Relocation (= move+destroy)
[official-gcc.git] / gcc / cp / class.c
blob1789d1ecb70ad34a9637fc5198a3169af5bb533e
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 void build_vtbl_initializer (tree, tree, tree, tree, int *,
137 vec<constructor_elt, va_gc> **);
138 static bool check_bitfield_decl (tree);
139 static bool check_field_decl (tree, tree, int *, int *);
140 static void check_field_decls (tree, tree *, int *, int *);
141 static tree *build_base_field (record_layout_info, tree, splay_tree, tree *);
142 static void build_base_fields (record_layout_info, splay_tree, tree *);
143 static void check_methods (tree);
144 static void remove_zero_width_bit_fields (tree);
145 static bool accessible_nvdtor_p (tree);
147 /* Used by find_flexarrays and related functions. */
148 struct flexmems_t;
149 static void diagnose_flexarrays (tree, const flexmems_t *);
150 static void find_flexarrays (tree, flexmems_t *, bool = false,
151 tree = NULL_TREE, tree = NULL_TREE);
152 static void check_flexarrays (tree, flexmems_t * = NULL, bool = false);
153 static void check_bases (tree, int *, int *);
154 static void check_bases_and_members (tree);
155 static tree create_vtable_ptr (tree, tree *);
156 static void include_empty_classes (record_layout_info);
157 static void layout_class_type (tree, tree *);
158 static void propagate_binfo_offsets (tree, tree);
159 static void layout_virtual_bases (record_layout_info, splay_tree);
160 static void build_vbase_offset_vtbl_entries (tree, vtbl_init_data *);
161 static void add_vcall_offset_vtbl_entries_r (tree, vtbl_init_data *);
162 static void add_vcall_offset_vtbl_entries_1 (tree, vtbl_init_data *);
163 static void build_vcall_offset_vtbl_entries (tree, vtbl_init_data *);
164 static void add_vcall_offset (tree, tree, vtbl_init_data *);
165 static void layout_vtable_decl (tree, int);
166 static tree dfs_find_final_overrider_pre (tree, void *);
167 static tree dfs_find_final_overrider_post (tree, void *);
168 static tree find_final_overrider (tree, tree, tree);
169 static int make_new_vtable (tree, tree);
170 static tree get_primary_binfo (tree);
171 static int maybe_indent_hierarchy (FILE *, int, int);
172 static tree dump_class_hierarchy_r (FILE *, dump_flags_t, tree, tree, int);
173 static void dump_class_hierarchy (tree);
174 static void dump_class_hierarchy_1 (FILE *, dump_flags_t, tree);
175 static void dump_array (FILE *, tree);
176 static void dump_vtable (tree, tree, tree);
177 static void dump_vtt (tree, tree);
178 static void dump_thunk (FILE *, int, tree);
179 static tree build_vtable (tree, tree, tree);
180 static void initialize_vtable (tree, vec<constructor_elt, va_gc> *);
181 static void layout_nonempty_base_or_field (record_layout_info,
182 tree, tree, splay_tree);
183 static void accumulate_vtbl_inits (tree, tree, tree, tree, tree,
184 vec<constructor_elt, va_gc> **);
185 static void dfs_accumulate_vtbl_inits (tree, tree, tree, tree, tree,
186 vec<constructor_elt, va_gc> **);
187 static void build_rtti_vtbl_entries (tree, vtbl_init_data *);
188 static void build_vcall_and_vbase_vtbl_entries (tree, vtbl_init_data *);
189 static void clone_constructors_and_destructors (tree);
190 static tree build_clone (tree, tree);
191 static void update_vtable_entry_for_fn (tree, tree, tree, tree *, unsigned);
192 static void build_ctor_vtbl_group (tree, tree);
193 static void build_vtt (tree);
194 static tree binfo_ctor_vtable (tree);
195 static void build_vtt_inits (tree, tree, vec<constructor_elt, va_gc> **,
196 tree *);
197 static tree dfs_build_secondary_vptr_vtt_inits (tree, void *);
198 static tree dfs_fixup_binfo_vtbls (tree, void *);
199 static int record_subobject_offset (tree, tree, splay_tree);
200 static int check_subobject_offset (tree, tree, splay_tree);
201 static int walk_subobject_offsets (tree, subobject_offset_fn,
202 tree, splay_tree, tree, int);
203 static int layout_conflict_p (tree, tree, splay_tree, int);
204 static int splay_tree_compare_integer_csts (splay_tree_key k1,
205 splay_tree_key k2);
206 static void warn_about_ambiguous_bases (tree);
207 static bool type_requires_array_cookie (tree);
208 static bool base_derived_from (tree, tree);
209 static int empty_base_at_nonzero_offset_p (tree, tree, splay_tree);
210 static tree end_of_base (tree);
211 static tree get_vcall_index (tree, tree);
212 static bool type_maybe_constexpr_default_constructor (tree);
214 /* Return a COND_EXPR that executes TRUE_STMT if this execution of the
215 'structor is in charge of 'structing virtual bases, or FALSE_STMT
216 otherwise. */
218 tree
219 build_if_in_charge (tree true_stmt, tree false_stmt)
221 gcc_assert (DECL_HAS_IN_CHARGE_PARM_P (current_function_decl));
222 tree cmp = build2 (NE_EXPR, boolean_type_node,
223 current_in_charge_parm, integer_zero_node);
224 tree type = unlowered_expr_type (true_stmt);
225 if (VOID_TYPE_P (type))
226 type = unlowered_expr_type (false_stmt);
227 tree cond = build3 (COND_EXPR, type,
228 cmp, true_stmt, false_stmt);
229 return cond;
232 /* Convert to or from a base subobject. EXPR is an expression of type
233 `A' or `A*', an expression of type `B' or `B*' is returned. To
234 convert A to a base B, CODE is PLUS_EXPR and BINFO is the binfo for
235 the B base instance within A. To convert base A to derived B, CODE
236 is MINUS_EXPR and BINFO is the binfo for the A instance within B.
237 In this latter case, A must not be a morally virtual base of B.
238 NONNULL is true if EXPR is known to be non-NULL (this is only
239 needed when EXPR is of pointer type). CV qualifiers are preserved
240 from EXPR. */
242 tree
243 build_base_path (enum tree_code code,
244 tree expr,
245 tree binfo,
246 int nonnull,
247 tsubst_flags_t complain)
249 tree v_binfo = NULL_TREE;
250 tree d_binfo = NULL_TREE;
251 tree probe;
252 tree offset;
253 tree target_type;
254 tree null_test = NULL;
255 tree ptr_target_type;
256 int fixed_type_p;
257 int want_pointer = TYPE_PTR_P (TREE_TYPE (expr));
258 bool has_empty = false;
259 bool virtual_access;
260 bool rvalue = false;
262 if (expr == error_mark_node || binfo == error_mark_node || !binfo)
263 return error_mark_node;
265 for (probe = binfo; probe; probe = BINFO_INHERITANCE_CHAIN (probe))
267 d_binfo = probe;
268 if (is_empty_class (BINFO_TYPE (probe)))
269 has_empty = true;
270 if (!v_binfo && BINFO_VIRTUAL_P (probe))
271 v_binfo = probe;
274 probe = TYPE_MAIN_VARIANT (TREE_TYPE (expr));
275 if (want_pointer)
276 probe = TYPE_MAIN_VARIANT (TREE_TYPE (probe));
277 if (dependent_type_p (probe))
278 if (tree open = currently_open_class (probe))
279 probe = open;
281 if (code == PLUS_EXPR
282 && !SAME_BINFO_TYPE_P (BINFO_TYPE (d_binfo), probe))
284 /* This can happen when adjust_result_of_qualified_name_lookup can't
285 find a unique base binfo in a call to a member function. We
286 couldn't give the diagnostic then since we might have been calling
287 a static member function, so we do it now. In other cases, eg.
288 during error recovery (c++/71979), we may not have a base at all. */
289 if (complain & tf_error)
291 tree base = lookup_base (probe, BINFO_TYPE (d_binfo),
292 ba_unique, NULL, complain);
293 gcc_assert (base == error_mark_node || !base);
295 return error_mark_node;
298 gcc_assert ((code == MINUS_EXPR
299 && SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), probe))
300 || code == PLUS_EXPR);
302 if (binfo == d_binfo)
303 /* Nothing to do. */
304 return expr;
306 if (code == MINUS_EXPR && v_binfo)
308 if (complain & tf_error)
310 if (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), BINFO_TYPE (v_binfo)))
312 if (want_pointer)
313 error ("cannot convert from pointer to base class %qT to "
314 "pointer to derived class %qT because the base is "
315 "virtual", BINFO_TYPE (binfo), BINFO_TYPE (d_binfo));
316 else
317 error ("cannot convert from base class %qT to derived "
318 "class %qT because the base is virtual",
319 BINFO_TYPE (binfo), BINFO_TYPE (d_binfo));
321 else
323 if (want_pointer)
324 error ("cannot convert from pointer to base class %qT to "
325 "pointer to derived class %qT via virtual base %qT",
326 BINFO_TYPE (binfo), BINFO_TYPE (d_binfo),
327 BINFO_TYPE (v_binfo));
328 else
329 error ("cannot convert from base class %qT to derived "
330 "class %qT via virtual base %qT", BINFO_TYPE (binfo),
331 BINFO_TYPE (d_binfo), BINFO_TYPE (v_binfo));
334 return error_mark_node;
337 if (!want_pointer)
339 rvalue = !lvalue_p (expr);
340 /* This must happen before the call to save_expr. */
341 expr = cp_build_addr_expr (expr, complain);
343 else
344 expr = mark_rvalue_use (expr);
346 offset = BINFO_OFFSET (binfo);
347 fixed_type_p = resolves_to_fixed_type_p (expr, &nonnull);
348 target_type = code == PLUS_EXPR ? BINFO_TYPE (binfo) : BINFO_TYPE (d_binfo);
349 /* TARGET_TYPE has been extracted from BINFO, and, is therefore always
350 cv-unqualified. Extract the cv-qualifiers from EXPR so that the
351 expression returned matches the input. */
352 target_type = cp_build_qualified_type
353 (target_type, cp_type_quals (TREE_TYPE (TREE_TYPE (expr))));
354 ptr_target_type = build_pointer_type (target_type);
356 /* Do we need to look in the vtable for the real offset? */
357 virtual_access = (v_binfo && fixed_type_p <= 0);
359 /* Don't bother with the calculations inside sizeof; they'll ICE if the
360 source type is incomplete and the pointer value doesn't matter. In a
361 template (even in instantiate_non_dependent_expr), we don't have vtables
362 set up properly yet, and the value doesn't matter there either; we're
363 just interested in the result of overload resolution. */
364 if (cp_unevaluated_operand != 0
365 || processing_template_decl
366 || in_template_function ())
368 expr = build_nop (ptr_target_type, expr);
369 goto indout;
372 if (!COMPLETE_TYPE_P (probe))
374 if (complain & tf_error)
375 error ("cannot convert from %qT to base class %qT because %qT is "
376 "incomplete", BINFO_TYPE (d_binfo), BINFO_TYPE (binfo),
377 BINFO_TYPE (d_binfo));
378 return error_mark_node;
381 /* If we're in an NSDMI, we don't have the full constructor context yet
382 that we need for converting to a virtual base, so just build a stub
383 CONVERT_EXPR and expand it later in bot_replace. */
384 if (virtual_access && fixed_type_p < 0
385 && current_scope () != current_function_decl)
387 expr = build1 (CONVERT_EXPR, ptr_target_type, expr);
388 CONVERT_EXPR_VBASE_PATH (expr) = true;
389 goto indout;
392 /* Do we need to check for a null pointer? */
393 if (want_pointer && !nonnull)
395 /* If we know the conversion will not actually change the value
396 of EXPR, then we can avoid testing the expression for NULL.
397 We have to avoid generating a COMPONENT_REF for a base class
398 field, because other parts of the compiler know that such
399 expressions are always non-NULL. */
400 if (!virtual_access && integer_zerop (offset))
401 return build_nop (ptr_target_type, expr);
402 null_test = error_mark_node;
405 /* Protect against multiple evaluation if necessary. */
406 if (TREE_SIDE_EFFECTS (expr) && (null_test || virtual_access))
407 expr = save_expr (expr);
409 /* Now that we've saved expr, build the real null test. */
410 if (null_test)
412 tree zero = cp_convert (TREE_TYPE (expr), nullptr_node, complain);
413 null_test = build2_loc (input_location, NE_EXPR, boolean_type_node,
414 expr, zero);
415 /* This is a compiler generated comparison, don't emit
416 e.g. -Wnonnull-compare warning for it. */
417 TREE_NO_WARNING (null_test) = 1;
420 /* If this is a simple base reference, express it as a COMPONENT_REF. */
421 if (code == PLUS_EXPR && !virtual_access
422 /* We don't build base fields for empty bases, and they aren't very
423 interesting to the optimizers anyway. */
424 && !has_empty)
426 expr = cp_build_fold_indirect_ref (expr);
427 expr = build_simple_base_path (expr, binfo);
428 if (rvalue && lvalue_p (expr))
429 expr = move (expr);
430 if (want_pointer)
431 expr = build_address (expr);
432 target_type = TREE_TYPE (expr);
433 goto out;
436 if (virtual_access)
438 /* Going via virtual base V_BINFO. We need the static offset
439 from V_BINFO to BINFO, and the dynamic offset from D_BINFO to
440 V_BINFO. That offset is an entry in D_BINFO's vtable. */
441 tree v_offset;
443 if (fixed_type_p < 0 && in_base_initializer)
445 /* In a base member initializer, we cannot rely on the
446 vtable being set up. We have to indirect via the
447 vtt_parm. */
448 tree t;
450 t = TREE_TYPE (TYPE_VFIELD (current_class_type));
451 t = build_pointer_type (t);
452 v_offset = fold_convert (t, current_vtt_parm);
453 v_offset = cp_build_fold_indirect_ref (v_offset);
455 else
457 tree t = expr;
458 if (sanitize_flags_p (SANITIZE_VPTR)
459 && fixed_type_p == 0)
461 t = cp_ubsan_maybe_instrument_cast_to_vbase (input_location,
462 probe, expr);
463 if (t == NULL_TREE)
464 t = expr;
466 v_offset = build_vfield_ref (cp_build_fold_indirect_ref (t),
467 TREE_TYPE (TREE_TYPE (expr)));
470 if (v_offset == error_mark_node)
471 return error_mark_node;
473 v_offset = fold_build_pointer_plus (v_offset, BINFO_VPTR_FIELD (v_binfo));
474 v_offset = build1 (NOP_EXPR,
475 build_pointer_type (ptrdiff_type_node),
476 v_offset);
477 v_offset = cp_build_fold_indirect_ref (v_offset);
478 TREE_CONSTANT (v_offset) = 1;
480 offset = convert_to_integer (ptrdiff_type_node,
481 size_diffop_loc (input_location, offset,
482 BINFO_OFFSET (v_binfo)));
484 if (!integer_zerop (offset))
485 v_offset = build2 (code, ptrdiff_type_node, v_offset, offset);
487 if (fixed_type_p < 0)
488 /* Negative fixed_type_p means this is a constructor or destructor;
489 virtual base layout is fixed in in-charge [cd]tors, but not in
490 base [cd]tors. */
491 offset = build_if_in_charge
492 (convert_to_integer (ptrdiff_type_node, BINFO_OFFSET (binfo)),
493 v_offset);
494 else
495 offset = v_offset;
498 if (want_pointer)
499 target_type = ptr_target_type;
501 expr = build1 (NOP_EXPR, ptr_target_type, expr);
503 if (!integer_zerop (offset))
505 offset = fold_convert (sizetype, offset);
506 if (code == MINUS_EXPR)
507 offset = fold_build1_loc (input_location, NEGATE_EXPR, sizetype, offset);
508 expr = fold_build_pointer_plus (expr, offset);
510 else
511 null_test = NULL;
513 indout:
514 if (!want_pointer)
516 expr = cp_build_fold_indirect_ref (expr);
517 if (rvalue)
518 expr = move (expr);
521 out:
522 if (null_test)
523 expr = fold_build3_loc (input_location, COND_EXPR, target_type, null_test, expr,
524 build_zero_cst (target_type));
526 return expr;
529 /* Subroutine of build_base_path; EXPR and BINFO are as in that function.
530 Perform a derived-to-base conversion by recursively building up a
531 sequence of COMPONENT_REFs to the appropriate base fields. */
533 static tree
534 build_simple_base_path (tree expr, tree binfo)
536 tree type = BINFO_TYPE (binfo);
537 tree d_binfo = BINFO_INHERITANCE_CHAIN (binfo);
538 tree field;
540 if (d_binfo == NULL_TREE)
542 tree temp;
544 gcc_assert (TYPE_MAIN_VARIANT (TREE_TYPE (expr)) == type);
546 /* Transform `(a, b).x' into `(*(a, &b)).x', `(a ? b : c).x'
547 into `(*(a ? &b : &c)).x', and so on. A COND_EXPR is only
548 an lvalue in the front end; only _DECLs and _REFs are lvalues
549 in the back end. */
550 temp = unary_complex_lvalue (ADDR_EXPR, expr);
551 if (temp)
552 expr = cp_build_fold_indirect_ref (temp);
554 return expr;
557 /* Recurse. */
558 expr = build_simple_base_path (expr, d_binfo);
560 for (field = TYPE_FIELDS (BINFO_TYPE (d_binfo));
561 field; field = DECL_CHAIN (field))
562 /* Is this the base field created by build_base_field? */
563 if (TREE_CODE (field) == FIELD_DECL
564 && DECL_FIELD_IS_BASE (field)
565 && TREE_TYPE (field) == type
566 /* If we're looking for a field in the most-derived class,
567 also check the field offset; we can have two base fields
568 of the same type if one is an indirect virtual base and one
569 is a direct non-virtual base. */
570 && (BINFO_INHERITANCE_CHAIN (d_binfo)
571 || tree_int_cst_equal (byte_position (field),
572 BINFO_OFFSET (binfo))))
574 /* We don't use build_class_member_access_expr here, as that
575 has unnecessary checks, and more importantly results in
576 recursive calls to dfs_walk_once. */
577 int type_quals = cp_type_quals (TREE_TYPE (expr));
579 expr = build3 (COMPONENT_REF,
580 cp_build_qualified_type (type, type_quals),
581 expr, field, NULL_TREE);
582 /* Mark the expression const or volatile, as appropriate.
583 Even though we've dealt with the type above, we still have
584 to mark the expression itself. */
585 if (type_quals & TYPE_QUAL_CONST)
586 TREE_READONLY (expr) = 1;
587 if (type_quals & TYPE_QUAL_VOLATILE)
588 TREE_THIS_VOLATILE (expr) = 1;
590 return expr;
593 /* Didn't find the base field?!? */
594 gcc_unreachable ();
597 /* Convert OBJECT to the base TYPE. OBJECT is an expression whose
598 type is a class type or a pointer to a class type. In the former
599 case, TYPE is also a class type; in the latter it is another
600 pointer type. If CHECK_ACCESS is true, an error message is emitted
601 if TYPE is inaccessible. If OBJECT has pointer type, the value is
602 assumed to be non-NULL. */
604 tree
605 convert_to_base (tree object, tree type, bool check_access, bool nonnull,
606 tsubst_flags_t complain)
608 tree binfo;
609 tree object_type;
611 if (TYPE_PTR_P (TREE_TYPE (object)))
613 object_type = TREE_TYPE (TREE_TYPE (object));
614 type = TREE_TYPE (type);
616 else
617 object_type = TREE_TYPE (object);
619 binfo = lookup_base (object_type, type, check_access ? ba_check : ba_unique,
620 NULL, complain);
621 if (!binfo || binfo == error_mark_node)
622 return error_mark_node;
624 return build_base_path (PLUS_EXPR, object, binfo, nonnull, complain);
627 /* EXPR is an expression with unqualified class type. BASE is a base
628 binfo of that class type. Returns EXPR, converted to the BASE
629 type. This function assumes that EXPR is the most derived class;
630 therefore virtual bases can be found at their static offsets. */
632 tree
633 convert_to_base_statically (tree expr, tree base)
635 tree expr_type;
637 expr_type = TREE_TYPE (expr);
638 if (!SAME_BINFO_TYPE_P (BINFO_TYPE (base), expr_type))
640 /* If this is a non-empty base, use a COMPONENT_REF. */
641 if (!is_empty_class (BINFO_TYPE (base)))
642 return build_simple_base_path (expr, base);
644 /* We use fold_build2 and fold_convert below to simplify the trees
645 provided to the optimizers. It is not safe to call these functions
646 when processing a template because they do not handle C++-specific
647 trees. */
648 gcc_assert (!processing_template_decl);
649 expr = cp_build_addr_expr (expr, tf_warning_or_error);
650 if (!integer_zerop (BINFO_OFFSET (base)))
651 expr = fold_build_pointer_plus_loc (input_location,
652 expr, BINFO_OFFSET (base));
653 expr = fold_convert (build_pointer_type (BINFO_TYPE (base)), expr);
654 expr = build_fold_indirect_ref_loc (input_location, expr);
657 return expr;
661 tree
662 build_vfield_ref (tree datum, tree type)
664 tree vfield, vcontext;
666 if (datum == error_mark_node
667 /* Can happen in case of duplicate base types (c++/59082). */
668 || !TYPE_VFIELD (type))
669 return error_mark_node;
671 /* First, convert to the requested type. */
672 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (datum), type))
673 datum = convert_to_base (datum, type, /*check_access=*/false,
674 /*nonnull=*/true, tf_warning_or_error);
676 /* Second, the requested type may not be the owner of its own vptr.
677 If not, convert to the base class that owns it. We cannot use
678 convert_to_base here, because VCONTEXT may appear more than once
679 in the inheritance hierarchy of TYPE, and thus direct conversion
680 between the types may be ambiguous. Following the path back up
681 one step at a time via primary bases avoids the problem. */
682 vfield = TYPE_VFIELD (type);
683 vcontext = DECL_CONTEXT (vfield);
684 while (!same_type_ignoring_top_level_qualifiers_p (vcontext, type))
686 datum = build_simple_base_path (datum, CLASSTYPE_PRIMARY_BINFO (type));
687 type = TREE_TYPE (datum);
690 return build3 (COMPONENT_REF, TREE_TYPE (vfield), datum, vfield, NULL_TREE);
693 /* Given an object INSTANCE, return an expression which yields the
694 vtable element corresponding to INDEX. There are many special
695 cases for INSTANCE which we take care of here, mainly to avoid
696 creating extra tree nodes when we don't have to. */
698 tree
699 build_vtbl_ref (tree instance, tree idx)
701 tree aref;
702 tree vtbl = NULL_TREE;
704 /* Try to figure out what a reference refers to, and
705 access its virtual function table directly. */
707 int cdtorp = 0;
708 tree fixed_type = fixed_type_or_null (instance, NULL, &cdtorp);
710 tree basetype = non_reference (TREE_TYPE (instance));
712 if (fixed_type && !cdtorp)
714 tree binfo = lookup_base (fixed_type, basetype,
715 ba_unique, NULL, tf_none);
716 if (binfo && binfo != error_mark_node)
717 vtbl = unshare_expr (BINFO_VTABLE (binfo));
720 if (!vtbl)
721 vtbl = build_vfield_ref (instance, basetype);
723 aref = build_array_ref (input_location, vtbl, idx);
724 TREE_CONSTANT (aref) |= TREE_CONSTANT (vtbl) && TREE_CONSTANT (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 (cp_build_fold_indirect_ref (instance_ptr), idx);
739 /* When using function descriptors, the address of the
740 vtable entry is treated as a function pointer. */
741 if (TARGET_VTABLE_USES_DESCRIPTORS)
742 aref = build1 (NOP_EXPR, TREE_TYPE (aref),
743 cp_build_addr_expr (aref, tf_warning_or_error));
745 /* Remember this as a method reference, for later devirtualization. */
746 aref = build3 (OBJ_TYPE_REF, TREE_TYPE (aref), aref, instance_ptr, idx);
748 return aref;
751 /* Return the name of the virtual function table (as an IDENTIFIER_NODE)
752 for the given TYPE. */
754 static tree
755 get_vtable_name (tree type)
757 return mangle_vtbl_for_type (type);
760 /* DECL is an entity associated with TYPE, like a virtual table or an
761 implicitly generated constructor. Determine whether or not DECL
762 should have external or internal linkage at the object file
763 level. This routine does not deal with COMDAT linkage and other
764 similar complexities; it simply sets TREE_PUBLIC if it possible for
765 entities in other translation units to contain copies of DECL, in
766 the abstract. */
768 void
769 set_linkage_according_to_type (tree /*type*/, tree decl)
771 TREE_PUBLIC (decl) = 1;
772 determine_visibility (decl);
775 /* Create a VAR_DECL for a primary or secondary vtable for CLASS_TYPE.
776 (For a secondary vtable for B-in-D, CLASS_TYPE should be D, not B.)
777 Use NAME for the name of the vtable, and VTABLE_TYPE for its type. */
779 static tree
780 build_vtable (tree class_type, tree name, tree vtable_type)
782 tree decl;
784 decl = build_lang_decl (VAR_DECL, name, vtable_type);
785 /* vtable names are already mangled; give them their DECL_ASSEMBLER_NAME
786 now to avoid confusion in mangle_decl. */
787 SET_DECL_ASSEMBLER_NAME (decl, name);
788 DECL_CONTEXT (decl) = class_type;
789 DECL_ARTIFICIAL (decl) = 1;
790 TREE_STATIC (decl) = 1;
791 TREE_READONLY (decl) = 1;
792 DECL_VIRTUAL_P (decl) = 1;
793 SET_DECL_ALIGN (decl, TARGET_VTABLE_ENTRY_ALIGN);
794 DECL_USER_ALIGN (decl) = true;
795 DECL_VTABLE_OR_VTT_P (decl) = 1;
796 set_linkage_according_to_type (class_type, decl);
797 /* The vtable has not been defined -- yet. */
798 DECL_EXTERNAL (decl) = 1;
799 DECL_NOT_REALLY_EXTERN (decl) = 1;
801 /* Mark the VAR_DECL node representing the vtable itself as a
802 "gratuitous" one, thereby forcing dwarfout.c to ignore it. It
803 is rather important that such things be ignored because any
804 effort to actually generate DWARF for them will run into
805 trouble when/if we encounter code like:
807 #pragma interface
808 struct S { virtual void member (); };
810 because the artificial declaration of the vtable itself (as
811 manufactured by the g++ front end) will say that the vtable is
812 a static member of `S' but only *after* the debug output for
813 the definition of `S' has already been output. This causes
814 grief because the DWARF entry for the definition of the vtable
815 will try to refer back to an earlier *declaration* of the
816 vtable as a static member of `S' and there won't be one. We
817 might be able to arrange to have the "vtable static member"
818 attached to the member list for `S' before the debug info for
819 `S' get written (which would solve the problem) but that would
820 require more intrusive changes to the g++ front end. */
821 DECL_IGNORED_P (decl) = 1;
823 return decl;
826 /* Get the VAR_DECL of the vtable for TYPE. TYPE need not be polymorphic,
827 or even complete. If this does not exist, create it. If COMPLETE is
828 nonzero, then complete the definition of it -- that will render it
829 impossible to actually build the vtable, but is useful to get at those
830 which are known to exist in the runtime. */
832 tree
833 get_vtable_decl (tree type, int complete)
835 tree decl;
837 if (CLASSTYPE_VTABLES (type))
838 return CLASSTYPE_VTABLES (type);
840 decl = build_vtable (type, get_vtable_name (type), vtbl_type_node);
841 CLASSTYPE_VTABLES (type) = decl;
843 if (complete)
845 DECL_EXTERNAL (decl) = 1;
846 cp_finish_decl (decl, NULL_TREE, false, NULL_TREE, 0);
849 return decl;
852 /* Build the primary virtual function table for TYPE. If BINFO is
853 non-NULL, build the vtable starting with the initial approximation
854 that it is the same as the one which is the head of the association
855 list. Returns a nonzero value if a new vtable is actually
856 created. */
858 static int
859 build_primary_vtable (tree binfo, tree type)
861 tree decl;
862 tree virtuals;
864 decl = get_vtable_decl (type, /*complete=*/0);
866 if (binfo)
868 if (BINFO_NEW_VTABLE_MARKED (binfo))
869 /* We have already created a vtable for this base, so there's
870 no need to do it again. */
871 return 0;
873 virtuals = copy_list (BINFO_VIRTUALS (binfo));
874 TREE_TYPE (decl) = TREE_TYPE (get_vtbl_decl_for_binfo (binfo));
875 DECL_SIZE (decl) = TYPE_SIZE (TREE_TYPE (decl));
876 DECL_SIZE_UNIT (decl) = TYPE_SIZE_UNIT (TREE_TYPE (decl));
878 else
880 gcc_assert (TREE_TYPE (decl) == vtbl_type_node);
881 virtuals = NULL_TREE;
884 /* Initialize the association list for this type, based
885 on our first approximation. */
886 BINFO_VTABLE (TYPE_BINFO (type)) = decl;
887 BINFO_VIRTUALS (TYPE_BINFO (type)) = virtuals;
888 SET_BINFO_NEW_VTABLE_MARKED (TYPE_BINFO (type));
889 return 1;
892 /* Give BINFO a new virtual function table which is initialized
893 with a skeleton-copy of its original initialization. The only
894 entry that changes is the `delta' entry, so we can really
895 share a lot of structure.
897 FOR_TYPE is the most derived type which caused this table to
898 be needed.
900 Returns nonzero if we haven't met BINFO before.
902 The order in which vtables are built (by calling this function) for
903 an object must remain the same, otherwise a binary incompatibility
904 can result. */
906 static int
907 build_secondary_vtable (tree binfo)
909 if (BINFO_NEW_VTABLE_MARKED (binfo))
910 /* We already created a vtable for this base. There's no need to
911 do it again. */
912 return 0;
914 /* Remember that we've created a vtable for this BINFO, so that we
915 don't try to do so again. */
916 SET_BINFO_NEW_VTABLE_MARKED (binfo);
918 /* Make fresh virtual list, so we can smash it later. */
919 BINFO_VIRTUALS (binfo) = copy_list (BINFO_VIRTUALS (binfo));
921 /* Secondary vtables are laid out as part of the same structure as
922 the primary vtable. */
923 BINFO_VTABLE (binfo) = NULL_TREE;
924 return 1;
927 /* Create a new vtable for BINFO which is the hierarchy dominated by
928 T. Return nonzero if we actually created a new vtable. */
930 static int
931 make_new_vtable (tree t, tree binfo)
933 if (binfo == TYPE_BINFO (t))
934 /* In this case, it is *type*'s vtable we are modifying. We start
935 with the approximation that its vtable is that of the
936 immediate base class. */
937 return build_primary_vtable (binfo, t);
938 else
939 /* This is our very own copy of `basetype' to play with. Later,
940 we will fill in all the virtual functions that override the
941 virtual functions in these base classes which are not defined
942 by the current type. */
943 return build_secondary_vtable (binfo);
946 /* Make *VIRTUALS, an entry on the BINFO_VIRTUALS list for BINFO
947 (which is in the hierarchy dominated by T) list FNDECL as its
948 BV_FN. DELTA is the required constant adjustment from the `this'
949 pointer where the vtable entry appears to the `this' required when
950 the function is actually called. */
952 static void
953 modify_vtable_entry (tree t,
954 tree binfo,
955 tree fndecl,
956 tree delta,
957 tree *virtuals)
959 tree v;
961 v = *virtuals;
963 if (fndecl != BV_FN (v)
964 || !tree_int_cst_equal (delta, BV_DELTA (v)))
966 /* We need a new vtable for BINFO. */
967 if (make_new_vtable (t, binfo))
969 /* If we really did make a new vtable, we also made a copy
970 of the BINFO_VIRTUALS list. Now, we have to find the
971 corresponding entry in that list. */
972 *virtuals = BINFO_VIRTUALS (binfo);
973 while (BV_FN (*virtuals) != BV_FN (v))
974 *virtuals = TREE_CHAIN (*virtuals);
975 v = *virtuals;
978 BV_DELTA (v) = delta;
979 BV_VCALL_INDEX (v) = NULL_TREE;
980 BV_FN (v) = fndecl;
985 /* Add method METHOD to class TYPE. If VIA_USING indicates whether
986 METHOD is being injected via a using_decl. Returns true if the
987 method could be added to the method vec. */
989 bool
990 add_method (tree type, tree method, bool via_using)
992 if (method == error_mark_node)
993 return false;
995 gcc_assert (!DECL_EXTERN_C_P (method));
997 tree *slot = find_member_slot (type, DECL_NAME (method));
998 tree current_fns = slot ? *slot : NULL_TREE;
1000 /* Check to see if we've already got this method. */
1001 for (ovl_iterator iter (current_fns); iter; ++iter)
1003 tree fn = *iter;
1004 tree fn_type;
1005 tree method_type;
1006 tree parms1;
1007 tree parms2;
1009 if (TREE_CODE (fn) != TREE_CODE (method))
1010 continue;
1012 /* Two using-declarations can coexist, we'll complain about ambiguity in
1013 overload resolution. */
1014 if (via_using && iter.using_p ()
1015 /* Except handle inherited constructors specially. */
1016 && ! DECL_CONSTRUCTOR_P (fn))
1017 continue;
1019 /* [over.load] Member function declarations with the
1020 same name and the same parameter types cannot be
1021 overloaded if any of them is a static member
1022 function declaration.
1024 [over.load] Member function declarations with the same name and
1025 the same parameter-type-list as well as member function template
1026 declarations with the same name, the same parameter-type-list, and
1027 the same template parameter lists cannot be overloaded if any of
1028 them, but not all, have a ref-qualifier.
1030 [namespace.udecl] When a using-declaration brings names
1031 from a base class into a derived class scope, member
1032 functions in the derived class override and/or hide member
1033 functions with the same name and parameter types in a base
1034 class (rather than conflicting). */
1035 fn_type = TREE_TYPE (fn);
1036 method_type = TREE_TYPE (method);
1037 parms1 = TYPE_ARG_TYPES (fn_type);
1038 parms2 = TYPE_ARG_TYPES (method_type);
1040 /* Compare the quals on the 'this' parm. Don't compare
1041 the whole types, as used functions are treated as
1042 coming from the using class in overload resolution. */
1043 if (! DECL_STATIC_FUNCTION_P (fn)
1044 && ! DECL_STATIC_FUNCTION_P (method)
1045 /* Either both or neither need to be ref-qualified for
1046 differing quals to allow overloading. */
1047 && (FUNCTION_REF_QUALIFIED (fn_type)
1048 == FUNCTION_REF_QUALIFIED (method_type))
1049 && (type_memfn_quals (fn_type) != type_memfn_quals (method_type)
1050 || type_memfn_rqual (fn_type) != type_memfn_rqual (method_type)))
1051 continue;
1053 /* For templates, the return type and template parameters
1054 must be identical. */
1055 if (TREE_CODE (fn) == TEMPLATE_DECL
1056 && (!same_type_p (TREE_TYPE (fn_type),
1057 TREE_TYPE (method_type))
1058 || !comp_template_parms (DECL_TEMPLATE_PARMS (fn),
1059 DECL_TEMPLATE_PARMS (method))))
1060 continue;
1062 if (! DECL_STATIC_FUNCTION_P (fn))
1063 parms1 = TREE_CHAIN (parms1);
1064 if (! DECL_STATIC_FUNCTION_P (method))
1065 parms2 = TREE_CHAIN (parms2);
1067 /* Bring back parameters omitted from an inherited ctor. */
1068 if (ctor_omit_inherited_parms (fn))
1069 parms1 = FUNCTION_FIRST_USER_PARMTYPE (DECL_ORIGIN (fn));
1070 if (ctor_omit_inherited_parms (method))
1071 parms2 = FUNCTION_FIRST_USER_PARMTYPE (DECL_ORIGIN (method));
1073 if (compparms (parms1, parms2)
1074 && (!DECL_CONV_FN_P (fn)
1075 || same_type_p (TREE_TYPE (fn_type),
1076 TREE_TYPE (method_type)))
1077 && equivalently_constrained (fn, method))
1079 /* If these are versions of the same function, process and
1080 move on. */
1081 if (TREE_CODE (fn) == FUNCTION_DECL
1082 && maybe_version_functions (method, fn, true))
1083 continue;
1085 if (DECL_INHERITED_CTOR (method))
1087 if (DECL_INHERITED_CTOR (fn))
1089 tree basem = DECL_INHERITED_CTOR_BASE (method);
1090 tree basef = DECL_INHERITED_CTOR_BASE (fn);
1091 if (flag_new_inheriting_ctors)
1093 if (basem == basef)
1095 /* Inheriting the same constructor along different
1096 paths, combine them. */
1097 SET_DECL_INHERITED_CTOR
1098 (fn, ovl_make (DECL_INHERITED_CTOR (method),
1099 DECL_INHERITED_CTOR (fn)));
1100 /* And discard the new one. */
1101 return false;
1103 else
1104 /* Inherited ctors can coexist until overload
1105 resolution. */
1106 continue;
1108 error_at (DECL_SOURCE_LOCATION (method),
1109 "%q#D conflicts with version inherited from %qT",
1110 method, basef);
1111 inform (DECL_SOURCE_LOCATION (fn),
1112 "version inherited from %qT declared here",
1113 basef);
1115 /* Otherwise defer to the other function. */
1116 return false;
1119 if (via_using)
1120 /* Defer to the local function. */
1121 return false;
1122 else if (flag_new_inheriting_ctors
1123 && DECL_INHERITED_CTOR (fn))
1125 /* Remove the inherited constructor. */
1126 current_fns = iter.remove_node (current_fns);
1127 continue;
1129 else
1131 error_at (DECL_SOURCE_LOCATION (method),
1132 "%q#D cannot be overloaded with %q#D", method, fn);
1133 inform (DECL_SOURCE_LOCATION (fn),
1134 "previous declaration %q#D", fn);
1135 return false;
1140 /* A class should never have more than one destructor. */
1141 gcc_assert (!current_fns || !DECL_DESTRUCTOR_P (method));
1143 current_fns = ovl_insert (method, current_fns, via_using);
1145 if (!COMPLETE_TYPE_P (type) && !DECL_CONV_FN_P (method)
1146 && !push_class_level_binding (DECL_NAME (method), current_fns))
1147 return false;
1149 if (!slot)
1150 slot = add_member_slot (type, DECL_NAME (method));
1152 /* Maintain TYPE_HAS_USER_CONSTRUCTOR, etc. */
1153 grok_special_member_properties (method);
1155 *slot = current_fns;
1157 return true;
1160 /* Subroutines of finish_struct. */
1162 /* Change the access of FDECL to ACCESS in T. Return 1 if change was
1163 legit, otherwise return 0. */
1165 static int
1166 alter_access (tree t, tree fdecl, tree access)
1168 tree elem;
1170 retrofit_lang_decl (fdecl);
1172 gcc_assert (!DECL_DISCRIMINATOR_P (fdecl));
1174 elem = purpose_member (t, DECL_ACCESS (fdecl));
1175 if (elem)
1177 if (TREE_VALUE (elem) != access)
1179 if (TREE_CODE (TREE_TYPE (fdecl)) == FUNCTION_DECL)
1180 error ("conflicting access specifications for method"
1181 " %q+D, ignored", TREE_TYPE (fdecl));
1182 else
1183 error ("conflicting access specifications for field %qE, ignored",
1184 DECL_NAME (fdecl));
1186 else
1188 /* They're changing the access to the same thing they changed
1189 it to before. That's OK. */
1193 else
1195 perform_or_defer_access_check (TYPE_BINFO (t), fdecl, fdecl,
1196 tf_warning_or_error);
1197 DECL_ACCESS (fdecl) = tree_cons (t, access, DECL_ACCESS (fdecl));
1198 return 1;
1200 return 0;
1203 /* Return the access node for DECL's access in its enclosing class. */
1205 tree
1206 declared_access (tree decl)
1208 return (TREE_PRIVATE (decl) ? access_private_node
1209 : TREE_PROTECTED (decl) ? access_protected_node
1210 : access_public_node);
1213 /* Process the USING_DECL, which is a member of T. */
1215 static void
1216 handle_using_decl (tree using_decl, tree t)
1218 tree decl = USING_DECL_DECLS (using_decl);
1219 tree name = DECL_NAME (using_decl);
1220 tree access = declared_access (using_decl);
1221 tree flist = NULL_TREE;
1222 tree old_value;
1224 gcc_assert (!processing_template_decl && decl);
1226 old_value = lookup_member (t, name, /*protect=*/0, /*want_type=*/false,
1227 tf_warning_or_error);
1228 if (old_value)
1230 old_value = OVL_FIRST (old_value);
1232 if (DECL_P (old_value) && DECL_CONTEXT (old_value) == t)
1233 /* OK */;
1234 else
1235 old_value = NULL_TREE;
1238 cp_emit_debug_info_for_using (decl, t);
1240 if (is_overloaded_fn (decl))
1241 flist = decl;
1243 if (! old_value)
1245 else if (is_overloaded_fn (old_value))
1247 if (flist)
1248 /* It's OK to use functions from a base when there are functions with
1249 the same name already present in the current class. */;
1250 else
1252 error_at (DECL_SOURCE_LOCATION (using_decl), "%qD invalid in %q#T "
1253 "because of local method %q#D with same name",
1254 using_decl, t, old_value);
1255 inform (DECL_SOURCE_LOCATION (old_value),
1256 "local method %q#D declared here", old_value);
1257 return;
1260 else if (!DECL_ARTIFICIAL (old_value))
1262 error_at (DECL_SOURCE_LOCATION (using_decl), "%qD invalid in %q#T "
1263 "because of local member %q#D with same name",
1264 using_decl, t, old_value);
1265 inform (DECL_SOURCE_LOCATION (old_value),
1266 "local member %q#D declared here", old_value);
1267 return;
1270 /* Make type T see field decl FDECL with access ACCESS. */
1271 if (flist)
1272 for (ovl_iterator iter (flist); iter; ++iter)
1274 add_method (t, *iter, true);
1275 alter_access (t, *iter, access);
1277 else
1278 alter_access (t, decl, access);
1281 /* Data structure for find_abi_tags_r, below. */
1283 struct abi_tag_data
1285 tree t; // The type that we're checking for missing tags.
1286 tree subob; // The subobject of T that we're getting tags from.
1287 tree tags; // error_mark_node for diagnostics, or a list of missing tags.
1290 /* Subroutine of find_abi_tags_r. Handle a single TAG found on the class TP
1291 in the context of P. TAG can be either an identifier (the DECL_NAME of
1292 a tag NAMESPACE_DECL) or a STRING_CST (a tag attribute). */
1294 static void
1295 check_tag (tree tag, tree id, tree *tp, abi_tag_data *p)
1297 if (!IDENTIFIER_MARKED (id))
1299 if (p->tags != error_mark_node)
1301 /* We're collecting tags from template arguments or from
1302 the type of a variable or function return type. */
1303 p->tags = tree_cons (NULL_TREE, tag, p->tags);
1305 /* Don't inherit this tag multiple times. */
1306 IDENTIFIER_MARKED (id) = true;
1308 if (TYPE_P (p->t))
1310 /* Tags inherited from type template arguments are only used
1311 to avoid warnings. */
1312 ABI_TAG_IMPLICIT (p->tags) = true;
1313 return;
1315 /* For functions and variables we want to warn, too. */
1318 /* Otherwise we're diagnosing missing tags. */
1319 if (TREE_CODE (p->t) == FUNCTION_DECL)
1321 auto_diagnostic_group d;
1322 if (warning (OPT_Wabi_tag, "%qD inherits the %E ABI tag "
1323 "that %qT (used in its return type) has",
1324 p->t, tag, *tp))
1325 inform (location_of (*tp), "%qT declared here", *tp);
1327 else if (VAR_P (p->t))
1329 auto_diagnostic_group d;
1330 if (warning (OPT_Wabi_tag, "%qD inherits the %E ABI tag "
1331 "that %qT (used in its type) has", p->t, tag, *tp))
1332 inform (location_of (*tp), "%qT declared here", *tp);
1334 else if (TYPE_P (p->subob))
1336 auto_diagnostic_group d;
1337 if (warning (OPT_Wabi_tag, "%qT does not have the %E ABI tag "
1338 "that base %qT has", p->t, tag, p->subob))
1339 inform (location_of (p->subob), "%qT declared here",
1340 p->subob);
1342 else
1344 auto_diagnostic_group d;
1345 if (warning (OPT_Wabi_tag, "%qT does not have the %E ABI tag "
1346 "that %qT (used in the type of %qD) has",
1347 p->t, tag, *tp, p->subob))
1349 inform (location_of (p->subob), "%qD declared here",
1350 p->subob);
1351 inform (location_of (*tp), "%qT declared here", *tp);
1357 /* Find all the ABI tags in the attribute list ATTR and either call
1358 check_tag (if TP is non-null) or set IDENTIFIER_MARKED to val. */
1360 static void
1361 mark_or_check_attr_tags (tree attr, tree *tp, abi_tag_data *p, bool val)
1363 if (!attr)
1364 return;
1365 for (; (attr = lookup_attribute ("abi_tag", attr));
1366 attr = TREE_CHAIN (attr))
1367 for (tree list = TREE_VALUE (attr); list;
1368 list = TREE_CHAIN (list))
1370 tree tag = TREE_VALUE (list);
1371 tree id = get_identifier (TREE_STRING_POINTER (tag));
1372 if (tp)
1373 check_tag (tag, id, tp, p);
1374 else
1375 IDENTIFIER_MARKED (id) = val;
1379 /* Find all the ABI tags on T and its enclosing scopes and either call
1380 check_tag (if TP is non-null) or set IDENTIFIER_MARKED to val. */
1382 static void
1383 mark_or_check_tags (tree t, tree *tp, abi_tag_data *p, bool val)
1385 while (t != global_namespace)
1387 tree attr;
1388 if (TYPE_P (t))
1390 attr = TYPE_ATTRIBUTES (t);
1391 t = CP_TYPE_CONTEXT (t);
1393 else
1395 attr = DECL_ATTRIBUTES (t);
1396 t = CP_DECL_CONTEXT (t);
1398 mark_or_check_attr_tags (attr, tp, p, val);
1402 /* walk_tree callback for check_abi_tags: if the type at *TP involves any
1403 types with ABI tags, add the corresponding identifiers to the VEC in
1404 *DATA and set IDENTIFIER_MARKED. */
1406 static tree
1407 find_abi_tags_r (tree *tp, int *walk_subtrees, void *data)
1409 if (!OVERLOAD_TYPE_P (*tp))
1410 return NULL_TREE;
1412 /* walk_tree shouldn't be walking into any subtrees of a RECORD_TYPE
1413 anyway, but let's make sure of it. */
1414 *walk_subtrees = false;
1416 abi_tag_data *p = static_cast<struct abi_tag_data*>(data);
1418 mark_or_check_tags (*tp, tp, p, false);
1420 return NULL_TREE;
1423 /* walk_tree callback for mark_abi_tags: if *TP is a class, set
1424 IDENTIFIER_MARKED on its ABI tags. */
1426 static tree
1427 mark_abi_tags_r (tree *tp, int *walk_subtrees, void *data)
1429 if (!OVERLOAD_TYPE_P (*tp))
1430 return NULL_TREE;
1432 /* walk_tree shouldn't be walking into any subtrees of a RECORD_TYPE
1433 anyway, but let's make sure of it. */
1434 *walk_subtrees = false;
1436 bool *valp = static_cast<bool*>(data);
1438 mark_or_check_tags (*tp, NULL, NULL, *valp);
1440 return NULL_TREE;
1443 /* Set IDENTIFIER_MARKED on all the ABI tags on T and its enclosing
1444 scopes. */
1446 static void
1447 mark_abi_tags (tree t, bool val)
1449 mark_or_check_tags (t, NULL, NULL, val);
1450 if (DECL_P (t))
1452 if (DECL_LANG_SPECIFIC (t) && DECL_USE_TEMPLATE (t)
1453 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (t)))
1455 /* Template arguments are part of the signature. */
1456 tree level = INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (t));
1457 for (int j = 0; j < TREE_VEC_LENGTH (level); ++j)
1459 tree arg = TREE_VEC_ELT (level, j);
1460 cp_walk_tree_without_duplicates (&arg, mark_abi_tags_r, &val);
1463 if (TREE_CODE (t) == FUNCTION_DECL)
1464 /* A function's parameter types are part of the signature, so
1465 we don't need to inherit any tags that are also in them. */
1466 for (tree arg = FUNCTION_FIRST_USER_PARMTYPE (t); arg;
1467 arg = TREE_CHAIN (arg))
1468 cp_walk_tree_without_duplicates (&TREE_VALUE (arg),
1469 mark_abi_tags_r, &val);
1473 /* Check that T has all the ABI tags that subobject SUBOB has, or
1474 warn if not. If T is a (variable or function) declaration, also
1475 return any missing tags, and add them to T if JUST_CHECKING is false. */
1477 static tree
1478 check_abi_tags (tree t, tree subob, bool just_checking = false)
1480 bool inherit = DECL_P (t);
1482 if (!inherit && !warn_abi_tag)
1483 return NULL_TREE;
1485 tree decl = TYPE_P (t) ? TYPE_NAME (t) : t;
1486 if (!TREE_PUBLIC (decl))
1487 /* No need to worry about things local to this TU. */
1488 return NULL_TREE;
1490 mark_abi_tags (t, true);
1492 tree subtype = TYPE_P (subob) ? subob : TREE_TYPE (subob);
1493 struct abi_tag_data data = { t, subob, error_mark_node };
1494 if (inherit)
1495 data.tags = NULL_TREE;
1497 cp_walk_tree_without_duplicates (&subtype, find_abi_tags_r, &data);
1499 if (!(inherit && data.tags))
1500 /* We don't need to do anything with data.tags. */;
1501 else if (just_checking)
1502 for (tree t = data.tags; t; t = TREE_CHAIN (t))
1504 tree id = get_identifier (TREE_STRING_POINTER (TREE_VALUE (t)));
1505 IDENTIFIER_MARKED (id) = false;
1507 else
1509 tree attr = lookup_attribute ("abi_tag", DECL_ATTRIBUTES (t));
1510 if (attr)
1511 TREE_VALUE (attr) = chainon (data.tags, TREE_VALUE (attr));
1512 else
1513 DECL_ATTRIBUTES (t)
1514 = tree_cons (abi_tag_identifier, data.tags, DECL_ATTRIBUTES (t));
1517 mark_abi_tags (t, false);
1519 return data.tags;
1522 /* Check that DECL has all the ABI tags that are used in parts of its type
1523 that are not reflected in its mangled name. */
1525 void
1526 check_abi_tags (tree decl)
1528 if (VAR_P (decl))
1529 check_abi_tags (decl, TREE_TYPE (decl));
1530 else if (TREE_CODE (decl) == FUNCTION_DECL
1531 && !DECL_CONV_FN_P (decl)
1532 && !mangle_return_type_p (decl))
1533 check_abi_tags (decl, TREE_TYPE (TREE_TYPE (decl)));
1536 /* Return any ABI tags that are used in parts of the type of DECL
1537 that are not reflected in its mangled name. This function is only
1538 used in backward-compatible mangling for ABI <11. */
1540 tree
1541 missing_abi_tags (tree decl)
1543 if (VAR_P (decl))
1544 return check_abi_tags (decl, TREE_TYPE (decl), true);
1545 else if (TREE_CODE (decl) == FUNCTION_DECL
1546 /* Don't check DECL_CONV_FN_P here like we do in check_abi_tags, so
1547 that we can use this function for setting need_abi_warning
1548 regardless of the current flag_abi_version. */
1549 && !mangle_return_type_p (decl))
1550 return check_abi_tags (decl, TREE_TYPE (TREE_TYPE (decl)), true);
1551 else
1552 return NULL_TREE;
1555 void
1556 inherit_targ_abi_tags (tree t)
1558 if (!CLASS_TYPE_P (t)
1559 || CLASSTYPE_TEMPLATE_INFO (t) == NULL_TREE)
1560 return;
1562 mark_abi_tags (t, true);
1564 tree args = CLASSTYPE_TI_ARGS (t);
1565 struct abi_tag_data data = { t, NULL_TREE, NULL_TREE };
1566 for (int i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
1568 tree level = TMPL_ARGS_LEVEL (args, i+1);
1569 for (int j = 0; j < TREE_VEC_LENGTH (level); ++j)
1571 tree arg = TREE_VEC_ELT (level, j);
1572 data.subob = arg;
1573 cp_walk_tree_without_duplicates (&arg, find_abi_tags_r, &data);
1577 // If we found some tags on our template arguments, add them to our
1578 // abi_tag attribute.
1579 if (data.tags)
1581 tree attr = lookup_attribute ("abi_tag", TYPE_ATTRIBUTES (t));
1582 if (attr)
1583 TREE_VALUE (attr) = chainon (data.tags, TREE_VALUE (attr));
1584 else
1585 TYPE_ATTRIBUTES (t)
1586 = tree_cons (abi_tag_identifier, data.tags, TYPE_ATTRIBUTES (t));
1589 mark_abi_tags (t, false);
1592 /* Return true, iff class T has a non-virtual destructor that is
1593 accessible from outside the class heirarchy (i.e. is public, or
1594 there's a suitable friend. */
1596 static bool
1597 accessible_nvdtor_p (tree t)
1599 tree dtor = CLASSTYPE_DESTRUCTOR (t);
1601 /* An implicitly declared destructor is always public. And,
1602 if it were virtual, we would have created it by now. */
1603 if (!dtor)
1604 return true;
1606 if (DECL_VINDEX (dtor))
1607 return false; /* Virtual */
1609 if (!TREE_PRIVATE (dtor) && !TREE_PROTECTED (dtor))
1610 return true; /* Public */
1612 if (CLASSTYPE_FRIEND_CLASSES (t)
1613 || DECL_FRIENDLIST (TYPE_MAIN_DECL (t)))
1614 return true; /* Has friends */
1616 return false;
1619 /* Run through the base classes of T, updating CANT_HAVE_CONST_CTOR_P,
1620 and NO_CONST_ASN_REF_P. Also set flag bits in T based on
1621 properties of the bases. */
1623 static void
1624 check_bases (tree t,
1625 int* cant_have_const_ctor_p,
1626 int* no_const_asn_ref_p)
1628 int i;
1629 bool seen_non_virtual_nearly_empty_base_p = 0;
1630 int seen_tm_mask = 0;
1631 tree base_binfo;
1632 tree binfo;
1633 tree field = NULL_TREE;
1635 if (!CLASSTYPE_NON_STD_LAYOUT (t))
1636 for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
1637 if (TREE_CODE (field) == FIELD_DECL)
1638 break;
1640 for (binfo = TYPE_BINFO (t), i = 0;
1641 BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
1643 tree basetype = TREE_TYPE (base_binfo);
1645 gcc_assert (COMPLETE_TYPE_P (basetype));
1647 if (CLASSTYPE_FINAL (basetype))
1648 error ("cannot derive from %<final%> base %qT in derived type %qT",
1649 basetype, t);
1651 /* If any base class is non-literal, so is the derived class. */
1652 if (!CLASSTYPE_LITERAL_P (basetype))
1653 CLASSTYPE_LITERAL_P (t) = false;
1655 /* If the base class doesn't have copy constructors or
1656 assignment operators that take const references, then the
1657 derived class cannot have such a member automatically
1658 generated. */
1659 if (TYPE_HAS_COPY_CTOR (basetype)
1660 && ! TYPE_HAS_CONST_COPY_CTOR (basetype))
1661 *cant_have_const_ctor_p = 1;
1662 if (TYPE_HAS_COPY_ASSIGN (basetype)
1663 && !TYPE_HAS_CONST_COPY_ASSIGN (basetype))
1664 *no_const_asn_ref_p = 1;
1666 if (BINFO_VIRTUAL_P (base_binfo))
1667 /* A virtual base does not effect nearly emptiness. */
1669 else if (CLASSTYPE_NEARLY_EMPTY_P (basetype))
1671 if (seen_non_virtual_nearly_empty_base_p)
1672 /* And if there is more than one nearly empty base, then the
1673 derived class is not nearly empty either. */
1674 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
1675 else
1676 /* Remember we've seen one. */
1677 seen_non_virtual_nearly_empty_base_p = 1;
1679 else if (!is_empty_class (basetype))
1680 /* If the base class is not empty or nearly empty, then this
1681 class cannot be nearly empty. */
1682 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
1684 /* A lot of properties from the bases also apply to the derived
1685 class. */
1686 TYPE_NEEDS_CONSTRUCTING (t) |= TYPE_NEEDS_CONSTRUCTING (basetype);
1687 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t)
1688 |= TYPE_HAS_NONTRIVIAL_DESTRUCTOR (basetype);
1689 TYPE_HAS_COMPLEX_COPY_ASSIGN (t)
1690 |= (TYPE_HAS_COMPLEX_COPY_ASSIGN (basetype)
1691 || !TYPE_HAS_COPY_ASSIGN (basetype));
1692 TYPE_HAS_COMPLEX_COPY_CTOR (t) |= (TYPE_HAS_COMPLEX_COPY_CTOR (basetype)
1693 || !TYPE_HAS_COPY_CTOR (basetype));
1694 TYPE_HAS_COMPLEX_MOVE_ASSIGN (t)
1695 |= TYPE_HAS_COMPLEX_MOVE_ASSIGN (basetype);
1696 TYPE_HAS_COMPLEX_MOVE_CTOR (t) |= TYPE_HAS_COMPLEX_MOVE_CTOR (basetype);
1697 TYPE_POLYMORPHIC_P (t) |= TYPE_POLYMORPHIC_P (basetype);
1698 CLASSTYPE_CONTAINS_EMPTY_CLASS_P (t)
1699 |= CLASSTYPE_CONTAINS_EMPTY_CLASS_P (basetype);
1700 TYPE_HAS_COMPLEX_DFLT (t) |= (!TYPE_HAS_DEFAULT_CONSTRUCTOR (basetype)
1701 || TYPE_HAS_COMPLEX_DFLT (basetype));
1702 SET_CLASSTYPE_READONLY_FIELDS_NEED_INIT
1703 (t, CLASSTYPE_READONLY_FIELDS_NEED_INIT (t)
1704 | CLASSTYPE_READONLY_FIELDS_NEED_INIT (basetype));
1705 SET_CLASSTYPE_REF_FIELDS_NEED_INIT
1706 (t, CLASSTYPE_REF_FIELDS_NEED_INIT (t)
1707 | CLASSTYPE_REF_FIELDS_NEED_INIT (basetype));
1708 if (TYPE_HAS_MUTABLE_P (basetype))
1709 CLASSTYPE_HAS_MUTABLE (t) = 1;
1711 /* A standard-layout class is a class that:
1713 * has no non-standard-layout base classes, */
1714 CLASSTYPE_NON_STD_LAYOUT (t) |= CLASSTYPE_NON_STD_LAYOUT (basetype);
1715 if (!CLASSTYPE_NON_STD_LAYOUT (t))
1717 tree basefield;
1718 /* ...has no base classes of the same type as the first non-static
1719 data member... */
1720 if (field && DECL_CONTEXT (field) == t
1721 && (same_type_ignoring_top_level_qualifiers_p
1722 (TREE_TYPE (field), basetype)))
1723 CLASSTYPE_NON_STD_LAYOUT (t) = 1;
1724 else
1725 /* ...either has no non-static data members in the most-derived
1726 class and at most one base class with non-static data
1727 members, or has no base classes with non-static data
1728 members */
1729 for (basefield = TYPE_FIELDS (basetype); basefield;
1730 basefield = DECL_CHAIN (basefield))
1731 if (TREE_CODE (basefield) == FIELD_DECL
1732 && !(DECL_FIELD_IS_BASE (basefield)
1733 && integer_zerop (DECL_SIZE (basefield))))
1735 if (field)
1736 CLASSTYPE_NON_STD_LAYOUT (t) = 1;
1737 else
1738 field = basefield;
1739 break;
1743 /* Don't bother collecting tm attributes if transactional memory
1744 support is not enabled. */
1745 if (flag_tm)
1747 tree tm_attr = find_tm_attribute (TYPE_ATTRIBUTES (basetype));
1748 if (tm_attr)
1749 seen_tm_mask |= tm_attr_to_mask (tm_attr);
1752 check_abi_tags (t, basetype);
1755 /* If one of the base classes had TM attributes, and the current class
1756 doesn't define its own, then the current class inherits one. */
1757 if (seen_tm_mask && !find_tm_attribute (TYPE_ATTRIBUTES (t)))
1759 tree tm_attr = tm_mask_to_attr (least_bit_hwi (seen_tm_mask));
1760 TYPE_ATTRIBUTES (t) = tree_cons (tm_attr, NULL, TYPE_ATTRIBUTES (t));
1764 /* Determine all the primary bases within T. Sets BINFO_PRIMARY_BASE_P for
1765 those that are primaries. Sets BINFO_LOST_PRIMARY_P for those
1766 that have had a nearly-empty virtual primary base stolen by some
1767 other base in the hierarchy. Determines CLASSTYPE_PRIMARY_BASE for
1768 T. */
1770 static void
1771 determine_primary_bases (tree t)
1773 unsigned i;
1774 tree primary = NULL_TREE;
1775 tree type_binfo = TYPE_BINFO (t);
1776 tree base_binfo;
1778 /* Determine the primary bases of our bases. */
1779 for (base_binfo = TREE_CHAIN (type_binfo); base_binfo;
1780 base_binfo = TREE_CHAIN (base_binfo))
1782 tree primary = CLASSTYPE_PRIMARY_BINFO (BINFO_TYPE (base_binfo));
1784 /* See if we're the non-virtual primary of our inheritance
1785 chain. */
1786 if (!BINFO_VIRTUAL_P (base_binfo))
1788 tree parent = BINFO_INHERITANCE_CHAIN (base_binfo);
1789 tree parent_primary = CLASSTYPE_PRIMARY_BINFO (BINFO_TYPE (parent));
1791 if (parent_primary
1792 && SAME_BINFO_TYPE_P (BINFO_TYPE (base_binfo),
1793 BINFO_TYPE (parent_primary)))
1794 /* We are the primary binfo. */
1795 BINFO_PRIMARY_P (base_binfo) = 1;
1797 /* Determine if we have a virtual primary base, and mark it so.
1799 if (primary && BINFO_VIRTUAL_P (primary))
1801 tree this_primary = copied_binfo (primary, base_binfo);
1803 if (BINFO_PRIMARY_P (this_primary))
1804 /* Someone already claimed this base. */
1805 BINFO_LOST_PRIMARY_P (base_binfo) = 1;
1806 else
1808 tree delta;
1810 BINFO_PRIMARY_P (this_primary) = 1;
1811 BINFO_INHERITANCE_CHAIN (this_primary) = base_binfo;
1813 /* A virtual binfo might have been copied from within
1814 another hierarchy. As we're about to use it as a
1815 primary base, make sure the offsets match. */
1816 delta = size_diffop_loc (input_location,
1817 fold_convert (ssizetype,
1818 BINFO_OFFSET (base_binfo)),
1819 fold_convert (ssizetype,
1820 BINFO_OFFSET (this_primary)));
1822 propagate_binfo_offsets (this_primary, delta);
1827 /* First look for a dynamic direct non-virtual base. */
1828 for (i = 0; BINFO_BASE_ITERATE (type_binfo, i, base_binfo); i++)
1830 tree basetype = BINFO_TYPE (base_binfo);
1832 if (TYPE_CONTAINS_VPTR_P (basetype) && !BINFO_VIRTUAL_P (base_binfo))
1834 primary = base_binfo;
1835 goto found;
1839 /* A "nearly-empty" virtual base class can be the primary base
1840 class, if no non-virtual polymorphic base can be found. Look for
1841 a nearly-empty virtual dynamic base that is not already a primary
1842 base of something in the hierarchy. If there is no such base,
1843 just pick the first nearly-empty virtual base. */
1845 for (base_binfo = TREE_CHAIN (type_binfo); base_binfo;
1846 base_binfo = TREE_CHAIN (base_binfo))
1847 if (BINFO_VIRTUAL_P (base_binfo)
1848 && CLASSTYPE_NEARLY_EMPTY_P (BINFO_TYPE (base_binfo)))
1850 if (!BINFO_PRIMARY_P (base_binfo))
1852 /* Found one that is not primary. */
1853 primary = base_binfo;
1854 goto found;
1856 else if (!primary)
1857 /* Remember the first candidate. */
1858 primary = base_binfo;
1861 found:
1862 /* If we've got a primary base, use it. */
1863 if (primary)
1865 tree basetype = BINFO_TYPE (primary);
1867 CLASSTYPE_PRIMARY_BINFO (t) = primary;
1868 if (BINFO_PRIMARY_P (primary))
1869 /* We are stealing a primary base. */
1870 BINFO_LOST_PRIMARY_P (BINFO_INHERITANCE_CHAIN (primary)) = 1;
1871 BINFO_PRIMARY_P (primary) = 1;
1872 if (BINFO_VIRTUAL_P (primary))
1874 tree delta;
1876 BINFO_INHERITANCE_CHAIN (primary) = type_binfo;
1877 /* A virtual binfo might have been copied from within
1878 another hierarchy. As we're about to use it as a primary
1879 base, make sure the offsets match. */
1880 delta = size_diffop_loc (input_location, ssize_int (0),
1881 fold_convert (ssizetype, BINFO_OFFSET (primary)));
1883 propagate_binfo_offsets (primary, delta);
1886 primary = TYPE_BINFO (basetype);
1888 TYPE_VFIELD (t) = TYPE_VFIELD (basetype);
1889 BINFO_VTABLE (type_binfo) = BINFO_VTABLE (primary);
1890 BINFO_VIRTUALS (type_binfo) = BINFO_VIRTUALS (primary);
1894 /* Update the variant types of T. */
1896 void
1897 fixup_type_variants (tree t)
1899 tree variants;
1901 if (!t)
1902 return;
1904 for (variants = TYPE_NEXT_VARIANT (t);
1905 variants;
1906 variants = TYPE_NEXT_VARIANT (variants))
1908 /* These fields are in the _TYPE part of the node, not in
1909 the TYPE_LANG_SPECIFIC component, so they are not shared. */
1910 TYPE_HAS_USER_CONSTRUCTOR (variants) = TYPE_HAS_USER_CONSTRUCTOR (t);
1911 TYPE_NEEDS_CONSTRUCTING (variants) = TYPE_NEEDS_CONSTRUCTING (t);
1912 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (variants)
1913 = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t);
1915 TYPE_POLYMORPHIC_P (variants) = TYPE_POLYMORPHIC_P (t);
1917 TYPE_BINFO (variants) = TYPE_BINFO (t);
1919 /* Copy whatever these are holding today. */
1920 TYPE_VFIELD (variants) = TYPE_VFIELD (t);
1921 TYPE_FIELDS (variants) = TYPE_FIELDS (t);
1925 /* KLASS is a class that we're applying may_alias to after the body is
1926 parsed. Fixup any POINTER_TO and REFERENCE_TO types. The
1927 canonical type(s) will be implicitly updated. */
1929 static void
1930 fixup_may_alias (tree klass)
1932 tree t, v;
1934 for (t = TYPE_POINTER_TO (klass); t; t = TYPE_NEXT_PTR_TO (t))
1935 for (v = TYPE_MAIN_VARIANT (t); v; v = TYPE_NEXT_VARIANT (v))
1936 TYPE_REF_CAN_ALIAS_ALL (v) = true;
1937 for (t = TYPE_REFERENCE_TO (klass); t; t = TYPE_NEXT_REF_TO (t))
1938 for (v = TYPE_MAIN_VARIANT (t); v; v = TYPE_NEXT_VARIANT (v))
1939 TYPE_REF_CAN_ALIAS_ALL (v) = true;
1942 /* Early variant fixups: we apply attributes at the beginning of the class
1943 definition, and we need to fix up any variants that have already been
1944 made via elaborated-type-specifier so that check_qualified_type works. */
1946 void
1947 fixup_attribute_variants (tree t)
1949 tree variants;
1951 if (!t)
1952 return;
1954 tree attrs = TYPE_ATTRIBUTES (t);
1955 unsigned align = TYPE_ALIGN (t);
1956 bool user_align = TYPE_USER_ALIGN (t);
1957 bool may_alias = lookup_attribute ("may_alias", attrs);
1959 if (may_alias)
1960 fixup_may_alias (t);
1962 for (variants = TYPE_NEXT_VARIANT (t);
1963 variants;
1964 variants = TYPE_NEXT_VARIANT (variants))
1966 /* These are the two fields that check_qualified_type looks at and
1967 are affected by attributes. */
1968 TYPE_ATTRIBUTES (variants) = attrs;
1969 unsigned valign = align;
1970 if (TYPE_USER_ALIGN (variants))
1971 valign = MAX (valign, TYPE_ALIGN (variants));
1972 else
1973 TYPE_USER_ALIGN (variants) = user_align;
1974 SET_TYPE_ALIGN (variants, valign);
1975 if (may_alias)
1976 fixup_may_alias (variants);
1980 /* Set memoizing fields and bits of T (and its variants) for later
1981 use. */
1983 static void
1984 finish_struct_bits (tree t)
1986 /* Fix up variants (if any). */
1987 fixup_type_variants (t);
1989 if (BINFO_N_BASE_BINFOS (TYPE_BINFO (t)) && TYPE_POLYMORPHIC_P (t))
1990 /* For a class w/o baseclasses, 'finish_struct' has set
1991 CLASSTYPE_PURE_VIRTUALS correctly (by definition).
1992 Similarly for a class whose base classes do not have vtables.
1993 When neither of these is true, we might have removed abstract
1994 virtuals (by providing a definition), added some (by declaring
1995 new ones), or redeclared ones from a base class. We need to
1996 recalculate what's really an abstract virtual at this point (by
1997 looking in the vtables). */
1998 get_pure_virtuals (t);
2000 /* If this type has a copy constructor or a destructor, force its
2001 mode to be BLKmode, and force its TREE_ADDRESSABLE bit to be
2002 nonzero. This will cause it to be passed by invisible reference
2003 and prevent it from being returned in a register. */
2004 if (type_has_nontrivial_copy_init (t)
2005 || TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t))
2007 tree variants;
2008 SET_DECL_MODE (TYPE_MAIN_DECL (t), BLKmode);
2009 for (variants = t; variants; variants = TYPE_NEXT_VARIANT (variants))
2011 SET_TYPE_MODE (variants, BLKmode);
2012 TREE_ADDRESSABLE (variants) = 1;
2017 /* Issue warnings about T having private constructors, but no friends,
2018 and so forth.
2020 HAS_NONPRIVATE_METHOD is nonzero if T has any non-private methods or
2021 static members. HAS_NONPRIVATE_STATIC_FN is nonzero if T has any
2022 non-private static member functions. */
2024 static void
2025 maybe_warn_about_overly_private_class (tree t)
2027 int has_member_fn = 0;
2028 int has_nonprivate_method = 0;
2029 bool nonprivate_ctor = false;
2031 if (!warn_ctor_dtor_privacy
2032 /* If the class has friends, those entities might create and
2033 access instances, so we should not warn. */
2034 || (CLASSTYPE_FRIEND_CLASSES (t)
2035 || DECL_FRIENDLIST (TYPE_MAIN_DECL (t)))
2036 /* We will have warned when the template was declared; there's
2037 no need to warn on every instantiation. */
2038 || CLASSTYPE_TEMPLATE_INSTANTIATION (t))
2039 /* There's no reason to even consider warning about this
2040 class. */
2041 return;
2043 /* We only issue one warning, if more than one applies, because
2044 otherwise, on code like:
2046 class A {
2047 // Oops - forgot `public:'
2048 A();
2049 A(const A&);
2050 ~A();
2053 we warn several times about essentially the same problem. */
2055 /* Check to see if all (non-constructor, non-destructor) member
2056 functions are private. (Since there are no friends or
2057 non-private statics, we can't ever call any of the private member
2058 functions.) */
2059 for (tree fn = TYPE_FIELDS (t); fn; fn = DECL_CHAIN (fn))
2060 if (TREE_CODE (fn) == USING_DECL
2061 && DECL_NAME (fn) == ctor_identifier
2062 && !TREE_PRIVATE (fn))
2063 nonprivate_ctor = true;
2064 else if (!DECL_DECLARES_FUNCTION_P (fn))
2065 /* Not a function. */;
2066 else if (DECL_ARTIFICIAL (fn))
2067 /* We're not interested in compiler-generated methods; they don't
2068 provide any way to call private members. */;
2069 else if (!TREE_PRIVATE (fn))
2071 if (DECL_STATIC_FUNCTION_P (fn))
2072 /* A non-private static member function is just like a
2073 friend; it can create and invoke private member
2074 functions, and be accessed without a class
2075 instance. */
2076 return;
2078 has_nonprivate_method = 1;
2079 /* Keep searching for a static member function. */
2081 else if (!DECL_CONSTRUCTOR_P (fn) && !DECL_DESTRUCTOR_P (fn))
2082 has_member_fn = 1;
2084 if (!has_nonprivate_method && has_member_fn)
2086 /* There are no non-private methods, and there's at least one
2087 private member function that isn't a constructor or
2088 destructor. (If all the private members are
2089 constructors/destructors we want to use the code below that
2090 issues error messages specifically referring to
2091 constructors/destructors.) */
2092 unsigned i;
2093 tree binfo = TYPE_BINFO (t);
2095 for (i = 0; i != BINFO_N_BASE_BINFOS (binfo); i++)
2096 if (BINFO_BASE_ACCESS (binfo, i) != access_private_node)
2098 has_nonprivate_method = 1;
2099 break;
2101 if (!has_nonprivate_method)
2103 warning (OPT_Wctor_dtor_privacy,
2104 "all member functions in class %qT are private", t);
2105 return;
2109 /* Even if some of the member functions are non-private, the class
2110 won't be useful for much if all the constructors or destructors
2111 are private: such an object can never be created or destroyed. */
2112 if (tree dtor = CLASSTYPE_DESTRUCTOR (t))
2113 if (TREE_PRIVATE (dtor))
2115 warning (OPT_Wctor_dtor_privacy,
2116 "%q#T only defines a private destructor and has no friends",
2118 return;
2121 /* Warn about classes that have private constructors and no friends. */
2122 if (TYPE_HAS_USER_CONSTRUCTOR (t)
2123 /* Implicitly generated constructors are always public. */
2124 && !CLASSTYPE_LAZY_DEFAULT_CTOR (t))
2126 tree copy_or_move = NULL_TREE;
2128 /* If a non-template class does not define a copy
2129 constructor, one is defined for it, enabling it to avoid
2130 this warning. For a template class, this does not
2131 happen, and so we would normally get a warning on:
2133 template <class T> class C { private: C(); };
2135 To avoid this asymmetry, we check TYPE_HAS_COPY_CTOR. All
2136 complete non-template or fully instantiated classes have this
2137 flag set. */
2138 if (!TYPE_HAS_COPY_CTOR (t))
2139 nonprivate_ctor = true;
2140 else
2141 for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (t));
2142 !nonprivate_ctor && iter; ++iter)
2143 if (TREE_PRIVATE (*iter))
2144 continue;
2145 else if (copy_fn_p (*iter) || move_fn_p (*iter))
2146 /* Ideally, we wouldn't count any constructor that takes
2147 an argument of the class type as a parameter, because
2148 such things cannot be used to construct an instance of
2149 the class unless you already have one. */
2150 copy_or_move = *iter;
2151 else
2152 nonprivate_ctor = true;
2154 if (!nonprivate_ctor)
2156 warning (OPT_Wctor_dtor_privacy,
2157 "%q#T only defines private constructors and has no friends",
2159 if (copy_or_move)
2160 inform (DECL_SOURCE_LOCATION (copy_or_move),
2161 "%q#D is public, but requires an existing %q#T object",
2162 copy_or_move, t);
2163 return;
2168 /* Make BINFO's vtable have N entries, including RTTI entries,
2169 vbase and vcall offsets, etc. Set its type and call the back end
2170 to lay it out. */
2172 static void
2173 layout_vtable_decl (tree binfo, int n)
2175 tree atype;
2176 tree vtable;
2178 atype = build_array_of_n_type (vtable_entry_type, n);
2179 layout_type (atype);
2181 /* We may have to grow the vtable. */
2182 vtable = get_vtbl_decl_for_binfo (binfo);
2183 if (!same_type_p (TREE_TYPE (vtable), atype))
2185 TREE_TYPE (vtable) = atype;
2186 DECL_SIZE (vtable) = DECL_SIZE_UNIT (vtable) = NULL_TREE;
2187 layout_decl (vtable, 0);
2191 /* True iff FNDECL and BASE_FNDECL (both non-static member functions)
2192 have the same signature. */
2195 same_signature_p (const_tree fndecl, const_tree base_fndecl)
2197 /* One destructor overrides another if they are the same kind of
2198 destructor. */
2199 if (DECL_DESTRUCTOR_P (base_fndecl) && DECL_DESTRUCTOR_P (fndecl)
2200 && special_function_p (base_fndecl) == special_function_p (fndecl))
2201 return 1;
2202 /* But a non-destructor never overrides a destructor, nor vice
2203 versa, nor do different kinds of destructors override
2204 one-another. For example, a complete object destructor does not
2205 override a deleting destructor. */
2206 if (DECL_DESTRUCTOR_P (base_fndecl) || DECL_DESTRUCTOR_P (fndecl))
2207 return 0;
2209 if (DECL_NAME (fndecl) == DECL_NAME (base_fndecl)
2210 || (DECL_CONV_FN_P (fndecl)
2211 && DECL_CONV_FN_P (base_fndecl)
2212 && same_type_p (DECL_CONV_FN_TYPE (fndecl),
2213 DECL_CONV_FN_TYPE (base_fndecl))))
2215 tree fntype = TREE_TYPE (fndecl);
2216 tree base_fntype = TREE_TYPE (base_fndecl);
2217 if (type_memfn_quals (fntype) == type_memfn_quals (base_fntype)
2218 && type_memfn_rqual (fntype) == type_memfn_rqual (base_fntype)
2219 && compparms (FUNCTION_FIRST_USER_PARMTYPE (fndecl),
2220 FUNCTION_FIRST_USER_PARMTYPE (base_fndecl)))
2221 return 1;
2223 return 0;
2226 /* Returns TRUE if DERIVED is a binfo containing the binfo BASE as a
2227 subobject. */
2229 static bool
2230 base_derived_from (tree derived, tree base)
2232 tree probe;
2234 for (probe = base; probe; probe = BINFO_INHERITANCE_CHAIN (probe))
2236 if (probe == derived)
2237 return true;
2238 else if (BINFO_VIRTUAL_P (probe))
2239 /* If we meet a virtual base, we can't follow the inheritance
2240 any more. See if the complete type of DERIVED contains
2241 such a virtual base. */
2242 return (binfo_for_vbase (BINFO_TYPE (probe), BINFO_TYPE (derived))
2243 != NULL_TREE);
2245 return false;
2248 struct find_final_overrider_data {
2249 /* The function for which we are trying to find a final overrider. */
2250 tree fn;
2251 /* The base class in which the function was declared. */
2252 tree declaring_base;
2253 /* The candidate overriders. */
2254 tree candidates;
2255 /* Path to most derived. */
2256 vec<tree> path;
2259 /* Add the overrider along the current path to FFOD->CANDIDATES.
2260 Returns true if an overrider was found; false otherwise. */
2262 static bool
2263 dfs_find_final_overrider_1 (tree binfo,
2264 find_final_overrider_data *ffod,
2265 unsigned depth)
2267 tree method;
2269 /* If BINFO is not the most derived type, try a more derived class.
2270 A definition there will overrider a definition here. */
2271 if (depth)
2273 depth--;
2274 if (dfs_find_final_overrider_1
2275 (ffod->path[depth], ffod, depth))
2276 return true;
2279 method = look_for_overrides_here (BINFO_TYPE (binfo), ffod->fn);
2280 if (method)
2282 tree *candidate = &ffod->candidates;
2284 /* Remove any candidates overridden by this new function. */
2285 while (*candidate)
2287 /* If *CANDIDATE overrides METHOD, then METHOD
2288 cannot override anything else on the list. */
2289 if (base_derived_from (TREE_VALUE (*candidate), binfo))
2290 return true;
2291 /* If METHOD overrides *CANDIDATE, remove *CANDIDATE. */
2292 if (base_derived_from (binfo, TREE_VALUE (*candidate)))
2293 *candidate = TREE_CHAIN (*candidate);
2294 else
2295 candidate = &TREE_CHAIN (*candidate);
2298 /* Add the new function. */
2299 ffod->candidates = tree_cons (method, binfo, ffod->candidates);
2300 return true;
2303 return false;
2306 /* Called from find_final_overrider via dfs_walk. */
2308 static tree
2309 dfs_find_final_overrider_pre (tree binfo, void *data)
2311 find_final_overrider_data *ffod = (find_final_overrider_data *) data;
2313 if (binfo == ffod->declaring_base)
2314 dfs_find_final_overrider_1 (binfo, ffod, ffod->path.length ());
2315 ffod->path.safe_push (binfo);
2317 return NULL_TREE;
2320 static tree
2321 dfs_find_final_overrider_post (tree /*binfo*/, void *data)
2323 find_final_overrider_data *ffod = (find_final_overrider_data *) data;
2324 ffod->path.pop ();
2326 return NULL_TREE;
2329 /* Returns a TREE_LIST whose TREE_PURPOSE is the final overrider for
2330 FN and whose TREE_VALUE is the binfo for the base where the
2331 overriding occurs. BINFO (in the hierarchy dominated by the binfo
2332 DERIVED) is the base object in which FN is declared. */
2334 static tree
2335 find_final_overrider (tree derived, tree binfo, tree fn)
2337 find_final_overrider_data ffod;
2339 /* Getting this right is a little tricky. This is valid:
2341 struct S { virtual void f (); };
2342 struct T { virtual void f (); };
2343 struct U : public S, public T { };
2345 even though calling `f' in `U' is ambiguous. But,
2347 struct R { virtual void f(); };
2348 struct S : virtual public R { virtual void f (); };
2349 struct T : virtual public R { virtual void f (); };
2350 struct U : public S, public T { };
2352 is not -- there's no way to decide whether to put `S::f' or
2353 `T::f' in the vtable for `R'.
2355 The solution is to look at all paths to BINFO. If we find
2356 different overriders along any two, then there is a problem. */
2357 if (DECL_THUNK_P (fn))
2358 fn = THUNK_TARGET (fn);
2360 /* Determine the depth of the hierarchy. */
2361 ffod.fn = fn;
2362 ffod.declaring_base = binfo;
2363 ffod.candidates = NULL_TREE;
2364 ffod.path.create (30);
2366 dfs_walk_all (derived, dfs_find_final_overrider_pre,
2367 dfs_find_final_overrider_post, &ffod);
2369 ffod.path.release ();
2371 /* If there was no winner, issue an error message. */
2372 if (!ffod.candidates || TREE_CHAIN (ffod.candidates))
2373 return error_mark_node;
2375 return ffod.candidates;
2378 /* Return the index of the vcall offset for FN when TYPE is used as a
2379 virtual base. */
2381 static tree
2382 get_vcall_index (tree fn, tree type)
2384 vec<tree_pair_s, va_gc> *indices = CLASSTYPE_VCALL_INDICES (type);
2385 tree_pair_p p;
2386 unsigned ix;
2388 FOR_EACH_VEC_SAFE_ELT (indices, ix, p)
2389 if ((DECL_DESTRUCTOR_P (fn) && DECL_DESTRUCTOR_P (p->purpose))
2390 || same_signature_p (fn, p->purpose))
2391 return p->value;
2393 /* There should always be an appropriate index. */
2394 gcc_unreachable ();
2397 /* Update an entry in the vtable for BINFO, which is in the hierarchy
2398 dominated by T. FN is the old function; VIRTUALS points to the
2399 corresponding position in the new BINFO_VIRTUALS list. IX is the index
2400 of that entry in the list. */
2402 static void
2403 update_vtable_entry_for_fn (tree t, tree binfo, tree fn, tree* virtuals,
2404 unsigned ix)
2406 tree b;
2407 tree overrider;
2408 tree delta;
2409 tree virtual_base;
2410 tree first_defn;
2411 tree overrider_fn, overrider_target;
2412 tree target_fn = DECL_THUNK_P (fn) ? THUNK_TARGET (fn) : fn;
2413 tree over_return, base_return;
2414 bool lost = false;
2416 /* Find the nearest primary base (possibly binfo itself) which defines
2417 this function; this is the class the caller will convert to when
2418 calling FN through BINFO. */
2419 for (b = binfo; ; b = get_primary_binfo (b))
2421 gcc_assert (b);
2422 if (look_for_overrides_here (BINFO_TYPE (b), target_fn))
2423 break;
2425 /* The nearest definition is from a lost primary. */
2426 if (BINFO_LOST_PRIMARY_P (b))
2427 lost = true;
2429 first_defn = b;
2431 /* Find the final overrider. */
2432 overrider = find_final_overrider (TYPE_BINFO (t), b, target_fn);
2433 if (overrider == error_mark_node)
2435 error ("no unique final overrider for %qD in %qT", target_fn, t);
2436 return;
2438 overrider_target = overrider_fn = TREE_PURPOSE (overrider);
2440 /* Check for adjusting covariant return types. */
2441 over_return = TREE_TYPE (TREE_TYPE (overrider_target));
2442 base_return = TREE_TYPE (TREE_TYPE (target_fn));
2444 if (INDIRECT_TYPE_P (over_return)
2445 && TREE_CODE (over_return) == TREE_CODE (base_return)
2446 && CLASS_TYPE_P (TREE_TYPE (over_return))
2447 && CLASS_TYPE_P (TREE_TYPE (base_return))
2448 /* If the overrider is invalid, don't even try. */
2449 && !DECL_INVALID_OVERRIDER_P (overrider_target))
2451 /* If FN is a covariant thunk, we must figure out the adjustment
2452 to the final base FN was converting to. As OVERRIDER_TARGET might
2453 also be converting to the return type of FN, we have to
2454 combine the two conversions here. */
2455 tree fixed_offset, virtual_offset;
2457 over_return = TREE_TYPE (over_return);
2458 base_return = TREE_TYPE (base_return);
2460 if (DECL_THUNK_P (fn))
2462 gcc_assert (DECL_RESULT_THUNK_P (fn));
2463 fixed_offset = ssize_int (THUNK_FIXED_OFFSET (fn));
2464 virtual_offset = THUNK_VIRTUAL_OFFSET (fn);
2466 else
2467 fixed_offset = virtual_offset = NULL_TREE;
2469 if (virtual_offset)
2470 /* Find the equivalent binfo within the return type of the
2471 overriding function. We will want the vbase offset from
2472 there. */
2473 virtual_offset = binfo_for_vbase (BINFO_TYPE (virtual_offset),
2474 over_return);
2475 else if (!same_type_ignoring_top_level_qualifiers_p
2476 (over_return, base_return))
2478 /* There was no existing virtual thunk (which takes
2479 precedence). So find the binfo of the base function's
2480 return type within the overriding function's return type.
2481 Fortunately we know the covariancy is valid (it
2482 has already been checked), so we can just iterate along
2483 the binfos, which have been chained in inheritance graph
2484 order. Of course it is lame that we have to repeat the
2485 search here anyway -- we should really be caching pieces
2486 of the vtable and avoiding this repeated work. */
2487 tree thunk_binfo = NULL_TREE;
2488 tree base_binfo = TYPE_BINFO (base_return);
2490 /* Find the base binfo within the overriding function's
2491 return type. We will always find a thunk_binfo, except
2492 when the covariancy is invalid (which we will have
2493 already diagnosed). */
2494 if (base_binfo)
2495 for (thunk_binfo = TYPE_BINFO (over_return); thunk_binfo;
2496 thunk_binfo = TREE_CHAIN (thunk_binfo))
2497 if (SAME_BINFO_TYPE_P (BINFO_TYPE (thunk_binfo),
2498 BINFO_TYPE (base_binfo)))
2499 break;
2500 gcc_assert (thunk_binfo || errorcount);
2502 /* See if virtual inheritance is involved. */
2503 for (virtual_offset = thunk_binfo;
2504 virtual_offset;
2505 virtual_offset = BINFO_INHERITANCE_CHAIN (virtual_offset))
2506 if (BINFO_VIRTUAL_P (virtual_offset))
2507 break;
2509 if (virtual_offset
2510 || (thunk_binfo && !BINFO_OFFSET_ZEROP (thunk_binfo)))
2512 tree offset = fold_convert (ssizetype, BINFO_OFFSET (thunk_binfo));
2514 if (virtual_offset)
2516 /* We convert via virtual base. Adjust the fixed
2517 offset to be from there. */
2518 offset =
2519 size_diffop (offset,
2520 fold_convert (ssizetype,
2521 BINFO_OFFSET (virtual_offset)));
2523 if (fixed_offset)
2524 /* There was an existing fixed offset, this must be
2525 from the base just converted to, and the base the
2526 FN was thunking to. */
2527 fixed_offset = size_binop (PLUS_EXPR, fixed_offset, offset);
2528 else
2529 fixed_offset = offset;
2533 if (fixed_offset || virtual_offset)
2534 /* Replace the overriding function with a covariant thunk. We
2535 will emit the overriding function in its own slot as
2536 well. */
2537 overrider_fn = make_thunk (overrider_target, /*this_adjusting=*/0,
2538 fixed_offset, virtual_offset);
2540 else
2541 gcc_assert (DECL_INVALID_OVERRIDER_P (overrider_target) ||
2542 !DECL_THUNK_P (fn));
2544 /* If we need a covariant thunk, then we may need to adjust first_defn.
2545 The ABI specifies that the thunks emitted with a function are
2546 determined by which bases the function overrides, so we need to be
2547 sure that we're using a thunk for some overridden base; even if we
2548 know that the necessary this adjustment is zero, there may not be an
2549 appropriate zero-this-adjustment thunk for us to use since thunks for
2550 overriding virtual bases always use the vcall offset.
2552 Furthermore, just choosing any base that overrides this function isn't
2553 quite right, as this slot won't be used for calls through a type that
2554 puts a covariant thunk here. Calling the function through such a type
2555 will use a different slot, and that slot is the one that determines
2556 the thunk emitted for that base.
2558 So, keep looking until we find the base that we're really overriding
2559 in this slot: the nearest primary base that doesn't use a covariant
2560 thunk in this slot. */
2561 if (overrider_target != overrider_fn)
2563 if (BINFO_TYPE (b) == DECL_CONTEXT (overrider_target))
2564 /* We already know that the overrider needs a covariant thunk. */
2565 b = get_primary_binfo (b);
2566 for (; ; b = get_primary_binfo (b))
2568 tree main_binfo = TYPE_BINFO (BINFO_TYPE (b));
2569 tree bv = chain_index (ix, BINFO_VIRTUALS (main_binfo));
2570 if (!DECL_THUNK_P (TREE_VALUE (bv)))
2571 break;
2572 if (BINFO_LOST_PRIMARY_P (b))
2573 lost = true;
2575 first_defn = b;
2578 /* Assume that we will produce a thunk that convert all the way to
2579 the final overrider, and not to an intermediate virtual base. */
2580 virtual_base = NULL_TREE;
2582 /* See if we can convert to an intermediate virtual base first, and then
2583 use the vcall offset located there to finish the conversion. */
2584 for (; b; b = BINFO_INHERITANCE_CHAIN (b))
2586 /* If we find the final overrider, then we can stop
2587 walking. */
2588 if (SAME_BINFO_TYPE_P (BINFO_TYPE (b),
2589 BINFO_TYPE (TREE_VALUE (overrider))))
2590 break;
2592 /* If we find a virtual base, and we haven't yet found the
2593 overrider, then there is a virtual base between the
2594 declaring base (first_defn) and the final overrider. */
2595 if (BINFO_VIRTUAL_P (b))
2597 virtual_base = b;
2598 break;
2602 /* Compute the constant adjustment to the `this' pointer. The
2603 `this' pointer, when this function is called, will point at BINFO
2604 (or one of its primary bases, which are at the same offset). */
2605 if (virtual_base)
2606 /* The `this' pointer needs to be adjusted from the declaration to
2607 the nearest virtual base. */
2608 delta = size_diffop_loc (input_location,
2609 fold_convert (ssizetype, BINFO_OFFSET (virtual_base)),
2610 fold_convert (ssizetype, BINFO_OFFSET (first_defn)));
2611 else if (lost)
2612 /* If the nearest definition is in a lost primary, we don't need an
2613 entry in our vtable. Except possibly in a constructor vtable,
2614 if we happen to get our primary back. In that case, the offset
2615 will be zero, as it will be a primary base. */
2616 delta = size_zero_node;
2617 else
2618 /* The `this' pointer needs to be adjusted from pointing to
2619 BINFO to pointing at the base where the final overrider
2620 appears. */
2621 delta = size_diffop_loc (input_location,
2622 fold_convert (ssizetype,
2623 BINFO_OFFSET (TREE_VALUE (overrider))),
2624 fold_convert (ssizetype, BINFO_OFFSET (binfo)));
2626 modify_vtable_entry (t, binfo, overrider_fn, delta, virtuals);
2628 if (virtual_base)
2629 BV_VCALL_INDEX (*virtuals)
2630 = get_vcall_index (overrider_target, BINFO_TYPE (virtual_base));
2631 else
2632 BV_VCALL_INDEX (*virtuals) = NULL_TREE;
2634 BV_LOST_PRIMARY (*virtuals) = lost;
2637 /* Called from modify_all_vtables via dfs_walk. */
2639 static tree
2640 dfs_modify_vtables (tree binfo, void* data)
2642 tree t = (tree) data;
2643 tree virtuals;
2644 tree old_virtuals;
2645 unsigned ix;
2647 if (!TYPE_CONTAINS_VPTR_P (BINFO_TYPE (binfo)))
2648 /* A base without a vtable needs no modification, and its bases
2649 are uninteresting. */
2650 return dfs_skip_bases;
2652 if (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), t)
2653 && !CLASSTYPE_HAS_PRIMARY_BASE_P (t))
2654 /* Don't do the primary vtable, if it's new. */
2655 return NULL_TREE;
2657 if (BINFO_PRIMARY_P (binfo) && !BINFO_VIRTUAL_P (binfo))
2658 /* There's no need to modify the vtable for a non-virtual primary
2659 base; we're not going to use that vtable anyhow. We do still
2660 need to do this for virtual primary bases, as they could become
2661 non-primary in a construction vtable. */
2662 return NULL_TREE;
2664 make_new_vtable (t, binfo);
2666 /* Now, go through each of the virtual functions in the virtual
2667 function table for BINFO. Find the final overrider, and update
2668 the BINFO_VIRTUALS list appropriately. */
2669 for (ix = 0, virtuals = BINFO_VIRTUALS (binfo),
2670 old_virtuals = BINFO_VIRTUALS (TYPE_BINFO (BINFO_TYPE (binfo)));
2671 virtuals;
2672 ix++, virtuals = TREE_CHAIN (virtuals),
2673 old_virtuals = TREE_CHAIN (old_virtuals))
2674 update_vtable_entry_for_fn (t,
2675 binfo,
2676 BV_FN (old_virtuals),
2677 &virtuals, ix);
2679 return NULL_TREE;
2682 /* Update all of the primary and secondary vtables for T. Create new
2683 vtables as required, and initialize their RTTI information. Each
2684 of the functions in VIRTUALS is declared in T and may override a
2685 virtual function from a base class; find and modify the appropriate
2686 entries to point to the overriding functions. Returns a list, in
2687 declaration order, of the virtual functions that are declared in T,
2688 but do not appear in the primary base class vtable, and which
2689 should therefore be appended to the end of the vtable for T. */
2691 static tree
2692 modify_all_vtables (tree t, tree virtuals)
2694 tree binfo = TYPE_BINFO (t);
2695 tree *fnsp;
2697 /* Mangle the vtable name before entering dfs_walk (c++/51884). */
2698 if (TYPE_CONTAINS_VPTR_P (t))
2699 get_vtable_decl (t, false);
2701 /* Update all of the vtables. */
2702 dfs_walk_once (binfo, dfs_modify_vtables, NULL, t);
2704 /* Add virtual functions not already in our primary vtable. These
2705 will be both those introduced by this class, and those overridden
2706 from secondary bases. It does not include virtuals merely
2707 inherited from secondary bases. */
2708 for (fnsp = &virtuals; *fnsp; )
2710 tree fn = TREE_VALUE (*fnsp);
2712 if (!value_member (fn, BINFO_VIRTUALS (binfo))
2713 || DECL_VINDEX (fn) == error_mark_node)
2715 /* We don't need to adjust the `this' pointer when
2716 calling this function. */
2717 BV_DELTA (*fnsp) = integer_zero_node;
2718 BV_VCALL_INDEX (*fnsp) = NULL_TREE;
2720 /* This is a function not already in our vtable. Keep it. */
2721 fnsp = &TREE_CHAIN (*fnsp);
2723 else
2724 /* We've already got an entry for this function. Skip it. */
2725 *fnsp = TREE_CHAIN (*fnsp);
2728 return virtuals;
2731 /* Get the base virtual function declarations in T that have the
2732 indicated NAME. */
2734 static void
2735 get_basefndecls (tree name, tree t, vec<tree> *base_fndecls)
2737 bool found_decls = false;
2739 /* Find virtual functions in T with the indicated NAME. */
2740 for (ovl_iterator iter (get_class_binding (t, name)); iter; ++iter)
2742 tree method = *iter;
2744 if (TREE_CODE (method) == FUNCTION_DECL && DECL_VINDEX (method))
2746 base_fndecls->safe_push (method);
2747 found_decls = true;
2751 if (found_decls)
2752 return;
2754 int n_baseclasses = BINFO_N_BASE_BINFOS (TYPE_BINFO (t));
2755 for (int i = 0; i < n_baseclasses; i++)
2757 tree basetype = BINFO_TYPE (BINFO_BASE_BINFO (TYPE_BINFO (t), i));
2758 get_basefndecls (name, basetype, base_fndecls);
2762 /* If this declaration supersedes the declaration of
2763 a method declared virtual in the base class, then
2764 mark this field as being virtual as well. */
2766 void
2767 check_for_override (tree decl, tree ctype)
2769 bool overrides_found = false;
2770 if (TREE_CODE (decl) == TEMPLATE_DECL)
2771 /* In [temp.mem] we have:
2773 A specialization of a member function template does not
2774 override a virtual function from a base class. */
2775 return;
2776 if ((DECL_DESTRUCTOR_P (decl)
2777 || IDENTIFIER_VIRTUAL_P (DECL_NAME (decl))
2778 || DECL_CONV_FN_P (decl))
2779 && look_for_overrides (ctype, decl)
2780 && !DECL_STATIC_FUNCTION_P (decl))
2781 /* Set DECL_VINDEX to a value that is neither an INTEGER_CST nor
2782 the error_mark_node so that we know it is an overriding
2783 function. */
2785 DECL_VINDEX (decl) = decl;
2786 overrides_found = true;
2787 if (warn_override && !DECL_OVERRIDE_P (decl)
2788 && !DECL_DESTRUCTOR_P (decl))
2789 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wsuggest_override,
2790 "%qD can be marked override", decl);
2793 if (DECL_VIRTUAL_P (decl))
2795 if (!DECL_VINDEX (decl))
2796 DECL_VINDEX (decl) = error_mark_node;
2797 IDENTIFIER_VIRTUAL_P (DECL_NAME (decl)) = 1;
2798 if (DECL_DESTRUCTOR_P (decl))
2799 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (ctype) = true;
2801 else if (DECL_FINAL_P (decl))
2802 error ("%q+#D marked %<final%>, but is not virtual", decl);
2803 if (DECL_OVERRIDE_P (decl) && !overrides_found)
2804 error ("%q+#D marked %<override%>, but does not override", decl);
2807 /* Warn about hidden virtual functions that are not overridden in t.
2808 We know that constructors and destructors don't apply. */
2810 static void
2811 warn_hidden (tree t)
2813 if (vec<tree, va_gc> *member_vec = CLASSTYPE_MEMBER_VEC (t))
2814 for (unsigned ix = member_vec->length (); ix--;)
2816 tree fns = (*member_vec)[ix];
2818 if (!OVL_P (fns))
2819 continue;
2821 tree name = OVL_NAME (fns);
2822 auto_vec<tree, 20> base_fndecls;
2823 tree base_binfo;
2824 tree binfo;
2825 unsigned j;
2827 /* Iterate through all of the base classes looking for possibly
2828 hidden functions. */
2829 for (binfo = TYPE_BINFO (t), j = 0;
2830 BINFO_BASE_ITERATE (binfo, j, base_binfo); j++)
2832 tree basetype = BINFO_TYPE (base_binfo);
2833 get_basefndecls (name, basetype, &base_fndecls);
2836 /* If there are no functions to hide, continue. */
2837 if (base_fndecls.is_empty ())
2838 continue;
2840 /* Remove any overridden functions. */
2841 for (ovl_iterator iter (fns); iter; ++iter)
2843 tree fndecl = *iter;
2844 if (TREE_CODE (fndecl) == FUNCTION_DECL
2845 && DECL_VINDEX (fndecl))
2847 /* If the method from the base class has the same
2848 signature as the method from the derived class, it
2849 has been overridden. */
2850 for (size_t k = 0; k < base_fndecls.length (); k++)
2851 if (base_fndecls[k]
2852 && same_signature_p (fndecl, base_fndecls[k]))
2853 base_fndecls[k] = NULL_TREE;
2857 /* Now give a warning for all base functions without overriders,
2858 as they are hidden. */
2859 tree base_fndecl;
2860 FOR_EACH_VEC_ELT (base_fndecls, j, base_fndecl)
2861 if (base_fndecl)
2863 /* Here we know it is a hider, and no overrider exists. */
2864 warning_at (location_of (base_fndecl),
2865 OPT_Woverloaded_virtual,
2866 "%qD was hidden", base_fndecl);
2867 warning_at (location_of (fns),
2868 OPT_Woverloaded_virtual, " by %qD", fns);
2873 /* Recursive helper for finish_struct_anon. */
2875 static void
2876 finish_struct_anon_r (tree field, bool complain)
2878 for (tree elt = TYPE_FIELDS (TREE_TYPE (field)); elt; elt = DECL_CHAIN (elt))
2880 /* We're generally only interested in entities the user
2881 declared, but we also find nested classes by noticing
2882 the TYPE_DECL that we create implicitly. You're
2883 allowed to put one anonymous union inside another,
2884 though, so we explicitly tolerate that. We use
2885 TYPE_UNNAMED_P rather than ANON_AGGR_TYPE_P so that
2886 we also allow unnamed types used for defining fields. */
2887 if (DECL_ARTIFICIAL (elt)
2888 && (!DECL_IMPLICIT_TYPEDEF_P (elt)
2889 || TYPE_UNNAMED_P (TREE_TYPE (elt))))
2890 continue;
2892 if (complain
2893 && (TREE_CODE (elt) != FIELD_DECL
2894 || (TREE_PRIVATE (elt) || TREE_PROTECTED (elt))))
2896 /* We already complained about static data members in
2897 finish_static_data_member_decl. */
2898 if (!VAR_P (elt))
2900 auto_diagnostic_group d;
2901 if (permerror (DECL_SOURCE_LOCATION (elt),
2902 TREE_CODE (TREE_TYPE (field)) == UNION_TYPE
2903 ? "%q#D invalid; an anonymous union may "
2904 "only have public non-static data members"
2905 : "%q#D invalid; an anonymous struct may "
2906 "only have public non-static data members", elt))
2908 static bool hint;
2909 if (flag_permissive && !hint)
2911 hint = true;
2912 inform (DECL_SOURCE_LOCATION (elt),
2913 "this flexibility is deprecated and will be "
2914 "removed");
2920 TREE_PRIVATE (elt) = TREE_PRIVATE (field);
2921 TREE_PROTECTED (elt) = TREE_PROTECTED (field);
2923 /* Recurse into the anonymous aggregates to correctly handle
2924 access control (c++/24926):
2926 class A {
2927 union {
2928 union {
2929 int i;
2934 int j=A().i; */
2935 if (DECL_NAME (elt) == NULL_TREE
2936 && ANON_AGGR_TYPE_P (TREE_TYPE (elt)))
2937 finish_struct_anon_r (elt, /*complain=*/false);
2941 /* Check for things that are invalid. There are probably plenty of other
2942 things we should check for also. */
2944 static void
2945 finish_struct_anon (tree t)
2947 for (tree field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
2949 if (TREE_STATIC (field))
2950 continue;
2951 if (TREE_CODE (field) != FIELD_DECL)
2952 continue;
2954 if (DECL_NAME (field) == NULL_TREE
2955 && ANON_AGGR_TYPE_P (TREE_TYPE (field)))
2956 finish_struct_anon_r (field, /*complain=*/true);
2960 /* Add T to CLASSTYPE_DECL_LIST of current_class_type which
2961 will be used later during class template instantiation.
2962 When FRIEND_P is zero, T can be a static member data (VAR_DECL),
2963 a non-static member data (FIELD_DECL), a member function
2964 (FUNCTION_DECL), a nested type (RECORD_TYPE, ENUM_TYPE),
2965 a typedef (TYPE_DECL) or a member class template (TEMPLATE_DECL)
2966 When FRIEND_P is nonzero, T is either a friend class
2967 (RECORD_TYPE, TEMPLATE_DECL) or a friend function
2968 (FUNCTION_DECL, TEMPLATE_DECL). */
2970 void
2971 maybe_add_class_template_decl_list (tree type, tree t, int friend_p)
2973 /* Save some memory by not creating TREE_LIST if TYPE is not template. */
2974 if (CLASSTYPE_TEMPLATE_INFO (type))
2975 CLASSTYPE_DECL_LIST (type)
2976 = tree_cons (friend_p ? NULL_TREE : type,
2977 t, CLASSTYPE_DECL_LIST (type));
2980 /* This function is called from declare_virt_assop_and_dtor via
2981 dfs_walk_all.
2983 DATA is a type that direcly or indirectly inherits the base
2984 represented by BINFO. If BINFO contains a virtual assignment [copy
2985 assignment or move assigment] operator or a virtual constructor,
2986 declare that function in DATA if it hasn't been already declared. */
2988 static tree
2989 dfs_declare_virt_assop_and_dtor (tree binfo, void *data)
2991 tree bv, fn, t = (tree)data;
2992 tree opname = assign_op_identifier;
2994 gcc_assert (t && CLASS_TYPE_P (t));
2995 gcc_assert (binfo && TREE_CODE (binfo) == TREE_BINFO);
2997 if (!TYPE_CONTAINS_VPTR_P (BINFO_TYPE (binfo)))
2998 /* A base without a vtable needs no modification, and its bases
2999 are uninteresting. */
3000 return dfs_skip_bases;
3002 if (BINFO_PRIMARY_P (binfo))
3003 /* If this is a primary base, then we have already looked at the
3004 virtual functions of its vtable. */
3005 return NULL_TREE;
3007 for (bv = BINFO_VIRTUALS (binfo); bv; bv = TREE_CHAIN (bv))
3009 fn = BV_FN (bv);
3011 if (DECL_NAME (fn) == opname)
3013 if (CLASSTYPE_LAZY_COPY_ASSIGN (t))
3014 lazily_declare_fn (sfk_copy_assignment, t);
3015 if (CLASSTYPE_LAZY_MOVE_ASSIGN (t))
3016 lazily_declare_fn (sfk_move_assignment, t);
3018 else if (DECL_DESTRUCTOR_P (fn)
3019 && CLASSTYPE_LAZY_DESTRUCTOR (t))
3020 lazily_declare_fn (sfk_destructor, t);
3023 return NULL_TREE;
3026 /* If the class type T has a direct or indirect base that contains a
3027 virtual assignment operator or a virtual destructor, declare that
3028 function in T if it hasn't been already declared. */
3030 static void
3031 declare_virt_assop_and_dtor (tree t)
3033 if (!(TYPE_POLYMORPHIC_P (t)
3034 && (CLASSTYPE_LAZY_COPY_ASSIGN (t)
3035 || CLASSTYPE_LAZY_MOVE_ASSIGN (t)
3036 || CLASSTYPE_LAZY_DESTRUCTOR (t))))
3037 return;
3039 dfs_walk_all (TYPE_BINFO (t),
3040 dfs_declare_virt_assop_and_dtor,
3041 NULL, t);
3044 /* Declare the inheriting constructor for class T inherited from base
3045 constructor CTOR with the parameter array PARMS of size NPARMS. */
3047 static void
3048 one_inheriting_sig (tree t, tree ctor, tree *parms, int nparms)
3050 gcc_assert (TYPE_MAIN_VARIANT (t) == t);
3052 /* We don't declare an inheriting ctor that would be a default,
3053 copy or move ctor for derived or base. */
3054 if (nparms == 0)
3055 return;
3056 if (nparms == 1
3057 && TYPE_REF_P (parms[0]))
3059 tree parm = TYPE_MAIN_VARIANT (TREE_TYPE (parms[0]));
3060 if (parm == t || parm == DECL_CONTEXT (ctor))
3061 return;
3064 tree parmlist = void_list_node;
3065 for (int i = nparms - 1; i >= 0; i--)
3066 parmlist = tree_cons (NULL_TREE, parms[i], parmlist);
3067 tree fn = implicitly_declare_fn (sfk_inheriting_constructor,
3068 t, false, ctor, parmlist);
3070 if (add_method (t, fn, false))
3072 DECL_CHAIN (fn) = TYPE_FIELDS (t);
3073 TYPE_FIELDS (t) = fn;
3077 /* Declare all the inheriting constructors for class T inherited from base
3078 constructor CTOR. */
3080 static void
3081 one_inherited_ctor (tree ctor, tree t, tree using_decl)
3083 tree parms = FUNCTION_FIRST_USER_PARMTYPE (ctor);
3085 if (flag_new_inheriting_ctors)
3087 ctor = implicitly_declare_fn (sfk_inheriting_constructor,
3088 t, /*const*/false, ctor, parms);
3089 add_method (t, ctor, using_decl != NULL_TREE);
3090 TYPE_HAS_USER_CONSTRUCTOR (t) = true;
3091 return;
3094 tree *new_parms = XALLOCAVEC (tree, list_length (parms));
3095 int i = 0;
3096 for (; parms && parms != void_list_node; parms = TREE_CHAIN (parms))
3098 if (TREE_PURPOSE (parms))
3099 one_inheriting_sig (t, ctor, new_parms, i);
3100 new_parms[i++] = TREE_VALUE (parms);
3102 one_inheriting_sig (t, ctor, new_parms, i);
3103 if (parms == NULL_TREE)
3105 auto_diagnostic_group d;
3106 if (warning (OPT_Winherited_variadic_ctor,
3107 "the ellipsis in %qD is not inherited", ctor))
3108 inform (DECL_SOURCE_LOCATION (ctor), "%qD declared here", ctor);
3112 /* Create default constructors, assignment operators, and so forth for
3113 the type indicated by T, if they are needed. CANT_HAVE_CONST_CTOR,
3114 and CANT_HAVE_CONST_ASSIGNMENT are nonzero if, for whatever reason,
3115 the class cannot have a default constructor, copy constructor
3116 taking a const reference argument, or an assignment operator taking
3117 a const reference, respectively. */
3119 static void
3120 add_implicitly_declared_members (tree t, tree* access_decls,
3121 int cant_have_const_cctor,
3122 int cant_have_const_assignment)
3124 /* Destructor. */
3125 if (!CLASSTYPE_DESTRUCTOR (t))
3126 /* In general, we create destructors lazily. */
3127 CLASSTYPE_LAZY_DESTRUCTOR (t) = 1;
3129 bool move_ok = false;
3130 if (cxx_dialect >= cxx11 && CLASSTYPE_LAZY_DESTRUCTOR (t)
3131 && !TYPE_HAS_COPY_CTOR (t) && !TYPE_HAS_COPY_ASSIGN (t)
3132 && !classtype_has_move_assign_or_move_ctor_p (t, false))
3133 move_ok = true;
3135 /* [class.ctor]
3137 If there is no user-declared constructor for a class, a default
3138 constructor is implicitly declared. */
3139 if (! TYPE_HAS_USER_CONSTRUCTOR (t))
3141 TYPE_HAS_DEFAULT_CONSTRUCTOR (t) = 1;
3142 CLASSTYPE_LAZY_DEFAULT_CTOR (t) = 1;
3143 if (cxx_dialect >= cxx11)
3144 TYPE_HAS_CONSTEXPR_CTOR (t)
3145 /* Don't force the declaration to get a hard answer; if the
3146 definition would have made the class non-literal, it will still be
3147 non-literal because of the base or member in question, and that
3148 gives a better diagnostic. */
3149 = type_maybe_constexpr_default_constructor (t);
3152 /* [class.ctor]
3154 If a class definition does not explicitly declare a copy
3155 constructor, one is declared implicitly. */
3156 if (! TYPE_HAS_COPY_CTOR (t))
3158 TYPE_HAS_COPY_CTOR (t) = 1;
3159 TYPE_HAS_CONST_COPY_CTOR (t) = !cant_have_const_cctor;
3160 CLASSTYPE_LAZY_COPY_CTOR (t) = 1;
3161 if (move_ok)
3162 CLASSTYPE_LAZY_MOVE_CTOR (t) = 1;
3165 /* If there is no assignment operator, one will be created if and
3166 when it is needed. For now, just record whether or not the type
3167 of the parameter to the assignment operator will be a const or
3168 non-const reference. */
3169 if (!TYPE_HAS_COPY_ASSIGN (t))
3171 TYPE_HAS_COPY_ASSIGN (t) = 1;
3172 TYPE_HAS_CONST_COPY_ASSIGN (t) = !cant_have_const_assignment;
3173 CLASSTYPE_LAZY_COPY_ASSIGN (t) = 1;
3174 if (move_ok && !LAMBDA_TYPE_P (t))
3175 CLASSTYPE_LAZY_MOVE_ASSIGN (t) = 1;
3178 /* We can't be lazy about declaring functions that might override
3179 a virtual function from a base class. */
3180 declare_virt_assop_and_dtor (t);
3182 while (*access_decls)
3184 tree using_decl = TREE_VALUE (*access_decls);
3185 tree decl = USING_DECL_DECLS (using_decl);
3186 if (DECL_NAME (using_decl) == ctor_identifier)
3188 /* declare, then remove the decl */
3189 tree ctor_list = decl;
3190 location_t loc = input_location;
3191 input_location = DECL_SOURCE_LOCATION (using_decl);
3192 for (ovl_iterator iter (ctor_list); iter; ++iter)
3193 one_inherited_ctor (*iter, t, using_decl);
3194 *access_decls = TREE_CHAIN (*access_decls);
3195 input_location = loc;
3197 else
3198 access_decls = &TREE_CHAIN (*access_decls);
3202 /* FIELD is a bit-field. We are finishing the processing for its
3203 enclosing type. Issue any appropriate messages and set appropriate
3204 flags. Returns false if an error has been diagnosed. */
3206 static bool
3207 check_bitfield_decl (tree field)
3209 tree type = TREE_TYPE (field);
3210 tree w;
3212 /* Extract the declared width of the bitfield, which has been
3213 temporarily stashed in DECL_BIT_FIELD_REPRESENTATIVE by grokbitfield. */
3214 w = DECL_BIT_FIELD_REPRESENTATIVE (field);
3215 gcc_assert (w != NULL_TREE);
3216 /* Remove the bit-field width indicator so that the rest of the
3217 compiler does not treat that value as a qualifier. */
3218 DECL_BIT_FIELD_REPRESENTATIVE (field) = NULL_TREE;
3220 /* Detect invalid bit-field type. */
3221 if (!INTEGRAL_OR_ENUMERATION_TYPE_P (type))
3223 error ("bit-field %q+#D with non-integral type", field);
3224 w = error_mark_node;
3226 else
3228 location_t loc = input_location;
3229 /* Avoid the non_lvalue wrapper added by fold for PLUS_EXPRs. */
3230 STRIP_NOPS (w);
3232 /* detect invalid field size. */
3233 input_location = DECL_SOURCE_LOCATION (field);
3234 w = cxx_constant_value (w);
3235 input_location = loc;
3237 if (TREE_CODE (w) != INTEGER_CST)
3239 error ("bit-field %q+D width not an integer constant", field);
3240 w = error_mark_node;
3242 else if (tree_int_cst_sgn (w) < 0)
3244 error ("negative width in bit-field %q+D", field);
3245 w = error_mark_node;
3247 else if (integer_zerop (w) && DECL_NAME (field) != 0)
3249 error ("zero width for bit-field %q+D", field);
3250 w = error_mark_node;
3252 else if ((TREE_CODE (type) != ENUMERAL_TYPE
3253 && TREE_CODE (type) != BOOLEAN_TYPE
3254 && compare_tree_int (w, TYPE_PRECISION (type)) > 0)
3255 || ((TREE_CODE (type) == ENUMERAL_TYPE
3256 || TREE_CODE (type) == BOOLEAN_TYPE)
3257 && tree_int_cst_lt (TYPE_SIZE (type), w)))
3258 warning_at (DECL_SOURCE_LOCATION (field), 0,
3259 "width of %qD exceeds its type", field);
3260 else if (TREE_CODE (type) == ENUMERAL_TYPE)
3262 int prec = TYPE_PRECISION (ENUM_UNDERLYING_TYPE (type));
3263 if (compare_tree_int (w, prec) < 0)
3264 warning_at (DECL_SOURCE_LOCATION (field), 0,
3265 "%qD is too small to hold all values of %q#T",
3266 field, type);
3270 if (w != error_mark_node)
3272 DECL_SIZE (field) = fold_convert (bitsizetype, w);
3273 DECL_BIT_FIELD (field) = 1;
3274 return true;
3276 else
3278 /* Non-bit-fields are aligned for their type. */
3279 DECL_BIT_FIELD (field) = 0;
3280 CLEAR_DECL_C_BIT_FIELD (field);
3281 return false;
3285 /* FIELD is a non bit-field. We are finishing the processing for its
3286 enclosing type T. Issue any appropriate messages and set appropriate
3287 flags. */
3289 static bool
3290 check_field_decl (tree field,
3291 tree t,
3292 int* cant_have_const_ctor,
3293 int* no_const_asn_ref)
3295 tree type = strip_array_types (TREE_TYPE (field));
3296 bool any_default_members = false;
3298 /* In C++98 an anonymous union cannot contain any fields which would change
3299 the settings of CANT_HAVE_CONST_CTOR and friends. */
3300 if (ANON_UNION_TYPE_P (type) && cxx_dialect < cxx11)
3302 /* And, we don't set TYPE_HAS_CONST_COPY_CTOR, etc., for anonymous
3303 structs. So, we recurse through their fields here. */
3304 else if (ANON_AGGR_TYPE_P (type))
3306 for (tree fields = TYPE_FIELDS (type); fields;
3307 fields = DECL_CHAIN (fields))
3308 if (TREE_CODE (fields) == FIELD_DECL)
3309 any_default_members |= check_field_decl (fields, t,
3310 cant_have_const_ctor,
3311 no_const_asn_ref);
3313 /* Check members with class type for constructors, destructors,
3314 etc. */
3315 else if (CLASS_TYPE_P (type))
3317 /* Never let anything with uninheritable virtuals
3318 make it through without complaint. */
3319 abstract_virtuals_error (field, type);
3321 if (TREE_CODE (t) == UNION_TYPE && cxx_dialect < cxx11)
3323 static bool warned;
3324 int oldcount = errorcount;
3325 if (TYPE_NEEDS_CONSTRUCTING (type))
3326 error ("member %q+#D with constructor not allowed in union",
3327 field);
3328 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
3329 error ("member %q+#D with destructor not allowed in union", field);
3330 if (TYPE_HAS_COMPLEX_COPY_ASSIGN (type))
3331 error ("member %q+#D with copy assignment operator not allowed in union",
3332 field);
3333 if (!warned && errorcount > oldcount)
3335 inform (DECL_SOURCE_LOCATION (field), "unrestricted unions "
3336 "only available with -std=c++11 or -std=gnu++11");
3337 warned = true;
3340 else
3342 TYPE_NEEDS_CONSTRUCTING (t) |= TYPE_NEEDS_CONSTRUCTING (type);
3343 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t)
3344 |= TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type);
3345 TYPE_HAS_COMPLEX_COPY_ASSIGN (t)
3346 |= (TYPE_HAS_COMPLEX_COPY_ASSIGN (type)
3347 || !TYPE_HAS_COPY_ASSIGN (type));
3348 TYPE_HAS_COMPLEX_COPY_CTOR (t) |= (TYPE_HAS_COMPLEX_COPY_CTOR (type)
3349 || !TYPE_HAS_COPY_CTOR (type));
3350 TYPE_HAS_COMPLEX_MOVE_ASSIGN (t) |= TYPE_HAS_COMPLEX_MOVE_ASSIGN (type);
3351 TYPE_HAS_COMPLEX_MOVE_CTOR (t) |= TYPE_HAS_COMPLEX_MOVE_CTOR (type);
3352 TYPE_HAS_COMPLEX_DFLT (t) |= (!TYPE_HAS_DEFAULT_CONSTRUCTOR (type)
3353 || TYPE_HAS_COMPLEX_DFLT (type));
3356 if (TYPE_HAS_COPY_CTOR (type)
3357 && !TYPE_HAS_CONST_COPY_CTOR (type))
3358 *cant_have_const_ctor = 1;
3360 if (TYPE_HAS_COPY_ASSIGN (type)
3361 && !TYPE_HAS_CONST_COPY_ASSIGN (type))
3362 *no_const_asn_ref = 1;
3365 check_abi_tags (t, field);
3367 if (DECL_INITIAL (field) != NULL_TREE)
3368 /* `build_class_init_list' does not recognize
3369 non-FIELD_DECLs. */
3370 any_default_members = true;
3372 return any_default_members;
3375 /* Check the data members (both static and non-static), class-scoped
3376 typedefs, etc., appearing in the declaration of T. Issue
3377 appropriate diagnostics. Sets ACCESS_DECLS to a list (in
3378 declaration order) of access declarations; each TREE_VALUE in this
3379 list is a USING_DECL.
3381 In addition, set the following flags:
3383 EMPTY_P
3384 The class is empty, i.e., contains no non-static data members.
3386 CANT_HAVE_CONST_CTOR_P
3387 This class cannot have an implicitly generated copy constructor
3388 taking a const reference.
3390 CANT_HAVE_CONST_ASN_REF
3391 This class cannot have an implicitly generated assignment
3392 operator taking a const reference.
3394 All of these flags should be initialized before calling this
3395 function.
3397 Returns a pointer to the end of the TYPE_FIELDs chain; additional
3398 fields can be added by adding to this chain. */
3400 static void
3401 check_field_decls (tree t, tree *access_decls,
3402 int *cant_have_const_ctor_p,
3403 int *no_const_asn_ref_p)
3405 tree *field;
3406 tree *next;
3407 bool has_pointers;
3408 bool any_default_members;
3409 int cant_pack = 0;
3410 int field_access = -1;
3412 /* Assume there are no access declarations. */
3413 *access_decls = NULL_TREE;
3414 /* Assume this class has no pointer members. */
3415 has_pointers = false;
3416 /* Assume none of the members of this class have default
3417 initializations. */
3418 any_default_members = false;
3420 for (field = &TYPE_FIELDS (t); *field; field = next)
3422 tree x = *field;
3423 tree type = TREE_TYPE (x);
3424 int this_field_access;
3426 next = &DECL_CHAIN (x);
3428 if (TREE_CODE (x) == USING_DECL)
3430 /* Save the access declarations for our caller. */
3431 *access_decls = tree_cons (NULL_TREE, x, *access_decls);
3432 continue;
3435 if (TREE_CODE (x) == TYPE_DECL
3436 || TREE_CODE (x) == TEMPLATE_DECL)
3437 continue;
3439 if (TREE_CODE (x) == FUNCTION_DECL)
3440 /* FIXME: We should fold in the checking from check_methods. */
3441 continue;
3443 /* If we've gotten this far, it's a data member, possibly static,
3444 or an enumerator. */
3445 if (TREE_CODE (x) != CONST_DECL)
3446 DECL_CONTEXT (x) = t;
3448 /* When this goes into scope, it will be a non-local reference. */
3449 DECL_NONLOCAL (x) = 1;
3451 if (TREE_CODE (t) == UNION_TYPE)
3453 /* [class.union] (C++98)
3455 If a union contains a static data member, or a member of
3456 reference type, the program is ill-formed.
3458 In C++11 [class.union] says:
3459 If a union contains a non-static data member of reference type
3460 the program is ill-formed. */
3461 if (VAR_P (x) && cxx_dialect < cxx11)
3463 error ("in C++98 %q+D may not be static because it is "
3464 "a member of a union", x);
3465 continue;
3467 if (TYPE_REF_P (type)
3468 && TREE_CODE (x) == FIELD_DECL)
3470 error ("non-static data member %q+D in a union may not "
3471 "have reference type %qT", x, type);
3472 continue;
3476 /* Perform error checking that did not get done in
3477 grokdeclarator. */
3478 if (TREE_CODE (type) == FUNCTION_TYPE)
3480 error ("field %q+D invalidly declared function type", x);
3481 type = build_pointer_type (type);
3482 TREE_TYPE (x) = type;
3484 else if (TREE_CODE (type) == METHOD_TYPE)
3486 error ("field %q+D invalidly declared method type", x);
3487 type = build_pointer_type (type);
3488 TREE_TYPE (x) = type;
3491 if (type == error_mark_node)
3492 continue;
3494 if (TREE_CODE (x) == CONST_DECL || VAR_P (x))
3495 continue;
3497 /* Now it can only be a FIELD_DECL. */
3499 if (TREE_PRIVATE (x) || TREE_PROTECTED (x))
3500 CLASSTYPE_NON_AGGREGATE (t) = 1;
3502 /* If at least one non-static data member is non-literal, the whole
3503 class becomes non-literal. Per Core/1453, volatile non-static
3504 data members and base classes are also not allowed.
3505 Note: if the type is incomplete we will complain later on. */
3506 if (COMPLETE_TYPE_P (type)
3507 && (!literal_type_p (type) || CP_TYPE_VOLATILE_P (type)))
3508 CLASSTYPE_LITERAL_P (t) = false;
3510 /* A standard-layout class is a class that:
3512 has the same access control (Clause 11) for all non-static data members,
3513 ... */
3514 this_field_access = TREE_PROTECTED (x) ? 1 : TREE_PRIVATE (x) ? 2 : 0;
3515 if (field_access == -1)
3516 field_access = this_field_access;
3517 else if (this_field_access != field_access)
3518 CLASSTYPE_NON_STD_LAYOUT (t) = 1;
3520 /* If this is of reference type, check if it needs an init. */
3521 if (TYPE_REF_P (type))
3523 CLASSTYPE_NON_LAYOUT_POD_P (t) = 1;
3524 CLASSTYPE_NON_STD_LAYOUT (t) = 1;
3525 if (DECL_INITIAL (x) == NULL_TREE)
3526 SET_CLASSTYPE_REF_FIELDS_NEED_INIT (t, 1);
3527 if (cxx_dialect < cxx11)
3529 /* ARM $12.6.2: [A member initializer list] (or, for an
3530 aggregate, initialization by a brace-enclosed list) is the
3531 only way to initialize nonstatic const and reference
3532 members. */
3533 TYPE_HAS_COMPLEX_COPY_ASSIGN (t) = 1;
3534 TYPE_HAS_COMPLEX_MOVE_ASSIGN (t) = 1;
3538 type = strip_array_types (type);
3540 if (TYPE_PACKED (t))
3542 if (!layout_pod_type_p (type) && !TYPE_PACKED (type))
3544 warning_at
3545 (DECL_SOURCE_LOCATION (x), 0,
3546 "ignoring packed attribute because of unpacked non-POD field %q#D",
3548 cant_pack = 1;
3550 else if (DECL_C_BIT_FIELD (x)
3551 || TYPE_ALIGN (TREE_TYPE (x)) > BITS_PER_UNIT)
3552 DECL_PACKED (x) = 1;
3555 if (DECL_C_BIT_FIELD (x)
3556 && integer_zerop (DECL_BIT_FIELD_REPRESENTATIVE (x)))
3557 /* We don't treat zero-width bitfields as making a class
3558 non-empty. */
3560 else
3562 /* The class is non-empty. */
3563 CLASSTYPE_EMPTY_P (t) = 0;
3564 /* The class is not even nearly empty. */
3565 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
3566 /* If one of the data members contains an empty class,
3567 so does T. */
3568 if (CLASS_TYPE_P (type)
3569 && CLASSTYPE_CONTAINS_EMPTY_CLASS_P (type))
3570 CLASSTYPE_CONTAINS_EMPTY_CLASS_P (t) = 1;
3573 /* This is used by -Weffc++ (see below). Warn only for pointers
3574 to members which might hold dynamic memory. So do not warn
3575 for pointers to functions or pointers to members. */
3576 if (TYPE_PTR_P (type)
3577 && !TYPE_PTRFN_P (type))
3578 has_pointers = true;
3580 if (CLASS_TYPE_P (type))
3582 if (CLASSTYPE_REF_FIELDS_NEED_INIT (type))
3583 SET_CLASSTYPE_REF_FIELDS_NEED_INIT (t, 1);
3584 if (CLASSTYPE_READONLY_FIELDS_NEED_INIT (type))
3585 SET_CLASSTYPE_READONLY_FIELDS_NEED_INIT (t, 1);
3588 if (DECL_MUTABLE_P (x) || TYPE_HAS_MUTABLE_P (type))
3589 CLASSTYPE_HAS_MUTABLE (t) = 1;
3591 if (DECL_MUTABLE_P (x))
3593 if (CP_TYPE_CONST_P (type))
3595 error ("member %q+D cannot be declared both %<const%> "
3596 "and %<mutable%>", x);
3597 continue;
3599 if (TYPE_REF_P (type))
3601 error ("member %q+D cannot be declared as a %<mutable%> "
3602 "reference", x);
3603 continue;
3607 if (! layout_pod_type_p (type))
3608 /* DR 148 now allows pointers to members (which are POD themselves),
3609 to be allowed in POD structs. */
3610 CLASSTYPE_NON_LAYOUT_POD_P (t) = 1;
3612 if (!std_layout_type_p (type))
3613 CLASSTYPE_NON_STD_LAYOUT (t) = 1;
3615 if (! zero_init_p (type))
3616 CLASSTYPE_NON_ZERO_INIT_P (t) = 1;
3618 /* We set DECL_C_BIT_FIELD in grokbitfield.
3619 If the type and width are valid, we'll also set DECL_BIT_FIELD. */
3620 if (DECL_C_BIT_FIELD (x))
3621 check_bitfield_decl (x);
3623 if (check_field_decl (x, t, cant_have_const_ctor_p, no_const_asn_ref_p))
3625 if (any_default_members
3626 && TREE_CODE (t) == UNION_TYPE)
3627 error ("multiple fields in union %qT initialized", t);
3628 any_default_members = true;
3631 /* Now that we've removed bit-field widths from DECL_INITIAL,
3632 anything left in DECL_INITIAL is an NSDMI that makes the class
3633 non-aggregate in C++11. */
3634 if (DECL_INITIAL (x) && cxx_dialect < cxx14)
3635 CLASSTYPE_NON_AGGREGATE (t) = true;
3637 /* If any field is const, the structure type is pseudo-const. */
3638 if (CP_TYPE_CONST_P (type))
3640 C_TYPE_FIELDS_READONLY (t) = 1;
3641 if (DECL_INITIAL (x) == NULL_TREE)
3642 SET_CLASSTYPE_READONLY_FIELDS_NEED_INIT (t, 1);
3643 if (cxx_dialect < cxx11)
3645 /* ARM $12.6.2: [A member initializer list] (or, for an
3646 aggregate, initialization by a brace-enclosed list) is the
3647 only way to initialize nonstatic const and reference
3648 members. */
3649 TYPE_HAS_COMPLEX_COPY_ASSIGN (t) = 1;
3650 TYPE_HAS_COMPLEX_MOVE_ASSIGN (t) = 1;
3653 /* A field that is pseudo-const makes the structure likewise. */
3654 else if (CLASS_TYPE_P (type))
3656 C_TYPE_FIELDS_READONLY (t) |= C_TYPE_FIELDS_READONLY (type);
3657 SET_CLASSTYPE_READONLY_FIELDS_NEED_INIT (t,
3658 CLASSTYPE_READONLY_FIELDS_NEED_INIT (t)
3659 | CLASSTYPE_READONLY_FIELDS_NEED_INIT (type));
3662 /* Core issue 80: A nonstatic data member is required to have a
3663 different name from the class iff the class has a
3664 user-declared constructor. */
3665 if (constructor_name_p (DECL_NAME (x), t)
3666 && TYPE_HAS_USER_CONSTRUCTOR (t))
3667 permerror (DECL_SOURCE_LOCATION (x),
3668 "field %q#D with same name as class", x);
3671 /* Effective C++ rule 11: if a class has dynamic memory held by pointers,
3672 it should also define a copy constructor and an assignment operator to
3673 implement the correct copy semantic (deep vs shallow, etc.). As it is
3674 not feasible to check whether the constructors do allocate dynamic memory
3675 and store it within members, we approximate the warning like this:
3677 -- Warn only if there are members which are pointers
3678 -- Warn only if there is a non-trivial constructor (otherwise,
3679 there cannot be memory allocated).
3680 -- Warn only if there is a non-trivial destructor. We assume that the
3681 user at least implemented the cleanup correctly, and a destructor
3682 is needed to free dynamic memory.
3684 This seems enough for practical purposes. */
3685 if (warn_ecpp
3686 && has_pointers
3687 && TYPE_HAS_USER_CONSTRUCTOR (t)
3688 && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t)
3689 && !(TYPE_HAS_COPY_CTOR (t) && TYPE_HAS_COPY_ASSIGN (t)))
3691 warning (OPT_Weffc__, "%q#T has pointer data members", t);
3693 if (! TYPE_HAS_COPY_CTOR (t))
3695 warning (OPT_Weffc__,
3696 " but does not override %<%T(const %T&)%>", t, t);
3697 if (!TYPE_HAS_COPY_ASSIGN (t))
3698 warning (OPT_Weffc__, " or %<operator=(const %T&)%>", t);
3700 else if (! TYPE_HAS_COPY_ASSIGN (t))
3701 warning (OPT_Weffc__,
3702 " but does not override %<operator=(const %T&)%>", t);
3705 /* Non-static data member initializers make the default constructor
3706 non-trivial. */
3707 if (any_default_members)
3709 TYPE_NEEDS_CONSTRUCTING (t) = true;
3710 TYPE_HAS_COMPLEX_DFLT (t) = true;
3713 /* If any of the fields couldn't be packed, unset TYPE_PACKED. */
3714 if (cant_pack)
3715 TYPE_PACKED (t) = 0;
3717 /* Check anonymous struct/anonymous union fields. */
3718 finish_struct_anon (t);
3720 /* We've built up the list of access declarations in reverse order.
3721 Fix that now. */
3722 *access_decls = nreverse (*access_decls);
3725 /* If TYPE is an empty class type, records its OFFSET in the table of
3726 OFFSETS. */
3728 static int
3729 record_subobject_offset (tree type, tree offset, splay_tree offsets)
3731 splay_tree_node n;
3733 if (!is_empty_class (type))
3734 return 0;
3736 /* Record the location of this empty object in OFFSETS. */
3737 n = splay_tree_lookup (offsets, (splay_tree_key) offset);
3738 if (!n)
3739 n = splay_tree_insert (offsets,
3740 (splay_tree_key) offset,
3741 (splay_tree_value) NULL_TREE);
3742 n->value = ((splay_tree_value)
3743 tree_cons (NULL_TREE,
3744 type,
3745 (tree) n->value));
3747 return 0;
3750 /* Returns nonzero if TYPE is an empty class type and there is
3751 already an entry in OFFSETS for the same TYPE as the same OFFSET. */
3753 static int
3754 check_subobject_offset (tree type, tree offset, splay_tree offsets)
3756 splay_tree_node n;
3757 tree t;
3759 if (!is_empty_class (type))
3760 return 0;
3762 /* Record the location of this empty object in OFFSETS. */
3763 n = splay_tree_lookup (offsets, (splay_tree_key) offset);
3764 if (!n)
3765 return 0;
3767 for (t = (tree) n->value; t; t = TREE_CHAIN (t))
3768 if (same_type_p (TREE_VALUE (t), type))
3769 return 1;
3771 return 0;
3774 /* Walk through all the subobjects of TYPE (located at OFFSET). Call
3775 F for every subobject, passing it the type, offset, and table of
3776 OFFSETS. If VBASES_P is one, then virtual non-primary bases should
3777 be traversed.
3779 If MAX_OFFSET is non-NULL, then subobjects with an offset greater
3780 than MAX_OFFSET will not be walked.
3782 If F returns a nonzero value, the traversal ceases, and that value
3783 is returned. Otherwise, returns zero. */
3785 static int
3786 walk_subobject_offsets (tree type,
3787 subobject_offset_fn f,
3788 tree offset,
3789 splay_tree offsets,
3790 tree max_offset,
3791 int vbases_p)
3793 int r = 0;
3794 tree type_binfo = NULL_TREE;
3796 /* If this OFFSET is bigger than the MAX_OFFSET, then we should
3797 stop. */
3798 if (max_offset && tree_int_cst_lt (max_offset, offset))
3799 return 0;
3801 if (type == error_mark_node)
3802 return 0;
3804 if (!TYPE_P (type))
3806 type_binfo = type;
3807 type = BINFO_TYPE (type);
3810 if (CLASS_TYPE_P (type))
3812 tree field;
3813 tree binfo;
3814 int i;
3816 /* Avoid recursing into objects that are not interesting. */
3817 if (!CLASSTYPE_CONTAINS_EMPTY_CLASS_P (type))
3818 return 0;
3820 /* Record the location of TYPE. */
3821 r = (*f) (type, offset, offsets);
3822 if (r)
3823 return r;
3825 /* Iterate through the direct base classes of TYPE. */
3826 if (!type_binfo)
3827 type_binfo = TYPE_BINFO (type);
3828 for (i = 0; BINFO_BASE_ITERATE (type_binfo, i, binfo); i++)
3830 tree binfo_offset;
3832 if (BINFO_VIRTUAL_P (binfo))
3833 continue;
3835 tree orig_binfo;
3836 /* We cannot rely on BINFO_OFFSET being set for the base
3837 class yet, but the offsets for direct non-virtual
3838 bases can be calculated by going back to the TYPE. */
3839 orig_binfo = BINFO_BASE_BINFO (TYPE_BINFO (type), i);
3840 binfo_offset = size_binop (PLUS_EXPR,
3841 offset,
3842 BINFO_OFFSET (orig_binfo));
3844 r = walk_subobject_offsets (binfo,
3846 binfo_offset,
3847 offsets,
3848 max_offset,
3849 /*vbases_p=*/0);
3850 if (r)
3851 return r;
3854 if (CLASSTYPE_VBASECLASSES (type))
3856 unsigned ix;
3857 vec<tree, va_gc> *vbases;
3859 /* Iterate through the virtual base classes of TYPE. In G++
3860 3.2, we included virtual bases in the direct base class
3861 loop above, which results in incorrect results; the
3862 correct offsets for virtual bases are only known when
3863 working with the most derived type. */
3864 if (vbases_p)
3865 for (vbases = CLASSTYPE_VBASECLASSES (type), ix = 0;
3866 vec_safe_iterate (vbases, ix, &binfo); ix++)
3868 r = walk_subobject_offsets (binfo,
3870 size_binop (PLUS_EXPR,
3871 offset,
3872 BINFO_OFFSET (binfo)),
3873 offsets,
3874 max_offset,
3875 /*vbases_p=*/0);
3876 if (r)
3877 return r;
3879 else
3881 /* We still have to walk the primary base, if it is
3882 virtual. (If it is non-virtual, then it was walked
3883 above.) */
3884 tree vbase = get_primary_binfo (type_binfo);
3886 if (vbase && BINFO_VIRTUAL_P (vbase)
3887 && BINFO_PRIMARY_P (vbase)
3888 && BINFO_INHERITANCE_CHAIN (vbase) == type_binfo)
3890 r = (walk_subobject_offsets
3891 (vbase, f, offset,
3892 offsets, max_offset, /*vbases_p=*/0));
3893 if (r)
3894 return r;
3899 /* Iterate through the fields of TYPE. */
3900 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
3901 if (TREE_CODE (field) == FIELD_DECL
3902 && TREE_TYPE (field) != error_mark_node
3903 && !DECL_ARTIFICIAL (field))
3905 tree field_offset;
3907 field_offset = byte_position (field);
3909 r = walk_subobject_offsets (TREE_TYPE (field),
3911 size_binop (PLUS_EXPR,
3912 offset,
3913 field_offset),
3914 offsets,
3915 max_offset,
3916 /*vbases_p=*/1);
3917 if (r)
3918 return r;
3921 else if (TREE_CODE (type) == ARRAY_TYPE)
3923 tree element_type = strip_array_types (type);
3924 tree domain = TYPE_DOMAIN (type);
3925 tree index;
3927 /* Avoid recursing into objects that are not interesting. */
3928 if (!CLASS_TYPE_P (element_type)
3929 || !CLASSTYPE_CONTAINS_EMPTY_CLASS_P (element_type)
3930 || !domain
3931 || integer_minus_onep (TYPE_MAX_VALUE (domain)))
3932 return 0;
3934 /* Step through each of the elements in the array. */
3935 for (index = size_zero_node;
3936 !tree_int_cst_lt (TYPE_MAX_VALUE (domain), index);
3937 index = size_binop (PLUS_EXPR, index, size_one_node))
3939 r = walk_subobject_offsets (TREE_TYPE (type),
3941 offset,
3942 offsets,
3943 max_offset,
3944 /*vbases_p=*/1);
3945 if (r)
3946 return r;
3947 offset = size_binop (PLUS_EXPR, offset,
3948 TYPE_SIZE_UNIT (TREE_TYPE (type)));
3949 /* If this new OFFSET is bigger than the MAX_OFFSET, then
3950 there's no point in iterating through the remaining
3951 elements of the array. */
3952 if (max_offset && tree_int_cst_lt (max_offset, offset))
3953 break;
3957 return 0;
3960 /* Return true iff FIELD_DECL DECL is potentially overlapping. */
3962 static bool
3963 field_poverlapping_p (tree decl)
3965 /* Base fields are actually potentially overlapping, but C++ bases go through
3966 a different code path based on binfos, and ObjC++ base fields are laid out
3967 in objc-act, so we don't want layout_class_type to mess with them. */
3968 if (DECL_FIELD_IS_BASE (decl))
3970 gcc_checking_assert (c_dialect_objc ());
3971 return false;
3974 return lookup_attribute ("no_unique_address",
3975 DECL_ATTRIBUTES (decl));
3978 /* Record all of the empty subobjects of DECL_OR_BINFO. */
3980 static void
3981 record_subobject_offsets (tree decl_or_binfo,
3982 splay_tree offsets)
3984 tree type, offset;
3985 bool overlapping, vbases_p;
3987 if (DECL_P (decl_or_binfo))
3989 tree decl = decl_or_binfo;
3990 type = TREE_TYPE (decl);
3991 offset = byte_position (decl);
3992 overlapping = field_poverlapping_p (decl);
3993 vbases_p = true;
3995 else
3997 type = BINFO_TYPE (decl_or_binfo);
3998 offset = BINFO_OFFSET (decl_or_binfo);
3999 overlapping = true;
4000 vbases_p = false;
4003 tree max_offset;
4004 /* If recording subobjects for a non-static data member or a
4005 non-empty base class, we do not need to record offsets beyond
4006 the size of the biggest empty class. Additional data members
4007 will go at the end of the class. Additional base classes will go
4008 either at offset zero (if empty, in which case they cannot
4009 overlap with offsets past the size of the biggest empty class) or
4010 at the end of the class.
4012 However, if we are placing an empty base class, then we must record
4013 all offsets, as either the empty class is at offset zero (where
4014 other empty classes might later be placed) or at the end of the
4015 class (where other objects might then be placed, so other empty
4016 subobjects might later overlap). */
4017 if (!overlapping
4018 || !is_empty_class (type))
4019 max_offset = sizeof_biggest_empty_class;
4020 else
4021 max_offset = NULL_TREE;
4022 walk_subobject_offsets (type, record_subobject_offset, offset,
4023 offsets, max_offset, vbases_p);
4026 /* Returns nonzero if any of the empty subobjects of TYPE (located at
4027 OFFSET) conflict with entries in OFFSETS. If VBASES_P is nonzero,
4028 virtual bases of TYPE are examined. */
4030 static int
4031 layout_conflict_p (tree type,
4032 tree offset,
4033 splay_tree offsets,
4034 int vbases_p)
4036 splay_tree_node max_node;
4038 /* Get the node in OFFSETS that indicates the maximum offset where
4039 an empty subobject is located. */
4040 max_node = splay_tree_max (offsets);
4041 /* If there aren't any empty subobjects, then there's no point in
4042 performing this check. */
4043 if (!max_node)
4044 return 0;
4046 return walk_subobject_offsets (type, check_subobject_offset, offset,
4047 offsets, (tree) (max_node->key),
4048 vbases_p);
4051 /* DECL is a FIELD_DECL corresponding either to a base subobject of a
4052 non-static data member of the type indicated by RLI. BINFO is the
4053 binfo corresponding to the base subobject, OFFSETS maps offsets to
4054 types already located at those offsets. This function determines
4055 the position of the DECL. */
4057 static void
4058 layout_nonempty_base_or_field (record_layout_info rli,
4059 tree decl,
4060 tree binfo,
4061 splay_tree offsets)
4063 tree offset = NULL_TREE;
4064 bool field_p;
4065 tree type;
4067 if (binfo)
4069 /* For the purposes of determining layout conflicts, we want to
4070 use the class type of BINFO; TREE_TYPE (DECL) will be the
4071 CLASSTYPE_AS_BASE version, which does not contain entries for
4072 zero-sized bases. */
4073 type = TREE_TYPE (binfo);
4074 field_p = false;
4076 else
4078 type = TREE_TYPE (decl);
4079 field_p = true;
4082 /* Try to place the field. It may take more than one try if we have
4083 a hard time placing the field without putting two objects of the
4084 same type at the same address. */
4085 while (1)
4087 struct record_layout_info_s old_rli = *rli;
4089 /* Place this field. */
4090 place_field (rli, decl);
4091 offset = byte_position (decl);
4093 /* We have to check to see whether or not there is already
4094 something of the same type at the offset we're about to use.
4095 For example, consider:
4097 struct S {};
4098 struct T : public S { int i; };
4099 struct U : public S, public T {};
4101 Here, we put S at offset zero in U. Then, we can't put T at
4102 offset zero -- its S component would be at the same address
4103 as the S we already allocated. So, we have to skip ahead.
4104 Since all data members, including those whose type is an
4105 empty class, have nonzero size, any overlap can happen only
4106 with a direct or indirect base-class -- it can't happen with
4107 a data member. */
4108 /* In a union, overlap is permitted; all members are placed at
4109 offset zero. */
4110 if (TREE_CODE (rli->t) == UNION_TYPE)
4111 break;
4112 if (layout_conflict_p (field_p ? type : binfo, offset,
4113 offsets, field_p))
4115 /* Strip off the size allocated to this field. That puts us
4116 at the first place we could have put the field with
4117 proper alignment. */
4118 *rli = old_rli;
4120 /* Bump up by the alignment required for the type. */
4121 rli->bitpos
4122 = size_binop (PLUS_EXPR, rli->bitpos,
4123 bitsize_int (binfo
4124 ? CLASSTYPE_ALIGN (type)
4125 : TYPE_ALIGN (type)));
4126 normalize_rli (rli);
4128 else if (TREE_CODE (type) == NULLPTR_TYPE
4129 && warn_abi && abi_version_crosses (9))
4131 /* Before ABI v9, we were giving nullptr_t alignment of 1; if
4132 the offset wasn't aligned like a pointer when we started to
4133 layout this field, that affects its position. */
4134 tree pos = rli_size_unit_so_far (&old_rli);
4135 if (int_cst_value (pos) % TYPE_ALIGN_UNIT (ptr_type_node) != 0)
4137 if (abi_version_at_least (9))
4138 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wabi,
4139 "alignment of %qD increased in -fabi-version=9 "
4140 "(GCC 5.2)", decl);
4141 else
4142 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wabi, "alignment "
4143 "of %qD will increase in -fabi-version=9", decl);
4145 break;
4147 else
4148 /* There was no conflict. We're done laying out this field. */
4149 break;
4152 /* Now that we know where it will be placed, update its
4153 BINFO_OFFSET. */
4154 if (binfo && CLASS_TYPE_P (BINFO_TYPE (binfo)))
4155 /* Indirect virtual bases may have a nonzero BINFO_OFFSET at
4156 this point because their BINFO_OFFSET is copied from another
4157 hierarchy. Therefore, we may not need to add the entire
4158 OFFSET. */
4159 propagate_binfo_offsets (binfo,
4160 size_diffop_loc (input_location,
4161 fold_convert (ssizetype, offset),
4162 fold_convert (ssizetype,
4163 BINFO_OFFSET (binfo))));
4166 /* Returns true if TYPE is empty and OFFSET is nonzero. */
4168 static int
4169 empty_base_at_nonzero_offset_p (tree type,
4170 tree offset,
4171 splay_tree /*offsets*/)
4173 return is_empty_class (type) && !integer_zerop (offset);
4176 /* Layout the empty base BINFO. EOC indicates the byte currently just
4177 past the end of the class, and should be correctly aligned for a
4178 class of the type indicated by BINFO; OFFSETS gives the offsets of
4179 the empty bases allocated so far. T is the most derived
4180 type. Return nonzero iff we added it at the end. */
4182 static bool
4183 layout_empty_base_or_field (record_layout_info rli, tree binfo_or_decl,
4184 splay_tree offsets)
4186 tree alignment;
4187 bool atend = false;
4188 tree binfo = NULL_TREE;
4189 tree decl = NULL_TREE;
4190 tree type;
4191 if (TREE_CODE (binfo_or_decl) == TREE_BINFO)
4193 binfo = binfo_or_decl;
4194 type = BINFO_TYPE (binfo);
4196 else
4198 decl = binfo_or_decl;
4199 type = TREE_TYPE (decl);
4202 /* On some platforms (ARM), even empty classes will not be
4203 byte-aligned. */
4204 tree eoc = round_up_loc (input_location,
4205 rli_size_unit_so_far (rli),
4206 CLASSTYPE_ALIGN_UNIT (type));
4208 /* This routine should only be used for empty classes. */
4209 gcc_assert (is_empty_class (type));
4210 alignment = size_int (CLASSTYPE_ALIGN_UNIT (type));
4212 /* This is an empty base class. We first try to put it at offset
4213 zero. */
4214 tree offset = size_zero_node;
4215 if (layout_conflict_p (type,
4216 offset,
4217 offsets,
4218 /*vbases_p=*/0))
4220 /* That didn't work. Now, we move forward from the next
4221 available spot in the class. */
4222 atend = true;
4223 offset = eoc;
4224 while (1)
4226 if (!layout_conflict_p (type,
4227 offset,
4228 offsets,
4229 /*vbases_p=*/0))
4230 /* We finally found a spot where there's no overlap. */
4231 break;
4233 /* There's overlap here, too. Bump along to the next spot. */
4234 offset = size_binop (PLUS_EXPR, offset, alignment);
4238 if (CLASSTYPE_USER_ALIGN (type))
4240 rli->record_align = MAX (rli->record_align, CLASSTYPE_ALIGN (type));
4241 if (warn_packed)
4242 rli->unpacked_align = MAX (rli->unpacked_align, CLASSTYPE_ALIGN (type));
4243 TYPE_USER_ALIGN (rli->t) = 1;
4246 if (binfo)
4247 /* Adjust BINFO_OFFSET (binfo) to be exactly OFFSET. */
4248 propagate_binfo_offsets (binfo,
4249 size_diffop (offset, BINFO_OFFSET (binfo)));
4250 else
4252 DECL_FIELD_OFFSET (decl) = offset;
4253 DECL_FIELD_BIT_OFFSET (decl) = bitsize_zero_node;
4254 SET_DECL_OFFSET_ALIGN (decl, BITS_PER_UNIT);
4257 return atend;
4260 /* Build the FIELD_DECL for BASETYPE as a base of T, add it to the chain of
4261 fields at NEXT_FIELD, and return it. */
4263 static tree
4264 build_base_field_1 (tree t, tree basetype, tree *&next_field)
4266 /* Create the FIELD_DECL. */
4267 gcc_assert (CLASSTYPE_AS_BASE (basetype));
4268 tree decl = build_decl (input_location,
4269 FIELD_DECL, NULL_TREE, CLASSTYPE_AS_BASE (basetype));
4270 DECL_ARTIFICIAL (decl) = 1;
4271 DECL_IGNORED_P (decl) = 1;
4272 DECL_FIELD_CONTEXT (decl) = t;
4273 if (is_empty_class (basetype))
4274 /* CLASSTYPE_SIZE is one byte, but the field needs to have size zero. */
4275 DECL_SIZE (decl) = DECL_SIZE_UNIT (decl) = size_zero_node;
4276 else
4278 DECL_SIZE (decl) = CLASSTYPE_SIZE (basetype);
4279 DECL_SIZE_UNIT (decl) = CLASSTYPE_SIZE_UNIT (basetype);
4281 SET_DECL_ALIGN (decl, CLASSTYPE_ALIGN (basetype));
4282 DECL_USER_ALIGN (decl) = CLASSTYPE_USER_ALIGN (basetype);
4283 SET_DECL_MODE (decl, TYPE_MODE (basetype));
4284 DECL_FIELD_IS_BASE (decl) = 1;
4286 /* Add the new FIELD_DECL to the list of fields for T. */
4287 DECL_CHAIN (decl) = *next_field;
4288 *next_field = decl;
4289 next_field = &DECL_CHAIN (decl);
4291 return decl;
4294 /* Layout the base given by BINFO in the class indicated by RLI.
4295 *BASE_ALIGN is a running maximum of the alignments of
4296 any base class. OFFSETS gives the location of empty base
4297 subobjects. T is the most derived type. Return nonzero if the new
4298 object cannot be nearly-empty. A new FIELD_DECL is inserted at
4299 *NEXT_FIELD, unless BINFO is for an empty base class.
4301 Returns the location at which the next field should be inserted. */
4303 static tree *
4304 build_base_field (record_layout_info rli, tree binfo,
4305 splay_tree offsets, tree *next_field)
4307 tree t = rli->t;
4308 tree basetype = BINFO_TYPE (binfo);
4310 if (!COMPLETE_TYPE_P (basetype))
4311 /* This error is now reported in xref_tag, thus giving better
4312 location information. */
4313 return next_field;
4315 /* Place the base class. */
4316 if (!is_empty_class (basetype))
4318 tree decl;
4320 /* The containing class is non-empty because it has a non-empty
4321 base class. */
4322 CLASSTYPE_EMPTY_P (t) = 0;
4324 /* Create the FIELD_DECL. */
4325 decl = build_base_field_1 (t, basetype, next_field);
4327 /* Try to place the field. It may take more than one try if we
4328 have a hard time placing the field without putting two
4329 objects of the same type at the same address. */
4330 layout_nonempty_base_or_field (rli, decl, binfo, offsets);
4332 else
4334 bool atend = layout_empty_base_or_field (rli, binfo, offsets);
4335 /* A nearly-empty class "has no proper base class that is empty,
4336 not morally virtual, and at an offset other than zero." */
4337 if (!BINFO_VIRTUAL_P (binfo) && CLASSTYPE_NEARLY_EMPTY_P (t))
4339 if (atend)
4340 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
4341 /* The check above (used in G++ 3.2) is insufficient because
4342 an empty class placed at offset zero might itself have an
4343 empty base at a nonzero offset. */
4344 else if (walk_subobject_offsets (basetype,
4345 empty_base_at_nonzero_offset_p,
4346 size_zero_node,
4347 /*offsets=*/NULL,
4348 /*max_offset=*/NULL_TREE,
4349 /*vbases_p=*/true))
4350 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
4353 /* We used to not create a FIELD_DECL for empty base classes because of
4354 back end issues with overlapping FIELD_DECLs, but that doesn't seem to
4355 be a problem anymore. We need them to handle initialization of C++17
4356 aggregate bases. */
4357 if (cxx_dialect >= cxx17 && !BINFO_VIRTUAL_P (binfo))
4359 tree decl = build_base_field_1 (t, basetype, next_field);
4360 DECL_FIELD_OFFSET (decl) = BINFO_OFFSET (binfo);
4361 DECL_FIELD_BIT_OFFSET (decl) = bitsize_zero_node;
4362 SET_DECL_OFFSET_ALIGN (decl, BITS_PER_UNIT);
4365 /* An empty virtual base causes a class to be non-empty
4366 -- but in that case we do not need to clear CLASSTYPE_EMPTY_P
4367 here because that was already done when the virtual table
4368 pointer was created. */
4371 /* Record the offsets of BINFO and its base subobjects. */
4372 record_subobject_offsets (binfo, offsets);
4374 return next_field;
4377 /* Layout all of the non-virtual base classes. Record empty
4378 subobjects in OFFSETS. T is the most derived type. Return nonzero
4379 if the type cannot be nearly empty. The fields created
4380 corresponding to the base classes will be inserted at
4381 *NEXT_FIELD. */
4383 static void
4384 build_base_fields (record_layout_info rli,
4385 splay_tree offsets, tree *next_field)
4387 /* Chain to hold all the new FIELD_DECLs which stand in for base class
4388 subobjects. */
4389 tree t = rli->t;
4390 int n_baseclasses = BINFO_N_BASE_BINFOS (TYPE_BINFO (t));
4391 int i;
4393 /* The primary base class is always allocated first. */
4394 if (CLASSTYPE_HAS_PRIMARY_BASE_P (t))
4395 next_field = build_base_field (rli, CLASSTYPE_PRIMARY_BINFO (t),
4396 offsets, next_field);
4398 /* Now allocate the rest of the bases. */
4399 for (i = 0; i < n_baseclasses; ++i)
4401 tree base_binfo;
4403 base_binfo = BINFO_BASE_BINFO (TYPE_BINFO (t), i);
4405 /* The primary base was already allocated above, so we don't
4406 need to allocate it again here. */
4407 if (base_binfo == CLASSTYPE_PRIMARY_BINFO (t))
4408 continue;
4410 /* Virtual bases are added at the end (a primary virtual base
4411 will have already been added). */
4412 if (BINFO_VIRTUAL_P (base_binfo))
4413 continue;
4415 next_field = build_base_field (rli, base_binfo,
4416 offsets, next_field);
4420 /* Go through the TYPE_FIELDS of T issuing any appropriate
4421 diagnostics, figuring out which methods override which other
4422 methods, and so forth. */
4424 static void
4425 check_methods (tree t)
4427 for (tree x = TYPE_FIELDS (t); x; x = DECL_CHAIN (x))
4428 if (DECL_DECLARES_FUNCTION_P (x))
4430 check_for_override (x, t);
4432 if (DECL_PURE_VIRTUAL_P (x)
4433 && (TREE_CODE (x) != FUNCTION_DECL || ! DECL_VINDEX (x)))
4434 error ("initializer specified for non-virtual method %q+D", x);
4435 /* The name of the field is the original field name
4436 Save this in auxiliary field for later overloading. */
4437 if (TREE_CODE (x) == FUNCTION_DECL && DECL_VINDEX (x))
4439 TYPE_POLYMORPHIC_P (t) = 1;
4440 if (DECL_PURE_VIRTUAL_P (x))
4441 vec_safe_push (CLASSTYPE_PURE_VIRTUALS (t), x);
4444 /* All user-provided destructors are non-trivial.
4445 Constructors and assignment ops are handled in
4446 grok_special_member_properties. */
4447 if (DECL_DESTRUCTOR_P (x) && user_provided_p (x))
4448 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t) = 1;
4449 if (!DECL_VIRTUAL_P (x)
4450 && lookup_attribute ("transaction_safe_dynamic",
4451 DECL_ATTRIBUTES (x)))
4452 error_at (DECL_SOURCE_LOCATION (x),
4453 "%<transaction_safe_dynamic%> may only be specified for "
4454 "a virtual function");
4458 /* FN is a constructor or destructor. Clone the declaration to create
4459 a specialized in-charge or not-in-charge version, as indicated by
4460 NAME. */
4462 static tree
4463 build_clone (tree fn, tree name)
4465 tree parms;
4466 tree clone;
4468 /* Copy the function. */
4469 clone = copy_decl (fn);
4470 /* Reset the function name. */
4471 DECL_NAME (clone) = name;
4472 /* Remember where this function came from. */
4473 DECL_ABSTRACT_ORIGIN (clone) = fn;
4474 /* Make it easy to find the CLONE given the FN. */
4475 DECL_CHAIN (clone) = DECL_CHAIN (fn);
4476 DECL_CHAIN (fn) = clone;
4478 /* If this is a template, do the rest on the DECL_TEMPLATE_RESULT. */
4479 if (TREE_CODE (clone) == TEMPLATE_DECL)
4481 tree result = build_clone (DECL_TEMPLATE_RESULT (clone), name);
4482 DECL_TEMPLATE_RESULT (clone) = result;
4483 DECL_TEMPLATE_INFO (result) = copy_node (DECL_TEMPLATE_INFO (result));
4484 DECL_TI_TEMPLATE (result) = clone;
4485 TREE_TYPE (clone) = TREE_TYPE (result);
4486 return clone;
4488 else
4490 // Clone constraints.
4491 if (flag_concepts)
4492 if (tree ci = get_constraints (fn))
4493 set_constraints (clone, copy_node (ci));
4497 SET_DECL_ASSEMBLER_NAME (clone, NULL_TREE);
4498 DECL_CLONED_FUNCTION (clone) = fn;
4499 /* There's no pending inline data for this function. */
4500 DECL_PENDING_INLINE_INFO (clone) = NULL;
4501 DECL_PENDING_INLINE_P (clone) = 0;
4503 /* The base-class destructor is not virtual. */
4504 if (name == base_dtor_identifier)
4506 DECL_VIRTUAL_P (clone) = 0;
4507 if (TREE_CODE (clone) != TEMPLATE_DECL)
4508 DECL_VINDEX (clone) = NULL_TREE;
4511 bool ctor_omit_inherited_parms_p = ctor_omit_inherited_parms (clone);
4512 if (ctor_omit_inherited_parms_p)
4513 gcc_assert (DECL_HAS_IN_CHARGE_PARM_P (clone));
4515 /* If there was an in-charge parameter, drop it from the function
4516 type. */
4517 if (DECL_HAS_IN_CHARGE_PARM_P (clone))
4519 tree basetype = TYPE_METHOD_BASETYPE (TREE_TYPE (clone));
4520 tree parmtypes = TYPE_ARG_TYPES (TREE_TYPE (clone));
4521 /* Skip the `this' parameter. */
4522 parmtypes = TREE_CHAIN (parmtypes);
4523 /* Skip the in-charge parameter. */
4524 parmtypes = TREE_CHAIN (parmtypes);
4525 /* And the VTT parm, in a complete [cd]tor. */
4526 if (DECL_HAS_VTT_PARM_P (fn)
4527 && ! DECL_NEEDS_VTT_PARM_P (clone))
4528 parmtypes = TREE_CHAIN (parmtypes);
4529 if (ctor_omit_inherited_parms_p)
4531 /* If we're omitting inherited parms, that just leaves the VTT. */
4532 gcc_assert (DECL_NEEDS_VTT_PARM_P (clone));
4533 parmtypes = tree_cons (NULL_TREE, vtt_parm_type, void_list_node);
4535 TREE_TYPE (clone)
4536 = build_method_type_directly (basetype,
4537 TREE_TYPE (TREE_TYPE (clone)),
4538 parmtypes);
4539 TREE_TYPE (clone)
4540 = cp_build_type_attribute_variant (TREE_TYPE (clone),
4541 TYPE_ATTRIBUTES (TREE_TYPE (fn)));
4542 TREE_TYPE (clone)
4543 = cxx_copy_lang_qualifiers (TREE_TYPE (clone), TREE_TYPE (fn));
4546 /* Copy the function parameters. */
4547 DECL_ARGUMENTS (clone) = copy_list (DECL_ARGUMENTS (clone));
4548 /* Remove the in-charge parameter. */
4549 if (DECL_HAS_IN_CHARGE_PARM_P (clone))
4551 DECL_CHAIN (DECL_ARGUMENTS (clone))
4552 = DECL_CHAIN (DECL_CHAIN (DECL_ARGUMENTS (clone)));
4553 DECL_HAS_IN_CHARGE_PARM_P (clone) = 0;
4555 /* And the VTT parm, in a complete [cd]tor. */
4556 if (DECL_HAS_VTT_PARM_P (fn))
4558 if (DECL_NEEDS_VTT_PARM_P (clone))
4559 DECL_HAS_VTT_PARM_P (clone) = 1;
4560 else
4562 DECL_CHAIN (DECL_ARGUMENTS (clone))
4563 = DECL_CHAIN (DECL_CHAIN (DECL_ARGUMENTS (clone)));
4564 DECL_HAS_VTT_PARM_P (clone) = 0;
4568 /* A base constructor inheriting from a virtual base doesn't get the
4569 arguments. */
4570 if (ctor_omit_inherited_parms_p)
4571 DECL_CHAIN (DECL_CHAIN (DECL_ARGUMENTS (clone))) = NULL_TREE;
4573 for (parms = DECL_ARGUMENTS (clone); parms; parms = DECL_CHAIN (parms))
4575 DECL_CONTEXT (parms) = clone;
4576 cxx_dup_lang_specific_decl (parms);
4579 /* Create the RTL for this function. */
4580 SET_DECL_RTL (clone, NULL);
4581 rest_of_decl_compilation (clone, /*top_level=*/1, at_eof);
4583 return clone;
4586 /* Implementation of DECL_CLONED_FUNCTION and DECL_CLONED_FUNCTION_P, do
4587 not invoke this function directly.
4589 For a non-thunk function, returns the address of the slot for storing
4590 the function it is a clone of. Otherwise returns NULL_TREE.
4592 If JUST_TESTING, looks through TEMPLATE_DECL and returns NULL if
4593 cloned_function is unset. This is to support the separate
4594 DECL_CLONED_FUNCTION and DECL_CLONED_FUNCTION_P modes; using the latter
4595 on a template makes sense, but not the former. */
4597 tree *
4598 decl_cloned_function_p (const_tree decl, bool just_testing)
4600 tree *ptr;
4601 if (just_testing)
4602 decl = STRIP_TEMPLATE (decl);
4604 if (TREE_CODE (decl) != FUNCTION_DECL
4605 || !DECL_LANG_SPECIFIC (decl)
4606 || DECL_LANG_SPECIFIC (decl)->u.fn.thunk_p)
4608 #if defined ENABLE_TREE_CHECKING && (GCC_VERSION >= 2007)
4609 if (!just_testing)
4610 lang_check_failed (__FILE__, __LINE__, __FUNCTION__);
4611 else
4612 #endif
4613 return NULL;
4616 ptr = &DECL_LANG_SPECIFIC (decl)->u.fn.u5.cloned_function;
4617 if (just_testing && *ptr == NULL_TREE)
4618 return NULL;
4619 else
4620 return ptr;
4623 /* Produce declarations for all appropriate clones of FN. If
4624 UPDATE_METHODS is true, the clones are added to the
4625 CLASSTYPE_MEMBER_VEC. */
4627 void
4628 clone_function_decl (tree fn, bool update_methods)
4630 tree clone;
4632 /* Avoid inappropriate cloning. */
4633 if (DECL_CHAIN (fn)
4634 && DECL_CLONED_FUNCTION_P (DECL_CHAIN (fn)))
4635 return;
4637 if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (fn))
4639 /* For each constructor, we need two variants: an in-charge version
4640 and a not-in-charge version. */
4641 clone = build_clone (fn, complete_ctor_identifier);
4642 if (update_methods)
4643 add_method (DECL_CONTEXT (clone), clone, false);
4644 clone = build_clone (fn, base_ctor_identifier);
4645 if (update_methods)
4646 add_method (DECL_CONTEXT (clone), clone, false);
4648 else
4650 gcc_assert (DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (fn));
4652 /* For each destructor, we need three variants: an in-charge
4653 version, a not-in-charge version, and an in-charge deleting
4654 version. We clone the deleting version first because that
4655 means it will go second on the TYPE_FIELDS list -- and that
4656 corresponds to the correct layout order in the virtual
4657 function table.
4659 For a non-virtual destructor, we do not build a deleting
4660 destructor. */
4661 if (DECL_VIRTUAL_P (fn))
4663 clone = build_clone (fn, deleting_dtor_identifier);
4664 if (update_methods)
4665 add_method (DECL_CONTEXT (clone), clone, false);
4667 clone = build_clone (fn, complete_dtor_identifier);
4668 if (update_methods)
4669 add_method (DECL_CONTEXT (clone), clone, false);
4670 clone = build_clone (fn, base_dtor_identifier);
4671 if (update_methods)
4672 add_method (DECL_CONTEXT (clone), clone, false);
4675 /* Note that this is an abstract function that is never emitted. */
4676 DECL_ABSTRACT_P (fn) = true;
4679 /* DECL is an in charge constructor, which is being defined. This will
4680 have had an in class declaration, from whence clones were
4681 declared. An out-of-class definition can specify additional default
4682 arguments. As it is the clones that are involved in overload
4683 resolution, we must propagate the information from the DECL to its
4684 clones. */
4686 void
4687 adjust_clone_args (tree decl)
4689 tree clone;
4691 for (clone = DECL_CHAIN (decl); clone && DECL_CLONED_FUNCTION_P (clone);
4692 clone = DECL_CHAIN (clone))
4694 tree orig_clone_parms = TYPE_ARG_TYPES (TREE_TYPE (clone));
4695 tree orig_decl_parms = TYPE_ARG_TYPES (TREE_TYPE (decl));
4696 tree decl_parms, clone_parms;
4698 clone_parms = orig_clone_parms;
4700 /* Skip the 'this' parameter. */
4701 orig_clone_parms = TREE_CHAIN (orig_clone_parms);
4702 orig_decl_parms = TREE_CHAIN (orig_decl_parms);
4704 if (DECL_HAS_IN_CHARGE_PARM_P (decl))
4705 orig_decl_parms = TREE_CHAIN (orig_decl_parms);
4706 if (DECL_HAS_VTT_PARM_P (decl))
4707 orig_decl_parms = TREE_CHAIN (orig_decl_parms);
4709 clone_parms = orig_clone_parms;
4710 if (DECL_HAS_VTT_PARM_P (clone))
4711 clone_parms = TREE_CHAIN (clone_parms);
4713 for (decl_parms = orig_decl_parms; decl_parms;
4714 decl_parms = TREE_CHAIN (decl_parms),
4715 clone_parms = TREE_CHAIN (clone_parms))
4717 if (clone_parms == void_list_node)
4719 gcc_assert (decl_parms == clone_parms
4720 || ctor_omit_inherited_parms (clone));
4721 break;
4724 gcc_assert (same_type_p (TREE_TYPE (decl_parms),
4725 TREE_TYPE (clone_parms)));
4727 if (TREE_PURPOSE (decl_parms) && !TREE_PURPOSE (clone_parms))
4729 /* A default parameter has been added. Adjust the
4730 clone's parameters. */
4731 clone_parms = orig_decl_parms;
4733 if (DECL_HAS_VTT_PARM_P (clone))
4735 clone_parms = tree_cons (TREE_PURPOSE (orig_clone_parms),
4736 TREE_VALUE (orig_clone_parms),
4737 clone_parms);
4738 TREE_TYPE (clone_parms) = TREE_TYPE (orig_clone_parms);
4741 tree basetype = TYPE_METHOD_BASETYPE (TREE_TYPE (clone));
4742 tree type
4743 = build_method_type_directly (basetype,
4744 TREE_TYPE (TREE_TYPE (clone)),
4745 clone_parms);
4746 if (tree attrs = TYPE_ATTRIBUTES (TREE_TYPE (clone)))
4747 type = cp_build_type_attribute_variant (type, attrs);
4748 type = cxx_copy_lang_qualifiers (type, TREE_TYPE (clone));
4749 TREE_TYPE (clone) = type;
4751 clone_parms = NULL_TREE;
4752 break;
4755 gcc_assert (!clone_parms || clone_parms == void_list_node);
4759 /* For each of the constructors and destructors in T, create an
4760 in-charge and not-in-charge variant. */
4762 static void
4763 clone_constructors_and_destructors (tree t)
4765 /* While constructors can be via a using declaration, at this point
4766 we no longer need to know that. */
4767 for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (t)); iter; ++iter)
4768 clone_function_decl (*iter, /*update_methods=*/true);
4770 if (tree dtor = CLASSTYPE_DESTRUCTOR (t))
4771 clone_function_decl (dtor, /*update_methods=*/true);
4774 /* Deduce noexcept for a destructor DTOR. */
4776 void
4777 deduce_noexcept_on_destructor (tree dtor)
4779 if (!TYPE_RAISES_EXCEPTIONS (TREE_TYPE (dtor)))
4780 TREE_TYPE (dtor) = build_exception_variant (TREE_TYPE (dtor),
4781 noexcept_deferred_spec);
4784 /* Subroutine of set_one_vmethod_tm_attributes. Search base classes
4785 of TYPE for virtual functions which FNDECL overrides. Return a
4786 mask of the tm attributes found therein. */
4788 static int
4789 look_for_tm_attr_overrides (tree type, tree fndecl)
4791 tree binfo = TYPE_BINFO (type);
4792 tree base_binfo;
4793 int ix, found = 0;
4795 for (ix = 0; BINFO_BASE_ITERATE (binfo, ix, base_binfo); ++ix)
4797 tree o, basetype = BINFO_TYPE (base_binfo);
4799 if (!TYPE_POLYMORPHIC_P (basetype))
4800 continue;
4802 o = look_for_overrides_here (basetype, fndecl);
4803 if (o)
4805 if (lookup_attribute ("transaction_safe_dynamic",
4806 DECL_ATTRIBUTES (o)))
4807 /* transaction_safe_dynamic is not inherited. */;
4808 else
4809 found |= tm_attr_to_mask (find_tm_attribute
4810 (TYPE_ATTRIBUTES (TREE_TYPE (o))));
4812 else
4813 found |= look_for_tm_attr_overrides (basetype, fndecl);
4816 return found;
4819 /* Subroutine of set_method_tm_attributes. Handle the checks and
4820 inheritance for one virtual method FNDECL. */
4822 static void
4823 set_one_vmethod_tm_attributes (tree type, tree fndecl)
4825 tree tm_attr;
4826 int found, have;
4828 found = look_for_tm_attr_overrides (type, fndecl);
4830 /* If FNDECL doesn't actually override anything (i.e. T is the
4831 class that first declares FNDECL virtual), then we're done. */
4832 if (found == 0)
4833 return;
4835 tm_attr = find_tm_attribute (TYPE_ATTRIBUTES (TREE_TYPE (fndecl)));
4836 have = tm_attr_to_mask (tm_attr);
4838 /* Intel STM Language Extension 3.0, Section 4.2 table 4:
4839 tm_pure must match exactly, otherwise no weakening of
4840 tm_safe > tm_callable > nothing. */
4841 /* ??? The tm_pure attribute didn't make the transition to the
4842 multivendor language spec. */
4843 if (have == TM_ATTR_PURE)
4845 if (found != TM_ATTR_PURE)
4847 found &= -found;
4848 goto err_override;
4851 /* If the overridden function is tm_pure, then FNDECL must be. */
4852 else if (found == TM_ATTR_PURE && tm_attr)
4853 goto err_override;
4854 /* Look for base class combinations that cannot be satisfied. */
4855 else if (found != TM_ATTR_PURE && (found & TM_ATTR_PURE))
4857 found &= ~TM_ATTR_PURE;
4858 found &= -found;
4859 error_at (DECL_SOURCE_LOCATION (fndecl),
4860 "method overrides both %<transaction_pure%> and %qE methods",
4861 tm_mask_to_attr (found));
4863 /* If FNDECL did not declare an attribute, then inherit the most
4864 restrictive one. */
4865 else if (tm_attr == NULL)
4867 apply_tm_attr (fndecl, tm_mask_to_attr (least_bit_hwi (found)));
4869 /* Otherwise validate that we're not weaker than a function
4870 that is being overridden. */
4871 else
4873 found &= -found;
4874 if (found <= TM_ATTR_CALLABLE && have > found)
4875 goto err_override;
4877 return;
4879 err_override:
4880 error_at (DECL_SOURCE_LOCATION (fndecl),
4881 "method declared %qE overriding %qE method",
4882 tm_attr, tm_mask_to_attr (found));
4885 /* For each of the methods in T, propagate a class-level tm attribute. */
4887 static void
4888 set_method_tm_attributes (tree t)
4890 tree class_tm_attr, fndecl;
4892 /* Don't bother collecting tm attributes if transactional memory
4893 support is not enabled. */
4894 if (!flag_tm)
4895 return;
4897 /* Process virtual methods first, as they inherit directly from the
4898 base virtual function and also require validation of new attributes. */
4899 if (TYPE_CONTAINS_VPTR_P (t))
4901 tree vchain;
4902 for (vchain = BINFO_VIRTUALS (TYPE_BINFO (t)); vchain;
4903 vchain = TREE_CHAIN (vchain))
4905 fndecl = BV_FN (vchain);
4906 if (DECL_THUNK_P (fndecl))
4907 fndecl = THUNK_TARGET (fndecl);
4908 set_one_vmethod_tm_attributes (t, fndecl);
4912 /* If the class doesn't have an attribute, nothing more to do. */
4913 class_tm_attr = find_tm_attribute (TYPE_ATTRIBUTES (t));
4914 if (class_tm_attr == NULL)
4915 return;
4917 /* Any method that does not yet have a tm attribute inherits
4918 the one from the class. */
4919 for (fndecl = TYPE_FIELDS (t); fndecl; fndecl = DECL_CHAIN (fndecl))
4920 if (DECL_DECLARES_FUNCTION_P (fndecl)
4921 && !find_tm_attribute (TYPE_ATTRIBUTES (TREE_TYPE (fndecl))))
4922 apply_tm_attr (fndecl, class_tm_attr);
4925 /* Returns true if FN is a default constructor. */
4927 bool
4928 default_ctor_p (tree fn)
4930 return (DECL_CONSTRUCTOR_P (fn)
4931 && sufficient_parms_p (FUNCTION_FIRST_USER_PARMTYPE (fn)));
4934 /* Returns true iff class T has a user-provided constructor that can be called
4935 with more than zero arguments. */
4937 bool
4938 type_has_user_nondefault_constructor (tree t)
4940 if (!TYPE_HAS_USER_CONSTRUCTOR (t))
4941 return false;
4943 for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (t)); iter; ++iter)
4945 tree fn = *iter;
4946 if (user_provided_p (fn)
4947 && (TREE_CODE (fn) == TEMPLATE_DECL
4948 || (skip_artificial_parms_for (fn, DECL_ARGUMENTS (fn))
4949 != NULL_TREE)))
4950 return true;
4953 return false;
4956 /* Returns the defaulted constructor if T has one. Otherwise, returns
4957 NULL_TREE. */
4959 tree
4960 in_class_defaulted_default_constructor (tree t)
4962 if (!TYPE_HAS_USER_CONSTRUCTOR (t))
4963 return NULL_TREE;
4965 for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (t)); iter; ++iter)
4967 tree fn = *iter;
4969 if (DECL_DEFAULTED_IN_CLASS_P (fn)
4970 && default_ctor_p (fn))
4971 return fn;
4974 return NULL_TREE;
4977 /* Returns true iff FN is a user-provided function, i.e. user-declared
4978 and not defaulted at its first declaration. */
4980 bool
4981 user_provided_p (tree fn)
4983 if (TREE_CODE (fn) == TEMPLATE_DECL)
4984 return true;
4985 else
4986 return (!DECL_ARTIFICIAL (fn)
4987 && !(DECL_INITIALIZED_IN_CLASS_P (fn)
4988 && (DECL_DEFAULTED_FN (fn) || DECL_DELETED_FN (fn))));
4991 /* Returns true iff class T has a user-provided constructor. */
4993 bool
4994 type_has_user_provided_constructor (tree t)
4996 if (!CLASS_TYPE_P (t))
4997 return false;
4999 if (!TYPE_HAS_USER_CONSTRUCTOR (t))
5000 return false;
5002 for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (t)); iter; ++iter)
5003 if (user_provided_p (*iter))
5004 return true;
5006 return false;
5009 /* Returns true iff class T has a user-provided or explicit constructor. */
5011 bool
5012 type_has_user_provided_or_explicit_constructor (tree t)
5014 if (!CLASS_TYPE_P (t))
5015 return false;
5017 if (!TYPE_HAS_USER_CONSTRUCTOR (t))
5018 return false;
5020 for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (t)); iter; ++iter)
5022 tree fn = *iter;
5023 if (user_provided_p (fn) || DECL_NONCONVERTING_P (fn))
5024 return true;
5027 return false;
5030 /* Returns true iff class T has a non-user-provided (i.e. implicitly
5031 declared or explicitly defaulted in the class body) default
5032 constructor. */
5034 bool
5035 type_has_non_user_provided_default_constructor (tree t)
5037 if (!TYPE_HAS_DEFAULT_CONSTRUCTOR (t))
5038 return false;
5039 if (CLASSTYPE_LAZY_DEFAULT_CTOR (t))
5040 return true;
5042 for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (t)); iter; ++iter)
5044 tree fn = *iter;
5045 if (TREE_CODE (fn) == FUNCTION_DECL
5046 && default_ctor_p (fn)
5047 && !user_provided_p (fn))
5048 return true;
5051 return false;
5054 /* TYPE is being used as a virtual base, and has a non-trivial move
5055 assignment. Return true if this is due to there being a user-provided
5056 move assignment in TYPE or one of its subobjects; if there isn't, then
5057 multiple move assignment can't cause any harm. */
5059 bool
5060 vbase_has_user_provided_move_assign (tree type)
5062 /* Does the type itself have a user-provided move assignment operator? */
5063 if (!CLASSTYPE_LAZY_MOVE_ASSIGN (type))
5064 for (ovl_iterator iter (get_class_binding_direct
5065 (type, assign_op_identifier));
5066 iter; ++iter)
5067 if (user_provided_p (*iter) && move_fn_p (*iter))
5068 return true;
5070 /* Do any of its bases? */
5071 tree binfo = TYPE_BINFO (type);
5072 tree base_binfo;
5073 for (int i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
5074 if (vbase_has_user_provided_move_assign (BINFO_TYPE (base_binfo)))
5075 return true;
5077 /* Or non-static data members? */
5078 for (tree field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
5080 if (TREE_CODE (field) == FIELD_DECL
5081 && CLASS_TYPE_P (TREE_TYPE (field))
5082 && vbase_has_user_provided_move_assign (TREE_TYPE (field)))
5083 return true;
5086 /* Seems not. */
5087 return false;
5090 /* If default-initialization leaves part of TYPE uninitialized, returns
5091 a DECL for the field or TYPE itself (DR 253). */
5093 tree
5094 default_init_uninitialized_part (tree type)
5096 tree t, r, binfo;
5097 int i;
5099 type = strip_array_types (type);
5100 if (!CLASS_TYPE_P (type))
5101 return type;
5102 if (!type_has_non_user_provided_default_constructor (type))
5103 return NULL_TREE;
5104 for (binfo = TYPE_BINFO (type), i = 0;
5105 BINFO_BASE_ITERATE (binfo, i, t); ++i)
5107 r = default_init_uninitialized_part (BINFO_TYPE (t));
5108 if (r)
5109 return r;
5111 for (t = TYPE_FIELDS (type); t; t = DECL_CHAIN (t))
5112 if (TREE_CODE (t) == FIELD_DECL
5113 && !DECL_ARTIFICIAL (t)
5114 && !DECL_INITIAL (t))
5116 r = default_init_uninitialized_part (TREE_TYPE (t));
5117 if (r)
5118 return DECL_P (r) ? r : t;
5121 return NULL_TREE;
5124 /* Returns true iff for class T, a trivial synthesized default constructor
5125 would be constexpr. */
5127 bool
5128 trivial_default_constructor_is_constexpr (tree t)
5130 /* A defaulted trivial default constructor is constexpr
5131 if there is nothing to initialize. */
5132 gcc_assert (!TYPE_HAS_COMPLEX_DFLT (t));
5133 return is_really_empty_class (t);
5136 /* Returns true iff class T has a constexpr default constructor. */
5138 bool
5139 type_has_constexpr_default_constructor (tree t)
5141 tree fns;
5143 if (!CLASS_TYPE_P (t))
5145 /* The caller should have stripped an enclosing array. */
5146 gcc_assert (TREE_CODE (t) != ARRAY_TYPE);
5147 return false;
5149 if (CLASSTYPE_LAZY_DEFAULT_CTOR (t))
5151 if (!TYPE_HAS_COMPLEX_DFLT (t))
5152 return trivial_default_constructor_is_constexpr (t);
5153 /* Non-trivial, we need to check subobject constructors. */
5154 lazily_declare_fn (sfk_constructor, t);
5156 fns = locate_ctor (t);
5157 return (fns && DECL_DECLARED_CONSTEXPR_P (fns));
5160 /* Returns true iff class T has a constexpr default constructor or has an
5161 implicitly declared default constructor that we can't tell if it's constexpr
5162 without forcing a lazy declaration (which might cause undesired
5163 instantiations). */
5165 bool
5166 type_maybe_constexpr_default_constructor (tree t)
5168 if (CLASS_TYPE_P (t) && CLASSTYPE_LAZY_DEFAULT_CTOR (t)
5169 && TYPE_HAS_COMPLEX_DFLT (t))
5170 /* Assume it's constexpr. */
5171 return true;
5172 return type_has_constexpr_default_constructor (t);
5175 /* Returns true iff class TYPE has a virtual destructor. */
5177 bool
5178 type_has_virtual_destructor (tree type)
5180 tree dtor;
5182 if (!CLASS_TYPE_P (type))
5183 return false;
5185 gcc_assert (COMPLETE_TYPE_P (type));
5186 dtor = CLASSTYPE_DESTRUCTOR (type);
5187 return (dtor && DECL_VIRTUAL_P (dtor));
5190 /* Returns true iff T, a class, has a move-assignment or
5191 move-constructor. Does not lazily declare either.
5192 If USER_P is false, any move function will do. If it is true, the
5193 move function must be user-declared.
5195 Note that user-declared here is different from "user-provided",
5196 which doesn't include functions that are defaulted in the
5197 class. */
5199 bool
5200 classtype_has_move_assign_or_move_ctor_p (tree t, bool user_p)
5202 gcc_assert (user_p
5203 || (!CLASSTYPE_LAZY_MOVE_CTOR (t)
5204 && !CLASSTYPE_LAZY_MOVE_ASSIGN (t)));
5206 if (!CLASSTYPE_LAZY_MOVE_CTOR (t))
5207 for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (t)); iter; ++iter)
5208 if ((!user_p || !DECL_ARTIFICIAL (*iter)) && move_fn_p (*iter))
5209 return true;
5211 if (!CLASSTYPE_LAZY_MOVE_ASSIGN (t))
5212 for (ovl_iterator iter (get_class_binding_direct
5213 (t, assign_op_identifier));
5214 iter; ++iter)
5215 if ((!user_p || !DECL_ARTIFICIAL (*iter)) && move_fn_p (*iter))
5216 return true;
5218 return false;
5221 /* True iff T has a move constructor that is not deleted. */
5223 bool
5224 classtype_has_non_deleted_move_ctor (tree t)
5226 if (CLASSTYPE_LAZY_MOVE_CTOR (t))
5227 lazily_declare_fn (sfk_move_constructor, t);
5228 for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (t)); iter; ++iter)
5229 if (move_fn_p (*iter) && !DECL_DELETED_FN (*iter))
5230 return true;
5231 return false;
5234 /* If T, a class, has a user-provided copy constructor, copy assignment
5235 operator, or destructor, returns that function. Otherwise, null. */
5237 tree
5238 classtype_has_user_copy_or_dtor (tree t)
5240 if (!CLASSTYPE_LAZY_COPY_CTOR (t))
5241 for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (t)); iter; ++iter)
5243 tree fn = *iter;
5244 if (user_provided_p (fn) && copy_fn_p (fn))
5245 return fn;
5248 if (!CLASSTYPE_LAZY_COPY_ASSIGN (t))
5249 for (ovl_iterator iter (get_class_binding_direct
5250 (t, assign_op_identifier));
5251 iter; ++iter)
5253 tree fn = *iter;
5254 if (user_provided_p (fn) && copy_fn_p (fn))
5255 return fn;
5258 if (!CLASSTYPE_LAZY_DESTRUCTOR (t))
5260 tree fn = CLASSTYPE_DESTRUCTOR (t);
5261 if (user_provided_p (fn))
5262 return fn;
5265 return NULL_TREE;
5268 /* Nonzero if we need to build up a constructor call when initializing an
5269 object of this class, either because it has a user-declared constructor
5270 or because it doesn't have a default constructor (so we need to give an
5271 error if no initializer is provided). Use TYPE_NEEDS_CONSTRUCTING when
5272 what you care about is whether or not an object can be produced by a
5273 constructor (e.g. so we don't set TREE_READONLY on const variables of
5274 such type); use this function when what you care about is whether or not
5275 to try to call a constructor to create an object. The latter case is
5276 the former plus some cases of constructors that cannot be called. */
5278 bool
5279 type_build_ctor_call (tree t)
5281 tree inner;
5282 if (TYPE_NEEDS_CONSTRUCTING (t))
5283 return true;
5284 inner = strip_array_types (t);
5285 if (!CLASS_TYPE_P (inner) || ANON_AGGR_TYPE_P (inner))
5286 return false;
5287 if (!TYPE_HAS_DEFAULT_CONSTRUCTOR (inner))
5288 return true;
5289 if (cxx_dialect < cxx11)
5290 return false;
5291 /* A user-declared constructor might be private, and a constructor might
5292 be trivial but deleted. */
5293 for (ovl_iterator iter (get_class_binding (inner, complete_ctor_identifier));
5294 iter; ++iter)
5296 tree fn = *iter;
5297 if (!DECL_ARTIFICIAL (fn)
5298 || TREE_DEPRECATED (fn)
5299 || DECL_DELETED_FN (fn))
5300 return true;
5302 return false;
5305 /* Like type_build_ctor_call, but for destructors. */
5307 bool
5308 type_build_dtor_call (tree t)
5310 tree inner;
5311 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t))
5312 return true;
5313 inner = strip_array_types (t);
5314 if (!CLASS_TYPE_P (inner) || ANON_AGGR_TYPE_P (inner)
5315 || !COMPLETE_TYPE_P (inner))
5316 return false;
5317 if (cxx_dialect < cxx11)
5318 return false;
5319 /* A user-declared destructor might be private, and a destructor might
5320 be trivial but deleted. */
5321 for (ovl_iterator iter (get_class_binding (inner, complete_dtor_identifier));
5322 iter; ++iter)
5324 tree fn = *iter;
5325 if (!DECL_ARTIFICIAL (fn)
5326 || TREE_DEPRECATED (fn)
5327 || DECL_DELETED_FN (fn))
5328 return true;
5330 return false;
5333 /* Remove all zero-width bit-fields from T. */
5335 static void
5336 remove_zero_width_bit_fields (tree t)
5338 tree *fieldsp;
5340 fieldsp = &TYPE_FIELDS (t);
5341 while (*fieldsp)
5343 if (TREE_CODE (*fieldsp) == FIELD_DECL
5344 && DECL_C_BIT_FIELD (*fieldsp)
5345 /* We should not be confused by the fact that grokbitfield
5346 temporarily sets the width of the bit field into
5347 DECL_BIT_FIELD_REPRESENTATIVE (*fieldsp).
5348 check_bitfield_decl eventually sets DECL_SIZE (*fieldsp)
5349 to that width. */
5350 && (DECL_SIZE (*fieldsp) == NULL_TREE
5351 || integer_zerop (DECL_SIZE (*fieldsp))))
5352 *fieldsp = DECL_CHAIN (*fieldsp);
5353 else
5354 fieldsp = &DECL_CHAIN (*fieldsp);
5358 /* Returns TRUE iff we need a cookie when dynamically allocating an
5359 array whose elements have the indicated class TYPE. */
5361 static bool
5362 type_requires_array_cookie (tree type)
5364 tree fns;
5365 bool has_two_argument_delete_p = false;
5367 gcc_assert (CLASS_TYPE_P (type));
5369 /* If there's a non-trivial destructor, we need a cookie. In order
5370 to iterate through the array calling the destructor for each
5371 element, we'll have to know how many elements there are. */
5372 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
5373 return true;
5375 /* If the usual deallocation function is a two-argument whose second
5376 argument is of type `size_t', then we have to pass the size of
5377 the array to the deallocation function, so we will need to store
5378 a cookie. */
5379 fns = lookup_fnfields (TYPE_BINFO (type),
5380 ovl_op_identifier (false, VEC_DELETE_EXPR),
5381 /*protect=*/0);
5382 /* If there are no `operator []' members, or the lookup is
5383 ambiguous, then we don't need a cookie. */
5384 if (!fns || fns == error_mark_node)
5385 return false;
5386 /* Loop through all of the functions. */
5387 for (lkp_iterator iter (BASELINK_FUNCTIONS (fns)); iter; ++iter)
5389 tree fn = *iter;
5391 /* See if this function is a one-argument delete function. If
5392 it is, then it will be the usual deallocation function. */
5393 tree second_parm = TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (fn)));
5394 if (second_parm == void_list_node)
5395 return false;
5396 /* Do not consider this function if its second argument is an
5397 ellipsis. */
5398 if (!second_parm)
5399 continue;
5400 /* Otherwise, if we have a two-argument function and the second
5401 argument is `size_t', it will be the usual deallocation
5402 function -- unless there is one-argument function, too. */
5403 if (TREE_CHAIN (second_parm) == void_list_node
5404 && same_type_p (TREE_VALUE (second_parm), size_type_node))
5405 has_two_argument_delete_p = true;
5408 return has_two_argument_delete_p;
5411 /* Finish computing the `literal type' property of class type T.
5413 At this point, we have already processed base classes and
5414 non-static data members. We need to check whether the copy
5415 constructor is trivial, the destructor is trivial, and there
5416 is a trivial default constructor or at least one constexpr
5417 constructor other than the copy constructor. */
5419 static void
5420 finalize_literal_type_property (tree t)
5422 tree fn;
5424 if (cxx_dialect < cxx11
5425 || TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t))
5426 CLASSTYPE_LITERAL_P (t) = false;
5427 else if (CLASSTYPE_LITERAL_P (t) && LAMBDA_TYPE_P (t))
5428 CLASSTYPE_LITERAL_P (t) = (cxx_dialect >= cxx17);
5429 else if (CLASSTYPE_LITERAL_P (t) && !TYPE_HAS_TRIVIAL_DFLT (t)
5430 && CLASSTYPE_NON_AGGREGATE (t)
5431 && !TYPE_HAS_CONSTEXPR_CTOR (t))
5432 CLASSTYPE_LITERAL_P (t) = false;
5434 /* C++14 DR 1684 removed this restriction. */
5435 if (cxx_dialect < cxx14
5436 && !CLASSTYPE_LITERAL_P (t) && !LAMBDA_TYPE_P (t))
5437 for (fn = TYPE_FIELDS (t); fn; fn = DECL_CHAIN (fn))
5438 if (TREE_CODE (fn) == FUNCTION_DECL
5439 && DECL_DECLARED_CONSTEXPR_P (fn)
5440 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
5441 && !DECL_CONSTRUCTOR_P (fn))
5443 DECL_DECLARED_CONSTEXPR_P (fn) = false;
5444 if (!DECL_GENERATED_P (fn))
5446 auto_diagnostic_group d;
5447 if (pedwarn (DECL_SOURCE_LOCATION (fn), OPT_Wpedantic,
5448 "enclosing class of %<constexpr%> non-static "
5449 "member function %q+#D is not a literal type", fn))
5450 explain_non_literal_class (t);
5455 /* T is a non-literal type used in a context which requires a constant
5456 expression. Explain why it isn't literal. */
5458 void
5459 explain_non_literal_class (tree t)
5461 static hash_set<tree> *diagnosed;
5463 if (!CLASS_TYPE_P (t))
5464 return;
5465 t = TYPE_MAIN_VARIANT (t);
5467 if (diagnosed == NULL)
5468 diagnosed = new hash_set<tree>;
5469 if (diagnosed->add (t))
5470 /* Already explained. */
5471 return;
5473 auto_diagnostic_group d;
5474 inform (UNKNOWN_LOCATION, "%q+T is not literal because:", t);
5475 if (cxx_dialect < cxx17 && LAMBDA_TYPE_P (t))
5476 inform (UNKNOWN_LOCATION,
5477 " %qT is a closure type, which is only literal in "
5478 "C++17 and later", t);
5479 else if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t))
5480 inform (UNKNOWN_LOCATION, " %q+T has a non-trivial destructor", t);
5481 else if (CLASSTYPE_NON_AGGREGATE (t)
5482 && !TYPE_HAS_TRIVIAL_DFLT (t)
5483 && !LAMBDA_TYPE_P (t)
5484 && !TYPE_HAS_CONSTEXPR_CTOR (t))
5486 inform (UNKNOWN_LOCATION,
5487 " %q+T is not an aggregate, does not have a trivial "
5488 "default constructor, and has no %<constexpr%> constructor that "
5489 "is not a copy or move constructor", t);
5490 if (type_has_non_user_provided_default_constructor (t))
5491 /* Note that we can't simply call locate_ctor because when the
5492 constructor is deleted it just returns NULL_TREE. */
5493 for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (t)); iter; ++iter)
5495 tree fn = *iter;
5496 tree parms = TYPE_ARG_TYPES (TREE_TYPE (fn));
5498 parms = skip_artificial_parms_for (fn, parms);
5500 if (sufficient_parms_p (parms))
5502 if (DECL_DELETED_FN (fn))
5503 maybe_explain_implicit_delete (fn);
5504 else
5505 explain_invalid_constexpr_fn (fn);
5506 break;
5510 else
5512 tree binfo, base_binfo, field; int i;
5513 for (binfo = TYPE_BINFO (t), i = 0;
5514 BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
5516 tree basetype = TREE_TYPE (base_binfo);
5517 if (!CLASSTYPE_LITERAL_P (basetype))
5519 inform (UNKNOWN_LOCATION,
5520 " base class %qT of %q+T is non-literal",
5521 basetype, t);
5522 explain_non_literal_class (basetype);
5523 return;
5526 for (field = TYPE_FIELDS (t); field; field = TREE_CHAIN (field))
5528 tree ftype;
5529 if (TREE_CODE (field) != FIELD_DECL)
5530 continue;
5531 ftype = TREE_TYPE (field);
5532 if (!literal_type_p (ftype))
5534 inform (DECL_SOURCE_LOCATION (field),
5535 " non-static data member %qD has non-literal type",
5536 field);
5537 if (CLASS_TYPE_P (ftype))
5538 explain_non_literal_class (ftype);
5540 if (CP_TYPE_VOLATILE_P (ftype))
5541 inform (DECL_SOURCE_LOCATION (field),
5542 " non-static data member %qD has volatile type", field);
5547 /* Check the validity of the bases and members declared in T. Add any
5548 implicitly-generated functions (like copy-constructors and
5549 assignment operators). Compute various flag bits (like
5550 CLASSTYPE_NON_LAYOUT_POD_T) for T. This routine works purely at the C++
5551 level: i.e., independently of the ABI in use. */
5553 static void
5554 check_bases_and_members (tree t)
5556 /* Nonzero if the implicitly generated copy constructor should take
5557 a non-const reference argument. */
5558 int cant_have_const_ctor;
5559 /* Nonzero if the implicitly generated assignment operator
5560 should take a non-const reference argument. */
5561 int no_const_asn_ref;
5562 tree access_decls;
5563 bool saved_complex_asn_ref;
5564 bool saved_nontrivial_dtor;
5565 tree fn;
5567 /* By default, we use const reference arguments and generate default
5568 constructors. */
5569 cant_have_const_ctor = 0;
5570 no_const_asn_ref = 0;
5572 /* Check all the base-classes and set FMEM members to point to arrays
5573 of potential interest. */
5574 check_bases (t, &cant_have_const_ctor, &no_const_asn_ref);
5576 /* Deduce noexcept on destructor. This needs to happen after we've set
5577 triviality flags appropriately for our bases. */
5578 if (cxx_dialect >= cxx11)
5579 if (tree dtor = CLASSTYPE_DESTRUCTOR (t))
5580 deduce_noexcept_on_destructor (dtor);
5582 /* Check all the method declarations. */
5583 check_methods (t);
5585 /* Save the initial values of these flags which only indicate whether
5586 or not the class has user-provided functions. As we analyze the
5587 bases and members we can set these flags for other reasons. */
5588 saved_complex_asn_ref = TYPE_HAS_COMPLEX_COPY_ASSIGN (t);
5589 saved_nontrivial_dtor = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t);
5591 /* Check all the data member declarations. We cannot call
5592 check_field_decls until we have called check_bases check_methods,
5593 as check_field_decls depends on TYPE_HAS_NONTRIVIAL_DESTRUCTOR
5594 being set appropriately. */
5595 check_field_decls (t, &access_decls,
5596 &cant_have_const_ctor,
5597 &no_const_asn_ref);
5599 /* A nearly-empty class has to be vptr-containing; a nearly empty
5600 class contains just a vptr. */
5601 if (!TYPE_CONTAINS_VPTR_P (t))
5602 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
5604 /* Do some bookkeeping that will guide the generation of implicitly
5605 declared member functions. */
5606 TYPE_HAS_COMPLEX_COPY_CTOR (t) |= TYPE_CONTAINS_VPTR_P (t);
5607 TYPE_HAS_COMPLEX_MOVE_CTOR (t) |= TYPE_CONTAINS_VPTR_P (t);
5608 /* We need to call a constructor for this class if it has a
5609 user-provided constructor, or if the default constructor is going
5610 to initialize the vptr. (This is not an if-and-only-if;
5611 TYPE_NEEDS_CONSTRUCTING is set elsewhere if bases or members
5612 themselves need constructing.) */
5613 TYPE_NEEDS_CONSTRUCTING (t)
5614 |= (type_has_user_provided_constructor (t) || TYPE_CONTAINS_VPTR_P (t));
5615 /* [dcl.init.aggr]
5617 An aggregate is an array or a class with no user-provided
5618 constructors ... and no virtual functions.
5620 Again, other conditions for being an aggregate are checked
5621 elsewhere. */
5622 CLASSTYPE_NON_AGGREGATE (t)
5623 |= ((cxx_dialect < cxx2a
5624 ? type_has_user_provided_or_explicit_constructor (t)
5625 : TYPE_HAS_USER_CONSTRUCTOR (t))
5626 || TYPE_POLYMORPHIC_P (t));
5627 /* This is the C++98/03 definition of POD; it changed in C++0x, but we
5628 retain the old definition internally for ABI reasons. */
5629 CLASSTYPE_NON_LAYOUT_POD_P (t)
5630 |= (CLASSTYPE_NON_AGGREGATE (t)
5631 || saved_nontrivial_dtor || saved_complex_asn_ref);
5632 CLASSTYPE_NON_STD_LAYOUT (t) |= TYPE_CONTAINS_VPTR_P (t);
5633 TYPE_HAS_COMPLEX_COPY_ASSIGN (t) |= TYPE_CONTAINS_VPTR_P (t);
5634 TYPE_HAS_COMPLEX_MOVE_ASSIGN (t) |= TYPE_CONTAINS_VPTR_P (t);
5635 TYPE_HAS_COMPLEX_DFLT (t) |= TYPE_CONTAINS_VPTR_P (t);
5637 /* If the only explicitly declared default constructor is user-provided,
5638 set TYPE_HAS_COMPLEX_DFLT. */
5639 if (!TYPE_HAS_COMPLEX_DFLT (t)
5640 && TYPE_HAS_DEFAULT_CONSTRUCTOR (t)
5641 && !type_has_non_user_provided_default_constructor (t))
5642 TYPE_HAS_COMPLEX_DFLT (t) = true;
5644 /* Warn if a public base of a polymorphic type has an accessible
5645 non-virtual destructor. It is only now that we know the class is
5646 polymorphic. Although a polymorphic base will have a already
5647 been diagnosed during its definition, we warn on use too. */
5648 if (TYPE_POLYMORPHIC_P (t) && warn_nonvdtor)
5650 tree binfo = TYPE_BINFO (t);
5651 vec<tree, va_gc> *accesses = BINFO_BASE_ACCESSES (binfo);
5652 tree base_binfo;
5653 unsigned i;
5655 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
5657 tree basetype = TREE_TYPE (base_binfo);
5659 if ((*accesses)[i] == access_public_node
5660 && (TYPE_POLYMORPHIC_P (basetype) || warn_ecpp)
5661 && accessible_nvdtor_p (basetype))
5662 warning (OPT_Wnon_virtual_dtor,
5663 "base class %q#T has accessible non-virtual destructor",
5664 basetype);
5668 /* If the class has no user-declared constructor, but does have
5669 non-static const or reference data members that can never be
5670 initialized, issue a warning. */
5671 if (warn_uninitialized
5672 /* Classes with user-declared constructors are presumed to
5673 initialize these members. */
5674 && !TYPE_HAS_USER_CONSTRUCTOR (t)
5675 /* Aggregates can be initialized with brace-enclosed
5676 initializers. */
5677 && CLASSTYPE_NON_AGGREGATE (t))
5679 tree field;
5681 for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
5683 tree type;
5685 if (TREE_CODE (field) != FIELD_DECL
5686 || DECL_INITIAL (field) != NULL_TREE)
5687 continue;
5689 type = TREE_TYPE (field);
5690 if (TYPE_REF_P (type))
5691 warning_at (DECL_SOURCE_LOCATION (field),
5692 OPT_Wuninitialized, "non-static reference %q#D "
5693 "in class without a constructor", field);
5694 else if (CP_TYPE_CONST_P (type)
5695 && (!CLASS_TYPE_P (type)
5696 || !TYPE_HAS_DEFAULT_CONSTRUCTOR (type)))
5697 warning_at (DECL_SOURCE_LOCATION (field),
5698 OPT_Wuninitialized, "non-static const member %q#D "
5699 "in class without a constructor", field);
5703 /* Synthesize any needed methods. */
5704 add_implicitly_declared_members (t, &access_decls,
5705 cant_have_const_ctor,
5706 no_const_asn_ref);
5708 /* Check defaulted declarations here so we have cant_have_const_ctor
5709 and don't need to worry about clones. */
5710 for (fn = TYPE_FIELDS (t); fn; fn = DECL_CHAIN (fn))
5711 if (DECL_DECLARES_FUNCTION_P (fn)
5712 && !DECL_ARTIFICIAL (fn)
5713 && DECL_DEFAULTED_IN_CLASS_P (fn))
5715 int copy = copy_fn_p (fn);
5716 if (copy > 0)
5718 bool imp_const_p
5719 = (DECL_CONSTRUCTOR_P (fn) ? !cant_have_const_ctor
5720 : !no_const_asn_ref);
5721 bool fn_const_p = (copy == 2);
5723 if (fn_const_p && !imp_const_p)
5724 /* If the function is defaulted outside the class, we just
5725 give the synthesis error. Core Issue #1331 says this is
5726 no longer ill-formed, it is defined as deleted instead. */
5727 DECL_DELETED_FN (fn) = true;
5729 defaulted_late_check (fn);
5732 if (LAMBDA_TYPE_P (t))
5734 /* "This class type is not an aggregate." */
5735 CLASSTYPE_NON_AGGREGATE (t) = 1;
5738 /* Compute the 'literal type' property before we
5739 do anything with non-static member functions. */
5740 finalize_literal_type_property (t);
5742 /* Create the in-charge and not-in-charge variants of constructors
5743 and destructors. */
5744 clone_constructors_and_destructors (t);
5746 /* Process the using-declarations. */
5747 for (; access_decls; access_decls = TREE_CHAIN (access_decls))
5748 handle_using_decl (TREE_VALUE (access_decls), t);
5750 /* Figure out whether or not we will need a cookie when dynamically
5751 allocating an array of this type. */
5752 LANG_TYPE_CLASS_CHECK (t)->vec_new_uses_cookie
5753 = type_requires_array_cookie (t);
5756 /* If T needs a pointer to its virtual function table, set TYPE_VFIELD
5757 accordingly. If a new vfield was created (because T doesn't have a
5758 primary base class), then the newly created field is returned. It
5759 is not added to the TYPE_FIELDS list; it is the caller's
5760 responsibility to do that. Accumulate declared virtual functions
5761 on VIRTUALS_P. */
5763 static tree
5764 create_vtable_ptr (tree t, tree* virtuals_p)
5766 tree fn;
5768 /* Collect the virtual functions declared in T. */
5769 for (fn = TYPE_FIELDS (t); fn; fn = DECL_CHAIN (fn))
5770 if (TREE_CODE (fn) == FUNCTION_DECL
5771 && DECL_VINDEX (fn) && !DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (fn)
5772 && TREE_CODE (DECL_VINDEX (fn)) != INTEGER_CST)
5774 tree new_virtual = make_node (TREE_LIST);
5776 BV_FN (new_virtual) = fn;
5777 BV_DELTA (new_virtual) = integer_zero_node;
5778 BV_VCALL_INDEX (new_virtual) = NULL_TREE;
5780 TREE_CHAIN (new_virtual) = *virtuals_p;
5781 *virtuals_p = new_virtual;
5784 /* If we couldn't find an appropriate base class, create a new field
5785 here. Even if there weren't any new virtual functions, we might need a
5786 new virtual function table if we're supposed to include vptrs in
5787 all classes that need them. */
5788 if (!TYPE_VFIELD (t) && (*virtuals_p || TYPE_CONTAINS_VPTR_P (t)))
5790 /* We build this decl with vtbl_ptr_type_node, which is a
5791 `vtable_entry_type*'. It might seem more precise to use
5792 `vtable_entry_type (*)[N]' where N is the number of virtual
5793 functions. However, that would require the vtable pointer in
5794 base classes to have a different type than the vtable pointer
5795 in derived classes. We could make that happen, but that
5796 still wouldn't solve all the problems. In particular, the
5797 type-based alias analysis code would decide that assignments
5798 to the base class vtable pointer can't alias assignments to
5799 the derived class vtable pointer, since they have different
5800 types. Thus, in a derived class destructor, where the base
5801 class constructor was inlined, we could generate bad code for
5802 setting up the vtable pointer.
5804 Therefore, we use one type for all vtable pointers. We still
5805 use a type-correct type; it's just doesn't indicate the array
5806 bounds. That's better than using `void*' or some such; it's
5807 cleaner, and it let's the alias analysis code know that these
5808 stores cannot alias stores to void*! */
5809 tree field;
5811 field = build_decl (input_location,
5812 FIELD_DECL, get_vfield_name (t), vtbl_ptr_type_node);
5813 DECL_VIRTUAL_P (field) = 1;
5814 DECL_ARTIFICIAL (field) = 1;
5815 DECL_FIELD_CONTEXT (field) = t;
5816 DECL_FCONTEXT (field) = t;
5817 if (TYPE_PACKED (t))
5818 DECL_PACKED (field) = 1;
5820 TYPE_VFIELD (t) = field;
5822 /* This class is non-empty. */
5823 CLASSTYPE_EMPTY_P (t) = 0;
5825 return field;
5828 return NULL_TREE;
5831 /* Add OFFSET to all base types of BINFO which is a base in the
5832 hierarchy dominated by T.
5834 OFFSET, which is a type offset, is number of bytes. */
5836 static void
5837 propagate_binfo_offsets (tree binfo, tree offset)
5839 int i;
5840 tree primary_binfo;
5841 tree base_binfo;
5843 /* Update BINFO's offset. */
5844 BINFO_OFFSET (binfo)
5845 = fold_convert (sizetype,
5846 size_binop (PLUS_EXPR,
5847 fold_convert (ssizetype, BINFO_OFFSET (binfo)),
5848 offset));
5850 /* Find the primary base class. */
5851 primary_binfo = get_primary_binfo (binfo);
5853 if (primary_binfo && BINFO_INHERITANCE_CHAIN (primary_binfo) == binfo)
5854 propagate_binfo_offsets (primary_binfo, offset);
5856 /* Scan all of the bases, pushing the BINFO_OFFSET adjust
5857 downwards. */
5858 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
5860 /* Don't do the primary base twice. */
5861 if (base_binfo == primary_binfo)
5862 continue;
5864 if (BINFO_VIRTUAL_P (base_binfo))
5865 continue;
5867 propagate_binfo_offsets (base_binfo, offset);
5871 /* Set BINFO_OFFSET for all of the virtual bases for RLI->T. Update
5872 TYPE_ALIGN and TYPE_SIZE for T. OFFSETS gives the location of
5873 empty subobjects of T. */
5875 static void
5876 layout_virtual_bases (record_layout_info rli, splay_tree offsets)
5878 tree vbase;
5879 tree t = rli->t;
5880 tree *next_field;
5882 if (BINFO_N_BASE_BINFOS (TYPE_BINFO (t)) == 0)
5883 return;
5885 /* Find the last field. The artificial fields created for virtual
5886 bases will go after the last extant field to date. */
5887 next_field = &TYPE_FIELDS (t);
5888 while (*next_field)
5889 next_field = &DECL_CHAIN (*next_field);
5891 /* Go through the virtual bases, allocating space for each virtual
5892 base that is not already a primary base class. These are
5893 allocated in inheritance graph order. */
5894 for (vbase = TYPE_BINFO (t); vbase; vbase = TREE_CHAIN (vbase))
5896 if (!BINFO_VIRTUAL_P (vbase))
5897 continue;
5899 if (!BINFO_PRIMARY_P (vbase))
5901 /* This virtual base is not a primary base of any class in the
5902 hierarchy, so we have to add space for it. */
5903 next_field = build_base_field (rli, vbase,
5904 offsets, next_field);
5909 /* Returns the offset of the byte just past the end of the base class
5910 BINFO. */
5912 static tree
5913 end_of_base (tree binfo)
5915 tree size;
5917 if (!CLASSTYPE_AS_BASE (BINFO_TYPE (binfo)))
5918 size = TYPE_SIZE_UNIT (char_type_node);
5919 else if (is_empty_class (BINFO_TYPE (binfo)))
5920 /* An empty class has zero CLASSTYPE_SIZE_UNIT, but we need to
5921 allocate some space for it. It cannot have virtual bases, so
5922 TYPE_SIZE_UNIT is fine. */
5923 size = TYPE_SIZE_UNIT (BINFO_TYPE (binfo));
5924 else
5925 size = CLASSTYPE_SIZE_UNIT (BINFO_TYPE (binfo));
5927 return size_binop (PLUS_EXPR, BINFO_OFFSET (binfo), size);
5930 /* Returns the offset of the byte just past the end of the base class
5931 with the highest offset in T. If INCLUDE_VIRTUALS_P is zero, then
5932 only non-virtual bases are included. If INCLUDE_FIELDS_P is true,
5933 then also consider non-static data members. */
5935 static tree
5936 end_of_class (tree t, bool include_virtuals_p, bool include_fields_p = false)
5938 tree result = size_zero_node;
5939 vec<tree, va_gc> *vbases;
5940 tree binfo;
5941 tree base_binfo;
5942 tree offset;
5943 int i;
5945 for (binfo = TYPE_BINFO (t), i = 0;
5946 BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
5948 if (!include_virtuals_p
5949 && BINFO_VIRTUAL_P (base_binfo)
5950 && (!BINFO_PRIMARY_P (base_binfo)
5951 || BINFO_INHERITANCE_CHAIN (base_binfo) != TYPE_BINFO (t)))
5952 continue;
5954 offset = end_of_base (base_binfo);
5955 if (tree_int_cst_lt (result, offset))
5956 result = offset;
5959 if (include_fields_p)
5960 for (tree field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
5961 if (TREE_CODE (field) == FIELD_DECL)
5963 offset = size_binop (PLUS_EXPR, DECL_FIELD_OFFSET (field),
5964 DECL_SIZE_UNIT (field));
5965 if (tree_int_cst_lt (result, offset))
5966 result = offset;
5969 if (include_virtuals_p)
5970 for (vbases = CLASSTYPE_VBASECLASSES (t), i = 0;
5971 vec_safe_iterate (vbases, i, &base_binfo); i++)
5973 offset = end_of_base (base_binfo);
5974 if (tree_int_cst_lt (result, offset))
5975 result = offset;
5978 return result;
5981 /* Warn about bases of T that are inaccessible because they are
5982 ambiguous. For example:
5984 struct S {};
5985 struct T : public S {};
5986 struct U : public S, public T {};
5988 Here, `(S*) new U' is not allowed because there are two `S'
5989 subobjects of U. */
5991 static void
5992 warn_about_ambiguous_bases (tree t)
5994 int i;
5995 vec<tree, va_gc> *vbases;
5996 tree basetype;
5997 tree binfo;
5998 tree base_binfo;
6000 /* If there are no repeated bases, nothing can be ambiguous. */
6001 if (!CLASSTYPE_REPEATED_BASE_P (t))
6002 return;
6004 /* Check direct bases. */
6005 for (binfo = TYPE_BINFO (t), i = 0;
6006 BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
6008 basetype = BINFO_TYPE (base_binfo);
6010 if (!uniquely_derived_from_p (basetype, t))
6011 warning (0, "direct base %qT inaccessible in %qT due to ambiguity",
6012 basetype, t);
6015 /* Check for ambiguous virtual bases. */
6016 if (extra_warnings)
6017 for (vbases = CLASSTYPE_VBASECLASSES (t), i = 0;
6018 vec_safe_iterate (vbases, i, &binfo); i++)
6020 basetype = BINFO_TYPE (binfo);
6022 if (!uniquely_derived_from_p (basetype, t))
6023 warning (OPT_Wextra, "virtual base %qT inaccessible in %qT due "
6024 "to ambiguity", basetype, t);
6028 /* Compare two INTEGER_CSTs K1 and K2. */
6030 static int
6031 splay_tree_compare_integer_csts (splay_tree_key k1, splay_tree_key k2)
6033 return tree_int_cst_compare ((tree) k1, (tree) k2);
6036 /* Increase the size indicated in RLI to account for empty classes
6037 that are "off the end" of the class. */
6039 static void
6040 include_empty_classes (record_layout_info rli)
6042 tree eoc;
6043 tree rli_size;
6045 /* It might be the case that we grew the class to allocate a
6046 zero-sized base class. That won't be reflected in RLI, yet,
6047 because we are willing to overlay multiple bases at the same
6048 offset. However, now we need to make sure that RLI is big enough
6049 to reflect the entire class. */
6050 eoc = end_of_class (rli->t, CLASSTYPE_AS_BASE (rli->t) != NULL_TREE);
6051 rli_size = rli_size_unit_so_far (rli);
6052 if (TREE_CODE (rli_size) == INTEGER_CST
6053 && tree_int_cst_lt (rli_size, eoc))
6055 /* The size should have been rounded to a whole byte. */
6056 gcc_assert (tree_int_cst_equal
6057 (rli->bitpos, round_down (rli->bitpos, BITS_PER_UNIT)));
6058 rli->bitpos
6059 = size_binop (PLUS_EXPR,
6060 rli->bitpos,
6061 size_binop (MULT_EXPR,
6062 fold_convert (bitsizetype,
6063 size_binop (MINUS_EXPR,
6064 eoc, rli_size)),
6065 bitsize_int (BITS_PER_UNIT)));
6066 normalize_rli (rli);
6070 /* Calculate the TYPE_SIZE, TYPE_ALIGN, etc for T. Calculate
6071 BINFO_OFFSETs for all of the base-classes. Position the vtable
6072 pointer. Accumulate declared virtual functions on VIRTUALS_P. */
6074 static void
6075 layout_class_type (tree t, tree *virtuals_p)
6077 tree non_static_data_members;
6078 tree field;
6079 tree vptr;
6080 record_layout_info rli;
6081 /* Maps offsets (represented as INTEGER_CSTs) to a TREE_LIST of
6082 types that appear at that offset. */
6083 splay_tree empty_base_offsets;
6084 /* True if the last field laid out was a bit-field. */
6085 bool last_field_was_bitfield = false;
6086 /* The location at which the next field should be inserted. */
6087 tree *next_field;
6089 /* Keep track of the first non-static data member. */
6090 non_static_data_members = TYPE_FIELDS (t);
6092 /* Start laying out the record. */
6093 rli = start_record_layout (t);
6095 /* Mark all the primary bases in the hierarchy. */
6096 determine_primary_bases (t);
6098 /* Create a pointer to our virtual function table. */
6099 vptr = create_vtable_ptr (t, virtuals_p);
6101 /* The vptr is always the first thing in the class. */
6102 if (vptr)
6104 DECL_CHAIN (vptr) = TYPE_FIELDS (t);
6105 TYPE_FIELDS (t) = vptr;
6106 next_field = &DECL_CHAIN (vptr);
6107 place_field (rli, vptr);
6109 else
6110 next_field = &TYPE_FIELDS (t);
6112 /* Build FIELD_DECLs for all of the non-virtual base-types. */
6113 empty_base_offsets = splay_tree_new (splay_tree_compare_integer_csts,
6114 NULL, NULL);
6115 build_base_fields (rli, empty_base_offsets, next_field);
6117 /* Layout the non-static data members. */
6118 for (field = non_static_data_members; field; field = DECL_CHAIN (field))
6120 tree type;
6121 tree padding;
6123 /* We still pass things that aren't non-static data members to
6124 the back end, in case it wants to do something with them. */
6125 if (TREE_CODE (field) != FIELD_DECL)
6127 place_field (rli, field);
6128 /* If the static data member has incomplete type, keep track
6129 of it so that it can be completed later. (The handling
6130 of pending statics in finish_record_layout is
6131 insufficient; consider:
6133 struct S1;
6134 struct S2 { static S1 s1; };
6136 At this point, finish_record_layout will be called, but
6137 S1 is still incomplete.) */
6138 if (VAR_P (field))
6140 maybe_register_incomplete_var (field);
6141 /* The visibility of static data members is determined
6142 at their point of declaration, not their point of
6143 definition. */
6144 determine_visibility (field);
6146 continue;
6149 type = TREE_TYPE (field);
6150 if (type == error_mark_node)
6151 continue;
6153 padding = NULL_TREE;
6155 bool might_overlap = field_poverlapping_p (field);
6157 if (might_overlap && CLASS_TYPE_P (type)
6158 && CLASSTYPE_NON_LAYOUT_POD_P (type))
6160 /* if D is a potentially-overlapping data member, update sizeof(C) to
6161 max (sizeof(C), offset(D)+max (nvsize(D), dsize(D))). */
6162 tree nvsize = CLASSTYPE_SIZE_UNIT (type);
6163 tree dsize = end_of_class (type, /*vbases*/true, /*fields*/true);
6164 if (tree_int_cst_le (dsize, nvsize))
6166 DECL_SIZE_UNIT (field) = nvsize;
6167 DECL_SIZE (field) = CLASSTYPE_SIZE (type);
6169 else
6171 DECL_SIZE_UNIT (field) = dsize;
6172 DECL_SIZE (field) = bit_from_pos (dsize, bitsize_zero_node);
6176 /* If this field is a bit-field whose width is greater than its
6177 type, then there are some special rules for allocating
6178 it. */
6179 if (DECL_C_BIT_FIELD (field)
6180 && tree_int_cst_lt (TYPE_SIZE (type), DECL_SIZE (field)))
6182 bool was_unnamed_p = false;
6183 /* We must allocate the bits as if suitably aligned for the
6184 longest integer type that fits in this many bits. Then,
6185 we are supposed to use the left over bits as additional
6186 padding. */
6188 /* Do not pick a type bigger than MAX_FIXED_MODE_SIZE. */
6189 tree limit = size_int (MAX_FIXED_MODE_SIZE);
6190 if (tree_int_cst_lt (DECL_SIZE (field), limit))
6191 limit = DECL_SIZE (field);
6193 tree integer_type = integer_types[itk_char];
6194 for (unsigned itk = itk_char; itk != itk_none; itk++)
6195 if (tree next = integer_types[itk])
6197 if (tree_int_cst_lt (limit, TYPE_SIZE (next)))
6198 /* Too big, so our current guess is what we want. */
6199 break;
6200 /* Not bigger than limit, ok */
6201 integer_type = next;
6204 /* Figure out how much additional padding is required. */
6205 if (TREE_CODE (t) == UNION_TYPE)
6206 /* In a union, the padding field must have the full width
6207 of the bit-field; all fields start at offset zero. */
6208 padding = DECL_SIZE (field);
6209 else
6210 padding = size_binop (MINUS_EXPR, DECL_SIZE (field),
6211 TYPE_SIZE (integer_type));
6213 if (integer_zerop (padding))
6214 padding = NULL_TREE;
6216 /* An unnamed bitfield does not normally affect the
6217 alignment of the containing class on a target where
6218 PCC_BITFIELD_TYPE_MATTERS. But, the C++ ABI does not
6219 make any exceptions for unnamed bitfields when the
6220 bitfields are longer than their types. Therefore, we
6221 temporarily give the field a name. */
6222 if (PCC_BITFIELD_TYPE_MATTERS && !DECL_NAME (field))
6224 was_unnamed_p = true;
6225 DECL_NAME (field) = make_anon_name ();
6228 DECL_SIZE (field) = TYPE_SIZE (integer_type);
6229 SET_DECL_ALIGN (field, TYPE_ALIGN (integer_type));
6230 DECL_USER_ALIGN (field) = TYPE_USER_ALIGN (integer_type);
6231 layout_nonempty_base_or_field (rli, field, NULL_TREE,
6232 empty_base_offsets);
6233 if (was_unnamed_p)
6234 DECL_NAME (field) = NULL_TREE;
6235 /* Now that layout has been performed, set the size of the
6236 field to the size of its declared type; the rest of the
6237 field is effectively invisible. */
6238 DECL_SIZE (field) = TYPE_SIZE (type);
6239 /* We must also reset the DECL_MODE of the field. */
6240 SET_DECL_MODE (field, TYPE_MODE (type));
6242 else if (might_overlap && is_empty_class (type))
6243 layout_empty_base_or_field (rli, field, empty_base_offsets);
6244 else
6245 layout_nonempty_base_or_field (rli, field, NULL_TREE,
6246 empty_base_offsets);
6248 /* Remember the location of any empty classes in FIELD. */
6249 record_subobject_offsets (field, empty_base_offsets);
6251 /* If a bit-field does not immediately follow another bit-field,
6252 and yet it starts in the middle of a byte, we have failed to
6253 comply with the ABI. */
6254 if (warn_abi
6255 && DECL_C_BIT_FIELD (field)
6256 /* The TREE_NO_WARNING flag gets set by Objective-C when
6257 laying out an Objective-C class. The ObjC ABI differs
6258 from the C++ ABI, and so we do not want a warning
6259 here. */
6260 && !TREE_NO_WARNING (field)
6261 && !last_field_was_bitfield
6262 && !integer_zerop (size_binop (TRUNC_MOD_EXPR,
6263 DECL_FIELD_BIT_OFFSET (field),
6264 bitsize_unit_node)))
6265 warning_at (DECL_SOURCE_LOCATION (field), OPT_Wabi,
6266 "offset of %qD is not ABI-compliant and may "
6267 "change in a future version of GCC", field);
6269 /* The middle end uses the type of expressions to determine the
6270 possible range of expression values. In order to optimize
6271 "x.i > 7" to "false" for a 2-bit bitfield "i", the middle end
6272 must be made aware of the width of "i", via its type.
6274 Because C++ does not have integer types of arbitrary width,
6275 we must (for the purposes of the front end) convert from the
6276 type assigned here to the declared type of the bitfield
6277 whenever a bitfield expression is used as an rvalue.
6278 Similarly, when assigning a value to a bitfield, the value
6279 must be converted to the type given the bitfield here. */
6280 if (DECL_C_BIT_FIELD (field))
6282 unsigned HOST_WIDE_INT width;
6283 tree ftype = TREE_TYPE (field);
6284 width = tree_to_uhwi (DECL_SIZE (field));
6285 if (width != TYPE_PRECISION (ftype))
6287 TREE_TYPE (field)
6288 = c_build_bitfield_integer_type (width,
6289 TYPE_UNSIGNED (ftype));
6290 TREE_TYPE (field)
6291 = cp_build_qualified_type (TREE_TYPE (field),
6292 cp_type_quals (ftype));
6296 /* If we needed additional padding after this field, add it
6297 now. */
6298 if (padding)
6300 tree padding_field;
6302 padding_field = build_decl (input_location,
6303 FIELD_DECL,
6304 NULL_TREE,
6305 char_type_node);
6306 DECL_BIT_FIELD (padding_field) = 1;
6307 DECL_SIZE (padding_field) = padding;
6308 DECL_CONTEXT (padding_field) = t;
6309 DECL_ARTIFICIAL (padding_field) = 1;
6310 DECL_IGNORED_P (padding_field) = 1;
6311 DECL_PADDING_P (padding_field) = 1;
6312 layout_nonempty_base_or_field (rli, padding_field,
6313 NULL_TREE,
6314 empty_base_offsets);
6317 last_field_was_bitfield = DECL_C_BIT_FIELD (field);
6320 if (!integer_zerop (rli->bitpos))
6322 /* Make sure that we are on a byte boundary so that the size of
6323 the class without virtual bases will always be a round number
6324 of bytes. */
6325 rli->bitpos = round_up_loc (input_location, rli->bitpos, BITS_PER_UNIT);
6326 normalize_rli (rli);
6329 /* Delete all zero-width bit-fields from the list of fields. Now
6330 that the type is laid out they are no longer important. */
6331 remove_zero_width_bit_fields (t);
6333 if (CLASSTYPE_NON_LAYOUT_POD_P (t) || CLASSTYPE_EMPTY_P (t))
6335 /* T needs a different layout as a base (eliding virtual bases
6336 or whatever). Create that version. */
6337 tree base_t = make_node (TREE_CODE (t));
6339 /* If the ABI version is not at least two, and the last
6340 field was a bit-field, RLI may not be on a byte
6341 boundary. In particular, rli_size_unit_so_far might
6342 indicate the last complete byte, while rli_size_so_far
6343 indicates the total number of bits used. Therefore,
6344 rli_size_so_far, rather than rli_size_unit_so_far, is
6345 used to compute TYPE_SIZE_UNIT. */
6346 tree eoc = end_of_class (t, /*include_virtuals_p=*/0);
6347 TYPE_SIZE_UNIT (base_t)
6348 = size_binop (MAX_EXPR,
6349 fold_convert (sizetype,
6350 size_binop (CEIL_DIV_EXPR,
6351 rli_size_so_far (rli),
6352 bitsize_int (BITS_PER_UNIT))),
6353 eoc);
6354 TYPE_SIZE (base_t)
6355 = size_binop (MAX_EXPR,
6356 rli_size_so_far (rli),
6357 size_binop (MULT_EXPR,
6358 fold_convert (bitsizetype, eoc),
6359 bitsize_int (BITS_PER_UNIT)));
6360 SET_TYPE_ALIGN (base_t, rli->record_align);
6361 TYPE_USER_ALIGN (base_t) = TYPE_USER_ALIGN (t);
6362 TYPE_TYPELESS_STORAGE (base_t) = TYPE_TYPELESS_STORAGE (t);
6364 /* Copy the non-static data members of T. This will include its
6365 direct non-virtual bases & vtable. */
6366 next_field = &TYPE_FIELDS (base_t);
6367 for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
6368 if (TREE_CODE (field) == FIELD_DECL)
6370 *next_field = copy_node (field);
6371 DECL_CONTEXT (*next_field) = base_t;
6372 next_field = &DECL_CHAIN (*next_field);
6374 *next_field = NULL_TREE;
6376 /* We use the base type for trivial assignments, and hence it
6377 needs a mode. */
6378 compute_record_mode (base_t);
6380 TYPE_CONTEXT (base_t) = t;
6382 /* Record the base version of the type. */
6383 CLASSTYPE_AS_BASE (t) = base_t;
6385 else
6386 CLASSTYPE_AS_BASE (t) = t;
6388 /* Every empty class contains an empty class. */
6389 if (CLASSTYPE_EMPTY_P (t))
6390 CLASSTYPE_CONTAINS_EMPTY_CLASS_P (t) = 1;
6392 /* Set the TYPE_DECL for this type to contain the right
6393 value for DECL_OFFSET, so that we can use it as part
6394 of a COMPONENT_REF for multiple inheritance. */
6395 layout_decl (TYPE_MAIN_DECL (t), 0);
6397 /* Now fix up any virtual base class types that we left lying
6398 around. We must get these done before we try to lay out the
6399 virtual function table. As a side-effect, this will remove the
6400 base subobject fields. */
6401 layout_virtual_bases (rli, empty_base_offsets);
6403 /* Make sure that empty classes are reflected in RLI at this
6404 point. */
6405 include_empty_classes (rli);
6407 /* Make sure not to create any structures with zero size. */
6408 if (integer_zerop (rli_size_unit_so_far (rli)) && CLASSTYPE_EMPTY_P (t))
6409 place_field (rli,
6410 build_decl (input_location,
6411 FIELD_DECL, NULL_TREE, char_type_node));
6413 /* If this is a non-POD, declaring it packed makes a difference to how it
6414 can be used as a field; don't let finalize_record_size undo it. */
6415 if (TYPE_PACKED (t) && !layout_pod_type_p (t))
6416 rli->packed_maybe_necessary = true;
6418 /* Let the back end lay out the type. */
6419 finish_record_layout (rli, /*free_p=*/true);
6421 if (TYPE_SIZE_UNIT (t)
6422 && TREE_CODE (TYPE_SIZE_UNIT (t)) == INTEGER_CST
6423 && !TREE_OVERFLOW (TYPE_SIZE_UNIT (t))
6424 && !valid_constant_size_p (TYPE_SIZE_UNIT (t)))
6425 error ("size of type %qT is too large (%qE bytes)", t, TYPE_SIZE_UNIT (t));
6427 /* Warn about bases that can't be talked about due to ambiguity. */
6428 warn_about_ambiguous_bases (t);
6430 /* Now that we're done with layout, give the base fields the real types. */
6431 for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
6432 if (DECL_ARTIFICIAL (field) && IS_FAKE_BASE_TYPE (TREE_TYPE (field)))
6433 TREE_TYPE (field) = TYPE_CONTEXT (TREE_TYPE (field));
6435 /* Clean up. */
6436 splay_tree_delete (empty_base_offsets);
6438 if (CLASSTYPE_EMPTY_P (t)
6439 && tree_int_cst_lt (sizeof_biggest_empty_class,
6440 TYPE_SIZE_UNIT (t)))
6441 sizeof_biggest_empty_class = TYPE_SIZE_UNIT (t);
6444 /* Determine the "key method" for the class type indicated by TYPE,
6445 and set CLASSTYPE_KEY_METHOD accordingly. */
6447 void
6448 determine_key_method (tree type)
6450 tree method;
6452 if (processing_template_decl
6453 || CLASSTYPE_TEMPLATE_INSTANTIATION (type)
6454 || CLASSTYPE_INTERFACE_KNOWN (type))
6455 return;
6457 /* The key method is the first non-pure virtual function that is not
6458 inline at the point of class definition. On some targets the
6459 key function may not be inline; those targets should not call
6460 this function until the end of the translation unit. */
6461 for (method = TYPE_FIELDS (type); method; method = DECL_CHAIN (method))
6462 if (TREE_CODE (method) == FUNCTION_DECL
6463 && DECL_VINDEX (method) != NULL_TREE
6464 && ! DECL_DECLARED_INLINE_P (method)
6465 && ! DECL_PURE_VIRTUAL_P (method))
6467 CLASSTYPE_KEY_METHOD (type) = method;
6468 break;
6471 return;
6474 /* Helper of find_flexarrays. Return true when FLD refers to a non-static
6475 class data member of non-zero size, otherwise false. */
6477 static inline bool
6478 field_nonempty_p (const_tree fld)
6480 if (TREE_CODE (fld) == ERROR_MARK)
6481 return false;
6483 tree type = TREE_TYPE (fld);
6484 if (TREE_CODE (fld) == FIELD_DECL
6485 && TREE_CODE (type) != ERROR_MARK
6486 && (DECL_NAME (fld) || RECORD_OR_UNION_TYPE_P (type)))
6488 return TYPE_SIZE (type)
6489 && (TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST
6490 || !tree_int_cst_equal (size_zero_node, TYPE_SIZE (type)));
6493 return false;
6496 /* Used by find_flexarrays and related functions. */
6498 struct flexmems_t
6500 /* The first flexible array member or non-zero array member found
6501 in the order of layout. */
6502 tree array;
6503 /* First non-static non-empty data member in the class or its bases. */
6504 tree first;
6505 /* The first non-static non-empty data member following either
6506 the flexible array member, if found, or the zero-length array member
6507 otherwise. AFTER[1] refers to the first such data member of a union
6508 of which the struct containing the flexible array member or zero-length
6509 array is a member, or NULL when no such union exists. This element is
6510 only used during searching, not for diagnosing problems. AFTER[0]
6511 refers to the first such data member that is not a member of such
6512 a union. */
6513 tree after[2];
6515 /* Refers to a struct (not union) in which the struct of which the flexible
6516 array is member is defined. Used to diagnose strictly (according to C)
6517 invalid uses of the latter structs. */
6518 tree enclosing;
6521 /* Find either the first flexible array member or the first zero-length
6522 array, in that order of preference, among members of class T (but not
6523 its base classes), and set members of FMEM accordingly.
6524 BASE_P is true if T is a base class of another class.
6525 PUN is set to the outermost union in which the flexible array member
6526 (or zero-length array) is defined if one such union exists, otherwise
6527 to NULL.
6528 Similarly, PSTR is set to a data member of the outermost struct of
6529 which the flexible array is a member if one such struct exists,
6530 otherwise to NULL. */
6532 static void
6533 find_flexarrays (tree t, flexmems_t *fmem, bool base_p,
6534 tree pun /* = NULL_TREE */,
6535 tree pstr /* = NULL_TREE */)
6537 /* Set the "pointer" to the outermost enclosing union if not set
6538 yet and maintain it for the remainder of the recursion. */
6539 if (!pun && TREE_CODE (t) == UNION_TYPE)
6540 pun = t;
6542 for (tree fld = TYPE_FIELDS (t); fld; fld = DECL_CHAIN (fld))
6544 if (fld == error_mark_node)
6545 return;
6547 /* Is FLD a typedef for an anonymous struct? */
6549 /* FIXME: Note that typedefs (as well as arrays) need to be fully
6550 handled elsewhere so that errors like the following are detected
6551 as well:
6552 typedef struct { int i, a[], j; } S; // bug c++/72753
6553 S s [2]; // bug c++/68489
6555 if (TREE_CODE (fld) == TYPE_DECL
6556 && DECL_IMPLICIT_TYPEDEF_P (fld)
6557 && CLASS_TYPE_P (TREE_TYPE (fld))
6558 && anon_aggrname_p (DECL_NAME (fld)))
6560 /* Check the nested unnamed type referenced via a typedef
6561 independently of FMEM (since it's not a data member of
6562 the enclosing class). */
6563 check_flexarrays (TREE_TYPE (fld));
6564 continue;
6567 /* Skip anything that's GCC-generated or not a (non-static) data
6568 member. */
6569 if (DECL_ARTIFICIAL (fld) || TREE_CODE (fld) != FIELD_DECL)
6570 continue;
6572 /* Type of the member. */
6573 tree fldtype = TREE_TYPE (fld);
6574 if (fldtype == error_mark_node)
6575 return;
6577 /* Determine the type of the array element or object referenced
6578 by the member so that it can be checked for flexible array
6579 members if it hasn't been yet. */
6580 tree eltype = fldtype;
6581 while (TREE_CODE (eltype) == ARRAY_TYPE
6582 || INDIRECT_TYPE_P (eltype))
6583 eltype = TREE_TYPE (eltype);
6585 if (RECORD_OR_UNION_TYPE_P (eltype))
6587 if (fmem->array && !fmem->after[bool (pun)])
6589 /* Once the member after the flexible array has been found
6590 we're done. */
6591 fmem->after[bool (pun)] = fld;
6592 break;
6595 if (eltype == fldtype || TYPE_UNNAMED_P (eltype))
6597 /* Descend into the non-static member struct or union and try
6598 to find a flexible array member or zero-length array among
6599 its members. This is only necessary for anonymous types
6600 and types in whose context the current type T has not been
6601 defined (the latter must not be checked again because they
6602 are already in the process of being checked by one of the
6603 recursive calls). */
6605 tree first = fmem->first;
6606 tree array = fmem->array;
6608 /* If this member isn't anonymous and a prior non-flexible array
6609 member has been seen in one of the enclosing structs, clear
6610 the FIRST member since it doesn't contribute to the flexible
6611 array struct's members. */
6612 if (first && !array && !ANON_AGGR_TYPE_P (eltype))
6613 fmem->first = NULL_TREE;
6615 find_flexarrays (eltype, fmem, false, pun,
6616 !pstr && TREE_CODE (t) == RECORD_TYPE ? fld : pstr);
6618 if (fmem->array != array)
6619 continue;
6621 if (first && !array && !ANON_AGGR_TYPE_P (eltype))
6623 /* Restore the FIRST member reset above if no flexible
6624 array member has been found in this member's struct. */
6625 fmem->first = first;
6628 /* If the member struct contains the first flexible array
6629 member, or if this member is a base class, continue to
6630 the next member and avoid setting the FMEM->NEXT pointer
6631 to point to it. */
6632 if (base_p)
6633 continue;
6637 if (field_nonempty_p (fld))
6639 /* Remember the first non-static data member. */
6640 if (!fmem->first)
6641 fmem->first = fld;
6643 /* Remember the first non-static data member after the flexible
6644 array member, if one has been found, or the zero-length array
6645 if it has been found. */
6646 if (fmem->array && !fmem->after[bool (pun)])
6647 fmem->after[bool (pun)] = fld;
6650 /* Skip non-arrays. */
6651 if (TREE_CODE (fldtype) != ARRAY_TYPE)
6652 continue;
6654 /* Determine the upper bound of the array if it has one. */
6655 if (TYPE_DOMAIN (fldtype))
6657 if (fmem->array)
6659 /* Make a record of the zero-length array if either one
6660 such field or a flexible array member has been seen to
6661 handle the pathological and unlikely case of multiple
6662 such members. */
6663 if (!fmem->after[bool (pun)])
6664 fmem->after[bool (pun)] = fld;
6666 else if (integer_all_onesp (TYPE_MAX_VALUE (TYPE_DOMAIN (fldtype))))
6668 /* Remember the first zero-length array unless a flexible array
6669 member has already been seen. */
6670 fmem->array = fld;
6671 fmem->enclosing = pstr;
6674 else
6676 /* Flexible array members have no upper bound. */
6677 if (fmem->array)
6679 if (TYPE_DOMAIN (TREE_TYPE (fmem->array)))
6681 /* Replace the zero-length array if it's been stored and
6682 reset the after pointer. */
6683 fmem->after[bool (pun)] = NULL_TREE;
6684 fmem->array = fld;
6685 fmem->enclosing = pstr;
6687 else if (!fmem->after[bool (pun)])
6688 /* Make a record of another flexible array member. */
6689 fmem->after[bool (pun)] = fld;
6691 else
6693 fmem->array = fld;
6694 fmem->enclosing = pstr;
6700 /* Diagnose a strictly (by the C standard) invalid use of a struct with
6701 a flexible array member (or the zero-length array extension). */
6703 static void
6704 diagnose_invalid_flexarray (const flexmems_t *fmem)
6706 if (fmem->array && fmem->enclosing)
6708 auto_diagnostic_group d;
6709 if (pedwarn (location_of (fmem->enclosing), OPT_Wpedantic,
6710 TYPE_DOMAIN (TREE_TYPE (fmem->array))
6711 ? G_("invalid use of %q#T with a zero-size array "
6712 "in %q#D")
6713 : G_("invalid use of %q#T with a flexible array member "
6714 "in %q#T"),
6715 DECL_CONTEXT (fmem->array),
6716 DECL_CONTEXT (fmem->enclosing)))
6717 inform (DECL_SOURCE_LOCATION (fmem->array),
6718 "array member %q#D declared here", fmem->array);
6722 /* Issue diagnostics for invalid flexible array members or zero-length
6723 arrays that are not the last elements of the containing class or its
6724 base classes or that are its sole members. */
6726 static void
6727 diagnose_flexarrays (tree t, const flexmems_t *fmem)
6729 if (!fmem->array)
6730 return;
6732 if (fmem->first && !fmem->after[0])
6734 diagnose_invalid_flexarray (fmem);
6735 return;
6738 /* Has a diagnostic been issued? */
6739 bool diagd = false;
6741 const char *msg = 0;
6743 if (TYPE_DOMAIN (TREE_TYPE (fmem->array)))
6745 if (fmem->after[0])
6746 msg = G_("zero-size array member %qD not at end of %q#T");
6747 else if (!fmem->first)
6748 msg = G_("zero-size array member %qD in an otherwise empty %q#T");
6750 if (msg)
6752 location_t loc = DECL_SOURCE_LOCATION (fmem->array);
6754 auto_diagnostic_group d;
6755 if (pedwarn (loc, OPT_Wpedantic, msg, fmem->array, t))
6757 inform (location_of (t), "in the definition of %q#T", t);
6758 diagd = true;
6762 else
6764 if (fmem->after[0])
6765 msg = G_("flexible array member %qD not at end of %q#T");
6766 else if (!fmem->first)
6767 msg = G_("flexible array member %qD in an otherwise empty %q#T");
6769 if (msg)
6771 location_t loc = DECL_SOURCE_LOCATION (fmem->array);
6772 diagd = true;
6774 auto_diagnostic_group d;
6775 error_at (loc, msg, fmem->array, t);
6777 /* In the unlikely event that the member following the flexible
6778 array member is declared in a different class, or the member
6779 overlaps another member of a common union, point to it.
6780 Otherwise it should be obvious. */
6781 if (fmem->after[0]
6782 && ((DECL_CONTEXT (fmem->after[0])
6783 != DECL_CONTEXT (fmem->array))))
6785 inform (DECL_SOURCE_LOCATION (fmem->after[0]),
6786 "next member %q#D declared here",
6787 fmem->after[0]);
6788 inform (location_of (t), "in the definition of %q#T", t);
6793 if (!diagd && fmem->array && fmem->enclosing)
6794 diagnose_invalid_flexarray (fmem);
6798 /* Recursively check to make sure that any flexible array or zero-length
6799 array members of class T or its bases are valid (i.e., not the sole
6800 non-static data member of T and, if one exists, that it is the last
6801 non-static data member of T and its base classes. FMEM is expected
6802 to be initially null and is used internally by recursive calls to
6803 the function. Issue the appropriate diagnostics for the array member
6804 that fails the checks. */
6806 static void
6807 check_flexarrays (tree t, flexmems_t *fmem /* = NULL */,
6808 bool base_p /* = false */)
6810 /* Initialize the result of a search for flexible array and zero-length
6811 array members. Avoid doing any work if the most interesting FMEM data
6812 have already been populated. */
6813 flexmems_t flexmems = flexmems_t ();
6814 if (!fmem)
6815 fmem = &flexmems;
6816 else if (fmem->array && fmem->first && fmem->after[0])
6817 return;
6819 tree fam = fmem->array;
6821 /* Recursively check the primary base class first. */
6822 if (CLASSTYPE_HAS_PRIMARY_BASE_P (t))
6824 tree basetype = BINFO_TYPE (CLASSTYPE_PRIMARY_BINFO (t));
6825 check_flexarrays (basetype, fmem, true);
6828 /* Recursively check the base classes. */
6829 int nbases = TYPE_BINFO (t) ? BINFO_N_BASE_BINFOS (TYPE_BINFO (t)) : 0;
6830 for (int i = 0; i < nbases; ++i)
6832 tree base_binfo = BINFO_BASE_BINFO (TYPE_BINFO (t), i);
6834 /* The primary base class was already checked above. */
6835 if (base_binfo == CLASSTYPE_PRIMARY_BINFO (t))
6836 continue;
6838 /* Virtual base classes are at the end. */
6839 if (BINFO_VIRTUAL_P (base_binfo))
6840 continue;
6842 /* Check the base class. */
6843 check_flexarrays (BINFO_TYPE (base_binfo), fmem, /*base_p=*/true);
6846 if (fmem == &flexmems)
6848 /* Check virtual base classes only once per derived class.
6849 I.e., this check is not performed recursively for base
6850 classes. */
6851 int i;
6852 tree base_binfo;
6853 vec<tree, va_gc> *vbases;
6854 for (vbases = CLASSTYPE_VBASECLASSES (t), i = 0;
6855 vec_safe_iterate (vbases, i, &base_binfo); i++)
6857 /* Check the virtual base class. */
6858 tree basetype = TREE_TYPE (base_binfo);
6860 check_flexarrays (basetype, fmem, /*base_p=*/true);
6864 /* Is the type unnamed (and therefore a member of it potentially
6865 an anonymous struct or union)? */
6866 bool maybe_anon_p = TYPE_UNNAMED_P (t);
6868 /* Search the members of the current (possibly derived) class, skipping
6869 unnamed structs and unions since those could be anonymous. */
6870 if (fmem != &flexmems || !maybe_anon_p)
6871 find_flexarrays (t, fmem, base_p || fam != fmem->array);
6873 if (fmem == &flexmems && !maybe_anon_p)
6875 /* Issue diagnostics for invalid flexible and zero-length array
6876 members found in base classes or among the members of the current
6877 class. Ignore anonymous structs and unions whose members are
6878 considered to be members of the enclosing class and thus will
6879 be diagnosed when checking it. */
6880 diagnose_flexarrays (t, fmem);
6884 /* Perform processing required when the definition of T (a class type)
6885 is complete. Diagnose invalid definitions of flexible array members
6886 and zero-size arrays. */
6888 void
6889 finish_struct_1 (tree t)
6891 tree x;
6892 /* A TREE_LIST. The TREE_VALUE of each node is a FUNCTION_DECL. */
6893 tree virtuals = NULL_TREE;
6895 if (COMPLETE_TYPE_P (t))
6897 gcc_assert (MAYBE_CLASS_TYPE_P (t));
6898 error ("redefinition of %q#T", t);
6899 popclass ();
6900 return;
6903 /* If this type was previously laid out as a forward reference,
6904 make sure we lay it out again. */
6905 TYPE_SIZE (t) = NULL_TREE;
6906 CLASSTYPE_PRIMARY_BINFO (t) = NULL_TREE;
6908 /* Make assumptions about the class; we'll reset the flags if
6909 necessary. */
6910 CLASSTYPE_EMPTY_P (t) = 1;
6911 CLASSTYPE_NEARLY_EMPTY_P (t) = 1;
6912 CLASSTYPE_CONTAINS_EMPTY_CLASS_P (t) = 0;
6913 CLASSTYPE_LITERAL_P (t) = true;
6915 /* Do end-of-class semantic processing: checking the validity of the
6916 bases and members and add implicitly generated methods. */
6917 check_bases_and_members (t);
6919 /* Find the key method. */
6920 if (TYPE_CONTAINS_VPTR_P (t))
6922 /* The Itanium C++ ABI permits the key method to be chosen when
6923 the class is defined -- even though the key method so
6924 selected may later turn out to be an inline function. On
6925 some systems (such as ARM Symbian OS) the key method cannot
6926 be determined until the end of the translation unit. On such
6927 systems, we leave CLASSTYPE_KEY_METHOD set to NULL, which
6928 will cause the class to be added to KEYED_CLASSES. Then, in
6929 finish_file we will determine the key method. */
6930 if (targetm.cxx.key_method_may_be_inline ())
6931 determine_key_method (t);
6933 /* If a polymorphic class has no key method, we may emit the vtable
6934 in every translation unit where the class definition appears. If
6935 we're devirtualizing, we can look into the vtable even if we
6936 aren't emitting it. */
6937 if (!CLASSTYPE_KEY_METHOD (t))
6938 vec_safe_push (keyed_classes, t);
6941 /* Layout the class itself. */
6942 layout_class_type (t, &virtuals);
6943 /* COMPLETE_TYPE_P is now true. */
6945 set_class_bindings (t);
6947 /* With the layout complete, check for flexible array members and
6948 zero-length arrays that might overlap other members in the final
6949 layout. */
6950 check_flexarrays (t);
6952 virtuals = modify_all_vtables (t, nreverse (virtuals));
6954 /* If necessary, create the primary vtable for this class. */
6955 if (virtuals || TYPE_CONTAINS_VPTR_P (t))
6957 /* We must enter these virtuals into the table. */
6958 if (!CLASSTYPE_HAS_PRIMARY_BASE_P (t))
6959 build_primary_vtable (NULL_TREE, t);
6960 else if (! BINFO_NEW_VTABLE_MARKED (TYPE_BINFO (t)))
6961 /* Here we know enough to change the type of our virtual
6962 function table, but we will wait until later this function. */
6963 build_primary_vtable (CLASSTYPE_PRIMARY_BINFO (t), t);
6965 /* If we're warning about ABI tags, check the types of the new
6966 virtual functions. */
6967 if (warn_abi_tag)
6968 for (tree v = virtuals; v; v = TREE_CHAIN (v))
6969 check_abi_tags (t, TREE_VALUE (v));
6972 if (TYPE_CONTAINS_VPTR_P (t))
6974 int vindex;
6975 tree fn;
6977 if (BINFO_VTABLE (TYPE_BINFO (t)))
6978 gcc_assert (DECL_VIRTUAL_P (BINFO_VTABLE (TYPE_BINFO (t))));
6979 if (!CLASSTYPE_HAS_PRIMARY_BASE_P (t))
6980 gcc_assert (BINFO_VIRTUALS (TYPE_BINFO (t)) == NULL_TREE);
6982 /* Add entries for virtual functions introduced by this class. */
6983 BINFO_VIRTUALS (TYPE_BINFO (t))
6984 = chainon (BINFO_VIRTUALS (TYPE_BINFO (t)), virtuals);
6986 /* Set DECL_VINDEX for all functions declared in this class. */
6987 for (vindex = 0, fn = BINFO_VIRTUALS (TYPE_BINFO (t));
6989 fn = TREE_CHAIN (fn),
6990 vindex += (TARGET_VTABLE_USES_DESCRIPTORS
6991 ? TARGET_VTABLE_USES_DESCRIPTORS : 1))
6993 tree fndecl = BV_FN (fn);
6995 if (DECL_THUNK_P (fndecl))
6996 /* A thunk. We should never be calling this entry directly
6997 from this vtable -- we'd use the entry for the non
6998 thunk base function. */
6999 DECL_VINDEX (fndecl) = NULL_TREE;
7000 else if (TREE_CODE (DECL_VINDEX (fndecl)) != INTEGER_CST)
7001 DECL_VINDEX (fndecl) = build_int_cst (NULL_TREE, vindex);
7005 finish_struct_bits (t);
7007 set_method_tm_attributes (t);
7008 if (flag_openmp || flag_openmp_simd)
7009 finish_omp_declare_simd_methods (t);
7011 /* Clear DECL_IN_AGGR_P for all member functions. Complete the rtl
7012 for any static member objects of the type we're working on. */
7013 for (x = TYPE_FIELDS (t); x; x = DECL_CHAIN (x))
7014 if (DECL_DECLARES_FUNCTION_P (x))
7015 DECL_IN_AGGR_P (x) = false;
7016 else if (VAR_P (x) && TREE_STATIC (x)
7017 && TREE_TYPE (x) != error_mark_node
7018 && same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (x)), t))
7019 SET_DECL_MODE (x, TYPE_MODE (t));
7021 /* Complain if one of the field types requires lower visibility. */
7022 constrain_class_visibility (t);
7024 /* Make the rtl for any new vtables we have created, and unmark
7025 the base types we marked. */
7026 finish_vtbls (t);
7028 /* Build the VTT for T. */
7029 build_vtt (t);
7031 if (warn_nonvdtor
7032 && TYPE_POLYMORPHIC_P (t) && accessible_nvdtor_p (t)
7033 && !CLASSTYPE_FINAL (t))
7034 warning (OPT_Wnon_virtual_dtor,
7035 "%q#T has virtual functions and accessible"
7036 " non-virtual destructor", t);
7038 complete_vars (t);
7040 if (warn_overloaded_virtual)
7041 warn_hidden (t);
7043 /* Class layout, assignment of virtual table slots, etc., is now
7044 complete. Give the back end a chance to tweak the visibility of
7045 the class or perform any other required target modifications. */
7046 targetm.cxx.adjust_class_at_definition (t);
7048 maybe_suppress_debug_info (t);
7050 if (flag_vtable_verify)
7051 vtv_save_class_info (t);
7053 dump_class_hierarchy (t);
7055 /* Finish debugging output for this type. */
7056 rest_of_type_compilation (t, ! LOCAL_CLASS_P (t));
7058 if (TYPE_TRANSPARENT_AGGR (t))
7060 tree field = first_field (t);
7061 if (field == NULL_TREE || error_operand_p (field))
7063 error ("type transparent %q#T does not have any fields", t);
7064 TYPE_TRANSPARENT_AGGR (t) = 0;
7066 else if (DECL_ARTIFICIAL (field))
7068 if (DECL_FIELD_IS_BASE (field))
7069 error ("type transparent class %qT has base classes", t);
7070 else
7072 gcc_checking_assert (DECL_VIRTUAL_P (field));
7073 error ("type transparent class %qT has virtual functions", t);
7075 TYPE_TRANSPARENT_AGGR (t) = 0;
7077 else if (TYPE_MODE (t) != DECL_MODE (field))
7079 error ("type transparent %q#T cannot be made transparent because "
7080 "the type of the first field has a different ABI from the "
7081 "class overall", t);
7082 TYPE_TRANSPARENT_AGGR (t) = 0;
7087 /* When T was built up, the member declarations were added in reverse
7088 order. Rearrange them to declaration order. */
7090 void
7091 unreverse_member_declarations (tree t)
7093 tree next;
7094 tree prev;
7095 tree x;
7097 /* The following lists are all in reverse order. Put them in
7098 declaration order now. */
7099 CLASSTYPE_DECL_LIST (t) = nreverse (CLASSTYPE_DECL_LIST (t));
7101 /* For the TYPE_FIELDS, only the non TYPE_DECLs are in reverse
7102 order, so we can't just use nreverse. Due to stat_hack
7103 chicanery in finish_member_declaration. */
7104 prev = NULL_TREE;
7105 for (x = TYPE_FIELDS (t);
7106 x && TREE_CODE (x) != TYPE_DECL;
7107 x = next)
7109 next = DECL_CHAIN (x);
7110 DECL_CHAIN (x) = prev;
7111 prev = x;
7114 if (prev)
7116 DECL_CHAIN (TYPE_FIELDS (t)) = x;
7117 TYPE_FIELDS (t) = prev;
7121 tree
7122 finish_struct (tree t, tree attributes)
7124 location_t saved_loc = input_location;
7126 /* Now that we've got all the field declarations, reverse everything
7127 as necessary. */
7128 unreverse_member_declarations (t);
7130 cplus_decl_attributes (&t, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
7131 fixup_attribute_variants (t);
7133 /* Nadger the current location so that diagnostics point to the start of
7134 the struct, not the end. */
7135 input_location = DECL_SOURCE_LOCATION (TYPE_NAME (t));
7137 if (processing_template_decl)
7139 tree x;
7141 /* We need to add the target functions of USING_DECLS, so that
7142 they can be found when the using declaration is not
7143 instantiated yet. */
7144 for (x = TYPE_FIELDS (t); x; x = DECL_CHAIN (x))
7145 if (TREE_CODE (x) == USING_DECL)
7147 tree fn = strip_using_decl (x);
7148 if (OVL_P (fn))
7149 for (lkp_iterator iter (fn); iter; ++iter)
7150 add_method (t, *iter, true);
7152 else if (DECL_DECLARES_FUNCTION_P (x))
7153 DECL_IN_AGGR_P (x) = false;
7155 TYPE_SIZE (t) = bitsize_zero_node;
7156 TYPE_SIZE_UNIT (t) = size_zero_node;
7157 /* COMPLETE_TYPE_P is now true. */
7159 set_class_bindings (t);
7161 /* We need to emit an error message if this type was used as a parameter
7162 and it is an abstract type, even if it is a template. We construct
7163 a simple CLASSTYPE_PURE_VIRTUALS list without taking bases into
7164 account and we call complete_vars with this type, which will check
7165 the PARM_DECLS. Note that while the type is being defined,
7166 CLASSTYPE_PURE_VIRTUALS contains the list of the inline friends
7167 (see CLASSTYPE_INLINE_FRIENDS) so we need to clear it. */
7168 CLASSTYPE_PURE_VIRTUALS (t) = NULL;
7169 for (x = TYPE_FIELDS (t); x; x = DECL_CHAIN (x))
7170 if (TREE_CODE (x) == FUNCTION_DECL && DECL_PURE_VIRTUAL_P (x))
7171 vec_safe_push (CLASSTYPE_PURE_VIRTUALS (t), x);
7172 complete_vars (t);
7174 /* Remember current #pragma pack value. */
7175 TYPE_PRECISION (t) = maximum_field_alignment;
7177 /* Fix up any variants we've already built. */
7178 for (x = TYPE_NEXT_VARIANT (t); x; x = TYPE_NEXT_VARIANT (x))
7180 TYPE_SIZE (x) = TYPE_SIZE (t);
7181 TYPE_SIZE_UNIT (x) = TYPE_SIZE_UNIT (t);
7182 TYPE_FIELDS (x) = TYPE_FIELDS (t);
7185 else
7186 finish_struct_1 (t);
7187 /* COMPLETE_TYPE_P is now true. */
7189 maybe_warn_about_overly_private_class (t);
7191 if (is_std_init_list (t))
7193 /* People keep complaining that the compiler crashes on an invalid
7194 definition of initializer_list, so I guess we should explicitly
7195 reject it. What the compiler internals care about is that it's a
7196 template and has a pointer field followed by size_type field. */
7197 bool ok = false;
7198 if (processing_template_decl)
7200 tree f = next_initializable_field (TYPE_FIELDS (t));
7201 if (f && TYPE_PTR_P (TREE_TYPE (f)))
7203 f = next_initializable_field (DECL_CHAIN (f));
7204 if (f && same_type_p (TREE_TYPE (f), size_type_node))
7205 ok = true;
7208 if (!ok)
7209 fatal_error (input_location, "definition of %qD does not match "
7210 "%<#include <initializer_list>%>", TYPE_NAME (t));
7213 input_location = saved_loc;
7215 TYPE_BEING_DEFINED (t) = 0;
7217 if (current_class_type)
7218 popclass ();
7219 else
7220 error ("trying to finish struct, but kicked out due to previous parse errors");
7222 if (processing_template_decl && at_function_scope_p ()
7223 /* Lambdas are defined by the LAMBDA_EXPR. */
7224 && !LAMBDA_TYPE_P (t))
7225 add_stmt (build_min (TAG_DEFN, t));
7227 return t;
7230 /* Hash table to avoid endless recursion when handling references. */
7231 static hash_table<nofree_ptr_hash<tree_node> > *fixed_type_or_null_ref_ht;
7233 /* Return the dynamic type of INSTANCE, if known.
7234 Used to determine whether the virtual function table is needed
7235 or not.
7237 *NONNULL is set iff INSTANCE can be known to be nonnull, regardless
7238 of our knowledge of its type. *NONNULL should be initialized
7239 before this function is called. */
7241 static tree
7242 fixed_type_or_null (tree instance, int *nonnull, int *cdtorp)
7244 #define RECUR(T) fixed_type_or_null((T), nonnull, cdtorp)
7246 switch (TREE_CODE (instance))
7248 case INDIRECT_REF:
7249 if (INDIRECT_TYPE_P (TREE_TYPE (instance)))
7250 return NULL_TREE;
7251 else
7252 return RECUR (TREE_OPERAND (instance, 0));
7254 case CALL_EXPR:
7255 /* This is a call to a constructor, hence it's never zero. */
7256 if (CALL_EXPR_FN (instance)
7257 && TREE_HAS_CONSTRUCTOR (instance))
7259 if (nonnull)
7260 *nonnull = 1;
7261 return TREE_TYPE (instance);
7263 return NULL_TREE;
7265 case SAVE_EXPR:
7266 /* This is a call to a constructor, hence it's never zero. */
7267 if (TREE_HAS_CONSTRUCTOR (instance))
7269 if (nonnull)
7270 *nonnull = 1;
7271 return TREE_TYPE (instance);
7273 return RECUR (TREE_OPERAND (instance, 0));
7275 case POINTER_PLUS_EXPR:
7276 case PLUS_EXPR:
7277 case MINUS_EXPR:
7278 if (TREE_CODE (TREE_OPERAND (instance, 0)) == ADDR_EXPR)
7279 return RECUR (TREE_OPERAND (instance, 0));
7280 if (TREE_CODE (TREE_OPERAND (instance, 1)) == INTEGER_CST)
7281 /* Propagate nonnull. */
7282 return RECUR (TREE_OPERAND (instance, 0));
7284 return NULL_TREE;
7286 CASE_CONVERT:
7287 return RECUR (TREE_OPERAND (instance, 0));
7289 case ADDR_EXPR:
7290 instance = TREE_OPERAND (instance, 0);
7291 if (nonnull)
7293 /* Just because we see an ADDR_EXPR doesn't mean we're dealing
7294 with a real object -- given &p->f, p can still be null. */
7295 tree t = get_base_address (instance);
7296 /* ??? Probably should check DECL_WEAK here. */
7297 if (t && DECL_P (t))
7298 *nonnull = 1;
7300 return RECUR (instance);
7302 case COMPONENT_REF:
7303 /* If this component is really a base class reference, then the field
7304 itself isn't definitive. */
7305 if (DECL_FIELD_IS_BASE (TREE_OPERAND (instance, 1)))
7306 return RECUR (TREE_OPERAND (instance, 0));
7307 return RECUR (TREE_OPERAND (instance, 1));
7309 case VAR_DECL:
7310 case FIELD_DECL:
7311 if (TREE_CODE (TREE_TYPE (instance)) == ARRAY_TYPE
7312 && MAYBE_CLASS_TYPE_P (TREE_TYPE (TREE_TYPE (instance))))
7314 if (nonnull)
7315 *nonnull = 1;
7316 return TREE_TYPE (TREE_TYPE (instance));
7318 /* fall through. */
7319 case TARGET_EXPR:
7320 case PARM_DECL:
7321 case RESULT_DECL:
7322 if (MAYBE_CLASS_TYPE_P (TREE_TYPE (instance)))
7324 if (nonnull)
7325 *nonnull = 1;
7326 return TREE_TYPE (instance);
7328 else if (instance == current_class_ptr)
7330 if (nonnull)
7331 *nonnull = 1;
7333 /* if we're in a ctor or dtor, we know our type. If
7334 current_class_ptr is set but we aren't in a function, we're in
7335 an NSDMI (and therefore a constructor). */
7336 if (current_scope () != current_function_decl
7337 || (DECL_LANG_SPECIFIC (current_function_decl)
7338 && (DECL_CONSTRUCTOR_P (current_function_decl)
7339 || DECL_DESTRUCTOR_P (current_function_decl))))
7341 if (cdtorp)
7342 *cdtorp = 1;
7343 return TREE_TYPE (TREE_TYPE (instance));
7346 else if (TYPE_REF_P (TREE_TYPE (instance)))
7348 /* We only need one hash table because it is always left empty. */
7349 if (!fixed_type_or_null_ref_ht)
7350 fixed_type_or_null_ref_ht
7351 = new hash_table<nofree_ptr_hash<tree_node> > (37);
7353 /* Reference variables should be references to objects. */
7354 if (nonnull)
7355 *nonnull = 1;
7357 /* Enter the INSTANCE in a table to prevent recursion; a
7358 variable's initializer may refer to the variable
7359 itself. */
7360 if (VAR_P (instance)
7361 && DECL_INITIAL (instance)
7362 && !type_dependent_expression_p_push (DECL_INITIAL (instance))
7363 && !fixed_type_or_null_ref_ht->find (instance))
7365 tree type;
7366 tree_node **slot;
7368 slot = fixed_type_or_null_ref_ht->find_slot (instance, INSERT);
7369 *slot = instance;
7370 type = RECUR (DECL_INITIAL (instance));
7371 fixed_type_or_null_ref_ht->remove_elt (instance);
7373 return type;
7376 return NULL_TREE;
7378 default:
7379 return NULL_TREE;
7381 #undef RECUR
7384 /* Return nonzero if the dynamic type of INSTANCE is known, and
7385 equivalent to the static type. We also handle the case where
7386 INSTANCE is really a pointer. Return negative if this is a
7387 ctor/dtor. There the dynamic type is known, but this might not be
7388 the most derived base of the original object, and hence virtual
7389 bases may not be laid out according to this type.
7391 Used to determine whether the virtual function table is needed
7392 or not.
7394 *NONNULL is set iff INSTANCE can be known to be nonnull, regardless
7395 of our knowledge of its type. *NONNULL should be initialized
7396 before this function is called. */
7399 resolves_to_fixed_type_p (tree instance, int* nonnull)
7401 tree t = TREE_TYPE (instance);
7402 int cdtorp = 0;
7403 tree fixed;
7405 /* processing_template_decl can be false in a template if we're in
7406 instantiate_non_dependent_expr, but we still want to suppress
7407 this check. */
7408 if (in_template_function ())
7410 /* In a template we only care about the type of the result. */
7411 if (nonnull)
7412 *nonnull = true;
7413 return true;
7416 fixed = fixed_type_or_null (instance, nonnull, &cdtorp);
7417 if (fixed == NULL_TREE)
7418 return 0;
7419 if (INDIRECT_TYPE_P (t))
7420 t = TREE_TYPE (t);
7421 if (!same_type_ignoring_top_level_qualifiers_p (t, fixed))
7422 return 0;
7423 return cdtorp ? -1 : 1;
7427 void
7428 init_class_processing (void)
7430 current_class_depth = 0;
7431 current_class_stack_size = 10;
7432 current_class_stack
7433 = XNEWVEC (struct class_stack_node, current_class_stack_size);
7434 vec_alloc (local_classes, 8);
7435 sizeof_biggest_empty_class = size_zero_node;
7437 ridpointers[(int) RID_PUBLIC] = access_public_node;
7438 ridpointers[(int) RID_PRIVATE] = access_private_node;
7439 ridpointers[(int) RID_PROTECTED] = access_protected_node;
7442 /* Restore the cached PREVIOUS_CLASS_LEVEL. */
7444 static void
7445 restore_class_cache (void)
7447 tree type;
7449 /* We are re-entering the same class we just left, so we don't
7450 have to search the whole inheritance matrix to find all the
7451 decls to bind again. Instead, we install the cached
7452 class_shadowed list and walk through it binding names. */
7453 push_binding_level (previous_class_level);
7454 class_binding_level = previous_class_level;
7455 /* Restore IDENTIFIER_TYPE_VALUE. */
7456 for (type = class_binding_level->type_shadowed;
7457 type;
7458 type = TREE_CHAIN (type))
7459 SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (type), TREE_TYPE (type));
7462 /* Set global variables CURRENT_CLASS_NAME and CURRENT_CLASS_TYPE as
7463 appropriate for TYPE.
7465 So that we may avoid calls to lookup_name, we cache the _TYPE
7466 nodes of local TYPE_DECLs in the TREE_TYPE field of the name.
7468 For multiple inheritance, we perform a two-pass depth-first search
7469 of the type lattice. */
7471 void
7472 pushclass (tree type)
7474 class_stack_node_t csn;
7476 type = TYPE_MAIN_VARIANT (type);
7478 /* Make sure there is enough room for the new entry on the stack. */
7479 if (current_class_depth + 1 >= current_class_stack_size)
7481 current_class_stack_size *= 2;
7482 current_class_stack
7483 = XRESIZEVEC (struct class_stack_node, current_class_stack,
7484 current_class_stack_size);
7487 /* Insert a new entry on the class stack. */
7488 csn = current_class_stack + current_class_depth;
7489 csn->name = current_class_name;
7490 csn->type = current_class_type;
7491 csn->access = current_access_specifier;
7492 csn->names_used = 0;
7493 csn->hidden = 0;
7494 current_class_depth++;
7496 /* Now set up the new type. */
7497 current_class_name = TYPE_NAME (type);
7498 if (TREE_CODE (current_class_name) == TYPE_DECL)
7499 current_class_name = DECL_NAME (current_class_name);
7500 current_class_type = type;
7502 /* By default, things in classes are private, while things in
7503 structures or unions are public. */
7504 current_access_specifier = (CLASSTYPE_DECLARED_CLASS (type)
7505 ? access_private_node
7506 : access_public_node);
7508 if (previous_class_level
7509 && type != previous_class_level->this_entity
7510 && current_class_depth == 1)
7512 /* Forcibly remove any old class remnants. */
7513 invalidate_class_lookup_cache ();
7516 if (!previous_class_level
7517 || type != previous_class_level->this_entity
7518 || current_class_depth > 1)
7519 pushlevel_class ();
7520 else
7521 restore_class_cache ();
7524 /* When we exit a toplevel class scope, we save its binding level so
7525 that we can restore it quickly. Here, we've entered some other
7526 class, so we must invalidate our cache. */
7528 void
7529 invalidate_class_lookup_cache (void)
7531 previous_class_level = NULL;
7534 /* Get out of the current class scope. If we were in a class scope
7535 previously, that is the one popped to. */
7537 void
7538 popclass (void)
7540 poplevel_class ();
7542 current_class_depth--;
7543 current_class_name = current_class_stack[current_class_depth].name;
7544 current_class_type = current_class_stack[current_class_depth].type;
7545 current_access_specifier = current_class_stack[current_class_depth].access;
7546 if (current_class_stack[current_class_depth].names_used)
7547 splay_tree_delete (current_class_stack[current_class_depth].names_used);
7550 /* Mark the top of the class stack as hidden. */
7552 void
7553 push_class_stack (void)
7555 if (current_class_depth)
7556 ++current_class_stack[current_class_depth - 1].hidden;
7559 /* Mark the top of the class stack as un-hidden. */
7561 void
7562 pop_class_stack (void)
7564 if (current_class_depth)
7565 --current_class_stack[current_class_depth - 1].hidden;
7568 /* If the class type currently being defined is either T or
7569 a nested type of T, returns the type from the current_class_stack,
7570 which might be equivalent to but not equal to T in case of
7571 constrained partial specializations. */
7573 tree
7574 currently_open_class (tree t)
7576 int i;
7578 if (!CLASS_TYPE_P (t))
7579 return NULL_TREE;
7581 t = TYPE_MAIN_VARIANT (t);
7583 /* We start looking from 1 because entry 0 is from global scope,
7584 and has no type. */
7585 for (i = current_class_depth; i > 0; --i)
7587 tree c;
7588 if (i == current_class_depth)
7589 c = current_class_type;
7590 else
7592 if (current_class_stack[i].hidden)
7593 break;
7594 c = current_class_stack[i].type;
7596 if (!c)
7597 continue;
7598 if (same_type_p (c, t))
7599 return c;
7601 return NULL_TREE;
7604 /* If either current_class_type or one of its enclosing classes are derived
7605 from T, return the appropriate type. Used to determine how we found
7606 something via unqualified lookup. */
7608 tree
7609 currently_open_derived_class (tree t)
7611 int i;
7613 /* The bases of a dependent type are unknown. */
7614 if (dependent_type_p (t))
7615 return NULL_TREE;
7617 if (!current_class_type)
7618 return NULL_TREE;
7620 if (DERIVED_FROM_P (t, current_class_type))
7621 return current_class_type;
7623 for (i = current_class_depth - 1; i > 0; --i)
7625 if (current_class_stack[i].hidden)
7626 break;
7627 if (DERIVED_FROM_P (t, current_class_stack[i].type))
7628 return current_class_stack[i].type;
7631 return NULL_TREE;
7634 /* Return the outermost enclosing class type that is still open, or
7635 NULL_TREE. */
7637 tree
7638 outermost_open_class (void)
7640 if (!current_class_type)
7641 return NULL_TREE;
7642 tree r = NULL_TREE;
7643 if (TYPE_BEING_DEFINED (current_class_type))
7644 r = current_class_type;
7645 for (int i = current_class_depth - 1; i > 0; --i)
7647 if (current_class_stack[i].hidden)
7648 break;
7649 tree t = current_class_stack[i].type;
7650 if (!TYPE_BEING_DEFINED (t))
7651 break;
7652 r = t;
7654 return r;
7657 /* Returns the innermost class type which is not a lambda closure type. */
7659 tree
7660 current_nonlambda_class_type (void)
7662 tree type = current_class_type;
7663 while (type && LAMBDA_TYPE_P (type))
7664 type = decl_type_context (TYPE_NAME (type));
7665 return type;
7668 /* When entering a class scope, all enclosing class scopes' names with
7669 static meaning (static variables, static functions, types and
7670 enumerators) have to be visible. This recursive function calls
7671 pushclass for all enclosing class contexts until global or a local
7672 scope is reached. TYPE is the enclosed class. */
7674 void
7675 push_nested_class (tree type)
7677 /* A namespace might be passed in error cases, like A::B:C. */
7678 if (type == NULL_TREE
7679 || !CLASS_TYPE_P (type))
7680 return;
7682 push_nested_class (DECL_CONTEXT (TYPE_MAIN_DECL (type)));
7684 pushclass (type);
7687 /* Undoes a push_nested_class call. */
7689 void
7690 pop_nested_class (void)
7692 tree context = DECL_CONTEXT (TYPE_MAIN_DECL (current_class_type));
7694 popclass ();
7695 if (context && CLASS_TYPE_P (context))
7696 pop_nested_class ();
7699 /* Returns the number of extern "LANG" blocks we are nested within. */
7702 current_lang_depth (void)
7704 return vec_safe_length (current_lang_base);
7707 /* Set global variables CURRENT_LANG_NAME to appropriate value
7708 so that behavior of name-mangling machinery is correct. */
7710 void
7711 push_lang_context (tree name)
7713 vec_safe_push (current_lang_base, current_lang_name);
7715 if (name == lang_name_cplusplus)
7716 current_lang_name = name;
7717 else if (name == lang_name_c)
7718 current_lang_name = name;
7719 else
7720 error ("language string %<\"%E\"%> not recognized", name);
7723 /* Get out of the current language scope. */
7725 void
7726 pop_lang_context (void)
7728 current_lang_name = current_lang_base->pop ();
7731 /* Type instantiation routines. */
7733 /* Given an OVERLOAD and a TARGET_TYPE, return the function that
7734 matches the TARGET_TYPE. If there is no satisfactory match, return
7735 error_mark_node, and issue an error & warning messages under
7736 control of FLAGS. Permit pointers to member function if FLAGS
7737 permits. If TEMPLATE_ONLY, the name of the overloaded function was
7738 a template-id, and EXPLICIT_TARGS are the explicitly provided
7739 template arguments.
7741 If OVERLOAD is for one or more member functions, then ACCESS_PATH
7742 is the base path used to reference those member functions. If
7743 the address is resolved to a member function, access checks will be
7744 performed and errors issued if appropriate. */
7746 static tree
7747 resolve_address_of_overloaded_function (tree target_type,
7748 tree overload,
7749 tsubst_flags_t complain,
7750 bool template_only,
7751 tree explicit_targs,
7752 tree access_path)
7754 /* Here's what the standard says:
7756 [over.over]
7758 If the name is a function template, template argument deduction
7759 is done, and if the argument deduction succeeds, the deduced
7760 arguments are used to generate a single template function, which
7761 is added to the set of overloaded functions considered.
7763 Non-member functions and static member functions match targets of
7764 type "pointer-to-function" or "reference-to-function." Nonstatic
7765 member functions match targets of type "pointer-to-member
7766 function;" the function type of the pointer to member is used to
7767 select the member function from the set of overloaded member
7768 functions. If a nonstatic member function is selected, the
7769 reference to the overloaded function name is required to have the
7770 form of a pointer to member as described in 5.3.1.
7772 If more than one function is selected, any template functions in
7773 the set are eliminated if the set also contains a non-template
7774 function, and any given template function is eliminated if the
7775 set contains a second template function that is more specialized
7776 than the first according to the partial ordering rules 14.5.5.2.
7777 After such eliminations, if any, there shall remain exactly one
7778 selected function. */
7780 int is_ptrmem = 0;
7781 /* We store the matches in a TREE_LIST rooted here. The functions
7782 are the TREE_PURPOSE, not the TREE_VALUE, in this list, for easy
7783 interoperability with most_specialized_instantiation. */
7784 tree matches = NULL_TREE;
7785 tree fn;
7786 tree target_fn_type;
7788 /* By the time we get here, we should be seeing only real
7789 pointer-to-member types, not the internal POINTER_TYPE to
7790 METHOD_TYPE representation. */
7791 gcc_assert (!TYPE_PTR_P (target_type)
7792 || TREE_CODE (TREE_TYPE (target_type)) != METHOD_TYPE);
7794 gcc_assert (is_overloaded_fn (overload));
7796 /* Check that the TARGET_TYPE is reasonable. */
7797 if (TYPE_PTRFN_P (target_type)
7798 || TYPE_REFFN_P (target_type))
7799 /* This is OK. */;
7800 else if (TYPE_PTRMEMFUNC_P (target_type))
7801 /* This is OK, too. */
7802 is_ptrmem = 1;
7803 else if (TREE_CODE (target_type) == FUNCTION_TYPE)
7804 /* This is OK, too. This comes from a conversion to reference
7805 type. */
7806 target_type = build_reference_type (target_type);
7807 else
7809 if (complain & tf_error)
7810 error ("cannot resolve overloaded function %qD based on"
7811 " conversion to type %qT",
7812 OVL_NAME (overload), target_type);
7813 return error_mark_node;
7816 /* Non-member functions and static member functions match targets of type
7817 "pointer-to-function" or "reference-to-function." Nonstatic member
7818 functions match targets of type "pointer-to-member-function;" the
7819 function type of the pointer to member is used to select the member
7820 function from the set of overloaded member functions.
7822 So figure out the FUNCTION_TYPE that we want to match against. */
7823 target_fn_type = static_fn_type (target_type);
7825 /* If we can find a non-template function that matches, we can just
7826 use it. There's no point in generating template instantiations
7827 if we're just going to throw them out anyhow. But, of course, we
7828 can only do this when we don't *need* a template function. */
7829 if (!template_only)
7830 for (lkp_iterator iter (overload); iter; ++iter)
7832 tree fn = *iter;
7834 if (TREE_CODE (fn) == TEMPLATE_DECL)
7835 /* We're not looking for templates just yet. */
7836 continue;
7838 if ((TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE) != is_ptrmem)
7839 /* We're looking for a non-static member, and this isn't
7840 one, or vice versa. */
7841 continue;
7843 /* In C++17 we need the noexcept-qualifier to compare types. */
7844 if (flag_noexcept_type
7845 && !maybe_instantiate_noexcept (fn, complain))
7846 continue;
7848 /* See if there's a match. */
7849 tree fntype = static_fn_type (fn);
7850 if (same_type_p (target_fn_type, fntype)
7851 || fnptr_conv_p (target_fn_type, fntype))
7852 matches = tree_cons (fn, NULL_TREE, matches);
7855 /* Now, if we've already got a match (or matches), there's no need
7856 to proceed to the template functions. But, if we don't have a
7857 match we need to look at them, too. */
7858 if (!matches)
7860 tree target_arg_types;
7861 tree target_ret_type;
7862 tree *args;
7863 unsigned int nargs, ia;
7864 tree arg;
7866 target_arg_types = TYPE_ARG_TYPES (target_fn_type);
7867 target_ret_type = TREE_TYPE (target_fn_type);
7869 nargs = list_length (target_arg_types);
7870 args = XALLOCAVEC (tree, nargs);
7871 for (arg = target_arg_types, ia = 0;
7872 arg != NULL_TREE && arg != void_list_node;
7873 arg = TREE_CHAIN (arg), ++ia)
7874 args[ia] = TREE_VALUE (arg);
7875 nargs = ia;
7877 for (lkp_iterator iter (overload); iter; ++iter)
7879 tree fn = *iter;
7880 tree instantiation;
7881 tree targs;
7883 if (TREE_CODE (fn) != TEMPLATE_DECL)
7884 /* We're only looking for templates. */
7885 continue;
7887 if ((TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
7888 != is_ptrmem)
7889 /* We're not looking for a non-static member, and this is
7890 one, or vice versa. */
7891 continue;
7893 tree ret = target_ret_type;
7895 /* If the template has a deduced return type, don't expose it to
7896 template argument deduction. */
7897 if (undeduced_auto_decl (fn))
7898 ret = NULL_TREE;
7900 /* Try to do argument deduction. */
7901 targs = make_tree_vec (DECL_NTPARMS (fn));
7902 instantiation = fn_type_unification (fn, explicit_targs, targs, args,
7903 nargs, ret,
7904 DEDUCE_EXACT, LOOKUP_NORMAL,
7905 NULL, false, false);
7906 if (instantiation == error_mark_node)
7907 /* Instantiation failed. */
7908 continue;
7910 /* Constraints must be satisfied. This is done before
7911 return type deduction since that instantiates the
7912 function. */
7913 if (flag_concepts && !constraints_satisfied_p (instantiation))
7914 continue;
7916 /* And now force instantiation to do return type deduction. */
7917 if (undeduced_auto_decl (instantiation))
7919 ++function_depth;
7920 instantiate_decl (instantiation, /*defer*/false, /*class*/false);
7921 --function_depth;
7923 require_deduced_type (instantiation);
7926 /* In C++17 we need the noexcept-qualifier to compare types. */
7927 if (flag_noexcept_type)
7928 maybe_instantiate_noexcept (instantiation, complain);
7930 /* See if there's a match. */
7931 tree fntype = static_fn_type (instantiation);
7932 if (same_type_p (target_fn_type, fntype)
7933 || fnptr_conv_p (target_fn_type, fntype))
7934 matches = tree_cons (instantiation, fn, matches);
7937 /* Now, remove all but the most specialized of the matches. */
7938 if (matches)
7940 tree match = most_specialized_instantiation (matches);
7942 if (match != error_mark_node)
7943 matches = tree_cons (TREE_PURPOSE (match),
7944 NULL_TREE,
7945 NULL_TREE);
7949 /* Now we should have exactly one function in MATCHES. */
7950 if (matches == NULL_TREE)
7952 /* There were *no* matches. */
7953 if (complain & tf_error)
7955 error ("no matches converting function %qD to type %q#T",
7956 OVL_NAME (overload), target_type);
7958 print_candidates (overload);
7960 return error_mark_node;
7962 else if (TREE_CHAIN (matches))
7964 /* There were too many matches. First check if they're all
7965 the same function. */
7966 tree match = NULL_TREE;
7968 fn = TREE_PURPOSE (matches);
7970 /* For multi-versioned functions, more than one match is just fine and
7971 decls_match will return false as they are different. */
7972 for (match = TREE_CHAIN (matches); match; match = TREE_CHAIN (match))
7973 if (!decls_match (fn, TREE_PURPOSE (match))
7974 && !targetm.target_option.function_versions
7975 (fn, TREE_PURPOSE (match)))
7976 break;
7978 if (match)
7980 if (complain & tf_error)
7982 error ("converting overloaded function %qD to type %q#T is ambiguous",
7983 OVL_NAME (overload), target_type);
7985 /* Since print_candidates expects the functions in the
7986 TREE_VALUE slot, we flip them here. */
7987 for (match = matches; match; match = TREE_CHAIN (match))
7988 TREE_VALUE (match) = TREE_PURPOSE (match);
7990 print_candidates (matches);
7993 return error_mark_node;
7997 /* Good, exactly one match. Now, convert it to the correct type. */
7998 fn = TREE_PURPOSE (matches);
8000 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
8001 && !(complain & tf_ptrmem_ok) && !flag_ms_extensions)
8003 static int explained;
8005 if (!(complain & tf_error))
8006 return error_mark_node;
8008 auto_diagnostic_group d;
8009 if (permerror (input_location, "assuming pointer to member %qD", fn)
8010 && !explained)
8012 inform (input_location, "(a pointer to member can only be "
8013 "formed with %<&%E%>)", fn);
8014 explained = 1;
8018 /* If a pointer to a function that is multi-versioned is requested, the
8019 pointer to the dispatcher function is returned instead. This works
8020 well because indirectly calling the function will dispatch the right
8021 function version at run-time. */
8022 if (DECL_FUNCTION_VERSIONED (fn))
8024 fn = get_function_version_dispatcher (fn);
8025 if (fn == NULL)
8026 return error_mark_node;
8027 /* Mark all the versions corresponding to the dispatcher as used. */
8028 if (!(complain & tf_conv))
8029 mark_versions_used (fn);
8032 /* If we're doing overload resolution purely for the purpose of
8033 determining conversion sequences, we should not consider the
8034 function used. If this conversion sequence is selected, the
8035 function will be marked as used at this point. */
8036 if (!(complain & tf_conv))
8038 /* Make =delete work with SFINAE. */
8039 if (DECL_DELETED_FN (fn) && !(complain & tf_error))
8040 return error_mark_node;
8041 if (!mark_used (fn, complain) && !(complain & tf_error))
8042 return error_mark_node;
8045 /* We could not check access to member functions when this
8046 expression was originally created since we did not know at that
8047 time to which function the expression referred. */
8048 if (DECL_FUNCTION_MEMBER_P (fn))
8050 gcc_assert (access_path);
8051 perform_or_defer_access_check (access_path, fn, fn, complain);
8054 if (TYPE_PTRFN_P (target_type) || TYPE_PTRMEMFUNC_P (target_type))
8055 return cp_build_addr_expr (fn, complain);
8056 else
8058 /* The target must be a REFERENCE_TYPE. Above, cp_build_unary_op
8059 will mark the function as addressed, but here we must do it
8060 explicitly. */
8061 cxx_mark_addressable (fn);
8063 return fn;
8067 /* This function will instantiate the type of the expression given in
8068 RHS to match the type of LHSTYPE. If errors exist, then return
8069 error_mark_node. COMPLAIN is a bit mask. If TF_ERROR is set, then
8070 we complain on errors. If we are not complaining, never modify rhs,
8071 as overload resolution wants to try many possible instantiations, in
8072 the hope that at least one will work.
8074 For non-recursive calls, LHSTYPE should be a function, pointer to
8075 function, or a pointer to member function. */
8077 tree
8078 instantiate_type (tree lhstype, tree rhs, tsubst_flags_t complain)
8080 tsubst_flags_t complain_in = complain;
8081 tree access_path = NULL_TREE;
8083 complain &= ~tf_ptrmem_ok;
8085 if (lhstype == unknown_type_node)
8087 if (complain & tf_error)
8088 error ("not enough type information");
8089 return error_mark_node;
8092 if (TREE_TYPE (rhs) != NULL_TREE && ! (type_unknown_p (rhs)))
8094 tree fntype = non_reference (lhstype);
8095 if (same_type_p (fntype, TREE_TYPE (rhs)))
8096 return rhs;
8097 if (fnptr_conv_p (fntype, TREE_TYPE (rhs)))
8098 return rhs;
8099 if (flag_ms_extensions
8100 && TYPE_PTRMEMFUNC_P (fntype)
8101 && !TYPE_PTRMEMFUNC_P (TREE_TYPE (rhs)))
8102 /* Microsoft allows `A::f' to be resolved to a
8103 pointer-to-member. */
8105 else
8107 if (complain & tf_error)
8108 error ("cannot convert %qE from type %qT to type %qT",
8109 rhs, TREE_TYPE (rhs), fntype);
8110 return error_mark_node;
8114 /* If we instantiate a template, and it is a A ?: C expression
8115 with omitted B, look through the SAVE_EXPR. */
8116 if (TREE_CODE (rhs) == SAVE_EXPR)
8117 rhs = TREE_OPERAND (rhs, 0);
8119 if (BASELINK_P (rhs))
8121 access_path = BASELINK_ACCESS_BINFO (rhs);
8122 rhs = BASELINK_FUNCTIONS (rhs);
8125 /* If we are in a template, and have a NON_DEPENDENT_EXPR, we cannot
8126 deduce any type information. */
8127 if (TREE_CODE (rhs) == NON_DEPENDENT_EXPR)
8129 if (complain & tf_error)
8130 error ("not enough type information");
8131 return error_mark_node;
8134 /* There are only a few kinds of expressions that may have a type
8135 dependent on overload resolution. */
8136 gcc_assert (TREE_CODE (rhs) == ADDR_EXPR
8137 || TREE_CODE (rhs) == COMPONENT_REF
8138 || is_overloaded_fn (rhs)
8139 || (flag_ms_extensions && TREE_CODE (rhs) == FUNCTION_DECL));
8141 /* This should really only be used when attempting to distinguish
8142 what sort of a pointer to function we have. For now, any
8143 arithmetic operation which is not supported on pointers
8144 is rejected as an error. */
8146 switch (TREE_CODE (rhs))
8148 case COMPONENT_REF:
8150 tree member = TREE_OPERAND (rhs, 1);
8152 member = instantiate_type (lhstype, member, complain);
8153 if (member != error_mark_node
8154 && TREE_SIDE_EFFECTS (TREE_OPERAND (rhs, 0)))
8155 /* Do not lose object's side effects. */
8156 return build2 (COMPOUND_EXPR, TREE_TYPE (member),
8157 TREE_OPERAND (rhs, 0), member);
8158 return member;
8161 case OFFSET_REF:
8162 rhs = TREE_OPERAND (rhs, 1);
8163 if (BASELINK_P (rhs))
8164 return instantiate_type (lhstype, rhs, complain_in);
8166 /* This can happen if we are forming a pointer-to-member for a
8167 member template. */
8168 gcc_assert (TREE_CODE (rhs) == TEMPLATE_ID_EXPR);
8170 /* Fall through. */
8172 case TEMPLATE_ID_EXPR:
8174 tree fns = TREE_OPERAND (rhs, 0);
8175 tree args = TREE_OPERAND (rhs, 1);
8177 return
8178 resolve_address_of_overloaded_function (lhstype, fns, complain_in,
8179 /*template_only=*/true,
8180 args, access_path);
8183 case OVERLOAD:
8184 case FUNCTION_DECL:
8185 return
8186 resolve_address_of_overloaded_function (lhstype, rhs, complain_in,
8187 /*template_only=*/false,
8188 /*explicit_targs=*/NULL_TREE,
8189 access_path);
8191 case ADDR_EXPR:
8193 if (PTRMEM_OK_P (rhs))
8194 complain |= tf_ptrmem_ok;
8196 return instantiate_type (lhstype, TREE_OPERAND (rhs, 0), complain);
8199 case ERROR_MARK:
8200 return error_mark_node;
8202 default:
8203 gcc_unreachable ();
8205 return error_mark_node;
8208 /* Return the name of the virtual function pointer field
8209 (as an IDENTIFIER_NODE) for the given TYPE. Note that
8210 this may have to look back through base types to find the
8211 ultimate field name. (For single inheritance, these could
8212 all be the same name. Who knows for multiple inheritance). */
8214 static tree
8215 get_vfield_name (tree type)
8217 tree binfo, base_binfo;
8219 for (binfo = TYPE_BINFO (type);
8220 BINFO_N_BASE_BINFOS (binfo);
8221 binfo = base_binfo)
8223 base_binfo = BINFO_BASE_BINFO (binfo, 0);
8225 if (BINFO_VIRTUAL_P (base_binfo)
8226 || !TYPE_CONTAINS_VPTR_P (BINFO_TYPE (base_binfo)))
8227 break;
8230 type = BINFO_TYPE (binfo);
8231 tree ctor_name = constructor_name (type);
8232 char *buf = (char *) alloca (sizeof (VFIELD_NAME_FORMAT)
8233 + IDENTIFIER_LENGTH (ctor_name) + 2);
8234 sprintf (buf, VFIELD_NAME_FORMAT, IDENTIFIER_POINTER (ctor_name));
8235 return get_identifier (buf);
8238 /* Build a dummy reference to ourselves so Derived::Base (and A::A) works,
8239 according to [class]:
8240 The class-name is also inserted
8241 into the scope of the class itself. For purposes of access checking,
8242 the inserted class name is treated as if it were a public member name. */
8244 void
8245 build_self_reference (void)
8247 tree name = DECL_NAME (TYPE_NAME (current_class_type));
8248 tree value = build_lang_decl (TYPE_DECL, name, current_class_type);
8250 DECL_NONLOCAL (value) = 1;
8251 DECL_CONTEXT (value) = current_class_type;
8252 DECL_ARTIFICIAL (value) = 1;
8253 SET_DECL_SELF_REFERENCE_P (value);
8254 set_underlying_type (value);
8256 if (processing_template_decl)
8257 value = push_template_decl (value);
8259 tree saved_cas = current_access_specifier;
8260 current_access_specifier = access_public_node;
8261 finish_member_declaration (value);
8262 current_access_specifier = saved_cas;
8265 /* Returns 1 if TYPE contains only padding bytes. */
8268 is_empty_class (tree type)
8270 if (type == error_mark_node)
8271 return 0;
8273 if (! CLASS_TYPE_P (type))
8274 return 0;
8276 return CLASSTYPE_EMPTY_P (type);
8279 /* Returns true if TYPE contains no actual data, just various
8280 possible combinations of empty classes and possibly a vptr. */
8282 bool
8283 is_really_empty_class (tree type)
8285 if (CLASS_TYPE_P (type))
8287 tree field;
8288 tree binfo;
8289 tree base_binfo;
8290 int i;
8292 /* CLASSTYPE_EMPTY_P isn't set properly until the class is actually laid
8293 out, but we'd like to be able to check this before then. */
8294 if (COMPLETE_TYPE_P (type) && is_empty_class (type))
8295 return true;
8297 for (binfo = TYPE_BINFO (type), i = 0;
8298 BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
8299 if (!is_really_empty_class (BINFO_TYPE (base_binfo)))
8300 return false;
8301 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
8302 if (TREE_CODE (field) == FIELD_DECL
8303 && !DECL_ARTIFICIAL (field)
8304 /* An unnamed bit-field is not a data member. */
8305 && !DECL_UNNAMED_BIT_FIELD (field)
8306 && !is_really_empty_class (TREE_TYPE (field)))
8307 return false;
8308 return true;
8310 else if (TREE_CODE (type) == ARRAY_TYPE)
8311 return (integer_zerop (array_type_nelts_top (type))
8312 || is_really_empty_class (TREE_TYPE (type)));
8313 return false;
8316 /* Note that NAME was looked up while the current class was being
8317 defined and that the result of that lookup was DECL. */
8319 void
8320 maybe_note_name_used_in_class (tree name, tree decl)
8322 splay_tree names_used;
8324 /* If we're not defining a class, there's nothing to do. */
8325 if (!(innermost_scope_kind() == sk_class
8326 && TYPE_BEING_DEFINED (current_class_type)
8327 && !LAMBDA_TYPE_P (current_class_type)))
8328 return;
8330 /* If there's already a binding for this NAME, then we don't have
8331 anything to worry about. */
8332 if (lookup_member (current_class_type, name,
8333 /*protect=*/0, /*want_type=*/false, tf_warning_or_error))
8334 return;
8336 if (!current_class_stack[current_class_depth - 1].names_used)
8337 current_class_stack[current_class_depth - 1].names_used
8338 = splay_tree_new (splay_tree_compare_pointers, 0, 0);
8339 names_used = current_class_stack[current_class_depth - 1].names_used;
8341 splay_tree_insert (names_used,
8342 (splay_tree_key) name,
8343 (splay_tree_value) decl);
8346 /* Note that NAME was declared (as DECL) in the current class. Check
8347 to see that the declaration is valid. */
8349 void
8350 note_name_declared_in_class (tree name, tree decl)
8352 splay_tree names_used;
8353 splay_tree_node n;
8355 /* Look to see if we ever used this name. */
8356 names_used
8357 = current_class_stack[current_class_depth - 1].names_used;
8358 if (!names_used)
8359 return;
8360 /* The C language allows members to be declared with a type of the same
8361 name, and the C++ standard says this diagnostic is not required. So
8362 allow it in extern "C" blocks unless predantic is specified.
8363 Allow it in all cases if -ms-extensions is specified. */
8364 if ((!pedantic && current_lang_name == lang_name_c)
8365 || flag_ms_extensions)
8366 return;
8367 n = splay_tree_lookup (names_used, (splay_tree_key) name);
8368 if (n)
8370 /* [basic.scope.class]
8372 A name N used in a class S shall refer to the same declaration
8373 in its context and when re-evaluated in the completed scope of
8374 S. */
8375 if (permerror (location_of (decl),
8376 "declaration of %q#D changes meaning of %qD",
8377 decl, OVL_NAME (decl)))
8378 inform (location_of ((tree) n->value),
8379 "%qD declared here as %q#D",
8380 OVL_NAME (decl), (tree) n->value);
8384 /* Returns the VAR_DECL for the complete vtable associated with BINFO.
8385 Secondary vtables are merged with primary vtables; this function
8386 will return the VAR_DECL for the primary vtable. */
8388 tree
8389 get_vtbl_decl_for_binfo (tree binfo)
8391 tree decl;
8393 decl = BINFO_VTABLE (binfo);
8394 if (decl && TREE_CODE (decl) == POINTER_PLUS_EXPR)
8396 gcc_assert (TREE_CODE (TREE_OPERAND (decl, 0)) == ADDR_EXPR);
8397 decl = TREE_OPERAND (TREE_OPERAND (decl, 0), 0);
8399 if (decl)
8400 gcc_assert (VAR_P (decl));
8401 return decl;
8405 /* Returns the binfo for the primary base of BINFO. If the resulting
8406 BINFO is a virtual base, and it is inherited elsewhere in the
8407 hierarchy, then the returned binfo might not be the primary base of
8408 BINFO in the complete object. Check BINFO_PRIMARY_P or
8409 BINFO_LOST_PRIMARY_P to be sure. */
8411 static tree
8412 get_primary_binfo (tree binfo)
8414 tree primary_base;
8416 primary_base = CLASSTYPE_PRIMARY_BINFO (BINFO_TYPE (binfo));
8417 if (!primary_base)
8418 return NULL_TREE;
8420 return copied_binfo (primary_base, binfo);
8423 /* As above, but iterate until we reach the binfo that actually provides the
8424 vptr for BINFO. */
8426 static tree
8427 most_primary_binfo (tree binfo)
8429 tree b = binfo;
8430 while (CLASSTYPE_HAS_PRIMARY_BASE_P (BINFO_TYPE (b))
8431 && !BINFO_LOST_PRIMARY_P (b))
8433 tree primary_base = get_primary_binfo (b);
8434 gcc_assert (BINFO_PRIMARY_P (primary_base)
8435 && BINFO_INHERITANCE_CHAIN (primary_base) == b);
8436 b = primary_base;
8438 return b;
8441 /* Returns true if BINFO gets its vptr from a virtual base of the most derived
8442 type. Note that the virtual inheritance might be above or below BINFO in
8443 the hierarchy. */
8445 bool
8446 vptr_via_virtual_p (tree binfo)
8448 if (TYPE_P (binfo))
8449 binfo = TYPE_BINFO (binfo);
8450 tree primary = most_primary_binfo (binfo);
8451 /* Don't limit binfo_via_virtual, we want to return true when BINFO itself is
8452 a morally virtual base. */
8453 tree virt = binfo_via_virtual (primary, NULL_TREE);
8454 return virt != NULL_TREE;
8457 /* If INDENTED_P is zero, indent to INDENT. Return nonzero. */
8459 static int
8460 maybe_indent_hierarchy (FILE * stream, int indent, int indented_p)
8462 if (!indented_p)
8463 fprintf (stream, "%*s", indent, "");
8464 return 1;
8467 /* Dump the offsets of all the bases rooted at BINFO to STREAM.
8468 INDENT should be zero when called from the top level; it is
8469 incremented recursively. IGO indicates the next expected BINFO in
8470 inheritance graph ordering. */
8472 static tree
8473 dump_class_hierarchy_r (FILE *stream,
8474 dump_flags_t flags,
8475 tree binfo,
8476 tree igo,
8477 int indent)
8479 int indented = 0;
8480 tree base_binfo;
8481 int i;
8483 indented = maybe_indent_hierarchy (stream, indent, 0);
8484 fprintf (stream, "%s (0x" HOST_WIDE_INT_PRINT_HEX ") ",
8485 type_as_string (BINFO_TYPE (binfo), TFF_PLAIN_IDENTIFIER),
8486 (HOST_WIDE_INT) (uintptr_t) binfo);
8487 if (binfo != igo)
8489 fprintf (stream, "alternative-path\n");
8490 return igo;
8492 igo = TREE_CHAIN (binfo);
8494 fprintf (stream, HOST_WIDE_INT_PRINT_DEC,
8495 tree_to_shwi (BINFO_OFFSET (binfo)));
8496 if (is_empty_class (BINFO_TYPE (binfo)))
8497 fprintf (stream, " empty");
8498 else if (CLASSTYPE_NEARLY_EMPTY_P (BINFO_TYPE (binfo)))
8499 fprintf (stream, " nearly-empty");
8500 if (BINFO_VIRTUAL_P (binfo))
8501 fprintf (stream, " virtual");
8502 fprintf (stream, "\n");
8504 indented = 0;
8505 if (BINFO_PRIMARY_P (binfo))
8507 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
8508 fprintf (stream, " primary-for %s (0x" HOST_WIDE_INT_PRINT_HEX ")",
8509 type_as_string (BINFO_TYPE (BINFO_INHERITANCE_CHAIN (binfo)),
8510 TFF_PLAIN_IDENTIFIER),
8511 (HOST_WIDE_INT) (uintptr_t) BINFO_INHERITANCE_CHAIN (binfo));
8513 if (BINFO_LOST_PRIMARY_P (binfo))
8515 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
8516 fprintf (stream, " lost-primary");
8518 if (indented)
8519 fprintf (stream, "\n");
8521 if (!(flags & TDF_SLIM))
8523 int indented = 0;
8525 if (BINFO_SUBVTT_INDEX (binfo))
8527 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
8528 fprintf (stream, " subvttidx=%s",
8529 expr_as_string (BINFO_SUBVTT_INDEX (binfo),
8530 TFF_PLAIN_IDENTIFIER));
8532 if (BINFO_VPTR_INDEX (binfo))
8534 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
8535 fprintf (stream, " vptridx=%s",
8536 expr_as_string (BINFO_VPTR_INDEX (binfo),
8537 TFF_PLAIN_IDENTIFIER));
8539 if (BINFO_VPTR_FIELD (binfo))
8541 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
8542 fprintf (stream, " vbaseoffset=%s",
8543 expr_as_string (BINFO_VPTR_FIELD (binfo),
8544 TFF_PLAIN_IDENTIFIER));
8546 if (BINFO_VTABLE (binfo))
8548 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
8549 fprintf (stream, " vptr=%s",
8550 expr_as_string (BINFO_VTABLE (binfo),
8551 TFF_PLAIN_IDENTIFIER));
8554 if (indented)
8555 fprintf (stream, "\n");
8558 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
8559 igo = dump_class_hierarchy_r (stream, flags, base_binfo, igo, indent + 2);
8561 return igo;
8564 /* Dump the BINFO hierarchy for T. */
8566 static void
8567 dump_class_hierarchy_1 (FILE *stream, dump_flags_t flags, tree t)
8569 fprintf (stream, "Class %s\n", type_as_string (t, TFF_PLAIN_IDENTIFIER));
8570 fprintf (stream, " size=%lu align=%lu\n",
8571 (unsigned long)(tree_to_shwi (TYPE_SIZE (t)) / BITS_PER_UNIT),
8572 (unsigned long)(TYPE_ALIGN (t) / BITS_PER_UNIT));
8573 fprintf (stream, " base size=%lu base align=%lu\n",
8574 (unsigned long)(tree_to_shwi (TYPE_SIZE (CLASSTYPE_AS_BASE (t)))
8575 / BITS_PER_UNIT),
8576 (unsigned long)(TYPE_ALIGN (CLASSTYPE_AS_BASE (t))
8577 / BITS_PER_UNIT));
8578 dump_class_hierarchy_r (stream, flags, TYPE_BINFO (t), TYPE_BINFO (t), 0);
8579 fprintf (stream, "\n");
8582 /* Debug interface to hierarchy dumping. */
8584 void
8585 debug_class (tree t)
8587 dump_class_hierarchy_1 (stderr, TDF_SLIM, t);
8590 static void
8591 dump_class_hierarchy (tree t)
8593 dump_flags_t flags;
8594 if (FILE *stream = dump_begin (class_dump_id, &flags))
8596 dump_class_hierarchy_1 (stream, flags, t);
8597 dump_end (class_dump_id, stream);
8601 static void
8602 dump_array (FILE * stream, tree decl)
8604 tree value;
8605 unsigned HOST_WIDE_INT ix;
8606 HOST_WIDE_INT elt;
8607 tree size = TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (decl)));
8609 elt = (tree_to_shwi (TYPE_SIZE (TREE_TYPE (TREE_TYPE (decl))))
8610 / BITS_PER_UNIT);
8611 fprintf (stream, "%s:", decl_as_string (decl, TFF_PLAIN_IDENTIFIER));
8612 fprintf (stream, " %s entries",
8613 expr_as_string (size_binop (PLUS_EXPR, size, size_one_node),
8614 TFF_PLAIN_IDENTIFIER));
8615 fprintf (stream, "\n");
8617 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (DECL_INITIAL (decl)),
8618 ix, value)
8619 fprintf (stream, "%-4ld %s\n", (long)(ix * elt),
8620 expr_as_string (value, TFF_PLAIN_IDENTIFIER));
8623 static void
8624 dump_vtable (tree t, tree binfo, tree vtable)
8626 dump_flags_t flags;
8627 FILE *stream = dump_begin (class_dump_id, &flags);
8629 if (!stream)
8630 return;
8632 if (!(flags & TDF_SLIM))
8634 int ctor_vtbl_p = TYPE_BINFO (t) != binfo;
8636 fprintf (stream, "%s for %s",
8637 ctor_vtbl_p ? "Construction vtable" : "Vtable",
8638 type_as_string (BINFO_TYPE (binfo), TFF_PLAIN_IDENTIFIER));
8639 if (ctor_vtbl_p)
8641 if (!BINFO_VIRTUAL_P (binfo))
8642 fprintf (stream, " (0x" HOST_WIDE_INT_PRINT_HEX " instance)",
8643 (HOST_WIDE_INT) (uintptr_t) binfo);
8644 fprintf (stream, " in %s", type_as_string (t, TFF_PLAIN_IDENTIFIER));
8646 fprintf (stream, "\n");
8647 dump_array (stream, vtable);
8648 fprintf (stream, "\n");
8651 dump_end (class_dump_id, stream);
8654 static void
8655 dump_vtt (tree t, tree vtt)
8657 dump_flags_t flags;
8658 FILE *stream = dump_begin (class_dump_id, &flags);
8660 if (!stream)
8661 return;
8663 if (!(flags & TDF_SLIM))
8665 fprintf (stream, "VTT for %s\n",
8666 type_as_string (t, TFF_PLAIN_IDENTIFIER));
8667 dump_array (stream, vtt);
8668 fprintf (stream, "\n");
8671 dump_end (class_dump_id, stream);
8674 /* Dump a function or thunk and its thunkees. */
8676 static void
8677 dump_thunk (FILE *stream, int indent, tree thunk)
8679 static const char spaces[] = " ";
8680 tree name = DECL_NAME (thunk);
8681 tree thunks;
8683 fprintf (stream, "%.*s%p %s %s", indent, spaces,
8684 (void *)thunk,
8685 !DECL_THUNK_P (thunk) ? "function"
8686 : DECL_THIS_THUNK_P (thunk) ? "this-thunk" : "covariant-thunk",
8687 name ? IDENTIFIER_POINTER (name) : "<unset>");
8688 if (DECL_THUNK_P (thunk))
8690 HOST_WIDE_INT fixed_adjust = THUNK_FIXED_OFFSET (thunk);
8691 tree virtual_adjust = THUNK_VIRTUAL_OFFSET (thunk);
8693 fprintf (stream, " fixed=" HOST_WIDE_INT_PRINT_DEC, fixed_adjust);
8694 if (!virtual_adjust)
8695 /*NOP*/;
8696 else if (DECL_THIS_THUNK_P (thunk))
8697 fprintf (stream, " vcall=" HOST_WIDE_INT_PRINT_DEC,
8698 tree_to_shwi (virtual_adjust));
8699 else
8700 fprintf (stream, " vbase=" HOST_WIDE_INT_PRINT_DEC "(%s)",
8701 tree_to_shwi (BINFO_VPTR_FIELD (virtual_adjust)),
8702 type_as_string (BINFO_TYPE (virtual_adjust), TFF_SCOPE));
8703 if (THUNK_ALIAS (thunk))
8704 fprintf (stream, " alias to %p", (void *)THUNK_ALIAS (thunk));
8706 fprintf (stream, "\n");
8707 for (thunks = DECL_THUNKS (thunk); thunks; thunks = TREE_CHAIN (thunks))
8708 dump_thunk (stream, indent + 2, thunks);
8711 /* Dump the thunks for FN. */
8713 void
8714 debug_thunks (tree fn)
8716 dump_thunk (stderr, 0, fn);
8719 /* Virtual function table initialization. */
8721 /* Create all the necessary vtables for T and its base classes. */
8723 static void
8724 finish_vtbls (tree t)
8726 tree vbase;
8727 vec<constructor_elt, va_gc> *v = NULL;
8728 tree vtable = BINFO_VTABLE (TYPE_BINFO (t));
8730 /* We lay out the primary and secondary vtables in one contiguous
8731 vtable. The primary vtable is first, followed by the non-virtual
8732 secondary vtables in inheritance graph order. */
8733 accumulate_vtbl_inits (TYPE_BINFO (t), TYPE_BINFO (t), TYPE_BINFO (t),
8734 vtable, t, &v);
8736 /* Then come the virtual bases, also in inheritance graph order. */
8737 for (vbase = TYPE_BINFO (t); vbase; vbase = TREE_CHAIN (vbase))
8739 if (!BINFO_VIRTUAL_P (vbase))
8740 continue;
8741 accumulate_vtbl_inits (vbase, vbase, TYPE_BINFO (t), vtable, t, &v);
8744 if (BINFO_VTABLE (TYPE_BINFO (t)))
8745 initialize_vtable (TYPE_BINFO (t), v);
8748 /* Initialize the vtable for BINFO with the INITS. */
8750 static void
8751 initialize_vtable (tree binfo, vec<constructor_elt, va_gc> *inits)
8753 tree decl;
8755 layout_vtable_decl (binfo, vec_safe_length (inits));
8756 decl = get_vtbl_decl_for_binfo (binfo);
8757 initialize_artificial_var (decl, inits);
8758 dump_vtable (BINFO_TYPE (binfo), binfo, decl);
8761 /* Build the VTT (virtual table table) for T.
8762 A class requires a VTT if it has virtual bases.
8764 This holds
8765 1 - primary virtual pointer for complete object T
8766 2 - secondary VTTs for each direct non-virtual base of T which requires a
8768 3 - secondary virtual pointers for each direct or indirect base of T which
8769 has virtual bases or is reachable via a virtual path from T.
8770 4 - secondary VTTs for each direct or indirect virtual base of T.
8772 Secondary VTTs look like complete object VTTs without part 4. */
8774 static void
8775 build_vtt (tree t)
8777 tree type;
8778 tree vtt;
8779 tree index;
8780 vec<constructor_elt, va_gc> *inits;
8782 /* Build up the initializers for the VTT. */
8783 inits = NULL;
8784 index = size_zero_node;
8785 build_vtt_inits (TYPE_BINFO (t), t, &inits, &index);
8787 /* If we didn't need a VTT, we're done. */
8788 if (!inits)
8789 return;
8791 /* Figure out the type of the VTT. */
8792 type = build_array_of_n_type (const_ptr_type_node,
8793 inits->length ());
8795 /* Now, build the VTT object itself. */
8796 vtt = build_vtable (t, mangle_vtt_for_type (t), type);
8797 initialize_artificial_var (vtt, inits);
8798 /* Add the VTT to the vtables list. */
8799 DECL_CHAIN (vtt) = DECL_CHAIN (CLASSTYPE_VTABLES (t));
8800 DECL_CHAIN (CLASSTYPE_VTABLES (t)) = vtt;
8802 dump_vtt (t, vtt);
8805 /* When building a secondary VTT, BINFO_VTABLE is set to a TREE_LIST with
8806 PURPOSE the RTTI_BINFO, VALUE the real vtable pointer for this binfo,
8807 and CHAIN the vtable pointer for this binfo after construction is
8808 complete. VALUE can also be another BINFO, in which case we recurse. */
8810 static tree
8811 binfo_ctor_vtable (tree binfo)
8813 tree vt;
8815 while (1)
8817 vt = BINFO_VTABLE (binfo);
8818 if (TREE_CODE (vt) == TREE_LIST)
8819 vt = TREE_VALUE (vt);
8820 if (TREE_CODE (vt) == TREE_BINFO)
8821 binfo = vt;
8822 else
8823 break;
8826 return vt;
8829 /* Data for secondary VTT initialization. */
8830 struct secondary_vptr_vtt_init_data
8832 /* Is this the primary VTT? */
8833 bool top_level_p;
8835 /* Current index into the VTT. */
8836 tree index;
8838 /* Vector of initializers built up. */
8839 vec<constructor_elt, va_gc> *inits;
8841 /* The type being constructed by this secondary VTT. */
8842 tree type_being_constructed;
8845 /* Recursively build the VTT-initializer for BINFO (which is in the
8846 hierarchy dominated by T). INITS points to the end of the initializer
8847 list to date. INDEX is the VTT index where the next element will be
8848 replaced. Iff BINFO is the binfo for T, this is the top level VTT (i.e.
8849 not a subvtt for some base of T). When that is so, we emit the sub-VTTs
8850 for virtual bases of T. When it is not so, we build the constructor
8851 vtables for the BINFO-in-T variant. */
8853 static void
8854 build_vtt_inits (tree binfo, tree t, vec<constructor_elt, va_gc> **inits,
8855 tree *index)
8857 int i;
8858 tree b;
8859 tree init;
8860 secondary_vptr_vtt_init_data data;
8861 int top_level_p = SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), t);
8863 /* We only need VTTs for subobjects with virtual bases. */
8864 if (!CLASSTYPE_VBASECLASSES (BINFO_TYPE (binfo)))
8865 return;
8867 /* We need to use a construction vtable if this is not the primary
8868 VTT. */
8869 if (!top_level_p)
8871 build_ctor_vtbl_group (binfo, t);
8873 /* Record the offset in the VTT where this sub-VTT can be found. */
8874 BINFO_SUBVTT_INDEX (binfo) = *index;
8877 /* Add the address of the primary vtable for the complete object. */
8878 init = binfo_ctor_vtable (binfo);
8879 CONSTRUCTOR_APPEND_ELT (*inits, NULL_TREE, init);
8880 if (top_level_p)
8882 gcc_assert (!BINFO_VPTR_INDEX (binfo));
8883 BINFO_VPTR_INDEX (binfo) = *index;
8885 *index = size_binop (PLUS_EXPR, *index, TYPE_SIZE_UNIT (ptr_type_node));
8887 /* Recursively add the secondary VTTs for non-virtual bases. */
8888 for (i = 0; BINFO_BASE_ITERATE (binfo, i, b); ++i)
8889 if (!BINFO_VIRTUAL_P (b))
8890 build_vtt_inits (b, t, inits, index);
8892 /* Add secondary virtual pointers for all subobjects of BINFO with
8893 either virtual bases or reachable along a virtual path, except
8894 subobjects that are non-virtual primary bases. */
8895 data.top_level_p = top_level_p;
8896 data.index = *index;
8897 data.inits = *inits;
8898 data.type_being_constructed = BINFO_TYPE (binfo);
8900 dfs_walk_once (binfo, dfs_build_secondary_vptr_vtt_inits, NULL, &data);
8902 *index = data.index;
8904 /* data.inits might have grown as we added secondary virtual pointers.
8905 Make sure our caller knows about the new vector. */
8906 *inits = data.inits;
8908 if (top_level_p)
8909 /* Add the secondary VTTs for virtual bases in inheritance graph
8910 order. */
8911 for (b = TYPE_BINFO (BINFO_TYPE (binfo)); b; b = TREE_CHAIN (b))
8913 if (!BINFO_VIRTUAL_P (b))
8914 continue;
8916 build_vtt_inits (b, t, inits, index);
8918 else
8919 /* Remove the ctor vtables we created. */
8920 dfs_walk_all (binfo, dfs_fixup_binfo_vtbls, NULL, binfo);
8923 /* Called from build_vtt_inits via dfs_walk. BINFO is the binfo for the base
8924 in most derived. DATA is a SECONDARY_VPTR_VTT_INIT_DATA structure. */
8926 static tree
8927 dfs_build_secondary_vptr_vtt_inits (tree binfo, void *data_)
8929 secondary_vptr_vtt_init_data *data = (secondary_vptr_vtt_init_data *)data_;
8931 /* We don't care about bases that don't have vtables. */
8932 if (!TYPE_VFIELD (BINFO_TYPE (binfo)))
8933 return dfs_skip_bases;
8935 /* We're only interested in proper subobjects of the type being
8936 constructed. */
8937 if (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), data->type_being_constructed))
8938 return NULL_TREE;
8940 /* We're only interested in bases with virtual bases or reachable
8941 via a virtual path from the type being constructed. */
8942 if (!(CLASSTYPE_VBASECLASSES (BINFO_TYPE (binfo))
8943 || binfo_via_virtual (binfo, data->type_being_constructed)))
8944 return dfs_skip_bases;
8946 /* We're not interested in non-virtual primary bases. */
8947 if (!BINFO_VIRTUAL_P (binfo) && BINFO_PRIMARY_P (binfo))
8948 return NULL_TREE;
8950 /* Record the index where this secondary vptr can be found. */
8951 if (data->top_level_p)
8953 gcc_assert (!BINFO_VPTR_INDEX (binfo));
8954 BINFO_VPTR_INDEX (binfo) = data->index;
8956 if (BINFO_VIRTUAL_P (binfo))
8958 /* It's a primary virtual base, and this is not a
8959 construction vtable. Find the base this is primary of in
8960 the inheritance graph, and use that base's vtable
8961 now. */
8962 while (BINFO_PRIMARY_P (binfo))
8963 binfo = BINFO_INHERITANCE_CHAIN (binfo);
8967 /* Add the initializer for the secondary vptr itself. */
8968 CONSTRUCTOR_APPEND_ELT (data->inits, NULL_TREE, binfo_ctor_vtable (binfo));
8970 /* Advance the vtt index. */
8971 data->index = size_binop (PLUS_EXPR, data->index,
8972 TYPE_SIZE_UNIT (ptr_type_node));
8974 return NULL_TREE;
8977 /* Called from build_vtt_inits via dfs_walk. After building
8978 constructor vtables and generating the sub-vtt from them, we need
8979 to restore the BINFO_VTABLES that were scribbled on. DATA is the
8980 binfo of the base whose sub vtt was generated. */
8982 static tree
8983 dfs_fixup_binfo_vtbls (tree binfo, void* data)
8985 tree vtable = BINFO_VTABLE (binfo);
8987 if (!TYPE_CONTAINS_VPTR_P (BINFO_TYPE (binfo)))
8988 /* If this class has no vtable, none of its bases do. */
8989 return dfs_skip_bases;
8991 if (!vtable)
8992 /* This might be a primary base, so have no vtable in this
8993 hierarchy. */
8994 return NULL_TREE;
8996 /* If we scribbled the construction vtable vptr into BINFO, clear it
8997 out now. */
8998 if (TREE_CODE (vtable) == TREE_LIST
8999 && (TREE_PURPOSE (vtable) == (tree) data))
9000 BINFO_VTABLE (binfo) = TREE_CHAIN (vtable);
9002 return NULL_TREE;
9005 /* Build the construction vtable group for BINFO which is in the
9006 hierarchy dominated by T. */
9008 static void
9009 build_ctor_vtbl_group (tree binfo, tree t)
9011 tree type;
9012 tree vtbl;
9013 tree id;
9014 tree vbase;
9015 vec<constructor_elt, va_gc> *v;
9017 /* See if we've already created this construction vtable group. */
9018 id = mangle_ctor_vtbl_for_type (t, binfo);
9019 if (get_global_binding (id))
9020 return;
9022 gcc_assert (!SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), t));
9023 /* Build a version of VTBL (with the wrong type) for use in
9024 constructing the addresses of secondary vtables in the
9025 construction vtable group. */
9026 vtbl = build_vtable (t, id, ptr_type_node);
9027 DECL_CONSTRUCTION_VTABLE_P (vtbl) = 1;
9028 /* Don't export construction vtables from shared libraries. Even on
9029 targets that don't support hidden visibility, this tells
9030 can_refer_decl_in_current_unit_p not to assume that it's safe to
9031 access from a different compilation unit (bz 54314). */
9032 DECL_VISIBILITY (vtbl) = VISIBILITY_HIDDEN;
9033 DECL_VISIBILITY_SPECIFIED (vtbl) = true;
9035 v = NULL;
9036 accumulate_vtbl_inits (binfo, TYPE_BINFO (TREE_TYPE (binfo)),
9037 binfo, vtbl, t, &v);
9039 /* Add the vtables for each of our virtual bases using the vbase in T
9040 binfo. */
9041 for (vbase = TYPE_BINFO (BINFO_TYPE (binfo));
9042 vbase;
9043 vbase = TREE_CHAIN (vbase))
9045 tree b;
9047 if (!BINFO_VIRTUAL_P (vbase))
9048 continue;
9049 b = copied_binfo (vbase, binfo);
9051 accumulate_vtbl_inits (b, vbase, binfo, vtbl, t, &v);
9054 /* Figure out the type of the construction vtable. */
9055 type = build_array_of_n_type (vtable_entry_type, v->length ());
9056 layout_type (type);
9057 TREE_TYPE (vtbl) = type;
9058 DECL_SIZE (vtbl) = DECL_SIZE_UNIT (vtbl) = NULL_TREE;
9059 layout_decl (vtbl, 0);
9061 /* Initialize the construction vtable. */
9062 CLASSTYPE_VTABLES (t) = chainon (CLASSTYPE_VTABLES (t), vtbl);
9063 initialize_artificial_var (vtbl, v);
9064 dump_vtable (t, binfo, vtbl);
9067 /* Add the vtbl initializers for BINFO (and its bases other than
9068 non-virtual primaries) to the list of INITS. BINFO is in the
9069 hierarchy dominated by T. RTTI_BINFO is the binfo within T of
9070 the constructor the vtbl inits should be accumulated for. (If this
9071 is the complete object vtbl then RTTI_BINFO will be TYPE_BINFO (T).)
9072 ORIG_BINFO is the binfo for this object within BINFO_TYPE (RTTI_BINFO).
9073 BINFO is the active base equivalent of ORIG_BINFO in the inheritance
9074 graph of T. Both BINFO and ORIG_BINFO will have the same BINFO_TYPE,
9075 but are not necessarily the same in terms of layout. */
9077 static void
9078 accumulate_vtbl_inits (tree binfo,
9079 tree orig_binfo,
9080 tree rtti_binfo,
9081 tree vtbl,
9082 tree t,
9083 vec<constructor_elt, va_gc> **inits)
9085 int i;
9086 tree base_binfo;
9087 int ctor_vtbl_p = !SAME_BINFO_TYPE_P (BINFO_TYPE (rtti_binfo), t);
9089 gcc_assert (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), BINFO_TYPE (orig_binfo)));
9091 /* If it doesn't have a vptr, we don't do anything. */
9092 if (!TYPE_CONTAINS_VPTR_P (BINFO_TYPE (binfo)))
9093 return;
9095 /* If we're building a construction vtable, we're not interested in
9096 subobjects that don't require construction vtables. */
9097 if (ctor_vtbl_p
9098 && !CLASSTYPE_VBASECLASSES (BINFO_TYPE (binfo))
9099 && !binfo_via_virtual (orig_binfo, BINFO_TYPE (rtti_binfo)))
9100 return;
9102 /* Build the initializers for the BINFO-in-T vtable. */
9103 dfs_accumulate_vtbl_inits (binfo, orig_binfo, rtti_binfo, vtbl, t, inits);
9105 /* Walk the BINFO and its bases. We walk in preorder so that as we
9106 initialize each vtable we can figure out at what offset the
9107 secondary vtable lies from the primary vtable. We can't use
9108 dfs_walk here because we need to iterate through bases of BINFO
9109 and RTTI_BINFO simultaneously. */
9110 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
9112 /* Skip virtual bases. */
9113 if (BINFO_VIRTUAL_P (base_binfo))
9114 continue;
9115 accumulate_vtbl_inits (base_binfo,
9116 BINFO_BASE_BINFO (orig_binfo, i),
9117 rtti_binfo, vtbl, t,
9118 inits);
9122 /* Called from accumulate_vtbl_inits. Adds the initializers for the
9123 BINFO vtable to L. */
9125 static void
9126 dfs_accumulate_vtbl_inits (tree binfo,
9127 tree orig_binfo,
9128 tree rtti_binfo,
9129 tree orig_vtbl,
9130 tree t,
9131 vec<constructor_elt, va_gc> **l)
9133 tree vtbl = NULL_TREE;
9134 int ctor_vtbl_p = !SAME_BINFO_TYPE_P (BINFO_TYPE (rtti_binfo), t);
9135 int n_inits;
9137 if (ctor_vtbl_p
9138 && BINFO_VIRTUAL_P (orig_binfo) && BINFO_PRIMARY_P (orig_binfo))
9140 /* In the hierarchy of BINFO_TYPE (RTTI_BINFO), this is a
9141 primary virtual base. If it is not the same primary in
9142 the hierarchy of T, we'll need to generate a ctor vtable
9143 for it, to place at its location in T. If it is the same
9144 primary, we still need a VTT entry for the vtable, but it
9145 should point to the ctor vtable for the base it is a
9146 primary for within the sub-hierarchy of RTTI_BINFO.
9148 There are three possible cases:
9150 1) We are in the same place.
9151 2) We are a primary base within a lost primary virtual base of
9152 RTTI_BINFO.
9153 3) We are primary to something not a base of RTTI_BINFO. */
9155 tree b;
9156 tree last = NULL_TREE;
9158 /* First, look through the bases we are primary to for RTTI_BINFO
9159 or a virtual base. */
9160 b = binfo;
9161 while (BINFO_PRIMARY_P (b))
9163 b = BINFO_INHERITANCE_CHAIN (b);
9164 last = b;
9165 if (BINFO_VIRTUAL_P (b) || b == rtti_binfo)
9166 goto found;
9168 /* If we run out of primary links, keep looking down our
9169 inheritance chain; we might be an indirect primary. */
9170 for (b = last; b; b = BINFO_INHERITANCE_CHAIN (b))
9171 if (BINFO_VIRTUAL_P (b) || b == rtti_binfo)
9172 break;
9173 found:
9175 /* If we found RTTI_BINFO, this is case 1. If we found a virtual
9176 base B and it is a base of RTTI_BINFO, this is case 2. In
9177 either case, we share our vtable with LAST, i.e. the
9178 derived-most base within B of which we are a primary. */
9179 if (b == rtti_binfo
9180 || (b && binfo_for_vbase (BINFO_TYPE (b), BINFO_TYPE (rtti_binfo))))
9181 /* Just set our BINFO_VTABLE to point to LAST, as we may not have
9182 set LAST's BINFO_VTABLE yet. We'll extract the actual vptr in
9183 binfo_ctor_vtable after everything's been set up. */
9184 vtbl = last;
9186 /* Otherwise, this is case 3 and we get our own. */
9188 else if (!BINFO_NEW_VTABLE_MARKED (orig_binfo))
9189 return;
9191 n_inits = vec_safe_length (*l);
9193 if (!vtbl)
9195 tree index;
9196 int non_fn_entries;
9198 /* Add the initializer for this vtable. */
9199 build_vtbl_initializer (binfo, orig_binfo, t, rtti_binfo,
9200 &non_fn_entries, l);
9202 /* Figure out the position to which the VPTR should point. */
9203 vtbl = build1 (ADDR_EXPR, vtbl_ptr_type_node, orig_vtbl);
9204 index = size_binop (MULT_EXPR,
9205 TYPE_SIZE_UNIT (vtable_entry_type),
9206 size_int (non_fn_entries + n_inits));
9207 vtbl = fold_build_pointer_plus (vtbl, index);
9210 if (ctor_vtbl_p)
9211 /* For a construction vtable, we can't overwrite BINFO_VTABLE.
9212 So, we make a TREE_LIST. Later, dfs_fixup_binfo_vtbls will
9213 straighten this out. */
9214 BINFO_VTABLE (binfo) = tree_cons (rtti_binfo, vtbl, BINFO_VTABLE (binfo));
9215 else if (BINFO_PRIMARY_P (binfo) && BINFO_VIRTUAL_P (binfo))
9216 /* Throw away any unneeded intializers. */
9217 (*l)->truncate (n_inits);
9218 else
9219 /* For an ordinary vtable, set BINFO_VTABLE. */
9220 BINFO_VTABLE (binfo) = vtbl;
9223 static GTY(()) tree abort_fndecl_addr;
9224 static GTY(()) tree dvirt_fn;
9226 /* Construct the initializer for BINFO's virtual function table. BINFO
9227 is part of the hierarchy dominated by T. If we're building a
9228 construction vtable, the ORIG_BINFO is the binfo we should use to
9229 find the actual function pointers to put in the vtable - but they
9230 can be overridden on the path to most-derived in the graph that
9231 ORIG_BINFO belongs. Otherwise,
9232 ORIG_BINFO should be the same as BINFO. The RTTI_BINFO is the
9233 BINFO that should be indicated by the RTTI information in the
9234 vtable; it will be a base class of T, rather than T itself, if we
9235 are building a construction vtable.
9237 The value returned is a TREE_LIST suitable for wrapping in a
9238 CONSTRUCTOR to use as the DECL_INITIAL for a vtable. If
9239 NON_FN_ENTRIES_P is not NULL, *NON_FN_ENTRIES_P is set to the
9240 number of non-function entries in the vtable.
9242 It might seem that this function should never be called with a
9243 BINFO for which BINFO_PRIMARY_P holds, the vtable for such a
9244 base is always subsumed by a derived class vtable. However, when
9245 we are building construction vtables, we do build vtables for
9246 primary bases; we need these while the primary base is being
9247 constructed. */
9249 static void
9250 build_vtbl_initializer (tree binfo,
9251 tree orig_binfo,
9252 tree t,
9253 tree rtti_binfo,
9254 int* non_fn_entries_p,
9255 vec<constructor_elt, va_gc> **inits)
9257 tree v;
9258 vtbl_init_data vid;
9259 unsigned ix, jx;
9260 tree vbinfo;
9261 vec<tree, va_gc> *vbases;
9262 constructor_elt *e;
9264 /* Initialize VID. */
9265 memset (&vid, 0, sizeof (vid));
9266 vid.binfo = binfo;
9267 vid.derived = t;
9268 vid.rtti_binfo = rtti_binfo;
9269 vid.primary_vtbl_p = SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), t);
9270 vid.ctor_vtbl_p = !SAME_BINFO_TYPE_P (BINFO_TYPE (rtti_binfo), t);
9271 vid.generate_vcall_entries = true;
9272 /* The first vbase or vcall offset is at index -3 in the vtable. */
9273 vid.index = ssize_int(-3 * TARGET_VTABLE_DATA_ENTRY_DISTANCE);
9275 /* Add entries to the vtable for RTTI. */
9276 build_rtti_vtbl_entries (binfo, &vid);
9278 /* Create an array for keeping track of the functions we've
9279 processed. When we see multiple functions with the same
9280 signature, we share the vcall offsets. */
9281 vec_alloc (vid.fns, 32);
9282 /* Add the vcall and vbase offset entries. */
9283 build_vcall_and_vbase_vtbl_entries (binfo, &vid);
9285 /* Clear BINFO_VTABLE_PATH_MARKED; it's set by
9286 build_vbase_offset_vtbl_entries. */
9287 for (vbases = CLASSTYPE_VBASECLASSES (t), ix = 0;
9288 vec_safe_iterate (vbases, ix, &vbinfo); ix++)
9289 BINFO_VTABLE_PATH_MARKED (vbinfo) = 0;
9291 /* If the target requires padding between data entries, add that now. */
9292 if (TARGET_VTABLE_DATA_ENTRY_DISTANCE > 1)
9294 int n_entries = vec_safe_length (vid.inits);
9296 vec_safe_grow (vid.inits, TARGET_VTABLE_DATA_ENTRY_DISTANCE * n_entries);
9298 /* Move data entries into their new positions and add padding
9299 after the new positions. Iterate backwards so we don't
9300 overwrite entries that we would need to process later. */
9301 for (ix = n_entries - 1;
9302 vid.inits->iterate (ix, &e);
9303 ix--)
9305 int j;
9306 int new_position = (TARGET_VTABLE_DATA_ENTRY_DISTANCE * ix
9307 + (TARGET_VTABLE_DATA_ENTRY_DISTANCE - 1));
9309 (*vid.inits)[new_position] = *e;
9311 for (j = 1; j < TARGET_VTABLE_DATA_ENTRY_DISTANCE; ++j)
9313 constructor_elt *f = &(*vid.inits)[new_position - j];
9314 f->index = NULL_TREE;
9315 f->value = build1 (NOP_EXPR, vtable_entry_type,
9316 null_pointer_node);
9321 if (non_fn_entries_p)
9322 *non_fn_entries_p = vec_safe_length (vid.inits);
9324 /* The initializers for virtual functions were built up in reverse
9325 order. Straighten them out and add them to the running list in one
9326 step. */
9327 jx = vec_safe_length (*inits);
9328 vec_safe_grow (*inits, jx + vid.inits->length ());
9330 for (ix = vid.inits->length () - 1;
9331 vid.inits->iterate (ix, &e);
9332 ix--, jx++)
9333 (**inits)[jx] = *e;
9335 /* Go through all the ordinary virtual functions, building up
9336 initializers. */
9337 for (v = BINFO_VIRTUALS (orig_binfo); v; v = TREE_CHAIN (v))
9339 tree delta;
9340 tree vcall_index;
9341 tree fn, fn_original;
9342 tree init = NULL_TREE;
9343 tree idx = size_int (jx++);
9345 fn = BV_FN (v);
9346 fn_original = fn;
9347 if (DECL_THUNK_P (fn))
9349 if (!DECL_NAME (fn))
9350 finish_thunk (fn);
9351 if (THUNK_ALIAS (fn))
9353 fn = THUNK_ALIAS (fn);
9354 BV_FN (v) = fn;
9356 fn_original = THUNK_TARGET (fn);
9359 /* If the only definition of this function signature along our
9360 primary base chain is from a lost primary, this vtable slot will
9361 never be used, so just zero it out. This is important to avoid
9362 requiring extra thunks which cannot be generated with the function.
9364 We first check this in update_vtable_entry_for_fn, so we handle
9365 restored primary bases properly; we also need to do it here so we
9366 zero out unused slots in ctor vtables, rather than filling them
9367 with erroneous values (though harmless, apart from relocation
9368 costs). */
9369 if (BV_LOST_PRIMARY (v))
9370 init = size_zero_node;
9372 if (! init)
9374 /* Pull the offset for `this', and the function to call, out of
9375 the list. */
9376 delta = BV_DELTA (v);
9377 vcall_index = BV_VCALL_INDEX (v);
9379 gcc_assert (TREE_CODE (delta) == INTEGER_CST);
9380 gcc_assert (TREE_CODE (fn) == FUNCTION_DECL);
9382 /* You can't call an abstract virtual function; it's abstract.
9383 So, we replace these functions with __pure_virtual. */
9384 if (DECL_PURE_VIRTUAL_P (fn_original))
9386 fn = abort_fndecl;
9387 if (!TARGET_VTABLE_USES_DESCRIPTORS)
9389 if (abort_fndecl_addr == NULL)
9390 abort_fndecl_addr
9391 = fold_convert (vfunc_ptr_type_node,
9392 build_fold_addr_expr (fn));
9393 init = abort_fndecl_addr;
9396 /* Likewise for deleted virtuals. */
9397 else if (DECL_DELETED_FN (fn_original))
9399 if (!dvirt_fn)
9401 tree name = get_identifier ("__cxa_deleted_virtual");
9402 dvirt_fn = get_global_binding (name);
9403 if (!dvirt_fn)
9404 dvirt_fn = push_library_fn
9405 (name,
9406 build_function_type_list (void_type_node, NULL_TREE),
9407 NULL_TREE, ECF_NORETURN | ECF_COLD);
9409 fn = dvirt_fn;
9410 if (!TARGET_VTABLE_USES_DESCRIPTORS)
9411 init = fold_convert (vfunc_ptr_type_node,
9412 build_fold_addr_expr (fn));
9414 else
9416 if (!integer_zerop (delta) || vcall_index)
9418 fn = make_thunk (fn, /*this_adjusting=*/1,
9419 delta, vcall_index);
9420 if (!DECL_NAME (fn))
9421 finish_thunk (fn);
9423 /* Take the address of the function, considering it to be of an
9424 appropriate generic type. */
9425 if (!TARGET_VTABLE_USES_DESCRIPTORS)
9426 init = fold_convert (vfunc_ptr_type_node,
9427 build_fold_addr_expr (fn));
9428 /* Don't refer to a virtual destructor from a constructor
9429 vtable or a vtable for an abstract class, since destroying
9430 an object under construction is undefined behavior and we
9431 don't want it to be considered a candidate for speculative
9432 devirtualization. But do create the thunk for ABI
9433 compliance. */
9434 if (DECL_DESTRUCTOR_P (fn_original)
9435 && (CLASSTYPE_PURE_VIRTUALS (DECL_CONTEXT (fn_original))
9436 || orig_binfo != binfo))
9437 init = size_zero_node;
9441 /* And add it to the chain of initializers. */
9442 if (TARGET_VTABLE_USES_DESCRIPTORS)
9444 int i;
9445 if (init == size_zero_node)
9446 for (i = 0; i < TARGET_VTABLE_USES_DESCRIPTORS; ++i)
9447 CONSTRUCTOR_APPEND_ELT (*inits, idx, init);
9448 else
9449 for (i = 0; i < TARGET_VTABLE_USES_DESCRIPTORS; ++i)
9451 tree fdesc = build2 (FDESC_EXPR, vfunc_ptr_type_node,
9452 fn, build_int_cst (NULL_TREE, i));
9453 TREE_CONSTANT (fdesc) = 1;
9455 CONSTRUCTOR_APPEND_ELT (*inits, idx, fdesc);
9458 else
9459 CONSTRUCTOR_APPEND_ELT (*inits, idx, init);
9463 /* Adds to vid->inits the initializers for the vbase and vcall
9464 offsets in BINFO, which is in the hierarchy dominated by T. */
9466 static void
9467 build_vcall_and_vbase_vtbl_entries (tree binfo, vtbl_init_data* vid)
9469 tree b;
9471 /* If this is a derived class, we must first create entries
9472 corresponding to the primary base class. */
9473 b = get_primary_binfo (binfo);
9474 if (b)
9475 build_vcall_and_vbase_vtbl_entries (b, vid);
9477 /* Add the vbase entries for this base. */
9478 build_vbase_offset_vtbl_entries (binfo, vid);
9479 /* Add the vcall entries for this base. */
9480 build_vcall_offset_vtbl_entries (binfo, vid);
9483 /* Returns the initializers for the vbase offset entries in the vtable
9484 for BINFO (which is part of the class hierarchy dominated by T), in
9485 reverse order. VBASE_OFFSET_INDEX gives the vtable index
9486 where the next vbase offset will go. */
9488 static void
9489 build_vbase_offset_vtbl_entries (tree binfo, vtbl_init_data* vid)
9491 tree vbase;
9492 tree t;
9493 tree non_primary_binfo;
9495 /* If there are no virtual baseclasses, then there is nothing to
9496 do. */
9497 if (!CLASSTYPE_VBASECLASSES (BINFO_TYPE (binfo)))
9498 return;
9500 t = vid->derived;
9502 /* We might be a primary base class. Go up the inheritance hierarchy
9503 until we find the most derived class of which we are a primary base:
9504 it is the offset of that which we need to use. */
9505 non_primary_binfo = binfo;
9506 while (BINFO_INHERITANCE_CHAIN (non_primary_binfo))
9508 tree b;
9510 /* If we have reached a virtual base, then it must be a primary
9511 base (possibly multi-level) of vid->binfo, or we wouldn't
9512 have called build_vcall_and_vbase_vtbl_entries for it. But it
9513 might be a lost primary, so just skip down to vid->binfo. */
9514 if (BINFO_VIRTUAL_P (non_primary_binfo))
9516 non_primary_binfo = vid->binfo;
9517 break;
9520 b = BINFO_INHERITANCE_CHAIN (non_primary_binfo);
9521 if (get_primary_binfo (b) != non_primary_binfo)
9522 break;
9523 non_primary_binfo = b;
9526 /* Go through the virtual bases, adding the offsets. */
9527 for (vbase = TYPE_BINFO (BINFO_TYPE (binfo));
9528 vbase;
9529 vbase = TREE_CHAIN (vbase))
9531 tree b;
9532 tree delta;
9534 if (!BINFO_VIRTUAL_P (vbase))
9535 continue;
9537 /* Find the instance of this virtual base in the complete
9538 object. */
9539 b = copied_binfo (vbase, binfo);
9541 /* If we've already got an offset for this virtual base, we
9542 don't need another one. */
9543 if (BINFO_VTABLE_PATH_MARKED (b))
9544 continue;
9545 BINFO_VTABLE_PATH_MARKED (b) = 1;
9547 /* Figure out where we can find this vbase offset. */
9548 delta = size_binop (MULT_EXPR,
9549 vid->index,
9550 fold_convert (ssizetype,
9551 TYPE_SIZE_UNIT (vtable_entry_type)));
9552 if (vid->primary_vtbl_p)
9553 BINFO_VPTR_FIELD (b) = delta;
9555 if (binfo != TYPE_BINFO (t))
9556 /* The vbase offset had better be the same. */
9557 gcc_assert (tree_int_cst_equal (delta, BINFO_VPTR_FIELD (vbase)));
9559 /* The next vbase will come at a more negative offset. */
9560 vid->index = size_binop (MINUS_EXPR, vid->index,
9561 ssize_int (TARGET_VTABLE_DATA_ENTRY_DISTANCE));
9563 /* The initializer is the delta from BINFO to this virtual base.
9564 The vbase offsets go in reverse inheritance-graph order, and
9565 we are walking in inheritance graph order so these end up in
9566 the right order. */
9567 delta = size_diffop_loc (input_location,
9568 BINFO_OFFSET (b), BINFO_OFFSET (non_primary_binfo));
9570 CONSTRUCTOR_APPEND_ELT (vid->inits, NULL_TREE,
9571 fold_build1_loc (input_location, NOP_EXPR,
9572 vtable_entry_type, delta));
9576 /* Adds the initializers for the vcall offset entries in the vtable
9577 for BINFO (which is part of the class hierarchy dominated by VID->DERIVED)
9578 to VID->INITS. */
9580 static void
9581 build_vcall_offset_vtbl_entries (tree binfo, vtbl_init_data* vid)
9583 /* We only need these entries if this base is a virtual base. We
9584 compute the indices -- but do not add to the vtable -- when
9585 building the main vtable for a class. */
9586 if (binfo == TYPE_BINFO (vid->derived)
9587 || (BINFO_VIRTUAL_P (binfo)
9588 /* If BINFO is RTTI_BINFO, then (since BINFO does not
9589 correspond to VID->DERIVED), we are building a primary
9590 construction virtual table. Since this is a primary
9591 virtual table, we do not need the vcall offsets for
9592 BINFO. */
9593 && binfo != vid->rtti_binfo))
9595 /* We need a vcall offset for each of the virtual functions in this
9596 vtable. For example:
9598 class A { virtual void f (); };
9599 class B1 : virtual public A { virtual void f (); };
9600 class B2 : virtual public A { virtual void f (); };
9601 class C: public B1, public B2 { virtual void f (); };
9603 A C object has a primary base of B1, which has a primary base of A. A
9604 C also has a secondary base of B2, which no longer has a primary base
9605 of A. So the B2-in-C construction vtable needs a secondary vtable for
9606 A, which will adjust the A* to a B2* to call f. We have no way of
9607 knowing what (or even whether) this offset will be when we define B2,
9608 so we store this "vcall offset" in the A sub-vtable and look it up in
9609 a "virtual thunk" for B2::f.
9611 We need entries for all the functions in our primary vtable and
9612 in our non-virtual bases' secondary vtables. */
9613 vid->vbase = binfo;
9614 /* If we are just computing the vcall indices -- but do not need
9615 the actual entries -- not that. */
9616 if (!BINFO_VIRTUAL_P (binfo))
9617 vid->generate_vcall_entries = false;
9618 /* Now, walk through the non-virtual bases, adding vcall offsets. */
9619 add_vcall_offset_vtbl_entries_r (binfo, vid);
9623 /* Build vcall offsets, starting with those for BINFO. */
9625 static void
9626 add_vcall_offset_vtbl_entries_r (tree binfo, vtbl_init_data* vid)
9628 int i;
9629 tree primary_binfo;
9630 tree base_binfo;
9632 /* Don't walk into virtual bases -- except, of course, for the
9633 virtual base for which we are building vcall offsets. Any
9634 primary virtual base will have already had its offsets generated
9635 through the recursion in build_vcall_and_vbase_vtbl_entries. */
9636 if (BINFO_VIRTUAL_P (binfo) && vid->vbase != binfo)
9637 return;
9639 /* If BINFO has a primary base, process it first. */
9640 primary_binfo = get_primary_binfo (binfo);
9641 if (primary_binfo)
9642 add_vcall_offset_vtbl_entries_r (primary_binfo, vid);
9644 /* Add BINFO itself to the list. */
9645 add_vcall_offset_vtbl_entries_1 (binfo, vid);
9647 /* Scan the non-primary bases of BINFO. */
9648 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
9649 if (base_binfo != primary_binfo)
9650 add_vcall_offset_vtbl_entries_r (base_binfo, vid);
9653 /* Called from build_vcall_offset_vtbl_entries_r. */
9655 static void
9656 add_vcall_offset_vtbl_entries_1 (tree binfo, vtbl_init_data* vid)
9658 /* Make entries for the rest of the virtuals. */
9659 tree orig_fn;
9661 /* The ABI requires that the methods be processed in declaration
9662 order. */
9663 for (orig_fn = TYPE_FIELDS (BINFO_TYPE (binfo));
9664 orig_fn;
9665 orig_fn = DECL_CHAIN (orig_fn))
9666 if (TREE_CODE (orig_fn) == FUNCTION_DECL && DECL_VINDEX (orig_fn))
9667 add_vcall_offset (orig_fn, binfo, vid);
9670 /* Add a vcall offset entry for ORIG_FN to the vtable. */
9672 static void
9673 add_vcall_offset (tree orig_fn, tree binfo, vtbl_init_data *vid)
9675 size_t i;
9676 tree vcall_offset;
9677 tree derived_entry;
9679 /* If there is already an entry for a function with the same
9680 signature as FN, then we do not need a second vcall offset.
9681 Check the list of functions already present in the derived
9682 class vtable. */
9683 FOR_EACH_VEC_SAFE_ELT (vid->fns, i, derived_entry)
9685 if (same_signature_p (derived_entry, orig_fn)
9686 /* We only use one vcall offset for virtual destructors,
9687 even though there are two virtual table entries. */
9688 || (DECL_DESTRUCTOR_P (derived_entry)
9689 && DECL_DESTRUCTOR_P (orig_fn)))
9690 return;
9693 /* If we are building these vcall offsets as part of building
9694 the vtable for the most derived class, remember the vcall
9695 offset. */
9696 if (vid->binfo == TYPE_BINFO (vid->derived))
9698 tree_pair_s elt = {orig_fn, vid->index};
9699 vec_safe_push (CLASSTYPE_VCALL_INDICES (vid->derived), elt);
9702 /* The next vcall offset will be found at a more negative
9703 offset. */
9704 vid->index = size_binop (MINUS_EXPR, vid->index,
9705 ssize_int (TARGET_VTABLE_DATA_ENTRY_DISTANCE));
9707 /* Keep track of this function. */
9708 vec_safe_push (vid->fns, orig_fn);
9710 if (vid->generate_vcall_entries)
9712 tree base;
9713 tree fn;
9715 /* Find the overriding function. */
9716 fn = find_final_overrider (vid->rtti_binfo, binfo, orig_fn);
9717 if (fn == error_mark_node)
9718 vcall_offset = build_zero_cst (vtable_entry_type);
9719 else
9721 base = TREE_VALUE (fn);
9723 /* The vbase we're working on is a primary base of
9724 vid->binfo. But it might be a lost primary, so its
9725 BINFO_OFFSET might be wrong, so we just use the
9726 BINFO_OFFSET from vid->binfo. */
9727 vcall_offset = size_diffop_loc (input_location,
9728 BINFO_OFFSET (base),
9729 BINFO_OFFSET (vid->binfo));
9730 vcall_offset = fold_build1_loc (input_location,
9731 NOP_EXPR, vtable_entry_type,
9732 vcall_offset);
9734 /* Add the initializer to the vtable. */
9735 CONSTRUCTOR_APPEND_ELT (vid->inits, NULL_TREE, vcall_offset);
9739 /* Return vtbl initializers for the RTTI entries corresponding to the
9740 BINFO's vtable. The RTTI entries should indicate the object given
9741 by VID->rtti_binfo. */
9743 static void
9744 build_rtti_vtbl_entries (tree binfo, vtbl_init_data* vid)
9746 tree b;
9747 tree t;
9748 tree offset;
9749 tree decl;
9750 tree init;
9752 t = BINFO_TYPE (vid->rtti_binfo);
9754 /* To find the complete object, we will first convert to our most
9755 primary base, and then add the offset in the vtbl to that value. */
9756 b = most_primary_binfo (binfo);
9757 offset = size_diffop_loc (input_location,
9758 BINFO_OFFSET (vid->rtti_binfo), BINFO_OFFSET (b));
9760 /* The second entry is the address of the typeinfo object. */
9761 if (flag_rtti)
9762 decl = build_address (get_tinfo_decl (t));
9763 else
9764 decl = integer_zero_node;
9766 /* Convert the declaration to a type that can be stored in the
9767 vtable. */
9768 init = build_nop (vfunc_ptr_type_node, decl);
9769 CONSTRUCTOR_APPEND_ELT (vid->inits, NULL_TREE, init);
9771 /* Add the offset-to-top entry. It comes earlier in the vtable than
9772 the typeinfo entry. Convert the offset to look like a
9773 function pointer, so that we can put it in the vtable. */
9774 init = build_nop (vfunc_ptr_type_node, offset);
9775 CONSTRUCTOR_APPEND_ELT (vid->inits, NULL_TREE, init);
9778 /* TRUE iff TYPE is uniquely derived from PARENT. Ignores
9779 accessibility. */
9781 bool
9782 uniquely_derived_from_p (tree parent, tree type)
9784 tree base = lookup_base (type, parent, ba_unique, NULL, tf_none);
9785 return base && base != error_mark_node;
9788 /* TRUE iff TYPE is publicly & uniquely derived from PARENT. */
9790 bool
9791 publicly_uniquely_derived_p (tree parent, tree type)
9793 tree base = lookup_base (type, parent, ba_ignore_scope | ba_check,
9794 NULL, tf_none);
9795 return base && base != error_mark_node;
9798 /* CTX1 and CTX2 are declaration contexts. Return the innermost common
9799 class between them, if any. */
9801 tree
9802 common_enclosing_class (tree ctx1, tree ctx2)
9804 if (!TYPE_P (ctx1) || !TYPE_P (ctx2))
9805 return NULL_TREE;
9806 gcc_assert (ctx1 == TYPE_MAIN_VARIANT (ctx1)
9807 && ctx2 == TYPE_MAIN_VARIANT (ctx2));
9808 if (ctx1 == ctx2)
9809 return ctx1;
9810 for (tree t = ctx1; TYPE_P (t); t = TYPE_CONTEXT (t))
9811 TYPE_MARKED_P (t) = true;
9812 tree found = NULL_TREE;
9813 for (tree t = ctx2; TYPE_P (t); t = TYPE_CONTEXT (t))
9814 if (TYPE_MARKED_P (t))
9816 found = t;
9817 break;
9819 for (tree t = ctx1; TYPE_P (t); t = TYPE_CONTEXT (t))
9820 TYPE_MARKED_P (t) = false;
9821 return found;
9824 #include "gt-cp-class.h"