* config/sparc/sparc.c (sparc_option_override): Set MASK_FSMULD flag
[official-gcc.git] / gcc / cp / class.c
blob508570bdb04ccb0a862bcb7d59cbc1d789010a02
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 finish_struct_methods (tree);
133 static void maybe_warn_about_overly_private_class (tree);
134 static int method_name_cmp (const void *, const void *);
135 static int resort_method_name_cmp (const void *, const void *);
136 static void add_implicitly_declared_members (tree, tree*, int, int);
137 static tree fixed_type_or_null (tree, int *, int *);
138 static tree build_simple_base_path (tree expr, tree binfo);
139 static tree build_vtbl_ref_1 (tree, tree);
140 static void build_vtbl_initializer (tree, tree, tree, tree, int *,
141 vec<constructor_elt, va_gc> **);
142 static int count_fields (tree);
143 static int add_fields_to_record_type (tree, struct sorted_fields_type*, int);
144 static void insert_into_classtype_sorted_fields (tree, tree, int);
145 static bool check_bitfield_decl (tree);
146 static bool check_field_decl (tree, tree, int *, int *);
147 static void check_field_decls (tree, tree *, int *, int *);
148 static tree *build_base_field (record_layout_info, tree, splay_tree, tree *);
149 static void build_base_fields (record_layout_info, splay_tree, tree *);
150 static void check_methods (tree);
151 static void remove_zero_width_bit_fields (tree);
152 static bool accessible_nvdtor_p (tree);
154 /* Used by find_flexarrays and related functions. */
155 struct flexmems_t;
156 static void diagnose_flexarrays (tree, const flexmems_t *);
157 static void find_flexarrays (tree, flexmems_t *, bool = false,
158 tree = NULL_TREE, tree = NULL_TREE);
159 static void check_flexarrays (tree, flexmems_t * = NULL, bool = false);
160 static void check_bases (tree, int *, int *);
161 static void check_bases_and_members (tree);
162 static tree create_vtable_ptr (tree, tree *);
163 static void include_empty_classes (record_layout_info);
164 static void layout_class_type (tree, tree *);
165 static void propagate_binfo_offsets (tree, tree);
166 static void layout_virtual_bases (record_layout_info, splay_tree);
167 static void build_vbase_offset_vtbl_entries (tree, vtbl_init_data *);
168 static void add_vcall_offset_vtbl_entries_r (tree, vtbl_init_data *);
169 static void add_vcall_offset_vtbl_entries_1 (tree, vtbl_init_data *);
170 static void build_vcall_offset_vtbl_entries (tree, vtbl_init_data *);
171 static void add_vcall_offset (tree, tree, vtbl_init_data *);
172 static void layout_vtable_decl (tree, int);
173 static tree dfs_find_final_overrider_pre (tree, void *);
174 static tree dfs_find_final_overrider_post (tree, void *);
175 static tree find_final_overrider (tree, tree, tree);
176 static int make_new_vtable (tree, tree);
177 static tree get_primary_binfo (tree);
178 static int maybe_indent_hierarchy (FILE *, int, int);
179 static tree dump_class_hierarchy_r (FILE *, dump_flags_t, tree, tree, int);
180 static void dump_class_hierarchy (tree);
181 static void dump_class_hierarchy_1 (FILE *, dump_flags_t, tree);
182 static void dump_array (FILE *, tree);
183 static void dump_vtable (tree, tree, tree);
184 static void dump_vtt (tree, tree);
185 static void dump_thunk (FILE *, int, tree);
186 static tree build_vtable (tree, tree, tree);
187 static void initialize_vtable (tree, vec<constructor_elt, va_gc> *);
188 static void layout_nonempty_base_or_field (record_layout_info,
189 tree, tree, splay_tree);
190 static tree end_of_class (tree, int);
191 static bool layout_empty_base (record_layout_info, tree, tree, splay_tree);
192 static void accumulate_vtbl_inits (tree, tree, tree, tree, tree,
193 vec<constructor_elt, va_gc> **);
194 static void dfs_accumulate_vtbl_inits (tree, tree, tree, tree, tree,
195 vec<constructor_elt, va_gc> **);
196 static void build_rtti_vtbl_entries (tree, vtbl_init_data *);
197 static void build_vcall_and_vbase_vtbl_entries (tree, vtbl_init_data *);
198 static void clone_constructors_and_destructors (tree);
199 static tree build_clone (tree, tree);
200 static void update_vtable_entry_for_fn (tree, tree, tree, tree *, unsigned);
201 static void build_ctor_vtbl_group (tree, tree);
202 static void build_vtt (tree);
203 static tree binfo_ctor_vtable (tree);
204 static void build_vtt_inits (tree, tree, vec<constructor_elt, va_gc> **,
205 tree *);
206 static tree dfs_build_secondary_vptr_vtt_inits (tree, void *);
207 static tree dfs_fixup_binfo_vtbls (tree, void *);
208 static int record_subobject_offset (tree, tree, splay_tree);
209 static int check_subobject_offset (tree, tree, splay_tree);
210 static int walk_subobject_offsets (tree, subobject_offset_fn,
211 tree, splay_tree, tree, int);
212 static void record_subobject_offsets (tree, tree, splay_tree, bool);
213 static int layout_conflict_p (tree, tree, splay_tree, int);
214 static int splay_tree_compare_integer_csts (splay_tree_key k1,
215 splay_tree_key k2);
216 static void warn_about_ambiguous_bases (tree);
217 static bool type_requires_array_cookie (tree);
218 static bool base_derived_from (tree, tree);
219 static int empty_base_at_nonzero_offset_p (tree, tree, splay_tree);
220 static tree end_of_base (tree);
221 static tree get_vcall_index (tree, tree);
222 static bool type_maybe_constexpr_default_constructor (tree);
224 /* Variables shared between class.c and call.c. */
226 int n_vtables = 0;
227 int n_vtable_entries = 0;
228 int n_vtable_searches = 0;
229 int n_vtable_elems = 0;
230 int n_convert_harshness = 0;
231 int n_compute_conversion_costs = 0;
232 int n_inner_fields_searched = 0;
234 /* Return a COND_EXPR that executes TRUE_STMT if this execution of the
235 'structor is in charge of 'structing virtual bases, or FALSE_STMT
236 otherwise. */
238 tree
239 build_if_in_charge (tree true_stmt, tree false_stmt)
241 gcc_assert (DECL_HAS_IN_CHARGE_PARM_P (current_function_decl));
242 tree cmp = build2 (NE_EXPR, boolean_type_node,
243 current_in_charge_parm, integer_zero_node);
244 tree type = unlowered_expr_type (true_stmt);
245 if (VOID_TYPE_P (type))
246 type = unlowered_expr_type (false_stmt);
247 tree cond = build3 (COND_EXPR, type,
248 cmp, true_stmt, false_stmt);
249 return cond;
252 /* Convert to or from a base subobject. EXPR is an expression of type
253 `A' or `A*', an expression of type `B' or `B*' is returned. To
254 convert A to a base B, CODE is PLUS_EXPR and BINFO is the binfo for
255 the B base instance within A. To convert base A to derived B, CODE
256 is MINUS_EXPR and BINFO is the binfo for the A instance within B.
257 In this latter case, A must not be a morally virtual base of B.
258 NONNULL is true if EXPR is known to be non-NULL (this is only
259 needed when EXPR is of pointer type). CV qualifiers are preserved
260 from EXPR. */
262 tree
263 build_base_path (enum tree_code code,
264 tree expr,
265 tree binfo,
266 int nonnull,
267 tsubst_flags_t complain)
269 tree v_binfo = NULL_TREE;
270 tree d_binfo = NULL_TREE;
271 tree probe;
272 tree offset;
273 tree target_type;
274 tree null_test = NULL;
275 tree ptr_target_type;
276 int fixed_type_p;
277 int want_pointer = TYPE_PTR_P (TREE_TYPE (expr));
278 bool has_empty = false;
279 bool virtual_access;
280 bool rvalue = false;
282 if (expr == error_mark_node || binfo == error_mark_node || !binfo)
283 return error_mark_node;
285 for (probe = binfo; probe; probe = BINFO_INHERITANCE_CHAIN (probe))
287 d_binfo = probe;
288 if (is_empty_class (BINFO_TYPE (probe)))
289 has_empty = true;
290 if (!v_binfo && BINFO_VIRTUAL_P (probe))
291 v_binfo = probe;
294 probe = TYPE_MAIN_VARIANT (TREE_TYPE (expr));
295 if (want_pointer)
296 probe = TYPE_MAIN_VARIANT (TREE_TYPE (probe));
298 if (code == PLUS_EXPR
299 && !SAME_BINFO_TYPE_P (BINFO_TYPE (d_binfo), probe))
301 /* This can happen when adjust_result_of_qualified_name_lookup can't
302 find a unique base binfo in a call to a member function. We
303 couldn't give the diagnostic then since we might have been calling
304 a static member function, so we do it now. In other cases, eg.
305 during error recovery (c++/71979), we may not have a base at all. */
306 if (complain & tf_error)
308 tree base = lookup_base (probe, BINFO_TYPE (d_binfo),
309 ba_unique, NULL, complain);
310 gcc_assert (base == error_mark_node || !base);
312 return error_mark_node;
315 gcc_assert ((code == MINUS_EXPR
316 && SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), probe))
317 || code == PLUS_EXPR);
319 if (binfo == d_binfo)
320 /* Nothing to do. */
321 return expr;
323 if (code == MINUS_EXPR && v_binfo)
325 if (complain & tf_error)
327 if (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), BINFO_TYPE (v_binfo)))
329 if (want_pointer)
330 error ("cannot convert from pointer to base class %qT to "
331 "pointer to derived class %qT because the base is "
332 "virtual", BINFO_TYPE (binfo), BINFO_TYPE (d_binfo));
333 else
334 error ("cannot convert from base class %qT to derived "
335 "class %qT because the base is virtual",
336 BINFO_TYPE (binfo), BINFO_TYPE (d_binfo));
338 else
340 if (want_pointer)
341 error ("cannot convert from pointer to base class %qT to "
342 "pointer to derived class %qT via virtual base %qT",
343 BINFO_TYPE (binfo), BINFO_TYPE (d_binfo),
344 BINFO_TYPE (v_binfo));
345 else
346 error ("cannot convert from base class %qT to derived "
347 "class %qT via virtual base %qT", BINFO_TYPE (binfo),
348 BINFO_TYPE (d_binfo), BINFO_TYPE (v_binfo));
351 return error_mark_node;
354 if (!want_pointer)
356 rvalue = !lvalue_p (expr);
357 /* This must happen before the call to save_expr. */
358 expr = cp_build_addr_expr (expr, complain);
360 else
361 expr = mark_rvalue_use (expr);
363 offset = BINFO_OFFSET (binfo);
364 fixed_type_p = resolves_to_fixed_type_p (expr, &nonnull);
365 target_type = code == PLUS_EXPR ? BINFO_TYPE (binfo) : BINFO_TYPE (d_binfo);
366 /* TARGET_TYPE has been extracted from BINFO, and, is therefore always
367 cv-unqualified. Extract the cv-qualifiers from EXPR so that the
368 expression returned matches the input. */
369 target_type = cp_build_qualified_type
370 (target_type, cp_type_quals (TREE_TYPE (TREE_TYPE (expr))));
371 ptr_target_type = build_pointer_type (target_type);
373 /* Do we need to look in the vtable for the real offset? */
374 virtual_access = (v_binfo && fixed_type_p <= 0);
376 /* Don't bother with the calculations inside sizeof; they'll ICE if the
377 source type is incomplete and the pointer value doesn't matter. In a
378 template (even in instantiate_non_dependent_expr), we don't have vtables
379 set up properly yet, and the value doesn't matter there either; we're
380 just interested in the result of overload resolution. */
381 if (cp_unevaluated_operand != 0
382 || processing_template_decl
383 || in_template_function ())
385 expr = build_nop (ptr_target_type, expr);
386 goto indout;
389 /* If we're in an NSDMI, we don't have the full constructor context yet
390 that we need for converting to a virtual base, so just build a stub
391 CONVERT_EXPR and expand it later in bot_replace. */
392 if (virtual_access && fixed_type_p < 0
393 && current_scope () != current_function_decl)
395 expr = build1 (CONVERT_EXPR, ptr_target_type, expr);
396 CONVERT_EXPR_VBASE_PATH (expr) = true;
397 goto indout;
400 /* Do we need to check for a null pointer? */
401 if (want_pointer && !nonnull)
403 /* If we know the conversion will not actually change the value
404 of EXPR, then we can avoid testing the expression for NULL.
405 We have to avoid generating a COMPONENT_REF for a base class
406 field, because other parts of the compiler know that such
407 expressions are always non-NULL. */
408 if (!virtual_access && integer_zerop (offset))
409 return build_nop (ptr_target_type, expr);
410 null_test = error_mark_node;
413 /* Protect against multiple evaluation if necessary. */
414 if (TREE_SIDE_EFFECTS (expr) && (null_test || virtual_access))
415 expr = save_expr (expr);
417 /* Now that we've saved expr, build the real null test. */
418 if (null_test)
420 tree zero = cp_convert (TREE_TYPE (expr), nullptr_node, complain);
421 null_test = build2_loc (input_location, NE_EXPR, boolean_type_node,
422 expr, zero);
423 /* This is a compiler generated comparison, don't emit
424 e.g. -Wnonnull-compare warning for it. */
425 TREE_NO_WARNING (null_test) = 1;
428 /* If this is a simple base reference, express it as a COMPONENT_REF. */
429 if (code == PLUS_EXPR && !virtual_access
430 /* We don't build base fields for empty bases, and they aren't very
431 interesting to the optimizers anyway. */
432 && !has_empty)
434 expr = cp_build_indirect_ref (expr, RO_NULL, complain);
435 expr = build_simple_base_path (expr, binfo);
436 if (rvalue)
437 expr = move (expr);
438 if (want_pointer)
439 expr = build_address (expr);
440 target_type = TREE_TYPE (expr);
441 goto out;
444 if (virtual_access)
446 /* Going via virtual base V_BINFO. We need the static offset
447 from V_BINFO to BINFO, and the dynamic offset from D_BINFO to
448 V_BINFO. That offset is an entry in D_BINFO's vtable. */
449 tree v_offset;
451 if (fixed_type_p < 0 && in_base_initializer)
453 /* In a base member initializer, we cannot rely on the
454 vtable being set up. We have to indirect via the
455 vtt_parm. */
456 tree t;
458 t = TREE_TYPE (TYPE_VFIELD (current_class_type));
459 t = build_pointer_type (t);
460 v_offset = fold_convert (t, current_vtt_parm);
461 v_offset = cp_build_indirect_ref (v_offset, RO_NULL, complain);
463 else
465 tree t = expr;
466 if (sanitize_flags_p (SANITIZE_VPTR)
467 && fixed_type_p == 0)
469 t = cp_ubsan_maybe_instrument_cast_to_vbase (input_location,
470 probe, expr);
471 if (t == NULL_TREE)
472 t = expr;
474 v_offset = build_vfield_ref (cp_build_indirect_ref (t, RO_NULL,
475 complain),
476 TREE_TYPE (TREE_TYPE (expr)));
479 if (v_offset == error_mark_node)
480 return error_mark_node;
482 v_offset = fold_build_pointer_plus (v_offset, BINFO_VPTR_FIELD (v_binfo));
483 v_offset = build1 (NOP_EXPR,
484 build_pointer_type (ptrdiff_type_node),
485 v_offset);
486 v_offset = cp_build_indirect_ref (v_offset, RO_NULL, complain);
487 TREE_CONSTANT (v_offset) = 1;
489 offset = convert_to_integer (ptrdiff_type_node,
490 size_diffop_loc (input_location, offset,
491 BINFO_OFFSET (v_binfo)));
493 if (!integer_zerop (offset))
494 v_offset = build2 (code, ptrdiff_type_node, v_offset, offset);
496 if (fixed_type_p < 0)
497 /* Negative fixed_type_p means this is a constructor or destructor;
498 virtual base layout is fixed in in-charge [cd]tors, but not in
499 base [cd]tors. */
500 offset = build_if_in_charge
501 (convert_to_integer (ptrdiff_type_node, BINFO_OFFSET (binfo)),
502 v_offset);
503 else
504 offset = v_offset;
507 if (want_pointer)
508 target_type = ptr_target_type;
510 expr = build1 (NOP_EXPR, ptr_target_type, expr);
512 if (!integer_zerop (offset))
514 offset = fold_convert (sizetype, offset);
515 if (code == MINUS_EXPR)
516 offset = fold_build1_loc (input_location, NEGATE_EXPR, sizetype, offset);
517 expr = fold_build_pointer_plus (expr, offset);
519 else
520 null_test = NULL;
522 indout:
523 if (!want_pointer)
525 expr = cp_build_indirect_ref (expr, RO_NULL, complain);
526 if (rvalue)
527 expr = move (expr);
530 out:
531 if (null_test)
532 expr = fold_build3_loc (input_location, COND_EXPR, target_type, null_test, expr,
533 build_zero_cst (target_type));
535 return expr;
538 /* Subroutine of build_base_path; EXPR and BINFO are as in that function.
539 Perform a derived-to-base conversion by recursively building up a
540 sequence of COMPONENT_REFs to the appropriate base fields. */
542 static tree
543 build_simple_base_path (tree expr, tree binfo)
545 tree type = BINFO_TYPE (binfo);
546 tree d_binfo = BINFO_INHERITANCE_CHAIN (binfo);
547 tree field;
549 if (d_binfo == NULL_TREE)
551 tree temp;
553 gcc_assert (TYPE_MAIN_VARIANT (TREE_TYPE (expr)) == type);
555 /* Transform `(a, b).x' into `(*(a, &b)).x', `(a ? b : c).x'
556 into `(*(a ? &b : &c)).x', and so on. A COND_EXPR is only
557 an lvalue in the front end; only _DECLs and _REFs are lvalues
558 in the back end. */
559 temp = unary_complex_lvalue (ADDR_EXPR, expr);
560 if (temp)
561 expr = cp_build_indirect_ref (temp, RO_NULL, tf_warning_or_error);
563 return expr;
566 /* Recurse. */
567 expr = build_simple_base_path (expr, d_binfo);
569 for (field = TYPE_FIELDS (BINFO_TYPE (d_binfo));
570 field; field = DECL_CHAIN (field))
571 /* Is this the base field created by build_base_field? */
572 if (TREE_CODE (field) == FIELD_DECL
573 && DECL_FIELD_IS_BASE (field)
574 && TREE_TYPE (field) == type
575 /* If we're looking for a field in the most-derived class,
576 also check the field offset; we can have two base fields
577 of the same type if one is an indirect virtual base and one
578 is a direct non-virtual base. */
579 && (BINFO_INHERITANCE_CHAIN (d_binfo)
580 || tree_int_cst_equal (byte_position (field),
581 BINFO_OFFSET (binfo))))
583 /* We don't use build_class_member_access_expr here, as that
584 has unnecessary checks, and more importantly results in
585 recursive calls to dfs_walk_once. */
586 int type_quals = cp_type_quals (TREE_TYPE (expr));
588 expr = build3 (COMPONENT_REF,
589 cp_build_qualified_type (type, type_quals),
590 expr, field, NULL_TREE);
591 /* Mark the expression const or volatile, as appropriate.
592 Even though we've dealt with the type above, we still have
593 to mark the expression itself. */
594 if (type_quals & TYPE_QUAL_CONST)
595 TREE_READONLY (expr) = 1;
596 if (type_quals & TYPE_QUAL_VOLATILE)
597 TREE_THIS_VOLATILE (expr) = 1;
599 return expr;
602 /* Didn't find the base field?!? */
603 gcc_unreachable ();
606 /* Convert OBJECT to the base TYPE. OBJECT is an expression whose
607 type is a class type or a pointer to a class type. In the former
608 case, TYPE is also a class type; in the latter it is another
609 pointer type. If CHECK_ACCESS is true, an error message is emitted
610 if TYPE is inaccessible. If OBJECT has pointer type, the value is
611 assumed to be non-NULL. */
613 tree
614 convert_to_base (tree object, tree type, bool check_access, bool nonnull,
615 tsubst_flags_t complain)
617 tree binfo;
618 tree object_type;
620 if (TYPE_PTR_P (TREE_TYPE (object)))
622 object_type = TREE_TYPE (TREE_TYPE (object));
623 type = TREE_TYPE (type);
625 else
626 object_type = TREE_TYPE (object);
628 binfo = lookup_base (object_type, type, check_access ? ba_check : ba_unique,
629 NULL, complain);
630 if (!binfo || binfo == error_mark_node)
631 return error_mark_node;
633 return build_base_path (PLUS_EXPR, object, binfo, nonnull, complain);
636 /* EXPR is an expression with unqualified class type. BASE is a base
637 binfo of that class type. Returns EXPR, converted to the BASE
638 type. This function assumes that EXPR is the most derived class;
639 therefore virtual bases can be found at their static offsets. */
641 tree
642 convert_to_base_statically (tree expr, tree base)
644 tree expr_type;
646 expr_type = TREE_TYPE (expr);
647 if (!SAME_BINFO_TYPE_P (BINFO_TYPE (base), expr_type))
649 /* If this is a non-empty base, use a COMPONENT_REF. */
650 if (!is_empty_class (BINFO_TYPE (base)))
651 return build_simple_base_path (expr, base);
653 /* We use fold_build2 and fold_convert below to simplify the trees
654 provided to the optimizers. It is not safe to call these functions
655 when processing a template because they do not handle C++-specific
656 trees. */
657 gcc_assert (!processing_template_decl);
658 expr = cp_build_addr_expr (expr, tf_warning_or_error);
659 if (!integer_zerop (BINFO_OFFSET (base)))
660 expr = fold_build_pointer_plus_loc (input_location,
661 expr, BINFO_OFFSET (base));
662 expr = fold_convert (build_pointer_type (BINFO_TYPE (base)), expr);
663 expr = build_fold_indirect_ref_loc (input_location, expr);
666 return expr;
670 tree
671 build_vfield_ref (tree datum, tree type)
673 tree vfield, vcontext;
675 if (datum == error_mark_node
676 /* Can happen in case of duplicate base types (c++/59082). */
677 || !TYPE_VFIELD (type))
678 return error_mark_node;
680 /* First, convert to the requested type. */
681 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (datum), type))
682 datum = convert_to_base (datum, type, /*check_access=*/false,
683 /*nonnull=*/true, tf_warning_or_error);
685 /* Second, the requested type may not be the owner of its own vptr.
686 If not, convert to the base class that owns it. We cannot use
687 convert_to_base here, because VCONTEXT may appear more than once
688 in the inheritance hierarchy of TYPE, and thus direct conversion
689 between the types may be ambiguous. Following the path back up
690 one step at a time via primary bases avoids the problem. */
691 vfield = TYPE_VFIELD (type);
692 vcontext = DECL_CONTEXT (vfield);
693 while (!same_type_ignoring_top_level_qualifiers_p (vcontext, type))
695 datum = build_simple_base_path (datum, CLASSTYPE_PRIMARY_BINFO (type));
696 type = TREE_TYPE (datum);
699 return build3 (COMPONENT_REF, TREE_TYPE (vfield), datum, vfield, NULL_TREE);
702 /* Given an object INSTANCE, return an expression which yields the
703 vtable element corresponding to INDEX. There are many special
704 cases for INSTANCE which we take care of here, mainly to avoid
705 creating extra tree nodes when we don't have to. */
707 static tree
708 build_vtbl_ref_1 (tree instance, tree idx)
710 tree aref;
711 tree vtbl = NULL_TREE;
713 /* Try to figure out what a reference refers to, and
714 access its virtual function table directly. */
716 int cdtorp = 0;
717 tree fixed_type = fixed_type_or_null (instance, NULL, &cdtorp);
719 tree basetype = non_reference (TREE_TYPE (instance));
721 if (fixed_type && !cdtorp)
723 tree binfo = lookup_base (fixed_type, basetype,
724 ba_unique, NULL, tf_none);
725 if (binfo && binfo != error_mark_node)
726 vtbl = unshare_expr (BINFO_VTABLE (binfo));
729 if (!vtbl)
730 vtbl = build_vfield_ref (instance, basetype);
732 aref = build_array_ref (input_location, vtbl, idx);
733 TREE_CONSTANT (aref) |= TREE_CONSTANT (vtbl) && TREE_CONSTANT (idx);
735 return aref;
738 tree
739 build_vtbl_ref (tree instance, tree idx)
741 tree aref = build_vtbl_ref_1 (instance, idx);
743 return aref;
746 /* Given a stable object pointer INSTANCE_PTR, return an expression which
747 yields a function pointer corresponding to vtable element INDEX. */
749 tree
750 build_vfn_ref (tree instance_ptr, tree idx)
752 tree aref;
754 aref = build_vtbl_ref_1 (cp_build_indirect_ref (instance_ptr, RO_NULL,
755 tf_warning_or_error),
756 idx);
758 /* When using function descriptors, the address of the
759 vtable entry is treated as a function pointer. */
760 if (TARGET_VTABLE_USES_DESCRIPTORS)
761 aref = build1 (NOP_EXPR, TREE_TYPE (aref),
762 cp_build_addr_expr (aref, tf_warning_or_error));
764 /* Remember this as a method reference, for later devirtualization. */
765 aref = build3 (OBJ_TYPE_REF, TREE_TYPE (aref), aref, instance_ptr, idx);
767 return aref;
770 /* Return the name of the virtual function table (as an IDENTIFIER_NODE)
771 for the given TYPE. */
773 static tree
774 get_vtable_name (tree type)
776 return mangle_vtbl_for_type (type);
779 /* DECL is an entity associated with TYPE, like a virtual table or an
780 implicitly generated constructor. Determine whether or not DECL
781 should have external or internal linkage at the object file
782 level. This routine does not deal with COMDAT linkage and other
783 similar complexities; it simply sets TREE_PUBLIC if it possible for
784 entities in other translation units to contain copies of DECL, in
785 the abstract. */
787 void
788 set_linkage_according_to_type (tree /*type*/, tree decl)
790 TREE_PUBLIC (decl) = 1;
791 determine_visibility (decl);
794 /* Create a VAR_DECL for a primary or secondary vtable for CLASS_TYPE.
795 (For a secondary vtable for B-in-D, CLASS_TYPE should be D, not B.)
796 Use NAME for the name of the vtable, and VTABLE_TYPE for its type. */
798 static tree
799 build_vtable (tree class_type, tree name, tree vtable_type)
801 tree decl;
803 decl = build_lang_decl (VAR_DECL, name, vtable_type);
804 /* vtable names are already mangled; give them their DECL_ASSEMBLER_NAME
805 now to avoid confusion in mangle_decl. */
806 SET_DECL_ASSEMBLER_NAME (decl, name);
807 DECL_CONTEXT (decl) = class_type;
808 DECL_ARTIFICIAL (decl) = 1;
809 TREE_STATIC (decl) = 1;
810 TREE_READONLY (decl) = 1;
811 DECL_VIRTUAL_P (decl) = 1;
812 SET_DECL_ALIGN (decl, TARGET_VTABLE_ENTRY_ALIGN);
813 DECL_USER_ALIGN (decl) = true;
814 DECL_VTABLE_OR_VTT_P (decl) = 1;
815 set_linkage_according_to_type (class_type, decl);
816 /* The vtable has not been defined -- yet. */
817 DECL_EXTERNAL (decl) = 1;
818 DECL_NOT_REALLY_EXTERN (decl) = 1;
820 /* Mark the VAR_DECL node representing the vtable itself as a
821 "gratuitous" one, thereby forcing dwarfout.c to ignore it. It
822 is rather important that such things be ignored because any
823 effort to actually generate DWARF for them will run into
824 trouble when/if we encounter code like:
826 #pragma interface
827 struct S { virtual void member (); };
829 because the artificial declaration of the vtable itself (as
830 manufactured by the g++ front end) will say that the vtable is
831 a static member of `S' but only *after* the debug output for
832 the definition of `S' has already been output. This causes
833 grief because the DWARF entry for the definition of the vtable
834 will try to refer back to an earlier *declaration* of the
835 vtable as a static member of `S' and there won't be one. We
836 might be able to arrange to have the "vtable static member"
837 attached to the member list for `S' before the debug info for
838 `S' get written (which would solve the problem) but that would
839 require more intrusive changes to the g++ front end. */
840 DECL_IGNORED_P (decl) = 1;
842 return decl;
845 /* Get the VAR_DECL of the vtable for TYPE. TYPE need not be polymorphic,
846 or even complete. If this does not exist, create it. If COMPLETE is
847 nonzero, then complete the definition of it -- that will render it
848 impossible to actually build the vtable, but is useful to get at those
849 which are known to exist in the runtime. */
851 tree
852 get_vtable_decl (tree type, int complete)
854 tree decl;
856 if (CLASSTYPE_VTABLES (type))
857 return CLASSTYPE_VTABLES (type);
859 decl = build_vtable (type, get_vtable_name (type), vtbl_type_node);
860 CLASSTYPE_VTABLES (type) = decl;
862 if (complete)
864 DECL_EXTERNAL (decl) = 1;
865 cp_finish_decl (decl, NULL_TREE, false, NULL_TREE, 0);
868 return decl;
871 /* Build the primary virtual function table for TYPE. If BINFO is
872 non-NULL, build the vtable starting with the initial approximation
873 that it is the same as the one which is the head of the association
874 list. Returns a nonzero value if a new vtable is actually
875 created. */
877 static int
878 build_primary_vtable (tree binfo, tree type)
880 tree decl;
881 tree virtuals;
883 decl = get_vtable_decl (type, /*complete=*/0);
885 if (binfo)
887 if (BINFO_NEW_VTABLE_MARKED (binfo))
888 /* We have already created a vtable for this base, so there's
889 no need to do it again. */
890 return 0;
892 virtuals = copy_list (BINFO_VIRTUALS (binfo));
893 TREE_TYPE (decl) = TREE_TYPE (get_vtbl_decl_for_binfo (binfo));
894 DECL_SIZE (decl) = TYPE_SIZE (TREE_TYPE (decl));
895 DECL_SIZE_UNIT (decl) = TYPE_SIZE_UNIT (TREE_TYPE (decl));
897 else
899 gcc_assert (TREE_TYPE (decl) == vtbl_type_node);
900 virtuals = NULL_TREE;
903 if (GATHER_STATISTICS)
905 n_vtables += 1;
906 n_vtable_elems += list_length (virtuals);
909 /* Initialize the association list for this type, based
910 on our first approximation. */
911 BINFO_VTABLE (TYPE_BINFO (type)) = decl;
912 BINFO_VIRTUALS (TYPE_BINFO (type)) = virtuals;
913 SET_BINFO_NEW_VTABLE_MARKED (TYPE_BINFO (type));
914 return 1;
917 /* Give BINFO a new virtual function table which is initialized
918 with a skeleton-copy of its original initialization. The only
919 entry that changes is the `delta' entry, so we can really
920 share a lot of structure.
922 FOR_TYPE is the most derived type which caused this table to
923 be needed.
925 Returns nonzero if we haven't met BINFO before.
927 The order in which vtables are built (by calling this function) for
928 an object must remain the same, otherwise a binary incompatibility
929 can result. */
931 static int
932 build_secondary_vtable (tree binfo)
934 if (BINFO_NEW_VTABLE_MARKED (binfo))
935 /* We already created a vtable for this base. There's no need to
936 do it again. */
937 return 0;
939 /* Remember that we've created a vtable for this BINFO, so that we
940 don't try to do so again. */
941 SET_BINFO_NEW_VTABLE_MARKED (binfo);
943 /* Make fresh virtual list, so we can smash it later. */
944 BINFO_VIRTUALS (binfo) = copy_list (BINFO_VIRTUALS (binfo));
946 /* Secondary vtables are laid out as part of the same structure as
947 the primary vtable. */
948 BINFO_VTABLE (binfo) = NULL_TREE;
949 return 1;
952 /* Create a new vtable for BINFO which is the hierarchy dominated by
953 T. Return nonzero if we actually created a new vtable. */
955 static int
956 make_new_vtable (tree t, tree binfo)
958 if (binfo == TYPE_BINFO (t))
959 /* In this case, it is *type*'s vtable we are modifying. We start
960 with the approximation that its vtable is that of the
961 immediate base class. */
962 return build_primary_vtable (binfo, t);
963 else
964 /* This is our very own copy of `basetype' to play with. Later,
965 we will fill in all the virtual functions that override the
966 virtual functions in these base classes which are not defined
967 by the current type. */
968 return build_secondary_vtable (binfo);
971 /* Make *VIRTUALS, an entry on the BINFO_VIRTUALS list for BINFO
972 (which is in the hierarchy dominated by T) list FNDECL as its
973 BV_FN. DELTA is the required constant adjustment from the `this'
974 pointer where the vtable entry appears to the `this' required when
975 the function is actually called. */
977 static void
978 modify_vtable_entry (tree t,
979 tree binfo,
980 tree fndecl,
981 tree delta,
982 tree *virtuals)
984 tree v;
986 v = *virtuals;
988 if (fndecl != BV_FN (v)
989 || !tree_int_cst_equal (delta, BV_DELTA (v)))
991 /* We need a new vtable for BINFO. */
992 if (make_new_vtable (t, binfo))
994 /* If we really did make a new vtable, we also made a copy
995 of the BINFO_VIRTUALS list. Now, we have to find the
996 corresponding entry in that list. */
997 *virtuals = BINFO_VIRTUALS (binfo);
998 while (BV_FN (*virtuals) != BV_FN (v))
999 *virtuals = TREE_CHAIN (*virtuals);
1000 v = *virtuals;
1003 BV_DELTA (v) = delta;
1004 BV_VCALL_INDEX (v) = NULL_TREE;
1005 BV_FN (v) = fndecl;
1010 /* Add method METHOD to class TYPE. If VIA_USING indicates whether
1011 METHOD is being injected via a using_decl. Returns true if the
1012 method could be added to the method vec. */
1014 bool
1015 add_method (tree type, tree method, bool via_using)
1017 unsigned slot;
1018 bool template_conv_p = false;
1019 bool conv_p;
1020 vec<tree, va_gc> *method_vec;
1021 bool complete_p;
1022 bool insert_p = false;
1023 tree current_fns;
1025 if (method == error_mark_node)
1026 return false;
1028 complete_p = COMPLETE_TYPE_P (type);
1029 conv_p = DECL_CONV_FN_P (method);
1030 if (conv_p)
1031 template_conv_p = (TREE_CODE (method) == TEMPLATE_DECL
1032 && DECL_TEMPLATE_CONV_FN_P (method));
1034 method_vec = CLASSTYPE_METHOD_VEC (type);
1035 if (!method_vec)
1037 /* Make a new method vector. We start with 8 entries. We must
1038 allocate at least two (for constructors and destructors), and
1039 we're going to end up with an assignment operator at some
1040 point as well. */
1041 vec_alloc (method_vec, 8);
1042 CLASSTYPE_METHOD_VEC (type) = method_vec;
1045 /* Maintain TYPE_HAS_USER_CONSTRUCTOR, etc. */
1046 grok_special_member_properties (method);
1048 tree m;
1050 insert_p = true;
1051 /* See if we already have an entry with this name. */
1052 for (slot = CLASSTYPE_FIRST_CONVERSION_SLOT;
1053 vec_safe_iterate (method_vec, slot, &m);
1054 ++slot)
1056 m = OVL_FIRST (m);
1057 if (template_conv_p)
1059 if (TREE_CODE (m) == TEMPLATE_DECL
1060 && DECL_TEMPLATE_CONV_FN_P (m))
1061 insert_p = false;
1062 break;
1064 if (conv_p && !DECL_CONV_FN_P (m))
1065 break;
1066 if (DECL_NAME (m) == DECL_NAME (method))
1068 insert_p = false;
1069 break;
1071 if (complete_p
1072 && !DECL_CONV_FN_P (m)
1073 && DECL_NAME (m) > DECL_NAME (method))
1074 break;
1076 current_fns = insert_p ? NULL_TREE : (*method_vec)[slot];
1078 /* Check to see if we've already got this method. */
1079 for (ovl_iterator iter (current_fns); iter; ++iter)
1081 tree fn = *iter;
1082 tree fn_type;
1083 tree method_type;
1084 tree parms1;
1085 tree parms2;
1087 if (TREE_CODE (fn) != TREE_CODE (method))
1088 continue;
1090 /* Two using-declarations can coexist, we'll complain about ambiguity in
1091 overload resolution. */
1092 if (via_using && iter.using_p ()
1093 /* Except handle inherited constructors specially. */
1094 && ! DECL_CONSTRUCTOR_P (fn))
1095 continue;
1097 /* [over.load] Member function declarations with the
1098 same name and the same parameter types cannot be
1099 overloaded if any of them is a static member
1100 function declaration.
1102 [over.load] Member function declarations with the same name and
1103 the same parameter-type-list as well as member function template
1104 declarations with the same name, the same parameter-type-list, and
1105 the same template parameter lists cannot be overloaded if any of
1106 them, but not all, have a ref-qualifier.
1108 [namespace.udecl] When a using-declaration brings names
1109 from a base class into a derived class scope, member
1110 functions in the derived class override and/or hide member
1111 functions with the same name and parameter types in a base
1112 class (rather than conflicting). */
1113 fn_type = TREE_TYPE (fn);
1114 method_type = TREE_TYPE (method);
1115 parms1 = TYPE_ARG_TYPES (fn_type);
1116 parms2 = TYPE_ARG_TYPES (method_type);
1118 /* Compare the quals on the 'this' parm. Don't compare
1119 the whole types, as used functions are treated as
1120 coming from the using class in overload resolution. */
1121 if (! DECL_STATIC_FUNCTION_P (fn)
1122 && ! DECL_STATIC_FUNCTION_P (method)
1123 /* Either both or neither need to be ref-qualified for
1124 differing quals to allow overloading. */
1125 && (FUNCTION_REF_QUALIFIED (fn_type)
1126 == FUNCTION_REF_QUALIFIED (method_type))
1127 && (type_memfn_quals (fn_type) != type_memfn_quals (method_type)
1128 || type_memfn_rqual (fn_type) != type_memfn_rqual (method_type)))
1129 continue;
1131 /* For templates, the return type and template parameters
1132 must be identical. */
1133 if (TREE_CODE (fn) == TEMPLATE_DECL
1134 && (!same_type_p (TREE_TYPE (fn_type),
1135 TREE_TYPE (method_type))
1136 || !comp_template_parms (DECL_TEMPLATE_PARMS (fn),
1137 DECL_TEMPLATE_PARMS (method))))
1138 continue;
1140 if (! DECL_STATIC_FUNCTION_P (fn))
1141 parms1 = TREE_CHAIN (parms1);
1142 if (! DECL_STATIC_FUNCTION_P (method))
1143 parms2 = TREE_CHAIN (parms2);
1145 /* Bring back parameters omitted from an inherited ctor. */
1146 if (ctor_omit_inherited_parms (fn))
1147 parms1 = FUNCTION_FIRST_USER_PARMTYPE (DECL_ORIGIN (fn));
1148 if (ctor_omit_inherited_parms (method))
1149 parms2 = FUNCTION_FIRST_USER_PARMTYPE (DECL_ORIGIN (method));
1151 if (compparms (parms1, parms2)
1152 && (!DECL_CONV_FN_P (fn)
1153 || same_type_p (TREE_TYPE (fn_type),
1154 TREE_TYPE (method_type)))
1155 && equivalently_constrained (fn, method))
1157 /* For function versions, their parms and types match
1158 but they are not duplicates. Record function versions
1159 as and when they are found. extern "C" functions are
1160 not treated as versions. */
1161 if (TREE_CODE (fn) == FUNCTION_DECL
1162 && TREE_CODE (method) == FUNCTION_DECL
1163 && !DECL_EXTERN_C_P (fn)
1164 && !DECL_EXTERN_C_P (method)
1165 && targetm.target_option.function_versions (fn, method))
1167 /* Mark functions as versions if necessary. Modify the mangled
1168 decl name if necessary. */
1169 if (!DECL_FUNCTION_VERSIONED (fn))
1171 DECL_FUNCTION_VERSIONED (fn) = 1;
1172 if (DECL_ASSEMBLER_NAME_SET_P (fn))
1173 mangle_decl (fn);
1175 if (!DECL_FUNCTION_VERSIONED (method))
1177 DECL_FUNCTION_VERSIONED (method) = 1;
1178 if (DECL_ASSEMBLER_NAME_SET_P (method))
1179 mangle_decl (method);
1181 cgraph_node::record_function_versions (fn, method);
1182 continue;
1185 if (DECL_INHERITED_CTOR (method))
1187 if (DECL_INHERITED_CTOR (fn))
1189 tree basem = DECL_INHERITED_CTOR_BASE (method);
1190 tree basef = DECL_INHERITED_CTOR_BASE (fn);
1191 if (flag_new_inheriting_ctors)
1193 if (basem == basef)
1195 /* Inheriting the same constructor along different
1196 paths, combine them. */
1197 SET_DECL_INHERITED_CTOR
1198 (fn, ovl_make (DECL_INHERITED_CTOR (method),
1199 DECL_INHERITED_CTOR (fn)));
1200 /* And discard the new one. */
1201 return false;
1203 else
1204 /* Inherited ctors can coexist until overload
1205 resolution. */
1206 continue;
1208 error_at (DECL_SOURCE_LOCATION (method),
1209 "%q#D conflicts with version inherited from %qT",
1210 method, basef);
1211 inform (DECL_SOURCE_LOCATION (fn),
1212 "version inherited from %qT declared here",
1213 basef);
1215 /* Otherwise defer to the other function. */
1216 return false;
1219 if (via_using)
1220 /* Defer to the local function. */
1221 return false;
1222 else if (flag_new_inheriting_ctors
1223 && DECL_INHERITED_CTOR (fn))
1225 /* Remove the inherited constructor. */
1226 current_fns = iter.remove_node (current_fns);
1227 continue;
1229 else
1231 error_at (DECL_SOURCE_LOCATION (method),
1232 "%q#D cannot be overloaded with %q#D", method, fn);
1233 inform (DECL_SOURCE_LOCATION (fn),
1234 "previous declaration %q#D", fn);
1235 return false;
1240 /* A class should never have more than one destructor. */
1241 if (current_fns && DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (method))
1242 return false;
1244 current_fns = ovl_insert (method, current_fns, via_using);
1246 if (conv_p)
1247 TYPE_HAS_CONVERSION (type) = 1;
1248 else if (!complete_p && !IDENTIFIER_CDTOR_P (DECL_NAME (method)))
1249 push_class_level_binding (DECL_NAME (method), current_fns);
1251 if (insert_p)
1253 bool reallocated;
1255 /* We only expect to add few methods in the COMPLETE_P case, so
1256 just make room for one more method in that case. */
1257 if (complete_p)
1258 reallocated = vec_safe_reserve_exact (method_vec, 1);
1259 else
1260 reallocated = vec_safe_reserve (method_vec, 1);
1261 if (reallocated)
1262 CLASSTYPE_METHOD_VEC (type) = method_vec;
1263 if (slot == method_vec->length ())
1264 method_vec->quick_push (current_fns);
1265 else
1266 method_vec->quick_insert (slot, current_fns);
1268 else
1269 /* Replace the current slot. */
1270 (*method_vec)[slot] = current_fns;
1271 return true;
1274 /* Subroutines of finish_struct. */
1276 /* Change the access of FDECL to ACCESS in T. Return 1 if change was
1277 legit, otherwise return 0. */
1279 static int
1280 alter_access (tree t, tree fdecl, tree access)
1282 tree elem;
1284 retrofit_lang_decl (fdecl);
1286 gcc_assert (!DECL_DISCRIMINATOR_P (fdecl));
1288 elem = purpose_member (t, DECL_ACCESS (fdecl));
1289 if (elem)
1291 if (TREE_VALUE (elem) != access)
1293 if (TREE_CODE (TREE_TYPE (fdecl)) == FUNCTION_DECL)
1294 error ("conflicting access specifications for method"
1295 " %q+D, ignored", TREE_TYPE (fdecl));
1296 else
1297 error ("conflicting access specifications for field %qE, ignored",
1298 DECL_NAME (fdecl));
1300 else
1302 /* They're changing the access to the same thing they changed
1303 it to before. That's OK. */
1307 else
1309 perform_or_defer_access_check (TYPE_BINFO (t), fdecl, fdecl,
1310 tf_warning_or_error);
1311 DECL_ACCESS (fdecl) = tree_cons (t, access, DECL_ACCESS (fdecl));
1312 return 1;
1314 return 0;
1317 /* Return the access node for DECL's access in its enclosing class. */
1319 tree
1320 declared_access (tree decl)
1322 return (TREE_PRIVATE (decl) ? access_private_node
1323 : TREE_PROTECTED (decl) ? access_protected_node
1324 : access_public_node);
1327 /* Process the USING_DECL, which is a member of T. */
1329 static void
1330 handle_using_decl (tree using_decl, tree t)
1332 tree decl = USING_DECL_DECLS (using_decl);
1333 tree name = DECL_NAME (using_decl);
1334 tree access = declared_access (using_decl);
1335 tree flist = NULL_TREE;
1336 tree old_value;
1338 gcc_assert (!processing_template_decl && decl);
1340 old_value = lookup_member (t, name, /*protect=*/0, /*want_type=*/false,
1341 tf_warning_or_error);
1342 if (old_value)
1344 old_value = OVL_FIRST (old_value);
1346 if (DECL_P (old_value) && DECL_CONTEXT (old_value) == t)
1347 /* OK */;
1348 else
1349 old_value = NULL_TREE;
1352 cp_emit_debug_info_for_using (decl, t);
1354 if (is_overloaded_fn (decl))
1355 flist = decl;
1357 if (! old_value)
1359 else if (is_overloaded_fn (old_value))
1361 if (flist)
1362 /* It's OK to use functions from a base when there are functions with
1363 the same name already present in the current class. */;
1364 else
1366 error_at (DECL_SOURCE_LOCATION (using_decl), "%qD invalid in %q#T "
1367 "because of local method %q#D with same name",
1368 using_decl, t, old_value);
1369 inform (DECL_SOURCE_LOCATION (old_value),
1370 "local method %q#D declared here", old_value);
1371 return;
1374 else if (!DECL_ARTIFICIAL (old_value))
1376 error_at (DECL_SOURCE_LOCATION (using_decl), "%qD invalid in %q#T "
1377 "because of local member %q#D with same name",
1378 using_decl, t, old_value);
1379 inform (DECL_SOURCE_LOCATION (old_value),
1380 "local member %q#D declared here", old_value);
1381 return;
1384 /* Make type T see field decl FDECL with access ACCESS. */
1385 if (flist)
1386 for (ovl_iterator iter (flist); iter; ++iter)
1388 add_method (t, *iter, true);
1389 alter_access (t, *iter, access);
1391 else
1392 alter_access (t, decl, access);
1395 /* Data structure for find_abi_tags_r, below. */
1397 struct abi_tag_data
1399 tree t; // The type that we're checking for missing tags.
1400 tree subob; // The subobject of T that we're getting tags from.
1401 tree tags; // error_mark_node for diagnostics, or a list of missing tags.
1404 /* Subroutine of find_abi_tags_r. Handle a single TAG found on the class TP
1405 in the context of P. TAG can be either an identifier (the DECL_NAME of
1406 a tag NAMESPACE_DECL) or a STRING_CST (a tag attribute). */
1408 static void
1409 check_tag (tree tag, tree id, tree *tp, abi_tag_data *p)
1411 if (!IDENTIFIER_MARKED (id))
1413 if (p->tags != error_mark_node)
1415 /* We're collecting tags from template arguments or from
1416 the type of a variable or function return type. */
1417 p->tags = tree_cons (NULL_TREE, tag, p->tags);
1419 /* Don't inherit this tag multiple times. */
1420 IDENTIFIER_MARKED (id) = true;
1422 if (TYPE_P (p->t))
1424 /* Tags inherited from type template arguments are only used
1425 to avoid warnings. */
1426 ABI_TAG_IMPLICIT (p->tags) = true;
1427 return;
1429 /* For functions and variables we want to warn, too. */
1432 /* Otherwise we're diagnosing missing tags. */
1433 if (TREE_CODE (p->t) == FUNCTION_DECL)
1435 if (warning (OPT_Wabi_tag, "%qD inherits the %E ABI tag "
1436 "that %qT (used in its return type) has",
1437 p->t, tag, *tp))
1438 inform (location_of (*tp), "%qT declared here", *tp);
1440 else if (VAR_P (p->t))
1442 if (warning (OPT_Wabi_tag, "%qD inherits the %E ABI tag "
1443 "that %qT (used in its type) has", p->t, tag, *tp))
1444 inform (location_of (*tp), "%qT declared here", *tp);
1446 else if (TYPE_P (p->subob))
1448 if (warning (OPT_Wabi_tag, "%qT does not have the %E ABI tag "
1449 "that base %qT has", p->t, tag, p->subob))
1450 inform (location_of (p->subob), "%qT declared here",
1451 p->subob);
1453 else
1455 if (warning (OPT_Wabi_tag, "%qT does not have the %E ABI tag "
1456 "that %qT (used in the type of %qD) has",
1457 p->t, tag, *tp, p->subob))
1459 inform (location_of (p->subob), "%qD declared here",
1460 p->subob);
1461 inform (location_of (*tp), "%qT declared here", *tp);
1467 /* Find all the ABI tags in the attribute list ATTR and either call
1468 check_tag (if TP is non-null) or set IDENTIFIER_MARKED to val. */
1470 static void
1471 mark_or_check_attr_tags (tree attr, tree *tp, abi_tag_data *p, bool val)
1473 if (!attr)
1474 return;
1475 for (; (attr = lookup_attribute ("abi_tag", attr));
1476 attr = TREE_CHAIN (attr))
1477 for (tree list = TREE_VALUE (attr); list;
1478 list = TREE_CHAIN (list))
1480 tree tag = TREE_VALUE (list);
1481 tree id = get_identifier (TREE_STRING_POINTER (tag));
1482 if (tp)
1483 check_tag (tag, id, tp, p);
1484 else
1485 IDENTIFIER_MARKED (id) = val;
1489 /* Find all the ABI tags on T and its enclosing scopes and either call
1490 check_tag (if TP is non-null) or set IDENTIFIER_MARKED to val. */
1492 static void
1493 mark_or_check_tags (tree t, tree *tp, abi_tag_data *p, bool val)
1495 while (t != global_namespace)
1497 tree attr;
1498 if (TYPE_P (t))
1500 attr = TYPE_ATTRIBUTES (t);
1501 t = CP_TYPE_CONTEXT (t);
1503 else
1505 attr = DECL_ATTRIBUTES (t);
1506 t = CP_DECL_CONTEXT (t);
1508 mark_or_check_attr_tags (attr, tp, p, val);
1512 /* walk_tree callback for check_abi_tags: if the type at *TP involves any
1513 types with ABI tags, add the corresponding identifiers to the VEC in
1514 *DATA and set IDENTIFIER_MARKED. */
1516 static tree
1517 find_abi_tags_r (tree *tp, int *walk_subtrees, void *data)
1519 if (!OVERLOAD_TYPE_P (*tp))
1520 return NULL_TREE;
1522 /* walk_tree shouldn't be walking into any subtrees of a RECORD_TYPE
1523 anyway, but let's make sure of it. */
1524 *walk_subtrees = false;
1526 abi_tag_data *p = static_cast<struct abi_tag_data*>(data);
1528 mark_or_check_tags (*tp, tp, p, false);
1530 return NULL_TREE;
1533 /* walk_tree callback for mark_abi_tags: if *TP is a class, set
1534 IDENTIFIER_MARKED on its ABI tags. */
1536 static tree
1537 mark_abi_tags_r (tree *tp, int *walk_subtrees, void *data)
1539 if (!OVERLOAD_TYPE_P (*tp))
1540 return NULL_TREE;
1542 /* walk_tree shouldn't be walking into any subtrees of a RECORD_TYPE
1543 anyway, but let's make sure of it. */
1544 *walk_subtrees = false;
1546 bool *valp = static_cast<bool*>(data);
1548 mark_or_check_tags (*tp, NULL, NULL, *valp);
1550 return NULL_TREE;
1553 /* Set IDENTIFIER_MARKED on all the ABI tags on T and its enclosing
1554 scopes. */
1556 static void
1557 mark_abi_tags (tree t, bool val)
1559 mark_or_check_tags (t, NULL, NULL, val);
1560 if (DECL_P (t))
1562 if (DECL_LANG_SPECIFIC (t) && DECL_USE_TEMPLATE (t)
1563 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (t)))
1565 /* Template arguments are part of the signature. */
1566 tree level = INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (t));
1567 for (int j = 0; j < TREE_VEC_LENGTH (level); ++j)
1569 tree arg = TREE_VEC_ELT (level, j);
1570 cp_walk_tree_without_duplicates (&arg, mark_abi_tags_r, &val);
1573 if (TREE_CODE (t) == FUNCTION_DECL)
1574 /* A function's parameter types are part of the signature, so
1575 we don't need to inherit any tags that are also in them. */
1576 for (tree arg = FUNCTION_FIRST_USER_PARMTYPE (t); arg;
1577 arg = TREE_CHAIN (arg))
1578 cp_walk_tree_without_duplicates (&TREE_VALUE (arg),
1579 mark_abi_tags_r, &val);
1583 /* Check that T has all the ABI tags that subobject SUBOB has, or
1584 warn if not. If T is a (variable or function) declaration, also
1585 return any missing tags, and add them to T if JUST_CHECKING is false. */
1587 static tree
1588 check_abi_tags (tree t, tree subob, bool just_checking = false)
1590 bool inherit = DECL_P (t);
1592 if (!inherit && !warn_abi_tag)
1593 return NULL_TREE;
1595 tree decl = TYPE_P (t) ? TYPE_NAME (t) : t;
1596 if (!TREE_PUBLIC (decl))
1597 /* No need to worry about things local to this TU. */
1598 return NULL_TREE;
1600 mark_abi_tags (t, true);
1602 tree subtype = TYPE_P (subob) ? subob : TREE_TYPE (subob);
1603 struct abi_tag_data data = { t, subob, error_mark_node };
1604 if (inherit)
1605 data.tags = NULL_TREE;
1607 cp_walk_tree_without_duplicates (&subtype, find_abi_tags_r, &data);
1609 if (!(inherit && data.tags))
1610 /* We don't need to do anything with data.tags. */;
1611 else if (just_checking)
1612 for (tree t = data.tags; t; t = TREE_CHAIN (t))
1614 tree id = get_identifier (TREE_STRING_POINTER (TREE_VALUE (t)));
1615 IDENTIFIER_MARKED (id) = false;
1617 else
1619 tree attr = lookup_attribute ("abi_tag", DECL_ATTRIBUTES (t));
1620 if (attr)
1621 TREE_VALUE (attr) = chainon (data.tags, TREE_VALUE (attr));
1622 else
1623 DECL_ATTRIBUTES (t)
1624 = tree_cons (get_identifier ("abi_tag"), data.tags,
1625 DECL_ATTRIBUTES (t));
1628 mark_abi_tags (t, false);
1630 return data.tags;
1633 /* Check that DECL has all the ABI tags that are used in parts of its type
1634 that are not reflected in its mangled name. */
1636 void
1637 check_abi_tags (tree decl)
1639 if (VAR_P (decl))
1640 check_abi_tags (decl, TREE_TYPE (decl));
1641 else if (TREE_CODE (decl) == FUNCTION_DECL
1642 && !DECL_CONV_FN_P (decl)
1643 && !mangle_return_type_p (decl))
1644 check_abi_tags (decl, TREE_TYPE (TREE_TYPE (decl)));
1647 /* Return any ABI tags that are used in parts of the type of DECL
1648 that are not reflected in its mangled name. This function is only
1649 used in backward-compatible mangling for ABI <11. */
1651 tree
1652 missing_abi_tags (tree decl)
1654 if (VAR_P (decl))
1655 return check_abi_tags (decl, TREE_TYPE (decl), true);
1656 else if (TREE_CODE (decl) == FUNCTION_DECL
1657 /* Don't check DECL_CONV_FN_P here like we do in check_abi_tags, so
1658 that we can use this function for setting need_abi_warning
1659 regardless of the current flag_abi_version. */
1660 && !mangle_return_type_p (decl))
1661 return check_abi_tags (decl, TREE_TYPE (TREE_TYPE (decl)), true);
1662 else
1663 return NULL_TREE;
1666 void
1667 inherit_targ_abi_tags (tree t)
1669 if (!CLASS_TYPE_P (t)
1670 || CLASSTYPE_TEMPLATE_INFO (t) == NULL_TREE)
1671 return;
1673 mark_abi_tags (t, true);
1675 tree args = CLASSTYPE_TI_ARGS (t);
1676 struct abi_tag_data data = { t, NULL_TREE, NULL_TREE };
1677 for (int i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
1679 tree level = TMPL_ARGS_LEVEL (args, i+1);
1680 for (int j = 0; j < TREE_VEC_LENGTH (level); ++j)
1682 tree arg = TREE_VEC_ELT (level, j);
1683 data.subob = arg;
1684 cp_walk_tree_without_duplicates (&arg, find_abi_tags_r, &data);
1688 // If we found some tags on our template arguments, add them to our
1689 // abi_tag attribute.
1690 if (data.tags)
1692 tree attr = lookup_attribute ("abi_tag", TYPE_ATTRIBUTES (t));
1693 if (attr)
1694 TREE_VALUE (attr) = chainon (data.tags, TREE_VALUE (attr));
1695 else
1696 TYPE_ATTRIBUTES (t)
1697 = tree_cons (get_identifier ("abi_tag"), data.tags,
1698 TYPE_ATTRIBUTES (t));
1701 mark_abi_tags (t, false);
1704 /* Return true, iff class T has a non-virtual destructor that is
1705 accessible from outside the class heirarchy (i.e. is public, or
1706 there's a suitable friend. */
1708 static bool
1709 accessible_nvdtor_p (tree t)
1711 tree dtor = CLASSTYPE_DESTRUCTOR (t);
1713 /* An implicitly declared destructor is always public. And,
1714 if it were virtual, we would have created it by now. */
1715 if (!dtor)
1716 return true;
1718 if (DECL_VINDEX (dtor))
1719 return false; /* Virtual */
1721 if (!TREE_PRIVATE (dtor) && !TREE_PROTECTED (dtor))
1722 return true; /* Public */
1724 if (CLASSTYPE_FRIEND_CLASSES (t)
1725 || DECL_FRIENDLIST (TYPE_MAIN_DECL (t)))
1726 return true; /* Has friends */
1728 return false;
1731 /* Run through the base classes of T, updating CANT_HAVE_CONST_CTOR_P,
1732 and NO_CONST_ASN_REF_P. Also set flag bits in T based on
1733 properties of the bases. */
1735 static void
1736 check_bases (tree t,
1737 int* cant_have_const_ctor_p,
1738 int* no_const_asn_ref_p)
1740 int i;
1741 bool seen_non_virtual_nearly_empty_base_p = 0;
1742 int seen_tm_mask = 0;
1743 tree base_binfo;
1744 tree binfo;
1745 tree field = NULL_TREE;
1747 if (!CLASSTYPE_NON_STD_LAYOUT (t))
1748 for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
1749 if (TREE_CODE (field) == FIELD_DECL)
1750 break;
1752 for (binfo = TYPE_BINFO (t), i = 0;
1753 BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
1755 tree basetype = TREE_TYPE (base_binfo);
1757 gcc_assert (COMPLETE_TYPE_P (basetype));
1759 if (CLASSTYPE_FINAL (basetype))
1760 error ("cannot derive from %<final%> base %qT in derived type %qT",
1761 basetype, t);
1763 /* If any base class is non-literal, so is the derived class. */
1764 if (!CLASSTYPE_LITERAL_P (basetype))
1765 CLASSTYPE_LITERAL_P (t) = false;
1767 /* If the base class doesn't have copy constructors or
1768 assignment operators that take const references, then the
1769 derived class cannot have such a member automatically
1770 generated. */
1771 if (TYPE_HAS_COPY_CTOR (basetype)
1772 && ! TYPE_HAS_CONST_COPY_CTOR (basetype))
1773 *cant_have_const_ctor_p = 1;
1774 if (TYPE_HAS_COPY_ASSIGN (basetype)
1775 && !TYPE_HAS_CONST_COPY_ASSIGN (basetype))
1776 *no_const_asn_ref_p = 1;
1778 if (BINFO_VIRTUAL_P (base_binfo))
1779 /* A virtual base does not effect nearly emptiness. */
1781 else if (CLASSTYPE_NEARLY_EMPTY_P (basetype))
1783 if (seen_non_virtual_nearly_empty_base_p)
1784 /* And if there is more than one nearly empty base, then the
1785 derived class is not nearly empty either. */
1786 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
1787 else
1788 /* Remember we've seen one. */
1789 seen_non_virtual_nearly_empty_base_p = 1;
1791 else if (!is_empty_class (basetype))
1792 /* If the base class is not empty or nearly empty, then this
1793 class cannot be nearly empty. */
1794 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
1796 /* A lot of properties from the bases also apply to the derived
1797 class. */
1798 TYPE_NEEDS_CONSTRUCTING (t) |= TYPE_NEEDS_CONSTRUCTING (basetype);
1799 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t)
1800 |= TYPE_HAS_NONTRIVIAL_DESTRUCTOR (basetype);
1801 TYPE_HAS_COMPLEX_COPY_ASSIGN (t)
1802 |= (TYPE_HAS_COMPLEX_COPY_ASSIGN (basetype)
1803 || !TYPE_HAS_COPY_ASSIGN (basetype));
1804 TYPE_HAS_COMPLEX_COPY_CTOR (t) |= (TYPE_HAS_COMPLEX_COPY_CTOR (basetype)
1805 || !TYPE_HAS_COPY_CTOR (basetype));
1806 TYPE_HAS_COMPLEX_MOVE_ASSIGN (t)
1807 |= TYPE_HAS_COMPLEX_MOVE_ASSIGN (basetype);
1808 TYPE_HAS_COMPLEX_MOVE_CTOR (t) |= TYPE_HAS_COMPLEX_MOVE_CTOR (basetype);
1809 TYPE_POLYMORPHIC_P (t) |= TYPE_POLYMORPHIC_P (basetype);
1810 CLASSTYPE_CONTAINS_EMPTY_CLASS_P (t)
1811 |= CLASSTYPE_CONTAINS_EMPTY_CLASS_P (basetype);
1812 TYPE_HAS_COMPLEX_DFLT (t) |= (!TYPE_HAS_DEFAULT_CONSTRUCTOR (basetype)
1813 || TYPE_HAS_COMPLEX_DFLT (basetype));
1814 SET_CLASSTYPE_READONLY_FIELDS_NEED_INIT
1815 (t, CLASSTYPE_READONLY_FIELDS_NEED_INIT (t)
1816 | CLASSTYPE_READONLY_FIELDS_NEED_INIT (basetype));
1817 SET_CLASSTYPE_REF_FIELDS_NEED_INIT
1818 (t, CLASSTYPE_REF_FIELDS_NEED_INIT (t)
1819 | CLASSTYPE_REF_FIELDS_NEED_INIT (basetype));
1820 if (TYPE_HAS_MUTABLE_P (basetype))
1821 CLASSTYPE_HAS_MUTABLE (t) = 1;
1823 /* A standard-layout class is a class that:
1825 * has no non-standard-layout base classes, */
1826 CLASSTYPE_NON_STD_LAYOUT (t) |= CLASSTYPE_NON_STD_LAYOUT (basetype);
1827 if (!CLASSTYPE_NON_STD_LAYOUT (t))
1829 tree basefield;
1830 /* ...has no base classes of the same type as the first non-static
1831 data member... */
1832 if (field && DECL_CONTEXT (field) == t
1833 && (same_type_ignoring_top_level_qualifiers_p
1834 (TREE_TYPE (field), basetype)))
1835 CLASSTYPE_NON_STD_LAYOUT (t) = 1;
1836 else
1837 /* ...either has no non-static data members in the most-derived
1838 class and at most one base class with non-static data
1839 members, or has no base classes with non-static data
1840 members */
1841 for (basefield = TYPE_FIELDS (basetype); basefield;
1842 basefield = DECL_CHAIN (basefield))
1843 if (TREE_CODE (basefield) == FIELD_DECL
1844 && !(DECL_FIELD_IS_BASE (basefield)
1845 && integer_zerop (DECL_SIZE (basefield))))
1847 if (field)
1848 CLASSTYPE_NON_STD_LAYOUT (t) = 1;
1849 else
1850 field = basefield;
1851 break;
1855 /* Don't bother collecting tm attributes if transactional memory
1856 support is not enabled. */
1857 if (flag_tm)
1859 tree tm_attr = find_tm_attribute (TYPE_ATTRIBUTES (basetype));
1860 if (tm_attr)
1861 seen_tm_mask |= tm_attr_to_mask (tm_attr);
1864 check_abi_tags (t, basetype);
1867 /* If one of the base classes had TM attributes, and the current class
1868 doesn't define its own, then the current class inherits one. */
1869 if (seen_tm_mask && !find_tm_attribute (TYPE_ATTRIBUTES (t)))
1871 tree tm_attr = tm_mask_to_attr (least_bit_hwi (seen_tm_mask));
1872 TYPE_ATTRIBUTES (t) = tree_cons (tm_attr, NULL, TYPE_ATTRIBUTES (t));
1876 /* Determine all the primary bases within T. Sets BINFO_PRIMARY_BASE_P for
1877 those that are primaries. Sets BINFO_LOST_PRIMARY_P for those
1878 that have had a nearly-empty virtual primary base stolen by some
1879 other base in the hierarchy. Determines CLASSTYPE_PRIMARY_BASE for
1880 T. */
1882 static void
1883 determine_primary_bases (tree t)
1885 unsigned i;
1886 tree primary = NULL_TREE;
1887 tree type_binfo = TYPE_BINFO (t);
1888 tree base_binfo;
1890 /* Determine the primary bases of our bases. */
1891 for (base_binfo = TREE_CHAIN (type_binfo); base_binfo;
1892 base_binfo = TREE_CHAIN (base_binfo))
1894 tree primary = CLASSTYPE_PRIMARY_BINFO (BINFO_TYPE (base_binfo));
1896 /* See if we're the non-virtual primary of our inheritance
1897 chain. */
1898 if (!BINFO_VIRTUAL_P (base_binfo))
1900 tree parent = BINFO_INHERITANCE_CHAIN (base_binfo);
1901 tree parent_primary = CLASSTYPE_PRIMARY_BINFO (BINFO_TYPE (parent));
1903 if (parent_primary
1904 && SAME_BINFO_TYPE_P (BINFO_TYPE (base_binfo),
1905 BINFO_TYPE (parent_primary)))
1906 /* We are the primary binfo. */
1907 BINFO_PRIMARY_P (base_binfo) = 1;
1909 /* Determine if we have a virtual primary base, and mark it so.
1911 if (primary && BINFO_VIRTUAL_P (primary))
1913 tree this_primary = copied_binfo (primary, base_binfo);
1915 if (BINFO_PRIMARY_P (this_primary))
1916 /* Someone already claimed this base. */
1917 BINFO_LOST_PRIMARY_P (base_binfo) = 1;
1918 else
1920 tree delta;
1922 BINFO_PRIMARY_P (this_primary) = 1;
1923 BINFO_INHERITANCE_CHAIN (this_primary) = base_binfo;
1925 /* A virtual binfo might have been copied from within
1926 another hierarchy. As we're about to use it as a
1927 primary base, make sure the offsets match. */
1928 delta = size_diffop_loc (input_location,
1929 fold_convert (ssizetype,
1930 BINFO_OFFSET (base_binfo)),
1931 fold_convert (ssizetype,
1932 BINFO_OFFSET (this_primary)));
1934 propagate_binfo_offsets (this_primary, delta);
1939 /* First look for a dynamic direct non-virtual base. */
1940 for (i = 0; BINFO_BASE_ITERATE (type_binfo, i, base_binfo); i++)
1942 tree basetype = BINFO_TYPE (base_binfo);
1944 if (TYPE_CONTAINS_VPTR_P (basetype) && !BINFO_VIRTUAL_P (base_binfo))
1946 primary = base_binfo;
1947 goto found;
1951 /* A "nearly-empty" virtual base class can be the primary base
1952 class, if no non-virtual polymorphic base can be found. Look for
1953 a nearly-empty virtual dynamic base that is not already a primary
1954 base of something in the hierarchy. If there is no such base,
1955 just pick the first nearly-empty virtual base. */
1957 for (base_binfo = TREE_CHAIN (type_binfo); base_binfo;
1958 base_binfo = TREE_CHAIN (base_binfo))
1959 if (BINFO_VIRTUAL_P (base_binfo)
1960 && CLASSTYPE_NEARLY_EMPTY_P (BINFO_TYPE (base_binfo)))
1962 if (!BINFO_PRIMARY_P (base_binfo))
1964 /* Found one that is not primary. */
1965 primary = base_binfo;
1966 goto found;
1968 else if (!primary)
1969 /* Remember the first candidate. */
1970 primary = base_binfo;
1973 found:
1974 /* If we've got a primary base, use it. */
1975 if (primary)
1977 tree basetype = BINFO_TYPE (primary);
1979 CLASSTYPE_PRIMARY_BINFO (t) = primary;
1980 if (BINFO_PRIMARY_P (primary))
1981 /* We are stealing a primary base. */
1982 BINFO_LOST_PRIMARY_P (BINFO_INHERITANCE_CHAIN (primary)) = 1;
1983 BINFO_PRIMARY_P (primary) = 1;
1984 if (BINFO_VIRTUAL_P (primary))
1986 tree delta;
1988 BINFO_INHERITANCE_CHAIN (primary) = type_binfo;
1989 /* A virtual binfo might have been copied from within
1990 another hierarchy. As we're about to use it as a primary
1991 base, make sure the offsets match. */
1992 delta = size_diffop_loc (input_location, ssize_int (0),
1993 fold_convert (ssizetype, BINFO_OFFSET (primary)));
1995 propagate_binfo_offsets (primary, delta);
1998 primary = TYPE_BINFO (basetype);
2000 TYPE_VFIELD (t) = TYPE_VFIELD (basetype);
2001 BINFO_VTABLE (type_binfo) = BINFO_VTABLE (primary);
2002 BINFO_VIRTUALS (type_binfo) = BINFO_VIRTUALS (primary);
2006 /* Update the variant types of T. */
2008 void
2009 fixup_type_variants (tree t)
2011 tree variants;
2013 if (!t)
2014 return;
2016 for (variants = TYPE_NEXT_VARIANT (t);
2017 variants;
2018 variants = TYPE_NEXT_VARIANT (variants))
2020 /* These fields are in the _TYPE part of the node, not in
2021 the TYPE_LANG_SPECIFIC component, so they are not shared. */
2022 TYPE_HAS_USER_CONSTRUCTOR (variants) = TYPE_HAS_USER_CONSTRUCTOR (t);
2023 TYPE_NEEDS_CONSTRUCTING (variants) = TYPE_NEEDS_CONSTRUCTING (t);
2024 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (variants)
2025 = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t);
2027 TYPE_POLYMORPHIC_P (variants) = TYPE_POLYMORPHIC_P (t);
2029 TYPE_BINFO (variants) = TYPE_BINFO (t);
2031 /* Copy whatever these are holding today. */
2032 TYPE_VFIELD (variants) = TYPE_VFIELD (t);
2033 TYPE_FIELDS (variants) = TYPE_FIELDS (t);
2037 /* KLASS is a class that we're applying may_alias to after the body is
2038 parsed. Fixup any POINTER_TO and REFERENCE_TO types. The
2039 canonical type(s) will be implicitly updated. */
2041 static void
2042 fixup_may_alias (tree klass)
2044 tree t, v;
2046 for (t = TYPE_POINTER_TO (klass); t; t = TYPE_NEXT_PTR_TO (t))
2047 for (v = TYPE_MAIN_VARIANT (t); v; v = TYPE_NEXT_VARIANT (v))
2048 TYPE_REF_CAN_ALIAS_ALL (v) = true;
2049 for (t = TYPE_REFERENCE_TO (klass); t; t = TYPE_NEXT_REF_TO (t))
2050 for (v = TYPE_MAIN_VARIANT (t); v; v = TYPE_NEXT_VARIANT (v))
2051 TYPE_REF_CAN_ALIAS_ALL (v) = true;
2054 /* Early variant fixups: we apply attributes at the beginning of the class
2055 definition, and we need to fix up any variants that have already been
2056 made via elaborated-type-specifier so that check_qualified_type works. */
2058 void
2059 fixup_attribute_variants (tree t)
2061 tree variants;
2063 if (!t)
2064 return;
2066 tree attrs = TYPE_ATTRIBUTES (t);
2067 unsigned align = TYPE_ALIGN (t);
2068 bool user_align = TYPE_USER_ALIGN (t);
2069 bool may_alias = lookup_attribute ("may_alias", attrs);
2071 if (may_alias)
2072 fixup_may_alias (t);
2074 for (variants = TYPE_NEXT_VARIANT (t);
2075 variants;
2076 variants = TYPE_NEXT_VARIANT (variants))
2078 /* These are the two fields that check_qualified_type looks at and
2079 are affected by attributes. */
2080 TYPE_ATTRIBUTES (variants) = attrs;
2081 unsigned valign = align;
2082 if (TYPE_USER_ALIGN (variants))
2083 valign = MAX (valign, TYPE_ALIGN (variants));
2084 else
2085 TYPE_USER_ALIGN (variants) = user_align;
2086 SET_TYPE_ALIGN (variants, valign);
2087 if (may_alias)
2088 fixup_may_alias (variants);
2092 /* Set memoizing fields and bits of T (and its variants) for later
2093 use. */
2095 static void
2096 finish_struct_bits (tree t)
2098 /* Fix up variants (if any). */
2099 fixup_type_variants (t);
2101 if (BINFO_N_BASE_BINFOS (TYPE_BINFO (t)) && TYPE_POLYMORPHIC_P (t))
2102 /* For a class w/o baseclasses, 'finish_struct' has set
2103 CLASSTYPE_PURE_VIRTUALS correctly (by definition).
2104 Similarly for a class whose base classes do not have vtables.
2105 When neither of these is true, we might have removed abstract
2106 virtuals (by providing a definition), added some (by declaring
2107 new ones), or redeclared ones from a base class. We need to
2108 recalculate what's really an abstract virtual at this point (by
2109 looking in the vtables). */
2110 get_pure_virtuals (t);
2112 /* If this type has a copy constructor or a destructor, force its
2113 mode to be BLKmode, and force its TREE_ADDRESSABLE bit to be
2114 nonzero. This will cause it to be passed by invisible reference
2115 and prevent it from being returned in a register. */
2116 if (type_has_nontrivial_copy_init (t)
2117 || TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t))
2119 tree variants;
2120 SET_DECL_MODE (TYPE_MAIN_DECL (t), BLKmode);
2121 for (variants = t; variants; variants = TYPE_NEXT_VARIANT (variants))
2123 SET_TYPE_MODE (variants, BLKmode);
2124 TREE_ADDRESSABLE (variants) = 1;
2129 /* Issue warnings about T having private constructors, but no friends,
2130 and so forth.
2132 HAS_NONPRIVATE_METHOD is nonzero if T has any non-private methods or
2133 static members. HAS_NONPRIVATE_STATIC_FN is nonzero if T has any
2134 non-private static member functions. */
2136 static void
2137 maybe_warn_about_overly_private_class (tree t)
2139 int has_member_fn = 0;
2140 int has_nonprivate_method = 0;
2142 if (!warn_ctor_dtor_privacy
2143 /* If the class has friends, those entities might create and
2144 access instances, so we should not warn. */
2145 || (CLASSTYPE_FRIEND_CLASSES (t)
2146 || DECL_FRIENDLIST (TYPE_MAIN_DECL (t)))
2147 /* We will have warned when the template was declared; there's
2148 no need to warn on every instantiation. */
2149 || CLASSTYPE_TEMPLATE_INSTANTIATION (t))
2150 /* There's no reason to even consider warning about this
2151 class. */
2152 return;
2154 /* We only issue one warning, if more than one applies, because
2155 otherwise, on code like:
2157 class A {
2158 // Oops - forgot `public:'
2159 A();
2160 A(const A&);
2161 ~A();
2164 we warn several times about essentially the same problem. */
2166 /* Check to see if all (non-constructor, non-destructor) member
2167 functions are private. (Since there are no friends or
2168 non-private statics, we can't ever call any of the private member
2169 functions.) */
2170 for (tree fn = TYPE_FIELDS (t); fn; fn = DECL_CHAIN (fn))
2171 if (!DECL_DECLARES_FUNCTION_P (fn))
2172 /* Not a function. */;
2173 else if (DECL_ARTIFICIAL (fn))
2174 /* We're not interested in compiler-generated methods; they don't
2175 provide any way to call private members. */;
2176 else if (!TREE_PRIVATE (fn))
2178 if (DECL_STATIC_FUNCTION_P (fn))
2179 /* A non-private static member function is just like a
2180 friend; it can create and invoke private member
2181 functions, and be accessed without a class
2182 instance. */
2183 return;
2185 has_nonprivate_method = 1;
2186 /* Keep searching for a static member function. */
2188 else if (!DECL_CONSTRUCTOR_P (fn) && !DECL_DESTRUCTOR_P (fn))
2189 has_member_fn = 1;
2191 if (!has_nonprivate_method && has_member_fn)
2193 /* There are no non-private methods, and there's at least one
2194 private member function that isn't a constructor or
2195 destructor. (If all the private members are
2196 constructors/destructors we want to use the code below that
2197 issues error messages specifically referring to
2198 constructors/destructors.) */
2199 unsigned i;
2200 tree binfo = TYPE_BINFO (t);
2202 for (i = 0; i != BINFO_N_BASE_BINFOS (binfo); i++)
2203 if (BINFO_BASE_ACCESS (binfo, i) != access_private_node)
2205 has_nonprivate_method = 1;
2206 break;
2208 if (!has_nonprivate_method)
2210 warning (OPT_Wctor_dtor_privacy,
2211 "all member functions in class %qT are private", t);
2212 return;
2216 /* Even if some of the member functions are non-private, the class
2217 won't be useful for much if all the constructors or destructors
2218 are private: such an object can never be created or destroyed. */
2219 if (tree dtor = CLASSTYPE_DESTRUCTOR (t))
2220 if (TREE_PRIVATE (dtor))
2222 warning (OPT_Wctor_dtor_privacy,
2223 "%q#T only defines a private destructor and has no friends",
2225 return;
2228 /* Warn about classes that have private constructors and no friends. */
2229 if (TYPE_HAS_USER_CONSTRUCTOR (t)
2230 /* Implicitly generated constructors are always public. */
2231 && !CLASSTYPE_LAZY_DEFAULT_CTOR (t))
2233 bool nonprivate_ctor = false;
2234 tree copy_or_move = NULL_TREE;
2236 /* If a non-template class does not define a copy
2237 constructor, one is defined for it, enabling it to avoid
2238 this warning. For a template class, this does not
2239 happen, and so we would normally get a warning on:
2241 template <class T> class C { private: C(); };
2243 To avoid this asymmetry, we check TYPE_HAS_COPY_CTOR. All
2244 complete non-template or fully instantiated classes have this
2245 flag set. */
2246 if (!TYPE_HAS_COPY_CTOR (t))
2247 nonprivate_ctor = true;
2248 else
2249 for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (t));
2250 !nonprivate_ctor && iter; ++iter)
2251 if (TREE_PRIVATE (*iter))
2252 continue;
2253 else if (copy_fn_p (*iter) || move_fn_p (*iter))
2254 /* Ideally, we wouldn't count any constructor that takes
2255 an argument of the class type as a parameter, because
2256 such things cannot be used to construct an instance of
2257 the class unless you already have one. */
2258 copy_or_move = *iter;
2259 else
2260 nonprivate_ctor = true;
2262 if (!nonprivate_ctor)
2264 warning (OPT_Wctor_dtor_privacy,
2265 "%q#T only defines private constructors and has no friends",
2267 if (copy_or_move)
2268 inform (DECL_SOURCE_LOCATION (copy_or_move),
2269 "%q#D is public, but requires an existing %q#T object",
2270 copy_or_move, t);
2271 return;
2276 static struct {
2277 gt_pointer_operator new_value;
2278 void *cookie;
2279 } resort_data;
2281 /* Comparison function to compare two TYPE_METHOD_VEC entries by name. */
2283 static int
2284 method_name_cmp (const void* m1_p, const void* m2_p)
2286 const tree *const m1 = (const tree *) m1_p;
2287 const tree *const m2 = (const tree *) m2_p;
2289 if (*m1 == NULL_TREE && *m2 == NULL_TREE)
2290 return 0;
2291 if (*m1 == NULL_TREE)
2292 return -1;
2293 if (*m2 == NULL_TREE)
2294 return 1;
2295 if (OVL_NAME (*m1) < OVL_NAME (*m2))
2296 return -1;
2297 return 1;
2300 /* This routine compares two fields like method_name_cmp but using the
2301 pointer operator in resort_field_decl_data. */
2303 static int
2304 resort_method_name_cmp (const void* m1_p, const void* m2_p)
2306 const tree *const m1 = (const tree *) m1_p;
2307 const tree *const m2 = (const tree *) m2_p;
2308 if (*m1 == NULL_TREE && *m2 == NULL_TREE)
2309 return 0;
2310 if (*m1 == NULL_TREE)
2311 return -1;
2312 if (*m2 == NULL_TREE)
2313 return 1;
2315 tree d1 = OVL_NAME (*m1);
2316 tree d2 = OVL_NAME (*m2);
2317 resort_data.new_value (&d1, resort_data.cookie);
2318 resort_data.new_value (&d2, resort_data.cookie);
2319 if (d1 < d2)
2320 return -1;
2322 return 1;
2325 /* Resort TYPE_METHOD_VEC because pointers have been reordered. */
2327 void
2328 resort_type_method_vec (void* obj,
2329 void* /*orig_obj*/,
2330 gt_pointer_operator new_value,
2331 void* cookie)
2333 if (vec<tree, va_gc> *method_vec = (vec<tree, va_gc> *) obj)
2335 int len = method_vec->length ();
2336 int slot;
2338 /* The type conversion ops have to live at the front of the vec, so we
2339 can't sort them. */
2340 for (slot = CLASSTYPE_FIRST_CONVERSION_SLOT;
2341 slot < len; slot++)
2342 if (!DECL_CONV_FN_P (OVL_FIRST ((*method_vec)[slot])))
2343 break;
2345 if (len > slot + 1)
2347 resort_data.new_value = new_value;
2348 resort_data.cookie = cookie;
2349 qsort (method_vec->address () + slot, len - slot, sizeof (tree),
2350 resort_method_name_cmp);
2355 /* Warn about duplicate methods in fn_fields.
2357 Sort methods that are not special (i.e., constructors, destructors,
2358 and type conversion operators) so that we can find them faster in
2359 search. */
2361 static void
2362 finish_struct_methods (tree t)
2364 vec<tree, va_gc> *method_vec;
2365 int slot, len;
2367 method_vec = CLASSTYPE_METHOD_VEC (t);
2368 if (!method_vec)
2369 return;
2371 len = method_vec->length ();
2373 /* Clear DECL_IN_AGGR_P for all functions. */
2374 for (tree fn = TYPE_FIELDS (t); fn; fn = DECL_CHAIN (fn))
2375 if (DECL_DECLARES_FUNCTION_P (fn))
2376 DECL_IN_AGGR_P (fn) = false;
2378 /* Issue warnings about private constructors and such. If there are
2379 no methods, then some public defaults are generated. */
2380 maybe_warn_about_overly_private_class (t);
2382 /* The type conversion ops have to live at the front of the vec, so we
2383 can't sort them. */
2384 tree fn_fields;
2385 for (slot = CLASSTYPE_FIRST_CONVERSION_SLOT;
2386 method_vec->iterate (slot, &fn_fields);
2387 ++slot)
2388 if (!DECL_CONV_FN_P (OVL_FIRST (fn_fields)))
2389 break;
2390 if (len - slot > 1)
2391 qsort (method_vec->address () + slot,
2392 len-slot, sizeof (tree), method_name_cmp);
2395 /* Make BINFO's vtable have N entries, including RTTI entries,
2396 vbase and vcall offsets, etc. Set its type and call the back end
2397 to lay it out. */
2399 static void
2400 layout_vtable_decl (tree binfo, int n)
2402 tree atype;
2403 tree vtable;
2405 atype = build_array_of_n_type (vtable_entry_type, n);
2406 layout_type (atype);
2408 /* We may have to grow the vtable. */
2409 vtable = get_vtbl_decl_for_binfo (binfo);
2410 if (!same_type_p (TREE_TYPE (vtable), atype))
2412 TREE_TYPE (vtable) = atype;
2413 DECL_SIZE (vtable) = DECL_SIZE_UNIT (vtable) = NULL_TREE;
2414 layout_decl (vtable, 0);
2418 /* True iff FNDECL and BASE_FNDECL (both non-static member functions)
2419 have the same signature. */
2422 same_signature_p (const_tree fndecl, const_tree base_fndecl)
2424 /* One destructor overrides another if they are the same kind of
2425 destructor. */
2426 if (DECL_DESTRUCTOR_P (base_fndecl) && DECL_DESTRUCTOR_P (fndecl)
2427 && special_function_p (base_fndecl) == special_function_p (fndecl))
2428 return 1;
2429 /* But a non-destructor never overrides a destructor, nor vice
2430 versa, nor do different kinds of destructors override
2431 one-another. For example, a complete object destructor does not
2432 override a deleting destructor. */
2433 if (DECL_DESTRUCTOR_P (base_fndecl) || DECL_DESTRUCTOR_P (fndecl))
2434 return 0;
2436 if (DECL_NAME (fndecl) == DECL_NAME (base_fndecl)
2437 || (DECL_CONV_FN_P (fndecl)
2438 && DECL_CONV_FN_P (base_fndecl)
2439 && same_type_p (DECL_CONV_FN_TYPE (fndecl),
2440 DECL_CONV_FN_TYPE (base_fndecl))))
2442 tree fntype = TREE_TYPE (fndecl);
2443 tree base_fntype = TREE_TYPE (base_fndecl);
2444 if (type_memfn_quals (fntype) == type_memfn_quals (base_fntype)
2445 && type_memfn_rqual (fntype) == type_memfn_rqual (base_fntype)
2446 && compparms (FUNCTION_FIRST_USER_PARMTYPE (fndecl),
2447 FUNCTION_FIRST_USER_PARMTYPE (base_fndecl)))
2448 return 1;
2450 return 0;
2453 /* Returns TRUE if DERIVED is a binfo containing the binfo BASE as a
2454 subobject. */
2456 static bool
2457 base_derived_from (tree derived, tree base)
2459 tree probe;
2461 for (probe = base; probe; probe = BINFO_INHERITANCE_CHAIN (probe))
2463 if (probe == derived)
2464 return true;
2465 else if (BINFO_VIRTUAL_P (probe))
2466 /* If we meet a virtual base, we can't follow the inheritance
2467 any more. See if the complete type of DERIVED contains
2468 such a virtual base. */
2469 return (binfo_for_vbase (BINFO_TYPE (probe), BINFO_TYPE (derived))
2470 != NULL_TREE);
2472 return false;
2475 struct find_final_overrider_data {
2476 /* The function for which we are trying to find a final overrider. */
2477 tree fn;
2478 /* The base class in which the function was declared. */
2479 tree declaring_base;
2480 /* The candidate overriders. */
2481 tree candidates;
2482 /* Path to most derived. */
2483 vec<tree> path;
2486 /* Add the overrider along the current path to FFOD->CANDIDATES.
2487 Returns true if an overrider was found; false otherwise. */
2489 static bool
2490 dfs_find_final_overrider_1 (tree binfo,
2491 find_final_overrider_data *ffod,
2492 unsigned depth)
2494 tree method;
2496 /* If BINFO is not the most derived type, try a more derived class.
2497 A definition there will overrider a definition here. */
2498 if (depth)
2500 depth--;
2501 if (dfs_find_final_overrider_1
2502 (ffod->path[depth], ffod, depth))
2503 return true;
2506 method = look_for_overrides_here (BINFO_TYPE (binfo), ffod->fn);
2507 if (method)
2509 tree *candidate = &ffod->candidates;
2511 /* Remove any candidates overridden by this new function. */
2512 while (*candidate)
2514 /* If *CANDIDATE overrides METHOD, then METHOD
2515 cannot override anything else on the list. */
2516 if (base_derived_from (TREE_VALUE (*candidate), binfo))
2517 return true;
2518 /* If METHOD overrides *CANDIDATE, remove *CANDIDATE. */
2519 if (base_derived_from (binfo, TREE_VALUE (*candidate)))
2520 *candidate = TREE_CHAIN (*candidate);
2521 else
2522 candidate = &TREE_CHAIN (*candidate);
2525 /* Add the new function. */
2526 ffod->candidates = tree_cons (method, binfo, ffod->candidates);
2527 return true;
2530 return false;
2533 /* Called from find_final_overrider via dfs_walk. */
2535 static tree
2536 dfs_find_final_overrider_pre (tree binfo, void *data)
2538 find_final_overrider_data *ffod = (find_final_overrider_data *) data;
2540 if (binfo == ffod->declaring_base)
2541 dfs_find_final_overrider_1 (binfo, ffod, ffod->path.length ());
2542 ffod->path.safe_push (binfo);
2544 return NULL_TREE;
2547 static tree
2548 dfs_find_final_overrider_post (tree /*binfo*/, void *data)
2550 find_final_overrider_data *ffod = (find_final_overrider_data *) data;
2551 ffod->path.pop ();
2553 return NULL_TREE;
2556 /* Returns a TREE_LIST whose TREE_PURPOSE is the final overrider for
2557 FN and whose TREE_VALUE is the binfo for the base where the
2558 overriding occurs. BINFO (in the hierarchy dominated by the binfo
2559 DERIVED) is the base object in which FN is declared. */
2561 static tree
2562 find_final_overrider (tree derived, tree binfo, tree fn)
2564 find_final_overrider_data ffod;
2566 /* Getting this right is a little tricky. This is valid:
2568 struct S { virtual void f (); };
2569 struct T { virtual void f (); };
2570 struct U : public S, public T { };
2572 even though calling `f' in `U' is ambiguous. But,
2574 struct R { virtual void f(); };
2575 struct S : virtual public R { virtual void f (); };
2576 struct T : virtual public R { virtual void f (); };
2577 struct U : public S, public T { };
2579 is not -- there's no way to decide whether to put `S::f' or
2580 `T::f' in the vtable for `R'.
2582 The solution is to look at all paths to BINFO. If we find
2583 different overriders along any two, then there is a problem. */
2584 if (DECL_THUNK_P (fn))
2585 fn = THUNK_TARGET (fn);
2587 /* Determine the depth of the hierarchy. */
2588 ffod.fn = fn;
2589 ffod.declaring_base = binfo;
2590 ffod.candidates = NULL_TREE;
2591 ffod.path.create (30);
2593 dfs_walk_all (derived, dfs_find_final_overrider_pre,
2594 dfs_find_final_overrider_post, &ffod);
2596 ffod.path.release ();
2598 /* If there was no winner, issue an error message. */
2599 if (!ffod.candidates || TREE_CHAIN (ffod.candidates))
2600 return error_mark_node;
2602 return ffod.candidates;
2605 /* Return the index of the vcall offset for FN when TYPE is used as a
2606 virtual base. */
2608 static tree
2609 get_vcall_index (tree fn, tree type)
2611 vec<tree_pair_s, va_gc> *indices = CLASSTYPE_VCALL_INDICES (type);
2612 tree_pair_p p;
2613 unsigned ix;
2615 FOR_EACH_VEC_SAFE_ELT (indices, ix, p)
2616 if ((DECL_DESTRUCTOR_P (fn) && DECL_DESTRUCTOR_P (p->purpose))
2617 || same_signature_p (fn, p->purpose))
2618 return p->value;
2620 /* There should always be an appropriate index. */
2621 gcc_unreachable ();
2624 /* Update an entry in the vtable for BINFO, which is in the hierarchy
2625 dominated by T. FN is the old function; VIRTUALS points to the
2626 corresponding position in the new BINFO_VIRTUALS list. IX is the index
2627 of that entry in the list. */
2629 static void
2630 update_vtable_entry_for_fn (tree t, tree binfo, tree fn, tree* virtuals,
2631 unsigned ix)
2633 tree b;
2634 tree overrider;
2635 tree delta;
2636 tree virtual_base;
2637 tree first_defn;
2638 tree overrider_fn, overrider_target;
2639 tree target_fn = DECL_THUNK_P (fn) ? THUNK_TARGET (fn) : fn;
2640 tree over_return, base_return;
2641 bool lost = false;
2643 /* Find the nearest primary base (possibly binfo itself) which defines
2644 this function; this is the class the caller will convert to when
2645 calling FN through BINFO. */
2646 for (b = binfo; ; b = get_primary_binfo (b))
2648 gcc_assert (b);
2649 if (look_for_overrides_here (BINFO_TYPE (b), target_fn))
2650 break;
2652 /* The nearest definition is from a lost primary. */
2653 if (BINFO_LOST_PRIMARY_P (b))
2654 lost = true;
2656 first_defn = b;
2658 /* Find the final overrider. */
2659 overrider = find_final_overrider (TYPE_BINFO (t), b, target_fn);
2660 if (overrider == error_mark_node)
2662 error ("no unique final overrider for %qD in %qT", target_fn, t);
2663 return;
2665 overrider_target = overrider_fn = TREE_PURPOSE (overrider);
2667 /* Check for adjusting covariant return types. */
2668 over_return = TREE_TYPE (TREE_TYPE (overrider_target));
2669 base_return = TREE_TYPE (TREE_TYPE (target_fn));
2671 if (POINTER_TYPE_P (over_return)
2672 && TREE_CODE (over_return) == TREE_CODE (base_return)
2673 && CLASS_TYPE_P (TREE_TYPE (over_return))
2674 && CLASS_TYPE_P (TREE_TYPE (base_return))
2675 /* If the overrider is invalid, don't even try. */
2676 && !DECL_INVALID_OVERRIDER_P (overrider_target))
2678 /* If FN is a covariant thunk, we must figure out the adjustment
2679 to the final base FN was converting to. As OVERRIDER_TARGET might
2680 also be converting to the return type of FN, we have to
2681 combine the two conversions here. */
2682 tree fixed_offset, virtual_offset;
2684 over_return = TREE_TYPE (over_return);
2685 base_return = TREE_TYPE (base_return);
2687 if (DECL_THUNK_P (fn))
2689 gcc_assert (DECL_RESULT_THUNK_P (fn));
2690 fixed_offset = ssize_int (THUNK_FIXED_OFFSET (fn));
2691 virtual_offset = THUNK_VIRTUAL_OFFSET (fn);
2693 else
2694 fixed_offset = virtual_offset = NULL_TREE;
2696 if (virtual_offset)
2697 /* Find the equivalent binfo within the return type of the
2698 overriding function. We will want the vbase offset from
2699 there. */
2700 virtual_offset = binfo_for_vbase (BINFO_TYPE (virtual_offset),
2701 over_return);
2702 else if (!same_type_ignoring_top_level_qualifiers_p
2703 (over_return, base_return))
2705 /* There was no existing virtual thunk (which takes
2706 precedence). So find the binfo of the base function's
2707 return type within the overriding function's return type.
2708 Fortunately we know the covariancy is valid (it
2709 has already been checked), so we can just iterate along
2710 the binfos, which have been chained in inheritance graph
2711 order. Of course it is lame that we have to repeat the
2712 search here anyway -- we should really be caching pieces
2713 of the vtable and avoiding this repeated work. */
2714 tree thunk_binfo, base_binfo;
2716 /* Find the base binfo within the overriding function's
2717 return type. We will always find a thunk_binfo, except
2718 when the covariancy is invalid (which we will have
2719 already diagnosed). */
2720 for (base_binfo = TYPE_BINFO (base_return),
2721 thunk_binfo = TYPE_BINFO (over_return);
2722 thunk_binfo;
2723 thunk_binfo = TREE_CHAIN (thunk_binfo))
2724 if (SAME_BINFO_TYPE_P (BINFO_TYPE (thunk_binfo),
2725 BINFO_TYPE (base_binfo)))
2726 break;
2728 /* See if virtual inheritance is involved. */
2729 for (virtual_offset = thunk_binfo;
2730 virtual_offset;
2731 virtual_offset = BINFO_INHERITANCE_CHAIN (virtual_offset))
2732 if (BINFO_VIRTUAL_P (virtual_offset))
2733 break;
2735 if (virtual_offset
2736 || (thunk_binfo && !BINFO_OFFSET_ZEROP (thunk_binfo)))
2738 tree offset = fold_convert (ssizetype, BINFO_OFFSET (thunk_binfo));
2740 if (virtual_offset)
2742 /* We convert via virtual base. Adjust the fixed
2743 offset to be from there. */
2744 offset =
2745 size_diffop (offset,
2746 fold_convert (ssizetype,
2747 BINFO_OFFSET (virtual_offset)));
2749 if (fixed_offset)
2750 /* There was an existing fixed offset, this must be
2751 from the base just converted to, and the base the
2752 FN was thunking to. */
2753 fixed_offset = size_binop (PLUS_EXPR, fixed_offset, offset);
2754 else
2755 fixed_offset = offset;
2759 if (fixed_offset || virtual_offset)
2760 /* Replace the overriding function with a covariant thunk. We
2761 will emit the overriding function in its own slot as
2762 well. */
2763 overrider_fn = make_thunk (overrider_target, /*this_adjusting=*/0,
2764 fixed_offset, virtual_offset);
2766 else
2767 gcc_assert (DECL_INVALID_OVERRIDER_P (overrider_target) ||
2768 !DECL_THUNK_P (fn));
2770 /* If we need a covariant thunk, then we may need to adjust first_defn.
2771 The ABI specifies that the thunks emitted with a function are
2772 determined by which bases the function overrides, so we need to be
2773 sure that we're using a thunk for some overridden base; even if we
2774 know that the necessary this adjustment is zero, there may not be an
2775 appropriate zero-this-adjustment thunk for us to use since thunks for
2776 overriding virtual bases always use the vcall offset.
2778 Furthermore, just choosing any base that overrides this function isn't
2779 quite right, as this slot won't be used for calls through a type that
2780 puts a covariant thunk here. Calling the function through such a type
2781 will use a different slot, and that slot is the one that determines
2782 the thunk emitted for that base.
2784 So, keep looking until we find the base that we're really overriding
2785 in this slot: the nearest primary base that doesn't use a covariant
2786 thunk in this slot. */
2787 if (overrider_target != overrider_fn)
2789 if (BINFO_TYPE (b) == DECL_CONTEXT (overrider_target))
2790 /* We already know that the overrider needs a covariant thunk. */
2791 b = get_primary_binfo (b);
2792 for (; ; b = get_primary_binfo (b))
2794 tree main_binfo = TYPE_BINFO (BINFO_TYPE (b));
2795 tree bv = chain_index (ix, BINFO_VIRTUALS (main_binfo));
2796 if (!DECL_THUNK_P (TREE_VALUE (bv)))
2797 break;
2798 if (BINFO_LOST_PRIMARY_P (b))
2799 lost = true;
2801 first_defn = b;
2804 /* Assume that we will produce a thunk that convert all the way to
2805 the final overrider, and not to an intermediate virtual base. */
2806 virtual_base = NULL_TREE;
2808 /* See if we can convert to an intermediate virtual base first, and then
2809 use the vcall offset located there to finish the conversion. */
2810 for (; b; b = BINFO_INHERITANCE_CHAIN (b))
2812 /* If we find the final overrider, then we can stop
2813 walking. */
2814 if (SAME_BINFO_TYPE_P (BINFO_TYPE (b),
2815 BINFO_TYPE (TREE_VALUE (overrider))))
2816 break;
2818 /* If we find a virtual base, and we haven't yet found the
2819 overrider, then there is a virtual base between the
2820 declaring base (first_defn) and the final overrider. */
2821 if (BINFO_VIRTUAL_P (b))
2823 virtual_base = b;
2824 break;
2828 /* Compute the constant adjustment to the `this' pointer. The
2829 `this' pointer, when this function is called, will point at BINFO
2830 (or one of its primary bases, which are at the same offset). */
2831 if (virtual_base)
2832 /* The `this' pointer needs to be adjusted from the declaration to
2833 the nearest virtual base. */
2834 delta = size_diffop_loc (input_location,
2835 fold_convert (ssizetype, BINFO_OFFSET (virtual_base)),
2836 fold_convert (ssizetype, BINFO_OFFSET (first_defn)));
2837 else if (lost)
2838 /* If the nearest definition is in a lost primary, we don't need an
2839 entry in our vtable. Except possibly in a constructor vtable,
2840 if we happen to get our primary back. In that case, the offset
2841 will be zero, as it will be a primary base. */
2842 delta = size_zero_node;
2843 else
2844 /* The `this' pointer needs to be adjusted from pointing to
2845 BINFO to pointing at the base where the final overrider
2846 appears. */
2847 delta = size_diffop_loc (input_location,
2848 fold_convert (ssizetype,
2849 BINFO_OFFSET (TREE_VALUE (overrider))),
2850 fold_convert (ssizetype, BINFO_OFFSET (binfo)));
2852 modify_vtable_entry (t, binfo, overrider_fn, delta, virtuals);
2854 if (virtual_base)
2855 BV_VCALL_INDEX (*virtuals)
2856 = get_vcall_index (overrider_target, BINFO_TYPE (virtual_base));
2857 else
2858 BV_VCALL_INDEX (*virtuals) = NULL_TREE;
2860 BV_LOST_PRIMARY (*virtuals) = lost;
2863 /* Called from modify_all_vtables via dfs_walk. */
2865 static tree
2866 dfs_modify_vtables (tree binfo, void* data)
2868 tree t = (tree) data;
2869 tree virtuals;
2870 tree old_virtuals;
2871 unsigned ix;
2873 if (!TYPE_CONTAINS_VPTR_P (BINFO_TYPE (binfo)))
2874 /* A base without a vtable needs no modification, and its bases
2875 are uninteresting. */
2876 return dfs_skip_bases;
2878 if (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), t)
2879 && !CLASSTYPE_HAS_PRIMARY_BASE_P (t))
2880 /* Don't do the primary vtable, if it's new. */
2881 return NULL_TREE;
2883 if (BINFO_PRIMARY_P (binfo) && !BINFO_VIRTUAL_P (binfo))
2884 /* There's no need to modify the vtable for a non-virtual primary
2885 base; we're not going to use that vtable anyhow. We do still
2886 need to do this for virtual primary bases, as they could become
2887 non-primary in a construction vtable. */
2888 return NULL_TREE;
2890 make_new_vtable (t, binfo);
2892 /* Now, go through each of the virtual functions in the virtual
2893 function table for BINFO. Find the final overrider, and update
2894 the BINFO_VIRTUALS list appropriately. */
2895 for (ix = 0, virtuals = BINFO_VIRTUALS (binfo),
2896 old_virtuals = BINFO_VIRTUALS (TYPE_BINFO (BINFO_TYPE (binfo)));
2897 virtuals;
2898 ix++, virtuals = TREE_CHAIN (virtuals),
2899 old_virtuals = TREE_CHAIN (old_virtuals))
2900 update_vtable_entry_for_fn (t,
2901 binfo,
2902 BV_FN (old_virtuals),
2903 &virtuals, ix);
2905 return NULL_TREE;
2908 /* Update all of the primary and secondary vtables for T. Create new
2909 vtables as required, and initialize their RTTI information. Each
2910 of the functions in VIRTUALS is declared in T and may override a
2911 virtual function from a base class; find and modify the appropriate
2912 entries to point to the overriding functions. Returns a list, in
2913 declaration order, of the virtual functions that are declared in T,
2914 but do not appear in the primary base class vtable, and which
2915 should therefore be appended to the end of the vtable for T. */
2917 static tree
2918 modify_all_vtables (tree t, tree virtuals)
2920 tree binfo = TYPE_BINFO (t);
2921 tree *fnsp;
2923 /* Mangle the vtable name before entering dfs_walk (c++/51884). */
2924 if (TYPE_CONTAINS_VPTR_P (t))
2925 get_vtable_decl (t, false);
2927 /* Update all of the vtables. */
2928 dfs_walk_once (binfo, dfs_modify_vtables, NULL, t);
2930 /* Add virtual functions not already in our primary vtable. These
2931 will be both those introduced by this class, and those overridden
2932 from secondary bases. It does not include virtuals merely
2933 inherited from secondary bases. */
2934 for (fnsp = &virtuals; *fnsp; )
2936 tree fn = TREE_VALUE (*fnsp);
2938 if (!value_member (fn, BINFO_VIRTUALS (binfo))
2939 || DECL_VINDEX (fn) == error_mark_node)
2941 /* We don't need to adjust the `this' pointer when
2942 calling this function. */
2943 BV_DELTA (*fnsp) = integer_zero_node;
2944 BV_VCALL_INDEX (*fnsp) = NULL_TREE;
2946 /* This is a function not already in our vtable. Keep it. */
2947 fnsp = &TREE_CHAIN (*fnsp);
2949 else
2950 /* We've already got an entry for this function. Skip it. */
2951 *fnsp = TREE_CHAIN (*fnsp);
2954 return virtuals;
2957 /* Get the base virtual function declarations in T that have the
2958 indicated NAME. */
2960 static void
2961 get_basefndecls (tree name, tree t, vec<tree> *base_fndecls)
2963 bool found_decls = false;
2965 /* Find virtual functions in T with the indicated NAME. */
2966 for (ovl_iterator iter (lookup_fnfields_slot (t, name)); iter; ++iter)
2968 tree method = *iter;
2970 if (TREE_CODE (method) == FUNCTION_DECL && DECL_VINDEX (method))
2972 base_fndecls->safe_push (method);
2973 found_decls = true;
2977 if (found_decls)
2978 return;
2980 int n_baseclasses = BINFO_N_BASE_BINFOS (TYPE_BINFO (t));
2981 for (int i = 0; i < n_baseclasses; i++)
2983 tree basetype = BINFO_TYPE (BINFO_BASE_BINFO (TYPE_BINFO (t), i));
2984 get_basefndecls (name, basetype, base_fndecls);
2988 /* If this declaration supersedes the declaration of
2989 a method declared virtual in the base class, then
2990 mark this field as being virtual as well. */
2992 void
2993 check_for_override (tree decl, tree ctype)
2995 bool overrides_found = false;
2996 if (TREE_CODE (decl) == TEMPLATE_DECL)
2997 /* In [temp.mem] we have:
2999 A specialization of a member function template does not
3000 override a virtual function from a base class. */
3001 return;
3002 if ((DECL_DESTRUCTOR_P (decl)
3003 || IDENTIFIER_VIRTUAL_P (DECL_NAME (decl))
3004 || DECL_CONV_FN_P (decl))
3005 && look_for_overrides (ctype, decl)
3006 && !DECL_STATIC_FUNCTION_P (decl))
3007 /* Set DECL_VINDEX to a value that is neither an INTEGER_CST nor
3008 the error_mark_node so that we know it is an overriding
3009 function. */
3011 DECL_VINDEX (decl) = decl;
3012 overrides_found = true;
3013 if (warn_override && !DECL_OVERRIDE_P (decl)
3014 && !DECL_DESTRUCTOR_P (decl))
3015 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wsuggest_override,
3016 "%qD can be marked override", decl);
3019 if (DECL_VIRTUAL_P (decl))
3021 if (!DECL_VINDEX (decl))
3022 DECL_VINDEX (decl) = error_mark_node;
3023 IDENTIFIER_VIRTUAL_P (DECL_NAME (decl)) = 1;
3024 if (DECL_DESTRUCTOR_P (decl))
3025 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (ctype) = true;
3027 else if (DECL_FINAL_P (decl))
3028 error ("%q+#D marked %<final%>, but is not virtual", decl);
3029 if (DECL_OVERRIDE_P (decl) && !overrides_found)
3030 error ("%q+#D marked %<override%>, but does not override", decl);
3033 /* Warn about hidden virtual functions that are not overridden in t.
3034 We know that constructors and destructors don't apply. */
3036 static void
3037 warn_hidden (tree t)
3039 vec<tree, va_gc> *method_vec = CLASSTYPE_METHOD_VEC (t);
3040 tree fns;
3041 size_t i;
3043 /* We go through each separately named virtual function. */
3044 for (i = CLASSTYPE_FIRST_CONVERSION_SLOT;
3045 vec_safe_iterate (method_vec, i, &fns);
3046 ++i)
3048 tree fndecl;
3049 tree base_binfo;
3050 tree binfo;
3051 int j;
3053 /* All functions in this slot in the CLASSTYPE_METHOD_VEC will
3054 have the same name. Figure out what name that is. */
3055 tree name = OVL_NAME (fns);
3056 /* There are no possibly hidden functions yet. */
3057 auto_vec<tree, 20> base_fndecls;
3058 /* Iterate through all of the base classes looking for possibly
3059 hidden functions. */
3060 for (binfo = TYPE_BINFO (t), j = 0;
3061 BINFO_BASE_ITERATE (binfo, j, base_binfo); j++)
3063 tree basetype = BINFO_TYPE (base_binfo);
3064 get_basefndecls (name, basetype, &base_fndecls);
3067 /* If there are no functions to hide, continue. */
3068 if (base_fndecls.is_empty ())
3069 continue;
3071 /* Remove any overridden functions. */
3072 for (ovl_iterator iter (fns); iter; ++iter)
3074 fndecl = *iter;
3075 if (TREE_CODE (fndecl) == FUNCTION_DECL
3076 && DECL_VINDEX (fndecl))
3078 /* If the method from the base class has the same
3079 signature as the method from the derived class, it
3080 has been overridden. */
3081 for (size_t k = 0; k < base_fndecls.length (); k++)
3082 if (base_fndecls[k]
3083 && same_signature_p (fndecl, base_fndecls[k]))
3084 base_fndecls[k] = NULL_TREE;
3088 /* Now give a warning for all base functions without overriders,
3089 as they are hidden. */
3090 size_t k;
3091 tree base_fndecl;
3092 FOR_EACH_VEC_ELT (base_fndecls, k, base_fndecl)
3093 if (base_fndecl)
3095 /* Here we know it is a hider, and no overrider exists. */
3096 warning_at (location_of (base_fndecl),
3097 OPT_Woverloaded_virtual,
3098 "%qD was hidden", base_fndecl);
3099 warning_at (location_of (fns),
3100 OPT_Woverloaded_virtual, " by %qD", fns);
3105 /* Recursive helper for finish_struct_anon. */
3107 static void
3108 finish_struct_anon_r (tree field, bool complain)
3110 bool is_union = TREE_CODE (TREE_TYPE (field)) == UNION_TYPE;
3111 tree elt = TYPE_FIELDS (TREE_TYPE (field));
3112 for (; elt; elt = DECL_CHAIN (elt))
3114 /* We're generally only interested in entities the user
3115 declared, but we also find nested classes by noticing
3116 the TYPE_DECL that we create implicitly. You're
3117 allowed to put one anonymous union inside another,
3118 though, so we explicitly tolerate that. We use
3119 TYPE_UNNAMED_P rather than ANON_AGGR_TYPE_P so that
3120 we also allow unnamed types used for defining fields. */
3121 if (DECL_ARTIFICIAL (elt)
3122 && (!DECL_IMPLICIT_TYPEDEF_P (elt)
3123 || TYPE_UNNAMED_P (TREE_TYPE (elt))))
3124 continue;
3126 if (TREE_CODE (elt) != FIELD_DECL)
3128 /* We already complained about static data members in
3129 finish_static_data_member_decl. */
3130 if (complain && !VAR_P (elt))
3132 if (is_union)
3133 permerror (DECL_SOURCE_LOCATION (elt),
3134 "%q#D invalid; an anonymous union can "
3135 "only have non-static data members", elt);
3136 else
3137 permerror (DECL_SOURCE_LOCATION (elt),
3138 "%q#D invalid; an anonymous struct can "
3139 "only have non-static data members", elt);
3141 continue;
3144 if (complain)
3146 if (TREE_PRIVATE (elt))
3148 if (is_union)
3149 permerror (DECL_SOURCE_LOCATION (elt),
3150 "private member %q#D in anonymous union", elt);
3151 else
3152 permerror (DECL_SOURCE_LOCATION (elt),
3153 "private member %q#D in anonymous struct", elt);
3155 else if (TREE_PROTECTED (elt))
3157 if (is_union)
3158 permerror (DECL_SOURCE_LOCATION (elt),
3159 "protected member %q#D in anonymous union", elt);
3160 else
3161 permerror (DECL_SOURCE_LOCATION (elt),
3162 "protected member %q#D in anonymous struct", elt);
3166 TREE_PRIVATE (elt) = TREE_PRIVATE (field);
3167 TREE_PROTECTED (elt) = TREE_PROTECTED (field);
3169 /* Recurse into the anonymous aggregates to handle correctly
3170 access control (c++/24926):
3172 class A {
3173 union {
3174 union {
3175 int i;
3180 int j=A().i; */
3181 if (DECL_NAME (elt) == NULL_TREE
3182 && ANON_AGGR_TYPE_P (TREE_TYPE (elt)))
3183 finish_struct_anon_r (elt, /*complain=*/false);
3187 /* Check for things that are invalid. There are probably plenty of other
3188 things we should check for also. */
3190 static void
3191 finish_struct_anon (tree t)
3193 for (tree field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
3195 if (TREE_STATIC (field))
3196 continue;
3197 if (TREE_CODE (field) != FIELD_DECL)
3198 continue;
3200 if (DECL_NAME (field) == NULL_TREE
3201 && ANON_AGGR_TYPE_P (TREE_TYPE (field)))
3202 finish_struct_anon_r (field, /*complain=*/true);
3206 /* Add T to CLASSTYPE_DECL_LIST of current_class_type which
3207 will be used later during class template instantiation.
3208 When FRIEND_P is zero, T can be a static member data (VAR_DECL),
3209 a non-static member data (FIELD_DECL), a member function
3210 (FUNCTION_DECL), a nested type (RECORD_TYPE, ENUM_TYPE),
3211 a typedef (TYPE_DECL) or a member class template (TEMPLATE_DECL)
3212 When FRIEND_P is nonzero, T is either a friend class
3213 (RECORD_TYPE, TEMPLATE_DECL) or a friend function
3214 (FUNCTION_DECL, TEMPLATE_DECL). */
3216 void
3217 maybe_add_class_template_decl_list (tree type, tree t, int friend_p)
3219 /* Save some memory by not creating TREE_LIST if TYPE is not template. */
3220 if (CLASSTYPE_TEMPLATE_INFO (type))
3221 CLASSTYPE_DECL_LIST (type)
3222 = tree_cons (friend_p ? NULL_TREE : type,
3223 t, CLASSTYPE_DECL_LIST (type));
3226 /* This function is called from declare_virt_assop_and_dtor via
3227 dfs_walk_all.
3229 DATA is a type that direcly or indirectly inherits the base
3230 represented by BINFO. If BINFO contains a virtual assignment [copy
3231 assignment or move assigment] operator or a virtual constructor,
3232 declare that function in DATA if it hasn't been already declared. */
3234 static tree
3235 dfs_declare_virt_assop_and_dtor (tree binfo, void *data)
3237 tree bv, fn, t = (tree)data;
3238 tree opname = cp_assignment_operator_id (NOP_EXPR);
3240 gcc_assert (t && CLASS_TYPE_P (t));
3241 gcc_assert (binfo && TREE_CODE (binfo) == TREE_BINFO);
3243 if (!TYPE_CONTAINS_VPTR_P (BINFO_TYPE (binfo)))
3244 /* A base without a vtable needs no modification, and its bases
3245 are uninteresting. */
3246 return dfs_skip_bases;
3248 if (BINFO_PRIMARY_P (binfo))
3249 /* If this is a primary base, then we have already looked at the
3250 virtual functions of its vtable. */
3251 return NULL_TREE;
3253 for (bv = BINFO_VIRTUALS (binfo); bv; bv = TREE_CHAIN (bv))
3255 fn = BV_FN (bv);
3257 if (DECL_NAME (fn) == opname)
3259 if (CLASSTYPE_LAZY_COPY_ASSIGN (t))
3260 lazily_declare_fn (sfk_copy_assignment, t);
3261 if (CLASSTYPE_LAZY_MOVE_ASSIGN (t))
3262 lazily_declare_fn (sfk_move_assignment, t);
3264 else if (DECL_DESTRUCTOR_P (fn)
3265 && CLASSTYPE_LAZY_DESTRUCTOR (t))
3266 lazily_declare_fn (sfk_destructor, t);
3269 return NULL_TREE;
3272 /* If the class type T has a direct or indirect base that contains a
3273 virtual assignment operator or a virtual destructor, declare that
3274 function in T if it hasn't been already declared. */
3276 static void
3277 declare_virt_assop_and_dtor (tree t)
3279 if (!(TYPE_POLYMORPHIC_P (t)
3280 && (CLASSTYPE_LAZY_COPY_ASSIGN (t)
3281 || CLASSTYPE_LAZY_MOVE_ASSIGN (t)
3282 || CLASSTYPE_LAZY_DESTRUCTOR (t))))
3283 return;
3285 dfs_walk_all (TYPE_BINFO (t),
3286 dfs_declare_virt_assop_and_dtor,
3287 NULL, t);
3290 /* Declare the inheriting constructor for class T inherited from base
3291 constructor CTOR with the parameter array PARMS of size NPARMS. */
3293 static void
3294 one_inheriting_sig (tree t, tree ctor, tree *parms, int nparms)
3296 gcc_assert (TYPE_MAIN_VARIANT (t) == t);
3298 /* We don't declare an inheriting ctor that would be a default,
3299 copy or move ctor for derived or base. */
3300 if (nparms == 0)
3301 return;
3302 if (nparms == 1
3303 && TREE_CODE (parms[0]) == REFERENCE_TYPE)
3305 tree parm = TYPE_MAIN_VARIANT (TREE_TYPE (parms[0]));
3306 if (parm == t || parm == DECL_CONTEXT (ctor))
3307 return;
3310 tree parmlist = void_list_node;
3311 for (int i = nparms - 1; i >= 0; i--)
3312 parmlist = tree_cons (NULL_TREE, parms[i], parmlist);
3313 tree fn = implicitly_declare_fn (sfk_inheriting_constructor,
3314 t, false, ctor, parmlist);
3316 if (add_method (t, fn, false))
3318 DECL_CHAIN (fn) = TYPE_FIELDS (t);
3319 TYPE_FIELDS (t) = fn;
3323 /* Declare all the inheriting constructors for class T inherited from base
3324 constructor CTOR. */
3326 static void
3327 one_inherited_ctor (tree ctor, tree t, tree using_decl)
3329 tree parms = FUNCTION_FIRST_USER_PARMTYPE (ctor);
3331 if (flag_new_inheriting_ctors)
3333 ctor = implicitly_declare_fn (sfk_inheriting_constructor,
3334 t, /*const*/false, ctor, parms);
3335 add_method (t, ctor, using_decl != NULL_TREE);
3336 TYPE_HAS_USER_CONSTRUCTOR (t) = true;
3337 return;
3340 tree *new_parms = XALLOCAVEC (tree, list_length (parms));
3341 int i = 0;
3342 for (; parms && parms != void_list_node; parms = TREE_CHAIN (parms))
3344 if (TREE_PURPOSE (parms))
3345 one_inheriting_sig (t, ctor, new_parms, i);
3346 new_parms[i++] = TREE_VALUE (parms);
3348 one_inheriting_sig (t, ctor, new_parms, i);
3349 if (parms == NULL_TREE)
3351 if (warning (OPT_Winherited_variadic_ctor,
3352 "the ellipsis in %qD is not inherited", ctor))
3353 inform (DECL_SOURCE_LOCATION (ctor), "%qD declared here", ctor);
3357 /* Create default constructors, assignment operators, and so forth for
3358 the type indicated by T, if they are needed. CANT_HAVE_CONST_CTOR,
3359 and CANT_HAVE_CONST_ASSIGNMENT are nonzero if, for whatever reason,
3360 the class cannot have a default constructor, copy constructor
3361 taking a const reference argument, or an assignment operator taking
3362 a const reference, respectively. */
3364 static void
3365 add_implicitly_declared_members (tree t, tree* access_decls,
3366 int cant_have_const_cctor,
3367 int cant_have_const_assignment)
3369 /* Destructor. */
3370 if (!CLASSTYPE_DESTRUCTOR (t))
3371 /* In general, we create destructors lazily. */
3372 CLASSTYPE_LAZY_DESTRUCTOR (t) = 1;
3374 bool move_ok = false;
3375 if (cxx_dialect >= cxx11 && CLASSTYPE_LAZY_DESTRUCTOR (t)
3376 && !TYPE_HAS_COPY_CTOR (t) && !TYPE_HAS_COPY_ASSIGN (t)
3377 && !classtype_has_move_assign_or_move_ctor_p (t, false))
3378 move_ok = true;
3380 /* [class.ctor]
3382 If there is no user-declared constructor for a class, a default
3383 constructor is implicitly declared. */
3384 if (! TYPE_HAS_USER_CONSTRUCTOR (t))
3386 TYPE_HAS_DEFAULT_CONSTRUCTOR (t) = 1;
3387 CLASSTYPE_LAZY_DEFAULT_CTOR (t) = 1;
3388 if (cxx_dialect >= cxx11)
3389 TYPE_HAS_CONSTEXPR_CTOR (t)
3390 /* Don't force the declaration to get a hard answer; if the
3391 definition would have made the class non-literal, it will still be
3392 non-literal because of the base or member in question, and that
3393 gives a better diagnostic. */
3394 = type_maybe_constexpr_default_constructor (t);
3397 /* [class.ctor]
3399 If a class definition does not explicitly declare a copy
3400 constructor, one is declared implicitly. */
3401 if (! TYPE_HAS_COPY_CTOR (t))
3403 TYPE_HAS_COPY_CTOR (t) = 1;
3404 TYPE_HAS_CONST_COPY_CTOR (t) = !cant_have_const_cctor;
3405 CLASSTYPE_LAZY_COPY_CTOR (t) = 1;
3406 if (move_ok)
3407 CLASSTYPE_LAZY_MOVE_CTOR (t) = 1;
3410 /* If there is no assignment operator, one will be created if and
3411 when it is needed. For now, just record whether or not the type
3412 of the parameter to the assignment operator will be a const or
3413 non-const reference. */
3414 if (!TYPE_HAS_COPY_ASSIGN (t))
3416 TYPE_HAS_COPY_ASSIGN (t) = 1;
3417 TYPE_HAS_CONST_COPY_ASSIGN (t) = !cant_have_const_assignment;
3418 CLASSTYPE_LAZY_COPY_ASSIGN (t) = 1;
3419 if (move_ok && !LAMBDA_TYPE_P (t))
3420 CLASSTYPE_LAZY_MOVE_ASSIGN (t) = 1;
3423 /* We can't be lazy about declaring functions that might override
3424 a virtual function from a base class. */
3425 declare_virt_assop_and_dtor (t);
3427 while (*access_decls)
3429 tree using_decl = TREE_VALUE (*access_decls);
3430 tree decl = USING_DECL_DECLS (using_decl);
3431 if (DECL_NAME (using_decl) == ctor_identifier)
3433 /* declare, then remove the decl */
3434 tree ctor_list = decl;
3435 location_t loc = input_location;
3436 input_location = DECL_SOURCE_LOCATION (using_decl);
3437 for (ovl_iterator iter (ctor_list); iter; ++iter)
3438 one_inherited_ctor (*iter, t, using_decl);
3439 *access_decls = TREE_CHAIN (*access_decls);
3440 input_location = loc;
3442 else
3443 access_decls = &TREE_CHAIN (*access_decls);
3447 /* Subroutine of insert_into_classtype_sorted_fields. Recursively
3448 count the number of fields in TYPE, including anonymous union
3449 members. */
3451 static int
3452 count_fields (tree fields)
3454 tree x;
3455 int n_fields = 0;
3456 for (x = fields; x; x = DECL_CHAIN (x))
3458 if (DECL_DECLARES_FUNCTION_P (x))
3459 /* Functions are dealt with separately. */;
3460 else if (TREE_CODE (x) == FIELD_DECL && ANON_AGGR_TYPE_P (TREE_TYPE (x)))
3461 n_fields += count_fields (TYPE_FIELDS (TREE_TYPE (x)));
3462 else
3463 n_fields += 1;
3465 return n_fields;
3468 /* Subroutine of insert_into_classtype_sorted_fields. Recursively add
3469 all the fields in the TREE_LIST FIELDS to the SORTED_FIELDS_TYPE
3470 elts, starting at offset IDX. */
3472 static int
3473 add_fields_to_record_type (tree fields, struct sorted_fields_type *field_vec, int idx)
3475 tree x;
3476 for (x = fields; x; x = DECL_CHAIN (x))
3478 if (DECL_DECLARES_FUNCTION_P (x))
3479 /* Functions are handled separately. */;
3480 else if (TREE_CODE (x) == FIELD_DECL && ANON_AGGR_TYPE_P (TREE_TYPE (x)))
3481 idx = add_fields_to_record_type (TYPE_FIELDS (TREE_TYPE (x)), field_vec, idx);
3482 else
3483 field_vec->elts[idx++] = x;
3485 return idx;
3488 /* Add all of the enum values of ENUMTYPE, to the FIELD_VEC elts,
3489 starting at offset IDX. */
3491 static int
3492 add_enum_fields_to_record_type (tree enumtype,
3493 struct sorted_fields_type *field_vec,
3494 int idx)
3496 tree values;
3497 for (values = TYPE_VALUES (enumtype); values; values = TREE_CHAIN (values))
3498 field_vec->elts[idx++] = TREE_VALUE (values);
3499 return idx;
3502 /* FIELD is a bit-field. We are finishing the processing for its
3503 enclosing type. Issue any appropriate messages and set appropriate
3504 flags. Returns false if an error has been diagnosed. */
3506 static bool
3507 check_bitfield_decl (tree field)
3509 tree type = TREE_TYPE (field);
3510 tree w;
3512 /* Extract the declared width of the bitfield, which has been
3513 temporarily stashed in DECL_INITIAL. */
3514 w = DECL_INITIAL (field);
3515 gcc_assert (w != NULL_TREE);
3516 /* Remove the bit-field width indicator so that the rest of the
3517 compiler does not treat that value as an initializer. */
3518 DECL_INITIAL (field) = NULL_TREE;
3520 /* Detect invalid bit-field type. */
3521 if (!INTEGRAL_OR_ENUMERATION_TYPE_P (type))
3523 error ("bit-field %q+#D with non-integral type", field);
3524 w = error_mark_node;
3526 else
3528 location_t loc = input_location;
3529 /* Avoid the non_lvalue wrapper added by fold for PLUS_EXPRs. */
3530 STRIP_NOPS (w);
3532 /* detect invalid field size. */
3533 input_location = DECL_SOURCE_LOCATION (field);
3534 w = cxx_constant_value (w);
3535 input_location = loc;
3537 if (TREE_CODE (w) != INTEGER_CST)
3539 error ("bit-field %q+D width not an integer constant", field);
3540 w = error_mark_node;
3542 else if (tree_int_cst_sgn (w) < 0)
3544 error ("negative width in bit-field %q+D", field);
3545 w = error_mark_node;
3547 else if (integer_zerop (w) && DECL_NAME (field) != 0)
3549 error ("zero width for bit-field %q+D", field);
3550 w = error_mark_node;
3552 else if ((TREE_CODE (type) != ENUMERAL_TYPE
3553 && TREE_CODE (type) != BOOLEAN_TYPE
3554 && compare_tree_int (w, TYPE_PRECISION (type)) > 0)
3555 || ((TREE_CODE (type) == ENUMERAL_TYPE
3556 || TREE_CODE (type) == BOOLEAN_TYPE)
3557 && tree_int_cst_lt (TYPE_SIZE (type), w)))
3558 warning_at (DECL_SOURCE_LOCATION (field), 0,
3559 "width of %qD exceeds its type", field);
3560 else if (TREE_CODE (type) == ENUMERAL_TYPE
3561 && (0 > (compare_tree_int
3562 (w, TYPE_PRECISION (ENUM_UNDERLYING_TYPE (type))))))
3563 warning_at (DECL_SOURCE_LOCATION (field), 0,
3564 "%qD is too small to hold all values of %q#T",
3565 field, type);
3568 if (w != error_mark_node)
3570 DECL_SIZE (field) = fold_convert (bitsizetype, w);
3571 DECL_BIT_FIELD (field) = 1;
3572 return true;
3574 else
3576 /* Non-bit-fields are aligned for their type. */
3577 DECL_BIT_FIELD (field) = 0;
3578 CLEAR_DECL_C_BIT_FIELD (field);
3579 return false;
3583 /* FIELD is a non bit-field. We are finishing the processing for its
3584 enclosing type T. Issue any appropriate messages and set appropriate
3585 flags. */
3587 static bool
3588 check_field_decl (tree field,
3589 tree t,
3590 int* cant_have_const_ctor,
3591 int* no_const_asn_ref)
3593 tree type = strip_array_types (TREE_TYPE (field));
3594 bool any_default_members = false;
3596 /* In C++98 an anonymous union cannot contain any fields which would change
3597 the settings of CANT_HAVE_CONST_CTOR and friends. */
3598 if (ANON_UNION_TYPE_P (type) && cxx_dialect < cxx11)
3600 /* And, we don't set TYPE_HAS_CONST_COPY_CTOR, etc., for anonymous
3601 structs. So, we recurse through their fields here. */
3602 else if (ANON_AGGR_TYPE_P (type))
3604 for (tree fields = TYPE_FIELDS (type); fields;
3605 fields = DECL_CHAIN (fields))
3606 if (TREE_CODE (fields) == FIELD_DECL && !DECL_C_BIT_FIELD (field))
3607 any_default_members |= check_field_decl (fields, t,
3608 cant_have_const_ctor,
3609 no_const_asn_ref);
3611 /* Check members with class type for constructors, destructors,
3612 etc. */
3613 else if (CLASS_TYPE_P (type))
3615 /* Never let anything with uninheritable virtuals
3616 make it through without complaint. */
3617 abstract_virtuals_error (field, type);
3619 if (TREE_CODE (t) == UNION_TYPE && cxx_dialect < cxx11)
3621 static bool warned;
3622 int oldcount = errorcount;
3623 if (TYPE_NEEDS_CONSTRUCTING (type))
3624 error ("member %q+#D with constructor not allowed in union",
3625 field);
3626 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
3627 error ("member %q+#D with destructor not allowed in union", field);
3628 if (TYPE_HAS_COMPLEX_COPY_ASSIGN (type))
3629 error ("member %q+#D with copy assignment operator not allowed in union",
3630 field);
3631 if (!warned && errorcount > oldcount)
3633 inform (DECL_SOURCE_LOCATION (field), "unrestricted unions "
3634 "only available with -std=c++11 or -std=gnu++11");
3635 warned = true;
3638 else
3640 TYPE_NEEDS_CONSTRUCTING (t) |= TYPE_NEEDS_CONSTRUCTING (type);
3641 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t)
3642 |= TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type);
3643 TYPE_HAS_COMPLEX_COPY_ASSIGN (t)
3644 |= (TYPE_HAS_COMPLEX_COPY_ASSIGN (type)
3645 || !TYPE_HAS_COPY_ASSIGN (type));
3646 TYPE_HAS_COMPLEX_COPY_CTOR (t) |= (TYPE_HAS_COMPLEX_COPY_CTOR (type)
3647 || !TYPE_HAS_COPY_CTOR (type));
3648 TYPE_HAS_COMPLEX_MOVE_ASSIGN (t) |= TYPE_HAS_COMPLEX_MOVE_ASSIGN (type);
3649 TYPE_HAS_COMPLEX_MOVE_CTOR (t) |= TYPE_HAS_COMPLEX_MOVE_CTOR (type);
3650 TYPE_HAS_COMPLEX_DFLT (t) |= (!TYPE_HAS_DEFAULT_CONSTRUCTOR (type)
3651 || TYPE_HAS_COMPLEX_DFLT (type));
3654 if (TYPE_HAS_COPY_CTOR (type)
3655 && !TYPE_HAS_CONST_COPY_CTOR (type))
3656 *cant_have_const_ctor = 1;
3658 if (TYPE_HAS_COPY_ASSIGN (type)
3659 && !TYPE_HAS_CONST_COPY_ASSIGN (type))
3660 *no_const_asn_ref = 1;
3663 check_abi_tags (t, field);
3665 if (DECL_INITIAL (field) != NULL_TREE)
3666 /* `build_class_init_list' does not recognize
3667 non-FIELD_DECLs. */
3668 any_default_members = true;
3670 return any_default_members;
3673 /* Check the data members (both static and non-static), class-scoped
3674 typedefs, etc., appearing in the declaration of T. Issue
3675 appropriate diagnostics. Sets ACCESS_DECLS to a list (in
3676 declaration order) of access declarations; each TREE_VALUE in this
3677 list is a USING_DECL.
3679 In addition, set the following flags:
3681 EMPTY_P
3682 The class is empty, i.e., contains no non-static data members.
3684 CANT_HAVE_CONST_CTOR_P
3685 This class cannot have an implicitly generated copy constructor
3686 taking a const reference.
3688 CANT_HAVE_CONST_ASN_REF
3689 This class cannot have an implicitly generated assignment
3690 operator taking a const reference.
3692 All of these flags should be initialized before calling this
3693 function.
3695 Returns a pointer to the end of the TYPE_FIELDs chain; additional
3696 fields can be added by adding to this chain. */
3698 static void
3699 check_field_decls (tree t, tree *access_decls,
3700 int *cant_have_const_ctor_p,
3701 int *no_const_asn_ref_p)
3703 tree *field;
3704 tree *next;
3705 bool has_pointers;
3706 bool any_default_members;
3707 int cant_pack = 0;
3708 int field_access = -1;
3710 /* Assume there are no access declarations. */
3711 *access_decls = NULL_TREE;
3712 /* Assume this class has no pointer members. */
3713 has_pointers = false;
3714 /* Assume none of the members of this class have default
3715 initializations. */
3716 any_default_members = false;
3718 for (field = &TYPE_FIELDS (t); *field; field = next)
3720 tree x = *field;
3721 tree type = TREE_TYPE (x);
3722 int this_field_access;
3724 next = &DECL_CHAIN (x);
3726 if (TREE_CODE (x) == USING_DECL)
3728 /* Save the access declarations for our caller. */
3729 *access_decls = tree_cons (NULL_TREE, x, *access_decls);
3730 continue;
3733 if (TREE_CODE (x) == TYPE_DECL
3734 || TREE_CODE (x) == TEMPLATE_DECL)
3735 continue;
3737 if (TREE_CODE (x) == FUNCTION_DECL)
3738 /* FIXME: We should fold in the checking from check_methods. */
3739 continue;
3741 /* If we've gotten this far, it's a data member, possibly static,
3742 or an enumerator. */
3743 if (TREE_CODE (x) != CONST_DECL)
3744 DECL_CONTEXT (x) = t;
3746 /* When this goes into scope, it will be a non-local reference. */
3747 DECL_NONLOCAL (x) = 1;
3749 if (TREE_CODE (t) == UNION_TYPE)
3751 /* [class.union] (C++98)
3753 If a union contains a static data member, or a member of
3754 reference type, the program is ill-formed.
3756 In C++11 [class.union] says:
3757 If a union contains a non-static data member of reference type
3758 the program is ill-formed. */
3759 if (VAR_P (x) && cxx_dialect < cxx11)
3761 error ("in C++98 %q+D may not be static because it is "
3762 "a member of a union", x);
3763 continue;
3765 if (TREE_CODE (type) == REFERENCE_TYPE
3766 && TREE_CODE (x) == FIELD_DECL)
3768 error ("non-static data member %q+D in a union may not "
3769 "have reference type %qT", x, type);
3770 continue;
3774 /* Perform error checking that did not get done in
3775 grokdeclarator. */
3776 if (TREE_CODE (type) == FUNCTION_TYPE)
3778 error ("field %q+D invalidly declared function type", x);
3779 type = build_pointer_type (type);
3780 TREE_TYPE (x) = type;
3782 else if (TREE_CODE (type) == METHOD_TYPE)
3784 error ("field %q+D invalidly declared method type", x);
3785 type = build_pointer_type (type);
3786 TREE_TYPE (x) = type;
3789 if (type == error_mark_node)
3790 continue;
3792 if (TREE_CODE (x) == CONST_DECL || VAR_P (x))
3793 continue;
3795 /* Now it can only be a FIELD_DECL. */
3797 if (TREE_PRIVATE (x) || TREE_PROTECTED (x))
3798 CLASSTYPE_NON_AGGREGATE (t) = 1;
3800 /* If at least one non-static data member is non-literal, the whole
3801 class becomes non-literal. Per Core/1453, volatile non-static
3802 data members and base classes are also not allowed.
3803 Note: if the type is incomplete we will complain later on. */
3804 if (COMPLETE_TYPE_P (type)
3805 && (!literal_type_p (type) || CP_TYPE_VOLATILE_P (type)))
3806 CLASSTYPE_LITERAL_P (t) = false;
3808 /* A standard-layout class is a class that:
3810 has the same access control (Clause 11) for all non-static data members,
3811 ... */
3812 this_field_access = TREE_PROTECTED (x) ? 1 : TREE_PRIVATE (x) ? 2 : 0;
3813 if (field_access == -1)
3814 field_access = this_field_access;
3815 else if (this_field_access != field_access)
3816 CLASSTYPE_NON_STD_LAYOUT (t) = 1;
3818 /* If this is of reference type, check if it needs an init. */
3819 if (TREE_CODE (type) == REFERENCE_TYPE)
3821 CLASSTYPE_NON_LAYOUT_POD_P (t) = 1;
3822 CLASSTYPE_NON_STD_LAYOUT (t) = 1;
3823 if (DECL_INITIAL (x) == NULL_TREE)
3824 SET_CLASSTYPE_REF_FIELDS_NEED_INIT (t, 1);
3825 if (cxx_dialect < cxx11)
3827 /* ARM $12.6.2: [A member initializer list] (or, for an
3828 aggregate, initialization by a brace-enclosed list) is the
3829 only way to initialize nonstatic const and reference
3830 members. */
3831 TYPE_HAS_COMPLEX_COPY_ASSIGN (t) = 1;
3832 TYPE_HAS_COMPLEX_MOVE_ASSIGN (t) = 1;
3836 type = strip_array_types (type);
3838 if (TYPE_PACKED (t))
3840 if (!layout_pod_type_p (type) && !TYPE_PACKED (type))
3842 warning_at
3843 (DECL_SOURCE_LOCATION (x), 0,
3844 "ignoring packed attribute because of unpacked non-POD field %q#D",
3846 cant_pack = 1;
3848 else if (DECL_C_BIT_FIELD (x)
3849 || TYPE_ALIGN (TREE_TYPE (x)) > BITS_PER_UNIT)
3850 DECL_PACKED (x) = 1;
3853 if (DECL_C_BIT_FIELD (x) && integer_zerop (DECL_INITIAL (x)))
3854 /* We don't treat zero-width bitfields as making a class
3855 non-empty. */
3857 else
3859 /* The class is non-empty. */
3860 CLASSTYPE_EMPTY_P (t) = 0;
3861 /* The class is not even nearly empty. */
3862 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
3863 /* If one of the data members contains an empty class,
3864 so does T. */
3865 if (CLASS_TYPE_P (type)
3866 && CLASSTYPE_CONTAINS_EMPTY_CLASS_P (type))
3867 CLASSTYPE_CONTAINS_EMPTY_CLASS_P (t) = 1;
3870 /* This is used by -Weffc++ (see below). Warn only for pointers
3871 to members which might hold dynamic memory. So do not warn
3872 for pointers to functions or pointers to members. */
3873 if (TYPE_PTR_P (type)
3874 && !TYPE_PTRFN_P (type))
3875 has_pointers = true;
3877 if (CLASS_TYPE_P (type))
3879 if (CLASSTYPE_REF_FIELDS_NEED_INIT (type))
3880 SET_CLASSTYPE_REF_FIELDS_NEED_INIT (t, 1);
3881 if (CLASSTYPE_READONLY_FIELDS_NEED_INIT (type))
3882 SET_CLASSTYPE_READONLY_FIELDS_NEED_INIT (t, 1);
3885 if (DECL_MUTABLE_P (x) || TYPE_HAS_MUTABLE_P (type))
3886 CLASSTYPE_HAS_MUTABLE (t) = 1;
3888 if (DECL_MUTABLE_P (x))
3890 if (CP_TYPE_CONST_P (type))
3892 error ("member %q+D cannot be declared both %<const%> "
3893 "and %<mutable%>", x);
3894 continue;
3896 if (TREE_CODE (type) == REFERENCE_TYPE)
3898 error ("member %q+D cannot be declared as a %<mutable%> "
3899 "reference", x);
3900 continue;
3904 if (! layout_pod_type_p (type))
3905 /* DR 148 now allows pointers to members (which are POD themselves),
3906 to be allowed in POD structs. */
3907 CLASSTYPE_NON_LAYOUT_POD_P (t) = 1;
3909 if (!std_layout_type_p (type))
3910 CLASSTYPE_NON_STD_LAYOUT (t) = 1;
3912 if (! zero_init_p (type))
3913 CLASSTYPE_NON_ZERO_INIT_P (t) = 1;
3915 /* We set DECL_C_BIT_FIELD in grokbitfield.
3916 If the type and width are valid, we'll also set DECL_BIT_FIELD. */
3917 if ((! DECL_C_BIT_FIELD (x) || ! check_bitfield_decl (x))
3918 && check_field_decl (x, t,
3919 cant_have_const_ctor_p,
3920 no_const_asn_ref_p))
3922 if (any_default_members
3923 && TREE_CODE (t) == UNION_TYPE)
3924 error ("multiple fields in union %qT initialized", t);
3925 any_default_members = true;
3928 /* Now that we've removed bit-field widths from DECL_INITIAL,
3929 anything left in DECL_INITIAL is an NSDMI that makes the class
3930 non-aggregate in C++11. */
3931 if (DECL_INITIAL (x) && cxx_dialect < cxx14)
3932 CLASSTYPE_NON_AGGREGATE (t) = true;
3934 /* If any field is const, the structure type is pseudo-const. */
3935 if (CP_TYPE_CONST_P (type))
3937 C_TYPE_FIELDS_READONLY (t) = 1;
3938 if (DECL_INITIAL (x) == NULL_TREE)
3939 SET_CLASSTYPE_READONLY_FIELDS_NEED_INIT (t, 1);
3940 if (cxx_dialect < cxx11)
3942 /* ARM $12.6.2: [A member initializer list] (or, for an
3943 aggregate, initialization by a brace-enclosed list) is the
3944 only way to initialize nonstatic const and reference
3945 members. */
3946 TYPE_HAS_COMPLEX_COPY_ASSIGN (t) = 1;
3947 TYPE_HAS_COMPLEX_MOVE_ASSIGN (t) = 1;
3950 /* A field that is pseudo-const makes the structure likewise. */
3951 else if (CLASS_TYPE_P (type))
3953 C_TYPE_FIELDS_READONLY (t) |= C_TYPE_FIELDS_READONLY (type);
3954 SET_CLASSTYPE_READONLY_FIELDS_NEED_INIT (t,
3955 CLASSTYPE_READONLY_FIELDS_NEED_INIT (t)
3956 | CLASSTYPE_READONLY_FIELDS_NEED_INIT (type));
3959 /* Core issue 80: A nonstatic data member is required to have a
3960 different name from the class iff the class has a
3961 user-declared constructor. */
3962 if (constructor_name_p (DECL_NAME (x), t)
3963 && TYPE_HAS_USER_CONSTRUCTOR (t))
3964 permerror (DECL_SOURCE_LOCATION (x),
3965 "field %q#D with same name as class", x);
3968 /* Effective C++ rule 11: if a class has dynamic memory held by pointers,
3969 it should also define a copy constructor and an assignment operator to
3970 implement the correct copy semantic (deep vs shallow, etc.). As it is
3971 not feasible to check whether the constructors do allocate dynamic memory
3972 and store it within members, we approximate the warning like this:
3974 -- Warn only if there are members which are pointers
3975 -- Warn only if there is a non-trivial constructor (otherwise,
3976 there cannot be memory allocated).
3977 -- Warn only if there is a non-trivial destructor. We assume that the
3978 user at least implemented the cleanup correctly, and a destructor
3979 is needed to free dynamic memory.
3981 This seems enough for practical purposes. */
3982 if (warn_ecpp
3983 && has_pointers
3984 && TYPE_HAS_USER_CONSTRUCTOR (t)
3985 && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t)
3986 && !(TYPE_HAS_COPY_CTOR (t) && TYPE_HAS_COPY_ASSIGN (t)))
3988 warning (OPT_Weffc__, "%q#T has pointer data members", t);
3990 if (! TYPE_HAS_COPY_CTOR (t))
3992 warning (OPT_Weffc__,
3993 " but does not override %<%T(const %T&)%>", t, t);
3994 if (!TYPE_HAS_COPY_ASSIGN (t))
3995 warning (OPT_Weffc__, " or %<operator=(const %T&)%>", t);
3997 else if (! TYPE_HAS_COPY_ASSIGN (t))
3998 warning (OPT_Weffc__,
3999 " but does not override %<operator=(const %T&)%>", t);
4002 /* Non-static data member initializers make the default constructor
4003 non-trivial. */
4004 if (any_default_members)
4006 TYPE_NEEDS_CONSTRUCTING (t) = true;
4007 TYPE_HAS_COMPLEX_DFLT (t) = true;
4010 /* If any of the fields couldn't be packed, unset TYPE_PACKED. */
4011 if (cant_pack)
4012 TYPE_PACKED (t) = 0;
4014 /* Check anonymous struct/anonymous union fields. */
4015 finish_struct_anon (t);
4017 /* We've built up the list of access declarations in reverse order.
4018 Fix that now. */
4019 *access_decls = nreverse (*access_decls);
4022 /* If TYPE is an empty class type, records its OFFSET in the table of
4023 OFFSETS. */
4025 static int
4026 record_subobject_offset (tree type, tree offset, splay_tree offsets)
4028 splay_tree_node n;
4030 if (!is_empty_class (type))
4031 return 0;
4033 /* Record the location of this empty object in OFFSETS. */
4034 n = splay_tree_lookup (offsets, (splay_tree_key) offset);
4035 if (!n)
4036 n = splay_tree_insert (offsets,
4037 (splay_tree_key) offset,
4038 (splay_tree_value) NULL_TREE);
4039 n->value = ((splay_tree_value)
4040 tree_cons (NULL_TREE,
4041 type,
4042 (tree) n->value));
4044 return 0;
4047 /* Returns nonzero if TYPE is an empty class type and there is
4048 already an entry in OFFSETS for the same TYPE as the same OFFSET. */
4050 static int
4051 check_subobject_offset (tree type, tree offset, splay_tree offsets)
4053 splay_tree_node n;
4054 tree t;
4056 if (!is_empty_class (type))
4057 return 0;
4059 /* Record the location of this empty object in OFFSETS. */
4060 n = splay_tree_lookup (offsets, (splay_tree_key) offset);
4061 if (!n)
4062 return 0;
4064 for (t = (tree) n->value; t; t = TREE_CHAIN (t))
4065 if (same_type_p (TREE_VALUE (t), type))
4066 return 1;
4068 return 0;
4071 /* Walk through all the subobjects of TYPE (located at OFFSET). Call
4072 F for every subobject, passing it the type, offset, and table of
4073 OFFSETS. If VBASES_P is one, then virtual non-primary bases should
4074 be traversed.
4076 If MAX_OFFSET is non-NULL, then subobjects with an offset greater
4077 than MAX_OFFSET will not be walked.
4079 If F returns a nonzero value, the traversal ceases, and that value
4080 is returned. Otherwise, returns zero. */
4082 static int
4083 walk_subobject_offsets (tree type,
4084 subobject_offset_fn f,
4085 tree offset,
4086 splay_tree offsets,
4087 tree max_offset,
4088 int vbases_p)
4090 int r = 0;
4091 tree type_binfo = NULL_TREE;
4093 /* If this OFFSET is bigger than the MAX_OFFSET, then we should
4094 stop. */
4095 if (max_offset && tree_int_cst_lt (max_offset, offset))
4096 return 0;
4098 if (type == error_mark_node)
4099 return 0;
4101 if (!TYPE_P (type))
4103 type_binfo = type;
4104 type = BINFO_TYPE (type);
4107 if (CLASS_TYPE_P (type))
4109 tree field;
4110 tree binfo;
4111 int i;
4113 /* Avoid recursing into objects that are not interesting. */
4114 if (!CLASSTYPE_CONTAINS_EMPTY_CLASS_P (type))
4115 return 0;
4117 /* Record the location of TYPE. */
4118 r = (*f) (type, offset, offsets);
4119 if (r)
4120 return r;
4122 /* Iterate through the direct base classes of TYPE. */
4123 if (!type_binfo)
4124 type_binfo = TYPE_BINFO (type);
4125 for (i = 0; BINFO_BASE_ITERATE (type_binfo, i, binfo); i++)
4127 tree binfo_offset;
4129 if (BINFO_VIRTUAL_P (binfo))
4130 continue;
4132 tree orig_binfo;
4133 /* We cannot rely on BINFO_OFFSET being set for the base
4134 class yet, but the offsets for direct non-virtual
4135 bases can be calculated by going back to the TYPE. */
4136 orig_binfo = BINFO_BASE_BINFO (TYPE_BINFO (type), i);
4137 binfo_offset = size_binop (PLUS_EXPR,
4138 offset,
4139 BINFO_OFFSET (orig_binfo));
4141 r = walk_subobject_offsets (binfo,
4143 binfo_offset,
4144 offsets,
4145 max_offset,
4146 /*vbases_p=*/0);
4147 if (r)
4148 return r;
4151 if (CLASSTYPE_VBASECLASSES (type))
4153 unsigned ix;
4154 vec<tree, va_gc> *vbases;
4156 /* Iterate through the virtual base classes of TYPE. In G++
4157 3.2, we included virtual bases in the direct base class
4158 loop above, which results in incorrect results; the
4159 correct offsets for virtual bases are only known when
4160 working with the most derived type. */
4161 if (vbases_p)
4162 for (vbases = CLASSTYPE_VBASECLASSES (type), ix = 0;
4163 vec_safe_iterate (vbases, ix, &binfo); ix++)
4165 r = walk_subobject_offsets (binfo,
4167 size_binop (PLUS_EXPR,
4168 offset,
4169 BINFO_OFFSET (binfo)),
4170 offsets,
4171 max_offset,
4172 /*vbases_p=*/0);
4173 if (r)
4174 return r;
4176 else
4178 /* We still have to walk the primary base, if it is
4179 virtual. (If it is non-virtual, then it was walked
4180 above.) */
4181 tree vbase = get_primary_binfo (type_binfo);
4183 if (vbase && BINFO_VIRTUAL_P (vbase)
4184 && BINFO_PRIMARY_P (vbase)
4185 && BINFO_INHERITANCE_CHAIN (vbase) == type_binfo)
4187 r = (walk_subobject_offsets
4188 (vbase, f, offset,
4189 offsets, max_offset, /*vbases_p=*/0));
4190 if (r)
4191 return r;
4196 /* Iterate through the fields of TYPE. */
4197 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
4198 if (TREE_CODE (field) == FIELD_DECL
4199 && TREE_TYPE (field) != error_mark_node
4200 && !DECL_ARTIFICIAL (field))
4202 tree field_offset;
4204 field_offset = byte_position (field);
4206 r = walk_subobject_offsets (TREE_TYPE (field),
4208 size_binop (PLUS_EXPR,
4209 offset,
4210 field_offset),
4211 offsets,
4212 max_offset,
4213 /*vbases_p=*/1);
4214 if (r)
4215 return r;
4218 else if (TREE_CODE (type) == ARRAY_TYPE)
4220 tree element_type = strip_array_types (type);
4221 tree domain = TYPE_DOMAIN (type);
4222 tree index;
4224 /* Avoid recursing into objects that are not interesting. */
4225 if (!CLASS_TYPE_P (element_type)
4226 || !CLASSTYPE_CONTAINS_EMPTY_CLASS_P (element_type)
4227 || !domain
4228 || integer_minus_onep (TYPE_MAX_VALUE (domain)))
4229 return 0;
4231 /* Step through each of the elements in the array. */
4232 for (index = size_zero_node;
4233 !tree_int_cst_lt (TYPE_MAX_VALUE (domain), index);
4234 index = size_binop (PLUS_EXPR, index, size_one_node))
4236 r = walk_subobject_offsets (TREE_TYPE (type),
4238 offset,
4239 offsets,
4240 max_offset,
4241 /*vbases_p=*/1);
4242 if (r)
4243 return r;
4244 offset = size_binop (PLUS_EXPR, offset,
4245 TYPE_SIZE_UNIT (TREE_TYPE (type)));
4246 /* If this new OFFSET is bigger than the MAX_OFFSET, then
4247 there's no point in iterating through the remaining
4248 elements of the array. */
4249 if (max_offset && tree_int_cst_lt (max_offset, offset))
4250 break;
4254 return 0;
4257 /* Record all of the empty subobjects of TYPE (either a type or a
4258 binfo). If IS_DATA_MEMBER is true, then a non-static data member
4259 is being placed at OFFSET; otherwise, it is a base class that is
4260 being placed at OFFSET. */
4262 static void
4263 record_subobject_offsets (tree type,
4264 tree offset,
4265 splay_tree offsets,
4266 bool is_data_member)
4268 tree max_offset;
4269 /* If recording subobjects for a non-static data member or a
4270 non-empty base class , we do not need to record offsets beyond
4271 the size of the biggest empty class. Additional data members
4272 will go at the end of the class. Additional base classes will go
4273 either at offset zero (if empty, in which case they cannot
4274 overlap with offsets past the size of the biggest empty class) or
4275 at the end of the class.
4277 However, if we are placing an empty base class, then we must record
4278 all offsets, as either the empty class is at offset zero (where
4279 other empty classes might later be placed) or at the end of the
4280 class (where other objects might then be placed, so other empty
4281 subobjects might later overlap). */
4282 if (is_data_member
4283 || !is_empty_class (BINFO_TYPE (type)))
4284 max_offset = sizeof_biggest_empty_class;
4285 else
4286 max_offset = NULL_TREE;
4287 walk_subobject_offsets (type, record_subobject_offset, offset,
4288 offsets, max_offset, is_data_member);
4291 /* Returns nonzero if any of the empty subobjects of TYPE (located at
4292 OFFSET) conflict with entries in OFFSETS. If VBASES_P is nonzero,
4293 virtual bases of TYPE are examined. */
4295 static int
4296 layout_conflict_p (tree type,
4297 tree offset,
4298 splay_tree offsets,
4299 int vbases_p)
4301 splay_tree_node max_node;
4303 /* Get the node in OFFSETS that indicates the maximum offset where
4304 an empty subobject is located. */
4305 max_node = splay_tree_max (offsets);
4306 /* If there aren't any empty subobjects, then there's no point in
4307 performing this check. */
4308 if (!max_node)
4309 return 0;
4311 return walk_subobject_offsets (type, check_subobject_offset, offset,
4312 offsets, (tree) (max_node->key),
4313 vbases_p);
4316 /* DECL is a FIELD_DECL corresponding either to a base subobject of a
4317 non-static data member of the type indicated by RLI. BINFO is the
4318 binfo corresponding to the base subobject, OFFSETS maps offsets to
4319 types already located at those offsets. This function determines
4320 the position of the DECL. */
4322 static void
4323 layout_nonempty_base_or_field (record_layout_info rli,
4324 tree decl,
4325 tree binfo,
4326 splay_tree offsets)
4328 tree offset = NULL_TREE;
4329 bool field_p;
4330 tree type;
4332 if (binfo)
4334 /* For the purposes of determining layout conflicts, we want to
4335 use the class type of BINFO; TREE_TYPE (DECL) will be the
4336 CLASSTYPE_AS_BASE version, which does not contain entries for
4337 zero-sized bases. */
4338 type = TREE_TYPE (binfo);
4339 field_p = false;
4341 else
4343 type = TREE_TYPE (decl);
4344 field_p = true;
4347 /* Try to place the field. It may take more than one try if we have
4348 a hard time placing the field without putting two objects of the
4349 same type at the same address. */
4350 while (1)
4352 struct record_layout_info_s old_rli = *rli;
4354 /* Place this field. */
4355 place_field (rli, decl);
4356 offset = byte_position (decl);
4358 /* We have to check to see whether or not there is already
4359 something of the same type at the offset we're about to use.
4360 For example, consider:
4362 struct S {};
4363 struct T : public S { int i; };
4364 struct U : public S, public T {};
4366 Here, we put S at offset zero in U. Then, we can't put T at
4367 offset zero -- its S component would be at the same address
4368 as the S we already allocated. So, we have to skip ahead.
4369 Since all data members, including those whose type is an
4370 empty class, have nonzero size, any overlap can happen only
4371 with a direct or indirect base-class -- it can't happen with
4372 a data member. */
4373 /* In a union, overlap is permitted; all members are placed at
4374 offset zero. */
4375 if (TREE_CODE (rli->t) == UNION_TYPE)
4376 break;
4377 if (layout_conflict_p (field_p ? type : binfo, offset,
4378 offsets, field_p))
4380 /* Strip off the size allocated to this field. That puts us
4381 at the first place we could have put the field with
4382 proper alignment. */
4383 *rli = old_rli;
4385 /* Bump up by the alignment required for the type. */
4386 rli->bitpos
4387 = size_binop (PLUS_EXPR, rli->bitpos,
4388 bitsize_int (binfo
4389 ? CLASSTYPE_ALIGN (type)
4390 : TYPE_ALIGN (type)));
4391 normalize_rli (rli);
4393 else if (TREE_CODE (type) == NULLPTR_TYPE
4394 && warn_abi && abi_version_crosses (9))
4396 /* Before ABI v9, we were giving nullptr_t alignment of 1; if
4397 the offset wasn't aligned like a pointer when we started to
4398 layout this field, that affects its position. */
4399 tree pos = rli_size_unit_so_far (&old_rli);
4400 if (int_cst_value (pos) % TYPE_ALIGN_UNIT (ptr_type_node) != 0)
4402 if (abi_version_at_least (9))
4403 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wabi,
4404 "alignment of %qD increased in -fabi-version=9 "
4405 "(GCC 5.2)", decl);
4406 else
4407 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wabi, "alignment "
4408 "of %qD will increase in -fabi-version=9", decl);
4410 break;
4412 else
4413 /* There was no conflict. We're done laying out this field. */
4414 break;
4417 /* Now that we know where it will be placed, update its
4418 BINFO_OFFSET. */
4419 if (binfo && CLASS_TYPE_P (BINFO_TYPE (binfo)))
4420 /* Indirect virtual bases may have a nonzero BINFO_OFFSET at
4421 this point because their BINFO_OFFSET is copied from another
4422 hierarchy. Therefore, we may not need to add the entire
4423 OFFSET. */
4424 propagate_binfo_offsets (binfo,
4425 size_diffop_loc (input_location,
4426 fold_convert (ssizetype, offset),
4427 fold_convert (ssizetype,
4428 BINFO_OFFSET (binfo))));
4431 /* Returns true if TYPE is empty and OFFSET is nonzero. */
4433 static int
4434 empty_base_at_nonzero_offset_p (tree type,
4435 tree offset,
4436 splay_tree /*offsets*/)
4438 return is_empty_class (type) && !integer_zerop (offset);
4441 /* Layout the empty base BINFO. EOC indicates the byte currently just
4442 past the end of the class, and should be correctly aligned for a
4443 class of the type indicated by BINFO; OFFSETS gives the offsets of
4444 the empty bases allocated so far. T is the most derived
4445 type. Return nonzero iff we added it at the end. */
4447 static bool
4448 layout_empty_base (record_layout_info rli, tree binfo,
4449 tree eoc, splay_tree offsets)
4451 tree alignment;
4452 tree basetype = BINFO_TYPE (binfo);
4453 bool atend = false;
4455 /* This routine should only be used for empty classes. */
4456 gcc_assert (is_empty_class (basetype));
4457 alignment = ssize_int (CLASSTYPE_ALIGN_UNIT (basetype));
4459 if (!integer_zerop (BINFO_OFFSET (binfo)))
4460 propagate_binfo_offsets
4461 (binfo, size_diffop_loc (input_location,
4462 size_zero_node, BINFO_OFFSET (binfo)));
4464 /* This is an empty base class. We first try to put it at offset
4465 zero. */
4466 if (layout_conflict_p (binfo,
4467 BINFO_OFFSET (binfo),
4468 offsets,
4469 /*vbases_p=*/0))
4471 /* That didn't work. Now, we move forward from the next
4472 available spot in the class. */
4473 atend = true;
4474 propagate_binfo_offsets (binfo, fold_convert (ssizetype, eoc));
4475 while (1)
4477 if (!layout_conflict_p (binfo,
4478 BINFO_OFFSET (binfo),
4479 offsets,
4480 /*vbases_p=*/0))
4481 /* We finally found a spot where there's no overlap. */
4482 break;
4484 /* There's overlap here, too. Bump along to the next spot. */
4485 propagate_binfo_offsets (binfo, alignment);
4489 if (CLASSTYPE_USER_ALIGN (basetype))
4491 rli->record_align = MAX (rli->record_align, CLASSTYPE_ALIGN (basetype));
4492 if (warn_packed)
4493 rli->unpacked_align = MAX (rli->unpacked_align, CLASSTYPE_ALIGN (basetype));
4494 TYPE_USER_ALIGN (rli->t) = 1;
4497 return atend;
4500 /* Build the FIELD_DECL for BASETYPE as a base of T, add it to the chain of
4501 fields at NEXT_FIELD, and return it. */
4503 static tree
4504 build_base_field_1 (tree t, tree basetype, tree *&next_field)
4506 /* Create the FIELD_DECL. */
4507 gcc_assert (CLASSTYPE_AS_BASE (basetype));
4508 tree decl = build_decl (input_location,
4509 FIELD_DECL, NULL_TREE, CLASSTYPE_AS_BASE (basetype));
4510 DECL_ARTIFICIAL (decl) = 1;
4511 DECL_IGNORED_P (decl) = 1;
4512 DECL_FIELD_CONTEXT (decl) = t;
4513 DECL_SIZE (decl) = CLASSTYPE_SIZE (basetype);
4514 DECL_SIZE_UNIT (decl) = CLASSTYPE_SIZE_UNIT (basetype);
4515 SET_DECL_ALIGN (decl, CLASSTYPE_ALIGN (basetype));
4516 DECL_USER_ALIGN (decl) = CLASSTYPE_USER_ALIGN (basetype);
4517 SET_DECL_MODE (decl, TYPE_MODE (basetype));
4518 DECL_FIELD_IS_BASE (decl) = 1;
4520 /* Add the new FIELD_DECL to the list of fields for T. */
4521 DECL_CHAIN (decl) = *next_field;
4522 *next_field = decl;
4523 next_field = &DECL_CHAIN (decl);
4525 return decl;
4528 /* Layout the base given by BINFO in the class indicated by RLI.
4529 *BASE_ALIGN is a running maximum of the alignments of
4530 any base class. OFFSETS gives the location of empty base
4531 subobjects. T is the most derived type. Return nonzero if the new
4532 object cannot be nearly-empty. A new FIELD_DECL is inserted at
4533 *NEXT_FIELD, unless BINFO is for an empty base class.
4535 Returns the location at which the next field should be inserted. */
4537 static tree *
4538 build_base_field (record_layout_info rli, tree binfo,
4539 splay_tree offsets, tree *next_field)
4541 tree t = rli->t;
4542 tree basetype = BINFO_TYPE (binfo);
4544 if (!COMPLETE_TYPE_P (basetype))
4545 /* This error is now reported in xref_tag, thus giving better
4546 location information. */
4547 return next_field;
4549 /* Place the base class. */
4550 if (!is_empty_class (basetype))
4552 tree decl;
4554 /* The containing class is non-empty because it has a non-empty
4555 base class. */
4556 CLASSTYPE_EMPTY_P (t) = 0;
4558 /* Create the FIELD_DECL. */
4559 decl = build_base_field_1 (t, basetype, next_field);
4561 /* Try to place the field. It may take more than one try if we
4562 have a hard time placing the field without putting two
4563 objects of the same type at the same address. */
4564 layout_nonempty_base_or_field (rli, decl, binfo, offsets);
4566 else
4568 tree eoc;
4569 bool atend;
4571 /* On some platforms (ARM), even empty classes will not be
4572 byte-aligned. */
4573 eoc = round_up_loc (input_location,
4574 rli_size_unit_so_far (rli),
4575 CLASSTYPE_ALIGN_UNIT (basetype));
4576 atend = layout_empty_base (rli, binfo, eoc, offsets);
4577 /* A nearly-empty class "has no proper base class that is empty,
4578 not morally virtual, and at an offset other than zero." */
4579 if (!BINFO_VIRTUAL_P (binfo) && CLASSTYPE_NEARLY_EMPTY_P (t))
4581 if (atend)
4582 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
4583 /* The check above (used in G++ 3.2) is insufficient because
4584 an empty class placed at offset zero might itself have an
4585 empty base at a nonzero offset. */
4586 else if (walk_subobject_offsets (basetype,
4587 empty_base_at_nonzero_offset_p,
4588 size_zero_node,
4589 /*offsets=*/NULL,
4590 /*max_offset=*/NULL_TREE,
4591 /*vbases_p=*/true))
4592 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
4595 /* We used to not create a FIELD_DECL for empty base classes because of
4596 back end issues with overlapping FIELD_DECLs, but that doesn't seem to
4597 be a problem anymore. We need them to handle initialization of C++17
4598 aggregate bases. */
4599 if (cxx_dialect >= cxx1z && !BINFO_VIRTUAL_P (binfo))
4601 tree decl = build_base_field_1 (t, basetype, next_field);
4602 DECL_FIELD_OFFSET (decl) = BINFO_OFFSET (binfo);
4603 DECL_FIELD_BIT_OFFSET (decl) = bitsize_zero_node;
4604 SET_DECL_OFFSET_ALIGN (decl, BITS_PER_UNIT);
4607 /* An empty virtual base causes a class to be non-empty
4608 -- but in that case we do not need to clear CLASSTYPE_EMPTY_P
4609 here because that was already done when the virtual table
4610 pointer was created. */
4613 /* Record the offsets of BINFO and its base subobjects. */
4614 record_subobject_offsets (binfo,
4615 BINFO_OFFSET (binfo),
4616 offsets,
4617 /*is_data_member=*/false);
4619 return next_field;
4622 /* Layout all of the non-virtual base classes. Record empty
4623 subobjects in OFFSETS. T is the most derived type. Return nonzero
4624 if the type cannot be nearly empty. The fields created
4625 corresponding to the base classes will be inserted at
4626 *NEXT_FIELD. */
4628 static void
4629 build_base_fields (record_layout_info rli,
4630 splay_tree offsets, tree *next_field)
4632 /* Chain to hold all the new FIELD_DECLs which stand in for base class
4633 subobjects. */
4634 tree t = rli->t;
4635 int n_baseclasses = BINFO_N_BASE_BINFOS (TYPE_BINFO (t));
4636 int i;
4638 /* The primary base class is always allocated first. */
4639 if (CLASSTYPE_HAS_PRIMARY_BASE_P (t))
4640 next_field = build_base_field (rli, CLASSTYPE_PRIMARY_BINFO (t),
4641 offsets, next_field);
4643 /* Now allocate the rest of the bases. */
4644 for (i = 0; i < n_baseclasses; ++i)
4646 tree base_binfo;
4648 base_binfo = BINFO_BASE_BINFO (TYPE_BINFO (t), i);
4650 /* The primary base was already allocated above, so we don't
4651 need to allocate it again here. */
4652 if (base_binfo == CLASSTYPE_PRIMARY_BINFO (t))
4653 continue;
4655 /* Virtual bases are added at the end (a primary virtual base
4656 will have already been added). */
4657 if (BINFO_VIRTUAL_P (base_binfo))
4658 continue;
4660 next_field = build_base_field (rli, base_binfo,
4661 offsets, next_field);
4665 /* Go through the TYPE_FIELDS of T issuing any appropriate
4666 diagnostics, figuring out which methods override which other
4667 methods, and so forth. */
4669 static void
4670 check_methods (tree t)
4672 for (tree x = TYPE_FIELDS (t); x; x = DECL_CHAIN (x))
4673 if (DECL_DECLARES_FUNCTION_P (x))
4675 check_for_override (x, t);
4677 if (DECL_PURE_VIRTUAL_P (x)
4678 && (TREE_CODE (x) != FUNCTION_DECL || ! DECL_VINDEX (x)))
4679 error ("initializer specified for non-virtual method %q+D", x);
4680 /* The name of the field is the original field name
4681 Save this in auxiliary field for later overloading. */
4682 if (TREE_CODE (x) == FUNCTION_DECL && DECL_VINDEX (x))
4684 TYPE_POLYMORPHIC_P (t) = 1;
4685 if (DECL_PURE_VIRTUAL_P (x))
4686 vec_safe_push (CLASSTYPE_PURE_VIRTUALS (t), x);
4689 /* All user-provided destructors are non-trivial.
4690 Constructors and assignment ops are handled in
4691 grok_special_member_properties. */
4692 if (DECL_DESTRUCTOR_P (x) && user_provided_p (x))
4693 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t) = 1;
4694 if (!DECL_VIRTUAL_P (x)
4695 && lookup_attribute ("transaction_safe_dynamic",
4696 DECL_ATTRIBUTES (x)))
4697 error_at (DECL_SOURCE_LOCATION (x),
4698 "%<transaction_safe_dynamic%> may only be specified for "
4699 "a virtual function");
4703 /* FN is a constructor or destructor. Clone the declaration to create
4704 a specialized in-charge or not-in-charge version, as indicated by
4705 NAME. */
4707 static tree
4708 build_clone (tree fn, tree name)
4710 tree parms;
4711 tree clone;
4713 /* Copy the function. */
4714 clone = copy_decl (fn);
4715 /* Reset the function name. */
4716 DECL_NAME (clone) = name;
4717 /* Remember where this function came from. */
4718 DECL_ABSTRACT_ORIGIN (clone) = fn;
4719 /* Make it easy to find the CLONE given the FN. */
4720 DECL_CHAIN (clone) = DECL_CHAIN (fn);
4721 DECL_CHAIN (fn) = clone;
4723 /* If this is a template, do the rest on the DECL_TEMPLATE_RESULT. */
4724 if (TREE_CODE (clone) == TEMPLATE_DECL)
4726 tree result = build_clone (DECL_TEMPLATE_RESULT (clone), name);
4727 DECL_TEMPLATE_RESULT (clone) = result;
4728 DECL_TEMPLATE_INFO (result) = copy_node (DECL_TEMPLATE_INFO (result));
4729 DECL_TI_TEMPLATE (result) = clone;
4730 TREE_TYPE (clone) = TREE_TYPE (result);
4731 return clone;
4733 else
4735 // Clone constraints.
4736 if (flag_concepts)
4737 if (tree ci = get_constraints (fn))
4738 set_constraints (clone, copy_node (ci));
4742 SET_DECL_ASSEMBLER_NAME (clone, NULL_TREE);
4743 DECL_CLONED_FUNCTION (clone) = fn;
4744 /* There's no pending inline data for this function. */
4745 DECL_PENDING_INLINE_INFO (clone) = NULL;
4746 DECL_PENDING_INLINE_P (clone) = 0;
4748 /* The base-class destructor is not virtual. */
4749 if (name == base_dtor_identifier)
4751 DECL_VIRTUAL_P (clone) = 0;
4752 if (TREE_CODE (clone) != TEMPLATE_DECL)
4753 DECL_VINDEX (clone) = NULL_TREE;
4756 bool ctor_omit_inherited_parms_p = ctor_omit_inherited_parms (clone);
4757 if (ctor_omit_inherited_parms_p)
4758 gcc_assert (DECL_HAS_IN_CHARGE_PARM_P (clone));
4760 /* If there was an in-charge parameter, drop it from the function
4761 type. */
4762 if (DECL_HAS_IN_CHARGE_PARM_P (clone))
4764 tree basetype;
4765 tree parmtypes;
4766 tree exceptions;
4768 exceptions = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (clone));
4769 basetype = TYPE_METHOD_BASETYPE (TREE_TYPE (clone));
4770 parmtypes = TYPE_ARG_TYPES (TREE_TYPE (clone));
4771 /* Skip the `this' parameter. */
4772 parmtypes = TREE_CHAIN (parmtypes);
4773 /* Skip the in-charge parameter. */
4774 parmtypes = TREE_CHAIN (parmtypes);
4775 /* And the VTT parm, in a complete [cd]tor. */
4776 if (DECL_HAS_VTT_PARM_P (fn)
4777 && ! DECL_NEEDS_VTT_PARM_P (clone))
4778 parmtypes = TREE_CHAIN (parmtypes);
4779 if (ctor_omit_inherited_parms_p)
4781 /* If we're omitting inherited parms, that just leaves the VTT. */
4782 gcc_assert (DECL_NEEDS_VTT_PARM_P (clone));
4783 parmtypes = tree_cons (NULL_TREE, vtt_parm_type, void_list_node);
4785 TREE_TYPE (clone)
4786 = build_method_type_directly (basetype,
4787 TREE_TYPE (TREE_TYPE (clone)),
4788 parmtypes);
4789 if (exceptions)
4790 TREE_TYPE (clone) = build_exception_variant (TREE_TYPE (clone),
4791 exceptions);
4792 TREE_TYPE (clone)
4793 = cp_build_type_attribute_variant (TREE_TYPE (clone),
4794 TYPE_ATTRIBUTES (TREE_TYPE (fn)));
4797 /* Copy the function parameters. */
4798 DECL_ARGUMENTS (clone) = copy_list (DECL_ARGUMENTS (clone));
4799 /* Remove the in-charge parameter. */
4800 if (DECL_HAS_IN_CHARGE_PARM_P (clone))
4802 DECL_CHAIN (DECL_ARGUMENTS (clone))
4803 = DECL_CHAIN (DECL_CHAIN (DECL_ARGUMENTS (clone)));
4804 DECL_HAS_IN_CHARGE_PARM_P (clone) = 0;
4806 /* And the VTT parm, in a complete [cd]tor. */
4807 if (DECL_HAS_VTT_PARM_P (fn))
4809 if (DECL_NEEDS_VTT_PARM_P (clone))
4810 DECL_HAS_VTT_PARM_P (clone) = 1;
4811 else
4813 DECL_CHAIN (DECL_ARGUMENTS (clone))
4814 = DECL_CHAIN (DECL_CHAIN (DECL_ARGUMENTS (clone)));
4815 DECL_HAS_VTT_PARM_P (clone) = 0;
4819 /* A base constructor inheriting from a virtual base doesn't get the
4820 arguments. */
4821 if (ctor_omit_inherited_parms_p)
4822 DECL_CHAIN (DECL_CHAIN (DECL_ARGUMENTS (clone))) = NULL_TREE;
4824 for (parms = DECL_ARGUMENTS (clone); parms; parms = DECL_CHAIN (parms))
4826 DECL_CONTEXT (parms) = clone;
4827 cxx_dup_lang_specific_decl (parms);
4830 /* Create the RTL for this function. */
4831 SET_DECL_RTL (clone, NULL);
4832 rest_of_decl_compilation (clone, /*top_level=*/1, at_eof);
4834 return clone;
4837 /* Implementation of DECL_CLONED_FUNCTION and DECL_CLONED_FUNCTION_P, do
4838 not invoke this function directly.
4840 For a non-thunk function, returns the address of the slot for storing
4841 the function it is a clone of. Otherwise returns NULL_TREE.
4843 If JUST_TESTING, looks through TEMPLATE_DECL and returns NULL if
4844 cloned_function is unset. This is to support the separate
4845 DECL_CLONED_FUNCTION and DECL_CLONED_FUNCTION_P modes; using the latter
4846 on a template makes sense, but not the former. */
4848 tree *
4849 decl_cloned_function_p (const_tree decl, bool just_testing)
4851 tree *ptr;
4852 if (just_testing)
4853 decl = STRIP_TEMPLATE (decl);
4855 if (TREE_CODE (decl) != FUNCTION_DECL
4856 || !DECL_LANG_SPECIFIC (decl)
4857 || DECL_LANG_SPECIFIC (decl)->u.fn.thunk_p)
4859 #if defined ENABLE_TREE_CHECKING && (GCC_VERSION >= 2007)
4860 if (!just_testing)
4861 lang_check_failed (__FILE__, __LINE__, __FUNCTION__);
4862 else
4863 #endif
4864 return NULL;
4867 ptr = &DECL_LANG_SPECIFIC (decl)->u.fn.u5.cloned_function;
4868 if (just_testing && *ptr == NULL_TREE)
4869 return NULL;
4870 else
4871 return ptr;
4874 /* Produce declarations for all appropriate clones of FN. If
4875 UPDATE_METHODS is true, the clones are added to the
4876 CLASSTYPE_METHOD_VEC. */
4878 void
4879 clone_function_decl (tree fn, bool update_methods)
4881 tree clone;
4883 /* Avoid inappropriate cloning. */
4884 if (DECL_CHAIN (fn)
4885 && DECL_CLONED_FUNCTION_P (DECL_CHAIN (fn)))
4886 return;
4888 if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (fn))
4890 /* For each constructor, we need two variants: an in-charge version
4891 and a not-in-charge version. */
4892 clone = build_clone (fn, complete_ctor_identifier);
4893 if (update_methods)
4894 add_method (DECL_CONTEXT (clone), clone, false);
4895 clone = build_clone (fn, base_ctor_identifier);
4896 if (update_methods)
4897 add_method (DECL_CONTEXT (clone), clone, false);
4899 else
4901 gcc_assert (DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (fn));
4903 /* For each destructor, we need three variants: an in-charge
4904 version, a not-in-charge version, and an in-charge deleting
4905 version. We clone the deleting version first because that
4906 means it will go second on the TYPE_FIELDS list -- and that
4907 corresponds to the correct layout order in the virtual
4908 function table.
4910 For a non-virtual destructor, we do not build a deleting
4911 destructor. */
4912 if (DECL_VIRTUAL_P (fn))
4914 clone = build_clone (fn, deleting_dtor_identifier);
4915 if (update_methods)
4916 add_method (DECL_CONTEXT (clone), clone, false);
4918 clone = build_clone (fn, complete_dtor_identifier);
4919 if (update_methods)
4920 add_method (DECL_CONTEXT (clone), clone, false);
4921 clone = build_clone (fn, base_dtor_identifier);
4922 if (update_methods)
4923 add_method (DECL_CONTEXT (clone), clone, false);
4926 /* Note that this is an abstract function that is never emitted. */
4927 DECL_ABSTRACT_P (fn) = true;
4930 /* DECL is an in charge constructor, which is being defined. This will
4931 have had an in class declaration, from whence clones were
4932 declared. An out-of-class definition can specify additional default
4933 arguments. As it is the clones that are involved in overload
4934 resolution, we must propagate the information from the DECL to its
4935 clones. */
4937 void
4938 adjust_clone_args (tree decl)
4940 tree clone;
4942 for (clone = DECL_CHAIN (decl); clone && DECL_CLONED_FUNCTION_P (clone);
4943 clone = DECL_CHAIN (clone))
4945 tree orig_clone_parms = TYPE_ARG_TYPES (TREE_TYPE (clone));
4946 tree orig_decl_parms = TYPE_ARG_TYPES (TREE_TYPE (decl));
4947 tree decl_parms, clone_parms;
4949 clone_parms = orig_clone_parms;
4951 /* Skip the 'this' parameter. */
4952 orig_clone_parms = TREE_CHAIN (orig_clone_parms);
4953 orig_decl_parms = TREE_CHAIN (orig_decl_parms);
4955 if (DECL_HAS_IN_CHARGE_PARM_P (decl))
4956 orig_decl_parms = TREE_CHAIN (orig_decl_parms);
4957 if (DECL_HAS_VTT_PARM_P (decl))
4958 orig_decl_parms = TREE_CHAIN (orig_decl_parms);
4960 clone_parms = orig_clone_parms;
4961 if (DECL_HAS_VTT_PARM_P (clone))
4962 clone_parms = TREE_CHAIN (clone_parms);
4964 for (decl_parms = orig_decl_parms; decl_parms;
4965 decl_parms = TREE_CHAIN (decl_parms),
4966 clone_parms = TREE_CHAIN (clone_parms))
4968 if (clone_parms == void_list_node)
4970 gcc_assert (decl_parms == clone_parms
4971 || ctor_omit_inherited_parms (clone));
4972 break;
4975 gcc_assert (same_type_p (TREE_TYPE (decl_parms),
4976 TREE_TYPE (clone_parms)));
4978 if (TREE_PURPOSE (decl_parms) && !TREE_PURPOSE (clone_parms))
4980 /* A default parameter has been added. Adjust the
4981 clone's parameters. */
4982 tree exceptions = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (clone));
4983 tree attrs = TYPE_ATTRIBUTES (TREE_TYPE (clone));
4984 tree basetype = TYPE_METHOD_BASETYPE (TREE_TYPE (clone));
4985 tree type;
4987 clone_parms = orig_decl_parms;
4989 if (DECL_HAS_VTT_PARM_P (clone))
4991 clone_parms = tree_cons (TREE_PURPOSE (orig_clone_parms),
4992 TREE_VALUE (orig_clone_parms),
4993 clone_parms);
4994 TREE_TYPE (clone_parms) = TREE_TYPE (orig_clone_parms);
4996 type = build_method_type_directly (basetype,
4997 TREE_TYPE (TREE_TYPE (clone)),
4998 clone_parms);
4999 if (exceptions)
5000 type = build_exception_variant (type, exceptions);
5001 if (attrs)
5002 type = cp_build_type_attribute_variant (type, attrs);
5003 TREE_TYPE (clone) = type;
5005 clone_parms = NULL_TREE;
5006 break;
5009 gcc_assert (!clone_parms || clone_parms == void_list_node);
5013 /* For each of the constructors and destructors in T, create an
5014 in-charge and not-in-charge variant. */
5016 static void
5017 clone_constructors_and_destructors (tree t)
5019 /* If for some reason we don't have a CLASSTYPE_METHOD_VEC, we bail
5020 out now. */
5021 if (!CLASSTYPE_METHOD_VEC (t))
5022 return;
5024 /* While constructors can be via a using declaration, at this point
5025 we no longer need to know that. */
5026 for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (t)); iter; ++iter)
5027 clone_function_decl (*iter, /*update_methods=*/true);
5029 if (tree dtor = CLASSTYPE_DESTRUCTOR (t))
5030 clone_function_decl (dtor, /*update_methods=*/true);
5033 /* Deduce noexcept for a destructor DTOR. */
5035 void
5036 deduce_noexcept_on_destructor (tree dtor)
5038 if (!TYPE_RAISES_EXCEPTIONS (TREE_TYPE (dtor)))
5039 TREE_TYPE (dtor) = build_exception_variant (TREE_TYPE (dtor),
5040 noexcept_deferred_spec);
5043 /* Subroutine of set_one_vmethod_tm_attributes. Search base classes
5044 of TYPE for virtual functions which FNDECL overrides. Return a
5045 mask of the tm attributes found therein. */
5047 static int
5048 look_for_tm_attr_overrides (tree type, tree fndecl)
5050 tree binfo = TYPE_BINFO (type);
5051 tree base_binfo;
5052 int ix, found = 0;
5054 for (ix = 0; BINFO_BASE_ITERATE (binfo, ix, base_binfo); ++ix)
5056 tree o, basetype = BINFO_TYPE (base_binfo);
5058 if (!TYPE_POLYMORPHIC_P (basetype))
5059 continue;
5061 o = look_for_overrides_here (basetype, fndecl);
5062 if (o)
5064 if (lookup_attribute ("transaction_safe_dynamic",
5065 DECL_ATTRIBUTES (o)))
5066 /* transaction_safe_dynamic is not inherited. */;
5067 else
5068 found |= tm_attr_to_mask (find_tm_attribute
5069 (TYPE_ATTRIBUTES (TREE_TYPE (o))));
5071 else
5072 found |= look_for_tm_attr_overrides (basetype, fndecl);
5075 return found;
5078 /* Subroutine of set_method_tm_attributes. Handle the checks and
5079 inheritance for one virtual method FNDECL. */
5081 static void
5082 set_one_vmethod_tm_attributes (tree type, tree fndecl)
5084 tree tm_attr;
5085 int found, have;
5087 found = look_for_tm_attr_overrides (type, fndecl);
5089 /* If FNDECL doesn't actually override anything (i.e. T is the
5090 class that first declares FNDECL virtual), then we're done. */
5091 if (found == 0)
5092 return;
5094 tm_attr = find_tm_attribute (TYPE_ATTRIBUTES (TREE_TYPE (fndecl)));
5095 have = tm_attr_to_mask (tm_attr);
5097 /* Intel STM Language Extension 3.0, Section 4.2 table 4:
5098 tm_pure must match exactly, otherwise no weakening of
5099 tm_safe > tm_callable > nothing. */
5100 /* ??? The tm_pure attribute didn't make the transition to the
5101 multivendor language spec. */
5102 if (have == TM_ATTR_PURE)
5104 if (found != TM_ATTR_PURE)
5106 found &= -found;
5107 goto err_override;
5110 /* If the overridden function is tm_pure, then FNDECL must be. */
5111 else if (found == TM_ATTR_PURE && tm_attr)
5112 goto err_override;
5113 /* Look for base class combinations that cannot be satisfied. */
5114 else if (found != TM_ATTR_PURE && (found & TM_ATTR_PURE))
5116 found &= ~TM_ATTR_PURE;
5117 found &= -found;
5118 error_at (DECL_SOURCE_LOCATION (fndecl),
5119 "method overrides both %<transaction_pure%> and %qE methods",
5120 tm_mask_to_attr (found));
5122 /* If FNDECL did not declare an attribute, then inherit the most
5123 restrictive one. */
5124 else if (tm_attr == NULL)
5126 apply_tm_attr (fndecl, tm_mask_to_attr (least_bit_hwi (found)));
5128 /* Otherwise validate that we're not weaker than a function
5129 that is being overridden. */
5130 else
5132 found &= -found;
5133 if (found <= TM_ATTR_CALLABLE && have > found)
5134 goto err_override;
5136 return;
5138 err_override:
5139 error_at (DECL_SOURCE_LOCATION (fndecl),
5140 "method declared %qE overriding %qE method",
5141 tm_attr, tm_mask_to_attr (found));
5144 /* For each of the methods in T, propagate a class-level tm attribute. */
5146 static void
5147 set_method_tm_attributes (tree t)
5149 tree class_tm_attr, fndecl;
5151 /* Don't bother collecting tm attributes if transactional memory
5152 support is not enabled. */
5153 if (!flag_tm)
5154 return;
5156 /* Process virtual methods first, as they inherit directly from the
5157 base virtual function and also require validation of new attributes. */
5158 if (TYPE_CONTAINS_VPTR_P (t))
5160 tree vchain;
5161 for (vchain = BINFO_VIRTUALS (TYPE_BINFO (t)); vchain;
5162 vchain = TREE_CHAIN (vchain))
5164 fndecl = BV_FN (vchain);
5165 if (DECL_THUNK_P (fndecl))
5166 fndecl = THUNK_TARGET (fndecl);
5167 set_one_vmethod_tm_attributes (t, fndecl);
5171 /* If the class doesn't have an attribute, nothing more to do. */
5172 class_tm_attr = find_tm_attribute (TYPE_ATTRIBUTES (t));
5173 if (class_tm_attr == NULL)
5174 return;
5176 /* Any method that does not yet have a tm attribute inherits
5177 the one from the class. */
5178 for (fndecl = TYPE_FIELDS (t); fndecl; fndecl = DECL_CHAIN (fndecl))
5179 if (DECL_DECLARES_FUNCTION_P (fndecl)
5180 && !find_tm_attribute (TYPE_ATTRIBUTES (TREE_TYPE (fndecl))))
5181 apply_tm_attr (fndecl, class_tm_attr);
5184 /* Returns true if FN is a default constructor. */
5186 bool
5187 default_ctor_p (tree fn)
5189 return (DECL_CONSTRUCTOR_P (fn)
5190 && sufficient_parms_p (FUNCTION_FIRST_USER_PARMTYPE (fn)));
5193 /* Returns true iff class T has a user-defined constructor that can be called
5194 with more than zero arguments. */
5196 bool
5197 type_has_user_nondefault_constructor (tree t)
5199 if (!TYPE_HAS_USER_CONSTRUCTOR (t))
5200 return false;
5202 for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (t)); iter; ++iter)
5204 tree fn = *iter;
5205 if (!DECL_ARTIFICIAL (fn)
5206 && (TREE_CODE (fn) == TEMPLATE_DECL
5207 || (skip_artificial_parms_for (fn, DECL_ARGUMENTS (fn))
5208 != NULL_TREE)))
5209 return true;
5212 return false;
5215 /* Returns the defaulted constructor if T has one. Otherwise, returns
5216 NULL_TREE. */
5218 tree
5219 in_class_defaulted_default_constructor (tree t)
5221 if (!TYPE_HAS_USER_CONSTRUCTOR (t))
5222 return NULL_TREE;
5224 for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (t)); iter; ++iter)
5226 tree fn = *iter;
5228 if (DECL_DEFAULTED_IN_CLASS_P (fn)
5229 && default_ctor_p (fn))
5230 return fn;
5233 return NULL_TREE;
5236 /* Returns true iff FN is a user-provided function, i.e. user-declared
5237 and not defaulted at its first declaration. */
5239 bool
5240 user_provided_p (tree fn)
5242 if (TREE_CODE (fn) == TEMPLATE_DECL)
5243 return true;
5244 else
5245 return (!DECL_ARTIFICIAL (fn)
5246 && !(DECL_INITIALIZED_IN_CLASS_P (fn)
5247 && (DECL_DEFAULTED_FN (fn) || DECL_DELETED_FN (fn))));
5250 /* Returns true iff class T has a user-provided constructor. */
5252 bool
5253 type_has_user_provided_constructor (tree t)
5255 if (!CLASS_TYPE_P (t))
5256 return false;
5258 if (!TYPE_HAS_USER_CONSTRUCTOR (t))
5259 return false;
5261 /* This can happen in error cases; avoid crashing. */
5262 if (!CLASSTYPE_METHOD_VEC (t))
5263 return false;
5265 for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (t)); iter; ++iter)
5266 if (user_provided_p (*iter))
5267 return true;
5269 return false;
5272 /* Returns true iff class T has a user-provided or explicit constructor. */
5274 bool
5275 type_has_user_provided_or_explicit_constructor (tree t)
5277 if (!CLASS_TYPE_P (t))
5278 return false;
5280 if (!TYPE_HAS_USER_CONSTRUCTOR (t))
5281 return false;
5283 /* This can happen in error cases; avoid crashing. */
5284 if (!CLASSTYPE_METHOD_VEC (t))
5285 return false;
5287 for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (t)); iter; ++iter)
5289 tree fn = *iter;
5290 if (user_provided_p (fn) || DECL_NONCONVERTING_P (fn))
5291 return true;
5294 return false;
5297 /* Returns true iff class T has a non-user-provided (i.e. implicitly
5298 declared or explicitly defaulted in the class body) default
5299 constructor. */
5301 bool
5302 type_has_non_user_provided_default_constructor (tree t)
5304 if (!TYPE_HAS_DEFAULT_CONSTRUCTOR (t))
5305 return false;
5306 if (CLASSTYPE_LAZY_DEFAULT_CTOR (t))
5307 return true;
5309 for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (t)); iter; ++iter)
5311 tree fn = *iter;
5312 if (TREE_CODE (fn) == FUNCTION_DECL
5313 && default_ctor_p (fn)
5314 && !user_provided_p (fn))
5315 return true;
5318 return false;
5321 /* TYPE is being used as a virtual base, and has a non-trivial move
5322 assignment. Return true if this is due to there being a user-provided
5323 move assignment in TYPE or one of its subobjects; if there isn't, then
5324 multiple move assignment can't cause any harm. */
5326 bool
5327 vbase_has_user_provided_move_assign (tree type)
5329 /* Does the type itself have a user-provided move assignment operator? */
5330 for (ovl_iterator iter (lookup_fnfields_slot_nolazy
5331 (type, cp_assignment_operator_id (NOP_EXPR)));
5332 iter; ++iter)
5334 tree fn = *iter;
5335 if (move_fn_p (fn) && user_provided_p (fn))
5336 return true;
5339 /* Do any of its bases? */
5340 tree binfo = TYPE_BINFO (type);
5341 tree base_binfo;
5342 for (int i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
5343 if (vbase_has_user_provided_move_assign (BINFO_TYPE (base_binfo)))
5344 return true;
5346 /* Or non-static data members? */
5347 for (tree field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
5349 if (TREE_CODE (field) == FIELD_DECL
5350 && CLASS_TYPE_P (TREE_TYPE (field))
5351 && vbase_has_user_provided_move_assign (TREE_TYPE (field)))
5352 return true;
5355 /* Seems not. */
5356 return false;
5359 /* If default-initialization leaves part of TYPE uninitialized, returns
5360 a DECL for the field or TYPE itself (DR 253). */
5362 tree
5363 default_init_uninitialized_part (tree type)
5365 tree t, r, binfo;
5366 int i;
5368 type = strip_array_types (type);
5369 if (!CLASS_TYPE_P (type))
5370 return type;
5371 if (!type_has_non_user_provided_default_constructor (type))
5372 return NULL_TREE;
5373 for (binfo = TYPE_BINFO (type), i = 0;
5374 BINFO_BASE_ITERATE (binfo, i, t); ++i)
5376 r = default_init_uninitialized_part (BINFO_TYPE (t));
5377 if (r)
5378 return r;
5380 for (t = TYPE_FIELDS (type); t; t = DECL_CHAIN (t))
5381 if (TREE_CODE (t) == FIELD_DECL
5382 && !DECL_ARTIFICIAL (t)
5383 && !DECL_INITIAL (t))
5385 r = default_init_uninitialized_part (TREE_TYPE (t));
5386 if (r)
5387 return DECL_P (r) ? r : t;
5390 return NULL_TREE;
5393 /* Returns true iff for class T, a trivial synthesized default constructor
5394 would be constexpr. */
5396 bool
5397 trivial_default_constructor_is_constexpr (tree t)
5399 /* A defaulted trivial default constructor is constexpr
5400 if there is nothing to initialize. */
5401 gcc_assert (!TYPE_HAS_COMPLEX_DFLT (t));
5402 return is_really_empty_class (t);
5405 /* Returns true iff class T has a constexpr default constructor. */
5407 bool
5408 type_has_constexpr_default_constructor (tree t)
5410 tree fns;
5412 if (!CLASS_TYPE_P (t))
5414 /* The caller should have stripped an enclosing array. */
5415 gcc_assert (TREE_CODE (t) != ARRAY_TYPE);
5416 return false;
5418 if (CLASSTYPE_LAZY_DEFAULT_CTOR (t))
5420 if (!TYPE_HAS_COMPLEX_DFLT (t))
5421 return trivial_default_constructor_is_constexpr (t);
5422 /* Non-trivial, we need to check subobject constructors. */
5423 lazily_declare_fn (sfk_constructor, t);
5425 fns = locate_ctor (t);
5426 return (fns && DECL_DECLARED_CONSTEXPR_P (fns));
5429 /* Returns true iff class T has a constexpr default constructor or has an
5430 implicitly declared default constructor that we can't tell if it's constexpr
5431 without forcing a lazy declaration (which might cause undesired
5432 instantiations). */
5434 bool
5435 type_maybe_constexpr_default_constructor (tree t)
5437 if (CLASS_TYPE_P (t) && CLASSTYPE_LAZY_DEFAULT_CTOR (t)
5438 && TYPE_HAS_COMPLEX_DFLT (t))
5439 /* Assume it's constexpr. */
5440 return true;
5441 return type_has_constexpr_default_constructor (t);
5444 /* Returns true iff class TYPE has a virtual destructor. */
5446 bool
5447 type_has_virtual_destructor (tree type)
5449 tree dtor;
5451 if (!CLASS_TYPE_P (type))
5452 return false;
5454 gcc_assert (COMPLETE_TYPE_P (type));
5455 dtor = CLASSTYPE_DESTRUCTOR (type);
5456 return (dtor && DECL_VIRTUAL_P (dtor));
5459 /* Returns true iff T, a class, has a move-assignment or
5460 move-constructor. Does not lazily declare either.
5461 If USER_P is false, any move function will do. If it is true, the
5462 move function must be user-declared.
5464 Note that user-declared here is different from "user-provided",
5465 which doesn't include functions that are defaulted in the
5466 class. */
5468 bool
5469 classtype_has_move_assign_or_move_ctor_p (tree t, bool user_p)
5471 gcc_assert (user_p
5472 || (!CLASSTYPE_LAZY_MOVE_CTOR (t)
5473 && !CLASSTYPE_LAZY_MOVE_ASSIGN (t)));
5475 if (!CLASSTYPE_METHOD_VEC (t))
5476 return false;
5478 if (!CLASSTYPE_LAZY_MOVE_CTOR (t))
5479 for (ovl_iterator iter (lookup_fnfields_slot_nolazy (t, ctor_identifier));
5480 iter; ++iter)
5481 if ((!user_p || !DECL_ARTIFICIAL (*iter)) && move_fn_p (*iter))
5482 return true;
5484 if (!CLASSTYPE_LAZY_MOVE_ASSIGN (t))
5485 for (ovl_iterator iter (lookup_fnfields_slot_nolazy
5486 (t, cp_assignment_operator_id (NOP_EXPR)));
5487 iter; ++iter)
5488 if ((!user_p || !DECL_ARTIFICIAL (*iter)) && move_fn_p (*iter))
5489 return true;
5491 return false;
5494 /* Nonzero if we need to build up a constructor call when initializing an
5495 object of this class, either because it has a user-declared constructor
5496 or because it doesn't have a default constructor (so we need to give an
5497 error if no initializer is provided). Use TYPE_NEEDS_CONSTRUCTING when
5498 what you care about is whether or not an object can be produced by a
5499 constructor (e.g. so we don't set TREE_READONLY on const variables of
5500 such type); use this function when what you care about is whether or not
5501 to try to call a constructor to create an object. The latter case is
5502 the former plus some cases of constructors that cannot be called. */
5504 bool
5505 type_build_ctor_call (tree t)
5507 tree inner;
5508 if (TYPE_NEEDS_CONSTRUCTING (t))
5509 return true;
5510 inner = strip_array_types (t);
5511 if (!CLASS_TYPE_P (inner) || ANON_AGGR_TYPE_P (inner))
5512 return false;
5513 if (!TYPE_HAS_DEFAULT_CONSTRUCTOR (inner))
5514 return true;
5515 if (cxx_dialect < cxx11)
5516 return false;
5517 /* A user-declared constructor might be private, and a constructor might
5518 be trivial but deleted. */
5519 for (ovl_iterator iter
5520 (lookup_fnfields_slot (inner, complete_ctor_identifier));
5521 iter; ++iter)
5523 tree fn = *iter;
5524 if (!DECL_ARTIFICIAL (fn)
5525 || DECL_DELETED_FN (fn))
5526 return true;
5528 return false;
5531 /* Like type_build_ctor_call, but for destructors. */
5533 bool
5534 type_build_dtor_call (tree t)
5536 tree inner;
5537 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t))
5538 return true;
5539 inner = strip_array_types (t);
5540 if (!CLASS_TYPE_P (inner) || ANON_AGGR_TYPE_P (inner)
5541 || !COMPLETE_TYPE_P (inner))
5542 return false;
5543 if (cxx_dialect < cxx11)
5544 return false;
5545 /* A user-declared destructor might be private, and a destructor might
5546 be trivial but deleted. */
5547 for (ovl_iterator iter
5548 (lookup_fnfields_slot (inner, complete_dtor_identifier));
5549 iter; ++iter)
5551 tree fn = *iter;
5552 if (!DECL_ARTIFICIAL (fn)
5553 || DECL_DELETED_FN (fn))
5554 return true;
5556 return false;
5559 /* Remove all zero-width bit-fields from T. */
5561 static void
5562 remove_zero_width_bit_fields (tree t)
5564 tree *fieldsp;
5566 fieldsp = &TYPE_FIELDS (t);
5567 while (*fieldsp)
5569 if (TREE_CODE (*fieldsp) == FIELD_DECL
5570 && DECL_C_BIT_FIELD (*fieldsp)
5571 /* We should not be confused by the fact that grokbitfield
5572 temporarily sets the width of the bit field into
5573 DECL_INITIAL (*fieldsp).
5574 check_bitfield_decl eventually sets DECL_SIZE (*fieldsp)
5575 to that width. */
5576 && (DECL_SIZE (*fieldsp) == NULL_TREE
5577 || integer_zerop (DECL_SIZE (*fieldsp))))
5578 *fieldsp = DECL_CHAIN (*fieldsp);
5579 else
5580 fieldsp = &DECL_CHAIN (*fieldsp);
5584 /* Returns TRUE iff we need a cookie when dynamically allocating an
5585 array whose elements have the indicated class TYPE. */
5587 static bool
5588 type_requires_array_cookie (tree type)
5590 tree fns;
5591 bool has_two_argument_delete_p = false;
5593 gcc_assert (CLASS_TYPE_P (type));
5595 /* If there's a non-trivial destructor, we need a cookie. In order
5596 to iterate through the array calling the destructor for each
5597 element, we'll have to know how many elements there are. */
5598 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
5599 return true;
5601 /* If the usual deallocation function is a two-argument whose second
5602 argument is of type `size_t', then we have to pass the size of
5603 the array to the deallocation function, so we will need to store
5604 a cookie. */
5605 fns = lookup_fnfields (TYPE_BINFO (type),
5606 cp_operator_id (VEC_DELETE_EXPR),
5607 /*protect=*/0);
5608 /* If there are no `operator []' members, or the lookup is
5609 ambiguous, then we don't need a cookie. */
5610 if (!fns || fns == error_mark_node)
5611 return false;
5612 /* Loop through all of the functions. */
5613 for (lkp_iterator iter (BASELINK_FUNCTIONS (fns)); iter; ++iter)
5615 tree fn = *iter;
5617 /* See if this function is a one-argument delete function. If
5618 it is, then it will be the usual deallocation function. */
5619 tree second_parm = TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (fn)));
5620 if (second_parm == void_list_node)
5621 return false;
5622 /* Do not consider this function if its second argument is an
5623 ellipsis. */
5624 if (!second_parm)
5625 continue;
5626 /* Otherwise, if we have a two-argument function and the second
5627 argument is `size_t', it will be the usual deallocation
5628 function -- unless there is one-argument function, too. */
5629 if (TREE_CHAIN (second_parm) == void_list_node
5630 && same_type_p (TREE_VALUE (second_parm), size_type_node))
5631 has_two_argument_delete_p = true;
5634 return has_two_argument_delete_p;
5637 /* Finish computing the `literal type' property of class type T.
5639 At this point, we have already processed base classes and
5640 non-static data members. We need to check whether the copy
5641 constructor is trivial, the destructor is trivial, and there
5642 is a trivial default constructor or at least one constexpr
5643 constructor other than the copy constructor. */
5645 static void
5646 finalize_literal_type_property (tree t)
5648 tree fn;
5650 if (cxx_dialect < cxx11
5651 || TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t))
5652 CLASSTYPE_LITERAL_P (t) = false;
5653 else if (CLASSTYPE_LITERAL_P (t) && LAMBDA_TYPE_P (t))
5654 CLASSTYPE_LITERAL_P (t) = (cxx_dialect >= cxx1z);
5655 else if (CLASSTYPE_LITERAL_P (t) && !TYPE_HAS_TRIVIAL_DFLT (t)
5656 && CLASSTYPE_NON_AGGREGATE (t)
5657 && !TYPE_HAS_CONSTEXPR_CTOR (t))
5658 CLASSTYPE_LITERAL_P (t) = false;
5660 /* C++14 DR 1684 removed this restriction. */
5661 if (cxx_dialect < cxx14
5662 && !CLASSTYPE_LITERAL_P (t) && !LAMBDA_TYPE_P (t))
5663 for (fn = TYPE_FIELDS (t); fn; fn = DECL_CHAIN (fn))
5664 if (TREE_CODE (fn) == FUNCTION_DECL
5665 && DECL_DECLARED_CONSTEXPR_P (fn)
5666 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
5667 && !DECL_CONSTRUCTOR_P (fn))
5669 DECL_DECLARED_CONSTEXPR_P (fn) = false;
5670 if (!DECL_GENERATED_P (fn)
5671 && pedwarn (DECL_SOURCE_LOCATION (fn), OPT_Wpedantic,
5672 "enclosing class of constexpr non-static member "
5673 "function %q+#D is not a literal type", fn))
5674 explain_non_literal_class (t);
5678 /* T is a non-literal type used in a context which requires a constant
5679 expression. Explain why it isn't literal. */
5681 void
5682 explain_non_literal_class (tree t)
5684 static hash_set<tree> *diagnosed;
5686 if (!CLASS_TYPE_P (t))
5687 return;
5688 t = TYPE_MAIN_VARIANT (t);
5690 if (diagnosed == NULL)
5691 diagnosed = new hash_set<tree>;
5692 if (diagnosed->add (t))
5693 /* Already explained. */
5694 return;
5696 inform (0, "%q+T is not literal because:", t);
5697 if (cxx_dialect < cxx1z && LAMBDA_TYPE_P (t))
5698 inform (0, " %qT is a closure type, which is only literal in "
5699 "C++1z and later", t);
5700 else if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t))
5701 inform (0, " %q+T has a non-trivial destructor", t);
5702 else if (CLASSTYPE_NON_AGGREGATE (t)
5703 && !TYPE_HAS_TRIVIAL_DFLT (t)
5704 && !LAMBDA_TYPE_P (t)
5705 && !TYPE_HAS_CONSTEXPR_CTOR (t))
5707 inform (0, " %q+T is not an aggregate, does not have a trivial "
5708 "default constructor, and has no constexpr constructor that "
5709 "is not a copy or move constructor", t);
5710 if (type_has_non_user_provided_default_constructor (t))
5711 /* Note that we can't simply call locate_ctor because when the
5712 constructor is deleted it just returns NULL_TREE. */
5713 for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (t)); iter; ++iter)
5715 tree fn = *iter;
5716 tree parms = TYPE_ARG_TYPES (TREE_TYPE (fn));
5718 parms = skip_artificial_parms_for (fn, parms);
5720 if (sufficient_parms_p (parms))
5722 if (DECL_DELETED_FN (fn))
5723 maybe_explain_implicit_delete (fn);
5724 else
5725 explain_invalid_constexpr_fn (fn);
5726 break;
5730 else
5732 tree binfo, base_binfo, field; int i;
5733 for (binfo = TYPE_BINFO (t), i = 0;
5734 BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
5736 tree basetype = TREE_TYPE (base_binfo);
5737 if (!CLASSTYPE_LITERAL_P (basetype))
5739 inform (0, " base class %qT of %q+T is non-literal",
5740 basetype, t);
5741 explain_non_literal_class (basetype);
5742 return;
5745 for (field = TYPE_FIELDS (t); field; field = TREE_CHAIN (field))
5747 tree ftype;
5748 if (TREE_CODE (field) != FIELD_DECL)
5749 continue;
5750 ftype = TREE_TYPE (field);
5751 if (!literal_type_p (ftype))
5753 inform (DECL_SOURCE_LOCATION (field),
5754 " non-static data member %qD has non-literal type",
5755 field);
5756 if (CLASS_TYPE_P (ftype))
5757 explain_non_literal_class (ftype);
5759 if (CP_TYPE_VOLATILE_P (ftype))
5760 inform (DECL_SOURCE_LOCATION (field),
5761 " non-static data member %qD has volatile type", field);
5766 /* Check the validity of the bases and members declared in T. Add any
5767 implicitly-generated functions (like copy-constructors and
5768 assignment operators). Compute various flag bits (like
5769 CLASSTYPE_NON_LAYOUT_POD_T) for T. This routine works purely at the C++
5770 level: i.e., independently of the ABI in use. */
5772 static void
5773 check_bases_and_members (tree t)
5775 /* Nonzero if the implicitly generated copy constructor should take
5776 a non-const reference argument. */
5777 int cant_have_const_ctor;
5778 /* Nonzero if the implicitly generated assignment operator
5779 should take a non-const reference argument. */
5780 int no_const_asn_ref;
5781 tree access_decls;
5782 bool saved_complex_asn_ref;
5783 bool saved_nontrivial_dtor;
5784 tree fn;
5786 /* By default, we use const reference arguments and generate default
5787 constructors. */
5788 cant_have_const_ctor = 0;
5789 no_const_asn_ref = 0;
5791 /* Check all the base-classes and set FMEM members to point to arrays
5792 of potential interest. */
5793 check_bases (t, &cant_have_const_ctor, &no_const_asn_ref);
5795 /* Deduce noexcept on destructor. This needs to happen after we've set
5796 triviality flags appropriately for our bases. */
5797 if (cxx_dialect >= cxx11)
5798 if (tree dtor = CLASSTYPE_DESTRUCTOR (t))
5799 deduce_noexcept_on_destructor (dtor);
5801 /* Check all the method declarations. */
5802 check_methods (t);
5804 /* Save the initial values of these flags which only indicate whether
5805 or not the class has user-provided functions. As we analyze the
5806 bases and members we can set these flags for other reasons. */
5807 saved_complex_asn_ref = TYPE_HAS_COMPLEX_COPY_ASSIGN (t);
5808 saved_nontrivial_dtor = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t);
5810 /* Check all the data member declarations. We cannot call
5811 check_field_decls until we have called check_bases check_methods,
5812 as check_field_decls depends on TYPE_HAS_NONTRIVIAL_DESTRUCTOR
5813 being set appropriately. */
5814 check_field_decls (t, &access_decls,
5815 &cant_have_const_ctor,
5816 &no_const_asn_ref);
5818 /* A nearly-empty class has to be vptr-containing; a nearly empty
5819 class contains just a vptr. */
5820 if (!TYPE_CONTAINS_VPTR_P (t))
5821 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
5823 /* Do some bookkeeping that will guide the generation of implicitly
5824 declared member functions. */
5825 TYPE_HAS_COMPLEX_COPY_CTOR (t) |= TYPE_CONTAINS_VPTR_P (t);
5826 TYPE_HAS_COMPLEX_MOVE_CTOR (t) |= TYPE_CONTAINS_VPTR_P (t);
5827 /* We need to call a constructor for this class if it has a
5828 user-provided constructor, or if the default constructor is going
5829 to initialize the vptr. (This is not an if-and-only-if;
5830 TYPE_NEEDS_CONSTRUCTING is set elsewhere if bases or members
5831 themselves need constructing.) */
5832 TYPE_NEEDS_CONSTRUCTING (t)
5833 |= (type_has_user_provided_constructor (t) || TYPE_CONTAINS_VPTR_P (t));
5834 /* [dcl.init.aggr]
5836 An aggregate is an array or a class with no user-provided
5837 constructors ... and no virtual functions.
5839 Again, other conditions for being an aggregate are checked
5840 elsewhere. */
5841 CLASSTYPE_NON_AGGREGATE (t)
5842 |= (type_has_user_provided_or_explicit_constructor (t)
5843 || TYPE_POLYMORPHIC_P (t));
5844 /* This is the C++98/03 definition of POD; it changed in C++0x, but we
5845 retain the old definition internally for ABI reasons. */
5846 CLASSTYPE_NON_LAYOUT_POD_P (t)
5847 |= (CLASSTYPE_NON_AGGREGATE (t)
5848 || saved_nontrivial_dtor || saved_complex_asn_ref);
5849 CLASSTYPE_NON_STD_LAYOUT (t) |= TYPE_CONTAINS_VPTR_P (t);
5850 TYPE_HAS_COMPLEX_COPY_ASSIGN (t) |= TYPE_CONTAINS_VPTR_P (t);
5851 TYPE_HAS_COMPLEX_MOVE_ASSIGN (t) |= TYPE_CONTAINS_VPTR_P (t);
5852 TYPE_HAS_COMPLEX_DFLT (t) |= TYPE_CONTAINS_VPTR_P (t);
5854 /* If the only explicitly declared default constructor is user-provided,
5855 set TYPE_HAS_COMPLEX_DFLT. */
5856 if (!TYPE_HAS_COMPLEX_DFLT (t)
5857 && TYPE_HAS_DEFAULT_CONSTRUCTOR (t)
5858 && !type_has_non_user_provided_default_constructor (t))
5859 TYPE_HAS_COMPLEX_DFLT (t) = true;
5861 /* Warn if a public base of a polymorphic type has an accessible
5862 non-virtual destructor. It is only now that we know the class is
5863 polymorphic. Although a polymorphic base will have a already
5864 been diagnosed during its definition, we warn on use too. */
5865 if (TYPE_POLYMORPHIC_P (t) && warn_nonvdtor)
5867 tree binfo = TYPE_BINFO (t);
5868 vec<tree, va_gc> *accesses = BINFO_BASE_ACCESSES (binfo);
5869 tree base_binfo;
5870 unsigned i;
5872 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
5874 tree basetype = TREE_TYPE (base_binfo);
5876 if ((*accesses)[i] == access_public_node
5877 && (TYPE_POLYMORPHIC_P (basetype) || warn_ecpp)
5878 && accessible_nvdtor_p (basetype))
5879 warning (OPT_Wnon_virtual_dtor,
5880 "base class %q#T has accessible non-virtual destructor",
5881 basetype);
5885 /* If the class has no user-declared constructor, but does have
5886 non-static const or reference data members that can never be
5887 initialized, issue a warning. */
5888 if (warn_uninitialized
5889 /* Classes with user-declared constructors are presumed to
5890 initialize these members. */
5891 && !TYPE_HAS_USER_CONSTRUCTOR (t)
5892 /* Aggregates can be initialized with brace-enclosed
5893 initializers. */
5894 && CLASSTYPE_NON_AGGREGATE (t))
5896 tree field;
5898 for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
5900 tree type;
5902 if (TREE_CODE (field) != FIELD_DECL
5903 || DECL_INITIAL (field) != NULL_TREE)
5904 continue;
5906 type = TREE_TYPE (field);
5907 if (TREE_CODE (type) == REFERENCE_TYPE)
5908 warning_at (DECL_SOURCE_LOCATION (field),
5909 OPT_Wuninitialized, "non-static reference %q#D "
5910 "in class without a constructor", field);
5911 else if (CP_TYPE_CONST_P (type)
5912 && (!CLASS_TYPE_P (type)
5913 || !TYPE_HAS_DEFAULT_CONSTRUCTOR (type)))
5914 warning_at (DECL_SOURCE_LOCATION (field),
5915 OPT_Wuninitialized, "non-static const member %q#D "
5916 "in class without a constructor", field);
5920 /* Synthesize any needed methods. */
5921 add_implicitly_declared_members (t, &access_decls,
5922 cant_have_const_ctor,
5923 no_const_asn_ref);
5925 /* Check defaulted declarations here so we have cant_have_const_ctor
5926 and don't need to worry about clones. */
5927 for (fn = TYPE_FIELDS (t); fn; fn = DECL_CHAIN (fn))
5928 if (DECL_DECLARES_FUNCTION_P (fn)
5929 && !DECL_ARTIFICIAL (fn)
5930 && DECL_DEFAULTED_IN_CLASS_P (fn))
5932 int copy = copy_fn_p (fn);
5933 if (copy > 0)
5935 bool imp_const_p
5936 = (DECL_CONSTRUCTOR_P (fn) ? !cant_have_const_ctor
5937 : !no_const_asn_ref);
5938 bool fn_const_p = (copy == 2);
5940 if (fn_const_p && !imp_const_p)
5941 /* If the function is defaulted outside the class, we just
5942 give the synthesis error. */
5943 error ("%q+D declared to take const reference, but implicit "
5944 "declaration would take non-const", fn);
5946 defaulted_late_check (fn);
5949 if (LAMBDA_TYPE_P (t))
5951 /* "This class type is not an aggregate." */
5952 CLASSTYPE_NON_AGGREGATE (t) = 1;
5955 /* Compute the 'literal type' property before we
5956 do anything with non-static member functions. */
5957 finalize_literal_type_property (t);
5959 /* Create the in-charge and not-in-charge variants of constructors
5960 and destructors. */
5961 clone_constructors_and_destructors (t);
5963 /* Process the using-declarations. */
5964 for (; access_decls; access_decls = TREE_CHAIN (access_decls))
5965 handle_using_decl (TREE_VALUE (access_decls), t);
5967 /* Build and sort the CLASSTYPE_METHOD_VEC. */
5968 finish_struct_methods (t);
5970 /* Figure out whether or not we will need a cookie when dynamically
5971 allocating an array of this type. */
5972 LANG_TYPE_CLASS_CHECK (t)->vec_new_uses_cookie
5973 = type_requires_array_cookie (t);
5976 /* If T needs a pointer to its virtual function table, set TYPE_VFIELD
5977 accordingly. If a new vfield was created (because T doesn't have a
5978 primary base class), then the newly created field is returned. It
5979 is not added to the TYPE_FIELDS list; it is the caller's
5980 responsibility to do that. Accumulate declared virtual functions
5981 on VIRTUALS_P. */
5983 static tree
5984 create_vtable_ptr (tree t, tree* virtuals_p)
5986 tree fn;
5988 /* Collect the virtual functions declared in T. */
5989 for (fn = TYPE_FIELDS (t); fn; fn = DECL_CHAIN (fn))
5990 if (TREE_CODE (fn) == FUNCTION_DECL
5991 && DECL_VINDEX (fn) && !DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (fn)
5992 && TREE_CODE (DECL_VINDEX (fn)) != INTEGER_CST)
5994 tree new_virtual = make_node (TREE_LIST);
5996 BV_FN (new_virtual) = fn;
5997 BV_DELTA (new_virtual) = integer_zero_node;
5998 BV_VCALL_INDEX (new_virtual) = NULL_TREE;
6000 TREE_CHAIN (new_virtual) = *virtuals_p;
6001 *virtuals_p = new_virtual;
6004 /* If we couldn't find an appropriate base class, create a new field
6005 here. Even if there weren't any new virtual functions, we might need a
6006 new virtual function table if we're supposed to include vptrs in
6007 all classes that need them. */
6008 if (!TYPE_VFIELD (t) && (*virtuals_p || TYPE_CONTAINS_VPTR_P (t)))
6010 /* We build this decl with vtbl_ptr_type_node, which is a
6011 `vtable_entry_type*'. It might seem more precise to use
6012 `vtable_entry_type (*)[N]' where N is the number of virtual
6013 functions. However, that would require the vtable pointer in
6014 base classes to have a different type than the vtable pointer
6015 in derived classes. We could make that happen, but that
6016 still wouldn't solve all the problems. In particular, the
6017 type-based alias analysis code would decide that assignments
6018 to the base class vtable pointer can't alias assignments to
6019 the derived class vtable pointer, since they have different
6020 types. Thus, in a derived class destructor, where the base
6021 class constructor was inlined, we could generate bad code for
6022 setting up the vtable pointer.
6024 Therefore, we use one type for all vtable pointers. We still
6025 use a type-correct type; it's just doesn't indicate the array
6026 bounds. That's better than using `void*' or some such; it's
6027 cleaner, and it let's the alias analysis code know that these
6028 stores cannot alias stores to void*! */
6029 tree field;
6031 field = build_decl (input_location,
6032 FIELD_DECL, get_vfield_name (t), vtbl_ptr_type_node);
6033 DECL_VIRTUAL_P (field) = 1;
6034 DECL_ARTIFICIAL (field) = 1;
6035 DECL_FIELD_CONTEXT (field) = t;
6036 DECL_FCONTEXT (field) = t;
6037 if (TYPE_PACKED (t))
6038 DECL_PACKED (field) = 1;
6040 TYPE_VFIELD (t) = field;
6042 /* This class is non-empty. */
6043 CLASSTYPE_EMPTY_P (t) = 0;
6045 return field;
6048 return NULL_TREE;
6051 /* Add OFFSET to all base types of BINFO which is a base in the
6052 hierarchy dominated by T.
6054 OFFSET, which is a type offset, is number of bytes. */
6056 static void
6057 propagate_binfo_offsets (tree binfo, tree offset)
6059 int i;
6060 tree primary_binfo;
6061 tree base_binfo;
6063 /* Update BINFO's offset. */
6064 BINFO_OFFSET (binfo)
6065 = fold_convert (sizetype,
6066 size_binop (PLUS_EXPR,
6067 fold_convert (ssizetype, BINFO_OFFSET (binfo)),
6068 offset));
6070 /* Find the primary base class. */
6071 primary_binfo = get_primary_binfo (binfo);
6073 if (primary_binfo && BINFO_INHERITANCE_CHAIN (primary_binfo) == binfo)
6074 propagate_binfo_offsets (primary_binfo, offset);
6076 /* Scan all of the bases, pushing the BINFO_OFFSET adjust
6077 downwards. */
6078 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
6080 /* Don't do the primary base twice. */
6081 if (base_binfo == primary_binfo)
6082 continue;
6084 if (BINFO_VIRTUAL_P (base_binfo))
6085 continue;
6087 propagate_binfo_offsets (base_binfo, offset);
6091 /* Set BINFO_OFFSET for all of the virtual bases for RLI->T. Update
6092 TYPE_ALIGN and TYPE_SIZE for T. OFFSETS gives the location of
6093 empty subobjects of T. */
6095 static void
6096 layout_virtual_bases (record_layout_info rli, splay_tree offsets)
6098 tree vbase;
6099 tree t = rli->t;
6100 tree *next_field;
6102 if (BINFO_N_BASE_BINFOS (TYPE_BINFO (t)) == 0)
6103 return;
6105 /* Find the last field. The artificial fields created for virtual
6106 bases will go after the last extant field to date. */
6107 next_field = &TYPE_FIELDS (t);
6108 while (*next_field)
6109 next_field = &DECL_CHAIN (*next_field);
6111 /* Go through the virtual bases, allocating space for each virtual
6112 base that is not already a primary base class. These are
6113 allocated in inheritance graph order. */
6114 for (vbase = TYPE_BINFO (t); vbase; vbase = TREE_CHAIN (vbase))
6116 if (!BINFO_VIRTUAL_P (vbase))
6117 continue;
6119 if (!BINFO_PRIMARY_P (vbase))
6121 /* This virtual base is not a primary base of any class in the
6122 hierarchy, so we have to add space for it. */
6123 next_field = build_base_field (rli, vbase,
6124 offsets, next_field);
6129 /* Returns the offset of the byte just past the end of the base class
6130 BINFO. */
6132 static tree
6133 end_of_base (tree binfo)
6135 tree size;
6137 if (!CLASSTYPE_AS_BASE (BINFO_TYPE (binfo)))
6138 size = TYPE_SIZE_UNIT (char_type_node);
6139 else if (is_empty_class (BINFO_TYPE (binfo)))
6140 /* An empty class has zero CLASSTYPE_SIZE_UNIT, but we need to
6141 allocate some space for it. It cannot have virtual bases, so
6142 TYPE_SIZE_UNIT is fine. */
6143 size = TYPE_SIZE_UNIT (BINFO_TYPE (binfo));
6144 else
6145 size = CLASSTYPE_SIZE_UNIT (BINFO_TYPE (binfo));
6147 return size_binop (PLUS_EXPR, BINFO_OFFSET (binfo), size);
6150 /* Returns the offset of the byte just past the end of the base class
6151 with the highest offset in T. If INCLUDE_VIRTUALS_P is zero, then
6152 only non-virtual bases are included. */
6154 static tree
6155 end_of_class (tree t, int include_virtuals_p)
6157 tree result = size_zero_node;
6158 vec<tree, va_gc> *vbases;
6159 tree binfo;
6160 tree base_binfo;
6161 tree offset;
6162 int i;
6164 for (binfo = TYPE_BINFO (t), i = 0;
6165 BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
6167 if (!include_virtuals_p
6168 && BINFO_VIRTUAL_P (base_binfo)
6169 && (!BINFO_PRIMARY_P (base_binfo)
6170 || BINFO_INHERITANCE_CHAIN (base_binfo) != TYPE_BINFO (t)))
6171 continue;
6173 offset = end_of_base (base_binfo);
6174 if (tree_int_cst_lt (result, offset))
6175 result = offset;
6178 if (include_virtuals_p)
6179 for (vbases = CLASSTYPE_VBASECLASSES (t), i = 0;
6180 vec_safe_iterate (vbases, i, &base_binfo); i++)
6182 offset = end_of_base (base_binfo);
6183 if (tree_int_cst_lt (result, offset))
6184 result = offset;
6187 return result;
6190 /* Warn about bases of T that are inaccessible because they are
6191 ambiguous. For example:
6193 struct S {};
6194 struct T : public S {};
6195 struct U : public S, public T {};
6197 Here, `(S*) new U' is not allowed because there are two `S'
6198 subobjects of U. */
6200 static void
6201 warn_about_ambiguous_bases (tree t)
6203 int i;
6204 vec<tree, va_gc> *vbases;
6205 tree basetype;
6206 tree binfo;
6207 tree base_binfo;
6209 /* If there are no repeated bases, nothing can be ambiguous. */
6210 if (!CLASSTYPE_REPEATED_BASE_P (t))
6211 return;
6213 /* Check direct bases. */
6214 for (binfo = TYPE_BINFO (t), i = 0;
6215 BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
6217 basetype = BINFO_TYPE (base_binfo);
6219 if (!uniquely_derived_from_p (basetype, t))
6220 warning (0, "direct base %qT inaccessible in %qT due to ambiguity",
6221 basetype, t);
6224 /* Check for ambiguous virtual bases. */
6225 if (extra_warnings)
6226 for (vbases = CLASSTYPE_VBASECLASSES (t), i = 0;
6227 vec_safe_iterate (vbases, i, &binfo); i++)
6229 basetype = BINFO_TYPE (binfo);
6231 if (!uniquely_derived_from_p (basetype, t))
6232 warning (OPT_Wextra, "virtual base %qT inaccessible in %qT due "
6233 "to ambiguity", basetype, t);
6237 /* Compare two INTEGER_CSTs K1 and K2. */
6239 static int
6240 splay_tree_compare_integer_csts (splay_tree_key k1, splay_tree_key k2)
6242 return tree_int_cst_compare ((tree) k1, (tree) k2);
6245 /* Increase the size indicated in RLI to account for empty classes
6246 that are "off the end" of the class. */
6248 static void
6249 include_empty_classes (record_layout_info rli)
6251 tree eoc;
6252 tree rli_size;
6254 /* It might be the case that we grew the class to allocate a
6255 zero-sized base class. That won't be reflected in RLI, yet,
6256 because we are willing to overlay multiple bases at the same
6257 offset. However, now we need to make sure that RLI is big enough
6258 to reflect the entire class. */
6259 eoc = end_of_class (rli->t, CLASSTYPE_AS_BASE (rli->t) != NULL_TREE);
6260 rli_size = rli_size_unit_so_far (rli);
6261 if (TREE_CODE (rli_size) == INTEGER_CST
6262 && tree_int_cst_lt (rli_size, eoc))
6264 /* The size should have been rounded to a whole byte. */
6265 gcc_assert (tree_int_cst_equal
6266 (rli->bitpos, round_down (rli->bitpos, BITS_PER_UNIT)));
6267 rli->bitpos
6268 = size_binop (PLUS_EXPR,
6269 rli->bitpos,
6270 size_binop (MULT_EXPR,
6271 fold_convert (bitsizetype,
6272 size_binop (MINUS_EXPR,
6273 eoc, rli_size)),
6274 bitsize_int (BITS_PER_UNIT)));
6275 normalize_rli (rli);
6279 /* Calculate the TYPE_SIZE, TYPE_ALIGN, etc for T. Calculate
6280 BINFO_OFFSETs for all of the base-classes. Position the vtable
6281 pointer. Accumulate declared virtual functions on VIRTUALS_P. */
6283 static void
6284 layout_class_type (tree t, tree *virtuals_p)
6286 tree non_static_data_members;
6287 tree field;
6288 tree vptr;
6289 record_layout_info rli;
6290 /* Maps offsets (represented as INTEGER_CSTs) to a TREE_LIST of
6291 types that appear at that offset. */
6292 splay_tree empty_base_offsets;
6293 /* True if the last field laid out was a bit-field. */
6294 bool last_field_was_bitfield = false;
6295 /* The location at which the next field should be inserted. */
6296 tree *next_field;
6297 /* T, as a base class. */
6298 tree base_t;
6300 /* Keep track of the first non-static data member. */
6301 non_static_data_members = TYPE_FIELDS (t);
6303 /* Start laying out the record. */
6304 rli = start_record_layout (t);
6306 /* Mark all the primary bases in the hierarchy. */
6307 determine_primary_bases (t);
6309 /* Create a pointer to our virtual function table. */
6310 vptr = create_vtable_ptr (t, virtuals_p);
6312 /* The vptr is always the first thing in the class. */
6313 if (vptr)
6315 DECL_CHAIN (vptr) = TYPE_FIELDS (t);
6316 TYPE_FIELDS (t) = vptr;
6317 next_field = &DECL_CHAIN (vptr);
6318 place_field (rli, vptr);
6320 else
6321 next_field = &TYPE_FIELDS (t);
6323 /* Build FIELD_DECLs for all of the non-virtual base-types. */
6324 empty_base_offsets = splay_tree_new (splay_tree_compare_integer_csts,
6325 NULL, NULL);
6326 build_base_fields (rli, empty_base_offsets, next_field);
6328 /* Layout the non-static data members. */
6329 for (field = non_static_data_members; field; field = DECL_CHAIN (field))
6331 tree type;
6332 tree padding;
6334 /* We still pass things that aren't non-static data members to
6335 the back end, in case it wants to do something with them. */
6336 if (TREE_CODE (field) != FIELD_DECL)
6338 place_field (rli, field);
6339 /* If the static data member has incomplete type, keep track
6340 of it so that it can be completed later. (The handling
6341 of pending statics in finish_record_layout is
6342 insufficient; consider:
6344 struct S1;
6345 struct S2 { static S1 s1; };
6347 At this point, finish_record_layout will be called, but
6348 S1 is still incomplete.) */
6349 if (VAR_P (field))
6351 maybe_register_incomplete_var (field);
6352 /* The visibility of static data members is determined
6353 at their point of declaration, not their point of
6354 definition. */
6355 determine_visibility (field);
6357 continue;
6360 type = TREE_TYPE (field);
6361 if (type == error_mark_node)
6362 continue;
6364 padding = NULL_TREE;
6366 /* If this field is a bit-field whose width is greater than its
6367 type, then there are some special rules for allocating
6368 it. */
6369 if (DECL_C_BIT_FIELD (field)
6370 && tree_int_cst_lt (TYPE_SIZE (type), DECL_SIZE (field)))
6372 bool was_unnamed_p = false;
6373 /* We must allocate the bits as if suitably aligned for the
6374 longest integer type that fits in this many bits. Then,
6375 we are supposed to use the left over bits as additional
6376 padding. */
6378 /* Do not pick a type bigger than MAX_FIXED_MODE_SIZE. */
6379 tree limit = size_int (MAX_FIXED_MODE_SIZE);
6380 if (tree_int_cst_lt (DECL_SIZE (field), limit))
6381 limit = DECL_SIZE (field);
6383 tree integer_type = integer_types[itk_char];
6384 for (unsigned itk = itk_char; itk != itk_none; itk++)
6385 if (tree next = integer_types[itk])
6387 if (tree_int_cst_lt (limit, TYPE_SIZE (next)))
6388 /* Too big, so our current guess is what we want. */
6389 break;
6390 /* Not bigger than limit, ok */
6391 integer_type = next;
6394 /* Figure out how much additional padding is required. */
6395 if (TREE_CODE (t) == UNION_TYPE)
6396 /* In a union, the padding field must have the full width
6397 of the bit-field; all fields start at offset zero. */
6398 padding = DECL_SIZE (field);
6399 else
6400 padding = size_binop (MINUS_EXPR, DECL_SIZE (field),
6401 TYPE_SIZE (integer_type));
6403 if (integer_zerop (padding))
6404 padding = NULL_TREE;
6406 /* An unnamed bitfield does not normally affect the
6407 alignment of the containing class on a target where
6408 PCC_BITFIELD_TYPE_MATTERS. But, the C++ ABI does not
6409 make any exceptions for unnamed bitfields when the
6410 bitfields are longer than their types. Therefore, we
6411 temporarily give the field a name. */
6412 if (PCC_BITFIELD_TYPE_MATTERS && !DECL_NAME (field))
6414 was_unnamed_p = true;
6415 DECL_NAME (field) = make_anon_name ();
6418 DECL_SIZE (field) = TYPE_SIZE (integer_type);
6419 SET_DECL_ALIGN (field, TYPE_ALIGN (integer_type));
6420 DECL_USER_ALIGN (field) = TYPE_USER_ALIGN (integer_type);
6421 layout_nonempty_base_or_field (rli, field, NULL_TREE,
6422 empty_base_offsets);
6423 if (was_unnamed_p)
6424 DECL_NAME (field) = NULL_TREE;
6425 /* Now that layout has been performed, set the size of the
6426 field to the size of its declared type; the rest of the
6427 field is effectively invisible. */
6428 DECL_SIZE (field) = TYPE_SIZE (type);
6429 /* We must also reset the DECL_MODE of the field. */
6430 SET_DECL_MODE (field, TYPE_MODE (type));
6432 else
6433 layout_nonempty_base_or_field (rli, field, NULL_TREE,
6434 empty_base_offsets);
6436 /* Remember the location of any empty classes in FIELD. */
6437 record_subobject_offsets (TREE_TYPE (field),
6438 byte_position(field),
6439 empty_base_offsets,
6440 /*is_data_member=*/true);
6442 /* If a bit-field does not immediately follow another bit-field,
6443 and yet it starts in the middle of a byte, we have failed to
6444 comply with the ABI. */
6445 if (warn_abi
6446 && DECL_C_BIT_FIELD (field)
6447 /* The TREE_NO_WARNING flag gets set by Objective-C when
6448 laying out an Objective-C class. The ObjC ABI differs
6449 from the C++ ABI, and so we do not want a warning
6450 here. */
6451 && !TREE_NO_WARNING (field)
6452 && !last_field_was_bitfield
6453 && !integer_zerop (size_binop (TRUNC_MOD_EXPR,
6454 DECL_FIELD_BIT_OFFSET (field),
6455 bitsize_unit_node)))
6456 warning_at (DECL_SOURCE_LOCATION (field), OPT_Wabi,
6457 "offset of %qD is not ABI-compliant and may "
6458 "change in a future version of GCC", field);
6460 /* The middle end uses the type of expressions to determine the
6461 possible range of expression values. In order to optimize
6462 "x.i > 7" to "false" for a 2-bit bitfield "i", the middle end
6463 must be made aware of the width of "i", via its type.
6465 Because C++ does not have integer types of arbitrary width,
6466 we must (for the purposes of the front end) convert from the
6467 type assigned here to the declared type of the bitfield
6468 whenever a bitfield expression is used as an rvalue.
6469 Similarly, when assigning a value to a bitfield, the value
6470 must be converted to the type given the bitfield here. */
6471 if (DECL_C_BIT_FIELD (field))
6473 unsigned HOST_WIDE_INT width;
6474 tree ftype = TREE_TYPE (field);
6475 width = tree_to_uhwi (DECL_SIZE (field));
6476 if (width != TYPE_PRECISION (ftype))
6478 TREE_TYPE (field)
6479 = c_build_bitfield_integer_type (width,
6480 TYPE_UNSIGNED (ftype));
6481 TREE_TYPE (field)
6482 = cp_build_qualified_type (TREE_TYPE (field),
6483 cp_type_quals (ftype));
6487 /* If we needed additional padding after this field, add it
6488 now. */
6489 if (padding)
6491 tree padding_field;
6493 padding_field = build_decl (input_location,
6494 FIELD_DECL,
6495 NULL_TREE,
6496 char_type_node);
6497 DECL_BIT_FIELD (padding_field) = 1;
6498 DECL_SIZE (padding_field) = padding;
6499 DECL_CONTEXT (padding_field) = t;
6500 DECL_ARTIFICIAL (padding_field) = 1;
6501 DECL_IGNORED_P (padding_field) = 1;
6502 layout_nonempty_base_or_field (rli, padding_field,
6503 NULL_TREE,
6504 empty_base_offsets);
6507 last_field_was_bitfield = DECL_C_BIT_FIELD (field);
6510 if (!integer_zerop (rli->bitpos))
6512 /* Make sure that we are on a byte boundary so that the size of
6513 the class without virtual bases will always be a round number
6514 of bytes. */
6515 rli->bitpos = round_up_loc (input_location, rli->bitpos, BITS_PER_UNIT);
6516 normalize_rli (rli);
6519 /* Delete all zero-width bit-fields from the list of fields. Now
6520 that the type is laid out they are no longer important. */
6521 remove_zero_width_bit_fields (t);
6523 /* Create the version of T used for virtual bases. We do not use
6524 make_class_type for this version; this is an artificial type. For
6525 a POD type, we just reuse T. */
6526 if (CLASSTYPE_NON_LAYOUT_POD_P (t) || CLASSTYPE_EMPTY_P (t))
6528 base_t = make_node (TREE_CODE (t));
6530 /* Set the size and alignment for the new type. */
6531 tree eoc;
6533 /* If the ABI version is not at least two, and the last
6534 field was a bit-field, RLI may not be on a byte
6535 boundary. In particular, rli_size_unit_so_far might
6536 indicate the last complete byte, while rli_size_so_far
6537 indicates the total number of bits used. Therefore,
6538 rli_size_so_far, rather than rli_size_unit_so_far, is
6539 used to compute TYPE_SIZE_UNIT. */
6540 eoc = end_of_class (t, /*include_virtuals_p=*/0);
6541 TYPE_SIZE_UNIT (base_t)
6542 = size_binop (MAX_EXPR,
6543 fold_convert (sizetype,
6544 size_binop (CEIL_DIV_EXPR,
6545 rli_size_so_far (rli),
6546 bitsize_int (BITS_PER_UNIT))),
6547 eoc);
6548 TYPE_SIZE (base_t)
6549 = size_binop (MAX_EXPR,
6550 rli_size_so_far (rli),
6551 size_binop (MULT_EXPR,
6552 fold_convert (bitsizetype, eoc),
6553 bitsize_int (BITS_PER_UNIT)));
6554 SET_TYPE_ALIGN (base_t, rli->record_align);
6555 TYPE_USER_ALIGN (base_t) = TYPE_USER_ALIGN (t);
6557 /* Copy the fields from T. */
6558 next_field = &TYPE_FIELDS (base_t);
6559 for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
6560 if (TREE_CODE (field) == FIELD_DECL)
6562 *next_field = copy_node (field);
6563 DECL_CONTEXT (*next_field) = base_t;
6564 next_field = &DECL_CHAIN (*next_field);
6566 *next_field = NULL_TREE;
6568 /* Record the base version of the type. */
6569 CLASSTYPE_AS_BASE (t) = base_t;
6570 TYPE_CONTEXT (base_t) = t;
6572 else
6573 CLASSTYPE_AS_BASE (t) = t;
6575 /* Every empty class contains an empty class. */
6576 if (CLASSTYPE_EMPTY_P (t))
6577 CLASSTYPE_CONTAINS_EMPTY_CLASS_P (t) = 1;
6579 /* Set the TYPE_DECL for this type to contain the right
6580 value for DECL_OFFSET, so that we can use it as part
6581 of a COMPONENT_REF for multiple inheritance. */
6582 layout_decl (TYPE_MAIN_DECL (t), 0);
6584 /* Now fix up any virtual base class types that we left lying
6585 around. We must get these done before we try to lay out the
6586 virtual function table. As a side-effect, this will remove the
6587 base subobject fields. */
6588 layout_virtual_bases (rli, empty_base_offsets);
6590 /* Make sure that empty classes are reflected in RLI at this
6591 point. */
6592 include_empty_classes (rli);
6594 /* Make sure not to create any structures with zero size. */
6595 if (integer_zerop (rli_size_unit_so_far (rli)) && CLASSTYPE_EMPTY_P (t))
6596 place_field (rli,
6597 build_decl (input_location,
6598 FIELD_DECL, NULL_TREE, char_type_node));
6600 /* If this is a non-POD, declaring it packed makes a difference to how it
6601 can be used as a field; don't let finalize_record_size undo it. */
6602 if (TYPE_PACKED (t) && !layout_pod_type_p (t))
6603 rli->packed_maybe_necessary = true;
6605 /* Let the back end lay out the type. */
6606 finish_record_layout (rli, /*free_p=*/true);
6608 if (TYPE_SIZE_UNIT (t)
6609 && TREE_CODE (TYPE_SIZE_UNIT (t)) == INTEGER_CST
6610 && !TREE_OVERFLOW (TYPE_SIZE_UNIT (t))
6611 && !valid_constant_size_p (TYPE_SIZE_UNIT (t)))
6612 error ("size of type %qT is too large (%qE bytes)", t, TYPE_SIZE_UNIT (t));
6614 /* Warn about bases that can't be talked about due to ambiguity. */
6615 warn_about_ambiguous_bases (t);
6617 /* Now that we're done with layout, give the base fields the real types. */
6618 for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
6619 if (DECL_ARTIFICIAL (field) && IS_FAKE_BASE_TYPE (TREE_TYPE (field)))
6620 TREE_TYPE (field) = TYPE_CONTEXT (TREE_TYPE (field));
6622 /* Clean up. */
6623 splay_tree_delete (empty_base_offsets);
6625 if (CLASSTYPE_EMPTY_P (t)
6626 && tree_int_cst_lt (sizeof_biggest_empty_class,
6627 TYPE_SIZE_UNIT (t)))
6628 sizeof_biggest_empty_class = TYPE_SIZE_UNIT (t);
6631 /* Determine the "key method" for the class type indicated by TYPE,
6632 and set CLASSTYPE_KEY_METHOD accordingly. */
6634 void
6635 determine_key_method (tree type)
6637 tree method;
6639 if (processing_template_decl
6640 || CLASSTYPE_TEMPLATE_INSTANTIATION (type)
6641 || CLASSTYPE_INTERFACE_KNOWN (type))
6642 return;
6644 /* The key method is the first non-pure virtual function that is not
6645 inline at the point of class definition. On some targets the
6646 key function may not be inline; those targets should not call
6647 this function until the end of the translation unit. */
6648 for (method = TYPE_FIELDS (type); method; method = DECL_CHAIN (method))
6649 if (TREE_CODE (method) == FUNCTION_DECL
6650 && DECL_VINDEX (method) != NULL_TREE
6651 && ! DECL_DECLARED_INLINE_P (method)
6652 && ! DECL_PURE_VIRTUAL_P (method))
6654 CLASSTYPE_KEY_METHOD (type) = method;
6655 break;
6658 return;
6662 /* Allocate and return an instance of struct sorted_fields_type with
6663 N fields. */
6665 static struct sorted_fields_type *
6666 sorted_fields_type_new (int n)
6668 struct sorted_fields_type *sft;
6669 sft = (sorted_fields_type *) ggc_internal_alloc (sizeof (sorted_fields_type)
6670 + n * sizeof (tree));
6671 sft->len = n;
6673 return sft;
6676 /* Helper of find_flexarrays. Return true when FLD refers to a non-static
6677 class data member of non-zero size, otherwise false. */
6679 static inline bool
6680 field_nonempty_p (const_tree fld)
6682 if (TREE_CODE (fld) == ERROR_MARK)
6683 return false;
6685 tree type = TREE_TYPE (fld);
6686 if (TREE_CODE (fld) == FIELD_DECL
6687 && TREE_CODE (type) != ERROR_MARK
6688 && (DECL_NAME (fld) || RECORD_OR_UNION_TYPE_P (type)))
6690 return TYPE_SIZE (type)
6691 && (TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST
6692 || !tree_int_cst_equal (size_zero_node, TYPE_SIZE (type)));
6695 return false;
6698 /* Used by find_flexarrays and related functions. */
6700 struct flexmems_t
6702 /* The first flexible array member or non-zero array member found
6703 in the order of layout. */
6704 tree array;
6705 /* First non-static non-empty data member in the class or its bases. */
6706 tree first;
6707 /* The first non-static non-empty data member following either
6708 the flexible array member, if found, or the zero-length array member
6709 otherwise. AFTER[1] refers to the first such data member of a union
6710 of which the struct containing the flexible array member or zero-length
6711 array is a member, or NULL when no such union exists. This element is
6712 only used during searching, not for diagnosing problems. AFTER[0]
6713 refers to the first such data member that is not a member of such
6714 a union. */
6715 tree after[2];
6717 /* Refers to a struct (not union) in which the struct of which the flexible
6718 array is member is defined. Used to diagnose strictly (according to C)
6719 invalid uses of the latter structs. */
6720 tree enclosing;
6723 /* Find either the first flexible array member or the first zero-length
6724 array, in that order of preference, among members of class T (but not
6725 its base classes), and set members of FMEM accordingly.
6726 BASE_P is true if T is a base class of another class.
6727 PUN is set to the outermost union in which the flexible array member
6728 (or zero-length array) is defined if one such union exists, otherwise
6729 to NULL.
6730 Similarly, PSTR is set to a data member of the outermost struct of
6731 which the flexible array is a member if one such struct exists,
6732 otherwise to NULL. */
6734 static void
6735 find_flexarrays (tree t, flexmems_t *fmem, bool base_p,
6736 tree pun /* = NULL_TREE */,
6737 tree pstr /* = NULL_TREE */)
6739 /* Set the "pointer" to the outermost enclosing union if not set
6740 yet and maintain it for the remainder of the recursion. */
6741 if (!pun && TREE_CODE (t) == UNION_TYPE)
6742 pun = t;
6744 for (tree fld = TYPE_FIELDS (t); fld; fld = DECL_CHAIN (fld))
6746 if (fld == error_mark_node)
6747 return;
6749 /* Is FLD a typedef for an anonymous struct? */
6751 /* FIXME: Note that typedefs (as well as arrays) need to be fully
6752 handled elsewhere so that errors like the following are detected
6753 as well:
6754 typedef struct { int i, a[], j; } S; // bug c++/72753
6755 S s [2]; // bug c++/68489
6757 if (TREE_CODE (fld) == TYPE_DECL
6758 && DECL_IMPLICIT_TYPEDEF_P (fld)
6759 && CLASS_TYPE_P (TREE_TYPE (fld))
6760 && anon_aggrname_p (DECL_NAME (fld)))
6762 /* Check the nested unnamed type referenced via a typedef
6763 independently of FMEM (since it's not a data member of
6764 the enclosing class). */
6765 check_flexarrays (TREE_TYPE (fld));
6766 continue;
6769 /* Skip anything that's GCC-generated or not a (non-static) data
6770 member. */
6771 if (DECL_ARTIFICIAL (fld) || TREE_CODE (fld) != FIELD_DECL)
6772 continue;
6774 /* Type of the member. */
6775 tree fldtype = TREE_TYPE (fld);
6776 if (fldtype == error_mark_node)
6777 return;
6779 /* Determine the type of the array element or object referenced
6780 by the member so that it can be checked for flexible array
6781 members if it hasn't been yet. */
6782 tree eltype = fldtype;
6783 while (TREE_CODE (eltype) == ARRAY_TYPE
6784 || TREE_CODE (eltype) == POINTER_TYPE
6785 || TREE_CODE (eltype) == REFERENCE_TYPE)
6786 eltype = TREE_TYPE (eltype);
6788 if (RECORD_OR_UNION_TYPE_P (eltype))
6790 if (fmem->array && !fmem->after[bool (pun)])
6792 /* Once the member after the flexible array has been found
6793 we're done. */
6794 fmem->after[bool (pun)] = fld;
6795 break;
6798 if (eltype == fldtype || TYPE_UNNAMED_P (eltype))
6800 /* Descend into the non-static member struct or union and try
6801 to find a flexible array member or zero-length array among
6802 its members. This is only necessary for anonymous types
6803 and types in whose context the current type T has not been
6804 defined (the latter must not be checked again because they
6805 are already in the process of being checked by one of the
6806 recursive calls). */
6808 tree first = fmem->first;
6809 tree array = fmem->array;
6811 /* If this member isn't anonymous and a prior non-flexible array
6812 member has been seen in one of the enclosing structs, clear
6813 the FIRST member since it doesn't contribute to the flexible
6814 array struct's members. */
6815 if (first && !array && !ANON_AGGR_TYPE_P (eltype))
6816 fmem->first = NULL_TREE;
6818 find_flexarrays (eltype, fmem, false, pun,
6819 !pstr && TREE_CODE (t) == RECORD_TYPE ? fld : pstr);
6821 if (fmem->array != array)
6822 continue;
6824 if (first && !array && !ANON_AGGR_TYPE_P (eltype))
6826 /* Restore the FIRST member reset above if no flexible
6827 array member has been found in this member's struct. */
6828 fmem->first = first;
6831 /* If the member struct contains the first flexible array
6832 member, or if this member is a base class, continue to
6833 the next member and avoid setting the FMEM->NEXT pointer
6834 to point to it. */
6835 if (base_p)
6836 continue;
6840 if (field_nonempty_p (fld))
6842 /* Remember the first non-static data member. */
6843 if (!fmem->first)
6844 fmem->first = fld;
6846 /* Remember the first non-static data member after the flexible
6847 array member, if one has been found, or the zero-length array
6848 if it has been found. */
6849 if (fmem->array && !fmem->after[bool (pun)])
6850 fmem->after[bool (pun)] = fld;
6853 /* Skip non-arrays. */
6854 if (TREE_CODE (fldtype) != ARRAY_TYPE)
6855 continue;
6857 /* Determine the upper bound of the array if it has one. */
6858 if (TYPE_DOMAIN (fldtype))
6860 if (fmem->array)
6862 /* Make a record of the zero-length array if either one
6863 such field or a flexible array member has been seen to
6864 handle the pathological and unlikely case of multiple
6865 such members. */
6866 if (!fmem->after[bool (pun)])
6867 fmem->after[bool (pun)] = fld;
6869 else if (integer_all_onesp (TYPE_MAX_VALUE (TYPE_DOMAIN (fldtype))))
6871 /* Remember the first zero-length array unless a flexible array
6872 member has already been seen. */
6873 fmem->array = fld;
6874 fmem->enclosing = pstr;
6877 else
6879 /* Flexible array members have no upper bound. */
6880 if (fmem->array)
6882 /* Replace the zero-length array if it's been stored and
6883 reset the after pointer. */
6884 if (TYPE_DOMAIN (TREE_TYPE (fmem->array)))
6886 fmem->after[bool (pun)] = NULL_TREE;
6887 fmem->array = fld;
6888 fmem->enclosing = pstr;
6891 else
6893 fmem->array = fld;
6894 fmem->enclosing = pstr;
6900 /* Diagnose a strictly (by the C standard) invalid use of a struct with
6901 a flexible array member (or the zero-length array extension). */
6903 static void
6904 diagnose_invalid_flexarray (const flexmems_t *fmem)
6906 if (fmem->array && fmem->enclosing
6907 && pedwarn (location_of (fmem->enclosing), OPT_Wpedantic,
6908 TYPE_DOMAIN (TREE_TYPE (fmem->array))
6909 ? G_("invalid use of %q#T with a zero-size array "
6910 "in %q#D")
6911 : G_("invalid use of %q#T with a flexible array member "
6912 "in %q#T"),
6913 DECL_CONTEXT (fmem->array),
6914 DECL_CONTEXT (fmem->enclosing)))
6915 inform (DECL_SOURCE_LOCATION (fmem->array),
6916 "array member %q#D declared here", fmem->array);
6919 /* Issue diagnostics for invalid flexible array members or zero-length
6920 arrays that are not the last elements of the containing class or its
6921 base classes or that are its sole members. */
6923 static void
6924 diagnose_flexarrays (tree t, const flexmems_t *fmem)
6926 if (!fmem->array)
6927 return;
6929 if (fmem->first && !fmem->after[0])
6931 diagnose_invalid_flexarray (fmem);
6932 return;
6935 /* Has a diagnostic been issued? */
6936 bool diagd = false;
6938 const char *msg = 0;
6940 if (TYPE_DOMAIN (TREE_TYPE (fmem->array)))
6942 if (fmem->after[0])
6943 msg = G_("zero-size array member %qD not at end of %q#T");
6944 else if (!fmem->first)
6945 msg = G_("zero-size array member %qD in an otherwise empty %q#T");
6947 if (msg)
6949 location_t loc = DECL_SOURCE_LOCATION (fmem->array);
6951 if (pedwarn (loc, OPT_Wpedantic, msg, fmem->array, t))
6953 inform (location_of (t), "in the definition of %q#T", t);
6954 diagd = true;
6958 else
6960 if (fmem->after[0])
6961 msg = G_("flexible array member %qD not at end of %q#T");
6962 else if (!fmem->first)
6963 msg = G_("flexible array member %qD in an otherwise empty %q#T");
6965 if (msg)
6967 location_t loc = DECL_SOURCE_LOCATION (fmem->array);
6968 diagd = true;
6970 error_at (loc, msg, fmem->array, t);
6972 /* In the unlikely event that the member following the flexible
6973 array member is declared in a different class, or the member
6974 overlaps another member of a common union, point to it.
6975 Otherwise it should be obvious. */
6976 if (fmem->after[0]
6977 && ((DECL_CONTEXT (fmem->after[0])
6978 != DECL_CONTEXT (fmem->array))))
6980 inform (DECL_SOURCE_LOCATION (fmem->after[0]),
6981 "next member %q#D declared here",
6982 fmem->after[0]);
6983 inform (location_of (t), "in the definition of %q#T", t);
6988 if (!diagd && fmem->array && fmem->enclosing)
6989 diagnose_invalid_flexarray (fmem);
6993 /* Recursively check to make sure that any flexible array or zero-length
6994 array members of class T or its bases are valid (i.e., not the sole
6995 non-static data member of T and, if one exists, that it is the last
6996 non-static data member of T and its base classes. FMEM is expected
6997 to be initially null and is used internally by recursive calls to
6998 the function. Issue the appropriate diagnostics for the array member
6999 that fails the checks. */
7001 static void
7002 check_flexarrays (tree t, flexmems_t *fmem /* = NULL */,
7003 bool base_p /* = false */)
7005 /* Initialize the result of a search for flexible array and zero-length
7006 array members. Avoid doing any work if the most interesting FMEM data
7007 have already been populated. */
7008 flexmems_t flexmems = flexmems_t ();
7009 if (!fmem)
7010 fmem = &flexmems;
7011 else if (fmem->array && fmem->first && fmem->after[0])
7012 return;
7014 tree fam = fmem->array;
7016 /* Recursively check the primary base class first. */
7017 if (CLASSTYPE_HAS_PRIMARY_BASE_P (t))
7019 tree basetype = BINFO_TYPE (CLASSTYPE_PRIMARY_BINFO (t));
7020 check_flexarrays (basetype, fmem, true);
7023 /* Recursively check the base classes. */
7024 int nbases = TYPE_BINFO (t) ? BINFO_N_BASE_BINFOS (TYPE_BINFO (t)) : 0;
7025 for (int i = 0; i < nbases; ++i)
7027 tree base_binfo = BINFO_BASE_BINFO (TYPE_BINFO (t), i);
7029 /* The primary base class was already checked above. */
7030 if (base_binfo == CLASSTYPE_PRIMARY_BINFO (t))
7031 continue;
7033 /* Virtual base classes are at the end. */
7034 if (BINFO_VIRTUAL_P (base_binfo))
7035 continue;
7037 /* Check the base class. */
7038 check_flexarrays (BINFO_TYPE (base_binfo), fmem, /*base_p=*/true);
7041 if (fmem == &flexmems)
7043 /* Check virtual base classes only once per derived class.
7044 I.e., this check is not performed recursively for base
7045 classes. */
7046 int i;
7047 tree base_binfo;
7048 vec<tree, va_gc> *vbases;
7049 for (vbases = CLASSTYPE_VBASECLASSES (t), i = 0;
7050 vec_safe_iterate (vbases, i, &base_binfo); i++)
7052 /* Check the virtual base class. */
7053 tree basetype = TREE_TYPE (base_binfo);
7055 check_flexarrays (basetype, fmem, /*base_p=*/true);
7059 /* Is the type unnamed (and therefore a member of it potentially
7060 an anonymous struct or union)? */
7061 bool maybe_anon_p = TYPE_UNNAMED_P (t);
7063 /* Search the members of the current (possibly derived) class, skipping
7064 unnamed structs and unions since those could be anonymous. */
7065 if (fmem != &flexmems || !maybe_anon_p)
7066 find_flexarrays (t, fmem, base_p || fam != fmem->array);
7068 if (fmem == &flexmems && !maybe_anon_p)
7070 /* Issue diagnostics for invalid flexible and zero-length array
7071 members found in base classes or among the members of the current
7072 class. Ignore anonymous structs and unions whose members are
7073 considered to be members of the enclosing class and thus will
7074 be diagnosed when checking it. */
7075 diagnose_flexarrays (t, fmem);
7079 /* Perform processing required when the definition of T (a class type)
7080 is complete. Diagnose invalid definitions of flexible array members
7081 and zero-size arrays. */
7083 void
7084 finish_struct_1 (tree t)
7086 tree x;
7087 /* A TREE_LIST. The TREE_VALUE of each node is a FUNCTION_DECL. */
7088 tree virtuals = NULL_TREE;
7090 if (COMPLETE_TYPE_P (t))
7092 gcc_assert (MAYBE_CLASS_TYPE_P (t));
7093 error ("redefinition of %q#T", t);
7094 popclass ();
7095 return;
7098 /* If this type was previously laid out as a forward reference,
7099 make sure we lay it out again. */
7100 TYPE_SIZE (t) = NULL_TREE;
7101 CLASSTYPE_PRIMARY_BINFO (t) = NULL_TREE;
7103 /* Make assumptions about the class; we'll reset the flags if
7104 necessary. */
7105 CLASSTYPE_EMPTY_P (t) = 1;
7106 CLASSTYPE_NEARLY_EMPTY_P (t) = 1;
7107 CLASSTYPE_CONTAINS_EMPTY_CLASS_P (t) = 0;
7108 CLASSTYPE_LITERAL_P (t) = true;
7110 /* Do end-of-class semantic processing: checking the validity of the
7111 bases and members and add implicitly generated methods. */
7112 check_bases_and_members (t);
7114 /* Find the key method. */
7115 if (TYPE_CONTAINS_VPTR_P (t))
7117 /* The Itanium C++ ABI permits the key method to be chosen when
7118 the class is defined -- even though the key method so
7119 selected may later turn out to be an inline function. On
7120 some systems (such as ARM Symbian OS) the key method cannot
7121 be determined until the end of the translation unit. On such
7122 systems, we leave CLASSTYPE_KEY_METHOD set to NULL, which
7123 will cause the class to be added to KEYED_CLASSES. Then, in
7124 finish_file we will determine the key method. */
7125 if (targetm.cxx.key_method_may_be_inline ())
7126 determine_key_method (t);
7128 /* If a polymorphic class has no key method, we may emit the vtable
7129 in every translation unit where the class definition appears. If
7130 we're devirtualizing, we can look into the vtable even if we
7131 aren't emitting it. */
7132 if (!CLASSTYPE_KEY_METHOD (t))
7133 vec_safe_push (keyed_classes, t);
7136 /* Layout the class itself. */
7137 layout_class_type (t, &virtuals);
7138 if (CLASSTYPE_AS_BASE (t) != t)
7139 /* We use the base type for trivial assignments, and hence it
7140 needs a mode. */
7141 compute_record_mode (CLASSTYPE_AS_BASE (t));
7143 /* With the layout complete, check for flexible array members and
7144 zero-length arrays that might overlap other members in the final
7145 layout. */
7146 check_flexarrays (t);
7148 virtuals = modify_all_vtables (t, nreverse (virtuals));
7150 /* If necessary, create the primary vtable for this class. */
7151 if (virtuals || TYPE_CONTAINS_VPTR_P (t))
7153 /* We must enter these virtuals into the table. */
7154 if (!CLASSTYPE_HAS_PRIMARY_BASE_P (t))
7155 build_primary_vtable (NULL_TREE, t);
7156 else if (! BINFO_NEW_VTABLE_MARKED (TYPE_BINFO (t)))
7157 /* Here we know enough to change the type of our virtual
7158 function table, but we will wait until later this function. */
7159 build_primary_vtable (CLASSTYPE_PRIMARY_BINFO (t), t);
7161 /* If we're warning about ABI tags, check the types of the new
7162 virtual functions. */
7163 if (warn_abi_tag)
7164 for (tree v = virtuals; v; v = TREE_CHAIN (v))
7165 check_abi_tags (t, TREE_VALUE (v));
7168 if (TYPE_CONTAINS_VPTR_P (t))
7170 int vindex;
7171 tree fn;
7173 if (BINFO_VTABLE (TYPE_BINFO (t)))
7174 gcc_assert (DECL_VIRTUAL_P (BINFO_VTABLE (TYPE_BINFO (t))));
7175 if (!CLASSTYPE_HAS_PRIMARY_BASE_P (t))
7176 gcc_assert (BINFO_VIRTUALS (TYPE_BINFO (t)) == NULL_TREE);
7178 /* Add entries for virtual functions introduced by this class. */
7179 BINFO_VIRTUALS (TYPE_BINFO (t))
7180 = chainon (BINFO_VIRTUALS (TYPE_BINFO (t)), virtuals);
7182 /* Set DECL_VINDEX for all functions declared in this class. */
7183 for (vindex = 0, fn = BINFO_VIRTUALS (TYPE_BINFO (t));
7185 fn = TREE_CHAIN (fn),
7186 vindex += (TARGET_VTABLE_USES_DESCRIPTORS
7187 ? TARGET_VTABLE_USES_DESCRIPTORS : 1))
7189 tree fndecl = BV_FN (fn);
7191 if (DECL_THUNK_P (fndecl))
7192 /* A thunk. We should never be calling this entry directly
7193 from this vtable -- we'd use the entry for the non
7194 thunk base function. */
7195 DECL_VINDEX (fndecl) = NULL_TREE;
7196 else if (TREE_CODE (DECL_VINDEX (fndecl)) != INTEGER_CST)
7197 DECL_VINDEX (fndecl) = build_int_cst (NULL_TREE, vindex);
7201 finish_struct_bits (t);
7202 set_method_tm_attributes (t);
7203 if (flag_openmp || flag_openmp_simd)
7204 finish_omp_declare_simd_methods (t);
7206 /* Complete the rtl for any static member objects of the type we're
7207 working on. */
7208 for (x = TYPE_FIELDS (t); x; x = DECL_CHAIN (x))
7209 if (VAR_P (x) && TREE_STATIC (x)
7210 && TREE_TYPE (x) != error_mark_node
7211 && same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (x)), t))
7212 SET_DECL_MODE (x, TYPE_MODE (t));
7214 /* Done with FIELDS...now decide whether to sort these for
7215 faster lookups later.
7217 We use a small number because most searches fail (succeeding
7218 ultimately as the search bores through the inheritance
7219 hierarchy), and we want this failure to occur quickly. */
7221 insert_into_classtype_sorted_fields (TYPE_FIELDS (t), t, 8);
7223 /* Complain if one of the field types requires lower visibility. */
7224 constrain_class_visibility (t);
7226 /* Make the rtl for any new vtables we have created, and unmark
7227 the base types we marked. */
7228 finish_vtbls (t);
7230 /* Build the VTT for T. */
7231 build_vtt (t);
7233 if (warn_nonvdtor
7234 && TYPE_POLYMORPHIC_P (t) && accessible_nvdtor_p (t)
7235 && !CLASSTYPE_FINAL (t))
7236 warning (OPT_Wnon_virtual_dtor,
7237 "%q#T has virtual functions and accessible"
7238 " non-virtual destructor", t);
7240 complete_vars (t);
7242 if (warn_overloaded_virtual)
7243 warn_hidden (t);
7245 /* Class layout, assignment of virtual table slots, etc., is now
7246 complete. Give the back end a chance to tweak the visibility of
7247 the class or perform any other required target modifications. */
7248 targetm.cxx.adjust_class_at_definition (t);
7250 maybe_suppress_debug_info (t);
7252 if (flag_vtable_verify)
7253 vtv_save_class_info (t);
7255 dump_class_hierarchy (t);
7257 /* Finish debugging output for this type. */
7258 rest_of_type_compilation (t, ! LOCAL_CLASS_P (t));
7260 if (TYPE_TRANSPARENT_AGGR (t))
7262 tree field = first_field (t);
7263 if (field == NULL_TREE || error_operand_p (field))
7265 error ("type transparent %q#T does not have any fields", t);
7266 TYPE_TRANSPARENT_AGGR (t) = 0;
7268 else if (DECL_ARTIFICIAL (field))
7270 if (DECL_FIELD_IS_BASE (field))
7271 error ("type transparent class %qT has base classes", t);
7272 else
7274 gcc_checking_assert (DECL_VIRTUAL_P (field));
7275 error ("type transparent class %qT has virtual functions", t);
7277 TYPE_TRANSPARENT_AGGR (t) = 0;
7279 else if (TYPE_MODE (t) != DECL_MODE (field))
7281 error ("type transparent %q#T cannot be made transparent because "
7282 "the type of the first field has a different ABI from the "
7283 "class overall", t);
7284 TYPE_TRANSPARENT_AGGR (t) = 0;
7289 /* Insert FIELDS into T for the sorted case if the FIELDS count is
7290 equal to THRESHOLD or greater than THRESHOLD. */
7292 static void
7293 insert_into_classtype_sorted_fields (tree fields, tree t, int threshold)
7295 int n_fields = count_fields (fields);
7296 if (n_fields >= threshold)
7298 struct sorted_fields_type *field_vec = sorted_fields_type_new (n_fields);
7299 add_fields_to_record_type (fields, field_vec, 0);
7300 qsort (field_vec->elts, n_fields, sizeof (tree), field_decl_cmp);
7301 CLASSTYPE_SORTED_FIELDS (t) = field_vec;
7305 /* Insert lately defined enum ENUMTYPE into T for the sorted case. */
7307 void
7308 insert_late_enum_def_into_classtype_sorted_fields (tree enumtype, tree t)
7310 struct sorted_fields_type *sorted_fields = CLASSTYPE_SORTED_FIELDS (t);
7311 if (sorted_fields)
7313 int i;
7314 int n_fields
7315 = list_length (TYPE_VALUES (enumtype)) + sorted_fields->len;
7316 struct sorted_fields_type *field_vec = sorted_fields_type_new (n_fields);
7318 for (i = 0; i < sorted_fields->len; ++i)
7319 field_vec->elts[i] = sorted_fields->elts[i];
7321 add_enum_fields_to_record_type (enumtype, field_vec,
7322 sorted_fields->len);
7323 qsort (field_vec->elts, n_fields, sizeof (tree), field_decl_cmp);
7324 CLASSTYPE_SORTED_FIELDS (t) = field_vec;
7328 /* When T was built up, the member declarations were added in reverse
7329 order. Rearrange them to declaration order. */
7331 void
7332 unreverse_member_declarations (tree t)
7334 tree next;
7335 tree prev;
7336 tree x;
7338 /* The following lists are all in reverse order. Put them in
7339 declaration order now. */
7340 CLASSTYPE_DECL_LIST (t) = nreverse (CLASSTYPE_DECL_LIST (t));
7342 /* For the TYPE_FIELDS, only the non TYPE_DECLs are in reverse
7343 order, so we can't just use nreverse. Due to stat_hack
7344 chicanery in finish_member_declarations. */
7345 prev = NULL_TREE;
7346 for (x = TYPE_FIELDS (t);
7347 x && TREE_CODE (x) != TYPE_DECL;
7348 x = next)
7350 next = DECL_CHAIN (x);
7351 DECL_CHAIN (x) = prev;
7352 prev = x;
7355 if (prev)
7357 DECL_CHAIN (TYPE_FIELDS (t)) = x;
7358 if (prev)
7359 TYPE_FIELDS (t) = prev;
7363 tree
7364 finish_struct (tree t, tree attributes)
7366 location_t saved_loc = input_location;
7368 /* Now that we've got all the field declarations, reverse everything
7369 as necessary. */
7370 unreverse_member_declarations (t);
7372 cplus_decl_attributes (&t, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
7373 fixup_attribute_variants (t);
7375 /* Nadger the current location so that diagnostics point to the start of
7376 the struct, not the end. */
7377 input_location = DECL_SOURCE_LOCATION (TYPE_NAME (t));
7379 if (processing_template_decl)
7381 tree x;
7383 finish_struct_methods (t);
7384 TYPE_SIZE (t) = bitsize_zero_node;
7385 TYPE_SIZE_UNIT (t) = size_zero_node;
7387 /* We need to emit an error message if this type was used as a parameter
7388 and it is an abstract type, even if it is a template. We construct
7389 a simple CLASSTYPE_PURE_VIRTUALS list without taking bases into
7390 account and we call complete_vars with this type, which will check
7391 the PARM_DECLS. Note that while the type is being defined,
7392 CLASSTYPE_PURE_VIRTUALS contains the list of the inline friends
7393 (see CLASSTYPE_INLINE_FRIENDS) so we need to clear it. */
7394 CLASSTYPE_PURE_VIRTUALS (t) = NULL;
7395 for (x = TYPE_FIELDS (t); x; x = DECL_CHAIN (x))
7396 if (TREE_CODE (x) == FUNCTION_DECL && DECL_PURE_VIRTUAL_P (x))
7397 vec_safe_push (CLASSTYPE_PURE_VIRTUALS (t), x);
7398 complete_vars (t);
7399 /* We need to add the target functions to the CLASSTYPE_METHOD_VEC if
7400 an enclosing scope is a template class, so that this function be
7401 found by lookup_fnfields_1 when the using declaration is not
7402 instantiated yet. */
7403 for (x = TYPE_FIELDS (t); x; x = DECL_CHAIN (x))
7404 if (TREE_CODE (x) == USING_DECL)
7406 tree fn = strip_using_decl (x);
7407 if (OVL_P (fn))
7408 for (lkp_iterator iter (fn); iter; ++iter)
7409 add_method (t, *iter, true);
7412 /* Remember current #pragma pack value. */
7413 TYPE_PRECISION (t) = maximum_field_alignment;
7415 /* Fix up any variants we've already built. */
7416 for (x = TYPE_NEXT_VARIANT (t); x; x = TYPE_NEXT_VARIANT (x))
7418 TYPE_SIZE (x) = TYPE_SIZE (t);
7419 TYPE_SIZE_UNIT (x) = TYPE_SIZE_UNIT (t);
7420 TYPE_FIELDS (x) = TYPE_FIELDS (t);
7423 else
7424 finish_struct_1 (t);
7426 if (is_std_init_list (t))
7428 /* People keep complaining that the compiler crashes on an invalid
7429 definition of initializer_list, so I guess we should explicitly
7430 reject it. What the compiler internals care about is that it's a
7431 template and has a pointer field followed by an integer field. */
7432 bool ok = false;
7433 if (processing_template_decl)
7435 tree f = next_initializable_field (TYPE_FIELDS (t));
7436 if (f && TREE_CODE (TREE_TYPE (f)) == POINTER_TYPE)
7438 f = next_initializable_field (DECL_CHAIN (f));
7439 if (f && same_type_p (TREE_TYPE (f), size_type_node))
7440 ok = true;
7443 if (!ok)
7444 fatal_error (input_location,
7445 "definition of std::initializer_list does not match "
7446 "#include <initializer_list>");
7449 input_location = saved_loc;
7451 TYPE_BEING_DEFINED (t) = 0;
7453 if (current_class_type)
7454 popclass ();
7455 else
7456 error ("trying to finish struct, but kicked out due to previous parse errors");
7458 if (processing_template_decl && at_function_scope_p ()
7459 /* Lambdas are defined by the LAMBDA_EXPR. */
7460 && !LAMBDA_TYPE_P (t))
7461 add_stmt (build_min (TAG_DEFN, t));
7463 return t;
7466 /* Hash table to avoid endless recursion when handling references. */
7467 static hash_table<nofree_ptr_hash<tree_node> > *fixed_type_or_null_ref_ht;
7469 /* Return the dynamic type of INSTANCE, if known.
7470 Used to determine whether the virtual function table is needed
7471 or not.
7473 *NONNULL is set iff INSTANCE can be known to be nonnull, regardless
7474 of our knowledge of its type. *NONNULL should be initialized
7475 before this function is called. */
7477 static tree
7478 fixed_type_or_null (tree instance, int *nonnull, int *cdtorp)
7480 #define RECUR(T) fixed_type_or_null((T), nonnull, cdtorp)
7482 switch (TREE_CODE (instance))
7484 case INDIRECT_REF:
7485 if (POINTER_TYPE_P (TREE_TYPE (instance)))
7486 return NULL_TREE;
7487 else
7488 return RECUR (TREE_OPERAND (instance, 0));
7490 case CALL_EXPR:
7491 /* This is a call to a constructor, hence it's never zero. */
7492 if (TREE_HAS_CONSTRUCTOR (instance))
7494 if (nonnull)
7495 *nonnull = 1;
7496 return TREE_TYPE (instance);
7498 return NULL_TREE;
7500 case SAVE_EXPR:
7501 /* This is a call to a constructor, hence it's never zero. */
7502 if (TREE_HAS_CONSTRUCTOR (instance))
7504 if (nonnull)
7505 *nonnull = 1;
7506 return TREE_TYPE (instance);
7508 return RECUR (TREE_OPERAND (instance, 0));
7510 case POINTER_PLUS_EXPR:
7511 case PLUS_EXPR:
7512 case MINUS_EXPR:
7513 if (TREE_CODE (TREE_OPERAND (instance, 0)) == ADDR_EXPR)
7514 return RECUR (TREE_OPERAND (instance, 0));
7515 if (TREE_CODE (TREE_OPERAND (instance, 1)) == INTEGER_CST)
7516 /* Propagate nonnull. */
7517 return RECUR (TREE_OPERAND (instance, 0));
7519 return NULL_TREE;
7521 CASE_CONVERT:
7522 return RECUR (TREE_OPERAND (instance, 0));
7524 case ADDR_EXPR:
7525 instance = TREE_OPERAND (instance, 0);
7526 if (nonnull)
7528 /* Just because we see an ADDR_EXPR doesn't mean we're dealing
7529 with a real object -- given &p->f, p can still be null. */
7530 tree t = get_base_address (instance);
7531 /* ??? Probably should check DECL_WEAK here. */
7532 if (t && DECL_P (t))
7533 *nonnull = 1;
7535 return RECUR (instance);
7537 case COMPONENT_REF:
7538 /* If this component is really a base class reference, then the field
7539 itself isn't definitive. */
7540 if (DECL_FIELD_IS_BASE (TREE_OPERAND (instance, 1)))
7541 return RECUR (TREE_OPERAND (instance, 0));
7542 return RECUR (TREE_OPERAND (instance, 1));
7544 case VAR_DECL:
7545 case FIELD_DECL:
7546 if (TREE_CODE (TREE_TYPE (instance)) == ARRAY_TYPE
7547 && MAYBE_CLASS_TYPE_P (TREE_TYPE (TREE_TYPE (instance))))
7549 if (nonnull)
7550 *nonnull = 1;
7551 return TREE_TYPE (TREE_TYPE (instance));
7553 /* fall through. */
7554 case TARGET_EXPR:
7555 case PARM_DECL:
7556 case RESULT_DECL:
7557 if (MAYBE_CLASS_TYPE_P (TREE_TYPE (instance)))
7559 if (nonnull)
7560 *nonnull = 1;
7561 return TREE_TYPE (instance);
7563 else if (instance == current_class_ptr)
7565 if (nonnull)
7566 *nonnull = 1;
7568 /* if we're in a ctor or dtor, we know our type. If
7569 current_class_ptr is set but we aren't in a function, we're in
7570 an NSDMI (and therefore a constructor). */
7571 if (current_scope () != current_function_decl
7572 || (DECL_LANG_SPECIFIC (current_function_decl)
7573 && (DECL_CONSTRUCTOR_P (current_function_decl)
7574 || DECL_DESTRUCTOR_P (current_function_decl))))
7576 if (cdtorp)
7577 *cdtorp = 1;
7578 return TREE_TYPE (TREE_TYPE (instance));
7581 else if (TREE_CODE (TREE_TYPE (instance)) == REFERENCE_TYPE)
7583 /* We only need one hash table because it is always left empty. */
7584 if (!fixed_type_or_null_ref_ht)
7585 fixed_type_or_null_ref_ht
7586 = new hash_table<nofree_ptr_hash<tree_node> > (37);
7588 /* Reference variables should be references to objects. */
7589 if (nonnull)
7590 *nonnull = 1;
7592 /* Enter the INSTANCE in a table to prevent recursion; a
7593 variable's initializer may refer to the variable
7594 itself. */
7595 if (VAR_P (instance)
7596 && DECL_INITIAL (instance)
7597 && !type_dependent_expression_p_push (DECL_INITIAL (instance))
7598 && !fixed_type_or_null_ref_ht->find (instance))
7600 tree type;
7601 tree_node **slot;
7603 slot = fixed_type_or_null_ref_ht->find_slot (instance, INSERT);
7604 *slot = instance;
7605 type = RECUR (DECL_INITIAL (instance));
7606 fixed_type_or_null_ref_ht->remove_elt (instance);
7608 return type;
7611 return NULL_TREE;
7613 default:
7614 return NULL_TREE;
7616 #undef RECUR
7619 /* Return nonzero if the dynamic type of INSTANCE is known, and
7620 equivalent to the static type. We also handle the case where
7621 INSTANCE is really a pointer. Return negative if this is a
7622 ctor/dtor. There the dynamic type is known, but this might not be
7623 the most derived base of the original object, and hence virtual
7624 bases may not be laid out according to this type.
7626 Used to determine whether the virtual function table is needed
7627 or not.
7629 *NONNULL is set iff INSTANCE can be known to be nonnull, regardless
7630 of our knowledge of its type. *NONNULL should be initialized
7631 before this function is called. */
7634 resolves_to_fixed_type_p (tree instance, int* nonnull)
7636 tree t = TREE_TYPE (instance);
7637 int cdtorp = 0;
7638 tree fixed;
7640 /* processing_template_decl can be false in a template if we're in
7641 instantiate_non_dependent_expr, but we still want to suppress
7642 this check. */
7643 if (in_template_function ())
7645 /* In a template we only care about the type of the result. */
7646 if (nonnull)
7647 *nonnull = true;
7648 return true;
7651 fixed = fixed_type_or_null (instance, nonnull, &cdtorp);
7652 if (fixed == NULL_TREE)
7653 return 0;
7654 if (POINTER_TYPE_P (t))
7655 t = TREE_TYPE (t);
7656 if (!same_type_ignoring_top_level_qualifiers_p (t, fixed))
7657 return 0;
7658 return cdtorp ? -1 : 1;
7662 void
7663 init_class_processing (void)
7665 current_class_depth = 0;
7666 current_class_stack_size = 10;
7667 current_class_stack
7668 = XNEWVEC (struct class_stack_node, current_class_stack_size);
7669 vec_alloc (local_classes, 8);
7670 sizeof_biggest_empty_class = size_zero_node;
7672 ridpointers[(int) RID_PUBLIC] = access_public_node;
7673 ridpointers[(int) RID_PRIVATE] = access_private_node;
7674 ridpointers[(int) RID_PROTECTED] = access_protected_node;
7677 /* Restore the cached PREVIOUS_CLASS_LEVEL. */
7679 static void
7680 restore_class_cache (void)
7682 tree type;
7684 /* We are re-entering the same class we just left, so we don't
7685 have to search the whole inheritance matrix to find all the
7686 decls to bind again. Instead, we install the cached
7687 class_shadowed list and walk through it binding names. */
7688 push_binding_level (previous_class_level);
7689 class_binding_level = previous_class_level;
7690 /* Restore IDENTIFIER_TYPE_VALUE. */
7691 for (type = class_binding_level->type_shadowed;
7692 type;
7693 type = TREE_CHAIN (type))
7694 SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (type), TREE_TYPE (type));
7697 /* Set global variables CURRENT_CLASS_NAME and CURRENT_CLASS_TYPE as
7698 appropriate for TYPE.
7700 So that we may avoid calls to lookup_name, we cache the _TYPE
7701 nodes of local TYPE_DECLs in the TREE_TYPE field of the name.
7703 For multiple inheritance, we perform a two-pass depth-first search
7704 of the type lattice. */
7706 void
7707 pushclass (tree type)
7709 class_stack_node_t csn;
7711 type = TYPE_MAIN_VARIANT (type);
7713 /* Make sure there is enough room for the new entry on the stack. */
7714 if (current_class_depth + 1 >= current_class_stack_size)
7716 current_class_stack_size *= 2;
7717 current_class_stack
7718 = XRESIZEVEC (struct class_stack_node, current_class_stack,
7719 current_class_stack_size);
7722 /* Insert a new entry on the class stack. */
7723 csn = current_class_stack + current_class_depth;
7724 csn->name = current_class_name;
7725 csn->type = current_class_type;
7726 csn->access = current_access_specifier;
7727 csn->names_used = 0;
7728 csn->hidden = 0;
7729 current_class_depth++;
7731 /* Now set up the new type. */
7732 current_class_name = TYPE_NAME (type);
7733 if (TREE_CODE (current_class_name) == TYPE_DECL)
7734 current_class_name = DECL_NAME (current_class_name);
7735 current_class_type = type;
7737 /* By default, things in classes are private, while things in
7738 structures or unions are public. */
7739 current_access_specifier = (CLASSTYPE_DECLARED_CLASS (type)
7740 ? access_private_node
7741 : access_public_node);
7743 if (previous_class_level
7744 && type != previous_class_level->this_entity
7745 && current_class_depth == 1)
7747 /* Forcibly remove any old class remnants. */
7748 invalidate_class_lookup_cache ();
7751 if (!previous_class_level
7752 || type != previous_class_level->this_entity
7753 || current_class_depth > 1)
7754 pushlevel_class ();
7755 else
7756 restore_class_cache ();
7759 /* When we exit a toplevel class scope, we save its binding level so
7760 that we can restore it quickly. Here, we've entered some other
7761 class, so we must invalidate our cache. */
7763 void
7764 invalidate_class_lookup_cache (void)
7766 previous_class_level = NULL;
7769 /* Get out of the current class scope. If we were in a class scope
7770 previously, that is the one popped to. */
7772 void
7773 popclass (void)
7775 poplevel_class ();
7777 current_class_depth--;
7778 current_class_name = current_class_stack[current_class_depth].name;
7779 current_class_type = current_class_stack[current_class_depth].type;
7780 current_access_specifier = current_class_stack[current_class_depth].access;
7781 if (current_class_stack[current_class_depth].names_used)
7782 splay_tree_delete (current_class_stack[current_class_depth].names_used);
7785 /* Mark the top of the class stack as hidden. */
7787 void
7788 push_class_stack (void)
7790 if (current_class_depth)
7791 ++current_class_stack[current_class_depth - 1].hidden;
7794 /* Mark the top of the class stack as un-hidden. */
7796 void
7797 pop_class_stack (void)
7799 if (current_class_depth)
7800 --current_class_stack[current_class_depth - 1].hidden;
7803 /* Returns 1 if the class type currently being defined is either T or
7804 a nested type of T. Returns the type from the current_class_stack,
7805 which might be equivalent to but not equal to T in case of
7806 constrained partial specializations. */
7808 tree
7809 currently_open_class (tree t)
7811 int i;
7813 if (!CLASS_TYPE_P (t))
7814 return NULL_TREE;
7816 t = TYPE_MAIN_VARIANT (t);
7818 /* We start looking from 1 because entry 0 is from global scope,
7819 and has no type. */
7820 for (i = current_class_depth; i > 0; --i)
7822 tree c;
7823 if (i == current_class_depth)
7824 c = current_class_type;
7825 else
7827 if (current_class_stack[i].hidden)
7828 break;
7829 c = current_class_stack[i].type;
7831 if (!c)
7832 continue;
7833 if (same_type_p (c, t))
7834 return c;
7836 return NULL_TREE;
7839 /* If either current_class_type or one of its enclosing classes are derived
7840 from T, return the appropriate type. Used to determine how we found
7841 something via unqualified lookup. */
7843 tree
7844 currently_open_derived_class (tree t)
7846 int i;
7848 /* The bases of a dependent type are unknown. */
7849 if (dependent_type_p (t))
7850 return NULL_TREE;
7852 if (!current_class_type)
7853 return NULL_TREE;
7855 if (DERIVED_FROM_P (t, current_class_type))
7856 return current_class_type;
7858 for (i = current_class_depth - 1; i > 0; --i)
7860 if (current_class_stack[i].hidden)
7861 break;
7862 if (DERIVED_FROM_P (t, current_class_stack[i].type))
7863 return current_class_stack[i].type;
7866 return NULL_TREE;
7869 /* Return the outermost enclosing class type that is still open, or
7870 NULL_TREE. */
7872 tree
7873 outermost_open_class (void)
7875 if (!current_class_type)
7876 return NULL_TREE;
7877 tree r = NULL_TREE;
7878 if (TYPE_BEING_DEFINED (current_class_type))
7879 r = current_class_type;
7880 for (int i = current_class_depth - 1; i > 0; --i)
7882 if (current_class_stack[i].hidden)
7883 break;
7884 tree t = current_class_stack[i].type;
7885 if (!TYPE_BEING_DEFINED (t))
7886 break;
7887 r = t;
7889 return r;
7892 /* Returns the innermost class type which is not a lambda closure type. */
7894 tree
7895 current_nonlambda_class_type (void)
7897 int i;
7899 /* We start looking from 1 because entry 0 is from global scope,
7900 and has no type. */
7901 for (i = current_class_depth; i > 0; --i)
7903 tree c;
7904 if (i == current_class_depth)
7905 c = current_class_type;
7906 else
7908 if (current_class_stack[i].hidden)
7909 break;
7910 c = current_class_stack[i].type;
7912 if (!c)
7913 continue;
7914 if (!LAMBDA_TYPE_P (c))
7915 return c;
7917 return NULL_TREE;
7920 /* When entering a class scope, all enclosing class scopes' names with
7921 static meaning (static variables, static functions, types and
7922 enumerators) have to be visible. This recursive function calls
7923 pushclass for all enclosing class contexts until global or a local
7924 scope is reached. TYPE is the enclosed class. */
7926 void
7927 push_nested_class (tree type)
7929 /* A namespace might be passed in error cases, like A::B:C. */
7930 if (type == NULL_TREE
7931 || !CLASS_TYPE_P (type))
7932 return;
7934 push_nested_class (DECL_CONTEXT (TYPE_MAIN_DECL (type)));
7936 pushclass (type);
7939 /* Undoes a push_nested_class call. */
7941 void
7942 pop_nested_class (void)
7944 tree context = DECL_CONTEXT (TYPE_MAIN_DECL (current_class_type));
7946 popclass ();
7947 if (context && CLASS_TYPE_P (context))
7948 pop_nested_class ();
7951 /* Returns the number of extern "LANG" blocks we are nested within. */
7954 current_lang_depth (void)
7956 return vec_safe_length (current_lang_base);
7959 /* Set global variables CURRENT_LANG_NAME to appropriate value
7960 so that behavior of name-mangling machinery is correct. */
7962 void
7963 push_lang_context (tree name)
7965 vec_safe_push (current_lang_base, current_lang_name);
7967 if (name == lang_name_cplusplus)
7968 current_lang_name = name;
7969 else if (name == lang_name_c)
7970 current_lang_name = name;
7971 else
7972 error ("language string %<\"%E\"%> not recognized", name);
7975 /* Get out of the current language scope. */
7977 void
7978 pop_lang_context (void)
7980 current_lang_name = current_lang_base->pop ();
7983 /* Type instantiation routines. */
7985 /* Given an OVERLOAD and a TARGET_TYPE, return the function that
7986 matches the TARGET_TYPE. If there is no satisfactory match, return
7987 error_mark_node, and issue an error & warning messages under
7988 control of FLAGS. Permit pointers to member function if FLAGS
7989 permits. If TEMPLATE_ONLY, the name of the overloaded function was
7990 a template-id, and EXPLICIT_TARGS are the explicitly provided
7991 template arguments.
7993 If OVERLOAD is for one or more member functions, then ACCESS_PATH
7994 is the base path used to reference those member functions. If
7995 the address is resolved to a member function, access checks will be
7996 performed and errors issued if appropriate. */
7998 static tree
7999 resolve_address_of_overloaded_function (tree target_type,
8000 tree overload,
8001 tsubst_flags_t complain,
8002 bool template_only,
8003 tree explicit_targs,
8004 tree access_path)
8006 /* Here's what the standard says:
8008 [over.over]
8010 If the name is a function template, template argument deduction
8011 is done, and if the argument deduction succeeds, the deduced
8012 arguments are used to generate a single template function, which
8013 is added to the set of overloaded functions considered.
8015 Non-member functions and static member functions match targets of
8016 type "pointer-to-function" or "reference-to-function." Nonstatic
8017 member functions match targets of type "pointer-to-member
8018 function;" the function type of the pointer to member is used to
8019 select the member function from the set of overloaded member
8020 functions. If a nonstatic member function is selected, the
8021 reference to the overloaded function name is required to have the
8022 form of a pointer to member as described in 5.3.1.
8024 If more than one function is selected, any template functions in
8025 the set are eliminated if the set also contains a non-template
8026 function, and any given template function is eliminated if the
8027 set contains a second template function that is more specialized
8028 than the first according to the partial ordering rules 14.5.5.2.
8029 After such eliminations, if any, there shall remain exactly one
8030 selected function. */
8032 int is_ptrmem = 0;
8033 /* We store the matches in a TREE_LIST rooted here. The functions
8034 are the TREE_PURPOSE, not the TREE_VALUE, in this list, for easy
8035 interoperability with most_specialized_instantiation. */
8036 tree matches = NULL_TREE;
8037 tree fn;
8038 tree target_fn_type;
8040 /* By the time we get here, we should be seeing only real
8041 pointer-to-member types, not the internal POINTER_TYPE to
8042 METHOD_TYPE representation. */
8043 gcc_assert (!TYPE_PTR_P (target_type)
8044 || TREE_CODE (TREE_TYPE (target_type)) != METHOD_TYPE);
8046 gcc_assert (is_overloaded_fn (overload));
8048 /* Check that the TARGET_TYPE is reasonable. */
8049 if (TYPE_PTRFN_P (target_type)
8050 || TYPE_REFFN_P (target_type))
8051 /* This is OK. */;
8052 else if (TYPE_PTRMEMFUNC_P (target_type))
8053 /* This is OK, too. */
8054 is_ptrmem = 1;
8055 else if (TREE_CODE (target_type) == FUNCTION_TYPE)
8056 /* This is OK, too. This comes from a conversion to reference
8057 type. */
8058 target_type = build_reference_type (target_type);
8059 else
8061 if (complain & tf_error)
8062 error ("cannot resolve overloaded function %qD based on"
8063 " conversion to type %qT",
8064 OVL_NAME (overload), target_type);
8065 return error_mark_node;
8068 /* Non-member functions and static member functions match targets of type
8069 "pointer-to-function" or "reference-to-function." Nonstatic member
8070 functions match targets of type "pointer-to-member-function;" the
8071 function type of the pointer to member is used to select the member
8072 function from the set of overloaded member functions.
8074 So figure out the FUNCTION_TYPE that we want to match against. */
8075 target_fn_type = static_fn_type (target_type);
8077 /* If we can find a non-template function that matches, we can just
8078 use it. There's no point in generating template instantiations
8079 if we're just going to throw them out anyhow. But, of course, we
8080 can only do this when we don't *need* a template function. */
8081 if (!template_only)
8082 for (lkp_iterator iter (overload); iter; ++iter)
8084 tree fn = *iter;
8086 if (TREE_CODE (fn) == TEMPLATE_DECL)
8087 /* We're not looking for templates just yet. */
8088 continue;
8090 if ((TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE) != is_ptrmem)
8091 /* We're looking for a non-static member, and this isn't
8092 one, or vice versa. */
8093 continue;
8095 /* In C++17 we need the noexcept-qualifier to compare types. */
8096 if (flag_noexcept_type)
8097 maybe_instantiate_noexcept (fn);
8099 /* See if there's a match. */
8100 tree fntype = static_fn_type (fn);
8101 if (same_type_p (target_fn_type, fntype)
8102 || fnptr_conv_p (target_fn_type, fntype))
8103 matches = tree_cons (fn, NULL_TREE, matches);
8106 /* Now, if we've already got a match (or matches), there's no need
8107 to proceed to the template functions. But, if we don't have a
8108 match we need to look at them, too. */
8109 if (!matches)
8111 tree target_arg_types;
8112 tree target_ret_type;
8113 tree *args;
8114 unsigned int nargs, ia;
8115 tree arg;
8117 target_arg_types = TYPE_ARG_TYPES (target_fn_type);
8118 target_ret_type = TREE_TYPE (target_fn_type);
8120 nargs = list_length (target_arg_types);
8121 args = XALLOCAVEC (tree, nargs);
8122 for (arg = target_arg_types, ia = 0;
8123 arg != NULL_TREE && arg != void_list_node;
8124 arg = TREE_CHAIN (arg), ++ia)
8125 args[ia] = TREE_VALUE (arg);
8126 nargs = ia;
8128 for (lkp_iterator iter (overload); iter; ++iter)
8130 tree fn = *iter;
8131 tree instantiation;
8132 tree targs;
8134 if (TREE_CODE (fn) != TEMPLATE_DECL)
8135 /* We're only looking for templates. */
8136 continue;
8138 if ((TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
8139 != is_ptrmem)
8140 /* We're not looking for a non-static member, and this is
8141 one, or vice versa. */
8142 continue;
8144 tree ret = target_ret_type;
8146 /* If the template has a deduced return type, don't expose it to
8147 template argument deduction. */
8148 if (undeduced_auto_decl (fn))
8149 ret = NULL_TREE;
8151 /* Try to do argument deduction. */
8152 targs = make_tree_vec (DECL_NTPARMS (fn));
8153 instantiation = fn_type_unification (fn, explicit_targs, targs, args,
8154 nargs, ret,
8155 DEDUCE_EXACT, LOOKUP_NORMAL,
8156 false, false);
8157 if (instantiation == error_mark_node)
8158 /* Instantiation failed. */
8159 continue;
8161 /* Constraints must be satisfied. This is done before
8162 return type deduction since that instantiates the
8163 function. */
8164 if (flag_concepts && !constraints_satisfied_p (instantiation))
8165 continue;
8167 /* And now force instantiation to do return type deduction. */
8168 if (undeduced_auto_decl (instantiation))
8170 ++function_depth;
8171 instantiate_decl (instantiation, /*defer*/false, /*class*/false);
8172 --function_depth;
8174 require_deduced_type (instantiation);
8177 /* In C++17 we need the noexcept-qualifier to compare types. */
8178 if (flag_noexcept_type)
8179 maybe_instantiate_noexcept (instantiation);
8181 /* See if there's a match. */
8182 tree fntype = static_fn_type (instantiation);
8183 if (same_type_p (target_fn_type, fntype)
8184 || fnptr_conv_p (target_fn_type, fntype))
8185 matches = tree_cons (instantiation, fn, matches);
8188 /* Now, remove all but the most specialized of the matches. */
8189 if (matches)
8191 tree match = most_specialized_instantiation (matches);
8193 if (match != error_mark_node)
8194 matches = tree_cons (TREE_PURPOSE (match),
8195 NULL_TREE,
8196 NULL_TREE);
8200 /* Now we should have exactly one function in MATCHES. */
8201 if (matches == NULL_TREE)
8203 /* There were *no* matches. */
8204 if (complain & tf_error)
8206 error ("no matches converting function %qD to type %q#T",
8207 OVL_NAME (overload), target_type);
8209 print_candidates (overload);
8211 return error_mark_node;
8213 else if (TREE_CHAIN (matches))
8215 /* There were too many matches. First check if they're all
8216 the same function. */
8217 tree match = NULL_TREE;
8219 fn = TREE_PURPOSE (matches);
8221 /* For multi-versioned functions, more than one match is just fine and
8222 decls_match will return false as they are different. */
8223 for (match = TREE_CHAIN (matches); match; match = TREE_CHAIN (match))
8224 if (!decls_match (fn, TREE_PURPOSE (match))
8225 && !targetm.target_option.function_versions
8226 (fn, TREE_PURPOSE (match)))
8227 break;
8229 if (match)
8231 if (complain & tf_error)
8233 error ("converting overloaded function %qD to type %q#T is ambiguous",
8234 OVL_NAME (overload), target_type);
8236 /* Since print_candidates expects the functions in the
8237 TREE_VALUE slot, we flip them here. */
8238 for (match = matches; match; match = TREE_CHAIN (match))
8239 TREE_VALUE (match) = TREE_PURPOSE (match);
8241 print_candidates (matches);
8244 return error_mark_node;
8248 /* Good, exactly one match. Now, convert it to the correct type. */
8249 fn = TREE_PURPOSE (matches);
8251 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
8252 && !(complain & tf_ptrmem_ok) && !flag_ms_extensions)
8254 static int explained;
8256 if (!(complain & tf_error))
8257 return error_mark_node;
8259 permerror (input_location, "assuming pointer to member %qD", fn);
8260 if (!explained)
8262 inform (input_location, "(a pointer to member can only be formed with %<&%E%>)", fn);
8263 explained = 1;
8267 /* If a pointer to a function that is multi-versioned is requested, the
8268 pointer to the dispatcher function is returned instead. This works
8269 well because indirectly calling the function will dispatch the right
8270 function version at run-time. */
8271 if (DECL_FUNCTION_VERSIONED (fn))
8273 fn = get_function_version_dispatcher (fn);
8274 if (fn == NULL)
8275 return error_mark_node;
8276 /* Mark all the versions corresponding to the dispatcher as used. */
8277 if (!(complain & tf_conv))
8278 mark_versions_used (fn);
8281 /* If we're doing overload resolution purely for the purpose of
8282 determining conversion sequences, we should not consider the
8283 function used. If this conversion sequence is selected, the
8284 function will be marked as used at this point. */
8285 if (!(complain & tf_conv))
8287 /* Make =delete work with SFINAE. */
8288 if (DECL_DELETED_FN (fn) && !(complain & tf_error))
8289 return error_mark_node;
8290 if (!mark_used (fn, complain) && !(complain & tf_error))
8291 return error_mark_node;
8294 /* We could not check access to member functions when this
8295 expression was originally created since we did not know at that
8296 time to which function the expression referred. */
8297 if (DECL_FUNCTION_MEMBER_P (fn))
8299 gcc_assert (access_path);
8300 perform_or_defer_access_check (access_path, fn, fn, complain);
8303 if (TYPE_PTRFN_P (target_type) || TYPE_PTRMEMFUNC_P (target_type))
8304 return cp_build_addr_expr (fn, complain);
8305 else
8307 /* The target must be a REFERENCE_TYPE. Above, cp_build_unary_op
8308 will mark the function as addressed, but here we must do it
8309 explicitly. */
8310 cxx_mark_addressable (fn);
8312 return fn;
8316 /* This function will instantiate the type of the expression given in
8317 RHS to match the type of LHSTYPE. If errors exist, then return
8318 error_mark_node. COMPLAIN is a bit mask. If TF_ERROR is set, then
8319 we complain on errors. If we are not complaining, never modify rhs,
8320 as overload resolution wants to try many possible instantiations, in
8321 the hope that at least one will work.
8323 For non-recursive calls, LHSTYPE should be a function, pointer to
8324 function, or a pointer to member function. */
8326 tree
8327 instantiate_type (tree lhstype, tree rhs, tsubst_flags_t complain)
8329 tsubst_flags_t complain_in = complain;
8330 tree access_path = NULL_TREE;
8332 complain &= ~tf_ptrmem_ok;
8334 if (lhstype == unknown_type_node)
8336 if (complain & tf_error)
8337 error ("not enough type information");
8338 return error_mark_node;
8341 if (TREE_TYPE (rhs) != NULL_TREE && ! (type_unknown_p (rhs)))
8343 tree fntype = non_reference (lhstype);
8344 if (same_type_p (fntype, TREE_TYPE (rhs)))
8345 return rhs;
8346 if (fnptr_conv_p (fntype, TREE_TYPE (rhs)))
8347 return rhs;
8348 if (flag_ms_extensions
8349 && TYPE_PTRMEMFUNC_P (fntype)
8350 && !TYPE_PTRMEMFUNC_P (TREE_TYPE (rhs)))
8351 /* Microsoft allows `A::f' to be resolved to a
8352 pointer-to-member. */
8354 else
8356 if (complain & tf_error)
8357 error ("cannot convert %qE from type %qT to type %qT",
8358 rhs, TREE_TYPE (rhs), fntype);
8359 return error_mark_node;
8363 if (BASELINK_P (rhs))
8365 access_path = BASELINK_ACCESS_BINFO (rhs);
8366 rhs = BASELINK_FUNCTIONS (rhs);
8369 /* If we are in a template, and have a NON_DEPENDENT_EXPR, we cannot
8370 deduce any type information. */
8371 if (TREE_CODE (rhs) == NON_DEPENDENT_EXPR)
8373 if (complain & tf_error)
8374 error ("not enough type information");
8375 return error_mark_node;
8378 /* If we instantiate a template, and it is a A ?: C expression
8379 with omitted B, look through the SAVE_EXPR. */
8380 if (TREE_CODE (rhs) == SAVE_EXPR)
8381 rhs = TREE_OPERAND (rhs, 0);
8383 /* There are only a few kinds of expressions that may have a type
8384 dependent on overload resolution. */
8385 gcc_assert (TREE_CODE (rhs) == ADDR_EXPR
8386 || TREE_CODE (rhs) == COMPONENT_REF
8387 || is_overloaded_fn (rhs)
8388 || (flag_ms_extensions && TREE_CODE (rhs) == FUNCTION_DECL));
8390 /* This should really only be used when attempting to distinguish
8391 what sort of a pointer to function we have. For now, any
8392 arithmetic operation which is not supported on pointers
8393 is rejected as an error. */
8395 switch (TREE_CODE (rhs))
8397 case COMPONENT_REF:
8399 tree member = TREE_OPERAND (rhs, 1);
8401 member = instantiate_type (lhstype, member, complain);
8402 if (member != error_mark_node
8403 && TREE_SIDE_EFFECTS (TREE_OPERAND (rhs, 0)))
8404 /* Do not lose object's side effects. */
8405 return build2 (COMPOUND_EXPR, TREE_TYPE (member),
8406 TREE_OPERAND (rhs, 0), member);
8407 return member;
8410 case OFFSET_REF:
8411 rhs = TREE_OPERAND (rhs, 1);
8412 if (BASELINK_P (rhs))
8413 return instantiate_type (lhstype, rhs, complain_in);
8415 /* This can happen if we are forming a pointer-to-member for a
8416 member template. */
8417 gcc_assert (TREE_CODE (rhs) == TEMPLATE_ID_EXPR);
8419 /* Fall through. */
8421 case TEMPLATE_ID_EXPR:
8423 tree fns = TREE_OPERAND (rhs, 0);
8424 tree args = TREE_OPERAND (rhs, 1);
8426 return
8427 resolve_address_of_overloaded_function (lhstype, fns, complain_in,
8428 /*template_only=*/true,
8429 args, access_path);
8432 case OVERLOAD:
8433 case FUNCTION_DECL:
8434 return
8435 resolve_address_of_overloaded_function (lhstype, rhs, complain_in,
8436 /*template_only=*/false,
8437 /*explicit_targs=*/NULL_TREE,
8438 access_path);
8440 case ADDR_EXPR:
8442 if (PTRMEM_OK_P (rhs))
8443 complain |= tf_ptrmem_ok;
8445 return instantiate_type (lhstype, TREE_OPERAND (rhs, 0), complain);
8448 case ERROR_MARK:
8449 return error_mark_node;
8451 default:
8452 gcc_unreachable ();
8454 return error_mark_node;
8457 /* Return the name of the virtual function pointer field
8458 (as an IDENTIFIER_NODE) for the given TYPE. Note that
8459 this may have to look back through base types to find the
8460 ultimate field name. (For single inheritance, these could
8461 all be the same name. Who knows for multiple inheritance). */
8463 static tree
8464 get_vfield_name (tree type)
8466 tree binfo, base_binfo;
8468 for (binfo = TYPE_BINFO (type);
8469 BINFO_N_BASE_BINFOS (binfo);
8470 binfo = base_binfo)
8472 base_binfo = BINFO_BASE_BINFO (binfo, 0);
8474 if (BINFO_VIRTUAL_P (base_binfo)
8475 || !TYPE_CONTAINS_VPTR_P (BINFO_TYPE (base_binfo)))
8476 break;
8479 type = BINFO_TYPE (binfo);
8480 tree ctor_name = constructor_name (type);
8481 char *buf = (char *) alloca (sizeof (VFIELD_NAME_FORMAT)
8482 + IDENTIFIER_LENGTH (ctor_name) + 2);
8483 sprintf (buf, VFIELD_NAME_FORMAT, IDENTIFIER_POINTER (ctor_name));
8484 return get_identifier (buf);
8487 void
8488 print_class_statistics (void)
8490 if (! GATHER_STATISTICS)
8491 return;
8493 fprintf (stderr, "convert_harshness = %d\n", n_convert_harshness);
8494 fprintf (stderr, "compute_conversion_costs = %d\n", n_compute_conversion_costs);
8495 if (n_vtables)
8497 fprintf (stderr, "vtables = %d; vtable searches = %d\n",
8498 n_vtables, n_vtable_searches);
8499 fprintf (stderr, "vtable entries = %d; vtable elems = %d\n",
8500 n_vtable_entries, n_vtable_elems);
8504 /* Build a dummy reference to ourselves so Derived::Base (and A::A) works,
8505 according to [class]:
8506 The class-name is also inserted
8507 into the scope of the class itself. For purposes of access checking,
8508 the inserted class name is treated as if it were a public member name. */
8510 void
8511 build_self_reference (void)
8513 tree name = DECL_NAME (TYPE_NAME (current_class_type));
8514 tree value = build_lang_decl (TYPE_DECL, name, current_class_type);
8516 DECL_NONLOCAL (value) = 1;
8517 DECL_CONTEXT (value) = current_class_type;
8518 DECL_ARTIFICIAL (value) = 1;
8519 SET_DECL_SELF_REFERENCE_P (value);
8520 set_underlying_type (value);
8522 if (processing_template_decl)
8523 value = push_template_decl (value);
8525 tree saved_cas = current_access_specifier;
8526 current_access_specifier = access_public_node;
8527 finish_member_declaration (value);
8528 current_access_specifier = saved_cas;
8531 /* Returns 1 if TYPE contains only padding bytes. */
8534 is_empty_class (tree type)
8536 if (type == error_mark_node)
8537 return 0;
8539 if (! CLASS_TYPE_P (type))
8540 return 0;
8542 return CLASSTYPE_EMPTY_P (type);
8545 /* Returns true if TYPE contains no actual data, just various
8546 possible combinations of empty classes and possibly a vptr. */
8548 bool
8549 is_really_empty_class (tree type)
8551 if (CLASS_TYPE_P (type))
8553 tree field;
8554 tree binfo;
8555 tree base_binfo;
8556 int i;
8558 /* CLASSTYPE_EMPTY_P isn't set properly until the class is actually laid
8559 out, but we'd like to be able to check this before then. */
8560 if (COMPLETE_TYPE_P (type) && is_empty_class (type))
8561 return true;
8563 for (binfo = TYPE_BINFO (type), i = 0;
8564 BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
8565 if (!is_really_empty_class (BINFO_TYPE (base_binfo)))
8566 return false;
8567 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
8568 if (TREE_CODE (field) == FIELD_DECL
8569 && !DECL_ARTIFICIAL (field)
8570 /* An unnamed bit-field is not a data member. */
8571 && (DECL_NAME (field) || !DECL_C_BIT_FIELD (field))
8572 && !is_really_empty_class (TREE_TYPE (field)))
8573 return false;
8574 return true;
8576 else if (TREE_CODE (type) == ARRAY_TYPE)
8577 return (integer_zerop (array_type_nelts_top (type))
8578 || is_really_empty_class (TREE_TYPE (type)));
8579 return false;
8582 /* Note that NAME was looked up while the current class was being
8583 defined and that the result of that lookup was DECL. */
8585 void
8586 maybe_note_name_used_in_class (tree name, tree decl)
8588 splay_tree names_used;
8590 /* If we're not defining a class, there's nothing to do. */
8591 if (!(innermost_scope_kind() == sk_class
8592 && TYPE_BEING_DEFINED (current_class_type)
8593 && !LAMBDA_TYPE_P (current_class_type)))
8594 return;
8596 /* If there's already a binding for this NAME, then we don't have
8597 anything to worry about. */
8598 if (lookup_member (current_class_type, name,
8599 /*protect=*/0, /*want_type=*/false, tf_warning_or_error))
8600 return;
8602 if (!current_class_stack[current_class_depth - 1].names_used)
8603 current_class_stack[current_class_depth - 1].names_used
8604 = splay_tree_new (splay_tree_compare_pointers, 0, 0);
8605 names_used = current_class_stack[current_class_depth - 1].names_used;
8607 splay_tree_insert (names_used,
8608 (splay_tree_key) name,
8609 (splay_tree_value) decl);
8612 /* Note that NAME was declared (as DECL) in the current class. Check
8613 to see that the declaration is valid. */
8615 void
8616 note_name_declared_in_class (tree name, tree decl)
8618 splay_tree names_used;
8619 splay_tree_node n;
8621 /* Look to see if we ever used this name. */
8622 names_used
8623 = current_class_stack[current_class_depth - 1].names_used;
8624 if (!names_used)
8625 return;
8626 /* The C language allows members to be declared with a type of the same
8627 name, and the C++ standard says this diagnostic is not required. So
8628 allow it in extern "C" blocks unless predantic is specified.
8629 Allow it in all cases if -ms-extensions is specified. */
8630 if ((!pedantic && current_lang_name == lang_name_c)
8631 || flag_ms_extensions)
8632 return;
8633 n = splay_tree_lookup (names_used, (splay_tree_key) name);
8634 if (n)
8636 /* [basic.scope.class]
8638 A name N used in a class S shall refer to the same declaration
8639 in its context and when re-evaluated in the completed scope of
8640 S. */
8641 permerror (input_location, "declaration of %q#D", decl);
8642 permerror (location_of ((tree) n->value),
8643 "changes meaning of %qD from %q#D",
8644 OVL_NAME (decl), (tree) n->value);
8648 /* Returns the VAR_DECL for the complete vtable associated with BINFO.
8649 Secondary vtables are merged with primary vtables; this function
8650 will return the VAR_DECL for the primary vtable. */
8652 tree
8653 get_vtbl_decl_for_binfo (tree binfo)
8655 tree decl;
8657 decl = BINFO_VTABLE (binfo);
8658 if (decl && TREE_CODE (decl) == POINTER_PLUS_EXPR)
8660 gcc_assert (TREE_CODE (TREE_OPERAND (decl, 0)) == ADDR_EXPR);
8661 decl = TREE_OPERAND (TREE_OPERAND (decl, 0), 0);
8663 if (decl)
8664 gcc_assert (VAR_P (decl));
8665 return decl;
8669 /* Returns the binfo for the primary base of BINFO. If the resulting
8670 BINFO is a virtual base, and it is inherited elsewhere in the
8671 hierarchy, then the returned binfo might not be the primary base of
8672 BINFO in the complete object. Check BINFO_PRIMARY_P or
8673 BINFO_LOST_PRIMARY_P to be sure. */
8675 static tree
8676 get_primary_binfo (tree binfo)
8678 tree primary_base;
8680 primary_base = CLASSTYPE_PRIMARY_BINFO (BINFO_TYPE (binfo));
8681 if (!primary_base)
8682 return NULL_TREE;
8684 return copied_binfo (primary_base, binfo);
8687 /* As above, but iterate until we reach the binfo that actually provides the
8688 vptr for BINFO. */
8690 static tree
8691 most_primary_binfo (tree binfo)
8693 tree b = binfo;
8694 while (CLASSTYPE_HAS_PRIMARY_BASE_P (BINFO_TYPE (b))
8695 && !BINFO_LOST_PRIMARY_P (b))
8697 tree primary_base = get_primary_binfo (b);
8698 gcc_assert (BINFO_PRIMARY_P (primary_base)
8699 && BINFO_INHERITANCE_CHAIN (primary_base) == b);
8700 b = primary_base;
8702 return b;
8705 /* Returns true if BINFO gets its vptr from a virtual base of the most derived
8706 type. Note that the virtual inheritance might be above or below BINFO in
8707 the hierarchy. */
8709 bool
8710 vptr_via_virtual_p (tree binfo)
8712 if (TYPE_P (binfo))
8713 binfo = TYPE_BINFO (binfo);
8714 tree primary = most_primary_binfo (binfo);
8715 /* Don't limit binfo_via_virtual, we want to return true when BINFO itself is
8716 a morally virtual base. */
8717 tree virt = binfo_via_virtual (primary, NULL_TREE);
8718 return virt != NULL_TREE;
8721 /* If INDENTED_P is zero, indent to INDENT. Return nonzero. */
8723 static int
8724 maybe_indent_hierarchy (FILE * stream, int indent, int indented_p)
8726 if (!indented_p)
8727 fprintf (stream, "%*s", indent, "");
8728 return 1;
8731 /* Dump the offsets of all the bases rooted at BINFO to STREAM.
8732 INDENT should be zero when called from the top level; it is
8733 incremented recursively. IGO indicates the next expected BINFO in
8734 inheritance graph ordering. */
8736 static tree
8737 dump_class_hierarchy_r (FILE *stream,
8738 dump_flags_t flags,
8739 tree binfo,
8740 tree igo,
8741 int indent)
8743 int indented = 0;
8744 tree base_binfo;
8745 int i;
8747 indented = maybe_indent_hierarchy (stream, indent, 0);
8748 fprintf (stream, "%s (0x" HOST_WIDE_INT_PRINT_HEX ") ",
8749 type_as_string (BINFO_TYPE (binfo), TFF_PLAIN_IDENTIFIER),
8750 (HOST_WIDE_INT) (uintptr_t) binfo);
8751 if (binfo != igo)
8753 fprintf (stream, "alternative-path\n");
8754 return igo;
8756 igo = TREE_CHAIN (binfo);
8758 fprintf (stream, HOST_WIDE_INT_PRINT_DEC,
8759 tree_to_shwi (BINFO_OFFSET (binfo)));
8760 if (is_empty_class (BINFO_TYPE (binfo)))
8761 fprintf (stream, " empty");
8762 else if (CLASSTYPE_NEARLY_EMPTY_P (BINFO_TYPE (binfo)))
8763 fprintf (stream, " nearly-empty");
8764 if (BINFO_VIRTUAL_P (binfo))
8765 fprintf (stream, " virtual");
8766 fprintf (stream, "\n");
8768 indented = 0;
8769 if (BINFO_PRIMARY_P (binfo))
8771 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
8772 fprintf (stream, " primary-for %s (0x" HOST_WIDE_INT_PRINT_HEX ")",
8773 type_as_string (BINFO_TYPE (BINFO_INHERITANCE_CHAIN (binfo)),
8774 TFF_PLAIN_IDENTIFIER),
8775 (HOST_WIDE_INT) (uintptr_t) BINFO_INHERITANCE_CHAIN (binfo));
8777 if (BINFO_LOST_PRIMARY_P (binfo))
8779 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
8780 fprintf (stream, " lost-primary");
8782 if (indented)
8783 fprintf (stream, "\n");
8785 if (!(flags & TDF_SLIM))
8787 int indented = 0;
8789 if (BINFO_SUBVTT_INDEX (binfo))
8791 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
8792 fprintf (stream, " subvttidx=%s",
8793 expr_as_string (BINFO_SUBVTT_INDEX (binfo),
8794 TFF_PLAIN_IDENTIFIER));
8796 if (BINFO_VPTR_INDEX (binfo))
8798 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
8799 fprintf (stream, " vptridx=%s",
8800 expr_as_string (BINFO_VPTR_INDEX (binfo),
8801 TFF_PLAIN_IDENTIFIER));
8803 if (BINFO_VPTR_FIELD (binfo))
8805 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
8806 fprintf (stream, " vbaseoffset=%s",
8807 expr_as_string (BINFO_VPTR_FIELD (binfo),
8808 TFF_PLAIN_IDENTIFIER));
8810 if (BINFO_VTABLE (binfo))
8812 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
8813 fprintf (stream, " vptr=%s",
8814 expr_as_string (BINFO_VTABLE (binfo),
8815 TFF_PLAIN_IDENTIFIER));
8818 if (indented)
8819 fprintf (stream, "\n");
8822 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
8823 igo = dump_class_hierarchy_r (stream, flags, base_binfo, igo, indent + 2);
8825 return igo;
8828 /* Dump the BINFO hierarchy for T. */
8830 static void
8831 dump_class_hierarchy_1 (FILE *stream, dump_flags_t flags, tree t)
8833 fprintf (stream, "Class %s\n", type_as_string (t, TFF_PLAIN_IDENTIFIER));
8834 fprintf (stream, " size=%lu align=%lu\n",
8835 (unsigned long)(tree_to_shwi (TYPE_SIZE (t)) / BITS_PER_UNIT),
8836 (unsigned long)(TYPE_ALIGN (t) / BITS_PER_UNIT));
8837 fprintf (stream, " base size=%lu base align=%lu\n",
8838 (unsigned long)(tree_to_shwi (TYPE_SIZE (CLASSTYPE_AS_BASE (t)))
8839 / BITS_PER_UNIT),
8840 (unsigned long)(TYPE_ALIGN (CLASSTYPE_AS_BASE (t))
8841 / BITS_PER_UNIT));
8842 dump_class_hierarchy_r (stream, flags, TYPE_BINFO (t), TYPE_BINFO (t), 0);
8843 fprintf (stream, "\n");
8846 /* Debug interface to hierarchy dumping. */
8848 void
8849 debug_class (tree t)
8851 dump_class_hierarchy_1 (stderr, TDF_SLIM, t);
8854 static void
8855 dump_class_hierarchy (tree t)
8857 dump_flags_t flags;
8858 if (FILE *stream = dump_begin (class_dump_id, &flags))
8860 dump_class_hierarchy_1 (stream, flags, t);
8861 dump_end (class_dump_id, stream);
8865 static void
8866 dump_array (FILE * stream, tree decl)
8868 tree value;
8869 unsigned HOST_WIDE_INT ix;
8870 HOST_WIDE_INT elt;
8871 tree size = TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (decl)));
8873 elt = (tree_to_shwi (TYPE_SIZE (TREE_TYPE (TREE_TYPE (decl))))
8874 / BITS_PER_UNIT);
8875 fprintf (stream, "%s:", decl_as_string (decl, TFF_PLAIN_IDENTIFIER));
8876 fprintf (stream, " %s entries",
8877 expr_as_string (size_binop (PLUS_EXPR, size, size_one_node),
8878 TFF_PLAIN_IDENTIFIER));
8879 fprintf (stream, "\n");
8881 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (DECL_INITIAL (decl)),
8882 ix, value)
8883 fprintf (stream, "%-4ld %s\n", (long)(ix * elt),
8884 expr_as_string (value, TFF_PLAIN_IDENTIFIER));
8887 static void
8888 dump_vtable (tree t, tree binfo, tree vtable)
8890 dump_flags_t flags;
8891 FILE *stream = dump_begin (class_dump_id, &flags);
8893 if (!stream)
8894 return;
8896 if (!(flags & TDF_SLIM))
8898 int ctor_vtbl_p = TYPE_BINFO (t) != binfo;
8900 fprintf (stream, "%s for %s",
8901 ctor_vtbl_p ? "Construction vtable" : "Vtable",
8902 type_as_string (BINFO_TYPE (binfo), TFF_PLAIN_IDENTIFIER));
8903 if (ctor_vtbl_p)
8905 if (!BINFO_VIRTUAL_P (binfo))
8906 fprintf (stream, " (0x" HOST_WIDE_INT_PRINT_HEX " instance)",
8907 (HOST_WIDE_INT) (uintptr_t) binfo);
8908 fprintf (stream, " in %s", type_as_string (t, TFF_PLAIN_IDENTIFIER));
8910 fprintf (stream, "\n");
8911 dump_array (stream, vtable);
8912 fprintf (stream, "\n");
8915 dump_end (class_dump_id, stream);
8918 static void
8919 dump_vtt (tree t, tree vtt)
8921 dump_flags_t flags;
8922 FILE *stream = dump_begin (class_dump_id, &flags);
8924 if (!stream)
8925 return;
8927 if (!(flags & TDF_SLIM))
8929 fprintf (stream, "VTT for %s\n",
8930 type_as_string (t, TFF_PLAIN_IDENTIFIER));
8931 dump_array (stream, vtt);
8932 fprintf (stream, "\n");
8935 dump_end (class_dump_id, stream);
8938 /* Dump a function or thunk and its thunkees. */
8940 static void
8941 dump_thunk (FILE *stream, int indent, tree thunk)
8943 static const char spaces[] = " ";
8944 tree name = DECL_NAME (thunk);
8945 tree thunks;
8947 fprintf (stream, "%.*s%p %s %s", indent, spaces,
8948 (void *)thunk,
8949 !DECL_THUNK_P (thunk) ? "function"
8950 : DECL_THIS_THUNK_P (thunk) ? "this-thunk" : "covariant-thunk",
8951 name ? IDENTIFIER_POINTER (name) : "<unset>");
8952 if (DECL_THUNK_P (thunk))
8954 HOST_WIDE_INT fixed_adjust = THUNK_FIXED_OFFSET (thunk);
8955 tree virtual_adjust = THUNK_VIRTUAL_OFFSET (thunk);
8957 fprintf (stream, " fixed=" HOST_WIDE_INT_PRINT_DEC, fixed_adjust);
8958 if (!virtual_adjust)
8959 /*NOP*/;
8960 else if (DECL_THIS_THUNK_P (thunk))
8961 fprintf (stream, " vcall=" HOST_WIDE_INT_PRINT_DEC,
8962 tree_to_shwi (virtual_adjust));
8963 else
8964 fprintf (stream, " vbase=" HOST_WIDE_INT_PRINT_DEC "(%s)",
8965 tree_to_shwi (BINFO_VPTR_FIELD (virtual_adjust)),
8966 type_as_string (BINFO_TYPE (virtual_adjust), TFF_SCOPE));
8967 if (THUNK_ALIAS (thunk))
8968 fprintf (stream, " alias to %p", (void *)THUNK_ALIAS (thunk));
8970 fprintf (stream, "\n");
8971 for (thunks = DECL_THUNKS (thunk); thunks; thunks = TREE_CHAIN (thunks))
8972 dump_thunk (stream, indent + 2, thunks);
8975 /* Dump the thunks for FN. */
8977 void
8978 debug_thunks (tree fn)
8980 dump_thunk (stderr, 0, fn);
8983 /* Virtual function table initialization. */
8985 /* Create all the necessary vtables for T and its base classes. */
8987 static void
8988 finish_vtbls (tree t)
8990 tree vbase;
8991 vec<constructor_elt, va_gc> *v = NULL;
8992 tree vtable = BINFO_VTABLE (TYPE_BINFO (t));
8994 /* We lay out the primary and secondary vtables in one contiguous
8995 vtable. The primary vtable is first, followed by the non-virtual
8996 secondary vtables in inheritance graph order. */
8997 accumulate_vtbl_inits (TYPE_BINFO (t), TYPE_BINFO (t), TYPE_BINFO (t),
8998 vtable, t, &v);
9000 /* Then come the virtual bases, also in inheritance graph order. */
9001 for (vbase = TYPE_BINFO (t); vbase; vbase = TREE_CHAIN (vbase))
9003 if (!BINFO_VIRTUAL_P (vbase))
9004 continue;
9005 accumulate_vtbl_inits (vbase, vbase, TYPE_BINFO (t), vtable, t, &v);
9008 if (BINFO_VTABLE (TYPE_BINFO (t)))
9009 initialize_vtable (TYPE_BINFO (t), v);
9012 /* Initialize the vtable for BINFO with the INITS. */
9014 static void
9015 initialize_vtable (tree binfo, vec<constructor_elt, va_gc> *inits)
9017 tree decl;
9019 layout_vtable_decl (binfo, vec_safe_length (inits));
9020 decl = get_vtbl_decl_for_binfo (binfo);
9021 initialize_artificial_var (decl, inits);
9022 dump_vtable (BINFO_TYPE (binfo), binfo, decl);
9025 /* Build the VTT (virtual table table) for T.
9026 A class requires a VTT if it has virtual bases.
9028 This holds
9029 1 - primary virtual pointer for complete object T
9030 2 - secondary VTTs for each direct non-virtual base of T which requires a
9032 3 - secondary virtual pointers for each direct or indirect base of T which
9033 has virtual bases or is reachable via a virtual path from T.
9034 4 - secondary VTTs for each direct or indirect virtual base of T.
9036 Secondary VTTs look like complete object VTTs without part 4. */
9038 static void
9039 build_vtt (tree t)
9041 tree type;
9042 tree vtt;
9043 tree index;
9044 vec<constructor_elt, va_gc> *inits;
9046 /* Build up the initializers for the VTT. */
9047 inits = NULL;
9048 index = size_zero_node;
9049 build_vtt_inits (TYPE_BINFO (t), t, &inits, &index);
9051 /* If we didn't need a VTT, we're done. */
9052 if (!inits)
9053 return;
9055 /* Figure out the type of the VTT. */
9056 type = build_array_of_n_type (const_ptr_type_node,
9057 inits->length ());
9059 /* Now, build the VTT object itself. */
9060 vtt = build_vtable (t, mangle_vtt_for_type (t), type);
9061 initialize_artificial_var (vtt, inits);
9062 /* Add the VTT to the vtables list. */
9063 DECL_CHAIN (vtt) = DECL_CHAIN (CLASSTYPE_VTABLES (t));
9064 DECL_CHAIN (CLASSTYPE_VTABLES (t)) = vtt;
9066 dump_vtt (t, vtt);
9069 /* When building a secondary VTT, BINFO_VTABLE is set to a TREE_LIST with
9070 PURPOSE the RTTI_BINFO, VALUE the real vtable pointer for this binfo,
9071 and CHAIN the vtable pointer for this binfo after construction is
9072 complete. VALUE can also be another BINFO, in which case we recurse. */
9074 static tree
9075 binfo_ctor_vtable (tree binfo)
9077 tree vt;
9079 while (1)
9081 vt = BINFO_VTABLE (binfo);
9082 if (TREE_CODE (vt) == TREE_LIST)
9083 vt = TREE_VALUE (vt);
9084 if (TREE_CODE (vt) == TREE_BINFO)
9085 binfo = vt;
9086 else
9087 break;
9090 return vt;
9093 /* Data for secondary VTT initialization. */
9094 struct secondary_vptr_vtt_init_data
9096 /* Is this the primary VTT? */
9097 bool top_level_p;
9099 /* Current index into the VTT. */
9100 tree index;
9102 /* Vector of initializers built up. */
9103 vec<constructor_elt, va_gc> *inits;
9105 /* The type being constructed by this secondary VTT. */
9106 tree type_being_constructed;
9109 /* Recursively build the VTT-initializer for BINFO (which is in the
9110 hierarchy dominated by T). INITS points to the end of the initializer
9111 list to date. INDEX is the VTT index where the next element will be
9112 replaced. Iff BINFO is the binfo for T, this is the top level VTT (i.e.
9113 not a subvtt for some base of T). When that is so, we emit the sub-VTTs
9114 for virtual bases of T. When it is not so, we build the constructor
9115 vtables for the BINFO-in-T variant. */
9117 static void
9118 build_vtt_inits (tree binfo, tree t, vec<constructor_elt, va_gc> **inits,
9119 tree *index)
9121 int i;
9122 tree b;
9123 tree init;
9124 secondary_vptr_vtt_init_data data;
9125 int top_level_p = SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), t);
9127 /* We only need VTTs for subobjects with virtual bases. */
9128 if (!CLASSTYPE_VBASECLASSES (BINFO_TYPE (binfo)))
9129 return;
9131 /* We need to use a construction vtable if this is not the primary
9132 VTT. */
9133 if (!top_level_p)
9135 build_ctor_vtbl_group (binfo, t);
9137 /* Record the offset in the VTT where this sub-VTT can be found. */
9138 BINFO_SUBVTT_INDEX (binfo) = *index;
9141 /* Add the address of the primary vtable for the complete object. */
9142 init = binfo_ctor_vtable (binfo);
9143 CONSTRUCTOR_APPEND_ELT (*inits, NULL_TREE, init);
9144 if (top_level_p)
9146 gcc_assert (!BINFO_VPTR_INDEX (binfo));
9147 BINFO_VPTR_INDEX (binfo) = *index;
9149 *index = size_binop (PLUS_EXPR, *index, TYPE_SIZE_UNIT (ptr_type_node));
9151 /* Recursively add the secondary VTTs for non-virtual bases. */
9152 for (i = 0; BINFO_BASE_ITERATE (binfo, i, b); ++i)
9153 if (!BINFO_VIRTUAL_P (b))
9154 build_vtt_inits (b, t, inits, index);
9156 /* Add secondary virtual pointers for all subobjects of BINFO with
9157 either virtual bases or reachable along a virtual path, except
9158 subobjects that are non-virtual primary bases. */
9159 data.top_level_p = top_level_p;
9160 data.index = *index;
9161 data.inits = *inits;
9162 data.type_being_constructed = BINFO_TYPE (binfo);
9164 dfs_walk_once (binfo, dfs_build_secondary_vptr_vtt_inits, NULL, &data);
9166 *index = data.index;
9168 /* data.inits might have grown as we added secondary virtual pointers.
9169 Make sure our caller knows about the new vector. */
9170 *inits = data.inits;
9172 if (top_level_p)
9173 /* Add the secondary VTTs for virtual bases in inheritance graph
9174 order. */
9175 for (b = TYPE_BINFO (BINFO_TYPE (binfo)); b; b = TREE_CHAIN (b))
9177 if (!BINFO_VIRTUAL_P (b))
9178 continue;
9180 build_vtt_inits (b, t, inits, index);
9182 else
9183 /* Remove the ctor vtables we created. */
9184 dfs_walk_all (binfo, dfs_fixup_binfo_vtbls, NULL, binfo);
9187 /* Called from build_vtt_inits via dfs_walk. BINFO is the binfo for the base
9188 in most derived. DATA is a SECONDARY_VPTR_VTT_INIT_DATA structure. */
9190 static tree
9191 dfs_build_secondary_vptr_vtt_inits (tree binfo, void *data_)
9193 secondary_vptr_vtt_init_data *data = (secondary_vptr_vtt_init_data *)data_;
9195 /* We don't care about bases that don't have vtables. */
9196 if (!TYPE_VFIELD (BINFO_TYPE (binfo)))
9197 return dfs_skip_bases;
9199 /* We're only interested in proper subobjects of the type being
9200 constructed. */
9201 if (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), data->type_being_constructed))
9202 return NULL_TREE;
9204 /* We're only interested in bases with virtual bases or reachable
9205 via a virtual path from the type being constructed. */
9206 if (!(CLASSTYPE_VBASECLASSES (BINFO_TYPE (binfo))
9207 || binfo_via_virtual (binfo, data->type_being_constructed)))
9208 return dfs_skip_bases;
9210 /* We're not interested in non-virtual primary bases. */
9211 if (!BINFO_VIRTUAL_P (binfo) && BINFO_PRIMARY_P (binfo))
9212 return NULL_TREE;
9214 /* Record the index where this secondary vptr can be found. */
9215 if (data->top_level_p)
9217 gcc_assert (!BINFO_VPTR_INDEX (binfo));
9218 BINFO_VPTR_INDEX (binfo) = data->index;
9220 if (BINFO_VIRTUAL_P (binfo))
9222 /* It's a primary virtual base, and this is not a
9223 construction vtable. Find the base this is primary of in
9224 the inheritance graph, and use that base's vtable
9225 now. */
9226 while (BINFO_PRIMARY_P (binfo))
9227 binfo = BINFO_INHERITANCE_CHAIN (binfo);
9231 /* Add the initializer for the secondary vptr itself. */
9232 CONSTRUCTOR_APPEND_ELT (data->inits, NULL_TREE, binfo_ctor_vtable (binfo));
9234 /* Advance the vtt index. */
9235 data->index = size_binop (PLUS_EXPR, data->index,
9236 TYPE_SIZE_UNIT (ptr_type_node));
9238 return NULL_TREE;
9241 /* Called from build_vtt_inits via dfs_walk. After building
9242 constructor vtables and generating the sub-vtt from them, we need
9243 to restore the BINFO_VTABLES that were scribbled on. DATA is the
9244 binfo of the base whose sub vtt was generated. */
9246 static tree
9247 dfs_fixup_binfo_vtbls (tree binfo, void* data)
9249 tree vtable = BINFO_VTABLE (binfo);
9251 if (!TYPE_CONTAINS_VPTR_P (BINFO_TYPE (binfo)))
9252 /* If this class has no vtable, none of its bases do. */
9253 return dfs_skip_bases;
9255 if (!vtable)
9256 /* This might be a primary base, so have no vtable in this
9257 hierarchy. */
9258 return NULL_TREE;
9260 /* If we scribbled the construction vtable vptr into BINFO, clear it
9261 out now. */
9262 if (TREE_CODE (vtable) == TREE_LIST
9263 && (TREE_PURPOSE (vtable) == (tree) data))
9264 BINFO_VTABLE (binfo) = TREE_CHAIN (vtable);
9266 return NULL_TREE;
9269 /* Build the construction vtable group for BINFO which is in the
9270 hierarchy dominated by T. */
9272 static void
9273 build_ctor_vtbl_group (tree binfo, tree t)
9275 tree type;
9276 tree vtbl;
9277 tree id;
9278 tree vbase;
9279 vec<constructor_elt, va_gc> *v;
9281 /* See if we've already created this construction vtable group. */
9282 id = mangle_ctor_vtbl_for_type (t, binfo);
9283 if (IDENTIFIER_GLOBAL_VALUE (id))
9284 return;
9286 gcc_assert (!SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), t));
9287 /* Build a version of VTBL (with the wrong type) for use in
9288 constructing the addresses of secondary vtables in the
9289 construction vtable group. */
9290 vtbl = build_vtable (t, id, ptr_type_node);
9291 DECL_CONSTRUCTION_VTABLE_P (vtbl) = 1;
9292 /* Don't export construction vtables from shared libraries. Even on
9293 targets that don't support hidden visibility, this tells
9294 can_refer_decl_in_current_unit_p not to assume that it's safe to
9295 access from a different compilation unit (bz 54314). */
9296 DECL_VISIBILITY (vtbl) = VISIBILITY_HIDDEN;
9297 DECL_VISIBILITY_SPECIFIED (vtbl) = true;
9299 v = NULL;
9300 accumulate_vtbl_inits (binfo, TYPE_BINFO (TREE_TYPE (binfo)),
9301 binfo, vtbl, t, &v);
9303 /* Add the vtables for each of our virtual bases using the vbase in T
9304 binfo. */
9305 for (vbase = TYPE_BINFO (BINFO_TYPE (binfo));
9306 vbase;
9307 vbase = TREE_CHAIN (vbase))
9309 tree b;
9311 if (!BINFO_VIRTUAL_P (vbase))
9312 continue;
9313 b = copied_binfo (vbase, binfo);
9315 accumulate_vtbl_inits (b, vbase, binfo, vtbl, t, &v);
9318 /* Figure out the type of the construction vtable. */
9319 type = build_array_of_n_type (vtable_entry_type, v->length ());
9320 layout_type (type);
9321 TREE_TYPE (vtbl) = type;
9322 DECL_SIZE (vtbl) = DECL_SIZE_UNIT (vtbl) = NULL_TREE;
9323 layout_decl (vtbl, 0);
9325 /* Initialize the construction vtable. */
9326 CLASSTYPE_VTABLES (t) = chainon (CLASSTYPE_VTABLES (t), vtbl);
9327 initialize_artificial_var (vtbl, v);
9328 dump_vtable (t, binfo, vtbl);
9331 /* Add the vtbl initializers for BINFO (and its bases other than
9332 non-virtual primaries) to the list of INITS. BINFO is in the
9333 hierarchy dominated by T. RTTI_BINFO is the binfo within T of
9334 the constructor the vtbl inits should be accumulated for. (If this
9335 is the complete object vtbl then RTTI_BINFO will be TYPE_BINFO (T).)
9336 ORIG_BINFO is the binfo for this object within BINFO_TYPE (RTTI_BINFO).
9337 BINFO is the active base equivalent of ORIG_BINFO in the inheritance
9338 graph of T. Both BINFO and ORIG_BINFO will have the same BINFO_TYPE,
9339 but are not necessarily the same in terms of layout. */
9341 static void
9342 accumulate_vtbl_inits (tree binfo,
9343 tree orig_binfo,
9344 tree rtti_binfo,
9345 tree vtbl,
9346 tree t,
9347 vec<constructor_elt, va_gc> **inits)
9349 int i;
9350 tree base_binfo;
9351 int ctor_vtbl_p = !SAME_BINFO_TYPE_P (BINFO_TYPE (rtti_binfo), t);
9353 gcc_assert (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), BINFO_TYPE (orig_binfo)));
9355 /* If it doesn't have a vptr, we don't do anything. */
9356 if (!TYPE_CONTAINS_VPTR_P (BINFO_TYPE (binfo)))
9357 return;
9359 /* If we're building a construction vtable, we're not interested in
9360 subobjects that don't require construction vtables. */
9361 if (ctor_vtbl_p
9362 && !CLASSTYPE_VBASECLASSES (BINFO_TYPE (binfo))
9363 && !binfo_via_virtual (orig_binfo, BINFO_TYPE (rtti_binfo)))
9364 return;
9366 /* Build the initializers for the BINFO-in-T vtable. */
9367 dfs_accumulate_vtbl_inits (binfo, orig_binfo, rtti_binfo, vtbl, t, inits);
9369 /* Walk the BINFO and its bases. We walk in preorder so that as we
9370 initialize each vtable we can figure out at what offset the
9371 secondary vtable lies from the primary vtable. We can't use
9372 dfs_walk here because we need to iterate through bases of BINFO
9373 and RTTI_BINFO simultaneously. */
9374 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
9376 /* Skip virtual bases. */
9377 if (BINFO_VIRTUAL_P (base_binfo))
9378 continue;
9379 accumulate_vtbl_inits (base_binfo,
9380 BINFO_BASE_BINFO (orig_binfo, i),
9381 rtti_binfo, vtbl, t,
9382 inits);
9386 /* Called from accumulate_vtbl_inits. Adds the initializers for the
9387 BINFO vtable to L. */
9389 static void
9390 dfs_accumulate_vtbl_inits (tree binfo,
9391 tree orig_binfo,
9392 tree rtti_binfo,
9393 tree orig_vtbl,
9394 tree t,
9395 vec<constructor_elt, va_gc> **l)
9397 tree vtbl = NULL_TREE;
9398 int ctor_vtbl_p = !SAME_BINFO_TYPE_P (BINFO_TYPE (rtti_binfo), t);
9399 int n_inits;
9401 if (ctor_vtbl_p
9402 && BINFO_VIRTUAL_P (orig_binfo) && BINFO_PRIMARY_P (orig_binfo))
9404 /* In the hierarchy of BINFO_TYPE (RTTI_BINFO), this is a
9405 primary virtual base. If it is not the same primary in
9406 the hierarchy of T, we'll need to generate a ctor vtable
9407 for it, to place at its location in T. If it is the same
9408 primary, we still need a VTT entry for the vtable, but it
9409 should point to the ctor vtable for the base it is a
9410 primary for within the sub-hierarchy of RTTI_BINFO.
9412 There are three possible cases:
9414 1) We are in the same place.
9415 2) We are a primary base within a lost primary virtual base of
9416 RTTI_BINFO.
9417 3) We are primary to something not a base of RTTI_BINFO. */
9419 tree b;
9420 tree last = NULL_TREE;
9422 /* First, look through the bases we are primary to for RTTI_BINFO
9423 or a virtual base. */
9424 b = binfo;
9425 while (BINFO_PRIMARY_P (b))
9427 b = BINFO_INHERITANCE_CHAIN (b);
9428 last = b;
9429 if (BINFO_VIRTUAL_P (b) || b == rtti_binfo)
9430 goto found;
9432 /* If we run out of primary links, keep looking down our
9433 inheritance chain; we might be an indirect primary. */
9434 for (b = last; b; b = BINFO_INHERITANCE_CHAIN (b))
9435 if (BINFO_VIRTUAL_P (b) || b == rtti_binfo)
9436 break;
9437 found:
9439 /* If we found RTTI_BINFO, this is case 1. If we found a virtual
9440 base B and it is a base of RTTI_BINFO, this is case 2. In
9441 either case, we share our vtable with LAST, i.e. the
9442 derived-most base within B of which we are a primary. */
9443 if (b == rtti_binfo
9444 || (b && binfo_for_vbase (BINFO_TYPE (b), BINFO_TYPE (rtti_binfo))))
9445 /* Just set our BINFO_VTABLE to point to LAST, as we may not have
9446 set LAST's BINFO_VTABLE yet. We'll extract the actual vptr in
9447 binfo_ctor_vtable after everything's been set up. */
9448 vtbl = last;
9450 /* Otherwise, this is case 3 and we get our own. */
9452 else if (!BINFO_NEW_VTABLE_MARKED (orig_binfo))
9453 return;
9455 n_inits = vec_safe_length (*l);
9457 if (!vtbl)
9459 tree index;
9460 int non_fn_entries;
9462 /* Add the initializer for this vtable. */
9463 build_vtbl_initializer (binfo, orig_binfo, t, rtti_binfo,
9464 &non_fn_entries, l);
9466 /* Figure out the position to which the VPTR should point. */
9467 vtbl = build1 (ADDR_EXPR, vtbl_ptr_type_node, orig_vtbl);
9468 index = size_binop (MULT_EXPR,
9469 TYPE_SIZE_UNIT (vtable_entry_type),
9470 size_int (non_fn_entries + n_inits));
9471 vtbl = fold_build_pointer_plus (vtbl, index);
9474 if (ctor_vtbl_p)
9475 /* For a construction vtable, we can't overwrite BINFO_VTABLE.
9476 So, we make a TREE_LIST. Later, dfs_fixup_binfo_vtbls will
9477 straighten this out. */
9478 BINFO_VTABLE (binfo) = tree_cons (rtti_binfo, vtbl, BINFO_VTABLE (binfo));
9479 else if (BINFO_PRIMARY_P (binfo) && BINFO_VIRTUAL_P (binfo))
9480 /* Throw away any unneeded intializers. */
9481 (*l)->truncate (n_inits);
9482 else
9483 /* For an ordinary vtable, set BINFO_VTABLE. */
9484 BINFO_VTABLE (binfo) = vtbl;
9487 static GTY(()) tree abort_fndecl_addr;
9488 static GTY(()) tree dvirt_fn;
9490 /* Construct the initializer for BINFO's virtual function table. BINFO
9491 is part of the hierarchy dominated by T. If we're building a
9492 construction vtable, the ORIG_BINFO is the binfo we should use to
9493 find the actual function pointers to put in the vtable - but they
9494 can be overridden on the path to most-derived in the graph that
9495 ORIG_BINFO belongs. Otherwise,
9496 ORIG_BINFO should be the same as BINFO. The RTTI_BINFO is the
9497 BINFO that should be indicated by the RTTI information in the
9498 vtable; it will be a base class of T, rather than T itself, if we
9499 are building a construction vtable.
9501 The value returned is a TREE_LIST suitable for wrapping in a
9502 CONSTRUCTOR to use as the DECL_INITIAL for a vtable. If
9503 NON_FN_ENTRIES_P is not NULL, *NON_FN_ENTRIES_P is set to the
9504 number of non-function entries in the vtable.
9506 It might seem that this function should never be called with a
9507 BINFO for which BINFO_PRIMARY_P holds, the vtable for such a
9508 base is always subsumed by a derived class vtable. However, when
9509 we are building construction vtables, we do build vtables for
9510 primary bases; we need these while the primary base is being
9511 constructed. */
9513 static void
9514 build_vtbl_initializer (tree binfo,
9515 tree orig_binfo,
9516 tree t,
9517 tree rtti_binfo,
9518 int* non_fn_entries_p,
9519 vec<constructor_elt, va_gc> **inits)
9521 tree v;
9522 vtbl_init_data vid;
9523 unsigned ix, jx;
9524 tree vbinfo;
9525 vec<tree, va_gc> *vbases;
9526 constructor_elt *e;
9528 /* Initialize VID. */
9529 memset (&vid, 0, sizeof (vid));
9530 vid.binfo = binfo;
9531 vid.derived = t;
9532 vid.rtti_binfo = rtti_binfo;
9533 vid.primary_vtbl_p = SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), t);
9534 vid.ctor_vtbl_p = !SAME_BINFO_TYPE_P (BINFO_TYPE (rtti_binfo), t);
9535 vid.generate_vcall_entries = true;
9536 /* The first vbase or vcall offset is at index -3 in the vtable. */
9537 vid.index = ssize_int(-3 * TARGET_VTABLE_DATA_ENTRY_DISTANCE);
9539 /* Add entries to the vtable for RTTI. */
9540 build_rtti_vtbl_entries (binfo, &vid);
9542 /* Create an array for keeping track of the functions we've
9543 processed. When we see multiple functions with the same
9544 signature, we share the vcall offsets. */
9545 vec_alloc (vid.fns, 32);
9546 /* Add the vcall and vbase offset entries. */
9547 build_vcall_and_vbase_vtbl_entries (binfo, &vid);
9549 /* Clear BINFO_VTABLE_PATH_MARKED; it's set by
9550 build_vbase_offset_vtbl_entries. */
9551 for (vbases = CLASSTYPE_VBASECLASSES (t), ix = 0;
9552 vec_safe_iterate (vbases, ix, &vbinfo); ix++)
9553 BINFO_VTABLE_PATH_MARKED (vbinfo) = 0;
9555 /* If the target requires padding between data entries, add that now. */
9556 if (TARGET_VTABLE_DATA_ENTRY_DISTANCE > 1)
9558 int n_entries = vec_safe_length (vid.inits);
9560 vec_safe_grow (vid.inits, TARGET_VTABLE_DATA_ENTRY_DISTANCE * n_entries);
9562 /* Move data entries into their new positions and add padding
9563 after the new positions. Iterate backwards so we don't
9564 overwrite entries that we would need to process later. */
9565 for (ix = n_entries - 1;
9566 vid.inits->iterate (ix, &e);
9567 ix--)
9569 int j;
9570 int new_position = (TARGET_VTABLE_DATA_ENTRY_DISTANCE * ix
9571 + (TARGET_VTABLE_DATA_ENTRY_DISTANCE - 1));
9573 (*vid.inits)[new_position] = *e;
9575 for (j = 1; j < TARGET_VTABLE_DATA_ENTRY_DISTANCE; ++j)
9577 constructor_elt *f = &(*vid.inits)[new_position - j];
9578 f->index = NULL_TREE;
9579 f->value = build1 (NOP_EXPR, vtable_entry_type,
9580 null_pointer_node);
9585 if (non_fn_entries_p)
9586 *non_fn_entries_p = vec_safe_length (vid.inits);
9588 /* The initializers for virtual functions were built up in reverse
9589 order. Straighten them out and add them to the running list in one
9590 step. */
9591 jx = vec_safe_length (*inits);
9592 vec_safe_grow (*inits, jx + vid.inits->length ());
9594 for (ix = vid.inits->length () - 1;
9595 vid.inits->iterate (ix, &e);
9596 ix--, jx++)
9597 (**inits)[jx] = *e;
9599 /* Go through all the ordinary virtual functions, building up
9600 initializers. */
9601 for (v = BINFO_VIRTUALS (orig_binfo); v; v = TREE_CHAIN (v))
9603 tree delta;
9604 tree vcall_index;
9605 tree fn, fn_original;
9606 tree init = NULL_TREE;
9608 fn = BV_FN (v);
9609 fn_original = fn;
9610 if (DECL_THUNK_P (fn))
9612 if (!DECL_NAME (fn))
9613 finish_thunk (fn);
9614 if (THUNK_ALIAS (fn))
9616 fn = THUNK_ALIAS (fn);
9617 BV_FN (v) = fn;
9619 fn_original = THUNK_TARGET (fn);
9622 /* If the only definition of this function signature along our
9623 primary base chain is from a lost primary, this vtable slot will
9624 never be used, so just zero it out. This is important to avoid
9625 requiring extra thunks which cannot be generated with the function.
9627 We first check this in update_vtable_entry_for_fn, so we handle
9628 restored primary bases properly; we also need to do it here so we
9629 zero out unused slots in ctor vtables, rather than filling them
9630 with erroneous values (though harmless, apart from relocation
9631 costs). */
9632 if (BV_LOST_PRIMARY (v))
9633 init = size_zero_node;
9635 if (! init)
9637 /* Pull the offset for `this', and the function to call, out of
9638 the list. */
9639 delta = BV_DELTA (v);
9640 vcall_index = BV_VCALL_INDEX (v);
9642 gcc_assert (TREE_CODE (delta) == INTEGER_CST);
9643 gcc_assert (TREE_CODE (fn) == FUNCTION_DECL);
9645 /* You can't call an abstract virtual function; it's abstract.
9646 So, we replace these functions with __pure_virtual. */
9647 if (DECL_PURE_VIRTUAL_P (fn_original))
9649 fn = abort_fndecl;
9650 if (!TARGET_VTABLE_USES_DESCRIPTORS)
9652 if (abort_fndecl_addr == NULL)
9653 abort_fndecl_addr
9654 = fold_convert (vfunc_ptr_type_node,
9655 build_fold_addr_expr (fn));
9656 init = abort_fndecl_addr;
9659 /* Likewise for deleted virtuals. */
9660 else if (DECL_DELETED_FN (fn_original))
9662 if (!dvirt_fn)
9664 tree name = get_identifier ("__cxa_deleted_virtual");
9665 dvirt_fn = IDENTIFIER_GLOBAL_VALUE (name);
9666 if (!dvirt_fn)
9667 dvirt_fn = push_library_fn
9668 (name,
9669 build_function_type_list (void_type_node, NULL_TREE),
9670 NULL_TREE, ECF_NORETURN | ECF_COLD);
9672 fn = dvirt_fn;
9673 if (!TARGET_VTABLE_USES_DESCRIPTORS)
9674 init = fold_convert (vfunc_ptr_type_node,
9675 build_fold_addr_expr (fn));
9677 else
9679 if (!integer_zerop (delta) || vcall_index)
9681 fn = make_thunk (fn, /*this_adjusting=*/1,
9682 delta, vcall_index);
9683 if (!DECL_NAME (fn))
9684 finish_thunk (fn);
9686 /* Take the address of the function, considering it to be of an
9687 appropriate generic type. */
9688 if (!TARGET_VTABLE_USES_DESCRIPTORS)
9689 init = fold_convert (vfunc_ptr_type_node,
9690 build_fold_addr_expr (fn));
9691 /* Don't refer to a virtual destructor from a constructor
9692 vtable or a vtable for an abstract class, since destroying
9693 an object under construction is undefined behavior and we
9694 don't want it to be considered a candidate for speculative
9695 devirtualization. But do create the thunk for ABI
9696 compliance. */
9697 if (DECL_DESTRUCTOR_P (fn_original)
9698 && (CLASSTYPE_PURE_VIRTUALS (DECL_CONTEXT (fn_original))
9699 || orig_binfo != binfo))
9700 init = size_zero_node;
9704 /* And add it to the chain of initializers. */
9705 if (TARGET_VTABLE_USES_DESCRIPTORS)
9707 int i;
9708 if (init == size_zero_node)
9709 for (i = 0; i < TARGET_VTABLE_USES_DESCRIPTORS; ++i)
9710 CONSTRUCTOR_APPEND_ELT (*inits, NULL_TREE, init);
9711 else
9712 for (i = 0; i < TARGET_VTABLE_USES_DESCRIPTORS; ++i)
9714 tree fdesc = build2 (FDESC_EXPR, vfunc_ptr_type_node,
9715 fn, build_int_cst (NULL_TREE, i));
9716 TREE_CONSTANT (fdesc) = 1;
9718 CONSTRUCTOR_APPEND_ELT (*inits, NULL_TREE, fdesc);
9721 else
9722 CONSTRUCTOR_APPEND_ELT (*inits, NULL_TREE, init);
9726 /* Adds to vid->inits the initializers for the vbase and vcall
9727 offsets in BINFO, which is in the hierarchy dominated by T. */
9729 static void
9730 build_vcall_and_vbase_vtbl_entries (tree binfo, vtbl_init_data* vid)
9732 tree b;
9734 /* If this is a derived class, we must first create entries
9735 corresponding to the primary base class. */
9736 b = get_primary_binfo (binfo);
9737 if (b)
9738 build_vcall_and_vbase_vtbl_entries (b, vid);
9740 /* Add the vbase entries for this base. */
9741 build_vbase_offset_vtbl_entries (binfo, vid);
9742 /* Add the vcall entries for this base. */
9743 build_vcall_offset_vtbl_entries (binfo, vid);
9746 /* Returns the initializers for the vbase offset entries in the vtable
9747 for BINFO (which is part of the class hierarchy dominated by T), in
9748 reverse order. VBASE_OFFSET_INDEX gives the vtable index
9749 where the next vbase offset will go. */
9751 static void
9752 build_vbase_offset_vtbl_entries (tree binfo, vtbl_init_data* vid)
9754 tree vbase;
9755 tree t;
9756 tree non_primary_binfo;
9758 /* If there are no virtual baseclasses, then there is nothing to
9759 do. */
9760 if (!CLASSTYPE_VBASECLASSES (BINFO_TYPE (binfo)))
9761 return;
9763 t = vid->derived;
9765 /* We might be a primary base class. Go up the inheritance hierarchy
9766 until we find the most derived class of which we are a primary base:
9767 it is the offset of that which we need to use. */
9768 non_primary_binfo = binfo;
9769 while (BINFO_INHERITANCE_CHAIN (non_primary_binfo))
9771 tree b;
9773 /* If we have reached a virtual base, then it must be a primary
9774 base (possibly multi-level) of vid->binfo, or we wouldn't
9775 have called build_vcall_and_vbase_vtbl_entries for it. But it
9776 might be a lost primary, so just skip down to vid->binfo. */
9777 if (BINFO_VIRTUAL_P (non_primary_binfo))
9779 non_primary_binfo = vid->binfo;
9780 break;
9783 b = BINFO_INHERITANCE_CHAIN (non_primary_binfo);
9784 if (get_primary_binfo (b) != non_primary_binfo)
9785 break;
9786 non_primary_binfo = b;
9789 /* Go through the virtual bases, adding the offsets. */
9790 for (vbase = TYPE_BINFO (BINFO_TYPE (binfo));
9791 vbase;
9792 vbase = TREE_CHAIN (vbase))
9794 tree b;
9795 tree delta;
9797 if (!BINFO_VIRTUAL_P (vbase))
9798 continue;
9800 /* Find the instance of this virtual base in the complete
9801 object. */
9802 b = copied_binfo (vbase, binfo);
9804 /* If we've already got an offset for this virtual base, we
9805 don't need another one. */
9806 if (BINFO_VTABLE_PATH_MARKED (b))
9807 continue;
9808 BINFO_VTABLE_PATH_MARKED (b) = 1;
9810 /* Figure out where we can find this vbase offset. */
9811 delta = size_binop (MULT_EXPR,
9812 vid->index,
9813 fold_convert (ssizetype,
9814 TYPE_SIZE_UNIT (vtable_entry_type)));
9815 if (vid->primary_vtbl_p)
9816 BINFO_VPTR_FIELD (b) = delta;
9818 if (binfo != TYPE_BINFO (t))
9819 /* The vbase offset had better be the same. */
9820 gcc_assert (tree_int_cst_equal (delta, BINFO_VPTR_FIELD (vbase)));
9822 /* The next vbase will come at a more negative offset. */
9823 vid->index = size_binop (MINUS_EXPR, vid->index,
9824 ssize_int (TARGET_VTABLE_DATA_ENTRY_DISTANCE));
9826 /* The initializer is the delta from BINFO to this virtual base.
9827 The vbase offsets go in reverse inheritance-graph order, and
9828 we are walking in inheritance graph order so these end up in
9829 the right order. */
9830 delta = size_diffop_loc (input_location,
9831 BINFO_OFFSET (b), BINFO_OFFSET (non_primary_binfo));
9833 CONSTRUCTOR_APPEND_ELT (vid->inits, NULL_TREE,
9834 fold_build1_loc (input_location, NOP_EXPR,
9835 vtable_entry_type, delta));
9839 /* Adds the initializers for the vcall offset entries in the vtable
9840 for BINFO (which is part of the class hierarchy dominated by VID->DERIVED)
9841 to VID->INITS. */
9843 static void
9844 build_vcall_offset_vtbl_entries (tree binfo, vtbl_init_data* vid)
9846 /* We only need these entries if this base is a virtual base. We
9847 compute the indices -- but do not add to the vtable -- when
9848 building the main vtable for a class. */
9849 if (binfo == TYPE_BINFO (vid->derived)
9850 || (BINFO_VIRTUAL_P (binfo)
9851 /* If BINFO is RTTI_BINFO, then (since BINFO does not
9852 correspond to VID->DERIVED), we are building a primary
9853 construction virtual table. Since this is a primary
9854 virtual table, we do not need the vcall offsets for
9855 BINFO. */
9856 && binfo != vid->rtti_binfo))
9858 /* We need a vcall offset for each of the virtual functions in this
9859 vtable. For example:
9861 class A { virtual void f (); };
9862 class B1 : virtual public A { virtual void f (); };
9863 class B2 : virtual public A { virtual void f (); };
9864 class C: public B1, public B2 { virtual void f (); };
9866 A C object has a primary base of B1, which has a primary base of A. A
9867 C also has a secondary base of B2, which no longer has a primary base
9868 of A. So the B2-in-C construction vtable needs a secondary vtable for
9869 A, which will adjust the A* to a B2* to call f. We have no way of
9870 knowing what (or even whether) this offset will be when we define B2,
9871 so we store this "vcall offset" in the A sub-vtable and look it up in
9872 a "virtual thunk" for B2::f.
9874 We need entries for all the functions in our primary vtable and
9875 in our non-virtual bases' secondary vtables. */
9876 vid->vbase = binfo;
9877 /* If we are just computing the vcall indices -- but do not need
9878 the actual entries -- not that. */
9879 if (!BINFO_VIRTUAL_P (binfo))
9880 vid->generate_vcall_entries = false;
9881 /* Now, walk through the non-virtual bases, adding vcall offsets. */
9882 add_vcall_offset_vtbl_entries_r (binfo, vid);
9886 /* Build vcall offsets, starting with those for BINFO. */
9888 static void
9889 add_vcall_offset_vtbl_entries_r (tree binfo, vtbl_init_data* vid)
9891 int i;
9892 tree primary_binfo;
9893 tree base_binfo;
9895 /* Don't walk into virtual bases -- except, of course, for the
9896 virtual base for which we are building vcall offsets. Any
9897 primary virtual base will have already had its offsets generated
9898 through the recursion in build_vcall_and_vbase_vtbl_entries. */
9899 if (BINFO_VIRTUAL_P (binfo) && vid->vbase != binfo)
9900 return;
9902 /* If BINFO has a primary base, process it first. */
9903 primary_binfo = get_primary_binfo (binfo);
9904 if (primary_binfo)
9905 add_vcall_offset_vtbl_entries_r (primary_binfo, vid);
9907 /* Add BINFO itself to the list. */
9908 add_vcall_offset_vtbl_entries_1 (binfo, vid);
9910 /* Scan the non-primary bases of BINFO. */
9911 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
9912 if (base_binfo != primary_binfo)
9913 add_vcall_offset_vtbl_entries_r (base_binfo, vid);
9916 /* Called from build_vcall_offset_vtbl_entries_r. */
9918 static void
9919 add_vcall_offset_vtbl_entries_1 (tree binfo, vtbl_init_data* vid)
9921 /* Make entries for the rest of the virtuals. */
9922 tree orig_fn;
9924 /* The ABI requires that the methods be processed in declaration
9925 order. */
9926 for (orig_fn = TYPE_FIELDS (BINFO_TYPE (binfo));
9927 orig_fn;
9928 orig_fn = DECL_CHAIN (orig_fn))
9929 if (TREE_CODE (orig_fn) == FUNCTION_DECL && DECL_VINDEX (orig_fn))
9930 add_vcall_offset (orig_fn, binfo, vid);
9933 /* Add a vcall offset entry for ORIG_FN to the vtable. */
9935 static void
9936 add_vcall_offset (tree orig_fn, tree binfo, vtbl_init_data *vid)
9938 size_t i;
9939 tree vcall_offset;
9940 tree derived_entry;
9942 /* If there is already an entry for a function with the same
9943 signature as FN, then we do not need a second vcall offset.
9944 Check the list of functions already present in the derived
9945 class vtable. */
9946 FOR_EACH_VEC_SAFE_ELT (vid->fns, i, derived_entry)
9948 if (same_signature_p (derived_entry, orig_fn)
9949 /* We only use one vcall offset for virtual destructors,
9950 even though there are two virtual table entries. */
9951 || (DECL_DESTRUCTOR_P (derived_entry)
9952 && DECL_DESTRUCTOR_P (orig_fn)))
9953 return;
9956 /* If we are building these vcall offsets as part of building
9957 the vtable for the most derived class, remember the vcall
9958 offset. */
9959 if (vid->binfo == TYPE_BINFO (vid->derived))
9961 tree_pair_s elt = {orig_fn, vid->index};
9962 vec_safe_push (CLASSTYPE_VCALL_INDICES (vid->derived), elt);
9965 /* The next vcall offset will be found at a more negative
9966 offset. */
9967 vid->index = size_binop (MINUS_EXPR, vid->index,
9968 ssize_int (TARGET_VTABLE_DATA_ENTRY_DISTANCE));
9970 /* Keep track of this function. */
9971 vec_safe_push (vid->fns, orig_fn);
9973 if (vid->generate_vcall_entries)
9975 tree base;
9976 tree fn;
9978 /* Find the overriding function. */
9979 fn = find_final_overrider (vid->rtti_binfo, binfo, orig_fn);
9980 if (fn == error_mark_node)
9981 vcall_offset = build_zero_cst (vtable_entry_type);
9982 else
9984 base = TREE_VALUE (fn);
9986 /* The vbase we're working on is a primary base of
9987 vid->binfo. But it might be a lost primary, so its
9988 BINFO_OFFSET might be wrong, so we just use the
9989 BINFO_OFFSET from vid->binfo. */
9990 vcall_offset = size_diffop_loc (input_location,
9991 BINFO_OFFSET (base),
9992 BINFO_OFFSET (vid->binfo));
9993 vcall_offset = fold_build1_loc (input_location,
9994 NOP_EXPR, vtable_entry_type,
9995 vcall_offset);
9997 /* Add the initializer to the vtable. */
9998 CONSTRUCTOR_APPEND_ELT (vid->inits, NULL_TREE, vcall_offset);
10002 /* Return vtbl initializers for the RTTI entries corresponding to the
10003 BINFO's vtable. The RTTI entries should indicate the object given
10004 by VID->rtti_binfo. */
10006 static void
10007 build_rtti_vtbl_entries (tree binfo, vtbl_init_data* vid)
10009 tree b;
10010 tree t;
10011 tree offset;
10012 tree decl;
10013 tree init;
10015 t = BINFO_TYPE (vid->rtti_binfo);
10017 /* To find the complete object, we will first convert to our most
10018 primary base, and then add the offset in the vtbl to that value. */
10019 b = most_primary_binfo (binfo);
10020 offset = size_diffop_loc (input_location,
10021 BINFO_OFFSET (vid->rtti_binfo), BINFO_OFFSET (b));
10023 /* The second entry is the address of the typeinfo object. */
10024 if (flag_rtti)
10025 decl = build_address (get_tinfo_decl (t));
10026 else
10027 decl = integer_zero_node;
10029 /* Convert the declaration to a type that can be stored in the
10030 vtable. */
10031 init = build_nop (vfunc_ptr_type_node, decl);
10032 CONSTRUCTOR_APPEND_ELT (vid->inits, NULL_TREE, init);
10034 /* Add the offset-to-top entry. It comes earlier in the vtable than
10035 the typeinfo entry. Convert the offset to look like a
10036 function pointer, so that we can put it in the vtable. */
10037 init = build_nop (vfunc_ptr_type_node, offset);
10038 CONSTRUCTOR_APPEND_ELT (vid->inits, NULL_TREE, init);
10041 /* TRUE iff TYPE is uniquely derived from PARENT. Ignores
10042 accessibility. */
10044 bool
10045 uniquely_derived_from_p (tree parent, tree type)
10047 tree base = lookup_base (type, parent, ba_unique, NULL, tf_none);
10048 return base && base != error_mark_node;
10051 /* TRUE iff TYPE is publicly & uniquely derived from PARENT. */
10053 bool
10054 publicly_uniquely_derived_p (tree parent, tree type)
10056 tree base = lookup_base (type, parent, ba_ignore_scope | ba_check,
10057 NULL, tf_none);
10058 return base && base != error_mark_node;
10061 /* CTX1 and CTX2 are declaration contexts. Return the innermost common
10062 class between them, if any. */
10064 tree
10065 common_enclosing_class (tree ctx1, tree ctx2)
10067 if (!TYPE_P (ctx1) || !TYPE_P (ctx2))
10068 return NULL_TREE;
10069 gcc_assert (ctx1 == TYPE_MAIN_VARIANT (ctx1)
10070 && ctx2 == TYPE_MAIN_VARIANT (ctx2));
10071 if (ctx1 == ctx2)
10072 return ctx1;
10073 for (tree t = ctx1; TYPE_P (t); t = TYPE_CONTEXT (t))
10074 TYPE_MARKED_P (t) = true;
10075 tree found = NULL_TREE;
10076 for (tree t = ctx2; TYPE_P (t); t = TYPE_CONTEXT (t))
10077 if (TYPE_MARKED_P (t))
10079 found = t;
10080 break;
10082 for (tree t = ctx1; TYPE_P (t); t = TYPE_CONTEXT (t))
10083 TYPE_MARKED_P (t) = false;
10084 return found;
10087 #include "gt-cp-class.h"