Improve max_insns_skipped logic
[official-gcc.git] / gcc / cp / class.c
blobe5f237cb0525cc55eaf0c9c1960b1acb29975c1d
1 /* Functions related to building classes and their related objects.
2 Copyright (C) 1987-2017 Free Software Foundation, Inc.
3 Contributed by Michael Tiemann (tiemann@cygnus.com)
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
10 any later version.
12 GCC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
22 /* High-level class interface. */
24 #include "config.h"
25 #include "system.h"
26 #include "coretypes.h"
27 #include "target.h"
28 #include "cp-tree.h"
29 #include "stringpool.h"
30 #include "cgraph.h"
31 #include "stor-layout.h"
32 #include "attribs.h"
33 #include "flags.h"
34 #include "toplev.h"
35 #include "convert.h"
36 #include "dumpfile.h"
37 #include "gimplify.h"
38 #include "intl.h"
39 #include "asan.h"
41 /* Id for dumping the class hierarchy. */
42 int class_dump_id;
44 /* The number of nested classes being processed. If we are not in the
45 scope of any class, this is zero. */
47 int current_class_depth;
49 /* In order to deal with nested classes, we keep a stack of classes.
50 The topmost entry is the innermost class, and is the entry at index
51 CURRENT_CLASS_DEPTH */
53 typedef struct class_stack_node {
54 /* The name of the class. */
55 tree name;
57 /* The _TYPE node for the class. */
58 tree type;
60 /* The access specifier pending for new declarations in the scope of
61 this class. */
62 tree access;
64 /* If were defining TYPE, the names used in this class. */
65 splay_tree names_used;
67 /* Nonzero if this class is no longer open, because of a call to
68 push_to_top_level. */
69 size_t hidden;
70 }* class_stack_node_t;
72 struct vtbl_init_data
74 /* The base for which we're building initializers. */
75 tree binfo;
76 /* The type of the most-derived type. */
77 tree derived;
78 /* The binfo for the dynamic type. This will be TYPE_BINFO (derived),
79 unless ctor_vtbl_p is true. */
80 tree rtti_binfo;
81 /* The negative-index vtable initializers built up so far. These
82 are in order from least negative index to most negative index. */
83 vec<constructor_elt, va_gc> *inits;
84 /* The binfo for the virtual base for which we're building
85 vcall offset initializers. */
86 tree vbase;
87 /* The functions in vbase for which we have already provided vcall
88 offsets. */
89 vec<tree, va_gc> *fns;
90 /* The vtable index of the next vcall or vbase offset. */
91 tree index;
92 /* Nonzero if we are building the initializer for the primary
93 vtable. */
94 int primary_vtbl_p;
95 /* Nonzero if we are building the initializer for a construction
96 vtable. */
97 int ctor_vtbl_p;
98 /* True when adding vcall offset entries to the vtable. False when
99 merely computing the indices. */
100 bool generate_vcall_entries;
103 /* The type of a function passed to walk_subobject_offsets. */
104 typedef int (*subobject_offset_fn) (tree, tree, splay_tree);
106 /* The stack itself. This is a dynamically resized array. The
107 number of elements allocated is CURRENT_CLASS_STACK_SIZE. */
108 static int current_class_stack_size;
109 static class_stack_node_t current_class_stack;
111 /* The size of the largest empty class seen in this translation unit. */
112 static GTY (()) tree sizeof_biggest_empty_class;
114 /* An array of all local classes present in this translation unit, in
115 declaration order. */
116 vec<tree, va_gc> *local_classes;
118 static tree get_vfield_name (tree);
119 static void finish_struct_anon (tree);
120 static tree get_vtable_name (tree);
121 static void get_basefndecls (tree, tree, vec<tree> *);
122 static int build_primary_vtable (tree, tree);
123 static int build_secondary_vtable (tree);
124 static void finish_vtbls (tree);
125 static void modify_vtable_entry (tree, tree, tree, tree, tree *);
126 static void finish_struct_bits (tree);
127 static int alter_access (tree, tree, tree);
128 static void handle_using_decl (tree, tree);
129 static tree dfs_modify_vtables (tree, void *);
130 static tree modify_all_vtables (tree, tree);
131 static void determine_primary_bases (tree);
132 static void maybe_warn_about_overly_private_class (tree);
133 static void add_implicitly_declared_members (tree, tree*, int, int);
134 static tree fixed_type_or_null (tree, int *, int *);
135 static tree build_simple_base_path (tree expr, tree binfo);
136 static tree build_vtbl_ref_1 (tree, tree);
137 static void build_vtbl_initializer (tree, tree, tree, tree, int *,
138 vec<constructor_elt, va_gc> **);
139 static bool check_bitfield_decl (tree);
140 static bool check_field_decl (tree, tree, int *, int *);
141 static void check_field_decls (tree, tree *, int *, int *);
142 static tree *build_base_field (record_layout_info, tree, splay_tree, tree *);
143 static void build_base_fields (record_layout_info, splay_tree, tree *);
144 static void check_methods (tree);
145 static void remove_zero_width_bit_fields (tree);
146 static bool accessible_nvdtor_p (tree);
148 /* Used by find_flexarrays and related functions. */
149 struct flexmems_t;
150 static void diagnose_flexarrays (tree, const flexmems_t *);
151 static void find_flexarrays (tree, flexmems_t *, bool = false,
152 tree = NULL_TREE, tree = NULL_TREE);
153 static void check_flexarrays (tree, flexmems_t * = NULL, bool = false);
154 static void check_bases (tree, int *, int *);
155 static void check_bases_and_members (tree);
156 static tree create_vtable_ptr (tree, tree *);
157 static void include_empty_classes (record_layout_info);
158 static void layout_class_type (tree, tree *);
159 static void propagate_binfo_offsets (tree, tree);
160 static void layout_virtual_bases (record_layout_info, splay_tree);
161 static void build_vbase_offset_vtbl_entries (tree, vtbl_init_data *);
162 static void add_vcall_offset_vtbl_entries_r (tree, vtbl_init_data *);
163 static void add_vcall_offset_vtbl_entries_1 (tree, vtbl_init_data *);
164 static void build_vcall_offset_vtbl_entries (tree, vtbl_init_data *);
165 static void add_vcall_offset (tree, tree, vtbl_init_data *);
166 static void layout_vtable_decl (tree, int);
167 static tree dfs_find_final_overrider_pre (tree, void *);
168 static tree dfs_find_final_overrider_post (tree, void *);
169 static tree find_final_overrider (tree, tree, tree);
170 static int make_new_vtable (tree, tree);
171 static tree get_primary_binfo (tree);
172 static int maybe_indent_hierarchy (FILE *, int, int);
173 static tree dump_class_hierarchy_r (FILE *, dump_flags_t, tree, tree, int);
174 static void dump_class_hierarchy (tree);
175 static void dump_class_hierarchy_1 (FILE *, dump_flags_t, tree);
176 static void dump_array (FILE *, tree);
177 static void dump_vtable (tree, tree, tree);
178 static void dump_vtt (tree, tree);
179 static void dump_thunk (FILE *, int, tree);
180 static tree build_vtable (tree, tree, tree);
181 static void initialize_vtable (tree, vec<constructor_elt, va_gc> *);
182 static void layout_nonempty_base_or_field (record_layout_info,
183 tree, tree, splay_tree);
184 static tree end_of_class (tree, int);
185 static bool layout_empty_base (record_layout_info, tree, tree, splay_tree);
186 static void accumulate_vtbl_inits (tree, tree, tree, tree, tree,
187 vec<constructor_elt, va_gc> **);
188 static void dfs_accumulate_vtbl_inits (tree, tree, tree, tree, tree,
189 vec<constructor_elt, va_gc> **);
190 static void build_rtti_vtbl_entries (tree, vtbl_init_data *);
191 static void build_vcall_and_vbase_vtbl_entries (tree, vtbl_init_data *);
192 static void clone_constructors_and_destructors (tree);
193 static tree build_clone (tree, tree);
194 static void update_vtable_entry_for_fn (tree, tree, tree, tree *, unsigned);
195 static void build_ctor_vtbl_group (tree, tree);
196 static void build_vtt (tree);
197 static tree binfo_ctor_vtable (tree);
198 static void build_vtt_inits (tree, tree, vec<constructor_elt, va_gc> **,
199 tree *);
200 static tree dfs_build_secondary_vptr_vtt_inits (tree, void *);
201 static tree dfs_fixup_binfo_vtbls (tree, void *);
202 static int record_subobject_offset (tree, tree, splay_tree);
203 static int check_subobject_offset (tree, tree, splay_tree);
204 static int walk_subobject_offsets (tree, subobject_offset_fn,
205 tree, splay_tree, tree, int);
206 static void record_subobject_offsets (tree, tree, splay_tree, bool);
207 static int layout_conflict_p (tree, tree, splay_tree, int);
208 static int splay_tree_compare_integer_csts (splay_tree_key k1,
209 splay_tree_key k2);
210 static void warn_about_ambiguous_bases (tree);
211 static bool type_requires_array_cookie (tree);
212 static bool base_derived_from (tree, tree);
213 static int empty_base_at_nonzero_offset_p (tree, tree, splay_tree);
214 static tree end_of_base (tree);
215 static tree get_vcall_index (tree, tree);
216 static bool type_maybe_constexpr_default_constructor (tree);
218 /* Variables shared between class.c and call.c. */
220 int n_vtables = 0;
221 int n_vtable_entries = 0;
222 int n_vtable_searches = 0;
223 int n_vtable_elems = 0;
224 int n_convert_harshness = 0;
225 int n_compute_conversion_costs = 0;
226 int n_inner_fields_searched = 0;
228 /* Return a COND_EXPR that executes TRUE_STMT if this execution of the
229 'structor is in charge of 'structing virtual bases, or FALSE_STMT
230 otherwise. */
232 tree
233 build_if_in_charge (tree true_stmt, tree false_stmt)
235 gcc_assert (DECL_HAS_IN_CHARGE_PARM_P (current_function_decl));
236 tree cmp = build2 (NE_EXPR, boolean_type_node,
237 current_in_charge_parm, integer_zero_node);
238 tree type = unlowered_expr_type (true_stmt);
239 if (VOID_TYPE_P (type))
240 type = unlowered_expr_type (false_stmt);
241 tree cond = build3 (COND_EXPR, type,
242 cmp, true_stmt, false_stmt);
243 return cond;
246 /* Convert to or from a base subobject. EXPR is an expression of type
247 `A' or `A*', an expression of type `B' or `B*' is returned. To
248 convert A to a base B, CODE is PLUS_EXPR and BINFO is the binfo for
249 the B base instance within A. To convert base A to derived B, CODE
250 is MINUS_EXPR and BINFO is the binfo for the A instance within B.
251 In this latter case, A must not be a morally virtual base of B.
252 NONNULL is true if EXPR is known to be non-NULL (this is only
253 needed when EXPR is of pointer type). CV qualifiers are preserved
254 from EXPR. */
256 tree
257 build_base_path (enum tree_code code,
258 tree expr,
259 tree binfo,
260 int nonnull,
261 tsubst_flags_t complain)
263 tree v_binfo = NULL_TREE;
264 tree d_binfo = NULL_TREE;
265 tree probe;
266 tree offset;
267 tree target_type;
268 tree null_test = NULL;
269 tree ptr_target_type;
270 int fixed_type_p;
271 int want_pointer = TYPE_PTR_P (TREE_TYPE (expr));
272 bool has_empty = false;
273 bool virtual_access;
274 bool rvalue = false;
276 if (expr == error_mark_node || binfo == error_mark_node || !binfo)
277 return error_mark_node;
279 for (probe = binfo; probe; probe = BINFO_INHERITANCE_CHAIN (probe))
281 d_binfo = probe;
282 if (is_empty_class (BINFO_TYPE (probe)))
283 has_empty = true;
284 if (!v_binfo && BINFO_VIRTUAL_P (probe))
285 v_binfo = probe;
288 probe = TYPE_MAIN_VARIANT (TREE_TYPE (expr));
289 if (want_pointer)
290 probe = TYPE_MAIN_VARIANT (TREE_TYPE (probe));
292 if (code == PLUS_EXPR
293 && !SAME_BINFO_TYPE_P (BINFO_TYPE (d_binfo), probe))
295 /* This can happen when adjust_result_of_qualified_name_lookup can't
296 find a unique base binfo in a call to a member function. We
297 couldn't give the diagnostic then since we might have been calling
298 a static member function, so we do it now. In other cases, eg.
299 during error recovery (c++/71979), we may not have a base at all. */
300 if (complain & tf_error)
302 tree base = lookup_base (probe, BINFO_TYPE (d_binfo),
303 ba_unique, NULL, complain);
304 gcc_assert (base == error_mark_node || !base);
306 return error_mark_node;
309 gcc_assert ((code == MINUS_EXPR
310 && SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), probe))
311 || code == PLUS_EXPR);
313 if (binfo == d_binfo)
314 /* Nothing to do. */
315 return expr;
317 if (code == MINUS_EXPR && v_binfo)
319 if (complain & tf_error)
321 if (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), BINFO_TYPE (v_binfo)))
323 if (want_pointer)
324 error ("cannot convert from pointer to base class %qT to "
325 "pointer to derived class %qT because the base is "
326 "virtual", BINFO_TYPE (binfo), BINFO_TYPE (d_binfo));
327 else
328 error ("cannot convert from base class %qT to derived "
329 "class %qT because the base is virtual",
330 BINFO_TYPE (binfo), BINFO_TYPE (d_binfo));
332 else
334 if (want_pointer)
335 error ("cannot convert from pointer to base class %qT to "
336 "pointer to derived class %qT via virtual base %qT",
337 BINFO_TYPE (binfo), BINFO_TYPE (d_binfo),
338 BINFO_TYPE (v_binfo));
339 else
340 error ("cannot convert from base class %qT to derived "
341 "class %qT via virtual base %qT", BINFO_TYPE (binfo),
342 BINFO_TYPE (d_binfo), BINFO_TYPE (v_binfo));
345 return error_mark_node;
348 if (!want_pointer)
350 rvalue = !lvalue_p (expr);
351 /* This must happen before the call to save_expr. */
352 expr = cp_build_addr_expr (expr, complain);
354 else
355 expr = mark_rvalue_use (expr);
357 offset = BINFO_OFFSET (binfo);
358 fixed_type_p = resolves_to_fixed_type_p (expr, &nonnull);
359 target_type = code == PLUS_EXPR ? BINFO_TYPE (binfo) : BINFO_TYPE (d_binfo);
360 /* TARGET_TYPE has been extracted from BINFO, and, is therefore always
361 cv-unqualified. Extract the cv-qualifiers from EXPR so that the
362 expression returned matches the input. */
363 target_type = cp_build_qualified_type
364 (target_type, cp_type_quals (TREE_TYPE (TREE_TYPE (expr))));
365 ptr_target_type = build_pointer_type (target_type);
367 /* Do we need to look in the vtable for the real offset? */
368 virtual_access = (v_binfo && fixed_type_p <= 0);
370 /* Don't bother with the calculations inside sizeof; they'll ICE if the
371 source type is incomplete and the pointer value doesn't matter. In a
372 template (even in instantiate_non_dependent_expr), we don't have vtables
373 set up properly yet, and the value doesn't matter there either; we're
374 just interested in the result of overload resolution. */
375 if (cp_unevaluated_operand != 0
376 || processing_template_decl
377 || in_template_function ())
379 expr = build_nop (ptr_target_type, expr);
380 goto indout;
383 /* If we're in an NSDMI, we don't have the full constructor context yet
384 that we need for converting to a virtual base, so just build a stub
385 CONVERT_EXPR and expand it later in bot_replace. */
386 if (virtual_access && fixed_type_p < 0
387 && current_scope () != current_function_decl)
389 expr = build1 (CONVERT_EXPR, ptr_target_type, expr);
390 CONVERT_EXPR_VBASE_PATH (expr) = true;
391 goto indout;
394 /* Do we need to check for a null pointer? */
395 if (want_pointer && !nonnull)
397 /* If we know the conversion will not actually change the value
398 of EXPR, then we can avoid testing the expression for NULL.
399 We have to avoid generating a COMPONENT_REF for a base class
400 field, because other parts of the compiler know that such
401 expressions are always non-NULL. */
402 if (!virtual_access && integer_zerop (offset))
403 return build_nop (ptr_target_type, expr);
404 null_test = error_mark_node;
407 /* Protect against multiple evaluation if necessary. */
408 if (TREE_SIDE_EFFECTS (expr) && (null_test || virtual_access))
409 expr = save_expr (expr);
411 /* Now that we've saved expr, build the real null test. */
412 if (null_test)
414 tree zero = cp_convert (TREE_TYPE (expr), nullptr_node, complain);
415 null_test = build2_loc (input_location, NE_EXPR, boolean_type_node,
416 expr, zero);
417 /* This is a compiler generated comparison, don't emit
418 e.g. -Wnonnull-compare warning for it. */
419 TREE_NO_WARNING (null_test) = 1;
422 /* If this is a simple base reference, express it as a COMPONENT_REF. */
423 if (code == PLUS_EXPR && !virtual_access
424 /* We don't build base fields for empty bases, and they aren't very
425 interesting to the optimizers anyway. */
426 && !has_empty)
428 expr = cp_build_indirect_ref (expr, RO_NULL, complain);
429 expr = build_simple_base_path (expr, binfo);
430 if (rvalue)
431 expr = move (expr);
432 if (want_pointer)
433 expr = build_address (expr);
434 target_type = TREE_TYPE (expr);
435 goto out;
438 if (virtual_access)
440 /* Going via virtual base V_BINFO. We need the static offset
441 from V_BINFO to BINFO, and the dynamic offset from D_BINFO to
442 V_BINFO. That offset is an entry in D_BINFO's vtable. */
443 tree v_offset;
445 if (fixed_type_p < 0 && in_base_initializer)
447 /* In a base member initializer, we cannot rely on the
448 vtable being set up. We have to indirect via the
449 vtt_parm. */
450 tree t;
452 t = TREE_TYPE (TYPE_VFIELD (current_class_type));
453 t = build_pointer_type (t);
454 v_offset = fold_convert (t, current_vtt_parm);
455 v_offset = cp_build_indirect_ref (v_offset, RO_NULL, complain);
457 else
459 tree t = expr;
460 if (sanitize_flags_p (SANITIZE_VPTR)
461 && fixed_type_p == 0)
463 t = cp_ubsan_maybe_instrument_cast_to_vbase (input_location,
464 probe, expr);
465 if (t == NULL_TREE)
466 t = expr;
468 v_offset = build_vfield_ref (cp_build_indirect_ref (t, RO_NULL,
469 complain),
470 TREE_TYPE (TREE_TYPE (expr)));
473 if (v_offset == error_mark_node)
474 return error_mark_node;
476 v_offset = fold_build_pointer_plus (v_offset, BINFO_VPTR_FIELD (v_binfo));
477 v_offset = build1 (NOP_EXPR,
478 build_pointer_type (ptrdiff_type_node),
479 v_offset);
480 v_offset = cp_build_indirect_ref (v_offset, RO_NULL, complain);
481 TREE_CONSTANT (v_offset) = 1;
483 offset = convert_to_integer (ptrdiff_type_node,
484 size_diffop_loc (input_location, offset,
485 BINFO_OFFSET (v_binfo)));
487 if (!integer_zerop (offset))
488 v_offset = build2 (code, ptrdiff_type_node, v_offset, offset);
490 if (fixed_type_p < 0)
491 /* Negative fixed_type_p means this is a constructor or destructor;
492 virtual base layout is fixed in in-charge [cd]tors, but not in
493 base [cd]tors. */
494 offset = build_if_in_charge
495 (convert_to_integer (ptrdiff_type_node, BINFO_OFFSET (binfo)),
496 v_offset);
497 else
498 offset = v_offset;
501 if (want_pointer)
502 target_type = ptr_target_type;
504 expr = build1 (NOP_EXPR, ptr_target_type, expr);
506 if (!integer_zerop (offset))
508 offset = fold_convert (sizetype, offset);
509 if (code == MINUS_EXPR)
510 offset = fold_build1_loc (input_location, NEGATE_EXPR, sizetype, offset);
511 expr = fold_build_pointer_plus (expr, offset);
513 else
514 null_test = NULL;
516 indout:
517 if (!want_pointer)
519 expr = cp_build_indirect_ref (expr, RO_NULL, complain);
520 if (rvalue)
521 expr = move (expr);
524 out:
525 if (null_test)
526 expr = fold_build3_loc (input_location, COND_EXPR, target_type, null_test, expr,
527 build_zero_cst (target_type));
529 return expr;
532 /* Subroutine of build_base_path; EXPR and BINFO are as in that function.
533 Perform a derived-to-base conversion by recursively building up a
534 sequence of COMPONENT_REFs to the appropriate base fields. */
536 static tree
537 build_simple_base_path (tree expr, tree binfo)
539 tree type = BINFO_TYPE (binfo);
540 tree d_binfo = BINFO_INHERITANCE_CHAIN (binfo);
541 tree field;
543 if (d_binfo == NULL_TREE)
545 tree temp;
547 gcc_assert (TYPE_MAIN_VARIANT (TREE_TYPE (expr)) == type);
549 /* Transform `(a, b).x' into `(*(a, &b)).x', `(a ? b : c).x'
550 into `(*(a ? &b : &c)).x', and so on. A COND_EXPR is only
551 an lvalue in the front end; only _DECLs and _REFs are lvalues
552 in the back end. */
553 temp = unary_complex_lvalue (ADDR_EXPR, expr);
554 if (temp)
555 expr = cp_build_indirect_ref (temp, RO_NULL, tf_warning_or_error);
557 return expr;
560 /* Recurse. */
561 expr = build_simple_base_path (expr, d_binfo);
563 for (field = TYPE_FIELDS (BINFO_TYPE (d_binfo));
564 field; field = DECL_CHAIN (field))
565 /* Is this the base field created by build_base_field? */
566 if (TREE_CODE (field) == FIELD_DECL
567 && DECL_FIELD_IS_BASE (field)
568 && TREE_TYPE (field) == type
569 /* If we're looking for a field in the most-derived class,
570 also check the field offset; we can have two base fields
571 of the same type if one is an indirect virtual base and one
572 is a direct non-virtual base. */
573 && (BINFO_INHERITANCE_CHAIN (d_binfo)
574 || tree_int_cst_equal (byte_position (field),
575 BINFO_OFFSET (binfo))))
577 /* We don't use build_class_member_access_expr here, as that
578 has unnecessary checks, and more importantly results in
579 recursive calls to dfs_walk_once. */
580 int type_quals = cp_type_quals (TREE_TYPE (expr));
582 expr = build3 (COMPONENT_REF,
583 cp_build_qualified_type (type, type_quals),
584 expr, field, NULL_TREE);
585 /* Mark the expression const or volatile, as appropriate.
586 Even though we've dealt with the type above, we still have
587 to mark the expression itself. */
588 if (type_quals & TYPE_QUAL_CONST)
589 TREE_READONLY (expr) = 1;
590 if (type_quals & TYPE_QUAL_VOLATILE)
591 TREE_THIS_VOLATILE (expr) = 1;
593 return expr;
596 /* Didn't find the base field?!? */
597 gcc_unreachable ();
600 /* Convert OBJECT to the base TYPE. OBJECT is an expression whose
601 type is a class type or a pointer to a class type. In the former
602 case, TYPE is also a class type; in the latter it is another
603 pointer type. If CHECK_ACCESS is true, an error message is emitted
604 if TYPE is inaccessible. If OBJECT has pointer type, the value is
605 assumed to be non-NULL. */
607 tree
608 convert_to_base (tree object, tree type, bool check_access, bool nonnull,
609 tsubst_flags_t complain)
611 tree binfo;
612 tree object_type;
614 if (TYPE_PTR_P (TREE_TYPE (object)))
616 object_type = TREE_TYPE (TREE_TYPE (object));
617 type = TREE_TYPE (type);
619 else
620 object_type = TREE_TYPE (object);
622 binfo = lookup_base (object_type, type, check_access ? ba_check : ba_unique,
623 NULL, complain);
624 if (!binfo || binfo == error_mark_node)
625 return error_mark_node;
627 return build_base_path (PLUS_EXPR, object, binfo, nonnull, complain);
630 /* EXPR is an expression with unqualified class type. BASE is a base
631 binfo of that class type. Returns EXPR, converted to the BASE
632 type. This function assumes that EXPR is the most derived class;
633 therefore virtual bases can be found at their static offsets. */
635 tree
636 convert_to_base_statically (tree expr, tree base)
638 tree expr_type;
640 expr_type = TREE_TYPE (expr);
641 if (!SAME_BINFO_TYPE_P (BINFO_TYPE (base), expr_type))
643 /* If this is a non-empty base, use a COMPONENT_REF. */
644 if (!is_empty_class (BINFO_TYPE (base)))
645 return build_simple_base_path (expr, base);
647 /* We use fold_build2 and fold_convert below to simplify the trees
648 provided to the optimizers. It is not safe to call these functions
649 when processing a template because they do not handle C++-specific
650 trees. */
651 gcc_assert (!processing_template_decl);
652 expr = cp_build_addr_expr (expr, tf_warning_or_error);
653 if (!integer_zerop (BINFO_OFFSET (base)))
654 expr = fold_build_pointer_plus_loc (input_location,
655 expr, BINFO_OFFSET (base));
656 expr = fold_convert (build_pointer_type (BINFO_TYPE (base)), expr);
657 expr = build_fold_indirect_ref_loc (input_location, expr);
660 return expr;
664 tree
665 build_vfield_ref (tree datum, tree type)
667 tree vfield, vcontext;
669 if (datum == error_mark_node
670 /* Can happen in case of duplicate base types (c++/59082). */
671 || !TYPE_VFIELD (type))
672 return error_mark_node;
674 /* First, convert to the requested type. */
675 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (datum), type))
676 datum = convert_to_base (datum, type, /*check_access=*/false,
677 /*nonnull=*/true, tf_warning_or_error);
679 /* Second, the requested type may not be the owner of its own vptr.
680 If not, convert to the base class that owns it. We cannot use
681 convert_to_base here, because VCONTEXT may appear more than once
682 in the inheritance hierarchy of TYPE, and thus direct conversion
683 between the types may be ambiguous. Following the path back up
684 one step at a time via primary bases avoids the problem. */
685 vfield = TYPE_VFIELD (type);
686 vcontext = DECL_CONTEXT (vfield);
687 while (!same_type_ignoring_top_level_qualifiers_p (vcontext, type))
689 datum = build_simple_base_path (datum, CLASSTYPE_PRIMARY_BINFO (type));
690 type = TREE_TYPE (datum);
693 return build3 (COMPONENT_REF, TREE_TYPE (vfield), datum, vfield, NULL_TREE);
696 /* Given an object INSTANCE, return an expression which yields the
697 vtable element corresponding to INDEX. There are many special
698 cases for INSTANCE which we take care of here, mainly to avoid
699 creating extra tree nodes when we don't have to. */
701 static tree
702 build_vtbl_ref_1 (tree instance, tree idx)
704 tree aref;
705 tree vtbl = NULL_TREE;
707 /* Try to figure out what a reference refers to, and
708 access its virtual function table directly. */
710 int cdtorp = 0;
711 tree fixed_type = fixed_type_or_null (instance, NULL, &cdtorp);
713 tree basetype = non_reference (TREE_TYPE (instance));
715 if (fixed_type && !cdtorp)
717 tree binfo = lookup_base (fixed_type, basetype,
718 ba_unique, NULL, tf_none);
719 if (binfo && binfo != error_mark_node)
720 vtbl = unshare_expr (BINFO_VTABLE (binfo));
723 if (!vtbl)
724 vtbl = build_vfield_ref (instance, basetype);
726 aref = build_array_ref (input_location, vtbl, idx);
727 TREE_CONSTANT (aref) |= TREE_CONSTANT (vtbl) && TREE_CONSTANT (idx);
729 return aref;
732 tree
733 build_vtbl_ref (tree instance, tree idx)
735 tree aref = build_vtbl_ref_1 (instance, idx);
737 return aref;
740 /* Given a stable object pointer INSTANCE_PTR, return an expression which
741 yields a function pointer corresponding to vtable element INDEX. */
743 tree
744 build_vfn_ref (tree instance_ptr, tree idx)
746 tree aref;
748 aref = build_vtbl_ref_1 (cp_build_indirect_ref (instance_ptr, RO_NULL,
749 tf_warning_or_error),
750 idx);
752 /* When using function descriptors, the address of the
753 vtable entry is treated as a function pointer. */
754 if (TARGET_VTABLE_USES_DESCRIPTORS)
755 aref = build1 (NOP_EXPR, TREE_TYPE (aref),
756 cp_build_addr_expr (aref, tf_warning_or_error));
758 /* Remember this as a method reference, for later devirtualization. */
759 aref = build3 (OBJ_TYPE_REF, TREE_TYPE (aref), aref, instance_ptr, idx);
761 return aref;
764 /* Return the name of the virtual function table (as an IDENTIFIER_NODE)
765 for the given TYPE. */
767 static tree
768 get_vtable_name (tree type)
770 return mangle_vtbl_for_type (type);
773 /* DECL is an entity associated with TYPE, like a virtual table or an
774 implicitly generated constructor. Determine whether or not DECL
775 should have external or internal linkage at the object file
776 level. This routine does not deal with COMDAT linkage and other
777 similar complexities; it simply sets TREE_PUBLIC if it possible for
778 entities in other translation units to contain copies of DECL, in
779 the abstract. */
781 void
782 set_linkage_according_to_type (tree /*type*/, tree decl)
784 TREE_PUBLIC (decl) = 1;
785 determine_visibility (decl);
788 /* Create a VAR_DECL for a primary or secondary vtable for CLASS_TYPE.
789 (For a secondary vtable for B-in-D, CLASS_TYPE should be D, not B.)
790 Use NAME for the name of the vtable, and VTABLE_TYPE for its type. */
792 static tree
793 build_vtable (tree class_type, tree name, tree vtable_type)
795 tree decl;
797 decl = build_lang_decl (VAR_DECL, name, vtable_type);
798 /* vtable names are already mangled; give them their DECL_ASSEMBLER_NAME
799 now to avoid confusion in mangle_decl. */
800 SET_DECL_ASSEMBLER_NAME (decl, name);
801 DECL_CONTEXT (decl) = class_type;
802 DECL_ARTIFICIAL (decl) = 1;
803 TREE_STATIC (decl) = 1;
804 TREE_READONLY (decl) = 1;
805 DECL_VIRTUAL_P (decl) = 1;
806 SET_DECL_ALIGN (decl, TARGET_VTABLE_ENTRY_ALIGN);
807 DECL_USER_ALIGN (decl) = true;
808 DECL_VTABLE_OR_VTT_P (decl) = 1;
809 set_linkage_according_to_type (class_type, decl);
810 /* The vtable has not been defined -- yet. */
811 DECL_EXTERNAL (decl) = 1;
812 DECL_NOT_REALLY_EXTERN (decl) = 1;
814 /* Mark the VAR_DECL node representing the vtable itself as a
815 "gratuitous" one, thereby forcing dwarfout.c to ignore it. It
816 is rather important that such things be ignored because any
817 effort to actually generate DWARF for them will run into
818 trouble when/if we encounter code like:
820 #pragma interface
821 struct S { virtual void member (); };
823 because the artificial declaration of the vtable itself (as
824 manufactured by the g++ front end) will say that the vtable is
825 a static member of `S' but only *after* the debug output for
826 the definition of `S' has already been output. This causes
827 grief because the DWARF entry for the definition of the vtable
828 will try to refer back to an earlier *declaration* of the
829 vtable as a static member of `S' and there won't be one. We
830 might be able to arrange to have the "vtable static member"
831 attached to the member list for `S' before the debug info for
832 `S' get written (which would solve the problem) but that would
833 require more intrusive changes to the g++ front end. */
834 DECL_IGNORED_P (decl) = 1;
836 return decl;
839 /* Get the VAR_DECL of the vtable for TYPE. TYPE need not be polymorphic,
840 or even complete. If this does not exist, create it. If COMPLETE is
841 nonzero, then complete the definition of it -- that will render it
842 impossible to actually build the vtable, but is useful to get at those
843 which are known to exist in the runtime. */
845 tree
846 get_vtable_decl (tree type, int complete)
848 tree decl;
850 if (CLASSTYPE_VTABLES (type))
851 return CLASSTYPE_VTABLES (type);
853 decl = build_vtable (type, get_vtable_name (type), vtbl_type_node);
854 CLASSTYPE_VTABLES (type) = decl;
856 if (complete)
858 DECL_EXTERNAL (decl) = 1;
859 cp_finish_decl (decl, NULL_TREE, false, NULL_TREE, 0);
862 return decl;
865 /* Build the primary virtual function table for TYPE. If BINFO is
866 non-NULL, build the vtable starting with the initial approximation
867 that it is the same as the one which is the head of the association
868 list. Returns a nonzero value if a new vtable is actually
869 created. */
871 static int
872 build_primary_vtable (tree binfo, tree type)
874 tree decl;
875 tree virtuals;
877 decl = get_vtable_decl (type, /*complete=*/0);
879 if (binfo)
881 if (BINFO_NEW_VTABLE_MARKED (binfo))
882 /* We have already created a vtable for this base, so there's
883 no need to do it again. */
884 return 0;
886 virtuals = copy_list (BINFO_VIRTUALS (binfo));
887 TREE_TYPE (decl) = TREE_TYPE (get_vtbl_decl_for_binfo (binfo));
888 DECL_SIZE (decl) = TYPE_SIZE (TREE_TYPE (decl));
889 DECL_SIZE_UNIT (decl) = TYPE_SIZE_UNIT (TREE_TYPE (decl));
891 else
893 gcc_assert (TREE_TYPE (decl) == vtbl_type_node);
894 virtuals = NULL_TREE;
897 if (GATHER_STATISTICS)
899 n_vtables += 1;
900 n_vtable_elems += list_length (virtuals);
903 /* Initialize the association list for this type, based
904 on our first approximation. */
905 BINFO_VTABLE (TYPE_BINFO (type)) = decl;
906 BINFO_VIRTUALS (TYPE_BINFO (type)) = virtuals;
907 SET_BINFO_NEW_VTABLE_MARKED (TYPE_BINFO (type));
908 return 1;
911 /* Give BINFO a new virtual function table which is initialized
912 with a skeleton-copy of its original initialization. The only
913 entry that changes is the `delta' entry, so we can really
914 share a lot of structure.
916 FOR_TYPE is the most derived type which caused this table to
917 be needed.
919 Returns nonzero if we haven't met BINFO before.
921 The order in which vtables are built (by calling this function) for
922 an object must remain the same, otherwise a binary incompatibility
923 can result. */
925 static int
926 build_secondary_vtable (tree binfo)
928 if (BINFO_NEW_VTABLE_MARKED (binfo))
929 /* We already created a vtable for this base. There's no need to
930 do it again. */
931 return 0;
933 /* Remember that we've created a vtable for this BINFO, so that we
934 don't try to do so again. */
935 SET_BINFO_NEW_VTABLE_MARKED (binfo);
937 /* Make fresh virtual list, so we can smash it later. */
938 BINFO_VIRTUALS (binfo) = copy_list (BINFO_VIRTUALS (binfo));
940 /* Secondary vtables are laid out as part of the same structure as
941 the primary vtable. */
942 BINFO_VTABLE (binfo) = NULL_TREE;
943 return 1;
946 /* Create a new vtable for BINFO which is the hierarchy dominated by
947 T. Return nonzero if we actually created a new vtable. */
949 static int
950 make_new_vtable (tree t, tree binfo)
952 if (binfo == TYPE_BINFO (t))
953 /* In this case, it is *type*'s vtable we are modifying. We start
954 with the approximation that its vtable is that of the
955 immediate base class. */
956 return build_primary_vtable (binfo, t);
957 else
958 /* This is our very own copy of `basetype' to play with. Later,
959 we will fill in all the virtual functions that override the
960 virtual functions in these base classes which are not defined
961 by the current type. */
962 return build_secondary_vtable (binfo);
965 /* Make *VIRTUALS, an entry on the BINFO_VIRTUALS list for BINFO
966 (which is in the hierarchy dominated by T) list FNDECL as its
967 BV_FN. DELTA is the required constant adjustment from the `this'
968 pointer where the vtable entry appears to the `this' required when
969 the function is actually called. */
971 static void
972 modify_vtable_entry (tree t,
973 tree binfo,
974 tree fndecl,
975 tree delta,
976 tree *virtuals)
978 tree v;
980 v = *virtuals;
982 if (fndecl != BV_FN (v)
983 || !tree_int_cst_equal (delta, BV_DELTA (v)))
985 /* We need a new vtable for BINFO. */
986 if (make_new_vtable (t, binfo))
988 /* If we really did make a new vtable, we also made a copy
989 of the BINFO_VIRTUALS list. Now, we have to find the
990 corresponding entry in that list. */
991 *virtuals = BINFO_VIRTUALS (binfo);
992 while (BV_FN (*virtuals) != BV_FN (v))
993 *virtuals = TREE_CHAIN (*virtuals);
994 v = *virtuals;
997 BV_DELTA (v) = delta;
998 BV_VCALL_INDEX (v) = NULL_TREE;
999 BV_FN (v) = fndecl;
1004 /* Add method METHOD to class TYPE. If VIA_USING indicates whether
1005 METHOD is being injected via a using_decl. Returns true if the
1006 method could be added to the method vec. */
1008 bool
1009 add_method (tree type, tree method, bool via_using)
1011 if (method == error_mark_node)
1012 return false;
1014 vec<tree, va_gc> *method_vec = CLASSTYPE_METHOD_VEC (type);
1015 if (!method_vec)
1017 /* Make a new method vector. We start with 8 entries. */
1018 vec_alloc (method_vec, 8);
1019 CLASSTYPE_METHOD_VEC (type) = method_vec;
1022 /* Maintain TYPE_HAS_USER_CONSTRUCTOR, etc. */
1023 grok_special_member_properties (method);
1025 bool insert_p = true;
1026 tree method_name = DECL_NAME (method);
1027 bool complete_p = COMPLETE_TYPE_P (type);
1028 bool conv_p = IDENTIFIER_CONV_OP_P (method_name);
1030 if (conv_p)
1031 method_name = conv_op_identifier;
1033 /* See if we already have an entry with this name. */
1034 unsigned slot;
1035 tree m;
1036 for (slot = 0; vec_safe_iterate (method_vec, slot, &m); ++slot)
1038 m = DECL_NAME (OVL_FIRST (m));
1039 if (m == method_name)
1041 insert_p = false;
1042 break;
1044 if (complete_p && m > method_name)
1045 break;
1047 tree current_fns = insert_p ? NULL_TREE : (*method_vec)[slot];
1049 tree conv_marker = NULL_TREE;
1050 if (conv_p)
1052 /* For conversion operators, we prepend a dummy overload
1053 pointing at conv_op_marker. That function's DECL_NAME is
1054 conv_op_identifier, so we can use identifier equality to
1055 locate it. */
1056 if (current_fns)
1058 gcc_checking_assert (OVL_FUNCTION (current_fns) == conv_op_marker);
1059 conv_marker = current_fns;
1060 current_fns = OVL_CHAIN (current_fns);
1062 else
1063 conv_marker = ovl_make (conv_op_marker, NULL_TREE);
1065 gcc_assert (!DECL_EXTERN_C_P (method));
1067 /* Check to see if we've already got this method. */
1068 for (ovl_iterator iter (current_fns); iter; ++iter)
1070 tree fn = *iter;
1071 tree fn_type;
1072 tree method_type;
1073 tree parms1;
1074 tree parms2;
1076 if (TREE_CODE (fn) != TREE_CODE (method))
1077 continue;
1079 /* Two using-declarations can coexist, we'll complain about ambiguity in
1080 overload resolution. */
1081 if (via_using && iter.using_p ()
1082 /* Except handle inherited constructors specially. */
1083 && ! DECL_CONSTRUCTOR_P (fn))
1084 continue;
1086 /* [over.load] Member function declarations with the
1087 same name and the same parameter types cannot be
1088 overloaded if any of them is a static member
1089 function declaration.
1091 [over.load] Member function declarations with the same name and
1092 the same parameter-type-list as well as member function template
1093 declarations with the same name, the same parameter-type-list, and
1094 the same template parameter lists cannot be overloaded if any of
1095 them, but not all, have a ref-qualifier.
1097 [namespace.udecl] When a using-declaration brings names
1098 from a base class into a derived class scope, member
1099 functions in the derived class override and/or hide member
1100 functions with the same name and parameter types in a base
1101 class (rather than conflicting). */
1102 fn_type = TREE_TYPE (fn);
1103 method_type = TREE_TYPE (method);
1104 parms1 = TYPE_ARG_TYPES (fn_type);
1105 parms2 = TYPE_ARG_TYPES (method_type);
1107 /* Compare the quals on the 'this' parm. Don't compare
1108 the whole types, as used functions are treated as
1109 coming from the using class in overload resolution. */
1110 if (! DECL_STATIC_FUNCTION_P (fn)
1111 && ! DECL_STATIC_FUNCTION_P (method)
1112 /* Either both or neither need to be ref-qualified for
1113 differing quals to allow overloading. */
1114 && (FUNCTION_REF_QUALIFIED (fn_type)
1115 == FUNCTION_REF_QUALIFIED (method_type))
1116 && (type_memfn_quals (fn_type) != type_memfn_quals (method_type)
1117 || type_memfn_rqual (fn_type) != type_memfn_rqual (method_type)))
1118 continue;
1120 /* For templates, the return type and template parameters
1121 must be identical. */
1122 if (TREE_CODE (fn) == TEMPLATE_DECL
1123 && (!same_type_p (TREE_TYPE (fn_type),
1124 TREE_TYPE (method_type))
1125 || !comp_template_parms (DECL_TEMPLATE_PARMS (fn),
1126 DECL_TEMPLATE_PARMS (method))))
1127 continue;
1129 if (! DECL_STATIC_FUNCTION_P (fn))
1130 parms1 = TREE_CHAIN (parms1);
1131 if (! DECL_STATIC_FUNCTION_P (method))
1132 parms2 = TREE_CHAIN (parms2);
1134 /* Bring back parameters omitted from an inherited ctor. */
1135 if (ctor_omit_inherited_parms (fn))
1136 parms1 = FUNCTION_FIRST_USER_PARMTYPE (DECL_ORIGIN (fn));
1137 if (ctor_omit_inherited_parms (method))
1138 parms2 = FUNCTION_FIRST_USER_PARMTYPE (DECL_ORIGIN (method));
1140 if (compparms (parms1, parms2)
1141 && (!DECL_CONV_FN_P (fn)
1142 || same_type_p (TREE_TYPE (fn_type),
1143 TREE_TYPE (method_type)))
1144 && equivalently_constrained (fn, method))
1146 /* If these are versions of the same function, process and
1147 move on. */
1148 if (TREE_CODE (fn) == FUNCTION_DECL
1149 && maybe_version_functions (method, fn))
1150 continue;
1152 if (DECL_INHERITED_CTOR (method))
1154 if (DECL_INHERITED_CTOR (fn))
1156 tree basem = DECL_INHERITED_CTOR_BASE (method);
1157 tree basef = DECL_INHERITED_CTOR_BASE (fn);
1158 if (flag_new_inheriting_ctors)
1160 if (basem == basef)
1162 /* Inheriting the same constructor along different
1163 paths, combine them. */
1164 SET_DECL_INHERITED_CTOR
1165 (fn, ovl_make (DECL_INHERITED_CTOR (method),
1166 DECL_INHERITED_CTOR (fn)));
1167 /* And discard the new one. */
1168 return false;
1170 else
1171 /* Inherited ctors can coexist until overload
1172 resolution. */
1173 continue;
1175 error_at (DECL_SOURCE_LOCATION (method),
1176 "%q#D conflicts with version inherited from %qT",
1177 method, basef);
1178 inform (DECL_SOURCE_LOCATION (fn),
1179 "version inherited from %qT declared here",
1180 basef);
1182 /* Otherwise defer to the other function. */
1183 return false;
1186 if (via_using)
1187 /* Defer to the local function. */
1188 return false;
1189 else if (flag_new_inheriting_ctors
1190 && DECL_INHERITED_CTOR (fn))
1192 /* Remove the inherited constructor. */
1193 current_fns = iter.remove_node (current_fns);
1194 continue;
1196 else
1198 error_at (DECL_SOURCE_LOCATION (method),
1199 "%q#D cannot be overloaded with %q#D", method, fn);
1200 inform (DECL_SOURCE_LOCATION (fn),
1201 "previous declaration %q#D", fn);
1202 return false;
1207 /* A class should never have more than one destructor. */
1208 gcc_assert (!current_fns || !DECL_DESTRUCTOR_P (method));
1210 current_fns = ovl_insert (method, current_fns, via_using);
1212 if (conv_p)
1214 TYPE_HAS_CONVERSION (type) = 1;
1215 /* Prepend the marker function. */
1216 OVL_CHAIN (conv_marker) = current_fns;
1217 current_fns = conv_marker;
1219 else if (!complete_p && !IDENTIFIER_CDTOR_P (DECL_NAME (method)))
1220 push_class_level_binding (DECL_NAME (method), current_fns);
1222 if (insert_p)
1224 bool reallocated;
1226 /* We only expect to add few methods in the COMPLETE_P case, so
1227 just make room for one more method in that case. */
1228 if (complete_p)
1229 reallocated = vec_safe_reserve_exact (method_vec, 1);
1230 else
1231 reallocated = vec_safe_reserve (method_vec, 1);
1232 if (reallocated)
1233 CLASSTYPE_METHOD_VEC (type) = method_vec;
1234 if (slot == method_vec->length ())
1235 method_vec->quick_push (current_fns);
1236 else
1237 method_vec->quick_insert (slot, current_fns);
1239 else
1240 /* Replace the current slot. */
1241 (*method_vec)[slot] = current_fns;
1242 return true;
1245 /* Subroutines of finish_struct. */
1247 /* Change the access of FDECL to ACCESS in T. Return 1 if change was
1248 legit, otherwise return 0. */
1250 static int
1251 alter_access (tree t, tree fdecl, tree access)
1253 tree elem;
1255 retrofit_lang_decl (fdecl);
1257 gcc_assert (!DECL_DISCRIMINATOR_P (fdecl));
1259 elem = purpose_member (t, DECL_ACCESS (fdecl));
1260 if (elem)
1262 if (TREE_VALUE (elem) != access)
1264 if (TREE_CODE (TREE_TYPE (fdecl)) == FUNCTION_DECL)
1265 error ("conflicting access specifications for method"
1266 " %q+D, ignored", TREE_TYPE (fdecl));
1267 else
1268 error ("conflicting access specifications for field %qE, ignored",
1269 DECL_NAME (fdecl));
1271 else
1273 /* They're changing the access to the same thing they changed
1274 it to before. That's OK. */
1278 else
1280 perform_or_defer_access_check (TYPE_BINFO (t), fdecl, fdecl,
1281 tf_warning_or_error);
1282 DECL_ACCESS (fdecl) = tree_cons (t, access, DECL_ACCESS (fdecl));
1283 return 1;
1285 return 0;
1288 /* Return the access node for DECL's access in its enclosing class. */
1290 tree
1291 declared_access (tree decl)
1293 return (TREE_PRIVATE (decl) ? access_private_node
1294 : TREE_PROTECTED (decl) ? access_protected_node
1295 : access_public_node);
1298 /* Process the USING_DECL, which is a member of T. */
1300 static void
1301 handle_using_decl (tree using_decl, tree t)
1303 tree decl = USING_DECL_DECLS (using_decl);
1304 tree name = DECL_NAME (using_decl);
1305 tree access = declared_access (using_decl);
1306 tree flist = NULL_TREE;
1307 tree old_value;
1309 gcc_assert (!processing_template_decl && decl);
1311 old_value = lookup_member (t, name, /*protect=*/0, /*want_type=*/false,
1312 tf_warning_or_error);
1313 if (old_value)
1315 old_value = OVL_FIRST (old_value);
1317 if (DECL_P (old_value) && DECL_CONTEXT (old_value) == t)
1318 /* OK */;
1319 else
1320 old_value = NULL_TREE;
1323 cp_emit_debug_info_for_using (decl, t);
1325 if (is_overloaded_fn (decl))
1326 flist = decl;
1328 if (! old_value)
1330 else if (is_overloaded_fn (old_value))
1332 if (flist)
1333 /* It's OK to use functions from a base when there are functions with
1334 the same name already present in the current class. */;
1335 else
1337 error_at (DECL_SOURCE_LOCATION (using_decl), "%qD invalid in %q#T "
1338 "because of local method %q#D with same name",
1339 using_decl, t, old_value);
1340 inform (DECL_SOURCE_LOCATION (old_value),
1341 "local method %q#D declared here", old_value);
1342 return;
1345 else if (!DECL_ARTIFICIAL (old_value))
1347 error_at (DECL_SOURCE_LOCATION (using_decl), "%qD invalid in %q#T "
1348 "because of local member %q#D with same name",
1349 using_decl, t, old_value);
1350 inform (DECL_SOURCE_LOCATION (old_value),
1351 "local member %q#D declared here", old_value);
1352 return;
1355 /* Make type T see field decl FDECL with access ACCESS. */
1356 if (flist)
1357 for (ovl_iterator iter (flist); iter; ++iter)
1359 add_method (t, *iter, true);
1360 alter_access (t, *iter, access);
1362 else
1363 alter_access (t, decl, access);
1366 /* Data structure for find_abi_tags_r, below. */
1368 struct abi_tag_data
1370 tree t; // The type that we're checking for missing tags.
1371 tree subob; // The subobject of T that we're getting tags from.
1372 tree tags; // error_mark_node for diagnostics, or a list of missing tags.
1375 /* Subroutine of find_abi_tags_r. Handle a single TAG found on the class TP
1376 in the context of P. TAG can be either an identifier (the DECL_NAME of
1377 a tag NAMESPACE_DECL) or a STRING_CST (a tag attribute). */
1379 static void
1380 check_tag (tree tag, tree id, tree *tp, abi_tag_data *p)
1382 if (!IDENTIFIER_MARKED (id))
1384 if (p->tags != error_mark_node)
1386 /* We're collecting tags from template arguments or from
1387 the type of a variable or function return type. */
1388 p->tags = tree_cons (NULL_TREE, tag, p->tags);
1390 /* Don't inherit this tag multiple times. */
1391 IDENTIFIER_MARKED (id) = true;
1393 if (TYPE_P (p->t))
1395 /* Tags inherited from type template arguments are only used
1396 to avoid warnings. */
1397 ABI_TAG_IMPLICIT (p->tags) = true;
1398 return;
1400 /* For functions and variables we want to warn, too. */
1403 /* Otherwise we're diagnosing missing tags. */
1404 if (TREE_CODE (p->t) == FUNCTION_DECL)
1406 if (warning (OPT_Wabi_tag, "%qD inherits the %E ABI tag "
1407 "that %qT (used in its return type) has",
1408 p->t, tag, *tp))
1409 inform (location_of (*tp), "%qT declared here", *tp);
1411 else if (VAR_P (p->t))
1413 if (warning (OPT_Wabi_tag, "%qD inherits the %E ABI tag "
1414 "that %qT (used in its type) has", p->t, tag, *tp))
1415 inform (location_of (*tp), "%qT declared here", *tp);
1417 else if (TYPE_P (p->subob))
1419 if (warning (OPT_Wabi_tag, "%qT does not have the %E ABI tag "
1420 "that base %qT has", p->t, tag, p->subob))
1421 inform (location_of (p->subob), "%qT declared here",
1422 p->subob);
1424 else
1426 if (warning (OPT_Wabi_tag, "%qT does not have the %E ABI tag "
1427 "that %qT (used in the type of %qD) has",
1428 p->t, tag, *tp, p->subob))
1430 inform (location_of (p->subob), "%qD declared here",
1431 p->subob);
1432 inform (location_of (*tp), "%qT declared here", *tp);
1438 /* Find all the ABI tags in the attribute list ATTR and either call
1439 check_tag (if TP is non-null) or set IDENTIFIER_MARKED to val. */
1441 static void
1442 mark_or_check_attr_tags (tree attr, tree *tp, abi_tag_data *p, bool val)
1444 if (!attr)
1445 return;
1446 for (; (attr = lookup_attribute ("abi_tag", attr));
1447 attr = TREE_CHAIN (attr))
1448 for (tree list = TREE_VALUE (attr); list;
1449 list = TREE_CHAIN (list))
1451 tree tag = TREE_VALUE (list);
1452 tree id = get_identifier (TREE_STRING_POINTER (tag));
1453 if (tp)
1454 check_tag (tag, id, tp, p);
1455 else
1456 IDENTIFIER_MARKED (id) = val;
1460 /* Find all the ABI tags on T and its enclosing scopes and either call
1461 check_tag (if TP is non-null) or set IDENTIFIER_MARKED to val. */
1463 static void
1464 mark_or_check_tags (tree t, tree *tp, abi_tag_data *p, bool val)
1466 while (t != global_namespace)
1468 tree attr;
1469 if (TYPE_P (t))
1471 attr = TYPE_ATTRIBUTES (t);
1472 t = CP_TYPE_CONTEXT (t);
1474 else
1476 attr = DECL_ATTRIBUTES (t);
1477 t = CP_DECL_CONTEXT (t);
1479 mark_or_check_attr_tags (attr, tp, p, val);
1483 /* walk_tree callback for check_abi_tags: if the type at *TP involves any
1484 types with ABI tags, add the corresponding identifiers to the VEC in
1485 *DATA and set IDENTIFIER_MARKED. */
1487 static tree
1488 find_abi_tags_r (tree *tp, int *walk_subtrees, void *data)
1490 if (!OVERLOAD_TYPE_P (*tp))
1491 return NULL_TREE;
1493 /* walk_tree shouldn't be walking into any subtrees of a RECORD_TYPE
1494 anyway, but let's make sure of it. */
1495 *walk_subtrees = false;
1497 abi_tag_data *p = static_cast<struct abi_tag_data*>(data);
1499 mark_or_check_tags (*tp, tp, p, false);
1501 return NULL_TREE;
1504 /* walk_tree callback for mark_abi_tags: if *TP is a class, set
1505 IDENTIFIER_MARKED on its ABI tags. */
1507 static tree
1508 mark_abi_tags_r (tree *tp, int *walk_subtrees, void *data)
1510 if (!OVERLOAD_TYPE_P (*tp))
1511 return NULL_TREE;
1513 /* walk_tree shouldn't be walking into any subtrees of a RECORD_TYPE
1514 anyway, but let's make sure of it. */
1515 *walk_subtrees = false;
1517 bool *valp = static_cast<bool*>(data);
1519 mark_or_check_tags (*tp, NULL, NULL, *valp);
1521 return NULL_TREE;
1524 /* Set IDENTIFIER_MARKED on all the ABI tags on T and its enclosing
1525 scopes. */
1527 static void
1528 mark_abi_tags (tree t, bool val)
1530 mark_or_check_tags (t, NULL, NULL, val);
1531 if (DECL_P (t))
1533 if (DECL_LANG_SPECIFIC (t) && DECL_USE_TEMPLATE (t)
1534 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (t)))
1536 /* Template arguments are part of the signature. */
1537 tree level = INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (t));
1538 for (int j = 0; j < TREE_VEC_LENGTH (level); ++j)
1540 tree arg = TREE_VEC_ELT (level, j);
1541 cp_walk_tree_without_duplicates (&arg, mark_abi_tags_r, &val);
1544 if (TREE_CODE (t) == FUNCTION_DECL)
1545 /* A function's parameter types are part of the signature, so
1546 we don't need to inherit any tags that are also in them. */
1547 for (tree arg = FUNCTION_FIRST_USER_PARMTYPE (t); arg;
1548 arg = TREE_CHAIN (arg))
1549 cp_walk_tree_without_duplicates (&TREE_VALUE (arg),
1550 mark_abi_tags_r, &val);
1554 /* Check that T has all the ABI tags that subobject SUBOB has, or
1555 warn if not. If T is a (variable or function) declaration, also
1556 return any missing tags, and add them to T if JUST_CHECKING is false. */
1558 static tree
1559 check_abi_tags (tree t, tree subob, bool just_checking = false)
1561 bool inherit = DECL_P (t);
1563 if (!inherit && !warn_abi_tag)
1564 return NULL_TREE;
1566 tree decl = TYPE_P (t) ? TYPE_NAME (t) : t;
1567 if (!TREE_PUBLIC (decl))
1568 /* No need to worry about things local to this TU. */
1569 return NULL_TREE;
1571 mark_abi_tags (t, true);
1573 tree subtype = TYPE_P (subob) ? subob : TREE_TYPE (subob);
1574 struct abi_tag_data data = { t, subob, error_mark_node };
1575 if (inherit)
1576 data.tags = NULL_TREE;
1578 cp_walk_tree_without_duplicates (&subtype, find_abi_tags_r, &data);
1580 if (!(inherit && data.tags))
1581 /* We don't need to do anything with data.tags. */;
1582 else if (just_checking)
1583 for (tree t = data.tags; t; t = TREE_CHAIN (t))
1585 tree id = get_identifier (TREE_STRING_POINTER (TREE_VALUE (t)));
1586 IDENTIFIER_MARKED (id) = false;
1588 else
1590 tree attr = lookup_attribute ("abi_tag", DECL_ATTRIBUTES (t));
1591 if (attr)
1592 TREE_VALUE (attr) = chainon (data.tags, TREE_VALUE (attr));
1593 else
1594 DECL_ATTRIBUTES (t)
1595 = tree_cons (get_identifier ("abi_tag"), data.tags,
1596 DECL_ATTRIBUTES (t));
1599 mark_abi_tags (t, false);
1601 return data.tags;
1604 /* Check that DECL has all the ABI tags that are used in parts of its type
1605 that are not reflected in its mangled name. */
1607 void
1608 check_abi_tags (tree decl)
1610 if (VAR_P (decl))
1611 check_abi_tags (decl, TREE_TYPE (decl));
1612 else if (TREE_CODE (decl) == FUNCTION_DECL
1613 && !DECL_CONV_FN_P (decl)
1614 && !mangle_return_type_p (decl))
1615 check_abi_tags (decl, TREE_TYPE (TREE_TYPE (decl)));
1618 /* Return any ABI tags that are used in parts of the type of DECL
1619 that are not reflected in its mangled name. This function is only
1620 used in backward-compatible mangling for ABI <11. */
1622 tree
1623 missing_abi_tags (tree decl)
1625 if (VAR_P (decl))
1626 return check_abi_tags (decl, TREE_TYPE (decl), true);
1627 else if (TREE_CODE (decl) == FUNCTION_DECL
1628 /* Don't check DECL_CONV_FN_P here like we do in check_abi_tags, so
1629 that we can use this function for setting need_abi_warning
1630 regardless of the current flag_abi_version. */
1631 && !mangle_return_type_p (decl))
1632 return check_abi_tags (decl, TREE_TYPE (TREE_TYPE (decl)), true);
1633 else
1634 return NULL_TREE;
1637 void
1638 inherit_targ_abi_tags (tree t)
1640 if (!CLASS_TYPE_P (t)
1641 || CLASSTYPE_TEMPLATE_INFO (t) == NULL_TREE)
1642 return;
1644 mark_abi_tags (t, true);
1646 tree args = CLASSTYPE_TI_ARGS (t);
1647 struct abi_tag_data data = { t, NULL_TREE, NULL_TREE };
1648 for (int i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
1650 tree level = TMPL_ARGS_LEVEL (args, i+1);
1651 for (int j = 0; j < TREE_VEC_LENGTH (level); ++j)
1653 tree arg = TREE_VEC_ELT (level, j);
1654 data.subob = arg;
1655 cp_walk_tree_without_duplicates (&arg, find_abi_tags_r, &data);
1659 // If we found some tags on our template arguments, add them to our
1660 // abi_tag attribute.
1661 if (data.tags)
1663 tree attr = lookup_attribute ("abi_tag", TYPE_ATTRIBUTES (t));
1664 if (attr)
1665 TREE_VALUE (attr) = chainon (data.tags, TREE_VALUE (attr));
1666 else
1667 TYPE_ATTRIBUTES (t)
1668 = tree_cons (get_identifier ("abi_tag"), data.tags,
1669 TYPE_ATTRIBUTES (t));
1672 mark_abi_tags (t, false);
1675 /* Return true, iff class T has a non-virtual destructor that is
1676 accessible from outside the class heirarchy (i.e. is public, or
1677 there's a suitable friend. */
1679 static bool
1680 accessible_nvdtor_p (tree t)
1682 tree dtor = CLASSTYPE_DESTRUCTOR (t);
1684 /* An implicitly declared destructor is always public. And,
1685 if it were virtual, we would have created it by now. */
1686 if (!dtor)
1687 return true;
1689 if (DECL_VINDEX (dtor))
1690 return false; /* Virtual */
1692 if (!TREE_PRIVATE (dtor) && !TREE_PROTECTED (dtor))
1693 return true; /* Public */
1695 if (CLASSTYPE_FRIEND_CLASSES (t)
1696 || DECL_FRIENDLIST (TYPE_MAIN_DECL (t)))
1697 return true; /* Has friends */
1699 return false;
1702 /* Run through the base classes of T, updating CANT_HAVE_CONST_CTOR_P,
1703 and NO_CONST_ASN_REF_P. Also set flag bits in T based on
1704 properties of the bases. */
1706 static void
1707 check_bases (tree t,
1708 int* cant_have_const_ctor_p,
1709 int* no_const_asn_ref_p)
1711 int i;
1712 bool seen_non_virtual_nearly_empty_base_p = 0;
1713 int seen_tm_mask = 0;
1714 tree base_binfo;
1715 tree binfo;
1716 tree field = NULL_TREE;
1718 if (!CLASSTYPE_NON_STD_LAYOUT (t))
1719 for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
1720 if (TREE_CODE (field) == FIELD_DECL)
1721 break;
1723 for (binfo = TYPE_BINFO (t), i = 0;
1724 BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
1726 tree basetype = TREE_TYPE (base_binfo);
1728 gcc_assert (COMPLETE_TYPE_P (basetype));
1730 if (CLASSTYPE_FINAL (basetype))
1731 error ("cannot derive from %<final%> base %qT in derived type %qT",
1732 basetype, t);
1734 /* If any base class is non-literal, so is the derived class. */
1735 if (!CLASSTYPE_LITERAL_P (basetype))
1736 CLASSTYPE_LITERAL_P (t) = false;
1738 /* If the base class doesn't have copy constructors or
1739 assignment operators that take const references, then the
1740 derived class cannot have such a member automatically
1741 generated. */
1742 if (TYPE_HAS_COPY_CTOR (basetype)
1743 && ! TYPE_HAS_CONST_COPY_CTOR (basetype))
1744 *cant_have_const_ctor_p = 1;
1745 if (TYPE_HAS_COPY_ASSIGN (basetype)
1746 && !TYPE_HAS_CONST_COPY_ASSIGN (basetype))
1747 *no_const_asn_ref_p = 1;
1749 if (BINFO_VIRTUAL_P (base_binfo))
1750 /* A virtual base does not effect nearly emptiness. */
1752 else if (CLASSTYPE_NEARLY_EMPTY_P (basetype))
1754 if (seen_non_virtual_nearly_empty_base_p)
1755 /* And if there is more than one nearly empty base, then the
1756 derived class is not nearly empty either. */
1757 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
1758 else
1759 /* Remember we've seen one. */
1760 seen_non_virtual_nearly_empty_base_p = 1;
1762 else if (!is_empty_class (basetype))
1763 /* If the base class is not empty or nearly empty, then this
1764 class cannot be nearly empty. */
1765 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
1767 /* A lot of properties from the bases also apply to the derived
1768 class. */
1769 TYPE_NEEDS_CONSTRUCTING (t) |= TYPE_NEEDS_CONSTRUCTING (basetype);
1770 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t)
1771 |= TYPE_HAS_NONTRIVIAL_DESTRUCTOR (basetype);
1772 TYPE_HAS_COMPLEX_COPY_ASSIGN (t)
1773 |= (TYPE_HAS_COMPLEX_COPY_ASSIGN (basetype)
1774 || !TYPE_HAS_COPY_ASSIGN (basetype));
1775 TYPE_HAS_COMPLEX_COPY_CTOR (t) |= (TYPE_HAS_COMPLEX_COPY_CTOR (basetype)
1776 || !TYPE_HAS_COPY_CTOR (basetype));
1777 TYPE_HAS_COMPLEX_MOVE_ASSIGN (t)
1778 |= TYPE_HAS_COMPLEX_MOVE_ASSIGN (basetype);
1779 TYPE_HAS_COMPLEX_MOVE_CTOR (t) |= TYPE_HAS_COMPLEX_MOVE_CTOR (basetype);
1780 TYPE_POLYMORPHIC_P (t) |= TYPE_POLYMORPHIC_P (basetype);
1781 CLASSTYPE_CONTAINS_EMPTY_CLASS_P (t)
1782 |= CLASSTYPE_CONTAINS_EMPTY_CLASS_P (basetype);
1783 TYPE_HAS_COMPLEX_DFLT (t) |= (!TYPE_HAS_DEFAULT_CONSTRUCTOR (basetype)
1784 || TYPE_HAS_COMPLEX_DFLT (basetype));
1785 SET_CLASSTYPE_READONLY_FIELDS_NEED_INIT
1786 (t, CLASSTYPE_READONLY_FIELDS_NEED_INIT (t)
1787 | CLASSTYPE_READONLY_FIELDS_NEED_INIT (basetype));
1788 SET_CLASSTYPE_REF_FIELDS_NEED_INIT
1789 (t, CLASSTYPE_REF_FIELDS_NEED_INIT (t)
1790 | CLASSTYPE_REF_FIELDS_NEED_INIT (basetype));
1791 if (TYPE_HAS_MUTABLE_P (basetype))
1792 CLASSTYPE_HAS_MUTABLE (t) = 1;
1794 /* A standard-layout class is a class that:
1796 * has no non-standard-layout base classes, */
1797 CLASSTYPE_NON_STD_LAYOUT (t) |= CLASSTYPE_NON_STD_LAYOUT (basetype);
1798 if (!CLASSTYPE_NON_STD_LAYOUT (t))
1800 tree basefield;
1801 /* ...has no base classes of the same type as the first non-static
1802 data member... */
1803 if (field && DECL_CONTEXT (field) == t
1804 && (same_type_ignoring_top_level_qualifiers_p
1805 (TREE_TYPE (field), basetype)))
1806 CLASSTYPE_NON_STD_LAYOUT (t) = 1;
1807 else
1808 /* ...either has no non-static data members in the most-derived
1809 class and at most one base class with non-static data
1810 members, or has no base classes with non-static data
1811 members */
1812 for (basefield = TYPE_FIELDS (basetype); basefield;
1813 basefield = DECL_CHAIN (basefield))
1814 if (TREE_CODE (basefield) == FIELD_DECL
1815 && !(DECL_FIELD_IS_BASE (basefield)
1816 && integer_zerop (DECL_SIZE (basefield))))
1818 if (field)
1819 CLASSTYPE_NON_STD_LAYOUT (t) = 1;
1820 else
1821 field = basefield;
1822 break;
1826 /* Don't bother collecting tm attributes if transactional memory
1827 support is not enabled. */
1828 if (flag_tm)
1830 tree tm_attr = find_tm_attribute (TYPE_ATTRIBUTES (basetype));
1831 if (tm_attr)
1832 seen_tm_mask |= tm_attr_to_mask (tm_attr);
1835 check_abi_tags (t, basetype);
1838 /* If one of the base classes had TM attributes, and the current class
1839 doesn't define its own, then the current class inherits one. */
1840 if (seen_tm_mask && !find_tm_attribute (TYPE_ATTRIBUTES (t)))
1842 tree tm_attr = tm_mask_to_attr (least_bit_hwi (seen_tm_mask));
1843 TYPE_ATTRIBUTES (t) = tree_cons (tm_attr, NULL, TYPE_ATTRIBUTES (t));
1847 /* Determine all the primary bases within T. Sets BINFO_PRIMARY_BASE_P for
1848 those that are primaries. Sets BINFO_LOST_PRIMARY_P for those
1849 that have had a nearly-empty virtual primary base stolen by some
1850 other base in the hierarchy. Determines CLASSTYPE_PRIMARY_BASE for
1851 T. */
1853 static void
1854 determine_primary_bases (tree t)
1856 unsigned i;
1857 tree primary = NULL_TREE;
1858 tree type_binfo = TYPE_BINFO (t);
1859 tree base_binfo;
1861 /* Determine the primary bases of our bases. */
1862 for (base_binfo = TREE_CHAIN (type_binfo); base_binfo;
1863 base_binfo = TREE_CHAIN (base_binfo))
1865 tree primary = CLASSTYPE_PRIMARY_BINFO (BINFO_TYPE (base_binfo));
1867 /* See if we're the non-virtual primary of our inheritance
1868 chain. */
1869 if (!BINFO_VIRTUAL_P (base_binfo))
1871 tree parent = BINFO_INHERITANCE_CHAIN (base_binfo);
1872 tree parent_primary = CLASSTYPE_PRIMARY_BINFO (BINFO_TYPE (parent));
1874 if (parent_primary
1875 && SAME_BINFO_TYPE_P (BINFO_TYPE (base_binfo),
1876 BINFO_TYPE (parent_primary)))
1877 /* We are the primary binfo. */
1878 BINFO_PRIMARY_P (base_binfo) = 1;
1880 /* Determine if we have a virtual primary base, and mark it so.
1882 if (primary && BINFO_VIRTUAL_P (primary))
1884 tree this_primary = copied_binfo (primary, base_binfo);
1886 if (BINFO_PRIMARY_P (this_primary))
1887 /* Someone already claimed this base. */
1888 BINFO_LOST_PRIMARY_P (base_binfo) = 1;
1889 else
1891 tree delta;
1893 BINFO_PRIMARY_P (this_primary) = 1;
1894 BINFO_INHERITANCE_CHAIN (this_primary) = base_binfo;
1896 /* A virtual binfo might have been copied from within
1897 another hierarchy. As we're about to use it as a
1898 primary base, make sure the offsets match. */
1899 delta = size_diffop_loc (input_location,
1900 fold_convert (ssizetype,
1901 BINFO_OFFSET (base_binfo)),
1902 fold_convert (ssizetype,
1903 BINFO_OFFSET (this_primary)));
1905 propagate_binfo_offsets (this_primary, delta);
1910 /* First look for a dynamic direct non-virtual base. */
1911 for (i = 0; BINFO_BASE_ITERATE (type_binfo, i, base_binfo); i++)
1913 tree basetype = BINFO_TYPE (base_binfo);
1915 if (TYPE_CONTAINS_VPTR_P (basetype) && !BINFO_VIRTUAL_P (base_binfo))
1917 primary = base_binfo;
1918 goto found;
1922 /* A "nearly-empty" virtual base class can be the primary base
1923 class, if no non-virtual polymorphic base can be found. Look for
1924 a nearly-empty virtual dynamic base that is not already a primary
1925 base of something in the hierarchy. If there is no such base,
1926 just pick the first nearly-empty virtual base. */
1928 for (base_binfo = TREE_CHAIN (type_binfo); base_binfo;
1929 base_binfo = TREE_CHAIN (base_binfo))
1930 if (BINFO_VIRTUAL_P (base_binfo)
1931 && CLASSTYPE_NEARLY_EMPTY_P (BINFO_TYPE (base_binfo)))
1933 if (!BINFO_PRIMARY_P (base_binfo))
1935 /* Found one that is not primary. */
1936 primary = base_binfo;
1937 goto found;
1939 else if (!primary)
1940 /* Remember the first candidate. */
1941 primary = base_binfo;
1944 found:
1945 /* If we've got a primary base, use it. */
1946 if (primary)
1948 tree basetype = BINFO_TYPE (primary);
1950 CLASSTYPE_PRIMARY_BINFO (t) = primary;
1951 if (BINFO_PRIMARY_P (primary))
1952 /* We are stealing a primary base. */
1953 BINFO_LOST_PRIMARY_P (BINFO_INHERITANCE_CHAIN (primary)) = 1;
1954 BINFO_PRIMARY_P (primary) = 1;
1955 if (BINFO_VIRTUAL_P (primary))
1957 tree delta;
1959 BINFO_INHERITANCE_CHAIN (primary) = type_binfo;
1960 /* A virtual binfo might have been copied from within
1961 another hierarchy. As we're about to use it as a primary
1962 base, make sure the offsets match. */
1963 delta = size_diffop_loc (input_location, ssize_int (0),
1964 fold_convert (ssizetype, BINFO_OFFSET (primary)));
1966 propagate_binfo_offsets (primary, delta);
1969 primary = TYPE_BINFO (basetype);
1971 TYPE_VFIELD (t) = TYPE_VFIELD (basetype);
1972 BINFO_VTABLE (type_binfo) = BINFO_VTABLE (primary);
1973 BINFO_VIRTUALS (type_binfo) = BINFO_VIRTUALS (primary);
1977 /* Update the variant types of T. */
1979 void
1980 fixup_type_variants (tree t)
1982 tree variants;
1984 if (!t)
1985 return;
1987 for (variants = TYPE_NEXT_VARIANT (t);
1988 variants;
1989 variants = TYPE_NEXT_VARIANT (variants))
1991 /* These fields are in the _TYPE part of the node, not in
1992 the TYPE_LANG_SPECIFIC component, so they are not shared. */
1993 TYPE_HAS_USER_CONSTRUCTOR (variants) = TYPE_HAS_USER_CONSTRUCTOR (t);
1994 TYPE_NEEDS_CONSTRUCTING (variants) = TYPE_NEEDS_CONSTRUCTING (t);
1995 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (variants)
1996 = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t);
1998 TYPE_POLYMORPHIC_P (variants) = TYPE_POLYMORPHIC_P (t);
2000 TYPE_BINFO (variants) = TYPE_BINFO (t);
2002 /* Copy whatever these are holding today. */
2003 TYPE_VFIELD (variants) = TYPE_VFIELD (t);
2004 TYPE_FIELDS (variants) = TYPE_FIELDS (t);
2008 /* KLASS is a class that we're applying may_alias to after the body is
2009 parsed. Fixup any POINTER_TO and REFERENCE_TO types. The
2010 canonical type(s) will be implicitly updated. */
2012 static void
2013 fixup_may_alias (tree klass)
2015 tree t, v;
2017 for (t = TYPE_POINTER_TO (klass); t; t = TYPE_NEXT_PTR_TO (t))
2018 for (v = TYPE_MAIN_VARIANT (t); v; v = TYPE_NEXT_VARIANT (v))
2019 TYPE_REF_CAN_ALIAS_ALL (v) = true;
2020 for (t = TYPE_REFERENCE_TO (klass); t; t = TYPE_NEXT_REF_TO (t))
2021 for (v = TYPE_MAIN_VARIANT (t); v; v = TYPE_NEXT_VARIANT (v))
2022 TYPE_REF_CAN_ALIAS_ALL (v) = true;
2025 /* Early variant fixups: we apply attributes at the beginning of the class
2026 definition, and we need to fix up any variants that have already been
2027 made via elaborated-type-specifier so that check_qualified_type works. */
2029 void
2030 fixup_attribute_variants (tree t)
2032 tree variants;
2034 if (!t)
2035 return;
2037 tree attrs = TYPE_ATTRIBUTES (t);
2038 unsigned align = TYPE_ALIGN (t);
2039 bool user_align = TYPE_USER_ALIGN (t);
2040 bool may_alias = lookup_attribute ("may_alias", attrs);
2042 if (may_alias)
2043 fixup_may_alias (t);
2045 for (variants = TYPE_NEXT_VARIANT (t);
2046 variants;
2047 variants = TYPE_NEXT_VARIANT (variants))
2049 /* These are the two fields that check_qualified_type looks at and
2050 are affected by attributes. */
2051 TYPE_ATTRIBUTES (variants) = attrs;
2052 unsigned valign = align;
2053 if (TYPE_USER_ALIGN (variants))
2054 valign = MAX (valign, TYPE_ALIGN (variants));
2055 else
2056 TYPE_USER_ALIGN (variants) = user_align;
2057 SET_TYPE_ALIGN (variants, valign);
2058 if (may_alias)
2059 fixup_may_alias (variants);
2063 /* Set memoizing fields and bits of T (and its variants) for later
2064 use. */
2066 static void
2067 finish_struct_bits (tree t)
2069 /* Fix up variants (if any). */
2070 fixup_type_variants (t);
2072 if (BINFO_N_BASE_BINFOS (TYPE_BINFO (t)) && TYPE_POLYMORPHIC_P (t))
2073 /* For a class w/o baseclasses, 'finish_struct' has set
2074 CLASSTYPE_PURE_VIRTUALS correctly (by definition).
2075 Similarly for a class whose base classes do not have vtables.
2076 When neither of these is true, we might have removed abstract
2077 virtuals (by providing a definition), added some (by declaring
2078 new ones), or redeclared ones from a base class. We need to
2079 recalculate what's really an abstract virtual at this point (by
2080 looking in the vtables). */
2081 get_pure_virtuals (t);
2083 /* If this type has a copy constructor or a destructor, force its
2084 mode to be BLKmode, and force its TREE_ADDRESSABLE bit to be
2085 nonzero. This will cause it to be passed by invisible reference
2086 and prevent it from being returned in a register. */
2087 if (type_has_nontrivial_copy_init (t)
2088 || TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t))
2090 tree variants;
2091 SET_DECL_MODE (TYPE_MAIN_DECL (t), BLKmode);
2092 for (variants = t; variants; variants = TYPE_NEXT_VARIANT (variants))
2094 SET_TYPE_MODE (variants, BLKmode);
2095 TREE_ADDRESSABLE (variants) = 1;
2100 /* Issue warnings about T having private constructors, but no friends,
2101 and so forth.
2103 HAS_NONPRIVATE_METHOD is nonzero if T has any non-private methods or
2104 static members. HAS_NONPRIVATE_STATIC_FN is nonzero if T has any
2105 non-private static member functions. */
2107 static void
2108 maybe_warn_about_overly_private_class (tree t)
2110 int has_member_fn = 0;
2111 int has_nonprivate_method = 0;
2113 if (!warn_ctor_dtor_privacy
2114 /* If the class has friends, those entities might create and
2115 access instances, so we should not warn. */
2116 || (CLASSTYPE_FRIEND_CLASSES (t)
2117 || DECL_FRIENDLIST (TYPE_MAIN_DECL (t)))
2118 /* We will have warned when the template was declared; there's
2119 no need to warn on every instantiation. */
2120 || CLASSTYPE_TEMPLATE_INSTANTIATION (t))
2121 /* There's no reason to even consider warning about this
2122 class. */
2123 return;
2125 /* We only issue one warning, if more than one applies, because
2126 otherwise, on code like:
2128 class A {
2129 // Oops - forgot `public:'
2130 A();
2131 A(const A&);
2132 ~A();
2135 we warn several times about essentially the same problem. */
2137 /* Check to see if all (non-constructor, non-destructor) member
2138 functions are private. (Since there are no friends or
2139 non-private statics, we can't ever call any of the private member
2140 functions.) */
2141 for (tree fn = TYPE_FIELDS (t); fn; fn = DECL_CHAIN (fn))
2142 if (!DECL_DECLARES_FUNCTION_P (fn))
2143 /* Not a function. */;
2144 else if (DECL_ARTIFICIAL (fn))
2145 /* We're not interested in compiler-generated methods; they don't
2146 provide any way to call private members. */;
2147 else if (!TREE_PRIVATE (fn))
2149 if (DECL_STATIC_FUNCTION_P (fn))
2150 /* A non-private static member function is just like a
2151 friend; it can create and invoke private member
2152 functions, and be accessed without a class
2153 instance. */
2154 return;
2156 has_nonprivate_method = 1;
2157 /* Keep searching for a static member function. */
2159 else if (!DECL_CONSTRUCTOR_P (fn) && !DECL_DESTRUCTOR_P (fn))
2160 has_member_fn = 1;
2162 if (!has_nonprivate_method && has_member_fn)
2164 /* There are no non-private methods, and there's at least one
2165 private member function that isn't a constructor or
2166 destructor. (If all the private members are
2167 constructors/destructors we want to use the code below that
2168 issues error messages specifically referring to
2169 constructors/destructors.) */
2170 unsigned i;
2171 tree binfo = TYPE_BINFO (t);
2173 for (i = 0; i != BINFO_N_BASE_BINFOS (binfo); i++)
2174 if (BINFO_BASE_ACCESS (binfo, i) != access_private_node)
2176 has_nonprivate_method = 1;
2177 break;
2179 if (!has_nonprivate_method)
2181 warning (OPT_Wctor_dtor_privacy,
2182 "all member functions in class %qT are private", t);
2183 return;
2187 /* Even if some of the member functions are non-private, the class
2188 won't be useful for much if all the constructors or destructors
2189 are private: such an object can never be created or destroyed. */
2190 if (tree dtor = CLASSTYPE_DESTRUCTOR (t))
2191 if (TREE_PRIVATE (dtor))
2193 warning (OPT_Wctor_dtor_privacy,
2194 "%q#T only defines a private destructor and has no friends",
2196 return;
2199 /* Warn about classes that have private constructors and no friends. */
2200 if (TYPE_HAS_USER_CONSTRUCTOR (t)
2201 /* Implicitly generated constructors are always public. */
2202 && !CLASSTYPE_LAZY_DEFAULT_CTOR (t))
2204 bool nonprivate_ctor = false;
2205 tree copy_or_move = NULL_TREE;
2207 /* If a non-template class does not define a copy
2208 constructor, one is defined for it, enabling it to avoid
2209 this warning. For a template class, this does not
2210 happen, and so we would normally get a warning on:
2212 template <class T> class C { private: C(); };
2214 To avoid this asymmetry, we check TYPE_HAS_COPY_CTOR. All
2215 complete non-template or fully instantiated classes have this
2216 flag set. */
2217 if (!TYPE_HAS_COPY_CTOR (t))
2218 nonprivate_ctor = true;
2219 else
2220 for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (t));
2221 !nonprivate_ctor && iter; ++iter)
2222 if (TREE_PRIVATE (*iter))
2223 continue;
2224 else if (copy_fn_p (*iter) || move_fn_p (*iter))
2225 /* Ideally, we wouldn't count any constructor that takes
2226 an argument of the class type as a parameter, because
2227 such things cannot be used to construct an instance of
2228 the class unless you already have one. */
2229 copy_or_move = *iter;
2230 else
2231 nonprivate_ctor = true;
2233 if (!nonprivate_ctor)
2235 warning (OPT_Wctor_dtor_privacy,
2236 "%q#T only defines private constructors and has no friends",
2238 if (copy_or_move)
2239 inform (DECL_SOURCE_LOCATION (copy_or_move),
2240 "%q#D is public, but requires an existing %q#T object",
2241 copy_or_move, t);
2242 return;
2247 /* Make BINFO's vtable have N entries, including RTTI entries,
2248 vbase and vcall offsets, etc. Set its type and call the back end
2249 to lay it out. */
2251 static void
2252 layout_vtable_decl (tree binfo, int n)
2254 tree atype;
2255 tree vtable;
2257 atype = build_array_of_n_type (vtable_entry_type, n);
2258 layout_type (atype);
2260 /* We may have to grow the vtable. */
2261 vtable = get_vtbl_decl_for_binfo (binfo);
2262 if (!same_type_p (TREE_TYPE (vtable), atype))
2264 TREE_TYPE (vtable) = atype;
2265 DECL_SIZE (vtable) = DECL_SIZE_UNIT (vtable) = NULL_TREE;
2266 layout_decl (vtable, 0);
2270 /* True iff FNDECL and BASE_FNDECL (both non-static member functions)
2271 have the same signature. */
2274 same_signature_p (const_tree fndecl, const_tree base_fndecl)
2276 /* One destructor overrides another if they are the same kind of
2277 destructor. */
2278 if (DECL_DESTRUCTOR_P (base_fndecl) && DECL_DESTRUCTOR_P (fndecl)
2279 && special_function_p (base_fndecl) == special_function_p (fndecl))
2280 return 1;
2281 /* But a non-destructor never overrides a destructor, nor vice
2282 versa, nor do different kinds of destructors override
2283 one-another. For example, a complete object destructor does not
2284 override a deleting destructor. */
2285 if (DECL_DESTRUCTOR_P (base_fndecl) || DECL_DESTRUCTOR_P (fndecl))
2286 return 0;
2288 if (DECL_NAME (fndecl) == DECL_NAME (base_fndecl)
2289 || (DECL_CONV_FN_P (fndecl)
2290 && DECL_CONV_FN_P (base_fndecl)
2291 && same_type_p (DECL_CONV_FN_TYPE (fndecl),
2292 DECL_CONV_FN_TYPE (base_fndecl))))
2294 tree fntype = TREE_TYPE (fndecl);
2295 tree base_fntype = TREE_TYPE (base_fndecl);
2296 if (type_memfn_quals (fntype) == type_memfn_quals (base_fntype)
2297 && type_memfn_rqual (fntype) == type_memfn_rqual (base_fntype)
2298 && compparms (FUNCTION_FIRST_USER_PARMTYPE (fndecl),
2299 FUNCTION_FIRST_USER_PARMTYPE (base_fndecl)))
2300 return 1;
2302 return 0;
2305 /* Returns TRUE if DERIVED is a binfo containing the binfo BASE as a
2306 subobject. */
2308 static bool
2309 base_derived_from (tree derived, tree base)
2311 tree probe;
2313 for (probe = base; probe; probe = BINFO_INHERITANCE_CHAIN (probe))
2315 if (probe == derived)
2316 return true;
2317 else if (BINFO_VIRTUAL_P (probe))
2318 /* If we meet a virtual base, we can't follow the inheritance
2319 any more. See if the complete type of DERIVED contains
2320 such a virtual base. */
2321 return (binfo_for_vbase (BINFO_TYPE (probe), BINFO_TYPE (derived))
2322 != NULL_TREE);
2324 return false;
2327 struct find_final_overrider_data {
2328 /* The function for which we are trying to find a final overrider. */
2329 tree fn;
2330 /* The base class in which the function was declared. */
2331 tree declaring_base;
2332 /* The candidate overriders. */
2333 tree candidates;
2334 /* Path to most derived. */
2335 vec<tree> path;
2338 /* Add the overrider along the current path to FFOD->CANDIDATES.
2339 Returns true if an overrider was found; false otherwise. */
2341 static bool
2342 dfs_find_final_overrider_1 (tree binfo,
2343 find_final_overrider_data *ffod,
2344 unsigned depth)
2346 tree method;
2348 /* If BINFO is not the most derived type, try a more derived class.
2349 A definition there will overrider a definition here. */
2350 if (depth)
2352 depth--;
2353 if (dfs_find_final_overrider_1
2354 (ffod->path[depth], ffod, depth))
2355 return true;
2358 method = look_for_overrides_here (BINFO_TYPE (binfo), ffod->fn);
2359 if (method)
2361 tree *candidate = &ffod->candidates;
2363 /* Remove any candidates overridden by this new function. */
2364 while (*candidate)
2366 /* If *CANDIDATE overrides METHOD, then METHOD
2367 cannot override anything else on the list. */
2368 if (base_derived_from (TREE_VALUE (*candidate), binfo))
2369 return true;
2370 /* If METHOD overrides *CANDIDATE, remove *CANDIDATE. */
2371 if (base_derived_from (binfo, TREE_VALUE (*candidate)))
2372 *candidate = TREE_CHAIN (*candidate);
2373 else
2374 candidate = &TREE_CHAIN (*candidate);
2377 /* Add the new function. */
2378 ffod->candidates = tree_cons (method, binfo, ffod->candidates);
2379 return true;
2382 return false;
2385 /* Called from find_final_overrider via dfs_walk. */
2387 static tree
2388 dfs_find_final_overrider_pre (tree binfo, void *data)
2390 find_final_overrider_data *ffod = (find_final_overrider_data *) data;
2392 if (binfo == ffod->declaring_base)
2393 dfs_find_final_overrider_1 (binfo, ffod, ffod->path.length ());
2394 ffod->path.safe_push (binfo);
2396 return NULL_TREE;
2399 static tree
2400 dfs_find_final_overrider_post (tree /*binfo*/, void *data)
2402 find_final_overrider_data *ffod = (find_final_overrider_data *) data;
2403 ffod->path.pop ();
2405 return NULL_TREE;
2408 /* Returns a TREE_LIST whose TREE_PURPOSE is the final overrider for
2409 FN and whose TREE_VALUE is the binfo for the base where the
2410 overriding occurs. BINFO (in the hierarchy dominated by the binfo
2411 DERIVED) is the base object in which FN is declared. */
2413 static tree
2414 find_final_overrider (tree derived, tree binfo, tree fn)
2416 find_final_overrider_data ffod;
2418 /* Getting this right is a little tricky. This is valid:
2420 struct S { virtual void f (); };
2421 struct T { virtual void f (); };
2422 struct U : public S, public T { };
2424 even though calling `f' in `U' is ambiguous. But,
2426 struct R { virtual void f(); };
2427 struct S : virtual public R { virtual void f (); };
2428 struct T : virtual public R { virtual void f (); };
2429 struct U : public S, public T { };
2431 is not -- there's no way to decide whether to put `S::f' or
2432 `T::f' in the vtable for `R'.
2434 The solution is to look at all paths to BINFO. If we find
2435 different overriders along any two, then there is a problem. */
2436 if (DECL_THUNK_P (fn))
2437 fn = THUNK_TARGET (fn);
2439 /* Determine the depth of the hierarchy. */
2440 ffod.fn = fn;
2441 ffod.declaring_base = binfo;
2442 ffod.candidates = NULL_TREE;
2443 ffod.path.create (30);
2445 dfs_walk_all (derived, dfs_find_final_overrider_pre,
2446 dfs_find_final_overrider_post, &ffod);
2448 ffod.path.release ();
2450 /* If there was no winner, issue an error message. */
2451 if (!ffod.candidates || TREE_CHAIN (ffod.candidates))
2452 return error_mark_node;
2454 return ffod.candidates;
2457 /* Return the index of the vcall offset for FN when TYPE is used as a
2458 virtual base. */
2460 static tree
2461 get_vcall_index (tree fn, tree type)
2463 vec<tree_pair_s, va_gc> *indices = CLASSTYPE_VCALL_INDICES (type);
2464 tree_pair_p p;
2465 unsigned ix;
2467 FOR_EACH_VEC_SAFE_ELT (indices, ix, p)
2468 if ((DECL_DESTRUCTOR_P (fn) && DECL_DESTRUCTOR_P (p->purpose))
2469 || same_signature_p (fn, p->purpose))
2470 return p->value;
2472 /* There should always be an appropriate index. */
2473 gcc_unreachable ();
2476 /* Update an entry in the vtable for BINFO, which is in the hierarchy
2477 dominated by T. FN is the old function; VIRTUALS points to the
2478 corresponding position in the new BINFO_VIRTUALS list. IX is the index
2479 of that entry in the list. */
2481 static void
2482 update_vtable_entry_for_fn (tree t, tree binfo, tree fn, tree* virtuals,
2483 unsigned ix)
2485 tree b;
2486 tree overrider;
2487 tree delta;
2488 tree virtual_base;
2489 tree first_defn;
2490 tree overrider_fn, overrider_target;
2491 tree target_fn = DECL_THUNK_P (fn) ? THUNK_TARGET (fn) : fn;
2492 tree over_return, base_return;
2493 bool lost = false;
2495 /* Find the nearest primary base (possibly binfo itself) which defines
2496 this function; this is the class the caller will convert to when
2497 calling FN through BINFO. */
2498 for (b = binfo; ; b = get_primary_binfo (b))
2500 gcc_assert (b);
2501 if (look_for_overrides_here (BINFO_TYPE (b), target_fn))
2502 break;
2504 /* The nearest definition is from a lost primary. */
2505 if (BINFO_LOST_PRIMARY_P (b))
2506 lost = true;
2508 first_defn = b;
2510 /* Find the final overrider. */
2511 overrider = find_final_overrider (TYPE_BINFO (t), b, target_fn);
2512 if (overrider == error_mark_node)
2514 error ("no unique final overrider for %qD in %qT", target_fn, t);
2515 return;
2517 overrider_target = overrider_fn = TREE_PURPOSE (overrider);
2519 /* Check for adjusting covariant return types. */
2520 over_return = TREE_TYPE (TREE_TYPE (overrider_target));
2521 base_return = TREE_TYPE (TREE_TYPE (target_fn));
2523 if (POINTER_TYPE_P (over_return)
2524 && TREE_CODE (over_return) == TREE_CODE (base_return)
2525 && CLASS_TYPE_P (TREE_TYPE (over_return))
2526 && CLASS_TYPE_P (TREE_TYPE (base_return))
2527 /* If the overrider is invalid, don't even try. */
2528 && !DECL_INVALID_OVERRIDER_P (overrider_target))
2530 /* If FN is a covariant thunk, we must figure out the adjustment
2531 to the final base FN was converting to. As OVERRIDER_TARGET might
2532 also be converting to the return type of FN, we have to
2533 combine the two conversions here. */
2534 tree fixed_offset, virtual_offset;
2536 over_return = TREE_TYPE (over_return);
2537 base_return = TREE_TYPE (base_return);
2539 if (DECL_THUNK_P (fn))
2541 gcc_assert (DECL_RESULT_THUNK_P (fn));
2542 fixed_offset = ssize_int (THUNK_FIXED_OFFSET (fn));
2543 virtual_offset = THUNK_VIRTUAL_OFFSET (fn);
2545 else
2546 fixed_offset = virtual_offset = NULL_TREE;
2548 if (virtual_offset)
2549 /* Find the equivalent binfo within the return type of the
2550 overriding function. We will want the vbase offset from
2551 there. */
2552 virtual_offset = binfo_for_vbase (BINFO_TYPE (virtual_offset),
2553 over_return);
2554 else if (!same_type_ignoring_top_level_qualifiers_p
2555 (over_return, base_return))
2557 /* There was no existing virtual thunk (which takes
2558 precedence). So find the binfo of the base function's
2559 return type within the overriding function's return type.
2560 Fortunately we know the covariancy is valid (it
2561 has already been checked), so we can just iterate along
2562 the binfos, which have been chained in inheritance graph
2563 order. Of course it is lame that we have to repeat the
2564 search here anyway -- we should really be caching pieces
2565 of the vtable and avoiding this repeated work. */
2566 tree thunk_binfo, base_binfo;
2568 /* Find the base binfo within the overriding function's
2569 return type. We will always find a thunk_binfo, except
2570 when the covariancy is invalid (which we will have
2571 already diagnosed). */
2572 for (base_binfo = TYPE_BINFO (base_return),
2573 thunk_binfo = TYPE_BINFO (over_return);
2574 thunk_binfo;
2575 thunk_binfo = TREE_CHAIN (thunk_binfo))
2576 if (SAME_BINFO_TYPE_P (BINFO_TYPE (thunk_binfo),
2577 BINFO_TYPE (base_binfo)))
2578 break;
2580 /* See if virtual inheritance is involved. */
2581 for (virtual_offset = thunk_binfo;
2582 virtual_offset;
2583 virtual_offset = BINFO_INHERITANCE_CHAIN (virtual_offset))
2584 if (BINFO_VIRTUAL_P (virtual_offset))
2585 break;
2587 if (virtual_offset
2588 || (thunk_binfo && !BINFO_OFFSET_ZEROP (thunk_binfo)))
2590 tree offset = fold_convert (ssizetype, BINFO_OFFSET (thunk_binfo));
2592 if (virtual_offset)
2594 /* We convert via virtual base. Adjust the fixed
2595 offset to be from there. */
2596 offset =
2597 size_diffop (offset,
2598 fold_convert (ssizetype,
2599 BINFO_OFFSET (virtual_offset)));
2601 if (fixed_offset)
2602 /* There was an existing fixed offset, this must be
2603 from the base just converted to, and the base the
2604 FN was thunking to. */
2605 fixed_offset = size_binop (PLUS_EXPR, fixed_offset, offset);
2606 else
2607 fixed_offset = offset;
2611 if (fixed_offset || virtual_offset)
2612 /* Replace the overriding function with a covariant thunk. We
2613 will emit the overriding function in its own slot as
2614 well. */
2615 overrider_fn = make_thunk (overrider_target, /*this_adjusting=*/0,
2616 fixed_offset, virtual_offset);
2618 else
2619 gcc_assert (DECL_INVALID_OVERRIDER_P (overrider_target) ||
2620 !DECL_THUNK_P (fn));
2622 /* If we need a covariant thunk, then we may need to adjust first_defn.
2623 The ABI specifies that the thunks emitted with a function are
2624 determined by which bases the function overrides, so we need to be
2625 sure that we're using a thunk for some overridden base; even if we
2626 know that the necessary this adjustment is zero, there may not be an
2627 appropriate zero-this-adjustment thunk for us to use since thunks for
2628 overriding virtual bases always use the vcall offset.
2630 Furthermore, just choosing any base that overrides this function isn't
2631 quite right, as this slot won't be used for calls through a type that
2632 puts a covariant thunk here. Calling the function through such a type
2633 will use a different slot, and that slot is the one that determines
2634 the thunk emitted for that base.
2636 So, keep looking until we find the base that we're really overriding
2637 in this slot: the nearest primary base that doesn't use a covariant
2638 thunk in this slot. */
2639 if (overrider_target != overrider_fn)
2641 if (BINFO_TYPE (b) == DECL_CONTEXT (overrider_target))
2642 /* We already know that the overrider needs a covariant thunk. */
2643 b = get_primary_binfo (b);
2644 for (; ; b = get_primary_binfo (b))
2646 tree main_binfo = TYPE_BINFO (BINFO_TYPE (b));
2647 tree bv = chain_index (ix, BINFO_VIRTUALS (main_binfo));
2648 if (!DECL_THUNK_P (TREE_VALUE (bv)))
2649 break;
2650 if (BINFO_LOST_PRIMARY_P (b))
2651 lost = true;
2653 first_defn = b;
2656 /* Assume that we will produce a thunk that convert all the way to
2657 the final overrider, and not to an intermediate virtual base. */
2658 virtual_base = NULL_TREE;
2660 /* See if we can convert to an intermediate virtual base first, and then
2661 use the vcall offset located there to finish the conversion. */
2662 for (; b; b = BINFO_INHERITANCE_CHAIN (b))
2664 /* If we find the final overrider, then we can stop
2665 walking. */
2666 if (SAME_BINFO_TYPE_P (BINFO_TYPE (b),
2667 BINFO_TYPE (TREE_VALUE (overrider))))
2668 break;
2670 /* If we find a virtual base, and we haven't yet found the
2671 overrider, then there is a virtual base between the
2672 declaring base (first_defn) and the final overrider. */
2673 if (BINFO_VIRTUAL_P (b))
2675 virtual_base = b;
2676 break;
2680 /* Compute the constant adjustment to the `this' pointer. The
2681 `this' pointer, when this function is called, will point at BINFO
2682 (or one of its primary bases, which are at the same offset). */
2683 if (virtual_base)
2684 /* The `this' pointer needs to be adjusted from the declaration to
2685 the nearest virtual base. */
2686 delta = size_diffop_loc (input_location,
2687 fold_convert (ssizetype, BINFO_OFFSET (virtual_base)),
2688 fold_convert (ssizetype, BINFO_OFFSET (first_defn)));
2689 else if (lost)
2690 /* If the nearest definition is in a lost primary, we don't need an
2691 entry in our vtable. Except possibly in a constructor vtable,
2692 if we happen to get our primary back. In that case, the offset
2693 will be zero, as it will be a primary base. */
2694 delta = size_zero_node;
2695 else
2696 /* The `this' pointer needs to be adjusted from pointing to
2697 BINFO to pointing at the base where the final overrider
2698 appears. */
2699 delta = size_diffop_loc (input_location,
2700 fold_convert (ssizetype,
2701 BINFO_OFFSET (TREE_VALUE (overrider))),
2702 fold_convert (ssizetype, BINFO_OFFSET (binfo)));
2704 modify_vtable_entry (t, binfo, overrider_fn, delta, virtuals);
2706 if (virtual_base)
2707 BV_VCALL_INDEX (*virtuals)
2708 = get_vcall_index (overrider_target, BINFO_TYPE (virtual_base));
2709 else
2710 BV_VCALL_INDEX (*virtuals) = NULL_TREE;
2712 BV_LOST_PRIMARY (*virtuals) = lost;
2715 /* Called from modify_all_vtables via dfs_walk. */
2717 static tree
2718 dfs_modify_vtables (tree binfo, void* data)
2720 tree t = (tree) data;
2721 tree virtuals;
2722 tree old_virtuals;
2723 unsigned ix;
2725 if (!TYPE_CONTAINS_VPTR_P (BINFO_TYPE (binfo)))
2726 /* A base without a vtable needs no modification, and its bases
2727 are uninteresting. */
2728 return dfs_skip_bases;
2730 if (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), t)
2731 && !CLASSTYPE_HAS_PRIMARY_BASE_P (t))
2732 /* Don't do the primary vtable, if it's new. */
2733 return NULL_TREE;
2735 if (BINFO_PRIMARY_P (binfo) && !BINFO_VIRTUAL_P (binfo))
2736 /* There's no need to modify the vtable for a non-virtual primary
2737 base; we're not going to use that vtable anyhow. We do still
2738 need to do this for virtual primary bases, as they could become
2739 non-primary in a construction vtable. */
2740 return NULL_TREE;
2742 make_new_vtable (t, binfo);
2744 /* Now, go through each of the virtual functions in the virtual
2745 function table for BINFO. Find the final overrider, and update
2746 the BINFO_VIRTUALS list appropriately. */
2747 for (ix = 0, virtuals = BINFO_VIRTUALS (binfo),
2748 old_virtuals = BINFO_VIRTUALS (TYPE_BINFO (BINFO_TYPE (binfo)));
2749 virtuals;
2750 ix++, virtuals = TREE_CHAIN (virtuals),
2751 old_virtuals = TREE_CHAIN (old_virtuals))
2752 update_vtable_entry_for_fn (t,
2753 binfo,
2754 BV_FN (old_virtuals),
2755 &virtuals, ix);
2757 return NULL_TREE;
2760 /* Update all of the primary and secondary vtables for T. Create new
2761 vtables as required, and initialize their RTTI information. Each
2762 of the functions in VIRTUALS is declared in T and may override a
2763 virtual function from a base class; find and modify the appropriate
2764 entries to point to the overriding functions. Returns a list, in
2765 declaration order, of the virtual functions that are declared in T,
2766 but do not appear in the primary base class vtable, and which
2767 should therefore be appended to the end of the vtable for T. */
2769 static tree
2770 modify_all_vtables (tree t, tree virtuals)
2772 tree binfo = TYPE_BINFO (t);
2773 tree *fnsp;
2775 /* Mangle the vtable name before entering dfs_walk (c++/51884). */
2776 if (TYPE_CONTAINS_VPTR_P (t))
2777 get_vtable_decl (t, false);
2779 /* Update all of the vtables. */
2780 dfs_walk_once (binfo, dfs_modify_vtables, NULL, t);
2782 /* Add virtual functions not already in our primary vtable. These
2783 will be both those introduced by this class, and those overridden
2784 from secondary bases. It does not include virtuals merely
2785 inherited from secondary bases. */
2786 for (fnsp = &virtuals; *fnsp; )
2788 tree fn = TREE_VALUE (*fnsp);
2790 if (!value_member (fn, BINFO_VIRTUALS (binfo))
2791 || DECL_VINDEX (fn) == error_mark_node)
2793 /* We don't need to adjust the `this' pointer when
2794 calling this function. */
2795 BV_DELTA (*fnsp) = integer_zero_node;
2796 BV_VCALL_INDEX (*fnsp) = NULL_TREE;
2798 /* This is a function not already in our vtable. Keep it. */
2799 fnsp = &TREE_CHAIN (*fnsp);
2801 else
2802 /* We've already got an entry for this function. Skip it. */
2803 *fnsp = TREE_CHAIN (*fnsp);
2806 return virtuals;
2809 /* Get the base virtual function declarations in T that have the
2810 indicated NAME. */
2812 static void
2813 get_basefndecls (tree name, tree t, vec<tree> *base_fndecls)
2815 bool found_decls = false;
2817 /* Find virtual functions in T with the indicated NAME. */
2818 for (ovl_iterator iter (lookup_fnfields_slot (t, name)); iter; ++iter)
2820 tree method = *iter;
2822 if (TREE_CODE (method) == FUNCTION_DECL && DECL_VINDEX (method))
2824 base_fndecls->safe_push (method);
2825 found_decls = true;
2829 if (found_decls)
2830 return;
2832 int n_baseclasses = BINFO_N_BASE_BINFOS (TYPE_BINFO (t));
2833 for (int i = 0; i < n_baseclasses; i++)
2835 tree basetype = BINFO_TYPE (BINFO_BASE_BINFO (TYPE_BINFO (t), i));
2836 get_basefndecls (name, basetype, base_fndecls);
2840 /* If this declaration supersedes the declaration of
2841 a method declared virtual in the base class, then
2842 mark this field as being virtual as well. */
2844 void
2845 check_for_override (tree decl, tree ctype)
2847 bool overrides_found = false;
2848 if (TREE_CODE (decl) == TEMPLATE_DECL)
2849 /* In [temp.mem] we have:
2851 A specialization of a member function template does not
2852 override a virtual function from a base class. */
2853 return;
2854 if ((DECL_DESTRUCTOR_P (decl)
2855 || IDENTIFIER_VIRTUAL_P (DECL_NAME (decl))
2856 || DECL_CONV_FN_P (decl))
2857 && look_for_overrides (ctype, decl)
2858 && !DECL_STATIC_FUNCTION_P (decl))
2859 /* Set DECL_VINDEX to a value that is neither an INTEGER_CST nor
2860 the error_mark_node so that we know it is an overriding
2861 function. */
2863 DECL_VINDEX (decl) = decl;
2864 overrides_found = true;
2865 if (warn_override && !DECL_OVERRIDE_P (decl)
2866 && !DECL_DESTRUCTOR_P (decl))
2867 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wsuggest_override,
2868 "%qD can be marked override", decl);
2871 if (DECL_VIRTUAL_P (decl))
2873 if (!DECL_VINDEX (decl))
2874 DECL_VINDEX (decl) = error_mark_node;
2875 IDENTIFIER_VIRTUAL_P (DECL_NAME (decl)) = 1;
2876 if (DECL_DESTRUCTOR_P (decl))
2877 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (ctype) = true;
2879 else if (DECL_FINAL_P (decl))
2880 error ("%q+#D marked %<final%>, but is not virtual", decl);
2881 if (DECL_OVERRIDE_P (decl) && !overrides_found)
2882 error ("%q+#D marked %<override%>, but does not override", decl);
2885 /* Warn about hidden virtual functions that are not overridden in t.
2886 We know that constructors and destructors don't apply. */
2888 static void
2889 warn_hidden (tree t)
2891 vec<tree, va_gc> *method_vec = CLASSTYPE_METHOD_VEC (t);
2892 tree fns;
2894 /* We go through each separately named virtual function. */
2895 for (int i = 0; vec_safe_iterate (method_vec, i, &fns); ++i)
2897 tree name = OVL_NAME (fns);
2898 auto_vec<tree, 20> base_fndecls;
2899 tree base_binfo;
2900 tree binfo;
2901 int j;
2903 /* Iterate through all of the base classes looking for possibly
2904 hidden functions. */
2905 for (binfo = TYPE_BINFO (t), j = 0;
2906 BINFO_BASE_ITERATE (binfo, j, base_binfo); j++)
2908 tree basetype = BINFO_TYPE (base_binfo);
2909 get_basefndecls (name, basetype, &base_fndecls);
2912 /* If there are no functions to hide, continue. */
2913 if (base_fndecls.is_empty ())
2914 continue;
2916 /* Remove any overridden functions. */
2917 for (ovl_iterator iter (fns); iter; ++iter)
2919 tree fndecl = *iter;
2920 if (TREE_CODE (fndecl) == FUNCTION_DECL
2921 && DECL_VINDEX (fndecl))
2923 /* If the method from the base class has the same
2924 signature as the method from the derived class, it
2925 has been overridden. */
2926 for (size_t k = 0; k < base_fndecls.length (); k++)
2927 if (base_fndecls[k]
2928 && same_signature_p (fndecl, base_fndecls[k]))
2929 base_fndecls[k] = NULL_TREE;
2933 /* Now give a warning for all base functions without overriders,
2934 as they are hidden. */
2935 size_t k;
2936 tree base_fndecl;
2937 FOR_EACH_VEC_ELT (base_fndecls, k, base_fndecl)
2938 if (base_fndecl)
2940 /* Here we know it is a hider, and no overrider exists. */
2941 warning_at (location_of (base_fndecl),
2942 OPT_Woverloaded_virtual,
2943 "%qD was hidden", base_fndecl);
2944 warning_at (location_of (fns),
2945 OPT_Woverloaded_virtual, " by %qD", fns);
2950 /* Recursive helper for finish_struct_anon. */
2952 static void
2953 finish_struct_anon_r (tree field, bool complain)
2955 bool is_union = TREE_CODE (TREE_TYPE (field)) == UNION_TYPE;
2956 tree elt = TYPE_FIELDS (TREE_TYPE (field));
2957 for (; elt; elt = DECL_CHAIN (elt))
2959 /* We're generally only interested in entities the user
2960 declared, but we also find nested classes by noticing
2961 the TYPE_DECL that we create implicitly. You're
2962 allowed to put one anonymous union inside another,
2963 though, so we explicitly tolerate that. We use
2964 TYPE_UNNAMED_P rather than ANON_AGGR_TYPE_P so that
2965 we also allow unnamed types used for defining fields. */
2966 if (DECL_ARTIFICIAL (elt)
2967 && (!DECL_IMPLICIT_TYPEDEF_P (elt)
2968 || TYPE_UNNAMED_P (TREE_TYPE (elt))))
2969 continue;
2971 if (TREE_CODE (elt) != FIELD_DECL)
2973 /* We already complained about static data members in
2974 finish_static_data_member_decl. */
2975 if (complain && !VAR_P (elt))
2977 if (is_union)
2978 permerror (DECL_SOURCE_LOCATION (elt),
2979 "%q#D invalid; an anonymous union can "
2980 "only have non-static data members", elt);
2981 else
2982 permerror (DECL_SOURCE_LOCATION (elt),
2983 "%q#D invalid; an anonymous struct can "
2984 "only have non-static data members", elt);
2986 continue;
2989 if (complain)
2991 if (TREE_PRIVATE (elt))
2993 if (is_union)
2994 permerror (DECL_SOURCE_LOCATION (elt),
2995 "private member %q#D in anonymous union", elt);
2996 else
2997 permerror (DECL_SOURCE_LOCATION (elt),
2998 "private member %q#D in anonymous struct", elt);
3000 else if (TREE_PROTECTED (elt))
3002 if (is_union)
3003 permerror (DECL_SOURCE_LOCATION (elt),
3004 "protected member %q#D in anonymous union", elt);
3005 else
3006 permerror (DECL_SOURCE_LOCATION (elt),
3007 "protected member %q#D in anonymous struct", elt);
3011 TREE_PRIVATE (elt) = TREE_PRIVATE (field);
3012 TREE_PROTECTED (elt) = TREE_PROTECTED (field);
3014 /* Recurse into the anonymous aggregates to handle correctly
3015 access control (c++/24926):
3017 class A {
3018 union {
3019 union {
3020 int i;
3025 int j=A().i; */
3026 if (DECL_NAME (elt) == NULL_TREE
3027 && ANON_AGGR_TYPE_P (TREE_TYPE (elt)))
3028 finish_struct_anon_r (elt, /*complain=*/false);
3032 /* Check for things that are invalid. There are probably plenty of other
3033 things we should check for also. */
3035 static void
3036 finish_struct_anon (tree t)
3038 for (tree field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
3040 if (TREE_STATIC (field))
3041 continue;
3042 if (TREE_CODE (field) != FIELD_DECL)
3043 continue;
3045 if (DECL_NAME (field) == NULL_TREE
3046 && ANON_AGGR_TYPE_P (TREE_TYPE (field)))
3047 finish_struct_anon_r (field, /*complain=*/true);
3051 /* Add T to CLASSTYPE_DECL_LIST of current_class_type which
3052 will be used later during class template instantiation.
3053 When FRIEND_P is zero, T can be a static member data (VAR_DECL),
3054 a non-static member data (FIELD_DECL), a member function
3055 (FUNCTION_DECL), a nested type (RECORD_TYPE, ENUM_TYPE),
3056 a typedef (TYPE_DECL) or a member class template (TEMPLATE_DECL)
3057 When FRIEND_P is nonzero, T is either a friend class
3058 (RECORD_TYPE, TEMPLATE_DECL) or a friend function
3059 (FUNCTION_DECL, TEMPLATE_DECL). */
3061 void
3062 maybe_add_class_template_decl_list (tree type, tree t, int friend_p)
3064 /* Save some memory by not creating TREE_LIST if TYPE is not template. */
3065 if (CLASSTYPE_TEMPLATE_INFO (type))
3066 CLASSTYPE_DECL_LIST (type)
3067 = tree_cons (friend_p ? NULL_TREE : type,
3068 t, CLASSTYPE_DECL_LIST (type));
3071 /* This function is called from declare_virt_assop_and_dtor via
3072 dfs_walk_all.
3074 DATA is a type that direcly or indirectly inherits the base
3075 represented by BINFO. If BINFO contains a virtual assignment [copy
3076 assignment or move assigment] operator or a virtual constructor,
3077 declare that function in DATA if it hasn't been already declared. */
3079 static tree
3080 dfs_declare_virt_assop_and_dtor (tree binfo, void *data)
3082 tree bv, fn, t = (tree)data;
3083 tree opname = cp_assignment_operator_id (NOP_EXPR);
3085 gcc_assert (t && CLASS_TYPE_P (t));
3086 gcc_assert (binfo && TREE_CODE (binfo) == TREE_BINFO);
3088 if (!TYPE_CONTAINS_VPTR_P (BINFO_TYPE (binfo)))
3089 /* A base without a vtable needs no modification, and its bases
3090 are uninteresting. */
3091 return dfs_skip_bases;
3093 if (BINFO_PRIMARY_P (binfo))
3094 /* If this is a primary base, then we have already looked at the
3095 virtual functions of its vtable. */
3096 return NULL_TREE;
3098 for (bv = BINFO_VIRTUALS (binfo); bv; bv = TREE_CHAIN (bv))
3100 fn = BV_FN (bv);
3102 if (DECL_NAME (fn) == opname)
3104 if (CLASSTYPE_LAZY_COPY_ASSIGN (t))
3105 lazily_declare_fn (sfk_copy_assignment, t);
3106 if (CLASSTYPE_LAZY_MOVE_ASSIGN (t))
3107 lazily_declare_fn (sfk_move_assignment, t);
3109 else if (DECL_DESTRUCTOR_P (fn)
3110 && CLASSTYPE_LAZY_DESTRUCTOR (t))
3111 lazily_declare_fn (sfk_destructor, t);
3114 return NULL_TREE;
3117 /* If the class type T has a direct or indirect base that contains a
3118 virtual assignment operator or a virtual destructor, declare that
3119 function in T if it hasn't been already declared. */
3121 static void
3122 declare_virt_assop_and_dtor (tree t)
3124 if (!(TYPE_POLYMORPHIC_P (t)
3125 && (CLASSTYPE_LAZY_COPY_ASSIGN (t)
3126 || CLASSTYPE_LAZY_MOVE_ASSIGN (t)
3127 || CLASSTYPE_LAZY_DESTRUCTOR (t))))
3128 return;
3130 dfs_walk_all (TYPE_BINFO (t),
3131 dfs_declare_virt_assop_and_dtor,
3132 NULL, t);
3135 /* Declare the inheriting constructor for class T inherited from base
3136 constructor CTOR with the parameter array PARMS of size NPARMS. */
3138 static void
3139 one_inheriting_sig (tree t, tree ctor, tree *parms, int nparms)
3141 gcc_assert (TYPE_MAIN_VARIANT (t) == t);
3143 /* We don't declare an inheriting ctor that would be a default,
3144 copy or move ctor for derived or base. */
3145 if (nparms == 0)
3146 return;
3147 if (nparms == 1
3148 && TREE_CODE (parms[0]) == REFERENCE_TYPE)
3150 tree parm = TYPE_MAIN_VARIANT (TREE_TYPE (parms[0]));
3151 if (parm == t || parm == DECL_CONTEXT (ctor))
3152 return;
3155 tree parmlist = void_list_node;
3156 for (int i = nparms - 1; i >= 0; i--)
3157 parmlist = tree_cons (NULL_TREE, parms[i], parmlist);
3158 tree fn = implicitly_declare_fn (sfk_inheriting_constructor,
3159 t, false, ctor, parmlist);
3161 if (add_method (t, fn, false))
3163 DECL_CHAIN (fn) = TYPE_FIELDS (t);
3164 TYPE_FIELDS (t) = fn;
3168 /* Declare all the inheriting constructors for class T inherited from base
3169 constructor CTOR. */
3171 static void
3172 one_inherited_ctor (tree ctor, tree t, tree using_decl)
3174 tree parms = FUNCTION_FIRST_USER_PARMTYPE (ctor);
3176 if (flag_new_inheriting_ctors)
3178 ctor = implicitly_declare_fn (sfk_inheriting_constructor,
3179 t, /*const*/false, ctor, parms);
3180 add_method (t, ctor, using_decl != NULL_TREE);
3181 TYPE_HAS_USER_CONSTRUCTOR (t) = true;
3182 return;
3185 tree *new_parms = XALLOCAVEC (tree, list_length (parms));
3186 int i = 0;
3187 for (; parms && parms != void_list_node; parms = TREE_CHAIN (parms))
3189 if (TREE_PURPOSE (parms))
3190 one_inheriting_sig (t, ctor, new_parms, i);
3191 new_parms[i++] = TREE_VALUE (parms);
3193 one_inheriting_sig (t, ctor, new_parms, i);
3194 if (parms == NULL_TREE)
3196 if (warning (OPT_Winherited_variadic_ctor,
3197 "the ellipsis in %qD is not inherited", ctor))
3198 inform (DECL_SOURCE_LOCATION (ctor), "%qD declared here", ctor);
3202 /* Create default constructors, assignment operators, and so forth for
3203 the type indicated by T, if they are needed. CANT_HAVE_CONST_CTOR,
3204 and CANT_HAVE_CONST_ASSIGNMENT are nonzero if, for whatever reason,
3205 the class cannot have a default constructor, copy constructor
3206 taking a const reference argument, or an assignment operator taking
3207 a const reference, respectively. */
3209 static void
3210 add_implicitly_declared_members (tree t, tree* access_decls,
3211 int cant_have_const_cctor,
3212 int cant_have_const_assignment)
3214 /* Destructor. */
3215 if (!CLASSTYPE_DESTRUCTOR (t))
3216 /* In general, we create destructors lazily. */
3217 CLASSTYPE_LAZY_DESTRUCTOR (t) = 1;
3219 bool move_ok = false;
3220 if (cxx_dialect >= cxx11 && CLASSTYPE_LAZY_DESTRUCTOR (t)
3221 && !TYPE_HAS_COPY_CTOR (t) && !TYPE_HAS_COPY_ASSIGN (t)
3222 && !classtype_has_move_assign_or_move_ctor_p (t, false))
3223 move_ok = true;
3225 /* [class.ctor]
3227 If there is no user-declared constructor for a class, a default
3228 constructor is implicitly declared. */
3229 if (! TYPE_HAS_USER_CONSTRUCTOR (t))
3231 TYPE_HAS_DEFAULT_CONSTRUCTOR (t) = 1;
3232 CLASSTYPE_LAZY_DEFAULT_CTOR (t) = 1;
3233 if (cxx_dialect >= cxx11)
3234 TYPE_HAS_CONSTEXPR_CTOR (t)
3235 /* Don't force the declaration to get a hard answer; if the
3236 definition would have made the class non-literal, it will still be
3237 non-literal because of the base or member in question, and that
3238 gives a better diagnostic. */
3239 = type_maybe_constexpr_default_constructor (t);
3242 /* [class.ctor]
3244 If a class definition does not explicitly declare a copy
3245 constructor, one is declared implicitly. */
3246 if (! TYPE_HAS_COPY_CTOR (t))
3248 TYPE_HAS_COPY_CTOR (t) = 1;
3249 TYPE_HAS_CONST_COPY_CTOR (t) = !cant_have_const_cctor;
3250 CLASSTYPE_LAZY_COPY_CTOR (t) = 1;
3251 if (move_ok)
3252 CLASSTYPE_LAZY_MOVE_CTOR (t) = 1;
3255 /* If there is no assignment operator, one will be created if and
3256 when it is needed. For now, just record whether or not the type
3257 of the parameter to the assignment operator will be a const or
3258 non-const reference. */
3259 if (!TYPE_HAS_COPY_ASSIGN (t))
3261 TYPE_HAS_COPY_ASSIGN (t) = 1;
3262 TYPE_HAS_CONST_COPY_ASSIGN (t) = !cant_have_const_assignment;
3263 CLASSTYPE_LAZY_COPY_ASSIGN (t) = 1;
3264 if (move_ok && !LAMBDA_TYPE_P (t))
3265 CLASSTYPE_LAZY_MOVE_ASSIGN (t) = 1;
3268 /* We can't be lazy about declaring functions that might override
3269 a virtual function from a base class. */
3270 declare_virt_assop_and_dtor (t);
3272 while (*access_decls)
3274 tree using_decl = TREE_VALUE (*access_decls);
3275 tree decl = USING_DECL_DECLS (using_decl);
3276 if (DECL_NAME (using_decl) == ctor_identifier)
3278 /* declare, then remove the decl */
3279 tree ctor_list = decl;
3280 location_t loc = input_location;
3281 input_location = DECL_SOURCE_LOCATION (using_decl);
3282 for (ovl_iterator iter (ctor_list); iter; ++iter)
3283 one_inherited_ctor (*iter, t, using_decl);
3284 *access_decls = TREE_CHAIN (*access_decls);
3285 input_location = loc;
3287 else
3288 access_decls = &TREE_CHAIN (*access_decls);
3292 /* FIELD is a bit-field. We are finishing the processing for its
3293 enclosing type. Issue any appropriate messages and set appropriate
3294 flags. Returns false if an error has been diagnosed. */
3296 static bool
3297 check_bitfield_decl (tree field)
3299 tree type = TREE_TYPE (field);
3300 tree w;
3302 /* Extract the declared width of the bitfield, which has been
3303 temporarily stashed in DECL_INITIAL. */
3304 w = DECL_INITIAL (field);
3305 gcc_assert (w != NULL_TREE);
3306 /* Remove the bit-field width indicator so that the rest of the
3307 compiler does not treat that value as an initializer. */
3308 DECL_INITIAL (field) = NULL_TREE;
3310 /* Detect invalid bit-field type. */
3311 if (!INTEGRAL_OR_ENUMERATION_TYPE_P (type))
3313 error ("bit-field %q+#D with non-integral type", field);
3314 w = error_mark_node;
3316 else
3318 location_t loc = input_location;
3319 /* Avoid the non_lvalue wrapper added by fold for PLUS_EXPRs. */
3320 STRIP_NOPS (w);
3322 /* detect invalid field size. */
3323 input_location = DECL_SOURCE_LOCATION (field);
3324 w = cxx_constant_value (w);
3325 input_location = loc;
3327 if (TREE_CODE (w) != INTEGER_CST)
3329 error ("bit-field %q+D width not an integer constant", field);
3330 w = error_mark_node;
3332 else if (tree_int_cst_sgn (w) < 0)
3334 error ("negative width in bit-field %q+D", field);
3335 w = error_mark_node;
3337 else if (integer_zerop (w) && DECL_NAME (field) != 0)
3339 error ("zero width for bit-field %q+D", field);
3340 w = error_mark_node;
3342 else if ((TREE_CODE (type) != ENUMERAL_TYPE
3343 && TREE_CODE (type) != BOOLEAN_TYPE
3344 && compare_tree_int (w, TYPE_PRECISION (type)) > 0)
3345 || ((TREE_CODE (type) == ENUMERAL_TYPE
3346 || TREE_CODE (type) == BOOLEAN_TYPE)
3347 && tree_int_cst_lt (TYPE_SIZE (type), w)))
3348 warning_at (DECL_SOURCE_LOCATION (field), 0,
3349 "width of %qD exceeds its type", field);
3350 else if (TREE_CODE (type) == ENUMERAL_TYPE
3351 && (0 > (compare_tree_int
3352 (w, TYPE_PRECISION (ENUM_UNDERLYING_TYPE (type))))))
3353 warning_at (DECL_SOURCE_LOCATION (field), 0,
3354 "%qD is too small to hold all values of %q#T",
3355 field, type);
3358 if (w != error_mark_node)
3360 DECL_SIZE (field) = fold_convert (bitsizetype, w);
3361 DECL_BIT_FIELD (field) = 1;
3362 return true;
3364 else
3366 /* Non-bit-fields are aligned for their type. */
3367 DECL_BIT_FIELD (field) = 0;
3368 CLEAR_DECL_C_BIT_FIELD (field);
3369 return false;
3373 /* FIELD is a non bit-field. We are finishing the processing for its
3374 enclosing type T. Issue any appropriate messages and set appropriate
3375 flags. */
3377 static bool
3378 check_field_decl (tree field,
3379 tree t,
3380 int* cant_have_const_ctor,
3381 int* no_const_asn_ref)
3383 tree type = strip_array_types (TREE_TYPE (field));
3384 bool any_default_members = false;
3386 /* In C++98 an anonymous union cannot contain any fields which would change
3387 the settings of CANT_HAVE_CONST_CTOR and friends. */
3388 if (ANON_UNION_TYPE_P (type) && cxx_dialect < cxx11)
3390 /* And, we don't set TYPE_HAS_CONST_COPY_CTOR, etc., for anonymous
3391 structs. So, we recurse through their fields here. */
3392 else if (ANON_AGGR_TYPE_P (type))
3394 for (tree fields = TYPE_FIELDS (type); fields;
3395 fields = DECL_CHAIN (fields))
3396 if (TREE_CODE (fields) == FIELD_DECL && !DECL_C_BIT_FIELD (field))
3397 any_default_members |= check_field_decl (fields, t,
3398 cant_have_const_ctor,
3399 no_const_asn_ref);
3401 /* Check members with class type for constructors, destructors,
3402 etc. */
3403 else if (CLASS_TYPE_P (type))
3405 /* Never let anything with uninheritable virtuals
3406 make it through without complaint. */
3407 abstract_virtuals_error (field, type);
3409 if (TREE_CODE (t) == UNION_TYPE && cxx_dialect < cxx11)
3411 static bool warned;
3412 int oldcount = errorcount;
3413 if (TYPE_NEEDS_CONSTRUCTING (type))
3414 error ("member %q+#D with constructor not allowed in union",
3415 field);
3416 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
3417 error ("member %q+#D with destructor not allowed in union", field);
3418 if (TYPE_HAS_COMPLEX_COPY_ASSIGN (type))
3419 error ("member %q+#D with copy assignment operator not allowed in union",
3420 field);
3421 if (!warned && errorcount > oldcount)
3423 inform (DECL_SOURCE_LOCATION (field), "unrestricted unions "
3424 "only available with -std=c++11 or -std=gnu++11");
3425 warned = true;
3428 else
3430 TYPE_NEEDS_CONSTRUCTING (t) |= TYPE_NEEDS_CONSTRUCTING (type);
3431 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t)
3432 |= TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type);
3433 TYPE_HAS_COMPLEX_COPY_ASSIGN (t)
3434 |= (TYPE_HAS_COMPLEX_COPY_ASSIGN (type)
3435 || !TYPE_HAS_COPY_ASSIGN (type));
3436 TYPE_HAS_COMPLEX_COPY_CTOR (t) |= (TYPE_HAS_COMPLEX_COPY_CTOR (type)
3437 || !TYPE_HAS_COPY_CTOR (type));
3438 TYPE_HAS_COMPLEX_MOVE_ASSIGN (t) |= TYPE_HAS_COMPLEX_MOVE_ASSIGN (type);
3439 TYPE_HAS_COMPLEX_MOVE_CTOR (t) |= TYPE_HAS_COMPLEX_MOVE_CTOR (type);
3440 TYPE_HAS_COMPLEX_DFLT (t) |= (!TYPE_HAS_DEFAULT_CONSTRUCTOR (type)
3441 || TYPE_HAS_COMPLEX_DFLT (type));
3444 if (TYPE_HAS_COPY_CTOR (type)
3445 && !TYPE_HAS_CONST_COPY_CTOR (type))
3446 *cant_have_const_ctor = 1;
3448 if (TYPE_HAS_COPY_ASSIGN (type)
3449 && !TYPE_HAS_CONST_COPY_ASSIGN (type))
3450 *no_const_asn_ref = 1;
3453 check_abi_tags (t, field);
3455 if (DECL_INITIAL (field) != NULL_TREE)
3456 /* `build_class_init_list' does not recognize
3457 non-FIELD_DECLs. */
3458 any_default_members = true;
3460 return any_default_members;
3463 /* Check the data members (both static and non-static), class-scoped
3464 typedefs, etc., appearing in the declaration of T. Issue
3465 appropriate diagnostics. Sets ACCESS_DECLS to a list (in
3466 declaration order) of access declarations; each TREE_VALUE in this
3467 list is a USING_DECL.
3469 In addition, set the following flags:
3471 EMPTY_P
3472 The class is empty, i.e., contains no non-static data members.
3474 CANT_HAVE_CONST_CTOR_P
3475 This class cannot have an implicitly generated copy constructor
3476 taking a const reference.
3478 CANT_HAVE_CONST_ASN_REF
3479 This class cannot have an implicitly generated assignment
3480 operator taking a const reference.
3482 All of these flags should be initialized before calling this
3483 function.
3485 Returns a pointer to the end of the TYPE_FIELDs chain; additional
3486 fields can be added by adding to this chain. */
3488 static void
3489 check_field_decls (tree t, tree *access_decls,
3490 int *cant_have_const_ctor_p,
3491 int *no_const_asn_ref_p)
3493 tree *field;
3494 tree *next;
3495 bool has_pointers;
3496 bool any_default_members;
3497 int cant_pack = 0;
3498 int field_access = -1;
3500 /* Assume there are no access declarations. */
3501 *access_decls = NULL_TREE;
3502 /* Assume this class has no pointer members. */
3503 has_pointers = false;
3504 /* Assume none of the members of this class have default
3505 initializations. */
3506 any_default_members = false;
3508 for (field = &TYPE_FIELDS (t); *field; field = next)
3510 tree x = *field;
3511 tree type = TREE_TYPE (x);
3512 int this_field_access;
3514 next = &DECL_CHAIN (x);
3516 if (TREE_CODE (x) == USING_DECL)
3518 /* Save the access declarations for our caller. */
3519 *access_decls = tree_cons (NULL_TREE, x, *access_decls);
3520 continue;
3523 if (TREE_CODE (x) == TYPE_DECL
3524 || TREE_CODE (x) == TEMPLATE_DECL)
3525 continue;
3527 if (TREE_CODE (x) == FUNCTION_DECL)
3528 /* FIXME: We should fold in the checking from check_methods. */
3529 continue;
3531 /* If we've gotten this far, it's a data member, possibly static,
3532 or an enumerator. */
3533 if (TREE_CODE (x) != CONST_DECL)
3534 DECL_CONTEXT (x) = t;
3536 /* When this goes into scope, it will be a non-local reference. */
3537 DECL_NONLOCAL (x) = 1;
3539 if (TREE_CODE (t) == UNION_TYPE)
3541 /* [class.union] (C++98)
3543 If a union contains a static data member, or a member of
3544 reference type, the program is ill-formed.
3546 In C++11 [class.union] says:
3547 If a union contains a non-static data member of reference type
3548 the program is ill-formed. */
3549 if (VAR_P (x) && cxx_dialect < cxx11)
3551 error ("in C++98 %q+D may not be static because it is "
3552 "a member of a union", x);
3553 continue;
3555 if (TREE_CODE (type) == REFERENCE_TYPE
3556 && TREE_CODE (x) == FIELD_DECL)
3558 error ("non-static data member %q+D in a union may not "
3559 "have reference type %qT", x, type);
3560 continue;
3564 /* Perform error checking that did not get done in
3565 grokdeclarator. */
3566 if (TREE_CODE (type) == FUNCTION_TYPE)
3568 error ("field %q+D invalidly declared function type", x);
3569 type = build_pointer_type (type);
3570 TREE_TYPE (x) = type;
3572 else if (TREE_CODE (type) == METHOD_TYPE)
3574 error ("field %q+D invalidly declared method type", x);
3575 type = build_pointer_type (type);
3576 TREE_TYPE (x) = type;
3579 if (type == error_mark_node)
3580 continue;
3582 if (TREE_CODE (x) == CONST_DECL || VAR_P (x))
3583 continue;
3585 /* Now it can only be a FIELD_DECL. */
3587 if (TREE_PRIVATE (x) || TREE_PROTECTED (x))
3588 CLASSTYPE_NON_AGGREGATE (t) = 1;
3590 /* If at least one non-static data member is non-literal, the whole
3591 class becomes non-literal. Per Core/1453, volatile non-static
3592 data members and base classes are also not allowed.
3593 Note: if the type is incomplete we will complain later on. */
3594 if (COMPLETE_TYPE_P (type)
3595 && (!literal_type_p (type) || CP_TYPE_VOLATILE_P (type)))
3596 CLASSTYPE_LITERAL_P (t) = false;
3598 /* A standard-layout class is a class that:
3600 has the same access control (Clause 11) for all non-static data members,
3601 ... */
3602 this_field_access = TREE_PROTECTED (x) ? 1 : TREE_PRIVATE (x) ? 2 : 0;
3603 if (field_access == -1)
3604 field_access = this_field_access;
3605 else if (this_field_access != field_access)
3606 CLASSTYPE_NON_STD_LAYOUT (t) = 1;
3608 /* If this is of reference type, check if it needs an init. */
3609 if (TREE_CODE (type) == REFERENCE_TYPE)
3611 CLASSTYPE_NON_LAYOUT_POD_P (t) = 1;
3612 CLASSTYPE_NON_STD_LAYOUT (t) = 1;
3613 if (DECL_INITIAL (x) == NULL_TREE)
3614 SET_CLASSTYPE_REF_FIELDS_NEED_INIT (t, 1);
3615 if (cxx_dialect < cxx11)
3617 /* ARM $12.6.2: [A member initializer list] (or, for an
3618 aggregate, initialization by a brace-enclosed list) is the
3619 only way to initialize nonstatic const and reference
3620 members. */
3621 TYPE_HAS_COMPLEX_COPY_ASSIGN (t) = 1;
3622 TYPE_HAS_COMPLEX_MOVE_ASSIGN (t) = 1;
3626 type = strip_array_types (type);
3628 if (TYPE_PACKED (t))
3630 if (!layout_pod_type_p (type) && !TYPE_PACKED (type))
3632 warning_at
3633 (DECL_SOURCE_LOCATION (x), 0,
3634 "ignoring packed attribute because of unpacked non-POD field %q#D",
3636 cant_pack = 1;
3638 else if (DECL_C_BIT_FIELD (x)
3639 || TYPE_ALIGN (TREE_TYPE (x)) > BITS_PER_UNIT)
3640 DECL_PACKED (x) = 1;
3643 if (DECL_C_BIT_FIELD (x) && integer_zerop (DECL_INITIAL (x)))
3644 /* We don't treat zero-width bitfields as making a class
3645 non-empty. */
3647 else
3649 /* The class is non-empty. */
3650 CLASSTYPE_EMPTY_P (t) = 0;
3651 /* The class is not even nearly empty. */
3652 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
3653 /* If one of the data members contains an empty class,
3654 so does T. */
3655 if (CLASS_TYPE_P (type)
3656 && CLASSTYPE_CONTAINS_EMPTY_CLASS_P (type))
3657 CLASSTYPE_CONTAINS_EMPTY_CLASS_P (t) = 1;
3660 /* This is used by -Weffc++ (see below). Warn only for pointers
3661 to members which might hold dynamic memory. So do not warn
3662 for pointers to functions or pointers to members. */
3663 if (TYPE_PTR_P (type)
3664 && !TYPE_PTRFN_P (type))
3665 has_pointers = true;
3667 if (CLASS_TYPE_P (type))
3669 if (CLASSTYPE_REF_FIELDS_NEED_INIT (type))
3670 SET_CLASSTYPE_REF_FIELDS_NEED_INIT (t, 1);
3671 if (CLASSTYPE_READONLY_FIELDS_NEED_INIT (type))
3672 SET_CLASSTYPE_READONLY_FIELDS_NEED_INIT (t, 1);
3675 if (DECL_MUTABLE_P (x) || TYPE_HAS_MUTABLE_P (type))
3676 CLASSTYPE_HAS_MUTABLE (t) = 1;
3678 if (DECL_MUTABLE_P (x))
3680 if (CP_TYPE_CONST_P (type))
3682 error ("member %q+D cannot be declared both %<const%> "
3683 "and %<mutable%>", x);
3684 continue;
3686 if (TREE_CODE (type) == REFERENCE_TYPE)
3688 error ("member %q+D cannot be declared as a %<mutable%> "
3689 "reference", x);
3690 continue;
3694 if (! layout_pod_type_p (type))
3695 /* DR 148 now allows pointers to members (which are POD themselves),
3696 to be allowed in POD structs. */
3697 CLASSTYPE_NON_LAYOUT_POD_P (t) = 1;
3699 if (!std_layout_type_p (type))
3700 CLASSTYPE_NON_STD_LAYOUT (t) = 1;
3702 if (! zero_init_p (type))
3703 CLASSTYPE_NON_ZERO_INIT_P (t) = 1;
3705 /* We set DECL_C_BIT_FIELD in grokbitfield.
3706 If the type and width are valid, we'll also set DECL_BIT_FIELD. */
3707 if ((! DECL_C_BIT_FIELD (x) || ! check_bitfield_decl (x))
3708 && check_field_decl (x, t,
3709 cant_have_const_ctor_p,
3710 no_const_asn_ref_p))
3712 if (any_default_members
3713 && TREE_CODE (t) == UNION_TYPE)
3714 error ("multiple fields in union %qT initialized", t);
3715 any_default_members = true;
3718 /* Now that we've removed bit-field widths from DECL_INITIAL,
3719 anything left in DECL_INITIAL is an NSDMI that makes the class
3720 non-aggregate in C++11. */
3721 if (DECL_INITIAL (x) && cxx_dialect < cxx14)
3722 CLASSTYPE_NON_AGGREGATE (t) = true;
3724 /* If any field is const, the structure type is pseudo-const. */
3725 if (CP_TYPE_CONST_P (type))
3727 C_TYPE_FIELDS_READONLY (t) = 1;
3728 if (DECL_INITIAL (x) == NULL_TREE)
3729 SET_CLASSTYPE_READONLY_FIELDS_NEED_INIT (t, 1);
3730 if (cxx_dialect < cxx11)
3732 /* ARM $12.6.2: [A member initializer list] (or, for an
3733 aggregate, initialization by a brace-enclosed list) is the
3734 only way to initialize nonstatic const and reference
3735 members. */
3736 TYPE_HAS_COMPLEX_COPY_ASSIGN (t) = 1;
3737 TYPE_HAS_COMPLEX_MOVE_ASSIGN (t) = 1;
3740 /* A field that is pseudo-const makes the structure likewise. */
3741 else if (CLASS_TYPE_P (type))
3743 C_TYPE_FIELDS_READONLY (t) |= C_TYPE_FIELDS_READONLY (type);
3744 SET_CLASSTYPE_READONLY_FIELDS_NEED_INIT (t,
3745 CLASSTYPE_READONLY_FIELDS_NEED_INIT (t)
3746 | CLASSTYPE_READONLY_FIELDS_NEED_INIT (type));
3749 /* Core issue 80: A nonstatic data member is required to have a
3750 different name from the class iff the class has a
3751 user-declared constructor. */
3752 if (constructor_name_p (DECL_NAME (x), t)
3753 && TYPE_HAS_USER_CONSTRUCTOR (t))
3754 permerror (DECL_SOURCE_LOCATION (x),
3755 "field %q#D with same name as class", x);
3758 /* Effective C++ rule 11: if a class has dynamic memory held by pointers,
3759 it should also define a copy constructor and an assignment operator to
3760 implement the correct copy semantic (deep vs shallow, etc.). As it is
3761 not feasible to check whether the constructors do allocate dynamic memory
3762 and store it within members, we approximate the warning like this:
3764 -- Warn only if there are members which are pointers
3765 -- Warn only if there is a non-trivial constructor (otherwise,
3766 there cannot be memory allocated).
3767 -- Warn only if there is a non-trivial destructor. We assume that the
3768 user at least implemented the cleanup correctly, and a destructor
3769 is needed to free dynamic memory.
3771 This seems enough for practical purposes. */
3772 if (warn_ecpp
3773 && has_pointers
3774 && TYPE_HAS_USER_CONSTRUCTOR (t)
3775 && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t)
3776 && !(TYPE_HAS_COPY_CTOR (t) && TYPE_HAS_COPY_ASSIGN (t)))
3778 warning (OPT_Weffc__, "%q#T has pointer data members", t);
3780 if (! TYPE_HAS_COPY_CTOR (t))
3782 warning (OPT_Weffc__,
3783 " but does not override %<%T(const %T&)%>", t, t);
3784 if (!TYPE_HAS_COPY_ASSIGN (t))
3785 warning (OPT_Weffc__, " or %<operator=(const %T&)%>", t);
3787 else if (! TYPE_HAS_COPY_ASSIGN (t))
3788 warning (OPT_Weffc__,
3789 " but does not override %<operator=(const %T&)%>", t);
3792 /* Non-static data member initializers make the default constructor
3793 non-trivial. */
3794 if (any_default_members)
3796 TYPE_NEEDS_CONSTRUCTING (t) = true;
3797 TYPE_HAS_COMPLEX_DFLT (t) = true;
3800 /* If any of the fields couldn't be packed, unset TYPE_PACKED. */
3801 if (cant_pack)
3802 TYPE_PACKED (t) = 0;
3804 /* Check anonymous struct/anonymous union fields. */
3805 finish_struct_anon (t);
3807 /* We've built up the list of access declarations in reverse order.
3808 Fix that now. */
3809 *access_decls = nreverse (*access_decls);
3812 /* If TYPE is an empty class type, records its OFFSET in the table of
3813 OFFSETS. */
3815 static int
3816 record_subobject_offset (tree type, tree offset, splay_tree offsets)
3818 splay_tree_node n;
3820 if (!is_empty_class (type))
3821 return 0;
3823 /* Record the location of this empty object in OFFSETS. */
3824 n = splay_tree_lookup (offsets, (splay_tree_key) offset);
3825 if (!n)
3826 n = splay_tree_insert (offsets,
3827 (splay_tree_key) offset,
3828 (splay_tree_value) NULL_TREE);
3829 n->value = ((splay_tree_value)
3830 tree_cons (NULL_TREE,
3831 type,
3832 (tree) n->value));
3834 return 0;
3837 /* Returns nonzero if TYPE is an empty class type and there is
3838 already an entry in OFFSETS for the same TYPE as the same OFFSET. */
3840 static int
3841 check_subobject_offset (tree type, tree offset, splay_tree offsets)
3843 splay_tree_node n;
3844 tree t;
3846 if (!is_empty_class (type))
3847 return 0;
3849 /* Record the location of this empty object in OFFSETS. */
3850 n = splay_tree_lookup (offsets, (splay_tree_key) offset);
3851 if (!n)
3852 return 0;
3854 for (t = (tree) n->value; t; t = TREE_CHAIN (t))
3855 if (same_type_p (TREE_VALUE (t), type))
3856 return 1;
3858 return 0;
3861 /* Walk through all the subobjects of TYPE (located at OFFSET). Call
3862 F for every subobject, passing it the type, offset, and table of
3863 OFFSETS. If VBASES_P is one, then virtual non-primary bases should
3864 be traversed.
3866 If MAX_OFFSET is non-NULL, then subobjects with an offset greater
3867 than MAX_OFFSET will not be walked.
3869 If F returns a nonzero value, the traversal ceases, and that value
3870 is returned. Otherwise, returns zero. */
3872 static int
3873 walk_subobject_offsets (tree type,
3874 subobject_offset_fn f,
3875 tree offset,
3876 splay_tree offsets,
3877 tree max_offset,
3878 int vbases_p)
3880 int r = 0;
3881 tree type_binfo = NULL_TREE;
3883 /* If this OFFSET is bigger than the MAX_OFFSET, then we should
3884 stop. */
3885 if (max_offset && tree_int_cst_lt (max_offset, offset))
3886 return 0;
3888 if (type == error_mark_node)
3889 return 0;
3891 if (!TYPE_P (type))
3893 type_binfo = type;
3894 type = BINFO_TYPE (type);
3897 if (CLASS_TYPE_P (type))
3899 tree field;
3900 tree binfo;
3901 int i;
3903 /* Avoid recursing into objects that are not interesting. */
3904 if (!CLASSTYPE_CONTAINS_EMPTY_CLASS_P (type))
3905 return 0;
3907 /* Record the location of TYPE. */
3908 r = (*f) (type, offset, offsets);
3909 if (r)
3910 return r;
3912 /* Iterate through the direct base classes of TYPE. */
3913 if (!type_binfo)
3914 type_binfo = TYPE_BINFO (type);
3915 for (i = 0; BINFO_BASE_ITERATE (type_binfo, i, binfo); i++)
3917 tree binfo_offset;
3919 if (BINFO_VIRTUAL_P (binfo))
3920 continue;
3922 tree orig_binfo;
3923 /* We cannot rely on BINFO_OFFSET being set for the base
3924 class yet, but the offsets for direct non-virtual
3925 bases can be calculated by going back to the TYPE. */
3926 orig_binfo = BINFO_BASE_BINFO (TYPE_BINFO (type), i);
3927 binfo_offset = size_binop (PLUS_EXPR,
3928 offset,
3929 BINFO_OFFSET (orig_binfo));
3931 r = walk_subobject_offsets (binfo,
3933 binfo_offset,
3934 offsets,
3935 max_offset,
3936 /*vbases_p=*/0);
3937 if (r)
3938 return r;
3941 if (CLASSTYPE_VBASECLASSES (type))
3943 unsigned ix;
3944 vec<tree, va_gc> *vbases;
3946 /* Iterate through the virtual base classes of TYPE. In G++
3947 3.2, we included virtual bases in the direct base class
3948 loop above, which results in incorrect results; the
3949 correct offsets for virtual bases are only known when
3950 working with the most derived type. */
3951 if (vbases_p)
3952 for (vbases = CLASSTYPE_VBASECLASSES (type), ix = 0;
3953 vec_safe_iterate (vbases, ix, &binfo); ix++)
3955 r = walk_subobject_offsets (binfo,
3957 size_binop (PLUS_EXPR,
3958 offset,
3959 BINFO_OFFSET (binfo)),
3960 offsets,
3961 max_offset,
3962 /*vbases_p=*/0);
3963 if (r)
3964 return r;
3966 else
3968 /* We still have to walk the primary base, if it is
3969 virtual. (If it is non-virtual, then it was walked
3970 above.) */
3971 tree vbase = get_primary_binfo (type_binfo);
3973 if (vbase && BINFO_VIRTUAL_P (vbase)
3974 && BINFO_PRIMARY_P (vbase)
3975 && BINFO_INHERITANCE_CHAIN (vbase) == type_binfo)
3977 r = (walk_subobject_offsets
3978 (vbase, f, offset,
3979 offsets, max_offset, /*vbases_p=*/0));
3980 if (r)
3981 return r;
3986 /* Iterate through the fields of TYPE. */
3987 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
3988 if (TREE_CODE (field) == FIELD_DECL
3989 && TREE_TYPE (field) != error_mark_node
3990 && !DECL_ARTIFICIAL (field))
3992 tree field_offset;
3994 field_offset = byte_position (field);
3996 r = walk_subobject_offsets (TREE_TYPE (field),
3998 size_binop (PLUS_EXPR,
3999 offset,
4000 field_offset),
4001 offsets,
4002 max_offset,
4003 /*vbases_p=*/1);
4004 if (r)
4005 return r;
4008 else if (TREE_CODE (type) == ARRAY_TYPE)
4010 tree element_type = strip_array_types (type);
4011 tree domain = TYPE_DOMAIN (type);
4012 tree index;
4014 /* Avoid recursing into objects that are not interesting. */
4015 if (!CLASS_TYPE_P (element_type)
4016 || !CLASSTYPE_CONTAINS_EMPTY_CLASS_P (element_type)
4017 || !domain
4018 || integer_minus_onep (TYPE_MAX_VALUE (domain)))
4019 return 0;
4021 /* Step through each of the elements in the array. */
4022 for (index = size_zero_node;
4023 !tree_int_cst_lt (TYPE_MAX_VALUE (domain), index);
4024 index = size_binop (PLUS_EXPR, index, size_one_node))
4026 r = walk_subobject_offsets (TREE_TYPE (type),
4028 offset,
4029 offsets,
4030 max_offset,
4031 /*vbases_p=*/1);
4032 if (r)
4033 return r;
4034 offset = size_binop (PLUS_EXPR, offset,
4035 TYPE_SIZE_UNIT (TREE_TYPE (type)));
4036 /* If this new OFFSET is bigger than the MAX_OFFSET, then
4037 there's no point in iterating through the remaining
4038 elements of the array. */
4039 if (max_offset && tree_int_cst_lt (max_offset, offset))
4040 break;
4044 return 0;
4047 /* Record all of the empty subobjects of TYPE (either a type or a
4048 binfo). If IS_DATA_MEMBER is true, then a non-static data member
4049 is being placed at OFFSET; otherwise, it is a base class that is
4050 being placed at OFFSET. */
4052 static void
4053 record_subobject_offsets (tree type,
4054 tree offset,
4055 splay_tree offsets,
4056 bool is_data_member)
4058 tree max_offset;
4059 /* If recording subobjects for a non-static data member or a
4060 non-empty base class , we do not need to record offsets beyond
4061 the size of the biggest empty class. Additional data members
4062 will go at the end of the class. Additional base classes will go
4063 either at offset zero (if empty, in which case they cannot
4064 overlap with offsets past the size of the biggest empty class) or
4065 at the end of the class.
4067 However, if we are placing an empty base class, then we must record
4068 all offsets, as either the empty class is at offset zero (where
4069 other empty classes might later be placed) or at the end of the
4070 class (where other objects might then be placed, so other empty
4071 subobjects might later overlap). */
4072 if (is_data_member
4073 || !is_empty_class (BINFO_TYPE (type)))
4074 max_offset = sizeof_biggest_empty_class;
4075 else
4076 max_offset = NULL_TREE;
4077 walk_subobject_offsets (type, record_subobject_offset, offset,
4078 offsets, max_offset, is_data_member);
4081 /* Returns nonzero if any of the empty subobjects of TYPE (located at
4082 OFFSET) conflict with entries in OFFSETS. If VBASES_P is nonzero,
4083 virtual bases of TYPE are examined. */
4085 static int
4086 layout_conflict_p (tree type,
4087 tree offset,
4088 splay_tree offsets,
4089 int vbases_p)
4091 splay_tree_node max_node;
4093 /* Get the node in OFFSETS that indicates the maximum offset where
4094 an empty subobject is located. */
4095 max_node = splay_tree_max (offsets);
4096 /* If there aren't any empty subobjects, then there's no point in
4097 performing this check. */
4098 if (!max_node)
4099 return 0;
4101 return walk_subobject_offsets (type, check_subobject_offset, offset,
4102 offsets, (tree) (max_node->key),
4103 vbases_p);
4106 /* DECL is a FIELD_DECL corresponding either to a base subobject of a
4107 non-static data member of the type indicated by RLI. BINFO is the
4108 binfo corresponding to the base subobject, OFFSETS maps offsets to
4109 types already located at those offsets. This function determines
4110 the position of the DECL. */
4112 static void
4113 layout_nonempty_base_or_field (record_layout_info rli,
4114 tree decl,
4115 tree binfo,
4116 splay_tree offsets)
4118 tree offset = NULL_TREE;
4119 bool field_p;
4120 tree type;
4122 if (binfo)
4124 /* For the purposes of determining layout conflicts, we want to
4125 use the class type of BINFO; TREE_TYPE (DECL) will be the
4126 CLASSTYPE_AS_BASE version, which does not contain entries for
4127 zero-sized bases. */
4128 type = TREE_TYPE (binfo);
4129 field_p = false;
4131 else
4133 type = TREE_TYPE (decl);
4134 field_p = true;
4137 /* Try to place the field. It may take more than one try if we have
4138 a hard time placing the field without putting two objects of the
4139 same type at the same address. */
4140 while (1)
4142 struct record_layout_info_s old_rli = *rli;
4144 /* Place this field. */
4145 place_field (rli, decl);
4146 offset = byte_position (decl);
4148 /* We have to check to see whether or not there is already
4149 something of the same type at the offset we're about to use.
4150 For example, consider:
4152 struct S {};
4153 struct T : public S { int i; };
4154 struct U : public S, public T {};
4156 Here, we put S at offset zero in U. Then, we can't put T at
4157 offset zero -- its S component would be at the same address
4158 as the S we already allocated. So, we have to skip ahead.
4159 Since all data members, including those whose type is an
4160 empty class, have nonzero size, any overlap can happen only
4161 with a direct or indirect base-class -- it can't happen with
4162 a data member. */
4163 /* In a union, overlap is permitted; all members are placed at
4164 offset zero. */
4165 if (TREE_CODE (rli->t) == UNION_TYPE)
4166 break;
4167 if (layout_conflict_p (field_p ? type : binfo, offset,
4168 offsets, field_p))
4170 /* Strip off the size allocated to this field. That puts us
4171 at the first place we could have put the field with
4172 proper alignment. */
4173 *rli = old_rli;
4175 /* Bump up by the alignment required for the type. */
4176 rli->bitpos
4177 = size_binop (PLUS_EXPR, rli->bitpos,
4178 bitsize_int (binfo
4179 ? CLASSTYPE_ALIGN (type)
4180 : TYPE_ALIGN (type)));
4181 normalize_rli (rli);
4183 else if (TREE_CODE (type) == NULLPTR_TYPE
4184 && warn_abi && abi_version_crosses (9))
4186 /* Before ABI v9, we were giving nullptr_t alignment of 1; if
4187 the offset wasn't aligned like a pointer when we started to
4188 layout this field, that affects its position. */
4189 tree pos = rli_size_unit_so_far (&old_rli);
4190 if (int_cst_value (pos) % TYPE_ALIGN_UNIT (ptr_type_node) != 0)
4192 if (abi_version_at_least (9))
4193 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wabi,
4194 "alignment of %qD increased in -fabi-version=9 "
4195 "(GCC 5.2)", decl);
4196 else
4197 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wabi, "alignment "
4198 "of %qD will increase in -fabi-version=9", decl);
4200 break;
4202 else
4203 /* There was no conflict. We're done laying out this field. */
4204 break;
4207 /* Now that we know where it will be placed, update its
4208 BINFO_OFFSET. */
4209 if (binfo && CLASS_TYPE_P (BINFO_TYPE (binfo)))
4210 /* Indirect virtual bases may have a nonzero BINFO_OFFSET at
4211 this point because their BINFO_OFFSET is copied from another
4212 hierarchy. Therefore, we may not need to add the entire
4213 OFFSET. */
4214 propagate_binfo_offsets (binfo,
4215 size_diffop_loc (input_location,
4216 fold_convert (ssizetype, offset),
4217 fold_convert (ssizetype,
4218 BINFO_OFFSET (binfo))));
4221 /* Returns true if TYPE is empty and OFFSET is nonzero. */
4223 static int
4224 empty_base_at_nonzero_offset_p (tree type,
4225 tree offset,
4226 splay_tree /*offsets*/)
4228 return is_empty_class (type) && !integer_zerop (offset);
4231 /* Layout the empty base BINFO. EOC indicates the byte currently just
4232 past the end of the class, and should be correctly aligned for a
4233 class of the type indicated by BINFO; OFFSETS gives the offsets of
4234 the empty bases allocated so far. T is the most derived
4235 type. Return nonzero iff we added it at the end. */
4237 static bool
4238 layout_empty_base (record_layout_info rli, tree binfo,
4239 tree eoc, splay_tree offsets)
4241 tree alignment;
4242 tree basetype = BINFO_TYPE (binfo);
4243 bool atend = false;
4245 /* This routine should only be used for empty classes. */
4246 gcc_assert (is_empty_class (basetype));
4247 alignment = ssize_int (CLASSTYPE_ALIGN_UNIT (basetype));
4249 if (!integer_zerop (BINFO_OFFSET (binfo)))
4250 propagate_binfo_offsets
4251 (binfo, size_diffop_loc (input_location,
4252 size_zero_node, BINFO_OFFSET (binfo)));
4254 /* This is an empty base class. We first try to put it at offset
4255 zero. */
4256 if (layout_conflict_p (binfo,
4257 BINFO_OFFSET (binfo),
4258 offsets,
4259 /*vbases_p=*/0))
4261 /* That didn't work. Now, we move forward from the next
4262 available spot in the class. */
4263 atend = true;
4264 propagate_binfo_offsets (binfo, fold_convert (ssizetype, eoc));
4265 while (1)
4267 if (!layout_conflict_p (binfo,
4268 BINFO_OFFSET (binfo),
4269 offsets,
4270 /*vbases_p=*/0))
4271 /* We finally found a spot where there's no overlap. */
4272 break;
4274 /* There's overlap here, too. Bump along to the next spot. */
4275 propagate_binfo_offsets (binfo, alignment);
4279 if (CLASSTYPE_USER_ALIGN (basetype))
4281 rli->record_align = MAX (rli->record_align, CLASSTYPE_ALIGN (basetype));
4282 if (warn_packed)
4283 rli->unpacked_align = MAX (rli->unpacked_align, CLASSTYPE_ALIGN (basetype));
4284 TYPE_USER_ALIGN (rli->t) = 1;
4287 return atend;
4290 /* Build the FIELD_DECL for BASETYPE as a base of T, add it to the chain of
4291 fields at NEXT_FIELD, and return it. */
4293 static tree
4294 build_base_field_1 (tree t, tree basetype, tree *&next_field)
4296 /* Create the FIELD_DECL. */
4297 gcc_assert (CLASSTYPE_AS_BASE (basetype));
4298 tree decl = build_decl (input_location,
4299 FIELD_DECL, NULL_TREE, CLASSTYPE_AS_BASE (basetype));
4300 DECL_ARTIFICIAL (decl) = 1;
4301 DECL_IGNORED_P (decl) = 1;
4302 DECL_FIELD_CONTEXT (decl) = t;
4303 DECL_SIZE (decl) = CLASSTYPE_SIZE (basetype);
4304 DECL_SIZE_UNIT (decl) = CLASSTYPE_SIZE_UNIT (basetype);
4305 SET_DECL_ALIGN (decl, CLASSTYPE_ALIGN (basetype));
4306 DECL_USER_ALIGN (decl) = CLASSTYPE_USER_ALIGN (basetype);
4307 SET_DECL_MODE (decl, TYPE_MODE (basetype));
4308 DECL_FIELD_IS_BASE (decl) = 1;
4310 /* Add the new FIELD_DECL to the list of fields for T. */
4311 DECL_CHAIN (decl) = *next_field;
4312 *next_field = decl;
4313 next_field = &DECL_CHAIN (decl);
4315 return decl;
4318 /* Layout the base given by BINFO in the class indicated by RLI.
4319 *BASE_ALIGN is a running maximum of the alignments of
4320 any base class. OFFSETS gives the location of empty base
4321 subobjects. T is the most derived type. Return nonzero if the new
4322 object cannot be nearly-empty. A new FIELD_DECL is inserted at
4323 *NEXT_FIELD, unless BINFO is for an empty base class.
4325 Returns the location at which the next field should be inserted. */
4327 static tree *
4328 build_base_field (record_layout_info rli, tree binfo,
4329 splay_tree offsets, tree *next_field)
4331 tree t = rli->t;
4332 tree basetype = BINFO_TYPE (binfo);
4334 if (!COMPLETE_TYPE_P (basetype))
4335 /* This error is now reported in xref_tag, thus giving better
4336 location information. */
4337 return next_field;
4339 /* Place the base class. */
4340 if (!is_empty_class (basetype))
4342 tree decl;
4344 /* The containing class is non-empty because it has a non-empty
4345 base class. */
4346 CLASSTYPE_EMPTY_P (t) = 0;
4348 /* Create the FIELD_DECL. */
4349 decl = build_base_field_1 (t, basetype, next_field);
4351 /* Try to place the field. It may take more than one try if we
4352 have a hard time placing the field without putting two
4353 objects of the same type at the same address. */
4354 layout_nonempty_base_or_field (rli, decl, binfo, offsets);
4356 else
4358 tree eoc;
4359 bool atend;
4361 /* On some platforms (ARM), even empty classes will not be
4362 byte-aligned. */
4363 eoc = round_up_loc (input_location,
4364 rli_size_unit_so_far (rli),
4365 CLASSTYPE_ALIGN_UNIT (basetype));
4366 atend = layout_empty_base (rli, binfo, eoc, offsets);
4367 /* A nearly-empty class "has no proper base class that is empty,
4368 not morally virtual, and at an offset other than zero." */
4369 if (!BINFO_VIRTUAL_P (binfo) && CLASSTYPE_NEARLY_EMPTY_P (t))
4371 if (atend)
4372 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
4373 /* The check above (used in G++ 3.2) is insufficient because
4374 an empty class placed at offset zero might itself have an
4375 empty base at a nonzero offset. */
4376 else if (walk_subobject_offsets (basetype,
4377 empty_base_at_nonzero_offset_p,
4378 size_zero_node,
4379 /*offsets=*/NULL,
4380 /*max_offset=*/NULL_TREE,
4381 /*vbases_p=*/true))
4382 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
4385 /* We used to not create a FIELD_DECL for empty base classes because of
4386 back end issues with overlapping FIELD_DECLs, but that doesn't seem to
4387 be a problem anymore. We need them to handle initialization of C++17
4388 aggregate bases. */
4389 if (cxx_dialect >= cxx1z && !BINFO_VIRTUAL_P (binfo))
4391 tree decl = build_base_field_1 (t, basetype, next_field);
4392 DECL_FIELD_OFFSET (decl) = BINFO_OFFSET (binfo);
4393 DECL_FIELD_BIT_OFFSET (decl) = bitsize_zero_node;
4394 SET_DECL_OFFSET_ALIGN (decl, BITS_PER_UNIT);
4397 /* An empty virtual base causes a class to be non-empty
4398 -- but in that case we do not need to clear CLASSTYPE_EMPTY_P
4399 here because that was already done when the virtual table
4400 pointer was created. */
4403 /* Record the offsets of BINFO and its base subobjects. */
4404 record_subobject_offsets (binfo,
4405 BINFO_OFFSET (binfo),
4406 offsets,
4407 /*is_data_member=*/false);
4409 return next_field;
4412 /* Layout all of the non-virtual base classes. Record empty
4413 subobjects in OFFSETS. T is the most derived type. Return nonzero
4414 if the type cannot be nearly empty. The fields created
4415 corresponding to the base classes will be inserted at
4416 *NEXT_FIELD. */
4418 static void
4419 build_base_fields (record_layout_info rli,
4420 splay_tree offsets, tree *next_field)
4422 /* Chain to hold all the new FIELD_DECLs which stand in for base class
4423 subobjects. */
4424 tree t = rli->t;
4425 int n_baseclasses = BINFO_N_BASE_BINFOS (TYPE_BINFO (t));
4426 int i;
4428 /* The primary base class is always allocated first. */
4429 if (CLASSTYPE_HAS_PRIMARY_BASE_P (t))
4430 next_field = build_base_field (rli, CLASSTYPE_PRIMARY_BINFO (t),
4431 offsets, next_field);
4433 /* Now allocate the rest of the bases. */
4434 for (i = 0; i < n_baseclasses; ++i)
4436 tree base_binfo;
4438 base_binfo = BINFO_BASE_BINFO (TYPE_BINFO (t), i);
4440 /* The primary base was already allocated above, so we don't
4441 need to allocate it again here. */
4442 if (base_binfo == CLASSTYPE_PRIMARY_BINFO (t))
4443 continue;
4445 /* Virtual bases are added at the end (a primary virtual base
4446 will have already been added). */
4447 if (BINFO_VIRTUAL_P (base_binfo))
4448 continue;
4450 next_field = build_base_field (rli, base_binfo,
4451 offsets, next_field);
4455 /* Go through the TYPE_FIELDS of T issuing any appropriate
4456 diagnostics, figuring out which methods override which other
4457 methods, and so forth. */
4459 static void
4460 check_methods (tree t)
4462 for (tree x = TYPE_FIELDS (t); x; x = DECL_CHAIN (x))
4463 if (DECL_DECLARES_FUNCTION_P (x))
4465 check_for_override (x, t);
4467 if (DECL_PURE_VIRTUAL_P (x)
4468 && (TREE_CODE (x) != FUNCTION_DECL || ! DECL_VINDEX (x)))
4469 error ("initializer specified for non-virtual method %q+D", x);
4470 /* The name of the field is the original field name
4471 Save this in auxiliary field for later overloading. */
4472 if (TREE_CODE (x) == FUNCTION_DECL && DECL_VINDEX (x))
4474 TYPE_POLYMORPHIC_P (t) = 1;
4475 if (DECL_PURE_VIRTUAL_P (x))
4476 vec_safe_push (CLASSTYPE_PURE_VIRTUALS (t), x);
4479 /* All user-provided destructors are non-trivial.
4480 Constructors and assignment ops are handled in
4481 grok_special_member_properties. */
4482 if (DECL_DESTRUCTOR_P (x) && user_provided_p (x))
4483 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t) = 1;
4484 if (!DECL_VIRTUAL_P (x)
4485 && lookup_attribute ("transaction_safe_dynamic",
4486 DECL_ATTRIBUTES (x)))
4487 error_at (DECL_SOURCE_LOCATION (x),
4488 "%<transaction_safe_dynamic%> may only be specified for "
4489 "a virtual function");
4493 /* FN is a constructor or destructor. Clone the declaration to create
4494 a specialized in-charge or not-in-charge version, as indicated by
4495 NAME. */
4497 static tree
4498 build_clone (tree fn, tree name)
4500 tree parms;
4501 tree clone;
4503 /* Copy the function. */
4504 clone = copy_decl (fn);
4505 /* Reset the function name. */
4506 DECL_NAME (clone) = name;
4507 /* Remember where this function came from. */
4508 DECL_ABSTRACT_ORIGIN (clone) = fn;
4509 /* Make it easy to find the CLONE given the FN. */
4510 DECL_CHAIN (clone) = DECL_CHAIN (fn);
4511 DECL_CHAIN (fn) = clone;
4513 /* If this is a template, do the rest on the DECL_TEMPLATE_RESULT. */
4514 if (TREE_CODE (clone) == TEMPLATE_DECL)
4516 tree result = build_clone (DECL_TEMPLATE_RESULT (clone), name);
4517 DECL_TEMPLATE_RESULT (clone) = result;
4518 DECL_TEMPLATE_INFO (result) = copy_node (DECL_TEMPLATE_INFO (result));
4519 DECL_TI_TEMPLATE (result) = clone;
4520 TREE_TYPE (clone) = TREE_TYPE (result);
4521 return clone;
4523 else
4525 // Clone constraints.
4526 if (flag_concepts)
4527 if (tree ci = get_constraints (fn))
4528 set_constraints (clone, copy_node (ci));
4532 SET_DECL_ASSEMBLER_NAME (clone, NULL_TREE);
4533 DECL_CLONED_FUNCTION (clone) = fn;
4534 /* There's no pending inline data for this function. */
4535 DECL_PENDING_INLINE_INFO (clone) = NULL;
4536 DECL_PENDING_INLINE_P (clone) = 0;
4538 /* The base-class destructor is not virtual. */
4539 if (name == base_dtor_identifier)
4541 DECL_VIRTUAL_P (clone) = 0;
4542 if (TREE_CODE (clone) != TEMPLATE_DECL)
4543 DECL_VINDEX (clone) = NULL_TREE;
4546 bool ctor_omit_inherited_parms_p = ctor_omit_inherited_parms (clone);
4547 if (ctor_omit_inherited_parms_p)
4548 gcc_assert (DECL_HAS_IN_CHARGE_PARM_P (clone));
4550 /* If there was an in-charge parameter, drop it from the function
4551 type. */
4552 if (DECL_HAS_IN_CHARGE_PARM_P (clone))
4554 tree basetype;
4555 tree parmtypes;
4556 tree exceptions;
4558 exceptions = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (clone));
4559 basetype = TYPE_METHOD_BASETYPE (TREE_TYPE (clone));
4560 parmtypes = TYPE_ARG_TYPES (TREE_TYPE (clone));
4561 /* Skip the `this' parameter. */
4562 parmtypes = TREE_CHAIN (parmtypes);
4563 /* Skip the in-charge parameter. */
4564 parmtypes = TREE_CHAIN (parmtypes);
4565 /* And the VTT parm, in a complete [cd]tor. */
4566 if (DECL_HAS_VTT_PARM_P (fn)
4567 && ! DECL_NEEDS_VTT_PARM_P (clone))
4568 parmtypes = TREE_CHAIN (parmtypes);
4569 if (ctor_omit_inherited_parms_p)
4571 /* If we're omitting inherited parms, that just leaves the VTT. */
4572 gcc_assert (DECL_NEEDS_VTT_PARM_P (clone));
4573 parmtypes = tree_cons (NULL_TREE, vtt_parm_type, void_list_node);
4575 TREE_TYPE (clone)
4576 = build_method_type_directly (basetype,
4577 TREE_TYPE (TREE_TYPE (clone)),
4578 parmtypes);
4579 if (exceptions)
4580 TREE_TYPE (clone) = build_exception_variant (TREE_TYPE (clone),
4581 exceptions);
4582 TREE_TYPE (clone)
4583 = cp_build_type_attribute_variant (TREE_TYPE (clone),
4584 TYPE_ATTRIBUTES (TREE_TYPE (fn)));
4587 /* Copy the function parameters. */
4588 DECL_ARGUMENTS (clone) = copy_list (DECL_ARGUMENTS (clone));
4589 /* Remove the in-charge parameter. */
4590 if (DECL_HAS_IN_CHARGE_PARM_P (clone))
4592 DECL_CHAIN (DECL_ARGUMENTS (clone))
4593 = DECL_CHAIN (DECL_CHAIN (DECL_ARGUMENTS (clone)));
4594 DECL_HAS_IN_CHARGE_PARM_P (clone) = 0;
4596 /* And the VTT parm, in a complete [cd]tor. */
4597 if (DECL_HAS_VTT_PARM_P (fn))
4599 if (DECL_NEEDS_VTT_PARM_P (clone))
4600 DECL_HAS_VTT_PARM_P (clone) = 1;
4601 else
4603 DECL_CHAIN (DECL_ARGUMENTS (clone))
4604 = DECL_CHAIN (DECL_CHAIN (DECL_ARGUMENTS (clone)));
4605 DECL_HAS_VTT_PARM_P (clone) = 0;
4609 /* A base constructor inheriting from a virtual base doesn't get the
4610 arguments. */
4611 if (ctor_omit_inherited_parms_p)
4612 DECL_CHAIN (DECL_CHAIN (DECL_ARGUMENTS (clone))) = NULL_TREE;
4614 for (parms = DECL_ARGUMENTS (clone); parms; parms = DECL_CHAIN (parms))
4616 DECL_CONTEXT (parms) = clone;
4617 cxx_dup_lang_specific_decl (parms);
4620 /* Create the RTL for this function. */
4621 SET_DECL_RTL (clone, NULL);
4622 rest_of_decl_compilation (clone, /*top_level=*/1, at_eof);
4624 return clone;
4627 /* Implementation of DECL_CLONED_FUNCTION and DECL_CLONED_FUNCTION_P, do
4628 not invoke this function directly.
4630 For a non-thunk function, returns the address of the slot for storing
4631 the function it is a clone of. Otherwise returns NULL_TREE.
4633 If JUST_TESTING, looks through TEMPLATE_DECL and returns NULL if
4634 cloned_function is unset. This is to support the separate
4635 DECL_CLONED_FUNCTION and DECL_CLONED_FUNCTION_P modes; using the latter
4636 on a template makes sense, but not the former. */
4638 tree *
4639 decl_cloned_function_p (const_tree decl, bool just_testing)
4641 tree *ptr;
4642 if (just_testing)
4643 decl = STRIP_TEMPLATE (decl);
4645 if (TREE_CODE (decl) != FUNCTION_DECL
4646 || !DECL_LANG_SPECIFIC (decl)
4647 || DECL_LANG_SPECIFIC (decl)->u.fn.thunk_p)
4649 #if defined ENABLE_TREE_CHECKING && (GCC_VERSION >= 2007)
4650 if (!just_testing)
4651 lang_check_failed (__FILE__, __LINE__, __FUNCTION__);
4652 else
4653 #endif
4654 return NULL;
4657 ptr = &DECL_LANG_SPECIFIC (decl)->u.fn.u5.cloned_function;
4658 if (just_testing && *ptr == NULL_TREE)
4659 return NULL;
4660 else
4661 return ptr;
4664 /* Produce declarations for all appropriate clones of FN. If
4665 UPDATE_METHODS is true, the clones are added to the
4666 CLASSTYPE_METHOD_VEC. */
4668 void
4669 clone_function_decl (tree fn, bool update_methods)
4671 tree clone;
4673 /* Avoid inappropriate cloning. */
4674 if (DECL_CHAIN (fn)
4675 && DECL_CLONED_FUNCTION_P (DECL_CHAIN (fn)))
4676 return;
4678 if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (fn))
4680 /* For each constructor, we need two variants: an in-charge version
4681 and a not-in-charge version. */
4682 clone = build_clone (fn, complete_ctor_identifier);
4683 if (update_methods)
4684 add_method (DECL_CONTEXT (clone), clone, false);
4685 clone = build_clone (fn, base_ctor_identifier);
4686 if (update_methods)
4687 add_method (DECL_CONTEXT (clone), clone, false);
4689 else
4691 gcc_assert (DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (fn));
4693 /* For each destructor, we need three variants: an in-charge
4694 version, a not-in-charge version, and an in-charge deleting
4695 version. We clone the deleting version first because that
4696 means it will go second on the TYPE_FIELDS list -- and that
4697 corresponds to the correct layout order in the virtual
4698 function table.
4700 For a non-virtual destructor, we do not build a deleting
4701 destructor. */
4702 if (DECL_VIRTUAL_P (fn))
4704 clone = build_clone (fn, deleting_dtor_identifier);
4705 if (update_methods)
4706 add_method (DECL_CONTEXT (clone), clone, false);
4708 clone = build_clone (fn, complete_dtor_identifier);
4709 if (update_methods)
4710 add_method (DECL_CONTEXT (clone), clone, false);
4711 clone = build_clone (fn, base_dtor_identifier);
4712 if (update_methods)
4713 add_method (DECL_CONTEXT (clone), clone, false);
4716 /* Note that this is an abstract function that is never emitted. */
4717 DECL_ABSTRACT_P (fn) = true;
4720 /* DECL is an in charge constructor, which is being defined. This will
4721 have had an in class declaration, from whence clones were
4722 declared. An out-of-class definition can specify additional default
4723 arguments. As it is the clones that are involved in overload
4724 resolution, we must propagate the information from the DECL to its
4725 clones. */
4727 void
4728 adjust_clone_args (tree decl)
4730 tree clone;
4732 for (clone = DECL_CHAIN (decl); clone && DECL_CLONED_FUNCTION_P (clone);
4733 clone = DECL_CHAIN (clone))
4735 tree orig_clone_parms = TYPE_ARG_TYPES (TREE_TYPE (clone));
4736 tree orig_decl_parms = TYPE_ARG_TYPES (TREE_TYPE (decl));
4737 tree decl_parms, clone_parms;
4739 clone_parms = orig_clone_parms;
4741 /* Skip the 'this' parameter. */
4742 orig_clone_parms = TREE_CHAIN (orig_clone_parms);
4743 orig_decl_parms = TREE_CHAIN (orig_decl_parms);
4745 if (DECL_HAS_IN_CHARGE_PARM_P (decl))
4746 orig_decl_parms = TREE_CHAIN (orig_decl_parms);
4747 if (DECL_HAS_VTT_PARM_P (decl))
4748 orig_decl_parms = TREE_CHAIN (orig_decl_parms);
4750 clone_parms = orig_clone_parms;
4751 if (DECL_HAS_VTT_PARM_P (clone))
4752 clone_parms = TREE_CHAIN (clone_parms);
4754 for (decl_parms = orig_decl_parms; decl_parms;
4755 decl_parms = TREE_CHAIN (decl_parms),
4756 clone_parms = TREE_CHAIN (clone_parms))
4758 if (clone_parms == void_list_node)
4760 gcc_assert (decl_parms == clone_parms
4761 || ctor_omit_inherited_parms (clone));
4762 break;
4765 gcc_assert (same_type_p (TREE_TYPE (decl_parms),
4766 TREE_TYPE (clone_parms)));
4768 if (TREE_PURPOSE (decl_parms) && !TREE_PURPOSE (clone_parms))
4770 /* A default parameter has been added. Adjust the
4771 clone's parameters. */
4772 tree exceptions = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (clone));
4773 tree attrs = TYPE_ATTRIBUTES (TREE_TYPE (clone));
4774 tree basetype = TYPE_METHOD_BASETYPE (TREE_TYPE (clone));
4775 tree type;
4777 clone_parms = orig_decl_parms;
4779 if (DECL_HAS_VTT_PARM_P (clone))
4781 clone_parms = tree_cons (TREE_PURPOSE (orig_clone_parms),
4782 TREE_VALUE (orig_clone_parms),
4783 clone_parms);
4784 TREE_TYPE (clone_parms) = TREE_TYPE (orig_clone_parms);
4786 type = build_method_type_directly (basetype,
4787 TREE_TYPE (TREE_TYPE (clone)),
4788 clone_parms);
4789 if (exceptions)
4790 type = build_exception_variant (type, exceptions);
4791 if (attrs)
4792 type = cp_build_type_attribute_variant (type, attrs);
4793 TREE_TYPE (clone) = type;
4795 clone_parms = NULL_TREE;
4796 break;
4799 gcc_assert (!clone_parms || clone_parms == void_list_node);
4803 /* For each of the constructors and destructors in T, create an
4804 in-charge and not-in-charge variant. */
4806 static void
4807 clone_constructors_and_destructors (tree t)
4809 /* While constructors can be via a using declaration, at this point
4810 we no longer need to know that. */
4811 for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (t)); iter; ++iter)
4812 clone_function_decl (*iter, /*update_methods=*/true);
4814 if (tree dtor = CLASSTYPE_DESTRUCTOR (t))
4815 clone_function_decl (dtor, /*update_methods=*/true);
4818 /* Deduce noexcept for a destructor DTOR. */
4820 void
4821 deduce_noexcept_on_destructor (tree dtor)
4823 if (!TYPE_RAISES_EXCEPTIONS (TREE_TYPE (dtor)))
4824 TREE_TYPE (dtor) = build_exception_variant (TREE_TYPE (dtor),
4825 noexcept_deferred_spec);
4828 /* Subroutine of set_one_vmethod_tm_attributes. Search base classes
4829 of TYPE for virtual functions which FNDECL overrides. Return a
4830 mask of the tm attributes found therein. */
4832 static int
4833 look_for_tm_attr_overrides (tree type, tree fndecl)
4835 tree binfo = TYPE_BINFO (type);
4836 tree base_binfo;
4837 int ix, found = 0;
4839 for (ix = 0; BINFO_BASE_ITERATE (binfo, ix, base_binfo); ++ix)
4841 tree o, basetype = BINFO_TYPE (base_binfo);
4843 if (!TYPE_POLYMORPHIC_P (basetype))
4844 continue;
4846 o = look_for_overrides_here (basetype, fndecl);
4847 if (o)
4849 if (lookup_attribute ("transaction_safe_dynamic",
4850 DECL_ATTRIBUTES (o)))
4851 /* transaction_safe_dynamic is not inherited. */;
4852 else
4853 found |= tm_attr_to_mask (find_tm_attribute
4854 (TYPE_ATTRIBUTES (TREE_TYPE (o))));
4856 else
4857 found |= look_for_tm_attr_overrides (basetype, fndecl);
4860 return found;
4863 /* Subroutine of set_method_tm_attributes. Handle the checks and
4864 inheritance for one virtual method FNDECL. */
4866 static void
4867 set_one_vmethod_tm_attributes (tree type, tree fndecl)
4869 tree tm_attr;
4870 int found, have;
4872 found = look_for_tm_attr_overrides (type, fndecl);
4874 /* If FNDECL doesn't actually override anything (i.e. T is the
4875 class that first declares FNDECL virtual), then we're done. */
4876 if (found == 0)
4877 return;
4879 tm_attr = find_tm_attribute (TYPE_ATTRIBUTES (TREE_TYPE (fndecl)));
4880 have = tm_attr_to_mask (tm_attr);
4882 /* Intel STM Language Extension 3.0, Section 4.2 table 4:
4883 tm_pure must match exactly, otherwise no weakening of
4884 tm_safe > tm_callable > nothing. */
4885 /* ??? The tm_pure attribute didn't make the transition to the
4886 multivendor language spec. */
4887 if (have == TM_ATTR_PURE)
4889 if (found != TM_ATTR_PURE)
4891 found &= -found;
4892 goto err_override;
4895 /* If the overridden function is tm_pure, then FNDECL must be. */
4896 else if (found == TM_ATTR_PURE && tm_attr)
4897 goto err_override;
4898 /* Look for base class combinations that cannot be satisfied. */
4899 else if (found != TM_ATTR_PURE && (found & TM_ATTR_PURE))
4901 found &= ~TM_ATTR_PURE;
4902 found &= -found;
4903 error_at (DECL_SOURCE_LOCATION (fndecl),
4904 "method overrides both %<transaction_pure%> and %qE methods",
4905 tm_mask_to_attr (found));
4907 /* If FNDECL did not declare an attribute, then inherit the most
4908 restrictive one. */
4909 else if (tm_attr == NULL)
4911 apply_tm_attr (fndecl, tm_mask_to_attr (least_bit_hwi (found)));
4913 /* Otherwise validate that we're not weaker than a function
4914 that is being overridden. */
4915 else
4917 found &= -found;
4918 if (found <= TM_ATTR_CALLABLE && have > found)
4919 goto err_override;
4921 return;
4923 err_override:
4924 error_at (DECL_SOURCE_LOCATION (fndecl),
4925 "method declared %qE overriding %qE method",
4926 tm_attr, tm_mask_to_attr (found));
4929 /* For each of the methods in T, propagate a class-level tm attribute. */
4931 static void
4932 set_method_tm_attributes (tree t)
4934 tree class_tm_attr, fndecl;
4936 /* Don't bother collecting tm attributes if transactional memory
4937 support is not enabled. */
4938 if (!flag_tm)
4939 return;
4941 /* Process virtual methods first, as they inherit directly from the
4942 base virtual function and also require validation of new attributes. */
4943 if (TYPE_CONTAINS_VPTR_P (t))
4945 tree vchain;
4946 for (vchain = BINFO_VIRTUALS (TYPE_BINFO (t)); vchain;
4947 vchain = TREE_CHAIN (vchain))
4949 fndecl = BV_FN (vchain);
4950 if (DECL_THUNK_P (fndecl))
4951 fndecl = THUNK_TARGET (fndecl);
4952 set_one_vmethod_tm_attributes (t, fndecl);
4956 /* If the class doesn't have an attribute, nothing more to do. */
4957 class_tm_attr = find_tm_attribute (TYPE_ATTRIBUTES (t));
4958 if (class_tm_attr == NULL)
4959 return;
4961 /* Any method that does not yet have a tm attribute inherits
4962 the one from the class. */
4963 for (fndecl = TYPE_FIELDS (t); fndecl; fndecl = DECL_CHAIN (fndecl))
4964 if (DECL_DECLARES_FUNCTION_P (fndecl)
4965 && !find_tm_attribute (TYPE_ATTRIBUTES (TREE_TYPE (fndecl))))
4966 apply_tm_attr (fndecl, class_tm_attr);
4969 /* Returns true if FN is a default constructor. */
4971 bool
4972 default_ctor_p (tree fn)
4974 return (DECL_CONSTRUCTOR_P (fn)
4975 && sufficient_parms_p (FUNCTION_FIRST_USER_PARMTYPE (fn)));
4978 /* Returns true iff class T has a user-defined constructor that can be called
4979 with more than zero arguments. */
4981 bool
4982 type_has_user_nondefault_constructor (tree t)
4984 if (!TYPE_HAS_USER_CONSTRUCTOR (t))
4985 return false;
4987 for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (t)); iter; ++iter)
4989 tree fn = *iter;
4990 if (!DECL_ARTIFICIAL (fn)
4991 && (TREE_CODE (fn) == TEMPLATE_DECL
4992 || (skip_artificial_parms_for (fn, DECL_ARGUMENTS (fn))
4993 != NULL_TREE)))
4994 return true;
4997 return false;
5000 /* Returns the defaulted constructor if T has one. Otherwise, returns
5001 NULL_TREE. */
5003 tree
5004 in_class_defaulted_default_constructor (tree t)
5006 if (!TYPE_HAS_USER_CONSTRUCTOR (t))
5007 return NULL_TREE;
5009 for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (t)); iter; ++iter)
5011 tree fn = *iter;
5013 if (DECL_DEFAULTED_IN_CLASS_P (fn)
5014 && default_ctor_p (fn))
5015 return fn;
5018 return NULL_TREE;
5021 /* Returns true iff FN is a user-provided function, i.e. user-declared
5022 and not defaulted at its first declaration. */
5024 bool
5025 user_provided_p (tree fn)
5027 if (TREE_CODE (fn) == TEMPLATE_DECL)
5028 return true;
5029 else
5030 return (!DECL_ARTIFICIAL (fn)
5031 && !(DECL_INITIALIZED_IN_CLASS_P (fn)
5032 && (DECL_DEFAULTED_FN (fn) || DECL_DELETED_FN (fn))));
5035 /* Returns true iff class T has a user-provided constructor. */
5037 bool
5038 type_has_user_provided_constructor (tree t)
5040 if (!CLASS_TYPE_P (t))
5041 return false;
5043 if (!TYPE_HAS_USER_CONSTRUCTOR (t))
5044 return false;
5046 for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (t)); iter; ++iter)
5047 if (user_provided_p (*iter))
5048 return true;
5050 return false;
5053 /* Returns true iff class T has a user-provided or explicit constructor. */
5055 bool
5056 type_has_user_provided_or_explicit_constructor (tree t)
5058 if (!CLASS_TYPE_P (t))
5059 return false;
5061 if (!TYPE_HAS_USER_CONSTRUCTOR (t))
5062 return false;
5064 for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (t)); iter; ++iter)
5066 tree fn = *iter;
5067 if (user_provided_p (fn) || DECL_NONCONVERTING_P (fn))
5068 return true;
5071 return false;
5074 /* Returns true iff class T has a non-user-provided (i.e. implicitly
5075 declared or explicitly defaulted in the class body) default
5076 constructor. */
5078 bool
5079 type_has_non_user_provided_default_constructor (tree t)
5081 if (!TYPE_HAS_DEFAULT_CONSTRUCTOR (t))
5082 return false;
5083 if (CLASSTYPE_LAZY_DEFAULT_CTOR (t))
5084 return true;
5086 for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (t)); iter; ++iter)
5088 tree fn = *iter;
5089 if (TREE_CODE (fn) == FUNCTION_DECL
5090 && default_ctor_p (fn)
5091 && !user_provided_p (fn))
5092 return true;
5095 return false;
5098 /* TYPE is being used as a virtual base, and has a non-trivial move
5099 assignment. Return true if this is due to there being a user-provided
5100 move assignment in TYPE or one of its subobjects; if there isn't, then
5101 multiple move assignment can't cause any harm. */
5103 bool
5104 vbase_has_user_provided_move_assign (tree type)
5106 /* Does the type itself have a user-provided move assignment operator? */
5107 for (ovl_iterator iter (lookup_fnfields_slot_nolazy
5108 (type, cp_assignment_operator_id (NOP_EXPR)));
5109 iter; ++iter)
5111 tree fn = *iter;
5112 if (move_fn_p (fn) && user_provided_p (fn))
5113 return true;
5116 /* Do any of its bases? */
5117 tree binfo = TYPE_BINFO (type);
5118 tree base_binfo;
5119 for (int i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
5120 if (vbase_has_user_provided_move_assign (BINFO_TYPE (base_binfo)))
5121 return true;
5123 /* Or non-static data members? */
5124 for (tree field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
5126 if (TREE_CODE (field) == FIELD_DECL
5127 && CLASS_TYPE_P (TREE_TYPE (field))
5128 && vbase_has_user_provided_move_assign (TREE_TYPE (field)))
5129 return true;
5132 /* Seems not. */
5133 return false;
5136 /* If default-initialization leaves part of TYPE uninitialized, returns
5137 a DECL for the field or TYPE itself (DR 253). */
5139 tree
5140 default_init_uninitialized_part (tree type)
5142 tree t, r, binfo;
5143 int i;
5145 type = strip_array_types (type);
5146 if (!CLASS_TYPE_P (type))
5147 return type;
5148 if (!type_has_non_user_provided_default_constructor (type))
5149 return NULL_TREE;
5150 for (binfo = TYPE_BINFO (type), i = 0;
5151 BINFO_BASE_ITERATE (binfo, i, t); ++i)
5153 r = default_init_uninitialized_part (BINFO_TYPE (t));
5154 if (r)
5155 return r;
5157 for (t = TYPE_FIELDS (type); t; t = DECL_CHAIN (t))
5158 if (TREE_CODE (t) == FIELD_DECL
5159 && !DECL_ARTIFICIAL (t)
5160 && !DECL_INITIAL (t))
5162 r = default_init_uninitialized_part (TREE_TYPE (t));
5163 if (r)
5164 return DECL_P (r) ? r : t;
5167 return NULL_TREE;
5170 /* Returns true iff for class T, a trivial synthesized default constructor
5171 would be constexpr. */
5173 bool
5174 trivial_default_constructor_is_constexpr (tree t)
5176 /* A defaulted trivial default constructor is constexpr
5177 if there is nothing to initialize. */
5178 gcc_assert (!TYPE_HAS_COMPLEX_DFLT (t));
5179 return is_really_empty_class (t);
5182 /* Returns true iff class T has a constexpr default constructor. */
5184 bool
5185 type_has_constexpr_default_constructor (tree t)
5187 tree fns;
5189 if (!CLASS_TYPE_P (t))
5191 /* The caller should have stripped an enclosing array. */
5192 gcc_assert (TREE_CODE (t) != ARRAY_TYPE);
5193 return false;
5195 if (CLASSTYPE_LAZY_DEFAULT_CTOR (t))
5197 if (!TYPE_HAS_COMPLEX_DFLT (t))
5198 return trivial_default_constructor_is_constexpr (t);
5199 /* Non-trivial, we need to check subobject constructors. */
5200 lazily_declare_fn (sfk_constructor, t);
5202 fns = locate_ctor (t);
5203 return (fns && DECL_DECLARED_CONSTEXPR_P (fns));
5206 /* Returns true iff class T has a constexpr default constructor or has an
5207 implicitly declared default constructor that we can't tell if it's constexpr
5208 without forcing a lazy declaration (which might cause undesired
5209 instantiations). */
5211 bool
5212 type_maybe_constexpr_default_constructor (tree t)
5214 if (CLASS_TYPE_P (t) && CLASSTYPE_LAZY_DEFAULT_CTOR (t)
5215 && TYPE_HAS_COMPLEX_DFLT (t))
5216 /* Assume it's constexpr. */
5217 return true;
5218 return type_has_constexpr_default_constructor (t);
5221 /* Returns true iff class TYPE has a virtual destructor. */
5223 bool
5224 type_has_virtual_destructor (tree type)
5226 tree dtor;
5228 if (!CLASS_TYPE_P (type))
5229 return false;
5231 gcc_assert (COMPLETE_TYPE_P (type));
5232 dtor = CLASSTYPE_DESTRUCTOR (type);
5233 return (dtor && DECL_VIRTUAL_P (dtor));
5236 /* Returns true iff T, a class, has a move-assignment or
5237 move-constructor. Does not lazily declare either.
5238 If USER_P is false, any move function will do. If it is true, the
5239 move function must be user-declared.
5241 Note that user-declared here is different from "user-provided",
5242 which doesn't include functions that are defaulted in the
5243 class. */
5245 bool
5246 classtype_has_move_assign_or_move_ctor_p (tree t, bool user_p)
5248 gcc_assert (user_p
5249 || (!CLASSTYPE_LAZY_MOVE_CTOR (t)
5250 && !CLASSTYPE_LAZY_MOVE_ASSIGN (t)));
5252 if (!CLASSTYPE_LAZY_MOVE_CTOR (t))
5253 for (ovl_iterator iter (lookup_fnfields_slot_nolazy (t, ctor_identifier));
5254 iter; ++iter)
5255 if ((!user_p || !DECL_ARTIFICIAL (*iter)) && move_fn_p (*iter))
5256 return true;
5258 if (!CLASSTYPE_LAZY_MOVE_ASSIGN (t))
5259 for (ovl_iterator iter (lookup_fnfields_slot_nolazy
5260 (t, cp_assignment_operator_id (NOP_EXPR)));
5261 iter; ++iter)
5262 if ((!user_p || !DECL_ARTIFICIAL (*iter)) && move_fn_p (*iter))
5263 return true;
5265 return false;
5268 /* Nonzero if we need to build up a constructor call when initializing an
5269 object of this class, either because it has a user-declared constructor
5270 or because it doesn't have a default constructor (so we need to give an
5271 error if no initializer is provided). Use TYPE_NEEDS_CONSTRUCTING when
5272 what you care about is whether or not an object can be produced by a
5273 constructor (e.g. so we don't set TREE_READONLY on const variables of
5274 such type); use this function when what you care about is whether or not
5275 to try to call a constructor to create an object. The latter case is
5276 the former plus some cases of constructors that cannot be called. */
5278 bool
5279 type_build_ctor_call (tree t)
5281 tree inner;
5282 if (TYPE_NEEDS_CONSTRUCTING (t))
5283 return true;
5284 inner = strip_array_types (t);
5285 if (!CLASS_TYPE_P (inner) || ANON_AGGR_TYPE_P (inner))
5286 return false;
5287 if (!TYPE_HAS_DEFAULT_CONSTRUCTOR (inner))
5288 return true;
5289 if (cxx_dialect < cxx11)
5290 return false;
5291 /* A user-declared constructor might be private, and a constructor might
5292 be trivial but deleted. */
5293 for (ovl_iterator iter
5294 (lookup_fnfields_slot (inner, complete_ctor_identifier));
5295 iter; ++iter)
5297 tree fn = *iter;
5298 if (!DECL_ARTIFICIAL (fn)
5299 || DECL_DELETED_FN (fn))
5300 return true;
5302 return false;
5305 /* Like type_build_ctor_call, but for destructors. */
5307 bool
5308 type_build_dtor_call (tree t)
5310 tree inner;
5311 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t))
5312 return true;
5313 inner = strip_array_types (t);
5314 if (!CLASS_TYPE_P (inner) || ANON_AGGR_TYPE_P (inner)
5315 || !COMPLETE_TYPE_P (inner))
5316 return false;
5317 if (cxx_dialect < cxx11)
5318 return false;
5319 /* A user-declared destructor might be private, and a destructor might
5320 be trivial but deleted. */
5321 for (ovl_iterator iter
5322 (lookup_fnfields_slot (inner, complete_dtor_identifier));
5323 iter; ++iter)
5325 tree fn = *iter;
5326 if (!DECL_ARTIFICIAL (fn)
5327 || DECL_DELETED_FN (fn))
5328 return true;
5330 return false;
5333 /* Remove all zero-width bit-fields from T. */
5335 static void
5336 remove_zero_width_bit_fields (tree t)
5338 tree *fieldsp;
5340 fieldsp = &TYPE_FIELDS (t);
5341 while (*fieldsp)
5343 if (TREE_CODE (*fieldsp) == FIELD_DECL
5344 && DECL_C_BIT_FIELD (*fieldsp)
5345 /* We should not be confused by the fact that grokbitfield
5346 temporarily sets the width of the bit field into
5347 DECL_INITIAL (*fieldsp).
5348 check_bitfield_decl eventually sets DECL_SIZE (*fieldsp)
5349 to that width. */
5350 && (DECL_SIZE (*fieldsp) == NULL_TREE
5351 || integer_zerop (DECL_SIZE (*fieldsp))))
5352 *fieldsp = DECL_CHAIN (*fieldsp);
5353 else
5354 fieldsp = &DECL_CHAIN (*fieldsp);
5358 /* Returns TRUE iff we need a cookie when dynamically allocating an
5359 array whose elements have the indicated class TYPE. */
5361 static bool
5362 type_requires_array_cookie (tree type)
5364 tree fns;
5365 bool has_two_argument_delete_p = false;
5367 gcc_assert (CLASS_TYPE_P (type));
5369 /* If there's a non-trivial destructor, we need a cookie. In order
5370 to iterate through the array calling the destructor for each
5371 element, we'll have to know how many elements there are. */
5372 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
5373 return true;
5375 /* If the usual deallocation function is a two-argument whose second
5376 argument is of type `size_t', then we have to pass the size of
5377 the array to the deallocation function, so we will need to store
5378 a cookie. */
5379 fns = lookup_fnfields (TYPE_BINFO (type),
5380 cp_operator_id (VEC_DELETE_EXPR),
5381 /*protect=*/0);
5382 /* If there are no `operator []' members, or the lookup is
5383 ambiguous, then we don't need a cookie. */
5384 if (!fns || fns == error_mark_node)
5385 return false;
5386 /* Loop through all of the functions. */
5387 for (lkp_iterator iter (BASELINK_FUNCTIONS (fns)); iter; ++iter)
5389 tree fn = *iter;
5391 /* See if this function is a one-argument delete function. If
5392 it is, then it will be the usual deallocation function. */
5393 tree second_parm = TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (fn)));
5394 if (second_parm == void_list_node)
5395 return false;
5396 /* Do not consider this function if its second argument is an
5397 ellipsis. */
5398 if (!second_parm)
5399 continue;
5400 /* Otherwise, if we have a two-argument function and the second
5401 argument is `size_t', it will be the usual deallocation
5402 function -- unless there is one-argument function, too. */
5403 if (TREE_CHAIN (second_parm) == void_list_node
5404 && same_type_p (TREE_VALUE (second_parm), size_type_node))
5405 has_two_argument_delete_p = true;
5408 return has_two_argument_delete_p;
5411 /* Finish computing the `literal type' property of class type T.
5413 At this point, we have already processed base classes and
5414 non-static data members. We need to check whether the copy
5415 constructor is trivial, the destructor is trivial, and there
5416 is a trivial default constructor or at least one constexpr
5417 constructor other than the copy constructor. */
5419 static void
5420 finalize_literal_type_property (tree t)
5422 tree fn;
5424 if (cxx_dialect < cxx11
5425 || TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t))
5426 CLASSTYPE_LITERAL_P (t) = false;
5427 else if (CLASSTYPE_LITERAL_P (t) && LAMBDA_TYPE_P (t))
5428 CLASSTYPE_LITERAL_P (t) = (cxx_dialect >= cxx1z);
5429 else if (CLASSTYPE_LITERAL_P (t) && !TYPE_HAS_TRIVIAL_DFLT (t)
5430 && CLASSTYPE_NON_AGGREGATE (t)
5431 && !TYPE_HAS_CONSTEXPR_CTOR (t))
5432 CLASSTYPE_LITERAL_P (t) = false;
5434 /* C++14 DR 1684 removed this restriction. */
5435 if (cxx_dialect < cxx14
5436 && !CLASSTYPE_LITERAL_P (t) && !LAMBDA_TYPE_P (t))
5437 for (fn = TYPE_FIELDS (t); fn; fn = DECL_CHAIN (fn))
5438 if (TREE_CODE (fn) == FUNCTION_DECL
5439 && DECL_DECLARED_CONSTEXPR_P (fn)
5440 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
5441 && !DECL_CONSTRUCTOR_P (fn))
5443 DECL_DECLARED_CONSTEXPR_P (fn) = false;
5444 if (!DECL_GENERATED_P (fn)
5445 && pedwarn (DECL_SOURCE_LOCATION (fn), OPT_Wpedantic,
5446 "enclosing class of constexpr non-static member "
5447 "function %q+#D is not a literal type", fn))
5448 explain_non_literal_class (t);
5452 /* T is a non-literal type used in a context which requires a constant
5453 expression. Explain why it isn't literal. */
5455 void
5456 explain_non_literal_class (tree t)
5458 static hash_set<tree> *diagnosed;
5460 if (!CLASS_TYPE_P (t))
5461 return;
5462 t = TYPE_MAIN_VARIANT (t);
5464 if (diagnosed == NULL)
5465 diagnosed = new hash_set<tree>;
5466 if (diagnosed->add (t))
5467 /* Already explained. */
5468 return;
5470 inform (0, "%q+T is not literal because:", t);
5471 if (cxx_dialect < cxx1z && LAMBDA_TYPE_P (t))
5472 inform (0, " %qT is a closure type, which is only literal in "
5473 "C++1z and later", t);
5474 else if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t))
5475 inform (0, " %q+T has a non-trivial destructor", t);
5476 else if (CLASSTYPE_NON_AGGREGATE (t)
5477 && !TYPE_HAS_TRIVIAL_DFLT (t)
5478 && !LAMBDA_TYPE_P (t)
5479 && !TYPE_HAS_CONSTEXPR_CTOR (t))
5481 inform (0, " %q+T is not an aggregate, does not have a trivial "
5482 "default constructor, and has no constexpr constructor that "
5483 "is not a copy or move constructor", t);
5484 if (type_has_non_user_provided_default_constructor (t))
5485 /* Note that we can't simply call locate_ctor because when the
5486 constructor is deleted it just returns NULL_TREE. */
5487 for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (t)); iter; ++iter)
5489 tree fn = *iter;
5490 tree parms = TYPE_ARG_TYPES (TREE_TYPE (fn));
5492 parms = skip_artificial_parms_for (fn, parms);
5494 if (sufficient_parms_p (parms))
5496 if (DECL_DELETED_FN (fn))
5497 maybe_explain_implicit_delete (fn);
5498 else
5499 explain_invalid_constexpr_fn (fn);
5500 break;
5504 else
5506 tree binfo, base_binfo, field; int i;
5507 for (binfo = TYPE_BINFO (t), i = 0;
5508 BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
5510 tree basetype = TREE_TYPE (base_binfo);
5511 if (!CLASSTYPE_LITERAL_P (basetype))
5513 inform (0, " base class %qT of %q+T is non-literal",
5514 basetype, t);
5515 explain_non_literal_class (basetype);
5516 return;
5519 for (field = TYPE_FIELDS (t); field; field = TREE_CHAIN (field))
5521 tree ftype;
5522 if (TREE_CODE (field) != FIELD_DECL)
5523 continue;
5524 ftype = TREE_TYPE (field);
5525 if (!literal_type_p (ftype))
5527 inform (DECL_SOURCE_LOCATION (field),
5528 " non-static data member %qD has non-literal type",
5529 field);
5530 if (CLASS_TYPE_P (ftype))
5531 explain_non_literal_class (ftype);
5533 if (CP_TYPE_VOLATILE_P (ftype))
5534 inform (DECL_SOURCE_LOCATION (field),
5535 " non-static data member %qD has volatile type", field);
5540 /* Check the validity of the bases and members declared in T. Add any
5541 implicitly-generated functions (like copy-constructors and
5542 assignment operators). Compute various flag bits (like
5543 CLASSTYPE_NON_LAYOUT_POD_T) for T. This routine works purely at the C++
5544 level: i.e., independently of the ABI in use. */
5546 static void
5547 check_bases_and_members (tree t)
5549 /* Nonzero if the implicitly generated copy constructor should take
5550 a non-const reference argument. */
5551 int cant_have_const_ctor;
5552 /* Nonzero if the implicitly generated assignment operator
5553 should take a non-const reference argument. */
5554 int no_const_asn_ref;
5555 tree access_decls;
5556 bool saved_complex_asn_ref;
5557 bool saved_nontrivial_dtor;
5558 tree fn;
5560 /* By default, we use const reference arguments and generate default
5561 constructors. */
5562 cant_have_const_ctor = 0;
5563 no_const_asn_ref = 0;
5565 /* Check all the base-classes and set FMEM members to point to arrays
5566 of potential interest. */
5567 check_bases (t, &cant_have_const_ctor, &no_const_asn_ref);
5569 /* Deduce noexcept on destructor. This needs to happen after we've set
5570 triviality flags appropriately for our bases. */
5571 if (cxx_dialect >= cxx11)
5572 if (tree dtor = CLASSTYPE_DESTRUCTOR (t))
5573 deduce_noexcept_on_destructor (dtor);
5575 /* Check all the method declarations. */
5576 check_methods (t);
5578 /* Save the initial values of these flags which only indicate whether
5579 or not the class has user-provided functions. As we analyze the
5580 bases and members we can set these flags for other reasons. */
5581 saved_complex_asn_ref = TYPE_HAS_COMPLEX_COPY_ASSIGN (t);
5582 saved_nontrivial_dtor = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t);
5584 /* Check all the data member declarations. We cannot call
5585 check_field_decls until we have called check_bases check_methods,
5586 as check_field_decls depends on TYPE_HAS_NONTRIVIAL_DESTRUCTOR
5587 being set appropriately. */
5588 check_field_decls (t, &access_decls,
5589 &cant_have_const_ctor,
5590 &no_const_asn_ref);
5592 /* A nearly-empty class has to be vptr-containing; a nearly empty
5593 class contains just a vptr. */
5594 if (!TYPE_CONTAINS_VPTR_P (t))
5595 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
5597 /* Do some bookkeeping that will guide the generation of implicitly
5598 declared member functions. */
5599 TYPE_HAS_COMPLEX_COPY_CTOR (t) |= TYPE_CONTAINS_VPTR_P (t);
5600 TYPE_HAS_COMPLEX_MOVE_CTOR (t) |= TYPE_CONTAINS_VPTR_P (t);
5601 /* We need to call a constructor for this class if it has a
5602 user-provided constructor, or if the default constructor is going
5603 to initialize the vptr. (This is not an if-and-only-if;
5604 TYPE_NEEDS_CONSTRUCTING is set elsewhere if bases or members
5605 themselves need constructing.) */
5606 TYPE_NEEDS_CONSTRUCTING (t)
5607 |= (type_has_user_provided_constructor (t) || TYPE_CONTAINS_VPTR_P (t));
5608 /* [dcl.init.aggr]
5610 An aggregate is an array or a class with no user-provided
5611 constructors ... and no virtual functions.
5613 Again, other conditions for being an aggregate are checked
5614 elsewhere. */
5615 CLASSTYPE_NON_AGGREGATE (t)
5616 |= (type_has_user_provided_or_explicit_constructor (t)
5617 || TYPE_POLYMORPHIC_P (t));
5618 /* This is the C++98/03 definition of POD; it changed in C++0x, but we
5619 retain the old definition internally for ABI reasons. */
5620 CLASSTYPE_NON_LAYOUT_POD_P (t)
5621 |= (CLASSTYPE_NON_AGGREGATE (t)
5622 || saved_nontrivial_dtor || saved_complex_asn_ref);
5623 CLASSTYPE_NON_STD_LAYOUT (t) |= TYPE_CONTAINS_VPTR_P (t);
5624 TYPE_HAS_COMPLEX_COPY_ASSIGN (t) |= TYPE_CONTAINS_VPTR_P (t);
5625 TYPE_HAS_COMPLEX_MOVE_ASSIGN (t) |= TYPE_CONTAINS_VPTR_P (t);
5626 TYPE_HAS_COMPLEX_DFLT (t) |= TYPE_CONTAINS_VPTR_P (t);
5628 /* If the only explicitly declared default constructor is user-provided,
5629 set TYPE_HAS_COMPLEX_DFLT. */
5630 if (!TYPE_HAS_COMPLEX_DFLT (t)
5631 && TYPE_HAS_DEFAULT_CONSTRUCTOR (t)
5632 && !type_has_non_user_provided_default_constructor (t))
5633 TYPE_HAS_COMPLEX_DFLT (t) = true;
5635 /* Warn if a public base of a polymorphic type has an accessible
5636 non-virtual destructor. It is only now that we know the class is
5637 polymorphic. Although a polymorphic base will have a already
5638 been diagnosed during its definition, we warn on use too. */
5639 if (TYPE_POLYMORPHIC_P (t) && warn_nonvdtor)
5641 tree binfo = TYPE_BINFO (t);
5642 vec<tree, va_gc> *accesses = BINFO_BASE_ACCESSES (binfo);
5643 tree base_binfo;
5644 unsigned i;
5646 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
5648 tree basetype = TREE_TYPE (base_binfo);
5650 if ((*accesses)[i] == access_public_node
5651 && (TYPE_POLYMORPHIC_P (basetype) || warn_ecpp)
5652 && accessible_nvdtor_p (basetype))
5653 warning (OPT_Wnon_virtual_dtor,
5654 "base class %q#T has accessible non-virtual destructor",
5655 basetype);
5659 /* If the class has no user-declared constructor, but does have
5660 non-static const or reference data members that can never be
5661 initialized, issue a warning. */
5662 if (warn_uninitialized
5663 /* Classes with user-declared constructors are presumed to
5664 initialize these members. */
5665 && !TYPE_HAS_USER_CONSTRUCTOR (t)
5666 /* Aggregates can be initialized with brace-enclosed
5667 initializers. */
5668 && CLASSTYPE_NON_AGGREGATE (t))
5670 tree field;
5672 for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
5674 tree type;
5676 if (TREE_CODE (field) != FIELD_DECL
5677 || DECL_INITIAL (field) != NULL_TREE)
5678 continue;
5680 type = TREE_TYPE (field);
5681 if (TREE_CODE (type) == REFERENCE_TYPE)
5682 warning_at (DECL_SOURCE_LOCATION (field),
5683 OPT_Wuninitialized, "non-static reference %q#D "
5684 "in class without a constructor", field);
5685 else if (CP_TYPE_CONST_P (type)
5686 && (!CLASS_TYPE_P (type)
5687 || !TYPE_HAS_DEFAULT_CONSTRUCTOR (type)))
5688 warning_at (DECL_SOURCE_LOCATION (field),
5689 OPT_Wuninitialized, "non-static const member %q#D "
5690 "in class without a constructor", field);
5694 /* Synthesize any needed methods. */
5695 add_implicitly_declared_members (t, &access_decls,
5696 cant_have_const_ctor,
5697 no_const_asn_ref);
5699 /* Check defaulted declarations here so we have cant_have_const_ctor
5700 and don't need to worry about clones. */
5701 for (fn = TYPE_FIELDS (t); fn; fn = DECL_CHAIN (fn))
5702 if (DECL_DECLARES_FUNCTION_P (fn)
5703 && !DECL_ARTIFICIAL (fn)
5704 && DECL_DEFAULTED_IN_CLASS_P (fn))
5706 int copy = copy_fn_p (fn);
5707 if (copy > 0)
5709 bool imp_const_p
5710 = (DECL_CONSTRUCTOR_P (fn) ? !cant_have_const_ctor
5711 : !no_const_asn_ref);
5712 bool fn_const_p = (copy == 2);
5714 if (fn_const_p && !imp_const_p)
5715 /* If the function is defaulted outside the class, we just
5716 give the synthesis error. */
5717 error ("%q+D declared to take const reference, but implicit "
5718 "declaration would take non-const", fn);
5720 defaulted_late_check (fn);
5723 if (LAMBDA_TYPE_P (t))
5725 /* "This class type is not an aggregate." */
5726 CLASSTYPE_NON_AGGREGATE (t) = 1;
5729 /* Compute the 'literal type' property before we
5730 do anything with non-static member functions. */
5731 finalize_literal_type_property (t);
5733 /* Create the in-charge and not-in-charge variants of constructors
5734 and destructors. */
5735 clone_constructors_and_destructors (t);
5737 /* Process the using-declarations. */
5738 for (; access_decls; access_decls = TREE_CHAIN (access_decls))
5739 handle_using_decl (TREE_VALUE (access_decls), t);
5741 /* Figure out whether or not we will need a cookie when dynamically
5742 allocating an array of this type. */
5743 LANG_TYPE_CLASS_CHECK (t)->vec_new_uses_cookie
5744 = type_requires_array_cookie (t);
5747 /* If T needs a pointer to its virtual function table, set TYPE_VFIELD
5748 accordingly. If a new vfield was created (because T doesn't have a
5749 primary base class), then the newly created field is returned. It
5750 is not added to the TYPE_FIELDS list; it is the caller's
5751 responsibility to do that. Accumulate declared virtual functions
5752 on VIRTUALS_P. */
5754 static tree
5755 create_vtable_ptr (tree t, tree* virtuals_p)
5757 tree fn;
5759 /* Collect the virtual functions declared in T. */
5760 for (fn = TYPE_FIELDS (t); fn; fn = DECL_CHAIN (fn))
5761 if (TREE_CODE (fn) == FUNCTION_DECL
5762 && DECL_VINDEX (fn) && !DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (fn)
5763 && TREE_CODE (DECL_VINDEX (fn)) != INTEGER_CST)
5765 tree new_virtual = make_node (TREE_LIST);
5767 BV_FN (new_virtual) = fn;
5768 BV_DELTA (new_virtual) = integer_zero_node;
5769 BV_VCALL_INDEX (new_virtual) = NULL_TREE;
5771 TREE_CHAIN (new_virtual) = *virtuals_p;
5772 *virtuals_p = new_virtual;
5775 /* If we couldn't find an appropriate base class, create a new field
5776 here. Even if there weren't any new virtual functions, we might need a
5777 new virtual function table if we're supposed to include vptrs in
5778 all classes that need them. */
5779 if (!TYPE_VFIELD (t) && (*virtuals_p || TYPE_CONTAINS_VPTR_P (t)))
5781 /* We build this decl with vtbl_ptr_type_node, which is a
5782 `vtable_entry_type*'. It might seem more precise to use
5783 `vtable_entry_type (*)[N]' where N is the number of virtual
5784 functions. However, that would require the vtable pointer in
5785 base classes to have a different type than the vtable pointer
5786 in derived classes. We could make that happen, but that
5787 still wouldn't solve all the problems. In particular, the
5788 type-based alias analysis code would decide that assignments
5789 to the base class vtable pointer can't alias assignments to
5790 the derived class vtable pointer, since they have different
5791 types. Thus, in a derived class destructor, where the base
5792 class constructor was inlined, we could generate bad code for
5793 setting up the vtable pointer.
5795 Therefore, we use one type for all vtable pointers. We still
5796 use a type-correct type; it's just doesn't indicate the array
5797 bounds. That's better than using `void*' or some such; it's
5798 cleaner, and it let's the alias analysis code know that these
5799 stores cannot alias stores to void*! */
5800 tree field;
5802 field = build_decl (input_location,
5803 FIELD_DECL, get_vfield_name (t), vtbl_ptr_type_node);
5804 DECL_VIRTUAL_P (field) = 1;
5805 DECL_ARTIFICIAL (field) = 1;
5806 DECL_FIELD_CONTEXT (field) = t;
5807 DECL_FCONTEXT (field) = t;
5808 if (TYPE_PACKED (t))
5809 DECL_PACKED (field) = 1;
5811 TYPE_VFIELD (t) = field;
5813 /* This class is non-empty. */
5814 CLASSTYPE_EMPTY_P (t) = 0;
5816 return field;
5819 return NULL_TREE;
5822 /* Add OFFSET to all base types of BINFO which is a base in the
5823 hierarchy dominated by T.
5825 OFFSET, which is a type offset, is number of bytes. */
5827 static void
5828 propagate_binfo_offsets (tree binfo, tree offset)
5830 int i;
5831 tree primary_binfo;
5832 tree base_binfo;
5834 /* Update BINFO's offset. */
5835 BINFO_OFFSET (binfo)
5836 = fold_convert (sizetype,
5837 size_binop (PLUS_EXPR,
5838 fold_convert (ssizetype, BINFO_OFFSET (binfo)),
5839 offset));
5841 /* Find the primary base class. */
5842 primary_binfo = get_primary_binfo (binfo);
5844 if (primary_binfo && BINFO_INHERITANCE_CHAIN (primary_binfo) == binfo)
5845 propagate_binfo_offsets (primary_binfo, offset);
5847 /* Scan all of the bases, pushing the BINFO_OFFSET adjust
5848 downwards. */
5849 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
5851 /* Don't do the primary base twice. */
5852 if (base_binfo == primary_binfo)
5853 continue;
5855 if (BINFO_VIRTUAL_P (base_binfo))
5856 continue;
5858 propagate_binfo_offsets (base_binfo, offset);
5862 /* Set BINFO_OFFSET for all of the virtual bases for RLI->T. Update
5863 TYPE_ALIGN and TYPE_SIZE for T. OFFSETS gives the location of
5864 empty subobjects of T. */
5866 static void
5867 layout_virtual_bases (record_layout_info rli, splay_tree offsets)
5869 tree vbase;
5870 tree t = rli->t;
5871 tree *next_field;
5873 if (BINFO_N_BASE_BINFOS (TYPE_BINFO (t)) == 0)
5874 return;
5876 /* Find the last field. The artificial fields created for virtual
5877 bases will go after the last extant field to date. */
5878 next_field = &TYPE_FIELDS (t);
5879 while (*next_field)
5880 next_field = &DECL_CHAIN (*next_field);
5882 /* Go through the virtual bases, allocating space for each virtual
5883 base that is not already a primary base class. These are
5884 allocated in inheritance graph order. */
5885 for (vbase = TYPE_BINFO (t); vbase; vbase = TREE_CHAIN (vbase))
5887 if (!BINFO_VIRTUAL_P (vbase))
5888 continue;
5890 if (!BINFO_PRIMARY_P (vbase))
5892 /* This virtual base is not a primary base of any class in the
5893 hierarchy, so we have to add space for it. */
5894 next_field = build_base_field (rli, vbase,
5895 offsets, next_field);
5900 /* Returns the offset of the byte just past the end of the base class
5901 BINFO. */
5903 static tree
5904 end_of_base (tree binfo)
5906 tree size;
5908 if (!CLASSTYPE_AS_BASE (BINFO_TYPE (binfo)))
5909 size = TYPE_SIZE_UNIT (char_type_node);
5910 else if (is_empty_class (BINFO_TYPE (binfo)))
5911 /* An empty class has zero CLASSTYPE_SIZE_UNIT, but we need to
5912 allocate some space for it. It cannot have virtual bases, so
5913 TYPE_SIZE_UNIT is fine. */
5914 size = TYPE_SIZE_UNIT (BINFO_TYPE (binfo));
5915 else
5916 size = CLASSTYPE_SIZE_UNIT (BINFO_TYPE (binfo));
5918 return size_binop (PLUS_EXPR, BINFO_OFFSET (binfo), size);
5921 /* Returns the offset of the byte just past the end of the base class
5922 with the highest offset in T. If INCLUDE_VIRTUALS_P is zero, then
5923 only non-virtual bases are included. */
5925 static tree
5926 end_of_class (tree t, int include_virtuals_p)
5928 tree result = size_zero_node;
5929 vec<tree, va_gc> *vbases;
5930 tree binfo;
5931 tree base_binfo;
5932 tree offset;
5933 int i;
5935 for (binfo = TYPE_BINFO (t), i = 0;
5936 BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
5938 if (!include_virtuals_p
5939 && BINFO_VIRTUAL_P (base_binfo)
5940 && (!BINFO_PRIMARY_P (base_binfo)
5941 || BINFO_INHERITANCE_CHAIN (base_binfo) != TYPE_BINFO (t)))
5942 continue;
5944 offset = end_of_base (base_binfo);
5945 if (tree_int_cst_lt (result, offset))
5946 result = offset;
5949 if (include_virtuals_p)
5950 for (vbases = CLASSTYPE_VBASECLASSES (t), i = 0;
5951 vec_safe_iterate (vbases, i, &base_binfo); i++)
5953 offset = end_of_base (base_binfo);
5954 if (tree_int_cst_lt (result, offset))
5955 result = offset;
5958 return result;
5961 /* Warn about bases of T that are inaccessible because they are
5962 ambiguous. For example:
5964 struct S {};
5965 struct T : public S {};
5966 struct U : public S, public T {};
5968 Here, `(S*) new U' is not allowed because there are two `S'
5969 subobjects of U. */
5971 static void
5972 warn_about_ambiguous_bases (tree t)
5974 int i;
5975 vec<tree, va_gc> *vbases;
5976 tree basetype;
5977 tree binfo;
5978 tree base_binfo;
5980 /* If there are no repeated bases, nothing can be ambiguous. */
5981 if (!CLASSTYPE_REPEATED_BASE_P (t))
5982 return;
5984 /* Check direct bases. */
5985 for (binfo = TYPE_BINFO (t), i = 0;
5986 BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
5988 basetype = BINFO_TYPE (base_binfo);
5990 if (!uniquely_derived_from_p (basetype, t))
5991 warning (0, "direct base %qT inaccessible in %qT due to ambiguity",
5992 basetype, t);
5995 /* Check for ambiguous virtual bases. */
5996 if (extra_warnings)
5997 for (vbases = CLASSTYPE_VBASECLASSES (t), i = 0;
5998 vec_safe_iterate (vbases, i, &binfo); i++)
6000 basetype = BINFO_TYPE (binfo);
6002 if (!uniquely_derived_from_p (basetype, t))
6003 warning (OPT_Wextra, "virtual base %qT inaccessible in %qT due "
6004 "to ambiguity", basetype, t);
6008 /* Compare two INTEGER_CSTs K1 and K2. */
6010 static int
6011 splay_tree_compare_integer_csts (splay_tree_key k1, splay_tree_key k2)
6013 return tree_int_cst_compare ((tree) k1, (tree) k2);
6016 /* Increase the size indicated in RLI to account for empty classes
6017 that are "off the end" of the class. */
6019 static void
6020 include_empty_classes (record_layout_info rli)
6022 tree eoc;
6023 tree rli_size;
6025 /* It might be the case that we grew the class to allocate a
6026 zero-sized base class. That won't be reflected in RLI, yet,
6027 because we are willing to overlay multiple bases at the same
6028 offset. However, now we need to make sure that RLI is big enough
6029 to reflect the entire class. */
6030 eoc = end_of_class (rli->t, CLASSTYPE_AS_BASE (rli->t) != NULL_TREE);
6031 rli_size = rli_size_unit_so_far (rli);
6032 if (TREE_CODE (rli_size) == INTEGER_CST
6033 && tree_int_cst_lt (rli_size, eoc))
6035 /* The size should have been rounded to a whole byte. */
6036 gcc_assert (tree_int_cst_equal
6037 (rli->bitpos, round_down (rli->bitpos, BITS_PER_UNIT)));
6038 rli->bitpos
6039 = size_binop (PLUS_EXPR,
6040 rli->bitpos,
6041 size_binop (MULT_EXPR,
6042 fold_convert (bitsizetype,
6043 size_binop (MINUS_EXPR,
6044 eoc, rli_size)),
6045 bitsize_int (BITS_PER_UNIT)));
6046 normalize_rli (rli);
6050 /* Calculate the TYPE_SIZE, TYPE_ALIGN, etc for T. Calculate
6051 BINFO_OFFSETs for all of the base-classes. Position the vtable
6052 pointer. Accumulate declared virtual functions on VIRTUALS_P. */
6054 static void
6055 layout_class_type (tree t, tree *virtuals_p)
6057 tree non_static_data_members;
6058 tree field;
6059 tree vptr;
6060 record_layout_info rli;
6061 /* Maps offsets (represented as INTEGER_CSTs) to a TREE_LIST of
6062 types that appear at that offset. */
6063 splay_tree empty_base_offsets;
6064 /* True if the last field laid out was a bit-field. */
6065 bool last_field_was_bitfield = false;
6066 /* The location at which the next field should be inserted. */
6067 tree *next_field;
6068 /* T, as a base class. */
6069 tree base_t;
6071 /* Keep track of the first non-static data member. */
6072 non_static_data_members = TYPE_FIELDS (t);
6074 /* Start laying out the record. */
6075 rli = start_record_layout (t);
6077 /* Mark all the primary bases in the hierarchy. */
6078 determine_primary_bases (t);
6080 /* Create a pointer to our virtual function table. */
6081 vptr = create_vtable_ptr (t, virtuals_p);
6083 /* The vptr is always the first thing in the class. */
6084 if (vptr)
6086 DECL_CHAIN (vptr) = TYPE_FIELDS (t);
6087 TYPE_FIELDS (t) = vptr;
6088 next_field = &DECL_CHAIN (vptr);
6089 place_field (rli, vptr);
6091 else
6092 next_field = &TYPE_FIELDS (t);
6094 /* Build FIELD_DECLs for all of the non-virtual base-types. */
6095 empty_base_offsets = splay_tree_new (splay_tree_compare_integer_csts,
6096 NULL, NULL);
6097 build_base_fields (rli, empty_base_offsets, next_field);
6099 /* Layout the non-static data members. */
6100 for (field = non_static_data_members; field; field = DECL_CHAIN (field))
6102 tree type;
6103 tree padding;
6105 /* We still pass things that aren't non-static data members to
6106 the back end, in case it wants to do something with them. */
6107 if (TREE_CODE (field) != FIELD_DECL)
6109 place_field (rli, field);
6110 /* If the static data member has incomplete type, keep track
6111 of it so that it can be completed later. (The handling
6112 of pending statics in finish_record_layout is
6113 insufficient; consider:
6115 struct S1;
6116 struct S2 { static S1 s1; };
6118 At this point, finish_record_layout will be called, but
6119 S1 is still incomplete.) */
6120 if (VAR_P (field))
6122 maybe_register_incomplete_var (field);
6123 /* The visibility of static data members is determined
6124 at their point of declaration, not their point of
6125 definition. */
6126 determine_visibility (field);
6128 continue;
6131 type = TREE_TYPE (field);
6132 if (type == error_mark_node)
6133 continue;
6135 padding = NULL_TREE;
6137 /* If this field is a bit-field whose width is greater than its
6138 type, then there are some special rules for allocating
6139 it. */
6140 if (DECL_C_BIT_FIELD (field)
6141 && tree_int_cst_lt (TYPE_SIZE (type), DECL_SIZE (field)))
6143 bool was_unnamed_p = false;
6144 /* We must allocate the bits as if suitably aligned for the
6145 longest integer type that fits in this many bits. Then,
6146 we are supposed to use the left over bits as additional
6147 padding. */
6149 /* Do not pick a type bigger than MAX_FIXED_MODE_SIZE. */
6150 tree limit = size_int (MAX_FIXED_MODE_SIZE);
6151 if (tree_int_cst_lt (DECL_SIZE (field), limit))
6152 limit = DECL_SIZE (field);
6154 tree integer_type = integer_types[itk_char];
6155 for (unsigned itk = itk_char; itk != itk_none; itk++)
6156 if (tree next = integer_types[itk])
6158 if (tree_int_cst_lt (limit, TYPE_SIZE (next)))
6159 /* Too big, so our current guess is what we want. */
6160 break;
6161 /* Not bigger than limit, ok */
6162 integer_type = next;
6165 /* Figure out how much additional padding is required. */
6166 if (TREE_CODE (t) == UNION_TYPE)
6167 /* In a union, the padding field must have the full width
6168 of the bit-field; all fields start at offset zero. */
6169 padding = DECL_SIZE (field);
6170 else
6171 padding = size_binop (MINUS_EXPR, DECL_SIZE (field),
6172 TYPE_SIZE (integer_type));
6174 if (integer_zerop (padding))
6175 padding = NULL_TREE;
6177 /* An unnamed bitfield does not normally affect the
6178 alignment of the containing class on a target where
6179 PCC_BITFIELD_TYPE_MATTERS. But, the C++ ABI does not
6180 make any exceptions for unnamed bitfields when the
6181 bitfields are longer than their types. Therefore, we
6182 temporarily give the field a name. */
6183 if (PCC_BITFIELD_TYPE_MATTERS && !DECL_NAME (field))
6185 was_unnamed_p = true;
6186 DECL_NAME (field) = make_anon_name ();
6189 DECL_SIZE (field) = TYPE_SIZE (integer_type);
6190 SET_DECL_ALIGN (field, TYPE_ALIGN (integer_type));
6191 DECL_USER_ALIGN (field) = TYPE_USER_ALIGN (integer_type);
6192 layout_nonempty_base_or_field (rli, field, NULL_TREE,
6193 empty_base_offsets);
6194 if (was_unnamed_p)
6195 DECL_NAME (field) = NULL_TREE;
6196 /* Now that layout has been performed, set the size of the
6197 field to the size of its declared type; the rest of the
6198 field is effectively invisible. */
6199 DECL_SIZE (field) = TYPE_SIZE (type);
6200 /* We must also reset the DECL_MODE of the field. */
6201 SET_DECL_MODE (field, TYPE_MODE (type));
6203 else
6204 layout_nonempty_base_or_field (rli, field, NULL_TREE,
6205 empty_base_offsets);
6207 /* Remember the location of any empty classes in FIELD. */
6208 record_subobject_offsets (TREE_TYPE (field),
6209 byte_position(field),
6210 empty_base_offsets,
6211 /*is_data_member=*/true);
6213 /* If a bit-field does not immediately follow another bit-field,
6214 and yet it starts in the middle of a byte, we have failed to
6215 comply with the ABI. */
6216 if (warn_abi
6217 && DECL_C_BIT_FIELD (field)
6218 /* The TREE_NO_WARNING flag gets set by Objective-C when
6219 laying out an Objective-C class. The ObjC ABI differs
6220 from the C++ ABI, and so we do not want a warning
6221 here. */
6222 && !TREE_NO_WARNING (field)
6223 && !last_field_was_bitfield
6224 && !integer_zerop (size_binop (TRUNC_MOD_EXPR,
6225 DECL_FIELD_BIT_OFFSET (field),
6226 bitsize_unit_node)))
6227 warning_at (DECL_SOURCE_LOCATION (field), OPT_Wabi,
6228 "offset of %qD is not ABI-compliant and may "
6229 "change in a future version of GCC", field);
6231 /* The middle end uses the type of expressions to determine the
6232 possible range of expression values. In order to optimize
6233 "x.i > 7" to "false" for a 2-bit bitfield "i", the middle end
6234 must be made aware of the width of "i", via its type.
6236 Because C++ does not have integer types of arbitrary width,
6237 we must (for the purposes of the front end) convert from the
6238 type assigned here to the declared type of the bitfield
6239 whenever a bitfield expression is used as an rvalue.
6240 Similarly, when assigning a value to a bitfield, the value
6241 must be converted to the type given the bitfield here. */
6242 if (DECL_C_BIT_FIELD (field))
6244 unsigned HOST_WIDE_INT width;
6245 tree ftype = TREE_TYPE (field);
6246 width = tree_to_uhwi (DECL_SIZE (field));
6247 if (width != TYPE_PRECISION (ftype))
6249 TREE_TYPE (field)
6250 = c_build_bitfield_integer_type (width,
6251 TYPE_UNSIGNED (ftype));
6252 TREE_TYPE (field)
6253 = cp_build_qualified_type (TREE_TYPE (field),
6254 cp_type_quals (ftype));
6258 /* If we needed additional padding after this field, add it
6259 now. */
6260 if (padding)
6262 tree padding_field;
6264 padding_field = build_decl (input_location,
6265 FIELD_DECL,
6266 NULL_TREE,
6267 char_type_node);
6268 DECL_BIT_FIELD (padding_field) = 1;
6269 DECL_SIZE (padding_field) = padding;
6270 DECL_CONTEXT (padding_field) = t;
6271 DECL_ARTIFICIAL (padding_field) = 1;
6272 DECL_IGNORED_P (padding_field) = 1;
6273 layout_nonempty_base_or_field (rli, padding_field,
6274 NULL_TREE,
6275 empty_base_offsets);
6278 last_field_was_bitfield = DECL_C_BIT_FIELD (field);
6281 if (!integer_zerop (rli->bitpos))
6283 /* Make sure that we are on a byte boundary so that the size of
6284 the class without virtual bases will always be a round number
6285 of bytes. */
6286 rli->bitpos = round_up_loc (input_location, rli->bitpos, BITS_PER_UNIT);
6287 normalize_rli (rli);
6290 /* Delete all zero-width bit-fields from the list of fields. Now
6291 that the type is laid out they are no longer important. */
6292 remove_zero_width_bit_fields (t);
6294 /* Create the version of T used for virtual bases. We do not use
6295 make_class_type for this version; this is an artificial type. For
6296 a POD type, we just reuse T. */
6297 if (CLASSTYPE_NON_LAYOUT_POD_P (t) || CLASSTYPE_EMPTY_P (t))
6299 base_t = make_node (TREE_CODE (t));
6301 /* Set the size and alignment for the new type. */
6302 tree eoc;
6304 /* If the ABI version is not at least two, and the last
6305 field was a bit-field, RLI may not be on a byte
6306 boundary. In particular, rli_size_unit_so_far might
6307 indicate the last complete byte, while rli_size_so_far
6308 indicates the total number of bits used. Therefore,
6309 rli_size_so_far, rather than rli_size_unit_so_far, is
6310 used to compute TYPE_SIZE_UNIT. */
6311 eoc = end_of_class (t, /*include_virtuals_p=*/0);
6312 TYPE_SIZE_UNIT (base_t)
6313 = size_binop (MAX_EXPR,
6314 fold_convert (sizetype,
6315 size_binop (CEIL_DIV_EXPR,
6316 rli_size_so_far (rli),
6317 bitsize_int (BITS_PER_UNIT))),
6318 eoc);
6319 TYPE_SIZE (base_t)
6320 = size_binop (MAX_EXPR,
6321 rli_size_so_far (rli),
6322 size_binop (MULT_EXPR,
6323 fold_convert (bitsizetype, eoc),
6324 bitsize_int (BITS_PER_UNIT)));
6325 SET_TYPE_ALIGN (base_t, rli->record_align);
6326 TYPE_USER_ALIGN (base_t) = TYPE_USER_ALIGN (t);
6328 /* Copy the fields from T. */
6329 next_field = &TYPE_FIELDS (base_t);
6330 for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
6331 if (TREE_CODE (field) == FIELD_DECL)
6333 *next_field = copy_node (field);
6334 DECL_CONTEXT (*next_field) = base_t;
6335 next_field = &DECL_CHAIN (*next_field);
6337 *next_field = NULL_TREE;
6339 /* Record the base version of the type. */
6340 CLASSTYPE_AS_BASE (t) = base_t;
6341 TYPE_CONTEXT (base_t) = t;
6343 else
6344 CLASSTYPE_AS_BASE (t) = t;
6346 /* Every empty class contains an empty class. */
6347 if (CLASSTYPE_EMPTY_P (t))
6348 CLASSTYPE_CONTAINS_EMPTY_CLASS_P (t) = 1;
6350 /* Set the TYPE_DECL for this type to contain the right
6351 value for DECL_OFFSET, so that we can use it as part
6352 of a COMPONENT_REF for multiple inheritance. */
6353 layout_decl (TYPE_MAIN_DECL (t), 0);
6355 /* Now fix up any virtual base class types that we left lying
6356 around. We must get these done before we try to lay out the
6357 virtual function table. As a side-effect, this will remove the
6358 base subobject fields. */
6359 layout_virtual_bases (rli, empty_base_offsets);
6361 /* Make sure that empty classes are reflected in RLI at this
6362 point. */
6363 include_empty_classes (rli);
6365 /* Make sure not to create any structures with zero size. */
6366 if (integer_zerop (rli_size_unit_so_far (rli)) && CLASSTYPE_EMPTY_P (t))
6367 place_field (rli,
6368 build_decl (input_location,
6369 FIELD_DECL, NULL_TREE, char_type_node));
6371 /* If this is a non-POD, declaring it packed makes a difference to how it
6372 can be used as a field; don't let finalize_record_size undo it. */
6373 if (TYPE_PACKED (t) && !layout_pod_type_p (t))
6374 rli->packed_maybe_necessary = true;
6376 /* Let the back end lay out the type. */
6377 finish_record_layout (rli, /*free_p=*/true);
6379 if (TYPE_SIZE_UNIT (t)
6380 && TREE_CODE (TYPE_SIZE_UNIT (t)) == INTEGER_CST
6381 && !TREE_OVERFLOW (TYPE_SIZE_UNIT (t))
6382 && !valid_constant_size_p (TYPE_SIZE_UNIT (t)))
6383 error ("size of type %qT is too large (%qE bytes)", t, TYPE_SIZE_UNIT (t));
6385 /* Warn about bases that can't be talked about due to ambiguity. */
6386 warn_about_ambiguous_bases (t);
6388 /* Now that we're done with layout, give the base fields the real types. */
6389 for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
6390 if (DECL_ARTIFICIAL (field) && IS_FAKE_BASE_TYPE (TREE_TYPE (field)))
6391 TREE_TYPE (field) = TYPE_CONTEXT (TREE_TYPE (field));
6393 /* Clean up. */
6394 splay_tree_delete (empty_base_offsets);
6396 if (CLASSTYPE_EMPTY_P (t)
6397 && tree_int_cst_lt (sizeof_biggest_empty_class,
6398 TYPE_SIZE_UNIT (t)))
6399 sizeof_biggest_empty_class = TYPE_SIZE_UNIT (t);
6402 /* Determine the "key method" for the class type indicated by TYPE,
6403 and set CLASSTYPE_KEY_METHOD accordingly. */
6405 void
6406 determine_key_method (tree type)
6408 tree method;
6410 if (processing_template_decl
6411 || CLASSTYPE_TEMPLATE_INSTANTIATION (type)
6412 || CLASSTYPE_INTERFACE_KNOWN (type))
6413 return;
6415 /* The key method is the first non-pure virtual function that is not
6416 inline at the point of class definition. On some targets the
6417 key function may not be inline; those targets should not call
6418 this function until the end of the translation unit. */
6419 for (method = TYPE_FIELDS (type); method; method = DECL_CHAIN (method))
6420 if (TREE_CODE (method) == FUNCTION_DECL
6421 && DECL_VINDEX (method) != NULL_TREE
6422 && ! DECL_DECLARED_INLINE_P (method)
6423 && ! DECL_PURE_VIRTUAL_P (method))
6425 CLASSTYPE_KEY_METHOD (type) = method;
6426 break;
6429 return;
6432 /* Helper of find_flexarrays. Return true when FLD refers to a non-static
6433 class data member of non-zero size, otherwise false. */
6435 static inline bool
6436 field_nonempty_p (const_tree fld)
6438 if (TREE_CODE (fld) == ERROR_MARK)
6439 return false;
6441 tree type = TREE_TYPE (fld);
6442 if (TREE_CODE (fld) == FIELD_DECL
6443 && TREE_CODE (type) != ERROR_MARK
6444 && (DECL_NAME (fld) || RECORD_OR_UNION_TYPE_P (type)))
6446 return TYPE_SIZE (type)
6447 && (TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST
6448 || !tree_int_cst_equal (size_zero_node, TYPE_SIZE (type)));
6451 return false;
6454 /* Used by find_flexarrays and related functions. */
6456 struct flexmems_t
6458 /* The first flexible array member or non-zero array member found
6459 in the order of layout. */
6460 tree array;
6461 /* First non-static non-empty data member in the class or its bases. */
6462 tree first;
6463 /* The first non-static non-empty data member following either
6464 the flexible array member, if found, or the zero-length array member
6465 otherwise. AFTER[1] refers to the first such data member of a union
6466 of which the struct containing the flexible array member or zero-length
6467 array is a member, or NULL when no such union exists. This element is
6468 only used during searching, not for diagnosing problems. AFTER[0]
6469 refers to the first such data member that is not a member of such
6470 a union. */
6471 tree after[2];
6473 /* Refers to a struct (not union) in which the struct of which the flexible
6474 array is member is defined. Used to diagnose strictly (according to C)
6475 invalid uses of the latter structs. */
6476 tree enclosing;
6479 /* Find either the first flexible array member or the first zero-length
6480 array, in that order of preference, among members of class T (but not
6481 its base classes), and set members of FMEM accordingly.
6482 BASE_P is true if T is a base class of another class.
6483 PUN is set to the outermost union in which the flexible array member
6484 (or zero-length array) is defined if one such union exists, otherwise
6485 to NULL.
6486 Similarly, PSTR is set to a data member of the outermost struct of
6487 which the flexible array is a member if one such struct exists,
6488 otherwise to NULL. */
6490 static void
6491 find_flexarrays (tree t, flexmems_t *fmem, bool base_p,
6492 tree pun /* = NULL_TREE */,
6493 tree pstr /* = NULL_TREE */)
6495 /* Set the "pointer" to the outermost enclosing union if not set
6496 yet and maintain it for the remainder of the recursion. */
6497 if (!pun && TREE_CODE (t) == UNION_TYPE)
6498 pun = t;
6500 for (tree fld = TYPE_FIELDS (t); fld; fld = DECL_CHAIN (fld))
6502 if (fld == error_mark_node)
6503 return;
6505 /* Is FLD a typedef for an anonymous struct? */
6507 /* FIXME: Note that typedefs (as well as arrays) need to be fully
6508 handled elsewhere so that errors like the following are detected
6509 as well:
6510 typedef struct { int i, a[], j; } S; // bug c++/72753
6511 S s [2]; // bug c++/68489
6513 if (TREE_CODE (fld) == TYPE_DECL
6514 && DECL_IMPLICIT_TYPEDEF_P (fld)
6515 && CLASS_TYPE_P (TREE_TYPE (fld))
6516 && anon_aggrname_p (DECL_NAME (fld)))
6518 /* Check the nested unnamed type referenced via a typedef
6519 independently of FMEM (since it's not a data member of
6520 the enclosing class). */
6521 check_flexarrays (TREE_TYPE (fld));
6522 continue;
6525 /* Skip anything that's GCC-generated or not a (non-static) data
6526 member. */
6527 if (DECL_ARTIFICIAL (fld) || TREE_CODE (fld) != FIELD_DECL)
6528 continue;
6530 /* Type of the member. */
6531 tree fldtype = TREE_TYPE (fld);
6532 if (fldtype == error_mark_node)
6533 return;
6535 /* Determine the type of the array element or object referenced
6536 by the member so that it can be checked for flexible array
6537 members if it hasn't been yet. */
6538 tree eltype = fldtype;
6539 while (TREE_CODE (eltype) == ARRAY_TYPE
6540 || TREE_CODE (eltype) == POINTER_TYPE
6541 || TREE_CODE (eltype) == REFERENCE_TYPE)
6542 eltype = TREE_TYPE (eltype);
6544 if (RECORD_OR_UNION_TYPE_P (eltype))
6546 if (fmem->array && !fmem->after[bool (pun)])
6548 /* Once the member after the flexible array has been found
6549 we're done. */
6550 fmem->after[bool (pun)] = fld;
6551 break;
6554 if (eltype == fldtype || TYPE_UNNAMED_P (eltype))
6556 /* Descend into the non-static member struct or union and try
6557 to find a flexible array member or zero-length array among
6558 its members. This is only necessary for anonymous types
6559 and types in whose context the current type T has not been
6560 defined (the latter must not be checked again because they
6561 are already in the process of being checked by one of the
6562 recursive calls). */
6564 tree first = fmem->first;
6565 tree array = fmem->array;
6567 /* If this member isn't anonymous and a prior non-flexible array
6568 member has been seen in one of the enclosing structs, clear
6569 the FIRST member since it doesn't contribute to the flexible
6570 array struct's members. */
6571 if (first && !array && !ANON_AGGR_TYPE_P (eltype))
6572 fmem->first = NULL_TREE;
6574 find_flexarrays (eltype, fmem, false, pun,
6575 !pstr && TREE_CODE (t) == RECORD_TYPE ? fld : pstr);
6577 if (fmem->array != array)
6578 continue;
6580 if (first && !array && !ANON_AGGR_TYPE_P (eltype))
6582 /* Restore the FIRST member reset above if no flexible
6583 array member has been found in this member's struct. */
6584 fmem->first = first;
6587 /* If the member struct contains the first flexible array
6588 member, or if this member is a base class, continue to
6589 the next member and avoid setting the FMEM->NEXT pointer
6590 to point to it. */
6591 if (base_p)
6592 continue;
6596 if (field_nonempty_p (fld))
6598 /* Remember the first non-static data member. */
6599 if (!fmem->first)
6600 fmem->first = fld;
6602 /* Remember the first non-static data member after the flexible
6603 array member, if one has been found, or the zero-length array
6604 if it has been found. */
6605 if (fmem->array && !fmem->after[bool (pun)])
6606 fmem->after[bool (pun)] = fld;
6609 /* Skip non-arrays. */
6610 if (TREE_CODE (fldtype) != ARRAY_TYPE)
6611 continue;
6613 /* Determine the upper bound of the array if it has one. */
6614 if (TYPE_DOMAIN (fldtype))
6616 if (fmem->array)
6618 /* Make a record of the zero-length array if either one
6619 such field or a flexible array member has been seen to
6620 handle the pathological and unlikely case of multiple
6621 such members. */
6622 if (!fmem->after[bool (pun)])
6623 fmem->after[bool (pun)] = fld;
6625 else if (integer_all_onesp (TYPE_MAX_VALUE (TYPE_DOMAIN (fldtype))))
6627 /* Remember the first zero-length array unless a flexible array
6628 member has already been seen. */
6629 fmem->array = fld;
6630 fmem->enclosing = pstr;
6633 else
6635 /* Flexible array members have no upper bound. */
6636 if (fmem->array)
6638 /* Replace the zero-length array if it's been stored and
6639 reset the after pointer. */
6640 if (TYPE_DOMAIN (TREE_TYPE (fmem->array)))
6642 fmem->after[bool (pun)] = NULL_TREE;
6643 fmem->array = fld;
6644 fmem->enclosing = pstr;
6647 else
6649 fmem->array = fld;
6650 fmem->enclosing = pstr;
6656 /* Diagnose a strictly (by the C standard) invalid use of a struct with
6657 a flexible array member (or the zero-length array extension). */
6659 static void
6660 diagnose_invalid_flexarray (const flexmems_t *fmem)
6662 if (fmem->array && fmem->enclosing
6663 && pedwarn (location_of (fmem->enclosing), OPT_Wpedantic,
6664 TYPE_DOMAIN (TREE_TYPE (fmem->array))
6665 ? G_("invalid use of %q#T with a zero-size array "
6666 "in %q#D")
6667 : G_("invalid use of %q#T with a flexible array member "
6668 "in %q#T"),
6669 DECL_CONTEXT (fmem->array),
6670 DECL_CONTEXT (fmem->enclosing)))
6671 inform (DECL_SOURCE_LOCATION (fmem->array),
6672 "array member %q#D declared here", fmem->array);
6675 /* Issue diagnostics for invalid flexible array members or zero-length
6676 arrays that are not the last elements of the containing class or its
6677 base classes or that are its sole members. */
6679 static void
6680 diagnose_flexarrays (tree t, const flexmems_t *fmem)
6682 if (!fmem->array)
6683 return;
6685 if (fmem->first && !fmem->after[0])
6687 diagnose_invalid_flexarray (fmem);
6688 return;
6691 /* Has a diagnostic been issued? */
6692 bool diagd = false;
6694 const char *msg = 0;
6696 if (TYPE_DOMAIN (TREE_TYPE (fmem->array)))
6698 if (fmem->after[0])
6699 msg = G_("zero-size array member %qD not at end of %q#T");
6700 else if (!fmem->first)
6701 msg = G_("zero-size array member %qD in an otherwise empty %q#T");
6703 if (msg)
6705 location_t loc = DECL_SOURCE_LOCATION (fmem->array);
6707 if (pedwarn (loc, OPT_Wpedantic, msg, fmem->array, t))
6709 inform (location_of (t), "in the definition of %q#T", t);
6710 diagd = true;
6714 else
6716 if (fmem->after[0])
6717 msg = G_("flexible array member %qD not at end of %q#T");
6718 else if (!fmem->first)
6719 msg = G_("flexible array member %qD in an otherwise empty %q#T");
6721 if (msg)
6723 location_t loc = DECL_SOURCE_LOCATION (fmem->array);
6724 diagd = true;
6726 error_at (loc, msg, fmem->array, t);
6728 /* In the unlikely event that the member following the flexible
6729 array member is declared in a different class, or the member
6730 overlaps another member of a common union, point to it.
6731 Otherwise it should be obvious. */
6732 if (fmem->after[0]
6733 && ((DECL_CONTEXT (fmem->after[0])
6734 != DECL_CONTEXT (fmem->array))))
6736 inform (DECL_SOURCE_LOCATION (fmem->after[0]),
6737 "next member %q#D declared here",
6738 fmem->after[0]);
6739 inform (location_of (t), "in the definition of %q#T", t);
6744 if (!diagd && fmem->array && fmem->enclosing)
6745 diagnose_invalid_flexarray (fmem);
6749 /* Recursively check to make sure that any flexible array or zero-length
6750 array members of class T or its bases are valid (i.e., not the sole
6751 non-static data member of T and, if one exists, that it is the last
6752 non-static data member of T and its base classes. FMEM is expected
6753 to be initially null and is used internally by recursive calls to
6754 the function. Issue the appropriate diagnostics for the array member
6755 that fails the checks. */
6757 static void
6758 check_flexarrays (tree t, flexmems_t *fmem /* = NULL */,
6759 bool base_p /* = false */)
6761 /* Initialize the result of a search for flexible array and zero-length
6762 array members. Avoid doing any work if the most interesting FMEM data
6763 have already been populated. */
6764 flexmems_t flexmems = flexmems_t ();
6765 if (!fmem)
6766 fmem = &flexmems;
6767 else if (fmem->array && fmem->first && fmem->after[0])
6768 return;
6770 tree fam = fmem->array;
6772 /* Recursively check the primary base class first. */
6773 if (CLASSTYPE_HAS_PRIMARY_BASE_P (t))
6775 tree basetype = BINFO_TYPE (CLASSTYPE_PRIMARY_BINFO (t));
6776 check_flexarrays (basetype, fmem, true);
6779 /* Recursively check the base classes. */
6780 int nbases = TYPE_BINFO (t) ? BINFO_N_BASE_BINFOS (TYPE_BINFO (t)) : 0;
6781 for (int i = 0; i < nbases; ++i)
6783 tree base_binfo = BINFO_BASE_BINFO (TYPE_BINFO (t), i);
6785 /* The primary base class was already checked above. */
6786 if (base_binfo == CLASSTYPE_PRIMARY_BINFO (t))
6787 continue;
6789 /* Virtual base classes are at the end. */
6790 if (BINFO_VIRTUAL_P (base_binfo))
6791 continue;
6793 /* Check the base class. */
6794 check_flexarrays (BINFO_TYPE (base_binfo), fmem, /*base_p=*/true);
6797 if (fmem == &flexmems)
6799 /* Check virtual base classes only once per derived class.
6800 I.e., this check is not performed recursively for base
6801 classes. */
6802 int i;
6803 tree base_binfo;
6804 vec<tree, va_gc> *vbases;
6805 for (vbases = CLASSTYPE_VBASECLASSES (t), i = 0;
6806 vec_safe_iterate (vbases, i, &base_binfo); i++)
6808 /* Check the virtual base class. */
6809 tree basetype = TREE_TYPE (base_binfo);
6811 check_flexarrays (basetype, fmem, /*base_p=*/true);
6815 /* Is the type unnamed (and therefore a member of it potentially
6816 an anonymous struct or union)? */
6817 bool maybe_anon_p = TYPE_UNNAMED_P (t);
6819 /* Search the members of the current (possibly derived) class, skipping
6820 unnamed structs and unions since those could be anonymous. */
6821 if (fmem != &flexmems || !maybe_anon_p)
6822 find_flexarrays (t, fmem, base_p || fam != fmem->array);
6824 if (fmem == &flexmems && !maybe_anon_p)
6826 /* Issue diagnostics for invalid flexible and zero-length array
6827 members found in base classes or among the members of the current
6828 class. Ignore anonymous structs and unions whose members are
6829 considered to be members of the enclosing class and thus will
6830 be diagnosed when checking it. */
6831 diagnose_flexarrays (t, fmem);
6835 /* Perform processing required when the definition of T (a class type)
6836 is complete. Diagnose invalid definitions of flexible array members
6837 and zero-size arrays. */
6839 void
6840 finish_struct_1 (tree t)
6842 tree x;
6843 /* A TREE_LIST. The TREE_VALUE of each node is a FUNCTION_DECL. */
6844 tree virtuals = NULL_TREE;
6846 if (COMPLETE_TYPE_P (t))
6848 gcc_assert (MAYBE_CLASS_TYPE_P (t));
6849 error ("redefinition of %q#T", t);
6850 popclass ();
6851 return;
6854 /* If this type was previously laid out as a forward reference,
6855 make sure we lay it out again. */
6856 TYPE_SIZE (t) = NULL_TREE;
6857 CLASSTYPE_PRIMARY_BINFO (t) = NULL_TREE;
6859 /* Make assumptions about the class; we'll reset the flags if
6860 necessary. */
6861 CLASSTYPE_EMPTY_P (t) = 1;
6862 CLASSTYPE_NEARLY_EMPTY_P (t) = 1;
6863 CLASSTYPE_CONTAINS_EMPTY_CLASS_P (t) = 0;
6864 CLASSTYPE_LITERAL_P (t) = true;
6866 /* Do end-of-class semantic processing: checking the validity of the
6867 bases and members and add implicitly generated methods. */
6868 check_bases_and_members (t);
6870 /* Find the key method. */
6871 if (TYPE_CONTAINS_VPTR_P (t))
6873 /* The Itanium C++ ABI permits the key method to be chosen when
6874 the class is defined -- even though the key method so
6875 selected may later turn out to be an inline function. On
6876 some systems (such as ARM Symbian OS) the key method cannot
6877 be determined until the end of the translation unit. On such
6878 systems, we leave CLASSTYPE_KEY_METHOD set to NULL, which
6879 will cause the class to be added to KEYED_CLASSES. Then, in
6880 finish_file we will determine the key method. */
6881 if (targetm.cxx.key_method_may_be_inline ())
6882 determine_key_method (t);
6884 /* If a polymorphic class has no key method, we may emit the vtable
6885 in every translation unit where the class definition appears. If
6886 we're devirtualizing, we can look into the vtable even if we
6887 aren't emitting it. */
6888 if (!CLASSTYPE_KEY_METHOD (t))
6889 vec_safe_push (keyed_classes, t);
6892 /* Layout the class itself. */
6893 layout_class_type (t, &virtuals);
6894 /* COMPLETE_TYPE_P is now true. */
6896 set_class_bindings (t);
6898 if (CLASSTYPE_AS_BASE (t) != t)
6899 /* We use the base type for trivial assignments, and hence it
6900 needs a mode. */
6901 compute_record_mode (CLASSTYPE_AS_BASE (t));
6903 /* With the layout complete, check for flexible array members and
6904 zero-length arrays that might overlap other members in the final
6905 layout. */
6906 check_flexarrays (t);
6908 virtuals = modify_all_vtables (t, nreverse (virtuals));
6910 /* If necessary, create the primary vtable for this class. */
6911 if (virtuals || TYPE_CONTAINS_VPTR_P (t))
6913 /* We must enter these virtuals into the table. */
6914 if (!CLASSTYPE_HAS_PRIMARY_BASE_P (t))
6915 build_primary_vtable (NULL_TREE, t);
6916 else if (! BINFO_NEW_VTABLE_MARKED (TYPE_BINFO (t)))
6917 /* Here we know enough to change the type of our virtual
6918 function table, but we will wait until later this function. */
6919 build_primary_vtable (CLASSTYPE_PRIMARY_BINFO (t), t);
6921 /* If we're warning about ABI tags, check the types of the new
6922 virtual functions. */
6923 if (warn_abi_tag)
6924 for (tree v = virtuals; v; v = TREE_CHAIN (v))
6925 check_abi_tags (t, TREE_VALUE (v));
6928 if (TYPE_CONTAINS_VPTR_P (t))
6930 int vindex;
6931 tree fn;
6933 if (BINFO_VTABLE (TYPE_BINFO (t)))
6934 gcc_assert (DECL_VIRTUAL_P (BINFO_VTABLE (TYPE_BINFO (t))));
6935 if (!CLASSTYPE_HAS_PRIMARY_BASE_P (t))
6936 gcc_assert (BINFO_VIRTUALS (TYPE_BINFO (t)) == NULL_TREE);
6938 /* Add entries for virtual functions introduced by this class. */
6939 BINFO_VIRTUALS (TYPE_BINFO (t))
6940 = chainon (BINFO_VIRTUALS (TYPE_BINFO (t)), virtuals);
6942 /* Set DECL_VINDEX for all functions declared in this class. */
6943 for (vindex = 0, fn = BINFO_VIRTUALS (TYPE_BINFO (t));
6945 fn = TREE_CHAIN (fn),
6946 vindex += (TARGET_VTABLE_USES_DESCRIPTORS
6947 ? TARGET_VTABLE_USES_DESCRIPTORS : 1))
6949 tree fndecl = BV_FN (fn);
6951 if (DECL_THUNK_P (fndecl))
6952 /* A thunk. We should never be calling this entry directly
6953 from this vtable -- we'd use the entry for the non
6954 thunk base function. */
6955 DECL_VINDEX (fndecl) = NULL_TREE;
6956 else if (TREE_CODE (DECL_VINDEX (fndecl)) != INTEGER_CST)
6957 DECL_VINDEX (fndecl) = build_int_cst (NULL_TREE, vindex);
6961 finish_struct_bits (t);
6963 set_method_tm_attributes (t);
6964 if (flag_openmp || flag_openmp_simd)
6965 finish_omp_declare_simd_methods (t);
6967 /* Clear DECL_IN_AGGR_P for all member functions. Complete the rtl
6968 for any static member objects of the type we're working on. */
6969 for (x = TYPE_FIELDS (t); x; x = DECL_CHAIN (x))
6970 if (DECL_DECLARES_FUNCTION_P (x))
6971 DECL_IN_AGGR_P (x) = false;
6972 else if (VAR_P (x) && TREE_STATIC (x)
6973 && TREE_TYPE (x) != error_mark_node
6974 && same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (x)), t))
6975 SET_DECL_MODE (x, TYPE_MODE (t));
6977 /* Complain if one of the field types requires lower visibility. */
6978 constrain_class_visibility (t);
6980 /* Make the rtl for any new vtables we have created, and unmark
6981 the base types we marked. */
6982 finish_vtbls (t);
6984 /* Build the VTT for T. */
6985 build_vtt (t);
6987 if (warn_nonvdtor
6988 && TYPE_POLYMORPHIC_P (t) && accessible_nvdtor_p (t)
6989 && !CLASSTYPE_FINAL (t))
6990 warning (OPT_Wnon_virtual_dtor,
6991 "%q#T has virtual functions and accessible"
6992 " non-virtual destructor", t);
6994 complete_vars (t);
6996 if (warn_overloaded_virtual)
6997 warn_hidden (t);
6999 /* Class layout, assignment of virtual table slots, etc., is now
7000 complete. Give the back end a chance to tweak the visibility of
7001 the class or perform any other required target modifications. */
7002 targetm.cxx.adjust_class_at_definition (t);
7004 maybe_suppress_debug_info (t);
7006 if (flag_vtable_verify)
7007 vtv_save_class_info (t);
7009 dump_class_hierarchy (t);
7011 /* Finish debugging output for this type. */
7012 rest_of_type_compilation (t, ! LOCAL_CLASS_P (t));
7014 if (TYPE_TRANSPARENT_AGGR (t))
7016 tree field = first_field (t);
7017 if (field == NULL_TREE || error_operand_p (field))
7019 error ("type transparent %q#T does not have any fields", t);
7020 TYPE_TRANSPARENT_AGGR (t) = 0;
7022 else if (DECL_ARTIFICIAL (field))
7024 if (DECL_FIELD_IS_BASE (field))
7025 error ("type transparent class %qT has base classes", t);
7026 else
7028 gcc_checking_assert (DECL_VIRTUAL_P (field));
7029 error ("type transparent class %qT has virtual functions", t);
7031 TYPE_TRANSPARENT_AGGR (t) = 0;
7033 else if (TYPE_MODE (t) != DECL_MODE (field))
7035 error ("type transparent %q#T cannot be made transparent because "
7036 "the type of the first field has a different ABI from the "
7037 "class overall", t);
7038 TYPE_TRANSPARENT_AGGR (t) = 0;
7043 /* When T was built up, the member declarations were added in reverse
7044 order. Rearrange them to declaration order. */
7046 void
7047 unreverse_member_declarations (tree t)
7049 tree next;
7050 tree prev;
7051 tree x;
7053 /* The following lists are all in reverse order. Put them in
7054 declaration order now. */
7055 CLASSTYPE_DECL_LIST (t) = nreverse (CLASSTYPE_DECL_LIST (t));
7057 /* For the TYPE_FIELDS, only the non TYPE_DECLs are in reverse
7058 order, so we can't just use nreverse. Due to stat_hack
7059 chicanery in finish_member_declarations. */
7060 prev = NULL_TREE;
7061 for (x = TYPE_FIELDS (t);
7062 x && TREE_CODE (x) != TYPE_DECL;
7063 x = next)
7065 next = DECL_CHAIN (x);
7066 DECL_CHAIN (x) = prev;
7067 prev = x;
7070 if (prev)
7072 DECL_CHAIN (TYPE_FIELDS (t)) = x;
7073 if (prev)
7074 TYPE_FIELDS (t) = prev;
7078 tree
7079 finish_struct (tree t, tree attributes)
7081 location_t saved_loc = input_location;
7083 /* Now that we've got all the field declarations, reverse everything
7084 as necessary. */
7085 unreverse_member_declarations (t);
7087 cplus_decl_attributes (&t, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
7088 fixup_attribute_variants (t);
7090 /* Nadger the current location so that diagnostics point to the start of
7091 the struct, not the end. */
7092 input_location = DECL_SOURCE_LOCATION (TYPE_NAME (t));
7094 if (processing_template_decl)
7096 tree x;
7098 /* We need to add the target functions of USING_DECLS, so that
7099 they can be found when the using declaration is not
7100 instantiated yet. */
7101 for (x = TYPE_FIELDS (t); x; x = DECL_CHAIN (x))
7102 if (TREE_CODE (x) == USING_DECL)
7104 tree fn = strip_using_decl (x);
7105 if (OVL_P (fn))
7106 for (lkp_iterator iter (fn); iter; ++iter)
7107 add_method (t, *iter, true);
7109 else if (DECL_DECLARES_FUNCTION_P (x))
7110 DECL_IN_AGGR_P (x) = false;
7112 TYPE_SIZE (t) = bitsize_zero_node;
7113 TYPE_SIZE_UNIT (t) = size_zero_node;
7114 /* COMPLETE_TYPE_P is now true. */
7116 set_class_bindings (t);
7118 /* We need to emit an error message if this type was used as a parameter
7119 and it is an abstract type, even if it is a template. We construct
7120 a simple CLASSTYPE_PURE_VIRTUALS list without taking bases into
7121 account and we call complete_vars with this type, which will check
7122 the PARM_DECLS. Note that while the type is being defined,
7123 CLASSTYPE_PURE_VIRTUALS contains the list of the inline friends
7124 (see CLASSTYPE_INLINE_FRIENDS) so we need to clear it. */
7125 CLASSTYPE_PURE_VIRTUALS (t) = NULL;
7126 for (x = TYPE_FIELDS (t); x; x = DECL_CHAIN (x))
7127 if (TREE_CODE (x) == FUNCTION_DECL && DECL_PURE_VIRTUAL_P (x))
7128 vec_safe_push (CLASSTYPE_PURE_VIRTUALS (t), x);
7129 complete_vars (t);
7131 /* Remember current #pragma pack value. */
7132 TYPE_PRECISION (t) = maximum_field_alignment;
7134 /* Fix up any variants we've already built. */
7135 for (x = TYPE_NEXT_VARIANT (t); x; x = TYPE_NEXT_VARIANT (x))
7137 TYPE_SIZE (x) = TYPE_SIZE (t);
7138 TYPE_SIZE_UNIT (x) = TYPE_SIZE_UNIT (t);
7139 TYPE_FIELDS (x) = TYPE_FIELDS (t);
7142 else
7143 finish_struct_1 (t);
7144 /* COMPLETE_TYPE_P is now true. */
7146 maybe_warn_about_overly_private_class (t);
7148 if (is_std_init_list (t))
7150 /* People keep complaining that the compiler crashes on an invalid
7151 definition of initializer_list, so I guess we should explicitly
7152 reject it. What the compiler internals care about is that it's a
7153 template and has a pointer field followed by an integer field. */
7154 bool ok = false;
7155 if (processing_template_decl)
7157 tree f = next_initializable_field (TYPE_FIELDS (t));
7158 if (f && TREE_CODE (TREE_TYPE (f)) == POINTER_TYPE)
7160 f = next_initializable_field (DECL_CHAIN (f));
7161 if (f && same_type_p (TREE_TYPE (f), size_type_node))
7162 ok = true;
7165 if (!ok)
7166 fatal_error (input_location,
7167 "definition of std::initializer_list does not match "
7168 "#include <initializer_list>");
7171 input_location = saved_loc;
7173 TYPE_BEING_DEFINED (t) = 0;
7175 if (current_class_type)
7176 popclass ();
7177 else
7178 error ("trying to finish struct, but kicked out due to previous parse errors");
7180 if (processing_template_decl && at_function_scope_p ()
7181 /* Lambdas are defined by the LAMBDA_EXPR. */
7182 && !LAMBDA_TYPE_P (t))
7183 add_stmt (build_min (TAG_DEFN, t));
7185 return t;
7188 /* Hash table to avoid endless recursion when handling references. */
7189 static hash_table<nofree_ptr_hash<tree_node> > *fixed_type_or_null_ref_ht;
7191 /* Return the dynamic type of INSTANCE, if known.
7192 Used to determine whether the virtual function table is needed
7193 or not.
7195 *NONNULL is set iff INSTANCE can be known to be nonnull, regardless
7196 of our knowledge of its type. *NONNULL should be initialized
7197 before this function is called. */
7199 static tree
7200 fixed_type_or_null (tree instance, int *nonnull, int *cdtorp)
7202 #define RECUR(T) fixed_type_or_null((T), nonnull, cdtorp)
7204 switch (TREE_CODE (instance))
7206 case INDIRECT_REF:
7207 if (POINTER_TYPE_P (TREE_TYPE (instance)))
7208 return NULL_TREE;
7209 else
7210 return RECUR (TREE_OPERAND (instance, 0));
7212 case CALL_EXPR:
7213 /* This is a call to a constructor, hence it's never zero. */
7214 if (TREE_HAS_CONSTRUCTOR (instance))
7216 if (nonnull)
7217 *nonnull = 1;
7218 return TREE_TYPE (instance);
7220 return NULL_TREE;
7222 case SAVE_EXPR:
7223 /* This is a call to a constructor, hence it's never zero. */
7224 if (TREE_HAS_CONSTRUCTOR (instance))
7226 if (nonnull)
7227 *nonnull = 1;
7228 return TREE_TYPE (instance);
7230 return RECUR (TREE_OPERAND (instance, 0));
7232 case POINTER_PLUS_EXPR:
7233 case PLUS_EXPR:
7234 case MINUS_EXPR:
7235 if (TREE_CODE (TREE_OPERAND (instance, 0)) == ADDR_EXPR)
7236 return RECUR (TREE_OPERAND (instance, 0));
7237 if (TREE_CODE (TREE_OPERAND (instance, 1)) == INTEGER_CST)
7238 /* Propagate nonnull. */
7239 return RECUR (TREE_OPERAND (instance, 0));
7241 return NULL_TREE;
7243 CASE_CONVERT:
7244 return RECUR (TREE_OPERAND (instance, 0));
7246 case ADDR_EXPR:
7247 instance = TREE_OPERAND (instance, 0);
7248 if (nonnull)
7250 /* Just because we see an ADDR_EXPR doesn't mean we're dealing
7251 with a real object -- given &p->f, p can still be null. */
7252 tree t = get_base_address (instance);
7253 /* ??? Probably should check DECL_WEAK here. */
7254 if (t && DECL_P (t))
7255 *nonnull = 1;
7257 return RECUR (instance);
7259 case COMPONENT_REF:
7260 /* If this component is really a base class reference, then the field
7261 itself isn't definitive. */
7262 if (DECL_FIELD_IS_BASE (TREE_OPERAND (instance, 1)))
7263 return RECUR (TREE_OPERAND (instance, 0));
7264 return RECUR (TREE_OPERAND (instance, 1));
7266 case VAR_DECL:
7267 case FIELD_DECL:
7268 if (TREE_CODE (TREE_TYPE (instance)) == ARRAY_TYPE
7269 && MAYBE_CLASS_TYPE_P (TREE_TYPE (TREE_TYPE (instance))))
7271 if (nonnull)
7272 *nonnull = 1;
7273 return TREE_TYPE (TREE_TYPE (instance));
7275 /* fall through. */
7276 case TARGET_EXPR:
7277 case PARM_DECL:
7278 case RESULT_DECL:
7279 if (MAYBE_CLASS_TYPE_P (TREE_TYPE (instance)))
7281 if (nonnull)
7282 *nonnull = 1;
7283 return TREE_TYPE (instance);
7285 else if (instance == current_class_ptr)
7287 if (nonnull)
7288 *nonnull = 1;
7290 /* if we're in a ctor or dtor, we know our type. If
7291 current_class_ptr is set but we aren't in a function, we're in
7292 an NSDMI (and therefore a constructor). */
7293 if (current_scope () != current_function_decl
7294 || (DECL_LANG_SPECIFIC (current_function_decl)
7295 && (DECL_CONSTRUCTOR_P (current_function_decl)
7296 || DECL_DESTRUCTOR_P (current_function_decl))))
7298 if (cdtorp)
7299 *cdtorp = 1;
7300 return TREE_TYPE (TREE_TYPE (instance));
7303 else if (TREE_CODE (TREE_TYPE (instance)) == REFERENCE_TYPE)
7305 /* We only need one hash table because it is always left empty. */
7306 if (!fixed_type_or_null_ref_ht)
7307 fixed_type_or_null_ref_ht
7308 = new hash_table<nofree_ptr_hash<tree_node> > (37);
7310 /* Reference variables should be references to objects. */
7311 if (nonnull)
7312 *nonnull = 1;
7314 /* Enter the INSTANCE in a table to prevent recursion; a
7315 variable's initializer may refer to the variable
7316 itself. */
7317 if (VAR_P (instance)
7318 && DECL_INITIAL (instance)
7319 && !type_dependent_expression_p_push (DECL_INITIAL (instance))
7320 && !fixed_type_or_null_ref_ht->find (instance))
7322 tree type;
7323 tree_node **slot;
7325 slot = fixed_type_or_null_ref_ht->find_slot (instance, INSERT);
7326 *slot = instance;
7327 type = RECUR (DECL_INITIAL (instance));
7328 fixed_type_or_null_ref_ht->remove_elt (instance);
7330 return type;
7333 return NULL_TREE;
7335 default:
7336 return NULL_TREE;
7338 #undef RECUR
7341 /* Return nonzero if the dynamic type of INSTANCE is known, and
7342 equivalent to the static type. We also handle the case where
7343 INSTANCE is really a pointer. Return negative if this is a
7344 ctor/dtor. There the dynamic type is known, but this might not be
7345 the most derived base of the original object, and hence virtual
7346 bases may not be laid out according to this type.
7348 Used to determine whether the virtual function table is needed
7349 or not.
7351 *NONNULL is set iff INSTANCE can be known to be nonnull, regardless
7352 of our knowledge of its type. *NONNULL should be initialized
7353 before this function is called. */
7356 resolves_to_fixed_type_p (tree instance, int* nonnull)
7358 tree t = TREE_TYPE (instance);
7359 int cdtorp = 0;
7360 tree fixed;
7362 /* processing_template_decl can be false in a template if we're in
7363 instantiate_non_dependent_expr, but we still want to suppress
7364 this check. */
7365 if (in_template_function ())
7367 /* In a template we only care about the type of the result. */
7368 if (nonnull)
7369 *nonnull = true;
7370 return true;
7373 fixed = fixed_type_or_null (instance, nonnull, &cdtorp);
7374 if (fixed == NULL_TREE)
7375 return 0;
7376 if (POINTER_TYPE_P (t))
7377 t = TREE_TYPE (t);
7378 if (!same_type_ignoring_top_level_qualifiers_p (t, fixed))
7379 return 0;
7380 return cdtorp ? -1 : 1;
7384 void
7385 init_class_processing (void)
7387 current_class_depth = 0;
7388 current_class_stack_size = 10;
7389 current_class_stack
7390 = XNEWVEC (struct class_stack_node, current_class_stack_size);
7391 vec_alloc (local_classes, 8);
7392 sizeof_biggest_empty_class = size_zero_node;
7394 ridpointers[(int) RID_PUBLIC] = access_public_node;
7395 ridpointers[(int) RID_PRIVATE] = access_private_node;
7396 ridpointers[(int) RID_PROTECTED] = access_protected_node;
7399 /* Restore the cached PREVIOUS_CLASS_LEVEL. */
7401 static void
7402 restore_class_cache (void)
7404 tree type;
7406 /* We are re-entering the same class we just left, so we don't
7407 have to search the whole inheritance matrix to find all the
7408 decls to bind again. Instead, we install the cached
7409 class_shadowed list and walk through it binding names. */
7410 push_binding_level (previous_class_level);
7411 class_binding_level = previous_class_level;
7412 /* Restore IDENTIFIER_TYPE_VALUE. */
7413 for (type = class_binding_level->type_shadowed;
7414 type;
7415 type = TREE_CHAIN (type))
7416 SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (type), TREE_TYPE (type));
7419 /* Set global variables CURRENT_CLASS_NAME and CURRENT_CLASS_TYPE as
7420 appropriate for TYPE.
7422 So that we may avoid calls to lookup_name, we cache the _TYPE
7423 nodes of local TYPE_DECLs in the TREE_TYPE field of the name.
7425 For multiple inheritance, we perform a two-pass depth-first search
7426 of the type lattice. */
7428 void
7429 pushclass (tree type)
7431 class_stack_node_t csn;
7433 type = TYPE_MAIN_VARIANT (type);
7435 /* Make sure there is enough room for the new entry on the stack. */
7436 if (current_class_depth + 1 >= current_class_stack_size)
7438 current_class_stack_size *= 2;
7439 current_class_stack
7440 = XRESIZEVEC (struct class_stack_node, current_class_stack,
7441 current_class_stack_size);
7444 /* Insert a new entry on the class stack. */
7445 csn = current_class_stack + current_class_depth;
7446 csn->name = current_class_name;
7447 csn->type = current_class_type;
7448 csn->access = current_access_specifier;
7449 csn->names_used = 0;
7450 csn->hidden = 0;
7451 current_class_depth++;
7453 /* Now set up the new type. */
7454 current_class_name = TYPE_NAME (type);
7455 if (TREE_CODE (current_class_name) == TYPE_DECL)
7456 current_class_name = DECL_NAME (current_class_name);
7457 current_class_type = type;
7459 /* By default, things in classes are private, while things in
7460 structures or unions are public. */
7461 current_access_specifier = (CLASSTYPE_DECLARED_CLASS (type)
7462 ? access_private_node
7463 : access_public_node);
7465 if (previous_class_level
7466 && type != previous_class_level->this_entity
7467 && current_class_depth == 1)
7469 /* Forcibly remove any old class remnants. */
7470 invalidate_class_lookup_cache ();
7473 if (!previous_class_level
7474 || type != previous_class_level->this_entity
7475 || current_class_depth > 1)
7476 pushlevel_class ();
7477 else
7478 restore_class_cache ();
7481 /* When we exit a toplevel class scope, we save its binding level so
7482 that we can restore it quickly. Here, we've entered some other
7483 class, so we must invalidate our cache. */
7485 void
7486 invalidate_class_lookup_cache (void)
7488 previous_class_level = NULL;
7491 /* Get out of the current class scope. If we were in a class scope
7492 previously, that is the one popped to. */
7494 void
7495 popclass (void)
7497 poplevel_class ();
7499 current_class_depth--;
7500 current_class_name = current_class_stack[current_class_depth].name;
7501 current_class_type = current_class_stack[current_class_depth].type;
7502 current_access_specifier = current_class_stack[current_class_depth].access;
7503 if (current_class_stack[current_class_depth].names_used)
7504 splay_tree_delete (current_class_stack[current_class_depth].names_used);
7507 /* Mark the top of the class stack as hidden. */
7509 void
7510 push_class_stack (void)
7512 if (current_class_depth)
7513 ++current_class_stack[current_class_depth - 1].hidden;
7516 /* Mark the top of the class stack as un-hidden. */
7518 void
7519 pop_class_stack (void)
7521 if (current_class_depth)
7522 --current_class_stack[current_class_depth - 1].hidden;
7525 /* Returns 1 if the class type currently being defined is either T or
7526 a nested type of T. Returns the type from the current_class_stack,
7527 which might be equivalent to but not equal to T in case of
7528 constrained partial specializations. */
7530 tree
7531 currently_open_class (tree t)
7533 int i;
7535 if (!CLASS_TYPE_P (t))
7536 return NULL_TREE;
7538 t = TYPE_MAIN_VARIANT (t);
7540 /* We start looking from 1 because entry 0 is from global scope,
7541 and has no type. */
7542 for (i = current_class_depth; i > 0; --i)
7544 tree c;
7545 if (i == current_class_depth)
7546 c = current_class_type;
7547 else
7549 if (current_class_stack[i].hidden)
7550 break;
7551 c = current_class_stack[i].type;
7553 if (!c)
7554 continue;
7555 if (same_type_p (c, t))
7556 return c;
7558 return NULL_TREE;
7561 /* If either current_class_type or one of its enclosing classes are derived
7562 from T, return the appropriate type. Used to determine how we found
7563 something via unqualified lookup. */
7565 tree
7566 currently_open_derived_class (tree t)
7568 int i;
7570 /* The bases of a dependent type are unknown. */
7571 if (dependent_type_p (t))
7572 return NULL_TREE;
7574 if (!current_class_type)
7575 return NULL_TREE;
7577 if (DERIVED_FROM_P (t, current_class_type))
7578 return current_class_type;
7580 for (i = current_class_depth - 1; i > 0; --i)
7582 if (current_class_stack[i].hidden)
7583 break;
7584 if (DERIVED_FROM_P (t, current_class_stack[i].type))
7585 return current_class_stack[i].type;
7588 return NULL_TREE;
7591 /* Return the outermost enclosing class type that is still open, or
7592 NULL_TREE. */
7594 tree
7595 outermost_open_class (void)
7597 if (!current_class_type)
7598 return NULL_TREE;
7599 tree r = NULL_TREE;
7600 if (TYPE_BEING_DEFINED (current_class_type))
7601 r = current_class_type;
7602 for (int i = current_class_depth - 1; i > 0; --i)
7604 if (current_class_stack[i].hidden)
7605 break;
7606 tree t = current_class_stack[i].type;
7607 if (!TYPE_BEING_DEFINED (t))
7608 break;
7609 r = t;
7611 return r;
7614 /* Returns the innermost class type which is not a lambda closure type. */
7616 tree
7617 current_nonlambda_class_type (void)
7619 tree type = current_class_type;
7620 while (type && LAMBDA_TYPE_P (type))
7621 type = decl_type_context (TYPE_NAME (type));
7622 return type;
7625 /* When entering a class scope, all enclosing class scopes' names with
7626 static meaning (static variables, static functions, types and
7627 enumerators) have to be visible. This recursive function calls
7628 pushclass for all enclosing class contexts until global or a local
7629 scope is reached. TYPE is the enclosed class. */
7631 void
7632 push_nested_class (tree type)
7634 /* A namespace might be passed in error cases, like A::B:C. */
7635 if (type == NULL_TREE
7636 || !CLASS_TYPE_P (type))
7637 return;
7639 push_nested_class (DECL_CONTEXT (TYPE_MAIN_DECL (type)));
7641 pushclass (type);
7644 /* Undoes a push_nested_class call. */
7646 void
7647 pop_nested_class (void)
7649 tree context = DECL_CONTEXT (TYPE_MAIN_DECL (current_class_type));
7651 popclass ();
7652 if (context && CLASS_TYPE_P (context))
7653 pop_nested_class ();
7656 /* Returns the number of extern "LANG" blocks we are nested within. */
7659 current_lang_depth (void)
7661 return vec_safe_length (current_lang_base);
7664 /* Set global variables CURRENT_LANG_NAME to appropriate value
7665 so that behavior of name-mangling machinery is correct. */
7667 void
7668 push_lang_context (tree name)
7670 vec_safe_push (current_lang_base, current_lang_name);
7672 if (name == lang_name_cplusplus)
7673 current_lang_name = name;
7674 else if (name == lang_name_c)
7675 current_lang_name = name;
7676 else
7677 error ("language string %<\"%E\"%> not recognized", name);
7680 /* Get out of the current language scope. */
7682 void
7683 pop_lang_context (void)
7685 current_lang_name = current_lang_base->pop ();
7688 /* Type instantiation routines. */
7690 /* Given an OVERLOAD and a TARGET_TYPE, return the function that
7691 matches the TARGET_TYPE. If there is no satisfactory match, return
7692 error_mark_node, and issue an error & warning messages under
7693 control of FLAGS. Permit pointers to member function if FLAGS
7694 permits. If TEMPLATE_ONLY, the name of the overloaded function was
7695 a template-id, and EXPLICIT_TARGS are the explicitly provided
7696 template arguments.
7698 If OVERLOAD is for one or more member functions, then ACCESS_PATH
7699 is the base path used to reference those member functions. If
7700 the address is resolved to a member function, access checks will be
7701 performed and errors issued if appropriate. */
7703 static tree
7704 resolve_address_of_overloaded_function (tree target_type,
7705 tree overload,
7706 tsubst_flags_t complain,
7707 bool template_only,
7708 tree explicit_targs,
7709 tree access_path)
7711 /* Here's what the standard says:
7713 [over.over]
7715 If the name is a function template, template argument deduction
7716 is done, and if the argument deduction succeeds, the deduced
7717 arguments are used to generate a single template function, which
7718 is added to the set of overloaded functions considered.
7720 Non-member functions and static member functions match targets of
7721 type "pointer-to-function" or "reference-to-function." Nonstatic
7722 member functions match targets of type "pointer-to-member
7723 function;" the function type of the pointer to member is used to
7724 select the member function from the set of overloaded member
7725 functions. If a nonstatic member function is selected, the
7726 reference to the overloaded function name is required to have the
7727 form of a pointer to member as described in 5.3.1.
7729 If more than one function is selected, any template functions in
7730 the set are eliminated if the set also contains a non-template
7731 function, and any given template function is eliminated if the
7732 set contains a second template function that is more specialized
7733 than the first according to the partial ordering rules 14.5.5.2.
7734 After such eliminations, if any, there shall remain exactly one
7735 selected function. */
7737 int is_ptrmem = 0;
7738 /* We store the matches in a TREE_LIST rooted here. The functions
7739 are the TREE_PURPOSE, not the TREE_VALUE, in this list, for easy
7740 interoperability with most_specialized_instantiation. */
7741 tree matches = NULL_TREE;
7742 tree fn;
7743 tree target_fn_type;
7745 /* By the time we get here, we should be seeing only real
7746 pointer-to-member types, not the internal POINTER_TYPE to
7747 METHOD_TYPE representation. */
7748 gcc_assert (!TYPE_PTR_P (target_type)
7749 || TREE_CODE (TREE_TYPE (target_type)) != METHOD_TYPE);
7751 gcc_assert (is_overloaded_fn (overload));
7753 /* Check that the TARGET_TYPE is reasonable. */
7754 if (TYPE_PTRFN_P (target_type)
7755 || TYPE_REFFN_P (target_type))
7756 /* This is OK. */;
7757 else if (TYPE_PTRMEMFUNC_P (target_type))
7758 /* This is OK, too. */
7759 is_ptrmem = 1;
7760 else if (TREE_CODE (target_type) == FUNCTION_TYPE)
7761 /* This is OK, too. This comes from a conversion to reference
7762 type. */
7763 target_type = build_reference_type (target_type);
7764 else
7766 if (complain & tf_error)
7767 error ("cannot resolve overloaded function %qD based on"
7768 " conversion to type %qT",
7769 OVL_NAME (overload), target_type);
7770 return error_mark_node;
7773 /* Non-member functions and static member functions match targets of type
7774 "pointer-to-function" or "reference-to-function." Nonstatic member
7775 functions match targets of type "pointer-to-member-function;" the
7776 function type of the pointer to member is used to select the member
7777 function from the set of overloaded member functions.
7779 So figure out the FUNCTION_TYPE that we want to match against. */
7780 target_fn_type = static_fn_type (target_type);
7782 /* If we can find a non-template function that matches, we can just
7783 use it. There's no point in generating template instantiations
7784 if we're just going to throw them out anyhow. But, of course, we
7785 can only do this when we don't *need* a template function. */
7786 if (!template_only)
7787 for (lkp_iterator iter (overload); iter; ++iter)
7789 tree fn = *iter;
7791 if (TREE_CODE (fn) == TEMPLATE_DECL)
7792 /* We're not looking for templates just yet. */
7793 continue;
7795 if ((TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE) != is_ptrmem)
7796 /* We're looking for a non-static member, and this isn't
7797 one, or vice versa. */
7798 continue;
7800 /* In C++17 we need the noexcept-qualifier to compare types. */
7801 if (flag_noexcept_type
7802 && !maybe_instantiate_noexcept (fn, complain))
7803 continue;
7805 /* See if there's a match. */
7806 tree fntype = static_fn_type (fn);
7807 if (same_type_p (target_fn_type, fntype)
7808 || fnptr_conv_p (target_fn_type, fntype))
7809 matches = tree_cons (fn, NULL_TREE, matches);
7812 /* Now, if we've already got a match (or matches), there's no need
7813 to proceed to the template functions. But, if we don't have a
7814 match we need to look at them, too. */
7815 if (!matches)
7817 tree target_arg_types;
7818 tree target_ret_type;
7819 tree *args;
7820 unsigned int nargs, ia;
7821 tree arg;
7823 target_arg_types = TYPE_ARG_TYPES (target_fn_type);
7824 target_ret_type = TREE_TYPE (target_fn_type);
7826 nargs = list_length (target_arg_types);
7827 args = XALLOCAVEC (tree, nargs);
7828 for (arg = target_arg_types, ia = 0;
7829 arg != NULL_TREE && arg != void_list_node;
7830 arg = TREE_CHAIN (arg), ++ia)
7831 args[ia] = TREE_VALUE (arg);
7832 nargs = ia;
7834 for (lkp_iterator iter (overload); iter; ++iter)
7836 tree fn = *iter;
7837 tree instantiation;
7838 tree targs;
7840 if (TREE_CODE (fn) != TEMPLATE_DECL)
7841 /* We're only looking for templates. */
7842 continue;
7844 if ((TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
7845 != is_ptrmem)
7846 /* We're not looking for a non-static member, and this is
7847 one, or vice versa. */
7848 continue;
7850 tree ret = target_ret_type;
7852 /* If the template has a deduced return type, don't expose it to
7853 template argument deduction. */
7854 if (undeduced_auto_decl (fn))
7855 ret = NULL_TREE;
7857 /* Try to do argument deduction. */
7858 targs = make_tree_vec (DECL_NTPARMS (fn));
7859 instantiation = fn_type_unification (fn, explicit_targs, targs, args,
7860 nargs, ret,
7861 DEDUCE_EXACT, LOOKUP_NORMAL,
7862 false, false);
7863 if (instantiation == error_mark_node)
7864 /* Instantiation failed. */
7865 continue;
7867 /* Constraints must be satisfied. This is done before
7868 return type deduction since that instantiates the
7869 function. */
7870 if (flag_concepts && !constraints_satisfied_p (instantiation))
7871 continue;
7873 /* And now force instantiation to do return type deduction. */
7874 if (undeduced_auto_decl (instantiation))
7876 ++function_depth;
7877 instantiate_decl (instantiation, /*defer*/false, /*class*/false);
7878 --function_depth;
7880 require_deduced_type (instantiation);
7883 /* In C++17 we need the noexcept-qualifier to compare types. */
7884 if (flag_noexcept_type)
7885 maybe_instantiate_noexcept (instantiation, complain);
7887 /* See if there's a match. */
7888 tree fntype = static_fn_type (instantiation);
7889 if (same_type_p (target_fn_type, fntype)
7890 || fnptr_conv_p (target_fn_type, fntype))
7891 matches = tree_cons (instantiation, fn, matches);
7894 /* Now, remove all but the most specialized of the matches. */
7895 if (matches)
7897 tree match = most_specialized_instantiation (matches);
7899 if (match != error_mark_node)
7900 matches = tree_cons (TREE_PURPOSE (match),
7901 NULL_TREE,
7902 NULL_TREE);
7906 /* Now we should have exactly one function in MATCHES. */
7907 if (matches == NULL_TREE)
7909 /* There were *no* matches. */
7910 if (complain & tf_error)
7912 error ("no matches converting function %qD to type %q#T",
7913 OVL_NAME (overload), target_type);
7915 print_candidates (overload);
7917 return error_mark_node;
7919 else if (TREE_CHAIN (matches))
7921 /* There were too many matches. First check if they're all
7922 the same function. */
7923 tree match = NULL_TREE;
7925 fn = TREE_PURPOSE (matches);
7927 /* For multi-versioned functions, more than one match is just fine and
7928 decls_match will return false as they are different. */
7929 for (match = TREE_CHAIN (matches); match; match = TREE_CHAIN (match))
7930 if (!decls_match (fn, TREE_PURPOSE (match))
7931 && !targetm.target_option.function_versions
7932 (fn, TREE_PURPOSE (match)))
7933 break;
7935 if (match)
7937 if (complain & tf_error)
7939 error ("converting overloaded function %qD to type %q#T is ambiguous",
7940 OVL_NAME (overload), target_type);
7942 /* Since print_candidates expects the functions in the
7943 TREE_VALUE slot, we flip them here. */
7944 for (match = matches; match; match = TREE_CHAIN (match))
7945 TREE_VALUE (match) = TREE_PURPOSE (match);
7947 print_candidates (matches);
7950 return error_mark_node;
7954 /* Good, exactly one match. Now, convert it to the correct type. */
7955 fn = TREE_PURPOSE (matches);
7957 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
7958 && !(complain & tf_ptrmem_ok) && !flag_ms_extensions)
7960 static int explained;
7962 if (!(complain & tf_error))
7963 return error_mark_node;
7965 permerror (input_location, "assuming pointer to member %qD", fn);
7966 if (!explained)
7968 inform (input_location, "(a pointer to member can only be formed with %<&%E%>)", fn);
7969 explained = 1;
7973 /* If a pointer to a function that is multi-versioned is requested, the
7974 pointer to the dispatcher function is returned instead. This works
7975 well because indirectly calling the function will dispatch the right
7976 function version at run-time. */
7977 if (DECL_FUNCTION_VERSIONED (fn))
7979 fn = get_function_version_dispatcher (fn);
7980 if (fn == NULL)
7981 return error_mark_node;
7982 /* Mark all the versions corresponding to the dispatcher as used. */
7983 if (!(complain & tf_conv))
7984 mark_versions_used (fn);
7987 /* If we're doing overload resolution purely for the purpose of
7988 determining conversion sequences, we should not consider the
7989 function used. If this conversion sequence is selected, the
7990 function will be marked as used at this point. */
7991 if (!(complain & tf_conv))
7993 /* Make =delete work with SFINAE. */
7994 if (DECL_DELETED_FN (fn) && !(complain & tf_error))
7995 return error_mark_node;
7996 if (!mark_used (fn, complain) && !(complain & tf_error))
7997 return error_mark_node;
8000 /* We could not check access to member functions when this
8001 expression was originally created since we did not know at that
8002 time to which function the expression referred. */
8003 if (DECL_FUNCTION_MEMBER_P (fn))
8005 gcc_assert (access_path);
8006 perform_or_defer_access_check (access_path, fn, fn, complain);
8009 if (TYPE_PTRFN_P (target_type) || TYPE_PTRMEMFUNC_P (target_type))
8010 return cp_build_addr_expr (fn, complain);
8011 else
8013 /* The target must be a REFERENCE_TYPE. Above, cp_build_unary_op
8014 will mark the function as addressed, but here we must do it
8015 explicitly. */
8016 cxx_mark_addressable (fn);
8018 return fn;
8022 /* This function will instantiate the type of the expression given in
8023 RHS to match the type of LHSTYPE. If errors exist, then return
8024 error_mark_node. COMPLAIN is a bit mask. If TF_ERROR is set, then
8025 we complain on errors. If we are not complaining, never modify rhs,
8026 as overload resolution wants to try many possible instantiations, in
8027 the hope that at least one will work.
8029 For non-recursive calls, LHSTYPE should be a function, pointer to
8030 function, or a pointer to member function. */
8032 tree
8033 instantiate_type (tree lhstype, tree rhs, tsubst_flags_t complain)
8035 tsubst_flags_t complain_in = complain;
8036 tree access_path = NULL_TREE;
8038 complain &= ~tf_ptrmem_ok;
8040 if (lhstype == unknown_type_node)
8042 if (complain & tf_error)
8043 error ("not enough type information");
8044 return error_mark_node;
8047 if (TREE_TYPE (rhs) != NULL_TREE && ! (type_unknown_p (rhs)))
8049 tree fntype = non_reference (lhstype);
8050 if (same_type_p (fntype, TREE_TYPE (rhs)))
8051 return rhs;
8052 if (fnptr_conv_p (fntype, TREE_TYPE (rhs)))
8053 return rhs;
8054 if (flag_ms_extensions
8055 && TYPE_PTRMEMFUNC_P (fntype)
8056 && !TYPE_PTRMEMFUNC_P (TREE_TYPE (rhs)))
8057 /* Microsoft allows `A::f' to be resolved to a
8058 pointer-to-member. */
8060 else
8062 if (complain & tf_error)
8063 error ("cannot convert %qE from type %qT to type %qT",
8064 rhs, TREE_TYPE (rhs), fntype);
8065 return error_mark_node;
8069 if (BASELINK_P (rhs))
8071 access_path = BASELINK_ACCESS_BINFO (rhs);
8072 rhs = BASELINK_FUNCTIONS (rhs);
8075 /* If we are in a template, and have a NON_DEPENDENT_EXPR, we cannot
8076 deduce any type information. */
8077 if (TREE_CODE (rhs) == NON_DEPENDENT_EXPR)
8079 if (complain & tf_error)
8080 error ("not enough type information");
8081 return error_mark_node;
8084 /* If we instantiate a template, and it is a A ?: C expression
8085 with omitted B, look through the SAVE_EXPR. */
8086 if (TREE_CODE (rhs) == SAVE_EXPR)
8087 rhs = TREE_OPERAND (rhs, 0);
8089 /* There are only a few kinds of expressions that may have a type
8090 dependent on overload resolution. */
8091 gcc_assert (TREE_CODE (rhs) == ADDR_EXPR
8092 || TREE_CODE (rhs) == COMPONENT_REF
8093 || is_overloaded_fn (rhs)
8094 || (flag_ms_extensions && TREE_CODE (rhs) == FUNCTION_DECL));
8096 /* This should really only be used when attempting to distinguish
8097 what sort of a pointer to function we have. For now, any
8098 arithmetic operation which is not supported on pointers
8099 is rejected as an error. */
8101 switch (TREE_CODE (rhs))
8103 case COMPONENT_REF:
8105 tree member = TREE_OPERAND (rhs, 1);
8107 member = instantiate_type (lhstype, member, complain);
8108 if (member != error_mark_node
8109 && TREE_SIDE_EFFECTS (TREE_OPERAND (rhs, 0)))
8110 /* Do not lose object's side effects. */
8111 return build2 (COMPOUND_EXPR, TREE_TYPE (member),
8112 TREE_OPERAND (rhs, 0), member);
8113 return member;
8116 case OFFSET_REF:
8117 rhs = TREE_OPERAND (rhs, 1);
8118 if (BASELINK_P (rhs))
8119 return instantiate_type (lhstype, rhs, complain_in);
8121 /* This can happen if we are forming a pointer-to-member for a
8122 member template. */
8123 gcc_assert (TREE_CODE (rhs) == TEMPLATE_ID_EXPR);
8125 /* Fall through. */
8127 case TEMPLATE_ID_EXPR:
8129 tree fns = TREE_OPERAND (rhs, 0);
8130 tree args = TREE_OPERAND (rhs, 1);
8132 return
8133 resolve_address_of_overloaded_function (lhstype, fns, complain_in,
8134 /*template_only=*/true,
8135 args, access_path);
8138 case OVERLOAD:
8139 case FUNCTION_DECL:
8140 return
8141 resolve_address_of_overloaded_function (lhstype, rhs, complain_in,
8142 /*template_only=*/false,
8143 /*explicit_targs=*/NULL_TREE,
8144 access_path);
8146 case ADDR_EXPR:
8148 if (PTRMEM_OK_P (rhs))
8149 complain |= tf_ptrmem_ok;
8151 return instantiate_type (lhstype, TREE_OPERAND (rhs, 0), complain);
8154 case ERROR_MARK:
8155 return error_mark_node;
8157 default:
8158 gcc_unreachable ();
8160 return error_mark_node;
8163 /* Return the name of the virtual function pointer field
8164 (as an IDENTIFIER_NODE) for the given TYPE. Note that
8165 this may have to look back through base types to find the
8166 ultimate field name. (For single inheritance, these could
8167 all be the same name. Who knows for multiple inheritance). */
8169 static tree
8170 get_vfield_name (tree type)
8172 tree binfo, base_binfo;
8174 for (binfo = TYPE_BINFO (type);
8175 BINFO_N_BASE_BINFOS (binfo);
8176 binfo = base_binfo)
8178 base_binfo = BINFO_BASE_BINFO (binfo, 0);
8180 if (BINFO_VIRTUAL_P (base_binfo)
8181 || !TYPE_CONTAINS_VPTR_P (BINFO_TYPE (base_binfo)))
8182 break;
8185 type = BINFO_TYPE (binfo);
8186 tree ctor_name = constructor_name (type);
8187 char *buf = (char *) alloca (sizeof (VFIELD_NAME_FORMAT)
8188 + IDENTIFIER_LENGTH (ctor_name) + 2);
8189 sprintf (buf, VFIELD_NAME_FORMAT, IDENTIFIER_POINTER (ctor_name));
8190 return get_identifier (buf);
8193 void
8194 print_class_statistics (void)
8196 if (! GATHER_STATISTICS)
8197 return;
8199 fprintf (stderr, "convert_harshness = %d\n", n_convert_harshness);
8200 fprintf (stderr, "compute_conversion_costs = %d\n", n_compute_conversion_costs);
8201 if (n_vtables)
8203 fprintf (stderr, "vtables = %d; vtable searches = %d\n",
8204 n_vtables, n_vtable_searches);
8205 fprintf (stderr, "vtable entries = %d; vtable elems = %d\n",
8206 n_vtable_entries, n_vtable_elems);
8210 /* Build a dummy reference to ourselves so Derived::Base (and A::A) works,
8211 according to [class]:
8212 The class-name is also inserted
8213 into the scope of the class itself. For purposes of access checking,
8214 the inserted class name is treated as if it were a public member name. */
8216 void
8217 build_self_reference (void)
8219 tree name = DECL_NAME (TYPE_NAME (current_class_type));
8220 tree value = build_lang_decl (TYPE_DECL, name, current_class_type);
8222 DECL_NONLOCAL (value) = 1;
8223 DECL_CONTEXT (value) = current_class_type;
8224 DECL_ARTIFICIAL (value) = 1;
8225 SET_DECL_SELF_REFERENCE_P (value);
8226 set_underlying_type (value);
8228 if (processing_template_decl)
8229 value = push_template_decl (value);
8231 tree saved_cas = current_access_specifier;
8232 current_access_specifier = access_public_node;
8233 finish_member_declaration (value);
8234 current_access_specifier = saved_cas;
8237 /* Returns 1 if TYPE contains only padding bytes. */
8240 is_empty_class (tree type)
8242 if (type == error_mark_node)
8243 return 0;
8245 if (! CLASS_TYPE_P (type))
8246 return 0;
8248 return CLASSTYPE_EMPTY_P (type);
8251 /* Returns true if TYPE contains no actual data, just various
8252 possible combinations of empty classes and possibly a vptr. */
8254 bool
8255 is_really_empty_class (tree type)
8257 if (CLASS_TYPE_P (type))
8259 tree field;
8260 tree binfo;
8261 tree base_binfo;
8262 int i;
8264 /* CLASSTYPE_EMPTY_P isn't set properly until the class is actually laid
8265 out, but we'd like to be able to check this before then. */
8266 if (COMPLETE_TYPE_P (type) && is_empty_class (type))
8267 return true;
8269 for (binfo = TYPE_BINFO (type), i = 0;
8270 BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
8271 if (!is_really_empty_class (BINFO_TYPE (base_binfo)))
8272 return false;
8273 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
8274 if (TREE_CODE (field) == FIELD_DECL
8275 && !DECL_ARTIFICIAL (field)
8276 /* An unnamed bit-field is not a data member. */
8277 && (DECL_NAME (field) || !DECL_C_BIT_FIELD (field))
8278 && !is_really_empty_class (TREE_TYPE (field)))
8279 return false;
8280 return true;
8282 else if (TREE_CODE (type) == ARRAY_TYPE)
8283 return (integer_zerop (array_type_nelts_top (type))
8284 || is_really_empty_class (TREE_TYPE (type)));
8285 return false;
8288 /* Note that NAME was looked up while the current class was being
8289 defined and that the result of that lookup was DECL. */
8291 void
8292 maybe_note_name_used_in_class (tree name, tree decl)
8294 splay_tree names_used;
8296 /* If we're not defining a class, there's nothing to do. */
8297 if (!(innermost_scope_kind() == sk_class
8298 && TYPE_BEING_DEFINED (current_class_type)
8299 && !LAMBDA_TYPE_P (current_class_type)))
8300 return;
8302 /* If there's already a binding for this NAME, then we don't have
8303 anything to worry about. */
8304 if (lookup_member (current_class_type, name,
8305 /*protect=*/0, /*want_type=*/false, tf_warning_or_error))
8306 return;
8308 if (!current_class_stack[current_class_depth - 1].names_used)
8309 current_class_stack[current_class_depth - 1].names_used
8310 = splay_tree_new (splay_tree_compare_pointers, 0, 0);
8311 names_used = current_class_stack[current_class_depth - 1].names_used;
8313 splay_tree_insert (names_used,
8314 (splay_tree_key) name,
8315 (splay_tree_value) decl);
8318 /* Note that NAME was declared (as DECL) in the current class. Check
8319 to see that the declaration is valid. */
8321 void
8322 note_name_declared_in_class (tree name, tree decl)
8324 splay_tree names_used;
8325 splay_tree_node n;
8327 /* Look to see if we ever used this name. */
8328 names_used
8329 = current_class_stack[current_class_depth - 1].names_used;
8330 if (!names_used)
8331 return;
8332 /* The C language allows members to be declared with a type of the same
8333 name, and the C++ standard says this diagnostic is not required. So
8334 allow it in extern "C" blocks unless predantic is specified.
8335 Allow it in all cases if -ms-extensions is specified. */
8336 if ((!pedantic && current_lang_name == lang_name_c)
8337 || flag_ms_extensions)
8338 return;
8339 n = splay_tree_lookup (names_used, (splay_tree_key) name);
8340 if (n)
8342 /* [basic.scope.class]
8344 A name N used in a class S shall refer to the same declaration
8345 in its context and when re-evaluated in the completed scope of
8346 S. */
8347 permerror (input_location, "declaration of %q#D", decl);
8348 permerror (location_of ((tree) n->value),
8349 "changes meaning of %qD from %q#D",
8350 OVL_NAME (decl), (tree) n->value);
8354 /* Returns the VAR_DECL for the complete vtable associated with BINFO.
8355 Secondary vtables are merged with primary vtables; this function
8356 will return the VAR_DECL for the primary vtable. */
8358 tree
8359 get_vtbl_decl_for_binfo (tree binfo)
8361 tree decl;
8363 decl = BINFO_VTABLE (binfo);
8364 if (decl && TREE_CODE (decl) == POINTER_PLUS_EXPR)
8366 gcc_assert (TREE_CODE (TREE_OPERAND (decl, 0)) == ADDR_EXPR);
8367 decl = TREE_OPERAND (TREE_OPERAND (decl, 0), 0);
8369 if (decl)
8370 gcc_assert (VAR_P (decl));
8371 return decl;
8375 /* Returns the binfo for the primary base of BINFO. If the resulting
8376 BINFO is a virtual base, and it is inherited elsewhere in the
8377 hierarchy, then the returned binfo might not be the primary base of
8378 BINFO in the complete object. Check BINFO_PRIMARY_P or
8379 BINFO_LOST_PRIMARY_P to be sure. */
8381 static tree
8382 get_primary_binfo (tree binfo)
8384 tree primary_base;
8386 primary_base = CLASSTYPE_PRIMARY_BINFO (BINFO_TYPE (binfo));
8387 if (!primary_base)
8388 return NULL_TREE;
8390 return copied_binfo (primary_base, binfo);
8393 /* As above, but iterate until we reach the binfo that actually provides the
8394 vptr for BINFO. */
8396 static tree
8397 most_primary_binfo (tree binfo)
8399 tree b = binfo;
8400 while (CLASSTYPE_HAS_PRIMARY_BASE_P (BINFO_TYPE (b))
8401 && !BINFO_LOST_PRIMARY_P (b))
8403 tree primary_base = get_primary_binfo (b);
8404 gcc_assert (BINFO_PRIMARY_P (primary_base)
8405 && BINFO_INHERITANCE_CHAIN (primary_base) == b);
8406 b = primary_base;
8408 return b;
8411 /* Returns true if BINFO gets its vptr from a virtual base of the most derived
8412 type. Note that the virtual inheritance might be above or below BINFO in
8413 the hierarchy. */
8415 bool
8416 vptr_via_virtual_p (tree binfo)
8418 if (TYPE_P (binfo))
8419 binfo = TYPE_BINFO (binfo);
8420 tree primary = most_primary_binfo (binfo);
8421 /* Don't limit binfo_via_virtual, we want to return true when BINFO itself is
8422 a morally virtual base. */
8423 tree virt = binfo_via_virtual (primary, NULL_TREE);
8424 return virt != NULL_TREE;
8427 /* If INDENTED_P is zero, indent to INDENT. Return nonzero. */
8429 static int
8430 maybe_indent_hierarchy (FILE * stream, int indent, int indented_p)
8432 if (!indented_p)
8433 fprintf (stream, "%*s", indent, "");
8434 return 1;
8437 /* Dump the offsets of all the bases rooted at BINFO to STREAM.
8438 INDENT should be zero when called from the top level; it is
8439 incremented recursively. IGO indicates the next expected BINFO in
8440 inheritance graph ordering. */
8442 static tree
8443 dump_class_hierarchy_r (FILE *stream,
8444 dump_flags_t flags,
8445 tree binfo,
8446 tree igo,
8447 int indent)
8449 int indented = 0;
8450 tree base_binfo;
8451 int i;
8453 indented = maybe_indent_hierarchy (stream, indent, 0);
8454 fprintf (stream, "%s (0x" HOST_WIDE_INT_PRINT_HEX ") ",
8455 type_as_string (BINFO_TYPE (binfo), TFF_PLAIN_IDENTIFIER),
8456 (HOST_WIDE_INT) (uintptr_t) binfo);
8457 if (binfo != igo)
8459 fprintf (stream, "alternative-path\n");
8460 return igo;
8462 igo = TREE_CHAIN (binfo);
8464 fprintf (stream, HOST_WIDE_INT_PRINT_DEC,
8465 tree_to_shwi (BINFO_OFFSET (binfo)));
8466 if (is_empty_class (BINFO_TYPE (binfo)))
8467 fprintf (stream, " empty");
8468 else if (CLASSTYPE_NEARLY_EMPTY_P (BINFO_TYPE (binfo)))
8469 fprintf (stream, " nearly-empty");
8470 if (BINFO_VIRTUAL_P (binfo))
8471 fprintf (stream, " virtual");
8472 fprintf (stream, "\n");
8474 indented = 0;
8475 if (BINFO_PRIMARY_P (binfo))
8477 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
8478 fprintf (stream, " primary-for %s (0x" HOST_WIDE_INT_PRINT_HEX ")",
8479 type_as_string (BINFO_TYPE (BINFO_INHERITANCE_CHAIN (binfo)),
8480 TFF_PLAIN_IDENTIFIER),
8481 (HOST_WIDE_INT) (uintptr_t) BINFO_INHERITANCE_CHAIN (binfo));
8483 if (BINFO_LOST_PRIMARY_P (binfo))
8485 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
8486 fprintf (stream, " lost-primary");
8488 if (indented)
8489 fprintf (stream, "\n");
8491 if (!(flags & TDF_SLIM))
8493 int indented = 0;
8495 if (BINFO_SUBVTT_INDEX (binfo))
8497 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
8498 fprintf (stream, " subvttidx=%s",
8499 expr_as_string (BINFO_SUBVTT_INDEX (binfo),
8500 TFF_PLAIN_IDENTIFIER));
8502 if (BINFO_VPTR_INDEX (binfo))
8504 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
8505 fprintf (stream, " vptridx=%s",
8506 expr_as_string (BINFO_VPTR_INDEX (binfo),
8507 TFF_PLAIN_IDENTIFIER));
8509 if (BINFO_VPTR_FIELD (binfo))
8511 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
8512 fprintf (stream, " vbaseoffset=%s",
8513 expr_as_string (BINFO_VPTR_FIELD (binfo),
8514 TFF_PLAIN_IDENTIFIER));
8516 if (BINFO_VTABLE (binfo))
8518 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
8519 fprintf (stream, " vptr=%s",
8520 expr_as_string (BINFO_VTABLE (binfo),
8521 TFF_PLAIN_IDENTIFIER));
8524 if (indented)
8525 fprintf (stream, "\n");
8528 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
8529 igo = dump_class_hierarchy_r (stream, flags, base_binfo, igo, indent + 2);
8531 return igo;
8534 /* Dump the BINFO hierarchy for T. */
8536 static void
8537 dump_class_hierarchy_1 (FILE *stream, dump_flags_t flags, tree t)
8539 fprintf (stream, "Class %s\n", type_as_string (t, TFF_PLAIN_IDENTIFIER));
8540 fprintf (stream, " size=%lu align=%lu\n",
8541 (unsigned long)(tree_to_shwi (TYPE_SIZE (t)) / BITS_PER_UNIT),
8542 (unsigned long)(TYPE_ALIGN (t) / BITS_PER_UNIT));
8543 fprintf (stream, " base size=%lu base align=%lu\n",
8544 (unsigned long)(tree_to_shwi (TYPE_SIZE (CLASSTYPE_AS_BASE (t)))
8545 / BITS_PER_UNIT),
8546 (unsigned long)(TYPE_ALIGN (CLASSTYPE_AS_BASE (t))
8547 / BITS_PER_UNIT));
8548 dump_class_hierarchy_r (stream, flags, TYPE_BINFO (t), TYPE_BINFO (t), 0);
8549 fprintf (stream, "\n");
8552 /* Debug interface to hierarchy dumping. */
8554 void
8555 debug_class (tree t)
8557 dump_class_hierarchy_1 (stderr, TDF_SLIM, t);
8560 static void
8561 dump_class_hierarchy (tree t)
8563 dump_flags_t flags;
8564 if (FILE *stream = dump_begin (class_dump_id, &flags))
8566 dump_class_hierarchy_1 (stream, flags, t);
8567 dump_end (class_dump_id, stream);
8571 static void
8572 dump_array (FILE * stream, tree decl)
8574 tree value;
8575 unsigned HOST_WIDE_INT ix;
8576 HOST_WIDE_INT elt;
8577 tree size = TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (decl)));
8579 elt = (tree_to_shwi (TYPE_SIZE (TREE_TYPE (TREE_TYPE (decl))))
8580 / BITS_PER_UNIT);
8581 fprintf (stream, "%s:", decl_as_string (decl, TFF_PLAIN_IDENTIFIER));
8582 fprintf (stream, " %s entries",
8583 expr_as_string (size_binop (PLUS_EXPR, size, size_one_node),
8584 TFF_PLAIN_IDENTIFIER));
8585 fprintf (stream, "\n");
8587 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (DECL_INITIAL (decl)),
8588 ix, value)
8589 fprintf (stream, "%-4ld %s\n", (long)(ix * elt),
8590 expr_as_string (value, TFF_PLAIN_IDENTIFIER));
8593 static void
8594 dump_vtable (tree t, tree binfo, tree vtable)
8596 dump_flags_t flags;
8597 FILE *stream = dump_begin (class_dump_id, &flags);
8599 if (!stream)
8600 return;
8602 if (!(flags & TDF_SLIM))
8604 int ctor_vtbl_p = TYPE_BINFO (t) != binfo;
8606 fprintf (stream, "%s for %s",
8607 ctor_vtbl_p ? "Construction vtable" : "Vtable",
8608 type_as_string (BINFO_TYPE (binfo), TFF_PLAIN_IDENTIFIER));
8609 if (ctor_vtbl_p)
8611 if (!BINFO_VIRTUAL_P (binfo))
8612 fprintf (stream, " (0x" HOST_WIDE_INT_PRINT_HEX " instance)",
8613 (HOST_WIDE_INT) (uintptr_t) binfo);
8614 fprintf (stream, " in %s", type_as_string (t, TFF_PLAIN_IDENTIFIER));
8616 fprintf (stream, "\n");
8617 dump_array (stream, vtable);
8618 fprintf (stream, "\n");
8621 dump_end (class_dump_id, stream);
8624 static void
8625 dump_vtt (tree t, tree vtt)
8627 dump_flags_t flags;
8628 FILE *stream = dump_begin (class_dump_id, &flags);
8630 if (!stream)
8631 return;
8633 if (!(flags & TDF_SLIM))
8635 fprintf (stream, "VTT for %s\n",
8636 type_as_string (t, TFF_PLAIN_IDENTIFIER));
8637 dump_array (stream, vtt);
8638 fprintf (stream, "\n");
8641 dump_end (class_dump_id, stream);
8644 /* Dump a function or thunk and its thunkees. */
8646 static void
8647 dump_thunk (FILE *stream, int indent, tree thunk)
8649 static const char spaces[] = " ";
8650 tree name = DECL_NAME (thunk);
8651 tree thunks;
8653 fprintf (stream, "%.*s%p %s %s", indent, spaces,
8654 (void *)thunk,
8655 !DECL_THUNK_P (thunk) ? "function"
8656 : DECL_THIS_THUNK_P (thunk) ? "this-thunk" : "covariant-thunk",
8657 name ? IDENTIFIER_POINTER (name) : "<unset>");
8658 if (DECL_THUNK_P (thunk))
8660 HOST_WIDE_INT fixed_adjust = THUNK_FIXED_OFFSET (thunk);
8661 tree virtual_adjust = THUNK_VIRTUAL_OFFSET (thunk);
8663 fprintf (stream, " fixed=" HOST_WIDE_INT_PRINT_DEC, fixed_adjust);
8664 if (!virtual_adjust)
8665 /*NOP*/;
8666 else if (DECL_THIS_THUNK_P (thunk))
8667 fprintf (stream, " vcall=" HOST_WIDE_INT_PRINT_DEC,
8668 tree_to_shwi (virtual_adjust));
8669 else
8670 fprintf (stream, " vbase=" HOST_WIDE_INT_PRINT_DEC "(%s)",
8671 tree_to_shwi (BINFO_VPTR_FIELD (virtual_adjust)),
8672 type_as_string (BINFO_TYPE (virtual_adjust), TFF_SCOPE));
8673 if (THUNK_ALIAS (thunk))
8674 fprintf (stream, " alias to %p", (void *)THUNK_ALIAS (thunk));
8676 fprintf (stream, "\n");
8677 for (thunks = DECL_THUNKS (thunk); thunks; thunks = TREE_CHAIN (thunks))
8678 dump_thunk (stream, indent + 2, thunks);
8681 /* Dump the thunks for FN. */
8683 void
8684 debug_thunks (tree fn)
8686 dump_thunk (stderr, 0, fn);
8689 /* Virtual function table initialization. */
8691 /* Create all the necessary vtables for T and its base classes. */
8693 static void
8694 finish_vtbls (tree t)
8696 tree vbase;
8697 vec<constructor_elt, va_gc> *v = NULL;
8698 tree vtable = BINFO_VTABLE (TYPE_BINFO (t));
8700 /* We lay out the primary and secondary vtables in one contiguous
8701 vtable. The primary vtable is first, followed by the non-virtual
8702 secondary vtables in inheritance graph order. */
8703 accumulate_vtbl_inits (TYPE_BINFO (t), TYPE_BINFO (t), TYPE_BINFO (t),
8704 vtable, t, &v);
8706 /* Then come the virtual bases, also in inheritance graph order. */
8707 for (vbase = TYPE_BINFO (t); vbase; vbase = TREE_CHAIN (vbase))
8709 if (!BINFO_VIRTUAL_P (vbase))
8710 continue;
8711 accumulate_vtbl_inits (vbase, vbase, TYPE_BINFO (t), vtable, t, &v);
8714 if (BINFO_VTABLE (TYPE_BINFO (t)))
8715 initialize_vtable (TYPE_BINFO (t), v);
8718 /* Initialize the vtable for BINFO with the INITS. */
8720 static void
8721 initialize_vtable (tree binfo, vec<constructor_elt, va_gc> *inits)
8723 tree decl;
8725 layout_vtable_decl (binfo, vec_safe_length (inits));
8726 decl = get_vtbl_decl_for_binfo (binfo);
8727 initialize_artificial_var (decl, inits);
8728 dump_vtable (BINFO_TYPE (binfo), binfo, decl);
8731 /* Build the VTT (virtual table table) for T.
8732 A class requires a VTT if it has virtual bases.
8734 This holds
8735 1 - primary virtual pointer for complete object T
8736 2 - secondary VTTs for each direct non-virtual base of T which requires a
8738 3 - secondary virtual pointers for each direct or indirect base of T which
8739 has virtual bases or is reachable via a virtual path from T.
8740 4 - secondary VTTs for each direct or indirect virtual base of T.
8742 Secondary VTTs look like complete object VTTs without part 4. */
8744 static void
8745 build_vtt (tree t)
8747 tree type;
8748 tree vtt;
8749 tree index;
8750 vec<constructor_elt, va_gc> *inits;
8752 /* Build up the initializers for the VTT. */
8753 inits = NULL;
8754 index = size_zero_node;
8755 build_vtt_inits (TYPE_BINFO (t), t, &inits, &index);
8757 /* If we didn't need a VTT, we're done. */
8758 if (!inits)
8759 return;
8761 /* Figure out the type of the VTT. */
8762 type = build_array_of_n_type (const_ptr_type_node,
8763 inits->length ());
8765 /* Now, build the VTT object itself. */
8766 vtt = build_vtable (t, mangle_vtt_for_type (t), type);
8767 initialize_artificial_var (vtt, inits);
8768 /* Add the VTT to the vtables list. */
8769 DECL_CHAIN (vtt) = DECL_CHAIN (CLASSTYPE_VTABLES (t));
8770 DECL_CHAIN (CLASSTYPE_VTABLES (t)) = vtt;
8772 dump_vtt (t, vtt);
8775 /* When building a secondary VTT, BINFO_VTABLE is set to a TREE_LIST with
8776 PURPOSE the RTTI_BINFO, VALUE the real vtable pointer for this binfo,
8777 and CHAIN the vtable pointer for this binfo after construction is
8778 complete. VALUE can also be another BINFO, in which case we recurse. */
8780 static tree
8781 binfo_ctor_vtable (tree binfo)
8783 tree vt;
8785 while (1)
8787 vt = BINFO_VTABLE (binfo);
8788 if (TREE_CODE (vt) == TREE_LIST)
8789 vt = TREE_VALUE (vt);
8790 if (TREE_CODE (vt) == TREE_BINFO)
8791 binfo = vt;
8792 else
8793 break;
8796 return vt;
8799 /* Data for secondary VTT initialization. */
8800 struct secondary_vptr_vtt_init_data
8802 /* Is this the primary VTT? */
8803 bool top_level_p;
8805 /* Current index into the VTT. */
8806 tree index;
8808 /* Vector of initializers built up. */
8809 vec<constructor_elt, va_gc> *inits;
8811 /* The type being constructed by this secondary VTT. */
8812 tree type_being_constructed;
8815 /* Recursively build the VTT-initializer for BINFO (which is in the
8816 hierarchy dominated by T). INITS points to the end of the initializer
8817 list to date. INDEX is the VTT index where the next element will be
8818 replaced. Iff BINFO is the binfo for T, this is the top level VTT (i.e.
8819 not a subvtt for some base of T). When that is so, we emit the sub-VTTs
8820 for virtual bases of T. When it is not so, we build the constructor
8821 vtables for the BINFO-in-T variant. */
8823 static void
8824 build_vtt_inits (tree binfo, tree t, vec<constructor_elt, va_gc> **inits,
8825 tree *index)
8827 int i;
8828 tree b;
8829 tree init;
8830 secondary_vptr_vtt_init_data data;
8831 int top_level_p = SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), t);
8833 /* We only need VTTs for subobjects with virtual bases. */
8834 if (!CLASSTYPE_VBASECLASSES (BINFO_TYPE (binfo)))
8835 return;
8837 /* We need to use a construction vtable if this is not the primary
8838 VTT. */
8839 if (!top_level_p)
8841 build_ctor_vtbl_group (binfo, t);
8843 /* Record the offset in the VTT where this sub-VTT can be found. */
8844 BINFO_SUBVTT_INDEX (binfo) = *index;
8847 /* Add the address of the primary vtable for the complete object. */
8848 init = binfo_ctor_vtable (binfo);
8849 CONSTRUCTOR_APPEND_ELT (*inits, NULL_TREE, init);
8850 if (top_level_p)
8852 gcc_assert (!BINFO_VPTR_INDEX (binfo));
8853 BINFO_VPTR_INDEX (binfo) = *index;
8855 *index = size_binop (PLUS_EXPR, *index, TYPE_SIZE_UNIT (ptr_type_node));
8857 /* Recursively add the secondary VTTs for non-virtual bases. */
8858 for (i = 0; BINFO_BASE_ITERATE (binfo, i, b); ++i)
8859 if (!BINFO_VIRTUAL_P (b))
8860 build_vtt_inits (b, t, inits, index);
8862 /* Add secondary virtual pointers for all subobjects of BINFO with
8863 either virtual bases or reachable along a virtual path, except
8864 subobjects that are non-virtual primary bases. */
8865 data.top_level_p = top_level_p;
8866 data.index = *index;
8867 data.inits = *inits;
8868 data.type_being_constructed = BINFO_TYPE (binfo);
8870 dfs_walk_once (binfo, dfs_build_secondary_vptr_vtt_inits, NULL, &data);
8872 *index = data.index;
8874 /* data.inits might have grown as we added secondary virtual pointers.
8875 Make sure our caller knows about the new vector. */
8876 *inits = data.inits;
8878 if (top_level_p)
8879 /* Add the secondary VTTs for virtual bases in inheritance graph
8880 order. */
8881 for (b = TYPE_BINFO (BINFO_TYPE (binfo)); b; b = TREE_CHAIN (b))
8883 if (!BINFO_VIRTUAL_P (b))
8884 continue;
8886 build_vtt_inits (b, t, inits, index);
8888 else
8889 /* Remove the ctor vtables we created. */
8890 dfs_walk_all (binfo, dfs_fixup_binfo_vtbls, NULL, binfo);
8893 /* Called from build_vtt_inits via dfs_walk. BINFO is the binfo for the base
8894 in most derived. DATA is a SECONDARY_VPTR_VTT_INIT_DATA structure. */
8896 static tree
8897 dfs_build_secondary_vptr_vtt_inits (tree binfo, void *data_)
8899 secondary_vptr_vtt_init_data *data = (secondary_vptr_vtt_init_data *)data_;
8901 /* We don't care about bases that don't have vtables. */
8902 if (!TYPE_VFIELD (BINFO_TYPE (binfo)))
8903 return dfs_skip_bases;
8905 /* We're only interested in proper subobjects of the type being
8906 constructed. */
8907 if (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), data->type_being_constructed))
8908 return NULL_TREE;
8910 /* We're only interested in bases with virtual bases or reachable
8911 via a virtual path from the type being constructed. */
8912 if (!(CLASSTYPE_VBASECLASSES (BINFO_TYPE (binfo))
8913 || binfo_via_virtual (binfo, data->type_being_constructed)))
8914 return dfs_skip_bases;
8916 /* We're not interested in non-virtual primary bases. */
8917 if (!BINFO_VIRTUAL_P (binfo) && BINFO_PRIMARY_P (binfo))
8918 return NULL_TREE;
8920 /* Record the index where this secondary vptr can be found. */
8921 if (data->top_level_p)
8923 gcc_assert (!BINFO_VPTR_INDEX (binfo));
8924 BINFO_VPTR_INDEX (binfo) = data->index;
8926 if (BINFO_VIRTUAL_P (binfo))
8928 /* It's a primary virtual base, and this is not a
8929 construction vtable. Find the base this is primary of in
8930 the inheritance graph, and use that base's vtable
8931 now. */
8932 while (BINFO_PRIMARY_P (binfo))
8933 binfo = BINFO_INHERITANCE_CHAIN (binfo);
8937 /* Add the initializer for the secondary vptr itself. */
8938 CONSTRUCTOR_APPEND_ELT (data->inits, NULL_TREE, binfo_ctor_vtable (binfo));
8940 /* Advance the vtt index. */
8941 data->index = size_binop (PLUS_EXPR, data->index,
8942 TYPE_SIZE_UNIT (ptr_type_node));
8944 return NULL_TREE;
8947 /* Called from build_vtt_inits via dfs_walk. After building
8948 constructor vtables and generating the sub-vtt from them, we need
8949 to restore the BINFO_VTABLES that were scribbled on. DATA is the
8950 binfo of the base whose sub vtt was generated. */
8952 static tree
8953 dfs_fixup_binfo_vtbls (tree binfo, void* data)
8955 tree vtable = BINFO_VTABLE (binfo);
8957 if (!TYPE_CONTAINS_VPTR_P (BINFO_TYPE (binfo)))
8958 /* If this class has no vtable, none of its bases do. */
8959 return dfs_skip_bases;
8961 if (!vtable)
8962 /* This might be a primary base, so have no vtable in this
8963 hierarchy. */
8964 return NULL_TREE;
8966 /* If we scribbled the construction vtable vptr into BINFO, clear it
8967 out now. */
8968 if (TREE_CODE (vtable) == TREE_LIST
8969 && (TREE_PURPOSE (vtable) == (tree) data))
8970 BINFO_VTABLE (binfo) = TREE_CHAIN (vtable);
8972 return NULL_TREE;
8975 /* Build the construction vtable group for BINFO which is in the
8976 hierarchy dominated by T. */
8978 static void
8979 build_ctor_vtbl_group (tree binfo, tree t)
8981 tree type;
8982 tree vtbl;
8983 tree id;
8984 tree vbase;
8985 vec<constructor_elt, va_gc> *v;
8987 /* See if we've already created this construction vtable group. */
8988 id = mangle_ctor_vtbl_for_type (t, binfo);
8989 if (IDENTIFIER_GLOBAL_VALUE (id))
8990 return;
8992 gcc_assert (!SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), t));
8993 /* Build a version of VTBL (with the wrong type) for use in
8994 constructing the addresses of secondary vtables in the
8995 construction vtable group. */
8996 vtbl = build_vtable (t, id, ptr_type_node);
8997 DECL_CONSTRUCTION_VTABLE_P (vtbl) = 1;
8998 /* Don't export construction vtables from shared libraries. Even on
8999 targets that don't support hidden visibility, this tells
9000 can_refer_decl_in_current_unit_p not to assume that it's safe to
9001 access from a different compilation unit (bz 54314). */
9002 DECL_VISIBILITY (vtbl) = VISIBILITY_HIDDEN;
9003 DECL_VISIBILITY_SPECIFIED (vtbl) = true;
9005 v = NULL;
9006 accumulate_vtbl_inits (binfo, TYPE_BINFO (TREE_TYPE (binfo)),
9007 binfo, vtbl, t, &v);
9009 /* Add the vtables for each of our virtual bases using the vbase in T
9010 binfo. */
9011 for (vbase = TYPE_BINFO (BINFO_TYPE (binfo));
9012 vbase;
9013 vbase = TREE_CHAIN (vbase))
9015 tree b;
9017 if (!BINFO_VIRTUAL_P (vbase))
9018 continue;
9019 b = copied_binfo (vbase, binfo);
9021 accumulate_vtbl_inits (b, vbase, binfo, vtbl, t, &v);
9024 /* Figure out the type of the construction vtable. */
9025 type = build_array_of_n_type (vtable_entry_type, v->length ());
9026 layout_type (type);
9027 TREE_TYPE (vtbl) = type;
9028 DECL_SIZE (vtbl) = DECL_SIZE_UNIT (vtbl) = NULL_TREE;
9029 layout_decl (vtbl, 0);
9031 /* Initialize the construction vtable. */
9032 CLASSTYPE_VTABLES (t) = chainon (CLASSTYPE_VTABLES (t), vtbl);
9033 initialize_artificial_var (vtbl, v);
9034 dump_vtable (t, binfo, vtbl);
9037 /* Add the vtbl initializers for BINFO (and its bases other than
9038 non-virtual primaries) to the list of INITS. BINFO is in the
9039 hierarchy dominated by T. RTTI_BINFO is the binfo within T of
9040 the constructor the vtbl inits should be accumulated for. (If this
9041 is the complete object vtbl then RTTI_BINFO will be TYPE_BINFO (T).)
9042 ORIG_BINFO is the binfo for this object within BINFO_TYPE (RTTI_BINFO).
9043 BINFO is the active base equivalent of ORIG_BINFO in the inheritance
9044 graph of T. Both BINFO and ORIG_BINFO will have the same BINFO_TYPE,
9045 but are not necessarily the same in terms of layout. */
9047 static void
9048 accumulate_vtbl_inits (tree binfo,
9049 tree orig_binfo,
9050 tree rtti_binfo,
9051 tree vtbl,
9052 tree t,
9053 vec<constructor_elt, va_gc> **inits)
9055 int i;
9056 tree base_binfo;
9057 int ctor_vtbl_p = !SAME_BINFO_TYPE_P (BINFO_TYPE (rtti_binfo), t);
9059 gcc_assert (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), BINFO_TYPE (orig_binfo)));
9061 /* If it doesn't have a vptr, we don't do anything. */
9062 if (!TYPE_CONTAINS_VPTR_P (BINFO_TYPE (binfo)))
9063 return;
9065 /* If we're building a construction vtable, we're not interested in
9066 subobjects that don't require construction vtables. */
9067 if (ctor_vtbl_p
9068 && !CLASSTYPE_VBASECLASSES (BINFO_TYPE (binfo))
9069 && !binfo_via_virtual (orig_binfo, BINFO_TYPE (rtti_binfo)))
9070 return;
9072 /* Build the initializers for the BINFO-in-T vtable. */
9073 dfs_accumulate_vtbl_inits (binfo, orig_binfo, rtti_binfo, vtbl, t, inits);
9075 /* Walk the BINFO and its bases. We walk in preorder so that as we
9076 initialize each vtable we can figure out at what offset the
9077 secondary vtable lies from the primary vtable. We can't use
9078 dfs_walk here because we need to iterate through bases of BINFO
9079 and RTTI_BINFO simultaneously. */
9080 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
9082 /* Skip virtual bases. */
9083 if (BINFO_VIRTUAL_P (base_binfo))
9084 continue;
9085 accumulate_vtbl_inits (base_binfo,
9086 BINFO_BASE_BINFO (orig_binfo, i),
9087 rtti_binfo, vtbl, t,
9088 inits);
9092 /* Called from accumulate_vtbl_inits. Adds the initializers for the
9093 BINFO vtable to L. */
9095 static void
9096 dfs_accumulate_vtbl_inits (tree binfo,
9097 tree orig_binfo,
9098 tree rtti_binfo,
9099 tree orig_vtbl,
9100 tree t,
9101 vec<constructor_elt, va_gc> **l)
9103 tree vtbl = NULL_TREE;
9104 int ctor_vtbl_p = !SAME_BINFO_TYPE_P (BINFO_TYPE (rtti_binfo), t);
9105 int n_inits;
9107 if (ctor_vtbl_p
9108 && BINFO_VIRTUAL_P (orig_binfo) && BINFO_PRIMARY_P (orig_binfo))
9110 /* In the hierarchy of BINFO_TYPE (RTTI_BINFO), this is a
9111 primary virtual base. If it is not the same primary in
9112 the hierarchy of T, we'll need to generate a ctor vtable
9113 for it, to place at its location in T. If it is the same
9114 primary, we still need a VTT entry for the vtable, but it
9115 should point to the ctor vtable for the base it is a
9116 primary for within the sub-hierarchy of RTTI_BINFO.
9118 There are three possible cases:
9120 1) We are in the same place.
9121 2) We are a primary base within a lost primary virtual base of
9122 RTTI_BINFO.
9123 3) We are primary to something not a base of RTTI_BINFO. */
9125 tree b;
9126 tree last = NULL_TREE;
9128 /* First, look through the bases we are primary to for RTTI_BINFO
9129 or a virtual base. */
9130 b = binfo;
9131 while (BINFO_PRIMARY_P (b))
9133 b = BINFO_INHERITANCE_CHAIN (b);
9134 last = b;
9135 if (BINFO_VIRTUAL_P (b) || b == rtti_binfo)
9136 goto found;
9138 /* If we run out of primary links, keep looking down our
9139 inheritance chain; we might be an indirect primary. */
9140 for (b = last; b; b = BINFO_INHERITANCE_CHAIN (b))
9141 if (BINFO_VIRTUAL_P (b) || b == rtti_binfo)
9142 break;
9143 found:
9145 /* If we found RTTI_BINFO, this is case 1. If we found a virtual
9146 base B and it is a base of RTTI_BINFO, this is case 2. In
9147 either case, we share our vtable with LAST, i.e. the
9148 derived-most base within B of which we are a primary. */
9149 if (b == rtti_binfo
9150 || (b && binfo_for_vbase (BINFO_TYPE (b), BINFO_TYPE (rtti_binfo))))
9151 /* Just set our BINFO_VTABLE to point to LAST, as we may not have
9152 set LAST's BINFO_VTABLE yet. We'll extract the actual vptr in
9153 binfo_ctor_vtable after everything's been set up. */
9154 vtbl = last;
9156 /* Otherwise, this is case 3 and we get our own. */
9158 else if (!BINFO_NEW_VTABLE_MARKED (orig_binfo))
9159 return;
9161 n_inits = vec_safe_length (*l);
9163 if (!vtbl)
9165 tree index;
9166 int non_fn_entries;
9168 /* Add the initializer for this vtable. */
9169 build_vtbl_initializer (binfo, orig_binfo, t, rtti_binfo,
9170 &non_fn_entries, l);
9172 /* Figure out the position to which the VPTR should point. */
9173 vtbl = build1 (ADDR_EXPR, vtbl_ptr_type_node, orig_vtbl);
9174 index = size_binop (MULT_EXPR,
9175 TYPE_SIZE_UNIT (vtable_entry_type),
9176 size_int (non_fn_entries + n_inits));
9177 vtbl = fold_build_pointer_plus (vtbl, index);
9180 if (ctor_vtbl_p)
9181 /* For a construction vtable, we can't overwrite BINFO_VTABLE.
9182 So, we make a TREE_LIST. Later, dfs_fixup_binfo_vtbls will
9183 straighten this out. */
9184 BINFO_VTABLE (binfo) = tree_cons (rtti_binfo, vtbl, BINFO_VTABLE (binfo));
9185 else if (BINFO_PRIMARY_P (binfo) && BINFO_VIRTUAL_P (binfo))
9186 /* Throw away any unneeded intializers. */
9187 (*l)->truncate (n_inits);
9188 else
9189 /* For an ordinary vtable, set BINFO_VTABLE. */
9190 BINFO_VTABLE (binfo) = vtbl;
9193 static GTY(()) tree abort_fndecl_addr;
9194 static GTY(()) tree dvirt_fn;
9196 /* Construct the initializer for BINFO's virtual function table. BINFO
9197 is part of the hierarchy dominated by T. If we're building a
9198 construction vtable, the ORIG_BINFO is the binfo we should use to
9199 find the actual function pointers to put in the vtable - but they
9200 can be overridden on the path to most-derived in the graph that
9201 ORIG_BINFO belongs. Otherwise,
9202 ORIG_BINFO should be the same as BINFO. The RTTI_BINFO is the
9203 BINFO that should be indicated by the RTTI information in the
9204 vtable; it will be a base class of T, rather than T itself, if we
9205 are building a construction vtable.
9207 The value returned is a TREE_LIST suitable for wrapping in a
9208 CONSTRUCTOR to use as the DECL_INITIAL for a vtable. If
9209 NON_FN_ENTRIES_P is not NULL, *NON_FN_ENTRIES_P is set to the
9210 number of non-function entries in the vtable.
9212 It might seem that this function should never be called with a
9213 BINFO for which BINFO_PRIMARY_P holds, the vtable for such a
9214 base is always subsumed by a derived class vtable. However, when
9215 we are building construction vtables, we do build vtables for
9216 primary bases; we need these while the primary base is being
9217 constructed. */
9219 static void
9220 build_vtbl_initializer (tree binfo,
9221 tree orig_binfo,
9222 tree t,
9223 tree rtti_binfo,
9224 int* non_fn_entries_p,
9225 vec<constructor_elt, va_gc> **inits)
9227 tree v;
9228 vtbl_init_data vid;
9229 unsigned ix, jx;
9230 tree vbinfo;
9231 vec<tree, va_gc> *vbases;
9232 constructor_elt *e;
9234 /* Initialize VID. */
9235 memset (&vid, 0, sizeof (vid));
9236 vid.binfo = binfo;
9237 vid.derived = t;
9238 vid.rtti_binfo = rtti_binfo;
9239 vid.primary_vtbl_p = SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), t);
9240 vid.ctor_vtbl_p = !SAME_BINFO_TYPE_P (BINFO_TYPE (rtti_binfo), t);
9241 vid.generate_vcall_entries = true;
9242 /* The first vbase or vcall offset is at index -3 in the vtable. */
9243 vid.index = ssize_int(-3 * TARGET_VTABLE_DATA_ENTRY_DISTANCE);
9245 /* Add entries to the vtable for RTTI. */
9246 build_rtti_vtbl_entries (binfo, &vid);
9248 /* Create an array for keeping track of the functions we've
9249 processed. When we see multiple functions with the same
9250 signature, we share the vcall offsets. */
9251 vec_alloc (vid.fns, 32);
9252 /* Add the vcall and vbase offset entries. */
9253 build_vcall_and_vbase_vtbl_entries (binfo, &vid);
9255 /* Clear BINFO_VTABLE_PATH_MARKED; it's set by
9256 build_vbase_offset_vtbl_entries. */
9257 for (vbases = CLASSTYPE_VBASECLASSES (t), ix = 0;
9258 vec_safe_iterate (vbases, ix, &vbinfo); ix++)
9259 BINFO_VTABLE_PATH_MARKED (vbinfo) = 0;
9261 /* If the target requires padding between data entries, add that now. */
9262 if (TARGET_VTABLE_DATA_ENTRY_DISTANCE > 1)
9264 int n_entries = vec_safe_length (vid.inits);
9266 vec_safe_grow (vid.inits, TARGET_VTABLE_DATA_ENTRY_DISTANCE * n_entries);
9268 /* Move data entries into their new positions and add padding
9269 after the new positions. Iterate backwards so we don't
9270 overwrite entries that we would need to process later. */
9271 for (ix = n_entries - 1;
9272 vid.inits->iterate (ix, &e);
9273 ix--)
9275 int j;
9276 int new_position = (TARGET_VTABLE_DATA_ENTRY_DISTANCE * ix
9277 + (TARGET_VTABLE_DATA_ENTRY_DISTANCE - 1));
9279 (*vid.inits)[new_position] = *e;
9281 for (j = 1; j < TARGET_VTABLE_DATA_ENTRY_DISTANCE; ++j)
9283 constructor_elt *f = &(*vid.inits)[new_position - j];
9284 f->index = NULL_TREE;
9285 f->value = build1 (NOP_EXPR, vtable_entry_type,
9286 null_pointer_node);
9291 if (non_fn_entries_p)
9292 *non_fn_entries_p = vec_safe_length (vid.inits);
9294 /* The initializers for virtual functions were built up in reverse
9295 order. Straighten them out and add them to the running list in one
9296 step. */
9297 jx = vec_safe_length (*inits);
9298 vec_safe_grow (*inits, jx + vid.inits->length ());
9300 for (ix = vid.inits->length () - 1;
9301 vid.inits->iterate (ix, &e);
9302 ix--, jx++)
9303 (**inits)[jx] = *e;
9305 /* Go through all the ordinary virtual functions, building up
9306 initializers. */
9307 for (v = BINFO_VIRTUALS (orig_binfo); v; v = TREE_CHAIN (v))
9309 tree delta;
9310 tree vcall_index;
9311 tree fn, fn_original;
9312 tree init = NULL_TREE;
9314 fn = BV_FN (v);
9315 fn_original = fn;
9316 if (DECL_THUNK_P (fn))
9318 if (!DECL_NAME (fn))
9319 finish_thunk (fn);
9320 if (THUNK_ALIAS (fn))
9322 fn = THUNK_ALIAS (fn);
9323 BV_FN (v) = fn;
9325 fn_original = THUNK_TARGET (fn);
9328 /* If the only definition of this function signature along our
9329 primary base chain is from a lost primary, this vtable slot will
9330 never be used, so just zero it out. This is important to avoid
9331 requiring extra thunks which cannot be generated with the function.
9333 We first check this in update_vtable_entry_for_fn, so we handle
9334 restored primary bases properly; we also need to do it here so we
9335 zero out unused slots in ctor vtables, rather than filling them
9336 with erroneous values (though harmless, apart from relocation
9337 costs). */
9338 if (BV_LOST_PRIMARY (v))
9339 init = size_zero_node;
9341 if (! init)
9343 /* Pull the offset for `this', and the function to call, out of
9344 the list. */
9345 delta = BV_DELTA (v);
9346 vcall_index = BV_VCALL_INDEX (v);
9348 gcc_assert (TREE_CODE (delta) == INTEGER_CST);
9349 gcc_assert (TREE_CODE (fn) == FUNCTION_DECL);
9351 /* You can't call an abstract virtual function; it's abstract.
9352 So, we replace these functions with __pure_virtual. */
9353 if (DECL_PURE_VIRTUAL_P (fn_original))
9355 fn = abort_fndecl;
9356 if (!TARGET_VTABLE_USES_DESCRIPTORS)
9358 if (abort_fndecl_addr == NULL)
9359 abort_fndecl_addr
9360 = fold_convert (vfunc_ptr_type_node,
9361 build_fold_addr_expr (fn));
9362 init = abort_fndecl_addr;
9365 /* Likewise for deleted virtuals. */
9366 else if (DECL_DELETED_FN (fn_original))
9368 if (!dvirt_fn)
9370 tree name = get_identifier ("__cxa_deleted_virtual");
9371 dvirt_fn = IDENTIFIER_GLOBAL_VALUE (name);
9372 if (!dvirt_fn)
9373 dvirt_fn = push_library_fn
9374 (name,
9375 build_function_type_list (void_type_node, NULL_TREE),
9376 NULL_TREE, ECF_NORETURN | ECF_COLD);
9378 fn = dvirt_fn;
9379 if (!TARGET_VTABLE_USES_DESCRIPTORS)
9380 init = fold_convert (vfunc_ptr_type_node,
9381 build_fold_addr_expr (fn));
9383 else
9385 if (!integer_zerop (delta) || vcall_index)
9387 fn = make_thunk (fn, /*this_adjusting=*/1,
9388 delta, vcall_index);
9389 if (!DECL_NAME (fn))
9390 finish_thunk (fn);
9392 /* Take the address of the function, considering it to be of an
9393 appropriate generic type. */
9394 if (!TARGET_VTABLE_USES_DESCRIPTORS)
9395 init = fold_convert (vfunc_ptr_type_node,
9396 build_fold_addr_expr (fn));
9397 /* Don't refer to a virtual destructor from a constructor
9398 vtable or a vtable for an abstract class, since destroying
9399 an object under construction is undefined behavior and we
9400 don't want it to be considered a candidate for speculative
9401 devirtualization. But do create the thunk for ABI
9402 compliance. */
9403 if (DECL_DESTRUCTOR_P (fn_original)
9404 && (CLASSTYPE_PURE_VIRTUALS (DECL_CONTEXT (fn_original))
9405 || orig_binfo != binfo))
9406 init = size_zero_node;
9410 /* And add it to the chain of initializers. */
9411 if (TARGET_VTABLE_USES_DESCRIPTORS)
9413 int i;
9414 if (init == size_zero_node)
9415 for (i = 0; i < TARGET_VTABLE_USES_DESCRIPTORS; ++i)
9416 CONSTRUCTOR_APPEND_ELT (*inits, NULL_TREE, init);
9417 else
9418 for (i = 0; i < TARGET_VTABLE_USES_DESCRIPTORS; ++i)
9420 tree fdesc = build2 (FDESC_EXPR, vfunc_ptr_type_node,
9421 fn, build_int_cst (NULL_TREE, i));
9422 TREE_CONSTANT (fdesc) = 1;
9424 CONSTRUCTOR_APPEND_ELT (*inits, NULL_TREE, fdesc);
9427 else
9428 CONSTRUCTOR_APPEND_ELT (*inits, NULL_TREE, init);
9432 /* Adds to vid->inits the initializers for the vbase and vcall
9433 offsets in BINFO, which is in the hierarchy dominated by T. */
9435 static void
9436 build_vcall_and_vbase_vtbl_entries (tree binfo, vtbl_init_data* vid)
9438 tree b;
9440 /* If this is a derived class, we must first create entries
9441 corresponding to the primary base class. */
9442 b = get_primary_binfo (binfo);
9443 if (b)
9444 build_vcall_and_vbase_vtbl_entries (b, vid);
9446 /* Add the vbase entries for this base. */
9447 build_vbase_offset_vtbl_entries (binfo, vid);
9448 /* Add the vcall entries for this base. */
9449 build_vcall_offset_vtbl_entries (binfo, vid);
9452 /* Returns the initializers for the vbase offset entries in the vtable
9453 for BINFO (which is part of the class hierarchy dominated by T), in
9454 reverse order. VBASE_OFFSET_INDEX gives the vtable index
9455 where the next vbase offset will go. */
9457 static void
9458 build_vbase_offset_vtbl_entries (tree binfo, vtbl_init_data* vid)
9460 tree vbase;
9461 tree t;
9462 tree non_primary_binfo;
9464 /* If there are no virtual baseclasses, then there is nothing to
9465 do. */
9466 if (!CLASSTYPE_VBASECLASSES (BINFO_TYPE (binfo)))
9467 return;
9469 t = vid->derived;
9471 /* We might be a primary base class. Go up the inheritance hierarchy
9472 until we find the most derived class of which we are a primary base:
9473 it is the offset of that which we need to use. */
9474 non_primary_binfo = binfo;
9475 while (BINFO_INHERITANCE_CHAIN (non_primary_binfo))
9477 tree b;
9479 /* If we have reached a virtual base, then it must be a primary
9480 base (possibly multi-level) of vid->binfo, or we wouldn't
9481 have called build_vcall_and_vbase_vtbl_entries for it. But it
9482 might be a lost primary, so just skip down to vid->binfo. */
9483 if (BINFO_VIRTUAL_P (non_primary_binfo))
9485 non_primary_binfo = vid->binfo;
9486 break;
9489 b = BINFO_INHERITANCE_CHAIN (non_primary_binfo);
9490 if (get_primary_binfo (b) != non_primary_binfo)
9491 break;
9492 non_primary_binfo = b;
9495 /* Go through the virtual bases, adding the offsets. */
9496 for (vbase = TYPE_BINFO (BINFO_TYPE (binfo));
9497 vbase;
9498 vbase = TREE_CHAIN (vbase))
9500 tree b;
9501 tree delta;
9503 if (!BINFO_VIRTUAL_P (vbase))
9504 continue;
9506 /* Find the instance of this virtual base in the complete
9507 object. */
9508 b = copied_binfo (vbase, binfo);
9510 /* If we've already got an offset for this virtual base, we
9511 don't need another one. */
9512 if (BINFO_VTABLE_PATH_MARKED (b))
9513 continue;
9514 BINFO_VTABLE_PATH_MARKED (b) = 1;
9516 /* Figure out where we can find this vbase offset. */
9517 delta = size_binop (MULT_EXPR,
9518 vid->index,
9519 fold_convert (ssizetype,
9520 TYPE_SIZE_UNIT (vtable_entry_type)));
9521 if (vid->primary_vtbl_p)
9522 BINFO_VPTR_FIELD (b) = delta;
9524 if (binfo != TYPE_BINFO (t))
9525 /* The vbase offset had better be the same. */
9526 gcc_assert (tree_int_cst_equal (delta, BINFO_VPTR_FIELD (vbase)));
9528 /* The next vbase will come at a more negative offset. */
9529 vid->index = size_binop (MINUS_EXPR, vid->index,
9530 ssize_int (TARGET_VTABLE_DATA_ENTRY_DISTANCE));
9532 /* The initializer is the delta from BINFO to this virtual base.
9533 The vbase offsets go in reverse inheritance-graph order, and
9534 we are walking in inheritance graph order so these end up in
9535 the right order. */
9536 delta = size_diffop_loc (input_location,
9537 BINFO_OFFSET (b), BINFO_OFFSET (non_primary_binfo));
9539 CONSTRUCTOR_APPEND_ELT (vid->inits, NULL_TREE,
9540 fold_build1_loc (input_location, NOP_EXPR,
9541 vtable_entry_type, delta));
9545 /* Adds the initializers for the vcall offset entries in the vtable
9546 for BINFO (which is part of the class hierarchy dominated by VID->DERIVED)
9547 to VID->INITS. */
9549 static void
9550 build_vcall_offset_vtbl_entries (tree binfo, vtbl_init_data* vid)
9552 /* We only need these entries if this base is a virtual base. We
9553 compute the indices -- but do not add to the vtable -- when
9554 building the main vtable for a class. */
9555 if (binfo == TYPE_BINFO (vid->derived)
9556 || (BINFO_VIRTUAL_P (binfo)
9557 /* If BINFO is RTTI_BINFO, then (since BINFO does not
9558 correspond to VID->DERIVED), we are building a primary
9559 construction virtual table. Since this is a primary
9560 virtual table, we do not need the vcall offsets for
9561 BINFO. */
9562 && binfo != vid->rtti_binfo))
9564 /* We need a vcall offset for each of the virtual functions in this
9565 vtable. For example:
9567 class A { virtual void f (); };
9568 class B1 : virtual public A { virtual void f (); };
9569 class B2 : virtual public A { virtual void f (); };
9570 class C: public B1, public B2 { virtual void f (); };
9572 A C object has a primary base of B1, which has a primary base of A. A
9573 C also has a secondary base of B2, which no longer has a primary base
9574 of A. So the B2-in-C construction vtable needs a secondary vtable for
9575 A, which will adjust the A* to a B2* to call f. We have no way of
9576 knowing what (or even whether) this offset will be when we define B2,
9577 so we store this "vcall offset" in the A sub-vtable and look it up in
9578 a "virtual thunk" for B2::f.
9580 We need entries for all the functions in our primary vtable and
9581 in our non-virtual bases' secondary vtables. */
9582 vid->vbase = binfo;
9583 /* If we are just computing the vcall indices -- but do not need
9584 the actual entries -- not that. */
9585 if (!BINFO_VIRTUAL_P (binfo))
9586 vid->generate_vcall_entries = false;
9587 /* Now, walk through the non-virtual bases, adding vcall offsets. */
9588 add_vcall_offset_vtbl_entries_r (binfo, vid);
9592 /* Build vcall offsets, starting with those for BINFO. */
9594 static void
9595 add_vcall_offset_vtbl_entries_r (tree binfo, vtbl_init_data* vid)
9597 int i;
9598 tree primary_binfo;
9599 tree base_binfo;
9601 /* Don't walk into virtual bases -- except, of course, for the
9602 virtual base for which we are building vcall offsets. Any
9603 primary virtual base will have already had its offsets generated
9604 through the recursion in build_vcall_and_vbase_vtbl_entries. */
9605 if (BINFO_VIRTUAL_P (binfo) && vid->vbase != binfo)
9606 return;
9608 /* If BINFO has a primary base, process it first. */
9609 primary_binfo = get_primary_binfo (binfo);
9610 if (primary_binfo)
9611 add_vcall_offset_vtbl_entries_r (primary_binfo, vid);
9613 /* Add BINFO itself to the list. */
9614 add_vcall_offset_vtbl_entries_1 (binfo, vid);
9616 /* Scan the non-primary bases of BINFO. */
9617 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
9618 if (base_binfo != primary_binfo)
9619 add_vcall_offset_vtbl_entries_r (base_binfo, vid);
9622 /* Called from build_vcall_offset_vtbl_entries_r. */
9624 static void
9625 add_vcall_offset_vtbl_entries_1 (tree binfo, vtbl_init_data* vid)
9627 /* Make entries for the rest of the virtuals. */
9628 tree orig_fn;
9630 /* The ABI requires that the methods be processed in declaration
9631 order. */
9632 for (orig_fn = TYPE_FIELDS (BINFO_TYPE (binfo));
9633 orig_fn;
9634 orig_fn = DECL_CHAIN (orig_fn))
9635 if (TREE_CODE (orig_fn) == FUNCTION_DECL && DECL_VINDEX (orig_fn))
9636 add_vcall_offset (orig_fn, binfo, vid);
9639 /* Add a vcall offset entry for ORIG_FN to the vtable. */
9641 static void
9642 add_vcall_offset (tree orig_fn, tree binfo, vtbl_init_data *vid)
9644 size_t i;
9645 tree vcall_offset;
9646 tree derived_entry;
9648 /* If there is already an entry for a function with the same
9649 signature as FN, then we do not need a second vcall offset.
9650 Check the list of functions already present in the derived
9651 class vtable. */
9652 FOR_EACH_VEC_SAFE_ELT (vid->fns, i, derived_entry)
9654 if (same_signature_p (derived_entry, orig_fn)
9655 /* We only use one vcall offset for virtual destructors,
9656 even though there are two virtual table entries. */
9657 || (DECL_DESTRUCTOR_P (derived_entry)
9658 && DECL_DESTRUCTOR_P (orig_fn)))
9659 return;
9662 /* If we are building these vcall offsets as part of building
9663 the vtable for the most derived class, remember the vcall
9664 offset. */
9665 if (vid->binfo == TYPE_BINFO (vid->derived))
9667 tree_pair_s elt = {orig_fn, vid->index};
9668 vec_safe_push (CLASSTYPE_VCALL_INDICES (vid->derived), elt);
9671 /* The next vcall offset will be found at a more negative
9672 offset. */
9673 vid->index = size_binop (MINUS_EXPR, vid->index,
9674 ssize_int (TARGET_VTABLE_DATA_ENTRY_DISTANCE));
9676 /* Keep track of this function. */
9677 vec_safe_push (vid->fns, orig_fn);
9679 if (vid->generate_vcall_entries)
9681 tree base;
9682 tree fn;
9684 /* Find the overriding function. */
9685 fn = find_final_overrider (vid->rtti_binfo, binfo, orig_fn);
9686 if (fn == error_mark_node)
9687 vcall_offset = build_zero_cst (vtable_entry_type);
9688 else
9690 base = TREE_VALUE (fn);
9692 /* The vbase we're working on is a primary base of
9693 vid->binfo. But it might be a lost primary, so its
9694 BINFO_OFFSET might be wrong, so we just use the
9695 BINFO_OFFSET from vid->binfo. */
9696 vcall_offset = size_diffop_loc (input_location,
9697 BINFO_OFFSET (base),
9698 BINFO_OFFSET (vid->binfo));
9699 vcall_offset = fold_build1_loc (input_location,
9700 NOP_EXPR, vtable_entry_type,
9701 vcall_offset);
9703 /* Add the initializer to the vtable. */
9704 CONSTRUCTOR_APPEND_ELT (vid->inits, NULL_TREE, vcall_offset);
9708 /* Return vtbl initializers for the RTTI entries corresponding to the
9709 BINFO's vtable. The RTTI entries should indicate the object given
9710 by VID->rtti_binfo. */
9712 static void
9713 build_rtti_vtbl_entries (tree binfo, vtbl_init_data* vid)
9715 tree b;
9716 tree t;
9717 tree offset;
9718 tree decl;
9719 tree init;
9721 t = BINFO_TYPE (vid->rtti_binfo);
9723 /* To find the complete object, we will first convert to our most
9724 primary base, and then add the offset in the vtbl to that value. */
9725 b = most_primary_binfo (binfo);
9726 offset = size_diffop_loc (input_location,
9727 BINFO_OFFSET (vid->rtti_binfo), BINFO_OFFSET (b));
9729 /* The second entry is the address of the typeinfo object. */
9730 if (flag_rtti)
9731 decl = build_address (get_tinfo_decl (t));
9732 else
9733 decl = integer_zero_node;
9735 /* Convert the declaration to a type that can be stored in the
9736 vtable. */
9737 init = build_nop (vfunc_ptr_type_node, decl);
9738 CONSTRUCTOR_APPEND_ELT (vid->inits, NULL_TREE, init);
9740 /* Add the offset-to-top entry. It comes earlier in the vtable than
9741 the typeinfo entry. Convert the offset to look like a
9742 function pointer, so that we can put it in the vtable. */
9743 init = build_nop (vfunc_ptr_type_node, offset);
9744 CONSTRUCTOR_APPEND_ELT (vid->inits, NULL_TREE, init);
9747 /* TRUE iff TYPE is uniquely derived from PARENT. Ignores
9748 accessibility. */
9750 bool
9751 uniquely_derived_from_p (tree parent, tree type)
9753 tree base = lookup_base (type, parent, ba_unique, NULL, tf_none);
9754 return base && base != error_mark_node;
9757 /* TRUE iff TYPE is publicly & uniquely derived from PARENT. */
9759 bool
9760 publicly_uniquely_derived_p (tree parent, tree type)
9762 tree base = lookup_base (type, parent, ba_ignore_scope | ba_check,
9763 NULL, tf_none);
9764 return base && base != error_mark_node;
9767 /* CTX1 and CTX2 are declaration contexts. Return the innermost common
9768 class between them, if any. */
9770 tree
9771 common_enclosing_class (tree ctx1, tree ctx2)
9773 if (!TYPE_P (ctx1) || !TYPE_P (ctx2))
9774 return NULL_TREE;
9775 gcc_assert (ctx1 == TYPE_MAIN_VARIANT (ctx1)
9776 && ctx2 == TYPE_MAIN_VARIANT (ctx2));
9777 if (ctx1 == ctx2)
9778 return ctx1;
9779 for (tree t = ctx1; TYPE_P (t); t = TYPE_CONTEXT (t))
9780 TYPE_MARKED_P (t) = true;
9781 tree found = NULL_TREE;
9782 for (tree t = ctx2; TYPE_P (t); t = TYPE_CONTEXT (t))
9783 if (TYPE_MARKED_P (t))
9785 found = t;
9786 break;
9788 for (tree t = ctx1; TYPE_P (t); t = TYPE_CONTEXT (t))
9789 TYPE_MARKED_P (t) = false;
9790 return found;
9793 #include "gt-cp-class.h"