* class.c (add_implicitly_declared_members): Use
[official-gcc.git] / gcc / cp / class.c
blob4b9b93a77a778131bbb4870848142d87170f316e
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 /* Create slots for constructors and destructors. */
1043 method_vec->quick_push (NULL_TREE);
1044 method_vec->quick_push (NULL_TREE);
1045 CLASSTYPE_METHOD_VEC (type) = method_vec;
1048 /* Maintain TYPE_HAS_USER_CONSTRUCTOR, etc. */
1049 grok_special_member_properties (method);
1051 /* Constructors and destructors go in special slots. */
1052 if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (method))
1053 slot = CLASSTYPE_CONSTRUCTOR_SLOT;
1054 else if (DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (method))
1055 slot = CLASSTYPE_DESTRUCTOR_SLOT;
1056 else
1058 tree m;
1060 insert_p = true;
1061 /* See if we already have an entry with this name. */
1062 for (slot = CLASSTYPE_FIRST_CONVERSION_SLOT;
1063 vec_safe_iterate (method_vec, slot, &m);
1064 ++slot)
1066 m = OVL_FIRST (m);
1067 if (template_conv_p)
1069 if (TREE_CODE (m) == TEMPLATE_DECL
1070 && DECL_TEMPLATE_CONV_FN_P (m))
1071 insert_p = false;
1072 break;
1074 if (conv_p && !DECL_CONV_FN_P (m))
1075 break;
1076 if (DECL_NAME (m) == DECL_NAME (method))
1078 insert_p = false;
1079 break;
1081 if (complete_p
1082 && !DECL_CONV_FN_P (m)
1083 && DECL_NAME (m) > DECL_NAME (method))
1084 break;
1087 current_fns = insert_p ? NULL_TREE : (*method_vec)[slot];
1089 /* Check to see if we've already got this method. */
1090 for (ovl_iterator iter (current_fns); iter; ++iter)
1092 tree fn = *iter;
1093 tree fn_type;
1094 tree method_type;
1095 tree parms1;
1096 tree parms2;
1098 if (TREE_CODE (fn) != TREE_CODE (method))
1099 continue;
1101 /* Two using-declarations can coexist, we'll complain about ambiguity in
1102 overload resolution. */
1103 if (via_using && iter.using_p ()
1104 /* Except handle inherited constructors specially. */
1105 && ! DECL_CONSTRUCTOR_P (fn))
1106 continue;
1108 /* [over.load] Member function declarations with the
1109 same name and the same parameter types cannot be
1110 overloaded if any of them is a static member
1111 function declaration.
1113 [over.load] Member function declarations with the same name and
1114 the same parameter-type-list as well as member function template
1115 declarations with the same name, the same parameter-type-list, and
1116 the same template parameter lists cannot be overloaded if any of
1117 them, but not all, have a ref-qualifier.
1119 [namespace.udecl] When a using-declaration brings names
1120 from a base class into a derived class scope, member
1121 functions in the derived class override and/or hide member
1122 functions with the same name and parameter types in a base
1123 class (rather than conflicting). */
1124 fn_type = TREE_TYPE (fn);
1125 method_type = TREE_TYPE (method);
1126 parms1 = TYPE_ARG_TYPES (fn_type);
1127 parms2 = TYPE_ARG_TYPES (method_type);
1129 /* Compare the quals on the 'this' parm. Don't compare
1130 the whole types, as used functions are treated as
1131 coming from the using class in overload resolution. */
1132 if (! DECL_STATIC_FUNCTION_P (fn)
1133 && ! DECL_STATIC_FUNCTION_P (method)
1134 /* Either both or neither need to be ref-qualified for
1135 differing quals to allow overloading. */
1136 && (FUNCTION_REF_QUALIFIED (fn_type)
1137 == FUNCTION_REF_QUALIFIED (method_type))
1138 && (type_memfn_quals (fn_type) != type_memfn_quals (method_type)
1139 || type_memfn_rqual (fn_type) != type_memfn_rqual (method_type)))
1140 continue;
1142 /* For templates, the return type and template parameters
1143 must be identical. */
1144 if (TREE_CODE (fn) == TEMPLATE_DECL
1145 && (!same_type_p (TREE_TYPE (fn_type),
1146 TREE_TYPE (method_type))
1147 || !comp_template_parms (DECL_TEMPLATE_PARMS (fn),
1148 DECL_TEMPLATE_PARMS (method))))
1149 continue;
1151 if (! DECL_STATIC_FUNCTION_P (fn))
1152 parms1 = TREE_CHAIN (parms1);
1153 if (! DECL_STATIC_FUNCTION_P (method))
1154 parms2 = TREE_CHAIN (parms2);
1156 /* Bring back parameters omitted from an inherited ctor. */
1157 if (ctor_omit_inherited_parms (fn))
1158 parms1 = FUNCTION_FIRST_USER_PARMTYPE (DECL_ORIGIN (fn));
1159 if (ctor_omit_inherited_parms (method))
1160 parms2 = FUNCTION_FIRST_USER_PARMTYPE (DECL_ORIGIN (method));
1162 if (compparms (parms1, parms2)
1163 && (!DECL_CONV_FN_P (fn)
1164 || same_type_p (TREE_TYPE (fn_type),
1165 TREE_TYPE (method_type)))
1166 && equivalently_constrained (fn, method))
1168 /* For function versions, their parms and types match
1169 but they are not duplicates. Record function versions
1170 as and when they are found. extern "C" functions are
1171 not treated as versions. */
1172 if (TREE_CODE (fn) == FUNCTION_DECL
1173 && TREE_CODE (method) == FUNCTION_DECL
1174 && !DECL_EXTERN_C_P (fn)
1175 && !DECL_EXTERN_C_P (method)
1176 && targetm.target_option.function_versions (fn, method))
1178 /* Mark functions as versions if necessary. Modify the mangled
1179 decl name if necessary. */
1180 if (!DECL_FUNCTION_VERSIONED (fn))
1182 DECL_FUNCTION_VERSIONED (fn) = 1;
1183 if (DECL_ASSEMBLER_NAME_SET_P (fn))
1184 mangle_decl (fn);
1186 if (!DECL_FUNCTION_VERSIONED (method))
1188 DECL_FUNCTION_VERSIONED (method) = 1;
1189 if (DECL_ASSEMBLER_NAME_SET_P (method))
1190 mangle_decl (method);
1192 cgraph_node::record_function_versions (fn, method);
1193 continue;
1196 if (DECL_INHERITED_CTOR (method))
1198 if (DECL_INHERITED_CTOR (fn))
1200 tree basem = DECL_INHERITED_CTOR_BASE (method);
1201 tree basef = DECL_INHERITED_CTOR_BASE (fn);
1202 if (flag_new_inheriting_ctors)
1204 if (basem == basef)
1206 /* Inheriting the same constructor along different
1207 paths, combine them. */
1208 SET_DECL_INHERITED_CTOR
1209 (fn, ovl_make (DECL_INHERITED_CTOR (method),
1210 DECL_INHERITED_CTOR (fn)));
1211 /* And discard the new one. */
1212 return false;
1214 else
1215 /* Inherited ctors can coexist until overload
1216 resolution. */
1217 continue;
1219 error_at (DECL_SOURCE_LOCATION (method),
1220 "%q#D conflicts with version inherited from %qT",
1221 method, basef);
1222 inform (DECL_SOURCE_LOCATION (fn),
1223 "version inherited from %qT declared here",
1224 basef);
1226 /* Otherwise defer to the other function. */
1227 return false;
1230 if (via_using)
1231 /* Defer to the local function. */
1232 return false;
1233 else if (flag_new_inheriting_ctors
1234 && DECL_INHERITED_CTOR (fn))
1236 /* Remove the inherited constructor. */
1237 current_fns = iter.remove_node (current_fns);
1238 continue;
1240 else
1242 error_at (DECL_SOURCE_LOCATION (method),
1243 "%q#D cannot be overloaded with %q#D", method, fn);
1244 inform (DECL_SOURCE_LOCATION (fn),
1245 "previous declaration %q#D", fn);
1246 return false;
1251 /* A class should never have more than one destructor. */
1252 if (current_fns && DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (method))
1253 return false;
1255 current_fns = ovl_insert (method, current_fns, via_using);
1257 if (conv_p)
1258 TYPE_HAS_CONVERSION (type) = 1;
1259 else if (slot >= CLASSTYPE_FIRST_CONVERSION_SLOT && !complete_p)
1260 push_class_level_binding (DECL_NAME (method), current_fns);
1262 if (insert_p)
1264 bool reallocated;
1266 /* We only expect to add few methods in the COMPLETE_P case, so
1267 just make room for one more method in that case. */
1268 if (complete_p)
1269 reallocated = vec_safe_reserve_exact (method_vec, 1);
1270 else
1271 reallocated = vec_safe_reserve (method_vec, 1);
1272 if (reallocated)
1273 CLASSTYPE_METHOD_VEC (type) = method_vec;
1274 if (slot == method_vec->length ())
1275 method_vec->quick_push (current_fns);
1276 else
1277 method_vec->quick_insert (slot, current_fns);
1279 else
1280 /* Replace the current slot. */
1281 (*method_vec)[slot] = current_fns;
1282 return true;
1285 /* Subroutines of finish_struct. */
1287 /* Change the access of FDECL to ACCESS in T. Return 1 if change was
1288 legit, otherwise return 0. */
1290 static int
1291 alter_access (tree t, tree fdecl, tree access)
1293 tree elem;
1295 retrofit_lang_decl (fdecl);
1297 gcc_assert (!DECL_DISCRIMINATOR_P (fdecl));
1299 elem = purpose_member (t, DECL_ACCESS (fdecl));
1300 if (elem)
1302 if (TREE_VALUE (elem) != access)
1304 if (TREE_CODE (TREE_TYPE (fdecl)) == FUNCTION_DECL)
1305 error ("conflicting access specifications for method"
1306 " %q+D, ignored", TREE_TYPE (fdecl));
1307 else
1308 error ("conflicting access specifications for field %qE, ignored",
1309 DECL_NAME (fdecl));
1311 else
1313 /* They're changing the access to the same thing they changed
1314 it to before. That's OK. */
1318 else
1320 perform_or_defer_access_check (TYPE_BINFO (t), fdecl, fdecl,
1321 tf_warning_or_error);
1322 DECL_ACCESS (fdecl) = tree_cons (t, access, DECL_ACCESS (fdecl));
1323 return 1;
1325 return 0;
1328 /* Return the access node for DECL's access in its enclosing class. */
1330 tree
1331 declared_access (tree decl)
1333 return (TREE_PRIVATE (decl) ? access_private_node
1334 : TREE_PROTECTED (decl) ? access_protected_node
1335 : access_public_node);
1338 /* Process the USING_DECL, which is a member of T. */
1340 static void
1341 handle_using_decl (tree using_decl, tree t)
1343 tree decl = USING_DECL_DECLS (using_decl);
1344 tree name = DECL_NAME (using_decl);
1345 tree access = declared_access (using_decl);
1346 tree flist = NULL_TREE;
1347 tree old_value;
1349 gcc_assert (!processing_template_decl && decl);
1351 old_value = lookup_member (t, name, /*protect=*/0, /*want_type=*/false,
1352 tf_warning_or_error);
1353 if (old_value)
1355 old_value = OVL_FIRST (old_value);
1357 if (DECL_P (old_value) && DECL_CONTEXT (old_value) == t)
1358 /* OK */;
1359 else
1360 old_value = NULL_TREE;
1363 cp_emit_debug_info_for_using (decl, t);
1365 if (is_overloaded_fn (decl))
1366 flist = decl;
1368 if (! old_value)
1370 else if (is_overloaded_fn (old_value))
1372 if (flist)
1373 /* It's OK to use functions from a base when there are functions with
1374 the same name already present in the current class. */;
1375 else
1377 error_at (DECL_SOURCE_LOCATION (using_decl), "%qD invalid in %q#T "
1378 "because of local method %q#D with same name",
1379 using_decl, t, old_value);
1380 inform (DECL_SOURCE_LOCATION (old_value),
1381 "local method %q#D declared here", old_value);
1382 return;
1385 else if (!DECL_ARTIFICIAL (old_value))
1387 error_at (DECL_SOURCE_LOCATION (using_decl), "%qD invalid in %q#T "
1388 "because of local member %q#D with same name",
1389 using_decl, t, old_value);
1390 inform (DECL_SOURCE_LOCATION (old_value),
1391 "local member %q#D declared here", old_value);
1392 return;
1395 /* Make type T see field decl FDECL with access ACCESS. */
1396 if (flist)
1397 for (ovl_iterator iter (flist); iter; ++iter)
1399 add_method (t, *iter, true);
1400 alter_access (t, *iter, access);
1402 else
1403 alter_access (t, decl, access);
1406 /* Data structure for find_abi_tags_r, below. */
1408 struct abi_tag_data
1410 tree t; // The type that we're checking for missing tags.
1411 tree subob; // The subobject of T that we're getting tags from.
1412 tree tags; // error_mark_node for diagnostics, or a list of missing tags.
1415 /* Subroutine of find_abi_tags_r. Handle a single TAG found on the class TP
1416 in the context of P. TAG can be either an identifier (the DECL_NAME of
1417 a tag NAMESPACE_DECL) or a STRING_CST (a tag attribute). */
1419 static void
1420 check_tag (tree tag, tree id, tree *tp, abi_tag_data *p)
1422 if (!IDENTIFIER_MARKED (id))
1424 if (p->tags != error_mark_node)
1426 /* We're collecting tags from template arguments or from
1427 the type of a variable or function return type. */
1428 p->tags = tree_cons (NULL_TREE, tag, p->tags);
1430 /* Don't inherit this tag multiple times. */
1431 IDENTIFIER_MARKED (id) = true;
1433 if (TYPE_P (p->t))
1435 /* Tags inherited from type template arguments are only used
1436 to avoid warnings. */
1437 ABI_TAG_IMPLICIT (p->tags) = true;
1438 return;
1440 /* For functions and variables we want to warn, too. */
1443 /* Otherwise we're diagnosing missing tags. */
1444 if (TREE_CODE (p->t) == FUNCTION_DECL)
1446 if (warning (OPT_Wabi_tag, "%qD inherits the %E ABI tag "
1447 "that %qT (used in its return type) has",
1448 p->t, tag, *tp))
1449 inform (location_of (*tp), "%qT declared here", *tp);
1451 else if (VAR_P (p->t))
1453 if (warning (OPT_Wabi_tag, "%qD inherits the %E ABI tag "
1454 "that %qT (used in its type) has", p->t, tag, *tp))
1455 inform (location_of (*tp), "%qT declared here", *tp);
1457 else if (TYPE_P (p->subob))
1459 if (warning (OPT_Wabi_tag, "%qT does not have the %E ABI tag "
1460 "that base %qT has", p->t, tag, p->subob))
1461 inform (location_of (p->subob), "%qT declared here",
1462 p->subob);
1464 else
1466 if (warning (OPT_Wabi_tag, "%qT does not have the %E ABI tag "
1467 "that %qT (used in the type of %qD) has",
1468 p->t, tag, *tp, p->subob))
1470 inform (location_of (p->subob), "%qD declared here",
1471 p->subob);
1472 inform (location_of (*tp), "%qT declared here", *tp);
1478 /* Find all the ABI tags in the attribute list ATTR and either call
1479 check_tag (if TP is non-null) or set IDENTIFIER_MARKED to val. */
1481 static void
1482 mark_or_check_attr_tags (tree attr, tree *tp, abi_tag_data *p, bool val)
1484 if (!attr)
1485 return;
1486 for (; (attr = lookup_attribute ("abi_tag", attr));
1487 attr = TREE_CHAIN (attr))
1488 for (tree list = TREE_VALUE (attr); list;
1489 list = TREE_CHAIN (list))
1491 tree tag = TREE_VALUE (list);
1492 tree id = get_identifier (TREE_STRING_POINTER (tag));
1493 if (tp)
1494 check_tag (tag, id, tp, p);
1495 else
1496 IDENTIFIER_MARKED (id) = val;
1500 /* Find all the ABI tags on T and its enclosing scopes and either call
1501 check_tag (if TP is non-null) or set IDENTIFIER_MARKED to val. */
1503 static void
1504 mark_or_check_tags (tree t, tree *tp, abi_tag_data *p, bool val)
1506 while (t != global_namespace)
1508 tree attr;
1509 if (TYPE_P (t))
1511 attr = TYPE_ATTRIBUTES (t);
1512 t = CP_TYPE_CONTEXT (t);
1514 else
1516 attr = DECL_ATTRIBUTES (t);
1517 t = CP_DECL_CONTEXT (t);
1519 mark_or_check_attr_tags (attr, tp, p, val);
1523 /* walk_tree callback for check_abi_tags: if the type at *TP involves any
1524 types with ABI tags, add the corresponding identifiers to the VEC in
1525 *DATA and set IDENTIFIER_MARKED. */
1527 static tree
1528 find_abi_tags_r (tree *tp, int *walk_subtrees, void *data)
1530 if (!OVERLOAD_TYPE_P (*tp))
1531 return NULL_TREE;
1533 /* walk_tree shouldn't be walking into any subtrees of a RECORD_TYPE
1534 anyway, but let's make sure of it. */
1535 *walk_subtrees = false;
1537 abi_tag_data *p = static_cast<struct abi_tag_data*>(data);
1539 mark_or_check_tags (*tp, tp, p, false);
1541 return NULL_TREE;
1544 /* walk_tree callback for mark_abi_tags: if *TP is a class, set
1545 IDENTIFIER_MARKED on its ABI tags. */
1547 static tree
1548 mark_abi_tags_r (tree *tp, int *walk_subtrees, void *data)
1550 if (!OVERLOAD_TYPE_P (*tp))
1551 return NULL_TREE;
1553 /* walk_tree shouldn't be walking into any subtrees of a RECORD_TYPE
1554 anyway, but let's make sure of it. */
1555 *walk_subtrees = false;
1557 bool *valp = static_cast<bool*>(data);
1559 mark_or_check_tags (*tp, NULL, NULL, *valp);
1561 return NULL_TREE;
1564 /* Set IDENTIFIER_MARKED on all the ABI tags on T and its enclosing
1565 scopes. */
1567 static void
1568 mark_abi_tags (tree t, bool val)
1570 mark_or_check_tags (t, NULL, NULL, val);
1571 if (DECL_P (t))
1573 if (DECL_LANG_SPECIFIC (t) && DECL_USE_TEMPLATE (t)
1574 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (t)))
1576 /* Template arguments are part of the signature. */
1577 tree level = INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (t));
1578 for (int j = 0; j < TREE_VEC_LENGTH (level); ++j)
1580 tree arg = TREE_VEC_ELT (level, j);
1581 cp_walk_tree_without_duplicates (&arg, mark_abi_tags_r, &val);
1584 if (TREE_CODE (t) == FUNCTION_DECL)
1585 /* A function's parameter types are part of the signature, so
1586 we don't need to inherit any tags that are also in them. */
1587 for (tree arg = FUNCTION_FIRST_USER_PARMTYPE (t); arg;
1588 arg = TREE_CHAIN (arg))
1589 cp_walk_tree_without_duplicates (&TREE_VALUE (arg),
1590 mark_abi_tags_r, &val);
1594 /* Check that T has all the ABI tags that subobject SUBOB has, or
1595 warn if not. If T is a (variable or function) declaration, also
1596 return any missing tags, and add them to T if JUST_CHECKING is false. */
1598 static tree
1599 check_abi_tags (tree t, tree subob, bool just_checking = false)
1601 bool inherit = DECL_P (t);
1603 if (!inherit && !warn_abi_tag)
1604 return NULL_TREE;
1606 tree decl = TYPE_P (t) ? TYPE_NAME (t) : t;
1607 if (!TREE_PUBLIC (decl))
1608 /* No need to worry about things local to this TU. */
1609 return NULL_TREE;
1611 mark_abi_tags (t, true);
1613 tree subtype = TYPE_P (subob) ? subob : TREE_TYPE (subob);
1614 struct abi_tag_data data = { t, subob, error_mark_node };
1615 if (inherit)
1616 data.tags = NULL_TREE;
1618 cp_walk_tree_without_duplicates (&subtype, find_abi_tags_r, &data);
1620 if (!(inherit && data.tags))
1621 /* We don't need to do anything with data.tags. */;
1622 else if (just_checking)
1623 for (tree t = data.tags; t; t = TREE_CHAIN (t))
1625 tree id = get_identifier (TREE_STRING_POINTER (TREE_VALUE (t)));
1626 IDENTIFIER_MARKED (id) = false;
1628 else
1630 tree attr = lookup_attribute ("abi_tag", DECL_ATTRIBUTES (t));
1631 if (attr)
1632 TREE_VALUE (attr) = chainon (data.tags, TREE_VALUE (attr));
1633 else
1634 DECL_ATTRIBUTES (t)
1635 = tree_cons (get_identifier ("abi_tag"), data.tags,
1636 DECL_ATTRIBUTES (t));
1639 mark_abi_tags (t, false);
1641 return data.tags;
1644 /* Check that DECL has all the ABI tags that are used in parts of its type
1645 that are not reflected in its mangled name. */
1647 void
1648 check_abi_tags (tree decl)
1650 if (VAR_P (decl))
1651 check_abi_tags (decl, TREE_TYPE (decl));
1652 else if (TREE_CODE (decl) == FUNCTION_DECL
1653 && !DECL_CONV_FN_P (decl)
1654 && !mangle_return_type_p (decl))
1655 check_abi_tags (decl, TREE_TYPE (TREE_TYPE (decl)));
1658 /* Return any ABI tags that are used in parts of the type of DECL
1659 that are not reflected in its mangled name. This function is only
1660 used in backward-compatible mangling for ABI <11. */
1662 tree
1663 missing_abi_tags (tree decl)
1665 if (VAR_P (decl))
1666 return check_abi_tags (decl, TREE_TYPE (decl), true);
1667 else if (TREE_CODE (decl) == FUNCTION_DECL
1668 /* Don't check DECL_CONV_FN_P here like we do in check_abi_tags, so
1669 that we can use this function for setting need_abi_warning
1670 regardless of the current flag_abi_version. */
1671 && !mangle_return_type_p (decl))
1672 return check_abi_tags (decl, TREE_TYPE (TREE_TYPE (decl)), true);
1673 else
1674 return NULL_TREE;
1677 void
1678 inherit_targ_abi_tags (tree t)
1680 if (!CLASS_TYPE_P (t)
1681 || CLASSTYPE_TEMPLATE_INFO (t) == NULL_TREE)
1682 return;
1684 mark_abi_tags (t, true);
1686 tree args = CLASSTYPE_TI_ARGS (t);
1687 struct abi_tag_data data = { t, NULL_TREE, NULL_TREE };
1688 for (int i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
1690 tree level = TMPL_ARGS_LEVEL (args, i+1);
1691 for (int j = 0; j < TREE_VEC_LENGTH (level); ++j)
1693 tree arg = TREE_VEC_ELT (level, j);
1694 data.subob = arg;
1695 cp_walk_tree_without_duplicates (&arg, find_abi_tags_r, &data);
1699 // If we found some tags on our template arguments, add them to our
1700 // abi_tag attribute.
1701 if (data.tags)
1703 tree attr = lookup_attribute ("abi_tag", TYPE_ATTRIBUTES (t));
1704 if (attr)
1705 TREE_VALUE (attr) = chainon (data.tags, TREE_VALUE (attr));
1706 else
1707 TYPE_ATTRIBUTES (t)
1708 = tree_cons (get_identifier ("abi_tag"), data.tags,
1709 TYPE_ATTRIBUTES (t));
1712 mark_abi_tags (t, false);
1715 /* Return true, iff class T has a non-virtual destructor that is
1716 accessible from outside the class heirarchy (i.e. is public, or
1717 there's a suitable friend. */
1719 static bool
1720 accessible_nvdtor_p (tree t)
1722 tree dtor = CLASSTYPE_DESTRUCTOR (t);
1724 /* An implicitly declared destructor is always public. And,
1725 if it were virtual, we would have created it by now. */
1726 if (!dtor)
1727 return true;
1729 if (DECL_VINDEX (dtor))
1730 return false; /* Virtual */
1732 if (!TREE_PRIVATE (dtor) && !TREE_PROTECTED (dtor))
1733 return true; /* Public */
1735 if (CLASSTYPE_FRIEND_CLASSES (t)
1736 || DECL_FRIENDLIST (TYPE_MAIN_DECL (t)))
1737 return true; /* Has friends */
1739 return false;
1742 /* Run through the base classes of T, updating CANT_HAVE_CONST_CTOR_P,
1743 and NO_CONST_ASN_REF_P. Also set flag bits in T based on
1744 properties of the bases. */
1746 static void
1747 check_bases (tree t,
1748 int* cant_have_const_ctor_p,
1749 int* no_const_asn_ref_p)
1751 int i;
1752 bool seen_non_virtual_nearly_empty_base_p = 0;
1753 int seen_tm_mask = 0;
1754 tree base_binfo;
1755 tree binfo;
1756 tree field = NULL_TREE;
1758 if (!CLASSTYPE_NON_STD_LAYOUT (t))
1759 for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
1760 if (TREE_CODE (field) == FIELD_DECL)
1761 break;
1763 for (binfo = TYPE_BINFO (t), i = 0;
1764 BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
1766 tree basetype = TREE_TYPE (base_binfo);
1768 gcc_assert (COMPLETE_TYPE_P (basetype));
1770 if (CLASSTYPE_FINAL (basetype))
1771 error ("cannot derive from %<final%> base %qT in derived type %qT",
1772 basetype, t);
1774 /* If any base class is non-literal, so is the derived class. */
1775 if (!CLASSTYPE_LITERAL_P (basetype))
1776 CLASSTYPE_LITERAL_P (t) = false;
1778 /* If the base class doesn't have copy constructors or
1779 assignment operators that take const references, then the
1780 derived class cannot have such a member automatically
1781 generated. */
1782 if (TYPE_HAS_COPY_CTOR (basetype)
1783 && ! TYPE_HAS_CONST_COPY_CTOR (basetype))
1784 *cant_have_const_ctor_p = 1;
1785 if (TYPE_HAS_COPY_ASSIGN (basetype)
1786 && !TYPE_HAS_CONST_COPY_ASSIGN (basetype))
1787 *no_const_asn_ref_p = 1;
1789 if (BINFO_VIRTUAL_P (base_binfo))
1790 /* A virtual base does not effect nearly emptiness. */
1792 else if (CLASSTYPE_NEARLY_EMPTY_P (basetype))
1794 if (seen_non_virtual_nearly_empty_base_p)
1795 /* And if there is more than one nearly empty base, then the
1796 derived class is not nearly empty either. */
1797 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
1798 else
1799 /* Remember we've seen one. */
1800 seen_non_virtual_nearly_empty_base_p = 1;
1802 else if (!is_empty_class (basetype))
1803 /* If the base class is not empty or nearly empty, then this
1804 class cannot be nearly empty. */
1805 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
1807 /* A lot of properties from the bases also apply to the derived
1808 class. */
1809 TYPE_NEEDS_CONSTRUCTING (t) |= TYPE_NEEDS_CONSTRUCTING (basetype);
1810 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t)
1811 |= TYPE_HAS_NONTRIVIAL_DESTRUCTOR (basetype);
1812 TYPE_HAS_COMPLEX_COPY_ASSIGN (t)
1813 |= (TYPE_HAS_COMPLEX_COPY_ASSIGN (basetype)
1814 || !TYPE_HAS_COPY_ASSIGN (basetype));
1815 TYPE_HAS_COMPLEX_COPY_CTOR (t) |= (TYPE_HAS_COMPLEX_COPY_CTOR (basetype)
1816 || !TYPE_HAS_COPY_CTOR (basetype));
1817 TYPE_HAS_COMPLEX_MOVE_ASSIGN (t)
1818 |= TYPE_HAS_COMPLEX_MOVE_ASSIGN (basetype);
1819 TYPE_HAS_COMPLEX_MOVE_CTOR (t) |= TYPE_HAS_COMPLEX_MOVE_CTOR (basetype);
1820 TYPE_POLYMORPHIC_P (t) |= TYPE_POLYMORPHIC_P (basetype);
1821 CLASSTYPE_CONTAINS_EMPTY_CLASS_P (t)
1822 |= CLASSTYPE_CONTAINS_EMPTY_CLASS_P (basetype);
1823 TYPE_HAS_COMPLEX_DFLT (t) |= (!TYPE_HAS_DEFAULT_CONSTRUCTOR (basetype)
1824 || TYPE_HAS_COMPLEX_DFLT (basetype));
1825 SET_CLASSTYPE_READONLY_FIELDS_NEED_INIT
1826 (t, CLASSTYPE_READONLY_FIELDS_NEED_INIT (t)
1827 | CLASSTYPE_READONLY_FIELDS_NEED_INIT (basetype));
1828 SET_CLASSTYPE_REF_FIELDS_NEED_INIT
1829 (t, CLASSTYPE_REF_FIELDS_NEED_INIT (t)
1830 | CLASSTYPE_REF_FIELDS_NEED_INIT (basetype));
1831 if (TYPE_HAS_MUTABLE_P (basetype))
1832 CLASSTYPE_HAS_MUTABLE (t) = 1;
1834 /* A standard-layout class is a class that:
1836 * has no non-standard-layout base classes, */
1837 CLASSTYPE_NON_STD_LAYOUT (t) |= CLASSTYPE_NON_STD_LAYOUT (basetype);
1838 if (!CLASSTYPE_NON_STD_LAYOUT (t))
1840 tree basefield;
1841 /* ...has no base classes of the same type as the first non-static
1842 data member... */
1843 if (field && DECL_CONTEXT (field) == t
1844 && (same_type_ignoring_top_level_qualifiers_p
1845 (TREE_TYPE (field), basetype)))
1846 CLASSTYPE_NON_STD_LAYOUT (t) = 1;
1847 else
1848 /* ...either has no non-static data members in the most-derived
1849 class and at most one base class with non-static data
1850 members, or has no base classes with non-static data
1851 members */
1852 for (basefield = TYPE_FIELDS (basetype); basefield;
1853 basefield = DECL_CHAIN (basefield))
1854 if (TREE_CODE (basefield) == FIELD_DECL
1855 && !(DECL_FIELD_IS_BASE (basefield)
1856 && integer_zerop (DECL_SIZE (basefield))))
1858 if (field)
1859 CLASSTYPE_NON_STD_LAYOUT (t) = 1;
1860 else
1861 field = basefield;
1862 break;
1866 /* Don't bother collecting tm attributes if transactional memory
1867 support is not enabled. */
1868 if (flag_tm)
1870 tree tm_attr = find_tm_attribute (TYPE_ATTRIBUTES (basetype));
1871 if (tm_attr)
1872 seen_tm_mask |= tm_attr_to_mask (tm_attr);
1875 check_abi_tags (t, basetype);
1878 /* If one of the base classes had TM attributes, and the current class
1879 doesn't define its own, then the current class inherits one. */
1880 if (seen_tm_mask && !find_tm_attribute (TYPE_ATTRIBUTES (t)))
1882 tree tm_attr = tm_mask_to_attr (least_bit_hwi (seen_tm_mask));
1883 TYPE_ATTRIBUTES (t) = tree_cons (tm_attr, NULL, TYPE_ATTRIBUTES (t));
1887 /* Determine all the primary bases within T. Sets BINFO_PRIMARY_BASE_P for
1888 those that are primaries. Sets BINFO_LOST_PRIMARY_P for those
1889 that have had a nearly-empty virtual primary base stolen by some
1890 other base in the hierarchy. Determines CLASSTYPE_PRIMARY_BASE for
1891 T. */
1893 static void
1894 determine_primary_bases (tree t)
1896 unsigned i;
1897 tree primary = NULL_TREE;
1898 tree type_binfo = TYPE_BINFO (t);
1899 tree base_binfo;
1901 /* Determine the primary bases of our bases. */
1902 for (base_binfo = TREE_CHAIN (type_binfo); base_binfo;
1903 base_binfo = TREE_CHAIN (base_binfo))
1905 tree primary = CLASSTYPE_PRIMARY_BINFO (BINFO_TYPE (base_binfo));
1907 /* See if we're the non-virtual primary of our inheritance
1908 chain. */
1909 if (!BINFO_VIRTUAL_P (base_binfo))
1911 tree parent = BINFO_INHERITANCE_CHAIN (base_binfo);
1912 tree parent_primary = CLASSTYPE_PRIMARY_BINFO (BINFO_TYPE (parent));
1914 if (parent_primary
1915 && SAME_BINFO_TYPE_P (BINFO_TYPE (base_binfo),
1916 BINFO_TYPE (parent_primary)))
1917 /* We are the primary binfo. */
1918 BINFO_PRIMARY_P (base_binfo) = 1;
1920 /* Determine if we have a virtual primary base, and mark it so.
1922 if (primary && BINFO_VIRTUAL_P (primary))
1924 tree this_primary = copied_binfo (primary, base_binfo);
1926 if (BINFO_PRIMARY_P (this_primary))
1927 /* Someone already claimed this base. */
1928 BINFO_LOST_PRIMARY_P (base_binfo) = 1;
1929 else
1931 tree delta;
1933 BINFO_PRIMARY_P (this_primary) = 1;
1934 BINFO_INHERITANCE_CHAIN (this_primary) = base_binfo;
1936 /* A virtual binfo might have been copied from within
1937 another hierarchy. As we're about to use it as a
1938 primary base, make sure the offsets match. */
1939 delta = size_diffop_loc (input_location,
1940 fold_convert (ssizetype,
1941 BINFO_OFFSET (base_binfo)),
1942 fold_convert (ssizetype,
1943 BINFO_OFFSET (this_primary)));
1945 propagate_binfo_offsets (this_primary, delta);
1950 /* First look for a dynamic direct non-virtual base. */
1951 for (i = 0; BINFO_BASE_ITERATE (type_binfo, i, base_binfo); i++)
1953 tree basetype = BINFO_TYPE (base_binfo);
1955 if (TYPE_CONTAINS_VPTR_P (basetype) && !BINFO_VIRTUAL_P (base_binfo))
1957 primary = base_binfo;
1958 goto found;
1962 /* A "nearly-empty" virtual base class can be the primary base
1963 class, if no non-virtual polymorphic base can be found. Look for
1964 a nearly-empty virtual dynamic base that is not already a primary
1965 base of something in the hierarchy. If there is no such base,
1966 just pick the first nearly-empty virtual base. */
1968 for (base_binfo = TREE_CHAIN (type_binfo); base_binfo;
1969 base_binfo = TREE_CHAIN (base_binfo))
1970 if (BINFO_VIRTUAL_P (base_binfo)
1971 && CLASSTYPE_NEARLY_EMPTY_P (BINFO_TYPE (base_binfo)))
1973 if (!BINFO_PRIMARY_P (base_binfo))
1975 /* Found one that is not primary. */
1976 primary = base_binfo;
1977 goto found;
1979 else if (!primary)
1980 /* Remember the first candidate. */
1981 primary = base_binfo;
1984 found:
1985 /* If we've got a primary base, use it. */
1986 if (primary)
1988 tree basetype = BINFO_TYPE (primary);
1990 CLASSTYPE_PRIMARY_BINFO (t) = primary;
1991 if (BINFO_PRIMARY_P (primary))
1992 /* We are stealing a primary base. */
1993 BINFO_LOST_PRIMARY_P (BINFO_INHERITANCE_CHAIN (primary)) = 1;
1994 BINFO_PRIMARY_P (primary) = 1;
1995 if (BINFO_VIRTUAL_P (primary))
1997 tree delta;
1999 BINFO_INHERITANCE_CHAIN (primary) = type_binfo;
2000 /* A virtual binfo might have been copied from within
2001 another hierarchy. As we're about to use it as a primary
2002 base, make sure the offsets match. */
2003 delta = size_diffop_loc (input_location, ssize_int (0),
2004 fold_convert (ssizetype, BINFO_OFFSET (primary)));
2006 propagate_binfo_offsets (primary, delta);
2009 primary = TYPE_BINFO (basetype);
2011 TYPE_VFIELD (t) = TYPE_VFIELD (basetype);
2012 BINFO_VTABLE (type_binfo) = BINFO_VTABLE (primary);
2013 BINFO_VIRTUALS (type_binfo) = BINFO_VIRTUALS (primary);
2017 /* Update the variant types of T. */
2019 void
2020 fixup_type_variants (tree t)
2022 tree variants;
2024 if (!t)
2025 return;
2027 for (variants = TYPE_NEXT_VARIANT (t);
2028 variants;
2029 variants = TYPE_NEXT_VARIANT (variants))
2031 /* These fields are in the _TYPE part of the node, not in
2032 the TYPE_LANG_SPECIFIC component, so they are not shared. */
2033 TYPE_HAS_USER_CONSTRUCTOR (variants) = TYPE_HAS_USER_CONSTRUCTOR (t);
2034 TYPE_NEEDS_CONSTRUCTING (variants) = TYPE_NEEDS_CONSTRUCTING (t);
2035 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (variants)
2036 = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t);
2038 TYPE_POLYMORPHIC_P (variants) = TYPE_POLYMORPHIC_P (t);
2040 TYPE_BINFO (variants) = TYPE_BINFO (t);
2042 /* Copy whatever these are holding today. */
2043 TYPE_VFIELD (variants) = TYPE_VFIELD (t);
2044 TYPE_FIELDS (variants) = TYPE_FIELDS (t);
2048 /* KLASS is a class that we're applying may_alias to after the body is
2049 parsed. Fixup any POINTER_TO and REFERENCE_TO types. The
2050 canonical type(s) will be implicitly updated. */
2052 static void
2053 fixup_may_alias (tree klass)
2055 tree t, v;
2057 for (t = TYPE_POINTER_TO (klass); t; t = TYPE_NEXT_PTR_TO (t))
2058 for (v = TYPE_MAIN_VARIANT (t); v; v = TYPE_NEXT_VARIANT (v))
2059 TYPE_REF_CAN_ALIAS_ALL (v) = true;
2060 for (t = TYPE_REFERENCE_TO (klass); t; t = TYPE_NEXT_REF_TO (t))
2061 for (v = TYPE_MAIN_VARIANT (t); v; v = TYPE_NEXT_VARIANT (v))
2062 TYPE_REF_CAN_ALIAS_ALL (v) = true;
2065 /* Early variant fixups: we apply attributes at the beginning of the class
2066 definition, and we need to fix up any variants that have already been
2067 made via elaborated-type-specifier so that check_qualified_type works. */
2069 void
2070 fixup_attribute_variants (tree t)
2072 tree variants;
2074 if (!t)
2075 return;
2077 tree attrs = TYPE_ATTRIBUTES (t);
2078 unsigned align = TYPE_ALIGN (t);
2079 bool user_align = TYPE_USER_ALIGN (t);
2080 bool may_alias = lookup_attribute ("may_alias", attrs);
2082 if (may_alias)
2083 fixup_may_alias (t);
2085 for (variants = TYPE_NEXT_VARIANT (t);
2086 variants;
2087 variants = TYPE_NEXT_VARIANT (variants))
2089 /* These are the two fields that check_qualified_type looks at and
2090 are affected by attributes. */
2091 TYPE_ATTRIBUTES (variants) = attrs;
2092 unsigned valign = align;
2093 if (TYPE_USER_ALIGN (variants))
2094 valign = MAX (valign, TYPE_ALIGN (variants));
2095 else
2096 TYPE_USER_ALIGN (variants) = user_align;
2097 SET_TYPE_ALIGN (variants, valign);
2098 if (may_alias)
2099 fixup_may_alias (variants);
2103 /* Set memoizing fields and bits of T (and its variants) for later
2104 use. */
2106 static void
2107 finish_struct_bits (tree t)
2109 /* Fix up variants (if any). */
2110 fixup_type_variants (t);
2112 if (BINFO_N_BASE_BINFOS (TYPE_BINFO (t)) && TYPE_POLYMORPHIC_P (t))
2113 /* For a class w/o baseclasses, 'finish_struct' has set
2114 CLASSTYPE_PURE_VIRTUALS correctly (by definition).
2115 Similarly for a class whose base classes do not have vtables.
2116 When neither of these is true, we might have removed abstract
2117 virtuals (by providing a definition), added some (by declaring
2118 new ones), or redeclared ones from a base class. We need to
2119 recalculate what's really an abstract virtual at this point (by
2120 looking in the vtables). */
2121 get_pure_virtuals (t);
2123 /* If this type has a copy constructor or a destructor, force its
2124 mode to be BLKmode, and force its TREE_ADDRESSABLE bit to be
2125 nonzero. This will cause it to be passed by invisible reference
2126 and prevent it from being returned in a register. */
2127 if (type_has_nontrivial_copy_init (t)
2128 || TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t))
2130 tree variants;
2131 SET_DECL_MODE (TYPE_MAIN_DECL (t), BLKmode);
2132 for (variants = t; variants; variants = TYPE_NEXT_VARIANT (variants))
2134 SET_TYPE_MODE (variants, BLKmode);
2135 TREE_ADDRESSABLE (variants) = 1;
2140 /* Issue warnings about T having private constructors, but no friends,
2141 and so forth.
2143 HAS_NONPRIVATE_METHOD is nonzero if T has any non-private methods or
2144 static members. HAS_NONPRIVATE_STATIC_FN is nonzero if T has any
2145 non-private static member functions. */
2147 static void
2148 maybe_warn_about_overly_private_class (tree t)
2150 int has_member_fn = 0;
2151 int has_nonprivate_method = 0;
2152 tree fn;
2154 if (!warn_ctor_dtor_privacy
2155 /* If the class has friends, those entities might create and
2156 access instances, so we should not warn. */
2157 || (CLASSTYPE_FRIEND_CLASSES (t)
2158 || DECL_FRIENDLIST (TYPE_MAIN_DECL (t)))
2159 /* We will have warned when the template was declared; there's
2160 no need to warn on every instantiation. */
2161 || CLASSTYPE_TEMPLATE_INSTANTIATION (t))
2162 /* There's no reason to even consider warning about this
2163 class. */
2164 return;
2166 /* We only issue one warning, if more than one applies, because
2167 otherwise, on code like:
2169 class A {
2170 // Oops - forgot `public:'
2171 A();
2172 A(const A&);
2173 ~A();
2176 we warn several times about essentially the same problem. */
2178 /* Check to see if all (non-constructor, non-destructor) member
2179 functions are private. (Since there are no friends or
2180 non-private statics, we can't ever call any of the private member
2181 functions.) */
2182 for (fn = TYPE_METHODS (t); fn; fn = DECL_CHAIN (fn))
2183 /* We're not interested in compiler-generated methods; they don't
2184 provide any way to call private members. */
2185 if (!DECL_ARTIFICIAL (fn))
2187 if (!TREE_PRIVATE (fn))
2189 if (DECL_STATIC_FUNCTION_P (fn))
2190 /* A non-private static member function is just like a
2191 friend; it can create and invoke private member
2192 functions, and be accessed without a class
2193 instance. */
2194 return;
2196 has_nonprivate_method = 1;
2197 /* Keep searching for a static member function. */
2199 else if (!DECL_CONSTRUCTOR_P (fn) && !DECL_DESTRUCTOR_P (fn))
2200 has_member_fn = 1;
2203 if (!has_nonprivate_method && has_member_fn)
2205 /* There are no non-private methods, and there's at least one
2206 private member function that isn't a constructor or
2207 destructor. (If all the private members are
2208 constructors/destructors we want to use the code below that
2209 issues error messages specifically referring to
2210 constructors/destructors.) */
2211 unsigned i;
2212 tree binfo = TYPE_BINFO (t);
2214 for (i = 0; i != BINFO_N_BASE_BINFOS (binfo); i++)
2215 if (BINFO_BASE_ACCESS (binfo, i) != access_private_node)
2217 has_nonprivate_method = 1;
2218 break;
2220 if (!has_nonprivate_method)
2222 warning (OPT_Wctor_dtor_privacy,
2223 "all member functions in class %qT are private", t);
2224 return;
2228 /* Even if some of the member functions are non-private, the class
2229 won't be useful for much if all the constructors or destructors
2230 are private: such an object can never be created or destroyed. */
2231 fn = CLASSTYPE_DESTRUCTOR (t);
2232 if (fn && TREE_PRIVATE (fn))
2234 warning (OPT_Wctor_dtor_privacy,
2235 "%q#T only defines a private destructor and has no friends",
2237 return;
2240 /* Warn about classes that have private constructors and no friends. */
2241 if (TYPE_HAS_USER_CONSTRUCTOR (t)
2242 /* Implicitly generated constructors are always public. */
2243 && !CLASSTYPE_LAZY_DEFAULT_CTOR (t))
2245 bool nonprivate_ctor = false;
2246 tree copy_or_move = NULL_TREE;
2248 /* If a non-template class does not define a copy
2249 constructor, one is defined for it, enabling it to avoid
2250 this warning. For a template class, this does not
2251 happen, and so we would normally get a warning on:
2253 template <class T> class C { private: C(); };
2255 To avoid this asymmetry, we check TYPE_HAS_COPY_CTOR. All
2256 complete non-template or fully instantiated classes have this
2257 flag set. */
2258 if (!TYPE_HAS_COPY_CTOR (t))
2259 nonprivate_ctor = true;
2260 else
2261 for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (t));
2262 !nonprivate_ctor && iter; ++iter)
2263 if (TREE_PRIVATE (*iter))
2264 continue;
2265 else if (copy_fn_p (*iter) || move_fn_p (*iter))
2266 /* Ideally, we wouldn't count any constructor that takes
2267 an argument of the class type as a parameter, because
2268 such things cannot be used to construct an instance of
2269 the class unless you already have one. */
2270 copy_or_move = *iter;
2271 else
2272 nonprivate_ctor = true;
2274 if (!nonprivate_ctor)
2276 warning (OPT_Wctor_dtor_privacy,
2277 "%q#T only defines private constructors and has no friends",
2279 if (copy_or_move)
2280 inform (DECL_SOURCE_LOCATION (copy_or_move),
2281 "%q#D is public, but requires an existing %q#T object",
2282 copy_or_move, t);
2283 return;
2288 static struct {
2289 gt_pointer_operator new_value;
2290 void *cookie;
2291 } resort_data;
2293 /* Comparison function to compare two TYPE_METHOD_VEC entries by name. */
2295 static int
2296 method_name_cmp (const void* m1_p, const void* m2_p)
2298 const tree *const m1 = (const tree *) m1_p;
2299 const tree *const m2 = (const tree *) m2_p;
2301 if (*m1 == NULL_TREE && *m2 == NULL_TREE)
2302 return 0;
2303 if (*m1 == NULL_TREE)
2304 return -1;
2305 if (*m2 == NULL_TREE)
2306 return 1;
2307 if (OVL_NAME (*m1) < OVL_NAME (*m2))
2308 return -1;
2309 return 1;
2312 /* This routine compares two fields like method_name_cmp but using the
2313 pointer operator in resort_field_decl_data. */
2315 static int
2316 resort_method_name_cmp (const void* m1_p, const void* m2_p)
2318 const tree *const m1 = (const tree *) m1_p;
2319 const tree *const m2 = (const tree *) m2_p;
2320 if (*m1 == NULL_TREE && *m2 == NULL_TREE)
2321 return 0;
2322 if (*m1 == NULL_TREE)
2323 return -1;
2324 if (*m2 == NULL_TREE)
2325 return 1;
2327 tree d1 = OVL_NAME (*m1);
2328 tree d2 = OVL_NAME (*m2);
2329 resort_data.new_value (&d1, resort_data.cookie);
2330 resort_data.new_value (&d2, resort_data.cookie);
2331 if (d1 < d2)
2332 return -1;
2334 return 1;
2337 /* Resort TYPE_METHOD_VEC because pointers have been reordered. */
2339 void
2340 resort_type_method_vec (void* obj,
2341 void* /*orig_obj*/,
2342 gt_pointer_operator new_value,
2343 void* cookie)
2345 if (vec<tree, va_gc> *method_vec = (vec<tree, va_gc> *) obj)
2347 int len = method_vec->length ();
2348 int slot;
2350 /* The type conversion ops have to live at the front of the vec, so we
2351 can't sort them. */
2352 for (slot = CLASSTYPE_FIRST_CONVERSION_SLOT;
2353 slot < len; slot++)
2354 if (!DECL_CONV_FN_P (OVL_FIRST ((*method_vec)[slot])))
2355 break;
2357 if (len > slot + 1)
2359 resort_data.new_value = new_value;
2360 resort_data.cookie = cookie;
2361 qsort (method_vec->address () + slot, len - slot, sizeof (tree),
2362 resort_method_name_cmp);
2367 /* Warn about duplicate methods in fn_fields.
2369 Sort methods that are not special (i.e., constructors, destructors,
2370 and type conversion operators) so that we can find them faster in
2371 search. */
2373 static void
2374 finish_struct_methods (tree t)
2376 tree fn_fields;
2377 vec<tree, va_gc> *method_vec;
2378 int slot, len;
2380 method_vec = CLASSTYPE_METHOD_VEC (t);
2381 if (!method_vec)
2382 return;
2384 len = method_vec->length ();
2386 /* Clear DECL_IN_AGGR_P for all functions. */
2387 for (fn_fields = TYPE_METHODS (t); fn_fields;
2388 fn_fields = DECL_CHAIN (fn_fields))
2389 DECL_IN_AGGR_P (fn_fields) = 0;
2391 /* Issue warnings about private constructors and such. If there are
2392 no methods, then some public defaults are generated. */
2393 maybe_warn_about_overly_private_class (t);
2395 /* The type conversion ops have to live at the front of the vec, so we
2396 can't sort them. */
2397 for (slot = CLASSTYPE_FIRST_CONVERSION_SLOT;
2398 method_vec->iterate (slot, &fn_fields);
2399 ++slot)
2400 if (!DECL_CONV_FN_P (OVL_FIRST (fn_fields)))
2401 break;
2402 if (len - slot > 1)
2403 qsort (method_vec->address () + slot,
2404 len-slot, sizeof (tree), method_name_cmp);
2407 /* Make BINFO's vtable have N entries, including RTTI entries,
2408 vbase and vcall offsets, etc. Set its type and call the back end
2409 to lay it out. */
2411 static void
2412 layout_vtable_decl (tree binfo, int n)
2414 tree atype;
2415 tree vtable;
2417 atype = build_array_of_n_type (vtable_entry_type, n);
2418 layout_type (atype);
2420 /* We may have to grow the vtable. */
2421 vtable = get_vtbl_decl_for_binfo (binfo);
2422 if (!same_type_p (TREE_TYPE (vtable), atype))
2424 TREE_TYPE (vtable) = atype;
2425 DECL_SIZE (vtable) = DECL_SIZE_UNIT (vtable) = NULL_TREE;
2426 layout_decl (vtable, 0);
2430 /* True iff FNDECL and BASE_FNDECL (both non-static member functions)
2431 have the same signature. */
2434 same_signature_p (const_tree fndecl, const_tree base_fndecl)
2436 /* One destructor overrides another if they are the same kind of
2437 destructor. */
2438 if (DECL_DESTRUCTOR_P (base_fndecl) && DECL_DESTRUCTOR_P (fndecl)
2439 && special_function_p (base_fndecl) == special_function_p (fndecl))
2440 return 1;
2441 /* But a non-destructor never overrides a destructor, nor vice
2442 versa, nor do different kinds of destructors override
2443 one-another. For example, a complete object destructor does not
2444 override a deleting destructor. */
2445 if (DECL_DESTRUCTOR_P (base_fndecl) || DECL_DESTRUCTOR_P (fndecl))
2446 return 0;
2448 if (DECL_NAME (fndecl) == DECL_NAME (base_fndecl)
2449 || (DECL_CONV_FN_P (fndecl)
2450 && DECL_CONV_FN_P (base_fndecl)
2451 && same_type_p (DECL_CONV_FN_TYPE (fndecl),
2452 DECL_CONV_FN_TYPE (base_fndecl))))
2454 tree fntype = TREE_TYPE (fndecl);
2455 tree base_fntype = TREE_TYPE (base_fndecl);
2456 if (type_memfn_quals (fntype) == type_memfn_quals (base_fntype)
2457 && type_memfn_rqual (fntype) == type_memfn_rqual (base_fntype)
2458 && compparms (FUNCTION_FIRST_USER_PARMTYPE (fndecl),
2459 FUNCTION_FIRST_USER_PARMTYPE (base_fndecl)))
2460 return 1;
2462 return 0;
2465 /* Returns TRUE if DERIVED is a binfo containing the binfo BASE as a
2466 subobject. */
2468 static bool
2469 base_derived_from (tree derived, tree base)
2471 tree probe;
2473 for (probe = base; probe; probe = BINFO_INHERITANCE_CHAIN (probe))
2475 if (probe == derived)
2476 return true;
2477 else if (BINFO_VIRTUAL_P (probe))
2478 /* If we meet a virtual base, we can't follow the inheritance
2479 any more. See if the complete type of DERIVED contains
2480 such a virtual base. */
2481 return (binfo_for_vbase (BINFO_TYPE (probe), BINFO_TYPE (derived))
2482 != NULL_TREE);
2484 return false;
2487 struct find_final_overrider_data {
2488 /* The function for which we are trying to find a final overrider. */
2489 tree fn;
2490 /* The base class in which the function was declared. */
2491 tree declaring_base;
2492 /* The candidate overriders. */
2493 tree candidates;
2494 /* Path to most derived. */
2495 vec<tree> path;
2498 /* Add the overrider along the current path to FFOD->CANDIDATES.
2499 Returns true if an overrider was found; false otherwise. */
2501 static bool
2502 dfs_find_final_overrider_1 (tree binfo,
2503 find_final_overrider_data *ffod,
2504 unsigned depth)
2506 tree method;
2508 /* If BINFO is not the most derived type, try a more derived class.
2509 A definition there will overrider a definition here. */
2510 if (depth)
2512 depth--;
2513 if (dfs_find_final_overrider_1
2514 (ffod->path[depth], ffod, depth))
2515 return true;
2518 method = look_for_overrides_here (BINFO_TYPE (binfo), ffod->fn);
2519 if (method)
2521 tree *candidate = &ffod->candidates;
2523 /* Remove any candidates overridden by this new function. */
2524 while (*candidate)
2526 /* If *CANDIDATE overrides METHOD, then METHOD
2527 cannot override anything else on the list. */
2528 if (base_derived_from (TREE_VALUE (*candidate), binfo))
2529 return true;
2530 /* If METHOD overrides *CANDIDATE, remove *CANDIDATE. */
2531 if (base_derived_from (binfo, TREE_VALUE (*candidate)))
2532 *candidate = TREE_CHAIN (*candidate);
2533 else
2534 candidate = &TREE_CHAIN (*candidate);
2537 /* Add the new function. */
2538 ffod->candidates = tree_cons (method, binfo, ffod->candidates);
2539 return true;
2542 return false;
2545 /* Called from find_final_overrider via dfs_walk. */
2547 static tree
2548 dfs_find_final_overrider_pre (tree binfo, void *data)
2550 find_final_overrider_data *ffod = (find_final_overrider_data *) data;
2552 if (binfo == ffod->declaring_base)
2553 dfs_find_final_overrider_1 (binfo, ffod, ffod->path.length ());
2554 ffod->path.safe_push (binfo);
2556 return NULL_TREE;
2559 static tree
2560 dfs_find_final_overrider_post (tree /*binfo*/, void *data)
2562 find_final_overrider_data *ffod = (find_final_overrider_data *) data;
2563 ffod->path.pop ();
2565 return NULL_TREE;
2568 /* Returns a TREE_LIST whose TREE_PURPOSE is the final overrider for
2569 FN and whose TREE_VALUE is the binfo for the base where the
2570 overriding occurs. BINFO (in the hierarchy dominated by the binfo
2571 DERIVED) is the base object in which FN is declared. */
2573 static tree
2574 find_final_overrider (tree derived, tree binfo, tree fn)
2576 find_final_overrider_data ffod;
2578 /* Getting this right is a little tricky. This is valid:
2580 struct S { virtual void f (); };
2581 struct T { virtual void f (); };
2582 struct U : public S, public T { };
2584 even though calling `f' in `U' is ambiguous. But,
2586 struct R { virtual void f(); };
2587 struct S : virtual public R { virtual void f (); };
2588 struct T : virtual public R { virtual void f (); };
2589 struct U : public S, public T { };
2591 is not -- there's no way to decide whether to put `S::f' or
2592 `T::f' in the vtable for `R'.
2594 The solution is to look at all paths to BINFO. If we find
2595 different overriders along any two, then there is a problem. */
2596 if (DECL_THUNK_P (fn))
2597 fn = THUNK_TARGET (fn);
2599 /* Determine the depth of the hierarchy. */
2600 ffod.fn = fn;
2601 ffod.declaring_base = binfo;
2602 ffod.candidates = NULL_TREE;
2603 ffod.path.create (30);
2605 dfs_walk_all (derived, dfs_find_final_overrider_pre,
2606 dfs_find_final_overrider_post, &ffod);
2608 ffod.path.release ();
2610 /* If there was no winner, issue an error message. */
2611 if (!ffod.candidates || TREE_CHAIN (ffod.candidates))
2612 return error_mark_node;
2614 return ffod.candidates;
2617 /* Return the index of the vcall offset for FN when TYPE is used as a
2618 virtual base. */
2620 static tree
2621 get_vcall_index (tree fn, tree type)
2623 vec<tree_pair_s, va_gc> *indices = CLASSTYPE_VCALL_INDICES (type);
2624 tree_pair_p p;
2625 unsigned ix;
2627 FOR_EACH_VEC_SAFE_ELT (indices, ix, p)
2628 if ((DECL_DESTRUCTOR_P (fn) && DECL_DESTRUCTOR_P (p->purpose))
2629 || same_signature_p (fn, p->purpose))
2630 return p->value;
2632 /* There should always be an appropriate index. */
2633 gcc_unreachable ();
2636 /* Update an entry in the vtable for BINFO, which is in the hierarchy
2637 dominated by T. FN is the old function; VIRTUALS points to the
2638 corresponding position in the new BINFO_VIRTUALS list. IX is the index
2639 of that entry in the list. */
2641 static void
2642 update_vtable_entry_for_fn (tree t, tree binfo, tree fn, tree* virtuals,
2643 unsigned ix)
2645 tree b;
2646 tree overrider;
2647 tree delta;
2648 tree virtual_base;
2649 tree first_defn;
2650 tree overrider_fn, overrider_target;
2651 tree target_fn = DECL_THUNK_P (fn) ? THUNK_TARGET (fn) : fn;
2652 tree over_return, base_return;
2653 bool lost = false;
2655 /* Find the nearest primary base (possibly binfo itself) which defines
2656 this function; this is the class the caller will convert to when
2657 calling FN through BINFO. */
2658 for (b = binfo; ; b = get_primary_binfo (b))
2660 gcc_assert (b);
2661 if (look_for_overrides_here (BINFO_TYPE (b), target_fn))
2662 break;
2664 /* The nearest definition is from a lost primary. */
2665 if (BINFO_LOST_PRIMARY_P (b))
2666 lost = true;
2668 first_defn = b;
2670 /* Find the final overrider. */
2671 overrider = find_final_overrider (TYPE_BINFO (t), b, target_fn);
2672 if (overrider == error_mark_node)
2674 error ("no unique final overrider for %qD in %qT", target_fn, t);
2675 return;
2677 overrider_target = overrider_fn = TREE_PURPOSE (overrider);
2679 /* Check for adjusting covariant return types. */
2680 over_return = TREE_TYPE (TREE_TYPE (overrider_target));
2681 base_return = TREE_TYPE (TREE_TYPE (target_fn));
2683 if (POINTER_TYPE_P (over_return)
2684 && TREE_CODE (over_return) == TREE_CODE (base_return)
2685 && CLASS_TYPE_P (TREE_TYPE (over_return))
2686 && CLASS_TYPE_P (TREE_TYPE (base_return))
2687 /* If the overrider is invalid, don't even try. */
2688 && !DECL_INVALID_OVERRIDER_P (overrider_target))
2690 /* If FN is a covariant thunk, we must figure out the adjustment
2691 to the final base FN was converting to. As OVERRIDER_TARGET might
2692 also be converting to the return type of FN, we have to
2693 combine the two conversions here. */
2694 tree fixed_offset, virtual_offset;
2696 over_return = TREE_TYPE (over_return);
2697 base_return = TREE_TYPE (base_return);
2699 if (DECL_THUNK_P (fn))
2701 gcc_assert (DECL_RESULT_THUNK_P (fn));
2702 fixed_offset = ssize_int (THUNK_FIXED_OFFSET (fn));
2703 virtual_offset = THUNK_VIRTUAL_OFFSET (fn);
2705 else
2706 fixed_offset = virtual_offset = NULL_TREE;
2708 if (virtual_offset)
2709 /* Find the equivalent binfo within the return type of the
2710 overriding function. We will want the vbase offset from
2711 there. */
2712 virtual_offset = binfo_for_vbase (BINFO_TYPE (virtual_offset),
2713 over_return);
2714 else if (!same_type_ignoring_top_level_qualifiers_p
2715 (over_return, base_return))
2717 /* There was no existing virtual thunk (which takes
2718 precedence). So find the binfo of the base function's
2719 return type within the overriding function's return type.
2720 Fortunately we know the covariancy is valid (it
2721 has already been checked), so we can just iterate along
2722 the binfos, which have been chained in inheritance graph
2723 order. Of course it is lame that we have to repeat the
2724 search here anyway -- we should really be caching pieces
2725 of the vtable and avoiding this repeated work. */
2726 tree thunk_binfo, base_binfo;
2728 /* Find the base binfo within the overriding function's
2729 return type. We will always find a thunk_binfo, except
2730 when the covariancy is invalid (which we will have
2731 already diagnosed). */
2732 for (base_binfo = TYPE_BINFO (base_return),
2733 thunk_binfo = TYPE_BINFO (over_return);
2734 thunk_binfo;
2735 thunk_binfo = TREE_CHAIN (thunk_binfo))
2736 if (SAME_BINFO_TYPE_P (BINFO_TYPE (thunk_binfo),
2737 BINFO_TYPE (base_binfo)))
2738 break;
2740 /* See if virtual inheritance is involved. */
2741 for (virtual_offset = thunk_binfo;
2742 virtual_offset;
2743 virtual_offset = BINFO_INHERITANCE_CHAIN (virtual_offset))
2744 if (BINFO_VIRTUAL_P (virtual_offset))
2745 break;
2747 if (virtual_offset
2748 || (thunk_binfo && !BINFO_OFFSET_ZEROP (thunk_binfo)))
2750 tree offset = fold_convert (ssizetype, BINFO_OFFSET (thunk_binfo));
2752 if (virtual_offset)
2754 /* We convert via virtual base. Adjust the fixed
2755 offset to be from there. */
2756 offset =
2757 size_diffop (offset,
2758 fold_convert (ssizetype,
2759 BINFO_OFFSET (virtual_offset)));
2761 if (fixed_offset)
2762 /* There was an existing fixed offset, this must be
2763 from the base just converted to, and the base the
2764 FN was thunking to. */
2765 fixed_offset = size_binop (PLUS_EXPR, fixed_offset, offset);
2766 else
2767 fixed_offset = offset;
2771 if (fixed_offset || virtual_offset)
2772 /* Replace the overriding function with a covariant thunk. We
2773 will emit the overriding function in its own slot as
2774 well. */
2775 overrider_fn = make_thunk (overrider_target, /*this_adjusting=*/0,
2776 fixed_offset, virtual_offset);
2778 else
2779 gcc_assert (DECL_INVALID_OVERRIDER_P (overrider_target) ||
2780 !DECL_THUNK_P (fn));
2782 /* If we need a covariant thunk, then we may need to adjust first_defn.
2783 The ABI specifies that the thunks emitted with a function are
2784 determined by which bases the function overrides, so we need to be
2785 sure that we're using a thunk for some overridden base; even if we
2786 know that the necessary this adjustment is zero, there may not be an
2787 appropriate zero-this-adjustment thunk for us to use since thunks for
2788 overriding virtual bases always use the vcall offset.
2790 Furthermore, just choosing any base that overrides this function isn't
2791 quite right, as this slot won't be used for calls through a type that
2792 puts a covariant thunk here. Calling the function through such a type
2793 will use a different slot, and that slot is the one that determines
2794 the thunk emitted for that base.
2796 So, keep looking until we find the base that we're really overriding
2797 in this slot: the nearest primary base that doesn't use a covariant
2798 thunk in this slot. */
2799 if (overrider_target != overrider_fn)
2801 if (BINFO_TYPE (b) == DECL_CONTEXT (overrider_target))
2802 /* We already know that the overrider needs a covariant thunk. */
2803 b = get_primary_binfo (b);
2804 for (; ; b = get_primary_binfo (b))
2806 tree main_binfo = TYPE_BINFO (BINFO_TYPE (b));
2807 tree bv = chain_index (ix, BINFO_VIRTUALS (main_binfo));
2808 if (!DECL_THUNK_P (TREE_VALUE (bv)))
2809 break;
2810 if (BINFO_LOST_PRIMARY_P (b))
2811 lost = true;
2813 first_defn = b;
2816 /* Assume that we will produce a thunk that convert all the way to
2817 the final overrider, and not to an intermediate virtual base. */
2818 virtual_base = NULL_TREE;
2820 /* See if we can convert to an intermediate virtual base first, and then
2821 use the vcall offset located there to finish the conversion. */
2822 for (; b; b = BINFO_INHERITANCE_CHAIN (b))
2824 /* If we find the final overrider, then we can stop
2825 walking. */
2826 if (SAME_BINFO_TYPE_P (BINFO_TYPE (b),
2827 BINFO_TYPE (TREE_VALUE (overrider))))
2828 break;
2830 /* If we find a virtual base, and we haven't yet found the
2831 overrider, then there is a virtual base between the
2832 declaring base (first_defn) and the final overrider. */
2833 if (BINFO_VIRTUAL_P (b))
2835 virtual_base = b;
2836 break;
2840 /* Compute the constant adjustment to the `this' pointer. The
2841 `this' pointer, when this function is called, will point at BINFO
2842 (or one of its primary bases, which are at the same offset). */
2843 if (virtual_base)
2844 /* The `this' pointer needs to be adjusted from the declaration to
2845 the nearest virtual base. */
2846 delta = size_diffop_loc (input_location,
2847 fold_convert (ssizetype, BINFO_OFFSET (virtual_base)),
2848 fold_convert (ssizetype, BINFO_OFFSET (first_defn)));
2849 else if (lost)
2850 /* If the nearest definition is in a lost primary, we don't need an
2851 entry in our vtable. Except possibly in a constructor vtable,
2852 if we happen to get our primary back. In that case, the offset
2853 will be zero, as it will be a primary base. */
2854 delta = size_zero_node;
2855 else
2856 /* The `this' pointer needs to be adjusted from pointing to
2857 BINFO to pointing at the base where the final overrider
2858 appears. */
2859 delta = size_diffop_loc (input_location,
2860 fold_convert (ssizetype,
2861 BINFO_OFFSET (TREE_VALUE (overrider))),
2862 fold_convert (ssizetype, BINFO_OFFSET (binfo)));
2864 modify_vtable_entry (t, binfo, overrider_fn, delta, virtuals);
2866 if (virtual_base)
2867 BV_VCALL_INDEX (*virtuals)
2868 = get_vcall_index (overrider_target, BINFO_TYPE (virtual_base));
2869 else
2870 BV_VCALL_INDEX (*virtuals) = NULL_TREE;
2872 BV_LOST_PRIMARY (*virtuals) = lost;
2875 /* Called from modify_all_vtables via dfs_walk. */
2877 static tree
2878 dfs_modify_vtables (tree binfo, void* data)
2880 tree t = (tree) data;
2881 tree virtuals;
2882 tree old_virtuals;
2883 unsigned ix;
2885 if (!TYPE_CONTAINS_VPTR_P (BINFO_TYPE (binfo)))
2886 /* A base without a vtable needs no modification, and its bases
2887 are uninteresting. */
2888 return dfs_skip_bases;
2890 if (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), t)
2891 && !CLASSTYPE_HAS_PRIMARY_BASE_P (t))
2892 /* Don't do the primary vtable, if it's new. */
2893 return NULL_TREE;
2895 if (BINFO_PRIMARY_P (binfo) && !BINFO_VIRTUAL_P (binfo))
2896 /* There's no need to modify the vtable for a non-virtual primary
2897 base; we're not going to use that vtable anyhow. We do still
2898 need to do this for virtual primary bases, as they could become
2899 non-primary in a construction vtable. */
2900 return NULL_TREE;
2902 make_new_vtable (t, binfo);
2904 /* Now, go through each of the virtual functions in the virtual
2905 function table for BINFO. Find the final overrider, and update
2906 the BINFO_VIRTUALS list appropriately. */
2907 for (ix = 0, virtuals = BINFO_VIRTUALS (binfo),
2908 old_virtuals = BINFO_VIRTUALS (TYPE_BINFO (BINFO_TYPE (binfo)));
2909 virtuals;
2910 ix++, virtuals = TREE_CHAIN (virtuals),
2911 old_virtuals = TREE_CHAIN (old_virtuals))
2912 update_vtable_entry_for_fn (t,
2913 binfo,
2914 BV_FN (old_virtuals),
2915 &virtuals, ix);
2917 return NULL_TREE;
2920 /* Update all of the primary and secondary vtables for T. Create new
2921 vtables as required, and initialize their RTTI information. Each
2922 of the functions in VIRTUALS is declared in T and may override a
2923 virtual function from a base class; find and modify the appropriate
2924 entries to point to the overriding functions. Returns a list, in
2925 declaration order, of the virtual functions that are declared in T,
2926 but do not appear in the primary base class vtable, and which
2927 should therefore be appended to the end of the vtable for T. */
2929 static tree
2930 modify_all_vtables (tree t, tree virtuals)
2932 tree binfo = TYPE_BINFO (t);
2933 tree *fnsp;
2935 /* Mangle the vtable name before entering dfs_walk (c++/51884). */
2936 if (TYPE_CONTAINS_VPTR_P (t))
2937 get_vtable_decl (t, false);
2939 /* Update all of the vtables. */
2940 dfs_walk_once (binfo, dfs_modify_vtables, NULL, t);
2942 /* Add virtual functions not already in our primary vtable. These
2943 will be both those introduced by this class, and those overridden
2944 from secondary bases. It does not include virtuals merely
2945 inherited from secondary bases. */
2946 for (fnsp = &virtuals; *fnsp; )
2948 tree fn = TREE_VALUE (*fnsp);
2950 if (!value_member (fn, BINFO_VIRTUALS (binfo))
2951 || DECL_VINDEX (fn) == error_mark_node)
2953 /* We don't need to adjust the `this' pointer when
2954 calling this function. */
2955 BV_DELTA (*fnsp) = integer_zero_node;
2956 BV_VCALL_INDEX (*fnsp) = NULL_TREE;
2958 /* This is a function not already in our vtable. Keep it. */
2959 fnsp = &TREE_CHAIN (*fnsp);
2961 else
2962 /* We've already got an entry for this function. Skip it. */
2963 *fnsp = TREE_CHAIN (*fnsp);
2966 return virtuals;
2969 /* Get the base virtual function declarations in T that have the
2970 indicated NAME. */
2972 static void
2973 get_basefndecls (tree name, tree t, vec<tree> *base_fndecls)
2975 bool found_decls = false;
2977 /* Find virtual functions in T with the indicated NAME. */
2978 for (ovl_iterator iter (lookup_fnfields_slot (t, name)); iter; ++iter)
2980 tree method = *iter;
2982 if (TREE_CODE (method) == FUNCTION_DECL && DECL_VINDEX (method))
2984 base_fndecls->safe_push (method);
2985 found_decls = true;
2989 if (found_decls)
2990 return;
2992 int n_baseclasses = BINFO_N_BASE_BINFOS (TYPE_BINFO (t));
2993 for (int i = 0; i < n_baseclasses; i++)
2995 tree basetype = BINFO_TYPE (BINFO_BASE_BINFO (TYPE_BINFO (t), i));
2996 get_basefndecls (name, basetype, base_fndecls);
3000 /* If this declaration supersedes the declaration of
3001 a method declared virtual in the base class, then
3002 mark this field as being virtual as well. */
3004 void
3005 check_for_override (tree decl, tree ctype)
3007 bool overrides_found = false;
3008 if (TREE_CODE (decl) == TEMPLATE_DECL)
3009 /* In [temp.mem] we have:
3011 A specialization of a member function template does not
3012 override a virtual function from a base class. */
3013 return;
3014 if ((DECL_DESTRUCTOR_P (decl)
3015 || IDENTIFIER_VIRTUAL_P (DECL_NAME (decl))
3016 || DECL_CONV_FN_P (decl))
3017 && look_for_overrides (ctype, decl)
3018 && !DECL_STATIC_FUNCTION_P (decl))
3019 /* Set DECL_VINDEX to a value that is neither an INTEGER_CST nor
3020 the error_mark_node so that we know it is an overriding
3021 function. */
3023 DECL_VINDEX (decl) = decl;
3024 overrides_found = true;
3025 if (warn_override && !DECL_OVERRIDE_P (decl)
3026 && !DECL_DESTRUCTOR_P (decl))
3027 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wsuggest_override,
3028 "%qD can be marked override", decl);
3031 if (DECL_VIRTUAL_P (decl))
3033 if (!DECL_VINDEX (decl))
3034 DECL_VINDEX (decl) = error_mark_node;
3035 IDENTIFIER_VIRTUAL_P (DECL_NAME (decl)) = 1;
3036 if (DECL_DESTRUCTOR_P (decl))
3037 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (ctype) = true;
3039 else if (DECL_FINAL_P (decl))
3040 error ("%q+#D marked %<final%>, but is not virtual", decl);
3041 if (DECL_OVERRIDE_P (decl) && !overrides_found)
3042 error ("%q+#D marked %<override%>, but does not override", decl);
3045 /* Warn about hidden virtual functions that are not overridden in t.
3046 We know that constructors and destructors don't apply. */
3048 static void
3049 warn_hidden (tree t)
3051 vec<tree, va_gc> *method_vec = CLASSTYPE_METHOD_VEC (t);
3052 tree fns;
3053 size_t i;
3055 /* We go through each separately named virtual function. */
3056 for (i = CLASSTYPE_FIRST_CONVERSION_SLOT;
3057 vec_safe_iterate (method_vec, i, &fns);
3058 ++i)
3060 tree fndecl;
3061 tree base_binfo;
3062 tree binfo;
3063 int j;
3065 /* All functions in this slot in the CLASSTYPE_METHOD_VEC will
3066 have the same name. Figure out what name that is. */
3067 tree name = OVL_NAME (fns);
3068 /* There are no possibly hidden functions yet. */
3069 auto_vec<tree, 20> base_fndecls;
3070 /* Iterate through all of the base classes looking for possibly
3071 hidden functions. */
3072 for (binfo = TYPE_BINFO (t), j = 0;
3073 BINFO_BASE_ITERATE (binfo, j, base_binfo); j++)
3075 tree basetype = BINFO_TYPE (base_binfo);
3076 get_basefndecls (name, basetype, &base_fndecls);
3079 /* If there are no functions to hide, continue. */
3080 if (base_fndecls.is_empty ())
3081 continue;
3083 /* Remove any overridden functions. */
3084 for (ovl_iterator iter (fns); iter; ++iter)
3086 fndecl = *iter;
3087 if (TREE_CODE (fndecl) == FUNCTION_DECL
3088 && DECL_VINDEX (fndecl))
3090 /* If the method from the base class has the same
3091 signature as the method from the derived class, it
3092 has been overridden. */
3093 for (size_t k = 0; k < base_fndecls.length (); k++)
3094 if (base_fndecls[k]
3095 && same_signature_p (fndecl, base_fndecls[k]))
3096 base_fndecls[k] = NULL_TREE;
3100 /* Now give a warning for all base functions without overriders,
3101 as they are hidden. */
3102 size_t k;
3103 tree base_fndecl;
3104 FOR_EACH_VEC_ELT (base_fndecls, k, base_fndecl)
3105 if (base_fndecl)
3107 /* Here we know it is a hider, and no overrider exists. */
3108 warning_at (location_of (base_fndecl),
3109 OPT_Woverloaded_virtual,
3110 "%qD was hidden", base_fndecl);
3111 warning_at (location_of (fns),
3112 OPT_Woverloaded_virtual, " by %qD", fns);
3117 /* Recursive helper for finish_struct_anon. */
3119 static void
3120 finish_struct_anon_r (tree field, bool complain)
3122 bool is_union = TREE_CODE (TREE_TYPE (field)) == UNION_TYPE;
3123 tree elt = TYPE_FIELDS (TREE_TYPE (field));
3124 for (; elt; elt = DECL_CHAIN (elt))
3126 /* We're generally only interested in entities the user
3127 declared, but we also find nested classes by noticing
3128 the TYPE_DECL that we create implicitly. You're
3129 allowed to put one anonymous union inside another,
3130 though, so we explicitly tolerate that. We use
3131 TYPE_UNNAMED_P rather than ANON_AGGR_TYPE_P so that
3132 we also allow unnamed types used for defining fields. */
3133 if (DECL_ARTIFICIAL (elt)
3134 && (!DECL_IMPLICIT_TYPEDEF_P (elt)
3135 || TYPE_UNNAMED_P (TREE_TYPE (elt))))
3136 continue;
3138 if (TREE_CODE (elt) != FIELD_DECL)
3140 /* We already complained about static data members in
3141 finish_static_data_member_decl. */
3142 if (complain && !VAR_P (elt))
3144 if (is_union)
3145 permerror (DECL_SOURCE_LOCATION (elt),
3146 "%q#D invalid; an anonymous union can "
3147 "only have non-static data members", elt);
3148 else
3149 permerror (DECL_SOURCE_LOCATION (elt),
3150 "%q#D invalid; an anonymous struct can "
3151 "only have non-static data members", elt);
3153 continue;
3156 if (complain)
3158 if (TREE_PRIVATE (elt))
3160 if (is_union)
3161 permerror (DECL_SOURCE_LOCATION (elt),
3162 "private member %q#D in anonymous union", elt);
3163 else
3164 permerror (DECL_SOURCE_LOCATION (elt),
3165 "private member %q#D in anonymous struct", elt);
3167 else if (TREE_PROTECTED (elt))
3169 if (is_union)
3170 permerror (DECL_SOURCE_LOCATION (elt),
3171 "protected member %q#D in anonymous union", elt);
3172 else
3173 permerror (DECL_SOURCE_LOCATION (elt),
3174 "protected member %q#D in anonymous struct", elt);
3178 TREE_PRIVATE (elt) = TREE_PRIVATE (field);
3179 TREE_PROTECTED (elt) = TREE_PROTECTED (field);
3181 /* Recurse into the anonymous aggregates to handle correctly
3182 access control (c++/24926):
3184 class A {
3185 union {
3186 union {
3187 int i;
3192 int j=A().i; */
3193 if (DECL_NAME (elt) == NULL_TREE
3194 && ANON_AGGR_TYPE_P (TREE_TYPE (elt)))
3195 finish_struct_anon_r (elt, /*complain=*/false);
3199 /* Check for things that are invalid. There are probably plenty of other
3200 things we should check for also. */
3202 static void
3203 finish_struct_anon (tree t)
3205 for (tree field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
3207 if (TREE_STATIC (field))
3208 continue;
3209 if (TREE_CODE (field) != FIELD_DECL)
3210 continue;
3212 if (DECL_NAME (field) == NULL_TREE
3213 && ANON_AGGR_TYPE_P (TREE_TYPE (field)))
3214 finish_struct_anon_r (field, /*complain=*/true);
3218 /* Add T to CLASSTYPE_DECL_LIST of current_class_type which
3219 will be used later during class template instantiation.
3220 When FRIEND_P is zero, T can be a static member data (VAR_DECL),
3221 a non-static member data (FIELD_DECL), a member function
3222 (FUNCTION_DECL), a nested type (RECORD_TYPE, ENUM_TYPE),
3223 a typedef (TYPE_DECL) or a member class template (TEMPLATE_DECL)
3224 When FRIEND_P is nonzero, T is either a friend class
3225 (RECORD_TYPE, TEMPLATE_DECL) or a friend function
3226 (FUNCTION_DECL, TEMPLATE_DECL). */
3228 void
3229 maybe_add_class_template_decl_list (tree type, tree t, int friend_p)
3231 /* Save some memory by not creating TREE_LIST if TYPE is not template. */
3232 if (CLASSTYPE_TEMPLATE_INFO (type))
3233 CLASSTYPE_DECL_LIST (type)
3234 = tree_cons (friend_p ? NULL_TREE : type,
3235 t, CLASSTYPE_DECL_LIST (type));
3238 /* This function is called from declare_virt_assop_and_dtor via
3239 dfs_walk_all.
3241 DATA is a type that direcly or indirectly inherits the base
3242 represented by BINFO. If BINFO contains a virtual assignment [copy
3243 assignment or move assigment] operator or a virtual constructor,
3244 declare that function in DATA if it hasn't been already declared. */
3246 static tree
3247 dfs_declare_virt_assop_and_dtor (tree binfo, void *data)
3249 tree bv, fn, t = (tree)data;
3250 tree opname = cp_assignment_operator_id (NOP_EXPR);
3252 gcc_assert (t && CLASS_TYPE_P (t));
3253 gcc_assert (binfo && TREE_CODE (binfo) == TREE_BINFO);
3255 if (!TYPE_CONTAINS_VPTR_P (BINFO_TYPE (binfo)))
3256 /* A base without a vtable needs no modification, and its bases
3257 are uninteresting. */
3258 return dfs_skip_bases;
3260 if (BINFO_PRIMARY_P (binfo))
3261 /* If this is a primary base, then we have already looked at the
3262 virtual functions of its vtable. */
3263 return NULL_TREE;
3265 for (bv = BINFO_VIRTUALS (binfo); bv; bv = TREE_CHAIN (bv))
3267 fn = BV_FN (bv);
3269 if (DECL_NAME (fn) == opname)
3271 if (CLASSTYPE_LAZY_COPY_ASSIGN (t))
3272 lazily_declare_fn (sfk_copy_assignment, t);
3273 if (CLASSTYPE_LAZY_MOVE_ASSIGN (t))
3274 lazily_declare_fn (sfk_move_assignment, t);
3276 else if (DECL_DESTRUCTOR_P (fn)
3277 && CLASSTYPE_LAZY_DESTRUCTOR (t))
3278 lazily_declare_fn (sfk_destructor, t);
3281 return NULL_TREE;
3284 /* If the class type T has a direct or indirect base that contains a
3285 virtual assignment operator or a virtual destructor, declare that
3286 function in T if it hasn't been already declared. */
3288 static void
3289 declare_virt_assop_and_dtor (tree t)
3291 if (!(TYPE_POLYMORPHIC_P (t)
3292 && (CLASSTYPE_LAZY_COPY_ASSIGN (t)
3293 || CLASSTYPE_LAZY_MOVE_ASSIGN (t)
3294 || CLASSTYPE_LAZY_DESTRUCTOR (t))))
3295 return;
3297 dfs_walk_all (TYPE_BINFO (t),
3298 dfs_declare_virt_assop_and_dtor,
3299 NULL, t);
3302 /* Declare the inheriting constructor for class T inherited from base
3303 constructor CTOR with the parameter array PARMS of size NPARMS. */
3305 static void
3306 one_inheriting_sig (tree t, tree ctor, tree *parms, int nparms)
3308 /* We don't declare an inheriting ctor that would be a default,
3309 copy or move ctor for derived or base. */
3310 if (nparms == 0)
3311 return;
3312 if (nparms == 1
3313 && TREE_CODE (parms[0]) == REFERENCE_TYPE)
3315 tree parm = TYPE_MAIN_VARIANT (TREE_TYPE (parms[0]));
3316 if (parm == t || parm == DECL_CONTEXT (ctor))
3317 return;
3320 tree parmlist = void_list_node;
3321 for (int i = nparms - 1; i >= 0; i--)
3322 parmlist = tree_cons (NULL_TREE, parms[i], parmlist);
3323 tree fn = implicitly_declare_fn (sfk_inheriting_constructor,
3324 t, false, ctor, parmlist);
3325 gcc_assert (TYPE_MAIN_VARIANT (t) == t);
3326 if (add_method (t, fn, false))
3328 DECL_CHAIN (fn) = TYPE_METHODS (t);
3329 TYPE_METHODS (t) = fn;
3333 /* Declare all the inheriting constructors for class T inherited from base
3334 constructor CTOR. */
3336 static void
3337 one_inherited_ctor (tree ctor, tree t, tree using_decl)
3339 tree parms = FUNCTION_FIRST_USER_PARMTYPE (ctor);
3341 if (flag_new_inheriting_ctors)
3343 ctor = implicitly_declare_fn (sfk_inheriting_constructor,
3344 t, /*const*/false, ctor, parms);
3345 add_method (t, ctor, using_decl != NULL_TREE);
3346 TYPE_HAS_USER_CONSTRUCTOR (t) = true;
3347 return;
3350 tree *new_parms = XALLOCAVEC (tree, list_length (parms));
3351 int i = 0;
3352 for (; parms && parms != void_list_node; parms = TREE_CHAIN (parms))
3354 if (TREE_PURPOSE (parms))
3355 one_inheriting_sig (t, ctor, new_parms, i);
3356 new_parms[i++] = TREE_VALUE (parms);
3358 one_inheriting_sig (t, ctor, new_parms, i);
3359 if (parms == NULL_TREE)
3361 if (warning (OPT_Winherited_variadic_ctor,
3362 "the ellipsis in %qD is not inherited", ctor))
3363 inform (DECL_SOURCE_LOCATION (ctor), "%qD declared here", ctor);
3367 /* Create default constructors, assignment operators, and so forth for
3368 the type indicated by T, if they are needed. CANT_HAVE_CONST_CTOR,
3369 and CANT_HAVE_CONST_ASSIGNMENT are nonzero if, for whatever reason,
3370 the class cannot have a default constructor, copy constructor
3371 taking a const reference argument, or an assignment operator taking
3372 a const reference, respectively. */
3374 static void
3375 add_implicitly_declared_members (tree t, tree* access_decls,
3376 int cant_have_const_cctor,
3377 int cant_have_const_assignment)
3379 /* Destructor. */
3380 if (!CLASSTYPE_DESTRUCTOR (t))
3381 /* In general, we create destructors lazily. */
3382 CLASSTYPE_LAZY_DESTRUCTOR (t) = 1;
3384 bool move_ok = false;
3385 if (cxx_dialect >= cxx11 && CLASSTYPE_LAZY_DESTRUCTOR (t)
3386 && !TYPE_HAS_COPY_CTOR (t) && !TYPE_HAS_COPY_ASSIGN (t)
3387 && !classtype_has_move_assign_or_move_ctor_p (t, false))
3388 move_ok = true;
3390 /* [class.ctor]
3392 If there is no user-declared constructor for a class, a default
3393 constructor is implicitly declared. */
3394 if (! TYPE_HAS_USER_CONSTRUCTOR (t))
3396 TYPE_HAS_DEFAULT_CONSTRUCTOR (t) = 1;
3397 CLASSTYPE_LAZY_DEFAULT_CTOR (t) = 1;
3398 if (cxx_dialect >= cxx11)
3399 TYPE_HAS_CONSTEXPR_CTOR (t)
3400 /* Don't force the declaration to get a hard answer; if the
3401 definition would have made the class non-literal, it will still be
3402 non-literal because of the base or member in question, and that
3403 gives a better diagnostic. */
3404 = type_maybe_constexpr_default_constructor (t);
3407 /* [class.ctor]
3409 If a class definition does not explicitly declare a copy
3410 constructor, one is declared implicitly. */
3411 if (! TYPE_HAS_COPY_CTOR (t))
3413 TYPE_HAS_COPY_CTOR (t) = 1;
3414 TYPE_HAS_CONST_COPY_CTOR (t) = !cant_have_const_cctor;
3415 CLASSTYPE_LAZY_COPY_CTOR (t) = 1;
3416 if (move_ok)
3417 CLASSTYPE_LAZY_MOVE_CTOR (t) = 1;
3420 /* If there is no assignment operator, one will be created if and
3421 when it is needed. For now, just record whether or not the type
3422 of the parameter to the assignment operator will be a const or
3423 non-const reference. */
3424 if (!TYPE_HAS_COPY_ASSIGN (t))
3426 TYPE_HAS_COPY_ASSIGN (t) = 1;
3427 TYPE_HAS_CONST_COPY_ASSIGN (t) = !cant_have_const_assignment;
3428 CLASSTYPE_LAZY_COPY_ASSIGN (t) = 1;
3429 if (move_ok && !LAMBDA_TYPE_P (t))
3430 CLASSTYPE_LAZY_MOVE_ASSIGN (t) = 1;
3433 /* We can't be lazy about declaring functions that might override
3434 a virtual function from a base class. */
3435 declare_virt_assop_and_dtor (t);
3437 while (*access_decls)
3439 tree using_decl = TREE_VALUE (*access_decls);
3440 tree decl = USING_DECL_DECLS (using_decl);
3441 if (DECL_NAME (using_decl) == ctor_identifier)
3443 /* declare, then remove the decl */
3444 tree ctor_list = decl;
3445 location_t loc = input_location;
3446 input_location = DECL_SOURCE_LOCATION (using_decl);
3447 for (ovl_iterator iter (ctor_list); iter; ++iter)
3448 one_inherited_ctor (*iter, t, using_decl);
3449 *access_decls = TREE_CHAIN (*access_decls);
3450 input_location = loc;
3452 else
3453 access_decls = &TREE_CHAIN (*access_decls);
3457 /* Subroutine of insert_into_classtype_sorted_fields. Recursively
3458 count the number of fields in TYPE, including anonymous union
3459 members. */
3461 static int
3462 count_fields (tree fields)
3464 tree x;
3465 int n_fields = 0;
3466 for (x = fields; x; x = DECL_CHAIN (x))
3468 if (TREE_CODE (x) == FIELD_DECL && ANON_AGGR_TYPE_P (TREE_TYPE (x)))
3469 n_fields += count_fields (TYPE_FIELDS (TREE_TYPE (x)));
3470 else
3471 n_fields += 1;
3473 return n_fields;
3476 /* Subroutine of insert_into_classtype_sorted_fields. Recursively add
3477 all the fields in the TREE_LIST FIELDS to the SORTED_FIELDS_TYPE
3478 elts, starting at offset IDX. */
3480 static int
3481 add_fields_to_record_type (tree fields, struct sorted_fields_type *field_vec, int idx)
3483 tree x;
3484 for (x = fields; x; x = DECL_CHAIN (x))
3486 if (TREE_CODE (x) == FIELD_DECL && ANON_AGGR_TYPE_P (TREE_TYPE (x)))
3487 idx = add_fields_to_record_type (TYPE_FIELDS (TREE_TYPE (x)), field_vec, idx);
3488 else
3489 field_vec->elts[idx++] = x;
3491 return idx;
3494 /* Add all of the enum values of ENUMTYPE, to the FIELD_VEC elts,
3495 starting at offset IDX. */
3497 static int
3498 add_enum_fields_to_record_type (tree enumtype,
3499 struct sorted_fields_type *field_vec,
3500 int idx)
3502 tree values;
3503 for (values = TYPE_VALUES (enumtype); values; values = TREE_CHAIN (values))
3504 field_vec->elts[idx++] = TREE_VALUE (values);
3505 return idx;
3508 /* FIELD is a bit-field. We are finishing the processing for its
3509 enclosing type. Issue any appropriate messages and set appropriate
3510 flags. Returns false if an error has been diagnosed. */
3512 static bool
3513 check_bitfield_decl (tree field)
3515 tree type = TREE_TYPE (field);
3516 tree w;
3518 /* Extract the declared width of the bitfield, which has been
3519 temporarily stashed in DECL_INITIAL. */
3520 w = DECL_INITIAL (field);
3521 gcc_assert (w != NULL_TREE);
3522 /* Remove the bit-field width indicator so that the rest of the
3523 compiler does not treat that value as an initializer. */
3524 DECL_INITIAL (field) = NULL_TREE;
3526 /* Detect invalid bit-field type. */
3527 if (!INTEGRAL_OR_ENUMERATION_TYPE_P (type))
3529 error ("bit-field %q+#D with non-integral type", field);
3530 w = error_mark_node;
3532 else
3534 location_t loc = input_location;
3535 /* Avoid the non_lvalue wrapper added by fold for PLUS_EXPRs. */
3536 STRIP_NOPS (w);
3538 /* detect invalid field size. */
3539 input_location = DECL_SOURCE_LOCATION (field);
3540 w = cxx_constant_value (w);
3541 input_location = loc;
3543 if (TREE_CODE (w) != INTEGER_CST)
3545 error ("bit-field %q+D width not an integer constant", field);
3546 w = error_mark_node;
3548 else if (tree_int_cst_sgn (w) < 0)
3550 error ("negative width in bit-field %q+D", field);
3551 w = error_mark_node;
3553 else if (integer_zerop (w) && DECL_NAME (field) != 0)
3555 error ("zero width for bit-field %q+D", field);
3556 w = error_mark_node;
3558 else if ((TREE_CODE (type) != ENUMERAL_TYPE
3559 && TREE_CODE (type) != BOOLEAN_TYPE
3560 && compare_tree_int (w, TYPE_PRECISION (type)) > 0)
3561 || ((TREE_CODE (type) == ENUMERAL_TYPE
3562 || TREE_CODE (type) == BOOLEAN_TYPE)
3563 && tree_int_cst_lt (TYPE_SIZE (type), w)))
3564 warning_at (DECL_SOURCE_LOCATION (field), 0,
3565 "width of %qD exceeds its type", field);
3566 else if (TREE_CODE (type) == ENUMERAL_TYPE
3567 && (0 > (compare_tree_int
3568 (w, TYPE_PRECISION (ENUM_UNDERLYING_TYPE (type))))))
3569 warning_at (DECL_SOURCE_LOCATION (field), 0,
3570 "%qD is too small to hold all values of %q#T",
3571 field, type);
3574 if (w != error_mark_node)
3576 DECL_SIZE (field) = fold_convert (bitsizetype, w);
3577 DECL_BIT_FIELD (field) = 1;
3578 return true;
3580 else
3582 /* Non-bit-fields are aligned for their type. */
3583 DECL_BIT_FIELD (field) = 0;
3584 CLEAR_DECL_C_BIT_FIELD (field);
3585 return false;
3589 /* FIELD is a non bit-field. We are finishing the processing for its
3590 enclosing type T. Issue any appropriate messages and set appropriate
3591 flags. */
3593 static bool
3594 check_field_decl (tree field,
3595 tree t,
3596 int* cant_have_const_ctor,
3597 int* no_const_asn_ref)
3599 tree type = strip_array_types (TREE_TYPE (field));
3600 bool any_default_members = false;
3602 /* In C++98 an anonymous union cannot contain any fields which would change
3603 the settings of CANT_HAVE_CONST_CTOR and friends. */
3604 if (ANON_UNION_TYPE_P (type) && cxx_dialect < cxx11)
3606 /* And, we don't set TYPE_HAS_CONST_COPY_CTOR, etc., for anonymous
3607 structs. So, we recurse through their fields here. */
3608 else if (ANON_AGGR_TYPE_P (type))
3610 for (tree fields = TYPE_FIELDS (type); fields;
3611 fields = DECL_CHAIN (fields))
3612 if (TREE_CODE (fields) == FIELD_DECL && !DECL_C_BIT_FIELD (field))
3613 any_default_members |= check_field_decl (fields, t,
3614 cant_have_const_ctor,
3615 no_const_asn_ref);
3617 /* Check members with class type for constructors, destructors,
3618 etc. */
3619 else if (CLASS_TYPE_P (type))
3621 /* Never let anything with uninheritable virtuals
3622 make it through without complaint. */
3623 abstract_virtuals_error (field, type);
3625 if (TREE_CODE (t) == UNION_TYPE && cxx_dialect < cxx11)
3627 static bool warned;
3628 int oldcount = errorcount;
3629 if (TYPE_NEEDS_CONSTRUCTING (type))
3630 error ("member %q+#D with constructor not allowed in union",
3631 field);
3632 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
3633 error ("member %q+#D with destructor not allowed in union", field);
3634 if (TYPE_HAS_COMPLEX_COPY_ASSIGN (type))
3635 error ("member %q+#D with copy assignment operator not allowed in union",
3636 field);
3637 if (!warned && errorcount > oldcount)
3639 inform (DECL_SOURCE_LOCATION (field), "unrestricted unions "
3640 "only available with -std=c++11 or -std=gnu++11");
3641 warned = true;
3644 else
3646 TYPE_NEEDS_CONSTRUCTING (t) |= TYPE_NEEDS_CONSTRUCTING (type);
3647 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t)
3648 |= TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type);
3649 TYPE_HAS_COMPLEX_COPY_ASSIGN (t)
3650 |= (TYPE_HAS_COMPLEX_COPY_ASSIGN (type)
3651 || !TYPE_HAS_COPY_ASSIGN (type));
3652 TYPE_HAS_COMPLEX_COPY_CTOR (t) |= (TYPE_HAS_COMPLEX_COPY_CTOR (type)
3653 || !TYPE_HAS_COPY_CTOR (type));
3654 TYPE_HAS_COMPLEX_MOVE_ASSIGN (t) |= TYPE_HAS_COMPLEX_MOVE_ASSIGN (type);
3655 TYPE_HAS_COMPLEX_MOVE_CTOR (t) |= TYPE_HAS_COMPLEX_MOVE_CTOR (type);
3656 TYPE_HAS_COMPLEX_DFLT (t) |= (!TYPE_HAS_DEFAULT_CONSTRUCTOR (type)
3657 || TYPE_HAS_COMPLEX_DFLT (type));
3660 if (TYPE_HAS_COPY_CTOR (type)
3661 && !TYPE_HAS_CONST_COPY_CTOR (type))
3662 *cant_have_const_ctor = 1;
3664 if (TYPE_HAS_COPY_ASSIGN (type)
3665 && !TYPE_HAS_CONST_COPY_ASSIGN (type))
3666 *no_const_asn_ref = 1;
3669 check_abi_tags (t, field);
3671 if (DECL_INITIAL (field) != NULL_TREE)
3672 /* `build_class_init_list' does not recognize
3673 non-FIELD_DECLs. */
3674 any_default_members = true;
3676 return any_default_members;
3679 /* Check the data members (both static and non-static), class-scoped
3680 typedefs, etc., appearing in the declaration of T. Issue
3681 appropriate diagnostics. Sets ACCESS_DECLS to a list (in
3682 declaration order) of access declarations; each TREE_VALUE in this
3683 list is a USING_DECL.
3685 In addition, set the following flags:
3687 EMPTY_P
3688 The class is empty, i.e., contains no non-static data members.
3690 CANT_HAVE_CONST_CTOR_P
3691 This class cannot have an implicitly generated copy constructor
3692 taking a const reference.
3694 CANT_HAVE_CONST_ASN_REF
3695 This class cannot have an implicitly generated assignment
3696 operator taking a const reference.
3698 All of these flags should be initialized before calling this
3699 function.
3701 Returns a pointer to the end of the TYPE_FIELDs chain; additional
3702 fields can be added by adding to this chain. */
3704 static void
3705 check_field_decls (tree t, tree *access_decls,
3706 int *cant_have_const_ctor_p,
3707 int *no_const_asn_ref_p)
3709 tree *field;
3710 tree *next;
3711 bool has_pointers;
3712 bool any_default_members;
3713 int cant_pack = 0;
3714 int field_access = -1;
3716 /* Assume there are no access declarations. */
3717 *access_decls = NULL_TREE;
3718 /* Assume this class has no pointer members. */
3719 has_pointers = false;
3720 /* Assume none of the members of this class have default
3721 initializations. */
3722 any_default_members = false;
3724 for (field = &TYPE_FIELDS (t); *field; field = next)
3726 tree x = *field;
3727 tree type = TREE_TYPE (x);
3728 int this_field_access;
3730 next = &DECL_CHAIN (x);
3732 if (TREE_CODE (x) == USING_DECL)
3734 /* Save the access declarations for our caller. */
3735 *access_decls = tree_cons (NULL_TREE, x, *access_decls);
3736 continue;
3739 if (TREE_CODE (x) == TYPE_DECL
3740 || TREE_CODE (x) == TEMPLATE_DECL)
3741 continue;
3743 /* If we've gotten this far, it's a data member, possibly static,
3744 or an enumerator. */
3745 if (TREE_CODE (x) != CONST_DECL)
3746 DECL_CONTEXT (x) = t;
3748 /* When this goes into scope, it will be a non-local reference. */
3749 DECL_NONLOCAL (x) = 1;
3751 if (TREE_CODE (t) == UNION_TYPE)
3753 /* [class.union] (C++98)
3755 If a union contains a static data member, or a member of
3756 reference type, the program is ill-formed.
3758 In C++11 [class.union] says:
3759 If a union contains a non-static data member of reference type
3760 the program is ill-formed. */
3761 if (VAR_P (x) && cxx_dialect < cxx11)
3763 error ("in C++98 %q+D may not be static because it is "
3764 "a member of a union", x);
3765 continue;
3767 if (TREE_CODE (type) == REFERENCE_TYPE
3768 && TREE_CODE (x) == FIELD_DECL)
3770 error ("non-static data member %q+D in a union may not "
3771 "have reference type %qT", x, type);
3772 continue;
3776 /* Perform error checking that did not get done in
3777 grokdeclarator. */
3778 if (TREE_CODE (type) == FUNCTION_TYPE)
3780 error ("field %q+D invalidly declared function type", x);
3781 type = build_pointer_type (type);
3782 TREE_TYPE (x) = type;
3784 else if (TREE_CODE (type) == METHOD_TYPE)
3786 error ("field %q+D invalidly declared method type", x);
3787 type = build_pointer_type (type);
3788 TREE_TYPE (x) = type;
3791 if (type == error_mark_node)
3792 continue;
3794 if (TREE_CODE (x) == CONST_DECL || VAR_P (x))
3795 continue;
3797 /* Now it can only be a FIELD_DECL. */
3799 if (TREE_PRIVATE (x) || TREE_PROTECTED (x))
3800 CLASSTYPE_NON_AGGREGATE (t) = 1;
3802 /* If at least one non-static data member is non-literal, the whole
3803 class becomes non-literal. Per Core/1453, volatile non-static
3804 data members and base classes are also not allowed.
3805 Note: if the type is incomplete we will complain later on. */
3806 if (COMPLETE_TYPE_P (type)
3807 && (!literal_type_p (type) || CP_TYPE_VOLATILE_P (type)))
3808 CLASSTYPE_LITERAL_P (t) = false;
3810 /* A standard-layout class is a class that:
3812 has the same access control (Clause 11) for all non-static data members,
3813 ... */
3814 this_field_access = TREE_PROTECTED (x) ? 1 : TREE_PRIVATE (x) ? 2 : 0;
3815 if (field_access == -1)
3816 field_access = this_field_access;
3817 else if (this_field_access != field_access)
3818 CLASSTYPE_NON_STD_LAYOUT (t) = 1;
3820 /* If this is of reference type, check if it needs an init. */
3821 if (TREE_CODE (type) == REFERENCE_TYPE)
3823 CLASSTYPE_NON_LAYOUT_POD_P (t) = 1;
3824 CLASSTYPE_NON_STD_LAYOUT (t) = 1;
3825 if (DECL_INITIAL (x) == NULL_TREE)
3826 SET_CLASSTYPE_REF_FIELDS_NEED_INIT (t, 1);
3827 if (cxx_dialect < cxx11)
3829 /* ARM $12.6.2: [A member initializer list] (or, for an
3830 aggregate, initialization by a brace-enclosed list) is the
3831 only way to initialize nonstatic const and reference
3832 members. */
3833 TYPE_HAS_COMPLEX_COPY_ASSIGN (t) = 1;
3834 TYPE_HAS_COMPLEX_MOVE_ASSIGN (t) = 1;
3838 type = strip_array_types (type);
3840 if (TYPE_PACKED (t))
3842 if (!layout_pod_type_p (type) && !TYPE_PACKED (type))
3844 warning_at
3845 (DECL_SOURCE_LOCATION (x), 0,
3846 "ignoring packed attribute because of unpacked non-POD field %q#D",
3848 cant_pack = 1;
3850 else if (DECL_C_BIT_FIELD (x)
3851 || TYPE_ALIGN (TREE_TYPE (x)) > BITS_PER_UNIT)
3852 DECL_PACKED (x) = 1;
3855 if (DECL_C_BIT_FIELD (x) && integer_zerop (DECL_INITIAL (x)))
3856 /* We don't treat zero-width bitfields as making a class
3857 non-empty. */
3859 else
3861 /* The class is non-empty. */
3862 CLASSTYPE_EMPTY_P (t) = 0;
3863 /* The class is not even nearly empty. */
3864 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
3865 /* If one of the data members contains an empty class,
3866 so does T. */
3867 if (CLASS_TYPE_P (type)
3868 && CLASSTYPE_CONTAINS_EMPTY_CLASS_P (type))
3869 CLASSTYPE_CONTAINS_EMPTY_CLASS_P (t) = 1;
3872 /* This is used by -Weffc++ (see below). Warn only for pointers
3873 to members which might hold dynamic memory. So do not warn
3874 for pointers to functions or pointers to members. */
3875 if (TYPE_PTR_P (type)
3876 && !TYPE_PTRFN_P (type))
3877 has_pointers = true;
3879 if (CLASS_TYPE_P (type))
3881 if (CLASSTYPE_REF_FIELDS_NEED_INIT (type))
3882 SET_CLASSTYPE_REF_FIELDS_NEED_INIT (t, 1);
3883 if (CLASSTYPE_READONLY_FIELDS_NEED_INIT (type))
3884 SET_CLASSTYPE_READONLY_FIELDS_NEED_INIT (t, 1);
3887 if (DECL_MUTABLE_P (x) || TYPE_HAS_MUTABLE_P (type))
3888 CLASSTYPE_HAS_MUTABLE (t) = 1;
3890 if (DECL_MUTABLE_P (x))
3892 if (CP_TYPE_CONST_P (type))
3894 error ("member %q+D cannot be declared both %<const%> "
3895 "and %<mutable%>", x);
3896 continue;
3898 if (TREE_CODE (type) == REFERENCE_TYPE)
3900 error ("member %q+D cannot be declared as a %<mutable%> "
3901 "reference", x);
3902 continue;
3906 if (! layout_pod_type_p (type))
3907 /* DR 148 now allows pointers to members (which are POD themselves),
3908 to be allowed in POD structs. */
3909 CLASSTYPE_NON_LAYOUT_POD_P (t) = 1;
3911 if (!std_layout_type_p (type))
3912 CLASSTYPE_NON_STD_LAYOUT (t) = 1;
3914 if (! zero_init_p (type))
3915 CLASSTYPE_NON_ZERO_INIT_P (t) = 1;
3917 /* We set DECL_C_BIT_FIELD in grokbitfield.
3918 If the type and width are valid, we'll also set DECL_BIT_FIELD. */
3919 if ((! DECL_C_BIT_FIELD (x) || ! check_bitfield_decl (x))
3920 && check_field_decl (x, t,
3921 cant_have_const_ctor_p,
3922 no_const_asn_ref_p))
3924 if (any_default_members
3925 && TREE_CODE (t) == UNION_TYPE)
3926 error ("multiple fields in union %qT initialized", t);
3927 any_default_members = true;
3930 /* Now that we've removed bit-field widths from DECL_INITIAL,
3931 anything left in DECL_INITIAL is an NSDMI that makes the class
3932 non-aggregate in C++11. */
3933 if (DECL_INITIAL (x) && cxx_dialect < cxx14)
3934 CLASSTYPE_NON_AGGREGATE (t) = true;
3936 /* If any field is const, the structure type is pseudo-const. */
3937 if (CP_TYPE_CONST_P (type))
3939 C_TYPE_FIELDS_READONLY (t) = 1;
3940 if (DECL_INITIAL (x) == NULL_TREE)
3941 SET_CLASSTYPE_READONLY_FIELDS_NEED_INIT (t, 1);
3942 if (cxx_dialect < cxx11)
3944 /* ARM $12.6.2: [A member initializer list] (or, for an
3945 aggregate, initialization by a brace-enclosed list) is the
3946 only way to initialize nonstatic const and reference
3947 members. */
3948 TYPE_HAS_COMPLEX_COPY_ASSIGN (t) = 1;
3949 TYPE_HAS_COMPLEX_MOVE_ASSIGN (t) = 1;
3952 /* A field that is pseudo-const makes the structure likewise. */
3953 else if (CLASS_TYPE_P (type))
3955 C_TYPE_FIELDS_READONLY (t) |= C_TYPE_FIELDS_READONLY (type);
3956 SET_CLASSTYPE_READONLY_FIELDS_NEED_INIT (t,
3957 CLASSTYPE_READONLY_FIELDS_NEED_INIT (t)
3958 | CLASSTYPE_READONLY_FIELDS_NEED_INIT (type));
3961 /* Core issue 80: A nonstatic data member is required to have a
3962 different name from the class iff the class has a
3963 user-declared constructor. */
3964 if (constructor_name_p (DECL_NAME (x), t)
3965 && TYPE_HAS_USER_CONSTRUCTOR (t))
3966 permerror (DECL_SOURCE_LOCATION (x),
3967 "field %q#D with same name as class", x);
3970 /* Effective C++ rule 11: if a class has dynamic memory held by pointers,
3971 it should also define a copy constructor and an assignment operator to
3972 implement the correct copy semantic (deep vs shallow, etc.). As it is
3973 not feasible to check whether the constructors do allocate dynamic memory
3974 and store it within members, we approximate the warning like this:
3976 -- Warn only if there are members which are pointers
3977 -- Warn only if there is a non-trivial constructor (otherwise,
3978 there cannot be memory allocated).
3979 -- Warn only if there is a non-trivial destructor. We assume that the
3980 user at least implemented the cleanup correctly, and a destructor
3981 is needed to free dynamic memory.
3983 This seems enough for practical purposes. */
3984 if (warn_ecpp
3985 && has_pointers
3986 && TYPE_HAS_USER_CONSTRUCTOR (t)
3987 && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t)
3988 && !(TYPE_HAS_COPY_CTOR (t) && TYPE_HAS_COPY_ASSIGN (t)))
3990 warning (OPT_Weffc__, "%q#T has pointer data members", t);
3992 if (! TYPE_HAS_COPY_CTOR (t))
3994 warning (OPT_Weffc__,
3995 " but does not override %<%T(const %T&)%>", t, t);
3996 if (!TYPE_HAS_COPY_ASSIGN (t))
3997 warning (OPT_Weffc__, " or %<operator=(const %T&)%>", t);
3999 else if (! TYPE_HAS_COPY_ASSIGN (t))
4000 warning (OPT_Weffc__,
4001 " but does not override %<operator=(const %T&)%>", t);
4004 /* Non-static data member initializers make the default constructor
4005 non-trivial. */
4006 if (any_default_members)
4008 TYPE_NEEDS_CONSTRUCTING (t) = true;
4009 TYPE_HAS_COMPLEX_DFLT (t) = true;
4012 /* If any of the fields couldn't be packed, unset TYPE_PACKED. */
4013 if (cant_pack)
4014 TYPE_PACKED (t) = 0;
4016 /* Check anonymous struct/anonymous union fields. */
4017 finish_struct_anon (t);
4019 /* We've built up the list of access declarations in reverse order.
4020 Fix that now. */
4021 *access_decls = nreverse (*access_decls);
4024 /* If TYPE is an empty class type, records its OFFSET in the table of
4025 OFFSETS. */
4027 static int
4028 record_subobject_offset (tree type, tree offset, splay_tree offsets)
4030 splay_tree_node n;
4032 if (!is_empty_class (type))
4033 return 0;
4035 /* Record the location of this empty object in OFFSETS. */
4036 n = splay_tree_lookup (offsets, (splay_tree_key) offset);
4037 if (!n)
4038 n = splay_tree_insert (offsets,
4039 (splay_tree_key) offset,
4040 (splay_tree_value) NULL_TREE);
4041 n->value = ((splay_tree_value)
4042 tree_cons (NULL_TREE,
4043 type,
4044 (tree) n->value));
4046 return 0;
4049 /* Returns nonzero if TYPE is an empty class type and there is
4050 already an entry in OFFSETS for the same TYPE as the same OFFSET. */
4052 static int
4053 check_subobject_offset (tree type, tree offset, splay_tree offsets)
4055 splay_tree_node n;
4056 tree t;
4058 if (!is_empty_class (type))
4059 return 0;
4061 /* Record the location of this empty object in OFFSETS. */
4062 n = splay_tree_lookup (offsets, (splay_tree_key) offset);
4063 if (!n)
4064 return 0;
4066 for (t = (tree) n->value; t; t = TREE_CHAIN (t))
4067 if (same_type_p (TREE_VALUE (t), type))
4068 return 1;
4070 return 0;
4073 /* Walk through all the subobjects of TYPE (located at OFFSET). Call
4074 F for every subobject, passing it the type, offset, and table of
4075 OFFSETS. If VBASES_P is one, then virtual non-primary bases should
4076 be traversed.
4078 If MAX_OFFSET is non-NULL, then subobjects with an offset greater
4079 than MAX_OFFSET will not be walked.
4081 If F returns a nonzero value, the traversal ceases, and that value
4082 is returned. Otherwise, returns zero. */
4084 static int
4085 walk_subobject_offsets (tree type,
4086 subobject_offset_fn f,
4087 tree offset,
4088 splay_tree offsets,
4089 tree max_offset,
4090 int vbases_p)
4092 int r = 0;
4093 tree type_binfo = NULL_TREE;
4095 /* If this OFFSET is bigger than the MAX_OFFSET, then we should
4096 stop. */
4097 if (max_offset && tree_int_cst_lt (max_offset, offset))
4098 return 0;
4100 if (type == error_mark_node)
4101 return 0;
4103 if (!TYPE_P (type))
4105 type_binfo = type;
4106 type = BINFO_TYPE (type);
4109 if (CLASS_TYPE_P (type))
4111 tree field;
4112 tree binfo;
4113 int i;
4115 /* Avoid recursing into objects that are not interesting. */
4116 if (!CLASSTYPE_CONTAINS_EMPTY_CLASS_P (type))
4117 return 0;
4119 /* Record the location of TYPE. */
4120 r = (*f) (type, offset, offsets);
4121 if (r)
4122 return r;
4124 /* Iterate through the direct base classes of TYPE. */
4125 if (!type_binfo)
4126 type_binfo = TYPE_BINFO (type);
4127 for (i = 0; BINFO_BASE_ITERATE (type_binfo, i, binfo); i++)
4129 tree binfo_offset;
4131 if (BINFO_VIRTUAL_P (binfo))
4132 continue;
4134 tree orig_binfo;
4135 /* We cannot rely on BINFO_OFFSET being set for the base
4136 class yet, but the offsets for direct non-virtual
4137 bases can be calculated by going back to the TYPE. */
4138 orig_binfo = BINFO_BASE_BINFO (TYPE_BINFO (type), i);
4139 binfo_offset = size_binop (PLUS_EXPR,
4140 offset,
4141 BINFO_OFFSET (orig_binfo));
4143 r = walk_subobject_offsets (binfo,
4145 binfo_offset,
4146 offsets,
4147 max_offset,
4148 /*vbases_p=*/0);
4149 if (r)
4150 return r;
4153 if (CLASSTYPE_VBASECLASSES (type))
4155 unsigned ix;
4156 vec<tree, va_gc> *vbases;
4158 /* Iterate through the virtual base classes of TYPE. In G++
4159 3.2, we included virtual bases in the direct base class
4160 loop above, which results in incorrect results; the
4161 correct offsets for virtual bases are only known when
4162 working with the most derived type. */
4163 if (vbases_p)
4164 for (vbases = CLASSTYPE_VBASECLASSES (type), ix = 0;
4165 vec_safe_iterate (vbases, ix, &binfo); ix++)
4167 r = walk_subobject_offsets (binfo,
4169 size_binop (PLUS_EXPR,
4170 offset,
4171 BINFO_OFFSET (binfo)),
4172 offsets,
4173 max_offset,
4174 /*vbases_p=*/0);
4175 if (r)
4176 return r;
4178 else
4180 /* We still have to walk the primary base, if it is
4181 virtual. (If it is non-virtual, then it was walked
4182 above.) */
4183 tree vbase = get_primary_binfo (type_binfo);
4185 if (vbase && BINFO_VIRTUAL_P (vbase)
4186 && BINFO_PRIMARY_P (vbase)
4187 && BINFO_INHERITANCE_CHAIN (vbase) == type_binfo)
4189 r = (walk_subobject_offsets
4190 (vbase, f, offset,
4191 offsets, max_offset, /*vbases_p=*/0));
4192 if (r)
4193 return r;
4198 /* Iterate through the fields of TYPE. */
4199 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
4200 if (TREE_CODE (field) == FIELD_DECL
4201 && TREE_TYPE (field) != error_mark_node
4202 && !DECL_ARTIFICIAL (field))
4204 tree field_offset;
4206 field_offset = byte_position (field);
4208 r = walk_subobject_offsets (TREE_TYPE (field),
4210 size_binop (PLUS_EXPR,
4211 offset,
4212 field_offset),
4213 offsets,
4214 max_offset,
4215 /*vbases_p=*/1);
4216 if (r)
4217 return r;
4220 else if (TREE_CODE (type) == ARRAY_TYPE)
4222 tree element_type = strip_array_types (type);
4223 tree domain = TYPE_DOMAIN (type);
4224 tree index;
4226 /* Avoid recursing into objects that are not interesting. */
4227 if (!CLASS_TYPE_P (element_type)
4228 || !CLASSTYPE_CONTAINS_EMPTY_CLASS_P (element_type)
4229 || !domain
4230 || integer_minus_onep (TYPE_MAX_VALUE (domain)))
4231 return 0;
4233 /* Step through each of the elements in the array. */
4234 for (index = size_zero_node;
4235 !tree_int_cst_lt (TYPE_MAX_VALUE (domain), index);
4236 index = size_binop (PLUS_EXPR, index, size_one_node))
4238 r = walk_subobject_offsets (TREE_TYPE (type),
4240 offset,
4241 offsets,
4242 max_offset,
4243 /*vbases_p=*/1);
4244 if (r)
4245 return r;
4246 offset = size_binop (PLUS_EXPR, offset,
4247 TYPE_SIZE_UNIT (TREE_TYPE (type)));
4248 /* If this new OFFSET is bigger than the MAX_OFFSET, then
4249 there's no point in iterating through the remaining
4250 elements of the array. */
4251 if (max_offset && tree_int_cst_lt (max_offset, offset))
4252 break;
4256 return 0;
4259 /* Record all of the empty subobjects of TYPE (either a type or a
4260 binfo). If IS_DATA_MEMBER is true, then a non-static data member
4261 is being placed at OFFSET; otherwise, it is a base class that is
4262 being placed at OFFSET. */
4264 static void
4265 record_subobject_offsets (tree type,
4266 tree offset,
4267 splay_tree offsets,
4268 bool is_data_member)
4270 tree max_offset;
4271 /* If recording subobjects for a non-static data member or a
4272 non-empty base class , we do not need to record offsets beyond
4273 the size of the biggest empty class. Additional data members
4274 will go at the end of the class. Additional base classes will go
4275 either at offset zero (if empty, in which case they cannot
4276 overlap with offsets past the size of the biggest empty class) or
4277 at the end of the class.
4279 However, if we are placing an empty base class, then we must record
4280 all offsets, as either the empty class is at offset zero (where
4281 other empty classes might later be placed) or at the end of the
4282 class (where other objects might then be placed, so other empty
4283 subobjects might later overlap). */
4284 if (is_data_member
4285 || !is_empty_class (BINFO_TYPE (type)))
4286 max_offset = sizeof_biggest_empty_class;
4287 else
4288 max_offset = NULL_TREE;
4289 walk_subobject_offsets (type, record_subobject_offset, offset,
4290 offsets, max_offset, is_data_member);
4293 /* Returns nonzero if any of the empty subobjects of TYPE (located at
4294 OFFSET) conflict with entries in OFFSETS. If VBASES_P is nonzero,
4295 virtual bases of TYPE are examined. */
4297 static int
4298 layout_conflict_p (tree type,
4299 tree offset,
4300 splay_tree offsets,
4301 int vbases_p)
4303 splay_tree_node max_node;
4305 /* Get the node in OFFSETS that indicates the maximum offset where
4306 an empty subobject is located. */
4307 max_node = splay_tree_max (offsets);
4308 /* If there aren't any empty subobjects, then there's no point in
4309 performing this check. */
4310 if (!max_node)
4311 return 0;
4313 return walk_subobject_offsets (type, check_subobject_offset, offset,
4314 offsets, (tree) (max_node->key),
4315 vbases_p);
4318 /* DECL is a FIELD_DECL corresponding either to a base subobject of a
4319 non-static data member of the type indicated by RLI. BINFO is the
4320 binfo corresponding to the base subobject, OFFSETS maps offsets to
4321 types already located at those offsets. This function determines
4322 the position of the DECL. */
4324 static void
4325 layout_nonempty_base_or_field (record_layout_info rli,
4326 tree decl,
4327 tree binfo,
4328 splay_tree offsets)
4330 tree offset = NULL_TREE;
4331 bool field_p;
4332 tree type;
4334 if (binfo)
4336 /* For the purposes of determining layout conflicts, we want to
4337 use the class type of BINFO; TREE_TYPE (DECL) will be the
4338 CLASSTYPE_AS_BASE version, which does not contain entries for
4339 zero-sized bases. */
4340 type = TREE_TYPE (binfo);
4341 field_p = false;
4343 else
4345 type = TREE_TYPE (decl);
4346 field_p = true;
4349 /* Try to place the field. It may take more than one try if we have
4350 a hard time placing the field without putting two objects of the
4351 same type at the same address. */
4352 while (1)
4354 struct record_layout_info_s old_rli = *rli;
4356 /* Place this field. */
4357 place_field (rli, decl);
4358 offset = byte_position (decl);
4360 /* We have to check to see whether or not there is already
4361 something of the same type at the offset we're about to use.
4362 For example, consider:
4364 struct S {};
4365 struct T : public S { int i; };
4366 struct U : public S, public T {};
4368 Here, we put S at offset zero in U. Then, we can't put T at
4369 offset zero -- its S component would be at the same address
4370 as the S we already allocated. So, we have to skip ahead.
4371 Since all data members, including those whose type is an
4372 empty class, have nonzero size, any overlap can happen only
4373 with a direct or indirect base-class -- it can't happen with
4374 a data member. */
4375 /* In a union, overlap is permitted; all members are placed at
4376 offset zero. */
4377 if (TREE_CODE (rli->t) == UNION_TYPE)
4378 break;
4379 if (layout_conflict_p (field_p ? type : binfo, offset,
4380 offsets, field_p))
4382 /* Strip off the size allocated to this field. That puts us
4383 at the first place we could have put the field with
4384 proper alignment. */
4385 *rli = old_rli;
4387 /* Bump up by the alignment required for the type. */
4388 rli->bitpos
4389 = size_binop (PLUS_EXPR, rli->bitpos,
4390 bitsize_int (binfo
4391 ? CLASSTYPE_ALIGN (type)
4392 : TYPE_ALIGN (type)));
4393 normalize_rli (rli);
4395 else if (TREE_CODE (type) == NULLPTR_TYPE
4396 && warn_abi && abi_version_crosses (9))
4398 /* Before ABI v9, we were giving nullptr_t alignment of 1; if
4399 the offset wasn't aligned like a pointer when we started to
4400 layout this field, that affects its position. */
4401 tree pos = rli_size_unit_so_far (&old_rli);
4402 if (int_cst_value (pos) % TYPE_ALIGN_UNIT (ptr_type_node) != 0)
4404 if (abi_version_at_least (9))
4405 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wabi,
4406 "alignment of %qD increased in -fabi-version=9 "
4407 "(GCC 5.2)", decl);
4408 else
4409 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wabi, "alignment "
4410 "of %qD will increase in -fabi-version=9", decl);
4412 break;
4414 else
4415 /* There was no conflict. We're done laying out this field. */
4416 break;
4419 /* Now that we know where it will be placed, update its
4420 BINFO_OFFSET. */
4421 if (binfo && CLASS_TYPE_P (BINFO_TYPE (binfo)))
4422 /* Indirect virtual bases may have a nonzero BINFO_OFFSET at
4423 this point because their BINFO_OFFSET is copied from another
4424 hierarchy. Therefore, we may not need to add the entire
4425 OFFSET. */
4426 propagate_binfo_offsets (binfo,
4427 size_diffop_loc (input_location,
4428 fold_convert (ssizetype, offset),
4429 fold_convert (ssizetype,
4430 BINFO_OFFSET (binfo))));
4433 /* Returns true if TYPE is empty and OFFSET is nonzero. */
4435 static int
4436 empty_base_at_nonzero_offset_p (tree type,
4437 tree offset,
4438 splay_tree /*offsets*/)
4440 return is_empty_class (type) && !integer_zerop (offset);
4443 /* Layout the empty base BINFO. EOC indicates the byte currently just
4444 past the end of the class, and should be correctly aligned for a
4445 class of the type indicated by BINFO; OFFSETS gives the offsets of
4446 the empty bases allocated so far. T is the most derived
4447 type. Return nonzero iff we added it at the end. */
4449 static bool
4450 layout_empty_base (record_layout_info rli, tree binfo,
4451 tree eoc, splay_tree offsets)
4453 tree alignment;
4454 tree basetype = BINFO_TYPE (binfo);
4455 bool atend = false;
4457 /* This routine should only be used for empty classes. */
4458 gcc_assert (is_empty_class (basetype));
4459 alignment = ssize_int (CLASSTYPE_ALIGN_UNIT (basetype));
4461 if (!integer_zerop (BINFO_OFFSET (binfo)))
4462 propagate_binfo_offsets
4463 (binfo, size_diffop_loc (input_location,
4464 size_zero_node, BINFO_OFFSET (binfo)));
4466 /* This is an empty base class. We first try to put it at offset
4467 zero. */
4468 if (layout_conflict_p (binfo,
4469 BINFO_OFFSET (binfo),
4470 offsets,
4471 /*vbases_p=*/0))
4473 /* That didn't work. Now, we move forward from the next
4474 available spot in the class. */
4475 atend = true;
4476 propagate_binfo_offsets (binfo, fold_convert (ssizetype, eoc));
4477 while (1)
4479 if (!layout_conflict_p (binfo,
4480 BINFO_OFFSET (binfo),
4481 offsets,
4482 /*vbases_p=*/0))
4483 /* We finally found a spot where there's no overlap. */
4484 break;
4486 /* There's overlap here, too. Bump along to the next spot. */
4487 propagate_binfo_offsets (binfo, alignment);
4491 if (CLASSTYPE_USER_ALIGN (basetype))
4493 rli->record_align = MAX (rli->record_align, CLASSTYPE_ALIGN (basetype));
4494 if (warn_packed)
4495 rli->unpacked_align = MAX (rli->unpacked_align, CLASSTYPE_ALIGN (basetype));
4496 TYPE_USER_ALIGN (rli->t) = 1;
4499 return atend;
4502 /* Build the FIELD_DECL for BASETYPE as a base of T, add it to the chain of
4503 fields at NEXT_FIELD, and return it. */
4505 static tree
4506 build_base_field_1 (tree t, tree basetype, tree *&next_field)
4508 /* Create the FIELD_DECL. */
4509 gcc_assert (CLASSTYPE_AS_BASE (basetype));
4510 tree decl = build_decl (input_location,
4511 FIELD_DECL, NULL_TREE, CLASSTYPE_AS_BASE (basetype));
4512 DECL_ARTIFICIAL (decl) = 1;
4513 DECL_IGNORED_P (decl) = 1;
4514 DECL_FIELD_CONTEXT (decl) = t;
4515 DECL_SIZE (decl) = CLASSTYPE_SIZE (basetype);
4516 DECL_SIZE_UNIT (decl) = CLASSTYPE_SIZE_UNIT (basetype);
4517 SET_DECL_ALIGN (decl, CLASSTYPE_ALIGN (basetype));
4518 DECL_USER_ALIGN (decl) = CLASSTYPE_USER_ALIGN (basetype);
4519 SET_DECL_MODE (decl, TYPE_MODE (basetype));
4520 DECL_FIELD_IS_BASE (decl) = 1;
4522 /* Add the new FIELD_DECL to the list of fields for T. */
4523 DECL_CHAIN (decl) = *next_field;
4524 *next_field = decl;
4525 next_field = &DECL_CHAIN (decl);
4527 return decl;
4530 /* Layout the base given by BINFO in the class indicated by RLI.
4531 *BASE_ALIGN is a running maximum of the alignments of
4532 any base class. OFFSETS gives the location of empty base
4533 subobjects. T is the most derived type. Return nonzero if the new
4534 object cannot be nearly-empty. A new FIELD_DECL is inserted at
4535 *NEXT_FIELD, unless BINFO is for an empty base class.
4537 Returns the location at which the next field should be inserted. */
4539 static tree *
4540 build_base_field (record_layout_info rli, tree binfo,
4541 splay_tree offsets, tree *next_field)
4543 tree t = rli->t;
4544 tree basetype = BINFO_TYPE (binfo);
4546 if (!COMPLETE_TYPE_P (basetype))
4547 /* This error is now reported in xref_tag, thus giving better
4548 location information. */
4549 return next_field;
4551 /* Place the base class. */
4552 if (!is_empty_class (basetype))
4554 tree decl;
4556 /* The containing class is non-empty because it has a non-empty
4557 base class. */
4558 CLASSTYPE_EMPTY_P (t) = 0;
4560 /* Create the FIELD_DECL. */
4561 decl = build_base_field_1 (t, basetype, next_field);
4563 /* Try to place the field. It may take more than one try if we
4564 have a hard time placing the field without putting two
4565 objects of the same type at the same address. */
4566 layout_nonempty_base_or_field (rli, decl, binfo, offsets);
4568 else
4570 tree eoc;
4571 bool atend;
4573 /* On some platforms (ARM), even empty classes will not be
4574 byte-aligned. */
4575 eoc = round_up_loc (input_location,
4576 rli_size_unit_so_far (rli),
4577 CLASSTYPE_ALIGN_UNIT (basetype));
4578 atend = layout_empty_base (rli, binfo, eoc, offsets);
4579 /* A nearly-empty class "has no proper base class that is empty,
4580 not morally virtual, and at an offset other than zero." */
4581 if (!BINFO_VIRTUAL_P (binfo) && CLASSTYPE_NEARLY_EMPTY_P (t))
4583 if (atend)
4584 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
4585 /* The check above (used in G++ 3.2) is insufficient because
4586 an empty class placed at offset zero might itself have an
4587 empty base at a nonzero offset. */
4588 else if (walk_subobject_offsets (basetype,
4589 empty_base_at_nonzero_offset_p,
4590 size_zero_node,
4591 /*offsets=*/NULL,
4592 /*max_offset=*/NULL_TREE,
4593 /*vbases_p=*/true))
4594 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
4597 /* We used to not create a FIELD_DECL for empty base classes because of
4598 back end issues with overlapping FIELD_DECLs, but that doesn't seem to
4599 be a problem anymore. We need them to handle initialization of C++17
4600 aggregate bases. */
4601 if (cxx_dialect >= cxx1z && !BINFO_VIRTUAL_P (binfo))
4603 tree decl = build_base_field_1 (t, basetype, next_field);
4604 DECL_FIELD_OFFSET (decl) = BINFO_OFFSET (binfo);
4605 DECL_FIELD_BIT_OFFSET (decl) = bitsize_zero_node;
4606 SET_DECL_OFFSET_ALIGN (decl, BITS_PER_UNIT);
4609 /* An empty virtual base causes a class to be non-empty
4610 -- but in that case we do not need to clear CLASSTYPE_EMPTY_P
4611 here because that was already done when the virtual table
4612 pointer was created. */
4615 /* Record the offsets of BINFO and its base subobjects. */
4616 record_subobject_offsets (binfo,
4617 BINFO_OFFSET (binfo),
4618 offsets,
4619 /*is_data_member=*/false);
4621 return next_field;
4624 /* Layout all of the non-virtual base classes. Record empty
4625 subobjects in OFFSETS. T is the most derived type. Return nonzero
4626 if the type cannot be nearly empty. The fields created
4627 corresponding to the base classes will be inserted at
4628 *NEXT_FIELD. */
4630 static void
4631 build_base_fields (record_layout_info rli,
4632 splay_tree offsets, tree *next_field)
4634 /* Chain to hold all the new FIELD_DECLs which stand in for base class
4635 subobjects. */
4636 tree t = rli->t;
4637 int n_baseclasses = BINFO_N_BASE_BINFOS (TYPE_BINFO (t));
4638 int i;
4640 /* The primary base class is always allocated first. */
4641 if (CLASSTYPE_HAS_PRIMARY_BASE_P (t))
4642 next_field = build_base_field (rli, CLASSTYPE_PRIMARY_BINFO (t),
4643 offsets, next_field);
4645 /* Now allocate the rest of the bases. */
4646 for (i = 0; i < n_baseclasses; ++i)
4648 tree base_binfo;
4650 base_binfo = BINFO_BASE_BINFO (TYPE_BINFO (t), i);
4652 /* The primary base was already allocated above, so we don't
4653 need to allocate it again here. */
4654 if (base_binfo == CLASSTYPE_PRIMARY_BINFO (t))
4655 continue;
4657 /* Virtual bases are added at the end (a primary virtual base
4658 will have already been added). */
4659 if (BINFO_VIRTUAL_P (base_binfo))
4660 continue;
4662 next_field = build_base_field (rli, base_binfo,
4663 offsets, next_field);
4667 /* Go through the TYPE_METHODS of T issuing any appropriate
4668 diagnostics, figuring out which methods override which other
4669 methods, and so forth. */
4671 static void
4672 check_methods (tree t)
4674 tree x;
4676 for (x = TYPE_METHODS (t); x; x = DECL_CHAIN (x))
4678 check_for_override (x, t);
4679 if (DECL_PURE_VIRTUAL_P (x) && (TREE_CODE (x) != FUNCTION_DECL || ! DECL_VINDEX (x)))
4680 error ("initializer specified for non-virtual method %q+D", x);
4681 /* The name of the field is the original field name
4682 Save this in auxiliary field for later overloading. */
4683 if (TREE_CODE (x) == FUNCTION_DECL && DECL_VINDEX (x))
4685 TYPE_POLYMORPHIC_P (t) = 1;
4686 if (DECL_PURE_VIRTUAL_P (x))
4687 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", DECL_ATTRIBUTES (x)))
4696 error_at (DECL_SOURCE_LOCATION (x),
4697 "%<transaction_safe_dynamic%> may only be specified for "
4698 "a virtual function");
4702 /* FN is a constructor or destructor. Clone the declaration to create
4703 a specialized in-charge or not-in-charge version, as indicated by
4704 NAME. */
4706 static tree
4707 build_clone (tree fn, tree name)
4709 tree parms;
4710 tree clone;
4712 /* Copy the function. */
4713 clone = copy_decl (fn);
4714 /* Reset the function name. */
4715 DECL_NAME (clone) = name;
4716 /* Remember where this function came from. */
4717 DECL_ABSTRACT_ORIGIN (clone) = fn;
4718 /* Make it easy to find the CLONE given the FN. */
4719 DECL_CHAIN (clone) = DECL_CHAIN (fn);
4720 DECL_CHAIN (fn) = clone;
4722 /* If this is a template, do the rest on the DECL_TEMPLATE_RESULT. */
4723 if (TREE_CODE (clone) == TEMPLATE_DECL)
4725 tree result = build_clone (DECL_TEMPLATE_RESULT (clone), name);
4726 DECL_TEMPLATE_RESULT (clone) = result;
4727 DECL_TEMPLATE_INFO (result) = copy_node (DECL_TEMPLATE_INFO (result));
4728 DECL_TI_TEMPLATE (result) = clone;
4729 TREE_TYPE (clone) = TREE_TYPE (result);
4730 return clone;
4732 else
4734 // Clone constraints.
4735 if (flag_concepts)
4736 if (tree ci = get_constraints (fn))
4737 set_constraints (clone, copy_node (ci));
4741 SET_DECL_ASSEMBLER_NAME (clone, NULL_TREE);
4742 DECL_CLONED_FUNCTION (clone) = fn;
4743 /* There's no pending inline data for this function. */
4744 DECL_PENDING_INLINE_INFO (clone) = NULL;
4745 DECL_PENDING_INLINE_P (clone) = 0;
4747 /* The base-class destructor is not virtual. */
4748 if (name == base_dtor_identifier)
4750 DECL_VIRTUAL_P (clone) = 0;
4751 if (TREE_CODE (clone) != TEMPLATE_DECL)
4752 DECL_VINDEX (clone) = NULL_TREE;
4755 bool ctor_omit_inherited_parms_p = ctor_omit_inherited_parms (clone);
4756 if (ctor_omit_inherited_parms_p)
4757 gcc_assert (DECL_HAS_IN_CHARGE_PARM_P (clone));
4759 /* If there was an in-charge parameter, drop it from the function
4760 type. */
4761 if (DECL_HAS_IN_CHARGE_PARM_P (clone))
4763 tree basetype;
4764 tree parmtypes;
4765 tree exceptions;
4767 exceptions = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (clone));
4768 basetype = TYPE_METHOD_BASETYPE (TREE_TYPE (clone));
4769 parmtypes = TYPE_ARG_TYPES (TREE_TYPE (clone));
4770 /* Skip the `this' parameter. */
4771 parmtypes = TREE_CHAIN (parmtypes);
4772 /* Skip the in-charge parameter. */
4773 parmtypes = TREE_CHAIN (parmtypes);
4774 /* And the VTT parm, in a complete [cd]tor. */
4775 if (DECL_HAS_VTT_PARM_P (fn)
4776 && ! DECL_NEEDS_VTT_PARM_P (clone))
4777 parmtypes = TREE_CHAIN (parmtypes);
4778 if (ctor_omit_inherited_parms_p)
4780 /* If we're omitting inherited parms, that just leaves the VTT. */
4781 gcc_assert (DECL_NEEDS_VTT_PARM_P (clone));
4782 parmtypes = tree_cons (NULL_TREE, vtt_parm_type, void_list_node);
4784 TREE_TYPE (clone)
4785 = build_method_type_directly (basetype,
4786 TREE_TYPE (TREE_TYPE (clone)),
4787 parmtypes);
4788 if (exceptions)
4789 TREE_TYPE (clone) = build_exception_variant (TREE_TYPE (clone),
4790 exceptions);
4791 TREE_TYPE (clone)
4792 = cp_build_type_attribute_variant (TREE_TYPE (clone),
4793 TYPE_ATTRIBUTES (TREE_TYPE (fn)));
4796 /* Copy the function parameters. */
4797 DECL_ARGUMENTS (clone) = copy_list (DECL_ARGUMENTS (clone));
4798 /* Remove the in-charge parameter. */
4799 if (DECL_HAS_IN_CHARGE_PARM_P (clone))
4801 DECL_CHAIN (DECL_ARGUMENTS (clone))
4802 = DECL_CHAIN (DECL_CHAIN (DECL_ARGUMENTS (clone)));
4803 DECL_HAS_IN_CHARGE_PARM_P (clone) = 0;
4805 /* And the VTT parm, in a complete [cd]tor. */
4806 if (DECL_HAS_VTT_PARM_P (fn))
4808 if (DECL_NEEDS_VTT_PARM_P (clone))
4809 DECL_HAS_VTT_PARM_P (clone) = 1;
4810 else
4812 DECL_CHAIN (DECL_ARGUMENTS (clone))
4813 = DECL_CHAIN (DECL_CHAIN (DECL_ARGUMENTS (clone)));
4814 DECL_HAS_VTT_PARM_P (clone) = 0;
4818 /* A base constructor inheriting from a virtual base doesn't get the
4819 arguments. */
4820 if (ctor_omit_inherited_parms_p)
4821 DECL_CHAIN (DECL_CHAIN (DECL_ARGUMENTS (clone))) = NULL_TREE;
4823 for (parms = DECL_ARGUMENTS (clone); parms; parms = DECL_CHAIN (parms))
4825 DECL_CONTEXT (parms) = clone;
4826 cxx_dup_lang_specific_decl (parms);
4829 /* Create the RTL for this function. */
4830 SET_DECL_RTL (clone, NULL);
4831 rest_of_decl_compilation (clone, /*top_level=*/1, at_eof);
4833 return clone;
4836 /* Implementation of DECL_CLONED_FUNCTION and DECL_CLONED_FUNCTION_P, do
4837 not invoke this function directly.
4839 For a non-thunk function, returns the address of the slot for storing
4840 the function it is a clone of. Otherwise returns NULL_TREE.
4842 If JUST_TESTING, looks through TEMPLATE_DECL and returns NULL if
4843 cloned_function is unset. This is to support the separate
4844 DECL_CLONED_FUNCTION and DECL_CLONED_FUNCTION_P modes; using the latter
4845 on a template makes sense, but not the former. */
4847 tree *
4848 decl_cloned_function_p (const_tree decl, bool just_testing)
4850 tree *ptr;
4851 if (just_testing)
4852 decl = STRIP_TEMPLATE (decl);
4854 if (TREE_CODE (decl) != FUNCTION_DECL
4855 || !DECL_LANG_SPECIFIC (decl)
4856 || DECL_LANG_SPECIFIC (decl)->u.fn.thunk_p)
4858 #if defined ENABLE_TREE_CHECKING && (GCC_VERSION >= 2007)
4859 if (!just_testing)
4860 lang_check_failed (__FILE__, __LINE__, __FUNCTION__);
4861 else
4862 #endif
4863 return NULL;
4866 ptr = &DECL_LANG_SPECIFIC (decl)->u.fn.u5.cloned_function;
4867 if (just_testing && *ptr == NULL_TREE)
4868 return NULL;
4869 else
4870 return ptr;
4873 /* Produce declarations for all appropriate clones of FN. If
4874 UPDATE_METHODS is true, the clones are added to the
4875 CLASSTYPE_METHOD_VEC. */
4877 void
4878 clone_function_decl (tree fn, bool update_methods)
4880 tree clone;
4882 /* Avoid inappropriate cloning. */
4883 if (DECL_CHAIN (fn)
4884 && DECL_CLONED_FUNCTION_P (DECL_CHAIN (fn)))
4885 return;
4887 if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (fn))
4889 /* For each constructor, we need two variants: an in-charge version
4890 and a not-in-charge version. */
4891 clone = build_clone (fn, complete_ctor_identifier);
4892 if (update_methods)
4893 add_method (DECL_CONTEXT (clone), clone, false);
4894 clone = build_clone (fn, base_ctor_identifier);
4895 if (update_methods)
4896 add_method (DECL_CONTEXT (clone), clone, false);
4898 else
4900 gcc_assert (DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (fn));
4902 /* For each destructor, we need three variants: an in-charge
4903 version, a not-in-charge version, and an in-charge deleting
4904 version. We clone the deleting version first because that
4905 means it will go second on the TYPE_METHODS list -- and that
4906 corresponds to the correct layout order in the virtual
4907 function table.
4909 For a non-virtual destructor, we do not build a deleting
4910 destructor. */
4911 if (DECL_VIRTUAL_P (fn))
4913 clone = build_clone (fn, deleting_dtor_identifier);
4914 if (update_methods)
4915 add_method (DECL_CONTEXT (clone), clone, false);
4917 clone = build_clone (fn, complete_dtor_identifier);
4918 if (update_methods)
4919 add_method (DECL_CONTEXT (clone), clone, false);
4920 clone = build_clone (fn, base_dtor_identifier);
4921 if (update_methods)
4922 add_method (DECL_CONTEXT (clone), clone, false);
4925 /* Note that this is an abstract function that is never emitted. */
4926 DECL_ABSTRACT_P (fn) = true;
4929 /* DECL is an in charge constructor, which is being defined. This will
4930 have had an in class declaration, from whence clones were
4931 declared. An out-of-class definition can specify additional default
4932 arguments. As it is the clones that are involved in overload
4933 resolution, we must propagate the information from the DECL to its
4934 clones. */
4936 void
4937 adjust_clone_args (tree decl)
4939 tree clone;
4941 for (clone = DECL_CHAIN (decl); clone && DECL_CLONED_FUNCTION_P (clone);
4942 clone = DECL_CHAIN (clone))
4944 tree orig_clone_parms = TYPE_ARG_TYPES (TREE_TYPE (clone));
4945 tree orig_decl_parms = TYPE_ARG_TYPES (TREE_TYPE (decl));
4946 tree decl_parms, clone_parms;
4948 clone_parms = orig_clone_parms;
4950 /* Skip the 'this' parameter. */
4951 orig_clone_parms = TREE_CHAIN (orig_clone_parms);
4952 orig_decl_parms = TREE_CHAIN (orig_decl_parms);
4954 if (DECL_HAS_IN_CHARGE_PARM_P (decl))
4955 orig_decl_parms = TREE_CHAIN (orig_decl_parms);
4956 if (DECL_HAS_VTT_PARM_P (decl))
4957 orig_decl_parms = TREE_CHAIN (orig_decl_parms);
4959 clone_parms = orig_clone_parms;
4960 if (DECL_HAS_VTT_PARM_P (clone))
4961 clone_parms = TREE_CHAIN (clone_parms);
4963 for (decl_parms = orig_decl_parms; decl_parms;
4964 decl_parms = TREE_CHAIN (decl_parms),
4965 clone_parms = TREE_CHAIN (clone_parms))
4967 if (clone_parms == void_list_node)
4969 gcc_assert (decl_parms == clone_parms
4970 || ctor_omit_inherited_parms (clone));
4971 break;
4974 gcc_assert (same_type_p (TREE_TYPE (decl_parms),
4975 TREE_TYPE (clone_parms)));
4977 if (TREE_PURPOSE (decl_parms) && !TREE_PURPOSE (clone_parms))
4979 /* A default parameter has been added. Adjust the
4980 clone's parameters. */
4981 tree exceptions = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (clone));
4982 tree attrs = TYPE_ATTRIBUTES (TREE_TYPE (clone));
4983 tree basetype = TYPE_METHOD_BASETYPE (TREE_TYPE (clone));
4984 tree type;
4986 clone_parms = orig_decl_parms;
4988 if (DECL_HAS_VTT_PARM_P (clone))
4990 clone_parms = tree_cons (TREE_PURPOSE (orig_clone_parms),
4991 TREE_VALUE (orig_clone_parms),
4992 clone_parms);
4993 TREE_TYPE (clone_parms) = TREE_TYPE (orig_clone_parms);
4995 type = build_method_type_directly (basetype,
4996 TREE_TYPE (TREE_TYPE (clone)),
4997 clone_parms);
4998 if (exceptions)
4999 type = build_exception_variant (type, exceptions);
5000 if (attrs)
5001 type = cp_build_type_attribute_variant (type, attrs);
5002 TREE_TYPE (clone) = type;
5004 clone_parms = NULL_TREE;
5005 break;
5008 gcc_assert (!clone_parms || clone_parms == void_list_node);
5012 /* For each of the constructors and destructors in T, create an
5013 in-charge and not-in-charge variant. */
5015 static void
5016 clone_constructors_and_destructors (tree t)
5018 /* If for some reason we don't have a CLASSTYPE_METHOD_VEC, we bail
5019 out now. */
5020 if (!CLASSTYPE_METHOD_VEC (t))
5021 return;
5023 /* While constructors can be via a using declaration, at this point
5024 we no longer need to know that. */
5025 for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (t)); iter; ++iter)
5026 clone_function_decl (*iter, /*update_methods=*/true);
5028 if (tree dtor = CLASSTYPE_DESTRUCTOR (t))
5029 clone_function_decl (dtor, /*update_methods=*/true);
5032 /* Deduce noexcept for a destructor DTOR. */
5034 void
5035 deduce_noexcept_on_destructor (tree dtor)
5037 if (!TYPE_RAISES_EXCEPTIONS (TREE_TYPE (dtor)))
5038 TREE_TYPE (dtor) = build_exception_variant (TREE_TYPE (dtor),
5039 noexcept_deferred_spec);
5042 /* Subroutine of set_one_vmethod_tm_attributes. Search base classes
5043 of TYPE for virtual functions which FNDECL overrides. Return a
5044 mask of the tm attributes found therein. */
5046 static int
5047 look_for_tm_attr_overrides (tree type, tree fndecl)
5049 tree binfo = TYPE_BINFO (type);
5050 tree base_binfo;
5051 int ix, found = 0;
5053 for (ix = 0; BINFO_BASE_ITERATE (binfo, ix, base_binfo); ++ix)
5055 tree o, basetype = BINFO_TYPE (base_binfo);
5057 if (!TYPE_POLYMORPHIC_P (basetype))
5058 continue;
5060 o = look_for_overrides_here (basetype, fndecl);
5061 if (o)
5063 if (lookup_attribute ("transaction_safe_dynamic",
5064 DECL_ATTRIBUTES (o)))
5065 /* transaction_safe_dynamic is not inherited. */;
5066 else
5067 found |= tm_attr_to_mask (find_tm_attribute
5068 (TYPE_ATTRIBUTES (TREE_TYPE (o))));
5070 else
5071 found |= look_for_tm_attr_overrides (basetype, fndecl);
5074 return found;
5077 /* Subroutine of set_method_tm_attributes. Handle the checks and
5078 inheritance for one virtual method FNDECL. */
5080 static void
5081 set_one_vmethod_tm_attributes (tree type, tree fndecl)
5083 tree tm_attr;
5084 int found, have;
5086 found = look_for_tm_attr_overrides (type, fndecl);
5088 /* If FNDECL doesn't actually override anything (i.e. T is the
5089 class that first declares FNDECL virtual), then we're done. */
5090 if (found == 0)
5091 return;
5093 tm_attr = find_tm_attribute (TYPE_ATTRIBUTES (TREE_TYPE (fndecl)));
5094 have = tm_attr_to_mask (tm_attr);
5096 /* Intel STM Language Extension 3.0, Section 4.2 table 4:
5097 tm_pure must match exactly, otherwise no weakening of
5098 tm_safe > tm_callable > nothing. */
5099 /* ??? The tm_pure attribute didn't make the transition to the
5100 multivendor language spec. */
5101 if (have == TM_ATTR_PURE)
5103 if (found != TM_ATTR_PURE)
5105 found &= -found;
5106 goto err_override;
5109 /* If the overridden function is tm_pure, then FNDECL must be. */
5110 else if (found == TM_ATTR_PURE && tm_attr)
5111 goto err_override;
5112 /* Look for base class combinations that cannot be satisfied. */
5113 else if (found != TM_ATTR_PURE && (found & TM_ATTR_PURE))
5115 found &= ~TM_ATTR_PURE;
5116 found &= -found;
5117 error_at (DECL_SOURCE_LOCATION (fndecl),
5118 "method overrides both %<transaction_pure%> and %qE methods",
5119 tm_mask_to_attr (found));
5121 /* If FNDECL did not declare an attribute, then inherit the most
5122 restrictive one. */
5123 else if (tm_attr == NULL)
5125 apply_tm_attr (fndecl, tm_mask_to_attr (least_bit_hwi (found)));
5127 /* Otherwise validate that we're not weaker than a function
5128 that is being overridden. */
5129 else
5131 found &= -found;
5132 if (found <= TM_ATTR_CALLABLE && have > found)
5133 goto err_override;
5135 return;
5137 err_override:
5138 error_at (DECL_SOURCE_LOCATION (fndecl),
5139 "method declared %qE overriding %qE method",
5140 tm_attr, tm_mask_to_attr (found));
5143 /* For each of the methods in T, propagate a class-level tm attribute. */
5145 static void
5146 set_method_tm_attributes (tree t)
5148 tree class_tm_attr, fndecl;
5150 /* Don't bother collecting tm attributes if transactional memory
5151 support is not enabled. */
5152 if (!flag_tm)
5153 return;
5155 /* Process virtual methods first, as they inherit directly from the
5156 base virtual function and also require validation of new attributes. */
5157 if (TYPE_CONTAINS_VPTR_P (t))
5159 tree vchain;
5160 for (vchain = BINFO_VIRTUALS (TYPE_BINFO (t)); vchain;
5161 vchain = TREE_CHAIN (vchain))
5163 fndecl = BV_FN (vchain);
5164 if (DECL_THUNK_P (fndecl))
5165 fndecl = THUNK_TARGET (fndecl);
5166 set_one_vmethod_tm_attributes (t, fndecl);
5170 /* If the class doesn't have an attribute, nothing more to do. */
5171 class_tm_attr = find_tm_attribute (TYPE_ATTRIBUTES (t));
5172 if (class_tm_attr == NULL)
5173 return;
5175 /* Any method that does not yet have a tm attribute inherits
5176 the one from the class. */
5177 for (fndecl = TYPE_METHODS (t); fndecl; fndecl = TREE_CHAIN (fndecl))
5179 if (!find_tm_attribute (TYPE_ATTRIBUTES (TREE_TYPE (fndecl))))
5180 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_METHODS (t); fn; fn = DECL_CHAIN (fn))
5664 if (DECL_DECLARED_CONSTEXPR_P (fn)
5665 && TREE_CODE (fn) != TEMPLATE_DECL
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_METHODS (t); fn; fn = DECL_CHAIN (fn))
5928 if (!DECL_ARTIFICIAL (fn) && DECL_DEFAULTED_IN_CLASS_P (fn))
5930 int copy = copy_fn_p (fn);
5931 if (copy > 0)
5933 bool imp_const_p
5934 = (DECL_CONSTRUCTOR_P (fn) ? !cant_have_const_ctor
5935 : !no_const_asn_ref);
5936 bool fn_const_p = (copy == 2);
5938 if (fn_const_p && !imp_const_p)
5939 /* If the function is defaulted outside the class, we just
5940 give the synthesis error. */
5941 error ("%q+D declared to take const reference, but implicit "
5942 "declaration would take non-const", fn);
5944 defaulted_late_check (fn);
5947 if (LAMBDA_TYPE_P (t))
5949 /* "This class type is not an aggregate." */
5950 CLASSTYPE_NON_AGGREGATE (t) = 1;
5953 /* Compute the 'literal type' property before we
5954 do anything with non-static member functions. */
5955 finalize_literal_type_property (t);
5957 /* Create the in-charge and not-in-charge variants of constructors
5958 and destructors. */
5959 clone_constructors_and_destructors (t);
5961 /* Process the using-declarations. */
5962 for (; access_decls; access_decls = TREE_CHAIN (access_decls))
5963 handle_using_decl (TREE_VALUE (access_decls), t);
5965 /* Build and sort the CLASSTYPE_METHOD_VEC. */
5966 finish_struct_methods (t);
5968 /* Figure out whether or not we will need a cookie when dynamically
5969 allocating an array of this type. */
5970 LANG_TYPE_CLASS_CHECK (t)->vec_new_uses_cookie
5971 = type_requires_array_cookie (t);
5974 /* If T needs a pointer to its virtual function table, set TYPE_VFIELD
5975 accordingly. If a new vfield was created (because T doesn't have a
5976 primary base class), then the newly created field is returned. It
5977 is not added to the TYPE_FIELDS list; it is the caller's
5978 responsibility to do that. Accumulate declared virtual functions
5979 on VIRTUALS_P. */
5981 static tree
5982 create_vtable_ptr (tree t, tree* virtuals_p)
5984 tree fn;
5986 /* Collect the virtual functions declared in T. */
5987 for (fn = TYPE_METHODS (t); fn; fn = DECL_CHAIN (fn))
5988 if (TREE_CODE (fn) == FUNCTION_DECL
5989 && DECL_VINDEX (fn) && !DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (fn)
5990 && TREE_CODE (DECL_VINDEX (fn)) != INTEGER_CST)
5992 tree new_virtual = make_node (TREE_LIST);
5994 BV_FN (new_virtual) = fn;
5995 BV_DELTA (new_virtual) = integer_zero_node;
5996 BV_VCALL_INDEX (new_virtual) = NULL_TREE;
5998 TREE_CHAIN (new_virtual) = *virtuals_p;
5999 *virtuals_p = new_virtual;
6002 /* If we couldn't find an appropriate base class, create a new field
6003 here. Even if there weren't any new virtual functions, we might need a
6004 new virtual function table if we're supposed to include vptrs in
6005 all classes that need them. */
6006 if (!TYPE_VFIELD (t) && (*virtuals_p || TYPE_CONTAINS_VPTR_P (t)))
6008 /* We build this decl with vtbl_ptr_type_node, which is a
6009 `vtable_entry_type*'. It might seem more precise to use
6010 `vtable_entry_type (*)[N]' where N is the number of virtual
6011 functions. However, that would require the vtable pointer in
6012 base classes to have a different type than the vtable pointer
6013 in derived classes. We could make that happen, but that
6014 still wouldn't solve all the problems. In particular, the
6015 type-based alias analysis code would decide that assignments
6016 to the base class vtable pointer can't alias assignments to
6017 the derived class vtable pointer, since they have different
6018 types. Thus, in a derived class destructor, where the base
6019 class constructor was inlined, we could generate bad code for
6020 setting up the vtable pointer.
6022 Therefore, we use one type for all vtable pointers. We still
6023 use a type-correct type; it's just doesn't indicate the array
6024 bounds. That's better than using `void*' or some such; it's
6025 cleaner, and it let's the alias analysis code know that these
6026 stores cannot alias stores to void*! */
6027 tree field;
6029 field = build_decl (input_location,
6030 FIELD_DECL, get_vfield_name (t), vtbl_ptr_type_node);
6031 DECL_VIRTUAL_P (field) = 1;
6032 DECL_ARTIFICIAL (field) = 1;
6033 DECL_FIELD_CONTEXT (field) = t;
6034 DECL_FCONTEXT (field) = t;
6035 if (TYPE_PACKED (t))
6036 DECL_PACKED (field) = 1;
6038 TYPE_VFIELD (t) = field;
6040 /* This class is non-empty. */
6041 CLASSTYPE_EMPTY_P (t) = 0;
6043 return field;
6046 return NULL_TREE;
6049 /* Add OFFSET to all base types of BINFO which is a base in the
6050 hierarchy dominated by T.
6052 OFFSET, which is a type offset, is number of bytes. */
6054 static void
6055 propagate_binfo_offsets (tree binfo, tree offset)
6057 int i;
6058 tree primary_binfo;
6059 tree base_binfo;
6061 /* Update BINFO's offset. */
6062 BINFO_OFFSET (binfo)
6063 = fold_convert (sizetype,
6064 size_binop (PLUS_EXPR,
6065 fold_convert (ssizetype, BINFO_OFFSET (binfo)),
6066 offset));
6068 /* Find the primary base class. */
6069 primary_binfo = get_primary_binfo (binfo);
6071 if (primary_binfo && BINFO_INHERITANCE_CHAIN (primary_binfo) == binfo)
6072 propagate_binfo_offsets (primary_binfo, offset);
6074 /* Scan all of the bases, pushing the BINFO_OFFSET adjust
6075 downwards. */
6076 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
6078 /* Don't do the primary base twice. */
6079 if (base_binfo == primary_binfo)
6080 continue;
6082 if (BINFO_VIRTUAL_P (base_binfo))
6083 continue;
6085 propagate_binfo_offsets (base_binfo, offset);
6089 /* Set BINFO_OFFSET for all of the virtual bases for RLI->T. Update
6090 TYPE_ALIGN and TYPE_SIZE for T. OFFSETS gives the location of
6091 empty subobjects of T. */
6093 static void
6094 layout_virtual_bases (record_layout_info rli, splay_tree offsets)
6096 tree vbase;
6097 tree t = rli->t;
6098 tree *next_field;
6100 if (BINFO_N_BASE_BINFOS (TYPE_BINFO (t)) == 0)
6101 return;
6103 /* Find the last field. The artificial fields created for virtual
6104 bases will go after the last extant field to date. */
6105 next_field = &TYPE_FIELDS (t);
6106 while (*next_field)
6107 next_field = &DECL_CHAIN (*next_field);
6109 /* Go through the virtual bases, allocating space for each virtual
6110 base that is not already a primary base class. These are
6111 allocated in inheritance graph order. */
6112 for (vbase = TYPE_BINFO (t); vbase; vbase = TREE_CHAIN (vbase))
6114 if (!BINFO_VIRTUAL_P (vbase))
6115 continue;
6117 if (!BINFO_PRIMARY_P (vbase))
6119 /* This virtual base is not a primary base of any class in the
6120 hierarchy, so we have to add space for it. */
6121 next_field = build_base_field (rli, vbase,
6122 offsets, next_field);
6127 /* Returns the offset of the byte just past the end of the base class
6128 BINFO. */
6130 static tree
6131 end_of_base (tree binfo)
6133 tree size;
6135 if (!CLASSTYPE_AS_BASE (BINFO_TYPE (binfo)))
6136 size = TYPE_SIZE_UNIT (char_type_node);
6137 else if (is_empty_class (BINFO_TYPE (binfo)))
6138 /* An empty class has zero CLASSTYPE_SIZE_UNIT, but we need to
6139 allocate some space for it. It cannot have virtual bases, so
6140 TYPE_SIZE_UNIT is fine. */
6141 size = TYPE_SIZE_UNIT (BINFO_TYPE (binfo));
6142 else
6143 size = CLASSTYPE_SIZE_UNIT (BINFO_TYPE (binfo));
6145 return size_binop (PLUS_EXPR, BINFO_OFFSET (binfo), size);
6148 /* Returns the offset of the byte just past the end of the base class
6149 with the highest offset in T. If INCLUDE_VIRTUALS_P is zero, then
6150 only non-virtual bases are included. */
6152 static tree
6153 end_of_class (tree t, int include_virtuals_p)
6155 tree result = size_zero_node;
6156 vec<tree, va_gc> *vbases;
6157 tree binfo;
6158 tree base_binfo;
6159 tree offset;
6160 int i;
6162 for (binfo = TYPE_BINFO (t), i = 0;
6163 BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
6165 if (!include_virtuals_p
6166 && BINFO_VIRTUAL_P (base_binfo)
6167 && (!BINFO_PRIMARY_P (base_binfo)
6168 || BINFO_INHERITANCE_CHAIN (base_binfo) != TYPE_BINFO (t)))
6169 continue;
6171 offset = end_of_base (base_binfo);
6172 if (tree_int_cst_lt (result, offset))
6173 result = offset;
6176 if (include_virtuals_p)
6177 for (vbases = CLASSTYPE_VBASECLASSES (t), i = 0;
6178 vec_safe_iterate (vbases, i, &base_binfo); i++)
6180 offset = end_of_base (base_binfo);
6181 if (tree_int_cst_lt (result, offset))
6182 result = offset;
6185 return result;
6188 /* Warn about bases of T that are inaccessible because they are
6189 ambiguous. For example:
6191 struct S {};
6192 struct T : public S {};
6193 struct U : public S, public T {};
6195 Here, `(S*) new U' is not allowed because there are two `S'
6196 subobjects of U. */
6198 static void
6199 warn_about_ambiguous_bases (tree t)
6201 int i;
6202 vec<tree, va_gc> *vbases;
6203 tree basetype;
6204 tree binfo;
6205 tree base_binfo;
6207 /* If there are no repeated bases, nothing can be ambiguous. */
6208 if (!CLASSTYPE_REPEATED_BASE_P (t))
6209 return;
6211 /* Check direct bases. */
6212 for (binfo = TYPE_BINFO (t), i = 0;
6213 BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
6215 basetype = BINFO_TYPE (base_binfo);
6217 if (!uniquely_derived_from_p (basetype, t))
6218 warning (0, "direct base %qT inaccessible in %qT due to ambiguity",
6219 basetype, t);
6222 /* Check for ambiguous virtual bases. */
6223 if (extra_warnings)
6224 for (vbases = CLASSTYPE_VBASECLASSES (t), i = 0;
6225 vec_safe_iterate (vbases, i, &binfo); i++)
6227 basetype = BINFO_TYPE (binfo);
6229 if (!uniquely_derived_from_p (basetype, t))
6230 warning (OPT_Wextra, "virtual base %qT inaccessible in %qT due "
6231 "to ambiguity", basetype, t);
6235 /* Compare two INTEGER_CSTs K1 and K2. */
6237 static int
6238 splay_tree_compare_integer_csts (splay_tree_key k1, splay_tree_key k2)
6240 return tree_int_cst_compare ((tree) k1, (tree) k2);
6243 /* Increase the size indicated in RLI to account for empty classes
6244 that are "off the end" of the class. */
6246 static void
6247 include_empty_classes (record_layout_info rli)
6249 tree eoc;
6250 tree rli_size;
6252 /* It might be the case that we grew the class to allocate a
6253 zero-sized base class. That won't be reflected in RLI, yet,
6254 because we are willing to overlay multiple bases at the same
6255 offset. However, now we need to make sure that RLI is big enough
6256 to reflect the entire class. */
6257 eoc = end_of_class (rli->t, CLASSTYPE_AS_BASE (rli->t) != NULL_TREE);
6258 rli_size = rli_size_unit_so_far (rli);
6259 if (TREE_CODE (rli_size) == INTEGER_CST
6260 && tree_int_cst_lt (rli_size, eoc))
6262 /* The size should have been rounded to a whole byte. */
6263 gcc_assert (tree_int_cst_equal
6264 (rli->bitpos, round_down (rli->bitpos, BITS_PER_UNIT)));
6265 rli->bitpos
6266 = size_binop (PLUS_EXPR,
6267 rli->bitpos,
6268 size_binop (MULT_EXPR,
6269 fold_convert (bitsizetype,
6270 size_binop (MINUS_EXPR,
6271 eoc, rli_size)),
6272 bitsize_int (BITS_PER_UNIT)));
6273 normalize_rli (rli);
6277 /* Calculate the TYPE_SIZE, TYPE_ALIGN, etc for T. Calculate
6278 BINFO_OFFSETs for all of the base-classes. Position the vtable
6279 pointer. Accumulate declared virtual functions on VIRTUALS_P. */
6281 static void
6282 layout_class_type (tree t, tree *virtuals_p)
6284 tree non_static_data_members;
6285 tree field;
6286 tree vptr;
6287 record_layout_info rli;
6288 /* Maps offsets (represented as INTEGER_CSTs) to a TREE_LIST of
6289 types that appear at that offset. */
6290 splay_tree empty_base_offsets;
6291 /* True if the last field laid out was a bit-field. */
6292 bool last_field_was_bitfield = false;
6293 /* The location at which the next field should be inserted. */
6294 tree *next_field;
6295 /* T, as a base class. */
6296 tree base_t;
6298 /* Keep track of the first non-static data member. */
6299 non_static_data_members = TYPE_FIELDS (t);
6301 /* Start laying out the record. */
6302 rli = start_record_layout (t);
6304 /* Mark all the primary bases in the hierarchy. */
6305 determine_primary_bases (t);
6307 /* Create a pointer to our virtual function table. */
6308 vptr = create_vtable_ptr (t, virtuals_p);
6310 /* The vptr is always the first thing in the class. */
6311 if (vptr)
6313 DECL_CHAIN (vptr) = TYPE_FIELDS (t);
6314 TYPE_FIELDS (t) = vptr;
6315 next_field = &DECL_CHAIN (vptr);
6316 place_field (rli, vptr);
6318 else
6319 next_field = &TYPE_FIELDS (t);
6321 /* Build FIELD_DECLs for all of the non-virtual base-types. */
6322 empty_base_offsets = splay_tree_new (splay_tree_compare_integer_csts,
6323 NULL, NULL);
6324 build_base_fields (rli, empty_base_offsets, next_field);
6326 /* Layout the non-static data members. */
6327 for (field = non_static_data_members; field; field = DECL_CHAIN (field))
6329 tree type;
6330 tree padding;
6332 /* We still pass things that aren't non-static data members to
6333 the back end, in case it wants to do something with them. */
6334 if (TREE_CODE (field) != FIELD_DECL)
6336 place_field (rli, field);
6337 /* If the static data member has incomplete type, keep track
6338 of it so that it can be completed later. (The handling
6339 of pending statics in finish_record_layout is
6340 insufficient; consider:
6342 struct S1;
6343 struct S2 { static S1 s1; };
6345 At this point, finish_record_layout will be called, but
6346 S1 is still incomplete.) */
6347 if (VAR_P (field))
6349 maybe_register_incomplete_var (field);
6350 /* The visibility of static data members is determined
6351 at their point of declaration, not their point of
6352 definition. */
6353 determine_visibility (field);
6355 continue;
6358 type = TREE_TYPE (field);
6359 if (type == error_mark_node)
6360 continue;
6362 padding = NULL_TREE;
6364 /* If this field is a bit-field whose width is greater than its
6365 type, then there are some special rules for allocating
6366 it. */
6367 if (DECL_C_BIT_FIELD (field)
6368 && tree_int_cst_lt (TYPE_SIZE (type), DECL_SIZE (field)))
6370 bool was_unnamed_p = false;
6371 /* We must allocate the bits as if suitably aligned for the
6372 longest integer type that fits in this many bits. Then,
6373 we are supposed to use the left over bits as additional
6374 padding. */
6376 /* Do not pick a type bigger than MAX_FIXED_MODE_SIZE. */
6377 tree limit = size_int (MAX_FIXED_MODE_SIZE);
6378 if (tree_int_cst_lt (DECL_SIZE (field), limit))
6379 limit = DECL_SIZE (field);
6381 tree integer_type = integer_types[itk_char];
6382 for (unsigned itk = itk_char; itk != itk_none; itk++)
6383 if (tree next = integer_types[itk])
6385 if (tree_int_cst_lt (limit, TYPE_SIZE (next)))
6386 /* Too big, so our current guess is what we want. */
6387 break;
6388 /* Not bigger than limit, ok */
6389 integer_type = next;
6392 /* Figure out how much additional padding is required. */
6393 if (TREE_CODE (t) == UNION_TYPE)
6394 /* In a union, the padding field must have the full width
6395 of the bit-field; all fields start at offset zero. */
6396 padding = DECL_SIZE (field);
6397 else
6398 padding = size_binop (MINUS_EXPR, DECL_SIZE (field),
6399 TYPE_SIZE (integer_type));
6401 if (integer_zerop (padding))
6402 padding = NULL_TREE;
6404 /* An unnamed bitfield does not normally affect the
6405 alignment of the containing class on a target where
6406 PCC_BITFIELD_TYPE_MATTERS. But, the C++ ABI does not
6407 make any exceptions for unnamed bitfields when the
6408 bitfields are longer than their types. Therefore, we
6409 temporarily give the field a name. */
6410 if (PCC_BITFIELD_TYPE_MATTERS && !DECL_NAME (field))
6412 was_unnamed_p = true;
6413 DECL_NAME (field) = make_anon_name ();
6416 DECL_SIZE (field) = TYPE_SIZE (integer_type);
6417 SET_DECL_ALIGN (field, TYPE_ALIGN (integer_type));
6418 DECL_USER_ALIGN (field) = TYPE_USER_ALIGN (integer_type);
6419 layout_nonempty_base_or_field (rli, field, NULL_TREE,
6420 empty_base_offsets);
6421 if (was_unnamed_p)
6422 DECL_NAME (field) = NULL_TREE;
6423 /* Now that layout has been performed, set the size of the
6424 field to the size of its declared type; the rest of the
6425 field is effectively invisible. */
6426 DECL_SIZE (field) = TYPE_SIZE (type);
6427 /* We must also reset the DECL_MODE of the field. */
6428 SET_DECL_MODE (field, TYPE_MODE (type));
6430 else
6431 layout_nonempty_base_or_field (rli, field, NULL_TREE,
6432 empty_base_offsets);
6434 /* Remember the location of any empty classes in FIELD. */
6435 record_subobject_offsets (TREE_TYPE (field),
6436 byte_position(field),
6437 empty_base_offsets,
6438 /*is_data_member=*/true);
6440 /* If a bit-field does not immediately follow another bit-field,
6441 and yet it starts in the middle of a byte, we have failed to
6442 comply with the ABI. */
6443 if (warn_abi
6444 && DECL_C_BIT_FIELD (field)
6445 /* The TREE_NO_WARNING flag gets set by Objective-C when
6446 laying out an Objective-C class. The ObjC ABI differs
6447 from the C++ ABI, and so we do not want a warning
6448 here. */
6449 && !TREE_NO_WARNING (field)
6450 && !last_field_was_bitfield
6451 && !integer_zerop (size_binop (TRUNC_MOD_EXPR,
6452 DECL_FIELD_BIT_OFFSET (field),
6453 bitsize_unit_node)))
6454 warning_at (DECL_SOURCE_LOCATION (field), OPT_Wabi,
6455 "offset of %qD is not ABI-compliant and may "
6456 "change in a future version of GCC", field);
6458 /* The middle end uses the type of expressions to determine the
6459 possible range of expression values. In order to optimize
6460 "x.i > 7" to "false" for a 2-bit bitfield "i", the middle end
6461 must be made aware of the width of "i", via its type.
6463 Because C++ does not have integer types of arbitrary width,
6464 we must (for the purposes of the front end) convert from the
6465 type assigned here to the declared type of the bitfield
6466 whenever a bitfield expression is used as an rvalue.
6467 Similarly, when assigning a value to a bitfield, the value
6468 must be converted to the type given the bitfield here. */
6469 if (DECL_C_BIT_FIELD (field))
6471 unsigned HOST_WIDE_INT width;
6472 tree ftype = TREE_TYPE (field);
6473 width = tree_to_uhwi (DECL_SIZE (field));
6474 if (width != TYPE_PRECISION (ftype))
6476 TREE_TYPE (field)
6477 = c_build_bitfield_integer_type (width,
6478 TYPE_UNSIGNED (ftype));
6479 TREE_TYPE (field)
6480 = cp_build_qualified_type (TREE_TYPE (field),
6481 cp_type_quals (ftype));
6485 /* If we needed additional padding after this field, add it
6486 now. */
6487 if (padding)
6489 tree padding_field;
6491 padding_field = build_decl (input_location,
6492 FIELD_DECL,
6493 NULL_TREE,
6494 char_type_node);
6495 DECL_BIT_FIELD (padding_field) = 1;
6496 DECL_SIZE (padding_field) = padding;
6497 DECL_CONTEXT (padding_field) = t;
6498 DECL_ARTIFICIAL (padding_field) = 1;
6499 DECL_IGNORED_P (padding_field) = 1;
6500 layout_nonempty_base_or_field (rli, padding_field,
6501 NULL_TREE,
6502 empty_base_offsets);
6505 last_field_was_bitfield = DECL_C_BIT_FIELD (field);
6508 if (!integer_zerop (rli->bitpos))
6510 /* Make sure that we are on a byte boundary so that the size of
6511 the class without virtual bases will always be a round number
6512 of bytes. */
6513 rli->bitpos = round_up_loc (input_location, rli->bitpos, BITS_PER_UNIT);
6514 normalize_rli (rli);
6517 /* Delete all zero-width bit-fields from the list of fields. Now
6518 that the type is laid out they are no longer important. */
6519 remove_zero_width_bit_fields (t);
6521 /* Create the version of T used for virtual bases. We do not use
6522 make_class_type for this version; this is an artificial type. For
6523 a POD type, we just reuse T. */
6524 if (CLASSTYPE_NON_LAYOUT_POD_P (t) || CLASSTYPE_EMPTY_P (t))
6526 base_t = make_node (TREE_CODE (t));
6528 /* Set the size and alignment for the new type. */
6529 tree eoc;
6531 /* If the ABI version is not at least two, and the last
6532 field was a bit-field, RLI may not be on a byte
6533 boundary. In particular, rli_size_unit_so_far might
6534 indicate the last complete byte, while rli_size_so_far
6535 indicates the total number of bits used. Therefore,
6536 rli_size_so_far, rather than rli_size_unit_so_far, is
6537 used to compute TYPE_SIZE_UNIT. */
6538 eoc = end_of_class (t, /*include_virtuals_p=*/0);
6539 TYPE_SIZE_UNIT (base_t)
6540 = size_binop (MAX_EXPR,
6541 fold_convert (sizetype,
6542 size_binop (CEIL_DIV_EXPR,
6543 rli_size_so_far (rli),
6544 bitsize_int (BITS_PER_UNIT))),
6545 eoc);
6546 TYPE_SIZE (base_t)
6547 = size_binop (MAX_EXPR,
6548 rli_size_so_far (rli),
6549 size_binop (MULT_EXPR,
6550 fold_convert (bitsizetype, eoc),
6551 bitsize_int (BITS_PER_UNIT)));
6552 SET_TYPE_ALIGN (base_t, rli->record_align);
6553 TYPE_USER_ALIGN (base_t) = TYPE_USER_ALIGN (t);
6555 /* Copy the fields from T. */
6556 next_field = &TYPE_FIELDS (base_t);
6557 for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
6558 if (TREE_CODE (field) == FIELD_DECL)
6560 *next_field = copy_node (field);
6561 DECL_CONTEXT (*next_field) = base_t;
6562 next_field = &DECL_CHAIN (*next_field);
6564 *next_field = NULL_TREE;
6566 /* Record the base version of the type. */
6567 CLASSTYPE_AS_BASE (t) = base_t;
6568 TYPE_CONTEXT (base_t) = t;
6570 else
6571 CLASSTYPE_AS_BASE (t) = t;
6573 /* Every empty class contains an empty class. */
6574 if (CLASSTYPE_EMPTY_P (t))
6575 CLASSTYPE_CONTAINS_EMPTY_CLASS_P (t) = 1;
6577 /* Set the TYPE_DECL for this type to contain the right
6578 value for DECL_OFFSET, so that we can use it as part
6579 of a COMPONENT_REF for multiple inheritance. */
6580 layout_decl (TYPE_MAIN_DECL (t), 0);
6582 /* Now fix up any virtual base class types that we left lying
6583 around. We must get these done before we try to lay out the
6584 virtual function table. As a side-effect, this will remove the
6585 base subobject fields. */
6586 layout_virtual_bases (rli, empty_base_offsets);
6588 /* Make sure that empty classes are reflected in RLI at this
6589 point. */
6590 include_empty_classes (rli);
6592 /* Make sure not to create any structures with zero size. */
6593 if (integer_zerop (rli_size_unit_so_far (rli)) && CLASSTYPE_EMPTY_P (t))
6594 place_field (rli,
6595 build_decl (input_location,
6596 FIELD_DECL, NULL_TREE, char_type_node));
6598 /* If this is a non-POD, declaring it packed makes a difference to how it
6599 can be used as a field; don't let finalize_record_size undo it. */
6600 if (TYPE_PACKED (t) && !layout_pod_type_p (t))
6601 rli->packed_maybe_necessary = true;
6603 /* Let the back end lay out the type. */
6604 finish_record_layout (rli, /*free_p=*/true);
6606 if (TYPE_SIZE_UNIT (t)
6607 && TREE_CODE (TYPE_SIZE_UNIT (t)) == INTEGER_CST
6608 && !TREE_OVERFLOW (TYPE_SIZE_UNIT (t))
6609 && !valid_constant_size_p (TYPE_SIZE_UNIT (t)))
6610 error ("size of type %qT is too large (%qE bytes)", t, TYPE_SIZE_UNIT (t));
6612 /* Warn about bases that can't be talked about due to ambiguity. */
6613 warn_about_ambiguous_bases (t);
6615 /* Now that we're done with layout, give the base fields the real types. */
6616 for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
6617 if (DECL_ARTIFICIAL (field) && IS_FAKE_BASE_TYPE (TREE_TYPE (field)))
6618 TREE_TYPE (field) = TYPE_CONTEXT (TREE_TYPE (field));
6620 /* Clean up. */
6621 splay_tree_delete (empty_base_offsets);
6623 if (CLASSTYPE_EMPTY_P (t)
6624 && tree_int_cst_lt (sizeof_biggest_empty_class,
6625 TYPE_SIZE_UNIT (t)))
6626 sizeof_biggest_empty_class = TYPE_SIZE_UNIT (t);
6629 /* Determine the "key method" for the class type indicated by TYPE,
6630 and set CLASSTYPE_KEY_METHOD accordingly. */
6632 void
6633 determine_key_method (tree type)
6635 tree method;
6637 if (processing_template_decl
6638 || CLASSTYPE_TEMPLATE_INSTANTIATION (type)
6639 || CLASSTYPE_INTERFACE_KNOWN (type))
6640 return;
6642 /* The key method is the first non-pure virtual function that is not
6643 inline at the point of class definition. On some targets the
6644 key function may not be inline; those targets should not call
6645 this function until the end of the translation unit. */
6646 for (method = TYPE_METHODS (type); method != NULL_TREE;
6647 method = DECL_CHAIN (method))
6648 if (TREE_CODE (method) == FUNCTION_DECL
6649 && DECL_VINDEX (method) != NULL_TREE
6650 && ! DECL_DECLARED_INLINE_P (method)
6651 && ! DECL_PURE_VIRTUAL_P (method))
6653 CLASSTYPE_KEY_METHOD (type) = method;
6654 break;
6657 return;
6661 /* Allocate and return an instance of struct sorted_fields_type with
6662 N fields. */
6664 static struct sorted_fields_type *
6665 sorted_fields_type_new (int n)
6667 struct sorted_fields_type *sft;
6668 sft = (sorted_fields_type *) ggc_internal_alloc (sizeof (sorted_fields_type)
6669 + n * sizeof (tree));
6670 sft->len = n;
6672 return sft;
6675 /* Helper of find_flexarrays. Return true when FLD refers to a non-static
6676 class data member of non-zero size, otherwise false. */
6678 static inline bool
6679 field_nonempty_p (const_tree fld)
6681 if (TREE_CODE (fld) == ERROR_MARK)
6682 return false;
6684 tree type = TREE_TYPE (fld);
6685 if (TREE_CODE (fld) == FIELD_DECL
6686 && TREE_CODE (type) != ERROR_MARK
6687 && (DECL_NAME (fld) || RECORD_OR_UNION_TYPE_P (type)))
6689 return TYPE_SIZE (type)
6690 && (TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST
6691 || !tree_int_cst_equal (size_zero_node, TYPE_SIZE (type)));
6694 return false;
6697 /* Used by find_flexarrays and related functions. */
6699 struct flexmems_t
6701 /* The first flexible array member or non-zero array member found
6702 in the order of layout. */
6703 tree array;
6704 /* First non-static non-empty data member in the class or its bases. */
6705 tree first;
6706 /* The first non-static non-empty data member following either
6707 the flexible array member, if found, or the zero-length array member
6708 otherwise. AFTER[1] refers to the first such data member of a union
6709 of which the struct containing the flexible array member or zero-length
6710 array is a member, or NULL when no such union exists. This element is
6711 only used during searching, not for diagnosing problems. AFTER[0]
6712 refers to the first such data member that is not a member of such
6713 a union. */
6714 tree after[2];
6716 /* Refers to a struct (not union) in which the struct of which the flexible
6717 array is member is defined. Used to diagnose strictly (according to C)
6718 invalid uses of the latter structs. */
6719 tree enclosing;
6722 /* Find either the first flexible array member or the first zero-length
6723 array, in that order of preference, among members of class T (but not
6724 its base classes), and set members of FMEM accordingly.
6725 BASE_P is true if T is a base class of another class.
6726 PUN is set to the outermost union in which the flexible array member
6727 (or zero-length array) is defined if one such union exists, otherwise
6728 to NULL.
6729 Similarly, PSTR is set to a data member of the outermost struct of
6730 which the flexible array is a member if one such struct exists,
6731 otherwise to NULL. */
6733 static void
6734 find_flexarrays (tree t, flexmems_t *fmem, bool base_p,
6735 tree pun /* = NULL_TREE */,
6736 tree pstr /* = NULL_TREE */)
6738 /* Set the "pointer" to the outermost enclosing union if not set
6739 yet and maintain it for the remainder of the recursion. */
6740 if (!pun && TREE_CODE (t) == UNION_TYPE)
6741 pun = t;
6743 for (tree fld = TYPE_FIELDS (t); fld; fld = DECL_CHAIN (fld))
6745 if (fld == error_mark_node)
6746 return;
6748 /* Is FLD a typedef for an anonymous struct? */
6750 /* FIXME: Note that typedefs (as well as arrays) need to be fully
6751 handled elsewhere so that errors like the following are detected
6752 as well:
6753 typedef struct { int i, a[], j; } S; // bug c++/72753
6754 S s [2]; // bug c++/68489
6756 if (TREE_CODE (fld) == TYPE_DECL
6757 && DECL_IMPLICIT_TYPEDEF_P (fld)
6758 && CLASS_TYPE_P (TREE_TYPE (fld))
6759 && anon_aggrname_p (DECL_NAME (fld)))
6761 /* Check the nested unnamed type referenced via a typedef
6762 independently of FMEM (since it's not a data member of
6763 the enclosing class). */
6764 check_flexarrays (TREE_TYPE (fld));
6765 continue;
6768 /* Skip anything that's GCC-generated or not a (non-static) data
6769 member. */
6770 if (DECL_ARTIFICIAL (fld) || TREE_CODE (fld) != FIELD_DECL)
6771 continue;
6773 /* Type of the member. */
6774 tree fldtype = TREE_TYPE (fld);
6775 if (fldtype == error_mark_node)
6776 return;
6778 /* Determine the type of the array element or object referenced
6779 by the member so that it can be checked for flexible array
6780 members if it hasn't been yet. */
6781 tree eltype = fldtype;
6782 while (TREE_CODE (eltype) == ARRAY_TYPE
6783 || TREE_CODE (eltype) == POINTER_TYPE
6784 || TREE_CODE (eltype) == REFERENCE_TYPE)
6785 eltype = TREE_TYPE (eltype);
6787 if (RECORD_OR_UNION_TYPE_P (eltype))
6789 if (fmem->array && !fmem->after[bool (pun)])
6791 /* Once the member after the flexible array has been found
6792 we're done. */
6793 fmem->after[bool (pun)] = fld;
6794 break;
6797 if (eltype == fldtype || TYPE_UNNAMED_P (eltype))
6799 /* Descend into the non-static member struct or union and try
6800 to find a flexible array member or zero-length array among
6801 its members. This is only necessary for anonymous types
6802 and types in whose context the current type T has not been
6803 defined (the latter must not be checked again because they
6804 are already in the process of being checked by one of the
6805 recursive calls). */
6807 tree first = fmem->first;
6808 tree array = fmem->array;
6810 /* If this member isn't anonymous and a prior non-flexible array
6811 member has been seen in one of the enclosing structs, clear
6812 the FIRST member since it doesn't contribute to the flexible
6813 array struct's members. */
6814 if (first && !array && !ANON_AGGR_TYPE_P (eltype))
6815 fmem->first = NULL_TREE;
6817 find_flexarrays (eltype, fmem, false, pun,
6818 !pstr && TREE_CODE (t) == RECORD_TYPE ? fld : pstr);
6820 if (fmem->array != array)
6821 continue;
6823 if (first && !array && !ANON_AGGR_TYPE_P (eltype))
6825 /* Restore the FIRST member reset above if no flexible
6826 array member has been found in this member's struct. */
6827 fmem->first = first;
6830 /* If the member struct contains the first flexible array
6831 member, or if this member is a base class, continue to
6832 the next member and avoid setting the FMEM->NEXT pointer
6833 to point to it. */
6834 if (base_p)
6835 continue;
6839 if (field_nonempty_p (fld))
6841 /* Remember the first non-static data member. */
6842 if (!fmem->first)
6843 fmem->first = fld;
6845 /* Remember the first non-static data member after the flexible
6846 array member, if one has been found, or the zero-length array
6847 if it has been found. */
6848 if (fmem->array && !fmem->after[bool (pun)])
6849 fmem->after[bool (pun)] = fld;
6852 /* Skip non-arrays. */
6853 if (TREE_CODE (fldtype) != ARRAY_TYPE)
6854 continue;
6856 /* Determine the upper bound of the array if it has one. */
6857 if (TYPE_DOMAIN (fldtype))
6859 if (fmem->array)
6861 /* Make a record of the zero-length array if either one
6862 such field or a flexible array member has been seen to
6863 handle the pathological and unlikely case of multiple
6864 such members. */
6865 if (!fmem->after[bool (pun)])
6866 fmem->after[bool (pun)] = fld;
6868 else if (integer_all_onesp (TYPE_MAX_VALUE (TYPE_DOMAIN (fldtype))))
6870 /* Remember the first zero-length array unless a flexible array
6871 member has already been seen. */
6872 fmem->array = fld;
6873 fmem->enclosing = pstr;
6876 else
6878 /* Flexible array members have no upper bound. */
6879 if (fmem->array)
6881 /* Replace the zero-length array if it's been stored and
6882 reset the after pointer. */
6883 if (TYPE_DOMAIN (TREE_TYPE (fmem->array)))
6885 fmem->after[bool (pun)] = NULL_TREE;
6886 fmem->array = fld;
6887 fmem->enclosing = pstr;
6890 else
6892 fmem->array = fld;
6893 fmem->enclosing = pstr;
6899 /* Diagnose a strictly (by the C standard) invalid use of a struct with
6900 a flexible array member (or the zero-length array extension). */
6902 static void
6903 diagnose_invalid_flexarray (const flexmems_t *fmem)
6905 if (fmem->array && fmem->enclosing
6906 && pedwarn (location_of (fmem->enclosing), OPT_Wpedantic,
6907 TYPE_DOMAIN (TREE_TYPE (fmem->array))
6908 ? G_("invalid use of %q#T with a zero-size array "
6909 "in %q#D")
6910 : G_("invalid use of %q#T with a flexible array member "
6911 "in %q#T"),
6912 DECL_CONTEXT (fmem->array),
6913 DECL_CONTEXT (fmem->enclosing)))
6914 inform (DECL_SOURCE_LOCATION (fmem->array),
6915 "array member %q#D declared here", fmem->array);
6918 /* Issue diagnostics for invalid flexible array members or zero-length
6919 arrays that are not the last elements of the containing class or its
6920 base classes or that are its sole members. */
6922 static void
6923 diagnose_flexarrays (tree t, const flexmems_t *fmem)
6925 if (!fmem->array)
6926 return;
6928 if (fmem->first && !fmem->after[0])
6930 diagnose_invalid_flexarray (fmem);
6931 return;
6934 /* Has a diagnostic been issued? */
6935 bool diagd = false;
6937 const char *msg = 0;
6939 if (TYPE_DOMAIN (TREE_TYPE (fmem->array)))
6941 if (fmem->after[0])
6942 msg = G_("zero-size array member %qD not at end of %q#T");
6943 else if (!fmem->first)
6944 msg = G_("zero-size array member %qD in an otherwise empty %q#T");
6946 if (msg)
6948 location_t loc = DECL_SOURCE_LOCATION (fmem->array);
6950 if (pedwarn (loc, OPT_Wpedantic, msg, fmem->array, t))
6952 inform (location_of (t), "in the definition of %q#T", t);
6953 diagd = true;
6957 else
6959 if (fmem->after[0])
6960 msg = G_("flexible array member %qD not at end of %q#T");
6961 else if (!fmem->first)
6962 msg = G_("flexible array member %qD in an otherwise empty %q#T");
6964 if (msg)
6966 location_t loc = DECL_SOURCE_LOCATION (fmem->array);
6967 diagd = true;
6969 error_at (loc, msg, fmem->array, t);
6971 /* In the unlikely event that the member following the flexible
6972 array member is declared in a different class, or the member
6973 overlaps another member of a common union, point to it.
6974 Otherwise it should be obvious. */
6975 if (fmem->after[0]
6976 && ((DECL_CONTEXT (fmem->after[0])
6977 != DECL_CONTEXT (fmem->array))))
6979 inform (DECL_SOURCE_LOCATION (fmem->after[0]),
6980 "next member %q#D declared here",
6981 fmem->after[0]);
6982 inform (location_of (t), "in the definition of %q#T", t);
6987 if (!diagd && fmem->array && fmem->enclosing)
6988 diagnose_invalid_flexarray (fmem);
6992 /* Recursively check to make sure that any flexible array or zero-length
6993 array members of class T or its bases are valid (i.e., not the sole
6994 non-static data member of T and, if one exists, that it is the last
6995 non-static data member of T and its base classes. FMEM is expected
6996 to be initially null and is used internally by recursive calls to
6997 the function. Issue the appropriate diagnostics for the array member
6998 that fails the checks. */
7000 static void
7001 check_flexarrays (tree t, flexmems_t *fmem /* = NULL */,
7002 bool base_p /* = false */)
7004 /* Initialize the result of a search for flexible array and zero-length
7005 array members. Avoid doing any work if the most interesting FMEM data
7006 have already been populated. */
7007 flexmems_t flexmems = flexmems_t ();
7008 if (!fmem)
7009 fmem = &flexmems;
7010 else if (fmem->array && fmem->first && fmem->after[0])
7011 return;
7013 tree fam = fmem->array;
7015 /* Recursively check the primary base class first. */
7016 if (CLASSTYPE_HAS_PRIMARY_BASE_P (t))
7018 tree basetype = BINFO_TYPE (CLASSTYPE_PRIMARY_BINFO (t));
7019 check_flexarrays (basetype, fmem, true);
7022 /* Recursively check the base classes. */
7023 int nbases = TYPE_BINFO (t) ? BINFO_N_BASE_BINFOS (TYPE_BINFO (t)) : 0;
7024 for (int i = 0; i < nbases; ++i)
7026 tree base_binfo = BINFO_BASE_BINFO (TYPE_BINFO (t), i);
7028 /* The primary base class was already checked above. */
7029 if (base_binfo == CLASSTYPE_PRIMARY_BINFO (t))
7030 continue;
7032 /* Virtual base classes are at the end. */
7033 if (BINFO_VIRTUAL_P (base_binfo))
7034 continue;
7036 /* Check the base class. */
7037 check_flexarrays (BINFO_TYPE (base_binfo), fmem, /*base_p=*/true);
7040 if (fmem == &flexmems)
7042 /* Check virtual base classes only once per derived class.
7043 I.e., this check is not performed recursively for base
7044 classes. */
7045 int i;
7046 tree base_binfo;
7047 vec<tree, va_gc> *vbases;
7048 for (vbases = CLASSTYPE_VBASECLASSES (t), i = 0;
7049 vec_safe_iterate (vbases, i, &base_binfo); i++)
7051 /* Check the virtual base class. */
7052 tree basetype = TREE_TYPE (base_binfo);
7054 check_flexarrays (basetype, fmem, /*base_p=*/true);
7058 /* Is the type unnamed (and therefore a member of it potentially
7059 an anonymous struct or union)? */
7060 bool maybe_anon_p = TYPE_UNNAMED_P (t);
7062 /* Search the members of the current (possibly derived) class, skipping
7063 unnamed structs and unions since those could be anonymous. */
7064 if (fmem != &flexmems || !maybe_anon_p)
7065 find_flexarrays (t, fmem, base_p || fam != fmem->array);
7067 if (fmem == &flexmems && !maybe_anon_p)
7069 /* Issue diagnostics for invalid flexible and zero-length array
7070 members found in base classes or among the members of the current
7071 class. Ignore anonymous structs and unions whose members are
7072 considered to be members of the enclosing class and thus will
7073 be diagnosed when checking it. */
7074 diagnose_flexarrays (t, fmem);
7078 /* Perform processing required when the definition of T (a class type)
7079 is complete. Diagnose invalid definitions of flexible array members
7080 and zero-size arrays. */
7082 void
7083 finish_struct_1 (tree t)
7085 tree x;
7086 /* A TREE_LIST. The TREE_VALUE of each node is a FUNCTION_DECL. */
7087 tree virtuals = NULL_TREE;
7089 if (COMPLETE_TYPE_P (t))
7091 gcc_assert (MAYBE_CLASS_TYPE_P (t));
7092 error ("redefinition of %q#T", t);
7093 popclass ();
7094 return;
7097 /* If this type was previously laid out as a forward reference,
7098 make sure we lay it out again. */
7099 TYPE_SIZE (t) = NULL_TREE;
7100 CLASSTYPE_PRIMARY_BINFO (t) = NULL_TREE;
7102 /* Make assumptions about the class; we'll reset the flags if
7103 necessary. */
7104 CLASSTYPE_EMPTY_P (t) = 1;
7105 CLASSTYPE_NEARLY_EMPTY_P (t) = 1;
7106 CLASSTYPE_CONTAINS_EMPTY_CLASS_P (t) = 0;
7107 CLASSTYPE_LITERAL_P (t) = true;
7109 /* Do end-of-class semantic processing: checking the validity of the
7110 bases and members and add implicitly generated methods. */
7111 check_bases_and_members (t);
7113 /* Find the key method. */
7114 if (TYPE_CONTAINS_VPTR_P (t))
7116 /* The Itanium C++ ABI permits the key method to be chosen when
7117 the class is defined -- even though the key method so
7118 selected may later turn out to be an inline function. On
7119 some systems (such as ARM Symbian OS) the key method cannot
7120 be determined until the end of the translation unit. On such
7121 systems, we leave CLASSTYPE_KEY_METHOD set to NULL, which
7122 will cause the class to be added to KEYED_CLASSES. Then, in
7123 finish_file we will determine the key method. */
7124 if (targetm.cxx.key_method_may_be_inline ())
7125 determine_key_method (t);
7127 /* If a polymorphic class has no key method, we may emit the vtable
7128 in every translation unit where the class definition appears. If
7129 we're devirtualizing, we can look into the vtable even if we
7130 aren't emitting it. */
7131 if (!CLASSTYPE_KEY_METHOD (t))
7132 vec_safe_push (keyed_classes, t);
7135 /* Layout the class itself. */
7136 layout_class_type (t, &virtuals);
7137 if (CLASSTYPE_AS_BASE (t) != t)
7138 /* We use the base type for trivial assignments, and hence it
7139 needs a mode. */
7140 compute_record_mode (CLASSTYPE_AS_BASE (t));
7142 /* With the layout complete, check for flexible array members and
7143 zero-length arrays that might overlap other members in the final
7144 layout. */
7145 check_flexarrays (t);
7147 virtuals = modify_all_vtables (t, nreverse (virtuals));
7149 /* If necessary, create the primary vtable for this class. */
7150 if (virtuals || TYPE_CONTAINS_VPTR_P (t))
7152 /* We must enter these virtuals into the table. */
7153 if (!CLASSTYPE_HAS_PRIMARY_BASE_P (t))
7154 build_primary_vtable (NULL_TREE, t);
7155 else if (! BINFO_NEW_VTABLE_MARKED (TYPE_BINFO (t)))
7156 /* Here we know enough to change the type of our virtual
7157 function table, but we will wait until later this function. */
7158 build_primary_vtable (CLASSTYPE_PRIMARY_BINFO (t), t);
7160 /* If we're warning about ABI tags, check the types of the new
7161 virtual functions. */
7162 if (warn_abi_tag)
7163 for (tree v = virtuals; v; v = TREE_CHAIN (v))
7164 check_abi_tags (t, TREE_VALUE (v));
7167 if (TYPE_CONTAINS_VPTR_P (t))
7169 int vindex;
7170 tree fn;
7172 if (BINFO_VTABLE (TYPE_BINFO (t)))
7173 gcc_assert (DECL_VIRTUAL_P (BINFO_VTABLE (TYPE_BINFO (t))));
7174 if (!CLASSTYPE_HAS_PRIMARY_BASE_P (t))
7175 gcc_assert (BINFO_VIRTUALS (TYPE_BINFO (t)) == NULL_TREE);
7177 /* Add entries for virtual functions introduced by this class. */
7178 BINFO_VIRTUALS (TYPE_BINFO (t))
7179 = chainon (BINFO_VIRTUALS (TYPE_BINFO (t)), virtuals);
7181 /* Set DECL_VINDEX for all functions declared in this class. */
7182 for (vindex = 0, fn = BINFO_VIRTUALS (TYPE_BINFO (t));
7184 fn = TREE_CHAIN (fn),
7185 vindex += (TARGET_VTABLE_USES_DESCRIPTORS
7186 ? TARGET_VTABLE_USES_DESCRIPTORS : 1))
7188 tree fndecl = BV_FN (fn);
7190 if (DECL_THUNK_P (fndecl))
7191 /* A thunk. We should never be calling this entry directly
7192 from this vtable -- we'd use the entry for the non
7193 thunk base function. */
7194 DECL_VINDEX (fndecl) = NULL_TREE;
7195 else if (TREE_CODE (DECL_VINDEX (fndecl)) != INTEGER_CST)
7196 DECL_VINDEX (fndecl) = build_int_cst (NULL_TREE, vindex);
7200 finish_struct_bits (t);
7201 set_method_tm_attributes (t);
7202 if (flag_openmp || flag_openmp_simd)
7203 finish_omp_declare_simd_methods (t);
7205 /* Complete the rtl for any static member objects of the type we're
7206 working on. */
7207 for (x = TYPE_FIELDS (t); x; x = DECL_CHAIN (x))
7208 if (VAR_P (x) && TREE_STATIC (x)
7209 && TREE_TYPE (x) != error_mark_node
7210 && same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (x)), t))
7211 SET_DECL_MODE (x, TYPE_MODE (t));
7213 /* Done with FIELDS...now decide whether to sort these for
7214 faster lookups later.
7216 We use a small number because most searches fail (succeeding
7217 ultimately as the search bores through the inheritance
7218 hierarchy), and we want this failure to occur quickly. */
7220 insert_into_classtype_sorted_fields (TYPE_FIELDS (t), t, 8);
7222 /* Complain if one of the field types requires lower visibility. */
7223 constrain_class_visibility (t);
7225 /* Make the rtl for any new vtables we have created, and unmark
7226 the base types we marked. */
7227 finish_vtbls (t);
7229 /* Build the VTT for T. */
7230 build_vtt (t);
7232 if (warn_nonvdtor
7233 && TYPE_POLYMORPHIC_P (t) && accessible_nvdtor_p (t)
7234 && !CLASSTYPE_FINAL (t))
7235 warning (OPT_Wnon_virtual_dtor,
7236 "%q#T has virtual functions and accessible"
7237 " non-virtual destructor", t);
7239 complete_vars (t);
7241 if (warn_overloaded_virtual)
7242 warn_hidden (t);
7244 /* Class layout, assignment of virtual table slots, etc., is now
7245 complete. Give the back end a chance to tweak the visibility of
7246 the class or perform any other required target modifications. */
7247 targetm.cxx.adjust_class_at_definition (t);
7249 maybe_suppress_debug_info (t);
7251 if (flag_vtable_verify)
7252 vtv_save_class_info (t);
7254 dump_class_hierarchy (t);
7256 /* Finish debugging output for this type. */
7257 rest_of_type_compilation (t, ! LOCAL_CLASS_P (t));
7259 if (TYPE_TRANSPARENT_AGGR (t))
7261 tree field = first_field (t);
7262 if (field == NULL_TREE || error_operand_p (field))
7264 error ("type transparent %q#T does not have any fields", t);
7265 TYPE_TRANSPARENT_AGGR (t) = 0;
7267 else if (DECL_ARTIFICIAL (field))
7269 if (DECL_FIELD_IS_BASE (field))
7270 error ("type transparent class %qT has base classes", t);
7271 else
7273 gcc_checking_assert (DECL_VIRTUAL_P (field));
7274 error ("type transparent class %qT has virtual functions", t);
7276 TYPE_TRANSPARENT_AGGR (t) = 0;
7278 else if (TYPE_MODE (t) != DECL_MODE (field))
7280 error ("type transparent %q#T cannot be made transparent because "
7281 "the type of the first field has a different ABI from the "
7282 "class overall", t);
7283 TYPE_TRANSPARENT_AGGR (t) = 0;
7288 /* Insert FIELDS into T for the sorted case if the FIELDS count is
7289 equal to THRESHOLD or greater than THRESHOLD. */
7291 static void
7292 insert_into_classtype_sorted_fields (tree fields, tree t, int threshold)
7294 int n_fields = count_fields (fields);
7295 if (n_fields >= threshold)
7297 struct sorted_fields_type *field_vec = sorted_fields_type_new (n_fields);
7298 add_fields_to_record_type (fields, field_vec, 0);
7299 qsort (field_vec->elts, n_fields, sizeof (tree), field_decl_cmp);
7300 CLASSTYPE_SORTED_FIELDS (t) = field_vec;
7304 /* Insert lately defined enum ENUMTYPE into T for the sorted case. */
7306 void
7307 insert_late_enum_def_into_classtype_sorted_fields (tree enumtype, tree t)
7309 struct sorted_fields_type *sorted_fields = CLASSTYPE_SORTED_FIELDS (t);
7310 if (sorted_fields)
7312 int i;
7313 int n_fields
7314 = list_length (TYPE_VALUES (enumtype)) + sorted_fields->len;
7315 struct sorted_fields_type *field_vec = sorted_fields_type_new (n_fields);
7317 for (i = 0; i < sorted_fields->len; ++i)
7318 field_vec->elts[i] = sorted_fields->elts[i];
7320 add_enum_fields_to_record_type (enumtype, field_vec,
7321 sorted_fields->len);
7322 qsort (field_vec->elts, n_fields, sizeof (tree), field_decl_cmp);
7323 CLASSTYPE_SORTED_FIELDS (t) = field_vec;
7327 /* When T was built up, the member declarations were added in reverse
7328 order. Rearrange them to declaration order. */
7330 void
7331 unreverse_member_declarations (tree t)
7333 tree next;
7334 tree prev;
7335 tree x;
7337 /* The following lists are all in reverse order. Put them in
7338 declaration order now. */
7339 TYPE_METHODS (t) = nreverse (TYPE_METHODS (t));
7340 CLASSTYPE_DECL_LIST (t) = nreverse (CLASSTYPE_DECL_LIST (t));
7342 /* Actually, for the TYPE_FIELDS, only the non TYPE_DECLs are in
7343 reverse order, so we can't just use nreverse. */
7344 prev = NULL_TREE;
7345 for (x = TYPE_FIELDS (t);
7346 x && TREE_CODE (x) != TYPE_DECL;
7347 x = next)
7349 next = DECL_CHAIN (x);
7350 DECL_CHAIN (x) = prev;
7351 prev = x;
7353 if (prev)
7355 DECL_CHAIN (TYPE_FIELDS (t)) = x;
7356 if (prev)
7357 TYPE_FIELDS (t) = prev;
7361 tree
7362 finish_struct (tree t, tree attributes)
7364 location_t saved_loc = input_location;
7366 /* Now that we've got all the field declarations, reverse everything
7367 as necessary. */
7368 unreverse_member_declarations (t);
7370 cplus_decl_attributes (&t, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
7371 fixup_attribute_variants (t);
7373 /* Nadger the current location so that diagnostics point to the start of
7374 the struct, not the end. */
7375 input_location = DECL_SOURCE_LOCATION (TYPE_NAME (t));
7377 if (processing_template_decl)
7379 tree x;
7381 finish_struct_methods (t);
7382 TYPE_SIZE (t) = bitsize_zero_node;
7383 TYPE_SIZE_UNIT (t) = size_zero_node;
7385 /* We need to emit an error message if this type was used as a parameter
7386 and it is an abstract type, even if it is a template. We construct
7387 a simple CLASSTYPE_PURE_VIRTUALS list without taking bases into
7388 account and we call complete_vars with this type, which will check
7389 the PARM_DECLS. Note that while the type is being defined,
7390 CLASSTYPE_PURE_VIRTUALS contains the list of the inline friends
7391 (see CLASSTYPE_INLINE_FRIENDS) so we need to clear it. */
7392 CLASSTYPE_PURE_VIRTUALS (t) = NULL;
7393 for (x = TYPE_METHODS (t); x; x = DECL_CHAIN (x))
7394 if (DECL_PURE_VIRTUAL_P (x))
7395 vec_safe_push (CLASSTYPE_PURE_VIRTUALS (t), x);
7396 complete_vars (t);
7397 /* We need to add the target functions to the CLASSTYPE_METHOD_VEC if
7398 an enclosing scope is a template class, so that this function be
7399 found by lookup_fnfields_1 when the using declaration is not
7400 instantiated yet. */
7401 for (x = TYPE_FIELDS (t); x; x = DECL_CHAIN (x))
7402 if (TREE_CODE (x) == USING_DECL)
7404 tree fn = strip_using_decl (x);
7405 if (OVL_P (fn))
7406 for (lkp_iterator iter (fn); iter; ++iter)
7407 add_method (t, *iter, true);
7410 /* Remember current #pragma pack value. */
7411 TYPE_PRECISION (t) = maximum_field_alignment;
7413 /* Fix up any variants we've already built. */
7414 for (x = TYPE_NEXT_VARIANT (t); x; x = TYPE_NEXT_VARIANT (x))
7416 TYPE_SIZE (x) = TYPE_SIZE (t);
7417 TYPE_SIZE_UNIT (x) = TYPE_SIZE_UNIT (t);
7418 TYPE_FIELDS (x) = TYPE_FIELDS (t);
7419 TYPE_METHODS (x) = TYPE_METHODS (t);
7422 else
7423 finish_struct_1 (t);
7425 if (is_std_init_list (t))
7427 /* People keep complaining that the compiler crashes on an invalid
7428 definition of initializer_list, so I guess we should explicitly
7429 reject it. What the compiler internals care about is that it's a
7430 template and has a pointer field followed by an integer field. */
7431 bool ok = false;
7432 if (processing_template_decl)
7434 tree f = next_initializable_field (TYPE_FIELDS (t));
7435 if (f && TREE_CODE (TREE_TYPE (f)) == POINTER_TYPE)
7437 f = next_initializable_field (DECL_CHAIN (f));
7438 if (f && same_type_p (TREE_TYPE (f), size_type_node))
7439 ok = true;
7442 if (!ok)
7443 fatal_error (input_location,
7444 "definition of std::initializer_list does not match "
7445 "#include <initializer_list>");
7448 input_location = saved_loc;
7450 TYPE_BEING_DEFINED (t) = 0;
7452 if (current_class_type)
7453 popclass ();
7454 else
7455 error ("trying to finish struct, but kicked out due to previous parse errors");
7457 if (processing_template_decl && at_function_scope_p ()
7458 /* Lambdas are defined by the LAMBDA_EXPR. */
7459 && !LAMBDA_TYPE_P (t))
7460 add_stmt (build_min (TAG_DEFN, t));
7462 return t;
7465 /* Hash table to avoid endless recursion when handling references. */
7466 static hash_table<nofree_ptr_hash<tree_node> > *fixed_type_or_null_ref_ht;
7468 /* Return the dynamic type of INSTANCE, if known.
7469 Used to determine whether the virtual function table is needed
7470 or not.
7472 *NONNULL is set iff INSTANCE can be known to be nonnull, regardless
7473 of our knowledge of its type. *NONNULL should be initialized
7474 before this function is called. */
7476 static tree
7477 fixed_type_or_null (tree instance, int *nonnull, int *cdtorp)
7479 #define RECUR(T) fixed_type_or_null((T), nonnull, cdtorp)
7481 switch (TREE_CODE (instance))
7483 case INDIRECT_REF:
7484 if (POINTER_TYPE_P (TREE_TYPE (instance)))
7485 return NULL_TREE;
7486 else
7487 return RECUR (TREE_OPERAND (instance, 0));
7489 case CALL_EXPR:
7490 /* This is a call to a constructor, hence it's never zero. */
7491 if (TREE_HAS_CONSTRUCTOR (instance))
7493 if (nonnull)
7494 *nonnull = 1;
7495 return TREE_TYPE (instance);
7497 return NULL_TREE;
7499 case SAVE_EXPR:
7500 /* This is a call to a constructor, hence it's never zero. */
7501 if (TREE_HAS_CONSTRUCTOR (instance))
7503 if (nonnull)
7504 *nonnull = 1;
7505 return TREE_TYPE (instance);
7507 return RECUR (TREE_OPERAND (instance, 0));
7509 case POINTER_PLUS_EXPR:
7510 case PLUS_EXPR:
7511 case MINUS_EXPR:
7512 if (TREE_CODE (TREE_OPERAND (instance, 0)) == ADDR_EXPR)
7513 return RECUR (TREE_OPERAND (instance, 0));
7514 if (TREE_CODE (TREE_OPERAND (instance, 1)) == INTEGER_CST)
7515 /* Propagate nonnull. */
7516 return RECUR (TREE_OPERAND (instance, 0));
7518 return NULL_TREE;
7520 CASE_CONVERT:
7521 return RECUR (TREE_OPERAND (instance, 0));
7523 case ADDR_EXPR:
7524 instance = TREE_OPERAND (instance, 0);
7525 if (nonnull)
7527 /* Just because we see an ADDR_EXPR doesn't mean we're dealing
7528 with a real object -- given &p->f, p can still be null. */
7529 tree t = get_base_address (instance);
7530 /* ??? Probably should check DECL_WEAK here. */
7531 if (t && DECL_P (t))
7532 *nonnull = 1;
7534 return RECUR (instance);
7536 case COMPONENT_REF:
7537 /* If this component is really a base class reference, then the field
7538 itself isn't definitive. */
7539 if (DECL_FIELD_IS_BASE (TREE_OPERAND (instance, 1)))
7540 return RECUR (TREE_OPERAND (instance, 0));
7541 return RECUR (TREE_OPERAND (instance, 1));
7543 case VAR_DECL:
7544 case FIELD_DECL:
7545 if (TREE_CODE (TREE_TYPE (instance)) == ARRAY_TYPE
7546 && MAYBE_CLASS_TYPE_P (TREE_TYPE (TREE_TYPE (instance))))
7548 if (nonnull)
7549 *nonnull = 1;
7550 return TREE_TYPE (TREE_TYPE (instance));
7552 /* fall through. */
7553 case TARGET_EXPR:
7554 case PARM_DECL:
7555 case RESULT_DECL:
7556 if (MAYBE_CLASS_TYPE_P (TREE_TYPE (instance)))
7558 if (nonnull)
7559 *nonnull = 1;
7560 return TREE_TYPE (instance);
7562 else if (instance == current_class_ptr)
7564 if (nonnull)
7565 *nonnull = 1;
7567 /* if we're in a ctor or dtor, we know our type. If
7568 current_class_ptr is set but we aren't in a function, we're in
7569 an NSDMI (and therefore a constructor). */
7570 if (current_scope () != current_function_decl
7571 || (DECL_LANG_SPECIFIC (current_function_decl)
7572 && (DECL_CONSTRUCTOR_P (current_function_decl)
7573 || DECL_DESTRUCTOR_P (current_function_decl))))
7575 if (cdtorp)
7576 *cdtorp = 1;
7577 return TREE_TYPE (TREE_TYPE (instance));
7580 else if (TREE_CODE (TREE_TYPE (instance)) == REFERENCE_TYPE)
7582 /* We only need one hash table because it is always left empty. */
7583 if (!fixed_type_or_null_ref_ht)
7584 fixed_type_or_null_ref_ht
7585 = new hash_table<nofree_ptr_hash<tree_node> > (37);
7587 /* Reference variables should be references to objects. */
7588 if (nonnull)
7589 *nonnull = 1;
7591 /* Enter the INSTANCE in a table to prevent recursion; a
7592 variable's initializer may refer to the variable
7593 itself. */
7594 if (VAR_P (instance)
7595 && DECL_INITIAL (instance)
7596 && !type_dependent_expression_p_push (DECL_INITIAL (instance))
7597 && !fixed_type_or_null_ref_ht->find (instance))
7599 tree type;
7600 tree_node **slot;
7602 slot = fixed_type_or_null_ref_ht->find_slot (instance, INSERT);
7603 *slot = instance;
7604 type = RECUR (DECL_INITIAL (instance));
7605 fixed_type_or_null_ref_ht->remove_elt (instance);
7607 return type;
7610 return NULL_TREE;
7612 default:
7613 return NULL_TREE;
7615 #undef RECUR
7618 /* Return nonzero if the dynamic type of INSTANCE is known, and
7619 equivalent to the static type. We also handle the case where
7620 INSTANCE is really a pointer. Return negative if this is a
7621 ctor/dtor. There the dynamic type is known, but this might not be
7622 the most derived base of the original object, and hence virtual
7623 bases may not be laid out according to this type.
7625 Used to determine whether the virtual function table is needed
7626 or not.
7628 *NONNULL is set iff INSTANCE can be known to be nonnull, regardless
7629 of our knowledge of its type. *NONNULL should be initialized
7630 before this function is called. */
7633 resolves_to_fixed_type_p (tree instance, int* nonnull)
7635 tree t = TREE_TYPE (instance);
7636 int cdtorp = 0;
7637 tree fixed;
7639 /* processing_template_decl can be false in a template if we're in
7640 instantiate_non_dependent_expr, but we still want to suppress
7641 this check. */
7642 if (in_template_function ())
7644 /* In a template we only care about the type of the result. */
7645 if (nonnull)
7646 *nonnull = true;
7647 return true;
7650 fixed = fixed_type_or_null (instance, nonnull, &cdtorp);
7651 if (fixed == NULL_TREE)
7652 return 0;
7653 if (POINTER_TYPE_P (t))
7654 t = TREE_TYPE (t);
7655 if (!same_type_ignoring_top_level_qualifiers_p (t, fixed))
7656 return 0;
7657 return cdtorp ? -1 : 1;
7661 void
7662 init_class_processing (void)
7664 current_class_depth = 0;
7665 current_class_stack_size = 10;
7666 current_class_stack
7667 = XNEWVEC (struct class_stack_node, current_class_stack_size);
7668 vec_alloc (local_classes, 8);
7669 sizeof_biggest_empty_class = size_zero_node;
7671 ridpointers[(int) RID_PUBLIC] = access_public_node;
7672 ridpointers[(int) RID_PRIVATE] = access_private_node;
7673 ridpointers[(int) RID_PROTECTED] = access_protected_node;
7676 /* Restore the cached PREVIOUS_CLASS_LEVEL. */
7678 static void
7679 restore_class_cache (void)
7681 tree type;
7683 /* We are re-entering the same class we just left, so we don't
7684 have to search the whole inheritance matrix to find all the
7685 decls to bind again. Instead, we install the cached
7686 class_shadowed list and walk through it binding names. */
7687 push_binding_level (previous_class_level);
7688 class_binding_level = previous_class_level;
7689 /* Restore IDENTIFIER_TYPE_VALUE. */
7690 for (type = class_binding_level->type_shadowed;
7691 type;
7692 type = TREE_CHAIN (type))
7693 SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (type), TREE_TYPE (type));
7696 /* Set global variables CURRENT_CLASS_NAME and CURRENT_CLASS_TYPE as
7697 appropriate for TYPE.
7699 So that we may avoid calls to lookup_name, we cache the _TYPE
7700 nodes of local TYPE_DECLs in the TREE_TYPE field of the name.
7702 For multiple inheritance, we perform a two-pass depth-first search
7703 of the type lattice. */
7705 void
7706 pushclass (tree type)
7708 class_stack_node_t csn;
7710 type = TYPE_MAIN_VARIANT (type);
7712 /* Make sure there is enough room for the new entry on the stack. */
7713 if (current_class_depth + 1 >= current_class_stack_size)
7715 current_class_stack_size *= 2;
7716 current_class_stack
7717 = XRESIZEVEC (struct class_stack_node, current_class_stack,
7718 current_class_stack_size);
7721 /* Insert a new entry on the class stack. */
7722 csn = current_class_stack + current_class_depth;
7723 csn->name = current_class_name;
7724 csn->type = current_class_type;
7725 csn->access = current_access_specifier;
7726 csn->names_used = 0;
7727 csn->hidden = 0;
7728 current_class_depth++;
7730 /* Now set up the new type. */
7731 current_class_name = TYPE_NAME (type);
7732 if (TREE_CODE (current_class_name) == TYPE_DECL)
7733 current_class_name = DECL_NAME (current_class_name);
7734 current_class_type = type;
7736 /* By default, things in classes are private, while things in
7737 structures or unions are public. */
7738 current_access_specifier = (CLASSTYPE_DECLARED_CLASS (type)
7739 ? access_private_node
7740 : access_public_node);
7742 if (previous_class_level
7743 && type != previous_class_level->this_entity
7744 && current_class_depth == 1)
7746 /* Forcibly remove any old class remnants. */
7747 invalidate_class_lookup_cache ();
7750 if (!previous_class_level
7751 || type != previous_class_level->this_entity
7752 || current_class_depth > 1)
7753 pushlevel_class ();
7754 else
7755 restore_class_cache ();
7758 /* When we exit a toplevel class scope, we save its binding level so
7759 that we can restore it quickly. Here, we've entered some other
7760 class, so we must invalidate our cache. */
7762 void
7763 invalidate_class_lookup_cache (void)
7765 previous_class_level = NULL;
7768 /* Get out of the current class scope. If we were in a class scope
7769 previously, that is the one popped to. */
7771 void
7772 popclass (void)
7774 poplevel_class ();
7776 current_class_depth--;
7777 current_class_name = current_class_stack[current_class_depth].name;
7778 current_class_type = current_class_stack[current_class_depth].type;
7779 current_access_specifier = current_class_stack[current_class_depth].access;
7780 if (current_class_stack[current_class_depth].names_used)
7781 splay_tree_delete (current_class_stack[current_class_depth].names_used);
7784 /* Mark the top of the class stack as hidden. */
7786 void
7787 push_class_stack (void)
7789 if (current_class_depth)
7790 ++current_class_stack[current_class_depth - 1].hidden;
7793 /* Mark the top of the class stack as un-hidden. */
7795 void
7796 pop_class_stack (void)
7798 if (current_class_depth)
7799 --current_class_stack[current_class_depth - 1].hidden;
7802 /* Returns 1 if the class type currently being defined is either T or
7803 a nested type of T. Returns the type from the current_class_stack,
7804 which might be equivalent to but not equal to T in case of
7805 constrained partial specializations. */
7807 tree
7808 currently_open_class (tree t)
7810 int i;
7812 if (!CLASS_TYPE_P (t))
7813 return NULL_TREE;
7815 t = TYPE_MAIN_VARIANT (t);
7817 /* We start looking from 1 because entry 0 is from global scope,
7818 and has no type. */
7819 for (i = current_class_depth; i > 0; --i)
7821 tree c;
7822 if (i == current_class_depth)
7823 c = current_class_type;
7824 else
7826 if (current_class_stack[i].hidden)
7827 break;
7828 c = current_class_stack[i].type;
7830 if (!c)
7831 continue;
7832 if (same_type_p (c, t))
7833 return c;
7835 return NULL_TREE;
7838 /* If either current_class_type or one of its enclosing classes are derived
7839 from T, return the appropriate type. Used to determine how we found
7840 something via unqualified lookup. */
7842 tree
7843 currently_open_derived_class (tree t)
7845 int i;
7847 /* The bases of a dependent type are unknown. */
7848 if (dependent_type_p (t))
7849 return NULL_TREE;
7851 if (!current_class_type)
7852 return NULL_TREE;
7854 if (DERIVED_FROM_P (t, current_class_type))
7855 return current_class_type;
7857 for (i = current_class_depth - 1; i > 0; --i)
7859 if (current_class_stack[i].hidden)
7860 break;
7861 if (DERIVED_FROM_P (t, current_class_stack[i].type))
7862 return current_class_stack[i].type;
7865 return NULL_TREE;
7868 /* Return the outermost enclosing class type that is still open, or
7869 NULL_TREE. */
7871 tree
7872 outermost_open_class (void)
7874 if (!current_class_type)
7875 return NULL_TREE;
7876 tree r = NULL_TREE;
7877 if (TYPE_BEING_DEFINED (current_class_type))
7878 r = current_class_type;
7879 for (int i = current_class_depth - 1; i > 0; --i)
7881 if (current_class_stack[i].hidden)
7882 break;
7883 tree t = current_class_stack[i].type;
7884 if (!TYPE_BEING_DEFINED (t))
7885 break;
7886 r = t;
7888 return r;
7891 /* Returns the innermost class type which is not a lambda closure type. */
7893 tree
7894 current_nonlambda_class_type (void)
7896 int i;
7898 /* We start looking from 1 because entry 0 is from global scope,
7899 and has no type. */
7900 for (i = current_class_depth; i > 0; --i)
7902 tree c;
7903 if (i == current_class_depth)
7904 c = current_class_type;
7905 else
7907 if (current_class_stack[i].hidden)
7908 break;
7909 c = current_class_stack[i].type;
7911 if (!c)
7912 continue;
7913 if (!LAMBDA_TYPE_P (c))
7914 return c;
7916 return NULL_TREE;
7919 /* When entering a class scope, all enclosing class scopes' names with
7920 static meaning (static variables, static functions, types and
7921 enumerators) have to be visible. This recursive function calls
7922 pushclass for all enclosing class contexts until global or a local
7923 scope is reached. TYPE is the enclosed class. */
7925 void
7926 push_nested_class (tree type)
7928 /* A namespace might be passed in error cases, like A::B:C. */
7929 if (type == NULL_TREE
7930 || !CLASS_TYPE_P (type))
7931 return;
7933 push_nested_class (DECL_CONTEXT (TYPE_MAIN_DECL (type)));
7935 pushclass (type);
7938 /* Undoes a push_nested_class call. */
7940 void
7941 pop_nested_class (void)
7943 tree context = DECL_CONTEXT (TYPE_MAIN_DECL (current_class_type));
7945 popclass ();
7946 if (context && CLASS_TYPE_P (context))
7947 pop_nested_class ();
7950 /* Returns the number of extern "LANG" blocks we are nested within. */
7953 current_lang_depth (void)
7955 return vec_safe_length (current_lang_base);
7958 /* Set global variables CURRENT_LANG_NAME to appropriate value
7959 so that behavior of name-mangling machinery is correct. */
7961 void
7962 push_lang_context (tree name)
7964 vec_safe_push (current_lang_base, current_lang_name);
7966 if (name == lang_name_cplusplus)
7967 current_lang_name = name;
7968 else if (name == lang_name_c)
7969 current_lang_name = name;
7970 else
7971 error ("language string %<\"%E\"%> not recognized", name);
7974 /* Get out of the current language scope. */
7976 void
7977 pop_lang_context (void)
7979 current_lang_name = current_lang_base->pop ();
7982 /* Type instantiation routines. */
7984 /* Given an OVERLOAD and a TARGET_TYPE, return the function that
7985 matches the TARGET_TYPE. If there is no satisfactory match, return
7986 error_mark_node, and issue an error & warning messages under
7987 control of FLAGS. Permit pointers to member function if FLAGS
7988 permits. If TEMPLATE_ONLY, the name of the overloaded function was
7989 a template-id, and EXPLICIT_TARGS are the explicitly provided
7990 template arguments.
7992 If OVERLOAD is for one or more member functions, then ACCESS_PATH
7993 is the base path used to reference those member functions. If
7994 the address is resolved to a member function, access checks will be
7995 performed and errors issued if appropriate. */
7997 static tree
7998 resolve_address_of_overloaded_function (tree target_type,
7999 tree overload,
8000 tsubst_flags_t complain,
8001 bool template_only,
8002 tree explicit_targs,
8003 tree access_path)
8005 /* Here's what the standard says:
8007 [over.over]
8009 If the name is a function template, template argument deduction
8010 is done, and if the argument deduction succeeds, the deduced
8011 arguments are used to generate a single template function, which
8012 is added to the set of overloaded functions considered.
8014 Non-member functions and static member functions match targets of
8015 type "pointer-to-function" or "reference-to-function." Nonstatic
8016 member functions match targets of type "pointer-to-member
8017 function;" the function type of the pointer to member is used to
8018 select the member function from the set of overloaded member
8019 functions. If a nonstatic member function is selected, the
8020 reference to the overloaded function name is required to have the
8021 form of a pointer to member as described in 5.3.1.
8023 If more than one function is selected, any template functions in
8024 the set are eliminated if the set also contains a non-template
8025 function, and any given template function is eliminated if the
8026 set contains a second template function that is more specialized
8027 than the first according to the partial ordering rules 14.5.5.2.
8028 After such eliminations, if any, there shall remain exactly one
8029 selected function. */
8031 int is_ptrmem = 0;
8032 /* We store the matches in a TREE_LIST rooted here. The functions
8033 are the TREE_PURPOSE, not the TREE_VALUE, in this list, for easy
8034 interoperability with most_specialized_instantiation. */
8035 tree matches = NULL_TREE;
8036 tree fn;
8037 tree target_fn_type;
8039 /* By the time we get here, we should be seeing only real
8040 pointer-to-member types, not the internal POINTER_TYPE to
8041 METHOD_TYPE representation. */
8042 gcc_assert (!TYPE_PTR_P (target_type)
8043 || TREE_CODE (TREE_TYPE (target_type)) != METHOD_TYPE);
8045 gcc_assert (is_overloaded_fn (overload));
8047 /* Check that the TARGET_TYPE is reasonable. */
8048 if (TYPE_PTRFN_P (target_type)
8049 || TYPE_REFFN_P (target_type))
8050 /* This is OK. */;
8051 else if (TYPE_PTRMEMFUNC_P (target_type))
8052 /* This is OK, too. */
8053 is_ptrmem = 1;
8054 else if (TREE_CODE (target_type) == FUNCTION_TYPE)
8055 /* This is OK, too. This comes from a conversion to reference
8056 type. */
8057 target_type = build_reference_type (target_type);
8058 else
8060 if (complain & tf_error)
8061 error ("cannot resolve overloaded function %qD based on"
8062 " conversion to type %qT",
8063 OVL_NAME (overload), target_type);
8064 return error_mark_node;
8067 /* Non-member functions and static member functions match targets of type
8068 "pointer-to-function" or "reference-to-function." Nonstatic member
8069 functions match targets of type "pointer-to-member-function;" the
8070 function type of the pointer to member is used to select the member
8071 function from the set of overloaded member functions.
8073 So figure out the FUNCTION_TYPE that we want to match against. */
8074 target_fn_type = static_fn_type (target_type);
8076 /* If we can find a non-template function that matches, we can just
8077 use it. There's no point in generating template instantiations
8078 if we're just going to throw them out anyhow. But, of course, we
8079 can only do this when we don't *need* a template function. */
8080 if (!template_only)
8081 for (lkp_iterator iter (overload); iter; ++iter)
8083 tree fn = *iter;
8085 if (TREE_CODE (fn) == TEMPLATE_DECL)
8086 /* We're not looking for templates just yet. */
8087 continue;
8089 if ((TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE) != is_ptrmem)
8090 /* We're looking for a non-static member, and this isn't
8091 one, or vice versa. */
8092 continue;
8094 /* In C++17 we need the noexcept-qualifier to compare types. */
8095 if (flag_noexcept_type)
8096 maybe_instantiate_noexcept (fn);
8098 /* See if there's a match. */
8099 tree fntype = static_fn_type (fn);
8100 if (same_type_p (target_fn_type, fntype)
8101 || fnptr_conv_p (target_fn_type, fntype))
8102 matches = tree_cons (fn, NULL_TREE, matches);
8105 /* Now, if we've already got a match (or matches), there's no need
8106 to proceed to the template functions. But, if we don't have a
8107 match we need to look at them, too. */
8108 if (!matches)
8110 tree target_arg_types;
8111 tree target_ret_type;
8112 tree *args;
8113 unsigned int nargs, ia;
8114 tree arg;
8116 target_arg_types = TYPE_ARG_TYPES (target_fn_type);
8117 target_ret_type = TREE_TYPE (target_fn_type);
8119 nargs = list_length (target_arg_types);
8120 args = XALLOCAVEC (tree, nargs);
8121 for (arg = target_arg_types, ia = 0;
8122 arg != NULL_TREE && arg != void_list_node;
8123 arg = TREE_CHAIN (arg), ++ia)
8124 args[ia] = TREE_VALUE (arg);
8125 nargs = ia;
8127 for (lkp_iterator iter (overload); iter; ++iter)
8129 tree fn = *iter;
8130 tree instantiation;
8131 tree targs;
8133 if (TREE_CODE (fn) != TEMPLATE_DECL)
8134 /* We're only looking for templates. */
8135 continue;
8137 if ((TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
8138 != is_ptrmem)
8139 /* We're not looking for a non-static member, and this is
8140 one, or vice versa. */
8141 continue;
8143 tree ret = target_ret_type;
8145 /* If the template has a deduced return type, don't expose it to
8146 template argument deduction. */
8147 if (undeduced_auto_decl (fn))
8148 ret = NULL_TREE;
8150 /* Try to do argument deduction. */
8151 targs = make_tree_vec (DECL_NTPARMS (fn));
8152 instantiation = fn_type_unification (fn, explicit_targs, targs, args,
8153 nargs, ret,
8154 DEDUCE_EXACT, LOOKUP_NORMAL,
8155 false, false);
8156 if (instantiation == error_mark_node)
8157 /* Instantiation failed. */
8158 continue;
8160 /* Constraints must be satisfied. This is done before
8161 return type deduction since that instantiates the
8162 function. */
8163 if (flag_concepts && !constraints_satisfied_p (instantiation))
8164 continue;
8166 /* And now force instantiation to do return type deduction. */
8167 if (undeduced_auto_decl (instantiation))
8169 ++function_depth;
8170 instantiate_decl (instantiation, /*defer*/false, /*class*/false);
8171 --function_depth;
8173 require_deduced_type (instantiation);
8176 /* In C++17 we need the noexcept-qualifier to compare types. */
8177 if (flag_noexcept_type)
8178 maybe_instantiate_noexcept (instantiation);
8180 /* See if there's a match. */
8181 tree fntype = static_fn_type (instantiation);
8182 if (same_type_p (target_fn_type, fntype)
8183 || fnptr_conv_p (target_fn_type, fntype))
8184 matches = tree_cons (instantiation, fn, matches);
8187 /* Now, remove all but the most specialized of the matches. */
8188 if (matches)
8190 tree match = most_specialized_instantiation (matches);
8192 if (match != error_mark_node)
8193 matches = tree_cons (TREE_PURPOSE (match),
8194 NULL_TREE,
8195 NULL_TREE);
8199 /* Now we should have exactly one function in MATCHES. */
8200 if (matches == NULL_TREE)
8202 /* There were *no* matches. */
8203 if (complain & tf_error)
8205 error ("no matches converting function %qD to type %q#T",
8206 OVL_NAME (overload), target_type);
8208 print_candidates (overload);
8210 return error_mark_node;
8212 else if (TREE_CHAIN (matches))
8214 /* There were too many matches. First check if they're all
8215 the same function. */
8216 tree match = NULL_TREE;
8218 fn = TREE_PURPOSE (matches);
8220 /* For multi-versioned functions, more than one match is just fine and
8221 decls_match will return false as they are different. */
8222 for (match = TREE_CHAIN (matches); match; match = TREE_CHAIN (match))
8223 if (!decls_match (fn, TREE_PURPOSE (match))
8224 && !targetm.target_option.function_versions
8225 (fn, TREE_PURPOSE (match)))
8226 break;
8228 if (match)
8230 if (complain & tf_error)
8232 error ("converting overloaded function %qD to type %q#T is ambiguous",
8233 OVL_NAME (overload), target_type);
8235 /* Since print_candidates expects the functions in the
8236 TREE_VALUE slot, we flip them here. */
8237 for (match = matches; match; match = TREE_CHAIN (match))
8238 TREE_VALUE (match) = TREE_PURPOSE (match);
8240 print_candidates (matches);
8243 return error_mark_node;
8247 /* Good, exactly one match. Now, convert it to the correct type. */
8248 fn = TREE_PURPOSE (matches);
8250 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
8251 && !(complain & tf_ptrmem_ok) && !flag_ms_extensions)
8253 static int explained;
8255 if (!(complain & tf_error))
8256 return error_mark_node;
8258 permerror (input_location, "assuming pointer to member %qD", fn);
8259 if (!explained)
8261 inform (input_location, "(a pointer to member can only be formed with %<&%E%>)", fn);
8262 explained = 1;
8266 /* If a pointer to a function that is multi-versioned is requested, the
8267 pointer to the dispatcher function is returned instead. This works
8268 well because indirectly calling the function will dispatch the right
8269 function version at run-time. */
8270 if (DECL_FUNCTION_VERSIONED (fn))
8272 fn = get_function_version_dispatcher (fn);
8273 if (fn == NULL)
8274 return error_mark_node;
8275 /* Mark all the versions corresponding to the dispatcher as used. */
8276 if (!(complain & tf_conv))
8277 mark_versions_used (fn);
8280 /* If we're doing overload resolution purely for the purpose of
8281 determining conversion sequences, we should not consider the
8282 function used. If this conversion sequence is selected, the
8283 function will be marked as used at this point. */
8284 if (!(complain & tf_conv))
8286 /* Make =delete work with SFINAE. */
8287 if (DECL_DELETED_FN (fn) && !(complain & tf_error))
8288 return error_mark_node;
8289 if (!mark_used (fn, complain) && !(complain & tf_error))
8290 return error_mark_node;
8293 /* We could not check access to member functions when this
8294 expression was originally created since we did not know at that
8295 time to which function the expression referred. */
8296 if (DECL_FUNCTION_MEMBER_P (fn))
8298 gcc_assert (access_path);
8299 perform_or_defer_access_check (access_path, fn, fn, complain);
8302 if (TYPE_PTRFN_P (target_type) || TYPE_PTRMEMFUNC_P (target_type))
8303 return cp_build_addr_expr (fn, complain);
8304 else
8306 /* The target must be a REFERENCE_TYPE. Above, cp_build_unary_op
8307 will mark the function as addressed, but here we must do it
8308 explicitly. */
8309 cxx_mark_addressable (fn);
8311 return fn;
8315 /* This function will instantiate the type of the expression given in
8316 RHS to match the type of LHSTYPE. If errors exist, then return
8317 error_mark_node. COMPLAIN is a bit mask. If TF_ERROR is set, then
8318 we complain on errors. If we are not complaining, never modify rhs,
8319 as overload resolution wants to try many possible instantiations, in
8320 the hope that at least one will work.
8322 For non-recursive calls, LHSTYPE should be a function, pointer to
8323 function, or a pointer to member function. */
8325 tree
8326 instantiate_type (tree lhstype, tree rhs, tsubst_flags_t complain)
8328 tsubst_flags_t complain_in = complain;
8329 tree access_path = NULL_TREE;
8331 complain &= ~tf_ptrmem_ok;
8333 if (lhstype == unknown_type_node)
8335 if (complain & tf_error)
8336 error ("not enough type information");
8337 return error_mark_node;
8340 if (TREE_TYPE (rhs) != NULL_TREE && ! (type_unknown_p (rhs)))
8342 tree fntype = non_reference (lhstype);
8343 if (same_type_p (fntype, TREE_TYPE (rhs)))
8344 return rhs;
8345 if (fnptr_conv_p (fntype, TREE_TYPE (rhs)))
8346 return rhs;
8347 if (flag_ms_extensions
8348 && TYPE_PTRMEMFUNC_P (fntype)
8349 && !TYPE_PTRMEMFUNC_P (TREE_TYPE (rhs)))
8350 /* Microsoft allows `A::f' to be resolved to a
8351 pointer-to-member. */
8353 else
8355 if (complain & tf_error)
8356 error ("cannot convert %qE from type %qT to type %qT",
8357 rhs, TREE_TYPE (rhs), fntype);
8358 return error_mark_node;
8362 if (BASELINK_P (rhs))
8364 access_path = BASELINK_ACCESS_BINFO (rhs);
8365 rhs = BASELINK_FUNCTIONS (rhs);
8368 /* If we are in a template, and have a NON_DEPENDENT_EXPR, we cannot
8369 deduce any type information. */
8370 if (TREE_CODE (rhs) == NON_DEPENDENT_EXPR)
8372 if (complain & tf_error)
8373 error ("not enough type information");
8374 return error_mark_node;
8377 /* If we instantiate a template, and it is a A ?: C expression
8378 with omitted B, look through the SAVE_EXPR. */
8379 if (TREE_CODE (rhs) == SAVE_EXPR)
8380 rhs = TREE_OPERAND (rhs, 0);
8382 /* There are only a few kinds of expressions that may have a type
8383 dependent on overload resolution. */
8384 gcc_assert (TREE_CODE (rhs) == ADDR_EXPR
8385 || TREE_CODE (rhs) == COMPONENT_REF
8386 || is_overloaded_fn (rhs)
8387 || (flag_ms_extensions && TREE_CODE (rhs) == FUNCTION_DECL));
8389 /* This should really only be used when attempting to distinguish
8390 what sort of a pointer to function we have. For now, any
8391 arithmetic operation which is not supported on pointers
8392 is rejected as an error. */
8394 switch (TREE_CODE (rhs))
8396 case COMPONENT_REF:
8398 tree member = TREE_OPERAND (rhs, 1);
8400 member = instantiate_type (lhstype, member, complain);
8401 if (member != error_mark_node
8402 && TREE_SIDE_EFFECTS (TREE_OPERAND (rhs, 0)))
8403 /* Do not lose object's side effects. */
8404 return build2 (COMPOUND_EXPR, TREE_TYPE (member),
8405 TREE_OPERAND (rhs, 0), member);
8406 return member;
8409 case OFFSET_REF:
8410 rhs = TREE_OPERAND (rhs, 1);
8411 if (BASELINK_P (rhs))
8412 return instantiate_type (lhstype, rhs, complain_in);
8414 /* This can happen if we are forming a pointer-to-member for a
8415 member template. */
8416 gcc_assert (TREE_CODE (rhs) == TEMPLATE_ID_EXPR);
8418 /* Fall through. */
8420 case TEMPLATE_ID_EXPR:
8422 tree fns = TREE_OPERAND (rhs, 0);
8423 tree args = TREE_OPERAND (rhs, 1);
8425 return
8426 resolve_address_of_overloaded_function (lhstype, fns, complain_in,
8427 /*template_only=*/true,
8428 args, access_path);
8431 case OVERLOAD:
8432 case FUNCTION_DECL:
8433 return
8434 resolve_address_of_overloaded_function (lhstype, rhs, complain_in,
8435 /*template_only=*/false,
8436 /*explicit_targs=*/NULL_TREE,
8437 access_path);
8439 case ADDR_EXPR:
8441 if (PTRMEM_OK_P (rhs))
8442 complain |= tf_ptrmem_ok;
8444 return instantiate_type (lhstype, TREE_OPERAND (rhs, 0), complain);
8447 case ERROR_MARK:
8448 return error_mark_node;
8450 default:
8451 gcc_unreachable ();
8453 return error_mark_node;
8456 /* Return the name of the virtual function pointer field
8457 (as an IDENTIFIER_NODE) for the given TYPE. Note that
8458 this may have to look back through base types to find the
8459 ultimate field name. (For single inheritance, these could
8460 all be the same name. Who knows for multiple inheritance). */
8462 static tree
8463 get_vfield_name (tree type)
8465 tree binfo, base_binfo;
8467 for (binfo = TYPE_BINFO (type);
8468 BINFO_N_BASE_BINFOS (binfo);
8469 binfo = base_binfo)
8471 base_binfo = BINFO_BASE_BINFO (binfo, 0);
8473 if (BINFO_VIRTUAL_P (base_binfo)
8474 || !TYPE_CONTAINS_VPTR_P (BINFO_TYPE (base_binfo)))
8475 break;
8478 type = BINFO_TYPE (binfo);
8479 tree ctor_name = constructor_name (type);
8480 char *buf = (char *) alloca (sizeof (VFIELD_NAME_FORMAT)
8481 + IDENTIFIER_LENGTH (ctor_name) + 2);
8482 sprintf (buf, VFIELD_NAME_FORMAT, IDENTIFIER_POINTER (ctor_name));
8483 return get_identifier (buf);
8486 void
8487 print_class_statistics (void)
8489 if (! GATHER_STATISTICS)
8490 return;
8492 fprintf (stderr, "convert_harshness = %d\n", n_convert_harshness);
8493 fprintf (stderr, "compute_conversion_costs = %d\n", n_compute_conversion_costs);
8494 if (n_vtables)
8496 fprintf (stderr, "vtables = %d; vtable searches = %d\n",
8497 n_vtables, n_vtable_searches);
8498 fprintf (stderr, "vtable entries = %d; vtable elems = %d\n",
8499 n_vtable_entries, n_vtable_elems);
8503 /* Build a dummy reference to ourselves so Derived::Base (and A::A) works,
8504 according to [class]:
8505 The class-name is also inserted
8506 into the scope of the class itself. For purposes of access checking,
8507 the inserted class name is treated as if it were a public member name. */
8509 void
8510 build_self_reference (void)
8512 tree name = DECL_NAME (TYPE_NAME (current_class_type));
8513 tree value = build_lang_decl (TYPE_DECL, name, current_class_type);
8515 DECL_NONLOCAL (value) = 1;
8516 DECL_CONTEXT (value) = current_class_type;
8517 DECL_ARTIFICIAL (value) = 1;
8518 SET_DECL_SELF_REFERENCE_P (value);
8519 set_underlying_type (value);
8521 if (processing_template_decl)
8522 value = push_template_decl (value);
8524 tree saved_cas = current_access_specifier;
8525 current_access_specifier = access_public_node;
8526 finish_member_declaration (value);
8527 current_access_specifier = saved_cas;
8530 /* Returns 1 if TYPE contains only padding bytes. */
8533 is_empty_class (tree type)
8535 if (type == error_mark_node)
8536 return 0;
8538 if (! CLASS_TYPE_P (type))
8539 return 0;
8541 return CLASSTYPE_EMPTY_P (type);
8544 /* Returns true if TYPE contains no actual data, just various
8545 possible combinations of empty classes and possibly a vptr. */
8547 bool
8548 is_really_empty_class (tree type)
8550 if (CLASS_TYPE_P (type))
8552 tree field;
8553 tree binfo;
8554 tree base_binfo;
8555 int i;
8557 /* CLASSTYPE_EMPTY_P isn't set properly until the class is actually laid
8558 out, but we'd like to be able to check this before then. */
8559 if (COMPLETE_TYPE_P (type) && is_empty_class (type))
8560 return true;
8562 for (binfo = TYPE_BINFO (type), i = 0;
8563 BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
8564 if (!is_really_empty_class (BINFO_TYPE (base_binfo)))
8565 return false;
8566 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
8567 if (TREE_CODE (field) == FIELD_DECL
8568 && !DECL_ARTIFICIAL (field)
8569 /* An unnamed bit-field is not a data member. */
8570 && (DECL_NAME (field) || !DECL_C_BIT_FIELD (field))
8571 && !is_really_empty_class (TREE_TYPE (field)))
8572 return false;
8573 return true;
8575 else if (TREE_CODE (type) == ARRAY_TYPE)
8576 return (integer_zerop (array_type_nelts_top (type))
8577 || is_really_empty_class (TREE_TYPE (type)));
8578 return false;
8581 /* Note that NAME was looked up while the current class was being
8582 defined and that the result of that lookup was DECL. */
8584 void
8585 maybe_note_name_used_in_class (tree name, tree decl)
8587 splay_tree names_used;
8589 /* If we're not defining a class, there's nothing to do. */
8590 if (!(innermost_scope_kind() == sk_class
8591 && TYPE_BEING_DEFINED (current_class_type)
8592 && !LAMBDA_TYPE_P (current_class_type)))
8593 return;
8595 /* If there's already a binding for this NAME, then we don't have
8596 anything to worry about. */
8597 if (lookup_member (current_class_type, name,
8598 /*protect=*/0, /*want_type=*/false, tf_warning_or_error))
8599 return;
8601 if (!current_class_stack[current_class_depth - 1].names_used)
8602 current_class_stack[current_class_depth - 1].names_used
8603 = splay_tree_new (splay_tree_compare_pointers, 0, 0);
8604 names_used = current_class_stack[current_class_depth - 1].names_used;
8606 splay_tree_insert (names_used,
8607 (splay_tree_key) name,
8608 (splay_tree_value) decl);
8611 /* Note that NAME was declared (as DECL) in the current class. Check
8612 to see that the declaration is valid. */
8614 void
8615 note_name_declared_in_class (tree name, tree decl)
8617 splay_tree names_used;
8618 splay_tree_node n;
8620 /* Look to see if we ever used this name. */
8621 names_used
8622 = current_class_stack[current_class_depth - 1].names_used;
8623 if (!names_used)
8624 return;
8625 /* The C language allows members to be declared with a type of the same
8626 name, and the C++ standard says this diagnostic is not required. So
8627 allow it in extern "C" blocks unless predantic is specified.
8628 Allow it in all cases if -ms-extensions is specified. */
8629 if ((!pedantic && current_lang_name == lang_name_c)
8630 || flag_ms_extensions)
8631 return;
8632 n = splay_tree_lookup (names_used, (splay_tree_key) name);
8633 if (n)
8635 /* [basic.scope.class]
8637 A name N used in a class S shall refer to the same declaration
8638 in its context and when re-evaluated in the completed scope of
8639 S. */
8640 permerror (input_location, "declaration of %q#D", decl);
8641 permerror (location_of ((tree) n->value),
8642 "changes meaning of %qD from %q#D",
8643 OVL_NAME (decl), (tree) n->value);
8647 /* Returns the VAR_DECL for the complete vtable associated with BINFO.
8648 Secondary vtables are merged with primary vtables; this function
8649 will return the VAR_DECL for the primary vtable. */
8651 tree
8652 get_vtbl_decl_for_binfo (tree binfo)
8654 tree decl;
8656 decl = BINFO_VTABLE (binfo);
8657 if (decl && TREE_CODE (decl) == POINTER_PLUS_EXPR)
8659 gcc_assert (TREE_CODE (TREE_OPERAND (decl, 0)) == ADDR_EXPR);
8660 decl = TREE_OPERAND (TREE_OPERAND (decl, 0), 0);
8662 if (decl)
8663 gcc_assert (VAR_P (decl));
8664 return decl;
8668 /* Returns the binfo for the primary base of BINFO. If the resulting
8669 BINFO is a virtual base, and it is inherited elsewhere in the
8670 hierarchy, then the returned binfo might not be the primary base of
8671 BINFO in the complete object. Check BINFO_PRIMARY_P or
8672 BINFO_LOST_PRIMARY_P to be sure. */
8674 static tree
8675 get_primary_binfo (tree binfo)
8677 tree primary_base;
8679 primary_base = CLASSTYPE_PRIMARY_BINFO (BINFO_TYPE (binfo));
8680 if (!primary_base)
8681 return NULL_TREE;
8683 return copied_binfo (primary_base, binfo);
8686 /* As above, but iterate until we reach the binfo that actually provides the
8687 vptr for BINFO. */
8689 static tree
8690 most_primary_binfo (tree binfo)
8692 tree b = binfo;
8693 while (CLASSTYPE_HAS_PRIMARY_BASE_P (BINFO_TYPE (b))
8694 && !BINFO_LOST_PRIMARY_P (b))
8696 tree primary_base = get_primary_binfo (b);
8697 gcc_assert (BINFO_PRIMARY_P (primary_base)
8698 && BINFO_INHERITANCE_CHAIN (primary_base) == b);
8699 b = primary_base;
8701 return b;
8704 /* Returns true if BINFO gets its vptr from a virtual base of the most derived
8705 type. Note that the virtual inheritance might be above or below BINFO in
8706 the hierarchy. */
8708 bool
8709 vptr_via_virtual_p (tree binfo)
8711 if (TYPE_P (binfo))
8712 binfo = TYPE_BINFO (binfo);
8713 tree primary = most_primary_binfo (binfo);
8714 /* Don't limit binfo_via_virtual, we want to return true when BINFO itself is
8715 a morally virtual base. */
8716 tree virt = binfo_via_virtual (primary, NULL_TREE);
8717 return virt != NULL_TREE;
8720 /* If INDENTED_P is zero, indent to INDENT. Return nonzero. */
8722 static int
8723 maybe_indent_hierarchy (FILE * stream, int indent, int indented_p)
8725 if (!indented_p)
8726 fprintf (stream, "%*s", indent, "");
8727 return 1;
8730 /* Dump the offsets of all the bases rooted at BINFO to STREAM.
8731 INDENT should be zero when called from the top level; it is
8732 incremented recursively. IGO indicates the next expected BINFO in
8733 inheritance graph ordering. */
8735 static tree
8736 dump_class_hierarchy_r (FILE *stream,
8737 dump_flags_t flags,
8738 tree binfo,
8739 tree igo,
8740 int indent)
8742 int indented = 0;
8743 tree base_binfo;
8744 int i;
8746 indented = maybe_indent_hierarchy (stream, indent, 0);
8747 fprintf (stream, "%s (0x" HOST_WIDE_INT_PRINT_HEX ") ",
8748 type_as_string (BINFO_TYPE (binfo), TFF_PLAIN_IDENTIFIER),
8749 (HOST_WIDE_INT) (uintptr_t) binfo);
8750 if (binfo != igo)
8752 fprintf (stream, "alternative-path\n");
8753 return igo;
8755 igo = TREE_CHAIN (binfo);
8757 fprintf (stream, HOST_WIDE_INT_PRINT_DEC,
8758 tree_to_shwi (BINFO_OFFSET (binfo)));
8759 if (is_empty_class (BINFO_TYPE (binfo)))
8760 fprintf (stream, " empty");
8761 else if (CLASSTYPE_NEARLY_EMPTY_P (BINFO_TYPE (binfo)))
8762 fprintf (stream, " nearly-empty");
8763 if (BINFO_VIRTUAL_P (binfo))
8764 fprintf (stream, " virtual");
8765 fprintf (stream, "\n");
8767 indented = 0;
8768 if (BINFO_PRIMARY_P (binfo))
8770 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
8771 fprintf (stream, " primary-for %s (0x" HOST_WIDE_INT_PRINT_HEX ")",
8772 type_as_string (BINFO_TYPE (BINFO_INHERITANCE_CHAIN (binfo)),
8773 TFF_PLAIN_IDENTIFIER),
8774 (HOST_WIDE_INT) (uintptr_t) BINFO_INHERITANCE_CHAIN (binfo));
8776 if (BINFO_LOST_PRIMARY_P (binfo))
8778 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
8779 fprintf (stream, " lost-primary");
8781 if (indented)
8782 fprintf (stream, "\n");
8784 if (!(flags & TDF_SLIM))
8786 int indented = 0;
8788 if (BINFO_SUBVTT_INDEX (binfo))
8790 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
8791 fprintf (stream, " subvttidx=%s",
8792 expr_as_string (BINFO_SUBVTT_INDEX (binfo),
8793 TFF_PLAIN_IDENTIFIER));
8795 if (BINFO_VPTR_INDEX (binfo))
8797 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
8798 fprintf (stream, " vptridx=%s",
8799 expr_as_string (BINFO_VPTR_INDEX (binfo),
8800 TFF_PLAIN_IDENTIFIER));
8802 if (BINFO_VPTR_FIELD (binfo))
8804 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
8805 fprintf (stream, " vbaseoffset=%s",
8806 expr_as_string (BINFO_VPTR_FIELD (binfo),
8807 TFF_PLAIN_IDENTIFIER));
8809 if (BINFO_VTABLE (binfo))
8811 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
8812 fprintf (stream, " vptr=%s",
8813 expr_as_string (BINFO_VTABLE (binfo),
8814 TFF_PLAIN_IDENTIFIER));
8817 if (indented)
8818 fprintf (stream, "\n");
8821 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
8822 igo = dump_class_hierarchy_r (stream, flags, base_binfo, igo, indent + 2);
8824 return igo;
8827 /* Dump the BINFO hierarchy for T. */
8829 static void
8830 dump_class_hierarchy_1 (FILE *stream, dump_flags_t flags, tree t)
8832 fprintf (stream, "Class %s\n", type_as_string (t, TFF_PLAIN_IDENTIFIER));
8833 fprintf (stream, " size=%lu align=%lu\n",
8834 (unsigned long)(tree_to_shwi (TYPE_SIZE (t)) / BITS_PER_UNIT),
8835 (unsigned long)(TYPE_ALIGN (t) / BITS_PER_UNIT));
8836 fprintf (stream, " base size=%lu base align=%lu\n",
8837 (unsigned long)(tree_to_shwi (TYPE_SIZE (CLASSTYPE_AS_BASE (t)))
8838 / BITS_PER_UNIT),
8839 (unsigned long)(TYPE_ALIGN (CLASSTYPE_AS_BASE (t))
8840 / BITS_PER_UNIT));
8841 dump_class_hierarchy_r (stream, flags, TYPE_BINFO (t), TYPE_BINFO (t), 0);
8842 fprintf (stream, "\n");
8845 /* Debug interface to hierarchy dumping. */
8847 void
8848 debug_class (tree t)
8850 dump_class_hierarchy_1 (stderr, TDF_SLIM, t);
8853 static void
8854 dump_class_hierarchy (tree t)
8856 dump_flags_t flags;
8857 if (FILE *stream = dump_begin (class_dump_id, &flags))
8859 dump_class_hierarchy_1 (stream, flags, t);
8860 dump_end (class_dump_id, stream);
8864 static void
8865 dump_array (FILE * stream, tree decl)
8867 tree value;
8868 unsigned HOST_WIDE_INT ix;
8869 HOST_WIDE_INT elt;
8870 tree size = TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (decl)));
8872 elt = (tree_to_shwi (TYPE_SIZE (TREE_TYPE (TREE_TYPE (decl))))
8873 / BITS_PER_UNIT);
8874 fprintf (stream, "%s:", decl_as_string (decl, TFF_PLAIN_IDENTIFIER));
8875 fprintf (stream, " %s entries",
8876 expr_as_string (size_binop (PLUS_EXPR, size, size_one_node),
8877 TFF_PLAIN_IDENTIFIER));
8878 fprintf (stream, "\n");
8880 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (DECL_INITIAL (decl)),
8881 ix, value)
8882 fprintf (stream, "%-4ld %s\n", (long)(ix * elt),
8883 expr_as_string (value, TFF_PLAIN_IDENTIFIER));
8886 static void
8887 dump_vtable (tree t, tree binfo, tree vtable)
8889 dump_flags_t flags;
8890 FILE *stream = dump_begin (class_dump_id, &flags);
8892 if (!stream)
8893 return;
8895 if (!(flags & TDF_SLIM))
8897 int ctor_vtbl_p = TYPE_BINFO (t) != binfo;
8899 fprintf (stream, "%s for %s",
8900 ctor_vtbl_p ? "Construction vtable" : "Vtable",
8901 type_as_string (BINFO_TYPE (binfo), TFF_PLAIN_IDENTIFIER));
8902 if (ctor_vtbl_p)
8904 if (!BINFO_VIRTUAL_P (binfo))
8905 fprintf (stream, " (0x" HOST_WIDE_INT_PRINT_HEX " instance)",
8906 (HOST_WIDE_INT) (uintptr_t) binfo);
8907 fprintf (stream, " in %s", type_as_string (t, TFF_PLAIN_IDENTIFIER));
8909 fprintf (stream, "\n");
8910 dump_array (stream, vtable);
8911 fprintf (stream, "\n");
8914 dump_end (class_dump_id, stream);
8917 static void
8918 dump_vtt (tree t, tree vtt)
8920 dump_flags_t flags;
8921 FILE *stream = dump_begin (class_dump_id, &flags);
8923 if (!stream)
8924 return;
8926 if (!(flags & TDF_SLIM))
8928 fprintf (stream, "VTT for %s\n",
8929 type_as_string (t, TFF_PLAIN_IDENTIFIER));
8930 dump_array (stream, vtt);
8931 fprintf (stream, "\n");
8934 dump_end (class_dump_id, stream);
8937 /* Dump a function or thunk and its thunkees. */
8939 static void
8940 dump_thunk (FILE *stream, int indent, tree thunk)
8942 static const char spaces[] = " ";
8943 tree name = DECL_NAME (thunk);
8944 tree thunks;
8946 fprintf (stream, "%.*s%p %s %s", indent, spaces,
8947 (void *)thunk,
8948 !DECL_THUNK_P (thunk) ? "function"
8949 : DECL_THIS_THUNK_P (thunk) ? "this-thunk" : "covariant-thunk",
8950 name ? IDENTIFIER_POINTER (name) : "<unset>");
8951 if (DECL_THUNK_P (thunk))
8953 HOST_WIDE_INT fixed_adjust = THUNK_FIXED_OFFSET (thunk);
8954 tree virtual_adjust = THUNK_VIRTUAL_OFFSET (thunk);
8956 fprintf (stream, " fixed=" HOST_WIDE_INT_PRINT_DEC, fixed_adjust);
8957 if (!virtual_adjust)
8958 /*NOP*/;
8959 else if (DECL_THIS_THUNK_P (thunk))
8960 fprintf (stream, " vcall=" HOST_WIDE_INT_PRINT_DEC,
8961 tree_to_shwi (virtual_adjust));
8962 else
8963 fprintf (stream, " vbase=" HOST_WIDE_INT_PRINT_DEC "(%s)",
8964 tree_to_shwi (BINFO_VPTR_FIELD (virtual_adjust)),
8965 type_as_string (BINFO_TYPE (virtual_adjust), TFF_SCOPE));
8966 if (THUNK_ALIAS (thunk))
8967 fprintf (stream, " alias to %p", (void *)THUNK_ALIAS (thunk));
8969 fprintf (stream, "\n");
8970 for (thunks = DECL_THUNKS (thunk); thunks; thunks = TREE_CHAIN (thunks))
8971 dump_thunk (stream, indent + 2, thunks);
8974 /* Dump the thunks for FN. */
8976 void
8977 debug_thunks (tree fn)
8979 dump_thunk (stderr, 0, fn);
8982 /* Virtual function table initialization. */
8984 /* Create all the necessary vtables for T and its base classes. */
8986 static void
8987 finish_vtbls (tree t)
8989 tree vbase;
8990 vec<constructor_elt, va_gc> *v = NULL;
8991 tree vtable = BINFO_VTABLE (TYPE_BINFO (t));
8993 /* We lay out the primary and secondary vtables in one contiguous
8994 vtable. The primary vtable is first, followed by the non-virtual
8995 secondary vtables in inheritance graph order. */
8996 accumulate_vtbl_inits (TYPE_BINFO (t), TYPE_BINFO (t), TYPE_BINFO (t),
8997 vtable, t, &v);
8999 /* Then come the virtual bases, also in inheritance graph order. */
9000 for (vbase = TYPE_BINFO (t); vbase; vbase = TREE_CHAIN (vbase))
9002 if (!BINFO_VIRTUAL_P (vbase))
9003 continue;
9004 accumulate_vtbl_inits (vbase, vbase, TYPE_BINFO (t), vtable, t, &v);
9007 if (BINFO_VTABLE (TYPE_BINFO (t)))
9008 initialize_vtable (TYPE_BINFO (t), v);
9011 /* Initialize the vtable for BINFO with the INITS. */
9013 static void
9014 initialize_vtable (tree binfo, vec<constructor_elt, va_gc> *inits)
9016 tree decl;
9018 layout_vtable_decl (binfo, vec_safe_length (inits));
9019 decl = get_vtbl_decl_for_binfo (binfo);
9020 initialize_artificial_var (decl, inits);
9021 dump_vtable (BINFO_TYPE (binfo), binfo, decl);
9024 /* Build the VTT (virtual table table) for T.
9025 A class requires a VTT if it has virtual bases.
9027 This holds
9028 1 - primary virtual pointer for complete object T
9029 2 - secondary VTTs for each direct non-virtual base of T which requires a
9031 3 - secondary virtual pointers for each direct or indirect base of T which
9032 has virtual bases or is reachable via a virtual path from T.
9033 4 - secondary VTTs for each direct or indirect virtual base of T.
9035 Secondary VTTs look like complete object VTTs without part 4. */
9037 static void
9038 build_vtt (tree t)
9040 tree type;
9041 tree vtt;
9042 tree index;
9043 vec<constructor_elt, va_gc> *inits;
9045 /* Build up the initializers for the VTT. */
9046 inits = NULL;
9047 index = size_zero_node;
9048 build_vtt_inits (TYPE_BINFO (t), t, &inits, &index);
9050 /* If we didn't need a VTT, we're done. */
9051 if (!inits)
9052 return;
9054 /* Figure out the type of the VTT. */
9055 type = build_array_of_n_type (const_ptr_type_node,
9056 inits->length ());
9058 /* Now, build the VTT object itself. */
9059 vtt = build_vtable (t, mangle_vtt_for_type (t), type);
9060 initialize_artificial_var (vtt, inits);
9061 /* Add the VTT to the vtables list. */
9062 DECL_CHAIN (vtt) = DECL_CHAIN (CLASSTYPE_VTABLES (t));
9063 DECL_CHAIN (CLASSTYPE_VTABLES (t)) = vtt;
9065 dump_vtt (t, vtt);
9068 /* When building a secondary VTT, BINFO_VTABLE is set to a TREE_LIST with
9069 PURPOSE the RTTI_BINFO, VALUE the real vtable pointer for this binfo,
9070 and CHAIN the vtable pointer for this binfo after construction is
9071 complete. VALUE can also be another BINFO, in which case we recurse. */
9073 static tree
9074 binfo_ctor_vtable (tree binfo)
9076 tree vt;
9078 while (1)
9080 vt = BINFO_VTABLE (binfo);
9081 if (TREE_CODE (vt) == TREE_LIST)
9082 vt = TREE_VALUE (vt);
9083 if (TREE_CODE (vt) == TREE_BINFO)
9084 binfo = vt;
9085 else
9086 break;
9089 return vt;
9092 /* Data for secondary VTT initialization. */
9093 struct secondary_vptr_vtt_init_data
9095 /* Is this the primary VTT? */
9096 bool top_level_p;
9098 /* Current index into the VTT. */
9099 tree index;
9101 /* Vector of initializers built up. */
9102 vec<constructor_elt, va_gc> *inits;
9104 /* The type being constructed by this secondary VTT. */
9105 tree type_being_constructed;
9108 /* Recursively build the VTT-initializer for BINFO (which is in the
9109 hierarchy dominated by T). INITS points to the end of the initializer
9110 list to date. INDEX is the VTT index where the next element will be
9111 replaced. Iff BINFO is the binfo for T, this is the top level VTT (i.e.
9112 not a subvtt for some base of T). When that is so, we emit the sub-VTTs
9113 for virtual bases of T. When it is not so, we build the constructor
9114 vtables for the BINFO-in-T variant. */
9116 static void
9117 build_vtt_inits (tree binfo, tree t, vec<constructor_elt, va_gc> **inits,
9118 tree *index)
9120 int i;
9121 tree b;
9122 tree init;
9123 secondary_vptr_vtt_init_data data;
9124 int top_level_p = SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), t);
9126 /* We only need VTTs for subobjects with virtual bases. */
9127 if (!CLASSTYPE_VBASECLASSES (BINFO_TYPE (binfo)))
9128 return;
9130 /* We need to use a construction vtable if this is not the primary
9131 VTT. */
9132 if (!top_level_p)
9134 build_ctor_vtbl_group (binfo, t);
9136 /* Record the offset in the VTT where this sub-VTT can be found. */
9137 BINFO_SUBVTT_INDEX (binfo) = *index;
9140 /* Add the address of the primary vtable for the complete object. */
9141 init = binfo_ctor_vtable (binfo);
9142 CONSTRUCTOR_APPEND_ELT (*inits, NULL_TREE, init);
9143 if (top_level_p)
9145 gcc_assert (!BINFO_VPTR_INDEX (binfo));
9146 BINFO_VPTR_INDEX (binfo) = *index;
9148 *index = size_binop (PLUS_EXPR, *index, TYPE_SIZE_UNIT (ptr_type_node));
9150 /* Recursively add the secondary VTTs for non-virtual bases. */
9151 for (i = 0; BINFO_BASE_ITERATE (binfo, i, b); ++i)
9152 if (!BINFO_VIRTUAL_P (b))
9153 build_vtt_inits (b, t, inits, index);
9155 /* Add secondary virtual pointers for all subobjects of BINFO with
9156 either virtual bases or reachable along a virtual path, except
9157 subobjects that are non-virtual primary bases. */
9158 data.top_level_p = top_level_p;
9159 data.index = *index;
9160 data.inits = *inits;
9161 data.type_being_constructed = BINFO_TYPE (binfo);
9163 dfs_walk_once (binfo, dfs_build_secondary_vptr_vtt_inits, NULL, &data);
9165 *index = data.index;
9167 /* data.inits might have grown as we added secondary virtual pointers.
9168 Make sure our caller knows about the new vector. */
9169 *inits = data.inits;
9171 if (top_level_p)
9172 /* Add the secondary VTTs for virtual bases in inheritance graph
9173 order. */
9174 for (b = TYPE_BINFO (BINFO_TYPE (binfo)); b; b = TREE_CHAIN (b))
9176 if (!BINFO_VIRTUAL_P (b))
9177 continue;
9179 build_vtt_inits (b, t, inits, index);
9181 else
9182 /* Remove the ctor vtables we created. */
9183 dfs_walk_all (binfo, dfs_fixup_binfo_vtbls, NULL, binfo);
9186 /* Called from build_vtt_inits via dfs_walk. BINFO is the binfo for the base
9187 in most derived. DATA is a SECONDARY_VPTR_VTT_INIT_DATA structure. */
9189 static tree
9190 dfs_build_secondary_vptr_vtt_inits (tree binfo, void *data_)
9192 secondary_vptr_vtt_init_data *data = (secondary_vptr_vtt_init_data *)data_;
9194 /* We don't care about bases that don't have vtables. */
9195 if (!TYPE_VFIELD (BINFO_TYPE (binfo)))
9196 return dfs_skip_bases;
9198 /* We're only interested in proper subobjects of the type being
9199 constructed. */
9200 if (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), data->type_being_constructed))
9201 return NULL_TREE;
9203 /* We're only interested in bases with virtual bases or reachable
9204 via a virtual path from the type being constructed. */
9205 if (!(CLASSTYPE_VBASECLASSES (BINFO_TYPE (binfo))
9206 || binfo_via_virtual (binfo, data->type_being_constructed)))
9207 return dfs_skip_bases;
9209 /* We're not interested in non-virtual primary bases. */
9210 if (!BINFO_VIRTUAL_P (binfo) && BINFO_PRIMARY_P (binfo))
9211 return NULL_TREE;
9213 /* Record the index where this secondary vptr can be found. */
9214 if (data->top_level_p)
9216 gcc_assert (!BINFO_VPTR_INDEX (binfo));
9217 BINFO_VPTR_INDEX (binfo) = data->index;
9219 if (BINFO_VIRTUAL_P (binfo))
9221 /* It's a primary virtual base, and this is not a
9222 construction vtable. Find the base this is primary of in
9223 the inheritance graph, and use that base's vtable
9224 now. */
9225 while (BINFO_PRIMARY_P (binfo))
9226 binfo = BINFO_INHERITANCE_CHAIN (binfo);
9230 /* Add the initializer for the secondary vptr itself. */
9231 CONSTRUCTOR_APPEND_ELT (data->inits, NULL_TREE, binfo_ctor_vtable (binfo));
9233 /* Advance the vtt index. */
9234 data->index = size_binop (PLUS_EXPR, data->index,
9235 TYPE_SIZE_UNIT (ptr_type_node));
9237 return NULL_TREE;
9240 /* Called from build_vtt_inits via dfs_walk. After building
9241 constructor vtables and generating the sub-vtt from them, we need
9242 to restore the BINFO_VTABLES that were scribbled on. DATA is the
9243 binfo of the base whose sub vtt was generated. */
9245 static tree
9246 dfs_fixup_binfo_vtbls (tree binfo, void* data)
9248 tree vtable = BINFO_VTABLE (binfo);
9250 if (!TYPE_CONTAINS_VPTR_P (BINFO_TYPE (binfo)))
9251 /* If this class has no vtable, none of its bases do. */
9252 return dfs_skip_bases;
9254 if (!vtable)
9255 /* This might be a primary base, so have no vtable in this
9256 hierarchy. */
9257 return NULL_TREE;
9259 /* If we scribbled the construction vtable vptr into BINFO, clear it
9260 out now. */
9261 if (TREE_CODE (vtable) == TREE_LIST
9262 && (TREE_PURPOSE (vtable) == (tree) data))
9263 BINFO_VTABLE (binfo) = TREE_CHAIN (vtable);
9265 return NULL_TREE;
9268 /* Build the construction vtable group for BINFO which is in the
9269 hierarchy dominated by T. */
9271 static void
9272 build_ctor_vtbl_group (tree binfo, tree t)
9274 tree type;
9275 tree vtbl;
9276 tree id;
9277 tree vbase;
9278 vec<constructor_elt, va_gc> *v;
9280 /* See if we've already created this construction vtable group. */
9281 id = mangle_ctor_vtbl_for_type (t, binfo);
9282 if (IDENTIFIER_GLOBAL_VALUE (id))
9283 return;
9285 gcc_assert (!SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), t));
9286 /* Build a version of VTBL (with the wrong type) for use in
9287 constructing the addresses of secondary vtables in the
9288 construction vtable group. */
9289 vtbl = build_vtable (t, id, ptr_type_node);
9290 DECL_CONSTRUCTION_VTABLE_P (vtbl) = 1;
9291 /* Don't export construction vtables from shared libraries. Even on
9292 targets that don't support hidden visibility, this tells
9293 can_refer_decl_in_current_unit_p not to assume that it's safe to
9294 access from a different compilation unit (bz 54314). */
9295 DECL_VISIBILITY (vtbl) = VISIBILITY_HIDDEN;
9296 DECL_VISIBILITY_SPECIFIED (vtbl) = true;
9298 v = NULL;
9299 accumulate_vtbl_inits (binfo, TYPE_BINFO (TREE_TYPE (binfo)),
9300 binfo, vtbl, t, &v);
9302 /* Add the vtables for each of our virtual bases using the vbase in T
9303 binfo. */
9304 for (vbase = TYPE_BINFO (BINFO_TYPE (binfo));
9305 vbase;
9306 vbase = TREE_CHAIN (vbase))
9308 tree b;
9310 if (!BINFO_VIRTUAL_P (vbase))
9311 continue;
9312 b = copied_binfo (vbase, binfo);
9314 accumulate_vtbl_inits (b, vbase, binfo, vtbl, t, &v);
9317 /* Figure out the type of the construction vtable. */
9318 type = build_array_of_n_type (vtable_entry_type, v->length ());
9319 layout_type (type);
9320 TREE_TYPE (vtbl) = type;
9321 DECL_SIZE (vtbl) = DECL_SIZE_UNIT (vtbl) = NULL_TREE;
9322 layout_decl (vtbl, 0);
9324 /* Initialize the construction vtable. */
9325 CLASSTYPE_VTABLES (t) = chainon (CLASSTYPE_VTABLES (t), vtbl);
9326 initialize_artificial_var (vtbl, v);
9327 dump_vtable (t, binfo, vtbl);
9330 /* Add the vtbl initializers for BINFO (and its bases other than
9331 non-virtual primaries) to the list of INITS. BINFO is in the
9332 hierarchy dominated by T. RTTI_BINFO is the binfo within T of
9333 the constructor the vtbl inits should be accumulated for. (If this
9334 is the complete object vtbl then RTTI_BINFO will be TYPE_BINFO (T).)
9335 ORIG_BINFO is the binfo for this object within BINFO_TYPE (RTTI_BINFO).
9336 BINFO is the active base equivalent of ORIG_BINFO in the inheritance
9337 graph of T. Both BINFO and ORIG_BINFO will have the same BINFO_TYPE,
9338 but are not necessarily the same in terms of layout. */
9340 static void
9341 accumulate_vtbl_inits (tree binfo,
9342 tree orig_binfo,
9343 tree rtti_binfo,
9344 tree vtbl,
9345 tree t,
9346 vec<constructor_elt, va_gc> **inits)
9348 int i;
9349 tree base_binfo;
9350 int ctor_vtbl_p = !SAME_BINFO_TYPE_P (BINFO_TYPE (rtti_binfo), t);
9352 gcc_assert (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), BINFO_TYPE (orig_binfo)));
9354 /* If it doesn't have a vptr, we don't do anything. */
9355 if (!TYPE_CONTAINS_VPTR_P (BINFO_TYPE (binfo)))
9356 return;
9358 /* If we're building a construction vtable, we're not interested in
9359 subobjects that don't require construction vtables. */
9360 if (ctor_vtbl_p
9361 && !CLASSTYPE_VBASECLASSES (BINFO_TYPE (binfo))
9362 && !binfo_via_virtual (orig_binfo, BINFO_TYPE (rtti_binfo)))
9363 return;
9365 /* Build the initializers for the BINFO-in-T vtable. */
9366 dfs_accumulate_vtbl_inits (binfo, orig_binfo, rtti_binfo, vtbl, t, inits);
9368 /* Walk the BINFO and its bases. We walk in preorder so that as we
9369 initialize each vtable we can figure out at what offset the
9370 secondary vtable lies from the primary vtable. We can't use
9371 dfs_walk here because we need to iterate through bases of BINFO
9372 and RTTI_BINFO simultaneously. */
9373 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
9375 /* Skip virtual bases. */
9376 if (BINFO_VIRTUAL_P (base_binfo))
9377 continue;
9378 accumulate_vtbl_inits (base_binfo,
9379 BINFO_BASE_BINFO (orig_binfo, i),
9380 rtti_binfo, vtbl, t,
9381 inits);
9385 /* Called from accumulate_vtbl_inits. Adds the initializers for the
9386 BINFO vtable to L. */
9388 static void
9389 dfs_accumulate_vtbl_inits (tree binfo,
9390 tree orig_binfo,
9391 tree rtti_binfo,
9392 tree orig_vtbl,
9393 tree t,
9394 vec<constructor_elt, va_gc> **l)
9396 tree vtbl = NULL_TREE;
9397 int ctor_vtbl_p = !SAME_BINFO_TYPE_P (BINFO_TYPE (rtti_binfo), t);
9398 int n_inits;
9400 if (ctor_vtbl_p
9401 && BINFO_VIRTUAL_P (orig_binfo) && BINFO_PRIMARY_P (orig_binfo))
9403 /* In the hierarchy of BINFO_TYPE (RTTI_BINFO), this is a
9404 primary virtual base. If it is not the same primary in
9405 the hierarchy of T, we'll need to generate a ctor vtable
9406 for it, to place at its location in T. If it is the same
9407 primary, we still need a VTT entry for the vtable, but it
9408 should point to the ctor vtable for the base it is a
9409 primary for within the sub-hierarchy of RTTI_BINFO.
9411 There are three possible cases:
9413 1) We are in the same place.
9414 2) We are a primary base within a lost primary virtual base of
9415 RTTI_BINFO.
9416 3) We are primary to something not a base of RTTI_BINFO. */
9418 tree b;
9419 tree last = NULL_TREE;
9421 /* First, look through the bases we are primary to for RTTI_BINFO
9422 or a virtual base. */
9423 b = binfo;
9424 while (BINFO_PRIMARY_P (b))
9426 b = BINFO_INHERITANCE_CHAIN (b);
9427 last = b;
9428 if (BINFO_VIRTUAL_P (b) || b == rtti_binfo)
9429 goto found;
9431 /* If we run out of primary links, keep looking down our
9432 inheritance chain; we might be an indirect primary. */
9433 for (b = last; b; b = BINFO_INHERITANCE_CHAIN (b))
9434 if (BINFO_VIRTUAL_P (b) || b == rtti_binfo)
9435 break;
9436 found:
9438 /* If we found RTTI_BINFO, this is case 1. If we found a virtual
9439 base B and it is a base of RTTI_BINFO, this is case 2. In
9440 either case, we share our vtable with LAST, i.e. the
9441 derived-most base within B of which we are a primary. */
9442 if (b == rtti_binfo
9443 || (b && binfo_for_vbase (BINFO_TYPE (b), BINFO_TYPE (rtti_binfo))))
9444 /* Just set our BINFO_VTABLE to point to LAST, as we may not have
9445 set LAST's BINFO_VTABLE yet. We'll extract the actual vptr in
9446 binfo_ctor_vtable after everything's been set up. */
9447 vtbl = last;
9449 /* Otherwise, this is case 3 and we get our own. */
9451 else if (!BINFO_NEW_VTABLE_MARKED (orig_binfo))
9452 return;
9454 n_inits = vec_safe_length (*l);
9456 if (!vtbl)
9458 tree index;
9459 int non_fn_entries;
9461 /* Add the initializer for this vtable. */
9462 build_vtbl_initializer (binfo, orig_binfo, t, rtti_binfo,
9463 &non_fn_entries, l);
9465 /* Figure out the position to which the VPTR should point. */
9466 vtbl = build1 (ADDR_EXPR, vtbl_ptr_type_node, orig_vtbl);
9467 index = size_binop (MULT_EXPR,
9468 TYPE_SIZE_UNIT (vtable_entry_type),
9469 size_int (non_fn_entries + n_inits));
9470 vtbl = fold_build_pointer_plus (vtbl, index);
9473 if (ctor_vtbl_p)
9474 /* For a construction vtable, we can't overwrite BINFO_VTABLE.
9475 So, we make a TREE_LIST. Later, dfs_fixup_binfo_vtbls will
9476 straighten this out. */
9477 BINFO_VTABLE (binfo) = tree_cons (rtti_binfo, vtbl, BINFO_VTABLE (binfo));
9478 else if (BINFO_PRIMARY_P (binfo) && BINFO_VIRTUAL_P (binfo))
9479 /* Throw away any unneeded intializers. */
9480 (*l)->truncate (n_inits);
9481 else
9482 /* For an ordinary vtable, set BINFO_VTABLE. */
9483 BINFO_VTABLE (binfo) = vtbl;
9486 static GTY(()) tree abort_fndecl_addr;
9487 static GTY(()) tree dvirt_fn;
9489 /* Construct the initializer for BINFO's virtual function table. BINFO
9490 is part of the hierarchy dominated by T. If we're building a
9491 construction vtable, the ORIG_BINFO is the binfo we should use to
9492 find the actual function pointers to put in the vtable - but they
9493 can be overridden on the path to most-derived in the graph that
9494 ORIG_BINFO belongs. Otherwise,
9495 ORIG_BINFO should be the same as BINFO. The RTTI_BINFO is the
9496 BINFO that should be indicated by the RTTI information in the
9497 vtable; it will be a base class of T, rather than T itself, if we
9498 are building a construction vtable.
9500 The value returned is a TREE_LIST suitable for wrapping in a
9501 CONSTRUCTOR to use as the DECL_INITIAL for a vtable. If
9502 NON_FN_ENTRIES_P is not NULL, *NON_FN_ENTRIES_P is set to the
9503 number of non-function entries in the vtable.
9505 It might seem that this function should never be called with a
9506 BINFO for which BINFO_PRIMARY_P holds, the vtable for such a
9507 base is always subsumed by a derived class vtable. However, when
9508 we are building construction vtables, we do build vtables for
9509 primary bases; we need these while the primary base is being
9510 constructed. */
9512 static void
9513 build_vtbl_initializer (tree binfo,
9514 tree orig_binfo,
9515 tree t,
9516 tree rtti_binfo,
9517 int* non_fn_entries_p,
9518 vec<constructor_elt, va_gc> **inits)
9520 tree v;
9521 vtbl_init_data vid;
9522 unsigned ix, jx;
9523 tree vbinfo;
9524 vec<tree, va_gc> *vbases;
9525 constructor_elt *e;
9527 /* Initialize VID. */
9528 memset (&vid, 0, sizeof (vid));
9529 vid.binfo = binfo;
9530 vid.derived = t;
9531 vid.rtti_binfo = rtti_binfo;
9532 vid.primary_vtbl_p = SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), t);
9533 vid.ctor_vtbl_p = !SAME_BINFO_TYPE_P (BINFO_TYPE (rtti_binfo), t);
9534 vid.generate_vcall_entries = true;
9535 /* The first vbase or vcall offset is at index -3 in the vtable. */
9536 vid.index = ssize_int(-3 * TARGET_VTABLE_DATA_ENTRY_DISTANCE);
9538 /* Add entries to the vtable for RTTI. */
9539 build_rtti_vtbl_entries (binfo, &vid);
9541 /* Create an array for keeping track of the functions we've
9542 processed. When we see multiple functions with the same
9543 signature, we share the vcall offsets. */
9544 vec_alloc (vid.fns, 32);
9545 /* Add the vcall and vbase offset entries. */
9546 build_vcall_and_vbase_vtbl_entries (binfo, &vid);
9548 /* Clear BINFO_VTABLE_PATH_MARKED; it's set by
9549 build_vbase_offset_vtbl_entries. */
9550 for (vbases = CLASSTYPE_VBASECLASSES (t), ix = 0;
9551 vec_safe_iterate (vbases, ix, &vbinfo); ix++)
9552 BINFO_VTABLE_PATH_MARKED (vbinfo) = 0;
9554 /* If the target requires padding between data entries, add that now. */
9555 if (TARGET_VTABLE_DATA_ENTRY_DISTANCE > 1)
9557 int n_entries = vec_safe_length (vid.inits);
9559 vec_safe_grow (vid.inits, TARGET_VTABLE_DATA_ENTRY_DISTANCE * n_entries);
9561 /* Move data entries into their new positions and add padding
9562 after the new positions. Iterate backwards so we don't
9563 overwrite entries that we would need to process later. */
9564 for (ix = n_entries - 1;
9565 vid.inits->iterate (ix, &e);
9566 ix--)
9568 int j;
9569 int new_position = (TARGET_VTABLE_DATA_ENTRY_DISTANCE * ix
9570 + (TARGET_VTABLE_DATA_ENTRY_DISTANCE - 1));
9572 (*vid.inits)[new_position] = *e;
9574 for (j = 1; j < TARGET_VTABLE_DATA_ENTRY_DISTANCE; ++j)
9576 constructor_elt *f = &(*vid.inits)[new_position - j];
9577 f->index = NULL_TREE;
9578 f->value = build1 (NOP_EXPR, vtable_entry_type,
9579 null_pointer_node);
9584 if (non_fn_entries_p)
9585 *non_fn_entries_p = vec_safe_length (vid.inits);
9587 /* The initializers for virtual functions were built up in reverse
9588 order. Straighten them out and add them to the running list in one
9589 step. */
9590 jx = vec_safe_length (*inits);
9591 vec_safe_grow (*inits, jx + vid.inits->length ());
9593 for (ix = vid.inits->length () - 1;
9594 vid.inits->iterate (ix, &e);
9595 ix--, jx++)
9596 (**inits)[jx] = *e;
9598 /* Go through all the ordinary virtual functions, building up
9599 initializers. */
9600 for (v = BINFO_VIRTUALS (orig_binfo); v; v = TREE_CHAIN (v))
9602 tree delta;
9603 tree vcall_index;
9604 tree fn, fn_original;
9605 tree init = NULL_TREE;
9607 fn = BV_FN (v);
9608 fn_original = fn;
9609 if (DECL_THUNK_P (fn))
9611 if (!DECL_NAME (fn))
9612 finish_thunk (fn);
9613 if (THUNK_ALIAS (fn))
9615 fn = THUNK_ALIAS (fn);
9616 BV_FN (v) = fn;
9618 fn_original = THUNK_TARGET (fn);
9621 /* If the only definition of this function signature along our
9622 primary base chain is from a lost primary, this vtable slot will
9623 never be used, so just zero it out. This is important to avoid
9624 requiring extra thunks which cannot be generated with the function.
9626 We first check this in update_vtable_entry_for_fn, so we handle
9627 restored primary bases properly; we also need to do it here so we
9628 zero out unused slots in ctor vtables, rather than filling them
9629 with erroneous values (though harmless, apart from relocation
9630 costs). */
9631 if (BV_LOST_PRIMARY (v))
9632 init = size_zero_node;
9634 if (! init)
9636 /* Pull the offset for `this', and the function to call, out of
9637 the list. */
9638 delta = BV_DELTA (v);
9639 vcall_index = BV_VCALL_INDEX (v);
9641 gcc_assert (TREE_CODE (delta) == INTEGER_CST);
9642 gcc_assert (TREE_CODE (fn) == FUNCTION_DECL);
9644 /* You can't call an abstract virtual function; it's abstract.
9645 So, we replace these functions with __pure_virtual. */
9646 if (DECL_PURE_VIRTUAL_P (fn_original))
9648 fn = abort_fndecl;
9649 if (!TARGET_VTABLE_USES_DESCRIPTORS)
9651 if (abort_fndecl_addr == NULL)
9652 abort_fndecl_addr
9653 = fold_convert (vfunc_ptr_type_node,
9654 build_fold_addr_expr (fn));
9655 init = abort_fndecl_addr;
9658 /* Likewise for deleted virtuals. */
9659 else if (DECL_DELETED_FN (fn_original))
9661 if (!dvirt_fn)
9663 tree name = get_identifier ("__cxa_deleted_virtual");
9664 dvirt_fn = IDENTIFIER_GLOBAL_VALUE (name);
9665 if (!dvirt_fn)
9666 dvirt_fn = push_library_fn
9667 (name,
9668 build_function_type_list (void_type_node, NULL_TREE),
9669 NULL_TREE, ECF_NORETURN | ECF_COLD);
9671 fn = dvirt_fn;
9672 if (!TARGET_VTABLE_USES_DESCRIPTORS)
9673 init = fold_convert (vfunc_ptr_type_node,
9674 build_fold_addr_expr (fn));
9676 else
9678 if (!integer_zerop (delta) || vcall_index)
9680 fn = make_thunk (fn, /*this_adjusting=*/1,
9681 delta, vcall_index);
9682 if (!DECL_NAME (fn))
9683 finish_thunk (fn);
9685 /* Take the address of the function, considering it to be of an
9686 appropriate generic type. */
9687 if (!TARGET_VTABLE_USES_DESCRIPTORS)
9688 init = fold_convert (vfunc_ptr_type_node,
9689 build_fold_addr_expr (fn));
9690 /* Don't refer to a virtual destructor from a constructor
9691 vtable or a vtable for an abstract class, since destroying
9692 an object under construction is undefined behavior and we
9693 don't want it to be considered a candidate for speculative
9694 devirtualization. But do create the thunk for ABI
9695 compliance. */
9696 if (DECL_DESTRUCTOR_P (fn_original)
9697 && (CLASSTYPE_PURE_VIRTUALS (DECL_CONTEXT (fn_original))
9698 || orig_binfo != binfo))
9699 init = size_zero_node;
9703 /* And add it to the chain of initializers. */
9704 if (TARGET_VTABLE_USES_DESCRIPTORS)
9706 int i;
9707 if (init == size_zero_node)
9708 for (i = 0; i < TARGET_VTABLE_USES_DESCRIPTORS; ++i)
9709 CONSTRUCTOR_APPEND_ELT (*inits, NULL_TREE, init);
9710 else
9711 for (i = 0; i < TARGET_VTABLE_USES_DESCRIPTORS; ++i)
9713 tree fdesc = build2 (FDESC_EXPR, vfunc_ptr_type_node,
9714 fn, build_int_cst (NULL_TREE, i));
9715 TREE_CONSTANT (fdesc) = 1;
9717 CONSTRUCTOR_APPEND_ELT (*inits, NULL_TREE, fdesc);
9720 else
9721 CONSTRUCTOR_APPEND_ELT (*inits, NULL_TREE, init);
9725 /* Adds to vid->inits the initializers for the vbase and vcall
9726 offsets in BINFO, which is in the hierarchy dominated by T. */
9728 static void
9729 build_vcall_and_vbase_vtbl_entries (tree binfo, vtbl_init_data* vid)
9731 tree b;
9733 /* If this is a derived class, we must first create entries
9734 corresponding to the primary base class. */
9735 b = get_primary_binfo (binfo);
9736 if (b)
9737 build_vcall_and_vbase_vtbl_entries (b, vid);
9739 /* Add the vbase entries for this base. */
9740 build_vbase_offset_vtbl_entries (binfo, vid);
9741 /* Add the vcall entries for this base. */
9742 build_vcall_offset_vtbl_entries (binfo, vid);
9745 /* Returns the initializers for the vbase offset entries in the vtable
9746 for BINFO (which is part of the class hierarchy dominated by T), in
9747 reverse order. VBASE_OFFSET_INDEX gives the vtable index
9748 where the next vbase offset will go. */
9750 static void
9751 build_vbase_offset_vtbl_entries (tree binfo, vtbl_init_data* vid)
9753 tree vbase;
9754 tree t;
9755 tree non_primary_binfo;
9757 /* If there are no virtual baseclasses, then there is nothing to
9758 do. */
9759 if (!CLASSTYPE_VBASECLASSES (BINFO_TYPE (binfo)))
9760 return;
9762 t = vid->derived;
9764 /* We might be a primary base class. Go up the inheritance hierarchy
9765 until we find the most derived class of which we are a primary base:
9766 it is the offset of that which we need to use. */
9767 non_primary_binfo = binfo;
9768 while (BINFO_INHERITANCE_CHAIN (non_primary_binfo))
9770 tree b;
9772 /* If we have reached a virtual base, then it must be a primary
9773 base (possibly multi-level) of vid->binfo, or we wouldn't
9774 have called build_vcall_and_vbase_vtbl_entries for it. But it
9775 might be a lost primary, so just skip down to vid->binfo. */
9776 if (BINFO_VIRTUAL_P (non_primary_binfo))
9778 non_primary_binfo = vid->binfo;
9779 break;
9782 b = BINFO_INHERITANCE_CHAIN (non_primary_binfo);
9783 if (get_primary_binfo (b) != non_primary_binfo)
9784 break;
9785 non_primary_binfo = b;
9788 /* Go through the virtual bases, adding the offsets. */
9789 for (vbase = TYPE_BINFO (BINFO_TYPE (binfo));
9790 vbase;
9791 vbase = TREE_CHAIN (vbase))
9793 tree b;
9794 tree delta;
9796 if (!BINFO_VIRTUAL_P (vbase))
9797 continue;
9799 /* Find the instance of this virtual base in the complete
9800 object. */
9801 b = copied_binfo (vbase, binfo);
9803 /* If we've already got an offset for this virtual base, we
9804 don't need another one. */
9805 if (BINFO_VTABLE_PATH_MARKED (b))
9806 continue;
9807 BINFO_VTABLE_PATH_MARKED (b) = 1;
9809 /* Figure out where we can find this vbase offset. */
9810 delta = size_binop (MULT_EXPR,
9811 vid->index,
9812 fold_convert (ssizetype,
9813 TYPE_SIZE_UNIT (vtable_entry_type)));
9814 if (vid->primary_vtbl_p)
9815 BINFO_VPTR_FIELD (b) = delta;
9817 if (binfo != TYPE_BINFO (t))
9818 /* The vbase offset had better be the same. */
9819 gcc_assert (tree_int_cst_equal (delta, BINFO_VPTR_FIELD (vbase)));
9821 /* The next vbase will come at a more negative offset. */
9822 vid->index = size_binop (MINUS_EXPR, vid->index,
9823 ssize_int (TARGET_VTABLE_DATA_ENTRY_DISTANCE));
9825 /* The initializer is the delta from BINFO to this virtual base.
9826 The vbase offsets go in reverse inheritance-graph order, and
9827 we are walking in inheritance graph order so these end up in
9828 the right order. */
9829 delta = size_diffop_loc (input_location,
9830 BINFO_OFFSET (b), BINFO_OFFSET (non_primary_binfo));
9832 CONSTRUCTOR_APPEND_ELT (vid->inits, NULL_TREE,
9833 fold_build1_loc (input_location, NOP_EXPR,
9834 vtable_entry_type, delta));
9838 /* Adds the initializers for the vcall offset entries in the vtable
9839 for BINFO (which is part of the class hierarchy dominated by VID->DERIVED)
9840 to VID->INITS. */
9842 static void
9843 build_vcall_offset_vtbl_entries (tree binfo, vtbl_init_data* vid)
9845 /* We only need these entries if this base is a virtual base. We
9846 compute the indices -- but do not add to the vtable -- when
9847 building the main vtable for a class. */
9848 if (binfo == TYPE_BINFO (vid->derived)
9849 || (BINFO_VIRTUAL_P (binfo)
9850 /* If BINFO is RTTI_BINFO, then (since BINFO does not
9851 correspond to VID->DERIVED), we are building a primary
9852 construction virtual table. Since this is a primary
9853 virtual table, we do not need the vcall offsets for
9854 BINFO. */
9855 && binfo != vid->rtti_binfo))
9857 /* We need a vcall offset for each of the virtual functions in this
9858 vtable. For example:
9860 class A { virtual void f (); };
9861 class B1 : virtual public A { virtual void f (); };
9862 class B2 : virtual public A { virtual void f (); };
9863 class C: public B1, public B2 { virtual void f (); };
9865 A C object has a primary base of B1, which has a primary base of A. A
9866 C also has a secondary base of B2, which no longer has a primary base
9867 of A. So the B2-in-C construction vtable needs a secondary vtable for
9868 A, which will adjust the A* to a B2* to call f. We have no way of
9869 knowing what (or even whether) this offset will be when we define B2,
9870 so we store this "vcall offset" in the A sub-vtable and look it up in
9871 a "virtual thunk" for B2::f.
9873 We need entries for all the functions in our primary vtable and
9874 in our non-virtual bases' secondary vtables. */
9875 vid->vbase = binfo;
9876 /* If we are just computing the vcall indices -- but do not need
9877 the actual entries -- not that. */
9878 if (!BINFO_VIRTUAL_P (binfo))
9879 vid->generate_vcall_entries = false;
9880 /* Now, walk through the non-virtual bases, adding vcall offsets. */
9881 add_vcall_offset_vtbl_entries_r (binfo, vid);
9885 /* Build vcall offsets, starting with those for BINFO. */
9887 static void
9888 add_vcall_offset_vtbl_entries_r (tree binfo, vtbl_init_data* vid)
9890 int i;
9891 tree primary_binfo;
9892 tree base_binfo;
9894 /* Don't walk into virtual bases -- except, of course, for the
9895 virtual base for which we are building vcall offsets. Any
9896 primary virtual base will have already had its offsets generated
9897 through the recursion in build_vcall_and_vbase_vtbl_entries. */
9898 if (BINFO_VIRTUAL_P (binfo) && vid->vbase != binfo)
9899 return;
9901 /* If BINFO has a primary base, process it first. */
9902 primary_binfo = get_primary_binfo (binfo);
9903 if (primary_binfo)
9904 add_vcall_offset_vtbl_entries_r (primary_binfo, vid);
9906 /* Add BINFO itself to the list. */
9907 add_vcall_offset_vtbl_entries_1 (binfo, vid);
9909 /* Scan the non-primary bases of BINFO. */
9910 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
9911 if (base_binfo != primary_binfo)
9912 add_vcall_offset_vtbl_entries_r (base_binfo, vid);
9915 /* Called from build_vcall_offset_vtbl_entries_r. */
9917 static void
9918 add_vcall_offset_vtbl_entries_1 (tree binfo, vtbl_init_data* vid)
9920 /* Make entries for the rest of the virtuals. */
9921 tree orig_fn;
9923 /* The ABI requires that the methods be processed in declaration
9924 order. */
9925 for (orig_fn = TYPE_METHODS (BINFO_TYPE (binfo));
9926 orig_fn;
9927 orig_fn = DECL_CHAIN (orig_fn))
9928 if (TREE_CODE (orig_fn) == FUNCTION_DECL && DECL_VINDEX (orig_fn))
9929 add_vcall_offset (orig_fn, binfo, vid);
9932 /* Add a vcall offset entry for ORIG_FN to the vtable. */
9934 static void
9935 add_vcall_offset (tree orig_fn, tree binfo, vtbl_init_data *vid)
9937 size_t i;
9938 tree vcall_offset;
9939 tree derived_entry;
9941 /* If there is already an entry for a function with the same
9942 signature as FN, then we do not need a second vcall offset.
9943 Check the list of functions already present in the derived
9944 class vtable. */
9945 FOR_EACH_VEC_SAFE_ELT (vid->fns, i, derived_entry)
9947 if (same_signature_p (derived_entry, orig_fn)
9948 /* We only use one vcall offset for virtual destructors,
9949 even though there are two virtual table entries. */
9950 || (DECL_DESTRUCTOR_P (derived_entry)
9951 && DECL_DESTRUCTOR_P (orig_fn)))
9952 return;
9955 /* If we are building these vcall offsets as part of building
9956 the vtable for the most derived class, remember the vcall
9957 offset. */
9958 if (vid->binfo == TYPE_BINFO (vid->derived))
9960 tree_pair_s elt = {orig_fn, vid->index};
9961 vec_safe_push (CLASSTYPE_VCALL_INDICES (vid->derived), elt);
9964 /* The next vcall offset will be found at a more negative
9965 offset. */
9966 vid->index = size_binop (MINUS_EXPR, vid->index,
9967 ssize_int (TARGET_VTABLE_DATA_ENTRY_DISTANCE));
9969 /* Keep track of this function. */
9970 vec_safe_push (vid->fns, orig_fn);
9972 if (vid->generate_vcall_entries)
9974 tree base;
9975 tree fn;
9977 /* Find the overriding function. */
9978 fn = find_final_overrider (vid->rtti_binfo, binfo, orig_fn);
9979 if (fn == error_mark_node)
9980 vcall_offset = build_zero_cst (vtable_entry_type);
9981 else
9983 base = TREE_VALUE (fn);
9985 /* The vbase we're working on is a primary base of
9986 vid->binfo. But it might be a lost primary, so its
9987 BINFO_OFFSET might be wrong, so we just use the
9988 BINFO_OFFSET from vid->binfo. */
9989 vcall_offset = size_diffop_loc (input_location,
9990 BINFO_OFFSET (base),
9991 BINFO_OFFSET (vid->binfo));
9992 vcall_offset = fold_build1_loc (input_location,
9993 NOP_EXPR, vtable_entry_type,
9994 vcall_offset);
9996 /* Add the initializer to the vtable. */
9997 CONSTRUCTOR_APPEND_ELT (vid->inits, NULL_TREE, vcall_offset);
10001 /* Return vtbl initializers for the RTTI entries corresponding to the
10002 BINFO's vtable. The RTTI entries should indicate the object given
10003 by VID->rtti_binfo. */
10005 static void
10006 build_rtti_vtbl_entries (tree binfo, vtbl_init_data* vid)
10008 tree b;
10009 tree t;
10010 tree offset;
10011 tree decl;
10012 tree init;
10014 t = BINFO_TYPE (vid->rtti_binfo);
10016 /* To find the complete object, we will first convert to our most
10017 primary base, and then add the offset in the vtbl to that value. */
10018 b = most_primary_binfo (binfo);
10019 offset = size_diffop_loc (input_location,
10020 BINFO_OFFSET (vid->rtti_binfo), BINFO_OFFSET (b));
10022 /* The second entry is the address of the typeinfo object. */
10023 if (flag_rtti)
10024 decl = build_address (get_tinfo_decl (t));
10025 else
10026 decl = integer_zero_node;
10028 /* Convert the declaration to a type that can be stored in the
10029 vtable. */
10030 init = build_nop (vfunc_ptr_type_node, decl);
10031 CONSTRUCTOR_APPEND_ELT (vid->inits, NULL_TREE, init);
10033 /* Add the offset-to-top entry. It comes earlier in the vtable than
10034 the typeinfo entry. Convert the offset to look like a
10035 function pointer, so that we can put it in the vtable. */
10036 init = build_nop (vfunc_ptr_type_node, offset);
10037 CONSTRUCTOR_APPEND_ELT (vid->inits, NULL_TREE, init);
10040 /* TRUE iff TYPE is uniquely derived from PARENT. Ignores
10041 accessibility. */
10043 bool
10044 uniquely_derived_from_p (tree parent, tree type)
10046 tree base = lookup_base (type, parent, ba_unique, NULL, tf_none);
10047 return base && base != error_mark_node;
10050 /* TRUE iff TYPE is publicly & uniquely derived from PARENT. */
10052 bool
10053 publicly_uniquely_derived_p (tree parent, tree type)
10055 tree base = lookup_base (type, parent, ba_ignore_scope | ba_check,
10056 NULL, tf_none);
10057 return base && base != error_mark_node;
10060 /* CTX1 and CTX2 are declaration contexts. Return the innermost common
10061 class between them, if any. */
10063 tree
10064 common_enclosing_class (tree ctx1, tree ctx2)
10066 if (!TYPE_P (ctx1) || !TYPE_P (ctx2))
10067 return NULL_TREE;
10068 gcc_assert (ctx1 == TYPE_MAIN_VARIANT (ctx1)
10069 && ctx2 == TYPE_MAIN_VARIANT (ctx2));
10070 if (ctx1 == ctx2)
10071 return ctx1;
10072 for (tree t = ctx1; TYPE_P (t); t = TYPE_CONTEXT (t))
10073 TYPE_MARKED_P (t) = true;
10074 tree found = NULL_TREE;
10075 for (tree t = ctx2; TYPE_P (t); t = TYPE_CONTEXT (t))
10076 if (TYPE_MARKED_P (t))
10078 found = t;
10079 break;
10081 for (tree t = ctx1; TYPE_P (t); t = TYPE_CONTEXT (t))
10082 TYPE_MARKED_P (t) = false;
10083 return found;
10086 #include "gt-cp-class.h"