ChangeLog fix
[official-gcc.git] / gcc / cp / class.c
blobab68b9be42a661c18feb4c159d1854f05c1279a0
1 /* Functions related to building classes and their related objects.
2 Copyright (C) 1987-2018 Free Software Foundation, Inc.
3 Contributed by Michael Tiemann (tiemann@cygnus.com)
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
10 any later version.
12 GCC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
22 /* High-level class interface. */
24 #include "config.h"
25 #include "system.h"
26 #include "coretypes.h"
27 #include "target.h"
28 #include "cp-tree.h"
29 #include "stringpool.h"
30 #include "cgraph.h"
31 #include "stor-layout.h"
32 #include "attribs.h"
33 #include "flags.h"
34 #include "toplev.h"
35 #include "convert.h"
36 #include "dumpfile.h"
37 #include "gimplify.h"
38 #include "intl.h"
39 #include "asan.h"
41 /* Id for dumping the class hierarchy. */
42 int class_dump_id;
44 /* The number of nested classes being processed. If we are not in the
45 scope of any class, this is zero. */
47 int current_class_depth;
49 /* In order to deal with nested classes, we keep a stack of classes.
50 The topmost entry is the innermost class, and is the entry at index
51 CURRENT_CLASS_DEPTH */
53 typedef struct class_stack_node {
54 /* The name of the class. */
55 tree name;
57 /* The _TYPE node for the class. */
58 tree type;
60 /* The access specifier pending for new declarations in the scope of
61 this class. */
62 tree access;
64 /* If were defining TYPE, the names used in this class. */
65 splay_tree names_used;
67 /* Nonzero if this class is no longer open, because of a call to
68 push_to_top_level. */
69 size_t hidden;
70 }* class_stack_node_t;
72 struct vtbl_init_data
74 /* The base for which we're building initializers. */
75 tree binfo;
76 /* The type of the most-derived type. */
77 tree derived;
78 /* The binfo for the dynamic type. This will be TYPE_BINFO (derived),
79 unless ctor_vtbl_p is true. */
80 tree rtti_binfo;
81 /* The negative-index vtable initializers built up so far. These
82 are in order from least negative index to most negative index. */
83 vec<constructor_elt, va_gc> *inits;
84 /* The binfo for the virtual base for which we're building
85 vcall offset initializers. */
86 tree vbase;
87 /* The functions in vbase for which we have already provided vcall
88 offsets. */
89 vec<tree, va_gc> *fns;
90 /* The vtable index of the next vcall or vbase offset. */
91 tree index;
92 /* Nonzero if we are building the initializer for the primary
93 vtable. */
94 int primary_vtbl_p;
95 /* Nonzero if we are building the initializer for a construction
96 vtable. */
97 int ctor_vtbl_p;
98 /* True when adding vcall offset entries to the vtable. False when
99 merely computing the indices. */
100 bool generate_vcall_entries;
103 /* The type of a function passed to walk_subobject_offsets. */
104 typedef int (*subobject_offset_fn) (tree, tree, splay_tree);
106 /* The stack itself. This is a dynamically resized array. The
107 number of elements allocated is CURRENT_CLASS_STACK_SIZE. */
108 static int current_class_stack_size;
109 static class_stack_node_t current_class_stack;
111 /* The size of the largest empty class seen in this translation unit. */
112 static GTY (()) tree sizeof_biggest_empty_class;
114 /* An array of all local classes present in this translation unit, in
115 declaration order. */
116 vec<tree, va_gc> *local_classes;
118 static tree get_vfield_name (tree);
119 static void finish_struct_anon (tree);
120 static tree get_vtable_name (tree);
121 static void get_basefndecls (tree, tree, vec<tree> *);
122 static int build_primary_vtable (tree, tree);
123 static int build_secondary_vtable (tree);
124 static void finish_vtbls (tree);
125 static void modify_vtable_entry (tree, tree, tree, tree, tree *);
126 static void finish_struct_bits (tree);
127 static int alter_access (tree, tree, tree);
128 static void handle_using_decl (tree, tree);
129 static tree dfs_modify_vtables (tree, void *);
130 static tree modify_all_vtables (tree, tree);
131 static void determine_primary_bases (tree);
132 static void maybe_warn_about_overly_private_class (tree);
133 static void add_implicitly_declared_members (tree, tree*, int, int);
134 static tree fixed_type_or_null (tree, int *, int *);
135 static tree build_simple_base_path (tree expr, tree binfo);
136 static tree build_vtbl_ref_1 (tree, tree);
137 static void build_vtbl_initializer (tree, tree, tree, tree, int *,
138 vec<constructor_elt, va_gc> **);
139 static bool check_bitfield_decl (tree);
140 static bool check_field_decl (tree, tree, int *, int *);
141 static void check_field_decls (tree, tree *, int *, int *);
142 static tree *build_base_field (record_layout_info, tree, splay_tree, tree *);
143 static void build_base_fields (record_layout_info, splay_tree, tree *);
144 static void check_methods (tree);
145 static void remove_zero_width_bit_fields (tree);
146 static bool accessible_nvdtor_p (tree);
148 /* Used by find_flexarrays and related functions. */
149 struct flexmems_t;
150 static void diagnose_flexarrays (tree, const flexmems_t *);
151 static void find_flexarrays (tree, flexmems_t *, bool = false,
152 tree = NULL_TREE, tree = NULL_TREE);
153 static void check_flexarrays (tree, flexmems_t * = NULL, bool = false);
154 static void check_bases (tree, int *, int *);
155 static void check_bases_and_members (tree);
156 static tree create_vtable_ptr (tree, tree *);
157 static void include_empty_classes (record_layout_info);
158 static void layout_class_type (tree, tree *);
159 static void propagate_binfo_offsets (tree, tree);
160 static void layout_virtual_bases (record_layout_info, splay_tree);
161 static void build_vbase_offset_vtbl_entries (tree, vtbl_init_data *);
162 static void add_vcall_offset_vtbl_entries_r (tree, vtbl_init_data *);
163 static void add_vcall_offset_vtbl_entries_1 (tree, vtbl_init_data *);
164 static void build_vcall_offset_vtbl_entries (tree, vtbl_init_data *);
165 static void add_vcall_offset (tree, tree, vtbl_init_data *);
166 static void layout_vtable_decl (tree, int);
167 static tree dfs_find_final_overrider_pre (tree, void *);
168 static tree dfs_find_final_overrider_post (tree, void *);
169 static tree find_final_overrider (tree, tree, tree);
170 static int make_new_vtable (tree, tree);
171 static tree get_primary_binfo (tree);
172 static int maybe_indent_hierarchy (FILE *, int, int);
173 static tree dump_class_hierarchy_r (FILE *, dump_flags_t, tree, tree, int);
174 static void dump_class_hierarchy (tree);
175 static void dump_class_hierarchy_1 (FILE *, dump_flags_t, tree);
176 static void dump_array (FILE *, tree);
177 static void dump_vtable (tree, tree, tree);
178 static void dump_vtt (tree, tree);
179 static void dump_thunk (FILE *, int, tree);
180 static tree build_vtable (tree, tree, tree);
181 static void initialize_vtable (tree, vec<constructor_elt, va_gc> *);
182 static void layout_nonempty_base_or_field (record_layout_info,
183 tree, tree, splay_tree);
184 static tree end_of_class (tree, int);
185 static bool layout_empty_base (record_layout_info, tree, tree, splay_tree);
186 static void accumulate_vtbl_inits (tree, tree, tree, tree, tree,
187 vec<constructor_elt, va_gc> **);
188 static void dfs_accumulate_vtbl_inits (tree, tree, tree, tree, tree,
189 vec<constructor_elt, va_gc> **);
190 static void build_rtti_vtbl_entries (tree, vtbl_init_data *);
191 static void build_vcall_and_vbase_vtbl_entries (tree, vtbl_init_data *);
192 static void clone_constructors_and_destructors (tree);
193 static tree build_clone (tree, tree);
194 static void update_vtable_entry_for_fn (tree, tree, tree, tree *, unsigned);
195 static void build_ctor_vtbl_group (tree, tree);
196 static void build_vtt (tree);
197 static tree binfo_ctor_vtable (tree);
198 static void build_vtt_inits (tree, tree, vec<constructor_elt, va_gc> **,
199 tree *);
200 static tree dfs_build_secondary_vptr_vtt_inits (tree, void *);
201 static tree dfs_fixup_binfo_vtbls (tree, void *);
202 static int record_subobject_offset (tree, tree, splay_tree);
203 static int check_subobject_offset (tree, tree, splay_tree);
204 static int walk_subobject_offsets (tree, subobject_offset_fn,
205 tree, splay_tree, tree, int);
206 static void record_subobject_offsets (tree, tree, splay_tree, bool);
207 static int layout_conflict_p (tree, tree, splay_tree, int);
208 static int splay_tree_compare_integer_csts (splay_tree_key k1,
209 splay_tree_key k2);
210 static void warn_about_ambiguous_bases (tree);
211 static bool type_requires_array_cookie (tree);
212 static bool base_derived_from (tree, tree);
213 static int empty_base_at_nonzero_offset_p (tree, tree, splay_tree);
214 static tree end_of_base (tree);
215 static tree get_vcall_index (tree, tree);
216 static bool type_maybe_constexpr_default_constructor (tree);
218 /* Return a COND_EXPR that executes TRUE_STMT if this execution of the
219 'structor is in charge of 'structing virtual bases, or FALSE_STMT
220 otherwise. */
222 tree
223 build_if_in_charge (tree true_stmt, tree false_stmt)
225 gcc_assert (DECL_HAS_IN_CHARGE_PARM_P (current_function_decl));
226 tree cmp = build2 (NE_EXPR, boolean_type_node,
227 current_in_charge_parm, integer_zero_node);
228 tree type = unlowered_expr_type (true_stmt);
229 if (VOID_TYPE_P (type))
230 type = unlowered_expr_type (false_stmt);
231 tree cond = build3 (COND_EXPR, type,
232 cmp, true_stmt, false_stmt);
233 return cond;
236 /* Convert to or from a base subobject. EXPR is an expression of type
237 `A' or `A*', an expression of type `B' or `B*' is returned. To
238 convert A to a base B, CODE is PLUS_EXPR and BINFO is the binfo for
239 the B base instance within A. To convert base A to derived B, CODE
240 is MINUS_EXPR and BINFO is the binfo for the A instance within B.
241 In this latter case, A must not be a morally virtual base of B.
242 NONNULL is true if EXPR is known to be non-NULL (this is only
243 needed when EXPR is of pointer type). CV qualifiers are preserved
244 from EXPR. */
246 tree
247 build_base_path (enum tree_code code,
248 tree expr,
249 tree binfo,
250 int nonnull,
251 tsubst_flags_t complain)
253 tree v_binfo = NULL_TREE;
254 tree d_binfo = NULL_TREE;
255 tree probe;
256 tree offset;
257 tree target_type;
258 tree null_test = NULL;
259 tree ptr_target_type;
260 int fixed_type_p;
261 int want_pointer = TYPE_PTR_P (TREE_TYPE (expr));
262 bool has_empty = false;
263 bool virtual_access;
264 bool rvalue = false;
266 if (expr == error_mark_node || binfo == error_mark_node || !binfo)
267 return error_mark_node;
269 for (probe = binfo; probe; probe = BINFO_INHERITANCE_CHAIN (probe))
271 d_binfo = probe;
272 if (is_empty_class (BINFO_TYPE (probe)))
273 has_empty = true;
274 if (!v_binfo && BINFO_VIRTUAL_P (probe))
275 v_binfo = probe;
278 probe = TYPE_MAIN_VARIANT (TREE_TYPE (expr));
279 if (want_pointer)
280 probe = TYPE_MAIN_VARIANT (TREE_TYPE (probe));
282 if (code == PLUS_EXPR
283 && !SAME_BINFO_TYPE_P (BINFO_TYPE (d_binfo), probe))
285 /* This can happen when adjust_result_of_qualified_name_lookup can't
286 find a unique base binfo in a call to a member function. We
287 couldn't give the diagnostic then since we might have been calling
288 a static member function, so we do it now. In other cases, eg.
289 during error recovery (c++/71979), we may not have a base at all. */
290 if (complain & tf_error)
292 tree base = lookup_base (probe, BINFO_TYPE (d_binfo),
293 ba_unique, NULL, complain);
294 gcc_assert (base == error_mark_node || !base);
296 return error_mark_node;
299 gcc_assert ((code == MINUS_EXPR
300 && SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), probe))
301 || code == PLUS_EXPR);
303 if (binfo == d_binfo)
304 /* Nothing to do. */
305 return expr;
307 if (code == MINUS_EXPR && v_binfo)
309 if (complain & tf_error)
311 if (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), BINFO_TYPE (v_binfo)))
313 if (want_pointer)
314 error ("cannot convert from pointer to base class %qT to "
315 "pointer to derived class %qT because the base is "
316 "virtual", BINFO_TYPE (binfo), BINFO_TYPE (d_binfo));
317 else
318 error ("cannot convert from base class %qT to derived "
319 "class %qT because the base is virtual",
320 BINFO_TYPE (binfo), BINFO_TYPE (d_binfo));
322 else
324 if (want_pointer)
325 error ("cannot convert from pointer to base class %qT to "
326 "pointer to derived class %qT via virtual base %qT",
327 BINFO_TYPE (binfo), BINFO_TYPE (d_binfo),
328 BINFO_TYPE (v_binfo));
329 else
330 error ("cannot convert from base class %qT to derived "
331 "class %qT via virtual base %qT", BINFO_TYPE (binfo),
332 BINFO_TYPE (d_binfo), BINFO_TYPE (v_binfo));
335 return error_mark_node;
338 if (!want_pointer)
340 rvalue = !lvalue_p (expr);
341 /* This must happen before the call to save_expr. */
342 expr = cp_build_addr_expr (expr, complain);
344 else
345 expr = mark_rvalue_use (expr);
347 offset = BINFO_OFFSET (binfo);
348 fixed_type_p = resolves_to_fixed_type_p (expr, &nonnull);
349 target_type = code == PLUS_EXPR ? BINFO_TYPE (binfo) : BINFO_TYPE (d_binfo);
350 /* TARGET_TYPE has been extracted from BINFO, and, is therefore always
351 cv-unqualified. Extract the cv-qualifiers from EXPR so that the
352 expression returned matches the input. */
353 target_type = cp_build_qualified_type
354 (target_type, cp_type_quals (TREE_TYPE (TREE_TYPE (expr))));
355 ptr_target_type = build_pointer_type (target_type);
357 /* Do we need to look in the vtable for the real offset? */
358 virtual_access = (v_binfo && fixed_type_p <= 0);
360 /* Don't bother with the calculations inside sizeof; they'll ICE if the
361 source type is incomplete and the pointer value doesn't matter. In a
362 template (even in instantiate_non_dependent_expr), we don't have vtables
363 set up properly yet, and the value doesn't matter there either; we're
364 just interested in the result of overload resolution. */
365 if (cp_unevaluated_operand != 0
366 || processing_template_decl
367 || in_template_function ())
369 expr = build_nop (ptr_target_type, expr);
370 goto indout;
373 if (!COMPLETE_TYPE_P (probe))
375 if (complain & tf_error)
376 error ("cannot convert from %qT to base class %qT because %qT is "
377 "incomplete", BINFO_TYPE (d_binfo), BINFO_TYPE (binfo),
378 BINFO_TYPE (d_binfo));
379 return error_mark_node;
382 /* If we're in an NSDMI, we don't have the full constructor context yet
383 that we need for converting to a virtual base, so just build a stub
384 CONVERT_EXPR and expand it later in bot_replace. */
385 if (virtual_access && fixed_type_p < 0
386 && current_scope () != current_function_decl)
388 expr = build1 (CONVERT_EXPR, ptr_target_type, expr);
389 CONVERT_EXPR_VBASE_PATH (expr) = true;
390 goto indout;
393 /* Do we need to check for a null pointer? */
394 if (want_pointer && !nonnull)
396 /* If we know the conversion will not actually change the value
397 of EXPR, then we can avoid testing the expression for NULL.
398 We have to avoid generating a COMPONENT_REF for a base class
399 field, because other parts of the compiler know that such
400 expressions are always non-NULL. */
401 if (!virtual_access && integer_zerop (offset))
402 return build_nop (ptr_target_type, expr);
403 null_test = error_mark_node;
406 /* Protect against multiple evaluation if necessary. */
407 if (TREE_SIDE_EFFECTS (expr) && (null_test || virtual_access))
408 expr = save_expr (expr);
410 /* Now that we've saved expr, build the real null test. */
411 if (null_test)
413 tree zero = cp_convert (TREE_TYPE (expr), nullptr_node, complain);
414 null_test = build2_loc (input_location, NE_EXPR, boolean_type_node,
415 expr, zero);
416 /* This is a compiler generated comparison, don't emit
417 e.g. -Wnonnull-compare warning for it. */
418 TREE_NO_WARNING (null_test) = 1;
421 /* If this is a simple base reference, express it as a COMPONENT_REF. */
422 if (code == PLUS_EXPR && !virtual_access
423 /* We don't build base fields for empty bases, and they aren't very
424 interesting to the optimizers anyway. */
425 && !has_empty)
427 expr = cp_build_fold_indirect_ref (expr);
428 expr = build_simple_base_path (expr, binfo);
429 if (rvalue && lvalue_p (expr))
430 expr = move (expr);
431 if (want_pointer)
432 expr = build_address (expr);
433 target_type = TREE_TYPE (expr);
434 goto out;
437 if (virtual_access)
439 /* Going via virtual base V_BINFO. We need the static offset
440 from V_BINFO to BINFO, and the dynamic offset from D_BINFO to
441 V_BINFO. That offset is an entry in D_BINFO's vtable. */
442 tree v_offset;
444 if (fixed_type_p < 0 && in_base_initializer)
446 /* In a base member initializer, we cannot rely on the
447 vtable being set up. We have to indirect via the
448 vtt_parm. */
449 tree t;
451 t = TREE_TYPE (TYPE_VFIELD (current_class_type));
452 t = build_pointer_type (t);
453 v_offset = fold_convert (t, current_vtt_parm);
454 v_offset = cp_build_fold_indirect_ref (v_offset);
456 else
458 tree t = expr;
459 if (sanitize_flags_p (SANITIZE_VPTR)
460 && fixed_type_p == 0)
462 t = cp_ubsan_maybe_instrument_cast_to_vbase (input_location,
463 probe, expr);
464 if (t == NULL_TREE)
465 t = expr;
467 v_offset = build_vfield_ref (cp_build_fold_indirect_ref (t),
468 TREE_TYPE (TREE_TYPE (expr)));
471 if (v_offset == error_mark_node)
472 return error_mark_node;
474 v_offset = fold_build_pointer_plus (v_offset, BINFO_VPTR_FIELD (v_binfo));
475 v_offset = build1 (NOP_EXPR,
476 build_pointer_type (ptrdiff_type_node),
477 v_offset);
478 v_offset = cp_build_fold_indirect_ref (v_offset);
479 TREE_CONSTANT (v_offset) = 1;
481 offset = convert_to_integer (ptrdiff_type_node,
482 size_diffop_loc (input_location, offset,
483 BINFO_OFFSET (v_binfo)));
485 if (!integer_zerop (offset))
486 v_offset = build2 (code, ptrdiff_type_node, v_offset, offset);
488 if (fixed_type_p < 0)
489 /* Negative fixed_type_p means this is a constructor or destructor;
490 virtual base layout is fixed in in-charge [cd]tors, but not in
491 base [cd]tors. */
492 offset = build_if_in_charge
493 (convert_to_integer (ptrdiff_type_node, BINFO_OFFSET (binfo)),
494 v_offset);
495 else
496 offset = v_offset;
499 if (want_pointer)
500 target_type = ptr_target_type;
502 expr = build1 (NOP_EXPR, ptr_target_type, expr);
504 if (!integer_zerop (offset))
506 offset = fold_convert (sizetype, offset);
507 if (code == MINUS_EXPR)
508 offset = fold_build1_loc (input_location, NEGATE_EXPR, sizetype, offset);
509 expr = fold_build_pointer_plus (expr, offset);
511 else
512 null_test = NULL;
514 indout:
515 if (!want_pointer)
517 expr = cp_build_fold_indirect_ref (expr);
518 if (rvalue)
519 expr = move (expr);
522 out:
523 if (null_test)
524 expr = fold_build3_loc (input_location, COND_EXPR, target_type, null_test, expr,
525 build_zero_cst (target_type));
527 return expr;
530 /* Subroutine of build_base_path; EXPR and BINFO are as in that function.
531 Perform a derived-to-base conversion by recursively building up a
532 sequence of COMPONENT_REFs to the appropriate base fields. */
534 static tree
535 build_simple_base_path (tree expr, tree binfo)
537 tree type = BINFO_TYPE (binfo);
538 tree d_binfo = BINFO_INHERITANCE_CHAIN (binfo);
539 tree field;
541 if (d_binfo == NULL_TREE)
543 tree temp;
545 gcc_assert (TYPE_MAIN_VARIANT (TREE_TYPE (expr)) == type);
547 /* Transform `(a, b).x' into `(*(a, &b)).x', `(a ? b : c).x'
548 into `(*(a ? &b : &c)).x', and so on. A COND_EXPR is only
549 an lvalue in the front end; only _DECLs and _REFs are lvalues
550 in the back end. */
551 temp = unary_complex_lvalue (ADDR_EXPR, expr);
552 if (temp)
553 expr = cp_build_fold_indirect_ref (temp);
555 return expr;
558 /* Recurse. */
559 expr = build_simple_base_path (expr, d_binfo);
561 for (field = TYPE_FIELDS (BINFO_TYPE (d_binfo));
562 field; field = DECL_CHAIN (field))
563 /* Is this the base field created by build_base_field? */
564 if (TREE_CODE (field) == FIELD_DECL
565 && DECL_FIELD_IS_BASE (field)
566 && TREE_TYPE (field) == type
567 /* If we're looking for a field in the most-derived class,
568 also check the field offset; we can have two base fields
569 of the same type if one is an indirect virtual base and one
570 is a direct non-virtual base. */
571 && (BINFO_INHERITANCE_CHAIN (d_binfo)
572 || tree_int_cst_equal (byte_position (field),
573 BINFO_OFFSET (binfo))))
575 /* We don't use build_class_member_access_expr here, as that
576 has unnecessary checks, and more importantly results in
577 recursive calls to dfs_walk_once. */
578 int type_quals = cp_type_quals (TREE_TYPE (expr));
580 expr = build3 (COMPONENT_REF,
581 cp_build_qualified_type (type, type_quals),
582 expr, field, NULL_TREE);
583 /* Mark the expression const or volatile, as appropriate.
584 Even though we've dealt with the type above, we still have
585 to mark the expression itself. */
586 if (type_quals & TYPE_QUAL_CONST)
587 TREE_READONLY (expr) = 1;
588 if (type_quals & TYPE_QUAL_VOLATILE)
589 TREE_THIS_VOLATILE (expr) = 1;
591 return expr;
594 /* Didn't find the base field?!? */
595 gcc_unreachable ();
598 /* Convert OBJECT to the base TYPE. OBJECT is an expression whose
599 type is a class type or a pointer to a class type. In the former
600 case, TYPE is also a class type; in the latter it is another
601 pointer type. If CHECK_ACCESS is true, an error message is emitted
602 if TYPE is inaccessible. If OBJECT has pointer type, the value is
603 assumed to be non-NULL. */
605 tree
606 convert_to_base (tree object, tree type, bool check_access, bool nonnull,
607 tsubst_flags_t complain)
609 tree binfo;
610 tree object_type;
612 if (TYPE_PTR_P (TREE_TYPE (object)))
614 object_type = TREE_TYPE (TREE_TYPE (object));
615 type = TREE_TYPE (type);
617 else
618 object_type = TREE_TYPE (object);
620 binfo = lookup_base (object_type, type, check_access ? ba_check : ba_unique,
621 NULL, complain);
622 if (!binfo || binfo == error_mark_node)
623 return error_mark_node;
625 return build_base_path (PLUS_EXPR, object, binfo, nonnull, complain);
628 /* EXPR is an expression with unqualified class type. BASE is a base
629 binfo of that class type. Returns EXPR, converted to the BASE
630 type. This function assumes that EXPR is the most derived class;
631 therefore virtual bases can be found at their static offsets. */
633 tree
634 convert_to_base_statically (tree expr, tree base)
636 tree expr_type;
638 expr_type = TREE_TYPE (expr);
639 if (!SAME_BINFO_TYPE_P (BINFO_TYPE (base), expr_type))
641 /* If this is a non-empty base, use a COMPONENT_REF. */
642 if (!is_empty_class (BINFO_TYPE (base)))
643 return build_simple_base_path (expr, base);
645 /* We use fold_build2 and fold_convert below to simplify the trees
646 provided to the optimizers. It is not safe to call these functions
647 when processing a template because they do not handle C++-specific
648 trees. */
649 gcc_assert (!processing_template_decl);
650 expr = cp_build_addr_expr (expr, tf_warning_or_error);
651 if (!integer_zerop (BINFO_OFFSET (base)))
652 expr = fold_build_pointer_plus_loc (input_location,
653 expr, BINFO_OFFSET (base));
654 expr = fold_convert (build_pointer_type (BINFO_TYPE (base)), expr);
655 expr = build_fold_indirect_ref_loc (input_location, expr);
658 return expr;
662 tree
663 build_vfield_ref (tree datum, tree type)
665 tree vfield, vcontext;
667 if (datum == error_mark_node
668 /* Can happen in case of duplicate base types (c++/59082). */
669 || !TYPE_VFIELD (type))
670 return error_mark_node;
672 /* First, convert to the requested type. */
673 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (datum), type))
674 datum = convert_to_base (datum, type, /*check_access=*/false,
675 /*nonnull=*/true, tf_warning_or_error);
677 /* Second, the requested type may not be the owner of its own vptr.
678 If not, convert to the base class that owns it. We cannot use
679 convert_to_base here, because VCONTEXT may appear more than once
680 in the inheritance hierarchy of TYPE, and thus direct conversion
681 between the types may be ambiguous. Following the path back up
682 one step at a time via primary bases avoids the problem. */
683 vfield = TYPE_VFIELD (type);
684 vcontext = DECL_CONTEXT (vfield);
685 while (!same_type_ignoring_top_level_qualifiers_p (vcontext, type))
687 datum = build_simple_base_path (datum, CLASSTYPE_PRIMARY_BINFO (type));
688 type = TREE_TYPE (datum);
691 return build3 (COMPONENT_REF, TREE_TYPE (vfield), datum, vfield, NULL_TREE);
694 /* Given an object INSTANCE, return an expression which yields the
695 vtable element corresponding to INDEX. There are many special
696 cases for INSTANCE which we take care of here, mainly to avoid
697 creating extra tree nodes when we don't have to. */
699 static tree
700 build_vtbl_ref_1 (tree instance, tree idx)
702 tree aref;
703 tree vtbl = NULL_TREE;
705 /* Try to figure out what a reference refers to, and
706 access its virtual function table directly. */
708 int cdtorp = 0;
709 tree fixed_type = fixed_type_or_null (instance, NULL, &cdtorp);
711 tree basetype = non_reference (TREE_TYPE (instance));
713 if (fixed_type && !cdtorp)
715 tree binfo = lookup_base (fixed_type, basetype,
716 ba_unique, NULL, tf_none);
717 if (binfo && binfo != error_mark_node)
718 vtbl = unshare_expr (BINFO_VTABLE (binfo));
721 if (!vtbl)
722 vtbl = build_vfield_ref (instance, basetype);
724 aref = build_array_ref (input_location, vtbl, idx);
725 TREE_CONSTANT (aref) |= TREE_CONSTANT (vtbl) && TREE_CONSTANT (idx);
727 return aref;
730 tree
731 build_vtbl_ref (tree instance, tree idx)
733 tree aref = build_vtbl_ref_1 (instance, idx);
735 return aref;
738 /* Given a stable object pointer INSTANCE_PTR, return an expression which
739 yields a function pointer corresponding to vtable element INDEX. */
741 tree
742 build_vfn_ref (tree instance_ptr, tree idx)
744 tree aref;
746 aref = build_vtbl_ref_1 (cp_build_fold_indirect_ref (instance_ptr),
747 idx);
749 /* When using function descriptors, the address of the
750 vtable entry is treated as a function pointer. */
751 if (TARGET_VTABLE_USES_DESCRIPTORS)
752 aref = build1 (NOP_EXPR, TREE_TYPE (aref),
753 cp_build_addr_expr (aref, tf_warning_or_error));
755 /* Remember this as a method reference, for later devirtualization. */
756 aref = build3 (OBJ_TYPE_REF, TREE_TYPE (aref), aref, instance_ptr, idx);
758 return aref;
761 /* Return the name of the virtual function table (as an IDENTIFIER_NODE)
762 for the given TYPE. */
764 static tree
765 get_vtable_name (tree type)
767 return mangle_vtbl_for_type (type);
770 /* DECL is an entity associated with TYPE, like a virtual table or an
771 implicitly generated constructor. Determine whether or not DECL
772 should have external or internal linkage at the object file
773 level. This routine does not deal with COMDAT linkage and other
774 similar complexities; it simply sets TREE_PUBLIC if it possible for
775 entities in other translation units to contain copies of DECL, in
776 the abstract. */
778 void
779 set_linkage_according_to_type (tree /*type*/, tree decl)
781 TREE_PUBLIC (decl) = 1;
782 determine_visibility (decl);
785 /* Create a VAR_DECL for a primary or secondary vtable for CLASS_TYPE.
786 (For a secondary vtable for B-in-D, CLASS_TYPE should be D, not B.)
787 Use NAME for the name of the vtable, and VTABLE_TYPE for its type. */
789 static tree
790 build_vtable (tree class_type, tree name, tree vtable_type)
792 tree decl;
794 decl = build_lang_decl (VAR_DECL, name, vtable_type);
795 /* vtable names are already mangled; give them their DECL_ASSEMBLER_NAME
796 now to avoid confusion in mangle_decl. */
797 SET_DECL_ASSEMBLER_NAME (decl, name);
798 DECL_CONTEXT (decl) = class_type;
799 DECL_ARTIFICIAL (decl) = 1;
800 TREE_STATIC (decl) = 1;
801 TREE_READONLY (decl) = 1;
802 DECL_VIRTUAL_P (decl) = 1;
803 SET_DECL_ALIGN (decl, TARGET_VTABLE_ENTRY_ALIGN);
804 DECL_USER_ALIGN (decl) = true;
805 DECL_VTABLE_OR_VTT_P (decl) = 1;
806 set_linkage_according_to_type (class_type, decl);
807 /* The vtable has not been defined -- yet. */
808 DECL_EXTERNAL (decl) = 1;
809 DECL_NOT_REALLY_EXTERN (decl) = 1;
811 /* Mark the VAR_DECL node representing the vtable itself as a
812 "gratuitous" one, thereby forcing dwarfout.c to ignore it. It
813 is rather important that such things be ignored because any
814 effort to actually generate DWARF for them will run into
815 trouble when/if we encounter code like:
817 #pragma interface
818 struct S { virtual void member (); };
820 because the artificial declaration of the vtable itself (as
821 manufactured by the g++ front end) will say that the vtable is
822 a static member of `S' but only *after* the debug output for
823 the definition of `S' has already been output. This causes
824 grief because the DWARF entry for the definition of the vtable
825 will try to refer back to an earlier *declaration* of the
826 vtable as a static member of `S' and there won't be one. We
827 might be able to arrange to have the "vtable static member"
828 attached to the member list for `S' before the debug info for
829 `S' get written (which would solve the problem) but that would
830 require more intrusive changes to the g++ front end. */
831 DECL_IGNORED_P (decl) = 1;
833 return decl;
836 /* Get the VAR_DECL of the vtable for TYPE. TYPE need not be polymorphic,
837 or even complete. If this does not exist, create it. If COMPLETE is
838 nonzero, then complete the definition of it -- that will render it
839 impossible to actually build the vtable, but is useful to get at those
840 which are known to exist in the runtime. */
842 tree
843 get_vtable_decl (tree type, int complete)
845 tree decl;
847 if (CLASSTYPE_VTABLES (type))
848 return CLASSTYPE_VTABLES (type);
850 decl = build_vtable (type, get_vtable_name (type), vtbl_type_node);
851 CLASSTYPE_VTABLES (type) = decl;
853 if (complete)
855 DECL_EXTERNAL (decl) = 1;
856 cp_finish_decl (decl, NULL_TREE, false, NULL_TREE, 0);
859 return decl;
862 /* Build the primary virtual function table for TYPE. If BINFO is
863 non-NULL, build the vtable starting with the initial approximation
864 that it is the same as the one which is the head of the association
865 list. Returns a nonzero value if a new vtable is actually
866 created. */
868 static int
869 build_primary_vtable (tree binfo, tree type)
871 tree decl;
872 tree virtuals;
874 decl = get_vtable_decl (type, /*complete=*/0);
876 if (binfo)
878 if (BINFO_NEW_VTABLE_MARKED (binfo))
879 /* We have already created a vtable for this base, so there's
880 no need to do it again. */
881 return 0;
883 virtuals = copy_list (BINFO_VIRTUALS (binfo));
884 TREE_TYPE (decl) = TREE_TYPE (get_vtbl_decl_for_binfo (binfo));
885 DECL_SIZE (decl) = TYPE_SIZE (TREE_TYPE (decl));
886 DECL_SIZE_UNIT (decl) = TYPE_SIZE_UNIT (TREE_TYPE (decl));
888 else
890 gcc_assert (TREE_TYPE (decl) == vtbl_type_node);
891 virtuals = NULL_TREE;
894 /* Initialize the association list for this type, based
895 on our first approximation. */
896 BINFO_VTABLE (TYPE_BINFO (type)) = decl;
897 BINFO_VIRTUALS (TYPE_BINFO (type)) = virtuals;
898 SET_BINFO_NEW_VTABLE_MARKED (TYPE_BINFO (type));
899 return 1;
902 /* Give BINFO a new virtual function table which is initialized
903 with a skeleton-copy of its original initialization. The only
904 entry that changes is the `delta' entry, so we can really
905 share a lot of structure.
907 FOR_TYPE is the most derived type which caused this table to
908 be needed.
910 Returns nonzero if we haven't met BINFO before.
912 The order in which vtables are built (by calling this function) for
913 an object must remain the same, otherwise a binary incompatibility
914 can result. */
916 static int
917 build_secondary_vtable (tree binfo)
919 if (BINFO_NEW_VTABLE_MARKED (binfo))
920 /* We already created a vtable for this base. There's no need to
921 do it again. */
922 return 0;
924 /* Remember that we've created a vtable for this BINFO, so that we
925 don't try to do so again. */
926 SET_BINFO_NEW_VTABLE_MARKED (binfo);
928 /* Make fresh virtual list, so we can smash it later. */
929 BINFO_VIRTUALS (binfo) = copy_list (BINFO_VIRTUALS (binfo));
931 /* Secondary vtables are laid out as part of the same structure as
932 the primary vtable. */
933 BINFO_VTABLE (binfo) = NULL_TREE;
934 return 1;
937 /* Create a new vtable for BINFO which is the hierarchy dominated by
938 T. Return nonzero if we actually created a new vtable. */
940 static int
941 make_new_vtable (tree t, tree binfo)
943 if (binfo == TYPE_BINFO (t))
944 /* In this case, it is *type*'s vtable we are modifying. We start
945 with the approximation that its vtable is that of the
946 immediate base class. */
947 return build_primary_vtable (binfo, t);
948 else
949 /* This is our very own copy of `basetype' to play with. Later,
950 we will fill in all the virtual functions that override the
951 virtual functions in these base classes which are not defined
952 by the current type. */
953 return build_secondary_vtable (binfo);
956 /* Make *VIRTUALS, an entry on the BINFO_VIRTUALS list for BINFO
957 (which is in the hierarchy dominated by T) list FNDECL as its
958 BV_FN. DELTA is the required constant adjustment from the `this'
959 pointer where the vtable entry appears to the `this' required when
960 the function is actually called. */
962 static void
963 modify_vtable_entry (tree t,
964 tree binfo,
965 tree fndecl,
966 tree delta,
967 tree *virtuals)
969 tree v;
971 v = *virtuals;
973 if (fndecl != BV_FN (v)
974 || !tree_int_cst_equal (delta, BV_DELTA (v)))
976 /* We need a new vtable for BINFO. */
977 if (make_new_vtable (t, binfo))
979 /* If we really did make a new vtable, we also made a copy
980 of the BINFO_VIRTUALS list. Now, we have to find the
981 corresponding entry in that list. */
982 *virtuals = BINFO_VIRTUALS (binfo);
983 while (BV_FN (*virtuals) != BV_FN (v))
984 *virtuals = TREE_CHAIN (*virtuals);
985 v = *virtuals;
988 BV_DELTA (v) = delta;
989 BV_VCALL_INDEX (v) = NULL_TREE;
990 BV_FN (v) = fndecl;
995 /* Add method METHOD to class TYPE. If VIA_USING indicates whether
996 METHOD is being injected via a using_decl. Returns true if the
997 method could be added to the method vec. */
999 bool
1000 add_method (tree type, tree method, bool via_using)
1002 if (method == error_mark_node)
1003 return false;
1005 gcc_assert (!DECL_EXTERN_C_P (method));
1007 tree *slot = find_member_slot (type, DECL_NAME (method));
1008 tree current_fns = slot ? *slot : NULL_TREE;
1010 /* Check to see if we've already got this method. */
1011 for (ovl_iterator iter (current_fns); iter; ++iter)
1013 tree fn = *iter;
1014 tree fn_type;
1015 tree method_type;
1016 tree parms1;
1017 tree parms2;
1019 if (TREE_CODE (fn) != TREE_CODE (method))
1020 continue;
1022 /* Two using-declarations can coexist, we'll complain about ambiguity in
1023 overload resolution. */
1024 if (via_using && iter.using_p ()
1025 /* Except handle inherited constructors specially. */
1026 && ! DECL_CONSTRUCTOR_P (fn))
1027 continue;
1029 /* [over.load] Member function declarations with the
1030 same name and the same parameter types cannot be
1031 overloaded if any of them is a static member
1032 function declaration.
1034 [over.load] Member function declarations with the same name and
1035 the same parameter-type-list as well as member function template
1036 declarations with the same name, the same parameter-type-list, and
1037 the same template parameter lists cannot be overloaded if any of
1038 them, but not all, have a ref-qualifier.
1040 [namespace.udecl] When a using-declaration brings names
1041 from a base class into a derived class scope, member
1042 functions in the derived class override and/or hide member
1043 functions with the same name and parameter types in a base
1044 class (rather than conflicting). */
1045 fn_type = TREE_TYPE (fn);
1046 method_type = TREE_TYPE (method);
1047 parms1 = TYPE_ARG_TYPES (fn_type);
1048 parms2 = TYPE_ARG_TYPES (method_type);
1050 /* Compare the quals on the 'this' parm. Don't compare
1051 the whole types, as used functions are treated as
1052 coming from the using class in overload resolution. */
1053 if (! DECL_STATIC_FUNCTION_P (fn)
1054 && ! DECL_STATIC_FUNCTION_P (method)
1055 /* Either both or neither need to be ref-qualified for
1056 differing quals to allow overloading. */
1057 && (FUNCTION_REF_QUALIFIED (fn_type)
1058 == FUNCTION_REF_QUALIFIED (method_type))
1059 && (type_memfn_quals (fn_type) != type_memfn_quals (method_type)
1060 || type_memfn_rqual (fn_type) != type_memfn_rqual (method_type)))
1061 continue;
1063 /* For templates, the return type and template parameters
1064 must be identical. */
1065 if (TREE_CODE (fn) == TEMPLATE_DECL
1066 && (!same_type_p (TREE_TYPE (fn_type),
1067 TREE_TYPE (method_type))
1068 || !comp_template_parms (DECL_TEMPLATE_PARMS (fn),
1069 DECL_TEMPLATE_PARMS (method))))
1070 continue;
1072 if (! DECL_STATIC_FUNCTION_P (fn))
1073 parms1 = TREE_CHAIN (parms1);
1074 if (! DECL_STATIC_FUNCTION_P (method))
1075 parms2 = TREE_CHAIN (parms2);
1077 /* Bring back parameters omitted from an inherited ctor. */
1078 if (ctor_omit_inherited_parms (fn))
1079 parms1 = FUNCTION_FIRST_USER_PARMTYPE (DECL_ORIGIN (fn));
1080 if (ctor_omit_inherited_parms (method))
1081 parms2 = FUNCTION_FIRST_USER_PARMTYPE (DECL_ORIGIN (method));
1083 if (compparms (parms1, parms2)
1084 && (!DECL_CONV_FN_P (fn)
1085 || same_type_p (TREE_TYPE (fn_type),
1086 TREE_TYPE (method_type)))
1087 && equivalently_constrained (fn, method))
1089 /* If these are versions of the same function, process and
1090 move on. */
1091 if (TREE_CODE (fn) == FUNCTION_DECL
1092 && maybe_version_functions (method, fn, true))
1093 continue;
1095 if (DECL_INHERITED_CTOR (method))
1097 if (DECL_INHERITED_CTOR (fn))
1099 tree basem = DECL_INHERITED_CTOR_BASE (method);
1100 tree basef = DECL_INHERITED_CTOR_BASE (fn);
1101 if (flag_new_inheriting_ctors)
1103 if (basem == basef)
1105 /* Inheriting the same constructor along different
1106 paths, combine them. */
1107 SET_DECL_INHERITED_CTOR
1108 (fn, ovl_make (DECL_INHERITED_CTOR (method),
1109 DECL_INHERITED_CTOR (fn)));
1110 /* And discard the new one. */
1111 return false;
1113 else
1114 /* Inherited ctors can coexist until overload
1115 resolution. */
1116 continue;
1118 error_at (DECL_SOURCE_LOCATION (method),
1119 "%q#D conflicts with version inherited from %qT",
1120 method, basef);
1121 inform (DECL_SOURCE_LOCATION (fn),
1122 "version inherited from %qT declared here",
1123 basef);
1125 /* Otherwise defer to the other function. */
1126 return false;
1129 if (via_using)
1130 /* Defer to the local function. */
1131 return false;
1132 else if (flag_new_inheriting_ctors
1133 && DECL_INHERITED_CTOR (fn))
1135 /* Remove the inherited constructor. */
1136 current_fns = iter.remove_node (current_fns);
1137 continue;
1139 else
1141 error_at (DECL_SOURCE_LOCATION (method),
1142 "%q#D cannot be overloaded with %q#D", method, fn);
1143 inform (DECL_SOURCE_LOCATION (fn),
1144 "previous declaration %q#D", fn);
1145 return false;
1150 /* A class should never have more than one destructor. */
1151 gcc_assert (!current_fns || !DECL_DESTRUCTOR_P (method));
1153 current_fns = ovl_insert (method, current_fns, via_using);
1155 if (!COMPLETE_TYPE_P (type) && !DECL_CONV_FN_P (method)
1156 && !push_class_level_binding (DECL_NAME (method), current_fns))
1157 return false;
1159 if (!slot)
1160 slot = add_member_slot (type, DECL_NAME (method));
1162 /* Maintain TYPE_HAS_USER_CONSTRUCTOR, etc. */
1163 grok_special_member_properties (method);
1165 *slot = current_fns;
1167 return true;
1170 /* Subroutines of finish_struct. */
1172 /* Change the access of FDECL to ACCESS in T. Return 1 if change was
1173 legit, otherwise return 0. */
1175 static int
1176 alter_access (tree t, tree fdecl, tree access)
1178 tree elem;
1180 retrofit_lang_decl (fdecl);
1182 gcc_assert (!DECL_DISCRIMINATOR_P (fdecl));
1184 elem = purpose_member (t, DECL_ACCESS (fdecl));
1185 if (elem)
1187 if (TREE_VALUE (elem) != access)
1189 if (TREE_CODE (TREE_TYPE (fdecl)) == FUNCTION_DECL)
1190 error ("conflicting access specifications for method"
1191 " %q+D, ignored", TREE_TYPE (fdecl));
1192 else
1193 error ("conflicting access specifications for field %qE, ignored",
1194 DECL_NAME (fdecl));
1196 else
1198 /* They're changing the access to the same thing they changed
1199 it to before. That's OK. */
1203 else
1205 perform_or_defer_access_check (TYPE_BINFO (t), fdecl, fdecl,
1206 tf_warning_or_error);
1207 DECL_ACCESS (fdecl) = tree_cons (t, access, DECL_ACCESS (fdecl));
1208 return 1;
1210 return 0;
1213 /* Return the access node for DECL's access in its enclosing class. */
1215 tree
1216 declared_access (tree decl)
1218 return (TREE_PRIVATE (decl) ? access_private_node
1219 : TREE_PROTECTED (decl) ? access_protected_node
1220 : access_public_node);
1223 /* Process the USING_DECL, which is a member of T. */
1225 static void
1226 handle_using_decl (tree using_decl, tree t)
1228 tree decl = USING_DECL_DECLS (using_decl);
1229 tree name = DECL_NAME (using_decl);
1230 tree access = declared_access (using_decl);
1231 tree flist = NULL_TREE;
1232 tree old_value;
1234 gcc_assert (!processing_template_decl && decl);
1236 old_value = lookup_member (t, name, /*protect=*/0, /*want_type=*/false,
1237 tf_warning_or_error);
1238 if (old_value)
1240 old_value = OVL_FIRST (old_value);
1242 if (DECL_P (old_value) && DECL_CONTEXT (old_value) == t)
1243 /* OK */;
1244 else
1245 old_value = NULL_TREE;
1248 cp_emit_debug_info_for_using (decl, t);
1250 if (is_overloaded_fn (decl))
1251 flist = decl;
1253 if (! old_value)
1255 else if (is_overloaded_fn (old_value))
1257 if (flist)
1258 /* It's OK to use functions from a base when there are functions with
1259 the same name already present in the current class. */;
1260 else
1262 error_at (DECL_SOURCE_LOCATION (using_decl), "%qD invalid in %q#T "
1263 "because of local method %q#D with same name",
1264 using_decl, t, old_value);
1265 inform (DECL_SOURCE_LOCATION (old_value),
1266 "local method %q#D declared here", old_value);
1267 return;
1270 else if (!DECL_ARTIFICIAL (old_value))
1272 error_at (DECL_SOURCE_LOCATION (using_decl), "%qD invalid in %q#T "
1273 "because of local member %q#D with same name",
1274 using_decl, t, old_value);
1275 inform (DECL_SOURCE_LOCATION (old_value),
1276 "local member %q#D declared here", old_value);
1277 return;
1280 /* Make type T see field decl FDECL with access ACCESS. */
1281 if (flist)
1282 for (ovl_iterator iter (flist); iter; ++iter)
1284 add_method (t, *iter, true);
1285 alter_access (t, *iter, access);
1287 else
1288 alter_access (t, decl, access);
1291 /* Data structure for find_abi_tags_r, below. */
1293 struct abi_tag_data
1295 tree t; // The type that we're checking for missing tags.
1296 tree subob; // The subobject of T that we're getting tags from.
1297 tree tags; // error_mark_node for diagnostics, or a list of missing tags.
1300 /* Subroutine of find_abi_tags_r. Handle a single TAG found on the class TP
1301 in the context of P. TAG can be either an identifier (the DECL_NAME of
1302 a tag NAMESPACE_DECL) or a STRING_CST (a tag attribute). */
1304 static void
1305 check_tag (tree tag, tree id, tree *tp, abi_tag_data *p)
1307 if (!IDENTIFIER_MARKED (id))
1309 if (p->tags != error_mark_node)
1311 /* We're collecting tags from template arguments or from
1312 the type of a variable or function return type. */
1313 p->tags = tree_cons (NULL_TREE, tag, p->tags);
1315 /* Don't inherit this tag multiple times. */
1316 IDENTIFIER_MARKED (id) = true;
1318 if (TYPE_P (p->t))
1320 /* Tags inherited from type template arguments are only used
1321 to avoid warnings. */
1322 ABI_TAG_IMPLICIT (p->tags) = true;
1323 return;
1325 /* For functions and variables we want to warn, too. */
1328 /* Otherwise we're diagnosing missing tags. */
1329 if (TREE_CODE (p->t) == FUNCTION_DECL)
1331 if (warning (OPT_Wabi_tag, "%qD inherits the %E ABI tag "
1332 "that %qT (used in its return type) has",
1333 p->t, tag, *tp))
1334 inform (location_of (*tp), "%qT declared here", *tp);
1336 else if (VAR_P (p->t))
1338 if (warning (OPT_Wabi_tag, "%qD inherits the %E ABI tag "
1339 "that %qT (used in its type) has", p->t, tag, *tp))
1340 inform (location_of (*tp), "%qT declared here", *tp);
1342 else if (TYPE_P (p->subob))
1344 if (warning (OPT_Wabi_tag, "%qT does not have the %E ABI tag "
1345 "that base %qT has", p->t, tag, p->subob))
1346 inform (location_of (p->subob), "%qT declared here",
1347 p->subob);
1349 else
1351 if (warning (OPT_Wabi_tag, "%qT does not have the %E ABI tag "
1352 "that %qT (used in the type of %qD) has",
1353 p->t, tag, *tp, p->subob))
1355 inform (location_of (p->subob), "%qD declared here",
1356 p->subob);
1357 inform (location_of (*tp), "%qT declared here", *tp);
1363 /* Find all the ABI tags in the attribute list ATTR and either call
1364 check_tag (if TP is non-null) or set IDENTIFIER_MARKED to val. */
1366 static void
1367 mark_or_check_attr_tags (tree attr, tree *tp, abi_tag_data *p, bool val)
1369 if (!attr)
1370 return;
1371 for (; (attr = lookup_attribute ("abi_tag", attr));
1372 attr = TREE_CHAIN (attr))
1373 for (tree list = TREE_VALUE (attr); list;
1374 list = TREE_CHAIN (list))
1376 tree tag = TREE_VALUE (list);
1377 tree id = get_identifier (TREE_STRING_POINTER (tag));
1378 if (tp)
1379 check_tag (tag, id, tp, p);
1380 else
1381 IDENTIFIER_MARKED (id) = val;
1385 /* Find all the ABI tags on T and its enclosing scopes and either call
1386 check_tag (if TP is non-null) or set IDENTIFIER_MARKED to val. */
1388 static void
1389 mark_or_check_tags (tree t, tree *tp, abi_tag_data *p, bool val)
1391 while (t != global_namespace)
1393 tree attr;
1394 if (TYPE_P (t))
1396 attr = TYPE_ATTRIBUTES (t);
1397 t = CP_TYPE_CONTEXT (t);
1399 else
1401 attr = DECL_ATTRIBUTES (t);
1402 t = CP_DECL_CONTEXT (t);
1404 mark_or_check_attr_tags (attr, tp, p, val);
1408 /* walk_tree callback for check_abi_tags: if the type at *TP involves any
1409 types with ABI tags, add the corresponding identifiers to the VEC in
1410 *DATA and set IDENTIFIER_MARKED. */
1412 static tree
1413 find_abi_tags_r (tree *tp, int *walk_subtrees, void *data)
1415 if (!OVERLOAD_TYPE_P (*tp))
1416 return NULL_TREE;
1418 /* walk_tree shouldn't be walking into any subtrees of a RECORD_TYPE
1419 anyway, but let's make sure of it. */
1420 *walk_subtrees = false;
1422 abi_tag_data *p = static_cast<struct abi_tag_data*>(data);
1424 mark_or_check_tags (*tp, tp, p, false);
1426 return NULL_TREE;
1429 /* walk_tree callback for mark_abi_tags: if *TP is a class, set
1430 IDENTIFIER_MARKED on its ABI tags. */
1432 static tree
1433 mark_abi_tags_r (tree *tp, int *walk_subtrees, void *data)
1435 if (!OVERLOAD_TYPE_P (*tp))
1436 return NULL_TREE;
1438 /* walk_tree shouldn't be walking into any subtrees of a RECORD_TYPE
1439 anyway, but let's make sure of it. */
1440 *walk_subtrees = false;
1442 bool *valp = static_cast<bool*>(data);
1444 mark_or_check_tags (*tp, NULL, NULL, *valp);
1446 return NULL_TREE;
1449 /* Set IDENTIFIER_MARKED on all the ABI tags on T and its enclosing
1450 scopes. */
1452 static void
1453 mark_abi_tags (tree t, bool val)
1455 mark_or_check_tags (t, NULL, NULL, val);
1456 if (DECL_P (t))
1458 if (DECL_LANG_SPECIFIC (t) && DECL_USE_TEMPLATE (t)
1459 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (t)))
1461 /* Template arguments are part of the signature. */
1462 tree level = INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (t));
1463 for (int j = 0; j < TREE_VEC_LENGTH (level); ++j)
1465 tree arg = TREE_VEC_ELT (level, j);
1466 cp_walk_tree_without_duplicates (&arg, mark_abi_tags_r, &val);
1469 if (TREE_CODE (t) == FUNCTION_DECL)
1470 /* A function's parameter types are part of the signature, so
1471 we don't need to inherit any tags that are also in them. */
1472 for (tree arg = FUNCTION_FIRST_USER_PARMTYPE (t); arg;
1473 arg = TREE_CHAIN (arg))
1474 cp_walk_tree_without_duplicates (&TREE_VALUE (arg),
1475 mark_abi_tags_r, &val);
1479 /* Check that T has all the ABI tags that subobject SUBOB has, or
1480 warn if not. If T is a (variable or function) declaration, also
1481 return any missing tags, and add them to T if JUST_CHECKING is false. */
1483 static tree
1484 check_abi_tags (tree t, tree subob, bool just_checking = false)
1486 bool inherit = DECL_P (t);
1488 if (!inherit && !warn_abi_tag)
1489 return NULL_TREE;
1491 tree decl = TYPE_P (t) ? TYPE_NAME (t) : t;
1492 if (!TREE_PUBLIC (decl))
1493 /* No need to worry about things local to this TU. */
1494 return NULL_TREE;
1496 mark_abi_tags (t, true);
1498 tree subtype = TYPE_P (subob) ? subob : TREE_TYPE (subob);
1499 struct abi_tag_data data = { t, subob, error_mark_node };
1500 if (inherit)
1501 data.tags = NULL_TREE;
1503 cp_walk_tree_without_duplicates (&subtype, find_abi_tags_r, &data);
1505 if (!(inherit && data.tags))
1506 /* We don't need to do anything with data.tags. */;
1507 else if (just_checking)
1508 for (tree t = data.tags; t; t = TREE_CHAIN (t))
1510 tree id = get_identifier (TREE_STRING_POINTER (TREE_VALUE (t)));
1511 IDENTIFIER_MARKED (id) = false;
1513 else
1515 tree attr = lookup_attribute ("abi_tag", DECL_ATTRIBUTES (t));
1516 if (attr)
1517 TREE_VALUE (attr) = chainon (data.tags, TREE_VALUE (attr));
1518 else
1519 DECL_ATTRIBUTES (t)
1520 = tree_cons (get_identifier ("abi_tag"), data.tags,
1521 DECL_ATTRIBUTES (t));
1524 mark_abi_tags (t, false);
1526 return data.tags;
1529 /* Check that DECL has all the ABI tags that are used in parts of its type
1530 that are not reflected in its mangled name. */
1532 void
1533 check_abi_tags (tree decl)
1535 if (VAR_P (decl))
1536 check_abi_tags (decl, TREE_TYPE (decl));
1537 else if (TREE_CODE (decl) == FUNCTION_DECL
1538 && !DECL_CONV_FN_P (decl)
1539 && !mangle_return_type_p (decl))
1540 check_abi_tags (decl, TREE_TYPE (TREE_TYPE (decl)));
1543 /* Return any ABI tags that are used in parts of the type of DECL
1544 that are not reflected in its mangled name. This function is only
1545 used in backward-compatible mangling for ABI <11. */
1547 tree
1548 missing_abi_tags (tree decl)
1550 if (VAR_P (decl))
1551 return check_abi_tags (decl, TREE_TYPE (decl), true);
1552 else if (TREE_CODE (decl) == FUNCTION_DECL
1553 /* Don't check DECL_CONV_FN_P here like we do in check_abi_tags, so
1554 that we can use this function for setting need_abi_warning
1555 regardless of the current flag_abi_version. */
1556 && !mangle_return_type_p (decl))
1557 return check_abi_tags (decl, TREE_TYPE (TREE_TYPE (decl)), true);
1558 else
1559 return NULL_TREE;
1562 void
1563 inherit_targ_abi_tags (tree t)
1565 if (!CLASS_TYPE_P (t)
1566 || CLASSTYPE_TEMPLATE_INFO (t) == NULL_TREE)
1567 return;
1569 mark_abi_tags (t, true);
1571 tree args = CLASSTYPE_TI_ARGS (t);
1572 struct abi_tag_data data = { t, NULL_TREE, NULL_TREE };
1573 for (int i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
1575 tree level = TMPL_ARGS_LEVEL (args, i+1);
1576 for (int j = 0; j < TREE_VEC_LENGTH (level); ++j)
1578 tree arg = TREE_VEC_ELT (level, j);
1579 data.subob = arg;
1580 cp_walk_tree_without_duplicates (&arg, find_abi_tags_r, &data);
1584 // If we found some tags on our template arguments, add them to our
1585 // abi_tag attribute.
1586 if (data.tags)
1588 tree attr = lookup_attribute ("abi_tag", TYPE_ATTRIBUTES (t));
1589 if (attr)
1590 TREE_VALUE (attr) = chainon (data.tags, TREE_VALUE (attr));
1591 else
1592 TYPE_ATTRIBUTES (t)
1593 = tree_cons (get_identifier ("abi_tag"), data.tags,
1594 TYPE_ATTRIBUTES (t));
1597 mark_abi_tags (t, false);
1600 /* Return true, iff class T has a non-virtual destructor that is
1601 accessible from outside the class heirarchy (i.e. is public, or
1602 there's a suitable friend. */
1604 static bool
1605 accessible_nvdtor_p (tree t)
1607 tree dtor = CLASSTYPE_DESTRUCTOR (t);
1609 /* An implicitly declared destructor is always public. And,
1610 if it were virtual, we would have created it by now. */
1611 if (!dtor)
1612 return true;
1614 if (DECL_VINDEX (dtor))
1615 return false; /* Virtual */
1617 if (!TREE_PRIVATE (dtor) && !TREE_PROTECTED (dtor))
1618 return true; /* Public */
1620 if (CLASSTYPE_FRIEND_CLASSES (t)
1621 || DECL_FRIENDLIST (TYPE_MAIN_DECL (t)))
1622 return true; /* Has friends */
1624 return false;
1627 /* Run through the base classes of T, updating CANT_HAVE_CONST_CTOR_P,
1628 and NO_CONST_ASN_REF_P. Also set flag bits in T based on
1629 properties of the bases. */
1631 static void
1632 check_bases (tree t,
1633 int* cant_have_const_ctor_p,
1634 int* no_const_asn_ref_p)
1636 int i;
1637 bool seen_non_virtual_nearly_empty_base_p = 0;
1638 int seen_tm_mask = 0;
1639 tree base_binfo;
1640 tree binfo;
1641 tree field = NULL_TREE;
1643 if (!CLASSTYPE_NON_STD_LAYOUT (t))
1644 for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
1645 if (TREE_CODE (field) == FIELD_DECL)
1646 break;
1648 for (binfo = TYPE_BINFO (t), i = 0;
1649 BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
1651 tree basetype = TREE_TYPE (base_binfo);
1653 gcc_assert (COMPLETE_TYPE_P (basetype));
1655 if (CLASSTYPE_FINAL (basetype))
1656 error ("cannot derive from %<final%> base %qT in derived type %qT",
1657 basetype, t);
1659 /* If any base class is non-literal, so is the derived class. */
1660 if (!CLASSTYPE_LITERAL_P (basetype))
1661 CLASSTYPE_LITERAL_P (t) = false;
1663 /* If the base class doesn't have copy constructors or
1664 assignment operators that take const references, then the
1665 derived class cannot have such a member automatically
1666 generated. */
1667 if (TYPE_HAS_COPY_CTOR (basetype)
1668 && ! TYPE_HAS_CONST_COPY_CTOR (basetype))
1669 *cant_have_const_ctor_p = 1;
1670 if (TYPE_HAS_COPY_ASSIGN (basetype)
1671 && !TYPE_HAS_CONST_COPY_ASSIGN (basetype))
1672 *no_const_asn_ref_p = 1;
1674 if (BINFO_VIRTUAL_P (base_binfo))
1675 /* A virtual base does not effect nearly emptiness. */
1677 else if (CLASSTYPE_NEARLY_EMPTY_P (basetype))
1679 if (seen_non_virtual_nearly_empty_base_p)
1680 /* And if there is more than one nearly empty base, then the
1681 derived class is not nearly empty either. */
1682 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
1683 else
1684 /* Remember we've seen one. */
1685 seen_non_virtual_nearly_empty_base_p = 1;
1687 else if (!is_empty_class (basetype))
1688 /* If the base class is not empty or nearly empty, then this
1689 class cannot be nearly empty. */
1690 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
1692 /* A lot of properties from the bases also apply to the derived
1693 class. */
1694 TYPE_NEEDS_CONSTRUCTING (t) |= TYPE_NEEDS_CONSTRUCTING (basetype);
1695 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t)
1696 |= TYPE_HAS_NONTRIVIAL_DESTRUCTOR (basetype);
1697 TYPE_HAS_COMPLEX_COPY_ASSIGN (t)
1698 |= (TYPE_HAS_COMPLEX_COPY_ASSIGN (basetype)
1699 || !TYPE_HAS_COPY_ASSIGN (basetype));
1700 TYPE_HAS_COMPLEX_COPY_CTOR (t) |= (TYPE_HAS_COMPLEX_COPY_CTOR (basetype)
1701 || !TYPE_HAS_COPY_CTOR (basetype));
1702 TYPE_HAS_COMPLEX_MOVE_ASSIGN (t)
1703 |= TYPE_HAS_COMPLEX_MOVE_ASSIGN (basetype);
1704 TYPE_HAS_COMPLEX_MOVE_CTOR (t) |= TYPE_HAS_COMPLEX_MOVE_CTOR (basetype);
1705 TYPE_POLYMORPHIC_P (t) |= TYPE_POLYMORPHIC_P (basetype);
1706 CLASSTYPE_CONTAINS_EMPTY_CLASS_P (t)
1707 |= CLASSTYPE_CONTAINS_EMPTY_CLASS_P (basetype);
1708 TYPE_HAS_COMPLEX_DFLT (t) |= (!TYPE_HAS_DEFAULT_CONSTRUCTOR (basetype)
1709 || TYPE_HAS_COMPLEX_DFLT (basetype));
1710 SET_CLASSTYPE_READONLY_FIELDS_NEED_INIT
1711 (t, CLASSTYPE_READONLY_FIELDS_NEED_INIT (t)
1712 | CLASSTYPE_READONLY_FIELDS_NEED_INIT (basetype));
1713 SET_CLASSTYPE_REF_FIELDS_NEED_INIT
1714 (t, CLASSTYPE_REF_FIELDS_NEED_INIT (t)
1715 | CLASSTYPE_REF_FIELDS_NEED_INIT (basetype));
1716 if (TYPE_HAS_MUTABLE_P (basetype))
1717 CLASSTYPE_HAS_MUTABLE (t) = 1;
1719 /* A standard-layout class is a class that:
1721 * has no non-standard-layout base classes, */
1722 CLASSTYPE_NON_STD_LAYOUT (t) |= CLASSTYPE_NON_STD_LAYOUT (basetype);
1723 if (!CLASSTYPE_NON_STD_LAYOUT (t))
1725 tree basefield;
1726 /* ...has no base classes of the same type as the first non-static
1727 data member... */
1728 if (field && DECL_CONTEXT (field) == t
1729 && (same_type_ignoring_top_level_qualifiers_p
1730 (TREE_TYPE (field), basetype)))
1731 CLASSTYPE_NON_STD_LAYOUT (t) = 1;
1732 else
1733 /* ...either has no non-static data members in the most-derived
1734 class and at most one base class with non-static data
1735 members, or has no base classes with non-static data
1736 members */
1737 for (basefield = TYPE_FIELDS (basetype); basefield;
1738 basefield = DECL_CHAIN (basefield))
1739 if (TREE_CODE (basefield) == FIELD_DECL
1740 && !(DECL_FIELD_IS_BASE (basefield)
1741 && integer_zerop (DECL_SIZE (basefield))))
1743 if (field)
1744 CLASSTYPE_NON_STD_LAYOUT (t) = 1;
1745 else
1746 field = basefield;
1747 break;
1751 /* Don't bother collecting tm attributes if transactional memory
1752 support is not enabled. */
1753 if (flag_tm)
1755 tree tm_attr = find_tm_attribute (TYPE_ATTRIBUTES (basetype));
1756 if (tm_attr)
1757 seen_tm_mask |= tm_attr_to_mask (tm_attr);
1760 check_abi_tags (t, basetype);
1763 /* If one of the base classes had TM attributes, and the current class
1764 doesn't define its own, then the current class inherits one. */
1765 if (seen_tm_mask && !find_tm_attribute (TYPE_ATTRIBUTES (t)))
1767 tree tm_attr = tm_mask_to_attr (least_bit_hwi (seen_tm_mask));
1768 TYPE_ATTRIBUTES (t) = tree_cons (tm_attr, NULL, TYPE_ATTRIBUTES (t));
1772 /* Determine all the primary bases within T. Sets BINFO_PRIMARY_BASE_P for
1773 those that are primaries. Sets BINFO_LOST_PRIMARY_P for those
1774 that have had a nearly-empty virtual primary base stolen by some
1775 other base in the hierarchy. Determines CLASSTYPE_PRIMARY_BASE for
1776 T. */
1778 static void
1779 determine_primary_bases (tree t)
1781 unsigned i;
1782 tree primary = NULL_TREE;
1783 tree type_binfo = TYPE_BINFO (t);
1784 tree base_binfo;
1786 /* Determine the primary bases of our bases. */
1787 for (base_binfo = TREE_CHAIN (type_binfo); base_binfo;
1788 base_binfo = TREE_CHAIN (base_binfo))
1790 tree primary = CLASSTYPE_PRIMARY_BINFO (BINFO_TYPE (base_binfo));
1792 /* See if we're the non-virtual primary of our inheritance
1793 chain. */
1794 if (!BINFO_VIRTUAL_P (base_binfo))
1796 tree parent = BINFO_INHERITANCE_CHAIN (base_binfo);
1797 tree parent_primary = CLASSTYPE_PRIMARY_BINFO (BINFO_TYPE (parent));
1799 if (parent_primary
1800 && SAME_BINFO_TYPE_P (BINFO_TYPE (base_binfo),
1801 BINFO_TYPE (parent_primary)))
1802 /* We are the primary binfo. */
1803 BINFO_PRIMARY_P (base_binfo) = 1;
1805 /* Determine if we have a virtual primary base, and mark it so.
1807 if (primary && BINFO_VIRTUAL_P (primary))
1809 tree this_primary = copied_binfo (primary, base_binfo);
1811 if (BINFO_PRIMARY_P (this_primary))
1812 /* Someone already claimed this base. */
1813 BINFO_LOST_PRIMARY_P (base_binfo) = 1;
1814 else
1816 tree delta;
1818 BINFO_PRIMARY_P (this_primary) = 1;
1819 BINFO_INHERITANCE_CHAIN (this_primary) = base_binfo;
1821 /* A virtual binfo might have been copied from within
1822 another hierarchy. As we're about to use it as a
1823 primary base, make sure the offsets match. */
1824 delta = size_diffop_loc (input_location,
1825 fold_convert (ssizetype,
1826 BINFO_OFFSET (base_binfo)),
1827 fold_convert (ssizetype,
1828 BINFO_OFFSET (this_primary)));
1830 propagate_binfo_offsets (this_primary, delta);
1835 /* First look for a dynamic direct non-virtual base. */
1836 for (i = 0; BINFO_BASE_ITERATE (type_binfo, i, base_binfo); i++)
1838 tree basetype = BINFO_TYPE (base_binfo);
1840 if (TYPE_CONTAINS_VPTR_P (basetype) && !BINFO_VIRTUAL_P (base_binfo))
1842 primary = base_binfo;
1843 goto found;
1847 /* A "nearly-empty" virtual base class can be the primary base
1848 class, if no non-virtual polymorphic base can be found. Look for
1849 a nearly-empty virtual dynamic base that is not already a primary
1850 base of something in the hierarchy. If there is no such base,
1851 just pick the first nearly-empty virtual base. */
1853 for (base_binfo = TREE_CHAIN (type_binfo); base_binfo;
1854 base_binfo = TREE_CHAIN (base_binfo))
1855 if (BINFO_VIRTUAL_P (base_binfo)
1856 && CLASSTYPE_NEARLY_EMPTY_P (BINFO_TYPE (base_binfo)))
1858 if (!BINFO_PRIMARY_P (base_binfo))
1860 /* Found one that is not primary. */
1861 primary = base_binfo;
1862 goto found;
1864 else if (!primary)
1865 /* Remember the first candidate. */
1866 primary = base_binfo;
1869 found:
1870 /* If we've got a primary base, use it. */
1871 if (primary)
1873 tree basetype = BINFO_TYPE (primary);
1875 CLASSTYPE_PRIMARY_BINFO (t) = primary;
1876 if (BINFO_PRIMARY_P (primary))
1877 /* We are stealing a primary base. */
1878 BINFO_LOST_PRIMARY_P (BINFO_INHERITANCE_CHAIN (primary)) = 1;
1879 BINFO_PRIMARY_P (primary) = 1;
1880 if (BINFO_VIRTUAL_P (primary))
1882 tree delta;
1884 BINFO_INHERITANCE_CHAIN (primary) = type_binfo;
1885 /* A virtual binfo might have been copied from within
1886 another hierarchy. As we're about to use it as a primary
1887 base, make sure the offsets match. */
1888 delta = size_diffop_loc (input_location, ssize_int (0),
1889 fold_convert (ssizetype, BINFO_OFFSET (primary)));
1891 propagate_binfo_offsets (primary, delta);
1894 primary = TYPE_BINFO (basetype);
1896 TYPE_VFIELD (t) = TYPE_VFIELD (basetype);
1897 BINFO_VTABLE (type_binfo) = BINFO_VTABLE (primary);
1898 BINFO_VIRTUALS (type_binfo) = BINFO_VIRTUALS (primary);
1902 /* Update the variant types of T. */
1904 void
1905 fixup_type_variants (tree t)
1907 tree variants;
1909 if (!t)
1910 return;
1912 for (variants = TYPE_NEXT_VARIANT (t);
1913 variants;
1914 variants = TYPE_NEXT_VARIANT (variants))
1916 /* These fields are in the _TYPE part of the node, not in
1917 the TYPE_LANG_SPECIFIC component, so they are not shared. */
1918 TYPE_HAS_USER_CONSTRUCTOR (variants) = TYPE_HAS_USER_CONSTRUCTOR (t);
1919 TYPE_NEEDS_CONSTRUCTING (variants) = TYPE_NEEDS_CONSTRUCTING (t);
1920 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (variants)
1921 = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t);
1923 TYPE_POLYMORPHIC_P (variants) = TYPE_POLYMORPHIC_P (t);
1925 TYPE_BINFO (variants) = TYPE_BINFO (t);
1927 /* Copy whatever these are holding today. */
1928 TYPE_VFIELD (variants) = TYPE_VFIELD (t);
1929 TYPE_FIELDS (variants) = TYPE_FIELDS (t);
1933 /* KLASS is a class that we're applying may_alias to after the body is
1934 parsed. Fixup any POINTER_TO and REFERENCE_TO types. The
1935 canonical type(s) will be implicitly updated. */
1937 static void
1938 fixup_may_alias (tree klass)
1940 tree t, v;
1942 for (t = TYPE_POINTER_TO (klass); t; t = TYPE_NEXT_PTR_TO (t))
1943 for (v = TYPE_MAIN_VARIANT (t); v; v = TYPE_NEXT_VARIANT (v))
1944 TYPE_REF_CAN_ALIAS_ALL (v) = true;
1945 for (t = TYPE_REFERENCE_TO (klass); t; t = TYPE_NEXT_REF_TO (t))
1946 for (v = TYPE_MAIN_VARIANT (t); v; v = TYPE_NEXT_VARIANT (v))
1947 TYPE_REF_CAN_ALIAS_ALL (v) = true;
1950 /* Early variant fixups: we apply attributes at the beginning of the class
1951 definition, and we need to fix up any variants that have already been
1952 made via elaborated-type-specifier so that check_qualified_type works. */
1954 void
1955 fixup_attribute_variants (tree t)
1957 tree variants;
1959 if (!t)
1960 return;
1962 tree attrs = TYPE_ATTRIBUTES (t);
1963 unsigned align = TYPE_ALIGN (t);
1964 bool user_align = TYPE_USER_ALIGN (t);
1965 bool may_alias = lookup_attribute ("may_alias", attrs);
1967 if (may_alias)
1968 fixup_may_alias (t);
1970 for (variants = TYPE_NEXT_VARIANT (t);
1971 variants;
1972 variants = TYPE_NEXT_VARIANT (variants))
1974 /* These are the two fields that check_qualified_type looks at and
1975 are affected by attributes. */
1976 TYPE_ATTRIBUTES (variants) = attrs;
1977 unsigned valign = align;
1978 if (TYPE_USER_ALIGN (variants))
1979 valign = MAX (valign, TYPE_ALIGN (variants));
1980 else
1981 TYPE_USER_ALIGN (variants) = user_align;
1982 SET_TYPE_ALIGN (variants, valign);
1983 if (may_alias)
1984 fixup_may_alias (variants);
1988 /* Set memoizing fields and bits of T (and its variants) for later
1989 use. */
1991 static void
1992 finish_struct_bits (tree t)
1994 /* Fix up variants (if any). */
1995 fixup_type_variants (t);
1997 if (BINFO_N_BASE_BINFOS (TYPE_BINFO (t)) && TYPE_POLYMORPHIC_P (t))
1998 /* For a class w/o baseclasses, 'finish_struct' has set
1999 CLASSTYPE_PURE_VIRTUALS correctly (by definition).
2000 Similarly for a class whose base classes do not have vtables.
2001 When neither of these is true, we might have removed abstract
2002 virtuals (by providing a definition), added some (by declaring
2003 new ones), or redeclared ones from a base class. We need to
2004 recalculate what's really an abstract virtual at this point (by
2005 looking in the vtables). */
2006 get_pure_virtuals (t);
2008 /* If this type has a copy constructor or a destructor, force its
2009 mode to be BLKmode, and force its TREE_ADDRESSABLE bit to be
2010 nonzero. This will cause it to be passed by invisible reference
2011 and prevent it from being returned in a register. */
2012 if (type_has_nontrivial_copy_init (t)
2013 || TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t))
2015 tree variants;
2016 SET_DECL_MODE (TYPE_MAIN_DECL (t), BLKmode);
2017 for (variants = t; variants; variants = TYPE_NEXT_VARIANT (variants))
2019 SET_TYPE_MODE (variants, BLKmode);
2020 TREE_ADDRESSABLE (variants) = 1;
2025 /* Issue warnings about T having private constructors, but no friends,
2026 and so forth.
2028 HAS_NONPRIVATE_METHOD is nonzero if T has any non-private methods or
2029 static members. HAS_NONPRIVATE_STATIC_FN is nonzero if T has any
2030 non-private static member functions. */
2032 static void
2033 maybe_warn_about_overly_private_class (tree t)
2035 int has_member_fn = 0;
2036 int has_nonprivate_method = 0;
2038 if (!warn_ctor_dtor_privacy
2039 /* If the class has friends, those entities might create and
2040 access instances, so we should not warn. */
2041 || (CLASSTYPE_FRIEND_CLASSES (t)
2042 || DECL_FRIENDLIST (TYPE_MAIN_DECL (t)))
2043 /* We will have warned when the template was declared; there's
2044 no need to warn on every instantiation. */
2045 || CLASSTYPE_TEMPLATE_INSTANTIATION (t))
2046 /* There's no reason to even consider warning about this
2047 class. */
2048 return;
2050 /* We only issue one warning, if more than one applies, because
2051 otherwise, on code like:
2053 class A {
2054 // Oops - forgot `public:'
2055 A();
2056 A(const A&);
2057 ~A();
2060 we warn several times about essentially the same problem. */
2062 /* Check to see if all (non-constructor, non-destructor) member
2063 functions are private. (Since there are no friends or
2064 non-private statics, we can't ever call any of the private member
2065 functions.) */
2066 for (tree fn = TYPE_FIELDS (t); fn; fn = DECL_CHAIN (fn))
2067 if (!DECL_DECLARES_FUNCTION_P (fn))
2068 /* Not a function. */;
2069 else if (DECL_ARTIFICIAL (fn))
2070 /* We're not interested in compiler-generated methods; they don't
2071 provide any way to call private members. */;
2072 else if (!TREE_PRIVATE (fn))
2074 if (DECL_STATIC_FUNCTION_P (fn))
2075 /* A non-private static member function is just like a
2076 friend; it can create and invoke private member
2077 functions, and be accessed without a class
2078 instance. */
2079 return;
2081 has_nonprivate_method = 1;
2082 /* Keep searching for a static member function. */
2084 else if (!DECL_CONSTRUCTOR_P (fn) && !DECL_DESTRUCTOR_P (fn))
2085 has_member_fn = 1;
2087 if (!has_nonprivate_method && has_member_fn)
2089 /* There are no non-private methods, and there's at least one
2090 private member function that isn't a constructor or
2091 destructor. (If all the private members are
2092 constructors/destructors we want to use the code below that
2093 issues error messages specifically referring to
2094 constructors/destructors.) */
2095 unsigned i;
2096 tree binfo = TYPE_BINFO (t);
2098 for (i = 0; i != BINFO_N_BASE_BINFOS (binfo); i++)
2099 if (BINFO_BASE_ACCESS (binfo, i) != access_private_node)
2101 has_nonprivate_method = 1;
2102 break;
2104 if (!has_nonprivate_method)
2106 warning (OPT_Wctor_dtor_privacy,
2107 "all member functions in class %qT are private", t);
2108 return;
2112 /* Even if some of the member functions are non-private, the class
2113 won't be useful for much if all the constructors or destructors
2114 are private: such an object can never be created or destroyed. */
2115 if (tree dtor = CLASSTYPE_DESTRUCTOR (t))
2116 if (TREE_PRIVATE (dtor))
2118 warning (OPT_Wctor_dtor_privacy,
2119 "%q#T only defines a private destructor and has no friends",
2121 return;
2124 /* Warn about classes that have private constructors and no friends. */
2125 if (TYPE_HAS_USER_CONSTRUCTOR (t)
2126 /* Implicitly generated constructors are always public. */
2127 && !CLASSTYPE_LAZY_DEFAULT_CTOR (t))
2129 bool nonprivate_ctor = false;
2130 tree copy_or_move = NULL_TREE;
2132 /* If a non-template class does not define a copy
2133 constructor, one is defined for it, enabling it to avoid
2134 this warning. For a template class, this does not
2135 happen, and so we would normally get a warning on:
2137 template <class T> class C { private: C(); };
2139 To avoid this asymmetry, we check TYPE_HAS_COPY_CTOR. All
2140 complete non-template or fully instantiated classes have this
2141 flag set. */
2142 if (!TYPE_HAS_COPY_CTOR (t))
2143 nonprivate_ctor = true;
2144 else
2145 for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (t));
2146 !nonprivate_ctor && iter; ++iter)
2147 if (TREE_PRIVATE (*iter))
2148 continue;
2149 else if (copy_fn_p (*iter) || move_fn_p (*iter))
2150 /* Ideally, we wouldn't count any constructor that takes
2151 an argument of the class type as a parameter, because
2152 such things cannot be used to construct an instance of
2153 the class unless you already have one. */
2154 copy_or_move = *iter;
2155 else
2156 nonprivate_ctor = true;
2158 if (!nonprivate_ctor)
2160 warning (OPT_Wctor_dtor_privacy,
2161 "%q#T only defines private constructors and has no friends",
2163 if (copy_or_move)
2164 inform (DECL_SOURCE_LOCATION (copy_or_move),
2165 "%q#D is public, but requires an existing %q#T object",
2166 copy_or_move, t);
2167 return;
2172 /* Make BINFO's vtable have N entries, including RTTI entries,
2173 vbase and vcall offsets, etc. Set its type and call the back end
2174 to lay it out. */
2176 static void
2177 layout_vtable_decl (tree binfo, int n)
2179 tree atype;
2180 tree vtable;
2182 atype = build_array_of_n_type (vtable_entry_type, n);
2183 layout_type (atype);
2185 /* We may have to grow the vtable. */
2186 vtable = get_vtbl_decl_for_binfo (binfo);
2187 if (!same_type_p (TREE_TYPE (vtable), atype))
2189 TREE_TYPE (vtable) = atype;
2190 DECL_SIZE (vtable) = DECL_SIZE_UNIT (vtable) = NULL_TREE;
2191 layout_decl (vtable, 0);
2195 /* True iff FNDECL and BASE_FNDECL (both non-static member functions)
2196 have the same signature. */
2199 same_signature_p (const_tree fndecl, const_tree base_fndecl)
2201 /* One destructor overrides another if they are the same kind of
2202 destructor. */
2203 if (DECL_DESTRUCTOR_P (base_fndecl) && DECL_DESTRUCTOR_P (fndecl)
2204 && special_function_p (base_fndecl) == special_function_p (fndecl))
2205 return 1;
2206 /* But a non-destructor never overrides a destructor, nor vice
2207 versa, nor do different kinds of destructors override
2208 one-another. For example, a complete object destructor does not
2209 override a deleting destructor. */
2210 if (DECL_DESTRUCTOR_P (base_fndecl) || DECL_DESTRUCTOR_P (fndecl))
2211 return 0;
2213 if (DECL_NAME (fndecl) == DECL_NAME (base_fndecl)
2214 || (DECL_CONV_FN_P (fndecl)
2215 && DECL_CONV_FN_P (base_fndecl)
2216 && same_type_p (DECL_CONV_FN_TYPE (fndecl),
2217 DECL_CONV_FN_TYPE (base_fndecl))))
2219 tree fntype = TREE_TYPE (fndecl);
2220 tree base_fntype = TREE_TYPE (base_fndecl);
2221 if (type_memfn_quals (fntype) == type_memfn_quals (base_fntype)
2222 && type_memfn_rqual (fntype) == type_memfn_rqual (base_fntype)
2223 && compparms (FUNCTION_FIRST_USER_PARMTYPE (fndecl),
2224 FUNCTION_FIRST_USER_PARMTYPE (base_fndecl)))
2225 return 1;
2227 return 0;
2230 /* Returns TRUE if DERIVED is a binfo containing the binfo BASE as a
2231 subobject. */
2233 static bool
2234 base_derived_from (tree derived, tree base)
2236 tree probe;
2238 for (probe = base; probe; probe = BINFO_INHERITANCE_CHAIN (probe))
2240 if (probe == derived)
2241 return true;
2242 else if (BINFO_VIRTUAL_P (probe))
2243 /* If we meet a virtual base, we can't follow the inheritance
2244 any more. See if the complete type of DERIVED contains
2245 such a virtual base. */
2246 return (binfo_for_vbase (BINFO_TYPE (probe), BINFO_TYPE (derived))
2247 != NULL_TREE);
2249 return false;
2252 struct find_final_overrider_data {
2253 /* The function for which we are trying to find a final overrider. */
2254 tree fn;
2255 /* The base class in which the function was declared. */
2256 tree declaring_base;
2257 /* The candidate overriders. */
2258 tree candidates;
2259 /* Path to most derived. */
2260 vec<tree> path;
2263 /* Add the overrider along the current path to FFOD->CANDIDATES.
2264 Returns true if an overrider was found; false otherwise. */
2266 static bool
2267 dfs_find_final_overrider_1 (tree binfo,
2268 find_final_overrider_data *ffod,
2269 unsigned depth)
2271 tree method;
2273 /* If BINFO is not the most derived type, try a more derived class.
2274 A definition there will overrider a definition here. */
2275 if (depth)
2277 depth--;
2278 if (dfs_find_final_overrider_1
2279 (ffod->path[depth], ffod, depth))
2280 return true;
2283 method = look_for_overrides_here (BINFO_TYPE (binfo), ffod->fn);
2284 if (method)
2286 tree *candidate = &ffod->candidates;
2288 /* Remove any candidates overridden by this new function. */
2289 while (*candidate)
2291 /* If *CANDIDATE overrides METHOD, then METHOD
2292 cannot override anything else on the list. */
2293 if (base_derived_from (TREE_VALUE (*candidate), binfo))
2294 return true;
2295 /* If METHOD overrides *CANDIDATE, remove *CANDIDATE. */
2296 if (base_derived_from (binfo, TREE_VALUE (*candidate)))
2297 *candidate = TREE_CHAIN (*candidate);
2298 else
2299 candidate = &TREE_CHAIN (*candidate);
2302 /* Add the new function. */
2303 ffod->candidates = tree_cons (method, binfo, ffod->candidates);
2304 return true;
2307 return false;
2310 /* Called from find_final_overrider via dfs_walk. */
2312 static tree
2313 dfs_find_final_overrider_pre (tree binfo, void *data)
2315 find_final_overrider_data *ffod = (find_final_overrider_data *) data;
2317 if (binfo == ffod->declaring_base)
2318 dfs_find_final_overrider_1 (binfo, ffod, ffod->path.length ());
2319 ffod->path.safe_push (binfo);
2321 return NULL_TREE;
2324 static tree
2325 dfs_find_final_overrider_post (tree /*binfo*/, void *data)
2327 find_final_overrider_data *ffod = (find_final_overrider_data *) data;
2328 ffod->path.pop ();
2330 return NULL_TREE;
2333 /* Returns a TREE_LIST whose TREE_PURPOSE is the final overrider for
2334 FN and whose TREE_VALUE is the binfo for the base where the
2335 overriding occurs. BINFO (in the hierarchy dominated by the binfo
2336 DERIVED) is the base object in which FN is declared. */
2338 static tree
2339 find_final_overrider (tree derived, tree binfo, tree fn)
2341 find_final_overrider_data ffod;
2343 /* Getting this right is a little tricky. This is valid:
2345 struct S { virtual void f (); };
2346 struct T { virtual void f (); };
2347 struct U : public S, public T { };
2349 even though calling `f' in `U' is ambiguous. But,
2351 struct R { virtual void f(); };
2352 struct S : virtual public R { virtual void f (); };
2353 struct T : virtual public R { virtual void f (); };
2354 struct U : public S, public T { };
2356 is not -- there's no way to decide whether to put `S::f' or
2357 `T::f' in the vtable for `R'.
2359 The solution is to look at all paths to BINFO. If we find
2360 different overriders along any two, then there is a problem. */
2361 if (DECL_THUNK_P (fn))
2362 fn = THUNK_TARGET (fn);
2364 /* Determine the depth of the hierarchy. */
2365 ffod.fn = fn;
2366 ffod.declaring_base = binfo;
2367 ffod.candidates = NULL_TREE;
2368 ffod.path.create (30);
2370 dfs_walk_all (derived, dfs_find_final_overrider_pre,
2371 dfs_find_final_overrider_post, &ffod);
2373 ffod.path.release ();
2375 /* If there was no winner, issue an error message. */
2376 if (!ffod.candidates || TREE_CHAIN (ffod.candidates))
2377 return error_mark_node;
2379 return ffod.candidates;
2382 /* Return the index of the vcall offset for FN when TYPE is used as a
2383 virtual base. */
2385 static tree
2386 get_vcall_index (tree fn, tree type)
2388 vec<tree_pair_s, va_gc> *indices = CLASSTYPE_VCALL_INDICES (type);
2389 tree_pair_p p;
2390 unsigned ix;
2392 FOR_EACH_VEC_SAFE_ELT (indices, ix, p)
2393 if ((DECL_DESTRUCTOR_P (fn) && DECL_DESTRUCTOR_P (p->purpose))
2394 || same_signature_p (fn, p->purpose))
2395 return p->value;
2397 /* There should always be an appropriate index. */
2398 gcc_unreachable ();
2401 /* Update an entry in the vtable for BINFO, which is in the hierarchy
2402 dominated by T. FN is the old function; VIRTUALS points to the
2403 corresponding position in the new BINFO_VIRTUALS list. IX is the index
2404 of that entry in the list. */
2406 static void
2407 update_vtable_entry_for_fn (tree t, tree binfo, tree fn, tree* virtuals,
2408 unsigned ix)
2410 tree b;
2411 tree overrider;
2412 tree delta;
2413 tree virtual_base;
2414 tree first_defn;
2415 tree overrider_fn, overrider_target;
2416 tree target_fn = DECL_THUNK_P (fn) ? THUNK_TARGET (fn) : fn;
2417 tree over_return, base_return;
2418 bool lost = false;
2420 /* Find the nearest primary base (possibly binfo itself) which defines
2421 this function; this is the class the caller will convert to when
2422 calling FN through BINFO. */
2423 for (b = binfo; ; b = get_primary_binfo (b))
2425 gcc_assert (b);
2426 if (look_for_overrides_here (BINFO_TYPE (b), target_fn))
2427 break;
2429 /* The nearest definition is from a lost primary. */
2430 if (BINFO_LOST_PRIMARY_P (b))
2431 lost = true;
2433 first_defn = b;
2435 /* Find the final overrider. */
2436 overrider = find_final_overrider (TYPE_BINFO (t), b, target_fn);
2437 if (overrider == error_mark_node)
2439 error ("no unique final overrider for %qD in %qT", target_fn, t);
2440 return;
2442 overrider_target = overrider_fn = TREE_PURPOSE (overrider);
2444 /* Check for adjusting covariant return types. */
2445 over_return = TREE_TYPE (TREE_TYPE (overrider_target));
2446 base_return = TREE_TYPE (TREE_TYPE (target_fn));
2448 if (INDIRECT_TYPE_P (over_return)
2449 && TREE_CODE (over_return) == TREE_CODE (base_return)
2450 && CLASS_TYPE_P (TREE_TYPE (over_return))
2451 && CLASS_TYPE_P (TREE_TYPE (base_return))
2452 /* If the overrider is invalid, don't even try. */
2453 && !DECL_INVALID_OVERRIDER_P (overrider_target))
2455 /* If FN is a covariant thunk, we must figure out the adjustment
2456 to the final base FN was converting to. As OVERRIDER_TARGET might
2457 also be converting to the return type of FN, we have to
2458 combine the two conversions here. */
2459 tree fixed_offset, virtual_offset;
2461 over_return = TREE_TYPE (over_return);
2462 base_return = TREE_TYPE (base_return);
2464 if (DECL_THUNK_P (fn))
2466 gcc_assert (DECL_RESULT_THUNK_P (fn));
2467 fixed_offset = ssize_int (THUNK_FIXED_OFFSET (fn));
2468 virtual_offset = THUNK_VIRTUAL_OFFSET (fn);
2470 else
2471 fixed_offset = virtual_offset = NULL_TREE;
2473 if (virtual_offset)
2474 /* Find the equivalent binfo within the return type of the
2475 overriding function. We will want the vbase offset from
2476 there. */
2477 virtual_offset = binfo_for_vbase (BINFO_TYPE (virtual_offset),
2478 over_return);
2479 else if (!same_type_ignoring_top_level_qualifiers_p
2480 (over_return, base_return))
2482 /* There was no existing virtual thunk (which takes
2483 precedence). So find the binfo of the base function's
2484 return type within the overriding function's return type.
2485 Fortunately we know the covariancy is valid (it
2486 has already been checked), so we can just iterate along
2487 the binfos, which have been chained in inheritance graph
2488 order. Of course it is lame that we have to repeat the
2489 search here anyway -- we should really be caching pieces
2490 of the vtable and avoiding this repeated work. */
2491 tree thunk_binfo = NULL_TREE;
2492 tree base_binfo = TYPE_BINFO (base_return);
2494 /* Find the base binfo within the overriding function's
2495 return type. We will always find a thunk_binfo, except
2496 when the covariancy is invalid (which we will have
2497 already diagnosed). */
2498 if (base_binfo)
2499 for (thunk_binfo = TYPE_BINFO (over_return); thunk_binfo;
2500 thunk_binfo = TREE_CHAIN (thunk_binfo))
2501 if (SAME_BINFO_TYPE_P (BINFO_TYPE (thunk_binfo),
2502 BINFO_TYPE (base_binfo)))
2503 break;
2504 gcc_assert (thunk_binfo || errorcount);
2506 /* See if virtual inheritance is involved. */
2507 for (virtual_offset = thunk_binfo;
2508 virtual_offset;
2509 virtual_offset = BINFO_INHERITANCE_CHAIN (virtual_offset))
2510 if (BINFO_VIRTUAL_P (virtual_offset))
2511 break;
2513 if (virtual_offset
2514 || (thunk_binfo && !BINFO_OFFSET_ZEROP (thunk_binfo)))
2516 tree offset = fold_convert (ssizetype, BINFO_OFFSET (thunk_binfo));
2518 if (virtual_offset)
2520 /* We convert via virtual base. Adjust the fixed
2521 offset to be from there. */
2522 offset =
2523 size_diffop (offset,
2524 fold_convert (ssizetype,
2525 BINFO_OFFSET (virtual_offset)));
2527 if (fixed_offset)
2528 /* There was an existing fixed offset, this must be
2529 from the base just converted to, and the base the
2530 FN was thunking to. */
2531 fixed_offset = size_binop (PLUS_EXPR, fixed_offset, offset);
2532 else
2533 fixed_offset = offset;
2537 if (fixed_offset || virtual_offset)
2538 /* Replace the overriding function with a covariant thunk. We
2539 will emit the overriding function in its own slot as
2540 well. */
2541 overrider_fn = make_thunk (overrider_target, /*this_adjusting=*/0,
2542 fixed_offset, virtual_offset);
2544 else
2545 gcc_assert (DECL_INVALID_OVERRIDER_P (overrider_target) ||
2546 !DECL_THUNK_P (fn));
2548 /* If we need a covariant thunk, then we may need to adjust first_defn.
2549 The ABI specifies that the thunks emitted with a function are
2550 determined by which bases the function overrides, so we need to be
2551 sure that we're using a thunk for some overridden base; even if we
2552 know that the necessary this adjustment is zero, there may not be an
2553 appropriate zero-this-adjustment thunk for us to use since thunks for
2554 overriding virtual bases always use the vcall offset.
2556 Furthermore, just choosing any base that overrides this function isn't
2557 quite right, as this slot won't be used for calls through a type that
2558 puts a covariant thunk here. Calling the function through such a type
2559 will use a different slot, and that slot is the one that determines
2560 the thunk emitted for that base.
2562 So, keep looking until we find the base that we're really overriding
2563 in this slot: the nearest primary base that doesn't use a covariant
2564 thunk in this slot. */
2565 if (overrider_target != overrider_fn)
2567 if (BINFO_TYPE (b) == DECL_CONTEXT (overrider_target))
2568 /* We already know that the overrider needs a covariant thunk. */
2569 b = get_primary_binfo (b);
2570 for (; ; b = get_primary_binfo (b))
2572 tree main_binfo = TYPE_BINFO (BINFO_TYPE (b));
2573 tree bv = chain_index (ix, BINFO_VIRTUALS (main_binfo));
2574 if (!DECL_THUNK_P (TREE_VALUE (bv)))
2575 break;
2576 if (BINFO_LOST_PRIMARY_P (b))
2577 lost = true;
2579 first_defn = b;
2582 /* Assume that we will produce a thunk that convert all the way to
2583 the final overrider, and not to an intermediate virtual base. */
2584 virtual_base = NULL_TREE;
2586 /* See if we can convert to an intermediate virtual base first, and then
2587 use the vcall offset located there to finish the conversion. */
2588 for (; b; b = BINFO_INHERITANCE_CHAIN (b))
2590 /* If we find the final overrider, then we can stop
2591 walking. */
2592 if (SAME_BINFO_TYPE_P (BINFO_TYPE (b),
2593 BINFO_TYPE (TREE_VALUE (overrider))))
2594 break;
2596 /* If we find a virtual base, and we haven't yet found the
2597 overrider, then there is a virtual base between the
2598 declaring base (first_defn) and the final overrider. */
2599 if (BINFO_VIRTUAL_P (b))
2601 virtual_base = b;
2602 break;
2606 /* Compute the constant adjustment to the `this' pointer. The
2607 `this' pointer, when this function is called, will point at BINFO
2608 (or one of its primary bases, which are at the same offset). */
2609 if (virtual_base)
2610 /* The `this' pointer needs to be adjusted from the declaration to
2611 the nearest virtual base. */
2612 delta = size_diffop_loc (input_location,
2613 fold_convert (ssizetype, BINFO_OFFSET (virtual_base)),
2614 fold_convert (ssizetype, BINFO_OFFSET (first_defn)));
2615 else if (lost)
2616 /* If the nearest definition is in a lost primary, we don't need an
2617 entry in our vtable. Except possibly in a constructor vtable,
2618 if we happen to get our primary back. In that case, the offset
2619 will be zero, as it will be a primary base. */
2620 delta = size_zero_node;
2621 else
2622 /* The `this' pointer needs to be adjusted from pointing to
2623 BINFO to pointing at the base where the final overrider
2624 appears. */
2625 delta = size_diffop_loc (input_location,
2626 fold_convert (ssizetype,
2627 BINFO_OFFSET (TREE_VALUE (overrider))),
2628 fold_convert (ssizetype, BINFO_OFFSET (binfo)));
2630 modify_vtable_entry (t, binfo, overrider_fn, delta, virtuals);
2632 if (virtual_base)
2633 BV_VCALL_INDEX (*virtuals)
2634 = get_vcall_index (overrider_target, BINFO_TYPE (virtual_base));
2635 else
2636 BV_VCALL_INDEX (*virtuals) = NULL_TREE;
2638 BV_LOST_PRIMARY (*virtuals) = lost;
2641 /* Called from modify_all_vtables via dfs_walk. */
2643 static tree
2644 dfs_modify_vtables (tree binfo, void* data)
2646 tree t = (tree) data;
2647 tree virtuals;
2648 tree old_virtuals;
2649 unsigned ix;
2651 if (!TYPE_CONTAINS_VPTR_P (BINFO_TYPE (binfo)))
2652 /* A base without a vtable needs no modification, and its bases
2653 are uninteresting. */
2654 return dfs_skip_bases;
2656 if (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), t)
2657 && !CLASSTYPE_HAS_PRIMARY_BASE_P (t))
2658 /* Don't do the primary vtable, if it's new. */
2659 return NULL_TREE;
2661 if (BINFO_PRIMARY_P (binfo) && !BINFO_VIRTUAL_P (binfo))
2662 /* There's no need to modify the vtable for a non-virtual primary
2663 base; we're not going to use that vtable anyhow. We do still
2664 need to do this for virtual primary bases, as they could become
2665 non-primary in a construction vtable. */
2666 return NULL_TREE;
2668 make_new_vtable (t, binfo);
2670 /* Now, go through each of the virtual functions in the virtual
2671 function table for BINFO. Find the final overrider, and update
2672 the BINFO_VIRTUALS list appropriately. */
2673 for (ix = 0, virtuals = BINFO_VIRTUALS (binfo),
2674 old_virtuals = BINFO_VIRTUALS (TYPE_BINFO (BINFO_TYPE (binfo)));
2675 virtuals;
2676 ix++, virtuals = TREE_CHAIN (virtuals),
2677 old_virtuals = TREE_CHAIN (old_virtuals))
2678 update_vtable_entry_for_fn (t,
2679 binfo,
2680 BV_FN (old_virtuals),
2681 &virtuals, ix);
2683 return NULL_TREE;
2686 /* Update all of the primary and secondary vtables for T. Create new
2687 vtables as required, and initialize their RTTI information. Each
2688 of the functions in VIRTUALS is declared in T and may override a
2689 virtual function from a base class; find and modify the appropriate
2690 entries to point to the overriding functions. Returns a list, in
2691 declaration order, of the virtual functions that are declared in T,
2692 but do not appear in the primary base class vtable, and which
2693 should therefore be appended to the end of the vtable for T. */
2695 static tree
2696 modify_all_vtables (tree t, tree virtuals)
2698 tree binfo = TYPE_BINFO (t);
2699 tree *fnsp;
2701 /* Mangle the vtable name before entering dfs_walk (c++/51884). */
2702 if (TYPE_CONTAINS_VPTR_P (t))
2703 get_vtable_decl (t, false);
2705 /* Update all of the vtables. */
2706 dfs_walk_once (binfo, dfs_modify_vtables, NULL, t);
2708 /* Add virtual functions not already in our primary vtable. These
2709 will be both those introduced by this class, and those overridden
2710 from secondary bases. It does not include virtuals merely
2711 inherited from secondary bases. */
2712 for (fnsp = &virtuals; *fnsp; )
2714 tree fn = TREE_VALUE (*fnsp);
2716 if (!value_member (fn, BINFO_VIRTUALS (binfo))
2717 || DECL_VINDEX (fn) == error_mark_node)
2719 /* We don't need to adjust the `this' pointer when
2720 calling this function. */
2721 BV_DELTA (*fnsp) = integer_zero_node;
2722 BV_VCALL_INDEX (*fnsp) = NULL_TREE;
2724 /* This is a function not already in our vtable. Keep it. */
2725 fnsp = &TREE_CHAIN (*fnsp);
2727 else
2728 /* We've already got an entry for this function. Skip it. */
2729 *fnsp = TREE_CHAIN (*fnsp);
2732 return virtuals;
2735 /* Get the base virtual function declarations in T that have the
2736 indicated NAME. */
2738 static void
2739 get_basefndecls (tree name, tree t, vec<tree> *base_fndecls)
2741 bool found_decls = false;
2743 /* Find virtual functions in T with the indicated NAME. */
2744 for (ovl_iterator iter (get_class_binding (t, name)); iter; ++iter)
2746 tree method = *iter;
2748 if (TREE_CODE (method) == FUNCTION_DECL && DECL_VINDEX (method))
2750 base_fndecls->safe_push (method);
2751 found_decls = true;
2755 if (found_decls)
2756 return;
2758 int n_baseclasses = BINFO_N_BASE_BINFOS (TYPE_BINFO (t));
2759 for (int i = 0; i < n_baseclasses; i++)
2761 tree basetype = BINFO_TYPE (BINFO_BASE_BINFO (TYPE_BINFO (t), i));
2762 get_basefndecls (name, basetype, base_fndecls);
2766 /* If this declaration supersedes the declaration of
2767 a method declared virtual in the base class, then
2768 mark this field as being virtual as well. */
2770 void
2771 check_for_override (tree decl, tree ctype)
2773 bool overrides_found = false;
2774 if (TREE_CODE (decl) == TEMPLATE_DECL)
2775 /* In [temp.mem] we have:
2777 A specialization of a member function template does not
2778 override a virtual function from a base class. */
2779 return;
2780 if ((DECL_DESTRUCTOR_P (decl)
2781 || IDENTIFIER_VIRTUAL_P (DECL_NAME (decl))
2782 || DECL_CONV_FN_P (decl))
2783 && look_for_overrides (ctype, decl)
2784 && !DECL_STATIC_FUNCTION_P (decl))
2785 /* Set DECL_VINDEX to a value that is neither an INTEGER_CST nor
2786 the error_mark_node so that we know it is an overriding
2787 function. */
2789 DECL_VINDEX (decl) = decl;
2790 overrides_found = true;
2791 if (warn_override && !DECL_OVERRIDE_P (decl)
2792 && !DECL_DESTRUCTOR_P (decl))
2793 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wsuggest_override,
2794 "%qD can be marked override", decl);
2797 if (DECL_VIRTUAL_P (decl))
2799 if (!DECL_VINDEX (decl))
2800 DECL_VINDEX (decl) = error_mark_node;
2801 IDENTIFIER_VIRTUAL_P (DECL_NAME (decl)) = 1;
2802 if (DECL_DESTRUCTOR_P (decl))
2803 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (ctype) = true;
2805 else if (DECL_FINAL_P (decl))
2806 error ("%q+#D marked %<final%>, but is not virtual", decl);
2807 if (DECL_OVERRIDE_P (decl) && !overrides_found)
2808 error ("%q+#D marked %<override%>, but does not override", decl);
2811 /* Warn about hidden virtual functions that are not overridden in t.
2812 We know that constructors and destructors don't apply. */
2814 static void
2815 warn_hidden (tree t)
2817 if (vec<tree, va_gc> *member_vec = CLASSTYPE_MEMBER_VEC (t))
2818 for (unsigned ix = member_vec->length (); ix--;)
2820 tree fns = (*member_vec)[ix];
2822 if (!OVL_P (fns))
2823 continue;
2825 tree name = OVL_NAME (fns);
2826 auto_vec<tree, 20> base_fndecls;
2827 tree base_binfo;
2828 tree binfo;
2829 unsigned j;
2831 /* Iterate through all of the base classes looking for possibly
2832 hidden functions. */
2833 for (binfo = TYPE_BINFO (t), j = 0;
2834 BINFO_BASE_ITERATE (binfo, j, base_binfo); j++)
2836 tree basetype = BINFO_TYPE (base_binfo);
2837 get_basefndecls (name, basetype, &base_fndecls);
2840 /* If there are no functions to hide, continue. */
2841 if (base_fndecls.is_empty ())
2842 continue;
2844 /* Remove any overridden functions. */
2845 for (ovl_iterator iter (fns); iter; ++iter)
2847 tree fndecl = *iter;
2848 if (TREE_CODE (fndecl) == FUNCTION_DECL
2849 && DECL_VINDEX (fndecl))
2851 /* If the method from the base class has the same
2852 signature as the method from the derived class, it
2853 has been overridden. */
2854 for (size_t k = 0; k < base_fndecls.length (); k++)
2855 if (base_fndecls[k]
2856 && same_signature_p (fndecl, base_fndecls[k]))
2857 base_fndecls[k] = NULL_TREE;
2861 /* Now give a warning for all base functions without overriders,
2862 as they are hidden. */
2863 tree base_fndecl;
2864 FOR_EACH_VEC_ELT (base_fndecls, j, base_fndecl)
2865 if (base_fndecl)
2867 /* Here we know it is a hider, and no overrider exists. */
2868 warning_at (location_of (base_fndecl),
2869 OPT_Woverloaded_virtual,
2870 "%qD was hidden", base_fndecl);
2871 warning_at (location_of (fns),
2872 OPT_Woverloaded_virtual, " by %qD", fns);
2877 /* Recursive helper for finish_struct_anon. */
2879 static void
2880 finish_struct_anon_r (tree field, bool complain)
2882 for (tree elt = TYPE_FIELDS (TREE_TYPE (field)); elt; elt = DECL_CHAIN (elt))
2884 /* We're generally only interested in entities the user
2885 declared, but we also find nested classes by noticing
2886 the TYPE_DECL that we create implicitly. You're
2887 allowed to put one anonymous union inside another,
2888 though, so we explicitly tolerate that. We use
2889 TYPE_UNNAMED_P rather than ANON_AGGR_TYPE_P so that
2890 we also allow unnamed types used for defining fields. */
2891 if (DECL_ARTIFICIAL (elt)
2892 && (!DECL_IMPLICIT_TYPEDEF_P (elt)
2893 || TYPE_UNNAMED_P (TREE_TYPE (elt))))
2894 continue;
2896 if (complain
2897 && (TREE_CODE (elt) != FIELD_DECL
2898 || (TREE_PRIVATE (elt) || TREE_PROTECTED (elt))))
2900 /* We already complained about static data members in
2901 finish_static_data_member_decl. */
2902 if (!VAR_P (elt)
2903 && permerror (DECL_SOURCE_LOCATION (elt),
2904 TREE_CODE (TREE_TYPE (field)) == UNION_TYPE
2905 ? "%q#D invalid; an anonymous union may "
2906 "only have public non-static data members"
2907 : "%q#D invalid; an anonymous struct may "
2908 "only have public non-static data members", elt))
2910 static bool hint;
2911 if (flag_permissive && !hint)
2913 hint = true;
2914 inform (DECL_SOURCE_LOCATION (elt),
2915 "this flexibility is deprecated and will be removed");
2920 TREE_PRIVATE (elt) = TREE_PRIVATE (field);
2921 TREE_PROTECTED (elt) = TREE_PROTECTED (field);
2923 /* Recurse into the anonymous aggregates to correctly handle
2924 access control (c++/24926):
2926 class A {
2927 union {
2928 union {
2929 int i;
2934 int j=A().i; */
2935 if (DECL_NAME (elt) == NULL_TREE
2936 && ANON_AGGR_TYPE_P (TREE_TYPE (elt)))
2937 finish_struct_anon_r (elt, /*complain=*/false);
2941 /* Check for things that are invalid. There are probably plenty of other
2942 things we should check for also. */
2944 static void
2945 finish_struct_anon (tree t)
2947 for (tree field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
2949 if (TREE_STATIC (field))
2950 continue;
2951 if (TREE_CODE (field) != FIELD_DECL)
2952 continue;
2954 if (DECL_NAME (field) == NULL_TREE
2955 && ANON_AGGR_TYPE_P (TREE_TYPE (field)))
2956 finish_struct_anon_r (field, /*complain=*/true);
2960 /* Add T to CLASSTYPE_DECL_LIST of current_class_type which
2961 will be used later during class template instantiation.
2962 When FRIEND_P is zero, T can be a static member data (VAR_DECL),
2963 a non-static member data (FIELD_DECL), a member function
2964 (FUNCTION_DECL), a nested type (RECORD_TYPE, ENUM_TYPE),
2965 a typedef (TYPE_DECL) or a member class template (TEMPLATE_DECL)
2966 When FRIEND_P is nonzero, T is either a friend class
2967 (RECORD_TYPE, TEMPLATE_DECL) or a friend function
2968 (FUNCTION_DECL, TEMPLATE_DECL). */
2970 void
2971 maybe_add_class_template_decl_list (tree type, tree t, int friend_p)
2973 /* Save some memory by not creating TREE_LIST if TYPE is not template. */
2974 if (CLASSTYPE_TEMPLATE_INFO (type))
2975 CLASSTYPE_DECL_LIST (type)
2976 = tree_cons (friend_p ? NULL_TREE : type,
2977 t, CLASSTYPE_DECL_LIST (type));
2980 /* This function is called from declare_virt_assop_and_dtor via
2981 dfs_walk_all.
2983 DATA is a type that direcly or indirectly inherits the base
2984 represented by BINFO. If BINFO contains a virtual assignment [copy
2985 assignment or move assigment] operator or a virtual constructor,
2986 declare that function in DATA if it hasn't been already declared. */
2988 static tree
2989 dfs_declare_virt_assop_and_dtor (tree binfo, void *data)
2991 tree bv, fn, t = (tree)data;
2992 tree opname = assign_op_identifier;
2994 gcc_assert (t && CLASS_TYPE_P (t));
2995 gcc_assert (binfo && TREE_CODE (binfo) == TREE_BINFO);
2997 if (!TYPE_CONTAINS_VPTR_P (BINFO_TYPE (binfo)))
2998 /* A base without a vtable needs no modification, and its bases
2999 are uninteresting. */
3000 return dfs_skip_bases;
3002 if (BINFO_PRIMARY_P (binfo))
3003 /* If this is a primary base, then we have already looked at the
3004 virtual functions of its vtable. */
3005 return NULL_TREE;
3007 for (bv = BINFO_VIRTUALS (binfo); bv; bv = TREE_CHAIN (bv))
3009 fn = BV_FN (bv);
3011 if (DECL_NAME (fn) == opname)
3013 if (CLASSTYPE_LAZY_COPY_ASSIGN (t))
3014 lazily_declare_fn (sfk_copy_assignment, t);
3015 if (CLASSTYPE_LAZY_MOVE_ASSIGN (t))
3016 lazily_declare_fn (sfk_move_assignment, t);
3018 else if (DECL_DESTRUCTOR_P (fn)
3019 && CLASSTYPE_LAZY_DESTRUCTOR (t))
3020 lazily_declare_fn (sfk_destructor, t);
3023 return NULL_TREE;
3026 /* If the class type T has a direct or indirect base that contains a
3027 virtual assignment operator or a virtual destructor, declare that
3028 function in T if it hasn't been already declared. */
3030 static void
3031 declare_virt_assop_and_dtor (tree t)
3033 if (!(TYPE_POLYMORPHIC_P (t)
3034 && (CLASSTYPE_LAZY_COPY_ASSIGN (t)
3035 || CLASSTYPE_LAZY_MOVE_ASSIGN (t)
3036 || CLASSTYPE_LAZY_DESTRUCTOR (t))))
3037 return;
3039 dfs_walk_all (TYPE_BINFO (t),
3040 dfs_declare_virt_assop_and_dtor,
3041 NULL, t);
3044 /* Declare the inheriting constructor for class T inherited from base
3045 constructor CTOR with the parameter array PARMS of size NPARMS. */
3047 static void
3048 one_inheriting_sig (tree t, tree ctor, tree *parms, int nparms)
3050 gcc_assert (TYPE_MAIN_VARIANT (t) == t);
3052 /* We don't declare an inheriting ctor that would be a default,
3053 copy or move ctor for derived or base. */
3054 if (nparms == 0)
3055 return;
3056 if (nparms == 1
3057 && TYPE_REF_P (parms[0]))
3059 tree parm = TYPE_MAIN_VARIANT (TREE_TYPE (parms[0]));
3060 if (parm == t || parm == DECL_CONTEXT (ctor))
3061 return;
3064 tree parmlist = void_list_node;
3065 for (int i = nparms - 1; i >= 0; i--)
3066 parmlist = tree_cons (NULL_TREE, parms[i], parmlist);
3067 tree fn = implicitly_declare_fn (sfk_inheriting_constructor,
3068 t, false, ctor, parmlist);
3070 if (add_method (t, fn, false))
3072 DECL_CHAIN (fn) = TYPE_FIELDS (t);
3073 TYPE_FIELDS (t) = fn;
3077 /* Declare all the inheriting constructors for class T inherited from base
3078 constructor CTOR. */
3080 static void
3081 one_inherited_ctor (tree ctor, tree t, tree using_decl)
3083 tree parms = FUNCTION_FIRST_USER_PARMTYPE (ctor);
3085 if (flag_new_inheriting_ctors)
3087 ctor = implicitly_declare_fn (sfk_inheriting_constructor,
3088 t, /*const*/false, ctor, parms);
3089 add_method (t, ctor, using_decl != NULL_TREE);
3090 TYPE_HAS_USER_CONSTRUCTOR (t) = true;
3091 return;
3094 tree *new_parms = XALLOCAVEC (tree, list_length (parms));
3095 int i = 0;
3096 for (; parms && parms != void_list_node; parms = TREE_CHAIN (parms))
3098 if (TREE_PURPOSE (parms))
3099 one_inheriting_sig (t, ctor, new_parms, i);
3100 new_parms[i++] = TREE_VALUE (parms);
3102 one_inheriting_sig (t, ctor, new_parms, i);
3103 if (parms == NULL_TREE)
3105 if (warning (OPT_Winherited_variadic_ctor,
3106 "the ellipsis in %qD is not inherited", ctor))
3107 inform (DECL_SOURCE_LOCATION (ctor), "%qD declared here", ctor);
3111 /* Create default constructors, assignment operators, and so forth for
3112 the type indicated by T, if they are needed. CANT_HAVE_CONST_CTOR,
3113 and CANT_HAVE_CONST_ASSIGNMENT are nonzero if, for whatever reason,
3114 the class cannot have a default constructor, copy constructor
3115 taking a const reference argument, or an assignment operator taking
3116 a const reference, respectively. */
3118 static void
3119 add_implicitly_declared_members (tree t, tree* access_decls,
3120 int cant_have_const_cctor,
3121 int cant_have_const_assignment)
3123 /* Destructor. */
3124 if (!CLASSTYPE_DESTRUCTOR (t))
3125 /* In general, we create destructors lazily. */
3126 CLASSTYPE_LAZY_DESTRUCTOR (t) = 1;
3128 bool move_ok = false;
3129 if (cxx_dialect >= cxx11 && CLASSTYPE_LAZY_DESTRUCTOR (t)
3130 && !TYPE_HAS_COPY_CTOR (t) && !TYPE_HAS_COPY_ASSIGN (t)
3131 && !classtype_has_move_assign_or_move_ctor_p (t, false))
3132 move_ok = true;
3134 /* [class.ctor]
3136 If there is no user-declared constructor for a class, a default
3137 constructor is implicitly declared. */
3138 if (! TYPE_HAS_USER_CONSTRUCTOR (t))
3140 TYPE_HAS_DEFAULT_CONSTRUCTOR (t) = 1;
3141 CLASSTYPE_LAZY_DEFAULT_CTOR (t) = 1;
3142 if (cxx_dialect >= cxx11)
3143 TYPE_HAS_CONSTEXPR_CTOR (t)
3144 /* Don't force the declaration to get a hard answer; if the
3145 definition would have made the class non-literal, it will still be
3146 non-literal because of the base or member in question, and that
3147 gives a better diagnostic. */
3148 = type_maybe_constexpr_default_constructor (t);
3151 /* [class.ctor]
3153 If a class definition does not explicitly declare a copy
3154 constructor, one is declared implicitly. */
3155 if (! TYPE_HAS_COPY_CTOR (t))
3157 TYPE_HAS_COPY_CTOR (t) = 1;
3158 TYPE_HAS_CONST_COPY_CTOR (t) = !cant_have_const_cctor;
3159 CLASSTYPE_LAZY_COPY_CTOR (t) = 1;
3160 if (move_ok)
3161 CLASSTYPE_LAZY_MOVE_CTOR (t) = 1;
3164 /* If there is no assignment operator, one will be created if and
3165 when it is needed. For now, just record whether or not the type
3166 of the parameter to the assignment operator will be a const or
3167 non-const reference. */
3168 if (!TYPE_HAS_COPY_ASSIGN (t))
3170 TYPE_HAS_COPY_ASSIGN (t) = 1;
3171 TYPE_HAS_CONST_COPY_ASSIGN (t) = !cant_have_const_assignment;
3172 CLASSTYPE_LAZY_COPY_ASSIGN (t) = 1;
3173 if (move_ok && !LAMBDA_TYPE_P (t))
3174 CLASSTYPE_LAZY_MOVE_ASSIGN (t) = 1;
3177 /* We can't be lazy about declaring functions that might override
3178 a virtual function from a base class. */
3179 declare_virt_assop_and_dtor (t);
3181 while (*access_decls)
3183 tree using_decl = TREE_VALUE (*access_decls);
3184 tree decl = USING_DECL_DECLS (using_decl);
3185 if (DECL_NAME (using_decl) == ctor_identifier)
3187 /* declare, then remove the decl */
3188 tree ctor_list = decl;
3189 location_t loc = input_location;
3190 input_location = DECL_SOURCE_LOCATION (using_decl);
3191 for (ovl_iterator iter (ctor_list); iter; ++iter)
3192 one_inherited_ctor (*iter, t, using_decl);
3193 *access_decls = TREE_CHAIN (*access_decls);
3194 input_location = loc;
3196 else
3197 access_decls = &TREE_CHAIN (*access_decls);
3201 /* FIELD is a bit-field. We are finishing the processing for its
3202 enclosing type. Issue any appropriate messages and set appropriate
3203 flags. Returns false if an error has been diagnosed. */
3205 static bool
3206 check_bitfield_decl (tree field)
3208 tree type = TREE_TYPE (field);
3209 tree w;
3211 /* Extract the declared width of the bitfield, which has been
3212 temporarily stashed in DECL_BIT_FIELD_REPRESENTATIVE by grokbitfield. */
3213 w = DECL_BIT_FIELD_REPRESENTATIVE (field);
3214 gcc_assert (w != NULL_TREE);
3215 /* Remove the bit-field width indicator so that the rest of the
3216 compiler does not treat that value as a qualifier. */
3217 DECL_BIT_FIELD_REPRESENTATIVE (field) = NULL_TREE;
3219 /* Detect invalid bit-field type. */
3220 if (!INTEGRAL_OR_ENUMERATION_TYPE_P (type))
3222 error ("bit-field %q+#D with non-integral type", field);
3223 w = error_mark_node;
3225 else
3227 location_t loc = input_location;
3228 /* Avoid the non_lvalue wrapper added by fold for PLUS_EXPRs. */
3229 STRIP_NOPS (w);
3231 /* detect invalid field size. */
3232 input_location = DECL_SOURCE_LOCATION (field);
3233 w = cxx_constant_value (w);
3234 input_location = loc;
3236 if (TREE_CODE (w) != INTEGER_CST)
3238 error ("bit-field %q+D width not an integer constant", field);
3239 w = error_mark_node;
3241 else if (tree_int_cst_sgn (w) < 0)
3243 error ("negative width in bit-field %q+D", field);
3244 w = error_mark_node;
3246 else if (integer_zerop (w) && DECL_NAME (field) != 0)
3248 error ("zero width for bit-field %q+D", field);
3249 w = error_mark_node;
3251 else if ((TREE_CODE (type) != ENUMERAL_TYPE
3252 && TREE_CODE (type) != BOOLEAN_TYPE
3253 && compare_tree_int (w, TYPE_PRECISION (type)) > 0)
3254 || ((TREE_CODE (type) == ENUMERAL_TYPE
3255 || TREE_CODE (type) == BOOLEAN_TYPE)
3256 && tree_int_cst_lt (TYPE_SIZE (type), w)))
3257 warning_at (DECL_SOURCE_LOCATION (field), 0,
3258 "width of %qD exceeds its type", field);
3259 else if (TREE_CODE (type) == ENUMERAL_TYPE)
3261 int prec = TYPE_PRECISION (ENUM_UNDERLYING_TYPE (type));
3262 if (compare_tree_int (w, prec) < 0)
3263 warning_at (DECL_SOURCE_LOCATION (field), 0,
3264 "%qD is too small to hold all values of %q#T",
3265 field, type);
3269 if (w != error_mark_node)
3271 DECL_SIZE (field) = fold_convert (bitsizetype, w);
3272 DECL_BIT_FIELD (field) = 1;
3273 return true;
3275 else
3277 /* Non-bit-fields are aligned for their type. */
3278 DECL_BIT_FIELD (field) = 0;
3279 CLEAR_DECL_C_BIT_FIELD (field);
3280 return false;
3284 /* FIELD is a non bit-field. We are finishing the processing for its
3285 enclosing type T. Issue any appropriate messages and set appropriate
3286 flags. */
3288 static bool
3289 check_field_decl (tree field,
3290 tree t,
3291 int* cant_have_const_ctor,
3292 int* no_const_asn_ref)
3294 tree type = strip_array_types (TREE_TYPE (field));
3295 bool any_default_members = false;
3297 /* In C++98 an anonymous union cannot contain any fields which would change
3298 the settings of CANT_HAVE_CONST_CTOR and friends. */
3299 if (ANON_UNION_TYPE_P (type) && cxx_dialect < cxx11)
3301 /* And, we don't set TYPE_HAS_CONST_COPY_CTOR, etc., for anonymous
3302 structs. So, we recurse through their fields here. */
3303 else if (ANON_AGGR_TYPE_P (type))
3305 for (tree fields = TYPE_FIELDS (type); fields;
3306 fields = DECL_CHAIN (fields))
3307 if (TREE_CODE (fields) == FIELD_DECL)
3308 any_default_members |= check_field_decl (fields, t,
3309 cant_have_const_ctor,
3310 no_const_asn_ref);
3312 /* Check members with class type for constructors, destructors,
3313 etc. */
3314 else if (CLASS_TYPE_P (type))
3316 /* Never let anything with uninheritable virtuals
3317 make it through without complaint. */
3318 abstract_virtuals_error (field, type);
3320 if (TREE_CODE (t) == UNION_TYPE && cxx_dialect < cxx11)
3322 static bool warned;
3323 int oldcount = errorcount;
3324 if (TYPE_NEEDS_CONSTRUCTING (type))
3325 error ("member %q+#D with constructor not allowed in union",
3326 field);
3327 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
3328 error ("member %q+#D with destructor not allowed in union", field);
3329 if (TYPE_HAS_COMPLEX_COPY_ASSIGN (type))
3330 error ("member %q+#D with copy assignment operator not allowed in union",
3331 field);
3332 if (!warned && errorcount > oldcount)
3334 inform (DECL_SOURCE_LOCATION (field), "unrestricted unions "
3335 "only available with -std=c++11 or -std=gnu++11");
3336 warned = true;
3339 else
3341 TYPE_NEEDS_CONSTRUCTING (t) |= TYPE_NEEDS_CONSTRUCTING (type);
3342 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t)
3343 |= TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type);
3344 TYPE_HAS_COMPLEX_COPY_ASSIGN (t)
3345 |= (TYPE_HAS_COMPLEX_COPY_ASSIGN (type)
3346 || !TYPE_HAS_COPY_ASSIGN (type));
3347 TYPE_HAS_COMPLEX_COPY_CTOR (t) |= (TYPE_HAS_COMPLEX_COPY_CTOR (type)
3348 || !TYPE_HAS_COPY_CTOR (type));
3349 TYPE_HAS_COMPLEX_MOVE_ASSIGN (t) |= TYPE_HAS_COMPLEX_MOVE_ASSIGN (type);
3350 TYPE_HAS_COMPLEX_MOVE_CTOR (t) |= TYPE_HAS_COMPLEX_MOVE_CTOR (type);
3351 TYPE_HAS_COMPLEX_DFLT (t) |= (!TYPE_HAS_DEFAULT_CONSTRUCTOR (type)
3352 || TYPE_HAS_COMPLEX_DFLT (type));
3355 if (TYPE_HAS_COPY_CTOR (type)
3356 && !TYPE_HAS_CONST_COPY_CTOR (type))
3357 *cant_have_const_ctor = 1;
3359 if (TYPE_HAS_COPY_ASSIGN (type)
3360 && !TYPE_HAS_CONST_COPY_ASSIGN (type))
3361 *no_const_asn_ref = 1;
3364 check_abi_tags (t, field);
3366 if (DECL_INITIAL (field) != NULL_TREE)
3367 /* `build_class_init_list' does not recognize
3368 non-FIELD_DECLs. */
3369 any_default_members = true;
3371 return any_default_members;
3374 /* Check the data members (both static and non-static), class-scoped
3375 typedefs, etc., appearing in the declaration of T. Issue
3376 appropriate diagnostics. Sets ACCESS_DECLS to a list (in
3377 declaration order) of access declarations; each TREE_VALUE in this
3378 list is a USING_DECL.
3380 In addition, set the following flags:
3382 EMPTY_P
3383 The class is empty, i.e., contains no non-static data members.
3385 CANT_HAVE_CONST_CTOR_P
3386 This class cannot have an implicitly generated copy constructor
3387 taking a const reference.
3389 CANT_HAVE_CONST_ASN_REF
3390 This class cannot have an implicitly generated assignment
3391 operator taking a const reference.
3393 All of these flags should be initialized before calling this
3394 function.
3396 Returns a pointer to the end of the TYPE_FIELDs chain; additional
3397 fields can be added by adding to this chain. */
3399 static void
3400 check_field_decls (tree t, tree *access_decls,
3401 int *cant_have_const_ctor_p,
3402 int *no_const_asn_ref_p)
3404 tree *field;
3405 tree *next;
3406 bool has_pointers;
3407 bool any_default_members;
3408 int cant_pack = 0;
3409 int field_access = -1;
3411 /* Assume there are no access declarations. */
3412 *access_decls = NULL_TREE;
3413 /* Assume this class has no pointer members. */
3414 has_pointers = false;
3415 /* Assume none of the members of this class have default
3416 initializations. */
3417 any_default_members = false;
3419 for (field = &TYPE_FIELDS (t); *field; field = next)
3421 tree x = *field;
3422 tree type = TREE_TYPE (x);
3423 int this_field_access;
3425 next = &DECL_CHAIN (x);
3427 if (TREE_CODE (x) == USING_DECL)
3429 /* Save the access declarations for our caller. */
3430 *access_decls = tree_cons (NULL_TREE, x, *access_decls);
3431 continue;
3434 if (TREE_CODE (x) == TYPE_DECL
3435 || TREE_CODE (x) == TEMPLATE_DECL)
3436 continue;
3438 if (TREE_CODE (x) == FUNCTION_DECL)
3439 /* FIXME: We should fold in the checking from check_methods. */
3440 continue;
3442 /* If we've gotten this far, it's a data member, possibly static,
3443 or an enumerator. */
3444 if (TREE_CODE (x) != CONST_DECL)
3445 DECL_CONTEXT (x) = t;
3447 /* When this goes into scope, it will be a non-local reference. */
3448 DECL_NONLOCAL (x) = 1;
3450 if (TREE_CODE (t) == UNION_TYPE)
3452 /* [class.union] (C++98)
3454 If a union contains a static data member, or a member of
3455 reference type, the program is ill-formed.
3457 In C++11 [class.union] says:
3458 If a union contains a non-static data member of reference type
3459 the program is ill-formed. */
3460 if (VAR_P (x) && cxx_dialect < cxx11)
3462 error ("in C++98 %q+D may not be static because it is "
3463 "a member of a union", x);
3464 continue;
3466 if (TYPE_REF_P (type)
3467 && TREE_CODE (x) == FIELD_DECL)
3469 error ("non-static data member %q+D in a union may not "
3470 "have reference type %qT", x, type);
3471 continue;
3475 /* Perform error checking that did not get done in
3476 grokdeclarator. */
3477 if (TREE_CODE (type) == FUNCTION_TYPE)
3479 error ("field %q+D invalidly declared function type", x);
3480 type = build_pointer_type (type);
3481 TREE_TYPE (x) = type;
3483 else if (TREE_CODE (type) == METHOD_TYPE)
3485 error ("field %q+D invalidly declared method type", x);
3486 type = build_pointer_type (type);
3487 TREE_TYPE (x) = type;
3490 if (type == error_mark_node)
3491 continue;
3493 if (TREE_CODE (x) == CONST_DECL || VAR_P (x))
3494 continue;
3496 /* Now it can only be a FIELD_DECL. */
3498 if (TREE_PRIVATE (x) || TREE_PROTECTED (x))
3499 CLASSTYPE_NON_AGGREGATE (t) = 1;
3501 /* If at least one non-static data member is non-literal, the whole
3502 class becomes non-literal. Per Core/1453, volatile non-static
3503 data members and base classes are also not allowed.
3504 Note: if the type is incomplete we will complain later on. */
3505 if (COMPLETE_TYPE_P (type)
3506 && (!literal_type_p (type) || CP_TYPE_VOLATILE_P (type)))
3507 CLASSTYPE_LITERAL_P (t) = false;
3509 /* A standard-layout class is a class that:
3511 has the same access control (Clause 11) for all non-static data members,
3512 ... */
3513 this_field_access = TREE_PROTECTED (x) ? 1 : TREE_PRIVATE (x) ? 2 : 0;
3514 if (field_access == -1)
3515 field_access = this_field_access;
3516 else if (this_field_access != field_access)
3517 CLASSTYPE_NON_STD_LAYOUT (t) = 1;
3519 /* If this is of reference type, check if it needs an init. */
3520 if (TYPE_REF_P (type))
3522 CLASSTYPE_NON_LAYOUT_POD_P (t) = 1;
3523 CLASSTYPE_NON_STD_LAYOUT (t) = 1;
3524 if (DECL_INITIAL (x) == NULL_TREE)
3525 SET_CLASSTYPE_REF_FIELDS_NEED_INIT (t, 1);
3526 if (cxx_dialect < cxx11)
3528 /* ARM $12.6.2: [A member initializer list] (or, for an
3529 aggregate, initialization by a brace-enclosed list) is the
3530 only way to initialize nonstatic const and reference
3531 members. */
3532 TYPE_HAS_COMPLEX_COPY_ASSIGN (t) = 1;
3533 TYPE_HAS_COMPLEX_MOVE_ASSIGN (t) = 1;
3537 type = strip_array_types (type);
3539 if (TYPE_PACKED (t))
3541 if (!layout_pod_type_p (type) && !TYPE_PACKED (type))
3543 warning_at
3544 (DECL_SOURCE_LOCATION (x), 0,
3545 "ignoring packed attribute because of unpacked non-POD field %q#D",
3547 cant_pack = 1;
3549 else if (DECL_C_BIT_FIELD (x)
3550 || TYPE_ALIGN (TREE_TYPE (x)) > BITS_PER_UNIT)
3551 DECL_PACKED (x) = 1;
3554 if (DECL_C_BIT_FIELD (x)
3555 && integer_zerop (DECL_BIT_FIELD_REPRESENTATIVE (x)))
3556 /* We don't treat zero-width bitfields as making a class
3557 non-empty. */
3559 else
3561 /* The class is non-empty. */
3562 CLASSTYPE_EMPTY_P (t) = 0;
3563 /* The class is not even nearly empty. */
3564 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
3565 /* If one of the data members contains an empty class,
3566 so does T. */
3567 if (CLASS_TYPE_P (type)
3568 && CLASSTYPE_CONTAINS_EMPTY_CLASS_P (type))
3569 CLASSTYPE_CONTAINS_EMPTY_CLASS_P (t) = 1;
3572 /* This is used by -Weffc++ (see below). Warn only for pointers
3573 to members which might hold dynamic memory. So do not warn
3574 for pointers to functions or pointers to members. */
3575 if (TYPE_PTR_P (type)
3576 && !TYPE_PTRFN_P (type))
3577 has_pointers = true;
3579 if (CLASS_TYPE_P (type))
3581 if (CLASSTYPE_REF_FIELDS_NEED_INIT (type))
3582 SET_CLASSTYPE_REF_FIELDS_NEED_INIT (t, 1);
3583 if (CLASSTYPE_READONLY_FIELDS_NEED_INIT (type))
3584 SET_CLASSTYPE_READONLY_FIELDS_NEED_INIT (t, 1);
3587 if (DECL_MUTABLE_P (x) || TYPE_HAS_MUTABLE_P (type))
3588 CLASSTYPE_HAS_MUTABLE (t) = 1;
3590 if (DECL_MUTABLE_P (x))
3592 if (CP_TYPE_CONST_P (type))
3594 error ("member %q+D cannot be declared both %<const%> "
3595 "and %<mutable%>", x);
3596 continue;
3598 if (TYPE_REF_P (type))
3600 error ("member %q+D cannot be declared as a %<mutable%> "
3601 "reference", x);
3602 continue;
3606 if (! layout_pod_type_p (type))
3607 /* DR 148 now allows pointers to members (which are POD themselves),
3608 to be allowed in POD structs. */
3609 CLASSTYPE_NON_LAYOUT_POD_P (t) = 1;
3611 if (!std_layout_type_p (type))
3612 CLASSTYPE_NON_STD_LAYOUT (t) = 1;
3614 if (! zero_init_p (type))
3615 CLASSTYPE_NON_ZERO_INIT_P (t) = 1;
3617 /* We set DECL_C_BIT_FIELD in grokbitfield.
3618 If the type and width are valid, we'll also set DECL_BIT_FIELD. */
3619 if (DECL_C_BIT_FIELD (x))
3620 check_bitfield_decl (x);
3622 if (check_field_decl (x, t, cant_have_const_ctor_p, no_const_asn_ref_p))
3624 if (any_default_members
3625 && TREE_CODE (t) == UNION_TYPE)
3626 error ("multiple fields in union %qT initialized", t);
3627 any_default_members = true;
3630 /* Now that we've removed bit-field widths from DECL_INITIAL,
3631 anything left in DECL_INITIAL is an NSDMI that makes the class
3632 non-aggregate in C++11. */
3633 if (DECL_INITIAL (x) && cxx_dialect < cxx14)
3634 CLASSTYPE_NON_AGGREGATE (t) = true;
3636 /* If any field is const, the structure type is pseudo-const. */
3637 if (CP_TYPE_CONST_P (type))
3639 C_TYPE_FIELDS_READONLY (t) = 1;
3640 if (DECL_INITIAL (x) == NULL_TREE)
3641 SET_CLASSTYPE_READONLY_FIELDS_NEED_INIT (t, 1);
3642 if (cxx_dialect < cxx11)
3644 /* ARM $12.6.2: [A member initializer list] (or, for an
3645 aggregate, initialization by a brace-enclosed list) is the
3646 only way to initialize nonstatic const and reference
3647 members. */
3648 TYPE_HAS_COMPLEX_COPY_ASSIGN (t) = 1;
3649 TYPE_HAS_COMPLEX_MOVE_ASSIGN (t) = 1;
3652 /* A field that is pseudo-const makes the structure likewise. */
3653 else if (CLASS_TYPE_P (type))
3655 C_TYPE_FIELDS_READONLY (t) |= C_TYPE_FIELDS_READONLY (type);
3656 SET_CLASSTYPE_READONLY_FIELDS_NEED_INIT (t,
3657 CLASSTYPE_READONLY_FIELDS_NEED_INIT (t)
3658 | CLASSTYPE_READONLY_FIELDS_NEED_INIT (type));
3661 /* Core issue 80: A nonstatic data member is required to have a
3662 different name from the class iff the class has a
3663 user-declared constructor. */
3664 if (constructor_name_p (DECL_NAME (x), t)
3665 && TYPE_HAS_USER_CONSTRUCTOR (t))
3666 permerror (DECL_SOURCE_LOCATION (x),
3667 "field %q#D with same name as class", x);
3670 /* Effective C++ rule 11: if a class has dynamic memory held by pointers,
3671 it should also define a copy constructor and an assignment operator to
3672 implement the correct copy semantic (deep vs shallow, etc.). As it is
3673 not feasible to check whether the constructors do allocate dynamic memory
3674 and store it within members, we approximate the warning like this:
3676 -- Warn only if there are members which are pointers
3677 -- Warn only if there is a non-trivial constructor (otherwise,
3678 there cannot be memory allocated).
3679 -- Warn only if there is a non-trivial destructor. We assume that the
3680 user at least implemented the cleanup correctly, and a destructor
3681 is needed to free dynamic memory.
3683 This seems enough for practical purposes. */
3684 if (warn_ecpp
3685 && has_pointers
3686 && TYPE_HAS_USER_CONSTRUCTOR (t)
3687 && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t)
3688 && !(TYPE_HAS_COPY_CTOR (t) && TYPE_HAS_COPY_ASSIGN (t)))
3690 warning (OPT_Weffc__, "%q#T has pointer data members", t);
3692 if (! TYPE_HAS_COPY_CTOR (t))
3694 warning (OPT_Weffc__,
3695 " but does not override %<%T(const %T&)%>", t, t);
3696 if (!TYPE_HAS_COPY_ASSIGN (t))
3697 warning (OPT_Weffc__, " or %<operator=(const %T&)%>", t);
3699 else if (! TYPE_HAS_COPY_ASSIGN (t))
3700 warning (OPT_Weffc__,
3701 " but does not override %<operator=(const %T&)%>", t);
3704 /* Non-static data member initializers make the default constructor
3705 non-trivial. */
3706 if (any_default_members)
3708 TYPE_NEEDS_CONSTRUCTING (t) = true;
3709 TYPE_HAS_COMPLEX_DFLT (t) = true;
3712 /* If any of the fields couldn't be packed, unset TYPE_PACKED. */
3713 if (cant_pack)
3714 TYPE_PACKED (t) = 0;
3716 /* Check anonymous struct/anonymous union fields. */
3717 finish_struct_anon (t);
3719 /* We've built up the list of access declarations in reverse order.
3720 Fix that now. */
3721 *access_decls = nreverse (*access_decls);
3724 /* If TYPE is an empty class type, records its OFFSET in the table of
3725 OFFSETS. */
3727 static int
3728 record_subobject_offset (tree type, tree offset, splay_tree offsets)
3730 splay_tree_node n;
3732 if (!is_empty_class (type))
3733 return 0;
3735 /* Record the location of this empty object in OFFSETS. */
3736 n = splay_tree_lookup (offsets, (splay_tree_key) offset);
3737 if (!n)
3738 n = splay_tree_insert (offsets,
3739 (splay_tree_key) offset,
3740 (splay_tree_value) NULL_TREE);
3741 n->value = ((splay_tree_value)
3742 tree_cons (NULL_TREE,
3743 type,
3744 (tree) n->value));
3746 return 0;
3749 /* Returns nonzero if TYPE is an empty class type and there is
3750 already an entry in OFFSETS for the same TYPE as the same OFFSET. */
3752 static int
3753 check_subobject_offset (tree type, tree offset, splay_tree offsets)
3755 splay_tree_node n;
3756 tree t;
3758 if (!is_empty_class (type))
3759 return 0;
3761 /* Record the location of this empty object in OFFSETS. */
3762 n = splay_tree_lookup (offsets, (splay_tree_key) offset);
3763 if (!n)
3764 return 0;
3766 for (t = (tree) n->value; t; t = TREE_CHAIN (t))
3767 if (same_type_p (TREE_VALUE (t), type))
3768 return 1;
3770 return 0;
3773 /* Walk through all the subobjects of TYPE (located at OFFSET). Call
3774 F for every subobject, passing it the type, offset, and table of
3775 OFFSETS. If VBASES_P is one, then virtual non-primary bases should
3776 be traversed.
3778 If MAX_OFFSET is non-NULL, then subobjects with an offset greater
3779 than MAX_OFFSET will not be walked.
3781 If F returns a nonzero value, the traversal ceases, and that value
3782 is returned. Otherwise, returns zero. */
3784 static int
3785 walk_subobject_offsets (tree type,
3786 subobject_offset_fn f,
3787 tree offset,
3788 splay_tree offsets,
3789 tree max_offset,
3790 int vbases_p)
3792 int r = 0;
3793 tree type_binfo = NULL_TREE;
3795 /* If this OFFSET is bigger than the MAX_OFFSET, then we should
3796 stop. */
3797 if (max_offset && tree_int_cst_lt (max_offset, offset))
3798 return 0;
3800 if (type == error_mark_node)
3801 return 0;
3803 if (!TYPE_P (type))
3805 type_binfo = type;
3806 type = BINFO_TYPE (type);
3809 if (CLASS_TYPE_P (type))
3811 tree field;
3812 tree binfo;
3813 int i;
3815 /* Avoid recursing into objects that are not interesting. */
3816 if (!CLASSTYPE_CONTAINS_EMPTY_CLASS_P (type))
3817 return 0;
3819 /* Record the location of TYPE. */
3820 r = (*f) (type, offset, offsets);
3821 if (r)
3822 return r;
3824 /* Iterate through the direct base classes of TYPE. */
3825 if (!type_binfo)
3826 type_binfo = TYPE_BINFO (type);
3827 for (i = 0; BINFO_BASE_ITERATE (type_binfo, i, binfo); i++)
3829 tree binfo_offset;
3831 if (BINFO_VIRTUAL_P (binfo))
3832 continue;
3834 tree orig_binfo;
3835 /* We cannot rely on BINFO_OFFSET being set for the base
3836 class yet, but the offsets for direct non-virtual
3837 bases can be calculated by going back to the TYPE. */
3838 orig_binfo = BINFO_BASE_BINFO (TYPE_BINFO (type), i);
3839 binfo_offset = size_binop (PLUS_EXPR,
3840 offset,
3841 BINFO_OFFSET (orig_binfo));
3843 r = walk_subobject_offsets (binfo,
3845 binfo_offset,
3846 offsets,
3847 max_offset,
3848 /*vbases_p=*/0);
3849 if (r)
3850 return r;
3853 if (CLASSTYPE_VBASECLASSES (type))
3855 unsigned ix;
3856 vec<tree, va_gc> *vbases;
3858 /* Iterate through the virtual base classes of TYPE. In G++
3859 3.2, we included virtual bases in the direct base class
3860 loop above, which results in incorrect results; the
3861 correct offsets for virtual bases are only known when
3862 working with the most derived type. */
3863 if (vbases_p)
3864 for (vbases = CLASSTYPE_VBASECLASSES (type), ix = 0;
3865 vec_safe_iterate (vbases, ix, &binfo); ix++)
3867 r = walk_subobject_offsets (binfo,
3869 size_binop (PLUS_EXPR,
3870 offset,
3871 BINFO_OFFSET (binfo)),
3872 offsets,
3873 max_offset,
3874 /*vbases_p=*/0);
3875 if (r)
3876 return r;
3878 else
3880 /* We still have to walk the primary base, if it is
3881 virtual. (If it is non-virtual, then it was walked
3882 above.) */
3883 tree vbase = get_primary_binfo (type_binfo);
3885 if (vbase && BINFO_VIRTUAL_P (vbase)
3886 && BINFO_PRIMARY_P (vbase)
3887 && BINFO_INHERITANCE_CHAIN (vbase) == type_binfo)
3889 r = (walk_subobject_offsets
3890 (vbase, f, offset,
3891 offsets, max_offset, /*vbases_p=*/0));
3892 if (r)
3893 return r;
3898 /* Iterate through the fields of TYPE. */
3899 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
3900 if (TREE_CODE (field) == FIELD_DECL
3901 && TREE_TYPE (field) != error_mark_node
3902 && !DECL_ARTIFICIAL (field))
3904 tree field_offset;
3906 field_offset = byte_position (field);
3908 r = walk_subobject_offsets (TREE_TYPE (field),
3910 size_binop (PLUS_EXPR,
3911 offset,
3912 field_offset),
3913 offsets,
3914 max_offset,
3915 /*vbases_p=*/1);
3916 if (r)
3917 return r;
3920 else if (TREE_CODE (type) == ARRAY_TYPE)
3922 tree element_type = strip_array_types (type);
3923 tree domain = TYPE_DOMAIN (type);
3924 tree index;
3926 /* Avoid recursing into objects that are not interesting. */
3927 if (!CLASS_TYPE_P (element_type)
3928 || !CLASSTYPE_CONTAINS_EMPTY_CLASS_P (element_type)
3929 || !domain
3930 || integer_minus_onep (TYPE_MAX_VALUE (domain)))
3931 return 0;
3933 /* Step through each of the elements in the array. */
3934 for (index = size_zero_node;
3935 !tree_int_cst_lt (TYPE_MAX_VALUE (domain), index);
3936 index = size_binop (PLUS_EXPR, index, size_one_node))
3938 r = walk_subobject_offsets (TREE_TYPE (type),
3940 offset,
3941 offsets,
3942 max_offset,
3943 /*vbases_p=*/1);
3944 if (r)
3945 return r;
3946 offset = size_binop (PLUS_EXPR, offset,
3947 TYPE_SIZE_UNIT (TREE_TYPE (type)));
3948 /* If this new OFFSET is bigger than the MAX_OFFSET, then
3949 there's no point in iterating through the remaining
3950 elements of the array. */
3951 if (max_offset && tree_int_cst_lt (max_offset, offset))
3952 break;
3956 return 0;
3959 /* Record all of the empty subobjects of TYPE (either a type or a
3960 binfo). If IS_DATA_MEMBER is true, then a non-static data member
3961 is being placed at OFFSET; otherwise, it is a base class that is
3962 being placed at OFFSET. */
3964 static void
3965 record_subobject_offsets (tree type,
3966 tree offset,
3967 splay_tree offsets,
3968 bool is_data_member)
3970 tree max_offset;
3971 /* If recording subobjects for a non-static data member or a
3972 non-empty base class , we do not need to record offsets beyond
3973 the size of the biggest empty class. Additional data members
3974 will go at the end of the class. Additional base classes will go
3975 either at offset zero (if empty, in which case they cannot
3976 overlap with offsets past the size of the biggest empty class) or
3977 at the end of the class.
3979 However, if we are placing an empty base class, then we must record
3980 all offsets, as either the empty class is at offset zero (where
3981 other empty classes might later be placed) or at the end of the
3982 class (where other objects might then be placed, so other empty
3983 subobjects might later overlap). */
3984 if (is_data_member
3985 || !is_empty_class (BINFO_TYPE (type)))
3986 max_offset = sizeof_biggest_empty_class;
3987 else
3988 max_offset = NULL_TREE;
3989 walk_subobject_offsets (type, record_subobject_offset, offset,
3990 offsets, max_offset, is_data_member);
3993 /* Returns nonzero if any of the empty subobjects of TYPE (located at
3994 OFFSET) conflict with entries in OFFSETS. If VBASES_P is nonzero,
3995 virtual bases of TYPE are examined. */
3997 static int
3998 layout_conflict_p (tree type,
3999 tree offset,
4000 splay_tree offsets,
4001 int vbases_p)
4003 splay_tree_node max_node;
4005 /* Get the node in OFFSETS that indicates the maximum offset where
4006 an empty subobject is located. */
4007 max_node = splay_tree_max (offsets);
4008 /* If there aren't any empty subobjects, then there's no point in
4009 performing this check. */
4010 if (!max_node)
4011 return 0;
4013 return walk_subobject_offsets (type, check_subobject_offset, offset,
4014 offsets, (tree) (max_node->key),
4015 vbases_p);
4018 /* DECL is a FIELD_DECL corresponding either to a base subobject of a
4019 non-static data member of the type indicated by RLI. BINFO is the
4020 binfo corresponding to the base subobject, OFFSETS maps offsets to
4021 types already located at those offsets. This function determines
4022 the position of the DECL. */
4024 static void
4025 layout_nonempty_base_or_field (record_layout_info rli,
4026 tree decl,
4027 tree binfo,
4028 splay_tree offsets)
4030 tree offset = NULL_TREE;
4031 bool field_p;
4032 tree type;
4034 if (binfo)
4036 /* For the purposes of determining layout conflicts, we want to
4037 use the class type of BINFO; TREE_TYPE (DECL) will be the
4038 CLASSTYPE_AS_BASE version, which does not contain entries for
4039 zero-sized bases. */
4040 type = TREE_TYPE (binfo);
4041 field_p = false;
4043 else
4045 type = TREE_TYPE (decl);
4046 field_p = true;
4049 /* Try to place the field. It may take more than one try if we have
4050 a hard time placing the field without putting two objects of the
4051 same type at the same address. */
4052 while (1)
4054 struct record_layout_info_s old_rli = *rli;
4056 /* Place this field. */
4057 place_field (rli, decl);
4058 offset = byte_position (decl);
4060 /* We have to check to see whether or not there is already
4061 something of the same type at the offset we're about to use.
4062 For example, consider:
4064 struct S {};
4065 struct T : public S { int i; };
4066 struct U : public S, public T {};
4068 Here, we put S at offset zero in U. Then, we can't put T at
4069 offset zero -- its S component would be at the same address
4070 as the S we already allocated. So, we have to skip ahead.
4071 Since all data members, including those whose type is an
4072 empty class, have nonzero size, any overlap can happen only
4073 with a direct or indirect base-class -- it can't happen with
4074 a data member. */
4075 /* In a union, overlap is permitted; all members are placed at
4076 offset zero. */
4077 if (TREE_CODE (rli->t) == UNION_TYPE)
4078 break;
4079 if (layout_conflict_p (field_p ? type : binfo, offset,
4080 offsets, field_p))
4082 /* Strip off the size allocated to this field. That puts us
4083 at the first place we could have put the field with
4084 proper alignment. */
4085 *rli = old_rli;
4087 /* Bump up by the alignment required for the type. */
4088 rli->bitpos
4089 = size_binop (PLUS_EXPR, rli->bitpos,
4090 bitsize_int (binfo
4091 ? CLASSTYPE_ALIGN (type)
4092 : TYPE_ALIGN (type)));
4093 normalize_rli (rli);
4095 else if (TREE_CODE (type) == NULLPTR_TYPE
4096 && warn_abi && abi_version_crosses (9))
4098 /* Before ABI v9, we were giving nullptr_t alignment of 1; if
4099 the offset wasn't aligned like a pointer when we started to
4100 layout this field, that affects its position. */
4101 tree pos = rli_size_unit_so_far (&old_rli);
4102 if (int_cst_value (pos) % TYPE_ALIGN_UNIT (ptr_type_node) != 0)
4104 if (abi_version_at_least (9))
4105 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wabi,
4106 "alignment of %qD increased in -fabi-version=9 "
4107 "(GCC 5.2)", decl);
4108 else
4109 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wabi, "alignment "
4110 "of %qD will increase in -fabi-version=9", decl);
4112 break;
4114 else
4115 /* There was no conflict. We're done laying out this field. */
4116 break;
4119 /* Now that we know where it will be placed, update its
4120 BINFO_OFFSET. */
4121 if (binfo && CLASS_TYPE_P (BINFO_TYPE (binfo)))
4122 /* Indirect virtual bases may have a nonzero BINFO_OFFSET at
4123 this point because their BINFO_OFFSET is copied from another
4124 hierarchy. Therefore, we may not need to add the entire
4125 OFFSET. */
4126 propagate_binfo_offsets (binfo,
4127 size_diffop_loc (input_location,
4128 fold_convert (ssizetype, offset),
4129 fold_convert (ssizetype,
4130 BINFO_OFFSET (binfo))));
4133 /* Returns true if TYPE is empty and OFFSET is nonzero. */
4135 static int
4136 empty_base_at_nonzero_offset_p (tree type,
4137 tree offset,
4138 splay_tree /*offsets*/)
4140 return is_empty_class (type) && !integer_zerop (offset);
4143 /* Layout the empty base BINFO. EOC indicates the byte currently just
4144 past the end of the class, and should be correctly aligned for a
4145 class of the type indicated by BINFO; OFFSETS gives the offsets of
4146 the empty bases allocated so far. T is the most derived
4147 type. Return nonzero iff we added it at the end. */
4149 static bool
4150 layout_empty_base (record_layout_info rli, tree binfo,
4151 tree eoc, splay_tree offsets)
4153 tree alignment;
4154 tree basetype = BINFO_TYPE (binfo);
4155 bool atend = false;
4157 /* This routine should only be used for empty classes. */
4158 gcc_assert (is_empty_class (basetype));
4159 alignment = ssize_int (CLASSTYPE_ALIGN_UNIT (basetype));
4161 if (!integer_zerop (BINFO_OFFSET (binfo)))
4162 propagate_binfo_offsets
4163 (binfo, size_diffop_loc (input_location,
4164 size_zero_node, BINFO_OFFSET (binfo)));
4166 /* This is an empty base class. We first try to put it at offset
4167 zero. */
4168 if (layout_conflict_p (binfo,
4169 BINFO_OFFSET (binfo),
4170 offsets,
4171 /*vbases_p=*/0))
4173 /* That didn't work. Now, we move forward from the next
4174 available spot in the class. */
4175 atend = true;
4176 propagate_binfo_offsets (binfo, fold_convert (ssizetype, eoc));
4177 while (1)
4179 if (!layout_conflict_p (binfo,
4180 BINFO_OFFSET (binfo),
4181 offsets,
4182 /*vbases_p=*/0))
4183 /* We finally found a spot where there's no overlap. */
4184 break;
4186 /* There's overlap here, too. Bump along to the next spot. */
4187 propagate_binfo_offsets (binfo, alignment);
4191 if (CLASSTYPE_USER_ALIGN (basetype))
4193 rli->record_align = MAX (rli->record_align, CLASSTYPE_ALIGN (basetype));
4194 if (warn_packed)
4195 rli->unpacked_align = MAX (rli->unpacked_align, CLASSTYPE_ALIGN (basetype));
4196 TYPE_USER_ALIGN (rli->t) = 1;
4199 return atend;
4202 /* Build the FIELD_DECL for BASETYPE as a base of T, add it to the chain of
4203 fields at NEXT_FIELD, and return it. */
4205 static tree
4206 build_base_field_1 (tree t, tree basetype, tree *&next_field)
4208 /* Create the FIELD_DECL. */
4209 gcc_assert (CLASSTYPE_AS_BASE (basetype));
4210 tree decl = build_decl (input_location,
4211 FIELD_DECL, NULL_TREE, CLASSTYPE_AS_BASE (basetype));
4212 DECL_ARTIFICIAL (decl) = 1;
4213 DECL_IGNORED_P (decl) = 1;
4214 DECL_FIELD_CONTEXT (decl) = t;
4215 if (is_empty_class (basetype))
4216 /* CLASSTYPE_SIZE is one byte, but the field needs to have size zero. */
4217 DECL_SIZE (decl) = DECL_SIZE_UNIT (decl) = size_zero_node;
4218 else
4220 DECL_SIZE (decl) = CLASSTYPE_SIZE (basetype);
4221 DECL_SIZE_UNIT (decl) = CLASSTYPE_SIZE_UNIT (basetype);
4223 SET_DECL_ALIGN (decl, CLASSTYPE_ALIGN (basetype));
4224 DECL_USER_ALIGN (decl) = CLASSTYPE_USER_ALIGN (basetype);
4225 SET_DECL_MODE (decl, TYPE_MODE (basetype));
4226 DECL_FIELD_IS_BASE (decl) = 1;
4228 /* Add the new FIELD_DECL to the list of fields for T. */
4229 DECL_CHAIN (decl) = *next_field;
4230 *next_field = decl;
4231 next_field = &DECL_CHAIN (decl);
4233 return decl;
4236 /* Layout the base given by BINFO in the class indicated by RLI.
4237 *BASE_ALIGN is a running maximum of the alignments of
4238 any base class. OFFSETS gives the location of empty base
4239 subobjects. T is the most derived type. Return nonzero if the new
4240 object cannot be nearly-empty. A new FIELD_DECL is inserted at
4241 *NEXT_FIELD, unless BINFO is for an empty base class.
4243 Returns the location at which the next field should be inserted. */
4245 static tree *
4246 build_base_field (record_layout_info rli, tree binfo,
4247 splay_tree offsets, tree *next_field)
4249 tree t = rli->t;
4250 tree basetype = BINFO_TYPE (binfo);
4252 if (!COMPLETE_TYPE_P (basetype))
4253 /* This error is now reported in xref_tag, thus giving better
4254 location information. */
4255 return next_field;
4257 /* Place the base class. */
4258 if (!is_empty_class (basetype))
4260 tree decl;
4262 /* The containing class is non-empty because it has a non-empty
4263 base class. */
4264 CLASSTYPE_EMPTY_P (t) = 0;
4266 /* Create the FIELD_DECL. */
4267 decl = build_base_field_1 (t, basetype, next_field);
4269 /* Try to place the field. It may take more than one try if we
4270 have a hard time placing the field without putting two
4271 objects of the same type at the same address. */
4272 layout_nonempty_base_or_field (rli, decl, binfo, offsets);
4274 else
4276 tree eoc;
4277 bool atend;
4279 /* On some platforms (ARM), even empty classes will not be
4280 byte-aligned. */
4281 eoc = round_up_loc (input_location,
4282 rli_size_unit_so_far (rli),
4283 CLASSTYPE_ALIGN_UNIT (basetype));
4284 atend = layout_empty_base (rli, binfo, eoc, offsets);
4285 /* A nearly-empty class "has no proper base class that is empty,
4286 not morally virtual, and at an offset other than zero." */
4287 if (!BINFO_VIRTUAL_P (binfo) && CLASSTYPE_NEARLY_EMPTY_P (t))
4289 if (atend)
4290 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
4291 /* The check above (used in G++ 3.2) is insufficient because
4292 an empty class placed at offset zero might itself have an
4293 empty base at a nonzero offset. */
4294 else if (walk_subobject_offsets (basetype,
4295 empty_base_at_nonzero_offset_p,
4296 size_zero_node,
4297 /*offsets=*/NULL,
4298 /*max_offset=*/NULL_TREE,
4299 /*vbases_p=*/true))
4300 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
4303 /* We used to not create a FIELD_DECL for empty base classes because of
4304 back end issues with overlapping FIELD_DECLs, but that doesn't seem to
4305 be a problem anymore. We need them to handle initialization of C++17
4306 aggregate bases. */
4307 if (cxx_dialect >= cxx17 && !BINFO_VIRTUAL_P (binfo))
4309 tree decl = build_base_field_1 (t, basetype, next_field);
4310 DECL_FIELD_OFFSET (decl) = BINFO_OFFSET (binfo);
4311 DECL_FIELD_BIT_OFFSET (decl) = bitsize_zero_node;
4312 SET_DECL_OFFSET_ALIGN (decl, BITS_PER_UNIT);
4315 /* An empty virtual base causes a class to be non-empty
4316 -- but in that case we do not need to clear CLASSTYPE_EMPTY_P
4317 here because that was already done when the virtual table
4318 pointer was created. */
4321 /* Record the offsets of BINFO and its base subobjects. */
4322 record_subobject_offsets (binfo,
4323 BINFO_OFFSET (binfo),
4324 offsets,
4325 /*is_data_member=*/false);
4327 return next_field;
4330 /* Layout all of the non-virtual base classes. Record empty
4331 subobjects in OFFSETS. T is the most derived type. Return nonzero
4332 if the type cannot be nearly empty. The fields created
4333 corresponding to the base classes will be inserted at
4334 *NEXT_FIELD. */
4336 static void
4337 build_base_fields (record_layout_info rli,
4338 splay_tree offsets, tree *next_field)
4340 /* Chain to hold all the new FIELD_DECLs which stand in for base class
4341 subobjects. */
4342 tree t = rli->t;
4343 int n_baseclasses = BINFO_N_BASE_BINFOS (TYPE_BINFO (t));
4344 int i;
4346 /* The primary base class is always allocated first. */
4347 if (CLASSTYPE_HAS_PRIMARY_BASE_P (t))
4348 next_field = build_base_field (rli, CLASSTYPE_PRIMARY_BINFO (t),
4349 offsets, next_field);
4351 /* Now allocate the rest of the bases. */
4352 for (i = 0; i < n_baseclasses; ++i)
4354 tree base_binfo;
4356 base_binfo = BINFO_BASE_BINFO (TYPE_BINFO (t), i);
4358 /* The primary base was already allocated above, so we don't
4359 need to allocate it again here. */
4360 if (base_binfo == CLASSTYPE_PRIMARY_BINFO (t))
4361 continue;
4363 /* Virtual bases are added at the end (a primary virtual base
4364 will have already been added). */
4365 if (BINFO_VIRTUAL_P (base_binfo))
4366 continue;
4368 next_field = build_base_field (rli, base_binfo,
4369 offsets, next_field);
4373 /* Go through the TYPE_FIELDS of T issuing any appropriate
4374 diagnostics, figuring out which methods override which other
4375 methods, and so forth. */
4377 static void
4378 check_methods (tree t)
4380 for (tree x = TYPE_FIELDS (t); x; x = DECL_CHAIN (x))
4381 if (DECL_DECLARES_FUNCTION_P (x))
4383 check_for_override (x, t);
4385 if (DECL_PURE_VIRTUAL_P (x)
4386 && (TREE_CODE (x) != FUNCTION_DECL || ! DECL_VINDEX (x)))
4387 error ("initializer specified for non-virtual method %q+D", x);
4388 /* The name of the field is the original field name
4389 Save this in auxiliary field for later overloading. */
4390 if (TREE_CODE (x) == FUNCTION_DECL && DECL_VINDEX (x))
4392 TYPE_POLYMORPHIC_P (t) = 1;
4393 if (DECL_PURE_VIRTUAL_P (x))
4394 vec_safe_push (CLASSTYPE_PURE_VIRTUALS (t), x);
4397 /* All user-provided destructors are non-trivial.
4398 Constructors and assignment ops are handled in
4399 grok_special_member_properties. */
4400 if (DECL_DESTRUCTOR_P (x) && user_provided_p (x))
4401 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t) = 1;
4402 if (!DECL_VIRTUAL_P (x)
4403 && lookup_attribute ("transaction_safe_dynamic",
4404 DECL_ATTRIBUTES (x)))
4405 error_at (DECL_SOURCE_LOCATION (x),
4406 "%<transaction_safe_dynamic%> may only be specified for "
4407 "a virtual function");
4411 /* FN is a constructor or destructor. Clone the declaration to create
4412 a specialized in-charge or not-in-charge version, as indicated by
4413 NAME. */
4415 static tree
4416 build_clone (tree fn, tree name)
4418 tree parms;
4419 tree clone;
4421 /* Copy the function. */
4422 clone = copy_decl (fn);
4423 /* Reset the function name. */
4424 DECL_NAME (clone) = name;
4425 /* Remember where this function came from. */
4426 DECL_ABSTRACT_ORIGIN (clone) = fn;
4427 /* Make it easy to find the CLONE given the FN. */
4428 DECL_CHAIN (clone) = DECL_CHAIN (fn);
4429 DECL_CHAIN (fn) = clone;
4431 /* If this is a template, do the rest on the DECL_TEMPLATE_RESULT. */
4432 if (TREE_CODE (clone) == TEMPLATE_DECL)
4434 tree result = build_clone (DECL_TEMPLATE_RESULT (clone), name);
4435 DECL_TEMPLATE_RESULT (clone) = result;
4436 DECL_TEMPLATE_INFO (result) = copy_node (DECL_TEMPLATE_INFO (result));
4437 DECL_TI_TEMPLATE (result) = clone;
4438 TREE_TYPE (clone) = TREE_TYPE (result);
4439 return clone;
4441 else
4443 // Clone constraints.
4444 if (flag_concepts)
4445 if (tree ci = get_constraints (fn))
4446 set_constraints (clone, copy_node (ci));
4450 SET_DECL_ASSEMBLER_NAME (clone, NULL_TREE);
4451 DECL_CLONED_FUNCTION (clone) = fn;
4452 /* There's no pending inline data for this function. */
4453 DECL_PENDING_INLINE_INFO (clone) = NULL;
4454 DECL_PENDING_INLINE_P (clone) = 0;
4456 /* The base-class destructor is not virtual. */
4457 if (name == base_dtor_identifier)
4459 DECL_VIRTUAL_P (clone) = 0;
4460 if (TREE_CODE (clone) != TEMPLATE_DECL)
4461 DECL_VINDEX (clone) = NULL_TREE;
4464 bool ctor_omit_inherited_parms_p = ctor_omit_inherited_parms (clone);
4465 if (ctor_omit_inherited_parms_p)
4466 gcc_assert (DECL_HAS_IN_CHARGE_PARM_P (clone));
4468 /* If there was an in-charge parameter, drop it from the function
4469 type. */
4470 if (DECL_HAS_IN_CHARGE_PARM_P (clone))
4472 tree basetype = TYPE_METHOD_BASETYPE (TREE_TYPE (clone));
4473 tree parmtypes = TYPE_ARG_TYPES (TREE_TYPE (clone));
4474 /* Skip the `this' parameter. */
4475 parmtypes = TREE_CHAIN (parmtypes);
4476 /* Skip the in-charge parameter. */
4477 parmtypes = TREE_CHAIN (parmtypes);
4478 /* And the VTT parm, in a complete [cd]tor. */
4479 if (DECL_HAS_VTT_PARM_P (fn)
4480 && ! DECL_NEEDS_VTT_PARM_P (clone))
4481 parmtypes = TREE_CHAIN (parmtypes);
4482 if (ctor_omit_inherited_parms_p)
4484 /* If we're omitting inherited parms, that just leaves the VTT. */
4485 gcc_assert (DECL_NEEDS_VTT_PARM_P (clone));
4486 parmtypes = tree_cons (NULL_TREE, vtt_parm_type, void_list_node);
4488 TREE_TYPE (clone)
4489 = build_method_type_directly (basetype,
4490 TREE_TYPE (TREE_TYPE (clone)),
4491 parmtypes);
4492 TREE_TYPE (clone)
4493 = cp_build_type_attribute_variant (TREE_TYPE (clone),
4494 TYPE_ATTRIBUTES (TREE_TYPE (fn)));
4495 TREE_TYPE (clone)
4496 = cxx_copy_lang_qualifiers (TREE_TYPE (clone), TREE_TYPE (fn));
4499 /* Copy the function parameters. */
4500 DECL_ARGUMENTS (clone) = copy_list (DECL_ARGUMENTS (clone));
4501 /* Remove the in-charge parameter. */
4502 if (DECL_HAS_IN_CHARGE_PARM_P (clone))
4504 DECL_CHAIN (DECL_ARGUMENTS (clone))
4505 = DECL_CHAIN (DECL_CHAIN (DECL_ARGUMENTS (clone)));
4506 DECL_HAS_IN_CHARGE_PARM_P (clone) = 0;
4508 /* And the VTT parm, in a complete [cd]tor. */
4509 if (DECL_HAS_VTT_PARM_P (fn))
4511 if (DECL_NEEDS_VTT_PARM_P (clone))
4512 DECL_HAS_VTT_PARM_P (clone) = 1;
4513 else
4515 DECL_CHAIN (DECL_ARGUMENTS (clone))
4516 = DECL_CHAIN (DECL_CHAIN (DECL_ARGUMENTS (clone)));
4517 DECL_HAS_VTT_PARM_P (clone) = 0;
4521 /* A base constructor inheriting from a virtual base doesn't get the
4522 arguments. */
4523 if (ctor_omit_inherited_parms_p)
4524 DECL_CHAIN (DECL_CHAIN (DECL_ARGUMENTS (clone))) = NULL_TREE;
4526 for (parms = DECL_ARGUMENTS (clone); parms; parms = DECL_CHAIN (parms))
4528 DECL_CONTEXT (parms) = clone;
4529 cxx_dup_lang_specific_decl (parms);
4532 /* Create the RTL for this function. */
4533 SET_DECL_RTL (clone, NULL);
4534 rest_of_decl_compilation (clone, /*top_level=*/1, at_eof);
4536 return clone;
4539 /* Implementation of DECL_CLONED_FUNCTION and DECL_CLONED_FUNCTION_P, do
4540 not invoke this function directly.
4542 For a non-thunk function, returns the address of the slot for storing
4543 the function it is a clone of. Otherwise returns NULL_TREE.
4545 If JUST_TESTING, looks through TEMPLATE_DECL and returns NULL if
4546 cloned_function is unset. This is to support the separate
4547 DECL_CLONED_FUNCTION and DECL_CLONED_FUNCTION_P modes; using the latter
4548 on a template makes sense, but not the former. */
4550 tree *
4551 decl_cloned_function_p (const_tree decl, bool just_testing)
4553 tree *ptr;
4554 if (just_testing)
4555 decl = STRIP_TEMPLATE (decl);
4557 if (TREE_CODE (decl) != FUNCTION_DECL
4558 || !DECL_LANG_SPECIFIC (decl)
4559 || DECL_LANG_SPECIFIC (decl)->u.fn.thunk_p)
4561 #if defined ENABLE_TREE_CHECKING && (GCC_VERSION >= 2007)
4562 if (!just_testing)
4563 lang_check_failed (__FILE__, __LINE__, __FUNCTION__);
4564 else
4565 #endif
4566 return NULL;
4569 ptr = &DECL_LANG_SPECIFIC (decl)->u.fn.u5.cloned_function;
4570 if (just_testing && *ptr == NULL_TREE)
4571 return NULL;
4572 else
4573 return ptr;
4576 /* Produce declarations for all appropriate clones of FN. If
4577 UPDATE_METHODS is true, the clones are added to the
4578 CLASSTYPE_MEMBER_VEC. */
4580 void
4581 clone_function_decl (tree fn, bool update_methods)
4583 tree clone;
4585 /* Avoid inappropriate cloning. */
4586 if (DECL_CHAIN (fn)
4587 && DECL_CLONED_FUNCTION_P (DECL_CHAIN (fn)))
4588 return;
4590 if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (fn))
4592 /* For each constructor, we need two variants: an in-charge version
4593 and a not-in-charge version. */
4594 clone = build_clone (fn, complete_ctor_identifier);
4595 if (update_methods)
4596 add_method (DECL_CONTEXT (clone), clone, false);
4597 clone = build_clone (fn, base_ctor_identifier);
4598 if (update_methods)
4599 add_method (DECL_CONTEXT (clone), clone, false);
4601 else
4603 gcc_assert (DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (fn));
4605 /* For each destructor, we need three variants: an in-charge
4606 version, a not-in-charge version, and an in-charge deleting
4607 version. We clone the deleting version first because that
4608 means it will go second on the TYPE_FIELDS list -- and that
4609 corresponds to the correct layout order in the virtual
4610 function table.
4612 For a non-virtual destructor, we do not build a deleting
4613 destructor. */
4614 if (DECL_VIRTUAL_P (fn))
4616 clone = build_clone (fn, deleting_dtor_identifier);
4617 if (update_methods)
4618 add_method (DECL_CONTEXT (clone), clone, false);
4620 clone = build_clone (fn, complete_dtor_identifier);
4621 if (update_methods)
4622 add_method (DECL_CONTEXT (clone), clone, false);
4623 clone = build_clone (fn, base_dtor_identifier);
4624 if (update_methods)
4625 add_method (DECL_CONTEXT (clone), clone, false);
4628 /* Note that this is an abstract function that is never emitted. */
4629 DECL_ABSTRACT_P (fn) = true;
4632 /* DECL is an in charge constructor, which is being defined. This will
4633 have had an in class declaration, from whence clones were
4634 declared. An out-of-class definition can specify additional default
4635 arguments. As it is the clones that are involved in overload
4636 resolution, we must propagate the information from the DECL to its
4637 clones. */
4639 void
4640 adjust_clone_args (tree decl)
4642 tree clone;
4644 for (clone = DECL_CHAIN (decl); clone && DECL_CLONED_FUNCTION_P (clone);
4645 clone = DECL_CHAIN (clone))
4647 tree orig_clone_parms = TYPE_ARG_TYPES (TREE_TYPE (clone));
4648 tree orig_decl_parms = TYPE_ARG_TYPES (TREE_TYPE (decl));
4649 tree decl_parms, clone_parms;
4651 clone_parms = orig_clone_parms;
4653 /* Skip the 'this' parameter. */
4654 orig_clone_parms = TREE_CHAIN (orig_clone_parms);
4655 orig_decl_parms = TREE_CHAIN (orig_decl_parms);
4657 if (DECL_HAS_IN_CHARGE_PARM_P (decl))
4658 orig_decl_parms = TREE_CHAIN (orig_decl_parms);
4659 if (DECL_HAS_VTT_PARM_P (decl))
4660 orig_decl_parms = TREE_CHAIN (orig_decl_parms);
4662 clone_parms = orig_clone_parms;
4663 if (DECL_HAS_VTT_PARM_P (clone))
4664 clone_parms = TREE_CHAIN (clone_parms);
4666 for (decl_parms = orig_decl_parms; decl_parms;
4667 decl_parms = TREE_CHAIN (decl_parms),
4668 clone_parms = TREE_CHAIN (clone_parms))
4670 if (clone_parms == void_list_node)
4672 gcc_assert (decl_parms == clone_parms
4673 || ctor_omit_inherited_parms (clone));
4674 break;
4677 gcc_assert (same_type_p (TREE_TYPE (decl_parms),
4678 TREE_TYPE (clone_parms)));
4680 if (TREE_PURPOSE (decl_parms) && !TREE_PURPOSE (clone_parms))
4682 /* A default parameter has been added. Adjust the
4683 clone's parameters. */
4684 clone_parms = orig_decl_parms;
4686 if (DECL_HAS_VTT_PARM_P (clone))
4688 clone_parms = tree_cons (TREE_PURPOSE (orig_clone_parms),
4689 TREE_VALUE (orig_clone_parms),
4690 clone_parms);
4691 TREE_TYPE (clone_parms) = TREE_TYPE (orig_clone_parms);
4694 tree basetype = TYPE_METHOD_BASETYPE (TREE_TYPE (clone));
4695 tree type
4696 = build_method_type_directly (basetype,
4697 TREE_TYPE (TREE_TYPE (clone)),
4698 clone_parms);
4699 if (tree attrs = TYPE_ATTRIBUTES (TREE_TYPE (clone)))
4700 type = cp_build_type_attribute_variant (type, attrs);
4701 type = cxx_copy_lang_qualifiers (type, TREE_TYPE (clone));
4702 TREE_TYPE (clone) = type;
4704 clone_parms = NULL_TREE;
4705 break;
4708 gcc_assert (!clone_parms || clone_parms == void_list_node);
4712 /* For each of the constructors and destructors in T, create an
4713 in-charge and not-in-charge variant. */
4715 static void
4716 clone_constructors_and_destructors (tree t)
4718 /* While constructors can be via a using declaration, at this point
4719 we no longer need to know that. */
4720 for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (t)); iter; ++iter)
4721 clone_function_decl (*iter, /*update_methods=*/true);
4723 if (tree dtor = CLASSTYPE_DESTRUCTOR (t))
4724 clone_function_decl (dtor, /*update_methods=*/true);
4727 /* Deduce noexcept for a destructor DTOR. */
4729 void
4730 deduce_noexcept_on_destructor (tree dtor)
4732 if (!TYPE_RAISES_EXCEPTIONS (TREE_TYPE (dtor)))
4733 TREE_TYPE (dtor) = build_exception_variant (TREE_TYPE (dtor),
4734 noexcept_deferred_spec);
4737 /* Subroutine of set_one_vmethod_tm_attributes. Search base classes
4738 of TYPE for virtual functions which FNDECL overrides. Return a
4739 mask of the tm attributes found therein. */
4741 static int
4742 look_for_tm_attr_overrides (tree type, tree fndecl)
4744 tree binfo = TYPE_BINFO (type);
4745 tree base_binfo;
4746 int ix, found = 0;
4748 for (ix = 0; BINFO_BASE_ITERATE (binfo, ix, base_binfo); ++ix)
4750 tree o, basetype = BINFO_TYPE (base_binfo);
4752 if (!TYPE_POLYMORPHIC_P (basetype))
4753 continue;
4755 o = look_for_overrides_here (basetype, fndecl);
4756 if (o)
4758 if (lookup_attribute ("transaction_safe_dynamic",
4759 DECL_ATTRIBUTES (o)))
4760 /* transaction_safe_dynamic is not inherited. */;
4761 else
4762 found |= tm_attr_to_mask (find_tm_attribute
4763 (TYPE_ATTRIBUTES (TREE_TYPE (o))));
4765 else
4766 found |= look_for_tm_attr_overrides (basetype, fndecl);
4769 return found;
4772 /* Subroutine of set_method_tm_attributes. Handle the checks and
4773 inheritance for one virtual method FNDECL. */
4775 static void
4776 set_one_vmethod_tm_attributes (tree type, tree fndecl)
4778 tree tm_attr;
4779 int found, have;
4781 found = look_for_tm_attr_overrides (type, fndecl);
4783 /* If FNDECL doesn't actually override anything (i.e. T is the
4784 class that first declares FNDECL virtual), then we're done. */
4785 if (found == 0)
4786 return;
4788 tm_attr = find_tm_attribute (TYPE_ATTRIBUTES (TREE_TYPE (fndecl)));
4789 have = tm_attr_to_mask (tm_attr);
4791 /* Intel STM Language Extension 3.0, Section 4.2 table 4:
4792 tm_pure must match exactly, otherwise no weakening of
4793 tm_safe > tm_callable > nothing. */
4794 /* ??? The tm_pure attribute didn't make the transition to the
4795 multivendor language spec. */
4796 if (have == TM_ATTR_PURE)
4798 if (found != TM_ATTR_PURE)
4800 found &= -found;
4801 goto err_override;
4804 /* If the overridden function is tm_pure, then FNDECL must be. */
4805 else if (found == TM_ATTR_PURE && tm_attr)
4806 goto err_override;
4807 /* Look for base class combinations that cannot be satisfied. */
4808 else if (found != TM_ATTR_PURE && (found & TM_ATTR_PURE))
4810 found &= ~TM_ATTR_PURE;
4811 found &= -found;
4812 error_at (DECL_SOURCE_LOCATION (fndecl),
4813 "method overrides both %<transaction_pure%> and %qE methods",
4814 tm_mask_to_attr (found));
4816 /* If FNDECL did not declare an attribute, then inherit the most
4817 restrictive one. */
4818 else if (tm_attr == NULL)
4820 apply_tm_attr (fndecl, tm_mask_to_attr (least_bit_hwi (found)));
4822 /* Otherwise validate that we're not weaker than a function
4823 that is being overridden. */
4824 else
4826 found &= -found;
4827 if (found <= TM_ATTR_CALLABLE && have > found)
4828 goto err_override;
4830 return;
4832 err_override:
4833 error_at (DECL_SOURCE_LOCATION (fndecl),
4834 "method declared %qE overriding %qE method",
4835 tm_attr, tm_mask_to_attr (found));
4838 /* For each of the methods in T, propagate a class-level tm attribute. */
4840 static void
4841 set_method_tm_attributes (tree t)
4843 tree class_tm_attr, fndecl;
4845 /* Don't bother collecting tm attributes if transactional memory
4846 support is not enabled. */
4847 if (!flag_tm)
4848 return;
4850 /* Process virtual methods first, as they inherit directly from the
4851 base virtual function and also require validation of new attributes. */
4852 if (TYPE_CONTAINS_VPTR_P (t))
4854 tree vchain;
4855 for (vchain = BINFO_VIRTUALS (TYPE_BINFO (t)); vchain;
4856 vchain = TREE_CHAIN (vchain))
4858 fndecl = BV_FN (vchain);
4859 if (DECL_THUNK_P (fndecl))
4860 fndecl = THUNK_TARGET (fndecl);
4861 set_one_vmethod_tm_attributes (t, fndecl);
4865 /* If the class doesn't have an attribute, nothing more to do. */
4866 class_tm_attr = find_tm_attribute (TYPE_ATTRIBUTES (t));
4867 if (class_tm_attr == NULL)
4868 return;
4870 /* Any method that does not yet have a tm attribute inherits
4871 the one from the class. */
4872 for (fndecl = TYPE_FIELDS (t); fndecl; fndecl = DECL_CHAIN (fndecl))
4873 if (DECL_DECLARES_FUNCTION_P (fndecl)
4874 && !find_tm_attribute (TYPE_ATTRIBUTES (TREE_TYPE (fndecl))))
4875 apply_tm_attr (fndecl, class_tm_attr);
4878 /* Returns true if FN is a default constructor. */
4880 bool
4881 default_ctor_p (tree fn)
4883 return (DECL_CONSTRUCTOR_P (fn)
4884 && sufficient_parms_p (FUNCTION_FIRST_USER_PARMTYPE (fn)));
4887 /* Returns true iff class T has a user-provided constructor that can be called
4888 with more than zero arguments. */
4890 bool
4891 type_has_user_nondefault_constructor (tree t)
4893 if (!TYPE_HAS_USER_CONSTRUCTOR (t))
4894 return false;
4896 for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (t)); iter; ++iter)
4898 tree fn = *iter;
4899 if (user_provided_p (fn)
4900 && (TREE_CODE (fn) == TEMPLATE_DECL
4901 || (skip_artificial_parms_for (fn, DECL_ARGUMENTS (fn))
4902 != NULL_TREE)))
4903 return true;
4906 return false;
4909 /* Returns the defaulted constructor if T has one. Otherwise, returns
4910 NULL_TREE. */
4912 tree
4913 in_class_defaulted_default_constructor (tree t)
4915 if (!TYPE_HAS_USER_CONSTRUCTOR (t))
4916 return NULL_TREE;
4918 for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (t)); iter; ++iter)
4920 tree fn = *iter;
4922 if (DECL_DEFAULTED_IN_CLASS_P (fn)
4923 && default_ctor_p (fn))
4924 return fn;
4927 return NULL_TREE;
4930 /* Returns true iff FN is a user-provided function, i.e. user-declared
4931 and not defaulted at its first declaration. */
4933 bool
4934 user_provided_p (tree fn)
4936 if (TREE_CODE (fn) == TEMPLATE_DECL)
4937 return true;
4938 else
4939 return (!DECL_ARTIFICIAL (fn)
4940 && !(DECL_INITIALIZED_IN_CLASS_P (fn)
4941 && (DECL_DEFAULTED_FN (fn) || DECL_DELETED_FN (fn))));
4944 /* Returns true iff class T has a user-provided constructor. */
4946 bool
4947 type_has_user_provided_constructor (tree t)
4949 if (!CLASS_TYPE_P (t))
4950 return false;
4952 if (!TYPE_HAS_USER_CONSTRUCTOR (t))
4953 return false;
4955 for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (t)); iter; ++iter)
4956 if (user_provided_p (*iter))
4957 return true;
4959 return false;
4962 /* Returns true iff class T has a user-provided or explicit constructor. */
4964 bool
4965 type_has_user_provided_or_explicit_constructor (tree t)
4967 if (!CLASS_TYPE_P (t))
4968 return false;
4970 if (!TYPE_HAS_USER_CONSTRUCTOR (t))
4971 return false;
4973 for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (t)); iter; ++iter)
4975 tree fn = *iter;
4976 if (user_provided_p (fn) || DECL_NONCONVERTING_P (fn))
4977 return true;
4980 return false;
4983 /* Returns true iff class T has a non-user-provided (i.e. implicitly
4984 declared or explicitly defaulted in the class body) default
4985 constructor. */
4987 bool
4988 type_has_non_user_provided_default_constructor (tree t)
4990 if (!TYPE_HAS_DEFAULT_CONSTRUCTOR (t))
4991 return false;
4992 if (CLASSTYPE_LAZY_DEFAULT_CTOR (t))
4993 return true;
4995 for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (t)); iter; ++iter)
4997 tree fn = *iter;
4998 if (TREE_CODE (fn) == FUNCTION_DECL
4999 && default_ctor_p (fn)
5000 && !user_provided_p (fn))
5001 return true;
5004 return false;
5007 /* TYPE is being used as a virtual base, and has a non-trivial move
5008 assignment. Return true if this is due to there being a user-provided
5009 move assignment in TYPE or one of its subobjects; if there isn't, then
5010 multiple move assignment can't cause any harm. */
5012 bool
5013 vbase_has_user_provided_move_assign (tree type)
5015 /* Does the type itself have a user-provided move assignment operator? */
5016 if (!CLASSTYPE_LAZY_MOVE_ASSIGN (type))
5017 for (ovl_iterator iter (get_class_binding_direct
5018 (type, assign_op_identifier));
5019 iter; ++iter)
5020 if (user_provided_p (*iter) && move_fn_p (*iter))
5021 return true;
5023 /* Do any of its bases? */
5024 tree binfo = TYPE_BINFO (type);
5025 tree base_binfo;
5026 for (int i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
5027 if (vbase_has_user_provided_move_assign (BINFO_TYPE (base_binfo)))
5028 return true;
5030 /* Or non-static data members? */
5031 for (tree field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
5033 if (TREE_CODE (field) == FIELD_DECL
5034 && CLASS_TYPE_P (TREE_TYPE (field))
5035 && vbase_has_user_provided_move_assign (TREE_TYPE (field)))
5036 return true;
5039 /* Seems not. */
5040 return false;
5043 /* If default-initialization leaves part of TYPE uninitialized, returns
5044 a DECL for the field or TYPE itself (DR 253). */
5046 tree
5047 default_init_uninitialized_part (tree type)
5049 tree t, r, binfo;
5050 int i;
5052 type = strip_array_types (type);
5053 if (!CLASS_TYPE_P (type))
5054 return type;
5055 if (!type_has_non_user_provided_default_constructor (type))
5056 return NULL_TREE;
5057 for (binfo = TYPE_BINFO (type), i = 0;
5058 BINFO_BASE_ITERATE (binfo, i, t); ++i)
5060 r = default_init_uninitialized_part (BINFO_TYPE (t));
5061 if (r)
5062 return r;
5064 for (t = TYPE_FIELDS (type); t; t = DECL_CHAIN (t))
5065 if (TREE_CODE (t) == FIELD_DECL
5066 && !DECL_ARTIFICIAL (t)
5067 && !DECL_INITIAL (t))
5069 r = default_init_uninitialized_part (TREE_TYPE (t));
5070 if (r)
5071 return DECL_P (r) ? r : t;
5074 return NULL_TREE;
5077 /* Returns true iff for class T, a trivial synthesized default constructor
5078 would be constexpr. */
5080 bool
5081 trivial_default_constructor_is_constexpr (tree t)
5083 /* A defaulted trivial default constructor is constexpr
5084 if there is nothing to initialize. */
5085 gcc_assert (!TYPE_HAS_COMPLEX_DFLT (t));
5086 return is_really_empty_class (t);
5089 /* Returns true iff class T has a constexpr default constructor. */
5091 bool
5092 type_has_constexpr_default_constructor (tree t)
5094 tree fns;
5096 if (!CLASS_TYPE_P (t))
5098 /* The caller should have stripped an enclosing array. */
5099 gcc_assert (TREE_CODE (t) != ARRAY_TYPE);
5100 return false;
5102 if (CLASSTYPE_LAZY_DEFAULT_CTOR (t))
5104 if (!TYPE_HAS_COMPLEX_DFLT (t))
5105 return trivial_default_constructor_is_constexpr (t);
5106 /* Non-trivial, we need to check subobject constructors. */
5107 lazily_declare_fn (sfk_constructor, t);
5109 fns = locate_ctor (t);
5110 return (fns && DECL_DECLARED_CONSTEXPR_P (fns));
5113 /* Returns true iff class T has a constexpr default constructor or has an
5114 implicitly declared default constructor that we can't tell if it's constexpr
5115 without forcing a lazy declaration (which might cause undesired
5116 instantiations). */
5118 bool
5119 type_maybe_constexpr_default_constructor (tree t)
5121 if (CLASS_TYPE_P (t) && CLASSTYPE_LAZY_DEFAULT_CTOR (t)
5122 && TYPE_HAS_COMPLEX_DFLT (t))
5123 /* Assume it's constexpr. */
5124 return true;
5125 return type_has_constexpr_default_constructor (t);
5128 /* Returns true iff class TYPE has a virtual destructor. */
5130 bool
5131 type_has_virtual_destructor (tree type)
5133 tree dtor;
5135 if (!CLASS_TYPE_P (type))
5136 return false;
5138 gcc_assert (COMPLETE_TYPE_P (type));
5139 dtor = CLASSTYPE_DESTRUCTOR (type);
5140 return (dtor && DECL_VIRTUAL_P (dtor));
5143 /* Returns true iff T, a class, has a move-assignment or
5144 move-constructor. Does not lazily declare either.
5145 If USER_P is false, any move function will do. If it is true, the
5146 move function must be user-declared.
5148 Note that user-declared here is different from "user-provided",
5149 which doesn't include functions that are defaulted in the
5150 class. */
5152 bool
5153 classtype_has_move_assign_or_move_ctor_p (tree t, bool user_p)
5155 gcc_assert (user_p
5156 || (!CLASSTYPE_LAZY_MOVE_CTOR (t)
5157 && !CLASSTYPE_LAZY_MOVE_ASSIGN (t)));
5159 if (!CLASSTYPE_LAZY_MOVE_CTOR (t))
5160 for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (t)); iter; ++iter)
5161 if ((!user_p || !DECL_ARTIFICIAL (*iter)) && move_fn_p (*iter))
5162 return true;
5164 if (!CLASSTYPE_LAZY_MOVE_ASSIGN (t))
5165 for (ovl_iterator iter (get_class_binding_direct
5166 (t, assign_op_identifier));
5167 iter; ++iter)
5168 if ((!user_p || !DECL_ARTIFICIAL (*iter)) && move_fn_p (*iter))
5169 return true;
5171 return false;
5174 /* If T, a class, has a user-provided copy constructor, copy assignment
5175 operator, or destructor, returns that function. Otherwise, null. */
5177 tree
5178 classtype_has_user_copy_or_dtor (tree t)
5180 if (!CLASSTYPE_LAZY_COPY_CTOR (t))
5181 for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (t)); iter; ++iter)
5183 tree fn = *iter;
5184 if (user_provided_p (fn) && copy_fn_p (fn))
5185 return fn;
5188 if (!CLASSTYPE_LAZY_COPY_ASSIGN (t))
5189 for (ovl_iterator iter (get_class_binding_direct
5190 (t, assign_op_identifier));
5191 iter; ++iter)
5193 tree fn = *iter;
5194 if (user_provided_p (fn) && copy_fn_p (fn))
5195 return fn;
5198 if (!CLASSTYPE_LAZY_DESTRUCTOR (t))
5200 tree fn = CLASSTYPE_DESTRUCTOR (t);
5201 if (user_provided_p (fn))
5202 return fn;
5205 return NULL_TREE;
5208 /* Nonzero if we need to build up a constructor call when initializing an
5209 object of this class, either because it has a user-declared constructor
5210 or because it doesn't have a default constructor (so we need to give an
5211 error if no initializer is provided). Use TYPE_NEEDS_CONSTRUCTING when
5212 what you care about is whether or not an object can be produced by a
5213 constructor (e.g. so we don't set TREE_READONLY on const variables of
5214 such type); use this function when what you care about is whether or not
5215 to try to call a constructor to create an object. The latter case is
5216 the former plus some cases of constructors that cannot be called. */
5218 bool
5219 type_build_ctor_call (tree t)
5221 tree inner;
5222 if (TYPE_NEEDS_CONSTRUCTING (t))
5223 return true;
5224 inner = strip_array_types (t);
5225 if (!CLASS_TYPE_P (inner) || ANON_AGGR_TYPE_P (inner))
5226 return false;
5227 if (!TYPE_HAS_DEFAULT_CONSTRUCTOR (inner))
5228 return true;
5229 if (cxx_dialect < cxx11)
5230 return false;
5231 /* A user-declared constructor might be private, and a constructor might
5232 be trivial but deleted. */
5233 for (ovl_iterator iter (get_class_binding (inner, complete_ctor_identifier));
5234 iter; ++iter)
5236 tree fn = *iter;
5237 if (!DECL_ARTIFICIAL (fn)
5238 || TREE_DEPRECATED (fn)
5239 || DECL_DELETED_FN (fn))
5240 return true;
5242 return false;
5245 /* Like type_build_ctor_call, but for destructors. */
5247 bool
5248 type_build_dtor_call (tree t)
5250 tree inner;
5251 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t))
5252 return true;
5253 inner = strip_array_types (t);
5254 if (!CLASS_TYPE_P (inner) || ANON_AGGR_TYPE_P (inner)
5255 || !COMPLETE_TYPE_P (inner))
5256 return false;
5257 if (cxx_dialect < cxx11)
5258 return false;
5259 /* A user-declared destructor might be private, and a destructor might
5260 be trivial but deleted. */
5261 for (ovl_iterator iter (get_class_binding (inner, complete_dtor_identifier));
5262 iter; ++iter)
5264 tree fn = *iter;
5265 if (!DECL_ARTIFICIAL (fn)
5266 || TREE_DEPRECATED (fn)
5267 || DECL_DELETED_FN (fn))
5268 return true;
5270 return false;
5273 /* Remove all zero-width bit-fields from T. */
5275 static void
5276 remove_zero_width_bit_fields (tree t)
5278 tree *fieldsp;
5280 fieldsp = &TYPE_FIELDS (t);
5281 while (*fieldsp)
5283 if (TREE_CODE (*fieldsp) == FIELD_DECL
5284 && DECL_C_BIT_FIELD (*fieldsp)
5285 /* We should not be confused by the fact that grokbitfield
5286 temporarily sets the width of the bit field into
5287 DECL_BIT_FIELD_REPRESENTATIVE (*fieldsp).
5288 check_bitfield_decl eventually sets DECL_SIZE (*fieldsp)
5289 to that width. */
5290 && (DECL_SIZE (*fieldsp) == NULL_TREE
5291 || integer_zerop (DECL_SIZE (*fieldsp))))
5292 *fieldsp = DECL_CHAIN (*fieldsp);
5293 else
5294 fieldsp = &DECL_CHAIN (*fieldsp);
5298 /* Returns TRUE iff we need a cookie when dynamically allocating an
5299 array whose elements have the indicated class TYPE. */
5301 static bool
5302 type_requires_array_cookie (tree type)
5304 tree fns;
5305 bool has_two_argument_delete_p = false;
5307 gcc_assert (CLASS_TYPE_P (type));
5309 /* If there's a non-trivial destructor, we need a cookie. In order
5310 to iterate through the array calling the destructor for each
5311 element, we'll have to know how many elements there are. */
5312 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
5313 return true;
5315 /* If the usual deallocation function is a two-argument whose second
5316 argument is of type `size_t', then we have to pass the size of
5317 the array to the deallocation function, so we will need to store
5318 a cookie. */
5319 fns = lookup_fnfields (TYPE_BINFO (type),
5320 ovl_op_identifier (false, VEC_DELETE_EXPR),
5321 /*protect=*/0);
5322 /* If there are no `operator []' members, or the lookup is
5323 ambiguous, then we don't need a cookie. */
5324 if (!fns || fns == error_mark_node)
5325 return false;
5326 /* Loop through all of the functions. */
5327 for (lkp_iterator iter (BASELINK_FUNCTIONS (fns)); iter; ++iter)
5329 tree fn = *iter;
5331 /* See if this function is a one-argument delete function. If
5332 it is, then it will be the usual deallocation function. */
5333 tree second_parm = TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (fn)));
5334 if (second_parm == void_list_node)
5335 return false;
5336 /* Do not consider this function if its second argument is an
5337 ellipsis. */
5338 if (!second_parm)
5339 continue;
5340 /* Otherwise, if we have a two-argument function and the second
5341 argument is `size_t', it will be the usual deallocation
5342 function -- unless there is one-argument function, too. */
5343 if (TREE_CHAIN (second_parm) == void_list_node
5344 && same_type_p (TREE_VALUE (second_parm), size_type_node))
5345 has_two_argument_delete_p = true;
5348 return has_two_argument_delete_p;
5351 /* Finish computing the `literal type' property of class type T.
5353 At this point, we have already processed base classes and
5354 non-static data members. We need to check whether the copy
5355 constructor is trivial, the destructor is trivial, and there
5356 is a trivial default constructor or at least one constexpr
5357 constructor other than the copy constructor. */
5359 static void
5360 finalize_literal_type_property (tree t)
5362 tree fn;
5364 if (cxx_dialect < cxx11
5365 || TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t))
5366 CLASSTYPE_LITERAL_P (t) = false;
5367 else if (CLASSTYPE_LITERAL_P (t) && LAMBDA_TYPE_P (t))
5368 CLASSTYPE_LITERAL_P (t) = (cxx_dialect >= cxx17);
5369 else if (CLASSTYPE_LITERAL_P (t) && !TYPE_HAS_TRIVIAL_DFLT (t)
5370 && CLASSTYPE_NON_AGGREGATE (t)
5371 && !TYPE_HAS_CONSTEXPR_CTOR (t))
5372 CLASSTYPE_LITERAL_P (t) = false;
5374 /* C++14 DR 1684 removed this restriction. */
5375 if (cxx_dialect < cxx14
5376 && !CLASSTYPE_LITERAL_P (t) && !LAMBDA_TYPE_P (t))
5377 for (fn = TYPE_FIELDS (t); fn; fn = DECL_CHAIN (fn))
5378 if (TREE_CODE (fn) == FUNCTION_DECL
5379 && DECL_DECLARED_CONSTEXPR_P (fn)
5380 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
5381 && !DECL_CONSTRUCTOR_P (fn))
5383 DECL_DECLARED_CONSTEXPR_P (fn) = false;
5384 if (!DECL_GENERATED_P (fn)
5385 && pedwarn (DECL_SOURCE_LOCATION (fn), OPT_Wpedantic,
5386 "enclosing class of %<constexpr%> non-static member "
5387 "function %q+#D is not a literal type", fn))
5388 explain_non_literal_class (t);
5392 /* T is a non-literal type used in a context which requires a constant
5393 expression. Explain why it isn't literal. */
5395 void
5396 explain_non_literal_class (tree t)
5398 static hash_set<tree> *diagnosed;
5400 if (!CLASS_TYPE_P (t))
5401 return;
5402 t = TYPE_MAIN_VARIANT (t);
5404 if (diagnosed == NULL)
5405 diagnosed = new hash_set<tree>;
5406 if (diagnosed->add (t))
5407 /* Already explained. */
5408 return;
5410 inform (UNKNOWN_LOCATION, "%q+T is not literal because:", t);
5411 if (cxx_dialect < cxx17 && LAMBDA_TYPE_P (t))
5412 inform (UNKNOWN_LOCATION,
5413 " %qT is a closure type, which is only literal in "
5414 "C++17 and later", t);
5415 else if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t))
5416 inform (UNKNOWN_LOCATION, " %q+T has a non-trivial destructor", t);
5417 else if (CLASSTYPE_NON_AGGREGATE (t)
5418 && !TYPE_HAS_TRIVIAL_DFLT (t)
5419 && !LAMBDA_TYPE_P (t)
5420 && !TYPE_HAS_CONSTEXPR_CTOR (t))
5422 inform (UNKNOWN_LOCATION,
5423 " %q+T is not an aggregate, does not have a trivial "
5424 "default constructor, and has no %<constexpr%> constructor that "
5425 "is not a copy or move constructor", t);
5426 if (type_has_non_user_provided_default_constructor (t))
5427 /* Note that we can't simply call locate_ctor because when the
5428 constructor is deleted it just returns NULL_TREE. */
5429 for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (t)); iter; ++iter)
5431 tree fn = *iter;
5432 tree parms = TYPE_ARG_TYPES (TREE_TYPE (fn));
5434 parms = skip_artificial_parms_for (fn, parms);
5436 if (sufficient_parms_p (parms))
5438 if (DECL_DELETED_FN (fn))
5439 maybe_explain_implicit_delete (fn);
5440 else
5441 explain_invalid_constexpr_fn (fn);
5442 break;
5446 else
5448 tree binfo, base_binfo, field; int i;
5449 for (binfo = TYPE_BINFO (t), i = 0;
5450 BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
5452 tree basetype = TREE_TYPE (base_binfo);
5453 if (!CLASSTYPE_LITERAL_P (basetype))
5455 inform (UNKNOWN_LOCATION,
5456 " base class %qT of %q+T is non-literal",
5457 basetype, t);
5458 explain_non_literal_class (basetype);
5459 return;
5462 for (field = TYPE_FIELDS (t); field; field = TREE_CHAIN (field))
5464 tree ftype;
5465 if (TREE_CODE (field) != FIELD_DECL)
5466 continue;
5467 ftype = TREE_TYPE (field);
5468 if (!literal_type_p (ftype))
5470 inform (DECL_SOURCE_LOCATION (field),
5471 " non-static data member %qD has non-literal type",
5472 field);
5473 if (CLASS_TYPE_P (ftype))
5474 explain_non_literal_class (ftype);
5476 if (CP_TYPE_VOLATILE_P (ftype))
5477 inform (DECL_SOURCE_LOCATION (field),
5478 " non-static data member %qD has volatile type", field);
5483 /* Check the validity of the bases and members declared in T. Add any
5484 implicitly-generated functions (like copy-constructors and
5485 assignment operators). Compute various flag bits (like
5486 CLASSTYPE_NON_LAYOUT_POD_T) for T. This routine works purely at the C++
5487 level: i.e., independently of the ABI in use. */
5489 static void
5490 check_bases_and_members (tree t)
5492 /* Nonzero if the implicitly generated copy constructor should take
5493 a non-const reference argument. */
5494 int cant_have_const_ctor;
5495 /* Nonzero if the implicitly generated assignment operator
5496 should take a non-const reference argument. */
5497 int no_const_asn_ref;
5498 tree access_decls;
5499 bool saved_complex_asn_ref;
5500 bool saved_nontrivial_dtor;
5501 tree fn;
5503 /* By default, we use const reference arguments and generate default
5504 constructors. */
5505 cant_have_const_ctor = 0;
5506 no_const_asn_ref = 0;
5508 /* Check all the base-classes and set FMEM members to point to arrays
5509 of potential interest. */
5510 check_bases (t, &cant_have_const_ctor, &no_const_asn_ref);
5512 /* Deduce noexcept on destructor. This needs to happen after we've set
5513 triviality flags appropriately for our bases. */
5514 if (cxx_dialect >= cxx11)
5515 if (tree dtor = CLASSTYPE_DESTRUCTOR (t))
5516 deduce_noexcept_on_destructor (dtor);
5518 /* Check all the method declarations. */
5519 check_methods (t);
5521 /* Save the initial values of these flags which only indicate whether
5522 or not the class has user-provided functions. As we analyze the
5523 bases and members we can set these flags for other reasons. */
5524 saved_complex_asn_ref = TYPE_HAS_COMPLEX_COPY_ASSIGN (t);
5525 saved_nontrivial_dtor = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t);
5527 /* Check all the data member declarations. We cannot call
5528 check_field_decls until we have called check_bases check_methods,
5529 as check_field_decls depends on TYPE_HAS_NONTRIVIAL_DESTRUCTOR
5530 being set appropriately. */
5531 check_field_decls (t, &access_decls,
5532 &cant_have_const_ctor,
5533 &no_const_asn_ref);
5535 /* A nearly-empty class has to be vptr-containing; a nearly empty
5536 class contains just a vptr. */
5537 if (!TYPE_CONTAINS_VPTR_P (t))
5538 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
5540 /* Do some bookkeeping that will guide the generation of implicitly
5541 declared member functions. */
5542 TYPE_HAS_COMPLEX_COPY_CTOR (t) |= TYPE_CONTAINS_VPTR_P (t);
5543 TYPE_HAS_COMPLEX_MOVE_CTOR (t) |= TYPE_CONTAINS_VPTR_P (t);
5544 /* We need to call a constructor for this class if it has a
5545 user-provided constructor, or if the default constructor is going
5546 to initialize the vptr. (This is not an if-and-only-if;
5547 TYPE_NEEDS_CONSTRUCTING is set elsewhere if bases or members
5548 themselves need constructing.) */
5549 TYPE_NEEDS_CONSTRUCTING (t)
5550 |= (type_has_user_provided_constructor (t) || TYPE_CONTAINS_VPTR_P (t));
5551 /* [dcl.init.aggr]
5553 An aggregate is an array or a class with no user-provided
5554 constructors ... and no virtual functions.
5556 Again, other conditions for being an aggregate are checked
5557 elsewhere. */
5558 CLASSTYPE_NON_AGGREGATE (t)
5559 |= (type_has_user_provided_or_explicit_constructor (t)
5560 || TYPE_POLYMORPHIC_P (t));
5561 /* This is the C++98/03 definition of POD; it changed in C++0x, but we
5562 retain the old definition internally for ABI reasons. */
5563 CLASSTYPE_NON_LAYOUT_POD_P (t)
5564 |= (CLASSTYPE_NON_AGGREGATE (t)
5565 || saved_nontrivial_dtor || saved_complex_asn_ref);
5566 CLASSTYPE_NON_STD_LAYOUT (t) |= TYPE_CONTAINS_VPTR_P (t);
5567 TYPE_HAS_COMPLEX_COPY_ASSIGN (t) |= TYPE_CONTAINS_VPTR_P (t);
5568 TYPE_HAS_COMPLEX_MOVE_ASSIGN (t) |= TYPE_CONTAINS_VPTR_P (t);
5569 TYPE_HAS_COMPLEX_DFLT (t) |= TYPE_CONTAINS_VPTR_P (t);
5571 /* If the only explicitly declared default constructor is user-provided,
5572 set TYPE_HAS_COMPLEX_DFLT. */
5573 if (!TYPE_HAS_COMPLEX_DFLT (t)
5574 && TYPE_HAS_DEFAULT_CONSTRUCTOR (t)
5575 && !type_has_non_user_provided_default_constructor (t))
5576 TYPE_HAS_COMPLEX_DFLT (t) = true;
5578 /* Warn if a public base of a polymorphic type has an accessible
5579 non-virtual destructor. It is only now that we know the class is
5580 polymorphic. Although a polymorphic base will have a already
5581 been diagnosed during its definition, we warn on use too. */
5582 if (TYPE_POLYMORPHIC_P (t) && warn_nonvdtor)
5584 tree binfo = TYPE_BINFO (t);
5585 vec<tree, va_gc> *accesses = BINFO_BASE_ACCESSES (binfo);
5586 tree base_binfo;
5587 unsigned i;
5589 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
5591 tree basetype = TREE_TYPE (base_binfo);
5593 if ((*accesses)[i] == access_public_node
5594 && (TYPE_POLYMORPHIC_P (basetype) || warn_ecpp)
5595 && accessible_nvdtor_p (basetype))
5596 warning (OPT_Wnon_virtual_dtor,
5597 "base class %q#T has accessible non-virtual destructor",
5598 basetype);
5602 /* If the class has no user-declared constructor, but does have
5603 non-static const or reference data members that can never be
5604 initialized, issue a warning. */
5605 if (warn_uninitialized
5606 /* Classes with user-declared constructors are presumed to
5607 initialize these members. */
5608 && !TYPE_HAS_USER_CONSTRUCTOR (t)
5609 /* Aggregates can be initialized with brace-enclosed
5610 initializers. */
5611 && CLASSTYPE_NON_AGGREGATE (t))
5613 tree field;
5615 for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
5617 tree type;
5619 if (TREE_CODE (field) != FIELD_DECL
5620 || DECL_INITIAL (field) != NULL_TREE)
5621 continue;
5623 type = TREE_TYPE (field);
5624 if (TYPE_REF_P (type))
5625 warning_at (DECL_SOURCE_LOCATION (field),
5626 OPT_Wuninitialized, "non-static reference %q#D "
5627 "in class without a constructor", field);
5628 else if (CP_TYPE_CONST_P (type)
5629 && (!CLASS_TYPE_P (type)
5630 || !TYPE_HAS_DEFAULT_CONSTRUCTOR (type)))
5631 warning_at (DECL_SOURCE_LOCATION (field),
5632 OPT_Wuninitialized, "non-static const member %q#D "
5633 "in class without a constructor", field);
5637 /* Synthesize any needed methods. */
5638 add_implicitly_declared_members (t, &access_decls,
5639 cant_have_const_ctor,
5640 no_const_asn_ref);
5642 /* Check defaulted declarations here so we have cant_have_const_ctor
5643 and don't need to worry about clones. */
5644 for (fn = TYPE_FIELDS (t); fn; fn = DECL_CHAIN (fn))
5645 if (DECL_DECLARES_FUNCTION_P (fn)
5646 && !DECL_ARTIFICIAL (fn)
5647 && DECL_DEFAULTED_IN_CLASS_P (fn))
5649 int copy = copy_fn_p (fn);
5650 if (copy > 0)
5652 bool imp_const_p
5653 = (DECL_CONSTRUCTOR_P (fn) ? !cant_have_const_ctor
5654 : !no_const_asn_ref);
5655 bool fn_const_p = (copy == 2);
5657 if (fn_const_p && !imp_const_p)
5658 /* If the function is defaulted outside the class, we just
5659 give the synthesis error. */
5660 error ("%q+D declared to take const reference, but implicit "
5661 "declaration would take non-const", fn);
5663 defaulted_late_check (fn);
5666 if (LAMBDA_TYPE_P (t))
5668 /* "This class type is not an aggregate." */
5669 CLASSTYPE_NON_AGGREGATE (t) = 1;
5672 /* Compute the 'literal type' property before we
5673 do anything with non-static member functions. */
5674 finalize_literal_type_property (t);
5676 /* Create the in-charge and not-in-charge variants of constructors
5677 and destructors. */
5678 clone_constructors_and_destructors (t);
5680 /* Process the using-declarations. */
5681 for (; access_decls; access_decls = TREE_CHAIN (access_decls))
5682 handle_using_decl (TREE_VALUE (access_decls), t);
5684 /* Figure out whether or not we will need a cookie when dynamically
5685 allocating an array of this type. */
5686 LANG_TYPE_CLASS_CHECK (t)->vec_new_uses_cookie
5687 = type_requires_array_cookie (t);
5690 /* If T needs a pointer to its virtual function table, set TYPE_VFIELD
5691 accordingly. If a new vfield was created (because T doesn't have a
5692 primary base class), then the newly created field is returned. It
5693 is not added to the TYPE_FIELDS list; it is the caller's
5694 responsibility to do that. Accumulate declared virtual functions
5695 on VIRTUALS_P. */
5697 static tree
5698 create_vtable_ptr (tree t, tree* virtuals_p)
5700 tree fn;
5702 /* Collect the virtual functions declared in T. */
5703 for (fn = TYPE_FIELDS (t); fn; fn = DECL_CHAIN (fn))
5704 if (TREE_CODE (fn) == FUNCTION_DECL
5705 && DECL_VINDEX (fn) && !DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (fn)
5706 && TREE_CODE (DECL_VINDEX (fn)) != INTEGER_CST)
5708 tree new_virtual = make_node (TREE_LIST);
5710 BV_FN (new_virtual) = fn;
5711 BV_DELTA (new_virtual) = integer_zero_node;
5712 BV_VCALL_INDEX (new_virtual) = NULL_TREE;
5714 TREE_CHAIN (new_virtual) = *virtuals_p;
5715 *virtuals_p = new_virtual;
5718 /* If we couldn't find an appropriate base class, create a new field
5719 here. Even if there weren't any new virtual functions, we might need a
5720 new virtual function table if we're supposed to include vptrs in
5721 all classes that need them. */
5722 if (!TYPE_VFIELD (t) && (*virtuals_p || TYPE_CONTAINS_VPTR_P (t)))
5724 /* We build this decl with vtbl_ptr_type_node, which is a
5725 `vtable_entry_type*'. It might seem more precise to use
5726 `vtable_entry_type (*)[N]' where N is the number of virtual
5727 functions. However, that would require the vtable pointer in
5728 base classes to have a different type than the vtable pointer
5729 in derived classes. We could make that happen, but that
5730 still wouldn't solve all the problems. In particular, the
5731 type-based alias analysis code would decide that assignments
5732 to the base class vtable pointer can't alias assignments to
5733 the derived class vtable pointer, since they have different
5734 types. Thus, in a derived class destructor, where the base
5735 class constructor was inlined, we could generate bad code for
5736 setting up the vtable pointer.
5738 Therefore, we use one type for all vtable pointers. We still
5739 use a type-correct type; it's just doesn't indicate the array
5740 bounds. That's better than using `void*' or some such; it's
5741 cleaner, and it let's the alias analysis code know that these
5742 stores cannot alias stores to void*! */
5743 tree field;
5745 field = build_decl (input_location,
5746 FIELD_DECL, get_vfield_name (t), vtbl_ptr_type_node);
5747 DECL_VIRTUAL_P (field) = 1;
5748 DECL_ARTIFICIAL (field) = 1;
5749 DECL_FIELD_CONTEXT (field) = t;
5750 DECL_FCONTEXT (field) = t;
5751 if (TYPE_PACKED (t))
5752 DECL_PACKED (field) = 1;
5754 TYPE_VFIELD (t) = field;
5756 /* This class is non-empty. */
5757 CLASSTYPE_EMPTY_P (t) = 0;
5759 return field;
5762 return NULL_TREE;
5765 /* Add OFFSET to all base types of BINFO which is a base in the
5766 hierarchy dominated by T.
5768 OFFSET, which is a type offset, is number of bytes. */
5770 static void
5771 propagate_binfo_offsets (tree binfo, tree offset)
5773 int i;
5774 tree primary_binfo;
5775 tree base_binfo;
5777 /* Update BINFO's offset. */
5778 BINFO_OFFSET (binfo)
5779 = fold_convert (sizetype,
5780 size_binop (PLUS_EXPR,
5781 fold_convert (ssizetype, BINFO_OFFSET (binfo)),
5782 offset));
5784 /* Find the primary base class. */
5785 primary_binfo = get_primary_binfo (binfo);
5787 if (primary_binfo && BINFO_INHERITANCE_CHAIN (primary_binfo) == binfo)
5788 propagate_binfo_offsets (primary_binfo, offset);
5790 /* Scan all of the bases, pushing the BINFO_OFFSET adjust
5791 downwards. */
5792 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
5794 /* Don't do the primary base twice. */
5795 if (base_binfo == primary_binfo)
5796 continue;
5798 if (BINFO_VIRTUAL_P (base_binfo))
5799 continue;
5801 propagate_binfo_offsets (base_binfo, offset);
5805 /* Set BINFO_OFFSET for all of the virtual bases for RLI->T. Update
5806 TYPE_ALIGN and TYPE_SIZE for T. OFFSETS gives the location of
5807 empty subobjects of T. */
5809 static void
5810 layout_virtual_bases (record_layout_info rli, splay_tree offsets)
5812 tree vbase;
5813 tree t = rli->t;
5814 tree *next_field;
5816 if (BINFO_N_BASE_BINFOS (TYPE_BINFO (t)) == 0)
5817 return;
5819 /* Find the last field. The artificial fields created for virtual
5820 bases will go after the last extant field to date. */
5821 next_field = &TYPE_FIELDS (t);
5822 while (*next_field)
5823 next_field = &DECL_CHAIN (*next_field);
5825 /* Go through the virtual bases, allocating space for each virtual
5826 base that is not already a primary base class. These are
5827 allocated in inheritance graph order. */
5828 for (vbase = TYPE_BINFO (t); vbase; vbase = TREE_CHAIN (vbase))
5830 if (!BINFO_VIRTUAL_P (vbase))
5831 continue;
5833 if (!BINFO_PRIMARY_P (vbase))
5835 /* This virtual base is not a primary base of any class in the
5836 hierarchy, so we have to add space for it. */
5837 next_field = build_base_field (rli, vbase,
5838 offsets, next_field);
5843 /* Returns the offset of the byte just past the end of the base class
5844 BINFO. */
5846 static tree
5847 end_of_base (tree binfo)
5849 tree size;
5851 if (!CLASSTYPE_AS_BASE (BINFO_TYPE (binfo)))
5852 size = TYPE_SIZE_UNIT (char_type_node);
5853 else if (is_empty_class (BINFO_TYPE (binfo)))
5854 /* An empty class has zero CLASSTYPE_SIZE_UNIT, but we need to
5855 allocate some space for it. It cannot have virtual bases, so
5856 TYPE_SIZE_UNIT is fine. */
5857 size = TYPE_SIZE_UNIT (BINFO_TYPE (binfo));
5858 else
5859 size = CLASSTYPE_SIZE_UNIT (BINFO_TYPE (binfo));
5861 return size_binop (PLUS_EXPR, BINFO_OFFSET (binfo), size);
5864 /* Returns the offset of the byte just past the end of the base class
5865 with the highest offset in T. If INCLUDE_VIRTUALS_P is zero, then
5866 only non-virtual bases are included. */
5868 static tree
5869 end_of_class (tree t, int include_virtuals_p)
5871 tree result = size_zero_node;
5872 vec<tree, va_gc> *vbases;
5873 tree binfo;
5874 tree base_binfo;
5875 tree offset;
5876 int i;
5878 for (binfo = TYPE_BINFO (t), i = 0;
5879 BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
5881 if (!include_virtuals_p
5882 && BINFO_VIRTUAL_P (base_binfo)
5883 && (!BINFO_PRIMARY_P (base_binfo)
5884 || BINFO_INHERITANCE_CHAIN (base_binfo) != TYPE_BINFO (t)))
5885 continue;
5887 offset = end_of_base (base_binfo);
5888 if (tree_int_cst_lt (result, offset))
5889 result = offset;
5892 if (include_virtuals_p)
5893 for (vbases = CLASSTYPE_VBASECLASSES (t), i = 0;
5894 vec_safe_iterate (vbases, i, &base_binfo); i++)
5896 offset = end_of_base (base_binfo);
5897 if (tree_int_cst_lt (result, offset))
5898 result = offset;
5901 return result;
5904 /* Warn about bases of T that are inaccessible because they are
5905 ambiguous. For example:
5907 struct S {};
5908 struct T : public S {};
5909 struct U : public S, public T {};
5911 Here, `(S*) new U' is not allowed because there are two `S'
5912 subobjects of U. */
5914 static void
5915 warn_about_ambiguous_bases (tree t)
5917 int i;
5918 vec<tree, va_gc> *vbases;
5919 tree basetype;
5920 tree binfo;
5921 tree base_binfo;
5923 /* If there are no repeated bases, nothing can be ambiguous. */
5924 if (!CLASSTYPE_REPEATED_BASE_P (t))
5925 return;
5927 /* Check direct bases. */
5928 for (binfo = TYPE_BINFO (t), i = 0;
5929 BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
5931 basetype = BINFO_TYPE (base_binfo);
5933 if (!uniquely_derived_from_p (basetype, t))
5934 warning (0, "direct base %qT inaccessible in %qT due to ambiguity",
5935 basetype, t);
5938 /* Check for ambiguous virtual bases. */
5939 if (extra_warnings)
5940 for (vbases = CLASSTYPE_VBASECLASSES (t), i = 0;
5941 vec_safe_iterate (vbases, i, &binfo); i++)
5943 basetype = BINFO_TYPE (binfo);
5945 if (!uniquely_derived_from_p (basetype, t))
5946 warning (OPT_Wextra, "virtual base %qT inaccessible in %qT due "
5947 "to ambiguity", basetype, t);
5951 /* Compare two INTEGER_CSTs K1 and K2. */
5953 static int
5954 splay_tree_compare_integer_csts (splay_tree_key k1, splay_tree_key k2)
5956 return tree_int_cst_compare ((tree) k1, (tree) k2);
5959 /* Increase the size indicated in RLI to account for empty classes
5960 that are "off the end" of the class. */
5962 static void
5963 include_empty_classes (record_layout_info rli)
5965 tree eoc;
5966 tree rli_size;
5968 /* It might be the case that we grew the class to allocate a
5969 zero-sized base class. That won't be reflected in RLI, yet,
5970 because we are willing to overlay multiple bases at the same
5971 offset. However, now we need to make sure that RLI is big enough
5972 to reflect the entire class. */
5973 eoc = end_of_class (rli->t, CLASSTYPE_AS_BASE (rli->t) != NULL_TREE);
5974 rli_size = rli_size_unit_so_far (rli);
5975 if (TREE_CODE (rli_size) == INTEGER_CST
5976 && tree_int_cst_lt (rli_size, eoc))
5978 /* The size should have been rounded to a whole byte. */
5979 gcc_assert (tree_int_cst_equal
5980 (rli->bitpos, round_down (rli->bitpos, BITS_PER_UNIT)));
5981 rli->bitpos
5982 = size_binop (PLUS_EXPR,
5983 rli->bitpos,
5984 size_binop (MULT_EXPR,
5985 fold_convert (bitsizetype,
5986 size_binop (MINUS_EXPR,
5987 eoc, rli_size)),
5988 bitsize_int (BITS_PER_UNIT)));
5989 normalize_rli (rli);
5993 /* Calculate the TYPE_SIZE, TYPE_ALIGN, etc for T. Calculate
5994 BINFO_OFFSETs for all of the base-classes. Position the vtable
5995 pointer. Accumulate declared virtual functions on VIRTUALS_P. */
5997 static void
5998 layout_class_type (tree t, tree *virtuals_p)
6000 tree non_static_data_members;
6001 tree field;
6002 tree vptr;
6003 record_layout_info rli;
6004 /* Maps offsets (represented as INTEGER_CSTs) to a TREE_LIST of
6005 types that appear at that offset. */
6006 splay_tree empty_base_offsets;
6007 /* True if the last field laid out was a bit-field. */
6008 bool last_field_was_bitfield = false;
6009 /* The location at which the next field should be inserted. */
6010 tree *next_field;
6012 /* Keep track of the first non-static data member. */
6013 non_static_data_members = TYPE_FIELDS (t);
6015 /* Start laying out the record. */
6016 rli = start_record_layout (t);
6018 /* Mark all the primary bases in the hierarchy. */
6019 determine_primary_bases (t);
6021 /* Create a pointer to our virtual function table. */
6022 vptr = create_vtable_ptr (t, virtuals_p);
6024 /* The vptr is always the first thing in the class. */
6025 if (vptr)
6027 DECL_CHAIN (vptr) = TYPE_FIELDS (t);
6028 TYPE_FIELDS (t) = vptr;
6029 next_field = &DECL_CHAIN (vptr);
6030 place_field (rli, vptr);
6032 else
6033 next_field = &TYPE_FIELDS (t);
6035 /* Build FIELD_DECLs for all of the non-virtual base-types. */
6036 empty_base_offsets = splay_tree_new (splay_tree_compare_integer_csts,
6037 NULL, NULL);
6038 build_base_fields (rli, empty_base_offsets, next_field);
6040 /* Layout the non-static data members. */
6041 for (field = non_static_data_members; field; field = DECL_CHAIN (field))
6043 tree type;
6044 tree padding;
6046 /* We still pass things that aren't non-static data members to
6047 the back end, in case it wants to do something with them. */
6048 if (TREE_CODE (field) != FIELD_DECL)
6050 place_field (rli, field);
6051 /* If the static data member has incomplete type, keep track
6052 of it so that it can be completed later. (The handling
6053 of pending statics in finish_record_layout is
6054 insufficient; consider:
6056 struct S1;
6057 struct S2 { static S1 s1; };
6059 At this point, finish_record_layout will be called, but
6060 S1 is still incomplete.) */
6061 if (VAR_P (field))
6063 maybe_register_incomplete_var (field);
6064 /* The visibility of static data members is determined
6065 at their point of declaration, not their point of
6066 definition. */
6067 determine_visibility (field);
6069 continue;
6072 type = TREE_TYPE (field);
6073 if (type == error_mark_node)
6074 continue;
6076 padding = NULL_TREE;
6078 /* If this field is a bit-field whose width is greater than its
6079 type, then there are some special rules for allocating
6080 it. */
6081 if (DECL_C_BIT_FIELD (field)
6082 && tree_int_cst_lt (TYPE_SIZE (type), DECL_SIZE (field)))
6084 bool was_unnamed_p = false;
6085 /* We must allocate the bits as if suitably aligned for the
6086 longest integer type that fits in this many bits. Then,
6087 we are supposed to use the left over bits as additional
6088 padding. */
6090 /* Do not pick a type bigger than MAX_FIXED_MODE_SIZE. */
6091 tree limit = size_int (MAX_FIXED_MODE_SIZE);
6092 if (tree_int_cst_lt (DECL_SIZE (field), limit))
6093 limit = DECL_SIZE (field);
6095 tree integer_type = integer_types[itk_char];
6096 for (unsigned itk = itk_char; itk != itk_none; itk++)
6097 if (tree next = integer_types[itk])
6099 if (tree_int_cst_lt (limit, TYPE_SIZE (next)))
6100 /* Too big, so our current guess is what we want. */
6101 break;
6102 /* Not bigger than limit, ok */
6103 integer_type = next;
6106 /* Figure out how much additional padding is required. */
6107 if (TREE_CODE (t) == UNION_TYPE)
6108 /* In a union, the padding field must have the full width
6109 of the bit-field; all fields start at offset zero. */
6110 padding = DECL_SIZE (field);
6111 else
6112 padding = size_binop (MINUS_EXPR, DECL_SIZE (field),
6113 TYPE_SIZE (integer_type));
6115 if (integer_zerop (padding))
6116 padding = NULL_TREE;
6118 /* An unnamed bitfield does not normally affect the
6119 alignment of the containing class on a target where
6120 PCC_BITFIELD_TYPE_MATTERS. But, the C++ ABI does not
6121 make any exceptions for unnamed bitfields when the
6122 bitfields are longer than their types. Therefore, we
6123 temporarily give the field a name. */
6124 if (PCC_BITFIELD_TYPE_MATTERS && !DECL_NAME (field))
6126 was_unnamed_p = true;
6127 DECL_NAME (field) = make_anon_name ();
6130 DECL_SIZE (field) = TYPE_SIZE (integer_type);
6131 SET_DECL_ALIGN (field, TYPE_ALIGN (integer_type));
6132 DECL_USER_ALIGN (field) = TYPE_USER_ALIGN (integer_type);
6133 layout_nonempty_base_or_field (rli, field, NULL_TREE,
6134 empty_base_offsets);
6135 if (was_unnamed_p)
6136 DECL_NAME (field) = NULL_TREE;
6137 /* Now that layout has been performed, set the size of the
6138 field to the size of its declared type; the rest of the
6139 field is effectively invisible. */
6140 DECL_SIZE (field) = TYPE_SIZE (type);
6141 /* We must also reset the DECL_MODE of the field. */
6142 SET_DECL_MODE (field, TYPE_MODE (type));
6144 else
6145 layout_nonempty_base_or_field (rli, field, NULL_TREE,
6146 empty_base_offsets);
6148 /* Remember the location of any empty classes in FIELD. */
6149 record_subobject_offsets (TREE_TYPE (field),
6150 byte_position(field),
6151 empty_base_offsets,
6152 /*is_data_member=*/true);
6154 /* If a bit-field does not immediately follow another bit-field,
6155 and yet it starts in the middle of a byte, we have failed to
6156 comply with the ABI. */
6157 if (warn_abi
6158 && DECL_C_BIT_FIELD (field)
6159 /* The TREE_NO_WARNING flag gets set by Objective-C when
6160 laying out an Objective-C class. The ObjC ABI differs
6161 from the C++ ABI, and so we do not want a warning
6162 here. */
6163 && !TREE_NO_WARNING (field)
6164 && !last_field_was_bitfield
6165 && !integer_zerop (size_binop (TRUNC_MOD_EXPR,
6166 DECL_FIELD_BIT_OFFSET (field),
6167 bitsize_unit_node)))
6168 warning_at (DECL_SOURCE_LOCATION (field), OPT_Wabi,
6169 "offset of %qD is not ABI-compliant and may "
6170 "change in a future version of GCC", field);
6172 /* The middle end uses the type of expressions to determine the
6173 possible range of expression values. In order to optimize
6174 "x.i > 7" to "false" for a 2-bit bitfield "i", the middle end
6175 must be made aware of the width of "i", via its type.
6177 Because C++ does not have integer types of arbitrary width,
6178 we must (for the purposes of the front end) convert from the
6179 type assigned here to the declared type of the bitfield
6180 whenever a bitfield expression is used as an rvalue.
6181 Similarly, when assigning a value to a bitfield, the value
6182 must be converted to the type given the bitfield here. */
6183 if (DECL_C_BIT_FIELD (field))
6185 unsigned HOST_WIDE_INT width;
6186 tree ftype = TREE_TYPE (field);
6187 width = tree_to_uhwi (DECL_SIZE (field));
6188 if (width != TYPE_PRECISION (ftype))
6190 TREE_TYPE (field)
6191 = c_build_bitfield_integer_type (width,
6192 TYPE_UNSIGNED (ftype));
6193 TREE_TYPE (field)
6194 = cp_build_qualified_type (TREE_TYPE (field),
6195 cp_type_quals (ftype));
6199 /* If we needed additional padding after this field, add it
6200 now. */
6201 if (padding)
6203 tree padding_field;
6205 padding_field = build_decl (input_location,
6206 FIELD_DECL,
6207 NULL_TREE,
6208 char_type_node);
6209 DECL_BIT_FIELD (padding_field) = 1;
6210 DECL_SIZE (padding_field) = padding;
6211 DECL_CONTEXT (padding_field) = t;
6212 DECL_ARTIFICIAL (padding_field) = 1;
6213 DECL_IGNORED_P (padding_field) = 1;
6214 DECL_PADDING_P (padding_field) = 1;
6215 layout_nonempty_base_or_field (rli, padding_field,
6216 NULL_TREE,
6217 empty_base_offsets);
6220 last_field_was_bitfield = DECL_C_BIT_FIELD (field);
6223 if (!integer_zerop (rli->bitpos))
6225 /* Make sure that we are on a byte boundary so that the size of
6226 the class without virtual bases will always be a round number
6227 of bytes. */
6228 rli->bitpos = round_up_loc (input_location, rli->bitpos, BITS_PER_UNIT);
6229 normalize_rli (rli);
6232 /* Delete all zero-width bit-fields from the list of fields. Now
6233 that the type is laid out they are no longer important. */
6234 remove_zero_width_bit_fields (t);
6236 if (CLASSTYPE_NON_LAYOUT_POD_P (t) || CLASSTYPE_EMPTY_P (t))
6238 /* T needs a different layout as a base (eliding virtual bases
6239 or whatever). Create that version. */
6240 tree base_t = make_node (TREE_CODE (t));
6242 /* If the ABI version is not at least two, and the last
6243 field was a bit-field, RLI may not be on a byte
6244 boundary. In particular, rli_size_unit_so_far might
6245 indicate the last complete byte, while rli_size_so_far
6246 indicates the total number of bits used. Therefore,
6247 rli_size_so_far, rather than rli_size_unit_so_far, is
6248 used to compute TYPE_SIZE_UNIT. */
6249 tree eoc = end_of_class (t, /*include_virtuals_p=*/0);
6250 TYPE_SIZE_UNIT (base_t)
6251 = size_binop (MAX_EXPR,
6252 fold_convert (sizetype,
6253 size_binop (CEIL_DIV_EXPR,
6254 rli_size_so_far (rli),
6255 bitsize_int (BITS_PER_UNIT))),
6256 eoc);
6257 TYPE_SIZE (base_t)
6258 = size_binop (MAX_EXPR,
6259 rli_size_so_far (rli),
6260 size_binop (MULT_EXPR,
6261 fold_convert (bitsizetype, eoc),
6262 bitsize_int (BITS_PER_UNIT)));
6263 SET_TYPE_ALIGN (base_t, rli->record_align);
6264 TYPE_USER_ALIGN (base_t) = TYPE_USER_ALIGN (t);
6266 /* Copy the non-static data members of T. This will include its
6267 direct non-virtual bases & vtable. */
6268 next_field = &TYPE_FIELDS (base_t);
6269 for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
6270 if (TREE_CODE (field) == FIELD_DECL)
6272 *next_field = copy_node (field);
6273 DECL_CONTEXT (*next_field) = base_t;
6274 next_field = &DECL_CHAIN (*next_field);
6276 *next_field = NULL_TREE;
6278 /* We use the base type for trivial assignments, and hence it
6279 needs a mode. */
6280 compute_record_mode (base_t);
6282 TYPE_CONTEXT (base_t) = t;
6284 /* Record the base version of the type. */
6285 CLASSTYPE_AS_BASE (t) = base_t;
6287 else
6288 CLASSTYPE_AS_BASE (t) = t;
6290 /* Every empty class contains an empty class. */
6291 if (CLASSTYPE_EMPTY_P (t))
6292 CLASSTYPE_CONTAINS_EMPTY_CLASS_P (t) = 1;
6294 /* Set the TYPE_DECL for this type to contain the right
6295 value for DECL_OFFSET, so that we can use it as part
6296 of a COMPONENT_REF for multiple inheritance. */
6297 layout_decl (TYPE_MAIN_DECL (t), 0);
6299 /* Now fix up any virtual base class types that we left lying
6300 around. We must get these done before we try to lay out the
6301 virtual function table. As a side-effect, this will remove the
6302 base subobject fields. */
6303 layout_virtual_bases (rli, empty_base_offsets);
6305 /* Make sure that empty classes are reflected in RLI at this
6306 point. */
6307 include_empty_classes (rli);
6309 /* Make sure not to create any structures with zero size. */
6310 if (integer_zerop (rli_size_unit_so_far (rli)) && CLASSTYPE_EMPTY_P (t))
6311 place_field (rli,
6312 build_decl (input_location,
6313 FIELD_DECL, NULL_TREE, char_type_node));
6315 /* If this is a non-POD, declaring it packed makes a difference to how it
6316 can be used as a field; don't let finalize_record_size undo it. */
6317 if (TYPE_PACKED (t) && !layout_pod_type_p (t))
6318 rli->packed_maybe_necessary = true;
6320 /* Let the back end lay out the type. */
6321 finish_record_layout (rli, /*free_p=*/true);
6323 if (TYPE_SIZE_UNIT (t)
6324 && TREE_CODE (TYPE_SIZE_UNIT (t)) == INTEGER_CST
6325 && !TREE_OVERFLOW (TYPE_SIZE_UNIT (t))
6326 && !valid_constant_size_p (TYPE_SIZE_UNIT (t)))
6327 error ("size of type %qT is too large (%qE bytes)", t, TYPE_SIZE_UNIT (t));
6329 /* Warn about bases that can't be talked about due to ambiguity. */
6330 warn_about_ambiguous_bases (t);
6332 /* Now that we're done with layout, give the base fields the real types. */
6333 for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
6334 if (DECL_ARTIFICIAL (field) && IS_FAKE_BASE_TYPE (TREE_TYPE (field)))
6335 TREE_TYPE (field) = TYPE_CONTEXT (TREE_TYPE (field));
6337 /* Clean up. */
6338 splay_tree_delete (empty_base_offsets);
6340 if (CLASSTYPE_EMPTY_P (t)
6341 && tree_int_cst_lt (sizeof_biggest_empty_class,
6342 TYPE_SIZE_UNIT (t)))
6343 sizeof_biggest_empty_class = TYPE_SIZE_UNIT (t);
6346 /* Determine the "key method" for the class type indicated by TYPE,
6347 and set CLASSTYPE_KEY_METHOD accordingly. */
6349 void
6350 determine_key_method (tree type)
6352 tree method;
6354 if (processing_template_decl
6355 || CLASSTYPE_TEMPLATE_INSTANTIATION (type)
6356 || CLASSTYPE_INTERFACE_KNOWN (type))
6357 return;
6359 /* The key method is the first non-pure virtual function that is not
6360 inline at the point of class definition. On some targets the
6361 key function may not be inline; those targets should not call
6362 this function until the end of the translation unit. */
6363 for (method = TYPE_FIELDS (type); method; method = DECL_CHAIN (method))
6364 if (TREE_CODE (method) == FUNCTION_DECL
6365 && DECL_VINDEX (method) != NULL_TREE
6366 && ! DECL_DECLARED_INLINE_P (method)
6367 && ! DECL_PURE_VIRTUAL_P (method))
6369 CLASSTYPE_KEY_METHOD (type) = method;
6370 break;
6373 return;
6376 /* Helper of find_flexarrays. Return true when FLD refers to a non-static
6377 class data member of non-zero size, otherwise false. */
6379 static inline bool
6380 field_nonempty_p (const_tree fld)
6382 if (TREE_CODE (fld) == ERROR_MARK)
6383 return false;
6385 tree type = TREE_TYPE (fld);
6386 if (TREE_CODE (fld) == FIELD_DECL
6387 && TREE_CODE (type) != ERROR_MARK
6388 && (DECL_NAME (fld) || RECORD_OR_UNION_TYPE_P (type)))
6390 return TYPE_SIZE (type)
6391 && (TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST
6392 || !tree_int_cst_equal (size_zero_node, TYPE_SIZE (type)));
6395 return false;
6398 /* Used by find_flexarrays and related functions. */
6400 struct flexmems_t
6402 /* The first flexible array member or non-zero array member found
6403 in the order of layout. */
6404 tree array;
6405 /* First non-static non-empty data member in the class or its bases. */
6406 tree first;
6407 /* The first non-static non-empty data member following either
6408 the flexible array member, if found, or the zero-length array member
6409 otherwise. AFTER[1] refers to the first such data member of a union
6410 of which the struct containing the flexible array member or zero-length
6411 array is a member, or NULL when no such union exists. This element is
6412 only used during searching, not for diagnosing problems. AFTER[0]
6413 refers to the first such data member that is not a member of such
6414 a union. */
6415 tree after[2];
6417 /* Refers to a struct (not union) in which the struct of which the flexible
6418 array is member is defined. Used to diagnose strictly (according to C)
6419 invalid uses of the latter structs. */
6420 tree enclosing;
6423 /* Find either the first flexible array member or the first zero-length
6424 array, in that order of preference, among members of class T (but not
6425 its base classes), and set members of FMEM accordingly.
6426 BASE_P is true if T is a base class of another class.
6427 PUN is set to the outermost union in which the flexible array member
6428 (or zero-length array) is defined if one such union exists, otherwise
6429 to NULL.
6430 Similarly, PSTR is set to a data member of the outermost struct of
6431 which the flexible array is a member if one such struct exists,
6432 otherwise to NULL. */
6434 static void
6435 find_flexarrays (tree t, flexmems_t *fmem, bool base_p,
6436 tree pun /* = NULL_TREE */,
6437 tree pstr /* = NULL_TREE */)
6439 /* Set the "pointer" to the outermost enclosing union if not set
6440 yet and maintain it for the remainder of the recursion. */
6441 if (!pun && TREE_CODE (t) == UNION_TYPE)
6442 pun = t;
6444 for (tree fld = TYPE_FIELDS (t); fld; fld = DECL_CHAIN (fld))
6446 if (fld == error_mark_node)
6447 return;
6449 /* Is FLD a typedef for an anonymous struct? */
6451 /* FIXME: Note that typedefs (as well as arrays) need to be fully
6452 handled elsewhere so that errors like the following are detected
6453 as well:
6454 typedef struct { int i, a[], j; } S; // bug c++/72753
6455 S s [2]; // bug c++/68489
6457 if (TREE_CODE (fld) == TYPE_DECL
6458 && DECL_IMPLICIT_TYPEDEF_P (fld)
6459 && CLASS_TYPE_P (TREE_TYPE (fld))
6460 && anon_aggrname_p (DECL_NAME (fld)))
6462 /* Check the nested unnamed type referenced via a typedef
6463 independently of FMEM (since it's not a data member of
6464 the enclosing class). */
6465 check_flexarrays (TREE_TYPE (fld));
6466 continue;
6469 /* Skip anything that's GCC-generated or not a (non-static) data
6470 member. */
6471 if (DECL_ARTIFICIAL (fld) || TREE_CODE (fld) != FIELD_DECL)
6472 continue;
6474 /* Type of the member. */
6475 tree fldtype = TREE_TYPE (fld);
6476 if (fldtype == error_mark_node)
6477 return;
6479 /* Determine the type of the array element or object referenced
6480 by the member so that it can be checked for flexible array
6481 members if it hasn't been yet. */
6482 tree eltype = fldtype;
6483 while (TREE_CODE (eltype) == ARRAY_TYPE
6484 || INDIRECT_TYPE_P (eltype))
6485 eltype = TREE_TYPE (eltype);
6487 if (RECORD_OR_UNION_TYPE_P (eltype))
6489 if (fmem->array && !fmem->after[bool (pun)])
6491 /* Once the member after the flexible array has been found
6492 we're done. */
6493 fmem->after[bool (pun)] = fld;
6494 break;
6497 if (eltype == fldtype || TYPE_UNNAMED_P (eltype))
6499 /* Descend into the non-static member struct or union and try
6500 to find a flexible array member or zero-length array among
6501 its members. This is only necessary for anonymous types
6502 and types in whose context the current type T has not been
6503 defined (the latter must not be checked again because they
6504 are already in the process of being checked by one of the
6505 recursive calls). */
6507 tree first = fmem->first;
6508 tree array = fmem->array;
6510 /* If this member isn't anonymous and a prior non-flexible array
6511 member has been seen in one of the enclosing structs, clear
6512 the FIRST member since it doesn't contribute to the flexible
6513 array struct's members. */
6514 if (first && !array && !ANON_AGGR_TYPE_P (eltype))
6515 fmem->first = NULL_TREE;
6517 find_flexarrays (eltype, fmem, false, pun,
6518 !pstr && TREE_CODE (t) == RECORD_TYPE ? fld : pstr);
6520 if (fmem->array != array)
6521 continue;
6523 if (first && !array && !ANON_AGGR_TYPE_P (eltype))
6525 /* Restore the FIRST member reset above if no flexible
6526 array member has been found in this member's struct. */
6527 fmem->first = first;
6530 /* If the member struct contains the first flexible array
6531 member, or if this member is a base class, continue to
6532 the next member and avoid setting the FMEM->NEXT pointer
6533 to point to it. */
6534 if (base_p)
6535 continue;
6539 if (field_nonempty_p (fld))
6541 /* Remember the first non-static data member. */
6542 if (!fmem->first)
6543 fmem->first = fld;
6545 /* Remember the first non-static data member after the flexible
6546 array member, if one has been found, or the zero-length array
6547 if it has been found. */
6548 if (fmem->array && !fmem->after[bool (pun)])
6549 fmem->after[bool (pun)] = fld;
6552 /* Skip non-arrays. */
6553 if (TREE_CODE (fldtype) != ARRAY_TYPE)
6554 continue;
6556 /* Determine the upper bound of the array if it has one. */
6557 if (TYPE_DOMAIN (fldtype))
6559 if (fmem->array)
6561 /* Make a record of the zero-length array if either one
6562 such field or a flexible array member has been seen to
6563 handle the pathological and unlikely case of multiple
6564 such members. */
6565 if (!fmem->after[bool (pun)])
6566 fmem->after[bool (pun)] = fld;
6568 else if (integer_all_onesp (TYPE_MAX_VALUE (TYPE_DOMAIN (fldtype))))
6570 /* Remember the first zero-length array unless a flexible array
6571 member has already been seen. */
6572 fmem->array = fld;
6573 fmem->enclosing = pstr;
6576 else
6578 /* Flexible array members have no upper bound. */
6579 if (fmem->array)
6581 if (TYPE_DOMAIN (TREE_TYPE (fmem->array)))
6583 /* Replace the zero-length array if it's been stored and
6584 reset the after pointer. */
6585 fmem->after[bool (pun)] = NULL_TREE;
6586 fmem->array = fld;
6587 fmem->enclosing = pstr;
6589 else if (!fmem->after[bool (pun)])
6590 /* Make a record of another flexible array member. */
6591 fmem->after[bool (pun)] = fld;
6593 else
6595 fmem->array = fld;
6596 fmem->enclosing = pstr;
6602 /* Diagnose a strictly (by the C standard) invalid use of a struct with
6603 a flexible array member (or the zero-length array extension). */
6605 static void
6606 diagnose_invalid_flexarray (const flexmems_t *fmem)
6608 if (fmem->array && fmem->enclosing
6609 && pedwarn (location_of (fmem->enclosing), OPT_Wpedantic,
6610 TYPE_DOMAIN (TREE_TYPE (fmem->array))
6611 ? G_("invalid use of %q#T with a zero-size array "
6612 "in %q#D")
6613 : G_("invalid use of %q#T with a flexible array member "
6614 "in %q#T"),
6615 DECL_CONTEXT (fmem->array),
6616 DECL_CONTEXT (fmem->enclosing)))
6617 inform (DECL_SOURCE_LOCATION (fmem->array),
6618 "array member %q#D declared here", fmem->array);
6621 /* Issue diagnostics for invalid flexible array members or zero-length
6622 arrays that are not the last elements of the containing class or its
6623 base classes or that are its sole members. */
6625 static void
6626 diagnose_flexarrays (tree t, const flexmems_t *fmem)
6628 if (!fmem->array)
6629 return;
6631 if (fmem->first && !fmem->after[0])
6633 diagnose_invalid_flexarray (fmem);
6634 return;
6637 /* Has a diagnostic been issued? */
6638 bool diagd = false;
6640 const char *msg = 0;
6642 if (TYPE_DOMAIN (TREE_TYPE (fmem->array)))
6644 if (fmem->after[0])
6645 msg = G_("zero-size array member %qD not at end of %q#T");
6646 else if (!fmem->first)
6647 msg = G_("zero-size array member %qD in an otherwise empty %q#T");
6649 if (msg)
6651 location_t loc = DECL_SOURCE_LOCATION (fmem->array);
6653 if (pedwarn (loc, OPT_Wpedantic, msg, fmem->array, t))
6655 inform (location_of (t), "in the definition of %q#T", t);
6656 diagd = true;
6660 else
6662 if (fmem->after[0])
6663 msg = G_("flexible array member %qD not at end of %q#T");
6664 else if (!fmem->first)
6665 msg = G_("flexible array member %qD in an otherwise empty %q#T");
6667 if (msg)
6669 location_t loc = DECL_SOURCE_LOCATION (fmem->array);
6670 diagd = true;
6672 error_at (loc, msg, fmem->array, t);
6674 /* In the unlikely event that the member following the flexible
6675 array member is declared in a different class, or the member
6676 overlaps another member of a common union, point to it.
6677 Otherwise it should be obvious. */
6678 if (fmem->after[0]
6679 && ((DECL_CONTEXT (fmem->after[0])
6680 != DECL_CONTEXT (fmem->array))))
6682 inform (DECL_SOURCE_LOCATION (fmem->after[0]),
6683 "next member %q#D declared here",
6684 fmem->after[0]);
6685 inform (location_of (t), "in the definition of %q#T", t);
6690 if (!diagd && fmem->array && fmem->enclosing)
6691 diagnose_invalid_flexarray (fmem);
6695 /* Recursively check to make sure that any flexible array or zero-length
6696 array members of class T or its bases are valid (i.e., not the sole
6697 non-static data member of T and, if one exists, that it is the last
6698 non-static data member of T and its base classes. FMEM is expected
6699 to be initially null and is used internally by recursive calls to
6700 the function. Issue the appropriate diagnostics for the array member
6701 that fails the checks. */
6703 static void
6704 check_flexarrays (tree t, flexmems_t *fmem /* = NULL */,
6705 bool base_p /* = false */)
6707 /* Initialize the result of a search for flexible array and zero-length
6708 array members. Avoid doing any work if the most interesting FMEM data
6709 have already been populated. */
6710 flexmems_t flexmems = flexmems_t ();
6711 if (!fmem)
6712 fmem = &flexmems;
6713 else if (fmem->array && fmem->first && fmem->after[0])
6714 return;
6716 tree fam = fmem->array;
6718 /* Recursively check the primary base class first. */
6719 if (CLASSTYPE_HAS_PRIMARY_BASE_P (t))
6721 tree basetype = BINFO_TYPE (CLASSTYPE_PRIMARY_BINFO (t));
6722 check_flexarrays (basetype, fmem, true);
6725 /* Recursively check the base classes. */
6726 int nbases = TYPE_BINFO (t) ? BINFO_N_BASE_BINFOS (TYPE_BINFO (t)) : 0;
6727 for (int i = 0; i < nbases; ++i)
6729 tree base_binfo = BINFO_BASE_BINFO (TYPE_BINFO (t), i);
6731 /* The primary base class was already checked above. */
6732 if (base_binfo == CLASSTYPE_PRIMARY_BINFO (t))
6733 continue;
6735 /* Virtual base classes are at the end. */
6736 if (BINFO_VIRTUAL_P (base_binfo))
6737 continue;
6739 /* Check the base class. */
6740 check_flexarrays (BINFO_TYPE (base_binfo), fmem, /*base_p=*/true);
6743 if (fmem == &flexmems)
6745 /* Check virtual base classes only once per derived class.
6746 I.e., this check is not performed recursively for base
6747 classes. */
6748 int i;
6749 tree base_binfo;
6750 vec<tree, va_gc> *vbases;
6751 for (vbases = CLASSTYPE_VBASECLASSES (t), i = 0;
6752 vec_safe_iterate (vbases, i, &base_binfo); i++)
6754 /* Check the virtual base class. */
6755 tree basetype = TREE_TYPE (base_binfo);
6757 check_flexarrays (basetype, fmem, /*base_p=*/true);
6761 /* Is the type unnamed (and therefore a member of it potentially
6762 an anonymous struct or union)? */
6763 bool maybe_anon_p = TYPE_UNNAMED_P (t);
6765 /* Search the members of the current (possibly derived) class, skipping
6766 unnamed structs and unions since those could be anonymous. */
6767 if (fmem != &flexmems || !maybe_anon_p)
6768 find_flexarrays (t, fmem, base_p || fam != fmem->array);
6770 if (fmem == &flexmems && !maybe_anon_p)
6772 /* Issue diagnostics for invalid flexible and zero-length array
6773 members found in base classes or among the members of the current
6774 class. Ignore anonymous structs and unions whose members are
6775 considered to be members of the enclosing class and thus will
6776 be diagnosed when checking it. */
6777 diagnose_flexarrays (t, fmem);
6781 /* Perform processing required when the definition of T (a class type)
6782 is complete. Diagnose invalid definitions of flexible array members
6783 and zero-size arrays. */
6785 void
6786 finish_struct_1 (tree t)
6788 tree x;
6789 /* A TREE_LIST. The TREE_VALUE of each node is a FUNCTION_DECL. */
6790 tree virtuals = NULL_TREE;
6792 if (COMPLETE_TYPE_P (t))
6794 gcc_assert (MAYBE_CLASS_TYPE_P (t));
6795 error ("redefinition of %q#T", t);
6796 popclass ();
6797 return;
6800 /* If this type was previously laid out as a forward reference,
6801 make sure we lay it out again. */
6802 TYPE_SIZE (t) = NULL_TREE;
6803 CLASSTYPE_PRIMARY_BINFO (t) = NULL_TREE;
6805 /* Make assumptions about the class; we'll reset the flags if
6806 necessary. */
6807 CLASSTYPE_EMPTY_P (t) = 1;
6808 CLASSTYPE_NEARLY_EMPTY_P (t) = 1;
6809 CLASSTYPE_CONTAINS_EMPTY_CLASS_P (t) = 0;
6810 CLASSTYPE_LITERAL_P (t) = true;
6812 /* Do end-of-class semantic processing: checking the validity of the
6813 bases and members and add implicitly generated methods. */
6814 check_bases_and_members (t);
6816 /* Find the key method. */
6817 if (TYPE_CONTAINS_VPTR_P (t))
6819 /* The Itanium C++ ABI permits the key method to be chosen when
6820 the class is defined -- even though the key method so
6821 selected may later turn out to be an inline function. On
6822 some systems (such as ARM Symbian OS) the key method cannot
6823 be determined until the end of the translation unit. On such
6824 systems, we leave CLASSTYPE_KEY_METHOD set to NULL, which
6825 will cause the class to be added to KEYED_CLASSES. Then, in
6826 finish_file we will determine the key method. */
6827 if (targetm.cxx.key_method_may_be_inline ())
6828 determine_key_method (t);
6830 /* If a polymorphic class has no key method, we may emit the vtable
6831 in every translation unit where the class definition appears. If
6832 we're devirtualizing, we can look into the vtable even if we
6833 aren't emitting it. */
6834 if (!CLASSTYPE_KEY_METHOD (t))
6835 vec_safe_push (keyed_classes, t);
6838 /* Layout the class itself. */
6839 layout_class_type (t, &virtuals);
6840 /* COMPLETE_TYPE_P is now true. */
6842 set_class_bindings (t);
6844 /* With the layout complete, check for flexible array members and
6845 zero-length arrays that might overlap other members in the final
6846 layout. */
6847 check_flexarrays (t);
6849 virtuals = modify_all_vtables (t, nreverse (virtuals));
6851 /* If necessary, create the primary vtable for this class. */
6852 if (virtuals || TYPE_CONTAINS_VPTR_P (t))
6854 /* We must enter these virtuals into the table. */
6855 if (!CLASSTYPE_HAS_PRIMARY_BASE_P (t))
6856 build_primary_vtable (NULL_TREE, t);
6857 else if (! BINFO_NEW_VTABLE_MARKED (TYPE_BINFO (t)))
6858 /* Here we know enough to change the type of our virtual
6859 function table, but we will wait until later this function. */
6860 build_primary_vtable (CLASSTYPE_PRIMARY_BINFO (t), t);
6862 /* If we're warning about ABI tags, check the types of the new
6863 virtual functions. */
6864 if (warn_abi_tag)
6865 for (tree v = virtuals; v; v = TREE_CHAIN (v))
6866 check_abi_tags (t, TREE_VALUE (v));
6869 if (TYPE_CONTAINS_VPTR_P (t))
6871 int vindex;
6872 tree fn;
6874 if (BINFO_VTABLE (TYPE_BINFO (t)))
6875 gcc_assert (DECL_VIRTUAL_P (BINFO_VTABLE (TYPE_BINFO (t))));
6876 if (!CLASSTYPE_HAS_PRIMARY_BASE_P (t))
6877 gcc_assert (BINFO_VIRTUALS (TYPE_BINFO (t)) == NULL_TREE);
6879 /* Add entries for virtual functions introduced by this class. */
6880 BINFO_VIRTUALS (TYPE_BINFO (t))
6881 = chainon (BINFO_VIRTUALS (TYPE_BINFO (t)), virtuals);
6883 /* Set DECL_VINDEX for all functions declared in this class. */
6884 for (vindex = 0, fn = BINFO_VIRTUALS (TYPE_BINFO (t));
6886 fn = TREE_CHAIN (fn),
6887 vindex += (TARGET_VTABLE_USES_DESCRIPTORS
6888 ? TARGET_VTABLE_USES_DESCRIPTORS : 1))
6890 tree fndecl = BV_FN (fn);
6892 if (DECL_THUNK_P (fndecl))
6893 /* A thunk. We should never be calling this entry directly
6894 from this vtable -- we'd use the entry for the non
6895 thunk base function. */
6896 DECL_VINDEX (fndecl) = NULL_TREE;
6897 else if (TREE_CODE (DECL_VINDEX (fndecl)) != INTEGER_CST)
6898 DECL_VINDEX (fndecl) = build_int_cst (NULL_TREE, vindex);
6902 finish_struct_bits (t);
6904 set_method_tm_attributes (t);
6905 if (flag_openmp || flag_openmp_simd)
6906 finish_omp_declare_simd_methods (t);
6908 /* Clear DECL_IN_AGGR_P for all member functions. Complete the rtl
6909 for any static member objects of the type we're working on. */
6910 for (x = TYPE_FIELDS (t); x; x = DECL_CHAIN (x))
6911 if (DECL_DECLARES_FUNCTION_P (x))
6912 DECL_IN_AGGR_P (x) = false;
6913 else if (VAR_P (x) && TREE_STATIC (x)
6914 && TREE_TYPE (x) != error_mark_node
6915 && same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (x)), t))
6916 SET_DECL_MODE (x, TYPE_MODE (t));
6918 /* Complain if one of the field types requires lower visibility. */
6919 constrain_class_visibility (t);
6921 /* Make the rtl for any new vtables we have created, and unmark
6922 the base types we marked. */
6923 finish_vtbls (t);
6925 /* Build the VTT for T. */
6926 build_vtt (t);
6928 if (warn_nonvdtor
6929 && TYPE_POLYMORPHIC_P (t) && accessible_nvdtor_p (t)
6930 && !CLASSTYPE_FINAL (t))
6931 warning (OPT_Wnon_virtual_dtor,
6932 "%q#T has virtual functions and accessible"
6933 " non-virtual destructor", t);
6935 complete_vars (t);
6937 if (warn_overloaded_virtual)
6938 warn_hidden (t);
6940 /* Class layout, assignment of virtual table slots, etc., is now
6941 complete. Give the back end a chance to tweak the visibility of
6942 the class or perform any other required target modifications. */
6943 targetm.cxx.adjust_class_at_definition (t);
6945 maybe_suppress_debug_info (t);
6947 if (flag_vtable_verify)
6948 vtv_save_class_info (t);
6950 dump_class_hierarchy (t);
6952 /* Finish debugging output for this type. */
6953 rest_of_type_compilation (t, ! LOCAL_CLASS_P (t));
6955 if (TYPE_TRANSPARENT_AGGR (t))
6957 tree field = first_field (t);
6958 if (field == NULL_TREE || error_operand_p (field))
6960 error ("type transparent %q#T does not have any fields", t);
6961 TYPE_TRANSPARENT_AGGR (t) = 0;
6963 else if (DECL_ARTIFICIAL (field))
6965 if (DECL_FIELD_IS_BASE (field))
6966 error ("type transparent class %qT has base classes", t);
6967 else
6969 gcc_checking_assert (DECL_VIRTUAL_P (field));
6970 error ("type transparent class %qT has virtual functions", t);
6972 TYPE_TRANSPARENT_AGGR (t) = 0;
6974 else if (TYPE_MODE (t) != DECL_MODE (field))
6976 error ("type transparent %q#T cannot be made transparent because "
6977 "the type of the first field has a different ABI from the "
6978 "class overall", t);
6979 TYPE_TRANSPARENT_AGGR (t) = 0;
6984 /* When T was built up, the member declarations were added in reverse
6985 order. Rearrange them to declaration order. */
6987 void
6988 unreverse_member_declarations (tree t)
6990 tree next;
6991 tree prev;
6992 tree x;
6994 /* The following lists are all in reverse order. Put them in
6995 declaration order now. */
6996 CLASSTYPE_DECL_LIST (t) = nreverse (CLASSTYPE_DECL_LIST (t));
6998 /* For the TYPE_FIELDS, only the non TYPE_DECLs are in reverse
6999 order, so we can't just use nreverse. Due to stat_hack
7000 chicanery in finish_member_declaration. */
7001 prev = NULL_TREE;
7002 for (x = TYPE_FIELDS (t);
7003 x && TREE_CODE (x) != TYPE_DECL;
7004 x = next)
7006 next = DECL_CHAIN (x);
7007 DECL_CHAIN (x) = prev;
7008 prev = x;
7011 if (prev)
7013 DECL_CHAIN (TYPE_FIELDS (t)) = x;
7014 TYPE_FIELDS (t) = prev;
7018 tree
7019 finish_struct (tree t, tree attributes)
7021 location_t saved_loc = input_location;
7023 /* Now that we've got all the field declarations, reverse everything
7024 as necessary. */
7025 unreverse_member_declarations (t);
7027 cplus_decl_attributes (&t, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
7028 fixup_attribute_variants (t);
7030 /* Nadger the current location so that diagnostics point to the start of
7031 the struct, not the end. */
7032 input_location = DECL_SOURCE_LOCATION (TYPE_NAME (t));
7034 if (processing_template_decl)
7036 tree x;
7038 /* We need to add the target functions of USING_DECLS, so that
7039 they can be found when the using declaration is not
7040 instantiated yet. */
7041 for (x = TYPE_FIELDS (t); x; x = DECL_CHAIN (x))
7042 if (TREE_CODE (x) == USING_DECL)
7044 tree fn = strip_using_decl (x);
7045 if (OVL_P (fn))
7046 for (lkp_iterator iter (fn); iter; ++iter)
7047 add_method (t, *iter, true);
7049 else if (DECL_DECLARES_FUNCTION_P (x))
7050 DECL_IN_AGGR_P (x) = false;
7052 TYPE_SIZE (t) = bitsize_zero_node;
7053 TYPE_SIZE_UNIT (t) = size_zero_node;
7054 /* COMPLETE_TYPE_P is now true. */
7056 set_class_bindings (t);
7058 /* We need to emit an error message if this type was used as a parameter
7059 and it is an abstract type, even if it is a template. We construct
7060 a simple CLASSTYPE_PURE_VIRTUALS list without taking bases into
7061 account and we call complete_vars with this type, which will check
7062 the PARM_DECLS. Note that while the type is being defined,
7063 CLASSTYPE_PURE_VIRTUALS contains the list of the inline friends
7064 (see CLASSTYPE_INLINE_FRIENDS) so we need to clear it. */
7065 CLASSTYPE_PURE_VIRTUALS (t) = NULL;
7066 for (x = TYPE_FIELDS (t); x; x = DECL_CHAIN (x))
7067 if (TREE_CODE (x) == FUNCTION_DECL && DECL_PURE_VIRTUAL_P (x))
7068 vec_safe_push (CLASSTYPE_PURE_VIRTUALS (t), x);
7069 complete_vars (t);
7071 /* Remember current #pragma pack value. */
7072 TYPE_PRECISION (t) = maximum_field_alignment;
7074 /* Fix up any variants we've already built. */
7075 for (x = TYPE_NEXT_VARIANT (t); x; x = TYPE_NEXT_VARIANT (x))
7077 TYPE_SIZE (x) = TYPE_SIZE (t);
7078 TYPE_SIZE_UNIT (x) = TYPE_SIZE_UNIT (t);
7079 TYPE_FIELDS (x) = TYPE_FIELDS (t);
7082 else
7083 finish_struct_1 (t);
7084 /* COMPLETE_TYPE_P is now true. */
7086 maybe_warn_about_overly_private_class (t);
7088 if (is_std_init_list (t))
7090 /* People keep complaining that the compiler crashes on an invalid
7091 definition of initializer_list, so I guess we should explicitly
7092 reject it. What the compiler internals care about is that it's a
7093 template and has a pointer field followed by size_type field. */
7094 bool ok = false;
7095 if (processing_template_decl)
7097 tree f = next_initializable_field (TYPE_FIELDS (t));
7098 if (f && TYPE_PTR_P (TREE_TYPE (f)))
7100 f = next_initializable_field (DECL_CHAIN (f));
7101 if (f && same_type_p (TREE_TYPE (f), size_type_node))
7102 ok = true;
7105 if (!ok)
7106 fatal_error (input_location, "definition of %qD does not match "
7107 "%<#include <initializer_list>%>", TYPE_NAME (t));
7110 input_location = saved_loc;
7112 TYPE_BEING_DEFINED (t) = 0;
7114 if (current_class_type)
7115 popclass ();
7116 else
7117 error ("trying to finish struct, but kicked out due to previous parse errors");
7119 if (processing_template_decl && at_function_scope_p ()
7120 /* Lambdas are defined by the LAMBDA_EXPR. */
7121 && !LAMBDA_TYPE_P (t))
7122 add_stmt (build_min (TAG_DEFN, t));
7124 return t;
7127 /* Hash table to avoid endless recursion when handling references. */
7128 static hash_table<nofree_ptr_hash<tree_node> > *fixed_type_or_null_ref_ht;
7130 /* Return the dynamic type of INSTANCE, if known.
7131 Used to determine whether the virtual function table is needed
7132 or not.
7134 *NONNULL is set iff INSTANCE can be known to be nonnull, regardless
7135 of our knowledge of its type. *NONNULL should be initialized
7136 before this function is called. */
7138 static tree
7139 fixed_type_or_null (tree instance, int *nonnull, int *cdtorp)
7141 #define RECUR(T) fixed_type_or_null((T), nonnull, cdtorp)
7143 switch (TREE_CODE (instance))
7145 case INDIRECT_REF:
7146 if (INDIRECT_TYPE_P (TREE_TYPE (instance)))
7147 return NULL_TREE;
7148 else
7149 return RECUR (TREE_OPERAND (instance, 0));
7151 case CALL_EXPR:
7152 /* This is a call to a constructor, hence it's never zero. */
7153 if (CALL_EXPR_FN (instance)
7154 && TREE_HAS_CONSTRUCTOR (instance))
7156 if (nonnull)
7157 *nonnull = 1;
7158 return TREE_TYPE (instance);
7160 return NULL_TREE;
7162 case SAVE_EXPR:
7163 /* This is a call to a constructor, hence it's never zero. */
7164 if (TREE_HAS_CONSTRUCTOR (instance))
7166 if (nonnull)
7167 *nonnull = 1;
7168 return TREE_TYPE (instance);
7170 return RECUR (TREE_OPERAND (instance, 0));
7172 case POINTER_PLUS_EXPR:
7173 case PLUS_EXPR:
7174 case MINUS_EXPR:
7175 if (TREE_CODE (TREE_OPERAND (instance, 0)) == ADDR_EXPR)
7176 return RECUR (TREE_OPERAND (instance, 0));
7177 if (TREE_CODE (TREE_OPERAND (instance, 1)) == INTEGER_CST)
7178 /* Propagate nonnull. */
7179 return RECUR (TREE_OPERAND (instance, 0));
7181 return NULL_TREE;
7183 CASE_CONVERT:
7184 return RECUR (TREE_OPERAND (instance, 0));
7186 case ADDR_EXPR:
7187 instance = TREE_OPERAND (instance, 0);
7188 if (nonnull)
7190 /* Just because we see an ADDR_EXPR doesn't mean we're dealing
7191 with a real object -- given &p->f, p can still be null. */
7192 tree t = get_base_address (instance);
7193 /* ??? Probably should check DECL_WEAK here. */
7194 if (t && DECL_P (t))
7195 *nonnull = 1;
7197 return RECUR (instance);
7199 case COMPONENT_REF:
7200 /* If this component is really a base class reference, then the field
7201 itself isn't definitive. */
7202 if (DECL_FIELD_IS_BASE (TREE_OPERAND (instance, 1)))
7203 return RECUR (TREE_OPERAND (instance, 0));
7204 return RECUR (TREE_OPERAND (instance, 1));
7206 case VAR_DECL:
7207 case FIELD_DECL:
7208 if (TREE_CODE (TREE_TYPE (instance)) == ARRAY_TYPE
7209 && MAYBE_CLASS_TYPE_P (TREE_TYPE (TREE_TYPE (instance))))
7211 if (nonnull)
7212 *nonnull = 1;
7213 return TREE_TYPE (TREE_TYPE (instance));
7215 /* fall through. */
7216 case TARGET_EXPR:
7217 case PARM_DECL:
7218 case RESULT_DECL:
7219 if (MAYBE_CLASS_TYPE_P (TREE_TYPE (instance)))
7221 if (nonnull)
7222 *nonnull = 1;
7223 return TREE_TYPE (instance);
7225 else if (instance == current_class_ptr)
7227 if (nonnull)
7228 *nonnull = 1;
7230 /* if we're in a ctor or dtor, we know our type. If
7231 current_class_ptr is set but we aren't in a function, we're in
7232 an NSDMI (and therefore a constructor). */
7233 if (current_scope () != current_function_decl
7234 || (DECL_LANG_SPECIFIC (current_function_decl)
7235 && (DECL_CONSTRUCTOR_P (current_function_decl)
7236 || DECL_DESTRUCTOR_P (current_function_decl))))
7238 if (cdtorp)
7239 *cdtorp = 1;
7240 return TREE_TYPE (TREE_TYPE (instance));
7243 else if (TYPE_REF_P (TREE_TYPE (instance)))
7245 /* We only need one hash table because it is always left empty. */
7246 if (!fixed_type_or_null_ref_ht)
7247 fixed_type_or_null_ref_ht
7248 = new hash_table<nofree_ptr_hash<tree_node> > (37);
7250 /* Reference variables should be references to objects. */
7251 if (nonnull)
7252 *nonnull = 1;
7254 /* Enter the INSTANCE in a table to prevent recursion; a
7255 variable's initializer may refer to the variable
7256 itself. */
7257 if (VAR_P (instance)
7258 && DECL_INITIAL (instance)
7259 && !type_dependent_expression_p_push (DECL_INITIAL (instance))
7260 && !fixed_type_or_null_ref_ht->find (instance))
7262 tree type;
7263 tree_node **slot;
7265 slot = fixed_type_or_null_ref_ht->find_slot (instance, INSERT);
7266 *slot = instance;
7267 type = RECUR (DECL_INITIAL (instance));
7268 fixed_type_or_null_ref_ht->remove_elt (instance);
7270 return type;
7273 return NULL_TREE;
7275 default:
7276 return NULL_TREE;
7278 #undef RECUR
7281 /* Return nonzero if the dynamic type of INSTANCE is known, and
7282 equivalent to the static type. We also handle the case where
7283 INSTANCE is really a pointer. Return negative if this is a
7284 ctor/dtor. There the dynamic type is known, but this might not be
7285 the most derived base of the original object, and hence virtual
7286 bases may not be laid out according to this type.
7288 Used to determine whether the virtual function table is needed
7289 or not.
7291 *NONNULL is set iff INSTANCE can be known to be nonnull, regardless
7292 of our knowledge of its type. *NONNULL should be initialized
7293 before this function is called. */
7296 resolves_to_fixed_type_p (tree instance, int* nonnull)
7298 tree t = TREE_TYPE (instance);
7299 int cdtorp = 0;
7300 tree fixed;
7302 /* processing_template_decl can be false in a template if we're in
7303 instantiate_non_dependent_expr, but we still want to suppress
7304 this check. */
7305 if (in_template_function ())
7307 /* In a template we only care about the type of the result. */
7308 if (nonnull)
7309 *nonnull = true;
7310 return true;
7313 fixed = fixed_type_or_null (instance, nonnull, &cdtorp);
7314 if (fixed == NULL_TREE)
7315 return 0;
7316 if (INDIRECT_TYPE_P (t))
7317 t = TREE_TYPE (t);
7318 if (!same_type_ignoring_top_level_qualifiers_p (t, fixed))
7319 return 0;
7320 return cdtorp ? -1 : 1;
7324 void
7325 init_class_processing (void)
7327 current_class_depth = 0;
7328 current_class_stack_size = 10;
7329 current_class_stack
7330 = XNEWVEC (struct class_stack_node, current_class_stack_size);
7331 vec_alloc (local_classes, 8);
7332 sizeof_biggest_empty_class = size_zero_node;
7334 ridpointers[(int) RID_PUBLIC] = access_public_node;
7335 ridpointers[(int) RID_PRIVATE] = access_private_node;
7336 ridpointers[(int) RID_PROTECTED] = access_protected_node;
7339 /* Restore the cached PREVIOUS_CLASS_LEVEL. */
7341 static void
7342 restore_class_cache (void)
7344 tree type;
7346 /* We are re-entering the same class we just left, so we don't
7347 have to search the whole inheritance matrix to find all the
7348 decls to bind again. Instead, we install the cached
7349 class_shadowed list and walk through it binding names. */
7350 push_binding_level (previous_class_level);
7351 class_binding_level = previous_class_level;
7352 /* Restore IDENTIFIER_TYPE_VALUE. */
7353 for (type = class_binding_level->type_shadowed;
7354 type;
7355 type = TREE_CHAIN (type))
7356 SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (type), TREE_TYPE (type));
7359 /* Set global variables CURRENT_CLASS_NAME and CURRENT_CLASS_TYPE as
7360 appropriate for TYPE.
7362 So that we may avoid calls to lookup_name, we cache the _TYPE
7363 nodes of local TYPE_DECLs in the TREE_TYPE field of the name.
7365 For multiple inheritance, we perform a two-pass depth-first search
7366 of the type lattice. */
7368 void
7369 pushclass (tree type)
7371 class_stack_node_t csn;
7373 type = TYPE_MAIN_VARIANT (type);
7375 /* Make sure there is enough room for the new entry on the stack. */
7376 if (current_class_depth + 1 >= current_class_stack_size)
7378 current_class_stack_size *= 2;
7379 current_class_stack
7380 = XRESIZEVEC (struct class_stack_node, current_class_stack,
7381 current_class_stack_size);
7384 /* Insert a new entry on the class stack. */
7385 csn = current_class_stack + current_class_depth;
7386 csn->name = current_class_name;
7387 csn->type = current_class_type;
7388 csn->access = current_access_specifier;
7389 csn->names_used = 0;
7390 csn->hidden = 0;
7391 current_class_depth++;
7393 /* Now set up the new type. */
7394 current_class_name = TYPE_NAME (type);
7395 if (TREE_CODE (current_class_name) == TYPE_DECL)
7396 current_class_name = DECL_NAME (current_class_name);
7397 current_class_type = type;
7399 /* By default, things in classes are private, while things in
7400 structures or unions are public. */
7401 current_access_specifier = (CLASSTYPE_DECLARED_CLASS (type)
7402 ? access_private_node
7403 : access_public_node);
7405 if (previous_class_level
7406 && type != previous_class_level->this_entity
7407 && current_class_depth == 1)
7409 /* Forcibly remove any old class remnants. */
7410 invalidate_class_lookup_cache ();
7413 if (!previous_class_level
7414 || type != previous_class_level->this_entity
7415 || current_class_depth > 1)
7416 pushlevel_class ();
7417 else
7418 restore_class_cache ();
7421 /* When we exit a toplevel class scope, we save its binding level so
7422 that we can restore it quickly. Here, we've entered some other
7423 class, so we must invalidate our cache. */
7425 void
7426 invalidate_class_lookup_cache (void)
7428 previous_class_level = NULL;
7431 /* Get out of the current class scope. If we were in a class scope
7432 previously, that is the one popped to. */
7434 void
7435 popclass (void)
7437 poplevel_class ();
7439 current_class_depth--;
7440 current_class_name = current_class_stack[current_class_depth].name;
7441 current_class_type = current_class_stack[current_class_depth].type;
7442 current_access_specifier = current_class_stack[current_class_depth].access;
7443 if (current_class_stack[current_class_depth].names_used)
7444 splay_tree_delete (current_class_stack[current_class_depth].names_used);
7447 /* Mark the top of the class stack as hidden. */
7449 void
7450 push_class_stack (void)
7452 if (current_class_depth)
7453 ++current_class_stack[current_class_depth - 1].hidden;
7456 /* Mark the top of the class stack as un-hidden. */
7458 void
7459 pop_class_stack (void)
7461 if (current_class_depth)
7462 --current_class_stack[current_class_depth - 1].hidden;
7465 /* If the class type currently being defined is either T or
7466 a nested type of T, returns the type from the current_class_stack,
7467 which might be equivalent to but not equal to T in case of
7468 constrained partial specializations. */
7470 tree
7471 currently_open_class (tree t)
7473 int i;
7475 if (!CLASS_TYPE_P (t))
7476 return NULL_TREE;
7478 t = TYPE_MAIN_VARIANT (t);
7480 /* We start looking from 1 because entry 0 is from global scope,
7481 and has no type. */
7482 for (i = current_class_depth; i > 0; --i)
7484 tree c;
7485 if (i == current_class_depth)
7486 c = current_class_type;
7487 else
7489 if (current_class_stack[i].hidden)
7490 break;
7491 c = current_class_stack[i].type;
7493 if (!c)
7494 continue;
7495 if (same_type_p (c, t))
7496 return c;
7498 return NULL_TREE;
7501 /* If either current_class_type or one of its enclosing classes are derived
7502 from T, return the appropriate type. Used to determine how we found
7503 something via unqualified lookup. */
7505 tree
7506 currently_open_derived_class (tree t)
7508 int i;
7510 /* The bases of a dependent type are unknown. */
7511 if (dependent_type_p (t))
7512 return NULL_TREE;
7514 if (!current_class_type)
7515 return NULL_TREE;
7517 if (DERIVED_FROM_P (t, current_class_type))
7518 return current_class_type;
7520 for (i = current_class_depth - 1; i > 0; --i)
7522 if (current_class_stack[i].hidden)
7523 break;
7524 if (DERIVED_FROM_P (t, current_class_stack[i].type))
7525 return current_class_stack[i].type;
7528 return NULL_TREE;
7531 /* Return the outermost enclosing class type that is still open, or
7532 NULL_TREE. */
7534 tree
7535 outermost_open_class (void)
7537 if (!current_class_type)
7538 return NULL_TREE;
7539 tree r = NULL_TREE;
7540 if (TYPE_BEING_DEFINED (current_class_type))
7541 r = current_class_type;
7542 for (int i = current_class_depth - 1; i > 0; --i)
7544 if (current_class_stack[i].hidden)
7545 break;
7546 tree t = current_class_stack[i].type;
7547 if (!TYPE_BEING_DEFINED (t))
7548 break;
7549 r = t;
7551 return r;
7554 /* Returns the innermost class type which is not a lambda closure type. */
7556 tree
7557 current_nonlambda_class_type (void)
7559 tree type = current_class_type;
7560 while (type && LAMBDA_TYPE_P (type))
7561 type = decl_type_context (TYPE_NAME (type));
7562 return type;
7565 /* When entering a class scope, all enclosing class scopes' names with
7566 static meaning (static variables, static functions, types and
7567 enumerators) have to be visible. This recursive function calls
7568 pushclass for all enclosing class contexts until global or a local
7569 scope is reached. TYPE is the enclosed class. */
7571 void
7572 push_nested_class (tree type)
7574 /* A namespace might be passed in error cases, like A::B:C. */
7575 if (type == NULL_TREE
7576 || !CLASS_TYPE_P (type))
7577 return;
7579 push_nested_class (DECL_CONTEXT (TYPE_MAIN_DECL (type)));
7581 pushclass (type);
7584 /* Undoes a push_nested_class call. */
7586 void
7587 pop_nested_class (void)
7589 tree context = DECL_CONTEXT (TYPE_MAIN_DECL (current_class_type));
7591 popclass ();
7592 if (context && CLASS_TYPE_P (context))
7593 pop_nested_class ();
7596 /* Returns the number of extern "LANG" blocks we are nested within. */
7599 current_lang_depth (void)
7601 return vec_safe_length (current_lang_base);
7604 /* Set global variables CURRENT_LANG_NAME to appropriate value
7605 so that behavior of name-mangling machinery is correct. */
7607 void
7608 push_lang_context (tree name)
7610 vec_safe_push (current_lang_base, current_lang_name);
7612 if (name == lang_name_cplusplus)
7613 current_lang_name = name;
7614 else if (name == lang_name_c)
7615 current_lang_name = name;
7616 else
7617 error ("language string %<\"%E\"%> not recognized", name);
7620 /* Get out of the current language scope. */
7622 void
7623 pop_lang_context (void)
7625 current_lang_name = current_lang_base->pop ();
7628 /* Type instantiation routines. */
7630 /* Given an OVERLOAD and a TARGET_TYPE, return the function that
7631 matches the TARGET_TYPE. If there is no satisfactory match, return
7632 error_mark_node, and issue an error & warning messages under
7633 control of FLAGS. Permit pointers to member function if FLAGS
7634 permits. If TEMPLATE_ONLY, the name of the overloaded function was
7635 a template-id, and EXPLICIT_TARGS are the explicitly provided
7636 template arguments.
7638 If OVERLOAD is for one or more member functions, then ACCESS_PATH
7639 is the base path used to reference those member functions. If
7640 the address is resolved to a member function, access checks will be
7641 performed and errors issued if appropriate. */
7643 static tree
7644 resolve_address_of_overloaded_function (tree target_type,
7645 tree overload,
7646 tsubst_flags_t complain,
7647 bool template_only,
7648 tree explicit_targs,
7649 tree access_path)
7651 /* Here's what the standard says:
7653 [over.over]
7655 If the name is a function template, template argument deduction
7656 is done, and if the argument deduction succeeds, the deduced
7657 arguments are used to generate a single template function, which
7658 is added to the set of overloaded functions considered.
7660 Non-member functions and static member functions match targets of
7661 type "pointer-to-function" or "reference-to-function." Nonstatic
7662 member functions match targets of type "pointer-to-member
7663 function;" the function type of the pointer to member is used to
7664 select the member function from the set of overloaded member
7665 functions. If a nonstatic member function is selected, the
7666 reference to the overloaded function name is required to have the
7667 form of a pointer to member as described in 5.3.1.
7669 If more than one function is selected, any template functions in
7670 the set are eliminated if the set also contains a non-template
7671 function, and any given template function is eliminated if the
7672 set contains a second template function that is more specialized
7673 than the first according to the partial ordering rules 14.5.5.2.
7674 After such eliminations, if any, there shall remain exactly one
7675 selected function. */
7677 int is_ptrmem = 0;
7678 /* We store the matches in a TREE_LIST rooted here. The functions
7679 are the TREE_PURPOSE, not the TREE_VALUE, in this list, for easy
7680 interoperability with most_specialized_instantiation. */
7681 tree matches = NULL_TREE;
7682 tree fn;
7683 tree target_fn_type;
7685 /* By the time we get here, we should be seeing only real
7686 pointer-to-member types, not the internal POINTER_TYPE to
7687 METHOD_TYPE representation. */
7688 gcc_assert (!TYPE_PTR_P (target_type)
7689 || TREE_CODE (TREE_TYPE (target_type)) != METHOD_TYPE);
7691 gcc_assert (is_overloaded_fn (overload));
7693 /* Check that the TARGET_TYPE is reasonable. */
7694 if (TYPE_PTRFN_P (target_type)
7695 || TYPE_REFFN_P (target_type))
7696 /* This is OK. */;
7697 else if (TYPE_PTRMEMFUNC_P (target_type))
7698 /* This is OK, too. */
7699 is_ptrmem = 1;
7700 else if (TREE_CODE (target_type) == FUNCTION_TYPE)
7701 /* This is OK, too. This comes from a conversion to reference
7702 type. */
7703 target_type = build_reference_type (target_type);
7704 else
7706 if (complain & tf_error)
7707 error ("cannot resolve overloaded function %qD based on"
7708 " conversion to type %qT",
7709 OVL_NAME (overload), target_type);
7710 return error_mark_node;
7713 /* Non-member functions and static member functions match targets of type
7714 "pointer-to-function" or "reference-to-function." Nonstatic member
7715 functions match targets of type "pointer-to-member-function;" the
7716 function type of the pointer to member is used to select the member
7717 function from the set of overloaded member functions.
7719 So figure out the FUNCTION_TYPE that we want to match against. */
7720 target_fn_type = static_fn_type (target_type);
7722 /* If we can find a non-template function that matches, we can just
7723 use it. There's no point in generating template instantiations
7724 if we're just going to throw them out anyhow. But, of course, we
7725 can only do this when we don't *need* a template function. */
7726 if (!template_only)
7727 for (lkp_iterator iter (overload); iter; ++iter)
7729 tree fn = *iter;
7731 if (TREE_CODE (fn) == TEMPLATE_DECL)
7732 /* We're not looking for templates just yet. */
7733 continue;
7735 if ((TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE) != is_ptrmem)
7736 /* We're looking for a non-static member, and this isn't
7737 one, or vice versa. */
7738 continue;
7740 /* In C++17 we need the noexcept-qualifier to compare types. */
7741 if (flag_noexcept_type
7742 && !maybe_instantiate_noexcept (fn, complain))
7743 continue;
7745 /* See if there's a match. */
7746 tree fntype = static_fn_type (fn);
7747 if (same_type_p (target_fn_type, fntype)
7748 || fnptr_conv_p (target_fn_type, fntype))
7749 matches = tree_cons (fn, NULL_TREE, matches);
7752 /* Now, if we've already got a match (or matches), there's no need
7753 to proceed to the template functions. But, if we don't have a
7754 match we need to look at them, too. */
7755 if (!matches)
7757 tree target_arg_types;
7758 tree target_ret_type;
7759 tree *args;
7760 unsigned int nargs, ia;
7761 tree arg;
7763 target_arg_types = TYPE_ARG_TYPES (target_fn_type);
7764 target_ret_type = TREE_TYPE (target_fn_type);
7766 nargs = list_length (target_arg_types);
7767 args = XALLOCAVEC (tree, nargs);
7768 for (arg = target_arg_types, ia = 0;
7769 arg != NULL_TREE && arg != void_list_node;
7770 arg = TREE_CHAIN (arg), ++ia)
7771 args[ia] = TREE_VALUE (arg);
7772 nargs = ia;
7774 for (lkp_iterator iter (overload); iter; ++iter)
7776 tree fn = *iter;
7777 tree instantiation;
7778 tree targs;
7780 if (TREE_CODE (fn) != TEMPLATE_DECL)
7781 /* We're only looking for templates. */
7782 continue;
7784 if ((TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
7785 != is_ptrmem)
7786 /* We're not looking for a non-static member, and this is
7787 one, or vice versa. */
7788 continue;
7790 tree ret = target_ret_type;
7792 /* If the template has a deduced return type, don't expose it to
7793 template argument deduction. */
7794 if (undeduced_auto_decl (fn))
7795 ret = NULL_TREE;
7797 /* Try to do argument deduction. */
7798 targs = make_tree_vec (DECL_NTPARMS (fn));
7799 instantiation = fn_type_unification (fn, explicit_targs, targs, args,
7800 nargs, ret,
7801 DEDUCE_EXACT, LOOKUP_NORMAL,
7802 false, false);
7803 if (instantiation == error_mark_node)
7804 /* Instantiation failed. */
7805 continue;
7807 /* Constraints must be satisfied. This is done before
7808 return type deduction since that instantiates the
7809 function. */
7810 if (flag_concepts && !constraints_satisfied_p (instantiation))
7811 continue;
7813 /* And now force instantiation to do return type deduction. */
7814 if (undeduced_auto_decl (instantiation))
7816 ++function_depth;
7817 instantiate_decl (instantiation, /*defer*/false, /*class*/false);
7818 --function_depth;
7820 require_deduced_type (instantiation);
7823 /* In C++17 we need the noexcept-qualifier to compare types. */
7824 if (flag_noexcept_type)
7825 maybe_instantiate_noexcept (instantiation, complain);
7827 /* See if there's a match. */
7828 tree fntype = static_fn_type (instantiation);
7829 if (same_type_p (target_fn_type, fntype)
7830 || fnptr_conv_p (target_fn_type, fntype))
7831 matches = tree_cons (instantiation, fn, matches);
7834 /* Now, remove all but the most specialized of the matches. */
7835 if (matches)
7837 tree match = most_specialized_instantiation (matches);
7839 if (match != error_mark_node)
7840 matches = tree_cons (TREE_PURPOSE (match),
7841 NULL_TREE,
7842 NULL_TREE);
7846 /* Now we should have exactly one function in MATCHES. */
7847 if (matches == NULL_TREE)
7849 /* There were *no* matches. */
7850 if (complain & tf_error)
7852 error ("no matches converting function %qD to type %q#T",
7853 OVL_NAME (overload), target_type);
7855 print_candidates (overload);
7857 return error_mark_node;
7859 else if (TREE_CHAIN (matches))
7861 /* There were too many matches. First check if they're all
7862 the same function. */
7863 tree match = NULL_TREE;
7865 fn = TREE_PURPOSE (matches);
7867 /* For multi-versioned functions, more than one match is just fine and
7868 decls_match will return false as they are different. */
7869 for (match = TREE_CHAIN (matches); match; match = TREE_CHAIN (match))
7870 if (!decls_match (fn, TREE_PURPOSE (match))
7871 && !targetm.target_option.function_versions
7872 (fn, TREE_PURPOSE (match)))
7873 break;
7875 if (match)
7877 if (complain & tf_error)
7879 error ("converting overloaded function %qD to type %q#T is ambiguous",
7880 OVL_NAME (overload), target_type);
7882 /* Since print_candidates expects the functions in the
7883 TREE_VALUE slot, we flip them here. */
7884 for (match = matches; match; match = TREE_CHAIN (match))
7885 TREE_VALUE (match) = TREE_PURPOSE (match);
7887 print_candidates (matches);
7890 return error_mark_node;
7894 /* Good, exactly one match. Now, convert it to the correct type. */
7895 fn = TREE_PURPOSE (matches);
7897 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
7898 && !(complain & tf_ptrmem_ok) && !flag_ms_extensions)
7900 static int explained;
7902 if (!(complain & tf_error))
7903 return error_mark_node;
7905 permerror (input_location, "assuming pointer to member %qD", fn);
7906 if (!explained)
7908 inform (input_location, "(a pointer to member can only be formed with %<&%E%>)", fn);
7909 explained = 1;
7913 /* If a pointer to a function that is multi-versioned is requested, the
7914 pointer to the dispatcher function is returned instead. This works
7915 well because indirectly calling the function will dispatch the right
7916 function version at run-time. */
7917 if (DECL_FUNCTION_VERSIONED (fn))
7919 fn = get_function_version_dispatcher (fn);
7920 if (fn == NULL)
7921 return error_mark_node;
7922 /* Mark all the versions corresponding to the dispatcher as used. */
7923 if (!(complain & tf_conv))
7924 mark_versions_used (fn);
7927 /* If we're doing overload resolution purely for the purpose of
7928 determining conversion sequences, we should not consider the
7929 function used. If this conversion sequence is selected, the
7930 function will be marked as used at this point. */
7931 if (!(complain & tf_conv))
7933 /* Make =delete work with SFINAE. */
7934 if (DECL_DELETED_FN (fn) && !(complain & tf_error))
7935 return error_mark_node;
7936 if (!mark_used (fn, complain) && !(complain & tf_error))
7937 return error_mark_node;
7940 /* We could not check access to member functions when this
7941 expression was originally created since we did not know at that
7942 time to which function the expression referred. */
7943 if (DECL_FUNCTION_MEMBER_P (fn))
7945 gcc_assert (access_path);
7946 perform_or_defer_access_check (access_path, fn, fn, complain);
7949 if (TYPE_PTRFN_P (target_type) || TYPE_PTRMEMFUNC_P (target_type))
7950 return cp_build_addr_expr (fn, complain);
7951 else
7953 /* The target must be a REFERENCE_TYPE. Above, cp_build_unary_op
7954 will mark the function as addressed, but here we must do it
7955 explicitly. */
7956 cxx_mark_addressable (fn);
7958 return fn;
7962 /* This function will instantiate the type of the expression given in
7963 RHS to match the type of LHSTYPE. If errors exist, then return
7964 error_mark_node. COMPLAIN is a bit mask. If TF_ERROR is set, then
7965 we complain on errors. If we are not complaining, never modify rhs,
7966 as overload resolution wants to try many possible instantiations, in
7967 the hope that at least one will work.
7969 For non-recursive calls, LHSTYPE should be a function, pointer to
7970 function, or a pointer to member function. */
7972 tree
7973 instantiate_type (tree lhstype, tree rhs, tsubst_flags_t complain)
7975 tsubst_flags_t complain_in = complain;
7976 tree access_path = NULL_TREE;
7978 complain &= ~tf_ptrmem_ok;
7980 if (lhstype == unknown_type_node)
7982 if (complain & tf_error)
7983 error ("not enough type information");
7984 return error_mark_node;
7987 if (TREE_TYPE (rhs) != NULL_TREE && ! (type_unknown_p (rhs)))
7989 tree fntype = non_reference (lhstype);
7990 if (same_type_p (fntype, TREE_TYPE (rhs)))
7991 return rhs;
7992 if (fnptr_conv_p (fntype, TREE_TYPE (rhs)))
7993 return rhs;
7994 if (flag_ms_extensions
7995 && TYPE_PTRMEMFUNC_P (fntype)
7996 && !TYPE_PTRMEMFUNC_P (TREE_TYPE (rhs)))
7997 /* Microsoft allows `A::f' to be resolved to a
7998 pointer-to-member. */
8000 else
8002 if (complain & tf_error)
8003 error ("cannot convert %qE from type %qT to type %qT",
8004 rhs, TREE_TYPE (rhs), fntype);
8005 return error_mark_node;
8009 /* If we instantiate a template, and it is a A ?: C expression
8010 with omitted B, look through the SAVE_EXPR. */
8011 if (TREE_CODE (rhs) == SAVE_EXPR)
8012 rhs = TREE_OPERAND (rhs, 0);
8014 if (BASELINK_P (rhs))
8016 access_path = BASELINK_ACCESS_BINFO (rhs);
8017 rhs = BASELINK_FUNCTIONS (rhs);
8020 /* If we are in a template, and have a NON_DEPENDENT_EXPR, we cannot
8021 deduce any type information. */
8022 if (TREE_CODE (rhs) == NON_DEPENDENT_EXPR)
8024 if (complain & tf_error)
8025 error ("not enough type information");
8026 return error_mark_node;
8029 /* There are only a few kinds of expressions that may have a type
8030 dependent on overload resolution. */
8031 gcc_assert (TREE_CODE (rhs) == ADDR_EXPR
8032 || TREE_CODE (rhs) == COMPONENT_REF
8033 || is_overloaded_fn (rhs)
8034 || (flag_ms_extensions && TREE_CODE (rhs) == FUNCTION_DECL));
8036 /* This should really only be used when attempting to distinguish
8037 what sort of a pointer to function we have. For now, any
8038 arithmetic operation which is not supported on pointers
8039 is rejected as an error. */
8041 switch (TREE_CODE (rhs))
8043 case COMPONENT_REF:
8045 tree member = TREE_OPERAND (rhs, 1);
8047 member = instantiate_type (lhstype, member, complain);
8048 if (member != error_mark_node
8049 && TREE_SIDE_EFFECTS (TREE_OPERAND (rhs, 0)))
8050 /* Do not lose object's side effects. */
8051 return build2 (COMPOUND_EXPR, TREE_TYPE (member),
8052 TREE_OPERAND (rhs, 0), member);
8053 return member;
8056 case OFFSET_REF:
8057 rhs = TREE_OPERAND (rhs, 1);
8058 if (BASELINK_P (rhs))
8059 return instantiate_type (lhstype, rhs, complain_in);
8061 /* This can happen if we are forming a pointer-to-member for a
8062 member template. */
8063 gcc_assert (TREE_CODE (rhs) == TEMPLATE_ID_EXPR);
8065 /* Fall through. */
8067 case TEMPLATE_ID_EXPR:
8069 tree fns = TREE_OPERAND (rhs, 0);
8070 tree args = TREE_OPERAND (rhs, 1);
8072 return
8073 resolve_address_of_overloaded_function (lhstype, fns, complain_in,
8074 /*template_only=*/true,
8075 args, access_path);
8078 case OVERLOAD:
8079 case FUNCTION_DECL:
8080 return
8081 resolve_address_of_overloaded_function (lhstype, rhs, complain_in,
8082 /*template_only=*/false,
8083 /*explicit_targs=*/NULL_TREE,
8084 access_path);
8086 case ADDR_EXPR:
8088 if (PTRMEM_OK_P (rhs))
8089 complain |= tf_ptrmem_ok;
8091 return instantiate_type (lhstype, TREE_OPERAND (rhs, 0), complain);
8094 case ERROR_MARK:
8095 return error_mark_node;
8097 default:
8098 gcc_unreachable ();
8100 return error_mark_node;
8103 /* Return the name of the virtual function pointer field
8104 (as an IDENTIFIER_NODE) for the given TYPE. Note that
8105 this may have to look back through base types to find the
8106 ultimate field name. (For single inheritance, these could
8107 all be the same name. Who knows for multiple inheritance). */
8109 static tree
8110 get_vfield_name (tree type)
8112 tree binfo, base_binfo;
8114 for (binfo = TYPE_BINFO (type);
8115 BINFO_N_BASE_BINFOS (binfo);
8116 binfo = base_binfo)
8118 base_binfo = BINFO_BASE_BINFO (binfo, 0);
8120 if (BINFO_VIRTUAL_P (base_binfo)
8121 || !TYPE_CONTAINS_VPTR_P (BINFO_TYPE (base_binfo)))
8122 break;
8125 type = BINFO_TYPE (binfo);
8126 tree ctor_name = constructor_name (type);
8127 char *buf = (char *) alloca (sizeof (VFIELD_NAME_FORMAT)
8128 + IDENTIFIER_LENGTH (ctor_name) + 2);
8129 sprintf (buf, VFIELD_NAME_FORMAT, IDENTIFIER_POINTER (ctor_name));
8130 return get_identifier (buf);
8133 /* Build a dummy reference to ourselves so Derived::Base (and A::A) works,
8134 according to [class]:
8135 The class-name is also inserted
8136 into the scope of the class itself. For purposes of access checking,
8137 the inserted class name is treated as if it were a public member name. */
8139 void
8140 build_self_reference (void)
8142 tree name = DECL_NAME (TYPE_NAME (current_class_type));
8143 tree value = build_lang_decl (TYPE_DECL, name, current_class_type);
8145 DECL_NONLOCAL (value) = 1;
8146 DECL_CONTEXT (value) = current_class_type;
8147 DECL_ARTIFICIAL (value) = 1;
8148 SET_DECL_SELF_REFERENCE_P (value);
8149 set_underlying_type (value);
8151 if (processing_template_decl)
8152 value = push_template_decl (value);
8154 tree saved_cas = current_access_specifier;
8155 current_access_specifier = access_public_node;
8156 finish_member_declaration (value);
8157 current_access_specifier = saved_cas;
8160 /* Returns 1 if TYPE contains only padding bytes. */
8163 is_empty_class (tree type)
8165 if (type == error_mark_node)
8166 return 0;
8168 if (! CLASS_TYPE_P (type))
8169 return 0;
8171 return CLASSTYPE_EMPTY_P (type);
8174 /* Returns true if TYPE contains no actual data, just various
8175 possible combinations of empty classes and possibly a vptr. */
8177 bool
8178 is_really_empty_class (tree type)
8180 if (CLASS_TYPE_P (type))
8182 tree field;
8183 tree binfo;
8184 tree base_binfo;
8185 int i;
8187 /* CLASSTYPE_EMPTY_P isn't set properly until the class is actually laid
8188 out, but we'd like to be able to check this before then. */
8189 if (COMPLETE_TYPE_P (type) && is_empty_class (type))
8190 return true;
8192 for (binfo = TYPE_BINFO (type), i = 0;
8193 BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
8194 if (!is_really_empty_class (BINFO_TYPE (base_binfo)))
8195 return false;
8196 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
8197 if (TREE_CODE (field) == FIELD_DECL
8198 && !DECL_ARTIFICIAL (field)
8199 /* An unnamed bit-field is not a data member. */
8200 && !DECL_UNNAMED_BIT_FIELD (field)
8201 && !is_really_empty_class (TREE_TYPE (field)))
8202 return false;
8203 return true;
8205 else if (TREE_CODE (type) == ARRAY_TYPE)
8206 return (integer_zerop (array_type_nelts_top (type))
8207 || is_really_empty_class (TREE_TYPE (type)));
8208 return false;
8211 /* Note that NAME was looked up while the current class was being
8212 defined and that the result of that lookup was DECL. */
8214 void
8215 maybe_note_name_used_in_class (tree name, tree decl)
8217 splay_tree names_used;
8219 /* If we're not defining a class, there's nothing to do. */
8220 if (!(innermost_scope_kind() == sk_class
8221 && TYPE_BEING_DEFINED (current_class_type)
8222 && !LAMBDA_TYPE_P (current_class_type)))
8223 return;
8225 /* If there's already a binding for this NAME, then we don't have
8226 anything to worry about. */
8227 if (lookup_member (current_class_type, name,
8228 /*protect=*/0, /*want_type=*/false, tf_warning_or_error))
8229 return;
8231 if (!current_class_stack[current_class_depth - 1].names_used)
8232 current_class_stack[current_class_depth - 1].names_used
8233 = splay_tree_new (splay_tree_compare_pointers, 0, 0);
8234 names_used = current_class_stack[current_class_depth - 1].names_used;
8236 splay_tree_insert (names_used,
8237 (splay_tree_key) name,
8238 (splay_tree_value) decl);
8241 /* Note that NAME was declared (as DECL) in the current class. Check
8242 to see that the declaration is valid. */
8244 void
8245 note_name_declared_in_class (tree name, tree decl)
8247 splay_tree names_used;
8248 splay_tree_node n;
8250 /* Look to see if we ever used this name. */
8251 names_used
8252 = current_class_stack[current_class_depth - 1].names_used;
8253 if (!names_used)
8254 return;
8255 /* The C language allows members to be declared with a type of the same
8256 name, and the C++ standard says this diagnostic is not required. So
8257 allow it in extern "C" blocks unless predantic is specified.
8258 Allow it in all cases if -ms-extensions is specified. */
8259 if ((!pedantic && current_lang_name == lang_name_c)
8260 || flag_ms_extensions)
8261 return;
8262 n = splay_tree_lookup (names_used, (splay_tree_key) name);
8263 if (n)
8265 /* [basic.scope.class]
8267 A name N used in a class S shall refer to the same declaration
8268 in its context and when re-evaluated in the completed scope of
8269 S. */
8270 permerror (input_location, "declaration of %q#D", decl);
8271 permerror (location_of ((tree) n->value),
8272 "changes meaning of %qD from %q#D",
8273 OVL_NAME (decl), (tree) n->value);
8277 /* Returns the VAR_DECL for the complete vtable associated with BINFO.
8278 Secondary vtables are merged with primary vtables; this function
8279 will return the VAR_DECL for the primary vtable. */
8281 tree
8282 get_vtbl_decl_for_binfo (tree binfo)
8284 tree decl;
8286 decl = BINFO_VTABLE (binfo);
8287 if (decl && TREE_CODE (decl) == POINTER_PLUS_EXPR)
8289 gcc_assert (TREE_CODE (TREE_OPERAND (decl, 0)) == ADDR_EXPR);
8290 decl = TREE_OPERAND (TREE_OPERAND (decl, 0), 0);
8292 if (decl)
8293 gcc_assert (VAR_P (decl));
8294 return decl;
8298 /* Returns the binfo for the primary base of BINFO. If the resulting
8299 BINFO is a virtual base, and it is inherited elsewhere in the
8300 hierarchy, then the returned binfo might not be the primary base of
8301 BINFO in the complete object. Check BINFO_PRIMARY_P or
8302 BINFO_LOST_PRIMARY_P to be sure. */
8304 static tree
8305 get_primary_binfo (tree binfo)
8307 tree primary_base;
8309 primary_base = CLASSTYPE_PRIMARY_BINFO (BINFO_TYPE (binfo));
8310 if (!primary_base)
8311 return NULL_TREE;
8313 return copied_binfo (primary_base, binfo);
8316 /* As above, but iterate until we reach the binfo that actually provides the
8317 vptr for BINFO. */
8319 static tree
8320 most_primary_binfo (tree binfo)
8322 tree b = binfo;
8323 while (CLASSTYPE_HAS_PRIMARY_BASE_P (BINFO_TYPE (b))
8324 && !BINFO_LOST_PRIMARY_P (b))
8326 tree primary_base = get_primary_binfo (b);
8327 gcc_assert (BINFO_PRIMARY_P (primary_base)
8328 && BINFO_INHERITANCE_CHAIN (primary_base) == b);
8329 b = primary_base;
8331 return b;
8334 /* Returns true if BINFO gets its vptr from a virtual base of the most derived
8335 type. Note that the virtual inheritance might be above or below BINFO in
8336 the hierarchy. */
8338 bool
8339 vptr_via_virtual_p (tree binfo)
8341 if (TYPE_P (binfo))
8342 binfo = TYPE_BINFO (binfo);
8343 tree primary = most_primary_binfo (binfo);
8344 /* Don't limit binfo_via_virtual, we want to return true when BINFO itself is
8345 a morally virtual base. */
8346 tree virt = binfo_via_virtual (primary, NULL_TREE);
8347 return virt != NULL_TREE;
8350 /* If INDENTED_P is zero, indent to INDENT. Return nonzero. */
8352 static int
8353 maybe_indent_hierarchy (FILE * stream, int indent, int indented_p)
8355 if (!indented_p)
8356 fprintf (stream, "%*s", indent, "");
8357 return 1;
8360 /* Dump the offsets of all the bases rooted at BINFO to STREAM.
8361 INDENT should be zero when called from the top level; it is
8362 incremented recursively. IGO indicates the next expected BINFO in
8363 inheritance graph ordering. */
8365 static tree
8366 dump_class_hierarchy_r (FILE *stream,
8367 dump_flags_t flags,
8368 tree binfo,
8369 tree igo,
8370 int indent)
8372 int indented = 0;
8373 tree base_binfo;
8374 int i;
8376 indented = maybe_indent_hierarchy (stream, indent, 0);
8377 fprintf (stream, "%s (0x" HOST_WIDE_INT_PRINT_HEX ") ",
8378 type_as_string (BINFO_TYPE (binfo), TFF_PLAIN_IDENTIFIER),
8379 (HOST_WIDE_INT) (uintptr_t) binfo);
8380 if (binfo != igo)
8382 fprintf (stream, "alternative-path\n");
8383 return igo;
8385 igo = TREE_CHAIN (binfo);
8387 fprintf (stream, HOST_WIDE_INT_PRINT_DEC,
8388 tree_to_shwi (BINFO_OFFSET (binfo)));
8389 if (is_empty_class (BINFO_TYPE (binfo)))
8390 fprintf (stream, " empty");
8391 else if (CLASSTYPE_NEARLY_EMPTY_P (BINFO_TYPE (binfo)))
8392 fprintf (stream, " nearly-empty");
8393 if (BINFO_VIRTUAL_P (binfo))
8394 fprintf (stream, " virtual");
8395 fprintf (stream, "\n");
8397 indented = 0;
8398 if (BINFO_PRIMARY_P (binfo))
8400 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
8401 fprintf (stream, " primary-for %s (0x" HOST_WIDE_INT_PRINT_HEX ")",
8402 type_as_string (BINFO_TYPE (BINFO_INHERITANCE_CHAIN (binfo)),
8403 TFF_PLAIN_IDENTIFIER),
8404 (HOST_WIDE_INT) (uintptr_t) BINFO_INHERITANCE_CHAIN (binfo));
8406 if (BINFO_LOST_PRIMARY_P (binfo))
8408 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
8409 fprintf (stream, " lost-primary");
8411 if (indented)
8412 fprintf (stream, "\n");
8414 if (!(flags & TDF_SLIM))
8416 int indented = 0;
8418 if (BINFO_SUBVTT_INDEX (binfo))
8420 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
8421 fprintf (stream, " subvttidx=%s",
8422 expr_as_string (BINFO_SUBVTT_INDEX (binfo),
8423 TFF_PLAIN_IDENTIFIER));
8425 if (BINFO_VPTR_INDEX (binfo))
8427 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
8428 fprintf (stream, " vptridx=%s",
8429 expr_as_string (BINFO_VPTR_INDEX (binfo),
8430 TFF_PLAIN_IDENTIFIER));
8432 if (BINFO_VPTR_FIELD (binfo))
8434 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
8435 fprintf (stream, " vbaseoffset=%s",
8436 expr_as_string (BINFO_VPTR_FIELD (binfo),
8437 TFF_PLAIN_IDENTIFIER));
8439 if (BINFO_VTABLE (binfo))
8441 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
8442 fprintf (stream, " vptr=%s",
8443 expr_as_string (BINFO_VTABLE (binfo),
8444 TFF_PLAIN_IDENTIFIER));
8447 if (indented)
8448 fprintf (stream, "\n");
8451 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
8452 igo = dump_class_hierarchy_r (stream, flags, base_binfo, igo, indent + 2);
8454 return igo;
8457 /* Dump the BINFO hierarchy for T. */
8459 static void
8460 dump_class_hierarchy_1 (FILE *stream, dump_flags_t flags, tree t)
8462 fprintf (stream, "Class %s\n", type_as_string (t, TFF_PLAIN_IDENTIFIER));
8463 fprintf (stream, " size=%lu align=%lu\n",
8464 (unsigned long)(tree_to_shwi (TYPE_SIZE (t)) / BITS_PER_UNIT),
8465 (unsigned long)(TYPE_ALIGN (t) / BITS_PER_UNIT));
8466 fprintf (stream, " base size=%lu base align=%lu\n",
8467 (unsigned long)(tree_to_shwi (TYPE_SIZE (CLASSTYPE_AS_BASE (t)))
8468 / BITS_PER_UNIT),
8469 (unsigned long)(TYPE_ALIGN (CLASSTYPE_AS_BASE (t))
8470 / BITS_PER_UNIT));
8471 dump_class_hierarchy_r (stream, flags, TYPE_BINFO (t), TYPE_BINFO (t), 0);
8472 fprintf (stream, "\n");
8475 /* Debug interface to hierarchy dumping. */
8477 void
8478 debug_class (tree t)
8480 dump_class_hierarchy_1 (stderr, TDF_SLIM, t);
8483 static void
8484 dump_class_hierarchy (tree t)
8486 dump_flags_t flags;
8487 if (FILE *stream = dump_begin (class_dump_id, &flags))
8489 dump_class_hierarchy_1 (stream, flags, t);
8490 dump_end (class_dump_id, stream);
8494 static void
8495 dump_array (FILE * stream, tree decl)
8497 tree value;
8498 unsigned HOST_WIDE_INT ix;
8499 HOST_WIDE_INT elt;
8500 tree size = TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (decl)));
8502 elt = (tree_to_shwi (TYPE_SIZE (TREE_TYPE (TREE_TYPE (decl))))
8503 / BITS_PER_UNIT);
8504 fprintf (stream, "%s:", decl_as_string (decl, TFF_PLAIN_IDENTIFIER));
8505 fprintf (stream, " %s entries",
8506 expr_as_string (size_binop (PLUS_EXPR, size, size_one_node),
8507 TFF_PLAIN_IDENTIFIER));
8508 fprintf (stream, "\n");
8510 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (DECL_INITIAL (decl)),
8511 ix, value)
8512 fprintf (stream, "%-4ld %s\n", (long)(ix * elt),
8513 expr_as_string (value, TFF_PLAIN_IDENTIFIER));
8516 static void
8517 dump_vtable (tree t, tree binfo, tree vtable)
8519 dump_flags_t flags;
8520 FILE *stream = dump_begin (class_dump_id, &flags);
8522 if (!stream)
8523 return;
8525 if (!(flags & TDF_SLIM))
8527 int ctor_vtbl_p = TYPE_BINFO (t) != binfo;
8529 fprintf (stream, "%s for %s",
8530 ctor_vtbl_p ? "Construction vtable" : "Vtable",
8531 type_as_string (BINFO_TYPE (binfo), TFF_PLAIN_IDENTIFIER));
8532 if (ctor_vtbl_p)
8534 if (!BINFO_VIRTUAL_P (binfo))
8535 fprintf (stream, " (0x" HOST_WIDE_INT_PRINT_HEX " instance)",
8536 (HOST_WIDE_INT) (uintptr_t) binfo);
8537 fprintf (stream, " in %s", type_as_string (t, TFF_PLAIN_IDENTIFIER));
8539 fprintf (stream, "\n");
8540 dump_array (stream, vtable);
8541 fprintf (stream, "\n");
8544 dump_end (class_dump_id, stream);
8547 static void
8548 dump_vtt (tree t, tree vtt)
8550 dump_flags_t flags;
8551 FILE *stream = dump_begin (class_dump_id, &flags);
8553 if (!stream)
8554 return;
8556 if (!(flags & TDF_SLIM))
8558 fprintf (stream, "VTT for %s\n",
8559 type_as_string (t, TFF_PLAIN_IDENTIFIER));
8560 dump_array (stream, vtt);
8561 fprintf (stream, "\n");
8564 dump_end (class_dump_id, stream);
8567 /* Dump a function or thunk and its thunkees. */
8569 static void
8570 dump_thunk (FILE *stream, int indent, tree thunk)
8572 static const char spaces[] = " ";
8573 tree name = DECL_NAME (thunk);
8574 tree thunks;
8576 fprintf (stream, "%.*s%p %s %s", indent, spaces,
8577 (void *)thunk,
8578 !DECL_THUNK_P (thunk) ? "function"
8579 : DECL_THIS_THUNK_P (thunk) ? "this-thunk" : "covariant-thunk",
8580 name ? IDENTIFIER_POINTER (name) : "<unset>");
8581 if (DECL_THUNK_P (thunk))
8583 HOST_WIDE_INT fixed_adjust = THUNK_FIXED_OFFSET (thunk);
8584 tree virtual_adjust = THUNK_VIRTUAL_OFFSET (thunk);
8586 fprintf (stream, " fixed=" HOST_WIDE_INT_PRINT_DEC, fixed_adjust);
8587 if (!virtual_adjust)
8588 /*NOP*/;
8589 else if (DECL_THIS_THUNK_P (thunk))
8590 fprintf (stream, " vcall=" HOST_WIDE_INT_PRINT_DEC,
8591 tree_to_shwi (virtual_adjust));
8592 else
8593 fprintf (stream, " vbase=" HOST_WIDE_INT_PRINT_DEC "(%s)",
8594 tree_to_shwi (BINFO_VPTR_FIELD (virtual_adjust)),
8595 type_as_string (BINFO_TYPE (virtual_adjust), TFF_SCOPE));
8596 if (THUNK_ALIAS (thunk))
8597 fprintf (stream, " alias to %p", (void *)THUNK_ALIAS (thunk));
8599 fprintf (stream, "\n");
8600 for (thunks = DECL_THUNKS (thunk); thunks; thunks = TREE_CHAIN (thunks))
8601 dump_thunk (stream, indent + 2, thunks);
8604 /* Dump the thunks for FN. */
8606 void
8607 debug_thunks (tree fn)
8609 dump_thunk (stderr, 0, fn);
8612 /* Virtual function table initialization. */
8614 /* Create all the necessary vtables for T and its base classes. */
8616 static void
8617 finish_vtbls (tree t)
8619 tree vbase;
8620 vec<constructor_elt, va_gc> *v = NULL;
8621 tree vtable = BINFO_VTABLE (TYPE_BINFO (t));
8623 /* We lay out the primary and secondary vtables in one contiguous
8624 vtable. The primary vtable is first, followed by the non-virtual
8625 secondary vtables in inheritance graph order. */
8626 accumulate_vtbl_inits (TYPE_BINFO (t), TYPE_BINFO (t), TYPE_BINFO (t),
8627 vtable, t, &v);
8629 /* Then come the virtual bases, also in inheritance graph order. */
8630 for (vbase = TYPE_BINFO (t); vbase; vbase = TREE_CHAIN (vbase))
8632 if (!BINFO_VIRTUAL_P (vbase))
8633 continue;
8634 accumulate_vtbl_inits (vbase, vbase, TYPE_BINFO (t), vtable, t, &v);
8637 if (BINFO_VTABLE (TYPE_BINFO (t)))
8638 initialize_vtable (TYPE_BINFO (t), v);
8641 /* Initialize the vtable for BINFO with the INITS. */
8643 static void
8644 initialize_vtable (tree binfo, vec<constructor_elt, va_gc> *inits)
8646 tree decl;
8648 layout_vtable_decl (binfo, vec_safe_length (inits));
8649 decl = get_vtbl_decl_for_binfo (binfo);
8650 initialize_artificial_var (decl, inits);
8651 dump_vtable (BINFO_TYPE (binfo), binfo, decl);
8654 /* Build the VTT (virtual table table) for T.
8655 A class requires a VTT if it has virtual bases.
8657 This holds
8658 1 - primary virtual pointer for complete object T
8659 2 - secondary VTTs for each direct non-virtual base of T which requires a
8661 3 - secondary virtual pointers for each direct or indirect base of T which
8662 has virtual bases or is reachable via a virtual path from T.
8663 4 - secondary VTTs for each direct or indirect virtual base of T.
8665 Secondary VTTs look like complete object VTTs without part 4. */
8667 static void
8668 build_vtt (tree t)
8670 tree type;
8671 tree vtt;
8672 tree index;
8673 vec<constructor_elt, va_gc> *inits;
8675 /* Build up the initializers for the VTT. */
8676 inits = NULL;
8677 index = size_zero_node;
8678 build_vtt_inits (TYPE_BINFO (t), t, &inits, &index);
8680 /* If we didn't need a VTT, we're done. */
8681 if (!inits)
8682 return;
8684 /* Figure out the type of the VTT. */
8685 type = build_array_of_n_type (const_ptr_type_node,
8686 inits->length ());
8688 /* Now, build the VTT object itself. */
8689 vtt = build_vtable (t, mangle_vtt_for_type (t), type);
8690 initialize_artificial_var (vtt, inits);
8691 /* Add the VTT to the vtables list. */
8692 DECL_CHAIN (vtt) = DECL_CHAIN (CLASSTYPE_VTABLES (t));
8693 DECL_CHAIN (CLASSTYPE_VTABLES (t)) = vtt;
8695 dump_vtt (t, vtt);
8698 /* When building a secondary VTT, BINFO_VTABLE is set to a TREE_LIST with
8699 PURPOSE the RTTI_BINFO, VALUE the real vtable pointer for this binfo,
8700 and CHAIN the vtable pointer for this binfo after construction is
8701 complete. VALUE can also be another BINFO, in which case we recurse. */
8703 static tree
8704 binfo_ctor_vtable (tree binfo)
8706 tree vt;
8708 while (1)
8710 vt = BINFO_VTABLE (binfo);
8711 if (TREE_CODE (vt) == TREE_LIST)
8712 vt = TREE_VALUE (vt);
8713 if (TREE_CODE (vt) == TREE_BINFO)
8714 binfo = vt;
8715 else
8716 break;
8719 return vt;
8722 /* Data for secondary VTT initialization. */
8723 struct secondary_vptr_vtt_init_data
8725 /* Is this the primary VTT? */
8726 bool top_level_p;
8728 /* Current index into the VTT. */
8729 tree index;
8731 /* Vector of initializers built up. */
8732 vec<constructor_elt, va_gc> *inits;
8734 /* The type being constructed by this secondary VTT. */
8735 tree type_being_constructed;
8738 /* Recursively build the VTT-initializer for BINFO (which is in the
8739 hierarchy dominated by T). INITS points to the end of the initializer
8740 list to date. INDEX is the VTT index where the next element will be
8741 replaced. Iff BINFO is the binfo for T, this is the top level VTT (i.e.
8742 not a subvtt for some base of T). When that is so, we emit the sub-VTTs
8743 for virtual bases of T. When it is not so, we build the constructor
8744 vtables for the BINFO-in-T variant. */
8746 static void
8747 build_vtt_inits (tree binfo, tree t, vec<constructor_elt, va_gc> **inits,
8748 tree *index)
8750 int i;
8751 tree b;
8752 tree init;
8753 secondary_vptr_vtt_init_data data;
8754 int top_level_p = SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), t);
8756 /* We only need VTTs for subobjects with virtual bases. */
8757 if (!CLASSTYPE_VBASECLASSES (BINFO_TYPE (binfo)))
8758 return;
8760 /* We need to use a construction vtable if this is not the primary
8761 VTT. */
8762 if (!top_level_p)
8764 build_ctor_vtbl_group (binfo, t);
8766 /* Record the offset in the VTT where this sub-VTT can be found. */
8767 BINFO_SUBVTT_INDEX (binfo) = *index;
8770 /* Add the address of the primary vtable for the complete object. */
8771 init = binfo_ctor_vtable (binfo);
8772 CONSTRUCTOR_APPEND_ELT (*inits, NULL_TREE, init);
8773 if (top_level_p)
8775 gcc_assert (!BINFO_VPTR_INDEX (binfo));
8776 BINFO_VPTR_INDEX (binfo) = *index;
8778 *index = size_binop (PLUS_EXPR, *index, TYPE_SIZE_UNIT (ptr_type_node));
8780 /* Recursively add the secondary VTTs for non-virtual bases. */
8781 for (i = 0; BINFO_BASE_ITERATE (binfo, i, b); ++i)
8782 if (!BINFO_VIRTUAL_P (b))
8783 build_vtt_inits (b, t, inits, index);
8785 /* Add secondary virtual pointers for all subobjects of BINFO with
8786 either virtual bases or reachable along a virtual path, except
8787 subobjects that are non-virtual primary bases. */
8788 data.top_level_p = top_level_p;
8789 data.index = *index;
8790 data.inits = *inits;
8791 data.type_being_constructed = BINFO_TYPE (binfo);
8793 dfs_walk_once (binfo, dfs_build_secondary_vptr_vtt_inits, NULL, &data);
8795 *index = data.index;
8797 /* data.inits might have grown as we added secondary virtual pointers.
8798 Make sure our caller knows about the new vector. */
8799 *inits = data.inits;
8801 if (top_level_p)
8802 /* Add the secondary VTTs for virtual bases in inheritance graph
8803 order. */
8804 for (b = TYPE_BINFO (BINFO_TYPE (binfo)); b; b = TREE_CHAIN (b))
8806 if (!BINFO_VIRTUAL_P (b))
8807 continue;
8809 build_vtt_inits (b, t, inits, index);
8811 else
8812 /* Remove the ctor vtables we created. */
8813 dfs_walk_all (binfo, dfs_fixup_binfo_vtbls, NULL, binfo);
8816 /* Called from build_vtt_inits via dfs_walk. BINFO is the binfo for the base
8817 in most derived. DATA is a SECONDARY_VPTR_VTT_INIT_DATA structure. */
8819 static tree
8820 dfs_build_secondary_vptr_vtt_inits (tree binfo, void *data_)
8822 secondary_vptr_vtt_init_data *data = (secondary_vptr_vtt_init_data *)data_;
8824 /* We don't care about bases that don't have vtables. */
8825 if (!TYPE_VFIELD (BINFO_TYPE (binfo)))
8826 return dfs_skip_bases;
8828 /* We're only interested in proper subobjects of the type being
8829 constructed. */
8830 if (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), data->type_being_constructed))
8831 return NULL_TREE;
8833 /* We're only interested in bases with virtual bases or reachable
8834 via a virtual path from the type being constructed. */
8835 if (!(CLASSTYPE_VBASECLASSES (BINFO_TYPE (binfo))
8836 || binfo_via_virtual (binfo, data->type_being_constructed)))
8837 return dfs_skip_bases;
8839 /* We're not interested in non-virtual primary bases. */
8840 if (!BINFO_VIRTUAL_P (binfo) && BINFO_PRIMARY_P (binfo))
8841 return NULL_TREE;
8843 /* Record the index where this secondary vptr can be found. */
8844 if (data->top_level_p)
8846 gcc_assert (!BINFO_VPTR_INDEX (binfo));
8847 BINFO_VPTR_INDEX (binfo) = data->index;
8849 if (BINFO_VIRTUAL_P (binfo))
8851 /* It's a primary virtual base, and this is not a
8852 construction vtable. Find the base this is primary of in
8853 the inheritance graph, and use that base's vtable
8854 now. */
8855 while (BINFO_PRIMARY_P (binfo))
8856 binfo = BINFO_INHERITANCE_CHAIN (binfo);
8860 /* Add the initializer for the secondary vptr itself. */
8861 CONSTRUCTOR_APPEND_ELT (data->inits, NULL_TREE, binfo_ctor_vtable (binfo));
8863 /* Advance the vtt index. */
8864 data->index = size_binop (PLUS_EXPR, data->index,
8865 TYPE_SIZE_UNIT (ptr_type_node));
8867 return NULL_TREE;
8870 /* Called from build_vtt_inits via dfs_walk. After building
8871 constructor vtables and generating the sub-vtt from them, we need
8872 to restore the BINFO_VTABLES that were scribbled on. DATA is the
8873 binfo of the base whose sub vtt was generated. */
8875 static tree
8876 dfs_fixup_binfo_vtbls (tree binfo, void* data)
8878 tree vtable = BINFO_VTABLE (binfo);
8880 if (!TYPE_CONTAINS_VPTR_P (BINFO_TYPE (binfo)))
8881 /* If this class has no vtable, none of its bases do. */
8882 return dfs_skip_bases;
8884 if (!vtable)
8885 /* This might be a primary base, so have no vtable in this
8886 hierarchy. */
8887 return NULL_TREE;
8889 /* If we scribbled the construction vtable vptr into BINFO, clear it
8890 out now. */
8891 if (TREE_CODE (vtable) == TREE_LIST
8892 && (TREE_PURPOSE (vtable) == (tree) data))
8893 BINFO_VTABLE (binfo) = TREE_CHAIN (vtable);
8895 return NULL_TREE;
8898 /* Build the construction vtable group for BINFO which is in the
8899 hierarchy dominated by T. */
8901 static void
8902 build_ctor_vtbl_group (tree binfo, tree t)
8904 tree type;
8905 tree vtbl;
8906 tree id;
8907 tree vbase;
8908 vec<constructor_elt, va_gc> *v;
8910 /* See if we've already created this construction vtable group. */
8911 id = mangle_ctor_vtbl_for_type (t, binfo);
8912 if (get_global_binding (id))
8913 return;
8915 gcc_assert (!SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), t));
8916 /* Build a version of VTBL (with the wrong type) for use in
8917 constructing the addresses of secondary vtables in the
8918 construction vtable group. */
8919 vtbl = build_vtable (t, id, ptr_type_node);
8920 DECL_CONSTRUCTION_VTABLE_P (vtbl) = 1;
8921 /* Don't export construction vtables from shared libraries. Even on
8922 targets that don't support hidden visibility, this tells
8923 can_refer_decl_in_current_unit_p not to assume that it's safe to
8924 access from a different compilation unit (bz 54314). */
8925 DECL_VISIBILITY (vtbl) = VISIBILITY_HIDDEN;
8926 DECL_VISIBILITY_SPECIFIED (vtbl) = true;
8928 v = NULL;
8929 accumulate_vtbl_inits (binfo, TYPE_BINFO (TREE_TYPE (binfo)),
8930 binfo, vtbl, t, &v);
8932 /* Add the vtables for each of our virtual bases using the vbase in T
8933 binfo. */
8934 for (vbase = TYPE_BINFO (BINFO_TYPE (binfo));
8935 vbase;
8936 vbase = TREE_CHAIN (vbase))
8938 tree b;
8940 if (!BINFO_VIRTUAL_P (vbase))
8941 continue;
8942 b = copied_binfo (vbase, binfo);
8944 accumulate_vtbl_inits (b, vbase, binfo, vtbl, t, &v);
8947 /* Figure out the type of the construction vtable. */
8948 type = build_array_of_n_type (vtable_entry_type, v->length ());
8949 layout_type (type);
8950 TREE_TYPE (vtbl) = type;
8951 DECL_SIZE (vtbl) = DECL_SIZE_UNIT (vtbl) = NULL_TREE;
8952 layout_decl (vtbl, 0);
8954 /* Initialize the construction vtable. */
8955 CLASSTYPE_VTABLES (t) = chainon (CLASSTYPE_VTABLES (t), vtbl);
8956 initialize_artificial_var (vtbl, v);
8957 dump_vtable (t, binfo, vtbl);
8960 /* Add the vtbl initializers for BINFO (and its bases other than
8961 non-virtual primaries) to the list of INITS. BINFO is in the
8962 hierarchy dominated by T. RTTI_BINFO is the binfo within T of
8963 the constructor the vtbl inits should be accumulated for. (If this
8964 is the complete object vtbl then RTTI_BINFO will be TYPE_BINFO (T).)
8965 ORIG_BINFO is the binfo for this object within BINFO_TYPE (RTTI_BINFO).
8966 BINFO is the active base equivalent of ORIG_BINFO in the inheritance
8967 graph of T. Both BINFO and ORIG_BINFO will have the same BINFO_TYPE,
8968 but are not necessarily the same in terms of layout. */
8970 static void
8971 accumulate_vtbl_inits (tree binfo,
8972 tree orig_binfo,
8973 tree rtti_binfo,
8974 tree vtbl,
8975 tree t,
8976 vec<constructor_elt, va_gc> **inits)
8978 int i;
8979 tree base_binfo;
8980 int ctor_vtbl_p = !SAME_BINFO_TYPE_P (BINFO_TYPE (rtti_binfo), t);
8982 gcc_assert (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), BINFO_TYPE (orig_binfo)));
8984 /* If it doesn't have a vptr, we don't do anything. */
8985 if (!TYPE_CONTAINS_VPTR_P (BINFO_TYPE (binfo)))
8986 return;
8988 /* If we're building a construction vtable, we're not interested in
8989 subobjects that don't require construction vtables. */
8990 if (ctor_vtbl_p
8991 && !CLASSTYPE_VBASECLASSES (BINFO_TYPE (binfo))
8992 && !binfo_via_virtual (orig_binfo, BINFO_TYPE (rtti_binfo)))
8993 return;
8995 /* Build the initializers for the BINFO-in-T vtable. */
8996 dfs_accumulate_vtbl_inits (binfo, orig_binfo, rtti_binfo, vtbl, t, inits);
8998 /* Walk the BINFO and its bases. We walk in preorder so that as we
8999 initialize each vtable we can figure out at what offset the
9000 secondary vtable lies from the primary vtable. We can't use
9001 dfs_walk here because we need to iterate through bases of BINFO
9002 and RTTI_BINFO simultaneously. */
9003 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
9005 /* Skip virtual bases. */
9006 if (BINFO_VIRTUAL_P (base_binfo))
9007 continue;
9008 accumulate_vtbl_inits (base_binfo,
9009 BINFO_BASE_BINFO (orig_binfo, i),
9010 rtti_binfo, vtbl, t,
9011 inits);
9015 /* Called from accumulate_vtbl_inits. Adds the initializers for the
9016 BINFO vtable to L. */
9018 static void
9019 dfs_accumulate_vtbl_inits (tree binfo,
9020 tree orig_binfo,
9021 tree rtti_binfo,
9022 tree orig_vtbl,
9023 tree t,
9024 vec<constructor_elt, va_gc> **l)
9026 tree vtbl = NULL_TREE;
9027 int ctor_vtbl_p = !SAME_BINFO_TYPE_P (BINFO_TYPE (rtti_binfo), t);
9028 int n_inits;
9030 if (ctor_vtbl_p
9031 && BINFO_VIRTUAL_P (orig_binfo) && BINFO_PRIMARY_P (orig_binfo))
9033 /* In the hierarchy of BINFO_TYPE (RTTI_BINFO), this is a
9034 primary virtual base. If it is not the same primary in
9035 the hierarchy of T, we'll need to generate a ctor vtable
9036 for it, to place at its location in T. If it is the same
9037 primary, we still need a VTT entry for the vtable, but it
9038 should point to the ctor vtable for the base it is a
9039 primary for within the sub-hierarchy of RTTI_BINFO.
9041 There are three possible cases:
9043 1) We are in the same place.
9044 2) We are a primary base within a lost primary virtual base of
9045 RTTI_BINFO.
9046 3) We are primary to something not a base of RTTI_BINFO. */
9048 tree b;
9049 tree last = NULL_TREE;
9051 /* First, look through the bases we are primary to for RTTI_BINFO
9052 or a virtual base. */
9053 b = binfo;
9054 while (BINFO_PRIMARY_P (b))
9056 b = BINFO_INHERITANCE_CHAIN (b);
9057 last = b;
9058 if (BINFO_VIRTUAL_P (b) || b == rtti_binfo)
9059 goto found;
9061 /* If we run out of primary links, keep looking down our
9062 inheritance chain; we might be an indirect primary. */
9063 for (b = last; b; b = BINFO_INHERITANCE_CHAIN (b))
9064 if (BINFO_VIRTUAL_P (b) || b == rtti_binfo)
9065 break;
9066 found:
9068 /* If we found RTTI_BINFO, this is case 1. If we found a virtual
9069 base B and it is a base of RTTI_BINFO, this is case 2. In
9070 either case, we share our vtable with LAST, i.e. the
9071 derived-most base within B of which we are a primary. */
9072 if (b == rtti_binfo
9073 || (b && binfo_for_vbase (BINFO_TYPE (b), BINFO_TYPE (rtti_binfo))))
9074 /* Just set our BINFO_VTABLE to point to LAST, as we may not have
9075 set LAST's BINFO_VTABLE yet. We'll extract the actual vptr in
9076 binfo_ctor_vtable after everything's been set up. */
9077 vtbl = last;
9079 /* Otherwise, this is case 3 and we get our own. */
9081 else if (!BINFO_NEW_VTABLE_MARKED (orig_binfo))
9082 return;
9084 n_inits = vec_safe_length (*l);
9086 if (!vtbl)
9088 tree index;
9089 int non_fn_entries;
9091 /* Add the initializer for this vtable. */
9092 build_vtbl_initializer (binfo, orig_binfo, t, rtti_binfo,
9093 &non_fn_entries, l);
9095 /* Figure out the position to which the VPTR should point. */
9096 vtbl = build1 (ADDR_EXPR, vtbl_ptr_type_node, orig_vtbl);
9097 index = size_binop (MULT_EXPR,
9098 TYPE_SIZE_UNIT (vtable_entry_type),
9099 size_int (non_fn_entries + n_inits));
9100 vtbl = fold_build_pointer_plus (vtbl, index);
9103 if (ctor_vtbl_p)
9104 /* For a construction vtable, we can't overwrite BINFO_VTABLE.
9105 So, we make a TREE_LIST. Later, dfs_fixup_binfo_vtbls will
9106 straighten this out. */
9107 BINFO_VTABLE (binfo) = tree_cons (rtti_binfo, vtbl, BINFO_VTABLE (binfo));
9108 else if (BINFO_PRIMARY_P (binfo) && BINFO_VIRTUAL_P (binfo))
9109 /* Throw away any unneeded intializers. */
9110 (*l)->truncate (n_inits);
9111 else
9112 /* For an ordinary vtable, set BINFO_VTABLE. */
9113 BINFO_VTABLE (binfo) = vtbl;
9116 static GTY(()) tree abort_fndecl_addr;
9117 static GTY(()) tree dvirt_fn;
9119 /* Construct the initializer for BINFO's virtual function table. BINFO
9120 is part of the hierarchy dominated by T. If we're building a
9121 construction vtable, the ORIG_BINFO is the binfo we should use to
9122 find the actual function pointers to put in the vtable - but they
9123 can be overridden on the path to most-derived in the graph that
9124 ORIG_BINFO belongs. Otherwise,
9125 ORIG_BINFO should be the same as BINFO. The RTTI_BINFO is the
9126 BINFO that should be indicated by the RTTI information in the
9127 vtable; it will be a base class of T, rather than T itself, if we
9128 are building a construction vtable.
9130 The value returned is a TREE_LIST suitable for wrapping in a
9131 CONSTRUCTOR to use as the DECL_INITIAL for a vtable. If
9132 NON_FN_ENTRIES_P is not NULL, *NON_FN_ENTRIES_P is set to the
9133 number of non-function entries in the vtable.
9135 It might seem that this function should never be called with a
9136 BINFO for which BINFO_PRIMARY_P holds, the vtable for such a
9137 base is always subsumed by a derived class vtable. However, when
9138 we are building construction vtables, we do build vtables for
9139 primary bases; we need these while the primary base is being
9140 constructed. */
9142 static void
9143 build_vtbl_initializer (tree binfo,
9144 tree orig_binfo,
9145 tree t,
9146 tree rtti_binfo,
9147 int* non_fn_entries_p,
9148 vec<constructor_elt, va_gc> **inits)
9150 tree v;
9151 vtbl_init_data vid;
9152 unsigned ix, jx;
9153 tree vbinfo;
9154 vec<tree, va_gc> *vbases;
9155 constructor_elt *e;
9157 /* Initialize VID. */
9158 memset (&vid, 0, sizeof (vid));
9159 vid.binfo = binfo;
9160 vid.derived = t;
9161 vid.rtti_binfo = rtti_binfo;
9162 vid.primary_vtbl_p = SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), t);
9163 vid.ctor_vtbl_p = !SAME_BINFO_TYPE_P (BINFO_TYPE (rtti_binfo), t);
9164 vid.generate_vcall_entries = true;
9165 /* The first vbase or vcall offset is at index -3 in the vtable. */
9166 vid.index = ssize_int(-3 * TARGET_VTABLE_DATA_ENTRY_DISTANCE);
9168 /* Add entries to the vtable for RTTI. */
9169 build_rtti_vtbl_entries (binfo, &vid);
9171 /* Create an array for keeping track of the functions we've
9172 processed. When we see multiple functions with the same
9173 signature, we share the vcall offsets. */
9174 vec_alloc (vid.fns, 32);
9175 /* Add the vcall and vbase offset entries. */
9176 build_vcall_and_vbase_vtbl_entries (binfo, &vid);
9178 /* Clear BINFO_VTABLE_PATH_MARKED; it's set by
9179 build_vbase_offset_vtbl_entries. */
9180 for (vbases = CLASSTYPE_VBASECLASSES (t), ix = 0;
9181 vec_safe_iterate (vbases, ix, &vbinfo); ix++)
9182 BINFO_VTABLE_PATH_MARKED (vbinfo) = 0;
9184 /* If the target requires padding between data entries, add that now. */
9185 if (TARGET_VTABLE_DATA_ENTRY_DISTANCE > 1)
9187 int n_entries = vec_safe_length (vid.inits);
9189 vec_safe_grow (vid.inits, TARGET_VTABLE_DATA_ENTRY_DISTANCE * n_entries);
9191 /* Move data entries into their new positions and add padding
9192 after the new positions. Iterate backwards so we don't
9193 overwrite entries that we would need to process later. */
9194 for (ix = n_entries - 1;
9195 vid.inits->iterate (ix, &e);
9196 ix--)
9198 int j;
9199 int new_position = (TARGET_VTABLE_DATA_ENTRY_DISTANCE * ix
9200 + (TARGET_VTABLE_DATA_ENTRY_DISTANCE - 1));
9202 (*vid.inits)[new_position] = *e;
9204 for (j = 1; j < TARGET_VTABLE_DATA_ENTRY_DISTANCE; ++j)
9206 constructor_elt *f = &(*vid.inits)[new_position - j];
9207 f->index = NULL_TREE;
9208 f->value = build1 (NOP_EXPR, vtable_entry_type,
9209 null_pointer_node);
9214 if (non_fn_entries_p)
9215 *non_fn_entries_p = vec_safe_length (vid.inits);
9217 /* The initializers for virtual functions were built up in reverse
9218 order. Straighten them out and add them to the running list in one
9219 step. */
9220 jx = vec_safe_length (*inits);
9221 vec_safe_grow (*inits, jx + vid.inits->length ());
9223 for (ix = vid.inits->length () - 1;
9224 vid.inits->iterate (ix, &e);
9225 ix--, jx++)
9226 (**inits)[jx] = *e;
9228 /* Go through all the ordinary virtual functions, building up
9229 initializers. */
9230 for (v = BINFO_VIRTUALS (orig_binfo); v; v = TREE_CHAIN (v))
9232 tree delta;
9233 tree vcall_index;
9234 tree fn, fn_original;
9235 tree init = NULL_TREE;
9237 fn = BV_FN (v);
9238 fn_original = fn;
9239 if (DECL_THUNK_P (fn))
9241 if (!DECL_NAME (fn))
9242 finish_thunk (fn);
9243 if (THUNK_ALIAS (fn))
9245 fn = THUNK_ALIAS (fn);
9246 BV_FN (v) = fn;
9248 fn_original = THUNK_TARGET (fn);
9251 /* If the only definition of this function signature along our
9252 primary base chain is from a lost primary, this vtable slot will
9253 never be used, so just zero it out. This is important to avoid
9254 requiring extra thunks which cannot be generated with the function.
9256 We first check this in update_vtable_entry_for_fn, so we handle
9257 restored primary bases properly; we also need to do it here so we
9258 zero out unused slots in ctor vtables, rather than filling them
9259 with erroneous values (though harmless, apart from relocation
9260 costs). */
9261 if (BV_LOST_PRIMARY (v))
9262 init = size_zero_node;
9264 if (! init)
9266 /* Pull the offset for `this', and the function to call, out of
9267 the list. */
9268 delta = BV_DELTA (v);
9269 vcall_index = BV_VCALL_INDEX (v);
9271 gcc_assert (TREE_CODE (delta) == INTEGER_CST);
9272 gcc_assert (TREE_CODE (fn) == FUNCTION_DECL);
9274 /* You can't call an abstract virtual function; it's abstract.
9275 So, we replace these functions with __pure_virtual. */
9276 if (DECL_PURE_VIRTUAL_P (fn_original))
9278 fn = abort_fndecl;
9279 if (!TARGET_VTABLE_USES_DESCRIPTORS)
9281 if (abort_fndecl_addr == NULL)
9282 abort_fndecl_addr
9283 = fold_convert (vfunc_ptr_type_node,
9284 build_fold_addr_expr (fn));
9285 init = abort_fndecl_addr;
9288 /* Likewise for deleted virtuals. */
9289 else if (DECL_DELETED_FN (fn_original))
9291 if (!dvirt_fn)
9293 tree name = get_identifier ("__cxa_deleted_virtual");
9294 dvirt_fn = get_global_binding (name);
9295 if (!dvirt_fn)
9296 dvirt_fn = push_library_fn
9297 (name,
9298 build_function_type_list (void_type_node, NULL_TREE),
9299 NULL_TREE, ECF_NORETURN | ECF_COLD);
9301 fn = dvirt_fn;
9302 if (!TARGET_VTABLE_USES_DESCRIPTORS)
9303 init = fold_convert (vfunc_ptr_type_node,
9304 build_fold_addr_expr (fn));
9306 else
9308 if (!integer_zerop (delta) || vcall_index)
9310 fn = make_thunk (fn, /*this_adjusting=*/1,
9311 delta, vcall_index);
9312 if (!DECL_NAME (fn))
9313 finish_thunk (fn);
9315 /* Take the address of the function, considering it to be of an
9316 appropriate generic type. */
9317 if (!TARGET_VTABLE_USES_DESCRIPTORS)
9318 init = fold_convert (vfunc_ptr_type_node,
9319 build_fold_addr_expr (fn));
9320 /* Don't refer to a virtual destructor from a constructor
9321 vtable or a vtable for an abstract class, since destroying
9322 an object under construction is undefined behavior and we
9323 don't want it to be considered a candidate for speculative
9324 devirtualization. But do create the thunk for ABI
9325 compliance. */
9326 if (DECL_DESTRUCTOR_P (fn_original)
9327 && (CLASSTYPE_PURE_VIRTUALS (DECL_CONTEXT (fn_original))
9328 || orig_binfo != binfo))
9329 init = size_zero_node;
9333 /* And add it to the chain of initializers. */
9334 if (TARGET_VTABLE_USES_DESCRIPTORS)
9336 int i;
9337 if (init == size_zero_node)
9338 for (i = 0; i < TARGET_VTABLE_USES_DESCRIPTORS; ++i)
9339 CONSTRUCTOR_APPEND_ELT (*inits, NULL_TREE, init);
9340 else
9341 for (i = 0; i < TARGET_VTABLE_USES_DESCRIPTORS; ++i)
9343 tree fdesc = build2 (FDESC_EXPR, vfunc_ptr_type_node,
9344 fn, build_int_cst (NULL_TREE, i));
9345 TREE_CONSTANT (fdesc) = 1;
9347 CONSTRUCTOR_APPEND_ELT (*inits, NULL_TREE, fdesc);
9350 else
9351 CONSTRUCTOR_APPEND_ELT (*inits, NULL_TREE, init);
9355 /* Adds to vid->inits the initializers for the vbase and vcall
9356 offsets in BINFO, which is in the hierarchy dominated by T. */
9358 static void
9359 build_vcall_and_vbase_vtbl_entries (tree binfo, vtbl_init_data* vid)
9361 tree b;
9363 /* If this is a derived class, we must first create entries
9364 corresponding to the primary base class. */
9365 b = get_primary_binfo (binfo);
9366 if (b)
9367 build_vcall_and_vbase_vtbl_entries (b, vid);
9369 /* Add the vbase entries for this base. */
9370 build_vbase_offset_vtbl_entries (binfo, vid);
9371 /* Add the vcall entries for this base. */
9372 build_vcall_offset_vtbl_entries (binfo, vid);
9375 /* Returns the initializers for the vbase offset entries in the vtable
9376 for BINFO (which is part of the class hierarchy dominated by T), in
9377 reverse order. VBASE_OFFSET_INDEX gives the vtable index
9378 where the next vbase offset will go. */
9380 static void
9381 build_vbase_offset_vtbl_entries (tree binfo, vtbl_init_data* vid)
9383 tree vbase;
9384 tree t;
9385 tree non_primary_binfo;
9387 /* If there are no virtual baseclasses, then there is nothing to
9388 do. */
9389 if (!CLASSTYPE_VBASECLASSES (BINFO_TYPE (binfo)))
9390 return;
9392 t = vid->derived;
9394 /* We might be a primary base class. Go up the inheritance hierarchy
9395 until we find the most derived class of which we are a primary base:
9396 it is the offset of that which we need to use. */
9397 non_primary_binfo = binfo;
9398 while (BINFO_INHERITANCE_CHAIN (non_primary_binfo))
9400 tree b;
9402 /* If we have reached a virtual base, then it must be a primary
9403 base (possibly multi-level) of vid->binfo, or we wouldn't
9404 have called build_vcall_and_vbase_vtbl_entries for it. But it
9405 might be a lost primary, so just skip down to vid->binfo. */
9406 if (BINFO_VIRTUAL_P (non_primary_binfo))
9408 non_primary_binfo = vid->binfo;
9409 break;
9412 b = BINFO_INHERITANCE_CHAIN (non_primary_binfo);
9413 if (get_primary_binfo (b) != non_primary_binfo)
9414 break;
9415 non_primary_binfo = b;
9418 /* Go through the virtual bases, adding the offsets. */
9419 for (vbase = TYPE_BINFO (BINFO_TYPE (binfo));
9420 vbase;
9421 vbase = TREE_CHAIN (vbase))
9423 tree b;
9424 tree delta;
9426 if (!BINFO_VIRTUAL_P (vbase))
9427 continue;
9429 /* Find the instance of this virtual base in the complete
9430 object. */
9431 b = copied_binfo (vbase, binfo);
9433 /* If we've already got an offset for this virtual base, we
9434 don't need another one. */
9435 if (BINFO_VTABLE_PATH_MARKED (b))
9436 continue;
9437 BINFO_VTABLE_PATH_MARKED (b) = 1;
9439 /* Figure out where we can find this vbase offset. */
9440 delta = size_binop (MULT_EXPR,
9441 vid->index,
9442 fold_convert (ssizetype,
9443 TYPE_SIZE_UNIT (vtable_entry_type)));
9444 if (vid->primary_vtbl_p)
9445 BINFO_VPTR_FIELD (b) = delta;
9447 if (binfo != TYPE_BINFO (t))
9448 /* The vbase offset had better be the same. */
9449 gcc_assert (tree_int_cst_equal (delta, BINFO_VPTR_FIELD (vbase)));
9451 /* The next vbase will come at a more negative offset. */
9452 vid->index = size_binop (MINUS_EXPR, vid->index,
9453 ssize_int (TARGET_VTABLE_DATA_ENTRY_DISTANCE));
9455 /* The initializer is the delta from BINFO to this virtual base.
9456 The vbase offsets go in reverse inheritance-graph order, and
9457 we are walking in inheritance graph order so these end up in
9458 the right order. */
9459 delta = size_diffop_loc (input_location,
9460 BINFO_OFFSET (b), BINFO_OFFSET (non_primary_binfo));
9462 CONSTRUCTOR_APPEND_ELT (vid->inits, NULL_TREE,
9463 fold_build1_loc (input_location, NOP_EXPR,
9464 vtable_entry_type, delta));
9468 /* Adds the initializers for the vcall offset entries in the vtable
9469 for BINFO (which is part of the class hierarchy dominated by VID->DERIVED)
9470 to VID->INITS. */
9472 static void
9473 build_vcall_offset_vtbl_entries (tree binfo, vtbl_init_data* vid)
9475 /* We only need these entries if this base is a virtual base. We
9476 compute the indices -- but do not add to the vtable -- when
9477 building the main vtable for a class. */
9478 if (binfo == TYPE_BINFO (vid->derived)
9479 || (BINFO_VIRTUAL_P (binfo)
9480 /* If BINFO is RTTI_BINFO, then (since BINFO does not
9481 correspond to VID->DERIVED), we are building a primary
9482 construction virtual table. Since this is a primary
9483 virtual table, we do not need the vcall offsets for
9484 BINFO. */
9485 && binfo != vid->rtti_binfo))
9487 /* We need a vcall offset for each of the virtual functions in this
9488 vtable. For example:
9490 class A { virtual void f (); };
9491 class B1 : virtual public A { virtual void f (); };
9492 class B2 : virtual public A { virtual void f (); };
9493 class C: public B1, public B2 { virtual void f (); };
9495 A C object has a primary base of B1, which has a primary base of A. A
9496 C also has a secondary base of B2, which no longer has a primary base
9497 of A. So the B2-in-C construction vtable needs a secondary vtable for
9498 A, which will adjust the A* to a B2* to call f. We have no way of
9499 knowing what (or even whether) this offset will be when we define B2,
9500 so we store this "vcall offset" in the A sub-vtable and look it up in
9501 a "virtual thunk" for B2::f.
9503 We need entries for all the functions in our primary vtable and
9504 in our non-virtual bases' secondary vtables. */
9505 vid->vbase = binfo;
9506 /* If we are just computing the vcall indices -- but do not need
9507 the actual entries -- not that. */
9508 if (!BINFO_VIRTUAL_P (binfo))
9509 vid->generate_vcall_entries = false;
9510 /* Now, walk through the non-virtual bases, adding vcall offsets. */
9511 add_vcall_offset_vtbl_entries_r (binfo, vid);
9515 /* Build vcall offsets, starting with those for BINFO. */
9517 static void
9518 add_vcall_offset_vtbl_entries_r (tree binfo, vtbl_init_data* vid)
9520 int i;
9521 tree primary_binfo;
9522 tree base_binfo;
9524 /* Don't walk into virtual bases -- except, of course, for the
9525 virtual base for which we are building vcall offsets. Any
9526 primary virtual base will have already had its offsets generated
9527 through the recursion in build_vcall_and_vbase_vtbl_entries. */
9528 if (BINFO_VIRTUAL_P (binfo) && vid->vbase != binfo)
9529 return;
9531 /* If BINFO has a primary base, process it first. */
9532 primary_binfo = get_primary_binfo (binfo);
9533 if (primary_binfo)
9534 add_vcall_offset_vtbl_entries_r (primary_binfo, vid);
9536 /* Add BINFO itself to the list. */
9537 add_vcall_offset_vtbl_entries_1 (binfo, vid);
9539 /* Scan the non-primary bases of BINFO. */
9540 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
9541 if (base_binfo != primary_binfo)
9542 add_vcall_offset_vtbl_entries_r (base_binfo, vid);
9545 /* Called from build_vcall_offset_vtbl_entries_r. */
9547 static void
9548 add_vcall_offset_vtbl_entries_1 (tree binfo, vtbl_init_data* vid)
9550 /* Make entries for the rest of the virtuals. */
9551 tree orig_fn;
9553 /* The ABI requires that the methods be processed in declaration
9554 order. */
9555 for (orig_fn = TYPE_FIELDS (BINFO_TYPE (binfo));
9556 orig_fn;
9557 orig_fn = DECL_CHAIN (orig_fn))
9558 if (TREE_CODE (orig_fn) == FUNCTION_DECL && DECL_VINDEX (orig_fn))
9559 add_vcall_offset (orig_fn, binfo, vid);
9562 /* Add a vcall offset entry for ORIG_FN to the vtable. */
9564 static void
9565 add_vcall_offset (tree orig_fn, tree binfo, vtbl_init_data *vid)
9567 size_t i;
9568 tree vcall_offset;
9569 tree derived_entry;
9571 /* If there is already an entry for a function with the same
9572 signature as FN, then we do not need a second vcall offset.
9573 Check the list of functions already present in the derived
9574 class vtable. */
9575 FOR_EACH_VEC_SAFE_ELT (vid->fns, i, derived_entry)
9577 if (same_signature_p (derived_entry, orig_fn)
9578 /* We only use one vcall offset for virtual destructors,
9579 even though there are two virtual table entries. */
9580 || (DECL_DESTRUCTOR_P (derived_entry)
9581 && DECL_DESTRUCTOR_P (orig_fn)))
9582 return;
9585 /* If we are building these vcall offsets as part of building
9586 the vtable for the most derived class, remember the vcall
9587 offset. */
9588 if (vid->binfo == TYPE_BINFO (vid->derived))
9590 tree_pair_s elt = {orig_fn, vid->index};
9591 vec_safe_push (CLASSTYPE_VCALL_INDICES (vid->derived), elt);
9594 /* The next vcall offset will be found at a more negative
9595 offset. */
9596 vid->index = size_binop (MINUS_EXPR, vid->index,
9597 ssize_int (TARGET_VTABLE_DATA_ENTRY_DISTANCE));
9599 /* Keep track of this function. */
9600 vec_safe_push (vid->fns, orig_fn);
9602 if (vid->generate_vcall_entries)
9604 tree base;
9605 tree fn;
9607 /* Find the overriding function. */
9608 fn = find_final_overrider (vid->rtti_binfo, binfo, orig_fn);
9609 if (fn == error_mark_node)
9610 vcall_offset = build_zero_cst (vtable_entry_type);
9611 else
9613 base = TREE_VALUE (fn);
9615 /* The vbase we're working on is a primary base of
9616 vid->binfo. But it might be a lost primary, so its
9617 BINFO_OFFSET might be wrong, so we just use the
9618 BINFO_OFFSET from vid->binfo. */
9619 vcall_offset = size_diffop_loc (input_location,
9620 BINFO_OFFSET (base),
9621 BINFO_OFFSET (vid->binfo));
9622 vcall_offset = fold_build1_loc (input_location,
9623 NOP_EXPR, vtable_entry_type,
9624 vcall_offset);
9626 /* Add the initializer to the vtable. */
9627 CONSTRUCTOR_APPEND_ELT (vid->inits, NULL_TREE, vcall_offset);
9631 /* Return vtbl initializers for the RTTI entries corresponding to the
9632 BINFO's vtable. The RTTI entries should indicate the object given
9633 by VID->rtti_binfo. */
9635 static void
9636 build_rtti_vtbl_entries (tree binfo, vtbl_init_data* vid)
9638 tree b;
9639 tree t;
9640 tree offset;
9641 tree decl;
9642 tree init;
9644 t = BINFO_TYPE (vid->rtti_binfo);
9646 /* To find the complete object, we will first convert to our most
9647 primary base, and then add the offset in the vtbl to that value. */
9648 b = most_primary_binfo (binfo);
9649 offset = size_diffop_loc (input_location,
9650 BINFO_OFFSET (vid->rtti_binfo), BINFO_OFFSET (b));
9652 /* The second entry is the address of the typeinfo object. */
9653 if (flag_rtti)
9654 decl = build_address (get_tinfo_decl (t));
9655 else
9656 decl = integer_zero_node;
9658 /* Convert the declaration to a type that can be stored in the
9659 vtable. */
9660 init = build_nop (vfunc_ptr_type_node, decl);
9661 CONSTRUCTOR_APPEND_ELT (vid->inits, NULL_TREE, init);
9663 /* Add the offset-to-top entry. It comes earlier in the vtable than
9664 the typeinfo entry. Convert the offset to look like a
9665 function pointer, so that we can put it in the vtable. */
9666 init = build_nop (vfunc_ptr_type_node, offset);
9667 CONSTRUCTOR_APPEND_ELT (vid->inits, NULL_TREE, init);
9670 /* TRUE iff TYPE is uniquely derived from PARENT. Ignores
9671 accessibility. */
9673 bool
9674 uniquely_derived_from_p (tree parent, tree type)
9676 tree base = lookup_base (type, parent, ba_unique, NULL, tf_none);
9677 return base && base != error_mark_node;
9680 /* TRUE iff TYPE is publicly & uniquely derived from PARENT. */
9682 bool
9683 publicly_uniquely_derived_p (tree parent, tree type)
9685 tree base = lookup_base (type, parent, ba_ignore_scope | ba_check,
9686 NULL, tf_none);
9687 return base && base != error_mark_node;
9690 /* CTX1 and CTX2 are declaration contexts. Return the innermost common
9691 class between them, if any. */
9693 tree
9694 common_enclosing_class (tree ctx1, tree ctx2)
9696 if (!TYPE_P (ctx1) || !TYPE_P (ctx2))
9697 return NULL_TREE;
9698 gcc_assert (ctx1 == TYPE_MAIN_VARIANT (ctx1)
9699 && ctx2 == TYPE_MAIN_VARIANT (ctx2));
9700 if (ctx1 == ctx2)
9701 return ctx1;
9702 for (tree t = ctx1; TYPE_P (t); t = TYPE_CONTEXT (t))
9703 TYPE_MARKED_P (t) = true;
9704 tree found = NULL_TREE;
9705 for (tree t = ctx2; TYPE_P (t); t = TYPE_CONTEXT (t))
9706 if (TYPE_MARKED_P (t))
9708 found = t;
9709 break;
9711 for (tree t = ctx1; TYPE_P (t); t = TYPE_CONTEXT (t))
9712 TYPE_MARKED_P (t) = false;
9713 return found;
9716 #include "gt-cp-class.h"