[PR c++/84426] ICE after conflicting member decl
[official-gcc.git] / gcc / cp / class.c
blob8348552a05b613762b2f385cf608d2372c8ac96c
1 /* Functions related to building classes and their related objects.
2 Copyright (C) 1987-2018 Free Software Foundation, Inc.
3 Contributed by Michael Tiemann (tiemann@cygnus.com)
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
10 any later version.
12 GCC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
22 /* High-level class interface. */
24 #include "config.h"
25 #include "system.h"
26 #include "coretypes.h"
27 #include "target.h"
28 #include "cp-tree.h"
29 #include "stringpool.h"
30 #include "cgraph.h"
31 #include "stor-layout.h"
32 #include "attribs.h"
33 #include "flags.h"
34 #include "toplev.h"
35 #include "convert.h"
36 #include "dumpfile.h"
37 #include "gimplify.h"
38 #include "intl.h"
39 #include "asan.h"
41 /* Id for dumping the class hierarchy. */
42 int class_dump_id;
44 /* The number of nested classes being processed. If we are not in the
45 scope of any class, this is zero. */
47 int current_class_depth;
49 /* In order to deal with nested classes, we keep a stack of classes.
50 The topmost entry is the innermost class, and is the entry at index
51 CURRENT_CLASS_DEPTH */
53 typedef struct class_stack_node {
54 /* The name of the class. */
55 tree name;
57 /* The _TYPE node for the class. */
58 tree type;
60 /* The access specifier pending for new declarations in the scope of
61 this class. */
62 tree access;
64 /* If were defining TYPE, the names used in this class. */
65 splay_tree names_used;
67 /* Nonzero if this class is no longer open, because of a call to
68 push_to_top_level. */
69 size_t hidden;
70 }* class_stack_node_t;
72 struct vtbl_init_data
74 /* The base for which we're building initializers. */
75 tree binfo;
76 /* The type of the most-derived type. */
77 tree derived;
78 /* The binfo for the dynamic type. This will be TYPE_BINFO (derived),
79 unless ctor_vtbl_p is true. */
80 tree rtti_binfo;
81 /* The negative-index vtable initializers built up so far. These
82 are in order from least negative index to most negative index. */
83 vec<constructor_elt, va_gc> *inits;
84 /* The binfo for the virtual base for which we're building
85 vcall offset initializers. */
86 tree vbase;
87 /* The functions in vbase for which we have already provided vcall
88 offsets. */
89 vec<tree, va_gc> *fns;
90 /* The vtable index of the next vcall or vbase offset. */
91 tree index;
92 /* Nonzero if we are building the initializer for the primary
93 vtable. */
94 int primary_vtbl_p;
95 /* Nonzero if we are building the initializer for a construction
96 vtable. */
97 int ctor_vtbl_p;
98 /* True when adding vcall offset entries to the vtable. False when
99 merely computing the indices. */
100 bool generate_vcall_entries;
103 /* The type of a function passed to walk_subobject_offsets. */
104 typedef int (*subobject_offset_fn) (tree, tree, splay_tree);
106 /* The stack itself. This is a dynamically resized array. The
107 number of elements allocated is CURRENT_CLASS_STACK_SIZE. */
108 static int current_class_stack_size;
109 static class_stack_node_t current_class_stack;
111 /* The size of the largest empty class seen in this translation unit. */
112 static GTY (()) tree sizeof_biggest_empty_class;
114 /* An array of all local classes present in this translation unit, in
115 declaration order. */
116 vec<tree, va_gc> *local_classes;
118 static tree get_vfield_name (tree);
119 static void finish_struct_anon (tree);
120 static tree get_vtable_name (tree);
121 static void get_basefndecls (tree, tree, vec<tree> *);
122 static int build_primary_vtable (tree, tree);
123 static int build_secondary_vtable (tree);
124 static void finish_vtbls (tree);
125 static void modify_vtable_entry (tree, tree, tree, tree, tree *);
126 static void finish_struct_bits (tree);
127 static int alter_access (tree, tree, tree);
128 static void handle_using_decl (tree, tree);
129 static tree dfs_modify_vtables (tree, void *);
130 static tree modify_all_vtables (tree, tree);
131 static void determine_primary_bases (tree);
132 static void maybe_warn_about_overly_private_class (tree);
133 static void add_implicitly_declared_members (tree, tree*, int, int);
134 static tree fixed_type_or_null (tree, int *, int *);
135 static tree build_simple_base_path (tree expr, tree binfo);
136 static tree build_vtbl_ref_1 (tree, tree);
137 static void build_vtbl_initializer (tree, tree, tree, tree, int *,
138 vec<constructor_elt, va_gc> **);
139 static bool check_bitfield_decl (tree);
140 static bool check_field_decl (tree, tree, int *, int *);
141 static void check_field_decls (tree, tree *, int *, int *);
142 static tree *build_base_field (record_layout_info, tree, splay_tree, tree *);
143 static void build_base_fields (record_layout_info, splay_tree, tree *);
144 static void check_methods (tree);
145 static void remove_zero_width_bit_fields (tree);
146 static bool accessible_nvdtor_p (tree);
148 /* Used by find_flexarrays and related functions. */
149 struct flexmems_t;
150 static void diagnose_flexarrays (tree, const flexmems_t *);
151 static void find_flexarrays (tree, flexmems_t *, bool = false,
152 tree = NULL_TREE, tree = NULL_TREE);
153 static void check_flexarrays (tree, flexmems_t * = NULL, bool = false);
154 static void check_bases (tree, int *, int *);
155 static void check_bases_and_members (tree);
156 static tree create_vtable_ptr (tree, tree *);
157 static void include_empty_classes (record_layout_info);
158 static void layout_class_type (tree, tree *);
159 static void propagate_binfo_offsets (tree, tree);
160 static void layout_virtual_bases (record_layout_info, splay_tree);
161 static void build_vbase_offset_vtbl_entries (tree, vtbl_init_data *);
162 static void add_vcall_offset_vtbl_entries_r (tree, vtbl_init_data *);
163 static void add_vcall_offset_vtbl_entries_1 (tree, vtbl_init_data *);
164 static void build_vcall_offset_vtbl_entries (tree, vtbl_init_data *);
165 static void add_vcall_offset (tree, tree, vtbl_init_data *);
166 static void layout_vtable_decl (tree, int);
167 static tree dfs_find_final_overrider_pre (tree, void *);
168 static tree dfs_find_final_overrider_post (tree, void *);
169 static tree find_final_overrider (tree, tree, tree);
170 static int make_new_vtable (tree, tree);
171 static tree get_primary_binfo (tree);
172 static int maybe_indent_hierarchy (FILE *, int, int);
173 static tree dump_class_hierarchy_r (FILE *, dump_flags_t, tree, tree, int);
174 static void dump_class_hierarchy (tree);
175 static void dump_class_hierarchy_1 (FILE *, dump_flags_t, tree);
176 static void dump_array (FILE *, tree);
177 static void dump_vtable (tree, tree, tree);
178 static void dump_vtt (tree, tree);
179 static void dump_thunk (FILE *, int, tree);
180 static tree build_vtable (tree, tree, tree);
181 static void initialize_vtable (tree, vec<constructor_elt, va_gc> *);
182 static void layout_nonempty_base_or_field (record_layout_info,
183 tree, tree, splay_tree);
184 static tree end_of_class (tree, int);
185 static bool layout_empty_base (record_layout_info, tree, tree, splay_tree);
186 static void accumulate_vtbl_inits (tree, tree, tree, tree, tree,
187 vec<constructor_elt, va_gc> **);
188 static void dfs_accumulate_vtbl_inits (tree, tree, tree, tree, tree,
189 vec<constructor_elt, va_gc> **);
190 static void build_rtti_vtbl_entries (tree, vtbl_init_data *);
191 static void build_vcall_and_vbase_vtbl_entries (tree, vtbl_init_data *);
192 static void clone_constructors_and_destructors (tree);
193 static tree build_clone (tree, tree);
194 static void update_vtable_entry_for_fn (tree, tree, tree, tree *, unsigned);
195 static void build_ctor_vtbl_group (tree, tree);
196 static void build_vtt (tree);
197 static tree binfo_ctor_vtable (tree);
198 static void build_vtt_inits (tree, tree, vec<constructor_elt, va_gc> **,
199 tree *);
200 static tree dfs_build_secondary_vptr_vtt_inits (tree, void *);
201 static tree dfs_fixup_binfo_vtbls (tree, void *);
202 static int record_subobject_offset (tree, tree, splay_tree);
203 static int check_subobject_offset (tree, tree, splay_tree);
204 static int walk_subobject_offsets (tree, subobject_offset_fn,
205 tree, splay_tree, tree, int);
206 static void record_subobject_offsets (tree, tree, splay_tree, bool);
207 static int layout_conflict_p (tree, tree, splay_tree, int);
208 static int splay_tree_compare_integer_csts (splay_tree_key k1,
209 splay_tree_key k2);
210 static void warn_about_ambiguous_bases (tree);
211 static bool type_requires_array_cookie (tree);
212 static bool base_derived_from (tree, tree);
213 static int empty_base_at_nonzero_offset_p (tree, tree, splay_tree);
214 static tree end_of_base (tree);
215 static tree get_vcall_index (tree, tree);
216 static bool type_maybe_constexpr_default_constructor (tree);
218 /* Return a COND_EXPR that executes TRUE_STMT if this execution of the
219 'structor is in charge of 'structing virtual bases, or FALSE_STMT
220 otherwise. */
222 tree
223 build_if_in_charge (tree true_stmt, tree false_stmt)
225 gcc_assert (DECL_HAS_IN_CHARGE_PARM_P (current_function_decl));
226 tree cmp = build2 (NE_EXPR, boolean_type_node,
227 current_in_charge_parm, integer_zero_node);
228 tree type = unlowered_expr_type (true_stmt);
229 if (VOID_TYPE_P (type))
230 type = unlowered_expr_type (false_stmt);
231 tree cond = build3 (COND_EXPR, type,
232 cmp, true_stmt, false_stmt);
233 return cond;
236 /* Convert to or from a base subobject. EXPR is an expression of type
237 `A' or `A*', an expression of type `B' or `B*' is returned. To
238 convert A to a base B, CODE is PLUS_EXPR and BINFO is the binfo for
239 the B base instance within A. To convert base A to derived B, CODE
240 is MINUS_EXPR and BINFO is the binfo for the A instance within B.
241 In this latter case, A must not be a morally virtual base of B.
242 NONNULL is true if EXPR is known to be non-NULL (this is only
243 needed when EXPR is of pointer type). CV qualifiers are preserved
244 from EXPR. */
246 tree
247 build_base_path (enum tree_code code,
248 tree expr,
249 tree binfo,
250 int nonnull,
251 tsubst_flags_t complain)
253 tree v_binfo = NULL_TREE;
254 tree d_binfo = NULL_TREE;
255 tree probe;
256 tree offset;
257 tree target_type;
258 tree null_test = NULL;
259 tree ptr_target_type;
260 int fixed_type_p;
261 int want_pointer = TYPE_PTR_P (TREE_TYPE (expr));
262 bool has_empty = false;
263 bool virtual_access;
264 bool rvalue = false;
266 if (expr == error_mark_node || binfo == error_mark_node || !binfo)
267 return error_mark_node;
269 for (probe = binfo; probe; probe = BINFO_INHERITANCE_CHAIN (probe))
271 d_binfo = probe;
272 if (is_empty_class (BINFO_TYPE (probe)))
273 has_empty = true;
274 if (!v_binfo && BINFO_VIRTUAL_P (probe))
275 v_binfo = probe;
278 probe = TYPE_MAIN_VARIANT (TREE_TYPE (expr));
279 if (want_pointer)
280 probe = TYPE_MAIN_VARIANT (TREE_TYPE (probe));
282 if (code == PLUS_EXPR
283 && !SAME_BINFO_TYPE_P (BINFO_TYPE (d_binfo), probe))
285 /* This can happen when adjust_result_of_qualified_name_lookup can't
286 find a unique base binfo in a call to a member function. We
287 couldn't give the diagnostic then since we might have been calling
288 a static member function, so we do it now. In other cases, eg.
289 during error recovery (c++/71979), we may not have a base at all. */
290 if (complain & tf_error)
292 tree base = lookup_base (probe, BINFO_TYPE (d_binfo),
293 ba_unique, NULL, complain);
294 gcc_assert (base == error_mark_node || !base);
296 return error_mark_node;
299 gcc_assert ((code == MINUS_EXPR
300 && SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), probe))
301 || code == PLUS_EXPR);
303 if (binfo == d_binfo)
304 /* Nothing to do. */
305 return expr;
307 if (code == MINUS_EXPR && v_binfo)
309 if (complain & tf_error)
311 if (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), BINFO_TYPE (v_binfo)))
313 if (want_pointer)
314 error ("cannot convert from pointer to base class %qT to "
315 "pointer to derived class %qT because the base is "
316 "virtual", BINFO_TYPE (binfo), BINFO_TYPE (d_binfo));
317 else
318 error ("cannot convert from base class %qT to derived "
319 "class %qT because the base is virtual",
320 BINFO_TYPE (binfo), BINFO_TYPE (d_binfo));
322 else
324 if (want_pointer)
325 error ("cannot convert from pointer to base class %qT to "
326 "pointer to derived class %qT via virtual base %qT",
327 BINFO_TYPE (binfo), BINFO_TYPE (d_binfo),
328 BINFO_TYPE (v_binfo));
329 else
330 error ("cannot convert from base class %qT to derived "
331 "class %qT via virtual base %qT", BINFO_TYPE (binfo),
332 BINFO_TYPE (d_binfo), BINFO_TYPE (v_binfo));
335 return error_mark_node;
338 if (!want_pointer)
340 rvalue = !lvalue_p (expr);
341 /* This must happen before the call to save_expr. */
342 expr = cp_build_addr_expr (expr, complain);
344 else
345 expr = mark_rvalue_use (expr);
347 offset = BINFO_OFFSET (binfo);
348 fixed_type_p = resolves_to_fixed_type_p (expr, &nonnull);
349 target_type = code == PLUS_EXPR ? BINFO_TYPE (binfo) : BINFO_TYPE (d_binfo);
350 /* TARGET_TYPE has been extracted from BINFO, and, is therefore always
351 cv-unqualified. Extract the cv-qualifiers from EXPR so that the
352 expression returned matches the input. */
353 target_type = cp_build_qualified_type
354 (target_type, cp_type_quals (TREE_TYPE (TREE_TYPE (expr))));
355 ptr_target_type = build_pointer_type (target_type);
357 /* Do we need to look in the vtable for the real offset? */
358 virtual_access = (v_binfo && fixed_type_p <= 0);
360 /* Don't bother with the calculations inside sizeof; they'll ICE if the
361 source type is incomplete and the pointer value doesn't matter. In a
362 template (even in instantiate_non_dependent_expr), we don't have vtables
363 set up properly yet, and the value doesn't matter there either; we're
364 just interested in the result of overload resolution. */
365 if (cp_unevaluated_operand != 0
366 || processing_template_decl
367 || in_template_function ())
369 expr = build_nop (ptr_target_type, expr);
370 goto indout;
373 /* If we're in an NSDMI, we don't have the full constructor context yet
374 that we need for converting to a virtual base, so just build a stub
375 CONVERT_EXPR and expand it later in bot_replace. */
376 if (virtual_access && fixed_type_p < 0
377 && current_scope () != current_function_decl)
379 expr = build1 (CONVERT_EXPR, ptr_target_type, expr);
380 CONVERT_EXPR_VBASE_PATH (expr) = true;
381 goto indout;
384 /* Do we need to check for a null pointer? */
385 if (want_pointer && !nonnull)
387 /* If we know the conversion will not actually change the value
388 of EXPR, then we can avoid testing the expression for NULL.
389 We have to avoid generating a COMPONENT_REF for a base class
390 field, because other parts of the compiler know that such
391 expressions are always non-NULL. */
392 if (!virtual_access && integer_zerop (offset))
393 return build_nop (ptr_target_type, expr);
394 null_test = error_mark_node;
397 /* Protect against multiple evaluation if necessary. */
398 if (TREE_SIDE_EFFECTS (expr) && (null_test || virtual_access))
399 expr = save_expr (expr);
401 /* Now that we've saved expr, build the real null test. */
402 if (null_test)
404 tree zero = cp_convert (TREE_TYPE (expr), nullptr_node, complain);
405 null_test = build2_loc (input_location, NE_EXPR, boolean_type_node,
406 expr, zero);
407 /* This is a compiler generated comparison, don't emit
408 e.g. -Wnonnull-compare warning for it. */
409 TREE_NO_WARNING (null_test) = 1;
412 /* If this is a simple base reference, express it as a COMPONENT_REF. */
413 if (code == PLUS_EXPR && !virtual_access
414 /* We don't build base fields for empty bases, and they aren't very
415 interesting to the optimizers anyway. */
416 && !has_empty)
418 expr = cp_build_fold_indirect_ref (expr);
419 expr = build_simple_base_path (expr, binfo);
420 if (rvalue)
421 expr = move (expr);
422 if (want_pointer)
423 expr = build_address (expr);
424 target_type = TREE_TYPE (expr);
425 goto out;
428 if (virtual_access)
430 /* Going via virtual base V_BINFO. We need the static offset
431 from V_BINFO to BINFO, and the dynamic offset from D_BINFO to
432 V_BINFO. That offset is an entry in D_BINFO's vtable. */
433 tree v_offset;
435 if (fixed_type_p < 0 && in_base_initializer)
437 /* In a base member initializer, we cannot rely on the
438 vtable being set up. We have to indirect via the
439 vtt_parm. */
440 tree t;
442 t = TREE_TYPE (TYPE_VFIELD (current_class_type));
443 t = build_pointer_type (t);
444 v_offset = fold_convert (t, current_vtt_parm);
445 v_offset = cp_build_fold_indirect_ref (v_offset);
447 else
449 tree t = expr;
450 if (sanitize_flags_p (SANITIZE_VPTR)
451 && fixed_type_p == 0)
453 t = cp_ubsan_maybe_instrument_cast_to_vbase (input_location,
454 probe, expr);
455 if (t == NULL_TREE)
456 t = expr;
458 v_offset = build_vfield_ref (cp_build_fold_indirect_ref (t),
459 TREE_TYPE (TREE_TYPE (expr)));
462 if (v_offset == error_mark_node)
463 return error_mark_node;
465 v_offset = fold_build_pointer_plus (v_offset, BINFO_VPTR_FIELD (v_binfo));
466 v_offset = build1 (NOP_EXPR,
467 build_pointer_type (ptrdiff_type_node),
468 v_offset);
469 v_offset = cp_build_fold_indirect_ref (v_offset);
470 TREE_CONSTANT (v_offset) = 1;
472 offset = convert_to_integer (ptrdiff_type_node,
473 size_diffop_loc (input_location, offset,
474 BINFO_OFFSET (v_binfo)));
476 if (!integer_zerop (offset))
477 v_offset = build2 (code, ptrdiff_type_node, v_offset, offset);
479 if (fixed_type_p < 0)
480 /* Negative fixed_type_p means this is a constructor or destructor;
481 virtual base layout is fixed in in-charge [cd]tors, but not in
482 base [cd]tors. */
483 offset = build_if_in_charge
484 (convert_to_integer (ptrdiff_type_node, BINFO_OFFSET (binfo)),
485 v_offset);
486 else
487 offset = v_offset;
490 if (want_pointer)
491 target_type = ptr_target_type;
493 expr = build1 (NOP_EXPR, ptr_target_type, expr);
495 if (!integer_zerop (offset))
497 offset = fold_convert (sizetype, offset);
498 if (code == MINUS_EXPR)
499 offset = fold_build1_loc (input_location, NEGATE_EXPR, sizetype, offset);
500 expr = fold_build_pointer_plus (expr, offset);
502 else
503 null_test = NULL;
505 indout:
506 if (!want_pointer)
508 expr = cp_build_fold_indirect_ref (expr);
509 if (rvalue)
510 expr = move (expr);
513 out:
514 if (null_test)
515 expr = fold_build3_loc (input_location, COND_EXPR, target_type, null_test, expr,
516 build_zero_cst (target_type));
518 return expr;
521 /* Subroutine of build_base_path; EXPR and BINFO are as in that function.
522 Perform a derived-to-base conversion by recursively building up a
523 sequence of COMPONENT_REFs to the appropriate base fields. */
525 static tree
526 build_simple_base_path (tree expr, tree binfo)
528 tree type = BINFO_TYPE (binfo);
529 tree d_binfo = BINFO_INHERITANCE_CHAIN (binfo);
530 tree field;
532 if (d_binfo == NULL_TREE)
534 tree temp;
536 gcc_assert (TYPE_MAIN_VARIANT (TREE_TYPE (expr)) == type);
538 /* Transform `(a, b).x' into `(*(a, &b)).x', `(a ? b : c).x'
539 into `(*(a ? &b : &c)).x', and so on. A COND_EXPR is only
540 an lvalue in the front end; only _DECLs and _REFs are lvalues
541 in the back end. */
542 temp = unary_complex_lvalue (ADDR_EXPR, expr);
543 if (temp)
544 expr = cp_build_fold_indirect_ref (temp);
546 return expr;
549 /* Recurse. */
550 expr = build_simple_base_path (expr, d_binfo);
552 for (field = TYPE_FIELDS (BINFO_TYPE (d_binfo));
553 field; field = DECL_CHAIN (field))
554 /* Is this the base field created by build_base_field? */
555 if (TREE_CODE (field) == FIELD_DECL
556 && DECL_FIELD_IS_BASE (field)
557 && TREE_TYPE (field) == type
558 /* If we're looking for a field in the most-derived class,
559 also check the field offset; we can have two base fields
560 of the same type if one is an indirect virtual base and one
561 is a direct non-virtual base. */
562 && (BINFO_INHERITANCE_CHAIN (d_binfo)
563 || tree_int_cst_equal (byte_position (field),
564 BINFO_OFFSET (binfo))))
566 /* We don't use build_class_member_access_expr here, as that
567 has unnecessary checks, and more importantly results in
568 recursive calls to dfs_walk_once. */
569 int type_quals = cp_type_quals (TREE_TYPE (expr));
571 expr = build3 (COMPONENT_REF,
572 cp_build_qualified_type (type, type_quals),
573 expr, field, NULL_TREE);
574 /* Mark the expression const or volatile, as appropriate.
575 Even though we've dealt with the type above, we still have
576 to mark the expression itself. */
577 if (type_quals & TYPE_QUAL_CONST)
578 TREE_READONLY (expr) = 1;
579 if (type_quals & TYPE_QUAL_VOLATILE)
580 TREE_THIS_VOLATILE (expr) = 1;
582 return expr;
585 /* Didn't find the base field?!? */
586 gcc_unreachable ();
589 /* Convert OBJECT to the base TYPE. OBJECT is an expression whose
590 type is a class type or a pointer to a class type. In the former
591 case, TYPE is also a class type; in the latter it is another
592 pointer type. If CHECK_ACCESS is true, an error message is emitted
593 if TYPE is inaccessible. If OBJECT has pointer type, the value is
594 assumed to be non-NULL. */
596 tree
597 convert_to_base (tree object, tree type, bool check_access, bool nonnull,
598 tsubst_flags_t complain)
600 tree binfo;
601 tree object_type;
603 if (TYPE_PTR_P (TREE_TYPE (object)))
605 object_type = TREE_TYPE (TREE_TYPE (object));
606 type = TREE_TYPE (type);
608 else
609 object_type = TREE_TYPE (object);
611 binfo = lookup_base (object_type, type, check_access ? ba_check : ba_unique,
612 NULL, complain);
613 if (!binfo || binfo == error_mark_node)
614 return error_mark_node;
616 return build_base_path (PLUS_EXPR, object, binfo, nonnull, complain);
619 /* EXPR is an expression with unqualified class type. BASE is a base
620 binfo of that class type. Returns EXPR, converted to the BASE
621 type. This function assumes that EXPR is the most derived class;
622 therefore virtual bases can be found at their static offsets. */
624 tree
625 convert_to_base_statically (tree expr, tree base)
627 tree expr_type;
629 expr_type = TREE_TYPE (expr);
630 if (!SAME_BINFO_TYPE_P (BINFO_TYPE (base), expr_type))
632 /* If this is a non-empty base, use a COMPONENT_REF. */
633 if (!is_empty_class (BINFO_TYPE (base)))
634 return build_simple_base_path (expr, base);
636 /* We use fold_build2 and fold_convert below to simplify the trees
637 provided to the optimizers. It is not safe to call these functions
638 when processing a template because they do not handle C++-specific
639 trees. */
640 gcc_assert (!processing_template_decl);
641 expr = cp_build_addr_expr (expr, tf_warning_or_error);
642 if (!integer_zerop (BINFO_OFFSET (base)))
643 expr = fold_build_pointer_plus_loc (input_location,
644 expr, BINFO_OFFSET (base));
645 expr = fold_convert (build_pointer_type (BINFO_TYPE (base)), expr);
646 expr = build_fold_indirect_ref_loc (input_location, expr);
649 return expr;
653 tree
654 build_vfield_ref (tree datum, tree type)
656 tree vfield, vcontext;
658 if (datum == error_mark_node
659 /* Can happen in case of duplicate base types (c++/59082). */
660 || !TYPE_VFIELD (type))
661 return error_mark_node;
663 /* First, convert to the requested type. */
664 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (datum), type))
665 datum = convert_to_base (datum, type, /*check_access=*/false,
666 /*nonnull=*/true, tf_warning_or_error);
668 /* Second, the requested type may not be the owner of its own vptr.
669 If not, convert to the base class that owns it. We cannot use
670 convert_to_base here, because VCONTEXT may appear more than once
671 in the inheritance hierarchy of TYPE, and thus direct conversion
672 between the types may be ambiguous. Following the path back up
673 one step at a time via primary bases avoids the problem. */
674 vfield = TYPE_VFIELD (type);
675 vcontext = DECL_CONTEXT (vfield);
676 while (!same_type_ignoring_top_level_qualifiers_p (vcontext, type))
678 datum = build_simple_base_path (datum, CLASSTYPE_PRIMARY_BINFO (type));
679 type = TREE_TYPE (datum);
682 return build3 (COMPONENT_REF, TREE_TYPE (vfield), datum, vfield, NULL_TREE);
685 /* Given an object INSTANCE, return an expression which yields the
686 vtable element corresponding to INDEX. There are many special
687 cases for INSTANCE which we take care of here, mainly to avoid
688 creating extra tree nodes when we don't have to. */
690 static tree
691 build_vtbl_ref_1 (tree instance, tree idx)
693 tree aref;
694 tree vtbl = NULL_TREE;
696 /* Try to figure out what a reference refers to, and
697 access its virtual function table directly. */
699 int cdtorp = 0;
700 tree fixed_type = fixed_type_or_null (instance, NULL, &cdtorp);
702 tree basetype = non_reference (TREE_TYPE (instance));
704 if (fixed_type && !cdtorp)
706 tree binfo = lookup_base (fixed_type, basetype,
707 ba_unique, NULL, tf_none);
708 if (binfo && binfo != error_mark_node)
709 vtbl = unshare_expr (BINFO_VTABLE (binfo));
712 if (!vtbl)
713 vtbl = build_vfield_ref (instance, basetype);
715 aref = build_array_ref (input_location, vtbl, idx);
716 TREE_CONSTANT (aref) |= TREE_CONSTANT (vtbl) && TREE_CONSTANT (idx);
718 return aref;
721 tree
722 build_vtbl_ref (tree instance, tree idx)
724 tree aref = build_vtbl_ref_1 (instance, idx);
726 return aref;
729 /* Given a stable object pointer INSTANCE_PTR, return an expression which
730 yields a function pointer corresponding to vtable element INDEX. */
732 tree
733 build_vfn_ref (tree instance_ptr, tree idx)
735 tree aref;
737 aref = build_vtbl_ref_1 (cp_build_fold_indirect_ref (instance_ptr),
738 idx);
740 /* When using function descriptors, the address of the
741 vtable entry is treated as a function pointer. */
742 if (TARGET_VTABLE_USES_DESCRIPTORS)
743 aref = build1 (NOP_EXPR, TREE_TYPE (aref),
744 cp_build_addr_expr (aref, tf_warning_or_error));
746 /* Remember this as a method reference, for later devirtualization. */
747 aref = build3 (OBJ_TYPE_REF, TREE_TYPE (aref), aref, instance_ptr, idx);
749 return aref;
752 /* Return the name of the virtual function table (as an IDENTIFIER_NODE)
753 for the given TYPE. */
755 static tree
756 get_vtable_name (tree type)
758 return mangle_vtbl_for_type (type);
761 /* DECL is an entity associated with TYPE, like a virtual table or an
762 implicitly generated constructor. Determine whether or not DECL
763 should have external or internal linkage at the object file
764 level. This routine does not deal with COMDAT linkage and other
765 similar complexities; it simply sets TREE_PUBLIC if it possible for
766 entities in other translation units to contain copies of DECL, in
767 the abstract. */
769 void
770 set_linkage_according_to_type (tree /*type*/, tree decl)
772 TREE_PUBLIC (decl) = 1;
773 determine_visibility (decl);
776 /* Create a VAR_DECL for a primary or secondary vtable for CLASS_TYPE.
777 (For a secondary vtable for B-in-D, CLASS_TYPE should be D, not B.)
778 Use NAME for the name of the vtable, and VTABLE_TYPE for its type. */
780 static tree
781 build_vtable (tree class_type, tree name, tree vtable_type)
783 tree decl;
785 decl = build_lang_decl (VAR_DECL, name, vtable_type);
786 /* vtable names are already mangled; give them their DECL_ASSEMBLER_NAME
787 now to avoid confusion in mangle_decl. */
788 SET_DECL_ASSEMBLER_NAME (decl, name);
789 DECL_CONTEXT (decl) = class_type;
790 DECL_ARTIFICIAL (decl) = 1;
791 TREE_STATIC (decl) = 1;
792 TREE_READONLY (decl) = 1;
793 DECL_VIRTUAL_P (decl) = 1;
794 SET_DECL_ALIGN (decl, TARGET_VTABLE_ENTRY_ALIGN);
795 DECL_USER_ALIGN (decl) = true;
796 DECL_VTABLE_OR_VTT_P (decl) = 1;
797 set_linkage_according_to_type (class_type, decl);
798 /* The vtable has not been defined -- yet. */
799 DECL_EXTERNAL (decl) = 1;
800 DECL_NOT_REALLY_EXTERN (decl) = 1;
802 /* Mark the VAR_DECL node representing the vtable itself as a
803 "gratuitous" one, thereby forcing dwarfout.c to ignore it. It
804 is rather important that such things be ignored because any
805 effort to actually generate DWARF for them will run into
806 trouble when/if we encounter code like:
808 #pragma interface
809 struct S { virtual void member (); };
811 because the artificial declaration of the vtable itself (as
812 manufactured by the g++ front end) will say that the vtable is
813 a static member of `S' but only *after* the debug output for
814 the definition of `S' has already been output. This causes
815 grief because the DWARF entry for the definition of the vtable
816 will try to refer back to an earlier *declaration* of the
817 vtable as a static member of `S' and there won't be one. We
818 might be able to arrange to have the "vtable static member"
819 attached to the member list for `S' before the debug info for
820 `S' get written (which would solve the problem) but that would
821 require more intrusive changes to the g++ front end. */
822 DECL_IGNORED_P (decl) = 1;
824 return decl;
827 /* Get the VAR_DECL of the vtable for TYPE. TYPE need not be polymorphic,
828 or even complete. If this does not exist, create it. If COMPLETE is
829 nonzero, then complete the definition of it -- that will render it
830 impossible to actually build the vtable, but is useful to get at those
831 which are known to exist in the runtime. */
833 tree
834 get_vtable_decl (tree type, int complete)
836 tree decl;
838 if (CLASSTYPE_VTABLES (type))
839 return CLASSTYPE_VTABLES (type);
841 decl = build_vtable (type, get_vtable_name (type), vtbl_type_node);
842 CLASSTYPE_VTABLES (type) = decl;
844 if (complete)
846 DECL_EXTERNAL (decl) = 1;
847 cp_finish_decl (decl, NULL_TREE, false, NULL_TREE, 0);
850 return decl;
853 /* Build the primary virtual function table for TYPE. If BINFO is
854 non-NULL, build the vtable starting with the initial approximation
855 that it is the same as the one which is the head of the association
856 list. Returns a nonzero value if a new vtable is actually
857 created. */
859 static int
860 build_primary_vtable (tree binfo, tree type)
862 tree decl;
863 tree virtuals;
865 decl = get_vtable_decl (type, /*complete=*/0);
867 if (binfo)
869 if (BINFO_NEW_VTABLE_MARKED (binfo))
870 /* We have already created a vtable for this base, so there's
871 no need to do it again. */
872 return 0;
874 virtuals = copy_list (BINFO_VIRTUALS (binfo));
875 TREE_TYPE (decl) = TREE_TYPE (get_vtbl_decl_for_binfo (binfo));
876 DECL_SIZE (decl) = TYPE_SIZE (TREE_TYPE (decl));
877 DECL_SIZE_UNIT (decl) = TYPE_SIZE_UNIT (TREE_TYPE (decl));
879 else
881 gcc_assert (TREE_TYPE (decl) == vtbl_type_node);
882 virtuals = NULL_TREE;
885 /* Initialize the association list for this type, based
886 on our first approximation. */
887 BINFO_VTABLE (TYPE_BINFO (type)) = decl;
888 BINFO_VIRTUALS (TYPE_BINFO (type)) = virtuals;
889 SET_BINFO_NEW_VTABLE_MARKED (TYPE_BINFO (type));
890 return 1;
893 /* Give BINFO a new virtual function table which is initialized
894 with a skeleton-copy of its original initialization. The only
895 entry that changes is the `delta' entry, so we can really
896 share a lot of structure.
898 FOR_TYPE is the most derived type which caused this table to
899 be needed.
901 Returns nonzero if we haven't met BINFO before.
903 The order in which vtables are built (by calling this function) for
904 an object must remain the same, otherwise a binary incompatibility
905 can result. */
907 static int
908 build_secondary_vtable (tree binfo)
910 if (BINFO_NEW_VTABLE_MARKED (binfo))
911 /* We already created a vtable for this base. There's no need to
912 do it again. */
913 return 0;
915 /* Remember that we've created a vtable for this BINFO, so that we
916 don't try to do so again. */
917 SET_BINFO_NEW_VTABLE_MARKED (binfo);
919 /* Make fresh virtual list, so we can smash it later. */
920 BINFO_VIRTUALS (binfo) = copy_list (BINFO_VIRTUALS (binfo));
922 /* Secondary vtables are laid out as part of the same structure as
923 the primary vtable. */
924 BINFO_VTABLE (binfo) = NULL_TREE;
925 return 1;
928 /* Create a new vtable for BINFO which is the hierarchy dominated by
929 T. Return nonzero if we actually created a new vtable. */
931 static int
932 make_new_vtable (tree t, tree binfo)
934 if (binfo == TYPE_BINFO (t))
935 /* In this case, it is *type*'s vtable we are modifying. We start
936 with the approximation that its vtable is that of the
937 immediate base class. */
938 return build_primary_vtable (binfo, t);
939 else
940 /* This is our very own copy of `basetype' to play with. Later,
941 we will fill in all the virtual functions that override the
942 virtual functions in these base classes which are not defined
943 by the current type. */
944 return build_secondary_vtable (binfo);
947 /* Make *VIRTUALS, an entry on the BINFO_VIRTUALS list for BINFO
948 (which is in the hierarchy dominated by T) list FNDECL as its
949 BV_FN. DELTA is the required constant adjustment from the `this'
950 pointer where the vtable entry appears to the `this' required when
951 the function is actually called. */
953 static void
954 modify_vtable_entry (tree t,
955 tree binfo,
956 tree fndecl,
957 tree delta,
958 tree *virtuals)
960 tree v;
962 v = *virtuals;
964 if (fndecl != BV_FN (v)
965 || !tree_int_cst_equal (delta, BV_DELTA (v)))
967 /* We need a new vtable for BINFO. */
968 if (make_new_vtable (t, binfo))
970 /* If we really did make a new vtable, we also made a copy
971 of the BINFO_VIRTUALS list. Now, we have to find the
972 corresponding entry in that list. */
973 *virtuals = BINFO_VIRTUALS (binfo);
974 while (BV_FN (*virtuals) != BV_FN (v))
975 *virtuals = TREE_CHAIN (*virtuals);
976 v = *virtuals;
979 BV_DELTA (v) = delta;
980 BV_VCALL_INDEX (v) = NULL_TREE;
981 BV_FN (v) = fndecl;
986 /* Add method METHOD to class TYPE. If VIA_USING indicates whether
987 METHOD is being injected via a using_decl. Returns true if the
988 method could be added to the method vec. */
990 bool
991 add_method (tree type, tree method, bool via_using)
993 if (method == error_mark_node)
994 return false;
996 gcc_assert (!DECL_EXTERN_C_P (method));
998 tree *slot = find_member_slot (type, DECL_NAME (method));
999 tree current_fns = slot ? *slot : NULL_TREE;
1001 /* Check to see if we've already got this method. */
1002 for (ovl_iterator iter (current_fns); iter; ++iter)
1004 tree fn = *iter;
1005 tree fn_type;
1006 tree method_type;
1007 tree parms1;
1008 tree parms2;
1010 if (TREE_CODE (fn) != TREE_CODE (method))
1011 continue;
1013 /* Two using-declarations can coexist, we'll complain about ambiguity in
1014 overload resolution. */
1015 if (via_using && iter.using_p ()
1016 /* Except handle inherited constructors specially. */
1017 && ! DECL_CONSTRUCTOR_P (fn))
1018 continue;
1020 /* [over.load] Member function declarations with the
1021 same name and the same parameter types cannot be
1022 overloaded if any of them is a static member
1023 function declaration.
1025 [over.load] Member function declarations with the same name and
1026 the same parameter-type-list as well as member function template
1027 declarations with the same name, the same parameter-type-list, and
1028 the same template parameter lists cannot be overloaded if any of
1029 them, but not all, have a ref-qualifier.
1031 [namespace.udecl] When a using-declaration brings names
1032 from a base class into a derived class scope, member
1033 functions in the derived class override and/or hide member
1034 functions with the same name and parameter types in a base
1035 class (rather than conflicting). */
1036 fn_type = TREE_TYPE (fn);
1037 method_type = TREE_TYPE (method);
1038 parms1 = TYPE_ARG_TYPES (fn_type);
1039 parms2 = TYPE_ARG_TYPES (method_type);
1041 /* Compare the quals on the 'this' parm. Don't compare
1042 the whole types, as used functions are treated as
1043 coming from the using class in overload resolution. */
1044 if (! DECL_STATIC_FUNCTION_P (fn)
1045 && ! DECL_STATIC_FUNCTION_P (method)
1046 /* Either both or neither need to be ref-qualified for
1047 differing quals to allow overloading. */
1048 && (FUNCTION_REF_QUALIFIED (fn_type)
1049 == FUNCTION_REF_QUALIFIED (method_type))
1050 && (type_memfn_quals (fn_type) != type_memfn_quals (method_type)
1051 || type_memfn_rqual (fn_type) != type_memfn_rqual (method_type)))
1052 continue;
1054 /* For templates, the return type and template parameters
1055 must be identical. */
1056 if (TREE_CODE (fn) == TEMPLATE_DECL
1057 && (!same_type_p (TREE_TYPE (fn_type),
1058 TREE_TYPE (method_type))
1059 || !comp_template_parms (DECL_TEMPLATE_PARMS (fn),
1060 DECL_TEMPLATE_PARMS (method))))
1061 continue;
1063 if (! DECL_STATIC_FUNCTION_P (fn))
1064 parms1 = TREE_CHAIN (parms1);
1065 if (! DECL_STATIC_FUNCTION_P (method))
1066 parms2 = TREE_CHAIN (parms2);
1068 /* Bring back parameters omitted from an inherited ctor. */
1069 if (ctor_omit_inherited_parms (fn))
1070 parms1 = FUNCTION_FIRST_USER_PARMTYPE (DECL_ORIGIN (fn));
1071 if (ctor_omit_inherited_parms (method))
1072 parms2 = FUNCTION_FIRST_USER_PARMTYPE (DECL_ORIGIN (method));
1074 if (compparms (parms1, parms2)
1075 && (!DECL_CONV_FN_P (fn)
1076 || same_type_p (TREE_TYPE (fn_type),
1077 TREE_TYPE (method_type)))
1078 && equivalently_constrained (fn, method))
1080 /* If these are versions of the same function, process and
1081 move on. */
1082 if (TREE_CODE (fn) == FUNCTION_DECL
1083 && maybe_version_functions (method, fn, true))
1084 continue;
1086 if (DECL_INHERITED_CTOR (method))
1088 if (DECL_INHERITED_CTOR (fn))
1090 tree basem = DECL_INHERITED_CTOR_BASE (method);
1091 tree basef = DECL_INHERITED_CTOR_BASE (fn);
1092 if (flag_new_inheriting_ctors)
1094 if (basem == basef)
1096 /* Inheriting the same constructor along different
1097 paths, combine them. */
1098 SET_DECL_INHERITED_CTOR
1099 (fn, ovl_make (DECL_INHERITED_CTOR (method),
1100 DECL_INHERITED_CTOR (fn)));
1101 /* And discard the new one. */
1102 return false;
1104 else
1105 /* Inherited ctors can coexist until overload
1106 resolution. */
1107 continue;
1109 error_at (DECL_SOURCE_LOCATION (method),
1110 "%q#D conflicts with version inherited from %qT",
1111 method, basef);
1112 inform (DECL_SOURCE_LOCATION (fn),
1113 "version inherited from %qT declared here",
1114 basef);
1116 /* Otherwise defer to the other function. */
1117 return false;
1120 if (via_using)
1121 /* Defer to the local function. */
1122 return false;
1123 else if (flag_new_inheriting_ctors
1124 && DECL_INHERITED_CTOR (fn))
1126 /* Remove the inherited constructor. */
1127 current_fns = iter.remove_node (current_fns);
1128 continue;
1130 else
1132 error_at (DECL_SOURCE_LOCATION (method),
1133 "%q#D cannot be overloaded with %q#D", method, fn);
1134 inform (DECL_SOURCE_LOCATION (fn),
1135 "previous declaration %q#D", fn);
1136 return false;
1141 /* A class should never have more than one destructor. */
1142 gcc_assert (!current_fns || !DECL_DESTRUCTOR_P (method));
1144 current_fns = ovl_insert (method, current_fns, via_using);
1146 if (!COMPLETE_TYPE_P (type) && !DECL_CONV_FN_P (method)
1147 && !push_class_level_binding (DECL_NAME (method), current_fns))
1148 return false;
1150 if (!slot)
1151 slot = add_member_slot (type, DECL_NAME (method));
1153 /* Maintain TYPE_HAS_USER_CONSTRUCTOR, etc. */
1154 grok_special_member_properties (method);
1156 *slot = current_fns;
1158 return true;
1161 /* Subroutines of finish_struct. */
1163 /* Change the access of FDECL to ACCESS in T. Return 1 if change was
1164 legit, otherwise return 0. */
1166 static int
1167 alter_access (tree t, tree fdecl, tree access)
1169 tree elem;
1171 retrofit_lang_decl (fdecl);
1173 gcc_assert (!DECL_DISCRIMINATOR_P (fdecl));
1175 elem = purpose_member (t, DECL_ACCESS (fdecl));
1176 if (elem)
1178 if (TREE_VALUE (elem) != access)
1180 if (TREE_CODE (TREE_TYPE (fdecl)) == FUNCTION_DECL)
1181 error ("conflicting access specifications for method"
1182 " %q+D, ignored", TREE_TYPE (fdecl));
1183 else
1184 error ("conflicting access specifications for field %qE, ignored",
1185 DECL_NAME (fdecl));
1187 else
1189 /* They're changing the access to the same thing they changed
1190 it to before. That's OK. */
1194 else
1196 perform_or_defer_access_check (TYPE_BINFO (t), fdecl, fdecl,
1197 tf_warning_or_error);
1198 DECL_ACCESS (fdecl) = tree_cons (t, access, DECL_ACCESS (fdecl));
1199 return 1;
1201 return 0;
1204 /* Return the access node for DECL's access in its enclosing class. */
1206 tree
1207 declared_access (tree decl)
1209 return (TREE_PRIVATE (decl) ? access_private_node
1210 : TREE_PROTECTED (decl) ? access_protected_node
1211 : access_public_node);
1214 /* Process the USING_DECL, which is a member of T. */
1216 static void
1217 handle_using_decl (tree using_decl, tree t)
1219 tree decl = USING_DECL_DECLS (using_decl);
1220 tree name = DECL_NAME (using_decl);
1221 tree access = declared_access (using_decl);
1222 tree flist = NULL_TREE;
1223 tree old_value;
1225 gcc_assert (!processing_template_decl && decl);
1227 old_value = lookup_member (t, name, /*protect=*/0, /*want_type=*/false,
1228 tf_warning_or_error);
1229 if (old_value)
1231 old_value = OVL_FIRST (old_value);
1233 if (DECL_P (old_value) && DECL_CONTEXT (old_value) == t)
1234 /* OK */;
1235 else
1236 old_value = NULL_TREE;
1239 cp_emit_debug_info_for_using (decl, t);
1241 if (is_overloaded_fn (decl))
1242 flist = decl;
1244 if (! old_value)
1246 else if (is_overloaded_fn (old_value))
1248 if (flist)
1249 /* It's OK to use functions from a base when there are functions with
1250 the same name already present in the current class. */;
1251 else
1253 error_at (DECL_SOURCE_LOCATION (using_decl), "%qD invalid in %q#T "
1254 "because of local method %q#D with same name",
1255 using_decl, t, old_value);
1256 inform (DECL_SOURCE_LOCATION (old_value),
1257 "local method %q#D declared here", old_value);
1258 return;
1261 else if (!DECL_ARTIFICIAL (old_value))
1263 error_at (DECL_SOURCE_LOCATION (using_decl), "%qD invalid in %q#T "
1264 "because of local member %q#D with same name",
1265 using_decl, t, old_value);
1266 inform (DECL_SOURCE_LOCATION (old_value),
1267 "local member %q#D declared here", old_value);
1268 return;
1271 /* Make type T see field decl FDECL with access ACCESS. */
1272 if (flist)
1273 for (ovl_iterator iter (flist); iter; ++iter)
1275 add_method (t, *iter, true);
1276 alter_access (t, *iter, access);
1278 else
1279 alter_access (t, decl, access);
1282 /* Data structure for find_abi_tags_r, below. */
1284 struct abi_tag_data
1286 tree t; // The type that we're checking for missing tags.
1287 tree subob; // The subobject of T that we're getting tags from.
1288 tree tags; // error_mark_node for diagnostics, or a list of missing tags.
1291 /* Subroutine of find_abi_tags_r. Handle a single TAG found on the class TP
1292 in the context of P. TAG can be either an identifier (the DECL_NAME of
1293 a tag NAMESPACE_DECL) or a STRING_CST (a tag attribute). */
1295 static void
1296 check_tag (tree tag, tree id, tree *tp, abi_tag_data *p)
1298 if (!IDENTIFIER_MARKED (id))
1300 if (p->tags != error_mark_node)
1302 /* We're collecting tags from template arguments or from
1303 the type of a variable or function return type. */
1304 p->tags = tree_cons (NULL_TREE, tag, p->tags);
1306 /* Don't inherit this tag multiple times. */
1307 IDENTIFIER_MARKED (id) = true;
1309 if (TYPE_P (p->t))
1311 /* Tags inherited from type template arguments are only used
1312 to avoid warnings. */
1313 ABI_TAG_IMPLICIT (p->tags) = true;
1314 return;
1316 /* For functions and variables we want to warn, too. */
1319 /* Otherwise we're diagnosing missing tags. */
1320 if (TREE_CODE (p->t) == FUNCTION_DECL)
1322 if (warning (OPT_Wabi_tag, "%qD inherits the %E ABI tag "
1323 "that %qT (used in its return type) has",
1324 p->t, tag, *tp))
1325 inform (location_of (*tp), "%qT declared here", *tp);
1327 else if (VAR_P (p->t))
1329 if (warning (OPT_Wabi_tag, "%qD inherits the %E ABI tag "
1330 "that %qT (used in its type) has", p->t, tag, *tp))
1331 inform (location_of (*tp), "%qT declared here", *tp);
1333 else if (TYPE_P (p->subob))
1335 if (warning (OPT_Wabi_tag, "%qT does not have the %E ABI tag "
1336 "that base %qT has", p->t, tag, p->subob))
1337 inform (location_of (p->subob), "%qT declared here",
1338 p->subob);
1340 else
1342 if (warning (OPT_Wabi_tag, "%qT does not have the %E ABI tag "
1343 "that %qT (used in the type of %qD) has",
1344 p->t, tag, *tp, p->subob))
1346 inform (location_of (p->subob), "%qD declared here",
1347 p->subob);
1348 inform (location_of (*tp), "%qT declared here", *tp);
1354 /* Find all the ABI tags in the attribute list ATTR and either call
1355 check_tag (if TP is non-null) or set IDENTIFIER_MARKED to val. */
1357 static void
1358 mark_or_check_attr_tags (tree attr, tree *tp, abi_tag_data *p, bool val)
1360 if (!attr)
1361 return;
1362 for (; (attr = lookup_attribute ("abi_tag", attr));
1363 attr = TREE_CHAIN (attr))
1364 for (tree list = TREE_VALUE (attr); list;
1365 list = TREE_CHAIN (list))
1367 tree tag = TREE_VALUE (list);
1368 tree id = get_identifier (TREE_STRING_POINTER (tag));
1369 if (tp)
1370 check_tag (tag, id, tp, p);
1371 else
1372 IDENTIFIER_MARKED (id) = val;
1376 /* Find all the ABI tags on T and its enclosing scopes and either call
1377 check_tag (if TP is non-null) or set IDENTIFIER_MARKED to val. */
1379 static void
1380 mark_or_check_tags (tree t, tree *tp, abi_tag_data *p, bool val)
1382 while (t != global_namespace)
1384 tree attr;
1385 if (TYPE_P (t))
1387 attr = TYPE_ATTRIBUTES (t);
1388 t = CP_TYPE_CONTEXT (t);
1390 else
1392 attr = DECL_ATTRIBUTES (t);
1393 t = CP_DECL_CONTEXT (t);
1395 mark_or_check_attr_tags (attr, tp, p, val);
1399 /* walk_tree callback for check_abi_tags: if the type at *TP involves any
1400 types with ABI tags, add the corresponding identifiers to the VEC in
1401 *DATA and set IDENTIFIER_MARKED. */
1403 static tree
1404 find_abi_tags_r (tree *tp, int *walk_subtrees, void *data)
1406 if (!OVERLOAD_TYPE_P (*tp))
1407 return NULL_TREE;
1409 /* walk_tree shouldn't be walking into any subtrees of a RECORD_TYPE
1410 anyway, but let's make sure of it. */
1411 *walk_subtrees = false;
1413 abi_tag_data *p = static_cast<struct abi_tag_data*>(data);
1415 mark_or_check_tags (*tp, tp, p, false);
1417 return NULL_TREE;
1420 /* walk_tree callback for mark_abi_tags: if *TP is a class, set
1421 IDENTIFIER_MARKED on its ABI tags. */
1423 static tree
1424 mark_abi_tags_r (tree *tp, int *walk_subtrees, void *data)
1426 if (!OVERLOAD_TYPE_P (*tp))
1427 return NULL_TREE;
1429 /* walk_tree shouldn't be walking into any subtrees of a RECORD_TYPE
1430 anyway, but let's make sure of it. */
1431 *walk_subtrees = false;
1433 bool *valp = static_cast<bool*>(data);
1435 mark_or_check_tags (*tp, NULL, NULL, *valp);
1437 return NULL_TREE;
1440 /* Set IDENTIFIER_MARKED on all the ABI tags on T and its enclosing
1441 scopes. */
1443 static void
1444 mark_abi_tags (tree t, bool val)
1446 mark_or_check_tags (t, NULL, NULL, val);
1447 if (DECL_P (t))
1449 if (DECL_LANG_SPECIFIC (t) && DECL_USE_TEMPLATE (t)
1450 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (t)))
1452 /* Template arguments are part of the signature. */
1453 tree level = INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (t));
1454 for (int j = 0; j < TREE_VEC_LENGTH (level); ++j)
1456 tree arg = TREE_VEC_ELT (level, j);
1457 cp_walk_tree_without_duplicates (&arg, mark_abi_tags_r, &val);
1460 if (TREE_CODE (t) == FUNCTION_DECL)
1461 /* A function's parameter types are part of the signature, so
1462 we don't need to inherit any tags that are also in them. */
1463 for (tree arg = FUNCTION_FIRST_USER_PARMTYPE (t); arg;
1464 arg = TREE_CHAIN (arg))
1465 cp_walk_tree_without_duplicates (&TREE_VALUE (arg),
1466 mark_abi_tags_r, &val);
1470 /* Check that T has all the ABI tags that subobject SUBOB has, or
1471 warn if not. If T is a (variable or function) declaration, also
1472 return any missing tags, and add them to T if JUST_CHECKING is false. */
1474 static tree
1475 check_abi_tags (tree t, tree subob, bool just_checking = false)
1477 bool inherit = DECL_P (t);
1479 if (!inherit && !warn_abi_tag)
1480 return NULL_TREE;
1482 tree decl = TYPE_P (t) ? TYPE_NAME (t) : t;
1483 if (!TREE_PUBLIC (decl))
1484 /* No need to worry about things local to this TU. */
1485 return NULL_TREE;
1487 mark_abi_tags (t, true);
1489 tree subtype = TYPE_P (subob) ? subob : TREE_TYPE (subob);
1490 struct abi_tag_data data = { t, subob, error_mark_node };
1491 if (inherit)
1492 data.tags = NULL_TREE;
1494 cp_walk_tree_without_duplicates (&subtype, find_abi_tags_r, &data);
1496 if (!(inherit && data.tags))
1497 /* We don't need to do anything with data.tags. */;
1498 else if (just_checking)
1499 for (tree t = data.tags; t; t = TREE_CHAIN (t))
1501 tree id = get_identifier (TREE_STRING_POINTER (TREE_VALUE (t)));
1502 IDENTIFIER_MARKED (id) = false;
1504 else
1506 tree attr = lookup_attribute ("abi_tag", DECL_ATTRIBUTES (t));
1507 if (attr)
1508 TREE_VALUE (attr) = chainon (data.tags, TREE_VALUE (attr));
1509 else
1510 DECL_ATTRIBUTES (t)
1511 = tree_cons (get_identifier ("abi_tag"), data.tags,
1512 DECL_ATTRIBUTES (t));
1515 mark_abi_tags (t, false);
1517 return data.tags;
1520 /* Check that DECL has all the ABI tags that are used in parts of its type
1521 that are not reflected in its mangled name. */
1523 void
1524 check_abi_tags (tree decl)
1526 if (VAR_P (decl))
1527 check_abi_tags (decl, TREE_TYPE (decl));
1528 else if (TREE_CODE (decl) == FUNCTION_DECL
1529 && !DECL_CONV_FN_P (decl)
1530 && !mangle_return_type_p (decl))
1531 check_abi_tags (decl, TREE_TYPE (TREE_TYPE (decl)));
1534 /* Return any ABI tags that are used in parts of the type of DECL
1535 that are not reflected in its mangled name. This function is only
1536 used in backward-compatible mangling for ABI <11. */
1538 tree
1539 missing_abi_tags (tree decl)
1541 if (VAR_P (decl))
1542 return check_abi_tags (decl, TREE_TYPE (decl), true);
1543 else if (TREE_CODE (decl) == FUNCTION_DECL
1544 /* Don't check DECL_CONV_FN_P here like we do in check_abi_tags, so
1545 that we can use this function for setting need_abi_warning
1546 regardless of the current flag_abi_version. */
1547 && !mangle_return_type_p (decl))
1548 return check_abi_tags (decl, TREE_TYPE (TREE_TYPE (decl)), true);
1549 else
1550 return NULL_TREE;
1553 void
1554 inherit_targ_abi_tags (tree t)
1556 if (!CLASS_TYPE_P (t)
1557 || CLASSTYPE_TEMPLATE_INFO (t) == NULL_TREE)
1558 return;
1560 mark_abi_tags (t, true);
1562 tree args = CLASSTYPE_TI_ARGS (t);
1563 struct abi_tag_data data = { t, NULL_TREE, NULL_TREE };
1564 for (int i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
1566 tree level = TMPL_ARGS_LEVEL (args, i+1);
1567 for (int j = 0; j < TREE_VEC_LENGTH (level); ++j)
1569 tree arg = TREE_VEC_ELT (level, j);
1570 data.subob = arg;
1571 cp_walk_tree_without_duplicates (&arg, find_abi_tags_r, &data);
1575 // If we found some tags on our template arguments, add them to our
1576 // abi_tag attribute.
1577 if (data.tags)
1579 tree attr = lookup_attribute ("abi_tag", TYPE_ATTRIBUTES (t));
1580 if (attr)
1581 TREE_VALUE (attr) = chainon (data.tags, TREE_VALUE (attr));
1582 else
1583 TYPE_ATTRIBUTES (t)
1584 = tree_cons (get_identifier ("abi_tag"), data.tags,
1585 TYPE_ATTRIBUTES (t));
1588 mark_abi_tags (t, false);
1591 /* Return true, iff class T has a non-virtual destructor that is
1592 accessible from outside the class heirarchy (i.e. is public, or
1593 there's a suitable friend. */
1595 static bool
1596 accessible_nvdtor_p (tree t)
1598 tree dtor = CLASSTYPE_DESTRUCTOR (t);
1600 /* An implicitly declared destructor is always public. And,
1601 if it were virtual, we would have created it by now. */
1602 if (!dtor)
1603 return true;
1605 if (DECL_VINDEX (dtor))
1606 return false; /* Virtual */
1608 if (!TREE_PRIVATE (dtor) && !TREE_PROTECTED (dtor))
1609 return true; /* Public */
1611 if (CLASSTYPE_FRIEND_CLASSES (t)
1612 || DECL_FRIENDLIST (TYPE_MAIN_DECL (t)))
1613 return true; /* Has friends */
1615 return false;
1618 /* Run through the base classes of T, updating CANT_HAVE_CONST_CTOR_P,
1619 and NO_CONST_ASN_REF_P. Also set flag bits in T based on
1620 properties of the bases. */
1622 static void
1623 check_bases (tree t,
1624 int* cant_have_const_ctor_p,
1625 int* no_const_asn_ref_p)
1627 int i;
1628 bool seen_non_virtual_nearly_empty_base_p = 0;
1629 int seen_tm_mask = 0;
1630 tree base_binfo;
1631 tree binfo;
1632 tree field = NULL_TREE;
1634 if (!CLASSTYPE_NON_STD_LAYOUT (t))
1635 for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
1636 if (TREE_CODE (field) == FIELD_DECL)
1637 break;
1639 for (binfo = TYPE_BINFO (t), i = 0;
1640 BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
1642 tree basetype = TREE_TYPE (base_binfo);
1644 gcc_assert (COMPLETE_TYPE_P (basetype));
1646 if (CLASSTYPE_FINAL (basetype))
1647 error ("cannot derive from %<final%> base %qT in derived type %qT",
1648 basetype, t);
1650 /* If any base class is non-literal, so is the derived class. */
1651 if (!CLASSTYPE_LITERAL_P (basetype))
1652 CLASSTYPE_LITERAL_P (t) = false;
1654 /* If the base class doesn't have copy constructors or
1655 assignment operators that take const references, then the
1656 derived class cannot have such a member automatically
1657 generated. */
1658 if (TYPE_HAS_COPY_CTOR (basetype)
1659 && ! TYPE_HAS_CONST_COPY_CTOR (basetype))
1660 *cant_have_const_ctor_p = 1;
1661 if (TYPE_HAS_COPY_ASSIGN (basetype)
1662 && !TYPE_HAS_CONST_COPY_ASSIGN (basetype))
1663 *no_const_asn_ref_p = 1;
1665 if (BINFO_VIRTUAL_P (base_binfo))
1666 /* A virtual base does not effect nearly emptiness. */
1668 else if (CLASSTYPE_NEARLY_EMPTY_P (basetype))
1670 if (seen_non_virtual_nearly_empty_base_p)
1671 /* And if there is more than one nearly empty base, then the
1672 derived class is not nearly empty either. */
1673 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
1674 else
1675 /* Remember we've seen one. */
1676 seen_non_virtual_nearly_empty_base_p = 1;
1678 else if (!is_empty_class (basetype))
1679 /* If the base class is not empty or nearly empty, then this
1680 class cannot be nearly empty. */
1681 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
1683 /* A lot of properties from the bases also apply to the derived
1684 class. */
1685 TYPE_NEEDS_CONSTRUCTING (t) |= TYPE_NEEDS_CONSTRUCTING (basetype);
1686 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t)
1687 |= TYPE_HAS_NONTRIVIAL_DESTRUCTOR (basetype);
1688 TYPE_HAS_COMPLEX_COPY_ASSIGN (t)
1689 |= (TYPE_HAS_COMPLEX_COPY_ASSIGN (basetype)
1690 || !TYPE_HAS_COPY_ASSIGN (basetype));
1691 TYPE_HAS_COMPLEX_COPY_CTOR (t) |= (TYPE_HAS_COMPLEX_COPY_CTOR (basetype)
1692 || !TYPE_HAS_COPY_CTOR (basetype));
1693 TYPE_HAS_COMPLEX_MOVE_ASSIGN (t)
1694 |= TYPE_HAS_COMPLEX_MOVE_ASSIGN (basetype);
1695 TYPE_HAS_COMPLEX_MOVE_CTOR (t) |= TYPE_HAS_COMPLEX_MOVE_CTOR (basetype);
1696 TYPE_POLYMORPHIC_P (t) |= TYPE_POLYMORPHIC_P (basetype);
1697 CLASSTYPE_CONTAINS_EMPTY_CLASS_P (t)
1698 |= CLASSTYPE_CONTAINS_EMPTY_CLASS_P (basetype);
1699 TYPE_HAS_COMPLEX_DFLT (t) |= (!TYPE_HAS_DEFAULT_CONSTRUCTOR (basetype)
1700 || TYPE_HAS_COMPLEX_DFLT (basetype));
1701 SET_CLASSTYPE_READONLY_FIELDS_NEED_INIT
1702 (t, CLASSTYPE_READONLY_FIELDS_NEED_INIT (t)
1703 | CLASSTYPE_READONLY_FIELDS_NEED_INIT (basetype));
1704 SET_CLASSTYPE_REF_FIELDS_NEED_INIT
1705 (t, CLASSTYPE_REF_FIELDS_NEED_INIT (t)
1706 | CLASSTYPE_REF_FIELDS_NEED_INIT (basetype));
1707 if (TYPE_HAS_MUTABLE_P (basetype))
1708 CLASSTYPE_HAS_MUTABLE (t) = 1;
1710 /* A standard-layout class is a class that:
1712 * has no non-standard-layout base classes, */
1713 CLASSTYPE_NON_STD_LAYOUT (t) |= CLASSTYPE_NON_STD_LAYOUT (basetype);
1714 if (!CLASSTYPE_NON_STD_LAYOUT (t))
1716 tree basefield;
1717 /* ...has no base classes of the same type as the first non-static
1718 data member... */
1719 if (field && DECL_CONTEXT (field) == t
1720 && (same_type_ignoring_top_level_qualifiers_p
1721 (TREE_TYPE (field), basetype)))
1722 CLASSTYPE_NON_STD_LAYOUT (t) = 1;
1723 else
1724 /* ...either has no non-static data members in the most-derived
1725 class and at most one base class with non-static data
1726 members, or has no base classes with non-static data
1727 members */
1728 for (basefield = TYPE_FIELDS (basetype); basefield;
1729 basefield = DECL_CHAIN (basefield))
1730 if (TREE_CODE (basefield) == FIELD_DECL
1731 && !(DECL_FIELD_IS_BASE (basefield)
1732 && integer_zerop (DECL_SIZE (basefield))))
1734 if (field)
1735 CLASSTYPE_NON_STD_LAYOUT (t) = 1;
1736 else
1737 field = basefield;
1738 break;
1742 /* Don't bother collecting tm attributes if transactional memory
1743 support is not enabled. */
1744 if (flag_tm)
1746 tree tm_attr = find_tm_attribute (TYPE_ATTRIBUTES (basetype));
1747 if (tm_attr)
1748 seen_tm_mask |= tm_attr_to_mask (tm_attr);
1751 check_abi_tags (t, basetype);
1754 /* If one of the base classes had TM attributes, and the current class
1755 doesn't define its own, then the current class inherits one. */
1756 if (seen_tm_mask && !find_tm_attribute (TYPE_ATTRIBUTES (t)))
1758 tree tm_attr = tm_mask_to_attr (least_bit_hwi (seen_tm_mask));
1759 TYPE_ATTRIBUTES (t) = tree_cons (tm_attr, NULL, TYPE_ATTRIBUTES (t));
1763 /* Determine all the primary bases within T. Sets BINFO_PRIMARY_BASE_P for
1764 those that are primaries. Sets BINFO_LOST_PRIMARY_P for those
1765 that have had a nearly-empty virtual primary base stolen by some
1766 other base in the hierarchy. Determines CLASSTYPE_PRIMARY_BASE for
1767 T. */
1769 static void
1770 determine_primary_bases (tree t)
1772 unsigned i;
1773 tree primary = NULL_TREE;
1774 tree type_binfo = TYPE_BINFO (t);
1775 tree base_binfo;
1777 /* Determine the primary bases of our bases. */
1778 for (base_binfo = TREE_CHAIN (type_binfo); base_binfo;
1779 base_binfo = TREE_CHAIN (base_binfo))
1781 tree primary = CLASSTYPE_PRIMARY_BINFO (BINFO_TYPE (base_binfo));
1783 /* See if we're the non-virtual primary of our inheritance
1784 chain. */
1785 if (!BINFO_VIRTUAL_P (base_binfo))
1787 tree parent = BINFO_INHERITANCE_CHAIN (base_binfo);
1788 tree parent_primary = CLASSTYPE_PRIMARY_BINFO (BINFO_TYPE (parent));
1790 if (parent_primary
1791 && SAME_BINFO_TYPE_P (BINFO_TYPE (base_binfo),
1792 BINFO_TYPE (parent_primary)))
1793 /* We are the primary binfo. */
1794 BINFO_PRIMARY_P (base_binfo) = 1;
1796 /* Determine if we have a virtual primary base, and mark it so.
1798 if (primary && BINFO_VIRTUAL_P (primary))
1800 tree this_primary = copied_binfo (primary, base_binfo);
1802 if (BINFO_PRIMARY_P (this_primary))
1803 /* Someone already claimed this base. */
1804 BINFO_LOST_PRIMARY_P (base_binfo) = 1;
1805 else
1807 tree delta;
1809 BINFO_PRIMARY_P (this_primary) = 1;
1810 BINFO_INHERITANCE_CHAIN (this_primary) = base_binfo;
1812 /* A virtual binfo might have been copied from within
1813 another hierarchy. As we're about to use it as a
1814 primary base, make sure the offsets match. */
1815 delta = size_diffop_loc (input_location,
1816 fold_convert (ssizetype,
1817 BINFO_OFFSET (base_binfo)),
1818 fold_convert (ssizetype,
1819 BINFO_OFFSET (this_primary)));
1821 propagate_binfo_offsets (this_primary, delta);
1826 /* First look for a dynamic direct non-virtual base. */
1827 for (i = 0; BINFO_BASE_ITERATE (type_binfo, i, base_binfo); i++)
1829 tree basetype = BINFO_TYPE (base_binfo);
1831 if (TYPE_CONTAINS_VPTR_P (basetype) && !BINFO_VIRTUAL_P (base_binfo))
1833 primary = base_binfo;
1834 goto found;
1838 /* A "nearly-empty" virtual base class can be the primary base
1839 class, if no non-virtual polymorphic base can be found. Look for
1840 a nearly-empty virtual dynamic base that is not already a primary
1841 base of something in the hierarchy. If there is no such base,
1842 just pick the first nearly-empty virtual base. */
1844 for (base_binfo = TREE_CHAIN (type_binfo); base_binfo;
1845 base_binfo = TREE_CHAIN (base_binfo))
1846 if (BINFO_VIRTUAL_P (base_binfo)
1847 && CLASSTYPE_NEARLY_EMPTY_P (BINFO_TYPE (base_binfo)))
1849 if (!BINFO_PRIMARY_P (base_binfo))
1851 /* Found one that is not primary. */
1852 primary = base_binfo;
1853 goto found;
1855 else if (!primary)
1856 /* Remember the first candidate. */
1857 primary = base_binfo;
1860 found:
1861 /* If we've got a primary base, use it. */
1862 if (primary)
1864 tree basetype = BINFO_TYPE (primary);
1866 CLASSTYPE_PRIMARY_BINFO (t) = primary;
1867 if (BINFO_PRIMARY_P (primary))
1868 /* We are stealing a primary base. */
1869 BINFO_LOST_PRIMARY_P (BINFO_INHERITANCE_CHAIN (primary)) = 1;
1870 BINFO_PRIMARY_P (primary) = 1;
1871 if (BINFO_VIRTUAL_P (primary))
1873 tree delta;
1875 BINFO_INHERITANCE_CHAIN (primary) = type_binfo;
1876 /* A virtual binfo might have been copied from within
1877 another hierarchy. As we're about to use it as a primary
1878 base, make sure the offsets match. */
1879 delta = size_diffop_loc (input_location, ssize_int (0),
1880 fold_convert (ssizetype, BINFO_OFFSET (primary)));
1882 propagate_binfo_offsets (primary, delta);
1885 primary = TYPE_BINFO (basetype);
1887 TYPE_VFIELD (t) = TYPE_VFIELD (basetype);
1888 BINFO_VTABLE (type_binfo) = BINFO_VTABLE (primary);
1889 BINFO_VIRTUALS (type_binfo) = BINFO_VIRTUALS (primary);
1893 /* Update the variant types of T. */
1895 void
1896 fixup_type_variants (tree t)
1898 tree variants;
1900 if (!t)
1901 return;
1903 for (variants = TYPE_NEXT_VARIANT (t);
1904 variants;
1905 variants = TYPE_NEXT_VARIANT (variants))
1907 /* These fields are in the _TYPE part of the node, not in
1908 the TYPE_LANG_SPECIFIC component, so they are not shared. */
1909 TYPE_HAS_USER_CONSTRUCTOR (variants) = TYPE_HAS_USER_CONSTRUCTOR (t);
1910 TYPE_NEEDS_CONSTRUCTING (variants) = TYPE_NEEDS_CONSTRUCTING (t);
1911 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (variants)
1912 = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t);
1914 TYPE_POLYMORPHIC_P (variants) = TYPE_POLYMORPHIC_P (t);
1916 TYPE_BINFO (variants) = TYPE_BINFO (t);
1918 /* Copy whatever these are holding today. */
1919 TYPE_VFIELD (variants) = TYPE_VFIELD (t);
1920 TYPE_FIELDS (variants) = TYPE_FIELDS (t);
1924 /* KLASS is a class that we're applying may_alias to after the body is
1925 parsed. Fixup any POINTER_TO and REFERENCE_TO types. The
1926 canonical type(s) will be implicitly updated. */
1928 static void
1929 fixup_may_alias (tree klass)
1931 tree t, v;
1933 for (t = TYPE_POINTER_TO (klass); t; t = TYPE_NEXT_PTR_TO (t))
1934 for (v = TYPE_MAIN_VARIANT (t); v; v = TYPE_NEXT_VARIANT (v))
1935 TYPE_REF_CAN_ALIAS_ALL (v) = true;
1936 for (t = TYPE_REFERENCE_TO (klass); t; t = TYPE_NEXT_REF_TO (t))
1937 for (v = TYPE_MAIN_VARIANT (t); v; v = TYPE_NEXT_VARIANT (v))
1938 TYPE_REF_CAN_ALIAS_ALL (v) = true;
1941 /* Early variant fixups: we apply attributes at the beginning of the class
1942 definition, and we need to fix up any variants that have already been
1943 made via elaborated-type-specifier so that check_qualified_type works. */
1945 void
1946 fixup_attribute_variants (tree t)
1948 tree variants;
1950 if (!t)
1951 return;
1953 tree attrs = TYPE_ATTRIBUTES (t);
1954 unsigned align = TYPE_ALIGN (t);
1955 bool user_align = TYPE_USER_ALIGN (t);
1956 bool may_alias = lookup_attribute ("may_alias", attrs);
1958 if (may_alias)
1959 fixup_may_alias (t);
1961 for (variants = TYPE_NEXT_VARIANT (t);
1962 variants;
1963 variants = TYPE_NEXT_VARIANT (variants))
1965 /* These are the two fields that check_qualified_type looks at and
1966 are affected by attributes. */
1967 TYPE_ATTRIBUTES (variants) = attrs;
1968 unsigned valign = align;
1969 if (TYPE_USER_ALIGN (variants))
1970 valign = MAX (valign, TYPE_ALIGN (variants));
1971 else
1972 TYPE_USER_ALIGN (variants) = user_align;
1973 SET_TYPE_ALIGN (variants, valign);
1974 if (may_alias)
1975 fixup_may_alias (variants);
1979 /* Set memoizing fields and bits of T (and its variants) for later
1980 use. */
1982 static void
1983 finish_struct_bits (tree t)
1985 /* Fix up variants (if any). */
1986 fixup_type_variants (t);
1988 if (BINFO_N_BASE_BINFOS (TYPE_BINFO (t)) && TYPE_POLYMORPHIC_P (t))
1989 /* For a class w/o baseclasses, 'finish_struct' has set
1990 CLASSTYPE_PURE_VIRTUALS correctly (by definition).
1991 Similarly for a class whose base classes do not have vtables.
1992 When neither of these is true, we might have removed abstract
1993 virtuals (by providing a definition), added some (by declaring
1994 new ones), or redeclared ones from a base class. We need to
1995 recalculate what's really an abstract virtual at this point (by
1996 looking in the vtables). */
1997 get_pure_virtuals (t);
1999 /* If this type has a copy constructor or a destructor, force its
2000 mode to be BLKmode, and force its TREE_ADDRESSABLE bit to be
2001 nonzero. This will cause it to be passed by invisible reference
2002 and prevent it from being returned in a register. */
2003 if (type_has_nontrivial_copy_init (t)
2004 || TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t))
2006 tree variants;
2007 SET_DECL_MODE (TYPE_MAIN_DECL (t), BLKmode);
2008 for (variants = t; variants; variants = TYPE_NEXT_VARIANT (variants))
2010 SET_TYPE_MODE (variants, BLKmode);
2011 TREE_ADDRESSABLE (variants) = 1;
2016 /* Issue warnings about T having private constructors, but no friends,
2017 and so forth.
2019 HAS_NONPRIVATE_METHOD is nonzero if T has any non-private methods or
2020 static members. HAS_NONPRIVATE_STATIC_FN is nonzero if T has any
2021 non-private static member functions. */
2023 static void
2024 maybe_warn_about_overly_private_class (tree t)
2026 int has_member_fn = 0;
2027 int has_nonprivate_method = 0;
2029 if (!warn_ctor_dtor_privacy
2030 /* If the class has friends, those entities might create and
2031 access instances, so we should not warn. */
2032 || (CLASSTYPE_FRIEND_CLASSES (t)
2033 || DECL_FRIENDLIST (TYPE_MAIN_DECL (t)))
2034 /* We will have warned when the template was declared; there's
2035 no need to warn on every instantiation. */
2036 || CLASSTYPE_TEMPLATE_INSTANTIATION (t))
2037 /* There's no reason to even consider warning about this
2038 class. */
2039 return;
2041 /* We only issue one warning, if more than one applies, because
2042 otherwise, on code like:
2044 class A {
2045 // Oops - forgot `public:'
2046 A();
2047 A(const A&);
2048 ~A();
2051 we warn several times about essentially the same problem. */
2053 /* Check to see if all (non-constructor, non-destructor) member
2054 functions are private. (Since there are no friends or
2055 non-private statics, we can't ever call any of the private member
2056 functions.) */
2057 for (tree fn = TYPE_FIELDS (t); fn; fn = DECL_CHAIN (fn))
2058 if (!DECL_DECLARES_FUNCTION_P (fn))
2059 /* Not a function. */;
2060 else if (DECL_ARTIFICIAL (fn))
2061 /* We're not interested in compiler-generated methods; they don't
2062 provide any way to call private members. */;
2063 else if (!TREE_PRIVATE (fn))
2065 if (DECL_STATIC_FUNCTION_P (fn))
2066 /* A non-private static member function is just like a
2067 friend; it can create and invoke private member
2068 functions, and be accessed without a class
2069 instance. */
2070 return;
2072 has_nonprivate_method = 1;
2073 /* Keep searching for a static member function. */
2075 else if (!DECL_CONSTRUCTOR_P (fn) && !DECL_DESTRUCTOR_P (fn))
2076 has_member_fn = 1;
2078 if (!has_nonprivate_method && has_member_fn)
2080 /* There are no non-private methods, and there's at least one
2081 private member function that isn't a constructor or
2082 destructor. (If all the private members are
2083 constructors/destructors we want to use the code below that
2084 issues error messages specifically referring to
2085 constructors/destructors.) */
2086 unsigned i;
2087 tree binfo = TYPE_BINFO (t);
2089 for (i = 0; i != BINFO_N_BASE_BINFOS (binfo); i++)
2090 if (BINFO_BASE_ACCESS (binfo, i) != access_private_node)
2092 has_nonprivate_method = 1;
2093 break;
2095 if (!has_nonprivate_method)
2097 warning (OPT_Wctor_dtor_privacy,
2098 "all member functions in class %qT are private", t);
2099 return;
2103 /* Even if some of the member functions are non-private, the class
2104 won't be useful for much if all the constructors or destructors
2105 are private: such an object can never be created or destroyed. */
2106 if (tree dtor = CLASSTYPE_DESTRUCTOR (t))
2107 if (TREE_PRIVATE (dtor))
2109 warning (OPT_Wctor_dtor_privacy,
2110 "%q#T only defines a private destructor and has no friends",
2112 return;
2115 /* Warn about classes that have private constructors and no friends. */
2116 if (TYPE_HAS_USER_CONSTRUCTOR (t)
2117 /* Implicitly generated constructors are always public. */
2118 && !CLASSTYPE_LAZY_DEFAULT_CTOR (t))
2120 bool nonprivate_ctor = false;
2121 tree copy_or_move = NULL_TREE;
2123 /* If a non-template class does not define a copy
2124 constructor, one is defined for it, enabling it to avoid
2125 this warning. For a template class, this does not
2126 happen, and so we would normally get a warning on:
2128 template <class T> class C { private: C(); };
2130 To avoid this asymmetry, we check TYPE_HAS_COPY_CTOR. All
2131 complete non-template or fully instantiated classes have this
2132 flag set. */
2133 if (!TYPE_HAS_COPY_CTOR (t))
2134 nonprivate_ctor = true;
2135 else
2136 for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (t));
2137 !nonprivate_ctor && iter; ++iter)
2138 if (TREE_PRIVATE (*iter))
2139 continue;
2140 else if (copy_fn_p (*iter) || move_fn_p (*iter))
2141 /* Ideally, we wouldn't count any constructor that takes
2142 an argument of the class type as a parameter, because
2143 such things cannot be used to construct an instance of
2144 the class unless you already have one. */
2145 copy_or_move = *iter;
2146 else
2147 nonprivate_ctor = true;
2149 if (!nonprivate_ctor)
2151 warning (OPT_Wctor_dtor_privacy,
2152 "%q#T only defines private constructors and has no friends",
2154 if (copy_or_move)
2155 inform (DECL_SOURCE_LOCATION (copy_or_move),
2156 "%q#D is public, but requires an existing %q#T object",
2157 copy_or_move, t);
2158 return;
2163 /* Make BINFO's vtable have N entries, including RTTI entries,
2164 vbase and vcall offsets, etc. Set its type and call the back end
2165 to lay it out. */
2167 static void
2168 layout_vtable_decl (tree binfo, int n)
2170 tree atype;
2171 tree vtable;
2173 atype = build_array_of_n_type (vtable_entry_type, n);
2174 layout_type (atype);
2176 /* We may have to grow the vtable. */
2177 vtable = get_vtbl_decl_for_binfo (binfo);
2178 if (!same_type_p (TREE_TYPE (vtable), atype))
2180 TREE_TYPE (vtable) = atype;
2181 DECL_SIZE (vtable) = DECL_SIZE_UNIT (vtable) = NULL_TREE;
2182 layout_decl (vtable, 0);
2186 /* True iff FNDECL and BASE_FNDECL (both non-static member functions)
2187 have the same signature. */
2190 same_signature_p (const_tree fndecl, const_tree base_fndecl)
2192 /* One destructor overrides another if they are the same kind of
2193 destructor. */
2194 if (DECL_DESTRUCTOR_P (base_fndecl) && DECL_DESTRUCTOR_P (fndecl)
2195 && special_function_p (base_fndecl) == special_function_p (fndecl))
2196 return 1;
2197 /* But a non-destructor never overrides a destructor, nor vice
2198 versa, nor do different kinds of destructors override
2199 one-another. For example, a complete object destructor does not
2200 override a deleting destructor. */
2201 if (DECL_DESTRUCTOR_P (base_fndecl) || DECL_DESTRUCTOR_P (fndecl))
2202 return 0;
2204 if (DECL_NAME (fndecl) == DECL_NAME (base_fndecl)
2205 || (DECL_CONV_FN_P (fndecl)
2206 && DECL_CONV_FN_P (base_fndecl)
2207 && same_type_p (DECL_CONV_FN_TYPE (fndecl),
2208 DECL_CONV_FN_TYPE (base_fndecl))))
2210 tree fntype = TREE_TYPE (fndecl);
2211 tree base_fntype = TREE_TYPE (base_fndecl);
2212 if (type_memfn_quals (fntype) == type_memfn_quals (base_fntype)
2213 && type_memfn_rqual (fntype) == type_memfn_rqual (base_fntype)
2214 && compparms (FUNCTION_FIRST_USER_PARMTYPE (fndecl),
2215 FUNCTION_FIRST_USER_PARMTYPE (base_fndecl)))
2216 return 1;
2218 return 0;
2221 /* Returns TRUE if DERIVED is a binfo containing the binfo BASE as a
2222 subobject. */
2224 static bool
2225 base_derived_from (tree derived, tree base)
2227 tree probe;
2229 for (probe = base; probe; probe = BINFO_INHERITANCE_CHAIN (probe))
2231 if (probe == derived)
2232 return true;
2233 else if (BINFO_VIRTUAL_P (probe))
2234 /* If we meet a virtual base, we can't follow the inheritance
2235 any more. See if the complete type of DERIVED contains
2236 such a virtual base. */
2237 return (binfo_for_vbase (BINFO_TYPE (probe), BINFO_TYPE (derived))
2238 != NULL_TREE);
2240 return false;
2243 struct find_final_overrider_data {
2244 /* The function for which we are trying to find a final overrider. */
2245 tree fn;
2246 /* The base class in which the function was declared. */
2247 tree declaring_base;
2248 /* The candidate overriders. */
2249 tree candidates;
2250 /* Path to most derived. */
2251 vec<tree> path;
2254 /* Add the overrider along the current path to FFOD->CANDIDATES.
2255 Returns true if an overrider was found; false otherwise. */
2257 static bool
2258 dfs_find_final_overrider_1 (tree binfo,
2259 find_final_overrider_data *ffod,
2260 unsigned depth)
2262 tree method;
2264 /* If BINFO is not the most derived type, try a more derived class.
2265 A definition there will overrider a definition here. */
2266 if (depth)
2268 depth--;
2269 if (dfs_find_final_overrider_1
2270 (ffod->path[depth], ffod, depth))
2271 return true;
2274 method = look_for_overrides_here (BINFO_TYPE (binfo), ffod->fn);
2275 if (method)
2277 tree *candidate = &ffod->candidates;
2279 /* Remove any candidates overridden by this new function. */
2280 while (*candidate)
2282 /* If *CANDIDATE overrides METHOD, then METHOD
2283 cannot override anything else on the list. */
2284 if (base_derived_from (TREE_VALUE (*candidate), binfo))
2285 return true;
2286 /* If METHOD overrides *CANDIDATE, remove *CANDIDATE. */
2287 if (base_derived_from (binfo, TREE_VALUE (*candidate)))
2288 *candidate = TREE_CHAIN (*candidate);
2289 else
2290 candidate = &TREE_CHAIN (*candidate);
2293 /* Add the new function. */
2294 ffod->candidates = tree_cons (method, binfo, ffod->candidates);
2295 return true;
2298 return false;
2301 /* Called from find_final_overrider via dfs_walk. */
2303 static tree
2304 dfs_find_final_overrider_pre (tree binfo, void *data)
2306 find_final_overrider_data *ffod = (find_final_overrider_data *) data;
2308 if (binfo == ffod->declaring_base)
2309 dfs_find_final_overrider_1 (binfo, ffod, ffod->path.length ());
2310 ffod->path.safe_push (binfo);
2312 return NULL_TREE;
2315 static tree
2316 dfs_find_final_overrider_post (tree /*binfo*/, void *data)
2318 find_final_overrider_data *ffod = (find_final_overrider_data *) data;
2319 ffod->path.pop ();
2321 return NULL_TREE;
2324 /* Returns a TREE_LIST whose TREE_PURPOSE is the final overrider for
2325 FN and whose TREE_VALUE is the binfo for the base where the
2326 overriding occurs. BINFO (in the hierarchy dominated by the binfo
2327 DERIVED) is the base object in which FN is declared. */
2329 static tree
2330 find_final_overrider (tree derived, tree binfo, tree fn)
2332 find_final_overrider_data ffod;
2334 /* Getting this right is a little tricky. This is valid:
2336 struct S { virtual void f (); };
2337 struct T { virtual void f (); };
2338 struct U : public S, public T { };
2340 even though calling `f' in `U' is ambiguous. But,
2342 struct R { virtual void f(); };
2343 struct S : virtual public R { virtual void f (); };
2344 struct T : virtual public R { virtual void f (); };
2345 struct U : public S, public T { };
2347 is not -- there's no way to decide whether to put `S::f' or
2348 `T::f' in the vtable for `R'.
2350 The solution is to look at all paths to BINFO. If we find
2351 different overriders along any two, then there is a problem. */
2352 if (DECL_THUNK_P (fn))
2353 fn = THUNK_TARGET (fn);
2355 /* Determine the depth of the hierarchy. */
2356 ffod.fn = fn;
2357 ffod.declaring_base = binfo;
2358 ffod.candidates = NULL_TREE;
2359 ffod.path.create (30);
2361 dfs_walk_all (derived, dfs_find_final_overrider_pre,
2362 dfs_find_final_overrider_post, &ffod);
2364 ffod.path.release ();
2366 /* If there was no winner, issue an error message. */
2367 if (!ffod.candidates || TREE_CHAIN (ffod.candidates))
2368 return error_mark_node;
2370 return ffod.candidates;
2373 /* Return the index of the vcall offset for FN when TYPE is used as a
2374 virtual base. */
2376 static tree
2377 get_vcall_index (tree fn, tree type)
2379 vec<tree_pair_s, va_gc> *indices = CLASSTYPE_VCALL_INDICES (type);
2380 tree_pair_p p;
2381 unsigned ix;
2383 FOR_EACH_VEC_SAFE_ELT (indices, ix, p)
2384 if ((DECL_DESTRUCTOR_P (fn) && DECL_DESTRUCTOR_P (p->purpose))
2385 || same_signature_p (fn, p->purpose))
2386 return p->value;
2388 /* There should always be an appropriate index. */
2389 gcc_unreachable ();
2392 /* Update an entry in the vtable for BINFO, which is in the hierarchy
2393 dominated by T. FN is the old function; VIRTUALS points to the
2394 corresponding position in the new BINFO_VIRTUALS list. IX is the index
2395 of that entry in the list. */
2397 static void
2398 update_vtable_entry_for_fn (tree t, tree binfo, tree fn, tree* virtuals,
2399 unsigned ix)
2401 tree b;
2402 tree overrider;
2403 tree delta;
2404 tree virtual_base;
2405 tree first_defn;
2406 tree overrider_fn, overrider_target;
2407 tree target_fn = DECL_THUNK_P (fn) ? THUNK_TARGET (fn) : fn;
2408 tree over_return, base_return;
2409 bool lost = false;
2411 /* Find the nearest primary base (possibly binfo itself) which defines
2412 this function; this is the class the caller will convert to when
2413 calling FN through BINFO. */
2414 for (b = binfo; ; b = get_primary_binfo (b))
2416 gcc_assert (b);
2417 if (look_for_overrides_here (BINFO_TYPE (b), target_fn))
2418 break;
2420 /* The nearest definition is from a lost primary. */
2421 if (BINFO_LOST_PRIMARY_P (b))
2422 lost = true;
2424 first_defn = b;
2426 /* Find the final overrider. */
2427 overrider = find_final_overrider (TYPE_BINFO (t), b, target_fn);
2428 if (overrider == error_mark_node)
2430 error ("no unique final overrider for %qD in %qT", target_fn, t);
2431 return;
2433 overrider_target = overrider_fn = TREE_PURPOSE (overrider);
2435 /* Check for adjusting covariant return types. */
2436 over_return = TREE_TYPE (TREE_TYPE (overrider_target));
2437 base_return = TREE_TYPE (TREE_TYPE (target_fn));
2439 if (POINTER_TYPE_P (over_return)
2440 && TREE_CODE (over_return) == TREE_CODE (base_return)
2441 && CLASS_TYPE_P (TREE_TYPE (over_return))
2442 && CLASS_TYPE_P (TREE_TYPE (base_return))
2443 /* If the overrider is invalid, don't even try. */
2444 && !DECL_INVALID_OVERRIDER_P (overrider_target))
2446 /* If FN is a covariant thunk, we must figure out the adjustment
2447 to the final base FN was converting to. As OVERRIDER_TARGET might
2448 also be converting to the return type of FN, we have to
2449 combine the two conversions here. */
2450 tree fixed_offset, virtual_offset;
2452 over_return = TREE_TYPE (over_return);
2453 base_return = TREE_TYPE (base_return);
2455 if (DECL_THUNK_P (fn))
2457 gcc_assert (DECL_RESULT_THUNK_P (fn));
2458 fixed_offset = ssize_int (THUNK_FIXED_OFFSET (fn));
2459 virtual_offset = THUNK_VIRTUAL_OFFSET (fn);
2461 else
2462 fixed_offset = virtual_offset = NULL_TREE;
2464 if (virtual_offset)
2465 /* Find the equivalent binfo within the return type of the
2466 overriding function. We will want the vbase offset from
2467 there. */
2468 virtual_offset = binfo_for_vbase (BINFO_TYPE (virtual_offset),
2469 over_return);
2470 else if (!same_type_ignoring_top_level_qualifiers_p
2471 (over_return, base_return))
2473 /* There was no existing virtual thunk (which takes
2474 precedence). So find the binfo of the base function's
2475 return type within the overriding function's return type.
2476 Fortunately we know the covariancy is valid (it
2477 has already been checked), so we can just iterate along
2478 the binfos, which have been chained in inheritance graph
2479 order. Of course it is lame that we have to repeat the
2480 search here anyway -- we should really be caching pieces
2481 of the vtable and avoiding this repeated work. */
2482 tree thunk_binfo, base_binfo;
2484 /* Find the base binfo within the overriding function's
2485 return type. We will always find a thunk_binfo, except
2486 when the covariancy is invalid (which we will have
2487 already diagnosed). */
2488 for (base_binfo = TYPE_BINFO (base_return),
2489 thunk_binfo = TYPE_BINFO (over_return);
2490 thunk_binfo;
2491 thunk_binfo = TREE_CHAIN (thunk_binfo))
2492 if (SAME_BINFO_TYPE_P (BINFO_TYPE (thunk_binfo),
2493 BINFO_TYPE (base_binfo)))
2494 break;
2496 /* See if virtual inheritance is involved. */
2497 for (virtual_offset = thunk_binfo;
2498 virtual_offset;
2499 virtual_offset = BINFO_INHERITANCE_CHAIN (virtual_offset))
2500 if (BINFO_VIRTUAL_P (virtual_offset))
2501 break;
2503 if (virtual_offset
2504 || (thunk_binfo && !BINFO_OFFSET_ZEROP (thunk_binfo)))
2506 tree offset = fold_convert (ssizetype, BINFO_OFFSET (thunk_binfo));
2508 if (virtual_offset)
2510 /* We convert via virtual base. Adjust the fixed
2511 offset to be from there. */
2512 offset =
2513 size_diffop (offset,
2514 fold_convert (ssizetype,
2515 BINFO_OFFSET (virtual_offset)));
2517 if (fixed_offset)
2518 /* There was an existing fixed offset, this must be
2519 from the base just converted to, and the base the
2520 FN was thunking to. */
2521 fixed_offset = size_binop (PLUS_EXPR, fixed_offset, offset);
2522 else
2523 fixed_offset = offset;
2527 if (fixed_offset || virtual_offset)
2528 /* Replace the overriding function with a covariant thunk. We
2529 will emit the overriding function in its own slot as
2530 well. */
2531 overrider_fn = make_thunk (overrider_target, /*this_adjusting=*/0,
2532 fixed_offset, virtual_offset);
2534 else
2535 gcc_assert (DECL_INVALID_OVERRIDER_P (overrider_target) ||
2536 !DECL_THUNK_P (fn));
2538 /* If we need a covariant thunk, then we may need to adjust first_defn.
2539 The ABI specifies that the thunks emitted with a function are
2540 determined by which bases the function overrides, so we need to be
2541 sure that we're using a thunk for some overridden base; even if we
2542 know that the necessary this adjustment is zero, there may not be an
2543 appropriate zero-this-adjustment thunk for us to use since thunks for
2544 overriding virtual bases always use the vcall offset.
2546 Furthermore, just choosing any base that overrides this function isn't
2547 quite right, as this slot won't be used for calls through a type that
2548 puts a covariant thunk here. Calling the function through such a type
2549 will use a different slot, and that slot is the one that determines
2550 the thunk emitted for that base.
2552 So, keep looking until we find the base that we're really overriding
2553 in this slot: the nearest primary base that doesn't use a covariant
2554 thunk in this slot. */
2555 if (overrider_target != overrider_fn)
2557 if (BINFO_TYPE (b) == DECL_CONTEXT (overrider_target))
2558 /* We already know that the overrider needs a covariant thunk. */
2559 b = get_primary_binfo (b);
2560 for (; ; b = get_primary_binfo (b))
2562 tree main_binfo = TYPE_BINFO (BINFO_TYPE (b));
2563 tree bv = chain_index (ix, BINFO_VIRTUALS (main_binfo));
2564 if (!DECL_THUNK_P (TREE_VALUE (bv)))
2565 break;
2566 if (BINFO_LOST_PRIMARY_P (b))
2567 lost = true;
2569 first_defn = b;
2572 /* Assume that we will produce a thunk that convert all the way to
2573 the final overrider, and not to an intermediate virtual base. */
2574 virtual_base = NULL_TREE;
2576 /* See if we can convert to an intermediate virtual base first, and then
2577 use the vcall offset located there to finish the conversion. */
2578 for (; b; b = BINFO_INHERITANCE_CHAIN (b))
2580 /* If we find the final overrider, then we can stop
2581 walking. */
2582 if (SAME_BINFO_TYPE_P (BINFO_TYPE (b),
2583 BINFO_TYPE (TREE_VALUE (overrider))))
2584 break;
2586 /* If we find a virtual base, and we haven't yet found the
2587 overrider, then there is a virtual base between the
2588 declaring base (first_defn) and the final overrider. */
2589 if (BINFO_VIRTUAL_P (b))
2591 virtual_base = b;
2592 break;
2596 /* Compute the constant adjustment to the `this' pointer. The
2597 `this' pointer, when this function is called, will point at BINFO
2598 (or one of its primary bases, which are at the same offset). */
2599 if (virtual_base)
2600 /* The `this' pointer needs to be adjusted from the declaration to
2601 the nearest virtual base. */
2602 delta = size_diffop_loc (input_location,
2603 fold_convert (ssizetype, BINFO_OFFSET (virtual_base)),
2604 fold_convert (ssizetype, BINFO_OFFSET (first_defn)));
2605 else if (lost)
2606 /* If the nearest definition is in a lost primary, we don't need an
2607 entry in our vtable. Except possibly in a constructor vtable,
2608 if we happen to get our primary back. In that case, the offset
2609 will be zero, as it will be a primary base. */
2610 delta = size_zero_node;
2611 else
2612 /* The `this' pointer needs to be adjusted from pointing to
2613 BINFO to pointing at the base where the final overrider
2614 appears. */
2615 delta = size_diffop_loc (input_location,
2616 fold_convert (ssizetype,
2617 BINFO_OFFSET (TREE_VALUE (overrider))),
2618 fold_convert (ssizetype, BINFO_OFFSET (binfo)));
2620 modify_vtable_entry (t, binfo, overrider_fn, delta, virtuals);
2622 if (virtual_base)
2623 BV_VCALL_INDEX (*virtuals)
2624 = get_vcall_index (overrider_target, BINFO_TYPE (virtual_base));
2625 else
2626 BV_VCALL_INDEX (*virtuals) = NULL_TREE;
2628 BV_LOST_PRIMARY (*virtuals) = lost;
2631 /* Called from modify_all_vtables via dfs_walk. */
2633 static tree
2634 dfs_modify_vtables (tree binfo, void* data)
2636 tree t = (tree) data;
2637 tree virtuals;
2638 tree old_virtuals;
2639 unsigned ix;
2641 if (!TYPE_CONTAINS_VPTR_P (BINFO_TYPE (binfo)))
2642 /* A base without a vtable needs no modification, and its bases
2643 are uninteresting. */
2644 return dfs_skip_bases;
2646 if (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), t)
2647 && !CLASSTYPE_HAS_PRIMARY_BASE_P (t))
2648 /* Don't do the primary vtable, if it's new. */
2649 return NULL_TREE;
2651 if (BINFO_PRIMARY_P (binfo) && !BINFO_VIRTUAL_P (binfo))
2652 /* There's no need to modify the vtable for a non-virtual primary
2653 base; we're not going to use that vtable anyhow. We do still
2654 need to do this for virtual primary bases, as they could become
2655 non-primary in a construction vtable. */
2656 return NULL_TREE;
2658 make_new_vtable (t, binfo);
2660 /* Now, go through each of the virtual functions in the virtual
2661 function table for BINFO. Find the final overrider, and update
2662 the BINFO_VIRTUALS list appropriately. */
2663 for (ix = 0, virtuals = BINFO_VIRTUALS (binfo),
2664 old_virtuals = BINFO_VIRTUALS (TYPE_BINFO (BINFO_TYPE (binfo)));
2665 virtuals;
2666 ix++, virtuals = TREE_CHAIN (virtuals),
2667 old_virtuals = TREE_CHAIN (old_virtuals))
2668 update_vtable_entry_for_fn (t,
2669 binfo,
2670 BV_FN (old_virtuals),
2671 &virtuals, ix);
2673 return NULL_TREE;
2676 /* Update all of the primary and secondary vtables for T. Create new
2677 vtables as required, and initialize their RTTI information. Each
2678 of the functions in VIRTUALS is declared in T and may override a
2679 virtual function from a base class; find and modify the appropriate
2680 entries to point to the overriding functions. Returns a list, in
2681 declaration order, of the virtual functions that are declared in T,
2682 but do not appear in the primary base class vtable, and which
2683 should therefore be appended to the end of the vtable for T. */
2685 static tree
2686 modify_all_vtables (tree t, tree virtuals)
2688 tree binfo = TYPE_BINFO (t);
2689 tree *fnsp;
2691 /* Mangle the vtable name before entering dfs_walk (c++/51884). */
2692 if (TYPE_CONTAINS_VPTR_P (t))
2693 get_vtable_decl (t, false);
2695 /* Update all of the vtables. */
2696 dfs_walk_once (binfo, dfs_modify_vtables, NULL, t);
2698 /* Add virtual functions not already in our primary vtable. These
2699 will be both those introduced by this class, and those overridden
2700 from secondary bases. It does not include virtuals merely
2701 inherited from secondary bases. */
2702 for (fnsp = &virtuals; *fnsp; )
2704 tree fn = TREE_VALUE (*fnsp);
2706 if (!value_member (fn, BINFO_VIRTUALS (binfo))
2707 || DECL_VINDEX (fn) == error_mark_node)
2709 /* We don't need to adjust the `this' pointer when
2710 calling this function. */
2711 BV_DELTA (*fnsp) = integer_zero_node;
2712 BV_VCALL_INDEX (*fnsp) = NULL_TREE;
2714 /* This is a function not already in our vtable. Keep it. */
2715 fnsp = &TREE_CHAIN (*fnsp);
2717 else
2718 /* We've already got an entry for this function. Skip it. */
2719 *fnsp = TREE_CHAIN (*fnsp);
2722 return virtuals;
2725 /* Get the base virtual function declarations in T that have the
2726 indicated NAME. */
2728 static void
2729 get_basefndecls (tree name, tree t, vec<tree> *base_fndecls)
2731 bool found_decls = false;
2733 /* Find virtual functions in T with the indicated NAME. */
2734 for (ovl_iterator iter (get_class_binding (t, name)); iter; ++iter)
2736 tree method = *iter;
2738 if (TREE_CODE (method) == FUNCTION_DECL && DECL_VINDEX (method))
2740 base_fndecls->safe_push (method);
2741 found_decls = true;
2745 if (found_decls)
2746 return;
2748 int n_baseclasses = BINFO_N_BASE_BINFOS (TYPE_BINFO (t));
2749 for (int i = 0; i < n_baseclasses; i++)
2751 tree basetype = BINFO_TYPE (BINFO_BASE_BINFO (TYPE_BINFO (t), i));
2752 get_basefndecls (name, basetype, base_fndecls);
2756 /* If this declaration supersedes the declaration of
2757 a method declared virtual in the base class, then
2758 mark this field as being virtual as well. */
2760 void
2761 check_for_override (tree decl, tree ctype)
2763 bool overrides_found = false;
2764 if (TREE_CODE (decl) == TEMPLATE_DECL)
2765 /* In [temp.mem] we have:
2767 A specialization of a member function template does not
2768 override a virtual function from a base class. */
2769 return;
2770 if ((DECL_DESTRUCTOR_P (decl)
2771 || IDENTIFIER_VIRTUAL_P (DECL_NAME (decl))
2772 || DECL_CONV_FN_P (decl))
2773 && look_for_overrides (ctype, decl)
2774 && !DECL_STATIC_FUNCTION_P (decl))
2775 /* Set DECL_VINDEX to a value that is neither an INTEGER_CST nor
2776 the error_mark_node so that we know it is an overriding
2777 function. */
2779 DECL_VINDEX (decl) = decl;
2780 overrides_found = true;
2781 if (warn_override && !DECL_OVERRIDE_P (decl)
2782 && !DECL_DESTRUCTOR_P (decl))
2783 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wsuggest_override,
2784 "%qD can be marked override", decl);
2787 if (DECL_VIRTUAL_P (decl))
2789 if (!DECL_VINDEX (decl))
2790 DECL_VINDEX (decl) = error_mark_node;
2791 IDENTIFIER_VIRTUAL_P (DECL_NAME (decl)) = 1;
2792 if (DECL_DESTRUCTOR_P (decl))
2793 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (ctype) = true;
2795 else if (DECL_FINAL_P (decl))
2796 error ("%q+#D marked %<final%>, but is not virtual", decl);
2797 if (DECL_OVERRIDE_P (decl) && !overrides_found)
2798 error ("%q+#D marked %<override%>, but does not override", decl);
2801 /* Warn about hidden virtual functions that are not overridden in t.
2802 We know that constructors and destructors don't apply. */
2804 static void
2805 warn_hidden (tree t)
2807 if (vec<tree, va_gc> *member_vec = CLASSTYPE_MEMBER_VEC (t))
2808 for (unsigned ix = member_vec->length (); ix--;)
2810 tree fns = (*member_vec)[ix];
2812 if (!OVL_P (fns))
2813 continue;
2815 tree name = OVL_NAME (fns);
2816 auto_vec<tree, 20> base_fndecls;
2817 tree base_binfo;
2818 tree binfo;
2819 unsigned j;
2821 /* Iterate through all of the base classes looking for possibly
2822 hidden functions. */
2823 for (binfo = TYPE_BINFO (t), j = 0;
2824 BINFO_BASE_ITERATE (binfo, j, base_binfo); j++)
2826 tree basetype = BINFO_TYPE (base_binfo);
2827 get_basefndecls (name, basetype, &base_fndecls);
2830 /* If there are no functions to hide, continue. */
2831 if (base_fndecls.is_empty ())
2832 continue;
2834 /* Remove any overridden functions. */
2835 for (ovl_iterator iter (fns); iter; ++iter)
2837 tree fndecl = *iter;
2838 if (TREE_CODE (fndecl) == FUNCTION_DECL
2839 && DECL_VINDEX (fndecl))
2841 /* If the method from the base class has the same
2842 signature as the method from the derived class, it
2843 has been overridden. */
2844 for (size_t k = 0; k < base_fndecls.length (); k++)
2845 if (base_fndecls[k]
2846 && same_signature_p (fndecl, base_fndecls[k]))
2847 base_fndecls[k] = NULL_TREE;
2851 /* Now give a warning for all base functions without overriders,
2852 as they are hidden. */
2853 tree base_fndecl;
2854 FOR_EACH_VEC_ELT (base_fndecls, j, base_fndecl)
2855 if (base_fndecl)
2857 /* Here we know it is a hider, and no overrider exists. */
2858 warning_at (location_of (base_fndecl),
2859 OPT_Woverloaded_virtual,
2860 "%qD was hidden", base_fndecl);
2861 warning_at (location_of (fns),
2862 OPT_Woverloaded_virtual, " by %qD", fns);
2867 /* Recursive helper for finish_struct_anon. */
2869 static void
2870 finish_struct_anon_r (tree field, bool complain)
2872 bool is_union = TREE_CODE (TREE_TYPE (field)) == UNION_TYPE;
2873 tree elt = TYPE_FIELDS (TREE_TYPE (field));
2874 for (; elt; elt = DECL_CHAIN (elt))
2876 /* We're generally only interested in entities the user
2877 declared, but we also find nested classes by noticing
2878 the TYPE_DECL that we create implicitly. You're
2879 allowed to put one anonymous union inside another,
2880 though, so we explicitly tolerate that. We use
2881 TYPE_UNNAMED_P rather than ANON_AGGR_TYPE_P so that
2882 we also allow unnamed types used for defining fields. */
2883 if (DECL_ARTIFICIAL (elt)
2884 && (!DECL_IMPLICIT_TYPEDEF_P (elt)
2885 || TYPE_UNNAMED_P (TREE_TYPE (elt))))
2886 continue;
2888 if (TREE_CODE (elt) != FIELD_DECL)
2890 /* We already complained about static data members in
2891 finish_static_data_member_decl. */
2892 if (complain && !VAR_P (elt))
2894 if (is_union)
2895 permerror (DECL_SOURCE_LOCATION (elt),
2896 "%q#D invalid; an anonymous union can "
2897 "only have non-static data members", elt);
2898 else
2899 permerror (DECL_SOURCE_LOCATION (elt),
2900 "%q#D invalid; an anonymous struct can "
2901 "only have non-static data members", elt);
2903 continue;
2906 if (complain)
2908 if (TREE_PRIVATE (elt))
2910 if (is_union)
2911 permerror (DECL_SOURCE_LOCATION (elt),
2912 "private member %q#D in anonymous union", elt);
2913 else
2914 permerror (DECL_SOURCE_LOCATION (elt),
2915 "private member %q#D in anonymous struct", elt);
2917 else if (TREE_PROTECTED (elt))
2919 if (is_union)
2920 permerror (DECL_SOURCE_LOCATION (elt),
2921 "protected member %q#D in anonymous union", elt);
2922 else
2923 permerror (DECL_SOURCE_LOCATION (elt),
2924 "protected member %q#D in anonymous struct", elt);
2928 TREE_PRIVATE (elt) = TREE_PRIVATE (field);
2929 TREE_PROTECTED (elt) = TREE_PROTECTED (field);
2931 /* Recurse into the anonymous aggregates to handle correctly
2932 access control (c++/24926):
2934 class A {
2935 union {
2936 union {
2937 int i;
2942 int j=A().i; */
2943 if (DECL_NAME (elt) == NULL_TREE
2944 && ANON_AGGR_TYPE_P (TREE_TYPE (elt)))
2945 finish_struct_anon_r (elt, /*complain=*/false);
2949 /* Check for things that are invalid. There are probably plenty of other
2950 things we should check for also. */
2952 static void
2953 finish_struct_anon (tree t)
2955 for (tree field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
2957 if (TREE_STATIC (field))
2958 continue;
2959 if (TREE_CODE (field) != FIELD_DECL)
2960 continue;
2962 if (DECL_NAME (field) == NULL_TREE
2963 && ANON_AGGR_TYPE_P (TREE_TYPE (field)))
2964 finish_struct_anon_r (field, /*complain=*/true);
2968 /* Add T to CLASSTYPE_DECL_LIST of current_class_type which
2969 will be used later during class template instantiation.
2970 When FRIEND_P is zero, T can be a static member data (VAR_DECL),
2971 a non-static member data (FIELD_DECL), a member function
2972 (FUNCTION_DECL), a nested type (RECORD_TYPE, ENUM_TYPE),
2973 a typedef (TYPE_DECL) or a member class template (TEMPLATE_DECL)
2974 When FRIEND_P is nonzero, T is either a friend class
2975 (RECORD_TYPE, TEMPLATE_DECL) or a friend function
2976 (FUNCTION_DECL, TEMPLATE_DECL). */
2978 void
2979 maybe_add_class_template_decl_list (tree type, tree t, int friend_p)
2981 /* Save some memory by not creating TREE_LIST if TYPE is not template. */
2982 if (CLASSTYPE_TEMPLATE_INFO (type))
2983 CLASSTYPE_DECL_LIST (type)
2984 = tree_cons (friend_p ? NULL_TREE : type,
2985 t, CLASSTYPE_DECL_LIST (type));
2988 /* This function is called from declare_virt_assop_and_dtor via
2989 dfs_walk_all.
2991 DATA is a type that direcly or indirectly inherits the base
2992 represented by BINFO. If BINFO contains a virtual assignment [copy
2993 assignment or move assigment] operator or a virtual constructor,
2994 declare that function in DATA if it hasn't been already declared. */
2996 static tree
2997 dfs_declare_virt_assop_and_dtor (tree binfo, void *data)
2999 tree bv, fn, t = (tree)data;
3000 tree opname = assign_op_identifier;
3002 gcc_assert (t && CLASS_TYPE_P (t));
3003 gcc_assert (binfo && TREE_CODE (binfo) == TREE_BINFO);
3005 if (!TYPE_CONTAINS_VPTR_P (BINFO_TYPE (binfo)))
3006 /* A base without a vtable needs no modification, and its bases
3007 are uninteresting. */
3008 return dfs_skip_bases;
3010 if (BINFO_PRIMARY_P (binfo))
3011 /* If this is a primary base, then we have already looked at the
3012 virtual functions of its vtable. */
3013 return NULL_TREE;
3015 for (bv = BINFO_VIRTUALS (binfo); bv; bv = TREE_CHAIN (bv))
3017 fn = BV_FN (bv);
3019 if (DECL_NAME (fn) == opname)
3021 if (CLASSTYPE_LAZY_COPY_ASSIGN (t))
3022 lazily_declare_fn (sfk_copy_assignment, t);
3023 if (CLASSTYPE_LAZY_MOVE_ASSIGN (t))
3024 lazily_declare_fn (sfk_move_assignment, t);
3026 else if (DECL_DESTRUCTOR_P (fn)
3027 && CLASSTYPE_LAZY_DESTRUCTOR (t))
3028 lazily_declare_fn (sfk_destructor, t);
3031 return NULL_TREE;
3034 /* If the class type T has a direct or indirect base that contains a
3035 virtual assignment operator or a virtual destructor, declare that
3036 function in T if it hasn't been already declared. */
3038 static void
3039 declare_virt_assop_and_dtor (tree t)
3041 if (!(TYPE_POLYMORPHIC_P (t)
3042 && (CLASSTYPE_LAZY_COPY_ASSIGN (t)
3043 || CLASSTYPE_LAZY_MOVE_ASSIGN (t)
3044 || CLASSTYPE_LAZY_DESTRUCTOR (t))))
3045 return;
3047 dfs_walk_all (TYPE_BINFO (t),
3048 dfs_declare_virt_assop_and_dtor,
3049 NULL, t);
3052 /* Declare the inheriting constructor for class T inherited from base
3053 constructor CTOR with the parameter array PARMS of size NPARMS. */
3055 static void
3056 one_inheriting_sig (tree t, tree ctor, tree *parms, int nparms)
3058 gcc_assert (TYPE_MAIN_VARIANT (t) == t);
3060 /* We don't declare an inheriting ctor that would be a default,
3061 copy or move ctor for derived or base. */
3062 if (nparms == 0)
3063 return;
3064 if (nparms == 1
3065 && TREE_CODE (parms[0]) == REFERENCE_TYPE)
3067 tree parm = TYPE_MAIN_VARIANT (TREE_TYPE (parms[0]));
3068 if (parm == t || parm == DECL_CONTEXT (ctor))
3069 return;
3072 tree parmlist = void_list_node;
3073 for (int i = nparms - 1; i >= 0; i--)
3074 parmlist = tree_cons (NULL_TREE, parms[i], parmlist);
3075 tree fn = implicitly_declare_fn (sfk_inheriting_constructor,
3076 t, false, ctor, parmlist);
3078 if (add_method (t, fn, false))
3080 DECL_CHAIN (fn) = TYPE_FIELDS (t);
3081 TYPE_FIELDS (t) = fn;
3085 /* Declare all the inheriting constructors for class T inherited from base
3086 constructor CTOR. */
3088 static void
3089 one_inherited_ctor (tree ctor, tree t, tree using_decl)
3091 tree parms = FUNCTION_FIRST_USER_PARMTYPE (ctor);
3093 if (flag_new_inheriting_ctors)
3095 ctor = implicitly_declare_fn (sfk_inheriting_constructor,
3096 t, /*const*/false, ctor, parms);
3097 add_method (t, ctor, using_decl != NULL_TREE);
3098 TYPE_HAS_USER_CONSTRUCTOR (t) = true;
3099 return;
3102 tree *new_parms = XALLOCAVEC (tree, list_length (parms));
3103 int i = 0;
3104 for (; parms && parms != void_list_node; parms = TREE_CHAIN (parms))
3106 if (TREE_PURPOSE (parms))
3107 one_inheriting_sig (t, ctor, new_parms, i);
3108 new_parms[i++] = TREE_VALUE (parms);
3110 one_inheriting_sig (t, ctor, new_parms, i);
3111 if (parms == NULL_TREE)
3113 if (warning (OPT_Winherited_variadic_ctor,
3114 "the ellipsis in %qD is not inherited", ctor))
3115 inform (DECL_SOURCE_LOCATION (ctor), "%qD declared here", ctor);
3119 /* Create default constructors, assignment operators, and so forth for
3120 the type indicated by T, if they are needed. CANT_HAVE_CONST_CTOR,
3121 and CANT_HAVE_CONST_ASSIGNMENT are nonzero if, for whatever reason,
3122 the class cannot have a default constructor, copy constructor
3123 taking a const reference argument, or an assignment operator taking
3124 a const reference, respectively. */
3126 static void
3127 add_implicitly_declared_members (tree t, tree* access_decls,
3128 int cant_have_const_cctor,
3129 int cant_have_const_assignment)
3131 /* Destructor. */
3132 if (!CLASSTYPE_DESTRUCTOR (t))
3133 /* In general, we create destructors lazily. */
3134 CLASSTYPE_LAZY_DESTRUCTOR (t) = 1;
3136 bool move_ok = false;
3137 if (cxx_dialect >= cxx11 && CLASSTYPE_LAZY_DESTRUCTOR (t)
3138 && !TYPE_HAS_COPY_CTOR (t) && !TYPE_HAS_COPY_ASSIGN (t)
3139 && !classtype_has_move_assign_or_move_ctor_p (t, false))
3140 move_ok = true;
3142 /* [class.ctor]
3144 If there is no user-declared constructor for a class, a default
3145 constructor is implicitly declared. */
3146 if (! TYPE_HAS_USER_CONSTRUCTOR (t))
3148 TYPE_HAS_DEFAULT_CONSTRUCTOR (t) = 1;
3149 CLASSTYPE_LAZY_DEFAULT_CTOR (t) = 1;
3150 if (cxx_dialect >= cxx11)
3151 TYPE_HAS_CONSTEXPR_CTOR (t)
3152 /* Don't force the declaration to get a hard answer; if the
3153 definition would have made the class non-literal, it will still be
3154 non-literal because of the base or member in question, and that
3155 gives a better diagnostic. */
3156 = type_maybe_constexpr_default_constructor (t);
3159 /* [class.ctor]
3161 If a class definition does not explicitly declare a copy
3162 constructor, one is declared implicitly. */
3163 if (! TYPE_HAS_COPY_CTOR (t))
3165 TYPE_HAS_COPY_CTOR (t) = 1;
3166 TYPE_HAS_CONST_COPY_CTOR (t) = !cant_have_const_cctor;
3167 CLASSTYPE_LAZY_COPY_CTOR (t) = 1;
3168 if (move_ok)
3169 CLASSTYPE_LAZY_MOVE_CTOR (t) = 1;
3172 /* If there is no assignment operator, one will be created if and
3173 when it is needed. For now, just record whether or not the type
3174 of the parameter to the assignment operator will be a const or
3175 non-const reference. */
3176 if (!TYPE_HAS_COPY_ASSIGN (t))
3178 TYPE_HAS_COPY_ASSIGN (t) = 1;
3179 TYPE_HAS_CONST_COPY_ASSIGN (t) = !cant_have_const_assignment;
3180 CLASSTYPE_LAZY_COPY_ASSIGN (t) = 1;
3181 if (move_ok && !LAMBDA_TYPE_P (t))
3182 CLASSTYPE_LAZY_MOVE_ASSIGN (t) = 1;
3185 /* We can't be lazy about declaring functions that might override
3186 a virtual function from a base class. */
3187 declare_virt_assop_and_dtor (t);
3189 while (*access_decls)
3191 tree using_decl = TREE_VALUE (*access_decls);
3192 tree decl = USING_DECL_DECLS (using_decl);
3193 if (DECL_NAME (using_decl) == ctor_identifier)
3195 /* declare, then remove the decl */
3196 tree ctor_list = decl;
3197 location_t loc = input_location;
3198 input_location = DECL_SOURCE_LOCATION (using_decl);
3199 for (ovl_iterator iter (ctor_list); iter; ++iter)
3200 one_inherited_ctor (*iter, t, using_decl);
3201 *access_decls = TREE_CHAIN (*access_decls);
3202 input_location = loc;
3204 else
3205 access_decls = &TREE_CHAIN (*access_decls);
3209 /* FIELD is a bit-field. We are finishing the processing for its
3210 enclosing type. Issue any appropriate messages and set appropriate
3211 flags. Returns false if an error has been diagnosed. */
3213 static bool
3214 check_bitfield_decl (tree field)
3216 tree type = TREE_TYPE (field);
3217 tree w;
3219 /* Extract the declared width of the bitfield, which has been
3220 temporarily stashed in DECL_BIT_FIELD_REPRESENTATIVE by grokbitfield. */
3221 w = DECL_BIT_FIELD_REPRESENTATIVE (field);
3222 gcc_assert (w != NULL_TREE);
3223 /* Remove the bit-field width indicator so that the rest of the
3224 compiler does not treat that value as a qualifier. */
3225 DECL_BIT_FIELD_REPRESENTATIVE (field) = NULL_TREE;
3227 /* Detect invalid bit-field type. */
3228 if (!INTEGRAL_OR_ENUMERATION_TYPE_P (type))
3230 error ("bit-field %q+#D with non-integral type", field);
3231 w = error_mark_node;
3233 else
3235 location_t loc = input_location;
3236 /* Avoid the non_lvalue wrapper added by fold for PLUS_EXPRs. */
3237 STRIP_NOPS (w);
3239 /* detect invalid field size. */
3240 input_location = DECL_SOURCE_LOCATION (field);
3241 w = cxx_constant_value (w);
3242 input_location = loc;
3244 if (TREE_CODE (w) != INTEGER_CST)
3246 error ("bit-field %q+D width not an integer constant", field);
3247 w = error_mark_node;
3249 else if (tree_int_cst_sgn (w) < 0)
3251 error ("negative width in bit-field %q+D", field);
3252 w = error_mark_node;
3254 else if (integer_zerop (w) && DECL_NAME (field) != 0)
3256 error ("zero width for bit-field %q+D", field);
3257 w = error_mark_node;
3259 else if ((TREE_CODE (type) != ENUMERAL_TYPE
3260 && TREE_CODE (type) != BOOLEAN_TYPE
3261 && compare_tree_int (w, TYPE_PRECISION (type)) > 0)
3262 || ((TREE_CODE (type) == ENUMERAL_TYPE
3263 || TREE_CODE (type) == BOOLEAN_TYPE)
3264 && tree_int_cst_lt (TYPE_SIZE (type), w)))
3265 warning_at (DECL_SOURCE_LOCATION (field), 0,
3266 "width of %qD exceeds its type", field);
3267 else if (TREE_CODE (type) == ENUMERAL_TYPE)
3269 int prec = TYPE_PRECISION (ENUM_UNDERLYING_TYPE (type));
3270 if (compare_tree_int (w, prec) < 0)
3271 warning_at (DECL_SOURCE_LOCATION (field), 0,
3272 "%qD is too small to hold all values of %q#T",
3273 field, type);
3277 if (w != error_mark_node)
3279 DECL_SIZE (field) = fold_convert (bitsizetype, w);
3280 DECL_BIT_FIELD (field) = 1;
3281 return true;
3283 else
3285 /* Non-bit-fields are aligned for their type. */
3286 DECL_BIT_FIELD (field) = 0;
3287 CLEAR_DECL_C_BIT_FIELD (field);
3288 return false;
3292 /* FIELD is a non bit-field. We are finishing the processing for its
3293 enclosing type T. Issue any appropriate messages and set appropriate
3294 flags. */
3296 static bool
3297 check_field_decl (tree field,
3298 tree t,
3299 int* cant_have_const_ctor,
3300 int* no_const_asn_ref)
3302 tree type = strip_array_types (TREE_TYPE (field));
3303 bool any_default_members = false;
3305 /* In C++98 an anonymous union cannot contain any fields which would change
3306 the settings of CANT_HAVE_CONST_CTOR and friends. */
3307 if (ANON_UNION_TYPE_P (type) && cxx_dialect < cxx11)
3309 /* And, we don't set TYPE_HAS_CONST_COPY_CTOR, etc., for anonymous
3310 structs. So, we recurse through their fields here. */
3311 else if (ANON_AGGR_TYPE_P (type))
3313 for (tree fields = TYPE_FIELDS (type); fields;
3314 fields = DECL_CHAIN (fields))
3315 if (TREE_CODE (fields) == FIELD_DECL)
3316 any_default_members |= check_field_decl (fields, t,
3317 cant_have_const_ctor,
3318 no_const_asn_ref);
3320 /* Check members with class type for constructors, destructors,
3321 etc. */
3322 else if (CLASS_TYPE_P (type))
3324 /* Never let anything with uninheritable virtuals
3325 make it through without complaint. */
3326 abstract_virtuals_error (field, type);
3328 if (TREE_CODE (t) == UNION_TYPE && cxx_dialect < cxx11)
3330 static bool warned;
3331 int oldcount = errorcount;
3332 if (TYPE_NEEDS_CONSTRUCTING (type))
3333 error ("member %q+#D with constructor not allowed in union",
3334 field);
3335 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
3336 error ("member %q+#D with destructor not allowed in union", field);
3337 if (TYPE_HAS_COMPLEX_COPY_ASSIGN (type))
3338 error ("member %q+#D with copy assignment operator not allowed in union",
3339 field);
3340 if (!warned && errorcount > oldcount)
3342 inform (DECL_SOURCE_LOCATION (field), "unrestricted unions "
3343 "only available with -std=c++11 or -std=gnu++11");
3344 warned = true;
3347 else
3349 TYPE_NEEDS_CONSTRUCTING (t) |= TYPE_NEEDS_CONSTRUCTING (type);
3350 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t)
3351 |= TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type);
3352 TYPE_HAS_COMPLEX_COPY_ASSIGN (t)
3353 |= (TYPE_HAS_COMPLEX_COPY_ASSIGN (type)
3354 || !TYPE_HAS_COPY_ASSIGN (type));
3355 TYPE_HAS_COMPLEX_COPY_CTOR (t) |= (TYPE_HAS_COMPLEX_COPY_CTOR (type)
3356 || !TYPE_HAS_COPY_CTOR (type));
3357 TYPE_HAS_COMPLEX_MOVE_ASSIGN (t) |= TYPE_HAS_COMPLEX_MOVE_ASSIGN (type);
3358 TYPE_HAS_COMPLEX_MOVE_CTOR (t) |= TYPE_HAS_COMPLEX_MOVE_CTOR (type);
3359 TYPE_HAS_COMPLEX_DFLT (t) |= (!TYPE_HAS_DEFAULT_CONSTRUCTOR (type)
3360 || TYPE_HAS_COMPLEX_DFLT (type));
3363 if (TYPE_HAS_COPY_CTOR (type)
3364 && !TYPE_HAS_CONST_COPY_CTOR (type))
3365 *cant_have_const_ctor = 1;
3367 if (TYPE_HAS_COPY_ASSIGN (type)
3368 && !TYPE_HAS_CONST_COPY_ASSIGN (type))
3369 *no_const_asn_ref = 1;
3372 check_abi_tags (t, field);
3374 if (DECL_INITIAL (field) != NULL_TREE)
3375 /* `build_class_init_list' does not recognize
3376 non-FIELD_DECLs. */
3377 any_default_members = true;
3379 return any_default_members;
3382 /* Check the data members (both static and non-static), class-scoped
3383 typedefs, etc., appearing in the declaration of T. Issue
3384 appropriate diagnostics. Sets ACCESS_DECLS to a list (in
3385 declaration order) of access declarations; each TREE_VALUE in this
3386 list is a USING_DECL.
3388 In addition, set the following flags:
3390 EMPTY_P
3391 The class is empty, i.e., contains no non-static data members.
3393 CANT_HAVE_CONST_CTOR_P
3394 This class cannot have an implicitly generated copy constructor
3395 taking a const reference.
3397 CANT_HAVE_CONST_ASN_REF
3398 This class cannot have an implicitly generated assignment
3399 operator taking a const reference.
3401 All of these flags should be initialized before calling this
3402 function.
3404 Returns a pointer to the end of the TYPE_FIELDs chain; additional
3405 fields can be added by adding to this chain. */
3407 static void
3408 check_field_decls (tree t, tree *access_decls,
3409 int *cant_have_const_ctor_p,
3410 int *no_const_asn_ref_p)
3412 tree *field;
3413 tree *next;
3414 bool has_pointers;
3415 bool any_default_members;
3416 int cant_pack = 0;
3417 int field_access = -1;
3419 /* Assume there are no access declarations. */
3420 *access_decls = NULL_TREE;
3421 /* Assume this class has no pointer members. */
3422 has_pointers = false;
3423 /* Assume none of the members of this class have default
3424 initializations. */
3425 any_default_members = false;
3427 for (field = &TYPE_FIELDS (t); *field; field = next)
3429 tree x = *field;
3430 tree type = TREE_TYPE (x);
3431 int this_field_access;
3433 next = &DECL_CHAIN (x);
3435 if (TREE_CODE (x) == USING_DECL)
3437 /* Save the access declarations for our caller. */
3438 *access_decls = tree_cons (NULL_TREE, x, *access_decls);
3439 continue;
3442 if (TREE_CODE (x) == TYPE_DECL
3443 || TREE_CODE (x) == TEMPLATE_DECL)
3444 continue;
3446 if (TREE_CODE (x) == FUNCTION_DECL)
3447 /* FIXME: We should fold in the checking from check_methods. */
3448 continue;
3450 /* If we've gotten this far, it's a data member, possibly static,
3451 or an enumerator. */
3452 if (TREE_CODE (x) != CONST_DECL)
3453 DECL_CONTEXT (x) = t;
3455 /* When this goes into scope, it will be a non-local reference. */
3456 DECL_NONLOCAL (x) = 1;
3458 if (TREE_CODE (t) == UNION_TYPE)
3460 /* [class.union] (C++98)
3462 If a union contains a static data member, or a member of
3463 reference type, the program is ill-formed.
3465 In C++11 [class.union] says:
3466 If a union contains a non-static data member of reference type
3467 the program is ill-formed. */
3468 if (VAR_P (x) && cxx_dialect < cxx11)
3470 error ("in C++98 %q+D may not be static because it is "
3471 "a member of a union", x);
3472 continue;
3474 if (TREE_CODE (type) == REFERENCE_TYPE
3475 && TREE_CODE (x) == FIELD_DECL)
3477 error ("non-static data member %q+D in a union may not "
3478 "have reference type %qT", x, type);
3479 continue;
3483 /* Perform error checking that did not get done in
3484 grokdeclarator. */
3485 if (TREE_CODE (type) == FUNCTION_TYPE)
3487 error ("field %q+D invalidly declared function type", x);
3488 type = build_pointer_type (type);
3489 TREE_TYPE (x) = type;
3491 else if (TREE_CODE (type) == METHOD_TYPE)
3493 error ("field %q+D invalidly declared method type", x);
3494 type = build_pointer_type (type);
3495 TREE_TYPE (x) = type;
3498 if (type == error_mark_node)
3499 continue;
3501 if (TREE_CODE (x) == CONST_DECL || VAR_P (x))
3502 continue;
3504 /* Now it can only be a FIELD_DECL. */
3506 if (TREE_PRIVATE (x) || TREE_PROTECTED (x))
3507 CLASSTYPE_NON_AGGREGATE (t) = 1;
3509 /* If at least one non-static data member is non-literal, the whole
3510 class becomes non-literal. Per Core/1453, volatile non-static
3511 data members and base classes are also not allowed.
3512 Note: if the type is incomplete we will complain later on. */
3513 if (COMPLETE_TYPE_P (type)
3514 && (!literal_type_p (type) || CP_TYPE_VOLATILE_P (type)))
3515 CLASSTYPE_LITERAL_P (t) = false;
3517 /* A standard-layout class is a class that:
3519 has the same access control (Clause 11) for all non-static data members,
3520 ... */
3521 this_field_access = TREE_PROTECTED (x) ? 1 : TREE_PRIVATE (x) ? 2 : 0;
3522 if (field_access == -1)
3523 field_access = this_field_access;
3524 else if (this_field_access != field_access)
3525 CLASSTYPE_NON_STD_LAYOUT (t) = 1;
3527 /* If this is of reference type, check if it needs an init. */
3528 if (TREE_CODE (type) == REFERENCE_TYPE)
3530 CLASSTYPE_NON_LAYOUT_POD_P (t) = 1;
3531 CLASSTYPE_NON_STD_LAYOUT (t) = 1;
3532 if (DECL_INITIAL (x) == NULL_TREE)
3533 SET_CLASSTYPE_REF_FIELDS_NEED_INIT (t, 1);
3534 if (cxx_dialect < cxx11)
3536 /* ARM $12.6.2: [A member initializer list] (or, for an
3537 aggregate, initialization by a brace-enclosed list) is the
3538 only way to initialize nonstatic const and reference
3539 members. */
3540 TYPE_HAS_COMPLEX_COPY_ASSIGN (t) = 1;
3541 TYPE_HAS_COMPLEX_MOVE_ASSIGN (t) = 1;
3545 type = strip_array_types (type);
3547 if (TYPE_PACKED (t))
3549 if (!layout_pod_type_p (type) && !TYPE_PACKED (type))
3551 warning_at
3552 (DECL_SOURCE_LOCATION (x), 0,
3553 "ignoring packed attribute because of unpacked non-POD field %q#D",
3555 cant_pack = 1;
3557 else if (DECL_C_BIT_FIELD (x)
3558 || TYPE_ALIGN (TREE_TYPE (x)) > BITS_PER_UNIT)
3559 DECL_PACKED (x) = 1;
3562 if (DECL_C_BIT_FIELD (x)
3563 && integer_zerop (DECL_BIT_FIELD_REPRESENTATIVE (x)))
3564 /* We don't treat zero-width bitfields as making a class
3565 non-empty. */
3567 else
3569 /* The class is non-empty. */
3570 CLASSTYPE_EMPTY_P (t) = 0;
3571 /* The class is not even nearly empty. */
3572 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
3573 /* If one of the data members contains an empty class,
3574 so does T. */
3575 if (CLASS_TYPE_P (type)
3576 && CLASSTYPE_CONTAINS_EMPTY_CLASS_P (type))
3577 CLASSTYPE_CONTAINS_EMPTY_CLASS_P (t) = 1;
3580 /* This is used by -Weffc++ (see below). Warn only for pointers
3581 to members which might hold dynamic memory. So do not warn
3582 for pointers to functions or pointers to members. */
3583 if (TYPE_PTR_P (type)
3584 && !TYPE_PTRFN_P (type))
3585 has_pointers = true;
3587 if (CLASS_TYPE_P (type))
3589 if (CLASSTYPE_REF_FIELDS_NEED_INIT (type))
3590 SET_CLASSTYPE_REF_FIELDS_NEED_INIT (t, 1);
3591 if (CLASSTYPE_READONLY_FIELDS_NEED_INIT (type))
3592 SET_CLASSTYPE_READONLY_FIELDS_NEED_INIT (t, 1);
3595 if (DECL_MUTABLE_P (x) || TYPE_HAS_MUTABLE_P (type))
3596 CLASSTYPE_HAS_MUTABLE (t) = 1;
3598 if (DECL_MUTABLE_P (x))
3600 if (CP_TYPE_CONST_P (type))
3602 error ("member %q+D cannot be declared both %<const%> "
3603 "and %<mutable%>", x);
3604 continue;
3606 if (TREE_CODE (type) == REFERENCE_TYPE)
3608 error ("member %q+D cannot be declared as a %<mutable%> "
3609 "reference", x);
3610 continue;
3614 if (! layout_pod_type_p (type))
3615 /* DR 148 now allows pointers to members (which are POD themselves),
3616 to be allowed in POD structs. */
3617 CLASSTYPE_NON_LAYOUT_POD_P (t) = 1;
3619 if (!std_layout_type_p (type))
3620 CLASSTYPE_NON_STD_LAYOUT (t) = 1;
3622 if (! zero_init_p (type))
3623 CLASSTYPE_NON_ZERO_INIT_P (t) = 1;
3625 /* We set DECL_C_BIT_FIELD in grokbitfield.
3626 If the type and width are valid, we'll also set DECL_BIT_FIELD. */
3627 if (DECL_C_BIT_FIELD (x))
3628 check_bitfield_decl (x);
3630 if (check_field_decl (x, t, cant_have_const_ctor_p, no_const_asn_ref_p))
3632 if (any_default_members
3633 && TREE_CODE (t) == UNION_TYPE)
3634 error ("multiple fields in union %qT initialized", t);
3635 any_default_members = true;
3638 /* Now that we've removed bit-field widths from DECL_INITIAL,
3639 anything left in DECL_INITIAL is an NSDMI that makes the class
3640 non-aggregate in C++11. */
3641 if (DECL_INITIAL (x) && cxx_dialect < cxx14)
3642 CLASSTYPE_NON_AGGREGATE (t) = true;
3644 /* If any field is const, the structure type is pseudo-const. */
3645 if (CP_TYPE_CONST_P (type))
3647 C_TYPE_FIELDS_READONLY (t) = 1;
3648 if (DECL_INITIAL (x) == NULL_TREE)
3649 SET_CLASSTYPE_READONLY_FIELDS_NEED_INIT (t, 1);
3650 if (cxx_dialect < cxx11)
3652 /* ARM $12.6.2: [A member initializer list] (or, for an
3653 aggregate, initialization by a brace-enclosed list) is the
3654 only way to initialize nonstatic const and reference
3655 members. */
3656 TYPE_HAS_COMPLEX_COPY_ASSIGN (t) = 1;
3657 TYPE_HAS_COMPLEX_MOVE_ASSIGN (t) = 1;
3660 /* A field that is pseudo-const makes the structure likewise. */
3661 else if (CLASS_TYPE_P (type))
3663 C_TYPE_FIELDS_READONLY (t) |= C_TYPE_FIELDS_READONLY (type);
3664 SET_CLASSTYPE_READONLY_FIELDS_NEED_INIT (t,
3665 CLASSTYPE_READONLY_FIELDS_NEED_INIT (t)
3666 | CLASSTYPE_READONLY_FIELDS_NEED_INIT (type));
3669 /* Core issue 80: A nonstatic data member is required to have a
3670 different name from the class iff the class has a
3671 user-declared constructor. */
3672 if (constructor_name_p (DECL_NAME (x), t)
3673 && TYPE_HAS_USER_CONSTRUCTOR (t))
3674 permerror (DECL_SOURCE_LOCATION (x),
3675 "field %q#D with same name as class", x);
3678 /* Effective C++ rule 11: if a class has dynamic memory held by pointers,
3679 it should also define a copy constructor and an assignment operator to
3680 implement the correct copy semantic (deep vs shallow, etc.). As it is
3681 not feasible to check whether the constructors do allocate dynamic memory
3682 and store it within members, we approximate the warning like this:
3684 -- Warn only if there are members which are pointers
3685 -- Warn only if there is a non-trivial constructor (otherwise,
3686 there cannot be memory allocated).
3687 -- Warn only if there is a non-trivial destructor. We assume that the
3688 user at least implemented the cleanup correctly, and a destructor
3689 is needed to free dynamic memory.
3691 This seems enough for practical purposes. */
3692 if (warn_ecpp
3693 && has_pointers
3694 && TYPE_HAS_USER_CONSTRUCTOR (t)
3695 && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t)
3696 && !(TYPE_HAS_COPY_CTOR (t) && TYPE_HAS_COPY_ASSIGN (t)))
3698 warning (OPT_Weffc__, "%q#T has pointer data members", t);
3700 if (! TYPE_HAS_COPY_CTOR (t))
3702 warning (OPT_Weffc__,
3703 " but does not override %<%T(const %T&)%>", t, t);
3704 if (!TYPE_HAS_COPY_ASSIGN (t))
3705 warning (OPT_Weffc__, " or %<operator=(const %T&)%>", t);
3707 else if (! TYPE_HAS_COPY_ASSIGN (t))
3708 warning (OPT_Weffc__,
3709 " but does not override %<operator=(const %T&)%>", t);
3712 /* Non-static data member initializers make the default constructor
3713 non-trivial. */
3714 if (any_default_members)
3716 TYPE_NEEDS_CONSTRUCTING (t) = true;
3717 TYPE_HAS_COMPLEX_DFLT (t) = true;
3720 /* If any of the fields couldn't be packed, unset TYPE_PACKED. */
3721 if (cant_pack)
3722 TYPE_PACKED (t) = 0;
3724 /* Check anonymous struct/anonymous union fields. */
3725 finish_struct_anon (t);
3727 /* We've built up the list of access declarations in reverse order.
3728 Fix that now. */
3729 *access_decls = nreverse (*access_decls);
3732 /* If TYPE is an empty class type, records its OFFSET in the table of
3733 OFFSETS. */
3735 static int
3736 record_subobject_offset (tree type, tree offset, splay_tree offsets)
3738 splay_tree_node n;
3740 if (!is_empty_class (type))
3741 return 0;
3743 /* Record the location of this empty object in OFFSETS. */
3744 n = splay_tree_lookup (offsets, (splay_tree_key) offset);
3745 if (!n)
3746 n = splay_tree_insert (offsets,
3747 (splay_tree_key) offset,
3748 (splay_tree_value) NULL_TREE);
3749 n->value = ((splay_tree_value)
3750 tree_cons (NULL_TREE,
3751 type,
3752 (tree) n->value));
3754 return 0;
3757 /* Returns nonzero if TYPE is an empty class type and there is
3758 already an entry in OFFSETS for the same TYPE as the same OFFSET. */
3760 static int
3761 check_subobject_offset (tree type, tree offset, splay_tree offsets)
3763 splay_tree_node n;
3764 tree t;
3766 if (!is_empty_class (type))
3767 return 0;
3769 /* Record the location of this empty object in OFFSETS. */
3770 n = splay_tree_lookup (offsets, (splay_tree_key) offset);
3771 if (!n)
3772 return 0;
3774 for (t = (tree) n->value; t; t = TREE_CHAIN (t))
3775 if (same_type_p (TREE_VALUE (t), type))
3776 return 1;
3778 return 0;
3781 /* Walk through all the subobjects of TYPE (located at OFFSET). Call
3782 F for every subobject, passing it the type, offset, and table of
3783 OFFSETS. If VBASES_P is one, then virtual non-primary bases should
3784 be traversed.
3786 If MAX_OFFSET is non-NULL, then subobjects with an offset greater
3787 than MAX_OFFSET will not be walked.
3789 If F returns a nonzero value, the traversal ceases, and that value
3790 is returned. Otherwise, returns zero. */
3792 static int
3793 walk_subobject_offsets (tree type,
3794 subobject_offset_fn f,
3795 tree offset,
3796 splay_tree offsets,
3797 tree max_offset,
3798 int vbases_p)
3800 int r = 0;
3801 tree type_binfo = NULL_TREE;
3803 /* If this OFFSET is bigger than the MAX_OFFSET, then we should
3804 stop. */
3805 if (max_offset && tree_int_cst_lt (max_offset, offset))
3806 return 0;
3808 if (type == error_mark_node)
3809 return 0;
3811 if (!TYPE_P (type))
3813 type_binfo = type;
3814 type = BINFO_TYPE (type);
3817 if (CLASS_TYPE_P (type))
3819 tree field;
3820 tree binfo;
3821 int i;
3823 /* Avoid recursing into objects that are not interesting. */
3824 if (!CLASSTYPE_CONTAINS_EMPTY_CLASS_P (type))
3825 return 0;
3827 /* Record the location of TYPE. */
3828 r = (*f) (type, offset, offsets);
3829 if (r)
3830 return r;
3832 /* Iterate through the direct base classes of TYPE. */
3833 if (!type_binfo)
3834 type_binfo = TYPE_BINFO (type);
3835 for (i = 0; BINFO_BASE_ITERATE (type_binfo, i, binfo); i++)
3837 tree binfo_offset;
3839 if (BINFO_VIRTUAL_P (binfo))
3840 continue;
3842 tree orig_binfo;
3843 /* We cannot rely on BINFO_OFFSET being set for the base
3844 class yet, but the offsets for direct non-virtual
3845 bases can be calculated by going back to the TYPE. */
3846 orig_binfo = BINFO_BASE_BINFO (TYPE_BINFO (type), i);
3847 binfo_offset = size_binop (PLUS_EXPR,
3848 offset,
3849 BINFO_OFFSET (orig_binfo));
3851 r = walk_subobject_offsets (binfo,
3853 binfo_offset,
3854 offsets,
3855 max_offset,
3856 /*vbases_p=*/0);
3857 if (r)
3858 return r;
3861 if (CLASSTYPE_VBASECLASSES (type))
3863 unsigned ix;
3864 vec<tree, va_gc> *vbases;
3866 /* Iterate through the virtual base classes of TYPE. In G++
3867 3.2, we included virtual bases in the direct base class
3868 loop above, which results in incorrect results; the
3869 correct offsets for virtual bases are only known when
3870 working with the most derived type. */
3871 if (vbases_p)
3872 for (vbases = CLASSTYPE_VBASECLASSES (type), ix = 0;
3873 vec_safe_iterate (vbases, ix, &binfo); ix++)
3875 r = walk_subobject_offsets (binfo,
3877 size_binop (PLUS_EXPR,
3878 offset,
3879 BINFO_OFFSET (binfo)),
3880 offsets,
3881 max_offset,
3882 /*vbases_p=*/0);
3883 if (r)
3884 return r;
3886 else
3888 /* We still have to walk the primary base, if it is
3889 virtual. (If it is non-virtual, then it was walked
3890 above.) */
3891 tree vbase = get_primary_binfo (type_binfo);
3893 if (vbase && BINFO_VIRTUAL_P (vbase)
3894 && BINFO_PRIMARY_P (vbase)
3895 && BINFO_INHERITANCE_CHAIN (vbase) == type_binfo)
3897 r = (walk_subobject_offsets
3898 (vbase, f, offset,
3899 offsets, max_offset, /*vbases_p=*/0));
3900 if (r)
3901 return r;
3906 /* Iterate through the fields of TYPE. */
3907 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
3908 if (TREE_CODE (field) == FIELD_DECL
3909 && TREE_TYPE (field) != error_mark_node
3910 && !DECL_ARTIFICIAL (field))
3912 tree field_offset;
3914 field_offset = byte_position (field);
3916 r = walk_subobject_offsets (TREE_TYPE (field),
3918 size_binop (PLUS_EXPR,
3919 offset,
3920 field_offset),
3921 offsets,
3922 max_offset,
3923 /*vbases_p=*/1);
3924 if (r)
3925 return r;
3928 else if (TREE_CODE (type) == ARRAY_TYPE)
3930 tree element_type = strip_array_types (type);
3931 tree domain = TYPE_DOMAIN (type);
3932 tree index;
3934 /* Avoid recursing into objects that are not interesting. */
3935 if (!CLASS_TYPE_P (element_type)
3936 || !CLASSTYPE_CONTAINS_EMPTY_CLASS_P (element_type)
3937 || !domain
3938 || integer_minus_onep (TYPE_MAX_VALUE (domain)))
3939 return 0;
3941 /* Step through each of the elements in the array. */
3942 for (index = size_zero_node;
3943 !tree_int_cst_lt (TYPE_MAX_VALUE (domain), index);
3944 index = size_binop (PLUS_EXPR, index, size_one_node))
3946 r = walk_subobject_offsets (TREE_TYPE (type),
3948 offset,
3949 offsets,
3950 max_offset,
3951 /*vbases_p=*/1);
3952 if (r)
3953 return r;
3954 offset = size_binop (PLUS_EXPR, offset,
3955 TYPE_SIZE_UNIT (TREE_TYPE (type)));
3956 /* If this new OFFSET is bigger than the MAX_OFFSET, then
3957 there's no point in iterating through the remaining
3958 elements of the array. */
3959 if (max_offset && tree_int_cst_lt (max_offset, offset))
3960 break;
3964 return 0;
3967 /* Record all of the empty subobjects of TYPE (either a type or a
3968 binfo). If IS_DATA_MEMBER is true, then a non-static data member
3969 is being placed at OFFSET; otherwise, it is a base class that is
3970 being placed at OFFSET. */
3972 static void
3973 record_subobject_offsets (tree type,
3974 tree offset,
3975 splay_tree offsets,
3976 bool is_data_member)
3978 tree max_offset;
3979 /* If recording subobjects for a non-static data member or a
3980 non-empty base class , we do not need to record offsets beyond
3981 the size of the biggest empty class. Additional data members
3982 will go at the end of the class. Additional base classes will go
3983 either at offset zero (if empty, in which case they cannot
3984 overlap with offsets past the size of the biggest empty class) or
3985 at the end of the class.
3987 However, if we are placing an empty base class, then we must record
3988 all offsets, as either the empty class is at offset zero (where
3989 other empty classes might later be placed) or at the end of the
3990 class (where other objects might then be placed, so other empty
3991 subobjects might later overlap). */
3992 if (is_data_member
3993 || !is_empty_class (BINFO_TYPE (type)))
3994 max_offset = sizeof_biggest_empty_class;
3995 else
3996 max_offset = NULL_TREE;
3997 walk_subobject_offsets (type, record_subobject_offset, offset,
3998 offsets, max_offset, is_data_member);
4001 /* Returns nonzero if any of the empty subobjects of TYPE (located at
4002 OFFSET) conflict with entries in OFFSETS. If VBASES_P is nonzero,
4003 virtual bases of TYPE are examined. */
4005 static int
4006 layout_conflict_p (tree type,
4007 tree offset,
4008 splay_tree offsets,
4009 int vbases_p)
4011 splay_tree_node max_node;
4013 /* Get the node in OFFSETS that indicates the maximum offset where
4014 an empty subobject is located. */
4015 max_node = splay_tree_max (offsets);
4016 /* If there aren't any empty subobjects, then there's no point in
4017 performing this check. */
4018 if (!max_node)
4019 return 0;
4021 return walk_subobject_offsets (type, check_subobject_offset, offset,
4022 offsets, (tree) (max_node->key),
4023 vbases_p);
4026 /* DECL is a FIELD_DECL corresponding either to a base subobject of a
4027 non-static data member of the type indicated by RLI. BINFO is the
4028 binfo corresponding to the base subobject, OFFSETS maps offsets to
4029 types already located at those offsets. This function determines
4030 the position of the DECL. */
4032 static void
4033 layout_nonempty_base_or_field (record_layout_info rli,
4034 tree decl,
4035 tree binfo,
4036 splay_tree offsets)
4038 tree offset = NULL_TREE;
4039 bool field_p;
4040 tree type;
4042 if (binfo)
4044 /* For the purposes of determining layout conflicts, we want to
4045 use the class type of BINFO; TREE_TYPE (DECL) will be the
4046 CLASSTYPE_AS_BASE version, which does not contain entries for
4047 zero-sized bases. */
4048 type = TREE_TYPE (binfo);
4049 field_p = false;
4051 else
4053 type = TREE_TYPE (decl);
4054 field_p = true;
4057 /* Try to place the field. It may take more than one try if we have
4058 a hard time placing the field without putting two objects of the
4059 same type at the same address. */
4060 while (1)
4062 struct record_layout_info_s old_rli = *rli;
4064 /* Place this field. */
4065 place_field (rli, decl);
4066 offset = byte_position (decl);
4068 /* We have to check to see whether or not there is already
4069 something of the same type at the offset we're about to use.
4070 For example, consider:
4072 struct S {};
4073 struct T : public S { int i; };
4074 struct U : public S, public T {};
4076 Here, we put S at offset zero in U. Then, we can't put T at
4077 offset zero -- its S component would be at the same address
4078 as the S we already allocated. So, we have to skip ahead.
4079 Since all data members, including those whose type is an
4080 empty class, have nonzero size, any overlap can happen only
4081 with a direct or indirect base-class -- it can't happen with
4082 a data member. */
4083 /* In a union, overlap is permitted; all members are placed at
4084 offset zero. */
4085 if (TREE_CODE (rli->t) == UNION_TYPE)
4086 break;
4087 if (layout_conflict_p (field_p ? type : binfo, offset,
4088 offsets, field_p))
4090 /* Strip off the size allocated to this field. That puts us
4091 at the first place we could have put the field with
4092 proper alignment. */
4093 *rli = old_rli;
4095 /* Bump up by the alignment required for the type. */
4096 rli->bitpos
4097 = size_binop (PLUS_EXPR, rli->bitpos,
4098 bitsize_int (binfo
4099 ? CLASSTYPE_ALIGN (type)
4100 : TYPE_ALIGN (type)));
4101 normalize_rli (rli);
4103 else if (TREE_CODE (type) == NULLPTR_TYPE
4104 && warn_abi && abi_version_crosses (9))
4106 /* Before ABI v9, we were giving nullptr_t alignment of 1; if
4107 the offset wasn't aligned like a pointer when we started to
4108 layout this field, that affects its position. */
4109 tree pos = rli_size_unit_so_far (&old_rli);
4110 if (int_cst_value (pos) % TYPE_ALIGN_UNIT (ptr_type_node) != 0)
4112 if (abi_version_at_least (9))
4113 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wabi,
4114 "alignment of %qD increased in -fabi-version=9 "
4115 "(GCC 5.2)", decl);
4116 else
4117 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wabi, "alignment "
4118 "of %qD will increase in -fabi-version=9", decl);
4120 break;
4122 else
4123 /* There was no conflict. We're done laying out this field. */
4124 break;
4127 /* Now that we know where it will be placed, update its
4128 BINFO_OFFSET. */
4129 if (binfo && CLASS_TYPE_P (BINFO_TYPE (binfo)))
4130 /* Indirect virtual bases may have a nonzero BINFO_OFFSET at
4131 this point because their BINFO_OFFSET is copied from another
4132 hierarchy. Therefore, we may not need to add the entire
4133 OFFSET. */
4134 propagate_binfo_offsets (binfo,
4135 size_diffop_loc (input_location,
4136 fold_convert (ssizetype, offset),
4137 fold_convert (ssizetype,
4138 BINFO_OFFSET (binfo))));
4141 /* Returns true if TYPE is empty and OFFSET is nonzero. */
4143 static int
4144 empty_base_at_nonzero_offset_p (tree type,
4145 tree offset,
4146 splay_tree /*offsets*/)
4148 return is_empty_class (type) && !integer_zerop (offset);
4151 /* Layout the empty base BINFO. EOC indicates the byte currently just
4152 past the end of the class, and should be correctly aligned for a
4153 class of the type indicated by BINFO; OFFSETS gives the offsets of
4154 the empty bases allocated so far. T is the most derived
4155 type. Return nonzero iff we added it at the end. */
4157 static bool
4158 layout_empty_base (record_layout_info rli, tree binfo,
4159 tree eoc, splay_tree offsets)
4161 tree alignment;
4162 tree basetype = BINFO_TYPE (binfo);
4163 bool atend = false;
4165 /* This routine should only be used for empty classes. */
4166 gcc_assert (is_empty_class (basetype));
4167 alignment = ssize_int (CLASSTYPE_ALIGN_UNIT (basetype));
4169 if (!integer_zerop (BINFO_OFFSET (binfo)))
4170 propagate_binfo_offsets
4171 (binfo, size_diffop_loc (input_location,
4172 size_zero_node, BINFO_OFFSET (binfo)));
4174 /* This is an empty base class. We first try to put it at offset
4175 zero. */
4176 if (layout_conflict_p (binfo,
4177 BINFO_OFFSET (binfo),
4178 offsets,
4179 /*vbases_p=*/0))
4181 /* That didn't work. Now, we move forward from the next
4182 available spot in the class. */
4183 atend = true;
4184 propagate_binfo_offsets (binfo, fold_convert (ssizetype, eoc));
4185 while (1)
4187 if (!layout_conflict_p (binfo,
4188 BINFO_OFFSET (binfo),
4189 offsets,
4190 /*vbases_p=*/0))
4191 /* We finally found a spot where there's no overlap. */
4192 break;
4194 /* There's overlap here, too. Bump along to the next spot. */
4195 propagate_binfo_offsets (binfo, alignment);
4199 if (CLASSTYPE_USER_ALIGN (basetype))
4201 rli->record_align = MAX (rli->record_align, CLASSTYPE_ALIGN (basetype));
4202 if (warn_packed)
4203 rli->unpacked_align = MAX (rli->unpacked_align, CLASSTYPE_ALIGN (basetype));
4204 TYPE_USER_ALIGN (rli->t) = 1;
4207 return atend;
4210 /* Build the FIELD_DECL for BASETYPE as a base of T, add it to the chain of
4211 fields at NEXT_FIELD, and return it. */
4213 static tree
4214 build_base_field_1 (tree t, tree basetype, tree *&next_field)
4216 /* Create the FIELD_DECL. */
4217 gcc_assert (CLASSTYPE_AS_BASE (basetype));
4218 tree decl = build_decl (input_location,
4219 FIELD_DECL, NULL_TREE, CLASSTYPE_AS_BASE (basetype));
4220 DECL_ARTIFICIAL (decl) = 1;
4221 DECL_IGNORED_P (decl) = 1;
4222 DECL_FIELD_CONTEXT (decl) = t;
4223 if (is_empty_class (basetype))
4224 /* CLASSTYPE_SIZE is one byte, but the field needs to have size zero. */
4225 DECL_SIZE (decl) = DECL_SIZE_UNIT (decl) = size_zero_node;
4226 else
4228 DECL_SIZE (decl) = CLASSTYPE_SIZE (basetype);
4229 DECL_SIZE_UNIT (decl) = CLASSTYPE_SIZE_UNIT (basetype);
4231 SET_DECL_ALIGN (decl, CLASSTYPE_ALIGN (basetype));
4232 DECL_USER_ALIGN (decl) = CLASSTYPE_USER_ALIGN (basetype);
4233 SET_DECL_MODE (decl, TYPE_MODE (basetype));
4234 DECL_FIELD_IS_BASE (decl) = 1;
4236 /* Add the new FIELD_DECL to the list of fields for T. */
4237 DECL_CHAIN (decl) = *next_field;
4238 *next_field = decl;
4239 next_field = &DECL_CHAIN (decl);
4241 return decl;
4244 /* Layout the base given by BINFO in the class indicated by RLI.
4245 *BASE_ALIGN is a running maximum of the alignments of
4246 any base class. OFFSETS gives the location of empty base
4247 subobjects. T is the most derived type. Return nonzero if the new
4248 object cannot be nearly-empty. A new FIELD_DECL is inserted at
4249 *NEXT_FIELD, unless BINFO is for an empty base class.
4251 Returns the location at which the next field should be inserted. */
4253 static tree *
4254 build_base_field (record_layout_info rli, tree binfo,
4255 splay_tree offsets, tree *next_field)
4257 tree t = rli->t;
4258 tree basetype = BINFO_TYPE (binfo);
4260 if (!COMPLETE_TYPE_P (basetype))
4261 /* This error is now reported in xref_tag, thus giving better
4262 location information. */
4263 return next_field;
4265 /* Place the base class. */
4266 if (!is_empty_class (basetype))
4268 tree decl;
4270 /* The containing class is non-empty because it has a non-empty
4271 base class. */
4272 CLASSTYPE_EMPTY_P (t) = 0;
4274 /* Create the FIELD_DECL. */
4275 decl = build_base_field_1 (t, basetype, next_field);
4277 /* Try to place the field. It may take more than one try if we
4278 have a hard time placing the field without putting two
4279 objects of the same type at the same address. */
4280 layout_nonempty_base_or_field (rli, decl, binfo, offsets);
4282 else
4284 tree eoc;
4285 bool atend;
4287 /* On some platforms (ARM), even empty classes will not be
4288 byte-aligned. */
4289 eoc = round_up_loc (input_location,
4290 rli_size_unit_so_far (rli),
4291 CLASSTYPE_ALIGN_UNIT (basetype));
4292 atend = layout_empty_base (rli, binfo, eoc, offsets);
4293 /* A nearly-empty class "has no proper base class that is empty,
4294 not morally virtual, and at an offset other than zero." */
4295 if (!BINFO_VIRTUAL_P (binfo) && CLASSTYPE_NEARLY_EMPTY_P (t))
4297 if (atend)
4298 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
4299 /* The check above (used in G++ 3.2) is insufficient because
4300 an empty class placed at offset zero might itself have an
4301 empty base at a nonzero offset. */
4302 else if (walk_subobject_offsets (basetype,
4303 empty_base_at_nonzero_offset_p,
4304 size_zero_node,
4305 /*offsets=*/NULL,
4306 /*max_offset=*/NULL_TREE,
4307 /*vbases_p=*/true))
4308 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
4311 /* We used to not create a FIELD_DECL for empty base classes because of
4312 back end issues with overlapping FIELD_DECLs, but that doesn't seem to
4313 be a problem anymore. We need them to handle initialization of C++17
4314 aggregate bases. */
4315 if (cxx_dialect >= cxx17 && !BINFO_VIRTUAL_P (binfo))
4317 tree decl = build_base_field_1 (t, basetype, next_field);
4318 DECL_FIELD_OFFSET (decl) = BINFO_OFFSET (binfo);
4319 DECL_FIELD_BIT_OFFSET (decl) = bitsize_zero_node;
4320 SET_DECL_OFFSET_ALIGN (decl, BITS_PER_UNIT);
4323 /* An empty virtual base causes a class to be non-empty
4324 -- but in that case we do not need to clear CLASSTYPE_EMPTY_P
4325 here because that was already done when the virtual table
4326 pointer was created. */
4329 /* Record the offsets of BINFO and its base subobjects. */
4330 record_subobject_offsets (binfo,
4331 BINFO_OFFSET (binfo),
4332 offsets,
4333 /*is_data_member=*/false);
4335 return next_field;
4338 /* Layout all of the non-virtual base classes. Record empty
4339 subobjects in OFFSETS. T is the most derived type. Return nonzero
4340 if the type cannot be nearly empty. The fields created
4341 corresponding to the base classes will be inserted at
4342 *NEXT_FIELD. */
4344 static void
4345 build_base_fields (record_layout_info rli,
4346 splay_tree offsets, tree *next_field)
4348 /* Chain to hold all the new FIELD_DECLs which stand in for base class
4349 subobjects. */
4350 tree t = rli->t;
4351 int n_baseclasses = BINFO_N_BASE_BINFOS (TYPE_BINFO (t));
4352 int i;
4354 /* The primary base class is always allocated first. */
4355 if (CLASSTYPE_HAS_PRIMARY_BASE_P (t))
4356 next_field = build_base_field (rli, CLASSTYPE_PRIMARY_BINFO (t),
4357 offsets, next_field);
4359 /* Now allocate the rest of the bases. */
4360 for (i = 0; i < n_baseclasses; ++i)
4362 tree base_binfo;
4364 base_binfo = BINFO_BASE_BINFO (TYPE_BINFO (t), i);
4366 /* The primary base was already allocated above, so we don't
4367 need to allocate it again here. */
4368 if (base_binfo == CLASSTYPE_PRIMARY_BINFO (t))
4369 continue;
4371 /* Virtual bases are added at the end (a primary virtual base
4372 will have already been added). */
4373 if (BINFO_VIRTUAL_P (base_binfo))
4374 continue;
4376 next_field = build_base_field (rli, base_binfo,
4377 offsets, next_field);
4381 /* Go through the TYPE_FIELDS of T issuing any appropriate
4382 diagnostics, figuring out which methods override which other
4383 methods, and so forth. */
4385 static void
4386 check_methods (tree t)
4388 for (tree x = TYPE_FIELDS (t); x; x = DECL_CHAIN (x))
4389 if (DECL_DECLARES_FUNCTION_P (x))
4391 check_for_override (x, t);
4393 if (DECL_PURE_VIRTUAL_P (x)
4394 && (TREE_CODE (x) != FUNCTION_DECL || ! DECL_VINDEX (x)))
4395 error ("initializer specified for non-virtual method %q+D", x);
4396 /* The name of the field is the original field name
4397 Save this in auxiliary field for later overloading. */
4398 if (TREE_CODE (x) == FUNCTION_DECL && DECL_VINDEX (x))
4400 TYPE_POLYMORPHIC_P (t) = 1;
4401 if (DECL_PURE_VIRTUAL_P (x))
4402 vec_safe_push (CLASSTYPE_PURE_VIRTUALS (t), x);
4405 /* All user-provided destructors are non-trivial.
4406 Constructors and assignment ops are handled in
4407 grok_special_member_properties. */
4408 if (DECL_DESTRUCTOR_P (x) && user_provided_p (x))
4409 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t) = 1;
4410 if (!DECL_VIRTUAL_P (x)
4411 && lookup_attribute ("transaction_safe_dynamic",
4412 DECL_ATTRIBUTES (x)))
4413 error_at (DECL_SOURCE_LOCATION (x),
4414 "%<transaction_safe_dynamic%> may only be specified for "
4415 "a virtual function");
4419 /* FN is a constructor or destructor. Clone the declaration to create
4420 a specialized in-charge or not-in-charge version, as indicated by
4421 NAME. */
4423 static tree
4424 build_clone (tree fn, tree name)
4426 tree parms;
4427 tree clone;
4429 /* Copy the function. */
4430 clone = copy_decl (fn);
4431 /* Reset the function name. */
4432 DECL_NAME (clone) = name;
4433 /* Remember where this function came from. */
4434 DECL_ABSTRACT_ORIGIN (clone) = fn;
4435 /* Make it easy to find the CLONE given the FN. */
4436 DECL_CHAIN (clone) = DECL_CHAIN (fn);
4437 DECL_CHAIN (fn) = clone;
4439 /* If this is a template, do the rest on the DECL_TEMPLATE_RESULT. */
4440 if (TREE_CODE (clone) == TEMPLATE_DECL)
4442 tree result = build_clone (DECL_TEMPLATE_RESULT (clone), name);
4443 DECL_TEMPLATE_RESULT (clone) = result;
4444 DECL_TEMPLATE_INFO (result) = copy_node (DECL_TEMPLATE_INFO (result));
4445 DECL_TI_TEMPLATE (result) = clone;
4446 TREE_TYPE (clone) = TREE_TYPE (result);
4447 return clone;
4449 else
4451 // Clone constraints.
4452 if (flag_concepts)
4453 if (tree ci = get_constraints (fn))
4454 set_constraints (clone, copy_node (ci));
4458 SET_DECL_ASSEMBLER_NAME (clone, NULL_TREE);
4459 DECL_CLONED_FUNCTION (clone) = fn;
4460 /* There's no pending inline data for this function. */
4461 DECL_PENDING_INLINE_INFO (clone) = NULL;
4462 DECL_PENDING_INLINE_P (clone) = 0;
4464 /* The base-class destructor is not virtual. */
4465 if (name == base_dtor_identifier)
4467 DECL_VIRTUAL_P (clone) = 0;
4468 if (TREE_CODE (clone) != TEMPLATE_DECL)
4469 DECL_VINDEX (clone) = NULL_TREE;
4472 bool ctor_omit_inherited_parms_p = ctor_omit_inherited_parms (clone);
4473 if (ctor_omit_inherited_parms_p)
4474 gcc_assert (DECL_HAS_IN_CHARGE_PARM_P (clone));
4476 /* If there was an in-charge parameter, drop it from the function
4477 type. */
4478 if (DECL_HAS_IN_CHARGE_PARM_P (clone))
4480 tree basetype;
4481 tree parmtypes;
4482 tree exceptions;
4484 exceptions = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (clone));
4485 basetype = TYPE_METHOD_BASETYPE (TREE_TYPE (clone));
4486 parmtypes = TYPE_ARG_TYPES (TREE_TYPE (clone));
4487 /* Skip the `this' parameter. */
4488 parmtypes = TREE_CHAIN (parmtypes);
4489 /* Skip the in-charge parameter. */
4490 parmtypes = TREE_CHAIN (parmtypes);
4491 /* And the VTT parm, in a complete [cd]tor. */
4492 if (DECL_HAS_VTT_PARM_P (fn)
4493 && ! DECL_NEEDS_VTT_PARM_P (clone))
4494 parmtypes = TREE_CHAIN (parmtypes);
4495 if (ctor_omit_inherited_parms_p)
4497 /* If we're omitting inherited parms, that just leaves the VTT. */
4498 gcc_assert (DECL_NEEDS_VTT_PARM_P (clone));
4499 parmtypes = tree_cons (NULL_TREE, vtt_parm_type, void_list_node);
4501 TREE_TYPE (clone)
4502 = build_method_type_directly (basetype,
4503 TREE_TYPE (TREE_TYPE (clone)),
4504 parmtypes);
4505 if (exceptions)
4506 TREE_TYPE (clone) = build_exception_variant (TREE_TYPE (clone),
4507 exceptions);
4508 TREE_TYPE (clone)
4509 = cp_build_type_attribute_variant (TREE_TYPE (clone),
4510 TYPE_ATTRIBUTES (TREE_TYPE (fn)));
4513 /* Copy the function parameters. */
4514 DECL_ARGUMENTS (clone) = copy_list (DECL_ARGUMENTS (clone));
4515 /* Remove the in-charge parameter. */
4516 if (DECL_HAS_IN_CHARGE_PARM_P (clone))
4518 DECL_CHAIN (DECL_ARGUMENTS (clone))
4519 = DECL_CHAIN (DECL_CHAIN (DECL_ARGUMENTS (clone)));
4520 DECL_HAS_IN_CHARGE_PARM_P (clone) = 0;
4522 /* And the VTT parm, in a complete [cd]tor. */
4523 if (DECL_HAS_VTT_PARM_P (fn))
4525 if (DECL_NEEDS_VTT_PARM_P (clone))
4526 DECL_HAS_VTT_PARM_P (clone) = 1;
4527 else
4529 DECL_CHAIN (DECL_ARGUMENTS (clone))
4530 = DECL_CHAIN (DECL_CHAIN (DECL_ARGUMENTS (clone)));
4531 DECL_HAS_VTT_PARM_P (clone) = 0;
4535 /* A base constructor inheriting from a virtual base doesn't get the
4536 arguments. */
4537 if (ctor_omit_inherited_parms_p)
4538 DECL_CHAIN (DECL_CHAIN (DECL_ARGUMENTS (clone))) = NULL_TREE;
4540 for (parms = DECL_ARGUMENTS (clone); parms; parms = DECL_CHAIN (parms))
4542 DECL_CONTEXT (parms) = clone;
4543 cxx_dup_lang_specific_decl (parms);
4546 /* Create the RTL for this function. */
4547 SET_DECL_RTL (clone, NULL);
4548 rest_of_decl_compilation (clone, /*top_level=*/1, at_eof);
4550 return clone;
4553 /* Implementation of DECL_CLONED_FUNCTION and DECL_CLONED_FUNCTION_P, do
4554 not invoke this function directly.
4556 For a non-thunk function, returns the address of the slot for storing
4557 the function it is a clone of. Otherwise returns NULL_TREE.
4559 If JUST_TESTING, looks through TEMPLATE_DECL and returns NULL if
4560 cloned_function is unset. This is to support the separate
4561 DECL_CLONED_FUNCTION and DECL_CLONED_FUNCTION_P modes; using the latter
4562 on a template makes sense, but not the former. */
4564 tree *
4565 decl_cloned_function_p (const_tree decl, bool just_testing)
4567 tree *ptr;
4568 if (just_testing)
4569 decl = STRIP_TEMPLATE (decl);
4571 if (TREE_CODE (decl) != FUNCTION_DECL
4572 || !DECL_LANG_SPECIFIC (decl)
4573 || DECL_LANG_SPECIFIC (decl)->u.fn.thunk_p)
4575 #if defined ENABLE_TREE_CHECKING && (GCC_VERSION >= 2007)
4576 if (!just_testing)
4577 lang_check_failed (__FILE__, __LINE__, __FUNCTION__);
4578 else
4579 #endif
4580 return NULL;
4583 ptr = &DECL_LANG_SPECIFIC (decl)->u.fn.u5.cloned_function;
4584 if (just_testing && *ptr == NULL_TREE)
4585 return NULL;
4586 else
4587 return ptr;
4590 /* Produce declarations for all appropriate clones of FN. If
4591 UPDATE_METHODS is true, the clones are added to the
4592 CLASSTYPE_MEMBER_VEC. */
4594 void
4595 clone_function_decl (tree fn, bool update_methods)
4597 tree clone;
4599 /* Avoid inappropriate cloning. */
4600 if (DECL_CHAIN (fn)
4601 && DECL_CLONED_FUNCTION_P (DECL_CHAIN (fn)))
4602 return;
4604 if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (fn))
4606 /* For each constructor, we need two variants: an in-charge version
4607 and a not-in-charge version. */
4608 clone = build_clone (fn, complete_ctor_identifier);
4609 if (update_methods)
4610 add_method (DECL_CONTEXT (clone), clone, false);
4611 clone = build_clone (fn, base_ctor_identifier);
4612 if (update_methods)
4613 add_method (DECL_CONTEXT (clone), clone, false);
4615 else
4617 gcc_assert (DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (fn));
4619 /* For each destructor, we need three variants: an in-charge
4620 version, a not-in-charge version, and an in-charge deleting
4621 version. We clone the deleting version first because that
4622 means it will go second on the TYPE_FIELDS list -- and that
4623 corresponds to the correct layout order in the virtual
4624 function table.
4626 For a non-virtual destructor, we do not build a deleting
4627 destructor. */
4628 if (DECL_VIRTUAL_P (fn))
4630 clone = build_clone (fn, deleting_dtor_identifier);
4631 if (update_methods)
4632 add_method (DECL_CONTEXT (clone), clone, false);
4634 clone = build_clone (fn, complete_dtor_identifier);
4635 if (update_methods)
4636 add_method (DECL_CONTEXT (clone), clone, false);
4637 clone = build_clone (fn, base_dtor_identifier);
4638 if (update_methods)
4639 add_method (DECL_CONTEXT (clone), clone, false);
4642 /* Note that this is an abstract function that is never emitted. */
4643 DECL_ABSTRACT_P (fn) = true;
4646 /* DECL is an in charge constructor, which is being defined. This will
4647 have had an in class declaration, from whence clones were
4648 declared. An out-of-class definition can specify additional default
4649 arguments. As it is the clones that are involved in overload
4650 resolution, we must propagate the information from the DECL to its
4651 clones. */
4653 void
4654 adjust_clone_args (tree decl)
4656 tree clone;
4658 for (clone = DECL_CHAIN (decl); clone && DECL_CLONED_FUNCTION_P (clone);
4659 clone = DECL_CHAIN (clone))
4661 tree orig_clone_parms = TYPE_ARG_TYPES (TREE_TYPE (clone));
4662 tree orig_decl_parms = TYPE_ARG_TYPES (TREE_TYPE (decl));
4663 tree decl_parms, clone_parms;
4665 clone_parms = orig_clone_parms;
4667 /* Skip the 'this' parameter. */
4668 orig_clone_parms = TREE_CHAIN (orig_clone_parms);
4669 orig_decl_parms = TREE_CHAIN (orig_decl_parms);
4671 if (DECL_HAS_IN_CHARGE_PARM_P (decl))
4672 orig_decl_parms = TREE_CHAIN (orig_decl_parms);
4673 if (DECL_HAS_VTT_PARM_P (decl))
4674 orig_decl_parms = TREE_CHAIN (orig_decl_parms);
4676 clone_parms = orig_clone_parms;
4677 if (DECL_HAS_VTT_PARM_P (clone))
4678 clone_parms = TREE_CHAIN (clone_parms);
4680 for (decl_parms = orig_decl_parms; decl_parms;
4681 decl_parms = TREE_CHAIN (decl_parms),
4682 clone_parms = TREE_CHAIN (clone_parms))
4684 if (clone_parms == void_list_node)
4686 gcc_assert (decl_parms == clone_parms
4687 || ctor_omit_inherited_parms (clone));
4688 break;
4691 gcc_assert (same_type_p (TREE_TYPE (decl_parms),
4692 TREE_TYPE (clone_parms)));
4694 if (TREE_PURPOSE (decl_parms) && !TREE_PURPOSE (clone_parms))
4696 /* A default parameter has been added. Adjust the
4697 clone's parameters. */
4698 tree exceptions = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (clone));
4699 tree attrs = TYPE_ATTRIBUTES (TREE_TYPE (clone));
4700 tree basetype = TYPE_METHOD_BASETYPE (TREE_TYPE (clone));
4701 tree type;
4703 clone_parms = orig_decl_parms;
4705 if (DECL_HAS_VTT_PARM_P (clone))
4707 clone_parms = tree_cons (TREE_PURPOSE (orig_clone_parms),
4708 TREE_VALUE (orig_clone_parms),
4709 clone_parms);
4710 TREE_TYPE (clone_parms) = TREE_TYPE (orig_clone_parms);
4712 type = build_method_type_directly (basetype,
4713 TREE_TYPE (TREE_TYPE (clone)),
4714 clone_parms);
4715 if (exceptions)
4716 type = build_exception_variant (type, exceptions);
4717 if (attrs)
4718 type = cp_build_type_attribute_variant (type, attrs);
4719 TREE_TYPE (clone) = type;
4721 clone_parms = NULL_TREE;
4722 break;
4725 gcc_assert (!clone_parms || clone_parms == void_list_node);
4729 /* For each of the constructors and destructors in T, create an
4730 in-charge and not-in-charge variant. */
4732 static void
4733 clone_constructors_and_destructors (tree t)
4735 /* While constructors can be via a using declaration, at this point
4736 we no longer need to know that. */
4737 for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (t)); iter; ++iter)
4738 clone_function_decl (*iter, /*update_methods=*/true);
4740 if (tree dtor = CLASSTYPE_DESTRUCTOR (t))
4741 clone_function_decl (dtor, /*update_methods=*/true);
4744 /* Deduce noexcept for a destructor DTOR. */
4746 void
4747 deduce_noexcept_on_destructor (tree dtor)
4749 if (!TYPE_RAISES_EXCEPTIONS (TREE_TYPE (dtor)))
4750 TREE_TYPE (dtor) = build_exception_variant (TREE_TYPE (dtor),
4751 noexcept_deferred_spec);
4754 /* Subroutine of set_one_vmethod_tm_attributes. Search base classes
4755 of TYPE for virtual functions which FNDECL overrides. Return a
4756 mask of the tm attributes found therein. */
4758 static int
4759 look_for_tm_attr_overrides (tree type, tree fndecl)
4761 tree binfo = TYPE_BINFO (type);
4762 tree base_binfo;
4763 int ix, found = 0;
4765 for (ix = 0; BINFO_BASE_ITERATE (binfo, ix, base_binfo); ++ix)
4767 tree o, basetype = BINFO_TYPE (base_binfo);
4769 if (!TYPE_POLYMORPHIC_P (basetype))
4770 continue;
4772 o = look_for_overrides_here (basetype, fndecl);
4773 if (o)
4775 if (lookup_attribute ("transaction_safe_dynamic",
4776 DECL_ATTRIBUTES (o)))
4777 /* transaction_safe_dynamic is not inherited. */;
4778 else
4779 found |= tm_attr_to_mask (find_tm_attribute
4780 (TYPE_ATTRIBUTES (TREE_TYPE (o))));
4782 else
4783 found |= look_for_tm_attr_overrides (basetype, fndecl);
4786 return found;
4789 /* Subroutine of set_method_tm_attributes. Handle the checks and
4790 inheritance for one virtual method FNDECL. */
4792 static void
4793 set_one_vmethod_tm_attributes (tree type, tree fndecl)
4795 tree tm_attr;
4796 int found, have;
4798 found = look_for_tm_attr_overrides (type, fndecl);
4800 /* If FNDECL doesn't actually override anything (i.e. T is the
4801 class that first declares FNDECL virtual), then we're done. */
4802 if (found == 0)
4803 return;
4805 tm_attr = find_tm_attribute (TYPE_ATTRIBUTES (TREE_TYPE (fndecl)));
4806 have = tm_attr_to_mask (tm_attr);
4808 /* Intel STM Language Extension 3.0, Section 4.2 table 4:
4809 tm_pure must match exactly, otherwise no weakening of
4810 tm_safe > tm_callable > nothing. */
4811 /* ??? The tm_pure attribute didn't make the transition to the
4812 multivendor language spec. */
4813 if (have == TM_ATTR_PURE)
4815 if (found != TM_ATTR_PURE)
4817 found &= -found;
4818 goto err_override;
4821 /* If the overridden function is tm_pure, then FNDECL must be. */
4822 else if (found == TM_ATTR_PURE && tm_attr)
4823 goto err_override;
4824 /* Look for base class combinations that cannot be satisfied. */
4825 else if (found != TM_ATTR_PURE && (found & TM_ATTR_PURE))
4827 found &= ~TM_ATTR_PURE;
4828 found &= -found;
4829 error_at (DECL_SOURCE_LOCATION (fndecl),
4830 "method overrides both %<transaction_pure%> and %qE methods",
4831 tm_mask_to_attr (found));
4833 /* If FNDECL did not declare an attribute, then inherit the most
4834 restrictive one. */
4835 else if (tm_attr == NULL)
4837 apply_tm_attr (fndecl, tm_mask_to_attr (least_bit_hwi (found)));
4839 /* Otherwise validate that we're not weaker than a function
4840 that is being overridden. */
4841 else
4843 found &= -found;
4844 if (found <= TM_ATTR_CALLABLE && have > found)
4845 goto err_override;
4847 return;
4849 err_override:
4850 error_at (DECL_SOURCE_LOCATION (fndecl),
4851 "method declared %qE overriding %qE method",
4852 tm_attr, tm_mask_to_attr (found));
4855 /* For each of the methods in T, propagate a class-level tm attribute. */
4857 static void
4858 set_method_tm_attributes (tree t)
4860 tree class_tm_attr, fndecl;
4862 /* Don't bother collecting tm attributes if transactional memory
4863 support is not enabled. */
4864 if (!flag_tm)
4865 return;
4867 /* Process virtual methods first, as they inherit directly from the
4868 base virtual function and also require validation of new attributes. */
4869 if (TYPE_CONTAINS_VPTR_P (t))
4871 tree vchain;
4872 for (vchain = BINFO_VIRTUALS (TYPE_BINFO (t)); vchain;
4873 vchain = TREE_CHAIN (vchain))
4875 fndecl = BV_FN (vchain);
4876 if (DECL_THUNK_P (fndecl))
4877 fndecl = THUNK_TARGET (fndecl);
4878 set_one_vmethod_tm_attributes (t, fndecl);
4882 /* If the class doesn't have an attribute, nothing more to do. */
4883 class_tm_attr = find_tm_attribute (TYPE_ATTRIBUTES (t));
4884 if (class_tm_attr == NULL)
4885 return;
4887 /* Any method that does not yet have a tm attribute inherits
4888 the one from the class. */
4889 for (fndecl = TYPE_FIELDS (t); fndecl; fndecl = DECL_CHAIN (fndecl))
4890 if (DECL_DECLARES_FUNCTION_P (fndecl)
4891 && !find_tm_attribute (TYPE_ATTRIBUTES (TREE_TYPE (fndecl))))
4892 apply_tm_attr (fndecl, class_tm_attr);
4895 /* Returns true if FN is a default constructor. */
4897 bool
4898 default_ctor_p (tree fn)
4900 return (DECL_CONSTRUCTOR_P (fn)
4901 && sufficient_parms_p (FUNCTION_FIRST_USER_PARMTYPE (fn)));
4904 /* Returns true iff class T has a user-defined constructor that can be called
4905 with more than zero arguments. */
4907 bool
4908 type_has_user_nondefault_constructor (tree t)
4910 if (!TYPE_HAS_USER_CONSTRUCTOR (t))
4911 return false;
4913 for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (t)); iter; ++iter)
4915 tree fn = *iter;
4916 if (!DECL_ARTIFICIAL (fn)
4917 && (TREE_CODE (fn) == TEMPLATE_DECL
4918 || (skip_artificial_parms_for (fn, DECL_ARGUMENTS (fn))
4919 != NULL_TREE)))
4920 return true;
4923 return false;
4926 /* Returns the defaulted constructor if T has one. Otherwise, returns
4927 NULL_TREE. */
4929 tree
4930 in_class_defaulted_default_constructor (tree t)
4932 if (!TYPE_HAS_USER_CONSTRUCTOR (t))
4933 return NULL_TREE;
4935 for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (t)); iter; ++iter)
4937 tree fn = *iter;
4939 if (DECL_DEFAULTED_IN_CLASS_P (fn)
4940 && default_ctor_p (fn))
4941 return fn;
4944 return NULL_TREE;
4947 /* Returns true iff FN is a user-provided function, i.e. user-declared
4948 and not defaulted at its first declaration. */
4950 bool
4951 user_provided_p (tree fn)
4953 if (TREE_CODE (fn) == TEMPLATE_DECL)
4954 return true;
4955 else
4956 return (!DECL_ARTIFICIAL (fn)
4957 && !(DECL_INITIALIZED_IN_CLASS_P (fn)
4958 && (DECL_DEFAULTED_FN (fn) || DECL_DELETED_FN (fn))));
4961 /* Returns true iff class T has a user-provided constructor. */
4963 bool
4964 type_has_user_provided_constructor (tree t)
4966 if (!CLASS_TYPE_P (t))
4967 return false;
4969 if (!TYPE_HAS_USER_CONSTRUCTOR (t))
4970 return false;
4972 for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (t)); iter; ++iter)
4973 if (user_provided_p (*iter))
4974 return true;
4976 return false;
4979 /* Returns true iff class T has a user-provided or explicit constructor. */
4981 bool
4982 type_has_user_provided_or_explicit_constructor (tree t)
4984 if (!CLASS_TYPE_P (t))
4985 return false;
4987 if (!TYPE_HAS_USER_CONSTRUCTOR (t))
4988 return false;
4990 for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (t)); iter; ++iter)
4992 tree fn = *iter;
4993 if (user_provided_p (fn) || DECL_NONCONVERTING_P (fn))
4994 return true;
4997 return false;
5000 /* Returns true iff class T has a non-user-provided (i.e. implicitly
5001 declared or explicitly defaulted in the class body) default
5002 constructor. */
5004 bool
5005 type_has_non_user_provided_default_constructor (tree t)
5007 if (!TYPE_HAS_DEFAULT_CONSTRUCTOR (t))
5008 return false;
5009 if (CLASSTYPE_LAZY_DEFAULT_CTOR (t))
5010 return true;
5012 for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (t)); iter; ++iter)
5014 tree fn = *iter;
5015 if (TREE_CODE (fn) == FUNCTION_DECL
5016 && default_ctor_p (fn)
5017 && !user_provided_p (fn))
5018 return true;
5021 return false;
5024 /* TYPE is being used as a virtual base, and has a non-trivial move
5025 assignment. Return true if this is due to there being a user-provided
5026 move assignment in TYPE or one of its subobjects; if there isn't, then
5027 multiple move assignment can't cause any harm. */
5029 bool
5030 vbase_has_user_provided_move_assign (tree type)
5032 /* Does the type itself have a user-provided move assignment operator? */
5033 if (!CLASSTYPE_LAZY_MOVE_ASSIGN (type))
5034 for (ovl_iterator iter (get_class_binding_direct
5035 (type, assign_op_identifier));
5036 iter; ++iter)
5037 if (!DECL_ARTIFICIAL (*iter) && move_fn_p (*iter))
5038 return true;
5040 /* Do any of its bases? */
5041 tree binfo = TYPE_BINFO (type);
5042 tree base_binfo;
5043 for (int i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
5044 if (vbase_has_user_provided_move_assign (BINFO_TYPE (base_binfo)))
5045 return true;
5047 /* Or non-static data members? */
5048 for (tree field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
5050 if (TREE_CODE (field) == FIELD_DECL
5051 && CLASS_TYPE_P (TREE_TYPE (field))
5052 && vbase_has_user_provided_move_assign (TREE_TYPE (field)))
5053 return true;
5056 /* Seems not. */
5057 return false;
5060 /* If default-initialization leaves part of TYPE uninitialized, returns
5061 a DECL for the field or TYPE itself (DR 253). */
5063 tree
5064 default_init_uninitialized_part (tree type)
5066 tree t, r, binfo;
5067 int i;
5069 type = strip_array_types (type);
5070 if (!CLASS_TYPE_P (type))
5071 return type;
5072 if (!type_has_non_user_provided_default_constructor (type))
5073 return NULL_TREE;
5074 for (binfo = TYPE_BINFO (type), i = 0;
5075 BINFO_BASE_ITERATE (binfo, i, t); ++i)
5077 r = default_init_uninitialized_part (BINFO_TYPE (t));
5078 if (r)
5079 return r;
5081 for (t = TYPE_FIELDS (type); t; t = DECL_CHAIN (t))
5082 if (TREE_CODE (t) == FIELD_DECL
5083 && !DECL_ARTIFICIAL (t)
5084 && !DECL_INITIAL (t))
5086 r = default_init_uninitialized_part (TREE_TYPE (t));
5087 if (r)
5088 return DECL_P (r) ? r : t;
5091 return NULL_TREE;
5094 /* Returns true iff for class T, a trivial synthesized default constructor
5095 would be constexpr. */
5097 bool
5098 trivial_default_constructor_is_constexpr (tree t)
5100 /* A defaulted trivial default constructor is constexpr
5101 if there is nothing to initialize. */
5102 gcc_assert (!TYPE_HAS_COMPLEX_DFLT (t));
5103 return is_really_empty_class (t);
5106 /* Returns true iff class T has a constexpr default constructor. */
5108 bool
5109 type_has_constexpr_default_constructor (tree t)
5111 tree fns;
5113 if (!CLASS_TYPE_P (t))
5115 /* The caller should have stripped an enclosing array. */
5116 gcc_assert (TREE_CODE (t) != ARRAY_TYPE);
5117 return false;
5119 if (CLASSTYPE_LAZY_DEFAULT_CTOR (t))
5121 if (!TYPE_HAS_COMPLEX_DFLT (t))
5122 return trivial_default_constructor_is_constexpr (t);
5123 /* Non-trivial, we need to check subobject constructors. */
5124 lazily_declare_fn (sfk_constructor, t);
5126 fns = locate_ctor (t);
5127 return (fns && DECL_DECLARED_CONSTEXPR_P (fns));
5130 /* Returns true iff class T has a constexpr default constructor or has an
5131 implicitly declared default constructor that we can't tell if it's constexpr
5132 without forcing a lazy declaration (which might cause undesired
5133 instantiations). */
5135 bool
5136 type_maybe_constexpr_default_constructor (tree t)
5138 if (CLASS_TYPE_P (t) && CLASSTYPE_LAZY_DEFAULT_CTOR (t)
5139 && TYPE_HAS_COMPLEX_DFLT (t))
5140 /* Assume it's constexpr. */
5141 return true;
5142 return type_has_constexpr_default_constructor (t);
5145 /* Returns true iff class TYPE has a virtual destructor. */
5147 bool
5148 type_has_virtual_destructor (tree type)
5150 tree dtor;
5152 if (!CLASS_TYPE_P (type))
5153 return false;
5155 gcc_assert (COMPLETE_TYPE_P (type));
5156 dtor = CLASSTYPE_DESTRUCTOR (type);
5157 return (dtor && DECL_VIRTUAL_P (dtor));
5160 /* Returns true iff T, a class, has a move-assignment or
5161 move-constructor. Does not lazily declare either.
5162 If USER_P is false, any move function will do. If it is true, the
5163 move function must be user-declared.
5165 Note that user-declared here is different from "user-provided",
5166 which doesn't include functions that are defaulted in the
5167 class. */
5169 bool
5170 classtype_has_move_assign_or_move_ctor_p (tree t, bool user_p)
5172 gcc_assert (user_p
5173 || (!CLASSTYPE_LAZY_MOVE_CTOR (t)
5174 && !CLASSTYPE_LAZY_MOVE_ASSIGN (t)));
5176 if (!CLASSTYPE_LAZY_MOVE_CTOR (t))
5177 for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (t)); iter; ++iter)
5178 if ((!user_p || !DECL_ARTIFICIAL (*iter)) && move_fn_p (*iter))
5179 return true;
5181 if (!CLASSTYPE_LAZY_MOVE_ASSIGN (t))
5182 for (ovl_iterator iter (get_class_binding_direct
5183 (t, assign_op_identifier));
5184 iter; ++iter)
5185 if ((!user_p || !DECL_ARTIFICIAL (*iter)) && move_fn_p (*iter))
5186 return true;
5188 return false;
5191 /* Nonzero if we need to build up a constructor call when initializing an
5192 object of this class, either because it has a user-declared constructor
5193 or because it doesn't have a default constructor (so we need to give an
5194 error if no initializer is provided). Use TYPE_NEEDS_CONSTRUCTING when
5195 what you care about is whether or not an object can be produced by a
5196 constructor (e.g. so we don't set TREE_READONLY on const variables of
5197 such type); use this function when what you care about is whether or not
5198 to try to call a constructor to create an object. The latter case is
5199 the former plus some cases of constructors that cannot be called. */
5201 bool
5202 type_build_ctor_call (tree t)
5204 tree inner;
5205 if (TYPE_NEEDS_CONSTRUCTING (t))
5206 return true;
5207 inner = strip_array_types (t);
5208 if (!CLASS_TYPE_P (inner) || ANON_AGGR_TYPE_P (inner))
5209 return false;
5210 if (!TYPE_HAS_DEFAULT_CONSTRUCTOR (inner))
5211 return true;
5212 if (cxx_dialect < cxx11)
5213 return false;
5214 /* A user-declared constructor might be private, and a constructor might
5215 be trivial but deleted. */
5216 for (ovl_iterator iter (get_class_binding (inner, complete_ctor_identifier));
5217 iter; ++iter)
5219 tree fn = *iter;
5220 if (!DECL_ARTIFICIAL (fn)
5221 || DECL_DELETED_FN (fn))
5222 return true;
5224 return false;
5227 /* Like type_build_ctor_call, but for destructors. */
5229 bool
5230 type_build_dtor_call (tree t)
5232 tree inner;
5233 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t))
5234 return true;
5235 inner = strip_array_types (t);
5236 if (!CLASS_TYPE_P (inner) || ANON_AGGR_TYPE_P (inner)
5237 || !COMPLETE_TYPE_P (inner))
5238 return false;
5239 if (cxx_dialect < cxx11)
5240 return false;
5241 /* A user-declared destructor might be private, and a destructor might
5242 be trivial but deleted. */
5243 for (ovl_iterator iter (get_class_binding (inner, complete_dtor_identifier));
5244 iter; ++iter)
5246 tree fn = *iter;
5247 if (!DECL_ARTIFICIAL (fn)
5248 || DECL_DELETED_FN (fn))
5249 return true;
5251 return false;
5254 /* Remove all zero-width bit-fields from T. */
5256 static void
5257 remove_zero_width_bit_fields (tree t)
5259 tree *fieldsp;
5261 fieldsp = &TYPE_FIELDS (t);
5262 while (*fieldsp)
5264 if (TREE_CODE (*fieldsp) == FIELD_DECL
5265 && DECL_C_BIT_FIELD (*fieldsp)
5266 /* We should not be confused by the fact that grokbitfield
5267 temporarily sets the width of the bit field into
5268 DECL_BIT_FIELD_REPRESENTATIVE (*fieldsp).
5269 check_bitfield_decl eventually sets DECL_SIZE (*fieldsp)
5270 to that width. */
5271 && (DECL_SIZE (*fieldsp) == NULL_TREE
5272 || integer_zerop (DECL_SIZE (*fieldsp))))
5273 *fieldsp = DECL_CHAIN (*fieldsp);
5274 else
5275 fieldsp = &DECL_CHAIN (*fieldsp);
5279 /* Returns TRUE iff we need a cookie when dynamically allocating an
5280 array whose elements have the indicated class TYPE. */
5282 static bool
5283 type_requires_array_cookie (tree type)
5285 tree fns;
5286 bool has_two_argument_delete_p = false;
5288 gcc_assert (CLASS_TYPE_P (type));
5290 /* If there's a non-trivial destructor, we need a cookie. In order
5291 to iterate through the array calling the destructor for each
5292 element, we'll have to know how many elements there are. */
5293 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
5294 return true;
5296 /* If the usual deallocation function is a two-argument whose second
5297 argument is of type `size_t', then we have to pass the size of
5298 the array to the deallocation function, so we will need to store
5299 a cookie. */
5300 fns = lookup_fnfields (TYPE_BINFO (type),
5301 ovl_op_identifier (false, VEC_DELETE_EXPR),
5302 /*protect=*/0);
5303 /* If there are no `operator []' members, or the lookup is
5304 ambiguous, then we don't need a cookie. */
5305 if (!fns || fns == error_mark_node)
5306 return false;
5307 /* Loop through all of the functions. */
5308 for (lkp_iterator iter (BASELINK_FUNCTIONS (fns)); iter; ++iter)
5310 tree fn = *iter;
5312 /* See if this function is a one-argument delete function. If
5313 it is, then it will be the usual deallocation function. */
5314 tree second_parm = TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (fn)));
5315 if (second_parm == void_list_node)
5316 return false;
5317 /* Do not consider this function if its second argument is an
5318 ellipsis. */
5319 if (!second_parm)
5320 continue;
5321 /* Otherwise, if we have a two-argument function and the second
5322 argument is `size_t', it will be the usual deallocation
5323 function -- unless there is one-argument function, too. */
5324 if (TREE_CHAIN (second_parm) == void_list_node
5325 && same_type_p (TREE_VALUE (second_parm), size_type_node))
5326 has_two_argument_delete_p = true;
5329 return has_two_argument_delete_p;
5332 /* Finish computing the `literal type' property of class type T.
5334 At this point, we have already processed base classes and
5335 non-static data members. We need to check whether the copy
5336 constructor is trivial, the destructor is trivial, and there
5337 is a trivial default constructor or at least one constexpr
5338 constructor other than the copy constructor. */
5340 static void
5341 finalize_literal_type_property (tree t)
5343 tree fn;
5345 if (cxx_dialect < cxx11
5346 || TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t))
5347 CLASSTYPE_LITERAL_P (t) = false;
5348 else if (CLASSTYPE_LITERAL_P (t) && LAMBDA_TYPE_P (t))
5349 CLASSTYPE_LITERAL_P (t) = (cxx_dialect >= cxx17);
5350 else if (CLASSTYPE_LITERAL_P (t) && !TYPE_HAS_TRIVIAL_DFLT (t)
5351 && CLASSTYPE_NON_AGGREGATE (t)
5352 && !TYPE_HAS_CONSTEXPR_CTOR (t))
5353 CLASSTYPE_LITERAL_P (t) = false;
5355 /* C++14 DR 1684 removed this restriction. */
5356 if (cxx_dialect < cxx14
5357 && !CLASSTYPE_LITERAL_P (t) && !LAMBDA_TYPE_P (t))
5358 for (fn = TYPE_FIELDS (t); fn; fn = DECL_CHAIN (fn))
5359 if (TREE_CODE (fn) == FUNCTION_DECL
5360 && DECL_DECLARED_CONSTEXPR_P (fn)
5361 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
5362 && !DECL_CONSTRUCTOR_P (fn))
5364 DECL_DECLARED_CONSTEXPR_P (fn) = false;
5365 if (!DECL_GENERATED_P (fn)
5366 && pedwarn (DECL_SOURCE_LOCATION (fn), OPT_Wpedantic,
5367 "enclosing class of %<constexpr%> non-static member "
5368 "function %q+#D is not a literal type", fn))
5369 explain_non_literal_class (t);
5373 /* T is a non-literal type used in a context which requires a constant
5374 expression. Explain why it isn't literal. */
5376 void
5377 explain_non_literal_class (tree t)
5379 static hash_set<tree> *diagnosed;
5381 if (!CLASS_TYPE_P (t))
5382 return;
5383 t = TYPE_MAIN_VARIANT (t);
5385 if (diagnosed == NULL)
5386 diagnosed = new hash_set<tree>;
5387 if (diagnosed->add (t))
5388 /* Already explained. */
5389 return;
5391 inform (UNKNOWN_LOCATION, "%q+T is not literal because:", t);
5392 if (cxx_dialect < cxx17 && LAMBDA_TYPE_P (t))
5393 inform (UNKNOWN_LOCATION,
5394 " %qT is a closure type, which is only literal in "
5395 "C++17 and later", t);
5396 else if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t))
5397 inform (UNKNOWN_LOCATION, " %q+T has a non-trivial destructor", t);
5398 else if (CLASSTYPE_NON_AGGREGATE (t)
5399 && !TYPE_HAS_TRIVIAL_DFLT (t)
5400 && !LAMBDA_TYPE_P (t)
5401 && !TYPE_HAS_CONSTEXPR_CTOR (t))
5403 inform (UNKNOWN_LOCATION,
5404 " %q+T is not an aggregate, does not have a trivial "
5405 "default constructor, and has no %<constexpr%> constructor that "
5406 "is not a copy or move constructor", t);
5407 if (type_has_non_user_provided_default_constructor (t))
5408 /* Note that we can't simply call locate_ctor because when the
5409 constructor is deleted it just returns NULL_TREE. */
5410 for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (t)); iter; ++iter)
5412 tree fn = *iter;
5413 tree parms = TYPE_ARG_TYPES (TREE_TYPE (fn));
5415 parms = skip_artificial_parms_for (fn, parms);
5417 if (sufficient_parms_p (parms))
5419 if (DECL_DELETED_FN (fn))
5420 maybe_explain_implicit_delete (fn);
5421 else
5422 explain_invalid_constexpr_fn (fn);
5423 break;
5427 else
5429 tree binfo, base_binfo, field; int i;
5430 for (binfo = TYPE_BINFO (t), i = 0;
5431 BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
5433 tree basetype = TREE_TYPE (base_binfo);
5434 if (!CLASSTYPE_LITERAL_P (basetype))
5436 inform (UNKNOWN_LOCATION,
5437 " base class %qT of %q+T is non-literal",
5438 basetype, t);
5439 explain_non_literal_class (basetype);
5440 return;
5443 for (field = TYPE_FIELDS (t); field; field = TREE_CHAIN (field))
5445 tree ftype;
5446 if (TREE_CODE (field) != FIELD_DECL)
5447 continue;
5448 ftype = TREE_TYPE (field);
5449 if (!literal_type_p (ftype))
5451 inform (DECL_SOURCE_LOCATION (field),
5452 " non-static data member %qD has non-literal type",
5453 field);
5454 if (CLASS_TYPE_P (ftype))
5455 explain_non_literal_class (ftype);
5457 if (CP_TYPE_VOLATILE_P (ftype))
5458 inform (DECL_SOURCE_LOCATION (field),
5459 " non-static data member %qD has volatile type", field);
5464 /* Check the validity of the bases and members declared in T. Add any
5465 implicitly-generated functions (like copy-constructors and
5466 assignment operators). Compute various flag bits (like
5467 CLASSTYPE_NON_LAYOUT_POD_T) for T. This routine works purely at the C++
5468 level: i.e., independently of the ABI in use. */
5470 static void
5471 check_bases_and_members (tree t)
5473 /* Nonzero if the implicitly generated copy constructor should take
5474 a non-const reference argument. */
5475 int cant_have_const_ctor;
5476 /* Nonzero if the implicitly generated assignment operator
5477 should take a non-const reference argument. */
5478 int no_const_asn_ref;
5479 tree access_decls;
5480 bool saved_complex_asn_ref;
5481 bool saved_nontrivial_dtor;
5482 tree fn;
5484 /* By default, we use const reference arguments and generate default
5485 constructors. */
5486 cant_have_const_ctor = 0;
5487 no_const_asn_ref = 0;
5489 /* Check all the base-classes and set FMEM members to point to arrays
5490 of potential interest. */
5491 check_bases (t, &cant_have_const_ctor, &no_const_asn_ref);
5493 /* Deduce noexcept on destructor. This needs to happen after we've set
5494 triviality flags appropriately for our bases. */
5495 if (cxx_dialect >= cxx11)
5496 if (tree dtor = CLASSTYPE_DESTRUCTOR (t))
5497 deduce_noexcept_on_destructor (dtor);
5499 /* Check all the method declarations. */
5500 check_methods (t);
5502 /* Save the initial values of these flags which only indicate whether
5503 or not the class has user-provided functions. As we analyze the
5504 bases and members we can set these flags for other reasons. */
5505 saved_complex_asn_ref = TYPE_HAS_COMPLEX_COPY_ASSIGN (t);
5506 saved_nontrivial_dtor = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t);
5508 /* Check all the data member declarations. We cannot call
5509 check_field_decls until we have called check_bases check_methods,
5510 as check_field_decls depends on TYPE_HAS_NONTRIVIAL_DESTRUCTOR
5511 being set appropriately. */
5512 check_field_decls (t, &access_decls,
5513 &cant_have_const_ctor,
5514 &no_const_asn_ref);
5516 /* A nearly-empty class has to be vptr-containing; a nearly empty
5517 class contains just a vptr. */
5518 if (!TYPE_CONTAINS_VPTR_P (t))
5519 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
5521 /* Do some bookkeeping that will guide the generation of implicitly
5522 declared member functions. */
5523 TYPE_HAS_COMPLEX_COPY_CTOR (t) |= TYPE_CONTAINS_VPTR_P (t);
5524 TYPE_HAS_COMPLEX_MOVE_CTOR (t) |= TYPE_CONTAINS_VPTR_P (t);
5525 /* We need to call a constructor for this class if it has a
5526 user-provided constructor, or if the default constructor is going
5527 to initialize the vptr. (This is not an if-and-only-if;
5528 TYPE_NEEDS_CONSTRUCTING is set elsewhere if bases or members
5529 themselves need constructing.) */
5530 TYPE_NEEDS_CONSTRUCTING (t)
5531 |= (type_has_user_provided_constructor (t) || TYPE_CONTAINS_VPTR_P (t));
5532 /* [dcl.init.aggr]
5534 An aggregate is an array or a class with no user-provided
5535 constructors ... and no virtual functions.
5537 Again, other conditions for being an aggregate are checked
5538 elsewhere. */
5539 CLASSTYPE_NON_AGGREGATE (t)
5540 |= (type_has_user_provided_or_explicit_constructor (t)
5541 || TYPE_POLYMORPHIC_P (t));
5542 /* This is the C++98/03 definition of POD; it changed in C++0x, but we
5543 retain the old definition internally for ABI reasons. */
5544 CLASSTYPE_NON_LAYOUT_POD_P (t)
5545 |= (CLASSTYPE_NON_AGGREGATE (t)
5546 || saved_nontrivial_dtor || saved_complex_asn_ref);
5547 CLASSTYPE_NON_STD_LAYOUT (t) |= TYPE_CONTAINS_VPTR_P (t);
5548 TYPE_HAS_COMPLEX_COPY_ASSIGN (t) |= TYPE_CONTAINS_VPTR_P (t);
5549 TYPE_HAS_COMPLEX_MOVE_ASSIGN (t) |= TYPE_CONTAINS_VPTR_P (t);
5550 TYPE_HAS_COMPLEX_DFLT (t) |= TYPE_CONTAINS_VPTR_P (t);
5552 /* If the only explicitly declared default constructor is user-provided,
5553 set TYPE_HAS_COMPLEX_DFLT. */
5554 if (!TYPE_HAS_COMPLEX_DFLT (t)
5555 && TYPE_HAS_DEFAULT_CONSTRUCTOR (t)
5556 && !type_has_non_user_provided_default_constructor (t))
5557 TYPE_HAS_COMPLEX_DFLT (t) = true;
5559 /* Warn if a public base of a polymorphic type has an accessible
5560 non-virtual destructor. It is only now that we know the class is
5561 polymorphic. Although a polymorphic base will have a already
5562 been diagnosed during its definition, we warn on use too. */
5563 if (TYPE_POLYMORPHIC_P (t) && warn_nonvdtor)
5565 tree binfo = TYPE_BINFO (t);
5566 vec<tree, va_gc> *accesses = BINFO_BASE_ACCESSES (binfo);
5567 tree base_binfo;
5568 unsigned i;
5570 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
5572 tree basetype = TREE_TYPE (base_binfo);
5574 if ((*accesses)[i] == access_public_node
5575 && (TYPE_POLYMORPHIC_P (basetype) || warn_ecpp)
5576 && accessible_nvdtor_p (basetype))
5577 warning (OPT_Wnon_virtual_dtor,
5578 "base class %q#T has accessible non-virtual destructor",
5579 basetype);
5583 /* If the class has no user-declared constructor, but does have
5584 non-static const or reference data members that can never be
5585 initialized, issue a warning. */
5586 if (warn_uninitialized
5587 /* Classes with user-declared constructors are presumed to
5588 initialize these members. */
5589 && !TYPE_HAS_USER_CONSTRUCTOR (t)
5590 /* Aggregates can be initialized with brace-enclosed
5591 initializers. */
5592 && CLASSTYPE_NON_AGGREGATE (t))
5594 tree field;
5596 for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
5598 tree type;
5600 if (TREE_CODE (field) != FIELD_DECL
5601 || DECL_INITIAL (field) != NULL_TREE)
5602 continue;
5604 type = TREE_TYPE (field);
5605 if (TREE_CODE (type) == REFERENCE_TYPE)
5606 warning_at (DECL_SOURCE_LOCATION (field),
5607 OPT_Wuninitialized, "non-static reference %q#D "
5608 "in class without a constructor", field);
5609 else if (CP_TYPE_CONST_P (type)
5610 && (!CLASS_TYPE_P (type)
5611 || !TYPE_HAS_DEFAULT_CONSTRUCTOR (type)))
5612 warning_at (DECL_SOURCE_LOCATION (field),
5613 OPT_Wuninitialized, "non-static const member %q#D "
5614 "in class without a constructor", field);
5618 /* Synthesize any needed methods. */
5619 add_implicitly_declared_members (t, &access_decls,
5620 cant_have_const_ctor,
5621 no_const_asn_ref);
5623 /* Check defaulted declarations here so we have cant_have_const_ctor
5624 and don't need to worry about clones. */
5625 for (fn = TYPE_FIELDS (t); fn; fn = DECL_CHAIN (fn))
5626 if (DECL_DECLARES_FUNCTION_P (fn)
5627 && !DECL_ARTIFICIAL (fn)
5628 && DECL_DEFAULTED_IN_CLASS_P (fn))
5630 int copy = copy_fn_p (fn);
5631 if (copy > 0)
5633 bool imp_const_p
5634 = (DECL_CONSTRUCTOR_P (fn) ? !cant_have_const_ctor
5635 : !no_const_asn_ref);
5636 bool fn_const_p = (copy == 2);
5638 if (fn_const_p && !imp_const_p)
5639 /* If the function is defaulted outside the class, we just
5640 give the synthesis error. */
5641 error ("%q+D declared to take const reference, but implicit "
5642 "declaration would take non-const", fn);
5644 defaulted_late_check (fn);
5647 if (LAMBDA_TYPE_P (t))
5649 /* "This class type is not an aggregate." */
5650 CLASSTYPE_NON_AGGREGATE (t) = 1;
5653 /* Compute the 'literal type' property before we
5654 do anything with non-static member functions. */
5655 finalize_literal_type_property (t);
5657 /* Create the in-charge and not-in-charge variants of constructors
5658 and destructors. */
5659 clone_constructors_and_destructors (t);
5661 /* Process the using-declarations. */
5662 for (; access_decls; access_decls = TREE_CHAIN (access_decls))
5663 handle_using_decl (TREE_VALUE (access_decls), t);
5665 /* Figure out whether or not we will need a cookie when dynamically
5666 allocating an array of this type. */
5667 LANG_TYPE_CLASS_CHECK (t)->vec_new_uses_cookie
5668 = type_requires_array_cookie (t);
5671 /* If T needs a pointer to its virtual function table, set TYPE_VFIELD
5672 accordingly. If a new vfield was created (because T doesn't have a
5673 primary base class), then the newly created field is returned. It
5674 is not added to the TYPE_FIELDS list; it is the caller's
5675 responsibility to do that. Accumulate declared virtual functions
5676 on VIRTUALS_P. */
5678 static tree
5679 create_vtable_ptr (tree t, tree* virtuals_p)
5681 tree fn;
5683 /* Collect the virtual functions declared in T. */
5684 for (fn = TYPE_FIELDS (t); fn; fn = DECL_CHAIN (fn))
5685 if (TREE_CODE (fn) == FUNCTION_DECL
5686 && DECL_VINDEX (fn) && !DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (fn)
5687 && TREE_CODE (DECL_VINDEX (fn)) != INTEGER_CST)
5689 tree new_virtual = make_node (TREE_LIST);
5691 BV_FN (new_virtual) = fn;
5692 BV_DELTA (new_virtual) = integer_zero_node;
5693 BV_VCALL_INDEX (new_virtual) = NULL_TREE;
5695 TREE_CHAIN (new_virtual) = *virtuals_p;
5696 *virtuals_p = new_virtual;
5699 /* If we couldn't find an appropriate base class, create a new field
5700 here. Even if there weren't any new virtual functions, we might need a
5701 new virtual function table if we're supposed to include vptrs in
5702 all classes that need them. */
5703 if (!TYPE_VFIELD (t) && (*virtuals_p || TYPE_CONTAINS_VPTR_P (t)))
5705 /* We build this decl with vtbl_ptr_type_node, which is a
5706 `vtable_entry_type*'. It might seem more precise to use
5707 `vtable_entry_type (*)[N]' where N is the number of virtual
5708 functions. However, that would require the vtable pointer in
5709 base classes to have a different type than the vtable pointer
5710 in derived classes. We could make that happen, but that
5711 still wouldn't solve all the problems. In particular, the
5712 type-based alias analysis code would decide that assignments
5713 to the base class vtable pointer can't alias assignments to
5714 the derived class vtable pointer, since they have different
5715 types. Thus, in a derived class destructor, where the base
5716 class constructor was inlined, we could generate bad code for
5717 setting up the vtable pointer.
5719 Therefore, we use one type for all vtable pointers. We still
5720 use a type-correct type; it's just doesn't indicate the array
5721 bounds. That's better than using `void*' or some such; it's
5722 cleaner, and it let's the alias analysis code know that these
5723 stores cannot alias stores to void*! */
5724 tree field;
5726 field = build_decl (input_location,
5727 FIELD_DECL, get_vfield_name (t), vtbl_ptr_type_node);
5728 DECL_VIRTUAL_P (field) = 1;
5729 DECL_ARTIFICIAL (field) = 1;
5730 DECL_FIELD_CONTEXT (field) = t;
5731 DECL_FCONTEXT (field) = t;
5732 if (TYPE_PACKED (t))
5733 DECL_PACKED (field) = 1;
5735 TYPE_VFIELD (t) = field;
5737 /* This class is non-empty. */
5738 CLASSTYPE_EMPTY_P (t) = 0;
5740 return field;
5743 return NULL_TREE;
5746 /* Add OFFSET to all base types of BINFO which is a base in the
5747 hierarchy dominated by T.
5749 OFFSET, which is a type offset, is number of bytes. */
5751 static void
5752 propagate_binfo_offsets (tree binfo, tree offset)
5754 int i;
5755 tree primary_binfo;
5756 tree base_binfo;
5758 /* Update BINFO's offset. */
5759 BINFO_OFFSET (binfo)
5760 = fold_convert (sizetype,
5761 size_binop (PLUS_EXPR,
5762 fold_convert (ssizetype, BINFO_OFFSET (binfo)),
5763 offset));
5765 /* Find the primary base class. */
5766 primary_binfo = get_primary_binfo (binfo);
5768 if (primary_binfo && BINFO_INHERITANCE_CHAIN (primary_binfo) == binfo)
5769 propagate_binfo_offsets (primary_binfo, offset);
5771 /* Scan all of the bases, pushing the BINFO_OFFSET adjust
5772 downwards. */
5773 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
5775 /* Don't do the primary base twice. */
5776 if (base_binfo == primary_binfo)
5777 continue;
5779 if (BINFO_VIRTUAL_P (base_binfo))
5780 continue;
5782 propagate_binfo_offsets (base_binfo, offset);
5786 /* Set BINFO_OFFSET for all of the virtual bases for RLI->T. Update
5787 TYPE_ALIGN and TYPE_SIZE for T. OFFSETS gives the location of
5788 empty subobjects of T. */
5790 static void
5791 layout_virtual_bases (record_layout_info rli, splay_tree offsets)
5793 tree vbase;
5794 tree t = rli->t;
5795 tree *next_field;
5797 if (BINFO_N_BASE_BINFOS (TYPE_BINFO (t)) == 0)
5798 return;
5800 /* Find the last field. The artificial fields created for virtual
5801 bases will go after the last extant field to date. */
5802 next_field = &TYPE_FIELDS (t);
5803 while (*next_field)
5804 next_field = &DECL_CHAIN (*next_field);
5806 /* Go through the virtual bases, allocating space for each virtual
5807 base that is not already a primary base class. These are
5808 allocated in inheritance graph order. */
5809 for (vbase = TYPE_BINFO (t); vbase; vbase = TREE_CHAIN (vbase))
5811 if (!BINFO_VIRTUAL_P (vbase))
5812 continue;
5814 if (!BINFO_PRIMARY_P (vbase))
5816 /* This virtual base is not a primary base of any class in the
5817 hierarchy, so we have to add space for it. */
5818 next_field = build_base_field (rli, vbase,
5819 offsets, next_field);
5824 /* Returns the offset of the byte just past the end of the base class
5825 BINFO. */
5827 static tree
5828 end_of_base (tree binfo)
5830 tree size;
5832 if (!CLASSTYPE_AS_BASE (BINFO_TYPE (binfo)))
5833 size = TYPE_SIZE_UNIT (char_type_node);
5834 else if (is_empty_class (BINFO_TYPE (binfo)))
5835 /* An empty class has zero CLASSTYPE_SIZE_UNIT, but we need to
5836 allocate some space for it. It cannot have virtual bases, so
5837 TYPE_SIZE_UNIT is fine. */
5838 size = TYPE_SIZE_UNIT (BINFO_TYPE (binfo));
5839 else
5840 size = CLASSTYPE_SIZE_UNIT (BINFO_TYPE (binfo));
5842 return size_binop (PLUS_EXPR, BINFO_OFFSET (binfo), size);
5845 /* Returns the offset of the byte just past the end of the base class
5846 with the highest offset in T. If INCLUDE_VIRTUALS_P is zero, then
5847 only non-virtual bases are included. */
5849 static tree
5850 end_of_class (tree t, int include_virtuals_p)
5852 tree result = size_zero_node;
5853 vec<tree, va_gc> *vbases;
5854 tree binfo;
5855 tree base_binfo;
5856 tree offset;
5857 int i;
5859 for (binfo = TYPE_BINFO (t), i = 0;
5860 BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
5862 if (!include_virtuals_p
5863 && BINFO_VIRTUAL_P (base_binfo)
5864 && (!BINFO_PRIMARY_P (base_binfo)
5865 || BINFO_INHERITANCE_CHAIN (base_binfo) != TYPE_BINFO (t)))
5866 continue;
5868 offset = end_of_base (base_binfo);
5869 if (tree_int_cst_lt (result, offset))
5870 result = offset;
5873 if (include_virtuals_p)
5874 for (vbases = CLASSTYPE_VBASECLASSES (t), i = 0;
5875 vec_safe_iterate (vbases, i, &base_binfo); i++)
5877 offset = end_of_base (base_binfo);
5878 if (tree_int_cst_lt (result, offset))
5879 result = offset;
5882 return result;
5885 /* Warn about bases of T that are inaccessible because they are
5886 ambiguous. For example:
5888 struct S {};
5889 struct T : public S {};
5890 struct U : public S, public T {};
5892 Here, `(S*) new U' is not allowed because there are two `S'
5893 subobjects of U. */
5895 static void
5896 warn_about_ambiguous_bases (tree t)
5898 int i;
5899 vec<tree, va_gc> *vbases;
5900 tree basetype;
5901 tree binfo;
5902 tree base_binfo;
5904 /* If there are no repeated bases, nothing can be ambiguous. */
5905 if (!CLASSTYPE_REPEATED_BASE_P (t))
5906 return;
5908 /* Check direct bases. */
5909 for (binfo = TYPE_BINFO (t), i = 0;
5910 BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
5912 basetype = BINFO_TYPE (base_binfo);
5914 if (!uniquely_derived_from_p (basetype, t))
5915 warning (0, "direct base %qT inaccessible in %qT due to ambiguity",
5916 basetype, t);
5919 /* Check for ambiguous virtual bases. */
5920 if (extra_warnings)
5921 for (vbases = CLASSTYPE_VBASECLASSES (t), i = 0;
5922 vec_safe_iterate (vbases, i, &binfo); i++)
5924 basetype = BINFO_TYPE (binfo);
5926 if (!uniquely_derived_from_p (basetype, t))
5927 warning (OPT_Wextra, "virtual base %qT inaccessible in %qT due "
5928 "to ambiguity", basetype, t);
5932 /* Compare two INTEGER_CSTs K1 and K2. */
5934 static int
5935 splay_tree_compare_integer_csts (splay_tree_key k1, splay_tree_key k2)
5937 return tree_int_cst_compare ((tree) k1, (tree) k2);
5940 /* Increase the size indicated in RLI to account for empty classes
5941 that are "off the end" of the class. */
5943 static void
5944 include_empty_classes (record_layout_info rli)
5946 tree eoc;
5947 tree rli_size;
5949 /* It might be the case that we grew the class to allocate a
5950 zero-sized base class. That won't be reflected in RLI, yet,
5951 because we are willing to overlay multiple bases at the same
5952 offset. However, now we need to make sure that RLI is big enough
5953 to reflect the entire class. */
5954 eoc = end_of_class (rli->t, CLASSTYPE_AS_BASE (rli->t) != NULL_TREE);
5955 rli_size = rli_size_unit_so_far (rli);
5956 if (TREE_CODE (rli_size) == INTEGER_CST
5957 && tree_int_cst_lt (rli_size, eoc))
5959 /* The size should have been rounded to a whole byte. */
5960 gcc_assert (tree_int_cst_equal
5961 (rli->bitpos, round_down (rli->bitpos, BITS_PER_UNIT)));
5962 rli->bitpos
5963 = size_binop (PLUS_EXPR,
5964 rli->bitpos,
5965 size_binop (MULT_EXPR,
5966 fold_convert (bitsizetype,
5967 size_binop (MINUS_EXPR,
5968 eoc, rli_size)),
5969 bitsize_int (BITS_PER_UNIT)));
5970 normalize_rli (rli);
5974 /* Calculate the TYPE_SIZE, TYPE_ALIGN, etc for T. Calculate
5975 BINFO_OFFSETs for all of the base-classes. Position the vtable
5976 pointer. Accumulate declared virtual functions on VIRTUALS_P. */
5978 static void
5979 layout_class_type (tree t, tree *virtuals_p)
5981 tree non_static_data_members;
5982 tree field;
5983 tree vptr;
5984 record_layout_info rli;
5985 /* Maps offsets (represented as INTEGER_CSTs) to a TREE_LIST of
5986 types that appear at that offset. */
5987 splay_tree empty_base_offsets;
5988 /* True if the last field laid out was a bit-field. */
5989 bool last_field_was_bitfield = false;
5990 /* The location at which the next field should be inserted. */
5991 tree *next_field;
5993 /* Keep track of the first non-static data member. */
5994 non_static_data_members = TYPE_FIELDS (t);
5996 /* Start laying out the record. */
5997 rli = start_record_layout (t);
5999 /* Mark all the primary bases in the hierarchy. */
6000 determine_primary_bases (t);
6002 /* Create a pointer to our virtual function table. */
6003 vptr = create_vtable_ptr (t, virtuals_p);
6005 /* The vptr is always the first thing in the class. */
6006 if (vptr)
6008 DECL_CHAIN (vptr) = TYPE_FIELDS (t);
6009 TYPE_FIELDS (t) = vptr;
6010 next_field = &DECL_CHAIN (vptr);
6011 place_field (rli, vptr);
6013 else
6014 next_field = &TYPE_FIELDS (t);
6016 /* Build FIELD_DECLs for all of the non-virtual base-types. */
6017 empty_base_offsets = splay_tree_new (splay_tree_compare_integer_csts,
6018 NULL, NULL);
6019 build_base_fields (rli, empty_base_offsets, next_field);
6021 /* Layout the non-static data members. */
6022 for (field = non_static_data_members; field; field = DECL_CHAIN (field))
6024 tree type;
6025 tree padding;
6027 /* We still pass things that aren't non-static data members to
6028 the back end, in case it wants to do something with them. */
6029 if (TREE_CODE (field) != FIELD_DECL)
6031 place_field (rli, field);
6032 /* If the static data member has incomplete type, keep track
6033 of it so that it can be completed later. (The handling
6034 of pending statics in finish_record_layout is
6035 insufficient; consider:
6037 struct S1;
6038 struct S2 { static S1 s1; };
6040 At this point, finish_record_layout will be called, but
6041 S1 is still incomplete.) */
6042 if (VAR_P (field))
6044 maybe_register_incomplete_var (field);
6045 /* The visibility of static data members is determined
6046 at their point of declaration, not their point of
6047 definition. */
6048 determine_visibility (field);
6050 continue;
6053 type = TREE_TYPE (field);
6054 if (type == error_mark_node)
6055 continue;
6057 padding = NULL_TREE;
6059 /* If this field is a bit-field whose width is greater than its
6060 type, then there are some special rules for allocating
6061 it. */
6062 if (DECL_C_BIT_FIELD (field)
6063 && tree_int_cst_lt (TYPE_SIZE (type), DECL_SIZE (field)))
6065 bool was_unnamed_p = false;
6066 /* We must allocate the bits as if suitably aligned for the
6067 longest integer type that fits in this many bits. Then,
6068 we are supposed to use the left over bits as additional
6069 padding. */
6071 /* Do not pick a type bigger than MAX_FIXED_MODE_SIZE. */
6072 tree limit = size_int (MAX_FIXED_MODE_SIZE);
6073 if (tree_int_cst_lt (DECL_SIZE (field), limit))
6074 limit = DECL_SIZE (field);
6076 tree integer_type = integer_types[itk_char];
6077 for (unsigned itk = itk_char; itk != itk_none; itk++)
6078 if (tree next = integer_types[itk])
6080 if (tree_int_cst_lt (limit, TYPE_SIZE (next)))
6081 /* Too big, so our current guess is what we want. */
6082 break;
6083 /* Not bigger than limit, ok */
6084 integer_type = next;
6087 /* Figure out how much additional padding is required. */
6088 if (TREE_CODE (t) == UNION_TYPE)
6089 /* In a union, the padding field must have the full width
6090 of the bit-field; all fields start at offset zero. */
6091 padding = DECL_SIZE (field);
6092 else
6093 padding = size_binop (MINUS_EXPR, DECL_SIZE (field),
6094 TYPE_SIZE (integer_type));
6096 if (integer_zerop (padding))
6097 padding = NULL_TREE;
6099 /* An unnamed bitfield does not normally affect the
6100 alignment of the containing class on a target where
6101 PCC_BITFIELD_TYPE_MATTERS. But, the C++ ABI does not
6102 make any exceptions for unnamed bitfields when the
6103 bitfields are longer than their types. Therefore, we
6104 temporarily give the field a name. */
6105 if (PCC_BITFIELD_TYPE_MATTERS && !DECL_NAME (field))
6107 was_unnamed_p = true;
6108 DECL_NAME (field) = make_anon_name ();
6111 DECL_SIZE (field) = TYPE_SIZE (integer_type);
6112 SET_DECL_ALIGN (field, TYPE_ALIGN (integer_type));
6113 DECL_USER_ALIGN (field) = TYPE_USER_ALIGN (integer_type);
6114 layout_nonempty_base_or_field (rli, field, NULL_TREE,
6115 empty_base_offsets);
6116 if (was_unnamed_p)
6117 DECL_NAME (field) = NULL_TREE;
6118 /* Now that layout has been performed, set the size of the
6119 field to the size of its declared type; the rest of the
6120 field is effectively invisible. */
6121 DECL_SIZE (field) = TYPE_SIZE (type);
6122 /* We must also reset the DECL_MODE of the field. */
6123 SET_DECL_MODE (field, TYPE_MODE (type));
6125 else
6126 layout_nonempty_base_or_field (rli, field, NULL_TREE,
6127 empty_base_offsets);
6129 /* Remember the location of any empty classes in FIELD. */
6130 record_subobject_offsets (TREE_TYPE (field),
6131 byte_position(field),
6132 empty_base_offsets,
6133 /*is_data_member=*/true);
6135 /* If a bit-field does not immediately follow another bit-field,
6136 and yet it starts in the middle of a byte, we have failed to
6137 comply with the ABI. */
6138 if (warn_abi
6139 && DECL_C_BIT_FIELD (field)
6140 /* The TREE_NO_WARNING flag gets set by Objective-C when
6141 laying out an Objective-C class. The ObjC ABI differs
6142 from the C++ ABI, and so we do not want a warning
6143 here. */
6144 && !TREE_NO_WARNING (field)
6145 && !last_field_was_bitfield
6146 && !integer_zerop (size_binop (TRUNC_MOD_EXPR,
6147 DECL_FIELD_BIT_OFFSET (field),
6148 bitsize_unit_node)))
6149 warning_at (DECL_SOURCE_LOCATION (field), OPT_Wabi,
6150 "offset of %qD is not ABI-compliant and may "
6151 "change in a future version of GCC", field);
6153 /* The middle end uses the type of expressions to determine the
6154 possible range of expression values. In order to optimize
6155 "x.i > 7" to "false" for a 2-bit bitfield "i", the middle end
6156 must be made aware of the width of "i", via its type.
6158 Because C++ does not have integer types of arbitrary width,
6159 we must (for the purposes of the front end) convert from the
6160 type assigned here to the declared type of the bitfield
6161 whenever a bitfield expression is used as an rvalue.
6162 Similarly, when assigning a value to a bitfield, the value
6163 must be converted to the type given the bitfield here. */
6164 if (DECL_C_BIT_FIELD (field))
6166 unsigned HOST_WIDE_INT width;
6167 tree ftype = TREE_TYPE (field);
6168 width = tree_to_uhwi (DECL_SIZE (field));
6169 if (width != TYPE_PRECISION (ftype))
6171 TREE_TYPE (field)
6172 = c_build_bitfield_integer_type (width,
6173 TYPE_UNSIGNED (ftype));
6174 TREE_TYPE (field)
6175 = cp_build_qualified_type (TREE_TYPE (field),
6176 cp_type_quals (ftype));
6180 /* If we needed additional padding after this field, add it
6181 now. */
6182 if (padding)
6184 tree padding_field;
6186 padding_field = build_decl (input_location,
6187 FIELD_DECL,
6188 NULL_TREE,
6189 char_type_node);
6190 DECL_BIT_FIELD (padding_field) = 1;
6191 DECL_SIZE (padding_field) = padding;
6192 DECL_CONTEXT (padding_field) = t;
6193 DECL_ARTIFICIAL (padding_field) = 1;
6194 DECL_IGNORED_P (padding_field) = 1;
6195 DECL_PADDING_P (padding_field) = 1;
6196 layout_nonempty_base_or_field (rli, padding_field,
6197 NULL_TREE,
6198 empty_base_offsets);
6201 last_field_was_bitfield = DECL_C_BIT_FIELD (field);
6204 if (!integer_zerop (rli->bitpos))
6206 /* Make sure that we are on a byte boundary so that the size of
6207 the class without virtual bases will always be a round number
6208 of bytes. */
6209 rli->bitpos = round_up_loc (input_location, rli->bitpos, BITS_PER_UNIT);
6210 normalize_rli (rli);
6213 /* Delete all zero-width bit-fields from the list of fields. Now
6214 that the type is laid out they are no longer important. */
6215 remove_zero_width_bit_fields (t);
6217 if (CLASSTYPE_NON_LAYOUT_POD_P (t) || CLASSTYPE_EMPTY_P (t))
6219 /* T needs a different layout as a base (eliding virtual bases
6220 or whatever). Create that version. */
6221 tree base_t = make_node (TREE_CODE (t));
6223 /* If the ABI version is not at least two, and the last
6224 field was a bit-field, RLI may not be on a byte
6225 boundary. In particular, rli_size_unit_so_far might
6226 indicate the last complete byte, while rli_size_so_far
6227 indicates the total number of bits used. Therefore,
6228 rli_size_so_far, rather than rli_size_unit_so_far, is
6229 used to compute TYPE_SIZE_UNIT. */
6230 tree eoc = end_of_class (t, /*include_virtuals_p=*/0);
6231 TYPE_SIZE_UNIT (base_t)
6232 = size_binop (MAX_EXPR,
6233 fold_convert (sizetype,
6234 size_binop (CEIL_DIV_EXPR,
6235 rli_size_so_far (rli),
6236 bitsize_int (BITS_PER_UNIT))),
6237 eoc);
6238 TYPE_SIZE (base_t)
6239 = size_binop (MAX_EXPR,
6240 rli_size_so_far (rli),
6241 size_binop (MULT_EXPR,
6242 fold_convert (bitsizetype, eoc),
6243 bitsize_int (BITS_PER_UNIT)));
6244 SET_TYPE_ALIGN (base_t, rli->record_align);
6245 TYPE_USER_ALIGN (base_t) = TYPE_USER_ALIGN (t);
6247 /* Copy the non-static data members of T. This will include its
6248 direct non-virtual bases & vtable. */
6249 next_field = &TYPE_FIELDS (base_t);
6250 for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
6251 if (TREE_CODE (field) == FIELD_DECL)
6253 *next_field = copy_node (field);
6254 DECL_CONTEXT (*next_field) = base_t;
6255 next_field = &DECL_CHAIN (*next_field);
6257 *next_field = NULL_TREE;
6259 /* We use the base type for trivial assignments, and hence it
6260 needs a mode. */
6261 compute_record_mode (base_t);
6263 TYPE_CONTEXT (base_t) = t;
6265 /* Record the base version of the type. */
6266 CLASSTYPE_AS_BASE (t) = base_t;
6268 else
6269 CLASSTYPE_AS_BASE (t) = t;
6271 /* Every empty class contains an empty class. */
6272 if (CLASSTYPE_EMPTY_P (t))
6273 CLASSTYPE_CONTAINS_EMPTY_CLASS_P (t) = 1;
6275 /* Set the TYPE_DECL for this type to contain the right
6276 value for DECL_OFFSET, so that we can use it as part
6277 of a COMPONENT_REF for multiple inheritance. */
6278 layout_decl (TYPE_MAIN_DECL (t), 0);
6280 /* Now fix up any virtual base class types that we left lying
6281 around. We must get these done before we try to lay out the
6282 virtual function table. As a side-effect, this will remove the
6283 base subobject fields. */
6284 layout_virtual_bases (rli, empty_base_offsets);
6286 /* Make sure that empty classes are reflected in RLI at this
6287 point. */
6288 include_empty_classes (rli);
6290 /* Make sure not to create any structures with zero size. */
6291 if (integer_zerop (rli_size_unit_so_far (rli)) && CLASSTYPE_EMPTY_P (t))
6292 place_field (rli,
6293 build_decl (input_location,
6294 FIELD_DECL, NULL_TREE, char_type_node));
6296 /* If this is a non-POD, declaring it packed makes a difference to how it
6297 can be used as a field; don't let finalize_record_size undo it. */
6298 if (TYPE_PACKED (t) && !layout_pod_type_p (t))
6299 rli->packed_maybe_necessary = true;
6301 /* Let the back end lay out the type. */
6302 finish_record_layout (rli, /*free_p=*/true);
6304 if (TYPE_SIZE_UNIT (t)
6305 && TREE_CODE (TYPE_SIZE_UNIT (t)) == INTEGER_CST
6306 && !TREE_OVERFLOW (TYPE_SIZE_UNIT (t))
6307 && !valid_constant_size_p (TYPE_SIZE_UNIT (t)))
6308 error ("size of type %qT is too large (%qE bytes)", t, TYPE_SIZE_UNIT (t));
6310 /* Warn about bases that can't be talked about due to ambiguity. */
6311 warn_about_ambiguous_bases (t);
6313 /* Now that we're done with layout, give the base fields the real types. */
6314 for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
6315 if (DECL_ARTIFICIAL (field) && IS_FAKE_BASE_TYPE (TREE_TYPE (field)))
6316 TREE_TYPE (field) = TYPE_CONTEXT (TREE_TYPE (field));
6318 /* Clean up. */
6319 splay_tree_delete (empty_base_offsets);
6321 if (CLASSTYPE_EMPTY_P (t)
6322 && tree_int_cst_lt (sizeof_biggest_empty_class,
6323 TYPE_SIZE_UNIT (t)))
6324 sizeof_biggest_empty_class = TYPE_SIZE_UNIT (t);
6327 /* Determine the "key method" for the class type indicated by TYPE,
6328 and set CLASSTYPE_KEY_METHOD accordingly. */
6330 void
6331 determine_key_method (tree type)
6333 tree method;
6335 if (processing_template_decl
6336 || CLASSTYPE_TEMPLATE_INSTANTIATION (type)
6337 || CLASSTYPE_INTERFACE_KNOWN (type))
6338 return;
6340 /* The key method is the first non-pure virtual function that is not
6341 inline at the point of class definition. On some targets the
6342 key function may not be inline; those targets should not call
6343 this function until the end of the translation unit. */
6344 for (method = TYPE_FIELDS (type); method; method = DECL_CHAIN (method))
6345 if (TREE_CODE (method) == FUNCTION_DECL
6346 && DECL_VINDEX (method) != NULL_TREE
6347 && ! DECL_DECLARED_INLINE_P (method)
6348 && ! DECL_PURE_VIRTUAL_P (method))
6350 CLASSTYPE_KEY_METHOD (type) = method;
6351 break;
6354 return;
6357 /* Helper of find_flexarrays. Return true when FLD refers to a non-static
6358 class data member of non-zero size, otherwise false. */
6360 static inline bool
6361 field_nonempty_p (const_tree fld)
6363 if (TREE_CODE (fld) == ERROR_MARK)
6364 return false;
6366 tree type = TREE_TYPE (fld);
6367 if (TREE_CODE (fld) == FIELD_DECL
6368 && TREE_CODE (type) != ERROR_MARK
6369 && (DECL_NAME (fld) || RECORD_OR_UNION_TYPE_P (type)))
6371 return TYPE_SIZE (type)
6372 && (TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST
6373 || !tree_int_cst_equal (size_zero_node, TYPE_SIZE (type)));
6376 return false;
6379 /* Used by find_flexarrays and related functions. */
6381 struct flexmems_t
6383 /* The first flexible array member or non-zero array member found
6384 in the order of layout. */
6385 tree array;
6386 /* First non-static non-empty data member in the class or its bases. */
6387 tree first;
6388 /* The first non-static non-empty data member following either
6389 the flexible array member, if found, or the zero-length array member
6390 otherwise. AFTER[1] refers to the first such data member of a union
6391 of which the struct containing the flexible array member or zero-length
6392 array is a member, or NULL when no such union exists. This element is
6393 only used during searching, not for diagnosing problems. AFTER[0]
6394 refers to the first such data member that is not a member of such
6395 a union. */
6396 tree after[2];
6398 /* Refers to a struct (not union) in which the struct of which the flexible
6399 array is member is defined. Used to diagnose strictly (according to C)
6400 invalid uses of the latter structs. */
6401 tree enclosing;
6404 /* Find either the first flexible array member or the first zero-length
6405 array, in that order of preference, among members of class T (but not
6406 its base classes), and set members of FMEM accordingly.
6407 BASE_P is true if T is a base class of another class.
6408 PUN is set to the outermost union in which the flexible array member
6409 (or zero-length array) is defined if one such union exists, otherwise
6410 to NULL.
6411 Similarly, PSTR is set to a data member of the outermost struct of
6412 which the flexible array is a member if one such struct exists,
6413 otherwise to NULL. */
6415 static void
6416 find_flexarrays (tree t, flexmems_t *fmem, bool base_p,
6417 tree pun /* = NULL_TREE */,
6418 tree pstr /* = NULL_TREE */)
6420 /* Set the "pointer" to the outermost enclosing union if not set
6421 yet and maintain it for the remainder of the recursion. */
6422 if (!pun && TREE_CODE (t) == UNION_TYPE)
6423 pun = t;
6425 for (tree fld = TYPE_FIELDS (t); fld; fld = DECL_CHAIN (fld))
6427 if (fld == error_mark_node)
6428 return;
6430 /* Is FLD a typedef for an anonymous struct? */
6432 /* FIXME: Note that typedefs (as well as arrays) need to be fully
6433 handled elsewhere so that errors like the following are detected
6434 as well:
6435 typedef struct { int i, a[], j; } S; // bug c++/72753
6436 S s [2]; // bug c++/68489
6438 if (TREE_CODE (fld) == TYPE_DECL
6439 && DECL_IMPLICIT_TYPEDEF_P (fld)
6440 && CLASS_TYPE_P (TREE_TYPE (fld))
6441 && anon_aggrname_p (DECL_NAME (fld)))
6443 /* Check the nested unnamed type referenced via a typedef
6444 independently of FMEM (since it's not a data member of
6445 the enclosing class). */
6446 check_flexarrays (TREE_TYPE (fld));
6447 continue;
6450 /* Skip anything that's GCC-generated or not a (non-static) data
6451 member. */
6452 if (DECL_ARTIFICIAL (fld) || TREE_CODE (fld) != FIELD_DECL)
6453 continue;
6455 /* Type of the member. */
6456 tree fldtype = TREE_TYPE (fld);
6457 if (fldtype == error_mark_node)
6458 return;
6460 /* Determine the type of the array element or object referenced
6461 by the member so that it can be checked for flexible array
6462 members if it hasn't been yet. */
6463 tree eltype = fldtype;
6464 while (TREE_CODE (eltype) == ARRAY_TYPE
6465 || TREE_CODE (eltype) == POINTER_TYPE
6466 || TREE_CODE (eltype) == REFERENCE_TYPE)
6467 eltype = TREE_TYPE (eltype);
6469 if (RECORD_OR_UNION_TYPE_P (eltype))
6471 if (fmem->array && !fmem->after[bool (pun)])
6473 /* Once the member after the flexible array has been found
6474 we're done. */
6475 fmem->after[bool (pun)] = fld;
6476 break;
6479 if (eltype == fldtype || TYPE_UNNAMED_P (eltype))
6481 /* Descend into the non-static member struct or union and try
6482 to find a flexible array member or zero-length array among
6483 its members. This is only necessary for anonymous types
6484 and types in whose context the current type T has not been
6485 defined (the latter must not be checked again because they
6486 are already in the process of being checked by one of the
6487 recursive calls). */
6489 tree first = fmem->first;
6490 tree array = fmem->array;
6492 /* If this member isn't anonymous and a prior non-flexible array
6493 member has been seen in one of the enclosing structs, clear
6494 the FIRST member since it doesn't contribute to the flexible
6495 array struct's members. */
6496 if (first && !array && !ANON_AGGR_TYPE_P (eltype))
6497 fmem->first = NULL_TREE;
6499 find_flexarrays (eltype, fmem, false, pun,
6500 !pstr && TREE_CODE (t) == RECORD_TYPE ? fld : pstr);
6502 if (fmem->array != array)
6503 continue;
6505 if (first && !array && !ANON_AGGR_TYPE_P (eltype))
6507 /* Restore the FIRST member reset above if no flexible
6508 array member has been found in this member's struct. */
6509 fmem->first = first;
6512 /* If the member struct contains the first flexible array
6513 member, or if this member is a base class, continue to
6514 the next member and avoid setting the FMEM->NEXT pointer
6515 to point to it. */
6516 if (base_p)
6517 continue;
6521 if (field_nonempty_p (fld))
6523 /* Remember the first non-static data member. */
6524 if (!fmem->first)
6525 fmem->first = fld;
6527 /* Remember the first non-static data member after the flexible
6528 array member, if one has been found, or the zero-length array
6529 if it has been found. */
6530 if (fmem->array && !fmem->after[bool (pun)])
6531 fmem->after[bool (pun)] = fld;
6534 /* Skip non-arrays. */
6535 if (TREE_CODE (fldtype) != ARRAY_TYPE)
6536 continue;
6538 /* Determine the upper bound of the array if it has one. */
6539 if (TYPE_DOMAIN (fldtype))
6541 if (fmem->array)
6543 /* Make a record of the zero-length array if either one
6544 such field or a flexible array member has been seen to
6545 handle the pathological and unlikely case of multiple
6546 such members. */
6547 if (!fmem->after[bool (pun)])
6548 fmem->after[bool (pun)] = fld;
6550 else if (integer_all_onesp (TYPE_MAX_VALUE (TYPE_DOMAIN (fldtype))))
6552 /* Remember the first zero-length array unless a flexible array
6553 member has already been seen. */
6554 fmem->array = fld;
6555 fmem->enclosing = pstr;
6558 else
6560 /* Flexible array members have no upper bound. */
6561 if (fmem->array)
6563 if (TYPE_DOMAIN (TREE_TYPE (fmem->array)))
6565 /* Replace the zero-length array if it's been stored and
6566 reset the after pointer. */
6567 fmem->after[bool (pun)] = NULL_TREE;
6568 fmem->array = fld;
6569 fmem->enclosing = pstr;
6571 else if (!fmem->after[bool (pun)])
6572 /* Make a record of another flexible array member. */
6573 fmem->after[bool (pun)] = fld;
6575 else
6577 fmem->array = fld;
6578 fmem->enclosing = pstr;
6584 /* Diagnose a strictly (by the C standard) invalid use of a struct with
6585 a flexible array member (or the zero-length array extension). */
6587 static void
6588 diagnose_invalid_flexarray (const flexmems_t *fmem)
6590 if (fmem->array && fmem->enclosing
6591 && pedwarn (location_of (fmem->enclosing), OPT_Wpedantic,
6592 TYPE_DOMAIN (TREE_TYPE (fmem->array))
6593 ? G_("invalid use of %q#T with a zero-size array "
6594 "in %q#D")
6595 : G_("invalid use of %q#T with a flexible array member "
6596 "in %q#T"),
6597 DECL_CONTEXT (fmem->array),
6598 DECL_CONTEXT (fmem->enclosing)))
6599 inform (DECL_SOURCE_LOCATION (fmem->array),
6600 "array member %q#D declared here", fmem->array);
6603 /* Issue diagnostics for invalid flexible array members or zero-length
6604 arrays that are not the last elements of the containing class or its
6605 base classes or that are its sole members. */
6607 static void
6608 diagnose_flexarrays (tree t, const flexmems_t *fmem)
6610 if (!fmem->array)
6611 return;
6613 if (fmem->first && !fmem->after[0])
6615 diagnose_invalid_flexarray (fmem);
6616 return;
6619 /* Has a diagnostic been issued? */
6620 bool diagd = false;
6622 const char *msg = 0;
6624 if (TYPE_DOMAIN (TREE_TYPE (fmem->array)))
6626 if (fmem->after[0])
6627 msg = G_("zero-size array member %qD not at end of %q#T");
6628 else if (!fmem->first)
6629 msg = G_("zero-size array member %qD in an otherwise empty %q#T");
6631 if (msg)
6633 location_t loc = DECL_SOURCE_LOCATION (fmem->array);
6635 if (pedwarn (loc, OPT_Wpedantic, msg, fmem->array, t))
6637 inform (location_of (t), "in the definition of %q#T", t);
6638 diagd = true;
6642 else
6644 if (fmem->after[0])
6645 msg = G_("flexible array member %qD not at end of %q#T");
6646 else if (!fmem->first)
6647 msg = G_("flexible array member %qD in an otherwise empty %q#T");
6649 if (msg)
6651 location_t loc = DECL_SOURCE_LOCATION (fmem->array);
6652 diagd = true;
6654 error_at (loc, msg, fmem->array, t);
6656 /* In the unlikely event that the member following the flexible
6657 array member is declared in a different class, or the member
6658 overlaps another member of a common union, point to it.
6659 Otherwise it should be obvious. */
6660 if (fmem->after[0]
6661 && ((DECL_CONTEXT (fmem->after[0])
6662 != DECL_CONTEXT (fmem->array))))
6664 inform (DECL_SOURCE_LOCATION (fmem->after[0]),
6665 "next member %q#D declared here",
6666 fmem->after[0]);
6667 inform (location_of (t), "in the definition of %q#T", t);
6672 if (!diagd && fmem->array && fmem->enclosing)
6673 diagnose_invalid_flexarray (fmem);
6677 /* Recursively check to make sure that any flexible array or zero-length
6678 array members of class T or its bases are valid (i.e., not the sole
6679 non-static data member of T and, if one exists, that it is the last
6680 non-static data member of T and its base classes. FMEM is expected
6681 to be initially null and is used internally by recursive calls to
6682 the function. Issue the appropriate diagnostics for the array member
6683 that fails the checks. */
6685 static void
6686 check_flexarrays (tree t, flexmems_t *fmem /* = NULL */,
6687 bool base_p /* = false */)
6689 /* Initialize the result of a search for flexible array and zero-length
6690 array members. Avoid doing any work if the most interesting FMEM data
6691 have already been populated. */
6692 flexmems_t flexmems = flexmems_t ();
6693 if (!fmem)
6694 fmem = &flexmems;
6695 else if (fmem->array && fmem->first && fmem->after[0])
6696 return;
6698 tree fam = fmem->array;
6700 /* Recursively check the primary base class first. */
6701 if (CLASSTYPE_HAS_PRIMARY_BASE_P (t))
6703 tree basetype = BINFO_TYPE (CLASSTYPE_PRIMARY_BINFO (t));
6704 check_flexarrays (basetype, fmem, true);
6707 /* Recursively check the base classes. */
6708 int nbases = TYPE_BINFO (t) ? BINFO_N_BASE_BINFOS (TYPE_BINFO (t)) : 0;
6709 for (int i = 0; i < nbases; ++i)
6711 tree base_binfo = BINFO_BASE_BINFO (TYPE_BINFO (t), i);
6713 /* The primary base class was already checked above. */
6714 if (base_binfo == CLASSTYPE_PRIMARY_BINFO (t))
6715 continue;
6717 /* Virtual base classes are at the end. */
6718 if (BINFO_VIRTUAL_P (base_binfo))
6719 continue;
6721 /* Check the base class. */
6722 check_flexarrays (BINFO_TYPE (base_binfo), fmem, /*base_p=*/true);
6725 if (fmem == &flexmems)
6727 /* Check virtual base classes only once per derived class.
6728 I.e., this check is not performed recursively for base
6729 classes. */
6730 int i;
6731 tree base_binfo;
6732 vec<tree, va_gc> *vbases;
6733 for (vbases = CLASSTYPE_VBASECLASSES (t), i = 0;
6734 vec_safe_iterate (vbases, i, &base_binfo); i++)
6736 /* Check the virtual base class. */
6737 tree basetype = TREE_TYPE (base_binfo);
6739 check_flexarrays (basetype, fmem, /*base_p=*/true);
6743 /* Is the type unnamed (and therefore a member of it potentially
6744 an anonymous struct or union)? */
6745 bool maybe_anon_p = TYPE_UNNAMED_P (t);
6747 /* Search the members of the current (possibly derived) class, skipping
6748 unnamed structs and unions since those could be anonymous. */
6749 if (fmem != &flexmems || !maybe_anon_p)
6750 find_flexarrays (t, fmem, base_p || fam != fmem->array);
6752 if (fmem == &flexmems && !maybe_anon_p)
6754 /* Issue diagnostics for invalid flexible and zero-length array
6755 members found in base classes or among the members of the current
6756 class. Ignore anonymous structs and unions whose members are
6757 considered to be members of the enclosing class and thus will
6758 be diagnosed when checking it. */
6759 diagnose_flexarrays (t, fmem);
6763 /* Perform processing required when the definition of T (a class type)
6764 is complete. Diagnose invalid definitions of flexible array members
6765 and zero-size arrays. */
6767 void
6768 finish_struct_1 (tree t)
6770 tree x;
6771 /* A TREE_LIST. The TREE_VALUE of each node is a FUNCTION_DECL. */
6772 tree virtuals = NULL_TREE;
6774 if (COMPLETE_TYPE_P (t))
6776 gcc_assert (MAYBE_CLASS_TYPE_P (t));
6777 error ("redefinition of %q#T", t);
6778 popclass ();
6779 return;
6782 /* If this type was previously laid out as a forward reference,
6783 make sure we lay it out again. */
6784 TYPE_SIZE (t) = NULL_TREE;
6785 CLASSTYPE_PRIMARY_BINFO (t) = NULL_TREE;
6787 /* Make assumptions about the class; we'll reset the flags if
6788 necessary. */
6789 CLASSTYPE_EMPTY_P (t) = 1;
6790 CLASSTYPE_NEARLY_EMPTY_P (t) = 1;
6791 CLASSTYPE_CONTAINS_EMPTY_CLASS_P (t) = 0;
6792 CLASSTYPE_LITERAL_P (t) = true;
6794 /* Do end-of-class semantic processing: checking the validity of the
6795 bases and members and add implicitly generated methods. */
6796 check_bases_and_members (t);
6798 /* Find the key method. */
6799 if (TYPE_CONTAINS_VPTR_P (t))
6801 /* The Itanium C++ ABI permits the key method to be chosen when
6802 the class is defined -- even though the key method so
6803 selected may later turn out to be an inline function. On
6804 some systems (such as ARM Symbian OS) the key method cannot
6805 be determined until the end of the translation unit. On such
6806 systems, we leave CLASSTYPE_KEY_METHOD set to NULL, which
6807 will cause the class to be added to KEYED_CLASSES. Then, in
6808 finish_file we will determine the key method. */
6809 if (targetm.cxx.key_method_may_be_inline ())
6810 determine_key_method (t);
6812 /* If a polymorphic class has no key method, we may emit the vtable
6813 in every translation unit where the class definition appears. If
6814 we're devirtualizing, we can look into the vtable even if we
6815 aren't emitting it. */
6816 if (!CLASSTYPE_KEY_METHOD (t))
6817 vec_safe_push (keyed_classes, t);
6820 /* Layout the class itself. */
6821 layout_class_type (t, &virtuals);
6822 /* COMPLETE_TYPE_P is now true. */
6824 set_class_bindings (t);
6826 /* With the layout complete, check for flexible array members and
6827 zero-length arrays that might overlap other members in the final
6828 layout. */
6829 check_flexarrays (t);
6831 virtuals = modify_all_vtables (t, nreverse (virtuals));
6833 /* If necessary, create the primary vtable for this class. */
6834 if (virtuals || TYPE_CONTAINS_VPTR_P (t))
6836 /* We must enter these virtuals into the table. */
6837 if (!CLASSTYPE_HAS_PRIMARY_BASE_P (t))
6838 build_primary_vtable (NULL_TREE, t);
6839 else if (! BINFO_NEW_VTABLE_MARKED (TYPE_BINFO (t)))
6840 /* Here we know enough to change the type of our virtual
6841 function table, but we will wait until later this function. */
6842 build_primary_vtable (CLASSTYPE_PRIMARY_BINFO (t), t);
6844 /* If we're warning about ABI tags, check the types of the new
6845 virtual functions. */
6846 if (warn_abi_tag)
6847 for (tree v = virtuals; v; v = TREE_CHAIN (v))
6848 check_abi_tags (t, TREE_VALUE (v));
6851 if (TYPE_CONTAINS_VPTR_P (t))
6853 int vindex;
6854 tree fn;
6856 if (BINFO_VTABLE (TYPE_BINFO (t)))
6857 gcc_assert (DECL_VIRTUAL_P (BINFO_VTABLE (TYPE_BINFO (t))));
6858 if (!CLASSTYPE_HAS_PRIMARY_BASE_P (t))
6859 gcc_assert (BINFO_VIRTUALS (TYPE_BINFO (t)) == NULL_TREE);
6861 /* Add entries for virtual functions introduced by this class. */
6862 BINFO_VIRTUALS (TYPE_BINFO (t))
6863 = chainon (BINFO_VIRTUALS (TYPE_BINFO (t)), virtuals);
6865 /* Set DECL_VINDEX for all functions declared in this class. */
6866 for (vindex = 0, fn = BINFO_VIRTUALS (TYPE_BINFO (t));
6868 fn = TREE_CHAIN (fn),
6869 vindex += (TARGET_VTABLE_USES_DESCRIPTORS
6870 ? TARGET_VTABLE_USES_DESCRIPTORS : 1))
6872 tree fndecl = BV_FN (fn);
6874 if (DECL_THUNK_P (fndecl))
6875 /* A thunk. We should never be calling this entry directly
6876 from this vtable -- we'd use the entry for the non
6877 thunk base function. */
6878 DECL_VINDEX (fndecl) = NULL_TREE;
6879 else if (TREE_CODE (DECL_VINDEX (fndecl)) != INTEGER_CST)
6880 DECL_VINDEX (fndecl) = build_int_cst (NULL_TREE, vindex);
6884 finish_struct_bits (t);
6886 set_method_tm_attributes (t);
6887 if (flag_openmp || flag_openmp_simd)
6888 finish_omp_declare_simd_methods (t);
6890 /* Clear DECL_IN_AGGR_P for all member functions. Complete the rtl
6891 for any static member objects of the type we're working on. */
6892 for (x = TYPE_FIELDS (t); x; x = DECL_CHAIN (x))
6893 if (DECL_DECLARES_FUNCTION_P (x))
6894 DECL_IN_AGGR_P (x) = false;
6895 else if (VAR_P (x) && TREE_STATIC (x)
6896 && TREE_TYPE (x) != error_mark_node
6897 && same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (x)), t))
6898 SET_DECL_MODE (x, TYPE_MODE (t));
6900 /* Complain if one of the field types requires lower visibility. */
6901 constrain_class_visibility (t);
6903 /* Make the rtl for any new vtables we have created, and unmark
6904 the base types we marked. */
6905 finish_vtbls (t);
6907 /* Build the VTT for T. */
6908 build_vtt (t);
6910 if (warn_nonvdtor
6911 && TYPE_POLYMORPHIC_P (t) && accessible_nvdtor_p (t)
6912 && !CLASSTYPE_FINAL (t))
6913 warning (OPT_Wnon_virtual_dtor,
6914 "%q#T has virtual functions and accessible"
6915 " non-virtual destructor", t);
6917 complete_vars (t);
6919 if (warn_overloaded_virtual)
6920 warn_hidden (t);
6922 /* Class layout, assignment of virtual table slots, etc., is now
6923 complete. Give the back end a chance to tweak the visibility of
6924 the class or perform any other required target modifications. */
6925 targetm.cxx.adjust_class_at_definition (t);
6927 maybe_suppress_debug_info (t);
6929 if (flag_vtable_verify)
6930 vtv_save_class_info (t);
6932 dump_class_hierarchy (t);
6934 /* Finish debugging output for this type. */
6935 rest_of_type_compilation (t, ! LOCAL_CLASS_P (t));
6937 if (TYPE_TRANSPARENT_AGGR (t))
6939 tree field = first_field (t);
6940 if (field == NULL_TREE || error_operand_p (field))
6942 error ("type transparent %q#T does not have any fields", t);
6943 TYPE_TRANSPARENT_AGGR (t) = 0;
6945 else if (DECL_ARTIFICIAL (field))
6947 if (DECL_FIELD_IS_BASE (field))
6948 error ("type transparent class %qT has base classes", t);
6949 else
6951 gcc_checking_assert (DECL_VIRTUAL_P (field));
6952 error ("type transparent class %qT has virtual functions", t);
6954 TYPE_TRANSPARENT_AGGR (t) = 0;
6956 else if (TYPE_MODE (t) != DECL_MODE (field))
6958 error ("type transparent %q#T cannot be made transparent because "
6959 "the type of the first field has a different ABI from the "
6960 "class overall", t);
6961 TYPE_TRANSPARENT_AGGR (t) = 0;
6966 /* When T was built up, the member declarations were added in reverse
6967 order. Rearrange them to declaration order. */
6969 void
6970 unreverse_member_declarations (tree t)
6972 tree next;
6973 tree prev;
6974 tree x;
6976 /* The following lists are all in reverse order. Put them in
6977 declaration order now. */
6978 CLASSTYPE_DECL_LIST (t) = nreverse (CLASSTYPE_DECL_LIST (t));
6980 /* For the TYPE_FIELDS, only the non TYPE_DECLs are in reverse
6981 order, so we can't just use nreverse. Due to stat_hack
6982 chicanery in finish_member_declaration. */
6983 prev = NULL_TREE;
6984 for (x = TYPE_FIELDS (t);
6985 x && TREE_CODE (x) != TYPE_DECL;
6986 x = next)
6988 next = DECL_CHAIN (x);
6989 DECL_CHAIN (x) = prev;
6990 prev = x;
6993 if (prev)
6995 DECL_CHAIN (TYPE_FIELDS (t)) = x;
6996 TYPE_FIELDS (t) = prev;
7000 tree
7001 finish_struct (tree t, tree attributes)
7003 location_t saved_loc = input_location;
7005 /* Now that we've got all the field declarations, reverse everything
7006 as necessary. */
7007 unreverse_member_declarations (t);
7009 cplus_decl_attributes (&t, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
7010 fixup_attribute_variants (t);
7012 /* Nadger the current location so that diagnostics point to the start of
7013 the struct, not the end. */
7014 input_location = DECL_SOURCE_LOCATION (TYPE_NAME (t));
7016 if (processing_template_decl)
7018 tree x;
7020 /* We need to add the target functions of USING_DECLS, so that
7021 they can be found when the using declaration is not
7022 instantiated yet. */
7023 for (x = TYPE_FIELDS (t); x; x = DECL_CHAIN (x))
7024 if (TREE_CODE (x) == USING_DECL)
7026 tree fn = strip_using_decl (x);
7027 if (OVL_P (fn))
7028 for (lkp_iterator iter (fn); iter; ++iter)
7029 add_method (t, *iter, true);
7031 else if (DECL_DECLARES_FUNCTION_P (x))
7032 DECL_IN_AGGR_P (x) = false;
7034 TYPE_SIZE (t) = bitsize_zero_node;
7035 TYPE_SIZE_UNIT (t) = size_zero_node;
7036 /* COMPLETE_TYPE_P is now true. */
7038 set_class_bindings (t);
7040 /* We need to emit an error message if this type was used as a parameter
7041 and it is an abstract type, even if it is a template. We construct
7042 a simple CLASSTYPE_PURE_VIRTUALS list without taking bases into
7043 account and we call complete_vars with this type, which will check
7044 the PARM_DECLS. Note that while the type is being defined,
7045 CLASSTYPE_PURE_VIRTUALS contains the list of the inline friends
7046 (see CLASSTYPE_INLINE_FRIENDS) so we need to clear it. */
7047 CLASSTYPE_PURE_VIRTUALS (t) = NULL;
7048 for (x = TYPE_FIELDS (t); x; x = DECL_CHAIN (x))
7049 if (TREE_CODE (x) == FUNCTION_DECL && DECL_PURE_VIRTUAL_P (x))
7050 vec_safe_push (CLASSTYPE_PURE_VIRTUALS (t), x);
7051 complete_vars (t);
7053 /* Remember current #pragma pack value. */
7054 TYPE_PRECISION (t) = maximum_field_alignment;
7056 /* Fix up any variants we've already built. */
7057 for (x = TYPE_NEXT_VARIANT (t); x; x = TYPE_NEXT_VARIANT (x))
7059 TYPE_SIZE (x) = TYPE_SIZE (t);
7060 TYPE_SIZE_UNIT (x) = TYPE_SIZE_UNIT (t);
7061 TYPE_FIELDS (x) = TYPE_FIELDS (t);
7064 else
7065 finish_struct_1 (t);
7066 /* COMPLETE_TYPE_P is now true. */
7068 maybe_warn_about_overly_private_class (t);
7070 if (is_std_init_list (t))
7072 /* People keep complaining that the compiler crashes on an invalid
7073 definition of initializer_list, so I guess we should explicitly
7074 reject it. What the compiler internals care about is that it's a
7075 template and has a pointer field followed by size_type field. */
7076 bool ok = false;
7077 if (processing_template_decl)
7079 tree f = next_initializable_field (TYPE_FIELDS (t));
7080 if (f && TREE_CODE (TREE_TYPE (f)) == POINTER_TYPE)
7082 f = next_initializable_field (DECL_CHAIN (f));
7083 if (f && same_type_p (TREE_TYPE (f), size_type_node))
7084 ok = true;
7087 if (!ok)
7088 fatal_error (input_location, "definition of %qD does not match "
7089 "%<#include <initializer_list>%>", TYPE_NAME (t));
7092 input_location = saved_loc;
7094 TYPE_BEING_DEFINED (t) = 0;
7096 if (current_class_type)
7097 popclass ();
7098 else
7099 error ("trying to finish struct, but kicked out due to previous parse errors");
7101 if (processing_template_decl && at_function_scope_p ()
7102 /* Lambdas are defined by the LAMBDA_EXPR. */
7103 && !LAMBDA_TYPE_P (t))
7104 add_stmt (build_min (TAG_DEFN, t));
7106 return t;
7109 /* Hash table to avoid endless recursion when handling references. */
7110 static hash_table<nofree_ptr_hash<tree_node> > *fixed_type_or_null_ref_ht;
7112 /* Return the dynamic type of INSTANCE, if known.
7113 Used to determine whether the virtual function table is needed
7114 or not.
7116 *NONNULL is set iff INSTANCE can be known to be nonnull, regardless
7117 of our knowledge of its type. *NONNULL should be initialized
7118 before this function is called. */
7120 static tree
7121 fixed_type_or_null (tree instance, int *nonnull, int *cdtorp)
7123 #define RECUR(T) fixed_type_or_null((T), nonnull, cdtorp)
7125 switch (TREE_CODE (instance))
7127 case INDIRECT_REF:
7128 if (POINTER_TYPE_P (TREE_TYPE (instance)))
7129 return NULL_TREE;
7130 else
7131 return RECUR (TREE_OPERAND (instance, 0));
7133 case CALL_EXPR:
7134 /* This is a call to a constructor, hence it's never zero. */
7135 if (CALL_EXPR_FN (instance)
7136 && TREE_HAS_CONSTRUCTOR (instance))
7138 if (nonnull)
7139 *nonnull = 1;
7140 return TREE_TYPE (instance);
7142 return NULL_TREE;
7144 case SAVE_EXPR:
7145 /* This is a call to a constructor, hence it's never zero. */
7146 if (TREE_HAS_CONSTRUCTOR (instance))
7148 if (nonnull)
7149 *nonnull = 1;
7150 return TREE_TYPE (instance);
7152 return RECUR (TREE_OPERAND (instance, 0));
7154 case POINTER_PLUS_EXPR:
7155 case PLUS_EXPR:
7156 case MINUS_EXPR:
7157 if (TREE_CODE (TREE_OPERAND (instance, 0)) == ADDR_EXPR)
7158 return RECUR (TREE_OPERAND (instance, 0));
7159 if (TREE_CODE (TREE_OPERAND (instance, 1)) == INTEGER_CST)
7160 /* Propagate nonnull. */
7161 return RECUR (TREE_OPERAND (instance, 0));
7163 return NULL_TREE;
7165 CASE_CONVERT:
7166 return RECUR (TREE_OPERAND (instance, 0));
7168 case ADDR_EXPR:
7169 instance = TREE_OPERAND (instance, 0);
7170 if (nonnull)
7172 /* Just because we see an ADDR_EXPR doesn't mean we're dealing
7173 with a real object -- given &p->f, p can still be null. */
7174 tree t = get_base_address (instance);
7175 /* ??? Probably should check DECL_WEAK here. */
7176 if (t && DECL_P (t))
7177 *nonnull = 1;
7179 return RECUR (instance);
7181 case COMPONENT_REF:
7182 /* If this component is really a base class reference, then the field
7183 itself isn't definitive. */
7184 if (DECL_FIELD_IS_BASE (TREE_OPERAND (instance, 1)))
7185 return RECUR (TREE_OPERAND (instance, 0));
7186 return RECUR (TREE_OPERAND (instance, 1));
7188 case VAR_DECL:
7189 case FIELD_DECL:
7190 if (TREE_CODE (TREE_TYPE (instance)) == ARRAY_TYPE
7191 && MAYBE_CLASS_TYPE_P (TREE_TYPE (TREE_TYPE (instance))))
7193 if (nonnull)
7194 *nonnull = 1;
7195 return TREE_TYPE (TREE_TYPE (instance));
7197 /* fall through. */
7198 case TARGET_EXPR:
7199 case PARM_DECL:
7200 case RESULT_DECL:
7201 if (MAYBE_CLASS_TYPE_P (TREE_TYPE (instance)))
7203 if (nonnull)
7204 *nonnull = 1;
7205 return TREE_TYPE (instance);
7207 else if (instance == current_class_ptr)
7209 if (nonnull)
7210 *nonnull = 1;
7212 /* if we're in a ctor or dtor, we know our type. If
7213 current_class_ptr is set but we aren't in a function, we're in
7214 an NSDMI (and therefore a constructor). */
7215 if (current_scope () != current_function_decl
7216 || (DECL_LANG_SPECIFIC (current_function_decl)
7217 && (DECL_CONSTRUCTOR_P (current_function_decl)
7218 || DECL_DESTRUCTOR_P (current_function_decl))))
7220 if (cdtorp)
7221 *cdtorp = 1;
7222 return TREE_TYPE (TREE_TYPE (instance));
7225 else if (TREE_CODE (TREE_TYPE (instance)) == REFERENCE_TYPE)
7227 /* We only need one hash table because it is always left empty. */
7228 if (!fixed_type_or_null_ref_ht)
7229 fixed_type_or_null_ref_ht
7230 = new hash_table<nofree_ptr_hash<tree_node> > (37);
7232 /* Reference variables should be references to objects. */
7233 if (nonnull)
7234 *nonnull = 1;
7236 /* Enter the INSTANCE in a table to prevent recursion; a
7237 variable's initializer may refer to the variable
7238 itself. */
7239 if (VAR_P (instance)
7240 && DECL_INITIAL (instance)
7241 && !type_dependent_expression_p_push (DECL_INITIAL (instance))
7242 && !fixed_type_or_null_ref_ht->find (instance))
7244 tree type;
7245 tree_node **slot;
7247 slot = fixed_type_or_null_ref_ht->find_slot (instance, INSERT);
7248 *slot = instance;
7249 type = RECUR (DECL_INITIAL (instance));
7250 fixed_type_or_null_ref_ht->remove_elt (instance);
7252 return type;
7255 return NULL_TREE;
7257 default:
7258 return NULL_TREE;
7260 #undef RECUR
7263 /* Return nonzero if the dynamic type of INSTANCE is known, and
7264 equivalent to the static type. We also handle the case where
7265 INSTANCE is really a pointer. Return negative if this is a
7266 ctor/dtor. There the dynamic type is known, but this might not be
7267 the most derived base of the original object, and hence virtual
7268 bases may not be laid out according to this type.
7270 Used to determine whether the virtual function table is needed
7271 or not.
7273 *NONNULL is set iff INSTANCE can be known to be nonnull, regardless
7274 of our knowledge of its type. *NONNULL should be initialized
7275 before this function is called. */
7278 resolves_to_fixed_type_p (tree instance, int* nonnull)
7280 tree t = TREE_TYPE (instance);
7281 int cdtorp = 0;
7282 tree fixed;
7284 /* processing_template_decl can be false in a template if we're in
7285 instantiate_non_dependent_expr, but we still want to suppress
7286 this check. */
7287 if (in_template_function ())
7289 /* In a template we only care about the type of the result. */
7290 if (nonnull)
7291 *nonnull = true;
7292 return true;
7295 fixed = fixed_type_or_null (instance, nonnull, &cdtorp);
7296 if (fixed == NULL_TREE)
7297 return 0;
7298 if (POINTER_TYPE_P (t))
7299 t = TREE_TYPE (t);
7300 if (!same_type_ignoring_top_level_qualifiers_p (t, fixed))
7301 return 0;
7302 return cdtorp ? -1 : 1;
7306 void
7307 init_class_processing (void)
7309 current_class_depth = 0;
7310 current_class_stack_size = 10;
7311 current_class_stack
7312 = XNEWVEC (struct class_stack_node, current_class_stack_size);
7313 vec_alloc (local_classes, 8);
7314 sizeof_biggest_empty_class = size_zero_node;
7316 ridpointers[(int) RID_PUBLIC] = access_public_node;
7317 ridpointers[(int) RID_PRIVATE] = access_private_node;
7318 ridpointers[(int) RID_PROTECTED] = access_protected_node;
7321 /* Restore the cached PREVIOUS_CLASS_LEVEL. */
7323 static void
7324 restore_class_cache (void)
7326 tree type;
7328 /* We are re-entering the same class we just left, so we don't
7329 have to search the whole inheritance matrix to find all the
7330 decls to bind again. Instead, we install the cached
7331 class_shadowed list and walk through it binding names. */
7332 push_binding_level (previous_class_level);
7333 class_binding_level = previous_class_level;
7334 /* Restore IDENTIFIER_TYPE_VALUE. */
7335 for (type = class_binding_level->type_shadowed;
7336 type;
7337 type = TREE_CHAIN (type))
7338 SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (type), TREE_TYPE (type));
7341 /* Set global variables CURRENT_CLASS_NAME and CURRENT_CLASS_TYPE as
7342 appropriate for TYPE.
7344 So that we may avoid calls to lookup_name, we cache the _TYPE
7345 nodes of local TYPE_DECLs in the TREE_TYPE field of the name.
7347 For multiple inheritance, we perform a two-pass depth-first search
7348 of the type lattice. */
7350 void
7351 pushclass (tree type)
7353 class_stack_node_t csn;
7355 type = TYPE_MAIN_VARIANT (type);
7357 /* Make sure there is enough room for the new entry on the stack. */
7358 if (current_class_depth + 1 >= current_class_stack_size)
7360 current_class_stack_size *= 2;
7361 current_class_stack
7362 = XRESIZEVEC (struct class_stack_node, current_class_stack,
7363 current_class_stack_size);
7366 /* Insert a new entry on the class stack. */
7367 csn = current_class_stack + current_class_depth;
7368 csn->name = current_class_name;
7369 csn->type = current_class_type;
7370 csn->access = current_access_specifier;
7371 csn->names_used = 0;
7372 csn->hidden = 0;
7373 current_class_depth++;
7375 /* Now set up the new type. */
7376 current_class_name = TYPE_NAME (type);
7377 if (TREE_CODE (current_class_name) == TYPE_DECL)
7378 current_class_name = DECL_NAME (current_class_name);
7379 current_class_type = type;
7381 /* By default, things in classes are private, while things in
7382 structures or unions are public. */
7383 current_access_specifier = (CLASSTYPE_DECLARED_CLASS (type)
7384 ? access_private_node
7385 : access_public_node);
7387 if (previous_class_level
7388 && type != previous_class_level->this_entity
7389 && current_class_depth == 1)
7391 /* Forcibly remove any old class remnants. */
7392 invalidate_class_lookup_cache ();
7395 if (!previous_class_level
7396 || type != previous_class_level->this_entity
7397 || current_class_depth > 1)
7398 pushlevel_class ();
7399 else
7400 restore_class_cache ();
7403 /* When we exit a toplevel class scope, we save its binding level so
7404 that we can restore it quickly. Here, we've entered some other
7405 class, so we must invalidate our cache. */
7407 void
7408 invalidate_class_lookup_cache (void)
7410 previous_class_level = NULL;
7413 /* Get out of the current class scope. If we were in a class scope
7414 previously, that is the one popped to. */
7416 void
7417 popclass (void)
7419 poplevel_class ();
7421 current_class_depth--;
7422 current_class_name = current_class_stack[current_class_depth].name;
7423 current_class_type = current_class_stack[current_class_depth].type;
7424 current_access_specifier = current_class_stack[current_class_depth].access;
7425 if (current_class_stack[current_class_depth].names_used)
7426 splay_tree_delete (current_class_stack[current_class_depth].names_used);
7429 /* Mark the top of the class stack as hidden. */
7431 void
7432 push_class_stack (void)
7434 if (current_class_depth)
7435 ++current_class_stack[current_class_depth - 1].hidden;
7438 /* Mark the top of the class stack as un-hidden. */
7440 void
7441 pop_class_stack (void)
7443 if (current_class_depth)
7444 --current_class_stack[current_class_depth - 1].hidden;
7447 /* Returns 1 if the class type currently being defined is either T or
7448 a nested type of T. Returns the type from the current_class_stack,
7449 which might be equivalent to but not equal to T in case of
7450 constrained partial specializations. */
7452 tree
7453 currently_open_class (tree t)
7455 int i;
7457 if (!CLASS_TYPE_P (t))
7458 return NULL_TREE;
7460 t = TYPE_MAIN_VARIANT (t);
7462 /* We start looking from 1 because entry 0 is from global scope,
7463 and has no type. */
7464 for (i = current_class_depth; i > 0; --i)
7466 tree c;
7467 if (i == current_class_depth)
7468 c = current_class_type;
7469 else
7471 if (current_class_stack[i].hidden)
7472 break;
7473 c = current_class_stack[i].type;
7475 if (!c)
7476 continue;
7477 if (same_type_p (c, t))
7478 return c;
7480 return NULL_TREE;
7483 /* If either current_class_type or one of its enclosing classes are derived
7484 from T, return the appropriate type. Used to determine how we found
7485 something via unqualified lookup. */
7487 tree
7488 currently_open_derived_class (tree t)
7490 int i;
7492 /* The bases of a dependent type are unknown. */
7493 if (dependent_type_p (t))
7494 return NULL_TREE;
7496 if (!current_class_type)
7497 return NULL_TREE;
7499 if (DERIVED_FROM_P (t, current_class_type))
7500 return current_class_type;
7502 for (i = current_class_depth - 1; i > 0; --i)
7504 if (current_class_stack[i].hidden)
7505 break;
7506 if (DERIVED_FROM_P (t, current_class_stack[i].type))
7507 return current_class_stack[i].type;
7510 return NULL_TREE;
7513 /* Return the outermost enclosing class type that is still open, or
7514 NULL_TREE. */
7516 tree
7517 outermost_open_class (void)
7519 if (!current_class_type)
7520 return NULL_TREE;
7521 tree r = NULL_TREE;
7522 if (TYPE_BEING_DEFINED (current_class_type))
7523 r = current_class_type;
7524 for (int i = current_class_depth - 1; i > 0; --i)
7526 if (current_class_stack[i].hidden)
7527 break;
7528 tree t = current_class_stack[i].type;
7529 if (!TYPE_BEING_DEFINED (t))
7530 break;
7531 r = t;
7533 return r;
7536 /* Returns the innermost class type which is not a lambda closure type. */
7538 tree
7539 current_nonlambda_class_type (void)
7541 tree type = current_class_type;
7542 while (type && LAMBDA_TYPE_P (type))
7543 type = decl_type_context (TYPE_NAME (type));
7544 return type;
7547 /* When entering a class scope, all enclosing class scopes' names with
7548 static meaning (static variables, static functions, types and
7549 enumerators) have to be visible. This recursive function calls
7550 pushclass for all enclosing class contexts until global or a local
7551 scope is reached. TYPE is the enclosed class. */
7553 void
7554 push_nested_class (tree type)
7556 /* A namespace might be passed in error cases, like A::B:C. */
7557 if (type == NULL_TREE
7558 || !CLASS_TYPE_P (type))
7559 return;
7561 push_nested_class (DECL_CONTEXT (TYPE_MAIN_DECL (type)));
7563 pushclass (type);
7566 /* Undoes a push_nested_class call. */
7568 void
7569 pop_nested_class (void)
7571 tree context = DECL_CONTEXT (TYPE_MAIN_DECL (current_class_type));
7573 popclass ();
7574 if (context && CLASS_TYPE_P (context))
7575 pop_nested_class ();
7578 /* Returns the number of extern "LANG" blocks we are nested within. */
7581 current_lang_depth (void)
7583 return vec_safe_length (current_lang_base);
7586 /* Set global variables CURRENT_LANG_NAME to appropriate value
7587 so that behavior of name-mangling machinery is correct. */
7589 void
7590 push_lang_context (tree name)
7592 vec_safe_push (current_lang_base, current_lang_name);
7594 if (name == lang_name_cplusplus)
7595 current_lang_name = name;
7596 else if (name == lang_name_c)
7597 current_lang_name = name;
7598 else
7599 error ("language string %<\"%E\"%> not recognized", name);
7602 /* Get out of the current language scope. */
7604 void
7605 pop_lang_context (void)
7607 current_lang_name = current_lang_base->pop ();
7610 /* Type instantiation routines. */
7612 /* Given an OVERLOAD and a TARGET_TYPE, return the function that
7613 matches the TARGET_TYPE. If there is no satisfactory match, return
7614 error_mark_node, and issue an error & warning messages under
7615 control of FLAGS. Permit pointers to member function if FLAGS
7616 permits. If TEMPLATE_ONLY, the name of the overloaded function was
7617 a template-id, and EXPLICIT_TARGS are the explicitly provided
7618 template arguments.
7620 If OVERLOAD is for one or more member functions, then ACCESS_PATH
7621 is the base path used to reference those member functions. If
7622 the address is resolved to a member function, access checks will be
7623 performed and errors issued if appropriate. */
7625 static tree
7626 resolve_address_of_overloaded_function (tree target_type,
7627 tree overload,
7628 tsubst_flags_t complain,
7629 bool template_only,
7630 tree explicit_targs,
7631 tree access_path)
7633 /* Here's what the standard says:
7635 [over.over]
7637 If the name is a function template, template argument deduction
7638 is done, and if the argument deduction succeeds, the deduced
7639 arguments are used to generate a single template function, which
7640 is added to the set of overloaded functions considered.
7642 Non-member functions and static member functions match targets of
7643 type "pointer-to-function" or "reference-to-function." Nonstatic
7644 member functions match targets of type "pointer-to-member
7645 function;" the function type of the pointer to member is used to
7646 select the member function from the set of overloaded member
7647 functions. If a nonstatic member function is selected, the
7648 reference to the overloaded function name is required to have the
7649 form of a pointer to member as described in 5.3.1.
7651 If more than one function is selected, any template functions in
7652 the set are eliminated if the set also contains a non-template
7653 function, and any given template function is eliminated if the
7654 set contains a second template function that is more specialized
7655 than the first according to the partial ordering rules 14.5.5.2.
7656 After such eliminations, if any, there shall remain exactly one
7657 selected function. */
7659 int is_ptrmem = 0;
7660 /* We store the matches in a TREE_LIST rooted here. The functions
7661 are the TREE_PURPOSE, not the TREE_VALUE, in this list, for easy
7662 interoperability with most_specialized_instantiation. */
7663 tree matches = NULL_TREE;
7664 tree fn;
7665 tree target_fn_type;
7667 /* By the time we get here, we should be seeing only real
7668 pointer-to-member types, not the internal POINTER_TYPE to
7669 METHOD_TYPE representation. */
7670 gcc_assert (!TYPE_PTR_P (target_type)
7671 || TREE_CODE (TREE_TYPE (target_type)) != METHOD_TYPE);
7673 gcc_assert (is_overloaded_fn (overload));
7675 /* Check that the TARGET_TYPE is reasonable. */
7676 if (TYPE_PTRFN_P (target_type)
7677 || TYPE_REFFN_P (target_type))
7678 /* This is OK. */;
7679 else if (TYPE_PTRMEMFUNC_P (target_type))
7680 /* This is OK, too. */
7681 is_ptrmem = 1;
7682 else if (TREE_CODE (target_type) == FUNCTION_TYPE)
7683 /* This is OK, too. This comes from a conversion to reference
7684 type. */
7685 target_type = build_reference_type (target_type);
7686 else
7688 if (complain & tf_error)
7689 error ("cannot resolve overloaded function %qD based on"
7690 " conversion to type %qT",
7691 OVL_NAME (overload), target_type);
7692 return error_mark_node;
7695 /* Non-member functions and static member functions match targets of type
7696 "pointer-to-function" or "reference-to-function." Nonstatic member
7697 functions match targets of type "pointer-to-member-function;" the
7698 function type of the pointer to member is used to select the member
7699 function from the set of overloaded member functions.
7701 So figure out the FUNCTION_TYPE that we want to match against. */
7702 target_fn_type = static_fn_type (target_type);
7704 /* If we can find a non-template function that matches, we can just
7705 use it. There's no point in generating template instantiations
7706 if we're just going to throw them out anyhow. But, of course, we
7707 can only do this when we don't *need* a template function. */
7708 if (!template_only)
7709 for (lkp_iterator iter (overload); iter; ++iter)
7711 tree fn = *iter;
7713 if (TREE_CODE (fn) == TEMPLATE_DECL)
7714 /* We're not looking for templates just yet. */
7715 continue;
7717 if ((TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE) != is_ptrmem)
7718 /* We're looking for a non-static member, and this isn't
7719 one, or vice versa. */
7720 continue;
7722 /* In C++17 we need the noexcept-qualifier to compare types. */
7723 if (flag_noexcept_type
7724 && !maybe_instantiate_noexcept (fn, complain))
7725 continue;
7727 /* See if there's a match. */
7728 tree fntype = static_fn_type (fn);
7729 if (same_type_p (target_fn_type, fntype)
7730 || fnptr_conv_p (target_fn_type, fntype))
7731 matches = tree_cons (fn, NULL_TREE, matches);
7734 /* Now, if we've already got a match (or matches), there's no need
7735 to proceed to the template functions. But, if we don't have a
7736 match we need to look at them, too. */
7737 if (!matches)
7739 tree target_arg_types;
7740 tree target_ret_type;
7741 tree *args;
7742 unsigned int nargs, ia;
7743 tree arg;
7745 target_arg_types = TYPE_ARG_TYPES (target_fn_type);
7746 target_ret_type = TREE_TYPE (target_fn_type);
7748 nargs = list_length (target_arg_types);
7749 args = XALLOCAVEC (tree, nargs);
7750 for (arg = target_arg_types, ia = 0;
7751 arg != NULL_TREE && arg != void_list_node;
7752 arg = TREE_CHAIN (arg), ++ia)
7753 args[ia] = TREE_VALUE (arg);
7754 nargs = ia;
7756 for (lkp_iterator iter (overload); iter; ++iter)
7758 tree fn = *iter;
7759 tree instantiation;
7760 tree targs;
7762 if (TREE_CODE (fn) != TEMPLATE_DECL)
7763 /* We're only looking for templates. */
7764 continue;
7766 if ((TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
7767 != is_ptrmem)
7768 /* We're not looking for a non-static member, and this is
7769 one, or vice versa. */
7770 continue;
7772 tree ret = target_ret_type;
7774 /* If the template has a deduced return type, don't expose it to
7775 template argument deduction. */
7776 if (undeduced_auto_decl (fn))
7777 ret = NULL_TREE;
7779 /* Try to do argument deduction. */
7780 targs = make_tree_vec (DECL_NTPARMS (fn));
7781 instantiation = fn_type_unification (fn, explicit_targs, targs, args,
7782 nargs, ret,
7783 DEDUCE_EXACT, LOOKUP_NORMAL,
7784 false, false);
7785 if (instantiation == error_mark_node)
7786 /* Instantiation failed. */
7787 continue;
7789 /* Constraints must be satisfied. This is done before
7790 return type deduction since that instantiates the
7791 function. */
7792 if (flag_concepts && !constraints_satisfied_p (instantiation))
7793 continue;
7795 /* And now force instantiation to do return type deduction. */
7796 if (undeduced_auto_decl (instantiation))
7798 ++function_depth;
7799 instantiate_decl (instantiation, /*defer*/false, /*class*/false);
7800 --function_depth;
7802 require_deduced_type (instantiation);
7805 /* In C++17 we need the noexcept-qualifier to compare types. */
7806 if (flag_noexcept_type)
7807 maybe_instantiate_noexcept (instantiation, complain);
7809 /* See if there's a match. */
7810 tree fntype = static_fn_type (instantiation);
7811 if (same_type_p (target_fn_type, fntype)
7812 || fnptr_conv_p (target_fn_type, fntype))
7813 matches = tree_cons (instantiation, fn, matches);
7816 /* Now, remove all but the most specialized of the matches. */
7817 if (matches)
7819 tree match = most_specialized_instantiation (matches);
7821 if (match != error_mark_node)
7822 matches = tree_cons (TREE_PURPOSE (match),
7823 NULL_TREE,
7824 NULL_TREE);
7828 /* Now we should have exactly one function in MATCHES. */
7829 if (matches == NULL_TREE)
7831 /* There were *no* matches. */
7832 if (complain & tf_error)
7834 error ("no matches converting function %qD to type %q#T",
7835 OVL_NAME (overload), target_type);
7837 print_candidates (overload);
7839 return error_mark_node;
7841 else if (TREE_CHAIN (matches))
7843 /* There were too many matches. First check if they're all
7844 the same function. */
7845 tree match = NULL_TREE;
7847 fn = TREE_PURPOSE (matches);
7849 /* For multi-versioned functions, more than one match is just fine and
7850 decls_match will return false as they are different. */
7851 for (match = TREE_CHAIN (matches); match; match = TREE_CHAIN (match))
7852 if (!decls_match (fn, TREE_PURPOSE (match))
7853 && !targetm.target_option.function_versions
7854 (fn, TREE_PURPOSE (match)))
7855 break;
7857 if (match)
7859 if (complain & tf_error)
7861 error ("converting overloaded function %qD to type %q#T is ambiguous",
7862 OVL_NAME (overload), target_type);
7864 /* Since print_candidates expects the functions in the
7865 TREE_VALUE slot, we flip them here. */
7866 for (match = matches; match; match = TREE_CHAIN (match))
7867 TREE_VALUE (match) = TREE_PURPOSE (match);
7869 print_candidates (matches);
7872 return error_mark_node;
7876 /* Good, exactly one match. Now, convert it to the correct type. */
7877 fn = TREE_PURPOSE (matches);
7879 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
7880 && !(complain & tf_ptrmem_ok) && !flag_ms_extensions)
7882 static int explained;
7884 if (!(complain & tf_error))
7885 return error_mark_node;
7887 permerror (input_location, "assuming pointer to member %qD", fn);
7888 if (!explained)
7890 inform (input_location, "(a pointer to member can only be formed with %<&%E%>)", fn);
7891 explained = 1;
7895 /* If a pointer to a function that is multi-versioned is requested, the
7896 pointer to the dispatcher function is returned instead. This works
7897 well because indirectly calling the function will dispatch the right
7898 function version at run-time. */
7899 if (DECL_FUNCTION_VERSIONED (fn))
7901 fn = get_function_version_dispatcher (fn);
7902 if (fn == NULL)
7903 return error_mark_node;
7904 /* Mark all the versions corresponding to the dispatcher as used. */
7905 if (!(complain & tf_conv))
7906 mark_versions_used (fn);
7909 /* If we're doing overload resolution purely for the purpose of
7910 determining conversion sequences, we should not consider the
7911 function used. If this conversion sequence is selected, the
7912 function will be marked as used at this point. */
7913 if (!(complain & tf_conv))
7915 /* Make =delete work with SFINAE. */
7916 if (DECL_DELETED_FN (fn) && !(complain & tf_error))
7917 return error_mark_node;
7918 if (!mark_used (fn, complain) && !(complain & tf_error))
7919 return error_mark_node;
7922 /* We could not check access to member functions when this
7923 expression was originally created since we did not know at that
7924 time to which function the expression referred. */
7925 if (DECL_FUNCTION_MEMBER_P (fn))
7927 gcc_assert (access_path);
7928 perform_or_defer_access_check (access_path, fn, fn, complain);
7931 if (TYPE_PTRFN_P (target_type) || TYPE_PTRMEMFUNC_P (target_type))
7932 return cp_build_addr_expr (fn, complain);
7933 else
7935 /* The target must be a REFERENCE_TYPE. Above, cp_build_unary_op
7936 will mark the function as addressed, but here we must do it
7937 explicitly. */
7938 cxx_mark_addressable (fn);
7940 return fn;
7944 /* This function will instantiate the type of the expression given in
7945 RHS to match the type of LHSTYPE. If errors exist, then return
7946 error_mark_node. COMPLAIN is a bit mask. If TF_ERROR is set, then
7947 we complain on errors. If we are not complaining, never modify rhs,
7948 as overload resolution wants to try many possible instantiations, in
7949 the hope that at least one will work.
7951 For non-recursive calls, LHSTYPE should be a function, pointer to
7952 function, or a pointer to member function. */
7954 tree
7955 instantiate_type (tree lhstype, tree rhs, tsubst_flags_t complain)
7957 tsubst_flags_t complain_in = complain;
7958 tree access_path = NULL_TREE;
7960 complain &= ~tf_ptrmem_ok;
7962 if (lhstype == unknown_type_node)
7964 if (complain & tf_error)
7965 error ("not enough type information");
7966 return error_mark_node;
7969 if (TREE_TYPE (rhs) != NULL_TREE && ! (type_unknown_p (rhs)))
7971 tree fntype = non_reference (lhstype);
7972 if (same_type_p (fntype, TREE_TYPE (rhs)))
7973 return rhs;
7974 if (fnptr_conv_p (fntype, TREE_TYPE (rhs)))
7975 return rhs;
7976 if (flag_ms_extensions
7977 && TYPE_PTRMEMFUNC_P (fntype)
7978 && !TYPE_PTRMEMFUNC_P (TREE_TYPE (rhs)))
7979 /* Microsoft allows `A::f' to be resolved to a
7980 pointer-to-member. */
7982 else
7984 if (complain & tf_error)
7985 error ("cannot convert %qE from type %qT to type %qT",
7986 rhs, TREE_TYPE (rhs), fntype);
7987 return error_mark_node;
7991 if (BASELINK_P (rhs))
7993 access_path = BASELINK_ACCESS_BINFO (rhs);
7994 rhs = BASELINK_FUNCTIONS (rhs);
7997 /* If we are in a template, and have a NON_DEPENDENT_EXPR, we cannot
7998 deduce any type information. */
7999 if (TREE_CODE (rhs) == NON_DEPENDENT_EXPR)
8001 if (complain & tf_error)
8002 error ("not enough type information");
8003 return error_mark_node;
8006 /* If we instantiate a template, and it is a A ?: C expression
8007 with omitted B, look through the SAVE_EXPR. */
8008 if (TREE_CODE (rhs) == SAVE_EXPR)
8009 rhs = TREE_OPERAND (rhs, 0);
8011 /* There are only a few kinds of expressions that may have a type
8012 dependent on overload resolution. */
8013 gcc_assert (TREE_CODE (rhs) == ADDR_EXPR
8014 || TREE_CODE (rhs) == COMPONENT_REF
8015 || is_overloaded_fn (rhs)
8016 || (flag_ms_extensions && TREE_CODE (rhs) == FUNCTION_DECL));
8018 /* This should really only be used when attempting to distinguish
8019 what sort of a pointer to function we have. For now, any
8020 arithmetic operation which is not supported on pointers
8021 is rejected as an error. */
8023 switch (TREE_CODE (rhs))
8025 case COMPONENT_REF:
8027 tree member = TREE_OPERAND (rhs, 1);
8029 member = instantiate_type (lhstype, member, complain);
8030 if (member != error_mark_node
8031 && TREE_SIDE_EFFECTS (TREE_OPERAND (rhs, 0)))
8032 /* Do not lose object's side effects. */
8033 return build2 (COMPOUND_EXPR, TREE_TYPE (member),
8034 TREE_OPERAND (rhs, 0), member);
8035 return member;
8038 case OFFSET_REF:
8039 rhs = TREE_OPERAND (rhs, 1);
8040 if (BASELINK_P (rhs))
8041 return instantiate_type (lhstype, rhs, complain_in);
8043 /* This can happen if we are forming a pointer-to-member for a
8044 member template. */
8045 gcc_assert (TREE_CODE (rhs) == TEMPLATE_ID_EXPR);
8047 /* Fall through. */
8049 case TEMPLATE_ID_EXPR:
8051 tree fns = TREE_OPERAND (rhs, 0);
8052 tree args = TREE_OPERAND (rhs, 1);
8054 return
8055 resolve_address_of_overloaded_function (lhstype, fns, complain_in,
8056 /*template_only=*/true,
8057 args, access_path);
8060 case OVERLOAD:
8061 case FUNCTION_DECL:
8062 return
8063 resolve_address_of_overloaded_function (lhstype, rhs, complain_in,
8064 /*template_only=*/false,
8065 /*explicit_targs=*/NULL_TREE,
8066 access_path);
8068 case ADDR_EXPR:
8070 if (PTRMEM_OK_P (rhs))
8071 complain |= tf_ptrmem_ok;
8073 return instantiate_type (lhstype, TREE_OPERAND (rhs, 0), complain);
8076 case ERROR_MARK:
8077 return error_mark_node;
8079 default:
8080 gcc_unreachable ();
8082 return error_mark_node;
8085 /* Return the name of the virtual function pointer field
8086 (as an IDENTIFIER_NODE) for the given TYPE. Note that
8087 this may have to look back through base types to find the
8088 ultimate field name. (For single inheritance, these could
8089 all be the same name. Who knows for multiple inheritance). */
8091 static tree
8092 get_vfield_name (tree type)
8094 tree binfo, base_binfo;
8096 for (binfo = TYPE_BINFO (type);
8097 BINFO_N_BASE_BINFOS (binfo);
8098 binfo = base_binfo)
8100 base_binfo = BINFO_BASE_BINFO (binfo, 0);
8102 if (BINFO_VIRTUAL_P (base_binfo)
8103 || !TYPE_CONTAINS_VPTR_P (BINFO_TYPE (base_binfo)))
8104 break;
8107 type = BINFO_TYPE (binfo);
8108 tree ctor_name = constructor_name (type);
8109 char *buf = (char *) alloca (sizeof (VFIELD_NAME_FORMAT)
8110 + IDENTIFIER_LENGTH (ctor_name) + 2);
8111 sprintf (buf, VFIELD_NAME_FORMAT, IDENTIFIER_POINTER (ctor_name));
8112 return get_identifier (buf);
8115 /* Build a dummy reference to ourselves so Derived::Base (and A::A) works,
8116 according to [class]:
8117 The class-name is also inserted
8118 into the scope of the class itself. For purposes of access checking,
8119 the inserted class name is treated as if it were a public member name. */
8121 void
8122 build_self_reference (void)
8124 tree name = DECL_NAME (TYPE_NAME (current_class_type));
8125 tree value = build_lang_decl (TYPE_DECL, name, current_class_type);
8127 DECL_NONLOCAL (value) = 1;
8128 DECL_CONTEXT (value) = current_class_type;
8129 DECL_ARTIFICIAL (value) = 1;
8130 SET_DECL_SELF_REFERENCE_P (value);
8131 set_underlying_type (value);
8133 if (processing_template_decl)
8134 value = push_template_decl (value);
8136 tree saved_cas = current_access_specifier;
8137 current_access_specifier = access_public_node;
8138 finish_member_declaration (value);
8139 current_access_specifier = saved_cas;
8142 /* Returns 1 if TYPE contains only padding bytes. */
8145 is_empty_class (tree type)
8147 if (type == error_mark_node)
8148 return 0;
8150 if (! CLASS_TYPE_P (type))
8151 return 0;
8153 return CLASSTYPE_EMPTY_P (type);
8156 /* Returns true if TYPE contains no actual data, just various
8157 possible combinations of empty classes and possibly a vptr. */
8159 bool
8160 is_really_empty_class (tree type)
8162 if (CLASS_TYPE_P (type))
8164 tree field;
8165 tree binfo;
8166 tree base_binfo;
8167 int i;
8169 /* CLASSTYPE_EMPTY_P isn't set properly until the class is actually laid
8170 out, but we'd like to be able to check this before then. */
8171 if (COMPLETE_TYPE_P (type) && is_empty_class (type))
8172 return true;
8174 for (binfo = TYPE_BINFO (type), i = 0;
8175 BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
8176 if (!is_really_empty_class (BINFO_TYPE (base_binfo)))
8177 return false;
8178 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
8179 if (TREE_CODE (field) == FIELD_DECL
8180 && !DECL_ARTIFICIAL (field)
8181 /* An unnamed bit-field is not a data member. */
8182 && !DECL_UNNAMED_BIT_FIELD (field)
8183 && !is_really_empty_class (TREE_TYPE (field)))
8184 return false;
8185 return true;
8187 else if (TREE_CODE (type) == ARRAY_TYPE)
8188 return (integer_zerop (array_type_nelts_top (type))
8189 || is_really_empty_class (TREE_TYPE (type)));
8190 return false;
8193 /* Note that NAME was looked up while the current class was being
8194 defined and that the result of that lookup was DECL. */
8196 void
8197 maybe_note_name_used_in_class (tree name, tree decl)
8199 splay_tree names_used;
8201 /* If we're not defining a class, there's nothing to do. */
8202 if (!(innermost_scope_kind() == sk_class
8203 && TYPE_BEING_DEFINED (current_class_type)
8204 && !LAMBDA_TYPE_P (current_class_type)))
8205 return;
8207 /* If there's already a binding for this NAME, then we don't have
8208 anything to worry about. */
8209 if (lookup_member (current_class_type, name,
8210 /*protect=*/0, /*want_type=*/false, tf_warning_or_error))
8211 return;
8213 if (!current_class_stack[current_class_depth - 1].names_used)
8214 current_class_stack[current_class_depth - 1].names_used
8215 = splay_tree_new (splay_tree_compare_pointers, 0, 0);
8216 names_used = current_class_stack[current_class_depth - 1].names_used;
8218 splay_tree_insert (names_used,
8219 (splay_tree_key) name,
8220 (splay_tree_value) decl);
8223 /* Note that NAME was declared (as DECL) in the current class. Check
8224 to see that the declaration is valid. */
8226 void
8227 note_name_declared_in_class (tree name, tree decl)
8229 splay_tree names_used;
8230 splay_tree_node n;
8232 /* Look to see if we ever used this name. */
8233 names_used
8234 = current_class_stack[current_class_depth - 1].names_used;
8235 if (!names_used)
8236 return;
8237 /* The C language allows members to be declared with a type of the same
8238 name, and the C++ standard says this diagnostic is not required. So
8239 allow it in extern "C" blocks unless predantic is specified.
8240 Allow it in all cases if -ms-extensions is specified. */
8241 if ((!pedantic && current_lang_name == lang_name_c)
8242 || flag_ms_extensions)
8243 return;
8244 n = splay_tree_lookup (names_used, (splay_tree_key) name);
8245 if (n)
8247 /* [basic.scope.class]
8249 A name N used in a class S shall refer to the same declaration
8250 in its context and when re-evaluated in the completed scope of
8251 S. */
8252 permerror (input_location, "declaration of %q#D", decl);
8253 permerror (location_of ((tree) n->value),
8254 "changes meaning of %qD from %q#D",
8255 OVL_NAME (decl), (tree) n->value);
8259 /* Returns the VAR_DECL for the complete vtable associated with BINFO.
8260 Secondary vtables are merged with primary vtables; this function
8261 will return the VAR_DECL for the primary vtable. */
8263 tree
8264 get_vtbl_decl_for_binfo (tree binfo)
8266 tree decl;
8268 decl = BINFO_VTABLE (binfo);
8269 if (decl && TREE_CODE (decl) == POINTER_PLUS_EXPR)
8271 gcc_assert (TREE_CODE (TREE_OPERAND (decl, 0)) == ADDR_EXPR);
8272 decl = TREE_OPERAND (TREE_OPERAND (decl, 0), 0);
8274 if (decl)
8275 gcc_assert (VAR_P (decl));
8276 return decl;
8280 /* Returns the binfo for the primary base of BINFO. If the resulting
8281 BINFO is a virtual base, and it is inherited elsewhere in the
8282 hierarchy, then the returned binfo might not be the primary base of
8283 BINFO in the complete object. Check BINFO_PRIMARY_P or
8284 BINFO_LOST_PRIMARY_P to be sure. */
8286 static tree
8287 get_primary_binfo (tree binfo)
8289 tree primary_base;
8291 primary_base = CLASSTYPE_PRIMARY_BINFO (BINFO_TYPE (binfo));
8292 if (!primary_base)
8293 return NULL_TREE;
8295 return copied_binfo (primary_base, binfo);
8298 /* As above, but iterate until we reach the binfo that actually provides the
8299 vptr for BINFO. */
8301 static tree
8302 most_primary_binfo (tree binfo)
8304 tree b = binfo;
8305 while (CLASSTYPE_HAS_PRIMARY_BASE_P (BINFO_TYPE (b))
8306 && !BINFO_LOST_PRIMARY_P (b))
8308 tree primary_base = get_primary_binfo (b);
8309 gcc_assert (BINFO_PRIMARY_P (primary_base)
8310 && BINFO_INHERITANCE_CHAIN (primary_base) == b);
8311 b = primary_base;
8313 return b;
8316 /* Returns true if BINFO gets its vptr from a virtual base of the most derived
8317 type. Note that the virtual inheritance might be above or below BINFO in
8318 the hierarchy. */
8320 bool
8321 vptr_via_virtual_p (tree binfo)
8323 if (TYPE_P (binfo))
8324 binfo = TYPE_BINFO (binfo);
8325 tree primary = most_primary_binfo (binfo);
8326 /* Don't limit binfo_via_virtual, we want to return true when BINFO itself is
8327 a morally virtual base. */
8328 tree virt = binfo_via_virtual (primary, NULL_TREE);
8329 return virt != NULL_TREE;
8332 /* If INDENTED_P is zero, indent to INDENT. Return nonzero. */
8334 static int
8335 maybe_indent_hierarchy (FILE * stream, int indent, int indented_p)
8337 if (!indented_p)
8338 fprintf (stream, "%*s", indent, "");
8339 return 1;
8342 /* Dump the offsets of all the bases rooted at BINFO to STREAM.
8343 INDENT should be zero when called from the top level; it is
8344 incremented recursively. IGO indicates the next expected BINFO in
8345 inheritance graph ordering. */
8347 static tree
8348 dump_class_hierarchy_r (FILE *stream,
8349 dump_flags_t flags,
8350 tree binfo,
8351 tree igo,
8352 int indent)
8354 int indented = 0;
8355 tree base_binfo;
8356 int i;
8358 indented = maybe_indent_hierarchy (stream, indent, 0);
8359 fprintf (stream, "%s (0x" HOST_WIDE_INT_PRINT_HEX ") ",
8360 type_as_string (BINFO_TYPE (binfo), TFF_PLAIN_IDENTIFIER),
8361 (HOST_WIDE_INT) (uintptr_t) binfo);
8362 if (binfo != igo)
8364 fprintf (stream, "alternative-path\n");
8365 return igo;
8367 igo = TREE_CHAIN (binfo);
8369 fprintf (stream, HOST_WIDE_INT_PRINT_DEC,
8370 tree_to_shwi (BINFO_OFFSET (binfo)));
8371 if (is_empty_class (BINFO_TYPE (binfo)))
8372 fprintf (stream, " empty");
8373 else if (CLASSTYPE_NEARLY_EMPTY_P (BINFO_TYPE (binfo)))
8374 fprintf (stream, " nearly-empty");
8375 if (BINFO_VIRTUAL_P (binfo))
8376 fprintf (stream, " virtual");
8377 fprintf (stream, "\n");
8379 indented = 0;
8380 if (BINFO_PRIMARY_P (binfo))
8382 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
8383 fprintf (stream, " primary-for %s (0x" HOST_WIDE_INT_PRINT_HEX ")",
8384 type_as_string (BINFO_TYPE (BINFO_INHERITANCE_CHAIN (binfo)),
8385 TFF_PLAIN_IDENTIFIER),
8386 (HOST_WIDE_INT) (uintptr_t) BINFO_INHERITANCE_CHAIN (binfo));
8388 if (BINFO_LOST_PRIMARY_P (binfo))
8390 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
8391 fprintf (stream, " lost-primary");
8393 if (indented)
8394 fprintf (stream, "\n");
8396 if (!(flags & TDF_SLIM))
8398 int indented = 0;
8400 if (BINFO_SUBVTT_INDEX (binfo))
8402 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
8403 fprintf (stream, " subvttidx=%s",
8404 expr_as_string (BINFO_SUBVTT_INDEX (binfo),
8405 TFF_PLAIN_IDENTIFIER));
8407 if (BINFO_VPTR_INDEX (binfo))
8409 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
8410 fprintf (stream, " vptridx=%s",
8411 expr_as_string (BINFO_VPTR_INDEX (binfo),
8412 TFF_PLAIN_IDENTIFIER));
8414 if (BINFO_VPTR_FIELD (binfo))
8416 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
8417 fprintf (stream, " vbaseoffset=%s",
8418 expr_as_string (BINFO_VPTR_FIELD (binfo),
8419 TFF_PLAIN_IDENTIFIER));
8421 if (BINFO_VTABLE (binfo))
8423 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
8424 fprintf (stream, " vptr=%s",
8425 expr_as_string (BINFO_VTABLE (binfo),
8426 TFF_PLAIN_IDENTIFIER));
8429 if (indented)
8430 fprintf (stream, "\n");
8433 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
8434 igo = dump_class_hierarchy_r (stream, flags, base_binfo, igo, indent + 2);
8436 return igo;
8439 /* Dump the BINFO hierarchy for T. */
8441 static void
8442 dump_class_hierarchy_1 (FILE *stream, dump_flags_t flags, tree t)
8444 fprintf (stream, "Class %s\n", type_as_string (t, TFF_PLAIN_IDENTIFIER));
8445 fprintf (stream, " size=%lu align=%lu\n",
8446 (unsigned long)(tree_to_shwi (TYPE_SIZE (t)) / BITS_PER_UNIT),
8447 (unsigned long)(TYPE_ALIGN (t) / BITS_PER_UNIT));
8448 fprintf (stream, " base size=%lu base align=%lu\n",
8449 (unsigned long)(tree_to_shwi (TYPE_SIZE (CLASSTYPE_AS_BASE (t)))
8450 / BITS_PER_UNIT),
8451 (unsigned long)(TYPE_ALIGN (CLASSTYPE_AS_BASE (t))
8452 / BITS_PER_UNIT));
8453 dump_class_hierarchy_r (stream, flags, TYPE_BINFO (t), TYPE_BINFO (t), 0);
8454 fprintf (stream, "\n");
8457 /* Debug interface to hierarchy dumping. */
8459 void
8460 debug_class (tree t)
8462 dump_class_hierarchy_1 (stderr, TDF_SLIM, t);
8465 static void
8466 dump_class_hierarchy (tree t)
8468 dump_flags_t flags;
8469 if (FILE *stream = dump_begin (class_dump_id, &flags))
8471 dump_class_hierarchy_1 (stream, flags, t);
8472 dump_end (class_dump_id, stream);
8476 static void
8477 dump_array (FILE * stream, tree decl)
8479 tree value;
8480 unsigned HOST_WIDE_INT ix;
8481 HOST_WIDE_INT elt;
8482 tree size = TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (decl)));
8484 elt = (tree_to_shwi (TYPE_SIZE (TREE_TYPE (TREE_TYPE (decl))))
8485 / BITS_PER_UNIT);
8486 fprintf (stream, "%s:", decl_as_string (decl, TFF_PLAIN_IDENTIFIER));
8487 fprintf (stream, " %s entries",
8488 expr_as_string (size_binop (PLUS_EXPR, size, size_one_node),
8489 TFF_PLAIN_IDENTIFIER));
8490 fprintf (stream, "\n");
8492 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (DECL_INITIAL (decl)),
8493 ix, value)
8494 fprintf (stream, "%-4ld %s\n", (long)(ix * elt),
8495 expr_as_string (value, TFF_PLAIN_IDENTIFIER));
8498 static void
8499 dump_vtable (tree t, tree binfo, tree vtable)
8501 dump_flags_t flags;
8502 FILE *stream = dump_begin (class_dump_id, &flags);
8504 if (!stream)
8505 return;
8507 if (!(flags & TDF_SLIM))
8509 int ctor_vtbl_p = TYPE_BINFO (t) != binfo;
8511 fprintf (stream, "%s for %s",
8512 ctor_vtbl_p ? "Construction vtable" : "Vtable",
8513 type_as_string (BINFO_TYPE (binfo), TFF_PLAIN_IDENTIFIER));
8514 if (ctor_vtbl_p)
8516 if (!BINFO_VIRTUAL_P (binfo))
8517 fprintf (stream, " (0x" HOST_WIDE_INT_PRINT_HEX " instance)",
8518 (HOST_WIDE_INT) (uintptr_t) binfo);
8519 fprintf (stream, " in %s", type_as_string (t, TFF_PLAIN_IDENTIFIER));
8521 fprintf (stream, "\n");
8522 dump_array (stream, vtable);
8523 fprintf (stream, "\n");
8526 dump_end (class_dump_id, stream);
8529 static void
8530 dump_vtt (tree t, tree vtt)
8532 dump_flags_t flags;
8533 FILE *stream = dump_begin (class_dump_id, &flags);
8535 if (!stream)
8536 return;
8538 if (!(flags & TDF_SLIM))
8540 fprintf (stream, "VTT for %s\n",
8541 type_as_string (t, TFF_PLAIN_IDENTIFIER));
8542 dump_array (stream, vtt);
8543 fprintf (stream, "\n");
8546 dump_end (class_dump_id, stream);
8549 /* Dump a function or thunk and its thunkees. */
8551 static void
8552 dump_thunk (FILE *stream, int indent, tree thunk)
8554 static const char spaces[] = " ";
8555 tree name = DECL_NAME (thunk);
8556 tree thunks;
8558 fprintf (stream, "%.*s%p %s %s", indent, spaces,
8559 (void *)thunk,
8560 !DECL_THUNK_P (thunk) ? "function"
8561 : DECL_THIS_THUNK_P (thunk) ? "this-thunk" : "covariant-thunk",
8562 name ? IDENTIFIER_POINTER (name) : "<unset>");
8563 if (DECL_THUNK_P (thunk))
8565 HOST_WIDE_INT fixed_adjust = THUNK_FIXED_OFFSET (thunk);
8566 tree virtual_adjust = THUNK_VIRTUAL_OFFSET (thunk);
8568 fprintf (stream, " fixed=" HOST_WIDE_INT_PRINT_DEC, fixed_adjust);
8569 if (!virtual_adjust)
8570 /*NOP*/;
8571 else if (DECL_THIS_THUNK_P (thunk))
8572 fprintf (stream, " vcall=" HOST_WIDE_INT_PRINT_DEC,
8573 tree_to_shwi (virtual_adjust));
8574 else
8575 fprintf (stream, " vbase=" HOST_WIDE_INT_PRINT_DEC "(%s)",
8576 tree_to_shwi (BINFO_VPTR_FIELD (virtual_adjust)),
8577 type_as_string (BINFO_TYPE (virtual_adjust), TFF_SCOPE));
8578 if (THUNK_ALIAS (thunk))
8579 fprintf (stream, " alias to %p", (void *)THUNK_ALIAS (thunk));
8581 fprintf (stream, "\n");
8582 for (thunks = DECL_THUNKS (thunk); thunks; thunks = TREE_CHAIN (thunks))
8583 dump_thunk (stream, indent + 2, thunks);
8586 /* Dump the thunks for FN. */
8588 void
8589 debug_thunks (tree fn)
8591 dump_thunk (stderr, 0, fn);
8594 /* Virtual function table initialization. */
8596 /* Create all the necessary vtables for T and its base classes. */
8598 static void
8599 finish_vtbls (tree t)
8601 tree vbase;
8602 vec<constructor_elt, va_gc> *v = NULL;
8603 tree vtable = BINFO_VTABLE (TYPE_BINFO (t));
8605 /* We lay out the primary and secondary vtables in one contiguous
8606 vtable. The primary vtable is first, followed by the non-virtual
8607 secondary vtables in inheritance graph order. */
8608 accumulate_vtbl_inits (TYPE_BINFO (t), TYPE_BINFO (t), TYPE_BINFO (t),
8609 vtable, t, &v);
8611 /* Then come the virtual bases, also in inheritance graph order. */
8612 for (vbase = TYPE_BINFO (t); vbase; vbase = TREE_CHAIN (vbase))
8614 if (!BINFO_VIRTUAL_P (vbase))
8615 continue;
8616 accumulate_vtbl_inits (vbase, vbase, TYPE_BINFO (t), vtable, t, &v);
8619 if (BINFO_VTABLE (TYPE_BINFO (t)))
8620 initialize_vtable (TYPE_BINFO (t), v);
8623 /* Initialize the vtable for BINFO with the INITS. */
8625 static void
8626 initialize_vtable (tree binfo, vec<constructor_elt, va_gc> *inits)
8628 tree decl;
8630 layout_vtable_decl (binfo, vec_safe_length (inits));
8631 decl = get_vtbl_decl_for_binfo (binfo);
8632 initialize_artificial_var (decl, inits);
8633 dump_vtable (BINFO_TYPE (binfo), binfo, decl);
8636 /* Build the VTT (virtual table table) for T.
8637 A class requires a VTT if it has virtual bases.
8639 This holds
8640 1 - primary virtual pointer for complete object T
8641 2 - secondary VTTs for each direct non-virtual base of T which requires a
8643 3 - secondary virtual pointers for each direct or indirect base of T which
8644 has virtual bases or is reachable via a virtual path from T.
8645 4 - secondary VTTs for each direct or indirect virtual base of T.
8647 Secondary VTTs look like complete object VTTs without part 4. */
8649 static void
8650 build_vtt (tree t)
8652 tree type;
8653 tree vtt;
8654 tree index;
8655 vec<constructor_elt, va_gc> *inits;
8657 /* Build up the initializers for the VTT. */
8658 inits = NULL;
8659 index = size_zero_node;
8660 build_vtt_inits (TYPE_BINFO (t), t, &inits, &index);
8662 /* If we didn't need a VTT, we're done. */
8663 if (!inits)
8664 return;
8666 /* Figure out the type of the VTT. */
8667 type = build_array_of_n_type (const_ptr_type_node,
8668 inits->length ());
8670 /* Now, build the VTT object itself. */
8671 vtt = build_vtable (t, mangle_vtt_for_type (t), type);
8672 initialize_artificial_var (vtt, inits);
8673 /* Add the VTT to the vtables list. */
8674 DECL_CHAIN (vtt) = DECL_CHAIN (CLASSTYPE_VTABLES (t));
8675 DECL_CHAIN (CLASSTYPE_VTABLES (t)) = vtt;
8677 dump_vtt (t, vtt);
8680 /* When building a secondary VTT, BINFO_VTABLE is set to a TREE_LIST with
8681 PURPOSE the RTTI_BINFO, VALUE the real vtable pointer for this binfo,
8682 and CHAIN the vtable pointer for this binfo after construction is
8683 complete. VALUE can also be another BINFO, in which case we recurse. */
8685 static tree
8686 binfo_ctor_vtable (tree binfo)
8688 tree vt;
8690 while (1)
8692 vt = BINFO_VTABLE (binfo);
8693 if (TREE_CODE (vt) == TREE_LIST)
8694 vt = TREE_VALUE (vt);
8695 if (TREE_CODE (vt) == TREE_BINFO)
8696 binfo = vt;
8697 else
8698 break;
8701 return vt;
8704 /* Data for secondary VTT initialization. */
8705 struct secondary_vptr_vtt_init_data
8707 /* Is this the primary VTT? */
8708 bool top_level_p;
8710 /* Current index into the VTT. */
8711 tree index;
8713 /* Vector of initializers built up. */
8714 vec<constructor_elt, va_gc> *inits;
8716 /* The type being constructed by this secondary VTT. */
8717 tree type_being_constructed;
8720 /* Recursively build the VTT-initializer for BINFO (which is in the
8721 hierarchy dominated by T). INITS points to the end of the initializer
8722 list to date. INDEX is the VTT index where the next element will be
8723 replaced. Iff BINFO is the binfo for T, this is the top level VTT (i.e.
8724 not a subvtt for some base of T). When that is so, we emit the sub-VTTs
8725 for virtual bases of T. When it is not so, we build the constructor
8726 vtables for the BINFO-in-T variant. */
8728 static void
8729 build_vtt_inits (tree binfo, tree t, vec<constructor_elt, va_gc> **inits,
8730 tree *index)
8732 int i;
8733 tree b;
8734 tree init;
8735 secondary_vptr_vtt_init_data data;
8736 int top_level_p = SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), t);
8738 /* We only need VTTs for subobjects with virtual bases. */
8739 if (!CLASSTYPE_VBASECLASSES (BINFO_TYPE (binfo)))
8740 return;
8742 /* We need to use a construction vtable if this is not the primary
8743 VTT. */
8744 if (!top_level_p)
8746 build_ctor_vtbl_group (binfo, t);
8748 /* Record the offset in the VTT where this sub-VTT can be found. */
8749 BINFO_SUBVTT_INDEX (binfo) = *index;
8752 /* Add the address of the primary vtable for the complete object. */
8753 init = binfo_ctor_vtable (binfo);
8754 CONSTRUCTOR_APPEND_ELT (*inits, NULL_TREE, init);
8755 if (top_level_p)
8757 gcc_assert (!BINFO_VPTR_INDEX (binfo));
8758 BINFO_VPTR_INDEX (binfo) = *index;
8760 *index = size_binop (PLUS_EXPR, *index, TYPE_SIZE_UNIT (ptr_type_node));
8762 /* Recursively add the secondary VTTs for non-virtual bases. */
8763 for (i = 0; BINFO_BASE_ITERATE (binfo, i, b); ++i)
8764 if (!BINFO_VIRTUAL_P (b))
8765 build_vtt_inits (b, t, inits, index);
8767 /* Add secondary virtual pointers for all subobjects of BINFO with
8768 either virtual bases or reachable along a virtual path, except
8769 subobjects that are non-virtual primary bases. */
8770 data.top_level_p = top_level_p;
8771 data.index = *index;
8772 data.inits = *inits;
8773 data.type_being_constructed = BINFO_TYPE (binfo);
8775 dfs_walk_once (binfo, dfs_build_secondary_vptr_vtt_inits, NULL, &data);
8777 *index = data.index;
8779 /* data.inits might have grown as we added secondary virtual pointers.
8780 Make sure our caller knows about the new vector. */
8781 *inits = data.inits;
8783 if (top_level_p)
8784 /* Add the secondary VTTs for virtual bases in inheritance graph
8785 order. */
8786 for (b = TYPE_BINFO (BINFO_TYPE (binfo)); b; b = TREE_CHAIN (b))
8788 if (!BINFO_VIRTUAL_P (b))
8789 continue;
8791 build_vtt_inits (b, t, inits, index);
8793 else
8794 /* Remove the ctor vtables we created. */
8795 dfs_walk_all (binfo, dfs_fixup_binfo_vtbls, NULL, binfo);
8798 /* Called from build_vtt_inits via dfs_walk. BINFO is the binfo for the base
8799 in most derived. DATA is a SECONDARY_VPTR_VTT_INIT_DATA structure. */
8801 static tree
8802 dfs_build_secondary_vptr_vtt_inits (tree binfo, void *data_)
8804 secondary_vptr_vtt_init_data *data = (secondary_vptr_vtt_init_data *)data_;
8806 /* We don't care about bases that don't have vtables. */
8807 if (!TYPE_VFIELD (BINFO_TYPE (binfo)))
8808 return dfs_skip_bases;
8810 /* We're only interested in proper subobjects of the type being
8811 constructed. */
8812 if (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), data->type_being_constructed))
8813 return NULL_TREE;
8815 /* We're only interested in bases with virtual bases or reachable
8816 via a virtual path from the type being constructed. */
8817 if (!(CLASSTYPE_VBASECLASSES (BINFO_TYPE (binfo))
8818 || binfo_via_virtual (binfo, data->type_being_constructed)))
8819 return dfs_skip_bases;
8821 /* We're not interested in non-virtual primary bases. */
8822 if (!BINFO_VIRTUAL_P (binfo) && BINFO_PRIMARY_P (binfo))
8823 return NULL_TREE;
8825 /* Record the index where this secondary vptr can be found. */
8826 if (data->top_level_p)
8828 gcc_assert (!BINFO_VPTR_INDEX (binfo));
8829 BINFO_VPTR_INDEX (binfo) = data->index;
8831 if (BINFO_VIRTUAL_P (binfo))
8833 /* It's a primary virtual base, and this is not a
8834 construction vtable. Find the base this is primary of in
8835 the inheritance graph, and use that base's vtable
8836 now. */
8837 while (BINFO_PRIMARY_P (binfo))
8838 binfo = BINFO_INHERITANCE_CHAIN (binfo);
8842 /* Add the initializer for the secondary vptr itself. */
8843 CONSTRUCTOR_APPEND_ELT (data->inits, NULL_TREE, binfo_ctor_vtable (binfo));
8845 /* Advance the vtt index. */
8846 data->index = size_binop (PLUS_EXPR, data->index,
8847 TYPE_SIZE_UNIT (ptr_type_node));
8849 return NULL_TREE;
8852 /* Called from build_vtt_inits via dfs_walk. After building
8853 constructor vtables and generating the sub-vtt from them, we need
8854 to restore the BINFO_VTABLES that were scribbled on. DATA is the
8855 binfo of the base whose sub vtt was generated. */
8857 static tree
8858 dfs_fixup_binfo_vtbls (tree binfo, void* data)
8860 tree vtable = BINFO_VTABLE (binfo);
8862 if (!TYPE_CONTAINS_VPTR_P (BINFO_TYPE (binfo)))
8863 /* If this class has no vtable, none of its bases do. */
8864 return dfs_skip_bases;
8866 if (!vtable)
8867 /* This might be a primary base, so have no vtable in this
8868 hierarchy. */
8869 return NULL_TREE;
8871 /* If we scribbled the construction vtable vptr into BINFO, clear it
8872 out now. */
8873 if (TREE_CODE (vtable) == TREE_LIST
8874 && (TREE_PURPOSE (vtable) == (tree) data))
8875 BINFO_VTABLE (binfo) = TREE_CHAIN (vtable);
8877 return NULL_TREE;
8880 /* Build the construction vtable group for BINFO which is in the
8881 hierarchy dominated by T. */
8883 static void
8884 build_ctor_vtbl_group (tree binfo, tree t)
8886 tree type;
8887 tree vtbl;
8888 tree id;
8889 tree vbase;
8890 vec<constructor_elt, va_gc> *v;
8892 /* See if we've already created this construction vtable group. */
8893 id = mangle_ctor_vtbl_for_type (t, binfo);
8894 if (get_global_binding (id))
8895 return;
8897 gcc_assert (!SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), t));
8898 /* Build a version of VTBL (with the wrong type) for use in
8899 constructing the addresses of secondary vtables in the
8900 construction vtable group. */
8901 vtbl = build_vtable (t, id, ptr_type_node);
8902 DECL_CONSTRUCTION_VTABLE_P (vtbl) = 1;
8903 /* Don't export construction vtables from shared libraries. Even on
8904 targets that don't support hidden visibility, this tells
8905 can_refer_decl_in_current_unit_p not to assume that it's safe to
8906 access from a different compilation unit (bz 54314). */
8907 DECL_VISIBILITY (vtbl) = VISIBILITY_HIDDEN;
8908 DECL_VISIBILITY_SPECIFIED (vtbl) = true;
8910 v = NULL;
8911 accumulate_vtbl_inits (binfo, TYPE_BINFO (TREE_TYPE (binfo)),
8912 binfo, vtbl, t, &v);
8914 /* Add the vtables for each of our virtual bases using the vbase in T
8915 binfo. */
8916 for (vbase = TYPE_BINFO (BINFO_TYPE (binfo));
8917 vbase;
8918 vbase = TREE_CHAIN (vbase))
8920 tree b;
8922 if (!BINFO_VIRTUAL_P (vbase))
8923 continue;
8924 b = copied_binfo (vbase, binfo);
8926 accumulate_vtbl_inits (b, vbase, binfo, vtbl, t, &v);
8929 /* Figure out the type of the construction vtable. */
8930 type = build_array_of_n_type (vtable_entry_type, v->length ());
8931 layout_type (type);
8932 TREE_TYPE (vtbl) = type;
8933 DECL_SIZE (vtbl) = DECL_SIZE_UNIT (vtbl) = NULL_TREE;
8934 layout_decl (vtbl, 0);
8936 /* Initialize the construction vtable. */
8937 CLASSTYPE_VTABLES (t) = chainon (CLASSTYPE_VTABLES (t), vtbl);
8938 initialize_artificial_var (vtbl, v);
8939 dump_vtable (t, binfo, vtbl);
8942 /* Add the vtbl initializers for BINFO (and its bases other than
8943 non-virtual primaries) to the list of INITS. BINFO is in the
8944 hierarchy dominated by T. RTTI_BINFO is the binfo within T of
8945 the constructor the vtbl inits should be accumulated for. (If this
8946 is the complete object vtbl then RTTI_BINFO will be TYPE_BINFO (T).)
8947 ORIG_BINFO is the binfo for this object within BINFO_TYPE (RTTI_BINFO).
8948 BINFO is the active base equivalent of ORIG_BINFO in the inheritance
8949 graph of T. Both BINFO and ORIG_BINFO will have the same BINFO_TYPE,
8950 but are not necessarily the same in terms of layout. */
8952 static void
8953 accumulate_vtbl_inits (tree binfo,
8954 tree orig_binfo,
8955 tree rtti_binfo,
8956 tree vtbl,
8957 tree t,
8958 vec<constructor_elt, va_gc> **inits)
8960 int i;
8961 tree base_binfo;
8962 int ctor_vtbl_p = !SAME_BINFO_TYPE_P (BINFO_TYPE (rtti_binfo), t);
8964 gcc_assert (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), BINFO_TYPE (orig_binfo)));
8966 /* If it doesn't have a vptr, we don't do anything. */
8967 if (!TYPE_CONTAINS_VPTR_P (BINFO_TYPE (binfo)))
8968 return;
8970 /* If we're building a construction vtable, we're not interested in
8971 subobjects that don't require construction vtables. */
8972 if (ctor_vtbl_p
8973 && !CLASSTYPE_VBASECLASSES (BINFO_TYPE (binfo))
8974 && !binfo_via_virtual (orig_binfo, BINFO_TYPE (rtti_binfo)))
8975 return;
8977 /* Build the initializers for the BINFO-in-T vtable. */
8978 dfs_accumulate_vtbl_inits (binfo, orig_binfo, rtti_binfo, vtbl, t, inits);
8980 /* Walk the BINFO and its bases. We walk in preorder so that as we
8981 initialize each vtable we can figure out at what offset the
8982 secondary vtable lies from the primary vtable. We can't use
8983 dfs_walk here because we need to iterate through bases of BINFO
8984 and RTTI_BINFO simultaneously. */
8985 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
8987 /* Skip virtual bases. */
8988 if (BINFO_VIRTUAL_P (base_binfo))
8989 continue;
8990 accumulate_vtbl_inits (base_binfo,
8991 BINFO_BASE_BINFO (orig_binfo, i),
8992 rtti_binfo, vtbl, t,
8993 inits);
8997 /* Called from accumulate_vtbl_inits. Adds the initializers for the
8998 BINFO vtable to L. */
9000 static void
9001 dfs_accumulate_vtbl_inits (tree binfo,
9002 tree orig_binfo,
9003 tree rtti_binfo,
9004 tree orig_vtbl,
9005 tree t,
9006 vec<constructor_elt, va_gc> **l)
9008 tree vtbl = NULL_TREE;
9009 int ctor_vtbl_p = !SAME_BINFO_TYPE_P (BINFO_TYPE (rtti_binfo), t);
9010 int n_inits;
9012 if (ctor_vtbl_p
9013 && BINFO_VIRTUAL_P (orig_binfo) && BINFO_PRIMARY_P (orig_binfo))
9015 /* In the hierarchy of BINFO_TYPE (RTTI_BINFO), this is a
9016 primary virtual base. If it is not the same primary in
9017 the hierarchy of T, we'll need to generate a ctor vtable
9018 for it, to place at its location in T. If it is the same
9019 primary, we still need a VTT entry for the vtable, but it
9020 should point to the ctor vtable for the base it is a
9021 primary for within the sub-hierarchy of RTTI_BINFO.
9023 There are three possible cases:
9025 1) We are in the same place.
9026 2) We are a primary base within a lost primary virtual base of
9027 RTTI_BINFO.
9028 3) We are primary to something not a base of RTTI_BINFO. */
9030 tree b;
9031 tree last = NULL_TREE;
9033 /* First, look through the bases we are primary to for RTTI_BINFO
9034 or a virtual base. */
9035 b = binfo;
9036 while (BINFO_PRIMARY_P (b))
9038 b = BINFO_INHERITANCE_CHAIN (b);
9039 last = b;
9040 if (BINFO_VIRTUAL_P (b) || b == rtti_binfo)
9041 goto found;
9043 /* If we run out of primary links, keep looking down our
9044 inheritance chain; we might be an indirect primary. */
9045 for (b = last; b; b = BINFO_INHERITANCE_CHAIN (b))
9046 if (BINFO_VIRTUAL_P (b) || b == rtti_binfo)
9047 break;
9048 found:
9050 /* If we found RTTI_BINFO, this is case 1. If we found a virtual
9051 base B and it is a base of RTTI_BINFO, this is case 2. In
9052 either case, we share our vtable with LAST, i.e. the
9053 derived-most base within B of which we are a primary. */
9054 if (b == rtti_binfo
9055 || (b && binfo_for_vbase (BINFO_TYPE (b), BINFO_TYPE (rtti_binfo))))
9056 /* Just set our BINFO_VTABLE to point to LAST, as we may not have
9057 set LAST's BINFO_VTABLE yet. We'll extract the actual vptr in
9058 binfo_ctor_vtable after everything's been set up. */
9059 vtbl = last;
9061 /* Otherwise, this is case 3 and we get our own. */
9063 else if (!BINFO_NEW_VTABLE_MARKED (orig_binfo))
9064 return;
9066 n_inits = vec_safe_length (*l);
9068 if (!vtbl)
9070 tree index;
9071 int non_fn_entries;
9073 /* Add the initializer for this vtable. */
9074 build_vtbl_initializer (binfo, orig_binfo, t, rtti_binfo,
9075 &non_fn_entries, l);
9077 /* Figure out the position to which the VPTR should point. */
9078 vtbl = build1 (ADDR_EXPR, vtbl_ptr_type_node, orig_vtbl);
9079 index = size_binop (MULT_EXPR,
9080 TYPE_SIZE_UNIT (vtable_entry_type),
9081 size_int (non_fn_entries + n_inits));
9082 vtbl = fold_build_pointer_plus (vtbl, index);
9085 if (ctor_vtbl_p)
9086 /* For a construction vtable, we can't overwrite BINFO_VTABLE.
9087 So, we make a TREE_LIST. Later, dfs_fixup_binfo_vtbls will
9088 straighten this out. */
9089 BINFO_VTABLE (binfo) = tree_cons (rtti_binfo, vtbl, BINFO_VTABLE (binfo));
9090 else if (BINFO_PRIMARY_P (binfo) && BINFO_VIRTUAL_P (binfo))
9091 /* Throw away any unneeded intializers. */
9092 (*l)->truncate (n_inits);
9093 else
9094 /* For an ordinary vtable, set BINFO_VTABLE. */
9095 BINFO_VTABLE (binfo) = vtbl;
9098 static GTY(()) tree abort_fndecl_addr;
9099 static GTY(()) tree dvirt_fn;
9101 /* Construct the initializer for BINFO's virtual function table. BINFO
9102 is part of the hierarchy dominated by T. If we're building a
9103 construction vtable, the ORIG_BINFO is the binfo we should use to
9104 find the actual function pointers to put in the vtable - but they
9105 can be overridden on the path to most-derived in the graph that
9106 ORIG_BINFO belongs. Otherwise,
9107 ORIG_BINFO should be the same as BINFO. The RTTI_BINFO is the
9108 BINFO that should be indicated by the RTTI information in the
9109 vtable; it will be a base class of T, rather than T itself, if we
9110 are building a construction vtable.
9112 The value returned is a TREE_LIST suitable for wrapping in a
9113 CONSTRUCTOR to use as the DECL_INITIAL for a vtable. If
9114 NON_FN_ENTRIES_P is not NULL, *NON_FN_ENTRIES_P is set to the
9115 number of non-function entries in the vtable.
9117 It might seem that this function should never be called with a
9118 BINFO for which BINFO_PRIMARY_P holds, the vtable for such a
9119 base is always subsumed by a derived class vtable. However, when
9120 we are building construction vtables, we do build vtables for
9121 primary bases; we need these while the primary base is being
9122 constructed. */
9124 static void
9125 build_vtbl_initializer (tree binfo,
9126 tree orig_binfo,
9127 tree t,
9128 tree rtti_binfo,
9129 int* non_fn_entries_p,
9130 vec<constructor_elt, va_gc> **inits)
9132 tree v;
9133 vtbl_init_data vid;
9134 unsigned ix, jx;
9135 tree vbinfo;
9136 vec<tree, va_gc> *vbases;
9137 constructor_elt *e;
9139 /* Initialize VID. */
9140 memset (&vid, 0, sizeof (vid));
9141 vid.binfo = binfo;
9142 vid.derived = t;
9143 vid.rtti_binfo = rtti_binfo;
9144 vid.primary_vtbl_p = SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), t);
9145 vid.ctor_vtbl_p = !SAME_BINFO_TYPE_P (BINFO_TYPE (rtti_binfo), t);
9146 vid.generate_vcall_entries = true;
9147 /* The first vbase or vcall offset is at index -3 in the vtable. */
9148 vid.index = ssize_int(-3 * TARGET_VTABLE_DATA_ENTRY_DISTANCE);
9150 /* Add entries to the vtable for RTTI. */
9151 build_rtti_vtbl_entries (binfo, &vid);
9153 /* Create an array for keeping track of the functions we've
9154 processed. When we see multiple functions with the same
9155 signature, we share the vcall offsets. */
9156 vec_alloc (vid.fns, 32);
9157 /* Add the vcall and vbase offset entries. */
9158 build_vcall_and_vbase_vtbl_entries (binfo, &vid);
9160 /* Clear BINFO_VTABLE_PATH_MARKED; it's set by
9161 build_vbase_offset_vtbl_entries. */
9162 for (vbases = CLASSTYPE_VBASECLASSES (t), ix = 0;
9163 vec_safe_iterate (vbases, ix, &vbinfo); ix++)
9164 BINFO_VTABLE_PATH_MARKED (vbinfo) = 0;
9166 /* If the target requires padding between data entries, add that now. */
9167 if (TARGET_VTABLE_DATA_ENTRY_DISTANCE > 1)
9169 int n_entries = vec_safe_length (vid.inits);
9171 vec_safe_grow (vid.inits, TARGET_VTABLE_DATA_ENTRY_DISTANCE * n_entries);
9173 /* Move data entries into their new positions and add padding
9174 after the new positions. Iterate backwards so we don't
9175 overwrite entries that we would need to process later. */
9176 for (ix = n_entries - 1;
9177 vid.inits->iterate (ix, &e);
9178 ix--)
9180 int j;
9181 int new_position = (TARGET_VTABLE_DATA_ENTRY_DISTANCE * ix
9182 + (TARGET_VTABLE_DATA_ENTRY_DISTANCE - 1));
9184 (*vid.inits)[new_position] = *e;
9186 for (j = 1; j < TARGET_VTABLE_DATA_ENTRY_DISTANCE; ++j)
9188 constructor_elt *f = &(*vid.inits)[new_position - j];
9189 f->index = NULL_TREE;
9190 f->value = build1 (NOP_EXPR, vtable_entry_type,
9191 null_pointer_node);
9196 if (non_fn_entries_p)
9197 *non_fn_entries_p = vec_safe_length (vid.inits);
9199 /* The initializers for virtual functions were built up in reverse
9200 order. Straighten them out and add them to the running list in one
9201 step. */
9202 jx = vec_safe_length (*inits);
9203 vec_safe_grow (*inits, jx + vid.inits->length ());
9205 for (ix = vid.inits->length () - 1;
9206 vid.inits->iterate (ix, &e);
9207 ix--, jx++)
9208 (**inits)[jx] = *e;
9210 /* Go through all the ordinary virtual functions, building up
9211 initializers. */
9212 for (v = BINFO_VIRTUALS (orig_binfo); v; v = TREE_CHAIN (v))
9214 tree delta;
9215 tree vcall_index;
9216 tree fn, fn_original;
9217 tree init = NULL_TREE;
9219 fn = BV_FN (v);
9220 fn_original = fn;
9221 if (DECL_THUNK_P (fn))
9223 if (!DECL_NAME (fn))
9224 finish_thunk (fn);
9225 if (THUNK_ALIAS (fn))
9227 fn = THUNK_ALIAS (fn);
9228 BV_FN (v) = fn;
9230 fn_original = THUNK_TARGET (fn);
9233 /* If the only definition of this function signature along our
9234 primary base chain is from a lost primary, this vtable slot will
9235 never be used, so just zero it out. This is important to avoid
9236 requiring extra thunks which cannot be generated with the function.
9238 We first check this in update_vtable_entry_for_fn, so we handle
9239 restored primary bases properly; we also need to do it here so we
9240 zero out unused slots in ctor vtables, rather than filling them
9241 with erroneous values (though harmless, apart from relocation
9242 costs). */
9243 if (BV_LOST_PRIMARY (v))
9244 init = size_zero_node;
9246 if (! init)
9248 /* Pull the offset for `this', and the function to call, out of
9249 the list. */
9250 delta = BV_DELTA (v);
9251 vcall_index = BV_VCALL_INDEX (v);
9253 gcc_assert (TREE_CODE (delta) == INTEGER_CST);
9254 gcc_assert (TREE_CODE (fn) == FUNCTION_DECL);
9256 /* You can't call an abstract virtual function; it's abstract.
9257 So, we replace these functions with __pure_virtual. */
9258 if (DECL_PURE_VIRTUAL_P (fn_original))
9260 fn = abort_fndecl;
9261 if (!TARGET_VTABLE_USES_DESCRIPTORS)
9263 if (abort_fndecl_addr == NULL)
9264 abort_fndecl_addr
9265 = fold_convert (vfunc_ptr_type_node,
9266 build_fold_addr_expr (fn));
9267 init = abort_fndecl_addr;
9270 /* Likewise for deleted virtuals. */
9271 else if (DECL_DELETED_FN (fn_original))
9273 if (!dvirt_fn)
9275 tree name = get_identifier ("__cxa_deleted_virtual");
9276 dvirt_fn = get_global_binding (name);
9277 if (!dvirt_fn)
9278 dvirt_fn = push_library_fn
9279 (name,
9280 build_function_type_list (void_type_node, NULL_TREE),
9281 NULL_TREE, ECF_NORETURN | ECF_COLD);
9283 fn = dvirt_fn;
9284 if (!TARGET_VTABLE_USES_DESCRIPTORS)
9285 init = fold_convert (vfunc_ptr_type_node,
9286 build_fold_addr_expr (fn));
9288 else
9290 if (!integer_zerop (delta) || vcall_index)
9292 fn = make_thunk (fn, /*this_adjusting=*/1,
9293 delta, vcall_index);
9294 if (!DECL_NAME (fn))
9295 finish_thunk (fn);
9297 /* Take the address of the function, considering it to be of an
9298 appropriate generic type. */
9299 if (!TARGET_VTABLE_USES_DESCRIPTORS)
9300 init = fold_convert (vfunc_ptr_type_node,
9301 build_fold_addr_expr (fn));
9302 /* Don't refer to a virtual destructor from a constructor
9303 vtable or a vtable for an abstract class, since destroying
9304 an object under construction is undefined behavior and we
9305 don't want it to be considered a candidate for speculative
9306 devirtualization. But do create the thunk for ABI
9307 compliance. */
9308 if (DECL_DESTRUCTOR_P (fn_original)
9309 && (CLASSTYPE_PURE_VIRTUALS (DECL_CONTEXT (fn_original))
9310 || orig_binfo != binfo))
9311 init = size_zero_node;
9315 /* And add it to the chain of initializers. */
9316 if (TARGET_VTABLE_USES_DESCRIPTORS)
9318 int i;
9319 if (init == size_zero_node)
9320 for (i = 0; i < TARGET_VTABLE_USES_DESCRIPTORS; ++i)
9321 CONSTRUCTOR_APPEND_ELT (*inits, NULL_TREE, init);
9322 else
9323 for (i = 0; i < TARGET_VTABLE_USES_DESCRIPTORS; ++i)
9325 tree fdesc = build2 (FDESC_EXPR, vfunc_ptr_type_node,
9326 fn, build_int_cst (NULL_TREE, i));
9327 TREE_CONSTANT (fdesc) = 1;
9329 CONSTRUCTOR_APPEND_ELT (*inits, NULL_TREE, fdesc);
9332 else
9333 CONSTRUCTOR_APPEND_ELT (*inits, NULL_TREE, init);
9337 /* Adds to vid->inits the initializers for the vbase and vcall
9338 offsets in BINFO, which is in the hierarchy dominated by T. */
9340 static void
9341 build_vcall_and_vbase_vtbl_entries (tree binfo, vtbl_init_data* vid)
9343 tree b;
9345 /* If this is a derived class, we must first create entries
9346 corresponding to the primary base class. */
9347 b = get_primary_binfo (binfo);
9348 if (b)
9349 build_vcall_and_vbase_vtbl_entries (b, vid);
9351 /* Add the vbase entries for this base. */
9352 build_vbase_offset_vtbl_entries (binfo, vid);
9353 /* Add the vcall entries for this base. */
9354 build_vcall_offset_vtbl_entries (binfo, vid);
9357 /* Returns the initializers for the vbase offset entries in the vtable
9358 for BINFO (which is part of the class hierarchy dominated by T), in
9359 reverse order. VBASE_OFFSET_INDEX gives the vtable index
9360 where the next vbase offset will go. */
9362 static void
9363 build_vbase_offset_vtbl_entries (tree binfo, vtbl_init_data* vid)
9365 tree vbase;
9366 tree t;
9367 tree non_primary_binfo;
9369 /* If there are no virtual baseclasses, then there is nothing to
9370 do. */
9371 if (!CLASSTYPE_VBASECLASSES (BINFO_TYPE (binfo)))
9372 return;
9374 t = vid->derived;
9376 /* We might be a primary base class. Go up the inheritance hierarchy
9377 until we find the most derived class of which we are a primary base:
9378 it is the offset of that which we need to use. */
9379 non_primary_binfo = binfo;
9380 while (BINFO_INHERITANCE_CHAIN (non_primary_binfo))
9382 tree b;
9384 /* If we have reached a virtual base, then it must be a primary
9385 base (possibly multi-level) of vid->binfo, or we wouldn't
9386 have called build_vcall_and_vbase_vtbl_entries for it. But it
9387 might be a lost primary, so just skip down to vid->binfo. */
9388 if (BINFO_VIRTUAL_P (non_primary_binfo))
9390 non_primary_binfo = vid->binfo;
9391 break;
9394 b = BINFO_INHERITANCE_CHAIN (non_primary_binfo);
9395 if (get_primary_binfo (b) != non_primary_binfo)
9396 break;
9397 non_primary_binfo = b;
9400 /* Go through the virtual bases, adding the offsets. */
9401 for (vbase = TYPE_BINFO (BINFO_TYPE (binfo));
9402 vbase;
9403 vbase = TREE_CHAIN (vbase))
9405 tree b;
9406 tree delta;
9408 if (!BINFO_VIRTUAL_P (vbase))
9409 continue;
9411 /* Find the instance of this virtual base in the complete
9412 object. */
9413 b = copied_binfo (vbase, binfo);
9415 /* If we've already got an offset for this virtual base, we
9416 don't need another one. */
9417 if (BINFO_VTABLE_PATH_MARKED (b))
9418 continue;
9419 BINFO_VTABLE_PATH_MARKED (b) = 1;
9421 /* Figure out where we can find this vbase offset. */
9422 delta = size_binop (MULT_EXPR,
9423 vid->index,
9424 fold_convert (ssizetype,
9425 TYPE_SIZE_UNIT (vtable_entry_type)));
9426 if (vid->primary_vtbl_p)
9427 BINFO_VPTR_FIELD (b) = delta;
9429 if (binfo != TYPE_BINFO (t))
9430 /* The vbase offset had better be the same. */
9431 gcc_assert (tree_int_cst_equal (delta, BINFO_VPTR_FIELD (vbase)));
9433 /* The next vbase will come at a more negative offset. */
9434 vid->index = size_binop (MINUS_EXPR, vid->index,
9435 ssize_int (TARGET_VTABLE_DATA_ENTRY_DISTANCE));
9437 /* The initializer is the delta from BINFO to this virtual base.
9438 The vbase offsets go in reverse inheritance-graph order, and
9439 we are walking in inheritance graph order so these end up in
9440 the right order. */
9441 delta = size_diffop_loc (input_location,
9442 BINFO_OFFSET (b), BINFO_OFFSET (non_primary_binfo));
9444 CONSTRUCTOR_APPEND_ELT (vid->inits, NULL_TREE,
9445 fold_build1_loc (input_location, NOP_EXPR,
9446 vtable_entry_type, delta));
9450 /* Adds the initializers for the vcall offset entries in the vtable
9451 for BINFO (which is part of the class hierarchy dominated by VID->DERIVED)
9452 to VID->INITS. */
9454 static void
9455 build_vcall_offset_vtbl_entries (tree binfo, vtbl_init_data* vid)
9457 /* We only need these entries if this base is a virtual base. We
9458 compute the indices -- but do not add to the vtable -- when
9459 building the main vtable for a class. */
9460 if (binfo == TYPE_BINFO (vid->derived)
9461 || (BINFO_VIRTUAL_P (binfo)
9462 /* If BINFO is RTTI_BINFO, then (since BINFO does not
9463 correspond to VID->DERIVED), we are building a primary
9464 construction virtual table. Since this is a primary
9465 virtual table, we do not need the vcall offsets for
9466 BINFO. */
9467 && binfo != vid->rtti_binfo))
9469 /* We need a vcall offset for each of the virtual functions in this
9470 vtable. For example:
9472 class A { virtual void f (); };
9473 class B1 : virtual public A { virtual void f (); };
9474 class B2 : virtual public A { virtual void f (); };
9475 class C: public B1, public B2 { virtual void f (); };
9477 A C object has a primary base of B1, which has a primary base of A. A
9478 C also has a secondary base of B2, which no longer has a primary base
9479 of A. So the B2-in-C construction vtable needs a secondary vtable for
9480 A, which will adjust the A* to a B2* to call f. We have no way of
9481 knowing what (or even whether) this offset will be when we define B2,
9482 so we store this "vcall offset" in the A sub-vtable and look it up in
9483 a "virtual thunk" for B2::f.
9485 We need entries for all the functions in our primary vtable and
9486 in our non-virtual bases' secondary vtables. */
9487 vid->vbase = binfo;
9488 /* If we are just computing the vcall indices -- but do not need
9489 the actual entries -- not that. */
9490 if (!BINFO_VIRTUAL_P (binfo))
9491 vid->generate_vcall_entries = false;
9492 /* Now, walk through the non-virtual bases, adding vcall offsets. */
9493 add_vcall_offset_vtbl_entries_r (binfo, vid);
9497 /* Build vcall offsets, starting with those for BINFO. */
9499 static void
9500 add_vcall_offset_vtbl_entries_r (tree binfo, vtbl_init_data* vid)
9502 int i;
9503 tree primary_binfo;
9504 tree base_binfo;
9506 /* Don't walk into virtual bases -- except, of course, for the
9507 virtual base for which we are building vcall offsets. Any
9508 primary virtual base will have already had its offsets generated
9509 through the recursion in build_vcall_and_vbase_vtbl_entries. */
9510 if (BINFO_VIRTUAL_P (binfo) && vid->vbase != binfo)
9511 return;
9513 /* If BINFO has a primary base, process it first. */
9514 primary_binfo = get_primary_binfo (binfo);
9515 if (primary_binfo)
9516 add_vcall_offset_vtbl_entries_r (primary_binfo, vid);
9518 /* Add BINFO itself to the list. */
9519 add_vcall_offset_vtbl_entries_1 (binfo, vid);
9521 /* Scan the non-primary bases of BINFO. */
9522 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
9523 if (base_binfo != primary_binfo)
9524 add_vcall_offset_vtbl_entries_r (base_binfo, vid);
9527 /* Called from build_vcall_offset_vtbl_entries_r. */
9529 static void
9530 add_vcall_offset_vtbl_entries_1 (tree binfo, vtbl_init_data* vid)
9532 /* Make entries for the rest of the virtuals. */
9533 tree orig_fn;
9535 /* The ABI requires that the methods be processed in declaration
9536 order. */
9537 for (orig_fn = TYPE_FIELDS (BINFO_TYPE (binfo));
9538 orig_fn;
9539 orig_fn = DECL_CHAIN (orig_fn))
9540 if (TREE_CODE (orig_fn) == FUNCTION_DECL && DECL_VINDEX (orig_fn))
9541 add_vcall_offset (orig_fn, binfo, vid);
9544 /* Add a vcall offset entry for ORIG_FN to the vtable. */
9546 static void
9547 add_vcall_offset (tree orig_fn, tree binfo, vtbl_init_data *vid)
9549 size_t i;
9550 tree vcall_offset;
9551 tree derived_entry;
9553 /* If there is already an entry for a function with the same
9554 signature as FN, then we do not need a second vcall offset.
9555 Check the list of functions already present in the derived
9556 class vtable. */
9557 FOR_EACH_VEC_SAFE_ELT (vid->fns, i, derived_entry)
9559 if (same_signature_p (derived_entry, orig_fn)
9560 /* We only use one vcall offset for virtual destructors,
9561 even though there are two virtual table entries. */
9562 || (DECL_DESTRUCTOR_P (derived_entry)
9563 && DECL_DESTRUCTOR_P (orig_fn)))
9564 return;
9567 /* If we are building these vcall offsets as part of building
9568 the vtable for the most derived class, remember the vcall
9569 offset. */
9570 if (vid->binfo == TYPE_BINFO (vid->derived))
9572 tree_pair_s elt = {orig_fn, vid->index};
9573 vec_safe_push (CLASSTYPE_VCALL_INDICES (vid->derived), elt);
9576 /* The next vcall offset will be found at a more negative
9577 offset. */
9578 vid->index = size_binop (MINUS_EXPR, vid->index,
9579 ssize_int (TARGET_VTABLE_DATA_ENTRY_DISTANCE));
9581 /* Keep track of this function. */
9582 vec_safe_push (vid->fns, orig_fn);
9584 if (vid->generate_vcall_entries)
9586 tree base;
9587 tree fn;
9589 /* Find the overriding function. */
9590 fn = find_final_overrider (vid->rtti_binfo, binfo, orig_fn);
9591 if (fn == error_mark_node)
9592 vcall_offset = build_zero_cst (vtable_entry_type);
9593 else
9595 base = TREE_VALUE (fn);
9597 /* The vbase we're working on is a primary base of
9598 vid->binfo. But it might be a lost primary, so its
9599 BINFO_OFFSET might be wrong, so we just use the
9600 BINFO_OFFSET from vid->binfo. */
9601 vcall_offset = size_diffop_loc (input_location,
9602 BINFO_OFFSET (base),
9603 BINFO_OFFSET (vid->binfo));
9604 vcall_offset = fold_build1_loc (input_location,
9605 NOP_EXPR, vtable_entry_type,
9606 vcall_offset);
9608 /* Add the initializer to the vtable. */
9609 CONSTRUCTOR_APPEND_ELT (vid->inits, NULL_TREE, vcall_offset);
9613 /* Return vtbl initializers for the RTTI entries corresponding to the
9614 BINFO's vtable. The RTTI entries should indicate the object given
9615 by VID->rtti_binfo. */
9617 static void
9618 build_rtti_vtbl_entries (tree binfo, vtbl_init_data* vid)
9620 tree b;
9621 tree t;
9622 tree offset;
9623 tree decl;
9624 tree init;
9626 t = BINFO_TYPE (vid->rtti_binfo);
9628 /* To find the complete object, we will first convert to our most
9629 primary base, and then add the offset in the vtbl to that value. */
9630 b = most_primary_binfo (binfo);
9631 offset = size_diffop_loc (input_location,
9632 BINFO_OFFSET (vid->rtti_binfo), BINFO_OFFSET (b));
9634 /* The second entry is the address of the typeinfo object. */
9635 if (flag_rtti)
9636 decl = build_address (get_tinfo_decl (t));
9637 else
9638 decl = integer_zero_node;
9640 /* Convert the declaration to a type that can be stored in the
9641 vtable. */
9642 init = build_nop (vfunc_ptr_type_node, decl);
9643 CONSTRUCTOR_APPEND_ELT (vid->inits, NULL_TREE, init);
9645 /* Add the offset-to-top entry. It comes earlier in the vtable than
9646 the typeinfo entry. Convert the offset to look like a
9647 function pointer, so that we can put it in the vtable. */
9648 init = build_nop (vfunc_ptr_type_node, offset);
9649 CONSTRUCTOR_APPEND_ELT (vid->inits, NULL_TREE, init);
9652 /* TRUE iff TYPE is uniquely derived from PARENT. Ignores
9653 accessibility. */
9655 bool
9656 uniquely_derived_from_p (tree parent, tree type)
9658 tree base = lookup_base (type, parent, ba_unique, NULL, tf_none);
9659 return base && base != error_mark_node;
9662 /* TRUE iff TYPE is publicly & uniquely derived from PARENT. */
9664 bool
9665 publicly_uniquely_derived_p (tree parent, tree type)
9667 tree base = lookup_base (type, parent, ba_ignore_scope | ba_check,
9668 NULL, tf_none);
9669 return base && base != error_mark_node;
9672 /* CTX1 and CTX2 are declaration contexts. Return the innermost common
9673 class between them, if any. */
9675 tree
9676 common_enclosing_class (tree ctx1, tree ctx2)
9678 if (!TYPE_P (ctx1) || !TYPE_P (ctx2))
9679 return NULL_TREE;
9680 gcc_assert (ctx1 == TYPE_MAIN_VARIANT (ctx1)
9681 && ctx2 == TYPE_MAIN_VARIANT (ctx2));
9682 if (ctx1 == ctx2)
9683 return ctx1;
9684 for (tree t = ctx1; TYPE_P (t); t = TYPE_CONTEXT (t))
9685 TYPE_MARKED_P (t) = true;
9686 tree found = NULL_TREE;
9687 for (tree t = ctx2; TYPE_P (t); t = TYPE_CONTEXT (t))
9688 if (TYPE_MARKED_P (t))
9690 found = t;
9691 break;
9693 for (tree t = ctx1; TYPE_P (t); t = TYPE_CONTEXT (t))
9694 TYPE_MARKED_P (t) = false;
9695 return found;
9698 #include "gt-cp-class.h"