* cfghooks.c (verify_flow_info): Disable check that all probabilities
[official-gcc.git] / gcc / cp / class.c
bloba90b85f2a5c731c7ade73223edd4f9269cc1d06e
1 /* Functions related to building classes and their related objects.
2 Copyright (C) 1987-2017 Free Software Foundation, Inc.
3 Contributed by Michael Tiemann (tiemann@cygnus.com)
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
10 any later version.
12 GCC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
22 /* High-level class interface. */
24 #include "config.h"
25 #include "system.h"
26 #include "coretypes.h"
27 #include "target.h"
28 #include "cp-tree.h"
29 #include "stringpool.h"
30 #include "cgraph.h"
31 #include "stor-layout.h"
32 #include "attribs.h"
33 #include "flags.h"
34 #include "toplev.h"
35 #include "convert.h"
36 #include "dumpfile.h"
37 #include "gimplify.h"
38 #include "intl.h"
39 #include "asan.h"
41 /* Id for dumping the class hierarchy. */
42 int class_dump_id;
44 /* The number of nested classes being processed. If we are not in the
45 scope of any class, this is zero. */
47 int current_class_depth;
49 /* In order to deal with nested classes, we keep a stack of classes.
50 The topmost entry is the innermost class, and is the entry at index
51 CURRENT_CLASS_DEPTH */
53 typedef struct class_stack_node {
54 /* The name of the class. */
55 tree name;
57 /* The _TYPE node for the class. */
58 tree type;
60 /* The access specifier pending for new declarations in the scope of
61 this class. */
62 tree access;
64 /* If were defining TYPE, the names used in this class. */
65 splay_tree names_used;
67 /* Nonzero if this class is no longer open, because of a call to
68 push_to_top_level. */
69 size_t hidden;
70 }* class_stack_node_t;
72 struct vtbl_init_data
74 /* The base for which we're building initializers. */
75 tree binfo;
76 /* The type of the most-derived type. */
77 tree derived;
78 /* The binfo for the dynamic type. This will be TYPE_BINFO (derived),
79 unless ctor_vtbl_p is true. */
80 tree rtti_binfo;
81 /* The negative-index vtable initializers built up so far. These
82 are in order from least negative index to most negative index. */
83 vec<constructor_elt, va_gc> *inits;
84 /* The binfo for the virtual base for which we're building
85 vcall offset initializers. */
86 tree vbase;
87 /* The functions in vbase for which we have already provided vcall
88 offsets. */
89 vec<tree, va_gc> *fns;
90 /* The vtable index of the next vcall or vbase offset. */
91 tree index;
92 /* Nonzero if we are building the initializer for the primary
93 vtable. */
94 int primary_vtbl_p;
95 /* Nonzero if we are building the initializer for a construction
96 vtable. */
97 int ctor_vtbl_p;
98 /* True when adding vcall offset entries to the vtable. False when
99 merely computing the indices. */
100 bool generate_vcall_entries;
103 /* The type of a function passed to walk_subobject_offsets. */
104 typedef int (*subobject_offset_fn) (tree, tree, splay_tree);
106 /* The stack itself. This is a dynamically resized array. The
107 number of elements allocated is CURRENT_CLASS_STACK_SIZE. */
108 static int current_class_stack_size;
109 static class_stack_node_t current_class_stack;
111 /* The size of the largest empty class seen in this translation unit. */
112 static GTY (()) tree sizeof_biggest_empty_class;
114 /* An array of all local classes present in this translation unit, in
115 declaration order. */
116 vec<tree, va_gc> *local_classes;
118 static tree get_vfield_name (tree);
119 static void finish_struct_anon (tree);
120 static tree get_vtable_name (tree);
121 static void get_basefndecls (tree, tree, vec<tree> *);
122 static int build_primary_vtable (tree, tree);
123 static int build_secondary_vtable (tree);
124 static void finish_vtbls (tree);
125 static void modify_vtable_entry (tree, tree, tree, tree, tree *);
126 static void finish_struct_bits (tree);
127 static int alter_access (tree, tree, tree);
128 static void handle_using_decl (tree, tree);
129 static tree dfs_modify_vtables (tree, void *);
130 static tree modify_all_vtables (tree, tree);
131 static void determine_primary_bases (tree);
132 static void maybe_warn_about_overly_private_class (tree);
133 static void add_implicitly_declared_members (tree, tree*, int, int);
134 static tree fixed_type_or_null (tree, int *, int *);
135 static tree build_simple_base_path (tree expr, tree binfo);
136 static tree build_vtbl_ref_1 (tree, tree);
137 static void build_vtbl_initializer (tree, tree, tree, tree, int *,
138 vec<constructor_elt, va_gc> **);
139 static bool check_bitfield_decl (tree);
140 static bool check_field_decl (tree, tree, int *, int *);
141 static void check_field_decls (tree, tree *, int *, int *);
142 static tree *build_base_field (record_layout_info, tree, splay_tree, tree *);
143 static void build_base_fields (record_layout_info, splay_tree, tree *);
144 static void check_methods (tree);
145 static void remove_zero_width_bit_fields (tree);
146 static bool accessible_nvdtor_p (tree);
148 /* Used by find_flexarrays and related functions. */
149 struct flexmems_t;
150 static void diagnose_flexarrays (tree, const flexmems_t *);
151 static void find_flexarrays (tree, flexmems_t *, bool = false,
152 tree = NULL_TREE, tree = NULL_TREE);
153 static void check_flexarrays (tree, flexmems_t * = NULL, bool = false);
154 static void check_bases (tree, int *, int *);
155 static void check_bases_and_members (tree);
156 static tree create_vtable_ptr (tree, tree *);
157 static void include_empty_classes (record_layout_info);
158 static void layout_class_type (tree, tree *);
159 static void propagate_binfo_offsets (tree, tree);
160 static void layout_virtual_bases (record_layout_info, splay_tree);
161 static void build_vbase_offset_vtbl_entries (tree, vtbl_init_data *);
162 static void add_vcall_offset_vtbl_entries_r (tree, vtbl_init_data *);
163 static void add_vcall_offset_vtbl_entries_1 (tree, vtbl_init_data *);
164 static void build_vcall_offset_vtbl_entries (tree, vtbl_init_data *);
165 static void add_vcall_offset (tree, tree, vtbl_init_data *);
166 static void layout_vtable_decl (tree, int);
167 static tree dfs_find_final_overrider_pre (tree, void *);
168 static tree dfs_find_final_overrider_post (tree, void *);
169 static tree find_final_overrider (tree, tree, tree);
170 static int make_new_vtable (tree, tree);
171 static tree get_primary_binfo (tree);
172 static int maybe_indent_hierarchy (FILE *, int, int);
173 static tree dump_class_hierarchy_r (FILE *, dump_flags_t, tree, tree, int);
174 static void dump_class_hierarchy (tree);
175 static void dump_class_hierarchy_1 (FILE *, dump_flags_t, tree);
176 static void dump_array (FILE *, tree);
177 static void dump_vtable (tree, tree, tree);
178 static void dump_vtt (tree, tree);
179 static void dump_thunk (FILE *, int, tree);
180 static tree build_vtable (tree, tree, tree);
181 static void initialize_vtable (tree, vec<constructor_elt, va_gc> *);
182 static void layout_nonempty_base_or_field (record_layout_info,
183 tree, tree, splay_tree);
184 static tree end_of_class (tree, int);
185 static bool layout_empty_base (record_layout_info, tree, tree, splay_tree);
186 static void accumulate_vtbl_inits (tree, tree, tree, tree, tree,
187 vec<constructor_elt, va_gc> **);
188 static void dfs_accumulate_vtbl_inits (tree, tree, tree, tree, tree,
189 vec<constructor_elt, va_gc> **);
190 static void build_rtti_vtbl_entries (tree, vtbl_init_data *);
191 static void build_vcall_and_vbase_vtbl_entries (tree, vtbl_init_data *);
192 static void clone_constructors_and_destructors (tree);
193 static tree build_clone (tree, tree);
194 static void update_vtable_entry_for_fn (tree, tree, tree, tree *, unsigned);
195 static void build_ctor_vtbl_group (tree, tree);
196 static void build_vtt (tree);
197 static tree binfo_ctor_vtable (tree);
198 static void build_vtt_inits (tree, tree, vec<constructor_elt, va_gc> **,
199 tree *);
200 static tree dfs_build_secondary_vptr_vtt_inits (tree, void *);
201 static tree dfs_fixup_binfo_vtbls (tree, void *);
202 static int record_subobject_offset (tree, tree, splay_tree);
203 static int check_subobject_offset (tree, tree, splay_tree);
204 static int walk_subobject_offsets (tree, subobject_offset_fn,
205 tree, splay_tree, tree, int);
206 static void record_subobject_offsets (tree, tree, splay_tree, bool);
207 static int layout_conflict_p (tree, tree, splay_tree, int);
208 static int splay_tree_compare_integer_csts (splay_tree_key k1,
209 splay_tree_key k2);
210 static void warn_about_ambiguous_bases (tree);
211 static bool type_requires_array_cookie (tree);
212 static bool base_derived_from (tree, tree);
213 static int empty_base_at_nonzero_offset_p (tree, tree, splay_tree);
214 static tree end_of_base (tree);
215 static tree get_vcall_index (tree, tree);
216 static bool type_maybe_constexpr_default_constructor (tree);
218 /* Variables shared between class.c and call.c. */
220 int n_vtables = 0;
221 int n_vtable_entries = 0;
222 int n_vtable_searches = 0;
223 int n_vtable_elems = 0;
224 int n_convert_harshness = 0;
225 int n_compute_conversion_costs = 0;
226 int n_inner_fields_searched = 0;
228 /* Return a COND_EXPR that executes TRUE_STMT if this execution of the
229 'structor is in charge of 'structing virtual bases, or FALSE_STMT
230 otherwise. */
232 tree
233 build_if_in_charge (tree true_stmt, tree false_stmt)
235 gcc_assert (DECL_HAS_IN_CHARGE_PARM_P (current_function_decl));
236 tree cmp = build2 (NE_EXPR, boolean_type_node,
237 current_in_charge_parm, integer_zero_node);
238 tree type = unlowered_expr_type (true_stmt);
239 if (VOID_TYPE_P (type))
240 type = unlowered_expr_type (false_stmt);
241 tree cond = build3 (COND_EXPR, type,
242 cmp, true_stmt, false_stmt);
243 return cond;
246 /* Convert to or from a base subobject. EXPR is an expression of type
247 `A' or `A*', an expression of type `B' or `B*' is returned. To
248 convert A to a base B, CODE is PLUS_EXPR and BINFO is the binfo for
249 the B base instance within A. To convert base A to derived B, CODE
250 is MINUS_EXPR and BINFO is the binfo for the A instance within B.
251 In this latter case, A must not be a morally virtual base of B.
252 NONNULL is true if EXPR is known to be non-NULL (this is only
253 needed when EXPR is of pointer type). CV qualifiers are preserved
254 from EXPR. */
256 tree
257 build_base_path (enum tree_code code,
258 tree expr,
259 tree binfo,
260 int nonnull,
261 tsubst_flags_t complain)
263 tree v_binfo = NULL_TREE;
264 tree d_binfo = NULL_TREE;
265 tree probe;
266 tree offset;
267 tree target_type;
268 tree null_test = NULL;
269 tree ptr_target_type;
270 int fixed_type_p;
271 int want_pointer = TYPE_PTR_P (TREE_TYPE (expr));
272 bool has_empty = false;
273 bool virtual_access;
274 bool rvalue = false;
276 if (expr == error_mark_node || binfo == error_mark_node || !binfo)
277 return error_mark_node;
279 for (probe = binfo; probe; probe = BINFO_INHERITANCE_CHAIN (probe))
281 d_binfo = probe;
282 if (is_empty_class (BINFO_TYPE (probe)))
283 has_empty = true;
284 if (!v_binfo && BINFO_VIRTUAL_P (probe))
285 v_binfo = probe;
288 probe = TYPE_MAIN_VARIANT (TREE_TYPE (expr));
289 if (want_pointer)
290 probe = TYPE_MAIN_VARIANT (TREE_TYPE (probe));
292 if (code == PLUS_EXPR
293 && !SAME_BINFO_TYPE_P (BINFO_TYPE (d_binfo), probe))
295 /* This can happen when adjust_result_of_qualified_name_lookup can't
296 find a unique base binfo in a call to a member function. We
297 couldn't give the diagnostic then since we might have been calling
298 a static member function, so we do it now. In other cases, eg.
299 during error recovery (c++/71979), we may not have a base at all. */
300 if (complain & tf_error)
302 tree base = lookup_base (probe, BINFO_TYPE (d_binfo),
303 ba_unique, NULL, complain);
304 gcc_assert (base == error_mark_node || !base);
306 return error_mark_node;
309 gcc_assert ((code == MINUS_EXPR
310 && SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), probe))
311 || code == PLUS_EXPR);
313 if (binfo == d_binfo)
314 /* Nothing to do. */
315 return expr;
317 if (code == MINUS_EXPR && v_binfo)
319 if (complain & tf_error)
321 if (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), BINFO_TYPE (v_binfo)))
323 if (want_pointer)
324 error ("cannot convert from pointer to base class %qT to "
325 "pointer to derived class %qT because the base is "
326 "virtual", BINFO_TYPE (binfo), BINFO_TYPE (d_binfo));
327 else
328 error ("cannot convert from base class %qT to derived "
329 "class %qT because the base is virtual",
330 BINFO_TYPE (binfo), BINFO_TYPE (d_binfo));
332 else
334 if (want_pointer)
335 error ("cannot convert from pointer to base class %qT to "
336 "pointer to derived class %qT via virtual base %qT",
337 BINFO_TYPE (binfo), BINFO_TYPE (d_binfo),
338 BINFO_TYPE (v_binfo));
339 else
340 error ("cannot convert from base class %qT to derived "
341 "class %qT via virtual base %qT", BINFO_TYPE (binfo),
342 BINFO_TYPE (d_binfo), BINFO_TYPE (v_binfo));
345 return error_mark_node;
348 if (!want_pointer)
350 rvalue = !lvalue_p (expr);
351 /* This must happen before the call to save_expr. */
352 expr = cp_build_addr_expr (expr, complain);
354 else
355 expr = mark_rvalue_use (expr);
357 offset = BINFO_OFFSET (binfo);
358 fixed_type_p = resolves_to_fixed_type_p (expr, &nonnull);
359 target_type = code == PLUS_EXPR ? BINFO_TYPE (binfo) : BINFO_TYPE (d_binfo);
360 /* TARGET_TYPE has been extracted from BINFO, and, is therefore always
361 cv-unqualified. Extract the cv-qualifiers from EXPR so that the
362 expression returned matches the input. */
363 target_type = cp_build_qualified_type
364 (target_type, cp_type_quals (TREE_TYPE (TREE_TYPE (expr))));
365 ptr_target_type = build_pointer_type (target_type);
367 /* Do we need to look in the vtable for the real offset? */
368 virtual_access = (v_binfo && fixed_type_p <= 0);
370 /* Don't bother with the calculations inside sizeof; they'll ICE if the
371 source type is incomplete and the pointer value doesn't matter. In a
372 template (even in instantiate_non_dependent_expr), we don't have vtables
373 set up properly yet, and the value doesn't matter there either; we're
374 just interested in the result of overload resolution. */
375 if (cp_unevaluated_operand != 0
376 || processing_template_decl
377 || in_template_function ())
379 expr = build_nop (ptr_target_type, expr);
380 goto indout;
383 /* If we're in an NSDMI, we don't have the full constructor context yet
384 that we need for converting to a virtual base, so just build a stub
385 CONVERT_EXPR and expand it later in bot_replace. */
386 if (virtual_access && fixed_type_p < 0
387 && current_scope () != current_function_decl)
389 expr = build1 (CONVERT_EXPR, ptr_target_type, expr);
390 CONVERT_EXPR_VBASE_PATH (expr) = true;
391 goto indout;
394 /* Do we need to check for a null pointer? */
395 if (want_pointer && !nonnull)
397 /* If we know the conversion will not actually change the value
398 of EXPR, then we can avoid testing the expression for NULL.
399 We have to avoid generating a COMPONENT_REF for a base class
400 field, because other parts of the compiler know that such
401 expressions are always non-NULL. */
402 if (!virtual_access && integer_zerop (offset))
403 return build_nop (ptr_target_type, expr);
404 null_test = error_mark_node;
407 /* Protect against multiple evaluation if necessary. */
408 if (TREE_SIDE_EFFECTS (expr) && (null_test || virtual_access))
409 expr = save_expr (expr);
411 /* Now that we've saved expr, build the real null test. */
412 if (null_test)
414 tree zero = cp_convert (TREE_TYPE (expr), nullptr_node, complain);
415 null_test = build2_loc (input_location, NE_EXPR, boolean_type_node,
416 expr, zero);
417 /* This is a compiler generated comparison, don't emit
418 e.g. -Wnonnull-compare warning for it. */
419 TREE_NO_WARNING (null_test) = 1;
422 /* If this is a simple base reference, express it as a COMPONENT_REF. */
423 if (code == PLUS_EXPR && !virtual_access
424 /* We don't build base fields for empty bases, and they aren't very
425 interesting to the optimizers anyway. */
426 && !has_empty)
428 expr = cp_build_indirect_ref (expr, RO_NULL, complain);
429 expr = build_simple_base_path (expr, binfo);
430 if (rvalue)
431 expr = move (expr);
432 if (want_pointer)
433 expr = build_address (expr);
434 target_type = TREE_TYPE (expr);
435 goto out;
438 if (virtual_access)
440 /* Going via virtual base V_BINFO. We need the static offset
441 from V_BINFO to BINFO, and the dynamic offset from D_BINFO to
442 V_BINFO. That offset is an entry in D_BINFO's vtable. */
443 tree v_offset;
445 if (fixed_type_p < 0 && in_base_initializer)
447 /* In a base member initializer, we cannot rely on the
448 vtable being set up. We have to indirect via the
449 vtt_parm. */
450 tree t;
452 t = TREE_TYPE (TYPE_VFIELD (current_class_type));
453 t = build_pointer_type (t);
454 v_offset = fold_convert (t, current_vtt_parm);
455 v_offset = cp_build_indirect_ref (v_offset, RO_NULL, complain);
457 else
459 tree t = expr;
460 if (sanitize_flags_p (SANITIZE_VPTR)
461 && fixed_type_p == 0)
463 t = cp_ubsan_maybe_instrument_cast_to_vbase (input_location,
464 probe, expr);
465 if (t == NULL_TREE)
466 t = expr;
468 v_offset = build_vfield_ref (cp_build_indirect_ref (t, RO_NULL,
469 complain),
470 TREE_TYPE (TREE_TYPE (expr)));
473 if (v_offset == error_mark_node)
474 return error_mark_node;
476 v_offset = fold_build_pointer_plus (v_offset, BINFO_VPTR_FIELD (v_binfo));
477 v_offset = build1 (NOP_EXPR,
478 build_pointer_type (ptrdiff_type_node),
479 v_offset);
480 v_offset = cp_build_indirect_ref (v_offset, RO_NULL, complain);
481 TREE_CONSTANT (v_offset) = 1;
483 offset = convert_to_integer (ptrdiff_type_node,
484 size_diffop_loc (input_location, offset,
485 BINFO_OFFSET (v_binfo)));
487 if (!integer_zerop (offset))
488 v_offset = build2 (code, ptrdiff_type_node, v_offset, offset);
490 if (fixed_type_p < 0)
491 /* Negative fixed_type_p means this is a constructor or destructor;
492 virtual base layout is fixed in in-charge [cd]tors, but not in
493 base [cd]tors. */
494 offset = build_if_in_charge
495 (convert_to_integer (ptrdiff_type_node, BINFO_OFFSET (binfo)),
496 v_offset);
497 else
498 offset = v_offset;
501 if (want_pointer)
502 target_type = ptr_target_type;
504 expr = build1 (NOP_EXPR, ptr_target_type, expr);
506 if (!integer_zerop (offset))
508 offset = fold_convert (sizetype, offset);
509 if (code == MINUS_EXPR)
510 offset = fold_build1_loc (input_location, NEGATE_EXPR, sizetype, offset);
511 expr = fold_build_pointer_plus (expr, offset);
513 else
514 null_test = NULL;
516 indout:
517 if (!want_pointer)
519 expr = cp_build_indirect_ref (expr, RO_NULL, complain);
520 if (rvalue)
521 expr = move (expr);
524 out:
525 if (null_test)
526 expr = fold_build3_loc (input_location, COND_EXPR, target_type, null_test, expr,
527 build_zero_cst (target_type));
529 return expr;
532 /* Subroutine of build_base_path; EXPR and BINFO are as in that function.
533 Perform a derived-to-base conversion by recursively building up a
534 sequence of COMPONENT_REFs to the appropriate base fields. */
536 static tree
537 build_simple_base_path (tree expr, tree binfo)
539 tree type = BINFO_TYPE (binfo);
540 tree d_binfo = BINFO_INHERITANCE_CHAIN (binfo);
541 tree field;
543 if (d_binfo == NULL_TREE)
545 tree temp;
547 gcc_assert (TYPE_MAIN_VARIANT (TREE_TYPE (expr)) == type);
549 /* Transform `(a, b).x' into `(*(a, &b)).x', `(a ? b : c).x'
550 into `(*(a ? &b : &c)).x', and so on. A COND_EXPR is only
551 an lvalue in the front end; only _DECLs and _REFs are lvalues
552 in the back end. */
553 temp = unary_complex_lvalue (ADDR_EXPR, expr);
554 if (temp)
555 expr = cp_build_indirect_ref (temp, RO_NULL, tf_warning_or_error);
557 return expr;
560 /* Recurse. */
561 expr = build_simple_base_path (expr, d_binfo);
563 for (field = TYPE_FIELDS (BINFO_TYPE (d_binfo));
564 field; field = DECL_CHAIN (field))
565 /* Is this the base field created by build_base_field? */
566 if (TREE_CODE (field) == FIELD_DECL
567 && DECL_FIELD_IS_BASE (field)
568 && TREE_TYPE (field) == type
569 /* If we're looking for a field in the most-derived class,
570 also check the field offset; we can have two base fields
571 of the same type if one is an indirect virtual base and one
572 is a direct non-virtual base. */
573 && (BINFO_INHERITANCE_CHAIN (d_binfo)
574 || tree_int_cst_equal (byte_position (field),
575 BINFO_OFFSET (binfo))))
577 /* We don't use build_class_member_access_expr here, as that
578 has unnecessary checks, and more importantly results in
579 recursive calls to dfs_walk_once. */
580 int type_quals = cp_type_quals (TREE_TYPE (expr));
582 expr = build3 (COMPONENT_REF,
583 cp_build_qualified_type (type, type_quals),
584 expr, field, NULL_TREE);
585 /* Mark the expression const or volatile, as appropriate.
586 Even though we've dealt with the type above, we still have
587 to mark the expression itself. */
588 if (type_quals & TYPE_QUAL_CONST)
589 TREE_READONLY (expr) = 1;
590 if (type_quals & TYPE_QUAL_VOLATILE)
591 TREE_THIS_VOLATILE (expr) = 1;
593 return expr;
596 /* Didn't find the base field?!? */
597 gcc_unreachable ();
600 /* Convert OBJECT to the base TYPE. OBJECT is an expression whose
601 type is a class type or a pointer to a class type. In the former
602 case, TYPE is also a class type; in the latter it is another
603 pointer type. If CHECK_ACCESS is true, an error message is emitted
604 if TYPE is inaccessible. If OBJECT has pointer type, the value is
605 assumed to be non-NULL. */
607 tree
608 convert_to_base (tree object, tree type, bool check_access, bool nonnull,
609 tsubst_flags_t complain)
611 tree binfo;
612 tree object_type;
614 if (TYPE_PTR_P (TREE_TYPE (object)))
616 object_type = TREE_TYPE (TREE_TYPE (object));
617 type = TREE_TYPE (type);
619 else
620 object_type = TREE_TYPE (object);
622 binfo = lookup_base (object_type, type, check_access ? ba_check : ba_unique,
623 NULL, complain);
624 if (!binfo || binfo == error_mark_node)
625 return error_mark_node;
627 return build_base_path (PLUS_EXPR, object, binfo, nonnull, complain);
630 /* EXPR is an expression with unqualified class type. BASE is a base
631 binfo of that class type. Returns EXPR, converted to the BASE
632 type. This function assumes that EXPR is the most derived class;
633 therefore virtual bases can be found at their static offsets. */
635 tree
636 convert_to_base_statically (tree expr, tree base)
638 tree expr_type;
640 expr_type = TREE_TYPE (expr);
641 if (!SAME_BINFO_TYPE_P (BINFO_TYPE (base), expr_type))
643 /* If this is a non-empty base, use a COMPONENT_REF. */
644 if (!is_empty_class (BINFO_TYPE (base)))
645 return build_simple_base_path (expr, base);
647 /* We use fold_build2 and fold_convert below to simplify the trees
648 provided to the optimizers. It is not safe to call these functions
649 when processing a template because they do not handle C++-specific
650 trees. */
651 gcc_assert (!processing_template_decl);
652 expr = cp_build_addr_expr (expr, tf_warning_or_error);
653 if (!integer_zerop (BINFO_OFFSET (base)))
654 expr = fold_build_pointer_plus_loc (input_location,
655 expr, BINFO_OFFSET (base));
656 expr = fold_convert (build_pointer_type (BINFO_TYPE (base)), expr);
657 expr = build_fold_indirect_ref_loc (input_location, expr);
660 return expr;
664 tree
665 build_vfield_ref (tree datum, tree type)
667 tree vfield, vcontext;
669 if (datum == error_mark_node
670 /* Can happen in case of duplicate base types (c++/59082). */
671 || !TYPE_VFIELD (type))
672 return error_mark_node;
674 /* First, convert to the requested type. */
675 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (datum), type))
676 datum = convert_to_base (datum, type, /*check_access=*/false,
677 /*nonnull=*/true, tf_warning_or_error);
679 /* Second, the requested type may not be the owner of its own vptr.
680 If not, convert to the base class that owns it. We cannot use
681 convert_to_base here, because VCONTEXT may appear more than once
682 in the inheritance hierarchy of TYPE, and thus direct conversion
683 between the types may be ambiguous. Following the path back up
684 one step at a time via primary bases avoids the problem. */
685 vfield = TYPE_VFIELD (type);
686 vcontext = DECL_CONTEXT (vfield);
687 while (!same_type_ignoring_top_level_qualifiers_p (vcontext, type))
689 datum = build_simple_base_path (datum, CLASSTYPE_PRIMARY_BINFO (type));
690 type = TREE_TYPE (datum);
693 return build3 (COMPONENT_REF, TREE_TYPE (vfield), datum, vfield, NULL_TREE);
696 /* Given an object INSTANCE, return an expression which yields the
697 vtable element corresponding to INDEX. There are many special
698 cases for INSTANCE which we take care of here, mainly to avoid
699 creating extra tree nodes when we don't have to. */
701 static tree
702 build_vtbl_ref_1 (tree instance, tree idx)
704 tree aref;
705 tree vtbl = NULL_TREE;
707 /* Try to figure out what a reference refers to, and
708 access its virtual function table directly. */
710 int cdtorp = 0;
711 tree fixed_type = fixed_type_or_null (instance, NULL, &cdtorp);
713 tree basetype = non_reference (TREE_TYPE (instance));
715 if (fixed_type && !cdtorp)
717 tree binfo = lookup_base (fixed_type, basetype,
718 ba_unique, NULL, tf_none);
719 if (binfo && binfo != error_mark_node)
720 vtbl = unshare_expr (BINFO_VTABLE (binfo));
723 if (!vtbl)
724 vtbl = build_vfield_ref (instance, basetype);
726 aref = build_array_ref (input_location, vtbl, idx);
727 TREE_CONSTANT (aref) |= TREE_CONSTANT (vtbl) && TREE_CONSTANT (idx);
729 return aref;
732 tree
733 build_vtbl_ref (tree instance, tree idx)
735 tree aref = build_vtbl_ref_1 (instance, idx);
737 return aref;
740 /* Given a stable object pointer INSTANCE_PTR, return an expression which
741 yields a function pointer corresponding to vtable element INDEX. */
743 tree
744 build_vfn_ref (tree instance_ptr, tree idx)
746 tree aref;
748 aref = build_vtbl_ref_1 (cp_build_indirect_ref (instance_ptr, RO_NULL,
749 tf_warning_or_error),
750 idx);
752 /* When using function descriptors, the address of the
753 vtable entry is treated as a function pointer. */
754 if (TARGET_VTABLE_USES_DESCRIPTORS)
755 aref = build1 (NOP_EXPR, TREE_TYPE (aref),
756 cp_build_addr_expr (aref, tf_warning_or_error));
758 /* Remember this as a method reference, for later devirtualization. */
759 aref = build3 (OBJ_TYPE_REF, TREE_TYPE (aref), aref, instance_ptr, idx);
761 return aref;
764 /* Return the name of the virtual function table (as an IDENTIFIER_NODE)
765 for the given TYPE. */
767 static tree
768 get_vtable_name (tree type)
770 return mangle_vtbl_for_type (type);
773 /* DECL is an entity associated with TYPE, like a virtual table or an
774 implicitly generated constructor. Determine whether or not DECL
775 should have external or internal linkage at the object file
776 level. This routine does not deal with COMDAT linkage and other
777 similar complexities; it simply sets TREE_PUBLIC if it possible for
778 entities in other translation units to contain copies of DECL, in
779 the abstract. */
781 void
782 set_linkage_according_to_type (tree /*type*/, tree decl)
784 TREE_PUBLIC (decl) = 1;
785 determine_visibility (decl);
788 /* Create a VAR_DECL for a primary or secondary vtable for CLASS_TYPE.
789 (For a secondary vtable for B-in-D, CLASS_TYPE should be D, not B.)
790 Use NAME for the name of the vtable, and VTABLE_TYPE for its type. */
792 static tree
793 build_vtable (tree class_type, tree name, tree vtable_type)
795 tree decl;
797 decl = build_lang_decl (VAR_DECL, name, vtable_type);
798 /* vtable names are already mangled; give them their DECL_ASSEMBLER_NAME
799 now to avoid confusion in mangle_decl. */
800 SET_DECL_ASSEMBLER_NAME (decl, name);
801 DECL_CONTEXT (decl) = class_type;
802 DECL_ARTIFICIAL (decl) = 1;
803 TREE_STATIC (decl) = 1;
804 TREE_READONLY (decl) = 1;
805 DECL_VIRTUAL_P (decl) = 1;
806 SET_DECL_ALIGN (decl, TARGET_VTABLE_ENTRY_ALIGN);
807 DECL_USER_ALIGN (decl) = true;
808 DECL_VTABLE_OR_VTT_P (decl) = 1;
809 set_linkage_according_to_type (class_type, decl);
810 /* The vtable has not been defined -- yet. */
811 DECL_EXTERNAL (decl) = 1;
812 DECL_NOT_REALLY_EXTERN (decl) = 1;
814 /* Mark the VAR_DECL node representing the vtable itself as a
815 "gratuitous" one, thereby forcing dwarfout.c to ignore it. It
816 is rather important that such things be ignored because any
817 effort to actually generate DWARF for them will run into
818 trouble when/if we encounter code like:
820 #pragma interface
821 struct S { virtual void member (); };
823 because the artificial declaration of the vtable itself (as
824 manufactured by the g++ front end) will say that the vtable is
825 a static member of `S' but only *after* the debug output for
826 the definition of `S' has already been output. This causes
827 grief because the DWARF entry for the definition of the vtable
828 will try to refer back to an earlier *declaration* of the
829 vtable as a static member of `S' and there won't be one. We
830 might be able to arrange to have the "vtable static member"
831 attached to the member list for `S' before the debug info for
832 `S' get written (which would solve the problem) but that would
833 require more intrusive changes to the g++ front end. */
834 DECL_IGNORED_P (decl) = 1;
836 return decl;
839 /* Get the VAR_DECL of the vtable for TYPE. TYPE need not be polymorphic,
840 or even complete. If this does not exist, create it. If COMPLETE is
841 nonzero, then complete the definition of it -- that will render it
842 impossible to actually build the vtable, but is useful to get at those
843 which are known to exist in the runtime. */
845 tree
846 get_vtable_decl (tree type, int complete)
848 tree decl;
850 if (CLASSTYPE_VTABLES (type))
851 return CLASSTYPE_VTABLES (type);
853 decl = build_vtable (type, get_vtable_name (type), vtbl_type_node);
854 CLASSTYPE_VTABLES (type) = decl;
856 if (complete)
858 DECL_EXTERNAL (decl) = 1;
859 cp_finish_decl (decl, NULL_TREE, false, NULL_TREE, 0);
862 return decl;
865 /* Build the primary virtual function table for TYPE. If BINFO is
866 non-NULL, build the vtable starting with the initial approximation
867 that it is the same as the one which is the head of the association
868 list. Returns a nonzero value if a new vtable is actually
869 created. */
871 static int
872 build_primary_vtable (tree binfo, tree type)
874 tree decl;
875 tree virtuals;
877 decl = get_vtable_decl (type, /*complete=*/0);
879 if (binfo)
881 if (BINFO_NEW_VTABLE_MARKED (binfo))
882 /* We have already created a vtable for this base, so there's
883 no need to do it again. */
884 return 0;
886 virtuals = copy_list (BINFO_VIRTUALS (binfo));
887 TREE_TYPE (decl) = TREE_TYPE (get_vtbl_decl_for_binfo (binfo));
888 DECL_SIZE (decl) = TYPE_SIZE (TREE_TYPE (decl));
889 DECL_SIZE_UNIT (decl) = TYPE_SIZE_UNIT (TREE_TYPE (decl));
891 else
893 gcc_assert (TREE_TYPE (decl) == vtbl_type_node);
894 virtuals = NULL_TREE;
897 if (GATHER_STATISTICS)
899 n_vtables += 1;
900 n_vtable_elems += list_length (virtuals);
903 /* Initialize the association list for this type, based
904 on our first approximation. */
905 BINFO_VTABLE (TYPE_BINFO (type)) = decl;
906 BINFO_VIRTUALS (TYPE_BINFO (type)) = virtuals;
907 SET_BINFO_NEW_VTABLE_MARKED (TYPE_BINFO (type));
908 return 1;
911 /* Give BINFO a new virtual function table which is initialized
912 with a skeleton-copy of its original initialization. The only
913 entry that changes is the `delta' entry, so we can really
914 share a lot of structure.
916 FOR_TYPE is the most derived type which caused this table to
917 be needed.
919 Returns nonzero if we haven't met BINFO before.
921 The order in which vtables are built (by calling this function) for
922 an object must remain the same, otherwise a binary incompatibility
923 can result. */
925 static int
926 build_secondary_vtable (tree binfo)
928 if (BINFO_NEW_VTABLE_MARKED (binfo))
929 /* We already created a vtable for this base. There's no need to
930 do it again. */
931 return 0;
933 /* Remember that we've created a vtable for this BINFO, so that we
934 don't try to do so again. */
935 SET_BINFO_NEW_VTABLE_MARKED (binfo);
937 /* Make fresh virtual list, so we can smash it later. */
938 BINFO_VIRTUALS (binfo) = copy_list (BINFO_VIRTUALS (binfo));
940 /* Secondary vtables are laid out as part of the same structure as
941 the primary vtable. */
942 BINFO_VTABLE (binfo) = NULL_TREE;
943 return 1;
946 /* Create a new vtable for BINFO which is the hierarchy dominated by
947 T. Return nonzero if we actually created a new vtable. */
949 static int
950 make_new_vtable (tree t, tree binfo)
952 if (binfo == TYPE_BINFO (t))
953 /* In this case, it is *type*'s vtable we are modifying. We start
954 with the approximation that its vtable is that of the
955 immediate base class. */
956 return build_primary_vtable (binfo, t);
957 else
958 /* This is our very own copy of `basetype' to play with. Later,
959 we will fill in all the virtual functions that override the
960 virtual functions in these base classes which are not defined
961 by the current type. */
962 return build_secondary_vtable (binfo);
965 /* Make *VIRTUALS, an entry on the BINFO_VIRTUALS list for BINFO
966 (which is in the hierarchy dominated by T) list FNDECL as its
967 BV_FN. DELTA is the required constant adjustment from the `this'
968 pointer where the vtable entry appears to the `this' required when
969 the function is actually called. */
971 static void
972 modify_vtable_entry (tree t,
973 tree binfo,
974 tree fndecl,
975 tree delta,
976 tree *virtuals)
978 tree v;
980 v = *virtuals;
982 if (fndecl != BV_FN (v)
983 || !tree_int_cst_equal (delta, BV_DELTA (v)))
985 /* We need a new vtable for BINFO. */
986 if (make_new_vtable (t, binfo))
988 /* If we really did make a new vtable, we also made a copy
989 of the BINFO_VIRTUALS list. Now, we have to find the
990 corresponding entry in that list. */
991 *virtuals = BINFO_VIRTUALS (binfo);
992 while (BV_FN (*virtuals) != BV_FN (v))
993 *virtuals = TREE_CHAIN (*virtuals);
994 v = *virtuals;
997 BV_DELTA (v) = delta;
998 BV_VCALL_INDEX (v) = NULL_TREE;
999 BV_FN (v) = fndecl;
1004 /* Add method METHOD to class TYPE. If VIA_USING indicates whether
1005 METHOD is being injected via a using_decl. Returns true if the
1006 method could be added to the method vec. */
1008 bool
1009 add_method (tree type, tree method, bool via_using)
1011 if (method == error_mark_node)
1012 return false;
1014 /* Maintain TYPE_HAS_USER_CONSTRUCTOR, etc. */
1015 grok_special_member_properties (method);
1017 tree *slot = get_member_slot (type, DECL_NAME (method));
1018 tree current_fns = *slot;
1020 gcc_assert (!DECL_EXTERN_C_P (method));
1022 /* Check to see if we've already got this method. */
1023 for (ovl_iterator iter (current_fns); iter; ++iter)
1025 tree fn = *iter;
1026 tree fn_type;
1027 tree method_type;
1028 tree parms1;
1029 tree parms2;
1031 if (TREE_CODE (fn) != TREE_CODE (method))
1032 continue;
1034 /* Two using-declarations can coexist, we'll complain about ambiguity in
1035 overload resolution. */
1036 if (via_using && iter.using_p ()
1037 /* Except handle inherited constructors specially. */
1038 && ! DECL_CONSTRUCTOR_P (fn))
1039 continue;
1041 /* [over.load] Member function declarations with the
1042 same name and the same parameter types cannot be
1043 overloaded if any of them is a static member
1044 function declaration.
1046 [over.load] Member function declarations with the same name and
1047 the same parameter-type-list as well as member function template
1048 declarations with the same name, the same parameter-type-list, and
1049 the same template parameter lists cannot be overloaded if any of
1050 them, but not all, have a ref-qualifier.
1052 [namespace.udecl] When a using-declaration brings names
1053 from a base class into a derived class scope, member
1054 functions in the derived class override and/or hide member
1055 functions with the same name and parameter types in a base
1056 class (rather than conflicting). */
1057 fn_type = TREE_TYPE (fn);
1058 method_type = TREE_TYPE (method);
1059 parms1 = TYPE_ARG_TYPES (fn_type);
1060 parms2 = TYPE_ARG_TYPES (method_type);
1062 /* Compare the quals on the 'this' parm. Don't compare
1063 the whole types, as used functions are treated as
1064 coming from the using class in overload resolution. */
1065 if (! DECL_STATIC_FUNCTION_P (fn)
1066 && ! DECL_STATIC_FUNCTION_P (method)
1067 /* Either both or neither need to be ref-qualified for
1068 differing quals to allow overloading. */
1069 && (FUNCTION_REF_QUALIFIED (fn_type)
1070 == FUNCTION_REF_QUALIFIED (method_type))
1071 && (type_memfn_quals (fn_type) != type_memfn_quals (method_type)
1072 || type_memfn_rqual (fn_type) != type_memfn_rqual (method_type)))
1073 continue;
1075 /* For templates, the return type and template parameters
1076 must be identical. */
1077 if (TREE_CODE (fn) == TEMPLATE_DECL
1078 && (!same_type_p (TREE_TYPE (fn_type),
1079 TREE_TYPE (method_type))
1080 || !comp_template_parms (DECL_TEMPLATE_PARMS (fn),
1081 DECL_TEMPLATE_PARMS (method))))
1082 continue;
1084 if (! DECL_STATIC_FUNCTION_P (fn))
1085 parms1 = TREE_CHAIN (parms1);
1086 if (! DECL_STATIC_FUNCTION_P (method))
1087 parms2 = TREE_CHAIN (parms2);
1089 /* Bring back parameters omitted from an inherited ctor. */
1090 if (ctor_omit_inherited_parms (fn))
1091 parms1 = FUNCTION_FIRST_USER_PARMTYPE (DECL_ORIGIN (fn));
1092 if (ctor_omit_inherited_parms (method))
1093 parms2 = FUNCTION_FIRST_USER_PARMTYPE (DECL_ORIGIN (method));
1095 if (compparms (parms1, parms2)
1096 && (!DECL_CONV_FN_P (fn)
1097 || same_type_p (TREE_TYPE (fn_type),
1098 TREE_TYPE (method_type)))
1099 && equivalently_constrained (fn, method))
1101 /* If these are versions of the same function, process and
1102 move on. */
1103 if (TREE_CODE (fn) == FUNCTION_DECL
1104 && maybe_version_functions (method, fn))
1105 continue;
1107 if (DECL_INHERITED_CTOR (method))
1109 if (DECL_INHERITED_CTOR (fn))
1111 tree basem = DECL_INHERITED_CTOR_BASE (method);
1112 tree basef = DECL_INHERITED_CTOR_BASE (fn);
1113 if (flag_new_inheriting_ctors)
1115 if (basem == basef)
1117 /* Inheriting the same constructor along different
1118 paths, combine them. */
1119 SET_DECL_INHERITED_CTOR
1120 (fn, ovl_make (DECL_INHERITED_CTOR (method),
1121 DECL_INHERITED_CTOR (fn)));
1122 /* And discard the new one. */
1123 return false;
1125 else
1126 /* Inherited ctors can coexist until overload
1127 resolution. */
1128 continue;
1130 error_at (DECL_SOURCE_LOCATION (method),
1131 "%q#D conflicts with version inherited from %qT",
1132 method, basef);
1133 inform (DECL_SOURCE_LOCATION (fn),
1134 "version inherited from %qT declared here",
1135 basef);
1137 /* Otherwise defer to the other function. */
1138 return false;
1141 if (via_using)
1142 /* Defer to the local function. */
1143 return false;
1144 else if (flag_new_inheriting_ctors
1145 && DECL_INHERITED_CTOR (fn))
1147 /* Remove the inherited constructor. */
1148 current_fns = iter.remove_node (current_fns);
1149 continue;
1151 else
1153 error_at (DECL_SOURCE_LOCATION (method),
1154 "%q#D cannot be overloaded with %q#D", method, fn);
1155 inform (DECL_SOURCE_LOCATION (fn),
1156 "previous declaration %q#D", fn);
1157 return false;
1162 /* A class should never have more than one destructor. */
1163 gcc_assert (!current_fns || !DECL_DESTRUCTOR_P (method));
1165 current_fns = ovl_insert (method, current_fns, via_using);
1167 if (!DECL_CONV_FN_P (method) && !COMPLETE_TYPE_P (type))
1168 push_class_level_binding (DECL_NAME (method), current_fns);
1170 *slot = current_fns;
1172 return true;
1175 /* Subroutines of finish_struct. */
1177 /* Change the access of FDECL to ACCESS in T. Return 1 if change was
1178 legit, otherwise return 0. */
1180 static int
1181 alter_access (tree t, tree fdecl, tree access)
1183 tree elem;
1185 retrofit_lang_decl (fdecl);
1187 gcc_assert (!DECL_DISCRIMINATOR_P (fdecl));
1189 elem = purpose_member (t, DECL_ACCESS (fdecl));
1190 if (elem)
1192 if (TREE_VALUE (elem) != access)
1194 if (TREE_CODE (TREE_TYPE (fdecl)) == FUNCTION_DECL)
1195 error ("conflicting access specifications for method"
1196 " %q+D, ignored", TREE_TYPE (fdecl));
1197 else
1198 error ("conflicting access specifications for field %qE, ignored",
1199 DECL_NAME (fdecl));
1201 else
1203 /* They're changing the access to the same thing they changed
1204 it to before. That's OK. */
1208 else
1210 perform_or_defer_access_check (TYPE_BINFO (t), fdecl, fdecl,
1211 tf_warning_or_error);
1212 DECL_ACCESS (fdecl) = tree_cons (t, access, DECL_ACCESS (fdecl));
1213 return 1;
1215 return 0;
1218 /* Return the access node for DECL's access in its enclosing class. */
1220 tree
1221 declared_access (tree decl)
1223 return (TREE_PRIVATE (decl) ? access_private_node
1224 : TREE_PROTECTED (decl) ? access_protected_node
1225 : access_public_node);
1228 /* Process the USING_DECL, which is a member of T. */
1230 static void
1231 handle_using_decl (tree using_decl, tree t)
1233 tree decl = USING_DECL_DECLS (using_decl);
1234 tree name = DECL_NAME (using_decl);
1235 tree access = declared_access (using_decl);
1236 tree flist = NULL_TREE;
1237 tree old_value;
1239 gcc_assert (!processing_template_decl && decl);
1241 old_value = lookup_member (t, name, /*protect=*/0, /*want_type=*/false,
1242 tf_warning_or_error);
1243 if (old_value)
1245 old_value = OVL_FIRST (old_value);
1247 if (DECL_P (old_value) && DECL_CONTEXT (old_value) == t)
1248 /* OK */;
1249 else
1250 old_value = NULL_TREE;
1253 cp_emit_debug_info_for_using (decl, t);
1255 if (is_overloaded_fn (decl))
1256 flist = decl;
1258 if (! old_value)
1260 else if (is_overloaded_fn (old_value))
1262 if (flist)
1263 /* It's OK to use functions from a base when there are functions with
1264 the same name already present in the current class. */;
1265 else
1267 error_at (DECL_SOURCE_LOCATION (using_decl), "%qD invalid in %q#T "
1268 "because of local method %q#D with same name",
1269 using_decl, t, old_value);
1270 inform (DECL_SOURCE_LOCATION (old_value),
1271 "local method %q#D declared here", old_value);
1272 return;
1275 else if (!DECL_ARTIFICIAL (old_value))
1277 error_at (DECL_SOURCE_LOCATION (using_decl), "%qD invalid in %q#T "
1278 "because of local member %q#D with same name",
1279 using_decl, t, old_value);
1280 inform (DECL_SOURCE_LOCATION (old_value),
1281 "local member %q#D declared here", old_value);
1282 return;
1285 /* Make type T see field decl FDECL with access ACCESS. */
1286 if (flist)
1287 for (ovl_iterator iter (flist); iter; ++iter)
1289 add_method (t, *iter, true);
1290 alter_access (t, *iter, access);
1292 else
1293 alter_access (t, decl, access);
1296 /* Data structure for find_abi_tags_r, below. */
1298 struct abi_tag_data
1300 tree t; // The type that we're checking for missing tags.
1301 tree subob; // The subobject of T that we're getting tags from.
1302 tree tags; // error_mark_node for diagnostics, or a list of missing tags.
1305 /* Subroutine of find_abi_tags_r. Handle a single TAG found on the class TP
1306 in the context of P. TAG can be either an identifier (the DECL_NAME of
1307 a tag NAMESPACE_DECL) or a STRING_CST (a tag attribute). */
1309 static void
1310 check_tag (tree tag, tree id, tree *tp, abi_tag_data *p)
1312 if (!IDENTIFIER_MARKED (id))
1314 if (p->tags != error_mark_node)
1316 /* We're collecting tags from template arguments or from
1317 the type of a variable or function return type. */
1318 p->tags = tree_cons (NULL_TREE, tag, p->tags);
1320 /* Don't inherit this tag multiple times. */
1321 IDENTIFIER_MARKED (id) = true;
1323 if (TYPE_P (p->t))
1325 /* Tags inherited from type template arguments are only used
1326 to avoid warnings. */
1327 ABI_TAG_IMPLICIT (p->tags) = true;
1328 return;
1330 /* For functions and variables we want to warn, too. */
1333 /* Otherwise we're diagnosing missing tags. */
1334 if (TREE_CODE (p->t) == FUNCTION_DECL)
1336 if (warning (OPT_Wabi_tag, "%qD inherits the %E ABI tag "
1337 "that %qT (used in its return type) has",
1338 p->t, tag, *tp))
1339 inform (location_of (*tp), "%qT declared here", *tp);
1341 else if (VAR_P (p->t))
1343 if (warning (OPT_Wabi_tag, "%qD inherits the %E ABI tag "
1344 "that %qT (used in its type) has", p->t, tag, *tp))
1345 inform (location_of (*tp), "%qT declared here", *tp);
1347 else if (TYPE_P (p->subob))
1349 if (warning (OPT_Wabi_tag, "%qT does not have the %E ABI tag "
1350 "that base %qT has", p->t, tag, p->subob))
1351 inform (location_of (p->subob), "%qT declared here",
1352 p->subob);
1354 else
1356 if (warning (OPT_Wabi_tag, "%qT does not have the %E ABI tag "
1357 "that %qT (used in the type of %qD) has",
1358 p->t, tag, *tp, p->subob))
1360 inform (location_of (p->subob), "%qD declared here",
1361 p->subob);
1362 inform (location_of (*tp), "%qT declared here", *tp);
1368 /* Find all the ABI tags in the attribute list ATTR and either call
1369 check_tag (if TP is non-null) or set IDENTIFIER_MARKED to val. */
1371 static void
1372 mark_or_check_attr_tags (tree attr, tree *tp, abi_tag_data *p, bool val)
1374 if (!attr)
1375 return;
1376 for (; (attr = lookup_attribute ("abi_tag", attr));
1377 attr = TREE_CHAIN (attr))
1378 for (tree list = TREE_VALUE (attr); list;
1379 list = TREE_CHAIN (list))
1381 tree tag = TREE_VALUE (list);
1382 tree id = get_identifier (TREE_STRING_POINTER (tag));
1383 if (tp)
1384 check_tag (tag, id, tp, p);
1385 else
1386 IDENTIFIER_MARKED (id) = val;
1390 /* Find all the ABI tags on T and its enclosing scopes and either call
1391 check_tag (if TP is non-null) or set IDENTIFIER_MARKED to val. */
1393 static void
1394 mark_or_check_tags (tree t, tree *tp, abi_tag_data *p, bool val)
1396 while (t != global_namespace)
1398 tree attr;
1399 if (TYPE_P (t))
1401 attr = TYPE_ATTRIBUTES (t);
1402 t = CP_TYPE_CONTEXT (t);
1404 else
1406 attr = DECL_ATTRIBUTES (t);
1407 t = CP_DECL_CONTEXT (t);
1409 mark_or_check_attr_tags (attr, tp, p, val);
1413 /* walk_tree callback for check_abi_tags: if the type at *TP involves any
1414 types with ABI tags, add the corresponding identifiers to the VEC in
1415 *DATA and set IDENTIFIER_MARKED. */
1417 static tree
1418 find_abi_tags_r (tree *tp, int *walk_subtrees, void *data)
1420 if (!OVERLOAD_TYPE_P (*tp))
1421 return NULL_TREE;
1423 /* walk_tree shouldn't be walking into any subtrees of a RECORD_TYPE
1424 anyway, but let's make sure of it. */
1425 *walk_subtrees = false;
1427 abi_tag_data *p = static_cast<struct abi_tag_data*>(data);
1429 mark_or_check_tags (*tp, tp, p, false);
1431 return NULL_TREE;
1434 /* walk_tree callback for mark_abi_tags: if *TP is a class, set
1435 IDENTIFIER_MARKED on its ABI tags. */
1437 static tree
1438 mark_abi_tags_r (tree *tp, int *walk_subtrees, void *data)
1440 if (!OVERLOAD_TYPE_P (*tp))
1441 return NULL_TREE;
1443 /* walk_tree shouldn't be walking into any subtrees of a RECORD_TYPE
1444 anyway, but let's make sure of it. */
1445 *walk_subtrees = false;
1447 bool *valp = static_cast<bool*>(data);
1449 mark_or_check_tags (*tp, NULL, NULL, *valp);
1451 return NULL_TREE;
1454 /* Set IDENTIFIER_MARKED on all the ABI tags on T and its enclosing
1455 scopes. */
1457 static void
1458 mark_abi_tags (tree t, bool val)
1460 mark_or_check_tags (t, NULL, NULL, val);
1461 if (DECL_P (t))
1463 if (DECL_LANG_SPECIFIC (t) && DECL_USE_TEMPLATE (t)
1464 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (t)))
1466 /* Template arguments are part of the signature. */
1467 tree level = INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (t));
1468 for (int j = 0; j < TREE_VEC_LENGTH (level); ++j)
1470 tree arg = TREE_VEC_ELT (level, j);
1471 cp_walk_tree_without_duplicates (&arg, mark_abi_tags_r, &val);
1474 if (TREE_CODE (t) == FUNCTION_DECL)
1475 /* A function's parameter types are part of the signature, so
1476 we don't need to inherit any tags that are also in them. */
1477 for (tree arg = FUNCTION_FIRST_USER_PARMTYPE (t); arg;
1478 arg = TREE_CHAIN (arg))
1479 cp_walk_tree_without_duplicates (&TREE_VALUE (arg),
1480 mark_abi_tags_r, &val);
1484 /* Check that T has all the ABI tags that subobject SUBOB has, or
1485 warn if not. If T is a (variable or function) declaration, also
1486 return any missing tags, and add them to T if JUST_CHECKING is false. */
1488 static tree
1489 check_abi_tags (tree t, tree subob, bool just_checking = false)
1491 bool inherit = DECL_P (t);
1493 if (!inherit && !warn_abi_tag)
1494 return NULL_TREE;
1496 tree decl = TYPE_P (t) ? TYPE_NAME (t) : t;
1497 if (!TREE_PUBLIC (decl))
1498 /* No need to worry about things local to this TU. */
1499 return NULL_TREE;
1501 mark_abi_tags (t, true);
1503 tree subtype = TYPE_P (subob) ? subob : TREE_TYPE (subob);
1504 struct abi_tag_data data = { t, subob, error_mark_node };
1505 if (inherit)
1506 data.tags = NULL_TREE;
1508 cp_walk_tree_without_duplicates (&subtype, find_abi_tags_r, &data);
1510 if (!(inherit && data.tags))
1511 /* We don't need to do anything with data.tags. */;
1512 else if (just_checking)
1513 for (tree t = data.tags; t; t = TREE_CHAIN (t))
1515 tree id = get_identifier (TREE_STRING_POINTER (TREE_VALUE (t)));
1516 IDENTIFIER_MARKED (id) = false;
1518 else
1520 tree attr = lookup_attribute ("abi_tag", DECL_ATTRIBUTES (t));
1521 if (attr)
1522 TREE_VALUE (attr) = chainon (data.tags, TREE_VALUE (attr));
1523 else
1524 DECL_ATTRIBUTES (t)
1525 = tree_cons (get_identifier ("abi_tag"), data.tags,
1526 DECL_ATTRIBUTES (t));
1529 mark_abi_tags (t, false);
1531 return data.tags;
1534 /* Check that DECL has all the ABI tags that are used in parts of its type
1535 that are not reflected in its mangled name. */
1537 void
1538 check_abi_tags (tree decl)
1540 if (VAR_P (decl))
1541 check_abi_tags (decl, TREE_TYPE (decl));
1542 else if (TREE_CODE (decl) == FUNCTION_DECL
1543 && !DECL_CONV_FN_P (decl)
1544 && !mangle_return_type_p (decl))
1545 check_abi_tags (decl, TREE_TYPE (TREE_TYPE (decl)));
1548 /* Return any ABI tags that are used in parts of the type of DECL
1549 that are not reflected in its mangled name. This function is only
1550 used in backward-compatible mangling for ABI <11. */
1552 tree
1553 missing_abi_tags (tree decl)
1555 if (VAR_P (decl))
1556 return check_abi_tags (decl, TREE_TYPE (decl), true);
1557 else if (TREE_CODE (decl) == FUNCTION_DECL
1558 /* Don't check DECL_CONV_FN_P here like we do in check_abi_tags, so
1559 that we can use this function for setting need_abi_warning
1560 regardless of the current flag_abi_version. */
1561 && !mangle_return_type_p (decl))
1562 return check_abi_tags (decl, TREE_TYPE (TREE_TYPE (decl)), true);
1563 else
1564 return NULL_TREE;
1567 void
1568 inherit_targ_abi_tags (tree t)
1570 if (!CLASS_TYPE_P (t)
1571 || CLASSTYPE_TEMPLATE_INFO (t) == NULL_TREE)
1572 return;
1574 mark_abi_tags (t, true);
1576 tree args = CLASSTYPE_TI_ARGS (t);
1577 struct abi_tag_data data = { t, NULL_TREE, NULL_TREE };
1578 for (int i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
1580 tree level = TMPL_ARGS_LEVEL (args, i+1);
1581 for (int j = 0; j < TREE_VEC_LENGTH (level); ++j)
1583 tree arg = TREE_VEC_ELT (level, j);
1584 data.subob = arg;
1585 cp_walk_tree_without_duplicates (&arg, find_abi_tags_r, &data);
1589 // If we found some tags on our template arguments, add them to our
1590 // abi_tag attribute.
1591 if (data.tags)
1593 tree attr = lookup_attribute ("abi_tag", TYPE_ATTRIBUTES (t));
1594 if (attr)
1595 TREE_VALUE (attr) = chainon (data.tags, TREE_VALUE (attr));
1596 else
1597 TYPE_ATTRIBUTES (t)
1598 = tree_cons (get_identifier ("abi_tag"), data.tags,
1599 TYPE_ATTRIBUTES (t));
1602 mark_abi_tags (t, false);
1605 /* Return true, iff class T has a non-virtual destructor that is
1606 accessible from outside the class heirarchy (i.e. is public, or
1607 there's a suitable friend. */
1609 static bool
1610 accessible_nvdtor_p (tree t)
1612 tree dtor = CLASSTYPE_DESTRUCTOR (t);
1614 /* An implicitly declared destructor is always public. And,
1615 if it were virtual, we would have created it by now. */
1616 if (!dtor)
1617 return true;
1619 if (DECL_VINDEX (dtor))
1620 return false; /* Virtual */
1622 if (!TREE_PRIVATE (dtor) && !TREE_PROTECTED (dtor))
1623 return true; /* Public */
1625 if (CLASSTYPE_FRIEND_CLASSES (t)
1626 || DECL_FRIENDLIST (TYPE_MAIN_DECL (t)))
1627 return true; /* Has friends */
1629 return false;
1632 /* Run through the base classes of T, updating CANT_HAVE_CONST_CTOR_P,
1633 and NO_CONST_ASN_REF_P. Also set flag bits in T based on
1634 properties of the bases. */
1636 static void
1637 check_bases (tree t,
1638 int* cant_have_const_ctor_p,
1639 int* no_const_asn_ref_p)
1641 int i;
1642 bool seen_non_virtual_nearly_empty_base_p = 0;
1643 int seen_tm_mask = 0;
1644 tree base_binfo;
1645 tree binfo;
1646 tree field = NULL_TREE;
1648 if (!CLASSTYPE_NON_STD_LAYOUT (t))
1649 for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
1650 if (TREE_CODE (field) == FIELD_DECL)
1651 break;
1653 for (binfo = TYPE_BINFO (t), i = 0;
1654 BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
1656 tree basetype = TREE_TYPE (base_binfo);
1658 gcc_assert (COMPLETE_TYPE_P (basetype));
1660 if (CLASSTYPE_FINAL (basetype))
1661 error ("cannot derive from %<final%> base %qT in derived type %qT",
1662 basetype, t);
1664 /* If any base class is non-literal, so is the derived class. */
1665 if (!CLASSTYPE_LITERAL_P (basetype))
1666 CLASSTYPE_LITERAL_P (t) = false;
1668 /* If the base class doesn't have copy constructors or
1669 assignment operators that take const references, then the
1670 derived class cannot have such a member automatically
1671 generated. */
1672 if (TYPE_HAS_COPY_CTOR (basetype)
1673 && ! TYPE_HAS_CONST_COPY_CTOR (basetype))
1674 *cant_have_const_ctor_p = 1;
1675 if (TYPE_HAS_COPY_ASSIGN (basetype)
1676 && !TYPE_HAS_CONST_COPY_ASSIGN (basetype))
1677 *no_const_asn_ref_p = 1;
1679 if (BINFO_VIRTUAL_P (base_binfo))
1680 /* A virtual base does not effect nearly emptiness. */
1682 else if (CLASSTYPE_NEARLY_EMPTY_P (basetype))
1684 if (seen_non_virtual_nearly_empty_base_p)
1685 /* And if there is more than one nearly empty base, then the
1686 derived class is not nearly empty either. */
1687 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
1688 else
1689 /* Remember we've seen one. */
1690 seen_non_virtual_nearly_empty_base_p = 1;
1692 else if (!is_empty_class (basetype))
1693 /* If the base class is not empty or nearly empty, then this
1694 class cannot be nearly empty. */
1695 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
1697 /* A lot of properties from the bases also apply to the derived
1698 class. */
1699 TYPE_NEEDS_CONSTRUCTING (t) |= TYPE_NEEDS_CONSTRUCTING (basetype);
1700 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t)
1701 |= TYPE_HAS_NONTRIVIAL_DESTRUCTOR (basetype);
1702 TYPE_HAS_COMPLEX_COPY_ASSIGN (t)
1703 |= (TYPE_HAS_COMPLEX_COPY_ASSIGN (basetype)
1704 || !TYPE_HAS_COPY_ASSIGN (basetype));
1705 TYPE_HAS_COMPLEX_COPY_CTOR (t) |= (TYPE_HAS_COMPLEX_COPY_CTOR (basetype)
1706 || !TYPE_HAS_COPY_CTOR (basetype));
1707 TYPE_HAS_COMPLEX_MOVE_ASSIGN (t)
1708 |= TYPE_HAS_COMPLEX_MOVE_ASSIGN (basetype);
1709 TYPE_HAS_COMPLEX_MOVE_CTOR (t) |= TYPE_HAS_COMPLEX_MOVE_CTOR (basetype);
1710 TYPE_POLYMORPHIC_P (t) |= TYPE_POLYMORPHIC_P (basetype);
1711 CLASSTYPE_CONTAINS_EMPTY_CLASS_P (t)
1712 |= CLASSTYPE_CONTAINS_EMPTY_CLASS_P (basetype);
1713 TYPE_HAS_COMPLEX_DFLT (t) |= (!TYPE_HAS_DEFAULT_CONSTRUCTOR (basetype)
1714 || TYPE_HAS_COMPLEX_DFLT (basetype));
1715 SET_CLASSTYPE_READONLY_FIELDS_NEED_INIT
1716 (t, CLASSTYPE_READONLY_FIELDS_NEED_INIT (t)
1717 | CLASSTYPE_READONLY_FIELDS_NEED_INIT (basetype));
1718 SET_CLASSTYPE_REF_FIELDS_NEED_INIT
1719 (t, CLASSTYPE_REF_FIELDS_NEED_INIT (t)
1720 | CLASSTYPE_REF_FIELDS_NEED_INIT (basetype));
1721 if (TYPE_HAS_MUTABLE_P (basetype))
1722 CLASSTYPE_HAS_MUTABLE (t) = 1;
1724 /* A standard-layout class is a class that:
1726 * has no non-standard-layout base classes, */
1727 CLASSTYPE_NON_STD_LAYOUT (t) |= CLASSTYPE_NON_STD_LAYOUT (basetype);
1728 if (!CLASSTYPE_NON_STD_LAYOUT (t))
1730 tree basefield;
1731 /* ...has no base classes of the same type as the first non-static
1732 data member... */
1733 if (field && DECL_CONTEXT (field) == t
1734 && (same_type_ignoring_top_level_qualifiers_p
1735 (TREE_TYPE (field), basetype)))
1736 CLASSTYPE_NON_STD_LAYOUT (t) = 1;
1737 else
1738 /* ...either has no non-static data members in the most-derived
1739 class and at most one base class with non-static data
1740 members, or has no base classes with non-static data
1741 members */
1742 for (basefield = TYPE_FIELDS (basetype); basefield;
1743 basefield = DECL_CHAIN (basefield))
1744 if (TREE_CODE (basefield) == FIELD_DECL
1745 && !(DECL_FIELD_IS_BASE (basefield)
1746 && integer_zerop (DECL_SIZE (basefield))))
1748 if (field)
1749 CLASSTYPE_NON_STD_LAYOUT (t) = 1;
1750 else
1751 field = basefield;
1752 break;
1756 /* Don't bother collecting tm attributes if transactional memory
1757 support is not enabled. */
1758 if (flag_tm)
1760 tree tm_attr = find_tm_attribute (TYPE_ATTRIBUTES (basetype));
1761 if (tm_attr)
1762 seen_tm_mask |= tm_attr_to_mask (tm_attr);
1765 check_abi_tags (t, basetype);
1768 /* If one of the base classes had TM attributes, and the current class
1769 doesn't define its own, then the current class inherits one. */
1770 if (seen_tm_mask && !find_tm_attribute (TYPE_ATTRIBUTES (t)))
1772 tree tm_attr = tm_mask_to_attr (least_bit_hwi (seen_tm_mask));
1773 TYPE_ATTRIBUTES (t) = tree_cons (tm_attr, NULL, TYPE_ATTRIBUTES (t));
1777 /* Determine all the primary bases within T. Sets BINFO_PRIMARY_BASE_P for
1778 those that are primaries. Sets BINFO_LOST_PRIMARY_P for those
1779 that have had a nearly-empty virtual primary base stolen by some
1780 other base in the hierarchy. Determines CLASSTYPE_PRIMARY_BASE for
1781 T. */
1783 static void
1784 determine_primary_bases (tree t)
1786 unsigned i;
1787 tree primary = NULL_TREE;
1788 tree type_binfo = TYPE_BINFO (t);
1789 tree base_binfo;
1791 /* Determine the primary bases of our bases. */
1792 for (base_binfo = TREE_CHAIN (type_binfo); base_binfo;
1793 base_binfo = TREE_CHAIN (base_binfo))
1795 tree primary = CLASSTYPE_PRIMARY_BINFO (BINFO_TYPE (base_binfo));
1797 /* See if we're the non-virtual primary of our inheritance
1798 chain. */
1799 if (!BINFO_VIRTUAL_P (base_binfo))
1801 tree parent = BINFO_INHERITANCE_CHAIN (base_binfo);
1802 tree parent_primary = CLASSTYPE_PRIMARY_BINFO (BINFO_TYPE (parent));
1804 if (parent_primary
1805 && SAME_BINFO_TYPE_P (BINFO_TYPE (base_binfo),
1806 BINFO_TYPE (parent_primary)))
1807 /* We are the primary binfo. */
1808 BINFO_PRIMARY_P (base_binfo) = 1;
1810 /* Determine if we have a virtual primary base, and mark it so.
1812 if (primary && BINFO_VIRTUAL_P (primary))
1814 tree this_primary = copied_binfo (primary, base_binfo);
1816 if (BINFO_PRIMARY_P (this_primary))
1817 /* Someone already claimed this base. */
1818 BINFO_LOST_PRIMARY_P (base_binfo) = 1;
1819 else
1821 tree delta;
1823 BINFO_PRIMARY_P (this_primary) = 1;
1824 BINFO_INHERITANCE_CHAIN (this_primary) = base_binfo;
1826 /* A virtual binfo might have been copied from within
1827 another hierarchy. As we're about to use it as a
1828 primary base, make sure the offsets match. */
1829 delta = size_diffop_loc (input_location,
1830 fold_convert (ssizetype,
1831 BINFO_OFFSET (base_binfo)),
1832 fold_convert (ssizetype,
1833 BINFO_OFFSET (this_primary)));
1835 propagate_binfo_offsets (this_primary, delta);
1840 /* First look for a dynamic direct non-virtual base. */
1841 for (i = 0; BINFO_BASE_ITERATE (type_binfo, i, base_binfo); i++)
1843 tree basetype = BINFO_TYPE (base_binfo);
1845 if (TYPE_CONTAINS_VPTR_P (basetype) && !BINFO_VIRTUAL_P (base_binfo))
1847 primary = base_binfo;
1848 goto found;
1852 /* A "nearly-empty" virtual base class can be the primary base
1853 class, if no non-virtual polymorphic base can be found. Look for
1854 a nearly-empty virtual dynamic base that is not already a primary
1855 base of something in the hierarchy. If there is no such base,
1856 just pick the first nearly-empty virtual base. */
1858 for (base_binfo = TREE_CHAIN (type_binfo); base_binfo;
1859 base_binfo = TREE_CHAIN (base_binfo))
1860 if (BINFO_VIRTUAL_P (base_binfo)
1861 && CLASSTYPE_NEARLY_EMPTY_P (BINFO_TYPE (base_binfo)))
1863 if (!BINFO_PRIMARY_P (base_binfo))
1865 /* Found one that is not primary. */
1866 primary = base_binfo;
1867 goto found;
1869 else if (!primary)
1870 /* Remember the first candidate. */
1871 primary = base_binfo;
1874 found:
1875 /* If we've got a primary base, use it. */
1876 if (primary)
1878 tree basetype = BINFO_TYPE (primary);
1880 CLASSTYPE_PRIMARY_BINFO (t) = primary;
1881 if (BINFO_PRIMARY_P (primary))
1882 /* We are stealing a primary base. */
1883 BINFO_LOST_PRIMARY_P (BINFO_INHERITANCE_CHAIN (primary)) = 1;
1884 BINFO_PRIMARY_P (primary) = 1;
1885 if (BINFO_VIRTUAL_P (primary))
1887 tree delta;
1889 BINFO_INHERITANCE_CHAIN (primary) = type_binfo;
1890 /* A virtual binfo might have been copied from within
1891 another hierarchy. As we're about to use it as a primary
1892 base, make sure the offsets match. */
1893 delta = size_diffop_loc (input_location, ssize_int (0),
1894 fold_convert (ssizetype, BINFO_OFFSET (primary)));
1896 propagate_binfo_offsets (primary, delta);
1899 primary = TYPE_BINFO (basetype);
1901 TYPE_VFIELD (t) = TYPE_VFIELD (basetype);
1902 BINFO_VTABLE (type_binfo) = BINFO_VTABLE (primary);
1903 BINFO_VIRTUALS (type_binfo) = BINFO_VIRTUALS (primary);
1907 /* Update the variant types of T. */
1909 void
1910 fixup_type_variants (tree t)
1912 tree variants;
1914 if (!t)
1915 return;
1917 for (variants = TYPE_NEXT_VARIANT (t);
1918 variants;
1919 variants = TYPE_NEXT_VARIANT (variants))
1921 /* These fields are in the _TYPE part of the node, not in
1922 the TYPE_LANG_SPECIFIC component, so they are not shared. */
1923 TYPE_HAS_USER_CONSTRUCTOR (variants) = TYPE_HAS_USER_CONSTRUCTOR (t);
1924 TYPE_NEEDS_CONSTRUCTING (variants) = TYPE_NEEDS_CONSTRUCTING (t);
1925 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (variants)
1926 = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t);
1928 TYPE_POLYMORPHIC_P (variants) = TYPE_POLYMORPHIC_P (t);
1930 TYPE_BINFO (variants) = TYPE_BINFO (t);
1932 /* Copy whatever these are holding today. */
1933 TYPE_VFIELD (variants) = TYPE_VFIELD (t);
1934 TYPE_FIELDS (variants) = TYPE_FIELDS (t);
1938 /* KLASS is a class that we're applying may_alias to after the body is
1939 parsed. Fixup any POINTER_TO and REFERENCE_TO types. The
1940 canonical type(s) will be implicitly updated. */
1942 static void
1943 fixup_may_alias (tree klass)
1945 tree t, v;
1947 for (t = TYPE_POINTER_TO (klass); t; t = TYPE_NEXT_PTR_TO (t))
1948 for (v = TYPE_MAIN_VARIANT (t); v; v = TYPE_NEXT_VARIANT (v))
1949 TYPE_REF_CAN_ALIAS_ALL (v) = true;
1950 for (t = TYPE_REFERENCE_TO (klass); t; t = TYPE_NEXT_REF_TO (t))
1951 for (v = TYPE_MAIN_VARIANT (t); v; v = TYPE_NEXT_VARIANT (v))
1952 TYPE_REF_CAN_ALIAS_ALL (v) = true;
1955 /* Early variant fixups: we apply attributes at the beginning of the class
1956 definition, and we need to fix up any variants that have already been
1957 made via elaborated-type-specifier so that check_qualified_type works. */
1959 void
1960 fixup_attribute_variants (tree t)
1962 tree variants;
1964 if (!t)
1965 return;
1967 tree attrs = TYPE_ATTRIBUTES (t);
1968 unsigned align = TYPE_ALIGN (t);
1969 bool user_align = TYPE_USER_ALIGN (t);
1970 bool may_alias = lookup_attribute ("may_alias", attrs);
1972 if (may_alias)
1973 fixup_may_alias (t);
1975 for (variants = TYPE_NEXT_VARIANT (t);
1976 variants;
1977 variants = TYPE_NEXT_VARIANT (variants))
1979 /* These are the two fields that check_qualified_type looks at and
1980 are affected by attributes. */
1981 TYPE_ATTRIBUTES (variants) = attrs;
1982 unsigned valign = align;
1983 if (TYPE_USER_ALIGN (variants))
1984 valign = MAX (valign, TYPE_ALIGN (variants));
1985 else
1986 TYPE_USER_ALIGN (variants) = user_align;
1987 SET_TYPE_ALIGN (variants, valign);
1988 if (may_alias)
1989 fixup_may_alias (variants);
1993 /* Set memoizing fields and bits of T (and its variants) for later
1994 use. */
1996 static void
1997 finish_struct_bits (tree t)
1999 /* Fix up variants (if any). */
2000 fixup_type_variants (t);
2002 if (BINFO_N_BASE_BINFOS (TYPE_BINFO (t)) && TYPE_POLYMORPHIC_P (t))
2003 /* For a class w/o baseclasses, 'finish_struct' has set
2004 CLASSTYPE_PURE_VIRTUALS correctly (by definition).
2005 Similarly for a class whose base classes do not have vtables.
2006 When neither of these is true, we might have removed abstract
2007 virtuals (by providing a definition), added some (by declaring
2008 new ones), or redeclared ones from a base class. We need to
2009 recalculate what's really an abstract virtual at this point (by
2010 looking in the vtables). */
2011 get_pure_virtuals (t);
2013 /* If this type has a copy constructor or a destructor, force its
2014 mode to be BLKmode, and force its TREE_ADDRESSABLE bit to be
2015 nonzero. This will cause it to be passed by invisible reference
2016 and prevent it from being returned in a register. */
2017 if (type_has_nontrivial_copy_init (t)
2018 || TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t))
2020 tree variants;
2021 SET_DECL_MODE (TYPE_MAIN_DECL (t), BLKmode);
2022 for (variants = t; variants; variants = TYPE_NEXT_VARIANT (variants))
2024 SET_TYPE_MODE (variants, BLKmode);
2025 TREE_ADDRESSABLE (variants) = 1;
2030 /* Issue warnings about T having private constructors, but no friends,
2031 and so forth.
2033 HAS_NONPRIVATE_METHOD is nonzero if T has any non-private methods or
2034 static members. HAS_NONPRIVATE_STATIC_FN is nonzero if T has any
2035 non-private static member functions. */
2037 static void
2038 maybe_warn_about_overly_private_class (tree t)
2040 int has_member_fn = 0;
2041 int has_nonprivate_method = 0;
2043 if (!warn_ctor_dtor_privacy
2044 /* If the class has friends, those entities might create and
2045 access instances, so we should not warn. */
2046 || (CLASSTYPE_FRIEND_CLASSES (t)
2047 || DECL_FRIENDLIST (TYPE_MAIN_DECL (t)))
2048 /* We will have warned when the template was declared; there's
2049 no need to warn on every instantiation. */
2050 || CLASSTYPE_TEMPLATE_INSTANTIATION (t))
2051 /* There's no reason to even consider warning about this
2052 class. */
2053 return;
2055 /* We only issue one warning, if more than one applies, because
2056 otherwise, on code like:
2058 class A {
2059 // Oops - forgot `public:'
2060 A();
2061 A(const A&);
2062 ~A();
2065 we warn several times about essentially the same problem. */
2067 /* Check to see if all (non-constructor, non-destructor) member
2068 functions are private. (Since there are no friends or
2069 non-private statics, we can't ever call any of the private member
2070 functions.) */
2071 for (tree fn = TYPE_FIELDS (t); fn; fn = DECL_CHAIN (fn))
2072 if (!DECL_DECLARES_FUNCTION_P (fn))
2073 /* Not a function. */;
2074 else if (DECL_ARTIFICIAL (fn))
2075 /* We're not interested in compiler-generated methods; they don't
2076 provide any way to call private members. */;
2077 else if (!TREE_PRIVATE (fn))
2079 if (DECL_STATIC_FUNCTION_P (fn))
2080 /* A non-private static member function is just like a
2081 friend; it can create and invoke private member
2082 functions, and be accessed without a class
2083 instance. */
2084 return;
2086 has_nonprivate_method = 1;
2087 /* Keep searching for a static member function. */
2089 else if (!DECL_CONSTRUCTOR_P (fn) && !DECL_DESTRUCTOR_P (fn))
2090 has_member_fn = 1;
2092 if (!has_nonprivate_method && has_member_fn)
2094 /* There are no non-private methods, and there's at least one
2095 private member function that isn't a constructor or
2096 destructor. (If all the private members are
2097 constructors/destructors we want to use the code below that
2098 issues error messages specifically referring to
2099 constructors/destructors.) */
2100 unsigned i;
2101 tree binfo = TYPE_BINFO (t);
2103 for (i = 0; i != BINFO_N_BASE_BINFOS (binfo); i++)
2104 if (BINFO_BASE_ACCESS (binfo, i) != access_private_node)
2106 has_nonprivate_method = 1;
2107 break;
2109 if (!has_nonprivate_method)
2111 warning (OPT_Wctor_dtor_privacy,
2112 "all member functions in class %qT are private", t);
2113 return;
2117 /* Even if some of the member functions are non-private, the class
2118 won't be useful for much if all the constructors or destructors
2119 are private: such an object can never be created or destroyed. */
2120 if (tree dtor = CLASSTYPE_DESTRUCTOR (t))
2121 if (TREE_PRIVATE (dtor))
2123 warning (OPT_Wctor_dtor_privacy,
2124 "%q#T only defines a private destructor and has no friends",
2126 return;
2129 /* Warn about classes that have private constructors and no friends. */
2130 if (TYPE_HAS_USER_CONSTRUCTOR (t)
2131 /* Implicitly generated constructors are always public. */
2132 && !CLASSTYPE_LAZY_DEFAULT_CTOR (t))
2134 bool nonprivate_ctor = false;
2135 tree copy_or_move = NULL_TREE;
2137 /* If a non-template class does not define a copy
2138 constructor, one is defined for it, enabling it to avoid
2139 this warning. For a template class, this does not
2140 happen, and so we would normally get a warning on:
2142 template <class T> class C { private: C(); };
2144 To avoid this asymmetry, we check TYPE_HAS_COPY_CTOR. All
2145 complete non-template or fully instantiated classes have this
2146 flag set. */
2147 if (!TYPE_HAS_COPY_CTOR (t))
2148 nonprivate_ctor = true;
2149 else
2150 for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (t));
2151 !nonprivate_ctor && iter; ++iter)
2152 if (TREE_PRIVATE (*iter))
2153 continue;
2154 else if (copy_fn_p (*iter) || move_fn_p (*iter))
2155 /* Ideally, we wouldn't count any constructor that takes
2156 an argument of the class type as a parameter, because
2157 such things cannot be used to construct an instance of
2158 the class unless you already have one. */
2159 copy_or_move = *iter;
2160 else
2161 nonprivate_ctor = true;
2163 if (!nonprivate_ctor)
2165 warning (OPT_Wctor_dtor_privacy,
2166 "%q#T only defines private constructors and has no friends",
2168 if (copy_or_move)
2169 inform (DECL_SOURCE_LOCATION (copy_or_move),
2170 "%q#D is public, but requires an existing %q#T object",
2171 copy_or_move, t);
2172 return;
2177 /* Make BINFO's vtable have N entries, including RTTI entries,
2178 vbase and vcall offsets, etc. Set its type and call the back end
2179 to lay it out. */
2181 static void
2182 layout_vtable_decl (tree binfo, int n)
2184 tree atype;
2185 tree vtable;
2187 atype = build_array_of_n_type (vtable_entry_type, n);
2188 layout_type (atype);
2190 /* We may have to grow the vtable. */
2191 vtable = get_vtbl_decl_for_binfo (binfo);
2192 if (!same_type_p (TREE_TYPE (vtable), atype))
2194 TREE_TYPE (vtable) = atype;
2195 DECL_SIZE (vtable) = DECL_SIZE_UNIT (vtable) = NULL_TREE;
2196 layout_decl (vtable, 0);
2200 /* True iff FNDECL and BASE_FNDECL (both non-static member functions)
2201 have the same signature. */
2204 same_signature_p (const_tree fndecl, const_tree base_fndecl)
2206 /* One destructor overrides another if they are the same kind of
2207 destructor. */
2208 if (DECL_DESTRUCTOR_P (base_fndecl) && DECL_DESTRUCTOR_P (fndecl)
2209 && special_function_p (base_fndecl) == special_function_p (fndecl))
2210 return 1;
2211 /* But a non-destructor never overrides a destructor, nor vice
2212 versa, nor do different kinds of destructors override
2213 one-another. For example, a complete object destructor does not
2214 override a deleting destructor. */
2215 if (DECL_DESTRUCTOR_P (base_fndecl) || DECL_DESTRUCTOR_P (fndecl))
2216 return 0;
2218 if (DECL_NAME (fndecl) == DECL_NAME (base_fndecl)
2219 || (DECL_CONV_FN_P (fndecl)
2220 && DECL_CONV_FN_P (base_fndecl)
2221 && same_type_p (DECL_CONV_FN_TYPE (fndecl),
2222 DECL_CONV_FN_TYPE (base_fndecl))))
2224 tree fntype = TREE_TYPE (fndecl);
2225 tree base_fntype = TREE_TYPE (base_fndecl);
2226 if (type_memfn_quals (fntype) == type_memfn_quals (base_fntype)
2227 && type_memfn_rqual (fntype) == type_memfn_rqual (base_fntype)
2228 && compparms (FUNCTION_FIRST_USER_PARMTYPE (fndecl),
2229 FUNCTION_FIRST_USER_PARMTYPE (base_fndecl)))
2230 return 1;
2232 return 0;
2235 /* Returns TRUE if DERIVED is a binfo containing the binfo BASE as a
2236 subobject. */
2238 static bool
2239 base_derived_from (tree derived, tree base)
2241 tree probe;
2243 for (probe = base; probe; probe = BINFO_INHERITANCE_CHAIN (probe))
2245 if (probe == derived)
2246 return true;
2247 else if (BINFO_VIRTUAL_P (probe))
2248 /* If we meet a virtual base, we can't follow the inheritance
2249 any more. See if the complete type of DERIVED contains
2250 such a virtual base. */
2251 return (binfo_for_vbase (BINFO_TYPE (probe), BINFO_TYPE (derived))
2252 != NULL_TREE);
2254 return false;
2257 struct find_final_overrider_data {
2258 /* The function for which we are trying to find a final overrider. */
2259 tree fn;
2260 /* The base class in which the function was declared. */
2261 tree declaring_base;
2262 /* The candidate overriders. */
2263 tree candidates;
2264 /* Path to most derived. */
2265 vec<tree> path;
2268 /* Add the overrider along the current path to FFOD->CANDIDATES.
2269 Returns true if an overrider was found; false otherwise. */
2271 static bool
2272 dfs_find_final_overrider_1 (tree binfo,
2273 find_final_overrider_data *ffod,
2274 unsigned depth)
2276 tree method;
2278 /* If BINFO is not the most derived type, try a more derived class.
2279 A definition there will overrider a definition here. */
2280 if (depth)
2282 depth--;
2283 if (dfs_find_final_overrider_1
2284 (ffod->path[depth], ffod, depth))
2285 return true;
2288 method = look_for_overrides_here (BINFO_TYPE (binfo), ffod->fn);
2289 if (method)
2291 tree *candidate = &ffod->candidates;
2293 /* Remove any candidates overridden by this new function. */
2294 while (*candidate)
2296 /* If *CANDIDATE overrides METHOD, then METHOD
2297 cannot override anything else on the list. */
2298 if (base_derived_from (TREE_VALUE (*candidate), binfo))
2299 return true;
2300 /* If METHOD overrides *CANDIDATE, remove *CANDIDATE. */
2301 if (base_derived_from (binfo, TREE_VALUE (*candidate)))
2302 *candidate = TREE_CHAIN (*candidate);
2303 else
2304 candidate = &TREE_CHAIN (*candidate);
2307 /* Add the new function. */
2308 ffod->candidates = tree_cons (method, binfo, ffod->candidates);
2309 return true;
2312 return false;
2315 /* Called from find_final_overrider via dfs_walk. */
2317 static tree
2318 dfs_find_final_overrider_pre (tree binfo, void *data)
2320 find_final_overrider_data *ffod = (find_final_overrider_data *) data;
2322 if (binfo == ffod->declaring_base)
2323 dfs_find_final_overrider_1 (binfo, ffod, ffod->path.length ());
2324 ffod->path.safe_push (binfo);
2326 return NULL_TREE;
2329 static tree
2330 dfs_find_final_overrider_post (tree /*binfo*/, void *data)
2332 find_final_overrider_data *ffod = (find_final_overrider_data *) data;
2333 ffod->path.pop ();
2335 return NULL_TREE;
2338 /* Returns a TREE_LIST whose TREE_PURPOSE is the final overrider for
2339 FN and whose TREE_VALUE is the binfo for the base where the
2340 overriding occurs. BINFO (in the hierarchy dominated by the binfo
2341 DERIVED) is the base object in which FN is declared. */
2343 static tree
2344 find_final_overrider (tree derived, tree binfo, tree fn)
2346 find_final_overrider_data ffod;
2348 /* Getting this right is a little tricky. This is valid:
2350 struct S { virtual void f (); };
2351 struct T { virtual void f (); };
2352 struct U : public S, public T { };
2354 even though calling `f' in `U' is ambiguous. But,
2356 struct R { virtual void f(); };
2357 struct S : virtual public R { virtual void f (); };
2358 struct T : virtual public R { virtual void f (); };
2359 struct U : public S, public T { };
2361 is not -- there's no way to decide whether to put `S::f' or
2362 `T::f' in the vtable for `R'.
2364 The solution is to look at all paths to BINFO. If we find
2365 different overriders along any two, then there is a problem. */
2366 if (DECL_THUNK_P (fn))
2367 fn = THUNK_TARGET (fn);
2369 /* Determine the depth of the hierarchy. */
2370 ffod.fn = fn;
2371 ffod.declaring_base = binfo;
2372 ffod.candidates = NULL_TREE;
2373 ffod.path.create (30);
2375 dfs_walk_all (derived, dfs_find_final_overrider_pre,
2376 dfs_find_final_overrider_post, &ffod);
2378 ffod.path.release ();
2380 /* If there was no winner, issue an error message. */
2381 if (!ffod.candidates || TREE_CHAIN (ffod.candidates))
2382 return error_mark_node;
2384 return ffod.candidates;
2387 /* Return the index of the vcall offset for FN when TYPE is used as a
2388 virtual base. */
2390 static tree
2391 get_vcall_index (tree fn, tree type)
2393 vec<tree_pair_s, va_gc> *indices = CLASSTYPE_VCALL_INDICES (type);
2394 tree_pair_p p;
2395 unsigned ix;
2397 FOR_EACH_VEC_SAFE_ELT (indices, ix, p)
2398 if ((DECL_DESTRUCTOR_P (fn) && DECL_DESTRUCTOR_P (p->purpose))
2399 || same_signature_p (fn, p->purpose))
2400 return p->value;
2402 /* There should always be an appropriate index. */
2403 gcc_unreachable ();
2406 /* Update an entry in the vtable for BINFO, which is in the hierarchy
2407 dominated by T. FN is the old function; VIRTUALS points to the
2408 corresponding position in the new BINFO_VIRTUALS list. IX is the index
2409 of that entry in the list. */
2411 static void
2412 update_vtable_entry_for_fn (tree t, tree binfo, tree fn, tree* virtuals,
2413 unsigned ix)
2415 tree b;
2416 tree overrider;
2417 tree delta;
2418 tree virtual_base;
2419 tree first_defn;
2420 tree overrider_fn, overrider_target;
2421 tree target_fn = DECL_THUNK_P (fn) ? THUNK_TARGET (fn) : fn;
2422 tree over_return, base_return;
2423 bool lost = false;
2425 /* Find the nearest primary base (possibly binfo itself) which defines
2426 this function; this is the class the caller will convert to when
2427 calling FN through BINFO. */
2428 for (b = binfo; ; b = get_primary_binfo (b))
2430 gcc_assert (b);
2431 if (look_for_overrides_here (BINFO_TYPE (b), target_fn))
2432 break;
2434 /* The nearest definition is from a lost primary. */
2435 if (BINFO_LOST_PRIMARY_P (b))
2436 lost = true;
2438 first_defn = b;
2440 /* Find the final overrider. */
2441 overrider = find_final_overrider (TYPE_BINFO (t), b, target_fn);
2442 if (overrider == error_mark_node)
2444 error ("no unique final overrider for %qD in %qT", target_fn, t);
2445 return;
2447 overrider_target = overrider_fn = TREE_PURPOSE (overrider);
2449 /* Check for adjusting covariant return types. */
2450 over_return = TREE_TYPE (TREE_TYPE (overrider_target));
2451 base_return = TREE_TYPE (TREE_TYPE (target_fn));
2453 if (POINTER_TYPE_P (over_return)
2454 && TREE_CODE (over_return) == TREE_CODE (base_return)
2455 && CLASS_TYPE_P (TREE_TYPE (over_return))
2456 && CLASS_TYPE_P (TREE_TYPE (base_return))
2457 /* If the overrider is invalid, don't even try. */
2458 && !DECL_INVALID_OVERRIDER_P (overrider_target))
2460 /* If FN is a covariant thunk, we must figure out the adjustment
2461 to the final base FN was converting to. As OVERRIDER_TARGET might
2462 also be converting to the return type of FN, we have to
2463 combine the two conversions here. */
2464 tree fixed_offset, virtual_offset;
2466 over_return = TREE_TYPE (over_return);
2467 base_return = TREE_TYPE (base_return);
2469 if (DECL_THUNK_P (fn))
2471 gcc_assert (DECL_RESULT_THUNK_P (fn));
2472 fixed_offset = ssize_int (THUNK_FIXED_OFFSET (fn));
2473 virtual_offset = THUNK_VIRTUAL_OFFSET (fn);
2475 else
2476 fixed_offset = virtual_offset = NULL_TREE;
2478 if (virtual_offset)
2479 /* Find the equivalent binfo within the return type of the
2480 overriding function. We will want the vbase offset from
2481 there. */
2482 virtual_offset = binfo_for_vbase (BINFO_TYPE (virtual_offset),
2483 over_return);
2484 else if (!same_type_ignoring_top_level_qualifiers_p
2485 (over_return, base_return))
2487 /* There was no existing virtual thunk (which takes
2488 precedence). So find the binfo of the base function's
2489 return type within the overriding function's return type.
2490 Fortunately we know the covariancy is valid (it
2491 has already been checked), so we can just iterate along
2492 the binfos, which have been chained in inheritance graph
2493 order. Of course it is lame that we have to repeat the
2494 search here anyway -- we should really be caching pieces
2495 of the vtable and avoiding this repeated work. */
2496 tree thunk_binfo, base_binfo;
2498 /* Find the base binfo within the overriding function's
2499 return type. We will always find a thunk_binfo, except
2500 when the covariancy is invalid (which we will have
2501 already diagnosed). */
2502 for (base_binfo = TYPE_BINFO (base_return),
2503 thunk_binfo = TYPE_BINFO (over_return);
2504 thunk_binfo;
2505 thunk_binfo = TREE_CHAIN (thunk_binfo))
2506 if (SAME_BINFO_TYPE_P (BINFO_TYPE (thunk_binfo),
2507 BINFO_TYPE (base_binfo)))
2508 break;
2510 /* See if virtual inheritance is involved. */
2511 for (virtual_offset = thunk_binfo;
2512 virtual_offset;
2513 virtual_offset = BINFO_INHERITANCE_CHAIN (virtual_offset))
2514 if (BINFO_VIRTUAL_P (virtual_offset))
2515 break;
2517 if (virtual_offset
2518 || (thunk_binfo && !BINFO_OFFSET_ZEROP (thunk_binfo)))
2520 tree offset = fold_convert (ssizetype, BINFO_OFFSET (thunk_binfo));
2522 if (virtual_offset)
2524 /* We convert via virtual base. Adjust the fixed
2525 offset to be from there. */
2526 offset =
2527 size_diffop (offset,
2528 fold_convert (ssizetype,
2529 BINFO_OFFSET (virtual_offset)));
2531 if (fixed_offset)
2532 /* There was an existing fixed offset, this must be
2533 from the base just converted to, and the base the
2534 FN was thunking to. */
2535 fixed_offset = size_binop (PLUS_EXPR, fixed_offset, offset);
2536 else
2537 fixed_offset = offset;
2541 if (fixed_offset || virtual_offset)
2542 /* Replace the overriding function with a covariant thunk. We
2543 will emit the overriding function in its own slot as
2544 well. */
2545 overrider_fn = make_thunk (overrider_target, /*this_adjusting=*/0,
2546 fixed_offset, virtual_offset);
2548 else
2549 gcc_assert (DECL_INVALID_OVERRIDER_P (overrider_target) ||
2550 !DECL_THUNK_P (fn));
2552 /* If we need a covariant thunk, then we may need to adjust first_defn.
2553 The ABI specifies that the thunks emitted with a function are
2554 determined by which bases the function overrides, so we need to be
2555 sure that we're using a thunk for some overridden base; even if we
2556 know that the necessary this adjustment is zero, there may not be an
2557 appropriate zero-this-adjustment thunk for us to use since thunks for
2558 overriding virtual bases always use the vcall offset.
2560 Furthermore, just choosing any base that overrides this function isn't
2561 quite right, as this slot won't be used for calls through a type that
2562 puts a covariant thunk here. Calling the function through such a type
2563 will use a different slot, and that slot is the one that determines
2564 the thunk emitted for that base.
2566 So, keep looking until we find the base that we're really overriding
2567 in this slot: the nearest primary base that doesn't use a covariant
2568 thunk in this slot. */
2569 if (overrider_target != overrider_fn)
2571 if (BINFO_TYPE (b) == DECL_CONTEXT (overrider_target))
2572 /* We already know that the overrider needs a covariant thunk. */
2573 b = get_primary_binfo (b);
2574 for (; ; b = get_primary_binfo (b))
2576 tree main_binfo = TYPE_BINFO (BINFO_TYPE (b));
2577 tree bv = chain_index (ix, BINFO_VIRTUALS (main_binfo));
2578 if (!DECL_THUNK_P (TREE_VALUE (bv)))
2579 break;
2580 if (BINFO_LOST_PRIMARY_P (b))
2581 lost = true;
2583 first_defn = b;
2586 /* Assume that we will produce a thunk that convert all the way to
2587 the final overrider, and not to an intermediate virtual base. */
2588 virtual_base = NULL_TREE;
2590 /* See if we can convert to an intermediate virtual base first, and then
2591 use the vcall offset located there to finish the conversion. */
2592 for (; b; b = BINFO_INHERITANCE_CHAIN (b))
2594 /* If we find the final overrider, then we can stop
2595 walking. */
2596 if (SAME_BINFO_TYPE_P (BINFO_TYPE (b),
2597 BINFO_TYPE (TREE_VALUE (overrider))))
2598 break;
2600 /* If we find a virtual base, and we haven't yet found the
2601 overrider, then there is a virtual base between the
2602 declaring base (first_defn) and the final overrider. */
2603 if (BINFO_VIRTUAL_P (b))
2605 virtual_base = b;
2606 break;
2610 /* Compute the constant adjustment to the `this' pointer. The
2611 `this' pointer, when this function is called, will point at BINFO
2612 (or one of its primary bases, which are at the same offset). */
2613 if (virtual_base)
2614 /* The `this' pointer needs to be adjusted from the declaration to
2615 the nearest virtual base. */
2616 delta = size_diffop_loc (input_location,
2617 fold_convert (ssizetype, BINFO_OFFSET (virtual_base)),
2618 fold_convert (ssizetype, BINFO_OFFSET (first_defn)));
2619 else if (lost)
2620 /* If the nearest definition is in a lost primary, we don't need an
2621 entry in our vtable. Except possibly in a constructor vtable,
2622 if we happen to get our primary back. In that case, the offset
2623 will be zero, as it will be a primary base. */
2624 delta = size_zero_node;
2625 else
2626 /* The `this' pointer needs to be adjusted from pointing to
2627 BINFO to pointing at the base where the final overrider
2628 appears. */
2629 delta = size_diffop_loc (input_location,
2630 fold_convert (ssizetype,
2631 BINFO_OFFSET (TREE_VALUE (overrider))),
2632 fold_convert (ssizetype, BINFO_OFFSET (binfo)));
2634 modify_vtable_entry (t, binfo, overrider_fn, delta, virtuals);
2636 if (virtual_base)
2637 BV_VCALL_INDEX (*virtuals)
2638 = get_vcall_index (overrider_target, BINFO_TYPE (virtual_base));
2639 else
2640 BV_VCALL_INDEX (*virtuals) = NULL_TREE;
2642 BV_LOST_PRIMARY (*virtuals) = lost;
2645 /* Called from modify_all_vtables via dfs_walk. */
2647 static tree
2648 dfs_modify_vtables (tree binfo, void* data)
2650 tree t = (tree) data;
2651 tree virtuals;
2652 tree old_virtuals;
2653 unsigned ix;
2655 if (!TYPE_CONTAINS_VPTR_P (BINFO_TYPE (binfo)))
2656 /* A base without a vtable needs no modification, and its bases
2657 are uninteresting. */
2658 return dfs_skip_bases;
2660 if (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), t)
2661 && !CLASSTYPE_HAS_PRIMARY_BASE_P (t))
2662 /* Don't do the primary vtable, if it's new. */
2663 return NULL_TREE;
2665 if (BINFO_PRIMARY_P (binfo) && !BINFO_VIRTUAL_P (binfo))
2666 /* There's no need to modify the vtable for a non-virtual primary
2667 base; we're not going to use that vtable anyhow. We do still
2668 need to do this for virtual primary bases, as they could become
2669 non-primary in a construction vtable. */
2670 return NULL_TREE;
2672 make_new_vtable (t, binfo);
2674 /* Now, go through each of the virtual functions in the virtual
2675 function table for BINFO. Find the final overrider, and update
2676 the BINFO_VIRTUALS list appropriately. */
2677 for (ix = 0, virtuals = BINFO_VIRTUALS (binfo),
2678 old_virtuals = BINFO_VIRTUALS (TYPE_BINFO (BINFO_TYPE (binfo)));
2679 virtuals;
2680 ix++, virtuals = TREE_CHAIN (virtuals),
2681 old_virtuals = TREE_CHAIN (old_virtuals))
2682 update_vtable_entry_for_fn (t,
2683 binfo,
2684 BV_FN (old_virtuals),
2685 &virtuals, ix);
2687 return NULL_TREE;
2690 /* Update all of the primary and secondary vtables for T. Create new
2691 vtables as required, and initialize their RTTI information. Each
2692 of the functions in VIRTUALS is declared in T and may override a
2693 virtual function from a base class; find and modify the appropriate
2694 entries to point to the overriding functions. Returns a list, in
2695 declaration order, of the virtual functions that are declared in T,
2696 but do not appear in the primary base class vtable, and which
2697 should therefore be appended to the end of the vtable for T. */
2699 static tree
2700 modify_all_vtables (tree t, tree virtuals)
2702 tree binfo = TYPE_BINFO (t);
2703 tree *fnsp;
2705 /* Mangle the vtable name before entering dfs_walk (c++/51884). */
2706 if (TYPE_CONTAINS_VPTR_P (t))
2707 get_vtable_decl (t, false);
2709 /* Update all of the vtables. */
2710 dfs_walk_once (binfo, dfs_modify_vtables, NULL, t);
2712 /* Add virtual functions not already in our primary vtable. These
2713 will be both those introduced by this class, and those overridden
2714 from secondary bases. It does not include virtuals merely
2715 inherited from secondary bases. */
2716 for (fnsp = &virtuals; *fnsp; )
2718 tree fn = TREE_VALUE (*fnsp);
2720 if (!value_member (fn, BINFO_VIRTUALS (binfo))
2721 || DECL_VINDEX (fn) == error_mark_node)
2723 /* We don't need to adjust the `this' pointer when
2724 calling this function. */
2725 BV_DELTA (*fnsp) = integer_zero_node;
2726 BV_VCALL_INDEX (*fnsp) = NULL_TREE;
2728 /* This is a function not already in our vtable. Keep it. */
2729 fnsp = &TREE_CHAIN (*fnsp);
2731 else
2732 /* We've already got an entry for this function. Skip it. */
2733 *fnsp = TREE_CHAIN (*fnsp);
2736 return virtuals;
2739 /* Get the base virtual function declarations in T that have the
2740 indicated NAME. */
2742 static void
2743 get_basefndecls (tree name, tree t, vec<tree> *base_fndecls)
2745 bool found_decls = false;
2747 /* Find virtual functions in T with the indicated NAME. */
2748 for (ovl_iterator iter (get_class_binding (t, name)); iter; ++iter)
2750 tree method = *iter;
2752 if (TREE_CODE (method) == FUNCTION_DECL && DECL_VINDEX (method))
2754 base_fndecls->safe_push (method);
2755 found_decls = true;
2759 if (found_decls)
2760 return;
2762 int n_baseclasses = BINFO_N_BASE_BINFOS (TYPE_BINFO (t));
2763 for (int i = 0; i < n_baseclasses; i++)
2765 tree basetype = BINFO_TYPE (BINFO_BASE_BINFO (TYPE_BINFO (t), i));
2766 get_basefndecls (name, basetype, base_fndecls);
2770 /* If this declaration supersedes the declaration of
2771 a method declared virtual in the base class, then
2772 mark this field as being virtual as well. */
2774 void
2775 check_for_override (tree decl, tree ctype)
2777 bool overrides_found = false;
2778 if (TREE_CODE (decl) == TEMPLATE_DECL)
2779 /* In [temp.mem] we have:
2781 A specialization of a member function template does not
2782 override a virtual function from a base class. */
2783 return;
2784 if ((DECL_DESTRUCTOR_P (decl)
2785 || IDENTIFIER_VIRTUAL_P (DECL_NAME (decl))
2786 || DECL_CONV_FN_P (decl))
2787 && look_for_overrides (ctype, decl)
2788 && !DECL_STATIC_FUNCTION_P (decl))
2789 /* Set DECL_VINDEX to a value that is neither an INTEGER_CST nor
2790 the error_mark_node so that we know it is an overriding
2791 function. */
2793 DECL_VINDEX (decl) = decl;
2794 overrides_found = true;
2795 if (warn_override && !DECL_OVERRIDE_P (decl)
2796 && !DECL_DESTRUCTOR_P (decl))
2797 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wsuggest_override,
2798 "%qD can be marked override", decl);
2801 if (DECL_VIRTUAL_P (decl))
2803 if (!DECL_VINDEX (decl))
2804 DECL_VINDEX (decl) = error_mark_node;
2805 IDENTIFIER_VIRTUAL_P (DECL_NAME (decl)) = 1;
2806 if (DECL_DESTRUCTOR_P (decl))
2807 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (ctype) = true;
2809 else if (DECL_FINAL_P (decl))
2810 error ("%q+#D marked %<final%>, but is not virtual", decl);
2811 if (DECL_OVERRIDE_P (decl) && !overrides_found)
2812 error ("%q+#D marked %<override%>, but does not override", decl);
2815 /* Warn about hidden virtual functions that are not overridden in t.
2816 We know that constructors and destructors don't apply. */
2818 static void
2819 warn_hidden (tree t)
2821 if (vec<tree, va_gc> *member_vec = CLASSTYPE_MEMBER_VEC (t))
2822 for (unsigned ix = member_vec->length (); ix--;)
2824 tree fns = (*member_vec)[ix];
2826 if (!OVL_P (fns))
2827 continue;
2829 tree name = OVL_NAME (fns);
2830 auto_vec<tree, 20> base_fndecls;
2831 tree base_binfo;
2832 tree binfo;
2833 unsigned j;
2835 /* Iterate through all of the base classes looking for possibly
2836 hidden functions. */
2837 for (binfo = TYPE_BINFO (t), j = 0;
2838 BINFO_BASE_ITERATE (binfo, j, base_binfo); j++)
2840 tree basetype = BINFO_TYPE (base_binfo);
2841 get_basefndecls (name, basetype, &base_fndecls);
2844 /* If there are no functions to hide, continue. */
2845 if (base_fndecls.is_empty ())
2846 continue;
2848 /* Remove any overridden functions. */
2849 for (ovl_iterator iter (fns); iter; ++iter)
2851 tree fndecl = *iter;
2852 if (TREE_CODE (fndecl) == FUNCTION_DECL
2853 && DECL_VINDEX (fndecl))
2855 /* If the method from the base class has the same
2856 signature as the method from the derived class, it
2857 has been overridden. */
2858 for (size_t k = 0; k < base_fndecls.length (); k++)
2859 if (base_fndecls[k]
2860 && same_signature_p (fndecl, base_fndecls[k]))
2861 base_fndecls[k] = NULL_TREE;
2865 /* Now give a warning for all base functions without overriders,
2866 as they are hidden. */
2867 tree base_fndecl;
2868 FOR_EACH_VEC_ELT (base_fndecls, j, base_fndecl)
2869 if (base_fndecl)
2871 /* Here we know it is a hider, and no overrider exists. */
2872 warning_at (location_of (base_fndecl),
2873 OPT_Woverloaded_virtual,
2874 "%qD was hidden", base_fndecl);
2875 warning_at (location_of (fns),
2876 OPT_Woverloaded_virtual, " by %qD", fns);
2881 /* Recursive helper for finish_struct_anon. */
2883 static void
2884 finish_struct_anon_r (tree field, bool complain)
2886 bool is_union = TREE_CODE (TREE_TYPE (field)) == UNION_TYPE;
2887 tree elt = TYPE_FIELDS (TREE_TYPE (field));
2888 for (; elt; elt = DECL_CHAIN (elt))
2890 /* We're generally only interested in entities the user
2891 declared, but we also find nested classes by noticing
2892 the TYPE_DECL that we create implicitly. You're
2893 allowed to put one anonymous union inside another,
2894 though, so we explicitly tolerate that. We use
2895 TYPE_UNNAMED_P rather than ANON_AGGR_TYPE_P so that
2896 we also allow unnamed types used for defining fields. */
2897 if (DECL_ARTIFICIAL (elt)
2898 && (!DECL_IMPLICIT_TYPEDEF_P (elt)
2899 || TYPE_UNNAMED_P (TREE_TYPE (elt))))
2900 continue;
2902 if (TREE_CODE (elt) != FIELD_DECL)
2904 /* We already complained about static data members in
2905 finish_static_data_member_decl. */
2906 if (complain && !VAR_P (elt))
2908 if (is_union)
2909 permerror (DECL_SOURCE_LOCATION (elt),
2910 "%q#D invalid; an anonymous union can "
2911 "only have non-static data members", elt);
2912 else
2913 permerror (DECL_SOURCE_LOCATION (elt),
2914 "%q#D invalid; an anonymous struct can "
2915 "only have non-static data members", elt);
2917 continue;
2920 if (complain)
2922 if (TREE_PRIVATE (elt))
2924 if (is_union)
2925 permerror (DECL_SOURCE_LOCATION (elt),
2926 "private member %q#D in anonymous union", elt);
2927 else
2928 permerror (DECL_SOURCE_LOCATION (elt),
2929 "private member %q#D in anonymous struct", elt);
2931 else if (TREE_PROTECTED (elt))
2933 if (is_union)
2934 permerror (DECL_SOURCE_LOCATION (elt),
2935 "protected member %q#D in anonymous union", elt);
2936 else
2937 permerror (DECL_SOURCE_LOCATION (elt),
2938 "protected member %q#D in anonymous struct", elt);
2942 TREE_PRIVATE (elt) = TREE_PRIVATE (field);
2943 TREE_PROTECTED (elt) = TREE_PROTECTED (field);
2945 /* Recurse into the anonymous aggregates to handle correctly
2946 access control (c++/24926):
2948 class A {
2949 union {
2950 union {
2951 int i;
2956 int j=A().i; */
2957 if (DECL_NAME (elt) == NULL_TREE
2958 && ANON_AGGR_TYPE_P (TREE_TYPE (elt)))
2959 finish_struct_anon_r (elt, /*complain=*/false);
2963 /* Check for things that are invalid. There are probably plenty of other
2964 things we should check for also. */
2966 static void
2967 finish_struct_anon (tree t)
2969 for (tree field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
2971 if (TREE_STATIC (field))
2972 continue;
2973 if (TREE_CODE (field) != FIELD_DECL)
2974 continue;
2976 if (DECL_NAME (field) == NULL_TREE
2977 && ANON_AGGR_TYPE_P (TREE_TYPE (field)))
2978 finish_struct_anon_r (field, /*complain=*/true);
2982 /* Add T to CLASSTYPE_DECL_LIST of current_class_type which
2983 will be used later during class template instantiation.
2984 When FRIEND_P is zero, T can be a static member data (VAR_DECL),
2985 a non-static member data (FIELD_DECL), a member function
2986 (FUNCTION_DECL), a nested type (RECORD_TYPE, ENUM_TYPE),
2987 a typedef (TYPE_DECL) or a member class template (TEMPLATE_DECL)
2988 When FRIEND_P is nonzero, T is either a friend class
2989 (RECORD_TYPE, TEMPLATE_DECL) or a friend function
2990 (FUNCTION_DECL, TEMPLATE_DECL). */
2992 void
2993 maybe_add_class_template_decl_list (tree type, tree t, int friend_p)
2995 /* Save some memory by not creating TREE_LIST if TYPE is not template. */
2996 if (CLASSTYPE_TEMPLATE_INFO (type))
2997 CLASSTYPE_DECL_LIST (type)
2998 = tree_cons (friend_p ? NULL_TREE : type,
2999 t, CLASSTYPE_DECL_LIST (type));
3002 /* This function is called from declare_virt_assop_and_dtor via
3003 dfs_walk_all.
3005 DATA is a type that direcly or indirectly inherits the base
3006 represented by BINFO. If BINFO contains a virtual assignment [copy
3007 assignment or move assigment] operator or a virtual constructor,
3008 declare that function in DATA if it hasn't been already declared. */
3010 static tree
3011 dfs_declare_virt_assop_and_dtor (tree binfo, void *data)
3013 tree bv, fn, t = (tree)data;
3014 tree opname = cp_assignment_operator_id (NOP_EXPR);
3016 gcc_assert (t && CLASS_TYPE_P (t));
3017 gcc_assert (binfo && TREE_CODE (binfo) == TREE_BINFO);
3019 if (!TYPE_CONTAINS_VPTR_P (BINFO_TYPE (binfo)))
3020 /* A base without a vtable needs no modification, and its bases
3021 are uninteresting. */
3022 return dfs_skip_bases;
3024 if (BINFO_PRIMARY_P (binfo))
3025 /* If this is a primary base, then we have already looked at the
3026 virtual functions of its vtable. */
3027 return NULL_TREE;
3029 for (bv = BINFO_VIRTUALS (binfo); bv; bv = TREE_CHAIN (bv))
3031 fn = BV_FN (bv);
3033 if (DECL_NAME (fn) == opname)
3035 if (CLASSTYPE_LAZY_COPY_ASSIGN (t))
3036 lazily_declare_fn (sfk_copy_assignment, t);
3037 if (CLASSTYPE_LAZY_MOVE_ASSIGN (t))
3038 lazily_declare_fn (sfk_move_assignment, t);
3040 else if (DECL_DESTRUCTOR_P (fn)
3041 && CLASSTYPE_LAZY_DESTRUCTOR (t))
3042 lazily_declare_fn (sfk_destructor, t);
3045 return NULL_TREE;
3048 /* If the class type T has a direct or indirect base that contains a
3049 virtual assignment operator or a virtual destructor, declare that
3050 function in T if it hasn't been already declared. */
3052 static void
3053 declare_virt_assop_and_dtor (tree t)
3055 if (!(TYPE_POLYMORPHIC_P (t)
3056 && (CLASSTYPE_LAZY_COPY_ASSIGN (t)
3057 || CLASSTYPE_LAZY_MOVE_ASSIGN (t)
3058 || CLASSTYPE_LAZY_DESTRUCTOR (t))))
3059 return;
3061 dfs_walk_all (TYPE_BINFO (t),
3062 dfs_declare_virt_assop_and_dtor,
3063 NULL, t);
3066 /* Declare the inheriting constructor for class T inherited from base
3067 constructor CTOR with the parameter array PARMS of size NPARMS. */
3069 static void
3070 one_inheriting_sig (tree t, tree ctor, tree *parms, int nparms)
3072 gcc_assert (TYPE_MAIN_VARIANT (t) == t);
3074 /* We don't declare an inheriting ctor that would be a default,
3075 copy or move ctor for derived or base. */
3076 if (nparms == 0)
3077 return;
3078 if (nparms == 1
3079 && TREE_CODE (parms[0]) == REFERENCE_TYPE)
3081 tree parm = TYPE_MAIN_VARIANT (TREE_TYPE (parms[0]));
3082 if (parm == t || parm == DECL_CONTEXT (ctor))
3083 return;
3086 tree parmlist = void_list_node;
3087 for (int i = nparms - 1; i >= 0; i--)
3088 parmlist = tree_cons (NULL_TREE, parms[i], parmlist);
3089 tree fn = implicitly_declare_fn (sfk_inheriting_constructor,
3090 t, false, ctor, parmlist);
3092 if (add_method (t, fn, false))
3094 DECL_CHAIN (fn) = TYPE_FIELDS (t);
3095 TYPE_FIELDS (t) = fn;
3099 /* Declare all the inheriting constructors for class T inherited from base
3100 constructor CTOR. */
3102 static void
3103 one_inherited_ctor (tree ctor, tree t, tree using_decl)
3105 tree parms = FUNCTION_FIRST_USER_PARMTYPE (ctor);
3107 if (flag_new_inheriting_ctors)
3109 ctor = implicitly_declare_fn (sfk_inheriting_constructor,
3110 t, /*const*/false, ctor, parms);
3111 add_method (t, ctor, using_decl != NULL_TREE);
3112 TYPE_HAS_USER_CONSTRUCTOR (t) = true;
3113 return;
3116 tree *new_parms = XALLOCAVEC (tree, list_length (parms));
3117 int i = 0;
3118 for (; parms && parms != void_list_node; parms = TREE_CHAIN (parms))
3120 if (TREE_PURPOSE (parms))
3121 one_inheriting_sig (t, ctor, new_parms, i);
3122 new_parms[i++] = TREE_VALUE (parms);
3124 one_inheriting_sig (t, ctor, new_parms, i);
3125 if (parms == NULL_TREE)
3127 if (warning (OPT_Winherited_variadic_ctor,
3128 "the ellipsis in %qD is not inherited", ctor))
3129 inform (DECL_SOURCE_LOCATION (ctor), "%qD declared here", ctor);
3133 /* Create default constructors, assignment operators, and so forth for
3134 the type indicated by T, if they are needed. CANT_HAVE_CONST_CTOR,
3135 and CANT_HAVE_CONST_ASSIGNMENT are nonzero if, for whatever reason,
3136 the class cannot have a default constructor, copy constructor
3137 taking a const reference argument, or an assignment operator taking
3138 a const reference, respectively. */
3140 static void
3141 add_implicitly_declared_members (tree t, tree* access_decls,
3142 int cant_have_const_cctor,
3143 int cant_have_const_assignment)
3145 /* Destructor. */
3146 if (!CLASSTYPE_DESTRUCTOR (t))
3147 /* In general, we create destructors lazily. */
3148 CLASSTYPE_LAZY_DESTRUCTOR (t) = 1;
3150 bool move_ok = false;
3151 if (cxx_dialect >= cxx11 && CLASSTYPE_LAZY_DESTRUCTOR (t)
3152 && !TYPE_HAS_COPY_CTOR (t) && !TYPE_HAS_COPY_ASSIGN (t)
3153 && !classtype_has_move_assign_or_move_ctor_p (t, false))
3154 move_ok = true;
3156 /* [class.ctor]
3158 If there is no user-declared constructor for a class, a default
3159 constructor is implicitly declared. */
3160 if (! TYPE_HAS_USER_CONSTRUCTOR (t))
3162 TYPE_HAS_DEFAULT_CONSTRUCTOR (t) = 1;
3163 CLASSTYPE_LAZY_DEFAULT_CTOR (t) = 1;
3164 if (cxx_dialect >= cxx11)
3165 TYPE_HAS_CONSTEXPR_CTOR (t)
3166 /* Don't force the declaration to get a hard answer; if the
3167 definition would have made the class non-literal, it will still be
3168 non-literal because of the base or member in question, and that
3169 gives a better diagnostic. */
3170 = type_maybe_constexpr_default_constructor (t);
3173 /* [class.ctor]
3175 If a class definition does not explicitly declare a copy
3176 constructor, one is declared implicitly. */
3177 if (! TYPE_HAS_COPY_CTOR (t))
3179 TYPE_HAS_COPY_CTOR (t) = 1;
3180 TYPE_HAS_CONST_COPY_CTOR (t) = !cant_have_const_cctor;
3181 CLASSTYPE_LAZY_COPY_CTOR (t) = 1;
3182 if (move_ok)
3183 CLASSTYPE_LAZY_MOVE_CTOR (t) = 1;
3186 /* If there is no assignment operator, one will be created if and
3187 when it is needed. For now, just record whether or not the type
3188 of the parameter to the assignment operator will be a const or
3189 non-const reference. */
3190 if (!TYPE_HAS_COPY_ASSIGN (t))
3192 TYPE_HAS_COPY_ASSIGN (t) = 1;
3193 TYPE_HAS_CONST_COPY_ASSIGN (t) = !cant_have_const_assignment;
3194 CLASSTYPE_LAZY_COPY_ASSIGN (t) = 1;
3195 if (move_ok && !LAMBDA_TYPE_P (t))
3196 CLASSTYPE_LAZY_MOVE_ASSIGN (t) = 1;
3199 /* We can't be lazy about declaring functions that might override
3200 a virtual function from a base class. */
3201 declare_virt_assop_and_dtor (t);
3203 while (*access_decls)
3205 tree using_decl = TREE_VALUE (*access_decls);
3206 tree decl = USING_DECL_DECLS (using_decl);
3207 if (DECL_NAME (using_decl) == ctor_identifier)
3209 /* declare, then remove the decl */
3210 tree ctor_list = decl;
3211 location_t loc = input_location;
3212 input_location = DECL_SOURCE_LOCATION (using_decl);
3213 for (ovl_iterator iter (ctor_list); iter; ++iter)
3214 one_inherited_ctor (*iter, t, using_decl);
3215 *access_decls = TREE_CHAIN (*access_decls);
3216 input_location = loc;
3218 else
3219 access_decls = &TREE_CHAIN (*access_decls);
3223 /* FIELD is a bit-field. We are finishing the processing for its
3224 enclosing type. Issue any appropriate messages and set appropriate
3225 flags. Returns false if an error has been diagnosed. */
3227 static bool
3228 check_bitfield_decl (tree field)
3230 tree type = TREE_TYPE (field);
3231 tree w;
3233 /* Extract the declared width of the bitfield, which has been
3234 temporarily stashed in DECL_BIT_FIELD_REPRESENTATIVE by grokbitfield. */
3235 w = DECL_BIT_FIELD_REPRESENTATIVE (field);
3236 gcc_assert (w != NULL_TREE);
3237 /* Remove the bit-field width indicator so that the rest of the
3238 compiler does not treat that value as a qualifier. */
3239 DECL_BIT_FIELD_REPRESENTATIVE (field) = NULL_TREE;
3241 /* Detect invalid bit-field type. */
3242 if (!INTEGRAL_OR_ENUMERATION_TYPE_P (type))
3244 error ("bit-field %q+#D with non-integral type", field);
3245 w = error_mark_node;
3247 else
3249 location_t loc = input_location;
3250 /* Avoid the non_lvalue wrapper added by fold for PLUS_EXPRs. */
3251 STRIP_NOPS (w);
3253 /* detect invalid field size. */
3254 input_location = DECL_SOURCE_LOCATION (field);
3255 w = cxx_constant_value (w);
3256 input_location = loc;
3258 if (TREE_CODE (w) != INTEGER_CST)
3260 error ("bit-field %q+D width not an integer constant", field);
3261 w = error_mark_node;
3263 else if (tree_int_cst_sgn (w) < 0)
3265 error ("negative width in bit-field %q+D", field);
3266 w = error_mark_node;
3268 else if (integer_zerop (w) && DECL_NAME (field) != 0)
3270 error ("zero width for bit-field %q+D", field);
3271 w = error_mark_node;
3273 else if ((TREE_CODE (type) != ENUMERAL_TYPE
3274 && TREE_CODE (type) != BOOLEAN_TYPE
3275 && compare_tree_int (w, TYPE_PRECISION (type)) > 0)
3276 || ((TREE_CODE (type) == ENUMERAL_TYPE
3277 || TREE_CODE (type) == BOOLEAN_TYPE)
3278 && tree_int_cst_lt (TYPE_SIZE (type), w)))
3279 warning_at (DECL_SOURCE_LOCATION (field), 0,
3280 "width of %qD exceeds its type", field);
3281 else if (TREE_CODE (type) == ENUMERAL_TYPE
3282 && (0 > (compare_tree_int
3283 (w, TYPE_PRECISION (ENUM_UNDERLYING_TYPE (type))))))
3284 warning_at (DECL_SOURCE_LOCATION (field), 0,
3285 "%qD is too small to hold all values of %q#T",
3286 field, type);
3289 if (w != error_mark_node)
3291 DECL_SIZE (field) = fold_convert (bitsizetype, w);
3292 DECL_BIT_FIELD (field) = 1;
3293 return true;
3295 else
3297 /* Non-bit-fields are aligned for their type. */
3298 DECL_BIT_FIELD (field) = 0;
3299 CLEAR_DECL_C_BIT_FIELD (field);
3300 return false;
3304 /* FIELD is a non bit-field. We are finishing the processing for its
3305 enclosing type T. Issue any appropriate messages and set appropriate
3306 flags. */
3308 static bool
3309 check_field_decl (tree field,
3310 tree t,
3311 int* cant_have_const_ctor,
3312 int* no_const_asn_ref)
3314 tree type = strip_array_types (TREE_TYPE (field));
3315 bool any_default_members = false;
3317 /* In C++98 an anonymous union cannot contain any fields which would change
3318 the settings of CANT_HAVE_CONST_CTOR and friends. */
3319 if (ANON_UNION_TYPE_P (type) && cxx_dialect < cxx11)
3321 /* And, we don't set TYPE_HAS_CONST_COPY_CTOR, etc., for anonymous
3322 structs. So, we recurse through their fields here. */
3323 else if (ANON_AGGR_TYPE_P (type))
3325 for (tree fields = TYPE_FIELDS (type); fields;
3326 fields = DECL_CHAIN (fields))
3327 if (TREE_CODE (fields) == FIELD_DECL)
3328 any_default_members |= check_field_decl (fields, t,
3329 cant_have_const_ctor,
3330 no_const_asn_ref);
3332 /* Check members with class type for constructors, destructors,
3333 etc. */
3334 else if (CLASS_TYPE_P (type))
3336 /* Never let anything with uninheritable virtuals
3337 make it through without complaint. */
3338 abstract_virtuals_error (field, type);
3340 if (TREE_CODE (t) == UNION_TYPE && cxx_dialect < cxx11)
3342 static bool warned;
3343 int oldcount = errorcount;
3344 if (TYPE_NEEDS_CONSTRUCTING (type))
3345 error ("member %q+#D with constructor not allowed in union",
3346 field);
3347 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
3348 error ("member %q+#D with destructor not allowed in union", field);
3349 if (TYPE_HAS_COMPLEX_COPY_ASSIGN (type))
3350 error ("member %q+#D with copy assignment operator not allowed in union",
3351 field);
3352 if (!warned && errorcount > oldcount)
3354 inform (DECL_SOURCE_LOCATION (field), "unrestricted unions "
3355 "only available with -std=c++11 or -std=gnu++11");
3356 warned = true;
3359 else
3361 TYPE_NEEDS_CONSTRUCTING (t) |= TYPE_NEEDS_CONSTRUCTING (type);
3362 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t)
3363 |= TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type);
3364 TYPE_HAS_COMPLEX_COPY_ASSIGN (t)
3365 |= (TYPE_HAS_COMPLEX_COPY_ASSIGN (type)
3366 || !TYPE_HAS_COPY_ASSIGN (type));
3367 TYPE_HAS_COMPLEX_COPY_CTOR (t) |= (TYPE_HAS_COMPLEX_COPY_CTOR (type)
3368 || !TYPE_HAS_COPY_CTOR (type));
3369 TYPE_HAS_COMPLEX_MOVE_ASSIGN (t) |= TYPE_HAS_COMPLEX_MOVE_ASSIGN (type);
3370 TYPE_HAS_COMPLEX_MOVE_CTOR (t) |= TYPE_HAS_COMPLEX_MOVE_CTOR (type);
3371 TYPE_HAS_COMPLEX_DFLT (t) |= (!TYPE_HAS_DEFAULT_CONSTRUCTOR (type)
3372 || TYPE_HAS_COMPLEX_DFLT (type));
3375 if (TYPE_HAS_COPY_CTOR (type)
3376 && !TYPE_HAS_CONST_COPY_CTOR (type))
3377 *cant_have_const_ctor = 1;
3379 if (TYPE_HAS_COPY_ASSIGN (type)
3380 && !TYPE_HAS_CONST_COPY_ASSIGN (type))
3381 *no_const_asn_ref = 1;
3384 check_abi_tags (t, field);
3386 if (DECL_INITIAL (field) != NULL_TREE)
3387 /* `build_class_init_list' does not recognize
3388 non-FIELD_DECLs. */
3389 any_default_members = true;
3391 return any_default_members;
3394 /* Check the data members (both static and non-static), class-scoped
3395 typedefs, etc., appearing in the declaration of T. Issue
3396 appropriate diagnostics. Sets ACCESS_DECLS to a list (in
3397 declaration order) of access declarations; each TREE_VALUE in this
3398 list is a USING_DECL.
3400 In addition, set the following flags:
3402 EMPTY_P
3403 The class is empty, i.e., contains no non-static data members.
3405 CANT_HAVE_CONST_CTOR_P
3406 This class cannot have an implicitly generated copy constructor
3407 taking a const reference.
3409 CANT_HAVE_CONST_ASN_REF
3410 This class cannot have an implicitly generated assignment
3411 operator taking a const reference.
3413 All of these flags should be initialized before calling this
3414 function.
3416 Returns a pointer to the end of the TYPE_FIELDs chain; additional
3417 fields can be added by adding to this chain. */
3419 static void
3420 check_field_decls (tree t, tree *access_decls,
3421 int *cant_have_const_ctor_p,
3422 int *no_const_asn_ref_p)
3424 tree *field;
3425 tree *next;
3426 bool has_pointers;
3427 bool any_default_members;
3428 int cant_pack = 0;
3429 int field_access = -1;
3431 /* Assume there are no access declarations. */
3432 *access_decls = NULL_TREE;
3433 /* Assume this class has no pointer members. */
3434 has_pointers = false;
3435 /* Assume none of the members of this class have default
3436 initializations. */
3437 any_default_members = false;
3439 for (field = &TYPE_FIELDS (t); *field; field = next)
3441 tree x = *field;
3442 tree type = TREE_TYPE (x);
3443 int this_field_access;
3445 next = &DECL_CHAIN (x);
3447 if (TREE_CODE (x) == USING_DECL)
3449 /* Save the access declarations for our caller. */
3450 *access_decls = tree_cons (NULL_TREE, x, *access_decls);
3451 continue;
3454 if (TREE_CODE (x) == TYPE_DECL
3455 || TREE_CODE (x) == TEMPLATE_DECL)
3456 continue;
3458 if (TREE_CODE (x) == FUNCTION_DECL)
3459 /* FIXME: We should fold in the checking from check_methods. */
3460 continue;
3462 /* If we've gotten this far, it's a data member, possibly static,
3463 or an enumerator. */
3464 if (TREE_CODE (x) != CONST_DECL)
3465 DECL_CONTEXT (x) = t;
3467 /* When this goes into scope, it will be a non-local reference. */
3468 DECL_NONLOCAL (x) = 1;
3470 if (TREE_CODE (t) == UNION_TYPE)
3472 /* [class.union] (C++98)
3474 If a union contains a static data member, or a member of
3475 reference type, the program is ill-formed.
3477 In C++11 [class.union] says:
3478 If a union contains a non-static data member of reference type
3479 the program is ill-formed. */
3480 if (VAR_P (x) && cxx_dialect < cxx11)
3482 error ("in C++98 %q+D may not be static because it is "
3483 "a member of a union", x);
3484 continue;
3486 if (TREE_CODE (type) == REFERENCE_TYPE
3487 && TREE_CODE (x) == FIELD_DECL)
3489 error ("non-static data member %q+D in a union may not "
3490 "have reference type %qT", x, type);
3491 continue;
3495 /* Perform error checking that did not get done in
3496 grokdeclarator. */
3497 if (TREE_CODE (type) == FUNCTION_TYPE)
3499 error ("field %q+D invalidly declared function type", x);
3500 type = build_pointer_type (type);
3501 TREE_TYPE (x) = type;
3503 else if (TREE_CODE (type) == METHOD_TYPE)
3505 error ("field %q+D invalidly declared method type", x);
3506 type = build_pointer_type (type);
3507 TREE_TYPE (x) = type;
3510 if (type == error_mark_node)
3511 continue;
3513 if (TREE_CODE (x) == CONST_DECL || VAR_P (x))
3514 continue;
3516 /* Now it can only be a FIELD_DECL. */
3518 if (TREE_PRIVATE (x) || TREE_PROTECTED (x))
3519 CLASSTYPE_NON_AGGREGATE (t) = 1;
3521 /* If at least one non-static data member is non-literal, the whole
3522 class becomes non-literal. Per Core/1453, volatile non-static
3523 data members and base classes are also not allowed.
3524 Note: if the type is incomplete we will complain later on. */
3525 if (COMPLETE_TYPE_P (type)
3526 && (!literal_type_p (type) || CP_TYPE_VOLATILE_P (type)))
3527 CLASSTYPE_LITERAL_P (t) = false;
3529 /* A standard-layout class is a class that:
3531 has the same access control (Clause 11) for all non-static data members,
3532 ... */
3533 this_field_access = TREE_PROTECTED (x) ? 1 : TREE_PRIVATE (x) ? 2 : 0;
3534 if (field_access == -1)
3535 field_access = this_field_access;
3536 else if (this_field_access != field_access)
3537 CLASSTYPE_NON_STD_LAYOUT (t) = 1;
3539 /* If this is of reference type, check if it needs an init. */
3540 if (TREE_CODE (type) == REFERENCE_TYPE)
3542 CLASSTYPE_NON_LAYOUT_POD_P (t) = 1;
3543 CLASSTYPE_NON_STD_LAYOUT (t) = 1;
3544 if (DECL_INITIAL (x) == NULL_TREE)
3545 SET_CLASSTYPE_REF_FIELDS_NEED_INIT (t, 1);
3546 if (cxx_dialect < cxx11)
3548 /* ARM $12.6.2: [A member initializer list] (or, for an
3549 aggregate, initialization by a brace-enclosed list) is the
3550 only way to initialize nonstatic const and reference
3551 members. */
3552 TYPE_HAS_COMPLEX_COPY_ASSIGN (t) = 1;
3553 TYPE_HAS_COMPLEX_MOVE_ASSIGN (t) = 1;
3557 type = strip_array_types (type);
3559 if (TYPE_PACKED (t))
3561 if (!layout_pod_type_p (type) && !TYPE_PACKED (type))
3563 warning_at
3564 (DECL_SOURCE_LOCATION (x), 0,
3565 "ignoring packed attribute because of unpacked non-POD field %q#D",
3567 cant_pack = 1;
3569 else if (DECL_C_BIT_FIELD (x)
3570 || TYPE_ALIGN (TREE_TYPE (x)) > BITS_PER_UNIT)
3571 DECL_PACKED (x) = 1;
3574 if (DECL_C_BIT_FIELD (x)
3575 && integer_zerop (DECL_BIT_FIELD_REPRESENTATIVE (x)))
3576 /* We don't treat zero-width bitfields as making a class
3577 non-empty. */
3579 else
3581 /* The class is non-empty. */
3582 CLASSTYPE_EMPTY_P (t) = 0;
3583 /* The class is not even nearly empty. */
3584 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
3585 /* If one of the data members contains an empty class,
3586 so does T. */
3587 if (CLASS_TYPE_P (type)
3588 && CLASSTYPE_CONTAINS_EMPTY_CLASS_P (type))
3589 CLASSTYPE_CONTAINS_EMPTY_CLASS_P (t) = 1;
3592 /* This is used by -Weffc++ (see below). Warn only for pointers
3593 to members which might hold dynamic memory. So do not warn
3594 for pointers to functions or pointers to members. */
3595 if (TYPE_PTR_P (type)
3596 && !TYPE_PTRFN_P (type))
3597 has_pointers = true;
3599 if (CLASS_TYPE_P (type))
3601 if (CLASSTYPE_REF_FIELDS_NEED_INIT (type))
3602 SET_CLASSTYPE_REF_FIELDS_NEED_INIT (t, 1);
3603 if (CLASSTYPE_READONLY_FIELDS_NEED_INIT (type))
3604 SET_CLASSTYPE_READONLY_FIELDS_NEED_INIT (t, 1);
3607 if (DECL_MUTABLE_P (x) || TYPE_HAS_MUTABLE_P (type))
3608 CLASSTYPE_HAS_MUTABLE (t) = 1;
3610 if (DECL_MUTABLE_P (x))
3612 if (CP_TYPE_CONST_P (type))
3614 error ("member %q+D cannot be declared both %<const%> "
3615 "and %<mutable%>", x);
3616 continue;
3618 if (TREE_CODE (type) == REFERENCE_TYPE)
3620 error ("member %q+D cannot be declared as a %<mutable%> "
3621 "reference", x);
3622 continue;
3626 if (! layout_pod_type_p (type))
3627 /* DR 148 now allows pointers to members (which are POD themselves),
3628 to be allowed in POD structs. */
3629 CLASSTYPE_NON_LAYOUT_POD_P (t) = 1;
3631 if (!std_layout_type_p (type))
3632 CLASSTYPE_NON_STD_LAYOUT (t) = 1;
3634 if (! zero_init_p (type))
3635 CLASSTYPE_NON_ZERO_INIT_P (t) = 1;
3637 /* We set DECL_C_BIT_FIELD in grokbitfield.
3638 If the type and width are valid, we'll also set DECL_BIT_FIELD. */
3639 if (DECL_C_BIT_FIELD (x))
3640 check_bitfield_decl (x);
3642 if (check_field_decl (x, t, cant_have_const_ctor_p, no_const_asn_ref_p))
3644 if (any_default_members
3645 && TREE_CODE (t) == UNION_TYPE)
3646 error ("multiple fields in union %qT initialized", t);
3647 any_default_members = true;
3650 /* Now that we've removed bit-field widths from DECL_INITIAL,
3651 anything left in DECL_INITIAL is an NSDMI that makes the class
3652 non-aggregate in C++11. */
3653 if (DECL_INITIAL (x) && cxx_dialect < cxx14)
3654 CLASSTYPE_NON_AGGREGATE (t) = true;
3656 /* If any field is const, the structure type is pseudo-const. */
3657 if (CP_TYPE_CONST_P (type))
3659 C_TYPE_FIELDS_READONLY (t) = 1;
3660 if (DECL_INITIAL (x) == NULL_TREE)
3661 SET_CLASSTYPE_READONLY_FIELDS_NEED_INIT (t, 1);
3662 if (cxx_dialect < cxx11)
3664 /* ARM $12.6.2: [A member initializer list] (or, for an
3665 aggregate, initialization by a brace-enclosed list) is the
3666 only way to initialize nonstatic const and reference
3667 members. */
3668 TYPE_HAS_COMPLEX_COPY_ASSIGN (t) = 1;
3669 TYPE_HAS_COMPLEX_MOVE_ASSIGN (t) = 1;
3672 /* A field that is pseudo-const makes the structure likewise. */
3673 else if (CLASS_TYPE_P (type))
3675 C_TYPE_FIELDS_READONLY (t) |= C_TYPE_FIELDS_READONLY (type);
3676 SET_CLASSTYPE_READONLY_FIELDS_NEED_INIT (t,
3677 CLASSTYPE_READONLY_FIELDS_NEED_INIT (t)
3678 | CLASSTYPE_READONLY_FIELDS_NEED_INIT (type));
3681 /* Core issue 80: A nonstatic data member is required to have a
3682 different name from the class iff the class has a
3683 user-declared constructor. */
3684 if (constructor_name_p (DECL_NAME (x), t)
3685 && TYPE_HAS_USER_CONSTRUCTOR (t))
3686 permerror (DECL_SOURCE_LOCATION (x),
3687 "field %q#D with same name as class", x);
3690 /* Effective C++ rule 11: if a class has dynamic memory held by pointers,
3691 it should also define a copy constructor and an assignment operator to
3692 implement the correct copy semantic (deep vs shallow, etc.). As it is
3693 not feasible to check whether the constructors do allocate dynamic memory
3694 and store it within members, we approximate the warning like this:
3696 -- Warn only if there are members which are pointers
3697 -- Warn only if there is a non-trivial constructor (otherwise,
3698 there cannot be memory allocated).
3699 -- Warn only if there is a non-trivial destructor. We assume that the
3700 user at least implemented the cleanup correctly, and a destructor
3701 is needed to free dynamic memory.
3703 This seems enough for practical purposes. */
3704 if (warn_ecpp
3705 && has_pointers
3706 && TYPE_HAS_USER_CONSTRUCTOR (t)
3707 && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t)
3708 && !(TYPE_HAS_COPY_CTOR (t) && TYPE_HAS_COPY_ASSIGN (t)))
3710 warning (OPT_Weffc__, "%q#T has pointer data members", t);
3712 if (! TYPE_HAS_COPY_CTOR (t))
3714 warning (OPT_Weffc__,
3715 " but does not override %<%T(const %T&)%>", t, t);
3716 if (!TYPE_HAS_COPY_ASSIGN (t))
3717 warning (OPT_Weffc__, " or %<operator=(const %T&)%>", t);
3719 else if (! TYPE_HAS_COPY_ASSIGN (t))
3720 warning (OPT_Weffc__,
3721 " but does not override %<operator=(const %T&)%>", t);
3724 /* Non-static data member initializers make the default constructor
3725 non-trivial. */
3726 if (any_default_members)
3728 TYPE_NEEDS_CONSTRUCTING (t) = true;
3729 TYPE_HAS_COMPLEX_DFLT (t) = true;
3732 /* If any of the fields couldn't be packed, unset TYPE_PACKED. */
3733 if (cant_pack)
3734 TYPE_PACKED (t) = 0;
3736 /* Check anonymous struct/anonymous union fields. */
3737 finish_struct_anon (t);
3739 /* We've built up the list of access declarations in reverse order.
3740 Fix that now. */
3741 *access_decls = nreverse (*access_decls);
3744 /* If TYPE is an empty class type, records its OFFSET in the table of
3745 OFFSETS. */
3747 static int
3748 record_subobject_offset (tree type, tree offset, splay_tree offsets)
3750 splay_tree_node n;
3752 if (!is_empty_class (type))
3753 return 0;
3755 /* Record the location of this empty object in OFFSETS. */
3756 n = splay_tree_lookup (offsets, (splay_tree_key) offset);
3757 if (!n)
3758 n = splay_tree_insert (offsets,
3759 (splay_tree_key) offset,
3760 (splay_tree_value) NULL_TREE);
3761 n->value = ((splay_tree_value)
3762 tree_cons (NULL_TREE,
3763 type,
3764 (tree) n->value));
3766 return 0;
3769 /* Returns nonzero if TYPE is an empty class type and there is
3770 already an entry in OFFSETS for the same TYPE as the same OFFSET. */
3772 static int
3773 check_subobject_offset (tree type, tree offset, splay_tree offsets)
3775 splay_tree_node n;
3776 tree t;
3778 if (!is_empty_class (type))
3779 return 0;
3781 /* Record the location of this empty object in OFFSETS. */
3782 n = splay_tree_lookup (offsets, (splay_tree_key) offset);
3783 if (!n)
3784 return 0;
3786 for (t = (tree) n->value; t; t = TREE_CHAIN (t))
3787 if (same_type_p (TREE_VALUE (t), type))
3788 return 1;
3790 return 0;
3793 /* Walk through all the subobjects of TYPE (located at OFFSET). Call
3794 F for every subobject, passing it the type, offset, and table of
3795 OFFSETS. If VBASES_P is one, then virtual non-primary bases should
3796 be traversed.
3798 If MAX_OFFSET is non-NULL, then subobjects with an offset greater
3799 than MAX_OFFSET will not be walked.
3801 If F returns a nonzero value, the traversal ceases, and that value
3802 is returned. Otherwise, returns zero. */
3804 static int
3805 walk_subobject_offsets (tree type,
3806 subobject_offset_fn f,
3807 tree offset,
3808 splay_tree offsets,
3809 tree max_offset,
3810 int vbases_p)
3812 int r = 0;
3813 tree type_binfo = NULL_TREE;
3815 /* If this OFFSET is bigger than the MAX_OFFSET, then we should
3816 stop. */
3817 if (max_offset && tree_int_cst_lt (max_offset, offset))
3818 return 0;
3820 if (type == error_mark_node)
3821 return 0;
3823 if (!TYPE_P (type))
3825 type_binfo = type;
3826 type = BINFO_TYPE (type);
3829 if (CLASS_TYPE_P (type))
3831 tree field;
3832 tree binfo;
3833 int i;
3835 /* Avoid recursing into objects that are not interesting. */
3836 if (!CLASSTYPE_CONTAINS_EMPTY_CLASS_P (type))
3837 return 0;
3839 /* Record the location of TYPE. */
3840 r = (*f) (type, offset, offsets);
3841 if (r)
3842 return r;
3844 /* Iterate through the direct base classes of TYPE. */
3845 if (!type_binfo)
3846 type_binfo = TYPE_BINFO (type);
3847 for (i = 0; BINFO_BASE_ITERATE (type_binfo, i, binfo); i++)
3849 tree binfo_offset;
3851 if (BINFO_VIRTUAL_P (binfo))
3852 continue;
3854 tree orig_binfo;
3855 /* We cannot rely on BINFO_OFFSET being set for the base
3856 class yet, but the offsets for direct non-virtual
3857 bases can be calculated by going back to the TYPE. */
3858 orig_binfo = BINFO_BASE_BINFO (TYPE_BINFO (type), i);
3859 binfo_offset = size_binop (PLUS_EXPR,
3860 offset,
3861 BINFO_OFFSET (orig_binfo));
3863 r = walk_subobject_offsets (binfo,
3865 binfo_offset,
3866 offsets,
3867 max_offset,
3868 /*vbases_p=*/0);
3869 if (r)
3870 return r;
3873 if (CLASSTYPE_VBASECLASSES (type))
3875 unsigned ix;
3876 vec<tree, va_gc> *vbases;
3878 /* Iterate through the virtual base classes of TYPE. In G++
3879 3.2, we included virtual bases in the direct base class
3880 loop above, which results in incorrect results; the
3881 correct offsets for virtual bases are only known when
3882 working with the most derived type. */
3883 if (vbases_p)
3884 for (vbases = CLASSTYPE_VBASECLASSES (type), ix = 0;
3885 vec_safe_iterate (vbases, ix, &binfo); ix++)
3887 r = walk_subobject_offsets (binfo,
3889 size_binop (PLUS_EXPR,
3890 offset,
3891 BINFO_OFFSET (binfo)),
3892 offsets,
3893 max_offset,
3894 /*vbases_p=*/0);
3895 if (r)
3896 return r;
3898 else
3900 /* We still have to walk the primary base, if it is
3901 virtual. (If it is non-virtual, then it was walked
3902 above.) */
3903 tree vbase = get_primary_binfo (type_binfo);
3905 if (vbase && BINFO_VIRTUAL_P (vbase)
3906 && BINFO_PRIMARY_P (vbase)
3907 && BINFO_INHERITANCE_CHAIN (vbase) == type_binfo)
3909 r = (walk_subobject_offsets
3910 (vbase, f, offset,
3911 offsets, max_offset, /*vbases_p=*/0));
3912 if (r)
3913 return r;
3918 /* Iterate through the fields of TYPE. */
3919 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
3920 if (TREE_CODE (field) == FIELD_DECL
3921 && TREE_TYPE (field) != error_mark_node
3922 && !DECL_ARTIFICIAL (field))
3924 tree field_offset;
3926 field_offset = byte_position (field);
3928 r = walk_subobject_offsets (TREE_TYPE (field),
3930 size_binop (PLUS_EXPR,
3931 offset,
3932 field_offset),
3933 offsets,
3934 max_offset,
3935 /*vbases_p=*/1);
3936 if (r)
3937 return r;
3940 else if (TREE_CODE (type) == ARRAY_TYPE)
3942 tree element_type = strip_array_types (type);
3943 tree domain = TYPE_DOMAIN (type);
3944 tree index;
3946 /* Avoid recursing into objects that are not interesting. */
3947 if (!CLASS_TYPE_P (element_type)
3948 || !CLASSTYPE_CONTAINS_EMPTY_CLASS_P (element_type)
3949 || !domain
3950 || integer_minus_onep (TYPE_MAX_VALUE (domain)))
3951 return 0;
3953 /* Step through each of the elements in the array. */
3954 for (index = size_zero_node;
3955 !tree_int_cst_lt (TYPE_MAX_VALUE (domain), index);
3956 index = size_binop (PLUS_EXPR, index, size_one_node))
3958 r = walk_subobject_offsets (TREE_TYPE (type),
3960 offset,
3961 offsets,
3962 max_offset,
3963 /*vbases_p=*/1);
3964 if (r)
3965 return r;
3966 offset = size_binop (PLUS_EXPR, offset,
3967 TYPE_SIZE_UNIT (TREE_TYPE (type)));
3968 /* If this new OFFSET is bigger than the MAX_OFFSET, then
3969 there's no point in iterating through the remaining
3970 elements of the array. */
3971 if (max_offset && tree_int_cst_lt (max_offset, offset))
3972 break;
3976 return 0;
3979 /* Record all of the empty subobjects of TYPE (either a type or a
3980 binfo). If IS_DATA_MEMBER is true, then a non-static data member
3981 is being placed at OFFSET; otherwise, it is a base class that is
3982 being placed at OFFSET. */
3984 static void
3985 record_subobject_offsets (tree type,
3986 tree offset,
3987 splay_tree offsets,
3988 bool is_data_member)
3990 tree max_offset;
3991 /* If recording subobjects for a non-static data member or a
3992 non-empty base class , we do not need to record offsets beyond
3993 the size of the biggest empty class. Additional data members
3994 will go at the end of the class. Additional base classes will go
3995 either at offset zero (if empty, in which case they cannot
3996 overlap with offsets past the size of the biggest empty class) or
3997 at the end of the class.
3999 However, if we are placing an empty base class, then we must record
4000 all offsets, as either the empty class is at offset zero (where
4001 other empty classes might later be placed) or at the end of the
4002 class (where other objects might then be placed, so other empty
4003 subobjects might later overlap). */
4004 if (is_data_member
4005 || !is_empty_class (BINFO_TYPE (type)))
4006 max_offset = sizeof_biggest_empty_class;
4007 else
4008 max_offset = NULL_TREE;
4009 walk_subobject_offsets (type, record_subobject_offset, offset,
4010 offsets, max_offset, is_data_member);
4013 /* Returns nonzero if any of the empty subobjects of TYPE (located at
4014 OFFSET) conflict with entries in OFFSETS. If VBASES_P is nonzero,
4015 virtual bases of TYPE are examined. */
4017 static int
4018 layout_conflict_p (tree type,
4019 tree offset,
4020 splay_tree offsets,
4021 int vbases_p)
4023 splay_tree_node max_node;
4025 /* Get the node in OFFSETS that indicates the maximum offset where
4026 an empty subobject is located. */
4027 max_node = splay_tree_max (offsets);
4028 /* If there aren't any empty subobjects, then there's no point in
4029 performing this check. */
4030 if (!max_node)
4031 return 0;
4033 return walk_subobject_offsets (type, check_subobject_offset, offset,
4034 offsets, (tree) (max_node->key),
4035 vbases_p);
4038 /* DECL is a FIELD_DECL corresponding either to a base subobject of a
4039 non-static data member of the type indicated by RLI. BINFO is the
4040 binfo corresponding to the base subobject, OFFSETS maps offsets to
4041 types already located at those offsets. This function determines
4042 the position of the DECL. */
4044 static void
4045 layout_nonempty_base_or_field (record_layout_info rli,
4046 tree decl,
4047 tree binfo,
4048 splay_tree offsets)
4050 tree offset = NULL_TREE;
4051 bool field_p;
4052 tree type;
4054 if (binfo)
4056 /* For the purposes of determining layout conflicts, we want to
4057 use the class type of BINFO; TREE_TYPE (DECL) will be the
4058 CLASSTYPE_AS_BASE version, which does not contain entries for
4059 zero-sized bases. */
4060 type = TREE_TYPE (binfo);
4061 field_p = false;
4063 else
4065 type = TREE_TYPE (decl);
4066 field_p = true;
4069 /* Try to place the field. It may take more than one try if we have
4070 a hard time placing the field without putting two objects of the
4071 same type at the same address. */
4072 while (1)
4074 struct record_layout_info_s old_rli = *rli;
4076 /* Place this field. */
4077 place_field (rli, decl);
4078 offset = byte_position (decl);
4080 /* We have to check to see whether or not there is already
4081 something of the same type at the offset we're about to use.
4082 For example, consider:
4084 struct S {};
4085 struct T : public S { int i; };
4086 struct U : public S, public T {};
4088 Here, we put S at offset zero in U. Then, we can't put T at
4089 offset zero -- its S component would be at the same address
4090 as the S we already allocated. So, we have to skip ahead.
4091 Since all data members, including those whose type is an
4092 empty class, have nonzero size, any overlap can happen only
4093 with a direct or indirect base-class -- it can't happen with
4094 a data member. */
4095 /* In a union, overlap is permitted; all members are placed at
4096 offset zero. */
4097 if (TREE_CODE (rli->t) == UNION_TYPE)
4098 break;
4099 if (layout_conflict_p (field_p ? type : binfo, offset,
4100 offsets, field_p))
4102 /* Strip off the size allocated to this field. That puts us
4103 at the first place we could have put the field with
4104 proper alignment. */
4105 *rli = old_rli;
4107 /* Bump up by the alignment required for the type. */
4108 rli->bitpos
4109 = size_binop (PLUS_EXPR, rli->bitpos,
4110 bitsize_int (binfo
4111 ? CLASSTYPE_ALIGN (type)
4112 : TYPE_ALIGN (type)));
4113 normalize_rli (rli);
4115 else if (TREE_CODE (type) == NULLPTR_TYPE
4116 && warn_abi && abi_version_crosses (9))
4118 /* Before ABI v9, we were giving nullptr_t alignment of 1; if
4119 the offset wasn't aligned like a pointer when we started to
4120 layout this field, that affects its position. */
4121 tree pos = rli_size_unit_so_far (&old_rli);
4122 if (int_cst_value (pos) % TYPE_ALIGN_UNIT (ptr_type_node) != 0)
4124 if (abi_version_at_least (9))
4125 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wabi,
4126 "alignment of %qD increased in -fabi-version=9 "
4127 "(GCC 5.2)", decl);
4128 else
4129 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wabi, "alignment "
4130 "of %qD will increase in -fabi-version=9", decl);
4132 break;
4134 else
4135 /* There was no conflict. We're done laying out this field. */
4136 break;
4139 /* Now that we know where it will be placed, update its
4140 BINFO_OFFSET. */
4141 if (binfo && CLASS_TYPE_P (BINFO_TYPE (binfo)))
4142 /* Indirect virtual bases may have a nonzero BINFO_OFFSET at
4143 this point because their BINFO_OFFSET is copied from another
4144 hierarchy. Therefore, we may not need to add the entire
4145 OFFSET. */
4146 propagate_binfo_offsets (binfo,
4147 size_diffop_loc (input_location,
4148 fold_convert (ssizetype, offset),
4149 fold_convert (ssizetype,
4150 BINFO_OFFSET (binfo))));
4153 /* Returns true if TYPE is empty and OFFSET is nonzero. */
4155 static int
4156 empty_base_at_nonzero_offset_p (tree type,
4157 tree offset,
4158 splay_tree /*offsets*/)
4160 return is_empty_class (type) && !integer_zerop (offset);
4163 /* Layout the empty base BINFO. EOC indicates the byte currently just
4164 past the end of the class, and should be correctly aligned for a
4165 class of the type indicated by BINFO; OFFSETS gives the offsets of
4166 the empty bases allocated so far. T is the most derived
4167 type. Return nonzero iff we added it at the end. */
4169 static bool
4170 layout_empty_base (record_layout_info rli, tree binfo,
4171 tree eoc, splay_tree offsets)
4173 tree alignment;
4174 tree basetype = BINFO_TYPE (binfo);
4175 bool atend = false;
4177 /* This routine should only be used for empty classes. */
4178 gcc_assert (is_empty_class (basetype));
4179 alignment = ssize_int (CLASSTYPE_ALIGN_UNIT (basetype));
4181 if (!integer_zerop (BINFO_OFFSET (binfo)))
4182 propagate_binfo_offsets
4183 (binfo, size_diffop_loc (input_location,
4184 size_zero_node, BINFO_OFFSET (binfo)));
4186 /* This is an empty base class. We first try to put it at offset
4187 zero. */
4188 if (layout_conflict_p (binfo,
4189 BINFO_OFFSET (binfo),
4190 offsets,
4191 /*vbases_p=*/0))
4193 /* That didn't work. Now, we move forward from the next
4194 available spot in the class. */
4195 atend = true;
4196 propagate_binfo_offsets (binfo, fold_convert (ssizetype, eoc));
4197 while (1)
4199 if (!layout_conflict_p (binfo,
4200 BINFO_OFFSET (binfo),
4201 offsets,
4202 /*vbases_p=*/0))
4203 /* We finally found a spot where there's no overlap. */
4204 break;
4206 /* There's overlap here, too. Bump along to the next spot. */
4207 propagate_binfo_offsets (binfo, alignment);
4211 if (CLASSTYPE_USER_ALIGN (basetype))
4213 rli->record_align = MAX (rli->record_align, CLASSTYPE_ALIGN (basetype));
4214 if (warn_packed)
4215 rli->unpacked_align = MAX (rli->unpacked_align, CLASSTYPE_ALIGN (basetype));
4216 TYPE_USER_ALIGN (rli->t) = 1;
4219 return atend;
4222 /* Build the FIELD_DECL for BASETYPE as a base of T, add it to the chain of
4223 fields at NEXT_FIELD, and return it. */
4225 static tree
4226 build_base_field_1 (tree t, tree basetype, tree *&next_field)
4228 /* Create the FIELD_DECL. */
4229 gcc_assert (CLASSTYPE_AS_BASE (basetype));
4230 tree decl = build_decl (input_location,
4231 FIELD_DECL, NULL_TREE, CLASSTYPE_AS_BASE (basetype));
4232 DECL_ARTIFICIAL (decl) = 1;
4233 DECL_IGNORED_P (decl) = 1;
4234 DECL_FIELD_CONTEXT (decl) = t;
4235 DECL_SIZE (decl) = CLASSTYPE_SIZE (basetype);
4236 DECL_SIZE_UNIT (decl) = CLASSTYPE_SIZE_UNIT (basetype);
4237 SET_DECL_ALIGN (decl, CLASSTYPE_ALIGN (basetype));
4238 DECL_USER_ALIGN (decl) = CLASSTYPE_USER_ALIGN (basetype);
4239 SET_DECL_MODE (decl, TYPE_MODE (basetype));
4240 DECL_FIELD_IS_BASE (decl) = 1;
4242 /* Add the new FIELD_DECL to the list of fields for T. */
4243 DECL_CHAIN (decl) = *next_field;
4244 *next_field = decl;
4245 next_field = &DECL_CHAIN (decl);
4247 return decl;
4250 /* Layout the base given by BINFO in the class indicated by RLI.
4251 *BASE_ALIGN is a running maximum of the alignments of
4252 any base class. OFFSETS gives the location of empty base
4253 subobjects. T is the most derived type. Return nonzero if the new
4254 object cannot be nearly-empty. A new FIELD_DECL is inserted at
4255 *NEXT_FIELD, unless BINFO is for an empty base class.
4257 Returns the location at which the next field should be inserted. */
4259 static tree *
4260 build_base_field (record_layout_info rli, tree binfo,
4261 splay_tree offsets, tree *next_field)
4263 tree t = rli->t;
4264 tree basetype = BINFO_TYPE (binfo);
4266 if (!COMPLETE_TYPE_P (basetype))
4267 /* This error is now reported in xref_tag, thus giving better
4268 location information. */
4269 return next_field;
4271 /* Place the base class. */
4272 if (!is_empty_class (basetype))
4274 tree decl;
4276 /* The containing class is non-empty because it has a non-empty
4277 base class. */
4278 CLASSTYPE_EMPTY_P (t) = 0;
4280 /* Create the FIELD_DECL. */
4281 decl = build_base_field_1 (t, basetype, next_field);
4283 /* Try to place the field. It may take more than one try if we
4284 have a hard time placing the field without putting two
4285 objects of the same type at the same address. */
4286 layout_nonempty_base_or_field (rli, decl, binfo, offsets);
4288 else
4290 tree eoc;
4291 bool atend;
4293 /* On some platforms (ARM), even empty classes will not be
4294 byte-aligned. */
4295 eoc = round_up_loc (input_location,
4296 rli_size_unit_so_far (rli),
4297 CLASSTYPE_ALIGN_UNIT (basetype));
4298 atend = layout_empty_base (rli, binfo, eoc, offsets);
4299 /* A nearly-empty class "has no proper base class that is empty,
4300 not morally virtual, and at an offset other than zero." */
4301 if (!BINFO_VIRTUAL_P (binfo) && CLASSTYPE_NEARLY_EMPTY_P (t))
4303 if (atend)
4304 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
4305 /* The check above (used in G++ 3.2) is insufficient because
4306 an empty class placed at offset zero might itself have an
4307 empty base at a nonzero offset. */
4308 else if (walk_subobject_offsets (basetype,
4309 empty_base_at_nonzero_offset_p,
4310 size_zero_node,
4311 /*offsets=*/NULL,
4312 /*max_offset=*/NULL_TREE,
4313 /*vbases_p=*/true))
4314 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
4317 /* We used to not create a FIELD_DECL for empty base classes because of
4318 back end issues with overlapping FIELD_DECLs, but that doesn't seem to
4319 be a problem anymore. We need them to handle initialization of C++17
4320 aggregate bases. */
4321 if (cxx_dialect >= cxx17 && !BINFO_VIRTUAL_P (binfo))
4323 tree decl = build_base_field_1 (t, basetype, next_field);
4324 DECL_FIELD_OFFSET (decl) = BINFO_OFFSET (binfo);
4325 DECL_FIELD_BIT_OFFSET (decl) = bitsize_zero_node;
4326 SET_DECL_OFFSET_ALIGN (decl, BITS_PER_UNIT);
4329 /* An empty virtual base causes a class to be non-empty
4330 -- but in that case we do not need to clear CLASSTYPE_EMPTY_P
4331 here because that was already done when the virtual table
4332 pointer was created. */
4335 /* Record the offsets of BINFO and its base subobjects. */
4336 record_subobject_offsets (binfo,
4337 BINFO_OFFSET (binfo),
4338 offsets,
4339 /*is_data_member=*/false);
4341 return next_field;
4344 /* Layout all of the non-virtual base classes. Record empty
4345 subobjects in OFFSETS. T is the most derived type. Return nonzero
4346 if the type cannot be nearly empty. The fields created
4347 corresponding to the base classes will be inserted at
4348 *NEXT_FIELD. */
4350 static void
4351 build_base_fields (record_layout_info rli,
4352 splay_tree offsets, tree *next_field)
4354 /* Chain to hold all the new FIELD_DECLs which stand in for base class
4355 subobjects. */
4356 tree t = rli->t;
4357 int n_baseclasses = BINFO_N_BASE_BINFOS (TYPE_BINFO (t));
4358 int i;
4360 /* The primary base class is always allocated first. */
4361 if (CLASSTYPE_HAS_PRIMARY_BASE_P (t))
4362 next_field = build_base_field (rli, CLASSTYPE_PRIMARY_BINFO (t),
4363 offsets, next_field);
4365 /* Now allocate the rest of the bases. */
4366 for (i = 0; i < n_baseclasses; ++i)
4368 tree base_binfo;
4370 base_binfo = BINFO_BASE_BINFO (TYPE_BINFO (t), i);
4372 /* The primary base was already allocated above, so we don't
4373 need to allocate it again here. */
4374 if (base_binfo == CLASSTYPE_PRIMARY_BINFO (t))
4375 continue;
4377 /* Virtual bases are added at the end (a primary virtual base
4378 will have already been added). */
4379 if (BINFO_VIRTUAL_P (base_binfo))
4380 continue;
4382 next_field = build_base_field (rli, base_binfo,
4383 offsets, next_field);
4387 /* Go through the TYPE_FIELDS of T issuing any appropriate
4388 diagnostics, figuring out which methods override which other
4389 methods, and so forth. */
4391 static void
4392 check_methods (tree t)
4394 for (tree x = TYPE_FIELDS (t); x; x = DECL_CHAIN (x))
4395 if (DECL_DECLARES_FUNCTION_P (x))
4397 check_for_override (x, t);
4399 if (DECL_PURE_VIRTUAL_P (x)
4400 && (TREE_CODE (x) != FUNCTION_DECL || ! DECL_VINDEX (x)))
4401 error ("initializer specified for non-virtual method %q+D", x);
4402 /* The name of the field is the original field name
4403 Save this in auxiliary field for later overloading. */
4404 if (TREE_CODE (x) == FUNCTION_DECL && DECL_VINDEX (x))
4406 TYPE_POLYMORPHIC_P (t) = 1;
4407 if (DECL_PURE_VIRTUAL_P (x))
4408 vec_safe_push (CLASSTYPE_PURE_VIRTUALS (t), x);
4411 /* All user-provided destructors are non-trivial.
4412 Constructors and assignment ops are handled in
4413 grok_special_member_properties. */
4414 if (DECL_DESTRUCTOR_P (x) && user_provided_p (x))
4415 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t) = 1;
4416 if (!DECL_VIRTUAL_P (x)
4417 && lookup_attribute ("transaction_safe_dynamic",
4418 DECL_ATTRIBUTES (x)))
4419 error_at (DECL_SOURCE_LOCATION (x),
4420 "%<transaction_safe_dynamic%> may only be specified for "
4421 "a virtual function");
4425 /* FN is a constructor or destructor. Clone the declaration to create
4426 a specialized in-charge or not-in-charge version, as indicated by
4427 NAME. */
4429 static tree
4430 build_clone (tree fn, tree name)
4432 tree parms;
4433 tree clone;
4435 /* Copy the function. */
4436 clone = copy_decl (fn);
4437 /* Reset the function name. */
4438 DECL_NAME (clone) = name;
4439 /* Remember where this function came from. */
4440 DECL_ABSTRACT_ORIGIN (clone) = fn;
4441 /* Make it easy to find the CLONE given the FN. */
4442 DECL_CHAIN (clone) = DECL_CHAIN (fn);
4443 DECL_CHAIN (fn) = clone;
4445 /* If this is a template, do the rest on the DECL_TEMPLATE_RESULT. */
4446 if (TREE_CODE (clone) == TEMPLATE_DECL)
4448 tree result = build_clone (DECL_TEMPLATE_RESULT (clone), name);
4449 DECL_TEMPLATE_RESULT (clone) = result;
4450 DECL_TEMPLATE_INFO (result) = copy_node (DECL_TEMPLATE_INFO (result));
4451 DECL_TI_TEMPLATE (result) = clone;
4452 TREE_TYPE (clone) = TREE_TYPE (result);
4453 return clone;
4455 else
4457 // Clone constraints.
4458 if (flag_concepts)
4459 if (tree ci = get_constraints (fn))
4460 set_constraints (clone, copy_node (ci));
4464 SET_DECL_ASSEMBLER_NAME (clone, NULL_TREE);
4465 DECL_CLONED_FUNCTION (clone) = fn;
4466 /* There's no pending inline data for this function. */
4467 DECL_PENDING_INLINE_INFO (clone) = NULL;
4468 DECL_PENDING_INLINE_P (clone) = 0;
4470 /* The base-class destructor is not virtual. */
4471 if (name == base_dtor_identifier)
4473 DECL_VIRTUAL_P (clone) = 0;
4474 if (TREE_CODE (clone) != TEMPLATE_DECL)
4475 DECL_VINDEX (clone) = NULL_TREE;
4478 bool ctor_omit_inherited_parms_p = ctor_omit_inherited_parms (clone);
4479 if (ctor_omit_inherited_parms_p)
4480 gcc_assert (DECL_HAS_IN_CHARGE_PARM_P (clone));
4482 /* If there was an in-charge parameter, drop it from the function
4483 type. */
4484 if (DECL_HAS_IN_CHARGE_PARM_P (clone))
4486 tree basetype;
4487 tree parmtypes;
4488 tree exceptions;
4490 exceptions = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (clone));
4491 basetype = TYPE_METHOD_BASETYPE (TREE_TYPE (clone));
4492 parmtypes = TYPE_ARG_TYPES (TREE_TYPE (clone));
4493 /* Skip the `this' parameter. */
4494 parmtypes = TREE_CHAIN (parmtypes);
4495 /* Skip the in-charge parameter. */
4496 parmtypes = TREE_CHAIN (parmtypes);
4497 /* And the VTT parm, in a complete [cd]tor. */
4498 if (DECL_HAS_VTT_PARM_P (fn)
4499 && ! DECL_NEEDS_VTT_PARM_P (clone))
4500 parmtypes = TREE_CHAIN (parmtypes);
4501 if (ctor_omit_inherited_parms_p)
4503 /* If we're omitting inherited parms, that just leaves the VTT. */
4504 gcc_assert (DECL_NEEDS_VTT_PARM_P (clone));
4505 parmtypes = tree_cons (NULL_TREE, vtt_parm_type, void_list_node);
4507 TREE_TYPE (clone)
4508 = build_method_type_directly (basetype,
4509 TREE_TYPE (TREE_TYPE (clone)),
4510 parmtypes);
4511 if (exceptions)
4512 TREE_TYPE (clone) = build_exception_variant (TREE_TYPE (clone),
4513 exceptions);
4514 TREE_TYPE (clone)
4515 = cp_build_type_attribute_variant (TREE_TYPE (clone),
4516 TYPE_ATTRIBUTES (TREE_TYPE (fn)));
4519 /* Copy the function parameters. */
4520 DECL_ARGUMENTS (clone) = copy_list (DECL_ARGUMENTS (clone));
4521 /* Remove the in-charge parameter. */
4522 if (DECL_HAS_IN_CHARGE_PARM_P (clone))
4524 DECL_CHAIN (DECL_ARGUMENTS (clone))
4525 = DECL_CHAIN (DECL_CHAIN (DECL_ARGUMENTS (clone)));
4526 DECL_HAS_IN_CHARGE_PARM_P (clone) = 0;
4528 /* And the VTT parm, in a complete [cd]tor. */
4529 if (DECL_HAS_VTT_PARM_P (fn))
4531 if (DECL_NEEDS_VTT_PARM_P (clone))
4532 DECL_HAS_VTT_PARM_P (clone) = 1;
4533 else
4535 DECL_CHAIN (DECL_ARGUMENTS (clone))
4536 = DECL_CHAIN (DECL_CHAIN (DECL_ARGUMENTS (clone)));
4537 DECL_HAS_VTT_PARM_P (clone) = 0;
4541 /* A base constructor inheriting from a virtual base doesn't get the
4542 arguments. */
4543 if (ctor_omit_inherited_parms_p)
4544 DECL_CHAIN (DECL_CHAIN (DECL_ARGUMENTS (clone))) = NULL_TREE;
4546 for (parms = DECL_ARGUMENTS (clone); parms; parms = DECL_CHAIN (parms))
4548 DECL_CONTEXT (parms) = clone;
4549 cxx_dup_lang_specific_decl (parms);
4552 /* Create the RTL for this function. */
4553 SET_DECL_RTL (clone, NULL);
4554 rest_of_decl_compilation (clone, /*top_level=*/1, at_eof);
4556 return clone;
4559 /* Implementation of DECL_CLONED_FUNCTION and DECL_CLONED_FUNCTION_P, do
4560 not invoke this function directly.
4562 For a non-thunk function, returns the address of the slot for storing
4563 the function it is a clone of. Otherwise returns NULL_TREE.
4565 If JUST_TESTING, looks through TEMPLATE_DECL and returns NULL if
4566 cloned_function is unset. This is to support the separate
4567 DECL_CLONED_FUNCTION and DECL_CLONED_FUNCTION_P modes; using the latter
4568 on a template makes sense, but not the former. */
4570 tree *
4571 decl_cloned_function_p (const_tree decl, bool just_testing)
4573 tree *ptr;
4574 if (just_testing)
4575 decl = STRIP_TEMPLATE (decl);
4577 if (TREE_CODE (decl) != FUNCTION_DECL
4578 || !DECL_LANG_SPECIFIC (decl)
4579 || DECL_LANG_SPECIFIC (decl)->u.fn.thunk_p)
4581 #if defined ENABLE_TREE_CHECKING && (GCC_VERSION >= 2007)
4582 if (!just_testing)
4583 lang_check_failed (__FILE__, __LINE__, __FUNCTION__);
4584 else
4585 #endif
4586 return NULL;
4589 ptr = &DECL_LANG_SPECIFIC (decl)->u.fn.u5.cloned_function;
4590 if (just_testing && *ptr == NULL_TREE)
4591 return NULL;
4592 else
4593 return ptr;
4596 /* Produce declarations for all appropriate clones of FN. If
4597 UPDATE_METHODS is true, the clones are added to the
4598 CLASSTYPE_MEMBER_VEC. */
4600 void
4601 clone_function_decl (tree fn, bool update_methods)
4603 tree clone;
4605 /* Avoid inappropriate cloning. */
4606 if (DECL_CHAIN (fn)
4607 && DECL_CLONED_FUNCTION_P (DECL_CHAIN (fn)))
4608 return;
4610 if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (fn))
4612 /* For each constructor, we need two variants: an in-charge version
4613 and a not-in-charge version. */
4614 clone = build_clone (fn, complete_ctor_identifier);
4615 if (update_methods)
4616 add_method (DECL_CONTEXT (clone), clone, false);
4617 clone = build_clone (fn, base_ctor_identifier);
4618 if (update_methods)
4619 add_method (DECL_CONTEXT (clone), clone, false);
4621 else
4623 gcc_assert (DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (fn));
4625 /* For each destructor, we need three variants: an in-charge
4626 version, a not-in-charge version, and an in-charge deleting
4627 version. We clone the deleting version first because that
4628 means it will go second on the TYPE_FIELDS list -- and that
4629 corresponds to the correct layout order in the virtual
4630 function table.
4632 For a non-virtual destructor, we do not build a deleting
4633 destructor. */
4634 if (DECL_VIRTUAL_P (fn))
4636 clone = build_clone (fn, deleting_dtor_identifier);
4637 if (update_methods)
4638 add_method (DECL_CONTEXT (clone), clone, false);
4640 clone = build_clone (fn, complete_dtor_identifier);
4641 if (update_methods)
4642 add_method (DECL_CONTEXT (clone), clone, false);
4643 clone = build_clone (fn, base_dtor_identifier);
4644 if (update_methods)
4645 add_method (DECL_CONTEXT (clone), clone, false);
4648 /* Note that this is an abstract function that is never emitted. */
4649 DECL_ABSTRACT_P (fn) = true;
4652 /* DECL is an in charge constructor, which is being defined. This will
4653 have had an in class declaration, from whence clones were
4654 declared. An out-of-class definition can specify additional default
4655 arguments. As it is the clones that are involved in overload
4656 resolution, we must propagate the information from the DECL to its
4657 clones. */
4659 void
4660 adjust_clone_args (tree decl)
4662 tree clone;
4664 for (clone = DECL_CHAIN (decl); clone && DECL_CLONED_FUNCTION_P (clone);
4665 clone = DECL_CHAIN (clone))
4667 tree orig_clone_parms = TYPE_ARG_TYPES (TREE_TYPE (clone));
4668 tree orig_decl_parms = TYPE_ARG_TYPES (TREE_TYPE (decl));
4669 tree decl_parms, clone_parms;
4671 clone_parms = orig_clone_parms;
4673 /* Skip the 'this' parameter. */
4674 orig_clone_parms = TREE_CHAIN (orig_clone_parms);
4675 orig_decl_parms = TREE_CHAIN (orig_decl_parms);
4677 if (DECL_HAS_IN_CHARGE_PARM_P (decl))
4678 orig_decl_parms = TREE_CHAIN (orig_decl_parms);
4679 if (DECL_HAS_VTT_PARM_P (decl))
4680 orig_decl_parms = TREE_CHAIN (orig_decl_parms);
4682 clone_parms = orig_clone_parms;
4683 if (DECL_HAS_VTT_PARM_P (clone))
4684 clone_parms = TREE_CHAIN (clone_parms);
4686 for (decl_parms = orig_decl_parms; decl_parms;
4687 decl_parms = TREE_CHAIN (decl_parms),
4688 clone_parms = TREE_CHAIN (clone_parms))
4690 if (clone_parms == void_list_node)
4692 gcc_assert (decl_parms == clone_parms
4693 || ctor_omit_inherited_parms (clone));
4694 break;
4697 gcc_assert (same_type_p (TREE_TYPE (decl_parms),
4698 TREE_TYPE (clone_parms)));
4700 if (TREE_PURPOSE (decl_parms) && !TREE_PURPOSE (clone_parms))
4702 /* A default parameter has been added. Adjust the
4703 clone's parameters. */
4704 tree exceptions = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (clone));
4705 tree attrs = TYPE_ATTRIBUTES (TREE_TYPE (clone));
4706 tree basetype = TYPE_METHOD_BASETYPE (TREE_TYPE (clone));
4707 tree type;
4709 clone_parms = orig_decl_parms;
4711 if (DECL_HAS_VTT_PARM_P (clone))
4713 clone_parms = tree_cons (TREE_PURPOSE (orig_clone_parms),
4714 TREE_VALUE (orig_clone_parms),
4715 clone_parms);
4716 TREE_TYPE (clone_parms) = TREE_TYPE (orig_clone_parms);
4718 type = build_method_type_directly (basetype,
4719 TREE_TYPE (TREE_TYPE (clone)),
4720 clone_parms);
4721 if (exceptions)
4722 type = build_exception_variant (type, exceptions);
4723 if (attrs)
4724 type = cp_build_type_attribute_variant (type, attrs);
4725 TREE_TYPE (clone) = type;
4727 clone_parms = NULL_TREE;
4728 break;
4731 gcc_assert (!clone_parms || clone_parms == void_list_node);
4735 /* For each of the constructors and destructors in T, create an
4736 in-charge and not-in-charge variant. */
4738 static void
4739 clone_constructors_and_destructors (tree t)
4741 /* While constructors can be via a using declaration, at this point
4742 we no longer need to know that. */
4743 for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (t)); iter; ++iter)
4744 clone_function_decl (*iter, /*update_methods=*/true);
4746 if (tree dtor = CLASSTYPE_DESTRUCTOR (t))
4747 clone_function_decl (dtor, /*update_methods=*/true);
4750 /* Deduce noexcept for a destructor DTOR. */
4752 void
4753 deduce_noexcept_on_destructor (tree dtor)
4755 if (!TYPE_RAISES_EXCEPTIONS (TREE_TYPE (dtor)))
4756 TREE_TYPE (dtor) = build_exception_variant (TREE_TYPE (dtor),
4757 noexcept_deferred_spec);
4760 /* Subroutine of set_one_vmethod_tm_attributes. Search base classes
4761 of TYPE for virtual functions which FNDECL overrides. Return a
4762 mask of the tm attributes found therein. */
4764 static int
4765 look_for_tm_attr_overrides (tree type, tree fndecl)
4767 tree binfo = TYPE_BINFO (type);
4768 tree base_binfo;
4769 int ix, found = 0;
4771 for (ix = 0; BINFO_BASE_ITERATE (binfo, ix, base_binfo); ++ix)
4773 tree o, basetype = BINFO_TYPE (base_binfo);
4775 if (!TYPE_POLYMORPHIC_P (basetype))
4776 continue;
4778 o = look_for_overrides_here (basetype, fndecl);
4779 if (o)
4781 if (lookup_attribute ("transaction_safe_dynamic",
4782 DECL_ATTRIBUTES (o)))
4783 /* transaction_safe_dynamic is not inherited. */;
4784 else
4785 found |= tm_attr_to_mask (find_tm_attribute
4786 (TYPE_ATTRIBUTES (TREE_TYPE (o))));
4788 else
4789 found |= look_for_tm_attr_overrides (basetype, fndecl);
4792 return found;
4795 /* Subroutine of set_method_tm_attributes. Handle the checks and
4796 inheritance for one virtual method FNDECL. */
4798 static void
4799 set_one_vmethod_tm_attributes (tree type, tree fndecl)
4801 tree tm_attr;
4802 int found, have;
4804 found = look_for_tm_attr_overrides (type, fndecl);
4806 /* If FNDECL doesn't actually override anything (i.e. T is the
4807 class that first declares FNDECL virtual), then we're done. */
4808 if (found == 0)
4809 return;
4811 tm_attr = find_tm_attribute (TYPE_ATTRIBUTES (TREE_TYPE (fndecl)));
4812 have = tm_attr_to_mask (tm_attr);
4814 /* Intel STM Language Extension 3.0, Section 4.2 table 4:
4815 tm_pure must match exactly, otherwise no weakening of
4816 tm_safe > tm_callable > nothing. */
4817 /* ??? The tm_pure attribute didn't make the transition to the
4818 multivendor language spec. */
4819 if (have == TM_ATTR_PURE)
4821 if (found != TM_ATTR_PURE)
4823 found &= -found;
4824 goto err_override;
4827 /* If the overridden function is tm_pure, then FNDECL must be. */
4828 else if (found == TM_ATTR_PURE && tm_attr)
4829 goto err_override;
4830 /* Look for base class combinations that cannot be satisfied. */
4831 else if (found != TM_ATTR_PURE && (found & TM_ATTR_PURE))
4833 found &= ~TM_ATTR_PURE;
4834 found &= -found;
4835 error_at (DECL_SOURCE_LOCATION (fndecl),
4836 "method overrides both %<transaction_pure%> and %qE methods",
4837 tm_mask_to_attr (found));
4839 /* If FNDECL did not declare an attribute, then inherit the most
4840 restrictive one. */
4841 else if (tm_attr == NULL)
4843 apply_tm_attr (fndecl, tm_mask_to_attr (least_bit_hwi (found)));
4845 /* Otherwise validate that we're not weaker than a function
4846 that is being overridden. */
4847 else
4849 found &= -found;
4850 if (found <= TM_ATTR_CALLABLE && have > found)
4851 goto err_override;
4853 return;
4855 err_override:
4856 error_at (DECL_SOURCE_LOCATION (fndecl),
4857 "method declared %qE overriding %qE method",
4858 tm_attr, tm_mask_to_attr (found));
4861 /* For each of the methods in T, propagate a class-level tm attribute. */
4863 static void
4864 set_method_tm_attributes (tree t)
4866 tree class_tm_attr, fndecl;
4868 /* Don't bother collecting tm attributes if transactional memory
4869 support is not enabled. */
4870 if (!flag_tm)
4871 return;
4873 /* Process virtual methods first, as they inherit directly from the
4874 base virtual function and also require validation of new attributes. */
4875 if (TYPE_CONTAINS_VPTR_P (t))
4877 tree vchain;
4878 for (vchain = BINFO_VIRTUALS (TYPE_BINFO (t)); vchain;
4879 vchain = TREE_CHAIN (vchain))
4881 fndecl = BV_FN (vchain);
4882 if (DECL_THUNK_P (fndecl))
4883 fndecl = THUNK_TARGET (fndecl);
4884 set_one_vmethod_tm_attributes (t, fndecl);
4888 /* If the class doesn't have an attribute, nothing more to do. */
4889 class_tm_attr = find_tm_attribute (TYPE_ATTRIBUTES (t));
4890 if (class_tm_attr == NULL)
4891 return;
4893 /* Any method that does not yet have a tm attribute inherits
4894 the one from the class. */
4895 for (fndecl = TYPE_FIELDS (t); fndecl; fndecl = DECL_CHAIN (fndecl))
4896 if (DECL_DECLARES_FUNCTION_P (fndecl)
4897 && !find_tm_attribute (TYPE_ATTRIBUTES (TREE_TYPE (fndecl))))
4898 apply_tm_attr (fndecl, class_tm_attr);
4901 /* Returns true if FN is a default constructor. */
4903 bool
4904 default_ctor_p (tree fn)
4906 return (DECL_CONSTRUCTOR_P (fn)
4907 && sufficient_parms_p (FUNCTION_FIRST_USER_PARMTYPE (fn)));
4910 /* Returns true iff class T has a user-defined constructor that can be called
4911 with more than zero arguments. */
4913 bool
4914 type_has_user_nondefault_constructor (tree t)
4916 if (!TYPE_HAS_USER_CONSTRUCTOR (t))
4917 return false;
4919 for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (t)); iter; ++iter)
4921 tree fn = *iter;
4922 if (!DECL_ARTIFICIAL (fn)
4923 && (TREE_CODE (fn) == TEMPLATE_DECL
4924 || (skip_artificial_parms_for (fn, DECL_ARGUMENTS (fn))
4925 != NULL_TREE)))
4926 return true;
4929 return false;
4932 /* Returns the defaulted constructor if T has one. Otherwise, returns
4933 NULL_TREE. */
4935 tree
4936 in_class_defaulted_default_constructor (tree t)
4938 if (!TYPE_HAS_USER_CONSTRUCTOR (t))
4939 return NULL_TREE;
4941 for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (t)); iter; ++iter)
4943 tree fn = *iter;
4945 if (DECL_DEFAULTED_IN_CLASS_P (fn)
4946 && default_ctor_p (fn))
4947 return fn;
4950 return NULL_TREE;
4953 /* Returns true iff FN is a user-provided function, i.e. user-declared
4954 and not defaulted at its first declaration. */
4956 bool
4957 user_provided_p (tree fn)
4959 if (TREE_CODE (fn) == TEMPLATE_DECL)
4960 return true;
4961 else
4962 return (!DECL_ARTIFICIAL (fn)
4963 && !(DECL_INITIALIZED_IN_CLASS_P (fn)
4964 && (DECL_DEFAULTED_FN (fn) || DECL_DELETED_FN (fn))));
4967 /* Returns true iff class T has a user-provided constructor. */
4969 bool
4970 type_has_user_provided_constructor (tree t)
4972 if (!CLASS_TYPE_P (t))
4973 return false;
4975 if (!TYPE_HAS_USER_CONSTRUCTOR (t))
4976 return false;
4978 for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (t)); iter; ++iter)
4979 if (user_provided_p (*iter))
4980 return true;
4982 return false;
4985 /* Returns true iff class T has a user-provided or explicit constructor. */
4987 bool
4988 type_has_user_provided_or_explicit_constructor (tree t)
4990 if (!CLASS_TYPE_P (t))
4991 return false;
4993 if (!TYPE_HAS_USER_CONSTRUCTOR (t))
4994 return false;
4996 for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (t)); iter; ++iter)
4998 tree fn = *iter;
4999 if (user_provided_p (fn) || DECL_NONCONVERTING_P (fn))
5000 return true;
5003 return false;
5006 /* Returns true iff class T has a non-user-provided (i.e. implicitly
5007 declared or explicitly defaulted in the class body) default
5008 constructor. */
5010 bool
5011 type_has_non_user_provided_default_constructor (tree t)
5013 if (!TYPE_HAS_DEFAULT_CONSTRUCTOR (t))
5014 return false;
5015 if (CLASSTYPE_LAZY_DEFAULT_CTOR (t))
5016 return true;
5018 for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (t)); iter; ++iter)
5020 tree fn = *iter;
5021 if (TREE_CODE (fn) == FUNCTION_DECL
5022 && default_ctor_p (fn)
5023 && !user_provided_p (fn))
5024 return true;
5027 return false;
5030 /* TYPE is being used as a virtual base, and has a non-trivial move
5031 assignment. Return true if this is due to there being a user-provided
5032 move assignment in TYPE or one of its subobjects; if there isn't, then
5033 multiple move assignment can't cause any harm. */
5035 bool
5036 vbase_has_user_provided_move_assign (tree type)
5038 /* Does the type itself have a user-provided move assignment operator? */
5039 if (!CLASSTYPE_LAZY_MOVE_ASSIGN (type))
5040 for (ovl_iterator iter (get_class_binding_direct
5041 (type, cp_assignment_operator_id (NOP_EXPR)));
5042 iter; ++iter)
5043 if (!DECL_ARTIFICIAL (*iter) && move_fn_p (*iter))
5044 return true;
5046 /* Do any of its bases? */
5047 tree binfo = TYPE_BINFO (type);
5048 tree base_binfo;
5049 for (int i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
5050 if (vbase_has_user_provided_move_assign (BINFO_TYPE (base_binfo)))
5051 return true;
5053 /* Or non-static data members? */
5054 for (tree field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
5056 if (TREE_CODE (field) == FIELD_DECL
5057 && CLASS_TYPE_P (TREE_TYPE (field))
5058 && vbase_has_user_provided_move_assign (TREE_TYPE (field)))
5059 return true;
5062 /* Seems not. */
5063 return false;
5066 /* If default-initialization leaves part of TYPE uninitialized, returns
5067 a DECL for the field or TYPE itself (DR 253). */
5069 tree
5070 default_init_uninitialized_part (tree type)
5072 tree t, r, binfo;
5073 int i;
5075 type = strip_array_types (type);
5076 if (!CLASS_TYPE_P (type))
5077 return type;
5078 if (!type_has_non_user_provided_default_constructor (type))
5079 return NULL_TREE;
5080 for (binfo = TYPE_BINFO (type), i = 0;
5081 BINFO_BASE_ITERATE (binfo, i, t); ++i)
5083 r = default_init_uninitialized_part (BINFO_TYPE (t));
5084 if (r)
5085 return r;
5087 for (t = TYPE_FIELDS (type); t; t = DECL_CHAIN (t))
5088 if (TREE_CODE (t) == FIELD_DECL
5089 && !DECL_ARTIFICIAL (t)
5090 && !DECL_INITIAL (t))
5092 r = default_init_uninitialized_part (TREE_TYPE (t));
5093 if (r)
5094 return DECL_P (r) ? r : t;
5097 return NULL_TREE;
5100 /* Returns true iff for class T, a trivial synthesized default constructor
5101 would be constexpr. */
5103 bool
5104 trivial_default_constructor_is_constexpr (tree t)
5106 /* A defaulted trivial default constructor is constexpr
5107 if there is nothing to initialize. */
5108 gcc_assert (!TYPE_HAS_COMPLEX_DFLT (t));
5109 return is_really_empty_class (t);
5112 /* Returns true iff class T has a constexpr default constructor. */
5114 bool
5115 type_has_constexpr_default_constructor (tree t)
5117 tree fns;
5119 if (!CLASS_TYPE_P (t))
5121 /* The caller should have stripped an enclosing array. */
5122 gcc_assert (TREE_CODE (t) != ARRAY_TYPE);
5123 return false;
5125 if (CLASSTYPE_LAZY_DEFAULT_CTOR (t))
5127 if (!TYPE_HAS_COMPLEX_DFLT (t))
5128 return trivial_default_constructor_is_constexpr (t);
5129 /* Non-trivial, we need to check subobject constructors. */
5130 lazily_declare_fn (sfk_constructor, t);
5132 fns = locate_ctor (t);
5133 return (fns && DECL_DECLARED_CONSTEXPR_P (fns));
5136 /* Returns true iff class T has a constexpr default constructor or has an
5137 implicitly declared default constructor that we can't tell if it's constexpr
5138 without forcing a lazy declaration (which might cause undesired
5139 instantiations). */
5141 bool
5142 type_maybe_constexpr_default_constructor (tree t)
5144 if (CLASS_TYPE_P (t) && CLASSTYPE_LAZY_DEFAULT_CTOR (t)
5145 && TYPE_HAS_COMPLEX_DFLT (t))
5146 /* Assume it's constexpr. */
5147 return true;
5148 return type_has_constexpr_default_constructor (t);
5151 /* Returns true iff class TYPE has a virtual destructor. */
5153 bool
5154 type_has_virtual_destructor (tree type)
5156 tree dtor;
5158 if (!CLASS_TYPE_P (type))
5159 return false;
5161 gcc_assert (COMPLETE_TYPE_P (type));
5162 dtor = CLASSTYPE_DESTRUCTOR (type);
5163 return (dtor && DECL_VIRTUAL_P (dtor));
5166 /* Returns true iff T, a class, has a move-assignment or
5167 move-constructor. Does not lazily declare either.
5168 If USER_P is false, any move function will do. If it is true, the
5169 move function must be user-declared.
5171 Note that user-declared here is different from "user-provided",
5172 which doesn't include functions that are defaulted in the
5173 class. */
5175 bool
5176 classtype_has_move_assign_or_move_ctor_p (tree t, bool user_p)
5178 gcc_assert (user_p
5179 || (!CLASSTYPE_LAZY_MOVE_CTOR (t)
5180 && !CLASSTYPE_LAZY_MOVE_ASSIGN (t)));
5182 if (!CLASSTYPE_LAZY_MOVE_CTOR (t))
5183 for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (t)); iter; ++iter)
5184 if ((!user_p || !DECL_ARTIFICIAL (*iter)) && move_fn_p (*iter))
5185 return true;
5187 if (!CLASSTYPE_LAZY_MOVE_ASSIGN (t))
5188 for (ovl_iterator iter (get_class_binding_direct
5189 (t, cp_assignment_operator_id (NOP_EXPR)));
5190 iter; ++iter)
5191 if ((!user_p || !DECL_ARTIFICIAL (*iter)) && move_fn_p (*iter))
5192 return true;
5194 return false;
5197 /* Nonzero if we need to build up a constructor call when initializing an
5198 object of this class, either because it has a user-declared constructor
5199 or because it doesn't have a default constructor (so we need to give an
5200 error if no initializer is provided). Use TYPE_NEEDS_CONSTRUCTING when
5201 what you care about is whether or not an object can be produced by a
5202 constructor (e.g. so we don't set TREE_READONLY on const variables of
5203 such type); use this function when what you care about is whether or not
5204 to try to call a constructor to create an object. The latter case is
5205 the former plus some cases of constructors that cannot be called. */
5207 bool
5208 type_build_ctor_call (tree t)
5210 tree inner;
5211 if (TYPE_NEEDS_CONSTRUCTING (t))
5212 return true;
5213 inner = strip_array_types (t);
5214 if (!CLASS_TYPE_P (inner) || ANON_AGGR_TYPE_P (inner))
5215 return false;
5216 if (!TYPE_HAS_DEFAULT_CONSTRUCTOR (inner))
5217 return true;
5218 if (cxx_dialect < cxx11)
5219 return false;
5220 /* A user-declared constructor might be private, and a constructor might
5221 be trivial but deleted. */
5222 for (ovl_iterator iter (get_class_binding (inner, complete_ctor_identifier));
5223 iter; ++iter)
5225 tree fn = *iter;
5226 if (!DECL_ARTIFICIAL (fn)
5227 || DECL_DELETED_FN (fn))
5228 return true;
5230 return false;
5233 /* Like type_build_ctor_call, but for destructors. */
5235 bool
5236 type_build_dtor_call (tree t)
5238 tree inner;
5239 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t))
5240 return true;
5241 inner = strip_array_types (t);
5242 if (!CLASS_TYPE_P (inner) || ANON_AGGR_TYPE_P (inner)
5243 || !COMPLETE_TYPE_P (inner))
5244 return false;
5245 if (cxx_dialect < cxx11)
5246 return false;
5247 /* A user-declared destructor might be private, and a destructor might
5248 be trivial but deleted. */
5249 for (ovl_iterator iter (get_class_binding (inner, complete_dtor_identifier));
5250 iter; ++iter)
5252 tree fn = *iter;
5253 if (!DECL_ARTIFICIAL (fn)
5254 || DECL_DELETED_FN (fn))
5255 return true;
5257 return false;
5260 /* Remove all zero-width bit-fields from T. */
5262 static void
5263 remove_zero_width_bit_fields (tree t)
5265 tree *fieldsp;
5267 fieldsp = &TYPE_FIELDS (t);
5268 while (*fieldsp)
5270 if (TREE_CODE (*fieldsp) == FIELD_DECL
5271 && DECL_C_BIT_FIELD (*fieldsp)
5272 /* We should not be confused by the fact that grokbitfield
5273 temporarily sets the width of the bit field into
5274 DECL_BIT_FIELD_REPRESENTATIVE (*fieldsp).
5275 check_bitfield_decl eventually sets DECL_SIZE (*fieldsp)
5276 to that width. */
5277 && (DECL_SIZE (*fieldsp) == NULL_TREE
5278 || integer_zerop (DECL_SIZE (*fieldsp))))
5279 *fieldsp = DECL_CHAIN (*fieldsp);
5280 else
5281 fieldsp = &DECL_CHAIN (*fieldsp);
5285 /* Returns TRUE iff we need a cookie when dynamically allocating an
5286 array whose elements have the indicated class TYPE. */
5288 static bool
5289 type_requires_array_cookie (tree type)
5291 tree fns;
5292 bool has_two_argument_delete_p = false;
5294 gcc_assert (CLASS_TYPE_P (type));
5296 /* If there's a non-trivial destructor, we need a cookie. In order
5297 to iterate through the array calling the destructor for each
5298 element, we'll have to know how many elements there are. */
5299 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
5300 return true;
5302 /* If the usual deallocation function is a two-argument whose second
5303 argument is of type `size_t', then we have to pass the size of
5304 the array to the deallocation function, so we will need to store
5305 a cookie. */
5306 fns = lookup_fnfields (TYPE_BINFO (type),
5307 cp_operator_id (VEC_DELETE_EXPR),
5308 /*protect=*/0);
5309 /* If there are no `operator []' members, or the lookup is
5310 ambiguous, then we don't need a cookie. */
5311 if (!fns || fns == error_mark_node)
5312 return false;
5313 /* Loop through all of the functions. */
5314 for (lkp_iterator iter (BASELINK_FUNCTIONS (fns)); iter; ++iter)
5316 tree fn = *iter;
5318 /* See if this function is a one-argument delete function. If
5319 it is, then it will be the usual deallocation function. */
5320 tree second_parm = TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (fn)));
5321 if (second_parm == void_list_node)
5322 return false;
5323 /* Do not consider this function if its second argument is an
5324 ellipsis. */
5325 if (!second_parm)
5326 continue;
5327 /* Otherwise, if we have a two-argument function and the second
5328 argument is `size_t', it will be the usual deallocation
5329 function -- unless there is one-argument function, too. */
5330 if (TREE_CHAIN (second_parm) == void_list_node
5331 && same_type_p (TREE_VALUE (second_parm), size_type_node))
5332 has_two_argument_delete_p = true;
5335 return has_two_argument_delete_p;
5338 /* Finish computing the `literal type' property of class type T.
5340 At this point, we have already processed base classes and
5341 non-static data members. We need to check whether the copy
5342 constructor is trivial, the destructor is trivial, and there
5343 is a trivial default constructor or at least one constexpr
5344 constructor other than the copy constructor. */
5346 static void
5347 finalize_literal_type_property (tree t)
5349 tree fn;
5351 if (cxx_dialect < cxx11
5352 || TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t))
5353 CLASSTYPE_LITERAL_P (t) = false;
5354 else if (CLASSTYPE_LITERAL_P (t) && LAMBDA_TYPE_P (t))
5355 CLASSTYPE_LITERAL_P (t) = (cxx_dialect >= cxx17);
5356 else if (CLASSTYPE_LITERAL_P (t) && !TYPE_HAS_TRIVIAL_DFLT (t)
5357 && CLASSTYPE_NON_AGGREGATE (t)
5358 && !TYPE_HAS_CONSTEXPR_CTOR (t))
5359 CLASSTYPE_LITERAL_P (t) = false;
5361 /* C++14 DR 1684 removed this restriction. */
5362 if (cxx_dialect < cxx14
5363 && !CLASSTYPE_LITERAL_P (t) && !LAMBDA_TYPE_P (t))
5364 for (fn = TYPE_FIELDS (t); fn; fn = DECL_CHAIN (fn))
5365 if (TREE_CODE (fn) == FUNCTION_DECL
5366 && DECL_DECLARED_CONSTEXPR_P (fn)
5367 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
5368 && !DECL_CONSTRUCTOR_P (fn))
5370 DECL_DECLARED_CONSTEXPR_P (fn) = false;
5371 if (!DECL_GENERATED_P (fn)
5372 && pedwarn (DECL_SOURCE_LOCATION (fn), OPT_Wpedantic,
5373 "enclosing class of constexpr non-static member "
5374 "function %q+#D is not a literal type", fn))
5375 explain_non_literal_class (t);
5379 /* T is a non-literal type used in a context which requires a constant
5380 expression. Explain why it isn't literal. */
5382 void
5383 explain_non_literal_class (tree t)
5385 static hash_set<tree> *diagnosed;
5387 if (!CLASS_TYPE_P (t))
5388 return;
5389 t = TYPE_MAIN_VARIANT (t);
5391 if (diagnosed == NULL)
5392 diagnosed = new hash_set<tree>;
5393 if (diagnosed->add (t))
5394 /* Already explained. */
5395 return;
5397 inform (0, "%q+T is not literal because:", t);
5398 if (cxx_dialect < cxx17 && LAMBDA_TYPE_P (t))
5399 inform (0, " %qT is a closure type, which is only literal in "
5400 "C++17 and later", t);
5401 else if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t))
5402 inform (0, " %q+T has a non-trivial destructor", t);
5403 else if (CLASSTYPE_NON_AGGREGATE (t)
5404 && !TYPE_HAS_TRIVIAL_DFLT (t)
5405 && !LAMBDA_TYPE_P (t)
5406 && !TYPE_HAS_CONSTEXPR_CTOR (t))
5408 inform (0, " %q+T is not an aggregate, does not have a trivial "
5409 "default constructor, and has no constexpr constructor that "
5410 "is not a copy or move constructor", t);
5411 if (type_has_non_user_provided_default_constructor (t))
5412 /* Note that we can't simply call locate_ctor because when the
5413 constructor is deleted it just returns NULL_TREE. */
5414 for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (t)); iter; ++iter)
5416 tree fn = *iter;
5417 tree parms = TYPE_ARG_TYPES (TREE_TYPE (fn));
5419 parms = skip_artificial_parms_for (fn, parms);
5421 if (sufficient_parms_p (parms))
5423 if (DECL_DELETED_FN (fn))
5424 maybe_explain_implicit_delete (fn);
5425 else
5426 explain_invalid_constexpr_fn (fn);
5427 break;
5431 else
5433 tree binfo, base_binfo, field; int i;
5434 for (binfo = TYPE_BINFO (t), i = 0;
5435 BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
5437 tree basetype = TREE_TYPE (base_binfo);
5438 if (!CLASSTYPE_LITERAL_P (basetype))
5440 inform (0, " base class %qT of %q+T is non-literal",
5441 basetype, t);
5442 explain_non_literal_class (basetype);
5443 return;
5446 for (field = TYPE_FIELDS (t); field; field = TREE_CHAIN (field))
5448 tree ftype;
5449 if (TREE_CODE (field) != FIELD_DECL)
5450 continue;
5451 ftype = TREE_TYPE (field);
5452 if (!literal_type_p (ftype))
5454 inform (DECL_SOURCE_LOCATION (field),
5455 " non-static data member %qD has non-literal type",
5456 field);
5457 if (CLASS_TYPE_P (ftype))
5458 explain_non_literal_class (ftype);
5460 if (CP_TYPE_VOLATILE_P (ftype))
5461 inform (DECL_SOURCE_LOCATION (field),
5462 " non-static data member %qD has volatile type", field);
5467 /* Check the validity of the bases and members declared in T. Add any
5468 implicitly-generated functions (like copy-constructors and
5469 assignment operators). Compute various flag bits (like
5470 CLASSTYPE_NON_LAYOUT_POD_T) for T. This routine works purely at the C++
5471 level: i.e., independently of the ABI in use. */
5473 static void
5474 check_bases_and_members (tree t)
5476 /* Nonzero if the implicitly generated copy constructor should take
5477 a non-const reference argument. */
5478 int cant_have_const_ctor;
5479 /* Nonzero if the implicitly generated assignment operator
5480 should take a non-const reference argument. */
5481 int no_const_asn_ref;
5482 tree access_decls;
5483 bool saved_complex_asn_ref;
5484 bool saved_nontrivial_dtor;
5485 tree fn;
5487 /* By default, we use const reference arguments and generate default
5488 constructors. */
5489 cant_have_const_ctor = 0;
5490 no_const_asn_ref = 0;
5492 /* Check all the base-classes and set FMEM members to point to arrays
5493 of potential interest. */
5494 check_bases (t, &cant_have_const_ctor, &no_const_asn_ref);
5496 /* Deduce noexcept on destructor. This needs to happen after we've set
5497 triviality flags appropriately for our bases. */
5498 if (cxx_dialect >= cxx11)
5499 if (tree dtor = CLASSTYPE_DESTRUCTOR (t))
5500 deduce_noexcept_on_destructor (dtor);
5502 /* Check all the method declarations. */
5503 check_methods (t);
5505 /* Save the initial values of these flags which only indicate whether
5506 or not the class has user-provided functions. As we analyze the
5507 bases and members we can set these flags for other reasons. */
5508 saved_complex_asn_ref = TYPE_HAS_COMPLEX_COPY_ASSIGN (t);
5509 saved_nontrivial_dtor = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t);
5511 /* Check all the data member declarations. We cannot call
5512 check_field_decls until we have called check_bases check_methods,
5513 as check_field_decls depends on TYPE_HAS_NONTRIVIAL_DESTRUCTOR
5514 being set appropriately. */
5515 check_field_decls (t, &access_decls,
5516 &cant_have_const_ctor,
5517 &no_const_asn_ref);
5519 /* A nearly-empty class has to be vptr-containing; a nearly empty
5520 class contains just a vptr. */
5521 if (!TYPE_CONTAINS_VPTR_P (t))
5522 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
5524 /* Do some bookkeeping that will guide the generation of implicitly
5525 declared member functions. */
5526 TYPE_HAS_COMPLEX_COPY_CTOR (t) |= TYPE_CONTAINS_VPTR_P (t);
5527 TYPE_HAS_COMPLEX_MOVE_CTOR (t) |= TYPE_CONTAINS_VPTR_P (t);
5528 /* We need to call a constructor for this class if it has a
5529 user-provided constructor, or if the default constructor is going
5530 to initialize the vptr. (This is not an if-and-only-if;
5531 TYPE_NEEDS_CONSTRUCTING is set elsewhere if bases or members
5532 themselves need constructing.) */
5533 TYPE_NEEDS_CONSTRUCTING (t)
5534 |= (type_has_user_provided_constructor (t) || TYPE_CONTAINS_VPTR_P (t));
5535 /* [dcl.init.aggr]
5537 An aggregate is an array or a class with no user-provided
5538 constructors ... and no virtual functions.
5540 Again, other conditions for being an aggregate are checked
5541 elsewhere. */
5542 CLASSTYPE_NON_AGGREGATE (t)
5543 |= (type_has_user_provided_or_explicit_constructor (t)
5544 || TYPE_POLYMORPHIC_P (t));
5545 /* This is the C++98/03 definition of POD; it changed in C++0x, but we
5546 retain the old definition internally for ABI reasons. */
5547 CLASSTYPE_NON_LAYOUT_POD_P (t)
5548 |= (CLASSTYPE_NON_AGGREGATE (t)
5549 || saved_nontrivial_dtor || saved_complex_asn_ref);
5550 CLASSTYPE_NON_STD_LAYOUT (t) |= TYPE_CONTAINS_VPTR_P (t);
5551 TYPE_HAS_COMPLEX_COPY_ASSIGN (t) |= TYPE_CONTAINS_VPTR_P (t);
5552 TYPE_HAS_COMPLEX_MOVE_ASSIGN (t) |= TYPE_CONTAINS_VPTR_P (t);
5553 TYPE_HAS_COMPLEX_DFLT (t) |= TYPE_CONTAINS_VPTR_P (t);
5555 /* If the only explicitly declared default constructor is user-provided,
5556 set TYPE_HAS_COMPLEX_DFLT. */
5557 if (!TYPE_HAS_COMPLEX_DFLT (t)
5558 && TYPE_HAS_DEFAULT_CONSTRUCTOR (t)
5559 && !type_has_non_user_provided_default_constructor (t))
5560 TYPE_HAS_COMPLEX_DFLT (t) = true;
5562 /* Warn if a public base of a polymorphic type has an accessible
5563 non-virtual destructor. It is only now that we know the class is
5564 polymorphic. Although a polymorphic base will have a already
5565 been diagnosed during its definition, we warn on use too. */
5566 if (TYPE_POLYMORPHIC_P (t) && warn_nonvdtor)
5568 tree binfo = TYPE_BINFO (t);
5569 vec<tree, va_gc> *accesses = BINFO_BASE_ACCESSES (binfo);
5570 tree base_binfo;
5571 unsigned i;
5573 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
5575 tree basetype = TREE_TYPE (base_binfo);
5577 if ((*accesses)[i] == access_public_node
5578 && (TYPE_POLYMORPHIC_P (basetype) || warn_ecpp)
5579 && accessible_nvdtor_p (basetype))
5580 warning (OPT_Wnon_virtual_dtor,
5581 "base class %q#T has accessible non-virtual destructor",
5582 basetype);
5586 /* If the class has no user-declared constructor, but does have
5587 non-static const or reference data members that can never be
5588 initialized, issue a warning. */
5589 if (warn_uninitialized
5590 /* Classes with user-declared constructors are presumed to
5591 initialize these members. */
5592 && !TYPE_HAS_USER_CONSTRUCTOR (t)
5593 /* Aggregates can be initialized with brace-enclosed
5594 initializers. */
5595 && CLASSTYPE_NON_AGGREGATE (t))
5597 tree field;
5599 for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
5601 tree type;
5603 if (TREE_CODE (field) != FIELD_DECL
5604 || DECL_INITIAL (field) != NULL_TREE)
5605 continue;
5607 type = TREE_TYPE (field);
5608 if (TREE_CODE (type) == REFERENCE_TYPE)
5609 warning_at (DECL_SOURCE_LOCATION (field),
5610 OPT_Wuninitialized, "non-static reference %q#D "
5611 "in class without a constructor", field);
5612 else if (CP_TYPE_CONST_P (type)
5613 && (!CLASS_TYPE_P (type)
5614 || !TYPE_HAS_DEFAULT_CONSTRUCTOR (type)))
5615 warning_at (DECL_SOURCE_LOCATION (field),
5616 OPT_Wuninitialized, "non-static const member %q#D "
5617 "in class without a constructor", field);
5621 /* Synthesize any needed methods. */
5622 add_implicitly_declared_members (t, &access_decls,
5623 cant_have_const_ctor,
5624 no_const_asn_ref);
5626 /* Check defaulted declarations here so we have cant_have_const_ctor
5627 and don't need to worry about clones. */
5628 for (fn = TYPE_FIELDS (t); fn; fn = DECL_CHAIN (fn))
5629 if (DECL_DECLARES_FUNCTION_P (fn)
5630 && !DECL_ARTIFICIAL (fn)
5631 && DECL_DEFAULTED_IN_CLASS_P (fn))
5633 int copy = copy_fn_p (fn);
5634 if (copy > 0)
5636 bool imp_const_p
5637 = (DECL_CONSTRUCTOR_P (fn) ? !cant_have_const_ctor
5638 : !no_const_asn_ref);
5639 bool fn_const_p = (copy == 2);
5641 if (fn_const_p && !imp_const_p)
5642 /* If the function is defaulted outside the class, we just
5643 give the synthesis error. */
5644 error ("%q+D declared to take const reference, but implicit "
5645 "declaration would take non-const", fn);
5647 defaulted_late_check (fn);
5650 if (LAMBDA_TYPE_P (t))
5652 /* "This class type is not an aggregate." */
5653 CLASSTYPE_NON_AGGREGATE (t) = 1;
5656 /* Compute the 'literal type' property before we
5657 do anything with non-static member functions. */
5658 finalize_literal_type_property (t);
5660 /* Create the in-charge and not-in-charge variants of constructors
5661 and destructors. */
5662 clone_constructors_and_destructors (t);
5664 /* Process the using-declarations. */
5665 for (; access_decls; access_decls = TREE_CHAIN (access_decls))
5666 handle_using_decl (TREE_VALUE (access_decls), t);
5668 /* Figure out whether or not we will need a cookie when dynamically
5669 allocating an array of this type. */
5670 LANG_TYPE_CLASS_CHECK (t)->vec_new_uses_cookie
5671 = type_requires_array_cookie (t);
5674 /* If T needs a pointer to its virtual function table, set TYPE_VFIELD
5675 accordingly. If a new vfield was created (because T doesn't have a
5676 primary base class), then the newly created field is returned. It
5677 is not added to the TYPE_FIELDS list; it is the caller's
5678 responsibility to do that. Accumulate declared virtual functions
5679 on VIRTUALS_P. */
5681 static tree
5682 create_vtable_ptr (tree t, tree* virtuals_p)
5684 tree fn;
5686 /* Collect the virtual functions declared in T. */
5687 for (fn = TYPE_FIELDS (t); fn; fn = DECL_CHAIN (fn))
5688 if (TREE_CODE (fn) == FUNCTION_DECL
5689 && DECL_VINDEX (fn) && !DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (fn)
5690 && TREE_CODE (DECL_VINDEX (fn)) != INTEGER_CST)
5692 tree new_virtual = make_node (TREE_LIST);
5694 BV_FN (new_virtual) = fn;
5695 BV_DELTA (new_virtual) = integer_zero_node;
5696 BV_VCALL_INDEX (new_virtual) = NULL_TREE;
5698 TREE_CHAIN (new_virtual) = *virtuals_p;
5699 *virtuals_p = new_virtual;
5702 /* If we couldn't find an appropriate base class, create a new field
5703 here. Even if there weren't any new virtual functions, we might need a
5704 new virtual function table if we're supposed to include vptrs in
5705 all classes that need them. */
5706 if (!TYPE_VFIELD (t) && (*virtuals_p || TYPE_CONTAINS_VPTR_P (t)))
5708 /* We build this decl with vtbl_ptr_type_node, which is a
5709 `vtable_entry_type*'. It might seem more precise to use
5710 `vtable_entry_type (*)[N]' where N is the number of virtual
5711 functions. However, that would require the vtable pointer in
5712 base classes to have a different type than the vtable pointer
5713 in derived classes. We could make that happen, but that
5714 still wouldn't solve all the problems. In particular, the
5715 type-based alias analysis code would decide that assignments
5716 to the base class vtable pointer can't alias assignments to
5717 the derived class vtable pointer, since they have different
5718 types. Thus, in a derived class destructor, where the base
5719 class constructor was inlined, we could generate bad code for
5720 setting up the vtable pointer.
5722 Therefore, we use one type for all vtable pointers. We still
5723 use a type-correct type; it's just doesn't indicate the array
5724 bounds. That's better than using `void*' or some such; it's
5725 cleaner, and it let's the alias analysis code know that these
5726 stores cannot alias stores to void*! */
5727 tree field;
5729 field = build_decl (input_location,
5730 FIELD_DECL, get_vfield_name (t), vtbl_ptr_type_node);
5731 DECL_VIRTUAL_P (field) = 1;
5732 DECL_ARTIFICIAL (field) = 1;
5733 DECL_FIELD_CONTEXT (field) = t;
5734 DECL_FCONTEXT (field) = t;
5735 if (TYPE_PACKED (t))
5736 DECL_PACKED (field) = 1;
5738 TYPE_VFIELD (t) = field;
5740 /* This class is non-empty. */
5741 CLASSTYPE_EMPTY_P (t) = 0;
5743 return field;
5746 return NULL_TREE;
5749 /* Add OFFSET to all base types of BINFO which is a base in the
5750 hierarchy dominated by T.
5752 OFFSET, which is a type offset, is number of bytes. */
5754 static void
5755 propagate_binfo_offsets (tree binfo, tree offset)
5757 int i;
5758 tree primary_binfo;
5759 tree base_binfo;
5761 /* Update BINFO's offset. */
5762 BINFO_OFFSET (binfo)
5763 = fold_convert (sizetype,
5764 size_binop (PLUS_EXPR,
5765 fold_convert (ssizetype, BINFO_OFFSET (binfo)),
5766 offset));
5768 /* Find the primary base class. */
5769 primary_binfo = get_primary_binfo (binfo);
5771 if (primary_binfo && BINFO_INHERITANCE_CHAIN (primary_binfo) == binfo)
5772 propagate_binfo_offsets (primary_binfo, offset);
5774 /* Scan all of the bases, pushing the BINFO_OFFSET adjust
5775 downwards. */
5776 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
5778 /* Don't do the primary base twice. */
5779 if (base_binfo == primary_binfo)
5780 continue;
5782 if (BINFO_VIRTUAL_P (base_binfo))
5783 continue;
5785 propagate_binfo_offsets (base_binfo, offset);
5789 /* Set BINFO_OFFSET for all of the virtual bases for RLI->T. Update
5790 TYPE_ALIGN and TYPE_SIZE for T. OFFSETS gives the location of
5791 empty subobjects of T. */
5793 static void
5794 layout_virtual_bases (record_layout_info rli, splay_tree offsets)
5796 tree vbase;
5797 tree t = rli->t;
5798 tree *next_field;
5800 if (BINFO_N_BASE_BINFOS (TYPE_BINFO (t)) == 0)
5801 return;
5803 /* Find the last field. The artificial fields created for virtual
5804 bases will go after the last extant field to date. */
5805 next_field = &TYPE_FIELDS (t);
5806 while (*next_field)
5807 next_field = &DECL_CHAIN (*next_field);
5809 /* Go through the virtual bases, allocating space for each virtual
5810 base that is not already a primary base class. These are
5811 allocated in inheritance graph order. */
5812 for (vbase = TYPE_BINFO (t); vbase; vbase = TREE_CHAIN (vbase))
5814 if (!BINFO_VIRTUAL_P (vbase))
5815 continue;
5817 if (!BINFO_PRIMARY_P (vbase))
5819 /* This virtual base is not a primary base of any class in the
5820 hierarchy, so we have to add space for it. */
5821 next_field = build_base_field (rli, vbase,
5822 offsets, next_field);
5827 /* Returns the offset of the byte just past the end of the base class
5828 BINFO. */
5830 static tree
5831 end_of_base (tree binfo)
5833 tree size;
5835 if (!CLASSTYPE_AS_BASE (BINFO_TYPE (binfo)))
5836 size = TYPE_SIZE_UNIT (char_type_node);
5837 else if (is_empty_class (BINFO_TYPE (binfo)))
5838 /* An empty class has zero CLASSTYPE_SIZE_UNIT, but we need to
5839 allocate some space for it. It cannot have virtual bases, so
5840 TYPE_SIZE_UNIT is fine. */
5841 size = TYPE_SIZE_UNIT (BINFO_TYPE (binfo));
5842 else
5843 size = CLASSTYPE_SIZE_UNIT (BINFO_TYPE (binfo));
5845 return size_binop (PLUS_EXPR, BINFO_OFFSET (binfo), size);
5848 /* Returns the offset of the byte just past the end of the base class
5849 with the highest offset in T. If INCLUDE_VIRTUALS_P is zero, then
5850 only non-virtual bases are included. */
5852 static tree
5853 end_of_class (tree t, int include_virtuals_p)
5855 tree result = size_zero_node;
5856 vec<tree, va_gc> *vbases;
5857 tree binfo;
5858 tree base_binfo;
5859 tree offset;
5860 int i;
5862 for (binfo = TYPE_BINFO (t), i = 0;
5863 BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
5865 if (!include_virtuals_p
5866 && BINFO_VIRTUAL_P (base_binfo)
5867 && (!BINFO_PRIMARY_P (base_binfo)
5868 || BINFO_INHERITANCE_CHAIN (base_binfo) != TYPE_BINFO (t)))
5869 continue;
5871 offset = end_of_base (base_binfo);
5872 if (tree_int_cst_lt (result, offset))
5873 result = offset;
5876 if (include_virtuals_p)
5877 for (vbases = CLASSTYPE_VBASECLASSES (t), i = 0;
5878 vec_safe_iterate (vbases, i, &base_binfo); i++)
5880 offset = end_of_base (base_binfo);
5881 if (tree_int_cst_lt (result, offset))
5882 result = offset;
5885 return result;
5888 /* Warn about bases of T that are inaccessible because they are
5889 ambiguous. For example:
5891 struct S {};
5892 struct T : public S {};
5893 struct U : public S, public T {};
5895 Here, `(S*) new U' is not allowed because there are two `S'
5896 subobjects of U. */
5898 static void
5899 warn_about_ambiguous_bases (tree t)
5901 int i;
5902 vec<tree, va_gc> *vbases;
5903 tree basetype;
5904 tree binfo;
5905 tree base_binfo;
5907 /* If there are no repeated bases, nothing can be ambiguous. */
5908 if (!CLASSTYPE_REPEATED_BASE_P (t))
5909 return;
5911 /* Check direct bases. */
5912 for (binfo = TYPE_BINFO (t), i = 0;
5913 BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
5915 basetype = BINFO_TYPE (base_binfo);
5917 if (!uniquely_derived_from_p (basetype, t))
5918 warning (0, "direct base %qT inaccessible in %qT due to ambiguity",
5919 basetype, t);
5922 /* Check for ambiguous virtual bases. */
5923 if (extra_warnings)
5924 for (vbases = CLASSTYPE_VBASECLASSES (t), i = 0;
5925 vec_safe_iterate (vbases, i, &binfo); i++)
5927 basetype = BINFO_TYPE (binfo);
5929 if (!uniquely_derived_from_p (basetype, t))
5930 warning (OPT_Wextra, "virtual base %qT inaccessible in %qT due "
5931 "to ambiguity", basetype, t);
5935 /* Compare two INTEGER_CSTs K1 and K2. */
5937 static int
5938 splay_tree_compare_integer_csts (splay_tree_key k1, splay_tree_key k2)
5940 return tree_int_cst_compare ((tree) k1, (tree) k2);
5943 /* Increase the size indicated in RLI to account for empty classes
5944 that are "off the end" of the class. */
5946 static void
5947 include_empty_classes (record_layout_info rli)
5949 tree eoc;
5950 tree rli_size;
5952 /* It might be the case that we grew the class to allocate a
5953 zero-sized base class. That won't be reflected in RLI, yet,
5954 because we are willing to overlay multiple bases at the same
5955 offset. However, now we need to make sure that RLI is big enough
5956 to reflect the entire class. */
5957 eoc = end_of_class (rli->t, CLASSTYPE_AS_BASE (rli->t) != NULL_TREE);
5958 rli_size = rli_size_unit_so_far (rli);
5959 if (TREE_CODE (rli_size) == INTEGER_CST
5960 && tree_int_cst_lt (rli_size, eoc))
5962 /* The size should have been rounded to a whole byte. */
5963 gcc_assert (tree_int_cst_equal
5964 (rli->bitpos, round_down (rli->bitpos, BITS_PER_UNIT)));
5965 rli->bitpos
5966 = size_binop (PLUS_EXPR,
5967 rli->bitpos,
5968 size_binop (MULT_EXPR,
5969 fold_convert (bitsizetype,
5970 size_binop (MINUS_EXPR,
5971 eoc, rli_size)),
5972 bitsize_int (BITS_PER_UNIT)));
5973 normalize_rli (rli);
5977 /* Calculate the TYPE_SIZE, TYPE_ALIGN, etc for T. Calculate
5978 BINFO_OFFSETs for all of the base-classes. Position the vtable
5979 pointer. Accumulate declared virtual functions on VIRTUALS_P. */
5981 static void
5982 layout_class_type (tree t, tree *virtuals_p)
5984 tree non_static_data_members;
5985 tree field;
5986 tree vptr;
5987 record_layout_info rli;
5988 /* Maps offsets (represented as INTEGER_CSTs) to a TREE_LIST of
5989 types that appear at that offset. */
5990 splay_tree empty_base_offsets;
5991 /* True if the last field laid out was a bit-field. */
5992 bool last_field_was_bitfield = false;
5993 /* The location at which the next field should be inserted. */
5994 tree *next_field;
5995 /* T, as a base class. */
5996 tree base_t;
5998 /* Keep track of the first non-static data member. */
5999 non_static_data_members = TYPE_FIELDS (t);
6001 /* Start laying out the record. */
6002 rli = start_record_layout (t);
6004 /* Mark all the primary bases in the hierarchy. */
6005 determine_primary_bases (t);
6007 /* Create a pointer to our virtual function table. */
6008 vptr = create_vtable_ptr (t, virtuals_p);
6010 /* The vptr is always the first thing in the class. */
6011 if (vptr)
6013 DECL_CHAIN (vptr) = TYPE_FIELDS (t);
6014 TYPE_FIELDS (t) = vptr;
6015 next_field = &DECL_CHAIN (vptr);
6016 place_field (rli, vptr);
6018 else
6019 next_field = &TYPE_FIELDS (t);
6021 /* Build FIELD_DECLs for all of the non-virtual base-types. */
6022 empty_base_offsets = splay_tree_new (splay_tree_compare_integer_csts,
6023 NULL, NULL);
6024 build_base_fields (rli, empty_base_offsets, next_field);
6026 /* Layout the non-static data members. */
6027 for (field = non_static_data_members; field; field = DECL_CHAIN (field))
6029 tree type;
6030 tree padding;
6032 /* We still pass things that aren't non-static data members to
6033 the back end, in case it wants to do something with them. */
6034 if (TREE_CODE (field) != FIELD_DECL)
6036 place_field (rli, field);
6037 /* If the static data member has incomplete type, keep track
6038 of it so that it can be completed later. (The handling
6039 of pending statics in finish_record_layout is
6040 insufficient; consider:
6042 struct S1;
6043 struct S2 { static S1 s1; };
6045 At this point, finish_record_layout will be called, but
6046 S1 is still incomplete.) */
6047 if (VAR_P (field))
6049 maybe_register_incomplete_var (field);
6050 /* The visibility of static data members is determined
6051 at their point of declaration, not their point of
6052 definition. */
6053 determine_visibility (field);
6055 continue;
6058 type = TREE_TYPE (field);
6059 if (type == error_mark_node)
6060 continue;
6062 padding = NULL_TREE;
6064 /* If this field is a bit-field whose width is greater than its
6065 type, then there are some special rules for allocating
6066 it. */
6067 if (DECL_C_BIT_FIELD (field)
6068 && tree_int_cst_lt (TYPE_SIZE (type), DECL_SIZE (field)))
6070 bool was_unnamed_p = false;
6071 /* We must allocate the bits as if suitably aligned for the
6072 longest integer type that fits in this many bits. Then,
6073 we are supposed to use the left over bits as additional
6074 padding. */
6076 /* Do not pick a type bigger than MAX_FIXED_MODE_SIZE. */
6077 tree limit = size_int (MAX_FIXED_MODE_SIZE);
6078 if (tree_int_cst_lt (DECL_SIZE (field), limit))
6079 limit = DECL_SIZE (field);
6081 tree integer_type = integer_types[itk_char];
6082 for (unsigned itk = itk_char; itk != itk_none; itk++)
6083 if (tree next = integer_types[itk])
6085 if (tree_int_cst_lt (limit, TYPE_SIZE (next)))
6086 /* Too big, so our current guess is what we want. */
6087 break;
6088 /* Not bigger than limit, ok */
6089 integer_type = next;
6092 /* Figure out how much additional padding is required. */
6093 if (TREE_CODE (t) == UNION_TYPE)
6094 /* In a union, the padding field must have the full width
6095 of the bit-field; all fields start at offset zero. */
6096 padding = DECL_SIZE (field);
6097 else
6098 padding = size_binop (MINUS_EXPR, DECL_SIZE (field),
6099 TYPE_SIZE (integer_type));
6101 if (integer_zerop (padding))
6102 padding = NULL_TREE;
6104 /* An unnamed bitfield does not normally affect the
6105 alignment of the containing class on a target where
6106 PCC_BITFIELD_TYPE_MATTERS. But, the C++ ABI does not
6107 make any exceptions for unnamed bitfields when the
6108 bitfields are longer than their types. Therefore, we
6109 temporarily give the field a name. */
6110 if (PCC_BITFIELD_TYPE_MATTERS && !DECL_NAME (field))
6112 was_unnamed_p = true;
6113 DECL_NAME (field) = make_anon_name ();
6116 DECL_SIZE (field) = TYPE_SIZE (integer_type);
6117 SET_DECL_ALIGN (field, TYPE_ALIGN (integer_type));
6118 DECL_USER_ALIGN (field) = TYPE_USER_ALIGN (integer_type);
6119 layout_nonempty_base_or_field (rli, field, NULL_TREE,
6120 empty_base_offsets);
6121 if (was_unnamed_p)
6122 DECL_NAME (field) = NULL_TREE;
6123 /* Now that layout has been performed, set the size of the
6124 field to the size of its declared type; the rest of the
6125 field is effectively invisible. */
6126 DECL_SIZE (field) = TYPE_SIZE (type);
6127 /* We must also reset the DECL_MODE of the field. */
6128 SET_DECL_MODE (field, TYPE_MODE (type));
6130 else
6131 layout_nonempty_base_or_field (rli, field, NULL_TREE,
6132 empty_base_offsets);
6134 /* Remember the location of any empty classes in FIELD. */
6135 record_subobject_offsets (TREE_TYPE (field),
6136 byte_position(field),
6137 empty_base_offsets,
6138 /*is_data_member=*/true);
6140 /* If a bit-field does not immediately follow another bit-field,
6141 and yet it starts in the middle of a byte, we have failed to
6142 comply with the ABI. */
6143 if (warn_abi
6144 && DECL_C_BIT_FIELD (field)
6145 /* The TREE_NO_WARNING flag gets set by Objective-C when
6146 laying out an Objective-C class. The ObjC ABI differs
6147 from the C++ ABI, and so we do not want a warning
6148 here. */
6149 && !TREE_NO_WARNING (field)
6150 && !last_field_was_bitfield
6151 && !integer_zerop (size_binop (TRUNC_MOD_EXPR,
6152 DECL_FIELD_BIT_OFFSET (field),
6153 bitsize_unit_node)))
6154 warning_at (DECL_SOURCE_LOCATION (field), OPT_Wabi,
6155 "offset of %qD is not ABI-compliant and may "
6156 "change in a future version of GCC", field);
6158 /* The middle end uses the type of expressions to determine the
6159 possible range of expression values. In order to optimize
6160 "x.i > 7" to "false" for a 2-bit bitfield "i", the middle end
6161 must be made aware of the width of "i", via its type.
6163 Because C++ does not have integer types of arbitrary width,
6164 we must (for the purposes of the front end) convert from the
6165 type assigned here to the declared type of the bitfield
6166 whenever a bitfield expression is used as an rvalue.
6167 Similarly, when assigning a value to a bitfield, the value
6168 must be converted to the type given the bitfield here. */
6169 if (DECL_C_BIT_FIELD (field))
6171 unsigned HOST_WIDE_INT width;
6172 tree ftype = TREE_TYPE (field);
6173 width = tree_to_uhwi (DECL_SIZE (field));
6174 if (width != TYPE_PRECISION (ftype))
6176 TREE_TYPE (field)
6177 = c_build_bitfield_integer_type (width,
6178 TYPE_UNSIGNED (ftype));
6179 TREE_TYPE (field)
6180 = cp_build_qualified_type (TREE_TYPE (field),
6181 cp_type_quals (ftype));
6185 /* If we needed additional padding after this field, add it
6186 now. */
6187 if (padding)
6189 tree padding_field;
6191 padding_field = build_decl (input_location,
6192 FIELD_DECL,
6193 NULL_TREE,
6194 char_type_node);
6195 DECL_BIT_FIELD (padding_field) = 1;
6196 DECL_SIZE (padding_field) = padding;
6197 DECL_CONTEXT (padding_field) = t;
6198 DECL_ARTIFICIAL (padding_field) = 1;
6199 DECL_IGNORED_P (padding_field) = 1;
6200 layout_nonempty_base_or_field (rli, padding_field,
6201 NULL_TREE,
6202 empty_base_offsets);
6205 last_field_was_bitfield = DECL_C_BIT_FIELD (field);
6208 if (!integer_zerop (rli->bitpos))
6210 /* Make sure that we are on a byte boundary so that the size of
6211 the class without virtual bases will always be a round number
6212 of bytes. */
6213 rli->bitpos = round_up_loc (input_location, rli->bitpos, BITS_PER_UNIT);
6214 normalize_rli (rli);
6217 /* Delete all zero-width bit-fields from the list of fields. Now
6218 that the type is laid out they are no longer important. */
6219 remove_zero_width_bit_fields (t);
6221 /* Create the version of T used for virtual bases. We do not use
6222 make_class_type for this version; this is an artificial type. For
6223 a POD type, we just reuse T. */
6224 if (CLASSTYPE_NON_LAYOUT_POD_P (t) || CLASSTYPE_EMPTY_P (t))
6226 base_t = make_node (TREE_CODE (t));
6228 /* Set the size and alignment for the new type. */
6229 tree eoc;
6231 /* If the ABI version is not at least two, and the last
6232 field was a bit-field, RLI may not be on a byte
6233 boundary. In particular, rli_size_unit_so_far might
6234 indicate the last complete byte, while rli_size_so_far
6235 indicates the total number of bits used. Therefore,
6236 rli_size_so_far, rather than rli_size_unit_so_far, is
6237 used to compute TYPE_SIZE_UNIT. */
6238 eoc = end_of_class (t, /*include_virtuals_p=*/0);
6239 TYPE_SIZE_UNIT (base_t)
6240 = size_binop (MAX_EXPR,
6241 fold_convert (sizetype,
6242 size_binop (CEIL_DIV_EXPR,
6243 rli_size_so_far (rli),
6244 bitsize_int (BITS_PER_UNIT))),
6245 eoc);
6246 TYPE_SIZE (base_t)
6247 = size_binop (MAX_EXPR,
6248 rli_size_so_far (rli),
6249 size_binop (MULT_EXPR,
6250 fold_convert (bitsizetype, eoc),
6251 bitsize_int (BITS_PER_UNIT)));
6252 SET_TYPE_ALIGN (base_t, rli->record_align);
6253 TYPE_USER_ALIGN (base_t) = TYPE_USER_ALIGN (t);
6255 /* Copy the fields from T. */
6256 next_field = &TYPE_FIELDS (base_t);
6257 for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
6258 if (TREE_CODE (field) == FIELD_DECL)
6260 *next_field = copy_node (field);
6261 DECL_CONTEXT (*next_field) = base_t;
6262 next_field = &DECL_CHAIN (*next_field);
6264 *next_field = NULL_TREE;
6266 /* Record the base version of the type. */
6267 CLASSTYPE_AS_BASE (t) = base_t;
6268 TYPE_CONTEXT (base_t) = t;
6270 else
6271 CLASSTYPE_AS_BASE (t) = t;
6273 /* Every empty class contains an empty class. */
6274 if (CLASSTYPE_EMPTY_P (t))
6275 CLASSTYPE_CONTAINS_EMPTY_CLASS_P (t) = 1;
6277 /* Set the TYPE_DECL for this type to contain the right
6278 value for DECL_OFFSET, so that we can use it as part
6279 of a COMPONENT_REF for multiple inheritance. */
6280 layout_decl (TYPE_MAIN_DECL (t), 0);
6282 /* Now fix up any virtual base class types that we left lying
6283 around. We must get these done before we try to lay out the
6284 virtual function table. As a side-effect, this will remove the
6285 base subobject fields. */
6286 layout_virtual_bases (rli, empty_base_offsets);
6288 /* Make sure that empty classes are reflected in RLI at this
6289 point. */
6290 include_empty_classes (rli);
6292 /* Make sure not to create any structures with zero size. */
6293 if (integer_zerop (rli_size_unit_so_far (rli)) && CLASSTYPE_EMPTY_P (t))
6294 place_field (rli,
6295 build_decl (input_location,
6296 FIELD_DECL, NULL_TREE, char_type_node));
6298 /* If this is a non-POD, declaring it packed makes a difference to how it
6299 can be used as a field; don't let finalize_record_size undo it. */
6300 if (TYPE_PACKED (t) && !layout_pod_type_p (t))
6301 rli->packed_maybe_necessary = true;
6303 /* Let the back end lay out the type. */
6304 finish_record_layout (rli, /*free_p=*/true);
6306 if (TYPE_SIZE_UNIT (t)
6307 && TREE_CODE (TYPE_SIZE_UNIT (t)) == INTEGER_CST
6308 && !TREE_OVERFLOW (TYPE_SIZE_UNIT (t))
6309 && !valid_constant_size_p (TYPE_SIZE_UNIT (t)))
6310 error ("size of type %qT is too large (%qE bytes)", t, TYPE_SIZE_UNIT (t));
6312 /* Warn about bases that can't be talked about due to ambiguity. */
6313 warn_about_ambiguous_bases (t);
6315 /* Now that we're done with layout, give the base fields the real types. */
6316 for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
6317 if (DECL_ARTIFICIAL (field) && IS_FAKE_BASE_TYPE (TREE_TYPE (field)))
6318 TREE_TYPE (field) = TYPE_CONTEXT (TREE_TYPE (field));
6320 /* Clean up. */
6321 splay_tree_delete (empty_base_offsets);
6323 if (CLASSTYPE_EMPTY_P (t)
6324 && tree_int_cst_lt (sizeof_biggest_empty_class,
6325 TYPE_SIZE_UNIT (t)))
6326 sizeof_biggest_empty_class = TYPE_SIZE_UNIT (t);
6329 /* Determine the "key method" for the class type indicated by TYPE,
6330 and set CLASSTYPE_KEY_METHOD accordingly. */
6332 void
6333 determine_key_method (tree type)
6335 tree method;
6337 if (processing_template_decl
6338 || CLASSTYPE_TEMPLATE_INSTANTIATION (type)
6339 || CLASSTYPE_INTERFACE_KNOWN (type))
6340 return;
6342 /* The key method is the first non-pure virtual function that is not
6343 inline at the point of class definition. On some targets the
6344 key function may not be inline; those targets should not call
6345 this function until the end of the translation unit. */
6346 for (method = TYPE_FIELDS (type); method; method = DECL_CHAIN (method))
6347 if (TREE_CODE (method) == FUNCTION_DECL
6348 && DECL_VINDEX (method) != NULL_TREE
6349 && ! DECL_DECLARED_INLINE_P (method)
6350 && ! DECL_PURE_VIRTUAL_P (method))
6352 CLASSTYPE_KEY_METHOD (type) = method;
6353 break;
6356 return;
6359 /* Helper of find_flexarrays. Return true when FLD refers to a non-static
6360 class data member of non-zero size, otherwise false. */
6362 static inline bool
6363 field_nonempty_p (const_tree fld)
6365 if (TREE_CODE (fld) == ERROR_MARK)
6366 return false;
6368 tree type = TREE_TYPE (fld);
6369 if (TREE_CODE (fld) == FIELD_DECL
6370 && TREE_CODE (type) != ERROR_MARK
6371 && (DECL_NAME (fld) || RECORD_OR_UNION_TYPE_P (type)))
6373 return TYPE_SIZE (type)
6374 && (TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST
6375 || !tree_int_cst_equal (size_zero_node, TYPE_SIZE (type)));
6378 return false;
6381 /* Used by find_flexarrays and related functions. */
6383 struct flexmems_t
6385 /* The first flexible array member or non-zero array member found
6386 in the order of layout. */
6387 tree array;
6388 /* First non-static non-empty data member in the class or its bases. */
6389 tree first;
6390 /* The first non-static non-empty data member following either
6391 the flexible array member, if found, or the zero-length array member
6392 otherwise. AFTER[1] refers to the first such data member of a union
6393 of which the struct containing the flexible array member or zero-length
6394 array is a member, or NULL when no such union exists. This element is
6395 only used during searching, not for diagnosing problems. AFTER[0]
6396 refers to the first such data member that is not a member of such
6397 a union. */
6398 tree after[2];
6400 /* Refers to a struct (not union) in which the struct of which the flexible
6401 array is member is defined. Used to diagnose strictly (according to C)
6402 invalid uses of the latter structs. */
6403 tree enclosing;
6406 /* Find either the first flexible array member or the first zero-length
6407 array, in that order of preference, among members of class T (but not
6408 its base classes), and set members of FMEM accordingly.
6409 BASE_P is true if T is a base class of another class.
6410 PUN is set to the outermost union in which the flexible array member
6411 (or zero-length array) is defined if one such union exists, otherwise
6412 to NULL.
6413 Similarly, PSTR is set to a data member of the outermost struct of
6414 which the flexible array is a member if one such struct exists,
6415 otherwise to NULL. */
6417 static void
6418 find_flexarrays (tree t, flexmems_t *fmem, bool base_p,
6419 tree pun /* = NULL_TREE */,
6420 tree pstr /* = NULL_TREE */)
6422 /* Set the "pointer" to the outermost enclosing union if not set
6423 yet and maintain it for the remainder of the recursion. */
6424 if (!pun && TREE_CODE (t) == UNION_TYPE)
6425 pun = t;
6427 for (tree fld = TYPE_FIELDS (t); fld; fld = DECL_CHAIN (fld))
6429 if (fld == error_mark_node)
6430 return;
6432 /* Is FLD a typedef for an anonymous struct? */
6434 /* FIXME: Note that typedefs (as well as arrays) need to be fully
6435 handled elsewhere so that errors like the following are detected
6436 as well:
6437 typedef struct { int i, a[], j; } S; // bug c++/72753
6438 S s [2]; // bug c++/68489
6440 if (TREE_CODE (fld) == TYPE_DECL
6441 && DECL_IMPLICIT_TYPEDEF_P (fld)
6442 && CLASS_TYPE_P (TREE_TYPE (fld))
6443 && anon_aggrname_p (DECL_NAME (fld)))
6445 /* Check the nested unnamed type referenced via a typedef
6446 independently of FMEM (since it's not a data member of
6447 the enclosing class). */
6448 check_flexarrays (TREE_TYPE (fld));
6449 continue;
6452 /* Skip anything that's GCC-generated or not a (non-static) data
6453 member. */
6454 if (DECL_ARTIFICIAL (fld) || TREE_CODE (fld) != FIELD_DECL)
6455 continue;
6457 /* Type of the member. */
6458 tree fldtype = TREE_TYPE (fld);
6459 if (fldtype == error_mark_node)
6460 return;
6462 /* Determine the type of the array element or object referenced
6463 by the member so that it can be checked for flexible array
6464 members if it hasn't been yet. */
6465 tree eltype = fldtype;
6466 while (TREE_CODE (eltype) == ARRAY_TYPE
6467 || TREE_CODE (eltype) == POINTER_TYPE
6468 || TREE_CODE (eltype) == REFERENCE_TYPE)
6469 eltype = TREE_TYPE (eltype);
6471 if (RECORD_OR_UNION_TYPE_P (eltype))
6473 if (fmem->array && !fmem->after[bool (pun)])
6475 /* Once the member after the flexible array has been found
6476 we're done. */
6477 fmem->after[bool (pun)] = fld;
6478 break;
6481 if (eltype == fldtype || TYPE_UNNAMED_P (eltype))
6483 /* Descend into the non-static member struct or union and try
6484 to find a flexible array member or zero-length array among
6485 its members. This is only necessary for anonymous types
6486 and types in whose context the current type T has not been
6487 defined (the latter must not be checked again because they
6488 are already in the process of being checked by one of the
6489 recursive calls). */
6491 tree first = fmem->first;
6492 tree array = fmem->array;
6494 /* If this member isn't anonymous and a prior non-flexible array
6495 member has been seen in one of the enclosing structs, clear
6496 the FIRST member since it doesn't contribute to the flexible
6497 array struct's members. */
6498 if (first && !array && !ANON_AGGR_TYPE_P (eltype))
6499 fmem->first = NULL_TREE;
6501 find_flexarrays (eltype, fmem, false, pun,
6502 !pstr && TREE_CODE (t) == RECORD_TYPE ? fld : pstr);
6504 if (fmem->array != array)
6505 continue;
6507 if (first && !array && !ANON_AGGR_TYPE_P (eltype))
6509 /* Restore the FIRST member reset above if no flexible
6510 array member has been found in this member's struct. */
6511 fmem->first = first;
6514 /* If the member struct contains the first flexible array
6515 member, or if this member is a base class, continue to
6516 the next member and avoid setting the FMEM->NEXT pointer
6517 to point to it. */
6518 if (base_p)
6519 continue;
6523 if (field_nonempty_p (fld))
6525 /* Remember the first non-static data member. */
6526 if (!fmem->first)
6527 fmem->first = fld;
6529 /* Remember the first non-static data member after the flexible
6530 array member, if one has been found, or the zero-length array
6531 if it has been found. */
6532 if (fmem->array && !fmem->after[bool (pun)])
6533 fmem->after[bool (pun)] = fld;
6536 /* Skip non-arrays. */
6537 if (TREE_CODE (fldtype) != ARRAY_TYPE)
6538 continue;
6540 /* Determine the upper bound of the array if it has one. */
6541 if (TYPE_DOMAIN (fldtype))
6543 if (fmem->array)
6545 /* Make a record of the zero-length array if either one
6546 such field or a flexible array member has been seen to
6547 handle the pathological and unlikely case of multiple
6548 such members. */
6549 if (!fmem->after[bool (pun)])
6550 fmem->after[bool (pun)] = fld;
6552 else if (integer_all_onesp (TYPE_MAX_VALUE (TYPE_DOMAIN (fldtype))))
6554 /* Remember the first zero-length array unless a flexible array
6555 member has already been seen. */
6556 fmem->array = fld;
6557 fmem->enclosing = pstr;
6560 else
6562 /* Flexible array members have no upper bound. */
6563 if (fmem->array)
6565 /* Replace the zero-length array if it's been stored and
6566 reset the after pointer. */
6567 if (TYPE_DOMAIN (TREE_TYPE (fmem->array)))
6569 fmem->after[bool (pun)] = NULL_TREE;
6570 fmem->array = fld;
6571 fmem->enclosing = pstr;
6574 else
6576 fmem->array = fld;
6577 fmem->enclosing = pstr;
6583 /* Diagnose a strictly (by the C standard) invalid use of a struct with
6584 a flexible array member (or the zero-length array extension). */
6586 static void
6587 diagnose_invalid_flexarray (const flexmems_t *fmem)
6589 if (fmem->array && fmem->enclosing
6590 && pedwarn (location_of (fmem->enclosing), OPT_Wpedantic,
6591 TYPE_DOMAIN (TREE_TYPE (fmem->array))
6592 ? G_("invalid use of %q#T with a zero-size array "
6593 "in %q#D")
6594 : G_("invalid use of %q#T with a flexible array member "
6595 "in %q#T"),
6596 DECL_CONTEXT (fmem->array),
6597 DECL_CONTEXT (fmem->enclosing)))
6598 inform (DECL_SOURCE_LOCATION (fmem->array),
6599 "array member %q#D declared here", fmem->array);
6602 /* Issue diagnostics for invalid flexible array members or zero-length
6603 arrays that are not the last elements of the containing class or its
6604 base classes or that are its sole members. */
6606 static void
6607 diagnose_flexarrays (tree t, const flexmems_t *fmem)
6609 if (!fmem->array)
6610 return;
6612 if (fmem->first && !fmem->after[0])
6614 diagnose_invalid_flexarray (fmem);
6615 return;
6618 /* Has a diagnostic been issued? */
6619 bool diagd = false;
6621 const char *msg = 0;
6623 if (TYPE_DOMAIN (TREE_TYPE (fmem->array)))
6625 if (fmem->after[0])
6626 msg = G_("zero-size array member %qD not at end of %q#T");
6627 else if (!fmem->first)
6628 msg = G_("zero-size array member %qD in an otherwise empty %q#T");
6630 if (msg)
6632 location_t loc = DECL_SOURCE_LOCATION (fmem->array);
6634 if (pedwarn (loc, OPT_Wpedantic, msg, fmem->array, t))
6636 inform (location_of (t), "in the definition of %q#T", t);
6637 diagd = true;
6641 else
6643 if (fmem->after[0])
6644 msg = G_("flexible array member %qD not at end of %q#T");
6645 else if (!fmem->first)
6646 msg = G_("flexible array member %qD in an otherwise empty %q#T");
6648 if (msg)
6650 location_t loc = DECL_SOURCE_LOCATION (fmem->array);
6651 diagd = true;
6653 error_at (loc, msg, fmem->array, t);
6655 /* In the unlikely event that the member following the flexible
6656 array member is declared in a different class, or the member
6657 overlaps another member of a common union, point to it.
6658 Otherwise it should be obvious. */
6659 if (fmem->after[0]
6660 && ((DECL_CONTEXT (fmem->after[0])
6661 != DECL_CONTEXT (fmem->array))))
6663 inform (DECL_SOURCE_LOCATION (fmem->after[0]),
6664 "next member %q#D declared here",
6665 fmem->after[0]);
6666 inform (location_of (t), "in the definition of %q#T", t);
6671 if (!diagd && fmem->array && fmem->enclosing)
6672 diagnose_invalid_flexarray (fmem);
6676 /* Recursively check to make sure that any flexible array or zero-length
6677 array members of class T or its bases are valid (i.e., not the sole
6678 non-static data member of T and, if one exists, that it is the last
6679 non-static data member of T and its base classes. FMEM is expected
6680 to be initially null and is used internally by recursive calls to
6681 the function. Issue the appropriate diagnostics for the array member
6682 that fails the checks. */
6684 static void
6685 check_flexarrays (tree t, flexmems_t *fmem /* = NULL */,
6686 bool base_p /* = false */)
6688 /* Initialize the result of a search for flexible array and zero-length
6689 array members. Avoid doing any work if the most interesting FMEM data
6690 have already been populated. */
6691 flexmems_t flexmems = flexmems_t ();
6692 if (!fmem)
6693 fmem = &flexmems;
6694 else if (fmem->array && fmem->first && fmem->after[0])
6695 return;
6697 tree fam = fmem->array;
6699 /* Recursively check the primary base class first. */
6700 if (CLASSTYPE_HAS_PRIMARY_BASE_P (t))
6702 tree basetype = BINFO_TYPE (CLASSTYPE_PRIMARY_BINFO (t));
6703 check_flexarrays (basetype, fmem, true);
6706 /* Recursively check the base classes. */
6707 int nbases = TYPE_BINFO (t) ? BINFO_N_BASE_BINFOS (TYPE_BINFO (t)) : 0;
6708 for (int i = 0; i < nbases; ++i)
6710 tree base_binfo = BINFO_BASE_BINFO (TYPE_BINFO (t), i);
6712 /* The primary base class was already checked above. */
6713 if (base_binfo == CLASSTYPE_PRIMARY_BINFO (t))
6714 continue;
6716 /* Virtual base classes are at the end. */
6717 if (BINFO_VIRTUAL_P (base_binfo))
6718 continue;
6720 /* Check the base class. */
6721 check_flexarrays (BINFO_TYPE (base_binfo), fmem, /*base_p=*/true);
6724 if (fmem == &flexmems)
6726 /* Check virtual base classes only once per derived class.
6727 I.e., this check is not performed recursively for base
6728 classes. */
6729 int i;
6730 tree base_binfo;
6731 vec<tree, va_gc> *vbases;
6732 for (vbases = CLASSTYPE_VBASECLASSES (t), i = 0;
6733 vec_safe_iterate (vbases, i, &base_binfo); i++)
6735 /* Check the virtual base class. */
6736 tree basetype = TREE_TYPE (base_binfo);
6738 check_flexarrays (basetype, fmem, /*base_p=*/true);
6742 /* Is the type unnamed (and therefore a member of it potentially
6743 an anonymous struct or union)? */
6744 bool maybe_anon_p = TYPE_UNNAMED_P (t);
6746 /* Search the members of the current (possibly derived) class, skipping
6747 unnamed structs and unions since those could be anonymous. */
6748 if (fmem != &flexmems || !maybe_anon_p)
6749 find_flexarrays (t, fmem, base_p || fam != fmem->array);
6751 if (fmem == &flexmems && !maybe_anon_p)
6753 /* Issue diagnostics for invalid flexible and zero-length array
6754 members found in base classes or among the members of the current
6755 class. Ignore anonymous structs and unions whose members are
6756 considered to be members of the enclosing class and thus will
6757 be diagnosed when checking it. */
6758 diagnose_flexarrays (t, fmem);
6762 /* Perform processing required when the definition of T (a class type)
6763 is complete. Diagnose invalid definitions of flexible array members
6764 and zero-size arrays. */
6766 void
6767 finish_struct_1 (tree t)
6769 tree x;
6770 /* A TREE_LIST. The TREE_VALUE of each node is a FUNCTION_DECL. */
6771 tree virtuals = NULL_TREE;
6773 if (COMPLETE_TYPE_P (t))
6775 gcc_assert (MAYBE_CLASS_TYPE_P (t));
6776 error ("redefinition of %q#T", t);
6777 popclass ();
6778 return;
6781 /* If this type was previously laid out as a forward reference,
6782 make sure we lay it out again. */
6783 TYPE_SIZE (t) = NULL_TREE;
6784 CLASSTYPE_PRIMARY_BINFO (t) = NULL_TREE;
6786 /* Make assumptions about the class; we'll reset the flags if
6787 necessary. */
6788 CLASSTYPE_EMPTY_P (t) = 1;
6789 CLASSTYPE_NEARLY_EMPTY_P (t) = 1;
6790 CLASSTYPE_CONTAINS_EMPTY_CLASS_P (t) = 0;
6791 CLASSTYPE_LITERAL_P (t) = true;
6793 /* Do end-of-class semantic processing: checking the validity of the
6794 bases and members and add implicitly generated methods. */
6795 check_bases_and_members (t);
6797 /* Find the key method. */
6798 if (TYPE_CONTAINS_VPTR_P (t))
6800 /* The Itanium C++ ABI permits the key method to be chosen when
6801 the class is defined -- even though the key method so
6802 selected may later turn out to be an inline function. On
6803 some systems (such as ARM Symbian OS) the key method cannot
6804 be determined until the end of the translation unit. On such
6805 systems, we leave CLASSTYPE_KEY_METHOD set to NULL, which
6806 will cause the class to be added to KEYED_CLASSES. Then, in
6807 finish_file we will determine the key method. */
6808 if (targetm.cxx.key_method_may_be_inline ())
6809 determine_key_method (t);
6811 /* If a polymorphic class has no key method, we may emit the vtable
6812 in every translation unit where the class definition appears. If
6813 we're devirtualizing, we can look into the vtable even if we
6814 aren't emitting it. */
6815 if (!CLASSTYPE_KEY_METHOD (t))
6816 vec_safe_push (keyed_classes, t);
6819 /* Layout the class itself. */
6820 layout_class_type (t, &virtuals);
6821 /* COMPLETE_TYPE_P is now true. */
6823 set_class_bindings (t);
6825 if (CLASSTYPE_AS_BASE (t) != t)
6826 /* We use the base type for trivial assignments, and hence it
6827 needs a mode. */
6828 compute_record_mode (CLASSTYPE_AS_BASE (t));
6830 /* With the layout complete, check for flexible array members and
6831 zero-length arrays that might overlap other members in the final
6832 layout. */
6833 check_flexarrays (t);
6835 virtuals = modify_all_vtables (t, nreverse (virtuals));
6837 /* If necessary, create the primary vtable for this class. */
6838 if (virtuals || TYPE_CONTAINS_VPTR_P (t))
6840 /* We must enter these virtuals into the table. */
6841 if (!CLASSTYPE_HAS_PRIMARY_BASE_P (t))
6842 build_primary_vtable (NULL_TREE, t);
6843 else if (! BINFO_NEW_VTABLE_MARKED (TYPE_BINFO (t)))
6844 /* Here we know enough to change the type of our virtual
6845 function table, but we will wait until later this function. */
6846 build_primary_vtable (CLASSTYPE_PRIMARY_BINFO (t), t);
6848 /* If we're warning about ABI tags, check the types of the new
6849 virtual functions. */
6850 if (warn_abi_tag)
6851 for (tree v = virtuals; v; v = TREE_CHAIN (v))
6852 check_abi_tags (t, TREE_VALUE (v));
6855 if (TYPE_CONTAINS_VPTR_P (t))
6857 int vindex;
6858 tree fn;
6860 if (BINFO_VTABLE (TYPE_BINFO (t)))
6861 gcc_assert (DECL_VIRTUAL_P (BINFO_VTABLE (TYPE_BINFO (t))));
6862 if (!CLASSTYPE_HAS_PRIMARY_BASE_P (t))
6863 gcc_assert (BINFO_VIRTUALS (TYPE_BINFO (t)) == NULL_TREE);
6865 /* Add entries for virtual functions introduced by this class. */
6866 BINFO_VIRTUALS (TYPE_BINFO (t))
6867 = chainon (BINFO_VIRTUALS (TYPE_BINFO (t)), virtuals);
6869 /* Set DECL_VINDEX for all functions declared in this class. */
6870 for (vindex = 0, fn = BINFO_VIRTUALS (TYPE_BINFO (t));
6872 fn = TREE_CHAIN (fn),
6873 vindex += (TARGET_VTABLE_USES_DESCRIPTORS
6874 ? TARGET_VTABLE_USES_DESCRIPTORS : 1))
6876 tree fndecl = BV_FN (fn);
6878 if (DECL_THUNK_P (fndecl))
6879 /* A thunk. We should never be calling this entry directly
6880 from this vtable -- we'd use the entry for the non
6881 thunk base function. */
6882 DECL_VINDEX (fndecl) = NULL_TREE;
6883 else if (TREE_CODE (DECL_VINDEX (fndecl)) != INTEGER_CST)
6884 DECL_VINDEX (fndecl) = build_int_cst (NULL_TREE, vindex);
6888 finish_struct_bits (t);
6890 set_method_tm_attributes (t);
6891 if (flag_openmp || flag_openmp_simd)
6892 finish_omp_declare_simd_methods (t);
6894 /* Clear DECL_IN_AGGR_P for all member functions. Complete the rtl
6895 for any static member objects of the type we're working on. */
6896 for (x = TYPE_FIELDS (t); x; x = DECL_CHAIN (x))
6897 if (DECL_DECLARES_FUNCTION_P (x))
6898 DECL_IN_AGGR_P (x) = false;
6899 else if (VAR_P (x) && TREE_STATIC (x)
6900 && TREE_TYPE (x) != error_mark_node
6901 && same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (x)), t))
6902 SET_DECL_MODE (x, TYPE_MODE (t));
6904 /* Complain if one of the field types requires lower visibility. */
6905 constrain_class_visibility (t);
6907 /* Make the rtl for any new vtables we have created, and unmark
6908 the base types we marked. */
6909 finish_vtbls (t);
6911 /* Build the VTT for T. */
6912 build_vtt (t);
6914 if (warn_nonvdtor
6915 && TYPE_POLYMORPHIC_P (t) && accessible_nvdtor_p (t)
6916 && !CLASSTYPE_FINAL (t))
6917 warning (OPT_Wnon_virtual_dtor,
6918 "%q#T has virtual functions and accessible"
6919 " non-virtual destructor", t);
6921 complete_vars (t);
6923 if (warn_overloaded_virtual)
6924 warn_hidden (t);
6926 /* Class layout, assignment of virtual table slots, etc., is now
6927 complete. Give the back end a chance to tweak the visibility of
6928 the class or perform any other required target modifications. */
6929 targetm.cxx.adjust_class_at_definition (t);
6931 maybe_suppress_debug_info (t);
6933 if (flag_vtable_verify)
6934 vtv_save_class_info (t);
6936 dump_class_hierarchy (t);
6938 /* Finish debugging output for this type. */
6939 rest_of_type_compilation (t, ! LOCAL_CLASS_P (t));
6941 if (TYPE_TRANSPARENT_AGGR (t))
6943 tree field = first_field (t);
6944 if (field == NULL_TREE || error_operand_p (field))
6946 error ("type transparent %q#T does not have any fields", t);
6947 TYPE_TRANSPARENT_AGGR (t) = 0;
6949 else if (DECL_ARTIFICIAL (field))
6951 if (DECL_FIELD_IS_BASE (field))
6952 error ("type transparent class %qT has base classes", t);
6953 else
6955 gcc_checking_assert (DECL_VIRTUAL_P (field));
6956 error ("type transparent class %qT has virtual functions", t);
6958 TYPE_TRANSPARENT_AGGR (t) = 0;
6960 else if (TYPE_MODE (t) != DECL_MODE (field))
6962 error ("type transparent %q#T cannot be made transparent because "
6963 "the type of the first field has a different ABI from the "
6964 "class overall", t);
6965 TYPE_TRANSPARENT_AGGR (t) = 0;
6970 /* When T was built up, the member declarations were added in reverse
6971 order. Rearrange them to declaration order. */
6973 void
6974 unreverse_member_declarations (tree t)
6976 tree next;
6977 tree prev;
6978 tree x;
6980 /* The following lists are all in reverse order. Put them in
6981 declaration order now. */
6982 CLASSTYPE_DECL_LIST (t) = nreverse (CLASSTYPE_DECL_LIST (t));
6984 /* For the TYPE_FIELDS, only the non TYPE_DECLs are in reverse
6985 order, so we can't just use nreverse. Due to stat_hack
6986 chicanery in finish_member_declaration. */
6987 prev = NULL_TREE;
6988 for (x = TYPE_FIELDS (t);
6989 x && TREE_CODE (x) != TYPE_DECL;
6990 x = next)
6992 next = DECL_CHAIN (x);
6993 DECL_CHAIN (x) = prev;
6994 prev = x;
6997 if (prev)
6999 DECL_CHAIN (TYPE_FIELDS (t)) = x;
7000 TYPE_FIELDS (t) = prev;
7004 tree
7005 finish_struct (tree t, tree attributes)
7007 location_t saved_loc = input_location;
7009 /* Now that we've got all the field declarations, reverse everything
7010 as necessary. */
7011 unreverse_member_declarations (t);
7013 cplus_decl_attributes (&t, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
7014 fixup_attribute_variants (t);
7016 /* Nadger the current location so that diagnostics point to the start of
7017 the struct, not the end. */
7018 input_location = DECL_SOURCE_LOCATION (TYPE_NAME (t));
7020 if (processing_template_decl)
7022 tree x;
7024 /* We need to add the target functions of USING_DECLS, so that
7025 they can be found when the using declaration is not
7026 instantiated yet. */
7027 for (x = TYPE_FIELDS (t); x; x = DECL_CHAIN (x))
7028 if (TREE_CODE (x) == USING_DECL)
7030 tree fn = strip_using_decl (x);
7031 if (OVL_P (fn))
7032 for (lkp_iterator iter (fn); iter; ++iter)
7033 add_method (t, *iter, true);
7035 else if (DECL_DECLARES_FUNCTION_P (x))
7036 DECL_IN_AGGR_P (x) = false;
7038 TYPE_SIZE (t) = bitsize_zero_node;
7039 TYPE_SIZE_UNIT (t) = size_zero_node;
7040 /* COMPLETE_TYPE_P is now true. */
7042 set_class_bindings (t);
7044 /* We need to emit an error message if this type was used as a parameter
7045 and it is an abstract type, even if it is a template. We construct
7046 a simple CLASSTYPE_PURE_VIRTUALS list without taking bases into
7047 account and we call complete_vars with this type, which will check
7048 the PARM_DECLS. Note that while the type is being defined,
7049 CLASSTYPE_PURE_VIRTUALS contains the list of the inline friends
7050 (see CLASSTYPE_INLINE_FRIENDS) so we need to clear it. */
7051 CLASSTYPE_PURE_VIRTUALS (t) = NULL;
7052 for (x = TYPE_FIELDS (t); x; x = DECL_CHAIN (x))
7053 if (TREE_CODE (x) == FUNCTION_DECL && DECL_PURE_VIRTUAL_P (x))
7054 vec_safe_push (CLASSTYPE_PURE_VIRTUALS (t), x);
7055 complete_vars (t);
7057 /* Remember current #pragma pack value. */
7058 TYPE_PRECISION (t) = maximum_field_alignment;
7060 /* Fix up any variants we've already built. */
7061 for (x = TYPE_NEXT_VARIANT (t); x; x = TYPE_NEXT_VARIANT (x))
7063 TYPE_SIZE (x) = TYPE_SIZE (t);
7064 TYPE_SIZE_UNIT (x) = TYPE_SIZE_UNIT (t);
7065 TYPE_FIELDS (x) = TYPE_FIELDS (t);
7068 else
7069 finish_struct_1 (t);
7070 /* COMPLETE_TYPE_P is now true. */
7072 maybe_warn_about_overly_private_class (t);
7074 if (is_std_init_list (t))
7076 /* People keep complaining that the compiler crashes on an invalid
7077 definition of initializer_list, so I guess we should explicitly
7078 reject it. What the compiler internals care about is that it's a
7079 template and has a pointer field followed by an integer field. */
7080 bool ok = false;
7081 if (processing_template_decl)
7083 tree f = next_initializable_field (TYPE_FIELDS (t));
7084 if (f && TREE_CODE (TREE_TYPE (f)) == POINTER_TYPE)
7086 f = next_initializable_field (DECL_CHAIN (f));
7087 if (f && same_type_p (TREE_TYPE (f), size_type_node))
7088 ok = true;
7091 if (!ok)
7092 fatal_error (input_location,
7093 "definition of std::initializer_list does not match "
7094 "#include <initializer_list>");
7097 input_location = saved_loc;
7099 TYPE_BEING_DEFINED (t) = 0;
7101 if (current_class_type)
7102 popclass ();
7103 else
7104 error ("trying to finish struct, but kicked out due to previous parse errors");
7106 if (processing_template_decl && at_function_scope_p ()
7107 /* Lambdas are defined by the LAMBDA_EXPR. */
7108 && !LAMBDA_TYPE_P (t))
7109 add_stmt (build_min (TAG_DEFN, t));
7111 return t;
7114 /* Hash table to avoid endless recursion when handling references. */
7115 static hash_table<nofree_ptr_hash<tree_node> > *fixed_type_or_null_ref_ht;
7117 /* Return the dynamic type of INSTANCE, if known.
7118 Used to determine whether the virtual function table is needed
7119 or not.
7121 *NONNULL is set iff INSTANCE can be known to be nonnull, regardless
7122 of our knowledge of its type. *NONNULL should be initialized
7123 before this function is called. */
7125 static tree
7126 fixed_type_or_null (tree instance, int *nonnull, int *cdtorp)
7128 #define RECUR(T) fixed_type_or_null((T), nonnull, cdtorp)
7130 switch (TREE_CODE (instance))
7132 case INDIRECT_REF:
7133 if (POINTER_TYPE_P (TREE_TYPE (instance)))
7134 return NULL_TREE;
7135 else
7136 return RECUR (TREE_OPERAND (instance, 0));
7138 case CALL_EXPR:
7139 /* This is a call to a constructor, hence it's never zero. */
7140 if (TREE_HAS_CONSTRUCTOR (instance))
7142 if (nonnull)
7143 *nonnull = 1;
7144 return TREE_TYPE (instance);
7146 return NULL_TREE;
7148 case SAVE_EXPR:
7149 /* This is a call to a constructor, hence it's never zero. */
7150 if (TREE_HAS_CONSTRUCTOR (instance))
7152 if (nonnull)
7153 *nonnull = 1;
7154 return TREE_TYPE (instance);
7156 return RECUR (TREE_OPERAND (instance, 0));
7158 case POINTER_PLUS_EXPR:
7159 case PLUS_EXPR:
7160 case MINUS_EXPR:
7161 if (TREE_CODE (TREE_OPERAND (instance, 0)) == ADDR_EXPR)
7162 return RECUR (TREE_OPERAND (instance, 0));
7163 if (TREE_CODE (TREE_OPERAND (instance, 1)) == INTEGER_CST)
7164 /* Propagate nonnull. */
7165 return RECUR (TREE_OPERAND (instance, 0));
7167 return NULL_TREE;
7169 CASE_CONVERT:
7170 return RECUR (TREE_OPERAND (instance, 0));
7172 case ADDR_EXPR:
7173 instance = TREE_OPERAND (instance, 0);
7174 if (nonnull)
7176 /* Just because we see an ADDR_EXPR doesn't mean we're dealing
7177 with a real object -- given &p->f, p can still be null. */
7178 tree t = get_base_address (instance);
7179 /* ??? Probably should check DECL_WEAK here. */
7180 if (t && DECL_P (t))
7181 *nonnull = 1;
7183 return RECUR (instance);
7185 case COMPONENT_REF:
7186 /* If this component is really a base class reference, then the field
7187 itself isn't definitive. */
7188 if (DECL_FIELD_IS_BASE (TREE_OPERAND (instance, 1)))
7189 return RECUR (TREE_OPERAND (instance, 0));
7190 return RECUR (TREE_OPERAND (instance, 1));
7192 case VAR_DECL:
7193 case FIELD_DECL:
7194 if (TREE_CODE (TREE_TYPE (instance)) == ARRAY_TYPE
7195 && MAYBE_CLASS_TYPE_P (TREE_TYPE (TREE_TYPE (instance))))
7197 if (nonnull)
7198 *nonnull = 1;
7199 return TREE_TYPE (TREE_TYPE (instance));
7201 /* fall through. */
7202 case TARGET_EXPR:
7203 case PARM_DECL:
7204 case RESULT_DECL:
7205 if (MAYBE_CLASS_TYPE_P (TREE_TYPE (instance)))
7207 if (nonnull)
7208 *nonnull = 1;
7209 return TREE_TYPE (instance);
7211 else if (instance == current_class_ptr)
7213 if (nonnull)
7214 *nonnull = 1;
7216 /* if we're in a ctor or dtor, we know our type. If
7217 current_class_ptr is set but we aren't in a function, we're in
7218 an NSDMI (and therefore a constructor). */
7219 if (current_scope () != current_function_decl
7220 || (DECL_LANG_SPECIFIC (current_function_decl)
7221 && (DECL_CONSTRUCTOR_P (current_function_decl)
7222 || DECL_DESTRUCTOR_P (current_function_decl))))
7224 if (cdtorp)
7225 *cdtorp = 1;
7226 return TREE_TYPE (TREE_TYPE (instance));
7229 else if (TREE_CODE (TREE_TYPE (instance)) == REFERENCE_TYPE)
7231 /* We only need one hash table because it is always left empty. */
7232 if (!fixed_type_or_null_ref_ht)
7233 fixed_type_or_null_ref_ht
7234 = new hash_table<nofree_ptr_hash<tree_node> > (37);
7236 /* Reference variables should be references to objects. */
7237 if (nonnull)
7238 *nonnull = 1;
7240 /* Enter the INSTANCE in a table to prevent recursion; a
7241 variable's initializer may refer to the variable
7242 itself. */
7243 if (VAR_P (instance)
7244 && DECL_INITIAL (instance)
7245 && !type_dependent_expression_p_push (DECL_INITIAL (instance))
7246 && !fixed_type_or_null_ref_ht->find (instance))
7248 tree type;
7249 tree_node **slot;
7251 slot = fixed_type_or_null_ref_ht->find_slot (instance, INSERT);
7252 *slot = instance;
7253 type = RECUR (DECL_INITIAL (instance));
7254 fixed_type_or_null_ref_ht->remove_elt (instance);
7256 return type;
7259 return NULL_TREE;
7261 default:
7262 return NULL_TREE;
7264 #undef RECUR
7267 /* Return nonzero if the dynamic type of INSTANCE is known, and
7268 equivalent to the static type. We also handle the case where
7269 INSTANCE is really a pointer. Return negative if this is a
7270 ctor/dtor. There the dynamic type is known, but this might not be
7271 the most derived base of the original object, and hence virtual
7272 bases may not be laid out according to this type.
7274 Used to determine whether the virtual function table is needed
7275 or not.
7277 *NONNULL is set iff INSTANCE can be known to be nonnull, regardless
7278 of our knowledge of its type. *NONNULL should be initialized
7279 before this function is called. */
7282 resolves_to_fixed_type_p (tree instance, int* nonnull)
7284 tree t = TREE_TYPE (instance);
7285 int cdtorp = 0;
7286 tree fixed;
7288 /* processing_template_decl can be false in a template if we're in
7289 instantiate_non_dependent_expr, but we still want to suppress
7290 this check. */
7291 if (in_template_function ())
7293 /* In a template we only care about the type of the result. */
7294 if (nonnull)
7295 *nonnull = true;
7296 return true;
7299 fixed = fixed_type_or_null (instance, nonnull, &cdtorp);
7300 if (fixed == NULL_TREE)
7301 return 0;
7302 if (POINTER_TYPE_P (t))
7303 t = TREE_TYPE (t);
7304 if (!same_type_ignoring_top_level_qualifiers_p (t, fixed))
7305 return 0;
7306 return cdtorp ? -1 : 1;
7310 void
7311 init_class_processing (void)
7313 current_class_depth = 0;
7314 current_class_stack_size = 10;
7315 current_class_stack
7316 = XNEWVEC (struct class_stack_node, current_class_stack_size);
7317 vec_alloc (local_classes, 8);
7318 sizeof_biggest_empty_class = size_zero_node;
7320 ridpointers[(int) RID_PUBLIC] = access_public_node;
7321 ridpointers[(int) RID_PRIVATE] = access_private_node;
7322 ridpointers[(int) RID_PROTECTED] = access_protected_node;
7325 /* Restore the cached PREVIOUS_CLASS_LEVEL. */
7327 static void
7328 restore_class_cache (void)
7330 tree type;
7332 /* We are re-entering the same class we just left, so we don't
7333 have to search the whole inheritance matrix to find all the
7334 decls to bind again. Instead, we install the cached
7335 class_shadowed list and walk through it binding names. */
7336 push_binding_level (previous_class_level);
7337 class_binding_level = previous_class_level;
7338 /* Restore IDENTIFIER_TYPE_VALUE. */
7339 for (type = class_binding_level->type_shadowed;
7340 type;
7341 type = TREE_CHAIN (type))
7342 SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (type), TREE_TYPE (type));
7345 /* Set global variables CURRENT_CLASS_NAME and CURRENT_CLASS_TYPE as
7346 appropriate for TYPE.
7348 So that we may avoid calls to lookup_name, we cache the _TYPE
7349 nodes of local TYPE_DECLs in the TREE_TYPE field of the name.
7351 For multiple inheritance, we perform a two-pass depth-first search
7352 of the type lattice. */
7354 void
7355 pushclass (tree type)
7357 class_stack_node_t csn;
7359 type = TYPE_MAIN_VARIANT (type);
7361 /* Make sure there is enough room for the new entry on the stack. */
7362 if (current_class_depth + 1 >= current_class_stack_size)
7364 current_class_stack_size *= 2;
7365 current_class_stack
7366 = XRESIZEVEC (struct class_stack_node, current_class_stack,
7367 current_class_stack_size);
7370 /* Insert a new entry on the class stack. */
7371 csn = current_class_stack + current_class_depth;
7372 csn->name = current_class_name;
7373 csn->type = current_class_type;
7374 csn->access = current_access_specifier;
7375 csn->names_used = 0;
7376 csn->hidden = 0;
7377 current_class_depth++;
7379 /* Now set up the new type. */
7380 current_class_name = TYPE_NAME (type);
7381 if (TREE_CODE (current_class_name) == TYPE_DECL)
7382 current_class_name = DECL_NAME (current_class_name);
7383 current_class_type = type;
7385 /* By default, things in classes are private, while things in
7386 structures or unions are public. */
7387 current_access_specifier = (CLASSTYPE_DECLARED_CLASS (type)
7388 ? access_private_node
7389 : access_public_node);
7391 if (previous_class_level
7392 && type != previous_class_level->this_entity
7393 && current_class_depth == 1)
7395 /* Forcibly remove any old class remnants. */
7396 invalidate_class_lookup_cache ();
7399 if (!previous_class_level
7400 || type != previous_class_level->this_entity
7401 || current_class_depth > 1)
7402 pushlevel_class ();
7403 else
7404 restore_class_cache ();
7407 /* When we exit a toplevel class scope, we save its binding level so
7408 that we can restore it quickly. Here, we've entered some other
7409 class, so we must invalidate our cache. */
7411 void
7412 invalidate_class_lookup_cache (void)
7414 previous_class_level = NULL;
7417 /* Get out of the current class scope. If we were in a class scope
7418 previously, that is the one popped to. */
7420 void
7421 popclass (void)
7423 poplevel_class ();
7425 current_class_depth--;
7426 current_class_name = current_class_stack[current_class_depth].name;
7427 current_class_type = current_class_stack[current_class_depth].type;
7428 current_access_specifier = current_class_stack[current_class_depth].access;
7429 if (current_class_stack[current_class_depth].names_used)
7430 splay_tree_delete (current_class_stack[current_class_depth].names_used);
7433 /* Mark the top of the class stack as hidden. */
7435 void
7436 push_class_stack (void)
7438 if (current_class_depth)
7439 ++current_class_stack[current_class_depth - 1].hidden;
7442 /* Mark the top of the class stack as un-hidden. */
7444 void
7445 pop_class_stack (void)
7447 if (current_class_depth)
7448 --current_class_stack[current_class_depth - 1].hidden;
7451 /* Returns 1 if the class type currently being defined is either T or
7452 a nested type of T. Returns the type from the current_class_stack,
7453 which might be equivalent to but not equal to T in case of
7454 constrained partial specializations. */
7456 tree
7457 currently_open_class (tree t)
7459 int i;
7461 if (!CLASS_TYPE_P (t))
7462 return NULL_TREE;
7464 t = TYPE_MAIN_VARIANT (t);
7466 /* We start looking from 1 because entry 0 is from global scope,
7467 and has no type. */
7468 for (i = current_class_depth; i > 0; --i)
7470 tree c;
7471 if (i == current_class_depth)
7472 c = current_class_type;
7473 else
7475 if (current_class_stack[i].hidden)
7476 break;
7477 c = current_class_stack[i].type;
7479 if (!c)
7480 continue;
7481 if (same_type_p (c, t))
7482 return c;
7484 return NULL_TREE;
7487 /* If either current_class_type or one of its enclosing classes are derived
7488 from T, return the appropriate type. Used to determine how we found
7489 something via unqualified lookup. */
7491 tree
7492 currently_open_derived_class (tree t)
7494 int i;
7496 /* The bases of a dependent type are unknown. */
7497 if (dependent_type_p (t))
7498 return NULL_TREE;
7500 if (!current_class_type)
7501 return NULL_TREE;
7503 if (DERIVED_FROM_P (t, current_class_type))
7504 return current_class_type;
7506 for (i = current_class_depth - 1; i > 0; --i)
7508 if (current_class_stack[i].hidden)
7509 break;
7510 if (DERIVED_FROM_P (t, current_class_stack[i].type))
7511 return current_class_stack[i].type;
7514 return NULL_TREE;
7517 /* Return the outermost enclosing class type that is still open, or
7518 NULL_TREE. */
7520 tree
7521 outermost_open_class (void)
7523 if (!current_class_type)
7524 return NULL_TREE;
7525 tree r = NULL_TREE;
7526 if (TYPE_BEING_DEFINED (current_class_type))
7527 r = current_class_type;
7528 for (int i = current_class_depth - 1; i > 0; --i)
7530 if (current_class_stack[i].hidden)
7531 break;
7532 tree t = current_class_stack[i].type;
7533 if (!TYPE_BEING_DEFINED (t))
7534 break;
7535 r = t;
7537 return r;
7540 /* Returns the innermost class type which is not a lambda closure type. */
7542 tree
7543 current_nonlambda_class_type (void)
7545 tree type = current_class_type;
7546 while (type && LAMBDA_TYPE_P (type))
7547 type = decl_type_context (TYPE_NAME (type));
7548 return type;
7551 /* When entering a class scope, all enclosing class scopes' names with
7552 static meaning (static variables, static functions, types and
7553 enumerators) have to be visible. This recursive function calls
7554 pushclass for all enclosing class contexts until global or a local
7555 scope is reached. TYPE is the enclosed class. */
7557 void
7558 push_nested_class (tree type)
7560 /* A namespace might be passed in error cases, like A::B:C. */
7561 if (type == NULL_TREE
7562 || !CLASS_TYPE_P (type))
7563 return;
7565 push_nested_class (DECL_CONTEXT (TYPE_MAIN_DECL (type)));
7567 pushclass (type);
7570 /* Undoes a push_nested_class call. */
7572 void
7573 pop_nested_class (void)
7575 tree context = DECL_CONTEXT (TYPE_MAIN_DECL (current_class_type));
7577 popclass ();
7578 if (context && CLASS_TYPE_P (context))
7579 pop_nested_class ();
7582 /* Returns the number of extern "LANG" blocks we are nested within. */
7585 current_lang_depth (void)
7587 return vec_safe_length (current_lang_base);
7590 /* Set global variables CURRENT_LANG_NAME to appropriate value
7591 so that behavior of name-mangling machinery is correct. */
7593 void
7594 push_lang_context (tree name)
7596 vec_safe_push (current_lang_base, current_lang_name);
7598 if (name == lang_name_cplusplus)
7599 current_lang_name = name;
7600 else if (name == lang_name_c)
7601 current_lang_name = name;
7602 else
7603 error ("language string %<\"%E\"%> not recognized", name);
7606 /* Get out of the current language scope. */
7608 void
7609 pop_lang_context (void)
7611 current_lang_name = current_lang_base->pop ();
7614 /* Type instantiation routines. */
7616 /* Given an OVERLOAD and a TARGET_TYPE, return the function that
7617 matches the TARGET_TYPE. If there is no satisfactory match, return
7618 error_mark_node, and issue an error & warning messages under
7619 control of FLAGS. Permit pointers to member function if FLAGS
7620 permits. If TEMPLATE_ONLY, the name of the overloaded function was
7621 a template-id, and EXPLICIT_TARGS are the explicitly provided
7622 template arguments.
7624 If OVERLOAD is for one or more member functions, then ACCESS_PATH
7625 is the base path used to reference those member functions. If
7626 the address is resolved to a member function, access checks will be
7627 performed and errors issued if appropriate. */
7629 static tree
7630 resolve_address_of_overloaded_function (tree target_type,
7631 tree overload,
7632 tsubst_flags_t complain,
7633 bool template_only,
7634 tree explicit_targs,
7635 tree access_path)
7637 /* Here's what the standard says:
7639 [over.over]
7641 If the name is a function template, template argument deduction
7642 is done, and if the argument deduction succeeds, the deduced
7643 arguments are used to generate a single template function, which
7644 is added to the set of overloaded functions considered.
7646 Non-member functions and static member functions match targets of
7647 type "pointer-to-function" or "reference-to-function." Nonstatic
7648 member functions match targets of type "pointer-to-member
7649 function;" the function type of the pointer to member is used to
7650 select the member function from the set of overloaded member
7651 functions. If a nonstatic member function is selected, the
7652 reference to the overloaded function name is required to have the
7653 form of a pointer to member as described in 5.3.1.
7655 If more than one function is selected, any template functions in
7656 the set are eliminated if the set also contains a non-template
7657 function, and any given template function is eliminated if the
7658 set contains a second template function that is more specialized
7659 than the first according to the partial ordering rules 14.5.5.2.
7660 After such eliminations, if any, there shall remain exactly one
7661 selected function. */
7663 int is_ptrmem = 0;
7664 /* We store the matches in a TREE_LIST rooted here. The functions
7665 are the TREE_PURPOSE, not the TREE_VALUE, in this list, for easy
7666 interoperability with most_specialized_instantiation. */
7667 tree matches = NULL_TREE;
7668 tree fn;
7669 tree target_fn_type;
7671 /* By the time we get here, we should be seeing only real
7672 pointer-to-member types, not the internal POINTER_TYPE to
7673 METHOD_TYPE representation. */
7674 gcc_assert (!TYPE_PTR_P (target_type)
7675 || TREE_CODE (TREE_TYPE (target_type)) != METHOD_TYPE);
7677 gcc_assert (is_overloaded_fn (overload));
7679 /* Check that the TARGET_TYPE is reasonable. */
7680 if (TYPE_PTRFN_P (target_type)
7681 || TYPE_REFFN_P (target_type))
7682 /* This is OK. */;
7683 else if (TYPE_PTRMEMFUNC_P (target_type))
7684 /* This is OK, too. */
7685 is_ptrmem = 1;
7686 else if (TREE_CODE (target_type) == FUNCTION_TYPE)
7687 /* This is OK, too. This comes from a conversion to reference
7688 type. */
7689 target_type = build_reference_type (target_type);
7690 else
7692 if (complain & tf_error)
7693 error ("cannot resolve overloaded function %qD based on"
7694 " conversion to type %qT",
7695 OVL_NAME (overload), target_type);
7696 return error_mark_node;
7699 /* Non-member functions and static member functions match targets of type
7700 "pointer-to-function" or "reference-to-function." Nonstatic member
7701 functions match targets of type "pointer-to-member-function;" the
7702 function type of the pointer to member is used to select the member
7703 function from the set of overloaded member functions.
7705 So figure out the FUNCTION_TYPE that we want to match against. */
7706 target_fn_type = static_fn_type (target_type);
7708 /* If we can find a non-template function that matches, we can just
7709 use it. There's no point in generating template instantiations
7710 if we're just going to throw them out anyhow. But, of course, we
7711 can only do this when we don't *need* a template function. */
7712 if (!template_only)
7713 for (lkp_iterator iter (overload); iter; ++iter)
7715 tree fn = *iter;
7717 if (TREE_CODE (fn) == TEMPLATE_DECL)
7718 /* We're not looking for templates just yet. */
7719 continue;
7721 if ((TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE) != is_ptrmem)
7722 /* We're looking for a non-static member, and this isn't
7723 one, or vice versa. */
7724 continue;
7726 /* In C++17 we need the noexcept-qualifier to compare types. */
7727 if (flag_noexcept_type
7728 && !maybe_instantiate_noexcept (fn, complain))
7729 continue;
7731 /* See if there's a match. */
7732 tree fntype = static_fn_type (fn);
7733 if (same_type_p (target_fn_type, fntype)
7734 || fnptr_conv_p (target_fn_type, fntype))
7735 matches = tree_cons (fn, NULL_TREE, matches);
7738 /* Now, if we've already got a match (or matches), there's no need
7739 to proceed to the template functions. But, if we don't have a
7740 match we need to look at them, too. */
7741 if (!matches)
7743 tree target_arg_types;
7744 tree target_ret_type;
7745 tree *args;
7746 unsigned int nargs, ia;
7747 tree arg;
7749 target_arg_types = TYPE_ARG_TYPES (target_fn_type);
7750 target_ret_type = TREE_TYPE (target_fn_type);
7752 nargs = list_length (target_arg_types);
7753 args = XALLOCAVEC (tree, nargs);
7754 for (arg = target_arg_types, ia = 0;
7755 arg != NULL_TREE && arg != void_list_node;
7756 arg = TREE_CHAIN (arg), ++ia)
7757 args[ia] = TREE_VALUE (arg);
7758 nargs = ia;
7760 for (lkp_iterator iter (overload); iter; ++iter)
7762 tree fn = *iter;
7763 tree instantiation;
7764 tree targs;
7766 if (TREE_CODE (fn) != TEMPLATE_DECL)
7767 /* We're only looking for templates. */
7768 continue;
7770 if ((TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
7771 != is_ptrmem)
7772 /* We're not looking for a non-static member, and this is
7773 one, or vice versa. */
7774 continue;
7776 tree ret = target_ret_type;
7778 /* If the template has a deduced return type, don't expose it to
7779 template argument deduction. */
7780 if (undeduced_auto_decl (fn))
7781 ret = NULL_TREE;
7783 /* Try to do argument deduction. */
7784 targs = make_tree_vec (DECL_NTPARMS (fn));
7785 instantiation = fn_type_unification (fn, explicit_targs, targs, args,
7786 nargs, ret,
7787 DEDUCE_EXACT, LOOKUP_NORMAL,
7788 false, false);
7789 if (instantiation == error_mark_node)
7790 /* Instantiation failed. */
7791 continue;
7793 /* Constraints must be satisfied. This is done before
7794 return type deduction since that instantiates the
7795 function. */
7796 if (flag_concepts && !constraints_satisfied_p (instantiation))
7797 continue;
7799 /* And now force instantiation to do return type deduction. */
7800 if (undeduced_auto_decl (instantiation))
7802 ++function_depth;
7803 instantiate_decl (instantiation, /*defer*/false, /*class*/false);
7804 --function_depth;
7806 require_deduced_type (instantiation);
7809 /* In C++17 we need the noexcept-qualifier to compare types. */
7810 if (flag_noexcept_type)
7811 maybe_instantiate_noexcept (instantiation, complain);
7813 /* See if there's a match. */
7814 tree fntype = static_fn_type (instantiation);
7815 if (same_type_p (target_fn_type, fntype)
7816 || fnptr_conv_p (target_fn_type, fntype))
7817 matches = tree_cons (instantiation, fn, matches);
7820 /* Now, remove all but the most specialized of the matches. */
7821 if (matches)
7823 tree match = most_specialized_instantiation (matches);
7825 if (match != error_mark_node)
7826 matches = tree_cons (TREE_PURPOSE (match),
7827 NULL_TREE,
7828 NULL_TREE);
7832 /* Now we should have exactly one function in MATCHES. */
7833 if (matches == NULL_TREE)
7835 /* There were *no* matches. */
7836 if (complain & tf_error)
7838 error ("no matches converting function %qD to type %q#T",
7839 OVL_NAME (overload), target_type);
7841 print_candidates (overload);
7843 return error_mark_node;
7845 else if (TREE_CHAIN (matches))
7847 /* There were too many matches. First check if they're all
7848 the same function. */
7849 tree match = NULL_TREE;
7851 fn = TREE_PURPOSE (matches);
7853 /* For multi-versioned functions, more than one match is just fine and
7854 decls_match will return false as they are different. */
7855 for (match = TREE_CHAIN (matches); match; match = TREE_CHAIN (match))
7856 if (!decls_match (fn, TREE_PURPOSE (match))
7857 && !targetm.target_option.function_versions
7858 (fn, TREE_PURPOSE (match)))
7859 break;
7861 if (match)
7863 if (complain & tf_error)
7865 error ("converting overloaded function %qD to type %q#T is ambiguous",
7866 OVL_NAME (overload), target_type);
7868 /* Since print_candidates expects the functions in the
7869 TREE_VALUE slot, we flip them here. */
7870 for (match = matches; match; match = TREE_CHAIN (match))
7871 TREE_VALUE (match) = TREE_PURPOSE (match);
7873 print_candidates (matches);
7876 return error_mark_node;
7880 /* Good, exactly one match. Now, convert it to the correct type. */
7881 fn = TREE_PURPOSE (matches);
7883 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
7884 && !(complain & tf_ptrmem_ok) && !flag_ms_extensions)
7886 static int explained;
7888 if (!(complain & tf_error))
7889 return error_mark_node;
7891 permerror (input_location, "assuming pointer to member %qD", fn);
7892 if (!explained)
7894 inform (input_location, "(a pointer to member can only be formed with %<&%E%>)", fn);
7895 explained = 1;
7899 /* If a pointer to a function that is multi-versioned is requested, the
7900 pointer to the dispatcher function is returned instead. This works
7901 well because indirectly calling the function will dispatch the right
7902 function version at run-time. */
7903 if (DECL_FUNCTION_VERSIONED (fn))
7905 fn = get_function_version_dispatcher (fn);
7906 if (fn == NULL)
7907 return error_mark_node;
7908 /* Mark all the versions corresponding to the dispatcher as used. */
7909 if (!(complain & tf_conv))
7910 mark_versions_used (fn);
7913 /* If we're doing overload resolution purely for the purpose of
7914 determining conversion sequences, we should not consider the
7915 function used. If this conversion sequence is selected, the
7916 function will be marked as used at this point. */
7917 if (!(complain & tf_conv))
7919 /* Make =delete work with SFINAE. */
7920 if (DECL_DELETED_FN (fn) && !(complain & tf_error))
7921 return error_mark_node;
7922 if (!mark_used (fn, complain) && !(complain & tf_error))
7923 return error_mark_node;
7926 /* We could not check access to member functions when this
7927 expression was originally created since we did not know at that
7928 time to which function the expression referred. */
7929 if (DECL_FUNCTION_MEMBER_P (fn))
7931 gcc_assert (access_path);
7932 perform_or_defer_access_check (access_path, fn, fn, complain);
7935 if (TYPE_PTRFN_P (target_type) || TYPE_PTRMEMFUNC_P (target_type))
7936 return cp_build_addr_expr (fn, complain);
7937 else
7939 /* The target must be a REFERENCE_TYPE. Above, cp_build_unary_op
7940 will mark the function as addressed, but here we must do it
7941 explicitly. */
7942 cxx_mark_addressable (fn);
7944 return fn;
7948 /* This function will instantiate the type of the expression given in
7949 RHS to match the type of LHSTYPE. If errors exist, then return
7950 error_mark_node. COMPLAIN is a bit mask. If TF_ERROR is set, then
7951 we complain on errors. If we are not complaining, never modify rhs,
7952 as overload resolution wants to try many possible instantiations, in
7953 the hope that at least one will work.
7955 For non-recursive calls, LHSTYPE should be a function, pointer to
7956 function, or a pointer to member function. */
7958 tree
7959 instantiate_type (tree lhstype, tree rhs, tsubst_flags_t complain)
7961 tsubst_flags_t complain_in = complain;
7962 tree access_path = NULL_TREE;
7964 complain &= ~tf_ptrmem_ok;
7966 if (lhstype == unknown_type_node)
7968 if (complain & tf_error)
7969 error ("not enough type information");
7970 return error_mark_node;
7973 if (TREE_TYPE (rhs) != NULL_TREE && ! (type_unknown_p (rhs)))
7975 tree fntype = non_reference (lhstype);
7976 if (same_type_p (fntype, TREE_TYPE (rhs)))
7977 return rhs;
7978 if (fnptr_conv_p (fntype, TREE_TYPE (rhs)))
7979 return rhs;
7980 if (flag_ms_extensions
7981 && TYPE_PTRMEMFUNC_P (fntype)
7982 && !TYPE_PTRMEMFUNC_P (TREE_TYPE (rhs)))
7983 /* Microsoft allows `A::f' to be resolved to a
7984 pointer-to-member. */
7986 else
7988 if (complain & tf_error)
7989 error ("cannot convert %qE from type %qT to type %qT",
7990 rhs, TREE_TYPE (rhs), fntype);
7991 return error_mark_node;
7995 if (BASELINK_P (rhs))
7997 access_path = BASELINK_ACCESS_BINFO (rhs);
7998 rhs = BASELINK_FUNCTIONS (rhs);
8001 /* If we are in a template, and have a NON_DEPENDENT_EXPR, we cannot
8002 deduce any type information. */
8003 if (TREE_CODE (rhs) == NON_DEPENDENT_EXPR)
8005 if (complain & tf_error)
8006 error ("not enough type information");
8007 return error_mark_node;
8010 /* If we instantiate a template, and it is a A ?: C expression
8011 with omitted B, look through the SAVE_EXPR. */
8012 if (TREE_CODE (rhs) == SAVE_EXPR)
8013 rhs = TREE_OPERAND (rhs, 0);
8015 /* There are only a few kinds of expressions that may have a type
8016 dependent on overload resolution. */
8017 gcc_assert (TREE_CODE (rhs) == ADDR_EXPR
8018 || TREE_CODE (rhs) == COMPONENT_REF
8019 || is_overloaded_fn (rhs)
8020 || (flag_ms_extensions && TREE_CODE (rhs) == FUNCTION_DECL));
8022 /* This should really only be used when attempting to distinguish
8023 what sort of a pointer to function we have. For now, any
8024 arithmetic operation which is not supported on pointers
8025 is rejected as an error. */
8027 switch (TREE_CODE (rhs))
8029 case COMPONENT_REF:
8031 tree member = TREE_OPERAND (rhs, 1);
8033 member = instantiate_type (lhstype, member, complain);
8034 if (member != error_mark_node
8035 && TREE_SIDE_EFFECTS (TREE_OPERAND (rhs, 0)))
8036 /* Do not lose object's side effects. */
8037 return build2 (COMPOUND_EXPR, TREE_TYPE (member),
8038 TREE_OPERAND (rhs, 0), member);
8039 return member;
8042 case OFFSET_REF:
8043 rhs = TREE_OPERAND (rhs, 1);
8044 if (BASELINK_P (rhs))
8045 return instantiate_type (lhstype, rhs, complain_in);
8047 /* This can happen if we are forming a pointer-to-member for a
8048 member template. */
8049 gcc_assert (TREE_CODE (rhs) == TEMPLATE_ID_EXPR);
8051 /* Fall through. */
8053 case TEMPLATE_ID_EXPR:
8055 tree fns = TREE_OPERAND (rhs, 0);
8056 tree args = TREE_OPERAND (rhs, 1);
8058 return
8059 resolve_address_of_overloaded_function (lhstype, fns, complain_in,
8060 /*template_only=*/true,
8061 args, access_path);
8064 case OVERLOAD:
8065 case FUNCTION_DECL:
8066 return
8067 resolve_address_of_overloaded_function (lhstype, rhs, complain_in,
8068 /*template_only=*/false,
8069 /*explicit_targs=*/NULL_TREE,
8070 access_path);
8072 case ADDR_EXPR:
8074 if (PTRMEM_OK_P (rhs))
8075 complain |= tf_ptrmem_ok;
8077 return instantiate_type (lhstype, TREE_OPERAND (rhs, 0), complain);
8080 case ERROR_MARK:
8081 return error_mark_node;
8083 default:
8084 gcc_unreachable ();
8086 return error_mark_node;
8089 /* Return the name of the virtual function pointer field
8090 (as an IDENTIFIER_NODE) for the given TYPE. Note that
8091 this may have to look back through base types to find the
8092 ultimate field name. (For single inheritance, these could
8093 all be the same name. Who knows for multiple inheritance). */
8095 static tree
8096 get_vfield_name (tree type)
8098 tree binfo, base_binfo;
8100 for (binfo = TYPE_BINFO (type);
8101 BINFO_N_BASE_BINFOS (binfo);
8102 binfo = base_binfo)
8104 base_binfo = BINFO_BASE_BINFO (binfo, 0);
8106 if (BINFO_VIRTUAL_P (base_binfo)
8107 || !TYPE_CONTAINS_VPTR_P (BINFO_TYPE (base_binfo)))
8108 break;
8111 type = BINFO_TYPE (binfo);
8112 tree ctor_name = constructor_name (type);
8113 char *buf = (char *) alloca (sizeof (VFIELD_NAME_FORMAT)
8114 + IDENTIFIER_LENGTH (ctor_name) + 2);
8115 sprintf (buf, VFIELD_NAME_FORMAT, IDENTIFIER_POINTER (ctor_name));
8116 return get_identifier (buf);
8119 void
8120 print_class_statistics (void)
8122 if (! GATHER_STATISTICS)
8123 return;
8125 fprintf (stderr, "convert_harshness = %d\n", n_convert_harshness);
8126 fprintf (stderr, "compute_conversion_costs = %d\n", n_compute_conversion_costs);
8127 if (n_vtables)
8129 fprintf (stderr, "vtables = %d; vtable searches = %d\n",
8130 n_vtables, n_vtable_searches);
8131 fprintf (stderr, "vtable entries = %d; vtable elems = %d\n",
8132 n_vtable_entries, n_vtable_elems);
8136 /* Build a dummy reference to ourselves so Derived::Base (and A::A) works,
8137 according to [class]:
8138 The class-name is also inserted
8139 into the scope of the class itself. For purposes of access checking,
8140 the inserted class name is treated as if it were a public member name. */
8142 void
8143 build_self_reference (void)
8145 tree name = DECL_NAME (TYPE_NAME (current_class_type));
8146 tree value = build_lang_decl (TYPE_DECL, name, current_class_type);
8148 DECL_NONLOCAL (value) = 1;
8149 DECL_CONTEXT (value) = current_class_type;
8150 DECL_ARTIFICIAL (value) = 1;
8151 SET_DECL_SELF_REFERENCE_P (value);
8152 set_underlying_type (value);
8154 if (processing_template_decl)
8155 value = push_template_decl (value);
8157 tree saved_cas = current_access_specifier;
8158 current_access_specifier = access_public_node;
8159 finish_member_declaration (value);
8160 current_access_specifier = saved_cas;
8163 /* Returns 1 if TYPE contains only padding bytes. */
8166 is_empty_class (tree type)
8168 if (type == error_mark_node)
8169 return 0;
8171 if (! CLASS_TYPE_P (type))
8172 return 0;
8174 return CLASSTYPE_EMPTY_P (type);
8177 /* Returns true if TYPE contains no actual data, just various
8178 possible combinations of empty classes and possibly a vptr. */
8180 bool
8181 is_really_empty_class (tree type)
8183 if (CLASS_TYPE_P (type))
8185 tree field;
8186 tree binfo;
8187 tree base_binfo;
8188 int i;
8190 /* CLASSTYPE_EMPTY_P isn't set properly until the class is actually laid
8191 out, but we'd like to be able to check this before then. */
8192 if (COMPLETE_TYPE_P (type) && is_empty_class (type))
8193 return true;
8195 for (binfo = TYPE_BINFO (type), i = 0;
8196 BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
8197 if (!is_really_empty_class (BINFO_TYPE (base_binfo)))
8198 return false;
8199 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
8200 if (TREE_CODE (field) == FIELD_DECL
8201 && !DECL_ARTIFICIAL (field)
8202 /* An unnamed bit-field is not a data member. */
8203 && (DECL_NAME (field) || !DECL_C_BIT_FIELD (field))
8204 && !is_really_empty_class (TREE_TYPE (field)))
8205 return false;
8206 return true;
8208 else if (TREE_CODE (type) == ARRAY_TYPE)
8209 return (integer_zerop (array_type_nelts_top (type))
8210 || is_really_empty_class (TREE_TYPE (type)));
8211 return false;
8214 /* Note that NAME was looked up while the current class was being
8215 defined and that the result of that lookup was DECL. */
8217 void
8218 maybe_note_name_used_in_class (tree name, tree decl)
8220 splay_tree names_used;
8222 /* If we're not defining a class, there's nothing to do. */
8223 if (!(innermost_scope_kind() == sk_class
8224 && TYPE_BEING_DEFINED (current_class_type)
8225 && !LAMBDA_TYPE_P (current_class_type)))
8226 return;
8228 /* If there's already a binding for this NAME, then we don't have
8229 anything to worry about. */
8230 if (lookup_member (current_class_type, name,
8231 /*protect=*/0, /*want_type=*/false, tf_warning_or_error))
8232 return;
8234 if (!current_class_stack[current_class_depth - 1].names_used)
8235 current_class_stack[current_class_depth - 1].names_used
8236 = splay_tree_new (splay_tree_compare_pointers, 0, 0);
8237 names_used = current_class_stack[current_class_depth - 1].names_used;
8239 splay_tree_insert (names_used,
8240 (splay_tree_key) name,
8241 (splay_tree_value) decl);
8244 /* Note that NAME was declared (as DECL) in the current class. Check
8245 to see that the declaration is valid. */
8247 void
8248 note_name_declared_in_class (tree name, tree decl)
8250 splay_tree names_used;
8251 splay_tree_node n;
8253 /* Look to see if we ever used this name. */
8254 names_used
8255 = current_class_stack[current_class_depth - 1].names_used;
8256 if (!names_used)
8257 return;
8258 /* The C language allows members to be declared with a type of the same
8259 name, and the C++ standard says this diagnostic is not required. So
8260 allow it in extern "C" blocks unless predantic is specified.
8261 Allow it in all cases if -ms-extensions is specified. */
8262 if ((!pedantic && current_lang_name == lang_name_c)
8263 || flag_ms_extensions)
8264 return;
8265 n = splay_tree_lookup (names_used, (splay_tree_key) name);
8266 if (n)
8268 /* [basic.scope.class]
8270 A name N used in a class S shall refer to the same declaration
8271 in its context and when re-evaluated in the completed scope of
8272 S. */
8273 permerror (input_location, "declaration of %q#D", decl);
8274 permerror (location_of ((tree) n->value),
8275 "changes meaning of %qD from %q#D",
8276 OVL_NAME (decl), (tree) n->value);
8280 /* Returns the VAR_DECL for the complete vtable associated with BINFO.
8281 Secondary vtables are merged with primary vtables; this function
8282 will return the VAR_DECL for the primary vtable. */
8284 tree
8285 get_vtbl_decl_for_binfo (tree binfo)
8287 tree decl;
8289 decl = BINFO_VTABLE (binfo);
8290 if (decl && TREE_CODE (decl) == POINTER_PLUS_EXPR)
8292 gcc_assert (TREE_CODE (TREE_OPERAND (decl, 0)) == ADDR_EXPR);
8293 decl = TREE_OPERAND (TREE_OPERAND (decl, 0), 0);
8295 if (decl)
8296 gcc_assert (VAR_P (decl));
8297 return decl;
8301 /* Returns the binfo for the primary base of BINFO. If the resulting
8302 BINFO is a virtual base, and it is inherited elsewhere in the
8303 hierarchy, then the returned binfo might not be the primary base of
8304 BINFO in the complete object. Check BINFO_PRIMARY_P or
8305 BINFO_LOST_PRIMARY_P to be sure. */
8307 static tree
8308 get_primary_binfo (tree binfo)
8310 tree primary_base;
8312 primary_base = CLASSTYPE_PRIMARY_BINFO (BINFO_TYPE (binfo));
8313 if (!primary_base)
8314 return NULL_TREE;
8316 return copied_binfo (primary_base, binfo);
8319 /* As above, but iterate until we reach the binfo that actually provides the
8320 vptr for BINFO. */
8322 static tree
8323 most_primary_binfo (tree binfo)
8325 tree b = binfo;
8326 while (CLASSTYPE_HAS_PRIMARY_BASE_P (BINFO_TYPE (b))
8327 && !BINFO_LOST_PRIMARY_P (b))
8329 tree primary_base = get_primary_binfo (b);
8330 gcc_assert (BINFO_PRIMARY_P (primary_base)
8331 && BINFO_INHERITANCE_CHAIN (primary_base) == b);
8332 b = primary_base;
8334 return b;
8337 /* Returns true if BINFO gets its vptr from a virtual base of the most derived
8338 type. Note that the virtual inheritance might be above or below BINFO in
8339 the hierarchy. */
8341 bool
8342 vptr_via_virtual_p (tree binfo)
8344 if (TYPE_P (binfo))
8345 binfo = TYPE_BINFO (binfo);
8346 tree primary = most_primary_binfo (binfo);
8347 /* Don't limit binfo_via_virtual, we want to return true when BINFO itself is
8348 a morally virtual base. */
8349 tree virt = binfo_via_virtual (primary, NULL_TREE);
8350 return virt != NULL_TREE;
8353 /* If INDENTED_P is zero, indent to INDENT. Return nonzero. */
8355 static int
8356 maybe_indent_hierarchy (FILE * stream, int indent, int indented_p)
8358 if (!indented_p)
8359 fprintf (stream, "%*s", indent, "");
8360 return 1;
8363 /* Dump the offsets of all the bases rooted at BINFO to STREAM.
8364 INDENT should be zero when called from the top level; it is
8365 incremented recursively. IGO indicates the next expected BINFO in
8366 inheritance graph ordering. */
8368 static tree
8369 dump_class_hierarchy_r (FILE *stream,
8370 dump_flags_t flags,
8371 tree binfo,
8372 tree igo,
8373 int indent)
8375 int indented = 0;
8376 tree base_binfo;
8377 int i;
8379 indented = maybe_indent_hierarchy (stream, indent, 0);
8380 fprintf (stream, "%s (0x" HOST_WIDE_INT_PRINT_HEX ") ",
8381 type_as_string (BINFO_TYPE (binfo), TFF_PLAIN_IDENTIFIER),
8382 (HOST_WIDE_INT) (uintptr_t) binfo);
8383 if (binfo != igo)
8385 fprintf (stream, "alternative-path\n");
8386 return igo;
8388 igo = TREE_CHAIN (binfo);
8390 fprintf (stream, HOST_WIDE_INT_PRINT_DEC,
8391 tree_to_shwi (BINFO_OFFSET (binfo)));
8392 if (is_empty_class (BINFO_TYPE (binfo)))
8393 fprintf (stream, " empty");
8394 else if (CLASSTYPE_NEARLY_EMPTY_P (BINFO_TYPE (binfo)))
8395 fprintf (stream, " nearly-empty");
8396 if (BINFO_VIRTUAL_P (binfo))
8397 fprintf (stream, " virtual");
8398 fprintf (stream, "\n");
8400 indented = 0;
8401 if (BINFO_PRIMARY_P (binfo))
8403 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
8404 fprintf (stream, " primary-for %s (0x" HOST_WIDE_INT_PRINT_HEX ")",
8405 type_as_string (BINFO_TYPE (BINFO_INHERITANCE_CHAIN (binfo)),
8406 TFF_PLAIN_IDENTIFIER),
8407 (HOST_WIDE_INT) (uintptr_t) BINFO_INHERITANCE_CHAIN (binfo));
8409 if (BINFO_LOST_PRIMARY_P (binfo))
8411 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
8412 fprintf (stream, " lost-primary");
8414 if (indented)
8415 fprintf (stream, "\n");
8417 if (!(flags & TDF_SLIM))
8419 int indented = 0;
8421 if (BINFO_SUBVTT_INDEX (binfo))
8423 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
8424 fprintf (stream, " subvttidx=%s",
8425 expr_as_string (BINFO_SUBVTT_INDEX (binfo),
8426 TFF_PLAIN_IDENTIFIER));
8428 if (BINFO_VPTR_INDEX (binfo))
8430 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
8431 fprintf (stream, " vptridx=%s",
8432 expr_as_string (BINFO_VPTR_INDEX (binfo),
8433 TFF_PLAIN_IDENTIFIER));
8435 if (BINFO_VPTR_FIELD (binfo))
8437 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
8438 fprintf (stream, " vbaseoffset=%s",
8439 expr_as_string (BINFO_VPTR_FIELD (binfo),
8440 TFF_PLAIN_IDENTIFIER));
8442 if (BINFO_VTABLE (binfo))
8444 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
8445 fprintf (stream, " vptr=%s",
8446 expr_as_string (BINFO_VTABLE (binfo),
8447 TFF_PLAIN_IDENTIFIER));
8450 if (indented)
8451 fprintf (stream, "\n");
8454 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
8455 igo = dump_class_hierarchy_r (stream, flags, base_binfo, igo, indent + 2);
8457 return igo;
8460 /* Dump the BINFO hierarchy for T. */
8462 static void
8463 dump_class_hierarchy_1 (FILE *stream, dump_flags_t flags, tree t)
8465 fprintf (stream, "Class %s\n", type_as_string (t, TFF_PLAIN_IDENTIFIER));
8466 fprintf (stream, " size=%lu align=%lu\n",
8467 (unsigned long)(tree_to_shwi (TYPE_SIZE (t)) / BITS_PER_UNIT),
8468 (unsigned long)(TYPE_ALIGN (t) / BITS_PER_UNIT));
8469 fprintf (stream, " base size=%lu base align=%lu\n",
8470 (unsigned long)(tree_to_shwi (TYPE_SIZE (CLASSTYPE_AS_BASE (t)))
8471 / BITS_PER_UNIT),
8472 (unsigned long)(TYPE_ALIGN (CLASSTYPE_AS_BASE (t))
8473 / BITS_PER_UNIT));
8474 dump_class_hierarchy_r (stream, flags, TYPE_BINFO (t), TYPE_BINFO (t), 0);
8475 fprintf (stream, "\n");
8478 /* Debug interface to hierarchy dumping. */
8480 void
8481 debug_class (tree t)
8483 dump_class_hierarchy_1 (stderr, TDF_SLIM, t);
8486 static void
8487 dump_class_hierarchy (tree t)
8489 dump_flags_t flags;
8490 if (FILE *stream = dump_begin (class_dump_id, &flags))
8492 dump_class_hierarchy_1 (stream, flags, t);
8493 dump_end (class_dump_id, stream);
8497 static void
8498 dump_array (FILE * stream, tree decl)
8500 tree value;
8501 unsigned HOST_WIDE_INT ix;
8502 HOST_WIDE_INT elt;
8503 tree size = TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (decl)));
8505 elt = (tree_to_shwi (TYPE_SIZE (TREE_TYPE (TREE_TYPE (decl))))
8506 / BITS_PER_UNIT);
8507 fprintf (stream, "%s:", decl_as_string (decl, TFF_PLAIN_IDENTIFIER));
8508 fprintf (stream, " %s entries",
8509 expr_as_string (size_binop (PLUS_EXPR, size, size_one_node),
8510 TFF_PLAIN_IDENTIFIER));
8511 fprintf (stream, "\n");
8513 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (DECL_INITIAL (decl)),
8514 ix, value)
8515 fprintf (stream, "%-4ld %s\n", (long)(ix * elt),
8516 expr_as_string (value, TFF_PLAIN_IDENTIFIER));
8519 static void
8520 dump_vtable (tree t, tree binfo, tree vtable)
8522 dump_flags_t flags;
8523 FILE *stream = dump_begin (class_dump_id, &flags);
8525 if (!stream)
8526 return;
8528 if (!(flags & TDF_SLIM))
8530 int ctor_vtbl_p = TYPE_BINFO (t) != binfo;
8532 fprintf (stream, "%s for %s",
8533 ctor_vtbl_p ? "Construction vtable" : "Vtable",
8534 type_as_string (BINFO_TYPE (binfo), TFF_PLAIN_IDENTIFIER));
8535 if (ctor_vtbl_p)
8537 if (!BINFO_VIRTUAL_P (binfo))
8538 fprintf (stream, " (0x" HOST_WIDE_INT_PRINT_HEX " instance)",
8539 (HOST_WIDE_INT) (uintptr_t) binfo);
8540 fprintf (stream, " in %s", type_as_string (t, TFF_PLAIN_IDENTIFIER));
8542 fprintf (stream, "\n");
8543 dump_array (stream, vtable);
8544 fprintf (stream, "\n");
8547 dump_end (class_dump_id, stream);
8550 static void
8551 dump_vtt (tree t, tree vtt)
8553 dump_flags_t flags;
8554 FILE *stream = dump_begin (class_dump_id, &flags);
8556 if (!stream)
8557 return;
8559 if (!(flags & TDF_SLIM))
8561 fprintf (stream, "VTT for %s\n",
8562 type_as_string (t, TFF_PLAIN_IDENTIFIER));
8563 dump_array (stream, vtt);
8564 fprintf (stream, "\n");
8567 dump_end (class_dump_id, stream);
8570 /* Dump a function or thunk and its thunkees. */
8572 static void
8573 dump_thunk (FILE *stream, int indent, tree thunk)
8575 static const char spaces[] = " ";
8576 tree name = DECL_NAME (thunk);
8577 tree thunks;
8579 fprintf (stream, "%.*s%p %s %s", indent, spaces,
8580 (void *)thunk,
8581 !DECL_THUNK_P (thunk) ? "function"
8582 : DECL_THIS_THUNK_P (thunk) ? "this-thunk" : "covariant-thunk",
8583 name ? IDENTIFIER_POINTER (name) : "<unset>");
8584 if (DECL_THUNK_P (thunk))
8586 HOST_WIDE_INT fixed_adjust = THUNK_FIXED_OFFSET (thunk);
8587 tree virtual_adjust = THUNK_VIRTUAL_OFFSET (thunk);
8589 fprintf (stream, " fixed=" HOST_WIDE_INT_PRINT_DEC, fixed_adjust);
8590 if (!virtual_adjust)
8591 /*NOP*/;
8592 else if (DECL_THIS_THUNK_P (thunk))
8593 fprintf (stream, " vcall=" HOST_WIDE_INT_PRINT_DEC,
8594 tree_to_shwi (virtual_adjust));
8595 else
8596 fprintf (stream, " vbase=" HOST_WIDE_INT_PRINT_DEC "(%s)",
8597 tree_to_shwi (BINFO_VPTR_FIELD (virtual_adjust)),
8598 type_as_string (BINFO_TYPE (virtual_adjust), TFF_SCOPE));
8599 if (THUNK_ALIAS (thunk))
8600 fprintf (stream, " alias to %p", (void *)THUNK_ALIAS (thunk));
8602 fprintf (stream, "\n");
8603 for (thunks = DECL_THUNKS (thunk); thunks; thunks = TREE_CHAIN (thunks))
8604 dump_thunk (stream, indent + 2, thunks);
8607 /* Dump the thunks for FN. */
8609 void
8610 debug_thunks (tree fn)
8612 dump_thunk (stderr, 0, fn);
8615 /* Virtual function table initialization. */
8617 /* Create all the necessary vtables for T and its base classes. */
8619 static void
8620 finish_vtbls (tree t)
8622 tree vbase;
8623 vec<constructor_elt, va_gc> *v = NULL;
8624 tree vtable = BINFO_VTABLE (TYPE_BINFO (t));
8626 /* We lay out the primary and secondary vtables in one contiguous
8627 vtable. The primary vtable is first, followed by the non-virtual
8628 secondary vtables in inheritance graph order. */
8629 accumulate_vtbl_inits (TYPE_BINFO (t), TYPE_BINFO (t), TYPE_BINFO (t),
8630 vtable, t, &v);
8632 /* Then come the virtual bases, also in inheritance graph order. */
8633 for (vbase = TYPE_BINFO (t); vbase; vbase = TREE_CHAIN (vbase))
8635 if (!BINFO_VIRTUAL_P (vbase))
8636 continue;
8637 accumulate_vtbl_inits (vbase, vbase, TYPE_BINFO (t), vtable, t, &v);
8640 if (BINFO_VTABLE (TYPE_BINFO (t)))
8641 initialize_vtable (TYPE_BINFO (t), v);
8644 /* Initialize the vtable for BINFO with the INITS. */
8646 static void
8647 initialize_vtable (tree binfo, vec<constructor_elt, va_gc> *inits)
8649 tree decl;
8651 layout_vtable_decl (binfo, vec_safe_length (inits));
8652 decl = get_vtbl_decl_for_binfo (binfo);
8653 initialize_artificial_var (decl, inits);
8654 dump_vtable (BINFO_TYPE (binfo), binfo, decl);
8657 /* Build the VTT (virtual table table) for T.
8658 A class requires a VTT if it has virtual bases.
8660 This holds
8661 1 - primary virtual pointer for complete object T
8662 2 - secondary VTTs for each direct non-virtual base of T which requires a
8664 3 - secondary virtual pointers for each direct or indirect base of T which
8665 has virtual bases or is reachable via a virtual path from T.
8666 4 - secondary VTTs for each direct or indirect virtual base of T.
8668 Secondary VTTs look like complete object VTTs without part 4. */
8670 static void
8671 build_vtt (tree t)
8673 tree type;
8674 tree vtt;
8675 tree index;
8676 vec<constructor_elt, va_gc> *inits;
8678 /* Build up the initializers for the VTT. */
8679 inits = NULL;
8680 index = size_zero_node;
8681 build_vtt_inits (TYPE_BINFO (t), t, &inits, &index);
8683 /* If we didn't need a VTT, we're done. */
8684 if (!inits)
8685 return;
8687 /* Figure out the type of the VTT. */
8688 type = build_array_of_n_type (const_ptr_type_node,
8689 inits->length ());
8691 /* Now, build the VTT object itself. */
8692 vtt = build_vtable (t, mangle_vtt_for_type (t), type);
8693 initialize_artificial_var (vtt, inits);
8694 /* Add the VTT to the vtables list. */
8695 DECL_CHAIN (vtt) = DECL_CHAIN (CLASSTYPE_VTABLES (t));
8696 DECL_CHAIN (CLASSTYPE_VTABLES (t)) = vtt;
8698 dump_vtt (t, vtt);
8701 /* When building a secondary VTT, BINFO_VTABLE is set to a TREE_LIST with
8702 PURPOSE the RTTI_BINFO, VALUE the real vtable pointer for this binfo,
8703 and CHAIN the vtable pointer for this binfo after construction is
8704 complete. VALUE can also be another BINFO, in which case we recurse. */
8706 static tree
8707 binfo_ctor_vtable (tree binfo)
8709 tree vt;
8711 while (1)
8713 vt = BINFO_VTABLE (binfo);
8714 if (TREE_CODE (vt) == TREE_LIST)
8715 vt = TREE_VALUE (vt);
8716 if (TREE_CODE (vt) == TREE_BINFO)
8717 binfo = vt;
8718 else
8719 break;
8722 return vt;
8725 /* Data for secondary VTT initialization. */
8726 struct secondary_vptr_vtt_init_data
8728 /* Is this the primary VTT? */
8729 bool top_level_p;
8731 /* Current index into the VTT. */
8732 tree index;
8734 /* Vector of initializers built up. */
8735 vec<constructor_elt, va_gc> *inits;
8737 /* The type being constructed by this secondary VTT. */
8738 tree type_being_constructed;
8741 /* Recursively build the VTT-initializer for BINFO (which is in the
8742 hierarchy dominated by T). INITS points to the end of the initializer
8743 list to date. INDEX is the VTT index where the next element will be
8744 replaced. Iff BINFO is the binfo for T, this is the top level VTT (i.e.
8745 not a subvtt for some base of T). When that is so, we emit the sub-VTTs
8746 for virtual bases of T. When it is not so, we build the constructor
8747 vtables for the BINFO-in-T variant. */
8749 static void
8750 build_vtt_inits (tree binfo, tree t, vec<constructor_elt, va_gc> **inits,
8751 tree *index)
8753 int i;
8754 tree b;
8755 tree init;
8756 secondary_vptr_vtt_init_data data;
8757 int top_level_p = SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), t);
8759 /* We only need VTTs for subobjects with virtual bases. */
8760 if (!CLASSTYPE_VBASECLASSES (BINFO_TYPE (binfo)))
8761 return;
8763 /* We need to use a construction vtable if this is not the primary
8764 VTT. */
8765 if (!top_level_p)
8767 build_ctor_vtbl_group (binfo, t);
8769 /* Record the offset in the VTT where this sub-VTT can be found. */
8770 BINFO_SUBVTT_INDEX (binfo) = *index;
8773 /* Add the address of the primary vtable for the complete object. */
8774 init = binfo_ctor_vtable (binfo);
8775 CONSTRUCTOR_APPEND_ELT (*inits, NULL_TREE, init);
8776 if (top_level_p)
8778 gcc_assert (!BINFO_VPTR_INDEX (binfo));
8779 BINFO_VPTR_INDEX (binfo) = *index;
8781 *index = size_binop (PLUS_EXPR, *index, TYPE_SIZE_UNIT (ptr_type_node));
8783 /* Recursively add the secondary VTTs for non-virtual bases. */
8784 for (i = 0; BINFO_BASE_ITERATE (binfo, i, b); ++i)
8785 if (!BINFO_VIRTUAL_P (b))
8786 build_vtt_inits (b, t, inits, index);
8788 /* Add secondary virtual pointers for all subobjects of BINFO with
8789 either virtual bases or reachable along a virtual path, except
8790 subobjects that are non-virtual primary bases. */
8791 data.top_level_p = top_level_p;
8792 data.index = *index;
8793 data.inits = *inits;
8794 data.type_being_constructed = BINFO_TYPE (binfo);
8796 dfs_walk_once (binfo, dfs_build_secondary_vptr_vtt_inits, NULL, &data);
8798 *index = data.index;
8800 /* data.inits might have grown as we added secondary virtual pointers.
8801 Make sure our caller knows about the new vector. */
8802 *inits = data.inits;
8804 if (top_level_p)
8805 /* Add the secondary VTTs for virtual bases in inheritance graph
8806 order. */
8807 for (b = TYPE_BINFO (BINFO_TYPE (binfo)); b; b = TREE_CHAIN (b))
8809 if (!BINFO_VIRTUAL_P (b))
8810 continue;
8812 build_vtt_inits (b, t, inits, index);
8814 else
8815 /* Remove the ctor vtables we created. */
8816 dfs_walk_all (binfo, dfs_fixup_binfo_vtbls, NULL, binfo);
8819 /* Called from build_vtt_inits via dfs_walk. BINFO is the binfo for the base
8820 in most derived. DATA is a SECONDARY_VPTR_VTT_INIT_DATA structure. */
8822 static tree
8823 dfs_build_secondary_vptr_vtt_inits (tree binfo, void *data_)
8825 secondary_vptr_vtt_init_data *data = (secondary_vptr_vtt_init_data *)data_;
8827 /* We don't care about bases that don't have vtables. */
8828 if (!TYPE_VFIELD (BINFO_TYPE (binfo)))
8829 return dfs_skip_bases;
8831 /* We're only interested in proper subobjects of the type being
8832 constructed. */
8833 if (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), data->type_being_constructed))
8834 return NULL_TREE;
8836 /* We're only interested in bases with virtual bases or reachable
8837 via a virtual path from the type being constructed. */
8838 if (!(CLASSTYPE_VBASECLASSES (BINFO_TYPE (binfo))
8839 || binfo_via_virtual (binfo, data->type_being_constructed)))
8840 return dfs_skip_bases;
8842 /* We're not interested in non-virtual primary bases. */
8843 if (!BINFO_VIRTUAL_P (binfo) && BINFO_PRIMARY_P (binfo))
8844 return NULL_TREE;
8846 /* Record the index where this secondary vptr can be found. */
8847 if (data->top_level_p)
8849 gcc_assert (!BINFO_VPTR_INDEX (binfo));
8850 BINFO_VPTR_INDEX (binfo) = data->index;
8852 if (BINFO_VIRTUAL_P (binfo))
8854 /* It's a primary virtual base, and this is not a
8855 construction vtable. Find the base this is primary of in
8856 the inheritance graph, and use that base's vtable
8857 now. */
8858 while (BINFO_PRIMARY_P (binfo))
8859 binfo = BINFO_INHERITANCE_CHAIN (binfo);
8863 /* Add the initializer for the secondary vptr itself. */
8864 CONSTRUCTOR_APPEND_ELT (data->inits, NULL_TREE, binfo_ctor_vtable (binfo));
8866 /* Advance the vtt index. */
8867 data->index = size_binop (PLUS_EXPR, data->index,
8868 TYPE_SIZE_UNIT (ptr_type_node));
8870 return NULL_TREE;
8873 /* Called from build_vtt_inits via dfs_walk. After building
8874 constructor vtables and generating the sub-vtt from them, we need
8875 to restore the BINFO_VTABLES that were scribbled on. DATA is the
8876 binfo of the base whose sub vtt was generated. */
8878 static tree
8879 dfs_fixup_binfo_vtbls (tree binfo, void* data)
8881 tree vtable = BINFO_VTABLE (binfo);
8883 if (!TYPE_CONTAINS_VPTR_P (BINFO_TYPE (binfo)))
8884 /* If this class has no vtable, none of its bases do. */
8885 return dfs_skip_bases;
8887 if (!vtable)
8888 /* This might be a primary base, so have no vtable in this
8889 hierarchy. */
8890 return NULL_TREE;
8892 /* If we scribbled the construction vtable vptr into BINFO, clear it
8893 out now. */
8894 if (TREE_CODE (vtable) == TREE_LIST
8895 && (TREE_PURPOSE (vtable) == (tree) data))
8896 BINFO_VTABLE (binfo) = TREE_CHAIN (vtable);
8898 return NULL_TREE;
8901 /* Build the construction vtable group for BINFO which is in the
8902 hierarchy dominated by T. */
8904 static void
8905 build_ctor_vtbl_group (tree binfo, tree t)
8907 tree type;
8908 tree vtbl;
8909 tree id;
8910 tree vbase;
8911 vec<constructor_elt, va_gc> *v;
8913 /* See if we've already created this construction vtable group. */
8914 id = mangle_ctor_vtbl_for_type (t, binfo);
8915 if (get_global_binding (id))
8916 return;
8918 gcc_assert (!SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), t));
8919 /* Build a version of VTBL (with the wrong type) for use in
8920 constructing the addresses of secondary vtables in the
8921 construction vtable group. */
8922 vtbl = build_vtable (t, id, ptr_type_node);
8923 DECL_CONSTRUCTION_VTABLE_P (vtbl) = 1;
8924 /* Don't export construction vtables from shared libraries. Even on
8925 targets that don't support hidden visibility, this tells
8926 can_refer_decl_in_current_unit_p not to assume that it's safe to
8927 access from a different compilation unit (bz 54314). */
8928 DECL_VISIBILITY (vtbl) = VISIBILITY_HIDDEN;
8929 DECL_VISIBILITY_SPECIFIED (vtbl) = true;
8931 v = NULL;
8932 accumulate_vtbl_inits (binfo, TYPE_BINFO (TREE_TYPE (binfo)),
8933 binfo, vtbl, t, &v);
8935 /* Add the vtables for each of our virtual bases using the vbase in T
8936 binfo. */
8937 for (vbase = TYPE_BINFO (BINFO_TYPE (binfo));
8938 vbase;
8939 vbase = TREE_CHAIN (vbase))
8941 tree b;
8943 if (!BINFO_VIRTUAL_P (vbase))
8944 continue;
8945 b = copied_binfo (vbase, binfo);
8947 accumulate_vtbl_inits (b, vbase, binfo, vtbl, t, &v);
8950 /* Figure out the type of the construction vtable. */
8951 type = build_array_of_n_type (vtable_entry_type, v->length ());
8952 layout_type (type);
8953 TREE_TYPE (vtbl) = type;
8954 DECL_SIZE (vtbl) = DECL_SIZE_UNIT (vtbl) = NULL_TREE;
8955 layout_decl (vtbl, 0);
8957 /* Initialize the construction vtable. */
8958 CLASSTYPE_VTABLES (t) = chainon (CLASSTYPE_VTABLES (t), vtbl);
8959 initialize_artificial_var (vtbl, v);
8960 dump_vtable (t, binfo, vtbl);
8963 /* Add the vtbl initializers for BINFO (and its bases other than
8964 non-virtual primaries) to the list of INITS. BINFO is in the
8965 hierarchy dominated by T. RTTI_BINFO is the binfo within T of
8966 the constructor the vtbl inits should be accumulated for. (If this
8967 is the complete object vtbl then RTTI_BINFO will be TYPE_BINFO (T).)
8968 ORIG_BINFO is the binfo for this object within BINFO_TYPE (RTTI_BINFO).
8969 BINFO is the active base equivalent of ORIG_BINFO in the inheritance
8970 graph of T. Both BINFO and ORIG_BINFO will have the same BINFO_TYPE,
8971 but are not necessarily the same in terms of layout. */
8973 static void
8974 accumulate_vtbl_inits (tree binfo,
8975 tree orig_binfo,
8976 tree rtti_binfo,
8977 tree vtbl,
8978 tree t,
8979 vec<constructor_elt, va_gc> **inits)
8981 int i;
8982 tree base_binfo;
8983 int ctor_vtbl_p = !SAME_BINFO_TYPE_P (BINFO_TYPE (rtti_binfo), t);
8985 gcc_assert (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), BINFO_TYPE (orig_binfo)));
8987 /* If it doesn't have a vptr, we don't do anything. */
8988 if (!TYPE_CONTAINS_VPTR_P (BINFO_TYPE (binfo)))
8989 return;
8991 /* If we're building a construction vtable, we're not interested in
8992 subobjects that don't require construction vtables. */
8993 if (ctor_vtbl_p
8994 && !CLASSTYPE_VBASECLASSES (BINFO_TYPE (binfo))
8995 && !binfo_via_virtual (orig_binfo, BINFO_TYPE (rtti_binfo)))
8996 return;
8998 /* Build the initializers for the BINFO-in-T vtable. */
8999 dfs_accumulate_vtbl_inits (binfo, orig_binfo, rtti_binfo, vtbl, t, inits);
9001 /* Walk the BINFO and its bases. We walk in preorder so that as we
9002 initialize each vtable we can figure out at what offset the
9003 secondary vtable lies from the primary vtable. We can't use
9004 dfs_walk here because we need to iterate through bases of BINFO
9005 and RTTI_BINFO simultaneously. */
9006 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
9008 /* Skip virtual bases. */
9009 if (BINFO_VIRTUAL_P (base_binfo))
9010 continue;
9011 accumulate_vtbl_inits (base_binfo,
9012 BINFO_BASE_BINFO (orig_binfo, i),
9013 rtti_binfo, vtbl, t,
9014 inits);
9018 /* Called from accumulate_vtbl_inits. Adds the initializers for the
9019 BINFO vtable to L. */
9021 static void
9022 dfs_accumulate_vtbl_inits (tree binfo,
9023 tree orig_binfo,
9024 tree rtti_binfo,
9025 tree orig_vtbl,
9026 tree t,
9027 vec<constructor_elt, va_gc> **l)
9029 tree vtbl = NULL_TREE;
9030 int ctor_vtbl_p = !SAME_BINFO_TYPE_P (BINFO_TYPE (rtti_binfo), t);
9031 int n_inits;
9033 if (ctor_vtbl_p
9034 && BINFO_VIRTUAL_P (orig_binfo) && BINFO_PRIMARY_P (orig_binfo))
9036 /* In the hierarchy of BINFO_TYPE (RTTI_BINFO), this is a
9037 primary virtual base. If it is not the same primary in
9038 the hierarchy of T, we'll need to generate a ctor vtable
9039 for it, to place at its location in T. If it is the same
9040 primary, we still need a VTT entry for the vtable, but it
9041 should point to the ctor vtable for the base it is a
9042 primary for within the sub-hierarchy of RTTI_BINFO.
9044 There are three possible cases:
9046 1) We are in the same place.
9047 2) We are a primary base within a lost primary virtual base of
9048 RTTI_BINFO.
9049 3) We are primary to something not a base of RTTI_BINFO. */
9051 tree b;
9052 tree last = NULL_TREE;
9054 /* First, look through the bases we are primary to for RTTI_BINFO
9055 or a virtual base. */
9056 b = binfo;
9057 while (BINFO_PRIMARY_P (b))
9059 b = BINFO_INHERITANCE_CHAIN (b);
9060 last = b;
9061 if (BINFO_VIRTUAL_P (b) || b == rtti_binfo)
9062 goto found;
9064 /* If we run out of primary links, keep looking down our
9065 inheritance chain; we might be an indirect primary. */
9066 for (b = last; b; b = BINFO_INHERITANCE_CHAIN (b))
9067 if (BINFO_VIRTUAL_P (b) || b == rtti_binfo)
9068 break;
9069 found:
9071 /* If we found RTTI_BINFO, this is case 1. If we found a virtual
9072 base B and it is a base of RTTI_BINFO, this is case 2. In
9073 either case, we share our vtable with LAST, i.e. the
9074 derived-most base within B of which we are a primary. */
9075 if (b == rtti_binfo
9076 || (b && binfo_for_vbase (BINFO_TYPE (b), BINFO_TYPE (rtti_binfo))))
9077 /* Just set our BINFO_VTABLE to point to LAST, as we may not have
9078 set LAST's BINFO_VTABLE yet. We'll extract the actual vptr in
9079 binfo_ctor_vtable after everything's been set up. */
9080 vtbl = last;
9082 /* Otherwise, this is case 3 and we get our own. */
9084 else if (!BINFO_NEW_VTABLE_MARKED (orig_binfo))
9085 return;
9087 n_inits = vec_safe_length (*l);
9089 if (!vtbl)
9091 tree index;
9092 int non_fn_entries;
9094 /* Add the initializer for this vtable. */
9095 build_vtbl_initializer (binfo, orig_binfo, t, rtti_binfo,
9096 &non_fn_entries, l);
9098 /* Figure out the position to which the VPTR should point. */
9099 vtbl = build1 (ADDR_EXPR, vtbl_ptr_type_node, orig_vtbl);
9100 index = size_binop (MULT_EXPR,
9101 TYPE_SIZE_UNIT (vtable_entry_type),
9102 size_int (non_fn_entries + n_inits));
9103 vtbl = fold_build_pointer_plus (vtbl, index);
9106 if (ctor_vtbl_p)
9107 /* For a construction vtable, we can't overwrite BINFO_VTABLE.
9108 So, we make a TREE_LIST. Later, dfs_fixup_binfo_vtbls will
9109 straighten this out. */
9110 BINFO_VTABLE (binfo) = tree_cons (rtti_binfo, vtbl, BINFO_VTABLE (binfo));
9111 else if (BINFO_PRIMARY_P (binfo) && BINFO_VIRTUAL_P (binfo))
9112 /* Throw away any unneeded intializers. */
9113 (*l)->truncate (n_inits);
9114 else
9115 /* For an ordinary vtable, set BINFO_VTABLE. */
9116 BINFO_VTABLE (binfo) = vtbl;
9119 static GTY(()) tree abort_fndecl_addr;
9120 static GTY(()) tree dvirt_fn;
9122 /* Construct the initializer for BINFO's virtual function table. BINFO
9123 is part of the hierarchy dominated by T. If we're building a
9124 construction vtable, the ORIG_BINFO is the binfo we should use to
9125 find the actual function pointers to put in the vtable - but they
9126 can be overridden on the path to most-derived in the graph that
9127 ORIG_BINFO belongs. Otherwise,
9128 ORIG_BINFO should be the same as BINFO. The RTTI_BINFO is the
9129 BINFO that should be indicated by the RTTI information in the
9130 vtable; it will be a base class of T, rather than T itself, if we
9131 are building a construction vtable.
9133 The value returned is a TREE_LIST suitable for wrapping in a
9134 CONSTRUCTOR to use as the DECL_INITIAL for a vtable. If
9135 NON_FN_ENTRIES_P is not NULL, *NON_FN_ENTRIES_P is set to the
9136 number of non-function entries in the vtable.
9138 It might seem that this function should never be called with a
9139 BINFO for which BINFO_PRIMARY_P holds, the vtable for such a
9140 base is always subsumed by a derived class vtable. However, when
9141 we are building construction vtables, we do build vtables for
9142 primary bases; we need these while the primary base is being
9143 constructed. */
9145 static void
9146 build_vtbl_initializer (tree binfo,
9147 tree orig_binfo,
9148 tree t,
9149 tree rtti_binfo,
9150 int* non_fn_entries_p,
9151 vec<constructor_elt, va_gc> **inits)
9153 tree v;
9154 vtbl_init_data vid;
9155 unsigned ix, jx;
9156 tree vbinfo;
9157 vec<tree, va_gc> *vbases;
9158 constructor_elt *e;
9160 /* Initialize VID. */
9161 memset (&vid, 0, sizeof (vid));
9162 vid.binfo = binfo;
9163 vid.derived = t;
9164 vid.rtti_binfo = rtti_binfo;
9165 vid.primary_vtbl_p = SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), t);
9166 vid.ctor_vtbl_p = !SAME_BINFO_TYPE_P (BINFO_TYPE (rtti_binfo), t);
9167 vid.generate_vcall_entries = true;
9168 /* The first vbase or vcall offset is at index -3 in the vtable. */
9169 vid.index = ssize_int(-3 * TARGET_VTABLE_DATA_ENTRY_DISTANCE);
9171 /* Add entries to the vtable for RTTI. */
9172 build_rtti_vtbl_entries (binfo, &vid);
9174 /* Create an array for keeping track of the functions we've
9175 processed. When we see multiple functions with the same
9176 signature, we share the vcall offsets. */
9177 vec_alloc (vid.fns, 32);
9178 /* Add the vcall and vbase offset entries. */
9179 build_vcall_and_vbase_vtbl_entries (binfo, &vid);
9181 /* Clear BINFO_VTABLE_PATH_MARKED; it's set by
9182 build_vbase_offset_vtbl_entries. */
9183 for (vbases = CLASSTYPE_VBASECLASSES (t), ix = 0;
9184 vec_safe_iterate (vbases, ix, &vbinfo); ix++)
9185 BINFO_VTABLE_PATH_MARKED (vbinfo) = 0;
9187 /* If the target requires padding between data entries, add that now. */
9188 if (TARGET_VTABLE_DATA_ENTRY_DISTANCE > 1)
9190 int n_entries = vec_safe_length (vid.inits);
9192 vec_safe_grow (vid.inits, TARGET_VTABLE_DATA_ENTRY_DISTANCE * n_entries);
9194 /* Move data entries into their new positions and add padding
9195 after the new positions. Iterate backwards so we don't
9196 overwrite entries that we would need to process later. */
9197 for (ix = n_entries - 1;
9198 vid.inits->iterate (ix, &e);
9199 ix--)
9201 int j;
9202 int new_position = (TARGET_VTABLE_DATA_ENTRY_DISTANCE * ix
9203 + (TARGET_VTABLE_DATA_ENTRY_DISTANCE - 1));
9205 (*vid.inits)[new_position] = *e;
9207 for (j = 1; j < TARGET_VTABLE_DATA_ENTRY_DISTANCE; ++j)
9209 constructor_elt *f = &(*vid.inits)[new_position - j];
9210 f->index = NULL_TREE;
9211 f->value = build1 (NOP_EXPR, vtable_entry_type,
9212 null_pointer_node);
9217 if (non_fn_entries_p)
9218 *non_fn_entries_p = vec_safe_length (vid.inits);
9220 /* The initializers for virtual functions were built up in reverse
9221 order. Straighten them out and add them to the running list in one
9222 step. */
9223 jx = vec_safe_length (*inits);
9224 vec_safe_grow (*inits, jx + vid.inits->length ());
9226 for (ix = vid.inits->length () - 1;
9227 vid.inits->iterate (ix, &e);
9228 ix--, jx++)
9229 (**inits)[jx] = *e;
9231 /* Go through all the ordinary virtual functions, building up
9232 initializers. */
9233 for (v = BINFO_VIRTUALS (orig_binfo); v; v = TREE_CHAIN (v))
9235 tree delta;
9236 tree vcall_index;
9237 tree fn, fn_original;
9238 tree init = NULL_TREE;
9240 fn = BV_FN (v);
9241 fn_original = fn;
9242 if (DECL_THUNK_P (fn))
9244 if (!DECL_NAME (fn))
9245 finish_thunk (fn);
9246 if (THUNK_ALIAS (fn))
9248 fn = THUNK_ALIAS (fn);
9249 BV_FN (v) = fn;
9251 fn_original = THUNK_TARGET (fn);
9254 /* If the only definition of this function signature along our
9255 primary base chain is from a lost primary, this vtable slot will
9256 never be used, so just zero it out. This is important to avoid
9257 requiring extra thunks which cannot be generated with the function.
9259 We first check this in update_vtable_entry_for_fn, so we handle
9260 restored primary bases properly; we also need to do it here so we
9261 zero out unused slots in ctor vtables, rather than filling them
9262 with erroneous values (though harmless, apart from relocation
9263 costs). */
9264 if (BV_LOST_PRIMARY (v))
9265 init = size_zero_node;
9267 if (! init)
9269 /* Pull the offset for `this', and the function to call, out of
9270 the list. */
9271 delta = BV_DELTA (v);
9272 vcall_index = BV_VCALL_INDEX (v);
9274 gcc_assert (TREE_CODE (delta) == INTEGER_CST);
9275 gcc_assert (TREE_CODE (fn) == FUNCTION_DECL);
9277 /* You can't call an abstract virtual function; it's abstract.
9278 So, we replace these functions with __pure_virtual. */
9279 if (DECL_PURE_VIRTUAL_P (fn_original))
9281 fn = abort_fndecl;
9282 if (!TARGET_VTABLE_USES_DESCRIPTORS)
9284 if (abort_fndecl_addr == NULL)
9285 abort_fndecl_addr
9286 = fold_convert (vfunc_ptr_type_node,
9287 build_fold_addr_expr (fn));
9288 init = abort_fndecl_addr;
9291 /* Likewise for deleted virtuals. */
9292 else if (DECL_DELETED_FN (fn_original))
9294 if (!dvirt_fn)
9296 tree name = get_identifier ("__cxa_deleted_virtual");
9297 dvirt_fn = get_global_binding (name);
9298 if (!dvirt_fn)
9299 dvirt_fn = push_library_fn
9300 (name,
9301 build_function_type_list (void_type_node, NULL_TREE),
9302 NULL_TREE, ECF_NORETURN | ECF_COLD);
9304 fn = dvirt_fn;
9305 if (!TARGET_VTABLE_USES_DESCRIPTORS)
9306 init = fold_convert (vfunc_ptr_type_node,
9307 build_fold_addr_expr (fn));
9309 else
9311 if (!integer_zerop (delta) || vcall_index)
9313 fn = make_thunk (fn, /*this_adjusting=*/1,
9314 delta, vcall_index);
9315 if (!DECL_NAME (fn))
9316 finish_thunk (fn);
9318 /* Take the address of the function, considering it to be of an
9319 appropriate generic type. */
9320 if (!TARGET_VTABLE_USES_DESCRIPTORS)
9321 init = fold_convert (vfunc_ptr_type_node,
9322 build_fold_addr_expr (fn));
9323 /* Don't refer to a virtual destructor from a constructor
9324 vtable or a vtable for an abstract class, since destroying
9325 an object under construction is undefined behavior and we
9326 don't want it to be considered a candidate for speculative
9327 devirtualization. But do create the thunk for ABI
9328 compliance. */
9329 if (DECL_DESTRUCTOR_P (fn_original)
9330 && (CLASSTYPE_PURE_VIRTUALS (DECL_CONTEXT (fn_original))
9331 || orig_binfo != binfo))
9332 init = size_zero_node;
9336 /* And add it to the chain of initializers. */
9337 if (TARGET_VTABLE_USES_DESCRIPTORS)
9339 int i;
9340 if (init == size_zero_node)
9341 for (i = 0; i < TARGET_VTABLE_USES_DESCRIPTORS; ++i)
9342 CONSTRUCTOR_APPEND_ELT (*inits, NULL_TREE, init);
9343 else
9344 for (i = 0; i < TARGET_VTABLE_USES_DESCRIPTORS; ++i)
9346 tree fdesc = build2 (FDESC_EXPR, vfunc_ptr_type_node,
9347 fn, build_int_cst (NULL_TREE, i));
9348 TREE_CONSTANT (fdesc) = 1;
9350 CONSTRUCTOR_APPEND_ELT (*inits, NULL_TREE, fdesc);
9353 else
9354 CONSTRUCTOR_APPEND_ELT (*inits, NULL_TREE, init);
9358 /* Adds to vid->inits the initializers for the vbase and vcall
9359 offsets in BINFO, which is in the hierarchy dominated by T. */
9361 static void
9362 build_vcall_and_vbase_vtbl_entries (tree binfo, vtbl_init_data* vid)
9364 tree b;
9366 /* If this is a derived class, we must first create entries
9367 corresponding to the primary base class. */
9368 b = get_primary_binfo (binfo);
9369 if (b)
9370 build_vcall_and_vbase_vtbl_entries (b, vid);
9372 /* Add the vbase entries for this base. */
9373 build_vbase_offset_vtbl_entries (binfo, vid);
9374 /* Add the vcall entries for this base. */
9375 build_vcall_offset_vtbl_entries (binfo, vid);
9378 /* Returns the initializers for the vbase offset entries in the vtable
9379 for BINFO (which is part of the class hierarchy dominated by T), in
9380 reverse order. VBASE_OFFSET_INDEX gives the vtable index
9381 where the next vbase offset will go. */
9383 static void
9384 build_vbase_offset_vtbl_entries (tree binfo, vtbl_init_data* vid)
9386 tree vbase;
9387 tree t;
9388 tree non_primary_binfo;
9390 /* If there are no virtual baseclasses, then there is nothing to
9391 do. */
9392 if (!CLASSTYPE_VBASECLASSES (BINFO_TYPE (binfo)))
9393 return;
9395 t = vid->derived;
9397 /* We might be a primary base class. Go up the inheritance hierarchy
9398 until we find the most derived class of which we are a primary base:
9399 it is the offset of that which we need to use. */
9400 non_primary_binfo = binfo;
9401 while (BINFO_INHERITANCE_CHAIN (non_primary_binfo))
9403 tree b;
9405 /* If we have reached a virtual base, then it must be a primary
9406 base (possibly multi-level) of vid->binfo, or we wouldn't
9407 have called build_vcall_and_vbase_vtbl_entries for it. But it
9408 might be a lost primary, so just skip down to vid->binfo. */
9409 if (BINFO_VIRTUAL_P (non_primary_binfo))
9411 non_primary_binfo = vid->binfo;
9412 break;
9415 b = BINFO_INHERITANCE_CHAIN (non_primary_binfo);
9416 if (get_primary_binfo (b) != non_primary_binfo)
9417 break;
9418 non_primary_binfo = b;
9421 /* Go through the virtual bases, adding the offsets. */
9422 for (vbase = TYPE_BINFO (BINFO_TYPE (binfo));
9423 vbase;
9424 vbase = TREE_CHAIN (vbase))
9426 tree b;
9427 tree delta;
9429 if (!BINFO_VIRTUAL_P (vbase))
9430 continue;
9432 /* Find the instance of this virtual base in the complete
9433 object. */
9434 b = copied_binfo (vbase, binfo);
9436 /* If we've already got an offset for this virtual base, we
9437 don't need another one. */
9438 if (BINFO_VTABLE_PATH_MARKED (b))
9439 continue;
9440 BINFO_VTABLE_PATH_MARKED (b) = 1;
9442 /* Figure out where we can find this vbase offset. */
9443 delta = size_binop (MULT_EXPR,
9444 vid->index,
9445 fold_convert (ssizetype,
9446 TYPE_SIZE_UNIT (vtable_entry_type)));
9447 if (vid->primary_vtbl_p)
9448 BINFO_VPTR_FIELD (b) = delta;
9450 if (binfo != TYPE_BINFO (t))
9451 /* The vbase offset had better be the same. */
9452 gcc_assert (tree_int_cst_equal (delta, BINFO_VPTR_FIELD (vbase)));
9454 /* The next vbase will come at a more negative offset. */
9455 vid->index = size_binop (MINUS_EXPR, vid->index,
9456 ssize_int (TARGET_VTABLE_DATA_ENTRY_DISTANCE));
9458 /* The initializer is the delta from BINFO to this virtual base.
9459 The vbase offsets go in reverse inheritance-graph order, and
9460 we are walking in inheritance graph order so these end up in
9461 the right order. */
9462 delta = size_diffop_loc (input_location,
9463 BINFO_OFFSET (b), BINFO_OFFSET (non_primary_binfo));
9465 CONSTRUCTOR_APPEND_ELT (vid->inits, NULL_TREE,
9466 fold_build1_loc (input_location, NOP_EXPR,
9467 vtable_entry_type, delta));
9471 /* Adds the initializers for the vcall offset entries in the vtable
9472 for BINFO (which is part of the class hierarchy dominated by VID->DERIVED)
9473 to VID->INITS. */
9475 static void
9476 build_vcall_offset_vtbl_entries (tree binfo, vtbl_init_data* vid)
9478 /* We only need these entries if this base is a virtual base. We
9479 compute the indices -- but do not add to the vtable -- when
9480 building the main vtable for a class. */
9481 if (binfo == TYPE_BINFO (vid->derived)
9482 || (BINFO_VIRTUAL_P (binfo)
9483 /* If BINFO is RTTI_BINFO, then (since BINFO does not
9484 correspond to VID->DERIVED), we are building a primary
9485 construction virtual table. Since this is a primary
9486 virtual table, we do not need the vcall offsets for
9487 BINFO. */
9488 && binfo != vid->rtti_binfo))
9490 /* We need a vcall offset for each of the virtual functions in this
9491 vtable. For example:
9493 class A { virtual void f (); };
9494 class B1 : virtual public A { virtual void f (); };
9495 class B2 : virtual public A { virtual void f (); };
9496 class C: public B1, public B2 { virtual void f (); };
9498 A C object has a primary base of B1, which has a primary base of A. A
9499 C also has a secondary base of B2, which no longer has a primary base
9500 of A. So the B2-in-C construction vtable needs a secondary vtable for
9501 A, which will adjust the A* to a B2* to call f. We have no way of
9502 knowing what (or even whether) this offset will be when we define B2,
9503 so we store this "vcall offset" in the A sub-vtable and look it up in
9504 a "virtual thunk" for B2::f.
9506 We need entries for all the functions in our primary vtable and
9507 in our non-virtual bases' secondary vtables. */
9508 vid->vbase = binfo;
9509 /* If we are just computing the vcall indices -- but do not need
9510 the actual entries -- not that. */
9511 if (!BINFO_VIRTUAL_P (binfo))
9512 vid->generate_vcall_entries = false;
9513 /* Now, walk through the non-virtual bases, adding vcall offsets. */
9514 add_vcall_offset_vtbl_entries_r (binfo, vid);
9518 /* Build vcall offsets, starting with those for BINFO. */
9520 static void
9521 add_vcall_offset_vtbl_entries_r (tree binfo, vtbl_init_data* vid)
9523 int i;
9524 tree primary_binfo;
9525 tree base_binfo;
9527 /* Don't walk into virtual bases -- except, of course, for the
9528 virtual base for which we are building vcall offsets. Any
9529 primary virtual base will have already had its offsets generated
9530 through the recursion in build_vcall_and_vbase_vtbl_entries. */
9531 if (BINFO_VIRTUAL_P (binfo) && vid->vbase != binfo)
9532 return;
9534 /* If BINFO has a primary base, process it first. */
9535 primary_binfo = get_primary_binfo (binfo);
9536 if (primary_binfo)
9537 add_vcall_offset_vtbl_entries_r (primary_binfo, vid);
9539 /* Add BINFO itself to the list. */
9540 add_vcall_offset_vtbl_entries_1 (binfo, vid);
9542 /* Scan the non-primary bases of BINFO. */
9543 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
9544 if (base_binfo != primary_binfo)
9545 add_vcall_offset_vtbl_entries_r (base_binfo, vid);
9548 /* Called from build_vcall_offset_vtbl_entries_r. */
9550 static void
9551 add_vcall_offset_vtbl_entries_1 (tree binfo, vtbl_init_data* vid)
9553 /* Make entries for the rest of the virtuals. */
9554 tree orig_fn;
9556 /* The ABI requires that the methods be processed in declaration
9557 order. */
9558 for (orig_fn = TYPE_FIELDS (BINFO_TYPE (binfo));
9559 orig_fn;
9560 orig_fn = DECL_CHAIN (orig_fn))
9561 if (TREE_CODE (orig_fn) == FUNCTION_DECL && DECL_VINDEX (orig_fn))
9562 add_vcall_offset (orig_fn, binfo, vid);
9565 /* Add a vcall offset entry for ORIG_FN to the vtable. */
9567 static void
9568 add_vcall_offset (tree orig_fn, tree binfo, vtbl_init_data *vid)
9570 size_t i;
9571 tree vcall_offset;
9572 tree derived_entry;
9574 /* If there is already an entry for a function with the same
9575 signature as FN, then we do not need a second vcall offset.
9576 Check the list of functions already present in the derived
9577 class vtable. */
9578 FOR_EACH_VEC_SAFE_ELT (vid->fns, i, derived_entry)
9580 if (same_signature_p (derived_entry, orig_fn)
9581 /* We only use one vcall offset for virtual destructors,
9582 even though there are two virtual table entries. */
9583 || (DECL_DESTRUCTOR_P (derived_entry)
9584 && DECL_DESTRUCTOR_P (orig_fn)))
9585 return;
9588 /* If we are building these vcall offsets as part of building
9589 the vtable for the most derived class, remember the vcall
9590 offset. */
9591 if (vid->binfo == TYPE_BINFO (vid->derived))
9593 tree_pair_s elt = {orig_fn, vid->index};
9594 vec_safe_push (CLASSTYPE_VCALL_INDICES (vid->derived), elt);
9597 /* The next vcall offset will be found at a more negative
9598 offset. */
9599 vid->index = size_binop (MINUS_EXPR, vid->index,
9600 ssize_int (TARGET_VTABLE_DATA_ENTRY_DISTANCE));
9602 /* Keep track of this function. */
9603 vec_safe_push (vid->fns, orig_fn);
9605 if (vid->generate_vcall_entries)
9607 tree base;
9608 tree fn;
9610 /* Find the overriding function. */
9611 fn = find_final_overrider (vid->rtti_binfo, binfo, orig_fn);
9612 if (fn == error_mark_node)
9613 vcall_offset = build_zero_cst (vtable_entry_type);
9614 else
9616 base = TREE_VALUE (fn);
9618 /* The vbase we're working on is a primary base of
9619 vid->binfo. But it might be a lost primary, so its
9620 BINFO_OFFSET might be wrong, so we just use the
9621 BINFO_OFFSET from vid->binfo. */
9622 vcall_offset = size_diffop_loc (input_location,
9623 BINFO_OFFSET (base),
9624 BINFO_OFFSET (vid->binfo));
9625 vcall_offset = fold_build1_loc (input_location,
9626 NOP_EXPR, vtable_entry_type,
9627 vcall_offset);
9629 /* Add the initializer to the vtable. */
9630 CONSTRUCTOR_APPEND_ELT (vid->inits, NULL_TREE, vcall_offset);
9634 /* Return vtbl initializers for the RTTI entries corresponding to the
9635 BINFO's vtable. The RTTI entries should indicate the object given
9636 by VID->rtti_binfo. */
9638 static void
9639 build_rtti_vtbl_entries (tree binfo, vtbl_init_data* vid)
9641 tree b;
9642 tree t;
9643 tree offset;
9644 tree decl;
9645 tree init;
9647 t = BINFO_TYPE (vid->rtti_binfo);
9649 /* To find the complete object, we will first convert to our most
9650 primary base, and then add the offset in the vtbl to that value. */
9651 b = most_primary_binfo (binfo);
9652 offset = size_diffop_loc (input_location,
9653 BINFO_OFFSET (vid->rtti_binfo), BINFO_OFFSET (b));
9655 /* The second entry is the address of the typeinfo object. */
9656 if (flag_rtti)
9657 decl = build_address (get_tinfo_decl (t));
9658 else
9659 decl = integer_zero_node;
9661 /* Convert the declaration to a type that can be stored in the
9662 vtable. */
9663 init = build_nop (vfunc_ptr_type_node, decl);
9664 CONSTRUCTOR_APPEND_ELT (vid->inits, NULL_TREE, init);
9666 /* Add the offset-to-top entry. It comes earlier in the vtable than
9667 the typeinfo entry. Convert the offset to look like a
9668 function pointer, so that we can put it in the vtable. */
9669 init = build_nop (vfunc_ptr_type_node, offset);
9670 CONSTRUCTOR_APPEND_ELT (vid->inits, NULL_TREE, init);
9673 /* TRUE iff TYPE is uniquely derived from PARENT. Ignores
9674 accessibility. */
9676 bool
9677 uniquely_derived_from_p (tree parent, tree type)
9679 tree base = lookup_base (type, parent, ba_unique, NULL, tf_none);
9680 return base && base != error_mark_node;
9683 /* TRUE iff TYPE is publicly & uniquely derived from PARENT. */
9685 bool
9686 publicly_uniquely_derived_p (tree parent, tree type)
9688 tree base = lookup_base (type, parent, ba_ignore_scope | ba_check,
9689 NULL, tf_none);
9690 return base && base != error_mark_node;
9693 /* CTX1 and CTX2 are declaration contexts. Return the innermost common
9694 class between them, if any. */
9696 tree
9697 common_enclosing_class (tree ctx1, tree ctx2)
9699 if (!TYPE_P (ctx1) || !TYPE_P (ctx2))
9700 return NULL_TREE;
9701 gcc_assert (ctx1 == TYPE_MAIN_VARIANT (ctx1)
9702 && ctx2 == TYPE_MAIN_VARIANT (ctx2));
9703 if (ctx1 == ctx2)
9704 return ctx1;
9705 for (tree t = ctx1; TYPE_P (t); t = TYPE_CONTEXT (t))
9706 TYPE_MARKED_P (t) = true;
9707 tree found = NULL_TREE;
9708 for (tree t = ctx2; TYPE_P (t); t = TYPE_CONTEXT (t))
9709 if (TYPE_MARKED_P (t))
9711 found = t;
9712 break;
9714 for (tree t = ctx1; TYPE_P (t); t = TYPE_CONTEXT (t))
9715 TYPE_MARKED_P (t) = false;
9716 return found;
9719 #include "gt-cp-class.h"