Remove assert in get_def_bb_for_const
[official-gcc.git] / gcc / cp / class.c
blob31fa4b03136a0b4d187bfbc6f5a93bd1cc1b4fbd
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 bool check_field_decl (tree, tree, int *, int *);
143 static void check_field_decls (tree, tree *, int *, int *);
144 static tree *build_base_field (record_layout_info, tree, splay_tree, tree *);
145 static void build_base_fields (record_layout_info, splay_tree, tree *);
146 static void check_methods (tree);
147 static void remove_zero_width_bit_fields (tree);
148 static bool accessible_nvdtor_p (tree);
150 /* Used by find_flexarrays and related. */
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);
217 static bool type_maybe_constexpr_default_constructor (tree);
219 /* Variables shared between class.c and call.c. */
221 int n_vtables = 0;
222 int n_vtable_entries = 0;
223 int n_vtable_searches = 0;
224 int n_vtable_elems = 0;
225 int n_convert_harshness = 0;
226 int n_compute_conversion_costs = 0;
227 int n_inner_fields_searched = 0;
229 /* Return a COND_EXPR that executes TRUE_STMT if this execution of the
230 'structor is in charge of 'structing virtual bases, or FALSE_STMT
231 otherwise. */
233 tree
234 build_if_in_charge (tree true_stmt, tree false_stmt)
236 gcc_assert (DECL_HAS_IN_CHARGE_PARM_P (current_function_decl));
237 tree cmp = build2 (NE_EXPR, boolean_type_node,
238 current_in_charge_parm, integer_zero_node);
239 tree type = unlowered_expr_type (true_stmt);
240 if (VOID_TYPE_P (type))
241 type = unlowered_expr_type (false_stmt);
242 tree cond = build3 (COND_EXPR, type,
243 cmp, true_stmt, false_stmt);
244 return cond;
247 /* Convert to or from a base subobject. EXPR is an expression of type
248 `A' or `A*', an expression of type `B' or `B*' is returned. To
249 convert A to a base B, CODE is PLUS_EXPR and BINFO is the binfo for
250 the B base instance within A. To convert base A to derived B, CODE
251 is MINUS_EXPR and BINFO is the binfo for the A instance within B.
252 In this latter case, A must not be a morally virtual base of B.
253 NONNULL is true if EXPR is known to be non-NULL (this is only
254 needed when EXPR is of pointer type). CV qualifiers are preserved
255 from EXPR. */
257 tree
258 build_base_path (enum tree_code code,
259 tree expr,
260 tree binfo,
261 int nonnull,
262 tsubst_flags_t complain)
264 tree v_binfo = NULL_TREE;
265 tree d_binfo = NULL_TREE;
266 tree probe;
267 tree offset;
268 tree target_type;
269 tree null_test = NULL;
270 tree ptr_target_type;
271 int fixed_type_p;
272 int want_pointer = TYPE_PTR_P (TREE_TYPE (expr));
273 bool has_empty = false;
274 bool virtual_access;
275 bool rvalue = false;
277 if (expr == error_mark_node || binfo == error_mark_node || !binfo)
278 return error_mark_node;
280 for (probe = binfo; probe; probe = BINFO_INHERITANCE_CHAIN (probe))
282 d_binfo = probe;
283 if (is_empty_class (BINFO_TYPE (probe)))
284 has_empty = true;
285 if (!v_binfo && BINFO_VIRTUAL_P (probe))
286 v_binfo = probe;
289 probe = TYPE_MAIN_VARIANT (TREE_TYPE (expr));
290 if (want_pointer)
291 probe = TYPE_MAIN_VARIANT (TREE_TYPE (probe));
293 if (code == PLUS_EXPR
294 && !SAME_BINFO_TYPE_P (BINFO_TYPE (d_binfo), probe))
296 /* This can happen when adjust_result_of_qualified_name_lookup can't
297 find a unique base binfo in a call to a member function. We
298 couldn't give the diagnostic then since we might have been calling
299 a static member function, so we do it now. */
300 if (complain & tf_error)
302 tree base = lookup_base (probe, BINFO_TYPE (d_binfo),
303 ba_unique, NULL, complain);
304 gcc_assert (base == error_mark_node);
306 return error_mark_node;
309 gcc_assert ((code == MINUS_EXPR
310 && SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), probe))
311 || code == PLUS_EXPR);
313 if (binfo == d_binfo)
314 /* Nothing to do. */
315 return expr;
317 if (code == MINUS_EXPR && v_binfo)
319 if (complain & tf_error)
321 if (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), BINFO_TYPE (v_binfo)))
323 if (want_pointer)
324 error ("cannot convert from pointer to base class %qT to "
325 "pointer to derived class %qT because the base is "
326 "virtual", BINFO_TYPE (binfo), BINFO_TYPE (d_binfo));
327 else
328 error ("cannot convert from base class %qT to derived "
329 "class %qT because the base is virtual",
330 BINFO_TYPE (binfo), BINFO_TYPE (d_binfo));
332 else
334 if (want_pointer)
335 error ("cannot convert from pointer to base class %qT to "
336 "pointer to derived class %qT via virtual base %qT",
337 BINFO_TYPE (binfo), BINFO_TYPE (d_binfo),
338 BINFO_TYPE (v_binfo));
339 else
340 error ("cannot convert from base class %qT to derived "
341 "class %qT via virtual base %qT", BINFO_TYPE (binfo),
342 BINFO_TYPE (d_binfo), BINFO_TYPE (v_binfo));
345 return error_mark_node;
348 if (!want_pointer)
350 rvalue = !real_lvalue_p (expr);
351 /* This must happen before the call to save_expr. */
352 expr = cp_build_addr_expr (expr, complain);
354 else
355 expr = mark_rvalue_use (expr);
357 offset = BINFO_OFFSET (binfo);
358 fixed_type_p = resolves_to_fixed_type_p (expr, &nonnull);
359 target_type = code == PLUS_EXPR ? BINFO_TYPE (binfo) : BINFO_TYPE (d_binfo);
360 /* TARGET_TYPE has been extracted from BINFO, and, is therefore always
361 cv-unqualified. Extract the cv-qualifiers from EXPR so that the
362 expression returned matches the input. */
363 target_type = cp_build_qualified_type
364 (target_type, cp_type_quals (TREE_TYPE (TREE_TYPE (expr))));
365 ptr_target_type = build_pointer_type (target_type);
367 /* Do we need to look in the vtable for the real offset? */
368 virtual_access = (v_binfo && fixed_type_p <= 0);
370 /* Don't bother with the calculations inside sizeof; they'll ICE if the
371 source type is incomplete and the pointer value doesn't matter. In a
372 template (even in instantiate_non_dependent_expr), we don't have vtables
373 set up properly yet, and the value doesn't matter there either; we're
374 just interested in the result of overload resolution. */
375 if (cp_unevaluated_operand != 0
376 || in_template_function ())
378 expr = build_nop (ptr_target_type, expr);
379 goto indout;
382 /* If we're in an NSDMI, we don't have the full constructor context yet
383 that we need for converting to a virtual base, so just build a stub
384 CONVERT_EXPR and expand it later in bot_replace. */
385 if (virtual_access && fixed_type_p < 0
386 && current_scope () != current_function_decl)
388 expr = build1 (CONVERT_EXPR, ptr_target_type, expr);
389 CONVERT_EXPR_VBASE_PATH (expr) = true;
390 goto indout;
393 /* Do we need to check for a null pointer? */
394 if (want_pointer && !nonnull)
396 /* If we know the conversion will not actually change the value
397 of EXPR, then we can avoid testing the expression for NULL.
398 We have to avoid generating a COMPONENT_REF for a base class
399 field, because other parts of the compiler know that such
400 expressions are always non-NULL. */
401 if (!virtual_access && integer_zerop (offset))
402 return build_nop (ptr_target_type, expr);
403 null_test = error_mark_node;
406 /* Protect against multiple evaluation if necessary. */
407 if (TREE_SIDE_EFFECTS (expr) && (null_test || virtual_access))
408 expr = save_expr (expr);
410 /* Now that we've saved expr, build the real null test. */
411 if (null_test)
413 tree zero = cp_convert (TREE_TYPE (expr), nullptr_node, complain);
414 null_test = build2_loc (input_location, NE_EXPR, boolean_type_node,
415 expr, zero);
416 /* This is a compiler generated comparison, don't emit
417 e.g. -Wnonnull-compare warning for it. */
418 TREE_NO_WARNING (null_test) = 1;
421 /* If this is a simple base reference, express it as a COMPONENT_REF. */
422 if (code == PLUS_EXPR && !virtual_access
423 /* We don't build base fields for empty bases, and they aren't very
424 interesting to the optimizers anyway. */
425 && !has_empty)
427 expr = cp_build_indirect_ref (expr, RO_NULL, complain);
428 expr = build_simple_base_path (expr, binfo);
429 if (rvalue)
430 expr = move (expr);
431 if (want_pointer)
432 expr = build_address (expr);
433 target_type = TREE_TYPE (expr);
434 goto out;
437 if (virtual_access)
439 /* Going via virtual base V_BINFO. We need the static offset
440 from V_BINFO to BINFO, and the dynamic offset from D_BINFO to
441 V_BINFO. That offset is an entry in D_BINFO's vtable. */
442 tree v_offset;
444 if (fixed_type_p < 0 && in_base_initializer)
446 /* In a base member initializer, we cannot rely on the
447 vtable being set up. We have to indirect via the
448 vtt_parm. */
449 tree t;
451 t = TREE_TYPE (TYPE_VFIELD (current_class_type));
452 t = build_pointer_type (t);
453 v_offset = fold_convert (t, current_vtt_parm);
454 v_offset = cp_build_indirect_ref (v_offset, RO_NULL, complain);
456 else
458 tree t = expr;
459 if ((flag_sanitize & SANITIZE_VPTR) && fixed_type_p == 0)
461 t = cp_ubsan_maybe_instrument_cast_to_vbase (input_location,
462 probe, expr);
463 if (t == NULL_TREE)
464 t = expr;
466 v_offset = build_vfield_ref (cp_build_indirect_ref (t, RO_NULL,
467 complain),
468 TREE_TYPE (TREE_TYPE (expr)));
471 if (v_offset == error_mark_node)
472 return error_mark_node;
474 v_offset = fold_build_pointer_plus (v_offset, BINFO_VPTR_FIELD (v_binfo));
475 v_offset = build1 (NOP_EXPR,
476 build_pointer_type (ptrdiff_type_node),
477 v_offset);
478 v_offset = cp_build_indirect_ref (v_offset, RO_NULL, complain);
479 TREE_CONSTANT (v_offset) = 1;
481 offset = convert_to_integer (ptrdiff_type_node,
482 size_diffop_loc (input_location, offset,
483 BINFO_OFFSET (v_binfo)));
485 if (!integer_zerop (offset))
486 v_offset = build2 (code, ptrdiff_type_node, v_offset, offset);
488 if (fixed_type_p < 0)
489 /* Negative fixed_type_p means this is a constructor or destructor;
490 virtual base layout is fixed in in-charge [cd]tors, but not in
491 base [cd]tors. */
492 offset = build_if_in_charge
493 (convert_to_integer (ptrdiff_type_node, BINFO_OFFSET (binfo)),
494 v_offset);
495 else
496 offset = v_offset;
499 if (want_pointer)
500 target_type = ptr_target_type;
502 expr = build1 (NOP_EXPR, ptr_target_type, expr);
504 if (!integer_zerop (offset))
506 offset = fold_convert (sizetype, offset);
507 if (code == MINUS_EXPR)
508 offset = fold_build1_loc (input_location, NEGATE_EXPR, sizetype, offset);
509 expr = fold_build_pointer_plus (expr, offset);
511 else
512 null_test = NULL;
514 indout:
515 if (!want_pointer)
517 expr = cp_build_indirect_ref (expr, RO_NULL, complain);
518 if (rvalue)
519 expr = move (expr);
522 out:
523 if (null_test)
524 expr = fold_build3_loc (input_location, COND_EXPR, target_type, null_test, expr,
525 build_zero_cst (target_type));
527 return expr;
530 /* Subroutine of build_base_path; EXPR and BINFO are as in that function.
531 Perform a derived-to-base conversion by recursively building up a
532 sequence of COMPONENT_REFs to the appropriate base fields. */
534 static tree
535 build_simple_base_path (tree expr, tree binfo)
537 tree type = BINFO_TYPE (binfo);
538 tree d_binfo = BINFO_INHERITANCE_CHAIN (binfo);
539 tree field;
541 if (d_binfo == NULL_TREE)
543 tree temp;
545 gcc_assert (TYPE_MAIN_VARIANT (TREE_TYPE (expr)) == type);
547 /* Transform `(a, b).x' into `(*(a, &b)).x', `(a ? b : c).x'
548 into `(*(a ? &b : &c)).x', and so on. A COND_EXPR is only
549 an lvalue in the front end; only _DECLs and _REFs are lvalues
550 in the back end. */
551 temp = unary_complex_lvalue (ADDR_EXPR, expr);
552 if (temp)
553 expr = cp_build_indirect_ref (temp, RO_NULL, tf_warning_or_error);
555 return expr;
558 /* Recurse. */
559 expr = build_simple_base_path (expr, d_binfo);
561 for (field = TYPE_FIELDS (BINFO_TYPE (d_binfo));
562 field; field = DECL_CHAIN (field))
563 /* Is this the base field created by build_base_field? */
564 if (TREE_CODE (field) == FIELD_DECL
565 && DECL_FIELD_IS_BASE (field)
566 && TREE_TYPE (field) == type
567 /* If we're looking for a field in the most-derived class,
568 also check the field offset; we can have two base fields
569 of the same type if one is an indirect virtual base and one
570 is a direct non-virtual base. */
571 && (BINFO_INHERITANCE_CHAIN (d_binfo)
572 || tree_int_cst_equal (byte_position (field),
573 BINFO_OFFSET (binfo))))
575 /* We don't use build_class_member_access_expr here, as that
576 has unnecessary checks, and more importantly results in
577 recursive calls to dfs_walk_once. */
578 int type_quals = cp_type_quals (TREE_TYPE (expr));
580 expr = build3 (COMPONENT_REF,
581 cp_build_qualified_type (type, type_quals),
582 expr, field, NULL_TREE);
583 /* Mark the expression const or volatile, as appropriate.
584 Even though we've dealt with the type above, we still have
585 to mark the expression itself. */
586 if (type_quals & TYPE_QUAL_CONST)
587 TREE_READONLY (expr) = 1;
588 if (type_quals & TYPE_QUAL_VOLATILE)
589 TREE_THIS_VOLATILE (expr) = 1;
591 return expr;
594 /* Didn't find the base field?!? */
595 gcc_unreachable ();
598 /* Convert OBJECT to the base TYPE. OBJECT is an expression whose
599 type is a class type or a pointer to a class type. In the former
600 case, TYPE is also a class type; in the latter it is another
601 pointer type. If CHECK_ACCESS is true, an error message is emitted
602 if TYPE is inaccessible. If OBJECT has pointer type, the value is
603 assumed to be non-NULL. */
605 tree
606 convert_to_base (tree object, tree type, bool check_access, bool nonnull,
607 tsubst_flags_t complain)
609 tree binfo;
610 tree object_type;
612 if (TYPE_PTR_P (TREE_TYPE (object)))
614 object_type = TREE_TYPE (TREE_TYPE (object));
615 type = TREE_TYPE (type);
617 else
618 object_type = TREE_TYPE (object);
620 binfo = lookup_base (object_type, type, check_access ? ba_check : ba_unique,
621 NULL, complain);
622 if (!binfo || binfo == error_mark_node)
623 return error_mark_node;
625 return build_base_path (PLUS_EXPR, object, binfo, nonnull, complain);
628 /* EXPR is an expression with unqualified class type. BASE is a base
629 binfo of that class type. Returns EXPR, converted to the BASE
630 type. This function assumes that EXPR is the most derived class;
631 therefore virtual bases can be found at their static offsets. */
633 tree
634 convert_to_base_statically (tree expr, tree base)
636 tree expr_type;
638 expr_type = TREE_TYPE (expr);
639 if (!SAME_BINFO_TYPE_P (BINFO_TYPE (base), expr_type))
641 /* If this is a non-empty base, use a COMPONENT_REF. */
642 if (!is_empty_class (BINFO_TYPE (base)))
643 return build_simple_base_path (expr, base);
645 /* We use fold_build2 and fold_convert below to simplify the trees
646 provided to the optimizers. It is not safe to call these functions
647 when processing a template because they do not handle C++-specific
648 trees. */
649 gcc_assert (!processing_template_decl);
650 expr = cp_build_addr_expr (expr, tf_warning_or_error);
651 if (!integer_zerop (BINFO_OFFSET (base)))
652 expr = fold_build_pointer_plus_loc (input_location,
653 expr, BINFO_OFFSET (base));
654 expr = fold_convert (build_pointer_type (BINFO_TYPE (base)), expr);
655 expr = build_fold_indirect_ref_loc (input_location, expr);
658 return expr;
662 tree
663 build_vfield_ref (tree datum, tree type)
665 tree vfield, vcontext;
667 if (datum == error_mark_node
668 /* Can happen in case of duplicate base types (c++/59082). */
669 || !TYPE_VFIELD (type))
670 return error_mark_node;
672 /* First, convert to the requested type. */
673 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (datum), type))
674 datum = convert_to_base (datum, type, /*check_access=*/false,
675 /*nonnull=*/true, tf_warning_or_error);
677 /* Second, the requested type may not be the owner of its own vptr.
678 If not, convert to the base class that owns it. We cannot use
679 convert_to_base here, because VCONTEXT may appear more than once
680 in the inheritance hierarchy of TYPE, and thus direct conversion
681 between the types may be ambiguous. Following the path back up
682 one step at a time via primary bases avoids the problem. */
683 vfield = TYPE_VFIELD (type);
684 vcontext = DECL_CONTEXT (vfield);
685 while (!same_type_ignoring_top_level_qualifiers_p (vcontext, type))
687 datum = build_simple_base_path (datum, CLASSTYPE_PRIMARY_BINFO (type));
688 type = TREE_TYPE (datum);
691 return build3 (COMPONENT_REF, TREE_TYPE (vfield), datum, vfield, NULL_TREE);
694 /* Given an object INSTANCE, return an expression which yields the
695 vtable element corresponding to INDEX. There are many special
696 cases for INSTANCE which we take care of here, mainly to avoid
697 creating extra tree nodes when we don't have to. */
699 static tree
700 build_vtbl_ref_1 (tree instance, tree idx)
702 tree aref;
703 tree vtbl = NULL_TREE;
705 /* Try to figure out what a reference refers to, and
706 access its virtual function table directly. */
708 int cdtorp = 0;
709 tree fixed_type = fixed_type_or_null (instance, NULL, &cdtorp);
711 tree basetype = non_reference (TREE_TYPE (instance));
713 if (fixed_type && !cdtorp)
715 tree binfo = lookup_base (fixed_type, basetype,
716 ba_unique, NULL, tf_none);
717 if (binfo && binfo != error_mark_node)
718 vtbl = unshare_expr (BINFO_VTABLE (binfo));
721 if (!vtbl)
722 vtbl = build_vfield_ref (instance, basetype);
724 aref = build_array_ref (input_location, vtbl, idx);
725 TREE_CONSTANT (aref) |= TREE_CONSTANT (vtbl) && TREE_CONSTANT (idx);
727 return aref;
730 tree
731 build_vtbl_ref (tree instance, tree idx)
733 tree aref = build_vtbl_ref_1 (instance, idx);
735 return aref;
738 /* Given a stable object pointer INSTANCE_PTR, return an expression which
739 yields a function pointer corresponding to vtable element INDEX. */
741 tree
742 build_vfn_ref (tree instance_ptr, tree idx)
744 tree aref;
746 aref = build_vtbl_ref_1 (cp_build_indirect_ref (instance_ptr, RO_NULL,
747 tf_warning_or_error),
748 idx);
750 /* When using function descriptors, the address of the
751 vtable entry is treated as a function pointer. */
752 if (TARGET_VTABLE_USES_DESCRIPTORS)
753 aref = build1 (NOP_EXPR, TREE_TYPE (aref),
754 cp_build_addr_expr (aref, tf_warning_or_error));
756 /* Remember this as a method reference, for later devirtualization. */
757 aref = build3 (OBJ_TYPE_REF, TREE_TYPE (aref), aref, instance_ptr, idx);
759 return aref;
762 /* Return the name of the virtual function table (as an IDENTIFIER_NODE)
763 for the given TYPE. */
765 static tree
766 get_vtable_name (tree type)
768 return mangle_vtbl_for_type (type);
771 /* DECL is an entity associated with TYPE, like a virtual table or an
772 implicitly generated constructor. Determine whether or not DECL
773 should have external or internal linkage at the object file
774 level. This routine does not deal with COMDAT linkage and other
775 similar complexities; it simply sets TREE_PUBLIC if it possible for
776 entities in other translation units to contain copies of DECL, in
777 the abstract. */
779 void
780 set_linkage_according_to_type (tree /*type*/, tree decl)
782 TREE_PUBLIC (decl) = 1;
783 determine_visibility (decl);
786 /* Create a VAR_DECL for a primary or secondary vtable for CLASS_TYPE.
787 (For a secondary vtable for B-in-D, CLASS_TYPE should be D, not B.)
788 Use NAME for the name of the vtable, and VTABLE_TYPE for its type. */
790 static tree
791 build_vtable (tree class_type, tree name, tree vtable_type)
793 tree decl;
795 decl = build_lang_decl (VAR_DECL, name, vtable_type);
796 /* vtable names are already mangled; give them their DECL_ASSEMBLER_NAME
797 now to avoid confusion in mangle_decl. */
798 SET_DECL_ASSEMBLER_NAME (decl, name);
799 DECL_CONTEXT (decl) = class_type;
800 DECL_ARTIFICIAL (decl) = 1;
801 TREE_STATIC (decl) = 1;
802 TREE_READONLY (decl) = 1;
803 DECL_VIRTUAL_P (decl) = 1;
804 SET_DECL_ALIGN (decl, TARGET_VTABLE_ENTRY_ALIGN);
805 DECL_USER_ALIGN (decl) = true;
806 DECL_VTABLE_OR_VTT_P (decl) = 1;
807 set_linkage_according_to_type (class_type, decl);
808 /* The vtable has not been defined -- yet. */
809 DECL_EXTERNAL (decl) = 1;
810 DECL_NOT_REALLY_EXTERN (decl) = 1;
812 /* Mark the VAR_DECL node representing the vtable itself as a
813 "gratuitous" one, thereby forcing dwarfout.c to ignore it. It
814 is rather important that such things be ignored because any
815 effort to actually generate DWARF for them will run into
816 trouble when/if we encounter code like:
818 #pragma interface
819 struct S { virtual void member (); };
821 because the artificial declaration of the vtable itself (as
822 manufactured by the g++ front end) will say that the vtable is
823 a static member of `S' but only *after* the debug output for
824 the definition of `S' has already been output. This causes
825 grief because the DWARF entry for the definition of the vtable
826 will try to refer back to an earlier *declaration* of the
827 vtable as a static member of `S' and there won't be one. We
828 might be able to arrange to have the "vtable static member"
829 attached to the member list for `S' before the debug info for
830 `S' get written (which would solve the problem) but that would
831 require more intrusive changes to the g++ front end. */
832 DECL_IGNORED_P (decl) = 1;
834 return decl;
837 /* Get the VAR_DECL of the vtable for TYPE. TYPE need not be polymorphic,
838 or even complete. If this does not exist, create it. If COMPLETE is
839 nonzero, then complete the definition of it -- that will render it
840 impossible to actually build the vtable, but is useful to get at those
841 which are known to exist in the runtime. */
843 tree
844 get_vtable_decl (tree type, int complete)
846 tree decl;
848 if (CLASSTYPE_VTABLES (type))
849 return CLASSTYPE_VTABLES (type);
851 decl = build_vtable (type, get_vtable_name (type), vtbl_type_node);
852 CLASSTYPE_VTABLES (type) = decl;
854 if (complete)
856 DECL_EXTERNAL (decl) = 1;
857 cp_finish_decl (decl, NULL_TREE, false, NULL_TREE, 0);
860 return decl;
863 /* Build the primary virtual function table for TYPE. If BINFO is
864 non-NULL, build the vtable starting with the initial approximation
865 that it is the same as the one which is the head of the association
866 list. Returns a nonzero value if a new vtable is actually
867 created. */
869 static int
870 build_primary_vtable (tree binfo, tree type)
872 tree decl;
873 tree virtuals;
875 decl = get_vtable_decl (type, /*complete=*/0);
877 if (binfo)
879 if (BINFO_NEW_VTABLE_MARKED (binfo))
880 /* We have already created a vtable for this base, so there's
881 no need to do it again. */
882 return 0;
884 virtuals = copy_list (BINFO_VIRTUALS (binfo));
885 TREE_TYPE (decl) = TREE_TYPE (get_vtbl_decl_for_binfo (binfo));
886 DECL_SIZE (decl) = TYPE_SIZE (TREE_TYPE (decl));
887 DECL_SIZE_UNIT (decl) = TYPE_SIZE_UNIT (TREE_TYPE (decl));
889 else
891 gcc_assert (TREE_TYPE (decl) == vtbl_type_node);
892 virtuals = NULL_TREE;
895 if (GATHER_STATISTICS)
897 n_vtables += 1;
898 n_vtable_elems += list_length (virtuals);
901 /* Initialize the association list for this type, based
902 on our first approximation. */
903 BINFO_VTABLE (TYPE_BINFO (type)) = decl;
904 BINFO_VIRTUALS (TYPE_BINFO (type)) = virtuals;
905 SET_BINFO_NEW_VTABLE_MARKED (TYPE_BINFO (type));
906 return 1;
909 /* Give BINFO a new virtual function table which is initialized
910 with a skeleton-copy of its original initialization. The only
911 entry that changes is the `delta' entry, so we can really
912 share a lot of structure.
914 FOR_TYPE is the most derived type which caused this table to
915 be needed.
917 Returns nonzero if we haven't met BINFO before.
919 The order in which vtables are built (by calling this function) for
920 an object must remain the same, otherwise a binary incompatibility
921 can result. */
923 static int
924 build_secondary_vtable (tree binfo)
926 if (BINFO_NEW_VTABLE_MARKED (binfo))
927 /* We already created a vtable for this base. There's no need to
928 do it again. */
929 return 0;
931 /* Remember that we've created a vtable for this BINFO, so that we
932 don't try to do so again. */
933 SET_BINFO_NEW_VTABLE_MARKED (binfo);
935 /* Make fresh virtual list, so we can smash it later. */
936 BINFO_VIRTUALS (binfo) = copy_list (BINFO_VIRTUALS (binfo));
938 /* Secondary vtables are laid out as part of the same structure as
939 the primary vtable. */
940 BINFO_VTABLE (binfo) = NULL_TREE;
941 return 1;
944 /* Create a new vtable for BINFO which is the hierarchy dominated by
945 T. Return nonzero if we actually created a new vtable. */
947 static int
948 make_new_vtable (tree t, tree binfo)
950 if (binfo == TYPE_BINFO (t))
951 /* In this case, it is *type*'s vtable we are modifying. We start
952 with the approximation that its vtable is that of the
953 immediate base class. */
954 return build_primary_vtable (binfo, t);
955 else
956 /* This is our very own copy of `basetype' to play with. Later,
957 we will fill in all the virtual functions that override the
958 virtual functions in these base classes which are not defined
959 by the current type. */
960 return build_secondary_vtable (binfo);
963 /* Make *VIRTUALS, an entry on the BINFO_VIRTUALS list for BINFO
964 (which is in the hierarchy dominated by T) list FNDECL as its
965 BV_FN. DELTA is the required constant adjustment from the `this'
966 pointer where the vtable entry appears to the `this' required when
967 the function is actually called. */
969 static void
970 modify_vtable_entry (tree t,
971 tree binfo,
972 tree fndecl,
973 tree delta,
974 tree *virtuals)
976 tree v;
978 v = *virtuals;
980 if (fndecl != BV_FN (v)
981 || !tree_int_cst_equal (delta, BV_DELTA (v)))
983 /* We need a new vtable for BINFO. */
984 if (make_new_vtable (t, binfo))
986 /* If we really did make a new vtable, we also made a copy
987 of the BINFO_VIRTUALS list. Now, we have to find the
988 corresponding entry in that list. */
989 *virtuals = BINFO_VIRTUALS (binfo);
990 while (BV_FN (*virtuals) != BV_FN (v))
991 *virtuals = TREE_CHAIN (*virtuals);
992 v = *virtuals;
995 BV_DELTA (v) = delta;
996 BV_VCALL_INDEX (v) = NULL_TREE;
997 BV_FN (v) = fndecl;
1002 /* Add method METHOD to class TYPE. If USING_DECL is non-null, it is
1003 the USING_DECL naming METHOD. Returns true if the method could be
1004 added to the method vec. */
1006 bool
1007 add_method (tree type, tree method, tree using_decl)
1009 unsigned slot;
1010 tree overload;
1011 bool template_conv_p = false;
1012 bool conv_p;
1013 vec<tree, va_gc> *method_vec;
1014 bool complete_p;
1015 bool insert_p = false;
1016 tree current_fns;
1017 tree fns;
1019 if (method == error_mark_node)
1020 return false;
1022 complete_p = COMPLETE_TYPE_P (type);
1023 conv_p = DECL_CONV_FN_P (method);
1024 if (conv_p)
1025 template_conv_p = (TREE_CODE (method) == TEMPLATE_DECL
1026 && DECL_TEMPLATE_CONV_FN_P (method));
1028 method_vec = CLASSTYPE_METHOD_VEC (type);
1029 if (!method_vec)
1031 /* Make a new method vector. We start with 8 entries. We must
1032 allocate at least two (for constructors and destructors), and
1033 we're going to end up with an assignment operator at some
1034 point as well. */
1035 vec_alloc (method_vec, 8);
1036 /* Create slots for constructors and destructors. */
1037 method_vec->quick_push (NULL_TREE);
1038 method_vec->quick_push (NULL_TREE);
1039 CLASSTYPE_METHOD_VEC (type) = method_vec;
1042 /* Maintain TYPE_HAS_USER_CONSTRUCTOR, etc. */
1043 grok_special_member_properties (method);
1045 /* Constructors and destructors go in special slots. */
1046 if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (method))
1047 slot = CLASSTYPE_CONSTRUCTOR_SLOT;
1048 else if (DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (method))
1050 slot = CLASSTYPE_DESTRUCTOR_SLOT;
1052 if (TYPE_FOR_JAVA (type))
1054 if (!DECL_ARTIFICIAL (method))
1055 error ("Java class %qT cannot have a destructor", type);
1056 else if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
1057 error ("Java class %qT cannot have an implicit non-trivial "
1058 "destructor",
1059 type);
1062 else
1064 tree m;
1066 insert_p = true;
1067 /* See if we already have an entry with this name. */
1068 for (slot = CLASSTYPE_FIRST_CONVERSION_SLOT;
1069 vec_safe_iterate (method_vec, slot, &m);
1070 ++slot)
1072 m = OVL_CURRENT (m);
1073 if (template_conv_p)
1075 if (TREE_CODE (m) == TEMPLATE_DECL
1076 && DECL_TEMPLATE_CONV_FN_P (m))
1077 insert_p = false;
1078 break;
1080 if (conv_p && !DECL_CONV_FN_P (m))
1081 break;
1082 if (DECL_NAME (m) == DECL_NAME (method))
1084 insert_p = false;
1085 break;
1087 if (complete_p
1088 && !DECL_CONV_FN_P (m)
1089 && DECL_NAME (m) > DECL_NAME (method))
1090 break;
1093 current_fns = insert_p ? NULL_TREE : (*method_vec)[slot];
1095 /* Check to see if we've already got this method. */
1096 for (fns = current_fns; fns; fns = OVL_NEXT (fns))
1098 tree fn = OVL_CURRENT (fns);
1099 tree fn_type;
1100 tree method_type;
1101 tree parms1;
1102 tree parms2;
1104 if (TREE_CODE (fn) != TREE_CODE (method))
1105 continue;
1107 /* [over.load] Member function declarations with the
1108 same name and the same parameter types cannot be
1109 overloaded if any of them is a static member
1110 function declaration.
1112 [over.load] Member function declarations with the same name and
1113 the same parameter-type-list as well as member function template
1114 declarations with the same name, the same parameter-type-list, and
1115 the same template parameter lists cannot be overloaded if any of
1116 them, but not all, have a ref-qualifier.
1118 [namespace.udecl] When a using-declaration brings names
1119 from a base class into a derived class scope, member
1120 functions in the derived class override and/or hide member
1121 functions with the same name and parameter types in a base
1122 class (rather than conflicting). */
1123 fn_type = TREE_TYPE (fn);
1124 method_type = TREE_TYPE (method);
1125 parms1 = TYPE_ARG_TYPES (fn_type);
1126 parms2 = TYPE_ARG_TYPES (method_type);
1128 /* Compare the quals on the 'this' parm. Don't compare
1129 the whole types, as used functions are treated as
1130 coming from the using class in overload resolution. */
1131 if (! DECL_STATIC_FUNCTION_P (fn)
1132 && ! DECL_STATIC_FUNCTION_P (method)
1133 /* Either both or neither need to be ref-qualified for
1134 differing quals to allow overloading. */
1135 && (FUNCTION_REF_QUALIFIED (fn_type)
1136 == FUNCTION_REF_QUALIFIED (method_type))
1137 && (type_memfn_quals (fn_type) != type_memfn_quals (method_type)
1138 || type_memfn_rqual (fn_type) != type_memfn_rqual (method_type)))
1139 continue;
1141 /* For templates, the return type and template parameters
1142 must be identical. */
1143 if (TREE_CODE (fn) == TEMPLATE_DECL
1144 && (!same_type_p (TREE_TYPE (fn_type),
1145 TREE_TYPE (method_type))
1146 || !comp_template_parms (DECL_TEMPLATE_PARMS (fn),
1147 DECL_TEMPLATE_PARMS (method))))
1148 continue;
1150 if (! DECL_STATIC_FUNCTION_P (fn))
1151 parms1 = TREE_CHAIN (parms1);
1152 if (! DECL_STATIC_FUNCTION_P (method))
1153 parms2 = TREE_CHAIN (parms2);
1155 if (compparms (parms1, parms2)
1156 && (!DECL_CONV_FN_P (fn)
1157 || same_type_p (TREE_TYPE (fn_type),
1158 TREE_TYPE (method_type)))
1159 && equivalently_constrained (fn, method))
1161 /* For function versions, their parms and types match
1162 but they are not duplicates. Record function versions
1163 as and when they are found. extern "C" functions are
1164 not treated as versions. */
1165 if (TREE_CODE (fn) == FUNCTION_DECL
1166 && TREE_CODE (method) == FUNCTION_DECL
1167 && !DECL_EXTERN_C_P (fn)
1168 && !DECL_EXTERN_C_P (method)
1169 && targetm.target_option.function_versions (fn, method))
1171 /* Mark functions as versions if necessary. Modify the mangled
1172 decl name if necessary. */
1173 if (!DECL_FUNCTION_VERSIONED (fn))
1175 DECL_FUNCTION_VERSIONED (fn) = 1;
1176 if (DECL_ASSEMBLER_NAME_SET_P (fn))
1177 mangle_decl (fn);
1179 if (!DECL_FUNCTION_VERSIONED (method))
1181 DECL_FUNCTION_VERSIONED (method) = 1;
1182 if (DECL_ASSEMBLER_NAME_SET_P (method))
1183 mangle_decl (method);
1185 cgraph_node::record_function_versions (fn, method);
1186 continue;
1188 if (DECL_INHERITED_CTOR_BASE (method))
1190 if (DECL_INHERITED_CTOR_BASE (fn))
1192 error_at (DECL_SOURCE_LOCATION (method),
1193 "%q#D inherited from %qT", method,
1194 DECL_INHERITED_CTOR_BASE (method));
1195 error_at (DECL_SOURCE_LOCATION (fn),
1196 "conflicts with version inherited from %qT",
1197 DECL_INHERITED_CTOR_BASE (fn));
1199 /* Otherwise defer to the other function. */
1200 return false;
1202 if (using_decl)
1204 if (DECL_CONTEXT (fn) == type)
1205 /* Defer to the local function. */
1206 return false;
1208 else
1210 error ("%q+#D cannot be overloaded", method);
1211 error ("with %q+#D", fn);
1214 /* We don't call duplicate_decls here to merge the
1215 declarations because that will confuse things if the
1216 methods have inline definitions. In particular, we
1217 will crash while processing the definitions. */
1218 return false;
1222 /* A class should never have more than one destructor. */
1223 if (current_fns && DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (method))
1224 return false;
1226 /* Add the new binding. */
1227 if (using_decl)
1229 overload = ovl_cons (method, current_fns);
1230 OVL_USED (overload) = true;
1232 else
1233 overload = build_overload (method, current_fns);
1235 if (conv_p)
1236 TYPE_HAS_CONVERSION (type) = 1;
1237 else if (slot >= CLASSTYPE_FIRST_CONVERSION_SLOT && !complete_p)
1238 push_class_level_binding (DECL_NAME (method), overload);
1240 if (insert_p)
1242 bool reallocated;
1244 /* We only expect to add few methods in the COMPLETE_P case, so
1245 just make room for one more method in that case. */
1246 if (complete_p)
1247 reallocated = vec_safe_reserve_exact (method_vec, 1);
1248 else
1249 reallocated = vec_safe_reserve (method_vec, 1);
1250 if (reallocated)
1251 CLASSTYPE_METHOD_VEC (type) = method_vec;
1252 if (slot == method_vec->length ())
1253 method_vec->quick_push (overload);
1254 else
1255 method_vec->quick_insert (slot, overload);
1257 else
1258 /* Replace the current slot. */
1259 (*method_vec)[slot] = overload;
1260 return true;
1263 /* Subroutines of finish_struct. */
1265 /* Change the access of FDECL to ACCESS in T. Return 1 if change was
1266 legit, otherwise return 0. */
1268 static int
1269 alter_access (tree t, tree fdecl, tree access)
1271 tree elem;
1273 if (!DECL_LANG_SPECIFIC (fdecl))
1274 retrofit_lang_decl (fdecl);
1276 gcc_assert (!DECL_DISCRIMINATOR_P (fdecl));
1278 elem = purpose_member (t, DECL_ACCESS (fdecl));
1279 if (elem)
1281 if (TREE_VALUE (elem) != access)
1283 if (TREE_CODE (TREE_TYPE (fdecl)) == FUNCTION_DECL)
1284 error ("conflicting access specifications for method"
1285 " %q+D, ignored", TREE_TYPE (fdecl));
1286 else
1287 error ("conflicting access specifications for field %qE, ignored",
1288 DECL_NAME (fdecl));
1290 else
1292 /* They're changing the access to the same thing they changed
1293 it to before. That's OK. */
1297 else
1299 perform_or_defer_access_check (TYPE_BINFO (t), fdecl, fdecl,
1300 tf_warning_or_error);
1301 DECL_ACCESS (fdecl) = tree_cons (t, access, DECL_ACCESS (fdecl));
1302 return 1;
1304 return 0;
1307 /* Process the USING_DECL, which is a member of T. */
1309 static void
1310 handle_using_decl (tree using_decl, tree t)
1312 tree decl = USING_DECL_DECLS (using_decl);
1313 tree name = DECL_NAME (using_decl);
1314 tree access
1315 = TREE_PRIVATE (using_decl) ? access_private_node
1316 : TREE_PROTECTED (using_decl) ? access_protected_node
1317 : access_public_node;
1318 tree flist = NULL_TREE;
1319 tree old_value;
1321 gcc_assert (!processing_template_decl && decl);
1323 old_value = lookup_member (t, name, /*protect=*/0, /*want_type=*/false,
1324 tf_warning_or_error);
1325 if (old_value)
1327 if (is_overloaded_fn (old_value))
1328 old_value = OVL_CURRENT (old_value);
1330 if (DECL_P (old_value) && DECL_CONTEXT (old_value) == t)
1331 /* OK */;
1332 else
1333 old_value = NULL_TREE;
1336 cp_emit_debug_info_for_using (decl, t);
1338 if (is_overloaded_fn (decl))
1339 flist = decl;
1341 if (! old_value)
1343 else if (is_overloaded_fn (old_value))
1345 if (flist)
1346 /* It's OK to use functions from a base when there are functions with
1347 the same name already present in the current class. */;
1348 else
1350 error ("%q+D invalid in %q#T", using_decl, t);
1351 error (" because of local method %q+#D with same name",
1352 OVL_CURRENT (old_value));
1353 return;
1356 else if (!DECL_ARTIFICIAL (old_value))
1358 error ("%q+D invalid in %q#T", using_decl, t);
1359 error (" because of local member %q+#D with same name", old_value);
1360 return;
1363 /* Make type T see field decl FDECL with access ACCESS. */
1364 if (flist)
1365 for (; flist; flist = OVL_NEXT (flist))
1367 add_method (t, OVL_CURRENT (flist), using_decl);
1368 alter_access (t, OVL_CURRENT (flist), access);
1370 else
1371 alter_access (t, decl, access);
1374 /* Data structure for find_abi_tags_r, below. */
1376 struct abi_tag_data
1378 tree t; // The type that we're checking for missing tags.
1379 tree subob; // The subobject of T that we're getting tags from.
1380 tree tags; // error_mark_node for diagnostics, or a list of missing tags.
1383 /* Subroutine of find_abi_tags_r. Handle a single TAG found on the class TP
1384 in the context of P. TAG can be either an identifier (the DECL_NAME of
1385 a tag NAMESPACE_DECL) or a STRING_CST (a tag attribute). */
1387 static void
1388 check_tag (tree tag, tree id, tree *tp, abi_tag_data *p)
1390 if (!IDENTIFIER_MARKED (id))
1392 if (p->tags != error_mark_node)
1394 /* We're collecting tags from template arguments or from
1395 the type of a variable or function return type. */
1396 p->tags = tree_cons (NULL_TREE, tag, p->tags);
1398 /* Don't inherit this tag multiple times. */
1399 IDENTIFIER_MARKED (id) = true;
1401 if (TYPE_P (p->t))
1403 /* Tags inherited from type template arguments are only used
1404 to avoid warnings. */
1405 ABI_TAG_IMPLICIT (p->tags) = true;
1406 return;
1408 /* For functions and variables we want to warn, too. */
1411 /* Otherwise we're diagnosing missing tags. */
1412 if (TREE_CODE (p->t) == FUNCTION_DECL)
1414 if (warning (OPT_Wabi_tag, "%qD inherits the %E ABI tag "
1415 "that %qT (used in its return type) has",
1416 p->t, tag, *tp))
1417 inform (location_of (*tp), "%qT declared here", *tp);
1419 else if (VAR_P (p->t))
1421 if (warning (OPT_Wabi_tag, "%qD inherits the %E ABI tag "
1422 "that %qT (used in its type) has", p->t, tag, *tp))
1423 inform (location_of (*tp), "%qT declared here", *tp);
1425 else if (TYPE_P (p->subob))
1427 if (warning (OPT_Wabi_tag, "%qT does not have the %E ABI tag "
1428 "that base %qT has", p->t, tag, p->subob))
1429 inform (location_of (p->subob), "%qT declared here",
1430 p->subob);
1432 else
1434 if (warning (OPT_Wabi_tag, "%qT does not have the %E ABI tag "
1435 "that %qT (used in the type of %qD) has",
1436 p->t, tag, *tp, p->subob))
1438 inform (location_of (p->subob), "%qD declared here",
1439 p->subob);
1440 inform (location_of (*tp), "%qT declared here", *tp);
1446 /* Find all the ABI tags in the attribute list ATTR and either call
1447 check_tag (if TP is non-null) or set IDENTIFIER_MARKED to val. */
1449 static void
1450 mark_or_check_attr_tags (tree attr, tree *tp, abi_tag_data *p, bool val)
1452 if (!attr)
1453 return;
1454 for (; (attr = lookup_attribute ("abi_tag", attr));
1455 attr = TREE_CHAIN (attr))
1456 for (tree list = TREE_VALUE (attr); list;
1457 list = TREE_CHAIN (list))
1459 tree tag = TREE_VALUE (list);
1460 tree id = get_identifier (TREE_STRING_POINTER (tag));
1461 if (tp)
1462 check_tag (tag, id, tp, p);
1463 else
1464 IDENTIFIER_MARKED (id) = val;
1468 /* Find all the ABI tags on T and its enclosing scopes and either call
1469 check_tag (if TP is non-null) or set IDENTIFIER_MARKED to val. */
1471 static void
1472 mark_or_check_tags (tree t, tree *tp, abi_tag_data *p, bool val)
1474 while (t != global_namespace)
1476 tree attr;
1477 if (TYPE_P (t))
1479 attr = TYPE_ATTRIBUTES (t);
1480 t = CP_TYPE_CONTEXT (t);
1482 else
1484 attr = DECL_ATTRIBUTES (t);
1485 t = CP_DECL_CONTEXT (t);
1487 mark_or_check_attr_tags (attr, tp, p, val);
1491 /* walk_tree callback for check_abi_tags: if the type at *TP involves any
1492 types with ABI tags, add the corresponding identifiers to the VEC in
1493 *DATA and set IDENTIFIER_MARKED. */
1495 static tree
1496 find_abi_tags_r (tree *tp, int *walk_subtrees, void *data)
1498 if (!OVERLOAD_TYPE_P (*tp))
1499 return NULL_TREE;
1501 /* walk_tree shouldn't be walking into any subtrees of a RECORD_TYPE
1502 anyway, but let's make sure of it. */
1503 *walk_subtrees = false;
1505 abi_tag_data *p = static_cast<struct abi_tag_data*>(data);
1507 mark_or_check_tags (*tp, tp, p, false);
1509 return NULL_TREE;
1512 /* walk_tree callback for mark_abi_tags: if *TP is a class, set
1513 IDENTIFIER_MARKED on its ABI tags. */
1515 static tree
1516 mark_abi_tags_r (tree *tp, int *walk_subtrees, void *data)
1518 if (!OVERLOAD_TYPE_P (*tp))
1519 return NULL_TREE;
1521 /* walk_tree shouldn't be walking into any subtrees of a RECORD_TYPE
1522 anyway, but let's make sure of it. */
1523 *walk_subtrees = false;
1525 bool *valp = static_cast<bool*>(data);
1527 mark_or_check_tags (*tp, NULL, NULL, *valp);
1529 return NULL_TREE;
1532 /* Set IDENTIFIER_MARKED on all the ABI tags on T and its enclosing
1533 scopes. */
1535 static void
1536 mark_abi_tags (tree t, bool val)
1538 mark_or_check_tags (t, NULL, NULL, val);
1539 if (DECL_P (t))
1541 if (DECL_LANG_SPECIFIC (t) && DECL_USE_TEMPLATE (t)
1542 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (t)))
1544 /* Template arguments are part of the signature. */
1545 tree level = INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (t));
1546 for (int j = 0; j < TREE_VEC_LENGTH (level); ++j)
1548 tree arg = TREE_VEC_ELT (level, j);
1549 cp_walk_tree_without_duplicates (&arg, mark_abi_tags_r, &val);
1552 if (TREE_CODE (t) == FUNCTION_DECL)
1553 /* A function's parameter types are part of the signature, so
1554 we don't need to inherit any tags that are also in them. */
1555 for (tree arg = FUNCTION_FIRST_USER_PARMTYPE (t); arg;
1556 arg = TREE_CHAIN (arg))
1557 cp_walk_tree_without_duplicates (&TREE_VALUE (arg),
1558 mark_abi_tags_r, &val);
1562 /* Check that T has all the ABI tags that subobject SUBOB has, or
1563 warn if not. If T is a (variable or function) declaration, also
1564 add any missing tags. */
1566 static void
1567 check_abi_tags (tree t, tree subob)
1569 bool inherit = DECL_P (t);
1571 if (!inherit && !warn_abi_tag)
1572 return;
1574 tree decl = TYPE_P (t) ? TYPE_NAME (t) : t;
1575 if (!TREE_PUBLIC (decl))
1576 /* No need to worry about things local to this TU. */
1577 return;
1579 mark_abi_tags (t, true);
1581 tree subtype = TYPE_P (subob) ? subob : TREE_TYPE (subob);
1582 struct abi_tag_data data = { t, subob, error_mark_node };
1583 if (inherit)
1584 data.tags = NULL_TREE;
1586 cp_walk_tree_without_duplicates (&subtype, find_abi_tags_r, &data);
1588 if (inherit && data.tags)
1590 tree attr = lookup_attribute ("abi_tag", DECL_ATTRIBUTES (t));
1591 if (attr)
1592 TREE_VALUE (attr) = chainon (data.tags, TREE_VALUE (attr));
1593 else
1594 DECL_ATTRIBUTES (t)
1595 = tree_cons (get_identifier ("abi_tag"), data.tags,
1596 DECL_ATTRIBUTES (t));
1599 mark_abi_tags (t, false);
1602 /* Check that DECL has all the ABI tags that are used in parts of its type
1603 that are not reflected in its mangled name. */
1605 void
1606 check_abi_tags (tree decl)
1608 tree t;
1609 if (abi_version_at_least (10)
1610 && DECL_LANG_SPECIFIC (decl)
1611 && DECL_USE_TEMPLATE (decl)
1612 && (t = DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (decl)),
1613 t != decl))
1614 /* Make sure that our template has the appropriate tags, since
1615 write_unqualified_name looks for them there. */
1616 check_abi_tags (t);
1617 if (VAR_P (decl))
1618 check_abi_tags (decl, TREE_TYPE (decl));
1619 else if (TREE_CODE (decl) == FUNCTION_DECL
1620 && !mangle_return_type_p (decl))
1621 check_abi_tags (decl, TREE_TYPE (TREE_TYPE (decl)));
1624 void
1625 inherit_targ_abi_tags (tree t)
1627 if (!CLASS_TYPE_P (t)
1628 || CLASSTYPE_TEMPLATE_INFO (t) == NULL_TREE)
1629 return;
1631 mark_abi_tags (t, true);
1633 tree args = CLASSTYPE_TI_ARGS (t);
1634 struct abi_tag_data data = { t, NULL_TREE, NULL_TREE };
1635 for (int i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
1637 tree level = TMPL_ARGS_LEVEL (args, i+1);
1638 for (int j = 0; j < TREE_VEC_LENGTH (level); ++j)
1640 tree arg = TREE_VEC_ELT (level, j);
1641 data.subob = arg;
1642 cp_walk_tree_without_duplicates (&arg, find_abi_tags_r, &data);
1646 // If we found some tags on our template arguments, add them to our
1647 // abi_tag attribute.
1648 if (data.tags)
1650 tree attr = lookup_attribute ("abi_tag", TYPE_ATTRIBUTES (t));
1651 if (attr)
1652 TREE_VALUE (attr) = chainon (data.tags, TREE_VALUE (attr));
1653 else
1654 TYPE_ATTRIBUTES (t)
1655 = tree_cons (get_identifier ("abi_tag"), data.tags,
1656 TYPE_ATTRIBUTES (t));
1659 mark_abi_tags (t, false);
1662 /* Return true, iff class T has a non-virtual destructor that is
1663 accessible from outside the class heirarchy (i.e. is public, or
1664 there's a suitable friend. */
1666 static bool
1667 accessible_nvdtor_p (tree t)
1669 tree dtor = CLASSTYPE_DESTRUCTORS (t);
1671 /* An implicitly declared destructor is always public. And,
1672 if it were virtual, we would have created it by now. */
1673 if (!dtor)
1674 return true;
1676 if (DECL_VINDEX (dtor))
1677 return false; /* Virtual */
1679 if (!TREE_PRIVATE (dtor) && !TREE_PROTECTED (dtor))
1680 return true; /* Public */
1682 if (CLASSTYPE_FRIEND_CLASSES (t)
1683 || DECL_FRIENDLIST (TYPE_MAIN_DECL (t)))
1684 return true; /* Has friends */
1686 return false;
1689 /* Run through the base classes of T, updating CANT_HAVE_CONST_CTOR_P,
1690 and NO_CONST_ASN_REF_P. Also set flag bits in T based on
1691 properties of the bases. */
1693 static void
1694 check_bases (tree t,
1695 int* cant_have_const_ctor_p,
1696 int* no_const_asn_ref_p)
1698 int i;
1699 bool seen_non_virtual_nearly_empty_base_p = 0;
1700 int seen_tm_mask = 0;
1701 tree base_binfo;
1702 tree binfo;
1703 tree field = NULL_TREE;
1705 if (!CLASSTYPE_NON_STD_LAYOUT (t))
1706 for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
1707 if (TREE_CODE (field) == FIELD_DECL)
1708 break;
1710 for (binfo = TYPE_BINFO (t), i = 0;
1711 BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
1713 tree basetype = TREE_TYPE (base_binfo);
1715 gcc_assert (COMPLETE_TYPE_P (basetype));
1717 if (CLASSTYPE_FINAL (basetype))
1718 error ("cannot derive from %<final%> base %qT in derived type %qT",
1719 basetype, t);
1721 /* If any base class is non-literal, so is the derived class. */
1722 if (!CLASSTYPE_LITERAL_P (basetype))
1723 CLASSTYPE_LITERAL_P (t) = false;
1725 /* If the base class doesn't have copy constructors or
1726 assignment operators that take const references, then the
1727 derived class cannot have such a member automatically
1728 generated. */
1729 if (TYPE_HAS_COPY_CTOR (basetype)
1730 && ! TYPE_HAS_CONST_COPY_CTOR (basetype))
1731 *cant_have_const_ctor_p = 1;
1732 if (TYPE_HAS_COPY_ASSIGN (basetype)
1733 && !TYPE_HAS_CONST_COPY_ASSIGN (basetype))
1734 *no_const_asn_ref_p = 1;
1736 if (BINFO_VIRTUAL_P (base_binfo))
1737 /* A virtual base does not effect nearly emptiness. */
1739 else if (CLASSTYPE_NEARLY_EMPTY_P (basetype))
1741 if (seen_non_virtual_nearly_empty_base_p)
1742 /* And if there is more than one nearly empty base, then the
1743 derived class is not nearly empty either. */
1744 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
1745 else
1746 /* Remember we've seen one. */
1747 seen_non_virtual_nearly_empty_base_p = 1;
1749 else if (!is_empty_class (basetype))
1750 /* If the base class is not empty or nearly empty, then this
1751 class cannot be nearly empty. */
1752 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
1754 /* A lot of properties from the bases also apply to the derived
1755 class. */
1756 TYPE_NEEDS_CONSTRUCTING (t) |= TYPE_NEEDS_CONSTRUCTING (basetype);
1757 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t)
1758 |= TYPE_HAS_NONTRIVIAL_DESTRUCTOR (basetype);
1759 TYPE_HAS_COMPLEX_COPY_ASSIGN (t)
1760 |= (TYPE_HAS_COMPLEX_COPY_ASSIGN (basetype)
1761 || !TYPE_HAS_COPY_ASSIGN (basetype));
1762 TYPE_HAS_COMPLEX_COPY_CTOR (t) |= (TYPE_HAS_COMPLEX_COPY_CTOR (basetype)
1763 || !TYPE_HAS_COPY_CTOR (basetype));
1764 TYPE_HAS_COMPLEX_MOVE_ASSIGN (t)
1765 |= TYPE_HAS_COMPLEX_MOVE_ASSIGN (basetype);
1766 TYPE_HAS_COMPLEX_MOVE_CTOR (t) |= TYPE_HAS_COMPLEX_MOVE_CTOR (basetype);
1767 TYPE_POLYMORPHIC_P (t) |= TYPE_POLYMORPHIC_P (basetype);
1768 CLASSTYPE_CONTAINS_EMPTY_CLASS_P (t)
1769 |= CLASSTYPE_CONTAINS_EMPTY_CLASS_P (basetype);
1770 TYPE_HAS_COMPLEX_DFLT (t) |= (!TYPE_HAS_DEFAULT_CONSTRUCTOR (basetype)
1771 || TYPE_HAS_COMPLEX_DFLT (basetype));
1772 SET_CLASSTYPE_READONLY_FIELDS_NEED_INIT
1773 (t, CLASSTYPE_READONLY_FIELDS_NEED_INIT (t)
1774 | CLASSTYPE_READONLY_FIELDS_NEED_INIT (basetype));
1775 SET_CLASSTYPE_REF_FIELDS_NEED_INIT
1776 (t, CLASSTYPE_REF_FIELDS_NEED_INIT (t)
1777 | CLASSTYPE_REF_FIELDS_NEED_INIT (basetype));
1779 /* A standard-layout class is a class that:
1781 * has no non-standard-layout base classes, */
1782 CLASSTYPE_NON_STD_LAYOUT (t) |= CLASSTYPE_NON_STD_LAYOUT (basetype);
1783 if (!CLASSTYPE_NON_STD_LAYOUT (t))
1785 tree basefield;
1786 /* ...has no base classes of the same type as the first non-static
1787 data member... */
1788 if (field && DECL_CONTEXT (field) == t
1789 && (same_type_ignoring_top_level_qualifiers_p
1790 (TREE_TYPE (field), basetype)))
1791 CLASSTYPE_NON_STD_LAYOUT (t) = 1;
1792 else
1793 /* ...either has no non-static data members in the most-derived
1794 class and at most one base class with non-static data
1795 members, or has no base classes with non-static data
1796 members */
1797 for (basefield = TYPE_FIELDS (basetype); basefield;
1798 basefield = DECL_CHAIN (basefield))
1799 if (TREE_CODE (basefield) == FIELD_DECL)
1801 if (field)
1802 CLASSTYPE_NON_STD_LAYOUT (t) = 1;
1803 else
1804 field = basefield;
1805 break;
1809 /* Don't bother collecting tm attributes if transactional memory
1810 support is not enabled. */
1811 if (flag_tm)
1813 tree tm_attr = find_tm_attribute (TYPE_ATTRIBUTES (basetype));
1814 if (tm_attr)
1815 seen_tm_mask |= tm_attr_to_mask (tm_attr);
1818 check_abi_tags (t, basetype);
1821 /* If one of the base classes had TM attributes, and the current class
1822 doesn't define its own, then the current class inherits one. */
1823 if (seen_tm_mask && !find_tm_attribute (TYPE_ATTRIBUTES (t)))
1825 tree tm_attr = tm_mask_to_attr (seen_tm_mask & -seen_tm_mask);
1826 TYPE_ATTRIBUTES (t) = tree_cons (tm_attr, NULL, TYPE_ATTRIBUTES (t));
1830 /* Determine all the primary bases within T. Sets BINFO_PRIMARY_BASE_P for
1831 those that are primaries. Sets BINFO_LOST_PRIMARY_P for those
1832 that have had a nearly-empty virtual primary base stolen by some
1833 other base in the hierarchy. Determines CLASSTYPE_PRIMARY_BASE for
1834 T. */
1836 static void
1837 determine_primary_bases (tree t)
1839 unsigned i;
1840 tree primary = NULL_TREE;
1841 tree type_binfo = TYPE_BINFO (t);
1842 tree base_binfo;
1844 /* Determine the primary bases of our bases. */
1845 for (base_binfo = TREE_CHAIN (type_binfo); base_binfo;
1846 base_binfo = TREE_CHAIN (base_binfo))
1848 tree primary = CLASSTYPE_PRIMARY_BINFO (BINFO_TYPE (base_binfo));
1850 /* See if we're the non-virtual primary of our inheritance
1851 chain. */
1852 if (!BINFO_VIRTUAL_P (base_binfo))
1854 tree parent = BINFO_INHERITANCE_CHAIN (base_binfo);
1855 tree parent_primary = CLASSTYPE_PRIMARY_BINFO (BINFO_TYPE (parent));
1857 if (parent_primary
1858 && SAME_BINFO_TYPE_P (BINFO_TYPE (base_binfo),
1859 BINFO_TYPE (parent_primary)))
1860 /* We are the primary binfo. */
1861 BINFO_PRIMARY_P (base_binfo) = 1;
1863 /* Determine if we have a virtual primary base, and mark it so.
1865 if (primary && BINFO_VIRTUAL_P (primary))
1867 tree this_primary = copied_binfo (primary, base_binfo);
1869 if (BINFO_PRIMARY_P (this_primary))
1870 /* Someone already claimed this base. */
1871 BINFO_LOST_PRIMARY_P (base_binfo) = 1;
1872 else
1874 tree delta;
1876 BINFO_PRIMARY_P (this_primary) = 1;
1877 BINFO_INHERITANCE_CHAIN (this_primary) = base_binfo;
1879 /* A virtual binfo might have been copied from within
1880 another hierarchy. As we're about to use it as a
1881 primary base, make sure the offsets match. */
1882 delta = size_diffop_loc (input_location,
1883 fold_convert (ssizetype,
1884 BINFO_OFFSET (base_binfo)),
1885 fold_convert (ssizetype,
1886 BINFO_OFFSET (this_primary)));
1888 propagate_binfo_offsets (this_primary, delta);
1893 /* First look for a dynamic direct non-virtual base. */
1894 for (i = 0; BINFO_BASE_ITERATE (type_binfo, i, base_binfo); i++)
1896 tree basetype = BINFO_TYPE (base_binfo);
1898 if (TYPE_CONTAINS_VPTR_P (basetype) && !BINFO_VIRTUAL_P (base_binfo))
1900 primary = base_binfo;
1901 goto found;
1905 /* A "nearly-empty" virtual base class can be the primary base
1906 class, if no non-virtual polymorphic base can be found. Look for
1907 a nearly-empty virtual dynamic base that is not already a primary
1908 base of something in the hierarchy. If there is no such base,
1909 just pick the first nearly-empty virtual base. */
1911 for (base_binfo = TREE_CHAIN (type_binfo); base_binfo;
1912 base_binfo = TREE_CHAIN (base_binfo))
1913 if (BINFO_VIRTUAL_P (base_binfo)
1914 && CLASSTYPE_NEARLY_EMPTY_P (BINFO_TYPE (base_binfo)))
1916 if (!BINFO_PRIMARY_P (base_binfo))
1918 /* Found one that is not primary. */
1919 primary = base_binfo;
1920 goto found;
1922 else if (!primary)
1923 /* Remember the first candidate. */
1924 primary = base_binfo;
1927 found:
1928 /* If we've got a primary base, use it. */
1929 if (primary)
1931 tree basetype = BINFO_TYPE (primary);
1933 CLASSTYPE_PRIMARY_BINFO (t) = primary;
1934 if (BINFO_PRIMARY_P (primary))
1935 /* We are stealing a primary base. */
1936 BINFO_LOST_PRIMARY_P (BINFO_INHERITANCE_CHAIN (primary)) = 1;
1937 BINFO_PRIMARY_P (primary) = 1;
1938 if (BINFO_VIRTUAL_P (primary))
1940 tree delta;
1942 BINFO_INHERITANCE_CHAIN (primary) = type_binfo;
1943 /* A virtual binfo might have been copied from within
1944 another hierarchy. As we're about to use it as a primary
1945 base, make sure the offsets match. */
1946 delta = size_diffop_loc (input_location, ssize_int (0),
1947 fold_convert (ssizetype, BINFO_OFFSET (primary)));
1949 propagate_binfo_offsets (primary, delta);
1952 primary = TYPE_BINFO (basetype);
1954 TYPE_VFIELD (t) = TYPE_VFIELD (basetype);
1955 BINFO_VTABLE (type_binfo) = BINFO_VTABLE (primary);
1956 BINFO_VIRTUALS (type_binfo) = BINFO_VIRTUALS (primary);
1960 /* Update the variant types of T. */
1962 void
1963 fixup_type_variants (tree t)
1965 tree variants;
1967 if (!t)
1968 return;
1970 for (variants = TYPE_NEXT_VARIANT (t);
1971 variants;
1972 variants = TYPE_NEXT_VARIANT (variants))
1974 /* These fields are in the _TYPE part of the node, not in
1975 the TYPE_LANG_SPECIFIC component, so they are not shared. */
1976 TYPE_HAS_USER_CONSTRUCTOR (variants) = TYPE_HAS_USER_CONSTRUCTOR (t);
1977 TYPE_NEEDS_CONSTRUCTING (variants) = TYPE_NEEDS_CONSTRUCTING (t);
1978 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (variants)
1979 = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t);
1981 TYPE_POLYMORPHIC_P (variants) = TYPE_POLYMORPHIC_P (t);
1983 TYPE_BINFO (variants) = TYPE_BINFO (t);
1985 /* Copy whatever these are holding today. */
1986 TYPE_VFIELD (variants) = TYPE_VFIELD (t);
1987 TYPE_FIELDS (variants) = TYPE_FIELDS (t);
1991 /* KLASS is a class that we're applying may_alias to after the body is
1992 parsed. Fixup any POINTER_TO and REFERENCE_TO types. The
1993 canonical type(s) will be implicitly updated. */
1995 static void
1996 fixup_may_alias (tree klass)
1998 tree t;
2000 for (t = TYPE_POINTER_TO (klass); t; t = TYPE_NEXT_PTR_TO (t))
2001 TYPE_REF_CAN_ALIAS_ALL (t) = true;
2002 for (t = TYPE_REFERENCE_TO (klass); t; t = TYPE_NEXT_REF_TO (t))
2003 TYPE_REF_CAN_ALIAS_ALL (t) = true;
2006 /* Early variant fixups: we apply attributes at the beginning of the class
2007 definition, and we need to fix up any variants that have already been
2008 made via elaborated-type-specifier so that check_qualified_type works. */
2010 void
2011 fixup_attribute_variants (tree t)
2013 tree variants;
2015 if (!t)
2016 return;
2018 tree attrs = TYPE_ATTRIBUTES (t);
2019 unsigned align = TYPE_ALIGN (t);
2020 bool user_align = TYPE_USER_ALIGN (t);
2021 bool may_alias = lookup_attribute ("may_alias", attrs);
2023 if (may_alias)
2024 fixup_may_alias (t);
2026 for (variants = TYPE_NEXT_VARIANT (t);
2027 variants;
2028 variants = TYPE_NEXT_VARIANT (variants))
2030 /* These are the two fields that check_qualified_type looks at and
2031 are affected by attributes. */
2032 TYPE_ATTRIBUTES (variants) = attrs;
2033 unsigned valign = align;
2034 if (TYPE_USER_ALIGN (variants))
2035 valign = MAX (valign, TYPE_ALIGN (variants));
2036 else
2037 TYPE_USER_ALIGN (variants) = user_align;
2038 SET_TYPE_ALIGN (variants, valign);
2039 if (may_alias)
2040 fixup_may_alias (variants);
2044 /* Set memoizing fields and bits of T (and its variants) for later
2045 use. */
2047 static void
2048 finish_struct_bits (tree t)
2050 /* Fix up variants (if any). */
2051 fixup_type_variants (t);
2053 if (BINFO_N_BASE_BINFOS (TYPE_BINFO (t)) && TYPE_POLYMORPHIC_P (t))
2054 /* For a class w/o baseclasses, 'finish_struct' has set
2055 CLASSTYPE_PURE_VIRTUALS correctly (by definition).
2056 Similarly for a class whose base classes do not have vtables.
2057 When neither of these is true, we might have removed abstract
2058 virtuals (by providing a definition), added some (by declaring
2059 new ones), or redeclared ones from a base class. We need to
2060 recalculate what's really an abstract virtual at this point (by
2061 looking in the vtables). */
2062 get_pure_virtuals (t);
2064 /* If this type has a copy constructor or a destructor, force its
2065 mode to be BLKmode, and force its TREE_ADDRESSABLE bit to be
2066 nonzero. This will cause it to be passed by invisible reference
2067 and prevent it from being returned in a register. */
2068 if (type_has_nontrivial_copy_init (t)
2069 || TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t))
2071 tree variants;
2072 DECL_MODE (TYPE_MAIN_DECL (t)) = BLKmode;
2073 for (variants = t; variants; variants = TYPE_NEXT_VARIANT (variants))
2075 SET_TYPE_MODE (variants, BLKmode);
2076 TREE_ADDRESSABLE (variants) = 1;
2081 /* Issue warnings about T having private constructors, but no friends,
2082 and so forth.
2084 HAS_NONPRIVATE_METHOD is nonzero if T has any non-private methods or
2085 static members. HAS_NONPRIVATE_STATIC_FN is nonzero if T has any
2086 non-private static member functions. */
2088 static void
2089 maybe_warn_about_overly_private_class (tree t)
2091 int has_member_fn = 0;
2092 int has_nonprivate_method = 0;
2093 tree fn;
2095 if (!warn_ctor_dtor_privacy
2096 /* If the class has friends, those entities might create and
2097 access instances, so we should not warn. */
2098 || (CLASSTYPE_FRIEND_CLASSES (t)
2099 || DECL_FRIENDLIST (TYPE_MAIN_DECL (t)))
2100 /* We will have warned when the template was declared; there's
2101 no need to warn on every instantiation. */
2102 || CLASSTYPE_TEMPLATE_INSTANTIATION (t))
2103 /* There's no reason to even consider warning about this
2104 class. */
2105 return;
2107 /* We only issue one warning, if more than one applies, because
2108 otherwise, on code like:
2110 class A {
2111 // Oops - forgot `public:'
2112 A();
2113 A(const A&);
2114 ~A();
2117 we warn several times about essentially the same problem. */
2119 /* Check to see if all (non-constructor, non-destructor) member
2120 functions are private. (Since there are no friends or
2121 non-private statics, we can't ever call any of the private member
2122 functions.) */
2123 for (fn = TYPE_METHODS (t); fn; fn = DECL_CHAIN (fn))
2124 /* We're not interested in compiler-generated methods; they don't
2125 provide any way to call private members. */
2126 if (!DECL_ARTIFICIAL (fn))
2128 if (!TREE_PRIVATE (fn))
2130 if (DECL_STATIC_FUNCTION_P (fn))
2131 /* A non-private static member function is just like a
2132 friend; it can create and invoke private member
2133 functions, and be accessed without a class
2134 instance. */
2135 return;
2137 has_nonprivate_method = 1;
2138 /* Keep searching for a static member function. */
2140 else if (!DECL_CONSTRUCTOR_P (fn) && !DECL_DESTRUCTOR_P (fn))
2141 has_member_fn = 1;
2144 if (!has_nonprivate_method && has_member_fn)
2146 /* There are no non-private methods, and there's at least one
2147 private member function that isn't a constructor or
2148 destructor. (If all the private members are
2149 constructors/destructors we want to use the code below that
2150 issues error messages specifically referring to
2151 constructors/destructors.) */
2152 unsigned i;
2153 tree binfo = TYPE_BINFO (t);
2155 for (i = 0; i != BINFO_N_BASE_BINFOS (binfo); i++)
2156 if (BINFO_BASE_ACCESS (binfo, i) != access_private_node)
2158 has_nonprivate_method = 1;
2159 break;
2161 if (!has_nonprivate_method)
2163 warning (OPT_Wctor_dtor_privacy,
2164 "all member functions in class %qT are private", t);
2165 return;
2169 /* Even if some of the member functions are non-private, the class
2170 won't be useful for much if all the constructors or destructors
2171 are private: such an object can never be created or destroyed. */
2172 fn = CLASSTYPE_DESTRUCTORS (t);
2173 if (fn && TREE_PRIVATE (fn))
2175 warning (OPT_Wctor_dtor_privacy,
2176 "%q#T only defines a private destructor and has no friends",
2178 return;
2181 /* Warn about classes that have private constructors and no friends. */
2182 if (TYPE_HAS_USER_CONSTRUCTOR (t)
2183 /* Implicitly generated constructors are always public. */
2184 && (!CLASSTYPE_LAZY_DEFAULT_CTOR (t)
2185 || !CLASSTYPE_LAZY_COPY_CTOR (t)))
2187 int nonprivate_ctor = 0;
2189 /* If a non-template class does not define a copy
2190 constructor, one is defined for it, enabling it to avoid
2191 this warning. For a template class, this does not
2192 happen, and so we would normally get a warning on:
2194 template <class T> class C { private: C(); };
2196 To avoid this asymmetry, we check TYPE_HAS_COPY_CTOR. All
2197 complete non-template or fully instantiated classes have this
2198 flag set. */
2199 if (!TYPE_HAS_COPY_CTOR (t))
2200 nonprivate_ctor = 1;
2201 else
2202 for (fn = CLASSTYPE_CONSTRUCTORS (t); fn; fn = OVL_NEXT (fn))
2204 tree ctor = OVL_CURRENT (fn);
2205 /* Ideally, we wouldn't count copy constructors (or, in
2206 fact, any constructor that takes an argument of the
2207 class type as a parameter) because such things cannot
2208 be used to construct an instance of the class unless
2209 you already have one. But, for now at least, we're
2210 more generous. */
2211 if (! TREE_PRIVATE (ctor))
2213 nonprivate_ctor = 1;
2214 break;
2218 if (nonprivate_ctor == 0)
2220 warning (OPT_Wctor_dtor_privacy,
2221 "%q#T only defines private constructors and has no friends",
2223 return;
2228 static struct {
2229 gt_pointer_operator new_value;
2230 void *cookie;
2231 } resort_data;
2233 /* Comparison function to compare two TYPE_METHOD_VEC entries by name. */
2235 static int
2236 method_name_cmp (const void* m1_p, const void* m2_p)
2238 const tree *const m1 = (const tree *) m1_p;
2239 const tree *const m2 = (const tree *) m2_p;
2241 if (*m1 == NULL_TREE && *m2 == NULL_TREE)
2242 return 0;
2243 if (*m1 == NULL_TREE)
2244 return -1;
2245 if (*m2 == NULL_TREE)
2246 return 1;
2247 if (DECL_NAME (OVL_CURRENT (*m1)) < DECL_NAME (OVL_CURRENT (*m2)))
2248 return -1;
2249 return 1;
2252 /* This routine compares two fields like method_name_cmp but using the
2253 pointer operator in resort_field_decl_data. */
2255 static int
2256 resort_method_name_cmp (const void* m1_p, const void* m2_p)
2258 const tree *const m1 = (const tree *) m1_p;
2259 const tree *const m2 = (const tree *) m2_p;
2260 if (*m1 == NULL_TREE && *m2 == NULL_TREE)
2261 return 0;
2262 if (*m1 == NULL_TREE)
2263 return -1;
2264 if (*m2 == NULL_TREE)
2265 return 1;
2267 tree d1 = DECL_NAME (OVL_CURRENT (*m1));
2268 tree d2 = DECL_NAME (OVL_CURRENT (*m2));
2269 resort_data.new_value (&d1, resort_data.cookie);
2270 resort_data.new_value (&d2, resort_data.cookie);
2271 if (d1 < d2)
2272 return -1;
2274 return 1;
2277 /* Resort TYPE_METHOD_VEC because pointers have been reordered. */
2279 void
2280 resort_type_method_vec (void* obj,
2281 void* /*orig_obj*/,
2282 gt_pointer_operator new_value,
2283 void* cookie)
2285 vec<tree, va_gc> *method_vec = (vec<tree, va_gc> *) obj;
2286 int len = vec_safe_length (method_vec);
2287 size_t slot;
2288 tree fn;
2290 /* The type conversion ops have to live at the front of the vec, so we
2291 can't sort them. */
2292 for (slot = CLASSTYPE_FIRST_CONVERSION_SLOT;
2293 vec_safe_iterate (method_vec, slot, &fn);
2294 ++slot)
2295 if (!DECL_CONV_FN_P (OVL_CURRENT (fn)))
2296 break;
2298 if (len - slot > 1)
2300 resort_data.new_value = new_value;
2301 resort_data.cookie = cookie;
2302 qsort (method_vec->address () + slot, len - slot, sizeof (tree),
2303 resort_method_name_cmp);
2307 /* Warn about duplicate methods in fn_fields.
2309 Sort methods that are not special (i.e., constructors, destructors,
2310 and type conversion operators) so that we can find them faster in
2311 search. */
2313 static void
2314 finish_struct_methods (tree t)
2316 tree fn_fields;
2317 vec<tree, va_gc> *method_vec;
2318 int slot, len;
2320 method_vec = CLASSTYPE_METHOD_VEC (t);
2321 if (!method_vec)
2322 return;
2324 len = method_vec->length ();
2326 /* Clear DECL_IN_AGGR_P for all functions. */
2327 for (fn_fields = TYPE_METHODS (t); fn_fields;
2328 fn_fields = DECL_CHAIN (fn_fields))
2329 DECL_IN_AGGR_P (fn_fields) = 0;
2331 /* Issue warnings about private constructors and such. If there are
2332 no methods, then some public defaults are generated. */
2333 maybe_warn_about_overly_private_class (t);
2335 /* The type conversion ops have to live at the front of the vec, so we
2336 can't sort them. */
2337 for (slot = CLASSTYPE_FIRST_CONVERSION_SLOT;
2338 method_vec->iterate (slot, &fn_fields);
2339 ++slot)
2340 if (!DECL_CONV_FN_P (OVL_CURRENT (fn_fields)))
2341 break;
2342 if (len - slot > 1)
2343 qsort (method_vec->address () + slot,
2344 len-slot, sizeof (tree), method_name_cmp);
2347 /* Make BINFO's vtable have N entries, including RTTI entries,
2348 vbase and vcall offsets, etc. Set its type and call the back end
2349 to lay it out. */
2351 static void
2352 layout_vtable_decl (tree binfo, int n)
2354 tree atype;
2355 tree vtable;
2357 atype = build_array_of_n_type (vtable_entry_type, n);
2358 layout_type (atype);
2360 /* We may have to grow the vtable. */
2361 vtable = get_vtbl_decl_for_binfo (binfo);
2362 if (!same_type_p (TREE_TYPE (vtable), atype))
2364 TREE_TYPE (vtable) = atype;
2365 DECL_SIZE (vtable) = DECL_SIZE_UNIT (vtable) = NULL_TREE;
2366 layout_decl (vtable, 0);
2370 /* True iff FNDECL and BASE_FNDECL (both non-static member functions)
2371 have the same signature. */
2374 same_signature_p (const_tree fndecl, const_tree base_fndecl)
2376 /* One destructor overrides another if they are the same kind of
2377 destructor. */
2378 if (DECL_DESTRUCTOR_P (base_fndecl) && DECL_DESTRUCTOR_P (fndecl)
2379 && special_function_p (base_fndecl) == special_function_p (fndecl))
2380 return 1;
2381 /* But a non-destructor never overrides a destructor, nor vice
2382 versa, nor do different kinds of destructors override
2383 one-another. For example, a complete object destructor does not
2384 override a deleting destructor. */
2385 if (DECL_DESTRUCTOR_P (base_fndecl) || DECL_DESTRUCTOR_P (fndecl))
2386 return 0;
2388 if (DECL_NAME (fndecl) == DECL_NAME (base_fndecl)
2389 || (DECL_CONV_FN_P (fndecl)
2390 && DECL_CONV_FN_P (base_fndecl)
2391 && same_type_p (DECL_CONV_FN_TYPE (fndecl),
2392 DECL_CONV_FN_TYPE (base_fndecl))))
2394 tree fntype = TREE_TYPE (fndecl);
2395 tree base_fntype = TREE_TYPE (base_fndecl);
2396 if (type_memfn_quals (fntype) == type_memfn_quals (base_fntype)
2397 && type_memfn_rqual (fntype) == type_memfn_rqual (base_fntype)
2398 && compparms (FUNCTION_FIRST_USER_PARMTYPE (fndecl),
2399 FUNCTION_FIRST_USER_PARMTYPE (base_fndecl)))
2400 return 1;
2402 return 0;
2405 /* Returns TRUE if DERIVED is a binfo containing the binfo BASE as a
2406 subobject. */
2408 static bool
2409 base_derived_from (tree derived, tree base)
2411 tree probe;
2413 for (probe = base; probe; probe = BINFO_INHERITANCE_CHAIN (probe))
2415 if (probe == derived)
2416 return true;
2417 else if (BINFO_VIRTUAL_P (probe))
2418 /* If we meet a virtual base, we can't follow the inheritance
2419 any more. See if the complete type of DERIVED contains
2420 such a virtual base. */
2421 return (binfo_for_vbase (BINFO_TYPE (probe), BINFO_TYPE (derived))
2422 != NULL_TREE);
2424 return false;
2427 struct find_final_overrider_data {
2428 /* The function for which we are trying to find a final overrider. */
2429 tree fn;
2430 /* The base class in which the function was declared. */
2431 tree declaring_base;
2432 /* The candidate overriders. */
2433 tree candidates;
2434 /* Path to most derived. */
2435 vec<tree> path;
2438 /* Add the overrider along the current path to FFOD->CANDIDATES.
2439 Returns true if an overrider was found; false otherwise. */
2441 static bool
2442 dfs_find_final_overrider_1 (tree binfo,
2443 find_final_overrider_data *ffod,
2444 unsigned depth)
2446 tree method;
2448 /* If BINFO is not the most derived type, try a more derived class.
2449 A definition there will overrider a definition here. */
2450 if (depth)
2452 depth--;
2453 if (dfs_find_final_overrider_1
2454 (ffod->path[depth], ffod, depth))
2455 return true;
2458 method = look_for_overrides_here (BINFO_TYPE (binfo), ffod->fn);
2459 if (method)
2461 tree *candidate = &ffod->candidates;
2463 /* Remove any candidates overridden by this new function. */
2464 while (*candidate)
2466 /* If *CANDIDATE overrides METHOD, then METHOD
2467 cannot override anything else on the list. */
2468 if (base_derived_from (TREE_VALUE (*candidate), binfo))
2469 return true;
2470 /* If METHOD overrides *CANDIDATE, remove *CANDIDATE. */
2471 if (base_derived_from (binfo, TREE_VALUE (*candidate)))
2472 *candidate = TREE_CHAIN (*candidate);
2473 else
2474 candidate = &TREE_CHAIN (*candidate);
2477 /* Add the new function. */
2478 ffod->candidates = tree_cons (method, binfo, ffod->candidates);
2479 return true;
2482 return false;
2485 /* Called from find_final_overrider via dfs_walk. */
2487 static tree
2488 dfs_find_final_overrider_pre (tree binfo, void *data)
2490 find_final_overrider_data *ffod = (find_final_overrider_data *) data;
2492 if (binfo == ffod->declaring_base)
2493 dfs_find_final_overrider_1 (binfo, ffod, ffod->path.length ());
2494 ffod->path.safe_push (binfo);
2496 return NULL_TREE;
2499 static tree
2500 dfs_find_final_overrider_post (tree /*binfo*/, void *data)
2502 find_final_overrider_data *ffod = (find_final_overrider_data *) data;
2503 ffod->path.pop ();
2505 return NULL_TREE;
2508 /* Returns a TREE_LIST whose TREE_PURPOSE is the final overrider for
2509 FN and whose TREE_VALUE is the binfo for the base where the
2510 overriding occurs. BINFO (in the hierarchy dominated by the binfo
2511 DERIVED) is the base object in which FN is declared. */
2513 static tree
2514 find_final_overrider (tree derived, tree binfo, tree fn)
2516 find_final_overrider_data ffod;
2518 /* Getting this right is a little tricky. This is valid:
2520 struct S { virtual void f (); };
2521 struct T { virtual void f (); };
2522 struct U : public S, public T { };
2524 even though calling `f' in `U' is ambiguous. But,
2526 struct R { virtual void f(); };
2527 struct S : virtual public R { virtual void f (); };
2528 struct T : virtual public R { virtual void f (); };
2529 struct U : public S, public T { };
2531 is not -- there's no way to decide whether to put `S::f' or
2532 `T::f' in the vtable for `R'.
2534 The solution is to look at all paths to BINFO. If we find
2535 different overriders along any two, then there is a problem. */
2536 if (DECL_THUNK_P (fn))
2537 fn = THUNK_TARGET (fn);
2539 /* Determine the depth of the hierarchy. */
2540 ffod.fn = fn;
2541 ffod.declaring_base = binfo;
2542 ffod.candidates = NULL_TREE;
2543 ffod.path.create (30);
2545 dfs_walk_all (derived, dfs_find_final_overrider_pre,
2546 dfs_find_final_overrider_post, &ffod);
2548 ffod.path.release ();
2550 /* If there was no winner, issue an error message. */
2551 if (!ffod.candidates || TREE_CHAIN (ffod.candidates))
2552 return error_mark_node;
2554 return ffod.candidates;
2557 /* Return the index of the vcall offset for FN when TYPE is used as a
2558 virtual base. */
2560 static tree
2561 get_vcall_index (tree fn, tree type)
2563 vec<tree_pair_s, va_gc> *indices = CLASSTYPE_VCALL_INDICES (type);
2564 tree_pair_p p;
2565 unsigned ix;
2567 FOR_EACH_VEC_SAFE_ELT (indices, ix, p)
2568 if ((DECL_DESTRUCTOR_P (fn) && DECL_DESTRUCTOR_P (p->purpose))
2569 || same_signature_p (fn, p->purpose))
2570 return p->value;
2572 /* There should always be an appropriate index. */
2573 gcc_unreachable ();
2576 /* Update an entry in the vtable for BINFO, which is in the hierarchy
2577 dominated by T. FN is the old function; VIRTUALS points to the
2578 corresponding position in the new BINFO_VIRTUALS list. IX is the index
2579 of that entry in the list. */
2581 static void
2582 update_vtable_entry_for_fn (tree t, tree binfo, tree fn, tree* virtuals,
2583 unsigned ix)
2585 tree b;
2586 tree overrider;
2587 tree delta;
2588 tree virtual_base;
2589 tree first_defn;
2590 tree overrider_fn, overrider_target;
2591 tree target_fn = DECL_THUNK_P (fn) ? THUNK_TARGET (fn) : fn;
2592 tree over_return, base_return;
2593 bool lost = false;
2595 /* Find the nearest primary base (possibly binfo itself) which defines
2596 this function; this is the class the caller will convert to when
2597 calling FN through BINFO. */
2598 for (b = binfo; ; b = get_primary_binfo (b))
2600 gcc_assert (b);
2601 if (look_for_overrides_here (BINFO_TYPE (b), target_fn))
2602 break;
2604 /* The nearest definition is from a lost primary. */
2605 if (BINFO_LOST_PRIMARY_P (b))
2606 lost = true;
2608 first_defn = b;
2610 /* Find the final overrider. */
2611 overrider = find_final_overrider (TYPE_BINFO (t), b, target_fn);
2612 if (overrider == error_mark_node)
2614 error ("no unique final overrider for %qD in %qT", target_fn, t);
2615 return;
2617 overrider_target = overrider_fn = TREE_PURPOSE (overrider);
2619 /* Check for adjusting covariant return types. */
2620 over_return = TREE_TYPE (TREE_TYPE (overrider_target));
2621 base_return = TREE_TYPE (TREE_TYPE (target_fn));
2623 if (POINTER_TYPE_P (over_return)
2624 && TREE_CODE (over_return) == TREE_CODE (base_return)
2625 && CLASS_TYPE_P (TREE_TYPE (over_return))
2626 && CLASS_TYPE_P (TREE_TYPE (base_return))
2627 /* If the overrider is invalid, don't even try. */
2628 && !DECL_INVALID_OVERRIDER_P (overrider_target))
2630 /* If FN is a covariant thunk, we must figure out the adjustment
2631 to the final base FN was converting to. As OVERRIDER_TARGET might
2632 also be converting to the return type of FN, we have to
2633 combine the two conversions here. */
2634 tree fixed_offset, virtual_offset;
2636 over_return = TREE_TYPE (over_return);
2637 base_return = TREE_TYPE (base_return);
2639 if (DECL_THUNK_P (fn))
2641 gcc_assert (DECL_RESULT_THUNK_P (fn));
2642 fixed_offset = ssize_int (THUNK_FIXED_OFFSET (fn));
2643 virtual_offset = THUNK_VIRTUAL_OFFSET (fn);
2645 else
2646 fixed_offset = virtual_offset = NULL_TREE;
2648 if (virtual_offset)
2649 /* Find the equivalent binfo within the return type of the
2650 overriding function. We will want the vbase offset from
2651 there. */
2652 virtual_offset = binfo_for_vbase (BINFO_TYPE (virtual_offset),
2653 over_return);
2654 else if (!same_type_ignoring_top_level_qualifiers_p
2655 (over_return, base_return))
2657 /* There was no existing virtual thunk (which takes
2658 precedence). So find the binfo of the base function's
2659 return type within the overriding function's return type.
2660 Fortunately we know the covariancy is valid (it
2661 has already been checked), so we can just iterate along
2662 the binfos, which have been chained in inheritance graph
2663 order. Of course it is lame that we have to repeat the
2664 search here anyway -- we should really be caching pieces
2665 of the vtable and avoiding this repeated work. */
2666 tree thunk_binfo, base_binfo;
2668 /* Find the base binfo within the overriding function's
2669 return type. We will always find a thunk_binfo, except
2670 when the covariancy is invalid (which we will have
2671 already diagnosed). */
2672 for (base_binfo = TYPE_BINFO (base_return),
2673 thunk_binfo = TYPE_BINFO (over_return);
2674 thunk_binfo;
2675 thunk_binfo = TREE_CHAIN (thunk_binfo))
2676 if (SAME_BINFO_TYPE_P (BINFO_TYPE (thunk_binfo),
2677 BINFO_TYPE (base_binfo)))
2678 break;
2680 /* See if virtual inheritance is involved. */
2681 for (virtual_offset = thunk_binfo;
2682 virtual_offset;
2683 virtual_offset = BINFO_INHERITANCE_CHAIN (virtual_offset))
2684 if (BINFO_VIRTUAL_P (virtual_offset))
2685 break;
2687 if (virtual_offset
2688 || (thunk_binfo && !BINFO_OFFSET_ZEROP (thunk_binfo)))
2690 tree offset = fold_convert (ssizetype, BINFO_OFFSET (thunk_binfo));
2692 if (virtual_offset)
2694 /* We convert via virtual base. Adjust the fixed
2695 offset to be from there. */
2696 offset =
2697 size_diffop (offset,
2698 fold_convert (ssizetype,
2699 BINFO_OFFSET (virtual_offset)));
2701 if (fixed_offset)
2702 /* There was an existing fixed offset, this must be
2703 from the base just converted to, and the base the
2704 FN was thunking to. */
2705 fixed_offset = size_binop (PLUS_EXPR, fixed_offset, offset);
2706 else
2707 fixed_offset = offset;
2711 if (fixed_offset || virtual_offset)
2712 /* Replace the overriding function with a covariant thunk. We
2713 will emit the overriding function in its own slot as
2714 well. */
2715 overrider_fn = make_thunk (overrider_target, /*this_adjusting=*/0,
2716 fixed_offset, virtual_offset);
2718 else
2719 gcc_assert (DECL_INVALID_OVERRIDER_P (overrider_target) ||
2720 !DECL_THUNK_P (fn));
2722 /* If we need a covariant thunk, then we may need to adjust first_defn.
2723 The ABI specifies that the thunks emitted with a function are
2724 determined by which bases the function overrides, so we need to be
2725 sure that we're using a thunk for some overridden base; even if we
2726 know that the necessary this adjustment is zero, there may not be an
2727 appropriate zero-this-adjusment thunk for us to use since thunks for
2728 overriding virtual bases always use the vcall offset.
2730 Furthermore, just choosing any base that overrides this function isn't
2731 quite right, as this slot won't be used for calls through a type that
2732 puts a covariant thunk here. Calling the function through such a type
2733 will use a different slot, and that slot is the one that determines
2734 the thunk emitted for that base.
2736 So, keep looking until we find the base that we're really overriding
2737 in this slot: the nearest primary base that doesn't use a covariant
2738 thunk in this slot. */
2739 if (overrider_target != overrider_fn)
2741 if (BINFO_TYPE (b) == DECL_CONTEXT (overrider_target))
2742 /* We already know that the overrider needs a covariant thunk. */
2743 b = get_primary_binfo (b);
2744 for (; ; b = get_primary_binfo (b))
2746 tree main_binfo = TYPE_BINFO (BINFO_TYPE (b));
2747 tree bv = chain_index (ix, BINFO_VIRTUALS (main_binfo));
2748 if (!DECL_THUNK_P (TREE_VALUE (bv)))
2749 break;
2750 if (BINFO_LOST_PRIMARY_P (b))
2751 lost = true;
2753 first_defn = b;
2756 /* Assume that we will produce a thunk that convert all the way to
2757 the final overrider, and not to an intermediate virtual base. */
2758 virtual_base = NULL_TREE;
2760 /* See if we can convert to an intermediate virtual base first, and then
2761 use the vcall offset located there to finish the conversion. */
2762 for (; b; b = BINFO_INHERITANCE_CHAIN (b))
2764 /* If we find the final overrider, then we can stop
2765 walking. */
2766 if (SAME_BINFO_TYPE_P (BINFO_TYPE (b),
2767 BINFO_TYPE (TREE_VALUE (overrider))))
2768 break;
2770 /* If we find a virtual base, and we haven't yet found the
2771 overrider, then there is a virtual base between the
2772 declaring base (first_defn) and the final overrider. */
2773 if (BINFO_VIRTUAL_P (b))
2775 virtual_base = b;
2776 break;
2780 /* Compute the constant adjustment to the `this' pointer. The
2781 `this' pointer, when this function is called, will point at BINFO
2782 (or one of its primary bases, which are at the same offset). */
2783 if (virtual_base)
2784 /* The `this' pointer needs to be adjusted from the declaration to
2785 the nearest virtual base. */
2786 delta = size_diffop_loc (input_location,
2787 fold_convert (ssizetype, BINFO_OFFSET (virtual_base)),
2788 fold_convert (ssizetype, BINFO_OFFSET (first_defn)));
2789 else if (lost)
2790 /* If the nearest definition is in a lost primary, we don't need an
2791 entry in our vtable. Except possibly in a constructor vtable,
2792 if we happen to get our primary back. In that case, the offset
2793 will be zero, as it will be a primary base. */
2794 delta = size_zero_node;
2795 else
2796 /* The `this' pointer needs to be adjusted from pointing to
2797 BINFO to pointing at the base where the final overrider
2798 appears. */
2799 delta = size_diffop_loc (input_location,
2800 fold_convert (ssizetype,
2801 BINFO_OFFSET (TREE_VALUE (overrider))),
2802 fold_convert (ssizetype, BINFO_OFFSET (binfo)));
2804 modify_vtable_entry (t, binfo, overrider_fn, delta, virtuals);
2806 if (virtual_base)
2807 BV_VCALL_INDEX (*virtuals)
2808 = get_vcall_index (overrider_target, BINFO_TYPE (virtual_base));
2809 else
2810 BV_VCALL_INDEX (*virtuals) = NULL_TREE;
2812 BV_LOST_PRIMARY (*virtuals) = lost;
2815 /* Called from modify_all_vtables via dfs_walk. */
2817 static tree
2818 dfs_modify_vtables (tree binfo, void* data)
2820 tree t = (tree) data;
2821 tree virtuals;
2822 tree old_virtuals;
2823 unsigned ix;
2825 if (!TYPE_CONTAINS_VPTR_P (BINFO_TYPE (binfo)))
2826 /* A base without a vtable needs no modification, and its bases
2827 are uninteresting. */
2828 return dfs_skip_bases;
2830 if (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), t)
2831 && !CLASSTYPE_HAS_PRIMARY_BASE_P (t))
2832 /* Don't do the primary vtable, if it's new. */
2833 return NULL_TREE;
2835 if (BINFO_PRIMARY_P (binfo) && !BINFO_VIRTUAL_P (binfo))
2836 /* There's no need to modify the vtable for a non-virtual primary
2837 base; we're not going to use that vtable anyhow. We do still
2838 need to do this for virtual primary bases, as they could become
2839 non-primary in a construction vtable. */
2840 return NULL_TREE;
2842 make_new_vtable (t, binfo);
2844 /* Now, go through each of the virtual functions in the virtual
2845 function table for BINFO. Find the final overrider, and update
2846 the BINFO_VIRTUALS list appropriately. */
2847 for (ix = 0, virtuals = BINFO_VIRTUALS (binfo),
2848 old_virtuals = BINFO_VIRTUALS (TYPE_BINFO (BINFO_TYPE (binfo)));
2849 virtuals;
2850 ix++, virtuals = TREE_CHAIN (virtuals),
2851 old_virtuals = TREE_CHAIN (old_virtuals))
2852 update_vtable_entry_for_fn (t,
2853 binfo,
2854 BV_FN (old_virtuals),
2855 &virtuals, ix);
2857 return NULL_TREE;
2860 /* Update all of the primary and secondary vtables for T. Create new
2861 vtables as required, and initialize their RTTI information. Each
2862 of the functions in VIRTUALS is declared in T and may override a
2863 virtual function from a base class; find and modify the appropriate
2864 entries to point to the overriding functions. Returns a list, in
2865 declaration order, of the virtual functions that are declared in T,
2866 but do not appear in the primary base class vtable, and which
2867 should therefore be appended to the end of the vtable for T. */
2869 static tree
2870 modify_all_vtables (tree t, tree virtuals)
2872 tree binfo = TYPE_BINFO (t);
2873 tree *fnsp;
2875 /* Mangle the vtable name before entering dfs_walk (c++/51884). */
2876 if (TYPE_CONTAINS_VPTR_P (t))
2877 get_vtable_decl (t, false);
2879 /* Update all of the vtables. */
2880 dfs_walk_once (binfo, dfs_modify_vtables, NULL, t);
2882 /* Add virtual functions not already in our primary vtable. These
2883 will be both those introduced by this class, and those overridden
2884 from secondary bases. It does not include virtuals merely
2885 inherited from secondary bases. */
2886 for (fnsp = &virtuals; *fnsp; )
2888 tree fn = TREE_VALUE (*fnsp);
2890 if (!value_member (fn, BINFO_VIRTUALS (binfo))
2891 || DECL_VINDEX (fn) == error_mark_node)
2893 /* We don't need to adjust the `this' pointer when
2894 calling this function. */
2895 BV_DELTA (*fnsp) = integer_zero_node;
2896 BV_VCALL_INDEX (*fnsp) = NULL_TREE;
2898 /* This is a function not already in our vtable. Keep it. */
2899 fnsp = &TREE_CHAIN (*fnsp);
2901 else
2902 /* We've already got an entry for this function. Skip it. */
2903 *fnsp = TREE_CHAIN (*fnsp);
2906 return virtuals;
2909 /* Get the base virtual function declarations in T that have the
2910 indicated NAME. */
2912 static void
2913 get_basefndecls (tree name, tree t, vec<tree> *base_fndecls)
2915 tree methods;
2916 int n_baseclasses = BINFO_N_BASE_BINFOS (TYPE_BINFO (t));
2917 int i;
2919 /* Find virtual functions in T with the indicated NAME. */
2920 i = lookup_fnfields_1 (t, name);
2921 bool found_decls = false;
2922 if (i != -1)
2923 for (methods = (*CLASSTYPE_METHOD_VEC (t))[i];
2924 methods;
2925 methods = OVL_NEXT (methods))
2927 tree method = OVL_CURRENT (methods);
2929 if (TREE_CODE (method) == FUNCTION_DECL
2930 && DECL_VINDEX (method))
2932 base_fndecls->safe_push (method);
2933 found_decls = true;
2937 if (found_decls)
2938 return;
2940 for (i = 0; i < n_baseclasses; i++)
2942 tree basetype = BINFO_TYPE (BINFO_BASE_BINFO (TYPE_BINFO (t), i));
2943 get_basefndecls (name, basetype, base_fndecls);
2947 /* If this declaration supersedes the declaration of
2948 a method declared virtual in the base class, then
2949 mark this field as being virtual as well. */
2951 void
2952 check_for_override (tree decl, tree ctype)
2954 bool overrides_found = false;
2955 if (TREE_CODE (decl) == TEMPLATE_DECL)
2956 /* In [temp.mem] we have:
2958 A specialization of a member function template does not
2959 override a virtual function from a base class. */
2960 return;
2961 if ((DECL_DESTRUCTOR_P (decl)
2962 || IDENTIFIER_VIRTUAL_P (DECL_NAME (decl))
2963 || DECL_CONV_FN_P (decl))
2964 && look_for_overrides (ctype, decl)
2965 && !DECL_STATIC_FUNCTION_P (decl))
2966 /* Set DECL_VINDEX to a value that is neither an INTEGER_CST nor
2967 the error_mark_node so that we know it is an overriding
2968 function. */
2970 DECL_VINDEX (decl) = decl;
2971 overrides_found = true;
2972 if (warn_override && !DECL_OVERRIDE_P (decl)
2973 && !DECL_DESTRUCTOR_P (decl))
2974 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wsuggest_override,
2975 "%qD can be marked override", decl);
2978 if (DECL_VIRTUAL_P (decl))
2980 if (!DECL_VINDEX (decl))
2981 DECL_VINDEX (decl) = error_mark_node;
2982 IDENTIFIER_VIRTUAL_P (DECL_NAME (decl)) = 1;
2983 if (DECL_DESTRUCTOR_P (decl))
2984 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (ctype) = true;
2986 else if (DECL_FINAL_P (decl))
2987 error ("%q+#D marked %<final%>, but is not virtual", decl);
2988 if (DECL_OVERRIDE_P (decl) && !overrides_found)
2989 error ("%q+#D marked %<override%>, but does not override", decl);
2992 /* Warn about hidden virtual functions that are not overridden in t.
2993 We know that constructors and destructors don't apply. */
2995 static void
2996 warn_hidden (tree t)
2998 vec<tree, va_gc> *method_vec = CLASSTYPE_METHOD_VEC (t);
2999 tree fns;
3000 size_t i;
3002 /* We go through each separately named virtual function. */
3003 for (i = CLASSTYPE_FIRST_CONVERSION_SLOT;
3004 vec_safe_iterate (method_vec, i, &fns);
3005 ++i)
3007 tree fn;
3008 tree name;
3009 tree fndecl;
3010 tree base_binfo;
3011 tree binfo;
3012 int j;
3014 /* All functions in this slot in the CLASSTYPE_METHOD_VEC will
3015 have the same name. Figure out what name that is. */
3016 name = DECL_NAME (OVL_CURRENT (fns));
3017 /* There are no possibly hidden functions yet. */
3018 auto_vec<tree, 20> base_fndecls;
3019 /* Iterate through all of the base classes looking for possibly
3020 hidden functions. */
3021 for (binfo = TYPE_BINFO (t), j = 0;
3022 BINFO_BASE_ITERATE (binfo, j, base_binfo); j++)
3024 tree basetype = BINFO_TYPE (base_binfo);
3025 get_basefndecls (name, basetype, &base_fndecls);
3028 /* If there are no functions to hide, continue. */
3029 if (base_fndecls.is_empty ())
3030 continue;
3032 /* Remove any overridden functions. */
3033 for (fn = fns; fn; fn = OVL_NEXT (fn))
3035 fndecl = OVL_CURRENT (fn);
3036 if (TREE_CODE (fndecl) == FUNCTION_DECL
3037 && DECL_VINDEX (fndecl))
3039 /* If the method from the base class has the same
3040 signature as the method from the derived class, it
3041 has been overridden. */
3042 for (size_t k = 0; k < base_fndecls.length (); k++)
3043 if (base_fndecls[k]
3044 && same_signature_p (fndecl, base_fndecls[k]))
3045 base_fndecls[k] = NULL_TREE;
3049 /* Now give a warning for all base functions without overriders,
3050 as they are hidden. */
3051 size_t k;
3052 tree base_fndecl;
3053 FOR_EACH_VEC_ELT (base_fndecls, k, base_fndecl)
3054 if (base_fndecl)
3056 /* Here we know it is a hider, and no overrider exists. */
3057 warning_at (location_of (base_fndecl),
3058 OPT_Woverloaded_virtual,
3059 "%qD was hidden", base_fndecl);
3060 warning_at (location_of (fns),
3061 OPT_Woverloaded_virtual, " by %qD", fns);
3066 /* Recursive helper for finish_struct_anon. */
3068 static void
3069 finish_struct_anon_r (tree field, bool complain)
3071 bool is_union = TREE_CODE (TREE_TYPE (field)) == UNION_TYPE;
3072 tree elt = TYPE_FIELDS (TREE_TYPE (field));
3073 for (; elt; elt = DECL_CHAIN (elt))
3075 /* We're generally only interested in entities the user
3076 declared, but we also find nested classes by noticing
3077 the TYPE_DECL that we create implicitly. You're
3078 allowed to put one anonymous union inside another,
3079 though, so we explicitly tolerate that. We use
3080 TYPE_ANONYMOUS_P rather than ANON_AGGR_TYPE_P so that
3081 we also allow unnamed types used for defining fields. */
3082 if (DECL_ARTIFICIAL (elt)
3083 && (!DECL_IMPLICIT_TYPEDEF_P (elt)
3084 || TYPE_ANONYMOUS_P (TREE_TYPE (elt))))
3085 continue;
3087 if (TREE_CODE (elt) != FIELD_DECL)
3089 /* We already complained about static data members in
3090 finish_static_data_member_decl. */
3091 if (complain && !VAR_P (elt))
3093 if (is_union)
3094 permerror (DECL_SOURCE_LOCATION (elt),
3095 "%q#D invalid; an anonymous union can "
3096 "only have non-static data members", elt);
3097 else
3098 permerror (DECL_SOURCE_LOCATION (elt),
3099 "%q#D invalid; an anonymous struct can "
3100 "only have non-static data members", elt);
3102 continue;
3105 if (complain)
3107 if (TREE_PRIVATE (elt))
3109 if (is_union)
3110 permerror (DECL_SOURCE_LOCATION (elt),
3111 "private member %q#D in anonymous union", elt);
3112 else
3113 permerror (DECL_SOURCE_LOCATION (elt),
3114 "private member %q#D in anonymous struct", elt);
3116 else if (TREE_PROTECTED (elt))
3118 if (is_union)
3119 permerror (DECL_SOURCE_LOCATION (elt),
3120 "protected member %q#D in anonymous union", elt);
3121 else
3122 permerror (DECL_SOURCE_LOCATION (elt),
3123 "protected member %q#D in anonymous struct", elt);
3127 TREE_PRIVATE (elt) = TREE_PRIVATE (field);
3128 TREE_PROTECTED (elt) = TREE_PROTECTED (field);
3130 /* Recurse into the anonymous aggregates to handle correctly
3131 access control (c++/24926):
3133 class A {
3134 union {
3135 union {
3136 int i;
3141 int j=A().i; */
3142 if (DECL_NAME (elt) == NULL_TREE
3143 && ANON_AGGR_TYPE_P (TREE_TYPE (elt)))
3144 finish_struct_anon_r (elt, /*complain=*/false);
3148 /* Check for things that are invalid. There are probably plenty of other
3149 things we should check for also. */
3151 static void
3152 finish_struct_anon (tree t)
3154 for (tree field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
3156 if (TREE_STATIC (field))
3157 continue;
3158 if (TREE_CODE (field) != FIELD_DECL)
3159 continue;
3161 if (DECL_NAME (field) == NULL_TREE
3162 && ANON_AGGR_TYPE_P (TREE_TYPE (field)))
3163 finish_struct_anon_r (field, /*complain=*/true);
3167 /* Add T to CLASSTYPE_DECL_LIST of current_class_type which
3168 will be used later during class template instantiation.
3169 When FRIEND_P is zero, T can be a static member data (VAR_DECL),
3170 a non-static member data (FIELD_DECL), a member function
3171 (FUNCTION_DECL), a nested type (RECORD_TYPE, ENUM_TYPE),
3172 a typedef (TYPE_DECL) or a member class template (TEMPLATE_DECL)
3173 When FRIEND_P is nonzero, T is either a friend class
3174 (RECORD_TYPE, TEMPLATE_DECL) or a friend function
3175 (FUNCTION_DECL, TEMPLATE_DECL). */
3177 void
3178 maybe_add_class_template_decl_list (tree type, tree t, int friend_p)
3180 /* Save some memory by not creating TREE_LIST if TYPE is not template. */
3181 if (CLASSTYPE_TEMPLATE_INFO (type))
3182 CLASSTYPE_DECL_LIST (type)
3183 = tree_cons (friend_p ? NULL_TREE : type,
3184 t, CLASSTYPE_DECL_LIST (type));
3187 /* This function is called from declare_virt_assop_and_dtor via
3188 dfs_walk_all.
3190 DATA is a type that direcly or indirectly inherits the base
3191 represented by BINFO. If BINFO contains a virtual assignment [copy
3192 assignment or move assigment] operator or a virtual constructor,
3193 declare that function in DATA if it hasn't been already declared. */
3195 static tree
3196 dfs_declare_virt_assop_and_dtor (tree binfo, void *data)
3198 tree bv, fn, t = (tree)data;
3199 tree opname = ansi_assopname (NOP_EXPR);
3201 gcc_assert (t && CLASS_TYPE_P (t));
3202 gcc_assert (binfo && TREE_CODE (binfo) == TREE_BINFO);
3204 if (!TYPE_CONTAINS_VPTR_P (BINFO_TYPE (binfo)))
3205 /* A base without a vtable needs no modification, and its bases
3206 are uninteresting. */
3207 return dfs_skip_bases;
3209 if (BINFO_PRIMARY_P (binfo))
3210 /* If this is a primary base, then we have already looked at the
3211 virtual functions of its vtable. */
3212 return NULL_TREE;
3214 for (bv = BINFO_VIRTUALS (binfo); bv; bv = TREE_CHAIN (bv))
3216 fn = BV_FN (bv);
3218 if (DECL_NAME (fn) == opname)
3220 if (CLASSTYPE_LAZY_COPY_ASSIGN (t))
3221 lazily_declare_fn (sfk_copy_assignment, t);
3222 if (CLASSTYPE_LAZY_MOVE_ASSIGN (t))
3223 lazily_declare_fn (sfk_move_assignment, t);
3225 else if (DECL_DESTRUCTOR_P (fn)
3226 && CLASSTYPE_LAZY_DESTRUCTOR (t))
3227 lazily_declare_fn (sfk_destructor, t);
3230 return NULL_TREE;
3233 /* If the class type T has a direct or indirect base that contains a
3234 virtual assignment operator or a virtual destructor, declare that
3235 function in T if it hasn't been already declared. */
3237 static void
3238 declare_virt_assop_and_dtor (tree t)
3240 if (!(TYPE_POLYMORPHIC_P (t)
3241 && (CLASSTYPE_LAZY_COPY_ASSIGN (t)
3242 || CLASSTYPE_LAZY_MOVE_ASSIGN (t)
3243 || CLASSTYPE_LAZY_DESTRUCTOR (t))))
3244 return;
3246 dfs_walk_all (TYPE_BINFO (t),
3247 dfs_declare_virt_assop_and_dtor,
3248 NULL, t);
3251 /* Declare the inheriting constructor for class T inherited from base
3252 constructor CTOR with the parameter array PARMS of size NPARMS. */
3254 static void
3255 one_inheriting_sig (tree t, tree ctor, tree *parms, int nparms)
3257 /* We don't declare an inheriting ctor that would be a default,
3258 copy or move ctor for derived or base. */
3259 if (nparms == 0)
3260 return;
3261 if (nparms == 1
3262 && TREE_CODE (parms[0]) == REFERENCE_TYPE)
3264 tree parm = TYPE_MAIN_VARIANT (TREE_TYPE (parms[0]));
3265 if (parm == t || parm == DECL_CONTEXT (ctor))
3266 return;
3269 tree parmlist = void_list_node;
3270 for (int i = nparms - 1; i >= 0; i--)
3271 parmlist = tree_cons (NULL_TREE, parms[i], parmlist);
3272 tree fn = implicitly_declare_fn (sfk_inheriting_constructor,
3273 t, false, ctor, parmlist);
3274 gcc_assert (TYPE_MAIN_VARIANT (t) == t);
3275 if (add_method (t, fn, NULL_TREE))
3277 DECL_CHAIN (fn) = TYPE_METHODS (t);
3278 TYPE_METHODS (t) = fn;
3282 /* Declare all the inheriting constructors for class T inherited from base
3283 constructor CTOR. */
3285 static void
3286 one_inherited_ctor (tree ctor, tree t)
3288 tree parms = FUNCTION_FIRST_USER_PARMTYPE (ctor);
3290 tree *new_parms = XALLOCAVEC (tree, list_length (parms));
3291 int i = 0;
3292 for (; parms && parms != void_list_node; parms = TREE_CHAIN (parms))
3294 if (TREE_PURPOSE (parms))
3295 one_inheriting_sig (t, ctor, new_parms, i);
3296 new_parms[i++] = TREE_VALUE (parms);
3298 one_inheriting_sig (t, ctor, new_parms, i);
3299 if (parms == NULL_TREE)
3301 if (warning (OPT_Winherited_variadic_ctor,
3302 "the ellipsis in %qD is not inherited", ctor))
3303 inform (DECL_SOURCE_LOCATION (ctor), "%qD declared here", ctor);
3307 /* Create default constructors, assignment operators, and so forth for
3308 the type indicated by T, if they are needed. CANT_HAVE_CONST_CTOR,
3309 and CANT_HAVE_CONST_ASSIGNMENT are nonzero if, for whatever reason,
3310 the class cannot have a default constructor, copy constructor
3311 taking a const reference argument, or an assignment operator taking
3312 a const reference, respectively. */
3314 static void
3315 add_implicitly_declared_members (tree t, tree* access_decls,
3316 int cant_have_const_cctor,
3317 int cant_have_const_assignment)
3319 bool move_ok = false;
3321 if (cxx_dialect >= cxx11 && !CLASSTYPE_DESTRUCTORS (t)
3322 && !TYPE_HAS_COPY_CTOR (t) && !TYPE_HAS_COPY_ASSIGN (t)
3323 && !type_has_move_constructor (t) && !type_has_move_assign (t))
3324 move_ok = true;
3326 /* Destructor. */
3327 if (!CLASSTYPE_DESTRUCTORS (t))
3329 /* In general, we create destructors lazily. */
3330 CLASSTYPE_LAZY_DESTRUCTOR (t) = 1;
3332 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t)
3333 && TYPE_FOR_JAVA (t))
3334 /* But if this is a Java class, any non-trivial destructor is
3335 invalid, even if compiler-generated. Therefore, if the
3336 destructor is non-trivial we create it now. */
3337 lazily_declare_fn (sfk_destructor, t);
3340 /* [class.ctor]
3342 If there is no user-declared constructor for a class, a default
3343 constructor is implicitly declared. */
3344 if (! TYPE_HAS_USER_CONSTRUCTOR (t))
3346 TYPE_HAS_DEFAULT_CONSTRUCTOR (t) = 1;
3347 CLASSTYPE_LAZY_DEFAULT_CTOR (t) = 1;
3348 if (cxx_dialect >= cxx11)
3349 TYPE_HAS_CONSTEXPR_CTOR (t)
3350 /* Don't force the declaration to get a hard answer; if the
3351 definition would have made the class non-literal, it will still be
3352 non-literal because of the base or member in question, and that
3353 gives a better diagnostic. */
3354 = type_maybe_constexpr_default_constructor (t);
3357 /* [class.ctor]
3359 If a class definition does not explicitly declare a copy
3360 constructor, one is declared implicitly. */
3361 if (! TYPE_HAS_COPY_CTOR (t) && ! TYPE_FOR_JAVA (t))
3363 TYPE_HAS_COPY_CTOR (t) = 1;
3364 TYPE_HAS_CONST_COPY_CTOR (t) = !cant_have_const_cctor;
3365 CLASSTYPE_LAZY_COPY_CTOR (t) = 1;
3366 if (move_ok)
3367 CLASSTYPE_LAZY_MOVE_CTOR (t) = 1;
3370 /* If there is no assignment operator, one will be created if and
3371 when it is needed. For now, just record whether or not the type
3372 of the parameter to the assignment operator will be a const or
3373 non-const reference. */
3374 if (!TYPE_HAS_COPY_ASSIGN (t) && !TYPE_FOR_JAVA (t))
3376 TYPE_HAS_COPY_ASSIGN (t) = 1;
3377 TYPE_HAS_CONST_COPY_ASSIGN (t) = !cant_have_const_assignment;
3378 CLASSTYPE_LAZY_COPY_ASSIGN (t) = 1;
3379 if (move_ok && !LAMBDA_TYPE_P (t))
3380 CLASSTYPE_LAZY_MOVE_ASSIGN (t) = 1;
3383 /* We can't be lazy about declaring functions that might override
3384 a virtual function from a base class. */
3385 declare_virt_assop_and_dtor (t);
3387 while (*access_decls)
3389 tree using_decl = TREE_VALUE (*access_decls);
3390 tree decl = USING_DECL_DECLS (using_decl);
3391 if (DECL_NAME (using_decl) == ctor_identifier)
3393 /* declare, then remove the decl */
3394 tree ctor_list = decl;
3395 location_t loc = input_location;
3396 input_location = DECL_SOURCE_LOCATION (using_decl);
3397 if (ctor_list)
3398 for (; ctor_list; ctor_list = OVL_NEXT (ctor_list))
3399 one_inherited_ctor (OVL_CURRENT (ctor_list), t);
3400 *access_decls = TREE_CHAIN (*access_decls);
3401 input_location = loc;
3403 else
3404 access_decls = &TREE_CHAIN (*access_decls);
3408 /* Subroutine of insert_into_classtype_sorted_fields. Recursively
3409 count the number of fields in TYPE, including anonymous union
3410 members. */
3412 static int
3413 count_fields (tree fields)
3415 tree x;
3416 int n_fields = 0;
3417 for (x = fields; x; x = DECL_CHAIN (x))
3419 if (TREE_CODE (x) == FIELD_DECL && ANON_AGGR_TYPE_P (TREE_TYPE (x)))
3420 n_fields += count_fields (TYPE_FIELDS (TREE_TYPE (x)));
3421 else
3422 n_fields += 1;
3424 return n_fields;
3427 /* Subroutine of insert_into_classtype_sorted_fields. Recursively add
3428 all the fields in the TREE_LIST FIELDS to the SORTED_FIELDS_TYPE
3429 elts, starting at offset IDX. */
3431 static int
3432 add_fields_to_record_type (tree fields, struct sorted_fields_type *field_vec, int idx)
3434 tree x;
3435 for (x = fields; x; x = DECL_CHAIN (x))
3437 if (TREE_CODE (x) == FIELD_DECL && ANON_AGGR_TYPE_P (TREE_TYPE (x)))
3438 idx = add_fields_to_record_type (TYPE_FIELDS (TREE_TYPE (x)), field_vec, idx);
3439 else
3440 field_vec->elts[idx++] = x;
3442 return idx;
3445 /* Add all of the enum values of ENUMTYPE, to the FIELD_VEC elts,
3446 starting at offset IDX. */
3448 static int
3449 add_enum_fields_to_record_type (tree enumtype,
3450 struct sorted_fields_type *field_vec,
3451 int idx)
3453 tree values;
3454 for (values = TYPE_VALUES (enumtype); values; values = TREE_CHAIN (values))
3455 field_vec->elts[idx++] = TREE_VALUE (values);
3456 return idx;
3459 /* FIELD is a bit-field. We are finishing the processing for its
3460 enclosing type. Issue any appropriate messages and set appropriate
3461 flags. Returns false if an error has been diagnosed. */
3463 static bool
3464 check_bitfield_decl (tree field)
3466 tree type = TREE_TYPE (field);
3467 tree w;
3469 /* Extract the declared width of the bitfield, which has been
3470 temporarily stashed in DECL_INITIAL. */
3471 w = DECL_INITIAL (field);
3472 gcc_assert (w != NULL_TREE);
3473 /* Remove the bit-field width indicator so that the rest of the
3474 compiler does not treat that value as an initializer. */
3475 DECL_INITIAL (field) = NULL_TREE;
3477 /* Detect invalid bit-field type. */
3478 if (!INTEGRAL_OR_ENUMERATION_TYPE_P (type))
3480 error ("bit-field %q+#D with non-integral type", field);
3481 w = error_mark_node;
3483 else
3485 location_t loc = input_location;
3486 /* Avoid the non_lvalue wrapper added by fold for PLUS_EXPRs. */
3487 STRIP_NOPS (w);
3489 /* detect invalid field size. */
3490 input_location = DECL_SOURCE_LOCATION (field);
3491 w = cxx_constant_value (w);
3492 input_location = loc;
3494 if (TREE_CODE (w) != INTEGER_CST)
3496 error ("bit-field %q+D width not an integer constant", field);
3497 w = error_mark_node;
3499 else if (tree_int_cst_sgn (w) < 0)
3501 error ("negative width in bit-field %q+D", field);
3502 w = error_mark_node;
3504 else if (integer_zerop (w) && DECL_NAME (field) != 0)
3506 error ("zero width for bit-field %q+D", field);
3507 w = error_mark_node;
3509 else if ((TREE_CODE (type) != ENUMERAL_TYPE
3510 && TREE_CODE (type) != BOOLEAN_TYPE
3511 && compare_tree_int (w, TYPE_PRECISION (type)) > 0)
3512 || ((TREE_CODE (type) == ENUMERAL_TYPE
3513 || TREE_CODE (type) == BOOLEAN_TYPE)
3514 && tree_int_cst_lt (TYPE_SIZE (type), w)))
3515 warning_at (DECL_SOURCE_LOCATION (field), 0,
3516 "width of %qD exceeds its type", field);
3517 else if (TREE_CODE (type) == ENUMERAL_TYPE
3518 && (0 > (compare_tree_int
3519 (w, TYPE_PRECISION (ENUM_UNDERLYING_TYPE (type))))))
3520 warning_at (DECL_SOURCE_LOCATION (field), 0,
3521 "%qD is too small to hold all values of %q#T",
3522 field, type);
3525 if (w != error_mark_node)
3527 DECL_SIZE (field) = fold_convert (bitsizetype, w);
3528 DECL_BIT_FIELD (field) = 1;
3529 return true;
3531 else
3533 /* Non-bit-fields are aligned for their type. */
3534 DECL_BIT_FIELD (field) = 0;
3535 CLEAR_DECL_C_BIT_FIELD (field);
3536 return false;
3540 /* FIELD is a non bit-field. We are finishing the processing for its
3541 enclosing type T. Issue any appropriate messages and set appropriate
3542 flags. */
3544 static bool
3545 check_field_decl (tree field,
3546 tree t,
3547 int* cant_have_const_ctor,
3548 int* no_const_asn_ref)
3550 tree type = strip_array_types (TREE_TYPE (field));
3551 bool any_default_members = false;
3553 /* In C++98 an anonymous union cannot contain any fields which would change
3554 the settings of CANT_HAVE_CONST_CTOR and friends. */
3555 if (ANON_UNION_TYPE_P (type) && cxx_dialect < cxx11)
3557 /* And, we don't set TYPE_HAS_CONST_COPY_CTOR, etc., for anonymous
3558 structs. So, we recurse through their fields here. */
3559 else if (ANON_AGGR_TYPE_P (type))
3561 for (tree fields = TYPE_FIELDS (type); fields;
3562 fields = DECL_CHAIN (fields))
3563 if (TREE_CODE (fields) == FIELD_DECL && !DECL_C_BIT_FIELD (field))
3564 any_default_members |= check_field_decl (fields, t,
3565 cant_have_const_ctor,
3566 no_const_asn_ref);
3568 /* Check members with class type for constructors, destructors,
3569 etc. */
3570 else if (CLASS_TYPE_P (type))
3572 /* Never let anything with uninheritable virtuals
3573 make it through without complaint. */
3574 abstract_virtuals_error (field, type);
3576 if (TREE_CODE (t) == UNION_TYPE && cxx_dialect < cxx11)
3578 static bool warned;
3579 int oldcount = errorcount;
3580 if (TYPE_NEEDS_CONSTRUCTING (type))
3581 error ("member %q+#D with constructor not allowed in union",
3582 field);
3583 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
3584 error ("member %q+#D with destructor not allowed in union", field);
3585 if (TYPE_HAS_COMPLEX_COPY_ASSIGN (type))
3586 error ("member %q+#D with copy assignment operator not allowed in union",
3587 field);
3588 if (!warned && errorcount > oldcount)
3590 inform (DECL_SOURCE_LOCATION (field), "unrestricted unions "
3591 "only available with -std=c++11 or -std=gnu++11");
3592 warned = true;
3595 else
3597 TYPE_NEEDS_CONSTRUCTING (t) |= TYPE_NEEDS_CONSTRUCTING (type);
3598 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t)
3599 |= TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type);
3600 TYPE_HAS_COMPLEX_COPY_ASSIGN (t)
3601 |= (TYPE_HAS_COMPLEX_COPY_ASSIGN (type)
3602 || !TYPE_HAS_COPY_ASSIGN (type));
3603 TYPE_HAS_COMPLEX_COPY_CTOR (t) |= (TYPE_HAS_COMPLEX_COPY_CTOR (type)
3604 || !TYPE_HAS_COPY_CTOR (type));
3605 TYPE_HAS_COMPLEX_MOVE_ASSIGN (t) |= TYPE_HAS_COMPLEX_MOVE_ASSIGN (type);
3606 TYPE_HAS_COMPLEX_MOVE_CTOR (t) |= TYPE_HAS_COMPLEX_MOVE_CTOR (type);
3607 TYPE_HAS_COMPLEX_DFLT (t) |= (!TYPE_HAS_DEFAULT_CONSTRUCTOR (type)
3608 || TYPE_HAS_COMPLEX_DFLT (type));
3611 if (TYPE_HAS_COPY_CTOR (type)
3612 && !TYPE_HAS_CONST_COPY_CTOR (type))
3613 *cant_have_const_ctor = 1;
3615 if (TYPE_HAS_COPY_ASSIGN (type)
3616 && !TYPE_HAS_CONST_COPY_ASSIGN (type))
3617 *no_const_asn_ref = 1;
3620 check_abi_tags (t, field);
3622 if (DECL_INITIAL (field) != NULL_TREE)
3623 /* `build_class_init_list' does not recognize
3624 non-FIELD_DECLs. */
3625 any_default_members = true;
3627 return any_default_members;
3630 /* Check the data members (both static and non-static), class-scoped
3631 typedefs, etc., appearing in the declaration of T. Issue
3632 appropriate diagnostics. Sets ACCESS_DECLS to a list (in
3633 declaration order) of access declarations; each TREE_VALUE in this
3634 list is a USING_DECL.
3636 In addition, set the following flags:
3638 EMPTY_P
3639 The class is empty, i.e., contains no non-static data members.
3641 CANT_HAVE_CONST_CTOR_P
3642 This class cannot have an implicitly generated copy constructor
3643 taking a const reference.
3645 CANT_HAVE_CONST_ASN_REF
3646 This class cannot have an implicitly generated assignment
3647 operator taking a const reference.
3649 All of these flags should be initialized before calling this
3650 function.
3652 Returns a pointer to the end of the TYPE_FIELDs chain; additional
3653 fields can be added by adding to this chain. */
3655 static void
3656 check_field_decls (tree t, tree *access_decls,
3657 int *cant_have_const_ctor_p,
3658 int *no_const_asn_ref_p)
3660 tree *field;
3661 tree *next;
3662 bool has_pointers;
3663 bool any_default_members;
3664 int cant_pack = 0;
3665 int field_access = -1;
3667 /* Assume there are no access declarations. */
3668 *access_decls = NULL_TREE;
3669 /* Assume this class has no pointer members. */
3670 has_pointers = false;
3671 /* Assume none of the members of this class have default
3672 initializations. */
3673 any_default_members = false;
3675 for (field = &TYPE_FIELDS (t); *field; field = next)
3677 tree x = *field;
3678 tree type = TREE_TYPE (x);
3679 int this_field_access;
3681 next = &DECL_CHAIN (x);
3683 if (TREE_CODE (x) == USING_DECL)
3685 /* Save the access declarations for our caller. */
3686 *access_decls = tree_cons (NULL_TREE, x, *access_decls);
3687 continue;
3690 if (TREE_CODE (x) == TYPE_DECL
3691 || TREE_CODE (x) == TEMPLATE_DECL)
3692 continue;
3694 /* If we've gotten this far, it's a data member, possibly static,
3695 or an enumerator. */
3696 if (TREE_CODE (x) != CONST_DECL)
3697 DECL_CONTEXT (x) = t;
3699 /* When this goes into scope, it will be a non-local reference. */
3700 DECL_NONLOCAL (x) = 1;
3702 if (TREE_CODE (t) == UNION_TYPE
3703 && cxx_dialect < cxx11)
3705 /* [class.union] (C++98)
3707 If a union contains a static data member, or a member of
3708 reference type, the program is ill-formed.
3710 In C++11 this limitation doesn't exist anymore. */
3711 if (VAR_P (x))
3713 error ("in C++98 %q+D may not be static because it is "
3714 "a member of a union", x);
3715 continue;
3717 if (TREE_CODE (type) == REFERENCE_TYPE)
3719 error ("in C++98 %q+D may not have reference type %qT "
3720 "because it is a member of a union", x, type);
3721 continue;
3725 /* Perform error checking that did not get done in
3726 grokdeclarator. */
3727 if (TREE_CODE (type) == FUNCTION_TYPE)
3729 error ("field %q+D invalidly declared function type", x);
3730 type = build_pointer_type (type);
3731 TREE_TYPE (x) = type;
3733 else if (TREE_CODE (type) == METHOD_TYPE)
3735 error ("field %q+D invalidly declared method type", x);
3736 type = build_pointer_type (type);
3737 TREE_TYPE (x) = type;
3740 if (type == error_mark_node)
3741 continue;
3743 if (TREE_CODE (x) == CONST_DECL || VAR_P (x))
3744 continue;
3746 /* Now it can only be a FIELD_DECL. */
3748 if (TREE_PRIVATE (x) || TREE_PROTECTED (x))
3749 CLASSTYPE_NON_AGGREGATE (t) = 1;
3751 /* If at least one non-static data member is non-literal, the whole
3752 class becomes non-literal. Per Core/1453, volatile non-static
3753 data members and base classes are also not allowed.
3754 Note: if the type is incomplete we will complain later on. */
3755 if (COMPLETE_TYPE_P (type)
3756 && (!literal_type_p (type) || CP_TYPE_VOLATILE_P (type)))
3757 CLASSTYPE_LITERAL_P (t) = false;
3759 /* A standard-layout class is a class that:
3761 has the same access control (Clause 11) for all non-static data members,
3762 ... */
3763 this_field_access = TREE_PROTECTED (x) ? 1 : TREE_PRIVATE (x) ? 2 : 0;
3764 if (field_access == -1)
3765 field_access = this_field_access;
3766 else if (this_field_access != field_access)
3767 CLASSTYPE_NON_STD_LAYOUT (t) = 1;
3769 /* If this is of reference type, check if it needs an init. */
3770 if (TREE_CODE (type) == REFERENCE_TYPE)
3772 CLASSTYPE_NON_LAYOUT_POD_P (t) = 1;
3773 CLASSTYPE_NON_STD_LAYOUT (t) = 1;
3774 if (DECL_INITIAL (x) == NULL_TREE)
3775 SET_CLASSTYPE_REF_FIELDS_NEED_INIT (t, 1);
3776 if (cxx_dialect < cxx11)
3778 /* ARM $12.6.2: [A member initializer list] (or, for an
3779 aggregate, initialization by a brace-enclosed list) is the
3780 only way to initialize nonstatic const and reference
3781 members. */
3782 TYPE_HAS_COMPLEX_COPY_ASSIGN (t) = 1;
3783 TYPE_HAS_COMPLEX_MOVE_ASSIGN (t) = 1;
3787 type = strip_array_types (type);
3789 if (TYPE_PACKED (t))
3791 if (!layout_pod_type_p (type) && !TYPE_PACKED (type))
3793 warning_at
3794 (DECL_SOURCE_LOCATION (x), 0,
3795 "ignoring packed attribute because of unpacked non-POD field %q#D",
3797 cant_pack = 1;
3799 else if (DECL_C_BIT_FIELD (x)
3800 || TYPE_ALIGN (TREE_TYPE (x)) > BITS_PER_UNIT)
3801 DECL_PACKED (x) = 1;
3804 if (DECL_C_BIT_FIELD (x) && integer_zerop (DECL_INITIAL (x)))
3805 /* We don't treat zero-width bitfields as making a class
3806 non-empty. */
3808 else
3810 /* The class is non-empty. */
3811 CLASSTYPE_EMPTY_P (t) = 0;
3812 /* The class is not even nearly empty. */
3813 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
3814 /* If one of the data members contains an empty class,
3815 so does T. */
3816 if (CLASS_TYPE_P (type)
3817 && CLASSTYPE_CONTAINS_EMPTY_CLASS_P (type))
3818 CLASSTYPE_CONTAINS_EMPTY_CLASS_P (t) = 1;
3821 /* This is used by -Weffc++ (see below). Warn only for pointers
3822 to members which might hold dynamic memory. So do not warn
3823 for pointers to functions or pointers to members. */
3824 if (TYPE_PTR_P (type)
3825 && !TYPE_PTRFN_P (type))
3826 has_pointers = true;
3828 if (CLASS_TYPE_P (type))
3830 if (CLASSTYPE_REF_FIELDS_NEED_INIT (type))
3831 SET_CLASSTYPE_REF_FIELDS_NEED_INIT (t, 1);
3832 if (CLASSTYPE_READONLY_FIELDS_NEED_INIT (type))
3833 SET_CLASSTYPE_READONLY_FIELDS_NEED_INIT (t, 1);
3836 if (DECL_MUTABLE_P (x) || TYPE_HAS_MUTABLE_P (type))
3837 CLASSTYPE_HAS_MUTABLE (t) = 1;
3839 if (DECL_MUTABLE_P (x))
3841 if (CP_TYPE_CONST_P (type))
3843 error ("member %q+D cannot be declared both %<const%> "
3844 "and %<mutable%>", x);
3845 continue;
3847 if (TREE_CODE (type) == REFERENCE_TYPE)
3849 error ("member %q+D cannot be declared as a %<mutable%> "
3850 "reference", x);
3851 continue;
3855 if (! layout_pod_type_p (type))
3856 /* DR 148 now allows pointers to members (which are POD themselves),
3857 to be allowed in POD structs. */
3858 CLASSTYPE_NON_LAYOUT_POD_P (t) = 1;
3860 if (!std_layout_type_p (type))
3861 CLASSTYPE_NON_STD_LAYOUT (t) = 1;
3863 if (! zero_init_p (type))
3864 CLASSTYPE_NON_ZERO_INIT_P (t) = 1;
3866 /* We set DECL_C_BIT_FIELD in grokbitfield.
3867 If the type and width are valid, we'll also set DECL_BIT_FIELD. */
3868 if ((! DECL_C_BIT_FIELD (x) || ! check_bitfield_decl (x))
3869 && check_field_decl (x, t,
3870 cant_have_const_ctor_p,
3871 no_const_asn_ref_p))
3873 if (any_default_members
3874 && TREE_CODE (t) == UNION_TYPE)
3875 error ("multiple fields in union %qT initialized", t);
3876 any_default_members = true;
3879 /* Now that we've removed bit-field widths from DECL_INITIAL,
3880 anything left in DECL_INITIAL is an NSDMI that makes the class
3881 non-aggregate in C++11. */
3882 if (DECL_INITIAL (x) && cxx_dialect < cxx14)
3883 CLASSTYPE_NON_AGGREGATE (t) = true;
3885 /* If any field is const, the structure type is pseudo-const. */
3886 if (CP_TYPE_CONST_P (type))
3888 C_TYPE_FIELDS_READONLY (t) = 1;
3889 if (DECL_INITIAL (x) == NULL_TREE)
3890 SET_CLASSTYPE_READONLY_FIELDS_NEED_INIT (t, 1);
3891 if (cxx_dialect < cxx11)
3893 /* ARM $12.6.2: [A member initializer list] (or, for an
3894 aggregate, initialization by a brace-enclosed list) is the
3895 only way to initialize nonstatic const and reference
3896 members. */
3897 TYPE_HAS_COMPLEX_COPY_ASSIGN (t) = 1;
3898 TYPE_HAS_COMPLEX_MOVE_ASSIGN (t) = 1;
3901 /* A field that is pseudo-const makes the structure likewise. */
3902 else if (CLASS_TYPE_P (type))
3904 C_TYPE_FIELDS_READONLY (t) |= C_TYPE_FIELDS_READONLY (type);
3905 SET_CLASSTYPE_READONLY_FIELDS_NEED_INIT (t,
3906 CLASSTYPE_READONLY_FIELDS_NEED_INIT (t)
3907 | CLASSTYPE_READONLY_FIELDS_NEED_INIT (type));
3910 /* Core issue 80: A nonstatic data member is required to have a
3911 different name from the class iff the class has a
3912 user-declared constructor. */
3913 if (constructor_name_p (DECL_NAME (x), t)
3914 && TYPE_HAS_USER_CONSTRUCTOR (t))
3915 permerror (DECL_SOURCE_LOCATION (x),
3916 "field %q#D with same name as class", x);
3919 /* Effective C++ rule 11: if a class has dynamic memory held by pointers,
3920 it should also define a copy constructor and an assignment operator to
3921 implement the correct copy semantic (deep vs shallow, etc.). As it is
3922 not feasible to check whether the constructors do allocate dynamic memory
3923 and store it within members, we approximate the warning like this:
3925 -- Warn only if there are members which are pointers
3926 -- Warn only if there is a non-trivial constructor (otherwise,
3927 there cannot be memory allocated).
3928 -- Warn only if there is a non-trivial destructor. We assume that the
3929 user at least implemented the cleanup correctly, and a destructor
3930 is needed to free dynamic memory.
3932 This seems enough for practical purposes. */
3933 if (warn_ecpp
3934 && has_pointers
3935 && TYPE_HAS_USER_CONSTRUCTOR (t)
3936 && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t)
3937 && !(TYPE_HAS_COPY_CTOR (t) && TYPE_HAS_COPY_ASSIGN (t)))
3939 warning (OPT_Weffc__, "%q#T has pointer data members", t);
3941 if (! TYPE_HAS_COPY_CTOR (t))
3943 warning (OPT_Weffc__,
3944 " but does not override %<%T(const %T&)%>", t, t);
3945 if (!TYPE_HAS_COPY_ASSIGN (t))
3946 warning (OPT_Weffc__, " or %<operator=(const %T&)%>", t);
3948 else if (! TYPE_HAS_COPY_ASSIGN (t))
3949 warning (OPT_Weffc__,
3950 " but does not override %<operator=(const %T&)%>", t);
3953 /* Non-static data member initializers make the default constructor
3954 non-trivial. */
3955 if (any_default_members)
3957 TYPE_NEEDS_CONSTRUCTING (t) = true;
3958 TYPE_HAS_COMPLEX_DFLT (t) = true;
3961 /* If any of the fields couldn't be packed, unset TYPE_PACKED. */
3962 if (cant_pack)
3963 TYPE_PACKED (t) = 0;
3965 /* Check anonymous struct/anonymous union fields. */
3966 finish_struct_anon (t);
3968 /* We've built up the list of access declarations in reverse order.
3969 Fix that now. */
3970 *access_decls = nreverse (*access_decls);
3973 /* If TYPE is an empty class type, records its OFFSET in the table of
3974 OFFSETS. */
3976 static int
3977 record_subobject_offset (tree type, tree offset, splay_tree offsets)
3979 splay_tree_node n;
3981 if (!is_empty_class (type))
3982 return 0;
3984 /* Record the location of this empty object in OFFSETS. */
3985 n = splay_tree_lookup (offsets, (splay_tree_key) offset);
3986 if (!n)
3987 n = splay_tree_insert (offsets,
3988 (splay_tree_key) offset,
3989 (splay_tree_value) NULL_TREE);
3990 n->value = ((splay_tree_value)
3991 tree_cons (NULL_TREE,
3992 type,
3993 (tree) n->value));
3995 return 0;
3998 /* Returns nonzero if TYPE is an empty class type and there is
3999 already an entry in OFFSETS for the same TYPE as the same OFFSET. */
4001 static int
4002 check_subobject_offset (tree type, tree offset, splay_tree offsets)
4004 splay_tree_node n;
4005 tree t;
4007 if (!is_empty_class (type))
4008 return 0;
4010 /* Record the location of this empty object in OFFSETS. */
4011 n = splay_tree_lookup (offsets, (splay_tree_key) offset);
4012 if (!n)
4013 return 0;
4015 for (t = (tree) n->value; t; t = TREE_CHAIN (t))
4016 if (same_type_p (TREE_VALUE (t), type))
4017 return 1;
4019 return 0;
4022 /* Walk through all the subobjects of TYPE (located at OFFSET). Call
4023 F for every subobject, passing it the type, offset, and table of
4024 OFFSETS. If VBASES_P is one, then virtual non-primary bases should
4025 be traversed.
4027 If MAX_OFFSET is non-NULL, then subobjects with an offset greater
4028 than MAX_OFFSET will not be walked.
4030 If F returns a nonzero value, the traversal ceases, and that value
4031 is returned. Otherwise, returns zero. */
4033 static int
4034 walk_subobject_offsets (tree type,
4035 subobject_offset_fn f,
4036 tree offset,
4037 splay_tree offsets,
4038 tree max_offset,
4039 int vbases_p)
4041 int r = 0;
4042 tree type_binfo = NULL_TREE;
4044 /* If this OFFSET is bigger than the MAX_OFFSET, then we should
4045 stop. */
4046 if (max_offset && tree_int_cst_lt (max_offset, offset))
4047 return 0;
4049 if (type == error_mark_node)
4050 return 0;
4052 if (!TYPE_P (type))
4054 type_binfo = type;
4055 type = BINFO_TYPE (type);
4058 if (CLASS_TYPE_P (type))
4060 tree field;
4061 tree binfo;
4062 int i;
4064 /* Avoid recursing into objects that are not interesting. */
4065 if (!CLASSTYPE_CONTAINS_EMPTY_CLASS_P (type))
4066 return 0;
4068 /* Record the location of TYPE. */
4069 r = (*f) (type, offset, offsets);
4070 if (r)
4071 return r;
4073 /* Iterate through the direct base classes of TYPE. */
4074 if (!type_binfo)
4075 type_binfo = TYPE_BINFO (type);
4076 for (i = 0; BINFO_BASE_ITERATE (type_binfo, i, binfo); i++)
4078 tree binfo_offset;
4080 if (BINFO_VIRTUAL_P (binfo))
4081 continue;
4083 tree orig_binfo;
4084 /* We cannot rely on BINFO_OFFSET being set for the base
4085 class yet, but the offsets for direct non-virtual
4086 bases can be calculated by going back to the TYPE. */
4087 orig_binfo = BINFO_BASE_BINFO (TYPE_BINFO (type), i);
4088 binfo_offset = size_binop (PLUS_EXPR,
4089 offset,
4090 BINFO_OFFSET (orig_binfo));
4092 r = walk_subobject_offsets (binfo,
4094 binfo_offset,
4095 offsets,
4096 max_offset,
4097 /*vbases_p=*/0);
4098 if (r)
4099 return r;
4102 if (CLASSTYPE_VBASECLASSES (type))
4104 unsigned ix;
4105 vec<tree, va_gc> *vbases;
4107 /* Iterate through the virtual base classes of TYPE. In G++
4108 3.2, we included virtual bases in the direct base class
4109 loop above, which results in incorrect results; the
4110 correct offsets for virtual bases are only known when
4111 working with the most derived type. */
4112 if (vbases_p)
4113 for (vbases = CLASSTYPE_VBASECLASSES (type), ix = 0;
4114 vec_safe_iterate (vbases, ix, &binfo); ix++)
4116 r = walk_subobject_offsets (binfo,
4118 size_binop (PLUS_EXPR,
4119 offset,
4120 BINFO_OFFSET (binfo)),
4121 offsets,
4122 max_offset,
4123 /*vbases_p=*/0);
4124 if (r)
4125 return r;
4127 else
4129 /* We still have to walk the primary base, if it is
4130 virtual. (If it is non-virtual, then it was walked
4131 above.) */
4132 tree vbase = get_primary_binfo (type_binfo);
4134 if (vbase && BINFO_VIRTUAL_P (vbase)
4135 && BINFO_PRIMARY_P (vbase)
4136 && BINFO_INHERITANCE_CHAIN (vbase) == type_binfo)
4138 r = (walk_subobject_offsets
4139 (vbase, f, offset,
4140 offsets, max_offset, /*vbases_p=*/0));
4141 if (r)
4142 return r;
4147 /* Iterate through the fields of TYPE. */
4148 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
4149 if (TREE_CODE (field) == FIELD_DECL
4150 && TREE_TYPE (field) != error_mark_node
4151 && !DECL_ARTIFICIAL (field))
4153 tree field_offset;
4155 field_offset = byte_position (field);
4157 r = walk_subobject_offsets (TREE_TYPE (field),
4159 size_binop (PLUS_EXPR,
4160 offset,
4161 field_offset),
4162 offsets,
4163 max_offset,
4164 /*vbases_p=*/1);
4165 if (r)
4166 return r;
4169 else if (TREE_CODE (type) == ARRAY_TYPE)
4171 tree element_type = strip_array_types (type);
4172 tree domain = TYPE_DOMAIN (type);
4173 tree index;
4175 /* Avoid recursing into objects that are not interesting. */
4176 if (!CLASS_TYPE_P (element_type)
4177 || !CLASSTYPE_CONTAINS_EMPTY_CLASS_P (element_type)
4178 || !domain)
4179 return 0;
4181 /* Step through each of the elements in the array. */
4182 for (index = size_zero_node;
4183 !tree_int_cst_lt (TYPE_MAX_VALUE (domain), index);
4184 index = size_binop (PLUS_EXPR, index, size_one_node))
4186 r = walk_subobject_offsets (TREE_TYPE (type),
4188 offset,
4189 offsets,
4190 max_offset,
4191 /*vbases_p=*/1);
4192 if (r)
4193 return r;
4194 offset = size_binop (PLUS_EXPR, offset,
4195 TYPE_SIZE_UNIT (TREE_TYPE (type)));
4196 /* If this new OFFSET is bigger than the MAX_OFFSET, then
4197 there's no point in iterating through the remaining
4198 elements of the array. */
4199 if (max_offset && tree_int_cst_lt (max_offset, offset))
4200 break;
4204 return 0;
4207 /* Record all of the empty subobjects of TYPE (either a type or a
4208 binfo). If IS_DATA_MEMBER is true, then a non-static data member
4209 is being placed at OFFSET; otherwise, it is a base class that is
4210 being placed at OFFSET. */
4212 static void
4213 record_subobject_offsets (tree type,
4214 tree offset,
4215 splay_tree offsets,
4216 bool is_data_member)
4218 tree max_offset;
4219 /* If recording subobjects for a non-static data member or a
4220 non-empty base class , we do not need to record offsets beyond
4221 the size of the biggest empty class. Additional data members
4222 will go at the end of the class. Additional base classes will go
4223 either at offset zero (if empty, in which case they cannot
4224 overlap with offsets past the size of the biggest empty class) or
4225 at the end of the class.
4227 However, if we are placing an empty base class, then we must record
4228 all offsets, as either the empty class is at offset zero (where
4229 other empty classes might later be placed) or at the end of the
4230 class (where other objects might then be placed, so other empty
4231 subobjects might later overlap). */
4232 if (is_data_member
4233 || !is_empty_class (BINFO_TYPE (type)))
4234 max_offset = sizeof_biggest_empty_class;
4235 else
4236 max_offset = NULL_TREE;
4237 walk_subobject_offsets (type, record_subobject_offset, offset,
4238 offsets, max_offset, is_data_member);
4241 /* Returns nonzero if any of the empty subobjects of TYPE (located at
4242 OFFSET) conflict with entries in OFFSETS. If VBASES_P is nonzero,
4243 virtual bases of TYPE are examined. */
4245 static int
4246 layout_conflict_p (tree type,
4247 tree offset,
4248 splay_tree offsets,
4249 int vbases_p)
4251 splay_tree_node max_node;
4253 /* Get the node in OFFSETS that indicates the maximum offset where
4254 an empty subobject is located. */
4255 max_node = splay_tree_max (offsets);
4256 /* If there aren't any empty subobjects, then there's no point in
4257 performing this check. */
4258 if (!max_node)
4259 return 0;
4261 return walk_subobject_offsets (type, check_subobject_offset, offset,
4262 offsets, (tree) (max_node->key),
4263 vbases_p);
4266 /* DECL is a FIELD_DECL corresponding either to a base subobject of a
4267 non-static data member of the type indicated by RLI. BINFO is the
4268 binfo corresponding to the base subobject, OFFSETS maps offsets to
4269 types already located at those offsets. This function determines
4270 the position of the DECL. */
4272 static void
4273 layout_nonempty_base_or_field (record_layout_info rli,
4274 tree decl,
4275 tree binfo,
4276 splay_tree offsets)
4278 tree offset = NULL_TREE;
4279 bool field_p;
4280 tree type;
4282 if (binfo)
4284 /* For the purposes of determining layout conflicts, we want to
4285 use the class type of BINFO; TREE_TYPE (DECL) will be the
4286 CLASSTYPE_AS_BASE version, which does not contain entries for
4287 zero-sized bases. */
4288 type = TREE_TYPE (binfo);
4289 field_p = false;
4291 else
4293 type = TREE_TYPE (decl);
4294 field_p = true;
4297 /* Try to place the field. It may take more than one try if we have
4298 a hard time placing the field without putting two objects of the
4299 same type at the same address. */
4300 while (1)
4302 struct record_layout_info_s old_rli = *rli;
4304 /* Place this field. */
4305 place_field (rli, decl);
4306 offset = byte_position (decl);
4308 /* We have to check to see whether or not there is already
4309 something of the same type at the offset we're about to use.
4310 For example, consider:
4312 struct S {};
4313 struct T : public S { int i; };
4314 struct U : public S, public T {};
4316 Here, we put S at offset zero in U. Then, we can't put T at
4317 offset zero -- its S component would be at the same address
4318 as the S we already allocated. So, we have to skip ahead.
4319 Since all data members, including those whose type is an
4320 empty class, have nonzero size, any overlap can happen only
4321 with a direct or indirect base-class -- it can't happen with
4322 a data member. */
4323 /* In a union, overlap is permitted; all members are placed at
4324 offset zero. */
4325 if (TREE_CODE (rli->t) == UNION_TYPE)
4326 break;
4327 if (layout_conflict_p (field_p ? type : binfo, offset,
4328 offsets, field_p))
4330 /* Strip off the size allocated to this field. That puts us
4331 at the first place we could have put the field with
4332 proper alignment. */
4333 *rli = old_rli;
4335 /* Bump up by the alignment required for the type. */
4336 rli->bitpos
4337 = size_binop (PLUS_EXPR, rli->bitpos,
4338 bitsize_int (binfo
4339 ? CLASSTYPE_ALIGN (type)
4340 : TYPE_ALIGN (type)));
4341 normalize_rli (rli);
4343 else if (TREE_CODE (type) == NULLPTR_TYPE
4344 && warn_abi && abi_version_crosses (9))
4346 /* Before ABI v9, we were giving nullptr_t alignment of 1; if
4347 the offset wasn't aligned like a pointer when we started to
4348 layout this field, that affects its position. */
4349 tree pos = rli_size_unit_so_far (&old_rli);
4350 if (int_cst_value (pos) % TYPE_ALIGN_UNIT (ptr_type_node) != 0)
4352 if (abi_version_at_least (9))
4353 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wabi,
4354 "alignment of %qD increased in -fabi-version=9 "
4355 "(GCC 5.2)", decl);
4356 else
4357 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wabi, "alignment "
4358 "of %qD will increase in -fabi-version=9", decl);
4360 break;
4362 else
4363 /* There was no conflict. We're done laying out this field. */
4364 break;
4367 /* Now that we know where it will be placed, update its
4368 BINFO_OFFSET. */
4369 if (binfo && CLASS_TYPE_P (BINFO_TYPE (binfo)))
4370 /* Indirect virtual bases may have a nonzero BINFO_OFFSET at
4371 this point because their BINFO_OFFSET is copied from another
4372 hierarchy. Therefore, we may not need to add the entire
4373 OFFSET. */
4374 propagate_binfo_offsets (binfo,
4375 size_diffop_loc (input_location,
4376 fold_convert (ssizetype, offset),
4377 fold_convert (ssizetype,
4378 BINFO_OFFSET (binfo))));
4381 /* Returns true if TYPE is empty and OFFSET is nonzero. */
4383 static int
4384 empty_base_at_nonzero_offset_p (tree type,
4385 tree offset,
4386 splay_tree /*offsets*/)
4388 return is_empty_class (type) && !integer_zerop (offset);
4391 /* Layout the empty base BINFO. EOC indicates the byte currently just
4392 past the end of the class, and should be correctly aligned for a
4393 class of the type indicated by BINFO; OFFSETS gives the offsets of
4394 the empty bases allocated so far. T is the most derived
4395 type. Return nonzero iff we added it at the end. */
4397 static bool
4398 layout_empty_base (record_layout_info rli, tree binfo,
4399 tree eoc, splay_tree offsets)
4401 tree alignment;
4402 tree basetype = BINFO_TYPE (binfo);
4403 bool atend = false;
4405 /* This routine should only be used for empty classes. */
4406 gcc_assert (is_empty_class (basetype));
4407 alignment = ssize_int (CLASSTYPE_ALIGN_UNIT (basetype));
4409 if (!integer_zerop (BINFO_OFFSET (binfo)))
4410 propagate_binfo_offsets
4411 (binfo, size_diffop_loc (input_location,
4412 size_zero_node, BINFO_OFFSET (binfo)));
4414 /* This is an empty base class. We first try to put it at offset
4415 zero. */
4416 if (layout_conflict_p (binfo,
4417 BINFO_OFFSET (binfo),
4418 offsets,
4419 /*vbases_p=*/0))
4421 /* That didn't work. Now, we move forward from the next
4422 available spot in the class. */
4423 atend = true;
4424 propagate_binfo_offsets (binfo, fold_convert (ssizetype, eoc));
4425 while (1)
4427 if (!layout_conflict_p (binfo,
4428 BINFO_OFFSET (binfo),
4429 offsets,
4430 /*vbases_p=*/0))
4431 /* We finally found a spot where there's no overlap. */
4432 break;
4434 /* There's overlap here, too. Bump along to the next spot. */
4435 propagate_binfo_offsets (binfo, alignment);
4439 if (CLASSTYPE_USER_ALIGN (basetype))
4441 rli->record_align = MAX (rli->record_align, CLASSTYPE_ALIGN (basetype));
4442 if (warn_packed)
4443 rli->unpacked_align = MAX (rli->unpacked_align, CLASSTYPE_ALIGN (basetype));
4444 TYPE_USER_ALIGN (rli->t) = 1;
4447 return atend;
4450 /* Layout the base given by BINFO in the class indicated by RLI.
4451 *BASE_ALIGN is a running maximum of the alignments of
4452 any base class. OFFSETS gives the location of empty base
4453 subobjects. T is the most derived type. Return nonzero if the new
4454 object cannot be nearly-empty. A new FIELD_DECL is inserted at
4455 *NEXT_FIELD, unless BINFO is for an empty base class.
4457 Returns the location at which the next field should be inserted. */
4459 static tree *
4460 build_base_field (record_layout_info rli, tree binfo,
4461 splay_tree offsets, tree *next_field)
4463 tree t = rli->t;
4464 tree basetype = BINFO_TYPE (binfo);
4466 if (!COMPLETE_TYPE_P (basetype))
4467 /* This error is now reported in xref_tag, thus giving better
4468 location information. */
4469 return next_field;
4471 /* Place the base class. */
4472 if (!is_empty_class (basetype))
4474 tree decl;
4476 /* The containing class is non-empty because it has a non-empty
4477 base class. */
4478 CLASSTYPE_EMPTY_P (t) = 0;
4480 /* Create the FIELD_DECL. */
4481 decl = build_decl (input_location,
4482 FIELD_DECL, NULL_TREE, CLASSTYPE_AS_BASE (basetype));
4483 DECL_ARTIFICIAL (decl) = 1;
4484 DECL_IGNORED_P (decl) = 1;
4485 DECL_FIELD_CONTEXT (decl) = t;
4486 if (CLASSTYPE_AS_BASE (basetype))
4488 DECL_SIZE (decl) = CLASSTYPE_SIZE (basetype);
4489 DECL_SIZE_UNIT (decl) = CLASSTYPE_SIZE_UNIT (basetype);
4490 SET_DECL_ALIGN (decl, CLASSTYPE_ALIGN (basetype));
4491 DECL_USER_ALIGN (decl) = CLASSTYPE_USER_ALIGN (basetype);
4492 DECL_MODE (decl) = TYPE_MODE (basetype);
4493 DECL_FIELD_IS_BASE (decl) = 1;
4495 /* Try to place the field. It may take more than one try if we
4496 have a hard time placing the field without putting two
4497 objects of the same type at the same address. */
4498 layout_nonempty_base_or_field (rli, decl, binfo, offsets);
4499 /* Add the new FIELD_DECL to the list of fields for T. */
4500 DECL_CHAIN (decl) = *next_field;
4501 *next_field = decl;
4502 next_field = &DECL_CHAIN (decl);
4505 else
4507 tree eoc;
4508 bool atend;
4510 /* On some platforms (ARM), even empty classes will not be
4511 byte-aligned. */
4512 eoc = round_up_loc (input_location,
4513 rli_size_unit_so_far (rli),
4514 CLASSTYPE_ALIGN_UNIT (basetype));
4515 atend = layout_empty_base (rli, binfo, eoc, offsets);
4516 /* A nearly-empty class "has no proper base class that is empty,
4517 not morally virtual, and at an offset other than zero." */
4518 if (!BINFO_VIRTUAL_P (binfo) && CLASSTYPE_NEARLY_EMPTY_P (t))
4520 if (atend)
4521 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
4522 /* The check above (used in G++ 3.2) is insufficient because
4523 an empty class placed at offset zero might itself have an
4524 empty base at a nonzero offset. */
4525 else if (walk_subobject_offsets (basetype,
4526 empty_base_at_nonzero_offset_p,
4527 size_zero_node,
4528 /*offsets=*/NULL,
4529 /*max_offset=*/NULL_TREE,
4530 /*vbases_p=*/true))
4531 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
4534 /* We do not create a FIELD_DECL for empty base classes because
4535 it might overlap some other field. We want to be able to
4536 create CONSTRUCTORs for the class by iterating over the
4537 FIELD_DECLs, and the back end does not handle overlapping
4538 FIELD_DECLs. */
4540 /* An empty virtual base causes a class to be non-empty
4541 -- but in that case we do not need to clear CLASSTYPE_EMPTY_P
4542 here because that was already done when the virtual table
4543 pointer was created. */
4546 /* Record the offsets of BINFO and its base subobjects. */
4547 record_subobject_offsets (binfo,
4548 BINFO_OFFSET (binfo),
4549 offsets,
4550 /*is_data_member=*/false);
4552 return next_field;
4555 /* Layout all of the non-virtual base classes. Record empty
4556 subobjects in OFFSETS. T is the most derived type. Return nonzero
4557 if the type cannot be nearly empty. The fields created
4558 corresponding to the base classes will be inserted at
4559 *NEXT_FIELD. */
4561 static void
4562 build_base_fields (record_layout_info rli,
4563 splay_tree offsets, tree *next_field)
4565 /* Chain to hold all the new FIELD_DECLs which stand in for base class
4566 subobjects. */
4567 tree t = rli->t;
4568 int n_baseclasses = BINFO_N_BASE_BINFOS (TYPE_BINFO (t));
4569 int i;
4571 /* The primary base class is always allocated first. */
4572 if (CLASSTYPE_HAS_PRIMARY_BASE_P (t))
4573 next_field = build_base_field (rli, CLASSTYPE_PRIMARY_BINFO (t),
4574 offsets, next_field);
4576 /* Now allocate the rest of the bases. */
4577 for (i = 0; i < n_baseclasses; ++i)
4579 tree base_binfo;
4581 base_binfo = BINFO_BASE_BINFO (TYPE_BINFO (t), i);
4583 /* The primary base was already allocated above, so we don't
4584 need to allocate it again here. */
4585 if (base_binfo == CLASSTYPE_PRIMARY_BINFO (t))
4586 continue;
4588 /* Virtual bases are added at the end (a primary virtual base
4589 will have already been added). */
4590 if (BINFO_VIRTUAL_P (base_binfo))
4591 continue;
4593 next_field = build_base_field (rli, base_binfo,
4594 offsets, next_field);
4598 /* Go through the TYPE_METHODS of T issuing any appropriate
4599 diagnostics, figuring out which methods override which other
4600 methods, and so forth. */
4602 static void
4603 check_methods (tree t)
4605 tree x;
4607 for (x = TYPE_METHODS (t); x; x = DECL_CHAIN (x))
4609 check_for_override (x, t);
4610 if (DECL_PURE_VIRTUAL_P (x) && (TREE_CODE (x) != FUNCTION_DECL || ! DECL_VINDEX (x)))
4611 error ("initializer specified for non-virtual method %q+D", x);
4612 /* The name of the field is the original field name
4613 Save this in auxiliary field for later overloading. */
4614 if (TREE_CODE (x) == FUNCTION_DECL && DECL_VINDEX (x))
4616 TYPE_POLYMORPHIC_P (t) = 1;
4617 if (DECL_PURE_VIRTUAL_P (x))
4618 vec_safe_push (CLASSTYPE_PURE_VIRTUALS (t), x);
4620 /* All user-provided destructors are non-trivial.
4621 Constructors and assignment ops are handled in
4622 grok_special_member_properties. */
4623 if (DECL_DESTRUCTOR_P (x) && user_provided_p (x))
4624 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t) = 1;
4625 if (!DECL_VIRTUAL_P (x)
4626 && lookup_attribute ("transaction_safe_dynamic", DECL_ATTRIBUTES (x)))
4627 error_at (DECL_SOURCE_LOCATION (x),
4628 "%<transaction_safe_dynamic%> may only be specified for "
4629 "a virtual function");
4633 /* FN is a constructor or destructor. Clone the declaration to create
4634 a specialized in-charge or not-in-charge version, as indicated by
4635 NAME. */
4637 static tree
4638 build_clone (tree fn, tree name)
4640 tree parms;
4641 tree clone;
4643 /* Copy the function. */
4644 clone = copy_decl (fn);
4645 /* Reset the function name. */
4646 DECL_NAME (clone) = name;
4647 /* Remember where this function came from. */
4648 DECL_ABSTRACT_ORIGIN (clone) = fn;
4649 /* Make it easy to find the CLONE given the FN. */
4650 DECL_CHAIN (clone) = DECL_CHAIN (fn);
4651 DECL_CHAIN (fn) = clone;
4653 /* If this is a template, do the rest on the DECL_TEMPLATE_RESULT. */
4654 if (TREE_CODE (clone) == TEMPLATE_DECL)
4656 tree result = build_clone (DECL_TEMPLATE_RESULT (clone), name);
4657 DECL_TEMPLATE_RESULT (clone) = result;
4658 DECL_TEMPLATE_INFO (result) = copy_node (DECL_TEMPLATE_INFO (result));
4659 DECL_TI_TEMPLATE (result) = clone;
4660 TREE_TYPE (clone) = TREE_TYPE (result);
4661 return clone;
4663 else
4665 // Clone constraints.
4666 if (flag_concepts)
4667 if (tree ci = get_constraints (fn))
4668 set_constraints (clone, copy_node (ci));
4672 SET_DECL_ASSEMBLER_NAME (clone, NULL_TREE);
4673 DECL_CLONED_FUNCTION (clone) = fn;
4674 /* There's no pending inline data for this function. */
4675 DECL_PENDING_INLINE_INFO (clone) = NULL;
4676 DECL_PENDING_INLINE_P (clone) = 0;
4678 /* The base-class destructor is not virtual. */
4679 if (name == base_dtor_identifier)
4681 DECL_VIRTUAL_P (clone) = 0;
4682 if (TREE_CODE (clone) != TEMPLATE_DECL)
4683 DECL_VINDEX (clone) = NULL_TREE;
4686 /* If there was an in-charge parameter, drop it from the function
4687 type. */
4688 if (DECL_HAS_IN_CHARGE_PARM_P (clone))
4690 tree basetype;
4691 tree parmtypes;
4692 tree exceptions;
4694 exceptions = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (clone));
4695 basetype = TYPE_METHOD_BASETYPE (TREE_TYPE (clone));
4696 parmtypes = TYPE_ARG_TYPES (TREE_TYPE (clone));
4697 /* Skip the `this' parameter. */
4698 parmtypes = TREE_CHAIN (parmtypes);
4699 /* Skip the in-charge parameter. */
4700 parmtypes = TREE_CHAIN (parmtypes);
4701 /* And the VTT parm, in a complete [cd]tor. */
4702 if (DECL_HAS_VTT_PARM_P (fn)
4703 && ! DECL_NEEDS_VTT_PARM_P (clone))
4704 parmtypes = TREE_CHAIN (parmtypes);
4705 /* If this is subobject constructor or destructor, add the vtt
4706 parameter. */
4707 TREE_TYPE (clone)
4708 = build_method_type_directly (basetype,
4709 TREE_TYPE (TREE_TYPE (clone)),
4710 parmtypes);
4711 if (exceptions)
4712 TREE_TYPE (clone) = build_exception_variant (TREE_TYPE (clone),
4713 exceptions);
4714 TREE_TYPE (clone)
4715 = cp_build_type_attribute_variant (TREE_TYPE (clone),
4716 TYPE_ATTRIBUTES (TREE_TYPE (fn)));
4719 /* Copy the function parameters. */
4720 DECL_ARGUMENTS (clone) = copy_list (DECL_ARGUMENTS (clone));
4721 /* Remove the in-charge parameter. */
4722 if (DECL_HAS_IN_CHARGE_PARM_P (clone))
4724 DECL_CHAIN (DECL_ARGUMENTS (clone))
4725 = DECL_CHAIN (DECL_CHAIN (DECL_ARGUMENTS (clone)));
4726 DECL_HAS_IN_CHARGE_PARM_P (clone) = 0;
4728 /* And the VTT parm, in a complete [cd]tor. */
4729 if (DECL_HAS_VTT_PARM_P (fn))
4731 if (DECL_NEEDS_VTT_PARM_P (clone))
4732 DECL_HAS_VTT_PARM_P (clone) = 1;
4733 else
4735 DECL_CHAIN (DECL_ARGUMENTS (clone))
4736 = DECL_CHAIN (DECL_CHAIN (DECL_ARGUMENTS (clone)));
4737 DECL_HAS_VTT_PARM_P (clone) = 0;
4741 for (parms = DECL_ARGUMENTS (clone); parms; parms = DECL_CHAIN (parms))
4743 DECL_CONTEXT (parms) = clone;
4744 cxx_dup_lang_specific_decl (parms);
4747 /* Create the RTL for this function. */
4748 SET_DECL_RTL (clone, NULL);
4749 rest_of_decl_compilation (clone, /*top_level=*/1, at_eof);
4751 return clone;
4754 /* Implementation of DECL_CLONED_FUNCTION and DECL_CLONED_FUNCTION_P, do
4755 not invoke this function directly.
4757 For a non-thunk function, returns the address of the slot for storing
4758 the function it is a clone of. Otherwise returns NULL_TREE.
4760 If JUST_TESTING, looks through TEMPLATE_DECL and returns NULL if
4761 cloned_function is unset. This is to support the separate
4762 DECL_CLONED_FUNCTION and DECL_CLONED_FUNCTION_P modes; using the latter
4763 on a template makes sense, but not the former. */
4765 tree *
4766 decl_cloned_function_p (const_tree decl, bool just_testing)
4768 tree *ptr;
4769 if (just_testing)
4770 decl = STRIP_TEMPLATE (decl);
4772 if (TREE_CODE (decl) != FUNCTION_DECL
4773 || !DECL_LANG_SPECIFIC (decl)
4774 || DECL_LANG_SPECIFIC (decl)->u.fn.thunk_p)
4776 #if defined ENABLE_TREE_CHECKING && (GCC_VERSION >= 2007)
4777 if (!just_testing)
4778 lang_check_failed (__FILE__, __LINE__, __FUNCTION__);
4779 else
4780 #endif
4781 return NULL;
4784 ptr = &DECL_LANG_SPECIFIC (decl)->u.fn.u5.cloned_function;
4785 if (just_testing && *ptr == NULL_TREE)
4786 return NULL;
4787 else
4788 return ptr;
4791 /* Produce declarations for all appropriate clones of FN. If
4792 UPDATE_METHOD_VEC_P is nonzero, the clones are added to the
4793 CLASTYPE_METHOD_VEC as well. */
4795 void
4796 clone_function_decl (tree fn, int update_method_vec_p)
4798 tree clone;
4800 /* Avoid inappropriate cloning. */
4801 if (DECL_CHAIN (fn)
4802 && DECL_CLONED_FUNCTION_P (DECL_CHAIN (fn)))
4803 return;
4805 if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (fn))
4807 /* For each constructor, we need two variants: an in-charge version
4808 and a not-in-charge version. */
4809 clone = build_clone (fn, complete_ctor_identifier);
4810 if (update_method_vec_p)
4811 add_method (DECL_CONTEXT (clone), clone, NULL_TREE);
4812 clone = build_clone (fn, base_ctor_identifier);
4813 if (update_method_vec_p)
4814 add_method (DECL_CONTEXT (clone), clone, NULL_TREE);
4816 else
4818 gcc_assert (DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (fn));
4820 /* For each destructor, we need three variants: an in-charge
4821 version, a not-in-charge version, and an in-charge deleting
4822 version. We clone the deleting version first because that
4823 means it will go second on the TYPE_METHODS list -- and that
4824 corresponds to the correct layout order in the virtual
4825 function table.
4827 For a non-virtual destructor, we do not build a deleting
4828 destructor. */
4829 if (DECL_VIRTUAL_P (fn))
4831 clone = build_clone (fn, deleting_dtor_identifier);
4832 if (update_method_vec_p)
4833 add_method (DECL_CONTEXT (clone), clone, NULL_TREE);
4835 clone = build_clone (fn, complete_dtor_identifier);
4836 if (update_method_vec_p)
4837 add_method (DECL_CONTEXT (clone), clone, NULL_TREE);
4838 clone = build_clone (fn, base_dtor_identifier);
4839 if (update_method_vec_p)
4840 add_method (DECL_CONTEXT (clone), clone, NULL_TREE);
4843 /* Note that this is an abstract function that is never emitted. */
4844 DECL_ABSTRACT_P (fn) = true;
4847 /* DECL is an in charge constructor, which is being defined. This will
4848 have had an in class declaration, from whence clones were
4849 declared. An out-of-class definition can specify additional default
4850 arguments. As it is the clones that are involved in overload
4851 resolution, we must propagate the information from the DECL to its
4852 clones. */
4854 void
4855 adjust_clone_args (tree decl)
4857 tree clone;
4859 for (clone = DECL_CHAIN (decl); clone && DECL_CLONED_FUNCTION_P (clone);
4860 clone = DECL_CHAIN (clone))
4862 tree orig_clone_parms = TYPE_ARG_TYPES (TREE_TYPE (clone));
4863 tree orig_decl_parms = TYPE_ARG_TYPES (TREE_TYPE (decl));
4864 tree decl_parms, clone_parms;
4866 clone_parms = orig_clone_parms;
4868 /* Skip the 'this' parameter. */
4869 orig_clone_parms = TREE_CHAIN (orig_clone_parms);
4870 orig_decl_parms = TREE_CHAIN (orig_decl_parms);
4872 if (DECL_HAS_IN_CHARGE_PARM_P (decl))
4873 orig_decl_parms = TREE_CHAIN (orig_decl_parms);
4874 if (DECL_HAS_VTT_PARM_P (decl))
4875 orig_decl_parms = TREE_CHAIN (orig_decl_parms);
4877 clone_parms = orig_clone_parms;
4878 if (DECL_HAS_VTT_PARM_P (clone))
4879 clone_parms = TREE_CHAIN (clone_parms);
4881 for (decl_parms = orig_decl_parms; decl_parms;
4882 decl_parms = TREE_CHAIN (decl_parms),
4883 clone_parms = TREE_CHAIN (clone_parms))
4885 gcc_assert (same_type_p (TREE_TYPE (decl_parms),
4886 TREE_TYPE (clone_parms)));
4888 if (TREE_PURPOSE (decl_parms) && !TREE_PURPOSE (clone_parms))
4890 /* A default parameter has been added. Adjust the
4891 clone's parameters. */
4892 tree exceptions = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (clone));
4893 tree attrs = TYPE_ATTRIBUTES (TREE_TYPE (clone));
4894 tree basetype = TYPE_METHOD_BASETYPE (TREE_TYPE (clone));
4895 tree type;
4897 clone_parms = orig_decl_parms;
4899 if (DECL_HAS_VTT_PARM_P (clone))
4901 clone_parms = tree_cons (TREE_PURPOSE (orig_clone_parms),
4902 TREE_VALUE (orig_clone_parms),
4903 clone_parms);
4904 TREE_TYPE (clone_parms) = TREE_TYPE (orig_clone_parms);
4906 type = build_method_type_directly (basetype,
4907 TREE_TYPE (TREE_TYPE (clone)),
4908 clone_parms);
4909 if (exceptions)
4910 type = build_exception_variant (type, exceptions);
4911 if (attrs)
4912 type = cp_build_type_attribute_variant (type, attrs);
4913 TREE_TYPE (clone) = type;
4915 clone_parms = NULL_TREE;
4916 break;
4919 gcc_assert (!clone_parms);
4923 /* For each of the constructors and destructors in T, create an
4924 in-charge and not-in-charge variant. */
4926 static void
4927 clone_constructors_and_destructors (tree t)
4929 tree fns;
4931 /* If for some reason we don't have a CLASSTYPE_METHOD_VEC, we bail
4932 out now. */
4933 if (!CLASSTYPE_METHOD_VEC (t))
4934 return;
4936 for (fns = CLASSTYPE_CONSTRUCTORS (t); fns; fns = OVL_NEXT (fns))
4937 clone_function_decl (OVL_CURRENT (fns), /*update_method_vec_p=*/1);
4938 for (fns = CLASSTYPE_DESTRUCTORS (t); fns; fns = OVL_NEXT (fns))
4939 clone_function_decl (OVL_CURRENT (fns), /*update_method_vec_p=*/1);
4942 /* Deduce noexcept for a destructor DTOR. */
4944 void
4945 deduce_noexcept_on_destructor (tree dtor)
4947 if (!TYPE_RAISES_EXCEPTIONS (TREE_TYPE (dtor)))
4949 tree eh_spec = unevaluated_noexcept_spec ();
4950 TREE_TYPE (dtor) = build_exception_variant (TREE_TYPE (dtor), eh_spec);
4954 /* For each destructor in T, deduce noexcept:
4956 12.4/3: A declaration of a destructor that does not have an
4957 exception-specification is implicitly considered to have the
4958 same exception-specification as an implicit declaration (15.4). */
4960 static void
4961 deduce_noexcept_on_destructors (tree t)
4963 /* If for some reason we don't have a CLASSTYPE_METHOD_VEC, we bail
4964 out now. */
4965 if (!CLASSTYPE_METHOD_VEC (t))
4966 return;
4968 for (tree fns = CLASSTYPE_DESTRUCTORS (t); fns; fns = OVL_NEXT (fns))
4969 deduce_noexcept_on_destructor (OVL_CURRENT (fns));
4972 /* Subroutine of set_one_vmethod_tm_attributes. Search base classes
4973 of TYPE for virtual functions which FNDECL overrides. Return a
4974 mask of the tm attributes found therein. */
4976 static int
4977 look_for_tm_attr_overrides (tree type, tree fndecl)
4979 tree binfo = TYPE_BINFO (type);
4980 tree base_binfo;
4981 int ix, found = 0;
4983 for (ix = 0; BINFO_BASE_ITERATE (binfo, ix, base_binfo); ++ix)
4985 tree o, basetype = BINFO_TYPE (base_binfo);
4987 if (!TYPE_POLYMORPHIC_P (basetype))
4988 continue;
4990 o = look_for_overrides_here (basetype, fndecl);
4991 if (o)
4993 if (lookup_attribute ("transaction_safe_dynamic",
4994 DECL_ATTRIBUTES (o)))
4995 /* transaction_safe_dynamic is not inherited. */;
4996 else
4997 found |= tm_attr_to_mask (find_tm_attribute
4998 (TYPE_ATTRIBUTES (TREE_TYPE (o))));
5000 else
5001 found |= look_for_tm_attr_overrides (basetype, fndecl);
5004 return found;
5007 /* Subroutine of set_method_tm_attributes. Handle the checks and
5008 inheritance for one virtual method FNDECL. */
5010 static void
5011 set_one_vmethod_tm_attributes (tree type, tree fndecl)
5013 tree tm_attr;
5014 int found, have;
5016 found = look_for_tm_attr_overrides (type, fndecl);
5018 /* If FNDECL doesn't actually override anything (i.e. T is the
5019 class that first declares FNDECL virtual), then we're done. */
5020 if (found == 0)
5021 return;
5023 tm_attr = find_tm_attribute (TYPE_ATTRIBUTES (TREE_TYPE (fndecl)));
5024 have = tm_attr_to_mask (tm_attr);
5026 /* Intel STM Language Extension 3.0, Section 4.2 table 4:
5027 tm_pure must match exactly, otherwise no weakening of
5028 tm_safe > tm_callable > nothing. */
5029 /* ??? The tm_pure attribute didn't make the transition to the
5030 multivendor language spec. */
5031 if (have == TM_ATTR_PURE)
5033 if (found != TM_ATTR_PURE)
5035 found &= -found;
5036 goto err_override;
5039 /* If the overridden function is tm_pure, then FNDECL must be. */
5040 else if (found == TM_ATTR_PURE && tm_attr)
5041 goto err_override;
5042 /* Look for base class combinations that cannot be satisfied. */
5043 else if (found != TM_ATTR_PURE && (found & TM_ATTR_PURE))
5045 found &= ~TM_ATTR_PURE;
5046 found &= -found;
5047 error_at (DECL_SOURCE_LOCATION (fndecl),
5048 "method overrides both %<transaction_pure%> and %qE methods",
5049 tm_mask_to_attr (found));
5051 /* If FNDECL did not declare an attribute, then inherit the most
5052 restrictive one. */
5053 else if (tm_attr == NULL)
5055 apply_tm_attr (fndecl, tm_mask_to_attr (found & -found));
5057 /* Otherwise validate that we're not weaker than a function
5058 that is being overridden. */
5059 else
5061 found &= -found;
5062 if (found <= TM_ATTR_CALLABLE && have > found)
5063 goto err_override;
5065 return;
5067 err_override:
5068 error_at (DECL_SOURCE_LOCATION (fndecl),
5069 "method declared %qE overriding %qE method",
5070 tm_attr, tm_mask_to_attr (found));
5073 /* For each of the methods in T, propagate a class-level tm attribute. */
5075 static void
5076 set_method_tm_attributes (tree t)
5078 tree class_tm_attr, fndecl;
5080 /* Don't bother collecting tm attributes if transactional memory
5081 support is not enabled. */
5082 if (!flag_tm)
5083 return;
5085 /* Process virtual methods first, as they inherit directly from the
5086 base virtual function and also require validation of new attributes. */
5087 if (TYPE_CONTAINS_VPTR_P (t))
5089 tree vchain;
5090 for (vchain = BINFO_VIRTUALS (TYPE_BINFO (t)); vchain;
5091 vchain = TREE_CHAIN (vchain))
5093 fndecl = BV_FN (vchain);
5094 if (DECL_THUNK_P (fndecl))
5095 fndecl = THUNK_TARGET (fndecl);
5096 set_one_vmethod_tm_attributes (t, fndecl);
5100 /* If the class doesn't have an attribute, nothing more to do. */
5101 class_tm_attr = find_tm_attribute (TYPE_ATTRIBUTES (t));
5102 if (class_tm_attr == NULL)
5103 return;
5105 /* Any method that does not yet have a tm attribute inherits
5106 the one from the class. */
5107 for (fndecl = TYPE_METHODS (t); fndecl; fndecl = TREE_CHAIN (fndecl))
5109 if (!find_tm_attribute (TYPE_ATTRIBUTES (TREE_TYPE (fndecl))))
5110 apply_tm_attr (fndecl, class_tm_attr);
5114 /* Returns true iff class T has a user-defined constructor other than
5115 the default constructor. */
5117 bool
5118 type_has_user_nondefault_constructor (tree t)
5120 tree fns;
5122 if (!TYPE_HAS_USER_CONSTRUCTOR (t))
5123 return false;
5125 for (fns = CLASSTYPE_CONSTRUCTORS (t); fns; fns = OVL_NEXT (fns))
5127 tree fn = OVL_CURRENT (fns);
5128 if (!DECL_ARTIFICIAL (fn)
5129 && (TREE_CODE (fn) == TEMPLATE_DECL
5130 || (skip_artificial_parms_for (fn, DECL_ARGUMENTS (fn))
5131 != NULL_TREE)))
5132 return true;
5135 return false;
5138 /* Returns the defaulted constructor if T has one. Otherwise, returns
5139 NULL_TREE. */
5141 tree
5142 in_class_defaulted_default_constructor (tree t)
5144 tree fns, args;
5146 if (!TYPE_HAS_USER_CONSTRUCTOR (t))
5147 return NULL_TREE;
5149 for (fns = CLASSTYPE_CONSTRUCTORS (t); fns; fns = OVL_NEXT (fns))
5151 tree fn = OVL_CURRENT (fns);
5153 if (DECL_DEFAULTED_IN_CLASS_P (fn))
5155 args = FUNCTION_FIRST_USER_PARMTYPE (fn);
5156 while (args && TREE_PURPOSE (args))
5157 args = TREE_CHAIN (args);
5158 if (!args || args == void_list_node)
5159 return fn;
5163 return NULL_TREE;
5166 /* Returns true iff FN is a user-provided function, i.e. user-declared
5167 and not defaulted at its first declaration; or explicit, private,
5168 protected, or non-const. */
5170 bool
5171 user_provided_p (tree fn)
5173 if (TREE_CODE (fn) == TEMPLATE_DECL)
5174 return true;
5175 else
5176 return (!DECL_ARTIFICIAL (fn)
5177 && !(DECL_INITIALIZED_IN_CLASS_P (fn)
5178 && (DECL_DEFAULTED_FN (fn) || DECL_DELETED_FN (fn))));
5181 /* Returns true iff class T has a user-provided constructor. */
5183 bool
5184 type_has_user_provided_constructor (tree t)
5186 tree fns;
5188 if (!CLASS_TYPE_P (t))
5189 return false;
5191 if (!TYPE_HAS_USER_CONSTRUCTOR (t))
5192 return false;
5194 /* This can happen in error cases; avoid crashing. */
5195 if (!CLASSTYPE_METHOD_VEC (t))
5196 return false;
5198 for (fns = CLASSTYPE_CONSTRUCTORS (t); fns; fns = OVL_NEXT (fns))
5199 if (user_provided_p (OVL_CURRENT (fns)))
5200 return true;
5202 return false;
5205 /* Returns true iff class T has a user-provided or explicit constructor. */
5207 bool
5208 type_has_user_provided_or_explicit_constructor (tree t)
5210 tree fns;
5212 if (!CLASS_TYPE_P (t))
5213 return false;
5215 if (!TYPE_HAS_USER_CONSTRUCTOR (t))
5216 return false;
5218 /* This can happen in error cases; avoid crashing. */
5219 if (!CLASSTYPE_METHOD_VEC (t))
5220 return false;
5222 for (fns = CLASSTYPE_CONSTRUCTORS (t); fns; fns = OVL_NEXT (fns))
5224 tree fn = OVL_CURRENT (fns);
5225 if (user_provided_p (fn) || DECL_NONCONVERTING_P (fn))
5226 return true;
5229 return false;
5232 /* Returns true iff class T has a non-user-provided (i.e. implicitly
5233 declared or explicitly defaulted in the class body) default
5234 constructor. */
5236 bool
5237 type_has_non_user_provided_default_constructor (tree t)
5239 tree fns;
5241 if (!TYPE_HAS_DEFAULT_CONSTRUCTOR (t))
5242 return false;
5243 if (CLASSTYPE_LAZY_DEFAULT_CTOR (t))
5244 return true;
5246 for (fns = CLASSTYPE_CONSTRUCTORS (t); fns; fns = OVL_NEXT (fns))
5248 tree fn = OVL_CURRENT (fns);
5249 if (TREE_CODE (fn) == FUNCTION_DECL
5250 && !user_provided_p (fn)
5251 && sufficient_parms_p (FUNCTION_FIRST_USER_PARMTYPE (fn)))
5252 return true;
5255 return false;
5258 /* TYPE is being used as a virtual base, and has a non-trivial move
5259 assignment. Return true if this is due to there being a user-provided
5260 move assignment in TYPE or one of its subobjects; if there isn't, then
5261 multiple move assignment can't cause any harm. */
5263 bool
5264 vbase_has_user_provided_move_assign (tree type)
5266 /* Does the type itself have a user-provided move assignment operator? */
5267 for (tree fns
5268 = lookup_fnfields_slot_nolazy (type, ansi_assopname (NOP_EXPR));
5269 fns; fns = OVL_NEXT (fns))
5271 tree fn = OVL_CURRENT (fns);
5272 if (move_fn_p (fn) && user_provided_p (fn))
5273 return true;
5276 /* Do any of its bases? */
5277 tree binfo = TYPE_BINFO (type);
5278 tree base_binfo;
5279 for (int i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
5280 if (vbase_has_user_provided_move_assign (BINFO_TYPE (base_binfo)))
5281 return true;
5283 /* Or non-static data members? */
5284 for (tree field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
5286 if (TREE_CODE (field) == FIELD_DECL
5287 && CLASS_TYPE_P (TREE_TYPE (field))
5288 && vbase_has_user_provided_move_assign (TREE_TYPE (field)))
5289 return true;
5292 /* Seems not. */
5293 return false;
5296 /* If default-initialization leaves part of TYPE uninitialized, returns
5297 a DECL for the field or TYPE itself (DR 253). */
5299 tree
5300 default_init_uninitialized_part (tree type)
5302 tree t, r, binfo;
5303 int i;
5305 type = strip_array_types (type);
5306 if (!CLASS_TYPE_P (type))
5307 return type;
5308 if (!type_has_non_user_provided_default_constructor (type))
5309 return NULL_TREE;
5310 for (binfo = TYPE_BINFO (type), i = 0;
5311 BINFO_BASE_ITERATE (binfo, i, t); ++i)
5313 r = default_init_uninitialized_part (BINFO_TYPE (t));
5314 if (r)
5315 return r;
5317 for (t = TYPE_FIELDS (type); t; t = DECL_CHAIN (t))
5318 if (TREE_CODE (t) == FIELD_DECL
5319 && !DECL_ARTIFICIAL (t)
5320 && !DECL_INITIAL (t))
5322 r = default_init_uninitialized_part (TREE_TYPE (t));
5323 if (r)
5324 return DECL_P (r) ? r : t;
5327 return NULL_TREE;
5330 /* Returns true iff for class T, a trivial synthesized default constructor
5331 would be constexpr. */
5333 bool
5334 trivial_default_constructor_is_constexpr (tree t)
5336 /* A defaulted trivial default constructor is constexpr
5337 if there is nothing to initialize. */
5338 gcc_assert (!TYPE_HAS_COMPLEX_DFLT (t));
5339 return is_really_empty_class (t);
5342 /* Returns true iff class T has a constexpr default constructor. */
5344 bool
5345 type_has_constexpr_default_constructor (tree t)
5347 tree fns;
5349 if (!CLASS_TYPE_P (t))
5351 /* The caller should have stripped an enclosing array. */
5352 gcc_assert (TREE_CODE (t) != ARRAY_TYPE);
5353 return false;
5355 if (CLASSTYPE_LAZY_DEFAULT_CTOR (t))
5357 if (!TYPE_HAS_COMPLEX_DFLT (t))
5358 return trivial_default_constructor_is_constexpr (t);
5359 /* Non-trivial, we need to check subobject constructors. */
5360 lazily_declare_fn (sfk_constructor, t);
5362 fns = locate_ctor (t);
5363 return (fns && DECL_DECLARED_CONSTEXPR_P (fns));
5366 /* Returns true iff class T has a constexpr default constructor or has an
5367 implicitly declared default constructor that we can't tell if it's constexpr
5368 without forcing a lazy declaration (which might cause undesired
5369 instantiations). */
5371 bool
5372 type_maybe_constexpr_default_constructor (tree t)
5374 if (CLASS_TYPE_P (t) && CLASSTYPE_LAZY_DEFAULT_CTOR (t)
5375 && TYPE_HAS_COMPLEX_DFLT (t))
5376 /* Assume it's constexpr. */
5377 return true;
5378 return type_has_constexpr_default_constructor (t);
5381 /* Returns true iff class TYPE has a virtual destructor. */
5383 bool
5384 type_has_virtual_destructor (tree type)
5386 tree dtor;
5388 if (!CLASS_TYPE_P (type))
5389 return false;
5391 gcc_assert (COMPLETE_TYPE_P (type));
5392 dtor = CLASSTYPE_DESTRUCTORS (type);
5393 return (dtor && DECL_VIRTUAL_P (dtor));
5396 /* Returns true iff class T has a move constructor. */
5398 bool
5399 type_has_move_constructor (tree t)
5401 tree fns;
5403 if (CLASSTYPE_LAZY_MOVE_CTOR (t))
5405 gcc_assert (COMPLETE_TYPE_P (t));
5406 lazily_declare_fn (sfk_move_constructor, t);
5409 if (!CLASSTYPE_METHOD_VEC (t))
5410 return false;
5412 for (fns = CLASSTYPE_CONSTRUCTORS (t); fns; fns = OVL_NEXT (fns))
5413 if (move_fn_p (OVL_CURRENT (fns)))
5414 return true;
5416 return false;
5419 /* Returns true iff class T has a move assignment operator. */
5421 bool
5422 type_has_move_assign (tree t)
5424 tree fns;
5426 if (CLASSTYPE_LAZY_MOVE_ASSIGN (t))
5428 gcc_assert (COMPLETE_TYPE_P (t));
5429 lazily_declare_fn (sfk_move_assignment, t);
5432 for (fns = lookup_fnfields_slot_nolazy (t, ansi_assopname (NOP_EXPR));
5433 fns; fns = OVL_NEXT (fns))
5434 if (move_fn_p (OVL_CURRENT (fns)))
5435 return true;
5437 return false;
5440 /* Returns true iff class T has a move constructor that was explicitly
5441 declared in the class body. Note that this is different from
5442 "user-provided", which doesn't include functions that are defaulted in
5443 the class. */
5445 bool
5446 type_has_user_declared_move_constructor (tree t)
5448 tree fns;
5450 if (CLASSTYPE_LAZY_MOVE_CTOR (t))
5451 return false;
5453 if (!CLASSTYPE_METHOD_VEC (t))
5454 return false;
5456 for (fns = CLASSTYPE_CONSTRUCTORS (t); fns; fns = OVL_NEXT (fns))
5458 tree fn = OVL_CURRENT (fns);
5459 if (move_fn_p (fn) && !DECL_ARTIFICIAL (fn))
5460 return true;
5463 return false;
5466 /* Returns true iff class T has a move assignment operator that was
5467 explicitly declared in the class body. */
5469 bool
5470 type_has_user_declared_move_assign (tree t)
5472 tree fns;
5474 if (CLASSTYPE_LAZY_MOVE_ASSIGN (t))
5475 return false;
5477 for (fns = lookup_fnfields_slot_nolazy (t, ansi_assopname (NOP_EXPR));
5478 fns; fns = OVL_NEXT (fns))
5480 tree fn = OVL_CURRENT (fns);
5481 if (move_fn_p (fn) && !DECL_ARTIFICIAL (fn))
5482 return true;
5485 return false;
5488 /* Nonzero if we need to build up a constructor call when initializing an
5489 object of this class, either because it has a user-declared constructor
5490 or because it doesn't have a default constructor (so we need to give an
5491 error if no initializer is provided). Use TYPE_NEEDS_CONSTRUCTING when
5492 what you care about is whether or not an object can be produced by a
5493 constructor (e.g. so we don't set TREE_READONLY on const variables of
5494 such type); use this function when what you care about is whether or not
5495 to try to call a constructor to create an object. The latter case is
5496 the former plus some cases of constructors that cannot be called. */
5498 bool
5499 type_build_ctor_call (tree t)
5501 tree inner;
5502 if (TYPE_NEEDS_CONSTRUCTING (t))
5503 return true;
5504 inner = strip_array_types (t);
5505 if (!CLASS_TYPE_P (inner) || ANON_AGGR_TYPE_P (inner))
5506 return false;
5507 if (!TYPE_HAS_DEFAULT_CONSTRUCTOR (inner))
5508 return true;
5509 if (cxx_dialect < cxx11)
5510 return false;
5511 /* A user-declared constructor might be private, and a constructor might
5512 be trivial but deleted. */
5513 for (tree fns = lookup_fnfields_slot (inner, complete_ctor_identifier);
5514 fns; fns = OVL_NEXT (fns))
5516 tree fn = OVL_CURRENT (fns);
5517 if (!DECL_ARTIFICIAL (fn)
5518 || DECL_DELETED_FN (fn))
5519 return true;
5521 return false;
5524 /* Like type_build_ctor_call, but for destructors. */
5526 bool
5527 type_build_dtor_call (tree t)
5529 tree inner;
5530 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t))
5531 return true;
5532 inner = strip_array_types (t);
5533 if (!CLASS_TYPE_P (inner) || ANON_AGGR_TYPE_P (inner)
5534 || !COMPLETE_TYPE_P (inner))
5535 return false;
5536 if (cxx_dialect < cxx11)
5537 return false;
5538 /* A user-declared destructor might be private, and a destructor might
5539 be trivial but deleted. */
5540 for (tree fns = lookup_fnfields_slot (inner, complete_dtor_identifier);
5541 fns; fns = OVL_NEXT (fns))
5543 tree fn = OVL_CURRENT (fns);
5544 if (!DECL_ARTIFICIAL (fn)
5545 || DECL_DELETED_FN (fn))
5546 return true;
5548 return false;
5551 /* Remove all zero-width bit-fields from T. */
5553 static void
5554 remove_zero_width_bit_fields (tree t)
5556 tree *fieldsp;
5558 fieldsp = &TYPE_FIELDS (t);
5559 while (*fieldsp)
5561 if (TREE_CODE (*fieldsp) == FIELD_DECL
5562 && DECL_C_BIT_FIELD (*fieldsp)
5563 /* We should not be confused by the fact that grokbitfield
5564 temporarily sets the width of the bit field into
5565 DECL_INITIAL (*fieldsp).
5566 check_bitfield_decl eventually sets DECL_SIZE (*fieldsp)
5567 to that width. */
5568 && (DECL_SIZE (*fieldsp) == NULL_TREE
5569 || integer_zerop (DECL_SIZE (*fieldsp))))
5570 *fieldsp = DECL_CHAIN (*fieldsp);
5571 else
5572 fieldsp = &DECL_CHAIN (*fieldsp);
5576 /* Returns TRUE iff we need a cookie when dynamically allocating an
5577 array whose elements have the indicated class TYPE. */
5579 static bool
5580 type_requires_array_cookie (tree type)
5582 tree fns;
5583 bool has_two_argument_delete_p = false;
5585 gcc_assert (CLASS_TYPE_P (type));
5587 /* If there's a non-trivial destructor, we need a cookie. In order
5588 to iterate through the array calling the destructor for each
5589 element, we'll have to know how many elements there are. */
5590 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
5591 return true;
5593 /* If the usual deallocation function is a two-argument whose second
5594 argument is of type `size_t', then we have to pass the size of
5595 the array to the deallocation function, so we will need to store
5596 a cookie. */
5597 fns = lookup_fnfields (TYPE_BINFO (type),
5598 ansi_opname (VEC_DELETE_EXPR),
5599 /*protect=*/0);
5600 /* If there are no `operator []' members, or the lookup is
5601 ambiguous, then we don't need a cookie. */
5602 if (!fns || fns == error_mark_node)
5603 return false;
5604 /* Loop through all of the functions. */
5605 for (fns = BASELINK_FUNCTIONS (fns); fns; fns = OVL_NEXT (fns))
5607 tree fn;
5608 tree second_parm;
5610 /* Select the current function. */
5611 fn = OVL_CURRENT (fns);
5612 /* See if this function is a one-argument delete function. If
5613 it is, then it will be the usual deallocation function. */
5614 second_parm = TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (fn)));
5615 if (second_parm == void_list_node)
5616 return false;
5617 /* Do not consider this function if its second argument is an
5618 ellipsis. */
5619 if (!second_parm)
5620 continue;
5621 /* Otherwise, if we have a two-argument function and the second
5622 argument is `size_t', it will be the usual deallocation
5623 function -- unless there is one-argument function, too. */
5624 if (TREE_CHAIN (second_parm) == void_list_node
5625 && same_type_p (TREE_VALUE (second_parm), size_type_node))
5626 has_two_argument_delete_p = true;
5629 return has_two_argument_delete_p;
5632 /* Finish computing the `literal type' property of class type T.
5634 At this point, we have already processed base classes and
5635 non-static data members. We need to check whether the copy
5636 constructor is trivial, the destructor is trivial, and there
5637 is a trivial default constructor or at least one constexpr
5638 constructor other than the copy constructor. */
5640 static void
5641 finalize_literal_type_property (tree t)
5643 tree fn;
5645 if (cxx_dialect < cxx11
5646 || TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t))
5647 CLASSTYPE_LITERAL_P (t) = false;
5648 else if (CLASSTYPE_LITERAL_P (t) && !TYPE_HAS_TRIVIAL_DFLT (t)
5649 && CLASSTYPE_NON_AGGREGATE (t)
5650 && !TYPE_HAS_CONSTEXPR_CTOR (t))
5651 CLASSTYPE_LITERAL_P (t) = false;
5653 if (!CLASSTYPE_LITERAL_P (t))
5654 for (fn = TYPE_METHODS (t); fn; fn = DECL_CHAIN (fn))
5655 if (DECL_DECLARED_CONSTEXPR_P (fn)
5656 && TREE_CODE (fn) != TEMPLATE_DECL
5657 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
5658 && !DECL_CONSTRUCTOR_P (fn))
5660 DECL_DECLARED_CONSTEXPR_P (fn) = false;
5661 if (!DECL_GENERATED_P (fn))
5663 error ("enclosing class of constexpr non-static member "
5664 "function %q+#D is not a literal type", fn);
5665 explain_non_literal_class (t);
5670 /* T is a non-literal type used in a context which requires a constant
5671 expression. Explain why it isn't literal. */
5673 void
5674 explain_non_literal_class (tree t)
5676 static hash_set<tree> *diagnosed;
5678 if (!CLASS_TYPE_P (t))
5679 return;
5680 t = TYPE_MAIN_VARIANT (t);
5682 if (diagnosed == NULL)
5683 diagnosed = new hash_set<tree>;
5684 if (diagnosed->add (t))
5685 /* Already explained. */
5686 return;
5688 inform (0, "%q+T is not literal because:", t);
5689 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t))
5690 inform (0, " %q+T has a non-trivial destructor", t);
5691 else if (CLASSTYPE_NON_AGGREGATE (t)
5692 && !TYPE_HAS_TRIVIAL_DFLT (t)
5693 && !TYPE_HAS_CONSTEXPR_CTOR (t))
5695 inform (0, " %q+T is not an aggregate, does not have a trivial "
5696 "default constructor, and has no constexpr constructor that "
5697 "is not a copy or move constructor", t);
5698 if (type_has_non_user_provided_default_constructor (t))
5700 /* Note that we can't simply call locate_ctor because when the
5701 constructor is deleted it just returns NULL_TREE. */
5702 tree fns;
5703 for (fns = CLASSTYPE_CONSTRUCTORS (t); fns; fns = OVL_NEXT (fns))
5705 tree fn = OVL_CURRENT (fns);
5706 tree parms = TYPE_ARG_TYPES (TREE_TYPE (fn));
5708 parms = skip_artificial_parms_for (fn, parms);
5710 if (sufficient_parms_p (parms))
5712 if (DECL_DELETED_FN (fn))
5713 maybe_explain_implicit_delete (fn);
5714 else
5715 explain_invalid_constexpr_fn (fn);
5716 break;
5721 else
5723 tree binfo, base_binfo, field; int i;
5724 for (binfo = TYPE_BINFO (t), i = 0;
5725 BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
5727 tree basetype = TREE_TYPE (base_binfo);
5728 if (!CLASSTYPE_LITERAL_P (basetype))
5730 inform (0, " base class %qT of %q+T is non-literal",
5731 basetype, t);
5732 explain_non_literal_class (basetype);
5733 return;
5736 for (field = TYPE_FIELDS (t); field; field = TREE_CHAIN (field))
5738 tree ftype;
5739 if (TREE_CODE (field) != FIELD_DECL)
5740 continue;
5741 ftype = TREE_TYPE (field);
5742 if (!literal_type_p (ftype))
5744 inform (DECL_SOURCE_LOCATION (field),
5745 " non-static data member %qD has non-literal type",
5746 field);
5747 if (CLASS_TYPE_P (ftype))
5748 explain_non_literal_class (ftype);
5750 if (CP_TYPE_VOLATILE_P (ftype))
5751 inform (DECL_SOURCE_LOCATION (field),
5752 " non-static data member %qD has volatile type", field);
5757 /* Check the validity of the bases and members declared in T. Add any
5758 implicitly-generated functions (like copy-constructors and
5759 assignment operators). Compute various flag bits (like
5760 CLASSTYPE_NON_LAYOUT_POD_T) for T. This routine works purely at the C++
5761 level: i.e., independently of the ABI in use. */
5763 static void
5764 check_bases_and_members (tree t)
5766 /* Nonzero if the implicitly generated copy constructor should take
5767 a non-const reference argument. */
5768 int cant_have_const_ctor;
5769 /* Nonzero if the implicitly generated assignment operator
5770 should take a non-const reference argument. */
5771 int no_const_asn_ref;
5772 tree access_decls;
5773 bool saved_complex_asn_ref;
5774 bool saved_nontrivial_dtor;
5775 tree fn;
5777 /* By default, we use const reference arguments and generate default
5778 constructors. */
5779 cant_have_const_ctor = 0;
5780 no_const_asn_ref = 0;
5782 /* Check all the base-classes and set FMEM members to point to arrays
5783 of potential interest. */
5784 check_bases (t, &cant_have_const_ctor, &no_const_asn_ref);
5786 /* Deduce noexcept on destructors. This needs to happen after we've set
5787 triviality flags appropriately for our bases. */
5788 if (cxx_dialect >= cxx11)
5789 deduce_noexcept_on_destructors (t);
5791 /* Check all the method declarations. */
5792 check_methods (t);
5794 /* Save the initial values of these flags which only indicate whether
5795 or not the class has user-provided functions. As we analyze the
5796 bases and members we can set these flags for other reasons. */
5797 saved_complex_asn_ref = TYPE_HAS_COMPLEX_COPY_ASSIGN (t);
5798 saved_nontrivial_dtor = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t);
5800 /* Check all the data member declarations. We cannot call
5801 check_field_decls until we have called check_bases check_methods,
5802 as check_field_decls depends on TYPE_HAS_NONTRIVIAL_DESTRUCTOR
5803 being set appropriately. */
5804 check_field_decls (t, &access_decls,
5805 &cant_have_const_ctor,
5806 &no_const_asn_ref);
5808 /* A nearly-empty class has to be vptr-containing; a nearly empty
5809 class contains just a vptr. */
5810 if (!TYPE_CONTAINS_VPTR_P (t))
5811 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
5813 /* Do some bookkeeping that will guide the generation of implicitly
5814 declared member functions. */
5815 TYPE_HAS_COMPLEX_COPY_CTOR (t) |= TYPE_CONTAINS_VPTR_P (t);
5816 TYPE_HAS_COMPLEX_MOVE_CTOR (t) |= TYPE_CONTAINS_VPTR_P (t);
5817 /* We need to call a constructor for this class if it has a
5818 user-provided constructor, or if the default constructor is going
5819 to initialize the vptr. (This is not an if-and-only-if;
5820 TYPE_NEEDS_CONSTRUCTING is set elsewhere if bases or members
5821 themselves need constructing.) */
5822 TYPE_NEEDS_CONSTRUCTING (t)
5823 |= (type_has_user_provided_constructor (t) || TYPE_CONTAINS_VPTR_P (t));
5824 /* [dcl.init.aggr]
5826 An aggregate is an array or a class with no user-provided
5827 constructors ... and no virtual functions.
5829 Again, other conditions for being an aggregate are checked
5830 elsewhere. */
5831 CLASSTYPE_NON_AGGREGATE (t)
5832 |= (type_has_user_provided_or_explicit_constructor (t)
5833 || TYPE_POLYMORPHIC_P (t));
5834 /* This is the C++98/03 definition of POD; it changed in C++0x, but we
5835 retain the old definition internally for ABI reasons. */
5836 CLASSTYPE_NON_LAYOUT_POD_P (t)
5837 |= (CLASSTYPE_NON_AGGREGATE (t)
5838 || saved_nontrivial_dtor || saved_complex_asn_ref);
5839 CLASSTYPE_NON_STD_LAYOUT (t) |= TYPE_CONTAINS_VPTR_P (t);
5840 TYPE_HAS_COMPLEX_COPY_ASSIGN (t) |= TYPE_CONTAINS_VPTR_P (t);
5841 TYPE_HAS_COMPLEX_MOVE_ASSIGN (t) |= TYPE_CONTAINS_VPTR_P (t);
5842 TYPE_HAS_COMPLEX_DFLT (t) |= TYPE_CONTAINS_VPTR_P (t);
5844 /* If the only explicitly declared default constructor is user-provided,
5845 set TYPE_HAS_COMPLEX_DFLT. */
5846 if (!TYPE_HAS_COMPLEX_DFLT (t)
5847 && TYPE_HAS_DEFAULT_CONSTRUCTOR (t)
5848 && !type_has_non_user_provided_default_constructor (t))
5849 TYPE_HAS_COMPLEX_DFLT (t) = true;
5851 /* Warn if a public base of a polymorphic type has an accessible
5852 non-virtual destructor. It is only now that we know the class is
5853 polymorphic. Although a polymorphic base will have a already
5854 been diagnosed during its definition, we warn on use too. */
5855 if (TYPE_POLYMORPHIC_P (t) && warn_nonvdtor)
5857 tree binfo = TYPE_BINFO (t);
5858 vec<tree, va_gc> *accesses = BINFO_BASE_ACCESSES (binfo);
5859 tree base_binfo;
5860 unsigned i;
5862 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
5864 tree basetype = TREE_TYPE (base_binfo);
5866 if ((*accesses)[i] == access_public_node
5867 && (TYPE_POLYMORPHIC_P (basetype) || warn_ecpp)
5868 && accessible_nvdtor_p (basetype))
5869 warning (OPT_Wnon_virtual_dtor,
5870 "base class %q#T has accessible non-virtual destructor",
5871 basetype);
5875 /* If the class has no user-declared constructor, but does have
5876 non-static const or reference data members that can never be
5877 initialized, issue a warning. */
5878 if (warn_uninitialized
5879 /* Classes with user-declared constructors are presumed to
5880 initialize these members. */
5881 && !TYPE_HAS_USER_CONSTRUCTOR (t)
5882 /* Aggregates can be initialized with brace-enclosed
5883 initializers. */
5884 && CLASSTYPE_NON_AGGREGATE (t))
5886 tree field;
5888 for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
5890 tree type;
5892 if (TREE_CODE (field) != FIELD_DECL
5893 || DECL_INITIAL (field) != NULL_TREE)
5894 continue;
5896 type = TREE_TYPE (field);
5897 if (TREE_CODE (type) == REFERENCE_TYPE)
5898 warning_at (DECL_SOURCE_LOCATION (field),
5899 OPT_Wuninitialized, "non-static reference %q#D "
5900 "in class without a constructor", field);
5901 else if (CP_TYPE_CONST_P (type)
5902 && (!CLASS_TYPE_P (type)
5903 || !TYPE_HAS_DEFAULT_CONSTRUCTOR (type)))
5904 warning_at (DECL_SOURCE_LOCATION (field),
5905 OPT_Wuninitialized, "non-static const member %q#D "
5906 "in class without a constructor", field);
5910 /* Synthesize any needed methods. */
5911 add_implicitly_declared_members (t, &access_decls,
5912 cant_have_const_ctor,
5913 no_const_asn_ref);
5915 /* Check defaulted declarations here so we have cant_have_const_ctor
5916 and don't need to worry about clones. */
5917 for (fn = TYPE_METHODS (t); fn; fn = DECL_CHAIN (fn))
5918 if (!DECL_ARTIFICIAL (fn) && DECL_DEFAULTED_IN_CLASS_P (fn))
5920 int copy = copy_fn_p (fn);
5921 if (copy > 0)
5923 bool imp_const_p
5924 = (DECL_CONSTRUCTOR_P (fn) ? !cant_have_const_ctor
5925 : !no_const_asn_ref);
5926 bool fn_const_p = (copy == 2);
5928 if (fn_const_p && !imp_const_p)
5929 /* If the function is defaulted outside the class, we just
5930 give the synthesis error. */
5931 error ("%q+D declared to take const reference, but implicit "
5932 "declaration would take non-const", fn);
5934 defaulted_late_check (fn);
5937 if (LAMBDA_TYPE_P (t))
5939 /* "This class type is not an aggregate." */
5940 CLASSTYPE_NON_AGGREGATE (t) = 1;
5943 /* Compute the 'literal type' property before we
5944 do anything with non-static member functions. */
5945 finalize_literal_type_property (t);
5947 /* Create the in-charge and not-in-charge variants of constructors
5948 and destructors. */
5949 clone_constructors_and_destructors (t);
5951 /* Process the using-declarations. */
5952 for (; access_decls; access_decls = TREE_CHAIN (access_decls))
5953 handle_using_decl (TREE_VALUE (access_decls), t);
5955 /* Build and sort the CLASSTYPE_METHOD_VEC. */
5956 finish_struct_methods (t);
5958 /* Figure out whether or not we will need a cookie when dynamically
5959 allocating an array of this type. */
5960 TYPE_LANG_SPECIFIC (t)->u.c.vec_new_uses_cookie
5961 = type_requires_array_cookie (t);
5964 /* If T needs a pointer to its virtual function table, set TYPE_VFIELD
5965 accordingly. If a new vfield was created (because T doesn't have a
5966 primary base class), then the newly created field is returned. It
5967 is not added to the TYPE_FIELDS list; it is the caller's
5968 responsibility to do that. Accumulate declared virtual functions
5969 on VIRTUALS_P. */
5971 static tree
5972 create_vtable_ptr (tree t, tree* virtuals_p)
5974 tree fn;
5976 /* Collect the virtual functions declared in T. */
5977 for (fn = TYPE_METHODS (t); fn; fn = DECL_CHAIN (fn))
5978 if (TREE_CODE (fn) == FUNCTION_DECL
5979 && DECL_VINDEX (fn) && !DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (fn)
5980 && TREE_CODE (DECL_VINDEX (fn)) != INTEGER_CST)
5982 tree new_virtual = make_node (TREE_LIST);
5984 BV_FN (new_virtual) = fn;
5985 BV_DELTA (new_virtual) = integer_zero_node;
5986 BV_VCALL_INDEX (new_virtual) = NULL_TREE;
5988 TREE_CHAIN (new_virtual) = *virtuals_p;
5989 *virtuals_p = new_virtual;
5992 /* If we couldn't find an appropriate base class, create a new field
5993 here. Even if there weren't any new virtual functions, we might need a
5994 new virtual function table if we're supposed to include vptrs in
5995 all classes that need them. */
5996 if (!TYPE_VFIELD (t) && (*virtuals_p || TYPE_CONTAINS_VPTR_P (t)))
5998 /* We build this decl with vtbl_ptr_type_node, which is a
5999 `vtable_entry_type*'. It might seem more precise to use
6000 `vtable_entry_type (*)[N]' where N is the number of virtual
6001 functions. However, that would require the vtable pointer in
6002 base classes to have a different type than the vtable pointer
6003 in derived classes. We could make that happen, but that
6004 still wouldn't solve all the problems. In particular, the
6005 type-based alias analysis code would decide that assignments
6006 to the base class vtable pointer can't alias assignments to
6007 the derived class vtable pointer, since they have different
6008 types. Thus, in a derived class destructor, where the base
6009 class constructor was inlined, we could generate bad code for
6010 setting up the vtable pointer.
6012 Therefore, we use one type for all vtable pointers. We still
6013 use a type-correct type; it's just doesn't indicate the array
6014 bounds. That's better than using `void*' or some such; it's
6015 cleaner, and it let's the alias analysis code know that these
6016 stores cannot alias stores to void*! */
6017 tree field;
6019 field = build_decl (input_location,
6020 FIELD_DECL, get_vfield_name (t), vtbl_ptr_type_node);
6021 DECL_VIRTUAL_P (field) = 1;
6022 DECL_ARTIFICIAL (field) = 1;
6023 DECL_FIELD_CONTEXT (field) = t;
6024 DECL_FCONTEXT (field) = t;
6025 if (TYPE_PACKED (t))
6026 DECL_PACKED (field) = 1;
6028 TYPE_VFIELD (t) = field;
6030 /* This class is non-empty. */
6031 CLASSTYPE_EMPTY_P (t) = 0;
6033 return field;
6036 return NULL_TREE;
6039 /* Add OFFSET to all base types of BINFO which is a base in the
6040 hierarchy dominated by T.
6042 OFFSET, which is a type offset, is number of bytes. */
6044 static void
6045 propagate_binfo_offsets (tree binfo, tree offset)
6047 int i;
6048 tree primary_binfo;
6049 tree base_binfo;
6051 /* Update BINFO's offset. */
6052 BINFO_OFFSET (binfo)
6053 = fold_convert (sizetype,
6054 size_binop (PLUS_EXPR,
6055 fold_convert (ssizetype, BINFO_OFFSET (binfo)),
6056 offset));
6058 /* Find the primary base class. */
6059 primary_binfo = get_primary_binfo (binfo);
6061 if (primary_binfo && BINFO_INHERITANCE_CHAIN (primary_binfo) == binfo)
6062 propagate_binfo_offsets (primary_binfo, offset);
6064 /* Scan all of the bases, pushing the BINFO_OFFSET adjust
6065 downwards. */
6066 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
6068 /* Don't do the primary base twice. */
6069 if (base_binfo == primary_binfo)
6070 continue;
6072 if (BINFO_VIRTUAL_P (base_binfo))
6073 continue;
6075 propagate_binfo_offsets (base_binfo, offset);
6079 /* Set BINFO_OFFSET for all of the virtual bases for RLI->T. Update
6080 TYPE_ALIGN and TYPE_SIZE for T. OFFSETS gives the location of
6081 empty subobjects of T. */
6083 static void
6084 layout_virtual_bases (record_layout_info rli, splay_tree offsets)
6086 tree vbase;
6087 tree t = rli->t;
6088 tree *next_field;
6090 if (BINFO_N_BASE_BINFOS (TYPE_BINFO (t)) == 0)
6091 return;
6093 /* Find the last field. The artificial fields created for virtual
6094 bases will go after the last extant field to date. */
6095 next_field = &TYPE_FIELDS (t);
6096 while (*next_field)
6097 next_field = &DECL_CHAIN (*next_field);
6099 /* Go through the virtual bases, allocating space for each virtual
6100 base that is not already a primary base class. These are
6101 allocated in inheritance graph order. */
6102 for (vbase = TYPE_BINFO (t); vbase; vbase = TREE_CHAIN (vbase))
6104 if (!BINFO_VIRTUAL_P (vbase))
6105 continue;
6107 if (!BINFO_PRIMARY_P (vbase))
6109 /* This virtual base is not a primary base of any class in the
6110 hierarchy, so we have to add space for it. */
6111 next_field = build_base_field (rli, vbase,
6112 offsets, next_field);
6117 /* Returns the offset of the byte just past the end of the base class
6118 BINFO. */
6120 static tree
6121 end_of_base (tree binfo)
6123 tree size;
6125 if (!CLASSTYPE_AS_BASE (BINFO_TYPE (binfo)))
6126 size = TYPE_SIZE_UNIT (char_type_node);
6127 else if (is_empty_class (BINFO_TYPE (binfo)))
6128 /* An empty class has zero CLASSTYPE_SIZE_UNIT, but we need to
6129 allocate some space for it. It cannot have virtual bases, so
6130 TYPE_SIZE_UNIT is fine. */
6131 size = TYPE_SIZE_UNIT (BINFO_TYPE (binfo));
6132 else
6133 size = CLASSTYPE_SIZE_UNIT (BINFO_TYPE (binfo));
6135 return size_binop (PLUS_EXPR, BINFO_OFFSET (binfo), size);
6138 /* Returns the offset of the byte just past the end of the base class
6139 with the highest offset in T. If INCLUDE_VIRTUALS_P is zero, then
6140 only non-virtual bases are included. */
6142 static tree
6143 end_of_class (tree t, int include_virtuals_p)
6145 tree result = size_zero_node;
6146 vec<tree, va_gc> *vbases;
6147 tree binfo;
6148 tree base_binfo;
6149 tree offset;
6150 int i;
6152 for (binfo = TYPE_BINFO (t), i = 0;
6153 BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
6155 if (!include_virtuals_p
6156 && BINFO_VIRTUAL_P (base_binfo)
6157 && (!BINFO_PRIMARY_P (base_binfo)
6158 || BINFO_INHERITANCE_CHAIN (base_binfo) != TYPE_BINFO (t)))
6159 continue;
6161 offset = end_of_base (base_binfo);
6162 if (tree_int_cst_lt (result, offset))
6163 result = offset;
6166 if (include_virtuals_p)
6167 for (vbases = CLASSTYPE_VBASECLASSES (t), i = 0;
6168 vec_safe_iterate (vbases, i, &base_binfo); i++)
6170 offset = end_of_base (base_binfo);
6171 if (tree_int_cst_lt (result, offset))
6172 result = offset;
6175 return result;
6178 /* Warn about bases of T that are inaccessible because they are
6179 ambiguous. For example:
6181 struct S {};
6182 struct T : public S {};
6183 struct U : public S, public T {};
6185 Here, `(S*) new U' is not allowed because there are two `S'
6186 subobjects of U. */
6188 static void
6189 warn_about_ambiguous_bases (tree t)
6191 int i;
6192 vec<tree, va_gc> *vbases;
6193 tree basetype;
6194 tree binfo;
6195 tree base_binfo;
6197 /* If there are no repeated bases, nothing can be ambiguous. */
6198 if (!CLASSTYPE_REPEATED_BASE_P (t))
6199 return;
6201 /* Check direct bases. */
6202 for (binfo = TYPE_BINFO (t), i = 0;
6203 BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
6205 basetype = BINFO_TYPE (base_binfo);
6207 if (!uniquely_derived_from_p (basetype, t))
6208 warning (0, "direct base %qT inaccessible in %qT due to ambiguity",
6209 basetype, t);
6212 /* Check for ambiguous virtual bases. */
6213 if (extra_warnings)
6214 for (vbases = CLASSTYPE_VBASECLASSES (t), i = 0;
6215 vec_safe_iterate (vbases, i, &binfo); i++)
6217 basetype = BINFO_TYPE (binfo);
6219 if (!uniquely_derived_from_p (basetype, t))
6220 warning (OPT_Wextra, "virtual base %qT inaccessible in %qT due "
6221 "to ambiguity", basetype, t);
6225 /* Compare two INTEGER_CSTs K1 and K2. */
6227 static int
6228 splay_tree_compare_integer_csts (splay_tree_key k1, splay_tree_key k2)
6230 return tree_int_cst_compare ((tree) k1, (tree) k2);
6233 /* Increase the size indicated in RLI to account for empty classes
6234 that are "off the end" of the class. */
6236 static void
6237 include_empty_classes (record_layout_info rli)
6239 tree eoc;
6240 tree rli_size;
6242 /* It might be the case that we grew the class to allocate a
6243 zero-sized base class. That won't be reflected in RLI, yet,
6244 because we are willing to overlay multiple bases at the same
6245 offset. However, now we need to make sure that RLI is big enough
6246 to reflect the entire class. */
6247 eoc = end_of_class (rli->t,
6248 CLASSTYPE_AS_BASE (rli->t) != NULL_TREE);
6249 rli_size = rli_size_unit_so_far (rli);
6250 if (TREE_CODE (rli_size) == INTEGER_CST
6251 && tree_int_cst_lt (rli_size, eoc))
6253 /* The size should have been rounded to a whole byte. */
6254 gcc_assert (tree_int_cst_equal
6255 (rli->bitpos, round_down (rli->bitpos, BITS_PER_UNIT)));
6256 rli->bitpos
6257 = size_binop (PLUS_EXPR,
6258 rli->bitpos,
6259 size_binop (MULT_EXPR,
6260 fold_convert (bitsizetype,
6261 size_binop (MINUS_EXPR,
6262 eoc, rli_size)),
6263 bitsize_int (BITS_PER_UNIT)));
6264 normalize_rli (rli);
6268 /* Calculate the TYPE_SIZE, TYPE_ALIGN, etc for T. Calculate
6269 BINFO_OFFSETs for all of the base-classes. Position the vtable
6270 pointer. Accumulate declared virtual functions on VIRTUALS_P. */
6272 static void
6273 layout_class_type (tree t, tree *virtuals_p)
6275 tree non_static_data_members;
6276 tree field;
6277 tree vptr;
6278 record_layout_info rli;
6279 /* Maps offsets (represented as INTEGER_CSTs) to a TREE_LIST of
6280 types that appear at that offset. */
6281 splay_tree empty_base_offsets;
6282 /* True if the last field laid out was a bit-field. */
6283 bool last_field_was_bitfield = false;
6284 /* The location at which the next field should be inserted. */
6285 tree *next_field;
6286 /* T, as a base class. */
6287 tree base_t;
6289 /* Keep track of the first non-static data member. */
6290 non_static_data_members = TYPE_FIELDS (t);
6292 /* Start laying out the record. */
6293 rli = start_record_layout (t);
6295 /* Mark all the primary bases in the hierarchy. */
6296 determine_primary_bases (t);
6298 /* Create a pointer to our virtual function table. */
6299 vptr = create_vtable_ptr (t, virtuals_p);
6301 /* The vptr is always the first thing in the class. */
6302 if (vptr)
6304 DECL_CHAIN (vptr) = TYPE_FIELDS (t);
6305 TYPE_FIELDS (t) = vptr;
6306 next_field = &DECL_CHAIN (vptr);
6307 place_field (rli, vptr);
6309 else
6310 next_field = &TYPE_FIELDS (t);
6312 /* Build FIELD_DECLs for all of the non-virtual base-types. */
6313 empty_base_offsets = splay_tree_new (splay_tree_compare_integer_csts,
6314 NULL, NULL);
6315 build_base_fields (rli, empty_base_offsets, next_field);
6317 /* Layout the non-static data members. */
6318 for (field = non_static_data_members; field; field = DECL_CHAIN (field))
6320 tree type;
6321 tree padding;
6323 /* We still pass things that aren't non-static data members to
6324 the back end, in case it wants to do something with them. */
6325 if (TREE_CODE (field) != FIELD_DECL)
6327 place_field (rli, field);
6328 /* If the static data member has incomplete type, keep track
6329 of it so that it can be completed later. (The handling
6330 of pending statics in finish_record_layout is
6331 insufficient; consider:
6333 struct S1;
6334 struct S2 { static S1 s1; };
6336 At this point, finish_record_layout will be called, but
6337 S1 is still incomplete.) */
6338 if (VAR_P (field))
6340 maybe_register_incomplete_var (field);
6341 /* The visibility of static data members is determined
6342 at their point of declaration, not their point of
6343 definition. */
6344 determine_visibility (field);
6346 continue;
6349 type = TREE_TYPE (field);
6350 if (type == error_mark_node)
6351 continue;
6353 padding = NULL_TREE;
6355 /* If this field is a bit-field whose width is greater than its
6356 type, then there are some special rules for allocating
6357 it. */
6358 if (DECL_C_BIT_FIELD (field)
6359 && tree_int_cst_lt (TYPE_SIZE (type), DECL_SIZE (field)))
6361 unsigned int itk;
6362 tree integer_type;
6363 bool was_unnamed_p = false;
6364 /* We must allocate the bits as if suitably aligned for the
6365 longest integer type that fits in this many bits. type
6366 of the field. Then, we are supposed to use the left over
6367 bits as additional padding. */
6368 for (itk = itk_char; itk != itk_none; ++itk)
6369 if (integer_types[itk] != NULL_TREE
6370 && (tree_int_cst_lt (size_int (MAX_FIXED_MODE_SIZE),
6371 TYPE_SIZE (integer_types[itk]))
6372 || tree_int_cst_lt (DECL_SIZE (field),
6373 TYPE_SIZE (integer_types[itk]))))
6374 break;
6376 /* ITK now indicates a type that is too large for the
6377 field. We have to back up by one to find the largest
6378 type that fits. */
6381 --itk;
6382 integer_type = integer_types[itk];
6383 } while (itk > 0 && integer_type == NULL_TREE);
6385 /* Figure out how much additional padding is required. */
6386 if (tree_int_cst_lt (TYPE_SIZE (integer_type), DECL_SIZE (field)))
6388 if (TREE_CODE (t) == UNION_TYPE)
6389 /* In a union, the padding field must have the full width
6390 of the bit-field; all fields start at offset zero. */
6391 padding = DECL_SIZE (field);
6392 else
6393 padding = size_binop (MINUS_EXPR, DECL_SIZE (field),
6394 TYPE_SIZE (integer_type));
6397 /* An unnamed bitfield does not normally affect the
6398 alignment of the containing class on a target where
6399 PCC_BITFIELD_TYPE_MATTERS. But, the C++ ABI does not
6400 make any exceptions for unnamed bitfields when the
6401 bitfields are longer than their types. Therefore, we
6402 temporarily give the field a name. */
6403 if (PCC_BITFIELD_TYPE_MATTERS && !DECL_NAME (field))
6405 was_unnamed_p = true;
6406 DECL_NAME (field) = make_anon_name ();
6409 DECL_SIZE (field) = TYPE_SIZE (integer_type);
6410 SET_DECL_ALIGN (field, TYPE_ALIGN (integer_type));
6411 DECL_USER_ALIGN (field) = TYPE_USER_ALIGN (integer_type);
6412 layout_nonempty_base_or_field (rli, field, NULL_TREE,
6413 empty_base_offsets);
6414 if (was_unnamed_p)
6415 DECL_NAME (field) = NULL_TREE;
6416 /* Now that layout has been performed, set the size of the
6417 field to the size of its declared type; the rest of the
6418 field is effectively invisible. */
6419 DECL_SIZE (field) = TYPE_SIZE (type);
6420 /* We must also reset the DECL_MODE of the field. */
6421 DECL_MODE (field) = TYPE_MODE (type);
6423 else
6424 layout_nonempty_base_or_field (rli, field, NULL_TREE,
6425 empty_base_offsets);
6427 /* Remember the location of any empty classes in FIELD. */
6428 record_subobject_offsets (TREE_TYPE (field),
6429 byte_position(field),
6430 empty_base_offsets,
6431 /*is_data_member=*/true);
6433 /* If a bit-field does not immediately follow another bit-field,
6434 and yet it starts in the middle of a byte, we have failed to
6435 comply with the ABI. */
6436 if (warn_abi
6437 && DECL_C_BIT_FIELD (field)
6438 /* The TREE_NO_WARNING flag gets set by Objective-C when
6439 laying out an Objective-C class. The ObjC ABI differs
6440 from the C++ ABI, and so we do not want a warning
6441 here. */
6442 && !TREE_NO_WARNING (field)
6443 && !last_field_was_bitfield
6444 && !integer_zerop (size_binop (TRUNC_MOD_EXPR,
6445 DECL_FIELD_BIT_OFFSET (field),
6446 bitsize_unit_node)))
6447 warning_at (DECL_SOURCE_LOCATION (field), OPT_Wabi,
6448 "offset of %qD is not ABI-compliant and may "
6449 "change in a future version of GCC", field);
6451 /* The middle end uses the type of expressions to determine the
6452 possible range of expression values. In order to optimize
6453 "x.i > 7" to "false" for a 2-bit bitfield "i", the middle end
6454 must be made aware of the width of "i", via its type.
6456 Because C++ does not have integer types of arbitrary width,
6457 we must (for the purposes of the front end) convert from the
6458 type assigned here to the declared type of the bitfield
6459 whenever a bitfield expression is used as an rvalue.
6460 Similarly, when assigning a value to a bitfield, the value
6461 must be converted to the type given the bitfield here. */
6462 if (DECL_C_BIT_FIELD (field))
6464 unsigned HOST_WIDE_INT width;
6465 tree ftype = TREE_TYPE (field);
6466 width = tree_to_uhwi (DECL_SIZE (field));
6467 if (width != TYPE_PRECISION (ftype))
6469 TREE_TYPE (field)
6470 = c_build_bitfield_integer_type (width,
6471 TYPE_UNSIGNED (ftype));
6472 TREE_TYPE (field)
6473 = cp_build_qualified_type (TREE_TYPE (field),
6474 cp_type_quals (ftype));
6478 /* If we needed additional padding after this field, add it
6479 now. */
6480 if (padding)
6482 tree padding_field;
6484 padding_field = build_decl (input_location,
6485 FIELD_DECL,
6486 NULL_TREE,
6487 char_type_node);
6488 DECL_BIT_FIELD (padding_field) = 1;
6489 DECL_SIZE (padding_field) = padding;
6490 DECL_CONTEXT (padding_field) = t;
6491 DECL_ARTIFICIAL (padding_field) = 1;
6492 DECL_IGNORED_P (padding_field) = 1;
6493 layout_nonempty_base_or_field (rli, padding_field,
6494 NULL_TREE,
6495 empty_base_offsets);
6498 last_field_was_bitfield = DECL_C_BIT_FIELD (field);
6501 if (!integer_zerop (rli->bitpos))
6503 /* Make sure that we are on a byte boundary so that the size of
6504 the class without virtual bases will always be a round number
6505 of bytes. */
6506 rli->bitpos = round_up_loc (input_location, rli->bitpos, BITS_PER_UNIT);
6507 normalize_rli (rli);
6510 /* Delete all zero-width bit-fields from the list of fields. Now
6511 that the type is laid out they are no longer important. */
6512 remove_zero_width_bit_fields (t);
6514 /* Create the version of T used for virtual bases. We do not use
6515 make_class_type for this version; this is an artificial type. For
6516 a POD type, we just reuse T. */
6517 if (CLASSTYPE_NON_LAYOUT_POD_P (t) || CLASSTYPE_EMPTY_P (t))
6519 base_t = make_node (TREE_CODE (t));
6521 /* Set the size and alignment for the new type. */
6522 tree eoc;
6524 /* If the ABI version is not at least two, and the last
6525 field was a bit-field, RLI may not be on a byte
6526 boundary. In particular, rli_size_unit_so_far might
6527 indicate the last complete byte, while rli_size_so_far
6528 indicates the total number of bits used. Therefore,
6529 rli_size_so_far, rather than rli_size_unit_so_far, is
6530 used to compute TYPE_SIZE_UNIT. */
6531 eoc = end_of_class (t, /*include_virtuals_p=*/0);
6532 TYPE_SIZE_UNIT (base_t)
6533 = size_binop (MAX_EXPR,
6534 fold_convert (sizetype,
6535 size_binop (CEIL_DIV_EXPR,
6536 rli_size_so_far (rli),
6537 bitsize_int (BITS_PER_UNIT))),
6538 eoc);
6539 TYPE_SIZE (base_t)
6540 = size_binop (MAX_EXPR,
6541 rli_size_so_far (rli),
6542 size_binop (MULT_EXPR,
6543 fold_convert (bitsizetype, eoc),
6544 bitsize_int (BITS_PER_UNIT)));
6545 SET_TYPE_ALIGN (base_t, rli->record_align);
6546 TYPE_USER_ALIGN (base_t) = TYPE_USER_ALIGN (t);
6548 /* Copy the fields from T. */
6549 next_field = &TYPE_FIELDS (base_t);
6550 for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
6551 if (TREE_CODE (field) == FIELD_DECL)
6553 *next_field = copy_node (field);
6554 DECL_CONTEXT (*next_field) = base_t;
6555 next_field = &DECL_CHAIN (*next_field);
6557 *next_field = NULL_TREE;
6559 /* Record the base version of the type. */
6560 CLASSTYPE_AS_BASE (t) = base_t;
6561 TYPE_CONTEXT (base_t) = t;
6563 else
6564 CLASSTYPE_AS_BASE (t) = t;
6566 /* Every empty class contains an empty class. */
6567 if (CLASSTYPE_EMPTY_P (t))
6568 CLASSTYPE_CONTAINS_EMPTY_CLASS_P (t) = 1;
6570 /* Set the TYPE_DECL for this type to contain the right
6571 value for DECL_OFFSET, so that we can use it as part
6572 of a COMPONENT_REF for multiple inheritance. */
6573 layout_decl (TYPE_MAIN_DECL (t), 0);
6575 /* Now fix up any virtual base class types that we left lying
6576 around. We must get these done before we try to lay out the
6577 virtual function table. As a side-effect, this will remove the
6578 base subobject fields. */
6579 layout_virtual_bases (rli, empty_base_offsets);
6581 /* Make sure that empty classes are reflected in RLI at this
6582 point. */
6583 include_empty_classes(rli);
6585 /* Make sure not to create any structures with zero size. */
6586 if (integer_zerop (rli_size_unit_so_far (rli)) && CLASSTYPE_EMPTY_P (t))
6587 place_field (rli,
6588 build_decl (input_location,
6589 FIELD_DECL, NULL_TREE, char_type_node));
6591 /* If this is a non-POD, declaring it packed makes a difference to how it
6592 can be used as a field; don't let finalize_record_size undo it. */
6593 if (TYPE_PACKED (t) && !layout_pod_type_p (t))
6594 rli->packed_maybe_necessary = true;
6596 /* Let the back end lay out the type. */
6597 finish_record_layout (rli, /*free_p=*/true);
6599 if (TYPE_SIZE_UNIT (t)
6600 && TREE_CODE (TYPE_SIZE_UNIT (t)) == INTEGER_CST
6601 && !TREE_OVERFLOW (TYPE_SIZE_UNIT (t))
6602 && !valid_constant_size_p (TYPE_SIZE_UNIT (t)))
6603 error ("size of type %qT is too large (%qE bytes)", t, TYPE_SIZE_UNIT (t));
6605 /* Warn about bases that can't be talked about due to ambiguity. */
6606 warn_about_ambiguous_bases (t);
6608 /* Now that we're done with layout, give the base fields the real types. */
6609 for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
6610 if (DECL_ARTIFICIAL (field) && IS_FAKE_BASE_TYPE (TREE_TYPE (field)))
6611 TREE_TYPE (field) = TYPE_CONTEXT (TREE_TYPE (field));
6613 /* Clean up. */
6614 splay_tree_delete (empty_base_offsets);
6616 if (CLASSTYPE_EMPTY_P (t)
6617 && tree_int_cst_lt (sizeof_biggest_empty_class,
6618 TYPE_SIZE_UNIT (t)))
6619 sizeof_biggest_empty_class = TYPE_SIZE_UNIT (t);
6622 /* Determine the "key method" for the class type indicated by TYPE,
6623 and set CLASSTYPE_KEY_METHOD accordingly. */
6625 void
6626 determine_key_method (tree type)
6628 tree method;
6630 if (TYPE_FOR_JAVA (type)
6631 || processing_template_decl
6632 || CLASSTYPE_TEMPLATE_INSTANTIATION (type)
6633 || CLASSTYPE_INTERFACE_KNOWN (type))
6634 return;
6636 /* The key method is the first non-pure virtual function that is not
6637 inline at the point of class definition. On some targets the
6638 key function may not be inline; those targets should not call
6639 this function until the end of the translation unit. */
6640 for (method = TYPE_METHODS (type); method != NULL_TREE;
6641 method = DECL_CHAIN (method))
6642 if (TREE_CODE (method) == FUNCTION_DECL
6643 && DECL_VINDEX (method) != NULL_TREE
6644 && ! DECL_DECLARED_INLINE_P (method)
6645 && ! DECL_PURE_VIRTUAL_P (method))
6647 CLASSTYPE_KEY_METHOD (type) = method;
6648 break;
6651 return;
6655 /* Allocate and return an instance of struct sorted_fields_type with
6656 N fields. */
6658 static struct sorted_fields_type *
6659 sorted_fields_type_new (int n)
6661 struct sorted_fields_type *sft;
6662 sft = (sorted_fields_type *) ggc_internal_alloc (sizeof (sorted_fields_type)
6663 + n * sizeof (tree));
6664 sft->len = n;
6666 return sft;
6669 /* Helper of find_flexarrays. Return true when FLD refers to a non-static
6670 class data member of non-zero size, otherwise false. */
6672 static inline bool
6673 field_nonempty_p (const_tree fld)
6675 if (TREE_CODE (fld) == ERROR_MARK)
6676 return false;
6678 tree type = TREE_TYPE (fld);
6679 if (TREE_CODE (fld) == FIELD_DECL
6680 && TREE_CODE (type) != ERROR_MARK
6681 && (DECL_NAME (fld) || RECORD_OR_UNION_TYPE_P (type)))
6683 return TYPE_SIZE (type)
6684 && (TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST
6685 || !tree_int_cst_equal (size_zero_node, TYPE_SIZE (type)));
6688 return false;
6691 /* Used by find_flexarrays and related. */
6692 struct flexmems_t {
6693 /* The first flexible array member or non-zero array member found
6694 in order of layout. */
6695 tree array;
6696 /* First non-static non-empty data member in the class or its bases. */
6697 tree first;
6698 /* First non-static non-empty data member following either the flexible
6699 array member, if found, or the zero-length array member. */
6700 tree after;
6703 /* Find either the first flexible array member or the first zero-length
6704 array, in that order or preference, among members of class T (but not
6705 its base classes), and set members of FMEM accordingly. */
6707 static void
6708 find_flexarrays (tree t, flexmems_t *fmem)
6710 for (tree fld = TYPE_FIELDS (t), next; fld; fld = next)
6712 /* Find the next non-static data member if it exists. */
6713 for (next = fld;
6714 (next = DECL_CHAIN (next))
6715 && TREE_CODE (next) != FIELD_DECL; );
6717 tree fldtype = TREE_TYPE (fld);
6718 if (TREE_CODE (fld) != TYPE_DECL
6719 && RECORD_OR_UNION_TYPE_P (fldtype)
6720 && TYPE_ANONYMOUS_P (fldtype))
6722 /* Members of anonymous structs and unions are treated as if
6723 they were members of the containing class. Descend into
6724 the anonymous struct or union and find a flexible array
6725 member or zero-length array among its fields. */
6726 find_flexarrays (fldtype, fmem);
6727 continue;
6730 /* Skip anything that's not a (non-static) data member. */
6731 if (TREE_CODE (fld) != FIELD_DECL)
6732 continue;
6734 /* Skip virtual table pointers. */
6735 if (DECL_ARTIFICIAL (fld))
6736 continue;
6738 if (field_nonempty_p (fld))
6740 /* Remember the first non-static data member. */
6741 if (!fmem->first)
6742 fmem->first = fld;
6744 /* Remember the first non-static data member after the flexible
6745 array member, if one has been found, or the zero-length array
6746 if it has been found. */
6747 if (!fmem->after && fmem->array)
6748 fmem->after = fld;
6751 /* Skip non-arrays. */
6752 if (TREE_CODE (fldtype) != ARRAY_TYPE)
6753 continue;
6755 /* Determine the upper bound of the array if it has one. */
6756 if (TYPE_DOMAIN (fldtype))
6758 if (fmem->array)
6760 /* Make a record of the zero-length array if either one
6761 such field or a flexible array member has been seen to
6762 handle the pathological and unlikely case of multiple
6763 such members. */
6764 if (!fmem->after)
6765 fmem->after = fld;
6767 else if (integer_all_onesp (TYPE_MAX_VALUE (TYPE_DOMAIN (fldtype))))
6768 /* Remember the first zero-length array unless a flexible array
6769 member has already been seen. */
6770 fmem->array = fld;
6772 else
6774 /* Flexible array members have no upper bound. */
6775 if (fmem->array)
6777 /* Replace the zero-length array if it's been stored and
6778 reset the after pointer. */
6779 if (TYPE_DOMAIN (TREE_TYPE (fmem->array)))
6781 fmem->array = fld;
6782 fmem->after = NULL_TREE;
6785 else
6786 fmem->array = fld;
6791 /* Issue diagnostics for invalid flexible array members or zero-length
6792 arrays that are not the last elements of the containing class or its
6793 base classes or that are its sole members. */
6795 static void
6796 diagnose_flexarrays (tree t, const flexmems_t *fmem)
6798 /* Members of anonymous structs and unions are considered to be members
6799 of the containing struct or union. */
6800 if (TYPE_ANONYMOUS_P (t) || !fmem->array)
6801 return;
6803 const char *msg = 0;
6805 if (TYPE_DOMAIN (TREE_TYPE (fmem->array)))
6807 if (fmem->after)
6808 msg = G_("zero-size array member %qD not at end of %q#T");
6809 else if (!fmem->first)
6810 msg = G_("zero-size array member %qD in an otherwise empty %q#T");
6812 if (msg && pedwarn (DECL_SOURCE_LOCATION (fmem->array),
6813 OPT_Wpedantic, msg, fmem->array, t))
6815 inform (location_of (t), "in the definition of %q#T", t);
6817 else
6819 if (fmem->after)
6820 msg = G_("flexible array member %qD not at end of %q#T");
6821 else if (!fmem->first)
6822 msg = G_("flexible array member %qD in an otherwise empty %q#T");
6824 if (msg)
6826 error_at (DECL_SOURCE_LOCATION (fmem->array), msg,
6827 fmem->array, t);
6829 /* In the unlikely event that the member following the flexible
6830 array member is declared in a different class, point to it.
6831 Otherwise it should be obvious. */
6832 if (fmem->after
6833 && (DECL_CONTEXT (fmem->after) != DECL_CONTEXT (fmem->array)))
6834 inform (DECL_SOURCE_LOCATION (fmem->after),
6835 "next member %q#D declared here",
6836 fmem->after);
6838 inform (location_of (t), "in the definition of %q#T", t);
6844 /* Recursively check to make sure that any flexible array or zero-length
6845 array members of class T or its bases are valid (i.e., not the sole
6846 non-static data member of T and, if one exists, that it is the last
6847 non-static data member of T and its base classes. FMEM is expected
6848 to be initially null and is used internally by recursive calls to
6849 the function. Issue the appropriate diagnostics for the array member
6850 that fails the checks. */
6852 static void
6853 check_flexarrays (tree t, flexmems_t *fmem /* = NULL */)
6855 /* Initialize the result of a search for flexible array and zero-length
6856 array members. Avoid doing any work if the most interesting FMEM data
6857 have already been populated. */
6858 flexmems_t flexmems = flexmems_t ();
6859 if (!fmem)
6860 fmem = &flexmems;
6861 else if (fmem->array && fmem->first && fmem->after)
6862 return;
6864 /* Recursively check the primary base class first. */
6865 if (CLASSTYPE_HAS_PRIMARY_BASE_P (t))
6867 tree basetype = BINFO_TYPE (CLASSTYPE_PRIMARY_BINFO (t));
6868 check_flexarrays (basetype, fmem);
6871 /* Recursively check the base classes. */
6872 int nbases = BINFO_N_BASE_BINFOS (TYPE_BINFO (t));
6873 for (int i = 0; i < nbases; ++i)
6875 tree base_binfo = BINFO_BASE_BINFO (TYPE_BINFO (t), i);
6877 /* The primary base class was already checked above. */
6878 if (base_binfo == CLASSTYPE_PRIMARY_BINFO (t))
6879 continue;
6881 /* Virtual base classes are at the end. */
6882 if (BINFO_VIRTUAL_P (base_binfo))
6883 continue;
6885 /* Check the base class. */
6886 check_flexarrays (BINFO_TYPE (base_binfo), fmem);
6889 if (fmem == &flexmems)
6891 /* Check virtual base classes only once per derived class.
6892 I.e., this check is not performed recursively for base
6893 classes. */
6894 int i;
6895 tree base_binfo;
6896 vec<tree, va_gc> *vbases;
6897 for (vbases = CLASSTYPE_VBASECLASSES (t), i = 0;
6898 vec_safe_iterate (vbases, i, &base_binfo); i++)
6900 /* Check the virtual base class. */
6901 tree basetype = TREE_TYPE (base_binfo);
6903 check_flexarrays (basetype, fmem);
6907 /* Search the members of the current (derived) class. */
6908 find_flexarrays (t, fmem);
6910 if (fmem == &flexmems)
6912 /* Issue diagnostics for invalid flexible and zero-length array members
6913 found in base classes or among the members of the current class. */
6914 diagnose_flexarrays (t, fmem);
6918 /* Perform processing required when the definition of T (a class type)
6919 is complete. Diagnose invalid definitions of flexible array members
6920 and zero-size arrays. */
6922 void
6923 finish_struct_1 (tree t)
6925 tree x;
6926 /* A TREE_LIST. The TREE_VALUE of each node is a FUNCTION_DECL. */
6927 tree virtuals = NULL_TREE;
6929 if (COMPLETE_TYPE_P (t))
6931 gcc_assert (MAYBE_CLASS_TYPE_P (t));
6932 error ("redefinition of %q#T", t);
6933 popclass ();
6934 return;
6937 /* If this type was previously laid out as a forward reference,
6938 make sure we lay it out again. */
6939 TYPE_SIZE (t) = NULL_TREE;
6940 CLASSTYPE_PRIMARY_BINFO (t) = NULL_TREE;
6942 /* Make assumptions about the class; we'll reset the flags if
6943 necessary. */
6944 CLASSTYPE_EMPTY_P (t) = 1;
6945 CLASSTYPE_NEARLY_EMPTY_P (t) = 1;
6946 CLASSTYPE_CONTAINS_EMPTY_CLASS_P (t) = 0;
6947 CLASSTYPE_LITERAL_P (t) = true;
6949 /* Do end-of-class semantic processing: checking the validity of the
6950 bases and members and add implicitly generated methods. */
6951 check_bases_and_members (t);
6953 /* Find the key method. */
6954 if (TYPE_CONTAINS_VPTR_P (t))
6956 /* The Itanium C++ ABI permits the key method to be chosen when
6957 the class is defined -- even though the key method so
6958 selected may later turn out to be an inline function. On
6959 some systems (such as ARM Symbian OS) the key method cannot
6960 be determined until the end of the translation unit. On such
6961 systems, we leave CLASSTYPE_KEY_METHOD set to NULL, which
6962 will cause the class to be added to KEYED_CLASSES. Then, in
6963 finish_file we will determine the key method. */
6964 if (targetm.cxx.key_method_may_be_inline ())
6965 determine_key_method (t);
6967 /* If a polymorphic class has no key method, we may emit the vtable
6968 in every translation unit where the class definition appears. If
6969 we're devirtualizing, we can look into the vtable even if we
6970 aren't emitting it. */
6971 if (CLASSTYPE_KEY_METHOD (t) == NULL_TREE)
6972 keyed_classes = tree_cons (NULL_TREE, t, keyed_classes);
6975 /* Layout the class itself. */
6976 layout_class_type (t, &virtuals);
6977 if (CLASSTYPE_AS_BASE (t) != t)
6978 /* We use the base type for trivial assignments, and hence it
6979 needs a mode. */
6980 compute_record_mode (CLASSTYPE_AS_BASE (t));
6982 /* With the layout complete, check for flexible array members and
6983 zero-length arrays that might overlap other members in the final
6984 layout. */
6985 check_flexarrays (t);
6987 virtuals = modify_all_vtables (t, nreverse (virtuals));
6989 /* If necessary, create the primary vtable for this class. */
6990 if (virtuals || TYPE_CONTAINS_VPTR_P (t))
6992 /* We must enter these virtuals into the table. */
6993 if (!CLASSTYPE_HAS_PRIMARY_BASE_P (t))
6994 build_primary_vtable (NULL_TREE, t);
6995 else if (! BINFO_NEW_VTABLE_MARKED (TYPE_BINFO (t)))
6996 /* Here we know enough to change the type of our virtual
6997 function table, but we will wait until later this function. */
6998 build_primary_vtable (CLASSTYPE_PRIMARY_BINFO (t), t);
7000 /* If we're warning about ABI tags, check the types of the new
7001 virtual functions. */
7002 if (warn_abi_tag)
7003 for (tree v = virtuals; v; v = TREE_CHAIN (v))
7004 check_abi_tags (t, TREE_VALUE (v));
7007 if (TYPE_CONTAINS_VPTR_P (t))
7009 int vindex;
7010 tree fn;
7012 if (BINFO_VTABLE (TYPE_BINFO (t)))
7013 gcc_assert (DECL_VIRTUAL_P (BINFO_VTABLE (TYPE_BINFO (t))));
7014 if (!CLASSTYPE_HAS_PRIMARY_BASE_P (t))
7015 gcc_assert (BINFO_VIRTUALS (TYPE_BINFO (t)) == NULL_TREE);
7017 /* Add entries for virtual functions introduced by this class. */
7018 BINFO_VIRTUALS (TYPE_BINFO (t))
7019 = chainon (BINFO_VIRTUALS (TYPE_BINFO (t)), virtuals);
7021 /* Set DECL_VINDEX for all functions declared in this class. */
7022 for (vindex = 0, fn = BINFO_VIRTUALS (TYPE_BINFO (t));
7024 fn = TREE_CHAIN (fn),
7025 vindex += (TARGET_VTABLE_USES_DESCRIPTORS
7026 ? TARGET_VTABLE_USES_DESCRIPTORS : 1))
7028 tree fndecl = BV_FN (fn);
7030 if (DECL_THUNK_P (fndecl))
7031 /* A thunk. We should never be calling this entry directly
7032 from this vtable -- we'd use the entry for the non
7033 thunk base function. */
7034 DECL_VINDEX (fndecl) = NULL_TREE;
7035 else if (TREE_CODE (DECL_VINDEX (fndecl)) != INTEGER_CST)
7036 DECL_VINDEX (fndecl) = build_int_cst (NULL_TREE, vindex);
7040 finish_struct_bits (t);
7041 set_method_tm_attributes (t);
7042 if (flag_openmp || flag_openmp_simd)
7043 finish_omp_declare_simd_methods (t);
7045 /* Complete the rtl for any static member objects of the type we're
7046 working on. */
7047 for (x = TYPE_FIELDS (t); x; x = DECL_CHAIN (x))
7048 if (VAR_P (x) && TREE_STATIC (x)
7049 && TREE_TYPE (x) != error_mark_node
7050 && same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (x)), t))
7051 DECL_MODE (x) = TYPE_MODE (t);
7053 /* Done with FIELDS...now decide whether to sort these for
7054 faster lookups later.
7056 We use a small number because most searches fail (succeeding
7057 ultimately as the search bores through the inheritance
7058 hierarchy), and we want this failure to occur quickly. */
7060 insert_into_classtype_sorted_fields (TYPE_FIELDS (t), t, 8);
7062 /* Complain if one of the field types requires lower visibility. */
7063 constrain_class_visibility (t);
7065 /* Make the rtl for any new vtables we have created, and unmark
7066 the base types we marked. */
7067 finish_vtbls (t);
7069 /* Build the VTT for T. */
7070 build_vtt (t);
7072 /* This warning does not make sense for Java classes, since they
7073 cannot have destructors. */
7074 if (!TYPE_FOR_JAVA (t) && warn_nonvdtor
7075 && TYPE_POLYMORPHIC_P (t) && accessible_nvdtor_p (t)
7076 && !CLASSTYPE_FINAL (t))
7077 warning (OPT_Wnon_virtual_dtor,
7078 "%q#T has virtual functions and accessible"
7079 " non-virtual destructor", t);
7081 complete_vars (t);
7083 if (warn_overloaded_virtual)
7084 warn_hidden (t);
7086 /* Class layout, assignment of virtual table slots, etc., is now
7087 complete. Give the back end a chance to tweak the visibility of
7088 the class or perform any other required target modifications. */
7089 targetm.cxx.adjust_class_at_definition (t);
7091 maybe_suppress_debug_info (t);
7093 if (flag_vtable_verify)
7094 vtv_save_class_info (t);
7096 dump_class_hierarchy (t);
7098 /* Finish debugging output for this type. */
7099 rest_of_type_compilation (t, ! LOCAL_CLASS_P (t));
7101 if (TYPE_TRANSPARENT_AGGR (t))
7103 tree field = first_field (t);
7104 if (field == NULL_TREE || error_operand_p (field))
7106 error ("type transparent %q#T does not have any fields", t);
7107 TYPE_TRANSPARENT_AGGR (t) = 0;
7109 else if (DECL_ARTIFICIAL (field))
7111 if (DECL_FIELD_IS_BASE (field))
7112 error ("type transparent class %qT has base classes", t);
7113 else
7115 gcc_checking_assert (DECL_VIRTUAL_P (field));
7116 error ("type transparent class %qT has virtual functions", t);
7118 TYPE_TRANSPARENT_AGGR (t) = 0;
7120 else if (TYPE_MODE (t) != DECL_MODE (field))
7122 error ("type transparent %q#T cannot be made transparent because "
7123 "the type of the first field has a different ABI from the "
7124 "class overall", t);
7125 TYPE_TRANSPARENT_AGGR (t) = 0;
7130 /* Insert FIELDS into T for the sorted case if the FIELDS count is
7131 equal to THRESHOLD or greater than THRESHOLD. */
7133 static void
7134 insert_into_classtype_sorted_fields (tree fields, tree t, int threshold)
7136 int n_fields = count_fields (fields);
7137 if (n_fields >= threshold)
7139 struct sorted_fields_type *field_vec = sorted_fields_type_new (n_fields);
7140 add_fields_to_record_type (fields, field_vec, 0);
7141 qsort (field_vec->elts, n_fields, sizeof (tree), field_decl_cmp);
7142 CLASSTYPE_SORTED_FIELDS (t) = field_vec;
7146 /* Insert lately defined enum ENUMTYPE into T for the sorted case. */
7148 void
7149 insert_late_enum_def_into_classtype_sorted_fields (tree enumtype, tree t)
7151 struct sorted_fields_type *sorted_fields = CLASSTYPE_SORTED_FIELDS (t);
7152 if (sorted_fields)
7154 int i;
7155 int n_fields
7156 = list_length (TYPE_VALUES (enumtype)) + sorted_fields->len;
7157 struct sorted_fields_type *field_vec = sorted_fields_type_new (n_fields);
7159 for (i = 0; i < sorted_fields->len; ++i)
7160 field_vec->elts[i] = sorted_fields->elts[i];
7162 add_enum_fields_to_record_type (enumtype, field_vec,
7163 sorted_fields->len);
7164 qsort (field_vec->elts, n_fields, sizeof (tree), field_decl_cmp);
7165 CLASSTYPE_SORTED_FIELDS (t) = field_vec;
7169 /* When T was built up, the member declarations were added in reverse
7170 order. Rearrange them to declaration order. */
7172 void
7173 unreverse_member_declarations (tree t)
7175 tree next;
7176 tree prev;
7177 tree x;
7179 /* The following lists are all in reverse order. Put them in
7180 declaration order now. */
7181 TYPE_METHODS (t) = nreverse (TYPE_METHODS (t));
7182 CLASSTYPE_DECL_LIST (t) = nreverse (CLASSTYPE_DECL_LIST (t));
7184 /* Actually, for the TYPE_FIELDS, only the non TYPE_DECLs are in
7185 reverse order, so we can't just use nreverse. */
7186 prev = NULL_TREE;
7187 for (x = TYPE_FIELDS (t);
7188 x && TREE_CODE (x) != TYPE_DECL;
7189 x = next)
7191 next = DECL_CHAIN (x);
7192 DECL_CHAIN (x) = prev;
7193 prev = x;
7195 if (prev)
7197 DECL_CHAIN (TYPE_FIELDS (t)) = x;
7198 if (prev)
7199 TYPE_FIELDS (t) = prev;
7203 tree
7204 finish_struct (tree t, tree attributes)
7206 location_t saved_loc = input_location;
7208 /* Now that we've got all the field declarations, reverse everything
7209 as necessary. */
7210 unreverse_member_declarations (t);
7212 cplus_decl_attributes (&t, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
7213 fixup_attribute_variants (t);
7215 /* Nadger the current location so that diagnostics point to the start of
7216 the struct, not the end. */
7217 input_location = DECL_SOURCE_LOCATION (TYPE_NAME (t));
7219 if (processing_template_decl)
7221 tree x;
7223 finish_struct_methods (t);
7224 TYPE_SIZE (t) = bitsize_zero_node;
7225 TYPE_SIZE_UNIT (t) = size_zero_node;
7227 /* We need to emit an error message if this type was used as a parameter
7228 and it is an abstract type, even if it is a template. We construct
7229 a simple CLASSTYPE_PURE_VIRTUALS list without taking bases into
7230 account and we call complete_vars with this type, which will check
7231 the PARM_DECLS. Note that while the type is being defined,
7232 CLASSTYPE_PURE_VIRTUALS contains the list of the inline friends
7233 (see CLASSTYPE_INLINE_FRIENDS) so we need to clear it. */
7234 CLASSTYPE_PURE_VIRTUALS (t) = NULL;
7235 for (x = TYPE_METHODS (t); x; x = DECL_CHAIN (x))
7236 if (DECL_PURE_VIRTUAL_P (x))
7237 vec_safe_push (CLASSTYPE_PURE_VIRTUALS (t), x);
7238 complete_vars (t);
7239 /* We need to add the target functions to the CLASSTYPE_METHOD_VEC if
7240 an enclosing scope is a template class, so that this function be
7241 found by lookup_fnfields_1 when the using declaration is not
7242 instantiated yet. */
7243 for (x = TYPE_FIELDS (t); x; x = DECL_CHAIN (x))
7244 if (TREE_CODE (x) == USING_DECL)
7246 tree fn = strip_using_decl (x);
7247 if (is_overloaded_fn (fn))
7248 for (; fn; fn = OVL_NEXT (fn))
7249 add_method (t, OVL_CURRENT (fn), x);
7252 /* Remember current #pragma pack value. */
7253 TYPE_PRECISION (t) = maximum_field_alignment;
7255 /* Fix up any variants we've already built. */
7256 for (x = TYPE_NEXT_VARIANT (t); x; x = TYPE_NEXT_VARIANT (x))
7258 TYPE_SIZE (x) = TYPE_SIZE (t);
7259 TYPE_SIZE_UNIT (x) = TYPE_SIZE_UNIT (t);
7260 TYPE_FIELDS (x) = TYPE_FIELDS (t);
7261 TYPE_METHODS (x) = TYPE_METHODS (t);
7264 else
7265 finish_struct_1 (t);
7267 if (is_std_init_list (t))
7269 /* People keep complaining that the compiler crashes on an invalid
7270 definition of initializer_list, so I guess we should explicitly
7271 reject it. What the compiler internals care about is that it's a
7272 template and has a pointer field followed by an integer field. */
7273 bool ok = false;
7274 if (processing_template_decl)
7276 tree f = next_initializable_field (TYPE_FIELDS (t));
7277 if (f && TREE_CODE (TREE_TYPE (f)) == POINTER_TYPE)
7279 f = next_initializable_field (DECL_CHAIN (f));
7280 if (f && same_type_p (TREE_TYPE (f), size_type_node))
7281 ok = true;
7284 if (!ok)
7285 fatal_error (input_location,
7286 "definition of std::initializer_list does not match "
7287 "#include <initializer_list>");
7290 input_location = saved_loc;
7292 TYPE_BEING_DEFINED (t) = 0;
7294 if (current_class_type)
7295 popclass ();
7296 else
7297 error ("trying to finish struct, but kicked out due to previous parse errors");
7299 if (processing_template_decl && at_function_scope_p ()
7300 /* Lambdas are defined by the LAMBDA_EXPR. */
7301 && !LAMBDA_TYPE_P (t))
7302 add_stmt (build_min (TAG_DEFN, t));
7304 return t;
7307 /* Hash table to avoid endless recursion when handling references. */
7308 static hash_table<nofree_ptr_hash<tree_node> > *fixed_type_or_null_ref_ht;
7310 /* Return the dynamic type of INSTANCE, if known.
7311 Used to determine whether the virtual function table is needed
7312 or not.
7314 *NONNULL is set iff INSTANCE can be known to be nonnull, regardless
7315 of our knowledge of its type. *NONNULL should be initialized
7316 before this function is called. */
7318 static tree
7319 fixed_type_or_null (tree instance, int *nonnull, int *cdtorp)
7321 #define RECUR(T) fixed_type_or_null((T), nonnull, cdtorp)
7323 switch (TREE_CODE (instance))
7325 case INDIRECT_REF:
7326 if (POINTER_TYPE_P (TREE_TYPE (instance)))
7327 return NULL_TREE;
7328 else
7329 return RECUR (TREE_OPERAND (instance, 0));
7331 case CALL_EXPR:
7332 /* This is a call to a constructor, hence it's never zero. */
7333 if (TREE_HAS_CONSTRUCTOR (instance))
7335 if (nonnull)
7336 *nonnull = 1;
7337 return TREE_TYPE (instance);
7339 return NULL_TREE;
7341 case SAVE_EXPR:
7342 /* This is a call to a constructor, hence it's never zero. */
7343 if (TREE_HAS_CONSTRUCTOR (instance))
7345 if (nonnull)
7346 *nonnull = 1;
7347 return TREE_TYPE (instance);
7349 return RECUR (TREE_OPERAND (instance, 0));
7351 case POINTER_PLUS_EXPR:
7352 case PLUS_EXPR:
7353 case MINUS_EXPR:
7354 if (TREE_CODE (TREE_OPERAND (instance, 0)) == ADDR_EXPR)
7355 return RECUR (TREE_OPERAND (instance, 0));
7356 if (TREE_CODE (TREE_OPERAND (instance, 1)) == INTEGER_CST)
7357 /* Propagate nonnull. */
7358 return RECUR (TREE_OPERAND (instance, 0));
7360 return NULL_TREE;
7362 CASE_CONVERT:
7363 return RECUR (TREE_OPERAND (instance, 0));
7365 case ADDR_EXPR:
7366 instance = TREE_OPERAND (instance, 0);
7367 if (nonnull)
7369 /* Just because we see an ADDR_EXPR doesn't mean we're dealing
7370 with a real object -- given &p->f, p can still be null. */
7371 tree t = get_base_address (instance);
7372 /* ??? Probably should check DECL_WEAK here. */
7373 if (t && DECL_P (t))
7374 *nonnull = 1;
7376 return RECUR (instance);
7378 case COMPONENT_REF:
7379 /* If this component is really a base class reference, then the field
7380 itself isn't definitive. */
7381 if (DECL_FIELD_IS_BASE (TREE_OPERAND (instance, 1)))
7382 return RECUR (TREE_OPERAND (instance, 0));
7383 return RECUR (TREE_OPERAND (instance, 1));
7385 case VAR_DECL:
7386 case FIELD_DECL:
7387 if (TREE_CODE (TREE_TYPE (instance)) == ARRAY_TYPE
7388 && MAYBE_CLASS_TYPE_P (TREE_TYPE (TREE_TYPE (instance))))
7390 if (nonnull)
7391 *nonnull = 1;
7392 return TREE_TYPE (TREE_TYPE (instance));
7394 /* fall through... */
7395 case TARGET_EXPR:
7396 case PARM_DECL:
7397 case RESULT_DECL:
7398 if (MAYBE_CLASS_TYPE_P (TREE_TYPE (instance)))
7400 if (nonnull)
7401 *nonnull = 1;
7402 return TREE_TYPE (instance);
7404 else if (instance == current_class_ptr)
7406 if (nonnull)
7407 *nonnull = 1;
7409 /* if we're in a ctor or dtor, we know our type. If
7410 current_class_ptr is set but we aren't in a function, we're in
7411 an NSDMI (and therefore a constructor). */
7412 if (current_scope () != current_function_decl
7413 || (DECL_LANG_SPECIFIC (current_function_decl)
7414 && (DECL_CONSTRUCTOR_P (current_function_decl)
7415 || DECL_DESTRUCTOR_P (current_function_decl))))
7417 if (cdtorp)
7418 *cdtorp = 1;
7419 return TREE_TYPE (TREE_TYPE (instance));
7422 else if (TREE_CODE (TREE_TYPE (instance)) == REFERENCE_TYPE)
7424 /* We only need one hash table because it is always left empty. */
7425 if (!fixed_type_or_null_ref_ht)
7426 fixed_type_or_null_ref_ht
7427 = new hash_table<nofree_ptr_hash<tree_node> > (37);
7429 /* Reference variables should be references to objects. */
7430 if (nonnull)
7431 *nonnull = 1;
7433 /* Enter the INSTANCE in a table to prevent recursion; a
7434 variable's initializer may refer to the variable
7435 itself. */
7436 if (VAR_P (instance)
7437 && DECL_INITIAL (instance)
7438 && !type_dependent_expression_p_push (DECL_INITIAL (instance))
7439 && !fixed_type_or_null_ref_ht->find (instance))
7441 tree type;
7442 tree_node **slot;
7444 slot = fixed_type_or_null_ref_ht->find_slot (instance, INSERT);
7445 *slot = instance;
7446 type = RECUR (DECL_INITIAL (instance));
7447 fixed_type_or_null_ref_ht->remove_elt (instance);
7449 return type;
7452 return NULL_TREE;
7454 default:
7455 return NULL_TREE;
7457 #undef RECUR
7460 /* Return nonzero if the dynamic type of INSTANCE is known, and
7461 equivalent to the static type. We also handle the case where
7462 INSTANCE is really a pointer. Return negative if this is a
7463 ctor/dtor. There the dynamic type is known, but this might not be
7464 the most derived base of the original object, and hence virtual
7465 bases may not be laid out according to this type.
7467 Used to determine whether the virtual function table is needed
7468 or not.
7470 *NONNULL is set iff INSTANCE can be known to be nonnull, regardless
7471 of our knowledge of its type. *NONNULL should be initialized
7472 before this function is called. */
7475 resolves_to_fixed_type_p (tree instance, int* nonnull)
7477 tree t = TREE_TYPE (instance);
7478 int cdtorp = 0;
7479 tree fixed;
7481 /* processing_template_decl can be false in a template if we're in
7482 instantiate_non_dependent_expr, but we still want to suppress
7483 this check. */
7484 if (in_template_function ())
7486 /* In a template we only care about the type of the result. */
7487 if (nonnull)
7488 *nonnull = true;
7489 return true;
7492 fixed = fixed_type_or_null (instance, nonnull, &cdtorp);
7493 if (fixed == NULL_TREE)
7494 return 0;
7495 if (POINTER_TYPE_P (t))
7496 t = TREE_TYPE (t);
7497 if (!same_type_ignoring_top_level_qualifiers_p (t, fixed))
7498 return 0;
7499 return cdtorp ? -1 : 1;
7503 void
7504 init_class_processing (void)
7506 current_class_depth = 0;
7507 current_class_stack_size = 10;
7508 current_class_stack
7509 = XNEWVEC (struct class_stack_node, current_class_stack_size);
7510 vec_alloc (local_classes, 8);
7511 sizeof_biggest_empty_class = size_zero_node;
7513 ridpointers[(int) RID_PUBLIC] = access_public_node;
7514 ridpointers[(int) RID_PRIVATE] = access_private_node;
7515 ridpointers[(int) RID_PROTECTED] = access_protected_node;
7518 /* Restore the cached PREVIOUS_CLASS_LEVEL. */
7520 static void
7521 restore_class_cache (void)
7523 tree type;
7525 /* We are re-entering the same class we just left, so we don't
7526 have to search the whole inheritance matrix to find all the
7527 decls to bind again. Instead, we install the cached
7528 class_shadowed list and walk through it binding names. */
7529 push_binding_level (previous_class_level);
7530 class_binding_level = previous_class_level;
7531 /* Restore IDENTIFIER_TYPE_VALUE. */
7532 for (type = class_binding_level->type_shadowed;
7533 type;
7534 type = TREE_CHAIN (type))
7535 SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (type), TREE_TYPE (type));
7538 /* Set global variables CURRENT_CLASS_NAME and CURRENT_CLASS_TYPE as
7539 appropriate for TYPE.
7541 So that we may avoid calls to lookup_name, we cache the _TYPE
7542 nodes of local TYPE_DECLs in the TREE_TYPE field of the name.
7544 For multiple inheritance, we perform a two-pass depth-first search
7545 of the type lattice. */
7547 void
7548 pushclass (tree type)
7550 class_stack_node_t csn;
7552 type = TYPE_MAIN_VARIANT (type);
7554 /* Make sure there is enough room for the new entry on the stack. */
7555 if (current_class_depth + 1 >= current_class_stack_size)
7557 current_class_stack_size *= 2;
7558 current_class_stack
7559 = XRESIZEVEC (struct class_stack_node, current_class_stack,
7560 current_class_stack_size);
7563 /* Insert a new entry on the class stack. */
7564 csn = current_class_stack + current_class_depth;
7565 csn->name = current_class_name;
7566 csn->type = current_class_type;
7567 csn->access = current_access_specifier;
7568 csn->names_used = 0;
7569 csn->hidden = 0;
7570 current_class_depth++;
7572 /* Now set up the new type. */
7573 current_class_name = TYPE_NAME (type);
7574 if (TREE_CODE (current_class_name) == TYPE_DECL)
7575 current_class_name = DECL_NAME (current_class_name);
7576 current_class_type = type;
7578 /* By default, things in classes are private, while things in
7579 structures or unions are public. */
7580 current_access_specifier = (CLASSTYPE_DECLARED_CLASS (type)
7581 ? access_private_node
7582 : access_public_node);
7584 if (previous_class_level
7585 && type != previous_class_level->this_entity
7586 && current_class_depth == 1)
7588 /* Forcibly remove any old class remnants. */
7589 invalidate_class_lookup_cache ();
7592 if (!previous_class_level
7593 || type != previous_class_level->this_entity
7594 || current_class_depth > 1)
7595 pushlevel_class ();
7596 else
7597 restore_class_cache ();
7600 /* When we exit a toplevel class scope, we save its binding level so
7601 that we can restore it quickly. Here, we've entered some other
7602 class, so we must invalidate our cache. */
7604 void
7605 invalidate_class_lookup_cache (void)
7607 previous_class_level = NULL;
7610 /* Get out of the current class scope. If we were in a class scope
7611 previously, that is the one popped to. */
7613 void
7614 popclass (void)
7616 poplevel_class ();
7618 current_class_depth--;
7619 current_class_name = current_class_stack[current_class_depth].name;
7620 current_class_type = current_class_stack[current_class_depth].type;
7621 current_access_specifier = current_class_stack[current_class_depth].access;
7622 if (current_class_stack[current_class_depth].names_used)
7623 splay_tree_delete (current_class_stack[current_class_depth].names_used);
7626 /* Mark the top of the class stack as hidden. */
7628 void
7629 push_class_stack (void)
7631 if (current_class_depth)
7632 ++current_class_stack[current_class_depth - 1].hidden;
7635 /* Mark the top of the class stack as un-hidden. */
7637 void
7638 pop_class_stack (void)
7640 if (current_class_depth)
7641 --current_class_stack[current_class_depth - 1].hidden;
7644 /* Returns 1 if the class type currently being defined is either T or
7645 a nested type of T. Returns the type from the current_class_stack,
7646 which might be equivalent to but not equal to T in case of
7647 constrained partial specializations. */
7649 tree
7650 currently_open_class (tree t)
7652 int i;
7654 if (!CLASS_TYPE_P (t))
7655 return NULL_TREE;
7657 t = TYPE_MAIN_VARIANT (t);
7659 /* We start looking from 1 because entry 0 is from global scope,
7660 and has no type. */
7661 for (i = current_class_depth; i > 0; --i)
7663 tree c;
7664 if (i == current_class_depth)
7665 c = current_class_type;
7666 else
7668 if (current_class_stack[i].hidden)
7669 break;
7670 c = current_class_stack[i].type;
7672 if (!c)
7673 continue;
7674 if (same_type_p (c, t))
7675 return c;
7677 return NULL_TREE;
7680 /* If either current_class_type or one of its enclosing classes are derived
7681 from T, return the appropriate type. Used to determine how we found
7682 something via unqualified lookup. */
7684 tree
7685 currently_open_derived_class (tree t)
7687 int i;
7689 /* The bases of a dependent type are unknown. */
7690 if (dependent_type_p (t))
7691 return NULL_TREE;
7693 if (!current_class_type)
7694 return NULL_TREE;
7696 if (DERIVED_FROM_P (t, current_class_type))
7697 return current_class_type;
7699 for (i = current_class_depth - 1; i > 0; --i)
7701 if (current_class_stack[i].hidden)
7702 break;
7703 if (DERIVED_FROM_P (t, current_class_stack[i].type))
7704 return current_class_stack[i].type;
7707 return NULL_TREE;
7710 /* Return the outermost enclosing class type that is still open, or
7711 NULL_TREE. */
7713 tree
7714 outermost_open_class (void)
7716 if (!current_class_type)
7717 return NULL_TREE;
7718 tree r = NULL_TREE;
7719 if (TYPE_BEING_DEFINED (current_class_type))
7720 r = current_class_type;
7721 for (int i = current_class_depth - 1; i > 0; --i)
7723 if (current_class_stack[i].hidden)
7724 break;
7725 tree t = current_class_stack[i].type;
7726 if (!TYPE_BEING_DEFINED (t))
7727 break;
7728 r = t;
7730 return r;
7733 /* Returns the innermost class type which is not a lambda closure type. */
7735 tree
7736 current_nonlambda_class_type (void)
7738 int i;
7740 /* We start looking from 1 because entry 0 is from global scope,
7741 and has no type. */
7742 for (i = current_class_depth; i > 0; --i)
7744 tree c;
7745 if (i == current_class_depth)
7746 c = current_class_type;
7747 else
7749 if (current_class_stack[i].hidden)
7750 break;
7751 c = current_class_stack[i].type;
7753 if (!c)
7754 continue;
7755 if (!LAMBDA_TYPE_P (c))
7756 return c;
7758 return NULL_TREE;
7761 /* When entering a class scope, all enclosing class scopes' names with
7762 static meaning (static variables, static functions, types and
7763 enumerators) have to be visible. This recursive function calls
7764 pushclass for all enclosing class contexts until global or a local
7765 scope is reached. TYPE is the enclosed class. */
7767 void
7768 push_nested_class (tree type)
7770 /* A namespace might be passed in error cases, like A::B:C. */
7771 if (type == NULL_TREE
7772 || !CLASS_TYPE_P (type))
7773 return;
7775 push_nested_class (DECL_CONTEXT (TYPE_MAIN_DECL (type)));
7777 pushclass (type);
7780 /* Undoes a push_nested_class call. */
7782 void
7783 pop_nested_class (void)
7785 tree context = DECL_CONTEXT (TYPE_MAIN_DECL (current_class_type));
7787 popclass ();
7788 if (context && CLASS_TYPE_P (context))
7789 pop_nested_class ();
7792 /* Returns the number of extern "LANG" blocks we are nested within. */
7795 current_lang_depth (void)
7797 return vec_safe_length (current_lang_base);
7800 /* Set global variables CURRENT_LANG_NAME to appropriate value
7801 so that behavior of name-mangling machinery is correct. */
7803 void
7804 push_lang_context (tree name)
7806 vec_safe_push (current_lang_base, current_lang_name);
7808 if (name == lang_name_cplusplus)
7810 current_lang_name = name;
7812 else if (name == lang_name_java)
7814 current_lang_name = name;
7815 /* DECL_IGNORED_P is initially set for these types, to avoid clutter.
7816 (See record_builtin_java_type in decl.c.) However, that causes
7817 incorrect debug entries if these types are actually used.
7818 So we re-enable debug output after extern "Java". */
7819 DECL_IGNORED_P (TYPE_NAME (java_byte_type_node)) = 0;
7820 DECL_IGNORED_P (TYPE_NAME (java_short_type_node)) = 0;
7821 DECL_IGNORED_P (TYPE_NAME (java_int_type_node)) = 0;
7822 DECL_IGNORED_P (TYPE_NAME (java_long_type_node)) = 0;
7823 DECL_IGNORED_P (TYPE_NAME (java_float_type_node)) = 0;
7824 DECL_IGNORED_P (TYPE_NAME (java_double_type_node)) = 0;
7825 DECL_IGNORED_P (TYPE_NAME (java_char_type_node)) = 0;
7826 DECL_IGNORED_P (TYPE_NAME (java_boolean_type_node)) = 0;
7828 else if (name == lang_name_c)
7830 current_lang_name = name;
7832 else
7833 error ("language string %<\"%E\"%> not recognized", name);
7836 /* Get out of the current language scope. */
7838 void
7839 pop_lang_context (void)
7841 current_lang_name = current_lang_base->pop ();
7844 /* Type instantiation routines. */
7846 /* Given an OVERLOAD and a TARGET_TYPE, return the function that
7847 matches the TARGET_TYPE. If there is no satisfactory match, return
7848 error_mark_node, and issue an error & warning messages under
7849 control of FLAGS. Permit pointers to member function if FLAGS
7850 permits. If TEMPLATE_ONLY, the name of the overloaded function was
7851 a template-id, and EXPLICIT_TARGS are the explicitly provided
7852 template arguments.
7854 If OVERLOAD is for one or more member functions, then ACCESS_PATH
7855 is the base path used to reference those member functions. If
7856 the address is resolved to a member function, access checks will be
7857 performed and errors issued if appropriate. */
7859 static tree
7860 resolve_address_of_overloaded_function (tree target_type,
7861 tree overload,
7862 tsubst_flags_t complain,
7863 bool template_only,
7864 tree explicit_targs,
7865 tree access_path)
7867 /* Here's what the standard says:
7869 [over.over]
7871 If the name is a function template, template argument deduction
7872 is done, and if the argument deduction succeeds, the deduced
7873 arguments are used to generate a single template function, which
7874 is added to the set of overloaded functions considered.
7876 Non-member functions and static member functions match targets of
7877 type "pointer-to-function" or "reference-to-function." Nonstatic
7878 member functions match targets of type "pointer-to-member
7879 function;" the function type of the pointer to member is used to
7880 select the member function from the set of overloaded member
7881 functions. If a nonstatic member function is selected, the
7882 reference to the overloaded function name is required to have the
7883 form of a pointer to member as described in 5.3.1.
7885 If more than one function is selected, any template functions in
7886 the set are eliminated if the set also contains a non-template
7887 function, and any given template function is eliminated if the
7888 set contains a second template function that is more specialized
7889 than the first according to the partial ordering rules 14.5.5.2.
7890 After such eliminations, if any, there shall remain exactly one
7891 selected function. */
7893 int is_ptrmem = 0;
7894 /* We store the matches in a TREE_LIST rooted here. The functions
7895 are the TREE_PURPOSE, not the TREE_VALUE, in this list, for easy
7896 interoperability with most_specialized_instantiation. */
7897 tree matches = NULL_TREE;
7898 tree fn;
7899 tree target_fn_type;
7901 /* By the time we get here, we should be seeing only real
7902 pointer-to-member types, not the internal POINTER_TYPE to
7903 METHOD_TYPE representation. */
7904 gcc_assert (!TYPE_PTR_P (target_type)
7905 || TREE_CODE (TREE_TYPE (target_type)) != METHOD_TYPE);
7907 gcc_assert (is_overloaded_fn (overload));
7909 /* Check that the TARGET_TYPE is reasonable. */
7910 if (TYPE_PTRFN_P (target_type)
7911 || TYPE_REFFN_P (target_type))
7912 /* This is OK. */;
7913 else if (TYPE_PTRMEMFUNC_P (target_type))
7914 /* This is OK, too. */
7915 is_ptrmem = 1;
7916 else if (TREE_CODE (target_type) == FUNCTION_TYPE)
7917 /* This is OK, too. This comes from a conversion to reference
7918 type. */
7919 target_type = build_reference_type (target_type);
7920 else
7922 if (complain & tf_error)
7923 error ("cannot resolve overloaded function %qD based on"
7924 " conversion to type %qT",
7925 DECL_NAME (OVL_FUNCTION (overload)), target_type);
7926 return error_mark_node;
7929 /* Non-member functions and static member functions match targets of type
7930 "pointer-to-function" or "reference-to-function." Nonstatic member
7931 functions match targets of type "pointer-to-member-function;" the
7932 function type of the pointer to member is used to select the member
7933 function from the set of overloaded member functions.
7935 So figure out the FUNCTION_TYPE that we want to match against. */
7936 target_fn_type = static_fn_type (target_type);
7938 /* If we can find a non-template function that matches, we can just
7939 use it. There's no point in generating template instantiations
7940 if we're just going to throw them out anyhow. But, of course, we
7941 can only do this when we don't *need* a template function. */
7942 if (!template_only)
7944 tree fns;
7946 for (fns = overload; fns; fns = OVL_NEXT (fns))
7948 tree fn = OVL_CURRENT (fns);
7950 if (TREE_CODE (fn) == TEMPLATE_DECL)
7951 /* We're not looking for templates just yet. */
7952 continue;
7954 if ((TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
7955 != is_ptrmem)
7956 /* We're looking for a non-static member, and this isn't
7957 one, or vice versa. */
7958 continue;
7960 /* Ignore functions which haven't been explicitly
7961 declared. */
7962 if (DECL_ANTICIPATED (fn))
7963 continue;
7965 /* See if there's a match. */
7966 tree fntype = static_fn_type (fn);
7967 if (same_type_p (target_fn_type, fntype)
7968 || can_convert_tx_safety (target_fn_type, fntype))
7969 matches = tree_cons (fn, NULL_TREE, matches);
7973 /* Now, if we've already got a match (or matches), there's no need
7974 to proceed to the template functions. But, if we don't have a
7975 match we need to look at them, too. */
7976 if (!matches)
7978 tree target_arg_types;
7979 tree target_ret_type;
7980 tree fns;
7981 tree *args;
7982 unsigned int nargs, ia;
7983 tree arg;
7985 target_arg_types = TYPE_ARG_TYPES (target_fn_type);
7986 target_ret_type = TREE_TYPE (target_fn_type);
7988 nargs = list_length (target_arg_types);
7989 args = XALLOCAVEC (tree, nargs);
7990 for (arg = target_arg_types, ia = 0;
7991 arg != NULL_TREE && arg != void_list_node;
7992 arg = TREE_CHAIN (arg), ++ia)
7993 args[ia] = TREE_VALUE (arg);
7994 nargs = ia;
7996 for (fns = overload; fns; fns = OVL_NEXT (fns))
7998 tree fn = OVL_CURRENT (fns);
7999 tree instantiation;
8000 tree targs;
8002 if (TREE_CODE (fn) != TEMPLATE_DECL)
8003 /* We're only looking for templates. */
8004 continue;
8006 if ((TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
8007 != is_ptrmem)
8008 /* We're not looking for a non-static member, and this is
8009 one, or vice versa. */
8010 continue;
8012 tree ret = target_ret_type;
8014 /* If the template has a deduced return type, don't expose it to
8015 template argument deduction. */
8016 if (undeduced_auto_decl (fn))
8017 ret = NULL_TREE;
8019 /* Try to do argument deduction. */
8020 targs = make_tree_vec (DECL_NTPARMS (fn));
8021 instantiation = fn_type_unification (fn, explicit_targs, targs, args,
8022 nargs, ret,
8023 DEDUCE_EXACT, LOOKUP_NORMAL,
8024 false, false);
8025 if (instantiation == error_mark_node)
8026 /* Instantiation failed. */
8027 continue;
8029 /* Constraints must be satisfied. This is done before
8030 return type deduction since that instantiates the
8031 function. */
8032 if (flag_concepts && !constraints_satisfied_p (instantiation))
8033 continue;
8035 /* And now force instantiation to do return type deduction. */
8036 if (undeduced_auto_decl (instantiation))
8038 ++function_depth;
8039 instantiate_decl (instantiation, /*defer*/false, /*class*/false);
8040 --function_depth;
8042 require_deduced_type (instantiation);
8045 /* See if there's a match. */
8046 tree fntype = static_fn_type (instantiation);
8047 if (same_type_p (target_fn_type, fntype)
8048 || can_convert_tx_safety (target_fn_type, fntype))
8049 matches = tree_cons (instantiation, fn, matches);
8052 /* Now, remove all but the most specialized of the matches. */
8053 if (matches)
8055 tree match = most_specialized_instantiation (matches);
8057 if (match != error_mark_node)
8058 matches = tree_cons (TREE_PURPOSE (match),
8059 NULL_TREE,
8060 NULL_TREE);
8064 /* Now we should have exactly one function in MATCHES. */
8065 if (matches == NULL_TREE)
8067 /* There were *no* matches. */
8068 if (complain & tf_error)
8070 error ("no matches converting function %qD to type %q#T",
8071 DECL_NAME (OVL_CURRENT (overload)),
8072 target_type);
8074 print_candidates (overload);
8076 return error_mark_node;
8078 else if (TREE_CHAIN (matches))
8080 /* There were too many matches. First check if they're all
8081 the same function. */
8082 tree match = NULL_TREE;
8084 fn = TREE_PURPOSE (matches);
8086 /* For multi-versioned functions, more than one match is just fine and
8087 decls_match will return false as they are different. */
8088 for (match = TREE_CHAIN (matches); match; match = TREE_CHAIN (match))
8089 if (!decls_match (fn, TREE_PURPOSE (match))
8090 && !targetm.target_option.function_versions
8091 (fn, TREE_PURPOSE (match)))
8092 break;
8094 if (match)
8096 if (complain & tf_error)
8098 error ("converting overloaded function %qD to type %q#T is ambiguous",
8099 DECL_NAME (OVL_FUNCTION (overload)),
8100 target_type);
8102 /* Since print_candidates expects the functions in the
8103 TREE_VALUE slot, we flip them here. */
8104 for (match = matches; match; match = TREE_CHAIN (match))
8105 TREE_VALUE (match) = TREE_PURPOSE (match);
8107 print_candidates (matches);
8110 return error_mark_node;
8114 /* Good, exactly one match. Now, convert it to the correct type. */
8115 fn = TREE_PURPOSE (matches);
8117 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
8118 && !(complain & tf_ptrmem_ok) && !flag_ms_extensions)
8120 static int explained;
8122 if (!(complain & tf_error))
8123 return error_mark_node;
8125 permerror (input_location, "assuming pointer to member %qD", fn);
8126 if (!explained)
8128 inform (input_location, "(a pointer to member can only be formed with %<&%E%>)", fn);
8129 explained = 1;
8133 /* If a pointer to a function that is multi-versioned is requested, the
8134 pointer to the dispatcher function is returned instead. This works
8135 well because indirectly calling the function will dispatch the right
8136 function version at run-time. */
8137 if (DECL_FUNCTION_VERSIONED (fn))
8139 fn = get_function_version_dispatcher (fn);
8140 if (fn == NULL)
8141 return error_mark_node;
8142 /* Mark all the versions corresponding to the dispatcher as used. */
8143 if (!(complain & tf_conv))
8144 mark_versions_used (fn);
8147 /* If we're doing overload resolution purely for the purpose of
8148 determining conversion sequences, we should not consider the
8149 function used. If this conversion sequence is selected, the
8150 function will be marked as used at this point. */
8151 if (!(complain & tf_conv))
8153 /* Make =delete work with SFINAE. */
8154 if (DECL_DELETED_FN (fn) && !(complain & tf_error))
8155 return error_mark_node;
8156 if (!mark_used (fn, complain) && !(complain & tf_error))
8157 return error_mark_node;
8160 /* We could not check access to member functions when this
8161 expression was originally created since we did not know at that
8162 time to which function the expression referred. */
8163 if (DECL_FUNCTION_MEMBER_P (fn))
8165 gcc_assert (access_path);
8166 perform_or_defer_access_check (access_path, fn, fn, complain);
8169 if (TYPE_PTRFN_P (target_type) || TYPE_PTRMEMFUNC_P (target_type))
8170 return cp_build_addr_expr (fn, complain);
8171 else
8173 /* The target must be a REFERENCE_TYPE. Above, cp_build_unary_op
8174 will mark the function as addressed, but here we must do it
8175 explicitly. */
8176 cxx_mark_addressable (fn);
8178 return fn;
8182 /* This function will instantiate the type of the expression given in
8183 RHS to match the type of LHSTYPE. If errors exist, then return
8184 error_mark_node. COMPLAIN is a bit mask. If TF_ERROR is set, then
8185 we complain on errors. If we are not complaining, never modify rhs,
8186 as overload resolution wants to try many possible instantiations, in
8187 the hope that at least one will work.
8189 For non-recursive calls, LHSTYPE should be a function, pointer to
8190 function, or a pointer to member function. */
8192 tree
8193 instantiate_type (tree lhstype, tree rhs, tsubst_flags_t complain)
8195 tsubst_flags_t complain_in = complain;
8196 tree access_path = NULL_TREE;
8198 complain &= ~tf_ptrmem_ok;
8200 if (lhstype == unknown_type_node)
8202 if (complain & tf_error)
8203 error ("not enough type information");
8204 return error_mark_node;
8207 if (TREE_TYPE (rhs) != NULL_TREE && ! (type_unknown_p (rhs)))
8209 tree fntype = non_reference (lhstype);
8210 if (same_type_p (fntype, TREE_TYPE (rhs)))
8211 return rhs;
8212 if (flag_ms_extensions
8213 && TYPE_PTRMEMFUNC_P (fntype)
8214 && !TYPE_PTRMEMFUNC_P (TREE_TYPE (rhs)))
8215 /* Microsoft allows `A::f' to be resolved to a
8216 pointer-to-member. */
8218 else
8220 if (complain & tf_error)
8221 error ("cannot convert %qE from type %qT to type %qT",
8222 rhs, TREE_TYPE (rhs), fntype);
8223 return error_mark_node;
8227 if (BASELINK_P (rhs))
8229 access_path = BASELINK_ACCESS_BINFO (rhs);
8230 rhs = BASELINK_FUNCTIONS (rhs);
8233 /* If we are in a template, and have a NON_DEPENDENT_EXPR, we cannot
8234 deduce any type information. */
8235 if (TREE_CODE (rhs) == NON_DEPENDENT_EXPR)
8237 if (complain & tf_error)
8238 error ("not enough type information");
8239 return error_mark_node;
8242 /* There only a few kinds of expressions that may have a type
8243 dependent on overload resolution. */
8244 gcc_assert (TREE_CODE (rhs) == ADDR_EXPR
8245 || TREE_CODE (rhs) == COMPONENT_REF
8246 || is_overloaded_fn (rhs)
8247 || (flag_ms_extensions && TREE_CODE (rhs) == FUNCTION_DECL));
8249 /* This should really only be used when attempting to distinguish
8250 what sort of a pointer to function we have. For now, any
8251 arithmetic operation which is not supported on pointers
8252 is rejected as an error. */
8254 switch (TREE_CODE (rhs))
8256 case COMPONENT_REF:
8258 tree member = TREE_OPERAND (rhs, 1);
8260 member = instantiate_type (lhstype, member, complain);
8261 if (member != error_mark_node
8262 && TREE_SIDE_EFFECTS (TREE_OPERAND (rhs, 0)))
8263 /* Do not lose object's side effects. */
8264 return build2 (COMPOUND_EXPR, TREE_TYPE (member),
8265 TREE_OPERAND (rhs, 0), member);
8266 return member;
8269 case OFFSET_REF:
8270 rhs = TREE_OPERAND (rhs, 1);
8271 if (BASELINK_P (rhs))
8272 return instantiate_type (lhstype, rhs, complain_in);
8274 /* This can happen if we are forming a pointer-to-member for a
8275 member template. */
8276 gcc_assert (TREE_CODE (rhs) == TEMPLATE_ID_EXPR);
8278 /* Fall through. */
8280 case TEMPLATE_ID_EXPR:
8282 tree fns = TREE_OPERAND (rhs, 0);
8283 tree args = TREE_OPERAND (rhs, 1);
8285 return
8286 resolve_address_of_overloaded_function (lhstype, fns, complain_in,
8287 /*template_only=*/true,
8288 args, access_path);
8291 case OVERLOAD:
8292 case FUNCTION_DECL:
8293 return
8294 resolve_address_of_overloaded_function (lhstype, rhs, complain_in,
8295 /*template_only=*/false,
8296 /*explicit_targs=*/NULL_TREE,
8297 access_path);
8299 case ADDR_EXPR:
8301 if (PTRMEM_OK_P (rhs))
8302 complain |= tf_ptrmem_ok;
8304 return instantiate_type (lhstype, TREE_OPERAND (rhs, 0), complain);
8307 case ERROR_MARK:
8308 return error_mark_node;
8310 default:
8311 gcc_unreachable ();
8313 return error_mark_node;
8316 /* Return the name of the virtual function pointer field
8317 (as an IDENTIFIER_NODE) for the given TYPE. Note that
8318 this may have to look back through base types to find the
8319 ultimate field name. (For single inheritance, these could
8320 all be the same name. Who knows for multiple inheritance). */
8322 static tree
8323 get_vfield_name (tree type)
8325 tree binfo, base_binfo;
8326 char *buf;
8328 for (binfo = TYPE_BINFO (type);
8329 BINFO_N_BASE_BINFOS (binfo);
8330 binfo = base_binfo)
8332 base_binfo = BINFO_BASE_BINFO (binfo, 0);
8334 if (BINFO_VIRTUAL_P (base_binfo)
8335 || !TYPE_CONTAINS_VPTR_P (BINFO_TYPE (base_binfo)))
8336 break;
8339 type = BINFO_TYPE (binfo);
8340 buf = (char *) alloca (sizeof (VFIELD_NAME_FORMAT)
8341 + TYPE_NAME_LENGTH (type) + 2);
8342 sprintf (buf, VFIELD_NAME_FORMAT,
8343 IDENTIFIER_POINTER (constructor_name (type)));
8344 return get_identifier (buf);
8347 void
8348 print_class_statistics (void)
8350 if (! GATHER_STATISTICS)
8351 return;
8353 fprintf (stderr, "convert_harshness = %d\n", n_convert_harshness);
8354 fprintf (stderr, "compute_conversion_costs = %d\n", n_compute_conversion_costs);
8355 if (n_vtables)
8357 fprintf (stderr, "vtables = %d; vtable searches = %d\n",
8358 n_vtables, n_vtable_searches);
8359 fprintf (stderr, "vtable entries = %d; vtable elems = %d\n",
8360 n_vtable_entries, n_vtable_elems);
8364 /* Build a dummy reference to ourselves so Derived::Base (and A::A) works,
8365 according to [class]:
8366 The class-name is also inserted
8367 into the scope of the class itself. For purposes of access checking,
8368 the inserted class name is treated as if it were a public member name. */
8370 void
8371 build_self_reference (void)
8373 tree name = constructor_name (current_class_type);
8374 tree value = build_lang_decl (TYPE_DECL, name, current_class_type);
8375 tree saved_cas;
8377 DECL_NONLOCAL (value) = 1;
8378 DECL_CONTEXT (value) = current_class_type;
8379 DECL_ARTIFICIAL (value) = 1;
8380 SET_DECL_SELF_REFERENCE_P (value);
8381 set_underlying_type (value);
8383 if (processing_template_decl)
8384 value = push_template_decl (value);
8386 saved_cas = current_access_specifier;
8387 current_access_specifier = access_public_node;
8388 finish_member_declaration (value);
8389 current_access_specifier = saved_cas;
8392 /* Returns 1 if TYPE contains only padding bytes. */
8395 is_empty_class (tree type)
8397 if (type == error_mark_node)
8398 return 0;
8400 if (! CLASS_TYPE_P (type))
8401 return 0;
8403 return CLASSTYPE_EMPTY_P (type);
8406 /* Returns true if TYPE contains no actual data, just various
8407 possible combinations of empty classes and possibly a vptr. */
8409 bool
8410 is_really_empty_class (tree type)
8412 if (CLASS_TYPE_P (type))
8414 tree field;
8415 tree binfo;
8416 tree base_binfo;
8417 int i;
8419 /* CLASSTYPE_EMPTY_P isn't set properly until the class is actually laid
8420 out, but we'd like to be able to check this before then. */
8421 if (COMPLETE_TYPE_P (type) && is_empty_class (type))
8422 return true;
8424 for (binfo = TYPE_BINFO (type), i = 0;
8425 BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
8426 if (!is_really_empty_class (BINFO_TYPE (base_binfo)))
8427 return false;
8428 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
8429 if (TREE_CODE (field) == FIELD_DECL
8430 && !DECL_ARTIFICIAL (field)
8431 /* An unnamed bit-field is not a data member. */
8432 && (DECL_NAME (field) || !DECL_C_BIT_FIELD (field))
8433 && !is_really_empty_class (TREE_TYPE (field)))
8434 return false;
8435 return true;
8437 else if (TREE_CODE (type) == ARRAY_TYPE)
8438 return (integer_zerop (array_type_nelts_top (type))
8439 || is_really_empty_class (TREE_TYPE (type)));
8440 return false;
8443 /* Note that NAME was looked up while the current class was being
8444 defined and that the result of that lookup was DECL. */
8446 void
8447 maybe_note_name_used_in_class (tree name, tree decl)
8449 splay_tree names_used;
8451 /* If we're not defining a class, there's nothing to do. */
8452 if (!(innermost_scope_kind() == sk_class
8453 && TYPE_BEING_DEFINED (current_class_type)
8454 && !LAMBDA_TYPE_P (current_class_type)))
8455 return;
8457 /* If there's already a binding for this NAME, then we don't have
8458 anything to worry about. */
8459 if (lookup_member (current_class_type, name,
8460 /*protect=*/0, /*want_type=*/false, tf_warning_or_error))
8461 return;
8463 if (!current_class_stack[current_class_depth - 1].names_used)
8464 current_class_stack[current_class_depth - 1].names_used
8465 = splay_tree_new (splay_tree_compare_pointers, 0, 0);
8466 names_used = current_class_stack[current_class_depth - 1].names_used;
8468 splay_tree_insert (names_used,
8469 (splay_tree_key) name,
8470 (splay_tree_value) decl);
8473 /* Note that NAME was declared (as DECL) in the current class. Check
8474 to see that the declaration is valid. */
8476 void
8477 note_name_declared_in_class (tree name, tree decl)
8479 splay_tree names_used;
8480 splay_tree_node n;
8482 /* Look to see if we ever used this name. */
8483 names_used
8484 = current_class_stack[current_class_depth - 1].names_used;
8485 if (!names_used)
8486 return;
8487 /* The C language allows members to be declared with a type of the same
8488 name, and the C++ standard says this diagnostic is not required. So
8489 allow it in extern "C" blocks unless predantic is specified.
8490 Allow it in all cases if -ms-extensions is specified. */
8491 if ((!pedantic && current_lang_name == lang_name_c)
8492 || flag_ms_extensions)
8493 return;
8494 n = splay_tree_lookup (names_used, (splay_tree_key) name);
8495 if (n)
8497 /* [basic.scope.class]
8499 A name N used in a class S shall refer to the same declaration
8500 in its context and when re-evaluated in the completed scope of
8501 S. */
8502 permerror (input_location, "declaration of %q#D", decl);
8503 permerror (location_of ((tree) n->value),
8504 "changes meaning of %qD from %q#D",
8505 DECL_NAME (OVL_CURRENT (decl)), (tree) n->value);
8509 /* Returns the VAR_DECL for the complete vtable associated with BINFO.
8510 Secondary vtables are merged with primary vtables; this function
8511 will return the VAR_DECL for the primary vtable. */
8513 tree
8514 get_vtbl_decl_for_binfo (tree binfo)
8516 tree decl;
8518 decl = BINFO_VTABLE (binfo);
8519 if (decl && TREE_CODE (decl) == POINTER_PLUS_EXPR)
8521 gcc_assert (TREE_CODE (TREE_OPERAND (decl, 0)) == ADDR_EXPR);
8522 decl = TREE_OPERAND (TREE_OPERAND (decl, 0), 0);
8524 if (decl)
8525 gcc_assert (VAR_P (decl));
8526 return decl;
8530 /* Returns the binfo for the primary base of BINFO. If the resulting
8531 BINFO is a virtual base, and it is inherited elsewhere in the
8532 hierarchy, then the returned binfo might not be the primary base of
8533 BINFO in the complete object. Check BINFO_PRIMARY_P or
8534 BINFO_LOST_PRIMARY_P to be sure. */
8536 static tree
8537 get_primary_binfo (tree binfo)
8539 tree primary_base;
8541 primary_base = CLASSTYPE_PRIMARY_BINFO (BINFO_TYPE (binfo));
8542 if (!primary_base)
8543 return NULL_TREE;
8545 return copied_binfo (primary_base, binfo);
8548 /* As above, but iterate until we reach the binfo that actually provides the
8549 vptr for BINFO. */
8551 static tree
8552 most_primary_binfo (tree binfo)
8554 tree b = binfo;
8555 while (CLASSTYPE_HAS_PRIMARY_BASE_P (BINFO_TYPE (b))
8556 && !BINFO_LOST_PRIMARY_P (b))
8558 tree primary_base = get_primary_binfo (b);
8559 gcc_assert (BINFO_PRIMARY_P (primary_base)
8560 && BINFO_INHERITANCE_CHAIN (primary_base) == b);
8561 b = primary_base;
8563 return b;
8566 /* Returns true if BINFO gets its vptr from a virtual base of the most derived
8567 type. Note that the virtual inheritance might be above or below BINFO in
8568 the hierarchy. */
8570 bool
8571 vptr_via_virtual_p (tree binfo)
8573 if (TYPE_P (binfo))
8574 binfo = TYPE_BINFO (binfo);
8575 tree primary = most_primary_binfo (binfo);
8576 /* Don't limit binfo_via_virtual, we want to return true when BINFO itself is
8577 a morally virtual base. */
8578 tree virt = binfo_via_virtual (primary, NULL_TREE);
8579 return virt != NULL_TREE;
8582 /* If INDENTED_P is zero, indent to INDENT. Return nonzero. */
8584 static int
8585 maybe_indent_hierarchy (FILE * stream, int indent, int indented_p)
8587 if (!indented_p)
8588 fprintf (stream, "%*s", indent, "");
8589 return 1;
8592 /* Dump the offsets of all the bases rooted at BINFO to STREAM.
8593 INDENT should be zero when called from the top level; it is
8594 incremented recursively. IGO indicates the next expected BINFO in
8595 inheritance graph ordering. */
8597 static tree
8598 dump_class_hierarchy_r (FILE *stream,
8599 int flags,
8600 tree binfo,
8601 tree igo,
8602 int indent)
8604 int indented = 0;
8605 tree base_binfo;
8606 int i;
8608 indented = maybe_indent_hierarchy (stream, indent, 0);
8609 fprintf (stream, "%s (0x" HOST_WIDE_INT_PRINT_HEX ") ",
8610 type_as_string (BINFO_TYPE (binfo), TFF_PLAIN_IDENTIFIER),
8611 (HOST_WIDE_INT) (uintptr_t) binfo);
8612 if (binfo != igo)
8614 fprintf (stream, "alternative-path\n");
8615 return igo;
8617 igo = TREE_CHAIN (binfo);
8619 fprintf (stream, HOST_WIDE_INT_PRINT_DEC,
8620 tree_to_shwi (BINFO_OFFSET (binfo)));
8621 if (is_empty_class (BINFO_TYPE (binfo)))
8622 fprintf (stream, " empty");
8623 else if (CLASSTYPE_NEARLY_EMPTY_P (BINFO_TYPE (binfo)))
8624 fprintf (stream, " nearly-empty");
8625 if (BINFO_VIRTUAL_P (binfo))
8626 fprintf (stream, " virtual");
8627 fprintf (stream, "\n");
8629 indented = 0;
8630 if (BINFO_PRIMARY_P (binfo))
8632 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
8633 fprintf (stream, " primary-for %s (0x" HOST_WIDE_INT_PRINT_HEX ")",
8634 type_as_string (BINFO_TYPE (BINFO_INHERITANCE_CHAIN (binfo)),
8635 TFF_PLAIN_IDENTIFIER),
8636 (HOST_WIDE_INT) (uintptr_t) BINFO_INHERITANCE_CHAIN (binfo));
8638 if (BINFO_LOST_PRIMARY_P (binfo))
8640 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
8641 fprintf (stream, " lost-primary");
8643 if (indented)
8644 fprintf (stream, "\n");
8646 if (!(flags & TDF_SLIM))
8648 int indented = 0;
8650 if (BINFO_SUBVTT_INDEX (binfo))
8652 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
8653 fprintf (stream, " subvttidx=%s",
8654 expr_as_string (BINFO_SUBVTT_INDEX (binfo),
8655 TFF_PLAIN_IDENTIFIER));
8657 if (BINFO_VPTR_INDEX (binfo))
8659 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
8660 fprintf (stream, " vptridx=%s",
8661 expr_as_string (BINFO_VPTR_INDEX (binfo),
8662 TFF_PLAIN_IDENTIFIER));
8664 if (BINFO_VPTR_FIELD (binfo))
8666 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
8667 fprintf (stream, " vbaseoffset=%s",
8668 expr_as_string (BINFO_VPTR_FIELD (binfo),
8669 TFF_PLAIN_IDENTIFIER));
8671 if (BINFO_VTABLE (binfo))
8673 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
8674 fprintf (stream, " vptr=%s",
8675 expr_as_string (BINFO_VTABLE (binfo),
8676 TFF_PLAIN_IDENTIFIER));
8679 if (indented)
8680 fprintf (stream, "\n");
8683 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
8684 igo = dump_class_hierarchy_r (stream, flags, base_binfo, igo, indent + 2);
8686 return igo;
8689 /* Dump the BINFO hierarchy for T. */
8691 static void
8692 dump_class_hierarchy_1 (FILE *stream, int flags, tree t)
8694 fprintf (stream, "Class %s\n", type_as_string (t, TFF_PLAIN_IDENTIFIER));
8695 fprintf (stream, " size=%lu align=%lu\n",
8696 (unsigned long)(tree_to_shwi (TYPE_SIZE (t)) / BITS_PER_UNIT),
8697 (unsigned long)(TYPE_ALIGN (t) / BITS_PER_UNIT));
8698 fprintf (stream, " base size=%lu base align=%lu\n",
8699 (unsigned long)(tree_to_shwi (TYPE_SIZE (CLASSTYPE_AS_BASE (t)))
8700 / BITS_PER_UNIT),
8701 (unsigned long)(TYPE_ALIGN (CLASSTYPE_AS_BASE (t))
8702 / BITS_PER_UNIT));
8703 dump_class_hierarchy_r (stream, flags, TYPE_BINFO (t), TYPE_BINFO (t), 0);
8704 fprintf (stream, "\n");
8707 /* Debug interface to hierarchy dumping. */
8709 void
8710 debug_class (tree t)
8712 dump_class_hierarchy_1 (stderr, TDF_SLIM, t);
8715 static void
8716 dump_class_hierarchy (tree t)
8718 int flags;
8719 FILE *stream = get_dump_info (TDI_class, &flags);
8721 if (stream)
8723 dump_class_hierarchy_1 (stream, flags, t);
8727 static void
8728 dump_array (FILE * stream, tree decl)
8730 tree value;
8731 unsigned HOST_WIDE_INT ix;
8732 HOST_WIDE_INT elt;
8733 tree size = TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (decl)));
8735 elt = (tree_to_shwi (TYPE_SIZE (TREE_TYPE (TREE_TYPE (decl))))
8736 / BITS_PER_UNIT);
8737 fprintf (stream, "%s:", decl_as_string (decl, TFF_PLAIN_IDENTIFIER));
8738 fprintf (stream, " %s entries",
8739 expr_as_string (size_binop (PLUS_EXPR, size, size_one_node),
8740 TFF_PLAIN_IDENTIFIER));
8741 fprintf (stream, "\n");
8743 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (DECL_INITIAL (decl)),
8744 ix, value)
8745 fprintf (stream, "%-4ld %s\n", (long)(ix * elt),
8746 expr_as_string (value, TFF_PLAIN_IDENTIFIER));
8749 static void
8750 dump_vtable (tree t, tree binfo, tree vtable)
8752 int flags;
8753 FILE *stream = get_dump_info (TDI_class, &flags);
8755 if (!stream)
8756 return;
8758 if (!(flags & TDF_SLIM))
8760 int ctor_vtbl_p = TYPE_BINFO (t) != binfo;
8762 fprintf (stream, "%s for %s",
8763 ctor_vtbl_p ? "Construction vtable" : "Vtable",
8764 type_as_string (BINFO_TYPE (binfo), TFF_PLAIN_IDENTIFIER));
8765 if (ctor_vtbl_p)
8767 if (!BINFO_VIRTUAL_P (binfo))
8768 fprintf (stream, " (0x" HOST_WIDE_INT_PRINT_HEX " instance)",
8769 (HOST_WIDE_INT) (uintptr_t) binfo);
8770 fprintf (stream, " in %s", type_as_string (t, TFF_PLAIN_IDENTIFIER));
8772 fprintf (stream, "\n");
8773 dump_array (stream, vtable);
8774 fprintf (stream, "\n");
8778 static void
8779 dump_vtt (tree t, tree vtt)
8781 int flags;
8782 FILE *stream = get_dump_info (TDI_class, &flags);
8784 if (!stream)
8785 return;
8787 if (!(flags & TDF_SLIM))
8789 fprintf (stream, "VTT for %s\n",
8790 type_as_string (t, TFF_PLAIN_IDENTIFIER));
8791 dump_array (stream, vtt);
8792 fprintf (stream, "\n");
8796 /* Dump a function or thunk and its thunkees. */
8798 static void
8799 dump_thunk (FILE *stream, int indent, tree thunk)
8801 static const char spaces[] = " ";
8802 tree name = DECL_NAME (thunk);
8803 tree thunks;
8805 fprintf (stream, "%.*s%p %s %s", indent, spaces,
8806 (void *)thunk,
8807 !DECL_THUNK_P (thunk) ? "function"
8808 : DECL_THIS_THUNK_P (thunk) ? "this-thunk" : "covariant-thunk",
8809 name ? IDENTIFIER_POINTER (name) : "<unset>");
8810 if (DECL_THUNK_P (thunk))
8812 HOST_WIDE_INT fixed_adjust = THUNK_FIXED_OFFSET (thunk);
8813 tree virtual_adjust = THUNK_VIRTUAL_OFFSET (thunk);
8815 fprintf (stream, " fixed=" HOST_WIDE_INT_PRINT_DEC, fixed_adjust);
8816 if (!virtual_adjust)
8817 /*NOP*/;
8818 else if (DECL_THIS_THUNK_P (thunk))
8819 fprintf (stream, " vcall=" HOST_WIDE_INT_PRINT_DEC,
8820 tree_to_shwi (virtual_adjust));
8821 else
8822 fprintf (stream, " vbase=" HOST_WIDE_INT_PRINT_DEC "(%s)",
8823 tree_to_shwi (BINFO_VPTR_FIELD (virtual_adjust)),
8824 type_as_string (BINFO_TYPE (virtual_adjust), TFF_SCOPE));
8825 if (THUNK_ALIAS (thunk))
8826 fprintf (stream, " alias to %p", (void *)THUNK_ALIAS (thunk));
8828 fprintf (stream, "\n");
8829 for (thunks = DECL_THUNKS (thunk); thunks; thunks = TREE_CHAIN (thunks))
8830 dump_thunk (stream, indent + 2, thunks);
8833 /* Dump the thunks for FN. */
8835 void
8836 debug_thunks (tree fn)
8838 dump_thunk (stderr, 0, fn);
8841 /* Virtual function table initialization. */
8843 /* Create all the necessary vtables for T and its base classes. */
8845 static void
8846 finish_vtbls (tree t)
8848 tree vbase;
8849 vec<constructor_elt, va_gc> *v = NULL;
8850 tree vtable = BINFO_VTABLE (TYPE_BINFO (t));
8852 /* We lay out the primary and secondary vtables in one contiguous
8853 vtable. The primary vtable is first, followed by the non-virtual
8854 secondary vtables in inheritance graph order. */
8855 accumulate_vtbl_inits (TYPE_BINFO (t), TYPE_BINFO (t), TYPE_BINFO (t),
8856 vtable, t, &v);
8858 /* Then come the virtual bases, also in inheritance graph order. */
8859 for (vbase = TYPE_BINFO (t); vbase; vbase = TREE_CHAIN (vbase))
8861 if (!BINFO_VIRTUAL_P (vbase))
8862 continue;
8863 accumulate_vtbl_inits (vbase, vbase, TYPE_BINFO (t), vtable, t, &v);
8866 if (BINFO_VTABLE (TYPE_BINFO (t)))
8867 initialize_vtable (TYPE_BINFO (t), v);
8870 /* Initialize the vtable for BINFO with the INITS. */
8872 static void
8873 initialize_vtable (tree binfo, vec<constructor_elt, va_gc> *inits)
8875 tree decl;
8877 layout_vtable_decl (binfo, vec_safe_length (inits));
8878 decl = get_vtbl_decl_for_binfo (binfo);
8879 initialize_artificial_var (decl, inits);
8880 dump_vtable (BINFO_TYPE (binfo), binfo, decl);
8883 /* Build the VTT (virtual table table) for T.
8884 A class requires a VTT if it has virtual bases.
8886 This holds
8887 1 - primary virtual pointer for complete object T
8888 2 - secondary VTTs for each direct non-virtual base of T which requires a
8890 3 - secondary virtual pointers for each direct or indirect base of T which
8891 has virtual bases or is reachable via a virtual path from T.
8892 4 - secondary VTTs for each direct or indirect virtual base of T.
8894 Secondary VTTs look like complete object VTTs without part 4. */
8896 static void
8897 build_vtt (tree t)
8899 tree type;
8900 tree vtt;
8901 tree index;
8902 vec<constructor_elt, va_gc> *inits;
8904 /* Build up the initializers for the VTT. */
8905 inits = NULL;
8906 index = size_zero_node;
8907 build_vtt_inits (TYPE_BINFO (t), t, &inits, &index);
8909 /* If we didn't need a VTT, we're done. */
8910 if (!inits)
8911 return;
8913 /* Figure out the type of the VTT. */
8914 type = build_array_of_n_type (const_ptr_type_node,
8915 inits->length ());
8917 /* Now, build the VTT object itself. */
8918 vtt = build_vtable (t, mangle_vtt_for_type (t), type);
8919 initialize_artificial_var (vtt, inits);
8920 /* Add the VTT to the vtables list. */
8921 DECL_CHAIN (vtt) = DECL_CHAIN (CLASSTYPE_VTABLES (t));
8922 DECL_CHAIN (CLASSTYPE_VTABLES (t)) = vtt;
8924 dump_vtt (t, vtt);
8927 /* When building a secondary VTT, BINFO_VTABLE is set to a TREE_LIST with
8928 PURPOSE the RTTI_BINFO, VALUE the real vtable pointer for this binfo,
8929 and CHAIN the vtable pointer for this binfo after construction is
8930 complete. VALUE can also be another BINFO, in which case we recurse. */
8932 static tree
8933 binfo_ctor_vtable (tree binfo)
8935 tree vt;
8937 while (1)
8939 vt = BINFO_VTABLE (binfo);
8940 if (TREE_CODE (vt) == TREE_LIST)
8941 vt = TREE_VALUE (vt);
8942 if (TREE_CODE (vt) == TREE_BINFO)
8943 binfo = vt;
8944 else
8945 break;
8948 return vt;
8951 /* Data for secondary VTT initialization. */
8952 struct secondary_vptr_vtt_init_data
8954 /* Is this the primary VTT? */
8955 bool top_level_p;
8957 /* Current index into the VTT. */
8958 tree index;
8960 /* Vector of initializers built up. */
8961 vec<constructor_elt, va_gc> *inits;
8963 /* The type being constructed by this secondary VTT. */
8964 tree type_being_constructed;
8967 /* Recursively build the VTT-initializer for BINFO (which is in the
8968 hierarchy dominated by T). INITS points to the end of the initializer
8969 list to date. INDEX is the VTT index where the next element will be
8970 replaced. Iff BINFO is the binfo for T, this is the top level VTT (i.e.
8971 not a subvtt for some base of T). When that is so, we emit the sub-VTTs
8972 for virtual bases of T. When it is not so, we build the constructor
8973 vtables for the BINFO-in-T variant. */
8975 static void
8976 build_vtt_inits (tree binfo, tree t, vec<constructor_elt, va_gc> **inits,
8977 tree *index)
8979 int i;
8980 tree b;
8981 tree init;
8982 secondary_vptr_vtt_init_data data;
8983 int top_level_p = SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), t);
8985 /* We only need VTTs for subobjects with virtual bases. */
8986 if (!CLASSTYPE_VBASECLASSES (BINFO_TYPE (binfo)))
8987 return;
8989 /* We need to use a construction vtable if this is not the primary
8990 VTT. */
8991 if (!top_level_p)
8993 build_ctor_vtbl_group (binfo, t);
8995 /* Record the offset in the VTT where this sub-VTT can be found. */
8996 BINFO_SUBVTT_INDEX (binfo) = *index;
8999 /* Add the address of the primary vtable for the complete object. */
9000 init = binfo_ctor_vtable (binfo);
9001 CONSTRUCTOR_APPEND_ELT (*inits, NULL_TREE, init);
9002 if (top_level_p)
9004 gcc_assert (!BINFO_VPTR_INDEX (binfo));
9005 BINFO_VPTR_INDEX (binfo) = *index;
9007 *index = size_binop (PLUS_EXPR, *index, TYPE_SIZE_UNIT (ptr_type_node));
9009 /* Recursively add the secondary VTTs for non-virtual bases. */
9010 for (i = 0; BINFO_BASE_ITERATE (binfo, i, b); ++i)
9011 if (!BINFO_VIRTUAL_P (b))
9012 build_vtt_inits (b, t, inits, index);
9014 /* Add secondary virtual pointers for all subobjects of BINFO with
9015 either virtual bases or reachable along a virtual path, except
9016 subobjects that are non-virtual primary bases. */
9017 data.top_level_p = top_level_p;
9018 data.index = *index;
9019 data.inits = *inits;
9020 data.type_being_constructed = BINFO_TYPE (binfo);
9022 dfs_walk_once (binfo, dfs_build_secondary_vptr_vtt_inits, NULL, &data);
9024 *index = data.index;
9026 /* data.inits might have grown as we added secondary virtual pointers.
9027 Make sure our caller knows about the new vector. */
9028 *inits = data.inits;
9030 if (top_level_p)
9031 /* Add the secondary VTTs for virtual bases in inheritance graph
9032 order. */
9033 for (b = TYPE_BINFO (BINFO_TYPE (binfo)); b; b = TREE_CHAIN (b))
9035 if (!BINFO_VIRTUAL_P (b))
9036 continue;
9038 build_vtt_inits (b, t, inits, index);
9040 else
9041 /* Remove the ctor vtables we created. */
9042 dfs_walk_all (binfo, dfs_fixup_binfo_vtbls, NULL, binfo);
9045 /* Called from build_vtt_inits via dfs_walk. BINFO is the binfo for the base
9046 in most derived. DATA is a SECONDARY_VPTR_VTT_INIT_DATA structure. */
9048 static tree
9049 dfs_build_secondary_vptr_vtt_inits (tree binfo, void *data_)
9051 secondary_vptr_vtt_init_data *data = (secondary_vptr_vtt_init_data *)data_;
9053 /* We don't care about bases that don't have vtables. */
9054 if (!TYPE_VFIELD (BINFO_TYPE (binfo)))
9055 return dfs_skip_bases;
9057 /* We're only interested in proper subobjects of the type being
9058 constructed. */
9059 if (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), data->type_being_constructed))
9060 return NULL_TREE;
9062 /* We're only interested in bases with virtual bases or reachable
9063 via a virtual path from the type being constructed. */
9064 if (!(CLASSTYPE_VBASECLASSES (BINFO_TYPE (binfo))
9065 || binfo_via_virtual (binfo, data->type_being_constructed)))
9066 return dfs_skip_bases;
9068 /* We're not interested in non-virtual primary bases. */
9069 if (!BINFO_VIRTUAL_P (binfo) && BINFO_PRIMARY_P (binfo))
9070 return NULL_TREE;
9072 /* Record the index where this secondary vptr can be found. */
9073 if (data->top_level_p)
9075 gcc_assert (!BINFO_VPTR_INDEX (binfo));
9076 BINFO_VPTR_INDEX (binfo) = data->index;
9078 if (BINFO_VIRTUAL_P (binfo))
9080 /* It's a primary virtual base, and this is not a
9081 construction vtable. Find the base this is primary of in
9082 the inheritance graph, and use that base's vtable
9083 now. */
9084 while (BINFO_PRIMARY_P (binfo))
9085 binfo = BINFO_INHERITANCE_CHAIN (binfo);
9089 /* Add the initializer for the secondary vptr itself. */
9090 CONSTRUCTOR_APPEND_ELT (data->inits, NULL_TREE, binfo_ctor_vtable (binfo));
9092 /* Advance the vtt index. */
9093 data->index = size_binop (PLUS_EXPR, data->index,
9094 TYPE_SIZE_UNIT (ptr_type_node));
9096 return NULL_TREE;
9099 /* Called from build_vtt_inits via dfs_walk. After building
9100 constructor vtables and generating the sub-vtt from them, we need
9101 to restore the BINFO_VTABLES that were scribbled on. DATA is the
9102 binfo of the base whose sub vtt was generated. */
9104 static tree
9105 dfs_fixup_binfo_vtbls (tree binfo, void* data)
9107 tree vtable = BINFO_VTABLE (binfo);
9109 if (!TYPE_CONTAINS_VPTR_P (BINFO_TYPE (binfo)))
9110 /* If this class has no vtable, none of its bases do. */
9111 return dfs_skip_bases;
9113 if (!vtable)
9114 /* This might be a primary base, so have no vtable in this
9115 hierarchy. */
9116 return NULL_TREE;
9118 /* If we scribbled the construction vtable vptr into BINFO, clear it
9119 out now. */
9120 if (TREE_CODE (vtable) == TREE_LIST
9121 && (TREE_PURPOSE (vtable) == (tree) data))
9122 BINFO_VTABLE (binfo) = TREE_CHAIN (vtable);
9124 return NULL_TREE;
9127 /* Build the construction vtable group for BINFO which is in the
9128 hierarchy dominated by T. */
9130 static void
9131 build_ctor_vtbl_group (tree binfo, tree t)
9133 tree type;
9134 tree vtbl;
9135 tree id;
9136 tree vbase;
9137 vec<constructor_elt, va_gc> *v;
9139 /* See if we've already created this construction vtable group. */
9140 id = mangle_ctor_vtbl_for_type (t, binfo);
9141 if (IDENTIFIER_GLOBAL_VALUE (id))
9142 return;
9144 gcc_assert (!SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), t));
9145 /* Build a version of VTBL (with the wrong type) for use in
9146 constructing the addresses of secondary vtables in the
9147 construction vtable group. */
9148 vtbl = build_vtable (t, id, ptr_type_node);
9149 DECL_CONSTRUCTION_VTABLE_P (vtbl) = 1;
9150 /* Don't export construction vtables from shared libraries. Even on
9151 targets that don't support hidden visibility, this tells
9152 can_refer_decl_in_current_unit_p not to assume that it's safe to
9153 access from a different compilation unit (bz 54314). */
9154 DECL_VISIBILITY (vtbl) = VISIBILITY_HIDDEN;
9155 DECL_VISIBILITY_SPECIFIED (vtbl) = true;
9157 v = NULL;
9158 accumulate_vtbl_inits (binfo, TYPE_BINFO (TREE_TYPE (binfo)),
9159 binfo, vtbl, t, &v);
9161 /* Add the vtables for each of our virtual bases using the vbase in T
9162 binfo. */
9163 for (vbase = TYPE_BINFO (BINFO_TYPE (binfo));
9164 vbase;
9165 vbase = TREE_CHAIN (vbase))
9167 tree b;
9169 if (!BINFO_VIRTUAL_P (vbase))
9170 continue;
9171 b = copied_binfo (vbase, binfo);
9173 accumulate_vtbl_inits (b, vbase, binfo, vtbl, t, &v);
9176 /* Figure out the type of the construction vtable. */
9177 type = build_array_of_n_type (vtable_entry_type, v->length ());
9178 layout_type (type);
9179 TREE_TYPE (vtbl) = type;
9180 DECL_SIZE (vtbl) = DECL_SIZE_UNIT (vtbl) = NULL_TREE;
9181 layout_decl (vtbl, 0);
9183 /* Initialize the construction vtable. */
9184 CLASSTYPE_VTABLES (t) = chainon (CLASSTYPE_VTABLES (t), vtbl);
9185 initialize_artificial_var (vtbl, v);
9186 dump_vtable (t, binfo, vtbl);
9189 /* Add the vtbl initializers for BINFO (and its bases other than
9190 non-virtual primaries) to the list of INITS. BINFO is in the
9191 hierarchy dominated by T. RTTI_BINFO is the binfo within T of
9192 the constructor the vtbl inits should be accumulated for. (If this
9193 is the complete object vtbl then RTTI_BINFO will be TYPE_BINFO (T).)
9194 ORIG_BINFO is the binfo for this object within BINFO_TYPE (RTTI_BINFO).
9195 BINFO is the active base equivalent of ORIG_BINFO in the inheritance
9196 graph of T. Both BINFO and ORIG_BINFO will have the same BINFO_TYPE,
9197 but are not necessarily the same in terms of layout. */
9199 static void
9200 accumulate_vtbl_inits (tree binfo,
9201 tree orig_binfo,
9202 tree rtti_binfo,
9203 tree vtbl,
9204 tree t,
9205 vec<constructor_elt, va_gc> **inits)
9207 int i;
9208 tree base_binfo;
9209 int ctor_vtbl_p = !SAME_BINFO_TYPE_P (BINFO_TYPE (rtti_binfo), t);
9211 gcc_assert (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), BINFO_TYPE (orig_binfo)));
9213 /* If it doesn't have a vptr, we don't do anything. */
9214 if (!TYPE_CONTAINS_VPTR_P (BINFO_TYPE (binfo)))
9215 return;
9217 /* If we're building a construction vtable, we're not interested in
9218 subobjects that don't require construction vtables. */
9219 if (ctor_vtbl_p
9220 && !CLASSTYPE_VBASECLASSES (BINFO_TYPE (binfo))
9221 && !binfo_via_virtual (orig_binfo, BINFO_TYPE (rtti_binfo)))
9222 return;
9224 /* Build the initializers for the BINFO-in-T vtable. */
9225 dfs_accumulate_vtbl_inits (binfo, orig_binfo, rtti_binfo, vtbl, t, inits);
9227 /* Walk the BINFO and its bases. We walk in preorder so that as we
9228 initialize each vtable we can figure out at what offset the
9229 secondary vtable lies from the primary vtable. We can't use
9230 dfs_walk here because we need to iterate through bases of BINFO
9231 and RTTI_BINFO simultaneously. */
9232 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
9234 /* Skip virtual bases. */
9235 if (BINFO_VIRTUAL_P (base_binfo))
9236 continue;
9237 accumulate_vtbl_inits (base_binfo,
9238 BINFO_BASE_BINFO (orig_binfo, i),
9239 rtti_binfo, vtbl, t,
9240 inits);
9244 /* Called from accumulate_vtbl_inits. Adds the initializers for the
9245 BINFO vtable to L. */
9247 static void
9248 dfs_accumulate_vtbl_inits (tree binfo,
9249 tree orig_binfo,
9250 tree rtti_binfo,
9251 tree orig_vtbl,
9252 tree t,
9253 vec<constructor_elt, va_gc> **l)
9255 tree vtbl = NULL_TREE;
9256 int ctor_vtbl_p = !SAME_BINFO_TYPE_P (BINFO_TYPE (rtti_binfo), t);
9257 int n_inits;
9259 if (ctor_vtbl_p
9260 && BINFO_VIRTUAL_P (orig_binfo) && BINFO_PRIMARY_P (orig_binfo))
9262 /* In the hierarchy of BINFO_TYPE (RTTI_BINFO), this is a
9263 primary virtual base. If it is not the same primary in
9264 the hierarchy of T, we'll need to generate a ctor vtable
9265 for it, to place at its location in T. If it is the same
9266 primary, we still need a VTT entry for the vtable, but it
9267 should point to the ctor vtable for the base it is a
9268 primary for within the sub-hierarchy of RTTI_BINFO.
9270 There are three possible cases:
9272 1) We are in the same place.
9273 2) We are a primary base within a lost primary virtual base of
9274 RTTI_BINFO.
9275 3) We are primary to something not a base of RTTI_BINFO. */
9277 tree b;
9278 tree last = NULL_TREE;
9280 /* First, look through the bases we are primary to for RTTI_BINFO
9281 or a virtual base. */
9282 b = binfo;
9283 while (BINFO_PRIMARY_P (b))
9285 b = BINFO_INHERITANCE_CHAIN (b);
9286 last = b;
9287 if (BINFO_VIRTUAL_P (b) || b == rtti_binfo)
9288 goto found;
9290 /* If we run out of primary links, keep looking down our
9291 inheritance chain; we might be an indirect primary. */
9292 for (b = last; b; b = BINFO_INHERITANCE_CHAIN (b))
9293 if (BINFO_VIRTUAL_P (b) || b == rtti_binfo)
9294 break;
9295 found:
9297 /* If we found RTTI_BINFO, this is case 1. If we found a virtual
9298 base B and it is a base of RTTI_BINFO, this is case 2. In
9299 either case, we share our vtable with LAST, i.e. the
9300 derived-most base within B of which we are a primary. */
9301 if (b == rtti_binfo
9302 || (b && binfo_for_vbase (BINFO_TYPE (b), BINFO_TYPE (rtti_binfo))))
9303 /* Just set our BINFO_VTABLE to point to LAST, as we may not have
9304 set LAST's BINFO_VTABLE yet. We'll extract the actual vptr in
9305 binfo_ctor_vtable after everything's been set up. */
9306 vtbl = last;
9308 /* Otherwise, this is case 3 and we get our own. */
9310 else if (!BINFO_NEW_VTABLE_MARKED (orig_binfo))
9311 return;
9313 n_inits = vec_safe_length (*l);
9315 if (!vtbl)
9317 tree index;
9318 int non_fn_entries;
9320 /* Add the initializer for this vtable. */
9321 build_vtbl_initializer (binfo, orig_binfo, t, rtti_binfo,
9322 &non_fn_entries, l);
9324 /* Figure out the position to which the VPTR should point. */
9325 vtbl = build1 (ADDR_EXPR, vtbl_ptr_type_node, orig_vtbl);
9326 index = size_binop (MULT_EXPR,
9327 TYPE_SIZE_UNIT (vtable_entry_type),
9328 size_int (non_fn_entries + n_inits));
9329 vtbl = fold_build_pointer_plus (vtbl, index);
9332 if (ctor_vtbl_p)
9333 /* For a construction vtable, we can't overwrite BINFO_VTABLE.
9334 So, we make a TREE_LIST. Later, dfs_fixup_binfo_vtbls will
9335 straighten this out. */
9336 BINFO_VTABLE (binfo) = tree_cons (rtti_binfo, vtbl, BINFO_VTABLE (binfo));
9337 else if (BINFO_PRIMARY_P (binfo) && BINFO_VIRTUAL_P (binfo))
9338 /* Throw away any unneeded intializers. */
9339 (*l)->truncate (n_inits);
9340 else
9341 /* For an ordinary vtable, set BINFO_VTABLE. */
9342 BINFO_VTABLE (binfo) = vtbl;
9345 static GTY(()) tree abort_fndecl_addr;
9347 /* Construct the initializer for BINFO's virtual function table. BINFO
9348 is part of the hierarchy dominated by T. If we're building a
9349 construction vtable, the ORIG_BINFO is the binfo we should use to
9350 find the actual function pointers to put in the vtable - but they
9351 can be overridden on the path to most-derived in the graph that
9352 ORIG_BINFO belongs. Otherwise,
9353 ORIG_BINFO should be the same as BINFO. The RTTI_BINFO is the
9354 BINFO that should be indicated by the RTTI information in the
9355 vtable; it will be a base class of T, rather than T itself, if we
9356 are building a construction vtable.
9358 The value returned is a TREE_LIST suitable for wrapping in a
9359 CONSTRUCTOR to use as the DECL_INITIAL for a vtable. If
9360 NON_FN_ENTRIES_P is not NULL, *NON_FN_ENTRIES_P is set to the
9361 number of non-function entries in the vtable.
9363 It might seem that this function should never be called with a
9364 BINFO for which BINFO_PRIMARY_P holds, the vtable for such a
9365 base is always subsumed by a derived class vtable. However, when
9366 we are building construction vtables, we do build vtables for
9367 primary bases; we need these while the primary base is being
9368 constructed. */
9370 static void
9371 build_vtbl_initializer (tree binfo,
9372 tree orig_binfo,
9373 tree t,
9374 tree rtti_binfo,
9375 int* non_fn_entries_p,
9376 vec<constructor_elt, va_gc> **inits)
9378 tree v;
9379 vtbl_init_data vid;
9380 unsigned ix, jx;
9381 tree vbinfo;
9382 vec<tree, va_gc> *vbases;
9383 constructor_elt *e;
9385 /* Initialize VID. */
9386 memset (&vid, 0, sizeof (vid));
9387 vid.binfo = binfo;
9388 vid.derived = t;
9389 vid.rtti_binfo = rtti_binfo;
9390 vid.primary_vtbl_p = SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), t);
9391 vid.ctor_vtbl_p = !SAME_BINFO_TYPE_P (BINFO_TYPE (rtti_binfo), t);
9392 vid.generate_vcall_entries = true;
9393 /* The first vbase or vcall offset is at index -3 in the vtable. */
9394 vid.index = ssize_int(-3 * TARGET_VTABLE_DATA_ENTRY_DISTANCE);
9396 /* Add entries to the vtable for RTTI. */
9397 build_rtti_vtbl_entries (binfo, &vid);
9399 /* Create an array for keeping track of the functions we've
9400 processed. When we see multiple functions with the same
9401 signature, we share the vcall offsets. */
9402 vec_alloc (vid.fns, 32);
9403 /* Add the vcall and vbase offset entries. */
9404 build_vcall_and_vbase_vtbl_entries (binfo, &vid);
9406 /* Clear BINFO_VTABLE_PATH_MARKED; it's set by
9407 build_vbase_offset_vtbl_entries. */
9408 for (vbases = CLASSTYPE_VBASECLASSES (t), ix = 0;
9409 vec_safe_iterate (vbases, ix, &vbinfo); ix++)
9410 BINFO_VTABLE_PATH_MARKED (vbinfo) = 0;
9412 /* If the target requires padding between data entries, add that now. */
9413 if (TARGET_VTABLE_DATA_ENTRY_DISTANCE > 1)
9415 int n_entries = vec_safe_length (vid.inits);
9417 vec_safe_grow (vid.inits, TARGET_VTABLE_DATA_ENTRY_DISTANCE * n_entries);
9419 /* Move data entries into their new positions and add padding
9420 after the new positions. Iterate backwards so we don't
9421 overwrite entries that we would need to process later. */
9422 for (ix = n_entries - 1;
9423 vid.inits->iterate (ix, &e);
9424 ix--)
9426 int j;
9427 int new_position = (TARGET_VTABLE_DATA_ENTRY_DISTANCE * ix
9428 + (TARGET_VTABLE_DATA_ENTRY_DISTANCE - 1));
9430 (*vid.inits)[new_position] = *e;
9432 for (j = 1; j < TARGET_VTABLE_DATA_ENTRY_DISTANCE; ++j)
9434 constructor_elt *f = &(*vid.inits)[new_position - j];
9435 f->index = NULL_TREE;
9436 f->value = build1 (NOP_EXPR, vtable_entry_type,
9437 null_pointer_node);
9442 if (non_fn_entries_p)
9443 *non_fn_entries_p = vec_safe_length (vid.inits);
9445 /* The initializers for virtual functions were built up in reverse
9446 order. Straighten them out and add them to the running list in one
9447 step. */
9448 jx = vec_safe_length (*inits);
9449 vec_safe_grow (*inits, jx + vid.inits->length ());
9451 for (ix = vid.inits->length () - 1;
9452 vid.inits->iterate (ix, &e);
9453 ix--, jx++)
9454 (**inits)[jx] = *e;
9456 /* Go through all the ordinary virtual functions, building up
9457 initializers. */
9458 for (v = BINFO_VIRTUALS (orig_binfo); v; v = TREE_CHAIN (v))
9460 tree delta;
9461 tree vcall_index;
9462 tree fn, fn_original;
9463 tree init = NULL_TREE;
9465 fn = BV_FN (v);
9466 fn_original = fn;
9467 if (DECL_THUNK_P (fn))
9469 if (!DECL_NAME (fn))
9470 finish_thunk (fn);
9471 if (THUNK_ALIAS (fn))
9473 fn = THUNK_ALIAS (fn);
9474 BV_FN (v) = fn;
9476 fn_original = THUNK_TARGET (fn);
9479 /* If the only definition of this function signature along our
9480 primary base chain is from a lost primary, this vtable slot will
9481 never be used, so just zero it out. This is important to avoid
9482 requiring extra thunks which cannot be generated with the function.
9484 We first check this in update_vtable_entry_for_fn, so we handle
9485 restored primary bases properly; we also need to do it here so we
9486 zero out unused slots in ctor vtables, rather than filling them
9487 with erroneous values (though harmless, apart from relocation
9488 costs). */
9489 if (BV_LOST_PRIMARY (v))
9490 init = size_zero_node;
9492 if (! init)
9494 /* Pull the offset for `this', and the function to call, out of
9495 the list. */
9496 delta = BV_DELTA (v);
9497 vcall_index = BV_VCALL_INDEX (v);
9499 gcc_assert (TREE_CODE (delta) == INTEGER_CST);
9500 gcc_assert (TREE_CODE (fn) == FUNCTION_DECL);
9502 /* You can't call an abstract virtual function; it's abstract.
9503 So, we replace these functions with __pure_virtual. */
9504 if (DECL_PURE_VIRTUAL_P (fn_original))
9506 fn = abort_fndecl;
9507 if (!TARGET_VTABLE_USES_DESCRIPTORS)
9509 if (abort_fndecl_addr == NULL)
9510 abort_fndecl_addr
9511 = fold_convert (vfunc_ptr_type_node,
9512 build_fold_addr_expr (fn));
9513 init = abort_fndecl_addr;
9516 /* Likewise for deleted virtuals. */
9517 else if (DECL_DELETED_FN (fn_original))
9519 fn = get_identifier ("__cxa_deleted_virtual");
9520 if (!get_global_value_if_present (fn, &fn))
9521 fn = push_library_fn (fn, (build_function_type_list
9522 (void_type_node, NULL_TREE)),
9523 NULL_TREE, ECF_NORETURN);
9524 if (!TARGET_VTABLE_USES_DESCRIPTORS)
9525 init = fold_convert (vfunc_ptr_type_node,
9526 build_fold_addr_expr (fn));
9528 else
9530 if (!integer_zerop (delta) || vcall_index)
9532 fn = make_thunk (fn, /*this_adjusting=*/1, delta, vcall_index);
9533 if (!DECL_NAME (fn))
9534 finish_thunk (fn);
9536 /* Take the address of the function, considering it to be of an
9537 appropriate generic type. */
9538 if (!TARGET_VTABLE_USES_DESCRIPTORS)
9539 init = fold_convert (vfunc_ptr_type_node,
9540 build_fold_addr_expr (fn));
9541 /* Don't refer to a virtual destructor from a constructor
9542 vtable or a vtable for an abstract class, since destroying
9543 an object under construction is undefined behavior and we
9544 don't want it to be considered a candidate for speculative
9545 devirtualization. But do create the thunk for ABI
9546 compliance. */
9547 if (DECL_DESTRUCTOR_P (fn_original)
9548 && (CLASSTYPE_PURE_VIRTUALS (DECL_CONTEXT (fn_original))
9549 || orig_binfo != binfo))
9550 init = size_zero_node;
9554 /* And add it to the chain of initializers. */
9555 if (TARGET_VTABLE_USES_DESCRIPTORS)
9557 int i;
9558 if (init == size_zero_node)
9559 for (i = 0; i < TARGET_VTABLE_USES_DESCRIPTORS; ++i)
9560 CONSTRUCTOR_APPEND_ELT (*inits, NULL_TREE, init);
9561 else
9562 for (i = 0; i < TARGET_VTABLE_USES_DESCRIPTORS; ++i)
9564 tree fdesc = build2 (FDESC_EXPR, vfunc_ptr_type_node,
9565 fn, build_int_cst (NULL_TREE, i));
9566 TREE_CONSTANT (fdesc) = 1;
9568 CONSTRUCTOR_APPEND_ELT (*inits, NULL_TREE, fdesc);
9571 else
9572 CONSTRUCTOR_APPEND_ELT (*inits, NULL_TREE, init);
9576 /* Adds to vid->inits the initializers for the vbase and vcall
9577 offsets in BINFO, which is in the hierarchy dominated by T. */
9579 static void
9580 build_vcall_and_vbase_vtbl_entries (tree binfo, vtbl_init_data* vid)
9582 tree b;
9584 /* If this is a derived class, we must first create entries
9585 corresponding to the primary base class. */
9586 b = get_primary_binfo (binfo);
9587 if (b)
9588 build_vcall_and_vbase_vtbl_entries (b, vid);
9590 /* Add the vbase entries for this base. */
9591 build_vbase_offset_vtbl_entries (binfo, vid);
9592 /* Add the vcall entries for this base. */
9593 build_vcall_offset_vtbl_entries (binfo, vid);
9596 /* Returns the initializers for the vbase offset entries in the vtable
9597 for BINFO (which is part of the class hierarchy dominated by T), in
9598 reverse order. VBASE_OFFSET_INDEX gives the vtable index
9599 where the next vbase offset will go. */
9601 static void
9602 build_vbase_offset_vtbl_entries (tree binfo, vtbl_init_data* vid)
9604 tree vbase;
9605 tree t;
9606 tree non_primary_binfo;
9608 /* If there are no virtual baseclasses, then there is nothing to
9609 do. */
9610 if (!CLASSTYPE_VBASECLASSES (BINFO_TYPE (binfo)))
9611 return;
9613 t = vid->derived;
9615 /* We might be a primary base class. Go up the inheritance hierarchy
9616 until we find the most derived class of which we are a primary base:
9617 it is the offset of that which we need to use. */
9618 non_primary_binfo = binfo;
9619 while (BINFO_INHERITANCE_CHAIN (non_primary_binfo))
9621 tree b;
9623 /* If we have reached a virtual base, then it must be a primary
9624 base (possibly multi-level) of vid->binfo, or we wouldn't
9625 have called build_vcall_and_vbase_vtbl_entries for it. But it
9626 might be a lost primary, so just skip down to vid->binfo. */
9627 if (BINFO_VIRTUAL_P (non_primary_binfo))
9629 non_primary_binfo = vid->binfo;
9630 break;
9633 b = BINFO_INHERITANCE_CHAIN (non_primary_binfo);
9634 if (get_primary_binfo (b) != non_primary_binfo)
9635 break;
9636 non_primary_binfo = b;
9639 /* Go through the virtual bases, adding the offsets. */
9640 for (vbase = TYPE_BINFO (BINFO_TYPE (binfo));
9641 vbase;
9642 vbase = TREE_CHAIN (vbase))
9644 tree b;
9645 tree delta;
9647 if (!BINFO_VIRTUAL_P (vbase))
9648 continue;
9650 /* Find the instance of this virtual base in the complete
9651 object. */
9652 b = copied_binfo (vbase, binfo);
9654 /* If we've already got an offset for this virtual base, we
9655 don't need another one. */
9656 if (BINFO_VTABLE_PATH_MARKED (b))
9657 continue;
9658 BINFO_VTABLE_PATH_MARKED (b) = 1;
9660 /* Figure out where we can find this vbase offset. */
9661 delta = size_binop (MULT_EXPR,
9662 vid->index,
9663 fold_convert (ssizetype,
9664 TYPE_SIZE_UNIT (vtable_entry_type)));
9665 if (vid->primary_vtbl_p)
9666 BINFO_VPTR_FIELD (b) = delta;
9668 if (binfo != TYPE_BINFO (t))
9669 /* The vbase offset had better be the same. */
9670 gcc_assert (tree_int_cst_equal (delta, BINFO_VPTR_FIELD (vbase)));
9672 /* The next vbase will come at a more negative offset. */
9673 vid->index = size_binop (MINUS_EXPR, vid->index,
9674 ssize_int (TARGET_VTABLE_DATA_ENTRY_DISTANCE));
9676 /* The initializer is the delta from BINFO to this virtual base.
9677 The vbase offsets go in reverse inheritance-graph order, and
9678 we are walking in inheritance graph order so these end up in
9679 the right order. */
9680 delta = size_diffop_loc (input_location,
9681 BINFO_OFFSET (b), BINFO_OFFSET (non_primary_binfo));
9683 CONSTRUCTOR_APPEND_ELT (vid->inits, NULL_TREE,
9684 fold_build1_loc (input_location, NOP_EXPR,
9685 vtable_entry_type, delta));
9689 /* Adds the initializers for the vcall offset entries in the vtable
9690 for BINFO (which is part of the class hierarchy dominated by VID->DERIVED)
9691 to VID->INITS. */
9693 static void
9694 build_vcall_offset_vtbl_entries (tree binfo, vtbl_init_data* vid)
9696 /* We only need these entries if this base is a virtual base. We
9697 compute the indices -- but do not add to the vtable -- when
9698 building the main vtable for a class. */
9699 if (binfo == TYPE_BINFO (vid->derived)
9700 || (BINFO_VIRTUAL_P (binfo)
9701 /* If BINFO is RTTI_BINFO, then (since BINFO does not
9702 correspond to VID->DERIVED), we are building a primary
9703 construction virtual table. Since this is a primary
9704 virtual table, we do not need the vcall offsets for
9705 BINFO. */
9706 && binfo != vid->rtti_binfo))
9708 /* We need a vcall offset for each of the virtual functions in this
9709 vtable. For example:
9711 class A { virtual void f (); };
9712 class B1 : virtual public A { virtual void f (); };
9713 class B2 : virtual public A { virtual void f (); };
9714 class C: public B1, public B2 { virtual void f (); };
9716 A C object has a primary base of B1, which has a primary base of A. A
9717 C also has a secondary base of B2, which no longer has a primary base
9718 of A. So the B2-in-C construction vtable needs a secondary vtable for
9719 A, which will adjust the A* to a B2* to call f. We have no way of
9720 knowing what (or even whether) this offset will be when we define B2,
9721 so we store this "vcall offset" in the A sub-vtable and look it up in
9722 a "virtual thunk" for B2::f.
9724 We need entries for all the functions in our primary vtable and
9725 in our non-virtual bases' secondary vtables. */
9726 vid->vbase = binfo;
9727 /* If we are just computing the vcall indices -- but do not need
9728 the actual entries -- not that. */
9729 if (!BINFO_VIRTUAL_P (binfo))
9730 vid->generate_vcall_entries = false;
9731 /* Now, walk through the non-virtual bases, adding vcall offsets. */
9732 add_vcall_offset_vtbl_entries_r (binfo, vid);
9736 /* Build vcall offsets, starting with those for BINFO. */
9738 static void
9739 add_vcall_offset_vtbl_entries_r (tree binfo, vtbl_init_data* vid)
9741 int i;
9742 tree primary_binfo;
9743 tree base_binfo;
9745 /* Don't walk into virtual bases -- except, of course, for the
9746 virtual base for which we are building vcall offsets. Any
9747 primary virtual base will have already had its offsets generated
9748 through the recursion in build_vcall_and_vbase_vtbl_entries. */
9749 if (BINFO_VIRTUAL_P (binfo) && vid->vbase != binfo)
9750 return;
9752 /* If BINFO has a primary base, process it first. */
9753 primary_binfo = get_primary_binfo (binfo);
9754 if (primary_binfo)
9755 add_vcall_offset_vtbl_entries_r (primary_binfo, vid);
9757 /* Add BINFO itself to the list. */
9758 add_vcall_offset_vtbl_entries_1 (binfo, vid);
9760 /* Scan the non-primary bases of BINFO. */
9761 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
9762 if (base_binfo != primary_binfo)
9763 add_vcall_offset_vtbl_entries_r (base_binfo, vid);
9766 /* Called from build_vcall_offset_vtbl_entries_r. */
9768 static void
9769 add_vcall_offset_vtbl_entries_1 (tree binfo, vtbl_init_data* vid)
9771 /* Make entries for the rest of the virtuals. */
9772 tree orig_fn;
9774 /* The ABI requires that the methods be processed in declaration
9775 order. */
9776 for (orig_fn = TYPE_METHODS (BINFO_TYPE (binfo));
9777 orig_fn;
9778 orig_fn = DECL_CHAIN (orig_fn))
9779 if (TREE_CODE (orig_fn) == FUNCTION_DECL && DECL_VINDEX (orig_fn))
9780 add_vcall_offset (orig_fn, binfo, vid);
9783 /* Add a vcall offset entry for ORIG_FN to the vtable. */
9785 static void
9786 add_vcall_offset (tree orig_fn, tree binfo, vtbl_init_data *vid)
9788 size_t i;
9789 tree vcall_offset;
9790 tree derived_entry;
9792 /* If there is already an entry for a function with the same
9793 signature as FN, then we do not need a second vcall offset.
9794 Check the list of functions already present in the derived
9795 class vtable. */
9796 FOR_EACH_VEC_SAFE_ELT (vid->fns, i, derived_entry)
9798 if (same_signature_p (derived_entry, orig_fn)
9799 /* We only use one vcall offset for virtual destructors,
9800 even though there are two virtual table entries. */
9801 || (DECL_DESTRUCTOR_P (derived_entry)
9802 && DECL_DESTRUCTOR_P (orig_fn)))
9803 return;
9806 /* If we are building these vcall offsets as part of building
9807 the vtable for the most derived class, remember the vcall
9808 offset. */
9809 if (vid->binfo == TYPE_BINFO (vid->derived))
9811 tree_pair_s elt = {orig_fn, vid->index};
9812 vec_safe_push (CLASSTYPE_VCALL_INDICES (vid->derived), elt);
9815 /* The next vcall offset will be found at a more negative
9816 offset. */
9817 vid->index = size_binop (MINUS_EXPR, vid->index,
9818 ssize_int (TARGET_VTABLE_DATA_ENTRY_DISTANCE));
9820 /* Keep track of this function. */
9821 vec_safe_push (vid->fns, orig_fn);
9823 if (vid->generate_vcall_entries)
9825 tree base;
9826 tree fn;
9828 /* Find the overriding function. */
9829 fn = find_final_overrider (vid->rtti_binfo, binfo, orig_fn);
9830 if (fn == error_mark_node)
9831 vcall_offset = build_zero_cst (vtable_entry_type);
9832 else
9834 base = TREE_VALUE (fn);
9836 /* The vbase we're working on is a primary base of
9837 vid->binfo. But it might be a lost primary, so its
9838 BINFO_OFFSET might be wrong, so we just use the
9839 BINFO_OFFSET from vid->binfo. */
9840 vcall_offset = size_diffop_loc (input_location,
9841 BINFO_OFFSET (base),
9842 BINFO_OFFSET (vid->binfo));
9843 vcall_offset = fold_build1_loc (input_location,
9844 NOP_EXPR, vtable_entry_type,
9845 vcall_offset);
9847 /* Add the initializer to the vtable. */
9848 CONSTRUCTOR_APPEND_ELT (vid->inits, NULL_TREE, vcall_offset);
9852 /* Return vtbl initializers for the RTTI entries corresponding to the
9853 BINFO's vtable. The RTTI entries should indicate the object given
9854 by VID->rtti_binfo. */
9856 static void
9857 build_rtti_vtbl_entries (tree binfo, vtbl_init_data* vid)
9859 tree b;
9860 tree t;
9861 tree offset;
9862 tree decl;
9863 tree init;
9865 t = BINFO_TYPE (vid->rtti_binfo);
9867 /* To find the complete object, we will first convert to our most
9868 primary base, and then add the offset in the vtbl to that value. */
9869 b = most_primary_binfo (binfo);
9870 offset = size_diffop_loc (input_location,
9871 BINFO_OFFSET (vid->rtti_binfo), BINFO_OFFSET (b));
9873 /* The second entry is the address of the typeinfo object. */
9874 if (flag_rtti)
9875 decl = build_address (get_tinfo_decl (t));
9876 else
9877 decl = integer_zero_node;
9879 /* Convert the declaration to a type that can be stored in the
9880 vtable. */
9881 init = build_nop (vfunc_ptr_type_node, decl);
9882 CONSTRUCTOR_APPEND_ELT (vid->inits, NULL_TREE, init);
9884 /* Add the offset-to-top entry. It comes earlier in the vtable than
9885 the typeinfo entry. Convert the offset to look like a
9886 function pointer, so that we can put it in the vtable. */
9887 init = build_nop (vfunc_ptr_type_node, offset);
9888 CONSTRUCTOR_APPEND_ELT (vid->inits, NULL_TREE, init);
9891 /* TRUE iff TYPE is uniquely derived from PARENT. Ignores
9892 accessibility. */
9894 bool
9895 uniquely_derived_from_p (tree parent, tree type)
9897 tree base = lookup_base (type, parent, ba_unique, NULL, tf_none);
9898 return base && base != error_mark_node;
9901 /* TRUE iff TYPE is publicly & uniquely derived from PARENT. */
9903 bool
9904 publicly_uniquely_derived_p (tree parent, tree type)
9906 tree base = lookup_base (type, parent, ba_ignore_scope | ba_check,
9907 NULL, tf_none);
9908 return base && base != error_mark_node;
9911 /* CTX1 and CTX2 are declaration contexts. Return the innermost common
9912 class between them, if any. */
9914 tree
9915 common_enclosing_class (tree ctx1, tree ctx2)
9917 if (!TYPE_P (ctx1) || !TYPE_P (ctx2))
9918 return NULL_TREE;
9919 gcc_assert (ctx1 == TYPE_MAIN_VARIANT (ctx1)
9920 && ctx2 == TYPE_MAIN_VARIANT (ctx2));
9921 if (ctx1 == ctx2)
9922 return ctx1;
9923 for (tree t = ctx1; TYPE_P (t); t = TYPE_CONTEXT (t))
9924 TYPE_MARKED_P (t) = true;
9925 tree found = NULL_TREE;
9926 for (tree t = ctx2; TYPE_P (t); t = TYPE_CONTEXT (t))
9927 if (TYPE_MARKED_P (t))
9929 found = t;
9930 break;
9932 for (tree t = ctx1; TYPE_P (t); t = TYPE_CONTEXT (t))
9933 TYPE_MARKED_P (t) = false;
9934 return found;
9937 #include "gt-cp-class.h"