PR c++/70512
[official-gcc.git] / gcc / cp / class.c
blobade2001165032733ac11f85699bf5379cfe6adb7
1 /* Functions related to building classes and their related objects.
2 Copyright (C) 1987-2016 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 void check_field_decl (tree, tree, int *, 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. */
151 struct flexmems_t;
152 static void find_flexarrays (tree, flexmems_t *);
153 static void diagnose_flexarrays (tree, const flexmems_t *);
154 static void check_flexarrays (tree, flexmems_t * = NULL);
155 static void check_bases (tree, int *, int *);
156 static void check_bases_and_members (tree);
157 static tree create_vtable_ptr (tree, tree *);
158 static void include_empty_classes (record_layout_info);
159 static void layout_class_type (tree, tree *);
160 static void propagate_binfo_offsets (tree, tree);
161 static void layout_virtual_bases (record_layout_info, splay_tree);
162 static void build_vbase_offset_vtbl_entries (tree, vtbl_init_data *);
163 static void add_vcall_offset_vtbl_entries_r (tree, vtbl_init_data *);
164 static void add_vcall_offset_vtbl_entries_1 (tree, vtbl_init_data *);
165 static void build_vcall_offset_vtbl_entries (tree, vtbl_init_data *);
166 static void add_vcall_offset (tree, tree, vtbl_init_data *);
167 static void layout_vtable_decl (tree, int);
168 static tree dfs_find_final_overrider_pre (tree, void *);
169 static tree dfs_find_final_overrider_post (tree, void *);
170 static tree find_final_overrider (tree, tree, tree);
171 static int make_new_vtable (tree, tree);
172 static tree get_primary_binfo (tree);
173 static int maybe_indent_hierarchy (FILE *, int, int);
174 static tree dump_class_hierarchy_r (FILE *, int, tree, tree, int);
175 static void dump_class_hierarchy (tree);
176 static void dump_class_hierarchy_1 (FILE *, int, tree);
177 static void dump_array (FILE *, tree);
178 static void dump_vtable (tree, tree, tree);
179 static void dump_vtt (tree, tree);
180 static void dump_thunk (FILE *, int, tree);
181 static tree build_vtable (tree, tree, tree);
182 static void initialize_vtable (tree, vec<constructor_elt, va_gc> *);
183 static void layout_nonempty_base_or_field (record_layout_info,
184 tree, tree, splay_tree);
185 static tree end_of_class (tree, int);
186 static bool layout_empty_base (record_layout_info, tree, tree, splay_tree);
187 static void accumulate_vtbl_inits (tree, tree, tree, tree, tree,
188 vec<constructor_elt, va_gc> **);
189 static void dfs_accumulate_vtbl_inits (tree, tree, tree, tree, tree,
190 vec<constructor_elt, va_gc> **);
191 static void build_rtti_vtbl_entries (tree, vtbl_init_data *);
192 static void build_vcall_and_vbase_vtbl_entries (tree, vtbl_init_data *);
193 static void clone_constructors_and_destructors (tree);
194 static tree build_clone (tree, tree);
195 static void update_vtable_entry_for_fn (tree, tree, tree, tree *, unsigned);
196 static void build_ctor_vtbl_group (tree, tree);
197 static void build_vtt (tree);
198 static tree binfo_ctor_vtable (tree);
199 static void build_vtt_inits (tree, tree, vec<constructor_elt, va_gc> **,
200 tree *);
201 static tree dfs_build_secondary_vptr_vtt_inits (tree, void *);
202 static tree dfs_fixup_binfo_vtbls (tree, void *);
203 static int record_subobject_offset (tree, tree, splay_tree);
204 static int check_subobject_offset (tree, tree, splay_tree);
205 static int walk_subobject_offsets (tree, subobject_offset_fn,
206 tree, splay_tree, tree, int);
207 static void record_subobject_offsets (tree, tree, splay_tree, bool);
208 static int layout_conflict_p (tree, tree, splay_tree, int);
209 static int splay_tree_compare_integer_csts (splay_tree_key k1,
210 splay_tree_key k2);
211 static void warn_about_ambiguous_bases (tree);
212 static bool type_requires_array_cookie (tree);
213 static bool base_derived_from (tree, tree);
214 static int empty_base_at_nonzero_offset_p (tree, tree, splay_tree);
215 static tree end_of_base (tree);
216 static tree get_vcall_index (tree, tree);
218 /* Variables shared between class.c and call.c. */
220 int n_vtables = 0;
221 int n_vtable_entries = 0;
222 int n_vtable_searches = 0;
223 int n_vtable_elems = 0;
224 int n_convert_harshness = 0;
225 int n_compute_conversion_costs = 0;
226 int n_inner_fields_searched = 0;
228 /* Return a COND_EXPR that executes TRUE_STMT if this execution of the
229 'structor is in charge of 'structing virtual bases, or FALSE_STMT
230 otherwise. */
232 tree
233 build_if_in_charge (tree true_stmt, tree false_stmt)
235 gcc_assert (DECL_HAS_IN_CHARGE_PARM_P (current_function_decl));
236 tree cmp = build2 (NE_EXPR, boolean_type_node,
237 current_in_charge_parm, integer_zero_node);
238 tree type = unlowered_expr_type (true_stmt);
239 if (VOID_TYPE_P (type))
240 type = unlowered_expr_type (false_stmt);
241 tree cond = build3 (COND_EXPR, type,
242 cmp, true_stmt, false_stmt);
243 return cond;
246 /* Convert to or from a base subobject. EXPR is an expression of type
247 `A' or `A*', an expression of type `B' or `B*' is returned. To
248 convert A to a base B, CODE is PLUS_EXPR and BINFO is the binfo for
249 the B base instance within A. To convert base A to derived B, CODE
250 is MINUS_EXPR and BINFO is the binfo for the A instance within B.
251 In this latter case, A must not be a morally virtual base of B.
252 NONNULL is true if EXPR is known to be non-NULL (this is only
253 needed when EXPR is of pointer type). CV qualifiers are preserved
254 from EXPR. */
256 tree
257 build_base_path (enum tree_code code,
258 tree expr,
259 tree binfo,
260 int nonnull,
261 tsubst_flags_t complain)
263 tree v_binfo = NULL_TREE;
264 tree d_binfo = NULL_TREE;
265 tree probe;
266 tree offset;
267 tree target_type;
268 tree null_test = NULL;
269 tree ptr_target_type;
270 int fixed_type_p;
271 int want_pointer = TYPE_PTR_P (TREE_TYPE (expr));
272 bool has_empty = false;
273 bool virtual_access;
274 bool rvalue = false;
276 if (expr == error_mark_node || binfo == error_mark_node || !binfo)
277 return error_mark_node;
279 for (probe = binfo; probe; probe = BINFO_INHERITANCE_CHAIN (probe))
281 d_binfo = probe;
282 if (is_empty_class (BINFO_TYPE (probe)))
283 has_empty = true;
284 if (!v_binfo && BINFO_VIRTUAL_P (probe))
285 v_binfo = probe;
288 probe = TYPE_MAIN_VARIANT (TREE_TYPE (expr));
289 if (want_pointer)
290 probe = TYPE_MAIN_VARIANT (TREE_TYPE (probe));
292 if (code == PLUS_EXPR
293 && !SAME_BINFO_TYPE_P (BINFO_TYPE (d_binfo), probe))
295 /* This can happen when adjust_result_of_qualified_name_lookup can't
296 find a unique base binfo in a call to a member function. We
297 couldn't give the diagnostic then since we might have been calling
298 a static member function, so we do it now. */
299 if (complain & tf_error)
301 tree base = lookup_base (probe, BINFO_TYPE (d_binfo),
302 ba_unique, NULL, complain);
303 gcc_assert (base == error_mark_node);
305 return error_mark_node;
308 gcc_assert ((code == MINUS_EXPR
309 && SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), probe))
310 || code == PLUS_EXPR);
312 if (binfo == d_binfo)
313 /* Nothing to do. */
314 return expr;
316 if (code == MINUS_EXPR && v_binfo)
318 if (complain & tf_error)
320 if (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), BINFO_TYPE (v_binfo)))
322 if (want_pointer)
323 error ("cannot convert from pointer to base class %qT to "
324 "pointer to derived class %qT because the base is "
325 "virtual", BINFO_TYPE (binfo), BINFO_TYPE (d_binfo));
326 else
327 error ("cannot convert from base class %qT to derived "
328 "class %qT because the base is virtual",
329 BINFO_TYPE (binfo), BINFO_TYPE (d_binfo));
331 else
333 if (want_pointer)
334 error ("cannot convert from pointer to base class %qT to "
335 "pointer to derived class %qT via virtual base %qT",
336 BINFO_TYPE (binfo), BINFO_TYPE (d_binfo),
337 BINFO_TYPE (v_binfo));
338 else
339 error ("cannot convert from base class %qT to derived "
340 "class %qT via virtual base %qT", BINFO_TYPE (binfo),
341 BINFO_TYPE (d_binfo), BINFO_TYPE (v_binfo));
344 return error_mark_node;
347 if (!want_pointer)
349 rvalue = !real_lvalue_p (expr);
350 /* This must happen before the call to save_expr. */
351 expr = cp_build_addr_expr (expr, complain);
353 else
354 expr = mark_rvalue_use (expr);
356 offset = BINFO_OFFSET (binfo);
357 fixed_type_p = resolves_to_fixed_type_p (expr, &nonnull);
358 target_type = code == PLUS_EXPR ? BINFO_TYPE (binfo) : BINFO_TYPE (d_binfo);
359 /* TARGET_TYPE has been extracted from BINFO, and, is therefore always
360 cv-unqualified. Extract the cv-qualifiers from EXPR so that the
361 expression returned matches the input. */
362 target_type = cp_build_qualified_type
363 (target_type, cp_type_quals (TREE_TYPE (TREE_TYPE (expr))));
364 ptr_target_type = build_pointer_type (target_type);
366 /* Do we need to look in the vtable for the real offset? */
367 virtual_access = (v_binfo && fixed_type_p <= 0);
369 /* Don't bother with the calculations inside sizeof; they'll ICE if the
370 source type is incomplete and the pointer value doesn't matter. In a
371 template (even in instantiate_non_dependent_expr), we don't have vtables
372 set up properly yet, and the value doesn't matter there either; we're
373 just interested in the result of overload resolution. */
374 if (cp_unevaluated_operand != 0
375 || in_template_function ())
377 expr = build_nop (ptr_target_type, expr);
378 goto indout;
381 /* If we're in an NSDMI, we don't have the full constructor context yet
382 that we need for converting to a virtual base, so just build a stub
383 CONVERT_EXPR and expand it later in bot_replace. */
384 if (virtual_access && fixed_type_p < 0
385 && current_scope () != current_function_decl)
387 expr = build1 (CONVERT_EXPR, ptr_target_type, expr);
388 CONVERT_EXPR_VBASE_PATH (expr) = true;
389 goto indout;
392 /* Do we need to check for a null pointer? */
393 if (want_pointer && !nonnull)
395 /* If we know the conversion will not actually change the value
396 of EXPR, then we can avoid testing the expression for NULL.
397 We have to avoid generating a COMPONENT_REF for a base class
398 field, because other parts of the compiler know that such
399 expressions are always non-NULL. */
400 if (!virtual_access && integer_zerop (offset))
401 return build_nop (ptr_target_type, expr);
402 null_test = error_mark_node;
405 /* Protect against multiple evaluation if necessary. */
406 if (TREE_SIDE_EFFECTS (expr) && (null_test || virtual_access))
407 expr = save_expr (expr);
409 /* Now that we've saved expr, build the real null test. */
410 if (null_test)
412 tree zero = cp_convert (TREE_TYPE (expr), nullptr_node, complain);
413 null_test = build2_loc (input_location, NE_EXPR, boolean_type_node,
414 expr, zero);
415 /* This is a compiler generated comparison, don't emit
416 e.g. -Wnonnull-compare warning for it. */
417 TREE_NO_WARNING (null_test) = 1;
420 /* If this is a simple base reference, express it as a COMPONENT_REF. */
421 if (code == PLUS_EXPR && !virtual_access
422 /* We don't build base fields for empty bases, and they aren't very
423 interesting to the optimizers anyway. */
424 && !has_empty)
426 expr = cp_build_indirect_ref (expr, RO_NULL, complain);
427 expr = build_simple_base_path (expr, binfo);
428 if (rvalue)
429 expr = move (expr);
430 if (want_pointer)
431 expr = build_address (expr);
432 target_type = TREE_TYPE (expr);
433 goto out;
436 if (virtual_access)
438 /* Going via virtual base V_BINFO. We need the static offset
439 from V_BINFO to BINFO, and the dynamic offset from D_BINFO to
440 V_BINFO. That offset is an entry in D_BINFO's vtable. */
441 tree v_offset;
443 if (fixed_type_p < 0 && in_base_initializer)
445 /* In a base member initializer, we cannot rely on the
446 vtable being set up. We have to indirect via the
447 vtt_parm. */
448 tree t;
450 t = TREE_TYPE (TYPE_VFIELD (current_class_type));
451 t = build_pointer_type (t);
452 v_offset = fold_convert (t, current_vtt_parm);
453 v_offset = cp_build_indirect_ref (v_offset, RO_NULL, complain);
455 else
457 tree t = expr;
458 if ((flag_sanitize & SANITIZE_VPTR) && fixed_type_p == 0)
460 t = cp_ubsan_maybe_instrument_cast_to_vbase (input_location,
461 probe, expr);
462 if (t == NULL_TREE)
463 t = expr;
465 v_offset = build_vfield_ref (cp_build_indirect_ref (t, RO_NULL,
466 complain),
467 TREE_TYPE (TREE_TYPE (expr)));
470 if (v_offset == error_mark_node)
471 return error_mark_node;
473 v_offset = fold_build_pointer_plus (v_offset, BINFO_VPTR_FIELD (v_binfo));
474 v_offset = build1 (NOP_EXPR,
475 build_pointer_type (ptrdiff_type_node),
476 v_offset);
477 v_offset = cp_build_indirect_ref (v_offset, RO_NULL, complain);
478 TREE_CONSTANT (v_offset) = 1;
480 offset = convert_to_integer (ptrdiff_type_node,
481 size_diffop_loc (input_location, offset,
482 BINFO_OFFSET (v_binfo)));
484 if (!integer_zerop (offset))
485 v_offset = build2 (code, ptrdiff_type_node, v_offset, offset);
487 if (fixed_type_p < 0)
488 /* Negative fixed_type_p means this is a constructor or destructor;
489 virtual base layout is fixed in in-charge [cd]tors, but not in
490 base [cd]tors. */
491 offset = build_if_in_charge
492 (convert_to_integer (ptrdiff_type_node, BINFO_OFFSET (binfo)),
493 v_offset);
494 else
495 offset = v_offset;
498 if (want_pointer)
499 target_type = ptr_target_type;
501 expr = build1 (NOP_EXPR, ptr_target_type, expr);
503 if (!integer_zerop (offset))
505 offset = fold_convert (sizetype, offset);
506 if (code == MINUS_EXPR)
507 offset = fold_build1_loc (input_location, NEGATE_EXPR, sizetype, offset);
508 expr = fold_build_pointer_plus (expr, offset);
510 else
511 null_test = NULL;
513 indout:
514 if (!want_pointer)
516 expr = cp_build_indirect_ref (expr, RO_NULL, complain);
517 if (rvalue)
518 expr = move (expr);
521 out:
522 if (null_test)
523 expr = fold_build3_loc (input_location, COND_EXPR, target_type, null_test, expr,
524 build_zero_cst (target_type));
526 return expr;
529 /* Subroutine of build_base_path; EXPR and BINFO are as in that function.
530 Perform a derived-to-base conversion by recursively building up a
531 sequence of COMPONENT_REFs to the appropriate base fields. */
533 static tree
534 build_simple_base_path (tree expr, tree binfo)
536 tree type = BINFO_TYPE (binfo);
537 tree d_binfo = BINFO_INHERITANCE_CHAIN (binfo);
538 tree field;
540 if (d_binfo == NULL_TREE)
542 tree temp;
544 gcc_assert (TYPE_MAIN_VARIANT (TREE_TYPE (expr)) == type);
546 /* Transform `(a, b).x' into `(*(a, &b)).x', `(a ? b : c).x'
547 into `(*(a ? &b : &c)).x', and so on. A COND_EXPR is only
548 an lvalue in the front end; only _DECLs and _REFs are lvalues
549 in the back end. */
550 temp = unary_complex_lvalue (ADDR_EXPR, expr);
551 if (temp)
552 expr = cp_build_indirect_ref (temp, RO_NULL, tf_warning_or_error);
554 return expr;
557 /* Recurse. */
558 expr = build_simple_base_path (expr, d_binfo);
560 for (field = TYPE_FIELDS (BINFO_TYPE (d_binfo));
561 field; field = DECL_CHAIN (field))
562 /* Is this the base field created by build_base_field? */
563 if (TREE_CODE (field) == FIELD_DECL
564 && DECL_FIELD_IS_BASE (field)
565 && TREE_TYPE (field) == type
566 /* If we're looking for a field in the most-derived class,
567 also check the field offset; we can have two base fields
568 of the same type if one is an indirect virtual base and one
569 is a direct non-virtual base. */
570 && (BINFO_INHERITANCE_CHAIN (d_binfo)
571 || tree_int_cst_equal (byte_position (field),
572 BINFO_OFFSET (binfo))))
574 /* We don't use build_class_member_access_expr here, as that
575 has unnecessary checks, and more importantly results in
576 recursive calls to dfs_walk_once. */
577 int type_quals = cp_type_quals (TREE_TYPE (expr));
579 expr = build3 (COMPONENT_REF,
580 cp_build_qualified_type (type, type_quals),
581 expr, field, NULL_TREE);
582 /* Mark the expression const or volatile, as appropriate.
583 Even though we've dealt with the type above, we still have
584 to mark the expression itself. */
585 if (type_quals & TYPE_QUAL_CONST)
586 TREE_READONLY (expr) = 1;
587 if (type_quals & TYPE_QUAL_VOLATILE)
588 TREE_THIS_VOLATILE (expr) = 1;
590 return expr;
593 /* Didn't find the base field?!? */
594 gcc_unreachable ();
597 /* Convert OBJECT to the base TYPE. OBJECT is an expression whose
598 type is a class type or a pointer to a class type. In the former
599 case, TYPE is also a class type; in the latter it is another
600 pointer type. If CHECK_ACCESS is true, an error message is emitted
601 if TYPE is inaccessible. If OBJECT has pointer type, the value is
602 assumed to be non-NULL. */
604 tree
605 convert_to_base (tree object, tree type, bool check_access, bool nonnull,
606 tsubst_flags_t complain)
608 tree binfo;
609 tree object_type;
611 if (TYPE_PTR_P (TREE_TYPE (object)))
613 object_type = TREE_TYPE (TREE_TYPE (object));
614 type = TREE_TYPE (type);
616 else
617 object_type = TREE_TYPE (object);
619 binfo = lookup_base (object_type, type, check_access ? ba_check : ba_unique,
620 NULL, complain);
621 if (!binfo || binfo == error_mark_node)
622 return error_mark_node;
624 return build_base_path (PLUS_EXPR, object, binfo, nonnull, complain);
627 /* EXPR is an expression with unqualified class type. BASE is a base
628 binfo of that class type. Returns EXPR, converted to the BASE
629 type. This function assumes that EXPR is the most derived class;
630 therefore virtual bases can be found at their static offsets. */
632 tree
633 convert_to_base_statically (tree expr, tree base)
635 tree expr_type;
637 expr_type = TREE_TYPE (expr);
638 if (!SAME_BINFO_TYPE_P (BINFO_TYPE (base), expr_type))
640 /* If this is a non-empty base, use a COMPONENT_REF. */
641 if (!is_empty_class (BINFO_TYPE (base)))
642 return build_simple_base_path (expr, base);
644 /* We use fold_build2 and fold_convert below to simplify the trees
645 provided to the optimizers. It is not safe to call these functions
646 when processing a template because they do not handle C++-specific
647 trees. */
648 gcc_assert (!processing_template_decl);
649 expr = cp_build_addr_expr (expr, tf_warning_or_error);
650 if (!integer_zerop (BINFO_OFFSET (base)))
651 expr = fold_build_pointer_plus_loc (input_location,
652 expr, BINFO_OFFSET (base));
653 expr = fold_convert (build_pointer_type (BINFO_TYPE (base)), expr);
654 expr = build_fold_indirect_ref_loc (input_location, expr);
657 return expr;
661 tree
662 build_vfield_ref (tree datum, tree type)
664 tree vfield, vcontext;
666 if (datum == error_mark_node
667 /* Can happen in case of duplicate base types (c++/59082). */
668 || !TYPE_VFIELD (type))
669 return error_mark_node;
671 /* First, convert to the requested type. */
672 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (datum), type))
673 datum = convert_to_base (datum, type, /*check_access=*/false,
674 /*nonnull=*/true, tf_warning_or_error);
676 /* Second, the requested type may not be the owner of its own vptr.
677 If not, convert to the base class that owns it. We cannot use
678 convert_to_base here, because VCONTEXT may appear more than once
679 in the inheritance hierarchy of TYPE, and thus direct conversion
680 between the types may be ambiguous. Following the path back up
681 one step at a time via primary bases avoids the problem. */
682 vfield = TYPE_VFIELD (type);
683 vcontext = DECL_CONTEXT (vfield);
684 while (!same_type_ignoring_top_level_qualifiers_p (vcontext, type))
686 datum = build_simple_base_path (datum, CLASSTYPE_PRIMARY_BINFO (type));
687 type = TREE_TYPE (datum);
690 return build3 (COMPONENT_REF, TREE_TYPE (vfield), datum, vfield, NULL_TREE);
693 /* Given an object INSTANCE, return an expression which yields the
694 vtable element corresponding to INDEX. There are many special
695 cases for INSTANCE which we take care of here, mainly to avoid
696 creating extra tree nodes when we don't have to. */
698 static tree
699 build_vtbl_ref_1 (tree instance, tree idx)
701 tree aref;
702 tree vtbl = NULL_TREE;
704 /* Try to figure out what a reference refers to, and
705 access its virtual function table directly. */
707 int cdtorp = 0;
708 tree fixed_type = fixed_type_or_null (instance, NULL, &cdtorp);
710 tree basetype = non_reference (TREE_TYPE (instance));
712 if (fixed_type && !cdtorp)
714 tree binfo = lookup_base (fixed_type, basetype,
715 ba_unique, NULL, tf_none);
716 if (binfo && binfo != error_mark_node)
717 vtbl = unshare_expr (BINFO_VTABLE (binfo));
720 if (!vtbl)
721 vtbl = build_vfield_ref (instance, basetype);
723 aref = build_array_ref (input_location, vtbl, idx);
724 TREE_CONSTANT (aref) |= TREE_CONSTANT (vtbl) && TREE_CONSTANT (idx);
726 return aref;
729 tree
730 build_vtbl_ref (tree instance, tree idx)
732 tree aref = build_vtbl_ref_1 (instance, idx);
734 return aref;
737 /* Given a stable object pointer INSTANCE_PTR, return an expression which
738 yields a function pointer corresponding to vtable element INDEX. */
740 tree
741 build_vfn_ref (tree instance_ptr, tree idx)
743 tree aref;
745 aref = build_vtbl_ref_1 (cp_build_indirect_ref (instance_ptr, RO_NULL,
746 tf_warning_or_error),
747 idx);
749 /* When using function descriptors, the address of the
750 vtable entry is treated as a function pointer. */
751 if (TARGET_VTABLE_USES_DESCRIPTORS)
752 aref = build1 (NOP_EXPR, TREE_TYPE (aref),
753 cp_build_addr_expr (aref, tf_warning_or_error));
755 /* Remember this as a method reference, for later devirtualization. */
756 aref = build3 (OBJ_TYPE_REF, TREE_TYPE (aref), aref, instance_ptr, idx);
758 return aref;
761 /* Return the name of the virtual function table (as an IDENTIFIER_NODE)
762 for the given TYPE. */
764 static tree
765 get_vtable_name (tree type)
767 return mangle_vtbl_for_type (type);
770 /* DECL is an entity associated with TYPE, like a virtual table or an
771 implicitly generated constructor. Determine whether or not DECL
772 should have external or internal linkage at the object file
773 level. This routine does not deal with COMDAT linkage and other
774 similar complexities; it simply sets TREE_PUBLIC if it possible for
775 entities in other translation units to contain copies of DECL, in
776 the abstract. */
778 void
779 set_linkage_according_to_type (tree /*type*/, tree decl)
781 TREE_PUBLIC (decl) = 1;
782 determine_visibility (decl);
785 /* Create a VAR_DECL for a primary or secondary vtable for CLASS_TYPE.
786 (For a secondary vtable for B-in-D, CLASS_TYPE should be D, not B.)
787 Use NAME for the name of the vtable, and VTABLE_TYPE for its type. */
789 static tree
790 build_vtable (tree class_type, tree name, tree vtable_type)
792 tree decl;
794 decl = build_lang_decl (VAR_DECL, name, vtable_type);
795 /* vtable names are already mangled; give them their DECL_ASSEMBLER_NAME
796 now to avoid confusion in mangle_decl. */
797 SET_DECL_ASSEMBLER_NAME (decl, name);
798 DECL_CONTEXT (decl) = class_type;
799 DECL_ARTIFICIAL (decl) = 1;
800 TREE_STATIC (decl) = 1;
801 TREE_READONLY (decl) = 1;
802 DECL_VIRTUAL_P (decl) = 1;
803 DECL_ALIGN (decl) = TARGET_VTABLE_ENTRY_ALIGN;
804 DECL_USER_ALIGN (decl) = true;
805 DECL_VTABLE_OR_VTT_P (decl) = 1;
806 set_linkage_according_to_type (class_type, decl);
807 /* The vtable has not been defined -- yet. */
808 DECL_EXTERNAL (decl) = 1;
809 DECL_NOT_REALLY_EXTERN (decl) = 1;
811 /* Mark the VAR_DECL node representing the vtable itself as a
812 "gratuitous" one, thereby forcing dwarfout.c to ignore it. It
813 is rather important that such things be ignored because any
814 effort to actually generate DWARF for them will run into
815 trouble when/if we encounter code like:
817 #pragma interface
818 struct S { virtual void member (); };
820 because the artificial declaration of the vtable itself (as
821 manufactured by the g++ front end) will say that the vtable is
822 a static member of `S' but only *after* the debug output for
823 the definition of `S' has already been output. This causes
824 grief because the DWARF entry for the definition of the vtable
825 will try to refer back to an earlier *declaration* of the
826 vtable as a static member of `S' and there won't be one. We
827 might be able to arrange to have the "vtable static member"
828 attached to the member list for `S' before the debug info for
829 `S' get written (which would solve the problem) but that would
830 require more intrusive changes to the g++ front end. */
831 DECL_IGNORED_P (decl) = 1;
833 return decl;
836 /* Get the VAR_DECL of the vtable for TYPE. TYPE need not be polymorphic,
837 or even complete. If this does not exist, create it. If COMPLETE is
838 nonzero, then complete the definition of it -- that will render it
839 impossible to actually build the vtable, but is useful to get at those
840 which are known to exist in the runtime. */
842 tree
843 get_vtable_decl (tree type, int complete)
845 tree decl;
847 if (CLASSTYPE_VTABLES (type))
848 return CLASSTYPE_VTABLES (type);
850 decl = build_vtable (type, get_vtable_name (type), vtbl_type_node);
851 CLASSTYPE_VTABLES (type) = decl;
853 if (complete)
855 DECL_EXTERNAL (decl) = 1;
856 cp_finish_decl (decl, NULL_TREE, false, NULL_TREE, 0);
859 return decl;
862 /* Build the primary virtual function table for TYPE. If BINFO is
863 non-NULL, build the vtable starting with the initial approximation
864 that it is the same as the one which is the head of the association
865 list. Returns a nonzero value if a new vtable is actually
866 created. */
868 static int
869 build_primary_vtable (tree binfo, tree type)
871 tree decl;
872 tree virtuals;
874 decl = get_vtable_decl (type, /*complete=*/0);
876 if (binfo)
878 if (BINFO_NEW_VTABLE_MARKED (binfo))
879 /* We have already created a vtable for this base, so there's
880 no need to do it again. */
881 return 0;
883 virtuals = copy_list (BINFO_VIRTUALS (binfo));
884 TREE_TYPE (decl) = TREE_TYPE (get_vtbl_decl_for_binfo (binfo));
885 DECL_SIZE (decl) = TYPE_SIZE (TREE_TYPE (decl));
886 DECL_SIZE_UNIT (decl) = TYPE_SIZE_UNIT (TREE_TYPE (decl));
888 else
890 gcc_assert (TREE_TYPE (decl) == vtbl_type_node);
891 virtuals = NULL_TREE;
894 if (GATHER_STATISTICS)
896 n_vtables += 1;
897 n_vtable_elems += list_length (virtuals);
900 /* Initialize the association list for this type, based
901 on our first approximation. */
902 BINFO_VTABLE (TYPE_BINFO (type)) = decl;
903 BINFO_VIRTUALS (TYPE_BINFO (type)) = virtuals;
904 SET_BINFO_NEW_VTABLE_MARKED (TYPE_BINFO (type));
905 return 1;
908 /* Give BINFO a new virtual function table which is initialized
909 with a skeleton-copy of its original initialization. The only
910 entry that changes is the `delta' entry, so we can really
911 share a lot of structure.
913 FOR_TYPE is the most derived type which caused this table to
914 be needed.
916 Returns nonzero if we haven't met BINFO before.
918 The order in which vtables are built (by calling this function) for
919 an object must remain the same, otherwise a binary incompatibility
920 can result. */
922 static int
923 build_secondary_vtable (tree binfo)
925 if (BINFO_NEW_VTABLE_MARKED (binfo))
926 /* We already created a vtable for this base. There's no need to
927 do it again. */
928 return 0;
930 /* Remember that we've created a vtable for this BINFO, so that we
931 don't try to do so again. */
932 SET_BINFO_NEW_VTABLE_MARKED (binfo);
934 /* Make fresh virtual list, so we can smash it later. */
935 BINFO_VIRTUALS (binfo) = copy_list (BINFO_VIRTUALS (binfo));
937 /* Secondary vtables are laid out as part of the same structure as
938 the primary vtable. */
939 BINFO_VTABLE (binfo) = NULL_TREE;
940 return 1;
943 /* Create a new vtable for BINFO which is the hierarchy dominated by
944 T. Return nonzero if we actually created a new vtable. */
946 static int
947 make_new_vtable (tree t, tree binfo)
949 if (binfo == TYPE_BINFO (t))
950 /* In this case, it is *type*'s vtable we are modifying. We start
951 with the approximation that its vtable is that of the
952 immediate base class. */
953 return build_primary_vtable (binfo, t);
954 else
955 /* This is our very own copy of `basetype' to play with. Later,
956 we will fill in all the virtual functions that override the
957 virtual functions in these base classes which are not defined
958 by the current type. */
959 return build_secondary_vtable (binfo);
962 /* Make *VIRTUALS, an entry on the BINFO_VIRTUALS list for BINFO
963 (which is in the hierarchy dominated by T) list FNDECL as its
964 BV_FN. DELTA is the required constant adjustment from the `this'
965 pointer where the vtable entry appears to the `this' required when
966 the function is actually called. */
968 static void
969 modify_vtable_entry (tree t,
970 tree binfo,
971 tree fndecl,
972 tree delta,
973 tree *virtuals)
975 tree v;
977 v = *virtuals;
979 if (fndecl != BV_FN (v)
980 || !tree_int_cst_equal (delta, BV_DELTA (v)))
982 /* We need a new vtable for BINFO. */
983 if (make_new_vtable (t, binfo))
985 /* If we really did make a new vtable, we also made a copy
986 of the BINFO_VIRTUALS list. Now, we have to find the
987 corresponding entry in that list. */
988 *virtuals = BINFO_VIRTUALS (binfo);
989 while (BV_FN (*virtuals) != BV_FN (v))
990 *virtuals = TREE_CHAIN (*virtuals);
991 v = *virtuals;
994 BV_DELTA (v) = delta;
995 BV_VCALL_INDEX (v) = NULL_TREE;
996 BV_FN (v) = fndecl;
1001 /* Add method METHOD to class TYPE. If USING_DECL is non-null, it is
1002 the USING_DECL naming METHOD. Returns true if the method could be
1003 added to the method vec. */
1005 bool
1006 add_method (tree type, tree method, tree using_decl)
1008 unsigned slot;
1009 tree overload;
1010 bool template_conv_p = false;
1011 bool conv_p;
1012 vec<tree, va_gc> *method_vec;
1013 bool complete_p;
1014 bool insert_p = false;
1015 tree current_fns;
1016 tree fns;
1018 if (method == error_mark_node)
1019 return false;
1021 complete_p = COMPLETE_TYPE_P (type);
1022 conv_p = DECL_CONV_FN_P (method);
1023 if (conv_p)
1024 template_conv_p = (TREE_CODE (method) == TEMPLATE_DECL
1025 && DECL_TEMPLATE_CONV_FN_P (method));
1027 method_vec = CLASSTYPE_METHOD_VEC (type);
1028 if (!method_vec)
1030 /* Make a new method vector. We start with 8 entries. We must
1031 allocate at least two (for constructors and destructors), and
1032 we're going to end up with an assignment operator at some
1033 point as well. */
1034 vec_alloc (method_vec, 8);
1035 /* Create slots for constructors and destructors. */
1036 method_vec->quick_push (NULL_TREE);
1037 method_vec->quick_push (NULL_TREE);
1038 CLASSTYPE_METHOD_VEC (type) = method_vec;
1041 /* Maintain TYPE_HAS_USER_CONSTRUCTOR, etc. */
1042 grok_special_member_properties (method);
1044 /* Constructors and destructors go in special slots. */
1045 if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (method))
1046 slot = CLASSTYPE_CONSTRUCTOR_SLOT;
1047 else if (DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (method))
1049 slot = CLASSTYPE_DESTRUCTOR_SLOT;
1051 if (TYPE_FOR_JAVA (type))
1053 if (!DECL_ARTIFICIAL (method))
1054 error ("Java class %qT cannot have a destructor", type);
1055 else if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
1056 error ("Java class %qT cannot have an implicit non-trivial "
1057 "destructor",
1058 type);
1061 else
1063 tree m;
1065 insert_p = true;
1066 /* See if we already have an entry with this name. */
1067 for (slot = CLASSTYPE_FIRST_CONVERSION_SLOT;
1068 vec_safe_iterate (method_vec, slot, &m);
1069 ++slot)
1071 m = OVL_CURRENT (m);
1072 if (template_conv_p)
1074 if (TREE_CODE (m) == TEMPLATE_DECL
1075 && DECL_TEMPLATE_CONV_FN_P (m))
1076 insert_p = false;
1077 break;
1079 if (conv_p && !DECL_CONV_FN_P (m))
1080 break;
1081 if (DECL_NAME (m) == DECL_NAME (method))
1083 insert_p = false;
1084 break;
1086 if (complete_p
1087 && !DECL_CONV_FN_P (m)
1088 && DECL_NAME (m) > DECL_NAME (method))
1089 break;
1092 current_fns = insert_p ? NULL_TREE : (*method_vec)[slot];
1094 /* Check to see if we've already got this method. */
1095 for (fns = current_fns; fns; fns = OVL_NEXT (fns))
1097 tree fn = OVL_CURRENT (fns);
1098 tree fn_type;
1099 tree method_type;
1100 tree parms1;
1101 tree parms2;
1103 if (TREE_CODE (fn) != TREE_CODE (method))
1104 continue;
1106 /* [over.load] Member function declarations with the
1107 same name and the same parameter types cannot be
1108 overloaded if any of them is a static member
1109 function declaration.
1111 [over.load] Member function declarations with the same name and
1112 the same parameter-type-list as well as member function template
1113 declarations with the same name, the same parameter-type-list, and
1114 the same template parameter lists cannot be overloaded if any of
1115 them, but not all, have a ref-qualifier.
1117 [namespace.udecl] When a using-declaration brings names
1118 from a base class into a derived class scope, member
1119 functions in the derived class override and/or hide member
1120 functions with the same name and parameter types in a base
1121 class (rather than conflicting). */
1122 fn_type = TREE_TYPE (fn);
1123 method_type = TREE_TYPE (method);
1124 parms1 = TYPE_ARG_TYPES (fn_type);
1125 parms2 = TYPE_ARG_TYPES (method_type);
1127 /* Compare the quals on the 'this' parm. Don't compare
1128 the whole types, as used functions are treated as
1129 coming from the using class in overload resolution. */
1130 if (! DECL_STATIC_FUNCTION_P (fn)
1131 && ! DECL_STATIC_FUNCTION_P (method)
1132 /* Either both or neither need to be ref-qualified for
1133 differing quals to allow overloading. */
1134 && (FUNCTION_REF_QUALIFIED (fn_type)
1135 == FUNCTION_REF_QUALIFIED (method_type))
1136 && (type_memfn_quals (fn_type) != type_memfn_quals (method_type)
1137 || type_memfn_rqual (fn_type) != type_memfn_rqual (method_type)))
1138 continue;
1140 /* For templates, the return type and template parameters
1141 must be identical. */
1142 if (TREE_CODE (fn) == TEMPLATE_DECL
1143 && (!same_type_p (TREE_TYPE (fn_type),
1144 TREE_TYPE (method_type))
1145 || !comp_template_parms (DECL_TEMPLATE_PARMS (fn),
1146 DECL_TEMPLATE_PARMS (method))))
1147 continue;
1149 if (! DECL_STATIC_FUNCTION_P (fn))
1150 parms1 = TREE_CHAIN (parms1);
1151 if (! DECL_STATIC_FUNCTION_P (method))
1152 parms2 = TREE_CHAIN (parms2);
1154 if (compparms (parms1, parms2)
1155 && (!DECL_CONV_FN_P (fn)
1156 || same_type_p (TREE_TYPE (fn_type),
1157 TREE_TYPE (method_type)))
1158 && equivalently_constrained (fn, method))
1160 /* For function versions, their parms and types match
1161 but they are not duplicates. Record function versions
1162 as and when they are found. extern "C" functions are
1163 not treated as versions. */
1164 if (TREE_CODE (fn) == FUNCTION_DECL
1165 && TREE_CODE (method) == FUNCTION_DECL
1166 && !DECL_EXTERN_C_P (fn)
1167 && !DECL_EXTERN_C_P (method)
1168 && targetm.target_option.function_versions (fn, method))
1170 /* Mark functions as versions if necessary. Modify the mangled
1171 decl name if necessary. */
1172 if (!DECL_FUNCTION_VERSIONED (fn))
1174 DECL_FUNCTION_VERSIONED (fn) = 1;
1175 if (DECL_ASSEMBLER_NAME_SET_P (fn))
1176 mangle_decl (fn);
1178 if (!DECL_FUNCTION_VERSIONED (method))
1180 DECL_FUNCTION_VERSIONED (method) = 1;
1181 if (DECL_ASSEMBLER_NAME_SET_P (method))
1182 mangle_decl (method);
1184 cgraph_node::record_function_versions (fn, method);
1185 continue;
1187 if (DECL_INHERITED_CTOR_BASE (method))
1189 if (DECL_INHERITED_CTOR_BASE (fn))
1191 error_at (DECL_SOURCE_LOCATION (method),
1192 "%q#D inherited from %qT", method,
1193 DECL_INHERITED_CTOR_BASE (method));
1194 error_at (DECL_SOURCE_LOCATION (fn),
1195 "conflicts with version inherited from %qT",
1196 DECL_INHERITED_CTOR_BASE (fn));
1198 /* Otherwise defer to the other function. */
1199 return false;
1201 if (using_decl)
1203 if (DECL_CONTEXT (fn) == type)
1204 /* Defer to the local function. */
1205 return false;
1207 else
1209 error ("%q+#D cannot be overloaded", method);
1210 error ("with %q+#D", fn);
1213 /* We don't call duplicate_decls here to merge the
1214 declarations because that will confuse things if the
1215 methods have inline definitions. In particular, we
1216 will crash while processing the definitions. */
1217 return false;
1221 /* A class should never have more than one destructor. */
1222 if (current_fns && DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (method))
1223 return false;
1225 /* Add the new binding. */
1226 if (using_decl)
1228 overload = ovl_cons (method, current_fns);
1229 OVL_USED (overload) = true;
1231 else
1232 overload = build_overload (method, current_fns);
1234 if (conv_p)
1235 TYPE_HAS_CONVERSION (type) = 1;
1236 else if (slot >= CLASSTYPE_FIRST_CONVERSION_SLOT && !complete_p)
1237 push_class_level_binding (DECL_NAME (method), overload);
1239 if (insert_p)
1241 bool reallocated;
1243 /* We only expect to add few methods in the COMPLETE_P case, so
1244 just make room for one more method in that case. */
1245 if (complete_p)
1246 reallocated = vec_safe_reserve_exact (method_vec, 1);
1247 else
1248 reallocated = vec_safe_reserve (method_vec, 1);
1249 if (reallocated)
1250 CLASSTYPE_METHOD_VEC (type) = method_vec;
1251 if (slot == method_vec->length ())
1252 method_vec->quick_push (overload);
1253 else
1254 method_vec->quick_insert (slot, overload);
1256 else
1257 /* Replace the current slot. */
1258 (*method_vec)[slot] = overload;
1259 return true;
1262 /* Subroutines of finish_struct. */
1264 /* Change the access of FDECL to ACCESS in T. Return 1 if change was
1265 legit, otherwise return 0. */
1267 static int
1268 alter_access (tree t, tree fdecl, tree access)
1270 tree elem;
1272 if (!DECL_LANG_SPECIFIC (fdecl))
1273 retrofit_lang_decl (fdecl);
1275 gcc_assert (!DECL_DISCRIMINATOR_P (fdecl));
1277 elem = purpose_member (t, DECL_ACCESS (fdecl));
1278 if (elem)
1280 if (TREE_VALUE (elem) != access)
1282 if (TREE_CODE (TREE_TYPE (fdecl)) == FUNCTION_DECL)
1283 error ("conflicting access specifications for method"
1284 " %q+D, ignored", TREE_TYPE (fdecl));
1285 else
1286 error ("conflicting access specifications for field %qE, ignored",
1287 DECL_NAME (fdecl));
1289 else
1291 /* They're changing the access to the same thing they changed
1292 it to before. That's OK. */
1296 else
1298 perform_or_defer_access_check (TYPE_BINFO (t), fdecl, fdecl,
1299 tf_warning_or_error);
1300 DECL_ACCESS (fdecl) = tree_cons (t, access, DECL_ACCESS (fdecl));
1301 return 1;
1303 return 0;
1306 /* Process the USING_DECL, which is a member of T. */
1308 static void
1309 handle_using_decl (tree using_decl, tree t)
1311 tree decl = USING_DECL_DECLS (using_decl);
1312 tree name = DECL_NAME (using_decl);
1313 tree access
1314 = TREE_PRIVATE (using_decl) ? access_private_node
1315 : TREE_PROTECTED (using_decl) ? access_protected_node
1316 : access_public_node;
1317 tree flist = NULL_TREE;
1318 tree old_value;
1320 gcc_assert (!processing_template_decl && decl);
1322 old_value = lookup_member (t, name, /*protect=*/0, /*want_type=*/false,
1323 tf_warning_or_error);
1324 if (old_value)
1326 if (is_overloaded_fn (old_value))
1327 old_value = OVL_CURRENT (old_value);
1329 if (DECL_P (old_value) && DECL_CONTEXT (old_value) == t)
1330 /* OK */;
1331 else
1332 old_value = NULL_TREE;
1335 cp_emit_debug_info_for_using (decl, t);
1337 if (is_overloaded_fn (decl))
1338 flist = decl;
1340 if (! old_value)
1342 else if (is_overloaded_fn (old_value))
1344 if (flist)
1345 /* It's OK to use functions from a base when there are functions with
1346 the same name already present in the current class. */;
1347 else
1349 error ("%q+D invalid in %q#T", using_decl, t);
1350 error (" because of local method %q+#D with same name",
1351 OVL_CURRENT (old_value));
1352 return;
1355 else if (!DECL_ARTIFICIAL (old_value))
1357 error ("%q+D invalid in %q#T", using_decl, t);
1358 error (" because of local member %q+#D with same name", old_value);
1359 return;
1362 /* Make type T see field decl FDECL with access ACCESS. */
1363 if (flist)
1364 for (; flist; flist = OVL_NEXT (flist))
1366 add_method (t, OVL_CURRENT (flist), using_decl);
1367 alter_access (t, OVL_CURRENT (flist), access);
1369 else
1370 alter_access (t, decl, access);
1373 /* Data structure for find_abi_tags_r, below. */
1375 struct abi_tag_data
1377 tree t; // The type that we're checking for missing tags.
1378 tree subob; // The subobject of T that we're getting tags from.
1379 tree tags; // error_mark_node for diagnostics, or a list of missing tags.
1382 /* Subroutine of find_abi_tags_r. Handle a single TAG found on the class TP
1383 in the context of P. TAG can be either an identifier (the DECL_NAME of
1384 a tag NAMESPACE_DECL) or a STRING_CST (a tag attribute). */
1386 static void
1387 check_tag (tree tag, tree id, tree *tp, abi_tag_data *p)
1389 if (!IDENTIFIER_MARKED (id))
1391 if (p->tags != error_mark_node)
1393 /* We're collecting tags from template arguments or from
1394 the type of a variable or function return type. */
1395 p->tags = tree_cons (NULL_TREE, tag, p->tags);
1397 /* Don't inherit this tag multiple times. */
1398 IDENTIFIER_MARKED (id) = true;
1400 if (TYPE_P (p->t))
1402 /* Tags inherited from type template arguments are only used
1403 to avoid warnings. */
1404 ABI_TAG_IMPLICIT (p->tags) = true;
1405 return;
1407 /* For functions and variables we want to warn, too. */
1410 /* Otherwise we're diagnosing missing tags. */
1411 if (TREE_CODE (p->t) == FUNCTION_DECL)
1413 if (warning (OPT_Wabi_tag, "%qD inherits the %E ABI tag "
1414 "that %qT (used in its return type) has",
1415 p->t, tag, *tp))
1416 inform (location_of (*tp), "%qT declared here", *tp);
1418 else if (VAR_P (p->t))
1420 if (warning (OPT_Wabi_tag, "%qD inherits the %E ABI tag "
1421 "that %qT (used in its type) has", p->t, tag, *tp))
1422 inform (location_of (*tp), "%qT declared here", *tp);
1424 else if (TYPE_P (p->subob))
1426 if (warning (OPT_Wabi_tag, "%qT does not have the %E ABI tag "
1427 "that base %qT has", p->t, tag, p->subob))
1428 inform (location_of (p->subob), "%qT declared here",
1429 p->subob);
1431 else
1433 if (warning (OPT_Wabi_tag, "%qT does not have the %E ABI tag "
1434 "that %qT (used in the type of %qD) has",
1435 p->t, tag, *tp, p->subob))
1437 inform (location_of (p->subob), "%qD declared here",
1438 p->subob);
1439 inform (location_of (*tp), "%qT declared here", *tp);
1445 /* Find all the ABI tags in the attribute list ATTR and either call
1446 check_tag (if TP is non-null) or set IDENTIFIER_MARKED to val. */
1448 static void
1449 mark_or_check_attr_tags (tree attr, tree *tp, abi_tag_data *p, bool val)
1451 if (!attr)
1452 return;
1453 for (; (attr = lookup_attribute ("abi_tag", attr));
1454 attr = TREE_CHAIN (attr))
1455 for (tree list = TREE_VALUE (attr); list;
1456 list = TREE_CHAIN (list))
1458 tree tag = TREE_VALUE (list);
1459 tree id = get_identifier (TREE_STRING_POINTER (tag));
1460 if (tp)
1461 check_tag (tag, id, tp, p);
1462 else
1463 IDENTIFIER_MARKED (id) = val;
1467 /* Find all the ABI tags on T and its enclosing scopes and either call
1468 check_tag (if TP is non-null) or set IDENTIFIER_MARKED to val. */
1470 static void
1471 mark_or_check_tags (tree t, tree *tp, abi_tag_data *p, bool val)
1473 while (t != global_namespace)
1475 tree attr;
1476 if (TYPE_P (t))
1478 attr = TYPE_ATTRIBUTES (t);
1479 t = CP_TYPE_CONTEXT (t);
1481 else
1483 attr = DECL_ATTRIBUTES (t);
1484 t = CP_DECL_CONTEXT (t);
1486 mark_or_check_attr_tags (attr, tp, p, val);
1490 /* walk_tree callback for check_abi_tags: if the type at *TP involves any
1491 types with ABI tags, add the corresponding identifiers to the VEC in
1492 *DATA and set IDENTIFIER_MARKED. */
1494 static tree
1495 find_abi_tags_r (tree *tp, int *walk_subtrees, void *data)
1497 if (!OVERLOAD_TYPE_P (*tp))
1498 return NULL_TREE;
1500 /* walk_tree shouldn't be walking into any subtrees of a RECORD_TYPE
1501 anyway, but let's make sure of it. */
1502 *walk_subtrees = false;
1504 abi_tag_data *p = static_cast<struct abi_tag_data*>(data);
1506 mark_or_check_tags (*tp, tp, p, false);
1508 return NULL_TREE;
1511 /* walk_tree callback for mark_abi_tags: if *TP is a class, set
1512 IDENTIFIER_MARKED on its ABI tags. */
1514 static tree
1515 mark_abi_tags_r (tree *tp, int *walk_subtrees, void *data)
1517 if (!OVERLOAD_TYPE_P (*tp))
1518 return NULL_TREE;
1520 /* walk_tree shouldn't be walking into any subtrees of a RECORD_TYPE
1521 anyway, but let's make sure of it. */
1522 *walk_subtrees = false;
1524 bool *valp = static_cast<bool*>(data);
1526 mark_or_check_tags (*tp, NULL, NULL, *valp);
1528 return NULL_TREE;
1531 /* Set IDENTIFIER_MARKED on all the ABI tags on T and its enclosing
1532 scopes. */
1534 static void
1535 mark_abi_tags (tree t, bool val)
1537 mark_or_check_tags (t, NULL, NULL, val);
1538 if (DECL_P (t))
1540 if (DECL_LANG_SPECIFIC (t) && DECL_USE_TEMPLATE (t)
1541 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (t)))
1543 /* Template arguments are part of the signature. */
1544 tree level = INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (t));
1545 for (int j = 0; j < TREE_VEC_LENGTH (level); ++j)
1547 tree arg = TREE_VEC_ELT (level, j);
1548 cp_walk_tree_without_duplicates (&arg, mark_abi_tags_r, &val);
1551 if (TREE_CODE (t) == FUNCTION_DECL)
1552 /* A function's parameter types are part of the signature, so
1553 we don't need to inherit any tags that are also in them. */
1554 for (tree arg = FUNCTION_FIRST_USER_PARMTYPE (t); arg;
1555 arg = TREE_CHAIN (arg))
1556 cp_walk_tree_without_duplicates (&TREE_VALUE (arg),
1557 mark_abi_tags_r, &val);
1561 /* Check that T has all the ABI tags that subobject SUBOB has, or
1562 warn if not. If T is a (variable or function) declaration, also
1563 add any missing tags. */
1565 static void
1566 check_abi_tags (tree t, tree subob)
1568 bool inherit = DECL_P (t);
1570 if (!inherit && !warn_abi_tag)
1571 return;
1573 tree decl = TYPE_P (t) ? TYPE_NAME (t) : t;
1574 if (!TREE_PUBLIC (decl))
1575 /* No need to worry about things local to this TU. */
1576 return;
1578 mark_abi_tags (t, true);
1580 tree subtype = TYPE_P (subob) ? subob : TREE_TYPE (subob);
1581 struct abi_tag_data data = { t, subob, error_mark_node };
1582 if (inherit)
1583 data.tags = NULL_TREE;
1585 cp_walk_tree_without_duplicates (&subtype, find_abi_tags_r, &data);
1587 if (inherit && data.tags)
1589 tree attr = lookup_attribute ("abi_tag", DECL_ATTRIBUTES (t));
1590 if (attr)
1591 TREE_VALUE (attr) = chainon (data.tags, TREE_VALUE (attr));
1592 else
1593 DECL_ATTRIBUTES (t)
1594 = tree_cons (get_identifier ("abi_tag"), data.tags,
1595 DECL_ATTRIBUTES (t));
1598 mark_abi_tags (t, false);
1601 /* Check that DECL has all the ABI tags that are used in parts of its type
1602 that are not reflected in its mangled name. */
1604 void
1605 check_abi_tags (tree decl)
1607 if (VAR_P (decl))
1608 check_abi_tags (decl, TREE_TYPE (decl));
1609 else if (TREE_CODE (decl) == FUNCTION_DECL
1610 && !mangle_return_type_p (decl))
1611 check_abi_tags (decl, TREE_TYPE (TREE_TYPE (decl)));
1614 void
1615 inherit_targ_abi_tags (tree t)
1617 if (!CLASS_TYPE_P (t)
1618 || CLASSTYPE_TEMPLATE_INFO (t) == NULL_TREE)
1619 return;
1621 mark_abi_tags (t, true);
1623 tree args = CLASSTYPE_TI_ARGS (t);
1624 struct abi_tag_data data = { t, NULL_TREE, NULL_TREE };
1625 for (int i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
1627 tree level = TMPL_ARGS_LEVEL (args, i+1);
1628 for (int j = 0; j < TREE_VEC_LENGTH (level); ++j)
1630 tree arg = TREE_VEC_ELT (level, j);
1631 data.subob = arg;
1632 cp_walk_tree_without_duplicates (&arg, find_abi_tags_r, &data);
1636 // If we found some tags on our template arguments, add them to our
1637 // abi_tag attribute.
1638 if (data.tags)
1640 tree attr = lookup_attribute ("abi_tag", TYPE_ATTRIBUTES (t));
1641 if (attr)
1642 TREE_VALUE (attr) = chainon (data.tags, TREE_VALUE (attr));
1643 else
1644 TYPE_ATTRIBUTES (t)
1645 = tree_cons (get_identifier ("abi_tag"), data.tags,
1646 TYPE_ATTRIBUTES (t));
1649 mark_abi_tags (t, false);
1652 /* Return true, iff class T has a non-virtual destructor that is
1653 accessible from outside the class heirarchy (i.e. is public, or
1654 there's a suitable friend. */
1656 static bool
1657 accessible_nvdtor_p (tree t)
1659 tree dtor = CLASSTYPE_DESTRUCTORS (t);
1661 /* An implicitly declared destructor is always public. And,
1662 if it were virtual, we would have created it by now. */
1663 if (!dtor)
1664 return true;
1666 if (DECL_VINDEX (dtor))
1667 return false; /* Virtual */
1669 if (!TREE_PRIVATE (dtor) && !TREE_PROTECTED (dtor))
1670 return true; /* Public */
1672 if (CLASSTYPE_FRIEND_CLASSES (t)
1673 || DECL_FRIENDLIST (TYPE_MAIN_DECL (t)))
1674 return true; /* Has friends */
1676 return false;
1679 /* Run through the base classes of T, updating CANT_HAVE_CONST_CTOR_P,
1680 and NO_CONST_ASN_REF_P. Also set flag bits in T based on
1681 properties of the bases. */
1683 static void
1684 check_bases (tree t,
1685 int* cant_have_const_ctor_p,
1686 int* no_const_asn_ref_p)
1688 int i;
1689 bool seen_non_virtual_nearly_empty_base_p = 0;
1690 int seen_tm_mask = 0;
1691 tree base_binfo;
1692 tree binfo;
1693 tree field = NULL_TREE;
1695 if (!CLASSTYPE_NON_STD_LAYOUT (t))
1696 for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
1697 if (TREE_CODE (field) == FIELD_DECL)
1698 break;
1700 for (binfo = TYPE_BINFO (t), i = 0;
1701 BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
1703 tree basetype = TREE_TYPE (base_binfo);
1705 gcc_assert (COMPLETE_TYPE_P (basetype));
1707 if (CLASSTYPE_FINAL (basetype))
1708 error ("cannot derive from %<final%> base %qT in derived type %qT",
1709 basetype, t);
1711 /* If any base class is non-literal, so is the derived class. */
1712 if (!CLASSTYPE_LITERAL_P (basetype))
1713 CLASSTYPE_LITERAL_P (t) = false;
1715 /* If the base class doesn't have copy constructors or
1716 assignment operators that take const references, then the
1717 derived class cannot have such a member automatically
1718 generated. */
1719 if (TYPE_HAS_COPY_CTOR (basetype)
1720 && ! TYPE_HAS_CONST_COPY_CTOR (basetype))
1721 *cant_have_const_ctor_p = 1;
1722 if (TYPE_HAS_COPY_ASSIGN (basetype)
1723 && !TYPE_HAS_CONST_COPY_ASSIGN (basetype))
1724 *no_const_asn_ref_p = 1;
1726 if (BINFO_VIRTUAL_P (base_binfo))
1727 /* A virtual base does not effect nearly emptiness. */
1729 else if (CLASSTYPE_NEARLY_EMPTY_P (basetype))
1731 if (seen_non_virtual_nearly_empty_base_p)
1732 /* And if there is more than one nearly empty base, then the
1733 derived class is not nearly empty either. */
1734 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
1735 else
1736 /* Remember we've seen one. */
1737 seen_non_virtual_nearly_empty_base_p = 1;
1739 else if (!is_empty_class (basetype))
1740 /* If the base class is not empty or nearly empty, then this
1741 class cannot be nearly empty. */
1742 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
1744 /* A lot of properties from the bases also apply to the derived
1745 class. */
1746 TYPE_NEEDS_CONSTRUCTING (t) |= TYPE_NEEDS_CONSTRUCTING (basetype);
1747 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t)
1748 |= TYPE_HAS_NONTRIVIAL_DESTRUCTOR (basetype);
1749 TYPE_HAS_COMPLEX_COPY_ASSIGN (t)
1750 |= (TYPE_HAS_COMPLEX_COPY_ASSIGN (basetype)
1751 || !TYPE_HAS_COPY_ASSIGN (basetype));
1752 TYPE_HAS_COMPLEX_COPY_CTOR (t) |= (TYPE_HAS_COMPLEX_COPY_CTOR (basetype)
1753 || !TYPE_HAS_COPY_CTOR (basetype));
1754 TYPE_HAS_COMPLEX_MOVE_ASSIGN (t)
1755 |= TYPE_HAS_COMPLEX_MOVE_ASSIGN (basetype);
1756 TYPE_HAS_COMPLEX_MOVE_CTOR (t) |= TYPE_HAS_COMPLEX_MOVE_CTOR (basetype);
1757 TYPE_POLYMORPHIC_P (t) |= TYPE_POLYMORPHIC_P (basetype);
1758 CLASSTYPE_CONTAINS_EMPTY_CLASS_P (t)
1759 |= CLASSTYPE_CONTAINS_EMPTY_CLASS_P (basetype);
1760 TYPE_HAS_COMPLEX_DFLT (t) |= (!TYPE_HAS_DEFAULT_CONSTRUCTOR (basetype)
1761 || TYPE_HAS_COMPLEX_DFLT (basetype));
1762 SET_CLASSTYPE_READONLY_FIELDS_NEED_INIT
1763 (t, CLASSTYPE_READONLY_FIELDS_NEED_INIT (t)
1764 | CLASSTYPE_READONLY_FIELDS_NEED_INIT (basetype));
1765 SET_CLASSTYPE_REF_FIELDS_NEED_INIT
1766 (t, CLASSTYPE_REF_FIELDS_NEED_INIT (t)
1767 | CLASSTYPE_REF_FIELDS_NEED_INIT (basetype));
1769 /* A standard-layout class is a class that:
1771 * has no non-standard-layout base classes, */
1772 CLASSTYPE_NON_STD_LAYOUT (t) |= CLASSTYPE_NON_STD_LAYOUT (basetype);
1773 if (!CLASSTYPE_NON_STD_LAYOUT (t))
1775 tree basefield;
1776 /* ...has no base classes of the same type as the first non-static
1777 data member... */
1778 if (field && DECL_CONTEXT (field) == t
1779 && (same_type_ignoring_top_level_qualifiers_p
1780 (TREE_TYPE (field), basetype)))
1781 CLASSTYPE_NON_STD_LAYOUT (t) = 1;
1782 else
1783 /* ...either has no non-static data members in the most-derived
1784 class and at most one base class with non-static data
1785 members, or has no base classes with non-static data
1786 members */
1787 for (basefield = TYPE_FIELDS (basetype); basefield;
1788 basefield = DECL_CHAIN (basefield))
1789 if (TREE_CODE (basefield) == FIELD_DECL)
1791 if (field)
1792 CLASSTYPE_NON_STD_LAYOUT (t) = 1;
1793 else
1794 field = basefield;
1795 break;
1799 /* Don't bother collecting tm attributes if transactional memory
1800 support is not enabled. */
1801 if (flag_tm)
1803 tree tm_attr = find_tm_attribute (TYPE_ATTRIBUTES (basetype));
1804 if (tm_attr)
1805 seen_tm_mask |= tm_attr_to_mask (tm_attr);
1808 check_abi_tags (t, basetype);
1811 /* If one of the base classes had TM attributes, and the current class
1812 doesn't define its own, then the current class inherits one. */
1813 if (seen_tm_mask && !find_tm_attribute (TYPE_ATTRIBUTES (t)))
1815 tree tm_attr = tm_mask_to_attr (seen_tm_mask & -seen_tm_mask);
1816 TYPE_ATTRIBUTES (t) = tree_cons (tm_attr, NULL, TYPE_ATTRIBUTES (t));
1820 /* Determine all the primary bases within T. Sets BINFO_PRIMARY_BASE_P for
1821 those that are primaries. Sets BINFO_LOST_PRIMARY_P for those
1822 that have had a nearly-empty virtual primary base stolen by some
1823 other base in the hierarchy. Determines CLASSTYPE_PRIMARY_BASE for
1824 T. */
1826 static void
1827 determine_primary_bases (tree t)
1829 unsigned i;
1830 tree primary = NULL_TREE;
1831 tree type_binfo = TYPE_BINFO (t);
1832 tree base_binfo;
1834 /* Determine the primary bases of our bases. */
1835 for (base_binfo = TREE_CHAIN (type_binfo); base_binfo;
1836 base_binfo = TREE_CHAIN (base_binfo))
1838 tree primary = CLASSTYPE_PRIMARY_BINFO (BINFO_TYPE (base_binfo));
1840 /* See if we're the non-virtual primary of our inheritance
1841 chain. */
1842 if (!BINFO_VIRTUAL_P (base_binfo))
1844 tree parent = BINFO_INHERITANCE_CHAIN (base_binfo);
1845 tree parent_primary = CLASSTYPE_PRIMARY_BINFO (BINFO_TYPE (parent));
1847 if (parent_primary
1848 && SAME_BINFO_TYPE_P (BINFO_TYPE (base_binfo),
1849 BINFO_TYPE (parent_primary)))
1850 /* We are the primary binfo. */
1851 BINFO_PRIMARY_P (base_binfo) = 1;
1853 /* Determine if we have a virtual primary base, and mark it so.
1855 if (primary && BINFO_VIRTUAL_P (primary))
1857 tree this_primary = copied_binfo (primary, base_binfo);
1859 if (BINFO_PRIMARY_P (this_primary))
1860 /* Someone already claimed this base. */
1861 BINFO_LOST_PRIMARY_P (base_binfo) = 1;
1862 else
1864 tree delta;
1866 BINFO_PRIMARY_P (this_primary) = 1;
1867 BINFO_INHERITANCE_CHAIN (this_primary) = base_binfo;
1869 /* A virtual binfo might have been copied from within
1870 another hierarchy. As we're about to use it as a
1871 primary base, make sure the offsets match. */
1872 delta = size_diffop_loc (input_location,
1873 fold_convert (ssizetype,
1874 BINFO_OFFSET (base_binfo)),
1875 fold_convert (ssizetype,
1876 BINFO_OFFSET (this_primary)));
1878 propagate_binfo_offsets (this_primary, delta);
1883 /* First look for a dynamic direct non-virtual base. */
1884 for (i = 0; BINFO_BASE_ITERATE (type_binfo, i, base_binfo); i++)
1886 tree basetype = BINFO_TYPE (base_binfo);
1888 if (TYPE_CONTAINS_VPTR_P (basetype) && !BINFO_VIRTUAL_P (base_binfo))
1890 primary = base_binfo;
1891 goto found;
1895 /* A "nearly-empty" virtual base class can be the primary base
1896 class, if no non-virtual polymorphic base can be found. Look for
1897 a nearly-empty virtual dynamic base that is not already a primary
1898 base of something in the hierarchy. If there is no such base,
1899 just pick the first nearly-empty virtual base. */
1901 for (base_binfo = TREE_CHAIN (type_binfo); base_binfo;
1902 base_binfo = TREE_CHAIN (base_binfo))
1903 if (BINFO_VIRTUAL_P (base_binfo)
1904 && CLASSTYPE_NEARLY_EMPTY_P (BINFO_TYPE (base_binfo)))
1906 if (!BINFO_PRIMARY_P (base_binfo))
1908 /* Found one that is not primary. */
1909 primary = base_binfo;
1910 goto found;
1912 else if (!primary)
1913 /* Remember the first candidate. */
1914 primary = base_binfo;
1917 found:
1918 /* If we've got a primary base, use it. */
1919 if (primary)
1921 tree basetype = BINFO_TYPE (primary);
1923 CLASSTYPE_PRIMARY_BINFO (t) = primary;
1924 if (BINFO_PRIMARY_P (primary))
1925 /* We are stealing a primary base. */
1926 BINFO_LOST_PRIMARY_P (BINFO_INHERITANCE_CHAIN (primary)) = 1;
1927 BINFO_PRIMARY_P (primary) = 1;
1928 if (BINFO_VIRTUAL_P (primary))
1930 tree delta;
1932 BINFO_INHERITANCE_CHAIN (primary) = type_binfo;
1933 /* A virtual binfo might have been copied from within
1934 another hierarchy. As we're about to use it as a primary
1935 base, make sure the offsets match. */
1936 delta = size_diffop_loc (input_location, ssize_int (0),
1937 fold_convert (ssizetype, BINFO_OFFSET (primary)));
1939 propagate_binfo_offsets (primary, delta);
1942 primary = TYPE_BINFO (basetype);
1944 TYPE_VFIELD (t) = TYPE_VFIELD (basetype);
1945 BINFO_VTABLE (type_binfo) = BINFO_VTABLE (primary);
1946 BINFO_VIRTUALS (type_binfo) = BINFO_VIRTUALS (primary);
1950 /* Update the variant types of T. */
1952 void
1953 fixup_type_variants (tree t)
1955 tree variants;
1957 if (!t)
1958 return;
1960 for (variants = TYPE_NEXT_VARIANT (t);
1961 variants;
1962 variants = TYPE_NEXT_VARIANT (variants))
1964 /* These fields are in the _TYPE part of the node, not in
1965 the TYPE_LANG_SPECIFIC component, so they are not shared. */
1966 TYPE_HAS_USER_CONSTRUCTOR (variants) = TYPE_HAS_USER_CONSTRUCTOR (t);
1967 TYPE_NEEDS_CONSTRUCTING (variants) = TYPE_NEEDS_CONSTRUCTING (t);
1968 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (variants)
1969 = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t);
1971 TYPE_POLYMORPHIC_P (variants) = TYPE_POLYMORPHIC_P (t);
1973 TYPE_BINFO (variants) = TYPE_BINFO (t);
1975 /* Copy whatever these are holding today. */
1976 TYPE_VFIELD (variants) = TYPE_VFIELD (t);
1977 TYPE_FIELDS (variants) = TYPE_FIELDS (t);
1981 /* KLASS is a class that we're applying may_alias to after the body is
1982 parsed. Fixup any POINTER_TO and REFERENCE_TO types. The
1983 canonical type(s) will be implicitly updated. */
1985 static void
1986 fixup_may_alias (tree klass)
1988 tree t;
1990 for (t = TYPE_POINTER_TO (klass); t; t = TYPE_NEXT_PTR_TO (t))
1991 TYPE_REF_CAN_ALIAS_ALL (t) = true;
1992 for (t = TYPE_REFERENCE_TO (klass); t; t = TYPE_NEXT_REF_TO (t))
1993 TYPE_REF_CAN_ALIAS_ALL (t) = true;
1996 /* Early variant fixups: we apply attributes at the beginning of the class
1997 definition, and we need to fix up any variants that have already been
1998 made via elaborated-type-specifier so that check_qualified_type works. */
2000 void
2001 fixup_attribute_variants (tree t)
2003 tree variants;
2005 if (!t)
2006 return;
2008 tree attrs = TYPE_ATTRIBUTES (t);
2009 unsigned align = TYPE_ALIGN (t);
2010 bool user_align = TYPE_USER_ALIGN (t);
2011 bool may_alias = lookup_attribute ("may_alias", attrs);
2013 if (may_alias)
2014 fixup_may_alias (t);
2016 for (variants = TYPE_NEXT_VARIANT (t);
2017 variants;
2018 variants = TYPE_NEXT_VARIANT (variants))
2020 /* These are the two fields that check_qualified_type looks at and
2021 are affected by attributes. */
2022 TYPE_ATTRIBUTES (variants) = attrs;
2023 unsigned valign = align;
2024 if (TYPE_USER_ALIGN (variants))
2025 valign = MAX (valign, TYPE_ALIGN (variants));
2026 else
2027 TYPE_USER_ALIGN (variants) = user_align;
2028 TYPE_ALIGN (variants) = valign;
2029 if (may_alias)
2030 fixup_may_alias (variants);
2034 /* Set memoizing fields and bits of T (and its variants) for later
2035 use. */
2037 static void
2038 finish_struct_bits (tree t)
2040 /* Fix up variants (if any). */
2041 fixup_type_variants (t);
2043 if (BINFO_N_BASE_BINFOS (TYPE_BINFO (t)) && TYPE_POLYMORPHIC_P (t))
2044 /* For a class w/o baseclasses, 'finish_struct' has set
2045 CLASSTYPE_PURE_VIRTUALS correctly (by definition).
2046 Similarly for a class whose base classes do not have vtables.
2047 When neither of these is true, we might have removed abstract
2048 virtuals (by providing a definition), added some (by declaring
2049 new ones), or redeclared ones from a base class. We need to
2050 recalculate what's really an abstract virtual at this point (by
2051 looking in the vtables). */
2052 get_pure_virtuals (t);
2054 /* If this type has a copy constructor or a destructor, force its
2055 mode to be BLKmode, and force its TREE_ADDRESSABLE bit to be
2056 nonzero. This will cause it to be passed by invisible reference
2057 and prevent it from being returned in a register. */
2058 if (type_has_nontrivial_copy_init (t)
2059 || TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t))
2061 tree variants;
2062 DECL_MODE (TYPE_MAIN_DECL (t)) = BLKmode;
2063 for (variants = t; variants; variants = TYPE_NEXT_VARIANT (variants))
2065 SET_TYPE_MODE (variants, BLKmode);
2066 TREE_ADDRESSABLE (variants) = 1;
2071 /* Issue warnings about T having private constructors, but no friends,
2072 and so forth.
2074 HAS_NONPRIVATE_METHOD is nonzero if T has any non-private methods or
2075 static members. HAS_NONPRIVATE_STATIC_FN is nonzero if T has any
2076 non-private static member functions. */
2078 static void
2079 maybe_warn_about_overly_private_class (tree t)
2081 int has_member_fn = 0;
2082 int has_nonprivate_method = 0;
2083 tree fn;
2085 if (!warn_ctor_dtor_privacy
2086 /* If the class has friends, those entities might create and
2087 access instances, so we should not warn. */
2088 || (CLASSTYPE_FRIEND_CLASSES (t)
2089 || DECL_FRIENDLIST (TYPE_MAIN_DECL (t)))
2090 /* We will have warned when the template was declared; there's
2091 no need to warn on every instantiation. */
2092 || CLASSTYPE_TEMPLATE_INSTANTIATION (t))
2093 /* There's no reason to even consider warning about this
2094 class. */
2095 return;
2097 /* We only issue one warning, if more than one applies, because
2098 otherwise, on code like:
2100 class A {
2101 // Oops - forgot `public:'
2102 A();
2103 A(const A&);
2104 ~A();
2107 we warn several times about essentially the same problem. */
2109 /* Check to see if all (non-constructor, non-destructor) member
2110 functions are private. (Since there are no friends or
2111 non-private statics, we can't ever call any of the private member
2112 functions.) */
2113 for (fn = TYPE_METHODS (t); fn; fn = DECL_CHAIN (fn))
2114 /* We're not interested in compiler-generated methods; they don't
2115 provide any way to call private members. */
2116 if (!DECL_ARTIFICIAL (fn))
2118 if (!TREE_PRIVATE (fn))
2120 if (DECL_STATIC_FUNCTION_P (fn))
2121 /* A non-private static member function is just like a
2122 friend; it can create and invoke private member
2123 functions, and be accessed without a class
2124 instance. */
2125 return;
2127 has_nonprivate_method = 1;
2128 /* Keep searching for a static member function. */
2130 else if (!DECL_CONSTRUCTOR_P (fn) && !DECL_DESTRUCTOR_P (fn))
2131 has_member_fn = 1;
2134 if (!has_nonprivate_method && has_member_fn)
2136 /* There are no non-private methods, and there's at least one
2137 private member function that isn't a constructor or
2138 destructor. (If all the private members are
2139 constructors/destructors we want to use the code below that
2140 issues error messages specifically referring to
2141 constructors/destructors.) */
2142 unsigned i;
2143 tree binfo = TYPE_BINFO (t);
2145 for (i = 0; i != BINFO_N_BASE_BINFOS (binfo); i++)
2146 if (BINFO_BASE_ACCESS (binfo, i) != access_private_node)
2148 has_nonprivate_method = 1;
2149 break;
2151 if (!has_nonprivate_method)
2153 warning (OPT_Wctor_dtor_privacy,
2154 "all member functions in class %qT are private", t);
2155 return;
2159 /* Even if some of the member functions are non-private, the class
2160 won't be useful for much if all the constructors or destructors
2161 are private: such an object can never be created or destroyed. */
2162 fn = CLASSTYPE_DESTRUCTORS (t);
2163 if (fn && TREE_PRIVATE (fn))
2165 warning (OPT_Wctor_dtor_privacy,
2166 "%q#T only defines a private destructor and has no friends",
2168 return;
2171 /* Warn about classes that have private constructors and no friends. */
2172 if (TYPE_HAS_USER_CONSTRUCTOR (t)
2173 /* Implicitly generated constructors are always public. */
2174 && (!CLASSTYPE_LAZY_DEFAULT_CTOR (t)
2175 || !CLASSTYPE_LAZY_COPY_CTOR (t)))
2177 int nonprivate_ctor = 0;
2179 /* If a non-template class does not define a copy
2180 constructor, one is defined for it, enabling it to avoid
2181 this warning. For a template class, this does not
2182 happen, and so we would normally get a warning on:
2184 template <class T> class C { private: C(); };
2186 To avoid this asymmetry, we check TYPE_HAS_COPY_CTOR. All
2187 complete non-template or fully instantiated classes have this
2188 flag set. */
2189 if (!TYPE_HAS_COPY_CTOR (t))
2190 nonprivate_ctor = 1;
2191 else
2192 for (fn = CLASSTYPE_CONSTRUCTORS (t); fn; fn = OVL_NEXT (fn))
2194 tree ctor = OVL_CURRENT (fn);
2195 /* Ideally, we wouldn't count copy constructors (or, in
2196 fact, any constructor that takes an argument of the
2197 class type as a parameter) because such things cannot
2198 be used to construct an instance of the class unless
2199 you already have one. But, for now at least, we're
2200 more generous. */
2201 if (! TREE_PRIVATE (ctor))
2203 nonprivate_ctor = 1;
2204 break;
2208 if (nonprivate_ctor == 0)
2210 warning (OPT_Wctor_dtor_privacy,
2211 "%q#T only defines private constructors and has no friends",
2213 return;
2218 static struct {
2219 gt_pointer_operator new_value;
2220 void *cookie;
2221 } resort_data;
2223 /* Comparison function to compare two TYPE_METHOD_VEC entries by name. */
2225 static int
2226 method_name_cmp (const void* m1_p, const void* m2_p)
2228 const tree *const m1 = (const tree *) m1_p;
2229 const tree *const m2 = (const tree *) m2_p;
2231 if (*m1 == NULL_TREE && *m2 == NULL_TREE)
2232 return 0;
2233 if (*m1 == NULL_TREE)
2234 return -1;
2235 if (*m2 == NULL_TREE)
2236 return 1;
2237 if (DECL_NAME (OVL_CURRENT (*m1)) < DECL_NAME (OVL_CURRENT (*m2)))
2238 return -1;
2239 return 1;
2242 /* This routine compares two fields like method_name_cmp but using the
2243 pointer operator in resort_field_decl_data. */
2245 static int
2246 resort_method_name_cmp (const void* m1_p, const void* m2_p)
2248 const tree *const m1 = (const tree *) m1_p;
2249 const tree *const m2 = (const tree *) m2_p;
2250 if (*m1 == NULL_TREE && *m2 == NULL_TREE)
2251 return 0;
2252 if (*m1 == NULL_TREE)
2253 return -1;
2254 if (*m2 == NULL_TREE)
2255 return 1;
2257 tree d1 = DECL_NAME (OVL_CURRENT (*m1));
2258 tree d2 = DECL_NAME (OVL_CURRENT (*m2));
2259 resort_data.new_value (&d1, resort_data.cookie);
2260 resort_data.new_value (&d2, resort_data.cookie);
2261 if (d1 < d2)
2262 return -1;
2264 return 1;
2267 /* Resort TYPE_METHOD_VEC because pointers have been reordered. */
2269 void
2270 resort_type_method_vec (void* obj,
2271 void* /*orig_obj*/,
2272 gt_pointer_operator new_value,
2273 void* cookie)
2275 vec<tree, va_gc> *method_vec = (vec<tree, va_gc> *) obj;
2276 int len = vec_safe_length (method_vec);
2277 size_t slot;
2278 tree fn;
2280 /* The type conversion ops have to live at the front of the vec, so we
2281 can't sort them. */
2282 for (slot = CLASSTYPE_FIRST_CONVERSION_SLOT;
2283 vec_safe_iterate (method_vec, slot, &fn);
2284 ++slot)
2285 if (!DECL_CONV_FN_P (OVL_CURRENT (fn)))
2286 break;
2288 if (len - slot > 1)
2290 resort_data.new_value = new_value;
2291 resort_data.cookie = cookie;
2292 qsort (method_vec->address () + slot, len - slot, sizeof (tree),
2293 resort_method_name_cmp);
2297 /* Warn about duplicate methods in fn_fields.
2299 Sort methods that are not special (i.e., constructors, destructors,
2300 and type conversion operators) so that we can find them faster in
2301 search. */
2303 static void
2304 finish_struct_methods (tree t)
2306 tree fn_fields;
2307 vec<tree, va_gc> *method_vec;
2308 int slot, len;
2310 method_vec = CLASSTYPE_METHOD_VEC (t);
2311 if (!method_vec)
2312 return;
2314 len = method_vec->length ();
2316 /* Clear DECL_IN_AGGR_P for all functions. */
2317 for (fn_fields = TYPE_METHODS (t); fn_fields;
2318 fn_fields = DECL_CHAIN (fn_fields))
2319 DECL_IN_AGGR_P (fn_fields) = 0;
2321 /* Issue warnings about private constructors and such. If there are
2322 no methods, then some public defaults are generated. */
2323 maybe_warn_about_overly_private_class (t);
2325 /* The type conversion ops have to live at the front of the vec, so we
2326 can't sort them. */
2327 for (slot = CLASSTYPE_FIRST_CONVERSION_SLOT;
2328 method_vec->iterate (slot, &fn_fields);
2329 ++slot)
2330 if (!DECL_CONV_FN_P (OVL_CURRENT (fn_fields)))
2331 break;
2332 if (len - slot > 1)
2333 qsort (method_vec->address () + slot,
2334 len-slot, sizeof (tree), method_name_cmp);
2337 /* Make BINFO's vtable have N entries, including RTTI entries,
2338 vbase and vcall offsets, etc. Set its type and call the back end
2339 to lay it out. */
2341 static void
2342 layout_vtable_decl (tree binfo, int n)
2344 tree atype;
2345 tree vtable;
2347 atype = build_array_of_n_type (vtable_entry_type, n);
2348 layout_type (atype);
2350 /* We may have to grow the vtable. */
2351 vtable = get_vtbl_decl_for_binfo (binfo);
2352 if (!same_type_p (TREE_TYPE (vtable), atype))
2354 TREE_TYPE (vtable) = atype;
2355 DECL_SIZE (vtable) = DECL_SIZE_UNIT (vtable) = NULL_TREE;
2356 layout_decl (vtable, 0);
2360 /* True iff FNDECL and BASE_FNDECL (both non-static member functions)
2361 have the same signature. */
2364 same_signature_p (const_tree fndecl, const_tree base_fndecl)
2366 /* One destructor overrides another if they are the same kind of
2367 destructor. */
2368 if (DECL_DESTRUCTOR_P (base_fndecl) && DECL_DESTRUCTOR_P (fndecl)
2369 && special_function_p (base_fndecl) == special_function_p (fndecl))
2370 return 1;
2371 /* But a non-destructor never overrides a destructor, nor vice
2372 versa, nor do different kinds of destructors override
2373 one-another. For example, a complete object destructor does not
2374 override a deleting destructor. */
2375 if (DECL_DESTRUCTOR_P (base_fndecl) || DECL_DESTRUCTOR_P (fndecl))
2376 return 0;
2378 if (DECL_NAME (fndecl) == DECL_NAME (base_fndecl)
2379 || (DECL_CONV_FN_P (fndecl)
2380 && DECL_CONV_FN_P (base_fndecl)
2381 && same_type_p (DECL_CONV_FN_TYPE (fndecl),
2382 DECL_CONV_FN_TYPE (base_fndecl))))
2384 tree fntype = TREE_TYPE (fndecl);
2385 tree base_fntype = TREE_TYPE (base_fndecl);
2386 if (type_memfn_quals (fntype) == type_memfn_quals (base_fntype)
2387 && type_memfn_rqual (fntype) == type_memfn_rqual (base_fntype)
2388 && compparms (FUNCTION_FIRST_USER_PARMTYPE (fndecl),
2389 FUNCTION_FIRST_USER_PARMTYPE (base_fndecl)))
2390 return 1;
2392 return 0;
2395 /* Returns TRUE if DERIVED is a binfo containing the binfo BASE as a
2396 subobject. */
2398 static bool
2399 base_derived_from (tree derived, tree base)
2401 tree probe;
2403 for (probe = base; probe; probe = BINFO_INHERITANCE_CHAIN (probe))
2405 if (probe == derived)
2406 return true;
2407 else if (BINFO_VIRTUAL_P (probe))
2408 /* If we meet a virtual base, we can't follow the inheritance
2409 any more. See if the complete type of DERIVED contains
2410 such a virtual base. */
2411 return (binfo_for_vbase (BINFO_TYPE (probe), BINFO_TYPE (derived))
2412 != NULL_TREE);
2414 return false;
2417 struct find_final_overrider_data {
2418 /* The function for which we are trying to find a final overrider. */
2419 tree fn;
2420 /* The base class in which the function was declared. */
2421 tree declaring_base;
2422 /* The candidate overriders. */
2423 tree candidates;
2424 /* Path to most derived. */
2425 vec<tree> path;
2428 /* Add the overrider along the current path to FFOD->CANDIDATES.
2429 Returns true if an overrider was found; false otherwise. */
2431 static bool
2432 dfs_find_final_overrider_1 (tree binfo,
2433 find_final_overrider_data *ffod,
2434 unsigned depth)
2436 tree method;
2438 /* If BINFO is not the most derived type, try a more derived class.
2439 A definition there will overrider a definition here. */
2440 if (depth)
2442 depth--;
2443 if (dfs_find_final_overrider_1
2444 (ffod->path[depth], ffod, depth))
2445 return true;
2448 method = look_for_overrides_here (BINFO_TYPE (binfo), ffod->fn);
2449 if (method)
2451 tree *candidate = &ffod->candidates;
2453 /* Remove any candidates overridden by this new function. */
2454 while (*candidate)
2456 /* If *CANDIDATE overrides METHOD, then METHOD
2457 cannot override anything else on the list. */
2458 if (base_derived_from (TREE_VALUE (*candidate), binfo))
2459 return true;
2460 /* If METHOD overrides *CANDIDATE, remove *CANDIDATE. */
2461 if (base_derived_from (binfo, TREE_VALUE (*candidate)))
2462 *candidate = TREE_CHAIN (*candidate);
2463 else
2464 candidate = &TREE_CHAIN (*candidate);
2467 /* Add the new function. */
2468 ffod->candidates = tree_cons (method, binfo, ffod->candidates);
2469 return true;
2472 return false;
2475 /* Called from find_final_overrider via dfs_walk. */
2477 static tree
2478 dfs_find_final_overrider_pre (tree binfo, void *data)
2480 find_final_overrider_data *ffod = (find_final_overrider_data *) data;
2482 if (binfo == ffod->declaring_base)
2483 dfs_find_final_overrider_1 (binfo, ffod, ffod->path.length ());
2484 ffod->path.safe_push (binfo);
2486 return NULL_TREE;
2489 static tree
2490 dfs_find_final_overrider_post (tree /*binfo*/, void *data)
2492 find_final_overrider_data *ffod = (find_final_overrider_data *) data;
2493 ffod->path.pop ();
2495 return NULL_TREE;
2498 /* Returns a TREE_LIST whose TREE_PURPOSE is the final overrider for
2499 FN and whose TREE_VALUE is the binfo for the base where the
2500 overriding occurs. BINFO (in the hierarchy dominated by the binfo
2501 DERIVED) is the base object in which FN is declared. */
2503 static tree
2504 find_final_overrider (tree derived, tree binfo, tree fn)
2506 find_final_overrider_data ffod;
2508 /* Getting this right is a little tricky. This is valid:
2510 struct S { virtual void f (); };
2511 struct T { virtual void f (); };
2512 struct U : public S, public T { };
2514 even though calling `f' in `U' is ambiguous. But,
2516 struct R { virtual void f(); };
2517 struct S : virtual public R { virtual void f (); };
2518 struct T : virtual public R { virtual void f (); };
2519 struct U : public S, public T { };
2521 is not -- there's no way to decide whether to put `S::f' or
2522 `T::f' in the vtable for `R'.
2524 The solution is to look at all paths to BINFO. If we find
2525 different overriders along any two, then there is a problem. */
2526 if (DECL_THUNK_P (fn))
2527 fn = THUNK_TARGET (fn);
2529 /* Determine the depth of the hierarchy. */
2530 ffod.fn = fn;
2531 ffod.declaring_base = binfo;
2532 ffod.candidates = NULL_TREE;
2533 ffod.path.create (30);
2535 dfs_walk_all (derived, dfs_find_final_overrider_pre,
2536 dfs_find_final_overrider_post, &ffod);
2538 ffod.path.release ();
2540 /* If there was no winner, issue an error message. */
2541 if (!ffod.candidates || TREE_CHAIN (ffod.candidates))
2542 return error_mark_node;
2544 return ffod.candidates;
2547 /* Return the index of the vcall offset for FN when TYPE is used as a
2548 virtual base. */
2550 static tree
2551 get_vcall_index (tree fn, tree type)
2553 vec<tree_pair_s, va_gc> *indices = CLASSTYPE_VCALL_INDICES (type);
2554 tree_pair_p p;
2555 unsigned ix;
2557 FOR_EACH_VEC_SAFE_ELT (indices, ix, p)
2558 if ((DECL_DESTRUCTOR_P (fn) && DECL_DESTRUCTOR_P (p->purpose))
2559 || same_signature_p (fn, p->purpose))
2560 return p->value;
2562 /* There should always be an appropriate index. */
2563 gcc_unreachable ();
2566 /* Update an entry in the vtable for BINFO, which is in the hierarchy
2567 dominated by T. FN is the old function; VIRTUALS points to the
2568 corresponding position in the new BINFO_VIRTUALS list. IX is the index
2569 of that entry in the list. */
2571 static void
2572 update_vtable_entry_for_fn (tree t, tree binfo, tree fn, tree* virtuals,
2573 unsigned ix)
2575 tree b;
2576 tree overrider;
2577 tree delta;
2578 tree virtual_base;
2579 tree first_defn;
2580 tree overrider_fn, overrider_target;
2581 tree target_fn = DECL_THUNK_P (fn) ? THUNK_TARGET (fn) : fn;
2582 tree over_return, base_return;
2583 bool lost = false;
2585 /* Find the nearest primary base (possibly binfo itself) which defines
2586 this function; this is the class the caller will convert to when
2587 calling FN through BINFO. */
2588 for (b = binfo; ; b = get_primary_binfo (b))
2590 gcc_assert (b);
2591 if (look_for_overrides_here (BINFO_TYPE (b), target_fn))
2592 break;
2594 /* The nearest definition is from a lost primary. */
2595 if (BINFO_LOST_PRIMARY_P (b))
2596 lost = true;
2598 first_defn = b;
2600 /* Find the final overrider. */
2601 overrider = find_final_overrider (TYPE_BINFO (t), b, target_fn);
2602 if (overrider == error_mark_node)
2604 error ("no unique final overrider for %qD in %qT", target_fn, t);
2605 return;
2607 overrider_target = overrider_fn = TREE_PURPOSE (overrider);
2609 /* Check for adjusting covariant return types. */
2610 over_return = TREE_TYPE (TREE_TYPE (overrider_target));
2611 base_return = TREE_TYPE (TREE_TYPE (target_fn));
2613 if (POINTER_TYPE_P (over_return)
2614 && TREE_CODE (over_return) == TREE_CODE (base_return)
2615 && CLASS_TYPE_P (TREE_TYPE (over_return))
2616 && CLASS_TYPE_P (TREE_TYPE (base_return))
2617 /* If the overrider is invalid, don't even try. */
2618 && !DECL_INVALID_OVERRIDER_P (overrider_target))
2620 /* If FN is a covariant thunk, we must figure out the adjustment
2621 to the final base FN was converting to. As OVERRIDER_TARGET might
2622 also be converting to the return type of FN, we have to
2623 combine the two conversions here. */
2624 tree fixed_offset, virtual_offset;
2626 over_return = TREE_TYPE (over_return);
2627 base_return = TREE_TYPE (base_return);
2629 if (DECL_THUNK_P (fn))
2631 gcc_assert (DECL_RESULT_THUNK_P (fn));
2632 fixed_offset = ssize_int (THUNK_FIXED_OFFSET (fn));
2633 virtual_offset = THUNK_VIRTUAL_OFFSET (fn);
2635 else
2636 fixed_offset = virtual_offset = NULL_TREE;
2638 if (virtual_offset)
2639 /* Find the equivalent binfo within the return type of the
2640 overriding function. We will want the vbase offset from
2641 there. */
2642 virtual_offset = binfo_for_vbase (BINFO_TYPE (virtual_offset),
2643 over_return);
2644 else if (!same_type_ignoring_top_level_qualifiers_p
2645 (over_return, base_return))
2647 /* There was no existing virtual thunk (which takes
2648 precedence). So find the binfo of the base function's
2649 return type within the overriding function's return type.
2650 Fortunately we know the covariancy is valid (it
2651 has already been checked), so we can just iterate along
2652 the binfos, which have been chained in inheritance graph
2653 order. Of course it is lame that we have to repeat the
2654 search here anyway -- we should really be caching pieces
2655 of the vtable and avoiding this repeated work. */
2656 tree thunk_binfo, base_binfo;
2658 /* Find the base binfo within the overriding function's
2659 return type. We will always find a thunk_binfo, except
2660 when the covariancy is invalid (which we will have
2661 already diagnosed). */
2662 for (base_binfo = TYPE_BINFO (base_return),
2663 thunk_binfo = TYPE_BINFO (over_return);
2664 thunk_binfo;
2665 thunk_binfo = TREE_CHAIN (thunk_binfo))
2666 if (SAME_BINFO_TYPE_P (BINFO_TYPE (thunk_binfo),
2667 BINFO_TYPE (base_binfo)))
2668 break;
2670 /* See if virtual inheritance is involved. */
2671 for (virtual_offset = thunk_binfo;
2672 virtual_offset;
2673 virtual_offset = BINFO_INHERITANCE_CHAIN (virtual_offset))
2674 if (BINFO_VIRTUAL_P (virtual_offset))
2675 break;
2677 if (virtual_offset
2678 || (thunk_binfo && !BINFO_OFFSET_ZEROP (thunk_binfo)))
2680 tree offset = fold_convert (ssizetype, BINFO_OFFSET (thunk_binfo));
2682 if (virtual_offset)
2684 /* We convert via virtual base. Adjust the fixed
2685 offset to be from there. */
2686 offset =
2687 size_diffop (offset,
2688 fold_convert (ssizetype,
2689 BINFO_OFFSET (virtual_offset)));
2691 if (fixed_offset)
2692 /* There was an existing fixed offset, this must be
2693 from the base just converted to, and the base the
2694 FN was thunking to. */
2695 fixed_offset = size_binop (PLUS_EXPR, fixed_offset, offset);
2696 else
2697 fixed_offset = offset;
2701 if (fixed_offset || virtual_offset)
2702 /* Replace the overriding function with a covariant thunk. We
2703 will emit the overriding function in its own slot as
2704 well. */
2705 overrider_fn = make_thunk (overrider_target, /*this_adjusting=*/0,
2706 fixed_offset, virtual_offset);
2708 else
2709 gcc_assert (DECL_INVALID_OVERRIDER_P (overrider_target) ||
2710 !DECL_THUNK_P (fn));
2712 /* If we need a covariant thunk, then we may need to adjust first_defn.
2713 The ABI specifies that the thunks emitted with a function are
2714 determined by which bases the function overrides, so we need to be
2715 sure that we're using a thunk for some overridden base; even if we
2716 know that the necessary this adjustment is zero, there may not be an
2717 appropriate zero-this-adjusment thunk for us to use since thunks for
2718 overriding virtual bases always use the vcall offset.
2720 Furthermore, just choosing any base that overrides this function isn't
2721 quite right, as this slot won't be used for calls through a type that
2722 puts a covariant thunk here. Calling the function through such a type
2723 will use a different slot, and that slot is the one that determines
2724 the thunk emitted for that base.
2726 So, keep looking until we find the base that we're really overriding
2727 in this slot: the nearest primary base that doesn't use a covariant
2728 thunk in this slot. */
2729 if (overrider_target != overrider_fn)
2731 if (BINFO_TYPE (b) == DECL_CONTEXT (overrider_target))
2732 /* We already know that the overrider needs a covariant thunk. */
2733 b = get_primary_binfo (b);
2734 for (; ; b = get_primary_binfo (b))
2736 tree main_binfo = TYPE_BINFO (BINFO_TYPE (b));
2737 tree bv = chain_index (ix, BINFO_VIRTUALS (main_binfo));
2738 if (!DECL_THUNK_P (TREE_VALUE (bv)))
2739 break;
2740 if (BINFO_LOST_PRIMARY_P (b))
2741 lost = true;
2743 first_defn = b;
2746 /* Assume that we will produce a thunk that convert all the way to
2747 the final overrider, and not to an intermediate virtual base. */
2748 virtual_base = NULL_TREE;
2750 /* See if we can convert to an intermediate virtual base first, and then
2751 use the vcall offset located there to finish the conversion. */
2752 for (; b; b = BINFO_INHERITANCE_CHAIN (b))
2754 /* If we find the final overrider, then we can stop
2755 walking. */
2756 if (SAME_BINFO_TYPE_P (BINFO_TYPE (b),
2757 BINFO_TYPE (TREE_VALUE (overrider))))
2758 break;
2760 /* If we find a virtual base, and we haven't yet found the
2761 overrider, then there is a virtual base between the
2762 declaring base (first_defn) and the final overrider. */
2763 if (BINFO_VIRTUAL_P (b))
2765 virtual_base = b;
2766 break;
2770 /* Compute the constant adjustment to the `this' pointer. The
2771 `this' pointer, when this function is called, will point at BINFO
2772 (or one of its primary bases, which are at the same offset). */
2773 if (virtual_base)
2774 /* The `this' pointer needs to be adjusted from the declaration to
2775 the nearest virtual base. */
2776 delta = size_diffop_loc (input_location,
2777 fold_convert (ssizetype, BINFO_OFFSET (virtual_base)),
2778 fold_convert (ssizetype, BINFO_OFFSET (first_defn)));
2779 else if (lost)
2780 /* If the nearest definition is in a lost primary, we don't need an
2781 entry in our vtable. Except possibly in a constructor vtable,
2782 if we happen to get our primary back. In that case, the offset
2783 will be zero, as it will be a primary base. */
2784 delta = size_zero_node;
2785 else
2786 /* The `this' pointer needs to be adjusted from pointing to
2787 BINFO to pointing at the base where the final overrider
2788 appears. */
2789 delta = size_diffop_loc (input_location,
2790 fold_convert (ssizetype,
2791 BINFO_OFFSET (TREE_VALUE (overrider))),
2792 fold_convert (ssizetype, BINFO_OFFSET (binfo)));
2794 modify_vtable_entry (t, binfo, overrider_fn, delta, virtuals);
2796 if (virtual_base)
2797 BV_VCALL_INDEX (*virtuals)
2798 = get_vcall_index (overrider_target, BINFO_TYPE (virtual_base));
2799 else
2800 BV_VCALL_INDEX (*virtuals) = NULL_TREE;
2802 BV_LOST_PRIMARY (*virtuals) = lost;
2805 /* Called from modify_all_vtables via dfs_walk. */
2807 static tree
2808 dfs_modify_vtables (tree binfo, void* data)
2810 tree t = (tree) data;
2811 tree virtuals;
2812 tree old_virtuals;
2813 unsigned ix;
2815 if (!TYPE_CONTAINS_VPTR_P (BINFO_TYPE (binfo)))
2816 /* A base without a vtable needs no modification, and its bases
2817 are uninteresting. */
2818 return dfs_skip_bases;
2820 if (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), t)
2821 && !CLASSTYPE_HAS_PRIMARY_BASE_P (t))
2822 /* Don't do the primary vtable, if it's new. */
2823 return NULL_TREE;
2825 if (BINFO_PRIMARY_P (binfo) && !BINFO_VIRTUAL_P (binfo))
2826 /* There's no need to modify the vtable for a non-virtual primary
2827 base; we're not going to use that vtable anyhow. We do still
2828 need to do this for virtual primary bases, as they could become
2829 non-primary in a construction vtable. */
2830 return NULL_TREE;
2832 make_new_vtable (t, binfo);
2834 /* Now, go through each of the virtual functions in the virtual
2835 function table for BINFO. Find the final overrider, and update
2836 the BINFO_VIRTUALS list appropriately. */
2837 for (ix = 0, virtuals = BINFO_VIRTUALS (binfo),
2838 old_virtuals = BINFO_VIRTUALS (TYPE_BINFO (BINFO_TYPE (binfo)));
2839 virtuals;
2840 ix++, virtuals = TREE_CHAIN (virtuals),
2841 old_virtuals = TREE_CHAIN (old_virtuals))
2842 update_vtable_entry_for_fn (t,
2843 binfo,
2844 BV_FN (old_virtuals),
2845 &virtuals, ix);
2847 return NULL_TREE;
2850 /* Update all of the primary and secondary vtables for T. Create new
2851 vtables as required, and initialize their RTTI information. Each
2852 of the functions in VIRTUALS is declared in T and may override a
2853 virtual function from a base class; find and modify the appropriate
2854 entries to point to the overriding functions. Returns a list, in
2855 declaration order, of the virtual functions that are declared in T,
2856 but do not appear in the primary base class vtable, and which
2857 should therefore be appended to the end of the vtable for T. */
2859 static tree
2860 modify_all_vtables (tree t, tree virtuals)
2862 tree binfo = TYPE_BINFO (t);
2863 tree *fnsp;
2865 /* Mangle the vtable name before entering dfs_walk (c++/51884). */
2866 if (TYPE_CONTAINS_VPTR_P (t))
2867 get_vtable_decl (t, false);
2869 /* Update all of the vtables. */
2870 dfs_walk_once (binfo, dfs_modify_vtables, NULL, t);
2872 /* Add virtual functions not already in our primary vtable. These
2873 will be both those introduced by this class, and those overridden
2874 from secondary bases. It does not include virtuals merely
2875 inherited from secondary bases. */
2876 for (fnsp = &virtuals; *fnsp; )
2878 tree fn = TREE_VALUE (*fnsp);
2880 if (!value_member (fn, BINFO_VIRTUALS (binfo))
2881 || DECL_VINDEX (fn) == error_mark_node)
2883 /* We don't need to adjust the `this' pointer when
2884 calling this function. */
2885 BV_DELTA (*fnsp) = integer_zero_node;
2886 BV_VCALL_INDEX (*fnsp) = NULL_TREE;
2888 /* This is a function not already in our vtable. Keep it. */
2889 fnsp = &TREE_CHAIN (*fnsp);
2891 else
2892 /* We've already got an entry for this function. Skip it. */
2893 *fnsp = TREE_CHAIN (*fnsp);
2896 return virtuals;
2899 /* Get the base virtual function declarations in T that have the
2900 indicated NAME. */
2902 static void
2903 get_basefndecls (tree name, tree t, vec<tree> *base_fndecls)
2905 tree methods;
2906 int n_baseclasses = BINFO_N_BASE_BINFOS (TYPE_BINFO (t));
2907 int i;
2909 /* Find virtual functions in T with the indicated NAME. */
2910 i = lookup_fnfields_1 (t, name);
2911 bool found_decls = false;
2912 if (i != -1)
2913 for (methods = (*CLASSTYPE_METHOD_VEC (t))[i];
2914 methods;
2915 methods = OVL_NEXT (methods))
2917 tree method = OVL_CURRENT (methods);
2919 if (TREE_CODE (method) == FUNCTION_DECL
2920 && DECL_VINDEX (method))
2922 base_fndecls->safe_push (method);
2923 found_decls = true;
2927 if (found_decls)
2928 return;
2930 for (i = 0; i < n_baseclasses; i++)
2932 tree basetype = BINFO_TYPE (BINFO_BASE_BINFO (TYPE_BINFO (t), i));
2933 get_basefndecls (name, basetype, base_fndecls);
2937 /* If this declaration supersedes the declaration of
2938 a method declared virtual in the base class, then
2939 mark this field as being virtual as well. */
2941 void
2942 check_for_override (tree decl, tree ctype)
2944 bool overrides_found = false;
2945 if (TREE_CODE (decl) == TEMPLATE_DECL)
2946 /* In [temp.mem] we have:
2948 A specialization of a member function template does not
2949 override a virtual function from a base class. */
2950 return;
2951 if ((DECL_DESTRUCTOR_P (decl)
2952 || IDENTIFIER_VIRTUAL_P (DECL_NAME (decl))
2953 || DECL_CONV_FN_P (decl))
2954 && look_for_overrides (ctype, decl)
2955 && !DECL_STATIC_FUNCTION_P (decl))
2956 /* Set DECL_VINDEX to a value that is neither an INTEGER_CST nor
2957 the error_mark_node so that we know it is an overriding
2958 function. */
2960 DECL_VINDEX (decl) = decl;
2961 overrides_found = true;
2962 if (warn_override && !DECL_OVERRIDE_P (decl)
2963 && !DECL_DESTRUCTOR_P (decl))
2964 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wsuggest_override,
2965 "%qD can be marked override", decl);
2968 if (DECL_VIRTUAL_P (decl))
2970 if (!DECL_VINDEX (decl))
2971 DECL_VINDEX (decl) = error_mark_node;
2972 IDENTIFIER_VIRTUAL_P (DECL_NAME (decl)) = 1;
2973 if (DECL_DESTRUCTOR_P (decl))
2974 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (ctype) = true;
2976 else if (DECL_FINAL_P (decl))
2977 error ("%q+#D marked %<final%>, but is not virtual", decl);
2978 if (DECL_OVERRIDE_P (decl) && !overrides_found)
2979 error ("%q+#D marked %<override%>, but does not override", decl);
2982 /* Warn about hidden virtual functions that are not overridden in t.
2983 We know that constructors and destructors don't apply. */
2985 static void
2986 warn_hidden (tree t)
2988 vec<tree, va_gc> *method_vec = CLASSTYPE_METHOD_VEC (t);
2989 tree fns;
2990 size_t i;
2992 /* We go through each separately named virtual function. */
2993 for (i = CLASSTYPE_FIRST_CONVERSION_SLOT;
2994 vec_safe_iterate (method_vec, i, &fns);
2995 ++i)
2997 tree fn;
2998 tree name;
2999 tree fndecl;
3000 tree base_binfo;
3001 tree binfo;
3002 int j;
3004 /* All functions in this slot in the CLASSTYPE_METHOD_VEC will
3005 have the same name. Figure out what name that is. */
3006 name = DECL_NAME (OVL_CURRENT (fns));
3007 /* There are no possibly hidden functions yet. */
3008 auto_vec<tree, 20> base_fndecls;
3009 /* Iterate through all of the base classes looking for possibly
3010 hidden functions. */
3011 for (binfo = TYPE_BINFO (t), j = 0;
3012 BINFO_BASE_ITERATE (binfo, j, base_binfo); j++)
3014 tree basetype = BINFO_TYPE (base_binfo);
3015 get_basefndecls (name, basetype, &base_fndecls);
3018 /* If there are no functions to hide, continue. */
3019 if (base_fndecls.is_empty ())
3020 continue;
3022 /* Remove any overridden functions. */
3023 for (fn = fns; fn; fn = OVL_NEXT (fn))
3025 fndecl = OVL_CURRENT (fn);
3026 if (TREE_CODE (fndecl) == FUNCTION_DECL
3027 && DECL_VINDEX (fndecl))
3029 /* If the method from the base class has the same
3030 signature as the method from the derived class, it
3031 has been overridden. */
3032 for (size_t k = 0; k < base_fndecls.length (); k++)
3033 if (base_fndecls[k]
3034 && same_signature_p (fndecl, base_fndecls[k]))
3035 base_fndecls[k] = NULL_TREE;
3039 /* Now give a warning for all base functions without overriders,
3040 as they are hidden. */
3041 size_t k;
3042 tree base_fndecl;
3043 FOR_EACH_VEC_ELT (base_fndecls, k, base_fndecl)
3044 if (base_fndecl)
3046 /* Here we know it is a hider, and no overrider exists. */
3047 warning_at (location_of (base_fndecl),
3048 OPT_Woverloaded_virtual,
3049 "%qD was hidden", base_fndecl);
3050 warning_at (location_of (fns),
3051 OPT_Woverloaded_virtual, " by %qD", fns);
3056 /* Recursive helper for finish_struct_anon. */
3058 static void
3059 finish_struct_anon_r (tree field, bool complain)
3061 bool is_union = TREE_CODE (TREE_TYPE (field)) == UNION_TYPE;
3062 tree elt = TYPE_FIELDS (TREE_TYPE (field));
3063 for (; elt; elt = DECL_CHAIN (elt))
3065 /* We're generally only interested in entities the user
3066 declared, but we also find nested classes by noticing
3067 the TYPE_DECL that we create implicitly. You're
3068 allowed to put one anonymous union inside another,
3069 though, so we explicitly tolerate that. We use
3070 TYPE_ANONYMOUS_P rather than ANON_AGGR_TYPE_P so that
3071 we also allow unnamed types used for defining fields. */
3072 if (DECL_ARTIFICIAL (elt)
3073 && (!DECL_IMPLICIT_TYPEDEF_P (elt)
3074 || TYPE_ANONYMOUS_P (TREE_TYPE (elt))))
3075 continue;
3077 if (TREE_CODE (elt) != FIELD_DECL)
3079 /* We already complained about static data members in
3080 finish_static_data_member_decl. */
3081 if (complain && !VAR_P (elt))
3083 if (is_union)
3084 permerror (DECL_SOURCE_LOCATION (elt),
3085 "%q#D invalid; an anonymous union can "
3086 "only have non-static data members", elt);
3087 else
3088 permerror (DECL_SOURCE_LOCATION (elt),
3089 "%q#D invalid; an anonymous struct can "
3090 "only have non-static data members", elt);
3092 continue;
3095 if (complain)
3097 if (TREE_PRIVATE (elt))
3099 if (is_union)
3100 permerror (DECL_SOURCE_LOCATION (elt),
3101 "private member %q#D in anonymous union", elt);
3102 else
3103 permerror (DECL_SOURCE_LOCATION (elt),
3104 "private member %q#D in anonymous struct", elt);
3106 else if (TREE_PROTECTED (elt))
3108 if (is_union)
3109 permerror (DECL_SOURCE_LOCATION (elt),
3110 "protected member %q#D in anonymous union", elt);
3111 else
3112 permerror (DECL_SOURCE_LOCATION (elt),
3113 "protected member %q#D in anonymous struct", elt);
3117 TREE_PRIVATE (elt) = TREE_PRIVATE (field);
3118 TREE_PROTECTED (elt) = TREE_PROTECTED (field);
3120 /* Recurse into the anonymous aggregates to handle correctly
3121 access control (c++/24926):
3123 class A {
3124 union {
3125 union {
3126 int i;
3131 int j=A().i; */
3132 if (DECL_NAME (elt) == NULL_TREE
3133 && ANON_AGGR_TYPE_P (TREE_TYPE (elt)))
3134 finish_struct_anon_r (elt, /*complain=*/false);
3138 /* Check for things that are invalid. There are probably plenty of other
3139 things we should check for also. */
3141 static void
3142 finish_struct_anon (tree t)
3144 for (tree field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
3146 if (TREE_STATIC (field))
3147 continue;
3148 if (TREE_CODE (field) != FIELD_DECL)
3149 continue;
3151 if (DECL_NAME (field) == NULL_TREE
3152 && ANON_AGGR_TYPE_P (TREE_TYPE (field)))
3153 finish_struct_anon_r (field, /*complain=*/true);
3157 /* Add T to CLASSTYPE_DECL_LIST of current_class_type which
3158 will be used later during class template instantiation.
3159 When FRIEND_P is zero, T can be a static member data (VAR_DECL),
3160 a non-static member data (FIELD_DECL), a member function
3161 (FUNCTION_DECL), a nested type (RECORD_TYPE, ENUM_TYPE),
3162 a typedef (TYPE_DECL) or a member class template (TEMPLATE_DECL)
3163 When FRIEND_P is nonzero, T is either a friend class
3164 (RECORD_TYPE, TEMPLATE_DECL) or a friend function
3165 (FUNCTION_DECL, TEMPLATE_DECL). */
3167 void
3168 maybe_add_class_template_decl_list (tree type, tree t, int friend_p)
3170 /* Save some memory by not creating TREE_LIST if TYPE is not template. */
3171 if (CLASSTYPE_TEMPLATE_INFO (type))
3172 CLASSTYPE_DECL_LIST (type)
3173 = tree_cons (friend_p ? NULL_TREE : type,
3174 t, CLASSTYPE_DECL_LIST (type));
3177 /* This function is called from declare_virt_assop_and_dtor via
3178 dfs_walk_all.
3180 DATA is a type that direcly or indirectly inherits the base
3181 represented by BINFO. If BINFO contains a virtual assignment [copy
3182 assignment or move assigment] operator or a virtual constructor,
3183 declare that function in DATA if it hasn't been already declared. */
3185 static tree
3186 dfs_declare_virt_assop_and_dtor (tree binfo, void *data)
3188 tree bv, fn, t = (tree)data;
3189 tree opname = ansi_assopname (NOP_EXPR);
3191 gcc_assert (t && CLASS_TYPE_P (t));
3192 gcc_assert (binfo && TREE_CODE (binfo) == TREE_BINFO);
3194 if (!TYPE_CONTAINS_VPTR_P (BINFO_TYPE (binfo)))
3195 /* A base without a vtable needs no modification, and its bases
3196 are uninteresting. */
3197 return dfs_skip_bases;
3199 if (BINFO_PRIMARY_P (binfo))
3200 /* If this is a primary base, then we have already looked at the
3201 virtual functions of its vtable. */
3202 return NULL_TREE;
3204 for (bv = BINFO_VIRTUALS (binfo); bv; bv = TREE_CHAIN (bv))
3206 fn = BV_FN (bv);
3208 if (DECL_NAME (fn) == opname)
3210 if (CLASSTYPE_LAZY_COPY_ASSIGN (t))
3211 lazily_declare_fn (sfk_copy_assignment, t);
3212 if (CLASSTYPE_LAZY_MOVE_ASSIGN (t))
3213 lazily_declare_fn (sfk_move_assignment, t);
3215 else if (DECL_DESTRUCTOR_P (fn)
3216 && CLASSTYPE_LAZY_DESTRUCTOR (t))
3217 lazily_declare_fn (sfk_destructor, t);
3220 return NULL_TREE;
3223 /* If the class type T has a direct or indirect base that contains a
3224 virtual assignment operator or a virtual destructor, declare that
3225 function in T if it hasn't been already declared. */
3227 static void
3228 declare_virt_assop_and_dtor (tree t)
3230 if (!(TYPE_POLYMORPHIC_P (t)
3231 && (CLASSTYPE_LAZY_COPY_ASSIGN (t)
3232 || CLASSTYPE_LAZY_MOVE_ASSIGN (t)
3233 || CLASSTYPE_LAZY_DESTRUCTOR (t))))
3234 return;
3236 dfs_walk_all (TYPE_BINFO (t),
3237 dfs_declare_virt_assop_and_dtor,
3238 NULL, t);
3241 /* Declare the inheriting constructor for class T inherited from base
3242 constructor CTOR with the parameter array PARMS of size NPARMS. */
3244 static void
3245 one_inheriting_sig (tree t, tree ctor, tree *parms, int nparms)
3247 /* We don't declare an inheriting ctor that would be a default,
3248 copy or move ctor for derived or base. */
3249 if (nparms == 0)
3250 return;
3251 if (nparms == 1
3252 && TREE_CODE (parms[0]) == REFERENCE_TYPE)
3254 tree parm = TYPE_MAIN_VARIANT (TREE_TYPE (parms[0]));
3255 if (parm == t || parm == DECL_CONTEXT (ctor))
3256 return;
3259 tree parmlist = void_list_node;
3260 for (int i = nparms - 1; i >= 0; i--)
3261 parmlist = tree_cons (NULL_TREE, parms[i], parmlist);
3262 tree fn = implicitly_declare_fn (sfk_inheriting_constructor,
3263 t, false, ctor, parmlist);
3264 gcc_assert (TYPE_MAIN_VARIANT (t) == t);
3265 if (add_method (t, fn, NULL_TREE))
3267 DECL_CHAIN (fn) = TYPE_METHODS (t);
3268 TYPE_METHODS (t) = fn;
3272 /* Declare all the inheriting constructors for class T inherited from base
3273 constructor CTOR. */
3275 static void
3276 one_inherited_ctor (tree ctor, tree t)
3278 tree parms = FUNCTION_FIRST_USER_PARMTYPE (ctor);
3280 tree *new_parms = XALLOCAVEC (tree, list_length (parms));
3281 int i = 0;
3282 for (; parms && parms != void_list_node; parms = TREE_CHAIN (parms))
3284 if (TREE_PURPOSE (parms))
3285 one_inheriting_sig (t, ctor, new_parms, i);
3286 new_parms[i++] = TREE_VALUE (parms);
3288 one_inheriting_sig (t, ctor, new_parms, i);
3289 if (parms == NULL_TREE)
3291 if (warning (OPT_Winherited_variadic_ctor,
3292 "the ellipsis in %qD is not inherited", ctor))
3293 inform (DECL_SOURCE_LOCATION (ctor), "%qD declared here", ctor);
3297 /* Create default constructors, assignment operators, and so forth for
3298 the type indicated by T, if they are needed. CANT_HAVE_CONST_CTOR,
3299 and CANT_HAVE_CONST_ASSIGNMENT are nonzero if, for whatever reason,
3300 the class cannot have a default constructor, copy constructor
3301 taking a const reference argument, or an assignment operator taking
3302 a const reference, respectively. */
3304 static void
3305 add_implicitly_declared_members (tree t, tree* access_decls,
3306 int cant_have_const_cctor,
3307 int cant_have_const_assignment)
3309 bool move_ok = false;
3311 if (cxx_dialect >= cxx11 && !CLASSTYPE_DESTRUCTORS (t)
3312 && !TYPE_HAS_COPY_CTOR (t) && !TYPE_HAS_COPY_ASSIGN (t)
3313 && !type_has_move_constructor (t) && !type_has_move_assign (t))
3314 move_ok = true;
3316 /* Destructor. */
3317 if (!CLASSTYPE_DESTRUCTORS (t))
3319 /* In general, we create destructors lazily. */
3320 CLASSTYPE_LAZY_DESTRUCTOR (t) = 1;
3322 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t)
3323 && TYPE_FOR_JAVA (t))
3324 /* But if this is a Java class, any non-trivial destructor is
3325 invalid, even if compiler-generated. Therefore, if the
3326 destructor is non-trivial we create it now. */
3327 lazily_declare_fn (sfk_destructor, t);
3330 /* [class.ctor]
3332 If there is no user-declared constructor for a class, a default
3333 constructor is implicitly declared. */
3334 if (! TYPE_HAS_USER_CONSTRUCTOR (t))
3336 TYPE_HAS_DEFAULT_CONSTRUCTOR (t) = 1;
3337 CLASSTYPE_LAZY_DEFAULT_CTOR (t) = 1;
3338 if (cxx_dialect >= cxx11)
3339 TYPE_HAS_CONSTEXPR_CTOR (t)
3340 /* This might force the declaration. */
3341 = type_has_constexpr_default_constructor (t);
3344 /* [class.ctor]
3346 If a class definition does not explicitly declare a copy
3347 constructor, one is declared implicitly. */
3348 if (! TYPE_HAS_COPY_CTOR (t) && ! TYPE_FOR_JAVA (t))
3350 TYPE_HAS_COPY_CTOR (t) = 1;
3351 TYPE_HAS_CONST_COPY_CTOR (t) = !cant_have_const_cctor;
3352 CLASSTYPE_LAZY_COPY_CTOR (t) = 1;
3353 if (move_ok)
3354 CLASSTYPE_LAZY_MOVE_CTOR (t) = 1;
3357 /* If there is no assignment operator, one will be created if and
3358 when it is needed. For now, just record whether or not the type
3359 of the parameter to the assignment operator will be a const or
3360 non-const reference. */
3361 if (!TYPE_HAS_COPY_ASSIGN (t) && !TYPE_FOR_JAVA (t))
3363 TYPE_HAS_COPY_ASSIGN (t) = 1;
3364 TYPE_HAS_CONST_COPY_ASSIGN (t) = !cant_have_const_assignment;
3365 CLASSTYPE_LAZY_COPY_ASSIGN (t) = 1;
3366 if (move_ok && !LAMBDA_TYPE_P (t))
3367 CLASSTYPE_LAZY_MOVE_ASSIGN (t) = 1;
3370 /* We can't be lazy about declaring functions that might override
3371 a virtual function from a base class. */
3372 declare_virt_assop_and_dtor (t);
3374 while (*access_decls)
3376 tree using_decl = TREE_VALUE (*access_decls);
3377 tree decl = USING_DECL_DECLS (using_decl);
3378 if (DECL_NAME (using_decl) == ctor_identifier)
3380 /* declare, then remove the decl */
3381 tree ctor_list = decl;
3382 location_t loc = input_location;
3383 input_location = DECL_SOURCE_LOCATION (using_decl);
3384 if (ctor_list)
3385 for (; ctor_list; ctor_list = OVL_NEXT (ctor_list))
3386 one_inherited_ctor (OVL_CURRENT (ctor_list), t);
3387 *access_decls = TREE_CHAIN (*access_decls);
3388 input_location = loc;
3390 else
3391 access_decls = &TREE_CHAIN (*access_decls);
3395 /* Subroutine of insert_into_classtype_sorted_fields. Recursively
3396 count the number of fields in TYPE, including anonymous union
3397 members. */
3399 static int
3400 count_fields (tree fields)
3402 tree x;
3403 int n_fields = 0;
3404 for (x = fields; x; x = DECL_CHAIN (x))
3406 if (TREE_CODE (x) == FIELD_DECL && ANON_AGGR_TYPE_P (TREE_TYPE (x)))
3407 n_fields += count_fields (TYPE_FIELDS (TREE_TYPE (x)));
3408 else
3409 n_fields += 1;
3411 return n_fields;
3414 /* Subroutine of insert_into_classtype_sorted_fields. Recursively add
3415 all the fields in the TREE_LIST FIELDS to the SORTED_FIELDS_TYPE
3416 elts, starting at offset IDX. */
3418 static int
3419 add_fields_to_record_type (tree fields, struct sorted_fields_type *field_vec, int idx)
3421 tree x;
3422 for (x = fields; x; x = DECL_CHAIN (x))
3424 if (TREE_CODE (x) == FIELD_DECL && ANON_AGGR_TYPE_P (TREE_TYPE (x)))
3425 idx = add_fields_to_record_type (TYPE_FIELDS (TREE_TYPE (x)), field_vec, idx);
3426 else
3427 field_vec->elts[idx++] = x;
3429 return idx;
3432 /* Add all of the enum values of ENUMTYPE, to the FIELD_VEC elts,
3433 starting at offset IDX. */
3435 static int
3436 add_enum_fields_to_record_type (tree enumtype,
3437 struct sorted_fields_type *field_vec,
3438 int idx)
3440 tree values;
3441 for (values = TYPE_VALUES (enumtype); values; values = TREE_CHAIN (values))
3442 field_vec->elts[idx++] = TREE_VALUE (values);
3443 return idx;
3446 /* FIELD is a bit-field. We are finishing the processing for its
3447 enclosing type. Issue any appropriate messages and set appropriate
3448 flags. Returns false if an error has been diagnosed. */
3450 static bool
3451 check_bitfield_decl (tree field)
3453 tree type = TREE_TYPE (field);
3454 tree w;
3456 /* Extract the declared width of the bitfield, which has been
3457 temporarily stashed in DECL_INITIAL. */
3458 w = DECL_INITIAL (field);
3459 gcc_assert (w != NULL_TREE);
3460 /* Remove the bit-field width indicator so that the rest of the
3461 compiler does not treat that value as an initializer. */
3462 DECL_INITIAL (field) = NULL_TREE;
3464 /* Detect invalid bit-field type. */
3465 if (!INTEGRAL_OR_ENUMERATION_TYPE_P (type))
3467 error ("bit-field %q+#D with non-integral type", field);
3468 w = error_mark_node;
3470 else
3472 location_t loc = input_location;
3473 /* Avoid the non_lvalue wrapper added by fold for PLUS_EXPRs. */
3474 STRIP_NOPS (w);
3476 /* detect invalid field size. */
3477 input_location = DECL_SOURCE_LOCATION (field);
3478 w = cxx_constant_value (w);
3479 input_location = loc;
3481 if (TREE_CODE (w) != INTEGER_CST)
3483 error ("bit-field %q+D width not an integer constant", field);
3484 w = error_mark_node;
3486 else if (tree_int_cst_sgn (w) < 0)
3488 error ("negative width in bit-field %q+D", field);
3489 w = error_mark_node;
3491 else if (integer_zerop (w) && DECL_NAME (field) != 0)
3493 error ("zero width for bit-field %q+D", field);
3494 w = error_mark_node;
3496 else if ((TREE_CODE (type) != ENUMERAL_TYPE
3497 && TREE_CODE (type) != BOOLEAN_TYPE
3498 && compare_tree_int (w, TYPE_PRECISION (type)) > 0)
3499 || ((TREE_CODE (type) == ENUMERAL_TYPE
3500 || TREE_CODE (type) == BOOLEAN_TYPE)
3501 && tree_int_cst_lt (TYPE_SIZE (type), w)))
3502 warning_at (DECL_SOURCE_LOCATION (field), 0,
3503 "width of %qD exceeds its type", field);
3504 else if (TREE_CODE (type) == ENUMERAL_TYPE
3505 && (0 > (compare_tree_int
3506 (w, TYPE_PRECISION (ENUM_UNDERLYING_TYPE (type))))))
3507 warning_at (DECL_SOURCE_LOCATION (field), 0,
3508 "%qD is too small to hold all values of %q#T",
3509 field, type);
3512 if (w != error_mark_node)
3514 DECL_SIZE (field) = fold_convert (bitsizetype, w);
3515 DECL_BIT_FIELD (field) = 1;
3516 return true;
3518 else
3520 /* Non-bit-fields are aligned for their type. */
3521 DECL_BIT_FIELD (field) = 0;
3522 CLEAR_DECL_C_BIT_FIELD (field);
3523 return false;
3527 /* FIELD is a non bit-field. We are finishing the processing for its
3528 enclosing type T. Issue any appropriate messages and set appropriate
3529 flags. */
3531 static void
3532 check_field_decl (tree field,
3533 tree t,
3534 int* cant_have_const_ctor,
3535 int* no_const_asn_ref,
3536 int* any_default_members)
3538 tree type = strip_array_types (TREE_TYPE (field));
3540 /* In C++98 an anonymous union cannot contain any fields which would change
3541 the settings of CANT_HAVE_CONST_CTOR and friends. */
3542 if (ANON_UNION_TYPE_P (type) && cxx_dialect < cxx11)
3544 /* And, we don't set TYPE_HAS_CONST_COPY_CTOR, etc., for anonymous
3545 structs. So, we recurse through their fields here. */
3546 else if (ANON_AGGR_TYPE_P (type))
3548 tree fields;
3550 for (fields = TYPE_FIELDS (type); fields; fields = DECL_CHAIN (fields))
3551 if (TREE_CODE (fields) == FIELD_DECL && !DECL_C_BIT_FIELD (field))
3552 check_field_decl (fields, t, cant_have_const_ctor,
3553 no_const_asn_ref, any_default_members);
3555 /* Check members with class type for constructors, destructors,
3556 etc. */
3557 else if (CLASS_TYPE_P (type))
3559 /* Never let anything with uninheritable virtuals
3560 make it through without complaint. */
3561 abstract_virtuals_error (field, type);
3563 if (TREE_CODE (t) == UNION_TYPE && cxx_dialect < cxx11)
3565 static bool warned;
3566 int oldcount = errorcount;
3567 if (TYPE_NEEDS_CONSTRUCTING (type))
3568 error ("member %q+#D with constructor not allowed in union",
3569 field);
3570 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
3571 error ("member %q+#D with destructor not allowed in union", field);
3572 if (TYPE_HAS_COMPLEX_COPY_ASSIGN (type))
3573 error ("member %q+#D with copy assignment operator not allowed in union",
3574 field);
3575 if (!warned && errorcount > oldcount)
3577 inform (DECL_SOURCE_LOCATION (field), "unrestricted unions "
3578 "only available with -std=c++11 or -std=gnu++11");
3579 warned = true;
3582 else
3584 TYPE_NEEDS_CONSTRUCTING (t) |= TYPE_NEEDS_CONSTRUCTING (type);
3585 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t)
3586 |= TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type);
3587 TYPE_HAS_COMPLEX_COPY_ASSIGN (t)
3588 |= (TYPE_HAS_COMPLEX_COPY_ASSIGN (type)
3589 || !TYPE_HAS_COPY_ASSIGN (type));
3590 TYPE_HAS_COMPLEX_COPY_CTOR (t) |= (TYPE_HAS_COMPLEX_COPY_CTOR (type)
3591 || !TYPE_HAS_COPY_CTOR (type));
3592 TYPE_HAS_COMPLEX_MOVE_ASSIGN (t) |= TYPE_HAS_COMPLEX_MOVE_ASSIGN (type);
3593 TYPE_HAS_COMPLEX_MOVE_CTOR (t) |= TYPE_HAS_COMPLEX_MOVE_CTOR (type);
3594 TYPE_HAS_COMPLEX_DFLT (t) |= (!TYPE_HAS_DEFAULT_CONSTRUCTOR (type)
3595 || TYPE_HAS_COMPLEX_DFLT (type));
3598 if (TYPE_HAS_COPY_CTOR (type)
3599 && !TYPE_HAS_CONST_COPY_CTOR (type))
3600 *cant_have_const_ctor = 1;
3602 if (TYPE_HAS_COPY_ASSIGN (type)
3603 && !TYPE_HAS_CONST_COPY_ASSIGN (type))
3604 *no_const_asn_ref = 1;
3607 check_abi_tags (t, field);
3609 if (DECL_INITIAL (field) != NULL_TREE)
3611 /* `build_class_init_list' does not recognize
3612 non-FIELD_DECLs. */
3613 if (TREE_CODE (t) == UNION_TYPE && *any_default_members != 0)
3614 error ("multiple fields in union %qT initialized", t);
3615 *any_default_members = 1;
3619 /* Check the data members (both static and non-static), class-scoped
3620 typedefs, etc., appearing in the declaration of T. Issue
3621 appropriate diagnostics. Sets ACCESS_DECLS to a list (in
3622 declaration order) of access declarations; each TREE_VALUE in this
3623 list is a USING_DECL.
3625 In addition, set the following flags:
3627 EMPTY_P
3628 The class is empty, i.e., contains no non-static data members.
3630 CANT_HAVE_CONST_CTOR_P
3631 This class cannot have an implicitly generated copy constructor
3632 taking a const reference.
3634 CANT_HAVE_CONST_ASN_REF
3635 This class cannot have an implicitly generated assignment
3636 operator taking a const reference.
3638 All of these flags should be initialized before calling this
3639 function.
3641 Returns a pointer to the end of the TYPE_FIELDs chain; additional
3642 fields can be added by adding to this chain. */
3644 static void
3645 check_field_decls (tree t, tree *access_decls,
3646 int *cant_have_const_ctor_p,
3647 int *no_const_asn_ref_p)
3649 tree *field;
3650 tree *next;
3651 bool has_pointers;
3652 int any_default_members;
3653 int cant_pack = 0;
3654 int field_access = -1;
3656 /* Assume there are no access declarations. */
3657 *access_decls = NULL_TREE;
3658 /* Assume this class has no pointer members. */
3659 has_pointers = false;
3660 /* Assume none of the members of this class have default
3661 initializations. */
3662 any_default_members = 0;
3664 for (field = &TYPE_FIELDS (t); *field; field = next)
3666 tree x = *field;
3667 tree type = TREE_TYPE (x);
3668 int this_field_access;
3670 next = &DECL_CHAIN (x);
3672 if (TREE_CODE (x) == USING_DECL)
3674 /* Save the access declarations for our caller. */
3675 *access_decls = tree_cons (NULL_TREE, x, *access_decls);
3676 continue;
3679 if (TREE_CODE (x) == TYPE_DECL
3680 || TREE_CODE (x) == TEMPLATE_DECL)
3681 continue;
3683 /* If we've gotten this far, it's a data member, possibly static,
3684 or an enumerator. */
3685 if (TREE_CODE (x) != CONST_DECL)
3686 DECL_CONTEXT (x) = t;
3688 /* When this goes into scope, it will be a non-local reference. */
3689 DECL_NONLOCAL (x) = 1;
3691 if (TREE_CODE (t) == UNION_TYPE
3692 && cxx_dialect < cxx11)
3694 /* [class.union] (C++98)
3696 If a union contains a static data member, or a member of
3697 reference type, the program is ill-formed.
3699 In C++11 this limitation doesn't exist anymore. */
3700 if (VAR_P (x))
3702 error ("in C++98 %q+D may not be static because it is "
3703 "a member of a union", x);
3704 continue;
3706 if (TREE_CODE (type) == REFERENCE_TYPE)
3708 error ("in C++98 %q+D may not have reference type %qT "
3709 "because it is a member of a union", x, type);
3710 continue;
3714 /* Perform error checking that did not get done in
3715 grokdeclarator. */
3716 if (TREE_CODE (type) == FUNCTION_TYPE)
3718 error ("field %q+D invalidly declared function type", x);
3719 type = build_pointer_type (type);
3720 TREE_TYPE (x) = type;
3722 else if (TREE_CODE (type) == METHOD_TYPE)
3724 error ("field %q+D invalidly declared method type", x);
3725 type = build_pointer_type (type);
3726 TREE_TYPE (x) = type;
3729 if (type == error_mark_node)
3730 continue;
3732 if (TREE_CODE (x) == CONST_DECL || VAR_P (x))
3733 continue;
3735 /* Now it can only be a FIELD_DECL. */
3737 if (TREE_PRIVATE (x) || TREE_PROTECTED (x))
3738 CLASSTYPE_NON_AGGREGATE (t) = 1;
3740 /* If at least one non-static data member is non-literal, the whole
3741 class becomes non-literal. Per Core/1453, volatile non-static
3742 data members and base classes are also not allowed.
3743 Note: if the type is incomplete we will complain later on. */
3744 if (COMPLETE_TYPE_P (type)
3745 && (!literal_type_p (type) || CP_TYPE_VOLATILE_P (type)))
3746 CLASSTYPE_LITERAL_P (t) = false;
3748 /* A standard-layout class is a class that:
3750 has the same access control (Clause 11) for all non-static data members,
3751 ... */
3752 this_field_access = TREE_PROTECTED (x) ? 1 : TREE_PRIVATE (x) ? 2 : 0;
3753 if (field_access == -1)
3754 field_access = this_field_access;
3755 else if (this_field_access != field_access)
3756 CLASSTYPE_NON_STD_LAYOUT (t) = 1;
3758 /* If this is of reference type, check if it needs an init. */
3759 if (TREE_CODE (type) == REFERENCE_TYPE)
3761 CLASSTYPE_NON_LAYOUT_POD_P (t) = 1;
3762 CLASSTYPE_NON_STD_LAYOUT (t) = 1;
3763 if (DECL_INITIAL (x) == NULL_TREE)
3764 SET_CLASSTYPE_REF_FIELDS_NEED_INIT (t, 1);
3765 if (cxx_dialect < cxx11)
3767 /* ARM $12.6.2: [A member initializer list] (or, for an
3768 aggregate, initialization by a brace-enclosed list) is the
3769 only way to initialize nonstatic const and reference
3770 members. */
3771 TYPE_HAS_COMPLEX_COPY_ASSIGN (t) = 1;
3772 TYPE_HAS_COMPLEX_MOVE_ASSIGN (t) = 1;
3776 type = strip_array_types (type);
3778 if (TYPE_PACKED (t))
3780 if (!layout_pod_type_p (type) && !TYPE_PACKED (type))
3782 warning_at
3783 (DECL_SOURCE_LOCATION (x), 0,
3784 "ignoring packed attribute because of unpacked non-POD field %q#D",
3786 cant_pack = 1;
3788 else if (DECL_C_BIT_FIELD (x)
3789 || TYPE_ALIGN (TREE_TYPE (x)) > BITS_PER_UNIT)
3790 DECL_PACKED (x) = 1;
3793 if (DECL_C_BIT_FIELD (x) && integer_zerop (DECL_INITIAL (x)))
3794 /* We don't treat zero-width bitfields as making a class
3795 non-empty. */
3797 else
3799 /* The class is non-empty. */
3800 CLASSTYPE_EMPTY_P (t) = 0;
3801 /* The class is not even nearly empty. */
3802 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
3803 /* If one of the data members contains an empty class,
3804 so does T. */
3805 if (CLASS_TYPE_P (type)
3806 && CLASSTYPE_CONTAINS_EMPTY_CLASS_P (type))
3807 CLASSTYPE_CONTAINS_EMPTY_CLASS_P (t) = 1;
3810 /* This is used by -Weffc++ (see below). Warn only for pointers
3811 to members which might hold dynamic memory. So do not warn
3812 for pointers to functions or pointers to members. */
3813 if (TYPE_PTR_P (type)
3814 && !TYPE_PTRFN_P (type))
3815 has_pointers = true;
3817 if (CLASS_TYPE_P (type))
3819 if (CLASSTYPE_REF_FIELDS_NEED_INIT (type))
3820 SET_CLASSTYPE_REF_FIELDS_NEED_INIT (t, 1);
3821 if (CLASSTYPE_READONLY_FIELDS_NEED_INIT (type))
3822 SET_CLASSTYPE_READONLY_FIELDS_NEED_INIT (t, 1);
3825 if (DECL_MUTABLE_P (x) || TYPE_HAS_MUTABLE_P (type))
3826 CLASSTYPE_HAS_MUTABLE (t) = 1;
3828 if (DECL_MUTABLE_P (x))
3830 if (CP_TYPE_CONST_P (type))
3832 error ("member %q+D cannot be declared both %<const%> "
3833 "and %<mutable%>", x);
3834 continue;
3836 if (TREE_CODE (type) == REFERENCE_TYPE)
3838 error ("member %q+D cannot be declared as a %<mutable%> "
3839 "reference", x);
3840 continue;
3844 if (! layout_pod_type_p (type))
3845 /* DR 148 now allows pointers to members (which are POD themselves),
3846 to be allowed in POD structs. */
3847 CLASSTYPE_NON_LAYOUT_POD_P (t) = 1;
3849 if (!std_layout_type_p (type))
3850 CLASSTYPE_NON_STD_LAYOUT (t) = 1;
3852 if (! zero_init_p (type))
3853 CLASSTYPE_NON_ZERO_INIT_P (t) = 1;
3855 /* We set DECL_C_BIT_FIELD in grokbitfield.
3856 If the type and width are valid, we'll also set DECL_BIT_FIELD. */
3857 if (! DECL_C_BIT_FIELD (x) || ! check_bitfield_decl (x))
3858 check_field_decl (x, t,
3859 cant_have_const_ctor_p,
3860 no_const_asn_ref_p,
3861 &any_default_members);
3863 /* Now that we've removed bit-field widths from DECL_INITIAL,
3864 anything left in DECL_INITIAL is an NSDMI that makes the class
3865 non-aggregate in C++11. */
3866 if (DECL_INITIAL (x) && cxx_dialect < cxx14)
3867 CLASSTYPE_NON_AGGREGATE (t) = true;
3869 /* If any field is const, the structure type is pseudo-const. */
3870 if (CP_TYPE_CONST_P (type))
3872 C_TYPE_FIELDS_READONLY (t) = 1;
3873 if (DECL_INITIAL (x) == NULL_TREE)
3874 SET_CLASSTYPE_READONLY_FIELDS_NEED_INIT (t, 1);
3875 if (cxx_dialect < cxx11)
3877 /* ARM $12.6.2: [A member initializer list] (or, for an
3878 aggregate, initialization by a brace-enclosed list) is the
3879 only way to initialize nonstatic const and reference
3880 members. */
3881 TYPE_HAS_COMPLEX_COPY_ASSIGN (t) = 1;
3882 TYPE_HAS_COMPLEX_MOVE_ASSIGN (t) = 1;
3885 /* A field that is pseudo-const makes the structure likewise. */
3886 else if (CLASS_TYPE_P (type))
3888 C_TYPE_FIELDS_READONLY (t) |= C_TYPE_FIELDS_READONLY (type);
3889 SET_CLASSTYPE_READONLY_FIELDS_NEED_INIT (t,
3890 CLASSTYPE_READONLY_FIELDS_NEED_INIT (t)
3891 | CLASSTYPE_READONLY_FIELDS_NEED_INIT (type));
3894 /* Core issue 80: A nonstatic data member is required to have a
3895 different name from the class iff the class has a
3896 user-declared constructor. */
3897 if (constructor_name_p (DECL_NAME (x), t)
3898 && TYPE_HAS_USER_CONSTRUCTOR (t))
3899 permerror (DECL_SOURCE_LOCATION (x),
3900 "field %q#D with same name as class", x);
3903 /* Effective C++ rule 11: if a class has dynamic memory held by pointers,
3904 it should also define a copy constructor and an assignment operator to
3905 implement the correct copy semantic (deep vs shallow, etc.). As it is
3906 not feasible to check whether the constructors do allocate dynamic memory
3907 and store it within members, we approximate the warning like this:
3909 -- Warn only if there are members which are pointers
3910 -- Warn only if there is a non-trivial constructor (otherwise,
3911 there cannot be memory allocated).
3912 -- Warn only if there is a non-trivial destructor. We assume that the
3913 user at least implemented the cleanup correctly, and a destructor
3914 is needed to free dynamic memory.
3916 This seems enough for practical purposes. */
3917 if (warn_ecpp
3918 && has_pointers
3919 && TYPE_HAS_USER_CONSTRUCTOR (t)
3920 && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t)
3921 && !(TYPE_HAS_COPY_CTOR (t) && TYPE_HAS_COPY_ASSIGN (t)))
3923 warning (OPT_Weffc__, "%q#T has pointer data members", t);
3925 if (! TYPE_HAS_COPY_CTOR (t))
3927 warning (OPT_Weffc__,
3928 " but does not override %<%T(const %T&)%>", t, t);
3929 if (!TYPE_HAS_COPY_ASSIGN (t))
3930 warning (OPT_Weffc__, " or %<operator=(const %T&)%>", t);
3932 else if (! TYPE_HAS_COPY_ASSIGN (t))
3933 warning (OPT_Weffc__,
3934 " but does not override %<operator=(const %T&)%>", t);
3937 /* Non-static data member initializers make the default constructor
3938 non-trivial. */
3939 if (any_default_members)
3941 TYPE_NEEDS_CONSTRUCTING (t) = true;
3942 TYPE_HAS_COMPLEX_DFLT (t) = true;
3945 /* If any of the fields couldn't be packed, unset TYPE_PACKED. */
3946 if (cant_pack)
3947 TYPE_PACKED (t) = 0;
3949 /* Check anonymous struct/anonymous union fields. */
3950 finish_struct_anon (t);
3952 /* We've built up the list of access declarations in reverse order.
3953 Fix that now. */
3954 *access_decls = nreverse (*access_decls);
3957 /* If TYPE is an empty class type, records its OFFSET in the table of
3958 OFFSETS. */
3960 static int
3961 record_subobject_offset (tree type, tree offset, splay_tree offsets)
3963 splay_tree_node n;
3965 if (!is_empty_class (type))
3966 return 0;
3968 /* Record the location of this empty object in OFFSETS. */
3969 n = splay_tree_lookup (offsets, (splay_tree_key) offset);
3970 if (!n)
3971 n = splay_tree_insert (offsets,
3972 (splay_tree_key) offset,
3973 (splay_tree_value) NULL_TREE);
3974 n->value = ((splay_tree_value)
3975 tree_cons (NULL_TREE,
3976 type,
3977 (tree) n->value));
3979 return 0;
3982 /* Returns nonzero if TYPE is an empty class type and there is
3983 already an entry in OFFSETS for the same TYPE as the same OFFSET. */
3985 static int
3986 check_subobject_offset (tree type, tree offset, splay_tree offsets)
3988 splay_tree_node n;
3989 tree t;
3991 if (!is_empty_class (type))
3992 return 0;
3994 /* Record the location of this empty object in OFFSETS. */
3995 n = splay_tree_lookup (offsets, (splay_tree_key) offset);
3996 if (!n)
3997 return 0;
3999 for (t = (tree) n->value; t; t = TREE_CHAIN (t))
4000 if (same_type_p (TREE_VALUE (t), type))
4001 return 1;
4003 return 0;
4006 /* Walk through all the subobjects of TYPE (located at OFFSET). Call
4007 F for every subobject, passing it the type, offset, and table of
4008 OFFSETS. If VBASES_P is one, then virtual non-primary bases should
4009 be traversed.
4011 If MAX_OFFSET is non-NULL, then subobjects with an offset greater
4012 than MAX_OFFSET will not be walked.
4014 If F returns a nonzero value, the traversal ceases, and that value
4015 is returned. Otherwise, returns zero. */
4017 static int
4018 walk_subobject_offsets (tree type,
4019 subobject_offset_fn f,
4020 tree offset,
4021 splay_tree offsets,
4022 tree max_offset,
4023 int vbases_p)
4025 int r = 0;
4026 tree type_binfo = NULL_TREE;
4028 /* If this OFFSET is bigger than the MAX_OFFSET, then we should
4029 stop. */
4030 if (max_offset && tree_int_cst_lt (max_offset, offset))
4031 return 0;
4033 if (type == error_mark_node)
4034 return 0;
4036 if (!TYPE_P (type))
4038 type_binfo = type;
4039 type = BINFO_TYPE (type);
4042 if (CLASS_TYPE_P (type))
4044 tree field;
4045 tree binfo;
4046 int i;
4048 /* Avoid recursing into objects that are not interesting. */
4049 if (!CLASSTYPE_CONTAINS_EMPTY_CLASS_P (type))
4050 return 0;
4052 /* Record the location of TYPE. */
4053 r = (*f) (type, offset, offsets);
4054 if (r)
4055 return r;
4057 /* Iterate through the direct base classes of TYPE. */
4058 if (!type_binfo)
4059 type_binfo = TYPE_BINFO (type);
4060 for (i = 0; BINFO_BASE_ITERATE (type_binfo, i, binfo); i++)
4062 tree binfo_offset;
4064 if (BINFO_VIRTUAL_P (binfo))
4065 continue;
4067 tree orig_binfo;
4068 /* We cannot rely on BINFO_OFFSET being set for the base
4069 class yet, but the offsets for direct non-virtual
4070 bases can be calculated by going back to the TYPE. */
4071 orig_binfo = BINFO_BASE_BINFO (TYPE_BINFO (type), i);
4072 binfo_offset = size_binop (PLUS_EXPR,
4073 offset,
4074 BINFO_OFFSET (orig_binfo));
4076 r = walk_subobject_offsets (binfo,
4078 binfo_offset,
4079 offsets,
4080 max_offset,
4081 /*vbases_p=*/0);
4082 if (r)
4083 return r;
4086 if (CLASSTYPE_VBASECLASSES (type))
4088 unsigned ix;
4089 vec<tree, va_gc> *vbases;
4091 /* Iterate through the virtual base classes of TYPE. In G++
4092 3.2, we included virtual bases in the direct base class
4093 loop above, which results in incorrect results; the
4094 correct offsets for virtual bases are only known when
4095 working with the most derived type. */
4096 if (vbases_p)
4097 for (vbases = CLASSTYPE_VBASECLASSES (type), ix = 0;
4098 vec_safe_iterate (vbases, ix, &binfo); ix++)
4100 r = walk_subobject_offsets (binfo,
4102 size_binop (PLUS_EXPR,
4103 offset,
4104 BINFO_OFFSET (binfo)),
4105 offsets,
4106 max_offset,
4107 /*vbases_p=*/0);
4108 if (r)
4109 return r;
4111 else
4113 /* We still have to walk the primary base, if it is
4114 virtual. (If it is non-virtual, then it was walked
4115 above.) */
4116 tree vbase = get_primary_binfo (type_binfo);
4118 if (vbase && BINFO_VIRTUAL_P (vbase)
4119 && BINFO_PRIMARY_P (vbase)
4120 && BINFO_INHERITANCE_CHAIN (vbase) == type_binfo)
4122 r = (walk_subobject_offsets
4123 (vbase, f, offset,
4124 offsets, max_offset, /*vbases_p=*/0));
4125 if (r)
4126 return r;
4131 /* Iterate through the fields of TYPE. */
4132 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
4133 if (TREE_CODE (field) == FIELD_DECL
4134 && TREE_TYPE (field) != error_mark_node
4135 && !DECL_ARTIFICIAL (field))
4137 tree field_offset;
4139 field_offset = byte_position (field);
4141 r = walk_subobject_offsets (TREE_TYPE (field),
4143 size_binop (PLUS_EXPR,
4144 offset,
4145 field_offset),
4146 offsets,
4147 max_offset,
4148 /*vbases_p=*/1);
4149 if (r)
4150 return r;
4153 else if (TREE_CODE (type) == ARRAY_TYPE)
4155 tree element_type = strip_array_types (type);
4156 tree domain = TYPE_DOMAIN (type);
4157 tree index;
4159 /* Avoid recursing into objects that are not interesting. */
4160 if (!CLASS_TYPE_P (element_type)
4161 || !CLASSTYPE_CONTAINS_EMPTY_CLASS_P (element_type)
4162 || !domain)
4163 return 0;
4165 /* Step through each of the elements in the array. */
4166 for (index = size_zero_node;
4167 !tree_int_cst_lt (TYPE_MAX_VALUE (domain), index);
4168 index = size_binop (PLUS_EXPR, index, size_one_node))
4170 r = walk_subobject_offsets (TREE_TYPE (type),
4172 offset,
4173 offsets,
4174 max_offset,
4175 /*vbases_p=*/1);
4176 if (r)
4177 return r;
4178 offset = size_binop (PLUS_EXPR, offset,
4179 TYPE_SIZE_UNIT (TREE_TYPE (type)));
4180 /* If this new OFFSET is bigger than the MAX_OFFSET, then
4181 there's no point in iterating through the remaining
4182 elements of the array. */
4183 if (max_offset && tree_int_cst_lt (max_offset, offset))
4184 break;
4188 return 0;
4191 /* Record all of the empty subobjects of TYPE (either a type or a
4192 binfo). If IS_DATA_MEMBER is true, then a non-static data member
4193 is being placed at OFFSET; otherwise, it is a base class that is
4194 being placed at OFFSET. */
4196 static void
4197 record_subobject_offsets (tree type,
4198 tree offset,
4199 splay_tree offsets,
4200 bool is_data_member)
4202 tree max_offset;
4203 /* If recording subobjects for a non-static data member or a
4204 non-empty base class , we do not need to record offsets beyond
4205 the size of the biggest empty class. Additional data members
4206 will go at the end of the class. Additional base classes will go
4207 either at offset zero (if empty, in which case they cannot
4208 overlap with offsets past the size of the biggest empty class) or
4209 at the end of the class.
4211 However, if we are placing an empty base class, then we must record
4212 all offsets, as either the empty class is at offset zero (where
4213 other empty classes might later be placed) or at the end of the
4214 class (where other objects might then be placed, so other empty
4215 subobjects might later overlap). */
4216 if (is_data_member
4217 || !is_empty_class (BINFO_TYPE (type)))
4218 max_offset = sizeof_biggest_empty_class;
4219 else
4220 max_offset = NULL_TREE;
4221 walk_subobject_offsets (type, record_subobject_offset, offset,
4222 offsets, max_offset, is_data_member);
4225 /* Returns nonzero if any of the empty subobjects of TYPE (located at
4226 OFFSET) conflict with entries in OFFSETS. If VBASES_P is nonzero,
4227 virtual bases of TYPE are examined. */
4229 static int
4230 layout_conflict_p (tree type,
4231 tree offset,
4232 splay_tree offsets,
4233 int vbases_p)
4235 splay_tree_node max_node;
4237 /* Get the node in OFFSETS that indicates the maximum offset where
4238 an empty subobject is located. */
4239 max_node = splay_tree_max (offsets);
4240 /* If there aren't any empty subobjects, then there's no point in
4241 performing this check. */
4242 if (!max_node)
4243 return 0;
4245 return walk_subobject_offsets (type, check_subobject_offset, offset,
4246 offsets, (tree) (max_node->key),
4247 vbases_p);
4250 /* DECL is a FIELD_DECL corresponding either to a base subobject of a
4251 non-static data member of the type indicated by RLI. BINFO is the
4252 binfo corresponding to the base subobject, OFFSETS maps offsets to
4253 types already located at those offsets. This function determines
4254 the position of the DECL. */
4256 static void
4257 layout_nonempty_base_or_field (record_layout_info rli,
4258 tree decl,
4259 tree binfo,
4260 splay_tree offsets)
4262 tree offset = NULL_TREE;
4263 bool field_p;
4264 tree type;
4266 if (binfo)
4268 /* For the purposes of determining layout conflicts, we want to
4269 use the class type of BINFO; TREE_TYPE (DECL) will be the
4270 CLASSTYPE_AS_BASE version, which does not contain entries for
4271 zero-sized bases. */
4272 type = TREE_TYPE (binfo);
4273 field_p = false;
4275 else
4277 type = TREE_TYPE (decl);
4278 field_p = true;
4281 /* Try to place the field. It may take more than one try if we have
4282 a hard time placing the field without putting two objects of the
4283 same type at the same address. */
4284 while (1)
4286 struct record_layout_info_s old_rli = *rli;
4288 /* Place this field. */
4289 place_field (rli, decl);
4290 offset = byte_position (decl);
4292 /* We have to check to see whether or not there is already
4293 something of the same type at the offset we're about to use.
4294 For example, consider:
4296 struct S {};
4297 struct T : public S { int i; };
4298 struct U : public S, public T {};
4300 Here, we put S at offset zero in U. Then, we can't put T at
4301 offset zero -- its S component would be at the same address
4302 as the S we already allocated. So, we have to skip ahead.
4303 Since all data members, including those whose type is an
4304 empty class, have nonzero size, any overlap can happen only
4305 with a direct or indirect base-class -- it can't happen with
4306 a data member. */
4307 /* In a union, overlap is permitted; all members are placed at
4308 offset zero. */
4309 if (TREE_CODE (rli->t) == UNION_TYPE)
4310 break;
4311 if (layout_conflict_p (field_p ? type : binfo, offset,
4312 offsets, field_p))
4314 /* Strip off the size allocated to this field. That puts us
4315 at the first place we could have put the field with
4316 proper alignment. */
4317 *rli = old_rli;
4319 /* Bump up by the alignment required for the type. */
4320 rli->bitpos
4321 = size_binop (PLUS_EXPR, rli->bitpos,
4322 bitsize_int (binfo
4323 ? CLASSTYPE_ALIGN (type)
4324 : TYPE_ALIGN (type)));
4325 normalize_rli (rli);
4327 else if (TREE_CODE (type) == NULLPTR_TYPE
4328 && warn_abi && abi_version_crosses (9))
4330 /* Before ABI v9, we were giving nullptr_t alignment of 1; if
4331 the offset wasn't aligned like a pointer when we started to
4332 layout this field, that affects its position. */
4333 tree pos = rli_size_unit_so_far (&old_rli);
4334 if (int_cst_value (pos) % TYPE_ALIGN_UNIT (ptr_type_node) != 0)
4336 if (abi_version_at_least (9))
4337 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wabi,
4338 "alignment of %qD increased in -fabi-version=9 "
4339 "(GCC 5.2)", decl);
4340 else
4341 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wabi, "alignment "
4342 "of %qD will increase in -fabi-version=9", decl);
4344 break;
4346 else
4347 /* There was no conflict. We're done laying out this field. */
4348 break;
4351 /* Now that we know where it will be placed, update its
4352 BINFO_OFFSET. */
4353 if (binfo && CLASS_TYPE_P (BINFO_TYPE (binfo)))
4354 /* Indirect virtual bases may have a nonzero BINFO_OFFSET at
4355 this point because their BINFO_OFFSET is copied from another
4356 hierarchy. Therefore, we may not need to add the entire
4357 OFFSET. */
4358 propagate_binfo_offsets (binfo,
4359 size_diffop_loc (input_location,
4360 fold_convert (ssizetype, offset),
4361 fold_convert (ssizetype,
4362 BINFO_OFFSET (binfo))));
4365 /* Returns true if TYPE is empty and OFFSET is nonzero. */
4367 static int
4368 empty_base_at_nonzero_offset_p (tree type,
4369 tree offset,
4370 splay_tree /*offsets*/)
4372 return is_empty_class (type) && !integer_zerop (offset);
4375 /* Layout the empty base BINFO. EOC indicates the byte currently just
4376 past the end of the class, and should be correctly aligned for a
4377 class of the type indicated by BINFO; OFFSETS gives the offsets of
4378 the empty bases allocated so far. T is the most derived
4379 type. Return nonzero iff we added it at the end. */
4381 static bool
4382 layout_empty_base (record_layout_info rli, tree binfo,
4383 tree eoc, splay_tree offsets)
4385 tree alignment;
4386 tree basetype = BINFO_TYPE (binfo);
4387 bool atend = false;
4389 /* This routine should only be used for empty classes. */
4390 gcc_assert (is_empty_class (basetype));
4391 alignment = ssize_int (CLASSTYPE_ALIGN_UNIT (basetype));
4393 if (!integer_zerop (BINFO_OFFSET (binfo)))
4394 propagate_binfo_offsets
4395 (binfo, size_diffop_loc (input_location,
4396 size_zero_node, BINFO_OFFSET (binfo)));
4398 /* This is an empty base class. We first try to put it at offset
4399 zero. */
4400 if (layout_conflict_p (binfo,
4401 BINFO_OFFSET (binfo),
4402 offsets,
4403 /*vbases_p=*/0))
4405 /* That didn't work. Now, we move forward from the next
4406 available spot in the class. */
4407 atend = true;
4408 propagate_binfo_offsets (binfo, fold_convert (ssizetype, eoc));
4409 while (1)
4411 if (!layout_conflict_p (binfo,
4412 BINFO_OFFSET (binfo),
4413 offsets,
4414 /*vbases_p=*/0))
4415 /* We finally found a spot where there's no overlap. */
4416 break;
4418 /* There's overlap here, too. Bump along to the next spot. */
4419 propagate_binfo_offsets (binfo, alignment);
4423 if (CLASSTYPE_USER_ALIGN (basetype))
4425 rli->record_align = MAX (rli->record_align, CLASSTYPE_ALIGN (basetype));
4426 if (warn_packed)
4427 rli->unpacked_align = MAX (rli->unpacked_align, CLASSTYPE_ALIGN (basetype));
4428 TYPE_USER_ALIGN (rli->t) = 1;
4431 return atend;
4434 /* Layout the base given by BINFO in the class indicated by RLI.
4435 *BASE_ALIGN is a running maximum of the alignments of
4436 any base class. OFFSETS gives the location of empty base
4437 subobjects. T is the most derived type. Return nonzero if the new
4438 object cannot be nearly-empty. A new FIELD_DECL is inserted at
4439 *NEXT_FIELD, unless BINFO is for an empty base class.
4441 Returns the location at which the next field should be inserted. */
4443 static tree *
4444 build_base_field (record_layout_info rli, tree binfo,
4445 splay_tree offsets, tree *next_field)
4447 tree t = rli->t;
4448 tree basetype = BINFO_TYPE (binfo);
4450 if (!COMPLETE_TYPE_P (basetype))
4451 /* This error is now reported in xref_tag, thus giving better
4452 location information. */
4453 return next_field;
4455 /* Place the base class. */
4456 if (!is_empty_class (basetype))
4458 tree decl;
4460 /* The containing class is non-empty because it has a non-empty
4461 base class. */
4462 CLASSTYPE_EMPTY_P (t) = 0;
4464 /* Create the FIELD_DECL. */
4465 decl = build_decl (input_location,
4466 FIELD_DECL, NULL_TREE, CLASSTYPE_AS_BASE (basetype));
4467 DECL_ARTIFICIAL (decl) = 1;
4468 DECL_IGNORED_P (decl) = 1;
4469 DECL_FIELD_CONTEXT (decl) = t;
4470 if (CLASSTYPE_AS_BASE (basetype))
4472 DECL_SIZE (decl) = CLASSTYPE_SIZE (basetype);
4473 DECL_SIZE_UNIT (decl) = CLASSTYPE_SIZE_UNIT (basetype);
4474 DECL_ALIGN (decl) = CLASSTYPE_ALIGN (basetype);
4475 DECL_USER_ALIGN (decl) = CLASSTYPE_USER_ALIGN (basetype);
4476 DECL_MODE (decl) = TYPE_MODE (basetype);
4477 DECL_FIELD_IS_BASE (decl) = 1;
4479 /* Try to place the field. It may take more than one try if we
4480 have a hard time placing the field without putting two
4481 objects of the same type at the same address. */
4482 layout_nonempty_base_or_field (rli, decl, binfo, offsets);
4483 /* Add the new FIELD_DECL to the list of fields for T. */
4484 DECL_CHAIN (decl) = *next_field;
4485 *next_field = decl;
4486 next_field = &DECL_CHAIN (decl);
4489 else
4491 tree eoc;
4492 bool atend;
4494 /* On some platforms (ARM), even empty classes will not be
4495 byte-aligned. */
4496 eoc = round_up_loc (input_location,
4497 rli_size_unit_so_far (rli),
4498 CLASSTYPE_ALIGN_UNIT (basetype));
4499 atend = layout_empty_base (rli, binfo, eoc, offsets);
4500 /* A nearly-empty class "has no proper base class that is empty,
4501 not morally virtual, and at an offset other than zero." */
4502 if (!BINFO_VIRTUAL_P (binfo) && CLASSTYPE_NEARLY_EMPTY_P (t))
4504 if (atend)
4505 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
4506 /* The check above (used in G++ 3.2) is insufficient because
4507 an empty class placed at offset zero might itself have an
4508 empty base at a nonzero offset. */
4509 else if (walk_subobject_offsets (basetype,
4510 empty_base_at_nonzero_offset_p,
4511 size_zero_node,
4512 /*offsets=*/NULL,
4513 /*max_offset=*/NULL_TREE,
4514 /*vbases_p=*/true))
4515 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
4518 /* We do not create a FIELD_DECL for empty base classes because
4519 it might overlap some other field. We want to be able to
4520 create CONSTRUCTORs for the class by iterating over the
4521 FIELD_DECLs, and the back end does not handle overlapping
4522 FIELD_DECLs. */
4524 /* An empty virtual base causes a class to be non-empty
4525 -- but in that case we do not need to clear CLASSTYPE_EMPTY_P
4526 here because that was already done when the virtual table
4527 pointer was created. */
4530 /* Record the offsets of BINFO and its base subobjects. */
4531 record_subobject_offsets (binfo,
4532 BINFO_OFFSET (binfo),
4533 offsets,
4534 /*is_data_member=*/false);
4536 return next_field;
4539 /* Layout all of the non-virtual base classes. Record empty
4540 subobjects in OFFSETS. T is the most derived type. Return nonzero
4541 if the type cannot be nearly empty. The fields created
4542 corresponding to the base classes will be inserted at
4543 *NEXT_FIELD. */
4545 static void
4546 build_base_fields (record_layout_info rli,
4547 splay_tree offsets, tree *next_field)
4549 /* Chain to hold all the new FIELD_DECLs which stand in for base class
4550 subobjects. */
4551 tree t = rli->t;
4552 int n_baseclasses = BINFO_N_BASE_BINFOS (TYPE_BINFO (t));
4553 int i;
4555 /* The primary base class is always allocated first. */
4556 if (CLASSTYPE_HAS_PRIMARY_BASE_P (t))
4557 next_field = build_base_field (rli, CLASSTYPE_PRIMARY_BINFO (t),
4558 offsets, next_field);
4560 /* Now allocate the rest of the bases. */
4561 for (i = 0; i < n_baseclasses; ++i)
4563 tree base_binfo;
4565 base_binfo = BINFO_BASE_BINFO (TYPE_BINFO (t), i);
4567 /* The primary base was already allocated above, so we don't
4568 need to allocate it again here. */
4569 if (base_binfo == CLASSTYPE_PRIMARY_BINFO (t))
4570 continue;
4572 /* Virtual bases are added at the end (a primary virtual base
4573 will have already been added). */
4574 if (BINFO_VIRTUAL_P (base_binfo))
4575 continue;
4577 next_field = build_base_field (rli, base_binfo,
4578 offsets, next_field);
4582 /* Go through the TYPE_METHODS of T issuing any appropriate
4583 diagnostics, figuring out which methods override which other
4584 methods, and so forth. */
4586 static void
4587 check_methods (tree t)
4589 tree x;
4591 for (x = TYPE_METHODS (t); x; x = DECL_CHAIN (x))
4593 check_for_override (x, t);
4594 if (DECL_PURE_VIRTUAL_P (x) && (TREE_CODE (x) != FUNCTION_DECL || ! DECL_VINDEX (x)))
4595 error ("initializer specified for non-virtual method %q+D", x);
4596 /* The name of the field is the original field name
4597 Save this in auxiliary field for later overloading. */
4598 if (TREE_CODE (x) == FUNCTION_DECL && DECL_VINDEX (x))
4600 TYPE_POLYMORPHIC_P (t) = 1;
4601 if (DECL_PURE_VIRTUAL_P (x))
4602 vec_safe_push (CLASSTYPE_PURE_VIRTUALS (t), x);
4604 /* All user-provided destructors are non-trivial.
4605 Constructors and assignment ops are handled in
4606 grok_special_member_properties. */
4607 if (DECL_DESTRUCTOR_P (x) && user_provided_p (x))
4608 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t) = 1;
4609 if (!DECL_VIRTUAL_P (x)
4610 && lookup_attribute ("transaction_safe_dynamic", DECL_ATTRIBUTES (x)))
4611 error_at (DECL_SOURCE_LOCATION (x),
4612 "%<transaction_safe_dynamic%> may only be specified for "
4613 "a virtual function");
4617 /* FN is a constructor or destructor. Clone the declaration to create
4618 a specialized in-charge or not-in-charge version, as indicated by
4619 NAME. */
4621 static tree
4622 build_clone (tree fn, tree name)
4624 tree parms;
4625 tree clone;
4627 /* Copy the function. */
4628 clone = copy_decl (fn);
4629 /* Reset the function name. */
4630 DECL_NAME (clone) = name;
4631 /* Remember where this function came from. */
4632 DECL_ABSTRACT_ORIGIN (clone) = fn;
4633 /* Make it easy to find the CLONE given the FN. */
4634 DECL_CHAIN (clone) = DECL_CHAIN (fn);
4635 DECL_CHAIN (fn) = clone;
4637 /* If this is a template, do the rest on the DECL_TEMPLATE_RESULT. */
4638 if (TREE_CODE (clone) == TEMPLATE_DECL)
4640 tree result = build_clone (DECL_TEMPLATE_RESULT (clone), name);
4641 DECL_TEMPLATE_RESULT (clone) = result;
4642 DECL_TEMPLATE_INFO (result) = copy_node (DECL_TEMPLATE_INFO (result));
4643 DECL_TI_TEMPLATE (result) = clone;
4644 TREE_TYPE (clone) = TREE_TYPE (result);
4645 return clone;
4647 else
4649 // Clone constraints.
4650 if (flag_concepts)
4651 if (tree ci = get_constraints (fn))
4652 set_constraints (clone, copy_node (ci));
4656 SET_DECL_ASSEMBLER_NAME (clone, NULL_TREE);
4657 DECL_CLONED_FUNCTION (clone) = fn;
4658 /* There's no pending inline data for this function. */
4659 DECL_PENDING_INLINE_INFO (clone) = NULL;
4660 DECL_PENDING_INLINE_P (clone) = 0;
4662 /* The base-class destructor is not virtual. */
4663 if (name == base_dtor_identifier)
4665 DECL_VIRTUAL_P (clone) = 0;
4666 if (TREE_CODE (clone) != TEMPLATE_DECL)
4667 DECL_VINDEX (clone) = NULL_TREE;
4670 /* If there was an in-charge parameter, drop it from the function
4671 type. */
4672 if (DECL_HAS_IN_CHARGE_PARM_P (clone))
4674 tree basetype;
4675 tree parmtypes;
4676 tree exceptions;
4678 exceptions = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (clone));
4679 basetype = TYPE_METHOD_BASETYPE (TREE_TYPE (clone));
4680 parmtypes = TYPE_ARG_TYPES (TREE_TYPE (clone));
4681 /* Skip the `this' parameter. */
4682 parmtypes = TREE_CHAIN (parmtypes);
4683 /* Skip the in-charge parameter. */
4684 parmtypes = TREE_CHAIN (parmtypes);
4685 /* And the VTT parm, in a complete [cd]tor. */
4686 if (DECL_HAS_VTT_PARM_P (fn)
4687 && ! DECL_NEEDS_VTT_PARM_P (clone))
4688 parmtypes = TREE_CHAIN (parmtypes);
4689 /* If this is subobject constructor or destructor, add the vtt
4690 parameter. */
4691 TREE_TYPE (clone)
4692 = build_method_type_directly (basetype,
4693 TREE_TYPE (TREE_TYPE (clone)),
4694 parmtypes);
4695 if (exceptions)
4696 TREE_TYPE (clone) = build_exception_variant (TREE_TYPE (clone),
4697 exceptions);
4698 TREE_TYPE (clone)
4699 = cp_build_type_attribute_variant (TREE_TYPE (clone),
4700 TYPE_ATTRIBUTES (TREE_TYPE (fn)));
4703 /* Copy the function parameters. */
4704 DECL_ARGUMENTS (clone) = copy_list (DECL_ARGUMENTS (clone));
4705 /* Remove the in-charge parameter. */
4706 if (DECL_HAS_IN_CHARGE_PARM_P (clone))
4708 DECL_CHAIN (DECL_ARGUMENTS (clone))
4709 = DECL_CHAIN (DECL_CHAIN (DECL_ARGUMENTS (clone)));
4710 DECL_HAS_IN_CHARGE_PARM_P (clone) = 0;
4712 /* And the VTT parm, in a complete [cd]tor. */
4713 if (DECL_HAS_VTT_PARM_P (fn))
4715 if (DECL_NEEDS_VTT_PARM_P (clone))
4716 DECL_HAS_VTT_PARM_P (clone) = 1;
4717 else
4719 DECL_CHAIN (DECL_ARGUMENTS (clone))
4720 = DECL_CHAIN (DECL_CHAIN (DECL_ARGUMENTS (clone)));
4721 DECL_HAS_VTT_PARM_P (clone) = 0;
4725 for (parms = DECL_ARGUMENTS (clone); parms; parms = DECL_CHAIN (parms))
4727 DECL_CONTEXT (parms) = clone;
4728 cxx_dup_lang_specific_decl (parms);
4731 /* Create the RTL for this function. */
4732 SET_DECL_RTL (clone, NULL);
4733 rest_of_decl_compilation (clone, /*top_level=*/1, at_eof);
4735 return clone;
4738 /* Implementation of DECL_CLONED_FUNCTION and DECL_CLONED_FUNCTION_P, do
4739 not invoke this function directly.
4741 For a non-thunk function, returns the address of the slot for storing
4742 the function it is a clone of. Otherwise returns NULL_TREE.
4744 If JUST_TESTING, looks through TEMPLATE_DECL and returns NULL if
4745 cloned_function is unset. This is to support the separate
4746 DECL_CLONED_FUNCTION and DECL_CLONED_FUNCTION_P modes; using the latter
4747 on a template makes sense, but not the former. */
4749 tree *
4750 decl_cloned_function_p (const_tree decl, bool just_testing)
4752 tree *ptr;
4753 if (just_testing)
4754 decl = STRIP_TEMPLATE (decl);
4756 if (TREE_CODE (decl) != FUNCTION_DECL
4757 || !DECL_LANG_SPECIFIC (decl)
4758 || DECL_LANG_SPECIFIC (decl)->u.fn.thunk_p)
4760 #if defined ENABLE_TREE_CHECKING && (GCC_VERSION >= 2007)
4761 if (!just_testing)
4762 lang_check_failed (__FILE__, __LINE__, __FUNCTION__);
4763 else
4764 #endif
4765 return NULL;
4768 ptr = &DECL_LANG_SPECIFIC (decl)->u.fn.u5.cloned_function;
4769 if (just_testing && *ptr == NULL_TREE)
4770 return NULL;
4771 else
4772 return ptr;
4775 /* Produce declarations for all appropriate clones of FN. If
4776 UPDATE_METHOD_VEC_P is nonzero, the clones are added to the
4777 CLASTYPE_METHOD_VEC as well. */
4779 void
4780 clone_function_decl (tree fn, int update_method_vec_p)
4782 tree clone;
4784 /* Avoid inappropriate cloning. */
4785 if (DECL_CHAIN (fn)
4786 && DECL_CLONED_FUNCTION_P (DECL_CHAIN (fn)))
4787 return;
4789 if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (fn))
4791 /* For each constructor, we need two variants: an in-charge version
4792 and a not-in-charge version. */
4793 clone = build_clone (fn, complete_ctor_identifier);
4794 if (update_method_vec_p)
4795 add_method (DECL_CONTEXT (clone), clone, NULL_TREE);
4796 clone = build_clone (fn, base_ctor_identifier);
4797 if (update_method_vec_p)
4798 add_method (DECL_CONTEXT (clone), clone, NULL_TREE);
4800 else
4802 gcc_assert (DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (fn));
4804 /* For each destructor, we need three variants: an in-charge
4805 version, a not-in-charge version, and an in-charge deleting
4806 version. We clone the deleting version first because that
4807 means it will go second on the TYPE_METHODS list -- and that
4808 corresponds to the correct layout order in the virtual
4809 function table.
4811 For a non-virtual destructor, we do not build a deleting
4812 destructor. */
4813 if (DECL_VIRTUAL_P (fn))
4815 clone = build_clone (fn, deleting_dtor_identifier);
4816 if (update_method_vec_p)
4817 add_method (DECL_CONTEXT (clone), clone, NULL_TREE);
4819 clone = build_clone (fn, complete_dtor_identifier);
4820 if (update_method_vec_p)
4821 add_method (DECL_CONTEXT (clone), clone, NULL_TREE);
4822 clone = build_clone (fn, base_dtor_identifier);
4823 if (update_method_vec_p)
4824 add_method (DECL_CONTEXT (clone), clone, NULL_TREE);
4827 /* Note that this is an abstract function that is never emitted. */
4828 DECL_ABSTRACT_P (fn) = true;
4831 /* DECL is an in charge constructor, which is being defined. This will
4832 have had an in class declaration, from whence clones were
4833 declared. An out-of-class definition can specify additional default
4834 arguments. As it is the clones that are involved in overload
4835 resolution, we must propagate the information from the DECL to its
4836 clones. */
4838 void
4839 adjust_clone_args (tree decl)
4841 tree clone;
4843 for (clone = DECL_CHAIN (decl); clone && DECL_CLONED_FUNCTION_P (clone);
4844 clone = DECL_CHAIN (clone))
4846 tree orig_clone_parms = TYPE_ARG_TYPES (TREE_TYPE (clone));
4847 tree orig_decl_parms = TYPE_ARG_TYPES (TREE_TYPE (decl));
4848 tree decl_parms, clone_parms;
4850 clone_parms = orig_clone_parms;
4852 /* Skip the 'this' parameter. */
4853 orig_clone_parms = TREE_CHAIN (orig_clone_parms);
4854 orig_decl_parms = TREE_CHAIN (orig_decl_parms);
4856 if (DECL_HAS_IN_CHARGE_PARM_P (decl))
4857 orig_decl_parms = TREE_CHAIN (orig_decl_parms);
4858 if (DECL_HAS_VTT_PARM_P (decl))
4859 orig_decl_parms = TREE_CHAIN (orig_decl_parms);
4861 clone_parms = orig_clone_parms;
4862 if (DECL_HAS_VTT_PARM_P (clone))
4863 clone_parms = TREE_CHAIN (clone_parms);
4865 for (decl_parms = orig_decl_parms; decl_parms;
4866 decl_parms = TREE_CHAIN (decl_parms),
4867 clone_parms = TREE_CHAIN (clone_parms))
4869 gcc_assert (same_type_p (TREE_TYPE (decl_parms),
4870 TREE_TYPE (clone_parms)));
4872 if (TREE_PURPOSE (decl_parms) && !TREE_PURPOSE (clone_parms))
4874 /* A default parameter has been added. Adjust the
4875 clone's parameters. */
4876 tree exceptions = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (clone));
4877 tree attrs = TYPE_ATTRIBUTES (TREE_TYPE (clone));
4878 tree basetype = TYPE_METHOD_BASETYPE (TREE_TYPE (clone));
4879 tree type;
4881 clone_parms = orig_decl_parms;
4883 if (DECL_HAS_VTT_PARM_P (clone))
4885 clone_parms = tree_cons (TREE_PURPOSE (orig_clone_parms),
4886 TREE_VALUE (orig_clone_parms),
4887 clone_parms);
4888 TREE_TYPE (clone_parms) = TREE_TYPE (orig_clone_parms);
4890 type = build_method_type_directly (basetype,
4891 TREE_TYPE (TREE_TYPE (clone)),
4892 clone_parms);
4893 if (exceptions)
4894 type = build_exception_variant (type, exceptions);
4895 if (attrs)
4896 type = cp_build_type_attribute_variant (type, attrs);
4897 TREE_TYPE (clone) = type;
4899 clone_parms = NULL_TREE;
4900 break;
4903 gcc_assert (!clone_parms);
4907 /* For each of the constructors and destructors in T, create an
4908 in-charge and not-in-charge variant. */
4910 static void
4911 clone_constructors_and_destructors (tree t)
4913 tree fns;
4915 /* If for some reason we don't have a CLASSTYPE_METHOD_VEC, we bail
4916 out now. */
4917 if (!CLASSTYPE_METHOD_VEC (t))
4918 return;
4920 for (fns = CLASSTYPE_CONSTRUCTORS (t); fns; fns = OVL_NEXT (fns))
4921 clone_function_decl (OVL_CURRENT (fns), /*update_method_vec_p=*/1);
4922 for (fns = CLASSTYPE_DESTRUCTORS (t); fns; fns = OVL_NEXT (fns))
4923 clone_function_decl (OVL_CURRENT (fns), /*update_method_vec_p=*/1);
4926 /* Deduce noexcept for a destructor DTOR. */
4928 void
4929 deduce_noexcept_on_destructor (tree dtor)
4931 if (!TYPE_RAISES_EXCEPTIONS (TREE_TYPE (dtor)))
4933 tree eh_spec = unevaluated_noexcept_spec ();
4934 TREE_TYPE (dtor) = build_exception_variant (TREE_TYPE (dtor), eh_spec);
4938 /* For each destructor in T, deduce noexcept:
4940 12.4/3: A declaration of a destructor that does not have an
4941 exception-specification is implicitly considered to have the
4942 same exception-specification as an implicit declaration (15.4). */
4944 static void
4945 deduce_noexcept_on_destructors (tree t)
4947 /* If for some reason we don't have a CLASSTYPE_METHOD_VEC, we bail
4948 out now. */
4949 if (!CLASSTYPE_METHOD_VEC (t))
4950 return;
4952 for (tree fns = CLASSTYPE_DESTRUCTORS (t); fns; fns = OVL_NEXT (fns))
4953 deduce_noexcept_on_destructor (OVL_CURRENT (fns));
4956 /* Subroutine of set_one_vmethod_tm_attributes. Search base classes
4957 of TYPE for virtual functions which FNDECL overrides. Return a
4958 mask of the tm attributes found therein. */
4960 static int
4961 look_for_tm_attr_overrides (tree type, tree fndecl)
4963 tree binfo = TYPE_BINFO (type);
4964 tree base_binfo;
4965 int ix, found = 0;
4967 for (ix = 0; BINFO_BASE_ITERATE (binfo, ix, base_binfo); ++ix)
4969 tree o, basetype = BINFO_TYPE (base_binfo);
4971 if (!TYPE_POLYMORPHIC_P (basetype))
4972 continue;
4974 o = look_for_overrides_here (basetype, fndecl);
4975 if (o)
4977 if (lookup_attribute ("transaction_safe_dynamic",
4978 DECL_ATTRIBUTES (o)))
4979 /* transaction_safe_dynamic is not inherited. */;
4980 else
4981 found |= tm_attr_to_mask (find_tm_attribute
4982 (TYPE_ATTRIBUTES (TREE_TYPE (o))));
4984 else
4985 found |= look_for_tm_attr_overrides (basetype, fndecl);
4988 return found;
4991 /* Subroutine of set_method_tm_attributes. Handle the checks and
4992 inheritance for one virtual method FNDECL. */
4994 static void
4995 set_one_vmethod_tm_attributes (tree type, tree fndecl)
4997 tree tm_attr;
4998 int found, have;
5000 found = look_for_tm_attr_overrides (type, fndecl);
5002 /* If FNDECL doesn't actually override anything (i.e. T is the
5003 class that first declares FNDECL virtual), then we're done. */
5004 if (found == 0)
5005 return;
5007 tm_attr = find_tm_attribute (TYPE_ATTRIBUTES (TREE_TYPE (fndecl)));
5008 have = tm_attr_to_mask (tm_attr);
5010 /* Intel STM Language Extension 3.0, Section 4.2 table 4:
5011 tm_pure must match exactly, otherwise no weakening of
5012 tm_safe > tm_callable > nothing. */
5013 /* ??? The tm_pure attribute didn't make the transition to the
5014 multivendor language spec. */
5015 if (have == TM_ATTR_PURE)
5017 if (found != TM_ATTR_PURE)
5019 found &= -found;
5020 goto err_override;
5023 /* If the overridden function is tm_pure, then FNDECL must be. */
5024 else if (found == TM_ATTR_PURE && tm_attr)
5025 goto err_override;
5026 /* Look for base class combinations that cannot be satisfied. */
5027 else if (found != TM_ATTR_PURE && (found & TM_ATTR_PURE))
5029 found &= ~TM_ATTR_PURE;
5030 found &= -found;
5031 error_at (DECL_SOURCE_LOCATION (fndecl),
5032 "method overrides both %<transaction_pure%> and %qE methods",
5033 tm_mask_to_attr (found));
5035 /* If FNDECL did not declare an attribute, then inherit the most
5036 restrictive one. */
5037 else if (tm_attr == NULL)
5039 apply_tm_attr (fndecl, tm_mask_to_attr (found & -found));
5041 /* Otherwise validate that we're not weaker than a function
5042 that is being overridden. */
5043 else
5045 found &= -found;
5046 if (found <= TM_ATTR_CALLABLE && have > found)
5047 goto err_override;
5049 return;
5051 err_override:
5052 error_at (DECL_SOURCE_LOCATION (fndecl),
5053 "method declared %qE overriding %qE method",
5054 tm_attr, tm_mask_to_attr (found));
5057 /* For each of the methods in T, propagate a class-level tm attribute. */
5059 static void
5060 set_method_tm_attributes (tree t)
5062 tree class_tm_attr, fndecl;
5064 /* Don't bother collecting tm attributes if transactional memory
5065 support is not enabled. */
5066 if (!flag_tm)
5067 return;
5069 /* Process virtual methods first, as they inherit directly from the
5070 base virtual function and also require validation of new attributes. */
5071 if (TYPE_CONTAINS_VPTR_P (t))
5073 tree vchain;
5074 for (vchain = BINFO_VIRTUALS (TYPE_BINFO (t)); vchain;
5075 vchain = TREE_CHAIN (vchain))
5077 fndecl = BV_FN (vchain);
5078 if (DECL_THUNK_P (fndecl))
5079 fndecl = THUNK_TARGET (fndecl);
5080 set_one_vmethod_tm_attributes (t, fndecl);
5084 /* If the class doesn't have an attribute, nothing more to do. */
5085 class_tm_attr = find_tm_attribute (TYPE_ATTRIBUTES (t));
5086 if (class_tm_attr == NULL)
5087 return;
5089 /* Any method that does not yet have a tm attribute inherits
5090 the one from the class. */
5091 for (fndecl = TYPE_METHODS (t); fndecl; fndecl = TREE_CHAIN (fndecl))
5093 if (!find_tm_attribute (TYPE_ATTRIBUTES (TREE_TYPE (fndecl))))
5094 apply_tm_attr (fndecl, class_tm_attr);
5098 /* Returns true iff class T has a user-defined constructor other than
5099 the default constructor. */
5101 bool
5102 type_has_user_nondefault_constructor (tree t)
5104 tree fns;
5106 if (!TYPE_HAS_USER_CONSTRUCTOR (t))
5107 return false;
5109 for (fns = CLASSTYPE_CONSTRUCTORS (t); fns; fns = OVL_NEXT (fns))
5111 tree fn = OVL_CURRENT (fns);
5112 if (!DECL_ARTIFICIAL (fn)
5113 && (TREE_CODE (fn) == TEMPLATE_DECL
5114 || (skip_artificial_parms_for (fn, DECL_ARGUMENTS (fn))
5115 != NULL_TREE)))
5116 return true;
5119 return false;
5122 /* Returns the defaulted constructor if T has one. Otherwise, returns
5123 NULL_TREE. */
5125 tree
5126 in_class_defaulted_default_constructor (tree t)
5128 tree fns, args;
5130 if (!TYPE_HAS_USER_CONSTRUCTOR (t))
5131 return NULL_TREE;
5133 for (fns = CLASSTYPE_CONSTRUCTORS (t); fns; fns = OVL_NEXT (fns))
5135 tree fn = OVL_CURRENT (fns);
5137 if (DECL_DEFAULTED_IN_CLASS_P (fn))
5139 args = FUNCTION_FIRST_USER_PARMTYPE (fn);
5140 while (args && TREE_PURPOSE (args))
5141 args = TREE_CHAIN (args);
5142 if (!args || args == void_list_node)
5143 return fn;
5147 return NULL_TREE;
5150 /* Returns true iff FN is a user-provided function, i.e. user-declared
5151 and not defaulted at its first declaration; or explicit, private,
5152 protected, or non-const. */
5154 bool
5155 user_provided_p (tree fn)
5157 if (TREE_CODE (fn) == TEMPLATE_DECL)
5158 return true;
5159 else
5160 return (!DECL_ARTIFICIAL (fn)
5161 && !(DECL_INITIALIZED_IN_CLASS_P (fn)
5162 && (DECL_DEFAULTED_FN (fn) || DECL_DELETED_FN (fn))));
5165 /* Returns true iff class T has a user-provided constructor. */
5167 bool
5168 type_has_user_provided_constructor (tree t)
5170 tree fns;
5172 if (!CLASS_TYPE_P (t))
5173 return false;
5175 if (!TYPE_HAS_USER_CONSTRUCTOR (t))
5176 return false;
5178 /* This can happen in error cases; avoid crashing. */
5179 if (!CLASSTYPE_METHOD_VEC (t))
5180 return false;
5182 for (fns = CLASSTYPE_CONSTRUCTORS (t); fns; fns = OVL_NEXT (fns))
5183 if (user_provided_p (OVL_CURRENT (fns)))
5184 return true;
5186 return false;
5189 /* Returns true iff class T has a user-provided or explicit constructor. */
5191 bool
5192 type_has_user_provided_or_explicit_constructor (tree t)
5194 tree fns;
5196 if (!CLASS_TYPE_P (t))
5197 return false;
5199 if (!TYPE_HAS_USER_CONSTRUCTOR (t))
5200 return false;
5202 /* This can happen in error cases; avoid crashing. */
5203 if (!CLASSTYPE_METHOD_VEC (t))
5204 return false;
5206 for (fns = CLASSTYPE_CONSTRUCTORS (t); fns; fns = OVL_NEXT (fns))
5208 tree fn = OVL_CURRENT (fns);
5209 if (user_provided_p (fn) || DECL_NONCONVERTING_P (fn))
5210 return true;
5213 return false;
5216 /* Returns true iff class T has a non-user-provided (i.e. implicitly
5217 declared or explicitly defaulted in the class body) default
5218 constructor. */
5220 bool
5221 type_has_non_user_provided_default_constructor (tree t)
5223 tree fns;
5225 if (!TYPE_HAS_DEFAULT_CONSTRUCTOR (t))
5226 return false;
5227 if (CLASSTYPE_LAZY_DEFAULT_CTOR (t))
5228 return true;
5230 for (fns = CLASSTYPE_CONSTRUCTORS (t); fns; fns = OVL_NEXT (fns))
5232 tree fn = OVL_CURRENT (fns);
5233 if (TREE_CODE (fn) == FUNCTION_DECL
5234 && !user_provided_p (fn)
5235 && sufficient_parms_p (FUNCTION_FIRST_USER_PARMTYPE (fn)))
5236 return true;
5239 return false;
5242 /* TYPE is being used as a virtual base, and has a non-trivial move
5243 assignment. Return true if this is due to there being a user-provided
5244 move assignment in TYPE or one of its subobjects; if there isn't, then
5245 multiple move assignment can't cause any harm. */
5247 bool
5248 vbase_has_user_provided_move_assign (tree type)
5250 /* Does the type itself have a user-provided move assignment operator? */
5251 for (tree fns
5252 = lookup_fnfields_slot_nolazy (type, ansi_assopname (NOP_EXPR));
5253 fns; fns = OVL_NEXT (fns))
5255 tree fn = OVL_CURRENT (fns);
5256 if (move_fn_p (fn) && user_provided_p (fn))
5257 return true;
5260 /* Do any of its bases? */
5261 tree binfo = TYPE_BINFO (type);
5262 tree base_binfo;
5263 for (int i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
5264 if (vbase_has_user_provided_move_assign (BINFO_TYPE (base_binfo)))
5265 return true;
5267 /* Or non-static data members? */
5268 for (tree field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
5270 if (TREE_CODE (field) == FIELD_DECL
5271 && CLASS_TYPE_P (TREE_TYPE (field))
5272 && vbase_has_user_provided_move_assign (TREE_TYPE (field)))
5273 return true;
5276 /* Seems not. */
5277 return false;
5280 /* If default-initialization leaves part of TYPE uninitialized, returns
5281 a DECL for the field or TYPE itself (DR 253). */
5283 tree
5284 default_init_uninitialized_part (tree type)
5286 tree t, r, binfo;
5287 int i;
5289 type = strip_array_types (type);
5290 if (!CLASS_TYPE_P (type))
5291 return type;
5292 if (!type_has_non_user_provided_default_constructor (type))
5293 return NULL_TREE;
5294 for (binfo = TYPE_BINFO (type), i = 0;
5295 BINFO_BASE_ITERATE (binfo, i, t); ++i)
5297 r = default_init_uninitialized_part (BINFO_TYPE (t));
5298 if (r)
5299 return r;
5301 for (t = TYPE_FIELDS (type); t; t = DECL_CHAIN (t))
5302 if (TREE_CODE (t) == FIELD_DECL
5303 && !DECL_ARTIFICIAL (t)
5304 && !DECL_INITIAL (t))
5306 r = default_init_uninitialized_part (TREE_TYPE (t));
5307 if (r)
5308 return DECL_P (r) ? r : t;
5311 return NULL_TREE;
5314 /* Returns true iff for class T, a trivial synthesized default constructor
5315 would be constexpr. */
5317 bool
5318 trivial_default_constructor_is_constexpr (tree t)
5320 /* A defaulted trivial default constructor is constexpr
5321 if there is nothing to initialize. */
5322 gcc_assert (!TYPE_HAS_COMPLEX_DFLT (t));
5323 return is_really_empty_class (t);
5326 /* Returns true iff class T has a constexpr default constructor. */
5328 bool
5329 type_has_constexpr_default_constructor (tree t)
5331 tree fns;
5333 if (!CLASS_TYPE_P (t))
5335 /* The caller should have stripped an enclosing array. */
5336 gcc_assert (TREE_CODE (t) != ARRAY_TYPE);
5337 return false;
5339 if (CLASSTYPE_LAZY_DEFAULT_CTOR (t))
5341 if (!TYPE_HAS_COMPLEX_DFLT (t))
5342 return trivial_default_constructor_is_constexpr (t);
5343 /* Non-trivial, we need to check subobject constructors. */
5344 lazily_declare_fn (sfk_constructor, t);
5346 fns = locate_ctor (t);
5347 return (fns && DECL_DECLARED_CONSTEXPR_P (fns));
5350 /* Returns true iff class TYPE has a virtual destructor. */
5352 bool
5353 type_has_virtual_destructor (tree type)
5355 tree dtor;
5357 if (!CLASS_TYPE_P (type))
5358 return false;
5360 gcc_assert (COMPLETE_TYPE_P (type));
5361 dtor = CLASSTYPE_DESTRUCTORS (type);
5362 return (dtor && DECL_VIRTUAL_P (dtor));
5365 /* Returns true iff class T has a move constructor. */
5367 bool
5368 type_has_move_constructor (tree t)
5370 tree fns;
5372 if (CLASSTYPE_LAZY_MOVE_CTOR (t))
5374 gcc_assert (COMPLETE_TYPE_P (t));
5375 lazily_declare_fn (sfk_move_constructor, t);
5378 if (!CLASSTYPE_METHOD_VEC (t))
5379 return false;
5381 for (fns = CLASSTYPE_CONSTRUCTORS (t); fns; fns = OVL_NEXT (fns))
5382 if (move_fn_p (OVL_CURRENT (fns)))
5383 return true;
5385 return false;
5388 /* Returns true iff class T has a move assignment operator. */
5390 bool
5391 type_has_move_assign (tree t)
5393 tree fns;
5395 if (CLASSTYPE_LAZY_MOVE_ASSIGN (t))
5397 gcc_assert (COMPLETE_TYPE_P (t));
5398 lazily_declare_fn (sfk_move_assignment, t);
5401 for (fns = lookup_fnfields_slot_nolazy (t, ansi_assopname (NOP_EXPR));
5402 fns; fns = OVL_NEXT (fns))
5403 if (move_fn_p (OVL_CURRENT (fns)))
5404 return true;
5406 return false;
5409 /* Returns true iff class T has a move constructor that was explicitly
5410 declared in the class body. Note that this is different from
5411 "user-provided", which doesn't include functions that are defaulted in
5412 the class. */
5414 bool
5415 type_has_user_declared_move_constructor (tree t)
5417 tree fns;
5419 if (CLASSTYPE_LAZY_MOVE_CTOR (t))
5420 return false;
5422 if (!CLASSTYPE_METHOD_VEC (t))
5423 return false;
5425 for (fns = CLASSTYPE_CONSTRUCTORS (t); fns; fns = OVL_NEXT (fns))
5427 tree fn = OVL_CURRENT (fns);
5428 if (move_fn_p (fn) && !DECL_ARTIFICIAL (fn))
5429 return true;
5432 return false;
5435 /* Returns true iff class T has a move assignment operator that was
5436 explicitly declared in the class body. */
5438 bool
5439 type_has_user_declared_move_assign (tree t)
5441 tree fns;
5443 if (CLASSTYPE_LAZY_MOVE_ASSIGN (t))
5444 return false;
5446 for (fns = lookup_fnfields_slot_nolazy (t, ansi_assopname (NOP_EXPR));
5447 fns; fns = OVL_NEXT (fns))
5449 tree fn = OVL_CURRENT (fns);
5450 if (move_fn_p (fn) && !DECL_ARTIFICIAL (fn))
5451 return true;
5454 return false;
5457 /* Nonzero if we need to build up a constructor call when initializing an
5458 object of this class, either because it has a user-declared constructor
5459 or because it doesn't have a default constructor (so we need to give an
5460 error if no initializer is provided). Use TYPE_NEEDS_CONSTRUCTING when
5461 what you care about is whether or not an object can be produced by a
5462 constructor (e.g. so we don't set TREE_READONLY on const variables of
5463 such type); use this function when what you care about is whether or not
5464 to try to call a constructor to create an object. The latter case is
5465 the former plus some cases of constructors that cannot be called. */
5467 bool
5468 type_build_ctor_call (tree t)
5470 tree inner;
5471 if (TYPE_NEEDS_CONSTRUCTING (t))
5472 return true;
5473 inner = strip_array_types (t);
5474 if (!CLASS_TYPE_P (inner) || ANON_AGGR_TYPE_P (inner))
5475 return false;
5476 if (!TYPE_HAS_DEFAULT_CONSTRUCTOR (inner))
5477 return true;
5478 if (cxx_dialect < cxx11)
5479 return false;
5480 /* A user-declared constructor might be private, and a constructor might
5481 be trivial but deleted. */
5482 for (tree fns = lookup_fnfields_slot (inner, complete_ctor_identifier);
5483 fns; fns = OVL_NEXT (fns))
5485 tree fn = OVL_CURRENT (fns);
5486 if (!DECL_ARTIFICIAL (fn)
5487 || DECL_DELETED_FN (fn))
5488 return true;
5490 return false;
5493 /* Like type_build_ctor_call, but for destructors. */
5495 bool
5496 type_build_dtor_call (tree t)
5498 tree inner;
5499 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t))
5500 return true;
5501 inner = strip_array_types (t);
5502 if (!CLASS_TYPE_P (inner) || ANON_AGGR_TYPE_P (inner)
5503 || !COMPLETE_TYPE_P (inner))
5504 return false;
5505 if (cxx_dialect < cxx11)
5506 return false;
5507 /* A user-declared destructor might be private, and a destructor might
5508 be trivial but deleted. */
5509 for (tree fns = lookup_fnfields_slot (inner, complete_dtor_identifier);
5510 fns; fns = OVL_NEXT (fns))
5512 tree fn = OVL_CURRENT (fns);
5513 if (!DECL_ARTIFICIAL (fn)
5514 || DECL_DELETED_FN (fn))
5515 return true;
5517 return false;
5520 /* Remove all zero-width bit-fields from T. */
5522 static void
5523 remove_zero_width_bit_fields (tree t)
5525 tree *fieldsp;
5527 fieldsp = &TYPE_FIELDS (t);
5528 while (*fieldsp)
5530 if (TREE_CODE (*fieldsp) == FIELD_DECL
5531 && DECL_C_BIT_FIELD (*fieldsp)
5532 /* We should not be confused by the fact that grokbitfield
5533 temporarily sets the width of the bit field into
5534 DECL_INITIAL (*fieldsp).
5535 check_bitfield_decl eventually sets DECL_SIZE (*fieldsp)
5536 to that width. */
5537 && (DECL_SIZE (*fieldsp) == NULL_TREE
5538 || integer_zerop (DECL_SIZE (*fieldsp))))
5539 *fieldsp = DECL_CHAIN (*fieldsp);
5540 else
5541 fieldsp = &DECL_CHAIN (*fieldsp);
5545 /* Returns TRUE iff we need a cookie when dynamically allocating an
5546 array whose elements have the indicated class TYPE. */
5548 static bool
5549 type_requires_array_cookie (tree type)
5551 tree fns;
5552 bool has_two_argument_delete_p = false;
5554 gcc_assert (CLASS_TYPE_P (type));
5556 /* If there's a non-trivial destructor, we need a cookie. In order
5557 to iterate through the array calling the destructor for each
5558 element, we'll have to know how many elements there are. */
5559 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
5560 return true;
5562 /* If the usual deallocation function is a two-argument whose second
5563 argument is of type `size_t', then we have to pass the size of
5564 the array to the deallocation function, so we will need to store
5565 a cookie. */
5566 fns = lookup_fnfields (TYPE_BINFO (type),
5567 ansi_opname (VEC_DELETE_EXPR),
5568 /*protect=*/0);
5569 /* If there are no `operator []' members, or the lookup is
5570 ambiguous, then we don't need a cookie. */
5571 if (!fns || fns == error_mark_node)
5572 return false;
5573 /* Loop through all of the functions. */
5574 for (fns = BASELINK_FUNCTIONS (fns); fns; fns = OVL_NEXT (fns))
5576 tree fn;
5577 tree second_parm;
5579 /* Select the current function. */
5580 fn = OVL_CURRENT (fns);
5581 /* See if this function is a one-argument delete function. If
5582 it is, then it will be the usual deallocation function. */
5583 second_parm = TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (fn)));
5584 if (second_parm == void_list_node)
5585 return false;
5586 /* Do not consider this function if its second argument is an
5587 ellipsis. */
5588 if (!second_parm)
5589 continue;
5590 /* Otherwise, if we have a two-argument function and the second
5591 argument is `size_t', it will be the usual deallocation
5592 function -- unless there is one-argument function, too. */
5593 if (TREE_CHAIN (second_parm) == void_list_node
5594 && same_type_p (TREE_VALUE (second_parm), size_type_node))
5595 has_two_argument_delete_p = true;
5598 return has_two_argument_delete_p;
5601 /* Finish computing the `literal type' property of class type T.
5603 At this point, we have already processed base classes and
5604 non-static data members. We need to check whether the copy
5605 constructor is trivial, the destructor is trivial, and there
5606 is a trivial default constructor or at least one constexpr
5607 constructor other than the copy constructor. */
5609 static void
5610 finalize_literal_type_property (tree t)
5612 tree fn;
5614 if (cxx_dialect < cxx11
5615 || TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t))
5616 CLASSTYPE_LITERAL_P (t) = false;
5617 else if (CLASSTYPE_LITERAL_P (t) && !TYPE_HAS_TRIVIAL_DFLT (t)
5618 && CLASSTYPE_NON_AGGREGATE (t)
5619 && !TYPE_HAS_CONSTEXPR_CTOR (t))
5620 CLASSTYPE_LITERAL_P (t) = false;
5622 if (!CLASSTYPE_LITERAL_P (t))
5623 for (fn = TYPE_METHODS (t); fn; fn = DECL_CHAIN (fn))
5624 if (DECL_DECLARED_CONSTEXPR_P (fn)
5625 && TREE_CODE (fn) != TEMPLATE_DECL
5626 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
5627 && !DECL_CONSTRUCTOR_P (fn))
5629 DECL_DECLARED_CONSTEXPR_P (fn) = false;
5630 if (!DECL_GENERATED_P (fn))
5632 error ("enclosing class of constexpr non-static member "
5633 "function %q+#D is not a literal type", fn);
5634 explain_non_literal_class (t);
5639 /* T is a non-literal type used in a context which requires a constant
5640 expression. Explain why it isn't literal. */
5642 void
5643 explain_non_literal_class (tree t)
5645 static hash_set<tree> *diagnosed;
5647 if (!CLASS_TYPE_P (t))
5648 return;
5649 t = TYPE_MAIN_VARIANT (t);
5651 if (diagnosed == NULL)
5652 diagnosed = new hash_set<tree>;
5653 if (diagnosed->add (t))
5654 /* Already explained. */
5655 return;
5657 inform (0, "%q+T is not literal because:", t);
5658 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t))
5659 inform (0, " %q+T has a non-trivial destructor", t);
5660 else if (CLASSTYPE_NON_AGGREGATE (t)
5661 && !TYPE_HAS_TRIVIAL_DFLT (t)
5662 && !TYPE_HAS_CONSTEXPR_CTOR (t))
5664 inform (0, " %q+T is not an aggregate, does not have a trivial "
5665 "default constructor, and has no constexpr constructor that "
5666 "is not a copy or move constructor", t);
5667 if (type_has_non_user_provided_default_constructor (t))
5669 /* Note that we can't simply call locate_ctor because when the
5670 constructor is deleted it just returns NULL_TREE. */
5671 tree fns;
5672 for (fns = CLASSTYPE_CONSTRUCTORS (t); fns; fns = OVL_NEXT (fns))
5674 tree fn = OVL_CURRENT (fns);
5675 tree parms = TYPE_ARG_TYPES (TREE_TYPE (fn));
5677 parms = skip_artificial_parms_for (fn, parms);
5679 if (sufficient_parms_p (parms))
5681 if (DECL_DELETED_FN (fn))
5682 maybe_explain_implicit_delete (fn);
5683 else
5684 explain_invalid_constexpr_fn (fn);
5685 break;
5690 else
5692 tree binfo, base_binfo, field; int i;
5693 for (binfo = TYPE_BINFO (t), i = 0;
5694 BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
5696 tree basetype = TREE_TYPE (base_binfo);
5697 if (!CLASSTYPE_LITERAL_P (basetype))
5699 inform (0, " base class %qT of %q+T is non-literal",
5700 basetype, t);
5701 explain_non_literal_class (basetype);
5702 return;
5705 for (field = TYPE_FIELDS (t); field; field = TREE_CHAIN (field))
5707 tree ftype;
5708 if (TREE_CODE (field) != FIELD_DECL)
5709 continue;
5710 ftype = TREE_TYPE (field);
5711 if (!literal_type_p (ftype))
5713 inform (DECL_SOURCE_LOCATION (field),
5714 " non-static data member %qD has non-literal type",
5715 field);
5716 if (CLASS_TYPE_P (ftype))
5717 explain_non_literal_class (ftype);
5719 if (CP_TYPE_VOLATILE_P (ftype))
5720 inform (DECL_SOURCE_LOCATION (field),
5721 " non-static data member %qD has volatile type", field);
5726 /* Check the validity of the bases and members declared in T. Add any
5727 implicitly-generated functions (like copy-constructors and
5728 assignment operators). Compute various flag bits (like
5729 CLASSTYPE_NON_LAYOUT_POD_T) for T. This routine works purely at the C++
5730 level: i.e., independently of the ABI in use. */
5732 static void
5733 check_bases_and_members (tree t)
5735 /* Nonzero if the implicitly generated copy constructor should take
5736 a non-const reference argument. */
5737 int cant_have_const_ctor;
5738 /* Nonzero if the implicitly generated assignment operator
5739 should take a non-const reference argument. */
5740 int no_const_asn_ref;
5741 tree access_decls;
5742 bool saved_complex_asn_ref;
5743 bool saved_nontrivial_dtor;
5744 tree fn;
5746 /* By default, we use const reference arguments and generate default
5747 constructors. */
5748 cant_have_const_ctor = 0;
5749 no_const_asn_ref = 0;
5751 /* Check all the base-classes and set FMEM members to point to arrays
5752 of potential interest. */
5753 check_bases (t, &cant_have_const_ctor, &no_const_asn_ref);
5755 /* Deduce noexcept on destructors. This needs to happen after we've set
5756 triviality flags appropriately for our bases. */
5757 if (cxx_dialect >= cxx11)
5758 deduce_noexcept_on_destructors (t);
5760 /* Check all the method declarations. */
5761 check_methods (t);
5763 /* Save the initial values of these flags which only indicate whether
5764 or not the class has user-provided functions. As we analyze the
5765 bases and members we can set these flags for other reasons. */
5766 saved_complex_asn_ref = TYPE_HAS_COMPLEX_COPY_ASSIGN (t);
5767 saved_nontrivial_dtor = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t);
5769 /* Check all the data member declarations. We cannot call
5770 check_field_decls until we have called check_bases check_methods,
5771 as check_field_decls depends on TYPE_HAS_NONTRIVIAL_DESTRUCTOR
5772 being set appropriately. */
5773 check_field_decls (t, &access_decls,
5774 &cant_have_const_ctor,
5775 &no_const_asn_ref);
5777 /* A nearly-empty class has to be vptr-containing; a nearly empty
5778 class contains just a vptr. */
5779 if (!TYPE_CONTAINS_VPTR_P (t))
5780 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
5782 /* Do some bookkeeping that will guide the generation of implicitly
5783 declared member functions. */
5784 TYPE_HAS_COMPLEX_COPY_CTOR (t) |= TYPE_CONTAINS_VPTR_P (t);
5785 TYPE_HAS_COMPLEX_MOVE_CTOR (t) |= TYPE_CONTAINS_VPTR_P (t);
5786 /* We need to call a constructor for this class if it has a
5787 user-provided constructor, or if the default constructor is going
5788 to initialize the vptr. (This is not an if-and-only-if;
5789 TYPE_NEEDS_CONSTRUCTING is set elsewhere if bases or members
5790 themselves need constructing.) */
5791 TYPE_NEEDS_CONSTRUCTING (t)
5792 |= (type_has_user_provided_constructor (t) || TYPE_CONTAINS_VPTR_P (t));
5793 /* [dcl.init.aggr]
5795 An aggregate is an array or a class with no user-provided
5796 constructors ... and no virtual functions.
5798 Again, other conditions for being an aggregate are checked
5799 elsewhere. */
5800 CLASSTYPE_NON_AGGREGATE (t)
5801 |= (type_has_user_provided_or_explicit_constructor (t)
5802 || TYPE_POLYMORPHIC_P (t));
5803 /* This is the C++98/03 definition of POD; it changed in C++0x, but we
5804 retain the old definition internally for ABI reasons. */
5805 CLASSTYPE_NON_LAYOUT_POD_P (t)
5806 |= (CLASSTYPE_NON_AGGREGATE (t)
5807 || saved_nontrivial_dtor || saved_complex_asn_ref);
5808 CLASSTYPE_NON_STD_LAYOUT (t) |= TYPE_CONTAINS_VPTR_P (t);
5809 TYPE_HAS_COMPLEX_COPY_ASSIGN (t) |= TYPE_CONTAINS_VPTR_P (t);
5810 TYPE_HAS_COMPLEX_MOVE_ASSIGN (t) |= TYPE_CONTAINS_VPTR_P (t);
5811 TYPE_HAS_COMPLEX_DFLT (t) |= TYPE_CONTAINS_VPTR_P (t);
5813 /* If the only explicitly declared default constructor is user-provided,
5814 set TYPE_HAS_COMPLEX_DFLT. */
5815 if (!TYPE_HAS_COMPLEX_DFLT (t)
5816 && TYPE_HAS_DEFAULT_CONSTRUCTOR (t)
5817 && !type_has_non_user_provided_default_constructor (t))
5818 TYPE_HAS_COMPLEX_DFLT (t) = true;
5820 /* Warn if a public base of a polymorphic type has an accessible
5821 non-virtual destructor. It is only now that we know the class is
5822 polymorphic. Although a polymorphic base will have a already
5823 been diagnosed during its definition, we warn on use too. */
5824 if (TYPE_POLYMORPHIC_P (t) && warn_nonvdtor)
5826 tree binfo = TYPE_BINFO (t);
5827 vec<tree, va_gc> *accesses = BINFO_BASE_ACCESSES (binfo);
5828 tree base_binfo;
5829 unsigned i;
5831 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
5833 tree basetype = TREE_TYPE (base_binfo);
5835 if ((*accesses)[i] == access_public_node
5836 && (TYPE_POLYMORPHIC_P (basetype) || warn_ecpp)
5837 && accessible_nvdtor_p (basetype))
5838 warning (OPT_Wnon_virtual_dtor,
5839 "base class %q#T has accessible non-virtual destructor",
5840 basetype);
5844 /* If the class has no user-declared constructor, but does have
5845 non-static const or reference data members that can never be
5846 initialized, issue a warning. */
5847 if (warn_uninitialized
5848 /* Classes with user-declared constructors are presumed to
5849 initialize these members. */
5850 && !TYPE_HAS_USER_CONSTRUCTOR (t)
5851 /* Aggregates can be initialized with brace-enclosed
5852 initializers. */
5853 && CLASSTYPE_NON_AGGREGATE (t))
5855 tree field;
5857 for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
5859 tree type;
5861 if (TREE_CODE (field) != FIELD_DECL
5862 || DECL_INITIAL (field) != NULL_TREE)
5863 continue;
5865 type = TREE_TYPE (field);
5866 if (TREE_CODE (type) == REFERENCE_TYPE)
5867 warning_at (DECL_SOURCE_LOCATION (field),
5868 OPT_Wuninitialized, "non-static reference %q#D "
5869 "in class without a constructor", field);
5870 else if (CP_TYPE_CONST_P (type)
5871 && (!CLASS_TYPE_P (type)
5872 || !TYPE_HAS_DEFAULT_CONSTRUCTOR (type)))
5873 warning_at (DECL_SOURCE_LOCATION (field),
5874 OPT_Wuninitialized, "non-static const member %q#D "
5875 "in class without a constructor", field);
5879 /* Synthesize any needed methods. */
5880 add_implicitly_declared_members (t, &access_decls,
5881 cant_have_const_ctor,
5882 no_const_asn_ref);
5884 /* Check defaulted declarations here so we have cant_have_const_ctor
5885 and don't need to worry about clones. */
5886 for (fn = TYPE_METHODS (t); fn; fn = DECL_CHAIN (fn))
5887 if (!DECL_ARTIFICIAL (fn) && DECL_DEFAULTED_IN_CLASS_P (fn))
5889 int copy = copy_fn_p (fn);
5890 if (copy > 0)
5892 bool imp_const_p
5893 = (DECL_CONSTRUCTOR_P (fn) ? !cant_have_const_ctor
5894 : !no_const_asn_ref);
5895 bool fn_const_p = (copy == 2);
5897 if (fn_const_p && !imp_const_p)
5898 /* If the function is defaulted outside the class, we just
5899 give the synthesis error. */
5900 error ("%q+D declared to take const reference, but implicit "
5901 "declaration would take non-const", fn);
5903 defaulted_late_check (fn);
5906 if (LAMBDA_TYPE_P (t))
5908 /* "This class type is not an aggregate." */
5909 CLASSTYPE_NON_AGGREGATE (t) = 1;
5912 /* Compute the 'literal type' property before we
5913 do anything with non-static member functions. */
5914 finalize_literal_type_property (t);
5916 /* Create the in-charge and not-in-charge variants of constructors
5917 and destructors. */
5918 clone_constructors_and_destructors (t);
5920 /* Process the using-declarations. */
5921 for (; access_decls; access_decls = TREE_CHAIN (access_decls))
5922 handle_using_decl (TREE_VALUE (access_decls), t);
5924 /* Build and sort the CLASSTYPE_METHOD_VEC. */
5925 finish_struct_methods (t);
5927 /* Figure out whether or not we will need a cookie when dynamically
5928 allocating an array of this type. */
5929 TYPE_LANG_SPECIFIC (t)->u.c.vec_new_uses_cookie
5930 = type_requires_array_cookie (t);
5933 /* If T needs a pointer to its virtual function table, set TYPE_VFIELD
5934 accordingly. If a new vfield was created (because T doesn't have a
5935 primary base class), then the newly created field is returned. It
5936 is not added to the TYPE_FIELDS list; it is the caller's
5937 responsibility to do that. Accumulate declared virtual functions
5938 on VIRTUALS_P. */
5940 static tree
5941 create_vtable_ptr (tree t, tree* virtuals_p)
5943 tree fn;
5945 /* Collect the virtual functions declared in T. */
5946 for (fn = TYPE_METHODS (t); fn; fn = DECL_CHAIN (fn))
5947 if (TREE_CODE (fn) == FUNCTION_DECL
5948 && DECL_VINDEX (fn) && !DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (fn)
5949 && TREE_CODE (DECL_VINDEX (fn)) != INTEGER_CST)
5951 tree new_virtual = make_node (TREE_LIST);
5953 BV_FN (new_virtual) = fn;
5954 BV_DELTA (new_virtual) = integer_zero_node;
5955 BV_VCALL_INDEX (new_virtual) = NULL_TREE;
5957 TREE_CHAIN (new_virtual) = *virtuals_p;
5958 *virtuals_p = new_virtual;
5961 /* If we couldn't find an appropriate base class, create a new field
5962 here. Even if there weren't any new virtual functions, we might need a
5963 new virtual function table if we're supposed to include vptrs in
5964 all classes that need them. */
5965 if (!TYPE_VFIELD (t) && (*virtuals_p || TYPE_CONTAINS_VPTR_P (t)))
5967 /* We build this decl with vtbl_ptr_type_node, which is a
5968 `vtable_entry_type*'. It might seem more precise to use
5969 `vtable_entry_type (*)[N]' where N is the number of virtual
5970 functions. However, that would require the vtable pointer in
5971 base classes to have a different type than the vtable pointer
5972 in derived classes. We could make that happen, but that
5973 still wouldn't solve all the problems. In particular, the
5974 type-based alias analysis code would decide that assignments
5975 to the base class vtable pointer can't alias assignments to
5976 the derived class vtable pointer, since they have different
5977 types. Thus, in a derived class destructor, where the base
5978 class constructor was inlined, we could generate bad code for
5979 setting up the vtable pointer.
5981 Therefore, we use one type for all vtable pointers. We still
5982 use a type-correct type; it's just doesn't indicate the array
5983 bounds. That's better than using `void*' or some such; it's
5984 cleaner, and it let's the alias analysis code know that these
5985 stores cannot alias stores to void*! */
5986 tree field;
5988 field = build_decl (input_location,
5989 FIELD_DECL, get_vfield_name (t), vtbl_ptr_type_node);
5990 DECL_VIRTUAL_P (field) = 1;
5991 DECL_ARTIFICIAL (field) = 1;
5992 DECL_FIELD_CONTEXT (field) = t;
5993 DECL_FCONTEXT (field) = t;
5994 if (TYPE_PACKED (t))
5995 DECL_PACKED (field) = 1;
5997 TYPE_VFIELD (t) = field;
5999 /* This class is non-empty. */
6000 CLASSTYPE_EMPTY_P (t) = 0;
6002 return field;
6005 return NULL_TREE;
6008 /* Add OFFSET to all base types of BINFO which is a base in the
6009 hierarchy dominated by T.
6011 OFFSET, which is a type offset, is number of bytes. */
6013 static void
6014 propagate_binfo_offsets (tree binfo, tree offset)
6016 int i;
6017 tree primary_binfo;
6018 tree base_binfo;
6020 /* Update BINFO's offset. */
6021 BINFO_OFFSET (binfo)
6022 = fold_convert (sizetype,
6023 size_binop (PLUS_EXPR,
6024 fold_convert (ssizetype, BINFO_OFFSET (binfo)),
6025 offset));
6027 /* Find the primary base class. */
6028 primary_binfo = get_primary_binfo (binfo);
6030 if (primary_binfo && BINFO_INHERITANCE_CHAIN (primary_binfo) == binfo)
6031 propagate_binfo_offsets (primary_binfo, offset);
6033 /* Scan all of the bases, pushing the BINFO_OFFSET adjust
6034 downwards. */
6035 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
6037 /* Don't do the primary base twice. */
6038 if (base_binfo == primary_binfo)
6039 continue;
6041 if (BINFO_VIRTUAL_P (base_binfo))
6042 continue;
6044 propagate_binfo_offsets (base_binfo, offset);
6048 /* Set BINFO_OFFSET for all of the virtual bases for RLI->T. Update
6049 TYPE_ALIGN and TYPE_SIZE for T. OFFSETS gives the location of
6050 empty subobjects of T. */
6052 static void
6053 layout_virtual_bases (record_layout_info rli, splay_tree offsets)
6055 tree vbase;
6056 tree t = rli->t;
6057 tree *next_field;
6059 if (BINFO_N_BASE_BINFOS (TYPE_BINFO (t)) == 0)
6060 return;
6062 /* Find the last field. The artificial fields created for virtual
6063 bases will go after the last extant field to date. */
6064 next_field = &TYPE_FIELDS (t);
6065 while (*next_field)
6066 next_field = &DECL_CHAIN (*next_field);
6068 /* Go through the virtual bases, allocating space for each virtual
6069 base that is not already a primary base class. These are
6070 allocated in inheritance graph order. */
6071 for (vbase = TYPE_BINFO (t); vbase; vbase = TREE_CHAIN (vbase))
6073 if (!BINFO_VIRTUAL_P (vbase))
6074 continue;
6076 if (!BINFO_PRIMARY_P (vbase))
6078 /* This virtual base is not a primary base of any class in the
6079 hierarchy, so we have to add space for it. */
6080 next_field = build_base_field (rli, vbase,
6081 offsets, next_field);
6086 /* Returns the offset of the byte just past the end of the base class
6087 BINFO. */
6089 static tree
6090 end_of_base (tree binfo)
6092 tree size;
6094 if (!CLASSTYPE_AS_BASE (BINFO_TYPE (binfo)))
6095 size = TYPE_SIZE_UNIT (char_type_node);
6096 else if (is_empty_class (BINFO_TYPE (binfo)))
6097 /* An empty class has zero CLASSTYPE_SIZE_UNIT, but we need to
6098 allocate some space for it. It cannot have virtual bases, so
6099 TYPE_SIZE_UNIT is fine. */
6100 size = TYPE_SIZE_UNIT (BINFO_TYPE (binfo));
6101 else
6102 size = CLASSTYPE_SIZE_UNIT (BINFO_TYPE (binfo));
6104 return size_binop (PLUS_EXPR, BINFO_OFFSET (binfo), size);
6107 /* Returns the offset of the byte just past the end of the base class
6108 with the highest offset in T. If INCLUDE_VIRTUALS_P is zero, then
6109 only non-virtual bases are included. */
6111 static tree
6112 end_of_class (tree t, int include_virtuals_p)
6114 tree result = size_zero_node;
6115 vec<tree, va_gc> *vbases;
6116 tree binfo;
6117 tree base_binfo;
6118 tree offset;
6119 int i;
6121 for (binfo = TYPE_BINFO (t), i = 0;
6122 BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
6124 if (!include_virtuals_p
6125 && BINFO_VIRTUAL_P (base_binfo)
6126 && (!BINFO_PRIMARY_P (base_binfo)
6127 || BINFO_INHERITANCE_CHAIN (base_binfo) != TYPE_BINFO (t)))
6128 continue;
6130 offset = end_of_base (base_binfo);
6131 if (tree_int_cst_lt (result, offset))
6132 result = offset;
6135 if (include_virtuals_p)
6136 for (vbases = CLASSTYPE_VBASECLASSES (t), i = 0;
6137 vec_safe_iterate (vbases, i, &base_binfo); i++)
6139 offset = end_of_base (base_binfo);
6140 if (tree_int_cst_lt (result, offset))
6141 result = offset;
6144 return result;
6147 /* Warn about bases of T that are inaccessible because they are
6148 ambiguous. For example:
6150 struct S {};
6151 struct T : public S {};
6152 struct U : public S, public T {};
6154 Here, `(S*) new U' is not allowed because there are two `S'
6155 subobjects of U. */
6157 static void
6158 warn_about_ambiguous_bases (tree t)
6160 int i;
6161 vec<tree, va_gc> *vbases;
6162 tree basetype;
6163 tree binfo;
6164 tree base_binfo;
6166 /* If there are no repeated bases, nothing can be ambiguous. */
6167 if (!CLASSTYPE_REPEATED_BASE_P (t))
6168 return;
6170 /* Check direct bases. */
6171 for (binfo = TYPE_BINFO (t), i = 0;
6172 BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
6174 basetype = BINFO_TYPE (base_binfo);
6176 if (!uniquely_derived_from_p (basetype, t))
6177 warning (0, "direct base %qT inaccessible in %qT due to ambiguity",
6178 basetype, t);
6181 /* Check for ambiguous virtual bases. */
6182 if (extra_warnings)
6183 for (vbases = CLASSTYPE_VBASECLASSES (t), i = 0;
6184 vec_safe_iterate (vbases, i, &binfo); i++)
6186 basetype = BINFO_TYPE (binfo);
6188 if (!uniquely_derived_from_p (basetype, t))
6189 warning (OPT_Wextra, "virtual base %qT inaccessible in %qT due "
6190 "to ambiguity", basetype, t);
6194 /* Compare two INTEGER_CSTs K1 and K2. */
6196 static int
6197 splay_tree_compare_integer_csts (splay_tree_key k1, splay_tree_key k2)
6199 return tree_int_cst_compare ((tree) k1, (tree) k2);
6202 /* Increase the size indicated in RLI to account for empty classes
6203 that are "off the end" of the class. */
6205 static void
6206 include_empty_classes (record_layout_info rli)
6208 tree eoc;
6209 tree rli_size;
6211 /* It might be the case that we grew the class to allocate a
6212 zero-sized base class. That won't be reflected in RLI, yet,
6213 because we are willing to overlay multiple bases at the same
6214 offset. However, now we need to make sure that RLI is big enough
6215 to reflect the entire class. */
6216 eoc = end_of_class (rli->t,
6217 CLASSTYPE_AS_BASE (rli->t) != NULL_TREE);
6218 rli_size = rli_size_unit_so_far (rli);
6219 if (TREE_CODE (rli_size) == INTEGER_CST
6220 && tree_int_cst_lt (rli_size, eoc))
6222 /* The size should have been rounded to a whole byte. */
6223 gcc_assert (tree_int_cst_equal
6224 (rli->bitpos, round_down (rli->bitpos, BITS_PER_UNIT)));
6225 rli->bitpos
6226 = size_binop (PLUS_EXPR,
6227 rli->bitpos,
6228 size_binop (MULT_EXPR,
6229 fold_convert (bitsizetype,
6230 size_binop (MINUS_EXPR,
6231 eoc, rli_size)),
6232 bitsize_int (BITS_PER_UNIT)));
6233 normalize_rli (rli);
6237 /* Calculate the TYPE_SIZE, TYPE_ALIGN, etc for T. Calculate
6238 BINFO_OFFSETs for all of the base-classes. Position the vtable
6239 pointer. Accumulate declared virtual functions on VIRTUALS_P. */
6241 static void
6242 layout_class_type (tree t, tree *virtuals_p)
6244 tree non_static_data_members;
6245 tree field;
6246 tree vptr;
6247 record_layout_info rli;
6248 /* Maps offsets (represented as INTEGER_CSTs) to a TREE_LIST of
6249 types that appear at that offset. */
6250 splay_tree empty_base_offsets;
6251 /* True if the last field laid out was a bit-field. */
6252 bool last_field_was_bitfield = false;
6253 /* The location at which the next field should be inserted. */
6254 tree *next_field;
6255 /* T, as a base class. */
6256 tree base_t;
6258 /* Keep track of the first non-static data member. */
6259 non_static_data_members = TYPE_FIELDS (t);
6261 /* Start laying out the record. */
6262 rli = start_record_layout (t);
6264 /* Mark all the primary bases in the hierarchy. */
6265 determine_primary_bases (t);
6267 /* Create a pointer to our virtual function table. */
6268 vptr = create_vtable_ptr (t, virtuals_p);
6270 /* The vptr is always the first thing in the class. */
6271 if (vptr)
6273 DECL_CHAIN (vptr) = TYPE_FIELDS (t);
6274 TYPE_FIELDS (t) = vptr;
6275 next_field = &DECL_CHAIN (vptr);
6276 place_field (rli, vptr);
6278 else
6279 next_field = &TYPE_FIELDS (t);
6281 /* Build FIELD_DECLs for all of the non-virtual base-types. */
6282 empty_base_offsets = splay_tree_new (splay_tree_compare_integer_csts,
6283 NULL, NULL);
6284 build_base_fields (rli, empty_base_offsets, next_field);
6286 /* Layout the non-static data members. */
6287 for (field = non_static_data_members; field; field = DECL_CHAIN (field))
6289 tree type;
6290 tree padding;
6292 /* We still pass things that aren't non-static data members to
6293 the back end, in case it wants to do something with them. */
6294 if (TREE_CODE (field) != FIELD_DECL)
6296 place_field (rli, field);
6297 /* If the static data member has incomplete type, keep track
6298 of it so that it can be completed later. (The handling
6299 of pending statics in finish_record_layout is
6300 insufficient; consider:
6302 struct S1;
6303 struct S2 { static S1 s1; };
6305 At this point, finish_record_layout will be called, but
6306 S1 is still incomplete.) */
6307 if (VAR_P (field))
6309 maybe_register_incomplete_var (field);
6310 /* The visibility of static data members is determined
6311 at their point of declaration, not their point of
6312 definition. */
6313 determine_visibility (field);
6315 continue;
6318 type = TREE_TYPE (field);
6319 if (type == error_mark_node)
6320 continue;
6322 padding = NULL_TREE;
6324 /* If this field is a bit-field whose width is greater than its
6325 type, then there are some special rules for allocating
6326 it. */
6327 if (DECL_C_BIT_FIELD (field)
6328 && tree_int_cst_lt (TYPE_SIZE (type), DECL_SIZE (field)))
6330 unsigned int itk;
6331 tree integer_type;
6332 bool was_unnamed_p = false;
6333 /* We must allocate the bits as if suitably aligned for the
6334 longest integer type that fits in this many bits. type
6335 of the field. Then, we are supposed to use the left over
6336 bits as additional padding. */
6337 for (itk = itk_char; itk != itk_none; ++itk)
6338 if (integer_types[itk] != NULL_TREE
6339 && (tree_int_cst_lt (size_int (MAX_FIXED_MODE_SIZE),
6340 TYPE_SIZE (integer_types[itk]))
6341 || tree_int_cst_lt (DECL_SIZE (field),
6342 TYPE_SIZE (integer_types[itk]))))
6343 break;
6345 /* ITK now indicates a type that is too large for the
6346 field. We have to back up by one to find the largest
6347 type that fits. */
6350 --itk;
6351 integer_type = integer_types[itk];
6352 } while (itk > 0 && integer_type == NULL_TREE);
6354 /* Figure out how much additional padding is required. */
6355 if (tree_int_cst_lt (TYPE_SIZE (integer_type), DECL_SIZE (field)))
6357 if (TREE_CODE (t) == UNION_TYPE)
6358 /* In a union, the padding field must have the full width
6359 of the bit-field; all fields start at offset zero. */
6360 padding = DECL_SIZE (field);
6361 else
6362 padding = size_binop (MINUS_EXPR, DECL_SIZE (field),
6363 TYPE_SIZE (integer_type));
6366 /* An unnamed bitfield does not normally affect the
6367 alignment of the containing class on a target where
6368 PCC_BITFIELD_TYPE_MATTERS. But, the C++ ABI does not
6369 make any exceptions for unnamed bitfields when the
6370 bitfields are longer than their types. Therefore, we
6371 temporarily give the field a name. */
6372 if (PCC_BITFIELD_TYPE_MATTERS && !DECL_NAME (field))
6374 was_unnamed_p = true;
6375 DECL_NAME (field) = make_anon_name ();
6378 DECL_SIZE (field) = TYPE_SIZE (integer_type);
6379 DECL_ALIGN (field) = TYPE_ALIGN (integer_type);
6380 DECL_USER_ALIGN (field) = TYPE_USER_ALIGN (integer_type);
6381 layout_nonempty_base_or_field (rli, field, NULL_TREE,
6382 empty_base_offsets);
6383 if (was_unnamed_p)
6384 DECL_NAME (field) = NULL_TREE;
6385 /* Now that layout has been performed, set the size of the
6386 field to the size of its declared type; the rest of the
6387 field is effectively invisible. */
6388 DECL_SIZE (field) = TYPE_SIZE (type);
6389 /* We must also reset the DECL_MODE of the field. */
6390 DECL_MODE (field) = TYPE_MODE (type);
6392 else
6393 layout_nonempty_base_or_field (rli, field, NULL_TREE,
6394 empty_base_offsets);
6396 /* Remember the location of any empty classes in FIELD. */
6397 record_subobject_offsets (TREE_TYPE (field),
6398 byte_position(field),
6399 empty_base_offsets,
6400 /*is_data_member=*/true);
6402 /* If a bit-field does not immediately follow another bit-field,
6403 and yet it starts in the middle of a byte, we have failed to
6404 comply with the ABI. */
6405 if (warn_abi
6406 && DECL_C_BIT_FIELD (field)
6407 /* The TREE_NO_WARNING flag gets set by Objective-C when
6408 laying out an Objective-C class. The ObjC ABI differs
6409 from the C++ ABI, and so we do not want a warning
6410 here. */
6411 && !TREE_NO_WARNING (field)
6412 && !last_field_was_bitfield
6413 && !integer_zerop (size_binop (TRUNC_MOD_EXPR,
6414 DECL_FIELD_BIT_OFFSET (field),
6415 bitsize_unit_node)))
6416 warning_at (DECL_SOURCE_LOCATION (field), OPT_Wabi,
6417 "offset of %qD is not ABI-compliant and may "
6418 "change in a future version of GCC", field);
6420 /* The middle end uses the type of expressions to determine the
6421 possible range of expression values. In order to optimize
6422 "x.i > 7" to "false" for a 2-bit bitfield "i", the middle end
6423 must be made aware of the width of "i", via its type.
6425 Because C++ does not have integer types of arbitrary width,
6426 we must (for the purposes of the front end) convert from the
6427 type assigned here to the declared type of the bitfield
6428 whenever a bitfield expression is used as an rvalue.
6429 Similarly, when assigning a value to a bitfield, the value
6430 must be converted to the type given the bitfield here. */
6431 if (DECL_C_BIT_FIELD (field))
6433 unsigned HOST_WIDE_INT width;
6434 tree ftype = TREE_TYPE (field);
6435 width = tree_to_uhwi (DECL_SIZE (field));
6436 if (width != TYPE_PRECISION (ftype))
6438 TREE_TYPE (field)
6439 = c_build_bitfield_integer_type (width,
6440 TYPE_UNSIGNED (ftype));
6441 TREE_TYPE (field)
6442 = cp_build_qualified_type (TREE_TYPE (field),
6443 cp_type_quals (ftype));
6447 /* If we needed additional padding after this field, add it
6448 now. */
6449 if (padding)
6451 tree padding_field;
6453 padding_field = build_decl (input_location,
6454 FIELD_DECL,
6455 NULL_TREE,
6456 char_type_node);
6457 DECL_BIT_FIELD (padding_field) = 1;
6458 DECL_SIZE (padding_field) = padding;
6459 DECL_CONTEXT (padding_field) = t;
6460 DECL_ARTIFICIAL (padding_field) = 1;
6461 DECL_IGNORED_P (padding_field) = 1;
6462 layout_nonempty_base_or_field (rli, padding_field,
6463 NULL_TREE,
6464 empty_base_offsets);
6467 last_field_was_bitfield = DECL_C_BIT_FIELD (field);
6470 if (!integer_zerop (rli->bitpos))
6472 /* Make sure that we are on a byte boundary so that the size of
6473 the class without virtual bases will always be a round number
6474 of bytes. */
6475 rli->bitpos = round_up_loc (input_location, rli->bitpos, BITS_PER_UNIT);
6476 normalize_rli (rli);
6479 /* Delete all zero-width bit-fields from the list of fields. Now
6480 that the type is laid out they are no longer important. */
6481 remove_zero_width_bit_fields (t);
6483 /* Create the version of T used for virtual bases. We do not use
6484 make_class_type for this version; this is an artificial type. For
6485 a POD type, we just reuse T. */
6486 if (CLASSTYPE_NON_LAYOUT_POD_P (t) || CLASSTYPE_EMPTY_P (t))
6488 base_t = make_node (TREE_CODE (t));
6490 /* Set the size and alignment for the new type. */
6491 tree eoc;
6493 /* If the ABI version is not at least two, and the last
6494 field was a bit-field, RLI may not be on a byte
6495 boundary. In particular, rli_size_unit_so_far might
6496 indicate the last complete byte, while rli_size_so_far
6497 indicates the total number of bits used. Therefore,
6498 rli_size_so_far, rather than rli_size_unit_so_far, is
6499 used to compute TYPE_SIZE_UNIT. */
6500 eoc = end_of_class (t, /*include_virtuals_p=*/0);
6501 TYPE_SIZE_UNIT (base_t)
6502 = size_binop (MAX_EXPR,
6503 fold_convert (sizetype,
6504 size_binop (CEIL_DIV_EXPR,
6505 rli_size_so_far (rli),
6506 bitsize_int (BITS_PER_UNIT))),
6507 eoc);
6508 TYPE_SIZE (base_t)
6509 = size_binop (MAX_EXPR,
6510 rli_size_so_far (rli),
6511 size_binop (MULT_EXPR,
6512 fold_convert (bitsizetype, eoc),
6513 bitsize_int (BITS_PER_UNIT)));
6514 TYPE_ALIGN (base_t) = rli->record_align;
6515 TYPE_USER_ALIGN (base_t) = TYPE_USER_ALIGN (t);
6517 /* Copy the fields from T. */
6518 next_field = &TYPE_FIELDS (base_t);
6519 for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
6520 if (TREE_CODE (field) == FIELD_DECL)
6522 *next_field = copy_node (field);
6523 DECL_CONTEXT (*next_field) = base_t;
6524 next_field = &DECL_CHAIN (*next_field);
6526 *next_field = NULL_TREE;
6528 /* Record the base version of the type. */
6529 CLASSTYPE_AS_BASE (t) = base_t;
6530 TYPE_CONTEXT (base_t) = t;
6532 else
6533 CLASSTYPE_AS_BASE (t) = t;
6535 /* Every empty class contains an empty class. */
6536 if (CLASSTYPE_EMPTY_P (t))
6537 CLASSTYPE_CONTAINS_EMPTY_CLASS_P (t) = 1;
6539 /* Set the TYPE_DECL for this type to contain the right
6540 value for DECL_OFFSET, so that we can use it as part
6541 of a COMPONENT_REF for multiple inheritance. */
6542 layout_decl (TYPE_MAIN_DECL (t), 0);
6544 /* Now fix up any virtual base class types that we left lying
6545 around. We must get these done before we try to lay out the
6546 virtual function table. As a side-effect, this will remove the
6547 base subobject fields. */
6548 layout_virtual_bases (rli, empty_base_offsets);
6550 /* Make sure that empty classes are reflected in RLI at this
6551 point. */
6552 include_empty_classes(rli);
6554 /* Make sure not to create any structures with zero size. */
6555 if (integer_zerop (rli_size_unit_so_far (rli)) && CLASSTYPE_EMPTY_P (t))
6556 place_field (rli,
6557 build_decl (input_location,
6558 FIELD_DECL, NULL_TREE, char_type_node));
6560 /* If this is a non-POD, declaring it packed makes a difference to how it
6561 can be used as a field; don't let finalize_record_size undo it. */
6562 if (TYPE_PACKED (t) && !layout_pod_type_p (t))
6563 rli->packed_maybe_necessary = true;
6565 /* Let the back end lay out the type. */
6566 finish_record_layout (rli, /*free_p=*/true);
6568 if (TYPE_SIZE_UNIT (t)
6569 && TREE_CODE (TYPE_SIZE_UNIT (t)) == INTEGER_CST
6570 && !TREE_OVERFLOW (TYPE_SIZE_UNIT (t))
6571 && !valid_constant_size_p (TYPE_SIZE_UNIT (t)))
6572 error ("size of type %qT is too large (%qE bytes)", t, TYPE_SIZE_UNIT (t));
6574 /* Warn about bases that can't be talked about due to ambiguity. */
6575 warn_about_ambiguous_bases (t);
6577 /* Now that we're done with layout, give the base fields the real types. */
6578 for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
6579 if (DECL_ARTIFICIAL (field) && IS_FAKE_BASE_TYPE (TREE_TYPE (field)))
6580 TREE_TYPE (field) = TYPE_CONTEXT (TREE_TYPE (field));
6582 /* Clean up. */
6583 splay_tree_delete (empty_base_offsets);
6585 if (CLASSTYPE_EMPTY_P (t)
6586 && tree_int_cst_lt (sizeof_biggest_empty_class,
6587 TYPE_SIZE_UNIT (t)))
6588 sizeof_biggest_empty_class = TYPE_SIZE_UNIT (t);
6591 /* Determine the "key method" for the class type indicated by TYPE,
6592 and set CLASSTYPE_KEY_METHOD accordingly. */
6594 void
6595 determine_key_method (tree type)
6597 tree method;
6599 if (TYPE_FOR_JAVA (type)
6600 || processing_template_decl
6601 || CLASSTYPE_TEMPLATE_INSTANTIATION (type)
6602 || CLASSTYPE_INTERFACE_KNOWN (type))
6603 return;
6605 /* The key method is the first non-pure virtual function that is not
6606 inline at the point of class definition. On some targets the
6607 key function may not be inline; those targets should not call
6608 this function until the end of the translation unit. */
6609 for (method = TYPE_METHODS (type); method != NULL_TREE;
6610 method = DECL_CHAIN (method))
6611 if (TREE_CODE (method) == FUNCTION_DECL
6612 && DECL_VINDEX (method) != NULL_TREE
6613 && ! DECL_DECLARED_INLINE_P (method)
6614 && ! DECL_PURE_VIRTUAL_P (method))
6616 CLASSTYPE_KEY_METHOD (type) = method;
6617 break;
6620 return;
6624 /* Allocate and return an instance of struct sorted_fields_type with
6625 N fields. */
6627 static struct sorted_fields_type *
6628 sorted_fields_type_new (int n)
6630 struct sorted_fields_type *sft;
6631 sft = (sorted_fields_type *) ggc_internal_alloc (sizeof (sorted_fields_type)
6632 + n * sizeof (tree));
6633 sft->len = n;
6635 return sft;
6638 /* Helper of find_flexarrays. Return true when FLD refers to a non-static
6639 class data member of non-zero size, otherwise false. */
6641 static inline bool
6642 field_nonempty_p (const_tree fld)
6644 if (TREE_CODE (fld) == ERROR_MARK)
6645 return false;
6647 tree type = TREE_TYPE (fld);
6648 if (TREE_CODE (fld) == FIELD_DECL
6649 && TREE_CODE (type) != ERROR_MARK
6650 && (DECL_NAME (fld) || RECORD_OR_UNION_TYPE_P (type)))
6652 return TYPE_SIZE (type)
6653 && (TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST
6654 || !tree_int_cst_equal (size_zero_node, TYPE_SIZE (type)));
6657 return false;
6660 /* Used by find_flexarrays and related. */
6661 struct flexmems_t {
6662 /* The first flexible array member or non-zero array member found
6663 in order of layout. */
6664 tree array;
6665 /* First non-static non-empty data member in the class or its bases. */
6666 tree first;
6667 /* First non-static non-empty data member following either the flexible
6668 array member, if found, or the zero-length array member. */
6669 tree after;
6672 /* Find either the first flexible array member or the first zero-length
6673 array, in that order or preference, among members of class T (but not
6674 its base classes), and set members of FMEM accordingly. */
6676 static void
6677 find_flexarrays (tree t, flexmems_t *fmem)
6679 for (tree fld = TYPE_FIELDS (t), next; fld; fld = next)
6681 /* Find the next non-static data member if it exists. */
6682 for (next = fld;
6683 (next = DECL_CHAIN (next))
6684 && TREE_CODE (next) != FIELD_DECL; );
6686 tree fldtype = TREE_TYPE (fld);
6687 if (TREE_CODE (fld) != TYPE_DECL
6688 && RECORD_OR_UNION_TYPE_P (fldtype)
6689 && TYPE_ANONYMOUS_P (fldtype))
6691 /* Members of anonymous structs and unions are treated as if
6692 they were members of the containing class. Descend into
6693 the anonymous struct or union and find a flexible array
6694 member or zero-length array among its fields. */
6695 find_flexarrays (fldtype, fmem);
6696 continue;
6699 /* Skip anything that's not a (non-static) data member. */
6700 if (TREE_CODE (fld) != FIELD_DECL)
6701 continue;
6703 /* Skip virtual table pointers. */
6704 if (DECL_ARTIFICIAL (fld))
6705 continue;
6707 if (field_nonempty_p (fld))
6709 /* Remember the first non-static data member. */
6710 if (!fmem->first)
6711 fmem->first = fld;
6713 /* Remember the first non-static data member after the flexible
6714 array member, if one has been found, or the zero-length array
6715 if it has been found. */
6716 if (!fmem->after && fmem->array)
6717 fmem->after = fld;
6720 /* Skip non-arrays. */
6721 if (TREE_CODE (fldtype) != ARRAY_TYPE)
6722 continue;
6724 /* Determine the upper bound of the array if it has one. */
6725 if (TYPE_DOMAIN (fldtype))
6727 if (fmem->array)
6729 /* Make a record of the zero-length array if either one
6730 such field or a flexible array member has been seen to
6731 handle the pathological and unlikely case of multiple
6732 such members. */
6733 if (!fmem->after)
6734 fmem->after = fld;
6736 else if (integer_all_onesp (TYPE_MAX_VALUE (TYPE_DOMAIN (fldtype))))
6737 /* Remember the first zero-length array unless a flexible array
6738 member has already been seen. */
6739 fmem->array = fld;
6741 else
6743 /* Flexible array members have no upper bound. */
6744 if (fmem->array)
6746 /* Replace the zero-length array if it's been stored and
6747 reset the after pointer. */
6748 if (TYPE_DOMAIN (TREE_TYPE (fmem->array)))
6750 fmem->array = fld;
6751 fmem->after = NULL_TREE;
6754 else
6755 fmem->array = fld;
6760 /* Issue diagnostics for invalid flexible array members or zero-length
6761 arrays that are not the last elements of the containing class or its
6762 base classes or that are its sole members. */
6764 static void
6765 diagnose_flexarrays (tree t, const flexmems_t *fmem)
6767 /* Members of anonymous structs and unions are considered to be members
6768 of the containing struct or union. */
6769 if (TYPE_ANONYMOUS_P (t) || !fmem->array)
6770 return;
6772 const char *msg = 0;
6774 if (TYPE_DOMAIN (TREE_TYPE (fmem->array)))
6776 if (fmem->after)
6777 msg = G_("zero-size array member %qD not at end of %q#T");
6778 else if (!fmem->first)
6779 msg = G_("zero-size array member %qD in an otherwise empty %q#T");
6781 if (msg && pedwarn (DECL_SOURCE_LOCATION (fmem->array),
6782 OPT_Wpedantic, msg, fmem->array, t))
6784 inform (location_of (t), "in the definition of %q#T", t);
6786 else
6788 if (fmem->after)
6789 msg = G_("flexible array member %qD not at end of %q#T");
6790 else if (!fmem->first)
6791 msg = G_("flexible array member %qD in an otherwise empty %q#T");
6793 if (msg)
6795 error_at (DECL_SOURCE_LOCATION (fmem->array), msg,
6796 fmem->array, t);
6798 /* In the unlikely event that the member following the flexible
6799 array member is declared in a different class, point to it.
6800 Otherwise it should be obvious. */
6801 if (fmem->after
6802 && (DECL_CONTEXT (fmem->after) != DECL_CONTEXT (fmem->array)))
6803 inform (DECL_SOURCE_LOCATION (fmem->after),
6804 "next member %q#D declared here",
6805 fmem->after);
6807 inform (location_of (t), "in the definition of %q#T", t);
6813 /* Recursively check to make sure that any flexible array or zero-length
6814 array members of class T or its bases are valid (i.e., not the sole
6815 non-static data member of T and, if one exists, that it is the last
6816 non-static data member of T and its base classes. FMEM is expected
6817 to be initially null and is used internally by recursive calls to
6818 the function. Issue the appropriate diagnostics for the array member
6819 that fails the checks. */
6821 static void
6822 check_flexarrays (tree t, flexmems_t *fmem /* = NULL */)
6824 /* Initialize the result of a search for flexible array and zero-length
6825 array members. Avoid doing any work if the most interesting FMEM data
6826 have already been populated. */
6827 flexmems_t flexmems = flexmems_t ();
6828 if (!fmem)
6829 fmem = &flexmems;
6830 else if (fmem->array && fmem->first && fmem->after)
6831 return;
6833 /* Recursively check the primary base class first. */
6834 if (CLASSTYPE_HAS_PRIMARY_BASE_P (t))
6836 tree basetype = BINFO_TYPE (CLASSTYPE_PRIMARY_BINFO (t));
6837 check_flexarrays (basetype, fmem);
6840 /* Recursively check the base classes. */
6841 int nbases = BINFO_N_BASE_BINFOS (TYPE_BINFO (t));
6842 for (int i = 0; i < nbases; ++i)
6844 tree base_binfo = BINFO_BASE_BINFO (TYPE_BINFO (t), i);
6846 /* The primary base class was already checked above. */
6847 if (base_binfo == CLASSTYPE_PRIMARY_BINFO (t))
6848 continue;
6850 /* Virtual base classes are at the end. */
6851 if (BINFO_VIRTUAL_P (base_binfo))
6852 continue;
6854 /* Check the base class. */
6855 check_flexarrays (BINFO_TYPE (base_binfo), fmem);
6858 if (fmem == &flexmems)
6860 /* Check virtual base classes only once per derived class.
6861 I.e., this check is not performed recursively for base
6862 classes. */
6863 int i;
6864 tree base_binfo;
6865 vec<tree, va_gc> *vbases;
6866 for (vbases = CLASSTYPE_VBASECLASSES (t), i = 0;
6867 vec_safe_iterate (vbases, i, &base_binfo); i++)
6869 /* Check the virtual base class. */
6870 tree basetype = TREE_TYPE (base_binfo);
6872 check_flexarrays (basetype, fmem);
6876 /* Search the members of the current (derived) class. */
6877 find_flexarrays (t, fmem);
6879 if (fmem == &flexmems)
6881 /* Issue diagnostics for invalid flexible and zero-length array members
6882 found in base classes or among the members of the current class. */
6883 diagnose_flexarrays (t, fmem);
6887 /* Perform processing required when the definition of T (a class type)
6888 is complete. Diagnose invalid definitions of flexible array members
6889 and zero-size arrays. */
6891 void
6892 finish_struct_1 (tree t)
6894 tree x;
6895 /* A TREE_LIST. The TREE_VALUE of each node is a FUNCTION_DECL. */
6896 tree virtuals = NULL_TREE;
6898 if (COMPLETE_TYPE_P (t))
6900 gcc_assert (MAYBE_CLASS_TYPE_P (t));
6901 error ("redefinition of %q#T", t);
6902 popclass ();
6903 return;
6906 /* If this type was previously laid out as a forward reference,
6907 make sure we lay it out again. */
6908 TYPE_SIZE (t) = NULL_TREE;
6909 CLASSTYPE_PRIMARY_BINFO (t) = NULL_TREE;
6911 /* Make assumptions about the class; we'll reset the flags if
6912 necessary. */
6913 CLASSTYPE_EMPTY_P (t) = 1;
6914 CLASSTYPE_NEARLY_EMPTY_P (t) = 1;
6915 CLASSTYPE_CONTAINS_EMPTY_CLASS_P (t) = 0;
6916 CLASSTYPE_LITERAL_P (t) = true;
6918 /* Do end-of-class semantic processing: checking the validity of the
6919 bases and members and add implicitly generated methods. */
6920 check_bases_and_members (t);
6922 /* Find the key method. */
6923 if (TYPE_CONTAINS_VPTR_P (t))
6925 /* The Itanium C++ ABI permits the key method to be chosen when
6926 the class is defined -- even though the key method so
6927 selected may later turn out to be an inline function. On
6928 some systems (such as ARM Symbian OS) the key method cannot
6929 be determined until the end of the translation unit. On such
6930 systems, we leave CLASSTYPE_KEY_METHOD set to NULL, which
6931 will cause the class to be added to KEYED_CLASSES. Then, in
6932 finish_file we will determine the key method. */
6933 if (targetm.cxx.key_method_may_be_inline ())
6934 determine_key_method (t);
6936 /* If a polymorphic class has no key method, we may emit the vtable
6937 in every translation unit where the class definition appears. If
6938 we're devirtualizing, we can look into the vtable even if we
6939 aren't emitting it. */
6940 if (CLASSTYPE_KEY_METHOD (t) == NULL_TREE)
6941 keyed_classes = tree_cons (NULL_TREE, t, keyed_classes);
6944 /* Layout the class itself. */
6945 layout_class_type (t, &virtuals);
6946 if (CLASSTYPE_AS_BASE (t) != t)
6947 /* We use the base type for trivial assignments, and hence it
6948 needs a mode. */
6949 compute_record_mode (CLASSTYPE_AS_BASE (t));
6951 /* With the layout complete, check for flexible array members and
6952 zero-length arrays that might overlap other members in the final
6953 layout. */
6954 check_flexarrays (t);
6956 virtuals = modify_all_vtables (t, nreverse (virtuals));
6958 /* If necessary, create the primary vtable for this class. */
6959 if (virtuals || TYPE_CONTAINS_VPTR_P (t))
6961 /* We must enter these virtuals into the table. */
6962 if (!CLASSTYPE_HAS_PRIMARY_BASE_P (t))
6963 build_primary_vtable (NULL_TREE, t);
6964 else if (! BINFO_NEW_VTABLE_MARKED (TYPE_BINFO (t)))
6965 /* Here we know enough to change the type of our virtual
6966 function table, but we will wait until later this function. */
6967 build_primary_vtable (CLASSTYPE_PRIMARY_BINFO (t), t);
6969 /* If we're warning about ABI tags, check the types of the new
6970 virtual functions. */
6971 if (warn_abi_tag)
6972 for (tree v = virtuals; v; v = TREE_CHAIN (v))
6973 check_abi_tags (t, TREE_VALUE (v));
6976 if (TYPE_CONTAINS_VPTR_P (t))
6978 int vindex;
6979 tree fn;
6981 if (BINFO_VTABLE (TYPE_BINFO (t)))
6982 gcc_assert (DECL_VIRTUAL_P (BINFO_VTABLE (TYPE_BINFO (t))));
6983 if (!CLASSTYPE_HAS_PRIMARY_BASE_P (t))
6984 gcc_assert (BINFO_VIRTUALS (TYPE_BINFO (t)) == NULL_TREE);
6986 /* Add entries for virtual functions introduced by this class. */
6987 BINFO_VIRTUALS (TYPE_BINFO (t))
6988 = chainon (BINFO_VIRTUALS (TYPE_BINFO (t)), virtuals);
6990 /* Set DECL_VINDEX for all functions declared in this class. */
6991 for (vindex = 0, fn = BINFO_VIRTUALS (TYPE_BINFO (t));
6993 fn = TREE_CHAIN (fn),
6994 vindex += (TARGET_VTABLE_USES_DESCRIPTORS
6995 ? TARGET_VTABLE_USES_DESCRIPTORS : 1))
6997 tree fndecl = BV_FN (fn);
6999 if (DECL_THUNK_P (fndecl))
7000 /* A thunk. We should never be calling this entry directly
7001 from this vtable -- we'd use the entry for the non
7002 thunk base function. */
7003 DECL_VINDEX (fndecl) = NULL_TREE;
7004 else if (TREE_CODE (DECL_VINDEX (fndecl)) != INTEGER_CST)
7005 DECL_VINDEX (fndecl) = build_int_cst (NULL_TREE, vindex);
7009 finish_struct_bits (t);
7010 set_method_tm_attributes (t);
7011 if (flag_openmp || flag_openmp_simd)
7012 finish_omp_declare_simd_methods (t);
7014 /* Complete the rtl for any static member objects of the type we're
7015 working on. */
7016 for (x = TYPE_FIELDS (t); x; x = DECL_CHAIN (x))
7017 if (VAR_P (x) && TREE_STATIC (x)
7018 && TREE_TYPE (x) != error_mark_node
7019 && same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (x)), t))
7020 DECL_MODE (x) = TYPE_MODE (t);
7022 /* Done with FIELDS...now decide whether to sort these for
7023 faster lookups later.
7025 We use a small number because most searches fail (succeeding
7026 ultimately as the search bores through the inheritance
7027 hierarchy), and we want this failure to occur quickly. */
7029 insert_into_classtype_sorted_fields (TYPE_FIELDS (t), t, 8);
7031 /* Complain if one of the field types requires lower visibility. */
7032 constrain_class_visibility (t);
7034 /* Make the rtl for any new vtables we have created, and unmark
7035 the base types we marked. */
7036 finish_vtbls (t);
7038 /* Build the VTT for T. */
7039 build_vtt (t);
7041 /* This warning does not make sense for Java classes, since they
7042 cannot have destructors. */
7043 if (!TYPE_FOR_JAVA (t) && warn_nonvdtor
7044 && TYPE_POLYMORPHIC_P (t) && accessible_nvdtor_p (t)
7045 && !CLASSTYPE_FINAL (t))
7046 warning (OPT_Wnon_virtual_dtor,
7047 "%q#T has virtual functions and accessible"
7048 " non-virtual destructor", t);
7050 complete_vars (t);
7052 if (warn_overloaded_virtual)
7053 warn_hidden (t);
7055 /* Class layout, assignment of virtual table slots, etc., is now
7056 complete. Give the back end a chance to tweak the visibility of
7057 the class or perform any other required target modifications. */
7058 targetm.cxx.adjust_class_at_definition (t);
7060 maybe_suppress_debug_info (t);
7062 if (flag_vtable_verify)
7063 vtv_save_class_info (t);
7065 dump_class_hierarchy (t);
7067 /* Finish debugging output for this type. */
7068 rest_of_type_compilation (t, ! LOCAL_CLASS_P (t));
7070 if (TYPE_TRANSPARENT_AGGR (t))
7072 tree field = first_field (t);
7073 if (field == NULL_TREE || error_operand_p (field))
7075 error ("type transparent %q#T does not have any fields", t);
7076 TYPE_TRANSPARENT_AGGR (t) = 0;
7078 else if (DECL_ARTIFICIAL (field))
7080 if (DECL_FIELD_IS_BASE (field))
7081 error ("type transparent class %qT has base classes", t);
7082 else
7084 gcc_checking_assert (DECL_VIRTUAL_P (field));
7085 error ("type transparent class %qT has virtual functions", t);
7087 TYPE_TRANSPARENT_AGGR (t) = 0;
7089 else if (TYPE_MODE (t) != DECL_MODE (field))
7091 error ("type transparent %q#T cannot be made transparent because "
7092 "the type of the first field has a different ABI from the "
7093 "class overall", t);
7094 TYPE_TRANSPARENT_AGGR (t) = 0;
7099 /* Insert FIELDS into T for the sorted case if the FIELDS count is
7100 equal to THRESHOLD or greater than THRESHOLD. */
7102 static void
7103 insert_into_classtype_sorted_fields (tree fields, tree t, int threshold)
7105 int n_fields = count_fields (fields);
7106 if (n_fields >= threshold)
7108 struct sorted_fields_type *field_vec = sorted_fields_type_new (n_fields);
7109 add_fields_to_record_type (fields, field_vec, 0);
7110 qsort (field_vec->elts, n_fields, sizeof (tree), field_decl_cmp);
7111 CLASSTYPE_SORTED_FIELDS (t) = field_vec;
7115 /* Insert lately defined enum ENUMTYPE into T for the sorted case. */
7117 void
7118 insert_late_enum_def_into_classtype_sorted_fields (tree enumtype, tree t)
7120 struct sorted_fields_type *sorted_fields = CLASSTYPE_SORTED_FIELDS (t);
7121 if (sorted_fields)
7123 int i;
7124 int n_fields
7125 = list_length (TYPE_VALUES (enumtype)) + sorted_fields->len;
7126 struct sorted_fields_type *field_vec = sorted_fields_type_new (n_fields);
7128 for (i = 0; i < sorted_fields->len; ++i)
7129 field_vec->elts[i] = sorted_fields->elts[i];
7131 add_enum_fields_to_record_type (enumtype, field_vec,
7132 sorted_fields->len);
7133 qsort (field_vec->elts, n_fields, sizeof (tree), field_decl_cmp);
7134 CLASSTYPE_SORTED_FIELDS (t) = field_vec;
7138 /* When T was built up, the member declarations were added in reverse
7139 order. Rearrange them to declaration order. */
7141 void
7142 unreverse_member_declarations (tree t)
7144 tree next;
7145 tree prev;
7146 tree x;
7148 /* The following lists are all in reverse order. Put them in
7149 declaration order now. */
7150 TYPE_METHODS (t) = nreverse (TYPE_METHODS (t));
7151 CLASSTYPE_DECL_LIST (t) = nreverse (CLASSTYPE_DECL_LIST (t));
7153 /* Actually, for the TYPE_FIELDS, only the non TYPE_DECLs are in
7154 reverse order, so we can't just use nreverse. */
7155 prev = NULL_TREE;
7156 for (x = TYPE_FIELDS (t);
7157 x && TREE_CODE (x) != TYPE_DECL;
7158 x = next)
7160 next = DECL_CHAIN (x);
7161 DECL_CHAIN (x) = prev;
7162 prev = x;
7164 if (prev)
7166 DECL_CHAIN (TYPE_FIELDS (t)) = x;
7167 if (prev)
7168 TYPE_FIELDS (t) = prev;
7172 tree
7173 finish_struct (tree t, tree attributes)
7175 location_t saved_loc = input_location;
7177 /* Now that we've got all the field declarations, reverse everything
7178 as necessary. */
7179 unreverse_member_declarations (t);
7181 cplus_decl_attributes (&t, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
7182 fixup_attribute_variants (t);
7184 /* Nadger the current location so that diagnostics point to the start of
7185 the struct, not the end. */
7186 input_location = DECL_SOURCE_LOCATION (TYPE_NAME (t));
7188 if (processing_template_decl)
7190 tree x;
7192 finish_struct_methods (t);
7193 TYPE_SIZE (t) = bitsize_zero_node;
7194 TYPE_SIZE_UNIT (t) = size_zero_node;
7196 /* We need to emit an error message if this type was used as a parameter
7197 and it is an abstract type, even if it is a template. We construct
7198 a simple CLASSTYPE_PURE_VIRTUALS list without taking bases into
7199 account and we call complete_vars with this type, which will check
7200 the PARM_DECLS. Note that while the type is being defined,
7201 CLASSTYPE_PURE_VIRTUALS contains the list of the inline friends
7202 (see CLASSTYPE_INLINE_FRIENDS) so we need to clear it. */
7203 CLASSTYPE_PURE_VIRTUALS (t) = NULL;
7204 for (x = TYPE_METHODS (t); x; x = DECL_CHAIN (x))
7205 if (DECL_PURE_VIRTUAL_P (x))
7206 vec_safe_push (CLASSTYPE_PURE_VIRTUALS (t), x);
7207 complete_vars (t);
7208 /* We need to add the target functions to the CLASSTYPE_METHOD_VEC if
7209 an enclosing scope is a template class, so that this function be
7210 found by lookup_fnfields_1 when the using declaration is not
7211 instantiated yet. */
7212 for (x = TYPE_FIELDS (t); x; x = DECL_CHAIN (x))
7213 if (TREE_CODE (x) == USING_DECL)
7215 tree fn = strip_using_decl (x);
7216 if (is_overloaded_fn (fn))
7217 for (; fn; fn = OVL_NEXT (fn))
7218 add_method (t, OVL_CURRENT (fn), x);
7221 /* Remember current #pragma pack value. */
7222 TYPE_PRECISION (t) = maximum_field_alignment;
7224 /* Fix up any variants we've already built. */
7225 for (x = TYPE_NEXT_VARIANT (t); x; x = TYPE_NEXT_VARIANT (x))
7227 TYPE_SIZE (x) = TYPE_SIZE (t);
7228 TYPE_SIZE_UNIT (x) = TYPE_SIZE_UNIT (t);
7229 TYPE_FIELDS (x) = TYPE_FIELDS (t);
7230 TYPE_METHODS (x) = TYPE_METHODS (t);
7233 else
7234 finish_struct_1 (t);
7236 if (is_std_init_list (t))
7238 /* People keep complaining that the compiler crashes on an invalid
7239 definition of initializer_list, so I guess we should explicitly
7240 reject it. What the compiler internals care about is that it's a
7241 template and has a pointer field followed by an integer field. */
7242 bool ok = false;
7243 if (processing_template_decl)
7245 tree f = next_initializable_field (TYPE_FIELDS (t));
7246 if (f && TREE_CODE (TREE_TYPE (f)) == POINTER_TYPE)
7248 f = next_initializable_field (DECL_CHAIN (f));
7249 if (f && same_type_p (TREE_TYPE (f), size_type_node))
7250 ok = true;
7253 if (!ok)
7254 fatal_error (input_location,
7255 "definition of std::initializer_list does not match "
7256 "#include <initializer_list>");
7259 input_location = saved_loc;
7261 TYPE_BEING_DEFINED (t) = 0;
7263 if (current_class_type)
7264 popclass ();
7265 else
7266 error ("trying to finish struct, but kicked out due to previous parse errors");
7268 if (processing_template_decl && at_function_scope_p ()
7269 /* Lambdas are defined by the LAMBDA_EXPR. */
7270 && !LAMBDA_TYPE_P (t))
7271 add_stmt (build_min (TAG_DEFN, t));
7273 return t;
7276 /* Hash table to avoid endless recursion when handling references. */
7277 static hash_table<nofree_ptr_hash<tree_node> > *fixed_type_or_null_ref_ht;
7279 /* Return the dynamic type of INSTANCE, if known.
7280 Used to determine whether the virtual function table is needed
7281 or not.
7283 *NONNULL is set iff INSTANCE can be known to be nonnull, regardless
7284 of our knowledge of its type. *NONNULL should be initialized
7285 before this function is called. */
7287 static tree
7288 fixed_type_or_null (tree instance, int *nonnull, int *cdtorp)
7290 #define RECUR(T) fixed_type_or_null((T), nonnull, cdtorp)
7292 switch (TREE_CODE (instance))
7294 case INDIRECT_REF:
7295 if (POINTER_TYPE_P (TREE_TYPE (instance)))
7296 return NULL_TREE;
7297 else
7298 return RECUR (TREE_OPERAND (instance, 0));
7300 case CALL_EXPR:
7301 /* This is a call to a constructor, hence it's never zero. */
7302 if (TREE_HAS_CONSTRUCTOR (instance))
7304 if (nonnull)
7305 *nonnull = 1;
7306 return TREE_TYPE (instance);
7308 return NULL_TREE;
7310 case SAVE_EXPR:
7311 /* This is a call to a constructor, hence it's never zero. */
7312 if (TREE_HAS_CONSTRUCTOR (instance))
7314 if (nonnull)
7315 *nonnull = 1;
7316 return TREE_TYPE (instance);
7318 return RECUR (TREE_OPERAND (instance, 0));
7320 case POINTER_PLUS_EXPR:
7321 case PLUS_EXPR:
7322 case MINUS_EXPR:
7323 if (TREE_CODE (TREE_OPERAND (instance, 0)) == ADDR_EXPR)
7324 return RECUR (TREE_OPERAND (instance, 0));
7325 if (TREE_CODE (TREE_OPERAND (instance, 1)) == INTEGER_CST)
7326 /* Propagate nonnull. */
7327 return RECUR (TREE_OPERAND (instance, 0));
7329 return NULL_TREE;
7331 CASE_CONVERT:
7332 return RECUR (TREE_OPERAND (instance, 0));
7334 case ADDR_EXPR:
7335 instance = TREE_OPERAND (instance, 0);
7336 if (nonnull)
7338 /* Just because we see an ADDR_EXPR doesn't mean we're dealing
7339 with a real object -- given &p->f, p can still be null. */
7340 tree t = get_base_address (instance);
7341 /* ??? Probably should check DECL_WEAK here. */
7342 if (t && DECL_P (t))
7343 *nonnull = 1;
7345 return RECUR (instance);
7347 case COMPONENT_REF:
7348 /* If this component is really a base class reference, then the field
7349 itself isn't definitive. */
7350 if (DECL_FIELD_IS_BASE (TREE_OPERAND (instance, 1)))
7351 return RECUR (TREE_OPERAND (instance, 0));
7352 return RECUR (TREE_OPERAND (instance, 1));
7354 case VAR_DECL:
7355 case FIELD_DECL:
7356 if (TREE_CODE (TREE_TYPE (instance)) == ARRAY_TYPE
7357 && MAYBE_CLASS_TYPE_P (TREE_TYPE (TREE_TYPE (instance))))
7359 if (nonnull)
7360 *nonnull = 1;
7361 return TREE_TYPE (TREE_TYPE (instance));
7363 /* fall through... */
7364 case TARGET_EXPR:
7365 case PARM_DECL:
7366 case RESULT_DECL:
7367 if (MAYBE_CLASS_TYPE_P (TREE_TYPE (instance)))
7369 if (nonnull)
7370 *nonnull = 1;
7371 return TREE_TYPE (instance);
7373 else if (instance == current_class_ptr)
7375 if (nonnull)
7376 *nonnull = 1;
7378 /* if we're in a ctor or dtor, we know our type. If
7379 current_class_ptr is set but we aren't in a function, we're in
7380 an NSDMI (and therefore a constructor). */
7381 if (current_scope () != current_function_decl
7382 || (DECL_LANG_SPECIFIC (current_function_decl)
7383 && (DECL_CONSTRUCTOR_P (current_function_decl)
7384 || DECL_DESTRUCTOR_P (current_function_decl))))
7386 if (cdtorp)
7387 *cdtorp = 1;
7388 return TREE_TYPE (TREE_TYPE (instance));
7391 else if (TREE_CODE (TREE_TYPE (instance)) == REFERENCE_TYPE)
7393 /* We only need one hash table because it is always left empty. */
7394 if (!fixed_type_or_null_ref_ht)
7395 fixed_type_or_null_ref_ht
7396 = new hash_table<nofree_ptr_hash<tree_node> > (37);
7398 /* Reference variables should be references to objects. */
7399 if (nonnull)
7400 *nonnull = 1;
7402 /* Enter the INSTANCE in a table to prevent recursion; a
7403 variable's initializer may refer to the variable
7404 itself. */
7405 if (VAR_P (instance)
7406 && DECL_INITIAL (instance)
7407 && !type_dependent_expression_p_push (DECL_INITIAL (instance))
7408 && !fixed_type_or_null_ref_ht->find (instance))
7410 tree type;
7411 tree_node **slot;
7413 slot = fixed_type_or_null_ref_ht->find_slot (instance, INSERT);
7414 *slot = instance;
7415 type = RECUR (DECL_INITIAL (instance));
7416 fixed_type_or_null_ref_ht->remove_elt (instance);
7418 return type;
7421 return NULL_TREE;
7423 default:
7424 return NULL_TREE;
7426 #undef RECUR
7429 /* Return nonzero if the dynamic type of INSTANCE is known, and
7430 equivalent to the static type. We also handle the case where
7431 INSTANCE is really a pointer. Return negative if this is a
7432 ctor/dtor. There the dynamic type is known, but this might not be
7433 the most derived base of the original object, and hence virtual
7434 bases may not be laid out according to this type.
7436 Used to determine whether the virtual function table is needed
7437 or not.
7439 *NONNULL is set iff INSTANCE can be known to be nonnull, regardless
7440 of our knowledge of its type. *NONNULL should be initialized
7441 before this function is called. */
7444 resolves_to_fixed_type_p (tree instance, int* nonnull)
7446 tree t = TREE_TYPE (instance);
7447 int cdtorp = 0;
7448 tree fixed;
7450 /* processing_template_decl can be false in a template if we're in
7451 instantiate_non_dependent_expr, but we still want to suppress
7452 this check. */
7453 if (in_template_function ())
7455 /* In a template we only care about the type of the result. */
7456 if (nonnull)
7457 *nonnull = true;
7458 return true;
7461 fixed = fixed_type_or_null (instance, nonnull, &cdtorp);
7462 if (fixed == NULL_TREE)
7463 return 0;
7464 if (POINTER_TYPE_P (t))
7465 t = TREE_TYPE (t);
7466 if (!same_type_ignoring_top_level_qualifiers_p (t, fixed))
7467 return 0;
7468 return cdtorp ? -1 : 1;
7472 void
7473 init_class_processing (void)
7475 current_class_depth = 0;
7476 current_class_stack_size = 10;
7477 current_class_stack
7478 = XNEWVEC (struct class_stack_node, current_class_stack_size);
7479 vec_alloc (local_classes, 8);
7480 sizeof_biggest_empty_class = size_zero_node;
7482 ridpointers[(int) RID_PUBLIC] = access_public_node;
7483 ridpointers[(int) RID_PRIVATE] = access_private_node;
7484 ridpointers[(int) RID_PROTECTED] = access_protected_node;
7487 /* Restore the cached PREVIOUS_CLASS_LEVEL. */
7489 static void
7490 restore_class_cache (void)
7492 tree type;
7494 /* We are re-entering the same class we just left, so we don't
7495 have to search the whole inheritance matrix to find all the
7496 decls to bind again. Instead, we install the cached
7497 class_shadowed list and walk through it binding names. */
7498 push_binding_level (previous_class_level);
7499 class_binding_level = previous_class_level;
7500 /* Restore IDENTIFIER_TYPE_VALUE. */
7501 for (type = class_binding_level->type_shadowed;
7502 type;
7503 type = TREE_CHAIN (type))
7504 SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (type), TREE_TYPE (type));
7507 /* Set global variables CURRENT_CLASS_NAME and CURRENT_CLASS_TYPE as
7508 appropriate for TYPE.
7510 So that we may avoid calls to lookup_name, we cache the _TYPE
7511 nodes of local TYPE_DECLs in the TREE_TYPE field of the name.
7513 For multiple inheritance, we perform a two-pass depth-first search
7514 of the type lattice. */
7516 void
7517 pushclass (tree type)
7519 class_stack_node_t csn;
7521 type = TYPE_MAIN_VARIANT (type);
7523 /* Make sure there is enough room for the new entry on the stack. */
7524 if (current_class_depth + 1 >= current_class_stack_size)
7526 current_class_stack_size *= 2;
7527 current_class_stack
7528 = XRESIZEVEC (struct class_stack_node, current_class_stack,
7529 current_class_stack_size);
7532 /* Insert a new entry on the class stack. */
7533 csn = current_class_stack + current_class_depth;
7534 csn->name = current_class_name;
7535 csn->type = current_class_type;
7536 csn->access = current_access_specifier;
7537 csn->names_used = 0;
7538 csn->hidden = 0;
7539 current_class_depth++;
7541 /* Now set up the new type. */
7542 current_class_name = TYPE_NAME (type);
7543 if (TREE_CODE (current_class_name) == TYPE_DECL)
7544 current_class_name = DECL_NAME (current_class_name);
7545 current_class_type = type;
7547 /* By default, things in classes are private, while things in
7548 structures or unions are public. */
7549 current_access_specifier = (CLASSTYPE_DECLARED_CLASS (type)
7550 ? access_private_node
7551 : access_public_node);
7553 if (previous_class_level
7554 && type != previous_class_level->this_entity
7555 && current_class_depth == 1)
7557 /* Forcibly remove any old class remnants. */
7558 invalidate_class_lookup_cache ();
7561 if (!previous_class_level
7562 || type != previous_class_level->this_entity
7563 || current_class_depth > 1)
7564 pushlevel_class ();
7565 else
7566 restore_class_cache ();
7569 /* When we exit a toplevel class scope, we save its binding level so
7570 that we can restore it quickly. Here, we've entered some other
7571 class, so we must invalidate our cache. */
7573 void
7574 invalidate_class_lookup_cache (void)
7576 previous_class_level = NULL;
7579 /* Get out of the current class scope. If we were in a class scope
7580 previously, that is the one popped to. */
7582 void
7583 popclass (void)
7585 poplevel_class ();
7587 current_class_depth--;
7588 current_class_name = current_class_stack[current_class_depth].name;
7589 current_class_type = current_class_stack[current_class_depth].type;
7590 current_access_specifier = current_class_stack[current_class_depth].access;
7591 if (current_class_stack[current_class_depth].names_used)
7592 splay_tree_delete (current_class_stack[current_class_depth].names_used);
7595 /* Mark the top of the class stack as hidden. */
7597 void
7598 push_class_stack (void)
7600 if (current_class_depth)
7601 ++current_class_stack[current_class_depth - 1].hidden;
7604 /* Mark the top of the class stack as un-hidden. */
7606 void
7607 pop_class_stack (void)
7609 if (current_class_depth)
7610 --current_class_stack[current_class_depth - 1].hidden;
7613 /* Returns 1 if the class type currently being defined is either T or
7614 a nested type of T. Returns the type from the current_class_stack,
7615 which might be equivalent to but not equal to T in case of
7616 constrained partial specializations. */
7618 tree
7619 currently_open_class (tree t)
7621 int i;
7623 if (!CLASS_TYPE_P (t))
7624 return NULL_TREE;
7626 t = TYPE_MAIN_VARIANT (t);
7628 /* We start looking from 1 because entry 0 is from global scope,
7629 and has no type. */
7630 for (i = current_class_depth; i > 0; --i)
7632 tree c;
7633 if (i == current_class_depth)
7634 c = current_class_type;
7635 else
7637 if (current_class_stack[i].hidden)
7638 break;
7639 c = current_class_stack[i].type;
7641 if (!c)
7642 continue;
7643 if (same_type_p (c, t))
7644 return c;
7646 return NULL_TREE;
7649 /* If either current_class_type or one of its enclosing classes are derived
7650 from T, return the appropriate type. Used to determine how we found
7651 something via unqualified lookup. */
7653 tree
7654 currently_open_derived_class (tree t)
7656 int i;
7658 /* The bases of a dependent type are unknown. */
7659 if (dependent_type_p (t))
7660 return NULL_TREE;
7662 if (!current_class_type)
7663 return NULL_TREE;
7665 if (DERIVED_FROM_P (t, current_class_type))
7666 return current_class_type;
7668 for (i = current_class_depth - 1; i > 0; --i)
7670 if (current_class_stack[i].hidden)
7671 break;
7672 if (DERIVED_FROM_P (t, current_class_stack[i].type))
7673 return current_class_stack[i].type;
7676 return NULL_TREE;
7679 /* Return the outermost enclosing class type that is still open, or
7680 NULL_TREE. */
7682 tree
7683 outermost_open_class (void)
7685 if (!current_class_type)
7686 return NULL_TREE;
7687 tree r = NULL_TREE;
7688 if (TYPE_BEING_DEFINED (current_class_type))
7689 r = current_class_type;
7690 for (int i = current_class_depth - 1; i > 0; --i)
7692 if (current_class_stack[i].hidden)
7693 break;
7694 tree t = current_class_stack[i].type;
7695 if (!TYPE_BEING_DEFINED (t))
7696 break;
7697 r = t;
7699 return r;
7702 /* Returns the innermost class type which is not a lambda closure type. */
7704 tree
7705 current_nonlambda_class_type (void)
7707 int i;
7709 /* We start looking from 1 because entry 0 is from global scope,
7710 and has no type. */
7711 for (i = current_class_depth; i > 0; --i)
7713 tree c;
7714 if (i == current_class_depth)
7715 c = current_class_type;
7716 else
7718 if (current_class_stack[i].hidden)
7719 break;
7720 c = current_class_stack[i].type;
7722 if (!c)
7723 continue;
7724 if (!LAMBDA_TYPE_P (c))
7725 return c;
7727 return NULL_TREE;
7730 /* When entering a class scope, all enclosing class scopes' names with
7731 static meaning (static variables, static functions, types and
7732 enumerators) have to be visible. This recursive function calls
7733 pushclass for all enclosing class contexts until global or a local
7734 scope is reached. TYPE is the enclosed class. */
7736 void
7737 push_nested_class (tree type)
7739 /* A namespace might be passed in error cases, like A::B:C. */
7740 if (type == NULL_TREE
7741 || !CLASS_TYPE_P (type))
7742 return;
7744 push_nested_class (DECL_CONTEXT (TYPE_MAIN_DECL (type)));
7746 pushclass (type);
7749 /* Undoes a push_nested_class call. */
7751 void
7752 pop_nested_class (void)
7754 tree context = DECL_CONTEXT (TYPE_MAIN_DECL (current_class_type));
7756 popclass ();
7757 if (context && CLASS_TYPE_P (context))
7758 pop_nested_class ();
7761 /* Returns the number of extern "LANG" blocks we are nested within. */
7764 current_lang_depth (void)
7766 return vec_safe_length (current_lang_base);
7769 /* Set global variables CURRENT_LANG_NAME to appropriate value
7770 so that behavior of name-mangling machinery is correct. */
7772 void
7773 push_lang_context (tree name)
7775 vec_safe_push (current_lang_base, current_lang_name);
7777 if (name == lang_name_cplusplus)
7779 current_lang_name = name;
7781 else if (name == lang_name_java)
7783 current_lang_name = name;
7784 /* DECL_IGNORED_P is initially set for these types, to avoid clutter.
7785 (See record_builtin_java_type in decl.c.) However, that causes
7786 incorrect debug entries if these types are actually used.
7787 So we re-enable debug output after extern "Java". */
7788 DECL_IGNORED_P (TYPE_NAME (java_byte_type_node)) = 0;
7789 DECL_IGNORED_P (TYPE_NAME (java_short_type_node)) = 0;
7790 DECL_IGNORED_P (TYPE_NAME (java_int_type_node)) = 0;
7791 DECL_IGNORED_P (TYPE_NAME (java_long_type_node)) = 0;
7792 DECL_IGNORED_P (TYPE_NAME (java_float_type_node)) = 0;
7793 DECL_IGNORED_P (TYPE_NAME (java_double_type_node)) = 0;
7794 DECL_IGNORED_P (TYPE_NAME (java_char_type_node)) = 0;
7795 DECL_IGNORED_P (TYPE_NAME (java_boolean_type_node)) = 0;
7797 else if (name == lang_name_c)
7799 current_lang_name = name;
7801 else
7802 error ("language string %<\"%E\"%> not recognized", name);
7805 /* Get out of the current language scope. */
7807 void
7808 pop_lang_context (void)
7810 current_lang_name = current_lang_base->pop ();
7813 /* Type instantiation routines. */
7815 /* Given an OVERLOAD and a TARGET_TYPE, return the function that
7816 matches the TARGET_TYPE. If there is no satisfactory match, return
7817 error_mark_node, and issue an error & warning messages under
7818 control of FLAGS. Permit pointers to member function if FLAGS
7819 permits. If TEMPLATE_ONLY, the name of the overloaded function was
7820 a template-id, and EXPLICIT_TARGS are the explicitly provided
7821 template arguments.
7823 If OVERLOAD is for one or more member functions, then ACCESS_PATH
7824 is the base path used to reference those member functions. If
7825 the address is resolved to a member function, access checks will be
7826 performed and errors issued if appropriate. */
7828 static tree
7829 resolve_address_of_overloaded_function (tree target_type,
7830 tree overload,
7831 tsubst_flags_t complain,
7832 bool template_only,
7833 tree explicit_targs,
7834 tree access_path)
7836 /* Here's what the standard says:
7838 [over.over]
7840 If the name is a function template, template argument deduction
7841 is done, and if the argument deduction succeeds, the deduced
7842 arguments are used to generate a single template function, which
7843 is added to the set of overloaded functions considered.
7845 Non-member functions and static member functions match targets of
7846 type "pointer-to-function" or "reference-to-function." Nonstatic
7847 member functions match targets of type "pointer-to-member
7848 function;" the function type of the pointer to member is used to
7849 select the member function from the set of overloaded member
7850 functions. If a nonstatic member function is selected, the
7851 reference to the overloaded function name is required to have the
7852 form of a pointer to member as described in 5.3.1.
7854 If more than one function is selected, any template functions in
7855 the set are eliminated if the set also contains a non-template
7856 function, and any given template function is eliminated if the
7857 set contains a second template function that is more specialized
7858 than the first according to the partial ordering rules 14.5.5.2.
7859 After such eliminations, if any, there shall remain exactly one
7860 selected function. */
7862 int is_ptrmem = 0;
7863 /* We store the matches in a TREE_LIST rooted here. The functions
7864 are the TREE_PURPOSE, not the TREE_VALUE, in this list, for easy
7865 interoperability with most_specialized_instantiation. */
7866 tree matches = NULL_TREE;
7867 tree fn;
7868 tree target_fn_type;
7870 /* By the time we get here, we should be seeing only real
7871 pointer-to-member types, not the internal POINTER_TYPE to
7872 METHOD_TYPE representation. */
7873 gcc_assert (!TYPE_PTR_P (target_type)
7874 || TREE_CODE (TREE_TYPE (target_type)) != METHOD_TYPE);
7876 gcc_assert (is_overloaded_fn (overload));
7878 /* Check that the TARGET_TYPE is reasonable. */
7879 if (TYPE_PTRFN_P (target_type)
7880 || TYPE_REFFN_P (target_type))
7881 /* This is OK. */;
7882 else if (TYPE_PTRMEMFUNC_P (target_type))
7883 /* This is OK, too. */
7884 is_ptrmem = 1;
7885 else if (TREE_CODE (target_type) == FUNCTION_TYPE)
7886 /* This is OK, too. This comes from a conversion to reference
7887 type. */
7888 target_type = build_reference_type (target_type);
7889 else
7891 if (complain & tf_error)
7892 error ("cannot resolve overloaded function %qD based on"
7893 " conversion to type %qT",
7894 DECL_NAME (OVL_FUNCTION (overload)), target_type);
7895 return error_mark_node;
7898 /* Non-member functions and static member functions match targets of type
7899 "pointer-to-function" or "reference-to-function." Nonstatic member
7900 functions match targets of type "pointer-to-member-function;" the
7901 function type of the pointer to member is used to select the member
7902 function from the set of overloaded member functions.
7904 So figure out the FUNCTION_TYPE that we want to match against. */
7905 target_fn_type = static_fn_type (target_type);
7907 /* If we can find a non-template function that matches, we can just
7908 use it. There's no point in generating template instantiations
7909 if we're just going to throw them out anyhow. But, of course, we
7910 can only do this when we don't *need* a template function. */
7911 if (!template_only)
7913 tree fns;
7915 for (fns = overload; fns; fns = OVL_NEXT (fns))
7917 tree fn = OVL_CURRENT (fns);
7919 if (TREE_CODE (fn) == TEMPLATE_DECL)
7920 /* We're not looking for templates just yet. */
7921 continue;
7923 if ((TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
7924 != is_ptrmem)
7925 /* We're looking for a non-static member, and this isn't
7926 one, or vice versa. */
7927 continue;
7929 /* Ignore functions which haven't been explicitly
7930 declared. */
7931 if (DECL_ANTICIPATED (fn))
7932 continue;
7934 /* See if there's a match. */
7935 tree fntype = static_fn_type (fn);
7936 if (same_type_p (target_fn_type, fntype)
7937 || can_convert_tx_safety (target_fn_type, fntype))
7938 matches = tree_cons (fn, NULL_TREE, matches);
7942 /* Now, if we've already got a match (or matches), there's no need
7943 to proceed to the template functions. But, if we don't have a
7944 match we need to look at them, too. */
7945 if (!matches)
7947 tree target_arg_types;
7948 tree target_ret_type;
7949 tree fns;
7950 tree *args;
7951 unsigned int nargs, ia;
7952 tree arg;
7954 target_arg_types = TYPE_ARG_TYPES (target_fn_type);
7955 target_ret_type = TREE_TYPE (target_fn_type);
7957 nargs = list_length (target_arg_types);
7958 args = XALLOCAVEC (tree, nargs);
7959 for (arg = target_arg_types, ia = 0;
7960 arg != NULL_TREE && arg != void_list_node;
7961 arg = TREE_CHAIN (arg), ++ia)
7962 args[ia] = TREE_VALUE (arg);
7963 nargs = ia;
7965 for (fns = overload; fns; fns = OVL_NEXT (fns))
7967 tree fn = OVL_CURRENT (fns);
7968 tree instantiation;
7969 tree targs;
7971 if (TREE_CODE (fn) != TEMPLATE_DECL)
7972 /* We're only looking for templates. */
7973 continue;
7975 if ((TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
7976 != is_ptrmem)
7977 /* We're not looking for a non-static member, and this is
7978 one, or vice versa. */
7979 continue;
7981 tree ret = target_ret_type;
7983 /* If the template has a deduced return type, don't expose it to
7984 template argument deduction. */
7985 if (undeduced_auto_decl (fn))
7986 ret = NULL_TREE;
7988 /* Try to do argument deduction. */
7989 targs = make_tree_vec (DECL_NTPARMS (fn));
7990 instantiation = fn_type_unification (fn, explicit_targs, targs, args,
7991 nargs, ret,
7992 DEDUCE_EXACT, LOOKUP_NORMAL,
7993 false, false);
7994 if (instantiation == error_mark_node)
7995 /* Instantiation failed. */
7996 continue;
7998 /* Constraints must be satisfied. This is done before
7999 return type deduction since that instantiates the
8000 function. */
8001 if (flag_concepts && !constraints_satisfied_p (instantiation))
8002 continue;
8004 /* And now force instantiation to do return type deduction. */
8005 if (undeduced_auto_decl (instantiation))
8007 ++function_depth;
8008 instantiate_decl (instantiation, /*defer*/false, /*class*/false);
8009 --function_depth;
8011 require_deduced_type (instantiation);
8014 /* See if there's a match. */
8015 tree fntype = static_fn_type (instantiation);
8016 if (same_type_p (target_fn_type, fntype)
8017 || can_convert_tx_safety (target_fn_type, fntype))
8018 matches = tree_cons (instantiation, fn, matches);
8021 /* Now, remove all but the most specialized of the matches. */
8022 if (matches)
8024 tree match = most_specialized_instantiation (matches);
8026 if (match != error_mark_node)
8027 matches = tree_cons (TREE_PURPOSE (match),
8028 NULL_TREE,
8029 NULL_TREE);
8033 /* Now we should have exactly one function in MATCHES. */
8034 if (matches == NULL_TREE)
8036 /* There were *no* matches. */
8037 if (complain & tf_error)
8039 error ("no matches converting function %qD to type %q#T",
8040 DECL_NAME (OVL_CURRENT (overload)),
8041 target_type);
8043 print_candidates (overload);
8045 return error_mark_node;
8047 else if (TREE_CHAIN (matches))
8049 /* There were too many matches. First check if they're all
8050 the same function. */
8051 tree match = NULL_TREE;
8053 fn = TREE_PURPOSE (matches);
8055 /* For multi-versioned functions, more than one match is just fine and
8056 decls_match will return false as they are different. */
8057 for (match = TREE_CHAIN (matches); match; match = TREE_CHAIN (match))
8058 if (!decls_match (fn, TREE_PURPOSE (match))
8059 && !targetm.target_option.function_versions
8060 (fn, TREE_PURPOSE (match)))
8061 break;
8063 if (match)
8065 if (complain & tf_error)
8067 error ("converting overloaded function %qD to type %q#T is ambiguous",
8068 DECL_NAME (OVL_FUNCTION (overload)),
8069 target_type);
8071 /* Since print_candidates expects the functions in the
8072 TREE_VALUE slot, we flip them here. */
8073 for (match = matches; match; match = TREE_CHAIN (match))
8074 TREE_VALUE (match) = TREE_PURPOSE (match);
8076 print_candidates (matches);
8079 return error_mark_node;
8083 /* Good, exactly one match. Now, convert it to the correct type. */
8084 fn = TREE_PURPOSE (matches);
8086 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
8087 && !(complain & tf_ptrmem_ok) && !flag_ms_extensions)
8089 static int explained;
8091 if (!(complain & tf_error))
8092 return error_mark_node;
8094 permerror (input_location, "assuming pointer to member %qD", fn);
8095 if (!explained)
8097 inform (input_location, "(a pointer to member can only be formed with %<&%E%>)", fn);
8098 explained = 1;
8102 /* If a pointer to a function that is multi-versioned is requested, the
8103 pointer to the dispatcher function is returned instead. This works
8104 well because indirectly calling the function will dispatch the right
8105 function version at run-time. */
8106 if (DECL_FUNCTION_VERSIONED (fn))
8108 fn = get_function_version_dispatcher (fn);
8109 if (fn == NULL)
8110 return error_mark_node;
8111 /* Mark all the versions corresponding to the dispatcher as used. */
8112 if (!(complain & tf_conv))
8113 mark_versions_used (fn);
8116 /* If we're doing overload resolution purely for the purpose of
8117 determining conversion sequences, we should not consider the
8118 function used. If this conversion sequence is selected, the
8119 function will be marked as used at this point. */
8120 if (!(complain & tf_conv))
8122 /* Make =delete work with SFINAE. */
8123 if (DECL_DELETED_FN (fn) && !(complain & tf_error))
8124 return error_mark_node;
8125 if (!mark_used (fn, complain) && !(complain & tf_error))
8126 return error_mark_node;
8129 /* We could not check access to member functions when this
8130 expression was originally created since we did not know at that
8131 time to which function the expression referred. */
8132 if (DECL_FUNCTION_MEMBER_P (fn))
8134 gcc_assert (access_path);
8135 perform_or_defer_access_check (access_path, fn, fn, complain);
8138 if (TYPE_PTRFN_P (target_type) || TYPE_PTRMEMFUNC_P (target_type))
8139 return cp_build_addr_expr (fn, complain);
8140 else
8142 /* The target must be a REFERENCE_TYPE. Above, cp_build_unary_op
8143 will mark the function as addressed, but here we must do it
8144 explicitly. */
8145 cxx_mark_addressable (fn);
8147 return fn;
8151 /* This function will instantiate the type of the expression given in
8152 RHS to match the type of LHSTYPE. If errors exist, then return
8153 error_mark_node. COMPLAIN is a bit mask. If TF_ERROR is set, then
8154 we complain on errors. If we are not complaining, never modify rhs,
8155 as overload resolution wants to try many possible instantiations, in
8156 the hope that at least one will work.
8158 For non-recursive calls, LHSTYPE should be a function, pointer to
8159 function, or a pointer to member function. */
8161 tree
8162 instantiate_type (tree lhstype, tree rhs, tsubst_flags_t complain)
8164 tsubst_flags_t complain_in = complain;
8165 tree access_path = NULL_TREE;
8167 complain &= ~tf_ptrmem_ok;
8169 if (lhstype == unknown_type_node)
8171 if (complain & tf_error)
8172 error ("not enough type information");
8173 return error_mark_node;
8176 if (TREE_TYPE (rhs) != NULL_TREE && ! (type_unknown_p (rhs)))
8178 tree fntype = non_reference (lhstype);
8179 if (same_type_p (fntype, TREE_TYPE (rhs)))
8180 return rhs;
8181 if (flag_ms_extensions
8182 && TYPE_PTRMEMFUNC_P (fntype)
8183 && !TYPE_PTRMEMFUNC_P (TREE_TYPE (rhs)))
8184 /* Microsoft allows `A::f' to be resolved to a
8185 pointer-to-member. */
8187 else
8189 if (complain & tf_error)
8190 error ("cannot convert %qE from type %qT to type %qT",
8191 rhs, TREE_TYPE (rhs), fntype);
8192 return error_mark_node;
8196 if (BASELINK_P (rhs))
8198 access_path = BASELINK_ACCESS_BINFO (rhs);
8199 rhs = BASELINK_FUNCTIONS (rhs);
8202 /* If we are in a template, and have a NON_DEPENDENT_EXPR, we cannot
8203 deduce any type information. */
8204 if (TREE_CODE (rhs) == NON_DEPENDENT_EXPR)
8206 if (complain & tf_error)
8207 error ("not enough type information");
8208 return error_mark_node;
8211 /* There only a few kinds of expressions that may have a type
8212 dependent on overload resolution. */
8213 gcc_assert (TREE_CODE (rhs) == ADDR_EXPR
8214 || TREE_CODE (rhs) == COMPONENT_REF
8215 || is_overloaded_fn (rhs)
8216 || (flag_ms_extensions && TREE_CODE (rhs) == FUNCTION_DECL));
8218 /* This should really only be used when attempting to distinguish
8219 what sort of a pointer to function we have. For now, any
8220 arithmetic operation which is not supported on pointers
8221 is rejected as an error. */
8223 switch (TREE_CODE (rhs))
8225 case COMPONENT_REF:
8227 tree member = TREE_OPERAND (rhs, 1);
8229 member = instantiate_type (lhstype, member, complain);
8230 if (member != error_mark_node
8231 && TREE_SIDE_EFFECTS (TREE_OPERAND (rhs, 0)))
8232 /* Do not lose object's side effects. */
8233 return build2 (COMPOUND_EXPR, TREE_TYPE (member),
8234 TREE_OPERAND (rhs, 0), member);
8235 return member;
8238 case OFFSET_REF:
8239 rhs = TREE_OPERAND (rhs, 1);
8240 if (BASELINK_P (rhs))
8241 return instantiate_type (lhstype, rhs, complain_in);
8243 /* This can happen if we are forming a pointer-to-member for a
8244 member template. */
8245 gcc_assert (TREE_CODE (rhs) == TEMPLATE_ID_EXPR);
8247 /* Fall through. */
8249 case TEMPLATE_ID_EXPR:
8251 tree fns = TREE_OPERAND (rhs, 0);
8252 tree args = TREE_OPERAND (rhs, 1);
8254 return
8255 resolve_address_of_overloaded_function (lhstype, fns, complain_in,
8256 /*template_only=*/true,
8257 args, access_path);
8260 case OVERLOAD:
8261 case FUNCTION_DECL:
8262 return
8263 resolve_address_of_overloaded_function (lhstype, rhs, complain_in,
8264 /*template_only=*/false,
8265 /*explicit_targs=*/NULL_TREE,
8266 access_path);
8268 case ADDR_EXPR:
8270 if (PTRMEM_OK_P (rhs))
8271 complain |= tf_ptrmem_ok;
8273 return instantiate_type (lhstype, TREE_OPERAND (rhs, 0), complain);
8276 case ERROR_MARK:
8277 return error_mark_node;
8279 default:
8280 gcc_unreachable ();
8282 return error_mark_node;
8285 /* Return the name of the virtual function pointer field
8286 (as an IDENTIFIER_NODE) for the given TYPE. Note that
8287 this may have to look back through base types to find the
8288 ultimate field name. (For single inheritance, these could
8289 all be the same name. Who knows for multiple inheritance). */
8291 static tree
8292 get_vfield_name (tree type)
8294 tree binfo, base_binfo;
8295 char *buf;
8297 for (binfo = TYPE_BINFO (type);
8298 BINFO_N_BASE_BINFOS (binfo);
8299 binfo = base_binfo)
8301 base_binfo = BINFO_BASE_BINFO (binfo, 0);
8303 if (BINFO_VIRTUAL_P (base_binfo)
8304 || !TYPE_CONTAINS_VPTR_P (BINFO_TYPE (base_binfo)))
8305 break;
8308 type = BINFO_TYPE (binfo);
8309 buf = (char *) alloca (sizeof (VFIELD_NAME_FORMAT)
8310 + TYPE_NAME_LENGTH (type) + 2);
8311 sprintf (buf, VFIELD_NAME_FORMAT,
8312 IDENTIFIER_POINTER (constructor_name (type)));
8313 return get_identifier (buf);
8316 void
8317 print_class_statistics (void)
8319 if (! GATHER_STATISTICS)
8320 return;
8322 fprintf (stderr, "convert_harshness = %d\n", n_convert_harshness);
8323 fprintf (stderr, "compute_conversion_costs = %d\n", n_compute_conversion_costs);
8324 if (n_vtables)
8326 fprintf (stderr, "vtables = %d; vtable searches = %d\n",
8327 n_vtables, n_vtable_searches);
8328 fprintf (stderr, "vtable entries = %d; vtable elems = %d\n",
8329 n_vtable_entries, n_vtable_elems);
8333 /* Build a dummy reference to ourselves so Derived::Base (and A::A) works,
8334 according to [class]:
8335 The class-name is also inserted
8336 into the scope of the class itself. For purposes of access checking,
8337 the inserted class name is treated as if it were a public member name. */
8339 void
8340 build_self_reference (void)
8342 tree name = constructor_name (current_class_type);
8343 tree value = build_lang_decl (TYPE_DECL, name, current_class_type);
8344 tree saved_cas;
8346 DECL_NONLOCAL (value) = 1;
8347 DECL_CONTEXT (value) = current_class_type;
8348 DECL_ARTIFICIAL (value) = 1;
8349 SET_DECL_SELF_REFERENCE_P (value);
8350 set_underlying_type (value);
8352 if (processing_template_decl)
8353 value = push_template_decl (value);
8355 saved_cas = current_access_specifier;
8356 current_access_specifier = access_public_node;
8357 finish_member_declaration (value);
8358 current_access_specifier = saved_cas;
8361 /* Returns 1 if TYPE contains only padding bytes. */
8364 is_empty_class (tree type)
8366 if (type == error_mark_node)
8367 return 0;
8369 if (! CLASS_TYPE_P (type))
8370 return 0;
8372 return CLASSTYPE_EMPTY_P (type);
8375 /* Returns true if TYPE contains no actual data, just various
8376 possible combinations of empty classes and possibly a vptr. */
8378 bool
8379 is_really_empty_class (tree type)
8381 if (CLASS_TYPE_P (type))
8383 tree field;
8384 tree binfo;
8385 tree base_binfo;
8386 int i;
8388 /* CLASSTYPE_EMPTY_P isn't set properly until the class is actually laid
8389 out, but we'd like to be able to check this before then. */
8390 if (COMPLETE_TYPE_P (type) && is_empty_class (type))
8391 return true;
8393 for (binfo = TYPE_BINFO (type), i = 0;
8394 BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
8395 if (!is_really_empty_class (BINFO_TYPE (base_binfo)))
8396 return false;
8397 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
8398 if (TREE_CODE (field) == FIELD_DECL
8399 && !DECL_ARTIFICIAL (field)
8400 && !is_really_empty_class (TREE_TYPE (field)))
8401 return false;
8402 return true;
8404 else if (TREE_CODE (type) == ARRAY_TYPE)
8405 return is_really_empty_class (TREE_TYPE (type));
8406 return false;
8409 /* Note that NAME was looked up while the current class was being
8410 defined and that the result of that lookup was DECL. */
8412 void
8413 maybe_note_name_used_in_class (tree name, tree decl)
8415 splay_tree names_used;
8417 /* If we're not defining a class, there's nothing to do. */
8418 if (!(innermost_scope_kind() == sk_class
8419 && TYPE_BEING_DEFINED (current_class_type)
8420 && !LAMBDA_TYPE_P (current_class_type)))
8421 return;
8423 /* If there's already a binding for this NAME, then we don't have
8424 anything to worry about. */
8425 if (lookup_member (current_class_type, name,
8426 /*protect=*/0, /*want_type=*/false, tf_warning_or_error))
8427 return;
8429 if (!current_class_stack[current_class_depth - 1].names_used)
8430 current_class_stack[current_class_depth - 1].names_used
8431 = splay_tree_new (splay_tree_compare_pointers, 0, 0);
8432 names_used = current_class_stack[current_class_depth - 1].names_used;
8434 splay_tree_insert (names_used,
8435 (splay_tree_key) name,
8436 (splay_tree_value) decl);
8439 /* Note that NAME was declared (as DECL) in the current class. Check
8440 to see that the declaration is valid. */
8442 void
8443 note_name_declared_in_class (tree name, tree decl)
8445 splay_tree names_used;
8446 splay_tree_node n;
8448 /* Look to see if we ever used this name. */
8449 names_used
8450 = current_class_stack[current_class_depth - 1].names_used;
8451 if (!names_used)
8452 return;
8453 /* The C language allows members to be declared with a type of the same
8454 name, and the C++ standard says this diagnostic is not required. So
8455 allow it in extern "C" blocks unless predantic is specified.
8456 Allow it in all cases if -ms-extensions is specified. */
8457 if ((!pedantic && current_lang_name == lang_name_c)
8458 || flag_ms_extensions)
8459 return;
8460 n = splay_tree_lookup (names_used, (splay_tree_key) name);
8461 if (n)
8463 /* [basic.scope.class]
8465 A name N used in a class S shall refer to the same declaration
8466 in its context and when re-evaluated in the completed scope of
8467 S. */
8468 permerror (input_location, "declaration of %q#D", decl);
8469 permerror (location_of ((tree) n->value),
8470 "changes meaning of %qD from %q#D",
8471 DECL_NAME (OVL_CURRENT (decl)), (tree) n->value);
8475 /* Returns the VAR_DECL for the complete vtable associated with BINFO.
8476 Secondary vtables are merged with primary vtables; this function
8477 will return the VAR_DECL for the primary vtable. */
8479 tree
8480 get_vtbl_decl_for_binfo (tree binfo)
8482 tree decl;
8484 decl = BINFO_VTABLE (binfo);
8485 if (decl && TREE_CODE (decl) == POINTER_PLUS_EXPR)
8487 gcc_assert (TREE_CODE (TREE_OPERAND (decl, 0)) == ADDR_EXPR);
8488 decl = TREE_OPERAND (TREE_OPERAND (decl, 0), 0);
8490 if (decl)
8491 gcc_assert (VAR_P (decl));
8492 return decl;
8496 /* Returns the binfo for the primary base of BINFO. If the resulting
8497 BINFO is a virtual base, and it is inherited elsewhere in the
8498 hierarchy, then the returned binfo might not be the primary base of
8499 BINFO in the complete object. Check BINFO_PRIMARY_P or
8500 BINFO_LOST_PRIMARY_P to be sure. */
8502 static tree
8503 get_primary_binfo (tree binfo)
8505 tree primary_base;
8507 primary_base = CLASSTYPE_PRIMARY_BINFO (BINFO_TYPE (binfo));
8508 if (!primary_base)
8509 return NULL_TREE;
8511 return copied_binfo (primary_base, binfo);
8514 /* As above, but iterate until we reach the binfo that actually provides the
8515 vptr for BINFO. */
8517 static tree
8518 most_primary_binfo (tree binfo)
8520 tree b = binfo;
8521 while (CLASSTYPE_HAS_PRIMARY_BASE_P (BINFO_TYPE (b))
8522 && !BINFO_LOST_PRIMARY_P (b))
8524 tree primary_base = get_primary_binfo (b);
8525 gcc_assert (BINFO_PRIMARY_P (primary_base)
8526 && BINFO_INHERITANCE_CHAIN (primary_base) == b);
8527 b = primary_base;
8529 return b;
8532 /* Returns true if BINFO gets its vptr from a virtual base of the most derived
8533 type. Note that the virtual inheritance might be above or below BINFO in
8534 the hierarchy. */
8536 bool
8537 vptr_via_virtual_p (tree binfo)
8539 if (TYPE_P (binfo))
8540 binfo = TYPE_BINFO (binfo);
8541 tree primary = most_primary_binfo (binfo);
8542 /* Don't limit binfo_via_virtual, we want to return true when BINFO itself is
8543 a morally virtual base. */
8544 tree virt = binfo_via_virtual (primary, NULL_TREE);
8545 return virt != NULL_TREE;
8548 /* If INDENTED_P is zero, indent to INDENT. Return nonzero. */
8550 static int
8551 maybe_indent_hierarchy (FILE * stream, int indent, int indented_p)
8553 if (!indented_p)
8554 fprintf (stream, "%*s", indent, "");
8555 return 1;
8558 /* Dump the offsets of all the bases rooted at BINFO to STREAM.
8559 INDENT should be zero when called from the top level; it is
8560 incremented recursively. IGO indicates the next expected BINFO in
8561 inheritance graph ordering. */
8563 static tree
8564 dump_class_hierarchy_r (FILE *stream,
8565 int flags,
8566 tree binfo,
8567 tree igo,
8568 int indent)
8570 int indented = 0;
8571 tree base_binfo;
8572 int i;
8574 indented = maybe_indent_hierarchy (stream, indent, 0);
8575 fprintf (stream, "%s (0x" HOST_WIDE_INT_PRINT_HEX ") ",
8576 type_as_string (BINFO_TYPE (binfo), TFF_PLAIN_IDENTIFIER),
8577 (HOST_WIDE_INT) (uintptr_t) binfo);
8578 if (binfo != igo)
8580 fprintf (stream, "alternative-path\n");
8581 return igo;
8583 igo = TREE_CHAIN (binfo);
8585 fprintf (stream, HOST_WIDE_INT_PRINT_DEC,
8586 tree_to_shwi (BINFO_OFFSET (binfo)));
8587 if (is_empty_class (BINFO_TYPE (binfo)))
8588 fprintf (stream, " empty");
8589 else if (CLASSTYPE_NEARLY_EMPTY_P (BINFO_TYPE (binfo)))
8590 fprintf (stream, " nearly-empty");
8591 if (BINFO_VIRTUAL_P (binfo))
8592 fprintf (stream, " virtual");
8593 fprintf (stream, "\n");
8595 indented = 0;
8596 if (BINFO_PRIMARY_P (binfo))
8598 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
8599 fprintf (stream, " primary-for %s (0x" HOST_WIDE_INT_PRINT_HEX ")",
8600 type_as_string (BINFO_TYPE (BINFO_INHERITANCE_CHAIN (binfo)),
8601 TFF_PLAIN_IDENTIFIER),
8602 (HOST_WIDE_INT) (uintptr_t) BINFO_INHERITANCE_CHAIN (binfo));
8604 if (BINFO_LOST_PRIMARY_P (binfo))
8606 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
8607 fprintf (stream, " lost-primary");
8609 if (indented)
8610 fprintf (stream, "\n");
8612 if (!(flags & TDF_SLIM))
8614 int indented = 0;
8616 if (BINFO_SUBVTT_INDEX (binfo))
8618 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
8619 fprintf (stream, " subvttidx=%s",
8620 expr_as_string (BINFO_SUBVTT_INDEX (binfo),
8621 TFF_PLAIN_IDENTIFIER));
8623 if (BINFO_VPTR_INDEX (binfo))
8625 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
8626 fprintf (stream, " vptridx=%s",
8627 expr_as_string (BINFO_VPTR_INDEX (binfo),
8628 TFF_PLAIN_IDENTIFIER));
8630 if (BINFO_VPTR_FIELD (binfo))
8632 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
8633 fprintf (stream, " vbaseoffset=%s",
8634 expr_as_string (BINFO_VPTR_FIELD (binfo),
8635 TFF_PLAIN_IDENTIFIER));
8637 if (BINFO_VTABLE (binfo))
8639 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
8640 fprintf (stream, " vptr=%s",
8641 expr_as_string (BINFO_VTABLE (binfo),
8642 TFF_PLAIN_IDENTIFIER));
8645 if (indented)
8646 fprintf (stream, "\n");
8649 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
8650 igo = dump_class_hierarchy_r (stream, flags, base_binfo, igo, indent + 2);
8652 return igo;
8655 /* Dump the BINFO hierarchy for T. */
8657 static void
8658 dump_class_hierarchy_1 (FILE *stream, int flags, tree t)
8660 fprintf (stream, "Class %s\n", type_as_string (t, TFF_PLAIN_IDENTIFIER));
8661 fprintf (stream, " size=%lu align=%lu\n",
8662 (unsigned long)(tree_to_shwi (TYPE_SIZE (t)) / BITS_PER_UNIT),
8663 (unsigned long)(TYPE_ALIGN (t) / BITS_PER_UNIT));
8664 fprintf (stream, " base size=%lu base align=%lu\n",
8665 (unsigned long)(tree_to_shwi (TYPE_SIZE (CLASSTYPE_AS_BASE (t)))
8666 / BITS_PER_UNIT),
8667 (unsigned long)(TYPE_ALIGN (CLASSTYPE_AS_BASE (t))
8668 / BITS_PER_UNIT));
8669 dump_class_hierarchy_r (stream, flags, TYPE_BINFO (t), TYPE_BINFO (t), 0);
8670 fprintf (stream, "\n");
8673 /* Debug interface to hierarchy dumping. */
8675 void
8676 debug_class (tree t)
8678 dump_class_hierarchy_1 (stderr, TDF_SLIM, t);
8681 static void
8682 dump_class_hierarchy (tree t)
8684 int flags;
8685 FILE *stream = get_dump_info (TDI_class, &flags);
8687 if (stream)
8689 dump_class_hierarchy_1 (stream, flags, t);
8693 static void
8694 dump_array (FILE * stream, tree decl)
8696 tree value;
8697 unsigned HOST_WIDE_INT ix;
8698 HOST_WIDE_INT elt;
8699 tree size = TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (decl)));
8701 elt = (tree_to_shwi (TYPE_SIZE (TREE_TYPE (TREE_TYPE (decl))))
8702 / BITS_PER_UNIT);
8703 fprintf (stream, "%s:", decl_as_string (decl, TFF_PLAIN_IDENTIFIER));
8704 fprintf (stream, " %s entries",
8705 expr_as_string (size_binop (PLUS_EXPR, size, size_one_node),
8706 TFF_PLAIN_IDENTIFIER));
8707 fprintf (stream, "\n");
8709 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (DECL_INITIAL (decl)),
8710 ix, value)
8711 fprintf (stream, "%-4ld %s\n", (long)(ix * elt),
8712 expr_as_string (value, TFF_PLAIN_IDENTIFIER));
8715 static void
8716 dump_vtable (tree t, tree binfo, tree vtable)
8718 int flags;
8719 FILE *stream = get_dump_info (TDI_class, &flags);
8721 if (!stream)
8722 return;
8724 if (!(flags & TDF_SLIM))
8726 int ctor_vtbl_p = TYPE_BINFO (t) != binfo;
8728 fprintf (stream, "%s for %s",
8729 ctor_vtbl_p ? "Construction vtable" : "Vtable",
8730 type_as_string (BINFO_TYPE (binfo), TFF_PLAIN_IDENTIFIER));
8731 if (ctor_vtbl_p)
8733 if (!BINFO_VIRTUAL_P (binfo))
8734 fprintf (stream, " (0x" HOST_WIDE_INT_PRINT_HEX " instance)",
8735 (HOST_WIDE_INT) (uintptr_t) binfo);
8736 fprintf (stream, " in %s", type_as_string (t, TFF_PLAIN_IDENTIFIER));
8738 fprintf (stream, "\n");
8739 dump_array (stream, vtable);
8740 fprintf (stream, "\n");
8744 static void
8745 dump_vtt (tree t, tree vtt)
8747 int flags;
8748 FILE *stream = get_dump_info (TDI_class, &flags);
8750 if (!stream)
8751 return;
8753 if (!(flags & TDF_SLIM))
8755 fprintf (stream, "VTT for %s\n",
8756 type_as_string (t, TFF_PLAIN_IDENTIFIER));
8757 dump_array (stream, vtt);
8758 fprintf (stream, "\n");
8762 /* Dump a function or thunk and its thunkees. */
8764 static void
8765 dump_thunk (FILE *stream, int indent, tree thunk)
8767 static const char spaces[] = " ";
8768 tree name = DECL_NAME (thunk);
8769 tree thunks;
8771 fprintf (stream, "%.*s%p %s %s", indent, spaces,
8772 (void *)thunk,
8773 !DECL_THUNK_P (thunk) ? "function"
8774 : DECL_THIS_THUNK_P (thunk) ? "this-thunk" : "covariant-thunk",
8775 name ? IDENTIFIER_POINTER (name) : "<unset>");
8776 if (DECL_THUNK_P (thunk))
8778 HOST_WIDE_INT fixed_adjust = THUNK_FIXED_OFFSET (thunk);
8779 tree virtual_adjust = THUNK_VIRTUAL_OFFSET (thunk);
8781 fprintf (stream, " fixed=" HOST_WIDE_INT_PRINT_DEC, fixed_adjust);
8782 if (!virtual_adjust)
8783 /*NOP*/;
8784 else if (DECL_THIS_THUNK_P (thunk))
8785 fprintf (stream, " vcall=" HOST_WIDE_INT_PRINT_DEC,
8786 tree_to_shwi (virtual_adjust));
8787 else
8788 fprintf (stream, " vbase=" HOST_WIDE_INT_PRINT_DEC "(%s)",
8789 tree_to_shwi (BINFO_VPTR_FIELD (virtual_adjust)),
8790 type_as_string (BINFO_TYPE (virtual_adjust), TFF_SCOPE));
8791 if (THUNK_ALIAS (thunk))
8792 fprintf (stream, " alias to %p", (void *)THUNK_ALIAS (thunk));
8794 fprintf (stream, "\n");
8795 for (thunks = DECL_THUNKS (thunk); thunks; thunks = TREE_CHAIN (thunks))
8796 dump_thunk (stream, indent + 2, thunks);
8799 /* Dump the thunks for FN. */
8801 void
8802 debug_thunks (tree fn)
8804 dump_thunk (stderr, 0, fn);
8807 /* Virtual function table initialization. */
8809 /* Create all the necessary vtables for T and its base classes. */
8811 static void
8812 finish_vtbls (tree t)
8814 tree vbase;
8815 vec<constructor_elt, va_gc> *v = NULL;
8816 tree vtable = BINFO_VTABLE (TYPE_BINFO (t));
8818 /* We lay out the primary and secondary vtables in one contiguous
8819 vtable. The primary vtable is first, followed by the non-virtual
8820 secondary vtables in inheritance graph order. */
8821 accumulate_vtbl_inits (TYPE_BINFO (t), TYPE_BINFO (t), TYPE_BINFO (t),
8822 vtable, t, &v);
8824 /* Then come the virtual bases, also in inheritance graph order. */
8825 for (vbase = TYPE_BINFO (t); vbase; vbase = TREE_CHAIN (vbase))
8827 if (!BINFO_VIRTUAL_P (vbase))
8828 continue;
8829 accumulate_vtbl_inits (vbase, vbase, TYPE_BINFO (t), vtable, t, &v);
8832 if (BINFO_VTABLE (TYPE_BINFO (t)))
8833 initialize_vtable (TYPE_BINFO (t), v);
8836 /* Initialize the vtable for BINFO with the INITS. */
8838 static void
8839 initialize_vtable (tree binfo, vec<constructor_elt, va_gc> *inits)
8841 tree decl;
8843 layout_vtable_decl (binfo, vec_safe_length (inits));
8844 decl = get_vtbl_decl_for_binfo (binfo);
8845 initialize_artificial_var (decl, inits);
8846 dump_vtable (BINFO_TYPE (binfo), binfo, decl);
8849 /* Build the VTT (virtual table table) for T.
8850 A class requires a VTT if it has virtual bases.
8852 This holds
8853 1 - primary virtual pointer for complete object T
8854 2 - secondary VTTs for each direct non-virtual base of T which requires a
8856 3 - secondary virtual pointers for each direct or indirect base of T which
8857 has virtual bases or is reachable via a virtual path from T.
8858 4 - secondary VTTs for each direct or indirect virtual base of T.
8860 Secondary VTTs look like complete object VTTs without part 4. */
8862 static void
8863 build_vtt (tree t)
8865 tree type;
8866 tree vtt;
8867 tree index;
8868 vec<constructor_elt, va_gc> *inits;
8870 /* Build up the initializers for the VTT. */
8871 inits = NULL;
8872 index = size_zero_node;
8873 build_vtt_inits (TYPE_BINFO (t), t, &inits, &index);
8875 /* If we didn't need a VTT, we're done. */
8876 if (!inits)
8877 return;
8879 /* Figure out the type of the VTT. */
8880 type = build_array_of_n_type (const_ptr_type_node,
8881 inits->length ());
8883 /* Now, build the VTT object itself. */
8884 vtt = build_vtable (t, mangle_vtt_for_type (t), type);
8885 initialize_artificial_var (vtt, inits);
8886 /* Add the VTT to the vtables list. */
8887 DECL_CHAIN (vtt) = DECL_CHAIN (CLASSTYPE_VTABLES (t));
8888 DECL_CHAIN (CLASSTYPE_VTABLES (t)) = vtt;
8890 dump_vtt (t, vtt);
8893 /* When building a secondary VTT, BINFO_VTABLE is set to a TREE_LIST with
8894 PURPOSE the RTTI_BINFO, VALUE the real vtable pointer for this binfo,
8895 and CHAIN the vtable pointer for this binfo after construction is
8896 complete. VALUE can also be another BINFO, in which case we recurse. */
8898 static tree
8899 binfo_ctor_vtable (tree binfo)
8901 tree vt;
8903 while (1)
8905 vt = BINFO_VTABLE (binfo);
8906 if (TREE_CODE (vt) == TREE_LIST)
8907 vt = TREE_VALUE (vt);
8908 if (TREE_CODE (vt) == TREE_BINFO)
8909 binfo = vt;
8910 else
8911 break;
8914 return vt;
8917 /* Data for secondary VTT initialization. */
8918 struct secondary_vptr_vtt_init_data
8920 /* Is this the primary VTT? */
8921 bool top_level_p;
8923 /* Current index into the VTT. */
8924 tree index;
8926 /* Vector of initializers built up. */
8927 vec<constructor_elt, va_gc> *inits;
8929 /* The type being constructed by this secondary VTT. */
8930 tree type_being_constructed;
8933 /* Recursively build the VTT-initializer for BINFO (which is in the
8934 hierarchy dominated by T). INITS points to the end of the initializer
8935 list to date. INDEX is the VTT index where the next element will be
8936 replaced. Iff BINFO is the binfo for T, this is the top level VTT (i.e.
8937 not a subvtt for some base of T). When that is so, we emit the sub-VTTs
8938 for virtual bases of T. When it is not so, we build the constructor
8939 vtables for the BINFO-in-T variant. */
8941 static void
8942 build_vtt_inits (tree binfo, tree t, vec<constructor_elt, va_gc> **inits,
8943 tree *index)
8945 int i;
8946 tree b;
8947 tree init;
8948 secondary_vptr_vtt_init_data data;
8949 int top_level_p = SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), t);
8951 /* We only need VTTs for subobjects with virtual bases. */
8952 if (!CLASSTYPE_VBASECLASSES (BINFO_TYPE (binfo)))
8953 return;
8955 /* We need to use a construction vtable if this is not the primary
8956 VTT. */
8957 if (!top_level_p)
8959 build_ctor_vtbl_group (binfo, t);
8961 /* Record the offset in the VTT where this sub-VTT can be found. */
8962 BINFO_SUBVTT_INDEX (binfo) = *index;
8965 /* Add the address of the primary vtable for the complete object. */
8966 init = binfo_ctor_vtable (binfo);
8967 CONSTRUCTOR_APPEND_ELT (*inits, NULL_TREE, init);
8968 if (top_level_p)
8970 gcc_assert (!BINFO_VPTR_INDEX (binfo));
8971 BINFO_VPTR_INDEX (binfo) = *index;
8973 *index = size_binop (PLUS_EXPR, *index, TYPE_SIZE_UNIT (ptr_type_node));
8975 /* Recursively add the secondary VTTs for non-virtual bases. */
8976 for (i = 0; BINFO_BASE_ITERATE (binfo, i, b); ++i)
8977 if (!BINFO_VIRTUAL_P (b))
8978 build_vtt_inits (b, t, inits, index);
8980 /* Add secondary virtual pointers for all subobjects of BINFO with
8981 either virtual bases or reachable along a virtual path, except
8982 subobjects that are non-virtual primary bases. */
8983 data.top_level_p = top_level_p;
8984 data.index = *index;
8985 data.inits = *inits;
8986 data.type_being_constructed = BINFO_TYPE (binfo);
8988 dfs_walk_once (binfo, dfs_build_secondary_vptr_vtt_inits, NULL, &data);
8990 *index = data.index;
8992 /* data.inits might have grown as we added secondary virtual pointers.
8993 Make sure our caller knows about the new vector. */
8994 *inits = data.inits;
8996 if (top_level_p)
8997 /* Add the secondary VTTs for virtual bases in inheritance graph
8998 order. */
8999 for (b = TYPE_BINFO (BINFO_TYPE (binfo)); b; b = TREE_CHAIN (b))
9001 if (!BINFO_VIRTUAL_P (b))
9002 continue;
9004 build_vtt_inits (b, t, inits, index);
9006 else
9007 /* Remove the ctor vtables we created. */
9008 dfs_walk_all (binfo, dfs_fixup_binfo_vtbls, NULL, binfo);
9011 /* Called from build_vtt_inits via dfs_walk. BINFO is the binfo for the base
9012 in most derived. DATA is a SECONDARY_VPTR_VTT_INIT_DATA structure. */
9014 static tree
9015 dfs_build_secondary_vptr_vtt_inits (tree binfo, void *data_)
9017 secondary_vptr_vtt_init_data *data = (secondary_vptr_vtt_init_data *)data_;
9019 /* We don't care about bases that don't have vtables. */
9020 if (!TYPE_VFIELD (BINFO_TYPE (binfo)))
9021 return dfs_skip_bases;
9023 /* We're only interested in proper subobjects of the type being
9024 constructed. */
9025 if (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), data->type_being_constructed))
9026 return NULL_TREE;
9028 /* We're only interested in bases with virtual bases or reachable
9029 via a virtual path from the type being constructed. */
9030 if (!(CLASSTYPE_VBASECLASSES (BINFO_TYPE (binfo))
9031 || binfo_via_virtual (binfo, data->type_being_constructed)))
9032 return dfs_skip_bases;
9034 /* We're not interested in non-virtual primary bases. */
9035 if (!BINFO_VIRTUAL_P (binfo) && BINFO_PRIMARY_P (binfo))
9036 return NULL_TREE;
9038 /* Record the index where this secondary vptr can be found. */
9039 if (data->top_level_p)
9041 gcc_assert (!BINFO_VPTR_INDEX (binfo));
9042 BINFO_VPTR_INDEX (binfo) = data->index;
9044 if (BINFO_VIRTUAL_P (binfo))
9046 /* It's a primary virtual base, and this is not a
9047 construction vtable. Find the base this is primary of in
9048 the inheritance graph, and use that base's vtable
9049 now. */
9050 while (BINFO_PRIMARY_P (binfo))
9051 binfo = BINFO_INHERITANCE_CHAIN (binfo);
9055 /* Add the initializer for the secondary vptr itself. */
9056 CONSTRUCTOR_APPEND_ELT (data->inits, NULL_TREE, binfo_ctor_vtable (binfo));
9058 /* Advance the vtt index. */
9059 data->index = size_binop (PLUS_EXPR, data->index,
9060 TYPE_SIZE_UNIT (ptr_type_node));
9062 return NULL_TREE;
9065 /* Called from build_vtt_inits via dfs_walk. After building
9066 constructor vtables and generating the sub-vtt from them, we need
9067 to restore the BINFO_VTABLES that were scribbled on. DATA is the
9068 binfo of the base whose sub vtt was generated. */
9070 static tree
9071 dfs_fixup_binfo_vtbls (tree binfo, void* data)
9073 tree vtable = BINFO_VTABLE (binfo);
9075 if (!TYPE_CONTAINS_VPTR_P (BINFO_TYPE (binfo)))
9076 /* If this class has no vtable, none of its bases do. */
9077 return dfs_skip_bases;
9079 if (!vtable)
9080 /* This might be a primary base, so have no vtable in this
9081 hierarchy. */
9082 return NULL_TREE;
9084 /* If we scribbled the construction vtable vptr into BINFO, clear it
9085 out now. */
9086 if (TREE_CODE (vtable) == TREE_LIST
9087 && (TREE_PURPOSE (vtable) == (tree) data))
9088 BINFO_VTABLE (binfo) = TREE_CHAIN (vtable);
9090 return NULL_TREE;
9093 /* Build the construction vtable group for BINFO which is in the
9094 hierarchy dominated by T. */
9096 static void
9097 build_ctor_vtbl_group (tree binfo, tree t)
9099 tree type;
9100 tree vtbl;
9101 tree id;
9102 tree vbase;
9103 vec<constructor_elt, va_gc> *v;
9105 /* See if we've already created this construction vtable group. */
9106 id = mangle_ctor_vtbl_for_type (t, binfo);
9107 if (IDENTIFIER_GLOBAL_VALUE (id))
9108 return;
9110 gcc_assert (!SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), t));
9111 /* Build a version of VTBL (with the wrong type) for use in
9112 constructing the addresses of secondary vtables in the
9113 construction vtable group. */
9114 vtbl = build_vtable (t, id, ptr_type_node);
9115 DECL_CONSTRUCTION_VTABLE_P (vtbl) = 1;
9116 /* Don't export construction vtables from shared libraries. Even on
9117 targets that don't support hidden visibility, this tells
9118 can_refer_decl_in_current_unit_p not to assume that it's safe to
9119 access from a different compilation unit (bz 54314). */
9120 DECL_VISIBILITY (vtbl) = VISIBILITY_HIDDEN;
9121 DECL_VISIBILITY_SPECIFIED (vtbl) = true;
9123 v = NULL;
9124 accumulate_vtbl_inits (binfo, TYPE_BINFO (TREE_TYPE (binfo)),
9125 binfo, vtbl, t, &v);
9127 /* Add the vtables for each of our virtual bases using the vbase in T
9128 binfo. */
9129 for (vbase = TYPE_BINFO (BINFO_TYPE (binfo));
9130 vbase;
9131 vbase = TREE_CHAIN (vbase))
9133 tree b;
9135 if (!BINFO_VIRTUAL_P (vbase))
9136 continue;
9137 b = copied_binfo (vbase, binfo);
9139 accumulate_vtbl_inits (b, vbase, binfo, vtbl, t, &v);
9142 /* Figure out the type of the construction vtable. */
9143 type = build_array_of_n_type (vtable_entry_type, v->length ());
9144 layout_type (type);
9145 TREE_TYPE (vtbl) = type;
9146 DECL_SIZE (vtbl) = DECL_SIZE_UNIT (vtbl) = NULL_TREE;
9147 layout_decl (vtbl, 0);
9149 /* Initialize the construction vtable. */
9150 CLASSTYPE_VTABLES (t) = chainon (CLASSTYPE_VTABLES (t), vtbl);
9151 initialize_artificial_var (vtbl, v);
9152 dump_vtable (t, binfo, vtbl);
9155 /* Add the vtbl initializers for BINFO (and its bases other than
9156 non-virtual primaries) to the list of INITS. BINFO is in the
9157 hierarchy dominated by T. RTTI_BINFO is the binfo within T of
9158 the constructor the vtbl inits should be accumulated for. (If this
9159 is the complete object vtbl then RTTI_BINFO will be TYPE_BINFO (T).)
9160 ORIG_BINFO is the binfo for this object within BINFO_TYPE (RTTI_BINFO).
9161 BINFO is the active base equivalent of ORIG_BINFO in the inheritance
9162 graph of T. Both BINFO and ORIG_BINFO will have the same BINFO_TYPE,
9163 but are not necessarily the same in terms of layout. */
9165 static void
9166 accumulate_vtbl_inits (tree binfo,
9167 tree orig_binfo,
9168 tree rtti_binfo,
9169 tree vtbl,
9170 tree t,
9171 vec<constructor_elt, va_gc> **inits)
9173 int i;
9174 tree base_binfo;
9175 int ctor_vtbl_p = !SAME_BINFO_TYPE_P (BINFO_TYPE (rtti_binfo), t);
9177 gcc_assert (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), BINFO_TYPE (orig_binfo)));
9179 /* If it doesn't have a vptr, we don't do anything. */
9180 if (!TYPE_CONTAINS_VPTR_P (BINFO_TYPE (binfo)))
9181 return;
9183 /* If we're building a construction vtable, we're not interested in
9184 subobjects that don't require construction vtables. */
9185 if (ctor_vtbl_p
9186 && !CLASSTYPE_VBASECLASSES (BINFO_TYPE (binfo))
9187 && !binfo_via_virtual (orig_binfo, BINFO_TYPE (rtti_binfo)))
9188 return;
9190 /* Build the initializers for the BINFO-in-T vtable. */
9191 dfs_accumulate_vtbl_inits (binfo, orig_binfo, rtti_binfo, vtbl, t, inits);
9193 /* Walk the BINFO and its bases. We walk in preorder so that as we
9194 initialize each vtable we can figure out at what offset the
9195 secondary vtable lies from the primary vtable. We can't use
9196 dfs_walk here because we need to iterate through bases of BINFO
9197 and RTTI_BINFO simultaneously. */
9198 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
9200 /* Skip virtual bases. */
9201 if (BINFO_VIRTUAL_P (base_binfo))
9202 continue;
9203 accumulate_vtbl_inits (base_binfo,
9204 BINFO_BASE_BINFO (orig_binfo, i),
9205 rtti_binfo, vtbl, t,
9206 inits);
9210 /* Called from accumulate_vtbl_inits. Adds the initializers for the
9211 BINFO vtable to L. */
9213 static void
9214 dfs_accumulate_vtbl_inits (tree binfo,
9215 tree orig_binfo,
9216 tree rtti_binfo,
9217 tree orig_vtbl,
9218 tree t,
9219 vec<constructor_elt, va_gc> **l)
9221 tree vtbl = NULL_TREE;
9222 int ctor_vtbl_p = !SAME_BINFO_TYPE_P (BINFO_TYPE (rtti_binfo), t);
9223 int n_inits;
9225 if (ctor_vtbl_p
9226 && BINFO_VIRTUAL_P (orig_binfo) && BINFO_PRIMARY_P (orig_binfo))
9228 /* In the hierarchy of BINFO_TYPE (RTTI_BINFO), this is a
9229 primary virtual base. If it is not the same primary in
9230 the hierarchy of T, we'll need to generate a ctor vtable
9231 for it, to place at its location in T. If it is the same
9232 primary, we still need a VTT entry for the vtable, but it
9233 should point to the ctor vtable for the base it is a
9234 primary for within the sub-hierarchy of RTTI_BINFO.
9236 There are three possible cases:
9238 1) We are in the same place.
9239 2) We are a primary base within a lost primary virtual base of
9240 RTTI_BINFO.
9241 3) We are primary to something not a base of RTTI_BINFO. */
9243 tree b;
9244 tree last = NULL_TREE;
9246 /* First, look through the bases we are primary to for RTTI_BINFO
9247 or a virtual base. */
9248 b = binfo;
9249 while (BINFO_PRIMARY_P (b))
9251 b = BINFO_INHERITANCE_CHAIN (b);
9252 last = b;
9253 if (BINFO_VIRTUAL_P (b) || b == rtti_binfo)
9254 goto found;
9256 /* If we run out of primary links, keep looking down our
9257 inheritance chain; we might be an indirect primary. */
9258 for (b = last; b; b = BINFO_INHERITANCE_CHAIN (b))
9259 if (BINFO_VIRTUAL_P (b) || b == rtti_binfo)
9260 break;
9261 found:
9263 /* If we found RTTI_BINFO, this is case 1. If we found a virtual
9264 base B and it is a base of RTTI_BINFO, this is case 2. In
9265 either case, we share our vtable with LAST, i.e. the
9266 derived-most base within B of which we are a primary. */
9267 if (b == rtti_binfo
9268 || (b && binfo_for_vbase (BINFO_TYPE (b), BINFO_TYPE (rtti_binfo))))
9269 /* Just set our BINFO_VTABLE to point to LAST, as we may not have
9270 set LAST's BINFO_VTABLE yet. We'll extract the actual vptr in
9271 binfo_ctor_vtable after everything's been set up. */
9272 vtbl = last;
9274 /* Otherwise, this is case 3 and we get our own. */
9276 else if (!BINFO_NEW_VTABLE_MARKED (orig_binfo))
9277 return;
9279 n_inits = vec_safe_length (*l);
9281 if (!vtbl)
9283 tree index;
9284 int non_fn_entries;
9286 /* Add the initializer for this vtable. */
9287 build_vtbl_initializer (binfo, orig_binfo, t, rtti_binfo,
9288 &non_fn_entries, l);
9290 /* Figure out the position to which the VPTR should point. */
9291 vtbl = build1 (ADDR_EXPR, vtbl_ptr_type_node, orig_vtbl);
9292 index = size_binop (MULT_EXPR,
9293 TYPE_SIZE_UNIT (vtable_entry_type),
9294 size_int (non_fn_entries + n_inits));
9295 vtbl = fold_build_pointer_plus (vtbl, index);
9298 if (ctor_vtbl_p)
9299 /* For a construction vtable, we can't overwrite BINFO_VTABLE.
9300 So, we make a TREE_LIST. Later, dfs_fixup_binfo_vtbls will
9301 straighten this out. */
9302 BINFO_VTABLE (binfo) = tree_cons (rtti_binfo, vtbl, BINFO_VTABLE (binfo));
9303 else if (BINFO_PRIMARY_P (binfo) && BINFO_VIRTUAL_P (binfo))
9304 /* Throw away any unneeded intializers. */
9305 (*l)->truncate (n_inits);
9306 else
9307 /* For an ordinary vtable, set BINFO_VTABLE. */
9308 BINFO_VTABLE (binfo) = vtbl;
9311 static GTY(()) tree abort_fndecl_addr;
9313 /* Construct the initializer for BINFO's virtual function table. BINFO
9314 is part of the hierarchy dominated by T. If we're building a
9315 construction vtable, the ORIG_BINFO is the binfo we should use to
9316 find the actual function pointers to put in the vtable - but they
9317 can be overridden on the path to most-derived in the graph that
9318 ORIG_BINFO belongs. Otherwise,
9319 ORIG_BINFO should be the same as BINFO. The RTTI_BINFO is the
9320 BINFO that should be indicated by the RTTI information in the
9321 vtable; it will be a base class of T, rather than T itself, if we
9322 are building a construction vtable.
9324 The value returned is a TREE_LIST suitable for wrapping in a
9325 CONSTRUCTOR to use as the DECL_INITIAL for a vtable. If
9326 NON_FN_ENTRIES_P is not NULL, *NON_FN_ENTRIES_P is set to the
9327 number of non-function entries in the vtable.
9329 It might seem that this function should never be called with a
9330 BINFO for which BINFO_PRIMARY_P holds, the vtable for such a
9331 base is always subsumed by a derived class vtable. However, when
9332 we are building construction vtables, we do build vtables for
9333 primary bases; we need these while the primary base is being
9334 constructed. */
9336 static void
9337 build_vtbl_initializer (tree binfo,
9338 tree orig_binfo,
9339 tree t,
9340 tree rtti_binfo,
9341 int* non_fn_entries_p,
9342 vec<constructor_elt, va_gc> **inits)
9344 tree v;
9345 vtbl_init_data vid;
9346 unsigned ix, jx;
9347 tree vbinfo;
9348 vec<tree, va_gc> *vbases;
9349 constructor_elt *e;
9351 /* Initialize VID. */
9352 memset (&vid, 0, sizeof (vid));
9353 vid.binfo = binfo;
9354 vid.derived = t;
9355 vid.rtti_binfo = rtti_binfo;
9356 vid.primary_vtbl_p = SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), t);
9357 vid.ctor_vtbl_p = !SAME_BINFO_TYPE_P (BINFO_TYPE (rtti_binfo), t);
9358 vid.generate_vcall_entries = true;
9359 /* The first vbase or vcall offset is at index -3 in the vtable. */
9360 vid.index = ssize_int(-3 * TARGET_VTABLE_DATA_ENTRY_DISTANCE);
9362 /* Add entries to the vtable for RTTI. */
9363 build_rtti_vtbl_entries (binfo, &vid);
9365 /* Create an array for keeping track of the functions we've
9366 processed. When we see multiple functions with the same
9367 signature, we share the vcall offsets. */
9368 vec_alloc (vid.fns, 32);
9369 /* Add the vcall and vbase offset entries. */
9370 build_vcall_and_vbase_vtbl_entries (binfo, &vid);
9372 /* Clear BINFO_VTABLE_PATH_MARKED; it's set by
9373 build_vbase_offset_vtbl_entries. */
9374 for (vbases = CLASSTYPE_VBASECLASSES (t), ix = 0;
9375 vec_safe_iterate (vbases, ix, &vbinfo); ix++)
9376 BINFO_VTABLE_PATH_MARKED (vbinfo) = 0;
9378 /* If the target requires padding between data entries, add that now. */
9379 if (TARGET_VTABLE_DATA_ENTRY_DISTANCE > 1)
9381 int n_entries = vec_safe_length (vid.inits);
9383 vec_safe_grow (vid.inits, TARGET_VTABLE_DATA_ENTRY_DISTANCE * n_entries);
9385 /* Move data entries into their new positions and add padding
9386 after the new positions. Iterate backwards so we don't
9387 overwrite entries that we would need to process later. */
9388 for (ix = n_entries - 1;
9389 vid.inits->iterate (ix, &e);
9390 ix--)
9392 int j;
9393 int new_position = (TARGET_VTABLE_DATA_ENTRY_DISTANCE * ix
9394 + (TARGET_VTABLE_DATA_ENTRY_DISTANCE - 1));
9396 (*vid.inits)[new_position] = *e;
9398 for (j = 1; j < TARGET_VTABLE_DATA_ENTRY_DISTANCE; ++j)
9400 constructor_elt *f = &(*vid.inits)[new_position - j];
9401 f->index = NULL_TREE;
9402 f->value = build1 (NOP_EXPR, vtable_entry_type,
9403 null_pointer_node);
9408 if (non_fn_entries_p)
9409 *non_fn_entries_p = vec_safe_length (vid.inits);
9411 /* The initializers for virtual functions were built up in reverse
9412 order. Straighten them out and add them to the running list in one
9413 step. */
9414 jx = vec_safe_length (*inits);
9415 vec_safe_grow (*inits, jx + vid.inits->length ());
9417 for (ix = vid.inits->length () - 1;
9418 vid.inits->iterate (ix, &e);
9419 ix--, jx++)
9420 (**inits)[jx] = *e;
9422 /* Go through all the ordinary virtual functions, building up
9423 initializers. */
9424 for (v = BINFO_VIRTUALS (orig_binfo); v; v = TREE_CHAIN (v))
9426 tree delta;
9427 tree vcall_index;
9428 tree fn, fn_original;
9429 tree init = NULL_TREE;
9431 fn = BV_FN (v);
9432 fn_original = fn;
9433 if (DECL_THUNK_P (fn))
9435 if (!DECL_NAME (fn))
9436 finish_thunk (fn);
9437 if (THUNK_ALIAS (fn))
9439 fn = THUNK_ALIAS (fn);
9440 BV_FN (v) = fn;
9442 fn_original = THUNK_TARGET (fn);
9445 /* If the only definition of this function signature along our
9446 primary base chain is from a lost primary, this vtable slot will
9447 never be used, so just zero it out. This is important to avoid
9448 requiring extra thunks which cannot be generated with the function.
9450 We first check this in update_vtable_entry_for_fn, so we handle
9451 restored primary bases properly; we also need to do it here so we
9452 zero out unused slots in ctor vtables, rather than filling them
9453 with erroneous values (though harmless, apart from relocation
9454 costs). */
9455 if (BV_LOST_PRIMARY (v))
9456 init = size_zero_node;
9458 if (! init)
9460 /* Pull the offset for `this', and the function to call, out of
9461 the list. */
9462 delta = BV_DELTA (v);
9463 vcall_index = BV_VCALL_INDEX (v);
9465 gcc_assert (TREE_CODE (delta) == INTEGER_CST);
9466 gcc_assert (TREE_CODE (fn) == FUNCTION_DECL);
9468 /* You can't call an abstract virtual function; it's abstract.
9469 So, we replace these functions with __pure_virtual. */
9470 if (DECL_PURE_VIRTUAL_P (fn_original))
9472 fn = abort_fndecl;
9473 if (!TARGET_VTABLE_USES_DESCRIPTORS)
9475 if (abort_fndecl_addr == NULL)
9476 abort_fndecl_addr
9477 = fold_convert (vfunc_ptr_type_node,
9478 build_fold_addr_expr (fn));
9479 init = abort_fndecl_addr;
9482 /* Likewise for deleted virtuals. */
9483 else if (DECL_DELETED_FN (fn_original))
9485 fn = get_identifier ("__cxa_deleted_virtual");
9486 if (!get_global_value_if_present (fn, &fn))
9487 fn = push_library_fn (fn, (build_function_type_list
9488 (void_type_node, NULL_TREE)),
9489 NULL_TREE, ECF_NORETURN);
9490 if (!TARGET_VTABLE_USES_DESCRIPTORS)
9491 init = fold_convert (vfunc_ptr_type_node,
9492 build_fold_addr_expr (fn));
9494 else
9496 if (!integer_zerop (delta) || vcall_index)
9498 fn = make_thunk (fn, /*this_adjusting=*/1, delta, vcall_index);
9499 if (!DECL_NAME (fn))
9500 finish_thunk (fn);
9502 /* Take the address of the function, considering it to be of an
9503 appropriate generic type. */
9504 if (!TARGET_VTABLE_USES_DESCRIPTORS)
9505 init = fold_convert (vfunc_ptr_type_node,
9506 build_fold_addr_expr (fn));
9507 /* Don't refer to a virtual destructor from a constructor
9508 vtable or a vtable for an abstract class, since destroying
9509 an object under construction is undefined behavior and we
9510 don't want it to be considered a candidate for speculative
9511 devirtualization. But do create the thunk for ABI
9512 compliance. */
9513 if (DECL_DESTRUCTOR_P (fn_original)
9514 && (CLASSTYPE_PURE_VIRTUALS (DECL_CONTEXT (fn_original))
9515 || orig_binfo != binfo))
9516 init = size_zero_node;
9520 /* And add it to the chain of initializers. */
9521 if (TARGET_VTABLE_USES_DESCRIPTORS)
9523 int i;
9524 if (init == size_zero_node)
9525 for (i = 0; i < TARGET_VTABLE_USES_DESCRIPTORS; ++i)
9526 CONSTRUCTOR_APPEND_ELT (*inits, NULL_TREE, init);
9527 else
9528 for (i = 0; i < TARGET_VTABLE_USES_DESCRIPTORS; ++i)
9530 tree fdesc = build2 (FDESC_EXPR, vfunc_ptr_type_node,
9531 fn, build_int_cst (NULL_TREE, i));
9532 TREE_CONSTANT (fdesc) = 1;
9534 CONSTRUCTOR_APPEND_ELT (*inits, NULL_TREE, fdesc);
9537 else
9538 CONSTRUCTOR_APPEND_ELT (*inits, NULL_TREE, init);
9542 /* Adds to vid->inits the initializers for the vbase and vcall
9543 offsets in BINFO, which is in the hierarchy dominated by T. */
9545 static void
9546 build_vcall_and_vbase_vtbl_entries (tree binfo, vtbl_init_data* vid)
9548 tree b;
9550 /* If this is a derived class, we must first create entries
9551 corresponding to the primary base class. */
9552 b = get_primary_binfo (binfo);
9553 if (b)
9554 build_vcall_and_vbase_vtbl_entries (b, vid);
9556 /* Add the vbase entries for this base. */
9557 build_vbase_offset_vtbl_entries (binfo, vid);
9558 /* Add the vcall entries for this base. */
9559 build_vcall_offset_vtbl_entries (binfo, vid);
9562 /* Returns the initializers for the vbase offset entries in the vtable
9563 for BINFO (which is part of the class hierarchy dominated by T), in
9564 reverse order. VBASE_OFFSET_INDEX gives the vtable index
9565 where the next vbase offset will go. */
9567 static void
9568 build_vbase_offset_vtbl_entries (tree binfo, vtbl_init_data* vid)
9570 tree vbase;
9571 tree t;
9572 tree non_primary_binfo;
9574 /* If there are no virtual baseclasses, then there is nothing to
9575 do. */
9576 if (!CLASSTYPE_VBASECLASSES (BINFO_TYPE (binfo)))
9577 return;
9579 t = vid->derived;
9581 /* We might be a primary base class. Go up the inheritance hierarchy
9582 until we find the most derived class of which we are a primary base:
9583 it is the offset of that which we need to use. */
9584 non_primary_binfo = binfo;
9585 while (BINFO_INHERITANCE_CHAIN (non_primary_binfo))
9587 tree b;
9589 /* If we have reached a virtual base, then it must be a primary
9590 base (possibly multi-level) of vid->binfo, or we wouldn't
9591 have called build_vcall_and_vbase_vtbl_entries for it. But it
9592 might be a lost primary, so just skip down to vid->binfo. */
9593 if (BINFO_VIRTUAL_P (non_primary_binfo))
9595 non_primary_binfo = vid->binfo;
9596 break;
9599 b = BINFO_INHERITANCE_CHAIN (non_primary_binfo);
9600 if (get_primary_binfo (b) != non_primary_binfo)
9601 break;
9602 non_primary_binfo = b;
9605 /* Go through the virtual bases, adding the offsets. */
9606 for (vbase = TYPE_BINFO (BINFO_TYPE (binfo));
9607 vbase;
9608 vbase = TREE_CHAIN (vbase))
9610 tree b;
9611 tree delta;
9613 if (!BINFO_VIRTUAL_P (vbase))
9614 continue;
9616 /* Find the instance of this virtual base in the complete
9617 object. */
9618 b = copied_binfo (vbase, binfo);
9620 /* If we've already got an offset for this virtual base, we
9621 don't need another one. */
9622 if (BINFO_VTABLE_PATH_MARKED (b))
9623 continue;
9624 BINFO_VTABLE_PATH_MARKED (b) = 1;
9626 /* Figure out where we can find this vbase offset. */
9627 delta = size_binop (MULT_EXPR,
9628 vid->index,
9629 fold_convert (ssizetype,
9630 TYPE_SIZE_UNIT (vtable_entry_type)));
9631 if (vid->primary_vtbl_p)
9632 BINFO_VPTR_FIELD (b) = delta;
9634 if (binfo != TYPE_BINFO (t))
9635 /* The vbase offset had better be the same. */
9636 gcc_assert (tree_int_cst_equal (delta, BINFO_VPTR_FIELD (vbase)));
9638 /* The next vbase will come at a more negative offset. */
9639 vid->index = size_binop (MINUS_EXPR, vid->index,
9640 ssize_int (TARGET_VTABLE_DATA_ENTRY_DISTANCE));
9642 /* The initializer is the delta from BINFO to this virtual base.
9643 The vbase offsets go in reverse inheritance-graph order, and
9644 we are walking in inheritance graph order so these end up in
9645 the right order. */
9646 delta = size_diffop_loc (input_location,
9647 BINFO_OFFSET (b), BINFO_OFFSET (non_primary_binfo));
9649 CONSTRUCTOR_APPEND_ELT (vid->inits, NULL_TREE,
9650 fold_build1_loc (input_location, NOP_EXPR,
9651 vtable_entry_type, delta));
9655 /* Adds the initializers for the vcall offset entries in the vtable
9656 for BINFO (which is part of the class hierarchy dominated by VID->DERIVED)
9657 to VID->INITS. */
9659 static void
9660 build_vcall_offset_vtbl_entries (tree binfo, vtbl_init_data* vid)
9662 /* We only need these entries if this base is a virtual base. We
9663 compute the indices -- but do not add to the vtable -- when
9664 building the main vtable for a class. */
9665 if (binfo == TYPE_BINFO (vid->derived)
9666 || (BINFO_VIRTUAL_P (binfo)
9667 /* If BINFO is RTTI_BINFO, then (since BINFO does not
9668 correspond to VID->DERIVED), we are building a primary
9669 construction virtual table. Since this is a primary
9670 virtual table, we do not need the vcall offsets for
9671 BINFO. */
9672 && binfo != vid->rtti_binfo))
9674 /* We need a vcall offset for each of the virtual functions in this
9675 vtable. For example:
9677 class A { virtual void f (); };
9678 class B1 : virtual public A { virtual void f (); };
9679 class B2 : virtual public A { virtual void f (); };
9680 class C: public B1, public B2 { virtual void f (); };
9682 A C object has a primary base of B1, which has a primary base of A. A
9683 C also has a secondary base of B2, which no longer has a primary base
9684 of A. So the B2-in-C construction vtable needs a secondary vtable for
9685 A, which will adjust the A* to a B2* to call f. We have no way of
9686 knowing what (or even whether) this offset will be when we define B2,
9687 so we store this "vcall offset" in the A sub-vtable and look it up in
9688 a "virtual thunk" for B2::f.
9690 We need entries for all the functions in our primary vtable and
9691 in our non-virtual bases' secondary vtables. */
9692 vid->vbase = binfo;
9693 /* If we are just computing the vcall indices -- but do not need
9694 the actual entries -- not that. */
9695 if (!BINFO_VIRTUAL_P (binfo))
9696 vid->generate_vcall_entries = false;
9697 /* Now, walk through the non-virtual bases, adding vcall offsets. */
9698 add_vcall_offset_vtbl_entries_r (binfo, vid);
9702 /* Build vcall offsets, starting with those for BINFO. */
9704 static void
9705 add_vcall_offset_vtbl_entries_r (tree binfo, vtbl_init_data* vid)
9707 int i;
9708 tree primary_binfo;
9709 tree base_binfo;
9711 /* Don't walk into virtual bases -- except, of course, for the
9712 virtual base for which we are building vcall offsets. Any
9713 primary virtual base will have already had its offsets generated
9714 through the recursion in build_vcall_and_vbase_vtbl_entries. */
9715 if (BINFO_VIRTUAL_P (binfo) && vid->vbase != binfo)
9716 return;
9718 /* If BINFO has a primary base, process it first. */
9719 primary_binfo = get_primary_binfo (binfo);
9720 if (primary_binfo)
9721 add_vcall_offset_vtbl_entries_r (primary_binfo, vid);
9723 /* Add BINFO itself to the list. */
9724 add_vcall_offset_vtbl_entries_1 (binfo, vid);
9726 /* Scan the non-primary bases of BINFO. */
9727 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
9728 if (base_binfo != primary_binfo)
9729 add_vcall_offset_vtbl_entries_r (base_binfo, vid);
9732 /* Called from build_vcall_offset_vtbl_entries_r. */
9734 static void
9735 add_vcall_offset_vtbl_entries_1 (tree binfo, vtbl_init_data* vid)
9737 /* Make entries for the rest of the virtuals. */
9738 tree orig_fn;
9740 /* The ABI requires that the methods be processed in declaration
9741 order. */
9742 for (orig_fn = TYPE_METHODS (BINFO_TYPE (binfo));
9743 orig_fn;
9744 orig_fn = DECL_CHAIN (orig_fn))
9745 if (TREE_CODE (orig_fn) == FUNCTION_DECL && DECL_VINDEX (orig_fn))
9746 add_vcall_offset (orig_fn, binfo, vid);
9749 /* Add a vcall offset entry for ORIG_FN to the vtable. */
9751 static void
9752 add_vcall_offset (tree orig_fn, tree binfo, vtbl_init_data *vid)
9754 size_t i;
9755 tree vcall_offset;
9756 tree derived_entry;
9758 /* If there is already an entry for a function with the same
9759 signature as FN, then we do not need a second vcall offset.
9760 Check the list of functions already present in the derived
9761 class vtable. */
9762 FOR_EACH_VEC_SAFE_ELT (vid->fns, i, derived_entry)
9764 if (same_signature_p (derived_entry, orig_fn)
9765 /* We only use one vcall offset for virtual destructors,
9766 even though there are two virtual table entries. */
9767 || (DECL_DESTRUCTOR_P (derived_entry)
9768 && DECL_DESTRUCTOR_P (orig_fn)))
9769 return;
9772 /* If we are building these vcall offsets as part of building
9773 the vtable for the most derived class, remember the vcall
9774 offset. */
9775 if (vid->binfo == TYPE_BINFO (vid->derived))
9777 tree_pair_s elt = {orig_fn, vid->index};
9778 vec_safe_push (CLASSTYPE_VCALL_INDICES (vid->derived), elt);
9781 /* The next vcall offset will be found at a more negative
9782 offset. */
9783 vid->index = size_binop (MINUS_EXPR, vid->index,
9784 ssize_int (TARGET_VTABLE_DATA_ENTRY_DISTANCE));
9786 /* Keep track of this function. */
9787 vec_safe_push (vid->fns, orig_fn);
9789 if (vid->generate_vcall_entries)
9791 tree base;
9792 tree fn;
9794 /* Find the overriding function. */
9795 fn = find_final_overrider (vid->rtti_binfo, binfo, orig_fn);
9796 if (fn == error_mark_node)
9797 vcall_offset = build_zero_cst (vtable_entry_type);
9798 else
9800 base = TREE_VALUE (fn);
9802 /* The vbase we're working on is a primary base of
9803 vid->binfo. But it might be a lost primary, so its
9804 BINFO_OFFSET might be wrong, so we just use the
9805 BINFO_OFFSET from vid->binfo. */
9806 vcall_offset = size_diffop_loc (input_location,
9807 BINFO_OFFSET (base),
9808 BINFO_OFFSET (vid->binfo));
9809 vcall_offset = fold_build1_loc (input_location,
9810 NOP_EXPR, vtable_entry_type,
9811 vcall_offset);
9813 /* Add the initializer to the vtable. */
9814 CONSTRUCTOR_APPEND_ELT (vid->inits, NULL_TREE, vcall_offset);
9818 /* Return vtbl initializers for the RTTI entries corresponding to the
9819 BINFO's vtable. The RTTI entries should indicate the object given
9820 by VID->rtti_binfo. */
9822 static void
9823 build_rtti_vtbl_entries (tree binfo, vtbl_init_data* vid)
9825 tree b;
9826 tree t;
9827 tree offset;
9828 tree decl;
9829 tree init;
9831 t = BINFO_TYPE (vid->rtti_binfo);
9833 /* To find the complete object, we will first convert to our most
9834 primary base, and then add the offset in the vtbl to that value. */
9835 b = most_primary_binfo (binfo);
9836 offset = size_diffop_loc (input_location,
9837 BINFO_OFFSET (vid->rtti_binfo), BINFO_OFFSET (b));
9839 /* The second entry is the address of the typeinfo object. */
9840 if (flag_rtti)
9841 decl = build_address (get_tinfo_decl (t));
9842 else
9843 decl = integer_zero_node;
9845 /* Convert the declaration to a type that can be stored in the
9846 vtable. */
9847 init = build_nop (vfunc_ptr_type_node, decl);
9848 CONSTRUCTOR_APPEND_ELT (vid->inits, NULL_TREE, init);
9850 /* Add the offset-to-top entry. It comes earlier in the vtable than
9851 the typeinfo entry. Convert the offset to look like a
9852 function pointer, so that we can put it in the vtable. */
9853 init = build_nop (vfunc_ptr_type_node, offset);
9854 CONSTRUCTOR_APPEND_ELT (vid->inits, NULL_TREE, init);
9857 /* TRUE iff TYPE is uniquely derived from PARENT. Ignores
9858 accessibility. */
9860 bool
9861 uniquely_derived_from_p (tree parent, tree type)
9863 tree base = lookup_base (type, parent, ba_unique, NULL, tf_none);
9864 return base && base != error_mark_node;
9867 /* TRUE iff TYPE is publicly & uniquely derived from PARENT. */
9869 bool
9870 publicly_uniquely_derived_p (tree parent, tree type)
9872 tree base = lookup_base (type, parent, ba_ignore_scope | ba_check,
9873 NULL, tf_none);
9874 return base && base != error_mark_node;
9877 /* CTX1 and CTX2 are declaration contexts. Return the innermost common
9878 class between them, if any. */
9880 tree
9881 common_enclosing_class (tree ctx1, tree ctx2)
9883 if (!TYPE_P (ctx1) || !TYPE_P (ctx2))
9884 return NULL_TREE;
9885 gcc_assert (ctx1 == TYPE_MAIN_VARIANT (ctx1)
9886 && ctx2 == TYPE_MAIN_VARIANT (ctx2));
9887 if (ctx1 == ctx2)
9888 return ctx1;
9889 for (tree t = ctx1; TYPE_P (t); t = TYPE_CONTEXT (t))
9890 TYPE_MARKED_P (t) = true;
9891 tree found = NULL_TREE;
9892 for (tree t = ctx2; TYPE_P (t); t = TYPE_CONTEXT (t))
9893 if (TYPE_MARKED_P (t))
9895 found = t;
9896 break;
9898 for (tree t = ctx1; TYPE_P (t); t = TYPE_CONTEXT (t))
9899 TYPE_MARKED_P (t) = false;
9900 return found;
9903 #include "gt-cp-class.h"