[C++ PATCH]: instantiation via vtable marking
[official-gcc.git] / gcc / cp / class.c
blobe48a04ade7de9f32ddead55e83b8e1f992929f00
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));
282 if (code == PLUS_EXPR
283 && !SAME_BINFO_TYPE_P (BINFO_TYPE (d_binfo), probe))
285 /* This can happen when adjust_result_of_qualified_name_lookup can't
286 find a unique base binfo in a call to a member function. We
287 couldn't give the diagnostic then since we might have been calling
288 a static member function, so we do it now. In other cases, eg.
289 during error recovery (c++/71979), we may not have a base at all. */
290 if (complain & tf_error)
292 tree base = lookup_base (probe, BINFO_TYPE (d_binfo),
293 ba_unique, NULL, complain);
294 gcc_assert (base == error_mark_node || !base);
296 return error_mark_node;
299 gcc_assert ((code == MINUS_EXPR
300 && SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), probe))
301 || code == PLUS_EXPR);
303 if (binfo == d_binfo)
304 /* Nothing to do. */
305 return expr;
307 if (code == MINUS_EXPR && v_binfo)
309 if (complain & tf_error)
311 if (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), BINFO_TYPE (v_binfo)))
313 if (want_pointer)
314 error ("cannot convert from pointer to base class %qT to "
315 "pointer to derived class %qT because the base is "
316 "virtual", BINFO_TYPE (binfo), BINFO_TYPE (d_binfo));
317 else
318 error ("cannot convert from base class %qT to derived "
319 "class %qT because the base is virtual",
320 BINFO_TYPE (binfo), BINFO_TYPE (d_binfo));
322 else
324 if (want_pointer)
325 error ("cannot convert from pointer to base class %qT to "
326 "pointer to derived class %qT via virtual base %qT",
327 BINFO_TYPE (binfo), BINFO_TYPE (d_binfo),
328 BINFO_TYPE (v_binfo));
329 else
330 error ("cannot convert from base class %qT to derived "
331 "class %qT via virtual base %qT", BINFO_TYPE (binfo),
332 BINFO_TYPE (d_binfo), BINFO_TYPE (v_binfo));
335 return error_mark_node;
338 if (!want_pointer)
340 rvalue = !lvalue_p (expr);
341 /* This must happen before the call to save_expr. */
342 expr = cp_build_addr_expr (expr, complain);
344 else
345 expr = mark_rvalue_use (expr);
347 offset = BINFO_OFFSET (binfo);
348 fixed_type_p = resolves_to_fixed_type_p (expr, &nonnull);
349 target_type = code == PLUS_EXPR ? BINFO_TYPE (binfo) : BINFO_TYPE (d_binfo);
350 /* TARGET_TYPE has been extracted from BINFO, and, is therefore always
351 cv-unqualified. Extract the cv-qualifiers from EXPR so that the
352 expression returned matches the input. */
353 target_type = cp_build_qualified_type
354 (target_type, cp_type_quals (TREE_TYPE (TREE_TYPE (expr))));
355 ptr_target_type = build_pointer_type (target_type);
357 /* Do we need to look in the vtable for the real offset? */
358 virtual_access = (v_binfo && fixed_type_p <= 0);
360 /* Don't bother with the calculations inside sizeof; they'll ICE if the
361 source type is incomplete and the pointer value doesn't matter. In a
362 template (even in instantiate_non_dependent_expr), we don't have vtables
363 set up properly yet, and the value doesn't matter there either; we're
364 just interested in the result of overload resolution. */
365 if (cp_unevaluated_operand != 0
366 || processing_template_decl
367 || in_template_function ())
369 expr = build_nop (ptr_target_type, expr);
370 goto indout;
373 /* If we're in an NSDMI, we don't have the full constructor context yet
374 that we need for converting to a virtual base, so just build a stub
375 CONVERT_EXPR and expand it later in bot_replace. */
376 if (virtual_access && fixed_type_p < 0
377 && current_scope () != current_function_decl)
379 expr = build1 (CONVERT_EXPR, ptr_target_type, expr);
380 CONVERT_EXPR_VBASE_PATH (expr) = true;
381 goto indout;
384 /* Do we need to check for a null pointer? */
385 if (want_pointer && !nonnull)
387 /* If we know the conversion will not actually change the value
388 of EXPR, then we can avoid testing the expression for NULL.
389 We have to avoid generating a COMPONENT_REF for a base class
390 field, because other parts of the compiler know that such
391 expressions are always non-NULL. */
392 if (!virtual_access && integer_zerop (offset))
393 return build_nop (ptr_target_type, expr);
394 null_test = error_mark_node;
397 /* Protect against multiple evaluation if necessary. */
398 if (TREE_SIDE_EFFECTS (expr) && (null_test || virtual_access))
399 expr = save_expr (expr);
401 /* Now that we've saved expr, build the real null test. */
402 if (null_test)
404 tree zero = cp_convert (TREE_TYPE (expr), nullptr_node, complain);
405 null_test = build2_loc (input_location, NE_EXPR, boolean_type_node,
406 expr, zero);
407 /* This is a compiler generated comparison, don't emit
408 e.g. -Wnonnull-compare warning for it. */
409 TREE_NO_WARNING (null_test) = 1;
412 /* If this is a simple base reference, express it as a COMPONENT_REF. */
413 if (code == PLUS_EXPR && !virtual_access
414 /* We don't build base fields for empty bases, and they aren't very
415 interesting to the optimizers anyway. */
416 && !has_empty)
418 expr = cp_build_fold_indirect_ref (expr);
419 expr = build_simple_base_path (expr, binfo);
420 if (rvalue)
421 expr = move (expr);
422 if (want_pointer)
423 expr = build_address (expr);
424 target_type = TREE_TYPE (expr);
425 goto out;
428 if (virtual_access)
430 /* Going via virtual base V_BINFO. We need the static offset
431 from V_BINFO to BINFO, and the dynamic offset from D_BINFO to
432 V_BINFO. That offset is an entry in D_BINFO's vtable. */
433 tree v_offset;
435 if (fixed_type_p < 0 && in_base_initializer)
437 /* In a base member initializer, we cannot rely on the
438 vtable being set up. We have to indirect via the
439 vtt_parm. */
440 tree t;
442 t = TREE_TYPE (TYPE_VFIELD (current_class_type));
443 t = build_pointer_type (t);
444 v_offset = fold_convert (t, current_vtt_parm);
445 v_offset = cp_build_fold_indirect_ref (v_offset);
447 else
449 tree t = expr;
450 if (sanitize_flags_p (SANITIZE_VPTR)
451 && fixed_type_p == 0)
453 t = cp_ubsan_maybe_instrument_cast_to_vbase (input_location,
454 probe, expr);
455 if (t == NULL_TREE)
456 t = expr;
458 v_offset = build_vfield_ref (cp_build_fold_indirect_ref (t),
459 TREE_TYPE (TREE_TYPE (expr)));
462 if (v_offset == error_mark_node)
463 return error_mark_node;
465 v_offset = fold_build_pointer_plus (v_offset, BINFO_VPTR_FIELD (v_binfo));
466 v_offset = build1 (NOP_EXPR,
467 build_pointer_type (ptrdiff_type_node),
468 v_offset);
469 v_offset = cp_build_fold_indirect_ref (v_offset);
470 TREE_CONSTANT (v_offset) = 1;
472 offset = convert_to_integer (ptrdiff_type_node,
473 size_diffop_loc (input_location, offset,
474 BINFO_OFFSET (v_binfo)));
476 if (!integer_zerop (offset))
477 v_offset = build2 (code, ptrdiff_type_node, v_offset, offset);
479 if (fixed_type_p < 0)
480 /* Negative fixed_type_p means this is a constructor or destructor;
481 virtual base layout is fixed in in-charge [cd]tors, but not in
482 base [cd]tors. */
483 offset = build_if_in_charge
484 (convert_to_integer (ptrdiff_type_node, BINFO_OFFSET (binfo)),
485 v_offset);
486 else
487 offset = v_offset;
490 if (want_pointer)
491 target_type = ptr_target_type;
493 expr = build1 (NOP_EXPR, ptr_target_type, expr);
495 if (!integer_zerop (offset))
497 offset = fold_convert (sizetype, offset);
498 if (code == MINUS_EXPR)
499 offset = fold_build1_loc (input_location, NEGATE_EXPR, sizetype, offset);
500 expr = fold_build_pointer_plus (expr, offset);
502 else
503 null_test = NULL;
505 indout:
506 if (!want_pointer)
508 expr = cp_build_fold_indirect_ref (expr);
509 if (rvalue)
510 expr = move (expr);
513 out:
514 if (null_test)
515 expr = fold_build3_loc (input_location, COND_EXPR, target_type, null_test, expr,
516 build_zero_cst (target_type));
518 return expr;
521 /* Subroutine of build_base_path; EXPR and BINFO are as in that function.
522 Perform a derived-to-base conversion by recursively building up a
523 sequence of COMPONENT_REFs to the appropriate base fields. */
525 static tree
526 build_simple_base_path (tree expr, tree binfo)
528 tree type = BINFO_TYPE (binfo);
529 tree d_binfo = BINFO_INHERITANCE_CHAIN (binfo);
530 tree field;
532 if (d_binfo == NULL_TREE)
534 tree temp;
536 gcc_assert (TYPE_MAIN_VARIANT (TREE_TYPE (expr)) == type);
538 /* Transform `(a, b).x' into `(*(a, &b)).x', `(a ? b : c).x'
539 into `(*(a ? &b : &c)).x', and so on. A COND_EXPR is only
540 an lvalue in the front end; only _DECLs and _REFs are lvalues
541 in the back end. */
542 temp = unary_complex_lvalue (ADDR_EXPR, expr);
543 if (temp)
544 expr = cp_build_fold_indirect_ref (temp);
546 return expr;
549 /* Recurse. */
550 expr = build_simple_base_path (expr, d_binfo);
552 for (field = TYPE_FIELDS (BINFO_TYPE (d_binfo));
553 field; field = DECL_CHAIN (field))
554 /* Is this the base field created by build_base_field? */
555 if (TREE_CODE (field) == FIELD_DECL
556 && DECL_FIELD_IS_BASE (field)
557 && TREE_TYPE (field) == type
558 /* If we're looking for a field in the most-derived class,
559 also check the field offset; we can have two base fields
560 of the same type if one is an indirect virtual base and one
561 is a direct non-virtual base. */
562 && (BINFO_INHERITANCE_CHAIN (d_binfo)
563 || tree_int_cst_equal (byte_position (field),
564 BINFO_OFFSET (binfo))))
566 /* We don't use build_class_member_access_expr here, as that
567 has unnecessary checks, and more importantly results in
568 recursive calls to dfs_walk_once. */
569 int type_quals = cp_type_quals (TREE_TYPE (expr));
571 expr = build3 (COMPONENT_REF,
572 cp_build_qualified_type (type, type_quals),
573 expr, field, NULL_TREE);
574 /* Mark the expression const or volatile, as appropriate.
575 Even though we've dealt with the type above, we still have
576 to mark the expression itself. */
577 if (type_quals & TYPE_QUAL_CONST)
578 TREE_READONLY (expr) = 1;
579 if (type_quals & TYPE_QUAL_VOLATILE)
580 TREE_THIS_VOLATILE (expr) = 1;
582 return expr;
585 /* Didn't find the base field?!? */
586 gcc_unreachable ();
589 /* Convert OBJECT to the base TYPE. OBJECT is an expression whose
590 type is a class type or a pointer to a class type. In the former
591 case, TYPE is also a class type; in the latter it is another
592 pointer type. If CHECK_ACCESS is true, an error message is emitted
593 if TYPE is inaccessible. If OBJECT has pointer type, the value is
594 assumed to be non-NULL. */
596 tree
597 convert_to_base (tree object, tree type, bool check_access, bool nonnull,
598 tsubst_flags_t complain)
600 tree binfo;
601 tree object_type;
603 if (TYPE_PTR_P (TREE_TYPE (object)))
605 object_type = TREE_TYPE (TREE_TYPE (object));
606 type = TREE_TYPE (type);
608 else
609 object_type = TREE_TYPE (object);
611 binfo = lookup_base (object_type, type, check_access ? ba_check : ba_unique,
612 NULL, complain);
613 if (!binfo || binfo == error_mark_node)
614 return error_mark_node;
616 return build_base_path (PLUS_EXPR, object, binfo, nonnull, complain);
619 /* EXPR is an expression with unqualified class type. BASE is a base
620 binfo of that class type. Returns EXPR, converted to the BASE
621 type. This function assumes that EXPR is the most derived class;
622 therefore virtual bases can be found at their static offsets. */
624 tree
625 convert_to_base_statically (tree expr, tree base)
627 tree expr_type;
629 expr_type = TREE_TYPE (expr);
630 if (!SAME_BINFO_TYPE_P (BINFO_TYPE (base), expr_type))
632 /* If this is a non-empty base, use a COMPONENT_REF. */
633 if (!is_empty_class (BINFO_TYPE (base)))
634 return build_simple_base_path (expr, base);
636 /* We use fold_build2 and fold_convert below to simplify the trees
637 provided to the optimizers. It is not safe to call these functions
638 when processing a template because they do not handle C++-specific
639 trees. */
640 gcc_assert (!processing_template_decl);
641 expr = cp_build_addr_expr (expr, tf_warning_or_error);
642 if (!integer_zerop (BINFO_OFFSET (base)))
643 expr = fold_build_pointer_plus_loc (input_location,
644 expr, BINFO_OFFSET (base));
645 expr = fold_convert (build_pointer_type (BINFO_TYPE (base)), expr);
646 expr = build_fold_indirect_ref_loc (input_location, expr);
649 return expr;
653 tree
654 build_vfield_ref (tree datum, tree type)
656 tree vfield, vcontext;
658 if (datum == error_mark_node
659 /* Can happen in case of duplicate base types (c++/59082). */
660 || !TYPE_VFIELD (type))
661 return error_mark_node;
663 /* First, convert to the requested type. */
664 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (datum), type))
665 datum = convert_to_base (datum, type, /*check_access=*/false,
666 /*nonnull=*/true, tf_warning_or_error);
668 /* Second, the requested type may not be the owner of its own vptr.
669 If not, convert to the base class that owns it. We cannot use
670 convert_to_base here, because VCONTEXT may appear more than once
671 in the inheritance hierarchy of TYPE, and thus direct conversion
672 between the types may be ambiguous. Following the path back up
673 one step at a time via primary bases avoids the problem. */
674 vfield = TYPE_VFIELD (type);
675 vcontext = DECL_CONTEXT (vfield);
676 while (!same_type_ignoring_top_level_qualifiers_p (vcontext, type))
678 datum = build_simple_base_path (datum, CLASSTYPE_PRIMARY_BINFO (type));
679 type = TREE_TYPE (datum);
682 return build3 (COMPONENT_REF, TREE_TYPE (vfield), datum, vfield, NULL_TREE);
685 /* Given an object INSTANCE, return an expression which yields the
686 vtable element corresponding to INDEX. There are many special
687 cases for INSTANCE which we take care of here, mainly to avoid
688 creating extra tree nodes when we don't have to. */
690 static tree
691 build_vtbl_ref_1 (tree instance, tree idx)
693 tree aref;
694 tree vtbl = NULL_TREE;
696 /* Try to figure out what a reference refers to, and
697 access its virtual function table directly. */
699 int cdtorp = 0;
700 tree fixed_type = fixed_type_or_null (instance, NULL, &cdtorp);
702 tree basetype = non_reference (TREE_TYPE (instance));
704 if (fixed_type && !cdtorp)
706 tree binfo = lookup_base (fixed_type, basetype,
707 ba_unique, NULL, tf_none);
708 if (binfo && binfo != error_mark_node)
709 vtbl = unshare_expr (BINFO_VTABLE (binfo));
712 if (!vtbl)
713 vtbl = build_vfield_ref (instance, basetype);
715 aref = build_array_ref (input_location, vtbl, idx);
716 TREE_CONSTANT (aref) |= TREE_CONSTANT (vtbl) && TREE_CONSTANT (idx);
718 return aref;
721 tree
722 build_vtbl_ref (tree instance, tree idx)
724 tree aref = build_vtbl_ref_1 (instance, idx);
726 return aref;
729 /* Given a stable object pointer INSTANCE_PTR, return an expression which
730 yields a function pointer corresponding to vtable element INDEX. */
732 tree
733 build_vfn_ref (tree instance_ptr, tree idx)
735 tree aref;
737 aref = build_vtbl_ref_1 (cp_build_fold_indirect_ref (instance_ptr),
738 idx);
740 /* When using function descriptors, the address of the
741 vtable entry is treated as a function pointer. */
742 if (TARGET_VTABLE_USES_DESCRIPTORS)
743 aref = build1 (NOP_EXPR, TREE_TYPE (aref),
744 cp_build_addr_expr (aref, tf_warning_or_error));
746 /* Remember this as a method reference, for later devirtualization. */
747 aref = build3 (OBJ_TYPE_REF, TREE_TYPE (aref), aref, instance_ptr, idx);
749 return aref;
752 /* Return the name of the virtual function table (as an IDENTIFIER_NODE)
753 for the given TYPE. */
755 static tree
756 get_vtable_name (tree type)
758 return mangle_vtbl_for_type (type);
761 /* DECL is an entity associated with TYPE, like a virtual table or an
762 implicitly generated constructor. Determine whether or not DECL
763 should have external or internal linkage at the object file
764 level. This routine does not deal with COMDAT linkage and other
765 similar complexities; it simply sets TREE_PUBLIC if it possible for
766 entities in other translation units to contain copies of DECL, in
767 the abstract. */
769 void
770 set_linkage_according_to_type (tree /*type*/, tree decl)
772 TREE_PUBLIC (decl) = 1;
773 determine_visibility (decl);
776 /* Create a VAR_DECL for a primary or secondary vtable for CLASS_TYPE.
777 (For a secondary vtable for B-in-D, CLASS_TYPE should be D, not B.)
778 Use NAME for the name of the vtable, and VTABLE_TYPE for its type. */
780 static tree
781 build_vtable (tree class_type, tree name, tree vtable_type)
783 tree decl;
785 decl = build_lang_decl (VAR_DECL, name, vtable_type);
786 /* vtable names are already mangled; give them their DECL_ASSEMBLER_NAME
787 now to avoid confusion in mangle_decl. */
788 SET_DECL_ASSEMBLER_NAME (decl, name);
789 DECL_CONTEXT (decl) = class_type;
790 DECL_ARTIFICIAL (decl) = 1;
791 TREE_STATIC (decl) = 1;
792 TREE_READONLY (decl) = 1;
793 DECL_VIRTUAL_P (decl) = 1;
794 SET_DECL_ALIGN (decl, TARGET_VTABLE_ENTRY_ALIGN);
795 DECL_USER_ALIGN (decl) = true;
796 DECL_VTABLE_OR_VTT_P (decl) = 1;
797 set_linkage_according_to_type (class_type, decl);
798 /* The vtable has not been defined -- yet. */
799 DECL_EXTERNAL (decl) = 1;
800 DECL_NOT_REALLY_EXTERN (decl) = 1;
802 /* Mark the VAR_DECL node representing the vtable itself as a
803 "gratuitous" one, thereby forcing dwarfout.c to ignore it. It
804 is rather important that such things be ignored because any
805 effort to actually generate DWARF for them will run into
806 trouble when/if we encounter code like:
808 #pragma interface
809 struct S { virtual void member (); };
811 because the artificial declaration of the vtable itself (as
812 manufactured by the g++ front end) will say that the vtable is
813 a static member of `S' but only *after* the debug output for
814 the definition of `S' has already been output. This causes
815 grief because the DWARF entry for the definition of the vtable
816 will try to refer back to an earlier *declaration* of the
817 vtable as a static member of `S' and there won't be one. We
818 might be able to arrange to have the "vtable static member"
819 attached to the member list for `S' before the debug info for
820 `S' get written (which would solve the problem) but that would
821 require more intrusive changes to the g++ front end. */
822 DECL_IGNORED_P (decl) = 1;
824 return decl;
827 /* Get the VAR_DECL of the vtable for TYPE. TYPE need not be polymorphic,
828 or even complete. If this does not exist, create it. If COMPLETE is
829 nonzero, then complete the definition of it -- that will render it
830 impossible to actually build the vtable, but is useful to get at those
831 which are known to exist in the runtime. */
833 tree
834 get_vtable_decl (tree type, int complete)
836 tree decl;
838 if (CLASSTYPE_VTABLES (type))
839 return CLASSTYPE_VTABLES (type);
841 decl = build_vtable (type, get_vtable_name (type), vtbl_type_node);
842 CLASSTYPE_VTABLES (type) = decl;
844 if (complete)
846 DECL_EXTERNAL (decl) = 1;
847 cp_finish_decl (decl, NULL_TREE, false, NULL_TREE, 0);
850 return decl;
853 /* Build the primary virtual function table for TYPE. If BINFO is
854 non-NULL, build the vtable starting with the initial approximation
855 that it is the same as the one which is the head of the association
856 list. Returns a nonzero value if a new vtable is actually
857 created. */
859 static int
860 build_primary_vtable (tree binfo, tree type)
862 tree decl;
863 tree virtuals;
865 decl = get_vtable_decl (type, /*complete=*/0);
867 if (binfo)
869 if (BINFO_NEW_VTABLE_MARKED (binfo))
870 /* We have already created a vtable for this base, so there's
871 no need to do it again. */
872 return 0;
874 virtuals = copy_list (BINFO_VIRTUALS (binfo));
875 TREE_TYPE (decl) = TREE_TYPE (get_vtbl_decl_for_binfo (binfo));
876 DECL_SIZE (decl) = TYPE_SIZE (TREE_TYPE (decl));
877 DECL_SIZE_UNIT (decl) = TYPE_SIZE_UNIT (TREE_TYPE (decl));
879 else
881 gcc_assert (TREE_TYPE (decl) == vtbl_type_node);
882 virtuals = NULL_TREE;
885 /* Initialize the association list for this type, based
886 on our first approximation. */
887 BINFO_VTABLE (TYPE_BINFO (type)) = decl;
888 BINFO_VIRTUALS (TYPE_BINFO (type)) = virtuals;
889 SET_BINFO_NEW_VTABLE_MARKED (TYPE_BINFO (type));
890 return 1;
893 /* Give BINFO a new virtual function table which is initialized
894 with a skeleton-copy of its original initialization. The only
895 entry that changes is the `delta' entry, so we can really
896 share a lot of structure.
898 FOR_TYPE is the most derived type which caused this table to
899 be needed.
901 Returns nonzero if we haven't met BINFO before.
903 The order in which vtables are built (by calling this function) for
904 an object must remain the same, otherwise a binary incompatibility
905 can result. */
907 static int
908 build_secondary_vtable (tree binfo)
910 if (BINFO_NEW_VTABLE_MARKED (binfo))
911 /* We already created a vtable for this base. There's no need to
912 do it again. */
913 return 0;
915 /* Remember that we've created a vtable for this BINFO, so that we
916 don't try to do so again. */
917 SET_BINFO_NEW_VTABLE_MARKED (binfo);
919 /* Make fresh virtual list, so we can smash it later. */
920 BINFO_VIRTUALS (binfo) = copy_list (BINFO_VIRTUALS (binfo));
922 /* Secondary vtables are laid out as part of the same structure as
923 the primary vtable. */
924 BINFO_VTABLE (binfo) = NULL_TREE;
925 return 1;
928 /* Create a new vtable for BINFO which is the hierarchy dominated by
929 T. Return nonzero if we actually created a new vtable. */
931 static int
932 make_new_vtable (tree t, tree binfo)
934 if (binfo == TYPE_BINFO (t))
935 /* In this case, it is *type*'s vtable we are modifying. We start
936 with the approximation that its vtable is that of the
937 immediate base class. */
938 return build_primary_vtable (binfo, t);
939 else
940 /* This is our very own copy of `basetype' to play with. Later,
941 we will fill in all the virtual functions that override the
942 virtual functions in these base classes which are not defined
943 by the current type. */
944 return build_secondary_vtable (binfo);
947 /* Make *VIRTUALS, an entry on the BINFO_VIRTUALS list for BINFO
948 (which is in the hierarchy dominated by T) list FNDECL as its
949 BV_FN. DELTA is the required constant adjustment from the `this'
950 pointer where the vtable entry appears to the `this' required when
951 the function is actually called. */
953 static void
954 modify_vtable_entry (tree t,
955 tree binfo,
956 tree fndecl,
957 tree delta,
958 tree *virtuals)
960 tree v;
962 v = *virtuals;
964 if (fndecl != BV_FN (v)
965 || !tree_int_cst_equal (delta, BV_DELTA (v)))
967 /* We need a new vtable for BINFO. */
968 if (make_new_vtable (t, binfo))
970 /* If we really did make a new vtable, we also made a copy
971 of the BINFO_VIRTUALS list. Now, we have to find the
972 corresponding entry in that list. */
973 *virtuals = BINFO_VIRTUALS (binfo);
974 while (BV_FN (*virtuals) != BV_FN (v))
975 *virtuals = TREE_CHAIN (*virtuals);
976 v = *virtuals;
979 BV_DELTA (v) = delta;
980 BV_VCALL_INDEX (v) = NULL_TREE;
981 BV_FN (v) = fndecl;
986 /* Add method METHOD to class TYPE. If VIA_USING indicates whether
987 METHOD is being injected via a using_decl. Returns true if the
988 method could be added to the method vec. */
990 bool
991 add_method (tree type, tree method, bool via_using)
993 if (method == error_mark_node)
994 return false;
996 /* Maintain TYPE_HAS_USER_CONSTRUCTOR, etc. */
997 grok_special_member_properties (method);
999 tree *slot = get_member_slot (type, DECL_NAME (method));
1000 tree current_fns = *slot;
1002 gcc_assert (!DECL_EXTERN_C_P (method));
1004 /* Check to see if we've already got this method. */
1005 for (ovl_iterator iter (current_fns); iter; ++iter)
1007 tree fn = *iter;
1008 tree fn_type;
1009 tree method_type;
1010 tree parms1;
1011 tree parms2;
1013 if (TREE_CODE (fn) != TREE_CODE (method))
1014 continue;
1016 /* Two using-declarations can coexist, we'll complain about ambiguity in
1017 overload resolution. */
1018 if (via_using && iter.using_p ()
1019 /* Except handle inherited constructors specially. */
1020 && ! DECL_CONSTRUCTOR_P (fn))
1021 continue;
1023 /* [over.load] Member function declarations with the
1024 same name and the same parameter types cannot be
1025 overloaded if any of them is a static member
1026 function declaration.
1028 [over.load] Member function declarations with the same name and
1029 the same parameter-type-list as well as member function template
1030 declarations with the same name, the same parameter-type-list, and
1031 the same template parameter lists cannot be overloaded if any of
1032 them, but not all, have a ref-qualifier.
1034 [namespace.udecl] When a using-declaration brings names
1035 from a base class into a derived class scope, member
1036 functions in the derived class override and/or hide member
1037 functions with the same name and parameter types in a base
1038 class (rather than conflicting). */
1039 fn_type = TREE_TYPE (fn);
1040 method_type = TREE_TYPE (method);
1041 parms1 = TYPE_ARG_TYPES (fn_type);
1042 parms2 = TYPE_ARG_TYPES (method_type);
1044 /* Compare the quals on the 'this' parm. Don't compare
1045 the whole types, as used functions are treated as
1046 coming from the using class in overload resolution. */
1047 if (! DECL_STATIC_FUNCTION_P (fn)
1048 && ! DECL_STATIC_FUNCTION_P (method)
1049 /* Either both or neither need to be ref-qualified for
1050 differing quals to allow overloading. */
1051 && (FUNCTION_REF_QUALIFIED (fn_type)
1052 == FUNCTION_REF_QUALIFIED (method_type))
1053 && (type_memfn_quals (fn_type) != type_memfn_quals (method_type)
1054 || type_memfn_rqual (fn_type) != type_memfn_rqual (method_type)))
1055 continue;
1057 /* For templates, the return type and template parameters
1058 must be identical. */
1059 if (TREE_CODE (fn) == TEMPLATE_DECL
1060 && (!same_type_p (TREE_TYPE (fn_type),
1061 TREE_TYPE (method_type))
1062 || !comp_template_parms (DECL_TEMPLATE_PARMS (fn),
1063 DECL_TEMPLATE_PARMS (method))))
1064 continue;
1066 if (! DECL_STATIC_FUNCTION_P (fn))
1067 parms1 = TREE_CHAIN (parms1);
1068 if (! DECL_STATIC_FUNCTION_P (method))
1069 parms2 = TREE_CHAIN (parms2);
1071 /* Bring back parameters omitted from an inherited ctor. */
1072 if (ctor_omit_inherited_parms (fn))
1073 parms1 = FUNCTION_FIRST_USER_PARMTYPE (DECL_ORIGIN (fn));
1074 if (ctor_omit_inherited_parms (method))
1075 parms2 = FUNCTION_FIRST_USER_PARMTYPE (DECL_ORIGIN (method));
1077 if (compparms (parms1, parms2)
1078 && (!DECL_CONV_FN_P (fn)
1079 || same_type_p (TREE_TYPE (fn_type),
1080 TREE_TYPE (method_type)))
1081 && equivalently_constrained (fn, method))
1083 /* If these are versions of the same function, process and
1084 move on. */
1085 if (TREE_CODE (fn) == FUNCTION_DECL
1086 && maybe_version_functions (method, fn, true))
1087 continue;
1089 if (DECL_INHERITED_CTOR (method))
1091 if (DECL_INHERITED_CTOR (fn))
1093 tree basem = DECL_INHERITED_CTOR_BASE (method);
1094 tree basef = DECL_INHERITED_CTOR_BASE (fn);
1095 if (flag_new_inheriting_ctors)
1097 if (basem == basef)
1099 /* Inheriting the same constructor along different
1100 paths, combine them. */
1101 SET_DECL_INHERITED_CTOR
1102 (fn, ovl_make (DECL_INHERITED_CTOR (method),
1103 DECL_INHERITED_CTOR (fn)));
1104 /* And discard the new one. */
1105 return false;
1107 else
1108 /* Inherited ctors can coexist until overload
1109 resolution. */
1110 continue;
1112 error_at (DECL_SOURCE_LOCATION (method),
1113 "%q#D conflicts with version inherited from %qT",
1114 method, basef);
1115 inform (DECL_SOURCE_LOCATION (fn),
1116 "version inherited from %qT declared here",
1117 basef);
1119 /* Otherwise defer to the other function. */
1120 return false;
1123 if (via_using)
1124 /* Defer to the local function. */
1125 return false;
1126 else if (flag_new_inheriting_ctors
1127 && DECL_INHERITED_CTOR (fn))
1129 /* Remove the inherited constructor. */
1130 current_fns = iter.remove_node (current_fns);
1131 continue;
1133 else
1135 error_at (DECL_SOURCE_LOCATION (method),
1136 "%q#D cannot be overloaded with %q#D", method, fn);
1137 inform (DECL_SOURCE_LOCATION (fn),
1138 "previous declaration %q#D", fn);
1139 return false;
1144 /* A class should never have more than one destructor. */
1145 gcc_assert (!current_fns || !DECL_DESTRUCTOR_P (method));
1147 current_fns = ovl_insert (method, current_fns, via_using);
1149 if (!DECL_CONV_FN_P (method) && !COMPLETE_TYPE_P (type))
1150 push_class_level_binding (DECL_NAME (method), current_fns);
1152 *slot = current_fns;
1154 return true;
1157 /* Subroutines of finish_struct. */
1159 /* Change the access of FDECL to ACCESS in T. Return 1 if change was
1160 legit, otherwise return 0. */
1162 static int
1163 alter_access (tree t, tree fdecl, tree access)
1165 tree elem;
1167 retrofit_lang_decl (fdecl);
1169 gcc_assert (!DECL_DISCRIMINATOR_P (fdecl));
1171 elem = purpose_member (t, DECL_ACCESS (fdecl));
1172 if (elem)
1174 if (TREE_VALUE (elem) != access)
1176 if (TREE_CODE (TREE_TYPE (fdecl)) == FUNCTION_DECL)
1177 error ("conflicting access specifications for method"
1178 " %q+D, ignored", TREE_TYPE (fdecl));
1179 else
1180 error ("conflicting access specifications for field %qE, ignored",
1181 DECL_NAME (fdecl));
1183 else
1185 /* They're changing the access to the same thing they changed
1186 it to before. That's OK. */
1190 else
1192 perform_or_defer_access_check (TYPE_BINFO (t), fdecl, fdecl,
1193 tf_warning_or_error);
1194 DECL_ACCESS (fdecl) = tree_cons (t, access, DECL_ACCESS (fdecl));
1195 return 1;
1197 return 0;
1200 /* Return the access node for DECL's access in its enclosing class. */
1202 tree
1203 declared_access (tree decl)
1205 return (TREE_PRIVATE (decl) ? access_private_node
1206 : TREE_PROTECTED (decl) ? access_protected_node
1207 : access_public_node);
1210 /* Process the USING_DECL, which is a member of T. */
1212 static void
1213 handle_using_decl (tree using_decl, tree t)
1215 tree decl = USING_DECL_DECLS (using_decl);
1216 tree name = DECL_NAME (using_decl);
1217 tree access = declared_access (using_decl);
1218 tree flist = NULL_TREE;
1219 tree old_value;
1221 gcc_assert (!processing_template_decl && decl);
1223 old_value = lookup_member (t, name, /*protect=*/0, /*want_type=*/false,
1224 tf_warning_or_error);
1225 if (old_value)
1227 old_value = OVL_FIRST (old_value);
1229 if (DECL_P (old_value) && DECL_CONTEXT (old_value) == t)
1230 /* OK */;
1231 else
1232 old_value = NULL_TREE;
1235 cp_emit_debug_info_for_using (decl, t);
1237 if (is_overloaded_fn (decl))
1238 flist = decl;
1240 if (! old_value)
1242 else if (is_overloaded_fn (old_value))
1244 if (flist)
1245 /* It's OK to use functions from a base when there are functions with
1246 the same name already present in the current class. */;
1247 else
1249 error_at (DECL_SOURCE_LOCATION (using_decl), "%qD invalid in %q#T "
1250 "because of local method %q#D with same name",
1251 using_decl, t, old_value);
1252 inform (DECL_SOURCE_LOCATION (old_value),
1253 "local method %q#D declared here", old_value);
1254 return;
1257 else if (!DECL_ARTIFICIAL (old_value))
1259 error_at (DECL_SOURCE_LOCATION (using_decl), "%qD invalid in %q#T "
1260 "because of local member %q#D with same name",
1261 using_decl, t, old_value);
1262 inform (DECL_SOURCE_LOCATION (old_value),
1263 "local member %q#D declared here", old_value);
1264 return;
1267 /* Make type T see field decl FDECL with access ACCESS. */
1268 if (flist)
1269 for (ovl_iterator iter (flist); iter; ++iter)
1271 add_method (t, *iter, true);
1272 alter_access (t, *iter, access);
1274 else
1275 alter_access (t, decl, access);
1278 /* Data structure for find_abi_tags_r, below. */
1280 struct abi_tag_data
1282 tree t; // The type that we're checking for missing tags.
1283 tree subob; // The subobject of T that we're getting tags from.
1284 tree tags; // error_mark_node for diagnostics, or a list of missing tags.
1287 /* Subroutine of find_abi_tags_r. Handle a single TAG found on the class TP
1288 in the context of P. TAG can be either an identifier (the DECL_NAME of
1289 a tag NAMESPACE_DECL) or a STRING_CST (a tag attribute). */
1291 static void
1292 check_tag (tree tag, tree id, tree *tp, abi_tag_data *p)
1294 if (!IDENTIFIER_MARKED (id))
1296 if (p->tags != error_mark_node)
1298 /* We're collecting tags from template arguments or from
1299 the type of a variable or function return type. */
1300 p->tags = tree_cons (NULL_TREE, tag, p->tags);
1302 /* Don't inherit this tag multiple times. */
1303 IDENTIFIER_MARKED (id) = true;
1305 if (TYPE_P (p->t))
1307 /* Tags inherited from type template arguments are only used
1308 to avoid warnings. */
1309 ABI_TAG_IMPLICIT (p->tags) = true;
1310 return;
1312 /* For functions and variables we want to warn, too. */
1315 /* Otherwise we're diagnosing missing tags. */
1316 if (TREE_CODE (p->t) == FUNCTION_DECL)
1318 if (warning (OPT_Wabi_tag, "%qD inherits the %E ABI tag "
1319 "that %qT (used in its return type) has",
1320 p->t, tag, *tp))
1321 inform (location_of (*tp), "%qT declared here", *tp);
1323 else if (VAR_P (p->t))
1325 if (warning (OPT_Wabi_tag, "%qD inherits the %E ABI tag "
1326 "that %qT (used in its type) has", p->t, tag, *tp))
1327 inform (location_of (*tp), "%qT declared here", *tp);
1329 else if (TYPE_P (p->subob))
1331 if (warning (OPT_Wabi_tag, "%qT does not have the %E ABI tag "
1332 "that base %qT has", p->t, tag, p->subob))
1333 inform (location_of (p->subob), "%qT declared here",
1334 p->subob);
1336 else
1338 if (warning (OPT_Wabi_tag, "%qT does not have the %E ABI tag "
1339 "that %qT (used in the type of %qD) has",
1340 p->t, tag, *tp, p->subob))
1342 inform (location_of (p->subob), "%qD declared here",
1343 p->subob);
1344 inform (location_of (*tp), "%qT declared here", *tp);
1350 /* Find all the ABI tags in the attribute list ATTR and either call
1351 check_tag (if TP is non-null) or set IDENTIFIER_MARKED to val. */
1353 static void
1354 mark_or_check_attr_tags (tree attr, tree *tp, abi_tag_data *p, bool val)
1356 if (!attr)
1357 return;
1358 for (; (attr = lookup_attribute ("abi_tag", attr));
1359 attr = TREE_CHAIN (attr))
1360 for (tree list = TREE_VALUE (attr); list;
1361 list = TREE_CHAIN (list))
1363 tree tag = TREE_VALUE (list);
1364 tree id = get_identifier (TREE_STRING_POINTER (tag));
1365 if (tp)
1366 check_tag (tag, id, tp, p);
1367 else
1368 IDENTIFIER_MARKED (id) = val;
1372 /* Find all the ABI tags on T and its enclosing scopes and either call
1373 check_tag (if TP is non-null) or set IDENTIFIER_MARKED to val. */
1375 static void
1376 mark_or_check_tags (tree t, tree *tp, abi_tag_data *p, bool val)
1378 while (t != global_namespace)
1380 tree attr;
1381 if (TYPE_P (t))
1383 attr = TYPE_ATTRIBUTES (t);
1384 t = CP_TYPE_CONTEXT (t);
1386 else
1388 attr = DECL_ATTRIBUTES (t);
1389 t = CP_DECL_CONTEXT (t);
1391 mark_or_check_attr_tags (attr, tp, p, val);
1395 /* walk_tree callback for check_abi_tags: if the type at *TP involves any
1396 types with ABI tags, add the corresponding identifiers to the VEC in
1397 *DATA and set IDENTIFIER_MARKED. */
1399 static tree
1400 find_abi_tags_r (tree *tp, int *walk_subtrees, void *data)
1402 if (!OVERLOAD_TYPE_P (*tp))
1403 return NULL_TREE;
1405 /* walk_tree shouldn't be walking into any subtrees of a RECORD_TYPE
1406 anyway, but let's make sure of it. */
1407 *walk_subtrees = false;
1409 abi_tag_data *p = static_cast<struct abi_tag_data*>(data);
1411 mark_or_check_tags (*tp, tp, p, false);
1413 return NULL_TREE;
1416 /* walk_tree callback for mark_abi_tags: if *TP is a class, set
1417 IDENTIFIER_MARKED on its ABI tags. */
1419 static tree
1420 mark_abi_tags_r (tree *tp, int *walk_subtrees, void *data)
1422 if (!OVERLOAD_TYPE_P (*tp))
1423 return NULL_TREE;
1425 /* walk_tree shouldn't be walking into any subtrees of a RECORD_TYPE
1426 anyway, but let's make sure of it. */
1427 *walk_subtrees = false;
1429 bool *valp = static_cast<bool*>(data);
1431 mark_or_check_tags (*tp, NULL, NULL, *valp);
1433 return NULL_TREE;
1436 /* Set IDENTIFIER_MARKED on all the ABI tags on T and its enclosing
1437 scopes. */
1439 static void
1440 mark_abi_tags (tree t, bool val)
1442 mark_or_check_tags (t, NULL, NULL, val);
1443 if (DECL_P (t))
1445 if (DECL_LANG_SPECIFIC (t) && DECL_USE_TEMPLATE (t)
1446 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (t)))
1448 /* Template arguments are part of the signature. */
1449 tree level = INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (t));
1450 for (int j = 0; j < TREE_VEC_LENGTH (level); ++j)
1452 tree arg = TREE_VEC_ELT (level, j);
1453 cp_walk_tree_without_duplicates (&arg, mark_abi_tags_r, &val);
1456 if (TREE_CODE (t) == FUNCTION_DECL)
1457 /* A function's parameter types are part of the signature, so
1458 we don't need to inherit any tags that are also in them. */
1459 for (tree arg = FUNCTION_FIRST_USER_PARMTYPE (t); arg;
1460 arg = TREE_CHAIN (arg))
1461 cp_walk_tree_without_duplicates (&TREE_VALUE (arg),
1462 mark_abi_tags_r, &val);
1466 /* Check that T has all the ABI tags that subobject SUBOB has, or
1467 warn if not. If T is a (variable or function) declaration, also
1468 return any missing tags, and add them to T if JUST_CHECKING is false. */
1470 static tree
1471 check_abi_tags (tree t, tree subob, bool just_checking = false)
1473 bool inherit = DECL_P (t);
1475 if (!inherit && !warn_abi_tag)
1476 return NULL_TREE;
1478 tree decl = TYPE_P (t) ? TYPE_NAME (t) : t;
1479 if (!TREE_PUBLIC (decl))
1480 /* No need to worry about things local to this TU. */
1481 return NULL_TREE;
1483 mark_abi_tags (t, true);
1485 tree subtype = TYPE_P (subob) ? subob : TREE_TYPE (subob);
1486 struct abi_tag_data data = { t, subob, error_mark_node };
1487 if (inherit)
1488 data.tags = NULL_TREE;
1490 cp_walk_tree_without_duplicates (&subtype, find_abi_tags_r, &data);
1492 if (!(inherit && data.tags))
1493 /* We don't need to do anything with data.tags. */;
1494 else if (just_checking)
1495 for (tree t = data.tags; t; t = TREE_CHAIN (t))
1497 tree id = get_identifier (TREE_STRING_POINTER (TREE_VALUE (t)));
1498 IDENTIFIER_MARKED (id) = false;
1500 else
1502 tree attr = lookup_attribute ("abi_tag", DECL_ATTRIBUTES (t));
1503 if (attr)
1504 TREE_VALUE (attr) = chainon (data.tags, TREE_VALUE (attr));
1505 else
1506 DECL_ATTRIBUTES (t)
1507 = tree_cons (get_identifier ("abi_tag"), data.tags,
1508 DECL_ATTRIBUTES (t));
1511 mark_abi_tags (t, false);
1513 return data.tags;
1516 /* Check that DECL has all the ABI tags that are used in parts of its type
1517 that are not reflected in its mangled name. */
1519 void
1520 check_abi_tags (tree decl)
1522 if (VAR_P (decl))
1523 check_abi_tags (decl, TREE_TYPE (decl));
1524 else if (TREE_CODE (decl) == FUNCTION_DECL
1525 && !DECL_CONV_FN_P (decl)
1526 && !mangle_return_type_p (decl))
1527 check_abi_tags (decl, TREE_TYPE (TREE_TYPE (decl)));
1530 /* Return any ABI tags that are used in parts of the type of DECL
1531 that are not reflected in its mangled name. This function is only
1532 used in backward-compatible mangling for ABI <11. */
1534 tree
1535 missing_abi_tags (tree decl)
1537 if (VAR_P (decl))
1538 return check_abi_tags (decl, TREE_TYPE (decl), true);
1539 else if (TREE_CODE (decl) == FUNCTION_DECL
1540 /* Don't check DECL_CONV_FN_P here like we do in check_abi_tags, so
1541 that we can use this function for setting need_abi_warning
1542 regardless of the current flag_abi_version. */
1543 && !mangle_return_type_p (decl))
1544 return check_abi_tags (decl, TREE_TYPE (TREE_TYPE (decl)), true);
1545 else
1546 return NULL_TREE;
1549 void
1550 inherit_targ_abi_tags (tree t)
1552 if (!CLASS_TYPE_P (t)
1553 || CLASSTYPE_TEMPLATE_INFO (t) == NULL_TREE)
1554 return;
1556 mark_abi_tags (t, true);
1558 tree args = CLASSTYPE_TI_ARGS (t);
1559 struct abi_tag_data data = { t, NULL_TREE, NULL_TREE };
1560 for (int i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
1562 tree level = TMPL_ARGS_LEVEL (args, i+1);
1563 for (int j = 0; j < TREE_VEC_LENGTH (level); ++j)
1565 tree arg = TREE_VEC_ELT (level, j);
1566 data.subob = arg;
1567 cp_walk_tree_without_duplicates (&arg, find_abi_tags_r, &data);
1571 // If we found some tags on our template arguments, add them to our
1572 // abi_tag attribute.
1573 if (data.tags)
1575 tree attr = lookup_attribute ("abi_tag", TYPE_ATTRIBUTES (t));
1576 if (attr)
1577 TREE_VALUE (attr) = chainon (data.tags, TREE_VALUE (attr));
1578 else
1579 TYPE_ATTRIBUTES (t)
1580 = tree_cons (get_identifier ("abi_tag"), data.tags,
1581 TYPE_ATTRIBUTES (t));
1584 mark_abi_tags (t, false);
1587 /* Return true, iff class T has a non-virtual destructor that is
1588 accessible from outside the class heirarchy (i.e. is public, or
1589 there's a suitable friend. */
1591 static bool
1592 accessible_nvdtor_p (tree t)
1594 tree dtor = CLASSTYPE_DESTRUCTOR (t);
1596 /* An implicitly declared destructor is always public. And,
1597 if it were virtual, we would have created it by now. */
1598 if (!dtor)
1599 return true;
1601 if (DECL_VINDEX (dtor))
1602 return false; /* Virtual */
1604 if (!TREE_PRIVATE (dtor) && !TREE_PROTECTED (dtor))
1605 return true; /* Public */
1607 if (CLASSTYPE_FRIEND_CLASSES (t)
1608 || DECL_FRIENDLIST (TYPE_MAIN_DECL (t)))
1609 return true; /* Has friends */
1611 return false;
1614 /* Run through the base classes of T, updating CANT_HAVE_CONST_CTOR_P,
1615 and NO_CONST_ASN_REF_P. Also set flag bits in T based on
1616 properties of the bases. */
1618 static void
1619 check_bases (tree t,
1620 int* cant_have_const_ctor_p,
1621 int* no_const_asn_ref_p)
1623 int i;
1624 bool seen_non_virtual_nearly_empty_base_p = 0;
1625 int seen_tm_mask = 0;
1626 tree base_binfo;
1627 tree binfo;
1628 tree field = NULL_TREE;
1630 if (!CLASSTYPE_NON_STD_LAYOUT (t))
1631 for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
1632 if (TREE_CODE (field) == FIELD_DECL)
1633 break;
1635 for (binfo = TYPE_BINFO (t), i = 0;
1636 BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
1638 tree basetype = TREE_TYPE (base_binfo);
1640 gcc_assert (COMPLETE_TYPE_P (basetype));
1642 if (CLASSTYPE_FINAL (basetype))
1643 error ("cannot derive from %<final%> base %qT in derived type %qT",
1644 basetype, t);
1646 /* If any base class is non-literal, so is the derived class. */
1647 if (!CLASSTYPE_LITERAL_P (basetype))
1648 CLASSTYPE_LITERAL_P (t) = false;
1650 /* If the base class doesn't have copy constructors or
1651 assignment operators that take const references, then the
1652 derived class cannot have such a member automatically
1653 generated. */
1654 if (TYPE_HAS_COPY_CTOR (basetype)
1655 && ! TYPE_HAS_CONST_COPY_CTOR (basetype))
1656 *cant_have_const_ctor_p = 1;
1657 if (TYPE_HAS_COPY_ASSIGN (basetype)
1658 && !TYPE_HAS_CONST_COPY_ASSIGN (basetype))
1659 *no_const_asn_ref_p = 1;
1661 if (BINFO_VIRTUAL_P (base_binfo))
1662 /* A virtual base does not effect nearly emptiness. */
1664 else if (CLASSTYPE_NEARLY_EMPTY_P (basetype))
1666 if (seen_non_virtual_nearly_empty_base_p)
1667 /* And if there is more than one nearly empty base, then the
1668 derived class is not nearly empty either. */
1669 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
1670 else
1671 /* Remember we've seen one. */
1672 seen_non_virtual_nearly_empty_base_p = 1;
1674 else if (!is_empty_class (basetype))
1675 /* If the base class is not empty or nearly empty, then this
1676 class cannot be nearly empty. */
1677 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
1679 /* A lot of properties from the bases also apply to the derived
1680 class. */
1681 TYPE_NEEDS_CONSTRUCTING (t) |= TYPE_NEEDS_CONSTRUCTING (basetype);
1682 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t)
1683 |= TYPE_HAS_NONTRIVIAL_DESTRUCTOR (basetype);
1684 TYPE_HAS_COMPLEX_COPY_ASSIGN (t)
1685 |= (TYPE_HAS_COMPLEX_COPY_ASSIGN (basetype)
1686 || !TYPE_HAS_COPY_ASSIGN (basetype));
1687 TYPE_HAS_COMPLEX_COPY_CTOR (t) |= (TYPE_HAS_COMPLEX_COPY_CTOR (basetype)
1688 || !TYPE_HAS_COPY_CTOR (basetype));
1689 TYPE_HAS_COMPLEX_MOVE_ASSIGN (t)
1690 |= TYPE_HAS_COMPLEX_MOVE_ASSIGN (basetype);
1691 TYPE_HAS_COMPLEX_MOVE_CTOR (t) |= TYPE_HAS_COMPLEX_MOVE_CTOR (basetype);
1692 TYPE_POLYMORPHIC_P (t) |= TYPE_POLYMORPHIC_P (basetype);
1693 CLASSTYPE_CONTAINS_EMPTY_CLASS_P (t)
1694 |= CLASSTYPE_CONTAINS_EMPTY_CLASS_P (basetype);
1695 TYPE_HAS_COMPLEX_DFLT (t) |= (!TYPE_HAS_DEFAULT_CONSTRUCTOR (basetype)
1696 || TYPE_HAS_COMPLEX_DFLT (basetype));
1697 SET_CLASSTYPE_READONLY_FIELDS_NEED_INIT
1698 (t, CLASSTYPE_READONLY_FIELDS_NEED_INIT (t)
1699 | CLASSTYPE_READONLY_FIELDS_NEED_INIT (basetype));
1700 SET_CLASSTYPE_REF_FIELDS_NEED_INIT
1701 (t, CLASSTYPE_REF_FIELDS_NEED_INIT (t)
1702 | CLASSTYPE_REF_FIELDS_NEED_INIT (basetype));
1703 if (TYPE_HAS_MUTABLE_P (basetype))
1704 CLASSTYPE_HAS_MUTABLE (t) = 1;
1706 /* A standard-layout class is a class that:
1708 * has no non-standard-layout base classes, */
1709 CLASSTYPE_NON_STD_LAYOUT (t) |= CLASSTYPE_NON_STD_LAYOUT (basetype);
1710 if (!CLASSTYPE_NON_STD_LAYOUT (t))
1712 tree basefield;
1713 /* ...has no base classes of the same type as the first non-static
1714 data member... */
1715 if (field && DECL_CONTEXT (field) == t
1716 && (same_type_ignoring_top_level_qualifiers_p
1717 (TREE_TYPE (field), basetype)))
1718 CLASSTYPE_NON_STD_LAYOUT (t) = 1;
1719 else
1720 /* ...either has no non-static data members in the most-derived
1721 class and at most one base class with non-static data
1722 members, or has no base classes with non-static data
1723 members */
1724 for (basefield = TYPE_FIELDS (basetype); basefield;
1725 basefield = DECL_CHAIN (basefield))
1726 if (TREE_CODE (basefield) == FIELD_DECL
1727 && !(DECL_FIELD_IS_BASE (basefield)
1728 && integer_zerop (DECL_SIZE (basefield))))
1730 if (field)
1731 CLASSTYPE_NON_STD_LAYOUT (t) = 1;
1732 else
1733 field = basefield;
1734 break;
1738 /* Don't bother collecting tm attributes if transactional memory
1739 support is not enabled. */
1740 if (flag_tm)
1742 tree tm_attr = find_tm_attribute (TYPE_ATTRIBUTES (basetype));
1743 if (tm_attr)
1744 seen_tm_mask |= tm_attr_to_mask (tm_attr);
1747 check_abi_tags (t, basetype);
1750 /* If one of the base classes had TM attributes, and the current class
1751 doesn't define its own, then the current class inherits one. */
1752 if (seen_tm_mask && !find_tm_attribute (TYPE_ATTRIBUTES (t)))
1754 tree tm_attr = tm_mask_to_attr (least_bit_hwi (seen_tm_mask));
1755 TYPE_ATTRIBUTES (t) = tree_cons (tm_attr, NULL, TYPE_ATTRIBUTES (t));
1759 /* Determine all the primary bases within T. Sets BINFO_PRIMARY_BASE_P for
1760 those that are primaries. Sets BINFO_LOST_PRIMARY_P for those
1761 that have had a nearly-empty virtual primary base stolen by some
1762 other base in the hierarchy. Determines CLASSTYPE_PRIMARY_BASE for
1763 T. */
1765 static void
1766 determine_primary_bases (tree t)
1768 unsigned i;
1769 tree primary = NULL_TREE;
1770 tree type_binfo = TYPE_BINFO (t);
1771 tree base_binfo;
1773 /* Determine the primary bases of our bases. */
1774 for (base_binfo = TREE_CHAIN (type_binfo); base_binfo;
1775 base_binfo = TREE_CHAIN (base_binfo))
1777 tree primary = CLASSTYPE_PRIMARY_BINFO (BINFO_TYPE (base_binfo));
1779 /* See if we're the non-virtual primary of our inheritance
1780 chain. */
1781 if (!BINFO_VIRTUAL_P (base_binfo))
1783 tree parent = BINFO_INHERITANCE_CHAIN (base_binfo);
1784 tree parent_primary = CLASSTYPE_PRIMARY_BINFO (BINFO_TYPE (parent));
1786 if (parent_primary
1787 && SAME_BINFO_TYPE_P (BINFO_TYPE (base_binfo),
1788 BINFO_TYPE (parent_primary)))
1789 /* We are the primary binfo. */
1790 BINFO_PRIMARY_P (base_binfo) = 1;
1792 /* Determine if we have a virtual primary base, and mark it so.
1794 if (primary && BINFO_VIRTUAL_P (primary))
1796 tree this_primary = copied_binfo (primary, base_binfo);
1798 if (BINFO_PRIMARY_P (this_primary))
1799 /* Someone already claimed this base. */
1800 BINFO_LOST_PRIMARY_P (base_binfo) = 1;
1801 else
1803 tree delta;
1805 BINFO_PRIMARY_P (this_primary) = 1;
1806 BINFO_INHERITANCE_CHAIN (this_primary) = base_binfo;
1808 /* A virtual binfo might have been copied from within
1809 another hierarchy. As we're about to use it as a
1810 primary base, make sure the offsets match. */
1811 delta = size_diffop_loc (input_location,
1812 fold_convert (ssizetype,
1813 BINFO_OFFSET (base_binfo)),
1814 fold_convert (ssizetype,
1815 BINFO_OFFSET (this_primary)));
1817 propagate_binfo_offsets (this_primary, delta);
1822 /* First look for a dynamic direct non-virtual base. */
1823 for (i = 0; BINFO_BASE_ITERATE (type_binfo, i, base_binfo); i++)
1825 tree basetype = BINFO_TYPE (base_binfo);
1827 if (TYPE_CONTAINS_VPTR_P (basetype) && !BINFO_VIRTUAL_P (base_binfo))
1829 primary = base_binfo;
1830 goto found;
1834 /* A "nearly-empty" virtual base class can be the primary base
1835 class, if no non-virtual polymorphic base can be found. Look for
1836 a nearly-empty virtual dynamic base that is not already a primary
1837 base of something in the hierarchy. If there is no such base,
1838 just pick the first nearly-empty virtual base. */
1840 for (base_binfo = TREE_CHAIN (type_binfo); base_binfo;
1841 base_binfo = TREE_CHAIN (base_binfo))
1842 if (BINFO_VIRTUAL_P (base_binfo)
1843 && CLASSTYPE_NEARLY_EMPTY_P (BINFO_TYPE (base_binfo)))
1845 if (!BINFO_PRIMARY_P (base_binfo))
1847 /* Found one that is not primary. */
1848 primary = base_binfo;
1849 goto found;
1851 else if (!primary)
1852 /* Remember the first candidate. */
1853 primary = base_binfo;
1856 found:
1857 /* If we've got a primary base, use it. */
1858 if (primary)
1860 tree basetype = BINFO_TYPE (primary);
1862 CLASSTYPE_PRIMARY_BINFO (t) = primary;
1863 if (BINFO_PRIMARY_P (primary))
1864 /* We are stealing a primary base. */
1865 BINFO_LOST_PRIMARY_P (BINFO_INHERITANCE_CHAIN (primary)) = 1;
1866 BINFO_PRIMARY_P (primary) = 1;
1867 if (BINFO_VIRTUAL_P (primary))
1869 tree delta;
1871 BINFO_INHERITANCE_CHAIN (primary) = type_binfo;
1872 /* A virtual binfo might have been copied from within
1873 another hierarchy. As we're about to use it as a primary
1874 base, make sure the offsets match. */
1875 delta = size_diffop_loc (input_location, ssize_int (0),
1876 fold_convert (ssizetype, BINFO_OFFSET (primary)));
1878 propagate_binfo_offsets (primary, delta);
1881 primary = TYPE_BINFO (basetype);
1883 TYPE_VFIELD (t) = TYPE_VFIELD (basetype);
1884 BINFO_VTABLE (type_binfo) = BINFO_VTABLE (primary);
1885 BINFO_VIRTUALS (type_binfo) = BINFO_VIRTUALS (primary);
1889 /* Update the variant types of T. */
1891 void
1892 fixup_type_variants (tree t)
1894 tree variants;
1896 if (!t)
1897 return;
1899 for (variants = TYPE_NEXT_VARIANT (t);
1900 variants;
1901 variants = TYPE_NEXT_VARIANT (variants))
1903 /* These fields are in the _TYPE part of the node, not in
1904 the TYPE_LANG_SPECIFIC component, so they are not shared. */
1905 TYPE_HAS_USER_CONSTRUCTOR (variants) = TYPE_HAS_USER_CONSTRUCTOR (t);
1906 TYPE_NEEDS_CONSTRUCTING (variants) = TYPE_NEEDS_CONSTRUCTING (t);
1907 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (variants)
1908 = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t);
1910 TYPE_POLYMORPHIC_P (variants) = TYPE_POLYMORPHIC_P (t);
1912 TYPE_BINFO (variants) = TYPE_BINFO (t);
1914 /* Copy whatever these are holding today. */
1915 TYPE_VFIELD (variants) = TYPE_VFIELD (t);
1916 TYPE_FIELDS (variants) = TYPE_FIELDS (t);
1920 /* KLASS is a class that we're applying may_alias to after the body is
1921 parsed. Fixup any POINTER_TO and REFERENCE_TO types. The
1922 canonical type(s) will be implicitly updated. */
1924 static void
1925 fixup_may_alias (tree klass)
1927 tree t, v;
1929 for (t = TYPE_POINTER_TO (klass); t; t = TYPE_NEXT_PTR_TO (t))
1930 for (v = TYPE_MAIN_VARIANT (t); v; v = TYPE_NEXT_VARIANT (v))
1931 TYPE_REF_CAN_ALIAS_ALL (v) = true;
1932 for (t = TYPE_REFERENCE_TO (klass); t; t = TYPE_NEXT_REF_TO (t))
1933 for (v = TYPE_MAIN_VARIANT (t); v; v = TYPE_NEXT_VARIANT (v))
1934 TYPE_REF_CAN_ALIAS_ALL (v) = true;
1937 /* Early variant fixups: we apply attributes at the beginning of the class
1938 definition, and we need to fix up any variants that have already been
1939 made via elaborated-type-specifier so that check_qualified_type works. */
1941 void
1942 fixup_attribute_variants (tree t)
1944 tree variants;
1946 if (!t)
1947 return;
1949 tree attrs = TYPE_ATTRIBUTES (t);
1950 unsigned align = TYPE_ALIGN (t);
1951 bool user_align = TYPE_USER_ALIGN (t);
1952 bool may_alias = lookup_attribute ("may_alias", attrs);
1954 if (may_alias)
1955 fixup_may_alias (t);
1957 for (variants = TYPE_NEXT_VARIANT (t);
1958 variants;
1959 variants = TYPE_NEXT_VARIANT (variants))
1961 /* These are the two fields that check_qualified_type looks at and
1962 are affected by attributes. */
1963 TYPE_ATTRIBUTES (variants) = attrs;
1964 unsigned valign = align;
1965 if (TYPE_USER_ALIGN (variants))
1966 valign = MAX (valign, TYPE_ALIGN (variants));
1967 else
1968 TYPE_USER_ALIGN (variants) = user_align;
1969 SET_TYPE_ALIGN (variants, valign);
1970 if (may_alias)
1971 fixup_may_alias (variants);
1975 /* Set memoizing fields and bits of T (and its variants) for later
1976 use. */
1978 static void
1979 finish_struct_bits (tree t)
1981 /* Fix up variants (if any). */
1982 fixup_type_variants (t);
1984 if (BINFO_N_BASE_BINFOS (TYPE_BINFO (t)) && TYPE_POLYMORPHIC_P (t))
1985 /* For a class w/o baseclasses, 'finish_struct' has set
1986 CLASSTYPE_PURE_VIRTUALS correctly (by definition).
1987 Similarly for a class whose base classes do not have vtables.
1988 When neither of these is true, we might have removed abstract
1989 virtuals (by providing a definition), added some (by declaring
1990 new ones), or redeclared ones from a base class. We need to
1991 recalculate what's really an abstract virtual at this point (by
1992 looking in the vtables). */
1993 get_pure_virtuals (t);
1995 /* If this type has a copy constructor or a destructor, force its
1996 mode to be BLKmode, and force its TREE_ADDRESSABLE bit to be
1997 nonzero. This will cause it to be passed by invisible reference
1998 and prevent it from being returned in a register. */
1999 if (type_has_nontrivial_copy_init (t)
2000 || TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t))
2002 tree variants;
2003 SET_DECL_MODE (TYPE_MAIN_DECL (t), BLKmode);
2004 for (variants = t; variants; variants = TYPE_NEXT_VARIANT (variants))
2006 SET_TYPE_MODE (variants, BLKmode);
2007 TREE_ADDRESSABLE (variants) = 1;
2012 /* Issue warnings about T having private constructors, but no friends,
2013 and so forth.
2015 HAS_NONPRIVATE_METHOD is nonzero if T has any non-private methods or
2016 static members. HAS_NONPRIVATE_STATIC_FN is nonzero if T has any
2017 non-private static member functions. */
2019 static void
2020 maybe_warn_about_overly_private_class (tree t)
2022 int has_member_fn = 0;
2023 int has_nonprivate_method = 0;
2025 if (!warn_ctor_dtor_privacy
2026 /* If the class has friends, those entities might create and
2027 access instances, so we should not warn. */
2028 || (CLASSTYPE_FRIEND_CLASSES (t)
2029 || DECL_FRIENDLIST (TYPE_MAIN_DECL (t)))
2030 /* We will have warned when the template was declared; there's
2031 no need to warn on every instantiation. */
2032 || CLASSTYPE_TEMPLATE_INSTANTIATION (t))
2033 /* There's no reason to even consider warning about this
2034 class. */
2035 return;
2037 /* We only issue one warning, if more than one applies, because
2038 otherwise, on code like:
2040 class A {
2041 // Oops - forgot `public:'
2042 A();
2043 A(const A&);
2044 ~A();
2047 we warn several times about essentially the same problem. */
2049 /* Check to see if all (non-constructor, non-destructor) member
2050 functions are private. (Since there are no friends or
2051 non-private statics, we can't ever call any of the private member
2052 functions.) */
2053 for (tree fn = TYPE_FIELDS (t); fn; fn = DECL_CHAIN (fn))
2054 if (!DECL_DECLARES_FUNCTION_P (fn))
2055 /* Not a function. */;
2056 else if (DECL_ARTIFICIAL (fn))
2057 /* We're not interested in compiler-generated methods; they don't
2058 provide any way to call private members. */;
2059 else if (!TREE_PRIVATE (fn))
2061 if (DECL_STATIC_FUNCTION_P (fn))
2062 /* A non-private static member function is just like a
2063 friend; it can create and invoke private member
2064 functions, and be accessed without a class
2065 instance. */
2066 return;
2068 has_nonprivate_method = 1;
2069 /* Keep searching for a static member function. */
2071 else if (!DECL_CONSTRUCTOR_P (fn) && !DECL_DESTRUCTOR_P (fn))
2072 has_member_fn = 1;
2074 if (!has_nonprivate_method && has_member_fn)
2076 /* There are no non-private methods, and there's at least one
2077 private member function that isn't a constructor or
2078 destructor. (If all the private members are
2079 constructors/destructors we want to use the code below that
2080 issues error messages specifically referring to
2081 constructors/destructors.) */
2082 unsigned i;
2083 tree binfo = TYPE_BINFO (t);
2085 for (i = 0; i != BINFO_N_BASE_BINFOS (binfo); i++)
2086 if (BINFO_BASE_ACCESS (binfo, i) != access_private_node)
2088 has_nonprivate_method = 1;
2089 break;
2091 if (!has_nonprivate_method)
2093 warning (OPT_Wctor_dtor_privacy,
2094 "all member functions in class %qT are private", t);
2095 return;
2099 /* Even if some of the member functions are non-private, the class
2100 won't be useful for much if all the constructors or destructors
2101 are private: such an object can never be created or destroyed. */
2102 if (tree dtor = CLASSTYPE_DESTRUCTOR (t))
2103 if (TREE_PRIVATE (dtor))
2105 warning (OPT_Wctor_dtor_privacy,
2106 "%q#T only defines a private destructor and has no friends",
2108 return;
2111 /* Warn about classes that have private constructors and no friends. */
2112 if (TYPE_HAS_USER_CONSTRUCTOR (t)
2113 /* Implicitly generated constructors are always public. */
2114 && !CLASSTYPE_LAZY_DEFAULT_CTOR (t))
2116 bool nonprivate_ctor = false;
2117 tree copy_or_move = NULL_TREE;
2119 /* If a non-template class does not define a copy
2120 constructor, one is defined for it, enabling it to avoid
2121 this warning. For a template class, this does not
2122 happen, and so we would normally get a warning on:
2124 template <class T> class C { private: C(); };
2126 To avoid this asymmetry, we check TYPE_HAS_COPY_CTOR. All
2127 complete non-template or fully instantiated classes have this
2128 flag set. */
2129 if (!TYPE_HAS_COPY_CTOR (t))
2130 nonprivate_ctor = true;
2131 else
2132 for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (t));
2133 !nonprivate_ctor && iter; ++iter)
2134 if (TREE_PRIVATE (*iter))
2135 continue;
2136 else if (copy_fn_p (*iter) || move_fn_p (*iter))
2137 /* Ideally, we wouldn't count any constructor that takes
2138 an argument of the class type as a parameter, because
2139 such things cannot be used to construct an instance of
2140 the class unless you already have one. */
2141 copy_or_move = *iter;
2142 else
2143 nonprivate_ctor = true;
2145 if (!nonprivate_ctor)
2147 warning (OPT_Wctor_dtor_privacy,
2148 "%q#T only defines private constructors and has no friends",
2150 if (copy_or_move)
2151 inform (DECL_SOURCE_LOCATION (copy_or_move),
2152 "%q#D is public, but requires an existing %q#T object",
2153 copy_or_move, t);
2154 return;
2159 /* Make BINFO's vtable have N entries, including RTTI entries,
2160 vbase and vcall offsets, etc. Set its type and call the back end
2161 to lay it out. */
2163 static void
2164 layout_vtable_decl (tree binfo, int n)
2166 tree atype;
2167 tree vtable;
2169 atype = build_array_of_n_type (vtable_entry_type, n);
2170 layout_type (atype);
2172 /* We may have to grow the vtable. */
2173 vtable = get_vtbl_decl_for_binfo (binfo);
2174 if (!same_type_p (TREE_TYPE (vtable), atype))
2176 TREE_TYPE (vtable) = atype;
2177 DECL_SIZE (vtable) = DECL_SIZE_UNIT (vtable) = NULL_TREE;
2178 layout_decl (vtable, 0);
2182 /* True iff FNDECL and BASE_FNDECL (both non-static member functions)
2183 have the same signature. */
2186 same_signature_p (const_tree fndecl, const_tree base_fndecl)
2188 /* One destructor overrides another if they are the same kind of
2189 destructor. */
2190 if (DECL_DESTRUCTOR_P (base_fndecl) && DECL_DESTRUCTOR_P (fndecl)
2191 && special_function_p (base_fndecl) == special_function_p (fndecl))
2192 return 1;
2193 /* But a non-destructor never overrides a destructor, nor vice
2194 versa, nor do different kinds of destructors override
2195 one-another. For example, a complete object destructor does not
2196 override a deleting destructor. */
2197 if (DECL_DESTRUCTOR_P (base_fndecl) || DECL_DESTRUCTOR_P (fndecl))
2198 return 0;
2200 if (DECL_NAME (fndecl) == DECL_NAME (base_fndecl)
2201 || (DECL_CONV_FN_P (fndecl)
2202 && DECL_CONV_FN_P (base_fndecl)
2203 && same_type_p (DECL_CONV_FN_TYPE (fndecl),
2204 DECL_CONV_FN_TYPE (base_fndecl))))
2206 tree fntype = TREE_TYPE (fndecl);
2207 tree base_fntype = TREE_TYPE (base_fndecl);
2208 if (type_memfn_quals (fntype) == type_memfn_quals (base_fntype)
2209 && type_memfn_rqual (fntype) == type_memfn_rqual (base_fntype)
2210 && compparms (FUNCTION_FIRST_USER_PARMTYPE (fndecl),
2211 FUNCTION_FIRST_USER_PARMTYPE (base_fndecl)))
2212 return 1;
2214 return 0;
2217 /* Returns TRUE if DERIVED is a binfo containing the binfo BASE as a
2218 subobject. */
2220 static bool
2221 base_derived_from (tree derived, tree base)
2223 tree probe;
2225 for (probe = base; probe; probe = BINFO_INHERITANCE_CHAIN (probe))
2227 if (probe == derived)
2228 return true;
2229 else if (BINFO_VIRTUAL_P (probe))
2230 /* If we meet a virtual base, we can't follow the inheritance
2231 any more. See if the complete type of DERIVED contains
2232 such a virtual base. */
2233 return (binfo_for_vbase (BINFO_TYPE (probe), BINFO_TYPE (derived))
2234 != NULL_TREE);
2236 return false;
2239 struct find_final_overrider_data {
2240 /* The function for which we are trying to find a final overrider. */
2241 tree fn;
2242 /* The base class in which the function was declared. */
2243 tree declaring_base;
2244 /* The candidate overriders. */
2245 tree candidates;
2246 /* Path to most derived. */
2247 vec<tree> path;
2250 /* Add the overrider along the current path to FFOD->CANDIDATES.
2251 Returns true if an overrider was found; false otherwise. */
2253 static bool
2254 dfs_find_final_overrider_1 (tree binfo,
2255 find_final_overrider_data *ffod,
2256 unsigned depth)
2258 tree method;
2260 /* If BINFO is not the most derived type, try a more derived class.
2261 A definition there will overrider a definition here. */
2262 if (depth)
2264 depth--;
2265 if (dfs_find_final_overrider_1
2266 (ffod->path[depth], ffod, depth))
2267 return true;
2270 method = look_for_overrides_here (BINFO_TYPE (binfo), ffod->fn);
2271 if (method)
2273 tree *candidate = &ffod->candidates;
2275 /* Remove any candidates overridden by this new function. */
2276 while (*candidate)
2278 /* If *CANDIDATE overrides METHOD, then METHOD
2279 cannot override anything else on the list. */
2280 if (base_derived_from (TREE_VALUE (*candidate), binfo))
2281 return true;
2282 /* If METHOD overrides *CANDIDATE, remove *CANDIDATE. */
2283 if (base_derived_from (binfo, TREE_VALUE (*candidate)))
2284 *candidate = TREE_CHAIN (*candidate);
2285 else
2286 candidate = &TREE_CHAIN (*candidate);
2289 /* Add the new function. */
2290 ffod->candidates = tree_cons (method, binfo, ffod->candidates);
2291 return true;
2294 return false;
2297 /* Called from find_final_overrider via dfs_walk. */
2299 static tree
2300 dfs_find_final_overrider_pre (tree binfo, void *data)
2302 find_final_overrider_data *ffod = (find_final_overrider_data *) data;
2304 if (binfo == ffod->declaring_base)
2305 dfs_find_final_overrider_1 (binfo, ffod, ffod->path.length ());
2306 ffod->path.safe_push (binfo);
2308 return NULL_TREE;
2311 static tree
2312 dfs_find_final_overrider_post (tree /*binfo*/, void *data)
2314 find_final_overrider_data *ffod = (find_final_overrider_data *) data;
2315 ffod->path.pop ();
2317 return NULL_TREE;
2320 /* Returns a TREE_LIST whose TREE_PURPOSE is the final overrider for
2321 FN and whose TREE_VALUE is the binfo for the base where the
2322 overriding occurs. BINFO (in the hierarchy dominated by the binfo
2323 DERIVED) is the base object in which FN is declared. */
2325 static tree
2326 find_final_overrider (tree derived, tree binfo, tree fn)
2328 find_final_overrider_data ffod;
2330 /* Getting this right is a little tricky. This is valid:
2332 struct S { virtual void f (); };
2333 struct T { virtual void f (); };
2334 struct U : public S, public T { };
2336 even though calling `f' in `U' is ambiguous. But,
2338 struct R { virtual void f(); };
2339 struct S : virtual public R { virtual void f (); };
2340 struct T : virtual public R { virtual void f (); };
2341 struct U : public S, public T { };
2343 is not -- there's no way to decide whether to put `S::f' or
2344 `T::f' in the vtable for `R'.
2346 The solution is to look at all paths to BINFO. If we find
2347 different overriders along any two, then there is a problem. */
2348 if (DECL_THUNK_P (fn))
2349 fn = THUNK_TARGET (fn);
2351 /* Determine the depth of the hierarchy. */
2352 ffod.fn = fn;
2353 ffod.declaring_base = binfo;
2354 ffod.candidates = NULL_TREE;
2355 ffod.path.create (30);
2357 dfs_walk_all (derived, dfs_find_final_overrider_pre,
2358 dfs_find_final_overrider_post, &ffod);
2360 ffod.path.release ();
2362 /* If there was no winner, issue an error message. */
2363 if (!ffod.candidates || TREE_CHAIN (ffod.candidates))
2364 return error_mark_node;
2366 return ffod.candidates;
2369 /* Return the index of the vcall offset for FN when TYPE is used as a
2370 virtual base. */
2372 static tree
2373 get_vcall_index (tree fn, tree type)
2375 vec<tree_pair_s, va_gc> *indices = CLASSTYPE_VCALL_INDICES (type);
2376 tree_pair_p p;
2377 unsigned ix;
2379 FOR_EACH_VEC_SAFE_ELT (indices, ix, p)
2380 if ((DECL_DESTRUCTOR_P (fn) && DECL_DESTRUCTOR_P (p->purpose))
2381 || same_signature_p (fn, p->purpose))
2382 return p->value;
2384 /* There should always be an appropriate index. */
2385 gcc_unreachable ();
2388 /* Update an entry in the vtable for BINFO, which is in the hierarchy
2389 dominated by T. FN is the old function; VIRTUALS points to the
2390 corresponding position in the new BINFO_VIRTUALS list. IX is the index
2391 of that entry in the list. */
2393 static void
2394 update_vtable_entry_for_fn (tree t, tree binfo, tree fn, tree* virtuals,
2395 unsigned ix)
2397 tree b;
2398 tree overrider;
2399 tree delta;
2400 tree virtual_base;
2401 tree first_defn;
2402 tree overrider_fn, overrider_target;
2403 tree target_fn = DECL_THUNK_P (fn) ? THUNK_TARGET (fn) : fn;
2404 tree over_return, base_return;
2405 bool lost = false;
2407 /* Find the nearest primary base (possibly binfo itself) which defines
2408 this function; this is the class the caller will convert to when
2409 calling FN through BINFO. */
2410 for (b = binfo; ; b = get_primary_binfo (b))
2412 gcc_assert (b);
2413 if (look_for_overrides_here (BINFO_TYPE (b), target_fn))
2414 break;
2416 /* The nearest definition is from a lost primary. */
2417 if (BINFO_LOST_PRIMARY_P (b))
2418 lost = true;
2420 first_defn = b;
2422 /* Find the final overrider. */
2423 overrider = find_final_overrider (TYPE_BINFO (t), b, target_fn);
2424 if (overrider == error_mark_node)
2426 error ("no unique final overrider for %qD in %qT", target_fn, t);
2427 return;
2429 overrider_target = overrider_fn = TREE_PURPOSE (overrider);
2431 /* Check for adjusting covariant return types. */
2432 over_return = TREE_TYPE (TREE_TYPE (overrider_target));
2433 base_return = TREE_TYPE (TREE_TYPE (target_fn));
2435 if (POINTER_TYPE_P (over_return)
2436 && TREE_CODE (over_return) == TREE_CODE (base_return)
2437 && CLASS_TYPE_P (TREE_TYPE (over_return))
2438 && CLASS_TYPE_P (TREE_TYPE (base_return))
2439 /* If the overrider is invalid, don't even try. */
2440 && !DECL_INVALID_OVERRIDER_P (overrider_target))
2442 /* If FN is a covariant thunk, we must figure out the adjustment
2443 to the final base FN was converting to. As OVERRIDER_TARGET might
2444 also be converting to the return type of FN, we have to
2445 combine the two conversions here. */
2446 tree fixed_offset, virtual_offset;
2448 over_return = TREE_TYPE (over_return);
2449 base_return = TREE_TYPE (base_return);
2451 if (DECL_THUNK_P (fn))
2453 gcc_assert (DECL_RESULT_THUNK_P (fn));
2454 fixed_offset = ssize_int (THUNK_FIXED_OFFSET (fn));
2455 virtual_offset = THUNK_VIRTUAL_OFFSET (fn);
2457 else
2458 fixed_offset = virtual_offset = NULL_TREE;
2460 if (virtual_offset)
2461 /* Find the equivalent binfo within the return type of the
2462 overriding function. We will want the vbase offset from
2463 there. */
2464 virtual_offset = binfo_for_vbase (BINFO_TYPE (virtual_offset),
2465 over_return);
2466 else if (!same_type_ignoring_top_level_qualifiers_p
2467 (over_return, base_return))
2469 /* There was no existing virtual thunk (which takes
2470 precedence). So find the binfo of the base function's
2471 return type within the overriding function's return type.
2472 Fortunately we know the covariancy is valid (it
2473 has already been checked), so we can just iterate along
2474 the binfos, which have been chained in inheritance graph
2475 order. Of course it is lame that we have to repeat the
2476 search here anyway -- we should really be caching pieces
2477 of the vtable and avoiding this repeated work. */
2478 tree thunk_binfo, base_binfo;
2480 /* Find the base binfo within the overriding function's
2481 return type. We will always find a thunk_binfo, except
2482 when the covariancy is invalid (which we will have
2483 already diagnosed). */
2484 for (base_binfo = TYPE_BINFO (base_return),
2485 thunk_binfo = TYPE_BINFO (over_return);
2486 thunk_binfo;
2487 thunk_binfo = TREE_CHAIN (thunk_binfo))
2488 if (SAME_BINFO_TYPE_P (BINFO_TYPE (thunk_binfo),
2489 BINFO_TYPE (base_binfo)))
2490 break;
2492 /* See if virtual inheritance is involved. */
2493 for (virtual_offset = thunk_binfo;
2494 virtual_offset;
2495 virtual_offset = BINFO_INHERITANCE_CHAIN (virtual_offset))
2496 if (BINFO_VIRTUAL_P (virtual_offset))
2497 break;
2499 if (virtual_offset
2500 || (thunk_binfo && !BINFO_OFFSET_ZEROP (thunk_binfo)))
2502 tree offset = fold_convert (ssizetype, BINFO_OFFSET (thunk_binfo));
2504 if (virtual_offset)
2506 /* We convert via virtual base. Adjust the fixed
2507 offset to be from there. */
2508 offset =
2509 size_diffop (offset,
2510 fold_convert (ssizetype,
2511 BINFO_OFFSET (virtual_offset)));
2513 if (fixed_offset)
2514 /* There was an existing fixed offset, this must be
2515 from the base just converted to, and the base the
2516 FN was thunking to. */
2517 fixed_offset = size_binop (PLUS_EXPR, fixed_offset, offset);
2518 else
2519 fixed_offset = offset;
2523 if (fixed_offset || virtual_offset)
2524 /* Replace the overriding function with a covariant thunk. We
2525 will emit the overriding function in its own slot as
2526 well. */
2527 overrider_fn = make_thunk (overrider_target, /*this_adjusting=*/0,
2528 fixed_offset, virtual_offset);
2530 else
2531 gcc_assert (DECL_INVALID_OVERRIDER_P (overrider_target) ||
2532 !DECL_THUNK_P (fn));
2534 /* If we need a covariant thunk, then we may need to adjust first_defn.
2535 The ABI specifies that the thunks emitted with a function are
2536 determined by which bases the function overrides, so we need to be
2537 sure that we're using a thunk for some overridden base; even if we
2538 know that the necessary this adjustment is zero, there may not be an
2539 appropriate zero-this-adjustment thunk for us to use since thunks for
2540 overriding virtual bases always use the vcall offset.
2542 Furthermore, just choosing any base that overrides this function isn't
2543 quite right, as this slot won't be used for calls through a type that
2544 puts a covariant thunk here. Calling the function through such a type
2545 will use a different slot, and that slot is the one that determines
2546 the thunk emitted for that base.
2548 So, keep looking until we find the base that we're really overriding
2549 in this slot: the nearest primary base that doesn't use a covariant
2550 thunk in this slot. */
2551 if (overrider_target != overrider_fn)
2553 if (BINFO_TYPE (b) == DECL_CONTEXT (overrider_target))
2554 /* We already know that the overrider needs a covariant thunk. */
2555 b = get_primary_binfo (b);
2556 for (; ; b = get_primary_binfo (b))
2558 tree main_binfo = TYPE_BINFO (BINFO_TYPE (b));
2559 tree bv = chain_index (ix, BINFO_VIRTUALS (main_binfo));
2560 if (!DECL_THUNK_P (TREE_VALUE (bv)))
2561 break;
2562 if (BINFO_LOST_PRIMARY_P (b))
2563 lost = true;
2565 first_defn = b;
2568 /* Assume that we will produce a thunk that convert all the way to
2569 the final overrider, and not to an intermediate virtual base. */
2570 virtual_base = NULL_TREE;
2572 /* See if we can convert to an intermediate virtual base first, and then
2573 use the vcall offset located there to finish the conversion. */
2574 for (; b; b = BINFO_INHERITANCE_CHAIN (b))
2576 /* If we find the final overrider, then we can stop
2577 walking. */
2578 if (SAME_BINFO_TYPE_P (BINFO_TYPE (b),
2579 BINFO_TYPE (TREE_VALUE (overrider))))
2580 break;
2582 /* If we find a virtual base, and we haven't yet found the
2583 overrider, then there is a virtual base between the
2584 declaring base (first_defn) and the final overrider. */
2585 if (BINFO_VIRTUAL_P (b))
2587 virtual_base = b;
2588 break;
2592 /* Compute the constant adjustment to the `this' pointer. The
2593 `this' pointer, when this function is called, will point at BINFO
2594 (or one of its primary bases, which are at the same offset). */
2595 if (virtual_base)
2596 /* The `this' pointer needs to be adjusted from the declaration to
2597 the nearest virtual base. */
2598 delta = size_diffop_loc (input_location,
2599 fold_convert (ssizetype, BINFO_OFFSET (virtual_base)),
2600 fold_convert (ssizetype, BINFO_OFFSET (first_defn)));
2601 else if (lost)
2602 /* If the nearest definition is in a lost primary, we don't need an
2603 entry in our vtable. Except possibly in a constructor vtable,
2604 if we happen to get our primary back. In that case, the offset
2605 will be zero, as it will be a primary base. */
2606 delta = size_zero_node;
2607 else
2608 /* The `this' pointer needs to be adjusted from pointing to
2609 BINFO to pointing at the base where the final overrider
2610 appears. */
2611 delta = size_diffop_loc (input_location,
2612 fold_convert (ssizetype,
2613 BINFO_OFFSET (TREE_VALUE (overrider))),
2614 fold_convert (ssizetype, BINFO_OFFSET (binfo)));
2616 modify_vtable_entry (t, binfo, overrider_fn, delta, virtuals);
2618 if (virtual_base)
2619 BV_VCALL_INDEX (*virtuals)
2620 = get_vcall_index (overrider_target, BINFO_TYPE (virtual_base));
2621 else
2622 BV_VCALL_INDEX (*virtuals) = NULL_TREE;
2624 BV_LOST_PRIMARY (*virtuals) = lost;
2627 /* Called from modify_all_vtables via dfs_walk. */
2629 static tree
2630 dfs_modify_vtables (tree binfo, void* data)
2632 tree t = (tree) data;
2633 tree virtuals;
2634 tree old_virtuals;
2635 unsigned ix;
2637 if (!TYPE_CONTAINS_VPTR_P (BINFO_TYPE (binfo)))
2638 /* A base without a vtable needs no modification, and its bases
2639 are uninteresting. */
2640 return dfs_skip_bases;
2642 if (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), t)
2643 && !CLASSTYPE_HAS_PRIMARY_BASE_P (t))
2644 /* Don't do the primary vtable, if it's new. */
2645 return NULL_TREE;
2647 if (BINFO_PRIMARY_P (binfo) && !BINFO_VIRTUAL_P (binfo))
2648 /* There's no need to modify the vtable for a non-virtual primary
2649 base; we're not going to use that vtable anyhow. We do still
2650 need to do this for virtual primary bases, as they could become
2651 non-primary in a construction vtable. */
2652 return NULL_TREE;
2654 make_new_vtable (t, binfo);
2656 /* Now, go through each of the virtual functions in the virtual
2657 function table for BINFO. Find the final overrider, and update
2658 the BINFO_VIRTUALS list appropriately. */
2659 for (ix = 0, virtuals = BINFO_VIRTUALS (binfo),
2660 old_virtuals = BINFO_VIRTUALS (TYPE_BINFO (BINFO_TYPE (binfo)));
2661 virtuals;
2662 ix++, virtuals = TREE_CHAIN (virtuals),
2663 old_virtuals = TREE_CHAIN (old_virtuals))
2664 update_vtable_entry_for_fn (t,
2665 binfo,
2666 BV_FN (old_virtuals),
2667 &virtuals, ix);
2669 return NULL_TREE;
2672 /* Update all of the primary and secondary vtables for T. Create new
2673 vtables as required, and initialize their RTTI information. Each
2674 of the functions in VIRTUALS is declared in T and may override a
2675 virtual function from a base class; find and modify the appropriate
2676 entries to point to the overriding functions. Returns a list, in
2677 declaration order, of the virtual functions that are declared in T,
2678 but do not appear in the primary base class vtable, and which
2679 should therefore be appended to the end of the vtable for T. */
2681 static tree
2682 modify_all_vtables (tree t, tree virtuals)
2684 tree binfo = TYPE_BINFO (t);
2685 tree *fnsp;
2687 /* Mangle the vtable name before entering dfs_walk (c++/51884). */
2688 if (TYPE_CONTAINS_VPTR_P (t))
2689 get_vtable_decl (t, false);
2691 /* Update all of the vtables. */
2692 dfs_walk_once (binfo, dfs_modify_vtables, NULL, t);
2694 /* Add virtual functions not already in our primary vtable. These
2695 will be both those introduced by this class, and those overridden
2696 from secondary bases. It does not include virtuals merely
2697 inherited from secondary bases. */
2698 for (fnsp = &virtuals; *fnsp; )
2700 tree fn = TREE_VALUE (*fnsp);
2702 if (!value_member (fn, BINFO_VIRTUALS (binfo))
2703 || DECL_VINDEX (fn) == error_mark_node)
2705 /* We don't need to adjust the `this' pointer when
2706 calling this function. */
2707 BV_DELTA (*fnsp) = integer_zero_node;
2708 BV_VCALL_INDEX (*fnsp) = NULL_TREE;
2710 /* This is a function not already in our vtable. Keep it. */
2711 fnsp = &TREE_CHAIN (*fnsp);
2713 else
2714 /* We've already got an entry for this function. Skip it. */
2715 *fnsp = TREE_CHAIN (*fnsp);
2718 return virtuals;
2721 /* Get the base virtual function declarations in T that have the
2722 indicated NAME. */
2724 static void
2725 get_basefndecls (tree name, tree t, vec<tree> *base_fndecls)
2727 bool found_decls = false;
2729 /* Find virtual functions in T with the indicated NAME. */
2730 for (ovl_iterator iter (get_class_binding (t, name)); iter; ++iter)
2732 tree method = *iter;
2734 if (TREE_CODE (method) == FUNCTION_DECL && DECL_VINDEX (method))
2736 base_fndecls->safe_push (method);
2737 found_decls = true;
2741 if (found_decls)
2742 return;
2744 int n_baseclasses = BINFO_N_BASE_BINFOS (TYPE_BINFO (t));
2745 for (int i = 0; i < n_baseclasses; i++)
2747 tree basetype = BINFO_TYPE (BINFO_BASE_BINFO (TYPE_BINFO (t), i));
2748 get_basefndecls (name, basetype, base_fndecls);
2752 /* If this declaration supersedes the declaration of
2753 a method declared virtual in the base class, then
2754 mark this field as being virtual as well. */
2756 void
2757 check_for_override (tree decl, tree ctype)
2759 bool overrides_found = false;
2760 if (TREE_CODE (decl) == TEMPLATE_DECL)
2761 /* In [temp.mem] we have:
2763 A specialization of a member function template does not
2764 override a virtual function from a base class. */
2765 return;
2766 if ((DECL_DESTRUCTOR_P (decl)
2767 || IDENTIFIER_VIRTUAL_P (DECL_NAME (decl))
2768 || DECL_CONV_FN_P (decl))
2769 && look_for_overrides (ctype, decl)
2770 && !DECL_STATIC_FUNCTION_P (decl))
2771 /* Set DECL_VINDEX to a value that is neither an INTEGER_CST nor
2772 the error_mark_node so that we know it is an overriding
2773 function. */
2775 DECL_VINDEX (decl) = decl;
2776 overrides_found = true;
2777 if (warn_override && !DECL_OVERRIDE_P (decl)
2778 && !DECL_DESTRUCTOR_P (decl))
2779 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wsuggest_override,
2780 "%qD can be marked override", decl);
2783 if (DECL_VIRTUAL_P (decl))
2785 if (!DECL_VINDEX (decl))
2786 DECL_VINDEX (decl) = error_mark_node;
2787 IDENTIFIER_VIRTUAL_P (DECL_NAME (decl)) = 1;
2788 if (DECL_DESTRUCTOR_P (decl))
2789 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (ctype) = true;
2791 else if (DECL_FINAL_P (decl))
2792 error ("%q+#D marked %<final%>, but is not virtual", decl);
2793 if (DECL_OVERRIDE_P (decl) && !overrides_found)
2794 error ("%q+#D marked %<override%>, but does not override", decl);
2797 /* Warn about hidden virtual functions that are not overridden in t.
2798 We know that constructors and destructors don't apply. */
2800 static void
2801 warn_hidden (tree t)
2803 if (vec<tree, va_gc> *member_vec = CLASSTYPE_MEMBER_VEC (t))
2804 for (unsigned ix = member_vec->length (); ix--;)
2806 tree fns = (*member_vec)[ix];
2808 if (!OVL_P (fns))
2809 continue;
2811 tree name = OVL_NAME (fns);
2812 auto_vec<tree, 20> base_fndecls;
2813 tree base_binfo;
2814 tree binfo;
2815 unsigned j;
2817 /* Iterate through all of the base classes looking for possibly
2818 hidden functions. */
2819 for (binfo = TYPE_BINFO (t), j = 0;
2820 BINFO_BASE_ITERATE (binfo, j, base_binfo); j++)
2822 tree basetype = BINFO_TYPE (base_binfo);
2823 get_basefndecls (name, basetype, &base_fndecls);
2826 /* If there are no functions to hide, continue. */
2827 if (base_fndecls.is_empty ())
2828 continue;
2830 /* Remove any overridden functions. */
2831 for (ovl_iterator iter (fns); iter; ++iter)
2833 tree fndecl = *iter;
2834 if (TREE_CODE (fndecl) == FUNCTION_DECL
2835 && DECL_VINDEX (fndecl))
2837 /* If the method from the base class has the same
2838 signature as the method from the derived class, it
2839 has been overridden. */
2840 for (size_t k = 0; k < base_fndecls.length (); k++)
2841 if (base_fndecls[k]
2842 && same_signature_p (fndecl, base_fndecls[k]))
2843 base_fndecls[k] = NULL_TREE;
2847 /* Now give a warning for all base functions without overriders,
2848 as they are hidden. */
2849 tree base_fndecl;
2850 FOR_EACH_VEC_ELT (base_fndecls, j, base_fndecl)
2851 if (base_fndecl)
2853 /* Here we know it is a hider, and no overrider exists. */
2854 warning_at (location_of (base_fndecl),
2855 OPT_Woverloaded_virtual,
2856 "%qD was hidden", base_fndecl);
2857 warning_at (location_of (fns),
2858 OPT_Woverloaded_virtual, " by %qD", fns);
2863 /* Recursive helper for finish_struct_anon. */
2865 static void
2866 finish_struct_anon_r (tree field, bool complain)
2868 bool is_union = TREE_CODE (TREE_TYPE (field)) == UNION_TYPE;
2869 tree elt = TYPE_FIELDS (TREE_TYPE (field));
2870 for (; elt; elt = DECL_CHAIN (elt))
2872 /* We're generally only interested in entities the user
2873 declared, but we also find nested classes by noticing
2874 the TYPE_DECL that we create implicitly. You're
2875 allowed to put one anonymous union inside another,
2876 though, so we explicitly tolerate that. We use
2877 TYPE_UNNAMED_P rather than ANON_AGGR_TYPE_P so that
2878 we also allow unnamed types used for defining fields. */
2879 if (DECL_ARTIFICIAL (elt)
2880 && (!DECL_IMPLICIT_TYPEDEF_P (elt)
2881 || TYPE_UNNAMED_P (TREE_TYPE (elt))))
2882 continue;
2884 if (TREE_CODE (elt) != FIELD_DECL)
2886 /* We already complained about static data members in
2887 finish_static_data_member_decl. */
2888 if (complain && !VAR_P (elt))
2890 if (is_union)
2891 permerror (DECL_SOURCE_LOCATION (elt),
2892 "%q#D invalid; an anonymous union can "
2893 "only have non-static data members", elt);
2894 else
2895 permerror (DECL_SOURCE_LOCATION (elt),
2896 "%q#D invalid; an anonymous struct can "
2897 "only have non-static data members", elt);
2899 continue;
2902 if (complain)
2904 if (TREE_PRIVATE (elt))
2906 if (is_union)
2907 permerror (DECL_SOURCE_LOCATION (elt),
2908 "private member %q#D in anonymous union", elt);
2909 else
2910 permerror (DECL_SOURCE_LOCATION (elt),
2911 "private member %q#D in anonymous struct", elt);
2913 else if (TREE_PROTECTED (elt))
2915 if (is_union)
2916 permerror (DECL_SOURCE_LOCATION (elt),
2917 "protected member %q#D in anonymous union", elt);
2918 else
2919 permerror (DECL_SOURCE_LOCATION (elt),
2920 "protected member %q#D in anonymous struct", elt);
2924 TREE_PRIVATE (elt) = TREE_PRIVATE (field);
2925 TREE_PROTECTED (elt) = TREE_PROTECTED (field);
2927 /* Recurse into the anonymous aggregates to handle correctly
2928 access control (c++/24926):
2930 class A {
2931 union {
2932 union {
2933 int i;
2938 int j=A().i; */
2939 if (DECL_NAME (elt) == NULL_TREE
2940 && ANON_AGGR_TYPE_P (TREE_TYPE (elt)))
2941 finish_struct_anon_r (elt, /*complain=*/false);
2945 /* Check for things that are invalid. There are probably plenty of other
2946 things we should check for also. */
2948 static void
2949 finish_struct_anon (tree t)
2951 for (tree field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
2953 if (TREE_STATIC (field))
2954 continue;
2955 if (TREE_CODE (field) != FIELD_DECL)
2956 continue;
2958 if (DECL_NAME (field) == NULL_TREE
2959 && ANON_AGGR_TYPE_P (TREE_TYPE (field)))
2960 finish_struct_anon_r (field, /*complain=*/true);
2964 /* Add T to CLASSTYPE_DECL_LIST of current_class_type which
2965 will be used later during class template instantiation.
2966 When FRIEND_P is zero, T can be a static member data (VAR_DECL),
2967 a non-static member data (FIELD_DECL), a member function
2968 (FUNCTION_DECL), a nested type (RECORD_TYPE, ENUM_TYPE),
2969 a typedef (TYPE_DECL) or a member class template (TEMPLATE_DECL)
2970 When FRIEND_P is nonzero, T is either a friend class
2971 (RECORD_TYPE, TEMPLATE_DECL) or a friend function
2972 (FUNCTION_DECL, TEMPLATE_DECL). */
2974 void
2975 maybe_add_class_template_decl_list (tree type, tree t, int friend_p)
2977 /* Save some memory by not creating TREE_LIST if TYPE is not template. */
2978 if (CLASSTYPE_TEMPLATE_INFO (type))
2979 CLASSTYPE_DECL_LIST (type)
2980 = tree_cons (friend_p ? NULL_TREE : type,
2981 t, CLASSTYPE_DECL_LIST (type));
2984 /* This function is called from declare_virt_assop_and_dtor via
2985 dfs_walk_all.
2987 DATA is a type that direcly or indirectly inherits the base
2988 represented by BINFO. If BINFO contains a virtual assignment [copy
2989 assignment or move assigment] operator or a virtual constructor,
2990 declare that function in DATA if it hasn't been already declared. */
2992 static tree
2993 dfs_declare_virt_assop_and_dtor (tree binfo, void *data)
2995 tree bv, fn, t = (tree)data;
2996 tree opname = assign_op_identifier;
2998 gcc_assert (t && CLASS_TYPE_P (t));
2999 gcc_assert (binfo && TREE_CODE (binfo) == TREE_BINFO);
3001 if (!TYPE_CONTAINS_VPTR_P (BINFO_TYPE (binfo)))
3002 /* A base without a vtable needs no modification, and its bases
3003 are uninteresting. */
3004 return dfs_skip_bases;
3006 if (BINFO_PRIMARY_P (binfo))
3007 /* If this is a primary base, then we have already looked at the
3008 virtual functions of its vtable. */
3009 return NULL_TREE;
3011 for (bv = BINFO_VIRTUALS (binfo); bv; bv = TREE_CHAIN (bv))
3013 fn = BV_FN (bv);
3015 if (DECL_NAME (fn) == opname)
3017 if (CLASSTYPE_LAZY_COPY_ASSIGN (t))
3018 lazily_declare_fn (sfk_copy_assignment, t);
3019 if (CLASSTYPE_LAZY_MOVE_ASSIGN (t))
3020 lazily_declare_fn (sfk_move_assignment, t);
3022 else if (DECL_DESTRUCTOR_P (fn)
3023 && CLASSTYPE_LAZY_DESTRUCTOR (t))
3024 lazily_declare_fn (sfk_destructor, t);
3027 return NULL_TREE;
3030 /* If the class type T has a direct or indirect base that contains a
3031 virtual assignment operator or a virtual destructor, declare that
3032 function in T if it hasn't been already declared. */
3034 static void
3035 declare_virt_assop_and_dtor (tree t)
3037 if (!(TYPE_POLYMORPHIC_P (t)
3038 && (CLASSTYPE_LAZY_COPY_ASSIGN (t)
3039 || CLASSTYPE_LAZY_MOVE_ASSIGN (t)
3040 || CLASSTYPE_LAZY_DESTRUCTOR (t))))
3041 return;
3043 dfs_walk_all (TYPE_BINFO (t),
3044 dfs_declare_virt_assop_and_dtor,
3045 NULL, t);
3048 /* Declare the inheriting constructor for class T inherited from base
3049 constructor CTOR with the parameter array PARMS of size NPARMS. */
3051 static void
3052 one_inheriting_sig (tree t, tree ctor, tree *parms, int nparms)
3054 gcc_assert (TYPE_MAIN_VARIANT (t) == t);
3056 /* We don't declare an inheriting ctor that would be a default,
3057 copy or move ctor for derived or base. */
3058 if (nparms == 0)
3059 return;
3060 if (nparms == 1
3061 && TREE_CODE (parms[0]) == REFERENCE_TYPE)
3063 tree parm = TYPE_MAIN_VARIANT (TREE_TYPE (parms[0]));
3064 if (parm == t || parm == DECL_CONTEXT (ctor))
3065 return;
3068 tree parmlist = void_list_node;
3069 for (int i = nparms - 1; i >= 0; i--)
3070 parmlist = tree_cons (NULL_TREE, parms[i], parmlist);
3071 tree fn = implicitly_declare_fn (sfk_inheriting_constructor,
3072 t, false, ctor, parmlist);
3074 if (add_method (t, fn, false))
3076 DECL_CHAIN (fn) = TYPE_FIELDS (t);
3077 TYPE_FIELDS (t) = fn;
3081 /* Declare all the inheriting constructors for class T inherited from base
3082 constructor CTOR. */
3084 static void
3085 one_inherited_ctor (tree ctor, tree t, tree using_decl)
3087 tree parms = FUNCTION_FIRST_USER_PARMTYPE (ctor);
3089 if (flag_new_inheriting_ctors)
3091 ctor = implicitly_declare_fn (sfk_inheriting_constructor,
3092 t, /*const*/false, ctor, parms);
3093 add_method (t, ctor, using_decl != NULL_TREE);
3094 TYPE_HAS_USER_CONSTRUCTOR (t) = true;
3095 return;
3098 tree *new_parms = XALLOCAVEC (tree, list_length (parms));
3099 int i = 0;
3100 for (; parms && parms != void_list_node; parms = TREE_CHAIN (parms))
3102 if (TREE_PURPOSE (parms))
3103 one_inheriting_sig (t, ctor, new_parms, i);
3104 new_parms[i++] = TREE_VALUE (parms);
3106 one_inheriting_sig (t, ctor, new_parms, i);
3107 if (parms == NULL_TREE)
3109 if (warning (OPT_Winherited_variadic_ctor,
3110 "the ellipsis in %qD is not inherited", ctor))
3111 inform (DECL_SOURCE_LOCATION (ctor), "%qD declared here", ctor);
3115 /* Create default constructors, assignment operators, and so forth for
3116 the type indicated by T, if they are needed. CANT_HAVE_CONST_CTOR,
3117 and CANT_HAVE_CONST_ASSIGNMENT are nonzero if, for whatever reason,
3118 the class cannot have a default constructor, copy constructor
3119 taking a const reference argument, or an assignment operator taking
3120 a const reference, respectively. */
3122 static void
3123 add_implicitly_declared_members (tree t, tree* access_decls,
3124 int cant_have_const_cctor,
3125 int cant_have_const_assignment)
3127 /* Destructor. */
3128 if (!CLASSTYPE_DESTRUCTOR (t))
3129 /* In general, we create destructors lazily. */
3130 CLASSTYPE_LAZY_DESTRUCTOR (t) = 1;
3132 bool move_ok = false;
3133 if (cxx_dialect >= cxx11 && CLASSTYPE_LAZY_DESTRUCTOR (t)
3134 && !TYPE_HAS_COPY_CTOR (t) && !TYPE_HAS_COPY_ASSIGN (t)
3135 && !classtype_has_move_assign_or_move_ctor_p (t, false))
3136 move_ok = true;
3138 /* [class.ctor]
3140 If there is no user-declared constructor for a class, a default
3141 constructor is implicitly declared. */
3142 if (! TYPE_HAS_USER_CONSTRUCTOR (t))
3144 TYPE_HAS_DEFAULT_CONSTRUCTOR (t) = 1;
3145 CLASSTYPE_LAZY_DEFAULT_CTOR (t) = 1;
3146 if (cxx_dialect >= cxx11)
3147 TYPE_HAS_CONSTEXPR_CTOR (t)
3148 /* Don't force the declaration to get a hard answer; if the
3149 definition would have made the class non-literal, it will still be
3150 non-literal because of the base or member in question, and that
3151 gives a better diagnostic. */
3152 = type_maybe_constexpr_default_constructor (t);
3155 /* [class.ctor]
3157 If a class definition does not explicitly declare a copy
3158 constructor, one is declared implicitly. */
3159 if (! TYPE_HAS_COPY_CTOR (t))
3161 TYPE_HAS_COPY_CTOR (t) = 1;
3162 TYPE_HAS_CONST_COPY_CTOR (t) = !cant_have_const_cctor;
3163 CLASSTYPE_LAZY_COPY_CTOR (t) = 1;
3164 if (move_ok)
3165 CLASSTYPE_LAZY_MOVE_CTOR (t) = 1;
3168 /* If there is no assignment operator, one will be created if and
3169 when it is needed. For now, just record whether or not the type
3170 of the parameter to the assignment operator will be a const or
3171 non-const reference. */
3172 if (!TYPE_HAS_COPY_ASSIGN (t))
3174 TYPE_HAS_COPY_ASSIGN (t) = 1;
3175 TYPE_HAS_CONST_COPY_ASSIGN (t) = !cant_have_const_assignment;
3176 CLASSTYPE_LAZY_COPY_ASSIGN (t) = 1;
3177 if (move_ok && !LAMBDA_TYPE_P (t))
3178 CLASSTYPE_LAZY_MOVE_ASSIGN (t) = 1;
3181 /* We can't be lazy about declaring functions that might override
3182 a virtual function from a base class. */
3183 declare_virt_assop_and_dtor (t);
3185 while (*access_decls)
3187 tree using_decl = TREE_VALUE (*access_decls);
3188 tree decl = USING_DECL_DECLS (using_decl);
3189 if (DECL_NAME (using_decl) == ctor_identifier)
3191 /* declare, then remove the decl */
3192 tree ctor_list = decl;
3193 location_t loc = input_location;
3194 input_location = DECL_SOURCE_LOCATION (using_decl);
3195 for (ovl_iterator iter (ctor_list); iter; ++iter)
3196 one_inherited_ctor (*iter, t, using_decl);
3197 *access_decls = TREE_CHAIN (*access_decls);
3198 input_location = loc;
3200 else
3201 access_decls = &TREE_CHAIN (*access_decls);
3205 /* FIELD is a bit-field. We are finishing the processing for its
3206 enclosing type. Issue any appropriate messages and set appropriate
3207 flags. Returns false if an error has been diagnosed. */
3209 static bool
3210 check_bitfield_decl (tree field)
3212 tree type = TREE_TYPE (field);
3213 tree w;
3215 /* Extract the declared width of the bitfield, which has been
3216 temporarily stashed in DECL_BIT_FIELD_REPRESENTATIVE by grokbitfield. */
3217 w = DECL_BIT_FIELD_REPRESENTATIVE (field);
3218 gcc_assert (w != NULL_TREE);
3219 /* Remove the bit-field width indicator so that the rest of the
3220 compiler does not treat that value as a qualifier. */
3221 DECL_BIT_FIELD_REPRESENTATIVE (field) = NULL_TREE;
3223 /* Detect invalid bit-field type. */
3224 if (!INTEGRAL_OR_ENUMERATION_TYPE_P (type))
3226 error ("bit-field %q+#D with non-integral type", field);
3227 w = error_mark_node;
3229 else
3231 location_t loc = input_location;
3232 /* Avoid the non_lvalue wrapper added by fold for PLUS_EXPRs. */
3233 STRIP_NOPS (w);
3235 /* detect invalid field size. */
3236 input_location = DECL_SOURCE_LOCATION (field);
3237 w = cxx_constant_value (w);
3238 input_location = loc;
3240 if (TREE_CODE (w) != INTEGER_CST)
3242 error ("bit-field %q+D width not an integer constant", field);
3243 w = error_mark_node;
3245 else if (tree_int_cst_sgn (w) < 0)
3247 error ("negative width in bit-field %q+D", field);
3248 w = error_mark_node;
3250 else if (integer_zerop (w) && DECL_NAME (field) != 0)
3252 error ("zero width for bit-field %q+D", field);
3253 w = error_mark_node;
3255 else if ((TREE_CODE (type) != ENUMERAL_TYPE
3256 && TREE_CODE (type) != BOOLEAN_TYPE
3257 && compare_tree_int (w, TYPE_PRECISION (type)) > 0)
3258 || ((TREE_CODE (type) == ENUMERAL_TYPE
3259 || TREE_CODE (type) == BOOLEAN_TYPE)
3260 && tree_int_cst_lt (TYPE_SIZE (type), w)))
3261 warning_at (DECL_SOURCE_LOCATION (field), 0,
3262 "width of %qD exceeds its type", field);
3263 else if (TREE_CODE (type) == ENUMERAL_TYPE)
3265 int prec = TYPE_PRECISION (ENUM_UNDERLYING_TYPE (type));
3266 if (compare_tree_int (w, prec) < 0)
3267 warning_at (DECL_SOURCE_LOCATION (field), 0,
3268 "%qD is too small to hold all values of %q#T",
3269 field, type);
3273 if (w != error_mark_node)
3275 DECL_SIZE (field) = fold_convert (bitsizetype, w);
3276 DECL_BIT_FIELD (field) = 1;
3277 return true;
3279 else
3281 /* Non-bit-fields are aligned for their type. */
3282 DECL_BIT_FIELD (field) = 0;
3283 CLEAR_DECL_C_BIT_FIELD (field);
3284 return false;
3288 /* FIELD is a non bit-field. We are finishing the processing for its
3289 enclosing type T. Issue any appropriate messages and set appropriate
3290 flags. */
3292 static bool
3293 check_field_decl (tree field,
3294 tree t,
3295 int* cant_have_const_ctor,
3296 int* no_const_asn_ref)
3298 tree type = strip_array_types (TREE_TYPE (field));
3299 bool any_default_members = false;
3301 /* In C++98 an anonymous union cannot contain any fields which would change
3302 the settings of CANT_HAVE_CONST_CTOR and friends. */
3303 if (ANON_UNION_TYPE_P (type) && cxx_dialect < cxx11)
3305 /* And, we don't set TYPE_HAS_CONST_COPY_CTOR, etc., for anonymous
3306 structs. So, we recurse through their fields here. */
3307 else if (ANON_AGGR_TYPE_P (type))
3309 for (tree fields = TYPE_FIELDS (type); fields;
3310 fields = DECL_CHAIN (fields))
3311 if (TREE_CODE (fields) == FIELD_DECL)
3312 any_default_members |= check_field_decl (fields, t,
3313 cant_have_const_ctor,
3314 no_const_asn_ref);
3316 /* Check members with class type for constructors, destructors,
3317 etc. */
3318 else if (CLASS_TYPE_P (type))
3320 /* Never let anything with uninheritable virtuals
3321 make it through without complaint. */
3322 abstract_virtuals_error (field, type);
3324 if (TREE_CODE (t) == UNION_TYPE && cxx_dialect < cxx11)
3326 static bool warned;
3327 int oldcount = errorcount;
3328 if (TYPE_NEEDS_CONSTRUCTING (type))
3329 error ("member %q+#D with constructor not allowed in union",
3330 field);
3331 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
3332 error ("member %q+#D with destructor not allowed in union", field);
3333 if (TYPE_HAS_COMPLEX_COPY_ASSIGN (type))
3334 error ("member %q+#D with copy assignment operator not allowed in union",
3335 field);
3336 if (!warned && errorcount > oldcount)
3338 inform (DECL_SOURCE_LOCATION (field), "unrestricted unions "
3339 "only available with -std=c++11 or -std=gnu++11");
3340 warned = true;
3343 else
3345 TYPE_NEEDS_CONSTRUCTING (t) |= TYPE_NEEDS_CONSTRUCTING (type);
3346 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t)
3347 |= TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type);
3348 TYPE_HAS_COMPLEX_COPY_ASSIGN (t)
3349 |= (TYPE_HAS_COMPLEX_COPY_ASSIGN (type)
3350 || !TYPE_HAS_COPY_ASSIGN (type));
3351 TYPE_HAS_COMPLEX_COPY_CTOR (t) |= (TYPE_HAS_COMPLEX_COPY_CTOR (type)
3352 || !TYPE_HAS_COPY_CTOR (type));
3353 TYPE_HAS_COMPLEX_MOVE_ASSIGN (t) |= TYPE_HAS_COMPLEX_MOVE_ASSIGN (type);
3354 TYPE_HAS_COMPLEX_MOVE_CTOR (t) |= TYPE_HAS_COMPLEX_MOVE_CTOR (type);
3355 TYPE_HAS_COMPLEX_DFLT (t) |= (!TYPE_HAS_DEFAULT_CONSTRUCTOR (type)
3356 || TYPE_HAS_COMPLEX_DFLT (type));
3359 if (TYPE_HAS_COPY_CTOR (type)
3360 && !TYPE_HAS_CONST_COPY_CTOR (type))
3361 *cant_have_const_ctor = 1;
3363 if (TYPE_HAS_COPY_ASSIGN (type)
3364 && !TYPE_HAS_CONST_COPY_ASSIGN (type))
3365 *no_const_asn_ref = 1;
3368 check_abi_tags (t, field);
3370 if (DECL_INITIAL (field) != NULL_TREE)
3371 /* `build_class_init_list' does not recognize
3372 non-FIELD_DECLs. */
3373 any_default_members = true;
3375 return any_default_members;
3378 /* Check the data members (both static and non-static), class-scoped
3379 typedefs, etc., appearing in the declaration of T. Issue
3380 appropriate diagnostics. Sets ACCESS_DECLS to a list (in
3381 declaration order) of access declarations; each TREE_VALUE in this
3382 list is a USING_DECL.
3384 In addition, set the following flags:
3386 EMPTY_P
3387 The class is empty, i.e., contains no non-static data members.
3389 CANT_HAVE_CONST_CTOR_P
3390 This class cannot have an implicitly generated copy constructor
3391 taking a const reference.
3393 CANT_HAVE_CONST_ASN_REF
3394 This class cannot have an implicitly generated assignment
3395 operator taking a const reference.
3397 All of these flags should be initialized before calling this
3398 function.
3400 Returns a pointer to the end of the TYPE_FIELDs chain; additional
3401 fields can be added by adding to this chain. */
3403 static void
3404 check_field_decls (tree t, tree *access_decls,
3405 int *cant_have_const_ctor_p,
3406 int *no_const_asn_ref_p)
3408 tree *field;
3409 tree *next;
3410 bool has_pointers;
3411 bool any_default_members;
3412 int cant_pack = 0;
3413 int field_access = -1;
3415 /* Assume there are no access declarations. */
3416 *access_decls = NULL_TREE;
3417 /* Assume this class has no pointer members. */
3418 has_pointers = false;
3419 /* Assume none of the members of this class have default
3420 initializations. */
3421 any_default_members = false;
3423 for (field = &TYPE_FIELDS (t); *field; field = next)
3425 tree x = *field;
3426 tree type = TREE_TYPE (x);
3427 int this_field_access;
3429 next = &DECL_CHAIN (x);
3431 if (TREE_CODE (x) == USING_DECL)
3433 /* Save the access declarations for our caller. */
3434 *access_decls = tree_cons (NULL_TREE, x, *access_decls);
3435 continue;
3438 if (TREE_CODE (x) == TYPE_DECL
3439 || TREE_CODE (x) == TEMPLATE_DECL)
3440 continue;
3442 if (TREE_CODE (x) == FUNCTION_DECL)
3443 /* FIXME: We should fold in the checking from check_methods. */
3444 continue;
3446 /* If we've gotten this far, it's a data member, possibly static,
3447 or an enumerator. */
3448 if (TREE_CODE (x) != CONST_DECL)
3449 DECL_CONTEXT (x) = t;
3451 /* When this goes into scope, it will be a non-local reference. */
3452 DECL_NONLOCAL (x) = 1;
3454 if (TREE_CODE (t) == UNION_TYPE)
3456 /* [class.union] (C++98)
3458 If a union contains a static data member, or a member of
3459 reference type, the program is ill-formed.
3461 In C++11 [class.union] says:
3462 If a union contains a non-static data member of reference type
3463 the program is ill-formed. */
3464 if (VAR_P (x) && cxx_dialect < cxx11)
3466 error ("in C++98 %q+D may not be static because it is "
3467 "a member of a union", x);
3468 continue;
3470 if (TREE_CODE (type) == REFERENCE_TYPE
3471 && TREE_CODE (x) == FIELD_DECL)
3473 error ("non-static data member %q+D in a union may not "
3474 "have reference type %qT", x, type);
3475 continue;
3479 /* Perform error checking that did not get done in
3480 grokdeclarator. */
3481 if (TREE_CODE (type) == FUNCTION_TYPE)
3483 error ("field %q+D invalidly declared function type", x);
3484 type = build_pointer_type (type);
3485 TREE_TYPE (x) = type;
3487 else if (TREE_CODE (type) == METHOD_TYPE)
3489 error ("field %q+D invalidly declared method type", x);
3490 type = build_pointer_type (type);
3491 TREE_TYPE (x) = type;
3494 if (type == error_mark_node)
3495 continue;
3497 if (TREE_CODE (x) == CONST_DECL || VAR_P (x))
3498 continue;
3500 /* Now it can only be a FIELD_DECL. */
3502 if (TREE_PRIVATE (x) || TREE_PROTECTED (x))
3503 CLASSTYPE_NON_AGGREGATE (t) = 1;
3505 /* If at least one non-static data member is non-literal, the whole
3506 class becomes non-literal. Per Core/1453, volatile non-static
3507 data members and base classes are also not allowed.
3508 Note: if the type is incomplete we will complain later on. */
3509 if (COMPLETE_TYPE_P (type)
3510 && (!literal_type_p (type) || CP_TYPE_VOLATILE_P (type)))
3511 CLASSTYPE_LITERAL_P (t) = false;
3513 /* A standard-layout class is a class that:
3515 has the same access control (Clause 11) for all non-static data members,
3516 ... */
3517 this_field_access = TREE_PROTECTED (x) ? 1 : TREE_PRIVATE (x) ? 2 : 0;
3518 if (field_access == -1)
3519 field_access = this_field_access;
3520 else if (this_field_access != field_access)
3521 CLASSTYPE_NON_STD_LAYOUT (t) = 1;
3523 /* If this is of reference type, check if it needs an init. */
3524 if (TREE_CODE (type) == REFERENCE_TYPE)
3526 CLASSTYPE_NON_LAYOUT_POD_P (t) = 1;
3527 CLASSTYPE_NON_STD_LAYOUT (t) = 1;
3528 if (DECL_INITIAL (x) == NULL_TREE)
3529 SET_CLASSTYPE_REF_FIELDS_NEED_INIT (t, 1);
3530 if (cxx_dialect < cxx11)
3532 /* ARM $12.6.2: [A member initializer list] (or, for an
3533 aggregate, initialization by a brace-enclosed list) is the
3534 only way to initialize nonstatic const and reference
3535 members. */
3536 TYPE_HAS_COMPLEX_COPY_ASSIGN (t) = 1;
3537 TYPE_HAS_COMPLEX_MOVE_ASSIGN (t) = 1;
3541 type = strip_array_types (type);
3543 if (TYPE_PACKED (t))
3545 if (!layout_pod_type_p (type) && !TYPE_PACKED (type))
3547 warning_at
3548 (DECL_SOURCE_LOCATION (x), 0,
3549 "ignoring packed attribute because of unpacked non-POD field %q#D",
3551 cant_pack = 1;
3553 else if (DECL_C_BIT_FIELD (x)
3554 || TYPE_ALIGN (TREE_TYPE (x)) > BITS_PER_UNIT)
3555 DECL_PACKED (x) = 1;
3558 if (DECL_C_BIT_FIELD (x)
3559 && integer_zerop (DECL_BIT_FIELD_REPRESENTATIVE (x)))
3560 /* We don't treat zero-width bitfields as making a class
3561 non-empty. */
3563 else
3565 /* The class is non-empty. */
3566 CLASSTYPE_EMPTY_P (t) = 0;
3567 /* The class is not even nearly empty. */
3568 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
3569 /* If one of the data members contains an empty class,
3570 so does T. */
3571 if (CLASS_TYPE_P (type)
3572 && CLASSTYPE_CONTAINS_EMPTY_CLASS_P (type))
3573 CLASSTYPE_CONTAINS_EMPTY_CLASS_P (t) = 1;
3576 /* This is used by -Weffc++ (see below). Warn only for pointers
3577 to members which might hold dynamic memory. So do not warn
3578 for pointers to functions or pointers to members. */
3579 if (TYPE_PTR_P (type)
3580 && !TYPE_PTRFN_P (type))
3581 has_pointers = true;
3583 if (CLASS_TYPE_P (type))
3585 if (CLASSTYPE_REF_FIELDS_NEED_INIT (type))
3586 SET_CLASSTYPE_REF_FIELDS_NEED_INIT (t, 1);
3587 if (CLASSTYPE_READONLY_FIELDS_NEED_INIT (type))
3588 SET_CLASSTYPE_READONLY_FIELDS_NEED_INIT (t, 1);
3591 if (DECL_MUTABLE_P (x) || TYPE_HAS_MUTABLE_P (type))
3592 CLASSTYPE_HAS_MUTABLE (t) = 1;
3594 if (DECL_MUTABLE_P (x))
3596 if (CP_TYPE_CONST_P (type))
3598 error ("member %q+D cannot be declared both %<const%> "
3599 "and %<mutable%>", x);
3600 continue;
3602 if (TREE_CODE (type) == REFERENCE_TYPE)
3604 error ("member %q+D cannot be declared as a %<mutable%> "
3605 "reference", x);
3606 continue;
3610 if (! layout_pod_type_p (type))
3611 /* DR 148 now allows pointers to members (which are POD themselves),
3612 to be allowed in POD structs. */
3613 CLASSTYPE_NON_LAYOUT_POD_P (t) = 1;
3615 if (!std_layout_type_p (type))
3616 CLASSTYPE_NON_STD_LAYOUT (t) = 1;
3618 if (! zero_init_p (type))
3619 CLASSTYPE_NON_ZERO_INIT_P (t) = 1;
3621 /* We set DECL_C_BIT_FIELD in grokbitfield.
3622 If the type and width are valid, we'll also set DECL_BIT_FIELD. */
3623 if (DECL_C_BIT_FIELD (x))
3624 check_bitfield_decl (x);
3626 if (check_field_decl (x, t, cant_have_const_ctor_p, no_const_asn_ref_p))
3628 if (any_default_members
3629 && TREE_CODE (t) == UNION_TYPE)
3630 error ("multiple fields in union %qT initialized", t);
3631 any_default_members = true;
3634 /* Now that we've removed bit-field widths from DECL_INITIAL,
3635 anything left in DECL_INITIAL is an NSDMI that makes the class
3636 non-aggregate in C++11. */
3637 if (DECL_INITIAL (x) && cxx_dialect < cxx14)
3638 CLASSTYPE_NON_AGGREGATE (t) = true;
3640 /* If any field is const, the structure type is pseudo-const. */
3641 if (CP_TYPE_CONST_P (type))
3643 C_TYPE_FIELDS_READONLY (t) = 1;
3644 if (DECL_INITIAL (x) == NULL_TREE)
3645 SET_CLASSTYPE_READONLY_FIELDS_NEED_INIT (t, 1);
3646 if (cxx_dialect < cxx11)
3648 /* ARM $12.6.2: [A member initializer list] (or, for an
3649 aggregate, initialization by a brace-enclosed list) is the
3650 only way to initialize nonstatic const and reference
3651 members. */
3652 TYPE_HAS_COMPLEX_COPY_ASSIGN (t) = 1;
3653 TYPE_HAS_COMPLEX_MOVE_ASSIGN (t) = 1;
3656 /* A field that is pseudo-const makes the structure likewise. */
3657 else if (CLASS_TYPE_P (type))
3659 C_TYPE_FIELDS_READONLY (t) |= C_TYPE_FIELDS_READONLY (type);
3660 SET_CLASSTYPE_READONLY_FIELDS_NEED_INIT (t,
3661 CLASSTYPE_READONLY_FIELDS_NEED_INIT (t)
3662 | CLASSTYPE_READONLY_FIELDS_NEED_INIT (type));
3665 /* Core issue 80: A nonstatic data member is required to have a
3666 different name from the class iff the class has a
3667 user-declared constructor. */
3668 if (constructor_name_p (DECL_NAME (x), t)
3669 && TYPE_HAS_USER_CONSTRUCTOR (t))
3670 permerror (DECL_SOURCE_LOCATION (x),
3671 "field %q#D with same name as class", x);
3674 /* Effective C++ rule 11: if a class has dynamic memory held by pointers,
3675 it should also define a copy constructor and an assignment operator to
3676 implement the correct copy semantic (deep vs shallow, etc.). As it is
3677 not feasible to check whether the constructors do allocate dynamic memory
3678 and store it within members, we approximate the warning like this:
3680 -- Warn only if there are members which are pointers
3681 -- Warn only if there is a non-trivial constructor (otherwise,
3682 there cannot be memory allocated).
3683 -- Warn only if there is a non-trivial destructor. We assume that the
3684 user at least implemented the cleanup correctly, and a destructor
3685 is needed to free dynamic memory.
3687 This seems enough for practical purposes. */
3688 if (warn_ecpp
3689 && has_pointers
3690 && TYPE_HAS_USER_CONSTRUCTOR (t)
3691 && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t)
3692 && !(TYPE_HAS_COPY_CTOR (t) && TYPE_HAS_COPY_ASSIGN (t)))
3694 warning (OPT_Weffc__, "%q#T has pointer data members", t);
3696 if (! TYPE_HAS_COPY_CTOR (t))
3698 warning (OPT_Weffc__,
3699 " but does not override %<%T(const %T&)%>", t, t);
3700 if (!TYPE_HAS_COPY_ASSIGN (t))
3701 warning (OPT_Weffc__, " or %<operator=(const %T&)%>", t);
3703 else if (! TYPE_HAS_COPY_ASSIGN (t))
3704 warning (OPT_Weffc__,
3705 " but does not override %<operator=(const %T&)%>", t);
3708 /* Non-static data member initializers make the default constructor
3709 non-trivial. */
3710 if (any_default_members)
3712 TYPE_NEEDS_CONSTRUCTING (t) = true;
3713 TYPE_HAS_COMPLEX_DFLT (t) = true;
3716 /* If any of the fields couldn't be packed, unset TYPE_PACKED. */
3717 if (cant_pack)
3718 TYPE_PACKED (t) = 0;
3720 /* Check anonymous struct/anonymous union fields. */
3721 finish_struct_anon (t);
3723 /* We've built up the list of access declarations in reverse order.
3724 Fix that now. */
3725 *access_decls = nreverse (*access_decls);
3728 /* If TYPE is an empty class type, records its OFFSET in the table of
3729 OFFSETS. */
3731 static int
3732 record_subobject_offset (tree type, tree offset, splay_tree offsets)
3734 splay_tree_node n;
3736 if (!is_empty_class (type))
3737 return 0;
3739 /* Record the location of this empty object in OFFSETS. */
3740 n = splay_tree_lookup (offsets, (splay_tree_key) offset);
3741 if (!n)
3742 n = splay_tree_insert (offsets,
3743 (splay_tree_key) offset,
3744 (splay_tree_value) NULL_TREE);
3745 n->value = ((splay_tree_value)
3746 tree_cons (NULL_TREE,
3747 type,
3748 (tree) n->value));
3750 return 0;
3753 /* Returns nonzero if TYPE is an empty class type and there is
3754 already an entry in OFFSETS for the same TYPE as the same OFFSET. */
3756 static int
3757 check_subobject_offset (tree type, tree offset, splay_tree offsets)
3759 splay_tree_node n;
3760 tree t;
3762 if (!is_empty_class (type))
3763 return 0;
3765 /* Record the location of this empty object in OFFSETS. */
3766 n = splay_tree_lookup (offsets, (splay_tree_key) offset);
3767 if (!n)
3768 return 0;
3770 for (t = (tree) n->value; t; t = TREE_CHAIN (t))
3771 if (same_type_p (TREE_VALUE (t), type))
3772 return 1;
3774 return 0;
3777 /* Walk through all the subobjects of TYPE (located at OFFSET). Call
3778 F for every subobject, passing it the type, offset, and table of
3779 OFFSETS. If VBASES_P is one, then virtual non-primary bases should
3780 be traversed.
3782 If MAX_OFFSET is non-NULL, then subobjects with an offset greater
3783 than MAX_OFFSET will not be walked.
3785 If F returns a nonzero value, the traversal ceases, and that value
3786 is returned. Otherwise, returns zero. */
3788 static int
3789 walk_subobject_offsets (tree type,
3790 subobject_offset_fn f,
3791 tree offset,
3792 splay_tree offsets,
3793 tree max_offset,
3794 int vbases_p)
3796 int r = 0;
3797 tree type_binfo = NULL_TREE;
3799 /* If this OFFSET is bigger than the MAX_OFFSET, then we should
3800 stop. */
3801 if (max_offset && tree_int_cst_lt (max_offset, offset))
3802 return 0;
3804 if (type == error_mark_node)
3805 return 0;
3807 if (!TYPE_P (type))
3809 type_binfo = type;
3810 type = BINFO_TYPE (type);
3813 if (CLASS_TYPE_P (type))
3815 tree field;
3816 tree binfo;
3817 int i;
3819 /* Avoid recursing into objects that are not interesting. */
3820 if (!CLASSTYPE_CONTAINS_EMPTY_CLASS_P (type))
3821 return 0;
3823 /* Record the location of TYPE. */
3824 r = (*f) (type, offset, offsets);
3825 if (r)
3826 return r;
3828 /* Iterate through the direct base classes of TYPE. */
3829 if (!type_binfo)
3830 type_binfo = TYPE_BINFO (type);
3831 for (i = 0; BINFO_BASE_ITERATE (type_binfo, i, binfo); i++)
3833 tree binfo_offset;
3835 if (BINFO_VIRTUAL_P (binfo))
3836 continue;
3838 tree orig_binfo;
3839 /* We cannot rely on BINFO_OFFSET being set for the base
3840 class yet, but the offsets for direct non-virtual
3841 bases can be calculated by going back to the TYPE. */
3842 orig_binfo = BINFO_BASE_BINFO (TYPE_BINFO (type), i);
3843 binfo_offset = size_binop (PLUS_EXPR,
3844 offset,
3845 BINFO_OFFSET (orig_binfo));
3847 r = walk_subobject_offsets (binfo,
3849 binfo_offset,
3850 offsets,
3851 max_offset,
3852 /*vbases_p=*/0);
3853 if (r)
3854 return r;
3857 if (CLASSTYPE_VBASECLASSES (type))
3859 unsigned ix;
3860 vec<tree, va_gc> *vbases;
3862 /* Iterate through the virtual base classes of TYPE. In G++
3863 3.2, we included virtual bases in the direct base class
3864 loop above, which results in incorrect results; the
3865 correct offsets for virtual bases are only known when
3866 working with the most derived type. */
3867 if (vbases_p)
3868 for (vbases = CLASSTYPE_VBASECLASSES (type), ix = 0;
3869 vec_safe_iterate (vbases, ix, &binfo); ix++)
3871 r = walk_subobject_offsets (binfo,
3873 size_binop (PLUS_EXPR,
3874 offset,
3875 BINFO_OFFSET (binfo)),
3876 offsets,
3877 max_offset,
3878 /*vbases_p=*/0);
3879 if (r)
3880 return r;
3882 else
3884 /* We still have to walk the primary base, if it is
3885 virtual. (If it is non-virtual, then it was walked
3886 above.) */
3887 tree vbase = get_primary_binfo (type_binfo);
3889 if (vbase && BINFO_VIRTUAL_P (vbase)
3890 && BINFO_PRIMARY_P (vbase)
3891 && BINFO_INHERITANCE_CHAIN (vbase) == type_binfo)
3893 r = (walk_subobject_offsets
3894 (vbase, f, offset,
3895 offsets, max_offset, /*vbases_p=*/0));
3896 if (r)
3897 return r;
3902 /* Iterate through the fields of TYPE. */
3903 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
3904 if (TREE_CODE (field) == FIELD_DECL
3905 && TREE_TYPE (field) != error_mark_node
3906 && !DECL_ARTIFICIAL (field))
3908 tree field_offset;
3910 field_offset = byte_position (field);
3912 r = walk_subobject_offsets (TREE_TYPE (field),
3914 size_binop (PLUS_EXPR,
3915 offset,
3916 field_offset),
3917 offsets,
3918 max_offset,
3919 /*vbases_p=*/1);
3920 if (r)
3921 return r;
3924 else if (TREE_CODE (type) == ARRAY_TYPE)
3926 tree element_type = strip_array_types (type);
3927 tree domain = TYPE_DOMAIN (type);
3928 tree index;
3930 /* Avoid recursing into objects that are not interesting. */
3931 if (!CLASS_TYPE_P (element_type)
3932 || !CLASSTYPE_CONTAINS_EMPTY_CLASS_P (element_type)
3933 || !domain
3934 || integer_minus_onep (TYPE_MAX_VALUE (domain)))
3935 return 0;
3937 /* Step through each of the elements in the array. */
3938 for (index = size_zero_node;
3939 !tree_int_cst_lt (TYPE_MAX_VALUE (domain), index);
3940 index = size_binop (PLUS_EXPR, index, size_one_node))
3942 r = walk_subobject_offsets (TREE_TYPE (type),
3944 offset,
3945 offsets,
3946 max_offset,
3947 /*vbases_p=*/1);
3948 if (r)
3949 return r;
3950 offset = size_binop (PLUS_EXPR, offset,
3951 TYPE_SIZE_UNIT (TREE_TYPE (type)));
3952 /* If this new OFFSET is bigger than the MAX_OFFSET, then
3953 there's no point in iterating through the remaining
3954 elements of the array. */
3955 if (max_offset && tree_int_cst_lt (max_offset, offset))
3956 break;
3960 return 0;
3963 /* Record all of the empty subobjects of TYPE (either a type or a
3964 binfo). If IS_DATA_MEMBER is true, then a non-static data member
3965 is being placed at OFFSET; otherwise, it is a base class that is
3966 being placed at OFFSET. */
3968 static void
3969 record_subobject_offsets (tree type,
3970 tree offset,
3971 splay_tree offsets,
3972 bool is_data_member)
3974 tree max_offset;
3975 /* If recording subobjects for a non-static data member or a
3976 non-empty base class , we do not need to record offsets beyond
3977 the size of the biggest empty class. Additional data members
3978 will go at the end of the class. Additional base classes will go
3979 either at offset zero (if empty, in which case they cannot
3980 overlap with offsets past the size of the biggest empty class) or
3981 at the end of the class.
3983 However, if we are placing an empty base class, then we must record
3984 all offsets, as either the empty class is at offset zero (where
3985 other empty classes might later be placed) or at the end of the
3986 class (where other objects might then be placed, so other empty
3987 subobjects might later overlap). */
3988 if (is_data_member
3989 || !is_empty_class (BINFO_TYPE (type)))
3990 max_offset = sizeof_biggest_empty_class;
3991 else
3992 max_offset = NULL_TREE;
3993 walk_subobject_offsets (type, record_subobject_offset, offset,
3994 offsets, max_offset, is_data_member);
3997 /* Returns nonzero if any of the empty subobjects of TYPE (located at
3998 OFFSET) conflict with entries in OFFSETS. If VBASES_P is nonzero,
3999 virtual bases of TYPE are examined. */
4001 static int
4002 layout_conflict_p (tree type,
4003 tree offset,
4004 splay_tree offsets,
4005 int vbases_p)
4007 splay_tree_node max_node;
4009 /* Get the node in OFFSETS that indicates the maximum offset where
4010 an empty subobject is located. */
4011 max_node = splay_tree_max (offsets);
4012 /* If there aren't any empty subobjects, then there's no point in
4013 performing this check. */
4014 if (!max_node)
4015 return 0;
4017 return walk_subobject_offsets (type, check_subobject_offset, offset,
4018 offsets, (tree) (max_node->key),
4019 vbases_p);
4022 /* DECL is a FIELD_DECL corresponding either to a base subobject of a
4023 non-static data member of the type indicated by RLI. BINFO is the
4024 binfo corresponding to the base subobject, OFFSETS maps offsets to
4025 types already located at those offsets. This function determines
4026 the position of the DECL. */
4028 static void
4029 layout_nonempty_base_or_field (record_layout_info rli,
4030 tree decl,
4031 tree binfo,
4032 splay_tree offsets)
4034 tree offset = NULL_TREE;
4035 bool field_p;
4036 tree type;
4038 if (binfo)
4040 /* For the purposes of determining layout conflicts, we want to
4041 use the class type of BINFO; TREE_TYPE (DECL) will be the
4042 CLASSTYPE_AS_BASE version, which does not contain entries for
4043 zero-sized bases. */
4044 type = TREE_TYPE (binfo);
4045 field_p = false;
4047 else
4049 type = TREE_TYPE (decl);
4050 field_p = true;
4053 /* Try to place the field. It may take more than one try if we have
4054 a hard time placing the field without putting two objects of the
4055 same type at the same address. */
4056 while (1)
4058 struct record_layout_info_s old_rli = *rli;
4060 /* Place this field. */
4061 place_field (rli, decl);
4062 offset = byte_position (decl);
4064 /* We have to check to see whether or not there is already
4065 something of the same type at the offset we're about to use.
4066 For example, consider:
4068 struct S {};
4069 struct T : public S { int i; };
4070 struct U : public S, public T {};
4072 Here, we put S at offset zero in U. Then, we can't put T at
4073 offset zero -- its S component would be at the same address
4074 as the S we already allocated. So, we have to skip ahead.
4075 Since all data members, including those whose type is an
4076 empty class, have nonzero size, any overlap can happen only
4077 with a direct or indirect base-class -- it can't happen with
4078 a data member. */
4079 /* In a union, overlap is permitted; all members are placed at
4080 offset zero. */
4081 if (TREE_CODE (rli->t) == UNION_TYPE)
4082 break;
4083 if (layout_conflict_p (field_p ? type : binfo, offset,
4084 offsets, field_p))
4086 /* Strip off the size allocated to this field. That puts us
4087 at the first place we could have put the field with
4088 proper alignment. */
4089 *rli = old_rli;
4091 /* Bump up by the alignment required for the type. */
4092 rli->bitpos
4093 = size_binop (PLUS_EXPR, rli->bitpos,
4094 bitsize_int (binfo
4095 ? CLASSTYPE_ALIGN (type)
4096 : TYPE_ALIGN (type)));
4097 normalize_rli (rli);
4099 else if (TREE_CODE (type) == NULLPTR_TYPE
4100 && warn_abi && abi_version_crosses (9))
4102 /* Before ABI v9, we were giving nullptr_t alignment of 1; if
4103 the offset wasn't aligned like a pointer when we started to
4104 layout this field, that affects its position. */
4105 tree pos = rli_size_unit_so_far (&old_rli);
4106 if (int_cst_value (pos) % TYPE_ALIGN_UNIT (ptr_type_node) != 0)
4108 if (abi_version_at_least (9))
4109 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wabi,
4110 "alignment of %qD increased in -fabi-version=9 "
4111 "(GCC 5.2)", decl);
4112 else
4113 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wabi, "alignment "
4114 "of %qD will increase in -fabi-version=9", decl);
4116 break;
4118 else
4119 /* There was no conflict. We're done laying out this field. */
4120 break;
4123 /* Now that we know where it will be placed, update its
4124 BINFO_OFFSET. */
4125 if (binfo && CLASS_TYPE_P (BINFO_TYPE (binfo)))
4126 /* Indirect virtual bases may have a nonzero BINFO_OFFSET at
4127 this point because their BINFO_OFFSET is copied from another
4128 hierarchy. Therefore, we may not need to add the entire
4129 OFFSET. */
4130 propagate_binfo_offsets (binfo,
4131 size_diffop_loc (input_location,
4132 fold_convert (ssizetype, offset),
4133 fold_convert (ssizetype,
4134 BINFO_OFFSET (binfo))));
4137 /* Returns true if TYPE is empty and OFFSET is nonzero. */
4139 static int
4140 empty_base_at_nonzero_offset_p (tree type,
4141 tree offset,
4142 splay_tree /*offsets*/)
4144 return is_empty_class (type) && !integer_zerop (offset);
4147 /* Layout the empty base BINFO. EOC indicates the byte currently just
4148 past the end of the class, and should be correctly aligned for a
4149 class of the type indicated by BINFO; OFFSETS gives the offsets of
4150 the empty bases allocated so far. T is the most derived
4151 type. Return nonzero iff we added it at the end. */
4153 static bool
4154 layout_empty_base (record_layout_info rli, tree binfo,
4155 tree eoc, splay_tree offsets)
4157 tree alignment;
4158 tree basetype = BINFO_TYPE (binfo);
4159 bool atend = false;
4161 /* This routine should only be used for empty classes. */
4162 gcc_assert (is_empty_class (basetype));
4163 alignment = ssize_int (CLASSTYPE_ALIGN_UNIT (basetype));
4165 if (!integer_zerop (BINFO_OFFSET (binfo)))
4166 propagate_binfo_offsets
4167 (binfo, size_diffop_loc (input_location,
4168 size_zero_node, BINFO_OFFSET (binfo)));
4170 /* This is an empty base class. We first try to put it at offset
4171 zero. */
4172 if (layout_conflict_p (binfo,
4173 BINFO_OFFSET (binfo),
4174 offsets,
4175 /*vbases_p=*/0))
4177 /* That didn't work. Now, we move forward from the next
4178 available spot in the class. */
4179 atend = true;
4180 propagate_binfo_offsets (binfo, fold_convert (ssizetype, eoc));
4181 while (1)
4183 if (!layout_conflict_p (binfo,
4184 BINFO_OFFSET (binfo),
4185 offsets,
4186 /*vbases_p=*/0))
4187 /* We finally found a spot where there's no overlap. */
4188 break;
4190 /* There's overlap here, too. Bump along to the next spot. */
4191 propagate_binfo_offsets (binfo, alignment);
4195 if (CLASSTYPE_USER_ALIGN (basetype))
4197 rli->record_align = MAX (rli->record_align, CLASSTYPE_ALIGN (basetype));
4198 if (warn_packed)
4199 rli->unpacked_align = MAX (rli->unpacked_align, CLASSTYPE_ALIGN (basetype));
4200 TYPE_USER_ALIGN (rli->t) = 1;
4203 return atend;
4206 /* Build the FIELD_DECL for BASETYPE as a base of T, add it to the chain of
4207 fields at NEXT_FIELD, and return it. */
4209 static tree
4210 build_base_field_1 (tree t, tree basetype, tree *&next_field)
4212 /* Create the FIELD_DECL. */
4213 gcc_assert (CLASSTYPE_AS_BASE (basetype));
4214 tree decl = build_decl (input_location,
4215 FIELD_DECL, NULL_TREE, CLASSTYPE_AS_BASE (basetype));
4216 DECL_ARTIFICIAL (decl) = 1;
4217 DECL_IGNORED_P (decl) = 1;
4218 DECL_FIELD_CONTEXT (decl) = t;
4219 DECL_SIZE (decl) = CLASSTYPE_SIZE (basetype);
4220 DECL_SIZE_UNIT (decl) = CLASSTYPE_SIZE_UNIT (basetype);
4221 SET_DECL_ALIGN (decl, CLASSTYPE_ALIGN (basetype));
4222 DECL_USER_ALIGN (decl) = CLASSTYPE_USER_ALIGN (basetype);
4223 SET_DECL_MODE (decl, TYPE_MODE (basetype));
4224 DECL_FIELD_IS_BASE (decl) = 1;
4226 /* Add the new FIELD_DECL to the list of fields for T. */
4227 DECL_CHAIN (decl) = *next_field;
4228 *next_field = decl;
4229 next_field = &DECL_CHAIN (decl);
4231 return decl;
4234 /* Layout the base given by BINFO in the class indicated by RLI.
4235 *BASE_ALIGN is a running maximum of the alignments of
4236 any base class. OFFSETS gives the location of empty base
4237 subobjects. T is the most derived type. Return nonzero if the new
4238 object cannot be nearly-empty. A new FIELD_DECL is inserted at
4239 *NEXT_FIELD, unless BINFO is for an empty base class.
4241 Returns the location at which the next field should be inserted. */
4243 static tree *
4244 build_base_field (record_layout_info rli, tree binfo,
4245 splay_tree offsets, tree *next_field)
4247 tree t = rli->t;
4248 tree basetype = BINFO_TYPE (binfo);
4250 if (!COMPLETE_TYPE_P (basetype))
4251 /* This error is now reported in xref_tag, thus giving better
4252 location information. */
4253 return next_field;
4255 /* Place the base class. */
4256 if (!is_empty_class (basetype))
4258 tree decl;
4260 /* The containing class is non-empty because it has a non-empty
4261 base class. */
4262 CLASSTYPE_EMPTY_P (t) = 0;
4264 /* Create the FIELD_DECL. */
4265 decl = build_base_field_1 (t, basetype, next_field);
4267 /* Try to place the field. It may take more than one try if we
4268 have a hard time placing the field without putting two
4269 objects of the same type at the same address. */
4270 layout_nonempty_base_or_field (rli, decl, binfo, offsets);
4272 else
4274 tree eoc;
4275 bool atend;
4277 /* On some platforms (ARM), even empty classes will not be
4278 byte-aligned. */
4279 eoc = round_up_loc (input_location,
4280 rli_size_unit_so_far (rli),
4281 CLASSTYPE_ALIGN_UNIT (basetype));
4282 atend = layout_empty_base (rli, binfo, eoc, offsets);
4283 /* A nearly-empty class "has no proper base class that is empty,
4284 not morally virtual, and at an offset other than zero." */
4285 if (!BINFO_VIRTUAL_P (binfo) && CLASSTYPE_NEARLY_EMPTY_P (t))
4287 if (atend)
4288 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
4289 /* The check above (used in G++ 3.2) is insufficient because
4290 an empty class placed at offset zero might itself have an
4291 empty base at a nonzero offset. */
4292 else if (walk_subobject_offsets (basetype,
4293 empty_base_at_nonzero_offset_p,
4294 size_zero_node,
4295 /*offsets=*/NULL,
4296 /*max_offset=*/NULL_TREE,
4297 /*vbases_p=*/true))
4298 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
4301 /* We used to not create a FIELD_DECL for empty base classes because of
4302 back end issues with overlapping FIELD_DECLs, but that doesn't seem to
4303 be a problem anymore. We need them to handle initialization of C++17
4304 aggregate bases. */
4305 if (cxx_dialect >= cxx17 && !BINFO_VIRTUAL_P (binfo))
4307 tree decl = build_base_field_1 (t, basetype, next_field);
4308 DECL_FIELD_OFFSET (decl) = BINFO_OFFSET (binfo);
4309 DECL_FIELD_BIT_OFFSET (decl) = bitsize_zero_node;
4310 SET_DECL_OFFSET_ALIGN (decl, BITS_PER_UNIT);
4313 /* An empty virtual base causes a class to be non-empty
4314 -- but in that case we do not need to clear CLASSTYPE_EMPTY_P
4315 here because that was already done when the virtual table
4316 pointer was created. */
4319 /* Record the offsets of BINFO and its base subobjects. */
4320 record_subobject_offsets (binfo,
4321 BINFO_OFFSET (binfo),
4322 offsets,
4323 /*is_data_member=*/false);
4325 return next_field;
4328 /* Layout all of the non-virtual base classes. Record empty
4329 subobjects in OFFSETS. T is the most derived type. Return nonzero
4330 if the type cannot be nearly empty. The fields created
4331 corresponding to the base classes will be inserted at
4332 *NEXT_FIELD. */
4334 static void
4335 build_base_fields (record_layout_info rli,
4336 splay_tree offsets, tree *next_field)
4338 /* Chain to hold all the new FIELD_DECLs which stand in for base class
4339 subobjects. */
4340 tree t = rli->t;
4341 int n_baseclasses = BINFO_N_BASE_BINFOS (TYPE_BINFO (t));
4342 int i;
4344 /* The primary base class is always allocated first. */
4345 if (CLASSTYPE_HAS_PRIMARY_BASE_P (t))
4346 next_field = build_base_field (rli, CLASSTYPE_PRIMARY_BINFO (t),
4347 offsets, next_field);
4349 /* Now allocate the rest of the bases. */
4350 for (i = 0; i < n_baseclasses; ++i)
4352 tree base_binfo;
4354 base_binfo = BINFO_BASE_BINFO (TYPE_BINFO (t), i);
4356 /* The primary base was already allocated above, so we don't
4357 need to allocate it again here. */
4358 if (base_binfo == CLASSTYPE_PRIMARY_BINFO (t))
4359 continue;
4361 /* Virtual bases are added at the end (a primary virtual base
4362 will have already been added). */
4363 if (BINFO_VIRTUAL_P (base_binfo))
4364 continue;
4366 next_field = build_base_field (rli, base_binfo,
4367 offsets, next_field);
4371 /* Go through the TYPE_FIELDS of T issuing any appropriate
4372 diagnostics, figuring out which methods override which other
4373 methods, and so forth. */
4375 static void
4376 check_methods (tree t)
4378 for (tree x = TYPE_FIELDS (t); x; x = DECL_CHAIN (x))
4379 if (DECL_DECLARES_FUNCTION_P (x))
4381 check_for_override (x, t);
4383 if (DECL_PURE_VIRTUAL_P (x)
4384 && (TREE_CODE (x) != FUNCTION_DECL || ! DECL_VINDEX (x)))
4385 error ("initializer specified for non-virtual method %q+D", x);
4386 /* The name of the field is the original field name
4387 Save this in auxiliary field for later overloading. */
4388 if (TREE_CODE (x) == FUNCTION_DECL && DECL_VINDEX (x))
4390 TYPE_POLYMORPHIC_P (t) = 1;
4391 if (DECL_PURE_VIRTUAL_P (x))
4392 vec_safe_push (CLASSTYPE_PURE_VIRTUALS (t), x);
4395 /* All user-provided destructors are non-trivial.
4396 Constructors and assignment ops are handled in
4397 grok_special_member_properties. */
4398 if (DECL_DESTRUCTOR_P (x) && user_provided_p (x))
4399 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t) = 1;
4400 if (!DECL_VIRTUAL_P (x)
4401 && lookup_attribute ("transaction_safe_dynamic",
4402 DECL_ATTRIBUTES (x)))
4403 error_at (DECL_SOURCE_LOCATION (x),
4404 "%<transaction_safe_dynamic%> may only be specified for "
4405 "a virtual function");
4409 /* FN is a constructor or destructor. Clone the declaration to create
4410 a specialized in-charge or not-in-charge version, as indicated by
4411 NAME. */
4413 static tree
4414 build_clone (tree fn, tree name)
4416 tree parms;
4417 tree clone;
4419 /* Copy the function. */
4420 clone = copy_decl (fn);
4421 /* Reset the function name. */
4422 DECL_NAME (clone) = name;
4423 /* Remember where this function came from. */
4424 DECL_ABSTRACT_ORIGIN (clone) = fn;
4425 /* Make it easy to find the CLONE given the FN. */
4426 DECL_CHAIN (clone) = DECL_CHAIN (fn);
4427 DECL_CHAIN (fn) = clone;
4429 /* If this is a template, do the rest on the DECL_TEMPLATE_RESULT. */
4430 if (TREE_CODE (clone) == TEMPLATE_DECL)
4432 tree result = build_clone (DECL_TEMPLATE_RESULT (clone), name);
4433 DECL_TEMPLATE_RESULT (clone) = result;
4434 DECL_TEMPLATE_INFO (result) = copy_node (DECL_TEMPLATE_INFO (result));
4435 DECL_TI_TEMPLATE (result) = clone;
4436 TREE_TYPE (clone) = TREE_TYPE (result);
4437 return clone;
4439 else
4441 // Clone constraints.
4442 if (flag_concepts)
4443 if (tree ci = get_constraints (fn))
4444 set_constraints (clone, copy_node (ci));
4448 SET_DECL_ASSEMBLER_NAME (clone, NULL_TREE);
4449 DECL_CLONED_FUNCTION (clone) = fn;
4450 /* There's no pending inline data for this function. */
4451 DECL_PENDING_INLINE_INFO (clone) = NULL;
4452 DECL_PENDING_INLINE_P (clone) = 0;
4454 /* The base-class destructor is not virtual. */
4455 if (name == base_dtor_identifier)
4457 DECL_VIRTUAL_P (clone) = 0;
4458 if (TREE_CODE (clone) != TEMPLATE_DECL)
4459 DECL_VINDEX (clone) = NULL_TREE;
4462 bool ctor_omit_inherited_parms_p = ctor_omit_inherited_parms (clone);
4463 if (ctor_omit_inherited_parms_p)
4464 gcc_assert (DECL_HAS_IN_CHARGE_PARM_P (clone));
4466 /* If there was an in-charge parameter, drop it from the function
4467 type. */
4468 if (DECL_HAS_IN_CHARGE_PARM_P (clone))
4470 tree basetype;
4471 tree parmtypes;
4472 tree exceptions;
4474 exceptions = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (clone));
4475 basetype = TYPE_METHOD_BASETYPE (TREE_TYPE (clone));
4476 parmtypes = TYPE_ARG_TYPES (TREE_TYPE (clone));
4477 /* Skip the `this' parameter. */
4478 parmtypes = TREE_CHAIN (parmtypes);
4479 /* Skip the in-charge parameter. */
4480 parmtypes = TREE_CHAIN (parmtypes);
4481 /* And the VTT parm, in a complete [cd]tor. */
4482 if (DECL_HAS_VTT_PARM_P (fn)
4483 && ! DECL_NEEDS_VTT_PARM_P (clone))
4484 parmtypes = TREE_CHAIN (parmtypes);
4485 if (ctor_omit_inherited_parms_p)
4487 /* If we're omitting inherited parms, that just leaves the VTT. */
4488 gcc_assert (DECL_NEEDS_VTT_PARM_P (clone));
4489 parmtypes = tree_cons (NULL_TREE, vtt_parm_type, void_list_node);
4491 TREE_TYPE (clone)
4492 = build_method_type_directly (basetype,
4493 TREE_TYPE (TREE_TYPE (clone)),
4494 parmtypes);
4495 if (exceptions)
4496 TREE_TYPE (clone) = build_exception_variant (TREE_TYPE (clone),
4497 exceptions);
4498 TREE_TYPE (clone)
4499 = cp_build_type_attribute_variant (TREE_TYPE (clone),
4500 TYPE_ATTRIBUTES (TREE_TYPE (fn)));
4503 /* Copy the function parameters. */
4504 DECL_ARGUMENTS (clone) = copy_list (DECL_ARGUMENTS (clone));
4505 /* Remove the in-charge parameter. */
4506 if (DECL_HAS_IN_CHARGE_PARM_P (clone))
4508 DECL_CHAIN (DECL_ARGUMENTS (clone))
4509 = DECL_CHAIN (DECL_CHAIN (DECL_ARGUMENTS (clone)));
4510 DECL_HAS_IN_CHARGE_PARM_P (clone) = 0;
4512 /* And the VTT parm, in a complete [cd]tor. */
4513 if (DECL_HAS_VTT_PARM_P (fn))
4515 if (DECL_NEEDS_VTT_PARM_P (clone))
4516 DECL_HAS_VTT_PARM_P (clone) = 1;
4517 else
4519 DECL_CHAIN (DECL_ARGUMENTS (clone))
4520 = DECL_CHAIN (DECL_CHAIN (DECL_ARGUMENTS (clone)));
4521 DECL_HAS_VTT_PARM_P (clone) = 0;
4525 /* A base constructor inheriting from a virtual base doesn't get the
4526 arguments. */
4527 if (ctor_omit_inherited_parms_p)
4528 DECL_CHAIN (DECL_CHAIN (DECL_ARGUMENTS (clone))) = NULL_TREE;
4530 for (parms = DECL_ARGUMENTS (clone); parms; parms = DECL_CHAIN (parms))
4532 DECL_CONTEXT (parms) = clone;
4533 cxx_dup_lang_specific_decl (parms);
4536 /* Create the RTL for this function. */
4537 SET_DECL_RTL (clone, NULL);
4538 rest_of_decl_compilation (clone, /*top_level=*/1, at_eof);
4540 return clone;
4543 /* Implementation of DECL_CLONED_FUNCTION and DECL_CLONED_FUNCTION_P, do
4544 not invoke this function directly.
4546 For a non-thunk function, returns the address of the slot for storing
4547 the function it is a clone of. Otherwise returns NULL_TREE.
4549 If JUST_TESTING, looks through TEMPLATE_DECL and returns NULL if
4550 cloned_function is unset. This is to support the separate
4551 DECL_CLONED_FUNCTION and DECL_CLONED_FUNCTION_P modes; using the latter
4552 on a template makes sense, but not the former. */
4554 tree *
4555 decl_cloned_function_p (const_tree decl, bool just_testing)
4557 tree *ptr;
4558 if (just_testing)
4559 decl = STRIP_TEMPLATE (decl);
4561 if (TREE_CODE (decl) != FUNCTION_DECL
4562 || !DECL_LANG_SPECIFIC (decl)
4563 || DECL_LANG_SPECIFIC (decl)->u.fn.thunk_p)
4565 #if defined ENABLE_TREE_CHECKING && (GCC_VERSION >= 2007)
4566 if (!just_testing)
4567 lang_check_failed (__FILE__, __LINE__, __FUNCTION__);
4568 else
4569 #endif
4570 return NULL;
4573 ptr = &DECL_LANG_SPECIFIC (decl)->u.fn.u5.cloned_function;
4574 if (just_testing && *ptr == NULL_TREE)
4575 return NULL;
4576 else
4577 return ptr;
4580 /* Produce declarations for all appropriate clones of FN. If
4581 UPDATE_METHODS is true, the clones are added to the
4582 CLASSTYPE_MEMBER_VEC. */
4584 void
4585 clone_function_decl (tree fn, bool update_methods)
4587 tree clone;
4589 /* Avoid inappropriate cloning. */
4590 if (DECL_CHAIN (fn)
4591 && DECL_CLONED_FUNCTION_P (DECL_CHAIN (fn)))
4592 return;
4594 if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (fn))
4596 /* For each constructor, we need two variants: an in-charge version
4597 and a not-in-charge version. */
4598 clone = build_clone (fn, complete_ctor_identifier);
4599 if (update_methods)
4600 add_method (DECL_CONTEXT (clone), clone, false);
4601 clone = build_clone (fn, base_ctor_identifier);
4602 if (update_methods)
4603 add_method (DECL_CONTEXT (clone), clone, false);
4605 else
4607 gcc_assert (DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (fn));
4609 /* For each destructor, we need three variants: an in-charge
4610 version, a not-in-charge version, and an in-charge deleting
4611 version. We clone the deleting version first because that
4612 means it will go second on the TYPE_FIELDS list -- and that
4613 corresponds to the correct layout order in the virtual
4614 function table.
4616 For a non-virtual destructor, we do not build a deleting
4617 destructor. */
4618 if (DECL_VIRTUAL_P (fn))
4620 clone = build_clone (fn, deleting_dtor_identifier);
4621 if (update_methods)
4622 add_method (DECL_CONTEXT (clone), clone, false);
4624 clone = build_clone (fn, complete_dtor_identifier);
4625 if (update_methods)
4626 add_method (DECL_CONTEXT (clone), clone, false);
4627 clone = build_clone (fn, base_dtor_identifier);
4628 if (update_methods)
4629 add_method (DECL_CONTEXT (clone), clone, false);
4632 /* Note that this is an abstract function that is never emitted. */
4633 DECL_ABSTRACT_P (fn) = true;
4636 /* DECL is an in charge constructor, which is being defined. This will
4637 have had an in class declaration, from whence clones were
4638 declared. An out-of-class definition can specify additional default
4639 arguments. As it is the clones that are involved in overload
4640 resolution, we must propagate the information from the DECL to its
4641 clones. */
4643 void
4644 adjust_clone_args (tree decl)
4646 tree clone;
4648 for (clone = DECL_CHAIN (decl); clone && DECL_CLONED_FUNCTION_P (clone);
4649 clone = DECL_CHAIN (clone))
4651 tree orig_clone_parms = TYPE_ARG_TYPES (TREE_TYPE (clone));
4652 tree orig_decl_parms = TYPE_ARG_TYPES (TREE_TYPE (decl));
4653 tree decl_parms, clone_parms;
4655 clone_parms = orig_clone_parms;
4657 /* Skip the 'this' parameter. */
4658 orig_clone_parms = TREE_CHAIN (orig_clone_parms);
4659 orig_decl_parms = TREE_CHAIN (orig_decl_parms);
4661 if (DECL_HAS_IN_CHARGE_PARM_P (decl))
4662 orig_decl_parms = TREE_CHAIN (orig_decl_parms);
4663 if (DECL_HAS_VTT_PARM_P (decl))
4664 orig_decl_parms = TREE_CHAIN (orig_decl_parms);
4666 clone_parms = orig_clone_parms;
4667 if (DECL_HAS_VTT_PARM_P (clone))
4668 clone_parms = TREE_CHAIN (clone_parms);
4670 for (decl_parms = orig_decl_parms; decl_parms;
4671 decl_parms = TREE_CHAIN (decl_parms),
4672 clone_parms = TREE_CHAIN (clone_parms))
4674 if (clone_parms == void_list_node)
4676 gcc_assert (decl_parms == clone_parms
4677 || ctor_omit_inherited_parms (clone));
4678 break;
4681 gcc_assert (same_type_p (TREE_TYPE (decl_parms),
4682 TREE_TYPE (clone_parms)));
4684 if (TREE_PURPOSE (decl_parms) && !TREE_PURPOSE (clone_parms))
4686 /* A default parameter has been added. Adjust the
4687 clone's parameters. */
4688 tree exceptions = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (clone));
4689 tree attrs = TYPE_ATTRIBUTES (TREE_TYPE (clone));
4690 tree basetype = TYPE_METHOD_BASETYPE (TREE_TYPE (clone));
4691 tree type;
4693 clone_parms = orig_decl_parms;
4695 if (DECL_HAS_VTT_PARM_P (clone))
4697 clone_parms = tree_cons (TREE_PURPOSE (orig_clone_parms),
4698 TREE_VALUE (orig_clone_parms),
4699 clone_parms);
4700 TREE_TYPE (clone_parms) = TREE_TYPE (orig_clone_parms);
4702 type = build_method_type_directly (basetype,
4703 TREE_TYPE (TREE_TYPE (clone)),
4704 clone_parms);
4705 if (exceptions)
4706 type = build_exception_variant (type, exceptions);
4707 if (attrs)
4708 type = cp_build_type_attribute_variant (type, attrs);
4709 TREE_TYPE (clone) = type;
4711 clone_parms = NULL_TREE;
4712 break;
4715 gcc_assert (!clone_parms || clone_parms == void_list_node);
4719 /* For each of the constructors and destructors in T, create an
4720 in-charge and not-in-charge variant. */
4722 static void
4723 clone_constructors_and_destructors (tree t)
4725 /* While constructors can be via a using declaration, at this point
4726 we no longer need to know that. */
4727 for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (t)); iter; ++iter)
4728 clone_function_decl (*iter, /*update_methods=*/true);
4730 if (tree dtor = CLASSTYPE_DESTRUCTOR (t))
4731 clone_function_decl (dtor, /*update_methods=*/true);
4734 /* Deduce noexcept for a destructor DTOR. */
4736 void
4737 deduce_noexcept_on_destructor (tree dtor)
4739 if (!TYPE_RAISES_EXCEPTIONS (TREE_TYPE (dtor)))
4740 TREE_TYPE (dtor) = build_exception_variant (TREE_TYPE (dtor),
4741 noexcept_deferred_spec);
4744 /* Subroutine of set_one_vmethod_tm_attributes. Search base classes
4745 of TYPE for virtual functions which FNDECL overrides. Return a
4746 mask of the tm attributes found therein. */
4748 static int
4749 look_for_tm_attr_overrides (tree type, tree fndecl)
4751 tree binfo = TYPE_BINFO (type);
4752 tree base_binfo;
4753 int ix, found = 0;
4755 for (ix = 0; BINFO_BASE_ITERATE (binfo, ix, base_binfo); ++ix)
4757 tree o, basetype = BINFO_TYPE (base_binfo);
4759 if (!TYPE_POLYMORPHIC_P (basetype))
4760 continue;
4762 o = look_for_overrides_here (basetype, fndecl);
4763 if (o)
4765 if (lookup_attribute ("transaction_safe_dynamic",
4766 DECL_ATTRIBUTES (o)))
4767 /* transaction_safe_dynamic is not inherited. */;
4768 else
4769 found |= tm_attr_to_mask (find_tm_attribute
4770 (TYPE_ATTRIBUTES (TREE_TYPE (o))));
4772 else
4773 found |= look_for_tm_attr_overrides (basetype, fndecl);
4776 return found;
4779 /* Subroutine of set_method_tm_attributes. Handle the checks and
4780 inheritance for one virtual method FNDECL. */
4782 static void
4783 set_one_vmethod_tm_attributes (tree type, tree fndecl)
4785 tree tm_attr;
4786 int found, have;
4788 found = look_for_tm_attr_overrides (type, fndecl);
4790 /* If FNDECL doesn't actually override anything (i.e. T is the
4791 class that first declares FNDECL virtual), then we're done. */
4792 if (found == 0)
4793 return;
4795 tm_attr = find_tm_attribute (TYPE_ATTRIBUTES (TREE_TYPE (fndecl)));
4796 have = tm_attr_to_mask (tm_attr);
4798 /* Intel STM Language Extension 3.0, Section 4.2 table 4:
4799 tm_pure must match exactly, otherwise no weakening of
4800 tm_safe > tm_callable > nothing. */
4801 /* ??? The tm_pure attribute didn't make the transition to the
4802 multivendor language spec. */
4803 if (have == TM_ATTR_PURE)
4805 if (found != TM_ATTR_PURE)
4807 found &= -found;
4808 goto err_override;
4811 /* If the overridden function is tm_pure, then FNDECL must be. */
4812 else if (found == TM_ATTR_PURE && tm_attr)
4813 goto err_override;
4814 /* Look for base class combinations that cannot be satisfied. */
4815 else if (found != TM_ATTR_PURE && (found & TM_ATTR_PURE))
4817 found &= ~TM_ATTR_PURE;
4818 found &= -found;
4819 error_at (DECL_SOURCE_LOCATION (fndecl),
4820 "method overrides both %<transaction_pure%> and %qE methods",
4821 tm_mask_to_attr (found));
4823 /* If FNDECL did not declare an attribute, then inherit the most
4824 restrictive one. */
4825 else if (tm_attr == NULL)
4827 apply_tm_attr (fndecl, tm_mask_to_attr (least_bit_hwi (found)));
4829 /* Otherwise validate that we're not weaker than a function
4830 that is being overridden. */
4831 else
4833 found &= -found;
4834 if (found <= TM_ATTR_CALLABLE && have > found)
4835 goto err_override;
4837 return;
4839 err_override:
4840 error_at (DECL_SOURCE_LOCATION (fndecl),
4841 "method declared %qE overriding %qE method",
4842 tm_attr, tm_mask_to_attr (found));
4845 /* For each of the methods in T, propagate a class-level tm attribute. */
4847 static void
4848 set_method_tm_attributes (tree t)
4850 tree class_tm_attr, fndecl;
4852 /* Don't bother collecting tm attributes if transactional memory
4853 support is not enabled. */
4854 if (!flag_tm)
4855 return;
4857 /* Process virtual methods first, as they inherit directly from the
4858 base virtual function and also require validation of new attributes. */
4859 if (TYPE_CONTAINS_VPTR_P (t))
4861 tree vchain;
4862 for (vchain = BINFO_VIRTUALS (TYPE_BINFO (t)); vchain;
4863 vchain = TREE_CHAIN (vchain))
4865 fndecl = BV_FN (vchain);
4866 if (DECL_THUNK_P (fndecl))
4867 fndecl = THUNK_TARGET (fndecl);
4868 set_one_vmethod_tm_attributes (t, fndecl);
4872 /* If the class doesn't have an attribute, nothing more to do. */
4873 class_tm_attr = find_tm_attribute (TYPE_ATTRIBUTES (t));
4874 if (class_tm_attr == NULL)
4875 return;
4877 /* Any method that does not yet have a tm attribute inherits
4878 the one from the class. */
4879 for (fndecl = TYPE_FIELDS (t); fndecl; fndecl = DECL_CHAIN (fndecl))
4880 if (DECL_DECLARES_FUNCTION_P (fndecl)
4881 && !find_tm_attribute (TYPE_ATTRIBUTES (TREE_TYPE (fndecl))))
4882 apply_tm_attr (fndecl, class_tm_attr);
4885 /* Returns true if FN is a default constructor. */
4887 bool
4888 default_ctor_p (tree fn)
4890 return (DECL_CONSTRUCTOR_P (fn)
4891 && sufficient_parms_p (FUNCTION_FIRST_USER_PARMTYPE (fn)));
4894 /* Returns true iff class T has a user-defined constructor that can be called
4895 with more than zero arguments. */
4897 bool
4898 type_has_user_nondefault_constructor (tree t)
4900 if (!TYPE_HAS_USER_CONSTRUCTOR (t))
4901 return false;
4903 for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (t)); iter; ++iter)
4905 tree fn = *iter;
4906 if (!DECL_ARTIFICIAL (fn)
4907 && (TREE_CODE (fn) == TEMPLATE_DECL
4908 || (skip_artificial_parms_for (fn, DECL_ARGUMENTS (fn))
4909 != NULL_TREE)))
4910 return true;
4913 return false;
4916 /* Returns the defaulted constructor if T has one. Otherwise, returns
4917 NULL_TREE. */
4919 tree
4920 in_class_defaulted_default_constructor (tree t)
4922 if (!TYPE_HAS_USER_CONSTRUCTOR (t))
4923 return NULL_TREE;
4925 for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (t)); iter; ++iter)
4927 tree fn = *iter;
4929 if (DECL_DEFAULTED_IN_CLASS_P (fn)
4930 && default_ctor_p (fn))
4931 return fn;
4934 return NULL_TREE;
4937 /* Returns true iff FN is a user-provided function, i.e. user-declared
4938 and not defaulted at its first declaration. */
4940 bool
4941 user_provided_p (tree fn)
4943 if (TREE_CODE (fn) == TEMPLATE_DECL)
4944 return true;
4945 else
4946 return (!DECL_ARTIFICIAL (fn)
4947 && !(DECL_INITIALIZED_IN_CLASS_P (fn)
4948 && (DECL_DEFAULTED_FN (fn) || DECL_DELETED_FN (fn))));
4951 /* Returns true iff class T has a user-provided constructor. */
4953 bool
4954 type_has_user_provided_constructor (tree t)
4956 if (!CLASS_TYPE_P (t))
4957 return false;
4959 if (!TYPE_HAS_USER_CONSTRUCTOR (t))
4960 return false;
4962 for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (t)); iter; ++iter)
4963 if (user_provided_p (*iter))
4964 return true;
4966 return false;
4969 /* Returns true iff class T has a user-provided or explicit constructor. */
4971 bool
4972 type_has_user_provided_or_explicit_constructor (tree t)
4974 if (!CLASS_TYPE_P (t))
4975 return false;
4977 if (!TYPE_HAS_USER_CONSTRUCTOR (t))
4978 return false;
4980 for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (t)); iter; ++iter)
4982 tree fn = *iter;
4983 if (user_provided_p (fn) || DECL_NONCONVERTING_P (fn))
4984 return true;
4987 return false;
4990 /* Returns true iff class T has a non-user-provided (i.e. implicitly
4991 declared or explicitly defaulted in the class body) default
4992 constructor. */
4994 bool
4995 type_has_non_user_provided_default_constructor (tree t)
4997 if (!TYPE_HAS_DEFAULT_CONSTRUCTOR (t))
4998 return false;
4999 if (CLASSTYPE_LAZY_DEFAULT_CTOR (t))
5000 return true;
5002 for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (t)); iter; ++iter)
5004 tree fn = *iter;
5005 if (TREE_CODE (fn) == FUNCTION_DECL
5006 && default_ctor_p (fn)
5007 && !user_provided_p (fn))
5008 return true;
5011 return false;
5014 /* TYPE is being used as a virtual base, and has a non-trivial move
5015 assignment. Return true if this is due to there being a user-provided
5016 move assignment in TYPE or one of its subobjects; if there isn't, then
5017 multiple move assignment can't cause any harm. */
5019 bool
5020 vbase_has_user_provided_move_assign (tree type)
5022 /* Does the type itself have a user-provided move assignment operator? */
5023 if (!CLASSTYPE_LAZY_MOVE_ASSIGN (type))
5024 for (ovl_iterator iter (get_class_binding_direct
5025 (type, assign_op_identifier));
5026 iter; ++iter)
5027 if (!DECL_ARTIFICIAL (*iter) && move_fn_p (*iter))
5028 return true;
5030 /* Do any of its bases? */
5031 tree binfo = TYPE_BINFO (type);
5032 tree base_binfo;
5033 for (int i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
5034 if (vbase_has_user_provided_move_assign (BINFO_TYPE (base_binfo)))
5035 return true;
5037 /* Or non-static data members? */
5038 for (tree field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
5040 if (TREE_CODE (field) == FIELD_DECL
5041 && CLASS_TYPE_P (TREE_TYPE (field))
5042 && vbase_has_user_provided_move_assign (TREE_TYPE (field)))
5043 return true;
5046 /* Seems not. */
5047 return false;
5050 /* If default-initialization leaves part of TYPE uninitialized, returns
5051 a DECL for the field or TYPE itself (DR 253). */
5053 tree
5054 default_init_uninitialized_part (tree type)
5056 tree t, r, binfo;
5057 int i;
5059 type = strip_array_types (type);
5060 if (!CLASS_TYPE_P (type))
5061 return type;
5062 if (!type_has_non_user_provided_default_constructor (type))
5063 return NULL_TREE;
5064 for (binfo = TYPE_BINFO (type), i = 0;
5065 BINFO_BASE_ITERATE (binfo, i, t); ++i)
5067 r = default_init_uninitialized_part (BINFO_TYPE (t));
5068 if (r)
5069 return r;
5071 for (t = TYPE_FIELDS (type); t; t = DECL_CHAIN (t))
5072 if (TREE_CODE (t) == FIELD_DECL
5073 && !DECL_ARTIFICIAL (t)
5074 && !DECL_INITIAL (t))
5076 r = default_init_uninitialized_part (TREE_TYPE (t));
5077 if (r)
5078 return DECL_P (r) ? r : t;
5081 return NULL_TREE;
5084 /* Returns true iff for class T, a trivial synthesized default constructor
5085 would be constexpr. */
5087 bool
5088 trivial_default_constructor_is_constexpr (tree t)
5090 /* A defaulted trivial default constructor is constexpr
5091 if there is nothing to initialize. */
5092 gcc_assert (!TYPE_HAS_COMPLEX_DFLT (t));
5093 return is_really_empty_class (t);
5096 /* Returns true iff class T has a constexpr default constructor. */
5098 bool
5099 type_has_constexpr_default_constructor (tree t)
5101 tree fns;
5103 if (!CLASS_TYPE_P (t))
5105 /* The caller should have stripped an enclosing array. */
5106 gcc_assert (TREE_CODE (t) != ARRAY_TYPE);
5107 return false;
5109 if (CLASSTYPE_LAZY_DEFAULT_CTOR (t))
5111 if (!TYPE_HAS_COMPLEX_DFLT (t))
5112 return trivial_default_constructor_is_constexpr (t);
5113 /* Non-trivial, we need to check subobject constructors. */
5114 lazily_declare_fn (sfk_constructor, t);
5116 fns = locate_ctor (t);
5117 return (fns && DECL_DECLARED_CONSTEXPR_P (fns));
5120 /* Returns true iff class T has a constexpr default constructor or has an
5121 implicitly declared default constructor that we can't tell if it's constexpr
5122 without forcing a lazy declaration (which might cause undesired
5123 instantiations). */
5125 bool
5126 type_maybe_constexpr_default_constructor (tree t)
5128 if (CLASS_TYPE_P (t) && CLASSTYPE_LAZY_DEFAULT_CTOR (t)
5129 && TYPE_HAS_COMPLEX_DFLT (t))
5130 /* Assume it's constexpr. */
5131 return true;
5132 return type_has_constexpr_default_constructor (t);
5135 /* Returns true iff class TYPE has a virtual destructor. */
5137 bool
5138 type_has_virtual_destructor (tree type)
5140 tree dtor;
5142 if (!CLASS_TYPE_P (type))
5143 return false;
5145 gcc_assert (COMPLETE_TYPE_P (type));
5146 dtor = CLASSTYPE_DESTRUCTOR (type);
5147 return (dtor && DECL_VIRTUAL_P (dtor));
5150 /* Returns true iff T, a class, has a move-assignment or
5151 move-constructor. Does not lazily declare either.
5152 If USER_P is false, any move function will do. If it is true, the
5153 move function must be user-declared.
5155 Note that user-declared here is different from "user-provided",
5156 which doesn't include functions that are defaulted in the
5157 class. */
5159 bool
5160 classtype_has_move_assign_or_move_ctor_p (tree t, bool user_p)
5162 gcc_assert (user_p
5163 || (!CLASSTYPE_LAZY_MOVE_CTOR (t)
5164 && !CLASSTYPE_LAZY_MOVE_ASSIGN (t)));
5166 if (!CLASSTYPE_LAZY_MOVE_CTOR (t))
5167 for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (t)); iter; ++iter)
5168 if ((!user_p || !DECL_ARTIFICIAL (*iter)) && move_fn_p (*iter))
5169 return true;
5171 if (!CLASSTYPE_LAZY_MOVE_ASSIGN (t))
5172 for (ovl_iterator iter (get_class_binding_direct
5173 (t, assign_op_identifier));
5174 iter; ++iter)
5175 if ((!user_p || !DECL_ARTIFICIAL (*iter)) && move_fn_p (*iter))
5176 return true;
5178 return false;
5181 /* Nonzero if we need to build up a constructor call when initializing an
5182 object of this class, either because it has a user-declared constructor
5183 or because it doesn't have a default constructor (so we need to give an
5184 error if no initializer is provided). Use TYPE_NEEDS_CONSTRUCTING when
5185 what you care about is whether or not an object can be produced by a
5186 constructor (e.g. so we don't set TREE_READONLY on const variables of
5187 such type); use this function when what you care about is whether or not
5188 to try to call a constructor to create an object. The latter case is
5189 the former plus some cases of constructors that cannot be called. */
5191 bool
5192 type_build_ctor_call (tree t)
5194 tree inner;
5195 if (TYPE_NEEDS_CONSTRUCTING (t))
5196 return true;
5197 inner = strip_array_types (t);
5198 if (!CLASS_TYPE_P (inner) || ANON_AGGR_TYPE_P (inner))
5199 return false;
5200 if (!TYPE_HAS_DEFAULT_CONSTRUCTOR (inner))
5201 return true;
5202 if (cxx_dialect < cxx11)
5203 return false;
5204 /* A user-declared constructor might be private, and a constructor might
5205 be trivial but deleted. */
5206 for (ovl_iterator iter (get_class_binding (inner, complete_ctor_identifier));
5207 iter; ++iter)
5209 tree fn = *iter;
5210 if (!DECL_ARTIFICIAL (fn)
5211 || DECL_DELETED_FN (fn))
5212 return true;
5214 return false;
5217 /* Like type_build_ctor_call, but for destructors. */
5219 bool
5220 type_build_dtor_call (tree t)
5222 tree inner;
5223 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t))
5224 return true;
5225 inner = strip_array_types (t);
5226 if (!CLASS_TYPE_P (inner) || ANON_AGGR_TYPE_P (inner)
5227 || !COMPLETE_TYPE_P (inner))
5228 return false;
5229 if (cxx_dialect < cxx11)
5230 return false;
5231 /* A user-declared destructor might be private, and a destructor might
5232 be trivial but deleted. */
5233 for (ovl_iterator iter (get_class_binding (inner, complete_dtor_identifier));
5234 iter; ++iter)
5236 tree fn = *iter;
5237 if (!DECL_ARTIFICIAL (fn)
5238 || DECL_DELETED_FN (fn))
5239 return true;
5241 return false;
5244 /* Remove all zero-width bit-fields from T. */
5246 static void
5247 remove_zero_width_bit_fields (tree t)
5249 tree *fieldsp;
5251 fieldsp = &TYPE_FIELDS (t);
5252 while (*fieldsp)
5254 if (TREE_CODE (*fieldsp) == FIELD_DECL
5255 && DECL_C_BIT_FIELD (*fieldsp)
5256 /* We should not be confused by the fact that grokbitfield
5257 temporarily sets the width of the bit field into
5258 DECL_BIT_FIELD_REPRESENTATIVE (*fieldsp).
5259 check_bitfield_decl eventually sets DECL_SIZE (*fieldsp)
5260 to that width. */
5261 && (DECL_SIZE (*fieldsp) == NULL_TREE
5262 || integer_zerop (DECL_SIZE (*fieldsp))))
5263 *fieldsp = DECL_CHAIN (*fieldsp);
5264 else
5265 fieldsp = &DECL_CHAIN (*fieldsp);
5269 /* Returns TRUE iff we need a cookie when dynamically allocating an
5270 array whose elements have the indicated class TYPE. */
5272 static bool
5273 type_requires_array_cookie (tree type)
5275 tree fns;
5276 bool has_two_argument_delete_p = false;
5278 gcc_assert (CLASS_TYPE_P (type));
5280 /* If there's a non-trivial destructor, we need a cookie. In order
5281 to iterate through the array calling the destructor for each
5282 element, we'll have to know how many elements there are. */
5283 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
5284 return true;
5286 /* If the usual deallocation function is a two-argument whose second
5287 argument is of type `size_t', then we have to pass the size of
5288 the array to the deallocation function, so we will need to store
5289 a cookie. */
5290 fns = lookup_fnfields (TYPE_BINFO (type),
5291 ovl_op_identifier (false, VEC_DELETE_EXPR),
5292 /*protect=*/0);
5293 /* If there are no `operator []' members, or the lookup is
5294 ambiguous, then we don't need a cookie. */
5295 if (!fns || fns == error_mark_node)
5296 return false;
5297 /* Loop through all of the functions. */
5298 for (lkp_iterator iter (BASELINK_FUNCTIONS (fns)); iter; ++iter)
5300 tree fn = *iter;
5302 /* See if this function is a one-argument delete function. If
5303 it is, then it will be the usual deallocation function. */
5304 tree second_parm = TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (fn)));
5305 if (second_parm == void_list_node)
5306 return false;
5307 /* Do not consider this function if its second argument is an
5308 ellipsis. */
5309 if (!second_parm)
5310 continue;
5311 /* Otherwise, if we have a two-argument function and the second
5312 argument is `size_t', it will be the usual deallocation
5313 function -- unless there is one-argument function, too. */
5314 if (TREE_CHAIN (second_parm) == void_list_node
5315 && same_type_p (TREE_VALUE (second_parm), size_type_node))
5316 has_two_argument_delete_p = true;
5319 return has_two_argument_delete_p;
5322 /* Finish computing the `literal type' property of class type T.
5324 At this point, we have already processed base classes and
5325 non-static data members. We need to check whether the copy
5326 constructor is trivial, the destructor is trivial, and there
5327 is a trivial default constructor or at least one constexpr
5328 constructor other than the copy constructor. */
5330 static void
5331 finalize_literal_type_property (tree t)
5333 tree fn;
5335 if (cxx_dialect < cxx11
5336 || TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t))
5337 CLASSTYPE_LITERAL_P (t) = false;
5338 else if (CLASSTYPE_LITERAL_P (t) && LAMBDA_TYPE_P (t))
5339 CLASSTYPE_LITERAL_P (t) = (cxx_dialect >= cxx17);
5340 else if (CLASSTYPE_LITERAL_P (t) && !TYPE_HAS_TRIVIAL_DFLT (t)
5341 && CLASSTYPE_NON_AGGREGATE (t)
5342 && !TYPE_HAS_CONSTEXPR_CTOR (t))
5343 CLASSTYPE_LITERAL_P (t) = false;
5345 /* C++14 DR 1684 removed this restriction. */
5346 if (cxx_dialect < cxx14
5347 && !CLASSTYPE_LITERAL_P (t) && !LAMBDA_TYPE_P (t))
5348 for (fn = TYPE_FIELDS (t); fn; fn = DECL_CHAIN (fn))
5349 if (TREE_CODE (fn) == FUNCTION_DECL
5350 && DECL_DECLARED_CONSTEXPR_P (fn)
5351 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
5352 && !DECL_CONSTRUCTOR_P (fn))
5354 DECL_DECLARED_CONSTEXPR_P (fn) = false;
5355 if (!DECL_GENERATED_P (fn)
5356 && pedwarn (DECL_SOURCE_LOCATION (fn), OPT_Wpedantic,
5357 "enclosing class of %<constexpr%> non-static member "
5358 "function %q+#D is not a literal type", fn))
5359 explain_non_literal_class (t);
5363 /* T is a non-literal type used in a context which requires a constant
5364 expression. Explain why it isn't literal. */
5366 void
5367 explain_non_literal_class (tree t)
5369 static hash_set<tree> *diagnosed;
5371 if (!CLASS_TYPE_P (t))
5372 return;
5373 t = TYPE_MAIN_VARIANT (t);
5375 if (diagnosed == NULL)
5376 diagnosed = new hash_set<tree>;
5377 if (diagnosed->add (t))
5378 /* Already explained. */
5379 return;
5381 inform (UNKNOWN_LOCATION, "%q+T is not literal because:", t);
5382 if (cxx_dialect < cxx17 && LAMBDA_TYPE_P (t))
5383 inform (UNKNOWN_LOCATION,
5384 " %qT is a closure type, which is only literal in "
5385 "C++17 and later", t);
5386 else if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t))
5387 inform (UNKNOWN_LOCATION, " %q+T has a non-trivial destructor", t);
5388 else if (CLASSTYPE_NON_AGGREGATE (t)
5389 && !TYPE_HAS_TRIVIAL_DFLT (t)
5390 && !LAMBDA_TYPE_P (t)
5391 && !TYPE_HAS_CONSTEXPR_CTOR (t))
5393 inform (UNKNOWN_LOCATION,
5394 " %q+T is not an aggregate, does not have a trivial "
5395 "default constructor, and has no %<constexpr%> constructor that "
5396 "is not a copy or move constructor", t);
5397 if (type_has_non_user_provided_default_constructor (t))
5398 /* Note that we can't simply call locate_ctor because when the
5399 constructor is deleted it just returns NULL_TREE. */
5400 for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (t)); iter; ++iter)
5402 tree fn = *iter;
5403 tree parms = TYPE_ARG_TYPES (TREE_TYPE (fn));
5405 parms = skip_artificial_parms_for (fn, parms);
5407 if (sufficient_parms_p (parms))
5409 if (DECL_DELETED_FN (fn))
5410 maybe_explain_implicit_delete (fn);
5411 else
5412 explain_invalid_constexpr_fn (fn);
5413 break;
5417 else
5419 tree binfo, base_binfo, field; int i;
5420 for (binfo = TYPE_BINFO (t), i = 0;
5421 BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
5423 tree basetype = TREE_TYPE (base_binfo);
5424 if (!CLASSTYPE_LITERAL_P (basetype))
5426 inform (UNKNOWN_LOCATION,
5427 " base class %qT of %q+T is non-literal",
5428 basetype, t);
5429 explain_non_literal_class (basetype);
5430 return;
5433 for (field = TYPE_FIELDS (t); field; field = TREE_CHAIN (field))
5435 tree ftype;
5436 if (TREE_CODE (field) != FIELD_DECL)
5437 continue;
5438 ftype = TREE_TYPE (field);
5439 if (!literal_type_p (ftype))
5441 inform (DECL_SOURCE_LOCATION (field),
5442 " non-static data member %qD has non-literal type",
5443 field);
5444 if (CLASS_TYPE_P (ftype))
5445 explain_non_literal_class (ftype);
5447 if (CP_TYPE_VOLATILE_P (ftype))
5448 inform (DECL_SOURCE_LOCATION (field),
5449 " non-static data member %qD has volatile type", field);
5454 /* Check the validity of the bases and members declared in T. Add any
5455 implicitly-generated functions (like copy-constructors and
5456 assignment operators). Compute various flag bits (like
5457 CLASSTYPE_NON_LAYOUT_POD_T) for T. This routine works purely at the C++
5458 level: i.e., independently of the ABI in use. */
5460 static void
5461 check_bases_and_members (tree t)
5463 /* Nonzero if the implicitly generated copy constructor should take
5464 a non-const reference argument. */
5465 int cant_have_const_ctor;
5466 /* Nonzero if the implicitly generated assignment operator
5467 should take a non-const reference argument. */
5468 int no_const_asn_ref;
5469 tree access_decls;
5470 bool saved_complex_asn_ref;
5471 bool saved_nontrivial_dtor;
5472 tree fn;
5474 /* By default, we use const reference arguments and generate default
5475 constructors. */
5476 cant_have_const_ctor = 0;
5477 no_const_asn_ref = 0;
5479 /* Check all the base-classes and set FMEM members to point to arrays
5480 of potential interest. */
5481 check_bases (t, &cant_have_const_ctor, &no_const_asn_ref);
5483 /* Deduce noexcept on destructor. This needs to happen after we've set
5484 triviality flags appropriately for our bases. */
5485 if (cxx_dialect >= cxx11)
5486 if (tree dtor = CLASSTYPE_DESTRUCTOR (t))
5487 deduce_noexcept_on_destructor (dtor);
5489 /* Check all the method declarations. */
5490 check_methods (t);
5492 /* Save the initial values of these flags which only indicate whether
5493 or not the class has user-provided functions. As we analyze the
5494 bases and members we can set these flags for other reasons. */
5495 saved_complex_asn_ref = TYPE_HAS_COMPLEX_COPY_ASSIGN (t);
5496 saved_nontrivial_dtor = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t);
5498 /* Check all the data member declarations. We cannot call
5499 check_field_decls until we have called check_bases check_methods,
5500 as check_field_decls depends on TYPE_HAS_NONTRIVIAL_DESTRUCTOR
5501 being set appropriately. */
5502 check_field_decls (t, &access_decls,
5503 &cant_have_const_ctor,
5504 &no_const_asn_ref);
5506 /* A nearly-empty class has to be vptr-containing; a nearly empty
5507 class contains just a vptr. */
5508 if (!TYPE_CONTAINS_VPTR_P (t))
5509 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
5511 /* Do some bookkeeping that will guide the generation of implicitly
5512 declared member functions. */
5513 TYPE_HAS_COMPLEX_COPY_CTOR (t) |= TYPE_CONTAINS_VPTR_P (t);
5514 TYPE_HAS_COMPLEX_MOVE_CTOR (t) |= TYPE_CONTAINS_VPTR_P (t);
5515 /* We need to call a constructor for this class if it has a
5516 user-provided constructor, or if the default constructor is going
5517 to initialize the vptr. (This is not an if-and-only-if;
5518 TYPE_NEEDS_CONSTRUCTING is set elsewhere if bases or members
5519 themselves need constructing.) */
5520 TYPE_NEEDS_CONSTRUCTING (t)
5521 |= (type_has_user_provided_constructor (t) || TYPE_CONTAINS_VPTR_P (t));
5522 /* [dcl.init.aggr]
5524 An aggregate is an array or a class with no user-provided
5525 constructors ... and no virtual functions.
5527 Again, other conditions for being an aggregate are checked
5528 elsewhere. */
5529 CLASSTYPE_NON_AGGREGATE (t)
5530 |= (type_has_user_provided_or_explicit_constructor (t)
5531 || TYPE_POLYMORPHIC_P (t));
5532 /* This is the C++98/03 definition of POD; it changed in C++0x, but we
5533 retain the old definition internally for ABI reasons. */
5534 CLASSTYPE_NON_LAYOUT_POD_P (t)
5535 |= (CLASSTYPE_NON_AGGREGATE (t)
5536 || saved_nontrivial_dtor || saved_complex_asn_ref);
5537 CLASSTYPE_NON_STD_LAYOUT (t) |= TYPE_CONTAINS_VPTR_P (t);
5538 TYPE_HAS_COMPLEX_COPY_ASSIGN (t) |= TYPE_CONTAINS_VPTR_P (t);
5539 TYPE_HAS_COMPLEX_MOVE_ASSIGN (t) |= TYPE_CONTAINS_VPTR_P (t);
5540 TYPE_HAS_COMPLEX_DFLT (t) |= TYPE_CONTAINS_VPTR_P (t);
5542 /* If the only explicitly declared default constructor is user-provided,
5543 set TYPE_HAS_COMPLEX_DFLT. */
5544 if (!TYPE_HAS_COMPLEX_DFLT (t)
5545 && TYPE_HAS_DEFAULT_CONSTRUCTOR (t)
5546 && !type_has_non_user_provided_default_constructor (t))
5547 TYPE_HAS_COMPLEX_DFLT (t) = true;
5549 /* Warn if a public base of a polymorphic type has an accessible
5550 non-virtual destructor. It is only now that we know the class is
5551 polymorphic. Although a polymorphic base will have a already
5552 been diagnosed during its definition, we warn on use too. */
5553 if (TYPE_POLYMORPHIC_P (t) && warn_nonvdtor)
5555 tree binfo = TYPE_BINFO (t);
5556 vec<tree, va_gc> *accesses = BINFO_BASE_ACCESSES (binfo);
5557 tree base_binfo;
5558 unsigned i;
5560 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
5562 tree basetype = TREE_TYPE (base_binfo);
5564 if ((*accesses)[i] == access_public_node
5565 && (TYPE_POLYMORPHIC_P (basetype) || warn_ecpp)
5566 && accessible_nvdtor_p (basetype))
5567 warning (OPT_Wnon_virtual_dtor,
5568 "base class %q#T has accessible non-virtual destructor",
5569 basetype);
5573 /* If the class has no user-declared constructor, but does have
5574 non-static const or reference data members that can never be
5575 initialized, issue a warning. */
5576 if (warn_uninitialized
5577 /* Classes with user-declared constructors are presumed to
5578 initialize these members. */
5579 && !TYPE_HAS_USER_CONSTRUCTOR (t)
5580 /* Aggregates can be initialized with brace-enclosed
5581 initializers. */
5582 && CLASSTYPE_NON_AGGREGATE (t))
5584 tree field;
5586 for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
5588 tree type;
5590 if (TREE_CODE (field) != FIELD_DECL
5591 || DECL_INITIAL (field) != NULL_TREE)
5592 continue;
5594 type = TREE_TYPE (field);
5595 if (TREE_CODE (type) == REFERENCE_TYPE)
5596 warning_at (DECL_SOURCE_LOCATION (field),
5597 OPT_Wuninitialized, "non-static reference %q#D "
5598 "in class without a constructor", field);
5599 else if (CP_TYPE_CONST_P (type)
5600 && (!CLASS_TYPE_P (type)
5601 || !TYPE_HAS_DEFAULT_CONSTRUCTOR (type)))
5602 warning_at (DECL_SOURCE_LOCATION (field),
5603 OPT_Wuninitialized, "non-static const member %q#D "
5604 "in class without a constructor", field);
5608 /* Synthesize any needed methods. */
5609 add_implicitly_declared_members (t, &access_decls,
5610 cant_have_const_ctor,
5611 no_const_asn_ref);
5613 /* Check defaulted declarations here so we have cant_have_const_ctor
5614 and don't need to worry about clones. */
5615 for (fn = TYPE_FIELDS (t); fn; fn = DECL_CHAIN (fn))
5616 if (DECL_DECLARES_FUNCTION_P (fn)
5617 && !DECL_ARTIFICIAL (fn)
5618 && DECL_DEFAULTED_IN_CLASS_P (fn))
5620 int copy = copy_fn_p (fn);
5621 if (copy > 0)
5623 bool imp_const_p
5624 = (DECL_CONSTRUCTOR_P (fn) ? !cant_have_const_ctor
5625 : !no_const_asn_ref);
5626 bool fn_const_p = (copy == 2);
5628 if (fn_const_p && !imp_const_p)
5629 /* If the function is defaulted outside the class, we just
5630 give the synthesis error. */
5631 error ("%q+D declared to take const reference, but implicit "
5632 "declaration would take non-const", fn);
5634 defaulted_late_check (fn);
5637 if (LAMBDA_TYPE_P (t))
5639 /* "This class type is not an aggregate." */
5640 CLASSTYPE_NON_AGGREGATE (t) = 1;
5643 /* Compute the 'literal type' property before we
5644 do anything with non-static member functions. */
5645 finalize_literal_type_property (t);
5647 /* Create the in-charge and not-in-charge variants of constructors
5648 and destructors. */
5649 clone_constructors_and_destructors (t);
5651 /* Process the using-declarations. */
5652 for (; access_decls; access_decls = TREE_CHAIN (access_decls))
5653 handle_using_decl (TREE_VALUE (access_decls), t);
5655 /* Figure out whether or not we will need a cookie when dynamically
5656 allocating an array of this type. */
5657 LANG_TYPE_CLASS_CHECK (t)->vec_new_uses_cookie
5658 = type_requires_array_cookie (t);
5661 /* If T needs a pointer to its virtual function table, set TYPE_VFIELD
5662 accordingly. If a new vfield was created (because T doesn't have a
5663 primary base class), then the newly created field is returned. It
5664 is not added to the TYPE_FIELDS list; it is the caller's
5665 responsibility to do that. Accumulate declared virtual functions
5666 on VIRTUALS_P. */
5668 static tree
5669 create_vtable_ptr (tree t, tree* virtuals_p)
5671 tree fn;
5673 /* Collect the virtual functions declared in T. */
5674 for (fn = TYPE_FIELDS (t); fn; fn = DECL_CHAIN (fn))
5675 if (TREE_CODE (fn) == FUNCTION_DECL
5676 && DECL_VINDEX (fn) && !DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (fn)
5677 && TREE_CODE (DECL_VINDEX (fn)) != INTEGER_CST)
5679 tree new_virtual = make_node (TREE_LIST);
5681 BV_FN (new_virtual) = fn;
5682 BV_DELTA (new_virtual) = integer_zero_node;
5683 BV_VCALL_INDEX (new_virtual) = NULL_TREE;
5685 TREE_CHAIN (new_virtual) = *virtuals_p;
5686 *virtuals_p = new_virtual;
5689 /* If we couldn't find an appropriate base class, create a new field
5690 here. Even if there weren't any new virtual functions, we might need a
5691 new virtual function table if we're supposed to include vptrs in
5692 all classes that need them. */
5693 if (!TYPE_VFIELD (t) && (*virtuals_p || TYPE_CONTAINS_VPTR_P (t)))
5695 /* We build this decl with vtbl_ptr_type_node, which is a
5696 `vtable_entry_type*'. It might seem more precise to use
5697 `vtable_entry_type (*)[N]' where N is the number of virtual
5698 functions. However, that would require the vtable pointer in
5699 base classes to have a different type than the vtable pointer
5700 in derived classes. We could make that happen, but that
5701 still wouldn't solve all the problems. In particular, the
5702 type-based alias analysis code would decide that assignments
5703 to the base class vtable pointer can't alias assignments to
5704 the derived class vtable pointer, since they have different
5705 types. Thus, in a derived class destructor, where the base
5706 class constructor was inlined, we could generate bad code for
5707 setting up the vtable pointer.
5709 Therefore, we use one type for all vtable pointers. We still
5710 use a type-correct type; it's just doesn't indicate the array
5711 bounds. That's better than using `void*' or some such; it's
5712 cleaner, and it let's the alias analysis code know that these
5713 stores cannot alias stores to void*! */
5714 tree field;
5716 field = build_decl (input_location,
5717 FIELD_DECL, get_vfield_name (t), vtbl_ptr_type_node);
5718 DECL_VIRTUAL_P (field) = 1;
5719 DECL_ARTIFICIAL (field) = 1;
5720 DECL_FIELD_CONTEXT (field) = t;
5721 DECL_FCONTEXT (field) = t;
5722 if (TYPE_PACKED (t))
5723 DECL_PACKED (field) = 1;
5725 TYPE_VFIELD (t) = field;
5727 /* This class is non-empty. */
5728 CLASSTYPE_EMPTY_P (t) = 0;
5730 return field;
5733 return NULL_TREE;
5736 /* Add OFFSET to all base types of BINFO which is a base in the
5737 hierarchy dominated by T.
5739 OFFSET, which is a type offset, is number of bytes. */
5741 static void
5742 propagate_binfo_offsets (tree binfo, tree offset)
5744 int i;
5745 tree primary_binfo;
5746 tree base_binfo;
5748 /* Update BINFO's offset. */
5749 BINFO_OFFSET (binfo)
5750 = fold_convert (sizetype,
5751 size_binop (PLUS_EXPR,
5752 fold_convert (ssizetype, BINFO_OFFSET (binfo)),
5753 offset));
5755 /* Find the primary base class. */
5756 primary_binfo = get_primary_binfo (binfo);
5758 if (primary_binfo && BINFO_INHERITANCE_CHAIN (primary_binfo) == binfo)
5759 propagate_binfo_offsets (primary_binfo, offset);
5761 /* Scan all of the bases, pushing the BINFO_OFFSET adjust
5762 downwards. */
5763 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
5765 /* Don't do the primary base twice. */
5766 if (base_binfo == primary_binfo)
5767 continue;
5769 if (BINFO_VIRTUAL_P (base_binfo))
5770 continue;
5772 propagate_binfo_offsets (base_binfo, offset);
5776 /* Set BINFO_OFFSET for all of the virtual bases for RLI->T. Update
5777 TYPE_ALIGN and TYPE_SIZE for T. OFFSETS gives the location of
5778 empty subobjects of T. */
5780 static void
5781 layout_virtual_bases (record_layout_info rli, splay_tree offsets)
5783 tree vbase;
5784 tree t = rli->t;
5785 tree *next_field;
5787 if (BINFO_N_BASE_BINFOS (TYPE_BINFO (t)) == 0)
5788 return;
5790 /* Find the last field. The artificial fields created for virtual
5791 bases will go after the last extant field to date. */
5792 next_field = &TYPE_FIELDS (t);
5793 while (*next_field)
5794 next_field = &DECL_CHAIN (*next_field);
5796 /* Go through the virtual bases, allocating space for each virtual
5797 base that is not already a primary base class. These are
5798 allocated in inheritance graph order. */
5799 for (vbase = TYPE_BINFO (t); vbase; vbase = TREE_CHAIN (vbase))
5801 if (!BINFO_VIRTUAL_P (vbase))
5802 continue;
5804 if (!BINFO_PRIMARY_P (vbase))
5806 /* This virtual base is not a primary base of any class in the
5807 hierarchy, so we have to add space for it. */
5808 next_field = build_base_field (rli, vbase,
5809 offsets, next_field);
5814 /* Returns the offset of the byte just past the end of the base class
5815 BINFO. */
5817 static tree
5818 end_of_base (tree binfo)
5820 tree size;
5822 if (!CLASSTYPE_AS_BASE (BINFO_TYPE (binfo)))
5823 size = TYPE_SIZE_UNIT (char_type_node);
5824 else if (is_empty_class (BINFO_TYPE (binfo)))
5825 /* An empty class has zero CLASSTYPE_SIZE_UNIT, but we need to
5826 allocate some space for it. It cannot have virtual bases, so
5827 TYPE_SIZE_UNIT is fine. */
5828 size = TYPE_SIZE_UNIT (BINFO_TYPE (binfo));
5829 else
5830 size = CLASSTYPE_SIZE_UNIT (BINFO_TYPE (binfo));
5832 return size_binop (PLUS_EXPR, BINFO_OFFSET (binfo), size);
5835 /* Returns the offset of the byte just past the end of the base class
5836 with the highest offset in T. If INCLUDE_VIRTUALS_P is zero, then
5837 only non-virtual bases are included. */
5839 static tree
5840 end_of_class (tree t, int include_virtuals_p)
5842 tree result = size_zero_node;
5843 vec<tree, va_gc> *vbases;
5844 tree binfo;
5845 tree base_binfo;
5846 tree offset;
5847 int i;
5849 for (binfo = TYPE_BINFO (t), i = 0;
5850 BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
5852 if (!include_virtuals_p
5853 && BINFO_VIRTUAL_P (base_binfo)
5854 && (!BINFO_PRIMARY_P (base_binfo)
5855 || BINFO_INHERITANCE_CHAIN (base_binfo) != TYPE_BINFO (t)))
5856 continue;
5858 offset = end_of_base (base_binfo);
5859 if (tree_int_cst_lt (result, offset))
5860 result = offset;
5863 if (include_virtuals_p)
5864 for (vbases = CLASSTYPE_VBASECLASSES (t), i = 0;
5865 vec_safe_iterate (vbases, i, &base_binfo); i++)
5867 offset = end_of_base (base_binfo);
5868 if (tree_int_cst_lt (result, offset))
5869 result = offset;
5872 return result;
5875 /* Warn about bases of T that are inaccessible because they are
5876 ambiguous. For example:
5878 struct S {};
5879 struct T : public S {};
5880 struct U : public S, public T {};
5882 Here, `(S*) new U' is not allowed because there are two `S'
5883 subobjects of U. */
5885 static void
5886 warn_about_ambiguous_bases (tree t)
5888 int i;
5889 vec<tree, va_gc> *vbases;
5890 tree basetype;
5891 tree binfo;
5892 tree base_binfo;
5894 /* If there are no repeated bases, nothing can be ambiguous. */
5895 if (!CLASSTYPE_REPEATED_BASE_P (t))
5896 return;
5898 /* Check direct bases. */
5899 for (binfo = TYPE_BINFO (t), i = 0;
5900 BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
5902 basetype = BINFO_TYPE (base_binfo);
5904 if (!uniquely_derived_from_p (basetype, t))
5905 warning (0, "direct base %qT inaccessible in %qT due to ambiguity",
5906 basetype, t);
5909 /* Check for ambiguous virtual bases. */
5910 if (extra_warnings)
5911 for (vbases = CLASSTYPE_VBASECLASSES (t), i = 0;
5912 vec_safe_iterate (vbases, i, &binfo); i++)
5914 basetype = BINFO_TYPE (binfo);
5916 if (!uniquely_derived_from_p (basetype, t))
5917 warning (OPT_Wextra, "virtual base %qT inaccessible in %qT due "
5918 "to ambiguity", basetype, t);
5922 /* Compare two INTEGER_CSTs K1 and K2. */
5924 static int
5925 splay_tree_compare_integer_csts (splay_tree_key k1, splay_tree_key k2)
5927 return tree_int_cst_compare ((tree) k1, (tree) k2);
5930 /* Increase the size indicated in RLI to account for empty classes
5931 that are "off the end" of the class. */
5933 static void
5934 include_empty_classes (record_layout_info rli)
5936 tree eoc;
5937 tree rli_size;
5939 /* It might be the case that we grew the class to allocate a
5940 zero-sized base class. That won't be reflected in RLI, yet,
5941 because we are willing to overlay multiple bases at the same
5942 offset. However, now we need to make sure that RLI is big enough
5943 to reflect the entire class. */
5944 eoc = end_of_class (rli->t, CLASSTYPE_AS_BASE (rli->t) != NULL_TREE);
5945 rli_size = rli_size_unit_so_far (rli);
5946 if (TREE_CODE (rli_size) == INTEGER_CST
5947 && tree_int_cst_lt (rli_size, eoc))
5949 /* The size should have been rounded to a whole byte. */
5950 gcc_assert (tree_int_cst_equal
5951 (rli->bitpos, round_down (rli->bitpos, BITS_PER_UNIT)));
5952 rli->bitpos
5953 = size_binop (PLUS_EXPR,
5954 rli->bitpos,
5955 size_binop (MULT_EXPR,
5956 fold_convert (bitsizetype,
5957 size_binop (MINUS_EXPR,
5958 eoc, rli_size)),
5959 bitsize_int (BITS_PER_UNIT)));
5960 normalize_rli (rli);
5964 /* Calculate the TYPE_SIZE, TYPE_ALIGN, etc for T. Calculate
5965 BINFO_OFFSETs for all of the base-classes. Position the vtable
5966 pointer. Accumulate declared virtual functions on VIRTUALS_P. */
5968 static void
5969 layout_class_type (tree t, tree *virtuals_p)
5971 tree non_static_data_members;
5972 tree field;
5973 tree vptr;
5974 record_layout_info rli;
5975 /* Maps offsets (represented as INTEGER_CSTs) to a TREE_LIST of
5976 types that appear at that offset. */
5977 splay_tree empty_base_offsets;
5978 /* True if the last field laid out was a bit-field. */
5979 bool last_field_was_bitfield = false;
5980 /* The location at which the next field should be inserted. */
5981 tree *next_field;
5983 /* Keep track of the first non-static data member. */
5984 non_static_data_members = TYPE_FIELDS (t);
5986 /* Start laying out the record. */
5987 rli = start_record_layout (t);
5989 /* Mark all the primary bases in the hierarchy. */
5990 determine_primary_bases (t);
5992 /* Create a pointer to our virtual function table. */
5993 vptr = create_vtable_ptr (t, virtuals_p);
5995 /* The vptr is always the first thing in the class. */
5996 if (vptr)
5998 DECL_CHAIN (vptr) = TYPE_FIELDS (t);
5999 TYPE_FIELDS (t) = vptr;
6000 next_field = &DECL_CHAIN (vptr);
6001 place_field (rli, vptr);
6003 else
6004 next_field = &TYPE_FIELDS (t);
6006 /* Build FIELD_DECLs for all of the non-virtual base-types. */
6007 empty_base_offsets = splay_tree_new (splay_tree_compare_integer_csts,
6008 NULL, NULL);
6009 build_base_fields (rli, empty_base_offsets, next_field);
6011 /* Layout the non-static data members. */
6012 for (field = non_static_data_members; field; field = DECL_CHAIN (field))
6014 tree type;
6015 tree padding;
6017 /* We still pass things that aren't non-static data members to
6018 the back end, in case it wants to do something with them. */
6019 if (TREE_CODE (field) != FIELD_DECL)
6021 place_field (rli, field);
6022 /* If the static data member has incomplete type, keep track
6023 of it so that it can be completed later. (The handling
6024 of pending statics in finish_record_layout is
6025 insufficient; consider:
6027 struct S1;
6028 struct S2 { static S1 s1; };
6030 At this point, finish_record_layout will be called, but
6031 S1 is still incomplete.) */
6032 if (VAR_P (field))
6034 maybe_register_incomplete_var (field);
6035 /* The visibility of static data members is determined
6036 at their point of declaration, not their point of
6037 definition. */
6038 determine_visibility (field);
6040 continue;
6043 type = TREE_TYPE (field);
6044 if (type == error_mark_node)
6045 continue;
6047 padding = NULL_TREE;
6049 /* If this field is a bit-field whose width is greater than its
6050 type, then there are some special rules for allocating
6051 it. */
6052 if (DECL_C_BIT_FIELD (field)
6053 && tree_int_cst_lt (TYPE_SIZE (type), DECL_SIZE (field)))
6055 bool was_unnamed_p = false;
6056 /* We must allocate the bits as if suitably aligned for the
6057 longest integer type that fits in this many bits. Then,
6058 we are supposed to use the left over bits as additional
6059 padding. */
6061 /* Do not pick a type bigger than MAX_FIXED_MODE_SIZE. */
6062 tree limit = size_int (MAX_FIXED_MODE_SIZE);
6063 if (tree_int_cst_lt (DECL_SIZE (field), limit))
6064 limit = DECL_SIZE (field);
6066 tree integer_type = integer_types[itk_char];
6067 for (unsigned itk = itk_char; itk != itk_none; itk++)
6068 if (tree next = integer_types[itk])
6070 if (tree_int_cst_lt (limit, TYPE_SIZE (next)))
6071 /* Too big, so our current guess is what we want. */
6072 break;
6073 /* Not bigger than limit, ok */
6074 integer_type = next;
6077 /* Figure out how much additional padding is required. */
6078 if (TREE_CODE (t) == UNION_TYPE)
6079 /* In a union, the padding field must have the full width
6080 of the bit-field; all fields start at offset zero. */
6081 padding = DECL_SIZE (field);
6082 else
6083 padding = size_binop (MINUS_EXPR, DECL_SIZE (field),
6084 TYPE_SIZE (integer_type));
6086 if (integer_zerop (padding))
6087 padding = NULL_TREE;
6089 /* An unnamed bitfield does not normally affect the
6090 alignment of the containing class on a target where
6091 PCC_BITFIELD_TYPE_MATTERS. But, the C++ ABI does not
6092 make any exceptions for unnamed bitfields when the
6093 bitfields are longer than their types. Therefore, we
6094 temporarily give the field a name. */
6095 if (PCC_BITFIELD_TYPE_MATTERS && !DECL_NAME (field))
6097 was_unnamed_p = true;
6098 DECL_NAME (field) = make_anon_name ();
6101 DECL_SIZE (field) = TYPE_SIZE (integer_type);
6102 SET_DECL_ALIGN (field, TYPE_ALIGN (integer_type));
6103 DECL_USER_ALIGN (field) = TYPE_USER_ALIGN (integer_type);
6104 layout_nonempty_base_or_field (rli, field, NULL_TREE,
6105 empty_base_offsets);
6106 if (was_unnamed_p)
6107 DECL_NAME (field) = NULL_TREE;
6108 /* Now that layout has been performed, set the size of the
6109 field to the size of its declared type; the rest of the
6110 field is effectively invisible. */
6111 DECL_SIZE (field) = TYPE_SIZE (type);
6112 /* We must also reset the DECL_MODE of the field. */
6113 SET_DECL_MODE (field, TYPE_MODE (type));
6115 else
6116 layout_nonempty_base_or_field (rli, field, NULL_TREE,
6117 empty_base_offsets);
6119 /* Remember the location of any empty classes in FIELD. */
6120 record_subobject_offsets (TREE_TYPE (field),
6121 byte_position(field),
6122 empty_base_offsets,
6123 /*is_data_member=*/true);
6125 /* If a bit-field does not immediately follow another bit-field,
6126 and yet it starts in the middle of a byte, we have failed to
6127 comply with the ABI. */
6128 if (warn_abi
6129 && DECL_C_BIT_FIELD (field)
6130 /* The TREE_NO_WARNING flag gets set by Objective-C when
6131 laying out an Objective-C class. The ObjC ABI differs
6132 from the C++ ABI, and so we do not want a warning
6133 here. */
6134 && !TREE_NO_WARNING (field)
6135 && !last_field_was_bitfield
6136 && !integer_zerop (size_binop (TRUNC_MOD_EXPR,
6137 DECL_FIELD_BIT_OFFSET (field),
6138 bitsize_unit_node)))
6139 warning_at (DECL_SOURCE_LOCATION (field), OPT_Wabi,
6140 "offset of %qD is not ABI-compliant and may "
6141 "change in a future version of GCC", field);
6143 /* The middle end uses the type of expressions to determine the
6144 possible range of expression values. In order to optimize
6145 "x.i > 7" to "false" for a 2-bit bitfield "i", the middle end
6146 must be made aware of the width of "i", via its type.
6148 Because C++ does not have integer types of arbitrary width,
6149 we must (for the purposes of the front end) convert from the
6150 type assigned here to the declared type of the bitfield
6151 whenever a bitfield expression is used as an rvalue.
6152 Similarly, when assigning a value to a bitfield, the value
6153 must be converted to the type given the bitfield here. */
6154 if (DECL_C_BIT_FIELD (field))
6156 unsigned HOST_WIDE_INT width;
6157 tree ftype = TREE_TYPE (field);
6158 width = tree_to_uhwi (DECL_SIZE (field));
6159 if (width != TYPE_PRECISION (ftype))
6161 TREE_TYPE (field)
6162 = c_build_bitfield_integer_type (width,
6163 TYPE_UNSIGNED (ftype));
6164 TREE_TYPE (field)
6165 = cp_build_qualified_type (TREE_TYPE (field),
6166 cp_type_quals (ftype));
6170 /* If we needed additional padding after this field, add it
6171 now. */
6172 if (padding)
6174 tree padding_field;
6176 padding_field = build_decl (input_location,
6177 FIELD_DECL,
6178 NULL_TREE,
6179 char_type_node);
6180 DECL_BIT_FIELD (padding_field) = 1;
6181 DECL_SIZE (padding_field) = padding;
6182 DECL_CONTEXT (padding_field) = t;
6183 DECL_ARTIFICIAL (padding_field) = 1;
6184 DECL_IGNORED_P (padding_field) = 1;
6185 DECL_PADDING_P (padding_field) = 1;
6186 layout_nonempty_base_or_field (rli, padding_field,
6187 NULL_TREE,
6188 empty_base_offsets);
6191 last_field_was_bitfield = DECL_C_BIT_FIELD (field);
6194 if (!integer_zerop (rli->bitpos))
6196 /* Make sure that we are on a byte boundary so that the size of
6197 the class without virtual bases will always be a round number
6198 of bytes. */
6199 rli->bitpos = round_up_loc (input_location, rli->bitpos, BITS_PER_UNIT);
6200 normalize_rli (rli);
6203 /* Delete all zero-width bit-fields from the list of fields. Now
6204 that the type is laid out they are no longer important. */
6205 remove_zero_width_bit_fields (t);
6207 if (CLASSTYPE_NON_LAYOUT_POD_P (t) || CLASSTYPE_EMPTY_P (t))
6209 /* T needs a different layout as a base (eliding virtual bases
6210 or whatever). Create that version. */
6211 tree base_t = make_node (TREE_CODE (t));
6213 /* If the ABI version is not at least two, and the last
6214 field was a bit-field, RLI may not be on a byte
6215 boundary. In particular, rli_size_unit_so_far might
6216 indicate the last complete byte, while rli_size_so_far
6217 indicates the total number of bits used. Therefore,
6218 rli_size_so_far, rather than rli_size_unit_so_far, is
6219 used to compute TYPE_SIZE_UNIT. */
6220 tree eoc = end_of_class (t, /*include_virtuals_p=*/0);
6221 TYPE_SIZE_UNIT (base_t)
6222 = size_binop (MAX_EXPR,
6223 fold_convert (sizetype,
6224 size_binop (CEIL_DIV_EXPR,
6225 rli_size_so_far (rli),
6226 bitsize_int (BITS_PER_UNIT))),
6227 eoc);
6228 TYPE_SIZE (base_t)
6229 = size_binop (MAX_EXPR,
6230 rli_size_so_far (rli),
6231 size_binop (MULT_EXPR,
6232 fold_convert (bitsizetype, eoc),
6233 bitsize_int (BITS_PER_UNIT)));
6234 SET_TYPE_ALIGN (base_t, rli->record_align);
6235 TYPE_USER_ALIGN (base_t) = TYPE_USER_ALIGN (t);
6237 /* Copy the non-static data members of T. This will include its
6238 direct non-virtual bases & vtable. */
6239 next_field = &TYPE_FIELDS (base_t);
6240 for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
6241 if (TREE_CODE (field) == FIELD_DECL)
6243 *next_field = copy_node (field);
6244 DECL_CONTEXT (*next_field) = base_t;
6245 next_field = &DECL_CHAIN (*next_field);
6247 *next_field = NULL_TREE;
6249 /* We use the base type for trivial assignments, and hence it
6250 needs a mode. */
6251 compute_record_mode (base_t);
6253 TYPE_CONTEXT (base_t) = t;
6255 /* Record the base version of the type. */
6256 CLASSTYPE_AS_BASE (t) = base_t;
6258 else
6259 CLASSTYPE_AS_BASE (t) = t;
6261 /* Every empty class contains an empty class. */
6262 if (CLASSTYPE_EMPTY_P (t))
6263 CLASSTYPE_CONTAINS_EMPTY_CLASS_P (t) = 1;
6265 /* Set the TYPE_DECL for this type to contain the right
6266 value for DECL_OFFSET, so that we can use it as part
6267 of a COMPONENT_REF for multiple inheritance. */
6268 layout_decl (TYPE_MAIN_DECL (t), 0);
6270 /* Now fix up any virtual base class types that we left lying
6271 around. We must get these done before we try to lay out the
6272 virtual function table. As a side-effect, this will remove the
6273 base subobject fields. */
6274 layout_virtual_bases (rli, empty_base_offsets);
6276 /* Make sure that empty classes are reflected in RLI at this
6277 point. */
6278 include_empty_classes (rli);
6280 /* Make sure not to create any structures with zero size. */
6281 if (integer_zerop (rli_size_unit_so_far (rli)) && CLASSTYPE_EMPTY_P (t))
6282 place_field (rli,
6283 build_decl (input_location,
6284 FIELD_DECL, NULL_TREE, char_type_node));
6286 /* If this is a non-POD, declaring it packed makes a difference to how it
6287 can be used as a field; don't let finalize_record_size undo it. */
6288 if (TYPE_PACKED (t) && !layout_pod_type_p (t))
6289 rli->packed_maybe_necessary = true;
6291 /* Let the back end lay out the type. */
6292 finish_record_layout (rli, /*free_p=*/true);
6294 if (TYPE_SIZE_UNIT (t)
6295 && TREE_CODE (TYPE_SIZE_UNIT (t)) == INTEGER_CST
6296 && !TREE_OVERFLOW (TYPE_SIZE_UNIT (t))
6297 && !valid_constant_size_p (TYPE_SIZE_UNIT (t)))
6298 error ("size of type %qT is too large (%qE bytes)", t, TYPE_SIZE_UNIT (t));
6300 /* Warn about bases that can't be talked about due to ambiguity. */
6301 warn_about_ambiguous_bases (t);
6303 /* Now that we're done with layout, give the base fields the real types. */
6304 for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
6305 if (DECL_ARTIFICIAL (field) && IS_FAKE_BASE_TYPE (TREE_TYPE (field)))
6306 TREE_TYPE (field) = TYPE_CONTEXT (TREE_TYPE (field));
6308 /* Clean up. */
6309 splay_tree_delete (empty_base_offsets);
6311 if (CLASSTYPE_EMPTY_P (t)
6312 && tree_int_cst_lt (sizeof_biggest_empty_class,
6313 TYPE_SIZE_UNIT (t)))
6314 sizeof_biggest_empty_class = TYPE_SIZE_UNIT (t);
6317 /* Determine the "key method" for the class type indicated by TYPE,
6318 and set CLASSTYPE_KEY_METHOD accordingly. */
6320 void
6321 determine_key_method (tree type)
6323 tree method;
6325 if (processing_template_decl
6326 || CLASSTYPE_TEMPLATE_INSTANTIATION (type)
6327 || CLASSTYPE_INTERFACE_KNOWN (type))
6328 return;
6330 /* The key method is the first non-pure virtual function that is not
6331 inline at the point of class definition. On some targets the
6332 key function may not be inline; those targets should not call
6333 this function until the end of the translation unit. */
6334 for (method = TYPE_FIELDS (type); method; method = DECL_CHAIN (method))
6335 if (TREE_CODE (method) == FUNCTION_DECL
6336 && DECL_VINDEX (method) != NULL_TREE
6337 && ! DECL_DECLARED_INLINE_P (method)
6338 && ! DECL_PURE_VIRTUAL_P (method))
6340 CLASSTYPE_KEY_METHOD (type) = method;
6341 break;
6344 return;
6347 /* Helper of find_flexarrays. Return true when FLD refers to a non-static
6348 class data member of non-zero size, otherwise false. */
6350 static inline bool
6351 field_nonempty_p (const_tree fld)
6353 if (TREE_CODE (fld) == ERROR_MARK)
6354 return false;
6356 tree type = TREE_TYPE (fld);
6357 if (TREE_CODE (fld) == FIELD_DECL
6358 && TREE_CODE (type) != ERROR_MARK
6359 && (DECL_NAME (fld) || RECORD_OR_UNION_TYPE_P (type)))
6361 return TYPE_SIZE (type)
6362 && (TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST
6363 || !tree_int_cst_equal (size_zero_node, TYPE_SIZE (type)));
6366 return false;
6369 /* Used by find_flexarrays and related functions. */
6371 struct flexmems_t
6373 /* The first flexible array member or non-zero array member found
6374 in the order of layout. */
6375 tree array;
6376 /* First non-static non-empty data member in the class or its bases. */
6377 tree first;
6378 /* The first non-static non-empty data member following either
6379 the flexible array member, if found, or the zero-length array member
6380 otherwise. AFTER[1] refers to the first such data member of a union
6381 of which the struct containing the flexible array member or zero-length
6382 array is a member, or NULL when no such union exists. This element is
6383 only used during searching, not for diagnosing problems. AFTER[0]
6384 refers to the first such data member that is not a member of such
6385 a union. */
6386 tree after[2];
6388 /* Refers to a struct (not union) in which the struct of which the flexible
6389 array is member is defined. Used to diagnose strictly (according to C)
6390 invalid uses of the latter structs. */
6391 tree enclosing;
6394 /* Find either the first flexible array member or the first zero-length
6395 array, in that order of preference, among members of class T (but not
6396 its base classes), and set members of FMEM accordingly.
6397 BASE_P is true if T is a base class of another class.
6398 PUN is set to the outermost union in which the flexible array member
6399 (or zero-length array) is defined if one such union exists, otherwise
6400 to NULL.
6401 Similarly, PSTR is set to a data member of the outermost struct of
6402 which the flexible array is a member if one such struct exists,
6403 otherwise to NULL. */
6405 static void
6406 find_flexarrays (tree t, flexmems_t *fmem, bool base_p,
6407 tree pun /* = NULL_TREE */,
6408 tree pstr /* = NULL_TREE */)
6410 /* Set the "pointer" to the outermost enclosing union if not set
6411 yet and maintain it for the remainder of the recursion. */
6412 if (!pun && TREE_CODE (t) == UNION_TYPE)
6413 pun = t;
6415 for (tree fld = TYPE_FIELDS (t); fld; fld = DECL_CHAIN (fld))
6417 if (fld == error_mark_node)
6418 return;
6420 /* Is FLD a typedef for an anonymous struct? */
6422 /* FIXME: Note that typedefs (as well as arrays) need to be fully
6423 handled elsewhere so that errors like the following are detected
6424 as well:
6425 typedef struct { int i, a[], j; } S; // bug c++/72753
6426 S s [2]; // bug c++/68489
6428 if (TREE_CODE (fld) == TYPE_DECL
6429 && DECL_IMPLICIT_TYPEDEF_P (fld)
6430 && CLASS_TYPE_P (TREE_TYPE (fld))
6431 && anon_aggrname_p (DECL_NAME (fld)))
6433 /* Check the nested unnamed type referenced via a typedef
6434 independently of FMEM (since it's not a data member of
6435 the enclosing class). */
6436 check_flexarrays (TREE_TYPE (fld));
6437 continue;
6440 /* Skip anything that's GCC-generated or not a (non-static) data
6441 member. */
6442 if (DECL_ARTIFICIAL (fld) || TREE_CODE (fld) != FIELD_DECL)
6443 continue;
6445 /* Type of the member. */
6446 tree fldtype = TREE_TYPE (fld);
6447 if (fldtype == error_mark_node)
6448 return;
6450 /* Determine the type of the array element or object referenced
6451 by the member so that it can be checked for flexible array
6452 members if it hasn't been yet. */
6453 tree eltype = fldtype;
6454 while (TREE_CODE (eltype) == ARRAY_TYPE
6455 || TREE_CODE (eltype) == POINTER_TYPE
6456 || TREE_CODE (eltype) == REFERENCE_TYPE)
6457 eltype = TREE_TYPE (eltype);
6459 if (RECORD_OR_UNION_TYPE_P (eltype))
6461 if (fmem->array && !fmem->after[bool (pun)])
6463 /* Once the member after the flexible array has been found
6464 we're done. */
6465 fmem->after[bool (pun)] = fld;
6466 break;
6469 if (eltype == fldtype || TYPE_UNNAMED_P (eltype))
6471 /* Descend into the non-static member struct or union and try
6472 to find a flexible array member or zero-length array among
6473 its members. This is only necessary for anonymous types
6474 and types in whose context the current type T has not been
6475 defined (the latter must not be checked again because they
6476 are already in the process of being checked by one of the
6477 recursive calls). */
6479 tree first = fmem->first;
6480 tree array = fmem->array;
6482 /* If this member isn't anonymous and a prior non-flexible array
6483 member has been seen in one of the enclosing structs, clear
6484 the FIRST member since it doesn't contribute to the flexible
6485 array struct's members. */
6486 if (first && !array && !ANON_AGGR_TYPE_P (eltype))
6487 fmem->first = NULL_TREE;
6489 find_flexarrays (eltype, fmem, false, pun,
6490 !pstr && TREE_CODE (t) == RECORD_TYPE ? fld : pstr);
6492 if (fmem->array != array)
6493 continue;
6495 if (first && !array && !ANON_AGGR_TYPE_P (eltype))
6497 /* Restore the FIRST member reset above if no flexible
6498 array member has been found in this member's struct. */
6499 fmem->first = first;
6502 /* If the member struct contains the first flexible array
6503 member, or if this member is a base class, continue to
6504 the next member and avoid setting the FMEM->NEXT pointer
6505 to point to it. */
6506 if (base_p)
6507 continue;
6511 if (field_nonempty_p (fld))
6513 /* Remember the first non-static data member. */
6514 if (!fmem->first)
6515 fmem->first = fld;
6517 /* Remember the first non-static data member after the flexible
6518 array member, if one has been found, or the zero-length array
6519 if it has been found. */
6520 if (fmem->array && !fmem->after[bool (pun)])
6521 fmem->after[bool (pun)] = fld;
6524 /* Skip non-arrays. */
6525 if (TREE_CODE (fldtype) != ARRAY_TYPE)
6526 continue;
6528 /* Determine the upper bound of the array if it has one. */
6529 if (TYPE_DOMAIN (fldtype))
6531 if (fmem->array)
6533 /* Make a record of the zero-length array if either one
6534 such field or a flexible array member has been seen to
6535 handle the pathological and unlikely case of multiple
6536 such members. */
6537 if (!fmem->after[bool (pun)])
6538 fmem->after[bool (pun)] = fld;
6540 else if (integer_all_onesp (TYPE_MAX_VALUE (TYPE_DOMAIN (fldtype))))
6542 /* Remember the first zero-length array unless a flexible array
6543 member has already been seen. */
6544 fmem->array = fld;
6545 fmem->enclosing = pstr;
6548 else
6550 /* Flexible array members have no upper bound. */
6551 if (fmem->array)
6553 if (TYPE_DOMAIN (TREE_TYPE (fmem->array)))
6555 /* Replace the zero-length array if it's been stored and
6556 reset the after pointer. */
6557 fmem->after[bool (pun)] = NULL_TREE;
6558 fmem->array = fld;
6559 fmem->enclosing = pstr;
6561 else if (!fmem->after[bool (pun)])
6562 /* Make a record of another flexible array member. */
6563 fmem->after[bool (pun)] = fld;
6565 else
6567 fmem->array = fld;
6568 fmem->enclosing = pstr;
6574 /* Diagnose a strictly (by the C standard) invalid use of a struct with
6575 a flexible array member (or the zero-length array extension). */
6577 static void
6578 diagnose_invalid_flexarray (const flexmems_t *fmem)
6580 if (fmem->array && fmem->enclosing
6581 && pedwarn (location_of (fmem->enclosing), OPT_Wpedantic,
6582 TYPE_DOMAIN (TREE_TYPE (fmem->array))
6583 ? G_("invalid use of %q#T with a zero-size array "
6584 "in %q#D")
6585 : G_("invalid use of %q#T with a flexible array member "
6586 "in %q#T"),
6587 DECL_CONTEXT (fmem->array),
6588 DECL_CONTEXT (fmem->enclosing)))
6589 inform (DECL_SOURCE_LOCATION (fmem->array),
6590 "array member %q#D declared here", fmem->array);
6593 /* Issue diagnostics for invalid flexible array members or zero-length
6594 arrays that are not the last elements of the containing class or its
6595 base classes or that are its sole members. */
6597 static void
6598 diagnose_flexarrays (tree t, const flexmems_t *fmem)
6600 if (!fmem->array)
6601 return;
6603 if (fmem->first && !fmem->after[0])
6605 diagnose_invalid_flexarray (fmem);
6606 return;
6609 /* Has a diagnostic been issued? */
6610 bool diagd = false;
6612 const char *msg = 0;
6614 if (TYPE_DOMAIN (TREE_TYPE (fmem->array)))
6616 if (fmem->after[0])
6617 msg = G_("zero-size array member %qD not at end of %q#T");
6618 else if (!fmem->first)
6619 msg = G_("zero-size array member %qD in an otherwise empty %q#T");
6621 if (msg)
6623 location_t loc = DECL_SOURCE_LOCATION (fmem->array);
6625 if (pedwarn (loc, OPT_Wpedantic, msg, fmem->array, t))
6627 inform (location_of (t), "in the definition of %q#T", t);
6628 diagd = true;
6632 else
6634 if (fmem->after[0])
6635 msg = G_("flexible array member %qD not at end of %q#T");
6636 else if (!fmem->first)
6637 msg = G_("flexible array member %qD in an otherwise empty %q#T");
6639 if (msg)
6641 location_t loc = DECL_SOURCE_LOCATION (fmem->array);
6642 diagd = true;
6644 error_at (loc, msg, fmem->array, t);
6646 /* In the unlikely event that the member following the flexible
6647 array member is declared in a different class, or the member
6648 overlaps another member of a common union, point to it.
6649 Otherwise it should be obvious. */
6650 if (fmem->after[0]
6651 && ((DECL_CONTEXT (fmem->after[0])
6652 != DECL_CONTEXT (fmem->array))))
6654 inform (DECL_SOURCE_LOCATION (fmem->after[0]),
6655 "next member %q#D declared here",
6656 fmem->after[0]);
6657 inform (location_of (t), "in the definition of %q#T", t);
6662 if (!diagd && fmem->array && fmem->enclosing)
6663 diagnose_invalid_flexarray (fmem);
6667 /* Recursively check to make sure that any flexible array or zero-length
6668 array members of class T or its bases are valid (i.e., not the sole
6669 non-static data member of T and, if one exists, that it is the last
6670 non-static data member of T and its base classes. FMEM is expected
6671 to be initially null and is used internally by recursive calls to
6672 the function. Issue the appropriate diagnostics for the array member
6673 that fails the checks. */
6675 static void
6676 check_flexarrays (tree t, flexmems_t *fmem /* = NULL */,
6677 bool base_p /* = false */)
6679 /* Initialize the result of a search for flexible array and zero-length
6680 array members. Avoid doing any work if the most interesting FMEM data
6681 have already been populated. */
6682 flexmems_t flexmems = flexmems_t ();
6683 if (!fmem)
6684 fmem = &flexmems;
6685 else if (fmem->array && fmem->first && fmem->after[0])
6686 return;
6688 tree fam = fmem->array;
6690 /* Recursively check the primary base class first. */
6691 if (CLASSTYPE_HAS_PRIMARY_BASE_P (t))
6693 tree basetype = BINFO_TYPE (CLASSTYPE_PRIMARY_BINFO (t));
6694 check_flexarrays (basetype, fmem, true);
6697 /* Recursively check the base classes. */
6698 int nbases = TYPE_BINFO (t) ? BINFO_N_BASE_BINFOS (TYPE_BINFO (t)) : 0;
6699 for (int i = 0; i < nbases; ++i)
6701 tree base_binfo = BINFO_BASE_BINFO (TYPE_BINFO (t), i);
6703 /* The primary base class was already checked above. */
6704 if (base_binfo == CLASSTYPE_PRIMARY_BINFO (t))
6705 continue;
6707 /* Virtual base classes are at the end. */
6708 if (BINFO_VIRTUAL_P (base_binfo))
6709 continue;
6711 /* Check the base class. */
6712 check_flexarrays (BINFO_TYPE (base_binfo), fmem, /*base_p=*/true);
6715 if (fmem == &flexmems)
6717 /* Check virtual base classes only once per derived class.
6718 I.e., this check is not performed recursively for base
6719 classes. */
6720 int i;
6721 tree base_binfo;
6722 vec<tree, va_gc> *vbases;
6723 for (vbases = CLASSTYPE_VBASECLASSES (t), i = 0;
6724 vec_safe_iterate (vbases, i, &base_binfo); i++)
6726 /* Check the virtual base class. */
6727 tree basetype = TREE_TYPE (base_binfo);
6729 check_flexarrays (basetype, fmem, /*base_p=*/true);
6733 /* Is the type unnamed (and therefore a member of it potentially
6734 an anonymous struct or union)? */
6735 bool maybe_anon_p = TYPE_UNNAMED_P (t);
6737 /* Search the members of the current (possibly derived) class, skipping
6738 unnamed structs and unions since those could be anonymous. */
6739 if (fmem != &flexmems || !maybe_anon_p)
6740 find_flexarrays (t, fmem, base_p || fam != fmem->array);
6742 if (fmem == &flexmems && !maybe_anon_p)
6744 /* Issue diagnostics for invalid flexible and zero-length array
6745 members found in base classes or among the members of the current
6746 class. Ignore anonymous structs and unions whose members are
6747 considered to be members of the enclosing class and thus will
6748 be diagnosed when checking it. */
6749 diagnose_flexarrays (t, fmem);
6753 /* Perform processing required when the definition of T (a class type)
6754 is complete. Diagnose invalid definitions of flexible array members
6755 and zero-size arrays. */
6757 void
6758 finish_struct_1 (tree t)
6760 tree x;
6761 /* A TREE_LIST. The TREE_VALUE of each node is a FUNCTION_DECL. */
6762 tree virtuals = NULL_TREE;
6764 if (COMPLETE_TYPE_P (t))
6766 gcc_assert (MAYBE_CLASS_TYPE_P (t));
6767 error ("redefinition of %q#T", t);
6768 popclass ();
6769 return;
6772 /* If this type was previously laid out as a forward reference,
6773 make sure we lay it out again. */
6774 TYPE_SIZE (t) = NULL_TREE;
6775 CLASSTYPE_PRIMARY_BINFO (t) = NULL_TREE;
6777 /* Make assumptions about the class; we'll reset the flags if
6778 necessary. */
6779 CLASSTYPE_EMPTY_P (t) = 1;
6780 CLASSTYPE_NEARLY_EMPTY_P (t) = 1;
6781 CLASSTYPE_CONTAINS_EMPTY_CLASS_P (t) = 0;
6782 CLASSTYPE_LITERAL_P (t) = true;
6784 /* Do end-of-class semantic processing: checking the validity of the
6785 bases and members and add implicitly generated methods. */
6786 check_bases_and_members (t);
6788 /* Find the key method. */
6789 if (TYPE_CONTAINS_VPTR_P (t))
6791 /* The Itanium C++ ABI permits the key method to be chosen when
6792 the class is defined -- even though the key method so
6793 selected may later turn out to be an inline function. On
6794 some systems (such as ARM Symbian OS) the key method cannot
6795 be determined until the end of the translation unit. On such
6796 systems, we leave CLASSTYPE_KEY_METHOD set to NULL, which
6797 will cause the class to be added to KEYED_CLASSES. Then, in
6798 finish_file we will determine the key method. */
6799 if (targetm.cxx.key_method_may_be_inline ())
6800 determine_key_method (t);
6802 /* If a polymorphic class has no key method, we may emit the vtable
6803 in every translation unit where the class definition appears. If
6804 we're devirtualizing, we can look into the vtable even if we
6805 aren't emitting it. */
6806 if (!CLASSTYPE_KEY_METHOD (t))
6807 vec_safe_push (keyed_classes, t);
6810 /* Layout the class itself. */
6811 layout_class_type (t, &virtuals);
6812 /* COMPLETE_TYPE_P is now true. */
6814 set_class_bindings (t);
6816 /* With the layout complete, check for flexible array members and
6817 zero-length arrays that might overlap other members in the final
6818 layout. */
6819 check_flexarrays (t);
6821 virtuals = modify_all_vtables (t, nreverse (virtuals));
6823 /* If necessary, create the primary vtable for this class. */
6824 if (virtuals || TYPE_CONTAINS_VPTR_P (t))
6826 /* We must enter these virtuals into the table. */
6827 if (!CLASSTYPE_HAS_PRIMARY_BASE_P (t))
6828 build_primary_vtable (NULL_TREE, t);
6829 else if (! BINFO_NEW_VTABLE_MARKED (TYPE_BINFO (t)))
6830 /* Here we know enough to change the type of our virtual
6831 function table, but we will wait until later this function. */
6832 build_primary_vtable (CLASSTYPE_PRIMARY_BINFO (t), t);
6834 /* If we're warning about ABI tags, check the types of the new
6835 virtual functions. */
6836 if (warn_abi_tag)
6837 for (tree v = virtuals; v; v = TREE_CHAIN (v))
6838 check_abi_tags (t, TREE_VALUE (v));
6841 if (TYPE_CONTAINS_VPTR_P (t))
6843 int vindex;
6844 tree fn;
6846 if (BINFO_VTABLE (TYPE_BINFO (t)))
6847 gcc_assert (DECL_VIRTUAL_P (BINFO_VTABLE (TYPE_BINFO (t))));
6848 if (!CLASSTYPE_HAS_PRIMARY_BASE_P (t))
6849 gcc_assert (BINFO_VIRTUALS (TYPE_BINFO (t)) == NULL_TREE);
6851 /* Add entries for virtual functions introduced by this class. */
6852 BINFO_VIRTUALS (TYPE_BINFO (t))
6853 = chainon (BINFO_VIRTUALS (TYPE_BINFO (t)), virtuals);
6855 /* Set DECL_VINDEX for all functions declared in this class. */
6856 for (vindex = 0, fn = BINFO_VIRTUALS (TYPE_BINFO (t));
6858 fn = TREE_CHAIN (fn),
6859 vindex += (TARGET_VTABLE_USES_DESCRIPTORS
6860 ? TARGET_VTABLE_USES_DESCRIPTORS : 1))
6862 tree fndecl = BV_FN (fn);
6864 if (DECL_THUNK_P (fndecl))
6865 /* A thunk. We should never be calling this entry directly
6866 from this vtable -- we'd use the entry for the non
6867 thunk base function. */
6868 DECL_VINDEX (fndecl) = NULL_TREE;
6869 else if (TREE_CODE (DECL_VINDEX (fndecl)) != INTEGER_CST)
6870 DECL_VINDEX (fndecl) = build_int_cst (NULL_TREE, vindex);
6874 finish_struct_bits (t);
6876 set_method_tm_attributes (t);
6877 if (flag_openmp || flag_openmp_simd)
6878 finish_omp_declare_simd_methods (t);
6880 /* Clear DECL_IN_AGGR_P for all member functions. Complete the rtl
6881 for any static member objects of the type we're working on. */
6882 for (x = TYPE_FIELDS (t); x; x = DECL_CHAIN (x))
6883 if (DECL_DECLARES_FUNCTION_P (x))
6884 DECL_IN_AGGR_P (x) = false;
6885 else if (VAR_P (x) && TREE_STATIC (x)
6886 && TREE_TYPE (x) != error_mark_node
6887 && same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (x)), t))
6888 SET_DECL_MODE (x, TYPE_MODE (t));
6890 /* Complain if one of the field types requires lower visibility. */
6891 constrain_class_visibility (t);
6893 /* Make the rtl for any new vtables we have created, and unmark
6894 the base types we marked. */
6895 finish_vtbls (t);
6897 /* Build the VTT for T. */
6898 build_vtt (t);
6900 if (warn_nonvdtor
6901 && TYPE_POLYMORPHIC_P (t) && accessible_nvdtor_p (t)
6902 && !CLASSTYPE_FINAL (t))
6903 warning (OPT_Wnon_virtual_dtor,
6904 "%q#T has virtual functions and accessible"
6905 " non-virtual destructor", t);
6907 complete_vars (t);
6909 if (warn_overloaded_virtual)
6910 warn_hidden (t);
6912 /* Class layout, assignment of virtual table slots, etc., is now
6913 complete. Give the back end a chance to tweak the visibility of
6914 the class or perform any other required target modifications. */
6915 targetm.cxx.adjust_class_at_definition (t);
6917 maybe_suppress_debug_info (t);
6919 if (flag_vtable_verify)
6920 vtv_save_class_info (t);
6922 dump_class_hierarchy (t);
6924 /* Finish debugging output for this type. */
6925 rest_of_type_compilation (t, ! LOCAL_CLASS_P (t));
6927 if (TYPE_TRANSPARENT_AGGR (t))
6929 tree field = first_field (t);
6930 if (field == NULL_TREE || error_operand_p (field))
6932 error ("type transparent %q#T does not have any fields", t);
6933 TYPE_TRANSPARENT_AGGR (t) = 0;
6935 else if (DECL_ARTIFICIAL (field))
6937 if (DECL_FIELD_IS_BASE (field))
6938 error ("type transparent class %qT has base classes", t);
6939 else
6941 gcc_checking_assert (DECL_VIRTUAL_P (field));
6942 error ("type transparent class %qT has virtual functions", t);
6944 TYPE_TRANSPARENT_AGGR (t) = 0;
6946 else if (TYPE_MODE (t) != DECL_MODE (field))
6948 error ("type transparent %q#T cannot be made transparent because "
6949 "the type of the first field has a different ABI from the "
6950 "class overall", t);
6951 TYPE_TRANSPARENT_AGGR (t) = 0;
6956 /* When T was built up, the member declarations were added in reverse
6957 order. Rearrange them to declaration order. */
6959 void
6960 unreverse_member_declarations (tree t)
6962 tree next;
6963 tree prev;
6964 tree x;
6966 /* The following lists are all in reverse order. Put them in
6967 declaration order now. */
6968 CLASSTYPE_DECL_LIST (t) = nreverse (CLASSTYPE_DECL_LIST (t));
6970 /* For the TYPE_FIELDS, only the non TYPE_DECLs are in reverse
6971 order, so we can't just use nreverse. Due to stat_hack
6972 chicanery in finish_member_declaration. */
6973 prev = NULL_TREE;
6974 for (x = TYPE_FIELDS (t);
6975 x && TREE_CODE (x) != TYPE_DECL;
6976 x = next)
6978 next = DECL_CHAIN (x);
6979 DECL_CHAIN (x) = prev;
6980 prev = x;
6983 if (prev)
6985 DECL_CHAIN (TYPE_FIELDS (t)) = x;
6986 TYPE_FIELDS (t) = prev;
6990 tree
6991 finish_struct (tree t, tree attributes)
6993 location_t saved_loc = input_location;
6995 /* Now that we've got all the field declarations, reverse everything
6996 as necessary. */
6997 unreverse_member_declarations (t);
6999 cplus_decl_attributes (&t, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
7000 fixup_attribute_variants (t);
7002 /* Nadger the current location so that diagnostics point to the start of
7003 the struct, not the end. */
7004 input_location = DECL_SOURCE_LOCATION (TYPE_NAME (t));
7006 if (processing_template_decl)
7008 tree x;
7010 /* We need to add the target functions of USING_DECLS, so that
7011 they can be found when the using declaration is not
7012 instantiated yet. */
7013 for (x = TYPE_FIELDS (t); x; x = DECL_CHAIN (x))
7014 if (TREE_CODE (x) == USING_DECL)
7016 tree fn = strip_using_decl (x);
7017 if (OVL_P (fn))
7018 for (lkp_iterator iter (fn); iter; ++iter)
7019 add_method (t, *iter, true);
7021 else if (DECL_DECLARES_FUNCTION_P (x))
7022 DECL_IN_AGGR_P (x) = false;
7024 TYPE_SIZE (t) = bitsize_zero_node;
7025 TYPE_SIZE_UNIT (t) = size_zero_node;
7026 /* COMPLETE_TYPE_P is now true. */
7028 set_class_bindings (t);
7030 /* We need to emit an error message if this type was used as a parameter
7031 and it is an abstract type, even if it is a template. We construct
7032 a simple CLASSTYPE_PURE_VIRTUALS list without taking bases into
7033 account and we call complete_vars with this type, which will check
7034 the PARM_DECLS. Note that while the type is being defined,
7035 CLASSTYPE_PURE_VIRTUALS contains the list of the inline friends
7036 (see CLASSTYPE_INLINE_FRIENDS) so we need to clear it. */
7037 CLASSTYPE_PURE_VIRTUALS (t) = NULL;
7038 for (x = TYPE_FIELDS (t); x; x = DECL_CHAIN (x))
7039 if (TREE_CODE (x) == FUNCTION_DECL && DECL_PURE_VIRTUAL_P (x))
7040 vec_safe_push (CLASSTYPE_PURE_VIRTUALS (t), x);
7041 complete_vars (t);
7043 /* Remember current #pragma pack value. */
7044 TYPE_PRECISION (t) = maximum_field_alignment;
7046 /* Fix up any variants we've already built. */
7047 for (x = TYPE_NEXT_VARIANT (t); x; x = TYPE_NEXT_VARIANT (x))
7049 TYPE_SIZE (x) = TYPE_SIZE (t);
7050 TYPE_SIZE_UNIT (x) = TYPE_SIZE_UNIT (t);
7051 TYPE_FIELDS (x) = TYPE_FIELDS (t);
7054 else
7055 finish_struct_1 (t);
7056 /* COMPLETE_TYPE_P is now true. */
7058 maybe_warn_about_overly_private_class (t);
7060 if (is_std_init_list (t))
7062 /* People keep complaining that the compiler crashes on an invalid
7063 definition of initializer_list, so I guess we should explicitly
7064 reject it. What the compiler internals care about is that it's a
7065 template and has a pointer field followed by size_type field. */
7066 bool ok = false;
7067 if (processing_template_decl)
7069 tree f = next_initializable_field (TYPE_FIELDS (t));
7070 if (f && TREE_CODE (TREE_TYPE (f)) == POINTER_TYPE)
7072 f = next_initializable_field (DECL_CHAIN (f));
7073 if (f && same_type_p (TREE_TYPE (f), size_type_node))
7074 ok = true;
7077 if (!ok)
7078 fatal_error (input_location, "definition of %qD does not match "
7079 "%<#include <initializer_list>%>", TYPE_NAME (t));
7082 input_location = saved_loc;
7084 TYPE_BEING_DEFINED (t) = 0;
7086 if (current_class_type)
7087 popclass ();
7088 else
7089 error ("trying to finish struct, but kicked out due to previous parse errors");
7091 if (processing_template_decl && at_function_scope_p ()
7092 /* Lambdas are defined by the LAMBDA_EXPR. */
7093 && !LAMBDA_TYPE_P (t))
7094 add_stmt (build_min (TAG_DEFN, t));
7096 return t;
7099 /* Hash table to avoid endless recursion when handling references. */
7100 static hash_table<nofree_ptr_hash<tree_node> > *fixed_type_or_null_ref_ht;
7102 /* Return the dynamic type of INSTANCE, if known.
7103 Used to determine whether the virtual function table is needed
7104 or not.
7106 *NONNULL is set iff INSTANCE can be known to be nonnull, regardless
7107 of our knowledge of its type. *NONNULL should be initialized
7108 before this function is called. */
7110 static tree
7111 fixed_type_or_null (tree instance, int *nonnull, int *cdtorp)
7113 #define RECUR(T) fixed_type_or_null((T), nonnull, cdtorp)
7115 switch (TREE_CODE (instance))
7117 case INDIRECT_REF:
7118 if (POINTER_TYPE_P (TREE_TYPE (instance)))
7119 return NULL_TREE;
7120 else
7121 return RECUR (TREE_OPERAND (instance, 0));
7123 case CALL_EXPR:
7124 /* This is a call to a constructor, hence it's never zero. */
7125 if (TREE_HAS_CONSTRUCTOR (instance))
7127 if (nonnull)
7128 *nonnull = 1;
7129 return TREE_TYPE (instance);
7131 return NULL_TREE;
7133 case SAVE_EXPR:
7134 /* This is a call to a constructor, hence it's never zero. */
7135 if (TREE_HAS_CONSTRUCTOR (instance))
7137 if (nonnull)
7138 *nonnull = 1;
7139 return TREE_TYPE (instance);
7141 return RECUR (TREE_OPERAND (instance, 0));
7143 case POINTER_PLUS_EXPR:
7144 case PLUS_EXPR:
7145 case MINUS_EXPR:
7146 if (TREE_CODE (TREE_OPERAND (instance, 0)) == ADDR_EXPR)
7147 return RECUR (TREE_OPERAND (instance, 0));
7148 if (TREE_CODE (TREE_OPERAND (instance, 1)) == INTEGER_CST)
7149 /* Propagate nonnull. */
7150 return RECUR (TREE_OPERAND (instance, 0));
7152 return NULL_TREE;
7154 CASE_CONVERT:
7155 return RECUR (TREE_OPERAND (instance, 0));
7157 case ADDR_EXPR:
7158 instance = TREE_OPERAND (instance, 0);
7159 if (nonnull)
7161 /* Just because we see an ADDR_EXPR doesn't mean we're dealing
7162 with a real object -- given &p->f, p can still be null. */
7163 tree t = get_base_address (instance);
7164 /* ??? Probably should check DECL_WEAK here. */
7165 if (t && DECL_P (t))
7166 *nonnull = 1;
7168 return RECUR (instance);
7170 case COMPONENT_REF:
7171 /* If this component is really a base class reference, then the field
7172 itself isn't definitive. */
7173 if (DECL_FIELD_IS_BASE (TREE_OPERAND (instance, 1)))
7174 return RECUR (TREE_OPERAND (instance, 0));
7175 return RECUR (TREE_OPERAND (instance, 1));
7177 case VAR_DECL:
7178 case FIELD_DECL:
7179 if (TREE_CODE (TREE_TYPE (instance)) == ARRAY_TYPE
7180 && MAYBE_CLASS_TYPE_P (TREE_TYPE (TREE_TYPE (instance))))
7182 if (nonnull)
7183 *nonnull = 1;
7184 return TREE_TYPE (TREE_TYPE (instance));
7186 /* fall through. */
7187 case TARGET_EXPR:
7188 case PARM_DECL:
7189 case RESULT_DECL:
7190 if (MAYBE_CLASS_TYPE_P (TREE_TYPE (instance)))
7192 if (nonnull)
7193 *nonnull = 1;
7194 return TREE_TYPE (instance);
7196 else if (instance == current_class_ptr)
7198 if (nonnull)
7199 *nonnull = 1;
7201 /* if we're in a ctor or dtor, we know our type. If
7202 current_class_ptr is set but we aren't in a function, we're in
7203 an NSDMI (and therefore a constructor). */
7204 if (current_scope () != current_function_decl
7205 || (DECL_LANG_SPECIFIC (current_function_decl)
7206 && (DECL_CONSTRUCTOR_P (current_function_decl)
7207 || DECL_DESTRUCTOR_P (current_function_decl))))
7209 if (cdtorp)
7210 *cdtorp = 1;
7211 return TREE_TYPE (TREE_TYPE (instance));
7214 else if (TREE_CODE (TREE_TYPE (instance)) == REFERENCE_TYPE)
7216 /* We only need one hash table because it is always left empty. */
7217 if (!fixed_type_or_null_ref_ht)
7218 fixed_type_or_null_ref_ht
7219 = new hash_table<nofree_ptr_hash<tree_node> > (37);
7221 /* Reference variables should be references to objects. */
7222 if (nonnull)
7223 *nonnull = 1;
7225 /* Enter the INSTANCE in a table to prevent recursion; a
7226 variable's initializer may refer to the variable
7227 itself. */
7228 if (VAR_P (instance)
7229 && DECL_INITIAL (instance)
7230 && !type_dependent_expression_p_push (DECL_INITIAL (instance))
7231 && !fixed_type_or_null_ref_ht->find (instance))
7233 tree type;
7234 tree_node **slot;
7236 slot = fixed_type_or_null_ref_ht->find_slot (instance, INSERT);
7237 *slot = instance;
7238 type = RECUR (DECL_INITIAL (instance));
7239 fixed_type_or_null_ref_ht->remove_elt (instance);
7241 return type;
7244 return NULL_TREE;
7246 default:
7247 return NULL_TREE;
7249 #undef RECUR
7252 /* Return nonzero if the dynamic type of INSTANCE is known, and
7253 equivalent to the static type. We also handle the case where
7254 INSTANCE is really a pointer. Return negative if this is a
7255 ctor/dtor. There the dynamic type is known, but this might not be
7256 the most derived base of the original object, and hence virtual
7257 bases may not be laid out according to this type.
7259 Used to determine whether the virtual function table is needed
7260 or not.
7262 *NONNULL is set iff INSTANCE can be known to be nonnull, regardless
7263 of our knowledge of its type. *NONNULL should be initialized
7264 before this function is called. */
7267 resolves_to_fixed_type_p (tree instance, int* nonnull)
7269 tree t = TREE_TYPE (instance);
7270 int cdtorp = 0;
7271 tree fixed;
7273 /* processing_template_decl can be false in a template if we're in
7274 instantiate_non_dependent_expr, but we still want to suppress
7275 this check. */
7276 if (in_template_function ())
7278 /* In a template we only care about the type of the result. */
7279 if (nonnull)
7280 *nonnull = true;
7281 return true;
7284 fixed = fixed_type_or_null (instance, nonnull, &cdtorp);
7285 if (fixed == NULL_TREE)
7286 return 0;
7287 if (POINTER_TYPE_P (t))
7288 t = TREE_TYPE (t);
7289 if (!same_type_ignoring_top_level_qualifiers_p (t, fixed))
7290 return 0;
7291 return cdtorp ? -1 : 1;
7295 void
7296 init_class_processing (void)
7298 current_class_depth = 0;
7299 current_class_stack_size = 10;
7300 current_class_stack
7301 = XNEWVEC (struct class_stack_node, current_class_stack_size);
7302 vec_alloc (local_classes, 8);
7303 sizeof_biggest_empty_class = size_zero_node;
7305 ridpointers[(int) RID_PUBLIC] = access_public_node;
7306 ridpointers[(int) RID_PRIVATE] = access_private_node;
7307 ridpointers[(int) RID_PROTECTED] = access_protected_node;
7310 /* Restore the cached PREVIOUS_CLASS_LEVEL. */
7312 static void
7313 restore_class_cache (void)
7315 tree type;
7317 /* We are re-entering the same class we just left, so we don't
7318 have to search the whole inheritance matrix to find all the
7319 decls to bind again. Instead, we install the cached
7320 class_shadowed list and walk through it binding names. */
7321 push_binding_level (previous_class_level);
7322 class_binding_level = previous_class_level;
7323 /* Restore IDENTIFIER_TYPE_VALUE. */
7324 for (type = class_binding_level->type_shadowed;
7325 type;
7326 type = TREE_CHAIN (type))
7327 SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (type), TREE_TYPE (type));
7330 /* Set global variables CURRENT_CLASS_NAME and CURRENT_CLASS_TYPE as
7331 appropriate for TYPE.
7333 So that we may avoid calls to lookup_name, we cache the _TYPE
7334 nodes of local TYPE_DECLs in the TREE_TYPE field of the name.
7336 For multiple inheritance, we perform a two-pass depth-first search
7337 of the type lattice. */
7339 void
7340 pushclass (tree type)
7342 class_stack_node_t csn;
7344 type = TYPE_MAIN_VARIANT (type);
7346 /* Make sure there is enough room for the new entry on the stack. */
7347 if (current_class_depth + 1 >= current_class_stack_size)
7349 current_class_stack_size *= 2;
7350 current_class_stack
7351 = XRESIZEVEC (struct class_stack_node, current_class_stack,
7352 current_class_stack_size);
7355 /* Insert a new entry on the class stack. */
7356 csn = current_class_stack + current_class_depth;
7357 csn->name = current_class_name;
7358 csn->type = current_class_type;
7359 csn->access = current_access_specifier;
7360 csn->names_used = 0;
7361 csn->hidden = 0;
7362 current_class_depth++;
7364 /* Now set up the new type. */
7365 current_class_name = TYPE_NAME (type);
7366 if (TREE_CODE (current_class_name) == TYPE_DECL)
7367 current_class_name = DECL_NAME (current_class_name);
7368 current_class_type = type;
7370 /* By default, things in classes are private, while things in
7371 structures or unions are public. */
7372 current_access_specifier = (CLASSTYPE_DECLARED_CLASS (type)
7373 ? access_private_node
7374 : access_public_node);
7376 if (previous_class_level
7377 && type != previous_class_level->this_entity
7378 && current_class_depth == 1)
7380 /* Forcibly remove any old class remnants. */
7381 invalidate_class_lookup_cache ();
7384 if (!previous_class_level
7385 || type != previous_class_level->this_entity
7386 || current_class_depth > 1)
7387 pushlevel_class ();
7388 else
7389 restore_class_cache ();
7392 /* When we exit a toplevel class scope, we save its binding level so
7393 that we can restore it quickly. Here, we've entered some other
7394 class, so we must invalidate our cache. */
7396 void
7397 invalidate_class_lookup_cache (void)
7399 previous_class_level = NULL;
7402 /* Get out of the current class scope. If we were in a class scope
7403 previously, that is the one popped to. */
7405 void
7406 popclass (void)
7408 poplevel_class ();
7410 current_class_depth--;
7411 current_class_name = current_class_stack[current_class_depth].name;
7412 current_class_type = current_class_stack[current_class_depth].type;
7413 current_access_specifier = current_class_stack[current_class_depth].access;
7414 if (current_class_stack[current_class_depth].names_used)
7415 splay_tree_delete (current_class_stack[current_class_depth].names_used);
7418 /* Mark the top of the class stack as hidden. */
7420 void
7421 push_class_stack (void)
7423 if (current_class_depth)
7424 ++current_class_stack[current_class_depth - 1].hidden;
7427 /* Mark the top of the class stack as un-hidden. */
7429 void
7430 pop_class_stack (void)
7432 if (current_class_depth)
7433 --current_class_stack[current_class_depth - 1].hidden;
7436 /* Returns 1 if the class type currently being defined is either T or
7437 a nested type of T. Returns the type from the current_class_stack,
7438 which might be equivalent to but not equal to T in case of
7439 constrained partial specializations. */
7441 tree
7442 currently_open_class (tree t)
7444 int i;
7446 if (!CLASS_TYPE_P (t))
7447 return NULL_TREE;
7449 t = TYPE_MAIN_VARIANT (t);
7451 /* We start looking from 1 because entry 0 is from global scope,
7452 and has no type. */
7453 for (i = current_class_depth; i > 0; --i)
7455 tree c;
7456 if (i == current_class_depth)
7457 c = current_class_type;
7458 else
7460 if (current_class_stack[i].hidden)
7461 break;
7462 c = current_class_stack[i].type;
7464 if (!c)
7465 continue;
7466 if (same_type_p (c, t))
7467 return c;
7469 return NULL_TREE;
7472 /* If either current_class_type or one of its enclosing classes are derived
7473 from T, return the appropriate type. Used to determine how we found
7474 something via unqualified lookup. */
7476 tree
7477 currently_open_derived_class (tree t)
7479 int i;
7481 /* The bases of a dependent type are unknown. */
7482 if (dependent_type_p (t))
7483 return NULL_TREE;
7485 if (!current_class_type)
7486 return NULL_TREE;
7488 if (DERIVED_FROM_P (t, current_class_type))
7489 return current_class_type;
7491 for (i = current_class_depth - 1; i > 0; --i)
7493 if (current_class_stack[i].hidden)
7494 break;
7495 if (DERIVED_FROM_P (t, current_class_stack[i].type))
7496 return current_class_stack[i].type;
7499 return NULL_TREE;
7502 /* Return the outermost enclosing class type that is still open, or
7503 NULL_TREE. */
7505 tree
7506 outermost_open_class (void)
7508 if (!current_class_type)
7509 return NULL_TREE;
7510 tree r = NULL_TREE;
7511 if (TYPE_BEING_DEFINED (current_class_type))
7512 r = current_class_type;
7513 for (int i = current_class_depth - 1; i > 0; --i)
7515 if (current_class_stack[i].hidden)
7516 break;
7517 tree t = current_class_stack[i].type;
7518 if (!TYPE_BEING_DEFINED (t))
7519 break;
7520 r = t;
7522 return r;
7525 /* Returns the innermost class type which is not a lambda closure type. */
7527 tree
7528 current_nonlambda_class_type (void)
7530 tree type = current_class_type;
7531 while (type && LAMBDA_TYPE_P (type))
7532 type = decl_type_context (TYPE_NAME (type));
7533 return type;
7536 /* When entering a class scope, all enclosing class scopes' names with
7537 static meaning (static variables, static functions, types and
7538 enumerators) have to be visible. This recursive function calls
7539 pushclass for all enclosing class contexts until global or a local
7540 scope is reached. TYPE is the enclosed class. */
7542 void
7543 push_nested_class (tree type)
7545 /* A namespace might be passed in error cases, like A::B:C. */
7546 if (type == NULL_TREE
7547 || !CLASS_TYPE_P (type))
7548 return;
7550 push_nested_class (DECL_CONTEXT (TYPE_MAIN_DECL (type)));
7552 pushclass (type);
7555 /* Undoes a push_nested_class call. */
7557 void
7558 pop_nested_class (void)
7560 tree context = DECL_CONTEXT (TYPE_MAIN_DECL (current_class_type));
7562 popclass ();
7563 if (context && CLASS_TYPE_P (context))
7564 pop_nested_class ();
7567 /* Returns the number of extern "LANG" blocks we are nested within. */
7570 current_lang_depth (void)
7572 return vec_safe_length (current_lang_base);
7575 /* Set global variables CURRENT_LANG_NAME to appropriate value
7576 so that behavior of name-mangling machinery is correct. */
7578 void
7579 push_lang_context (tree name)
7581 vec_safe_push (current_lang_base, current_lang_name);
7583 if (name == lang_name_cplusplus)
7584 current_lang_name = name;
7585 else if (name == lang_name_c)
7586 current_lang_name = name;
7587 else
7588 error ("language string %<\"%E\"%> not recognized", name);
7591 /* Get out of the current language scope. */
7593 void
7594 pop_lang_context (void)
7596 current_lang_name = current_lang_base->pop ();
7599 /* Type instantiation routines. */
7601 /* Given an OVERLOAD and a TARGET_TYPE, return the function that
7602 matches the TARGET_TYPE. If there is no satisfactory match, return
7603 error_mark_node, and issue an error & warning messages under
7604 control of FLAGS. Permit pointers to member function if FLAGS
7605 permits. If TEMPLATE_ONLY, the name of the overloaded function was
7606 a template-id, and EXPLICIT_TARGS are the explicitly provided
7607 template arguments.
7609 If OVERLOAD is for one or more member functions, then ACCESS_PATH
7610 is the base path used to reference those member functions. If
7611 the address is resolved to a member function, access checks will be
7612 performed and errors issued if appropriate. */
7614 static tree
7615 resolve_address_of_overloaded_function (tree target_type,
7616 tree overload,
7617 tsubst_flags_t complain,
7618 bool template_only,
7619 tree explicit_targs,
7620 tree access_path)
7622 /* Here's what the standard says:
7624 [over.over]
7626 If the name is a function template, template argument deduction
7627 is done, and if the argument deduction succeeds, the deduced
7628 arguments are used to generate a single template function, which
7629 is added to the set of overloaded functions considered.
7631 Non-member functions and static member functions match targets of
7632 type "pointer-to-function" or "reference-to-function." Nonstatic
7633 member functions match targets of type "pointer-to-member
7634 function;" the function type of the pointer to member is used to
7635 select the member function from the set of overloaded member
7636 functions. If a nonstatic member function is selected, the
7637 reference to the overloaded function name is required to have the
7638 form of a pointer to member as described in 5.3.1.
7640 If more than one function is selected, any template functions in
7641 the set are eliminated if the set also contains a non-template
7642 function, and any given template function is eliminated if the
7643 set contains a second template function that is more specialized
7644 than the first according to the partial ordering rules 14.5.5.2.
7645 After such eliminations, if any, there shall remain exactly one
7646 selected function. */
7648 int is_ptrmem = 0;
7649 /* We store the matches in a TREE_LIST rooted here. The functions
7650 are the TREE_PURPOSE, not the TREE_VALUE, in this list, for easy
7651 interoperability with most_specialized_instantiation. */
7652 tree matches = NULL_TREE;
7653 tree fn;
7654 tree target_fn_type;
7656 /* By the time we get here, we should be seeing only real
7657 pointer-to-member types, not the internal POINTER_TYPE to
7658 METHOD_TYPE representation. */
7659 gcc_assert (!TYPE_PTR_P (target_type)
7660 || TREE_CODE (TREE_TYPE (target_type)) != METHOD_TYPE);
7662 gcc_assert (is_overloaded_fn (overload));
7664 /* Check that the TARGET_TYPE is reasonable. */
7665 if (TYPE_PTRFN_P (target_type)
7666 || TYPE_REFFN_P (target_type))
7667 /* This is OK. */;
7668 else if (TYPE_PTRMEMFUNC_P (target_type))
7669 /* This is OK, too. */
7670 is_ptrmem = 1;
7671 else if (TREE_CODE (target_type) == FUNCTION_TYPE)
7672 /* This is OK, too. This comes from a conversion to reference
7673 type. */
7674 target_type = build_reference_type (target_type);
7675 else
7677 if (complain & tf_error)
7678 error ("cannot resolve overloaded function %qD based on"
7679 " conversion to type %qT",
7680 OVL_NAME (overload), target_type);
7681 return error_mark_node;
7684 /* Non-member functions and static member functions match targets of type
7685 "pointer-to-function" or "reference-to-function." Nonstatic member
7686 functions match targets of type "pointer-to-member-function;" the
7687 function type of the pointer to member is used to select the member
7688 function from the set of overloaded member functions.
7690 So figure out the FUNCTION_TYPE that we want to match against. */
7691 target_fn_type = static_fn_type (target_type);
7693 /* If we can find a non-template function that matches, we can just
7694 use it. There's no point in generating template instantiations
7695 if we're just going to throw them out anyhow. But, of course, we
7696 can only do this when we don't *need* a template function. */
7697 if (!template_only)
7698 for (lkp_iterator iter (overload); iter; ++iter)
7700 tree fn = *iter;
7702 if (TREE_CODE (fn) == TEMPLATE_DECL)
7703 /* We're not looking for templates just yet. */
7704 continue;
7706 if ((TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE) != is_ptrmem)
7707 /* We're looking for a non-static member, and this isn't
7708 one, or vice versa. */
7709 continue;
7711 /* In C++17 we need the noexcept-qualifier to compare types. */
7712 if (flag_noexcept_type
7713 && !maybe_instantiate_noexcept (fn, complain))
7714 continue;
7716 /* See if there's a match. */
7717 tree fntype = static_fn_type (fn);
7718 if (same_type_p (target_fn_type, fntype)
7719 || fnptr_conv_p (target_fn_type, fntype))
7720 matches = tree_cons (fn, NULL_TREE, matches);
7723 /* Now, if we've already got a match (or matches), there's no need
7724 to proceed to the template functions. But, if we don't have a
7725 match we need to look at them, too. */
7726 if (!matches)
7728 tree target_arg_types;
7729 tree target_ret_type;
7730 tree *args;
7731 unsigned int nargs, ia;
7732 tree arg;
7734 target_arg_types = TYPE_ARG_TYPES (target_fn_type);
7735 target_ret_type = TREE_TYPE (target_fn_type);
7737 nargs = list_length (target_arg_types);
7738 args = XALLOCAVEC (tree, nargs);
7739 for (arg = target_arg_types, ia = 0;
7740 arg != NULL_TREE && arg != void_list_node;
7741 arg = TREE_CHAIN (arg), ++ia)
7742 args[ia] = TREE_VALUE (arg);
7743 nargs = ia;
7745 for (lkp_iterator iter (overload); iter; ++iter)
7747 tree fn = *iter;
7748 tree instantiation;
7749 tree targs;
7751 if (TREE_CODE (fn) != TEMPLATE_DECL)
7752 /* We're only looking for templates. */
7753 continue;
7755 if ((TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
7756 != is_ptrmem)
7757 /* We're not looking for a non-static member, and this is
7758 one, or vice versa. */
7759 continue;
7761 tree ret = target_ret_type;
7763 /* If the template has a deduced return type, don't expose it to
7764 template argument deduction. */
7765 if (undeduced_auto_decl (fn))
7766 ret = NULL_TREE;
7768 /* Try to do argument deduction. */
7769 targs = make_tree_vec (DECL_NTPARMS (fn));
7770 instantiation = fn_type_unification (fn, explicit_targs, targs, args,
7771 nargs, ret,
7772 DEDUCE_EXACT, LOOKUP_NORMAL,
7773 false, false);
7774 if (instantiation == error_mark_node)
7775 /* Instantiation failed. */
7776 continue;
7778 /* Constraints must be satisfied. This is done before
7779 return type deduction since that instantiates the
7780 function. */
7781 if (flag_concepts && !constraints_satisfied_p (instantiation))
7782 continue;
7784 /* And now force instantiation to do return type deduction. */
7785 if (undeduced_auto_decl (instantiation))
7787 ++function_depth;
7788 instantiate_decl (instantiation, /*defer*/false, /*class*/false);
7789 --function_depth;
7791 require_deduced_type (instantiation);
7794 /* In C++17 we need the noexcept-qualifier to compare types. */
7795 if (flag_noexcept_type)
7796 maybe_instantiate_noexcept (instantiation, complain);
7798 /* See if there's a match. */
7799 tree fntype = static_fn_type (instantiation);
7800 if (same_type_p (target_fn_type, fntype)
7801 || fnptr_conv_p (target_fn_type, fntype))
7802 matches = tree_cons (instantiation, fn, matches);
7805 /* Now, remove all but the most specialized of the matches. */
7806 if (matches)
7808 tree match = most_specialized_instantiation (matches);
7810 if (match != error_mark_node)
7811 matches = tree_cons (TREE_PURPOSE (match),
7812 NULL_TREE,
7813 NULL_TREE);
7817 /* Now we should have exactly one function in MATCHES. */
7818 if (matches == NULL_TREE)
7820 /* There were *no* matches. */
7821 if (complain & tf_error)
7823 error ("no matches converting function %qD to type %q#T",
7824 OVL_NAME (overload), target_type);
7826 print_candidates (overload);
7828 return error_mark_node;
7830 else if (TREE_CHAIN (matches))
7832 /* There were too many matches. First check if they're all
7833 the same function. */
7834 tree match = NULL_TREE;
7836 fn = TREE_PURPOSE (matches);
7838 /* For multi-versioned functions, more than one match is just fine and
7839 decls_match will return false as they are different. */
7840 for (match = TREE_CHAIN (matches); match; match = TREE_CHAIN (match))
7841 if (!decls_match (fn, TREE_PURPOSE (match))
7842 && !targetm.target_option.function_versions
7843 (fn, TREE_PURPOSE (match)))
7844 break;
7846 if (match)
7848 if (complain & tf_error)
7850 error ("converting overloaded function %qD to type %q#T is ambiguous",
7851 OVL_NAME (overload), target_type);
7853 /* Since print_candidates expects the functions in the
7854 TREE_VALUE slot, we flip them here. */
7855 for (match = matches; match; match = TREE_CHAIN (match))
7856 TREE_VALUE (match) = TREE_PURPOSE (match);
7858 print_candidates (matches);
7861 return error_mark_node;
7865 /* Good, exactly one match. Now, convert it to the correct type. */
7866 fn = TREE_PURPOSE (matches);
7868 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
7869 && !(complain & tf_ptrmem_ok) && !flag_ms_extensions)
7871 static int explained;
7873 if (!(complain & tf_error))
7874 return error_mark_node;
7876 permerror (input_location, "assuming pointer to member %qD", fn);
7877 if (!explained)
7879 inform (input_location, "(a pointer to member can only be formed with %<&%E%>)", fn);
7880 explained = 1;
7884 /* If a pointer to a function that is multi-versioned is requested, the
7885 pointer to the dispatcher function is returned instead. This works
7886 well because indirectly calling the function will dispatch the right
7887 function version at run-time. */
7888 if (DECL_FUNCTION_VERSIONED (fn))
7890 fn = get_function_version_dispatcher (fn);
7891 if (fn == NULL)
7892 return error_mark_node;
7893 /* Mark all the versions corresponding to the dispatcher as used. */
7894 if (!(complain & tf_conv))
7895 mark_versions_used (fn);
7898 /* If we're doing overload resolution purely for the purpose of
7899 determining conversion sequences, we should not consider the
7900 function used. If this conversion sequence is selected, the
7901 function will be marked as used at this point. */
7902 if (!(complain & tf_conv))
7904 /* Make =delete work with SFINAE. */
7905 if (DECL_DELETED_FN (fn) && !(complain & tf_error))
7906 return error_mark_node;
7907 if (!mark_used (fn, complain) && !(complain & tf_error))
7908 return error_mark_node;
7911 /* We could not check access to member functions when this
7912 expression was originally created since we did not know at that
7913 time to which function the expression referred. */
7914 if (DECL_FUNCTION_MEMBER_P (fn))
7916 gcc_assert (access_path);
7917 perform_or_defer_access_check (access_path, fn, fn, complain);
7920 if (TYPE_PTRFN_P (target_type) || TYPE_PTRMEMFUNC_P (target_type))
7921 return cp_build_addr_expr (fn, complain);
7922 else
7924 /* The target must be a REFERENCE_TYPE. Above, cp_build_unary_op
7925 will mark the function as addressed, but here we must do it
7926 explicitly. */
7927 cxx_mark_addressable (fn);
7929 return fn;
7933 /* This function will instantiate the type of the expression given in
7934 RHS to match the type of LHSTYPE. If errors exist, then return
7935 error_mark_node. COMPLAIN is a bit mask. If TF_ERROR is set, then
7936 we complain on errors. If we are not complaining, never modify rhs,
7937 as overload resolution wants to try many possible instantiations, in
7938 the hope that at least one will work.
7940 For non-recursive calls, LHSTYPE should be a function, pointer to
7941 function, or a pointer to member function. */
7943 tree
7944 instantiate_type (tree lhstype, tree rhs, tsubst_flags_t complain)
7946 tsubst_flags_t complain_in = complain;
7947 tree access_path = NULL_TREE;
7949 complain &= ~tf_ptrmem_ok;
7951 if (lhstype == unknown_type_node)
7953 if (complain & tf_error)
7954 error ("not enough type information");
7955 return error_mark_node;
7958 if (TREE_TYPE (rhs) != NULL_TREE && ! (type_unknown_p (rhs)))
7960 tree fntype = non_reference (lhstype);
7961 if (same_type_p (fntype, TREE_TYPE (rhs)))
7962 return rhs;
7963 if (fnptr_conv_p (fntype, TREE_TYPE (rhs)))
7964 return rhs;
7965 if (flag_ms_extensions
7966 && TYPE_PTRMEMFUNC_P (fntype)
7967 && !TYPE_PTRMEMFUNC_P (TREE_TYPE (rhs)))
7968 /* Microsoft allows `A::f' to be resolved to a
7969 pointer-to-member. */
7971 else
7973 if (complain & tf_error)
7974 error ("cannot convert %qE from type %qT to type %qT",
7975 rhs, TREE_TYPE (rhs), fntype);
7976 return error_mark_node;
7980 if (BASELINK_P (rhs))
7982 access_path = BASELINK_ACCESS_BINFO (rhs);
7983 rhs = BASELINK_FUNCTIONS (rhs);
7986 /* If we are in a template, and have a NON_DEPENDENT_EXPR, we cannot
7987 deduce any type information. */
7988 if (TREE_CODE (rhs) == NON_DEPENDENT_EXPR)
7990 if (complain & tf_error)
7991 error ("not enough type information");
7992 return error_mark_node;
7995 /* If we instantiate a template, and it is a A ?: C expression
7996 with omitted B, look through the SAVE_EXPR. */
7997 if (TREE_CODE (rhs) == SAVE_EXPR)
7998 rhs = TREE_OPERAND (rhs, 0);
8000 /* There are only a few kinds of expressions that may have a type
8001 dependent on overload resolution. */
8002 gcc_assert (TREE_CODE (rhs) == ADDR_EXPR
8003 || TREE_CODE (rhs) == COMPONENT_REF
8004 || is_overloaded_fn (rhs)
8005 || (flag_ms_extensions && TREE_CODE (rhs) == FUNCTION_DECL));
8007 /* This should really only be used when attempting to distinguish
8008 what sort of a pointer to function we have. For now, any
8009 arithmetic operation which is not supported on pointers
8010 is rejected as an error. */
8012 switch (TREE_CODE (rhs))
8014 case COMPONENT_REF:
8016 tree member = TREE_OPERAND (rhs, 1);
8018 member = instantiate_type (lhstype, member, complain);
8019 if (member != error_mark_node
8020 && TREE_SIDE_EFFECTS (TREE_OPERAND (rhs, 0)))
8021 /* Do not lose object's side effects. */
8022 return build2 (COMPOUND_EXPR, TREE_TYPE (member),
8023 TREE_OPERAND (rhs, 0), member);
8024 return member;
8027 case OFFSET_REF:
8028 rhs = TREE_OPERAND (rhs, 1);
8029 if (BASELINK_P (rhs))
8030 return instantiate_type (lhstype, rhs, complain_in);
8032 /* This can happen if we are forming a pointer-to-member for a
8033 member template. */
8034 gcc_assert (TREE_CODE (rhs) == TEMPLATE_ID_EXPR);
8036 /* Fall through. */
8038 case TEMPLATE_ID_EXPR:
8040 tree fns = TREE_OPERAND (rhs, 0);
8041 tree args = TREE_OPERAND (rhs, 1);
8043 return
8044 resolve_address_of_overloaded_function (lhstype, fns, complain_in,
8045 /*template_only=*/true,
8046 args, access_path);
8049 case OVERLOAD:
8050 case FUNCTION_DECL:
8051 return
8052 resolve_address_of_overloaded_function (lhstype, rhs, complain_in,
8053 /*template_only=*/false,
8054 /*explicit_targs=*/NULL_TREE,
8055 access_path);
8057 case ADDR_EXPR:
8059 if (PTRMEM_OK_P (rhs))
8060 complain |= tf_ptrmem_ok;
8062 return instantiate_type (lhstype, TREE_OPERAND (rhs, 0), complain);
8065 case ERROR_MARK:
8066 return error_mark_node;
8068 default:
8069 gcc_unreachable ();
8071 return error_mark_node;
8074 /* Return the name of the virtual function pointer field
8075 (as an IDENTIFIER_NODE) for the given TYPE. Note that
8076 this may have to look back through base types to find the
8077 ultimate field name. (For single inheritance, these could
8078 all be the same name. Who knows for multiple inheritance). */
8080 static tree
8081 get_vfield_name (tree type)
8083 tree binfo, base_binfo;
8085 for (binfo = TYPE_BINFO (type);
8086 BINFO_N_BASE_BINFOS (binfo);
8087 binfo = base_binfo)
8089 base_binfo = BINFO_BASE_BINFO (binfo, 0);
8091 if (BINFO_VIRTUAL_P (base_binfo)
8092 || !TYPE_CONTAINS_VPTR_P (BINFO_TYPE (base_binfo)))
8093 break;
8096 type = BINFO_TYPE (binfo);
8097 tree ctor_name = constructor_name (type);
8098 char *buf = (char *) alloca (sizeof (VFIELD_NAME_FORMAT)
8099 + IDENTIFIER_LENGTH (ctor_name) + 2);
8100 sprintf (buf, VFIELD_NAME_FORMAT, IDENTIFIER_POINTER (ctor_name));
8101 return get_identifier (buf);
8104 /* Build a dummy reference to ourselves so Derived::Base (and A::A) works,
8105 according to [class]:
8106 The class-name is also inserted
8107 into the scope of the class itself. For purposes of access checking,
8108 the inserted class name is treated as if it were a public member name. */
8110 void
8111 build_self_reference (void)
8113 tree name = DECL_NAME (TYPE_NAME (current_class_type));
8114 tree value = build_lang_decl (TYPE_DECL, name, current_class_type);
8116 DECL_NONLOCAL (value) = 1;
8117 DECL_CONTEXT (value) = current_class_type;
8118 DECL_ARTIFICIAL (value) = 1;
8119 SET_DECL_SELF_REFERENCE_P (value);
8120 set_underlying_type (value);
8122 if (processing_template_decl)
8123 value = push_template_decl (value);
8125 tree saved_cas = current_access_specifier;
8126 current_access_specifier = access_public_node;
8127 finish_member_declaration (value);
8128 current_access_specifier = saved_cas;
8131 /* Returns 1 if TYPE contains only padding bytes. */
8134 is_empty_class (tree type)
8136 if (type == error_mark_node)
8137 return 0;
8139 if (! CLASS_TYPE_P (type))
8140 return 0;
8142 return CLASSTYPE_EMPTY_P (type);
8145 /* Returns true if TYPE contains no actual data, just various
8146 possible combinations of empty classes and possibly a vptr. */
8148 bool
8149 is_really_empty_class (tree type)
8151 if (CLASS_TYPE_P (type))
8153 tree field;
8154 tree binfo;
8155 tree base_binfo;
8156 int i;
8158 /* CLASSTYPE_EMPTY_P isn't set properly until the class is actually laid
8159 out, but we'd like to be able to check this before then. */
8160 if (COMPLETE_TYPE_P (type) && is_empty_class (type))
8161 return true;
8163 for (binfo = TYPE_BINFO (type), i = 0;
8164 BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
8165 if (!is_really_empty_class (BINFO_TYPE (base_binfo)))
8166 return false;
8167 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
8168 if (TREE_CODE (field) == FIELD_DECL
8169 && !DECL_ARTIFICIAL (field)
8170 /* An unnamed bit-field is not a data member. */
8171 && !DECL_UNNAMED_BIT_FIELD (field)
8172 && !is_really_empty_class (TREE_TYPE (field)))
8173 return false;
8174 return true;
8176 else if (TREE_CODE (type) == ARRAY_TYPE)
8177 return (integer_zerop (array_type_nelts_top (type))
8178 || is_really_empty_class (TREE_TYPE (type)));
8179 return false;
8182 /* Note that NAME was looked up while the current class was being
8183 defined and that the result of that lookup was DECL. */
8185 void
8186 maybe_note_name_used_in_class (tree name, tree decl)
8188 splay_tree names_used;
8190 /* If we're not defining a class, there's nothing to do. */
8191 if (!(innermost_scope_kind() == sk_class
8192 && TYPE_BEING_DEFINED (current_class_type)
8193 && !LAMBDA_TYPE_P (current_class_type)))
8194 return;
8196 /* If there's already a binding for this NAME, then we don't have
8197 anything to worry about. */
8198 if (lookup_member (current_class_type, name,
8199 /*protect=*/0, /*want_type=*/false, tf_warning_or_error))
8200 return;
8202 if (!current_class_stack[current_class_depth - 1].names_used)
8203 current_class_stack[current_class_depth - 1].names_used
8204 = splay_tree_new (splay_tree_compare_pointers, 0, 0);
8205 names_used = current_class_stack[current_class_depth - 1].names_used;
8207 splay_tree_insert (names_used,
8208 (splay_tree_key) name,
8209 (splay_tree_value) decl);
8212 /* Note that NAME was declared (as DECL) in the current class. Check
8213 to see that the declaration is valid. */
8215 void
8216 note_name_declared_in_class (tree name, tree decl)
8218 splay_tree names_used;
8219 splay_tree_node n;
8221 /* Look to see if we ever used this name. */
8222 names_used
8223 = current_class_stack[current_class_depth - 1].names_used;
8224 if (!names_used)
8225 return;
8226 /* The C language allows members to be declared with a type of the same
8227 name, and the C++ standard says this diagnostic is not required. So
8228 allow it in extern "C" blocks unless predantic is specified.
8229 Allow it in all cases if -ms-extensions is specified. */
8230 if ((!pedantic && current_lang_name == lang_name_c)
8231 || flag_ms_extensions)
8232 return;
8233 n = splay_tree_lookup (names_used, (splay_tree_key) name);
8234 if (n)
8236 /* [basic.scope.class]
8238 A name N used in a class S shall refer to the same declaration
8239 in its context and when re-evaluated in the completed scope of
8240 S. */
8241 permerror (input_location, "declaration of %q#D", decl);
8242 permerror (location_of ((tree) n->value),
8243 "changes meaning of %qD from %q#D",
8244 OVL_NAME (decl), (tree) n->value);
8248 /* Returns the VAR_DECL for the complete vtable associated with BINFO.
8249 Secondary vtables are merged with primary vtables; this function
8250 will return the VAR_DECL for the primary vtable. */
8252 tree
8253 get_vtbl_decl_for_binfo (tree binfo)
8255 tree decl;
8257 decl = BINFO_VTABLE (binfo);
8258 if (decl && TREE_CODE (decl) == POINTER_PLUS_EXPR)
8260 gcc_assert (TREE_CODE (TREE_OPERAND (decl, 0)) == ADDR_EXPR);
8261 decl = TREE_OPERAND (TREE_OPERAND (decl, 0), 0);
8263 if (decl)
8264 gcc_assert (VAR_P (decl));
8265 return decl;
8269 /* Returns the binfo for the primary base of BINFO. If the resulting
8270 BINFO is a virtual base, and it is inherited elsewhere in the
8271 hierarchy, then the returned binfo might not be the primary base of
8272 BINFO in the complete object. Check BINFO_PRIMARY_P or
8273 BINFO_LOST_PRIMARY_P to be sure. */
8275 static tree
8276 get_primary_binfo (tree binfo)
8278 tree primary_base;
8280 primary_base = CLASSTYPE_PRIMARY_BINFO (BINFO_TYPE (binfo));
8281 if (!primary_base)
8282 return NULL_TREE;
8284 return copied_binfo (primary_base, binfo);
8287 /* As above, but iterate until we reach the binfo that actually provides the
8288 vptr for BINFO. */
8290 static tree
8291 most_primary_binfo (tree binfo)
8293 tree b = binfo;
8294 while (CLASSTYPE_HAS_PRIMARY_BASE_P (BINFO_TYPE (b))
8295 && !BINFO_LOST_PRIMARY_P (b))
8297 tree primary_base = get_primary_binfo (b);
8298 gcc_assert (BINFO_PRIMARY_P (primary_base)
8299 && BINFO_INHERITANCE_CHAIN (primary_base) == b);
8300 b = primary_base;
8302 return b;
8305 /* Returns true if BINFO gets its vptr from a virtual base of the most derived
8306 type. Note that the virtual inheritance might be above or below BINFO in
8307 the hierarchy. */
8309 bool
8310 vptr_via_virtual_p (tree binfo)
8312 if (TYPE_P (binfo))
8313 binfo = TYPE_BINFO (binfo);
8314 tree primary = most_primary_binfo (binfo);
8315 /* Don't limit binfo_via_virtual, we want to return true when BINFO itself is
8316 a morally virtual base. */
8317 tree virt = binfo_via_virtual (primary, NULL_TREE);
8318 return virt != NULL_TREE;
8321 /* If INDENTED_P is zero, indent to INDENT. Return nonzero. */
8323 static int
8324 maybe_indent_hierarchy (FILE * stream, int indent, int indented_p)
8326 if (!indented_p)
8327 fprintf (stream, "%*s", indent, "");
8328 return 1;
8331 /* Dump the offsets of all the bases rooted at BINFO to STREAM.
8332 INDENT should be zero when called from the top level; it is
8333 incremented recursively. IGO indicates the next expected BINFO in
8334 inheritance graph ordering. */
8336 static tree
8337 dump_class_hierarchy_r (FILE *stream,
8338 dump_flags_t flags,
8339 tree binfo,
8340 tree igo,
8341 int indent)
8343 int indented = 0;
8344 tree base_binfo;
8345 int i;
8347 indented = maybe_indent_hierarchy (stream, indent, 0);
8348 fprintf (stream, "%s (0x" HOST_WIDE_INT_PRINT_HEX ") ",
8349 type_as_string (BINFO_TYPE (binfo), TFF_PLAIN_IDENTIFIER),
8350 (HOST_WIDE_INT) (uintptr_t) binfo);
8351 if (binfo != igo)
8353 fprintf (stream, "alternative-path\n");
8354 return igo;
8356 igo = TREE_CHAIN (binfo);
8358 fprintf (stream, HOST_WIDE_INT_PRINT_DEC,
8359 tree_to_shwi (BINFO_OFFSET (binfo)));
8360 if (is_empty_class (BINFO_TYPE (binfo)))
8361 fprintf (stream, " empty");
8362 else if (CLASSTYPE_NEARLY_EMPTY_P (BINFO_TYPE (binfo)))
8363 fprintf (stream, " nearly-empty");
8364 if (BINFO_VIRTUAL_P (binfo))
8365 fprintf (stream, " virtual");
8366 fprintf (stream, "\n");
8368 indented = 0;
8369 if (BINFO_PRIMARY_P (binfo))
8371 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
8372 fprintf (stream, " primary-for %s (0x" HOST_WIDE_INT_PRINT_HEX ")",
8373 type_as_string (BINFO_TYPE (BINFO_INHERITANCE_CHAIN (binfo)),
8374 TFF_PLAIN_IDENTIFIER),
8375 (HOST_WIDE_INT) (uintptr_t) BINFO_INHERITANCE_CHAIN (binfo));
8377 if (BINFO_LOST_PRIMARY_P (binfo))
8379 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
8380 fprintf (stream, " lost-primary");
8382 if (indented)
8383 fprintf (stream, "\n");
8385 if (!(flags & TDF_SLIM))
8387 int indented = 0;
8389 if (BINFO_SUBVTT_INDEX (binfo))
8391 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
8392 fprintf (stream, " subvttidx=%s",
8393 expr_as_string (BINFO_SUBVTT_INDEX (binfo),
8394 TFF_PLAIN_IDENTIFIER));
8396 if (BINFO_VPTR_INDEX (binfo))
8398 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
8399 fprintf (stream, " vptridx=%s",
8400 expr_as_string (BINFO_VPTR_INDEX (binfo),
8401 TFF_PLAIN_IDENTIFIER));
8403 if (BINFO_VPTR_FIELD (binfo))
8405 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
8406 fprintf (stream, " vbaseoffset=%s",
8407 expr_as_string (BINFO_VPTR_FIELD (binfo),
8408 TFF_PLAIN_IDENTIFIER));
8410 if (BINFO_VTABLE (binfo))
8412 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
8413 fprintf (stream, " vptr=%s",
8414 expr_as_string (BINFO_VTABLE (binfo),
8415 TFF_PLAIN_IDENTIFIER));
8418 if (indented)
8419 fprintf (stream, "\n");
8422 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
8423 igo = dump_class_hierarchy_r (stream, flags, base_binfo, igo, indent + 2);
8425 return igo;
8428 /* Dump the BINFO hierarchy for T. */
8430 static void
8431 dump_class_hierarchy_1 (FILE *stream, dump_flags_t flags, tree t)
8433 fprintf (stream, "Class %s\n", type_as_string (t, TFF_PLAIN_IDENTIFIER));
8434 fprintf (stream, " size=%lu align=%lu\n",
8435 (unsigned long)(tree_to_shwi (TYPE_SIZE (t)) / BITS_PER_UNIT),
8436 (unsigned long)(TYPE_ALIGN (t) / BITS_PER_UNIT));
8437 fprintf (stream, " base size=%lu base align=%lu\n",
8438 (unsigned long)(tree_to_shwi (TYPE_SIZE (CLASSTYPE_AS_BASE (t)))
8439 / BITS_PER_UNIT),
8440 (unsigned long)(TYPE_ALIGN (CLASSTYPE_AS_BASE (t))
8441 / BITS_PER_UNIT));
8442 dump_class_hierarchy_r (stream, flags, TYPE_BINFO (t), TYPE_BINFO (t), 0);
8443 fprintf (stream, "\n");
8446 /* Debug interface to hierarchy dumping. */
8448 void
8449 debug_class (tree t)
8451 dump_class_hierarchy_1 (stderr, TDF_SLIM, t);
8454 static void
8455 dump_class_hierarchy (tree t)
8457 dump_flags_t flags;
8458 if (FILE *stream = dump_begin (class_dump_id, &flags))
8460 dump_class_hierarchy_1 (stream, flags, t);
8461 dump_end (class_dump_id, stream);
8465 static void
8466 dump_array (FILE * stream, tree decl)
8468 tree value;
8469 unsigned HOST_WIDE_INT ix;
8470 HOST_WIDE_INT elt;
8471 tree size = TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (decl)));
8473 elt = (tree_to_shwi (TYPE_SIZE (TREE_TYPE (TREE_TYPE (decl))))
8474 / BITS_PER_UNIT);
8475 fprintf (stream, "%s:", decl_as_string (decl, TFF_PLAIN_IDENTIFIER));
8476 fprintf (stream, " %s entries",
8477 expr_as_string (size_binop (PLUS_EXPR, size, size_one_node),
8478 TFF_PLAIN_IDENTIFIER));
8479 fprintf (stream, "\n");
8481 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (DECL_INITIAL (decl)),
8482 ix, value)
8483 fprintf (stream, "%-4ld %s\n", (long)(ix * elt),
8484 expr_as_string (value, TFF_PLAIN_IDENTIFIER));
8487 static void
8488 dump_vtable (tree t, tree binfo, tree vtable)
8490 dump_flags_t flags;
8491 FILE *stream = dump_begin (class_dump_id, &flags);
8493 if (!stream)
8494 return;
8496 if (!(flags & TDF_SLIM))
8498 int ctor_vtbl_p = TYPE_BINFO (t) != binfo;
8500 fprintf (stream, "%s for %s",
8501 ctor_vtbl_p ? "Construction vtable" : "Vtable",
8502 type_as_string (BINFO_TYPE (binfo), TFF_PLAIN_IDENTIFIER));
8503 if (ctor_vtbl_p)
8505 if (!BINFO_VIRTUAL_P (binfo))
8506 fprintf (stream, " (0x" HOST_WIDE_INT_PRINT_HEX " instance)",
8507 (HOST_WIDE_INT) (uintptr_t) binfo);
8508 fprintf (stream, " in %s", type_as_string (t, TFF_PLAIN_IDENTIFIER));
8510 fprintf (stream, "\n");
8511 dump_array (stream, vtable);
8512 fprintf (stream, "\n");
8515 dump_end (class_dump_id, stream);
8518 static void
8519 dump_vtt (tree t, tree vtt)
8521 dump_flags_t flags;
8522 FILE *stream = dump_begin (class_dump_id, &flags);
8524 if (!stream)
8525 return;
8527 if (!(flags & TDF_SLIM))
8529 fprintf (stream, "VTT for %s\n",
8530 type_as_string (t, TFF_PLAIN_IDENTIFIER));
8531 dump_array (stream, vtt);
8532 fprintf (stream, "\n");
8535 dump_end (class_dump_id, stream);
8538 /* Dump a function or thunk and its thunkees. */
8540 static void
8541 dump_thunk (FILE *stream, int indent, tree thunk)
8543 static const char spaces[] = " ";
8544 tree name = DECL_NAME (thunk);
8545 tree thunks;
8547 fprintf (stream, "%.*s%p %s %s", indent, spaces,
8548 (void *)thunk,
8549 !DECL_THUNK_P (thunk) ? "function"
8550 : DECL_THIS_THUNK_P (thunk) ? "this-thunk" : "covariant-thunk",
8551 name ? IDENTIFIER_POINTER (name) : "<unset>");
8552 if (DECL_THUNK_P (thunk))
8554 HOST_WIDE_INT fixed_adjust = THUNK_FIXED_OFFSET (thunk);
8555 tree virtual_adjust = THUNK_VIRTUAL_OFFSET (thunk);
8557 fprintf (stream, " fixed=" HOST_WIDE_INT_PRINT_DEC, fixed_adjust);
8558 if (!virtual_adjust)
8559 /*NOP*/;
8560 else if (DECL_THIS_THUNK_P (thunk))
8561 fprintf (stream, " vcall=" HOST_WIDE_INT_PRINT_DEC,
8562 tree_to_shwi (virtual_adjust));
8563 else
8564 fprintf (stream, " vbase=" HOST_WIDE_INT_PRINT_DEC "(%s)",
8565 tree_to_shwi (BINFO_VPTR_FIELD (virtual_adjust)),
8566 type_as_string (BINFO_TYPE (virtual_adjust), TFF_SCOPE));
8567 if (THUNK_ALIAS (thunk))
8568 fprintf (stream, " alias to %p", (void *)THUNK_ALIAS (thunk));
8570 fprintf (stream, "\n");
8571 for (thunks = DECL_THUNKS (thunk); thunks; thunks = TREE_CHAIN (thunks))
8572 dump_thunk (stream, indent + 2, thunks);
8575 /* Dump the thunks for FN. */
8577 void
8578 debug_thunks (tree fn)
8580 dump_thunk (stderr, 0, fn);
8583 /* Virtual function table initialization. */
8585 /* Create all the necessary vtables for T and its base classes. */
8587 static void
8588 finish_vtbls (tree t)
8590 tree vbase;
8591 vec<constructor_elt, va_gc> *v = NULL;
8592 tree vtable = BINFO_VTABLE (TYPE_BINFO (t));
8594 /* We lay out the primary and secondary vtables in one contiguous
8595 vtable. The primary vtable is first, followed by the non-virtual
8596 secondary vtables in inheritance graph order. */
8597 accumulate_vtbl_inits (TYPE_BINFO (t), TYPE_BINFO (t), TYPE_BINFO (t),
8598 vtable, t, &v);
8600 /* Then come the virtual bases, also in inheritance graph order. */
8601 for (vbase = TYPE_BINFO (t); vbase; vbase = TREE_CHAIN (vbase))
8603 if (!BINFO_VIRTUAL_P (vbase))
8604 continue;
8605 accumulate_vtbl_inits (vbase, vbase, TYPE_BINFO (t), vtable, t, &v);
8608 if (BINFO_VTABLE (TYPE_BINFO (t)))
8609 initialize_vtable (TYPE_BINFO (t), v);
8612 /* Initialize the vtable for BINFO with the INITS. */
8614 static void
8615 initialize_vtable (tree binfo, vec<constructor_elt, va_gc> *inits)
8617 tree decl;
8619 layout_vtable_decl (binfo, vec_safe_length (inits));
8620 decl = get_vtbl_decl_for_binfo (binfo);
8621 initialize_artificial_var (decl, inits);
8622 dump_vtable (BINFO_TYPE (binfo), binfo, decl);
8625 /* Build the VTT (virtual table table) for T.
8626 A class requires a VTT if it has virtual bases.
8628 This holds
8629 1 - primary virtual pointer for complete object T
8630 2 - secondary VTTs for each direct non-virtual base of T which requires a
8632 3 - secondary virtual pointers for each direct or indirect base of T which
8633 has virtual bases or is reachable via a virtual path from T.
8634 4 - secondary VTTs for each direct or indirect virtual base of T.
8636 Secondary VTTs look like complete object VTTs without part 4. */
8638 static void
8639 build_vtt (tree t)
8641 tree type;
8642 tree vtt;
8643 tree index;
8644 vec<constructor_elt, va_gc> *inits;
8646 /* Build up the initializers for the VTT. */
8647 inits = NULL;
8648 index = size_zero_node;
8649 build_vtt_inits (TYPE_BINFO (t), t, &inits, &index);
8651 /* If we didn't need a VTT, we're done. */
8652 if (!inits)
8653 return;
8655 /* Figure out the type of the VTT. */
8656 type = build_array_of_n_type (const_ptr_type_node,
8657 inits->length ());
8659 /* Now, build the VTT object itself. */
8660 vtt = build_vtable (t, mangle_vtt_for_type (t), type);
8661 initialize_artificial_var (vtt, inits);
8662 /* Add the VTT to the vtables list. */
8663 DECL_CHAIN (vtt) = DECL_CHAIN (CLASSTYPE_VTABLES (t));
8664 DECL_CHAIN (CLASSTYPE_VTABLES (t)) = vtt;
8666 dump_vtt (t, vtt);
8669 /* When building a secondary VTT, BINFO_VTABLE is set to a TREE_LIST with
8670 PURPOSE the RTTI_BINFO, VALUE the real vtable pointer for this binfo,
8671 and CHAIN the vtable pointer for this binfo after construction is
8672 complete. VALUE can also be another BINFO, in which case we recurse. */
8674 static tree
8675 binfo_ctor_vtable (tree binfo)
8677 tree vt;
8679 while (1)
8681 vt = BINFO_VTABLE (binfo);
8682 if (TREE_CODE (vt) == TREE_LIST)
8683 vt = TREE_VALUE (vt);
8684 if (TREE_CODE (vt) == TREE_BINFO)
8685 binfo = vt;
8686 else
8687 break;
8690 return vt;
8693 /* Data for secondary VTT initialization. */
8694 struct secondary_vptr_vtt_init_data
8696 /* Is this the primary VTT? */
8697 bool top_level_p;
8699 /* Current index into the VTT. */
8700 tree index;
8702 /* Vector of initializers built up. */
8703 vec<constructor_elt, va_gc> *inits;
8705 /* The type being constructed by this secondary VTT. */
8706 tree type_being_constructed;
8709 /* Recursively build the VTT-initializer for BINFO (which is in the
8710 hierarchy dominated by T). INITS points to the end of the initializer
8711 list to date. INDEX is the VTT index where the next element will be
8712 replaced. Iff BINFO is the binfo for T, this is the top level VTT (i.e.
8713 not a subvtt for some base of T). When that is so, we emit the sub-VTTs
8714 for virtual bases of T. When it is not so, we build the constructor
8715 vtables for the BINFO-in-T variant. */
8717 static void
8718 build_vtt_inits (tree binfo, tree t, vec<constructor_elt, va_gc> **inits,
8719 tree *index)
8721 int i;
8722 tree b;
8723 tree init;
8724 secondary_vptr_vtt_init_data data;
8725 int top_level_p = SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), t);
8727 /* We only need VTTs for subobjects with virtual bases. */
8728 if (!CLASSTYPE_VBASECLASSES (BINFO_TYPE (binfo)))
8729 return;
8731 /* We need to use a construction vtable if this is not the primary
8732 VTT. */
8733 if (!top_level_p)
8735 build_ctor_vtbl_group (binfo, t);
8737 /* Record the offset in the VTT where this sub-VTT can be found. */
8738 BINFO_SUBVTT_INDEX (binfo) = *index;
8741 /* Add the address of the primary vtable for the complete object. */
8742 init = binfo_ctor_vtable (binfo);
8743 CONSTRUCTOR_APPEND_ELT (*inits, NULL_TREE, init);
8744 if (top_level_p)
8746 gcc_assert (!BINFO_VPTR_INDEX (binfo));
8747 BINFO_VPTR_INDEX (binfo) = *index;
8749 *index = size_binop (PLUS_EXPR, *index, TYPE_SIZE_UNIT (ptr_type_node));
8751 /* Recursively add the secondary VTTs for non-virtual bases. */
8752 for (i = 0; BINFO_BASE_ITERATE (binfo, i, b); ++i)
8753 if (!BINFO_VIRTUAL_P (b))
8754 build_vtt_inits (b, t, inits, index);
8756 /* Add secondary virtual pointers for all subobjects of BINFO with
8757 either virtual bases or reachable along a virtual path, except
8758 subobjects that are non-virtual primary bases. */
8759 data.top_level_p = top_level_p;
8760 data.index = *index;
8761 data.inits = *inits;
8762 data.type_being_constructed = BINFO_TYPE (binfo);
8764 dfs_walk_once (binfo, dfs_build_secondary_vptr_vtt_inits, NULL, &data);
8766 *index = data.index;
8768 /* data.inits might have grown as we added secondary virtual pointers.
8769 Make sure our caller knows about the new vector. */
8770 *inits = data.inits;
8772 if (top_level_p)
8773 /* Add the secondary VTTs for virtual bases in inheritance graph
8774 order. */
8775 for (b = TYPE_BINFO (BINFO_TYPE (binfo)); b; b = TREE_CHAIN (b))
8777 if (!BINFO_VIRTUAL_P (b))
8778 continue;
8780 build_vtt_inits (b, t, inits, index);
8782 else
8783 /* Remove the ctor vtables we created. */
8784 dfs_walk_all (binfo, dfs_fixup_binfo_vtbls, NULL, binfo);
8787 /* Called from build_vtt_inits via dfs_walk. BINFO is the binfo for the base
8788 in most derived. DATA is a SECONDARY_VPTR_VTT_INIT_DATA structure. */
8790 static tree
8791 dfs_build_secondary_vptr_vtt_inits (tree binfo, void *data_)
8793 secondary_vptr_vtt_init_data *data = (secondary_vptr_vtt_init_data *)data_;
8795 /* We don't care about bases that don't have vtables. */
8796 if (!TYPE_VFIELD (BINFO_TYPE (binfo)))
8797 return dfs_skip_bases;
8799 /* We're only interested in proper subobjects of the type being
8800 constructed. */
8801 if (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), data->type_being_constructed))
8802 return NULL_TREE;
8804 /* We're only interested in bases with virtual bases or reachable
8805 via a virtual path from the type being constructed. */
8806 if (!(CLASSTYPE_VBASECLASSES (BINFO_TYPE (binfo))
8807 || binfo_via_virtual (binfo, data->type_being_constructed)))
8808 return dfs_skip_bases;
8810 /* We're not interested in non-virtual primary bases. */
8811 if (!BINFO_VIRTUAL_P (binfo) && BINFO_PRIMARY_P (binfo))
8812 return NULL_TREE;
8814 /* Record the index where this secondary vptr can be found. */
8815 if (data->top_level_p)
8817 gcc_assert (!BINFO_VPTR_INDEX (binfo));
8818 BINFO_VPTR_INDEX (binfo) = data->index;
8820 if (BINFO_VIRTUAL_P (binfo))
8822 /* It's a primary virtual base, and this is not a
8823 construction vtable. Find the base this is primary of in
8824 the inheritance graph, and use that base's vtable
8825 now. */
8826 while (BINFO_PRIMARY_P (binfo))
8827 binfo = BINFO_INHERITANCE_CHAIN (binfo);
8831 /* Add the initializer for the secondary vptr itself. */
8832 CONSTRUCTOR_APPEND_ELT (data->inits, NULL_TREE, binfo_ctor_vtable (binfo));
8834 /* Advance the vtt index. */
8835 data->index = size_binop (PLUS_EXPR, data->index,
8836 TYPE_SIZE_UNIT (ptr_type_node));
8838 return NULL_TREE;
8841 /* Called from build_vtt_inits via dfs_walk. After building
8842 constructor vtables and generating the sub-vtt from them, we need
8843 to restore the BINFO_VTABLES that were scribbled on. DATA is the
8844 binfo of the base whose sub vtt was generated. */
8846 static tree
8847 dfs_fixup_binfo_vtbls (tree binfo, void* data)
8849 tree vtable = BINFO_VTABLE (binfo);
8851 if (!TYPE_CONTAINS_VPTR_P (BINFO_TYPE (binfo)))
8852 /* If this class has no vtable, none of its bases do. */
8853 return dfs_skip_bases;
8855 if (!vtable)
8856 /* This might be a primary base, so have no vtable in this
8857 hierarchy. */
8858 return NULL_TREE;
8860 /* If we scribbled the construction vtable vptr into BINFO, clear it
8861 out now. */
8862 if (TREE_CODE (vtable) == TREE_LIST
8863 && (TREE_PURPOSE (vtable) == (tree) data))
8864 BINFO_VTABLE (binfo) = TREE_CHAIN (vtable);
8866 return NULL_TREE;
8869 /* Build the construction vtable group for BINFO which is in the
8870 hierarchy dominated by T. */
8872 static void
8873 build_ctor_vtbl_group (tree binfo, tree t)
8875 tree type;
8876 tree vtbl;
8877 tree id;
8878 tree vbase;
8879 vec<constructor_elt, va_gc> *v;
8881 /* See if we've already created this construction vtable group. */
8882 id = mangle_ctor_vtbl_for_type (t, binfo);
8883 if (get_global_binding (id))
8884 return;
8886 gcc_assert (!SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), t));
8887 /* Build a version of VTBL (with the wrong type) for use in
8888 constructing the addresses of secondary vtables in the
8889 construction vtable group. */
8890 vtbl = build_vtable (t, id, ptr_type_node);
8891 DECL_CONSTRUCTION_VTABLE_P (vtbl) = 1;
8892 /* Don't export construction vtables from shared libraries. Even on
8893 targets that don't support hidden visibility, this tells
8894 can_refer_decl_in_current_unit_p not to assume that it's safe to
8895 access from a different compilation unit (bz 54314). */
8896 DECL_VISIBILITY (vtbl) = VISIBILITY_HIDDEN;
8897 DECL_VISIBILITY_SPECIFIED (vtbl) = true;
8899 v = NULL;
8900 accumulate_vtbl_inits (binfo, TYPE_BINFO (TREE_TYPE (binfo)),
8901 binfo, vtbl, t, &v);
8903 /* Add the vtables for each of our virtual bases using the vbase in T
8904 binfo. */
8905 for (vbase = TYPE_BINFO (BINFO_TYPE (binfo));
8906 vbase;
8907 vbase = TREE_CHAIN (vbase))
8909 tree b;
8911 if (!BINFO_VIRTUAL_P (vbase))
8912 continue;
8913 b = copied_binfo (vbase, binfo);
8915 accumulate_vtbl_inits (b, vbase, binfo, vtbl, t, &v);
8918 /* Figure out the type of the construction vtable. */
8919 type = build_array_of_n_type (vtable_entry_type, v->length ());
8920 layout_type (type);
8921 TREE_TYPE (vtbl) = type;
8922 DECL_SIZE (vtbl) = DECL_SIZE_UNIT (vtbl) = NULL_TREE;
8923 layout_decl (vtbl, 0);
8925 /* Initialize the construction vtable. */
8926 CLASSTYPE_VTABLES (t) = chainon (CLASSTYPE_VTABLES (t), vtbl);
8927 initialize_artificial_var (vtbl, v);
8928 dump_vtable (t, binfo, vtbl);
8931 /* Add the vtbl initializers for BINFO (and its bases other than
8932 non-virtual primaries) to the list of INITS. BINFO is in the
8933 hierarchy dominated by T. RTTI_BINFO is the binfo within T of
8934 the constructor the vtbl inits should be accumulated for. (If this
8935 is the complete object vtbl then RTTI_BINFO will be TYPE_BINFO (T).)
8936 ORIG_BINFO is the binfo for this object within BINFO_TYPE (RTTI_BINFO).
8937 BINFO is the active base equivalent of ORIG_BINFO in the inheritance
8938 graph of T. Both BINFO and ORIG_BINFO will have the same BINFO_TYPE,
8939 but are not necessarily the same in terms of layout. */
8941 static void
8942 accumulate_vtbl_inits (tree binfo,
8943 tree orig_binfo,
8944 tree rtti_binfo,
8945 tree vtbl,
8946 tree t,
8947 vec<constructor_elt, va_gc> **inits)
8949 int i;
8950 tree base_binfo;
8951 int ctor_vtbl_p = !SAME_BINFO_TYPE_P (BINFO_TYPE (rtti_binfo), t);
8953 gcc_assert (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), BINFO_TYPE (orig_binfo)));
8955 /* If it doesn't have a vptr, we don't do anything. */
8956 if (!TYPE_CONTAINS_VPTR_P (BINFO_TYPE (binfo)))
8957 return;
8959 /* If we're building a construction vtable, we're not interested in
8960 subobjects that don't require construction vtables. */
8961 if (ctor_vtbl_p
8962 && !CLASSTYPE_VBASECLASSES (BINFO_TYPE (binfo))
8963 && !binfo_via_virtual (orig_binfo, BINFO_TYPE (rtti_binfo)))
8964 return;
8966 /* Build the initializers for the BINFO-in-T vtable. */
8967 dfs_accumulate_vtbl_inits (binfo, orig_binfo, rtti_binfo, vtbl, t, inits);
8969 /* Walk the BINFO and its bases. We walk in preorder so that as we
8970 initialize each vtable we can figure out at what offset the
8971 secondary vtable lies from the primary vtable. We can't use
8972 dfs_walk here because we need to iterate through bases of BINFO
8973 and RTTI_BINFO simultaneously. */
8974 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
8976 /* Skip virtual bases. */
8977 if (BINFO_VIRTUAL_P (base_binfo))
8978 continue;
8979 accumulate_vtbl_inits (base_binfo,
8980 BINFO_BASE_BINFO (orig_binfo, i),
8981 rtti_binfo, vtbl, t,
8982 inits);
8986 /* Called from accumulate_vtbl_inits. Adds the initializers for the
8987 BINFO vtable to L. */
8989 static void
8990 dfs_accumulate_vtbl_inits (tree binfo,
8991 tree orig_binfo,
8992 tree rtti_binfo,
8993 tree orig_vtbl,
8994 tree t,
8995 vec<constructor_elt, va_gc> **l)
8997 tree vtbl = NULL_TREE;
8998 int ctor_vtbl_p = !SAME_BINFO_TYPE_P (BINFO_TYPE (rtti_binfo), t);
8999 int n_inits;
9001 if (ctor_vtbl_p
9002 && BINFO_VIRTUAL_P (orig_binfo) && BINFO_PRIMARY_P (orig_binfo))
9004 /* In the hierarchy of BINFO_TYPE (RTTI_BINFO), this is a
9005 primary virtual base. If it is not the same primary in
9006 the hierarchy of T, we'll need to generate a ctor vtable
9007 for it, to place at its location in T. If it is the same
9008 primary, we still need a VTT entry for the vtable, but it
9009 should point to the ctor vtable for the base it is a
9010 primary for within the sub-hierarchy of RTTI_BINFO.
9012 There are three possible cases:
9014 1) We are in the same place.
9015 2) We are a primary base within a lost primary virtual base of
9016 RTTI_BINFO.
9017 3) We are primary to something not a base of RTTI_BINFO. */
9019 tree b;
9020 tree last = NULL_TREE;
9022 /* First, look through the bases we are primary to for RTTI_BINFO
9023 or a virtual base. */
9024 b = binfo;
9025 while (BINFO_PRIMARY_P (b))
9027 b = BINFO_INHERITANCE_CHAIN (b);
9028 last = b;
9029 if (BINFO_VIRTUAL_P (b) || b == rtti_binfo)
9030 goto found;
9032 /* If we run out of primary links, keep looking down our
9033 inheritance chain; we might be an indirect primary. */
9034 for (b = last; b; b = BINFO_INHERITANCE_CHAIN (b))
9035 if (BINFO_VIRTUAL_P (b) || b == rtti_binfo)
9036 break;
9037 found:
9039 /* If we found RTTI_BINFO, this is case 1. If we found a virtual
9040 base B and it is a base of RTTI_BINFO, this is case 2. In
9041 either case, we share our vtable with LAST, i.e. the
9042 derived-most base within B of which we are a primary. */
9043 if (b == rtti_binfo
9044 || (b && binfo_for_vbase (BINFO_TYPE (b), BINFO_TYPE (rtti_binfo))))
9045 /* Just set our BINFO_VTABLE to point to LAST, as we may not have
9046 set LAST's BINFO_VTABLE yet. We'll extract the actual vptr in
9047 binfo_ctor_vtable after everything's been set up. */
9048 vtbl = last;
9050 /* Otherwise, this is case 3 and we get our own. */
9052 else if (!BINFO_NEW_VTABLE_MARKED (orig_binfo))
9053 return;
9055 n_inits = vec_safe_length (*l);
9057 if (!vtbl)
9059 tree index;
9060 int non_fn_entries;
9062 /* Add the initializer for this vtable. */
9063 build_vtbl_initializer (binfo, orig_binfo, t, rtti_binfo,
9064 &non_fn_entries, l);
9066 /* Figure out the position to which the VPTR should point. */
9067 vtbl = build1 (ADDR_EXPR, vtbl_ptr_type_node, orig_vtbl);
9068 index = size_binop (MULT_EXPR,
9069 TYPE_SIZE_UNIT (vtable_entry_type),
9070 size_int (non_fn_entries + n_inits));
9071 vtbl = fold_build_pointer_plus (vtbl, index);
9074 if (ctor_vtbl_p)
9075 /* For a construction vtable, we can't overwrite BINFO_VTABLE.
9076 So, we make a TREE_LIST. Later, dfs_fixup_binfo_vtbls will
9077 straighten this out. */
9078 BINFO_VTABLE (binfo) = tree_cons (rtti_binfo, vtbl, BINFO_VTABLE (binfo));
9079 else if (BINFO_PRIMARY_P (binfo) && BINFO_VIRTUAL_P (binfo))
9080 /* Throw away any unneeded intializers. */
9081 (*l)->truncate (n_inits);
9082 else
9083 /* For an ordinary vtable, set BINFO_VTABLE. */
9084 BINFO_VTABLE (binfo) = vtbl;
9087 static GTY(()) tree abort_fndecl_addr;
9088 static GTY(()) tree dvirt_fn;
9090 /* Construct the initializer for BINFO's virtual function table. BINFO
9091 is part of the hierarchy dominated by T. If we're building a
9092 construction vtable, the ORIG_BINFO is the binfo we should use to
9093 find the actual function pointers to put in the vtable - but they
9094 can be overridden on the path to most-derived in the graph that
9095 ORIG_BINFO belongs. Otherwise,
9096 ORIG_BINFO should be the same as BINFO. The RTTI_BINFO is the
9097 BINFO that should be indicated by the RTTI information in the
9098 vtable; it will be a base class of T, rather than T itself, if we
9099 are building a construction vtable.
9101 The value returned is a TREE_LIST suitable for wrapping in a
9102 CONSTRUCTOR to use as the DECL_INITIAL for a vtable. If
9103 NON_FN_ENTRIES_P is not NULL, *NON_FN_ENTRIES_P is set to the
9104 number of non-function entries in the vtable.
9106 It might seem that this function should never be called with a
9107 BINFO for which BINFO_PRIMARY_P holds, the vtable for such a
9108 base is always subsumed by a derived class vtable. However, when
9109 we are building construction vtables, we do build vtables for
9110 primary bases; we need these while the primary base is being
9111 constructed. */
9113 static void
9114 build_vtbl_initializer (tree binfo,
9115 tree orig_binfo,
9116 tree t,
9117 tree rtti_binfo,
9118 int* non_fn_entries_p,
9119 vec<constructor_elt, va_gc> **inits)
9121 tree v;
9122 vtbl_init_data vid;
9123 unsigned ix, jx;
9124 tree vbinfo;
9125 vec<tree, va_gc> *vbases;
9126 constructor_elt *e;
9128 /* Initialize VID. */
9129 memset (&vid, 0, sizeof (vid));
9130 vid.binfo = binfo;
9131 vid.derived = t;
9132 vid.rtti_binfo = rtti_binfo;
9133 vid.primary_vtbl_p = SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), t);
9134 vid.ctor_vtbl_p = !SAME_BINFO_TYPE_P (BINFO_TYPE (rtti_binfo), t);
9135 vid.generate_vcall_entries = true;
9136 /* The first vbase or vcall offset is at index -3 in the vtable. */
9137 vid.index = ssize_int(-3 * TARGET_VTABLE_DATA_ENTRY_DISTANCE);
9139 /* Add entries to the vtable for RTTI. */
9140 build_rtti_vtbl_entries (binfo, &vid);
9142 /* Create an array for keeping track of the functions we've
9143 processed. When we see multiple functions with the same
9144 signature, we share the vcall offsets. */
9145 vec_alloc (vid.fns, 32);
9146 /* Add the vcall and vbase offset entries. */
9147 build_vcall_and_vbase_vtbl_entries (binfo, &vid);
9149 /* Clear BINFO_VTABLE_PATH_MARKED; it's set by
9150 build_vbase_offset_vtbl_entries. */
9151 for (vbases = CLASSTYPE_VBASECLASSES (t), ix = 0;
9152 vec_safe_iterate (vbases, ix, &vbinfo); ix++)
9153 BINFO_VTABLE_PATH_MARKED (vbinfo) = 0;
9155 /* If the target requires padding between data entries, add that now. */
9156 if (TARGET_VTABLE_DATA_ENTRY_DISTANCE > 1)
9158 int n_entries = vec_safe_length (vid.inits);
9160 vec_safe_grow (vid.inits, TARGET_VTABLE_DATA_ENTRY_DISTANCE * n_entries);
9162 /* Move data entries into their new positions and add padding
9163 after the new positions. Iterate backwards so we don't
9164 overwrite entries that we would need to process later. */
9165 for (ix = n_entries - 1;
9166 vid.inits->iterate (ix, &e);
9167 ix--)
9169 int j;
9170 int new_position = (TARGET_VTABLE_DATA_ENTRY_DISTANCE * ix
9171 + (TARGET_VTABLE_DATA_ENTRY_DISTANCE - 1));
9173 (*vid.inits)[new_position] = *e;
9175 for (j = 1; j < TARGET_VTABLE_DATA_ENTRY_DISTANCE; ++j)
9177 constructor_elt *f = &(*vid.inits)[new_position - j];
9178 f->index = NULL_TREE;
9179 f->value = build1 (NOP_EXPR, vtable_entry_type,
9180 null_pointer_node);
9185 if (non_fn_entries_p)
9186 *non_fn_entries_p = vec_safe_length (vid.inits);
9188 /* The initializers for virtual functions were built up in reverse
9189 order. Straighten them out and add them to the running list in one
9190 step. */
9191 jx = vec_safe_length (*inits);
9192 vec_safe_grow (*inits, jx + vid.inits->length ());
9194 for (ix = vid.inits->length () - 1;
9195 vid.inits->iterate (ix, &e);
9196 ix--, jx++)
9197 (**inits)[jx] = *e;
9199 /* Go through all the ordinary virtual functions, building up
9200 initializers. */
9201 for (v = BINFO_VIRTUALS (orig_binfo); v; v = TREE_CHAIN (v))
9203 tree delta;
9204 tree vcall_index;
9205 tree fn, fn_original;
9206 tree init = NULL_TREE;
9208 fn = BV_FN (v);
9209 fn_original = fn;
9210 if (DECL_THUNK_P (fn))
9212 if (!DECL_NAME (fn))
9213 finish_thunk (fn);
9214 if (THUNK_ALIAS (fn))
9216 fn = THUNK_ALIAS (fn);
9217 BV_FN (v) = fn;
9219 fn_original = THUNK_TARGET (fn);
9222 /* If the only definition of this function signature along our
9223 primary base chain is from a lost primary, this vtable slot will
9224 never be used, so just zero it out. This is important to avoid
9225 requiring extra thunks which cannot be generated with the function.
9227 We first check this in update_vtable_entry_for_fn, so we handle
9228 restored primary bases properly; we also need to do it here so we
9229 zero out unused slots in ctor vtables, rather than filling them
9230 with erroneous values (though harmless, apart from relocation
9231 costs). */
9232 if (BV_LOST_PRIMARY (v))
9233 init = size_zero_node;
9235 if (! init)
9237 /* Pull the offset for `this', and the function to call, out of
9238 the list. */
9239 delta = BV_DELTA (v);
9240 vcall_index = BV_VCALL_INDEX (v);
9242 gcc_assert (TREE_CODE (delta) == INTEGER_CST);
9243 gcc_assert (TREE_CODE (fn) == FUNCTION_DECL);
9245 /* You can't call an abstract virtual function; it's abstract.
9246 So, we replace these functions with __pure_virtual. */
9247 if (DECL_PURE_VIRTUAL_P (fn_original))
9249 fn = abort_fndecl;
9250 if (!TARGET_VTABLE_USES_DESCRIPTORS)
9252 if (abort_fndecl_addr == NULL)
9253 abort_fndecl_addr
9254 = fold_convert (vfunc_ptr_type_node,
9255 build_fold_addr_expr (fn));
9256 init = abort_fndecl_addr;
9259 /* Likewise for deleted virtuals. */
9260 else if (DECL_DELETED_FN (fn_original))
9262 if (!dvirt_fn)
9264 tree name = get_identifier ("__cxa_deleted_virtual");
9265 dvirt_fn = get_global_binding (name);
9266 if (!dvirt_fn)
9267 dvirt_fn = push_library_fn
9268 (name,
9269 build_function_type_list (void_type_node, NULL_TREE),
9270 NULL_TREE, ECF_NORETURN | ECF_COLD);
9272 fn = dvirt_fn;
9273 if (!TARGET_VTABLE_USES_DESCRIPTORS)
9274 init = fold_convert (vfunc_ptr_type_node,
9275 build_fold_addr_expr (fn));
9277 else
9279 if (!integer_zerop (delta) || vcall_index)
9281 fn = make_thunk (fn, /*this_adjusting=*/1,
9282 delta, vcall_index);
9283 if (!DECL_NAME (fn))
9284 finish_thunk (fn);
9286 /* Take the address of the function, considering it to be of an
9287 appropriate generic type. */
9288 if (!TARGET_VTABLE_USES_DESCRIPTORS)
9289 init = fold_convert (vfunc_ptr_type_node,
9290 build_fold_addr_expr (fn));
9291 /* Don't refer to a virtual destructor from a constructor
9292 vtable or a vtable for an abstract class, since destroying
9293 an object under construction is undefined behavior and we
9294 don't want it to be considered a candidate for speculative
9295 devirtualization. But do create the thunk for ABI
9296 compliance. */
9297 if (DECL_DESTRUCTOR_P (fn_original)
9298 && (CLASSTYPE_PURE_VIRTUALS (DECL_CONTEXT (fn_original))
9299 || orig_binfo != binfo))
9300 init = size_zero_node;
9304 /* And add it to the chain of initializers. */
9305 if (TARGET_VTABLE_USES_DESCRIPTORS)
9307 int i;
9308 if (init == size_zero_node)
9309 for (i = 0; i < TARGET_VTABLE_USES_DESCRIPTORS; ++i)
9310 CONSTRUCTOR_APPEND_ELT (*inits, NULL_TREE, init);
9311 else
9312 for (i = 0; i < TARGET_VTABLE_USES_DESCRIPTORS; ++i)
9314 tree fdesc = build2 (FDESC_EXPR, vfunc_ptr_type_node,
9315 fn, build_int_cst (NULL_TREE, i));
9316 TREE_CONSTANT (fdesc) = 1;
9318 CONSTRUCTOR_APPEND_ELT (*inits, NULL_TREE, fdesc);
9321 else
9322 CONSTRUCTOR_APPEND_ELT (*inits, NULL_TREE, init);
9326 /* Adds to vid->inits the initializers for the vbase and vcall
9327 offsets in BINFO, which is in the hierarchy dominated by T. */
9329 static void
9330 build_vcall_and_vbase_vtbl_entries (tree binfo, vtbl_init_data* vid)
9332 tree b;
9334 /* If this is a derived class, we must first create entries
9335 corresponding to the primary base class. */
9336 b = get_primary_binfo (binfo);
9337 if (b)
9338 build_vcall_and_vbase_vtbl_entries (b, vid);
9340 /* Add the vbase entries for this base. */
9341 build_vbase_offset_vtbl_entries (binfo, vid);
9342 /* Add the vcall entries for this base. */
9343 build_vcall_offset_vtbl_entries (binfo, vid);
9346 /* Returns the initializers for the vbase offset entries in the vtable
9347 for BINFO (which is part of the class hierarchy dominated by T), in
9348 reverse order. VBASE_OFFSET_INDEX gives the vtable index
9349 where the next vbase offset will go. */
9351 static void
9352 build_vbase_offset_vtbl_entries (tree binfo, vtbl_init_data* vid)
9354 tree vbase;
9355 tree t;
9356 tree non_primary_binfo;
9358 /* If there are no virtual baseclasses, then there is nothing to
9359 do. */
9360 if (!CLASSTYPE_VBASECLASSES (BINFO_TYPE (binfo)))
9361 return;
9363 t = vid->derived;
9365 /* We might be a primary base class. Go up the inheritance hierarchy
9366 until we find the most derived class of which we are a primary base:
9367 it is the offset of that which we need to use. */
9368 non_primary_binfo = binfo;
9369 while (BINFO_INHERITANCE_CHAIN (non_primary_binfo))
9371 tree b;
9373 /* If we have reached a virtual base, then it must be a primary
9374 base (possibly multi-level) of vid->binfo, or we wouldn't
9375 have called build_vcall_and_vbase_vtbl_entries for it. But it
9376 might be a lost primary, so just skip down to vid->binfo. */
9377 if (BINFO_VIRTUAL_P (non_primary_binfo))
9379 non_primary_binfo = vid->binfo;
9380 break;
9383 b = BINFO_INHERITANCE_CHAIN (non_primary_binfo);
9384 if (get_primary_binfo (b) != non_primary_binfo)
9385 break;
9386 non_primary_binfo = b;
9389 /* Go through the virtual bases, adding the offsets. */
9390 for (vbase = TYPE_BINFO (BINFO_TYPE (binfo));
9391 vbase;
9392 vbase = TREE_CHAIN (vbase))
9394 tree b;
9395 tree delta;
9397 if (!BINFO_VIRTUAL_P (vbase))
9398 continue;
9400 /* Find the instance of this virtual base in the complete
9401 object. */
9402 b = copied_binfo (vbase, binfo);
9404 /* If we've already got an offset for this virtual base, we
9405 don't need another one. */
9406 if (BINFO_VTABLE_PATH_MARKED (b))
9407 continue;
9408 BINFO_VTABLE_PATH_MARKED (b) = 1;
9410 /* Figure out where we can find this vbase offset. */
9411 delta = size_binop (MULT_EXPR,
9412 vid->index,
9413 fold_convert (ssizetype,
9414 TYPE_SIZE_UNIT (vtable_entry_type)));
9415 if (vid->primary_vtbl_p)
9416 BINFO_VPTR_FIELD (b) = delta;
9418 if (binfo != TYPE_BINFO (t))
9419 /* The vbase offset had better be the same. */
9420 gcc_assert (tree_int_cst_equal (delta, BINFO_VPTR_FIELD (vbase)));
9422 /* The next vbase will come at a more negative offset. */
9423 vid->index = size_binop (MINUS_EXPR, vid->index,
9424 ssize_int (TARGET_VTABLE_DATA_ENTRY_DISTANCE));
9426 /* The initializer is the delta from BINFO to this virtual base.
9427 The vbase offsets go in reverse inheritance-graph order, and
9428 we are walking in inheritance graph order so these end up in
9429 the right order. */
9430 delta = size_diffop_loc (input_location,
9431 BINFO_OFFSET (b), BINFO_OFFSET (non_primary_binfo));
9433 CONSTRUCTOR_APPEND_ELT (vid->inits, NULL_TREE,
9434 fold_build1_loc (input_location, NOP_EXPR,
9435 vtable_entry_type, delta));
9439 /* Adds the initializers for the vcall offset entries in the vtable
9440 for BINFO (which is part of the class hierarchy dominated by VID->DERIVED)
9441 to VID->INITS. */
9443 static void
9444 build_vcall_offset_vtbl_entries (tree binfo, vtbl_init_data* vid)
9446 /* We only need these entries if this base is a virtual base. We
9447 compute the indices -- but do not add to the vtable -- when
9448 building the main vtable for a class. */
9449 if (binfo == TYPE_BINFO (vid->derived)
9450 || (BINFO_VIRTUAL_P (binfo)
9451 /* If BINFO is RTTI_BINFO, then (since BINFO does not
9452 correspond to VID->DERIVED), we are building a primary
9453 construction virtual table. Since this is a primary
9454 virtual table, we do not need the vcall offsets for
9455 BINFO. */
9456 && binfo != vid->rtti_binfo))
9458 /* We need a vcall offset for each of the virtual functions in this
9459 vtable. For example:
9461 class A { virtual void f (); };
9462 class B1 : virtual public A { virtual void f (); };
9463 class B2 : virtual public A { virtual void f (); };
9464 class C: public B1, public B2 { virtual void f (); };
9466 A C object has a primary base of B1, which has a primary base of A. A
9467 C also has a secondary base of B2, which no longer has a primary base
9468 of A. So the B2-in-C construction vtable needs a secondary vtable for
9469 A, which will adjust the A* to a B2* to call f. We have no way of
9470 knowing what (or even whether) this offset will be when we define B2,
9471 so we store this "vcall offset" in the A sub-vtable and look it up in
9472 a "virtual thunk" for B2::f.
9474 We need entries for all the functions in our primary vtable and
9475 in our non-virtual bases' secondary vtables. */
9476 vid->vbase = binfo;
9477 /* If we are just computing the vcall indices -- but do not need
9478 the actual entries -- not that. */
9479 if (!BINFO_VIRTUAL_P (binfo))
9480 vid->generate_vcall_entries = false;
9481 /* Now, walk through the non-virtual bases, adding vcall offsets. */
9482 add_vcall_offset_vtbl_entries_r (binfo, vid);
9486 /* Build vcall offsets, starting with those for BINFO. */
9488 static void
9489 add_vcall_offset_vtbl_entries_r (tree binfo, vtbl_init_data* vid)
9491 int i;
9492 tree primary_binfo;
9493 tree base_binfo;
9495 /* Don't walk into virtual bases -- except, of course, for the
9496 virtual base for which we are building vcall offsets. Any
9497 primary virtual base will have already had its offsets generated
9498 through the recursion in build_vcall_and_vbase_vtbl_entries. */
9499 if (BINFO_VIRTUAL_P (binfo) && vid->vbase != binfo)
9500 return;
9502 /* If BINFO has a primary base, process it first. */
9503 primary_binfo = get_primary_binfo (binfo);
9504 if (primary_binfo)
9505 add_vcall_offset_vtbl_entries_r (primary_binfo, vid);
9507 /* Add BINFO itself to the list. */
9508 add_vcall_offset_vtbl_entries_1 (binfo, vid);
9510 /* Scan the non-primary bases of BINFO. */
9511 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
9512 if (base_binfo != primary_binfo)
9513 add_vcall_offset_vtbl_entries_r (base_binfo, vid);
9516 /* Called from build_vcall_offset_vtbl_entries_r. */
9518 static void
9519 add_vcall_offset_vtbl_entries_1 (tree binfo, vtbl_init_data* vid)
9521 /* Make entries for the rest of the virtuals. */
9522 tree orig_fn;
9524 /* The ABI requires that the methods be processed in declaration
9525 order. */
9526 for (orig_fn = TYPE_FIELDS (BINFO_TYPE (binfo));
9527 orig_fn;
9528 orig_fn = DECL_CHAIN (orig_fn))
9529 if (TREE_CODE (orig_fn) == FUNCTION_DECL && DECL_VINDEX (orig_fn))
9530 add_vcall_offset (orig_fn, binfo, vid);
9533 /* Add a vcall offset entry for ORIG_FN to the vtable. */
9535 static void
9536 add_vcall_offset (tree orig_fn, tree binfo, vtbl_init_data *vid)
9538 size_t i;
9539 tree vcall_offset;
9540 tree derived_entry;
9542 /* If there is already an entry for a function with the same
9543 signature as FN, then we do not need a second vcall offset.
9544 Check the list of functions already present in the derived
9545 class vtable. */
9546 FOR_EACH_VEC_SAFE_ELT (vid->fns, i, derived_entry)
9548 if (same_signature_p (derived_entry, orig_fn)
9549 /* We only use one vcall offset for virtual destructors,
9550 even though there are two virtual table entries. */
9551 || (DECL_DESTRUCTOR_P (derived_entry)
9552 && DECL_DESTRUCTOR_P (orig_fn)))
9553 return;
9556 /* If we are building these vcall offsets as part of building
9557 the vtable for the most derived class, remember the vcall
9558 offset. */
9559 if (vid->binfo == TYPE_BINFO (vid->derived))
9561 tree_pair_s elt = {orig_fn, vid->index};
9562 vec_safe_push (CLASSTYPE_VCALL_INDICES (vid->derived), elt);
9565 /* The next vcall offset will be found at a more negative
9566 offset. */
9567 vid->index = size_binop (MINUS_EXPR, vid->index,
9568 ssize_int (TARGET_VTABLE_DATA_ENTRY_DISTANCE));
9570 /* Keep track of this function. */
9571 vec_safe_push (vid->fns, orig_fn);
9573 if (vid->generate_vcall_entries)
9575 tree base;
9576 tree fn;
9578 /* Find the overriding function. */
9579 fn = find_final_overrider (vid->rtti_binfo, binfo, orig_fn);
9580 if (fn == error_mark_node)
9581 vcall_offset = build_zero_cst (vtable_entry_type);
9582 else
9584 base = TREE_VALUE (fn);
9586 /* The vbase we're working on is a primary base of
9587 vid->binfo. But it might be a lost primary, so its
9588 BINFO_OFFSET might be wrong, so we just use the
9589 BINFO_OFFSET from vid->binfo. */
9590 vcall_offset = size_diffop_loc (input_location,
9591 BINFO_OFFSET (base),
9592 BINFO_OFFSET (vid->binfo));
9593 vcall_offset = fold_build1_loc (input_location,
9594 NOP_EXPR, vtable_entry_type,
9595 vcall_offset);
9597 /* Add the initializer to the vtable. */
9598 CONSTRUCTOR_APPEND_ELT (vid->inits, NULL_TREE, vcall_offset);
9602 /* Return vtbl initializers for the RTTI entries corresponding to the
9603 BINFO's vtable. The RTTI entries should indicate the object given
9604 by VID->rtti_binfo. */
9606 static void
9607 build_rtti_vtbl_entries (tree binfo, vtbl_init_data* vid)
9609 tree b;
9610 tree t;
9611 tree offset;
9612 tree decl;
9613 tree init;
9615 t = BINFO_TYPE (vid->rtti_binfo);
9617 /* To find the complete object, we will first convert to our most
9618 primary base, and then add the offset in the vtbl to that value. */
9619 b = most_primary_binfo (binfo);
9620 offset = size_diffop_loc (input_location,
9621 BINFO_OFFSET (vid->rtti_binfo), BINFO_OFFSET (b));
9623 /* The second entry is the address of the typeinfo object. */
9624 if (flag_rtti)
9625 decl = build_address (get_tinfo_decl (t));
9626 else
9627 decl = integer_zero_node;
9629 /* Convert the declaration to a type that can be stored in the
9630 vtable. */
9631 init = build_nop (vfunc_ptr_type_node, decl);
9632 CONSTRUCTOR_APPEND_ELT (vid->inits, NULL_TREE, init);
9634 /* Add the offset-to-top entry. It comes earlier in the vtable than
9635 the typeinfo entry. Convert the offset to look like a
9636 function pointer, so that we can put it in the vtable. */
9637 init = build_nop (vfunc_ptr_type_node, offset);
9638 CONSTRUCTOR_APPEND_ELT (vid->inits, NULL_TREE, init);
9641 /* TRUE iff TYPE is uniquely derived from PARENT. Ignores
9642 accessibility. */
9644 bool
9645 uniquely_derived_from_p (tree parent, tree type)
9647 tree base = lookup_base (type, parent, ba_unique, NULL, tf_none);
9648 return base && base != error_mark_node;
9651 /* TRUE iff TYPE is publicly & uniquely derived from PARENT. */
9653 bool
9654 publicly_uniquely_derived_p (tree parent, tree type)
9656 tree base = lookup_base (type, parent, ba_ignore_scope | ba_check,
9657 NULL, tf_none);
9658 return base && base != error_mark_node;
9661 /* CTX1 and CTX2 are declaration contexts. Return the innermost common
9662 class between them, if any. */
9664 tree
9665 common_enclosing_class (tree ctx1, tree ctx2)
9667 if (!TYPE_P (ctx1) || !TYPE_P (ctx2))
9668 return NULL_TREE;
9669 gcc_assert (ctx1 == TYPE_MAIN_VARIANT (ctx1)
9670 && ctx2 == TYPE_MAIN_VARIANT (ctx2));
9671 if (ctx1 == ctx2)
9672 return ctx1;
9673 for (tree t = ctx1; TYPE_P (t); t = TYPE_CONTEXT (t))
9674 TYPE_MARKED_P (t) = true;
9675 tree found = NULL_TREE;
9676 for (tree t = ctx2; TYPE_P (t); t = TYPE_CONTEXT (t))
9677 if (TYPE_MARKED_P (t))
9679 found = t;
9680 break;
9682 for (tree t = ctx1; TYPE_P (t); t = TYPE_CONTEXT (t))
9683 TYPE_MARKED_P (t) = false;
9684 return found;
9687 #include "gt-cp-class.h"