/cp
[official-gcc.git] / gcc / cp / class.c
blob9ca464418717f36439ddacd1a172c71853d2e1c1
1 /* Functions related to building classes and their related objects.
2 Copyright (C) 1987-2018 Free Software Foundation, Inc.
3 Contributed by Michael Tiemann (tiemann@cygnus.com)
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
10 any later version.
12 GCC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
22 /* High-level class interface. */
24 #include "config.h"
25 #include "system.h"
26 #include "coretypes.h"
27 #include "target.h"
28 #include "cp-tree.h"
29 #include "stringpool.h"
30 #include "cgraph.h"
31 #include "stor-layout.h"
32 #include "attribs.h"
33 #include "flags.h"
34 #include "toplev.h"
35 #include "convert.h"
36 #include "dumpfile.h"
37 #include "gimplify.h"
38 #include "intl.h"
39 #include "asan.h"
41 /* Id for dumping the class hierarchy. */
42 int class_dump_id;
44 /* The number of nested classes being processed. If we are not in the
45 scope of any class, this is zero. */
47 int current_class_depth;
49 /* In order to deal with nested classes, we keep a stack of classes.
50 The topmost entry is the innermost class, and is the entry at index
51 CURRENT_CLASS_DEPTH */
53 typedef struct class_stack_node {
54 /* The name of the class. */
55 tree name;
57 /* The _TYPE node for the class. */
58 tree type;
60 /* The access specifier pending for new declarations in the scope of
61 this class. */
62 tree access;
64 /* If were defining TYPE, the names used in this class. */
65 splay_tree names_used;
67 /* Nonzero if this class is no longer open, because of a call to
68 push_to_top_level. */
69 size_t hidden;
70 }* class_stack_node_t;
72 struct vtbl_init_data
74 /* The base for which we're building initializers. */
75 tree binfo;
76 /* The type of the most-derived type. */
77 tree derived;
78 /* The binfo for the dynamic type. This will be TYPE_BINFO (derived),
79 unless ctor_vtbl_p is true. */
80 tree rtti_binfo;
81 /* The negative-index vtable initializers built up so far. These
82 are in order from least negative index to most negative index. */
83 vec<constructor_elt, va_gc> *inits;
84 /* The binfo for the virtual base for which we're building
85 vcall offset initializers. */
86 tree vbase;
87 /* The functions in vbase for which we have already provided vcall
88 offsets. */
89 vec<tree, va_gc> *fns;
90 /* The vtable index of the next vcall or vbase offset. */
91 tree index;
92 /* Nonzero if we are building the initializer for the primary
93 vtable. */
94 int primary_vtbl_p;
95 /* Nonzero if we are building the initializer for a construction
96 vtable. */
97 int ctor_vtbl_p;
98 /* True when adding vcall offset entries to the vtable. False when
99 merely computing the indices. */
100 bool generate_vcall_entries;
103 /* The type of a function passed to walk_subobject_offsets. */
104 typedef int (*subobject_offset_fn) (tree, tree, splay_tree);
106 /* The stack itself. This is a dynamically resized array. The
107 number of elements allocated is CURRENT_CLASS_STACK_SIZE. */
108 static int current_class_stack_size;
109 static class_stack_node_t current_class_stack;
111 /* The size of the largest empty class seen in this translation unit. */
112 static GTY (()) tree sizeof_biggest_empty_class;
114 /* An array of all local classes present in this translation unit, in
115 declaration order. */
116 vec<tree, va_gc> *local_classes;
118 static tree get_vfield_name (tree);
119 static void finish_struct_anon (tree);
120 static tree get_vtable_name (tree);
121 static void get_basefndecls (tree, tree, vec<tree> *);
122 static int build_primary_vtable (tree, tree);
123 static int build_secondary_vtable (tree);
124 static void finish_vtbls (tree);
125 static void modify_vtable_entry (tree, tree, tree, tree, tree *);
126 static void finish_struct_bits (tree);
127 static int alter_access (tree, tree, tree);
128 static void handle_using_decl (tree, tree);
129 static tree dfs_modify_vtables (tree, void *);
130 static tree modify_all_vtables (tree, tree);
131 static void determine_primary_bases (tree);
132 static void maybe_warn_about_overly_private_class (tree);
133 static void add_implicitly_declared_members (tree, tree*, int, int);
134 static tree fixed_type_or_null (tree, int *, int *);
135 static tree build_simple_base_path (tree expr, tree binfo);
136 static void build_vtbl_initializer (tree, tree, tree, tree, int *,
137 vec<constructor_elt, va_gc> **);
138 static bool check_bitfield_decl (tree);
139 static bool check_field_decl (tree, tree, int *, int *);
140 static void check_field_decls (tree, tree *, int *, int *);
141 static tree *build_base_field (record_layout_info, tree, splay_tree, tree *);
142 static void build_base_fields (record_layout_info, splay_tree, tree *);
143 static void check_methods (tree);
144 static void remove_zero_width_bit_fields (tree);
145 static bool accessible_nvdtor_p (tree);
147 /* Used by find_flexarrays and related functions. */
148 struct flexmems_t;
149 static void diagnose_flexarrays (tree, const flexmems_t *);
150 static void find_flexarrays (tree, flexmems_t *, bool = false,
151 tree = NULL_TREE, tree = NULL_TREE);
152 static void check_flexarrays (tree, flexmems_t * = NULL, bool = false);
153 static void check_bases (tree, int *, int *);
154 static void check_bases_and_members (tree);
155 static tree create_vtable_ptr (tree, tree *);
156 static void include_empty_classes (record_layout_info);
157 static void layout_class_type (tree, tree *);
158 static void propagate_binfo_offsets (tree, tree);
159 static void layout_virtual_bases (record_layout_info, splay_tree);
160 static void build_vbase_offset_vtbl_entries (tree, vtbl_init_data *);
161 static void add_vcall_offset_vtbl_entries_r (tree, vtbl_init_data *);
162 static void add_vcall_offset_vtbl_entries_1 (tree, vtbl_init_data *);
163 static void build_vcall_offset_vtbl_entries (tree, vtbl_init_data *);
164 static void add_vcall_offset (tree, tree, vtbl_init_data *);
165 static void layout_vtable_decl (tree, int);
166 static tree dfs_find_final_overrider_pre (tree, void *);
167 static tree dfs_find_final_overrider_post (tree, void *);
168 static tree find_final_overrider (tree, tree, tree);
169 static int make_new_vtable (tree, tree);
170 static tree get_primary_binfo (tree);
171 static int maybe_indent_hierarchy (FILE *, int, int);
172 static tree dump_class_hierarchy_r (FILE *, dump_flags_t, tree, tree, int);
173 static void dump_class_hierarchy (tree);
174 static void dump_class_hierarchy_1 (FILE *, dump_flags_t, tree);
175 static void dump_array (FILE *, tree);
176 static void dump_vtable (tree, tree, tree);
177 static void dump_vtt (tree, tree);
178 static void dump_thunk (FILE *, int, tree);
179 static tree build_vtable (tree, tree, tree);
180 static void initialize_vtable (tree, vec<constructor_elt, va_gc> *);
181 static void layout_nonempty_base_or_field (record_layout_info,
182 tree, tree, splay_tree);
183 static tree end_of_class (tree, int);
184 static bool layout_empty_base (record_layout_info, tree, tree, splay_tree);
185 static void accumulate_vtbl_inits (tree, tree, tree, tree, tree,
186 vec<constructor_elt, va_gc> **);
187 static void dfs_accumulate_vtbl_inits (tree, tree, tree, tree, tree,
188 vec<constructor_elt, va_gc> **);
189 static void build_rtti_vtbl_entries (tree, vtbl_init_data *);
190 static void build_vcall_and_vbase_vtbl_entries (tree, vtbl_init_data *);
191 static void clone_constructors_and_destructors (tree);
192 static tree build_clone (tree, tree);
193 static void update_vtable_entry_for_fn (tree, tree, tree, tree *, unsigned);
194 static void build_ctor_vtbl_group (tree, tree);
195 static void build_vtt (tree);
196 static tree binfo_ctor_vtable (tree);
197 static void build_vtt_inits (tree, tree, vec<constructor_elt, va_gc> **,
198 tree *);
199 static tree dfs_build_secondary_vptr_vtt_inits (tree, void *);
200 static tree dfs_fixup_binfo_vtbls (tree, void *);
201 static int record_subobject_offset (tree, tree, splay_tree);
202 static int check_subobject_offset (tree, tree, splay_tree);
203 static int walk_subobject_offsets (tree, subobject_offset_fn,
204 tree, splay_tree, tree, int);
205 static void record_subobject_offsets (tree, tree, splay_tree, bool);
206 static int layout_conflict_p (tree, tree, splay_tree, int);
207 static int splay_tree_compare_integer_csts (splay_tree_key k1,
208 splay_tree_key k2);
209 static void warn_about_ambiguous_bases (tree);
210 static bool type_requires_array_cookie (tree);
211 static bool base_derived_from (tree, tree);
212 static int empty_base_at_nonzero_offset_p (tree, tree, splay_tree);
213 static tree end_of_base (tree);
214 static tree get_vcall_index (tree, tree);
215 static bool type_maybe_constexpr_default_constructor (tree);
217 /* Return a COND_EXPR that executes TRUE_STMT if this execution of the
218 'structor is in charge of 'structing virtual bases, or FALSE_STMT
219 otherwise. */
221 tree
222 build_if_in_charge (tree true_stmt, tree false_stmt)
224 gcc_assert (DECL_HAS_IN_CHARGE_PARM_P (current_function_decl));
225 tree cmp = build2 (NE_EXPR, boolean_type_node,
226 current_in_charge_parm, integer_zero_node);
227 tree type = unlowered_expr_type (true_stmt);
228 if (VOID_TYPE_P (type))
229 type = unlowered_expr_type (false_stmt);
230 tree cond = build3 (COND_EXPR, type,
231 cmp, true_stmt, false_stmt);
232 return cond;
235 /* Convert to or from a base subobject. EXPR is an expression of type
236 `A' or `A*', an expression of type `B' or `B*' is returned. To
237 convert A to a base B, CODE is PLUS_EXPR and BINFO is the binfo for
238 the B base instance within A. To convert base A to derived B, CODE
239 is MINUS_EXPR and BINFO is the binfo for the A instance within B.
240 In this latter case, A must not be a morally virtual base of B.
241 NONNULL is true if EXPR is known to be non-NULL (this is only
242 needed when EXPR is of pointer type). CV qualifiers are preserved
243 from EXPR. */
245 tree
246 build_base_path (enum tree_code code,
247 tree expr,
248 tree binfo,
249 int nonnull,
250 tsubst_flags_t complain)
252 tree v_binfo = NULL_TREE;
253 tree d_binfo = NULL_TREE;
254 tree probe;
255 tree offset;
256 tree target_type;
257 tree null_test = NULL;
258 tree ptr_target_type;
259 int fixed_type_p;
260 int want_pointer = TYPE_PTR_P (TREE_TYPE (expr));
261 bool has_empty = false;
262 bool virtual_access;
263 bool rvalue = false;
265 if (expr == error_mark_node || binfo == error_mark_node || !binfo)
266 return error_mark_node;
268 for (probe = binfo; probe; probe = BINFO_INHERITANCE_CHAIN (probe))
270 d_binfo = probe;
271 if (is_empty_class (BINFO_TYPE (probe)))
272 has_empty = true;
273 if (!v_binfo && BINFO_VIRTUAL_P (probe))
274 v_binfo = probe;
277 probe = TYPE_MAIN_VARIANT (TREE_TYPE (expr));
278 if (want_pointer)
279 probe = TYPE_MAIN_VARIANT (TREE_TYPE (probe));
280 if (dependent_type_p (probe))
281 if (tree open = currently_open_class (probe))
282 probe = open;
284 if (code == PLUS_EXPR
285 && !SAME_BINFO_TYPE_P (BINFO_TYPE (d_binfo), probe))
287 /* This can happen when adjust_result_of_qualified_name_lookup can't
288 find a unique base binfo in a call to a member function. We
289 couldn't give the diagnostic then since we might have been calling
290 a static member function, so we do it now. In other cases, eg.
291 during error recovery (c++/71979), we may not have a base at all. */
292 if (complain & tf_error)
294 tree base = lookup_base (probe, BINFO_TYPE (d_binfo),
295 ba_unique, NULL, complain);
296 gcc_assert (base == error_mark_node || !base);
298 return error_mark_node;
301 gcc_assert ((code == MINUS_EXPR
302 && SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), probe))
303 || code == PLUS_EXPR);
305 if (binfo == d_binfo)
306 /* Nothing to do. */
307 return expr;
309 if (code == MINUS_EXPR && v_binfo)
311 if (complain & tf_error)
313 if (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), BINFO_TYPE (v_binfo)))
315 if (want_pointer)
316 error ("cannot convert from pointer to base class %qT to "
317 "pointer to derived class %qT because the base is "
318 "virtual", BINFO_TYPE (binfo), BINFO_TYPE (d_binfo));
319 else
320 error ("cannot convert from base class %qT to derived "
321 "class %qT because the base is virtual",
322 BINFO_TYPE (binfo), BINFO_TYPE (d_binfo));
324 else
326 if (want_pointer)
327 error ("cannot convert from pointer to base class %qT to "
328 "pointer to derived class %qT via virtual base %qT",
329 BINFO_TYPE (binfo), BINFO_TYPE (d_binfo),
330 BINFO_TYPE (v_binfo));
331 else
332 error ("cannot convert from base class %qT to derived "
333 "class %qT via virtual base %qT", BINFO_TYPE (binfo),
334 BINFO_TYPE (d_binfo), BINFO_TYPE (v_binfo));
337 return error_mark_node;
340 if (!want_pointer)
342 rvalue = !lvalue_p (expr);
343 /* This must happen before the call to save_expr. */
344 expr = cp_build_addr_expr (expr, complain);
346 else
347 expr = mark_rvalue_use (expr);
349 offset = BINFO_OFFSET (binfo);
350 fixed_type_p = resolves_to_fixed_type_p (expr, &nonnull);
351 target_type = code == PLUS_EXPR ? BINFO_TYPE (binfo) : BINFO_TYPE (d_binfo);
352 /* TARGET_TYPE has been extracted from BINFO, and, is therefore always
353 cv-unqualified. Extract the cv-qualifiers from EXPR so that the
354 expression returned matches the input. */
355 target_type = cp_build_qualified_type
356 (target_type, cp_type_quals (TREE_TYPE (TREE_TYPE (expr))));
357 ptr_target_type = build_pointer_type (target_type);
359 /* Do we need to look in the vtable for the real offset? */
360 virtual_access = (v_binfo && fixed_type_p <= 0);
362 /* Don't bother with the calculations inside sizeof; they'll ICE if the
363 source type is incomplete and the pointer value doesn't matter. In a
364 template (even in instantiate_non_dependent_expr), we don't have vtables
365 set up properly yet, and the value doesn't matter there either; we're
366 just interested in the result of overload resolution. */
367 if (cp_unevaluated_operand != 0
368 || processing_template_decl
369 || in_template_function ())
371 expr = build_nop (ptr_target_type, expr);
372 goto indout;
375 if (!COMPLETE_TYPE_P (probe))
377 if (complain & tf_error)
378 error ("cannot convert from %qT to base class %qT because %qT is "
379 "incomplete", BINFO_TYPE (d_binfo), BINFO_TYPE (binfo),
380 BINFO_TYPE (d_binfo));
381 return error_mark_node;
384 /* If we're in an NSDMI, we don't have the full constructor context yet
385 that we need for converting to a virtual base, so just build a stub
386 CONVERT_EXPR and expand it later in bot_replace. */
387 if (virtual_access && fixed_type_p < 0
388 && current_scope () != current_function_decl)
390 expr = build1 (CONVERT_EXPR, ptr_target_type, expr);
391 CONVERT_EXPR_VBASE_PATH (expr) = true;
392 goto indout;
395 /* Do we need to check for a null pointer? */
396 if (want_pointer && !nonnull)
398 /* If we know the conversion will not actually change the value
399 of EXPR, then we can avoid testing the expression for NULL.
400 We have to avoid generating a COMPONENT_REF for a base class
401 field, because other parts of the compiler know that such
402 expressions are always non-NULL. */
403 if (!virtual_access && integer_zerop (offset))
404 return build_nop (ptr_target_type, expr);
405 null_test = error_mark_node;
408 /* Protect against multiple evaluation if necessary. */
409 if (TREE_SIDE_EFFECTS (expr) && (null_test || virtual_access))
410 expr = save_expr (expr);
412 /* Now that we've saved expr, build the real null test. */
413 if (null_test)
415 tree zero = cp_convert (TREE_TYPE (expr), nullptr_node, complain);
416 null_test = build2_loc (input_location, NE_EXPR, boolean_type_node,
417 expr, zero);
418 /* This is a compiler generated comparison, don't emit
419 e.g. -Wnonnull-compare warning for it. */
420 TREE_NO_WARNING (null_test) = 1;
423 /* If this is a simple base reference, express it as a COMPONENT_REF. */
424 if (code == PLUS_EXPR && !virtual_access
425 /* We don't build base fields for empty bases, and they aren't very
426 interesting to the optimizers anyway. */
427 && !has_empty)
429 expr = cp_build_fold_indirect_ref (expr);
430 expr = build_simple_base_path (expr, binfo);
431 if (rvalue && lvalue_p (expr))
432 expr = move (expr);
433 if (want_pointer)
434 expr = build_address (expr);
435 target_type = TREE_TYPE (expr);
436 goto out;
439 if (virtual_access)
441 /* Going via virtual base V_BINFO. We need the static offset
442 from V_BINFO to BINFO, and the dynamic offset from D_BINFO to
443 V_BINFO. That offset is an entry in D_BINFO's vtable. */
444 tree v_offset;
446 if (fixed_type_p < 0 && in_base_initializer)
448 /* In a base member initializer, we cannot rely on the
449 vtable being set up. We have to indirect via the
450 vtt_parm. */
451 tree t;
453 t = TREE_TYPE (TYPE_VFIELD (current_class_type));
454 t = build_pointer_type (t);
455 v_offset = fold_convert (t, current_vtt_parm);
456 v_offset = cp_build_fold_indirect_ref (v_offset);
458 else
460 tree t = expr;
461 if (sanitize_flags_p (SANITIZE_VPTR)
462 && fixed_type_p == 0)
464 t = cp_ubsan_maybe_instrument_cast_to_vbase (input_location,
465 probe, expr);
466 if (t == NULL_TREE)
467 t = expr;
469 v_offset = build_vfield_ref (cp_build_fold_indirect_ref (t),
470 TREE_TYPE (TREE_TYPE (expr)));
473 if (v_offset == error_mark_node)
474 return error_mark_node;
476 v_offset = fold_build_pointer_plus (v_offset, BINFO_VPTR_FIELD (v_binfo));
477 v_offset = build1 (NOP_EXPR,
478 build_pointer_type (ptrdiff_type_node),
479 v_offset);
480 v_offset = cp_build_fold_indirect_ref (v_offset);
481 TREE_CONSTANT (v_offset) = 1;
483 offset = convert_to_integer (ptrdiff_type_node,
484 size_diffop_loc (input_location, offset,
485 BINFO_OFFSET (v_binfo)));
487 if (!integer_zerop (offset))
488 v_offset = build2 (code, ptrdiff_type_node, v_offset, offset);
490 if (fixed_type_p < 0)
491 /* Negative fixed_type_p means this is a constructor or destructor;
492 virtual base layout is fixed in in-charge [cd]tors, but not in
493 base [cd]tors. */
494 offset = build_if_in_charge
495 (convert_to_integer (ptrdiff_type_node, BINFO_OFFSET (binfo)),
496 v_offset);
497 else
498 offset = v_offset;
501 if (want_pointer)
502 target_type = ptr_target_type;
504 expr = build1 (NOP_EXPR, ptr_target_type, expr);
506 if (!integer_zerop (offset))
508 offset = fold_convert (sizetype, offset);
509 if (code == MINUS_EXPR)
510 offset = fold_build1_loc (input_location, NEGATE_EXPR, sizetype, offset);
511 expr = fold_build_pointer_plus (expr, offset);
513 else
514 null_test = NULL;
516 indout:
517 if (!want_pointer)
519 expr = cp_build_fold_indirect_ref (expr);
520 if (rvalue)
521 expr = move (expr);
524 out:
525 if (null_test)
526 expr = fold_build3_loc (input_location, COND_EXPR, target_type, null_test, expr,
527 build_zero_cst (target_type));
529 return expr;
532 /* Subroutine of build_base_path; EXPR and BINFO are as in that function.
533 Perform a derived-to-base conversion by recursively building up a
534 sequence of COMPONENT_REFs to the appropriate base fields. */
536 static tree
537 build_simple_base_path (tree expr, tree binfo)
539 tree type = BINFO_TYPE (binfo);
540 tree d_binfo = BINFO_INHERITANCE_CHAIN (binfo);
541 tree field;
543 if (d_binfo == NULL_TREE)
545 tree temp;
547 gcc_assert (TYPE_MAIN_VARIANT (TREE_TYPE (expr)) == type);
549 /* Transform `(a, b).x' into `(*(a, &b)).x', `(a ? b : c).x'
550 into `(*(a ? &b : &c)).x', and so on. A COND_EXPR is only
551 an lvalue in the front end; only _DECLs and _REFs are lvalues
552 in the back end. */
553 temp = unary_complex_lvalue (ADDR_EXPR, expr);
554 if (temp)
555 expr = cp_build_fold_indirect_ref (temp);
557 return expr;
560 /* Recurse. */
561 expr = build_simple_base_path (expr, d_binfo);
563 for (field = TYPE_FIELDS (BINFO_TYPE (d_binfo));
564 field; field = DECL_CHAIN (field))
565 /* Is this the base field created by build_base_field? */
566 if (TREE_CODE (field) == FIELD_DECL
567 && DECL_FIELD_IS_BASE (field)
568 && TREE_TYPE (field) == type
569 /* If we're looking for a field in the most-derived class,
570 also check the field offset; we can have two base fields
571 of the same type if one is an indirect virtual base and one
572 is a direct non-virtual base. */
573 && (BINFO_INHERITANCE_CHAIN (d_binfo)
574 || tree_int_cst_equal (byte_position (field),
575 BINFO_OFFSET (binfo))))
577 /* We don't use build_class_member_access_expr here, as that
578 has unnecessary checks, and more importantly results in
579 recursive calls to dfs_walk_once. */
580 int type_quals = cp_type_quals (TREE_TYPE (expr));
582 expr = build3 (COMPONENT_REF,
583 cp_build_qualified_type (type, type_quals),
584 expr, field, NULL_TREE);
585 /* Mark the expression const or volatile, as appropriate.
586 Even though we've dealt with the type above, we still have
587 to mark the expression itself. */
588 if (type_quals & TYPE_QUAL_CONST)
589 TREE_READONLY (expr) = 1;
590 if (type_quals & TYPE_QUAL_VOLATILE)
591 TREE_THIS_VOLATILE (expr) = 1;
593 return expr;
596 /* Didn't find the base field?!? */
597 gcc_unreachable ();
600 /* Convert OBJECT to the base TYPE. OBJECT is an expression whose
601 type is a class type or a pointer to a class type. In the former
602 case, TYPE is also a class type; in the latter it is another
603 pointer type. If CHECK_ACCESS is true, an error message is emitted
604 if TYPE is inaccessible. If OBJECT has pointer type, the value is
605 assumed to be non-NULL. */
607 tree
608 convert_to_base (tree object, tree type, bool check_access, bool nonnull,
609 tsubst_flags_t complain)
611 tree binfo;
612 tree object_type;
614 if (TYPE_PTR_P (TREE_TYPE (object)))
616 object_type = TREE_TYPE (TREE_TYPE (object));
617 type = TREE_TYPE (type);
619 else
620 object_type = TREE_TYPE (object);
622 binfo = lookup_base (object_type, type, check_access ? ba_check : ba_unique,
623 NULL, complain);
624 if (!binfo || binfo == error_mark_node)
625 return error_mark_node;
627 return build_base_path (PLUS_EXPR, object, binfo, nonnull, complain);
630 /* EXPR is an expression with unqualified class type. BASE is a base
631 binfo of that class type. Returns EXPR, converted to the BASE
632 type. This function assumes that EXPR is the most derived class;
633 therefore virtual bases can be found at their static offsets. */
635 tree
636 convert_to_base_statically (tree expr, tree base)
638 tree expr_type;
640 expr_type = TREE_TYPE (expr);
641 if (!SAME_BINFO_TYPE_P (BINFO_TYPE (base), expr_type))
643 /* If this is a non-empty base, use a COMPONENT_REF. */
644 if (!is_empty_class (BINFO_TYPE (base)))
645 return build_simple_base_path (expr, base);
647 /* We use fold_build2 and fold_convert below to simplify the trees
648 provided to the optimizers. It is not safe to call these functions
649 when processing a template because they do not handle C++-specific
650 trees. */
651 gcc_assert (!processing_template_decl);
652 expr = cp_build_addr_expr (expr, tf_warning_or_error);
653 if (!integer_zerop (BINFO_OFFSET (base)))
654 expr = fold_build_pointer_plus_loc (input_location,
655 expr, BINFO_OFFSET (base));
656 expr = fold_convert (build_pointer_type (BINFO_TYPE (base)), expr);
657 expr = build_fold_indirect_ref_loc (input_location, expr);
660 return expr;
664 tree
665 build_vfield_ref (tree datum, tree type)
667 tree vfield, vcontext;
669 if (datum == error_mark_node
670 /* Can happen in case of duplicate base types (c++/59082). */
671 || !TYPE_VFIELD (type))
672 return error_mark_node;
674 /* First, convert to the requested type. */
675 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (datum), type))
676 datum = convert_to_base (datum, type, /*check_access=*/false,
677 /*nonnull=*/true, tf_warning_or_error);
679 /* Second, the requested type may not be the owner of its own vptr.
680 If not, convert to the base class that owns it. We cannot use
681 convert_to_base here, because VCONTEXT may appear more than once
682 in the inheritance hierarchy of TYPE, and thus direct conversion
683 between the types may be ambiguous. Following the path back up
684 one step at a time via primary bases avoids the problem. */
685 vfield = TYPE_VFIELD (type);
686 vcontext = DECL_CONTEXT (vfield);
687 while (!same_type_ignoring_top_level_qualifiers_p (vcontext, type))
689 datum = build_simple_base_path (datum, CLASSTYPE_PRIMARY_BINFO (type));
690 type = TREE_TYPE (datum);
693 return build3 (COMPONENT_REF, TREE_TYPE (vfield), datum, vfield, NULL_TREE);
696 /* Given an object INSTANCE, return an expression which yields the
697 vtable element corresponding to INDEX. There are many special
698 cases for INSTANCE which we take care of here, mainly to avoid
699 creating extra tree nodes when we don't have to. */
701 tree
702 build_vtbl_ref (tree instance, tree idx)
704 tree aref;
705 tree vtbl = NULL_TREE;
707 /* Try to figure out what a reference refers to, and
708 access its virtual function table directly. */
710 int cdtorp = 0;
711 tree fixed_type = fixed_type_or_null (instance, NULL, &cdtorp);
713 tree basetype = non_reference (TREE_TYPE (instance));
715 if (fixed_type && !cdtorp)
717 tree binfo = lookup_base (fixed_type, basetype,
718 ba_unique, NULL, tf_none);
719 if (binfo && binfo != error_mark_node)
720 vtbl = unshare_expr (BINFO_VTABLE (binfo));
723 if (!vtbl)
724 vtbl = build_vfield_ref (instance, basetype);
726 aref = build_array_ref (input_location, vtbl, idx);
727 TREE_CONSTANT (aref) |= TREE_CONSTANT (vtbl) && TREE_CONSTANT (idx);
729 return aref;
732 /* Given a stable object pointer INSTANCE_PTR, return an expression which
733 yields a function pointer corresponding to vtable element INDEX. */
735 tree
736 build_vfn_ref (tree instance_ptr, tree idx)
738 tree aref;
740 aref = build_vtbl_ref (cp_build_fold_indirect_ref (instance_ptr), idx);
742 /* When using function descriptors, the address of the
743 vtable entry is treated as a function pointer. */
744 if (TARGET_VTABLE_USES_DESCRIPTORS)
745 aref = build1 (NOP_EXPR, TREE_TYPE (aref),
746 cp_build_addr_expr (aref, tf_warning_or_error));
748 /* Remember this as a method reference, for later devirtualization. */
749 aref = build3 (OBJ_TYPE_REF, TREE_TYPE (aref), aref, instance_ptr, idx);
751 return aref;
754 /* Return the name of the virtual function table (as an IDENTIFIER_NODE)
755 for the given TYPE. */
757 static tree
758 get_vtable_name (tree type)
760 return mangle_vtbl_for_type (type);
763 /* DECL is an entity associated with TYPE, like a virtual table or an
764 implicitly generated constructor. Determine whether or not DECL
765 should have external or internal linkage at the object file
766 level. This routine does not deal with COMDAT linkage and other
767 similar complexities; it simply sets TREE_PUBLIC if it possible for
768 entities in other translation units to contain copies of DECL, in
769 the abstract. */
771 void
772 set_linkage_according_to_type (tree /*type*/, tree decl)
774 TREE_PUBLIC (decl) = 1;
775 determine_visibility (decl);
778 /* Create a VAR_DECL for a primary or secondary vtable for CLASS_TYPE.
779 (For a secondary vtable for B-in-D, CLASS_TYPE should be D, not B.)
780 Use NAME for the name of the vtable, and VTABLE_TYPE for its type. */
782 static tree
783 build_vtable (tree class_type, tree name, tree vtable_type)
785 tree decl;
787 decl = build_lang_decl (VAR_DECL, name, vtable_type);
788 /* vtable names are already mangled; give them their DECL_ASSEMBLER_NAME
789 now to avoid confusion in mangle_decl. */
790 SET_DECL_ASSEMBLER_NAME (decl, name);
791 DECL_CONTEXT (decl) = class_type;
792 DECL_ARTIFICIAL (decl) = 1;
793 TREE_STATIC (decl) = 1;
794 TREE_READONLY (decl) = 1;
795 DECL_VIRTUAL_P (decl) = 1;
796 SET_DECL_ALIGN (decl, TARGET_VTABLE_ENTRY_ALIGN);
797 DECL_USER_ALIGN (decl) = true;
798 DECL_VTABLE_OR_VTT_P (decl) = 1;
799 set_linkage_according_to_type (class_type, decl);
800 /* The vtable has not been defined -- yet. */
801 DECL_EXTERNAL (decl) = 1;
802 DECL_NOT_REALLY_EXTERN (decl) = 1;
804 /* Mark the VAR_DECL node representing the vtable itself as a
805 "gratuitous" one, thereby forcing dwarfout.c to ignore it. It
806 is rather important that such things be ignored because any
807 effort to actually generate DWARF for them will run into
808 trouble when/if we encounter code like:
810 #pragma interface
811 struct S { virtual void member (); };
813 because the artificial declaration of the vtable itself (as
814 manufactured by the g++ front end) will say that the vtable is
815 a static member of `S' but only *after* the debug output for
816 the definition of `S' has already been output. This causes
817 grief because the DWARF entry for the definition of the vtable
818 will try to refer back to an earlier *declaration* of the
819 vtable as a static member of `S' and there won't be one. We
820 might be able to arrange to have the "vtable static member"
821 attached to the member list for `S' before the debug info for
822 `S' get written (which would solve the problem) but that would
823 require more intrusive changes to the g++ front end. */
824 DECL_IGNORED_P (decl) = 1;
826 return decl;
829 /* Get the VAR_DECL of the vtable for TYPE. TYPE need not be polymorphic,
830 or even complete. If this does not exist, create it. If COMPLETE is
831 nonzero, then complete the definition of it -- that will render it
832 impossible to actually build the vtable, but is useful to get at those
833 which are known to exist in the runtime. */
835 tree
836 get_vtable_decl (tree type, int complete)
838 tree decl;
840 if (CLASSTYPE_VTABLES (type))
841 return CLASSTYPE_VTABLES (type);
843 decl = build_vtable (type, get_vtable_name (type), vtbl_type_node);
844 CLASSTYPE_VTABLES (type) = decl;
846 if (complete)
848 DECL_EXTERNAL (decl) = 1;
849 cp_finish_decl (decl, NULL_TREE, false, NULL_TREE, 0);
852 return decl;
855 /* Build the primary virtual function table for TYPE. If BINFO is
856 non-NULL, build the vtable starting with the initial approximation
857 that it is the same as the one which is the head of the association
858 list. Returns a nonzero value if a new vtable is actually
859 created. */
861 static int
862 build_primary_vtable (tree binfo, tree type)
864 tree decl;
865 tree virtuals;
867 decl = get_vtable_decl (type, /*complete=*/0);
869 if (binfo)
871 if (BINFO_NEW_VTABLE_MARKED (binfo))
872 /* We have already created a vtable for this base, so there's
873 no need to do it again. */
874 return 0;
876 virtuals = copy_list (BINFO_VIRTUALS (binfo));
877 TREE_TYPE (decl) = TREE_TYPE (get_vtbl_decl_for_binfo (binfo));
878 DECL_SIZE (decl) = TYPE_SIZE (TREE_TYPE (decl));
879 DECL_SIZE_UNIT (decl) = TYPE_SIZE_UNIT (TREE_TYPE (decl));
881 else
883 gcc_assert (TREE_TYPE (decl) == vtbl_type_node);
884 virtuals = NULL_TREE;
887 /* Initialize the association list for this type, based
888 on our first approximation. */
889 BINFO_VTABLE (TYPE_BINFO (type)) = decl;
890 BINFO_VIRTUALS (TYPE_BINFO (type)) = virtuals;
891 SET_BINFO_NEW_VTABLE_MARKED (TYPE_BINFO (type));
892 return 1;
895 /* Give BINFO a new virtual function table which is initialized
896 with a skeleton-copy of its original initialization. The only
897 entry that changes is the `delta' entry, so we can really
898 share a lot of structure.
900 FOR_TYPE is the most derived type which caused this table to
901 be needed.
903 Returns nonzero if we haven't met BINFO before.
905 The order in which vtables are built (by calling this function) for
906 an object must remain the same, otherwise a binary incompatibility
907 can result. */
909 static int
910 build_secondary_vtable (tree binfo)
912 if (BINFO_NEW_VTABLE_MARKED (binfo))
913 /* We already created a vtable for this base. There's no need to
914 do it again. */
915 return 0;
917 /* Remember that we've created a vtable for this BINFO, so that we
918 don't try to do so again. */
919 SET_BINFO_NEW_VTABLE_MARKED (binfo);
921 /* Make fresh virtual list, so we can smash it later. */
922 BINFO_VIRTUALS (binfo) = copy_list (BINFO_VIRTUALS (binfo));
924 /* Secondary vtables are laid out as part of the same structure as
925 the primary vtable. */
926 BINFO_VTABLE (binfo) = NULL_TREE;
927 return 1;
930 /* Create a new vtable for BINFO which is the hierarchy dominated by
931 T. Return nonzero if we actually created a new vtable. */
933 static int
934 make_new_vtable (tree t, tree binfo)
936 if (binfo == TYPE_BINFO (t))
937 /* In this case, it is *type*'s vtable we are modifying. We start
938 with the approximation that its vtable is that of the
939 immediate base class. */
940 return build_primary_vtable (binfo, t);
941 else
942 /* This is our very own copy of `basetype' to play with. Later,
943 we will fill in all the virtual functions that override the
944 virtual functions in these base classes which are not defined
945 by the current type. */
946 return build_secondary_vtable (binfo);
949 /* Make *VIRTUALS, an entry on the BINFO_VIRTUALS list for BINFO
950 (which is in the hierarchy dominated by T) list FNDECL as its
951 BV_FN. DELTA is the required constant adjustment from the `this'
952 pointer where the vtable entry appears to the `this' required when
953 the function is actually called. */
955 static void
956 modify_vtable_entry (tree t,
957 tree binfo,
958 tree fndecl,
959 tree delta,
960 tree *virtuals)
962 tree v;
964 v = *virtuals;
966 if (fndecl != BV_FN (v)
967 || !tree_int_cst_equal (delta, BV_DELTA (v)))
969 /* We need a new vtable for BINFO. */
970 if (make_new_vtable (t, binfo))
972 /* If we really did make a new vtable, we also made a copy
973 of the BINFO_VIRTUALS list. Now, we have to find the
974 corresponding entry in that list. */
975 *virtuals = BINFO_VIRTUALS (binfo);
976 while (BV_FN (*virtuals) != BV_FN (v))
977 *virtuals = TREE_CHAIN (*virtuals);
978 v = *virtuals;
981 BV_DELTA (v) = delta;
982 BV_VCALL_INDEX (v) = NULL_TREE;
983 BV_FN (v) = fndecl;
988 /* Add method METHOD to class TYPE. If VIA_USING indicates whether
989 METHOD is being injected via a using_decl. Returns true if the
990 method could be added to the method vec. */
992 bool
993 add_method (tree type, tree method, bool via_using)
995 if (method == error_mark_node)
996 return false;
998 gcc_assert (!DECL_EXTERN_C_P (method));
1000 tree *slot = find_member_slot (type, DECL_NAME (method));
1001 tree current_fns = slot ? *slot : NULL_TREE;
1003 /* Check to see if we've already got this method. */
1004 for (ovl_iterator iter (current_fns); iter; ++iter)
1006 tree fn = *iter;
1007 tree fn_type;
1008 tree method_type;
1009 tree parms1;
1010 tree parms2;
1012 if (TREE_CODE (fn) != TREE_CODE (method))
1013 continue;
1015 /* Two using-declarations can coexist, we'll complain about ambiguity in
1016 overload resolution. */
1017 if (via_using && iter.using_p ()
1018 /* Except handle inherited constructors specially. */
1019 && ! DECL_CONSTRUCTOR_P (fn))
1020 continue;
1022 /* [over.load] Member function declarations with the
1023 same name and the same parameter types cannot be
1024 overloaded if any of them is a static member
1025 function declaration.
1027 [over.load] Member function declarations with the same name and
1028 the same parameter-type-list as well as member function template
1029 declarations with the same name, the same parameter-type-list, and
1030 the same template parameter lists cannot be overloaded if any of
1031 them, but not all, have a ref-qualifier.
1033 [namespace.udecl] When a using-declaration brings names
1034 from a base class into a derived class scope, member
1035 functions in the derived class override and/or hide member
1036 functions with the same name and parameter types in a base
1037 class (rather than conflicting). */
1038 fn_type = TREE_TYPE (fn);
1039 method_type = TREE_TYPE (method);
1040 parms1 = TYPE_ARG_TYPES (fn_type);
1041 parms2 = TYPE_ARG_TYPES (method_type);
1043 /* Compare the quals on the 'this' parm. Don't compare
1044 the whole types, as used functions are treated as
1045 coming from the using class in overload resolution. */
1046 if (! DECL_STATIC_FUNCTION_P (fn)
1047 && ! DECL_STATIC_FUNCTION_P (method)
1048 /* Either both or neither need to be ref-qualified for
1049 differing quals to allow overloading. */
1050 && (FUNCTION_REF_QUALIFIED (fn_type)
1051 == FUNCTION_REF_QUALIFIED (method_type))
1052 && (type_memfn_quals (fn_type) != type_memfn_quals (method_type)
1053 || type_memfn_rqual (fn_type) != type_memfn_rqual (method_type)))
1054 continue;
1056 /* For templates, the return type and template parameters
1057 must be identical. */
1058 if (TREE_CODE (fn) == TEMPLATE_DECL
1059 && (!same_type_p (TREE_TYPE (fn_type),
1060 TREE_TYPE (method_type))
1061 || !comp_template_parms (DECL_TEMPLATE_PARMS (fn),
1062 DECL_TEMPLATE_PARMS (method))))
1063 continue;
1065 if (! DECL_STATIC_FUNCTION_P (fn))
1066 parms1 = TREE_CHAIN (parms1);
1067 if (! DECL_STATIC_FUNCTION_P (method))
1068 parms2 = TREE_CHAIN (parms2);
1070 /* Bring back parameters omitted from an inherited ctor. */
1071 if (ctor_omit_inherited_parms (fn))
1072 parms1 = FUNCTION_FIRST_USER_PARMTYPE (DECL_ORIGIN (fn));
1073 if (ctor_omit_inherited_parms (method))
1074 parms2 = FUNCTION_FIRST_USER_PARMTYPE (DECL_ORIGIN (method));
1076 if (compparms (parms1, parms2)
1077 && (!DECL_CONV_FN_P (fn)
1078 || same_type_p (TREE_TYPE (fn_type),
1079 TREE_TYPE (method_type)))
1080 && equivalently_constrained (fn, method))
1082 /* If these are versions of the same function, process and
1083 move on. */
1084 if (TREE_CODE (fn) == FUNCTION_DECL
1085 && maybe_version_functions (method, fn, true))
1086 continue;
1088 if (DECL_INHERITED_CTOR (method))
1090 if (DECL_INHERITED_CTOR (fn))
1092 tree basem = DECL_INHERITED_CTOR_BASE (method);
1093 tree basef = DECL_INHERITED_CTOR_BASE (fn);
1094 if (flag_new_inheriting_ctors)
1096 if (basem == basef)
1098 /* Inheriting the same constructor along different
1099 paths, combine them. */
1100 SET_DECL_INHERITED_CTOR
1101 (fn, ovl_make (DECL_INHERITED_CTOR (method),
1102 DECL_INHERITED_CTOR (fn)));
1103 /* And discard the new one. */
1104 return false;
1106 else
1107 /* Inherited ctors can coexist until overload
1108 resolution. */
1109 continue;
1111 error_at (DECL_SOURCE_LOCATION (method),
1112 "%q#D conflicts with version inherited from %qT",
1113 method, basef);
1114 inform (DECL_SOURCE_LOCATION (fn),
1115 "version inherited from %qT declared here",
1116 basef);
1118 /* Otherwise defer to the other function. */
1119 return false;
1122 if (via_using)
1123 /* Defer to the local function. */
1124 return false;
1125 else if (flag_new_inheriting_ctors
1126 && DECL_INHERITED_CTOR (fn))
1128 /* Remove the inherited constructor. */
1129 current_fns = iter.remove_node (current_fns);
1130 continue;
1132 else
1134 error_at (DECL_SOURCE_LOCATION (method),
1135 "%q#D cannot be overloaded with %q#D", method, fn);
1136 inform (DECL_SOURCE_LOCATION (fn),
1137 "previous declaration %q#D", fn);
1138 return false;
1143 /* A class should never have more than one destructor. */
1144 gcc_assert (!current_fns || !DECL_DESTRUCTOR_P (method));
1146 current_fns = ovl_insert (method, current_fns, via_using);
1148 if (!COMPLETE_TYPE_P (type) && !DECL_CONV_FN_P (method)
1149 && !push_class_level_binding (DECL_NAME (method), current_fns))
1150 return false;
1152 if (!slot)
1153 slot = add_member_slot (type, DECL_NAME (method));
1155 /* Maintain TYPE_HAS_USER_CONSTRUCTOR, etc. */
1156 grok_special_member_properties (method);
1158 *slot = current_fns;
1160 return true;
1163 /* Subroutines of finish_struct. */
1165 /* Change the access of FDECL to ACCESS in T. Return 1 if change was
1166 legit, otherwise return 0. */
1168 static int
1169 alter_access (tree t, tree fdecl, tree access)
1171 tree elem;
1173 retrofit_lang_decl (fdecl);
1175 gcc_assert (!DECL_DISCRIMINATOR_P (fdecl));
1177 elem = purpose_member (t, DECL_ACCESS (fdecl));
1178 if (elem)
1180 if (TREE_VALUE (elem) != access)
1182 if (TREE_CODE (TREE_TYPE (fdecl)) == FUNCTION_DECL)
1183 error ("conflicting access specifications for method"
1184 " %q+D, ignored", TREE_TYPE (fdecl));
1185 else
1186 error ("conflicting access specifications for field %qE, ignored",
1187 DECL_NAME (fdecl));
1189 else
1191 /* They're changing the access to the same thing they changed
1192 it to before. That's OK. */
1196 else
1198 perform_or_defer_access_check (TYPE_BINFO (t), fdecl, fdecl,
1199 tf_warning_or_error);
1200 DECL_ACCESS (fdecl) = tree_cons (t, access, DECL_ACCESS (fdecl));
1201 return 1;
1203 return 0;
1206 /* Return the access node for DECL's access in its enclosing class. */
1208 tree
1209 declared_access (tree decl)
1211 return (TREE_PRIVATE (decl) ? access_private_node
1212 : TREE_PROTECTED (decl) ? access_protected_node
1213 : access_public_node);
1216 /* Process the USING_DECL, which is a member of T. */
1218 static void
1219 handle_using_decl (tree using_decl, tree t)
1221 tree decl = USING_DECL_DECLS (using_decl);
1222 tree name = DECL_NAME (using_decl);
1223 tree access = declared_access (using_decl);
1224 tree flist = NULL_TREE;
1225 tree old_value;
1227 gcc_assert (!processing_template_decl && decl);
1229 old_value = lookup_member (t, name, /*protect=*/0, /*want_type=*/false,
1230 tf_warning_or_error);
1231 if (old_value)
1233 old_value = OVL_FIRST (old_value);
1235 if (DECL_P (old_value) && DECL_CONTEXT (old_value) == t)
1236 /* OK */;
1237 else
1238 old_value = NULL_TREE;
1241 cp_emit_debug_info_for_using (decl, t);
1243 if (is_overloaded_fn (decl))
1244 flist = decl;
1246 if (! old_value)
1248 else if (is_overloaded_fn (old_value))
1250 if (flist)
1251 /* It's OK to use functions from a base when there are functions with
1252 the same name already present in the current class. */;
1253 else
1255 error_at (DECL_SOURCE_LOCATION (using_decl), "%qD invalid in %q#T "
1256 "because of local method %q#D with same name",
1257 using_decl, t, old_value);
1258 inform (DECL_SOURCE_LOCATION (old_value),
1259 "local method %q#D declared here", old_value);
1260 return;
1263 else if (!DECL_ARTIFICIAL (old_value))
1265 error_at (DECL_SOURCE_LOCATION (using_decl), "%qD invalid in %q#T "
1266 "because of local member %q#D with same name",
1267 using_decl, t, old_value);
1268 inform (DECL_SOURCE_LOCATION (old_value),
1269 "local member %q#D declared here", old_value);
1270 return;
1273 /* Make type T see field decl FDECL with access ACCESS. */
1274 if (flist)
1275 for (ovl_iterator iter (flist); iter; ++iter)
1277 add_method (t, *iter, true);
1278 alter_access (t, *iter, access);
1280 else
1281 alter_access (t, decl, access);
1284 /* Data structure for find_abi_tags_r, below. */
1286 struct abi_tag_data
1288 tree t; // The type that we're checking for missing tags.
1289 tree subob; // The subobject of T that we're getting tags from.
1290 tree tags; // error_mark_node for diagnostics, or a list of missing tags.
1293 /* Subroutine of find_abi_tags_r. Handle a single TAG found on the class TP
1294 in the context of P. TAG can be either an identifier (the DECL_NAME of
1295 a tag NAMESPACE_DECL) or a STRING_CST (a tag attribute). */
1297 static void
1298 check_tag (tree tag, tree id, tree *tp, abi_tag_data *p)
1300 if (!IDENTIFIER_MARKED (id))
1302 if (p->tags != error_mark_node)
1304 /* We're collecting tags from template arguments or from
1305 the type of a variable or function return type. */
1306 p->tags = tree_cons (NULL_TREE, tag, p->tags);
1308 /* Don't inherit this tag multiple times. */
1309 IDENTIFIER_MARKED (id) = true;
1311 if (TYPE_P (p->t))
1313 /* Tags inherited from type template arguments are only used
1314 to avoid warnings. */
1315 ABI_TAG_IMPLICIT (p->tags) = true;
1316 return;
1318 /* For functions and variables we want to warn, too. */
1321 /* Otherwise we're diagnosing missing tags. */
1322 if (TREE_CODE (p->t) == FUNCTION_DECL)
1324 auto_diagnostic_group d;
1325 if (warning (OPT_Wabi_tag, "%qD inherits the %E ABI tag "
1326 "that %qT (used in its return type) has",
1327 p->t, tag, *tp))
1328 inform (location_of (*tp), "%qT declared here", *tp);
1330 else if (VAR_P (p->t))
1332 auto_diagnostic_group d;
1333 if (warning (OPT_Wabi_tag, "%qD inherits the %E ABI tag "
1334 "that %qT (used in its type) has", p->t, tag, *tp))
1335 inform (location_of (*tp), "%qT declared here", *tp);
1337 else if (TYPE_P (p->subob))
1339 auto_diagnostic_group d;
1340 if (warning (OPT_Wabi_tag, "%qT does not have the %E ABI tag "
1341 "that base %qT has", p->t, tag, p->subob))
1342 inform (location_of (p->subob), "%qT declared here",
1343 p->subob);
1345 else
1347 auto_diagnostic_group d;
1348 if (warning (OPT_Wabi_tag, "%qT does not have the %E ABI tag "
1349 "that %qT (used in the type of %qD) has",
1350 p->t, tag, *tp, p->subob))
1352 inform (location_of (p->subob), "%qD declared here",
1353 p->subob);
1354 inform (location_of (*tp), "%qT declared here", *tp);
1360 /* Find all the ABI tags in the attribute list ATTR and either call
1361 check_tag (if TP is non-null) or set IDENTIFIER_MARKED to val. */
1363 static void
1364 mark_or_check_attr_tags (tree attr, tree *tp, abi_tag_data *p, bool val)
1366 if (!attr)
1367 return;
1368 for (; (attr = lookup_attribute ("abi_tag", attr));
1369 attr = TREE_CHAIN (attr))
1370 for (tree list = TREE_VALUE (attr); list;
1371 list = TREE_CHAIN (list))
1373 tree tag = TREE_VALUE (list);
1374 tree id = get_identifier (TREE_STRING_POINTER (tag));
1375 if (tp)
1376 check_tag (tag, id, tp, p);
1377 else
1378 IDENTIFIER_MARKED (id) = val;
1382 /* Find all the ABI tags on T and its enclosing scopes and either call
1383 check_tag (if TP is non-null) or set IDENTIFIER_MARKED to val. */
1385 static void
1386 mark_or_check_tags (tree t, tree *tp, abi_tag_data *p, bool val)
1388 while (t != global_namespace)
1390 tree attr;
1391 if (TYPE_P (t))
1393 attr = TYPE_ATTRIBUTES (t);
1394 t = CP_TYPE_CONTEXT (t);
1396 else
1398 attr = DECL_ATTRIBUTES (t);
1399 t = CP_DECL_CONTEXT (t);
1401 mark_or_check_attr_tags (attr, tp, p, val);
1405 /* walk_tree callback for check_abi_tags: if the type at *TP involves any
1406 types with ABI tags, add the corresponding identifiers to the VEC in
1407 *DATA and set IDENTIFIER_MARKED. */
1409 static tree
1410 find_abi_tags_r (tree *tp, int *walk_subtrees, void *data)
1412 if (!OVERLOAD_TYPE_P (*tp))
1413 return NULL_TREE;
1415 /* walk_tree shouldn't be walking into any subtrees of a RECORD_TYPE
1416 anyway, but let's make sure of it. */
1417 *walk_subtrees = false;
1419 abi_tag_data *p = static_cast<struct abi_tag_data*>(data);
1421 mark_or_check_tags (*tp, tp, p, false);
1423 return NULL_TREE;
1426 /* walk_tree callback for mark_abi_tags: if *TP is a class, set
1427 IDENTIFIER_MARKED on its ABI tags. */
1429 static tree
1430 mark_abi_tags_r (tree *tp, int *walk_subtrees, void *data)
1432 if (!OVERLOAD_TYPE_P (*tp))
1433 return NULL_TREE;
1435 /* walk_tree shouldn't be walking into any subtrees of a RECORD_TYPE
1436 anyway, but let's make sure of it. */
1437 *walk_subtrees = false;
1439 bool *valp = static_cast<bool*>(data);
1441 mark_or_check_tags (*tp, NULL, NULL, *valp);
1443 return NULL_TREE;
1446 /* Set IDENTIFIER_MARKED on all the ABI tags on T and its enclosing
1447 scopes. */
1449 static void
1450 mark_abi_tags (tree t, bool val)
1452 mark_or_check_tags (t, NULL, NULL, val);
1453 if (DECL_P (t))
1455 if (DECL_LANG_SPECIFIC (t) && DECL_USE_TEMPLATE (t)
1456 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (t)))
1458 /* Template arguments are part of the signature. */
1459 tree level = INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (t));
1460 for (int j = 0; j < TREE_VEC_LENGTH (level); ++j)
1462 tree arg = TREE_VEC_ELT (level, j);
1463 cp_walk_tree_without_duplicates (&arg, mark_abi_tags_r, &val);
1466 if (TREE_CODE (t) == FUNCTION_DECL)
1467 /* A function's parameter types are part of the signature, so
1468 we don't need to inherit any tags that are also in them. */
1469 for (tree arg = FUNCTION_FIRST_USER_PARMTYPE (t); arg;
1470 arg = TREE_CHAIN (arg))
1471 cp_walk_tree_without_duplicates (&TREE_VALUE (arg),
1472 mark_abi_tags_r, &val);
1476 /* Check that T has all the ABI tags that subobject SUBOB has, or
1477 warn if not. If T is a (variable or function) declaration, also
1478 return any missing tags, and add them to T if JUST_CHECKING is false. */
1480 static tree
1481 check_abi_tags (tree t, tree subob, bool just_checking = false)
1483 bool inherit = DECL_P (t);
1485 if (!inherit && !warn_abi_tag)
1486 return NULL_TREE;
1488 tree decl = TYPE_P (t) ? TYPE_NAME (t) : t;
1489 if (!TREE_PUBLIC (decl))
1490 /* No need to worry about things local to this TU. */
1491 return NULL_TREE;
1493 mark_abi_tags (t, true);
1495 tree subtype = TYPE_P (subob) ? subob : TREE_TYPE (subob);
1496 struct abi_tag_data data = { t, subob, error_mark_node };
1497 if (inherit)
1498 data.tags = NULL_TREE;
1500 cp_walk_tree_without_duplicates (&subtype, find_abi_tags_r, &data);
1502 if (!(inherit && data.tags))
1503 /* We don't need to do anything with data.tags. */;
1504 else if (just_checking)
1505 for (tree t = data.tags; t; t = TREE_CHAIN (t))
1507 tree id = get_identifier (TREE_STRING_POINTER (TREE_VALUE (t)));
1508 IDENTIFIER_MARKED (id) = false;
1510 else
1512 tree attr = lookup_attribute ("abi_tag", DECL_ATTRIBUTES (t));
1513 if (attr)
1514 TREE_VALUE (attr) = chainon (data.tags, TREE_VALUE (attr));
1515 else
1516 DECL_ATTRIBUTES (t)
1517 = tree_cons (abi_tag_identifier, data.tags, DECL_ATTRIBUTES (t));
1520 mark_abi_tags (t, false);
1522 return data.tags;
1525 /* Check that DECL has all the ABI tags that are used in parts of its type
1526 that are not reflected in its mangled name. */
1528 void
1529 check_abi_tags (tree decl)
1531 if (VAR_P (decl))
1532 check_abi_tags (decl, TREE_TYPE (decl));
1533 else if (TREE_CODE (decl) == FUNCTION_DECL
1534 && !DECL_CONV_FN_P (decl)
1535 && !mangle_return_type_p (decl))
1536 check_abi_tags (decl, TREE_TYPE (TREE_TYPE (decl)));
1539 /* Return any ABI tags that are used in parts of the type of DECL
1540 that are not reflected in its mangled name. This function is only
1541 used in backward-compatible mangling for ABI <11. */
1543 tree
1544 missing_abi_tags (tree decl)
1546 if (VAR_P (decl))
1547 return check_abi_tags (decl, TREE_TYPE (decl), true);
1548 else if (TREE_CODE (decl) == FUNCTION_DECL
1549 /* Don't check DECL_CONV_FN_P here like we do in check_abi_tags, so
1550 that we can use this function for setting need_abi_warning
1551 regardless of the current flag_abi_version. */
1552 && !mangle_return_type_p (decl))
1553 return check_abi_tags (decl, TREE_TYPE (TREE_TYPE (decl)), true);
1554 else
1555 return NULL_TREE;
1558 void
1559 inherit_targ_abi_tags (tree t)
1561 if (!CLASS_TYPE_P (t)
1562 || CLASSTYPE_TEMPLATE_INFO (t) == NULL_TREE)
1563 return;
1565 mark_abi_tags (t, true);
1567 tree args = CLASSTYPE_TI_ARGS (t);
1568 struct abi_tag_data data = { t, NULL_TREE, NULL_TREE };
1569 for (int i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
1571 tree level = TMPL_ARGS_LEVEL (args, i+1);
1572 for (int j = 0; j < TREE_VEC_LENGTH (level); ++j)
1574 tree arg = TREE_VEC_ELT (level, j);
1575 data.subob = arg;
1576 cp_walk_tree_without_duplicates (&arg, find_abi_tags_r, &data);
1580 // If we found some tags on our template arguments, add them to our
1581 // abi_tag attribute.
1582 if (data.tags)
1584 tree attr = lookup_attribute ("abi_tag", TYPE_ATTRIBUTES (t));
1585 if (attr)
1586 TREE_VALUE (attr) = chainon (data.tags, TREE_VALUE (attr));
1587 else
1588 TYPE_ATTRIBUTES (t)
1589 = tree_cons (abi_tag_identifier, data.tags, TYPE_ATTRIBUTES (t));
1592 mark_abi_tags (t, false);
1595 /* Return true, iff class T has a non-virtual destructor that is
1596 accessible from outside the class heirarchy (i.e. is public, or
1597 there's a suitable friend. */
1599 static bool
1600 accessible_nvdtor_p (tree t)
1602 tree dtor = CLASSTYPE_DESTRUCTOR (t);
1604 /* An implicitly declared destructor is always public. And,
1605 if it were virtual, we would have created it by now. */
1606 if (!dtor)
1607 return true;
1609 if (DECL_VINDEX (dtor))
1610 return false; /* Virtual */
1612 if (!TREE_PRIVATE (dtor) && !TREE_PROTECTED (dtor))
1613 return true; /* Public */
1615 if (CLASSTYPE_FRIEND_CLASSES (t)
1616 || DECL_FRIENDLIST (TYPE_MAIN_DECL (t)))
1617 return true; /* Has friends */
1619 return false;
1622 /* Run through the base classes of T, updating CANT_HAVE_CONST_CTOR_P,
1623 and NO_CONST_ASN_REF_P. Also set flag bits in T based on
1624 properties of the bases. */
1626 static void
1627 check_bases (tree t,
1628 int* cant_have_const_ctor_p,
1629 int* no_const_asn_ref_p)
1631 int i;
1632 bool seen_non_virtual_nearly_empty_base_p = 0;
1633 int seen_tm_mask = 0;
1634 tree base_binfo;
1635 tree binfo;
1636 tree field = NULL_TREE;
1638 if (!CLASSTYPE_NON_STD_LAYOUT (t))
1639 for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
1640 if (TREE_CODE (field) == FIELD_DECL)
1641 break;
1643 for (binfo = TYPE_BINFO (t), i = 0;
1644 BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
1646 tree basetype = TREE_TYPE (base_binfo);
1648 gcc_assert (COMPLETE_TYPE_P (basetype));
1650 if (CLASSTYPE_FINAL (basetype))
1651 error ("cannot derive from %<final%> base %qT in derived type %qT",
1652 basetype, t);
1654 /* If any base class is non-literal, so is the derived class. */
1655 if (!CLASSTYPE_LITERAL_P (basetype))
1656 CLASSTYPE_LITERAL_P (t) = false;
1658 /* If the base class doesn't have copy constructors or
1659 assignment operators that take const references, then the
1660 derived class cannot have such a member automatically
1661 generated. */
1662 if (TYPE_HAS_COPY_CTOR (basetype)
1663 && ! TYPE_HAS_CONST_COPY_CTOR (basetype))
1664 *cant_have_const_ctor_p = 1;
1665 if (TYPE_HAS_COPY_ASSIGN (basetype)
1666 && !TYPE_HAS_CONST_COPY_ASSIGN (basetype))
1667 *no_const_asn_ref_p = 1;
1669 if (BINFO_VIRTUAL_P (base_binfo))
1670 /* A virtual base does not effect nearly emptiness. */
1672 else if (CLASSTYPE_NEARLY_EMPTY_P (basetype))
1674 if (seen_non_virtual_nearly_empty_base_p)
1675 /* And if there is more than one nearly empty base, then the
1676 derived class is not nearly empty either. */
1677 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
1678 else
1679 /* Remember we've seen one. */
1680 seen_non_virtual_nearly_empty_base_p = 1;
1682 else if (!is_empty_class (basetype))
1683 /* If the base class is not empty or nearly empty, then this
1684 class cannot be nearly empty. */
1685 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
1687 /* A lot of properties from the bases also apply to the derived
1688 class. */
1689 TYPE_NEEDS_CONSTRUCTING (t) |= TYPE_NEEDS_CONSTRUCTING (basetype);
1690 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t)
1691 |= TYPE_HAS_NONTRIVIAL_DESTRUCTOR (basetype);
1692 TYPE_HAS_COMPLEX_COPY_ASSIGN (t)
1693 |= (TYPE_HAS_COMPLEX_COPY_ASSIGN (basetype)
1694 || !TYPE_HAS_COPY_ASSIGN (basetype));
1695 TYPE_HAS_COMPLEX_COPY_CTOR (t) |= (TYPE_HAS_COMPLEX_COPY_CTOR (basetype)
1696 || !TYPE_HAS_COPY_CTOR (basetype));
1697 TYPE_HAS_COMPLEX_MOVE_ASSIGN (t)
1698 |= TYPE_HAS_COMPLEX_MOVE_ASSIGN (basetype);
1699 TYPE_HAS_COMPLEX_MOVE_CTOR (t) |= TYPE_HAS_COMPLEX_MOVE_CTOR (basetype);
1700 TYPE_POLYMORPHIC_P (t) |= TYPE_POLYMORPHIC_P (basetype);
1701 CLASSTYPE_CONTAINS_EMPTY_CLASS_P (t)
1702 |= CLASSTYPE_CONTAINS_EMPTY_CLASS_P (basetype);
1703 TYPE_HAS_COMPLEX_DFLT (t) |= (!TYPE_HAS_DEFAULT_CONSTRUCTOR (basetype)
1704 || TYPE_HAS_COMPLEX_DFLT (basetype));
1705 SET_CLASSTYPE_READONLY_FIELDS_NEED_INIT
1706 (t, CLASSTYPE_READONLY_FIELDS_NEED_INIT (t)
1707 | CLASSTYPE_READONLY_FIELDS_NEED_INIT (basetype));
1708 SET_CLASSTYPE_REF_FIELDS_NEED_INIT
1709 (t, CLASSTYPE_REF_FIELDS_NEED_INIT (t)
1710 | CLASSTYPE_REF_FIELDS_NEED_INIT (basetype));
1711 if (TYPE_HAS_MUTABLE_P (basetype))
1712 CLASSTYPE_HAS_MUTABLE (t) = 1;
1714 /* A standard-layout class is a class that:
1716 * has no non-standard-layout base classes, */
1717 CLASSTYPE_NON_STD_LAYOUT (t) |= CLASSTYPE_NON_STD_LAYOUT (basetype);
1718 if (!CLASSTYPE_NON_STD_LAYOUT (t))
1720 tree basefield;
1721 /* ...has no base classes of the same type as the first non-static
1722 data member... */
1723 if (field && DECL_CONTEXT (field) == t
1724 && (same_type_ignoring_top_level_qualifiers_p
1725 (TREE_TYPE (field), basetype)))
1726 CLASSTYPE_NON_STD_LAYOUT (t) = 1;
1727 else
1728 /* ...either has no non-static data members in the most-derived
1729 class and at most one base class with non-static data
1730 members, or has no base classes with non-static data
1731 members */
1732 for (basefield = TYPE_FIELDS (basetype); basefield;
1733 basefield = DECL_CHAIN (basefield))
1734 if (TREE_CODE (basefield) == FIELD_DECL
1735 && !(DECL_FIELD_IS_BASE (basefield)
1736 && integer_zerop (DECL_SIZE (basefield))))
1738 if (field)
1739 CLASSTYPE_NON_STD_LAYOUT (t) = 1;
1740 else
1741 field = basefield;
1742 break;
1746 /* Don't bother collecting tm attributes if transactional memory
1747 support is not enabled. */
1748 if (flag_tm)
1750 tree tm_attr = find_tm_attribute (TYPE_ATTRIBUTES (basetype));
1751 if (tm_attr)
1752 seen_tm_mask |= tm_attr_to_mask (tm_attr);
1755 check_abi_tags (t, basetype);
1758 /* If one of the base classes had TM attributes, and the current class
1759 doesn't define its own, then the current class inherits one. */
1760 if (seen_tm_mask && !find_tm_attribute (TYPE_ATTRIBUTES (t)))
1762 tree tm_attr = tm_mask_to_attr (least_bit_hwi (seen_tm_mask));
1763 TYPE_ATTRIBUTES (t) = tree_cons (tm_attr, NULL, TYPE_ATTRIBUTES (t));
1767 /* Determine all the primary bases within T. Sets BINFO_PRIMARY_BASE_P for
1768 those that are primaries. Sets BINFO_LOST_PRIMARY_P for those
1769 that have had a nearly-empty virtual primary base stolen by some
1770 other base in the hierarchy. Determines CLASSTYPE_PRIMARY_BASE for
1771 T. */
1773 static void
1774 determine_primary_bases (tree t)
1776 unsigned i;
1777 tree primary = NULL_TREE;
1778 tree type_binfo = TYPE_BINFO (t);
1779 tree base_binfo;
1781 /* Determine the primary bases of our bases. */
1782 for (base_binfo = TREE_CHAIN (type_binfo); base_binfo;
1783 base_binfo = TREE_CHAIN (base_binfo))
1785 tree primary = CLASSTYPE_PRIMARY_BINFO (BINFO_TYPE (base_binfo));
1787 /* See if we're the non-virtual primary of our inheritance
1788 chain. */
1789 if (!BINFO_VIRTUAL_P (base_binfo))
1791 tree parent = BINFO_INHERITANCE_CHAIN (base_binfo);
1792 tree parent_primary = CLASSTYPE_PRIMARY_BINFO (BINFO_TYPE (parent));
1794 if (parent_primary
1795 && SAME_BINFO_TYPE_P (BINFO_TYPE (base_binfo),
1796 BINFO_TYPE (parent_primary)))
1797 /* We are the primary binfo. */
1798 BINFO_PRIMARY_P (base_binfo) = 1;
1800 /* Determine if we have a virtual primary base, and mark it so.
1802 if (primary && BINFO_VIRTUAL_P (primary))
1804 tree this_primary = copied_binfo (primary, base_binfo);
1806 if (BINFO_PRIMARY_P (this_primary))
1807 /* Someone already claimed this base. */
1808 BINFO_LOST_PRIMARY_P (base_binfo) = 1;
1809 else
1811 tree delta;
1813 BINFO_PRIMARY_P (this_primary) = 1;
1814 BINFO_INHERITANCE_CHAIN (this_primary) = base_binfo;
1816 /* A virtual binfo might have been copied from within
1817 another hierarchy. As we're about to use it as a
1818 primary base, make sure the offsets match. */
1819 delta = size_diffop_loc (input_location,
1820 fold_convert (ssizetype,
1821 BINFO_OFFSET (base_binfo)),
1822 fold_convert (ssizetype,
1823 BINFO_OFFSET (this_primary)));
1825 propagate_binfo_offsets (this_primary, delta);
1830 /* First look for a dynamic direct non-virtual base. */
1831 for (i = 0; BINFO_BASE_ITERATE (type_binfo, i, base_binfo); i++)
1833 tree basetype = BINFO_TYPE (base_binfo);
1835 if (TYPE_CONTAINS_VPTR_P (basetype) && !BINFO_VIRTUAL_P (base_binfo))
1837 primary = base_binfo;
1838 goto found;
1842 /* A "nearly-empty" virtual base class can be the primary base
1843 class, if no non-virtual polymorphic base can be found. Look for
1844 a nearly-empty virtual dynamic base that is not already a primary
1845 base of something in the hierarchy. If there is no such base,
1846 just pick the first nearly-empty virtual base. */
1848 for (base_binfo = TREE_CHAIN (type_binfo); base_binfo;
1849 base_binfo = TREE_CHAIN (base_binfo))
1850 if (BINFO_VIRTUAL_P (base_binfo)
1851 && CLASSTYPE_NEARLY_EMPTY_P (BINFO_TYPE (base_binfo)))
1853 if (!BINFO_PRIMARY_P (base_binfo))
1855 /* Found one that is not primary. */
1856 primary = base_binfo;
1857 goto found;
1859 else if (!primary)
1860 /* Remember the first candidate. */
1861 primary = base_binfo;
1864 found:
1865 /* If we've got a primary base, use it. */
1866 if (primary)
1868 tree basetype = BINFO_TYPE (primary);
1870 CLASSTYPE_PRIMARY_BINFO (t) = primary;
1871 if (BINFO_PRIMARY_P (primary))
1872 /* We are stealing a primary base. */
1873 BINFO_LOST_PRIMARY_P (BINFO_INHERITANCE_CHAIN (primary)) = 1;
1874 BINFO_PRIMARY_P (primary) = 1;
1875 if (BINFO_VIRTUAL_P (primary))
1877 tree delta;
1879 BINFO_INHERITANCE_CHAIN (primary) = type_binfo;
1880 /* A virtual binfo might have been copied from within
1881 another hierarchy. As we're about to use it as a primary
1882 base, make sure the offsets match. */
1883 delta = size_diffop_loc (input_location, ssize_int (0),
1884 fold_convert (ssizetype, BINFO_OFFSET (primary)));
1886 propagate_binfo_offsets (primary, delta);
1889 primary = TYPE_BINFO (basetype);
1891 TYPE_VFIELD (t) = TYPE_VFIELD (basetype);
1892 BINFO_VTABLE (type_binfo) = BINFO_VTABLE (primary);
1893 BINFO_VIRTUALS (type_binfo) = BINFO_VIRTUALS (primary);
1897 /* Update the variant types of T. */
1899 void
1900 fixup_type_variants (tree t)
1902 tree variants;
1904 if (!t)
1905 return;
1907 for (variants = TYPE_NEXT_VARIANT (t);
1908 variants;
1909 variants = TYPE_NEXT_VARIANT (variants))
1911 /* These fields are in the _TYPE part of the node, not in
1912 the TYPE_LANG_SPECIFIC component, so they are not shared. */
1913 TYPE_HAS_USER_CONSTRUCTOR (variants) = TYPE_HAS_USER_CONSTRUCTOR (t);
1914 TYPE_NEEDS_CONSTRUCTING (variants) = TYPE_NEEDS_CONSTRUCTING (t);
1915 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (variants)
1916 = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t);
1918 TYPE_POLYMORPHIC_P (variants) = TYPE_POLYMORPHIC_P (t);
1920 TYPE_BINFO (variants) = TYPE_BINFO (t);
1922 /* Copy whatever these are holding today. */
1923 TYPE_VFIELD (variants) = TYPE_VFIELD (t);
1924 TYPE_FIELDS (variants) = TYPE_FIELDS (t);
1928 /* KLASS is a class that we're applying may_alias to after the body is
1929 parsed. Fixup any POINTER_TO and REFERENCE_TO types. The
1930 canonical type(s) will be implicitly updated. */
1932 static void
1933 fixup_may_alias (tree klass)
1935 tree t, v;
1937 for (t = TYPE_POINTER_TO (klass); t; t = TYPE_NEXT_PTR_TO (t))
1938 for (v = TYPE_MAIN_VARIANT (t); v; v = TYPE_NEXT_VARIANT (v))
1939 TYPE_REF_CAN_ALIAS_ALL (v) = true;
1940 for (t = TYPE_REFERENCE_TO (klass); t; t = TYPE_NEXT_REF_TO (t))
1941 for (v = TYPE_MAIN_VARIANT (t); v; v = TYPE_NEXT_VARIANT (v))
1942 TYPE_REF_CAN_ALIAS_ALL (v) = true;
1945 /* Early variant fixups: we apply attributes at the beginning of the class
1946 definition, and we need to fix up any variants that have already been
1947 made via elaborated-type-specifier so that check_qualified_type works. */
1949 void
1950 fixup_attribute_variants (tree t)
1952 tree variants;
1954 if (!t)
1955 return;
1957 tree attrs = TYPE_ATTRIBUTES (t);
1958 unsigned align = TYPE_ALIGN (t);
1959 bool user_align = TYPE_USER_ALIGN (t);
1960 bool may_alias = lookup_attribute ("may_alias", attrs);
1962 if (may_alias)
1963 fixup_may_alias (t);
1965 for (variants = TYPE_NEXT_VARIANT (t);
1966 variants;
1967 variants = TYPE_NEXT_VARIANT (variants))
1969 /* These are the two fields that check_qualified_type looks at and
1970 are affected by attributes. */
1971 TYPE_ATTRIBUTES (variants) = attrs;
1972 unsigned valign = align;
1973 if (TYPE_USER_ALIGN (variants))
1974 valign = MAX (valign, TYPE_ALIGN (variants));
1975 else
1976 TYPE_USER_ALIGN (variants) = user_align;
1977 SET_TYPE_ALIGN (variants, valign);
1978 if (may_alias)
1979 fixup_may_alias (variants);
1983 /* Set memoizing fields and bits of T (and its variants) for later
1984 use. */
1986 static void
1987 finish_struct_bits (tree t)
1989 /* Fix up variants (if any). */
1990 fixup_type_variants (t);
1992 if (BINFO_N_BASE_BINFOS (TYPE_BINFO (t)) && TYPE_POLYMORPHIC_P (t))
1993 /* For a class w/o baseclasses, 'finish_struct' has set
1994 CLASSTYPE_PURE_VIRTUALS correctly (by definition).
1995 Similarly for a class whose base classes do not have vtables.
1996 When neither of these is true, we might have removed abstract
1997 virtuals (by providing a definition), added some (by declaring
1998 new ones), or redeclared ones from a base class. We need to
1999 recalculate what's really an abstract virtual at this point (by
2000 looking in the vtables). */
2001 get_pure_virtuals (t);
2003 /* If this type has a copy constructor or a destructor, force its
2004 mode to be BLKmode, and force its TREE_ADDRESSABLE bit to be
2005 nonzero. This will cause it to be passed by invisible reference
2006 and prevent it from being returned in a register. */
2007 if (type_has_nontrivial_copy_init (t)
2008 || TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t))
2010 tree variants;
2011 SET_DECL_MODE (TYPE_MAIN_DECL (t), BLKmode);
2012 for (variants = t; variants; variants = TYPE_NEXT_VARIANT (variants))
2014 SET_TYPE_MODE (variants, BLKmode);
2015 TREE_ADDRESSABLE (variants) = 1;
2020 /* Issue warnings about T having private constructors, but no friends,
2021 and so forth.
2023 HAS_NONPRIVATE_METHOD is nonzero if T has any non-private methods or
2024 static members. HAS_NONPRIVATE_STATIC_FN is nonzero if T has any
2025 non-private static member functions. */
2027 static void
2028 maybe_warn_about_overly_private_class (tree t)
2030 int has_member_fn = 0;
2031 int has_nonprivate_method = 0;
2032 bool nonprivate_ctor = false;
2034 if (!warn_ctor_dtor_privacy
2035 /* If the class has friends, those entities might create and
2036 access instances, so we should not warn. */
2037 || (CLASSTYPE_FRIEND_CLASSES (t)
2038 || DECL_FRIENDLIST (TYPE_MAIN_DECL (t)))
2039 /* We will have warned when the template was declared; there's
2040 no need to warn on every instantiation. */
2041 || CLASSTYPE_TEMPLATE_INSTANTIATION (t))
2042 /* There's no reason to even consider warning about this
2043 class. */
2044 return;
2046 /* We only issue one warning, if more than one applies, because
2047 otherwise, on code like:
2049 class A {
2050 // Oops - forgot `public:'
2051 A();
2052 A(const A&);
2053 ~A();
2056 we warn several times about essentially the same problem. */
2058 /* Check to see if all (non-constructor, non-destructor) member
2059 functions are private. (Since there are no friends or
2060 non-private statics, we can't ever call any of the private member
2061 functions.) */
2062 for (tree fn = TYPE_FIELDS (t); fn; fn = DECL_CHAIN (fn))
2063 if (TREE_CODE (fn) == USING_DECL
2064 && DECL_NAME (fn) == ctor_identifier
2065 && !TREE_PRIVATE (fn))
2066 nonprivate_ctor = true;
2067 else if (!DECL_DECLARES_FUNCTION_P (fn))
2068 /* Not a function. */;
2069 else if (DECL_ARTIFICIAL (fn))
2070 /* We're not interested in compiler-generated methods; they don't
2071 provide any way to call private members. */;
2072 else if (!TREE_PRIVATE (fn))
2074 if (DECL_STATIC_FUNCTION_P (fn))
2075 /* A non-private static member function is just like a
2076 friend; it can create and invoke private member
2077 functions, and be accessed without a class
2078 instance. */
2079 return;
2081 has_nonprivate_method = 1;
2082 /* Keep searching for a static member function. */
2084 else if (!DECL_CONSTRUCTOR_P (fn) && !DECL_DESTRUCTOR_P (fn))
2085 has_member_fn = 1;
2087 if (!has_nonprivate_method && has_member_fn)
2089 /* There are no non-private methods, and there's at least one
2090 private member function that isn't a constructor or
2091 destructor. (If all the private members are
2092 constructors/destructors we want to use the code below that
2093 issues error messages specifically referring to
2094 constructors/destructors.) */
2095 unsigned i;
2096 tree binfo = TYPE_BINFO (t);
2098 for (i = 0; i != BINFO_N_BASE_BINFOS (binfo); i++)
2099 if (BINFO_BASE_ACCESS (binfo, i) != access_private_node)
2101 has_nonprivate_method = 1;
2102 break;
2104 if (!has_nonprivate_method)
2106 warning (OPT_Wctor_dtor_privacy,
2107 "all member functions in class %qT are private", t);
2108 return;
2112 /* Even if some of the member functions are non-private, the class
2113 won't be useful for much if all the constructors or destructors
2114 are private: such an object can never be created or destroyed. */
2115 if (tree dtor = CLASSTYPE_DESTRUCTOR (t))
2116 if (TREE_PRIVATE (dtor))
2118 warning (OPT_Wctor_dtor_privacy,
2119 "%q#T only defines a private destructor and has no friends",
2121 return;
2124 /* Warn about classes that have private constructors and no friends. */
2125 if (TYPE_HAS_USER_CONSTRUCTOR (t)
2126 /* Implicitly generated constructors are always public. */
2127 && !CLASSTYPE_LAZY_DEFAULT_CTOR (t))
2129 tree copy_or_move = NULL_TREE;
2131 /* If a non-template class does not define a copy
2132 constructor, one is defined for it, enabling it to avoid
2133 this warning. For a template class, this does not
2134 happen, and so we would normally get a warning on:
2136 template <class T> class C { private: C(); };
2138 To avoid this asymmetry, we check TYPE_HAS_COPY_CTOR. All
2139 complete non-template or fully instantiated classes have this
2140 flag set. */
2141 if (!TYPE_HAS_COPY_CTOR (t))
2142 nonprivate_ctor = true;
2143 else
2144 for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (t));
2145 !nonprivate_ctor && iter; ++iter)
2146 if (TREE_PRIVATE (*iter))
2147 continue;
2148 else if (copy_fn_p (*iter) || move_fn_p (*iter))
2149 /* Ideally, we wouldn't count any constructor that takes
2150 an argument of the class type as a parameter, because
2151 such things cannot be used to construct an instance of
2152 the class unless you already have one. */
2153 copy_or_move = *iter;
2154 else
2155 nonprivate_ctor = true;
2157 if (!nonprivate_ctor)
2159 warning (OPT_Wctor_dtor_privacy,
2160 "%q#T only defines private constructors and has no friends",
2162 if (copy_or_move)
2163 inform (DECL_SOURCE_LOCATION (copy_or_move),
2164 "%q#D is public, but requires an existing %q#T object",
2165 copy_or_move, t);
2166 return;
2171 /* Make BINFO's vtable have N entries, including RTTI entries,
2172 vbase and vcall offsets, etc. Set its type and call the back end
2173 to lay it out. */
2175 static void
2176 layout_vtable_decl (tree binfo, int n)
2178 tree atype;
2179 tree vtable;
2181 atype = build_array_of_n_type (vtable_entry_type, n);
2182 layout_type (atype);
2184 /* We may have to grow the vtable. */
2185 vtable = get_vtbl_decl_for_binfo (binfo);
2186 if (!same_type_p (TREE_TYPE (vtable), atype))
2188 TREE_TYPE (vtable) = atype;
2189 DECL_SIZE (vtable) = DECL_SIZE_UNIT (vtable) = NULL_TREE;
2190 layout_decl (vtable, 0);
2194 /* True iff FNDECL and BASE_FNDECL (both non-static member functions)
2195 have the same signature. */
2198 same_signature_p (const_tree fndecl, const_tree base_fndecl)
2200 /* One destructor overrides another if they are the same kind of
2201 destructor. */
2202 if (DECL_DESTRUCTOR_P (base_fndecl) && DECL_DESTRUCTOR_P (fndecl)
2203 && special_function_p (base_fndecl) == special_function_p (fndecl))
2204 return 1;
2205 /* But a non-destructor never overrides a destructor, nor vice
2206 versa, nor do different kinds of destructors override
2207 one-another. For example, a complete object destructor does not
2208 override a deleting destructor. */
2209 if (DECL_DESTRUCTOR_P (base_fndecl) || DECL_DESTRUCTOR_P (fndecl))
2210 return 0;
2212 if (DECL_NAME (fndecl) == DECL_NAME (base_fndecl)
2213 || (DECL_CONV_FN_P (fndecl)
2214 && DECL_CONV_FN_P (base_fndecl)
2215 && same_type_p (DECL_CONV_FN_TYPE (fndecl),
2216 DECL_CONV_FN_TYPE (base_fndecl))))
2218 tree fntype = TREE_TYPE (fndecl);
2219 tree base_fntype = TREE_TYPE (base_fndecl);
2220 if (type_memfn_quals (fntype) == type_memfn_quals (base_fntype)
2221 && type_memfn_rqual (fntype) == type_memfn_rqual (base_fntype)
2222 && compparms (FUNCTION_FIRST_USER_PARMTYPE (fndecl),
2223 FUNCTION_FIRST_USER_PARMTYPE (base_fndecl)))
2224 return 1;
2226 return 0;
2229 /* Returns TRUE if DERIVED is a binfo containing the binfo BASE as a
2230 subobject. */
2232 static bool
2233 base_derived_from (tree derived, tree base)
2235 tree probe;
2237 for (probe = base; probe; probe = BINFO_INHERITANCE_CHAIN (probe))
2239 if (probe == derived)
2240 return true;
2241 else if (BINFO_VIRTUAL_P (probe))
2242 /* If we meet a virtual base, we can't follow the inheritance
2243 any more. See if the complete type of DERIVED contains
2244 such a virtual base. */
2245 return (binfo_for_vbase (BINFO_TYPE (probe), BINFO_TYPE (derived))
2246 != NULL_TREE);
2248 return false;
2251 struct find_final_overrider_data {
2252 /* The function for which we are trying to find a final overrider. */
2253 tree fn;
2254 /* The base class in which the function was declared. */
2255 tree declaring_base;
2256 /* The candidate overriders. */
2257 tree candidates;
2258 /* Path to most derived. */
2259 vec<tree> path;
2262 /* Add the overrider along the current path to FFOD->CANDIDATES.
2263 Returns true if an overrider was found; false otherwise. */
2265 static bool
2266 dfs_find_final_overrider_1 (tree binfo,
2267 find_final_overrider_data *ffod,
2268 unsigned depth)
2270 tree method;
2272 /* If BINFO is not the most derived type, try a more derived class.
2273 A definition there will overrider a definition here. */
2274 if (depth)
2276 depth--;
2277 if (dfs_find_final_overrider_1
2278 (ffod->path[depth], ffod, depth))
2279 return true;
2282 method = look_for_overrides_here (BINFO_TYPE (binfo), ffod->fn);
2283 if (method)
2285 tree *candidate = &ffod->candidates;
2287 /* Remove any candidates overridden by this new function. */
2288 while (*candidate)
2290 /* If *CANDIDATE overrides METHOD, then METHOD
2291 cannot override anything else on the list. */
2292 if (base_derived_from (TREE_VALUE (*candidate), binfo))
2293 return true;
2294 /* If METHOD overrides *CANDIDATE, remove *CANDIDATE. */
2295 if (base_derived_from (binfo, TREE_VALUE (*candidate)))
2296 *candidate = TREE_CHAIN (*candidate);
2297 else
2298 candidate = &TREE_CHAIN (*candidate);
2301 /* Add the new function. */
2302 ffod->candidates = tree_cons (method, binfo, ffod->candidates);
2303 return true;
2306 return false;
2309 /* Called from find_final_overrider via dfs_walk. */
2311 static tree
2312 dfs_find_final_overrider_pre (tree binfo, void *data)
2314 find_final_overrider_data *ffod = (find_final_overrider_data *) data;
2316 if (binfo == ffod->declaring_base)
2317 dfs_find_final_overrider_1 (binfo, ffod, ffod->path.length ());
2318 ffod->path.safe_push (binfo);
2320 return NULL_TREE;
2323 static tree
2324 dfs_find_final_overrider_post (tree /*binfo*/, void *data)
2326 find_final_overrider_data *ffod = (find_final_overrider_data *) data;
2327 ffod->path.pop ();
2329 return NULL_TREE;
2332 /* Returns a TREE_LIST whose TREE_PURPOSE is the final overrider for
2333 FN and whose TREE_VALUE is the binfo for the base where the
2334 overriding occurs. BINFO (in the hierarchy dominated by the binfo
2335 DERIVED) is the base object in which FN is declared. */
2337 static tree
2338 find_final_overrider (tree derived, tree binfo, tree fn)
2340 find_final_overrider_data ffod;
2342 /* Getting this right is a little tricky. This is valid:
2344 struct S { virtual void f (); };
2345 struct T { virtual void f (); };
2346 struct U : public S, public T { };
2348 even though calling `f' in `U' is ambiguous. But,
2350 struct R { virtual void f(); };
2351 struct S : virtual public R { virtual void f (); };
2352 struct T : virtual public R { virtual void f (); };
2353 struct U : public S, public T { };
2355 is not -- there's no way to decide whether to put `S::f' or
2356 `T::f' in the vtable for `R'.
2358 The solution is to look at all paths to BINFO. If we find
2359 different overriders along any two, then there is a problem. */
2360 if (DECL_THUNK_P (fn))
2361 fn = THUNK_TARGET (fn);
2363 /* Determine the depth of the hierarchy. */
2364 ffod.fn = fn;
2365 ffod.declaring_base = binfo;
2366 ffod.candidates = NULL_TREE;
2367 ffod.path.create (30);
2369 dfs_walk_all (derived, dfs_find_final_overrider_pre,
2370 dfs_find_final_overrider_post, &ffod);
2372 ffod.path.release ();
2374 /* If there was no winner, issue an error message. */
2375 if (!ffod.candidates || TREE_CHAIN (ffod.candidates))
2376 return error_mark_node;
2378 return ffod.candidates;
2381 /* Return the index of the vcall offset for FN when TYPE is used as a
2382 virtual base. */
2384 static tree
2385 get_vcall_index (tree fn, tree type)
2387 vec<tree_pair_s, va_gc> *indices = CLASSTYPE_VCALL_INDICES (type);
2388 tree_pair_p p;
2389 unsigned ix;
2391 FOR_EACH_VEC_SAFE_ELT (indices, ix, p)
2392 if ((DECL_DESTRUCTOR_P (fn) && DECL_DESTRUCTOR_P (p->purpose))
2393 || same_signature_p (fn, p->purpose))
2394 return p->value;
2396 /* There should always be an appropriate index. */
2397 gcc_unreachable ();
2400 /* Update an entry in the vtable for BINFO, which is in the hierarchy
2401 dominated by T. FN is the old function; VIRTUALS points to the
2402 corresponding position in the new BINFO_VIRTUALS list. IX is the index
2403 of that entry in the list. */
2405 static void
2406 update_vtable_entry_for_fn (tree t, tree binfo, tree fn, tree* virtuals,
2407 unsigned ix)
2409 tree b;
2410 tree overrider;
2411 tree delta;
2412 tree virtual_base;
2413 tree first_defn;
2414 tree overrider_fn, overrider_target;
2415 tree target_fn = DECL_THUNK_P (fn) ? THUNK_TARGET (fn) : fn;
2416 tree over_return, base_return;
2417 bool lost = false;
2419 /* Find the nearest primary base (possibly binfo itself) which defines
2420 this function; this is the class the caller will convert to when
2421 calling FN through BINFO. */
2422 for (b = binfo; ; b = get_primary_binfo (b))
2424 gcc_assert (b);
2425 if (look_for_overrides_here (BINFO_TYPE (b), target_fn))
2426 break;
2428 /* The nearest definition is from a lost primary. */
2429 if (BINFO_LOST_PRIMARY_P (b))
2430 lost = true;
2432 first_defn = b;
2434 /* Find the final overrider. */
2435 overrider = find_final_overrider (TYPE_BINFO (t), b, target_fn);
2436 if (overrider == error_mark_node)
2438 error ("no unique final overrider for %qD in %qT", target_fn, t);
2439 return;
2441 overrider_target = overrider_fn = TREE_PURPOSE (overrider);
2443 /* Check for adjusting covariant return types. */
2444 over_return = TREE_TYPE (TREE_TYPE (overrider_target));
2445 base_return = TREE_TYPE (TREE_TYPE (target_fn));
2447 if (INDIRECT_TYPE_P (over_return)
2448 && TREE_CODE (over_return) == TREE_CODE (base_return)
2449 && CLASS_TYPE_P (TREE_TYPE (over_return))
2450 && CLASS_TYPE_P (TREE_TYPE (base_return))
2451 /* If the overrider is invalid, don't even try. */
2452 && !DECL_INVALID_OVERRIDER_P (overrider_target))
2454 /* If FN is a covariant thunk, we must figure out the adjustment
2455 to the final base FN was converting to. As OVERRIDER_TARGET might
2456 also be converting to the return type of FN, we have to
2457 combine the two conversions here. */
2458 tree fixed_offset, virtual_offset;
2460 over_return = TREE_TYPE (over_return);
2461 base_return = TREE_TYPE (base_return);
2463 if (DECL_THUNK_P (fn))
2465 gcc_assert (DECL_RESULT_THUNK_P (fn));
2466 fixed_offset = ssize_int (THUNK_FIXED_OFFSET (fn));
2467 virtual_offset = THUNK_VIRTUAL_OFFSET (fn);
2469 else
2470 fixed_offset = virtual_offset = NULL_TREE;
2472 if (virtual_offset)
2473 /* Find the equivalent binfo within the return type of the
2474 overriding function. We will want the vbase offset from
2475 there. */
2476 virtual_offset = binfo_for_vbase (BINFO_TYPE (virtual_offset),
2477 over_return);
2478 else if (!same_type_ignoring_top_level_qualifiers_p
2479 (over_return, base_return))
2481 /* There was no existing virtual thunk (which takes
2482 precedence). So find the binfo of the base function's
2483 return type within the overriding function's return type.
2484 Fortunately we know the covariancy is valid (it
2485 has already been checked), so we can just iterate along
2486 the binfos, which have been chained in inheritance graph
2487 order. Of course it is lame that we have to repeat the
2488 search here anyway -- we should really be caching pieces
2489 of the vtable and avoiding this repeated work. */
2490 tree thunk_binfo = NULL_TREE;
2491 tree base_binfo = TYPE_BINFO (base_return);
2493 /* Find the base binfo within the overriding function's
2494 return type. We will always find a thunk_binfo, except
2495 when the covariancy is invalid (which we will have
2496 already diagnosed). */
2497 if (base_binfo)
2498 for (thunk_binfo = TYPE_BINFO (over_return); thunk_binfo;
2499 thunk_binfo = TREE_CHAIN (thunk_binfo))
2500 if (SAME_BINFO_TYPE_P (BINFO_TYPE (thunk_binfo),
2501 BINFO_TYPE (base_binfo)))
2502 break;
2503 gcc_assert (thunk_binfo || errorcount);
2505 /* See if virtual inheritance is involved. */
2506 for (virtual_offset = thunk_binfo;
2507 virtual_offset;
2508 virtual_offset = BINFO_INHERITANCE_CHAIN (virtual_offset))
2509 if (BINFO_VIRTUAL_P (virtual_offset))
2510 break;
2512 if (virtual_offset
2513 || (thunk_binfo && !BINFO_OFFSET_ZEROP (thunk_binfo)))
2515 tree offset = fold_convert (ssizetype, BINFO_OFFSET (thunk_binfo));
2517 if (virtual_offset)
2519 /* We convert via virtual base. Adjust the fixed
2520 offset to be from there. */
2521 offset =
2522 size_diffop (offset,
2523 fold_convert (ssizetype,
2524 BINFO_OFFSET (virtual_offset)));
2526 if (fixed_offset)
2527 /* There was an existing fixed offset, this must be
2528 from the base just converted to, and the base the
2529 FN was thunking to. */
2530 fixed_offset = size_binop (PLUS_EXPR, fixed_offset, offset);
2531 else
2532 fixed_offset = offset;
2536 if (fixed_offset || virtual_offset)
2537 /* Replace the overriding function with a covariant thunk. We
2538 will emit the overriding function in its own slot as
2539 well. */
2540 overrider_fn = make_thunk (overrider_target, /*this_adjusting=*/0,
2541 fixed_offset, virtual_offset);
2543 else
2544 gcc_assert (DECL_INVALID_OVERRIDER_P (overrider_target) ||
2545 !DECL_THUNK_P (fn));
2547 /* If we need a covariant thunk, then we may need to adjust first_defn.
2548 The ABI specifies that the thunks emitted with a function are
2549 determined by which bases the function overrides, so we need to be
2550 sure that we're using a thunk for some overridden base; even if we
2551 know that the necessary this adjustment is zero, there may not be an
2552 appropriate zero-this-adjustment thunk for us to use since thunks for
2553 overriding virtual bases always use the vcall offset.
2555 Furthermore, just choosing any base that overrides this function isn't
2556 quite right, as this slot won't be used for calls through a type that
2557 puts a covariant thunk here. Calling the function through such a type
2558 will use a different slot, and that slot is the one that determines
2559 the thunk emitted for that base.
2561 So, keep looking until we find the base that we're really overriding
2562 in this slot: the nearest primary base that doesn't use a covariant
2563 thunk in this slot. */
2564 if (overrider_target != overrider_fn)
2566 if (BINFO_TYPE (b) == DECL_CONTEXT (overrider_target))
2567 /* We already know that the overrider needs a covariant thunk. */
2568 b = get_primary_binfo (b);
2569 for (; ; b = get_primary_binfo (b))
2571 tree main_binfo = TYPE_BINFO (BINFO_TYPE (b));
2572 tree bv = chain_index (ix, BINFO_VIRTUALS (main_binfo));
2573 if (!DECL_THUNK_P (TREE_VALUE (bv)))
2574 break;
2575 if (BINFO_LOST_PRIMARY_P (b))
2576 lost = true;
2578 first_defn = b;
2581 /* Assume that we will produce a thunk that convert all the way to
2582 the final overrider, and not to an intermediate virtual base. */
2583 virtual_base = NULL_TREE;
2585 /* See if we can convert to an intermediate virtual base first, and then
2586 use the vcall offset located there to finish the conversion. */
2587 for (; b; b = BINFO_INHERITANCE_CHAIN (b))
2589 /* If we find the final overrider, then we can stop
2590 walking. */
2591 if (SAME_BINFO_TYPE_P (BINFO_TYPE (b),
2592 BINFO_TYPE (TREE_VALUE (overrider))))
2593 break;
2595 /* If we find a virtual base, and we haven't yet found the
2596 overrider, then there is a virtual base between the
2597 declaring base (first_defn) and the final overrider. */
2598 if (BINFO_VIRTUAL_P (b))
2600 virtual_base = b;
2601 break;
2605 /* Compute the constant adjustment to the `this' pointer. The
2606 `this' pointer, when this function is called, will point at BINFO
2607 (or one of its primary bases, which are at the same offset). */
2608 if (virtual_base)
2609 /* The `this' pointer needs to be adjusted from the declaration to
2610 the nearest virtual base. */
2611 delta = size_diffop_loc (input_location,
2612 fold_convert (ssizetype, BINFO_OFFSET (virtual_base)),
2613 fold_convert (ssizetype, BINFO_OFFSET (first_defn)));
2614 else if (lost)
2615 /* If the nearest definition is in a lost primary, we don't need an
2616 entry in our vtable. Except possibly in a constructor vtable,
2617 if we happen to get our primary back. In that case, the offset
2618 will be zero, as it will be a primary base. */
2619 delta = size_zero_node;
2620 else
2621 /* The `this' pointer needs to be adjusted from pointing to
2622 BINFO to pointing at the base where the final overrider
2623 appears. */
2624 delta = size_diffop_loc (input_location,
2625 fold_convert (ssizetype,
2626 BINFO_OFFSET (TREE_VALUE (overrider))),
2627 fold_convert (ssizetype, BINFO_OFFSET (binfo)));
2629 modify_vtable_entry (t, binfo, overrider_fn, delta, virtuals);
2631 if (virtual_base)
2632 BV_VCALL_INDEX (*virtuals)
2633 = get_vcall_index (overrider_target, BINFO_TYPE (virtual_base));
2634 else
2635 BV_VCALL_INDEX (*virtuals) = NULL_TREE;
2637 BV_LOST_PRIMARY (*virtuals) = lost;
2640 /* Called from modify_all_vtables via dfs_walk. */
2642 static tree
2643 dfs_modify_vtables (tree binfo, void* data)
2645 tree t = (tree) data;
2646 tree virtuals;
2647 tree old_virtuals;
2648 unsigned ix;
2650 if (!TYPE_CONTAINS_VPTR_P (BINFO_TYPE (binfo)))
2651 /* A base without a vtable needs no modification, and its bases
2652 are uninteresting. */
2653 return dfs_skip_bases;
2655 if (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), t)
2656 && !CLASSTYPE_HAS_PRIMARY_BASE_P (t))
2657 /* Don't do the primary vtable, if it's new. */
2658 return NULL_TREE;
2660 if (BINFO_PRIMARY_P (binfo) && !BINFO_VIRTUAL_P (binfo))
2661 /* There's no need to modify the vtable for a non-virtual primary
2662 base; we're not going to use that vtable anyhow. We do still
2663 need to do this for virtual primary bases, as they could become
2664 non-primary in a construction vtable. */
2665 return NULL_TREE;
2667 make_new_vtable (t, binfo);
2669 /* Now, go through each of the virtual functions in the virtual
2670 function table for BINFO. Find the final overrider, and update
2671 the BINFO_VIRTUALS list appropriately. */
2672 for (ix = 0, virtuals = BINFO_VIRTUALS (binfo),
2673 old_virtuals = BINFO_VIRTUALS (TYPE_BINFO (BINFO_TYPE (binfo)));
2674 virtuals;
2675 ix++, virtuals = TREE_CHAIN (virtuals),
2676 old_virtuals = TREE_CHAIN (old_virtuals))
2677 update_vtable_entry_for_fn (t,
2678 binfo,
2679 BV_FN (old_virtuals),
2680 &virtuals, ix);
2682 return NULL_TREE;
2685 /* Update all of the primary and secondary vtables for T. Create new
2686 vtables as required, and initialize their RTTI information. Each
2687 of the functions in VIRTUALS is declared in T and may override a
2688 virtual function from a base class; find and modify the appropriate
2689 entries to point to the overriding functions. Returns a list, in
2690 declaration order, of the virtual functions that are declared in T,
2691 but do not appear in the primary base class vtable, and which
2692 should therefore be appended to the end of the vtable for T. */
2694 static tree
2695 modify_all_vtables (tree t, tree virtuals)
2697 tree binfo = TYPE_BINFO (t);
2698 tree *fnsp;
2700 /* Mangle the vtable name before entering dfs_walk (c++/51884). */
2701 if (TYPE_CONTAINS_VPTR_P (t))
2702 get_vtable_decl (t, false);
2704 /* Update all of the vtables. */
2705 dfs_walk_once (binfo, dfs_modify_vtables, NULL, t);
2707 /* Add virtual functions not already in our primary vtable. These
2708 will be both those introduced by this class, and those overridden
2709 from secondary bases. It does not include virtuals merely
2710 inherited from secondary bases. */
2711 for (fnsp = &virtuals; *fnsp; )
2713 tree fn = TREE_VALUE (*fnsp);
2715 if (!value_member (fn, BINFO_VIRTUALS (binfo))
2716 || DECL_VINDEX (fn) == error_mark_node)
2718 /* We don't need to adjust the `this' pointer when
2719 calling this function. */
2720 BV_DELTA (*fnsp) = integer_zero_node;
2721 BV_VCALL_INDEX (*fnsp) = NULL_TREE;
2723 /* This is a function not already in our vtable. Keep it. */
2724 fnsp = &TREE_CHAIN (*fnsp);
2726 else
2727 /* We've already got an entry for this function. Skip it. */
2728 *fnsp = TREE_CHAIN (*fnsp);
2731 return virtuals;
2734 /* Get the base virtual function declarations in T that have the
2735 indicated NAME. */
2737 static void
2738 get_basefndecls (tree name, tree t, vec<tree> *base_fndecls)
2740 bool found_decls = false;
2742 /* Find virtual functions in T with the indicated NAME. */
2743 for (ovl_iterator iter (get_class_binding (t, name)); iter; ++iter)
2745 tree method = *iter;
2747 if (TREE_CODE (method) == FUNCTION_DECL && DECL_VINDEX (method))
2749 base_fndecls->safe_push (method);
2750 found_decls = true;
2754 if (found_decls)
2755 return;
2757 int n_baseclasses = BINFO_N_BASE_BINFOS (TYPE_BINFO (t));
2758 for (int i = 0; i < n_baseclasses; i++)
2760 tree basetype = BINFO_TYPE (BINFO_BASE_BINFO (TYPE_BINFO (t), i));
2761 get_basefndecls (name, basetype, base_fndecls);
2765 /* If this declaration supersedes the declaration of
2766 a method declared virtual in the base class, then
2767 mark this field as being virtual as well. */
2769 void
2770 check_for_override (tree decl, tree ctype)
2772 bool overrides_found = false;
2773 if (TREE_CODE (decl) == TEMPLATE_DECL)
2774 /* In [temp.mem] we have:
2776 A specialization of a member function template does not
2777 override a virtual function from a base class. */
2778 return;
2779 if ((DECL_DESTRUCTOR_P (decl)
2780 || IDENTIFIER_VIRTUAL_P (DECL_NAME (decl))
2781 || DECL_CONV_FN_P (decl))
2782 && look_for_overrides (ctype, decl)
2783 && !DECL_STATIC_FUNCTION_P (decl))
2784 /* Set DECL_VINDEX to a value that is neither an INTEGER_CST nor
2785 the error_mark_node so that we know it is an overriding
2786 function. */
2788 DECL_VINDEX (decl) = decl;
2789 overrides_found = true;
2790 if (warn_override && !DECL_OVERRIDE_P (decl)
2791 && !DECL_DESTRUCTOR_P (decl))
2792 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wsuggest_override,
2793 "%qD can be marked override", decl);
2796 if (DECL_VIRTUAL_P (decl))
2798 if (!DECL_VINDEX (decl))
2799 DECL_VINDEX (decl) = error_mark_node;
2800 IDENTIFIER_VIRTUAL_P (DECL_NAME (decl)) = 1;
2801 if (DECL_DESTRUCTOR_P (decl))
2802 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (ctype) = true;
2804 else if (DECL_FINAL_P (decl))
2805 error ("%q+#D marked %<final%>, but is not virtual", decl);
2806 if (DECL_OVERRIDE_P (decl) && !overrides_found)
2807 error ("%q+#D marked %<override%>, but does not override", decl);
2810 /* Warn about hidden virtual functions that are not overridden in t.
2811 We know that constructors and destructors don't apply. */
2813 static void
2814 warn_hidden (tree t)
2816 if (vec<tree, va_gc> *member_vec = CLASSTYPE_MEMBER_VEC (t))
2817 for (unsigned ix = member_vec->length (); ix--;)
2819 tree fns = (*member_vec)[ix];
2821 if (!OVL_P (fns))
2822 continue;
2824 tree name = OVL_NAME (fns);
2825 auto_vec<tree, 20> base_fndecls;
2826 tree base_binfo;
2827 tree binfo;
2828 unsigned j;
2830 /* Iterate through all of the base classes looking for possibly
2831 hidden functions. */
2832 for (binfo = TYPE_BINFO (t), j = 0;
2833 BINFO_BASE_ITERATE (binfo, j, base_binfo); j++)
2835 tree basetype = BINFO_TYPE (base_binfo);
2836 get_basefndecls (name, basetype, &base_fndecls);
2839 /* If there are no functions to hide, continue. */
2840 if (base_fndecls.is_empty ())
2841 continue;
2843 /* Remove any overridden functions. */
2844 for (ovl_iterator iter (fns); iter; ++iter)
2846 tree fndecl = *iter;
2847 if (TREE_CODE (fndecl) == FUNCTION_DECL
2848 && DECL_VINDEX (fndecl))
2850 /* If the method from the base class has the same
2851 signature as the method from the derived class, it
2852 has been overridden. */
2853 for (size_t k = 0; k < base_fndecls.length (); k++)
2854 if (base_fndecls[k]
2855 && same_signature_p (fndecl, base_fndecls[k]))
2856 base_fndecls[k] = NULL_TREE;
2860 /* Now give a warning for all base functions without overriders,
2861 as they are hidden. */
2862 tree base_fndecl;
2863 FOR_EACH_VEC_ELT (base_fndecls, j, base_fndecl)
2864 if (base_fndecl)
2866 /* Here we know it is a hider, and no overrider exists. */
2867 warning_at (location_of (base_fndecl),
2868 OPT_Woverloaded_virtual,
2869 "%qD was hidden", base_fndecl);
2870 warning_at (location_of (fns),
2871 OPT_Woverloaded_virtual, " by %qD", fns);
2876 /* Recursive helper for finish_struct_anon. */
2878 static void
2879 finish_struct_anon_r (tree field, bool complain)
2881 for (tree elt = TYPE_FIELDS (TREE_TYPE (field)); elt; elt = DECL_CHAIN (elt))
2883 /* We're generally only interested in entities the user
2884 declared, but we also find nested classes by noticing
2885 the TYPE_DECL that we create implicitly. You're
2886 allowed to put one anonymous union inside another,
2887 though, so we explicitly tolerate that. We use
2888 TYPE_UNNAMED_P rather than ANON_AGGR_TYPE_P so that
2889 we also allow unnamed types used for defining fields. */
2890 if (DECL_ARTIFICIAL (elt)
2891 && (!DECL_IMPLICIT_TYPEDEF_P (elt)
2892 || TYPE_UNNAMED_P (TREE_TYPE (elt))))
2893 continue;
2895 if (complain
2896 && (TREE_CODE (elt) != FIELD_DECL
2897 || (TREE_PRIVATE (elt) || TREE_PROTECTED (elt))))
2899 /* We already complained about static data members in
2900 finish_static_data_member_decl. */
2901 if (!VAR_P (elt))
2903 auto_diagnostic_group d;
2904 if (permerror (DECL_SOURCE_LOCATION (elt),
2905 TREE_CODE (TREE_TYPE (field)) == UNION_TYPE
2906 ? "%q#D invalid; an anonymous union may "
2907 "only have public non-static data members"
2908 : "%q#D invalid; an anonymous struct may "
2909 "only have public non-static data members", elt))
2911 static bool hint;
2912 if (flag_permissive && !hint)
2914 hint = true;
2915 inform (DECL_SOURCE_LOCATION (elt),
2916 "this flexibility is deprecated and will be "
2917 "removed");
2923 TREE_PRIVATE (elt) = TREE_PRIVATE (field);
2924 TREE_PROTECTED (elt) = TREE_PROTECTED (field);
2926 /* Recurse into the anonymous aggregates to correctly handle
2927 access control (c++/24926):
2929 class A {
2930 union {
2931 union {
2932 int i;
2937 int j=A().i; */
2938 if (DECL_NAME (elt) == NULL_TREE
2939 && ANON_AGGR_TYPE_P (TREE_TYPE (elt)))
2940 finish_struct_anon_r (elt, /*complain=*/false);
2944 /* Check for things that are invalid. There are probably plenty of other
2945 things we should check for also. */
2947 static void
2948 finish_struct_anon (tree t)
2950 for (tree field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
2952 if (TREE_STATIC (field))
2953 continue;
2954 if (TREE_CODE (field) != FIELD_DECL)
2955 continue;
2957 if (DECL_NAME (field) == NULL_TREE
2958 && ANON_AGGR_TYPE_P (TREE_TYPE (field)))
2959 finish_struct_anon_r (field, /*complain=*/true);
2963 /* Add T to CLASSTYPE_DECL_LIST of current_class_type which
2964 will be used later during class template instantiation.
2965 When FRIEND_P is zero, T can be a static member data (VAR_DECL),
2966 a non-static member data (FIELD_DECL), a member function
2967 (FUNCTION_DECL), a nested type (RECORD_TYPE, ENUM_TYPE),
2968 a typedef (TYPE_DECL) or a member class template (TEMPLATE_DECL)
2969 When FRIEND_P is nonzero, T is either a friend class
2970 (RECORD_TYPE, TEMPLATE_DECL) or a friend function
2971 (FUNCTION_DECL, TEMPLATE_DECL). */
2973 void
2974 maybe_add_class_template_decl_list (tree type, tree t, int friend_p)
2976 /* Save some memory by not creating TREE_LIST if TYPE is not template. */
2977 if (CLASSTYPE_TEMPLATE_INFO (type))
2978 CLASSTYPE_DECL_LIST (type)
2979 = tree_cons (friend_p ? NULL_TREE : type,
2980 t, CLASSTYPE_DECL_LIST (type));
2983 /* This function is called from declare_virt_assop_and_dtor via
2984 dfs_walk_all.
2986 DATA is a type that direcly or indirectly inherits the base
2987 represented by BINFO. If BINFO contains a virtual assignment [copy
2988 assignment or move assigment] operator or a virtual constructor,
2989 declare that function in DATA if it hasn't been already declared. */
2991 static tree
2992 dfs_declare_virt_assop_and_dtor (tree binfo, void *data)
2994 tree bv, fn, t = (tree)data;
2995 tree opname = assign_op_identifier;
2997 gcc_assert (t && CLASS_TYPE_P (t));
2998 gcc_assert (binfo && TREE_CODE (binfo) == TREE_BINFO);
3000 if (!TYPE_CONTAINS_VPTR_P (BINFO_TYPE (binfo)))
3001 /* A base without a vtable needs no modification, and its bases
3002 are uninteresting. */
3003 return dfs_skip_bases;
3005 if (BINFO_PRIMARY_P (binfo))
3006 /* If this is a primary base, then we have already looked at the
3007 virtual functions of its vtable. */
3008 return NULL_TREE;
3010 for (bv = BINFO_VIRTUALS (binfo); bv; bv = TREE_CHAIN (bv))
3012 fn = BV_FN (bv);
3014 if (DECL_NAME (fn) == opname)
3016 if (CLASSTYPE_LAZY_COPY_ASSIGN (t))
3017 lazily_declare_fn (sfk_copy_assignment, t);
3018 if (CLASSTYPE_LAZY_MOVE_ASSIGN (t))
3019 lazily_declare_fn (sfk_move_assignment, t);
3021 else if (DECL_DESTRUCTOR_P (fn)
3022 && CLASSTYPE_LAZY_DESTRUCTOR (t))
3023 lazily_declare_fn (sfk_destructor, t);
3026 return NULL_TREE;
3029 /* If the class type T has a direct or indirect base that contains a
3030 virtual assignment operator or a virtual destructor, declare that
3031 function in T if it hasn't been already declared. */
3033 static void
3034 declare_virt_assop_and_dtor (tree t)
3036 if (!(TYPE_POLYMORPHIC_P (t)
3037 && (CLASSTYPE_LAZY_COPY_ASSIGN (t)
3038 || CLASSTYPE_LAZY_MOVE_ASSIGN (t)
3039 || CLASSTYPE_LAZY_DESTRUCTOR (t))))
3040 return;
3042 dfs_walk_all (TYPE_BINFO (t),
3043 dfs_declare_virt_assop_and_dtor,
3044 NULL, t);
3047 /* Declare the inheriting constructor for class T inherited from base
3048 constructor CTOR with the parameter array PARMS of size NPARMS. */
3050 static void
3051 one_inheriting_sig (tree t, tree ctor, tree *parms, int nparms)
3053 gcc_assert (TYPE_MAIN_VARIANT (t) == t);
3055 /* We don't declare an inheriting ctor that would be a default,
3056 copy or move ctor for derived or base. */
3057 if (nparms == 0)
3058 return;
3059 if (nparms == 1
3060 && TYPE_REF_P (parms[0]))
3062 tree parm = TYPE_MAIN_VARIANT (TREE_TYPE (parms[0]));
3063 if (parm == t || parm == DECL_CONTEXT (ctor))
3064 return;
3067 tree parmlist = void_list_node;
3068 for (int i = nparms - 1; i >= 0; i--)
3069 parmlist = tree_cons (NULL_TREE, parms[i], parmlist);
3070 tree fn = implicitly_declare_fn (sfk_inheriting_constructor,
3071 t, false, ctor, parmlist);
3073 if (add_method (t, fn, false))
3075 DECL_CHAIN (fn) = TYPE_FIELDS (t);
3076 TYPE_FIELDS (t) = fn;
3080 /* Declare all the inheriting constructors for class T inherited from base
3081 constructor CTOR. */
3083 static void
3084 one_inherited_ctor (tree ctor, tree t, tree using_decl)
3086 tree parms = FUNCTION_FIRST_USER_PARMTYPE (ctor);
3088 if (flag_new_inheriting_ctors)
3090 ctor = implicitly_declare_fn (sfk_inheriting_constructor,
3091 t, /*const*/false, ctor, parms);
3092 add_method (t, ctor, using_decl != NULL_TREE);
3093 TYPE_HAS_USER_CONSTRUCTOR (t) = true;
3094 return;
3097 tree *new_parms = XALLOCAVEC (tree, list_length (parms));
3098 int i = 0;
3099 for (; parms && parms != void_list_node; parms = TREE_CHAIN (parms))
3101 if (TREE_PURPOSE (parms))
3102 one_inheriting_sig (t, ctor, new_parms, i);
3103 new_parms[i++] = TREE_VALUE (parms);
3105 one_inheriting_sig (t, ctor, new_parms, i);
3106 if (parms == NULL_TREE)
3108 auto_diagnostic_group d;
3109 if (warning (OPT_Winherited_variadic_ctor,
3110 "the ellipsis in %qD is not inherited", ctor))
3111 inform (DECL_SOURCE_LOCATION (ctor), "%qD declared here", ctor);
3115 /* Create default constructors, assignment operators, and so forth for
3116 the type indicated by T, if they are needed. CANT_HAVE_CONST_CTOR,
3117 and CANT_HAVE_CONST_ASSIGNMENT are nonzero if, for whatever reason,
3118 the class cannot have a default constructor, copy constructor
3119 taking a const reference argument, or an assignment operator taking
3120 a const reference, respectively. */
3122 static void
3123 add_implicitly_declared_members (tree t, tree* access_decls,
3124 int cant_have_const_cctor,
3125 int cant_have_const_assignment)
3127 /* Destructor. */
3128 if (!CLASSTYPE_DESTRUCTOR (t))
3129 /* In general, we create destructors lazily. */
3130 CLASSTYPE_LAZY_DESTRUCTOR (t) = 1;
3132 bool move_ok = false;
3133 if (cxx_dialect >= cxx11 && CLASSTYPE_LAZY_DESTRUCTOR (t)
3134 && !TYPE_HAS_COPY_CTOR (t) && !TYPE_HAS_COPY_ASSIGN (t)
3135 && !classtype_has_move_assign_or_move_ctor_p (t, false))
3136 move_ok = true;
3138 /* [class.ctor]
3140 If there is no user-declared constructor for a class, a default
3141 constructor is implicitly declared. */
3142 if (! TYPE_HAS_USER_CONSTRUCTOR (t))
3144 TYPE_HAS_DEFAULT_CONSTRUCTOR (t) = 1;
3145 CLASSTYPE_LAZY_DEFAULT_CTOR (t) = 1;
3146 if (cxx_dialect >= cxx11)
3147 TYPE_HAS_CONSTEXPR_CTOR (t)
3148 /* Don't force the declaration to get a hard answer; if the
3149 definition would have made the class non-literal, it will still be
3150 non-literal because of the base or member in question, and that
3151 gives a better diagnostic. */
3152 = type_maybe_constexpr_default_constructor (t);
3155 /* [class.ctor]
3157 If a class definition does not explicitly declare a copy
3158 constructor, one is declared implicitly. */
3159 if (! TYPE_HAS_COPY_CTOR (t))
3161 TYPE_HAS_COPY_CTOR (t) = 1;
3162 TYPE_HAS_CONST_COPY_CTOR (t) = !cant_have_const_cctor;
3163 CLASSTYPE_LAZY_COPY_CTOR (t) = 1;
3164 if (move_ok)
3165 CLASSTYPE_LAZY_MOVE_CTOR (t) = 1;
3168 /* If there is no assignment operator, one will be created if and
3169 when it is needed. For now, just record whether or not the type
3170 of the parameter to the assignment operator will be a const or
3171 non-const reference. */
3172 if (!TYPE_HAS_COPY_ASSIGN (t))
3174 TYPE_HAS_COPY_ASSIGN (t) = 1;
3175 TYPE_HAS_CONST_COPY_ASSIGN (t) = !cant_have_const_assignment;
3176 CLASSTYPE_LAZY_COPY_ASSIGN (t) = 1;
3177 if (move_ok && !LAMBDA_TYPE_P (t))
3178 CLASSTYPE_LAZY_MOVE_ASSIGN (t) = 1;
3181 /* We can't be lazy about declaring functions that might override
3182 a virtual function from a base class. */
3183 declare_virt_assop_and_dtor (t);
3185 while (*access_decls)
3187 tree using_decl = TREE_VALUE (*access_decls);
3188 tree decl = USING_DECL_DECLS (using_decl);
3189 if (DECL_NAME (using_decl) == ctor_identifier)
3191 /* declare, then remove the decl */
3192 tree ctor_list = decl;
3193 location_t loc = input_location;
3194 input_location = DECL_SOURCE_LOCATION (using_decl);
3195 for (ovl_iterator iter (ctor_list); iter; ++iter)
3196 one_inherited_ctor (*iter, t, using_decl);
3197 *access_decls = TREE_CHAIN (*access_decls);
3198 input_location = loc;
3200 else
3201 access_decls = &TREE_CHAIN (*access_decls);
3205 /* FIELD is a bit-field. We are finishing the processing for its
3206 enclosing type. Issue any appropriate messages and set appropriate
3207 flags. Returns false if an error has been diagnosed. */
3209 static bool
3210 check_bitfield_decl (tree field)
3212 tree type = TREE_TYPE (field);
3213 tree w;
3215 /* Extract the declared width of the bitfield, which has been
3216 temporarily stashed in DECL_BIT_FIELD_REPRESENTATIVE by grokbitfield. */
3217 w = DECL_BIT_FIELD_REPRESENTATIVE (field);
3218 gcc_assert (w != NULL_TREE);
3219 /* Remove the bit-field width indicator so that the rest of the
3220 compiler does not treat that value as a qualifier. */
3221 DECL_BIT_FIELD_REPRESENTATIVE (field) = NULL_TREE;
3223 /* Detect invalid bit-field type. */
3224 if (!INTEGRAL_OR_ENUMERATION_TYPE_P (type))
3226 error ("bit-field %q+#D with non-integral type", field);
3227 w = error_mark_node;
3229 else
3231 location_t loc = input_location;
3232 /* Avoid the non_lvalue wrapper added by fold for PLUS_EXPRs. */
3233 STRIP_NOPS (w);
3235 /* detect invalid field size. */
3236 input_location = DECL_SOURCE_LOCATION (field);
3237 w = cxx_constant_value (w);
3238 input_location = loc;
3240 if (TREE_CODE (w) != INTEGER_CST)
3242 error ("bit-field %q+D width not an integer constant", field);
3243 w = error_mark_node;
3245 else if (tree_int_cst_sgn (w) < 0)
3247 error ("negative width in bit-field %q+D", field);
3248 w = error_mark_node;
3250 else if (integer_zerop (w) && DECL_NAME (field) != 0)
3252 error ("zero width for bit-field %q+D", field);
3253 w = error_mark_node;
3255 else if ((TREE_CODE (type) != ENUMERAL_TYPE
3256 && TREE_CODE (type) != BOOLEAN_TYPE
3257 && compare_tree_int (w, TYPE_PRECISION (type)) > 0)
3258 || ((TREE_CODE (type) == ENUMERAL_TYPE
3259 || TREE_CODE (type) == BOOLEAN_TYPE)
3260 && tree_int_cst_lt (TYPE_SIZE (type), w)))
3261 warning_at (DECL_SOURCE_LOCATION (field), 0,
3262 "width of %qD exceeds its type", field);
3263 else if (TREE_CODE (type) == ENUMERAL_TYPE)
3265 int prec = TYPE_PRECISION (ENUM_UNDERLYING_TYPE (type));
3266 if (compare_tree_int (w, prec) < 0)
3267 warning_at (DECL_SOURCE_LOCATION (field), 0,
3268 "%qD is too small to hold all values of %q#T",
3269 field, type);
3273 if (w != error_mark_node)
3275 DECL_SIZE (field) = fold_convert (bitsizetype, w);
3276 DECL_BIT_FIELD (field) = 1;
3277 return true;
3279 else
3281 /* Non-bit-fields are aligned for their type. */
3282 DECL_BIT_FIELD (field) = 0;
3283 CLEAR_DECL_C_BIT_FIELD (field);
3284 return false;
3288 /* FIELD is a non bit-field. We are finishing the processing for its
3289 enclosing type T. Issue any appropriate messages and set appropriate
3290 flags. */
3292 static bool
3293 check_field_decl (tree field,
3294 tree t,
3295 int* cant_have_const_ctor,
3296 int* no_const_asn_ref)
3298 tree type = strip_array_types (TREE_TYPE (field));
3299 bool any_default_members = false;
3301 /* In C++98 an anonymous union cannot contain any fields which would change
3302 the settings of CANT_HAVE_CONST_CTOR and friends. */
3303 if (ANON_UNION_TYPE_P (type) && cxx_dialect < cxx11)
3305 /* And, we don't set TYPE_HAS_CONST_COPY_CTOR, etc., for anonymous
3306 structs. So, we recurse through their fields here. */
3307 else if (ANON_AGGR_TYPE_P (type))
3309 for (tree fields = TYPE_FIELDS (type); fields;
3310 fields = DECL_CHAIN (fields))
3311 if (TREE_CODE (fields) == FIELD_DECL)
3312 any_default_members |= check_field_decl (fields, t,
3313 cant_have_const_ctor,
3314 no_const_asn_ref);
3316 /* Check members with class type for constructors, destructors,
3317 etc. */
3318 else if (CLASS_TYPE_P (type))
3320 /* Never let anything with uninheritable virtuals
3321 make it through without complaint. */
3322 abstract_virtuals_error (field, type);
3324 if (TREE_CODE (t) == UNION_TYPE && cxx_dialect < cxx11)
3326 static bool warned;
3327 int oldcount = errorcount;
3328 if (TYPE_NEEDS_CONSTRUCTING (type))
3329 error ("member %q+#D with constructor not allowed in union",
3330 field);
3331 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
3332 error ("member %q+#D with destructor not allowed in union", field);
3333 if (TYPE_HAS_COMPLEX_COPY_ASSIGN (type))
3334 error ("member %q+#D with copy assignment operator not allowed in union",
3335 field);
3336 if (!warned && errorcount > oldcount)
3338 inform (DECL_SOURCE_LOCATION (field), "unrestricted unions "
3339 "only available with -std=c++11 or -std=gnu++11");
3340 warned = true;
3343 else
3345 TYPE_NEEDS_CONSTRUCTING (t) |= TYPE_NEEDS_CONSTRUCTING (type);
3346 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t)
3347 |= TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type);
3348 TYPE_HAS_COMPLEX_COPY_ASSIGN (t)
3349 |= (TYPE_HAS_COMPLEX_COPY_ASSIGN (type)
3350 || !TYPE_HAS_COPY_ASSIGN (type));
3351 TYPE_HAS_COMPLEX_COPY_CTOR (t) |= (TYPE_HAS_COMPLEX_COPY_CTOR (type)
3352 || !TYPE_HAS_COPY_CTOR (type));
3353 TYPE_HAS_COMPLEX_MOVE_ASSIGN (t) |= TYPE_HAS_COMPLEX_MOVE_ASSIGN (type);
3354 TYPE_HAS_COMPLEX_MOVE_CTOR (t) |= TYPE_HAS_COMPLEX_MOVE_CTOR (type);
3355 TYPE_HAS_COMPLEX_DFLT (t) |= (!TYPE_HAS_DEFAULT_CONSTRUCTOR (type)
3356 || TYPE_HAS_COMPLEX_DFLT (type));
3359 if (TYPE_HAS_COPY_CTOR (type)
3360 && !TYPE_HAS_CONST_COPY_CTOR (type))
3361 *cant_have_const_ctor = 1;
3363 if (TYPE_HAS_COPY_ASSIGN (type)
3364 && !TYPE_HAS_CONST_COPY_ASSIGN (type))
3365 *no_const_asn_ref = 1;
3368 check_abi_tags (t, field);
3370 if (DECL_INITIAL (field) != NULL_TREE)
3371 /* `build_class_init_list' does not recognize
3372 non-FIELD_DECLs. */
3373 any_default_members = true;
3375 return any_default_members;
3378 /* Check the data members (both static and non-static), class-scoped
3379 typedefs, etc., appearing in the declaration of T. Issue
3380 appropriate diagnostics. Sets ACCESS_DECLS to a list (in
3381 declaration order) of access declarations; each TREE_VALUE in this
3382 list is a USING_DECL.
3384 In addition, set the following flags:
3386 EMPTY_P
3387 The class is empty, i.e., contains no non-static data members.
3389 CANT_HAVE_CONST_CTOR_P
3390 This class cannot have an implicitly generated copy constructor
3391 taking a const reference.
3393 CANT_HAVE_CONST_ASN_REF
3394 This class cannot have an implicitly generated assignment
3395 operator taking a const reference.
3397 All of these flags should be initialized before calling this
3398 function.
3400 Returns a pointer to the end of the TYPE_FIELDs chain; additional
3401 fields can be added by adding to this chain. */
3403 static void
3404 check_field_decls (tree t, tree *access_decls,
3405 int *cant_have_const_ctor_p,
3406 int *no_const_asn_ref_p)
3408 tree *field;
3409 tree *next;
3410 bool has_pointers;
3411 bool any_default_members;
3412 int cant_pack = 0;
3413 int field_access = -1;
3415 /* Assume there are no access declarations. */
3416 *access_decls = NULL_TREE;
3417 /* Assume this class has no pointer members. */
3418 has_pointers = false;
3419 /* Assume none of the members of this class have default
3420 initializations. */
3421 any_default_members = false;
3423 for (field = &TYPE_FIELDS (t); *field; field = next)
3425 tree x = *field;
3426 tree type = TREE_TYPE (x);
3427 int this_field_access;
3429 next = &DECL_CHAIN (x);
3431 if (TREE_CODE (x) == USING_DECL)
3433 /* Save the access declarations for our caller. */
3434 *access_decls = tree_cons (NULL_TREE, x, *access_decls);
3435 continue;
3438 if (TREE_CODE (x) == TYPE_DECL
3439 || TREE_CODE (x) == TEMPLATE_DECL)
3440 continue;
3442 if (TREE_CODE (x) == FUNCTION_DECL)
3443 /* FIXME: We should fold in the checking from check_methods. */
3444 continue;
3446 /* If we've gotten this far, it's a data member, possibly static,
3447 or an enumerator. */
3448 if (TREE_CODE (x) != CONST_DECL)
3449 DECL_CONTEXT (x) = t;
3451 /* When this goes into scope, it will be a non-local reference. */
3452 DECL_NONLOCAL (x) = 1;
3454 if (TREE_CODE (t) == UNION_TYPE)
3456 /* [class.union] (C++98)
3458 If a union contains a static data member, or a member of
3459 reference type, the program is ill-formed.
3461 In C++11 [class.union] says:
3462 If a union contains a non-static data member of reference type
3463 the program is ill-formed. */
3464 if (VAR_P (x) && cxx_dialect < cxx11)
3466 error ("in C++98 %q+D may not be static because it is "
3467 "a member of a union", x);
3468 continue;
3470 if (TYPE_REF_P (type)
3471 && TREE_CODE (x) == FIELD_DECL)
3473 error ("non-static data member %q+D in a union may not "
3474 "have reference type %qT", x, type);
3475 continue;
3479 /* Perform error checking that did not get done in
3480 grokdeclarator. */
3481 if (TREE_CODE (type) == FUNCTION_TYPE)
3483 error ("field %q+D invalidly declared function type", x);
3484 type = build_pointer_type (type);
3485 TREE_TYPE (x) = type;
3487 else if (TREE_CODE (type) == METHOD_TYPE)
3489 error ("field %q+D invalidly declared method type", x);
3490 type = build_pointer_type (type);
3491 TREE_TYPE (x) = type;
3494 if (type == error_mark_node)
3495 continue;
3497 if (TREE_CODE (x) == CONST_DECL || VAR_P (x))
3498 continue;
3500 /* Now it can only be a FIELD_DECL. */
3502 if (TREE_PRIVATE (x) || TREE_PROTECTED (x))
3503 CLASSTYPE_NON_AGGREGATE (t) = 1;
3505 /* If at least one non-static data member is non-literal, the whole
3506 class becomes non-literal. Per Core/1453, volatile non-static
3507 data members and base classes are also not allowed.
3508 Note: if the type is incomplete we will complain later on. */
3509 if (COMPLETE_TYPE_P (type)
3510 && (!literal_type_p (type) || CP_TYPE_VOLATILE_P (type)))
3511 CLASSTYPE_LITERAL_P (t) = false;
3513 /* A standard-layout class is a class that:
3515 has the same access control (Clause 11) for all non-static data members,
3516 ... */
3517 this_field_access = TREE_PROTECTED (x) ? 1 : TREE_PRIVATE (x) ? 2 : 0;
3518 if (field_access == -1)
3519 field_access = this_field_access;
3520 else if (this_field_access != field_access)
3521 CLASSTYPE_NON_STD_LAYOUT (t) = 1;
3523 /* If this is of reference type, check if it needs an init. */
3524 if (TYPE_REF_P (type))
3526 CLASSTYPE_NON_LAYOUT_POD_P (t) = 1;
3527 CLASSTYPE_NON_STD_LAYOUT (t) = 1;
3528 if (DECL_INITIAL (x) == NULL_TREE)
3529 SET_CLASSTYPE_REF_FIELDS_NEED_INIT (t, 1);
3530 if (cxx_dialect < cxx11)
3532 /* ARM $12.6.2: [A member initializer list] (or, for an
3533 aggregate, initialization by a brace-enclosed list) is the
3534 only way to initialize nonstatic const and reference
3535 members. */
3536 TYPE_HAS_COMPLEX_COPY_ASSIGN (t) = 1;
3537 TYPE_HAS_COMPLEX_MOVE_ASSIGN (t) = 1;
3541 type = strip_array_types (type);
3543 if (TYPE_PACKED (t))
3545 if (!layout_pod_type_p (type) && !TYPE_PACKED (type))
3547 warning_at
3548 (DECL_SOURCE_LOCATION (x), 0,
3549 "ignoring packed attribute because of unpacked non-POD field %q#D",
3551 cant_pack = 1;
3553 else if (DECL_C_BIT_FIELD (x)
3554 || TYPE_ALIGN (TREE_TYPE (x)) > BITS_PER_UNIT)
3555 DECL_PACKED (x) = 1;
3558 if (DECL_C_BIT_FIELD (x)
3559 && integer_zerop (DECL_BIT_FIELD_REPRESENTATIVE (x)))
3560 /* We don't treat zero-width bitfields as making a class
3561 non-empty. */
3563 else
3565 /* The class is non-empty. */
3566 CLASSTYPE_EMPTY_P (t) = 0;
3567 /* The class is not even nearly empty. */
3568 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
3569 /* If one of the data members contains an empty class,
3570 so does T. */
3571 if (CLASS_TYPE_P (type)
3572 && CLASSTYPE_CONTAINS_EMPTY_CLASS_P (type))
3573 CLASSTYPE_CONTAINS_EMPTY_CLASS_P (t) = 1;
3576 /* This is used by -Weffc++ (see below). Warn only for pointers
3577 to members which might hold dynamic memory. So do not warn
3578 for pointers to functions or pointers to members. */
3579 if (TYPE_PTR_P (type)
3580 && !TYPE_PTRFN_P (type))
3581 has_pointers = true;
3583 if (CLASS_TYPE_P (type))
3585 if (CLASSTYPE_REF_FIELDS_NEED_INIT (type))
3586 SET_CLASSTYPE_REF_FIELDS_NEED_INIT (t, 1);
3587 if (CLASSTYPE_READONLY_FIELDS_NEED_INIT (type))
3588 SET_CLASSTYPE_READONLY_FIELDS_NEED_INIT (t, 1);
3591 if (DECL_MUTABLE_P (x) || TYPE_HAS_MUTABLE_P (type))
3592 CLASSTYPE_HAS_MUTABLE (t) = 1;
3594 if (DECL_MUTABLE_P (x))
3596 if (CP_TYPE_CONST_P (type))
3598 error ("member %q+D cannot be declared both %<const%> "
3599 "and %<mutable%>", x);
3600 continue;
3602 if (TYPE_REF_P (type))
3604 error ("member %q+D cannot be declared as a %<mutable%> "
3605 "reference", x);
3606 continue;
3610 if (! layout_pod_type_p (type))
3611 /* DR 148 now allows pointers to members (which are POD themselves),
3612 to be allowed in POD structs. */
3613 CLASSTYPE_NON_LAYOUT_POD_P (t) = 1;
3615 if (!std_layout_type_p (type))
3616 CLASSTYPE_NON_STD_LAYOUT (t) = 1;
3618 if (! zero_init_p (type))
3619 CLASSTYPE_NON_ZERO_INIT_P (t) = 1;
3621 /* We set DECL_C_BIT_FIELD in grokbitfield.
3622 If the type and width are valid, we'll also set DECL_BIT_FIELD. */
3623 if (DECL_C_BIT_FIELD (x))
3624 check_bitfield_decl (x);
3626 if (check_field_decl (x, t, cant_have_const_ctor_p, no_const_asn_ref_p))
3628 if (any_default_members
3629 && TREE_CODE (t) == UNION_TYPE)
3630 error ("multiple fields in union %qT initialized", t);
3631 any_default_members = true;
3634 /* Now that we've removed bit-field widths from DECL_INITIAL,
3635 anything left in DECL_INITIAL is an NSDMI that makes the class
3636 non-aggregate in C++11. */
3637 if (DECL_INITIAL (x) && cxx_dialect < cxx14)
3638 CLASSTYPE_NON_AGGREGATE (t) = true;
3640 /* If any field is const, the structure type is pseudo-const. */
3641 if (CP_TYPE_CONST_P (type))
3643 C_TYPE_FIELDS_READONLY (t) = 1;
3644 if (DECL_INITIAL (x) == NULL_TREE)
3645 SET_CLASSTYPE_READONLY_FIELDS_NEED_INIT (t, 1);
3646 if (cxx_dialect < cxx11)
3648 /* ARM $12.6.2: [A member initializer list] (or, for an
3649 aggregate, initialization by a brace-enclosed list) is the
3650 only way to initialize nonstatic const and reference
3651 members. */
3652 TYPE_HAS_COMPLEX_COPY_ASSIGN (t) = 1;
3653 TYPE_HAS_COMPLEX_MOVE_ASSIGN (t) = 1;
3656 /* A field that is pseudo-const makes the structure likewise. */
3657 else if (CLASS_TYPE_P (type))
3659 C_TYPE_FIELDS_READONLY (t) |= C_TYPE_FIELDS_READONLY (type);
3660 SET_CLASSTYPE_READONLY_FIELDS_NEED_INIT (t,
3661 CLASSTYPE_READONLY_FIELDS_NEED_INIT (t)
3662 | CLASSTYPE_READONLY_FIELDS_NEED_INIT (type));
3665 /* Core issue 80: A nonstatic data member is required to have a
3666 different name from the class iff the class has a
3667 user-declared constructor. */
3668 if (constructor_name_p (DECL_NAME (x), t)
3669 && TYPE_HAS_USER_CONSTRUCTOR (t))
3670 permerror (DECL_SOURCE_LOCATION (x),
3671 "field %q#D with same name as class", x);
3674 /* Effective C++ rule 11: if a class has dynamic memory held by pointers,
3675 it should also define a copy constructor and an assignment operator to
3676 implement the correct copy semantic (deep vs shallow, etc.). As it is
3677 not feasible to check whether the constructors do allocate dynamic memory
3678 and store it within members, we approximate the warning like this:
3680 -- Warn only if there are members which are pointers
3681 -- Warn only if there is a non-trivial constructor (otherwise,
3682 there cannot be memory allocated).
3683 -- Warn only if there is a non-trivial destructor. We assume that the
3684 user at least implemented the cleanup correctly, and a destructor
3685 is needed to free dynamic memory.
3687 This seems enough for practical purposes. */
3688 if (warn_ecpp
3689 && has_pointers
3690 && TYPE_HAS_USER_CONSTRUCTOR (t)
3691 && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t)
3692 && !(TYPE_HAS_COPY_CTOR (t) && TYPE_HAS_COPY_ASSIGN (t)))
3694 warning (OPT_Weffc__, "%q#T has pointer data members", t);
3696 if (! TYPE_HAS_COPY_CTOR (t))
3698 warning (OPT_Weffc__,
3699 " but does not override %<%T(const %T&)%>", t, t);
3700 if (!TYPE_HAS_COPY_ASSIGN (t))
3701 warning (OPT_Weffc__, " or %<operator=(const %T&)%>", t);
3703 else if (! TYPE_HAS_COPY_ASSIGN (t))
3704 warning (OPT_Weffc__,
3705 " but does not override %<operator=(const %T&)%>", t);
3708 /* Non-static data member initializers make the default constructor
3709 non-trivial. */
3710 if (any_default_members)
3712 TYPE_NEEDS_CONSTRUCTING (t) = true;
3713 TYPE_HAS_COMPLEX_DFLT (t) = true;
3716 /* If any of the fields couldn't be packed, unset TYPE_PACKED. */
3717 if (cant_pack)
3718 TYPE_PACKED (t) = 0;
3720 /* Check anonymous struct/anonymous union fields. */
3721 finish_struct_anon (t);
3723 /* We've built up the list of access declarations in reverse order.
3724 Fix that now. */
3725 *access_decls = nreverse (*access_decls);
3728 /* If TYPE is an empty class type, records its OFFSET in the table of
3729 OFFSETS. */
3731 static int
3732 record_subobject_offset (tree type, tree offset, splay_tree offsets)
3734 splay_tree_node n;
3736 if (!is_empty_class (type))
3737 return 0;
3739 /* Record the location of this empty object in OFFSETS. */
3740 n = splay_tree_lookup (offsets, (splay_tree_key) offset);
3741 if (!n)
3742 n = splay_tree_insert (offsets,
3743 (splay_tree_key) offset,
3744 (splay_tree_value) NULL_TREE);
3745 n->value = ((splay_tree_value)
3746 tree_cons (NULL_TREE,
3747 type,
3748 (tree) n->value));
3750 return 0;
3753 /* Returns nonzero if TYPE is an empty class type and there is
3754 already an entry in OFFSETS for the same TYPE as the same OFFSET. */
3756 static int
3757 check_subobject_offset (tree type, tree offset, splay_tree offsets)
3759 splay_tree_node n;
3760 tree t;
3762 if (!is_empty_class (type))
3763 return 0;
3765 /* Record the location of this empty object in OFFSETS. */
3766 n = splay_tree_lookup (offsets, (splay_tree_key) offset);
3767 if (!n)
3768 return 0;
3770 for (t = (tree) n->value; t; t = TREE_CHAIN (t))
3771 if (same_type_p (TREE_VALUE (t), type))
3772 return 1;
3774 return 0;
3777 /* Walk through all the subobjects of TYPE (located at OFFSET). Call
3778 F for every subobject, passing it the type, offset, and table of
3779 OFFSETS. If VBASES_P is one, then virtual non-primary bases should
3780 be traversed.
3782 If MAX_OFFSET is non-NULL, then subobjects with an offset greater
3783 than MAX_OFFSET will not be walked.
3785 If F returns a nonzero value, the traversal ceases, and that value
3786 is returned. Otherwise, returns zero. */
3788 static int
3789 walk_subobject_offsets (tree type,
3790 subobject_offset_fn f,
3791 tree offset,
3792 splay_tree offsets,
3793 tree max_offset,
3794 int vbases_p)
3796 int r = 0;
3797 tree type_binfo = NULL_TREE;
3799 /* If this OFFSET is bigger than the MAX_OFFSET, then we should
3800 stop. */
3801 if (max_offset && tree_int_cst_lt (max_offset, offset))
3802 return 0;
3804 if (type == error_mark_node)
3805 return 0;
3807 if (!TYPE_P (type))
3809 type_binfo = type;
3810 type = BINFO_TYPE (type);
3813 if (CLASS_TYPE_P (type))
3815 tree field;
3816 tree binfo;
3817 int i;
3819 /* Avoid recursing into objects that are not interesting. */
3820 if (!CLASSTYPE_CONTAINS_EMPTY_CLASS_P (type))
3821 return 0;
3823 /* Record the location of TYPE. */
3824 r = (*f) (type, offset, offsets);
3825 if (r)
3826 return r;
3828 /* Iterate through the direct base classes of TYPE. */
3829 if (!type_binfo)
3830 type_binfo = TYPE_BINFO (type);
3831 for (i = 0; BINFO_BASE_ITERATE (type_binfo, i, binfo); i++)
3833 tree binfo_offset;
3835 if (BINFO_VIRTUAL_P (binfo))
3836 continue;
3838 tree orig_binfo;
3839 /* We cannot rely on BINFO_OFFSET being set for the base
3840 class yet, but the offsets for direct non-virtual
3841 bases can be calculated by going back to the TYPE. */
3842 orig_binfo = BINFO_BASE_BINFO (TYPE_BINFO (type), i);
3843 binfo_offset = size_binop (PLUS_EXPR,
3844 offset,
3845 BINFO_OFFSET (orig_binfo));
3847 r = walk_subobject_offsets (binfo,
3849 binfo_offset,
3850 offsets,
3851 max_offset,
3852 /*vbases_p=*/0);
3853 if (r)
3854 return r;
3857 if (CLASSTYPE_VBASECLASSES (type))
3859 unsigned ix;
3860 vec<tree, va_gc> *vbases;
3862 /* Iterate through the virtual base classes of TYPE. In G++
3863 3.2, we included virtual bases in the direct base class
3864 loop above, which results in incorrect results; the
3865 correct offsets for virtual bases are only known when
3866 working with the most derived type. */
3867 if (vbases_p)
3868 for (vbases = CLASSTYPE_VBASECLASSES (type), ix = 0;
3869 vec_safe_iterate (vbases, ix, &binfo); ix++)
3871 r = walk_subobject_offsets (binfo,
3873 size_binop (PLUS_EXPR,
3874 offset,
3875 BINFO_OFFSET (binfo)),
3876 offsets,
3877 max_offset,
3878 /*vbases_p=*/0);
3879 if (r)
3880 return r;
3882 else
3884 /* We still have to walk the primary base, if it is
3885 virtual. (If it is non-virtual, then it was walked
3886 above.) */
3887 tree vbase = get_primary_binfo (type_binfo);
3889 if (vbase && BINFO_VIRTUAL_P (vbase)
3890 && BINFO_PRIMARY_P (vbase)
3891 && BINFO_INHERITANCE_CHAIN (vbase) == type_binfo)
3893 r = (walk_subobject_offsets
3894 (vbase, f, offset,
3895 offsets, max_offset, /*vbases_p=*/0));
3896 if (r)
3897 return r;
3902 /* Iterate through the fields of TYPE. */
3903 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
3904 if (TREE_CODE (field) == FIELD_DECL
3905 && TREE_TYPE (field) != error_mark_node
3906 && !DECL_ARTIFICIAL (field))
3908 tree field_offset;
3910 field_offset = byte_position (field);
3912 r = walk_subobject_offsets (TREE_TYPE (field),
3914 size_binop (PLUS_EXPR,
3915 offset,
3916 field_offset),
3917 offsets,
3918 max_offset,
3919 /*vbases_p=*/1);
3920 if (r)
3921 return r;
3924 else if (TREE_CODE (type) == ARRAY_TYPE)
3926 tree element_type = strip_array_types (type);
3927 tree domain = TYPE_DOMAIN (type);
3928 tree index;
3930 /* Avoid recursing into objects that are not interesting. */
3931 if (!CLASS_TYPE_P (element_type)
3932 || !CLASSTYPE_CONTAINS_EMPTY_CLASS_P (element_type)
3933 || !domain
3934 || integer_minus_onep (TYPE_MAX_VALUE (domain)))
3935 return 0;
3937 /* Step through each of the elements in the array. */
3938 for (index = size_zero_node;
3939 !tree_int_cst_lt (TYPE_MAX_VALUE (domain), index);
3940 index = size_binop (PLUS_EXPR, index, size_one_node))
3942 r = walk_subobject_offsets (TREE_TYPE (type),
3944 offset,
3945 offsets,
3946 max_offset,
3947 /*vbases_p=*/1);
3948 if (r)
3949 return r;
3950 offset = size_binop (PLUS_EXPR, offset,
3951 TYPE_SIZE_UNIT (TREE_TYPE (type)));
3952 /* If this new OFFSET is bigger than the MAX_OFFSET, then
3953 there's no point in iterating through the remaining
3954 elements of the array. */
3955 if (max_offset && tree_int_cst_lt (max_offset, offset))
3956 break;
3960 return 0;
3963 /* Record all of the empty subobjects of TYPE (either a type or a
3964 binfo). If IS_DATA_MEMBER is true, then a non-static data member
3965 is being placed at OFFSET; otherwise, it is a base class that is
3966 being placed at OFFSET. */
3968 static void
3969 record_subobject_offsets (tree type,
3970 tree offset,
3971 splay_tree offsets,
3972 bool is_data_member)
3974 tree max_offset;
3975 /* If recording subobjects for a non-static data member or a
3976 non-empty base class , we do not need to record offsets beyond
3977 the size of the biggest empty class. Additional data members
3978 will go at the end of the class. Additional base classes will go
3979 either at offset zero (if empty, in which case they cannot
3980 overlap with offsets past the size of the biggest empty class) or
3981 at the end of the class.
3983 However, if we are placing an empty base class, then we must record
3984 all offsets, as either the empty class is at offset zero (where
3985 other empty classes might later be placed) or at the end of the
3986 class (where other objects might then be placed, so other empty
3987 subobjects might later overlap). */
3988 if (is_data_member
3989 || !is_empty_class (BINFO_TYPE (type)))
3990 max_offset = sizeof_biggest_empty_class;
3991 else
3992 max_offset = NULL_TREE;
3993 walk_subobject_offsets (type, record_subobject_offset, offset,
3994 offsets, max_offset, is_data_member);
3997 /* Returns nonzero if any of the empty subobjects of TYPE (located at
3998 OFFSET) conflict with entries in OFFSETS. If VBASES_P is nonzero,
3999 virtual bases of TYPE are examined. */
4001 static int
4002 layout_conflict_p (tree type,
4003 tree offset,
4004 splay_tree offsets,
4005 int vbases_p)
4007 splay_tree_node max_node;
4009 /* Get the node in OFFSETS that indicates the maximum offset where
4010 an empty subobject is located. */
4011 max_node = splay_tree_max (offsets);
4012 /* If there aren't any empty subobjects, then there's no point in
4013 performing this check. */
4014 if (!max_node)
4015 return 0;
4017 return walk_subobject_offsets (type, check_subobject_offset, offset,
4018 offsets, (tree) (max_node->key),
4019 vbases_p);
4022 /* DECL is a FIELD_DECL corresponding either to a base subobject of a
4023 non-static data member of the type indicated by RLI. BINFO is the
4024 binfo corresponding to the base subobject, OFFSETS maps offsets to
4025 types already located at those offsets. This function determines
4026 the position of the DECL. */
4028 static void
4029 layout_nonempty_base_or_field (record_layout_info rli,
4030 tree decl,
4031 tree binfo,
4032 splay_tree offsets)
4034 tree offset = NULL_TREE;
4035 bool field_p;
4036 tree type;
4038 if (binfo)
4040 /* For the purposes of determining layout conflicts, we want to
4041 use the class type of BINFO; TREE_TYPE (DECL) will be the
4042 CLASSTYPE_AS_BASE version, which does not contain entries for
4043 zero-sized bases. */
4044 type = TREE_TYPE (binfo);
4045 field_p = false;
4047 else
4049 type = TREE_TYPE (decl);
4050 field_p = true;
4053 /* Try to place the field. It may take more than one try if we have
4054 a hard time placing the field without putting two objects of the
4055 same type at the same address. */
4056 while (1)
4058 struct record_layout_info_s old_rli = *rli;
4060 /* Place this field. */
4061 place_field (rli, decl);
4062 offset = byte_position (decl);
4064 /* We have to check to see whether or not there is already
4065 something of the same type at the offset we're about to use.
4066 For example, consider:
4068 struct S {};
4069 struct T : public S { int i; };
4070 struct U : public S, public T {};
4072 Here, we put S at offset zero in U. Then, we can't put T at
4073 offset zero -- its S component would be at the same address
4074 as the S we already allocated. So, we have to skip ahead.
4075 Since all data members, including those whose type is an
4076 empty class, have nonzero size, any overlap can happen only
4077 with a direct or indirect base-class -- it can't happen with
4078 a data member. */
4079 /* In a union, overlap is permitted; all members are placed at
4080 offset zero. */
4081 if (TREE_CODE (rli->t) == UNION_TYPE)
4082 break;
4083 if (layout_conflict_p (field_p ? type : binfo, offset,
4084 offsets, field_p))
4086 /* Strip off the size allocated to this field. That puts us
4087 at the first place we could have put the field with
4088 proper alignment. */
4089 *rli = old_rli;
4091 /* Bump up by the alignment required for the type. */
4092 rli->bitpos
4093 = size_binop (PLUS_EXPR, rli->bitpos,
4094 bitsize_int (binfo
4095 ? CLASSTYPE_ALIGN (type)
4096 : TYPE_ALIGN (type)));
4097 normalize_rli (rli);
4099 else if (TREE_CODE (type) == NULLPTR_TYPE
4100 && warn_abi && abi_version_crosses (9))
4102 /* Before ABI v9, we were giving nullptr_t alignment of 1; if
4103 the offset wasn't aligned like a pointer when we started to
4104 layout this field, that affects its position. */
4105 tree pos = rli_size_unit_so_far (&old_rli);
4106 if (int_cst_value (pos) % TYPE_ALIGN_UNIT (ptr_type_node) != 0)
4108 if (abi_version_at_least (9))
4109 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wabi,
4110 "alignment of %qD increased in -fabi-version=9 "
4111 "(GCC 5.2)", decl);
4112 else
4113 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wabi, "alignment "
4114 "of %qD will increase in -fabi-version=9", decl);
4116 break;
4118 else
4119 /* There was no conflict. We're done laying out this field. */
4120 break;
4123 /* Now that we know where it will be placed, update its
4124 BINFO_OFFSET. */
4125 if (binfo && CLASS_TYPE_P (BINFO_TYPE (binfo)))
4126 /* Indirect virtual bases may have a nonzero BINFO_OFFSET at
4127 this point because their BINFO_OFFSET is copied from another
4128 hierarchy. Therefore, we may not need to add the entire
4129 OFFSET. */
4130 propagate_binfo_offsets (binfo,
4131 size_diffop_loc (input_location,
4132 fold_convert (ssizetype, offset),
4133 fold_convert (ssizetype,
4134 BINFO_OFFSET (binfo))));
4137 /* Returns true if TYPE is empty and OFFSET is nonzero. */
4139 static int
4140 empty_base_at_nonzero_offset_p (tree type,
4141 tree offset,
4142 splay_tree /*offsets*/)
4144 return is_empty_class (type) && !integer_zerop (offset);
4147 /* Layout the empty base BINFO. EOC indicates the byte currently just
4148 past the end of the class, and should be correctly aligned for a
4149 class of the type indicated by BINFO; OFFSETS gives the offsets of
4150 the empty bases allocated so far. T is the most derived
4151 type. Return nonzero iff we added it at the end. */
4153 static bool
4154 layout_empty_base (record_layout_info rli, tree binfo,
4155 tree eoc, splay_tree offsets)
4157 tree alignment;
4158 tree basetype = BINFO_TYPE (binfo);
4159 bool atend = false;
4161 /* This routine should only be used for empty classes. */
4162 gcc_assert (is_empty_class (basetype));
4163 alignment = ssize_int (CLASSTYPE_ALIGN_UNIT (basetype));
4165 if (!integer_zerop (BINFO_OFFSET (binfo)))
4166 propagate_binfo_offsets
4167 (binfo, size_diffop_loc (input_location,
4168 size_zero_node, BINFO_OFFSET (binfo)));
4170 /* This is an empty base class. We first try to put it at offset
4171 zero. */
4172 if (layout_conflict_p (binfo,
4173 BINFO_OFFSET (binfo),
4174 offsets,
4175 /*vbases_p=*/0))
4177 /* That didn't work. Now, we move forward from the next
4178 available spot in the class. */
4179 atend = true;
4180 propagate_binfo_offsets (binfo, fold_convert (ssizetype, eoc));
4181 while (1)
4183 if (!layout_conflict_p (binfo,
4184 BINFO_OFFSET (binfo),
4185 offsets,
4186 /*vbases_p=*/0))
4187 /* We finally found a spot where there's no overlap. */
4188 break;
4190 /* There's overlap here, too. Bump along to the next spot. */
4191 propagate_binfo_offsets (binfo, alignment);
4195 if (CLASSTYPE_USER_ALIGN (basetype))
4197 rli->record_align = MAX (rli->record_align, CLASSTYPE_ALIGN (basetype));
4198 if (warn_packed)
4199 rli->unpacked_align = MAX (rli->unpacked_align, CLASSTYPE_ALIGN (basetype));
4200 TYPE_USER_ALIGN (rli->t) = 1;
4203 return atend;
4206 /* Build the FIELD_DECL for BASETYPE as a base of T, add it to the chain of
4207 fields at NEXT_FIELD, and return it. */
4209 static tree
4210 build_base_field_1 (tree t, tree basetype, tree *&next_field)
4212 /* Create the FIELD_DECL. */
4213 gcc_assert (CLASSTYPE_AS_BASE (basetype));
4214 tree decl = build_decl (input_location,
4215 FIELD_DECL, NULL_TREE, CLASSTYPE_AS_BASE (basetype));
4216 DECL_ARTIFICIAL (decl) = 1;
4217 DECL_IGNORED_P (decl) = 1;
4218 DECL_FIELD_CONTEXT (decl) = t;
4219 if (is_empty_class (basetype))
4220 /* CLASSTYPE_SIZE is one byte, but the field needs to have size zero. */
4221 DECL_SIZE (decl) = DECL_SIZE_UNIT (decl) = size_zero_node;
4222 else
4224 DECL_SIZE (decl) = CLASSTYPE_SIZE (basetype);
4225 DECL_SIZE_UNIT (decl) = CLASSTYPE_SIZE_UNIT (basetype);
4227 SET_DECL_ALIGN (decl, CLASSTYPE_ALIGN (basetype));
4228 DECL_USER_ALIGN (decl) = CLASSTYPE_USER_ALIGN (basetype);
4229 SET_DECL_MODE (decl, TYPE_MODE (basetype));
4230 DECL_FIELD_IS_BASE (decl) = 1;
4232 /* Add the new FIELD_DECL to the list of fields for T. */
4233 DECL_CHAIN (decl) = *next_field;
4234 *next_field = decl;
4235 next_field = &DECL_CHAIN (decl);
4237 return decl;
4240 /* Layout the base given by BINFO in the class indicated by RLI.
4241 *BASE_ALIGN is a running maximum of the alignments of
4242 any base class. OFFSETS gives the location of empty base
4243 subobjects. T is the most derived type. Return nonzero if the new
4244 object cannot be nearly-empty. A new FIELD_DECL is inserted at
4245 *NEXT_FIELD, unless BINFO is for an empty base class.
4247 Returns the location at which the next field should be inserted. */
4249 static tree *
4250 build_base_field (record_layout_info rli, tree binfo,
4251 splay_tree offsets, tree *next_field)
4253 tree t = rli->t;
4254 tree basetype = BINFO_TYPE (binfo);
4256 if (!COMPLETE_TYPE_P (basetype))
4257 /* This error is now reported in xref_tag, thus giving better
4258 location information. */
4259 return next_field;
4261 /* Place the base class. */
4262 if (!is_empty_class (basetype))
4264 tree decl;
4266 /* The containing class is non-empty because it has a non-empty
4267 base class. */
4268 CLASSTYPE_EMPTY_P (t) = 0;
4270 /* Create the FIELD_DECL. */
4271 decl = build_base_field_1 (t, basetype, next_field);
4273 /* Try to place the field. It may take more than one try if we
4274 have a hard time placing the field without putting two
4275 objects of the same type at the same address. */
4276 layout_nonempty_base_or_field (rli, decl, binfo, offsets);
4278 else
4280 tree eoc;
4281 bool atend;
4283 /* On some platforms (ARM), even empty classes will not be
4284 byte-aligned. */
4285 eoc = round_up_loc (input_location,
4286 rli_size_unit_so_far (rli),
4287 CLASSTYPE_ALIGN_UNIT (basetype));
4288 atend = layout_empty_base (rli, binfo, eoc, offsets);
4289 /* A nearly-empty class "has no proper base class that is empty,
4290 not morally virtual, and at an offset other than zero." */
4291 if (!BINFO_VIRTUAL_P (binfo) && CLASSTYPE_NEARLY_EMPTY_P (t))
4293 if (atend)
4294 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
4295 /* The check above (used in G++ 3.2) is insufficient because
4296 an empty class placed at offset zero might itself have an
4297 empty base at a nonzero offset. */
4298 else if (walk_subobject_offsets (basetype,
4299 empty_base_at_nonzero_offset_p,
4300 size_zero_node,
4301 /*offsets=*/NULL,
4302 /*max_offset=*/NULL_TREE,
4303 /*vbases_p=*/true))
4304 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
4307 /* We used to not create a FIELD_DECL for empty base classes because of
4308 back end issues with overlapping FIELD_DECLs, but that doesn't seem to
4309 be a problem anymore. We need them to handle initialization of C++17
4310 aggregate bases. */
4311 if (cxx_dialect >= cxx17 && !BINFO_VIRTUAL_P (binfo))
4313 tree decl = build_base_field_1 (t, basetype, next_field);
4314 DECL_FIELD_OFFSET (decl) = BINFO_OFFSET (binfo);
4315 DECL_FIELD_BIT_OFFSET (decl) = bitsize_zero_node;
4316 SET_DECL_OFFSET_ALIGN (decl, BITS_PER_UNIT);
4319 /* An empty virtual base causes a class to be non-empty
4320 -- but in that case we do not need to clear CLASSTYPE_EMPTY_P
4321 here because that was already done when the virtual table
4322 pointer was created. */
4325 /* Record the offsets of BINFO and its base subobjects. */
4326 record_subobject_offsets (binfo,
4327 BINFO_OFFSET (binfo),
4328 offsets,
4329 /*is_data_member=*/false);
4331 return next_field;
4334 /* Layout all of the non-virtual base classes. Record empty
4335 subobjects in OFFSETS. T is the most derived type. Return nonzero
4336 if the type cannot be nearly empty. The fields created
4337 corresponding to the base classes will be inserted at
4338 *NEXT_FIELD. */
4340 static void
4341 build_base_fields (record_layout_info rli,
4342 splay_tree offsets, tree *next_field)
4344 /* Chain to hold all the new FIELD_DECLs which stand in for base class
4345 subobjects. */
4346 tree t = rli->t;
4347 int n_baseclasses = BINFO_N_BASE_BINFOS (TYPE_BINFO (t));
4348 int i;
4350 /* The primary base class is always allocated first. */
4351 if (CLASSTYPE_HAS_PRIMARY_BASE_P (t))
4352 next_field = build_base_field (rli, CLASSTYPE_PRIMARY_BINFO (t),
4353 offsets, next_field);
4355 /* Now allocate the rest of the bases. */
4356 for (i = 0; i < n_baseclasses; ++i)
4358 tree base_binfo;
4360 base_binfo = BINFO_BASE_BINFO (TYPE_BINFO (t), i);
4362 /* The primary base was already allocated above, so we don't
4363 need to allocate it again here. */
4364 if (base_binfo == CLASSTYPE_PRIMARY_BINFO (t))
4365 continue;
4367 /* Virtual bases are added at the end (a primary virtual base
4368 will have already been added). */
4369 if (BINFO_VIRTUAL_P (base_binfo))
4370 continue;
4372 next_field = build_base_field (rli, base_binfo,
4373 offsets, next_field);
4377 /* Go through the TYPE_FIELDS of T issuing any appropriate
4378 diagnostics, figuring out which methods override which other
4379 methods, and so forth. */
4381 static void
4382 check_methods (tree t)
4384 for (tree x = TYPE_FIELDS (t); x; x = DECL_CHAIN (x))
4385 if (DECL_DECLARES_FUNCTION_P (x))
4387 check_for_override (x, t);
4389 if (DECL_PURE_VIRTUAL_P (x)
4390 && (TREE_CODE (x) != FUNCTION_DECL || ! DECL_VINDEX (x)))
4391 error ("initializer specified for non-virtual method %q+D", x);
4392 /* The name of the field is the original field name
4393 Save this in auxiliary field for later overloading. */
4394 if (TREE_CODE (x) == FUNCTION_DECL && DECL_VINDEX (x))
4396 TYPE_POLYMORPHIC_P (t) = 1;
4397 if (DECL_PURE_VIRTUAL_P (x))
4398 vec_safe_push (CLASSTYPE_PURE_VIRTUALS (t), x);
4401 /* All user-provided destructors are non-trivial.
4402 Constructors and assignment ops are handled in
4403 grok_special_member_properties. */
4404 if (DECL_DESTRUCTOR_P (x) && user_provided_p (x))
4405 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t) = 1;
4406 if (!DECL_VIRTUAL_P (x)
4407 && lookup_attribute ("transaction_safe_dynamic",
4408 DECL_ATTRIBUTES (x)))
4409 error_at (DECL_SOURCE_LOCATION (x),
4410 "%<transaction_safe_dynamic%> may only be specified for "
4411 "a virtual function");
4415 /* FN is a constructor or destructor. Clone the declaration to create
4416 a specialized in-charge or not-in-charge version, as indicated by
4417 NAME. */
4419 static tree
4420 build_clone (tree fn, tree name)
4422 tree parms;
4423 tree clone;
4425 /* Copy the function. */
4426 clone = copy_decl (fn);
4427 /* Reset the function name. */
4428 DECL_NAME (clone) = name;
4429 /* Remember where this function came from. */
4430 DECL_ABSTRACT_ORIGIN (clone) = fn;
4431 /* Make it easy to find the CLONE given the FN. */
4432 DECL_CHAIN (clone) = DECL_CHAIN (fn);
4433 DECL_CHAIN (fn) = clone;
4435 /* If this is a template, do the rest on the DECL_TEMPLATE_RESULT. */
4436 if (TREE_CODE (clone) == TEMPLATE_DECL)
4438 tree result = build_clone (DECL_TEMPLATE_RESULT (clone), name);
4439 DECL_TEMPLATE_RESULT (clone) = result;
4440 DECL_TEMPLATE_INFO (result) = copy_node (DECL_TEMPLATE_INFO (result));
4441 DECL_TI_TEMPLATE (result) = clone;
4442 TREE_TYPE (clone) = TREE_TYPE (result);
4443 return clone;
4445 else
4447 // Clone constraints.
4448 if (flag_concepts)
4449 if (tree ci = get_constraints (fn))
4450 set_constraints (clone, copy_node (ci));
4454 SET_DECL_ASSEMBLER_NAME (clone, NULL_TREE);
4455 DECL_CLONED_FUNCTION (clone) = fn;
4456 /* There's no pending inline data for this function. */
4457 DECL_PENDING_INLINE_INFO (clone) = NULL;
4458 DECL_PENDING_INLINE_P (clone) = 0;
4460 /* The base-class destructor is not virtual. */
4461 if (name == base_dtor_identifier)
4463 DECL_VIRTUAL_P (clone) = 0;
4464 if (TREE_CODE (clone) != TEMPLATE_DECL)
4465 DECL_VINDEX (clone) = NULL_TREE;
4468 bool ctor_omit_inherited_parms_p = ctor_omit_inherited_parms (clone);
4469 if (ctor_omit_inherited_parms_p)
4470 gcc_assert (DECL_HAS_IN_CHARGE_PARM_P (clone));
4472 /* If there was an in-charge parameter, drop it from the function
4473 type. */
4474 if (DECL_HAS_IN_CHARGE_PARM_P (clone))
4476 tree basetype = TYPE_METHOD_BASETYPE (TREE_TYPE (clone));
4477 tree parmtypes = TYPE_ARG_TYPES (TREE_TYPE (clone));
4478 /* Skip the `this' parameter. */
4479 parmtypes = TREE_CHAIN (parmtypes);
4480 /* Skip the in-charge parameter. */
4481 parmtypes = TREE_CHAIN (parmtypes);
4482 /* And the VTT parm, in a complete [cd]tor. */
4483 if (DECL_HAS_VTT_PARM_P (fn)
4484 && ! DECL_NEEDS_VTT_PARM_P (clone))
4485 parmtypes = TREE_CHAIN (parmtypes);
4486 if (ctor_omit_inherited_parms_p)
4488 /* If we're omitting inherited parms, that just leaves the VTT. */
4489 gcc_assert (DECL_NEEDS_VTT_PARM_P (clone));
4490 parmtypes = tree_cons (NULL_TREE, vtt_parm_type, void_list_node);
4492 TREE_TYPE (clone)
4493 = build_method_type_directly (basetype,
4494 TREE_TYPE (TREE_TYPE (clone)),
4495 parmtypes);
4496 TREE_TYPE (clone)
4497 = cp_build_type_attribute_variant (TREE_TYPE (clone),
4498 TYPE_ATTRIBUTES (TREE_TYPE (fn)));
4499 TREE_TYPE (clone)
4500 = cxx_copy_lang_qualifiers (TREE_TYPE (clone), TREE_TYPE (fn));
4503 /* Copy the function parameters. */
4504 DECL_ARGUMENTS (clone) = copy_list (DECL_ARGUMENTS (clone));
4505 /* Remove the in-charge parameter. */
4506 if (DECL_HAS_IN_CHARGE_PARM_P (clone))
4508 DECL_CHAIN (DECL_ARGUMENTS (clone))
4509 = DECL_CHAIN (DECL_CHAIN (DECL_ARGUMENTS (clone)));
4510 DECL_HAS_IN_CHARGE_PARM_P (clone) = 0;
4512 /* And the VTT parm, in a complete [cd]tor. */
4513 if (DECL_HAS_VTT_PARM_P (fn))
4515 if (DECL_NEEDS_VTT_PARM_P (clone))
4516 DECL_HAS_VTT_PARM_P (clone) = 1;
4517 else
4519 DECL_CHAIN (DECL_ARGUMENTS (clone))
4520 = DECL_CHAIN (DECL_CHAIN (DECL_ARGUMENTS (clone)));
4521 DECL_HAS_VTT_PARM_P (clone) = 0;
4525 /* A base constructor inheriting from a virtual base doesn't get the
4526 arguments. */
4527 if (ctor_omit_inherited_parms_p)
4528 DECL_CHAIN (DECL_CHAIN (DECL_ARGUMENTS (clone))) = NULL_TREE;
4530 for (parms = DECL_ARGUMENTS (clone); parms; parms = DECL_CHAIN (parms))
4532 DECL_CONTEXT (parms) = clone;
4533 cxx_dup_lang_specific_decl (parms);
4536 /* Create the RTL for this function. */
4537 SET_DECL_RTL (clone, NULL);
4538 rest_of_decl_compilation (clone, /*top_level=*/1, at_eof);
4540 return clone;
4543 /* Implementation of DECL_CLONED_FUNCTION and DECL_CLONED_FUNCTION_P, do
4544 not invoke this function directly.
4546 For a non-thunk function, returns the address of the slot for storing
4547 the function it is a clone of. Otherwise returns NULL_TREE.
4549 If JUST_TESTING, looks through TEMPLATE_DECL and returns NULL if
4550 cloned_function is unset. This is to support the separate
4551 DECL_CLONED_FUNCTION and DECL_CLONED_FUNCTION_P modes; using the latter
4552 on a template makes sense, but not the former. */
4554 tree *
4555 decl_cloned_function_p (const_tree decl, bool just_testing)
4557 tree *ptr;
4558 if (just_testing)
4559 decl = STRIP_TEMPLATE (decl);
4561 if (TREE_CODE (decl) != FUNCTION_DECL
4562 || !DECL_LANG_SPECIFIC (decl)
4563 || DECL_LANG_SPECIFIC (decl)->u.fn.thunk_p)
4565 #if defined ENABLE_TREE_CHECKING && (GCC_VERSION >= 2007)
4566 if (!just_testing)
4567 lang_check_failed (__FILE__, __LINE__, __FUNCTION__);
4568 else
4569 #endif
4570 return NULL;
4573 ptr = &DECL_LANG_SPECIFIC (decl)->u.fn.u5.cloned_function;
4574 if (just_testing && *ptr == NULL_TREE)
4575 return NULL;
4576 else
4577 return ptr;
4580 /* Produce declarations for all appropriate clones of FN. If
4581 UPDATE_METHODS is true, the clones are added to the
4582 CLASSTYPE_MEMBER_VEC. */
4584 void
4585 clone_function_decl (tree fn, bool update_methods)
4587 tree clone;
4589 /* Avoid inappropriate cloning. */
4590 if (DECL_CHAIN (fn)
4591 && DECL_CLONED_FUNCTION_P (DECL_CHAIN (fn)))
4592 return;
4594 if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (fn))
4596 /* For each constructor, we need two variants: an in-charge version
4597 and a not-in-charge version. */
4598 clone = build_clone (fn, complete_ctor_identifier);
4599 if (update_methods)
4600 add_method (DECL_CONTEXT (clone), clone, false);
4601 clone = build_clone (fn, base_ctor_identifier);
4602 if (update_methods)
4603 add_method (DECL_CONTEXT (clone), clone, false);
4605 else
4607 gcc_assert (DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (fn));
4609 /* For each destructor, we need three variants: an in-charge
4610 version, a not-in-charge version, and an in-charge deleting
4611 version. We clone the deleting version first because that
4612 means it will go second on the TYPE_FIELDS list -- and that
4613 corresponds to the correct layout order in the virtual
4614 function table.
4616 For a non-virtual destructor, we do not build a deleting
4617 destructor. */
4618 if (DECL_VIRTUAL_P (fn))
4620 clone = build_clone (fn, deleting_dtor_identifier);
4621 if (update_methods)
4622 add_method (DECL_CONTEXT (clone), clone, false);
4624 clone = build_clone (fn, complete_dtor_identifier);
4625 if (update_methods)
4626 add_method (DECL_CONTEXT (clone), clone, false);
4627 clone = build_clone (fn, base_dtor_identifier);
4628 if (update_methods)
4629 add_method (DECL_CONTEXT (clone), clone, false);
4632 /* Note that this is an abstract function that is never emitted. */
4633 DECL_ABSTRACT_P (fn) = true;
4636 /* DECL is an in charge constructor, which is being defined. This will
4637 have had an in class declaration, from whence clones were
4638 declared. An out-of-class definition can specify additional default
4639 arguments. As it is the clones that are involved in overload
4640 resolution, we must propagate the information from the DECL to its
4641 clones. */
4643 void
4644 adjust_clone_args (tree decl)
4646 tree clone;
4648 for (clone = DECL_CHAIN (decl); clone && DECL_CLONED_FUNCTION_P (clone);
4649 clone = DECL_CHAIN (clone))
4651 tree orig_clone_parms = TYPE_ARG_TYPES (TREE_TYPE (clone));
4652 tree orig_decl_parms = TYPE_ARG_TYPES (TREE_TYPE (decl));
4653 tree decl_parms, clone_parms;
4655 clone_parms = orig_clone_parms;
4657 /* Skip the 'this' parameter. */
4658 orig_clone_parms = TREE_CHAIN (orig_clone_parms);
4659 orig_decl_parms = TREE_CHAIN (orig_decl_parms);
4661 if (DECL_HAS_IN_CHARGE_PARM_P (decl))
4662 orig_decl_parms = TREE_CHAIN (orig_decl_parms);
4663 if (DECL_HAS_VTT_PARM_P (decl))
4664 orig_decl_parms = TREE_CHAIN (orig_decl_parms);
4666 clone_parms = orig_clone_parms;
4667 if (DECL_HAS_VTT_PARM_P (clone))
4668 clone_parms = TREE_CHAIN (clone_parms);
4670 for (decl_parms = orig_decl_parms; decl_parms;
4671 decl_parms = TREE_CHAIN (decl_parms),
4672 clone_parms = TREE_CHAIN (clone_parms))
4674 if (clone_parms == void_list_node)
4676 gcc_assert (decl_parms == clone_parms
4677 || ctor_omit_inherited_parms (clone));
4678 break;
4681 gcc_assert (same_type_p (TREE_TYPE (decl_parms),
4682 TREE_TYPE (clone_parms)));
4684 if (TREE_PURPOSE (decl_parms) && !TREE_PURPOSE (clone_parms))
4686 /* A default parameter has been added. Adjust the
4687 clone's parameters. */
4688 clone_parms = orig_decl_parms;
4690 if (DECL_HAS_VTT_PARM_P (clone))
4692 clone_parms = tree_cons (TREE_PURPOSE (orig_clone_parms),
4693 TREE_VALUE (orig_clone_parms),
4694 clone_parms);
4695 TREE_TYPE (clone_parms) = TREE_TYPE (orig_clone_parms);
4698 tree basetype = TYPE_METHOD_BASETYPE (TREE_TYPE (clone));
4699 tree type
4700 = build_method_type_directly (basetype,
4701 TREE_TYPE (TREE_TYPE (clone)),
4702 clone_parms);
4703 if (tree attrs = TYPE_ATTRIBUTES (TREE_TYPE (clone)))
4704 type = cp_build_type_attribute_variant (type, attrs);
4705 type = cxx_copy_lang_qualifiers (type, TREE_TYPE (clone));
4706 TREE_TYPE (clone) = type;
4708 clone_parms = NULL_TREE;
4709 break;
4712 gcc_assert (!clone_parms || clone_parms == void_list_node);
4716 /* For each of the constructors and destructors in T, create an
4717 in-charge and not-in-charge variant. */
4719 static void
4720 clone_constructors_and_destructors (tree t)
4722 /* While constructors can be via a using declaration, at this point
4723 we no longer need to know that. */
4724 for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (t)); iter; ++iter)
4725 clone_function_decl (*iter, /*update_methods=*/true);
4727 if (tree dtor = CLASSTYPE_DESTRUCTOR (t))
4728 clone_function_decl (dtor, /*update_methods=*/true);
4731 /* Deduce noexcept for a destructor DTOR. */
4733 void
4734 deduce_noexcept_on_destructor (tree dtor)
4736 if (!TYPE_RAISES_EXCEPTIONS (TREE_TYPE (dtor)))
4737 TREE_TYPE (dtor) = build_exception_variant (TREE_TYPE (dtor),
4738 noexcept_deferred_spec);
4741 /* Subroutine of set_one_vmethod_tm_attributes. Search base classes
4742 of TYPE for virtual functions which FNDECL overrides. Return a
4743 mask of the tm attributes found therein. */
4745 static int
4746 look_for_tm_attr_overrides (tree type, tree fndecl)
4748 tree binfo = TYPE_BINFO (type);
4749 tree base_binfo;
4750 int ix, found = 0;
4752 for (ix = 0; BINFO_BASE_ITERATE (binfo, ix, base_binfo); ++ix)
4754 tree o, basetype = BINFO_TYPE (base_binfo);
4756 if (!TYPE_POLYMORPHIC_P (basetype))
4757 continue;
4759 o = look_for_overrides_here (basetype, fndecl);
4760 if (o)
4762 if (lookup_attribute ("transaction_safe_dynamic",
4763 DECL_ATTRIBUTES (o)))
4764 /* transaction_safe_dynamic is not inherited. */;
4765 else
4766 found |= tm_attr_to_mask (find_tm_attribute
4767 (TYPE_ATTRIBUTES (TREE_TYPE (o))));
4769 else
4770 found |= look_for_tm_attr_overrides (basetype, fndecl);
4773 return found;
4776 /* Subroutine of set_method_tm_attributes. Handle the checks and
4777 inheritance for one virtual method FNDECL. */
4779 static void
4780 set_one_vmethod_tm_attributes (tree type, tree fndecl)
4782 tree tm_attr;
4783 int found, have;
4785 found = look_for_tm_attr_overrides (type, fndecl);
4787 /* If FNDECL doesn't actually override anything (i.e. T is the
4788 class that first declares FNDECL virtual), then we're done. */
4789 if (found == 0)
4790 return;
4792 tm_attr = find_tm_attribute (TYPE_ATTRIBUTES (TREE_TYPE (fndecl)));
4793 have = tm_attr_to_mask (tm_attr);
4795 /* Intel STM Language Extension 3.0, Section 4.2 table 4:
4796 tm_pure must match exactly, otherwise no weakening of
4797 tm_safe > tm_callable > nothing. */
4798 /* ??? The tm_pure attribute didn't make the transition to the
4799 multivendor language spec. */
4800 if (have == TM_ATTR_PURE)
4802 if (found != TM_ATTR_PURE)
4804 found &= -found;
4805 goto err_override;
4808 /* If the overridden function is tm_pure, then FNDECL must be. */
4809 else if (found == TM_ATTR_PURE && tm_attr)
4810 goto err_override;
4811 /* Look for base class combinations that cannot be satisfied. */
4812 else if (found != TM_ATTR_PURE && (found & TM_ATTR_PURE))
4814 found &= ~TM_ATTR_PURE;
4815 found &= -found;
4816 error_at (DECL_SOURCE_LOCATION (fndecl),
4817 "method overrides both %<transaction_pure%> and %qE methods",
4818 tm_mask_to_attr (found));
4820 /* If FNDECL did not declare an attribute, then inherit the most
4821 restrictive one. */
4822 else if (tm_attr == NULL)
4824 apply_tm_attr (fndecl, tm_mask_to_attr (least_bit_hwi (found)));
4826 /* Otherwise validate that we're not weaker than a function
4827 that is being overridden. */
4828 else
4830 found &= -found;
4831 if (found <= TM_ATTR_CALLABLE && have > found)
4832 goto err_override;
4834 return;
4836 err_override:
4837 error_at (DECL_SOURCE_LOCATION (fndecl),
4838 "method declared %qE overriding %qE method",
4839 tm_attr, tm_mask_to_attr (found));
4842 /* For each of the methods in T, propagate a class-level tm attribute. */
4844 static void
4845 set_method_tm_attributes (tree t)
4847 tree class_tm_attr, fndecl;
4849 /* Don't bother collecting tm attributes if transactional memory
4850 support is not enabled. */
4851 if (!flag_tm)
4852 return;
4854 /* Process virtual methods first, as they inherit directly from the
4855 base virtual function and also require validation of new attributes. */
4856 if (TYPE_CONTAINS_VPTR_P (t))
4858 tree vchain;
4859 for (vchain = BINFO_VIRTUALS (TYPE_BINFO (t)); vchain;
4860 vchain = TREE_CHAIN (vchain))
4862 fndecl = BV_FN (vchain);
4863 if (DECL_THUNK_P (fndecl))
4864 fndecl = THUNK_TARGET (fndecl);
4865 set_one_vmethod_tm_attributes (t, fndecl);
4869 /* If the class doesn't have an attribute, nothing more to do. */
4870 class_tm_attr = find_tm_attribute (TYPE_ATTRIBUTES (t));
4871 if (class_tm_attr == NULL)
4872 return;
4874 /* Any method that does not yet have a tm attribute inherits
4875 the one from the class. */
4876 for (fndecl = TYPE_FIELDS (t); fndecl; fndecl = DECL_CHAIN (fndecl))
4877 if (DECL_DECLARES_FUNCTION_P (fndecl)
4878 && !find_tm_attribute (TYPE_ATTRIBUTES (TREE_TYPE (fndecl))))
4879 apply_tm_attr (fndecl, class_tm_attr);
4882 /* Returns true if FN is a default constructor. */
4884 bool
4885 default_ctor_p (tree fn)
4887 return (DECL_CONSTRUCTOR_P (fn)
4888 && sufficient_parms_p (FUNCTION_FIRST_USER_PARMTYPE (fn)));
4891 /* Returns true iff class T has a user-provided constructor that can be called
4892 with more than zero arguments. */
4894 bool
4895 type_has_user_nondefault_constructor (tree t)
4897 if (!TYPE_HAS_USER_CONSTRUCTOR (t))
4898 return false;
4900 for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (t)); iter; ++iter)
4902 tree fn = *iter;
4903 if (user_provided_p (fn)
4904 && (TREE_CODE (fn) == TEMPLATE_DECL
4905 || (skip_artificial_parms_for (fn, DECL_ARGUMENTS (fn))
4906 != NULL_TREE)))
4907 return true;
4910 return false;
4913 /* Returns the defaulted constructor if T has one. Otherwise, returns
4914 NULL_TREE. */
4916 tree
4917 in_class_defaulted_default_constructor (tree t)
4919 if (!TYPE_HAS_USER_CONSTRUCTOR (t))
4920 return NULL_TREE;
4922 for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (t)); iter; ++iter)
4924 tree fn = *iter;
4926 if (DECL_DEFAULTED_IN_CLASS_P (fn)
4927 && default_ctor_p (fn))
4928 return fn;
4931 return NULL_TREE;
4934 /* Returns true iff FN is a user-provided function, i.e. user-declared
4935 and not defaulted at its first declaration. */
4937 bool
4938 user_provided_p (tree fn)
4940 if (TREE_CODE (fn) == TEMPLATE_DECL)
4941 return true;
4942 else
4943 return (!DECL_ARTIFICIAL (fn)
4944 && !(DECL_INITIALIZED_IN_CLASS_P (fn)
4945 && (DECL_DEFAULTED_FN (fn) || DECL_DELETED_FN (fn))));
4948 /* Returns true iff class T has a user-provided constructor. */
4950 bool
4951 type_has_user_provided_constructor (tree t)
4953 if (!CLASS_TYPE_P (t))
4954 return false;
4956 if (!TYPE_HAS_USER_CONSTRUCTOR (t))
4957 return false;
4959 for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (t)); iter; ++iter)
4960 if (user_provided_p (*iter))
4961 return true;
4963 return false;
4966 /* Returns true iff class T has a user-provided or explicit constructor. */
4968 bool
4969 type_has_user_provided_or_explicit_constructor (tree t)
4971 if (!CLASS_TYPE_P (t))
4972 return false;
4974 if (!TYPE_HAS_USER_CONSTRUCTOR (t))
4975 return false;
4977 for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (t)); iter; ++iter)
4979 tree fn = *iter;
4980 if (user_provided_p (fn) || DECL_NONCONVERTING_P (fn))
4981 return true;
4984 return false;
4987 /* Returns true iff class T has a non-user-provided (i.e. implicitly
4988 declared or explicitly defaulted in the class body) default
4989 constructor. */
4991 bool
4992 type_has_non_user_provided_default_constructor (tree t)
4994 if (!TYPE_HAS_DEFAULT_CONSTRUCTOR (t))
4995 return false;
4996 if (CLASSTYPE_LAZY_DEFAULT_CTOR (t))
4997 return true;
4999 for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (t)); iter; ++iter)
5001 tree fn = *iter;
5002 if (TREE_CODE (fn) == FUNCTION_DECL
5003 && default_ctor_p (fn)
5004 && !user_provided_p (fn))
5005 return true;
5008 return false;
5011 /* TYPE is being used as a virtual base, and has a non-trivial move
5012 assignment. Return true if this is due to there being a user-provided
5013 move assignment in TYPE or one of its subobjects; if there isn't, then
5014 multiple move assignment can't cause any harm. */
5016 bool
5017 vbase_has_user_provided_move_assign (tree type)
5019 /* Does the type itself have a user-provided move assignment operator? */
5020 if (!CLASSTYPE_LAZY_MOVE_ASSIGN (type))
5021 for (ovl_iterator iter (get_class_binding_direct
5022 (type, assign_op_identifier));
5023 iter; ++iter)
5024 if (user_provided_p (*iter) && move_fn_p (*iter))
5025 return true;
5027 /* Do any of its bases? */
5028 tree binfo = TYPE_BINFO (type);
5029 tree base_binfo;
5030 for (int i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
5031 if (vbase_has_user_provided_move_assign (BINFO_TYPE (base_binfo)))
5032 return true;
5034 /* Or non-static data members? */
5035 for (tree field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
5037 if (TREE_CODE (field) == FIELD_DECL
5038 && CLASS_TYPE_P (TREE_TYPE (field))
5039 && vbase_has_user_provided_move_assign (TREE_TYPE (field)))
5040 return true;
5043 /* Seems not. */
5044 return false;
5047 /* If default-initialization leaves part of TYPE uninitialized, returns
5048 a DECL for the field or TYPE itself (DR 253). */
5050 tree
5051 default_init_uninitialized_part (tree type)
5053 tree t, r, binfo;
5054 int i;
5056 type = strip_array_types (type);
5057 if (!CLASS_TYPE_P (type))
5058 return type;
5059 if (!type_has_non_user_provided_default_constructor (type))
5060 return NULL_TREE;
5061 for (binfo = TYPE_BINFO (type), i = 0;
5062 BINFO_BASE_ITERATE (binfo, i, t); ++i)
5064 r = default_init_uninitialized_part (BINFO_TYPE (t));
5065 if (r)
5066 return r;
5068 for (t = TYPE_FIELDS (type); t; t = DECL_CHAIN (t))
5069 if (TREE_CODE (t) == FIELD_DECL
5070 && !DECL_ARTIFICIAL (t)
5071 && !DECL_INITIAL (t))
5073 r = default_init_uninitialized_part (TREE_TYPE (t));
5074 if (r)
5075 return DECL_P (r) ? r : t;
5078 return NULL_TREE;
5081 /* Returns true iff for class T, a trivial synthesized default constructor
5082 would be constexpr. */
5084 bool
5085 trivial_default_constructor_is_constexpr (tree t)
5087 /* A defaulted trivial default constructor is constexpr
5088 if there is nothing to initialize. */
5089 gcc_assert (!TYPE_HAS_COMPLEX_DFLT (t));
5090 return is_really_empty_class (t);
5093 /* Returns true iff class T has a constexpr default constructor. */
5095 bool
5096 type_has_constexpr_default_constructor (tree t)
5098 tree fns;
5100 if (!CLASS_TYPE_P (t))
5102 /* The caller should have stripped an enclosing array. */
5103 gcc_assert (TREE_CODE (t) != ARRAY_TYPE);
5104 return false;
5106 if (CLASSTYPE_LAZY_DEFAULT_CTOR (t))
5108 if (!TYPE_HAS_COMPLEX_DFLT (t))
5109 return trivial_default_constructor_is_constexpr (t);
5110 /* Non-trivial, we need to check subobject constructors. */
5111 lazily_declare_fn (sfk_constructor, t);
5113 fns = locate_ctor (t);
5114 return (fns && DECL_DECLARED_CONSTEXPR_P (fns));
5117 /* Returns true iff class T has a constexpr default constructor or has an
5118 implicitly declared default constructor that we can't tell if it's constexpr
5119 without forcing a lazy declaration (which might cause undesired
5120 instantiations). */
5122 bool
5123 type_maybe_constexpr_default_constructor (tree t)
5125 if (CLASS_TYPE_P (t) && CLASSTYPE_LAZY_DEFAULT_CTOR (t)
5126 && TYPE_HAS_COMPLEX_DFLT (t))
5127 /* Assume it's constexpr. */
5128 return true;
5129 return type_has_constexpr_default_constructor (t);
5132 /* Returns true iff class TYPE has a virtual destructor. */
5134 bool
5135 type_has_virtual_destructor (tree type)
5137 tree dtor;
5139 if (!CLASS_TYPE_P (type))
5140 return false;
5142 gcc_assert (COMPLETE_TYPE_P (type));
5143 dtor = CLASSTYPE_DESTRUCTOR (type);
5144 return (dtor && DECL_VIRTUAL_P (dtor));
5147 /* Returns true iff T, a class, has a move-assignment or
5148 move-constructor. Does not lazily declare either.
5149 If USER_P is false, any move function will do. If it is true, the
5150 move function must be user-declared.
5152 Note that user-declared here is different from "user-provided",
5153 which doesn't include functions that are defaulted in the
5154 class. */
5156 bool
5157 classtype_has_move_assign_or_move_ctor_p (tree t, bool user_p)
5159 gcc_assert (user_p
5160 || (!CLASSTYPE_LAZY_MOVE_CTOR (t)
5161 && !CLASSTYPE_LAZY_MOVE_ASSIGN (t)));
5163 if (!CLASSTYPE_LAZY_MOVE_CTOR (t))
5164 for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (t)); iter; ++iter)
5165 if ((!user_p || !DECL_ARTIFICIAL (*iter)) && move_fn_p (*iter))
5166 return true;
5168 if (!CLASSTYPE_LAZY_MOVE_ASSIGN (t))
5169 for (ovl_iterator iter (get_class_binding_direct
5170 (t, assign_op_identifier));
5171 iter; ++iter)
5172 if ((!user_p || !DECL_ARTIFICIAL (*iter)) && move_fn_p (*iter))
5173 return true;
5175 return false;
5178 /* True iff T has a move constructor that is not deleted. */
5180 bool
5181 classtype_has_non_deleted_move_ctor (tree t)
5183 if (CLASSTYPE_LAZY_MOVE_CTOR (t))
5184 lazily_declare_fn (sfk_move_constructor, t);
5185 for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (t)); iter; ++iter)
5186 if (move_fn_p (*iter) && !DECL_DELETED_FN (*iter))
5187 return true;
5188 return false;
5191 /* If T, a class, has a user-provided copy constructor, copy assignment
5192 operator, or destructor, returns that function. Otherwise, null. */
5194 tree
5195 classtype_has_user_copy_or_dtor (tree t)
5197 if (!CLASSTYPE_LAZY_COPY_CTOR (t))
5198 for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (t)); iter; ++iter)
5200 tree fn = *iter;
5201 if (user_provided_p (fn) && copy_fn_p (fn))
5202 return fn;
5205 if (!CLASSTYPE_LAZY_COPY_ASSIGN (t))
5206 for (ovl_iterator iter (get_class_binding_direct
5207 (t, assign_op_identifier));
5208 iter; ++iter)
5210 tree fn = *iter;
5211 if (user_provided_p (fn) && copy_fn_p (fn))
5212 return fn;
5215 if (!CLASSTYPE_LAZY_DESTRUCTOR (t))
5217 tree fn = CLASSTYPE_DESTRUCTOR (t);
5218 if (user_provided_p (fn))
5219 return fn;
5222 return NULL_TREE;
5225 /* Nonzero if we need to build up a constructor call when initializing an
5226 object of this class, either because it has a user-declared constructor
5227 or because it doesn't have a default constructor (so we need to give an
5228 error if no initializer is provided). Use TYPE_NEEDS_CONSTRUCTING when
5229 what you care about is whether or not an object can be produced by a
5230 constructor (e.g. so we don't set TREE_READONLY on const variables of
5231 such type); use this function when what you care about is whether or not
5232 to try to call a constructor to create an object. The latter case is
5233 the former plus some cases of constructors that cannot be called. */
5235 bool
5236 type_build_ctor_call (tree t)
5238 tree inner;
5239 if (TYPE_NEEDS_CONSTRUCTING (t))
5240 return true;
5241 inner = strip_array_types (t);
5242 if (!CLASS_TYPE_P (inner) || ANON_AGGR_TYPE_P (inner))
5243 return false;
5244 if (!TYPE_HAS_DEFAULT_CONSTRUCTOR (inner))
5245 return true;
5246 if (cxx_dialect < cxx11)
5247 return false;
5248 /* A user-declared constructor might be private, and a constructor might
5249 be trivial but deleted. */
5250 for (ovl_iterator iter (get_class_binding (inner, complete_ctor_identifier));
5251 iter; ++iter)
5253 tree fn = *iter;
5254 if (!DECL_ARTIFICIAL (fn)
5255 || TREE_DEPRECATED (fn)
5256 || DECL_DELETED_FN (fn))
5257 return true;
5259 return false;
5262 /* Like type_build_ctor_call, but for destructors. */
5264 bool
5265 type_build_dtor_call (tree t)
5267 tree inner;
5268 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t))
5269 return true;
5270 inner = strip_array_types (t);
5271 if (!CLASS_TYPE_P (inner) || ANON_AGGR_TYPE_P (inner)
5272 || !COMPLETE_TYPE_P (inner))
5273 return false;
5274 if (cxx_dialect < cxx11)
5275 return false;
5276 /* A user-declared destructor might be private, and a destructor might
5277 be trivial but deleted. */
5278 for (ovl_iterator iter (get_class_binding (inner, complete_dtor_identifier));
5279 iter; ++iter)
5281 tree fn = *iter;
5282 if (!DECL_ARTIFICIAL (fn)
5283 || TREE_DEPRECATED (fn)
5284 || DECL_DELETED_FN (fn))
5285 return true;
5287 return false;
5290 /* Remove all zero-width bit-fields from T. */
5292 static void
5293 remove_zero_width_bit_fields (tree t)
5295 tree *fieldsp;
5297 fieldsp = &TYPE_FIELDS (t);
5298 while (*fieldsp)
5300 if (TREE_CODE (*fieldsp) == FIELD_DECL
5301 && DECL_C_BIT_FIELD (*fieldsp)
5302 /* We should not be confused by the fact that grokbitfield
5303 temporarily sets the width of the bit field into
5304 DECL_BIT_FIELD_REPRESENTATIVE (*fieldsp).
5305 check_bitfield_decl eventually sets DECL_SIZE (*fieldsp)
5306 to that width. */
5307 && (DECL_SIZE (*fieldsp) == NULL_TREE
5308 || integer_zerop (DECL_SIZE (*fieldsp))))
5309 *fieldsp = DECL_CHAIN (*fieldsp);
5310 else
5311 fieldsp = &DECL_CHAIN (*fieldsp);
5315 /* Returns TRUE iff we need a cookie when dynamically allocating an
5316 array whose elements have the indicated class TYPE. */
5318 static bool
5319 type_requires_array_cookie (tree type)
5321 tree fns;
5322 bool has_two_argument_delete_p = false;
5324 gcc_assert (CLASS_TYPE_P (type));
5326 /* If there's a non-trivial destructor, we need a cookie. In order
5327 to iterate through the array calling the destructor for each
5328 element, we'll have to know how many elements there are. */
5329 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
5330 return true;
5332 /* If the usual deallocation function is a two-argument whose second
5333 argument is of type `size_t', then we have to pass the size of
5334 the array to the deallocation function, so we will need to store
5335 a cookie. */
5336 fns = lookup_fnfields (TYPE_BINFO (type),
5337 ovl_op_identifier (false, VEC_DELETE_EXPR),
5338 /*protect=*/0);
5339 /* If there are no `operator []' members, or the lookup is
5340 ambiguous, then we don't need a cookie. */
5341 if (!fns || fns == error_mark_node)
5342 return false;
5343 /* Loop through all of the functions. */
5344 for (lkp_iterator iter (BASELINK_FUNCTIONS (fns)); iter; ++iter)
5346 tree fn = *iter;
5348 /* See if this function is a one-argument delete function. If
5349 it is, then it will be the usual deallocation function. */
5350 tree second_parm = TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (fn)));
5351 if (second_parm == void_list_node)
5352 return false;
5353 /* Do not consider this function if its second argument is an
5354 ellipsis. */
5355 if (!second_parm)
5356 continue;
5357 /* Otherwise, if we have a two-argument function and the second
5358 argument is `size_t', it will be the usual deallocation
5359 function -- unless there is one-argument function, too. */
5360 if (TREE_CHAIN (second_parm) == void_list_node
5361 && same_type_p (TREE_VALUE (second_parm), size_type_node))
5362 has_two_argument_delete_p = true;
5365 return has_two_argument_delete_p;
5368 /* Finish computing the `literal type' property of class type T.
5370 At this point, we have already processed base classes and
5371 non-static data members. We need to check whether the copy
5372 constructor is trivial, the destructor is trivial, and there
5373 is a trivial default constructor or at least one constexpr
5374 constructor other than the copy constructor. */
5376 static void
5377 finalize_literal_type_property (tree t)
5379 tree fn;
5381 if (cxx_dialect < cxx11
5382 || TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t))
5383 CLASSTYPE_LITERAL_P (t) = false;
5384 else if (CLASSTYPE_LITERAL_P (t) && LAMBDA_TYPE_P (t))
5385 CLASSTYPE_LITERAL_P (t) = (cxx_dialect >= cxx17);
5386 else if (CLASSTYPE_LITERAL_P (t) && !TYPE_HAS_TRIVIAL_DFLT (t)
5387 && CLASSTYPE_NON_AGGREGATE (t)
5388 && !TYPE_HAS_CONSTEXPR_CTOR (t))
5389 CLASSTYPE_LITERAL_P (t) = false;
5391 /* C++14 DR 1684 removed this restriction. */
5392 if (cxx_dialect < cxx14
5393 && !CLASSTYPE_LITERAL_P (t) && !LAMBDA_TYPE_P (t))
5394 for (fn = TYPE_FIELDS (t); fn; fn = DECL_CHAIN (fn))
5395 if (TREE_CODE (fn) == FUNCTION_DECL
5396 && DECL_DECLARED_CONSTEXPR_P (fn)
5397 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
5398 && !DECL_CONSTRUCTOR_P (fn))
5400 DECL_DECLARED_CONSTEXPR_P (fn) = false;
5401 if (!DECL_GENERATED_P (fn))
5403 auto_diagnostic_group d;
5404 if (pedwarn (DECL_SOURCE_LOCATION (fn), OPT_Wpedantic,
5405 "enclosing class of %<constexpr%> non-static "
5406 "member function %q+#D is not a literal type", fn))
5407 explain_non_literal_class (t);
5412 /* T is a non-literal type used in a context which requires a constant
5413 expression. Explain why it isn't literal. */
5415 void
5416 explain_non_literal_class (tree t)
5418 static hash_set<tree> *diagnosed;
5420 if (!CLASS_TYPE_P (t))
5421 return;
5422 t = TYPE_MAIN_VARIANT (t);
5424 if (diagnosed == NULL)
5425 diagnosed = new hash_set<tree>;
5426 if (diagnosed->add (t))
5427 /* Already explained. */
5428 return;
5430 auto_diagnostic_group d;
5431 inform (UNKNOWN_LOCATION, "%q+T is not literal because:", t);
5432 if (cxx_dialect < cxx17 && LAMBDA_TYPE_P (t))
5433 inform (UNKNOWN_LOCATION,
5434 " %qT is a closure type, which is only literal in "
5435 "C++17 and later", t);
5436 else if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t))
5437 inform (UNKNOWN_LOCATION, " %q+T has a non-trivial destructor", t);
5438 else if (CLASSTYPE_NON_AGGREGATE (t)
5439 && !TYPE_HAS_TRIVIAL_DFLT (t)
5440 && !LAMBDA_TYPE_P (t)
5441 && !TYPE_HAS_CONSTEXPR_CTOR (t))
5443 inform (UNKNOWN_LOCATION,
5444 " %q+T is not an aggregate, does not have a trivial "
5445 "default constructor, and has no %<constexpr%> constructor that "
5446 "is not a copy or move constructor", t);
5447 if (type_has_non_user_provided_default_constructor (t))
5448 /* Note that we can't simply call locate_ctor because when the
5449 constructor is deleted it just returns NULL_TREE. */
5450 for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (t)); iter; ++iter)
5452 tree fn = *iter;
5453 tree parms = TYPE_ARG_TYPES (TREE_TYPE (fn));
5455 parms = skip_artificial_parms_for (fn, parms);
5457 if (sufficient_parms_p (parms))
5459 if (DECL_DELETED_FN (fn))
5460 maybe_explain_implicit_delete (fn);
5461 else
5462 explain_invalid_constexpr_fn (fn);
5463 break;
5467 else
5469 tree binfo, base_binfo, field; int i;
5470 for (binfo = TYPE_BINFO (t), i = 0;
5471 BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
5473 tree basetype = TREE_TYPE (base_binfo);
5474 if (!CLASSTYPE_LITERAL_P (basetype))
5476 inform (UNKNOWN_LOCATION,
5477 " base class %qT of %q+T is non-literal",
5478 basetype, t);
5479 explain_non_literal_class (basetype);
5480 return;
5483 for (field = TYPE_FIELDS (t); field; field = TREE_CHAIN (field))
5485 tree ftype;
5486 if (TREE_CODE (field) != FIELD_DECL)
5487 continue;
5488 ftype = TREE_TYPE (field);
5489 if (!literal_type_p (ftype))
5491 inform (DECL_SOURCE_LOCATION (field),
5492 " non-static data member %qD has non-literal type",
5493 field);
5494 if (CLASS_TYPE_P (ftype))
5495 explain_non_literal_class (ftype);
5497 if (CP_TYPE_VOLATILE_P (ftype))
5498 inform (DECL_SOURCE_LOCATION (field),
5499 " non-static data member %qD has volatile type", field);
5504 /* Check the validity of the bases and members declared in T. Add any
5505 implicitly-generated functions (like copy-constructors and
5506 assignment operators). Compute various flag bits (like
5507 CLASSTYPE_NON_LAYOUT_POD_T) for T. This routine works purely at the C++
5508 level: i.e., independently of the ABI in use. */
5510 static void
5511 check_bases_and_members (tree t)
5513 /* Nonzero if the implicitly generated copy constructor should take
5514 a non-const reference argument. */
5515 int cant_have_const_ctor;
5516 /* Nonzero if the implicitly generated assignment operator
5517 should take a non-const reference argument. */
5518 int no_const_asn_ref;
5519 tree access_decls;
5520 bool saved_complex_asn_ref;
5521 bool saved_nontrivial_dtor;
5522 tree fn;
5524 /* By default, we use const reference arguments and generate default
5525 constructors. */
5526 cant_have_const_ctor = 0;
5527 no_const_asn_ref = 0;
5529 /* Check all the base-classes and set FMEM members to point to arrays
5530 of potential interest. */
5531 check_bases (t, &cant_have_const_ctor, &no_const_asn_ref);
5533 /* Deduce noexcept on destructor. This needs to happen after we've set
5534 triviality flags appropriately for our bases. */
5535 if (cxx_dialect >= cxx11)
5536 if (tree dtor = CLASSTYPE_DESTRUCTOR (t))
5537 deduce_noexcept_on_destructor (dtor);
5539 /* Check all the method declarations. */
5540 check_methods (t);
5542 /* Save the initial values of these flags which only indicate whether
5543 or not the class has user-provided functions. As we analyze the
5544 bases and members we can set these flags for other reasons. */
5545 saved_complex_asn_ref = TYPE_HAS_COMPLEX_COPY_ASSIGN (t);
5546 saved_nontrivial_dtor = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t);
5548 /* Check all the data member declarations. We cannot call
5549 check_field_decls until we have called check_bases check_methods,
5550 as check_field_decls depends on TYPE_HAS_NONTRIVIAL_DESTRUCTOR
5551 being set appropriately. */
5552 check_field_decls (t, &access_decls,
5553 &cant_have_const_ctor,
5554 &no_const_asn_ref);
5556 /* A nearly-empty class has to be vptr-containing; a nearly empty
5557 class contains just a vptr. */
5558 if (!TYPE_CONTAINS_VPTR_P (t))
5559 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
5561 /* Do some bookkeeping that will guide the generation of implicitly
5562 declared member functions. */
5563 TYPE_HAS_COMPLEX_COPY_CTOR (t) |= TYPE_CONTAINS_VPTR_P (t);
5564 TYPE_HAS_COMPLEX_MOVE_CTOR (t) |= TYPE_CONTAINS_VPTR_P (t);
5565 /* We need to call a constructor for this class if it has a
5566 user-provided constructor, or if the default constructor is going
5567 to initialize the vptr. (This is not an if-and-only-if;
5568 TYPE_NEEDS_CONSTRUCTING is set elsewhere if bases or members
5569 themselves need constructing.) */
5570 TYPE_NEEDS_CONSTRUCTING (t)
5571 |= (type_has_user_provided_constructor (t) || TYPE_CONTAINS_VPTR_P (t));
5572 /* [dcl.init.aggr]
5574 An aggregate is an array or a class with no user-provided
5575 constructors ... and no virtual functions.
5577 Again, other conditions for being an aggregate are checked
5578 elsewhere. */
5579 CLASSTYPE_NON_AGGREGATE (t)
5580 |= ((cxx_dialect < cxx2a
5581 ? type_has_user_provided_or_explicit_constructor (t)
5582 : TYPE_HAS_USER_CONSTRUCTOR (t))
5583 || TYPE_POLYMORPHIC_P (t));
5584 /* This is the C++98/03 definition of POD; it changed in C++0x, but we
5585 retain the old definition internally for ABI reasons. */
5586 CLASSTYPE_NON_LAYOUT_POD_P (t)
5587 |= (CLASSTYPE_NON_AGGREGATE (t)
5588 || saved_nontrivial_dtor || saved_complex_asn_ref);
5589 CLASSTYPE_NON_STD_LAYOUT (t) |= TYPE_CONTAINS_VPTR_P (t);
5590 TYPE_HAS_COMPLEX_COPY_ASSIGN (t) |= TYPE_CONTAINS_VPTR_P (t);
5591 TYPE_HAS_COMPLEX_MOVE_ASSIGN (t) |= TYPE_CONTAINS_VPTR_P (t);
5592 TYPE_HAS_COMPLEX_DFLT (t) |= TYPE_CONTAINS_VPTR_P (t);
5594 /* If the only explicitly declared default constructor is user-provided,
5595 set TYPE_HAS_COMPLEX_DFLT. */
5596 if (!TYPE_HAS_COMPLEX_DFLT (t)
5597 && TYPE_HAS_DEFAULT_CONSTRUCTOR (t)
5598 && !type_has_non_user_provided_default_constructor (t))
5599 TYPE_HAS_COMPLEX_DFLT (t) = true;
5601 /* Warn if a public base of a polymorphic type has an accessible
5602 non-virtual destructor. It is only now that we know the class is
5603 polymorphic. Although a polymorphic base will have a already
5604 been diagnosed during its definition, we warn on use too. */
5605 if (TYPE_POLYMORPHIC_P (t) && warn_nonvdtor)
5607 tree binfo = TYPE_BINFO (t);
5608 vec<tree, va_gc> *accesses = BINFO_BASE_ACCESSES (binfo);
5609 tree base_binfo;
5610 unsigned i;
5612 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
5614 tree basetype = TREE_TYPE (base_binfo);
5616 if ((*accesses)[i] == access_public_node
5617 && (TYPE_POLYMORPHIC_P (basetype) || warn_ecpp)
5618 && accessible_nvdtor_p (basetype))
5619 warning (OPT_Wnon_virtual_dtor,
5620 "base class %q#T has accessible non-virtual destructor",
5621 basetype);
5625 /* If the class has no user-declared constructor, but does have
5626 non-static const or reference data members that can never be
5627 initialized, issue a warning. */
5628 if (warn_uninitialized
5629 /* Classes with user-declared constructors are presumed to
5630 initialize these members. */
5631 && !TYPE_HAS_USER_CONSTRUCTOR (t)
5632 /* Aggregates can be initialized with brace-enclosed
5633 initializers. */
5634 && CLASSTYPE_NON_AGGREGATE (t))
5636 tree field;
5638 for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
5640 tree type;
5642 if (TREE_CODE (field) != FIELD_DECL
5643 || DECL_INITIAL (field) != NULL_TREE)
5644 continue;
5646 type = TREE_TYPE (field);
5647 if (TYPE_REF_P (type))
5648 warning_at (DECL_SOURCE_LOCATION (field),
5649 OPT_Wuninitialized, "non-static reference %q#D "
5650 "in class without a constructor", field);
5651 else if (CP_TYPE_CONST_P (type)
5652 && (!CLASS_TYPE_P (type)
5653 || !TYPE_HAS_DEFAULT_CONSTRUCTOR (type)))
5654 warning_at (DECL_SOURCE_LOCATION (field),
5655 OPT_Wuninitialized, "non-static const member %q#D "
5656 "in class without a constructor", field);
5660 /* Synthesize any needed methods. */
5661 add_implicitly_declared_members (t, &access_decls,
5662 cant_have_const_ctor,
5663 no_const_asn_ref);
5665 /* Check defaulted declarations here so we have cant_have_const_ctor
5666 and don't need to worry about clones. */
5667 for (fn = TYPE_FIELDS (t); fn; fn = DECL_CHAIN (fn))
5668 if (DECL_DECLARES_FUNCTION_P (fn)
5669 && !DECL_ARTIFICIAL (fn)
5670 && DECL_DEFAULTED_IN_CLASS_P (fn))
5672 int copy = copy_fn_p (fn);
5673 if (copy > 0)
5675 bool imp_const_p
5676 = (DECL_CONSTRUCTOR_P (fn) ? !cant_have_const_ctor
5677 : !no_const_asn_ref);
5678 bool fn_const_p = (copy == 2);
5680 if (fn_const_p && !imp_const_p)
5681 /* If the function is defaulted outside the class, we just
5682 give the synthesis error. Core Issue #1331 says this is
5683 no longer ill-formed, it is defined as deleted instead. */
5684 DECL_DELETED_FN (fn) = true;
5686 defaulted_late_check (fn);
5689 if (LAMBDA_TYPE_P (t))
5691 /* "This class type is not an aggregate." */
5692 CLASSTYPE_NON_AGGREGATE (t) = 1;
5695 /* Compute the 'literal type' property before we
5696 do anything with non-static member functions. */
5697 finalize_literal_type_property (t);
5699 /* Create the in-charge and not-in-charge variants of constructors
5700 and destructors. */
5701 clone_constructors_and_destructors (t);
5703 /* Process the using-declarations. */
5704 for (; access_decls; access_decls = TREE_CHAIN (access_decls))
5705 handle_using_decl (TREE_VALUE (access_decls), t);
5707 /* Figure out whether or not we will need a cookie when dynamically
5708 allocating an array of this type. */
5709 LANG_TYPE_CLASS_CHECK (t)->vec_new_uses_cookie
5710 = type_requires_array_cookie (t);
5713 /* If T needs a pointer to its virtual function table, set TYPE_VFIELD
5714 accordingly. If a new vfield was created (because T doesn't have a
5715 primary base class), then the newly created field is returned. It
5716 is not added to the TYPE_FIELDS list; it is the caller's
5717 responsibility to do that. Accumulate declared virtual functions
5718 on VIRTUALS_P. */
5720 static tree
5721 create_vtable_ptr (tree t, tree* virtuals_p)
5723 tree fn;
5725 /* Collect the virtual functions declared in T. */
5726 for (fn = TYPE_FIELDS (t); fn; fn = DECL_CHAIN (fn))
5727 if (TREE_CODE (fn) == FUNCTION_DECL
5728 && DECL_VINDEX (fn) && !DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (fn)
5729 && TREE_CODE (DECL_VINDEX (fn)) != INTEGER_CST)
5731 tree new_virtual = make_node (TREE_LIST);
5733 BV_FN (new_virtual) = fn;
5734 BV_DELTA (new_virtual) = integer_zero_node;
5735 BV_VCALL_INDEX (new_virtual) = NULL_TREE;
5737 TREE_CHAIN (new_virtual) = *virtuals_p;
5738 *virtuals_p = new_virtual;
5741 /* If we couldn't find an appropriate base class, create a new field
5742 here. Even if there weren't any new virtual functions, we might need a
5743 new virtual function table if we're supposed to include vptrs in
5744 all classes that need them. */
5745 if (!TYPE_VFIELD (t) && (*virtuals_p || TYPE_CONTAINS_VPTR_P (t)))
5747 /* We build this decl with vtbl_ptr_type_node, which is a
5748 `vtable_entry_type*'. It might seem more precise to use
5749 `vtable_entry_type (*)[N]' where N is the number of virtual
5750 functions. However, that would require the vtable pointer in
5751 base classes to have a different type than the vtable pointer
5752 in derived classes. We could make that happen, but that
5753 still wouldn't solve all the problems. In particular, the
5754 type-based alias analysis code would decide that assignments
5755 to the base class vtable pointer can't alias assignments to
5756 the derived class vtable pointer, since they have different
5757 types. Thus, in a derived class destructor, where the base
5758 class constructor was inlined, we could generate bad code for
5759 setting up the vtable pointer.
5761 Therefore, we use one type for all vtable pointers. We still
5762 use a type-correct type; it's just doesn't indicate the array
5763 bounds. That's better than using `void*' or some such; it's
5764 cleaner, and it let's the alias analysis code know that these
5765 stores cannot alias stores to void*! */
5766 tree field;
5768 field = build_decl (input_location,
5769 FIELD_DECL, get_vfield_name (t), vtbl_ptr_type_node);
5770 DECL_VIRTUAL_P (field) = 1;
5771 DECL_ARTIFICIAL (field) = 1;
5772 DECL_FIELD_CONTEXT (field) = t;
5773 DECL_FCONTEXT (field) = t;
5774 if (TYPE_PACKED (t))
5775 DECL_PACKED (field) = 1;
5777 TYPE_VFIELD (t) = field;
5779 /* This class is non-empty. */
5780 CLASSTYPE_EMPTY_P (t) = 0;
5782 return field;
5785 return NULL_TREE;
5788 /* Add OFFSET to all base types of BINFO which is a base in the
5789 hierarchy dominated by T.
5791 OFFSET, which is a type offset, is number of bytes. */
5793 static void
5794 propagate_binfo_offsets (tree binfo, tree offset)
5796 int i;
5797 tree primary_binfo;
5798 tree base_binfo;
5800 /* Update BINFO's offset. */
5801 BINFO_OFFSET (binfo)
5802 = fold_convert (sizetype,
5803 size_binop (PLUS_EXPR,
5804 fold_convert (ssizetype, BINFO_OFFSET (binfo)),
5805 offset));
5807 /* Find the primary base class. */
5808 primary_binfo = get_primary_binfo (binfo);
5810 if (primary_binfo && BINFO_INHERITANCE_CHAIN (primary_binfo) == binfo)
5811 propagate_binfo_offsets (primary_binfo, offset);
5813 /* Scan all of the bases, pushing the BINFO_OFFSET adjust
5814 downwards. */
5815 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
5817 /* Don't do the primary base twice. */
5818 if (base_binfo == primary_binfo)
5819 continue;
5821 if (BINFO_VIRTUAL_P (base_binfo))
5822 continue;
5824 propagate_binfo_offsets (base_binfo, offset);
5828 /* Set BINFO_OFFSET for all of the virtual bases for RLI->T. Update
5829 TYPE_ALIGN and TYPE_SIZE for T. OFFSETS gives the location of
5830 empty subobjects of T. */
5832 static void
5833 layout_virtual_bases (record_layout_info rli, splay_tree offsets)
5835 tree vbase;
5836 tree t = rli->t;
5837 tree *next_field;
5839 if (BINFO_N_BASE_BINFOS (TYPE_BINFO (t)) == 0)
5840 return;
5842 /* Find the last field. The artificial fields created for virtual
5843 bases will go after the last extant field to date. */
5844 next_field = &TYPE_FIELDS (t);
5845 while (*next_field)
5846 next_field = &DECL_CHAIN (*next_field);
5848 /* Go through the virtual bases, allocating space for each virtual
5849 base that is not already a primary base class. These are
5850 allocated in inheritance graph order. */
5851 for (vbase = TYPE_BINFO (t); vbase; vbase = TREE_CHAIN (vbase))
5853 if (!BINFO_VIRTUAL_P (vbase))
5854 continue;
5856 if (!BINFO_PRIMARY_P (vbase))
5858 /* This virtual base is not a primary base of any class in the
5859 hierarchy, so we have to add space for it. */
5860 next_field = build_base_field (rli, vbase,
5861 offsets, next_field);
5866 /* Returns the offset of the byte just past the end of the base class
5867 BINFO. */
5869 static tree
5870 end_of_base (tree binfo)
5872 tree size;
5874 if (!CLASSTYPE_AS_BASE (BINFO_TYPE (binfo)))
5875 size = TYPE_SIZE_UNIT (char_type_node);
5876 else if (is_empty_class (BINFO_TYPE (binfo)))
5877 /* An empty class has zero CLASSTYPE_SIZE_UNIT, but we need to
5878 allocate some space for it. It cannot have virtual bases, so
5879 TYPE_SIZE_UNIT is fine. */
5880 size = TYPE_SIZE_UNIT (BINFO_TYPE (binfo));
5881 else
5882 size = CLASSTYPE_SIZE_UNIT (BINFO_TYPE (binfo));
5884 return size_binop (PLUS_EXPR, BINFO_OFFSET (binfo), size);
5887 /* Returns the offset of the byte just past the end of the base class
5888 with the highest offset in T. If INCLUDE_VIRTUALS_P is zero, then
5889 only non-virtual bases are included. */
5891 static tree
5892 end_of_class (tree t, int include_virtuals_p)
5894 tree result = size_zero_node;
5895 vec<tree, va_gc> *vbases;
5896 tree binfo;
5897 tree base_binfo;
5898 tree offset;
5899 int i;
5901 for (binfo = TYPE_BINFO (t), i = 0;
5902 BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
5904 if (!include_virtuals_p
5905 && BINFO_VIRTUAL_P (base_binfo)
5906 && (!BINFO_PRIMARY_P (base_binfo)
5907 || BINFO_INHERITANCE_CHAIN (base_binfo) != TYPE_BINFO (t)))
5908 continue;
5910 offset = end_of_base (base_binfo);
5911 if (tree_int_cst_lt (result, offset))
5912 result = offset;
5915 if (include_virtuals_p)
5916 for (vbases = CLASSTYPE_VBASECLASSES (t), i = 0;
5917 vec_safe_iterate (vbases, i, &base_binfo); i++)
5919 offset = end_of_base (base_binfo);
5920 if (tree_int_cst_lt (result, offset))
5921 result = offset;
5924 return result;
5927 /* Warn about bases of T that are inaccessible because they are
5928 ambiguous. For example:
5930 struct S {};
5931 struct T : public S {};
5932 struct U : public S, public T {};
5934 Here, `(S*) new U' is not allowed because there are two `S'
5935 subobjects of U. */
5937 static void
5938 warn_about_ambiguous_bases (tree t)
5940 int i;
5941 vec<tree, va_gc> *vbases;
5942 tree basetype;
5943 tree binfo;
5944 tree base_binfo;
5946 /* If there are no repeated bases, nothing can be ambiguous. */
5947 if (!CLASSTYPE_REPEATED_BASE_P (t))
5948 return;
5950 /* Check direct bases. */
5951 for (binfo = TYPE_BINFO (t), i = 0;
5952 BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
5954 basetype = BINFO_TYPE (base_binfo);
5956 if (!uniquely_derived_from_p (basetype, t))
5957 warning (0, "direct base %qT inaccessible in %qT due to ambiguity",
5958 basetype, t);
5961 /* Check for ambiguous virtual bases. */
5962 if (extra_warnings)
5963 for (vbases = CLASSTYPE_VBASECLASSES (t), i = 0;
5964 vec_safe_iterate (vbases, i, &binfo); i++)
5966 basetype = BINFO_TYPE (binfo);
5968 if (!uniquely_derived_from_p (basetype, t))
5969 warning (OPT_Wextra, "virtual base %qT inaccessible in %qT due "
5970 "to ambiguity", basetype, t);
5974 /* Compare two INTEGER_CSTs K1 and K2. */
5976 static int
5977 splay_tree_compare_integer_csts (splay_tree_key k1, splay_tree_key k2)
5979 return tree_int_cst_compare ((tree) k1, (tree) k2);
5982 /* Increase the size indicated in RLI to account for empty classes
5983 that are "off the end" of the class. */
5985 static void
5986 include_empty_classes (record_layout_info rli)
5988 tree eoc;
5989 tree rli_size;
5991 /* It might be the case that we grew the class to allocate a
5992 zero-sized base class. That won't be reflected in RLI, yet,
5993 because we are willing to overlay multiple bases at the same
5994 offset. However, now we need to make sure that RLI is big enough
5995 to reflect the entire class. */
5996 eoc = end_of_class (rli->t, CLASSTYPE_AS_BASE (rli->t) != NULL_TREE);
5997 rli_size = rli_size_unit_so_far (rli);
5998 if (TREE_CODE (rli_size) == INTEGER_CST
5999 && tree_int_cst_lt (rli_size, eoc))
6001 /* The size should have been rounded to a whole byte. */
6002 gcc_assert (tree_int_cst_equal
6003 (rli->bitpos, round_down (rli->bitpos, BITS_PER_UNIT)));
6004 rli->bitpos
6005 = size_binop (PLUS_EXPR,
6006 rli->bitpos,
6007 size_binop (MULT_EXPR,
6008 fold_convert (bitsizetype,
6009 size_binop (MINUS_EXPR,
6010 eoc, rli_size)),
6011 bitsize_int (BITS_PER_UNIT)));
6012 normalize_rli (rli);
6016 /* Calculate the TYPE_SIZE, TYPE_ALIGN, etc for T. Calculate
6017 BINFO_OFFSETs for all of the base-classes. Position the vtable
6018 pointer. Accumulate declared virtual functions on VIRTUALS_P. */
6020 static void
6021 layout_class_type (tree t, tree *virtuals_p)
6023 tree non_static_data_members;
6024 tree field;
6025 tree vptr;
6026 record_layout_info rli;
6027 /* Maps offsets (represented as INTEGER_CSTs) to a TREE_LIST of
6028 types that appear at that offset. */
6029 splay_tree empty_base_offsets;
6030 /* True if the last field laid out was a bit-field. */
6031 bool last_field_was_bitfield = false;
6032 /* The location at which the next field should be inserted. */
6033 tree *next_field;
6035 /* Keep track of the first non-static data member. */
6036 non_static_data_members = TYPE_FIELDS (t);
6038 /* Start laying out the record. */
6039 rli = start_record_layout (t);
6041 /* Mark all the primary bases in the hierarchy. */
6042 determine_primary_bases (t);
6044 /* Create a pointer to our virtual function table. */
6045 vptr = create_vtable_ptr (t, virtuals_p);
6047 /* The vptr is always the first thing in the class. */
6048 if (vptr)
6050 DECL_CHAIN (vptr) = TYPE_FIELDS (t);
6051 TYPE_FIELDS (t) = vptr;
6052 next_field = &DECL_CHAIN (vptr);
6053 place_field (rli, vptr);
6055 else
6056 next_field = &TYPE_FIELDS (t);
6058 /* Build FIELD_DECLs for all of the non-virtual base-types. */
6059 empty_base_offsets = splay_tree_new (splay_tree_compare_integer_csts,
6060 NULL, NULL);
6061 build_base_fields (rli, empty_base_offsets, next_field);
6063 /* Layout the non-static data members. */
6064 for (field = non_static_data_members; field; field = DECL_CHAIN (field))
6066 tree type;
6067 tree padding;
6069 /* We still pass things that aren't non-static data members to
6070 the back end, in case it wants to do something with them. */
6071 if (TREE_CODE (field) != FIELD_DECL)
6073 place_field (rli, field);
6074 /* If the static data member has incomplete type, keep track
6075 of it so that it can be completed later. (The handling
6076 of pending statics in finish_record_layout is
6077 insufficient; consider:
6079 struct S1;
6080 struct S2 { static S1 s1; };
6082 At this point, finish_record_layout will be called, but
6083 S1 is still incomplete.) */
6084 if (VAR_P (field))
6086 maybe_register_incomplete_var (field);
6087 /* The visibility of static data members is determined
6088 at their point of declaration, not their point of
6089 definition. */
6090 determine_visibility (field);
6092 continue;
6095 type = TREE_TYPE (field);
6096 if (type == error_mark_node)
6097 continue;
6099 padding = NULL_TREE;
6101 /* If this field is a bit-field whose width is greater than its
6102 type, then there are some special rules for allocating
6103 it. */
6104 if (DECL_C_BIT_FIELD (field)
6105 && tree_int_cst_lt (TYPE_SIZE (type), DECL_SIZE (field)))
6107 bool was_unnamed_p = false;
6108 /* We must allocate the bits as if suitably aligned for the
6109 longest integer type that fits in this many bits. Then,
6110 we are supposed to use the left over bits as additional
6111 padding. */
6113 /* Do not pick a type bigger than MAX_FIXED_MODE_SIZE. */
6114 tree limit = size_int (MAX_FIXED_MODE_SIZE);
6115 if (tree_int_cst_lt (DECL_SIZE (field), limit))
6116 limit = DECL_SIZE (field);
6118 tree integer_type = integer_types[itk_char];
6119 for (unsigned itk = itk_char; itk != itk_none; itk++)
6120 if (tree next = integer_types[itk])
6122 if (tree_int_cst_lt (limit, TYPE_SIZE (next)))
6123 /* Too big, so our current guess is what we want. */
6124 break;
6125 /* Not bigger than limit, ok */
6126 integer_type = next;
6129 /* Figure out how much additional padding is required. */
6130 if (TREE_CODE (t) == UNION_TYPE)
6131 /* In a union, the padding field must have the full width
6132 of the bit-field; all fields start at offset zero. */
6133 padding = DECL_SIZE (field);
6134 else
6135 padding = size_binop (MINUS_EXPR, DECL_SIZE (field),
6136 TYPE_SIZE (integer_type));
6138 if (integer_zerop (padding))
6139 padding = NULL_TREE;
6141 /* An unnamed bitfield does not normally affect the
6142 alignment of the containing class on a target where
6143 PCC_BITFIELD_TYPE_MATTERS. But, the C++ ABI does not
6144 make any exceptions for unnamed bitfields when the
6145 bitfields are longer than their types. Therefore, we
6146 temporarily give the field a name. */
6147 if (PCC_BITFIELD_TYPE_MATTERS && !DECL_NAME (field))
6149 was_unnamed_p = true;
6150 DECL_NAME (field) = make_anon_name ();
6153 DECL_SIZE (field) = TYPE_SIZE (integer_type);
6154 SET_DECL_ALIGN (field, TYPE_ALIGN (integer_type));
6155 DECL_USER_ALIGN (field) = TYPE_USER_ALIGN (integer_type);
6156 layout_nonempty_base_or_field (rli, field, NULL_TREE,
6157 empty_base_offsets);
6158 if (was_unnamed_p)
6159 DECL_NAME (field) = NULL_TREE;
6160 /* Now that layout has been performed, set the size of the
6161 field to the size of its declared type; the rest of the
6162 field is effectively invisible. */
6163 DECL_SIZE (field) = TYPE_SIZE (type);
6164 /* We must also reset the DECL_MODE of the field. */
6165 SET_DECL_MODE (field, TYPE_MODE (type));
6167 else
6168 layout_nonempty_base_or_field (rli, field, NULL_TREE,
6169 empty_base_offsets);
6171 /* Remember the location of any empty classes in FIELD. */
6172 record_subobject_offsets (TREE_TYPE (field),
6173 byte_position(field),
6174 empty_base_offsets,
6175 /*is_data_member=*/true);
6177 /* If a bit-field does not immediately follow another bit-field,
6178 and yet it starts in the middle of a byte, we have failed to
6179 comply with the ABI. */
6180 if (warn_abi
6181 && DECL_C_BIT_FIELD (field)
6182 /* The TREE_NO_WARNING flag gets set by Objective-C when
6183 laying out an Objective-C class. The ObjC ABI differs
6184 from the C++ ABI, and so we do not want a warning
6185 here. */
6186 && !TREE_NO_WARNING (field)
6187 && !last_field_was_bitfield
6188 && !integer_zerop (size_binop (TRUNC_MOD_EXPR,
6189 DECL_FIELD_BIT_OFFSET (field),
6190 bitsize_unit_node)))
6191 warning_at (DECL_SOURCE_LOCATION (field), OPT_Wabi,
6192 "offset of %qD is not ABI-compliant and may "
6193 "change in a future version of GCC", field);
6195 /* The middle end uses the type of expressions to determine the
6196 possible range of expression values. In order to optimize
6197 "x.i > 7" to "false" for a 2-bit bitfield "i", the middle end
6198 must be made aware of the width of "i", via its type.
6200 Because C++ does not have integer types of arbitrary width,
6201 we must (for the purposes of the front end) convert from the
6202 type assigned here to the declared type of the bitfield
6203 whenever a bitfield expression is used as an rvalue.
6204 Similarly, when assigning a value to a bitfield, the value
6205 must be converted to the type given the bitfield here. */
6206 if (DECL_C_BIT_FIELD (field))
6208 unsigned HOST_WIDE_INT width;
6209 tree ftype = TREE_TYPE (field);
6210 width = tree_to_uhwi (DECL_SIZE (field));
6211 if (width != TYPE_PRECISION (ftype))
6213 TREE_TYPE (field)
6214 = c_build_bitfield_integer_type (width,
6215 TYPE_UNSIGNED (ftype));
6216 TREE_TYPE (field)
6217 = cp_build_qualified_type (TREE_TYPE (field),
6218 cp_type_quals (ftype));
6222 /* If we needed additional padding after this field, add it
6223 now. */
6224 if (padding)
6226 tree padding_field;
6228 padding_field = build_decl (input_location,
6229 FIELD_DECL,
6230 NULL_TREE,
6231 char_type_node);
6232 DECL_BIT_FIELD (padding_field) = 1;
6233 DECL_SIZE (padding_field) = padding;
6234 DECL_CONTEXT (padding_field) = t;
6235 DECL_ARTIFICIAL (padding_field) = 1;
6236 DECL_IGNORED_P (padding_field) = 1;
6237 DECL_PADDING_P (padding_field) = 1;
6238 layout_nonempty_base_or_field (rli, padding_field,
6239 NULL_TREE,
6240 empty_base_offsets);
6243 last_field_was_bitfield = DECL_C_BIT_FIELD (field);
6246 if (!integer_zerop (rli->bitpos))
6248 /* Make sure that we are on a byte boundary so that the size of
6249 the class without virtual bases will always be a round number
6250 of bytes. */
6251 rli->bitpos = round_up_loc (input_location, rli->bitpos, BITS_PER_UNIT);
6252 normalize_rli (rli);
6255 /* Delete all zero-width bit-fields from the list of fields. Now
6256 that the type is laid out they are no longer important. */
6257 remove_zero_width_bit_fields (t);
6259 if (CLASSTYPE_NON_LAYOUT_POD_P (t) || CLASSTYPE_EMPTY_P (t))
6261 /* T needs a different layout as a base (eliding virtual bases
6262 or whatever). Create that version. */
6263 tree base_t = make_node (TREE_CODE (t));
6265 /* If the ABI version is not at least two, and the last
6266 field was a bit-field, RLI may not be on a byte
6267 boundary. In particular, rli_size_unit_so_far might
6268 indicate the last complete byte, while rli_size_so_far
6269 indicates the total number of bits used. Therefore,
6270 rli_size_so_far, rather than rli_size_unit_so_far, is
6271 used to compute TYPE_SIZE_UNIT. */
6272 tree eoc = end_of_class (t, /*include_virtuals_p=*/0);
6273 TYPE_SIZE_UNIT (base_t)
6274 = size_binop (MAX_EXPR,
6275 fold_convert (sizetype,
6276 size_binop (CEIL_DIV_EXPR,
6277 rli_size_so_far (rli),
6278 bitsize_int (BITS_PER_UNIT))),
6279 eoc);
6280 TYPE_SIZE (base_t)
6281 = size_binop (MAX_EXPR,
6282 rli_size_so_far (rli),
6283 size_binop (MULT_EXPR,
6284 fold_convert (bitsizetype, eoc),
6285 bitsize_int (BITS_PER_UNIT)));
6286 SET_TYPE_ALIGN (base_t, rli->record_align);
6287 TYPE_USER_ALIGN (base_t) = TYPE_USER_ALIGN (t);
6288 TYPE_TYPELESS_STORAGE (base_t) = TYPE_TYPELESS_STORAGE (t);
6290 /* Copy the non-static data members of T. This will include its
6291 direct non-virtual bases & vtable. */
6292 next_field = &TYPE_FIELDS (base_t);
6293 for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
6294 if (TREE_CODE (field) == FIELD_DECL)
6296 *next_field = copy_node (field);
6297 DECL_CONTEXT (*next_field) = base_t;
6298 next_field = &DECL_CHAIN (*next_field);
6300 *next_field = NULL_TREE;
6302 /* We use the base type for trivial assignments, and hence it
6303 needs a mode. */
6304 compute_record_mode (base_t);
6306 TYPE_CONTEXT (base_t) = t;
6308 /* Record the base version of the type. */
6309 CLASSTYPE_AS_BASE (t) = base_t;
6311 else
6312 CLASSTYPE_AS_BASE (t) = t;
6314 /* Every empty class contains an empty class. */
6315 if (CLASSTYPE_EMPTY_P (t))
6316 CLASSTYPE_CONTAINS_EMPTY_CLASS_P (t) = 1;
6318 /* Set the TYPE_DECL for this type to contain the right
6319 value for DECL_OFFSET, so that we can use it as part
6320 of a COMPONENT_REF for multiple inheritance. */
6321 layout_decl (TYPE_MAIN_DECL (t), 0);
6323 /* Now fix up any virtual base class types that we left lying
6324 around. We must get these done before we try to lay out the
6325 virtual function table. As a side-effect, this will remove the
6326 base subobject fields. */
6327 layout_virtual_bases (rli, empty_base_offsets);
6329 /* Make sure that empty classes are reflected in RLI at this
6330 point. */
6331 include_empty_classes (rli);
6333 /* Make sure not to create any structures with zero size. */
6334 if (integer_zerop (rli_size_unit_so_far (rli)) && CLASSTYPE_EMPTY_P (t))
6335 place_field (rli,
6336 build_decl (input_location,
6337 FIELD_DECL, NULL_TREE, char_type_node));
6339 /* If this is a non-POD, declaring it packed makes a difference to how it
6340 can be used as a field; don't let finalize_record_size undo it. */
6341 if (TYPE_PACKED (t) && !layout_pod_type_p (t))
6342 rli->packed_maybe_necessary = true;
6344 /* Let the back end lay out the type. */
6345 finish_record_layout (rli, /*free_p=*/true);
6347 if (TYPE_SIZE_UNIT (t)
6348 && TREE_CODE (TYPE_SIZE_UNIT (t)) == INTEGER_CST
6349 && !TREE_OVERFLOW (TYPE_SIZE_UNIT (t))
6350 && !valid_constant_size_p (TYPE_SIZE_UNIT (t)))
6351 error ("size of type %qT is too large (%qE bytes)", t, TYPE_SIZE_UNIT (t));
6353 /* Warn about bases that can't be talked about due to ambiguity. */
6354 warn_about_ambiguous_bases (t);
6356 /* Now that we're done with layout, give the base fields the real types. */
6357 for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
6358 if (DECL_ARTIFICIAL (field) && IS_FAKE_BASE_TYPE (TREE_TYPE (field)))
6359 TREE_TYPE (field) = TYPE_CONTEXT (TREE_TYPE (field));
6361 /* Clean up. */
6362 splay_tree_delete (empty_base_offsets);
6364 if (CLASSTYPE_EMPTY_P (t)
6365 && tree_int_cst_lt (sizeof_biggest_empty_class,
6366 TYPE_SIZE_UNIT (t)))
6367 sizeof_biggest_empty_class = TYPE_SIZE_UNIT (t);
6370 /* Determine the "key method" for the class type indicated by TYPE,
6371 and set CLASSTYPE_KEY_METHOD accordingly. */
6373 void
6374 determine_key_method (tree type)
6376 tree method;
6378 if (processing_template_decl
6379 || CLASSTYPE_TEMPLATE_INSTANTIATION (type)
6380 || CLASSTYPE_INTERFACE_KNOWN (type))
6381 return;
6383 /* The key method is the first non-pure virtual function that is not
6384 inline at the point of class definition. On some targets the
6385 key function may not be inline; those targets should not call
6386 this function until the end of the translation unit. */
6387 for (method = TYPE_FIELDS (type); method; method = DECL_CHAIN (method))
6388 if (TREE_CODE (method) == FUNCTION_DECL
6389 && DECL_VINDEX (method) != NULL_TREE
6390 && ! DECL_DECLARED_INLINE_P (method)
6391 && ! DECL_PURE_VIRTUAL_P (method))
6393 CLASSTYPE_KEY_METHOD (type) = method;
6394 break;
6397 return;
6400 /* Helper of find_flexarrays. Return true when FLD refers to a non-static
6401 class data member of non-zero size, otherwise false. */
6403 static inline bool
6404 field_nonempty_p (const_tree fld)
6406 if (TREE_CODE (fld) == ERROR_MARK)
6407 return false;
6409 tree type = TREE_TYPE (fld);
6410 if (TREE_CODE (fld) == FIELD_DECL
6411 && TREE_CODE (type) != ERROR_MARK
6412 && (DECL_NAME (fld) || RECORD_OR_UNION_TYPE_P (type)))
6414 return TYPE_SIZE (type)
6415 && (TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST
6416 || !tree_int_cst_equal (size_zero_node, TYPE_SIZE (type)));
6419 return false;
6422 /* Used by find_flexarrays and related functions. */
6424 struct flexmems_t
6426 /* The first flexible array member or non-zero array member found
6427 in the order of layout. */
6428 tree array;
6429 /* First non-static non-empty data member in the class or its bases. */
6430 tree first;
6431 /* The first non-static non-empty data member following either
6432 the flexible array member, if found, or the zero-length array member
6433 otherwise. AFTER[1] refers to the first such data member of a union
6434 of which the struct containing the flexible array member or zero-length
6435 array is a member, or NULL when no such union exists. This element is
6436 only used during searching, not for diagnosing problems. AFTER[0]
6437 refers to the first such data member that is not a member of such
6438 a union. */
6439 tree after[2];
6441 /* Refers to a struct (not union) in which the struct of which the flexible
6442 array is member is defined. Used to diagnose strictly (according to C)
6443 invalid uses of the latter structs. */
6444 tree enclosing;
6447 /* Find either the first flexible array member or the first zero-length
6448 array, in that order of preference, among members of class T (but not
6449 its base classes), and set members of FMEM accordingly.
6450 BASE_P is true if T is a base class of another class.
6451 PUN is set to the outermost union in which the flexible array member
6452 (or zero-length array) is defined if one such union exists, otherwise
6453 to NULL.
6454 Similarly, PSTR is set to a data member of the outermost struct of
6455 which the flexible array is a member if one such struct exists,
6456 otherwise to NULL. */
6458 static void
6459 find_flexarrays (tree t, flexmems_t *fmem, bool base_p,
6460 tree pun /* = NULL_TREE */,
6461 tree pstr /* = NULL_TREE */)
6463 /* Set the "pointer" to the outermost enclosing union if not set
6464 yet and maintain it for the remainder of the recursion. */
6465 if (!pun && TREE_CODE (t) == UNION_TYPE)
6466 pun = t;
6468 for (tree fld = TYPE_FIELDS (t); fld; fld = DECL_CHAIN (fld))
6470 if (fld == error_mark_node)
6471 return;
6473 /* Is FLD a typedef for an anonymous struct? */
6475 /* FIXME: Note that typedefs (as well as arrays) need to be fully
6476 handled elsewhere so that errors like the following are detected
6477 as well:
6478 typedef struct { int i, a[], j; } S; // bug c++/72753
6479 S s [2]; // bug c++/68489
6481 if (TREE_CODE (fld) == TYPE_DECL
6482 && DECL_IMPLICIT_TYPEDEF_P (fld)
6483 && CLASS_TYPE_P (TREE_TYPE (fld))
6484 && anon_aggrname_p (DECL_NAME (fld)))
6486 /* Check the nested unnamed type referenced via a typedef
6487 independently of FMEM (since it's not a data member of
6488 the enclosing class). */
6489 check_flexarrays (TREE_TYPE (fld));
6490 continue;
6493 /* Skip anything that's GCC-generated or not a (non-static) data
6494 member. */
6495 if (DECL_ARTIFICIAL (fld) || TREE_CODE (fld) != FIELD_DECL)
6496 continue;
6498 /* Type of the member. */
6499 tree fldtype = TREE_TYPE (fld);
6500 if (fldtype == error_mark_node)
6501 return;
6503 /* Determine the type of the array element or object referenced
6504 by the member so that it can be checked for flexible array
6505 members if it hasn't been yet. */
6506 tree eltype = fldtype;
6507 while (TREE_CODE (eltype) == ARRAY_TYPE
6508 || INDIRECT_TYPE_P (eltype))
6509 eltype = TREE_TYPE (eltype);
6511 if (RECORD_OR_UNION_TYPE_P (eltype))
6513 if (fmem->array && !fmem->after[bool (pun)])
6515 /* Once the member after the flexible array has been found
6516 we're done. */
6517 fmem->after[bool (pun)] = fld;
6518 break;
6521 if (eltype == fldtype || TYPE_UNNAMED_P (eltype))
6523 /* Descend into the non-static member struct or union and try
6524 to find a flexible array member or zero-length array among
6525 its members. This is only necessary for anonymous types
6526 and types in whose context the current type T has not been
6527 defined (the latter must not be checked again because they
6528 are already in the process of being checked by one of the
6529 recursive calls). */
6531 tree first = fmem->first;
6532 tree array = fmem->array;
6534 /* If this member isn't anonymous and a prior non-flexible array
6535 member has been seen in one of the enclosing structs, clear
6536 the FIRST member since it doesn't contribute to the flexible
6537 array struct's members. */
6538 if (first && !array && !ANON_AGGR_TYPE_P (eltype))
6539 fmem->first = NULL_TREE;
6541 find_flexarrays (eltype, fmem, false, pun,
6542 !pstr && TREE_CODE (t) == RECORD_TYPE ? fld : pstr);
6544 if (fmem->array != array)
6545 continue;
6547 if (first && !array && !ANON_AGGR_TYPE_P (eltype))
6549 /* Restore the FIRST member reset above if no flexible
6550 array member has been found in this member's struct. */
6551 fmem->first = first;
6554 /* If the member struct contains the first flexible array
6555 member, or if this member is a base class, continue to
6556 the next member and avoid setting the FMEM->NEXT pointer
6557 to point to it. */
6558 if (base_p)
6559 continue;
6563 if (field_nonempty_p (fld))
6565 /* Remember the first non-static data member. */
6566 if (!fmem->first)
6567 fmem->first = fld;
6569 /* Remember the first non-static data member after the flexible
6570 array member, if one has been found, or the zero-length array
6571 if it has been found. */
6572 if (fmem->array && !fmem->after[bool (pun)])
6573 fmem->after[bool (pun)] = fld;
6576 /* Skip non-arrays. */
6577 if (TREE_CODE (fldtype) != ARRAY_TYPE)
6578 continue;
6580 /* Determine the upper bound of the array if it has one. */
6581 if (TYPE_DOMAIN (fldtype))
6583 if (fmem->array)
6585 /* Make a record of the zero-length array if either one
6586 such field or a flexible array member has been seen to
6587 handle the pathological and unlikely case of multiple
6588 such members. */
6589 if (!fmem->after[bool (pun)])
6590 fmem->after[bool (pun)] = fld;
6592 else if (integer_all_onesp (TYPE_MAX_VALUE (TYPE_DOMAIN (fldtype))))
6594 /* Remember the first zero-length array unless a flexible array
6595 member has already been seen. */
6596 fmem->array = fld;
6597 fmem->enclosing = pstr;
6600 else
6602 /* Flexible array members have no upper bound. */
6603 if (fmem->array)
6605 if (TYPE_DOMAIN (TREE_TYPE (fmem->array)))
6607 /* Replace the zero-length array if it's been stored and
6608 reset the after pointer. */
6609 fmem->after[bool (pun)] = NULL_TREE;
6610 fmem->array = fld;
6611 fmem->enclosing = pstr;
6613 else if (!fmem->after[bool (pun)])
6614 /* Make a record of another flexible array member. */
6615 fmem->after[bool (pun)] = fld;
6617 else
6619 fmem->array = fld;
6620 fmem->enclosing = pstr;
6626 /* Diagnose a strictly (by the C standard) invalid use of a struct with
6627 a flexible array member (or the zero-length array extension). */
6629 static void
6630 diagnose_invalid_flexarray (const flexmems_t *fmem)
6632 if (fmem->array && fmem->enclosing)
6634 auto_diagnostic_group d;
6635 if (pedwarn (location_of (fmem->enclosing), OPT_Wpedantic,
6636 TYPE_DOMAIN (TREE_TYPE (fmem->array))
6637 ? G_("invalid use of %q#T with a zero-size array "
6638 "in %q#D")
6639 : G_("invalid use of %q#T with a flexible array member "
6640 "in %q#T"),
6641 DECL_CONTEXT (fmem->array),
6642 DECL_CONTEXT (fmem->enclosing)))
6643 inform (DECL_SOURCE_LOCATION (fmem->array),
6644 "array member %q#D declared here", fmem->array);
6648 /* Issue diagnostics for invalid flexible array members or zero-length
6649 arrays that are not the last elements of the containing class or its
6650 base classes or that are its sole members. */
6652 static void
6653 diagnose_flexarrays (tree t, const flexmems_t *fmem)
6655 if (!fmem->array)
6656 return;
6658 if (fmem->first && !fmem->after[0])
6660 diagnose_invalid_flexarray (fmem);
6661 return;
6664 /* Has a diagnostic been issued? */
6665 bool diagd = false;
6667 const char *msg = 0;
6669 if (TYPE_DOMAIN (TREE_TYPE (fmem->array)))
6671 if (fmem->after[0])
6672 msg = G_("zero-size array member %qD not at end of %q#T");
6673 else if (!fmem->first)
6674 msg = G_("zero-size array member %qD in an otherwise empty %q#T");
6676 if (msg)
6678 location_t loc = DECL_SOURCE_LOCATION (fmem->array);
6680 auto_diagnostic_group d;
6681 if (pedwarn (loc, OPT_Wpedantic, msg, fmem->array, t))
6683 inform (location_of (t), "in the definition of %q#T", t);
6684 diagd = true;
6688 else
6690 if (fmem->after[0])
6691 msg = G_("flexible array member %qD not at end of %q#T");
6692 else if (!fmem->first)
6693 msg = G_("flexible array member %qD in an otherwise empty %q#T");
6695 if (msg)
6697 location_t loc = DECL_SOURCE_LOCATION (fmem->array);
6698 diagd = true;
6700 auto_diagnostic_group d;
6701 error_at (loc, msg, fmem->array, t);
6703 /* In the unlikely event that the member following the flexible
6704 array member is declared in a different class, or the member
6705 overlaps another member of a common union, point to it.
6706 Otherwise it should be obvious. */
6707 if (fmem->after[0]
6708 && ((DECL_CONTEXT (fmem->after[0])
6709 != DECL_CONTEXT (fmem->array))))
6711 inform (DECL_SOURCE_LOCATION (fmem->after[0]),
6712 "next member %q#D declared here",
6713 fmem->after[0]);
6714 inform (location_of (t), "in the definition of %q#T", t);
6719 if (!diagd && fmem->array && fmem->enclosing)
6720 diagnose_invalid_flexarray (fmem);
6724 /* Recursively check to make sure that any flexible array or zero-length
6725 array members of class T or its bases are valid (i.e., not the sole
6726 non-static data member of T and, if one exists, that it is the last
6727 non-static data member of T and its base classes. FMEM is expected
6728 to be initially null and is used internally by recursive calls to
6729 the function. Issue the appropriate diagnostics for the array member
6730 that fails the checks. */
6732 static void
6733 check_flexarrays (tree t, flexmems_t *fmem /* = NULL */,
6734 bool base_p /* = false */)
6736 /* Initialize the result of a search for flexible array and zero-length
6737 array members. Avoid doing any work if the most interesting FMEM data
6738 have already been populated. */
6739 flexmems_t flexmems = flexmems_t ();
6740 if (!fmem)
6741 fmem = &flexmems;
6742 else if (fmem->array && fmem->first && fmem->after[0])
6743 return;
6745 tree fam = fmem->array;
6747 /* Recursively check the primary base class first. */
6748 if (CLASSTYPE_HAS_PRIMARY_BASE_P (t))
6750 tree basetype = BINFO_TYPE (CLASSTYPE_PRIMARY_BINFO (t));
6751 check_flexarrays (basetype, fmem, true);
6754 /* Recursively check the base classes. */
6755 int nbases = TYPE_BINFO (t) ? BINFO_N_BASE_BINFOS (TYPE_BINFO (t)) : 0;
6756 for (int i = 0; i < nbases; ++i)
6758 tree base_binfo = BINFO_BASE_BINFO (TYPE_BINFO (t), i);
6760 /* The primary base class was already checked above. */
6761 if (base_binfo == CLASSTYPE_PRIMARY_BINFO (t))
6762 continue;
6764 /* Virtual base classes are at the end. */
6765 if (BINFO_VIRTUAL_P (base_binfo))
6766 continue;
6768 /* Check the base class. */
6769 check_flexarrays (BINFO_TYPE (base_binfo), fmem, /*base_p=*/true);
6772 if (fmem == &flexmems)
6774 /* Check virtual base classes only once per derived class.
6775 I.e., this check is not performed recursively for base
6776 classes. */
6777 int i;
6778 tree base_binfo;
6779 vec<tree, va_gc> *vbases;
6780 for (vbases = CLASSTYPE_VBASECLASSES (t), i = 0;
6781 vec_safe_iterate (vbases, i, &base_binfo); i++)
6783 /* Check the virtual base class. */
6784 tree basetype = TREE_TYPE (base_binfo);
6786 check_flexarrays (basetype, fmem, /*base_p=*/true);
6790 /* Is the type unnamed (and therefore a member of it potentially
6791 an anonymous struct or union)? */
6792 bool maybe_anon_p = TYPE_UNNAMED_P (t);
6794 /* Search the members of the current (possibly derived) class, skipping
6795 unnamed structs and unions since those could be anonymous. */
6796 if (fmem != &flexmems || !maybe_anon_p)
6797 find_flexarrays (t, fmem, base_p || fam != fmem->array);
6799 if (fmem == &flexmems && !maybe_anon_p)
6801 /* Issue diagnostics for invalid flexible and zero-length array
6802 members found in base classes or among the members of the current
6803 class. Ignore anonymous structs and unions whose members are
6804 considered to be members of the enclosing class and thus will
6805 be diagnosed when checking it. */
6806 diagnose_flexarrays (t, fmem);
6810 /* Perform processing required when the definition of T (a class type)
6811 is complete. Diagnose invalid definitions of flexible array members
6812 and zero-size arrays. */
6814 void
6815 finish_struct_1 (tree t)
6817 tree x;
6818 /* A TREE_LIST. The TREE_VALUE of each node is a FUNCTION_DECL. */
6819 tree virtuals = NULL_TREE;
6821 if (COMPLETE_TYPE_P (t))
6823 gcc_assert (MAYBE_CLASS_TYPE_P (t));
6824 error ("redefinition of %q#T", t);
6825 popclass ();
6826 return;
6829 /* If this type was previously laid out as a forward reference,
6830 make sure we lay it out again. */
6831 TYPE_SIZE (t) = NULL_TREE;
6832 CLASSTYPE_PRIMARY_BINFO (t) = NULL_TREE;
6834 /* Make assumptions about the class; we'll reset the flags if
6835 necessary. */
6836 CLASSTYPE_EMPTY_P (t) = 1;
6837 CLASSTYPE_NEARLY_EMPTY_P (t) = 1;
6838 CLASSTYPE_CONTAINS_EMPTY_CLASS_P (t) = 0;
6839 CLASSTYPE_LITERAL_P (t) = true;
6841 /* Do end-of-class semantic processing: checking the validity of the
6842 bases and members and add implicitly generated methods. */
6843 check_bases_and_members (t);
6845 /* Find the key method. */
6846 if (TYPE_CONTAINS_VPTR_P (t))
6848 /* The Itanium C++ ABI permits the key method to be chosen when
6849 the class is defined -- even though the key method so
6850 selected may later turn out to be an inline function. On
6851 some systems (such as ARM Symbian OS) the key method cannot
6852 be determined until the end of the translation unit. On such
6853 systems, we leave CLASSTYPE_KEY_METHOD set to NULL, which
6854 will cause the class to be added to KEYED_CLASSES. Then, in
6855 finish_file we will determine the key method. */
6856 if (targetm.cxx.key_method_may_be_inline ())
6857 determine_key_method (t);
6859 /* If a polymorphic class has no key method, we may emit the vtable
6860 in every translation unit where the class definition appears. If
6861 we're devirtualizing, we can look into the vtable even if we
6862 aren't emitting it. */
6863 if (!CLASSTYPE_KEY_METHOD (t))
6864 vec_safe_push (keyed_classes, t);
6867 /* Layout the class itself. */
6868 layout_class_type (t, &virtuals);
6869 /* COMPLETE_TYPE_P is now true. */
6871 set_class_bindings (t);
6873 /* With the layout complete, check for flexible array members and
6874 zero-length arrays that might overlap other members in the final
6875 layout. */
6876 check_flexarrays (t);
6878 virtuals = modify_all_vtables (t, nreverse (virtuals));
6880 /* If necessary, create the primary vtable for this class. */
6881 if (virtuals || TYPE_CONTAINS_VPTR_P (t))
6883 /* We must enter these virtuals into the table. */
6884 if (!CLASSTYPE_HAS_PRIMARY_BASE_P (t))
6885 build_primary_vtable (NULL_TREE, t);
6886 else if (! BINFO_NEW_VTABLE_MARKED (TYPE_BINFO (t)))
6887 /* Here we know enough to change the type of our virtual
6888 function table, but we will wait until later this function. */
6889 build_primary_vtable (CLASSTYPE_PRIMARY_BINFO (t), t);
6891 /* If we're warning about ABI tags, check the types of the new
6892 virtual functions. */
6893 if (warn_abi_tag)
6894 for (tree v = virtuals; v; v = TREE_CHAIN (v))
6895 check_abi_tags (t, TREE_VALUE (v));
6898 if (TYPE_CONTAINS_VPTR_P (t))
6900 int vindex;
6901 tree fn;
6903 if (BINFO_VTABLE (TYPE_BINFO (t)))
6904 gcc_assert (DECL_VIRTUAL_P (BINFO_VTABLE (TYPE_BINFO (t))));
6905 if (!CLASSTYPE_HAS_PRIMARY_BASE_P (t))
6906 gcc_assert (BINFO_VIRTUALS (TYPE_BINFO (t)) == NULL_TREE);
6908 /* Add entries for virtual functions introduced by this class. */
6909 BINFO_VIRTUALS (TYPE_BINFO (t))
6910 = chainon (BINFO_VIRTUALS (TYPE_BINFO (t)), virtuals);
6912 /* Set DECL_VINDEX for all functions declared in this class. */
6913 for (vindex = 0, fn = BINFO_VIRTUALS (TYPE_BINFO (t));
6915 fn = TREE_CHAIN (fn),
6916 vindex += (TARGET_VTABLE_USES_DESCRIPTORS
6917 ? TARGET_VTABLE_USES_DESCRIPTORS : 1))
6919 tree fndecl = BV_FN (fn);
6921 if (DECL_THUNK_P (fndecl))
6922 /* A thunk. We should never be calling this entry directly
6923 from this vtable -- we'd use the entry for the non
6924 thunk base function. */
6925 DECL_VINDEX (fndecl) = NULL_TREE;
6926 else if (TREE_CODE (DECL_VINDEX (fndecl)) != INTEGER_CST)
6927 DECL_VINDEX (fndecl) = build_int_cst (NULL_TREE, vindex);
6931 finish_struct_bits (t);
6933 set_method_tm_attributes (t);
6934 if (flag_openmp || flag_openmp_simd)
6935 finish_omp_declare_simd_methods (t);
6937 /* Clear DECL_IN_AGGR_P for all member functions. Complete the rtl
6938 for any static member objects of the type we're working on. */
6939 for (x = TYPE_FIELDS (t); x; x = DECL_CHAIN (x))
6940 if (DECL_DECLARES_FUNCTION_P (x))
6941 DECL_IN_AGGR_P (x) = false;
6942 else if (VAR_P (x) && TREE_STATIC (x)
6943 && TREE_TYPE (x) != error_mark_node
6944 && same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (x)), t))
6945 SET_DECL_MODE (x, TYPE_MODE (t));
6947 /* Complain if one of the field types requires lower visibility. */
6948 constrain_class_visibility (t);
6950 /* Make the rtl for any new vtables we have created, and unmark
6951 the base types we marked. */
6952 finish_vtbls (t);
6954 /* Build the VTT for T. */
6955 build_vtt (t);
6957 if (warn_nonvdtor
6958 && TYPE_POLYMORPHIC_P (t) && accessible_nvdtor_p (t)
6959 && !CLASSTYPE_FINAL (t))
6960 warning (OPT_Wnon_virtual_dtor,
6961 "%q#T has virtual functions and accessible"
6962 " non-virtual destructor", t);
6964 complete_vars (t);
6966 if (warn_overloaded_virtual)
6967 warn_hidden (t);
6969 /* Class layout, assignment of virtual table slots, etc., is now
6970 complete. Give the back end a chance to tweak the visibility of
6971 the class or perform any other required target modifications. */
6972 targetm.cxx.adjust_class_at_definition (t);
6974 maybe_suppress_debug_info (t);
6976 if (flag_vtable_verify)
6977 vtv_save_class_info (t);
6979 dump_class_hierarchy (t);
6981 /* Finish debugging output for this type. */
6982 rest_of_type_compilation (t, ! LOCAL_CLASS_P (t));
6984 if (TYPE_TRANSPARENT_AGGR (t))
6986 tree field = first_field (t);
6987 if (field == NULL_TREE || error_operand_p (field))
6989 error ("type transparent %q#T does not have any fields", t);
6990 TYPE_TRANSPARENT_AGGR (t) = 0;
6992 else if (DECL_ARTIFICIAL (field))
6994 if (DECL_FIELD_IS_BASE (field))
6995 error ("type transparent class %qT has base classes", t);
6996 else
6998 gcc_checking_assert (DECL_VIRTUAL_P (field));
6999 error ("type transparent class %qT has virtual functions", t);
7001 TYPE_TRANSPARENT_AGGR (t) = 0;
7003 else if (TYPE_MODE (t) != DECL_MODE (field))
7005 error ("type transparent %q#T cannot be made transparent because "
7006 "the type of the first field has a different ABI from the "
7007 "class overall", t);
7008 TYPE_TRANSPARENT_AGGR (t) = 0;
7013 /* When T was built up, the member declarations were added in reverse
7014 order. Rearrange them to declaration order. */
7016 void
7017 unreverse_member_declarations (tree t)
7019 tree next;
7020 tree prev;
7021 tree x;
7023 /* The following lists are all in reverse order. Put them in
7024 declaration order now. */
7025 CLASSTYPE_DECL_LIST (t) = nreverse (CLASSTYPE_DECL_LIST (t));
7027 /* For the TYPE_FIELDS, only the non TYPE_DECLs are in reverse
7028 order, so we can't just use nreverse. Due to stat_hack
7029 chicanery in finish_member_declaration. */
7030 prev = NULL_TREE;
7031 for (x = TYPE_FIELDS (t);
7032 x && TREE_CODE (x) != TYPE_DECL;
7033 x = next)
7035 next = DECL_CHAIN (x);
7036 DECL_CHAIN (x) = prev;
7037 prev = x;
7040 if (prev)
7042 DECL_CHAIN (TYPE_FIELDS (t)) = x;
7043 TYPE_FIELDS (t) = prev;
7047 tree
7048 finish_struct (tree t, tree attributes)
7050 location_t saved_loc = input_location;
7052 /* Now that we've got all the field declarations, reverse everything
7053 as necessary. */
7054 unreverse_member_declarations (t);
7056 cplus_decl_attributes (&t, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
7057 fixup_attribute_variants (t);
7059 /* Nadger the current location so that diagnostics point to the start of
7060 the struct, not the end. */
7061 input_location = DECL_SOURCE_LOCATION (TYPE_NAME (t));
7063 if (processing_template_decl)
7065 tree x;
7067 /* We need to add the target functions of USING_DECLS, so that
7068 they can be found when the using declaration is not
7069 instantiated yet. */
7070 for (x = TYPE_FIELDS (t); x; x = DECL_CHAIN (x))
7071 if (TREE_CODE (x) == USING_DECL)
7073 tree fn = strip_using_decl (x);
7074 if (OVL_P (fn))
7075 for (lkp_iterator iter (fn); iter; ++iter)
7076 add_method (t, *iter, true);
7078 else if (DECL_DECLARES_FUNCTION_P (x))
7079 DECL_IN_AGGR_P (x) = false;
7081 TYPE_SIZE (t) = bitsize_zero_node;
7082 TYPE_SIZE_UNIT (t) = size_zero_node;
7083 /* COMPLETE_TYPE_P is now true. */
7085 set_class_bindings (t);
7087 /* We need to emit an error message if this type was used as a parameter
7088 and it is an abstract type, even if it is a template. We construct
7089 a simple CLASSTYPE_PURE_VIRTUALS list without taking bases into
7090 account and we call complete_vars with this type, which will check
7091 the PARM_DECLS. Note that while the type is being defined,
7092 CLASSTYPE_PURE_VIRTUALS contains the list of the inline friends
7093 (see CLASSTYPE_INLINE_FRIENDS) so we need to clear it. */
7094 CLASSTYPE_PURE_VIRTUALS (t) = NULL;
7095 for (x = TYPE_FIELDS (t); x; x = DECL_CHAIN (x))
7096 if (TREE_CODE (x) == FUNCTION_DECL && DECL_PURE_VIRTUAL_P (x))
7097 vec_safe_push (CLASSTYPE_PURE_VIRTUALS (t), x);
7098 complete_vars (t);
7100 /* Remember current #pragma pack value. */
7101 TYPE_PRECISION (t) = maximum_field_alignment;
7103 /* Fix up any variants we've already built. */
7104 for (x = TYPE_NEXT_VARIANT (t); x; x = TYPE_NEXT_VARIANT (x))
7106 TYPE_SIZE (x) = TYPE_SIZE (t);
7107 TYPE_SIZE_UNIT (x) = TYPE_SIZE_UNIT (t);
7108 TYPE_FIELDS (x) = TYPE_FIELDS (t);
7111 else
7112 finish_struct_1 (t);
7113 /* COMPLETE_TYPE_P is now true. */
7115 maybe_warn_about_overly_private_class (t);
7117 if (is_std_init_list (t))
7119 /* People keep complaining that the compiler crashes on an invalid
7120 definition of initializer_list, so I guess we should explicitly
7121 reject it. What the compiler internals care about is that it's a
7122 template and has a pointer field followed by size_type field. */
7123 bool ok = false;
7124 if (processing_template_decl)
7126 tree f = next_initializable_field (TYPE_FIELDS (t));
7127 if (f && TYPE_PTR_P (TREE_TYPE (f)))
7129 f = next_initializable_field (DECL_CHAIN (f));
7130 if (f && same_type_p (TREE_TYPE (f), size_type_node))
7131 ok = true;
7134 if (!ok)
7135 fatal_error (input_location, "definition of %qD does not match "
7136 "%<#include <initializer_list>%>", TYPE_NAME (t));
7139 input_location = saved_loc;
7141 TYPE_BEING_DEFINED (t) = 0;
7143 if (current_class_type)
7144 popclass ();
7145 else
7146 error ("trying to finish struct, but kicked out due to previous parse errors");
7148 if (processing_template_decl && at_function_scope_p ()
7149 /* Lambdas are defined by the LAMBDA_EXPR. */
7150 && !LAMBDA_TYPE_P (t))
7151 add_stmt (build_min (TAG_DEFN, t));
7153 return t;
7156 /* Hash table to avoid endless recursion when handling references. */
7157 static hash_table<nofree_ptr_hash<tree_node> > *fixed_type_or_null_ref_ht;
7159 /* Return the dynamic type of INSTANCE, if known.
7160 Used to determine whether the virtual function table is needed
7161 or not.
7163 *NONNULL is set iff INSTANCE can be known to be nonnull, regardless
7164 of our knowledge of its type. *NONNULL should be initialized
7165 before this function is called. */
7167 static tree
7168 fixed_type_or_null (tree instance, int *nonnull, int *cdtorp)
7170 #define RECUR(T) fixed_type_or_null((T), nonnull, cdtorp)
7172 switch (TREE_CODE (instance))
7174 case INDIRECT_REF:
7175 if (INDIRECT_TYPE_P (TREE_TYPE (instance)))
7176 return NULL_TREE;
7177 else
7178 return RECUR (TREE_OPERAND (instance, 0));
7180 case CALL_EXPR:
7181 /* This is a call to a constructor, hence it's never zero. */
7182 if (CALL_EXPR_FN (instance)
7183 && TREE_HAS_CONSTRUCTOR (instance))
7185 if (nonnull)
7186 *nonnull = 1;
7187 return TREE_TYPE (instance);
7189 return NULL_TREE;
7191 case SAVE_EXPR:
7192 /* This is a call to a constructor, hence it's never zero. */
7193 if (TREE_HAS_CONSTRUCTOR (instance))
7195 if (nonnull)
7196 *nonnull = 1;
7197 return TREE_TYPE (instance);
7199 return RECUR (TREE_OPERAND (instance, 0));
7201 case POINTER_PLUS_EXPR:
7202 case PLUS_EXPR:
7203 case MINUS_EXPR:
7204 if (TREE_CODE (TREE_OPERAND (instance, 0)) == ADDR_EXPR)
7205 return RECUR (TREE_OPERAND (instance, 0));
7206 if (TREE_CODE (TREE_OPERAND (instance, 1)) == INTEGER_CST)
7207 /* Propagate nonnull. */
7208 return RECUR (TREE_OPERAND (instance, 0));
7210 return NULL_TREE;
7212 CASE_CONVERT:
7213 return RECUR (TREE_OPERAND (instance, 0));
7215 case ADDR_EXPR:
7216 instance = TREE_OPERAND (instance, 0);
7217 if (nonnull)
7219 /* Just because we see an ADDR_EXPR doesn't mean we're dealing
7220 with a real object -- given &p->f, p can still be null. */
7221 tree t = get_base_address (instance);
7222 /* ??? Probably should check DECL_WEAK here. */
7223 if (t && DECL_P (t))
7224 *nonnull = 1;
7226 return RECUR (instance);
7228 case COMPONENT_REF:
7229 /* If this component is really a base class reference, then the field
7230 itself isn't definitive. */
7231 if (DECL_FIELD_IS_BASE (TREE_OPERAND (instance, 1)))
7232 return RECUR (TREE_OPERAND (instance, 0));
7233 return RECUR (TREE_OPERAND (instance, 1));
7235 case VAR_DECL:
7236 case FIELD_DECL:
7237 if (TREE_CODE (TREE_TYPE (instance)) == ARRAY_TYPE
7238 && MAYBE_CLASS_TYPE_P (TREE_TYPE (TREE_TYPE (instance))))
7240 if (nonnull)
7241 *nonnull = 1;
7242 return TREE_TYPE (TREE_TYPE (instance));
7244 /* fall through. */
7245 case TARGET_EXPR:
7246 case PARM_DECL:
7247 case RESULT_DECL:
7248 if (MAYBE_CLASS_TYPE_P (TREE_TYPE (instance)))
7250 if (nonnull)
7251 *nonnull = 1;
7252 return TREE_TYPE (instance);
7254 else if (instance == current_class_ptr)
7256 if (nonnull)
7257 *nonnull = 1;
7259 /* if we're in a ctor or dtor, we know our type. If
7260 current_class_ptr is set but we aren't in a function, we're in
7261 an NSDMI (and therefore a constructor). */
7262 if (current_scope () != current_function_decl
7263 || (DECL_LANG_SPECIFIC (current_function_decl)
7264 && (DECL_CONSTRUCTOR_P (current_function_decl)
7265 || DECL_DESTRUCTOR_P (current_function_decl))))
7267 if (cdtorp)
7268 *cdtorp = 1;
7269 return TREE_TYPE (TREE_TYPE (instance));
7272 else if (TYPE_REF_P (TREE_TYPE (instance)))
7274 /* We only need one hash table because it is always left empty. */
7275 if (!fixed_type_or_null_ref_ht)
7276 fixed_type_or_null_ref_ht
7277 = new hash_table<nofree_ptr_hash<tree_node> > (37);
7279 /* Reference variables should be references to objects. */
7280 if (nonnull)
7281 *nonnull = 1;
7283 /* Enter the INSTANCE in a table to prevent recursion; a
7284 variable's initializer may refer to the variable
7285 itself. */
7286 if (VAR_P (instance)
7287 && DECL_INITIAL (instance)
7288 && !type_dependent_expression_p_push (DECL_INITIAL (instance))
7289 && !fixed_type_or_null_ref_ht->find (instance))
7291 tree type;
7292 tree_node **slot;
7294 slot = fixed_type_or_null_ref_ht->find_slot (instance, INSERT);
7295 *slot = instance;
7296 type = RECUR (DECL_INITIAL (instance));
7297 fixed_type_or_null_ref_ht->remove_elt (instance);
7299 return type;
7302 return NULL_TREE;
7304 default:
7305 return NULL_TREE;
7307 #undef RECUR
7310 /* Return nonzero if the dynamic type of INSTANCE is known, and
7311 equivalent to the static type. We also handle the case where
7312 INSTANCE is really a pointer. Return negative if this is a
7313 ctor/dtor. There the dynamic type is known, but this might not be
7314 the most derived base of the original object, and hence virtual
7315 bases may not be laid out according to this type.
7317 Used to determine whether the virtual function table is needed
7318 or not.
7320 *NONNULL is set iff INSTANCE can be known to be nonnull, regardless
7321 of our knowledge of its type. *NONNULL should be initialized
7322 before this function is called. */
7325 resolves_to_fixed_type_p (tree instance, int* nonnull)
7327 tree t = TREE_TYPE (instance);
7328 int cdtorp = 0;
7329 tree fixed;
7331 /* processing_template_decl can be false in a template if we're in
7332 instantiate_non_dependent_expr, but we still want to suppress
7333 this check. */
7334 if (in_template_function ())
7336 /* In a template we only care about the type of the result. */
7337 if (nonnull)
7338 *nonnull = true;
7339 return true;
7342 fixed = fixed_type_or_null (instance, nonnull, &cdtorp);
7343 if (fixed == NULL_TREE)
7344 return 0;
7345 if (INDIRECT_TYPE_P (t))
7346 t = TREE_TYPE (t);
7347 if (!same_type_ignoring_top_level_qualifiers_p (t, fixed))
7348 return 0;
7349 return cdtorp ? -1 : 1;
7353 void
7354 init_class_processing (void)
7356 current_class_depth = 0;
7357 current_class_stack_size = 10;
7358 current_class_stack
7359 = XNEWVEC (struct class_stack_node, current_class_stack_size);
7360 vec_alloc (local_classes, 8);
7361 sizeof_biggest_empty_class = size_zero_node;
7363 ridpointers[(int) RID_PUBLIC] = access_public_node;
7364 ridpointers[(int) RID_PRIVATE] = access_private_node;
7365 ridpointers[(int) RID_PROTECTED] = access_protected_node;
7368 /* Restore the cached PREVIOUS_CLASS_LEVEL. */
7370 static void
7371 restore_class_cache (void)
7373 tree type;
7375 /* We are re-entering the same class we just left, so we don't
7376 have to search the whole inheritance matrix to find all the
7377 decls to bind again. Instead, we install the cached
7378 class_shadowed list and walk through it binding names. */
7379 push_binding_level (previous_class_level);
7380 class_binding_level = previous_class_level;
7381 /* Restore IDENTIFIER_TYPE_VALUE. */
7382 for (type = class_binding_level->type_shadowed;
7383 type;
7384 type = TREE_CHAIN (type))
7385 SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (type), TREE_TYPE (type));
7388 /* Set global variables CURRENT_CLASS_NAME and CURRENT_CLASS_TYPE as
7389 appropriate for TYPE.
7391 So that we may avoid calls to lookup_name, we cache the _TYPE
7392 nodes of local TYPE_DECLs in the TREE_TYPE field of the name.
7394 For multiple inheritance, we perform a two-pass depth-first search
7395 of the type lattice. */
7397 void
7398 pushclass (tree type)
7400 class_stack_node_t csn;
7402 type = TYPE_MAIN_VARIANT (type);
7404 /* Make sure there is enough room for the new entry on the stack. */
7405 if (current_class_depth + 1 >= current_class_stack_size)
7407 current_class_stack_size *= 2;
7408 current_class_stack
7409 = XRESIZEVEC (struct class_stack_node, current_class_stack,
7410 current_class_stack_size);
7413 /* Insert a new entry on the class stack. */
7414 csn = current_class_stack + current_class_depth;
7415 csn->name = current_class_name;
7416 csn->type = current_class_type;
7417 csn->access = current_access_specifier;
7418 csn->names_used = 0;
7419 csn->hidden = 0;
7420 current_class_depth++;
7422 /* Now set up the new type. */
7423 current_class_name = TYPE_NAME (type);
7424 if (TREE_CODE (current_class_name) == TYPE_DECL)
7425 current_class_name = DECL_NAME (current_class_name);
7426 current_class_type = type;
7428 /* By default, things in classes are private, while things in
7429 structures or unions are public. */
7430 current_access_specifier = (CLASSTYPE_DECLARED_CLASS (type)
7431 ? access_private_node
7432 : access_public_node);
7434 if (previous_class_level
7435 && type != previous_class_level->this_entity
7436 && current_class_depth == 1)
7438 /* Forcibly remove any old class remnants. */
7439 invalidate_class_lookup_cache ();
7442 if (!previous_class_level
7443 || type != previous_class_level->this_entity
7444 || current_class_depth > 1)
7445 pushlevel_class ();
7446 else
7447 restore_class_cache ();
7450 /* When we exit a toplevel class scope, we save its binding level so
7451 that we can restore it quickly. Here, we've entered some other
7452 class, so we must invalidate our cache. */
7454 void
7455 invalidate_class_lookup_cache (void)
7457 previous_class_level = NULL;
7460 /* Get out of the current class scope. If we were in a class scope
7461 previously, that is the one popped to. */
7463 void
7464 popclass (void)
7466 poplevel_class ();
7468 current_class_depth--;
7469 current_class_name = current_class_stack[current_class_depth].name;
7470 current_class_type = current_class_stack[current_class_depth].type;
7471 current_access_specifier = current_class_stack[current_class_depth].access;
7472 if (current_class_stack[current_class_depth].names_used)
7473 splay_tree_delete (current_class_stack[current_class_depth].names_used);
7476 /* Mark the top of the class stack as hidden. */
7478 void
7479 push_class_stack (void)
7481 if (current_class_depth)
7482 ++current_class_stack[current_class_depth - 1].hidden;
7485 /* Mark the top of the class stack as un-hidden. */
7487 void
7488 pop_class_stack (void)
7490 if (current_class_depth)
7491 --current_class_stack[current_class_depth - 1].hidden;
7494 /* If the class type currently being defined is either T or
7495 a nested type of T, returns the type from the current_class_stack,
7496 which might be equivalent to but not equal to T in case of
7497 constrained partial specializations. */
7499 tree
7500 currently_open_class (tree t)
7502 int i;
7504 if (!CLASS_TYPE_P (t))
7505 return NULL_TREE;
7507 t = TYPE_MAIN_VARIANT (t);
7509 /* We start looking from 1 because entry 0 is from global scope,
7510 and has no type. */
7511 for (i = current_class_depth; i > 0; --i)
7513 tree c;
7514 if (i == current_class_depth)
7515 c = current_class_type;
7516 else
7518 if (current_class_stack[i].hidden)
7519 break;
7520 c = current_class_stack[i].type;
7522 if (!c)
7523 continue;
7524 if (same_type_p (c, t))
7525 return c;
7527 return NULL_TREE;
7530 /* If either current_class_type or one of its enclosing classes are derived
7531 from T, return the appropriate type. Used to determine how we found
7532 something via unqualified lookup. */
7534 tree
7535 currently_open_derived_class (tree t)
7537 int i;
7539 /* The bases of a dependent type are unknown. */
7540 if (dependent_type_p (t))
7541 return NULL_TREE;
7543 if (!current_class_type)
7544 return NULL_TREE;
7546 if (DERIVED_FROM_P (t, current_class_type))
7547 return current_class_type;
7549 for (i = current_class_depth - 1; i > 0; --i)
7551 if (current_class_stack[i].hidden)
7552 break;
7553 if (DERIVED_FROM_P (t, current_class_stack[i].type))
7554 return current_class_stack[i].type;
7557 return NULL_TREE;
7560 /* Return the outermost enclosing class type that is still open, or
7561 NULL_TREE. */
7563 tree
7564 outermost_open_class (void)
7566 if (!current_class_type)
7567 return NULL_TREE;
7568 tree r = NULL_TREE;
7569 if (TYPE_BEING_DEFINED (current_class_type))
7570 r = current_class_type;
7571 for (int i = current_class_depth - 1; i > 0; --i)
7573 if (current_class_stack[i].hidden)
7574 break;
7575 tree t = current_class_stack[i].type;
7576 if (!TYPE_BEING_DEFINED (t))
7577 break;
7578 r = t;
7580 return r;
7583 /* Returns the innermost class type which is not a lambda closure type. */
7585 tree
7586 current_nonlambda_class_type (void)
7588 tree type = current_class_type;
7589 while (type && LAMBDA_TYPE_P (type))
7590 type = decl_type_context (TYPE_NAME (type));
7591 return type;
7594 /* When entering a class scope, all enclosing class scopes' names with
7595 static meaning (static variables, static functions, types and
7596 enumerators) have to be visible. This recursive function calls
7597 pushclass for all enclosing class contexts until global or a local
7598 scope is reached. TYPE is the enclosed class. */
7600 void
7601 push_nested_class (tree type)
7603 /* A namespace might be passed in error cases, like A::B:C. */
7604 if (type == NULL_TREE
7605 || !CLASS_TYPE_P (type))
7606 return;
7608 push_nested_class (DECL_CONTEXT (TYPE_MAIN_DECL (type)));
7610 pushclass (type);
7613 /* Undoes a push_nested_class call. */
7615 void
7616 pop_nested_class (void)
7618 tree context = DECL_CONTEXT (TYPE_MAIN_DECL (current_class_type));
7620 popclass ();
7621 if (context && CLASS_TYPE_P (context))
7622 pop_nested_class ();
7625 /* Returns the number of extern "LANG" blocks we are nested within. */
7628 current_lang_depth (void)
7630 return vec_safe_length (current_lang_base);
7633 /* Set global variables CURRENT_LANG_NAME to appropriate value
7634 so that behavior of name-mangling machinery is correct. */
7636 void
7637 push_lang_context (tree name)
7639 vec_safe_push (current_lang_base, current_lang_name);
7641 if (name == lang_name_cplusplus)
7642 current_lang_name = name;
7643 else if (name == lang_name_c)
7644 current_lang_name = name;
7645 else
7646 error ("language string %<\"%E\"%> not recognized", name);
7649 /* Get out of the current language scope. */
7651 void
7652 pop_lang_context (void)
7654 current_lang_name = current_lang_base->pop ();
7657 /* Type instantiation routines. */
7659 /* Given an OVERLOAD and a TARGET_TYPE, return the function that
7660 matches the TARGET_TYPE. If there is no satisfactory match, return
7661 error_mark_node, and issue an error & warning messages under
7662 control of FLAGS. Permit pointers to member function if FLAGS
7663 permits. If TEMPLATE_ONLY, the name of the overloaded function was
7664 a template-id, and EXPLICIT_TARGS are the explicitly provided
7665 template arguments.
7667 If OVERLOAD is for one or more member functions, then ACCESS_PATH
7668 is the base path used to reference those member functions. If
7669 the address is resolved to a member function, access checks will be
7670 performed and errors issued if appropriate. */
7672 static tree
7673 resolve_address_of_overloaded_function (tree target_type,
7674 tree overload,
7675 tsubst_flags_t complain,
7676 bool template_only,
7677 tree explicit_targs,
7678 tree access_path)
7680 /* Here's what the standard says:
7682 [over.over]
7684 If the name is a function template, template argument deduction
7685 is done, and if the argument deduction succeeds, the deduced
7686 arguments are used to generate a single template function, which
7687 is added to the set of overloaded functions considered.
7689 Non-member functions and static member functions match targets of
7690 type "pointer-to-function" or "reference-to-function." Nonstatic
7691 member functions match targets of type "pointer-to-member
7692 function;" the function type of the pointer to member is used to
7693 select the member function from the set of overloaded member
7694 functions. If a nonstatic member function is selected, the
7695 reference to the overloaded function name is required to have the
7696 form of a pointer to member as described in 5.3.1.
7698 If more than one function is selected, any template functions in
7699 the set are eliminated if the set also contains a non-template
7700 function, and any given template function is eliminated if the
7701 set contains a second template function that is more specialized
7702 than the first according to the partial ordering rules 14.5.5.2.
7703 After such eliminations, if any, there shall remain exactly one
7704 selected function. */
7706 int is_ptrmem = 0;
7707 /* We store the matches in a TREE_LIST rooted here. The functions
7708 are the TREE_PURPOSE, not the TREE_VALUE, in this list, for easy
7709 interoperability with most_specialized_instantiation. */
7710 tree matches = NULL_TREE;
7711 tree fn;
7712 tree target_fn_type;
7714 /* By the time we get here, we should be seeing only real
7715 pointer-to-member types, not the internal POINTER_TYPE to
7716 METHOD_TYPE representation. */
7717 gcc_assert (!TYPE_PTR_P (target_type)
7718 || TREE_CODE (TREE_TYPE (target_type)) != METHOD_TYPE);
7720 gcc_assert (is_overloaded_fn (overload));
7722 /* Check that the TARGET_TYPE is reasonable. */
7723 if (TYPE_PTRFN_P (target_type)
7724 || TYPE_REFFN_P (target_type))
7725 /* This is OK. */;
7726 else if (TYPE_PTRMEMFUNC_P (target_type))
7727 /* This is OK, too. */
7728 is_ptrmem = 1;
7729 else if (TREE_CODE (target_type) == FUNCTION_TYPE)
7730 /* This is OK, too. This comes from a conversion to reference
7731 type. */
7732 target_type = build_reference_type (target_type);
7733 else
7735 if (complain & tf_error)
7736 error ("cannot resolve overloaded function %qD based on"
7737 " conversion to type %qT",
7738 OVL_NAME (overload), target_type);
7739 return error_mark_node;
7742 /* Non-member functions and static member functions match targets of type
7743 "pointer-to-function" or "reference-to-function." Nonstatic member
7744 functions match targets of type "pointer-to-member-function;" the
7745 function type of the pointer to member is used to select the member
7746 function from the set of overloaded member functions.
7748 So figure out the FUNCTION_TYPE that we want to match against. */
7749 target_fn_type = static_fn_type (target_type);
7751 /* If we can find a non-template function that matches, we can just
7752 use it. There's no point in generating template instantiations
7753 if we're just going to throw them out anyhow. But, of course, we
7754 can only do this when we don't *need* a template function. */
7755 if (!template_only)
7756 for (lkp_iterator iter (overload); iter; ++iter)
7758 tree fn = *iter;
7760 if (TREE_CODE (fn) == TEMPLATE_DECL)
7761 /* We're not looking for templates just yet. */
7762 continue;
7764 if ((TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE) != is_ptrmem)
7765 /* We're looking for a non-static member, and this isn't
7766 one, or vice versa. */
7767 continue;
7769 /* In C++17 we need the noexcept-qualifier to compare types. */
7770 if (flag_noexcept_type
7771 && !maybe_instantiate_noexcept (fn, complain))
7772 continue;
7774 /* See if there's a match. */
7775 tree fntype = static_fn_type (fn);
7776 if (same_type_p (target_fn_type, fntype)
7777 || fnptr_conv_p (target_fn_type, fntype))
7778 matches = tree_cons (fn, NULL_TREE, matches);
7781 /* Now, if we've already got a match (or matches), there's no need
7782 to proceed to the template functions. But, if we don't have a
7783 match we need to look at them, too. */
7784 if (!matches)
7786 tree target_arg_types;
7787 tree target_ret_type;
7788 tree *args;
7789 unsigned int nargs, ia;
7790 tree arg;
7792 target_arg_types = TYPE_ARG_TYPES (target_fn_type);
7793 target_ret_type = TREE_TYPE (target_fn_type);
7795 nargs = list_length (target_arg_types);
7796 args = XALLOCAVEC (tree, nargs);
7797 for (arg = target_arg_types, ia = 0;
7798 arg != NULL_TREE && arg != void_list_node;
7799 arg = TREE_CHAIN (arg), ++ia)
7800 args[ia] = TREE_VALUE (arg);
7801 nargs = ia;
7803 for (lkp_iterator iter (overload); iter; ++iter)
7805 tree fn = *iter;
7806 tree instantiation;
7807 tree targs;
7809 if (TREE_CODE (fn) != TEMPLATE_DECL)
7810 /* We're only looking for templates. */
7811 continue;
7813 if ((TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
7814 != is_ptrmem)
7815 /* We're not looking for a non-static member, and this is
7816 one, or vice versa. */
7817 continue;
7819 tree ret = target_ret_type;
7821 /* If the template has a deduced return type, don't expose it to
7822 template argument deduction. */
7823 if (undeduced_auto_decl (fn))
7824 ret = NULL_TREE;
7826 /* Try to do argument deduction. */
7827 targs = make_tree_vec (DECL_NTPARMS (fn));
7828 instantiation = fn_type_unification (fn, explicit_targs, targs, args,
7829 nargs, ret,
7830 DEDUCE_EXACT, LOOKUP_NORMAL,
7831 NULL, false, false);
7832 if (instantiation == error_mark_node)
7833 /* Instantiation failed. */
7834 continue;
7836 /* Constraints must be satisfied. This is done before
7837 return type deduction since that instantiates the
7838 function. */
7839 if (flag_concepts && !constraints_satisfied_p (instantiation))
7840 continue;
7842 /* And now force instantiation to do return type deduction. */
7843 if (undeduced_auto_decl (instantiation))
7845 ++function_depth;
7846 instantiate_decl (instantiation, /*defer*/false, /*class*/false);
7847 --function_depth;
7849 require_deduced_type (instantiation);
7852 /* In C++17 we need the noexcept-qualifier to compare types. */
7853 if (flag_noexcept_type)
7854 maybe_instantiate_noexcept (instantiation, complain);
7856 /* See if there's a match. */
7857 tree fntype = static_fn_type (instantiation);
7858 if (same_type_p (target_fn_type, fntype)
7859 || fnptr_conv_p (target_fn_type, fntype))
7860 matches = tree_cons (instantiation, fn, matches);
7863 /* Now, remove all but the most specialized of the matches. */
7864 if (matches)
7866 tree match = most_specialized_instantiation (matches);
7868 if (match != error_mark_node)
7869 matches = tree_cons (TREE_PURPOSE (match),
7870 NULL_TREE,
7871 NULL_TREE);
7875 /* Now we should have exactly one function in MATCHES. */
7876 if (matches == NULL_TREE)
7878 /* There were *no* matches. */
7879 if (complain & tf_error)
7881 error ("no matches converting function %qD to type %q#T",
7882 OVL_NAME (overload), target_type);
7884 print_candidates (overload);
7886 return error_mark_node;
7888 else if (TREE_CHAIN (matches))
7890 /* There were too many matches. First check if they're all
7891 the same function. */
7892 tree match = NULL_TREE;
7894 fn = TREE_PURPOSE (matches);
7896 /* For multi-versioned functions, more than one match is just fine and
7897 decls_match will return false as they are different. */
7898 for (match = TREE_CHAIN (matches); match; match = TREE_CHAIN (match))
7899 if (!decls_match (fn, TREE_PURPOSE (match))
7900 && !targetm.target_option.function_versions
7901 (fn, TREE_PURPOSE (match)))
7902 break;
7904 if (match)
7906 if (complain & tf_error)
7908 error ("converting overloaded function %qD to type %q#T is ambiguous",
7909 OVL_NAME (overload), target_type);
7911 /* Since print_candidates expects the functions in the
7912 TREE_VALUE slot, we flip them here. */
7913 for (match = matches; match; match = TREE_CHAIN (match))
7914 TREE_VALUE (match) = TREE_PURPOSE (match);
7916 print_candidates (matches);
7919 return error_mark_node;
7923 /* Good, exactly one match. Now, convert it to the correct type. */
7924 fn = TREE_PURPOSE (matches);
7926 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
7927 && !(complain & tf_ptrmem_ok) && !flag_ms_extensions)
7929 static int explained;
7931 if (!(complain & tf_error))
7932 return error_mark_node;
7934 auto_diagnostic_group d;
7935 if (permerror (input_location, "assuming pointer to member %qD", fn)
7936 && !explained)
7938 inform (input_location, "(a pointer to member can only be "
7939 "formed with %<&%E%>)", fn);
7940 explained = 1;
7944 /* If a pointer to a function that is multi-versioned is requested, the
7945 pointer to the dispatcher function is returned instead. This works
7946 well because indirectly calling the function will dispatch the right
7947 function version at run-time. */
7948 if (DECL_FUNCTION_VERSIONED (fn))
7950 fn = get_function_version_dispatcher (fn);
7951 if (fn == NULL)
7952 return error_mark_node;
7953 /* Mark all the versions corresponding to the dispatcher as used. */
7954 if (!(complain & tf_conv))
7955 mark_versions_used (fn);
7958 /* If we're doing overload resolution purely for the purpose of
7959 determining conversion sequences, we should not consider the
7960 function used. If this conversion sequence is selected, the
7961 function will be marked as used at this point. */
7962 if (!(complain & tf_conv))
7964 /* Make =delete work with SFINAE. */
7965 if (DECL_DELETED_FN (fn) && !(complain & tf_error))
7966 return error_mark_node;
7967 if (!mark_used (fn, complain) && !(complain & tf_error))
7968 return error_mark_node;
7971 /* We could not check access to member functions when this
7972 expression was originally created since we did not know at that
7973 time to which function the expression referred. */
7974 if (DECL_FUNCTION_MEMBER_P (fn))
7976 gcc_assert (access_path);
7977 perform_or_defer_access_check (access_path, fn, fn, complain);
7980 if (TYPE_PTRFN_P (target_type) || TYPE_PTRMEMFUNC_P (target_type))
7981 return cp_build_addr_expr (fn, complain);
7982 else
7984 /* The target must be a REFERENCE_TYPE. Above, cp_build_unary_op
7985 will mark the function as addressed, but here we must do it
7986 explicitly. */
7987 cxx_mark_addressable (fn);
7989 return fn;
7993 /* This function will instantiate the type of the expression given in
7994 RHS to match the type of LHSTYPE. If errors exist, then return
7995 error_mark_node. COMPLAIN is a bit mask. If TF_ERROR is set, then
7996 we complain on errors. If we are not complaining, never modify rhs,
7997 as overload resolution wants to try many possible instantiations, in
7998 the hope that at least one will work.
8000 For non-recursive calls, LHSTYPE should be a function, pointer to
8001 function, or a pointer to member function. */
8003 tree
8004 instantiate_type (tree lhstype, tree rhs, tsubst_flags_t complain)
8006 tsubst_flags_t complain_in = complain;
8007 tree access_path = NULL_TREE;
8009 complain &= ~tf_ptrmem_ok;
8011 if (lhstype == unknown_type_node)
8013 if (complain & tf_error)
8014 error ("not enough type information");
8015 return error_mark_node;
8018 if (TREE_TYPE (rhs) != NULL_TREE && ! (type_unknown_p (rhs)))
8020 tree fntype = non_reference (lhstype);
8021 if (same_type_p (fntype, TREE_TYPE (rhs)))
8022 return rhs;
8023 if (fnptr_conv_p (fntype, TREE_TYPE (rhs)))
8024 return rhs;
8025 if (flag_ms_extensions
8026 && TYPE_PTRMEMFUNC_P (fntype)
8027 && !TYPE_PTRMEMFUNC_P (TREE_TYPE (rhs)))
8028 /* Microsoft allows `A::f' to be resolved to a
8029 pointer-to-member. */
8031 else
8033 if (complain & tf_error)
8034 error ("cannot convert %qE from type %qT to type %qT",
8035 rhs, TREE_TYPE (rhs), fntype);
8036 return error_mark_node;
8040 /* If we instantiate a template, and it is a A ?: C expression
8041 with omitted B, look through the SAVE_EXPR. */
8042 if (TREE_CODE (rhs) == SAVE_EXPR)
8043 rhs = TREE_OPERAND (rhs, 0);
8045 if (BASELINK_P (rhs))
8047 access_path = BASELINK_ACCESS_BINFO (rhs);
8048 rhs = BASELINK_FUNCTIONS (rhs);
8051 /* If we are in a template, and have a NON_DEPENDENT_EXPR, we cannot
8052 deduce any type information. */
8053 if (TREE_CODE (rhs) == NON_DEPENDENT_EXPR)
8055 if (complain & tf_error)
8056 error ("not enough type information");
8057 return error_mark_node;
8060 /* There are only a few kinds of expressions that may have a type
8061 dependent on overload resolution. */
8062 gcc_assert (TREE_CODE (rhs) == ADDR_EXPR
8063 || TREE_CODE (rhs) == COMPONENT_REF
8064 || is_overloaded_fn (rhs)
8065 || (flag_ms_extensions && TREE_CODE (rhs) == FUNCTION_DECL));
8067 /* This should really only be used when attempting to distinguish
8068 what sort of a pointer to function we have. For now, any
8069 arithmetic operation which is not supported on pointers
8070 is rejected as an error. */
8072 switch (TREE_CODE (rhs))
8074 case COMPONENT_REF:
8076 tree member = TREE_OPERAND (rhs, 1);
8078 member = instantiate_type (lhstype, member, complain);
8079 if (member != error_mark_node
8080 && TREE_SIDE_EFFECTS (TREE_OPERAND (rhs, 0)))
8081 /* Do not lose object's side effects. */
8082 return build2 (COMPOUND_EXPR, TREE_TYPE (member),
8083 TREE_OPERAND (rhs, 0), member);
8084 return member;
8087 case OFFSET_REF:
8088 rhs = TREE_OPERAND (rhs, 1);
8089 if (BASELINK_P (rhs))
8090 return instantiate_type (lhstype, rhs, complain_in);
8092 /* This can happen if we are forming a pointer-to-member for a
8093 member template. */
8094 gcc_assert (TREE_CODE (rhs) == TEMPLATE_ID_EXPR);
8096 /* Fall through. */
8098 case TEMPLATE_ID_EXPR:
8100 tree fns = TREE_OPERAND (rhs, 0);
8101 tree args = TREE_OPERAND (rhs, 1);
8103 return
8104 resolve_address_of_overloaded_function (lhstype, fns, complain_in,
8105 /*template_only=*/true,
8106 args, access_path);
8109 case OVERLOAD:
8110 case FUNCTION_DECL:
8111 return
8112 resolve_address_of_overloaded_function (lhstype, rhs, complain_in,
8113 /*template_only=*/false,
8114 /*explicit_targs=*/NULL_TREE,
8115 access_path);
8117 case ADDR_EXPR:
8119 if (PTRMEM_OK_P (rhs))
8120 complain |= tf_ptrmem_ok;
8122 return instantiate_type (lhstype, TREE_OPERAND (rhs, 0), complain);
8125 case ERROR_MARK:
8126 return error_mark_node;
8128 default:
8129 gcc_unreachable ();
8131 return error_mark_node;
8134 /* Return the name of the virtual function pointer field
8135 (as an IDENTIFIER_NODE) for the given TYPE. Note that
8136 this may have to look back through base types to find the
8137 ultimate field name. (For single inheritance, these could
8138 all be the same name. Who knows for multiple inheritance). */
8140 static tree
8141 get_vfield_name (tree type)
8143 tree binfo, base_binfo;
8145 for (binfo = TYPE_BINFO (type);
8146 BINFO_N_BASE_BINFOS (binfo);
8147 binfo = base_binfo)
8149 base_binfo = BINFO_BASE_BINFO (binfo, 0);
8151 if (BINFO_VIRTUAL_P (base_binfo)
8152 || !TYPE_CONTAINS_VPTR_P (BINFO_TYPE (base_binfo)))
8153 break;
8156 type = BINFO_TYPE (binfo);
8157 tree ctor_name = constructor_name (type);
8158 char *buf = (char *) alloca (sizeof (VFIELD_NAME_FORMAT)
8159 + IDENTIFIER_LENGTH (ctor_name) + 2);
8160 sprintf (buf, VFIELD_NAME_FORMAT, IDENTIFIER_POINTER (ctor_name));
8161 return get_identifier (buf);
8164 /* Build a dummy reference to ourselves so Derived::Base (and A::A) works,
8165 according to [class]:
8166 The class-name is also inserted
8167 into the scope of the class itself. For purposes of access checking,
8168 the inserted class name is treated as if it were a public member name. */
8170 void
8171 build_self_reference (void)
8173 tree name = DECL_NAME (TYPE_NAME (current_class_type));
8174 tree value = build_lang_decl (TYPE_DECL, name, current_class_type);
8176 DECL_NONLOCAL (value) = 1;
8177 DECL_CONTEXT (value) = current_class_type;
8178 DECL_ARTIFICIAL (value) = 1;
8179 SET_DECL_SELF_REFERENCE_P (value);
8180 set_underlying_type (value);
8182 if (processing_template_decl)
8183 value = push_template_decl (value);
8185 tree saved_cas = current_access_specifier;
8186 current_access_specifier = access_public_node;
8187 finish_member_declaration (value);
8188 current_access_specifier = saved_cas;
8191 /* Returns 1 if TYPE contains only padding bytes. */
8194 is_empty_class (tree type)
8196 if (type == error_mark_node)
8197 return 0;
8199 if (! CLASS_TYPE_P (type))
8200 return 0;
8202 return CLASSTYPE_EMPTY_P (type);
8205 /* Returns true if TYPE contains no actual data, just various
8206 possible combinations of empty classes and possibly a vptr. */
8208 bool
8209 is_really_empty_class (tree type)
8211 if (CLASS_TYPE_P (type))
8213 tree field;
8214 tree binfo;
8215 tree base_binfo;
8216 int i;
8218 /* CLASSTYPE_EMPTY_P isn't set properly until the class is actually laid
8219 out, but we'd like to be able to check this before then. */
8220 if (COMPLETE_TYPE_P (type) && is_empty_class (type))
8221 return true;
8223 for (binfo = TYPE_BINFO (type), i = 0;
8224 BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
8225 if (!is_really_empty_class (BINFO_TYPE (base_binfo)))
8226 return false;
8227 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
8228 if (TREE_CODE (field) == FIELD_DECL
8229 && !DECL_ARTIFICIAL (field)
8230 /* An unnamed bit-field is not a data member. */
8231 && !DECL_UNNAMED_BIT_FIELD (field)
8232 && !is_really_empty_class (TREE_TYPE (field)))
8233 return false;
8234 return true;
8236 else if (TREE_CODE (type) == ARRAY_TYPE)
8237 return (integer_zerop (array_type_nelts_top (type))
8238 || is_really_empty_class (TREE_TYPE (type)));
8239 return false;
8242 /* Note that NAME was looked up while the current class was being
8243 defined and that the result of that lookup was DECL. */
8245 void
8246 maybe_note_name_used_in_class (tree name, tree decl)
8248 splay_tree names_used;
8250 /* If we're not defining a class, there's nothing to do. */
8251 if (!(innermost_scope_kind() == sk_class
8252 && TYPE_BEING_DEFINED (current_class_type)
8253 && !LAMBDA_TYPE_P (current_class_type)))
8254 return;
8256 /* If there's already a binding for this NAME, then we don't have
8257 anything to worry about. */
8258 if (lookup_member (current_class_type, name,
8259 /*protect=*/0, /*want_type=*/false, tf_warning_or_error))
8260 return;
8262 if (!current_class_stack[current_class_depth - 1].names_used)
8263 current_class_stack[current_class_depth - 1].names_used
8264 = splay_tree_new (splay_tree_compare_pointers, 0, 0);
8265 names_used = current_class_stack[current_class_depth - 1].names_used;
8267 splay_tree_insert (names_used,
8268 (splay_tree_key) name,
8269 (splay_tree_value) decl);
8272 /* Note that NAME was declared (as DECL) in the current class. Check
8273 to see that the declaration is valid. */
8275 void
8276 note_name_declared_in_class (tree name, tree decl)
8278 splay_tree names_used;
8279 splay_tree_node n;
8281 /* Look to see if we ever used this name. */
8282 names_used
8283 = current_class_stack[current_class_depth - 1].names_used;
8284 if (!names_used)
8285 return;
8286 /* The C language allows members to be declared with a type of the same
8287 name, and the C++ standard says this diagnostic is not required. So
8288 allow it in extern "C" blocks unless predantic is specified.
8289 Allow it in all cases if -ms-extensions is specified. */
8290 if ((!pedantic && current_lang_name == lang_name_c)
8291 || flag_ms_extensions)
8292 return;
8293 n = splay_tree_lookup (names_used, (splay_tree_key) name);
8294 if (n)
8296 /* [basic.scope.class]
8298 A name N used in a class S shall refer to the same declaration
8299 in its context and when re-evaluated in the completed scope of
8300 S. */
8301 if (permerror (location_of (decl),
8302 "declaration of %q#D changes meaning of %qD",
8303 decl, OVL_NAME (decl)))
8304 inform (location_of ((tree) n->value),
8305 "%qD declared here as %q#D",
8306 OVL_NAME (decl), (tree) n->value);
8310 /* Returns the VAR_DECL for the complete vtable associated with BINFO.
8311 Secondary vtables are merged with primary vtables; this function
8312 will return the VAR_DECL for the primary vtable. */
8314 tree
8315 get_vtbl_decl_for_binfo (tree binfo)
8317 tree decl;
8319 decl = BINFO_VTABLE (binfo);
8320 if (decl && TREE_CODE (decl) == POINTER_PLUS_EXPR)
8322 gcc_assert (TREE_CODE (TREE_OPERAND (decl, 0)) == ADDR_EXPR);
8323 decl = TREE_OPERAND (TREE_OPERAND (decl, 0), 0);
8325 if (decl)
8326 gcc_assert (VAR_P (decl));
8327 return decl;
8331 /* Returns the binfo for the primary base of BINFO. If the resulting
8332 BINFO is a virtual base, and it is inherited elsewhere in the
8333 hierarchy, then the returned binfo might not be the primary base of
8334 BINFO in the complete object. Check BINFO_PRIMARY_P or
8335 BINFO_LOST_PRIMARY_P to be sure. */
8337 static tree
8338 get_primary_binfo (tree binfo)
8340 tree primary_base;
8342 primary_base = CLASSTYPE_PRIMARY_BINFO (BINFO_TYPE (binfo));
8343 if (!primary_base)
8344 return NULL_TREE;
8346 return copied_binfo (primary_base, binfo);
8349 /* As above, but iterate until we reach the binfo that actually provides the
8350 vptr for BINFO. */
8352 static tree
8353 most_primary_binfo (tree binfo)
8355 tree b = binfo;
8356 while (CLASSTYPE_HAS_PRIMARY_BASE_P (BINFO_TYPE (b))
8357 && !BINFO_LOST_PRIMARY_P (b))
8359 tree primary_base = get_primary_binfo (b);
8360 gcc_assert (BINFO_PRIMARY_P (primary_base)
8361 && BINFO_INHERITANCE_CHAIN (primary_base) == b);
8362 b = primary_base;
8364 return b;
8367 /* Returns true if BINFO gets its vptr from a virtual base of the most derived
8368 type. Note that the virtual inheritance might be above or below BINFO in
8369 the hierarchy. */
8371 bool
8372 vptr_via_virtual_p (tree binfo)
8374 if (TYPE_P (binfo))
8375 binfo = TYPE_BINFO (binfo);
8376 tree primary = most_primary_binfo (binfo);
8377 /* Don't limit binfo_via_virtual, we want to return true when BINFO itself is
8378 a morally virtual base. */
8379 tree virt = binfo_via_virtual (primary, NULL_TREE);
8380 return virt != NULL_TREE;
8383 /* If INDENTED_P is zero, indent to INDENT. Return nonzero. */
8385 static int
8386 maybe_indent_hierarchy (FILE * stream, int indent, int indented_p)
8388 if (!indented_p)
8389 fprintf (stream, "%*s", indent, "");
8390 return 1;
8393 /* Dump the offsets of all the bases rooted at BINFO to STREAM.
8394 INDENT should be zero when called from the top level; it is
8395 incremented recursively. IGO indicates the next expected BINFO in
8396 inheritance graph ordering. */
8398 static tree
8399 dump_class_hierarchy_r (FILE *stream,
8400 dump_flags_t flags,
8401 tree binfo,
8402 tree igo,
8403 int indent)
8405 int indented = 0;
8406 tree base_binfo;
8407 int i;
8409 indented = maybe_indent_hierarchy (stream, indent, 0);
8410 fprintf (stream, "%s (0x" HOST_WIDE_INT_PRINT_HEX ") ",
8411 type_as_string (BINFO_TYPE (binfo), TFF_PLAIN_IDENTIFIER),
8412 (HOST_WIDE_INT) (uintptr_t) binfo);
8413 if (binfo != igo)
8415 fprintf (stream, "alternative-path\n");
8416 return igo;
8418 igo = TREE_CHAIN (binfo);
8420 fprintf (stream, HOST_WIDE_INT_PRINT_DEC,
8421 tree_to_shwi (BINFO_OFFSET (binfo)));
8422 if (is_empty_class (BINFO_TYPE (binfo)))
8423 fprintf (stream, " empty");
8424 else if (CLASSTYPE_NEARLY_EMPTY_P (BINFO_TYPE (binfo)))
8425 fprintf (stream, " nearly-empty");
8426 if (BINFO_VIRTUAL_P (binfo))
8427 fprintf (stream, " virtual");
8428 fprintf (stream, "\n");
8430 indented = 0;
8431 if (BINFO_PRIMARY_P (binfo))
8433 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
8434 fprintf (stream, " primary-for %s (0x" HOST_WIDE_INT_PRINT_HEX ")",
8435 type_as_string (BINFO_TYPE (BINFO_INHERITANCE_CHAIN (binfo)),
8436 TFF_PLAIN_IDENTIFIER),
8437 (HOST_WIDE_INT) (uintptr_t) BINFO_INHERITANCE_CHAIN (binfo));
8439 if (BINFO_LOST_PRIMARY_P (binfo))
8441 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
8442 fprintf (stream, " lost-primary");
8444 if (indented)
8445 fprintf (stream, "\n");
8447 if (!(flags & TDF_SLIM))
8449 int indented = 0;
8451 if (BINFO_SUBVTT_INDEX (binfo))
8453 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
8454 fprintf (stream, " subvttidx=%s",
8455 expr_as_string (BINFO_SUBVTT_INDEX (binfo),
8456 TFF_PLAIN_IDENTIFIER));
8458 if (BINFO_VPTR_INDEX (binfo))
8460 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
8461 fprintf (stream, " vptridx=%s",
8462 expr_as_string (BINFO_VPTR_INDEX (binfo),
8463 TFF_PLAIN_IDENTIFIER));
8465 if (BINFO_VPTR_FIELD (binfo))
8467 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
8468 fprintf (stream, " vbaseoffset=%s",
8469 expr_as_string (BINFO_VPTR_FIELD (binfo),
8470 TFF_PLAIN_IDENTIFIER));
8472 if (BINFO_VTABLE (binfo))
8474 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
8475 fprintf (stream, " vptr=%s",
8476 expr_as_string (BINFO_VTABLE (binfo),
8477 TFF_PLAIN_IDENTIFIER));
8480 if (indented)
8481 fprintf (stream, "\n");
8484 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
8485 igo = dump_class_hierarchy_r (stream, flags, base_binfo, igo, indent + 2);
8487 return igo;
8490 /* Dump the BINFO hierarchy for T. */
8492 static void
8493 dump_class_hierarchy_1 (FILE *stream, dump_flags_t flags, tree t)
8495 fprintf (stream, "Class %s\n", type_as_string (t, TFF_PLAIN_IDENTIFIER));
8496 fprintf (stream, " size=%lu align=%lu\n",
8497 (unsigned long)(tree_to_shwi (TYPE_SIZE (t)) / BITS_PER_UNIT),
8498 (unsigned long)(TYPE_ALIGN (t) / BITS_PER_UNIT));
8499 fprintf (stream, " base size=%lu base align=%lu\n",
8500 (unsigned long)(tree_to_shwi (TYPE_SIZE (CLASSTYPE_AS_BASE (t)))
8501 / BITS_PER_UNIT),
8502 (unsigned long)(TYPE_ALIGN (CLASSTYPE_AS_BASE (t))
8503 / BITS_PER_UNIT));
8504 dump_class_hierarchy_r (stream, flags, TYPE_BINFO (t), TYPE_BINFO (t), 0);
8505 fprintf (stream, "\n");
8508 /* Debug interface to hierarchy dumping. */
8510 void
8511 debug_class (tree t)
8513 dump_class_hierarchy_1 (stderr, TDF_SLIM, t);
8516 static void
8517 dump_class_hierarchy (tree t)
8519 dump_flags_t flags;
8520 if (FILE *stream = dump_begin (class_dump_id, &flags))
8522 dump_class_hierarchy_1 (stream, flags, t);
8523 dump_end (class_dump_id, stream);
8527 static void
8528 dump_array (FILE * stream, tree decl)
8530 tree value;
8531 unsigned HOST_WIDE_INT ix;
8532 HOST_WIDE_INT elt;
8533 tree size = TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (decl)));
8535 elt = (tree_to_shwi (TYPE_SIZE (TREE_TYPE (TREE_TYPE (decl))))
8536 / BITS_PER_UNIT);
8537 fprintf (stream, "%s:", decl_as_string (decl, TFF_PLAIN_IDENTIFIER));
8538 fprintf (stream, " %s entries",
8539 expr_as_string (size_binop (PLUS_EXPR, size, size_one_node),
8540 TFF_PLAIN_IDENTIFIER));
8541 fprintf (stream, "\n");
8543 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (DECL_INITIAL (decl)),
8544 ix, value)
8545 fprintf (stream, "%-4ld %s\n", (long)(ix * elt),
8546 expr_as_string (value, TFF_PLAIN_IDENTIFIER));
8549 static void
8550 dump_vtable (tree t, tree binfo, tree vtable)
8552 dump_flags_t flags;
8553 FILE *stream = dump_begin (class_dump_id, &flags);
8555 if (!stream)
8556 return;
8558 if (!(flags & TDF_SLIM))
8560 int ctor_vtbl_p = TYPE_BINFO (t) != binfo;
8562 fprintf (stream, "%s for %s",
8563 ctor_vtbl_p ? "Construction vtable" : "Vtable",
8564 type_as_string (BINFO_TYPE (binfo), TFF_PLAIN_IDENTIFIER));
8565 if (ctor_vtbl_p)
8567 if (!BINFO_VIRTUAL_P (binfo))
8568 fprintf (stream, " (0x" HOST_WIDE_INT_PRINT_HEX " instance)",
8569 (HOST_WIDE_INT) (uintptr_t) binfo);
8570 fprintf (stream, " in %s", type_as_string (t, TFF_PLAIN_IDENTIFIER));
8572 fprintf (stream, "\n");
8573 dump_array (stream, vtable);
8574 fprintf (stream, "\n");
8577 dump_end (class_dump_id, stream);
8580 static void
8581 dump_vtt (tree t, tree vtt)
8583 dump_flags_t flags;
8584 FILE *stream = dump_begin (class_dump_id, &flags);
8586 if (!stream)
8587 return;
8589 if (!(flags & TDF_SLIM))
8591 fprintf (stream, "VTT for %s\n",
8592 type_as_string (t, TFF_PLAIN_IDENTIFIER));
8593 dump_array (stream, vtt);
8594 fprintf (stream, "\n");
8597 dump_end (class_dump_id, stream);
8600 /* Dump a function or thunk and its thunkees. */
8602 static void
8603 dump_thunk (FILE *stream, int indent, tree thunk)
8605 static const char spaces[] = " ";
8606 tree name = DECL_NAME (thunk);
8607 tree thunks;
8609 fprintf (stream, "%.*s%p %s %s", indent, spaces,
8610 (void *)thunk,
8611 !DECL_THUNK_P (thunk) ? "function"
8612 : DECL_THIS_THUNK_P (thunk) ? "this-thunk" : "covariant-thunk",
8613 name ? IDENTIFIER_POINTER (name) : "<unset>");
8614 if (DECL_THUNK_P (thunk))
8616 HOST_WIDE_INT fixed_adjust = THUNK_FIXED_OFFSET (thunk);
8617 tree virtual_adjust = THUNK_VIRTUAL_OFFSET (thunk);
8619 fprintf (stream, " fixed=" HOST_WIDE_INT_PRINT_DEC, fixed_adjust);
8620 if (!virtual_adjust)
8621 /*NOP*/;
8622 else if (DECL_THIS_THUNK_P (thunk))
8623 fprintf (stream, " vcall=" HOST_WIDE_INT_PRINT_DEC,
8624 tree_to_shwi (virtual_adjust));
8625 else
8626 fprintf (stream, " vbase=" HOST_WIDE_INT_PRINT_DEC "(%s)",
8627 tree_to_shwi (BINFO_VPTR_FIELD (virtual_adjust)),
8628 type_as_string (BINFO_TYPE (virtual_adjust), TFF_SCOPE));
8629 if (THUNK_ALIAS (thunk))
8630 fprintf (stream, " alias to %p", (void *)THUNK_ALIAS (thunk));
8632 fprintf (stream, "\n");
8633 for (thunks = DECL_THUNKS (thunk); thunks; thunks = TREE_CHAIN (thunks))
8634 dump_thunk (stream, indent + 2, thunks);
8637 /* Dump the thunks for FN. */
8639 void
8640 debug_thunks (tree fn)
8642 dump_thunk (stderr, 0, fn);
8645 /* Virtual function table initialization. */
8647 /* Create all the necessary vtables for T and its base classes. */
8649 static void
8650 finish_vtbls (tree t)
8652 tree vbase;
8653 vec<constructor_elt, va_gc> *v = NULL;
8654 tree vtable = BINFO_VTABLE (TYPE_BINFO (t));
8656 /* We lay out the primary and secondary vtables in one contiguous
8657 vtable. The primary vtable is first, followed by the non-virtual
8658 secondary vtables in inheritance graph order. */
8659 accumulate_vtbl_inits (TYPE_BINFO (t), TYPE_BINFO (t), TYPE_BINFO (t),
8660 vtable, t, &v);
8662 /* Then come the virtual bases, also in inheritance graph order. */
8663 for (vbase = TYPE_BINFO (t); vbase; vbase = TREE_CHAIN (vbase))
8665 if (!BINFO_VIRTUAL_P (vbase))
8666 continue;
8667 accumulate_vtbl_inits (vbase, vbase, TYPE_BINFO (t), vtable, t, &v);
8670 if (BINFO_VTABLE (TYPE_BINFO (t)))
8671 initialize_vtable (TYPE_BINFO (t), v);
8674 /* Initialize the vtable for BINFO with the INITS. */
8676 static void
8677 initialize_vtable (tree binfo, vec<constructor_elt, va_gc> *inits)
8679 tree decl;
8681 layout_vtable_decl (binfo, vec_safe_length (inits));
8682 decl = get_vtbl_decl_for_binfo (binfo);
8683 initialize_artificial_var (decl, inits);
8684 dump_vtable (BINFO_TYPE (binfo), binfo, decl);
8687 /* Build the VTT (virtual table table) for T.
8688 A class requires a VTT if it has virtual bases.
8690 This holds
8691 1 - primary virtual pointer for complete object T
8692 2 - secondary VTTs for each direct non-virtual base of T which requires a
8694 3 - secondary virtual pointers for each direct or indirect base of T which
8695 has virtual bases or is reachable via a virtual path from T.
8696 4 - secondary VTTs for each direct or indirect virtual base of T.
8698 Secondary VTTs look like complete object VTTs without part 4. */
8700 static void
8701 build_vtt (tree t)
8703 tree type;
8704 tree vtt;
8705 tree index;
8706 vec<constructor_elt, va_gc> *inits;
8708 /* Build up the initializers for the VTT. */
8709 inits = NULL;
8710 index = size_zero_node;
8711 build_vtt_inits (TYPE_BINFO (t), t, &inits, &index);
8713 /* If we didn't need a VTT, we're done. */
8714 if (!inits)
8715 return;
8717 /* Figure out the type of the VTT. */
8718 type = build_array_of_n_type (const_ptr_type_node,
8719 inits->length ());
8721 /* Now, build the VTT object itself. */
8722 vtt = build_vtable (t, mangle_vtt_for_type (t), type);
8723 initialize_artificial_var (vtt, inits);
8724 /* Add the VTT to the vtables list. */
8725 DECL_CHAIN (vtt) = DECL_CHAIN (CLASSTYPE_VTABLES (t));
8726 DECL_CHAIN (CLASSTYPE_VTABLES (t)) = vtt;
8728 dump_vtt (t, vtt);
8731 /* When building a secondary VTT, BINFO_VTABLE is set to a TREE_LIST with
8732 PURPOSE the RTTI_BINFO, VALUE the real vtable pointer for this binfo,
8733 and CHAIN the vtable pointer for this binfo after construction is
8734 complete. VALUE can also be another BINFO, in which case we recurse. */
8736 static tree
8737 binfo_ctor_vtable (tree binfo)
8739 tree vt;
8741 while (1)
8743 vt = BINFO_VTABLE (binfo);
8744 if (TREE_CODE (vt) == TREE_LIST)
8745 vt = TREE_VALUE (vt);
8746 if (TREE_CODE (vt) == TREE_BINFO)
8747 binfo = vt;
8748 else
8749 break;
8752 return vt;
8755 /* Data for secondary VTT initialization. */
8756 struct secondary_vptr_vtt_init_data
8758 /* Is this the primary VTT? */
8759 bool top_level_p;
8761 /* Current index into the VTT. */
8762 tree index;
8764 /* Vector of initializers built up. */
8765 vec<constructor_elt, va_gc> *inits;
8767 /* The type being constructed by this secondary VTT. */
8768 tree type_being_constructed;
8771 /* Recursively build the VTT-initializer for BINFO (which is in the
8772 hierarchy dominated by T). INITS points to the end of the initializer
8773 list to date. INDEX is the VTT index where the next element will be
8774 replaced. Iff BINFO is the binfo for T, this is the top level VTT (i.e.
8775 not a subvtt for some base of T). When that is so, we emit the sub-VTTs
8776 for virtual bases of T. When it is not so, we build the constructor
8777 vtables for the BINFO-in-T variant. */
8779 static void
8780 build_vtt_inits (tree binfo, tree t, vec<constructor_elt, va_gc> **inits,
8781 tree *index)
8783 int i;
8784 tree b;
8785 tree init;
8786 secondary_vptr_vtt_init_data data;
8787 int top_level_p = SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), t);
8789 /* We only need VTTs for subobjects with virtual bases. */
8790 if (!CLASSTYPE_VBASECLASSES (BINFO_TYPE (binfo)))
8791 return;
8793 /* We need to use a construction vtable if this is not the primary
8794 VTT. */
8795 if (!top_level_p)
8797 build_ctor_vtbl_group (binfo, t);
8799 /* Record the offset in the VTT where this sub-VTT can be found. */
8800 BINFO_SUBVTT_INDEX (binfo) = *index;
8803 /* Add the address of the primary vtable for the complete object. */
8804 init = binfo_ctor_vtable (binfo);
8805 CONSTRUCTOR_APPEND_ELT (*inits, NULL_TREE, init);
8806 if (top_level_p)
8808 gcc_assert (!BINFO_VPTR_INDEX (binfo));
8809 BINFO_VPTR_INDEX (binfo) = *index;
8811 *index = size_binop (PLUS_EXPR, *index, TYPE_SIZE_UNIT (ptr_type_node));
8813 /* Recursively add the secondary VTTs for non-virtual bases. */
8814 for (i = 0; BINFO_BASE_ITERATE (binfo, i, b); ++i)
8815 if (!BINFO_VIRTUAL_P (b))
8816 build_vtt_inits (b, t, inits, index);
8818 /* Add secondary virtual pointers for all subobjects of BINFO with
8819 either virtual bases or reachable along a virtual path, except
8820 subobjects that are non-virtual primary bases. */
8821 data.top_level_p = top_level_p;
8822 data.index = *index;
8823 data.inits = *inits;
8824 data.type_being_constructed = BINFO_TYPE (binfo);
8826 dfs_walk_once (binfo, dfs_build_secondary_vptr_vtt_inits, NULL, &data);
8828 *index = data.index;
8830 /* data.inits might have grown as we added secondary virtual pointers.
8831 Make sure our caller knows about the new vector. */
8832 *inits = data.inits;
8834 if (top_level_p)
8835 /* Add the secondary VTTs for virtual bases in inheritance graph
8836 order. */
8837 for (b = TYPE_BINFO (BINFO_TYPE (binfo)); b; b = TREE_CHAIN (b))
8839 if (!BINFO_VIRTUAL_P (b))
8840 continue;
8842 build_vtt_inits (b, t, inits, index);
8844 else
8845 /* Remove the ctor vtables we created. */
8846 dfs_walk_all (binfo, dfs_fixup_binfo_vtbls, NULL, binfo);
8849 /* Called from build_vtt_inits via dfs_walk. BINFO is the binfo for the base
8850 in most derived. DATA is a SECONDARY_VPTR_VTT_INIT_DATA structure. */
8852 static tree
8853 dfs_build_secondary_vptr_vtt_inits (tree binfo, void *data_)
8855 secondary_vptr_vtt_init_data *data = (secondary_vptr_vtt_init_data *)data_;
8857 /* We don't care about bases that don't have vtables. */
8858 if (!TYPE_VFIELD (BINFO_TYPE (binfo)))
8859 return dfs_skip_bases;
8861 /* We're only interested in proper subobjects of the type being
8862 constructed. */
8863 if (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), data->type_being_constructed))
8864 return NULL_TREE;
8866 /* We're only interested in bases with virtual bases or reachable
8867 via a virtual path from the type being constructed. */
8868 if (!(CLASSTYPE_VBASECLASSES (BINFO_TYPE (binfo))
8869 || binfo_via_virtual (binfo, data->type_being_constructed)))
8870 return dfs_skip_bases;
8872 /* We're not interested in non-virtual primary bases. */
8873 if (!BINFO_VIRTUAL_P (binfo) && BINFO_PRIMARY_P (binfo))
8874 return NULL_TREE;
8876 /* Record the index where this secondary vptr can be found. */
8877 if (data->top_level_p)
8879 gcc_assert (!BINFO_VPTR_INDEX (binfo));
8880 BINFO_VPTR_INDEX (binfo) = data->index;
8882 if (BINFO_VIRTUAL_P (binfo))
8884 /* It's a primary virtual base, and this is not a
8885 construction vtable. Find the base this is primary of in
8886 the inheritance graph, and use that base's vtable
8887 now. */
8888 while (BINFO_PRIMARY_P (binfo))
8889 binfo = BINFO_INHERITANCE_CHAIN (binfo);
8893 /* Add the initializer for the secondary vptr itself. */
8894 CONSTRUCTOR_APPEND_ELT (data->inits, NULL_TREE, binfo_ctor_vtable (binfo));
8896 /* Advance the vtt index. */
8897 data->index = size_binop (PLUS_EXPR, data->index,
8898 TYPE_SIZE_UNIT (ptr_type_node));
8900 return NULL_TREE;
8903 /* Called from build_vtt_inits via dfs_walk. After building
8904 constructor vtables and generating the sub-vtt from them, we need
8905 to restore the BINFO_VTABLES that were scribbled on. DATA is the
8906 binfo of the base whose sub vtt was generated. */
8908 static tree
8909 dfs_fixup_binfo_vtbls (tree binfo, void* data)
8911 tree vtable = BINFO_VTABLE (binfo);
8913 if (!TYPE_CONTAINS_VPTR_P (BINFO_TYPE (binfo)))
8914 /* If this class has no vtable, none of its bases do. */
8915 return dfs_skip_bases;
8917 if (!vtable)
8918 /* This might be a primary base, so have no vtable in this
8919 hierarchy. */
8920 return NULL_TREE;
8922 /* If we scribbled the construction vtable vptr into BINFO, clear it
8923 out now. */
8924 if (TREE_CODE (vtable) == TREE_LIST
8925 && (TREE_PURPOSE (vtable) == (tree) data))
8926 BINFO_VTABLE (binfo) = TREE_CHAIN (vtable);
8928 return NULL_TREE;
8931 /* Build the construction vtable group for BINFO which is in the
8932 hierarchy dominated by T. */
8934 static void
8935 build_ctor_vtbl_group (tree binfo, tree t)
8937 tree type;
8938 tree vtbl;
8939 tree id;
8940 tree vbase;
8941 vec<constructor_elt, va_gc> *v;
8943 /* See if we've already created this construction vtable group. */
8944 id = mangle_ctor_vtbl_for_type (t, binfo);
8945 if (get_global_binding (id))
8946 return;
8948 gcc_assert (!SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), t));
8949 /* Build a version of VTBL (with the wrong type) for use in
8950 constructing the addresses of secondary vtables in the
8951 construction vtable group. */
8952 vtbl = build_vtable (t, id, ptr_type_node);
8953 DECL_CONSTRUCTION_VTABLE_P (vtbl) = 1;
8954 /* Don't export construction vtables from shared libraries. Even on
8955 targets that don't support hidden visibility, this tells
8956 can_refer_decl_in_current_unit_p not to assume that it's safe to
8957 access from a different compilation unit (bz 54314). */
8958 DECL_VISIBILITY (vtbl) = VISIBILITY_HIDDEN;
8959 DECL_VISIBILITY_SPECIFIED (vtbl) = true;
8961 v = NULL;
8962 accumulate_vtbl_inits (binfo, TYPE_BINFO (TREE_TYPE (binfo)),
8963 binfo, vtbl, t, &v);
8965 /* Add the vtables for each of our virtual bases using the vbase in T
8966 binfo. */
8967 for (vbase = TYPE_BINFO (BINFO_TYPE (binfo));
8968 vbase;
8969 vbase = TREE_CHAIN (vbase))
8971 tree b;
8973 if (!BINFO_VIRTUAL_P (vbase))
8974 continue;
8975 b = copied_binfo (vbase, binfo);
8977 accumulate_vtbl_inits (b, vbase, binfo, vtbl, t, &v);
8980 /* Figure out the type of the construction vtable. */
8981 type = build_array_of_n_type (vtable_entry_type, v->length ());
8982 layout_type (type);
8983 TREE_TYPE (vtbl) = type;
8984 DECL_SIZE (vtbl) = DECL_SIZE_UNIT (vtbl) = NULL_TREE;
8985 layout_decl (vtbl, 0);
8987 /* Initialize the construction vtable. */
8988 CLASSTYPE_VTABLES (t) = chainon (CLASSTYPE_VTABLES (t), vtbl);
8989 initialize_artificial_var (vtbl, v);
8990 dump_vtable (t, binfo, vtbl);
8993 /* Add the vtbl initializers for BINFO (and its bases other than
8994 non-virtual primaries) to the list of INITS. BINFO is in the
8995 hierarchy dominated by T. RTTI_BINFO is the binfo within T of
8996 the constructor the vtbl inits should be accumulated for. (If this
8997 is the complete object vtbl then RTTI_BINFO will be TYPE_BINFO (T).)
8998 ORIG_BINFO is the binfo for this object within BINFO_TYPE (RTTI_BINFO).
8999 BINFO is the active base equivalent of ORIG_BINFO in the inheritance
9000 graph of T. Both BINFO and ORIG_BINFO will have the same BINFO_TYPE,
9001 but are not necessarily the same in terms of layout. */
9003 static void
9004 accumulate_vtbl_inits (tree binfo,
9005 tree orig_binfo,
9006 tree rtti_binfo,
9007 tree vtbl,
9008 tree t,
9009 vec<constructor_elt, va_gc> **inits)
9011 int i;
9012 tree base_binfo;
9013 int ctor_vtbl_p = !SAME_BINFO_TYPE_P (BINFO_TYPE (rtti_binfo), t);
9015 gcc_assert (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), BINFO_TYPE (orig_binfo)));
9017 /* If it doesn't have a vptr, we don't do anything. */
9018 if (!TYPE_CONTAINS_VPTR_P (BINFO_TYPE (binfo)))
9019 return;
9021 /* If we're building a construction vtable, we're not interested in
9022 subobjects that don't require construction vtables. */
9023 if (ctor_vtbl_p
9024 && !CLASSTYPE_VBASECLASSES (BINFO_TYPE (binfo))
9025 && !binfo_via_virtual (orig_binfo, BINFO_TYPE (rtti_binfo)))
9026 return;
9028 /* Build the initializers for the BINFO-in-T vtable. */
9029 dfs_accumulate_vtbl_inits (binfo, orig_binfo, rtti_binfo, vtbl, t, inits);
9031 /* Walk the BINFO and its bases. We walk in preorder so that as we
9032 initialize each vtable we can figure out at what offset the
9033 secondary vtable lies from the primary vtable. We can't use
9034 dfs_walk here because we need to iterate through bases of BINFO
9035 and RTTI_BINFO simultaneously. */
9036 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
9038 /* Skip virtual bases. */
9039 if (BINFO_VIRTUAL_P (base_binfo))
9040 continue;
9041 accumulate_vtbl_inits (base_binfo,
9042 BINFO_BASE_BINFO (orig_binfo, i),
9043 rtti_binfo, vtbl, t,
9044 inits);
9048 /* Called from accumulate_vtbl_inits. Adds the initializers for the
9049 BINFO vtable to L. */
9051 static void
9052 dfs_accumulate_vtbl_inits (tree binfo,
9053 tree orig_binfo,
9054 tree rtti_binfo,
9055 tree orig_vtbl,
9056 tree t,
9057 vec<constructor_elt, va_gc> **l)
9059 tree vtbl = NULL_TREE;
9060 int ctor_vtbl_p = !SAME_BINFO_TYPE_P (BINFO_TYPE (rtti_binfo), t);
9061 int n_inits;
9063 if (ctor_vtbl_p
9064 && BINFO_VIRTUAL_P (orig_binfo) && BINFO_PRIMARY_P (orig_binfo))
9066 /* In the hierarchy of BINFO_TYPE (RTTI_BINFO), this is a
9067 primary virtual base. If it is not the same primary in
9068 the hierarchy of T, we'll need to generate a ctor vtable
9069 for it, to place at its location in T. If it is the same
9070 primary, we still need a VTT entry for the vtable, but it
9071 should point to the ctor vtable for the base it is a
9072 primary for within the sub-hierarchy of RTTI_BINFO.
9074 There are three possible cases:
9076 1) We are in the same place.
9077 2) We are a primary base within a lost primary virtual base of
9078 RTTI_BINFO.
9079 3) We are primary to something not a base of RTTI_BINFO. */
9081 tree b;
9082 tree last = NULL_TREE;
9084 /* First, look through the bases we are primary to for RTTI_BINFO
9085 or a virtual base. */
9086 b = binfo;
9087 while (BINFO_PRIMARY_P (b))
9089 b = BINFO_INHERITANCE_CHAIN (b);
9090 last = b;
9091 if (BINFO_VIRTUAL_P (b) || b == rtti_binfo)
9092 goto found;
9094 /* If we run out of primary links, keep looking down our
9095 inheritance chain; we might be an indirect primary. */
9096 for (b = last; b; b = BINFO_INHERITANCE_CHAIN (b))
9097 if (BINFO_VIRTUAL_P (b) || b == rtti_binfo)
9098 break;
9099 found:
9101 /* If we found RTTI_BINFO, this is case 1. If we found a virtual
9102 base B and it is a base of RTTI_BINFO, this is case 2. In
9103 either case, we share our vtable with LAST, i.e. the
9104 derived-most base within B of which we are a primary. */
9105 if (b == rtti_binfo
9106 || (b && binfo_for_vbase (BINFO_TYPE (b), BINFO_TYPE (rtti_binfo))))
9107 /* Just set our BINFO_VTABLE to point to LAST, as we may not have
9108 set LAST's BINFO_VTABLE yet. We'll extract the actual vptr in
9109 binfo_ctor_vtable after everything's been set up. */
9110 vtbl = last;
9112 /* Otherwise, this is case 3 and we get our own. */
9114 else if (!BINFO_NEW_VTABLE_MARKED (orig_binfo))
9115 return;
9117 n_inits = vec_safe_length (*l);
9119 if (!vtbl)
9121 tree index;
9122 int non_fn_entries;
9124 /* Add the initializer for this vtable. */
9125 build_vtbl_initializer (binfo, orig_binfo, t, rtti_binfo,
9126 &non_fn_entries, l);
9128 /* Figure out the position to which the VPTR should point. */
9129 vtbl = build1 (ADDR_EXPR, vtbl_ptr_type_node, orig_vtbl);
9130 index = size_binop (MULT_EXPR,
9131 TYPE_SIZE_UNIT (vtable_entry_type),
9132 size_int (non_fn_entries + n_inits));
9133 vtbl = fold_build_pointer_plus (vtbl, index);
9136 if (ctor_vtbl_p)
9137 /* For a construction vtable, we can't overwrite BINFO_VTABLE.
9138 So, we make a TREE_LIST. Later, dfs_fixup_binfo_vtbls will
9139 straighten this out. */
9140 BINFO_VTABLE (binfo) = tree_cons (rtti_binfo, vtbl, BINFO_VTABLE (binfo));
9141 else if (BINFO_PRIMARY_P (binfo) && BINFO_VIRTUAL_P (binfo))
9142 /* Throw away any unneeded intializers. */
9143 (*l)->truncate (n_inits);
9144 else
9145 /* For an ordinary vtable, set BINFO_VTABLE. */
9146 BINFO_VTABLE (binfo) = vtbl;
9149 static GTY(()) tree abort_fndecl_addr;
9150 static GTY(()) tree dvirt_fn;
9152 /* Construct the initializer for BINFO's virtual function table. BINFO
9153 is part of the hierarchy dominated by T. If we're building a
9154 construction vtable, the ORIG_BINFO is the binfo we should use to
9155 find the actual function pointers to put in the vtable - but they
9156 can be overridden on the path to most-derived in the graph that
9157 ORIG_BINFO belongs. Otherwise,
9158 ORIG_BINFO should be the same as BINFO. The RTTI_BINFO is the
9159 BINFO that should be indicated by the RTTI information in the
9160 vtable; it will be a base class of T, rather than T itself, if we
9161 are building a construction vtable.
9163 The value returned is a TREE_LIST suitable for wrapping in a
9164 CONSTRUCTOR to use as the DECL_INITIAL for a vtable. If
9165 NON_FN_ENTRIES_P is not NULL, *NON_FN_ENTRIES_P is set to the
9166 number of non-function entries in the vtable.
9168 It might seem that this function should never be called with a
9169 BINFO for which BINFO_PRIMARY_P holds, the vtable for such a
9170 base is always subsumed by a derived class vtable. However, when
9171 we are building construction vtables, we do build vtables for
9172 primary bases; we need these while the primary base is being
9173 constructed. */
9175 static void
9176 build_vtbl_initializer (tree binfo,
9177 tree orig_binfo,
9178 tree t,
9179 tree rtti_binfo,
9180 int* non_fn_entries_p,
9181 vec<constructor_elt, va_gc> **inits)
9183 tree v;
9184 vtbl_init_data vid;
9185 unsigned ix, jx;
9186 tree vbinfo;
9187 vec<tree, va_gc> *vbases;
9188 constructor_elt *e;
9190 /* Initialize VID. */
9191 memset (&vid, 0, sizeof (vid));
9192 vid.binfo = binfo;
9193 vid.derived = t;
9194 vid.rtti_binfo = rtti_binfo;
9195 vid.primary_vtbl_p = SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), t);
9196 vid.ctor_vtbl_p = !SAME_BINFO_TYPE_P (BINFO_TYPE (rtti_binfo), t);
9197 vid.generate_vcall_entries = true;
9198 /* The first vbase or vcall offset is at index -3 in the vtable. */
9199 vid.index = ssize_int(-3 * TARGET_VTABLE_DATA_ENTRY_DISTANCE);
9201 /* Add entries to the vtable for RTTI. */
9202 build_rtti_vtbl_entries (binfo, &vid);
9204 /* Create an array for keeping track of the functions we've
9205 processed. When we see multiple functions with the same
9206 signature, we share the vcall offsets. */
9207 vec_alloc (vid.fns, 32);
9208 /* Add the vcall and vbase offset entries. */
9209 build_vcall_and_vbase_vtbl_entries (binfo, &vid);
9211 /* Clear BINFO_VTABLE_PATH_MARKED; it's set by
9212 build_vbase_offset_vtbl_entries. */
9213 for (vbases = CLASSTYPE_VBASECLASSES (t), ix = 0;
9214 vec_safe_iterate (vbases, ix, &vbinfo); ix++)
9215 BINFO_VTABLE_PATH_MARKED (vbinfo) = 0;
9217 /* If the target requires padding between data entries, add that now. */
9218 if (TARGET_VTABLE_DATA_ENTRY_DISTANCE > 1)
9220 int n_entries = vec_safe_length (vid.inits);
9222 vec_safe_grow (vid.inits, TARGET_VTABLE_DATA_ENTRY_DISTANCE * n_entries);
9224 /* Move data entries into their new positions and add padding
9225 after the new positions. Iterate backwards so we don't
9226 overwrite entries that we would need to process later. */
9227 for (ix = n_entries - 1;
9228 vid.inits->iterate (ix, &e);
9229 ix--)
9231 int j;
9232 int new_position = (TARGET_VTABLE_DATA_ENTRY_DISTANCE * ix
9233 + (TARGET_VTABLE_DATA_ENTRY_DISTANCE - 1));
9235 (*vid.inits)[new_position] = *e;
9237 for (j = 1; j < TARGET_VTABLE_DATA_ENTRY_DISTANCE; ++j)
9239 constructor_elt *f = &(*vid.inits)[new_position - j];
9240 f->index = NULL_TREE;
9241 f->value = build1 (NOP_EXPR, vtable_entry_type,
9242 null_pointer_node);
9247 if (non_fn_entries_p)
9248 *non_fn_entries_p = vec_safe_length (vid.inits);
9250 /* The initializers for virtual functions were built up in reverse
9251 order. Straighten them out and add them to the running list in one
9252 step. */
9253 jx = vec_safe_length (*inits);
9254 vec_safe_grow (*inits, jx + vid.inits->length ());
9256 for (ix = vid.inits->length () - 1;
9257 vid.inits->iterate (ix, &e);
9258 ix--, jx++)
9259 (**inits)[jx] = *e;
9261 /* Go through all the ordinary virtual functions, building up
9262 initializers. */
9263 for (v = BINFO_VIRTUALS (orig_binfo); v; v = TREE_CHAIN (v))
9265 tree delta;
9266 tree vcall_index;
9267 tree fn, fn_original;
9268 tree init = NULL_TREE;
9269 tree idx = size_int (jx++);
9271 fn = BV_FN (v);
9272 fn_original = fn;
9273 if (DECL_THUNK_P (fn))
9275 if (!DECL_NAME (fn))
9276 finish_thunk (fn);
9277 if (THUNK_ALIAS (fn))
9279 fn = THUNK_ALIAS (fn);
9280 BV_FN (v) = fn;
9282 fn_original = THUNK_TARGET (fn);
9285 /* If the only definition of this function signature along our
9286 primary base chain is from a lost primary, this vtable slot will
9287 never be used, so just zero it out. This is important to avoid
9288 requiring extra thunks which cannot be generated with the function.
9290 We first check this in update_vtable_entry_for_fn, so we handle
9291 restored primary bases properly; we also need to do it here so we
9292 zero out unused slots in ctor vtables, rather than filling them
9293 with erroneous values (though harmless, apart from relocation
9294 costs). */
9295 if (BV_LOST_PRIMARY (v))
9296 init = size_zero_node;
9298 if (! init)
9300 /* Pull the offset for `this', and the function to call, out of
9301 the list. */
9302 delta = BV_DELTA (v);
9303 vcall_index = BV_VCALL_INDEX (v);
9305 gcc_assert (TREE_CODE (delta) == INTEGER_CST);
9306 gcc_assert (TREE_CODE (fn) == FUNCTION_DECL);
9308 /* You can't call an abstract virtual function; it's abstract.
9309 So, we replace these functions with __pure_virtual. */
9310 if (DECL_PURE_VIRTUAL_P (fn_original))
9312 fn = abort_fndecl;
9313 if (!TARGET_VTABLE_USES_DESCRIPTORS)
9315 if (abort_fndecl_addr == NULL)
9316 abort_fndecl_addr
9317 = fold_convert (vfunc_ptr_type_node,
9318 build_fold_addr_expr (fn));
9319 init = abort_fndecl_addr;
9322 /* Likewise for deleted virtuals. */
9323 else if (DECL_DELETED_FN (fn_original))
9325 if (!dvirt_fn)
9327 tree name = get_identifier ("__cxa_deleted_virtual");
9328 dvirt_fn = get_global_binding (name);
9329 if (!dvirt_fn)
9330 dvirt_fn = push_library_fn
9331 (name,
9332 build_function_type_list (void_type_node, NULL_TREE),
9333 NULL_TREE, ECF_NORETURN | ECF_COLD);
9335 fn = dvirt_fn;
9336 if (!TARGET_VTABLE_USES_DESCRIPTORS)
9337 init = fold_convert (vfunc_ptr_type_node,
9338 build_fold_addr_expr (fn));
9340 else
9342 if (!integer_zerop (delta) || vcall_index)
9344 fn = make_thunk (fn, /*this_adjusting=*/1,
9345 delta, vcall_index);
9346 if (!DECL_NAME (fn))
9347 finish_thunk (fn);
9349 /* Take the address of the function, considering it to be of an
9350 appropriate generic type. */
9351 if (!TARGET_VTABLE_USES_DESCRIPTORS)
9352 init = fold_convert (vfunc_ptr_type_node,
9353 build_fold_addr_expr (fn));
9354 /* Don't refer to a virtual destructor from a constructor
9355 vtable or a vtable for an abstract class, since destroying
9356 an object under construction is undefined behavior and we
9357 don't want it to be considered a candidate for speculative
9358 devirtualization. But do create the thunk for ABI
9359 compliance. */
9360 if (DECL_DESTRUCTOR_P (fn_original)
9361 && (CLASSTYPE_PURE_VIRTUALS (DECL_CONTEXT (fn_original))
9362 || orig_binfo != binfo))
9363 init = size_zero_node;
9367 /* And add it to the chain of initializers. */
9368 if (TARGET_VTABLE_USES_DESCRIPTORS)
9370 int i;
9371 if (init == size_zero_node)
9372 for (i = 0; i < TARGET_VTABLE_USES_DESCRIPTORS; ++i)
9373 CONSTRUCTOR_APPEND_ELT (*inits, idx, init);
9374 else
9375 for (i = 0; i < TARGET_VTABLE_USES_DESCRIPTORS; ++i)
9377 tree fdesc = build2 (FDESC_EXPR, vfunc_ptr_type_node,
9378 fn, build_int_cst (NULL_TREE, i));
9379 TREE_CONSTANT (fdesc) = 1;
9381 CONSTRUCTOR_APPEND_ELT (*inits, idx, fdesc);
9384 else
9385 CONSTRUCTOR_APPEND_ELT (*inits, idx, init);
9389 /* Adds to vid->inits the initializers for the vbase and vcall
9390 offsets in BINFO, which is in the hierarchy dominated by T. */
9392 static void
9393 build_vcall_and_vbase_vtbl_entries (tree binfo, vtbl_init_data* vid)
9395 tree b;
9397 /* If this is a derived class, we must first create entries
9398 corresponding to the primary base class. */
9399 b = get_primary_binfo (binfo);
9400 if (b)
9401 build_vcall_and_vbase_vtbl_entries (b, vid);
9403 /* Add the vbase entries for this base. */
9404 build_vbase_offset_vtbl_entries (binfo, vid);
9405 /* Add the vcall entries for this base. */
9406 build_vcall_offset_vtbl_entries (binfo, vid);
9409 /* Returns the initializers for the vbase offset entries in the vtable
9410 for BINFO (which is part of the class hierarchy dominated by T), in
9411 reverse order. VBASE_OFFSET_INDEX gives the vtable index
9412 where the next vbase offset will go. */
9414 static void
9415 build_vbase_offset_vtbl_entries (tree binfo, vtbl_init_data* vid)
9417 tree vbase;
9418 tree t;
9419 tree non_primary_binfo;
9421 /* If there are no virtual baseclasses, then there is nothing to
9422 do. */
9423 if (!CLASSTYPE_VBASECLASSES (BINFO_TYPE (binfo)))
9424 return;
9426 t = vid->derived;
9428 /* We might be a primary base class. Go up the inheritance hierarchy
9429 until we find the most derived class of which we are a primary base:
9430 it is the offset of that which we need to use. */
9431 non_primary_binfo = binfo;
9432 while (BINFO_INHERITANCE_CHAIN (non_primary_binfo))
9434 tree b;
9436 /* If we have reached a virtual base, then it must be a primary
9437 base (possibly multi-level) of vid->binfo, or we wouldn't
9438 have called build_vcall_and_vbase_vtbl_entries for it. But it
9439 might be a lost primary, so just skip down to vid->binfo. */
9440 if (BINFO_VIRTUAL_P (non_primary_binfo))
9442 non_primary_binfo = vid->binfo;
9443 break;
9446 b = BINFO_INHERITANCE_CHAIN (non_primary_binfo);
9447 if (get_primary_binfo (b) != non_primary_binfo)
9448 break;
9449 non_primary_binfo = b;
9452 /* Go through the virtual bases, adding the offsets. */
9453 for (vbase = TYPE_BINFO (BINFO_TYPE (binfo));
9454 vbase;
9455 vbase = TREE_CHAIN (vbase))
9457 tree b;
9458 tree delta;
9460 if (!BINFO_VIRTUAL_P (vbase))
9461 continue;
9463 /* Find the instance of this virtual base in the complete
9464 object. */
9465 b = copied_binfo (vbase, binfo);
9467 /* If we've already got an offset for this virtual base, we
9468 don't need another one. */
9469 if (BINFO_VTABLE_PATH_MARKED (b))
9470 continue;
9471 BINFO_VTABLE_PATH_MARKED (b) = 1;
9473 /* Figure out where we can find this vbase offset. */
9474 delta = size_binop (MULT_EXPR,
9475 vid->index,
9476 fold_convert (ssizetype,
9477 TYPE_SIZE_UNIT (vtable_entry_type)));
9478 if (vid->primary_vtbl_p)
9479 BINFO_VPTR_FIELD (b) = delta;
9481 if (binfo != TYPE_BINFO (t))
9482 /* The vbase offset had better be the same. */
9483 gcc_assert (tree_int_cst_equal (delta, BINFO_VPTR_FIELD (vbase)));
9485 /* The next vbase will come at a more negative offset. */
9486 vid->index = size_binop (MINUS_EXPR, vid->index,
9487 ssize_int (TARGET_VTABLE_DATA_ENTRY_DISTANCE));
9489 /* The initializer is the delta from BINFO to this virtual base.
9490 The vbase offsets go in reverse inheritance-graph order, and
9491 we are walking in inheritance graph order so these end up in
9492 the right order. */
9493 delta = size_diffop_loc (input_location,
9494 BINFO_OFFSET (b), BINFO_OFFSET (non_primary_binfo));
9496 CONSTRUCTOR_APPEND_ELT (vid->inits, NULL_TREE,
9497 fold_build1_loc (input_location, NOP_EXPR,
9498 vtable_entry_type, delta));
9502 /* Adds the initializers for the vcall offset entries in the vtable
9503 for BINFO (which is part of the class hierarchy dominated by VID->DERIVED)
9504 to VID->INITS. */
9506 static void
9507 build_vcall_offset_vtbl_entries (tree binfo, vtbl_init_data* vid)
9509 /* We only need these entries if this base is a virtual base. We
9510 compute the indices -- but do not add to the vtable -- when
9511 building the main vtable for a class. */
9512 if (binfo == TYPE_BINFO (vid->derived)
9513 || (BINFO_VIRTUAL_P (binfo)
9514 /* If BINFO is RTTI_BINFO, then (since BINFO does not
9515 correspond to VID->DERIVED), we are building a primary
9516 construction virtual table. Since this is a primary
9517 virtual table, we do not need the vcall offsets for
9518 BINFO. */
9519 && binfo != vid->rtti_binfo))
9521 /* We need a vcall offset for each of the virtual functions in this
9522 vtable. For example:
9524 class A { virtual void f (); };
9525 class B1 : virtual public A { virtual void f (); };
9526 class B2 : virtual public A { virtual void f (); };
9527 class C: public B1, public B2 { virtual void f (); };
9529 A C object has a primary base of B1, which has a primary base of A. A
9530 C also has a secondary base of B2, which no longer has a primary base
9531 of A. So the B2-in-C construction vtable needs a secondary vtable for
9532 A, which will adjust the A* to a B2* to call f. We have no way of
9533 knowing what (or even whether) this offset will be when we define B2,
9534 so we store this "vcall offset" in the A sub-vtable and look it up in
9535 a "virtual thunk" for B2::f.
9537 We need entries for all the functions in our primary vtable and
9538 in our non-virtual bases' secondary vtables. */
9539 vid->vbase = binfo;
9540 /* If we are just computing the vcall indices -- but do not need
9541 the actual entries -- not that. */
9542 if (!BINFO_VIRTUAL_P (binfo))
9543 vid->generate_vcall_entries = false;
9544 /* Now, walk through the non-virtual bases, adding vcall offsets. */
9545 add_vcall_offset_vtbl_entries_r (binfo, vid);
9549 /* Build vcall offsets, starting with those for BINFO. */
9551 static void
9552 add_vcall_offset_vtbl_entries_r (tree binfo, vtbl_init_data* vid)
9554 int i;
9555 tree primary_binfo;
9556 tree base_binfo;
9558 /* Don't walk into virtual bases -- except, of course, for the
9559 virtual base for which we are building vcall offsets. Any
9560 primary virtual base will have already had its offsets generated
9561 through the recursion in build_vcall_and_vbase_vtbl_entries. */
9562 if (BINFO_VIRTUAL_P (binfo) && vid->vbase != binfo)
9563 return;
9565 /* If BINFO has a primary base, process it first. */
9566 primary_binfo = get_primary_binfo (binfo);
9567 if (primary_binfo)
9568 add_vcall_offset_vtbl_entries_r (primary_binfo, vid);
9570 /* Add BINFO itself to the list. */
9571 add_vcall_offset_vtbl_entries_1 (binfo, vid);
9573 /* Scan the non-primary bases of BINFO. */
9574 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
9575 if (base_binfo != primary_binfo)
9576 add_vcall_offset_vtbl_entries_r (base_binfo, vid);
9579 /* Called from build_vcall_offset_vtbl_entries_r. */
9581 static void
9582 add_vcall_offset_vtbl_entries_1 (tree binfo, vtbl_init_data* vid)
9584 /* Make entries for the rest of the virtuals. */
9585 tree orig_fn;
9587 /* The ABI requires that the methods be processed in declaration
9588 order. */
9589 for (orig_fn = TYPE_FIELDS (BINFO_TYPE (binfo));
9590 orig_fn;
9591 orig_fn = DECL_CHAIN (orig_fn))
9592 if (TREE_CODE (orig_fn) == FUNCTION_DECL && DECL_VINDEX (orig_fn))
9593 add_vcall_offset (orig_fn, binfo, vid);
9596 /* Add a vcall offset entry for ORIG_FN to the vtable. */
9598 static void
9599 add_vcall_offset (tree orig_fn, tree binfo, vtbl_init_data *vid)
9601 size_t i;
9602 tree vcall_offset;
9603 tree derived_entry;
9605 /* If there is already an entry for a function with the same
9606 signature as FN, then we do not need a second vcall offset.
9607 Check the list of functions already present in the derived
9608 class vtable. */
9609 FOR_EACH_VEC_SAFE_ELT (vid->fns, i, derived_entry)
9611 if (same_signature_p (derived_entry, orig_fn)
9612 /* We only use one vcall offset for virtual destructors,
9613 even though there are two virtual table entries. */
9614 || (DECL_DESTRUCTOR_P (derived_entry)
9615 && DECL_DESTRUCTOR_P (orig_fn)))
9616 return;
9619 /* If we are building these vcall offsets as part of building
9620 the vtable for the most derived class, remember the vcall
9621 offset. */
9622 if (vid->binfo == TYPE_BINFO (vid->derived))
9624 tree_pair_s elt = {orig_fn, vid->index};
9625 vec_safe_push (CLASSTYPE_VCALL_INDICES (vid->derived), elt);
9628 /* The next vcall offset will be found at a more negative
9629 offset. */
9630 vid->index = size_binop (MINUS_EXPR, vid->index,
9631 ssize_int (TARGET_VTABLE_DATA_ENTRY_DISTANCE));
9633 /* Keep track of this function. */
9634 vec_safe_push (vid->fns, orig_fn);
9636 if (vid->generate_vcall_entries)
9638 tree base;
9639 tree fn;
9641 /* Find the overriding function. */
9642 fn = find_final_overrider (vid->rtti_binfo, binfo, orig_fn);
9643 if (fn == error_mark_node)
9644 vcall_offset = build_zero_cst (vtable_entry_type);
9645 else
9647 base = TREE_VALUE (fn);
9649 /* The vbase we're working on is a primary base of
9650 vid->binfo. But it might be a lost primary, so its
9651 BINFO_OFFSET might be wrong, so we just use the
9652 BINFO_OFFSET from vid->binfo. */
9653 vcall_offset = size_diffop_loc (input_location,
9654 BINFO_OFFSET (base),
9655 BINFO_OFFSET (vid->binfo));
9656 vcall_offset = fold_build1_loc (input_location,
9657 NOP_EXPR, vtable_entry_type,
9658 vcall_offset);
9660 /* Add the initializer to the vtable. */
9661 CONSTRUCTOR_APPEND_ELT (vid->inits, NULL_TREE, vcall_offset);
9665 /* Return vtbl initializers for the RTTI entries corresponding to the
9666 BINFO's vtable. The RTTI entries should indicate the object given
9667 by VID->rtti_binfo. */
9669 static void
9670 build_rtti_vtbl_entries (tree binfo, vtbl_init_data* vid)
9672 tree b;
9673 tree t;
9674 tree offset;
9675 tree decl;
9676 tree init;
9678 t = BINFO_TYPE (vid->rtti_binfo);
9680 /* To find the complete object, we will first convert to our most
9681 primary base, and then add the offset in the vtbl to that value. */
9682 b = most_primary_binfo (binfo);
9683 offset = size_diffop_loc (input_location,
9684 BINFO_OFFSET (vid->rtti_binfo), BINFO_OFFSET (b));
9686 /* The second entry is the address of the typeinfo object. */
9687 if (flag_rtti)
9688 decl = build_address (get_tinfo_decl (t));
9689 else
9690 decl = integer_zero_node;
9692 /* Convert the declaration to a type that can be stored in the
9693 vtable. */
9694 init = build_nop (vfunc_ptr_type_node, decl);
9695 CONSTRUCTOR_APPEND_ELT (vid->inits, NULL_TREE, init);
9697 /* Add the offset-to-top entry. It comes earlier in the vtable than
9698 the typeinfo entry. Convert the offset to look like a
9699 function pointer, so that we can put it in the vtable. */
9700 init = build_nop (vfunc_ptr_type_node, offset);
9701 CONSTRUCTOR_APPEND_ELT (vid->inits, NULL_TREE, init);
9704 /* TRUE iff TYPE is uniquely derived from PARENT. Ignores
9705 accessibility. */
9707 bool
9708 uniquely_derived_from_p (tree parent, tree type)
9710 tree base = lookup_base (type, parent, ba_unique, NULL, tf_none);
9711 return base && base != error_mark_node;
9714 /* TRUE iff TYPE is publicly & uniquely derived from PARENT. */
9716 bool
9717 publicly_uniquely_derived_p (tree parent, tree type)
9719 tree base = lookup_base (type, parent, ba_ignore_scope | ba_check,
9720 NULL, tf_none);
9721 return base && base != error_mark_node;
9724 /* CTX1 and CTX2 are declaration contexts. Return the innermost common
9725 class between them, if any. */
9727 tree
9728 common_enclosing_class (tree ctx1, tree ctx2)
9730 if (!TYPE_P (ctx1) || !TYPE_P (ctx2))
9731 return NULL_TREE;
9732 gcc_assert (ctx1 == TYPE_MAIN_VARIANT (ctx1)
9733 && ctx2 == TYPE_MAIN_VARIANT (ctx2));
9734 if (ctx1 == ctx2)
9735 return ctx1;
9736 for (tree t = ctx1; TYPE_P (t); t = TYPE_CONTEXT (t))
9737 TYPE_MARKED_P (t) = true;
9738 tree found = NULL_TREE;
9739 for (tree t = ctx2; TYPE_P (t); t = TYPE_CONTEXT (t))
9740 if (TYPE_MARKED_P (t))
9742 found = t;
9743 break;
9745 for (tree t = ctx1; TYPE_P (t); t = TYPE_CONTEXT (t))
9746 TYPE_MARKED_P (t) = false;
9747 return found;
9750 #include "gt-cp-class.h"