Introduce C++ support in libcc1
[official-gcc.git] / gcc / cp / class.c
blobd99ebcdc7dfd8993070e51857bd9178488ac904a
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"
40 /* The number of nested classes being processed. If we are not in the
41 scope of any class, this is zero. */
43 int current_class_depth;
45 /* In order to deal with nested classes, we keep a stack of classes.
46 The topmost entry is the innermost class, and is the entry at index
47 CURRENT_CLASS_DEPTH */
49 typedef struct class_stack_node {
50 /* The name of the class. */
51 tree name;
53 /* The _TYPE node for the class. */
54 tree type;
56 /* The access specifier pending for new declarations in the scope of
57 this class. */
58 tree access;
60 /* If were defining TYPE, the names used in this class. */
61 splay_tree names_used;
63 /* Nonzero if this class is no longer open, because of a call to
64 push_to_top_level. */
65 size_t hidden;
66 }* class_stack_node_t;
68 struct vtbl_init_data
70 /* The base for which we're building initializers. */
71 tree binfo;
72 /* The type of the most-derived type. */
73 tree derived;
74 /* The binfo for the dynamic type. This will be TYPE_BINFO (derived),
75 unless ctor_vtbl_p is true. */
76 tree rtti_binfo;
77 /* The negative-index vtable initializers built up so far. These
78 are in order from least negative index to most negative index. */
79 vec<constructor_elt, va_gc> *inits;
80 /* The binfo for the virtual base for which we're building
81 vcall offset initializers. */
82 tree vbase;
83 /* The functions in vbase for which we have already provided vcall
84 offsets. */
85 vec<tree, va_gc> *fns;
86 /* The vtable index of the next vcall or vbase offset. */
87 tree index;
88 /* Nonzero if we are building the initializer for the primary
89 vtable. */
90 int primary_vtbl_p;
91 /* Nonzero if we are building the initializer for a construction
92 vtable. */
93 int ctor_vtbl_p;
94 /* True when adding vcall offset entries to the vtable. False when
95 merely computing the indices. */
96 bool generate_vcall_entries;
99 /* The type of a function passed to walk_subobject_offsets. */
100 typedef int (*subobject_offset_fn) (tree, tree, splay_tree);
102 /* The stack itself. This is a dynamically resized array. The
103 number of elements allocated is CURRENT_CLASS_STACK_SIZE. */
104 static int current_class_stack_size;
105 static class_stack_node_t current_class_stack;
107 /* The size of the largest empty class seen in this translation unit. */
108 static GTY (()) tree sizeof_biggest_empty_class;
110 /* An array of all local classes present in this translation unit, in
111 declaration order. */
112 vec<tree, va_gc> *local_classes;
114 static tree get_vfield_name (tree);
115 static void finish_struct_anon (tree);
116 static tree get_vtable_name (tree);
117 static void get_basefndecls (tree, tree, vec<tree> *);
118 static int build_primary_vtable (tree, tree);
119 static int build_secondary_vtable (tree);
120 static void finish_vtbls (tree);
121 static void modify_vtable_entry (tree, tree, tree, tree, tree *);
122 static void finish_struct_bits (tree);
123 static int alter_access (tree, tree, tree);
124 static void handle_using_decl (tree, tree);
125 static tree dfs_modify_vtables (tree, void *);
126 static tree modify_all_vtables (tree, tree);
127 static void determine_primary_bases (tree);
128 static void finish_struct_methods (tree);
129 static void maybe_warn_about_overly_private_class (tree);
130 static int method_name_cmp (const void *, const void *);
131 static int resort_method_name_cmp (const void *, const void *);
132 static void add_implicitly_declared_members (tree, tree*, int, int);
133 static tree fixed_type_or_null (tree, int *, int *);
134 static tree build_simple_base_path (tree expr, tree binfo);
135 static tree build_vtbl_ref_1 (tree, tree);
136 static void build_vtbl_initializer (tree, tree, tree, tree, int *,
137 vec<constructor_elt, va_gc> **);
138 static int count_fields (tree);
139 static int add_fields_to_record_type (tree, struct sorted_fields_type*, int);
140 static void insert_into_classtype_sorted_fields (tree, tree, int);
141 static bool check_bitfield_decl (tree);
142 static bool check_field_decl (tree, tree, int *, int *);
143 static void check_field_decls (tree, tree *, int *, int *);
144 static tree *build_base_field (record_layout_info, tree, splay_tree, tree *);
145 static void build_base_fields (record_layout_info, splay_tree, tree *);
146 static void check_methods (tree);
147 static void remove_zero_width_bit_fields (tree);
148 static bool accessible_nvdtor_p (tree);
150 /* Used by find_flexarrays and related functions. */
151 struct flexmems_t;
152 static void diagnose_flexarrays (tree, const flexmems_t *);
153 static void find_flexarrays (tree, flexmems_t *, bool = false,
154 tree = NULL_TREE, tree = NULL_TREE);
155 static void check_flexarrays (tree, flexmems_t * = NULL, bool = false);
156 static void check_bases (tree, int *, int *);
157 static void check_bases_and_members (tree);
158 static tree create_vtable_ptr (tree, tree *);
159 static void include_empty_classes (record_layout_info);
160 static void layout_class_type (tree, tree *);
161 static void propagate_binfo_offsets (tree, tree);
162 static void layout_virtual_bases (record_layout_info, splay_tree);
163 static void build_vbase_offset_vtbl_entries (tree, vtbl_init_data *);
164 static void add_vcall_offset_vtbl_entries_r (tree, vtbl_init_data *);
165 static void add_vcall_offset_vtbl_entries_1 (tree, vtbl_init_data *);
166 static void build_vcall_offset_vtbl_entries (tree, vtbl_init_data *);
167 static void add_vcall_offset (tree, tree, vtbl_init_data *);
168 static void layout_vtable_decl (tree, int);
169 static tree dfs_find_final_overrider_pre (tree, void *);
170 static tree dfs_find_final_overrider_post (tree, void *);
171 static tree find_final_overrider (tree, tree, tree);
172 static int make_new_vtable (tree, tree);
173 static tree get_primary_binfo (tree);
174 static int maybe_indent_hierarchy (FILE *, int, int);
175 static tree dump_class_hierarchy_r (FILE *, int, tree, tree, int);
176 static void dump_class_hierarchy (tree);
177 static void dump_class_hierarchy_1 (FILE *, int, tree);
178 static void dump_array (FILE *, tree);
179 static void dump_vtable (tree, tree, tree);
180 static void dump_vtt (tree, tree);
181 static void dump_thunk (FILE *, int, tree);
182 static tree build_vtable (tree, tree, tree);
183 static void initialize_vtable (tree, vec<constructor_elt, va_gc> *);
184 static void layout_nonempty_base_or_field (record_layout_info,
185 tree, tree, splay_tree);
186 static tree end_of_class (tree, int);
187 static bool layout_empty_base (record_layout_info, tree, tree, splay_tree);
188 static void accumulate_vtbl_inits (tree, tree, tree, tree, tree,
189 vec<constructor_elt, va_gc> **);
190 static void dfs_accumulate_vtbl_inits (tree, tree, tree, tree, tree,
191 vec<constructor_elt, va_gc> **);
192 static void build_rtti_vtbl_entries (tree, vtbl_init_data *);
193 static void build_vcall_and_vbase_vtbl_entries (tree, vtbl_init_data *);
194 static void clone_constructors_and_destructors (tree);
195 static tree build_clone (tree, tree);
196 static void update_vtable_entry_for_fn (tree, tree, tree, tree *, unsigned);
197 static void build_ctor_vtbl_group (tree, tree);
198 static void build_vtt (tree);
199 static tree binfo_ctor_vtable (tree);
200 static void build_vtt_inits (tree, tree, vec<constructor_elt, va_gc> **,
201 tree *);
202 static tree dfs_build_secondary_vptr_vtt_inits (tree, void *);
203 static tree dfs_fixup_binfo_vtbls (tree, void *);
204 static int record_subobject_offset (tree, tree, splay_tree);
205 static int check_subobject_offset (tree, tree, splay_tree);
206 static int walk_subobject_offsets (tree, subobject_offset_fn,
207 tree, splay_tree, tree, int);
208 static void record_subobject_offsets (tree, tree, splay_tree, bool);
209 static int layout_conflict_p (tree, tree, splay_tree, int);
210 static int splay_tree_compare_integer_csts (splay_tree_key k1,
211 splay_tree_key k2);
212 static void warn_about_ambiguous_bases (tree);
213 static bool type_requires_array_cookie (tree);
214 static bool base_derived_from (tree, tree);
215 static int empty_base_at_nonzero_offset_p (tree, tree, splay_tree);
216 static tree end_of_base (tree);
217 static tree get_vcall_index (tree, tree);
218 static bool type_maybe_constexpr_default_constructor (tree);
220 /* Variables shared between class.c and call.c. */
222 int n_vtables = 0;
223 int n_vtable_entries = 0;
224 int n_vtable_searches = 0;
225 int n_vtable_elems = 0;
226 int n_convert_harshness = 0;
227 int n_compute_conversion_costs = 0;
228 int n_inner_fields_searched = 0;
230 /* Return a COND_EXPR that executes TRUE_STMT if this execution of the
231 'structor is in charge of 'structing virtual bases, or FALSE_STMT
232 otherwise. */
234 tree
235 build_if_in_charge (tree true_stmt, tree false_stmt)
237 gcc_assert (DECL_HAS_IN_CHARGE_PARM_P (current_function_decl));
238 tree cmp = build2 (NE_EXPR, boolean_type_node,
239 current_in_charge_parm, integer_zero_node);
240 tree type = unlowered_expr_type (true_stmt);
241 if (VOID_TYPE_P (type))
242 type = unlowered_expr_type (false_stmt);
243 tree cond = build3 (COND_EXPR, type,
244 cmp, true_stmt, false_stmt);
245 return cond;
248 /* Convert to or from a base subobject. EXPR is an expression of type
249 `A' or `A*', an expression of type `B' or `B*' is returned. To
250 convert A to a base B, CODE is PLUS_EXPR and BINFO is the binfo for
251 the B base instance within A. To convert base A to derived B, CODE
252 is MINUS_EXPR and BINFO is the binfo for the A instance within B.
253 In this latter case, A must not be a morally virtual base of B.
254 NONNULL is true if EXPR is known to be non-NULL (this is only
255 needed when EXPR is of pointer type). CV qualifiers are preserved
256 from EXPR. */
258 tree
259 build_base_path (enum tree_code code,
260 tree expr,
261 tree binfo,
262 int nonnull,
263 tsubst_flags_t complain)
265 tree v_binfo = NULL_TREE;
266 tree d_binfo = NULL_TREE;
267 tree probe;
268 tree offset;
269 tree target_type;
270 tree null_test = NULL;
271 tree ptr_target_type;
272 int fixed_type_p;
273 int want_pointer = TYPE_PTR_P (TREE_TYPE (expr));
274 bool has_empty = false;
275 bool virtual_access;
276 bool rvalue = false;
278 if (expr == error_mark_node || binfo == error_mark_node || !binfo)
279 return error_mark_node;
281 for (probe = binfo; probe; probe = BINFO_INHERITANCE_CHAIN (probe))
283 d_binfo = probe;
284 if (is_empty_class (BINFO_TYPE (probe)))
285 has_empty = true;
286 if (!v_binfo && BINFO_VIRTUAL_P (probe))
287 v_binfo = probe;
290 probe = TYPE_MAIN_VARIANT (TREE_TYPE (expr));
291 if (want_pointer)
292 probe = TYPE_MAIN_VARIANT (TREE_TYPE (probe));
294 if (code == PLUS_EXPR
295 && !SAME_BINFO_TYPE_P (BINFO_TYPE (d_binfo), probe))
297 /* This can happen when adjust_result_of_qualified_name_lookup can't
298 find a unique base binfo in a call to a member function. We
299 couldn't give the diagnostic then since we might have been calling
300 a static member function, so we do it now. In other cases, eg.
301 during error recovery (c++/71979), we may not have a base at all. */
302 if (complain & tf_error)
304 tree base = lookup_base (probe, BINFO_TYPE (d_binfo),
305 ba_unique, NULL, complain);
306 gcc_assert (base == error_mark_node || !base);
308 return error_mark_node;
311 gcc_assert ((code == MINUS_EXPR
312 && SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), probe))
313 || code == PLUS_EXPR);
315 if (binfo == d_binfo)
316 /* Nothing to do. */
317 return expr;
319 if (code == MINUS_EXPR && v_binfo)
321 if (complain & tf_error)
323 if (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), BINFO_TYPE (v_binfo)))
325 if (want_pointer)
326 error ("cannot convert from pointer to base class %qT to "
327 "pointer to derived class %qT because the base is "
328 "virtual", BINFO_TYPE (binfo), BINFO_TYPE (d_binfo));
329 else
330 error ("cannot convert from base class %qT to derived "
331 "class %qT because the base is virtual",
332 BINFO_TYPE (binfo), BINFO_TYPE (d_binfo));
334 else
336 if (want_pointer)
337 error ("cannot convert from pointer to base class %qT to "
338 "pointer to derived class %qT via virtual base %qT",
339 BINFO_TYPE (binfo), BINFO_TYPE (d_binfo),
340 BINFO_TYPE (v_binfo));
341 else
342 error ("cannot convert from base class %qT to derived "
343 "class %qT via virtual base %qT", BINFO_TYPE (binfo),
344 BINFO_TYPE (d_binfo), BINFO_TYPE (v_binfo));
347 return error_mark_node;
350 if (!want_pointer)
352 rvalue = !lvalue_p (expr);
353 /* This must happen before the call to save_expr. */
354 expr = cp_build_addr_expr (expr, complain);
356 else
357 expr = mark_rvalue_use (expr);
359 offset = BINFO_OFFSET (binfo);
360 fixed_type_p = resolves_to_fixed_type_p (expr, &nonnull);
361 target_type = code == PLUS_EXPR ? BINFO_TYPE (binfo) : BINFO_TYPE (d_binfo);
362 /* TARGET_TYPE has been extracted from BINFO, and, is therefore always
363 cv-unqualified. Extract the cv-qualifiers from EXPR so that the
364 expression returned matches the input. */
365 target_type = cp_build_qualified_type
366 (target_type, cp_type_quals (TREE_TYPE (TREE_TYPE (expr))));
367 ptr_target_type = build_pointer_type (target_type);
369 /* Do we need to look in the vtable for the real offset? */
370 virtual_access = (v_binfo && fixed_type_p <= 0);
372 /* Don't bother with the calculations inside sizeof; they'll ICE if the
373 source type is incomplete and the pointer value doesn't matter. In a
374 template (even in instantiate_non_dependent_expr), we don't have vtables
375 set up properly yet, and the value doesn't matter there either; we're
376 just interested in the result of overload resolution. */
377 if (cp_unevaluated_operand != 0
378 || in_template_function ())
380 expr = build_nop (ptr_target_type, expr);
381 goto indout;
384 /* If we're in an NSDMI, we don't have the full constructor context yet
385 that we need for converting to a virtual base, so just build a stub
386 CONVERT_EXPR and expand it later in bot_replace. */
387 if (virtual_access && fixed_type_p < 0
388 && current_scope () != current_function_decl)
390 expr = build1 (CONVERT_EXPR, ptr_target_type, expr);
391 CONVERT_EXPR_VBASE_PATH (expr) = true;
392 goto indout;
395 /* Do we need to check for a null pointer? */
396 if (want_pointer && !nonnull)
398 /* If we know the conversion will not actually change the value
399 of EXPR, then we can avoid testing the expression for NULL.
400 We have to avoid generating a COMPONENT_REF for a base class
401 field, because other parts of the compiler know that such
402 expressions are always non-NULL. */
403 if (!virtual_access && integer_zerop (offset))
404 return build_nop (ptr_target_type, expr);
405 null_test = error_mark_node;
408 /* Protect against multiple evaluation if necessary. */
409 if (TREE_SIDE_EFFECTS (expr) && (null_test || virtual_access))
410 expr = save_expr (expr);
412 /* Now that we've saved expr, build the real null test. */
413 if (null_test)
415 tree zero = cp_convert (TREE_TYPE (expr), nullptr_node, complain);
416 null_test = build2_loc (input_location, NE_EXPR, boolean_type_node,
417 expr, zero);
418 /* This is a compiler generated comparison, don't emit
419 e.g. -Wnonnull-compare warning for it. */
420 TREE_NO_WARNING (null_test) = 1;
423 /* If this is a simple base reference, express it as a COMPONENT_REF. */
424 if (code == PLUS_EXPR && !virtual_access
425 /* We don't build base fields for empty bases, and they aren't very
426 interesting to the optimizers anyway. */
427 && !has_empty)
429 expr = cp_build_indirect_ref (expr, RO_NULL, complain);
430 expr = build_simple_base_path (expr, binfo);
431 if (rvalue)
432 expr = move (expr);
433 if (want_pointer)
434 expr = build_address (expr);
435 target_type = TREE_TYPE (expr);
436 goto out;
439 if (virtual_access)
441 /* Going via virtual base V_BINFO. We need the static offset
442 from V_BINFO to BINFO, and the dynamic offset from D_BINFO to
443 V_BINFO. That offset is an entry in D_BINFO's vtable. */
444 tree v_offset;
446 if (fixed_type_p < 0 && in_base_initializer)
448 /* In a base member initializer, we cannot rely on the
449 vtable being set up. We have to indirect via the
450 vtt_parm. */
451 tree t;
453 t = TREE_TYPE (TYPE_VFIELD (current_class_type));
454 t = build_pointer_type (t);
455 v_offset = fold_convert (t, current_vtt_parm);
456 v_offset = cp_build_indirect_ref (v_offset, RO_NULL, complain);
458 else
460 tree t = expr;
461 if ((flag_sanitize & SANITIZE_VPTR) && fixed_type_p == 0)
463 t = cp_ubsan_maybe_instrument_cast_to_vbase (input_location,
464 probe, expr);
465 if (t == NULL_TREE)
466 t = expr;
468 v_offset = build_vfield_ref (cp_build_indirect_ref (t, RO_NULL,
469 complain),
470 TREE_TYPE (TREE_TYPE (expr)));
473 if (v_offset == error_mark_node)
474 return error_mark_node;
476 v_offset = fold_build_pointer_plus (v_offset, BINFO_VPTR_FIELD (v_binfo));
477 v_offset = build1 (NOP_EXPR,
478 build_pointer_type (ptrdiff_type_node),
479 v_offset);
480 v_offset = cp_build_indirect_ref (v_offset, RO_NULL, complain);
481 TREE_CONSTANT (v_offset) = 1;
483 offset = convert_to_integer (ptrdiff_type_node,
484 size_diffop_loc (input_location, offset,
485 BINFO_OFFSET (v_binfo)));
487 if (!integer_zerop (offset))
488 v_offset = build2 (code, ptrdiff_type_node, v_offset, offset);
490 if (fixed_type_p < 0)
491 /* Negative fixed_type_p means this is a constructor or destructor;
492 virtual base layout is fixed in in-charge [cd]tors, but not in
493 base [cd]tors. */
494 offset = build_if_in_charge
495 (convert_to_integer (ptrdiff_type_node, BINFO_OFFSET (binfo)),
496 v_offset);
497 else
498 offset = v_offset;
501 if (want_pointer)
502 target_type = ptr_target_type;
504 expr = build1 (NOP_EXPR, ptr_target_type, expr);
506 if (!integer_zerop (offset))
508 offset = fold_convert (sizetype, offset);
509 if (code == MINUS_EXPR)
510 offset = fold_build1_loc (input_location, NEGATE_EXPR, sizetype, offset);
511 expr = fold_build_pointer_plus (expr, offset);
513 else
514 null_test = NULL;
516 indout:
517 if (!want_pointer)
519 expr = cp_build_indirect_ref (expr, RO_NULL, complain);
520 if (rvalue)
521 expr = move (expr);
524 out:
525 if (null_test)
526 expr = fold_build3_loc (input_location, COND_EXPR, target_type, null_test, expr,
527 build_zero_cst (target_type));
529 return expr;
532 /* Subroutine of build_base_path; EXPR and BINFO are as in that function.
533 Perform a derived-to-base conversion by recursively building up a
534 sequence of COMPONENT_REFs to the appropriate base fields. */
536 static tree
537 build_simple_base_path (tree expr, tree binfo)
539 tree type = BINFO_TYPE (binfo);
540 tree d_binfo = BINFO_INHERITANCE_CHAIN (binfo);
541 tree field;
543 if (d_binfo == NULL_TREE)
545 tree temp;
547 gcc_assert (TYPE_MAIN_VARIANT (TREE_TYPE (expr)) == type);
549 /* Transform `(a, b).x' into `(*(a, &b)).x', `(a ? b : c).x'
550 into `(*(a ? &b : &c)).x', and so on. A COND_EXPR is only
551 an lvalue in the front end; only _DECLs and _REFs are lvalues
552 in the back end. */
553 temp = unary_complex_lvalue (ADDR_EXPR, expr);
554 if (temp)
555 expr = cp_build_indirect_ref (temp, RO_NULL, tf_warning_or_error);
557 return expr;
560 /* Recurse. */
561 expr = build_simple_base_path (expr, d_binfo);
563 for (field = TYPE_FIELDS (BINFO_TYPE (d_binfo));
564 field; field = DECL_CHAIN (field))
565 /* Is this the base field created by build_base_field? */
566 if (TREE_CODE (field) == FIELD_DECL
567 && DECL_FIELD_IS_BASE (field)
568 && TREE_TYPE (field) == type
569 /* If we're looking for a field in the most-derived class,
570 also check the field offset; we can have two base fields
571 of the same type if one is an indirect virtual base and one
572 is a direct non-virtual base. */
573 && (BINFO_INHERITANCE_CHAIN (d_binfo)
574 || tree_int_cst_equal (byte_position (field),
575 BINFO_OFFSET (binfo))))
577 /* We don't use build_class_member_access_expr here, as that
578 has unnecessary checks, and more importantly results in
579 recursive calls to dfs_walk_once. */
580 int type_quals = cp_type_quals (TREE_TYPE (expr));
582 expr = build3 (COMPONENT_REF,
583 cp_build_qualified_type (type, type_quals),
584 expr, field, NULL_TREE);
585 /* Mark the expression const or volatile, as appropriate.
586 Even though we've dealt with the type above, we still have
587 to mark the expression itself. */
588 if (type_quals & TYPE_QUAL_CONST)
589 TREE_READONLY (expr) = 1;
590 if (type_quals & TYPE_QUAL_VOLATILE)
591 TREE_THIS_VOLATILE (expr) = 1;
593 return expr;
596 /* Didn't find the base field?!? */
597 gcc_unreachable ();
600 /* Convert OBJECT to the base TYPE. OBJECT is an expression whose
601 type is a class type or a pointer to a class type. In the former
602 case, TYPE is also a class type; in the latter it is another
603 pointer type. If CHECK_ACCESS is true, an error message is emitted
604 if TYPE is inaccessible. If OBJECT has pointer type, the value is
605 assumed to be non-NULL. */
607 tree
608 convert_to_base (tree object, tree type, bool check_access, bool nonnull,
609 tsubst_flags_t complain)
611 tree binfo;
612 tree object_type;
614 if (TYPE_PTR_P (TREE_TYPE (object)))
616 object_type = TREE_TYPE (TREE_TYPE (object));
617 type = TREE_TYPE (type);
619 else
620 object_type = TREE_TYPE (object);
622 binfo = lookup_base (object_type, type, check_access ? ba_check : ba_unique,
623 NULL, complain);
624 if (!binfo || binfo == error_mark_node)
625 return error_mark_node;
627 return build_base_path (PLUS_EXPR, object, binfo, nonnull, complain);
630 /* EXPR is an expression with unqualified class type. BASE is a base
631 binfo of that class type. Returns EXPR, converted to the BASE
632 type. This function assumes that EXPR is the most derived class;
633 therefore virtual bases can be found at their static offsets. */
635 tree
636 convert_to_base_statically (tree expr, tree base)
638 tree expr_type;
640 expr_type = TREE_TYPE (expr);
641 if (!SAME_BINFO_TYPE_P (BINFO_TYPE (base), expr_type))
643 /* If this is a non-empty base, use a COMPONENT_REF. */
644 if (!is_empty_class (BINFO_TYPE (base)))
645 return build_simple_base_path (expr, base);
647 /* We use fold_build2 and fold_convert below to simplify the trees
648 provided to the optimizers. It is not safe to call these functions
649 when processing a template because they do not handle C++-specific
650 trees. */
651 gcc_assert (!processing_template_decl);
652 expr = cp_build_addr_expr (expr, tf_warning_or_error);
653 if (!integer_zerop (BINFO_OFFSET (base)))
654 expr = fold_build_pointer_plus_loc (input_location,
655 expr, BINFO_OFFSET (base));
656 expr = fold_convert (build_pointer_type (BINFO_TYPE (base)), expr);
657 expr = build_fold_indirect_ref_loc (input_location, expr);
660 return expr;
664 tree
665 build_vfield_ref (tree datum, tree type)
667 tree vfield, vcontext;
669 if (datum == error_mark_node
670 /* Can happen in case of duplicate base types (c++/59082). */
671 || !TYPE_VFIELD (type))
672 return error_mark_node;
674 /* First, convert to the requested type. */
675 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (datum), type))
676 datum = convert_to_base (datum, type, /*check_access=*/false,
677 /*nonnull=*/true, tf_warning_or_error);
679 /* Second, the requested type may not be the owner of its own vptr.
680 If not, convert to the base class that owns it. We cannot use
681 convert_to_base here, because VCONTEXT may appear more than once
682 in the inheritance hierarchy of TYPE, and thus direct conversion
683 between the types may be ambiguous. Following the path back up
684 one step at a time via primary bases avoids the problem. */
685 vfield = TYPE_VFIELD (type);
686 vcontext = DECL_CONTEXT (vfield);
687 while (!same_type_ignoring_top_level_qualifiers_p (vcontext, type))
689 datum = build_simple_base_path (datum, CLASSTYPE_PRIMARY_BINFO (type));
690 type = TREE_TYPE (datum);
693 return build3 (COMPONENT_REF, TREE_TYPE (vfield), datum, vfield, NULL_TREE);
696 /* Given an object INSTANCE, return an expression which yields the
697 vtable element corresponding to INDEX. There are many special
698 cases for INSTANCE which we take care of here, mainly to avoid
699 creating extra tree nodes when we don't have to. */
701 static tree
702 build_vtbl_ref_1 (tree instance, tree idx)
704 tree aref;
705 tree vtbl = NULL_TREE;
707 /* Try to figure out what a reference refers to, and
708 access its virtual function table directly. */
710 int cdtorp = 0;
711 tree fixed_type = fixed_type_or_null (instance, NULL, &cdtorp);
713 tree basetype = non_reference (TREE_TYPE (instance));
715 if (fixed_type && !cdtorp)
717 tree binfo = lookup_base (fixed_type, basetype,
718 ba_unique, NULL, tf_none);
719 if (binfo && binfo != error_mark_node)
720 vtbl = unshare_expr (BINFO_VTABLE (binfo));
723 if (!vtbl)
724 vtbl = build_vfield_ref (instance, basetype);
726 aref = build_array_ref (input_location, vtbl, idx);
727 TREE_CONSTANT (aref) |= TREE_CONSTANT (vtbl) && TREE_CONSTANT (idx);
729 return aref;
732 tree
733 build_vtbl_ref (tree instance, tree idx)
735 tree aref = build_vtbl_ref_1 (instance, idx);
737 return aref;
740 /* Given a stable object pointer INSTANCE_PTR, return an expression which
741 yields a function pointer corresponding to vtable element INDEX. */
743 tree
744 build_vfn_ref (tree instance_ptr, tree idx)
746 tree aref;
748 aref = build_vtbl_ref_1 (cp_build_indirect_ref (instance_ptr, RO_NULL,
749 tf_warning_or_error),
750 idx);
752 /* When using function descriptors, the address of the
753 vtable entry is treated as a function pointer. */
754 if (TARGET_VTABLE_USES_DESCRIPTORS)
755 aref = build1 (NOP_EXPR, TREE_TYPE (aref),
756 cp_build_addr_expr (aref, tf_warning_or_error));
758 /* Remember this as a method reference, for later devirtualization. */
759 aref = build3 (OBJ_TYPE_REF, TREE_TYPE (aref), aref, instance_ptr, idx);
761 return aref;
764 /* Return the name of the virtual function table (as an IDENTIFIER_NODE)
765 for the given TYPE. */
767 static tree
768 get_vtable_name (tree type)
770 return mangle_vtbl_for_type (type);
773 /* DECL is an entity associated with TYPE, like a virtual table or an
774 implicitly generated constructor. Determine whether or not DECL
775 should have external or internal linkage at the object file
776 level. This routine does not deal with COMDAT linkage and other
777 similar complexities; it simply sets TREE_PUBLIC if it possible for
778 entities in other translation units to contain copies of DECL, in
779 the abstract. */
781 void
782 set_linkage_according_to_type (tree /*type*/, tree decl)
784 TREE_PUBLIC (decl) = 1;
785 determine_visibility (decl);
788 /* Create a VAR_DECL for a primary or secondary vtable for CLASS_TYPE.
789 (For a secondary vtable for B-in-D, CLASS_TYPE should be D, not B.)
790 Use NAME for the name of the vtable, and VTABLE_TYPE for its type. */
792 static tree
793 build_vtable (tree class_type, tree name, tree vtable_type)
795 tree decl;
797 decl = build_lang_decl (VAR_DECL, name, vtable_type);
798 /* vtable names are already mangled; give them their DECL_ASSEMBLER_NAME
799 now to avoid confusion in mangle_decl. */
800 SET_DECL_ASSEMBLER_NAME (decl, name);
801 DECL_CONTEXT (decl) = class_type;
802 DECL_ARTIFICIAL (decl) = 1;
803 TREE_STATIC (decl) = 1;
804 TREE_READONLY (decl) = 1;
805 DECL_VIRTUAL_P (decl) = 1;
806 SET_DECL_ALIGN (decl, TARGET_VTABLE_ENTRY_ALIGN);
807 DECL_USER_ALIGN (decl) = true;
808 DECL_VTABLE_OR_VTT_P (decl) = 1;
809 set_linkage_according_to_type (class_type, decl);
810 /* The vtable has not been defined -- yet. */
811 DECL_EXTERNAL (decl) = 1;
812 DECL_NOT_REALLY_EXTERN (decl) = 1;
814 /* Mark the VAR_DECL node representing the vtable itself as a
815 "gratuitous" one, thereby forcing dwarfout.c to ignore it. It
816 is rather important that such things be ignored because any
817 effort to actually generate DWARF for them will run into
818 trouble when/if we encounter code like:
820 #pragma interface
821 struct S { virtual void member (); };
823 because the artificial declaration of the vtable itself (as
824 manufactured by the g++ front end) will say that the vtable is
825 a static member of `S' but only *after* the debug output for
826 the definition of `S' has already been output. This causes
827 grief because the DWARF entry for the definition of the vtable
828 will try to refer back to an earlier *declaration* of the
829 vtable as a static member of `S' and there won't be one. We
830 might be able to arrange to have the "vtable static member"
831 attached to the member list for `S' before the debug info for
832 `S' get written (which would solve the problem) but that would
833 require more intrusive changes to the g++ front end. */
834 DECL_IGNORED_P (decl) = 1;
836 return decl;
839 /* Get the VAR_DECL of the vtable for TYPE. TYPE need not be polymorphic,
840 or even complete. If this does not exist, create it. If COMPLETE is
841 nonzero, then complete the definition of it -- that will render it
842 impossible to actually build the vtable, but is useful to get at those
843 which are known to exist in the runtime. */
845 tree
846 get_vtable_decl (tree type, int complete)
848 tree decl;
850 if (CLASSTYPE_VTABLES (type))
851 return CLASSTYPE_VTABLES (type);
853 decl = build_vtable (type, get_vtable_name (type), vtbl_type_node);
854 CLASSTYPE_VTABLES (type) = decl;
856 if (complete)
858 DECL_EXTERNAL (decl) = 1;
859 cp_finish_decl (decl, NULL_TREE, false, NULL_TREE, 0);
862 return decl;
865 /* Build the primary virtual function table for TYPE. If BINFO is
866 non-NULL, build the vtable starting with the initial approximation
867 that it is the same as the one which is the head of the association
868 list. Returns a nonzero value if a new vtable is actually
869 created. */
871 static int
872 build_primary_vtable (tree binfo, tree type)
874 tree decl;
875 tree virtuals;
877 decl = get_vtable_decl (type, /*complete=*/0);
879 if (binfo)
881 if (BINFO_NEW_VTABLE_MARKED (binfo))
882 /* We have already created a vtable for this base, so there's
883 no need to do it again. */
884 return 0;
886 virtuals = copy_list (BINFO_VIRTUALS (binfo));
887 TREE_TYPE (decl) = TREE_TYPE (get_vtbl_decl_for_binfo (binfo));
888 DECL_SIZE (decl) = TYPE_SIZE (TREE_TYPE (decl));
889 DECL_SIZE_UNIT (decl) = TYPE_SIZE_UNIT (TREE_TYPE (decl));
891 else
893 gcc_assert (TREE_TYPE (decl) == vtbl_type_node);
894 virtuals = NULL_TREE;
897 if (GATHER_STATISTICS)
899 n_vtables += 1;
900 n_vtable_elems += list_length (virtuals);
903 /* Initialize the association list for this type, based
904 on our first approximation. */
905 BINFO_VTABLE (TYPE_BINFO (type)) = decl;
906 BINFO_VIRTUALS (TYPE_BINFO (type)) = virtuals;
907 SET_BINFO_NEW_VTABLE_MARKED (TYPE_BINFO (type));
908 return 1;
911 /* Give BINFO a new virtual function table which is initialized
912 with a skeleton-copy of its original initialization. The only
913 entry that changes is the `delta' entry, so we can really
914 share a lot of structure.
916 FOR_TYPE is the most derived type which caused this table to
917 be needed.
919 Returns nonzero if we haven't met BINFO before.
921 The order in which vtables are built (by calling this function) for
922 an object must remain the same, otherwise a binary incompatibility
923 can result. */
925 static int
926 build_secondary_vtable (tree binfo)
928 if (BINFO_NEW_VTABLE_MARKED (binfo))
929 /* We already created a vtable for this base. There's no need to
930 do it again. */
931 return 0;
933 /* Remember that we've created a vtable for this BINFO, so that we
934 don't try to do so again. */
935 SET_BINFO_NEW_VTABLE_MARKED (binfo);
937 /* Make fresh virtual list, so we can smash it later. */
938 BINFO_VIRTUALS (binfo) = copy_list (BINFO_VIRTUALS (binfo));
940 /* Secondary vtables are laid out as part of the same structure as
941 the primary vtable. */
942 BINFO_VTABLE (binfo) = NULL_TREE;
943 return 1;
946 /* Create a new vtable for BINFO which is the hierarchy dominated by
947 T. Return nonzero if we actually created a new vtable. */
949 static int
950 make_new_vtable (tree t, tree binfo)
952 if (binfo == TYPE_BINFO (t))
953 /* In this case, it is *type*'s vtable we are modifying. We start
954 with the approximation that its vtable is that of the
955 immediate base class. */
956 return build_primary_vtable (binfo, t);
957 else
958 /* This is our very own copy of `basetype' to play with. Later,
959 we will fill in all the virtual functions that override the
960 virtual functions in these base classes which are not defined
961 by the current type. */
962 return build_secondary_vtable (binfo);
965 /* Make *VIRTUALS, an entry on the BINFO_VIRTUALS list for BINFO
966 (which is in the hierarchy dominated by T) list FNDECL as its
967 BV_FN. DELTA is the required constant adjustment from the `this'
968 pointer where the vtable entry appears to the `this' required when
969 the function is actually called. */
971 static void
972 modify_vtable_entry (tree t,
973 tree binfo,
974 tree fndecl,
975 tree delta,
976 tree *virtuals)
978 tree v;
980 v = *virtuals;
982 if (fndecl != BV_FN (v)
983 || !tree_int_cst_equal (delta, BV_DELTA (v)))
985 /* We need a new vtable for BINFO. */
986 if (make_new_vtable (t, binfo))
988 /* If we really did make a new vtable, we also made a copy
989 of the BINFO_VIRTUALS list. Now, we have to find the
990 corresponding entry in that list. */
991 *virtuals = BINFO_VIRTUALS (binfo);
992 while (BV_FN (*virtuals) != BV_FN (v))
993 *virtuals = TREE_CHAIN (*virtuals);
994 v = *virtuals;
997 BV_DELTA (v) = delta;
998 BV_VCALL_INDEX (v) = NULL_TREE;
999 BV_FN (v) = fndecl;
1004 /* Add method METHOD to class TYPE. If USING_DECL is non-null, it is
1005 the USING_DECL naming METHOD. Returns true if the method could be
1006 added to the method vec. */
1008 bool
1009 add_method (tree type, tree method, tree using_decl)
1011 unsigned slot;
1012 tree overload;
1013 bool template_conv_p = false;
1014 bool conv_p;
1015 vec<tree, va_gc> *method_vec;
1016 bool complete_p;
1017 bool insert_p = false;
1018 tree current_fns;
1020 if (method == error_mark_node)
1021 return false;
1023 complete_p = COMPLETE_TYPE_P (type);
1024 conv_p = DECL_CONV_FN_P (method);
1025 if (conv_p)
1026 template_conv_p = (TREE_CODE (method) == TEMPLATE_DECL
1027 && DECL_TEMPLATE_CONV_FN_P (method));
1029 method_vec = CLASSTYPE_METHOD_VEC (type);
1030 if (!method_vec)
1032 /* Make a new method vector. We start with 8 entries. We must
1033 allocate at least two (for constructors and destructors), and
1034 we're going to end up with an assignment operator at some
1035 point as well. */
1036 vec_alloc (method_vec, 8);
1037 /* Create slots for constructors and destructors. */
1038 method_vec->quick_push (NULL_TREE);
1039 method_vec->quick_push (NULL_TREE);
1040 CLASSTYPE_METHOD_VEC (type) = method_vec;
1043 /* Maintain TYPE_HAS_USER_CONSTRUCTOR, etc. */
1044 grok_special_member_properties (method);
1046 /* Constructors and destructors go in special slots. */
1047 if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (method))
1048 slot = CLASSTYPE_CONSTRUCTOR_SLOT;
1049 else if (DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (method))
1050 slot = CLASSTYPE_DESTRUCTOR_SLOT;
1051 else
1053 tree m;
1055 insert_p = true;
1056 /* See if we already have an entry with this name. */
1057 for (slot = CLASSTYPE_FIRST_CONVERSION_SLOT;
1058 vec_safe_iterate (method_vec, slot, &m);
1059 ++slot)
1061 m = OVL_CURRENT (m);
1062 if (template_conv_p)
1064 if (TREE_CODE (m) == TEMPLATE_DECL
1065 && DECL_TEMPLATE_CONV_FN_P (m))
1066 insert_p = false;
1067 break;
1069 if (conv_p && !DECL_CONV_FN_P (m))
1070 break;
1071 if (DECL_NAME (m) == DECL_NAME (method))
1073 insert_p = false;
1074 break;
1076 if (complete_p
1077 && !DECL_CONV_FN_P (m)
1078 && DECL_NAME (m) > DECL_NAME (method))
1079 break;
1082 current_fns = insert_p ? NULL_TREE : (*method_vec)[slot];
1084 /* Check to see if we've already got this method. */
1085 for (tree *p = &current_fns; *p; )
1087 tree fns = *p;
1088 tree fn = OVL_CURRENT (fns);
1089 tree fn_type;
1090 tree method_type;
1091 tree parms1;
1092 tree parms2;
1094 if (TREE_CODE (fn) != TREE_CODE (method))
1095 goto cont;
1097 /* Two using-declarations can coexist, we'll complain about ambiguity in
1098 overload resolution. */
1099 if (using_decl && TREE_CODE (fns) == OVERLOAD && OVL_USED (fns)
1100 /* Except handle inherited constructors specially. */
1101 && ! DECL_CONSTRUCTOR_P (fn))
1102 goto cont;
1104 /* [over.load] Member function declarations with the
1105 same name and the same parameter types cannot be
1106 overloaded if any of them is a static member
1107 function declaration.
1109 [over.load] Member function declarations with the same name and
1110 the same parameter-type-list as well as member function template
1111 declarations with the same name, the same parameter-type-list, and
1112 the same template parameter lists cannot be overloaded if any of
1113 them, but not all, have a ref-qualifier.
1115 [namespace.udecl] When a using-declaration brings names
1116 from a base class into a derived class scope, member
1117 functions in the derived class override and/or hide member
1118 functions with the same name and parameter types in a base
1119 class (rather than conflicting). */
1120 fn_type = TREE_TYPE (fn);
1121 method_type = TREE_TYPE (method);
1122 parms1 = TYPE_ARG_TYPES (fn_type);
1123 parms2 = TYPE_ARG_TYPES (method_type);
1125 /* Compare the quals on the 'this' parm. Don't compare
1126 the whole types, as used functions are treated as
1127 coming from the using class in overload resolution. */
1128 if (! DECL_STATIC_FUNCTION_P (fn)
1129 && ! DECL_STATIC_FUNCTION_P (method)
1130 /* Either both or neither need to be ref-qualified for
1131 differing quals to allow overloading. */
1132 && (FUNCTION_REF_QUALIFIED (fn_type)
1133 == FUNCTION_REF_QUALIFIED (method_type))
1134 && (type_memfn_quals (fn_type) != type_memfn_quals (method_type)
1135 || type_memfn_rqual (fn_type) != type_memfn_rqual (method_type)))
1136 goto cont;
1138 /* For templates, the return type and template parameters
1139 must be identical. */
1140 if (TREE_CODE (fn) == TEMPLATE_DECL
1141 && (!same_type_p (TREE_TYPE (fn_type),
1142 TREE_TYPE (method_type))
1143 || !comp_template_parms (DECL_TEMPLATE_PARMS (fn),
1144 DECL_TEMPLATE_PARMS (method))))
1145 goto cont;
1147 if (! DECL_STATIC_FUNCTION_P (fn))
1148 parms1 = TREE_CHAIN (parms1);
1149 if (! DECL_STATIC_FUNCTION_P (method))
1150 parms2 = TREE_CHAIN (parms2);
1152 if (compparms (parms1, parms2)
1153 && (!DECL_CONV_FN_P (fn)
1154 || same_type_p (TREE_TYPE (fn_type),
1155 TREE_TYPE (method_type)))
1156 && equivalently_constrained (fn, method))
1158 /* For function versions, their parms and types match
1159 but they are not duplicates. Record function versions
1160 as and when they are found. extern "C" functions are
1161 not treated as versions. */
1162 if (TREE_CODE (fn) == FUNCTION_DECL
1163 && TREE_CODE (method) == FUNCTION_DECL
1164 && !DECL_EXTERN_C_P (fn)
1165 && !DECL_EXTERN_C_P (method)
1166 && targetm.target_option.function_versions (fn, method))
1168 /* Mark functions as versions if necessary. Modify the mangled
1169 decl name if necessary. */
1170 if (!DECL_FUNCTION_VERSIONED (fn))
1172 DECL_FUNCTION_VERSIONED (fn) = 1;
1173 if (DECL_ASSEMBLER_NAME_SET_P (fn))
1174 mangle_decl (fn);
1176 if (!DECL_FUNCTION_VERSIONED (method))
1178 DECL_FUNCTION_VERSIONED (method) = 1;
1179 if (DECL_ASSEMBLER_NAME_SET_P (method))
1180 mangle_decl (method);
1182 cgraph_node::record_function_versions (fn, method);
1183 goto cont;
1185 if (DECL_INHERITED_CTOR (method))
1187 if (DECL_INHERITED_CTOR (fn))
1189 tree basem = DECL_INHERITED_CTOR_BASE (method);
1190 tree basef = DECL_INHERITED_CTOR_BASE (fn);
1191 if (flag_new_inheriting_ctors)
1193 if (basem == basef)
1195 /* Inheriting the same constructor along different
1196 paths, combine them. */
1197 SET_DECL_INHERITED_CTOR
1198 (fn, ovl_cons (DECL_INHERITED_CTOR (method),
1199 DECL_INHERITED_CTOR (fn)));
1200 /* And discard the new one. */
1201 return false;
1203 else
1204 /* Inherited ctors can coexist until overload
1205 resolution. */
1206 goto cont;
1208 error_at (DECL_SOURCE_LOCATION (method),
1209 "%q#D", method);
1210 error_at (DECL_SOURCE_LOCATION (fn),
1211 "conflicts with version inherited from %qT",
1212 basef);
1214 /* Otherwise defer to the other function. */
1215 return false;
1217 if (using_decl)
1219 if (DECL_CONTEXT (fn) == type)
1220 /* Defer to the local function. */
1221 return false;
1223 else if (flag_new_inheriting_ctors
1224 && DECL_INHERITED_CTOR (fn))
1226 /* Hide the inherited constructor. */
1227 *p = OVL_NEXT (fns);
1228 continue;
1230 else
1232 error ("%q+#D cannot be overloaded", method);
1233 error ("with %q+#D", fn);
1236 /* We don't call duplicate_decls here to merge the
1237 declarations because that will confuse things if the
1238 methods have inline definitions. In particular, we
1239 will crash while processing the definitions. */
1240 return false;
1243 cont:
1244 if (TREE_CODE (fns) == OVERLOAD)
1245 p = &OVL_CHAIN (fns);
1246 else
1247 break;
1250 /* A class should never have more than one destructor. */
1251 if (current_fns && DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (method))
1252 return false;
1254 /* Add the new binding. */
1255 if (using_decl)
1257 overload = ovl_cons (method, current_fns);
1258 OVL_USED (overload) = true;
1260 else
1261 overload = build_overload (method, current_fns);
1263 if (conv_p)
1264 TYPE_HAS_CONVERSION (type) = 1;
1265 else if (slot >= CLASSTYPE_FIRST_CONVERSION_SLOT && !complete_p)
1266 push_class_level_binding (DECL_NAME (method), overload);
1268 if (insert_p)
1270 bool reallocated;
1272 /* We only expect to add few methods in the COMPLETE_P case, so
1273 just make room for one more method in that case. */
1274 if (complete_p)
1275 reallocated = vec_safe_reserve_exact (method_vec, 1);
1276 else
1277 reallocated = vec_safe_reserve (method_vec, 1);
1278 if (reallocated)
1279 CLASSTYPE_METHOD_VEC (type) = method_vec;
1280 if (slot == method_vec->length ())
1281 method_vec->quick_push (overload);
1282 else
1283 method_vec->quick_insert (slot, overload);
1285 else
1286 /* Replace the current slot. */
1287 (*method_vec)[slot] = overload;
1288 return true;
1291 /* Subroutines of finish_struct. */
1293 /* Change the access of FDECL to ACCESS in T. Return 1 if change was
1294 legit, otherwise return 0. */
1296 static int
1297 alter_access (tree t, tree fdecl, tree access)
1299 tree elem;
1301 if (!DECL_LANG_SPECIFIC (fdecl))
1302 retrofit_lang_decl (fdecl);
1304 gcc_assert (!DECL_DISCRIMINATOR_P (fdecl));
1306 elem = purpose_member (t, DECL_ACCESS (fdecl));
1307 if (elem)
1309 if (TREE_VALUE (elem) != access)
1311 if (TREE_CODE (TREE_TYPE (fdecl)) == FUNCTION_DECL)
1312 error ("conflicting access specifications for method"
1313 " %q+D, ignored", TREE_TYPE (fdecl));
1314 else
1315 error ("conflicting access specifications for field %qE, ignored",
1316 DECL_NAME (fdecl));
1318 else
1320 /* They're changing the access to the same thing they changed
1321 it to before. That's OK. */
1325 else
1327 perform_or_defer_access_check (TYPE_BINFO (t), fdecl, fdecl,
1328 tf_warning_or_error);
1329 DECL_ACCESS (fdecl) = tree_cons (t, access, DECL_ACCESS (fdecl));
1330 return 1;
1332 return 0;
1335 /* Return the access node for DECL's access in its enclosing class. */
1337 tree
1338 declared_access (tree decl)
1340 return (TREE_PRIVATE (decl) ? access_private_node
1341 : TREE_PROTECTED (decl) ? access_protected_node
1342 : access_public_node);
1345 /* Process the USING_DECL, which is a member of T. */
1347 static void
1348 handle_using_decl (tree using_decl, tree t)
1350 tree decl = USING_DECL_DECLS (using_decl);
1351 tree name = DECL_NAME (using_decl);
1352 tree access = declared_access (using_decl);
1353 tree flist = NULL_TREE;
1354 tree old_value;
1356 gcc_assert (!processing_template_decl && decl);
1358 old_value = lookup_member (t, name, /*protect=*/0, /*want_type=*/false,
1359 tf_warning_or_error);
1360 if (old_value)
1362 if (is_overloaded_fn (old_value))
1363 old_value = OVL_CURRENT (old_value);
1365 if (DECL_P (old_value) && DECL_CONTEXT (old_value) == t)
1366 /* OK */;
1367 else
1368 old_value = NULL_TREE;
1371 cp_emit_debug_info_for_using (decl, t);
1373 if (is_overloaded_fn (decl))
1374 flist = decl;
1376 if (! old_value)
1378 else if (is_overloaded_fn (old_value))
1380 if (flist)
1381 /* It's OK to use functions from a base when there are functions with
1382 the same name already present in the current class. */;
1383 else
1385 error ("%q+D invalid in %q#T", using_decl, t);
1386 error (" because of local method %q+#D with same name",
1387 OVL_CURRENT (old_value));
1388 return;
1391 else if (!DECL_ARTIFICIAL (old_value))
1393 error ("%q+D invalid in %q#T", using_decl, t);
1394 error (" because of local member %q+#D with same name", old_value);
1395 return;
1398 /* Make type T see field decl FDECL with access ACCESS. */
1399 if (flist)
1400 for (; flist; flist = OVL_NEXT (flist))
1402 add_method (t, OVL_CURRENT (flist), using_decl);
1403 alter_access (t, OVL_CURRENT (flist), access);
1405 else
1406 alter_access (t, decl, access);
1409 /* Data structure for find_abi_tags_r, below. */
1411 struct abi_tag_data
1413 tree t; // The type that we're checking for missing tags.
1414 tree subob; // The subobject of T that we're getting tags from.
1415 tree tags; // error_mark_node for diagnostics, or a list of missing tags.
1418 /* Subroutine of find_abi_tags_r. Handle a single TAG found on the class TP
1419 in the context of P. TAG can be either an identifier (the DECL_NAME of
1420 a tag NAMESPACE_DECL) or a STRING_CST (a tag attribute). */
1422 static void
1423 check_tag (tree tag, tree id, tree *tp, abi_tag_data *p)
1425 if (!IDENTIFIER_MARKED (id))
1427 if (p->tags != error_mark_node)
1429 /* We're collecting tags from template arguments or from
1430 the type of a variable or function return type. */
1431 p->tags = tree_cons (NULL_TREE, tag, p->tags);
1433 /* Don't inherit this tag multiple times. */
1434 IDENTIFIER_MARKED (id) = true;
1436 if (TYPE_P (p->t))
1438 /* Tags inherited from type template arguments are only used
1439 to avoid warnings. */
1440 ABI_TAG_IMPLICIT (p->tags) = true;
1441 return;
1443 /* For functions and variables we want to warn, too. */
1446 /* Otherwise we're diagnosing missing tags. */
1447 if (TREE_CODE (p->t) == FUNCTION_DECL)
1449 if (warning (OPT_Wabi_tag, "%qD inherits the %E ABI tag "
1450 "that %qT (used in its return type) has",
1451 p->t, tag, *tp))
1452 inform (location_of (*tp), "%qT declared here", *tp);
1454 else if (VAR_P (p->t))
1456 if (warning (OPT_Wabi_tag, "%qD inherits the %E ABI tag "
1457 "that %qT (used in its type) has", p->t, tag, *tp))
1458 inform (location_of (*tp), "%qT declared here", *tp);
1460 else if (TYPE_P (p->subob))
1462 if (warning (OPT_Wabi_tag, "%qT does not have the %E ABI tag "
1463 "that base %qT has", p->t, tag, p->subob))
1464 inform (location_of (p->subob), "%qT declared here",
1465 p->subob);
1467 else
1469 if (warning (OPT_Wabi_tag, "%qT does not have the %E ABI tag "
1470 "that %qT (used in the type of %qD) has",
1471 p->t, tag, *tp, p->subob))
1473 inform (location_of (p->subob), "%qD declared here",
1474 p->subob);
1475 inform (location_of (*tp), "%qT declared here", *tp);
1481 /* Find all the ABI tags in the attribute list ATTR and either call
1482 check_tag (if TP is non-null) or set IDENTIFIER_MARKED to val. */
1484 static void
1485 mark_or_check_attr_tags (tree attr, tree *tp, abi_tag_data *p, bool val)
1487 if (!attr)
1488 return;
1489 for (; (attr = lookup_attribute ("abi_tag", attr));
1490 attr = TREE_CHAIN (attr))
1491 for (tree list = TREE_VALUE (attr); list;
1492 list = TREE_CHAIN (list))
1494 tree tag = TREE_VALUE (list);
1495 tree id = get_identifier (TREE_STRING_POINTER (tag));
1496 if (tp)
1497 check_tag (tag, id, tp, p);
1498 else
1499 IDENTIFIER_MARKED (id) = val;
1503 /* Find all the ABI tags on T and its enclosing scopes and either call
1504 check_tag (if TP is non-null) or set IDENTIFIER_MARKED to val. */
1506 static void
1507 mark_or_check_tags (tree t, tree *tp, abi_tag_data *p, bool val)
1509 while (t != global_namespace)
1511 tree attr;
1512 if (TYPE_P (t))
1514 attr = TYPE_ATTRIBUTES (t);
1515 t = CP_TYPE_CONTEXT (t);
1517 else
1519 attr = DECL_ATTRIBUTES (t);
1520 t = CP_DECL_CONTEXT (t);
1522 mark_or_check_attr_tags (attr, tp, p, val);
1526 /* walk_tree callback for check_abi_tags: if the type at *TP involves any
1527 types with ABI tags, add the corresponding identifiers to the VEC in
1528 *DATA and set IDENTIFIER_MARKED. */
1530 static tree
1531 find_abi_tags_r (tree *tp, int *walk_subtrees, void *data)
1533 if (!OVERLOAD_TYPE_P (*tp))
1534 return NULL_TREE;
1536 /* walk_tree shouldn't be walking into any subtrees of a RECORD_TYPE
1537 anyway, but let's make sure of it. */
1538 *walk_subtrees = false;
1540 abi_tag_data *p = static_cast<struct abi_tag_data*>(data);
1542 mark_or_check_tags (*tp, tp, p, false);
1544 return NULL_TREE;
1547 /* walk_tree callback for mark_abi_tags: if *TP is a class, set
1548 IDENTIFIER_MARKED on its ABI tags. */
1550 static tree
1551 mark_abi_tags_r (tree *tp, int *walk_subtrees, void *data)
1553 if (!OVERLOAD_TYPE_P (*tp))
1554 return NULL_TREE;
1556 /* walk_tree shouldn't be walking into any subtrees of a RECORD_TYPE
1557 anyway, but let's make sure of it. */
1558 *walk_subtrees = false;
1560 bool *valp = static_cast<bool*>(data);
1562 mark_or_check_tags (*tp, NULL, NULL, *valp);
1564 return NULL_TREE;
1567 /* Set IDENTIFIER_MARKED on all the ABI tags on T and its enclosing
1568 scopes. */
1570 static void
1571 mark_abi_tags (tree t, bool val)
1573 mark_or_check_tags (t, NULL, NULL, val);
1574 if (DECL_P (t))
1576 if (DECL_LANG_SPECIFIC (t) && DECL_USE_TEMPLATE (t)
1577 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (t)))
1579 /* Template arguments are part of the signature. */
1580 tree level = INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (t));
1581 for (int j = 0; j < TREE_VEC_LENGTH (level); ++j)
1583 tree arg = TREE_VEC_ELT (level, j);
1584 cp_walk_tree_without_duplicates (&arg, mark_abi_tags_r, &val);
1587 if (TREE_CODE (t) == FUNCTION_DECL)
1588 /* A function's parameter types are part of the signature, so
1589 we don't need to inherit any tags that are also in them. */
1590 for (tree arg = FUNCTION_FIRST_USER_PARMTYPE (t); arg;
1591 arg = TREE_CHAIN (arg))
1592 cp_walk_tree_without_duplicates (&TREE_VALUE (arg),
1593 mark_abi_tags_r, &val);
1597 /* Check that T has all the ABI tags that subobject SUBOB has, or
1598 warn if not. If T is a (variable or function) declaration, also
1599 return any missing tags, and add them to T if JUST_CHECKING is false. */
1601 static tree
1602 check_abi_tags (tree t, tree subob, bool just_checking = false)
1604 bool inherit = DECL_P (t);
1606 if (!inherit && !warn_abi_tag)
1607 return NULL_TREE;
1609 tree decl = TYPE_P (t) ? TYPE_NAME (t) : t;
1610 if (!TREE_PUBLIC (decl))
1611 /* No need to worry about things local to this TU. */
1612 return NULL_TREE;
1614 mark_abi_tags (t, true);
1616 tree subtype = TYPE_P (subob) ? subob : TREE_TYPE (subob);
1617 struct abi_tag_data data = { t, subob, error_mark_node };
1618 if (inherit)
1619 data.tags = NULL_TREE;
1621 cp_walk_tree_without_duplicates (&subtype, find_abi_tags_r, &data);
1623 if (!(inherit && data.tags))
1624 /* We don't need to do anything with data.tags. */;
1625 else if (just_checking)
1626 for (tree t = data.tags; t; t = TREE_CHAIN (t))
1628 tree id = get_identifier (TREE_STRING_POINTER (TREE_VALUE (t)));
1629 IDENTIFIER_MARKED (id) = false;
1631 else
1633 tree attr = lookup_attribute ("abi_tag", DECL_ATTRIBUTES (t));
1634 if (attr)
1635 TREE_VALUE (attr) = chainon (data.tags, TREE_VALUE (attr));
1636 else
1637 DECL_ATTRIBUTES (t)
1638 = tree_cons (get_identifier ("abi_tag"), data.tags,
1639 DECL_ATTRIBUTES (t));
1642 mark_abi_tags (t, false);
1644 return data.tags;
1647 /* Check that DECL has all the ABI tags that are used in parts of its type
1648 that are not reflected in its mangled name. */
1650 void
1651 check_abi_tags (tree decl)
1653 if (VAR_P (decl))
1654 check_abi_tags (decl, TREE_TYPE (decl));
1655 else if (TREE_CODE (decl) == FUNCTION_DECL
1656 && !DECL_CONV_FN_P (decl)
1657 && !mangle_return_type_p (decl))
1658 check_abi_tags (decl, TREE_TYPE (TREE_TYPE (decl)));
1661 /* Return any ABI tags that are used in parts of the type of DECL
1662 that are not reflected in its mangled name. This function is only
1663 used in backward-compatible mangling for ABI <11. */
1665 tree
1666 missing_abi_tags (tree decl)
1668 if (VAR_P (decl))
1669 return check_abi_tags (decl, TREE_TYPE (decl), true);
1670 else if (TREE_CODE (decl) == FUNCTION_DECL
1671 /* Don't check DECL_CONV_FN_P here like we do in check_abi_tags, so
1672 that we can use this function for setting need_abi_warning
1673 regardless of the current flag_abi_version. */
1674 && !mangle_return_type_p (decl))
1675 return check_abi_tags (decl, TREE_TYPE (TREE_TYPE (decl)), true);
1676 else
1677 return NULL_TREE;
1680 void
1681 inherit_targ_abi_tags (tree t)
1683 if (!CLASS_TYPE_P (t)
1684 || CLASSTYPE_TEMPLATE_INFO (t) == NULL_TREE)
1685 return;
1687 mark_abi_tags (t, true);
1689 tree args = CLASSTYPE_TI_ARGS (t);
1690 struct abi_tag_data data = { t, NULL_TREE, NULL_TREE };
1691 for (int i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
1693 tree level = TMPL_ARGS_LEVEL (args, i+1);
1694 for (int j = 0; j < TREE_VEC_LENGTH (level); ++j)
1696 tree arg = TREE_VEC_ELT (level, j);
1697 data.subob = arg;
1698 cp_walk_tree_without_duplicates (&arg, find_abi_tags_r, &data);
1702 // If we found some tags on our template arguments, add them to our
1703 // abi_tag attribute.
1704 if (data.tags)
1706 tree attr = lookup_attribute ("abi_tag", TYPE_ATTRIBUTES (t));
1707 if (attr)
1708 TREE_VALUE (attr) = chainon (data.tags, TREE_VALUE (attr));
1709 else
1710 TYPE_ATTRIBUTES (t)
1711 = tree_cons (get_identifier ("abi_tag"), data.tags,
1712 TYPE_ATTRIBUTES (t));
1715 mark_abi_tags (t, false);
1718 /* Return true, iff class T has a non-virtual destructor that is
1719 accessible from outside the class heirarchy (i.e. is public, or
1720 there's a suitable friend. */
1722 static bool
1723 accessible_nvdtor_p (tree t)
1725 tree dtor = CLASSTYPE_DESTRUCTORS (t);
1727 /* An implicitly declared destructor is always public. And,
1728 if it were virtual, we would have created it by now. */
1729 if (!dtor)
1730 return true;
1732 if (DECL_VINDEX (dtor))
1733 return false; /* Virtual */
1735 if (!TREE_PRIVATE (dtor) && !TREE_PROTECTED (dtor))
1736 return true; /* Public */
1738 if (CLASSTYPE_FRIEND_CLASSES (t)
1739 || DECL_FRIENDLIST (TYPE_MAIN_DECL (t)))
1740 return true; /* Has friends */
1742 return false;
1745 /* Run through the base classes of T, updating CANT_HAVE_CONST_CTOR_P,
1746 and NO_CONST_ASN_REF_P. Also set flag bits in T based on
1747 properties of the bases. */
1749 static void
1750 check_bases (tree t,
1751 int* cant_have_const_ctor_p,
1752 int* no_const_asn_ref_p)
1754 int i;
1755 bool seen_non_virtual_nearly_empty_base_p = 0;
1756 int seen_tm_mask = 0;
1757 tree base_binfo;
1758 tree binfo;
1759 tree field = NULL_TREE;
1761 if (!CLASSTYPE_NON_STD_LAYOUT (t))
1762 for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
1763 if (TREE_CODE (field) == FIELD_DECL)
1764 break;
1766 for (binfo = TYPE_BINFO (t), i = 0;
1767 BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
1769 tree basetype = TREE_TYPE (base_binfo);
1771 gcc_assert (COMPLETE_TYPE_P (basetype));
1773 if (CLASSTYPE_FINAL (basetype))
1774 error ("cannot derive from %<final%> base %qT in derived type %qT",
1775 basetype, t);
1777 /* If any base class is non-literal, so is the derived class. */
1778 if (!CLASSTYPE_LITERAL_P (basetype))
1779 CLASSTYPE_LITERAL_P (t) = false;
1781 /* If the base class doesn't have copy constructors or
1782 assignment operators that take const references, then the
1783 derived class cannot have such a member automatically
1784 generated. */
1785 if (TYPE_HAS_COPY_CTOR (basetype)
1786 && ! TYPE_HAS_CONST_COPY_CTOR (basetype))
1787 *cant_have_const_ctor_p = 1;
1788 if (TYPE_HAS_COPY_ASSIGN (basetype)
1789 && !TYPE_HAS_CONST_COPY_ASSIGN (basetype))
1790 *no_const_asn_ref_p = 1;
1792 if (BINFO_VIRTUAL_P (base_binfo))
1793 /* A virtual base does not effect nearly emptiness. */
1795 else if (CLASSTYPE_NEARLY_EMPTY_P (basetype))
1797 if (seen_non_virtual_nearly_empty_base_p)
1798 /* And if there is more than one nearly empty base, then the
1799 derived class is not nearly empty either. */
1800 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
1801 else
1802 /* Remember we've seen one. */
1803 seen_non_virtual_nearly_empty_base_p = 1;
1805 else if (!is_empty_class (basetype))
1806 /* If the base class is not empty or nearly empty, then this
1807 class cannot be nearly empty. */
1808 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
1810 /* A lot of properties from the bases also apply to the derived
1811 class. */
1812 TYPE_NEEDS_CONSTRUCTING (t) |= TYPE_NEEDS_CONSTRUCTING (basetype);
1813 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t)
1814 |= TYPE_HAS_NONTRIVIAL_DESTRUCTOR (basetype);
1815 TYPE_HAS_COMPLEX_COPY_ASSIGN (t)
1816 |= (TYPE_HAS_COMPLEX_COPY_ASSIGN (basetype)
1817 || !TYPE_HAS_COPY_ASSIGN (basetype));
1818 TYPE_HAS_COMPLEX_COPY_CTOR (t) |= (TYPE_HAS_COMPLEX_COPY_CTOR (basetype)
1819 || !TYPE_HAS_COPY_CTOR (basetype));
1820 TYPE_HAS_COMPLEX_MOVE_ASSIGN (t)
1821 |= TYPE_HAS_COMPLEX_MOVE_ASSIGN (basetype);
1822 TYPE_HAS_COMPLEX_MOVE_CTOR (t) |= TYPE_HAS_COMPLEX_MOVE_CTOR (basetype);
1823 TYPE_POLYMORPHIC_P (t) |= TYPE_POLYMORPHIC_P (basetype);
1824 CLASSTYPE_CONTAINS_EMPTY_CLASS_P (t)
1825 |= CLASSTYPE_CONTAINS_EMPTY_CLASS_P (basetype);
1826 TYPE_HAS_COMPLEX_DFLT (t) |= (!TYPE_HAS_DEFAULT_CONSTRUCTOR (basetype)
1827 || TYPE_HAS_COMPLEX_DFLT (basetype));
1828 SET_CLASSTYPE_READONLY_FIELDS_NEED_INIT
1829 (t, CLASSTYPE_READONLY_FIELDS_NEED_INIT (t)
1830 | CLASSTYPE_READONLY_FIELDS_NEED_INIT (basetype));
1831 SET_CLASSTYPE_REF_FIELDS_NEED_INIT
1832 (t, CLASSTYPE_REF_FIELDS_NEED_INIT (t)
1833 | CLASSTYPE_REF_FIELDS_NEED_INIT (basetype));
1834 if (TYPE_HAS_MUTABLE_P (basetype))
1835 CLASSTYPE_HAS_MUTABLE (t) = 1;
1837 /* A standard-layout class is a class that:
1839 * has no non-standard-layout base classes, */
1840 CLASSTYPE_NON_STD_LAYOUT (t) |= CLASSTYPE_NON_STD_LAYOUT (basetype);
1841 if (!CLASSTYPE_NON_STD_LAYOUT (t))
1843 tree basefield;
1844 /* ...has no base classes of the same type as the first non-static
1845 data member... */
1846 if (field && DECL_CONTEXT (field) == t
1847 && (same_type_ignoring_top_level_qualifiers_p
1848 (TREE_TYPE (field), basetype)))
1849 CLASSTYPE_NON_STD_LAYOUT (t) = 1;
1850 else
1851 /* ...either has no non-static data members in the most-derived
1852 class and at most one base class with non-static data
1853 members, or has no base classes with non-static data
1854 members */
1855 for (basefield = TYPE_FIELDS (basetype); basefield;
1856 basefield = DECL_CHAIN (basefield))
1857 if (TREE_CODE (basefield) == FIELD_DECL)
1859 if (field)
1860 CLASSTYPE_NON_STD_LAYOUT (t) = 1;
1861 else
1862 field = basefield;
1863 break;
1867 /* Don't bother collecting tm attributes if transactional memory
1868 support is not enabled. */
1869 if (flag_tm)
1871 tree tm_attr = find_tm_attribute (TYPE_ATTRIBUTES (basetype));
1872 if (tm_attr)
1873 seen_tm_mask |= tm_attr_to_mask (tm_attr);
1876 check_abi_tags (t, basetype);
1879 /* If one of the base classes had TM attributes, and the current class
1880 doesn't define its own, then the current class inherits one. */
1881 if (seen_tm_mask && !find_tm_attribute (TYPE_ATTRIBUTES (t)))
1883 tree tm_attr = tm_mask_to_attr (least_bit_hwi (seen_tm_mask));
1884 TYPE_ATTRIBUTES (t) = tree_cons (tm_attr, NULL, TYPE_ATTRIBUTES (t));
1888 /* Determine all the primary bases within T. Sets BINFO_PRIMARY_BASE_P for
1889 those that are primaries. Sets BINFO_LOST_PRIMARY_P for those
1890 that have had a nearly-empty virtual primary base stolen by some
1891 other base in the hierarchy. Determines CLASSTYPE_PRIMARY_BASE for
1892 T. */
1894 static void
1895 determine_primary_bases (tree t)
1897 unsigned i;
1898 tree primary = NULL_TREE;
1899 tree type_binfo = TYPE_BINFO (t);
1900 tree base_binfo;
1902 /* Determine the primary bases of our bases. */
1903 for (base_binfo = TREE_CHAIN (type_binfo); base_binfo;
1904 base_binfo = TREE_CHAIN (base_binfo))
1906 tree primary = CLASSTYPE_PRIMARY_BINFO (BINFO_TYPE (base_binfo));
1908 /* See if we're the non-virtual primary of our inheritance
1909 chain. */
1910 if (!BINFO_VIRTUAL_P (base_binfo))
1912 tree parent = BINFO_INHERITANCE_CHAIN (base_binfo);
1913 tree parent_primary = CLASSTYPE_PRIMARY_BINFO (BINFO_TYPE (parent));
1915 if (parent_primary
1916 && SAME_BINFO_TYPE_P (BINFO_TYPE (base_binfo),
1917 BINFO_TYPE (parent_primary)))
1918 /* We are the primary binfo. */
1919 BINFO_PRIMARY_P (base_binfo) = 1;
1921 /* Determine if we have a virtual primary base, and mark it so.
1923 if (primary && BINFO_VIRTUAL_P (primary))
1925 tree this_primary = copied_binfo (primary, base_binfo);
1927 if (BINFO_PRIMARY_P (this_primary))
1928 /* Someone already claimed this base. */
1929 BINFO_LOST_PRIMARY_P (base_binfo) = 1;
1930 else
1932 tree delta;
1934 BINFO_PRIMARY_P (this_primary) = 1;
1935 BINFO_INHERITANCE_CHAIN (this_primary) = base_binfo;
1937 /* A virtual binfo might have been copied from within
1938 another hierarchy. As we're about to use it as a
1939 primary base, make sure the offsets match. */
1940 delta = size_diffop_loc (input_location,
1941 fold_convert (ssizetype,
1942 BINFO_OFFSET (base_binfo)),
1943 fold_convert (ssizetype,
1944 BINFO_OFFSET (this_primary)));
1946 propagate_binfo_offsets (this_primary, delta);
1951 /* First look for a dynamic direct non-virtual base. */
1952 for (i = 0; BINFO_BASE_ITERATE (type_binfo, i, base_binfo); i++)
1954 tree basetype = BINFO_TYPE (base_binfo);
1956 if (TYPE_CONTAINS_VPTR_P (basetype) && !BINFO_VIRTUAL_P (base_binfo))
1958 primary = base_binfo;
1959 goto found;
1963 /* A "nearly-empty" virtual base class can be the primary base
1964 class, if no non-virtual polymorphic base can be found. Look for
1965 a nearly-empty virtual dynamic base that is not already a primary
1966 base of something in the hierarchy. If there is no such base,
1967 just pick the first nearly-empty virtual base. */
1969 for (base_binfo = TREE_CHAIN (type_binfo); base_binfo;
1970 base_binfo = TREE_CHAIN (base_binfo))
1971 if (BINFO_VIRTUAL_P (base_binfo)
1972 && CLASSTYPE_NEARLY_EMPTY_P (BINFO_TYPE (base_binfo)))
1974 if (!BINFO_PRIMARY_P (base_binfo))
1976 /* Found one that is not primary. */
1977 primary = base_binfo;
1978 goto found;
1980 else if (!primary)
1981 /* Remember the first candidate. */
1982 primary = base_binfo;
1985 found:
1986 /* If we've got a primary base, use it. */
1987 if (primary)
1989 tree basetype = BINFO_TYPE (primary);
1991 CLASSTYPE_PRIMARY_BINFO (t) = primary;
1992 if (BINFO_PRIMARY_P (primary))
1993 /* We are stealing a primary base. */
1994 BINFO_LOST_PRIMARY_P (BINFO_INHERITANCE_CHAIN (primary)) = 1;
1995 BINFO_PRIMARY_P (primary) = 1;
1996 if (BINFO_VIRTUAL_P (primary))
1998 tree delta;
2000 BINFO_INHERITANCE_CHAIN (primary) = type_binfo;
2001 /* A virtual binfo might have been copied from within
2002 another hierarchy. As we're about to use it as a primary
2003 base, make sure the offsets match. */
2004 delta = size_diffop_loc (input_location, ssize_int (0),
2005 fold_convert (ssizetype, BINFO_OFFSET (primary)));
2007 propagate_binfo_offsets (primary, delta);
2010 primary = TYPE_BINFO (basetype);
2012 TYPE_VFIELD (t) = TYPE_VFIELD (basetype);
2013 BINFO_VTABLE (type_binfo) = BINFO_VTABLE (primary);
2014 BINFO_VIRTUALS (type_binfo) = BINFO_VIRTUALS (primary);
2018 /* Update the variant types of T. */
2020 void
2021 fixup_type_variants (tree t)
2023 tree variants;
2025 if (!t)
2026 return;
2028 for (variants = TYPE_NEXT_VARIANT (t);
2029 variants;
2030 variants = TYPE_NEXT_VARIANT (variants))
2032 /* These fields are in the _TYPE part of the node, not in
2033 the TYPE_LANG_SPECIFIC component, so they are not shared. */
2034 TYPE_HAS_USER_CONSTRUCTOR (variants) = TYPE_HAS_USER_CONSTRUCTOR (t);
2035 TYPE_NEEDS_CONSTRUCTING (variants) = TYPE_NEEDS_CONSTRUCTING (t);
2036 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (variants)
2037 = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t);
2039 TYPE_POLYMORPHIC_P (variants) = TYPE_POLYMORPHIC_P (t);
2041 TYPE_BINFO (variants) = TYPE_BINFO (t);
2043 /* Copy whatever these are holding today. */
2044 TYPE_VFIELD (variants) = TYPE_VFIELD (t);
2045 TYPE_FIELDS (variants) = TYPE_FIELDS (t);
2049 /* KLASS is a class that we're applying may_alias to after the body is
2050 parsed. Fixup any POINTER_TO and REFERENCE_TO types. The
2051 canonical type(s) will be implicitly updated. */
2053 static void
2054 fixup_may_alias (tree klass)
2056 tree t;
2058 for (t = TYPE_POINTER_TO (klass); t; t = TYPE_NEXT_PTR_TO (t))
2059 TYPE_REF_CAN_ALIAS_ALL (t) = true;
2060 for (t = TYPE_REFERENCE_TO (klass); t; t = TYPE_NEXT_REF_TO (t))
2061 TYPE_REF_CAN_ALIAS_ALL (t) = true;
2064 /* Early variant fixups: we apply attributes at the beginning of the class
2065 definition, and we need to fix up any variants that have already been
2066 made via elaborated-type-specifier so that check_qualified_type works. */
2068 void
2069 fixup_attribute_variants (tree t)
2071 tree variants;
2073 if (!t)
2074 return;
2076 tree attrs = TYPE_ATTRIBUTES (t);
2077 unsigned align = TYPE_ALIGN (t);
2078 bool user_align = TYPE_USER_ALIGN (t);
2079 bool may_alias = lookup_attribute ("may_alias", attrs);
2081 if (may_alias)
2082 fixup_may_alias (t);
2084 for (variants = TYPE_NEXT_VARIANT (t);
2085 variants;
2086 variants = TYPE_NEXT_VARIANT (variants))
2088 /* These are the two fields that check_qualified_type looks at and
2089 are affected by attributes. */
2090 TYPE_ATTRIBUTES (variants) = attrs;
2091 unsigned valign = align;
2092 if (TYPE_USER_ALIGN (variants))
2093 valign = MAX (valign, TYPE_ALIGN (variants));
2094 else
2095 TYPE_USER_ALIGN (variants) = user_align;
2096 SET_TYPE_ALIGN (variants, valign);
2097 if (may_alias)
2098 fixup_may_alias (variants);
2102 /* Set memoizing fields and bits of T (and its variants) for later
2103 use. */
2105 static void
2106 finish_struct_bits (tree t)
2108 /* Fix up variants (if any). */
2109 fixup_type_variants (t);
2111 if (BINFO_N_BASE_BINFOS (TYPE_BINFO (t)) && TYPE_POLYMORPHIC_P (t))
2112 /* For a class w/o baseclasses, 'finish_struct' has set
2113 CLASSTYPE_PURE_VIRTUALS correctly (by definition).
2114 Similarly for a class whose base classes do not have vtables.
2115 When neither of these is true, we might have removed abstract
2116 virtuals (by providing a definition), added some (by declaring
2117 new ones), or redeclared ones from a base class. We need to
2118 recalculate what's really an abstract virtual at this point (by
2119 looking in the vtables). */
2120 get_pure_virtuals (t);
2122 /* If this type has a copy constructor or a destructor, force its
2123 mode to be BLKmode, and force its TREE_ADDRESSABLE bit to be
2124 nonzero. This will cause it to be passed by invisible reference
2125 and prevent it from being returned in a register. */
2126 if (type_has_nontrivial_copy_init (t)
2127 || TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t))
2129 tree variants;
2130 SET_DECL_MODE (TYPE_MAIN_DECL (t), BLKmode);
2131 for (variants = t; variants; variants = TYPE_NEXT_VARIANT (variants))
2133 SET_TYPE_MODE (variants, BLKmode);
2134 TREE_ADDRESSABLE (variants) = 1;
2139 /* Issue warnings about T having private constructors, but no friends,
2140 and so forth.
2142 HAS_NONPRIVATE_METHOD is nonzero if T has any non-private methods or
2143 static members. HAS_NONPRIVATE_STATIC_FN is nonzero if T has any
2144 non-private static member functions. */
2146 static void
2147 maybe_warn_about_overly_private_class (tree t)
2149 int has_member_fn = 0;
2150 int has_nonprivate_method = 0;
2151 tree fn;
2153 if (!warn_ctor_dtor_privacy
2154 /* If the class has friends, those entities might create and
2155 access instances, so we should not warn. */
2156 || (CLASSTYPE_FRIEND_CLASSES (t)
2157 || DECL_FRIENDLIST (TYPE_MAIN_DECL (t)))
2158 /* We will have warned when the template was declared; there's
2159 no need to warn on every instantiation. */
2160 || CLASSTYPE_TEMPLATE_INSTANTIATION (t))
2161 /* There's no reason to even consider warning about this
2162 class. */
2163 return;
2165 /* We only issue one warning, if more than one applies, because
2166 otherwise, on code like:
2168 class A {
2169 // Oops - forgot `public:'
2170 A();
2171 A(const A&);
2172 ~A();
2175 we warn several times about essentially the same problem. */
2177 /* Check to see if all (non-constructor, non-destructor) member
2178 functions are private. (Since there are no friends or
2179 non-private statics, we can't ever call any of the private member
2180 functions.) */
2181 for (fn = TYPE_METHODS (t); fn; fn = DECL_CHAIN (fn))
2182 /* We're not interested in compiler-generated methods; they don't
2183 provide any way to call private members. */
2184 if (!DECL_ARTIFICIAL (fn))
2186 if (!TREE_PRIVATE (fn))
2188 if (DECL_STATIC_FUNCTION_P (fn))
2189 /* A non-private static member function is just like a
2190 friend; it can create and invoke private member
2191 functions, and be accessed without a class
2192 instance. */
2193 return;
2195 has_nonprivate_method = 1;
2196 /* Keep searching for a static member function. */
2198 else if (!DECL_CONSTRUCTOR_P (fn) && !DECL_DESTRUCTOR_P (fn))
2199 has_member_fn = 1;
2202 if (!has_nonprivate_method && has_member_fn)
2204 /* There are no non-private methods, and there's at least one
2205 private member function that isn't a constructor or
2206 destructor. (If all the private members are
2207 constructors/destructors we want to use the code below that
2208 issues error messages specifically referring to
2209 constructors/destructors.) */
2210 unsigned i;
2211 tree binfo = TYPE_BINFO (t);
2213 for (i = 0; i != BINFO_N_BASE_BINFOS (binfo); i++)
2214 if (BINFO_BASE_ACCESS (binfo, i) != access_private_node)
2216 has_nonprivate_method = 1;
2217 break;
2219 if (!has_nonprivate_method)
2221 warning (OPT_Wctor_dtor_privacy,
2222 "all member functions in class %qT are private", t);
2223 return;
2227 /* Even if some of the member functions are non-private, the class
2228 won't be useful for much if all the constructors or destructors
2229 are private: such an object can never be created or destroyed. */
2230 fn = CLASSTYPE_DESTRUCTORS (t);
2231 if (fn && TREE_PRIVATE (fn))
2233 warning (OPT_Wctor_dtor_privacy,
2234 "%q#T only defines a private destructor and has no friends",
2236 return;
2239 /* Warn about classes that have private constructors and no friends. */
2240 if (TYPE_HAS_USER_CONSTRUCTOR (t)
2241 /* Implicitly generated constructors are always public. */
2242 && (!CLASSTYPE_LAZY_DEFAULT_CTOR (t)
2243 || !CLASSTYPE_LAZY_COPY_CTOR (t)))
2245 int nonprivate_ctor = 0;
2247 /* If a non-template class does not define a copy
2248 constructor, one is defined for it, enabling it to avoid
2249 this warning. For a template class, this does not
2250 happen, and so we would normally get a warning on:
2252 template <class T> class C { private: C(); };
2254 To avoid this asymmetry, we check TYPE_HAS_COPY_CTOR. All
2255 complete non-template or fully instantiated classes have this
2256 flag set. */
2257 if (!TYPE_HAS_COPY_CTOR (t))
2258 nonprivate_ctor = 1;
2259 else
2260 for (fn = CLASSTYPE_CONSTRUCTORS (t); fn; fn = OVL_NEXT (fn))
2262 tree ctor = OVL_CURRENT (fn);
2263 /* Ideally, we wouldn't count copy constructors (or, in
2264 fact, any constructor that takes an argument of the
2265 class type as a parameter) because such things cannot
2266 be used to construct an instance of the class unless
2267 you already have one. But, for now at least, we're
2268 more generous. */
2269 if (! TREE_PRIVATE (ctor))
2271 nonprivate_ctor = 1;
2272 break;
2276 if (nonprivate_ctor == 0)
2278 warning (OPT_Wctor_dtor_privacy,
2279 "%q#T only defines private constructors and has no friends",
2281 return;
2286 static struct {
2287 gt_pointer_operator new_value;
2288 void *cookie;
2289 } resort_data;
2291 /* Comparison function to compare two TYPE_METHOD_VEC entries by name. */
2293 static int
2294 method_name_cmp (const void* m1_p, const void* m2_p)
2296 const tree *const m1 = (const tree *) m1_p;
2297 const tree *const m2 = (const tree *) m2_p;
2299 if (*m1 == NULL_TREE && *m2 == NULL_TREE)
2300 return 0;
2301 if (*m1 == NULL_TREE)
2302 return -1;
2303 if (*m2 == NULL_TREE)
2304 return 1;
2305 if (DECL_NAME (OVL_CURRENT (*m1)) < DECL_NAME (OVL_CURRENT (*m2)))
2306 return -1;
2307 return 1;
2310 /* This routine compares two fields like method_name_cmp but using the
2311 pointer operator in resort_field_decl_data. */
2313 static int
2314 resort_method_name_cmp (const void* m1_p, const void* m2_p)
2316 const tree *const m1 = (const tree *) m1_p;
2317 const tree *const m2 = (const tree *) m2_p;
2318 if (*m1 == NULL_TREE && *m2 == NULL_TREE)
2319 return 0;
2320 if (*m1 == NULL_TREE)
2321 return -1;
2322 if (*m2 == NULL_TREE)
2323 return 1;
2325 tree d1 = DECL_NAME (OVL_CURRENT (*m1));
2326 tree d2 = DECL_NAME (OVL_CURRENT (*m2));
2327 resort_data.new_value (&d1, resort_data.cookie);
2328 resort_data.new_value (&d2, resort_data.cookie);
2329 if (d1 < d2)
2330 return -1;
2332 return 1;
2335 /* Resort TYPE_METHOD_VEC because pointers have been reordered. */
2337 void
2338 resort_type_method_vec (void* obj,
2339 void* /*orig_obj*/,
2340 gt_pointer_operator new_value,
2341 void* cookie)
2343 vec<tree, va_gc> *method_vec = (vec<tree, va_gc> *) obj;
2344 int len = vec_safe_length (method_vec);
2345 size_t slot;
2346 tree fn;
2348 /* The type conversion ops have to live at the front of the vec, so we
2349 can't sort them. */
2350 for (slot = CLASSTYPE_FIRST_CONVERSION_SLOT;
2351 vec_safe_iterate (method_vec, slot, &fn);
2352 ++slot)
2353 if (!DECL_CONV_FN_P (OVL_CURRENT (fn)))
2354 break;
2356 if (len - slot > 1)
2358 resort_data.new_value = new_value;
2359 resort_data.cookie = cookie;
2360 qsort (method_vec->address () + slot, len - slot, sizeof (tree),
2361 resort_method_name_cmp);
2365 /* Warn about duplicate methods in fn_fields.
2367 Sort methods that are not special (i.e., constructors, destructors,
2368 and type conversion operators) so that we can find them faster in
2369 search. */
2371 static void
2372 finish_struct_methods (tree t)
2374 tree fn_fields;
2375 vec<tree, va_gc> *method_vec;
2376 int slot, len;
2378 method_vec = CLASSTYPE_METHOD_VEC (t);
2379 if (!method_vec)
2380 return;
2382 len = method_vec->length ();
2384 /* Clear DECL_IN_AGGR_P for all functions. */
2385 for (fn_fields = TYPE_METHODS (t); fn_fields;
2386 fn_fields = DECL_CHAIN (fn_fields))
2387 DECL_IN_AGGR_P (fn_fields) = 0;
2389 /* Issue warnings about private constructors and such. If there are
2390 no methods, then some public defaults are generated. */
2391 maybe_warn_about_overly_private_class (t);
2393 /* The type conversion ops have to live at the front of the vec, so we
2394 can't sort them. */
2395 for (slot = CLASSTYPE_FIRST_CONVERSION_SLOT;
2396 method_vec->iterate (slot, &fn_fields);
2397 ++slot)
2398 if (!DECL_CONV_FN_P (OVL_CURRENT (fn_fields)))
2399 break;
2400 if (len - slot > 1)
2401 qsort (method_vec->address () + slot,
2402 len-slot, sizeof (tree), method_name_cmp);
2405 /* Make BINFO's vtable have N entries, including RTTI entries,
2406 vbase and vcall offsets, etc. Set its type and call the back end
2407 to lay it out. */
2409 static void
2410 layout_vtable_decl (tree binfo, int n)
2412 tree atype;
2413 tree vtable;
2415 atype = build_array_of_n_type (vtable_entry_type, n);
2416 layout_type (atype);
2418 /* We may have to grow the vtable. */
2419 vtable = get_vtbl_decl_for_binfo (binfo);
2420 if (!same_type_p (TREE_TYPE (vtable), atype))
2422 TREE_TYPE (vtable) = atype;
2423 DECL_SIZE (vtable) = DECL_SIZE_UNIT (vtable) = NULL_TREE;
2424 layout_decl (vtable, 0);
2428 /* True iff FNDECL and BASE_FNDECL (both non-static member functions)
2429 have the same signature. */
2432 same_signature_p (const_tree fndecl, const_tree base_fndecl)
2434 /* One destructor overrides another if they are the same kind of
2435 destructor. */
2436 if (DECL_DESTRUCTOR_P (base_fndecl) && DECL_DESTRUCTOR_P (fndecl)
2437 && special_function_p (base_fndecl) == special_function_p (fndecl))
2438 return 1;
2439 /* But a non-destructor never overrides a destructor, nor vice
2440 versa, nor do different kinds of destructors override
2441 one-another. For example, a complete object destructor does not
2442 override a deleting destructor. */
2443 if (DECL_DESTRUCTOR_P (base_fndecl) || DECL_DESTRUCTOR_P (fndecl))
2444 return 0;
2446 if (DECL_NAME (fndecl) == DECL_NAME (base_fndecl)
2447 || (DECL_CONV_FN_P (fndecl)
2448 && DECL_CONV_FN_P (base_fndecl)
2449 && same_type_p (DECL_CONV_FN_TYPE (fndecl),
2450 DECL_CONV_FN_TYPE (base_fndecl))))
2452 tree fntype = TREE_TYPE (fndecl);
2453 tree base_fntype = TREE_TYPE (base_fndecl);
2454 if (type_memfn_quals (fntype) == type_memfn_quals (base_fntype)
2455 && type_memfn_rqual (fntype) == type_memfn_rqual (base_fntype)
2456 && compparms (FUNCTION_FIRST_USER_PARMTYPE (fndecl),
2457 FUNCTION_FIRST_USER_PARMTYPE (base_fndecl)))
2458 return 1;
2460 return 0;
2463 /* Returns TRUE if DERIVED is a binfo containing the binfo BASE as a
2464 subobject. */
2466 static bool
2467 base_derived_from (tree derived, tree base)
2469 tree probe;
2471 for (probe = base; probe; probe = BINFO_INHERITANCE_CHAIN (probe))
2473 if (probe == derived)
2474 return true;
2475 else if (BINFO_VIRTUAL_P (probe))
2476 /* If we meet a virtual base, we can't follow the inheritance
2477 any more. See if the complete type of DERIVED contains
2478 such a virtual base. */
2479 return (binfo_for_vbase (BINFO_TYPE (probe), BINFO_TYPE (derived))
2480 != NULL_TREE);
2482 return false;
2485 struct find_final_overrider_data {
2486 /* The function for which we are trying to find a final overrider. */
2487 tree fn;
2488 /* The base class in which the function was declared. */
2489 tree declaring_base;
2490 /* The candidate overriders. */
2491 tree candidates;
2492 /* Path to most derived. */
2493 vec<tree> path;
2496 /* Add the overrider along the current path to FFOD->CANDIDATES.
2497 Returns true if an overrider was found; false otherwise. */
2499 static bool
2500 dfs_find_final_overrider_1 (tree binfo,
2501 find_final_overrider_data *ffod,
2502 unsigned depth)
2504 tree method;
2506 /* If BINFO is not the most derived type, try a more derived class.
2507 A definition there will overrider a definition here. */
2508 if (depth)
2510 depth--;
2511 if (dfs_find_final_overrider_1
2512 (ffod->path[depth], ffod, depth))
2513 return true;
2516 method = look_for_overrides_here (BINFO_TYPE (binfo), ffod->fn);
2517 if (method)
2519 tree *candidate = &ffod->candidates;
2521 /* Remove any candidates overridden by this new function. */
2522 while (*candidate)
2524 /* If *CANDIDATE overrides METHOD, then METHOD
2525 cannot override anything else on the list. */
2526 if (base_derived_from (TREE_VALUE (*candidate), binfo))
2527 return true;
2528 /* If METHOD overrides *CANDIDATE, remove *CANDIDATE. */
2529 if (base_derived_from (binfo, TREE_VALUE (*candidate)))
2530 *candidate = TREE_CHAIN (*candidate);
2531 else
2532 candidate = &TREE_CHAIN (*candidate);
2535 /* Add the new function. */
2536 ffod->candidates = tree_cons (method, binfo, ffod->candidates);
2537 return true;
2540 return false;
2543 /* Called from find_final_overrider via dfs_walk. */
2545 static tree
2546 dfs_find_final_overrider_pre (tree binfo, void *data)
2548 find_final_overrider_data *ffod = (find_final_overrider_data *) data;
2550 if (binfo == ffod->declaring_base)
2551 dfs_find_final_overrider_1 (binfo, ffod, ffod->path.length ());
2552 ffod->path.safe_push (binfo);
2554 return NULL_TREE;
2557 static tree
2558 dfs_find_final_overrider_post (tree /*binfo*/, void *data)
2560 find_final_overrider_data *ffod = (find_final_overrider_data *) data;
2561 ffod->path.pop ();
2563 return NULL_TREE;
2566 /* Returns a TREE_LIST whose TREE_PURPOSE is the final overrider for
2567 FN and whose TREE_VALUE is the binfo for the base where the
2568 overriding occurs. BINFO (in the hierarchy dominated by the binfo
2569 DERIVED) is the base object in which FN is declared. */
2571 static tree
2572 find_final_overrider (tree derived, tree binfo, tree fn)
2574 find_final_overrider_data ffod;
2576 /* Getting this right is a little tricky. This is valid:
2578 struct S { virtual void f (); };
2579 struct T { virtual void f (); };
2580 struct U : public S, public T { };
2582 even though calling `f' in `U' is ambiguous. But,
2584 struct R { virtual void f(); };
2585 struct S : virtual public R { virtual void f (); };
2586 struct T : virtual public R { virtual void f (); };
2587 struct U : public S, public T { };
2589 is not -- there's no way to decide whether to put `S::f' or
2590 `T::f' in the vtable for `R'.
2592 The solution is to look at all paths to BINFO. If we find
2593 different overriders along any two, then there is a problem. */
2594 if (DECL_THUNK_P (fn))
2595 fn = THUNK_TARGET (fn);
2597 /* Determine the depth of the hierarchy. */
2598 ffod.fn = fn;
2599 ffod.declaring_base = binfo;
2600 ffod.candidates = NULL_TREE;
2601 ffod.path.create (30);
2603 dfs_walk_all (derived, dfs_find_final_overrider_pre,
2604 dfs_find_final_overrider_post, &ffod);
2606 ffod.path.release ();
2608 /* If there was no winner, issue an error message. */
2609 if (!ffod.candidates || TREE_CHAIN (ffod.candidates))
2610 return error_mark_node;
2612 return ffod.candidates;
2615 /* Return the index of the vcall offset for FN when TYPE is used as a
2616 virtual base. */
2618 static tree
2619 get_vcall_index (tree fn, tree type)
2621 vec<tree_pair_s, va_gc> *indices = CLASSTYPE_VCALL_INDICES (type);
2622 tree_pair_p p;
2623 unsigned ix;
2625 FOR_EACH_VEC_SAFE_ELT (indices, ix, p)
2626 if ((DECL_DESTRUCTOR_P (fn) && DECL_DESTRUCTOR_P (p->purpose))
2627 || same_signature_p (fn, p->purpose))
2628 return p->value;
2630 /* There should always be an appropriate index. */
2631 gcc_unreachable ();
2634 /* Update an entry in the vtable for BINFO, which is in the hierarchy
2635 dominated by T. FN is the old function; VIRTUALS points to the
2636 corresponding position in the new BINFO_VIRTUALS list. IX is the index
2637 of that entry in the list. */
2639 static void
2640 update_vtable_entry_for_fn (tree t, tree binfo, tree fn, tree* virtuals,
2641 unsigned ix)
2643 tree b;
2644 tree overrider;
2645 tree delta;
2646 tree virtual_base;
2647 tree first_defn;
2648 tree overrider_fn, overrider_target;
2649 tree target_fn = DECL_THUNK_P (fn) ? THUNK_TARGET (fn) : fn;
2650 tree over_return, base_return;
2651 bool lost = false;
2653 /* Find the nearest primary base (possibly binfo itself) which defines
2654 this function; this is the class the caller will convert to when
2655 calling FN through BINFO. */
2656 for (b = binfo; ; b = get_primary_binfo (b))
2658 gcc_assert (b);
2659 if (look_for_overrides_here (BINFO_TYPE (b), target_fn))
2660 break;
2662 /* The nearest definition is from a lost primary. */
2663 if (BINFO_LOST_PRIMARY_P (b))
2664 lost = true;
2666 first_defn = b;
2668 /* Find the final overrider. */
2669 overrider = find_final_overrider (TYPE_BINFO (t), b, target_fn);
2670 if (overrider == error_mark_node)
2672 error ("no unique final overrider for %qD in %qT", target_fn, t);
2673 return;
2675 overrider_target = overrider_fn = TREE_PURPOSE (overrider);
2677 /* Check for adjusting covariant return types. */
2678 over_return = TREE_TYPE (TREE_TYPE (overrider_target));
2679 base_return = TREE_TYPE (TREE_TYPE (target_fn));
2681 if (POINTER_TYPE_P (over_return)
2682 && TREE_CODE (over_return) == TREE_CODE (base_return)
2683 && CLASS_TYPE_P (TREE_TYPE (over_return))
2684 && CLASS_TYPE_P (TREE_TYPE (base_return))
2685 /* If the overrider is invalid, don't even try. */
2686 && !DECL_INVALID_OVERRIDER_P (overrider_target))
2688 /* If FN is a covariant thunk, we must figure out the adjustment
2689 to the final base FN was converting to. As OVERRIDER_TARGET might
2690 also be converting to the return type of FN, we have to
2691 combine the two conversions here. */
2692 tree fixed_offset, virtual_offset;
2694 over_return = TREE_TYPE (over_return);
2695 base_return = TREE_TYPE (base_return);
2697 if (DECL_THUNK_P (fn))
2699 gcc_assert (DECL_RESULT_THUNK_P (fn));
2700 fixed_offset = ssize_int (THUNK_FIXED_OFFSET (fn));
2701 virtual_offset = THUNK_VIRTUAL_OFFSET (fn);
2703 else
2704 fixed_offset = virtual_offset = NULL_TREE;
2706 if (virtual_offset)
2707 /* Find the equivalent binfo within the return type of the
2708 overriding function. We will want the vbase offset from
2709 there. */
2710 virtual_offset = binfo_for_vbase (BINFO_TYPE (virtual_offset),
2711 over_return);
2712 else if (!same_type_ignoring_top_level_qualifiers_p
2713 (over_return, base_return))
2715 /* There was no existing virtual thunk (which takes
2716 precedence). So find the binfo of the base function's
2717 return type within the overriding function's return type.
2718 Fortunately we know the covariancy is valid (it
2719 has already been checked), so we can just iterate along
2720 the binfos, which have been chained in inheritance graph
2721 order. Of course it is lame that we have to repeat the
2722 search here anyway -- we should really be caching pieces
2723 of the vtable and avoiding this repeated work. */
2724 tree thunk_binfo, base_binfo;
2726 /* Find the base binfo within the overriding function's
2727 return type. We will always find a thunk_binfo, except
2728 when the covariancy is invalid (which we will have
2729 already diagnosed). */
2730 for (base_binfo = TYPE_BINFO (base_return),
2731 thunk_binfo = TYPE_BINFO (over_return);
2732 thunk_binfo;
2733 thunk_binfo = TREE_CHAIN (thunk_binfo))
2734 if (SAME_BINFO_TYPE_P (BINFO_TYPE (thunk_binfo),
2735 BINFO_TYPE (base_binfo)))
2736 break;
2738 /* See if virtual inheritance is involved. */
2739 for (virtual_offset = thunk_binfo;
2740 virtual_offset;
2741 virtual_offset = BINFO_INHERITANCE_CHAIN (virtual_offset))
2742 if (BINFO_VIRTUAL_P (virtual_offset))
2743 break;
2745 if (virtual_offset
2746 || (thunk_binfo && !BINFO_OFFSET_ZEROP (thunk_binfo)))
2748 tree offset = fold_convert (ssizetype, BINFO_OFFSET (thunk_binfo));
2750 if (virtual_offset)
2752 /* We convert via virtual base. Adjust the fixed
2753 offset to be from there. */
2754 offset =
2755 size_diffop (offset,
2756 fold_convert (ssizetype,
2757 BINFO_OFFSET (virtual_offset)));
2759 if (fixed_offset)
2760 /* There was an existing fixed offset, this must be
2761 from the base just converted to, and the base the
2762 FN was thunking to. */
2763 fixed_offset = size_binop (PLUS_EXPR, fixed_offset, offset);
2764 else
2765 fixed_offset = offset;
2769 if (fixed_offset || virtual_offset)
2770 /* Replace the overriding function with a covariant thunk. We
2771 will emit the overriding function in its own slot as
2772 well. */
2773 overrider_fn = make_thunk (overrider_target, /*this_adjusting=*/0,
2774 fixed_offset, virtual_offset);
2776 else
2777 gcc_assert (DECL_INVALID_OVERRIDER_P (overrider_target) ||
2778 !DECL_THUNK_P (fn));
2780 /* If we need a covariant thunk, then we may need to adjust first_defn.
2781 The ABI specifies that the thunks emitted with a function are
2782 determined by which bases the function overrides, so we need to be
2783 sure that we're using a thunk for some overridden base; even if we
2784 know that the necessary this adjustment is zero, there may not be an
2785 appropriate zero-this-adjusment thunk for us to use since thunks for
2786 overriding virtual bases always use the vcall offset.
2788 Furthermore, just choosing any base that overrides this function isn't
2789 quite right, as this slot won't be used for calls through a type that
2790 puts a covariant thunk here. Calling the function through such a type
2791 will use a different slot, and that slot is the one that determines
2792 the thunk emitted for that base.
2794 So, keep looking until we find the base that we're really overriding
2795 in this slot: the nearest primary base that doesn't use a covariant
2796 thunk in this slot. */
2797 if (overrider_target != overrider_fn)
2799 if (BINFO_TYPE (b) == DECL_CONTEXT (overrider_target))
2800 /* We already know that the overrider needs a covariant thunk. */
2801 b = get_primary_binfo (b);
2802 for (; ; b = get_primary_binfo (b))
2804 tree main_binfo = TYPE_BINFO (BINFO_TYPE (b));
2805 tree bv = chain_index (ix, BINFO_VIRTUALS (main_binfo));
2806 if (!DECL_THUNK_P (TREE_VALUE (bv)))
2807 break;
2808 if (BINFO_LOST_PRIMARY_P (b))
2809 lost = true;
2811 first_defn = b;
2814 /* Assume that we will produce a thunk that convert all the way to
2815 the final overrider, and not to an intermediate virtual base. */
2816 virtual_base = NULL_TREE;
2818 /* See if we can convert to an intermediate virtual base first, and then
2819 use the vcall offset located there to finish the conversion. */
2820 for (; b; b = BINFO_INHERITANCE_CHAIN (b))
2822 /* If we find the final overrider, then we can stop
2823 walking. */
2824 if (SAME_BINFO_TYPE_P (BINFO_TYPE (b),
2825 BINFO_TYPE (TREE_VALUE (overrider))))
2826 break;
2828 /* If we find a virtual base, and we haven't yet found the
2829 overrider, then there is a virtual base between the
2830 declaring base (first_defn) and the final overrider. */
2831 if (BINFO_VIRTUAL_P (b))
2833 virtual_base = b;
2834 break;
2838 /* Compute the constant adjustment to the `this' pointer. The
2839 `this' pointer, when this function is called, will point at BINFO
2840 (or one of its primary bases, which are at the same offset). */
2841 if (virtual_base)
2842 /* The `this' pointer needs to be adjusted from the declaration to
2843 the nearest virtual base. */
2844 delta = size_diffop_loc (input_location,
2845 fold_convert (ssizetype, BINFO_OFFSET (virtual_base)),
2846 fold_convert (ssizetype, BINFO_OFFSET (first_defn)));
2847 else if (lost)
2848 /* If the nearest definition is in a lost primary, we don't need an
2849 entry in our vtable. Except possibly in a constructor vtable,
2850 if we happen to get our primary back. In that case, the offset
2851 will be zero, as it will be a primary base. */
2852 delta = size_zero_node;
2853 else
2854 /* The `this' pointer needs to be adjusted from pointing to
2855 BINFO to pointing at the base where the final overrider
2856 appears. */
2857 delta = size_diffop_loc (input_location,
2858 fold_convert (ssizetype,
2859 BINFO_OFFSET (TREE_VALUE (overrider))),
2860 fold_convert (ssizetype, BINFO_OFFSET (binfo)));
2862 modify_vtable_entry (t, binfo, overrider_fn, delta, virtuals);
2864 if (virtual_base)
2865 BV_VCALL_INDEX (*virtuals)
2866 = get_vcall_index (overrider_target, BINFO_TYPE (virtual_base));
2867 else
2868 BV_VCALL_INDEX (*virtuals) = NULL_TREE;
2870 BV_LOST_PRIMARY (*virtuals) = lost;
2873 /* Called from modify_all_vtables via dfs_walk. */
2875 static tree
2876 dfs_modify_vtables (tree binfo, void* data)
2878 tree t = (tree) data;
2879 tree virtuals;
2880 tree old_virtuals;
2881 unsigned ix;
2883 if (!TYPE_CONTAINS_VPTR_P (BINFO_TYPE (binfo)))
2884 /* A base without a vtable needs no modification, and its bases
2885 are uninteresting. */
2886 return dfs_skip_bases;
2888 if (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), t)
2889 && !CLASSTYPE_HAS_PRIMARY_BASE_P (t))
2890 /* Don't do the primary vtable, if it's new. */
2891 return NULL_TREE;
2893 if (BINFO_PRIMARY_P (binfo) && !BINFO_VIRTUAL_P (binfo))
2894 /* There's no need to modify the vtable for a non-virtual primary
2895 base; we're not going to use that vtable anyhow. We do still
2896 need to do this for virtual primary bases, as they could become
2897 non-primary in a construction vtable. */
2898 return NULL_TREE;
2900 make_new_vtable (t, binfo);
2902 /* Now, go through each of the virtual functions in the virtual
2903 function table for BINFO. Find the final overrider, and update
2904 the BINFO_VIRTUALS list appropriately. */
2905 for (ix = 0, virtuals = BINFO_VIRTUALS (binfo),
2906 old_virtuals = BINFO_VIRTUALS (TYPE_BINFO (BINFO_TYPE (binfo)));
2907 virtuals;
2908 ix++, virtuals = TREE_CHAIN (virtuals),
2909 old_virtuals = TREE_CHAIN (old_virtuals))
2910 update_vtable_entry_for_fn (t,
2911 binfo,
2912 BV_FN (old_virtuals),
2913 &virtuals, ix);
2915 return NULL_TREE;
2918 /* Update all of the primary and secondary vtables for T. Create new
2919 vtables as required, and initialize their RTTI information. Each
2920 of the functions in VIRTUALS is declared in T and may override a
2921 virtual function from a base class; find and modify the appropriate
2922 entries to point to the overriding functions. Returns a list, in
2923 declaration order, of the virtual functions that are declared in T,
2924 but do not appear in the primary base class vtable, and which
2925 should therefore be appended to the end of the vtable for T. */
2927 static tree
2928 modify_all_vtables (tree t, tree virtuals)
2930 tree binfo = TYPE_BINFO (t);
2931 tree *fnsp;
2933 /* Mangle the vtable name before entering dfs_walk (c++/51884). */
2934 if (TYPE_CONTAINS_VPTR_P (t))
2935 get_vtable_decl (t, false);
2937 /* Update all of the vtables. */
2938 dfs_walk_once (binfo, dfs_modify_vtables, NULL, t);
2940 /* Add virtual functions not already in our primary vtable. These
2941 will be both those introduced by this class, and those overridden
2942 from secondary bases. It does not include virtuals merely
2943 inherited from secondary bases. */
2944 for (fnsp = &virtuals; *fnsp; )
2946 tree fn = TREE_VALUE (*fnsp);
2948 if (!value_member (fn, BINFO_VIRTUALS (binfo))
2949 || DECL_VINDEX (fn) == error_mark_node)
2951 /* We don't need to adjust the `this' pointer when
2952 calling this function. */
2953 BV_DELTA (*fnsp) = integer_zero_node;
2954 BV_VCALL_INDEX (*fnsp) = NULL_TREE;
2956 /* This is a function not already in our vtable. Keep it. */
2957 fnsp = &TREE_CHAIN (*fnsp);
2959 else
2960 /* We've already got an entry for this function. Skip it. */
2961 *fnsp = TREE_CHAIN (*fnsp);
2964 return virtuals;
2967 /* Get the base virtual function declarations in T that have the
2968 indicated NAME. */
2970 static void
2971 get_basefndecls (tree name, tree t, vec<tree> *base_fndecls)
2973 tree methods;
2974 int n_baseclasses = BINFO_N_BASE_BINFOS (TYPE_BINFO (t));
2975 int i;
2977 /* Find virtual functions in T with the indicated NAME. */
2978 i = lookup_fnfields_1 (t, name);
2979 bool found_decls = false;
2980 if (i != -1)
2981 for (methods = (*CLASSTYPE_METHOD_VEC (t))[i];
2982 methods;
2983 methods = OVL_NEXT (methods))
2985 tree method = OVL_CURRENT (methods);
2987 if (TREE_CODE (method) == FUNCTION_DECL
2988 && DECL_VINDEX (method))
2990 base_fndecls->safe_push (method);
2991 found_decls = true;
2995 if (found_decls)
2996 return;
2998 for (i = 0; i < n_baseclasses; i++)
3000 tree basetype = BINFO_TYPE (BINFO_BASE_BINFO (TYPE_BINFO (t), i));
3001 get_basefndecls (name, basetype, base_fndecls);
3005 /* If this declaration supersedes the declaration of
3006 a method declared virtual in the base class, then
3007 mark this field as being virtual as well. */
3009 void
3010 check_for_override (tree decl, tree ctype)
3012 bool overrides_found = false;
3013 if (TREE_CODE (decl) == TEMPLATE_DECL)
3014 /* In [temp.mem] we have:
3016 A specialization of a member function template does not
3017 override a virtual function from a base class. */
3018 return;
3019 if ((DECL_DESTRUCTOR_P (decl)
3020 || IDENTIFIER_VIRTUAL_P (DECL_NAME (decl))
3021 || DECL_CONV_FN_P (decl))
3022 && look_for_overrides (ctype, decl)
3023 && !DECL_STATIC_FUNCTION_P (decl))
3024 /* Set DECL_VINDEX to a value that is neither an INTEGER_CST nor
3025 the error_mark_node so that we know it is an overriding
3026 function. */
3028 DECL_VINDEX (decl) = decl;
3029 overrides_found = true;
3030 if (warn_override && !DECL_OVERRIDE_P (decl)
3031 && !DECL_DESTRUCTOR_P (decl))
3032 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wsuggest_override,
3033 "%qD can be marked override", decl);
3036 if (DECL_VIRTUAL_P (decl))
3038 if (!DECL_VINDEX (decl))
3039 DECL_VINDEX (decl) = error_mark_node;
3040 IDENTIFIER_VIRTUAL_P (DECL_NAME (decl)) = 1;
3041 if (DECL_DESTRUCTOR_P (decl))
3042 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (ctype) = true;
3044 else if (DECL_FINAL_P (decl))
3045 error ("%q+#D marked %<final%>, but is not virtual", decl);
3046 if (DECL_OVERRIDE_P (decl) && !overrides_found)
3047 error ("%q+#D marked %<override%>, but does not override", decl);
3050 /* Warn about hidden virtual functions that are not overridden in t.
3051 We know that constructors and destructors don't apply. */
3053 static void
3054 warn_hidden (tree t)
3056 vec<tree, va_gc> *method_vec = CLASSTYPE_METHOD_VEC (t);
3057 tree fns;
3058 size_t i;
3060 /* We go through each separately named virtual function. */
3061 for (i = CLASSTYPE_FIRST_CONVERSION_SLOT;
3062 vec_safe_iterate (method_vec, i, &fns);
3063 ++i)
3065 tree fn;
3066 tree name;
3067 tree fndecl;
3068 tree base_binfo;
3069 tree binfo;
3070 int j;
3072 /* All functions in this slot in the CLASSTYPE_METHOD_VEC will
3073 have the same name. Figure out what name that is. */
3074 name = DECL_NAME (OVL_CURRENT (fns));
3075 /* There are no possibly hidden functions yet. */
3076 auto_vec<tree, 20> base_fndecls;
3077 /* Iterate through all of the base classes looking for possibly
3078 hidden functions. */
3079 for (binfo = TYPE_BINFO (t), j = 0;
3080 BINFO_BASE_ITERATE (binfo, j, base_binfo); j++)
3082 tree basetype = BINFO_TYPE (base_binfo);
3083 get_basefndecls (name, basetype, &base_fndecls);
3086 /* If there are no functions to hide, continue. */
3087 if (base_fndecls.is_empty ())
3088 continue;
3090 /* Remove any overridden functions. */
3091 for (fn = fns; fn; fn = OVL_NEXT (fn))
3093 fndecl = OVL_CURRENT (fn);
3094 if (TREE_CODE (fndecl) == FUNCTION_DECL
3095 && DECL_VINDEX (fndecl))
3097 /* If the method from the base class has the same
3098 signature as the method from the derived class, it
3099 has been overridden. */
3100 for (size_t k = 0; k < base_fndecls.length (); k++)
3101 if (base_fndecls[k]
3102 && same_signature_p (fndecl, base_fndecls[k]))
3103 base_fndecls[k] = NULL_TREE;
3107 /* Now give a warning for all base functions without overriders,
3108 as they are hidden. */
3109 size_t k;
3110 tree base_fndecl;
3111 FOR_EACH_VEC_ELT (base_fndecls, k, base_fndecl)
3112 if (base_fndecl)
3114 /* Here we know it is a hider, and no overrider exists. */
3115 warning_at (location_of (base_fndecl),
3116 OPT_Woverloaded_virtual,
3117 "%qD was hidden", base_fndecl);
3118 warning_at (location_of (fns),
3119 OPT_Woverloaded_virtual, " by %qD", fns);
3124 /* Recursive helper for finish_struct_anon. */
3126 static void
3127 finish_struct_anon_r (tree field, bool complain)
3129 bool is_union = TREE_CODE (TREE_TYPE (field)) == UNION_TYPE;
3130 tree elt = TYPE_FIELDS (TREE_TYPE (field));
3131 for (; elt; elt = DECL_CHAIN (elt))
3133 /* We're generally only interested in entities the user
3134 declared, but we also find nested classes by noticing
3135 the TYPE_DECL that we create implicitly. You're
3136 allowed to put one anonymous union inside another,
3137 though, so we explicitly tolerate that. We use
3138 TYPE_UNNAMED_P rather than ANON_AGGR_TYPE_P so that
3139 we also allow unnamed types used for defining fields. */
3140 if (DECL_ARTIFICIAL (elt)
3141 && (!DECL_IMPLICIT_TYPEDEF_P (elt)
3142 || TYPE_UNNAMED_P (TREE_TYPE (elt))))
3143 continue;
3145 if (TREE_CODE (elt) != FIELD_DECL)
3147 /* We already complained about static data members in
3148 finish_static_data_member_decl. */
3149 if (complain && !VAR_P (elt))
3151 if (is_union)
3152 permerror (DECL_SOURCE_LOCATION (elt),
3153 "%q#D invalid; an anonymous union can "
3154 "only have non-static data members", elt);
3155 else
3156 permerror (DECL_SOURCE_LOCATION (elt),
3157 "%q#D invalid; an anonymous struct can "
3158 "only have non-static data members", elt);
3160 continue;
3163 if (complain)
3165 if (TREE_PRIVATE (elt))
3167 if (is_union)
3168 permerror (DECL_SOURCE_LOCATION (elt),
3169 "private member %q#D in anonymous union", elt);
3170 else
3171 permerror (DECL_SOURCE_LOCATION (elt),
3172 "private member %q#D in anonymous struct", elt);
3174 else if (TREE_PROTECTED (elt))
3176 if (is_union)
3177 permerror (DECL_SOURCE_LOCATION (elt),
3178 "protected member %q#D in anonymous union", elt);
3179 else
3180 permerror (DECL_SOURCE_LOCATION (elt),
3181 "protected member %q#D in anonymous struct", elt);
3185 TREE_PRIVATE (elt) = TREE_PRIVATE (field);
3186 TREE_PROTECTED (elt) = TREE_PROTECTED (field);
3188 /* Recurse into the anonymous aggregates to handle correctly
3189 access control (c++/24926):
3191 class A {
3192 union {
3193 union {
3194 int i;
3199 int j=A().i; */
3200 if (DECL_NAME (elt) == NULL_TREE
3201 && ANON_AGGR_TYPE_P (TREE_TYPE (elt)))
3202 finish_struct_anon_r (elt, /*complain=*/false);
3206 /* Check for things that are invalid. There are probably plenty of other
3207 things we should check for also. */
3209 static void
3210 finish_struct_anon (tree t)
3212 for (tree field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
3214 if (TREE_STATIC (field))
3215 continue;
3216 if (TREE_CODE (field) != FIELD_DECL)
3217 continue;
3219 if (DECL_NAME (field) == NULL_TREE
3220 && ANON_AGGR_TYPE_P (TREE_TYPE (field)))
3221 finish_struct_anon_r (field, /*complain=*/true);
3225 /* Add T to CLASSTYPE_DECL_LIST of current_class_type which
3226 will be used later during class template instantiation.
3227 When FRIEND_P is zero, T can be a static member data (VAR_DECL),
3228 a non-static member data (FIELD_DECL), a member function
3229 (FUNCTION_DECL), a nested type (RECORD_TYPE, ENUM_TYPE),
3230 a typedef (TYPE_DECL) or a member class template (TEMPLATE_DECL)
3231 When FRIEND_P is nonzero, T is either a friend class
3232 (RECORD_TYPE, TEMPLATE_DECL) or a friend function
3233 (FUNCTION_DECL, TEMPLATE_DECL). */
3235 void
3236 maybe_add_class_template_decl_list (tree type, tree t, int friend_p)
3238 /* Save some memory by not creating TREE_LIST if TYPE is not template. */
3239 if (CLASSTYPE_TEMPLATE_INFO (type))
3240 CLASSTYPE_DECL_LIST (type)
3241 = tree_cons (friend_p ? NULL_TREE : type,
3242 t, CLASSTYPE_DECL_LIST (type));
3245 /* This function is called from declare_virt_assop_and_dtor via
3246 dfs_walk_all.
3248 DATA is a type that direcly or indirectly inherits the base
3249 represented by BINFO. If BINFO contains a virtual assignment [copy
3250 assignment or move assigment] operator or a virtual constructor,
3251 declare that function in DATA if it hasn't been already declared. */
3253 static tree
3254 dfs_declare_virt_assop_and_dtor (tree binfo, void *data)
3256 tree bv, fn, t = (tree)data;
3257 tree opname = cp_assignment_operator_id (NOP_EXPR);
3259 gcc_assert (t && CLASS_TYPE_P (t));
3260 gcc_assert (binfo && TREE_CODE (binfo) == TREE_BINFO);
3262 if (!TYPE_CONTAINS_VPTR_P (BINFO_TYPE (binfo)))
3263 /* A base without a vtable needs no modification, and its bases
3264 are uninteresting. */
3265 return dfs_skip_bases;
3267 if (BINFO_PRIMARY_P (binfo))
3268 /* If this is a primary base, then we have already looked at the
3269 virtual functions of its vtable. */
3270 return NULL_TREE;
3272 for (bv = BINFO_VIRTUALS (binfo); bv; bv = TREE_CHAIN (bv))
3274 fn = BV_FN (bv);
3276 if (DECL_NAME (fn) == opname)
3278 if (CLASSTYPE_LAZY_COPY_ASSIGN (t))
3279 lazily_declare_fn (sfk_copy_assignment, t);
3280 if (CLASSTYPE_LAZY_MOVE_ASSIGN (t))
3281 lazily_declare_fn (sfk_move_assignment, t);
3283 else if (DECL_DESTRUCTOR_P (fn)
3284 && CLASSTYPE_LAZY_DESTRUCTOR (t))
3285 lazily_declare_fn (sfk_destructor, t);
3288 return NULL_TREE;
3291 /* If the class type T has a direct or indirect base that contains a
3292 virtual assignment operator or a virtual destructor, declare that
3293 function in T if it hasn't been already declared. */
3295 static void
3296 declare_virt_assop_and_dtor (tree t)
3298 if (!(TYPE_POLYMORPHIC_P (t)
3299 && (CLASSTYPE_LAZY_COPY_ASSIGN (t)
3300 || CLASSTYPE_LAZY_MOVE_ASSIGN (t)
3301 || CLASSTYPE_LAZY_DESTRUCTOR (t))))
3302 return;
3304 dfs_walk_all (TYPE_BINFO (t),
3305 dfs_declare_virt_assop_and_dtor,
3306 NULL, t);
3309 /* Declare the inheriting constructor for class T inherited from base
3310 constructor CTOR with the parameter array PARMS of size NPARMS. */
3312 static void
3313 one_inheriting_sig (tree t, tree ctor, tree *parms, int nparms)
3315 /* We don't declare an inheriting ctor that would be a default,
3316 copy or move ctor for derived or base. */
3317 if (nparms == 0)
3318 return;
3319 if (nparms == 1
3320 && TREE_CODE (parms[0]) == REFERENCE_TYPE)
3322 tree parm = TYPE_MAIN_VARIANT (TREE_TYPE (parms[0]));
3323 if (parm == t || parm == DECL_CONTEXT (ctor))
3324 return;
3327 tree parmlist = void_list_node;
3328 for (int i = nparms - 1; i >= 0; i--)
3329 parmlist = tree_cons (NULL_TREE, parms[i], parmlist);
3330 tree fn = implicitly_declare_fn (sfk_inheriting_constructor,
3331 t, false, ctor, parmlist);
3332 gcc_assert (TYPE_MAIN_VARIANT (t) == t);
3333 if (add_method (t, fn, NULL_TREE))
3335 DECL_CHAIN (fn) = TYPE_METHODS (t);
3336 TYPE_METHODS (t) = fn;
3340 /* Declare all the inheriting constructors for class T inherited from base
3341 constructor CTOR. */
3343 static void
3344 one_inherited_ctor (tree ctor, tree t, tree using_decl)
3346 tree parms = FUNCTION_FIRST_USER_PARMTYPE (ctor);
3348 if (flag_new_inheriting_ctors)
3350 ctor = implicitly_declare_fn (sfk_inheriting_constructor,
3351 t, /*const*/false, ctor, parms);
3352 add_method (t, ctor, using_decl);
3353 TYPE_HAS_USER_CONSTRUCTOR (t) = true;
3354 return;
3357 tree *new_parms = XALLOCAVEC (tree, list_length (parms));
3358 int i = 0;
3359 for (; parms && parms != void_list_node; parms = TREE_CHAIN (parms))
3361 if (TREE_PURPOSE (parms))
3362 one_inheriting_sig (t, ctor, new_parms, i);
3363 new_parms[i++] = TREE_VALUE (parms);
3365 one_inheriting_sig (t, ctor, new_parms, i);
3366 if (parms == NULL_TREE)
3368 if (warning (OPT_Winherited_variadic_ctor,
3369 "the ellipsis in %qD is not inherited", ctor))
3370 inform (DECL_SOURCE_LOCATION (ctor), "%qD declared here", ctor);
3374 /* Create default constructors, assignment operators, and so forth for
3375 the type indicated by T, if they are needed. CANT_HAVE_CONST_CTOR,
3376 and CANT_HAVE_CONST_ASSIGNMENT are nonzero if, for whatever reason,
3377 the class cannot have a default constructor, copy constructor
3378 taking a const reference argument, or an assignment operator taking
3379 a const reference, respectively. */
3381 static void
3382 add_implicitly_declared_members (tree t, tree* access_decls,
3383 int cant_have_const_cctor,
3384 int cant_have_const_assignment)
3386 bool move_ok = false;
3388 if (cxx_dialect >= cxx11 && !CLASSTYPE_DESTRUCTORS (t)
3389 && !TYPE_HAS_COPY_CTOR (t) && !TYPE_HAS_COPY_ASSIGN (t)
3390 && !type_has_move_constructor (t) && !type_has_move_assign (t))
3391 move_ok = true;
3393 /* Destructor. */
3394 if (!CLASSTYPE_DESTRUCTORS (t))
3395 /* In general, we create destructors lazily. */
3396 CLASSTYPE_LAZY_DESTRUCTOR (t) = 1;
3398 /* [class.ctor]
3400 If there is no user-declared constructor for a class, a default
3401 constructor is implicitly declared. */
3402 if (! TYPE_HAS_USER_CONSTRUCTOR (t))
3404 TYPE_HAS_DEFAULT_CONSTRUCTOR (t) = 1;
3405 CLASSTYPE_LAZY_DEFAULT_CTOR (t) = 1;
3406 if (cxx_dialect >= cxx11)
3407 TYPE_HAS_CONSTEXPR_CTOR (t)
3408 /* Don't force the declaration to get a hard answer; if the
3409 definition would have made the class non-literal, it will still be
3410 non-literal because of the base or member in question, and that
3411 gives a better diagnostic. */
3412 = type_maybe_constexpr_default_constructor (t);
3415 /* [class.ctor]
3417 If a class definition does not explicitly declare a copy
3418 constructor, one is declared implicitly. */
3419 if (! TYPE_HAS_COPY_CTOR (t))
3421 TYPE_HAS_COPY_CTOR (t) = 1;
3422 TYPE_HAS_CONST_COPY_CTOR (t) = !cant_have_const_cctor;
3423 CLASSTYPE_LAZY_COPY_CTOR (t) = 1;
3424 if (move_ok)
3425 CLASSTYPE_LAZY_MOVE_CTOR (t) = 1;
3428 /* If there is no assignment operator, one will be created if and
3429 when it is needed. For now, just record whether or not the type
3430 of the parameter to the assignment operator will be a const or
3431 non-const reference. */
3432 if (!TYPE_HAS_COPY_ASSIGN (t))
3434 TYPE_HAS_COPY_ASSIGN (t) = 1;
3435 TYPE_HAS_CONST_COPY_ASSIGN (t) = !cant_have_const_assignment;
3436 CLASSTYPE_LAZY_COPY_ASSIGN (t) = 1;
3437 if (move_ok && !LAMBDA_TYPE_P (t))
3438 CLASSTYPE_LAZY_MOVE_ASSIGN (t) = 1;
3441 /* We can't be lazy about declaring functions that might override
3442 a virtual function from a base class. */
3443 declare_virt_assop_and_dtor (t);
3445 while (*access_decls)
3447 tree using_decl = TREE_VALUE (*access_decls);
3448 tree decl = USING_DECL_DECLS (using_decl);
3449 if (DECL_NAME (using_decl) == ctor_identifier)
3451 /* declare, then remove the decl */
3452 tree ctor_list = decl;
3453 location_t loc = input_location;
3454 input_location = DECL_SOURCE_LOCATION (using_decl);
3455 if (ctor_list)
3456 for (; ctor_list; ctor_list = OVL_NEXT (ctor_list))
3457 one_inherited_ctor (OVL_CURRENT (ctor_list), t, using_decl);
3458 *access_decls = TREE_CHAIN (*access_decls);
3459 input_location = loc;
3461 else
3462 access_decls = &TREE_CHAIN (*access_decls);
3466 /* Subroutine of insert_into_classtype_sorted_fields. Recursively
3467 count the number of fields in TYPE, including anonymous union
3468 members. */
3470 static int
3471 count_fields (tree fields)
3473 tree x;
3474 int n_fields = 0;
3475 for (x = fields; x; x = DECL_CHAIN (x))
3477 if (TREE_CODE (x) == FIELD_DECL && ANON_AGGR_TYPE_P (TREE_TYPE (x)))
3478 n_fields += count_fields (TYPE_FIELDS (TREE_TYPE (x)));
3479 else
3480 n_fields += 1;
3482 return n_fields;
3485 /* Subroutine of insert_into_classtype_sorted_fields. Recursively add
3486 all the fields in the TREE_LIST FIELDS to the SORTED_FIELDS_TYPE
3487 elts, starting at offset IDX. */
3489 static int
3490 add_fields_to_record_type (tree fields, struct sorted_fields_type *field_vec, int idx)
3492 tree x;
3493 for (x = fields; x; x = DECL_CHAIN (x))
3495 if (TREE_CODE (x) == FIELD_DECL && ANON_AGGR_TYPE_P (TREE_TYPE (x)))
3496 idx = add_fields_to_record_type (TYPE_FIELDS (TREE_TYPE (x)), field_vec, idx);
3497 else
3498 field_vec->elts[idx++] = x;
3500 return idx;
3503 /* Add all of the enum values of ENUMTYPE, to the FIELD_VEC elts,
3504 starting at offset IDX. */
3506 static int
3507 add_enum_fields_to_record_type (tree enumtype,
3508 struct sorted_fields_type *field_vec,
3509 int idx)
3511 tree values;
3512 for (values = TYPE_VALUES (enumtype); values; values = TREE_CHAIN (values))
3513 field_vec->elts[idx++] = TREE_VALUE (values);
3514 return idx;
3517 /* FIELD is a bit-field. We are finishing the processing for its
3518 enclosing type. Issue any appropriate messages and set appropriate
3519 flags. Returns false if an error has been diagnosed. */
3521 static bool
3522 check_bitfield_decl (tree field)
3524 tree type = TREE_TYPE (field);
3525 tree w;
3527 /* Extract the declared width of the bitfield, which has been
3528 temporarily stashed in DECL_INITIAL. */
3529 w = DECL_INITIAL (field);
3530 gcc_assert (w != NULL_TREE);
3531 /* Remove the bit-field width indicator so that the rest of the
3532 compiler does not treat that value as an initializer. */
3533 DECL_INITIAL (field) = NULL_TREE;
3535 /* Detect invalid bit-field type. */
3536 if (!INTEGRAL_OR_ENUMERATION_TYPE_P (type))
3538 error ("bit-field %q+#D with non-integral type", field);
3539 w = error_mark_node;
3541 else
3543 location_t loc = input_location;
3544 /* Avoid the non_lvalue wrapper added by fold for PLUS_EXPRs. */
3545 STRIP_NOPS (w);
3547 /* detect invalid field size. */
3548 input_location = DECL_SOURCE_LOCATION (field);
3549 w = cxx_constant_value (w);
3550 input_location = loc;
3552 if (TREE_CODE (w) != INTEGER_CST)
3554 error ("bit-field %q+D width not an integer constant", field);
3555 w = error_mark_node;
3557 else if (tree_int_cst_sgn (w) < 0)
3559 error ("negative width in bit-field %q+D", field);
3560 w = error_mark_node;
3562 else if (integer_zerop (w) && DECL_NAME (field) != 0)
3564 error ("zero width for bit-field %q+D", field);
3565 w = error_mark_node;
3567 else if ((TREE_CODE (type) != ENUMERAL_TYPE
3568 && TREE_CODE (type) != BOOLEAN_TYPE
3569 && compare_tree_int (w, TYPE_PRECISION (type)) > 0)
3570 || ((TREE_CODE (type) == ENUMERAL_TYPE
3571 || TREE_CODE (type) == BOOLEAN_TYPE)
3572 && tree_int_cst_lt (TYPE_SIZE (type), w)))
3573 warning_at (DECL_SOURCE_LOCATION (field), 0,
3574 "width of %qD exceeds its type", field);
3575 else if (TREE_CODE (type) == ENUMERAL_TYPE
3576 && (0 > (compare_tree_int
3577 (w, TYPE_PRECISION (ENUM_UNDERLYING_TYPE (type))))))
3578 warning_at (DECL_SOURCE_LOCATION (field), 0,
3579 "%qD is too small to hold all values of %q#T",
3580 field, type);
3583 if (w != error_mark_node)
3585 DECL_SIZE (field) = fold_convert (bitsizetype, w);
3586 DECL_BIT_FIELD (field) = 1;
3587 return true;
3589 else
3591 /* Non-bit-fields are aligned for their type. */
3592 DECL_BIT_FIELD (field) = 0;
3593 CLEAR_DECL_C_BIT_FIELD (field);
3594 return false;
3598 /* FIELD is a non bit-field. We are finishing the processing for its
3599 enclosing type T. Issue any appropriate messages and set appropriate
3600 flags. */
3602 static bool
3603 check_field_decl (tree field,
3604 tree t,
3605 int* cant_have_const_ctor,
3606 int* no_const_asn_ref)
3608 tree type = strip_array_types (TREE_TYPE (field));
3609 bool any_default_members = false;
3611 /* In C++98 an anonymous union cannot contain any fields which would change
3612 the settings of CANT_HAVE_CONST_CTOR and friends. */
3613 if (ANON_UNION_TYPE_P (type) && cxx_dialect < cxx11)
3615 /* And, we don't set TYPE_HAS_CONST_COPY_CTOR, etc., for anonymous
3616 structs. So, we recurse through their fields here. */
3617 else if (ANON_AGGR_TYPE_P (type))
3619 for (tree fields = TYPE_FIELDS (type); fields;
3620 fields = DECL_CHAIN (fields))
3621 if (TREE_CODE (fields) == FIELD_DECL && !DECL_C_BIT_FIELD (field))
3622 any_default_members |= check_field_decl (fields, t,
3623 cant_have_const_ctor,
3624 no_const_asn_ref);
3626 /* Check members with class type for constructors, destructors,
3627 etc. */
3628 else if (CLASS_TYPE_P (type))
3630 /* Never let anything with uninheritable virtuals
3631 make it through without complaint. */
3632 abstract_virtuals_error (field, type);
3634 if (TREE_CODE (t) == UNION_TYPE && cxx_dialect < cxx11)
3636 static bool warned;
3637 int oldcount = errorcount;
3638 if (TYPE_NEEDS_CONSTRUCTING (type))
3639 error ("member %q+#D with constructor not allowed in union",
3640 field);
3641 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
3642 error ("member %q+#D with destructor not allowed in union", field);
3643 if (TYPE_HAS_COMPLEX_COPY_ASSIGN (type))
3644 error ("member %q+#D with copy assignment operator not allowed in union",
3645 field);
3646 if (!warned && errorcount > oldcount)
3648 inform (DECL_SOURCE_LOCATION (field), "unrestricted unions "
3649 "only available with -std=c++11 or -std=gnu++11");
3650 warned = true;
3653 else
3655 TYPE_NEEDS_CONSTRUCTING (t) |= TYPE_NEEDS_CONSTRUCTING (type);
3656 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t)
3657 |= TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type);
3658 TYPE_HAS_COMPLEX_COPY_ASSIGN (t)
3659 |= (TYPE_HAS_COMPLEX_COPY_ASSIGN (type)
3660 || !TYPE_HAS_COPY_ASSIGN (type));
3661 TYPE_HAS_COMPLEX_COPY_CTOR (t) |= (TYPE_HAS_COMPLEX_COPY_CTOR (type)
3662 || !TYPE_HAS_COPY_CTOR (type));
3663 TYPE_HAS_COMPLEX_MOVE_ASSIGN (t) |= TYPE_HAS_COMPLEX_MOVE_ASSIGN (type);
3664 TYPE_HAS_COMPLEX_MOVE_CTOR (t) |= TYPE_HAS_COMPLEX_MOVE_CTOR (type);
3665 TYPE_HAS_COMPLEX_DFLT (t) |= (!TYPE_HAS_DEFAULT_CONSTRUCTOR (type)
3666 || TYPE_HAS_COMPLEX_DFLT (type));
3669 if (TYPE_HAS_COPY_CTOR (type)
3670 && !TYPE_HAS_CONST_COPY_CTOR (type))
3671 *cant_have_const_ctor = 1;
3673 if (TYPE_HAS_COPY_ASSIGN (type)
3674 && !TYPE_HAS_CONST_COPY_ASSIGN (type))
3675 *no_const_asn_ref = 1;
3678 check_abi_tags (t, field);
3680 if (DECL_INITIAL (field) != NULL_TREE)
3681 /* `build_class_init_list' does not recognize
3682 non-FIELD_DECLs. */
3683 any_default_members = true;
3685 return any_default_members;
3688 /* Check the data members (both static and non-static), class-scoped
3689 typedefs, etc., appearing in the declaration of T. Issue
3690 appropriate diagnostics. Sets ACCESS_DECLS to a list (in
3691 declaration order) of access declarations; each TREE_VALUE in this
3692 list is a USING_DECL.
3694 In addition, set the following flags:
3696 EMPTY_P
3697 The class is empty, i.e., contains no non-static data members.
3699 CANT_HAVE_CONST_CTOR_P
3700 This class cannot have an implicitly generated copy constructor
3701 taking a const reference.
3703 CANT_HAVE_CONST_ASN_REF
3704 This class cannot have an implicitly generated assignment
3705 operator taking a const reference.
3707 All of these flags should be initialized before calling this
3708 function.
3710 Returns a pointer to the end of the TYPE_FIELDs chain; additional
3711 fields can be added by adding to this chain. */
3713 static void
3714 check_field_decls (tree t, tree *access_decls,
3715 int *cant_have_const_ctor_p,
3716 int *no_const_asn_ref_p)
3718 tree *field;
3719 tree *next;
3720 bool has_pointers;
3721 bool any_default_members;
3722 int cant_pack = 0;
3723 int field_access = -1;
3725 /* Assume there are no access declarations. */
3726 *access_decls = NULL_TREE;
3727 /* Assume this class has no pointer members. */
3728 has_pointers = false;
3729 /* Assume none of the members of this class have default
3730 initializations. */
3731 any_default_members = false;
3733 for (field = &TYPE_FIELDS (t); *field; field = next)
3735 tree x = *field;
3736 tree type = TREE_TYPE (x);
3737 int this_field_access;
3739 next = &DECL_CHAIN (x);
3741 if (TREE_CODE (x) == USING_DECL)
3743 /* Save the access declarations for our caller. */
3744 *access_decls = tree_cons (NULL_TREE, x, *access_decls);
3745 continue;
3748 if (TREE_CODE (x) == TYPE_DECL
3749 || TREE_CODE (x) == TEMPLATE_DECL)
3750 continue;
3752 /* If we've gotten this far, it's a data member, possibly static,
3753 or an enumerator. */
3754 if (TREE_CODE (x) != CONST_DECL)
3755 DECL_CONTEXT (x) = t;
3757 /* When this goes into scope, it will be a non-local reference. */
3758 DECL_NONLOCAL (x) = 1;
3760 if (TREE_CODE (t) == UNION_TYPE)
3762 /* [class.union] (C++98)
3764 If a union contains a static data member, or a member of
3765 reference type, the program is ill-formed.
3767 In C++11 [class.union] says:
3768 If a union contains a non-static data member of reference type
3769 the program is ill-formed. */
3770 if (VAR_P (x) && cxx_dialect < cxx11)
3772 error ("in C++98 %q+D may not be static because it is "
3773 "a member of a union", x);
3774 continue;
3776 if (TREE_CODE (type) == REFERENCE_TYPE
3777 && TREE_CODE (x) == FIELD_DECL)
3779 error ("non-static data member %q+D in a union may not "
3780 "have reference type %qT", x, type);
3781 continue;
3785 /* Perform error checking that did not get done in
3786 grokdeclarator. */
3787 if (TREE_CODE (type) == FUNCTION_TYPE)
3789 error ("field %q+D invalidly declared function type", x);
3790 type = build_pointer_type (type);
3791 TREE_TYPE (x) = type;
3793 else if (TREE_CODE (type) == METHOD_TYPE)
3795 error ("field %q+D invalidly declared method type", x);
3796 type = build_pointer_type (type);
3797 TREE_TYPE (x) = type;
3800 if (type == error_mark_node)
3801 continue;
3803 if (TREE_CODE (x) == CONST_DECL || VAR_P (x))
3804 continue;
3806 /* Now it can only be a FIELD_DECL. */
3808 if (TREE_PRIVATE (x) || TREE_PROTECTED (x))
3809 CLASSTYPE_NON_AGGREGATE (t) = 1;
3811 /* If at least one non-static data member is non-literal, the whole
3812 class becomes non-literal. Per Core/1453, volatile non-static
3813 data members and base classes are also not allowed.
3814 Note: if the type is incomplete we will complain later on. */
3815 if (COMPLETE_TYPE_P (type)
3816 && (!literal_type_p (type) || CP_TYPE_VOLATILE_P (type)))
3817 CLASSTYPE_LITERAL_P (t) = false;
3819 /* A standard-layout class is a class that:
3821 has the same access control (Clause 11) for all non-static data members,
3822 ... */
3823 this_field_access = TREE_PROTECTED (x) ? 1 : TREE_PRIVATE (x) ? 2 : 0;
3824 if (field_access == -1)
3825 field_access = this_field_access;
3826 else if (this_field_access != field_access)
3827 CLASSTYPE_NON_STD_LAYOUT (t) = 1;
3829 /* If this is of reference type, check if it needs an init. */
3830 if (TREE_CODE (type) == REFERENCE_TYPE)
3832 CLASSTYPE_NON_LAYOUT_POD_P (t) = 1;
3833 CLASSTYPE_NON_STD_LAYOUT (t) = 1;
3834 if (DECL_INITIAL (x) == NULL_TREE)
3835 SET_CLASSTYPE_REF_FIELDS_NEED_INIT (t, 1);
3836 if (cxx_dialect < cxx11)
3838 /* ARM $12.6.2: [A member initializer list] (or, for an
3839 aggregate, initialization by a brace-enclosed list) is the
3840 only way to initialize nonstatic const and reference
3841 members. */
3842 TYPE_HAS_COMPLEX_COPY_ASSIGN (t) = 1;
3843 TYPE_HAS_COMPLEX_MOVE_ASSIGN (t) = 1;
3847 type = strip_array_types (type);
3849 if (TYPE_PACKED (t))
3851 if (!layout_pod_type_p (type) && !TYPE_PACKED (type))
3853 warning_at
3854 (DECL_SOURCE_LOCATION (x), 0,
3855 "ignoring packed attribute because of unpacked non-POD field %q#D",
3857 cant_pack = 1;
3859 else if (DECL_C_BIT_FIELD (x)
3860 || TYPE_ALIGN (TREE_TYPE (x)) > BITS_PER_UNIT)
3861 DECL_PACKED (x) = 1;
3864 if (DECL_C_BIT_FIELD (x) && integer_zerop (DECL_INITIAL (x)))
3865 /* We don't treat zero-width bitfields as making a class
3866 non-empty. */
3868 else
3870 /* The class is non-empty. */
3871 CLASSTYPE_EMPTY_P (t) = 0;
3872 /* The class is not even nearly empty. */
3873 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
3874 /* If one of the data members contains an empty class,
3875 so does T. */
3876 if (CLASS_TYPE_P (type)
3877 && CLASSTYPE_CONTAINS_EMPTY_CLASS_P (type))
3878 CLASSTYPE_CONTAINS_EMPTY_CLASS_P (t) = 1;
3881 /* This is used by -Weffc++ (see below). Warn only for pointers
3882 to members which might hold dynamic memory. So do not warn
3883 for pointers to functions or pointers to members. */
3884 if (TYPE_PTR_P (type)
3885 && !TYPE_PTRFN_P (type))
3886 has_pointers = true;
3888 if (CLASS_TYPE_P (type))
3890 if (CLASSTYPE_REF_FIELDS_NEED_INIT (type))
3891 SET_CLASSTYPE_REF_FIELDS_NEED_INIT (t, 1);
3892 if (CLASSTYPE_READONLY_FIELDS_NEED_INIT (type))
3893 SET_CLASSTYPE_READONLY_FIELDS_NEED_INIT (t, 1);
3896 if (DECL_MUTABLE_P (x) || TYPE_HAS_MUTABLE_P (type))
3897 CLASSTYPE_HAS_MUTABLE (t) = 1;
3899 if (DECL_MUTABLE_P (x))
3901 if (CP_TYPE_CONST_P (type))
3903 error ("member %q+D cannot be declared both %<const%> "
3904 "and %<mutable%>", x);
3905 continue;
3907 if (TREE_CODE (type) == REFERENCE_TYPE)
3909 error ("member %q+D cannot be declared as a %<mutable%> "
3910 "reference", x);
3911 continue;
3915 if (! layout_pod_type_p (type))
3916 /* DR 148 now allows pointers to members (which are POD themselves),
3917 to be allowed in POD structs. */
3918 CLASSTYPE_NON_LAYOUT_POD_P (t) = 1;
3920 if (!std_layout_type_p (type))
3921 CLASSTYPE_NON_STD_LAYOUT (t) = 1;
3923 if (! zero_init_p (type))
3924 CLASSTYPE_NON_ZERO_INIT_P (t) = 1;
3926 /* We set DECL_C_BIT_FIELD in grokbitfield.
3927 If the type and width are valid, we'll also set DECL_BIT_FIELD. */
3928 if ((! DECL_C_BIT_FIELD (x) || ! check_bitfield_decl (x))
3929 && check_field_decl (x, t,
3930 cant_have_const_ctor_p,
3931 no_const_asn_ref_p))
3933 if (any_default_members
3934 && TREE_CODE (t) == UNION_TYPE)
3935 error ("multiple fields in union %qT initialized", t);
3936 any_default_members = true;
3939 /* Now that we've removed bit-field widths from DECL_INITIAL,
3940 anything left in DECL_INITIAL is an NSDMI that makes the class
3941 non-aggregate in C++11. */
3942 if (DECL_INITIAL (x) && cxx_dialect < cxx14)
3943 CLASSTYPE_NON_AGGREGATE (t) = true;
3945 /* If any field is const, the structure type is pseudo-const. */
3946 if (CP_TYPE_CONST_P (type))
3948 C_TYPE_FIELDS_READONLY (t) = 1;
3949 if (DECL_INITIAL (x) == NULL_TREE)
3950 SET_CLASSTYPE_READONLY_FIELDS_NEED_INIT (t, 1);
3951 if (cxx_dialect < cxx11)
3953 /* ARM $12.6.2: [A member initializer list] (or, for an
3954 aggregate, initialization by a brace-enclosed list) is the
3955 only way to initialize nonstatic const and reference
3956 members. */
3957 TYPE_HAS_COMPLEX_COPY_ASSIGN (t) = 1;
3958 TYPE_HAS_COMPLEX_MOVE_ASSIGN (t) = 1;
3961 /* A field that is pseudo-const makes the structure likewise. */
3962 else if (CLASS_TYPE_P (type))
3964 C_TYPE_FIELDS_READONLY (t) |= C_TYPE_FIELDS_READONLY (type);
3965 SET_CLASSTYPE_READONLY_FIELDS_NEED_INIT (t,
3966 CLASSTYPE_READONLY_FIELDS_NEED_INIT (t)
3967 | CLASSTYPE_READONLY_FIELDS_NEED_INIT (type));
3970 /* Core issue 80: A nonstatic data member is required to have a
3971 different name from the class iff the class has a
3972 user-declared constructor. */
3973 if (constructor_name_p (DECL_NAME (x), t)
3974 && TYPE_HAS_USER_CONSTRUCTOR (t))
3975 permerror (DECL_SOURCE_LOCATION (x),
3976 "field %q#D with same name as class", x);
3979 /* Effective C++ rule 11: if a class has dynamic memory held by pointers,
3980 it should also define a copy constructor and an assignment operator to
3981 implement the correct copy semantic (deep vs shallow, etc.). As it is
3982 not feasible to check whether the constructors do allocate dynamic memory
3983 and store it within members, we approximate the warning like this:
3985 -- Warn only if there are members which are pointers
3986 -- Warn only if there is a non-trivial constructor (otherwise,
3987 there cannot be memory allocated).
3988 -- Warn only if there is a non-trivial destructor. We assume that the
3989 user at least implemented the cleanup correctly, and a destructor
3990 is needed to free dynamic memory.
3992 This seems enough for practical purposes. */
3993 if (warn_ecpp
3994 && has_pointers
3995 && TYPE_HAS_USER_CONSTRUCTOR (t)
3996 && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t)
3997 && !(TYPE_HAS_COPY_CTOR (t) && TYPE_HAS_COPY_ASSIGN (t)))
3999 warning (OPT_Weffc__, "%q#T has pointer data members", t);
4001 if (! TYPE_HAS_COPY_CTOR (t))
4003 warning (OPT_Weffc__,
4004 " but does not override %<%T(const %T&)%>", t, t);
4005 if (!TYPE_HAS_COPY_ASSIGN (t))
4006 warning (OPT_Weffc__, " or %<operator=(const %T&)%>", t);
4008 else if (! TYPE_HAS_COPY_ASSIGN (t))
4009 warning (OPT_Weffc__,
4010 " but does not override %<operator=(const %T&)%>", t);
4013 /* Non-static data member initializers make the default constructor
4014 non-trivial. */
4015 if (any_default_members)
4017 TYPE_NEEDS_CONSTRUCTING (t) = true;
4018 TYPE_HAS_COMPLEX_DFLT (t) = true;
4021 /* If any of the fields couldn't be packed, unset TYPE_PACKED. */
4022 if (cant_pack)
4023 TYPE_PACKED (t) = 0;
4025 /* Check anonymous struct/anonymous union fields. */
4026 finish_struct_anon (t);
4028 /* We've built up the list of access declarations in reverse order.
4029 Fix that now. */
4030 *access_decls = nreverse (*access_decls);
4033 /* If TYPE is an empty class type, records its OFFSET in the table of
4034 OFFSETS. */
4036 static int
4037 record_subobject_offset (tree type, tree offset, splay_tree offsets)
4039 splay_tree_node n;
4041 if (!is_empty_class (type))
4042 return 0;
4044 /* Record the location of this empty object in OFFSETS. */
4045 n = splay_tree_lookup (offsets, (splay_tree_key) offset);
4046 if (!n)
4047 n = splay_tree_insert (offsets,
4048 (splay_tree_key) offset,
4049 (splay_tree_value) NULL_TREE);
4050 n->value = ((splay_tree_value)
4051 tree_cons (NULL_TREE,
4052 type,
4053 (tree) n->value));
4055 return 0;
4058 /* Returns nonzero if TYPE is an empty class type and there is
4059 already an entry in OFFSETS for the same TYPE as the same OFFSET. */
4061 static int
4062 check_subobject_offset (tree type, tree offset, splay_tree offsets)
4064 splay_tree_node n;
4065 tree t;
4067 if (!is_empty_class (type))
4068 return 0;
4070 /* Record the location of this empty object in OFFSETS. */
4071 n = splay_tree_lookup (offsets, (splay_tree_key) offset);
4072 if (!n)
4073 return 0;
4075 for (t = (tree) n->value; t; t = TREE_CHAIN (t))
4076 if (same_type_p (TREE_VALUE (t), type))
4077 return 1;
4079 return 0;
4082 /* Walk through all the subobjects of TYPE (located at OFFSET). Call
4083 F for every subobject, passing it the type, offset, and table of
4084 OFFSETS. If VBASES_P is one, then virtual non-primary bases should
4085 be traversed.
4087 If MAX_OFFSET is non-NULL, then subobjects with an offset greater
4088 than MAX_OFFSET will not be walked.
4090 If F returns a nonzero value, the traversal ceases, and that value
4091 is returned. Otherwise, returns zero. */
4093 static int
4094 walk_subobject_offsets (tree type,
4095 subobject_offset_fn f,
4096 tree offset,
4097 splay_tree offsets,
4098 tree max_offset,
4099 int vbases_p)
4101 int r = 0;
4102 tree type_binfo = NULL_TREE;
4104 /* If this OFFSET is bigger than the MAX_OFFSET, then we should
4105 stop. */
4106 if (max_offset && tree_int_cst_lt (max_offset, offset))
4107 return 0;
4109 if (type == error_mark_node)
4110 return 0;
4112 if (!TYPE_P (type))
4114 type_binfo = type;
4115 type = BINFO_TYPE (type);
4118 if (CLASS_TYPE_P (type))
4120 tree field;
4121 tree binfo;
4122 int i;
4124 /* Avoid recursing into objects that are not interesting. */
4125 if (!CLASSTYPE_CONTAINS_EMPTY_CLASS_P (type))
4126 return 0;
4128 /* Record the location of TYPE. */
4129 r = (*f) (type, offset, offsets);
4130 if (r)
4131 return r;
4133 /* Iterate through the direct base classes of TYPE. */
4134 if (!type_binfo)
4135 type_binfo = TYPE_BINFO (type);
4136 for (i = 0; BINFO_BASE_ITERATE (type_binfo, i, binfo); i++)
4138 tree binfo_offset;
4140 if (BINFO_VIRTUAL_P (binfo))
4141 continue;
4143 tree orig_binfo;
4144 /* We cannot rely on BINFO_OFFSET being set for the base
4145 class yet, but the offsets for direct non-virtual
4146 bases can be calculated by going back to the TYPE. */
4147 orig_binfo = BINFO_BASE_BINFO (TYPE_BINFO (type), i);
4148 binfo_offset = size_binop (PLUS_EXPR,
4149 offset,
4150 BINFO_OFFSET (orig_binfo));
4152 r = walk_subobject_offsets (binfo,
4154 binfo_offset,
4155 offsets,
4156 max_offset,
4157 /*vbases_p=*/0);
4158 if (r)
4159 return r;
4162 if (CLASSTYPE_VBASECLASSES (type))
4164 unsigned ix;
4165 vec<tree, va_gc> *vbases;
4167 /* Iterate through the virtual base classes of TYPE. In G++
4168 3.2, we included virtual bases in the direct base class
4169 loop above, which results in incorrect results; the
4170 correct offsets for virtual bases are only known when
4171 working with the most derived type. */
4172 if (vbases_p)
4173 for (vbases = CLASSTYPE_VBASECLASSES (type), ix = 0;
4174 vec_safe_iterate (vbases, ix, &binfo); ix++)
4176 r = walk_subobject_offsets (binfo,
4178 size_binop (PLUS_EXPR,
4179 offset,
4180 BINFO_OFFSET (binfo)),
4181 offsets,
4182 max_offset,
4183 /*vbases_p=*/0);
4184 if (r)
4185 return r;
4187 else
4189 /* We still have to walk the primary base, if it is
4190 virtual. (If it is non-virtual, then it was walked
4191 above.) */
4192 tree vbase = get_primary_binfo (type_binfo);
4194 if (vbase && BINFO_VIRTUAL_P (vbase)
4195 && BINFO_PRIMARY_P (vbase)
4196 && BINFO_INHERITANCE_CHAIN (vbase) == type_binfo)
4198 r = (walk_subobject_offsets
4199 (vbase, f, offset,
4200 offsets, max_offset, /*vbases_p=*/0));
4201 if (r)
4202 return r;
4207 /* Iterate through the fields of TYPE. */
4208 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
4209 if (TREE_CODE (field) == FIELD_DECL
4210 && TREE_TYPE (field) != error_mark_node
4211 && !DECL_ARTIFICIAL (field))
4213 tree field_offset;
4215 field_offset = byte_position (field);
4217 r = walk_subobject_offsets (TREE_TYPE (field),
4219 size_binop (PLUS_EXPR,
4220 offset,
4221 field_offset),
4222 offsets,
4223 max_offset,
4224 /*vbases_p=*/1);
4225 if (r)
4226 return r;
4229 else if (TREE_CODE (type) == ARRAY_TYPE)
4231 tree element_type = strip_array_types (type);
4232 tree domain = TYPE_DOMAIN (type);
4233 tree index;
4235 /* Avoid recursing into objects that are not interesting. */
4236 if (!CLASS_TYPE_P (element_type)
4237 || !CLASSTYPE_CONTAINS_EMPTY_CLASS_P (element_type)
4238 || !domain
4239 || integer_minus_onep (TYPE_MAX_VALUE (domain)))
4240 return 0;
4242 /* Step through each of the elements in the array. */
4243 for (index = size_zero_node;
4244 !tree_int_cst_lt (TYPE_MAX_VALUE (domain), index);
4245 index = size_binop (PLUS_EXPR, index, size_one_node))
4247 r = walk_subobject_offsets (TREE_TYPE (type),
4249 offset,
4250 offsets,
4251 max_offset,
4252 /*vbases_p=*/1);
4253 if (r)
4254 return r;
4255 offset = size_binop (PLUS_EXPR, offset,
4256 TYPE_SIZE_UNIT (TREE_TYPE (type)));
4257 /* If this new OFFSET is bigger than the MAX_OFFSET, then
4258 there's no point in iterating through the remaining
4259 elements of the array. */
4260 if (max_offset && tree_int_cst_lt (max_offset, offset))
4261 break;
4265 return 0;
4268 /* Record all of the empty subobjects of TYPE (either a type or a
4269 binfo). If IS_DATA_MEMBER is true, then a non-static data member
4270 is being placed at OFFSET; otherwise, it is a base class that is
4271 being placed at OFFSET. */
4273 static void
4274 record_subobject_offsets (tree type,
4275 tree offset,
4276 splay_tree offsets,
4277 bool is_data_member)
4279 tree max_offset;
4280 /* If recording subobjects for a non-static data member or a
4281 non-empty base class , we do not need to record offsets beyond
4282 the size of the biggest empty class. Additional data members
4283 will go at the end of the class. Additional base classes will go
4284 either at offset zero (if empty, in which case they cannot
4285 overlap with offsets past the size of the biggest empty class) or
4286 at the end of the class.
4288 However, if we are placing an empty base class, then we must record
4289 all offsets, as either the empty class is at offset zero (where
4290 other empty classes might later be placed) or at the end of the
4291 class (where other objects might then be placed, so other empty
4292 subobjects might later overlap). */
4293 if (is_data_member
4294 || !is_empty_class (BINFO_TYPE (type)))
4295 max_offset = sizeof_biggest_empty_class;
4296 else
4297 max_offset = NULL_TREE;
4298 walk_subobject_offsets (type, record_subobject_offset, offset,
4299 offsets, max_offset, is_data_member);
4302 /* Returns nonzero if any of the empty subobjects of TYPE (located at
4303 OFFSET) conflict with entries in OFFSETS. If VBASES_P is nonzero,
4304 virtual bases of TYPE are examined. */
4306 static int
4307 layout_conflict_p (tree type,
4308 tree offset,
4309 splay_tree offsets,
4310 int vbases_p)
4312 splay_tree_node max_node;
4314 /* Get the node in OFFSETS that indicates the maximum offset where
4315 an empty subobject is located. */
4316 max_node = splay_tree_max (offsets);
4317 /* If there aren't any empty subobjects, then there's no point in
4318 performing this check. */
4319 if (!max_node)
4320 return 0;
4322 return walk_subobject_offsets (type, check_subobject_offset, offset,
4323 offsets, (tree) (max_node->key),
4324 vbases_p);
4327 /* DECL is a FIELD_DECL corresponding either to a base subobject of a
4328 non-static data member of the type indicated by RLI. BINFO is the
4329 binfo corresponding to the base subobject, OFFSETS maps offsets to
4330 types already located at those offsets. This function determines
4331 the position of the DECL. */
4333 static void
4334 layout_nonempty_base_or_field (record_layout_info rli,
4335 tree decl,
4336 tree binfo,
4337 splay_tree offsets)
4339 tree offset = NULL_TREE;
4340 bool field_p;
4341 tree type;
4343 if (binfo)
4345 /* For the purposes of determining layout conflicts, we want to
4346 use the class type of BINFO; TREE_TYPE (DECL) will be the
4347 CLASSTYPE_AS_BASE version, which does not contain entries for
4348 zero-sized bases. */
4349 type = TREE_TYPE (binfo);
4350 field_p = false;
4352 else
4354 type = TREE_TYPE (decl);
4355 field_p = true;
4358 /* Try to place the field. It may take more than one try if we have
4359 a hard time placing the field without putting two objects of the
4360 same type at the same address. */
4361 while (1)
4363 struct record_layout_info_s old_rli = *rli;
4365 /* Place this field. */
4366 place_field (rli, decl);
4367 offset = byte_position (decl);
4369 /* We have to check to see whether or not there is already
4370 something of the same type at the offset we're about to use.
4371 For example, consider:
4373 struct S {};
4374 struct T : public S { int i; };
4375 struct U : public S, public T {};
4377 Here, we put S at offset zero in U. Then, we can't put T at
4378 offset zero -- its S component would be at the same address
4379 as the S we already allocated. So, we have to skip ahead.
4380 Since all data members, including those whose type is an
4381 empty class, have nonzero size, any overlap can happen only
4382 with a direct or indirect base-class -- it can't happen with
4383 a data member. */
4384 /* In a union, overlap is permitted; all members are placed at
4385 offset zero. */
4386 if (TREE_CODE (rli->t) == UNION_TYPE)
4387 break;
4388 if (layout_conflict_p (field_p ? type : binfo, offset,
4389 offsets, field_p))
4391 /* Strip off the size allocated to this field. That puts us
4392 at the first place we could have put the field with
4393 proper alignment. */
4394 *rli = old_rli;
4396 /* Bump up by the alignment required for the type. */
4397 rli->bitpos
4398 = size_binop (PLUS_EXPR, rli->bitpos,
4399 bitsize_int (binfo
4400 ? CLASSTYPE_ALIGN (type)
4401 : TYPE_ALIGN (type)));
4402 normalize_rli (rli);
4404 else if (TREE_CODE (type) == NULLPTR_TYPE
4405 && warn_abi && abi_version_crosses (9))
4407 /* Before ABI v9, we were giving nullptr_t alignment of 1; if
4408 the offset wasn't aligned like a pointer when we started to
4409 layout this field, that affects its position. */
4410 tree pos = rli_size_unit_so_far (&old_rli);
4411 if (int_cst_value (pos) % TYPE_ALIGN_UNIT (ptr_type_node) != 0)
4413 if (abi_version_at_least (9))
4414 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wabi,
4415 "alignment of %qD increased in -fabi-version=9 "
4416 "(GCC 5.2)", decl);
4417 else
4418 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wabi, "alignment "
4419 "of %qD will increase in -fabi-version=9", decl);
4421 break;
4423 else
4424 /* There was no conflict. We're done laying out this field. */
4425 break;
4428 /* Now that we know where it will be placed, update its
4429 BINFO_OFFSET. */
4430 if (binfo && CLASS_TYPE_P (BINFO_TYPE (binfo)))
4431 /* Indirect virtual bases may have a nonzero BINFO_OFFSET at
4432 this point because their BINFO_OFFSET is copied from another
4433 hierarchy. Therefore, we may not need to add the entire
4434 OFFSET. */
4435 propagate_binfo_offsets (binfo,
4436 size_diffop_loc (input_location,
4437 fold_convert (ssizetype, offset),
4438 fold_convert (ssizetype,
4439 BINFO_OFFSET (binfo))));
4442 /* Returns true if TYPE is empty and OFFSET is nonzero. */
4444 static int
4445 empty_base_at_nonzero_offset_p (tree type,
4446 tree offset,
4447 splay_tree /*offsets*/)
4449 return is_empty_class (type) && !integer_zerop (offset);
4452 /* Layout the empty base BINFO. EOC indicates the byte currently just
4453 past the end of the class, and should be correctly aligned for a
4454 class of the type indicated by BINFO; OFFSETS gives the offsets of
4455 the empty bases allocated so far. T is the most derived
4456 type. Return nonzero iff we added it at the end. */
4458 static bool
4459 layout_empty_base (record_layout_info rli, tree binfo,
4460 tree eoc, splay_tree offsets)
4462 tree alignment;
4463 tree basetype = BINFO_TYPE (binfo);
4464 bool atend = false;
4466 /* This routine should only be used for empty classes. */
4467 gcc_assert (is_empty_class (basetype));
4468 alignment = ssize_int (CLASSTYPE_ALIGN_UNIT (basetype));
4470 if (!integer_zerop (BINFO_OFFSET (binfo)))
4471 propagate_binfo_offsets
4472 (binfo, size_diffop_loc (input_location,
4473 size_zero_node, BINFO_OFFSET (binfo)));
4475 /* This is an empty base class. We first try to put it at offset
4476 zero. */
4477 if (layout_conflict_p (binfo,
4478 BINFO_OFFSET (binfo),
4479 offsets,
4480 /*vbases_p=*/0))
4482 /* That didn't work. Now, we move forward from the next
4483 available spot in the class. */
4484 atend = true;
4485 propagate_binfo_offsets (binfo, fold_convert (ssizetype, eoc));
4486 while (1)
4488 if (!layout_conflict_p (binfo,
4489 BINFO_OFFSET (binfo),
4490 offsets,
4491 /*vbases_p=*/0))
4492 /* We finally found a spot where there's no overlap. */
4493 break;
4495 /* There's overlap here, too. Bump along to the next spot. */
4496 propagate_binfo_offsets (binfo, alignment);
4500 if (CLASSTYPE_USER_ALIGN (basetype))
4502 rli->record_align = MAX (rli->record_align, CLASSTYPE_ALIGN (basetype));
4503 if (warn_packed)
4504 rli->unpacked_align = MAX (rli->unpacked_align, CLASSTYPE_ALIGN (basetype));
4505 TYPE_USER_ALIGN (rli->t) = 1;
4508 return atend;
4511 /* Build the FIELD_DECL for BASETYPE as a base of T, add it to the chain of
4512 fields at NEXT_FIELD, and return it. */
4514 static tree
4515 build_base_field_1 (tree t, tree basetype, tree *&next_field)
4517 /* Create the FIELD_DECL. */
4518 gcc_assert (CLASSTYPE_AS_BASE (basetype));
4519 tree decl = build_decl (input_location,
4520 FIELD_DECL, NULL_TREE, CLASSTYPE_AS_BASE (basetype));
4521 DECL_ARTIFICIAL (decl) = 1;
4522 DECL_IGNORED_P (decl) = 1;
4523 DECL_FIELD_CONTEXT (decl) = t;
4524 DECL_SIZE (decl) = CLASSTYPE_SIZE (basetype);
4525 DECL_SIZE_UNIT (decl) = CLASSTYPE_SIZE_UNIT (basetype);
4526 SET_DECL_ALIGN (decl, CLASSTYPE_ALIGN (basetype));
4527 DECL_USER_ALIGN (decl) = CLASSTYPE_USER_ALIGN (basetype);
4528 SET_DECL_MODE (decl, TYPE_MODE (basetype));
4529 DECL_FIELD_IS_BASE (decl) = 1;
4531 /* Add the new FIELD_DECL to the list of fields for T. */
4532 DECL_CHAIN (decl) = *next_field;
4533 *next_field = decl;
4534 next_field = &DECL_CHAIN (decl);
4536 return decl;
4539 /* Layout the base given by BINFO in the class indicated by RLI.
4540 *BASE_ALIGN is a running maximum of the alignments of
4541 any base class. OFFSETS gives the location of empty base
4542 subobjects. T is the most derived type. Return nonzero if the new
4543 object cannot be nearly-empty. A new FIELD_DECL is inserted at
4544 *NEXT_FIELD, unless BINFO is for an empty base class.
4546 Returns the location at which the next field should be inserted. */
4548 static tree *
4549 build_base_field (record_layout_info rli, tree binfo,
4550 splay_tree offsets, tree *next_field)
4552 tree t = rli->t;
4553 tree basetype = BINFO_TYPE (binfo);
4555 if (!COMPLETE_TYPE_P (basetype))
4556 /* This error is now reported in xref_tag, thus giving better
4557 location information. */
4558 return next_field;
4560 /* Place the base class. */
4561 if (!is_empty_class (basetype))
4563 tree decl;
4565 /* The containing class is non-empty because it has a non-empty
4566 base class. */
4567 CLASSTYPE_EMPTY_P (t) = 0;
4569 /* Create the FIELD_DECL. */
4570 decl = build_base_field_1 (t, basetype, next_field);
4572 /* Try to place the field. It may take more than one try if we
4573 have a hard time placing the field without putting two
4574 objects of the same type at the same address. */
4575 layout_nonempty_base_or_field (rli, decl, binfo, offsets);
4577 else
4579 tree eoc;
4580 bool atend;
4582 /* On some platforms (ARM), even empty classes will not be
4583 byte-aligned. */
4584 eoc = round_up_loc (input_location,
4585 rli_size_unit_so_far (rli),
4586 CLASSTYPE_ALIGN_UNIT (basetype));
4587 atend = layout_empty_base (rli, binfo, eoc, offsets);
4588 /* A nearly-empty class "has no proper base class that is empty,
4589 not morally virtual, and at an offset other than zero." */
4590 if (!BINFO_VIRTUAL_P (binfo) && CLASSTYPE_NEARLY_EMPTY_P (t))
4592 if (atend)
4593 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
4594 /* The check above (used in G++ 3.2) is insufficient because
4595 an empty class placed at offset zero might itself have an
4596 empty base at a nonzero offset. */
4597 else if (walk_subobject_offsets (basetype,
4598 empty_base_at_nonzero_offset_p,
4599 size_zero_node,
4600 /*offsets=*/NULL,
4601 /*max_offset=*/NULL_TREE,
4602 /*vbases_p=*/true))
4603 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
4606 /* We used to not create a FIELD_DECL for empty base classes because of
4607 back end issues with overlapping FIELD_DECLs, but that doesn't seem to
4608 be a problem anymore. We need them to handle initialization of C++17
4609 aggregate bases. */
4610 if (cxx_dialect >= cxx1z && !BINFO_VIRTUAL_P (binfo))
4612 tree decl = build_base_field_1 (t, basetype, next_field);
4613 DECL_FIELD_OFFSET (decl) = BINFO_OFFSET (binfo);
4614 DECL_FIELD_BIT_OFFSET (decl) = bitsize_zero_node;
4615 SET_DECL_OFFSET_ALIGN (decl, BITS_PER_UNIT);
4618 /* An empty virtual base causes a class to be non-empty
4619 -- but in that case we do not need to clear CLASSTYPE_EMPTY_P
4620 here because that was already done when the virtual table
4621 pointer was created. */
4624 /* Record the offsets of BINFO and its base subobjects. */
4625 record_subobject_offsets (binfo,
4626 BINFO_OFFSET (binfo),
4627 offsets,
4628 /*is_data_member=*/false);
4630 return next_field;
4633 /* Layout all of the non-virtual base classes. Record empty
4634 subobjects in OFFSETS. T is the most derived type. Return nonzero
4635 if the type cannot be nearly empty. The fields created
4636 corresponding to the base classes will be inserted at
4637 *NEXT_FIELD. */
4639 static void
4640 build_base_fields (record_layout_info rli,
4641 splay_tree offsets, tree *next_field)
4643 /* Chain to hold all the new FIELD_DECLs which stand in for base class
4644 subobjects. */
4645 tree t = rli->t;
4646 int n_baseclasses = BINFO_N_BASE_BINFOS (TYPE_BINFO (t));
4647 int i;
4649 /* The primary base class is always allocated first. */
4650 if (CLASSTYPE_HAS_PRIMARY_BASE_P (t))
4651 next_field = build_base_field (rli, CLASSTYPE_PRIMARY_BINFO (t),
4652 offsets, next_field);
4654 /* Now allocate the rest of the bases. */
4655 for (i = 0; i < n_baseclasses; ++i)
4657 tree base_binfo;
4659 base_binfo = BINFO_BASE_BINFO (TYPE_BINFO (t), i);
4661 /* The primary base was already allocated above, so we don't
4662 need to allocate it again here. */
4663 if (base_binfo == CLASSTYPE_PRIMARY_BINFO (t))
4664 continue;
4666 /* Virtual bases are added at the end (a primary virtual base
4667 will have already been added). */
4668 if (BINFO_VIRTUAL_P (base_binfo))
4669 continue;
4671 next_field = build_base_field (rli, base_binfo,
4672 offsets, next_field);
4676 /* Go through the TYPE_METHODS of T issuing any appropriate
4677 diagnostics, figuring out which methods override which other
4678 methods, and so forth. */
4680 static void
4681 check_methods (tree t)
4683 tree x;
4685 for (x = TYPE_METHODS (t); x; x = DECL_CHAIN (x))
4687 check_for_override (x, t);
4688 if (DECL_PURE_VIRTUAL_P (x) && (TREE_CODE (x) != FUNCTION_DECL || ! DECL_VINDEX (x)))
4689 error ("initializer specified for non-virtual method %q+D", x);
4690 /* The name of the field is the original field name
4691 Save this in auxiliary field for later overloading. */
4692 if (TREE_CODE (x) == FUNCTION_DECL && DECL_VINDEX (x))
4694 TYPE_POLYMORPHIC_P (t) = 1;
4695 if (DECL_PURE_VIRTUAL_P (x))
4696 vec_safe_push (CLASSTYPE_PURE_VIRTUALS (t), x);
4698 /* All user-provided destructors are non-trivial.
4699 Constructors and assignment ops are handled in
4700 grok_special_member_properties. */
4701 if (DECL_DESTRUCTOR_P (x) && user_provided_p (x))
4702 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t) = 1;
4703 if (!DECL_VIRTUAL_P (x)
4704 && lookup_attribute ("transaction_safe_dynamic", DECL_ATTRIBUTES (x)))
4705 error_at (DECL_SOURCE_LOCATION (x),
4706 "%<transaction_safe_dynamic%> may only be specified for "
4707 "a virtual function");
4711 /* FN is a constructor or destructor. Clone the declaration to create
4712 a specialized in-charge or not-in-charge version, as indicated by
4713 NAME. */
4715 static tree
4716 build_clone (tree fn, tree name)
4718 tree parms;
4719 tree clone;
4721 /* Copy the function. */
4722 clone = copy_decl (fn);
4723 /* Reset the function name. */
4724 DECL_NAME (clone) = name;
4725 /* Remember where this function came from. */
4726 DECL_ABSTRACT_ORIGIN (clone) = fn;
4727 /* Make it easy to find the CLONE given the FN. */
4728 DECL_CHAIN (clone) = DECL_CHAIN (fn);
4729 DECL_CHAIN (fn) = clone;
4731 /* If this is a template, do the rest on the DECL_TEMPLATE_RESULT. */
4732 if (TREE_CODE (clone) == TEMPLATE_DECL)
4734 tree result = build_clone (DECL_TEMPLATE_RESULT (clone), name);
4735 DECL_TEMPLATE_RESULT (clone) = result;
4736 DECL_TEMPLATE_INFO (result) = copy_node (DECL_TEMPLATE_INFO (result));
4737 DECL_TI_TEMPLATE (result) = clone;
4738 TREE_TYPE (clone) = TREE_TYPE (result);
4739 return clone;
4741 else
4743 // Clone constraints.
4744 if (flag_concepts)
4745 if (tree ci = get_constraints (fn))
4746 set_constraints (clone, copy_node (ci));
4750 SET_DECL_ASSEMBLER_NAME (clone, NULL_TREE);
4751 DECL_CLONED_FUNCTION (clone) = fn;
4752 /* There's no pending inline data for this function. */
4753 DECL_PENDING_INLINE_INFO (clone) = NULL;
4754 DECL_PENDING_INLINE_P (clone) = 0;
4756 /* The base-class destructor is not virtual. */
4757 if (name == base_dtor_identifier)
4759 DECL_VIRTUAL_P (clone) = 0;
4760 if (TREE_CODE (clone) != TEMPLATE_DECL)
4761 DECL_VINDEX (clone) = NULL_TREE;
4764 /* If there was an in-charge parameter, drop it from the function
4765 type. */
4766 if (DECL_HAS_IN_CHARGE_PARM_P (clone))
4768 tree basetype;
4769 tree parmtypes;
4770 tree exceptions;
4772 exceptions = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (clone));
4773 basetype = TYPE_METHOD_BASETYPE (TREE_TYPE (clone));
4774 parmtypes = TYPE_ARG_TYPES (TREE_TYPE (clone));
4775 /* Skip the `this' parameter. */
4776 parmtypes = TREE_CHAIN (parmtypes);
4777 /* Skip the in-charge parameter. */
4778 parmtypes = TREE_CHAIN (parmtypes);
4779 /* And the VTT parm, in a complete [cd]tor. */
4780 if (DECL_HAS_VTT_PARM_P (fn)
4781 && ! DECL_NEEDS_VTT_PARM_P (clone))
4782 parmtypes = TREE_CHAIN (parmtypes);
4783 /* If this is subobject constructor or destructor, add the vtt
4784 parameter. */
4785 TREE_TYPE (clone)
4786 = build_method_type_directly (basetype,
4787 TREE_TYPE (TREE_TYPE (clone)),
4788 parmtypes);
4789 if (exceptions)
4790 TREE_TYPE (clone) = build_exception_variant (TREE_TYPE (clone),
4791 exceptions);
4792 TREE_TYPE (clone)
4793 = cp_build_type_attribute_variant (TREE_TYPE (clone),
4794 TYPE_ATTRIBUTES (TREE_TYPE (fn)));
4797 /* Copy the function parameters. */
4798 DECL_ARGUMENTS (clone) = copy_list (DECL_ARGUMENTS (clone));
4799 /* Remove the in-charge parameter. */
4800 if (DECL_HAS_IN_CHARGE_PARM_P (clone))
4802 DECL_CHAIN (DECL_ARGUMENTS (clone))
4803 = DECL_CHAIN (DECL_CHAIN (DECL_ARGUMENTS (clone)));
4804 DECL_HAS_IN_CHARGE_PARM_P (clone) = 0;
4806 /* And the VTT parm, in a complete [cd]tor. */
4807 if (DECL_HAS_VTT_PARM_P (fn))
4809 if (DECL_NEEDS_VTT_PARM_P (clone))
4810 DECL_HAS_VTT_PARM_P (clone) = 1;
4811 else
4813 DECL_CHAIN (DECL_ARGUMENTS (clone))
4814 = DECL_CHAIN (DECL_CHAIN (DECL_ARGUMENTS (clone)));
4815 DECL_HAS_VTT_PARM_P (clone) = 0;
4819 /* A base constructor inheriting from a virtual base doesn't get the
4820 arguments. */
4821 if (ctor_omit_inherited_parms (fn))
4822 DECL_CHAIN (DECL_CHAIN (DECL_ARGUMENTS (clone))) = NULL_TREE;
4824 for (parms = DECL_ARGUMENTS (clone); parms; parms = DECL_CHAIN (parms))
4826 DECL_CONTEXT (parms) = clone;
4827 cxx_dup_lang_specific_decl (parms);
4830 /* Create the RTL for this function. */
4831 SET_DECL_RTL (clone, NULL);
4832 rest_of_decl_compilation (clone, /*top_level=*/1, at_eof);
4834 return clone;
4837 /* Implementation of DECL_CLONED_FUNCTION and DECL_CLONED_FUNCTION_P, do
4838 not invoke this function directly.
4840 For a non-thunk function, returns the address of the slot for storing
4841 the function it is a clone of. Otherwise returns NULL_TREE.
4843 If JUST_TESTING, looks through TEMPLATE_DECL and returns NULL if
4844 cloned_function is unset. This is to support the separate
4845 DECL_CLONED_FUNCTION and DECL_CLONED_FUNCTION_P modes; using the latter
4846 on a template makes sense, but not the former. */
4848 tree *
4849 decl_cloned_function_p (const_tree decl, bool just_testing)
4851 tree *ptr;
4852 if (just_testing)
4853 decl = STRIP_TEMPLATE (decl);
4855 if (TREE_CODE (decl) != FUNCTION_DECL
4856 || !DECL_LANG_SPECIFIC (decl)
4857 || DECL_LANG_SPECIFIC (decl)->u.fn.thunk_p)
4859 #if defined ENABLE_TREE_CHECKING && (GCC_VERSION >= 2007)
4860 if (!just_testing)
4861 lang_check_failed (__FILE__, __LINE__, __FUNCTION__);
4862 else
4863 #endif
4864 return NULL;
4867 ptr = &DECL_LANG_SPECIFIC (decl)->u.fn.u5.cloned_function;
4868 if (just_testing && *ptr == NULL_TREE)
4869 return NULL;
4870 else
4871 return ptr;
4874 /* Produce declarations for all appropriate clones of FN. If
4875 UPDATE_METHOD_VEC_P is nonzero, the clones are added to the
4876 CLASTYPE_METHOD_VEC as well. */
4878 void
4879 clone_function_decl (tree fn, int update_method_vec_p)
4881 tree clone;
4883 /* Avoid inappropriate cloning. */
4884 if (DECL_CHAIN (fn)
4885 && DECL_CLONED_FUNCTION_P (DECL_CHAIN (fn)))
4886 return;
4888 if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (fn))
4890 /* For each constructor, we need two variants: an in-charge version
4891 and a not-in-charge version. */
4892 clone = build_clone (fn, complete_ctor_identifier);
4893 if (update_method_vec_p)
4894 add_method (DECL_CONTEXT (clone), clone, NULL_TREE);
4895 clone = build_clone (fn, base_ctor_identifier);
4896 if (update_method_vec_p)
4897 add_method (DECL_CONTEXT (clone), clone, NULL_TREE);
4899 else
4901 gcc_assert (DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (fn));
4903 /* For each destructor, we need three variants: an in-charge
4904 version, a not-in-charge version, and an in-charge deleting
4905 version. We clone the deleting version first because that
4906 means it will go second on the TYPE_METHODS list -- and that
4907 corresponds to the correct layout order in the virtual
4908 function table.
4910 For a non-virtual destructor, we do not build a deleting
4911 destructor. */
4912 if (DECL_VIRTUAL_P (fn))
4914 clone = build_clone (fn, deleting_dtor_identifier);
4915 if (update_method_vec_p)
4916 add_method (DECL_CONTEXT (clone), clone, NULL_TREE);
4918 clone = build_clone (fn, complete_dtor_identifier);
4919 if (update_method_vec_p)
4920 add_method (DECL_CONTEXT (clone), clone, NULL_TREE);
4921 clone = build_clone (fn, base_dtor_identifier);
4922 if (update_method_vec_p)
4923 add_method (DECL_CONTEXT (clone), clone, NULL_TREE);
4926 /* Note that this is an abstract function that is never emitted. */
4927 DECL_ABSTRACT_P (fn) = true;
4930 /* DECL is an in charge constructor, which is being defined. This will
4931 have had an in class declaration, from whence clones were
4932 declared. An out-of-class definition can specify additional default
4933 arguments. As it is the clones that are involved in overload
4934 resolution, we must propagate the information from the DECL to its
4935 clones. */
4937 void
4938 adjust_clone_args (tree decl)
4940 tree clone;
4942 for (clone = DECL_CHAIN (decl); clone && DECL_CLONED_FUNCTION_P (clone);
4943 clone = DECL_CHAIN (clone))
4945 tree orig_clone_parms = TYPE_ARG_TYPES (TREE_TYPE (clone));
4946 tree orig_decl_parms = TYPE_ARG_TYPES (TREE_TYPE (decl));
4947 tree decl_parms, clone_parms;
4949 clone_parms = orig_clone_parms;
4951 /* Skip the 'this' parameter. */
4952 orig_clone_parms = TREE_CHAIN (orig_clone_parms);
4953 orig_decl_parms = TREE_CHAIN (orig_decl_parms);
4955 if (DECL_HAS_IN_CHARGE_PARM_P (decl))
4956 orig_decl_parms = TREE_CHAIN (orig_decl_parms);
4957 if (DECL_HAS_VTT_PARM_P (decl))
4958 orig_decl_parms = TREE_CHAIN (orig_decl_parms);
4960 clone_parms = orig_clone_parms;
4961 if (DECL_HAS_VTT_PARM_P (clone))
4962 clone_parms = TREE_CHAIN (clone_parms);
4964 for (decl_parms = orig_decl_parms; decl_parms;
4965 decl_parms = TREE_CHAIN (decl_parms),
4966 clone_parms = TREE_CHAIN (clone_parms))
4968 gcc_assert (same_type_p (TREE_TYPE (decl_parms),
4969 TREE_TYPE (clone_parms)));
4971 if (TREE_PURPOSE (decl_parms) && !TREE_PURPOSE (clone_parms))
4973 /* A default parameter has been added. Adjust the
4974 clone's parameters. */
4975 tree exceptions = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (clone));
4976 tree attrs = TYPE_ATTRIBUTES (TREE_TYPE (clone));
4977 tree basetype = TYPE_METHOD_BASETYPE (TREE_TYPE (clone));
4978 tree type;
4980 clone_parms = orig_decl_parms;
4982 if (DECL_HAS_VTT_PARM_P (clone))
4984 clone_parms = tree_cons (TREE_PURPOSE (orig_clone_parms),
4985 TREE_VALUE (orig_clone_parms),
4986 clone_parms);
4987 TREE_TYPE (clone_parms) = TREE_TYPE (orig_clone_parms);
4989 type = build_method_type_directly (basetype,
4990 TREE_TYPE (TREE_TYPE (clone)),
4991 clone_parms);
4992 if (exceptions)
4993 type = build_exception_variant (type, exceptions);
4994 if (attrs)
4995 type = cp_build_type_attribute_variant (type, attrs);
4996 TREE_TYPE (clone) = type;
4998 clone_parms = NULL_TREE;
4999 break;
5002 gcc_assert (!clone_parms);
5006 /* For each of the constructors and destructors in T, create an
5007 in-charge and not-in-charge variant. */
5009 static void
5010 clone_constructors_and_destructors (tree t)
5012 tree fns;
5014 /* If for some reason we don't have a CLASSTYPE_METHOD_VEC, we bail
5015 out now. */
5016 if (!CLASSTYPE_METHOD_VEC (t))
5017 return;
5019 for (fns = CLASSTYPE_CONSTRUCTORS (t); fns; fns = OVL_NEXT (fns))
5020 clone_function_decl (OVL_CURRENT (fns), /*update_method_vec_p=*/1);
5021 for (fns = CLASSTYPE_DESTRUCTORS (t); fns; fns = OVL_NEXT (fns))
5022 clone_function_decl (OVL_CURRENT (fns), /*update_method_vec_p=*/1);
5025 /* Deduce noexcept for a destructor DTOR. */
5027 void
5028 deduce_noexcept_on_destructor (tree dtor)
5030 if (!TYPE_RAISES_EXCEPTIONS (TREE_TYPE (dtor)))
5032 tree eh_spec = unevaluated_noexcept_spec ();
5033 TREE_TYPE (dtor) = build_exception_variant (TREE_TYPE (dtor), eh_spec);
5037 /* For each destructor in T, deduce noexcept:
5039 12.4/3: A declaration of a destructor that does not have an
5040 exception-specification is implicitly considered to have the
5041 same exception-specification as an implicit declaration (15.4). */
5043 static void
5044 deduce_noexcept_on_destructors (tree t)
5046 /* If for some reason we don't have a CLASSTYPE_METHOD_VEC, we bail
5047 out now. */
5048 if (!CLASSTYPE_METHOD_VEC (t))
5049 return;
5051 for (tree fns = CLASSTYPE_DESTRUCTORS (t); fns; fns = OVL_NEXT (fns))
5052 deduce_noexcept_on_destructor (OVL_CURRENT (fns));
5055 /* Subroutine of set_one_vmethod_tm_attributes. Search base classes
5056 of TYPE for virtual functions which FNDECL overrides. Return a
5057 mask of the tm attributes found therein. */
5059 static int
5060 look_for_tm_attr_overrides (tree type, tree fndecl)
5062 tree binfo = TYPE_BINFO (type);
5063 tree base_binfo;
5064 int ix, found = 0;
5066 for (ix = 0; BINFO_BASE_ITERATE (binfo, ix, base_binfo); ++ix)
5068 tree o, basetype = BINFO_TYPE (base_binfo);
5070 if (!TYPE_POLYMORPHIC_P (basetype))
5071 continue;
5073 o = look_for_overrides_here (basetype, fndecl);
5074 if (o)
5076 if (lookup_attribute ("transaction_safe_dynamic",
5077 DECL_ATTRIBUTES (o)))
5078 /* transaction_safe_dynamic is not inherited. */;
5079 else
5080 found |= tm_attr_to_mask (find_tm_attribute
5081 (TYPE_ATTRIBUTES (TREE_TYPE (o))));
5083 else
5084 found |= look_for_tm_attr_overrides (basetype, fndecl);
5087 return found;
5090 /* Subroutine of set_method_tm_attributes. Handle the checks and
5091 inheritance for one virtual method FNDECL. */
5093 static void
5094 set_one_vmethod_tm_attributes (tree type, tree fndecl)
5096 tree tm_attr;
5097 int found, have;
5099 found = look_for_tm_attr_overrides (type, fndecl);
5101 /* If FNDECL doesn't actually override anything (i.e. T is the
5102 class that first declares FNDECL virtual), then we're done. */
5103 if (found == 0)
5104 return;
5106 tm_attr = find_tm_attribute (TYPE_ATTRIBUTES (TREE_TYPE (fndecl)));
5107 have = tm_attr_to_mask (tm_attr);
5109 /* Intel STM Language Extension 3.0, Section 4.2 table 4:
5110 tm_pure must match exactly, otherwise no weakening of
5111 tm_safe > tm_callable > nothing. */
5112 /* ??? The tm_pure attribute didn't make the transition to the
5113 multivendor language spec. */
5114 if (have == TM_ATTR_PURE)
5116 if (found != TM_ATTR_PURE)
5118 found &= -found;
5119 goto err_override;
5122 /* If the overridden function is tm_pure, then FNDECL must be. */
5123 else if (found == TM_ATTR_PURE && tm_attr)
5124 goto err_override;
5125 /* Look for base class combinations that cannot be satisfied. */
5126 else if (found != TM_ATTR_PURE && (found & TM_ATTR_PURE))
5128 found &= ~TM_ATTR_PURE;
5129 found &= -found;
5130 error_at (DECL_SOURCE_LOCATION (fndecl),
5131 "method overrides both %<transaction_pure%> and %qE methods",
5132 tm_mask_to_attr (found));
5134 /* If FNDECL did not declare an attribute, then inherit the most
5135 restrictive one. */
5136 else if (tm_attr == NULL)
5138 apply_tm_attr (fndecl, tm_mask_to_attr (least_bit_hwi (found)));
5140 /* Otherwise validate that we're not weaker than a function
5141 that is being overridden. */
5142 else
5144 found &= -found;
5145 if (found <= TM_ATTR_CALLABLE && have > found)
5146 goto err_override;
5148 return;
5150 err_override:
5151 error_at (DECL_SOURCE_LOCATION (fndecl),
5152 "method declared %qE overriding %qE method",
5153 tm_attr, tm_mask_to_attr (found));
5156 /* For each of the methods in T, propagate a class-level tm attribute. */
5158 static void
5159 set_method_tm_attributes (tree t)
5161 tree class_tm_attr, fndecl;
5163 /* Don't bother collecting tm attributes if transactional memory
5164 support is not enabled. */
5165 if (!flag_tm)
5166 return;
5168 /* Process virtual methods first, as they inherit directly from the
5169 base virtual function and also require validation of new attributes. */
5170 if (TYPE_CONTAINS_VPTR_P (t))
5172 tree vchain;
5173 for (vchain = BINFO_VIRTUALS (TYPE_BINFO (t)); vchain;
5174 vchain = TREE_CHAIN (vchain))
5176 fndecl = BV_FN (vchain);
5177 if (DECL_THUNK_P (fndecl))
5178 fndecl = THUNK_TARGET (fndecl);
5179 set_one_vmethod_tm_attributes (t, fndecl);
5183 /* If the class doesn't have an attribute, nothing more to do. */
5184 class_tm_attr = find_tm_attribute (TYPE_ATTRIBUTES (t));
5185 if (class_tm_attr == NULL)
5186 return;
5188 /* Any method that does not yet have a tm attribute inherits
5189 the one from the class. */
5190 for (fndecl = TYPE_METHODS (t); fndecl; fndecl = TREE_CHAIN (fndecl))
5192 if (!find_tm_attribute (TYPE_ATTRIBUTES (TREE_TYPE (fndecl))))
5193 apply_tm_attr (fndecl, class_tm_attr);
5197 /* Returns true if FN is a default constructor. */
5199 bool
5200 default_ctor_p (tree fn)
5202 return (DECL_CONSTRUCTOR_P (fn)
5203 && sufficient_parms_p (FUNCTION_FIRST_USER_PARMTYPE (fn)));
5206 /* Returns true iff class T has a user-defined constructor that can be called
5207 with more than zero arguments. */
5209 bool
5210 type_has_user_nondefault_constructor (tree t)
5212 tree fns;
5214 if (!TYPE_HAS_USER_CONSTRUCTOR (t))
5215 return false;
5217 for (fns = CLASSTYPE_CONSTRUCTORS (t); fns; fns = OVL_NEXT (fns))
5219 tree fn = OVL_CURRENT (fns);
5220 if (!DECL_ARTIFICIAL (fn)
5221 && (TREE_CODE (fn) == TEMPLATE_DECL
5222 || (skip_artificial_parms_for (fn, DECL_ARGUMENTS (fn))
5223 != NULL_TREE)))
5224 return true;
5227 return false;
5230 /* Returns the defaulted constructor if T has one. Otherwise, returns
5231 NULL_TREE. */
5233 tree
5234 in_class_defaulted_default_constructor (tree t)
5236 if (!TYPE_HAS_USER_CONSTRUCTOR (t))
5237 return NULL_TREE;
5239 for (tree fns = CLASSTYPE_CONSTRUCTORS (t); fns; fns = OVL_NEXT (fns))
5241 tree fn = OVL_CURRENT (fns);
5243 if (DECL_DEFAULTED_IN_CLASS_P (fn)
5244 && default_ctor_p (fn))
5245 return fn;
5248 return NULL_TREE;
5251 /* Returns true iff FN is a user-provided function, i.e. user-declared
5252 and not defaulted at its first declaration. */
5254 bool
5255 user_provided_p (tree fn)
5257 if (TREE_CODE (fn) == TEMPLATE_DECL)
5258 return true;
5259 else
5260 return (!DECL_ARTIFICIAL (fn)
5261 && !(DECL_INITIALIZED_IN_CLASS_P (fn)
5262 && (DECL_DEFAULTED_FN (fn) || DECL_DELETED_FN (fn))));
5265 /* Returns true iff class T has a user-provided constructor. */
5267 bool
5268 type_has_user_provided_constructor (tree t)
5270 tree fns;
5272 if (!CLASS_TYPE_P (t))
5273 return false;
5275 if (!TYPE_HAS_USER_CONSTRUCTOR (t))
5276 return false;
5278 /* This can happen in error cases; avoid crashing. */
5279 if (!CLASSTYPE_METHOD_VEC (t))
5280 return false;
5282 for (fns = CLASSTYPE_CONSTRUCTORS (t); fns; fns = OVL_NEXT (fns))
5283 if (user_provided_p (OVL_CURRENT (fns)))
5284 return true;
5286 return false;
5289 /* Returns true iff class T has a user-provided or explicit constructor. */
5291 bool
5292 type_has_user_provided_or_explicit_constructor (tree t)
5294 tree fns;
5296 if (!CLASS_TYPE_P (t))
5297 return false;
5299 if (!TYPE_HAS_USER_CONSTRUCTOR (t))
5300 return false;
5302 /* This can happen in error cases; avoid crashing. */
5303 if (!CLASSTYPE_METHOD_VEC (t))
5304 return false;
5306 for (fns = CLASSTYPE_CONSTRUCTORS (t); fns; fns = OVL_NEXT (fns))
5308 tree fn = OVL_CURRENT (fns);
5309 if (user_provided_p (fn) || DECL_NONCONVERTING_P (fn))
5310 return true;
5313 return false;
5316 /* Returns true iff class T has a non-user-provided (i.e. implicitly
5317 declared or explicitly defaulted in the class body) default
5318 constructor. */
5320 bool
5321 type_has_non_user_provided_default_constructor (tree t)
5323 tree fns;
5325 if (!TYPE_HAS_DEFAULT_CONSTRUCTOR (t))
5326 return false;
5327 if (CLASSTYPE_LAZY_DEFAULT_CTOR (t))
5328 return true;
5330 for (fns = CLASSTYPE_CONSTRUCTORS (t); fns; fns = OVL_NEXT (fns))
5332 tree fn = OVL_CURRENT (fns);
5333 if (TREE_CODE (fn) == FUNCTION_DECL
5334 && default_ctor_p (fn)
5335 && !user_provided_p (fn))
5336 return true;
5339 return false;
5342 /* TYPE is being used as a virtual base, and has a non-trivial move
5343 assignment. Return true if this is due to there being a user-provided
5344 move assignment in TYPE or one of its subobjects; if there isn't, then
5345 multiple move assignment can't cause any harm. */
5347 bool
5348 vbase_has_user_provided_move_assign (tree type)
5350 /* Does the type itself have a user-provided move assignment operator? */
5351 for (tree fns
5352 = lookup_fnfields_slot_nolazy (type, cp_assignment_operator_id (NOP_EXPR));
5353 fns; fns = OVL_NEXT (fns))
5355 tree fn = OVL_CURRENT (fns);
5356 if (move_fn_p (fn) && user_provided_p (fn))
5357 return true;
5360 /* Do any of its bases? */
5361 tree binfo = TYPE_BINFO (type);
5362 tree base_binfo;
5363 for (int i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
5364 if (vbase_has_user_provided_move_assign (BINFO_TYPE (base_binfo)))
5365 return true;
5367 /* Or non-static data members? */
5368 for (tree field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
5370 if (TREE_CODE (field) == FIELD_DECL
5371 && CLASS_TYPE_P (TREE_TYPE (field))
5372 && vbase_has_user_provided_move_assign (TREE_TYPE (field)))
5373 return true;
5376 /* Seems not. */
5377 return false;
5380 /* If default-initialization leaves part of TYPE uninitialized, returns
5381 a DECL for the field or TYPE itself (DR 253). */
5383 tree
5384 default_init_uninitialized_part (tree type)
5386 tree t, r, binfo;
5387 int i;
5389 type = strip_array_types (type);
5390 if (!CLASS_TYPE_P (type))
5391 return type;
5392 if (!type_has_non_user_provided_default_constructor (type))
5393 return NULL_TREE;
5394 for (binfo = TYPE_BINFO (type), i = 0;
5395 BINFO_BASE_ITERATE (binfo, i, t); ++i)
5397 r = default_init_uninitialized_part (BINFO_TYPE (t));
5398 if (r)
5399 return r;
5401 for (t = TYPE_FIELDS (type); t; t = DECL_CHAIN (t))
5402 if (TREE_CODE (t) == FIELD_DECL
5403 && !DECL_ARTIFICIAL (t)
5404 && !DECL_INITIAL (t))
5406 r = default_init_uninitialized_part (TREE_TYPE (t));
5407 if (r)
5408 return DECL_P (r) ? r : t;
5411 return NULL_TREE;
5414 /* Returns true iff for class T, a trivial synthesized default constructor
5415 would be constexpr. */
5417 bool
5418 trivial_default_constructor_is_constexpr (tree t)
5420 /* A defaulted trivial default constructor is constexpr
5421 if there is nothing to initialize. */
5422 gcc_assert (!TYPE_HAS_COMPLEX_DFLT (t));
5423 return is_really_empty_class (t);
5426 /* Returns true iff class T has a constexpr default constructor. */
5428 bool
5429 type_has_constexpr_default_constructor (tree t)
5431 tree fns;
5433 if (!CLASS_TYPE_P (t))
5435 /* The caller should have stripped an enclosing array. */
5436 gcc_assert (TREE_CODE (t) != ARRAY_TYPE);
5437 return false;
5439 if (CLASSTYPE_LAZY_DEFAULT_CTOR (t))
5441 if (!TYPE_HAS_COMPLEX_DFLT (t))
5442 return trivial_default_constructor_is_constexpr (t);
5443 /* Non-trivial, we need to check subobject constructors. */
5444 lazily_declare_fn (sfk_constructor, t);
5446 fns = locate_ctor (t);
5447 return (fns && DECL_DECLARED_CONSTEXPR_P (fns));
5450 /* Returns true iff class T has a constexpr default constructor or has an
5451 implicitly declared default constructor that we can't tell if it's constexpr
5452 without forcing a lazy declaration (which might cause undesired
5453 instantiations). */
5455 bool
5456 type_maybe_constexpr_default_constructor (tree t)
5458 if (CLASS_TYPE_P (t) && CLASSTYPE_LAZY_DEFAULT_CTOR (t)
5459 && TYPE_HAS_COMPLEX_DFLT (t))
5460 /* Assume it's constexpr. */
5461 return true;
5462 return type_has_constexpr_default_constructor (t);
5465 /* Returns true iff class TYPE has a virtual destructor. */
5467 bool
5468 type_has_virtual_destructor (tree type)
5470 tree dtor;
5472 if (!CLASS_TYPE_P (type))
5473 return false;
5475 gcc_assert (COMPLETE_TYPE_P (type));
5476 dtor = CLASSTYPE_DESTRUCTORS (type);
5477 return (dtor && DECL_VIRTUAL_P (dtor));
5480 /* Returns true iff class T has a move constructor. */
5482 bool
5483 type_has_move_constructor (tree t)
5485 tree fns;
5487 if (CLASSTYPE_LAZY_MOVE_CTOR (t))
5489 gcc_assert (COMPLETE_TYPE_P (t));
5490 lazily_declare_fn (sfk_move_constructor, t);
5493 if (!CLASSTYPE_METHOD_VEC (t))
5494 return false;
5496 for (fns = CLASSTYPE_CONSTRUCTORS (t); fns; fns = OVL_NEXT (fns))
5497 if (move_fn_p (OVL_CURRENT (fns)))
5498 return true;
5500 return false;
5503 /* Returns true iff class T has a move assignment operator. */
5505 bool
5506 type_has_move_assign (tree t)
5508 tree fns;
5510 if (CLASSTYPE_LAZY_MOVE_ASSIGN (t))
5512 gcc_assert (COMPLETE_TYPE_P (t));
5513 lazily_declare_fn (sfk_move_assignment, t);
5516 for (fns = lookup_fnfields_slot_nolazy (t, cp_assignment_operator_id (NOP_EXPR));
5517 fns; fns = OVL_NEXT (fns))
5518 if (move_fn_p (OVL_CURRENT (fns)))
5519 return true;
5521 return false;
5524 /* Returns true iff class T has a move constructor that was explicitly
5525 declared in the class body. Note that this is different from
5526 "user-provided", which doesn't include functions that are defaulted in
5527 the class. */
5529 bool
5530 type_has_user_declared_move_constructor (tree t)
5532 tree fns;
5534 if (CLASSTYPE_LAZY_MOVE_CTOR (t))
5535 return false;
5537 if (!CLASSTYPE_METHOD_VEC (t))
5538 return false;
5540 for (fns = CLASSTYPE_CONSTRUCTORS (t); fns; fns = OVL_NEXT (fns))
5542 tree fn = OVL_CURRENT (fns);
5543 if (move_fn_p (fn) && !DECL_ARTIFICIAL (fn))
5544 return true;
5547 return false;
5550 /* Returns true iff class T has a move assignment operator that was
5551 explicitly declared in the class body. */
5553 bool
5554 type_has_user_declared_move_assign (tree t)
5556 tree fns;
5558 if (CLASSTYPE_LAZY_MOVE_ASSIGN (t))
5559 return false;
5561 for (fns = lookup_fnfields_slot_nolazy (t, cp_assignment_operator_id (NOP_EXPR));
5562 fns; fns = OVL_NEXT (fns))
5564 tree fn = OVL_CURRENT (fns);
5565 if (move_fn_p (fn) && !DECL_ARTIFICIAL (fn))
5566 return true;
5569 return false;
5572 /* Nonzero if we need to build up a constructor call when initializing an
5573 object of this class, either because it has a user-declared constructor
5574 or because it doesn't have a default constructor (so we need to give an
5575 error if no initializer is provided). Use TYPE_NEEDS_CONSTRUCTING when
5576 what you care about is whether or not an object can be produced by a
5577 constructor (e.g. so we don't set TREE_READONLY on const variables of
5578 such type); use this function when what you care about is whether or not
5579 to try to call a constructor to create an object. The latter case is
5580 the former plus some cases of constructors that cannot be called. */
5582 bool
5583 type_build_ctor_call (tree t)
5585 tree inner;
5586 if (TYPE_NEEDS_CONSTRUCTING (t))
5587 return true;
5588 inner = strip_array_types (t);
5589 if (!CLASS_TYPE_P (inner) || ANON_AGGR_TYPE_P (inner))
5590 return false;
5591 if (!TYPE_HAS_DEFAULT_CONSTRUCTOR (inner))
5592 return true;
5593 if (cxx_dialect < cxx11)
5594 return false;
5595 /* A user-declared constructor might be private, and a constructor might
5596 be trivial but deleted. */
5597 for (tree fns = lookup_fnfields_slot (inner, complete_ctor_identifier);
5598 fns; fns = OVL_NEXT (fns))
5600 tree fn = OVL_CURRENT (fns);
5601 if (!DECL_ARTIFICIAL (fn)
5602 || DECL_DELETED_FN (fn))
5603 return true;
5605 return false;
5608 /* Like type_build_ctor_call, but for destructors. */
5610 bool
5611 type_build_dtor_call (tree t)
5613 tree inner;
5614 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t))
5615 return true;
5616 inner = strip_array_types (t);
5617 if (!CLASS_TYPE_P (inner) || ANON_AGGR_TYPE_P (inner)
5618 || !COMPLETE_TYPE_P (inner))
5619 return false;
5620 if (cxx_dialect < cxx11)
5621 return false;
5622 /* A user-declared destructor might be private, and a destructor might
5623 be trivial but deleted. */
5624 for (tree fns = lookup_fnfields_slot (inner, complete_dtor_identifier);
5625 fns; fns = OVL_NEXT (fns))
5627 tree fn = OVL_CURRENT (fns);
5628 if (!DECL_ARTIFICIAL (fn)
5629 || DECL_DELETED_FN (fn))
5630 return true;
5632 return false;
5635 /* Remove all zero-width bit-fields from T. */
5637 static void
5638 remove_zero_width_bit_fields (tree t)
5640 tree *fieldsp;
5642 fieldsp = &TYPE_FIELDS (t);
5643 while (*fieldsp)
5645 if (TREE_CODE (*fieldsp) == FIELD_DECL
5646 && DECL_C_BIT_FIELD (*fieldsp)
5647 /* We should not be confused by the fact that grokbitfield
5648 temporarily sets the width of the bit field into
5649 DECL_INITIAL (*fieldsp).
5650 check_bitfield_decl eventually sets DECL_SIZE (*fieldsp)
5651 to that width. */
5652 && (DECL_SIZE (*fieldsp) == NULL_TREE
5653 || integer_zerop (DECL_SIZE (*fieldsp))))
5654 *fieldsp = DECL_CHAIN (*fieldsp);
5655 else
5656 fieldsp = &DECL_CHAIN (*fieldsp);
5660 /* Returns TRUE iff we need a cookie when dynamically allocating an
5661 array whose elements have the indicated class TYPE. */
5663 static bool
5664 type_requires_array_cookie (tree type)
5666 tree fns;
5667 bool has_two_argument_delete_p = false;
5669 gcc_assert (CLASS_TYPE_P (type));
5671 /* If there's a non-trivial destructor, we need a cookie. In order
5672 to iterate through the array calling the destructor for each
5673 element, we'll have to know how many elements there are. */
5674 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
5675 return true;
5677 /* If the usual deallocation function is a two-argument whose second
5678 argument is of type `size_t', then we have to pass the size of
5679 the array to the deallocation function, so we will need to store
5680 a cookie. */
5681 fns = lookup_fnfields (TYPE_BINFO (type),
5682 cp_operator_id (VEC_DELETE_EXPR),
5683 /*protect=*/0);
5684 /* If there are no `operator []' members, or the lookup is
5685 ambiguous, then we don't need a cookie. */
5686 if (!fns || fns == error_mark_node)
5687 return false;
5688 /* Loop through all of the functions. */
5689 for (fns = BASELINK_FUNCTIONS (fns); fns; fns = OVL_NEXT (fns))
5691 tree fn;
5692 tree second_parm;
5694 /* Select the current function. */
5695 fn = OVL_CURRENT (fns);
5696 /* See if this function is a one-argument delete function. If
5697 it is, then it will be the usual deallocation function. */
5698 second_parm = TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (fn)));
5699 if (second_parm == void_list_node)
5700 return false;
5701 /* Do not consider this function if its second argument is an
5702 ellipsis. */
5703 if (!second_parm)
5704 continue;
5705 /* Otherwise, if we have a two-argument function and the second
5706 argument is `size_t', it will be the usual deallocation
5707 function -- unless there is one-argument function, too. */
5708 if (TREE_CHAIN (second_parm) == void_list_node
5709 && same_type_p (TREE_VALUE (second_parm), size_type_node))
5710 has_two_argument_delete_p = true;
5713 return has_two_argument_delete_p;
5716 /* Finish computing the `literal type' property of class type T.
5718 At this point, we have already processed base classes and
5719 non-static data members. We need to check whether the copy
5720 constructor is trivial, the destructor is trivial, and there
5721 is a trivial default constructor or at least one constexpr
5722 constructor other than the copy constructor. */
5724 static void
5725 finalize_literal_type_property (tree t)
5727 tree fn;
5729 if (cxx_dialect < cxx11
5730 || TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t))
5731 CLASSTYPE_LITERAL_P (t) = false;
5732 else if (CLASSTYPE_LITERAL_P (t) && !TYPE_HAS_TRIVIAL_DFLT (t)
5733 && CLASSTYPE_NON_AGGREGATE (t)
5734 && !TYPE_HAS_CONSTEXPR_CTOR (t))
5735 CLASSTYPE_LITERAL_P (t) = false;
5737 if (!CLASSTYPE_LITERAL_P (t))
5738 for (fn = TYPE_METHODS (t); fn; fn = DECL_CHAIN (fn))
5739 if (DECL_DECLARED_CONSTEXPR_P (fn)
5740 && TREE_CODE (fn) != TEMPLATE_DECL
5741 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
5742 && !DECL_CONSTRUCTOR_P (fn))
5744 DECL_DECLARED_CONSTEXPR_P (fn) = false;
5745 if (!DECL_GENERATED_P (fn) && !LAMBDA_TYPE_P (t))
5747 error ("enclosing class of constexpr non-static member "
5748 "function %q+#D is not a literal type", fn);
5749 explain_non_literal_class (t);
5754 /* T is a non-literal type used in a context which requires a constant
5755 expression. Explain why it isn't literal. */
5757 void
5758 explain_non_literal_class (tree t)
5760 static hash_set<tree> *diagnosed;
5762 if (!CLASS_TYPE_P (t))
5763 return;
5764 t = TYPE_MAIN_VARIANT (t);
5766 if (diagnosed == NULL)
5767 diagnosed = new hash_set<tree>;
5768 if (diagnosed->add (t))
5769 /* Already explained. */
5770 return;
5772 inform (0, "%q+T is not literal because:", t);
5773 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t))
5774 inform (0, " %q+T has a non-trivial destructor", t);
5775 else if (CLASSTYPE_NON_AGGREGATE (t)
5776 && !TYPE_HAS_TRIVIAL_DFLT (t)
5777 && !TYPE_HAS_CONSTEXPR_CTOR (t))
5779 inform (0, " %q+T is not an aggregate, does not have a trivial "
5780 "default constructor, and has no constexpr constructor that "
5781 "is not a copy or move constructor", t);
5782 if (type_has_non_user_provided_default_constructor (t))
5784 /* Note that we can't simply call locate_ctor because when the
5785 constructor is deleted it just returns NULL_TREE. */
5786 tree fns;
5787 for (fns = CLASSTYPE_CONSTRUCTORS (t); fns; fns = OVL_NEXT (fns))
5789 tree fn = OVL_CURRENT (fns);
5790 tree parms = TYPE_ARG_TYPES (TREE_TYPE (fn));
5792 parms = skip_artificial_parms_for (fn, parms);
5794 if (sufficient_parms_p (parms))
5796 if (DECL_DELETED_FN (fn))
5797 maybe_explain_implicit_delete (fn);
5798 else
5799 explain_invalid_constexpr_fn (fn);
5800 break;
5805 else
5807 tree binfo, base_binfo, field; int i;
5808 for (binfo = TYPE_BINFO (t), i = 0;
5809 BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
5811 tree basetype = TREE_TYPE (base_binfo);
5812 if (!CLASSTYPE_LITERAL_P (basetype))
5814 inform (0, " base class %qT of %q+T is non-literal",
5815 basetype, t);
5816 explain_non_literal_class (basetype);
5817 return;
5820 for (field = TYPE_FIELDS (t); field; field = TREE_CHAIN (field))
5822 tree ftype;
5823 if (TREE_CODE (field) != FIELD_DECL)
5824 continue;
5825 ftype = TREE_TYPE (field);
5826 if (!literal_type_p (ftype))
5828 inform (DECL_SOURCE_LOCATION (field),
5829 " non-static data member %qD has non-literal type",
5830 field);
5831 if (CLASS_TYPE_P (ftype))
5832 explain_non_literal_class (ftype);
5834 if (CP_TYPE_VOLATILE_P (ftype))
5835 inform (DECL_SOURCE_LOCATION (field),
5836 " non-static data member %qD has volatile type", field);
5841 /* Check the validity of the bases and members declared in T. Add any
5842 implicitly-generated functions (like copy-constructors and
5843 assignment operators). Compute various flag bits (like
5844 CLASSTYPE_NON_LAYOUT_POD_T) for T. This routine works purely at the C++
5845 level: i.e., independently of the ABI in use. */
5847 static void
5848 check_bases_and_members (tree t)
5850 /* Nonzero if the implicitly generated copy constructor should take
5851 a non-const reference argument. */
5852 int cant_have_const_ctor;
5853 /* Nonzero if the implicitly generated assignment operator
5854 should take a non-const reference argument. */
5855 int no_const_asn_ref;
5856 tree access_decls;
5857 bool saved_complex_asn_ref;
5858 bool saved_nontrivial_dtor;
5859 tree fn;
5861 /* By default, we use const reference arguments and generate default
5862 constructors. */
5863 cant_have_const_ctor = 0;
5864 no_const_asn_ref = 0;
5866 /* Check all the base-classes and set FMEM members to point to arrays
5867 of potential interest. */
5868 check_bases (t, &cant_have_const_ctor, &no_const_asn_ref);
5870 /* Deduce noexcept on destructors. This needs to happen after we've set
5871 triviality flags appropriately for our bases. */
5872 if (cxx_dialect >= cxx11)
5873 deduce_noexcept_on_destructors (t);
5875 /* Check all the method declarations. */
5876 check_methods (t);
5878 /* Save the initial values of these flags which only indicate whether
5879 or not the class has user-provided functions. As we analyze the
5880 bases and members we can set these flags for other reasons. */
5881 saved_complex_asn_ref = TYPE_HAS_COMPLEX_COPY_ASSIGN (t);
5882 saved_nontrivial_dtor = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t);
5884 /* Check all the data member declarations. We cannot call
5885 check_field_decls until we have called check_bases check_methods,
5886 as check_field_decls depends on TYPE_HAS_NONTRIVIAL_DESTRUCTOR
5887 being set appropriately. */
5888 check_field_decls (t, &access_decls,
5889 &cant_have_const_ctor,
5890 &no_const_asn_ref);
5892 /* A nearly-empty class has to be vptr-containing; a nearly empty
5893 class contains just a vptr. */
5894 if (!TYPE_CONTAINS_VPTR_P (t))
5895 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
5897 /* Do some bookkeeping that will guide the generation of implicitly
5898 declared member functions. */
5899 TYPE_HAS_COMPLEX_COPY_CTOR (t) |= TYPE_CONTAINS_VPTR_P (t);
5900 TYPE_HAS_COMPLEX_MOVE_CTOR (t) |= TYPE_CONTAINS_VPTR_P (t);
5901 /* We need to call a constructor for this class if it has a
5902 user-provided constructor, or if the default constructor is going
5903 to initialize the vptr. (This is not an if-and-only-if;
5904 TYPE_NEEDS_CONSTRUCTING is set elsewhere if bases or members
5905 themselves need constructing.) */
5906 TYPE_NEEDS_CONSTRUCTING (t)
5907 |= (type_has_user_provided_constructor (t) || TYPE_CONTAINS_VPTR_P (t));
5908 /* [dcl.init.aggr]
5910 An aggregate is an array or a class with no user-provided
5911 constructors ... and no virtual functions.
5913 Again, other conditions for being an aggregate are checked
5914 elsewhere. */
5915 CLASSTYPE_NON_AGGREGATE (t)
5916 |= (type_has_user_provided_or_explicit_constructor (t)
5917 || TYPE_POLYMORPHIC_P (t));
5918 /* This is the C++98/03 definition of POD; it changed in C++0x, but we
5919 retain the old definition internally for ABI reasons. */
5920 CLASSTYPE_NON_LAYOUT_POD_P (t)
5921 |= (CLASSTYPE_NON_AGGREGATE (t)
5922 || saved_nontrivial_dtor || saved_complex_asn_ref);
5923 CLASSTYPE_NON_STD_LAYOUT (t) |= TYPE_CONTAINS_VPTR_P (t);
5924 TYPE_HAS_COMPLEX_COPY_ASSIGN (t) |= TYPE_CONTAINS_VPTR_P (t);
5925 TYPE_HAS_COMPLEX_MOVE_ASSIGN (t) |= TYPE_CONTAINS_VPTR_P (t);
5926 TYPE_HAS_COMPLEX_DFLT (t) |= TYPE_CONTAINS_VPTR_P (t);
5928 /* If the only explicitly declared default constructor is user-provided,
5929 set TYPE_HAS_COMPLEX_DFLT. */
5930 if (!TYPE_HAS_COMPLEX_DFLT (t)
5931 && TYPE_HAS_DEFAULT_CONSTRUCTOR (t)
5932 && !type_has_non_user_provided_default_constructor (t))
5933 TYPE_HAS_COMPLEX_DFLT (t) = true;
5935 /* Warn if a public base of a polymorphic type has an accessible
5936 non-virtual destructor. It is only now that we know the class is
5937 polymorphic. Although a polymorphic base will have a already
5938 been diagnosed during its definition, we warn on use too. */
5939 if (TYPE_POLYMORPHIC_P (t) && warn_nonvdtor)
5941 tree binfo = TYPE_BINFO (t);
5942 vec<tree, va_gc> *accesses = BINFO_BASE_ACCESSES (binfo);
5943 tree base_binfo;
5944 unsigned i;
5946 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
5948 tree basetype = TREE_TYPE (base_binfo);
5950 if ((*accesses)[i] == access_public_node
5951 && (TYPE_POLYMORPHIC_P (basetype) || warn_ecpp)
5952 && accessible_nvdtor_p (basetype))
5953 warning (OPT_Wnon_virtual_dtor,
5954 "base class %q#T has accessible non-virtual destructor",
5955 basetype);
5959 /* If the class has no user-declared constructor, but does have
5960 non-static const or reference data members that can never be
5961 initialized, issue a warning. */
5962 if (warn_uninitialized
5963 /* Classes with user-declared constructors are presumed to
5964 initialize these members. */
5965 && !TYPE_HAS_USER_CONSTRUCTOR (t)
5966 /* Aggregates can be initialized with brace-enclosed
5967 initializers. */
5968 && CLASSTYPE_NON_AGGREGATE (t))
5970 tree field;
5972 for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
5974 tree type;
5976 if (TREE_CODE (field) != FIELD_DECL
5977 || DECL_INITIAL (field) != NULL_TREE)
5978 continue;
5980 type = TREE_TYPE (field);
5981 if (TREE_CODE (type) == REFERENCE_TYPE)
5982 warning_at (DECL_SOURCE_LOCATION (field),
5983 OPT_Wuninitialized, "non-static reference %q#D "
5984 "in class without a constructor", field);
5985 else if (CP_TYPE_CONST_P (type)
5986 && (!CLASS_TYPE_P (type)
5987 || !TYPE_HAS_DEFAULT_CONSTRUCTOR (type)))
5988 warning_at (DECL_SOURCE_LOCATION (field),
5989 OPT_Wuninitialized, "non-static const member %q#D "
5990 "in class without a constructor", field);
5994 /* Synthesize any needed methods. */
5995 add_implicitly_declared_members (t, &access_decls,
5996 cant_have_const_ctor,
5997 no_const_asn_ref);
5999 /* Check defaulted declarations here so we have cant_have_const_ctor
6000 and don't need to worry about clones. */
6001 for (fn = TYPE_METHODS (t); fn; fn = DECL_CHAIN (fn))
6002 if (!DECL_ARTIFICIAL (fn) && DECL_DEFAULTED_IN_CLASS_P (fn))
6004 int copy = copy_fn_p (fn);
6005 if (copy > 0)
6007 bool imp_const_p
6008 = (DECL_CONSTRUCTOR_P (fn) ? !cant_have_const_ctor
6009 : !no_const_asn_ref);
6010 bool fn_const_p = (copy == 2);
6012 if (fn_const_p && !imp_const_p)
6013 /* If the function is defaulted outside the class, we just
6014 give the synthesis error. */
6015 error ("%q+D declared to take const reference, but implicit "
6016 "declaration would take non-const", fn);
6018 defaulted_late_check (fn);
6021 if (LAMBDA_TYPE_P (t))
6023 /* "This class type is not an aggregate." */
6024 CLASSTYPE_NON_AGGREGATE (t) = 1;
6027 /* Compute the 'literal type' property before we
6028 do anything with non-static member functions. */
6029 finalize_literal_type_property (t);
6031 /* Create the in-charge and not-in-charge variants of constructors
6032 and destructors. */
6033 clone_constructors_and_destructors (t);
6035 /* Process the using-declarations. */
6036 for (; access_decls; access_decls = TREE_CHAIN (access_decls))
6037 handle_using_decl (TREE_VALUE (access_decls), t);
6039 /* Build and sort the CLASSTYPE_METHOD_VEC. */
6040 finish_struct_methods (t);
6042 /* Figure out whether or not we will need a cookie when dynamically
6043 allocating an array of this type. */
6044 TYPE_LANG_SPECIFIC (t)->u.c.vec_new_uses_cookie
6045 = type_requires_array_cookie (t);
6048 /* If T needs a pointer to its virtual function table, set TYPE_VFIELD
6049 accordingly. If a new vfield was created (because T doesn't have a
6050 primary base class), then the newly created field is returned. It
6051 is not added to the TYPE_FIELDS list; it is the caller's
6052 responsibility to do that. Accumulate declared virtual functions
6053 on VIRTUALS_P. */
6055 static tree
6056 create_vtable_ptr (tree t, tree* virtuals_p)
6058 tree fn;
6060 /* Collect the virtual functions declared in T. */
6061 for (fn = TYPE_METHODS (t); fn; fn = DECL_CHAIN (fn))
6062 if (TREE_CODE (fn) == FUNCTION_DECL
6063 && DECL_VINDEX (fn) && !DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (fn)
6064 && TREE_CODE (DECL_VINDEX (fn)) != INTEGER_CST)
6066 tree new_virtual = make_node (TREE_LIST);
6068 BV_FN (new_virtual) = fn;
6069 BV_DELTA (new_virtual) = integer_zero_node;
6070 BV_VCALL_INDEX (new_virtual) = NULL_TREE;
6072 TREE_CHAIN (new_virtual) = *virtuals_p;
6073 *virtuals_p = new_virtual;
6076 /* If we couldn't find an appropriate base class, create a new field
6077 here. Even if there weren't any new virtual functions, we might need a
6078 new virtual function table if we're supposed to include vptrs in
6079 all classes that need them. */
6080 if (!TYPE_VFIELD (t) && (*virtuals_p || TYPE_CONTAINS_VPTR_P (t)))
6082 /* We build this decl with vtbl_ptr_type_node, which is a
6083 `vtable_entry_type*'. It might seem more precise to use
6084 `vtable_entry_type (*)[N]' where N is the number of virtual
6085 functions. However, that would require the vtable pointer in
6086 base classes to have a different type than the vtable pointer
6087 in derived classes. We could make that happen, but that
6088 still wouldn't solve all the problems. In particular, the
6089 type-based alias analysis code would decide that assignments
6090 to the base class vtable pointer can't alias assignments to
6091 the derived class vtable pointer, since they have different
6092 types. Thus, in a derived class destructor, where the base
6093 class constructor was inlined, we could generate bad code for
6094 setting up the vtable pointer.
6096 Therefore, we use one type for all vtable pointers. We still
6097 use a type-correct type; it's just doesn't indicate the array
6098 bounds. That's better than using `void*' or some such; it's
6099 cleaner, and it let's the alias analysis code know that these
6100 stores cannot alias stores to void*! */
6101 tree field;
6103 field = build_decl (input_location,
6104 FIELD_DECL, get_vfield_name (t), vtbl_ptr_type_node);
6105 DECL_VIRTUAL_P (field) = 1;
6106 DECL_ARTIFICIAL (field) = 1;
6107 DECL_FIELD_CONTEXT (field) = t;
6108 DECL_FCONTEXT (field) = t;
6109 if (TYPE_PACKED (t))
6110 DECL_PACKED (field) = 1;
6112 TYPE_VFIELD (t) = field;
6114 /* This class is non-empty. */
6115 CLASSTYPE_EMPTY_P (t) = 0;
6117 return field;
6120 return NULL_TREE;
6123 /* Add OFFSET to all base types of BINFO which is a base in the
6124 hierarchy dominated by T.
6126 OFFSET, which is a type offset, is number of bytes. */
6128 static void
6129 propagate_binfo_offsets (tree binfo, tree offset)
6131 int i;
6132 tree primary_binfo;
6133 tree base_binfo;
6135 /* Update BINFO's offset. */
6136 BINFO_OFFSET (binfo)
6137 = fold_convert (sizetype,
6138 size_binop (PLUS_EXPR,
6139 fold_convert (ssizetype, BINFO_OFFSET (binfo)),
6140 offset));
6142 /* Find the primary base class. */
6143 primary_binfo = get_primary_binfo (binfo);
6145 if (primary_binfo && BINFO_INHERITANCE_CHAIN (primary_binfo) == binfo)
6146 propagate_binfo_offsets (primary_binfo, offset);
6148 /* Scan all of the bases, pushing the BINFO_OFFSET adjust
6149 downwards. */
6150 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
6152 /* Don't do the primary base twice. */
6153 if (base_binfo == primary_binfo)
6154 continue;
6156 if (BINFO_VIRTUAL_P (base_binfo))
6157 continue;
6159 propagate_binfo_offsets (base_binfo, offset);
6163 /* Set BINFO_OFFSET for all of the virtual bases for RLI->T. Update
6164 TYPE_ALIGN and TYPE_SIZE for T. OFFSETS gives the location of
6165 empty subobjects of T. */
6167 static void
6168 layout_virtual_bases (record_layout_info rli, splay_tree offsets)
6170 tree vbase;
6171 tree t = rli->t;
6172 tree *next_field;
6174 if (BINFO_N_BASE_BINFOS (TYPE_BINFO (t)) == 0)
6175 return;
6177 /* Find the last field. The artificial fields created for virtual
6178 bases will go after the last extant field to date. */
6179 next_field = &TYPE_FIELDS (t);
6180 while (*next_field)
6181 next_field = &DECL_CHAIN (*next_field);
6183 /* Go through the virtual bases, allocating space for each virtual
6184 base that is not already a primary base class. These are
6185 allocated in inheritance graph order. */
6186 for (vbase = TYPE_BINFO (t); vbase; vbase = TREE_CHAIN (vbase))
6188 if (!BINFO_VIRTUAL_P (vbase))
6189 continue;
6191 if (!BINFO_PRIMARY_P (vbase))
6193 /* This virtual base is not a primary base of any class in the
6194 hierarchy, so we have to add space for it. */
6195 next_field = build_base_field (rli, vbase,
6196 offsets, next_field);
6201 /* Returns the offset of the byte just past the end of the base class
6202 BINFO. */
6204 static tree
6205 end_of_base (tree binfo)
6207 tree size;
6209 if (!CLASSTYPE_AS_BASE (BINFO_TYPE (binfo)))
6210 size = TYPE_SIZE_UNIT (char_type_node);
6211 else if (is_empty_class (BINFO_TYPE (binfo)))
6212 /* An empty class has zero CLASSTYPE_SIZE_UNIT, but we need to
6213 allocate some space for it. It cannot have virtual bases, so
6214 TYPE_SIZE_UNIT is fine. */
6215 size = TYPE_SIZE_UNIT (BINFO_TYPE (binfo));
6216 else
6217 size = CLASSTYPE_SIZE_UNIT (BINFO_TYPE (binfo));
6219 return size_binop (PLUS_EXPR, BINFO_OFFSET (binfo), size);
6222 /* Returns the offset of the byte just past the end of the base class
6223 with the highest offset in T. If INCLUDE_VIRTUALS_P is zero, then
6224 only non-virtual bases are included. */
6226 static tree
6227 end_of_class (tree t, int include_virtuals_p)
6229 tree result = size_zero_node;
6230 vec<tree, va_gc> *vbases;
6231 tree binfo;
6232 tree base_binfo;
6233 tree offset;
6234 int i;
6236 for (binfo = TYPE_BINFO (t), i = 0;
6237 BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
6239 if (!include_virtuals_p
6240 && BINFO_VIRTUAL_P (base_binfo)
6241 && (!BINFO_PRIMARY_P (base_binfo)
6242 || BINFO_INHERITANCE_CHAIN (base_binfo) != TYPE_BINFO (t)))
6243 continue;
6245 offset = end_of_base (base_binfo);
6246 if (tree_int_cst_lt (result, offset))
6247 result = offset;
6250 if (include_virtuals_p)
6251 for (vbases = CLASSTYPE_VBASECLASSES (t), i = 0;
6252 vec_safe_iterate (vbases, i, &base_binfo); i++)
6254 offset = end_of_base (base_binfo);
6255 if (tree_int_cst_lt (result, offset))
6256 result = offset;
6259 return result;
6262 /* Warn about bases of T that are inaccessible because they are
6263 ambiguous. For example:
6265 struct S {};
6266 struct T : public S {};
6267 struct U : public S, public T {};
6269 Here, `(S*) new U' is not allowed because there are two `S'
6270 subobjects of U. */
6272 static void
6273 warn_about_ambiguous_bases (tree t)
6275 int i;
6276 vec<tree, va_gc> *vbases;
6277 tree basetype;
6278 tree binfo;
6279 tree base_binfo;
6281 /* If there are no repeated bases, nothing can be ambiguous. */
6282 if (!CLASSTYPE_REPEATED_BASE_P (t))
6283 return;
6285 /* Check direct bases. */
6286 for (binfo = TYPE_BINFO (t), i = 0;
6287 BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
6289 basetype = BINFO_TYPE (base_binfo);
6291 if (!uniquely_derived_from_p (basetype, t))
6292 warning (0, "direct base %qT inaccessible in %qT due to ambiguity",
6293 basetype, t);
6296 /* Check for ambiguous virtual bases. */
6297 if (extra_warnings)
6298 for (vbases = CLASSTYPE_VBASECLASSES (t), i = 0;
6299 vec_safe_iterate (vbases, i, &binfo); i++)
6301 basetype = BINFO_TYPE (binfo);
6303 if (!uniquely_derived_from_p (basetype, t))
6304 warning (OPT_Wextra, "virtual base %qT inaccessible in %qT due "
6305 "to ambiguity", basetype, t);
6309 /* Compare two INTEGER_CSTs K1 and K2. */
6311 static int
6312 splay_tree_compare_integer_csts (splay_tree_key k1, splay_tree_key k2)
6314 return tree_int_cst_compare ((tree) k1, (tree) k2);
6317 /* Increase the size indicated in RLI to account for empty classes
6318 that are "off the end" of the class. */
6320 static void
6321 include_empty_classes (record_layout_info rli)
6323 tree eoc;
6324 tree rli_size;
6326 /* It might be the case that we grew the class to allocate a
6327 zero-sized base class. That won't be reflected in RLI, yet,
6328 because we are willing to overlay multiple bases at the same
6329 offset. However, now we need to make sure that RLI is big enough
6330 to reflect the entire class. */
6331 eoc = end_of_class (rli->t,
6332 CLASSTYPE_AS_BASE (rli->t) != NULL_TREE);
6333 rli_size = rli_size_unit_so_far (rli);
6334 if (TREE_CODE (rli_size) == INTEGER_CST
6335 && tree_int_cst_lt (rli_size, eoc))
6337 /* The size should have been rounded to a whole byte. */
6338 gcc_assert (tree_int_cst_equal
6339 (rli->bitpos, round_down (rli->bitpos, BITS_PER_UNIT)));
6340 rli->bitpos
6341 = size_binop (PLUS_EXPR,
6342 rli->bitpos,
6343 size_binop (MULT_EXPR,
6344 fold_convert (bitsizetype,
6345 size_binop (MINUS_EXPR,
6346 eoc, rli_size)),
6347 bitsize_int (BITS_PER_UNIT)));
6348 normalize_rli (rli);
6352 /* Calculate the TYPE_SIZE, TYPE_ALIGN, etc for T. Calculate
6353 BINFO_OFFSETs for all of the base-classes. Position the vtable
6354 pointer. Accumulate declared virtual functions on VIRTUALS_P. */
6356 static void
6357 layout_class_type (tree t, tree *virtuals_p)
6359 tree non_static_data_members;
6360 tree field;
6361 tree vptr;
6362 record_layout_info rli;
6363 /* Maps offsets (represented as INTEGER_CSTs) to a TREE_LIST of
6364 types that appear at that offset. */
6365 splay_tree empty_base_offsets;
6366 /* True if the last field laid out was a bit-field. */
6367 bool last_field_was_bitfield = false;
6368 /* The location at which the next field should be inserted. */
6369 tree *next_field;
6370 /* T, as a base class. */
6371 tree base_t;
6373 /* Keep track of the first non-static data member. */
6374 non_static_data_members = TYPE_FIELDS (t);
6376 /* Start laying out the record. */
6377 rli = start_record_layout (t);
6379 /* Mark all the primary bases in the hierarchy. */
6380 determine_primary_bases (t);
6382 /* Create a pointer to our virtual function table. */
6383 vptr = create_vtable_ptr (t, virtuals_p);
6385 /* The vptr is always the first thing in the class. */
6386 if (vptr)
6388 DECL_CHAIN (vptr) = TYPE_FIELDS (t);
6389 TYPE_FIELDS (t) = vptr;
6390 next_field = &DECL_CHAIN (vptr);
6391 place_field (rli, vptr);
6393 else
6394 next_field = &TYPE_FIELDS (t);
6396 /* Build FIELD_DECLs for all of the non-virtual base-types. */
6397 empty_base_offsets = splay_tree_new (splay_tree_compare_integer_csts,
6398 NULL, NULL);
6399 build_base_fields (rli, empty_base_offsets, next_field);
6401 /* Layout the non-static data members. */
6402 for (field = non_static_data_members; field; field = DECL_CHAIN (field))
6404 tree type;
6405 tree padding;
6407 /* We still pass things that aren't non-static data members to
6408 the back end, in case it wants to do something with them. */
6409 if (TREE_CODE (field) != FIELD_DECL)
6411 place_field (rli, field);
6412 /* If the static data member has incomplete type, keep track
6413 of it so that it can be completed later. (The handling
6414 of pending statics in finish_record_layout is
6415 insufficient; consider:
6417 struct S1;
6418 struct S2 { static S1 s1; };
6420 At this point, finish_record_layout will be called, but
6421 S1 is still incomplete.) */
6422 if (VAR_P (field))
6424 maybe_register_incomplete_var (field);
6425 /* The visibility of static data members is determined
6426 at their point of declaration, not their point of
6427 definition. */
6428 determine_visibility (field);
6430 continue;
6433 type = TREE_TYPE (field);
6434 if (type == error_mark_node)
6435 continue;
6437 padding = NULL_TREE;
6439 /* If this field is a bit-field whose width is greater than its
6440 type, then there are some special rules for allocating
6441 it. */
6442 if (DECL_C_BIT_FIELD (field)
6443 && tree_int_cst_lt (TYPE_SIZE (type), DECL_SIZE (field)))
6445 unsigned int itk;
6446 tree integer_type;
6447 bool was_unnamed_p = false;
6448 /* We must allocate the bits as if suitably aligned for the
6449 longest integer type that fits in this many bits. type
6450 of the field. Then, we are supposed to use the left over
6451 bits as additional padding. */
6452 for (itk = itk_char; itk != itk_none; ++itk)
6453 if (integer_types[itk] != NULL_TREE
6454 && (tree_int_cst_lt (size_int (MAX_FIXED_MODE_SIZE),
6455 TYPE_SIZE (integer_types[itk]))
6456 || tree_int_cst_lt (DECL_SIZE (field),
6457 TYPE_SIZE (integer_types[itk]))))
6458 break;
6460 /* ITK now indicates a type that is too large for the
6461 field. We have to back up by one to find the largest
6462 type that fits. */
6465 --itk;
6466 integer_type = integer_types[itk];
6467 } while (itk > 0 && integer_type == NULL_TREE);
6469 /* Figure out how much additional padding is required. */
6470 if (tree_int_cst_lt (TYPE_SIZE (integer_type), DECL_SIZE (field)))
6472 if (TREE_CODE (t) == UNION_TYPE)
6473 /* In a union, the padding field must have the full width
6474 of the bit-field; all fields start at offset zero. */
6475 padding = DECL_SIZE (field);
6476 else
6477 padding = size_binop (MINUS_EXPR, DECL_SIZE (field),
6478 TYPE_SIZE (integer_type));
6481 /* An unnamed bitfield does not normally affect the
6482 alignment of the containing class on a target where
6483 PCC_BITFIELD_TYPE_MATTERS. But, the C++ ABI does not
6484 make any exceptions for unnamed bitfields when the
6485 bitfields are longer than their types. Therefore, we
6486 temporarily give the field a name. */
6487 if (PCC_BITFIELD_TYPE_MATTERS && !DECL_NAME (field))
6489 was_unnamed_p = true;
6490 DECL_NAME (field) = make_anon_name ();
6493 DECL_SIZE (field) = TYPE_SIZE (integer_type);
6494 SET_DECL_ALIGN (field, TYPE_ALIGN (integer_type));
6495 DECL_USER_ALIGN (field) = TYPE_USER_ALIGN (integer_type);
6496 layout_nonempty_base_or_field (rli, field, NULL_TREE,
6497 empty_base_offsets);
6498 if (was_unnamed_p)
6499 DECL_NAME (field) = NULL_TREE;
6500 /* Now that layout has been performed, set the size of the
6501 field to the size of its declared type; the rest of the
6502 field is effectively invisible. */
6503 DECL_SIZE (field) = TYPE_SIZE (type);
6504 /* We must also reset the DECL_MODE of the field. */
6505 SET_DECL_MODE (field, TYPE_MODE (type));
6507 else
6508 layout_nonempty_base_or_field (rli, field, NULL_TREE,
6509 empty_base_offsets);
6511 /* Remember the location of any empty classes in FIELD. */
6512 record_subobject_offsets (TREE_TYPE (field),
6513 byte_position(field),
6514 empty_base_offsets,
6515 /*is_data_member=*/true);
6517 /* If a bit-field does not immediately follow another bit-field,
6518 and yet it starts in the middle of a byte, we have failed to
6519 comply with the ABI. */
6520 if (warn_abi
6521 && DECL_C_BIT_FIELD (field)
6522 /* The TREE_NO_WARNING flag gets set by Objective-C when
6523 laying out an Objective-C class. The ObjC ABI differs
6524 from the C++ ABI, and so we do not want a warning
6525 here. */
6526 && !TREE_NO_WARNING (field)
6527 && !last_field_was_bitfield
6528 && !integer_zerop (size_binop (TRUNC_MOD_EXPR,
6529 DECL_FIELD_BIT_OFFSET (field),
6530 bitsize_unit_node)))
6531 warning_at (DECL_SOURCE_LOCATION (field), OPT_Wabi,
6532 "offset of %qD is not ABI-compliant and may "
6533 "change in a future version of GCC", field);
6535 /* The middle end uses the type of expressions to determine the
6536 possible range of expression values. In order to optimize
6537 "x.i > 7" to "false" for a 2-bit bitfield "i", the middle end
6538 must be made aware of the width of "i", via its type.
6540 Because C++ does not have integer types of arbitrary width,
6541 we must (for the purposes of the front end) convert from the
6542 type assigned here to the declared type of the bitfield
6543 whenever a bitfield expression is used as an rvalue.
6544 Similarly, when assigning a value to a bitfield, the value
6545 must be converted to the type given the bitfield here. */
6546 if (DECL_C_BIT_FIELD (field))
6548 unsigned HOST_WIDE_INT width;
6549 tree ftype = TREE_TYPE (field);
6550 width = tree_to_uhwi (DECL_SIZE (field));
6551 if (width != TYPE_PRECISION (ftype))
6553 TREE_TYPE (field)
6554 = c_build_bitfield_integer_type (width,
6555 TYPE_UNSIGNED (ftype));
6556 TREE_TYPE (field)
6557 = cp_build_qualified_type (TREE_TYPE (field),
6558 cp_type_quals (ftype));
6562 /* If we needed additional padding after this field, add it
6563 now. */
6564 if (padding)
6566 tree padding_field;
6568 padding_field = build_decl (input_location,
6569 FIELD_DECL,
6570 NULL_TREE,
6571 char_type_node);
6572 DECL_BIT_FIELD (padding_field) = 1;
6573 DECL_SIZE (padding_field) = padding;
6574 DECL_CONTEXT (padding_field) = t;
6575 DECL_ARTIFICIAL (padding_field) = 1;
6576 DECL_IGNORED_P (padding_field) = 1;
6577 layout_nonempty_base_or_field (rli, padding_field,
6578 NULL_TREE,
6579 empty_base_offsets);
6582 last_field_was_bitfield = DECL_C_BIT_FIELD (field);
6585 if (!integer_zerop (rli->bitpos))
6587 /* Make sure that we are on a byte boundary so that the size of
6588 the class without virtual bases will always be a round number
6589 of bytes. */
6590 rli->bitpos = round_up_loc (input_location, rli->bitpos, BITS_PER_UNIT);
6591 normalize_rli (rli);
6594 /* Delete all zero-width bit-fields from the list of fields. Now
6595 that the type is laid out they are no longer important. */
6596 remove_zero_width_bit_fields (t);
6598 /* Create the version of T used for virtual bases. We do not use
6599 make_class_type for this version; this is an artificial type. For
6600 a POD type, we just reuse T. */
6601 if (CLASSTYPE_NON_LAYOUT_POD_P (t) || CLASSTYPE_EMPTY_P (t))
6603 base_t = make_node (TREE_CODE (t));
6605 /* Set the size and alignment for the new type. */
6606 tree eoc;
6608 /* If the ABI version is not at least two, and the last
6609 field was a bit-field, RLI may not be on a byte
6610 boundary. In particular, rli_size_unit_so_far might
6611 indicate the last complete byte, while rli_size_so_far
6612 indicates the total number of bits used. Therefore,
6613 rli_size_so_far, rather than rli_size_unit_so_far, is
6614 used to compute TYPE_SIZE_UNIT. */
6615 eoc = end_of_class (t, /*include_virtuals_p=*/0);
6616 TYPE_SIZE_UNIT (base_t)
6617 = size_binop (MAX_EXPR,
6618 fold_convert (sizetype,
6619 size_binop (CEIL_DIV_EXPR,
6620 rli_size_so_far (rli),
6621 bitsize_int (BITS_PER_UNIT))),
6622 eoc);
6623 TYPE_SIZE (base_t)
6624 = size_binop (MAX_EXPR,
6625 rli_size_so_far (rli),
6626 size_binop (MULT_EXPR,
6627 fold_convert (bitsizetype, eoc),
6628 bitsize_int (BITS_PER_UNIT)));
6629 SET_TYPE_ALIGN (base_t, rli->record_align);
6630 TYPE_USER_ALIGN (base_t) = TYPE_USER_ALIGN (t);
6632 /* Copy the fields from T. */
6633 next_field = &TYPE_FIELDS (base_t);
6634 for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
6635 if (TREE_CODE (field) == FIELD_DECL)
6637 *next_field = copy_node (field);
6638 DECL_CONTEXT (*next_field) = base_t;
6639 next_field = &DECL_CHAIN (*next_field);
6641 *next_field = NULL_TREE;
6643 /* Record the base version of the type. */
6644 CLASSTYPE_AS_BASE (t) = base_t;
6645 TYPE_CONTEXT (base_t) = t;
6647 else
6648 CLASSTYPE_AS_BASE (t) = t;
6650 /* Every empty class contains an empty class. */
6651 if (CLASSTYPE_EMPTY_P (t))
6652 CLASSTYPE_CONTAINS_EMPTY_CLASS_P (t) = 1;
6654 /* Set the TYPE_DECL for this type to contain the right
6655 value for DECL_OFFSET, so that we can use it as part
6656 of a COMPONENT_REF for multiple inheritance. */
6657 layout_decl (TYPE_MAIN_DECL (t), 0);
6659 /* Now fix up any virtual base class types that we left lying
6660 around. We must get these done before we try to lay out the
6661 virtual function table. As a side-effect, this will remove the
6662 base subobject fields. */
6663 layout_virtual_bases (rli, empty_base_offsets);
6665 /* Make sure that empty classes are reflected in RLI at this
6666 point. */
6667 include_empty_classes (rli);
6669 /* Make sure not to create any structures with zero size. */
6670 if (integer_zerop (rli_size_unit_so_far (rli)) && CLASSTYPE_EMPTY_P (t))
6671 place_field (rli,
6672 build_decl (input_location,
6673 FIELD_DECL, NULL_TREE, char_type_node));
6675 /* If this is a non-POD, declaring it packed makes a difference to how it
6676 can be used as a field; don't let finalize_record_size undo it. */
6677 if (TYPE_PACKED (t) && !layout_pod_type_p (t))
6678 rli->packed_maybe_necessary = true;
6680 /* Let the back end lay out the type. */
6681 finish_record_layout (rli, /*free_p=*/true);
6683 if (TYPE_SIZE_UNIT (t)
6684 && TREE_CODE (TYPE_SIZE_UNIT (t)) == INTEGER_CST
6685 && !TREE_OVERFLOW (TYPE_SIZE_UNIT (t))
6686 && !valid_constant_size_p (TYPE_SIZE_UNIT (t)))
6687 error ("size of type %qT is too large (%qE bytes)", t, TYPE_SIZE_UNIT (t));
6689 /* Warn about bases that can't be talked about due to ambiguity. */
6690 warn_about_ambiguous_bases (t);
6692 /* Now that we're done with layout, give the base fields the real types. */
6693 for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
6694 if (DECL_ARTIFICIAL (field) && IS_FAKE_BASE_TYPE (TREE_TYPE (field)))
6695 TREE_TYPE (field) = TYPE_CONTEXT (TREE_TYPE (field));
6697 /* Clean up. */
6698 splay_tree_delete (empty_base_offsets);
6700 if (CLASSTYPE_EMPTY_P (t)
6701 && tree_int_cst_lt (sizeof_biggest_empty_class,
6702 TYPE_SIZE_UNIT (t)))
6703 sizeof_biggest_empty_class = TYPE_SIZE_UNIT (t);
6706 /* Determine the "key method" for the class type indicated by TYPE,
6707 and set CLASSTYPE_KEY_METHOD accordingly. */
6709 void
6710 determine_key_method (tree type)
6712 tree method;
6714 if (processing_template_decl
6715 || CLASSTYPE_TEMPLATE_INSTANTIATION (type)
6716 || CLASSTYPE_INTERFACE_KNOWN (type))
6717 return;
6719 /* The key method is the first non-pure virtual function that is not
6720 inline at the point of class definition. On some targets the
6721 key function may not be inline; those targets should not call
6722 this function until the end of the translation unit. */
6723 for (method = TYPE_METHODS (type); method != NULL_TREE;
6724 method = DECL_CHAIN (method))
6725 if (TREE_CODE (method) == FUNCTION_DECL
6726 && DECL_VINDEX (method) != NULL_TREE
6727 && ! DECL_DECLARED_INLINE_P (method)
6728 && ! DECL_PURE_VIRTUAL_P (method))
6730 CLASSTYPE_KEY_METHOD (type) = method;
6731 break;
6734 return;
6738 /* Allocate and return an instance of struct sorted_fields_type with
6739 N fields. */
6741 static struct sorted_fields_type *
6742 sorted_fields_type_new (int n)
6744 struct sorted_fields_type *sft;
6745 sft = (sorted_fields_type *) ggc_internal_alloc (sizeof (sorted_fields_type)
6746 + n * sizeof (tree));
6747 sft->len = n;
6749 return sft;
6752 /* Helper of find_flexarrays. Return true when FLD refers to a non-static
6753 class data member of non-zero size, otherwise false. */
6755 static inline bool
6756 field_nonempty_p (const_tree fld)
6758 if (TREE_CODE (fld) == ERROR_MARK)
6759 return false;
6761 tree type = TREE_TYPE (fld);
6762 if (TREE_CODE (fld) == FIELD_DECL
6763 && TREE_CODE (type) != ERROR_MARK
6764 && (DECL_NAME (fld) || RECORD_OR_UNION_TYPE_P (type)))
6766 return TYPE_SIZE (type)
6767 && (TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST
6768 || !tree_int_cst_equal (size_zero_node, TYPE_SIZE (type)));
6771 return false;
6774 /* Used by find_flexarrays and related functions. */
6776 struct flexmems_t
6778 /* The first flexible array member or non-zero array member found
6779 in the order of layout. */
6780 tree array;
6781 /* First non-static non-empty data member in the class or its bases. */
6782 tree first;
6783 /* The first non-static non-empty data member following either
6784 the flexible array member, if found, or the zero-length array member
6785 otherwise. AFTER[1] refers to the first such data member of a union
6786 of which the struct containing the flexible array member or zero-length
6787 array is a member, or NULL when no such union exists. This element is
6788 only used during searching, not for diagnosing problems. AFTER[0]
6789 refers to the first such data member that is not a member of such
6790 a union. */
6791 tree after[2];
6793 /* Refers to a struct (not union) in which the struct of which the flexible
6794 array is member is defined. Used to diagnose strictly (according to C)
6795 invalid uses of the latter structs. */
6796 tree enclosing;
6799 /* Find either the first flexible array member or the first zero-length
6800 array, in that order of preference, among members of class T (but not
6801 its base classes), and set members of FMEM accordingly.
6802 BASE_P is true if T is a base class of another class.
6803 PUN is set to the outermost union in which the flexible array member
6804 (or zero-length array) is defined if one such union exists, otherwise
6805 to NULL.
6806 Similarly, PSTR is set to a data member of the outermost struct of
6807 which the flexible array is a member if one such struct exists,
6808 otherwise to NULL. */
6810 static void
6811 find_flexarrays (tree t, flexmems_t *fmem, bool base_p,
6812 tree pun /* = NULL_TREE */,
6813 tree pstr /* = NULL_TREE */)
6815 /* Set the "pointer" to the outermost enclosing union if not set
6816 yet and maintain it for the remainder of the recursion. */
6817 if (!pun && TREE_CODE (t) == UNION_TYPE)
6818 pun = t;
6820 for (tree fld = TYPE_FIELDS (t); fld; fld = DECL_CHAIN (fld))
6822 if (fld == error_mark_node)
6823 return;
6825 /* Is FLD a typedef for an anonymous struct? */
6827 /* FIXME: Note that typedefs (as well as arrays) need to be fully
6828 handled elsewhere so that errors like the following are detected
6829 as well:
6830 typedef struct { int i, a[], j; } S; // bug c++/72753
6831 S s [2]; // bug c++/68489
6833 if (TREE_CODE (fld) == TYPE_DECL
6834 && DECL_IMPLICIT_TYPEDEF_P (fld)
6835 && CLASS_TYPE_P (TREE_TYPE (fld))
6836 && anon_aggrname_p (DECL_NAME (fld)))
6838 /* Check the nested unnamed type referenced via a typedef
6839 independently of FMEM (since it's not a data member of
6840 the enclosing class). */
6841 check_flexarrays (TREE_TYPE (fld));
6842 continue;
6845 /* Skip anything that's GCC-generated or not a (non-static) data
6846 member. */
6847 if (DECL_ARTIFICIAL (fld) || TREE_CODE (fld) != FIELD_DECL)
6848 continue;
6850 /* Type of the member. */
6851 tree fldtype = TREE_TYPE (fld);
6852 if (fldtype == error_mark_node)
6853 return;
6855 /* Determine the type of the array element or object referenced
6856 by the member so that it can be checked for flexible array
6857 members if it hasn't been yet. */
6858 tree eltype = fldtype;
6859 while (TREE_CODE (eltype) == ARRAY_TYPE
6860 || TREE_CODE (eltype) == POINTER_TYPE
6861 || TREE_CODE (eltype) == REFERENCE_TYPE)
6862 eltype = TREE_TYPE (eltype);
6864 if (RECORD_OR_UNION_TYPE_P (eltype))
6866 if (fmem->array && !fmem->after[bool (pun)])
6868 /* Once the member after the flexible array has been found
6869 we're done. */
6870 fmem->after[bool (pun)] = fld;
6871 break;
6874 if (eltype == fldtype || TYPE_UNNAMED_P (eltype))
6876 /* Descend into the non-static member struct or union and try
6877 to find a flexible array member or zero-length array among
6878 its members. This is only necessary for anonymous types
6879 and types in whose context the current type T has not been
6880 defined (the latter must not be checked again because they
6881 are already in the process of being checked by one of the
6882 recursive calls). */
6884 tree first = fmem->first;
6885 tree array = fmem->array;
6887 /* If this member isn't anonymous and a prior non-flexible array
6888 member has been seen in one of the enclosing structs, clear
6889 the FIRST member since it doesn't contribute to the flexible
6890 array struct's members. */
6891 if (first && !array && !ANON_AGGR_TYPE_P (eltype))
6892 fmem->first = NULL_TREE;
6894 find_flexarrays (eltype, fmem, false, pun,
6895 !pstr && TREE_CODE (t) == RECORD_TYPE ? fld : pstr);
6897 if (fmem->array != array)
6898 continue;
6900 if (first && !array && !ANON_AGGR_TYPE_P (eltype))
6902 /* Restore the FIRST member reset above if no flexible
6903 array member has been found in this member's struct. */
6904 fmem->first = first;
6907 /* If the member struct contains the first flexible array
6908 member, or if this member is a base class, continue to
6909 the next member and avoid setting the FMEM->NEXT pointer
6910 to point to it. */
6911 if (base_p)
6912 continue;
6916 if (field_nonempty_p (fld))
6918 /* Remember the first non-static data member. */
6919 if (!fmem->first)
6920 fmem->first = fld;
6922 /* Remember the first non-static data member after the flexible
6923 array member, if one has been found, or the zero-length array
6924 if it has been found. */
6925 if (fmem->array && !fmem->after[bool (pun)])
6926 fmem->after[bool (pun)] = fld;
6929 /* Skip non-arrays. */
6930 if (TREE_CODE (fldtype) != ARRAY_TYPE)
6931 continue;
6933 /* Determine the upper bound of the array if it has one. */
6934 if (TYPE_DOMAIN (fldtype))
6936 if (fmem->array)
6938 /* Make a record of the zero-length array if either one
6939 such field or a flexible array member has been seen to
6940 handle the pathological and unlikely case of multiple
6941 such members. */
6942 if (!fmem->after[bool (pun)])
6943 fmem->after[bool (pun)] = fld;
6945 else if (integer_all_onesp (TYPE_MAX_VALUE (TYPE_DOMAIN (fldtype))))
6947 /* Remember the first zero-length array unless a flexible array
6948 member has already been seen. */
6949 fmem->array = fld;
6950 fmem->enclosing = pstr;
6953 else
6955 /* Flexible array members have no upper bound. */
6956 if (fmem->array)
6958 /* Replace the zero-length array if it's been stored and
6959 reset the after pointer. */
6960 if (TYPE_DOMAIN (TREE_TYPE (fmem->array)))
6962 fmem->after[bool (pun)] = NULL_TREE;
6963 fmem->array = fld;
6964 fmem->enclosing = pstr;
6967 else
6969 fmem->array = fld;
6970 fmem->enclosing = pstr;
6976 /* Diagnose a strictly (by the C standard) invalid use of a struct with
6977 a flexible array member (or the zero-length array extension). */
6979 static void
6980 diagnose_invalid_flexarray (const flexmems_t *fmem)
6982 if (fmem->array && fmem->enclosing
6983 && pedwarn (location_of (fmem->enclosing), OPT_Wpedantic,
6984 TYPE_DOMAIN (TREE_TYPE (fmem->array))
6985 ? G_("invalid use of %q#T with a zero-size array "
6986 "in %q#D")
6987 : G_("invalid use of %q#T with a flexible array member "
6988 "in %q#T"),
6989 DECL_CONTEXT (fmem->array),
6990 DECL_CONTEXT (fmem->enclosing)))
6991 inform (DECL_SOURCE_LOCATION (fmem->array),
6992 "array member %q#D declared here", fmem->array);
6995 /* Issue diagnostics for invalid flexible array members or zero-length
6996 arrays that are not the last elements of the containing class or its
6997 base classes or that are its sole members. */
6999 static void
7000 diagnose_flexarrays (tree t, const flexmems_t *fmem)
7002 if (!fmem->array)
7003 return;
7005 if (fmem->first && !fmem->after[0])
7007 diagnose_invalid_flexarray (fmem);
7008 return;
7011 /* Has a diagnostic been issued? */
7012 bool diagd = false;
7014 const char *msg = 0;
7016 if (TYPE_DOMAIN (TREE_TYPE (fmem->array)))
7018 if (fmem->after[0])
7019 msg = G_("zero-size array member %qD not at end of %q#T");
7020 else if (!fmem->first)
7021 msg = G_("zero-size array member %qD in an otherwise empty %q#T");
7023 if (msg)
7025 location_t loc = DECL_SOURCE_LOCATION (fmem->array);
7027 if (pedwarn (loc, OPT_Wpedantic, msg, fmem->array, t))
7029 inform (location_of (t), "in the definition of %q#T", t);
7030 diagd = true;
7034 else
7036 if (fmem->after[0])
7037 msg = G_("flexible array member %qD not at end of %q#T");
7038 else if (!fmem->first)
7039 msg = G_("flexible array member %qD in an otherwise empty %q#T");
7041 if (msg)
7043 location_t loc = DECL_SOURCE_LOCATION (fmem->array);
7044 diagd = true;
7046 error_at (loc, msg, fmem->array, t);
7048 /* In the unlikely event that the member following the flexible
7049 array member is declared in a different class, or the member
7050 overlaps another member of a common union, point to it.
7051 Otherwise it should be obvious. */
7052 if (fmem->after[0]
7053 && ((DECL_CONTEXT (fmem->after[0])
7054 != DECL_CONTEXT (fmem->array))))
7056 inform (DECL_SOURCE_LOCATION (fmem->after[0]),
7057 "next member %q#D declared here",
7058 fmem->after[0]);
7059 inform (location_of (t), "in the definition of %q#T", t);
7064 if (!diagd && fmem->array && fmem->enclosing)
7065 diagnose_invalid_flexarray (fmem);
7069 /* Recursively check to make sure that any flexible array or zero-length
7070 array members of class T or its bases are valid (i.e., not the sole
7071 non-static data member of T and, if one exists, that it is the last
7072 non-static data member of T and its base classes. FMEM is expected
7073 to be initially null and is used internally by recursive calls to
7074 the function. Issue the appropriate diagnostics for the array member
7075 that fails the checks. */
7077 static void
7078 check_flexarrays (tree t, flexmems_t *fmem /* = NULL */,
7079 bool base_p /* = false */)
7081 /* Initialize the result of a search for flexible array and zero-length
7082 array members. Avoid doing any work if the most interesting FMEM data
7083 have already been populated. */
7084 flexmems_t flexmems = flexmems_t ();
7085 if (!fmem)
7086 fmem = &flexmems;
7087 else if (fmem->array && fmem->first && fmem->after[0])
7088 return;
7090 tree fam = fmem->array;
7092 /* Recursively check the primary base class first. */
7093 if (CLASSTYPE_HAS_PRIMARY_BASE_P (t))
7095 tree basetype = BINFO_TYPE (CLASSTYPE_PRIMARY_BINFO (t));
7096 check_flexarrays (basetype, fmem, true);
7099 /* Recursively check the base classes. */
7100 int nbases = TYPE_BINFO (t) ? BINFO_N_BASE_BINFOS (TYPE_BINFO (t)) : 0;
7101 for (int i = 0; i < nbases; ++i)
7103 tree base_binfo = BINFO_BASE_BINFO (TYPE_BINFO (t), i);
7105 /* The primary base class was already checked above. */
7106 if (base_binfo == CLASSTYPE_PRIMARY_BINFO (t))
7107 continue;
7109 /* Virtual base classes are at the end. */
7110 if (BINFO_VIRTUAL_P (base_binfo))
7111 continue;
7113 /* Check the base class. */
7114 check_flexarrays (BINFO_TYPE (base_binfo), fmem, /*base_p=*/true);
7117 if (fmem == &flexmems)
7119 /* Check virtual base classes only once per derived class.
7120 I.e., this check is not performed recursively for base
7121 classes. */
7122 int i;
7123 tree base_binfo;
7124 vec<tree, va_gc> *vbases;
7125 for (vbases = CLASSTYPE_VBASECLASSES (t), i = 0;
7126 vec_safe_iterate (vbases, i, &base_binfo); i++)
7128 /* Check the virtual base class. */
7129 tree basetype = TREE_TYPE (base_binfo);
7131 check_flexarrays (basetype, fmem, /*base_p=*/true);
7135 /* Is the type unnamed (and therefore a member of it potentially
7136 an anonymous struct or union)? */
7137 bool maybe_anon_p = TYPE_UNNAMED_P (t);
7139 /* Search the members of the current (possibly derived) class, skipping
7140 unnamed structs and unions since those could be anonymous. */
7141 if (fmem != &flexmems || !maybe_anon_p)
7142 find_flexarrays (t, fmem, base_p || fam != fmem->array);
7144 if (fmem == &flexmems && !maybe_anon_p)
7146 /* Issue diagnostics for invalid flexible and zero-length array
7147 members found in base classes or among the members of the current
7148 class. Ignore anonymous structs and unions whose members are
7149 considered to be members of the enclosing class and thus will
7150 be diagnosed when checking it. */
7151 diagnose_flexarrays (t, fmem);
7155 /* Perform processing required when the definition of T (a class type)
7156 is complete. Diagnose invalid definitions of flexible array members
7157 and zero-size arrays. */
7159 void
7160 finish_struct_1 (tree t)
7162 tree x;
7163 /* A TREE_LIST. The TREE_VALUE of each node is a FUNCTION_DECL. */
7164 tree virtuals = NULL_TREE;
7166 if (COMPLETE_TYPE_P (t))
7168 gcc_assert (MAYBE_CLASS_TYPE_P (t));
7169 error ("redefinition of %q#T", t);
7170 popclass ();
7171 return;
7174 /* If this type was previously laid out as a forward reference,
7175 make sure we lay it out again. */
7176 TYPE_SIZE (t) = NULL_TREE;
7177 CLASSTYPE_PRIMARY_BINFO (t) = NULL_TREE;
7179 /* Make assumptions about the class; we'll reset the flags if
7180 necessary. */
7181 CLASSTYPE_EMPTY_P (t) = 1;
7182 CLASSTYPE_NEARLY_EMPTY_P (t) = 1;
7183 CLASSTYPE_CONTAINS_EMPTY_CLASS_P (t) = 0;
7184 CLASSTYPE_LITERAL_P (t) = true;
7186 /* Do end-of-class semantic processing: checking the validity of the
7187 bases and members and add implicitly generated methods. */
7188 check_bases_and_members (t);
7190 /* Find the key method. */
7191 if (TYPE_CONTAINS_VPTR_P (t))
7193 /* The Itanium C++ ABI permits the key method to be chosen when
7194 the class is defined -- even though the key method so
7195 selected may later turn out to be an inline function. On
7196 some systems (such as ARM Symbian OS) the key method cannot
7197 be determined until the end of the translation unit. On such
7198 systems, we leave CLASSTYPE_KEY_METHOD set to NULL, which
7199 will cause the class to be added to KEYED_CLASSES. Then, in
7200 finish_file we will determine the key method. */
7201 if (targetm.cxx.key_method_may_be_inline ())
7202 determine_key_method (t);
7204 /* If a polymorphic class has no key method, we may emit the vtable
7205 in every translation unit where the class definition appears. If
7206 we're devirtualizing, we can look into the vtable even if we
7207 aren't emitting it. */
7208 if (CLASSTYPE_KEY_METHOD (t) == NULL_TREE)
7209 keyed_classes = tree_cons (NULL_TREE, t, keyed_classes);
7212 /* Layout the class itself. */
7213 layout_class_type (t, &virtuals);
7214 if (CLASSTYPE_AS_BASE (t) != t)
7215 /* We use the base type for trivial assignments, and hence it
7216 needs a mode. */
7217 compute_record_mode (CLASSTYPE_AS_BASE (t));
7219 /* With the layout complete, check for flexible array members and
7220 zero-length arrays that might overlap other members in the final
7221 layout. */
7222 check_flexarrays (t);
7224 virtuals = modify_all_vtables (t, nreverse (virtuals));
7226 /* If necessary, create the primary vtable for this class. */
7227 if (virtuals || TYPE_CONTAINS_VPTR_P (t))
7229 /* We must enter these virtuals into the table. */
7230 if (!CLASSTYPE_HAS_PRIMARY_BASE_P (t))
7231 build_primary_vtable (NULL_TREE, t);
7232 else if (! BINFO_NEW_VTABLE_MARKED (TYPE_BINFO (t)))
7233 /* Here we know enough to change the type of our virtual
7234 function table, but we will wait until later this function. */
7235 build_primary_vtable (CLASSTYPE_PRIMARY_BINFO (t), t);
7237 /* If we're warning about ABI tags, check the types of the new
7238 virtual functions. */
7239 if (warn_abi_tag)
7240 for (tree v = virtuals; v; v = TREE_CHAIN (v))
7241 check_abi_tags (t, TREE_VALUE (v));
7244 if (TYPE_CONTAINS_VPTR_P (t))
7246 int vindex;
7247 tree fn;
7249 if (BINFO_VTABLE (TYPE_BINFO (t)))
7250 gcc_assert (DECL_VIRTUAL_P (BINFO_VTABLE (TYPE_BINFO (t))));
7251 if (!CLASSTYPE_HAS_PRIMARY_BASE_P (t))
7252 gcc_assert (BINFO_VIRTUALS (TYPE_BINFO (t)) == NULL_TREE);
7254 /* Add entries for virtual functions introduced by this class. */
7255 BINFO_VIRTUALS (TYPE_BINFO (t))
7256 = chainon (BINFO_VIRTUALS (TYPE_BINFO (t)), virtuals);
7258 /* Set DECL_VINDEX for all functions declared in this class. */
7259 for (vindex = 0, fn = BINFO_VIRTUALS (TYPE_BINFO (t));
7261 fn = TREE_CHAIN (fn),
7262 vindex += (TARGET_VTABLE_USES_DESCRIPTORS
7263 ? TARGET_VTABLE_USES_DESCRIPTORS : 1))
7265 tree fndecl = BV_FN (fn);
7267 if (DECL_THUNK_P (fndecl))
7268 /* A thunk. We should never be calling this entry directly
7269 from this vtable -- we'd use the entry for the non
7270 thunk base function. */
7271 DECL_VINDEX (fndecl) = NULL_TREE;
7272 else if (TREE_CODE (DECL_VINDEX (fndecl)) != INTEGER_CST)
7273 DECL_VINDEX (fndecl) = build_int_cst (NULL_TREE, vindex);
7277 finish_struct_bits (t);
7278 set_method_tm_attributes (t);
7279 if (flag_openmp || flag_openmp_simd)
7280 finish_omp_declare_simd_methods (t);
7282 /* Complete the rtl for any static member objects of the type we're
7283 working on. */
7284 for (x = TYPE_FIELDS (t); x; x = DECL_CHAIN (x))
7285 if (VAR_P (x) && TREE_STATIC (x)
7286 && TREE_TYPE (x) != error_mark_node
7287 && same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (x)), t))
7288 SET_DECL_MODE (x, TYPE_MODE (t));
7290 /* Done with FIELDS...now decide whether to sort these for
7291 faster lookups later.
7293 We use a small number because most searches fail (succeeding
7294 ultimately as the search bores through the inheritance
7295 hierarchy), and we want this failure to occur quickly. */
7297 insert_into_classtype_sorted_fields (TYPE_FIELDS (t), t, 8);
7299 /* Complain if one of the field types requires lower visibility. */
7300 constrain_class_visibility (t);
7302 /* Make the rtl for any new vtables we have created, and unmark
7303 the base types we marked. */
7304 finish_vtbls (t);
7306 /* Build the VTT for T. */
7307 build_vtt (t);
7309 if (warn_nonvdtor
7310 && TYPE_POLYMORPHIC_P (t) && accessible_nvdtor_p (t)
7311 && !CLASSTYPE_FINAL (t))
7312 warning (OPT_Wnon_virtual_dtor,
7313 "%q#T has virtual functions and accessible"
7314 " non-virtual destructor", t);
7316 complete_vars (t);
7318 if (warn_overloaded_virtual)
7319 warn_hidden (t);
7321 /* Class layout, assignment of virtual table slots, etc., is now
7322 complete. Give the back end a chance to tweak the visibility of
7323 the class or perform any other required target modifications. */
7324 targetm.cxx.adjust_class_at_definition (t);
7326 maybe_suppress_debug_info (t);
7328 if (flag_vtable_verify)
7329 vtv_save_class_info (t);
7331 dump_class_hierarchy (t);
7333 /* Finish debugging output for this type. */
7334 rest_of_type_compilation (t, ! LOCAL_CLASS_P (t));
7336 if (TYPE_TRANSPARENT_AGGR (t))
7338 tree field = first_field (t);
7339 if (field == NULL_TREE || error_operand_p (field))
7341 error ("type transparent %q#T does not have any fields", t);
7342 TYPE_TRANSPARENT_AGGR (t) = 0;
7344 else if (DECL_ARTIFICIAL (field))
7346 if (DECL_FIELD_IS_BASE (field))
7347 error ("type transparent class %qT has base classes", t);
7348 else
7350 gcc_checking_assert (DECL_VIRTUAL_P (field));
7351 error ("type transparent class %qT has virtual functions", t);
7353 TYPE_TRANSPARENT_AGGR (t) = 0;
7355 else if (TYPE_MODE (t) != DECL_MODE (field))
7357 error ("type transparent %q#T cannot be made transparent because "
7358 "the type of the first field has a different ABI from the "
7359 "class overall", t);
7360 TYPE_TRANSPARENT_AGGR (t) = 0;
7365 /* Insert FIELDS into T for the sorted case if the FIELDS count is
7366 equal to THRESHOLD or greater than THRESHOLD. */
7368 static void
7369 insert_into_classtype_sorted_fields (tree fields, tree t, int threshold)
7371 int n_fields = count_fields (fields);
7372 if (n_fields >= threshold)
7374 struct sorted_fields_type *field_vec = sorted_fields_type_new (n_fields);
7375 add_fields_to_record_type (fields, field_vec, 0);
7376 qsort (field_vec->elts, n_fields, sizeof (tree), field_decl_cmp);
7377 CLASSTYPE_SORTED_FIELDS (t) = field_vec;
7381 /* Insert lately defined enum ENUMTYPE into T for the sorted case. */
7383 void
7384 insert_late_enum_def_into_classtype_sorted_fields (tree enumtype, tree t)
7386 struct sorted_fields_type *sorted_fields = CLASSTYPE_SORTED_FIELDS (t);
7387 if (sorted_fields)
7389 int i;
7390 int n_fields
7391 = list_length (TYPE_VALUES (enumtype)) + sorted_fields->len;
7392 struct sorted_fields_type *field_vec = sorted_fields_type_new (n_fields);
7394 for (i = 0; i < sorted_fields->len; ++i)
7395 field_vec->elts[i] = sorted_fields->elts[i];
7397 add_enum_fields_to_record_type (enumtype, field_vec,
7398 sorted_fields->len);
7399 qsort (field_vec->elts, n_fields, sizeof (tree), field_decl_cmp);
7400 CLASSTYPE_SORTED_FIELDS (t) = field_vec;
7404 /* When T was built up, the member declarations were added in reverse
7405 order. Rearrange them to declaration order. */
7407 void
7408 unreverse_member_declarations (tree t)
7410 tree next;
7411 tree prev;
7412 tree x;
7414 /* The following lists are all in reverse order. Put them in
7415 declaration order now. */
7416 TYPE_METHODS (t) = nreverse (TYPE_METHODS (t));
7417 CLASSTYPE_DECL_LIST (t) = nreverse (CLASSTYPE_DECL_LIST (t));
7419 /* Actually, for the TYPE_FIELDS, only the non TYPE_DECLs are in
7420 reverse order, so we can't just use nreverse. */
7421 prev = NULL_TREE;
7422 for (x = TYPE_FIELDS (t);
7423 x && TREE_CODE (x) != TYPE_DECL;
7424 x = next)
7426 next = DECL_CHAIN (x);
7427 DECL_CHAIN (x) = prev;
7428 prev = x;
7430 if (prev)
7432 DECL_CHAIN (TYPE_FIELDS (t)) = x;
7433 if (prev)
7434 TYPE_FIELDS (t) = prev;
7438 tree
7439 finish_struct (tree t, tree attributes)
7441 location_t saved_loc = input_location;
7443 /* Now that we've got all the field declarations, reverse everything
7444 as necessary. */
7445 unreverse_member_declarations (t);
7447 cplus_decl_attributes (&t, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
7448 fixup_attribute_variants (t);
7450 /* Nadger the current location so that diagnostics point to the start of
7451 the struct, not the end. */
7452 input_location = DECL_SOURCE_LOCATION (TYPE_NAME (t));
7454 if (processing_template_decl)
7456 tree x;
7458 finish_struct_methods (t);
7459 TYPE_SIZE (t) = bitsize_zero_node;
7460 TYPE_SIZE_UNIT (t) = size_zero_node;
7462 /* We need to emit an error message if this type was used as a parameter
7463 and it is an abstract type, even if it is a template. We construct
7464 a simple CLASSTYPE_PURE_VIRTUALS list without taking bases into
7465 account and we call complete_vars with this type, which will check
7466 the PARM_DECLS. Note that while the type is being defined,
7467 CLASSTYPE_PURE_VIRTUALS contains the list of the inline friends
7468 (see CLASSTYPE_INLINE_FRIENDS) so we need to clear it. */
7469 CLASSTYPE_PURE_VIRTUALS (t) = NULL;
7470 for (x = TYPE_METHODS (t); x; x = DECL_CHAIN (x))
7471 if (DECL_PURE_VIRTUAL_P (x))
7472 vec_safe_push (CLASSTYPE_PURE_VIRTUALS (t), x);
7473 complete_vars (t);
7474 /* We need to add the target functions to the CLASSTYPE_METHOD_VEC if
7475 an enclosing scope is a template class, so that this function be
7476 found by lookup_fnfields_1 when the using declaration is not
7477 instantiated yet. */
7478 for (x = TYPE_FIELDS (t); x; x = DECL_CHAIN (x))
7479 if (TREE_CODE (x) == USING_DECL)
7481 tree fn = strip_using_decl (x);
7482 if (is_overloaded_fn (fn))
7483 for (; fn; fn = OVL_NEXT (fn))
7484 add_method (t, OVL_CURRENT (fn), x);
7487 /* Remember current #pragma pack value. */
7488 TYPE_PRECISION (t) = maximum_field_alignment;
7490 /* Fix up any variants we've already built. */
7491 for (x = TYPE_NEXT_VARIANT (t); x; x = TYPE_NEXT_VARIANT (x))
7493 TYPE_SIZE (x) = TYPE_SIZE (t);
7494 TYPE_SIZE_UNIT (x) = TYPE_SIZE_UNIT (t);
7495 TYPE_FIELDS (x) = TYPE_FIELDS (t);
7496 TYPE_METHODS (x) = TYPE_METHODS (t);
7499 else
7500 finish_struct_1 (t);
7502 if (is_std_init_list (t))
7504 /* People keep complaining that the compiler crashes on an invalid
7505 definition of initializer_list, so I guess we should explicitly
7506 reject it. What the compiler internals care about is that it's a
7507 template and has a pointer field followed by an integer field. */
7508 bool ok = false;
7509 if (processing_template_decl)
7511 tree f = next_initializable_field (TYPE_FIELDS (t));
7512 if (f && TREE_CODE (TREE_TYPE (f)) == POINTER_TYPE)
7514 f = next_initializable_field (DECL_CHAIN (f));
7515 if (f && same_type_p (TREE_TYPE (f), size_type_node))
7516 ok = true;
7519 if (!ok)
7520 fatal_error (input_location,
7521 "definition of std::initializer_list does not match "
7522 "#include <initializer_list>");
7525 input_location = saved_loc;
7527 TYPE_BEING_DEFINED (t) = 0;
7529 if (current_class_type)
7530 popclass ();
7531 else
7532 error ("trying to finish struct, but kicked out due to previous parse errors");
7534 if (processing_template_decl && at_function_scope_p ()
7535 /* Lambdas are defined by the LAMBDA_EXPR. */
7536 && !LAMBDA_TYPE_P (t))
7537 add_stmt (build_min (TAG_DEFN, t));
7539 return t;
7542 /* Hash table to avoid endless recursion when handling references. */
7543 static hash_table<nofree_ptr_hash<tree_node> > *fixed_type_or_null_ref_ht;
7545 /* Return the dynamic type of INSTANCE, if known.
7546 Used to determine whether the virtual function table is needed
7547 or not.
7549 *NONNULL is set iff INSTANCE can be known to be nonnull, regardless
7550 of our knowledge of its type. *NONNULL should be initialized
7551 before this function is called. */
7553 static tree
7554 fixed_type_or_null (tree instance, int *nonnull, int *cdtorp)
7556 #define RECUR(T) fixed_type_or_null((T), nonnull, cdtorp)
7558 switch (TREE_CODE (instance))
7560 case INDIRECT_REF:
7561 if (POINTER_TYPE_P (TREE_TYPE (instance)))
7562 return NULL_TREE;
7563 else
7564 return RECUR (TREE_OPERAND (instance, 0));
7566 case CALL_EXPR:
7567 /* This is a call to a constructor, hence it's never zero. */
7568 if (TREE_HAS_CONSTRUCTOR (instance))
7570 if (nonnull)
7571 *nonnull = 1;
7572 return TREE_TYPE (instance);
7574 return NULL_TREE;
7576 case SAVE_EXPR:
7577 /* This is a call to a constructor, hence it's never zero. */
7578 if (TREE_HAS_CONSTRUCTOR (instance))
7580 if (nonnull)
7581 *nonnull = 1;
7582 return TREE_TYPE (instance);
7584 return RECUR (TREE_OPERAND (instance, 0));
7586 case POINTER_PLUS_EXPR:
7587 case PLUS_EXPR:
7588 case MINUS_EXPR:
7589 if (TREE_CODE (TREE_OPERAND (instance, 0)) == ADDR_EXPR)
7590 return RECUR (TREE_OPERAND (instance, 0));
7591 if (TREE_CODE (TREE_OPERAND (instance, 1)) == INTEGER_CST)
7592 /* Propagate nonnull. */
7593 return RECUR (TREE_OPERAND (instance, 0));
7595 return NULL_TREE;
7597 CASE_CONVERT:
7598 return RECUR (TREE_OPERAND (instance, 0));
7600 case ADDR_EXPR:
7601 instance = TREE_OPERAND (instance, 0);
7602 if (nonnull)
7604 /* Just because we see an ADDR_EXPR doesn't mean we're dealing
7605 with a real object -- given &p->f, p can still be null. */
7606 tree t = get_base_address (instance);
7607 /* ??? Probably should check DECL_WEAK here. */
7608 if (t && DECL_P (t))
7609 *nonnull = 1;
7611 return RECUR (instance);
7613 case COMPONENT_REF:
7614 /* If this component is really a base class reference, then the field
7615 itself isn't definitive. */
7616 if (DECL_FIELD_IS_BASE (TREE_OPERAND (instance, 1)))
7617 return RECUR (TREE_OPERAND (instance, 0));
7618 return RECUR (TREE_OPERAND (instance, 1));
7620 case VAR_DECL:
7621 case FIELD_DECL:
7622 if (TREE_CODE (TREE_TYPE (instance)) == ARRAY_TYPE
7623 && MAYBE_CLASS_TYPE_P (TREE_TYPE (TREE_TYPE (instance))))
7625 if (nonnull)
7626 *nonnull = 1;
7627 return TREE_TYPE (TREE_TYPE (instance));
7629 /* fall through. */
7630 case TARGET_EXPR:
7631 case PARM_DECL:
7632 case RESULT_DECL:
7633 if (MAYBE_CLASS_TYPE_P (TREE_TYPE (instance)))
7635 if (nonnull)
7636 *nonnull = 1;
7637 return TREE_TYPE (instance);
7639 else if (instance == current_class_ptr)
7641 if (nonnull)
7642 *nonnull = 1;
7644 /* if we're in a ctor or dtor, we know our type. If
7645 current_class_ptr is set but we aren't in a function, we're in
7646 an NSDMI (and therefore a constructor). */
7647 if (current_scope () != current_function_decl
7648 || (DECL_LANG_SPECIFIC (current_function_decl)
7649 && (DECL_CONSTRUCTOR_P (current_function_decl)
7650 || DECL_DESTRUCTOR_P (current_function_decl))))
7652 if (cdtorp)
7653 *cdtorp = 1;
7654 return TREE_TYPE (TREE_TYPE (instance));
7657 else if (TREE_CODE (TREE_TYPE (instance)) == REFERENCE_TYPE)
7659 /* We only need one hash table because it is always left empty. */
7660 if (!fixed_type_or_null_ref_ht)
7661 fixed_type_or_null_ref_ht
7662 = new hash_table<nofree_ptr_hash<tree_node> > (37);
7664 /* Reference variables should be references to objects. */
7665 if (nonnull)
7666 *nonnull = 1;
7668 /* Enter the INSTANCE in a table to prevent recursion; a
7669 variable's initializer may refer to the variable
7670 itself. */
7671 if (VAR_P (instance)
7672 && DECL_INITIAL (instance)
7673 && !type_dependent_expression_p_push (DECL_INITIAL (instance))
7674 && !fixed_type_or_null_ref_ht->find (instance))
7676 tree type;
7677 tree_node **slot;
7679 slot = fixed_type_or_null_ref_ht->find_slot (instance, INSERT);
7680 *slot = instance;
7681 type = RECUR (DECL_INITIAL (instance));
7682 fixed_type_or_null_ref_ht->remove_elt (instance);
7684 return type;
7687 return NULL_TREE;
7689 default:
7690 return NULL_TREE;
7692 #undef RECUR
7695 /* Return nonzero if the dynamic type of INSTANCE is known, and
7696 equivalent to the static type. We also handle the case where
7697 INSTANCE is really a pointer. Return negative if this is a
7698 ctor/dtor. There the dynamic type is known, but this might not be
7699 the most derived base of the original object, and hence virtual
7700 bases may not be laid out according to this type.
7702 Used to determine whether the virtual function table is needed
7703 or not.
7705 *NONNULL is set iff INSTANCE can be known to be nonnull, regardless
7706 of our knowledge of its type. *NONNULL should be initialized
7707 before this function is called. */
7710 resolves_to_fixed_type_p (tree instance, int* nonnull)
7712 tree t = TREE_TYPE (instance);
7713 int cdtorp = 0;
7714 tree fixed;
7716 /* processing_template_decl can be false in a template if we're in
7717 instantiate_non_dependent_expr, but we still want to suppress
7718 this check. */
7719 if (in_template_function ())
7721 /* In a template we only care about the type of the result. */
7722 if (nonnull)
7723 *nonnull = true;
7724 return true;
7727 fixed = fixed_type_or_null (instance, nonnull, &cdtorp);
7728 if (fixed == NULL_TREE)
7729 return 0;
7730 if (POINTER_TYPE_P (t))
7731 t = TREE_TYPE (t);
7732 if (!same_type_ignoring_top_level_qualifiers_p (t, fixed))
7733 return 0;
7734 return cdtorp ? -1 : 1;
7738 void
7739 init_class_processing (void)
7741 current_class_depth = 0;
7742 current_class_stack_size = 10;
7743 current_class_stack
7744 = XNEWVEC (struct class_stack_node, current_class_stack_size);
7745 vec_alloc (local_classes, 8);
7746 sizeof_biggest_empty_class = size_zero_node;
7748 ridpointers[(int) RID_PUBLIC] = access_public_node;
7749 ridpointers[(int) RID_PRIVATE] = access_private_node;
7750 ridpointers[(int) RID_PROTECTED] = access_protected_node;
7753 /* Restore the cached PREVIOUS_CLASS_LEVEL. */
7755 static void
7756 restore_class_cache (void)
7758 tree type;
7760 /* We are re-entering the same class we just left, so we don't
7761 have to search the whole inheritance matrix to find all the
7762 decls to bind again. Instead, we install the cached
7763 class_shadowed list and walk through it binding names. */
7764 push_binding_level (previous_class_level);
7765 class_binding_level = previous_class_level;
7766 /* Restore IDENTIFIER_TYPE_VALUE. */
7767 for (type = class_binding_level->type_shadowed;
7768 type;
7769 type = TREE_CHAIN (type))
7770 SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (type), TREE_TYPE (type));
7773 /* Set global variables CURRENT_CLASS_NAME and CURRENT_CLASS_TYPE as
7774 appropriate for TYPE.
7776 So that we may avoid calls to lookup_name, we cache the _TYPE
7777 nodes of local TYPE_DECLs in the TREE_TYPE field of the name.
7779 For multiple inheritance, we perform a two-pass depth-first search
7780 of the type lattice. */
7782 void
7783 pushclass (tree type)
7785 class_stack_node_t csn;
7787 type = TYPE_MAIN_VARIANT (type);
7789 /* Make sure there is enough room for the new entry on the stack. */
7790 if (current_class_depth + 1 >= current_class_stack_size)
7792 current_class_stack_size *= 2;
7793 current_class_stack
7794 = XRESIZEVEC (struct class_stack_node, current_class_stack,
7795 current_class_stack_size);
7798 /* Insert a new entry on the class stack. */
7799 csn = current_class_stack + current_class_depth;
7800 csn->name = current_class_name;
7801 csn->type = current_class_type;
7802 csn->access = current_access_specifier;
7803 csn->names_used = 0;
7804 csn->hidden = 0;
7805 current_class_depth++;
7807 /* Now set up the new type. */
7808 current_class_name = TYPE_NAME (type);
7809 if (TREE_CODE (current_class_name) == TYPE_DECL)
7810 current_class_name = DECL_NAME (current_class_name);
7811 current_class_type = type;
7813 /* By default, things in classes are private, while things in
7814 structures or unions are public. */
7815 current_access_specifier = (CLASSTYPE_DECLARED_CLASS (type)
7816 ? access_private_node
7817 : access_public_node);
7819 if (previous_class_level
7820 && type != previous_class_level->this_entity
7821 && current_class_depth == 1)
7823 /* Forcibly remove any old class remnants. */
7824 invalidate_class_lookup_cache ();
7827 if (!previous_class_level
7828 || type != previous_class_level->this_entity
7829 || current_class_depth > 1)
7830 pushlevel_class ();
7831 else
7832 restore_class_cache ();
7835 /* When we exit a toplevel class scope, we save its binding level so
7836 that we can restore it quickly. Here, we've entered some other
7837 class, so we must invalidate our cache. */
7839 void
7840 invalidate_class_lookup_cache (void)
7842 previous_class_level = NULL;
7845 /* Get out of the current class scope. If we were in a class scope
7846 previously, that is the one popped to. */
7848 void
7849 popclass (void)
7851 poplevel_class ();
7853 current_class_depth--;
7854 current_class_name = current_class_stack[current_class_depth].name;
7855 current_class_type = current_class_stack[current_class_depth].type;
7856 current_access_specifier = current_class_stack[current_class_depth].access;
7857 if (current_class_stack[current_class_depth].names_used)
7858 splay_tree_delete (current_class_stack[current_class_depth].names_used);
7861 /* Mark the top of the class stack as hidden. */
7863 void
7864 push_class_stack (void)
7866 if (current_class_depth)
7867 ++current_class_stack[current_class_depth - 1].hidden;
7870 /* Mark the top of the class stack as un-hidden. */
7872 void
7873 pop_class_stack (void)
7875 if (current_class_depth)
7876 --current_class_stack[current_class_depth - 1].hidden;
7879 /* Returns 1 if the class type currently being defined is either T or
7880 a nested type of T. Returns the type from the current_class_stack,
7881 which might be equivalent to but not equal to T in case of
7882 constrained partial specializations. */
7884 tree
7885 currently_open_class (tree t)
7887 int i;
7889 if (!CLASS_TYPE_P (t))
7890 return NULL_TREE;
7892 t = TYPE_MAIN_VARIANT (t);
7894 /* We start looking from 1 because entry 0 is from global scope,
7895 and has no type. */
7896 for (i = current_class_depth; i > 0; --i)
7898 tree c;
7899 if (i == current_class_depth)
7900 c = current_class_type;
7901 else
7903 if (current_class_stack[i].hidden)
7904 break;
7905 c = current_class_stack[i].type;
7907 if (!c)
7908 continue;
7909 if (same_type_p (c, t))
7910 return c;
7912 return NULL_TREE;
7915 /* If either current_class_type or one of its enclosing classes are derived
7916 from T, return the appropriate type. Used to determine how we found
7917 something via unqualified lookup. */
7919 tree
7920 currently_open_derived_class (tree t)
7922 int i;
7924 /* The bases of a dependent type are unknown. */
7925 if (dependent_type_p (t))
7926 return NULL_TREE;
7928 if (!current_class_type)
7929 return NULL_TREE;
7931 if (DERIVED_FROM_P (t, current_class_type))
7932 return current_class_type;
7934 for (i = current_class_depth - 1; i > 0; --i)
7936 if (current_class_stack[i].hidden)
7937 break;
7938 if (DERIVED_FROM_P (t, current_class_stack[i].type))
7939 return current_class_stack[i].type;
7942 return NULL_TREE;
7945 /* Return the outermost enclosing class type that is still open, or
7946 NULL_TREE. */
7948 tree
7949 outermost_open_class (void)
7951 if (!current_class_type)
7952 return NULL_TREE;
7953 tree r = NULL_TREE;
7954 if (TYPE_BEING_DEFINED (current_class_type))
7955 r = current_class_type;
7956 for (int i = current_class_depth - 1; i > 0; --i)
7958 if (current_class_stack[i].hidden)
7959 break;
7960 tree t = current_class_stack[i].type;
7961 if (!TYPE_BEING_DEFINED (t))
7962 break;
7963 r = t;
7965 return r;
7968 /* Returns the innermost class type which is not a lambda closure type. */
7970 tree
7971 current_nonlambda_class_type (void)
7973 int i;
7975 /* We start looking from 1 because entry 0 is from global scope,
7976 and has no type. */
7977 for (i = current_class_depth; i > 0; --i)
7979 tree c;
7980 if (i == current_class_depth)
7981 c = current_class_type;
7982 else
7984 if (current_class_stack[i].hidden)
7985 break;
7986 c = current_class_stack[i].type;
7988 if (!c)
7989 continue;
7990 if (!LAMBDA_TYPE_P (c))
7991 return c;
7993 return NULL_TREE;
7996 /* When entering a class scope, all enclosing class scopes' names with
7997 static meaning (static variables, static functions, types and
7998 enumerators) have to be visible. This recursive function calls
7999 pushclass for all enclosing class contexts until global or a local
8000 scope is reached. TYPE is the enclosed class. */
8002 void
8003 push_nested_class (tree type)
8005 /* A namespace might be passed in error cases, like A::B:C. */
8006 if (type == NULL_TREE
8007 || !CLASS_TYPE_P (type))
8008 return;
8010 push_nested_class (DECL_CONTEXT (TYPE_MAIN_DECL (type)));
8012 pushclass (type);
8015 /* Undoes a push_nested_class call. */
8017 void
8018 pop_nested_class (void)
8020 tree context = DECL_CONTEXT (TYPE_MAIN_DECL (current_class_type));
8022 popclass ();
8023 if (context && CLASS_TYPE_P (context))
8024 pop_nested_class ();
8027 /* Returns the number of extern "LANG" blocks we are nested within. */
8030 current_lang_depth (void)
8032 return vec_safe_length (current_lang_base);
8035 /* Set global variables CURRENT_LANG_NAME to appropriate value
8036 so that behavior of name-mangling machinery is correct. */
8038 void
8039 push_lang_context (tree name)
8041 vec_safe_push (current_lang_base, current_lang_name);
8043 if (name == lang_name_cplusplus)
8044 current_lang_name = name;
8045 else if (name == lang_name_c)
8046 current_lang_name = name;
8047 else
8048 error ("language string %<\"%E\"%> not recognized", name);
8051 /* Get out of the current language scope. */
8053 void
8054 pop_lang_context (void)
8056 current_lang_name = current_lang_base->pop ();
8059 /* Type instantiation routines. */
8061 /* Given an OVERLOAD and a TARGET_TYPE, return the function that
8062 matches the TARGET_TYPE. If there is no satisfactory match, return
8063 error_mark_node, and issue an error & warning messages under
8064 control of FLAGS. Permit pointers to member function if FLAGS
8065 permits. If TEMPLATE_ONLY, the name of the overloaded function was
8066 a template-id, and EXPLICIT_TARGS are the explicitly provided
8067 template arguments.
8069 If OVERLOAD is for one or more member functions, then ACCESS_PATH
8070 is the base path used to reference those member functions. If
8071 the address is resolved to a member function, access checks will be
8072 performed and errors issued if appropriate. */
8074 static tree
8075 resolve_address_of_overloaded_function (tree target_type,
8076 tree overload,
8077 tsubst_flags_t complain,
8078 bool template_only,
8079 tree explicit_targs,
8080 tree access_path)
8082 /* Here's what the standard says:
8084 [over.over]
8086 If the name is a function template, template argument deduction
8087 is done, and if the argument deduction succeeds, the deduced
8088 arguments are used to generate a single template function, which
8089 is added to the set of overloaded functions considered.
8091 Non-member functions and static member functions match targets of
8092 type "pointer-to-function" or "reference-to-function." Nonstatic
8093 member functions match targets of type "pointer-to-member
8094 function;" the function type of the pointer to member is used to
8095 select the member function from the set of overloaded member
8096 functions. If a nonstatic member function is selected, the
8097 reference to the overloaded function name is required to have the
8098 form of a pointer to member as described in 5.3.1.
8100 If more than one function is selected, any template functions in
8101 the set are eliminated if the set also contains a non-template
8102 function, and any given template function is eliminated if the
8103 set contains a second template function that is more specialized
8104 than the first according to the partial ordering rules 14.5.5.2.
8105 After such eliminations, if any, there shall remain exactly one
8106 selected function. */
8108 int is_ptrmem = 0;
8109 /* We store the matches in a TREE_LIST rooted here. The functions
8110 are the TREE_PURPOSE, not the TREE_VALUE, in this list, for easy
8111 interoperability with most_specialized_instantiation. */
8112 tree matches = NULL_TREE;
8113 tree fn;
8114 tree target_fn_type;
8116 /* By the time we get here, we should be seeing only real
8117 pointer-to-member types, not the internal POINTER_TYPE to
8118 METHOD_TYPE representation. */
8119 gcc_assert (!TYPE_PTR_P (target_type)
8120 || TREE_CODE (TREE_TYPE (target_type)) != METHOD_TYPE);
8122 gcc_assert (is_overloaded_fn (overload));
8124 /* Check that the TARGET_TYPE is reasonable. */
8125 if (TYPE_PTRFN_P (target_type)
8126 || TYPE_REFFN_P (target_type))
8127 /* This is OK. */;
8128 else if (TYPE_PTRMEMFUNC_P (target_type))
8129 /* This is OK, too. */
8130 is_ptrmem = 1;
8131 else if (TREE_CODE (target_type) == FUNCTION_TYPE)
8132 /* This is OK, too. This comes from a conversion to reference
8133 type. */
8134 target_type = build_reference_type (target_type);
8135 else
8137 if (complain & tf_error)
8138 error ("cannot resolve overloaded function %qD based on"
8139 " conversion to type %qT",
8140 DECL_NAME (OVL_FUNCTION (overload)), target_type);
8141 return error_mark_node;
8144 /* Non-member functions and static member functions match targets of type
8145 "pointer-to-function" or "reference-to-function." Nonstatic member
8146 functions match targets of type "pointer-to-member-function;" the
8147 function type of the pointer to member is used to select the member
8148 function from the set of overloaded member functions.
8150 So figure out the FUNCTION_TYPE that we want to match against. */
8151 target_fn_type = static_fn_type (target_type);
8153 /* If we can find a non-template function that matches, we can just
8154 use it. There's no point in generating template instantiations
8155 if we're just going to throw them out anyhow. But, of course, we
8156 can only do this when we don't *need* a template function. */
8157 if (!template_only)
8159 tree fns;
8161 for (fns = overload; fns; fns = OVL_NEXT (fns))
8163 tree fn = OVL_CURRENT (fns);
8165 if (TREE_CODE (fn) == TEMPLATE_DECL)
8166 /* We're not looking for templates just yet. */
8167 continue;
8169 if ((TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
8170 != is_ptrmem)
8171 /* We're looking for a non-static member, and this isn't
8172 one, or vice versa. */
8173 continue;
8175 /* Ignore functions which haven't been explicitly
8176 declared. */
8177 if (DECL_ANTICIPATED (fn))
8178 continue;
8180 /* In C++17 we need the noexcept-qualifier to compare types. */
8181 if (flag_noexcept_type)
8182 maybe_instantiate_noexcept (fn);
8184 /* See if there's a match. */
8185 tree fntype = static_fn_type (fn);
8186 if (same_type_p (target_fn_type, fntype)
8187 || fnptr_conv_p (target_fn_type, fntype))
8188 matches = tree_cons (fn, NULL_TREE, matches);
8192 /* Now, if we've already got a match (or matches), there's no need
8193 to proceed to the template functions. But, if we don't have a
8194 match we need to look at them, too. */
8195 if (!matches)
8197 tree target_arg_types;
8198 tree target_ret_type;
8199 tree fns;
8200 tree *args;
8201 unsigned int nargs, ia;
8202 tree arg;
8204 target_arg_types = TYPE_ARG_TYPES (target_fn_type);
8205 target_ret_type = TREE_TYPE (target_fn_type);
8207 nargs = list_length (target_arg_types);
8208 args = XALLOCAVEC (tree, nargs);
8209 for (arg = target_arg_types, ia = 0;
8210 arg != NULL_TREE && arg != void_list_node;
8211 arg = TREE_CHAIN (arg), ++ia)
8212 args[ia] = TREE_VALUE (arg);
8213 nargs = ia;
8215 for (fns = overload; fns; fns = OVL_NEXT (fns))
8217 tree fn = OVL_CURRENT (fns);
8218 tree instantiation;
8219 tree targs;
8221 if (TREE_CODE (fn) != TEMPLATE_DECL)
8222 /* We're only looking for templates. */
8223 continue;
8225 if ((TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
8226 != is_ptrmem)
8227 /* We're not looking for a non-static member, and this is
8228 one, or vice versa. */
8229 continue;
8231 tree ret = target_ret_type;
8233 /* If the template has a deduced return type, don't expose it to
8234 template argument deduction. */
8235 if (undeduced_auto_decl (fn))
8236 ret = NULL_TREE;
8238 /* Try to do argument deduction. */
8239 targs = make_tree_vec (DECL_NTPARMS (fn));
8240 instantiation = fn_type_unification (fn, explicit_targs, targs, args,
8241 nargs, ret,
8242 DEDUCE_EXACT, LOOKUP_NORMAL,
8243 false, false);
8244 if (instantiation == error_mark_node)
8245 /* Instantiation failed. */
8246 continue;
8248 /* Constraints must be satisfied. This is done before
8249 return type deduction since that instantiates the
8250 function. */
8251 if (flag_concepts && !constraints_satisfied_p (instantiation))
8252 continue;
8254 /* And now force instantiation to do return type deduction. */
8255 if (undeduced_auto_decl (instantiation))
8257 ++function_depth;
8258 instantiate_decl (instantiation, /*defer*/false, /*class*/false);
8259 --function_depth;
8261 require_deduced_type (instantiation);
8264 /* In C++17 we need the noexcept-qualifier to compare types. */
8265 if (flag_noexcept_type)
8266 maybe_instantiate_noexcept (instantiation);
8268 /* See if there's a match. */
8269 tree fntype = static_fn_type (instantiation);
8270 if (same_type_p (target_fn_type, fntype)
8271 || fnptr_conv_p (target_fn_type, fntype))
8272 matches = tree_cons (instantiation, fn, matches);
8275 /* Now, remove all but the most specialized of the matches. */
8276 if (matches)
8278 tree match = most_specialized_instantiation (matches);
8280 if (match != error_mark_node)
8281 matches = tree_cons (TREE_PURPOSE (match),
8282 NULL_TREE,
8283 NULL_TREE);
8287 /* Now we should have exactly one function in MATCHES. */
8288 if (matches == NULL_TREE)
8290 /* There were *no* matches. */
8291 if (complain & tf_error)
8293 error ("no matches converting function %qD to type %q#T",
8294 DECL_NAME (OVL_CURRENT (overload)),
8295 target_type);
8297 print_candidates (overload);
8299 return error_mark_node;
8301 else if (TREE_CHAIN (matches))
8303 /* There were too many matches. First check if they're all
8304 the same function. */
8305 tree match = NULL_TREE;
8307 fn = TREE_PURPOSE (matches);
8309 /* For multi-versioned functions, more than one match is just fine and
8310 decls_match will return false as they are different. */
8311 for (match = TREE_CHAIN (matches); match; match = TREE_CHAIN (match))
8312 if (!decls_match (fn, TREE_PURPOSE (match))
8313 && !targetm.target_option.function_versions
8314 (fn, TREE_PURPOSE (match)))
8315 break;
8317 if (match)
8319 if (complain & tf_error)
8321 error ("converting overloaded function %qD to type %q#T is ambiguous",
8322 DECL_NAME (OVL_FUNCTION (overload)),
8323 target_type);
8325 /* Since print_candidates expects the functions in the
8326 TREE_VALUE slot, we flip them here. */
8327 for (match = matches; match; match = TREE_CHAIN (match))
8328 TREE_VALUE (match) = TREE_PURPOSE (match);
8330 print_candidates (matches);
8333 return error_mark_node;
8337 /* Good, exactly one match. Now, convert it to the correct type. */
8338 fn = TREE_PURPOSE (matches);
8340 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
8341 && !(complain & tf_ptrmem_ok) && !flag_ms_extensions)
8343 static int explained;
8345 if (!(complain & tf_error))
8346 return error_mark_node;
8348 permerror (input_location, "assuming pointer to member %qD", fn);
8349 if (!explained)
8351 inform (input_location, "(a pointer to member can only be formed with %<&%E%>)", fn);
8352 explained = 1;
8356 /* If a pointer to a function that is multi-versioned is requested, the
8357 pointer to the dispatcher function is returned instead. This works
8358 well because indirectly calling the function will dispatch the right
8359 function version at run-time. */
8360 if (DECL_FUNCTION_VERSIONED (fn))
8362 fn = get_function_version_dispatcher (fn);
8363 if (fn == NULL)
8364 return error_mark_node;
8365 /* Mark all the versions corresponding to the dispatcher as used. */
8366 if (!(complain & tf_conv))
8367 mark_versions_used (fn);
8370 /* If we're doing overload resolution purely for the purpose of
8371 determining conversion sequences, we should not consider the
8372 function used. If this conversion sequence is selected, the
8373 function will be marked as used at this point. */
8374 if (!(complain & tf_conv))
8376 /* Make =delete work with SFINAE. */
8377 if (DECL_DELETED_FN (fn) && !(complain & tf_error))
8378 return error_mark_node;
8379 if (!mark_used (fn, complain) && !(complain & tf_error))
8380 return error_mark_node;
8383 /* We could not check access to member functions when this
8384 expression was originally created since we did not know at that
8385 time to which function the expression referred. */
8386 if (DECL_FUNCTION_MEMBER_P (fn))
8388 gcc_assert (access_path);
8389 perform_or_defer_access_check (access_path, fn, fn, complain);
8392 if (TYPE_PTRFN_P (target_type) || TYPE_PTRMEMFUNC_P (target_type))
8393 return cp_build_addr_expr (fn, complain);
8394 else
8396 /* The target must be a REFERENCE_TYPE. Above, cp_build_unary_op
8397 will mark the function as addressed, but here we must do it
8398 explicitly. */
8399 cxx_mark_addressable (fn);
8401 return fn;
8405 /* This function will instantiate the type of the expression given in
8406 RHS to match the type of LHSTYPE. If errors exist, then return
8407 error_mark_node. COMPLAIN is a bit mask. If TF_ERROR is set, then
8408 we complain on errors. If we are not complaining, never modify rhs,
8409 as overload resolution wants to try many possible instantiations, in
8410 the hope that at least one will work.
8412 For non-recursive calls, LHSTYPE should be a function, pointer to
8413 function, or a pointer to member function. */
8415 tree
8416 instantiate_type (tree lhstype, tree rhs, tsubst_flags_t complain)
8418 tsubst_flags_t complain_in = complain;
8419 tree access_path = NULL_TREE;
8421 complain &= ~tf_ptrmem_ok;
8423 if (lhstype == unknown_type_node)
8425 if (complain & tf_error)
8426 error ("not enough type information");
8427 return error_mark_node;
8430 if (TREE_TYPE (rhs) != NULL_TREE && ! (type_unknown_p (rhs)))
8432 tree fntype = non_reference (lhstype);
8433 if (same_type_p (fntype, TREE_TYPE (rhs)))
8434 return rhs;
8435 if (fnptr_conv_p (fntype, TREE_TYPE (rhs)))
8436 return rhs;
8437 if (flag_ms_extensions
8438 && TYPE_PTRMEMFUNC_P (fntype)
8439 && !TYPE_PTRMEMFUNC_P (TREE_TYPE (rhs)))
8440 /* Microsoft allows `A::f' to be resolved to a
8441 pointer-to-member. */
8443 else
8445 if (complain & tf_error)
8446 error ("cannot convert %qE from type %qT to type %qT",
8447 rhs, TREE_TYPE (rhs), fntype);
8448 return error_mark_node;
8452 if (BASELINK_P (rhs))
8454 access_path = BASELINK_ACCESS_BINFO (rhs);
8455 rhs = BASELINK_FUNCTIONS (rhs);
8458 /* If we are in a template, and have a NON_DEPENDENT_EXPR, we cannot
8459 deduce any type information. */
8460 if (TREE_CODE (rhs) == NON_DEPENDENT_EXPR)
8462 if (complain & tf_error)
8463 error ("not enough type information");
8464 return error_mark_node;
8467 /* If we instantiate a template, and it is a A ?: C expression
8468 with omitted B, look through the SAVE_EXPR. */
8469 if (TREE_CODE (rhs) == SAVE_EXPR)
8470 rhs = TREE_OPERAND (rhs, 0);
8472 /* There are only a few kinds of expressions that may have a type
8473 dependent on overload resolution. */
8474 gcc_assert (TREE_CODE (rhs) == ADDR_EXPR
8475 || TREE_CODE (rhs) == COMPONENT_REF
8476 || is_overloaded_fn (rhs)
8477 || (flag_ms_extensions && TREE_CODE (rhs) == FUNCTION_DECL));
8479 /* This should really only be used when attempting to distinguish
8480 what sort of a pointer to function we have. For now, any
8481 arithmetic operation which is not supported on pointers
8482 is rejected as an error. */
8484 switch (TREE_CODE (rhs))
8486 case COMPONENT_REF:
8488 tree member = TREE_OPERAND (rhs, 1);
8490 member = instantiate_type (lhstype, member, complain);
8491 if (member != error_mark_node
8492 && TREE_SIDE_EFFECTS (TREE_OPERAND (rhs, 0)))
8493 /* Do not lose object's side effects. */
8494 return build2 (COMPOUND_EXPR, TREE_TYPE (member),
8495 TREE_OPERAND (rhs, 0), member);
8496 return member;
8499 case OFFSET_REF:
8500 rhs = TREE_OPERAND (rhs, 1);
8501 if (BASELINK_P (rhs))
8502 return instantiate_type (lhstype, rhs, complain_in);
8504 /* This can happen if we are forming a pointer-to-member for a
8505 member template. */
8506 gcc_assert (TREE_CODE (rhs) == TEMPLATE_ID_EXPR);
8508 /* Fall through. */
8510 case TEMPLATE_ID_EXPR:
8512 tree fns = TREE_OPERAND (rhs, 0);
8513 tree args = TREE_OPERAND (rhs, 1);
8515 return
8516 resolve_address_of_overloaded_function (lhstype, fns, complain_in,
8517 /*template_only=*/true,
8518 args, access_path);
8521 case OVERLOAD:
8522 case FUNCTION_DECL:
8523 return
8524 resolve_address_of_overloaded_function (lhstype, rhs, complain_in,
8525 /*template_only=*/false,
8526 /*explicit_targs=*/NULL_TREE,
8527 access_path);
8529 case ADDR_EXPR:
8531 if (PTRMEM_OK_P (rhs))
8532 complain |= tf_ptrmem_ok;
8534 return instantiate_type (lhstype, TREE_OPERAND (rhs, 0), complain);
8537 case ERROR_MARK:
8538 return error_mark_node;
8540 default:
8541 gcc_unreachable ();
8543 return error_mark_node;
8546 /* Return the name of the virtual function pointer field
8547 (as an IDENTIFIER_NODE) for the given TYPE. Note that
8548 this may have to look back through base types to find the
8549 ultimate field name. (For single inheritance, these could
8550 all be the same name. Who knows for multiple inheritance). */
8552 static tree
8553 get_vfield_name (tree type)
8555 tree binfo, base_binfo;
8556 char *buf;
8558 for (binfo = TYPE_BINFO (type);
8559 BINFO_N_BASE_BINFOS (binfo);
8560 binfo = base_binfo)
8562 base_binfo = BINFO_BASE_BINFO (binfo, 0);
8564 if (BINFO_VIRTUAL_P (base_binfo)
8565 || !TYPE_CONTAINS_VPTR_P (BINFO_TYPE (base_binfo)))
8566 break;
8569 type = BINFO_TYPE (binfo);
8570 buf = (char *) alloca (sizeof (VFIELD_NAME_FORMAT)
8571 + TYPE_NAME_LENGTH (type) + 2);
8572 sprintf (buf, VFIELD_NAME_FORMAT,
8573 IDENTIFIER_POINTER (constructor_name (type)));
8574 return get_identifier (buf);
8577 void
8578 print_class_statistics (void)
8580 if (! GATHER_STATISTICS)
8581 return;
8583 fprintf (stderr, "convert_harshness = %d\n", n_convert_harshness);
8584 fprintf (stderr, "compute_conversion_costs = %d\n", n_compute_conversion_costs);
8585 if (n_vtables)
8587 fprintf (stderr, "vtables = %d; vtable searches = %d\n",
8588 n_vtables, n_vtable_searches);
8589 fprintf (stderr, "vtable entries = %d; vtable elems = %d\n",
8590 n_vtable_entries, n_vtable_elems);
8594 /* Build a dummy reference to ourselves so Derived::Base (and A::A) works,
8595 according to [class]:
8596 The class-name is also inserted
8597 into the scope of the class itself. For purposes of access checking,
8598 the inserted class name is treated as if it were a public member name. */
8600 void
8601 build_self_reference (void)
8603 tree name = constructor_name (current_class_type);
8604 tree value = build_lang_decl (TYPE_DECL, name, current_class_type);
8605 tree saved_cas;
8607 DECL_NONLOCAL (value) = 1;
8608 DECL_CONTEXT (value) = current_class_type;
8609 DECL_ARTIFICIAL (value) = 1;
8610 SET_DECL_SELF_REFERENCE_P (value);
8611 set_underlying_type (value);
8613 if (processing_template_decl)
8614 value = push_template_decl (value);
8616 saved_cas = current_access_specifier;
8617 current_access_specifier = access_public_node;
8618 finish_member_declaration (value);
8619 current_access_specifier = saved_cas;
8622 /* Returns 1 if TYPE contains only padding bytes. */
8625 is_empty_class (tree type)
8627 if (type == error_mark_node)
8628 return 0;
8630 if (! CLASS_TYPE_P (type))
8631 return 0;
8633 return CLASSTYPE_EMPTY_P (type);
8636 /* Returns true if TYPE contains no actual data, just various
8637 possible combinations of empty classes and possibly a vptr. */
8639 bool
8640 is_really_empty_class (tree type)
8642 if (CLASS_TYPE_P (type))
8644 tree field;
8645 tree binfo;
8646 tree base_binfo;
8647 int i;
8649 /* CLASSTYPE_EMPTY_P isn't set properly until the class is actually laid
8650 out, but we'd like to be able to check this before then. */
8651 if (COMPLETE_TYPE_P (type) && is_empty_class (type))
8652 return true;
8654 for (binfo = TYPE_BINFO (type), i = 0;
8655 BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
8656 if (!is_really_empty_class (BINFO_TYPE (base_binfo)))
8657 return false;
8658 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
8659 if (TREE_CODE (field) == FIELD_DECL
8660 && !DECL_ARTIFICIAL (field)
8661 /* An unnamed bit-field is not a data member. */
8662 && (DECL_NAME (field) || !DECL_C_BIT_FIELD (field))
8663 && !is_really_empty_class (TREE_TYPE (field)))
8664 return false;
8665 return true;
8667 else if (TREE_CODE (type) == ARRAY_TYPE)
8668 return (integer_zerop (array_type_nelts_top (type))
8669 || is_really_empty_class (TREE_TYPE (type)));
8670 return false;
8673 /* Note that NAME was looked up while the current class was being
8674 defined and that the result of that lookup was DECL. */
8676 void
8677 maybe_note_name_used_in_class (tree name, tree decl)
8679 splay_tree names_used;
8681 /* If we're not defining a class, there's nothing to do. */
8682 if (!(innermost_scope_kind() == sk_class
8683 && TYPE_BEING_DEFINED (current_class_type)
8684 && !LAMBDA_TYPE_P (current_class_type)))
8685 return;
8687 /* If there's already a binding for this NAME, then we don't have
8688 anything to worry about. */
8689 if (lookup_member (current_class_type, name,
8690 /*protect=*/0, /*want_type=*/false, tf_warning_or_error))
8691 return;
8693 if (!current_class_stack[current_class_depth - 1].names_used)
8694 current_class_stack[current_class_depth - 1].names_used
8695 = splay_tree_new (splay_tree_compare_pointers, 0, 0);
8696 names_used = current_class_stack[current_class_depth - 1].names_used;
8698 splay_tree_insert (names_used,
8699 (splay_tree_key) name,
8700 (splay_tree_value) decl);
8703 /* Note that NAME was declared (as DECL) in the current class. Check
8704 to see that the declaration is valid. */
8706 void
8707 note_name_declared_in_class (tree name, tree decl)
8709 splay_tree names_used;
8710 splay_tree_node n;
8712 /* Look to see if we ever used this name. */
8713 names_used
8714 = current_class_stack[current_class_depth - 1].names_used;
8715 if (!names_used)
8716 return;
8717 /* The C language allows members to be declared with a type of the same
8718 name, and the C++ standard says this diagnostic is not required. So
8719 allow it in extern "C" blocks unless predantic is specified.
8720 Allow it in all cases if -ms-extensions is specified. */
8721 if ((!pedantic && current_lang_name == lang_name_c)
8722 || flag_ms_extensions)
8723 return;
8724 n = splay_tree_lookup (names_used, (splay_tree_key) name);
8725 if (n)
8727 /* [basic.scope.class]
8729 A name N used in a class S shall refer to the same declaration
8730 in its context and when re-evaluated in the completed scope of
8731 S. */
8732 permerror (input_location, "declaration of %q#D", decl);
8733 permerror (location_of ((tree) n->value),
8734 "changes meaning of %qD from %q#D",
8735 DECL_NAME (OVL_CURRENT (decl)), (tree) n->value);
8739 /* Returns the VAR_DECL for the complete vtable associated with BINFO.
8740 Secondary vtables are merged with primary vtables; this function
8741 will return the VAR_DECL for the primary vtable. */
8743 tree
8744 get_vtbl_decl_for_binfo (tree binfo)
8746 tree decl;
8748 decl = BINFO_VTABLE (binfo);
8749 if (decl && TREE_CODE (decl) == POINTER_PLUS_EXPR)
8751 gcc_assert (TREE_CODE (TREE_OPERAND (decl, 0)) == ADDR_EXPR);
8752 decl = TREE_OPERAND (TREE_OPERAND (decl, 0), 0);
8754 if (decl)
8755 gcc_assert (VAR_P (decl));
8756 return decl;
8760 /* Returns the binfo for the primary base of BINFO. If the resulting
8761 BINFO is a virtual base, and it is inherited elsewhere in the
8762 hierarchy, then the returned binfo might not be the primary base of
8763 BINFO in the complete object. Check BINFO_PRIMARY_P or
8764 BINFO_LOST_PRIMARY_P to be sure. */
8766 static tree
8767 get_primary_binfo (tree binfo)
8769 tree primary_base;
8771 primary_base = CLASSTYPE_PRIMARY_BINFO (BINFO_TYPE (binfo));
8772 if (!primary_base)
8773 return NULL_TREE;
8775 return copied_binfo (primary_base, binfo);
8778 /* As above, but iterate until we reach the binfo that actually provides the
8779 vptr for BINFO. */
8781 static tree
8782 most_primary_binfo (tree binfo)
8784 tree b = binfo;
8785 while (CLASSTYPE_HAS_PRIMARY_BASE_P (BINFO_TYPE (b))
8786 && !BINFO_LOST_PRIMARY_P (b))
8788 tree primary_base = get_primary_binfo (b);
8789 gcc_assert (BINFO_PRIMARY_P (primary_base)
8790 && BINFO_INHERITANCE_CHAIN (primary_base) == b);
8791 b = primary_base;
8793 return b;
8796 /* Returns true if BINFO gets its vptr from a virtual base of the most derived
8797 type. Note that the virtual inheritance might be above or below BINFO in
8798 the hierarchy. */
8800 bool
8801 vptr_via_virtual_p (tree binfo)
8803 if (TYPE_P (binfo))
8804 binfo = TYPE_BINFO (binfo);
8805 tree primary = most_primary_binfo (binfo);
8806 /* Don't limit binfo_via_virtual, we want to return true when BINFO itself is
8807 a morally virtual base. */
8808 tree virt = binfo_via_virtual (primary, NULL_TREE);
8809 return virt != NULL_TREE;
8812 /* If INDENTED_P is zero, indent to INDENT. Return nonzero. */
8814 static int
8815 maybe_indent_hierarchy (FILE * stream, int indent, int indented_p)
8817 if (!indented_p)
8818 fprintf (stream, "%*s", indent, "");
8819 return 1;
8822 /* Dump the offsets of all the bases rooted at BINFO to STREAM.
8823 INDENT should be zero when called from the top level; it is
8824 incremented recursively. IGO indicates the next expected BINFO in
8825 inheritance graph ordering. */
8827 static tree
8828 dump_class_hierarchy_r (FILE *stream,
8829 int flags,
8830 tree binfo,
8831 tree igo,
8832 int indent)
8834 int indented = 0;
8835 tree base_binfo;
8836 int i;
8838 indented = maybe_indent_hierarchy (stream, indent, 0);
8839 fprintf (stream, "%s (0x" HOST_WIDE_INT_PRINT_HEX ") ",
8840 type_as_string (BINFO_TYPE (binfo), TFF_PLAIN_IDENTIFIER),
8841 (HOST_WIDE_INT) (uintptr_t) binfo);
8842 if (binfo != igo)
8844 fprintf (stream, "alternative-path\n");
8845 return igo;
8847 igo = TREE_CHAIN (binfo);
8849 fprintf (stream, HOST_WIDE_INT_PRINT_DEC,
8850 tree_to_shwi (BINFO_OFFSET (binfo)));
8851 if (is_empty_class (BINFO_TYPE (binfo)))
8852 fprintf (stream, " empty");
8853 else if (CLASSTYPE_NEARLY_EMPTY_P (BINFO_TYPE (binfo)))
8854 fprintf (stream, " nearly-empty");
8855 if (BINFO_VIRTUAL_P (binfo))
8856 fprintf (stream, " virtual");
8857 fprintf (stream, "\n");
8859 indented = 0;
8860 if (BINFO_PRIMARY_P (binfo))
8862 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
8863 fprintf (stream, " primary-for %s (0x" HOST_WIDE_INT_PRINT_HEX ")",
8864 type_as_string (BINFO_TYPE (BINFO_INHERITANCE_CHAIN (binfo)),
8865 TFF_PLAIN_IDENTIFIER),
8866 (HOST_WIDE_INT) (uintptr_t) BINFO_INHERITANCE_CHAIN (binfo));
8868 if (BINFO_LOST_PRIMARY_P (binfo))
8870 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
8871 fprintf (stream, " lost-primary");
8873 if (indented)
8874 fprintf (stream, "\n");
8876 if (!(flags & TDF_SLIM))
8878 int indented = 0;
8880 if (BINFO_SUBVTT_INDEX (binfo))
8882 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
8883 fprintf (stream, " subvttidx=%s",
8884 expr_as_string (BINFO_SUBVTT_INDEX (binfo),
8885 TFF_PLAIN_IDENTIFIER));
8887 if (BINFO_VPTR_INDEX (binfo))
8889 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
8890 fprintf (stream, " vptridx=%s",
8891 expr_as_string (BINFO_VPTR_INDEX (binfo),
8892 TFF_PLAIN_IDENTIFIER));
8894 if (BINFO_VPTR_FIELD (binfo))
8896 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
8897 fprintf (stream, " vbaseoffset=%s",
8898 expr_as_string (BINFO_VPTR_FIELD (binfo),
8899 TFF_PLAIN_IDENTIFIER));
8901 if (BINFO_VTABLE (binfo))
8903 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
8904 fprintf (stream, " vptr=%s",
8905 expr_as_string (BINFO_VTABLE (binfo),
8906 TFF_PLAIN_IDENTIFIER));
8909 if (indented)
8910 fprintf (stream, "\n");
8913 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
8914 igo = dump_class_hierarchy_r (stream, flags, base_binfo, igo, indent + 2);
8916 return igo;
8919 /* Dump the BINFO hierarchy for T. */
8921 static void
8922 dump_class_hierarchy_1 (FILE *stream, int flags, tree t)
8924 fprintf (stream, "Class %s\n", type_as_string (t, TFF_PLAIN_IDENTIFIER));
8925 fprintf (stream, " size=%lu align=%lu\n",
8926 (unsigned long)(tree_to_shwi (TYPE_SIZE (t)) / BITS_PER_UNIT),
8927 (unsigned long)(TYPE_ALIGN (t) / BITS_PER_UNIT));
8928 fprintf (stream, " base size=%lu base align=%lu\n",
8929 (unsigned long)(tree_to_shwi (TYPE_SIZE (CLASSTYPE_AS_BASE (t)))
8930 / BITS_PER_UNIT),
8931 (unsigned long)(TYPE_ALIGN (CLASSTYPE_AS_BASE (t))
8932 / BITS_PER_UNIT));
8933 dump_class_hierarchy_r (stream, flags, TYPE_BINFO (t), TYPE_BINFO (t), 0);
8934 fprintf (stream, "\n");
8937 /* Debug interface to hierarchy dumping. */
8939 void
8940 debug_class (tree t)
8942 dump_class_hierarchy_1 (stderr, TDF_SLIM, t);
8945 static void
8946 dump_class_hierarchy (tree t)
8948 int flags;
8949 FILE *stream = get_dump_info (TDI_class, &flags);
8951 if (stream)
8953 dump_class_hierarchy_1 (stream, flags, t);
8957 static void
8958 dump_array (FILE * stream, tree decl)
8960 tree value;
8961 unsigned HOST_WIDE_INT ix;
8962 HOST_WIDE_INT elt;
8963 tree size = TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (decl)));
8965 elt = (tree_to_shwi (TYPE_SIZE (TREE_TYPE (TREE_TYPE (decl))))
8966 / BITS_PER_UNIT);
8967 fprintf (stream, "%s:", decl_as_string (decl, TFF_PLAIN_IDENTIFIER));
8968 fprintf (stream, " %s entries",
8969 expr_as_string (size_binop (PLUS_EXPR, size, size_one_node),
8970 TFF_PLAIN_IDENTIFIER));
8971 fprintf (stream, "\n");
8973 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (DECL_INITIAL (decl)),
8974 ix, value)
8975 fprintf (stream, "%-4ld %s\n", (long)(ix * elt),
8976 expr_as_string (value, TFF_PLAIN_IDENTIFIER));
8979 static void
8980 dump_vtable (tree t, tree binfo, tree vtable)
8982 int flags;
8983 FILE *stream = get_dump_info (TDI_class, &flags);
8985 if (!stream)
8986 return;
8988 if (!(flags & TDF_SLIM))
8990 int ctor_vtbl_p = TYPE_BINFO (t) != binfo;
8992 fprintf (stream, "%s for %s",
8993 ctor_vtbl_p ? "Construction vtable" : "Vtable",
8994 type_as_string (BINFO_TYPE (binfo), TFF_PLAIN_IDENTIFIER));
8995 if (ctor_vtbl_p)
8997 if (!BINFO_VIRTUAL_P (binfo))
8998 fprintf (stream, " (0x" HOST_WIDE_INT_PRINT_HEX " instance)",
8999 (HOST_WIDE_INT) (uintptr_t) binfo);
9000 fprintf (stream, " in %s", type_as_string (t, TFF_PLAIN_IDENTIFIER));
9002 fprintf (stream, "\n");
9003 dump_array (stream, vtable);
9004 fprintf (stream, "\n");
9008 static void
9009 dump_vtt (tree t, tree vtt)
9011 int flags;
9012 FILE *stream = get_dump_info (TDI_class, &flags);
9014 if (!stream)
9015 return;
9017 if (!(flags & TDF_SLIM))
9019 fprintf (stream, "VTT for %s\n",
9020 type_as_string (t, TFF_PLAIN_IDENTIFIER));
9021 dump_array (stream, vtt);
9022 fprintf (stream, "\n");
9026 /* Dump a function or thunk and its thunkees. */
9028 static void
9029 dump_thunk (FILE *stream, int indent, tree thunk)
9031 static const char spaces[] = " ";
9032 tree name = DECL_NAME (thunk);
9033 tree thunks;
9035 fprintf (stream, "%.*s%p %s %s", indent, spaces,
9036 (void *)thunk,
9037 !DECL_THUNK_P (thunk) ? "function"
9038 : DECL_THIS_THUNK_P (thunk) ? "this-thunk" : "covariant-thunk",
9039 name ? IDENTIFIER_POINTER (name) : "<unset>");
9040 if (DECL_THUNK_P (thunk))
9042 HOST_WIDE_INT fixed_adjust = THUNK_FIXED_OFFSET (thunk);
9043 tree virtual_adjust = THUNK_VIRTUAL_OFFSET (thunk);
9045 fprintf (stream, " fixed=" HOST_WIDE_INT_PRINT_DEC, fixed_adjust);
9046 if (!virtual_adjust)
9047 /*NOP*/;
9048 else if (DECL_THIS_THUNK_P (thunk))
9049 fprintf (stream, " vcall=" HOST_WIDE_INT_PRINT_DEC,
9050 tree_to_shwi (virtual_adjust));
9051 else
9052 fprintf (stream, " vbase=" HOST_WIDE_INT_PRINT_DEC "(%s)",
9053 tree_to_shwi (BINFO_VPTR_FIELD (virtual_adjust)),
9054 type_as_string (BINFO_TYPE (virtual_adjust), TFF_SCOPE));
9055 if (THUNK_ALIAS (thunk))
9056 fprintf (stream, " alias to %p", (void *)THUNK_ALIAS (thunk));
9058 fprintf (stream, "\n");
9059 for (thunks = DECL_THUNKS (thunk); thunks; thunks = TREE_CHAIN (thunks))
9060 dump_thunk (stream, indent + 2, thunks);
9063 /* Dump the thunks for FN. */
9065 void
9066 debug_thunks (tree fn)
9068 dump_thunk (stderr, 0, fn);
9071 /* Virtual function table initialization. */
9073 /* Create all the necessary vtables for T and its base classes. */
9075 static void
9076 finish_vtbls (tree t)
9078 tree vbase;
9079 vec<constructor_elt, va_gc> *v = NULL;
9080 tree vtable = BINFO_VTABLE (TYPE_BINFO (t));
9082 /* We lay out the primary and secondary vtables in one contiguous
9083 vtable. The primary vtable is first, followed by the non-virtual
9084 secondary vtables in inheritance graph order. */
9085 accumulate_vtbl_inits (TYPE_BINFO (t), TYPE_BINFO (t), TYPE_BINFO (t),
9086 vtable, t, &v);
9088 /* Then come the virtual bases, also in inheritance graph order. */
9089 for (vbase = TYPE_BINFO (t); vbase; vbase = TREE_CHAIN (vbase))
9091 if (!BINFO_VIRTUAL_P (vbase))
9092 continue;
9093 accumulate_vtbl_inits (vbase, vbase, TYPE_BINFO (t), vtable, t, &v);
9096 if (BINFO_VTABLE (TYPE_BINFO (t)))
9097 initialize_vtable (TYPE_BINFO (t), v);
9100 /* Initialize the vtable for BINFO with the INITS. */
9102 static void
9103 initialize_vtable (tree binfo, vec<constructor_elt, va_gc> *inits)
9105 tree decl;
9107 layout_vtable_decl (binfo, vec_safe_length (inits));
9108 decl = get_vtbl_decl_for_binfo (binfo);
9109 initialize_artificial_var (decl, inits);
9110 dump_vtable (BINFO_TYPE (binfo), binfo, decl);
9113 /* Build the VTT (virtual table table) for T.
9114 A class requires a VTT if it has virtual bases.
9116 This holds
9117 1 - primary virtual pointer for complete object T
9118 2 - secondary VTTs for each direct non-virtual base of T which requires a
9120 3 - secondary virtual pointers for each direct or indirect base of T which
9121 has virtual bases or is reachable via a virtual path from T.
9122 4 - secondary VTTs for each direct or indirect virtual base of T.
9124 Secondary VTTs look like complete object VTTs without part 4. */
9126 static void
9127 build_vtt (tree t)
9129 tree type;
9130 tree vtt;
9131 tree index;
9132 vec<constructor_elt, va_gc> *inits;
9134 /* Build up the initializers for the VTT. */
9135 inits = NULL;
9136 index = size_zero_node;
9137 build_vtt_inits (TYPE_BINFO (t), t, &inits, &index);
9139 /* If we didn't need a VTT, we're done. */
9140 if (!inits)
9141 return;
9143 /* Figure out the type of the VTT. */
9144 type = build_array_of_n_type (const_ptr_type_node,
9145 inits->length ());
9147 /* Now, build the VTT object itself. */
9148 vtt = build_vtable (t, mangle_vtt_for_type (t), type);
9149 initialize_artificial_var (vtt, inits);
9150 /* Add the VTT to the vtables list. */
9151 DECL_CHAIN (vtt) = DECL_CHAIN (CLASSTYPE_VTABLES (t));
9152 DECL_CHAIN (CLASSTYPE_VTABLES (t)) = vtt;
9154 dump_vtt (t, vtt);
9157 /* When building a secondary VTT, BINFO_VTABLE is set to a TREE_LIST with
9158 PURPOSE the RTTI_BINFO, VALUE the real vtable pointer for this binfo,
9159 and CHAIN the vtable pointer for this binfo after construction is
9160 complete. VALUE can also be another BINFO, in which case we recurse. */
9162 static tree
9163 binfo_ctor_vtable (tree binfo)
9165 tree vt;
9167 while (1)
9169 vt = BINFO_VTABLE (binfo);
9170 if (TREE_CODE (vt) == TREE_LIST)
9171 vt = TREE_VALUE (vt);
9172 if (TREE_CODE (vt) == TREE_BINFO)
9173 binfo = vt;
9174 else
9175 break;
9178 return vt;
9181 /* Data for secondary VTT initialization. */
9182 struct secondary_vptr_vtt_init_data
9184 /* Is this the primary VTT? */
9185 bool top_level_p;
9187 /* Current index into the VTT. */
9188 tree index;
9190 /* Vector of initializers built up. */
9191 vec<constructor_elt, va_gc> *inits;
9193 /* The type being constructed by this secondary VTT. */
9194 tree type_being_constructed;
9197 /* Recursively build the VTT-initializer for BINFO (which is in the
9198 hierarchy dominated by T). INITS points to the end of the initializer
9199 list to date. INDEX is the VTT index where the next element will be
9200 replaced. Iff BINFO is the binfo for T, this is the top level VTT (i.e.
9201 not a subvtt for some base of T). When that is so, we emit the sub-VTTs
9202 for virtual bases of T. When it is not so, we build the constructor
9203 vtables for the BINFO-in-T variant. */
9205 static void
9206 build_vtt_inits (tree binfo, tree t, vec<constructor_elt, va_gc> **inits,
9207 tree *index)
9209 int i;
9210 tree b;
9211 tree init;
9212 secondary_vptr_vtt_init_data data;
9213 int top_level_p = SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), t);
9215 /* We only need VTTs for subobjects with virtual bases. */
9216 if (!CLASSTYPE_VBASECLASSES (BINFO_TYPE (binfo)))
9217 return;
9219 /* We need to use a construction vtable if this is not the primary
9220 VTT. */
9221 if (!top_level_p)
9223 build_ctor_vtbl_group (binfo, t);
9225 /* Record the offset in the VTT where this sub-VTT can be found. */
9226 BINFO_SUBVTT_INDEX (binfo) = *index;
9229 /* Add the address of the primary vtable for the complete object. */
9230 init = binfo_ctor_vtable (binfo);
9231 CONSTRUCTOR_APPEND_ELT (*inits, NULL_TREE, init);
9232 if (top_level_p)
9234 gcc_assert (!BINFO_VPTR_INDEX (binfo));
9235 BINFO_VPTR_INDEX (binfo) = *index;
9237 *index = size_binop (PLUS_EXPR, *index, TYPE_SIZE_UNIT (ptr_type_node));
9239 /* Recursively add the secondary VTTs for non-virtual bases. */
9240 for (i = 0; BINFO_BASE_ITERATE (binfo, i, b); ++i)
9241 if (!BINFO_VIRTUAL_P (b))
9242 build_vtt_inits (b, t, inits, index);
9244 /* Add secondary virtual pointers for all subobjects of BINFO with
9245 either virtual bases or reachable along a virtual path, except
9246 subobjects that are non-virtual primary bases. */
9247 data.top_level_p = top_level_p;
9248 data.index = *index;
9249 data.inits = *inits;
9250 data.type_being_constructed = BINFO_TYPE (binfo);
9252 dfs_walk_once (binfo, dfs_build_secondary_vptr_vtt_inits, NULL, &data);
9254 *index = data.index;
9256 /* data.inits might have grown as we added secondary virtual pointers.
9257 Make sure our caller knows about the new vector. */
9258 *inits = data.inits;
9260 if (top_level_p)
9261 /* Add the secondary VTTs for virtual bases in inheritance graph
9262 order. */
9263 for (b = TYPE_BINFO (BINFO_TYPE (binfo)); b; b = TREE_CHAIN (b))
9265 if (!BINFO_VIRTUAL_P (b))
9266 continue;
9268 build_vtt_inits (b, t, inits, index);
9270 else
9271 /* Remove the ctor vtables we created. */
9272 dfs_walk_all (binfo, dfs_fixup_binfo_vtbls, NULL, binfo);
9275 /* Called from build_vtt_inits via dfs_walk. BINFO is the binfo for the base
9276 in most derived. DATA is a SECONDARY_VPTR_VTT_INIT_DATA structure. */
9278 static tree
9279 dfs_build_secondary_vptr_vtt_inits (tree binfo, void *data_)
9281 secondary_vptr_vtt_init_data *data = (secondary_vptr_vtt_init_data *)data_;
9283 /* We don't care about bases that don't have vtables. */
9284 if (!TYPE_VFIELD (BINFO_TYPE (binfo)))
9285 return dfs_skip_bases;
9287 /* We're only interested in proper subobjects of the type being
9288 constructed. */
9289 if (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), data->type_being_constructed))
9290 return NULL_TREE;
9292 /* We're only interested in bases with virtual bases or reachable
9293 via a virtual path from the type being constructed. */
9294 if (!(CLASSTYPE_VBASECLASSES (BINFO_TYPE (binfo))
9295 || binfo_via_virtual (binfo, data->type_being_constructed)))
9296 return dfs_skip_bases;
9298 /* We're not interested in non-virtual primary bases. */
9299 if (!BINFO_VIRTUAL_P (binfo) && BINFO_PRIMARY_P (binfo))
9300 return NULL_TREE;
9302 /* Record the index where this secondary vptr can be found. */
9303 if (data->top_level_p)
9305 gcc_assert (!BINFO_VPTR_INDEX (binfo));
9306 BINFO_VPTR_INDEX (binfo) = data->index;
9308 if (BINFO_VIRTUAL_P (binfo))
9310 /* It's a primary virtual base, and this is not a
9311 construction vtable. Find the base this is primary of in
9312 the inheritance graph, and use that base's vtable
9313 now. */
9314 while (BINFO_PRIMARY_P (binfo))
9315 binfo = BINFO_INHERITANCE_CHAIN (binfo);
9319 /* Add the initializer for the secondary vptr itself. */
9320 CONSTRUCTOR_APPEND_ELT (data->inits, NULL_TREE, binfo_ctor_vtable (binfo));
9322 /* Advance the vtt index. */
9323 data->index = size_binop (PLUS_EXPR, data->index,
9324 TYPE_SIZE_UNIT (ptr_type_node));
9326 return NULL_TREE;
9329 /* Called from build_vtt_inits via dfs_walk. After building
9330 constructor vtables and generating the sub-vtt from them, we need
9331 to restore the BINFO_VTABLES that were scribbled on. DATA is the
9332 binfo of the base whose sub vtt was generated. */
9334 static tree
9335 dfs_fixup_binfo_vtbls (tree binfo, void* data)
9337 tree vtable = BINFO_VTABLE (binfo);
9339 if (!TYPE_CONTAINS_VPTR_P (BINFO_TYPE (binfo)))
9340 /* If this class has no vtable, none of its bases do. */
9341 return dfs_skip_bases;
9343 if (!vtable)
9344 /* This might be a primary base, so have no vtable in this
9345 hierarchy. */
9346 return NULL_TREE;
9348 /* If we scribbled the construction vtable vptr into BINFO, clear it
9349 out now. */
9350 if (TREE_CODE (vtable) == TREE_LIST
9351 && (TREE_PURPOSE (vtable) == (tree) data))
9352 BINFO_VTABLE (binfo) = TREE_CHAIN (vtable);
9354 return NULL_TREE;
9357 /* Build the construction vtable group for BINFO which is in the
9358 hierarchy dominated by T. */
9360 static void
9361 build_ctor_vtbl_group (tree binfo, tree t)
9363 tree type;
9364 tree vtbl;
9365 tree id;
9366 tree vbase;
9367 vec<constructor_elt, va_gc> *v;
9369 /* See if we've already created this construction vtable group. */
9370 id = mangle_ctor_vtbl_for_type (t, binfo);
9371 if (IDENTIFIER_GLOBAL_VALUE (id))
9372 return;
9374 gcc_assert (!SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), t));
9375 /* Build a version of VTBL (with the wrong type) for use in
9376 constructing the addresses of secondary vtables in the
9377 construction vtable group. */
9378 vtbl = build_vtable (t, id, ptr_type_node);
9379 DECL_CONSTRUCTION_VTABLE_P (vtbl) = 1;
9380 /* Don't export construction vtables from shared libraries. Even on
9381 targets that don't support hidden visibility, this tells
9382 can_refer_decl_in_current_unit_p not to assume that it's safe to
9383 access from a different compilation unit (bz 54314). */
9384 DECL_VISIBILITY (vtbl) = VISIBILITY_HIDDEN;
9385 DECL_VISIBILITY_SPECIFIED (vtbl) = true;
9387 v = NULL;
9388 accumulate_vtbl_inits (binfo, TYPE_BINFO (TREE_TYPE (binfo)),
9389 binfo, vtbl, t, &v);
9391 /* Add the vtables for each of our virtual bases using the vbase in T
9392 binfo. */
9393 for (vbase = TYPE_BINFO (BINFO_TYPE (binfo));
9394 vbase;
9395 vbase = TREE_CHAIN (vbase))
9397 tree b;
9399 if (!BINFO_VIRTUAL_P (vbase))
9400 continue;
9401 b = copied_binfo (vbase, binfo);
9403 accumulate_vtbl_inits (b, vbase, binfo, vtbl, t, &v);
9406 /* Figure out the type of the construction vtable. */
9407 type = build_array_of_n_type (vtable_entry_type, v->length ());
9408 layout_type (type);
9409 TREE_TYPE (vtbl) = type;
9410 DECL_SIZE (vtbl) = DECL_SIZE_UNIT (vtbl) = NULL_TREE;
9411 layout_decl (vtbl, 0);
9413 /* Initialize the construction vtable. */
9414 CLASSTYPE_VTABLES (t) = chainon (CLASSTYPE_VTABLES (t), vtbl);
9415 initialize_artificial_var (vtbl, v);
9416 dump_vtable (t, binfo, vtbl);
9419 /* Add the vtbl initializers for BINFO (and its bases other than
9420 non-virtual primaries) to the list of INITS. BINFO is in the
9421 hierarchy dominated by T. RTTI_BINFO is the binfo within T of
9422 the constructor the vtbl inits should be accumulated for. (If this
9423 is the complete object vtbl then RTTI_BINFO will be TYPE_BINFO (T).)
9424 ORIG_BINFO is the binfo for this object within BINFO_TYPE (RTTI_BINFO).
9425 BINFO is the active base equivalent of ORIG_BINFO in the inheritance
9426 graph of T. Both BINFO and ORIG_BINFO will have the same BINFO_TYPE,
9427 but are not necessarily the same in terms of layout. */
9429 static void
9430 accumulate_vtbl_inits (tree binfo,
9431 tree orig_binfo,
9432 tree rtti_binfo,
9433 tree vtbl,
9434 tree t,
9435 vec<constructor_elt, va_gc> **inits)
9437 int i;
9438 tree base_binfo;
9439 int ctor_vtbl_p = !SAME_BINFO_TYPE_P (BINFO_TYPE (rtti_binfo), t);
9441 gcc_assert (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), BINFO_TYPE (orig_binfo)));
9443 /* If it doesn't have a vptr, we don't do anything. */
9444 if (!TYPE_CONTAINS_VPTR_P (BINFO_TYPE (binfo)))
9445 return;
9447 /* If we're building a construction vtable, we're not interested in
9448 subobjects that don't require construction vtables. */
9449 if (ctor_vtbl_p
9450 && !CLASSTYPE_VBASECLASSES (BINFO_TYPE (binfo))
9451 && !binfo_via_virtual (orig_binfo, BINFO_TYPE (rtti_binfo)))
9452 return;
9454 /* Build the initializers for the BINFO-in-T vtable. */
9455 dfs_accumulate_vtbl_inits (binfo, orig_binfo, rtti_binfo, vtbl, t, inits);
9457 /* Walk the BINFO and its bases. We walk in preorder so that as we
9458 initialize each vtable we can figure out at what offset the
9459 secondary vtable lies from the primary vtable. We can't use
9460 dfs_walk here because we need to iterate through bases of BINFO
9461 and RTTI_BINFO simultaneously. */
9462 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
9464 /* Skip virtual bases. */
9465 if (BINFO_VIRTUAL_P (base_binfo))
9466 continue;
9467 accumulate_vtbl_inits (base_binfo,
9468 BINFO_BASE_BINFO (orig_binfo, i),
9469 rtti_binfo, vtbl, t,
9470 inits);
9474 /* Called from accumulate_vtbl_inits. Adds the initializers for the
9475 BINFO vtable to L. */
9477 static void
9478 dfs_accumulate_vtbl_inits (tree binfo,
9479 tree orig_binfo,
9480 tree rtti_binfo,
9481 tree orig_vtbl,
9482 tree t,
9483 vec<constructor_elt, va_gc> **l)
9485 tree vtbl = NULL_TREE;
9486 int ctor_vtbl_p = !SAME_BINFO_TYPE_P (BINFO_TYPE (rtti_binfo), t);
9487 int n_inits;
9489 if (ctor_vtbl_p
9490 && BINFO_VIRTUAL_P (orig_binfo) && BINFO_PRIMARY_P (orig_binfo))
9492 /* In the hierarchy of BINFO_TYPE (RTTI_BINFO), this is a
9493 primary virtual base. If it is not the same primary in
9494 the hierarchy of T, we'll need to generate a ctor vtable
9495 for it, to place at its location in T. If it is the same
9496 primary, we still need a VTT entry for the vtable, but it
9497 should point to the ctor vtable for the base it is a
9498 primary for within the sub-hierarchy of RTTI_BINFO.
9500 There are three possible cases:
9502 1) We are in the same place.
9503 2) We are a primary base within a lost primary virtual base of
9504 RTTI_BINFO.
9505 3) We are primary to something not a base of RTTI_BINFO. */
9507 tree b;
9508 tree last = NULL_TREE;
9510 /* First, look through the bases we are primary to for RTTI_BINFO
9511 or a virtual base. */
9512 b = binfo;
9513 while (BINFO_PRIMARY_P (b))
9515 b = BINFO_INHERITANCE_CHAIN (b);
9516 last = b;
9517 if (BINFO_VIRTUAL_P (b) || b == rtti_binfo)
9518 goto found;
9520 /* If we run out of primary links, keep looking down our
9521 inheritance chain; we might be an indirect primary. */
9522 for (b = last; b; b = BINFO_INHERITANCE_CHAIN (b))
9523 if (BINFO_VIRTUAL_P (b) || b == rtti_binfo)
9524 break;
9525 found:
9527 /* If we found RTTI_BINFO, this is case 1. If we found a virtual
9528 base B and it is a base of RTTI_BINFO, this is case 2. In
9529 either case, we share our vtable with LAST, i.e. the
9530 derived-most base within B of which we are a primary. */
9531 if (b == rtti_binfo
9532 || (b && binfo_for_vbase (BINFO_TYPE (b), BINFO_TYPE (rtti_binfo))))
9533 /* Just set our BINFO_VTABLE to point to LAST, as we may not have
9534 set LAST's BINFO_VTABLE yet. We'll extract the actual vptr in
9535 binfo_ctor_vtable after everything's been set up. */
9536 vtbl = last;
9538 /* Otherwise, this is case 3 and we get our own. */
9540 else if (!BINFO_NEW_VTABLE_MARKED (orig_binfo))
9541 return;
9543 n_inits = vec_safe_length (*l);
9545 if (!vtbl)
9547 tree index;
9548 int non_fn_entries;
9550 /* Add the initializer for this vtable. */
9551 build_vtbl_initializer (binfo, orig_binfo, t, rtti_binfo,
9552 &non_fn_entries, l);
9554 /* Figure out the position to which the VPTR should point. */
9555 vtbl = build1 (ADDR_EXPR, vtbl_ptr_type_node, orig_vtbl);
9556 index = size_binop (MULT_EXPR,
9557 TYPE_SIZE_UNIT (vtable_entry_type),
9558 size_int (non_fn_entries + n_inits));
9559 vtbl = fold_build_pointer_plus (vtbl, index);
9562 if (ctor_vtbl_p)
9563 /* For a construction vtable, we can't overwrite BINFO_VTABLE.
9564 So, we make a TREE_LIST. Later, dfs_fixup_binfo_vtbls will
9565 straighten this out. */
9566 BINFO_VTABLE (binfo) = tree_cons (rtti_binfo, vtbl, BINFO_VTABLE (binfo));
9567 else if (BINFO_PRIMARY_P (binfo) && BINFO_VIRTUAL_P (binfo))
9568 /* Throw away any unneeded intializers. */
9569 (*l)->truncate (n_inits);
9570 else
9571 /* For an ordinary vtable, set BINFO_VTABLE. */
9572 BINFO_VTABLE (binfo) = vtbl;
9575 static GTY(()) tree abort_fndecl_addr;
9577 /* Construct the initializer for BINFO's virtual function table. BINFO
9578 is part of the hierarchy dominated by T. If we're building a
9579 construction vtable, the ORIG_BINFO is the binfo we should use to
9580 find the actual function pointers to put in the vtable - but they
9581 can be overridden on the path to most-derived in the graph that
9582 ORIG_BINFO belongs. Otherwise,
9583 ORIG_BINFO should be the same as BINFO. The RTTI_BINFO is the
9584 BINFO that should be indicated by the RTTI information in the
9585 vtable; it will be a base class of T, rather than T itself, if we
9586 are building a construction vtable.
9588 The value returned is a TREE_LIST suitable for wrapping in a
9589 CONSTRUCTOR to use as the DECL_INITIAL for a vtable. If
9590 NON_FN_ENTRIES_P is not NULL, *NON_FN_ENTRIES_P is set to the
9591 number of non-function entries in the vtable.
9593 It might seem that this function should never be called with a
9594 BINFO for which BINFO_PRIMARY_P holds, the vtable for such a
9595 base is always subsumed by a derived class vtable. However, when
9596 we are building construction vtables, we do build vtables for
9597 primary bases; we need these while the primary base is being
9598 constructed. */
9600 static void
9601 build_vtbl_initializer (tree binfo,
9602 tree orig_binfo,
9603 tree t,
9604 tree rtti_binfo,
9605 int* non_fn_entries_p,
9606 vec<constructor_elt, va_gc> **inits)
9608 tree v;
9609 vtbl_init_data vid;
9610 unsigned ix, jx;
9611 tree vbinfo;
9612 vec<tree, va_gc> *vbases;
9613 constructor_elt *e;
9615 /* Initialize VID. */
9616 memset (&vid, 0, sizeof (vid));
9617 vid.binfo = binfo;
9618 vid.derived = t;
9619 vid.rtti_binfo = rtti_binfo;
9620 vid.primary_vtbl_p = SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), t);
9621 vid.ctor_vtbl_p = !SAME_BINFO_TYPE_P (BINFO_TYPE (rtti_binfo), t);
9622 vid.generate_vcall_entries = true;
9623 /* The first vbase or vcall offset is at index -3 in the vtable. */
9624 vid.index = ssize_int(-3 * TARGET_VTABLE_DATA_ENTRY_DISTANCE);
9626 /* Add entries to the vtable for RTTI. */
9627 build_rtti_vtbl_entries (binfo, &vid);
9629 /* Create an array for keeping track of the functions we've
9630 processed. When we see multiple functions with the same
9631 signature, we share the vcall offsets. */
9632 vec_alloc (vid.fns, 32);
9633 /* Add the vcall and vbase offset entries. */
9634 build_vcall_and_vbase_vtbl_entries (binfo, &vid);
9636 /* Clear BINFO_VTABLE_PATH_MARKED; it's set by
9637 build_vbase_offset_vtbl_entries. */
9638 for (vbases = CLASSTYPE_VBASECLASSES (t), ix = 0;
9639 vec_safe_iterate (vbases, ix, &vbinfo); ix++)
9640 BINFO_VTABLE_PATH_MARKED (vbinfo) = 0;
9642 /* If the target requires padding between data entries, add that now. */
9643 if (TARGET_VTABLE_DATA_ENTRY_DISTANCE > 1)
9645 int n_entries = vec_safe_length (vid.inits);
9647 vec_safe_grow (vid.inits, TARGET_VTABLE_DATA_ENTRY_DISTANCE * n_entries);
9649 /* Move data entries into their new positions and add padding
9650 after the new positions. Iterate backwards so we don't
9651 overwrite entries that we would need to process later. */
9652 for (ix = n_entries - 1;
9653 vid.inits->iterate (ix, &e);
9654 ix--)
9656 int j;
9657 int new_position = (TARGET_VTABLE_DATA_ENTRY_DISTANCE * ix
9658 + (TARGET_VTABLE_DATA_ENTRY_DISTANCE - 1));
9660 (*vid.inits)[new_position] = *e;
9662 for (j = 1; j < TARGET_VTABLE_DATA_ENTRY_DISTANCE; ++j)
9664 constructor_elt *f = &(*vid.inits)[new_position - j];
9665 f->index = NULL_TREE;
9666 f->value = build1 (NOP_EXPR, vtable_entry_type,
9667 null_pointer_node);
9672 if (non_fn_entries_p)
9673 *non_fn_entries_p = vec_safe_length (vid.inits);
9675 /* The initializers for virtual functions were built up in reverse
9676 order. Straighten them out and add them to the running list in one
9677 step. */
9678 jx = vec_safe_length (*inits);
9679 vec_safe_grow (*inits, jx + vid.inits->length ());
9681 for (ix = vid.inits->length () - 1;
9682 vid.inits->iterate (ix, &e);
9683 ix--, jx++)
9684 (**inits)[jx] = *e;
9686 /* Go through all the ordinary virtual functions, building up
9687 initializers. */
9688 for (v = BINFO_VIRTUALS (orig_binfo); v; v = TREE_CHAIN (v))
9690 tree delta;
9691 tree vcall_index;
9692 tree fn, fn_original;
9693 tree init = NULL_TREE;
9695 fn = BV_FN (v);
9696 fn_original = fn;
9697 if (DECL_THUNK_P (fn))
9699 if (!DECL_NAME (fn))
9700 finish_thunk (fn);
9701 if (THUNK_ALIAS (fn))
9703 fn = THUNK_ALIAS (fn);
9704 BV_FN (v) = fn;
9706 fn_original = THUNK_TARGET (fn);
9709 /* If the only definition of this function signature along our
9710 primary base chain is from a lost primary, this vtable slot will
9711 never be used, so just zero it out. This is important to avoid
9712 requiring extra thunks which cannot be generated with the function.
9714 We first check this in update_vtable_entry_for_fn, so we handle
9715 restored primary bases properly; we also need to do it here so we
9716 zero out unused slots in ctor vtables, rather than filling them
9717 with erroneous values (though harmless, apart from relocation
9718 costs). */
9719 if (BV_LOST_PRIMARY (v))
9720 init = size_zero_node;
9722 if (! init)
9724 /* Pull the offset for `this', and the function to call, out of
9725 the list. */
9726 delta = BV_DELTA (v);
9727 vcall_index = BV_VCALL_INDEX (v);
9729 gcc_assert (TREE_CODE (delta) == INTEGER_CST);
9730 gcc_assert (TREE_CODE (fn) == FUNCTION_DECL);
9732 /* You can't call an abstract virtual function; it's abstract.
9733 So, we replace these functions with __pure_virtual. */
9734 if (DECL_PURE_VIRTUAL_P (fn_original))
9736 fn = abort_fndecl;
9737 if (!TARGET_VTABLE_USES_DESCRIPTORS)
9739 if (abort_fndecl_addr == NULL)
9740 abort_fndecl_addr
9741 = fold_convert (vfunc_ptr_type_node,
9742 build_fold_addr_expr (fn));
9743 init = abort_fndecl_addr;
9746 /* Likewise for deleted virtuals. */
9747 else if (DECL_DELETED_FN (fn_original))
9749 fn = get_identifier ("__cxa_deleted_virtual");
9750 if (!get_global_value_if_present (fn, &fn))
9751 fn = push_library_fn (fn, (build_function_type_list
9752 (void_type_node, NULL_TREE)),
9753 NULL_TREE, ECF_NORETURN);
9754 if (!TARGET_VTABLE_USES_DESCRIPTORS)
9755 init = fold_convert (vfunc_ptr_type_node,
9756 build_fold_addr_expr (fn));
9758 else
9760 if (!integer_zerop (delta) || vcall_index)
9762 fn = make_thunk (fn, /*this_adjusting=*/1, delta, vcall_index);
9763 if (!DECL_NAME (fn))
9764 finish_thunk (fn);
9766 /* Take the address of the function, considering it to be of an
9767 appropriate generic type. */
9768 if (!TARGET_VTABLE_USES_DESCRIPTORS)
9769 init = fold_convert (vfunc_ptr_type_node,
9770 build_fold_addr_expr (fn));
9771 /* Don't refer to a virtual destructor from a constructor
9772 vtable or a vtable for an abstract class, since destroying
9773 an object under construction is undefined behavior and we
9774 don't want it to be considered a candidate for speculative
9775 devirtualization. But do create the thunk for ABI
9776 compliance. */
9777 if (DECL_DESTRUCTOR_P (fn_original)
9778 && (CLASSTYPE_PURE_VIRTUALS (DECL_CONTEXT (fn_original))
9779 || orig_binfo != binfo))
9780 init = size_zero_node;
9784 /* And add it to the chain of initializers. */
9785 if (TARGET_VTABLE_USES_DESCRIPTORS)
9787 int i;
9788 if (init == size_zero_node)
9789 for (i = 0; i < TARGET_VTABLE_USES_DESCRIPTORS; ++i)
9790 CONSTRUCTOR_APPEND_ELT (*inits, NULL_TREE, init);
9791 else
9792 for (i = 0; i < TARGET_VTABLE_USES_DESCRIPTORS; ++i)
9794 tree fdesc = build2 (FDESC_EXPR, vfunc_ptr_type_node,
9795 fn, build_int_cst (NULL_TREE, i));
9796 TREE_CONSTANT (fdesc) = 1;
9798 CONSTRUCTOR_APPEND_ELT (*inits, NULL_TREE, fdesc);
9801 else
9802 CONSTRUCTOR_APPEND_ELT (*inits, NULL_TREE, init);
9806 /* Adds to vid->inits the initializers for the vbase and vcall
9807 offsets in BINFO, which is in the hierarchy dominated by T. */
9809 static void
9810 build_vcall_and_vbase_vtbl_entries (tree binfo, vtbl_init_data* vid)
9812 tree b;
9814 /* If this is a derived class, we must first create entries
9815 corresponding to the primary base class. */
9816 b = get_primary_binfo (binfo);
9817 if (b)
9818 build_vcall_and_vbase_vtbl_entries (b, vid);
9820 /* Add the vbase entries for this base. */
9821 build_vbase_offset_vtbl_entries (binfo, vid);
9822 /* Add the vcall entries for this base. */
9823 build_vcall_offset_vtbl_entries (binfo, vid);
9826 /* Returns the initializers for the vbase offset entries in the vtable
9827 for BINFO (which is part of the class hierarchy dominated by T), in
9828 reverse order. VBASE_OFFSET_INDEX gives the vtable index
9829 where the next vbase offset will go. */
9831 static void
9832 build_vbase_offset_vtbl_entries (tree binfo, vtbl_init_data* vid)
9834 tree vbase;
9835 tree t;
9836 tree non_primary_binfo;
9838 /* If there are no virtual baseclasses, then there is nothing to
9839 do. */
9840 if (!CLASSTYPE_VBASECLASSES (BINFO_TYPE (binfo)))
9841 return;
9843 t = vid->derived;
9845 /* We might be a primary base class. Go up the inheritance hierarchy
9846 until we find the most derived class of which we are a primary base:
9847 it is the offset of that which we need to use. */
9848 non_primary_binfo = binfo;
9849 while (BINFO_INHERITANCE_CHAIN (non_primary_binfo))
9851 tree b;
9853 /* If we have reached a virtual base, then it must be a primary
9854 base (possibly multi-level) of vid->binfo, or we wouldn't
9855 have called build_vcall_and_vbase_vtbl_entries for it. But it
9856 might be a lost primary, so just skip down to vid->binfo. */
9857 if (BINFO_VIRTUAL_P (non_primary_binfo))
9859 non_primary_binfo = vid->binfo;
9860 break;
9863 b = BINFO_INHERITANCE_CHAIN (non_primary_binfo);
9864 if (get_primary_binfo (b) != non_primary_binfo)
9865 break;
9866 non_primary_binfo = b;
9869 /* Go through the virtual bases, adding the offsets. */
9870 for (vbase = TYPE_BINFO (BINFO_TYPE (binfo));
9871 vbase;
9872 vbase = TREE_CHAIN (vbase))
9874 tree b;
9875 tree delta;
9877 if (!BINFO_VIRTUAL_P (vbase))
9878 continue;
9880 /* Find the instance of this virtual base in the complete
9881 object. */
9882 b = copied_binfo (vbase, binfo);
9884 /* If we've already got an offset for this virtual base, we
9885 don't need another one. */
9886 if (BINFO_VTABLE_PATH_MARKED (b))
9887 continue;
9888 BINFO_VTABLE_PATH_MARKED (b) = 1;
9890 /* Figure out where we can find this vbase offset. */
9891 delta = size_binop (MULT_EXPR,
9892 vid->index,
9893 fold_convert (ssizetype,
9894 TYPE_SIZE_UNIT (vtable_entry_type)));
9895 if (vid->primary_vtbl_p)
9896 BINFO_VPTR_FIELD (b) = delta;
9898 if (binfo != TYPE_BINFO (t))
9899 /* The vbase offset had better be the same. */
9900 gcc_assert (tree_int_cst_equal (delta, BINFO_VPTR_FIELD (vbase)));
9902 /* The next vbase will come at a more negative offset. */
9903 vid->index = size_binop (MINUS_EXPR, vid->index,
9904 ssize_int (TARGET_VTABLE_DATA_ENTRY_DISTANCE));
9906 /* The initializer is the delta from BINFO to this virtual base.
9907 The vbase offsets go in reverse inheritance-graph order, and
9908 we are walking in inheritance graph order so these end up in
9909 the right order. */
9910 delta = size_diffop_loc (input_location,
9911 BINFO_OFFSET (b), BINFO_OFFSET (non_primary_binfo));
9913 CONSTRUCTOR_APPEND_ELT (vid->inits, NULL_TREE,
9914 fold_build1_loc (input_location, NOP_EXPR,
9915 vtable_entry_type, delta));
9919 /* Adds the initializers for the vcall offset entries in the vtable
9920 for BINFO (which is part of the class hierarchy dominated by VID->DERIVED)
9921 to VID->INITS. */
9923 static void
9924 build_vcall_offset_vtbl_entries (tree binfo, vtbl_init_data* vid)
9926 /* We only need these entries if this base is a virtual base. We
9927 compute the indices -- but do not add to the vtable -- when
9928 building the main vtable for a class. */
9929 if (binfo == TYPE_BINFO (vid->derived)
9930 || (BINFO_VIRTUAL_P (binfo)
9931 /* If BINFO is RTTI_BINFO, then (since BINFO does not
9932 correspond to VID->DERIVED), we are building a primary
9933 construction virtual table. Since this is a primary
9934 virtual table, we do not need the vcall offsets for
9935 BINFO. */
9936 && binfo != vid->rtti_binfo))
9938 /* We need a vcall offset for each of the virtual functions in this
9939 vtable. For example:
9941 class A { virtual void f (); };
9942 class B1 : virtual public A { virtual void f (); };
9943 class B2 : virtual public A { virtual void f (); };
9944 class C: public B1, public B2 { virtual void f (); };
9946 A C object has a primary base of B1, which has a primary base of A. A
9947 C also has a secondary base of B2, which no longer has a primary base
9948 of A. So the B2-in-C construction vtable needs a secondary vtable for
9949 A, which will adjust the A* to a B2* to call f. We have no way of
9950 knowing what (or even whether) this offset will be when we define B2,
9951 so we store this "vcall offset" in the A sub-vtable and look it up in
9952 a "virtual thunk" for B2::f.
9954 We need entries for all the functions in our primary vtable and
9955 in our non-virtual bases' secondary vtables. */
9956 vid->vbase = binfo;
9957 /* If we are just computing the vcall indices -- but do not need
9958 the actual entries -- not that. */
9959 if (!BINFO_VIRTUAL_P (binfo))
9960 vid->generate_vcall_entries = false;
9961 /* Now, walk through the non-virtual bases, adding vcall offsets. */
9962 add_vcall_offset_vtbl_entries_r (binfo, vid);
9966 /* Build vcall offsets, starting with those for BINFO. */
9968 static void
9969 add_vcall_offset_vtbl_entries_r (tree binfo, vtbl_init_data* vid)
9971 int i;
9972 tree primary_binfo;
9973 tree base_binfo;
9975 /* Don't walk into virtual bases -- except, of course, for the
9976 virtual base for which we are building vcall offsets. Any
9977 primary virtual base will have already had its offsets generated
9978 through the recursion in build_vcall_and_vbase_vtbl_entries. */
9979 if (BINFO_VIRTUAL_P (binfo) && vid->vbase != binfo)
9980 return;
9982 /* If BINFO has a primary base, process it first. */
9983 primary_binfo = get_primary_binfo (binfo);
9984 if (primary_binfo)
9985 add_vcall_offset_vtbl_entries_r (primary_binfo, vid);
9987 /* Add BINFO itself to the list. */
9988 add_vcall_offset_vtbl_entries_1 (binfo, vid);
9990 /* Scan the non-primary bases of BINFO. */
9991 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
9992 if (base_binfo != primary_binfo)
9993 add_vcall_offset_vtbl_entries_r (base_binfo, vid);
9996 /* Called from build_vcall_offset_vtbl_entries_r. */
9998 static void
9999 add_vcall_offset_vtbl_entries_1 (tree binfo, vtbl_init_data* vid)
10001 /* Make entries for the rest of the virtuals. */
10002 tree orig_fn;
10004 /* The ABI requires that the methods be processed in declaration
10005 order. */
10006 for (orig_fn = TYPE_METHODS (BINFO_TYPE (binfo));
10007 orig_fn;
10008 orig_fn = DECL_CHAIN (orig_fn))
10009 if (TREE_CODE (orig_fn) == FUNCTION_DECL && DECL_VINDEX (orig_fn))
10010 add_vcall_offset (orig_fn, binfo, vid);
10013 /* Add a vcall offset entry for ORIG_FN to the vtable. */
10015 static void
10016 add_vcall_offset (tree orig_fn, tree binfo, vtbl_init_data *vid)
10018 size_t i;
10019 tree vcall_offset;
10020 tree derived_entry;
10022 /* If there is already an entry for a function with the same
10023 signature as FN, then we do not need a second vcall offset.
10024 Check the list of functions already present in the derived
10025 class vtable. */
10026 FOR_EACH_VEC_SAFE_ELT (vid->fns, i, derived_entry)
10028 if (same_signature_p (derived_entry, orig_fn)
10029 /* We only use one vcall offset for virtual destructors,
10030 even though there are two virtual table entries. */
10031 || (DECL_DESTRUCTOR_P (derived_entry)
10032 && DECL_DESTRUCTOR_P (orig_fn)))
10033 return;
10036 /* If we are building these vcall offsets as part of building
10037 the vtable for the most derived class, remember the vcall
10038 offset. */
10039 if (vid->binfo == TYPE_BINFO (vid->derived))
10041 tree_pair_s elt = {orig_fn, vid->index};
10042 vec_safe_push (CLASSTYPE_VCALL_INDICES (vid->derived), elt);
10045 /* The next vcall offset will be found at a more negative
10046 offset. */
10047 vid->index = size_binop (MINUS_EXPR, vid->index,
10048 ssize_int (TARGET_VTABLE_DATA_ENTRY_DISTANCE));
10050 /* Keep track of this function. */
10051 vec_safe_push (vid->fns, orig_fn);
10053 if (vid->generate_vcall_entries)
10055 tree base;
10056 tree fn;
10058 /* Find the overriding function. */
10059 fn = find_final_overrider (vid->rtti_binfo, binfo, orig_fn);
10060 if (fn == error_mark_node)
10061 vcall_offset = build_zero_cst (vtable_entry_type);
10062 else
10064 base = TREE_VALUE (fn);
10066 /* The vbase we're working on is a primary base of
10067 vid->binfo. But it might be a lost primary, so its
10068 BINFO_OFFSET might be wrong, so we just use the
10069 BINFO_OFFSET from vid->binfo. */
10070 vcall_offset = size_diffop_loc (input_location,
10071 BINFO_OFFSET (base),
10072 BINFO_OFFSET (vid->binfo));
10073 vcall_offset = fold_build1_loc (input_location,
10074 NOP_EXPR, vtable_entry_type,
10075 vcall_offset);
10077 /* Add the initializer to the vtable. */
10078 CONSTRUCTOR_APPEND_ELT (vid->inits, NULL_TREE, vcall_offset);
10082 /* Return vtbl initializers for the RTTI entries corresponding to the
10083 BINFO's vtable. The RTTI entries should indicate the object given
10084 by VID->rtti_binfo. */
10086 static void
10087 build_rtti_vtbl_entries (tree binfo, vtbl_init_data* vid)
10089 tree b;
10090 tree t;
10091 tree offset;
10092 tree decl;
10093 tree init;
10095 t = BINFO_TYPE (vid->rtti_binfo);
10097 /* To find the complete object, we will first convert to our most
10098 primary base, and then add the offset in the vtbl to that value. */
10099 b = most_primary_binfo (binfo);
10100 offset = size_diffop_loc (input_location,
10101 BINFO_OFFSET (vid->rtti_binfo), BINFO_OFFSET (b));
10103 /* The second entry is the address of the typeinfo object. */
10104 if (flag_rtti)
10105 decl = build_address (get_tinfo_decl (t));
10106 else
10107 decl = integer_zero_node;
10109 /* Convert the declaration to a type that can be stored in the
10110 vtable. */
10111 init = build_nop (vfunc_ptr_type_node, decl);
10112 CONSTRUCTOR_APPEND_ELT (vid->inits, NULL_TREE, init);
10114 /* Add the offset-to-top entry. It comes earlier in the vtable than
10115 the typeinfo entry. Convert the offset to look like a
10116 function pointer, so that we can put it in the vtable. */
10117 init = build_nop (vfunc_ptr_type_node, offset);
10118 CONSTRUCTOR_APPEND_ELT (vid->inits, NULL_TREE, init);
10121 /* TRUE iff TYPE is uniquely derived from PARENT. Ignores
10122 accessibility. */
10124 bool
10125 uniquely_derived_from_p (tree parent, tree type)
10127 tree base = lookup_base (type, parent, ba_unique, NULL, tf_none);
10128 return base && base != error_mark_node;
10131 /* TRUE iff TYPE is publicly & uniquely derived from PARENT. */
10133 bool
10134 publicly_uniquely_derived_p (tree parent, tree type)
10136 tree base = lookup_base (type, parent, ba_ignore_scope | ba_check,
10137 NULL, tf_none);
10138 return base && base != error_mark_node;
10141 /* CTX1 and CTX2 are declaration contexts. Return the innermost common
10142 class between them, if any. */
10144 tree
10145 common_enclosing_class (tree ctx1, tree ctx2)
10147 if (!TYPE_P (ctx1) || !TYPE_P (ctx2))
10148 return NULL_TREE;
10149 gcc_assert (ctx1 == TYPE_MAIN_VARIANT (ctx1)
10150 && ctx2 == TYPE_MAIN_VARIANT (ctx2));
10151 if (ctx1 == ctx2)
10152 return ctx1;
10153 for (tree t = ctx1; TYPE_P (t); t = TYPE_CONTEXT (t))
10154 TYPE_MARKED_P (t) = true;
10155 tree found = NULL_TREE;
10156 for (tree t = ctx2; TYPE_P (t); t = TYPE_CONTEXT (t))
10157 if (TYPE_MARKED_P (t))
10159 found = t;
10160 break;
10162 for (tree t = ctx1; TYPE_P (t); t = TYPE_CONTEXT (t))
10163 TYPE_MARKED_P (t) = false;
10164 return found;
10167 #include "gt-cp-class.h"