* sv.po: Update.
[official-gcc.git] / gcc / cp / class.c
blobb705d6bba596760d5b5f27e9ecb27ae2e30a9f64
1 /* Functions related to building classes and their related objects.
2 Copyright (C) 1987-2016 Free Software Foundation, Inc.
3 Contributed by Michael Tiemann (tiemann@cygnus.com)
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
10 any later version.
12 GCC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
22 /* High-level class interface. */
24 #include "config.h"
25 #include "system.h"
26 #include "coretypes.h"
27 #include "target.h"
28 #include "cp-tree.h"
29 #include "stringpool.h"
30 #include "cgraph.h"
31 #include "stor-layout.h"
32 #include "attribs.h"
33 #include "flags.h"
34 #include "toplev.h"
35 #include "convert.h"
36 #include "dumpfile.h"
37 #include "gimplify.h"
38 #include "intl.h"
40 /* The number of nested classes being processed. If we are not in the
41 scope of any class, this is zero. */
43 int current_class_depth;
45 /* In order to deal with nested classes, we keep a stack of classes.
46 The topmost entry is the innermost class, and is the entry at index
47 CURRENT_CLASS_DEPTH */
49 typedef struct class_stack_node {
50 /* The name of the class. */
51 tree name;
53 /* The _TYPE node for the class. */
54 tree type;
56 /* The access specifier pending for new declarations in the scope of
57 this class. */
58 tree access;
60 /* If were defining TYPE, the names used in this class. */
61 splay_tree names_used;
63 /* Nonzero if this class is no longer open, because of a call to
64 push_to_top_level. */
65 size_t hidden;
66 }* class_stack_node_t;
68 struct vtbl_init_data
70 /* The base for which we're building initializers. */
71 tree binfo;
72 /* The type of the most-derived type. */
73 tree derived;
74 /* The binfo for the dynamic type. This will be TYPE_BINFO (derived),
75 unless ctor_vtbl_p is true. */
76 tree rtti_binfo;
77 /* The negative-index vtable initializers built up so far. These
78 are in order from least negative index to most negative index. */
79 vec<constructor_elt, va_gc> *inits;
80 /* The binfo for the virtual base for which we're building
81 vcall offset initializers. */
82 tree vbase;
83 /* The functions in vbase for which we have already provided vcall
84 offsets. */
85 vec<tree, va_gc> *fns;
86 /* The vtable index of the next vcall or vbase offset. */
87 tree index;
88 /* Nonzero if we are building the initializer for the primary
89 vtable. */
90 int primary_vtbl_p;
91 /* Nonzero if we are building the initializer for a construction
92 vtable. */
93 int ctor_vtbl_p;
94 /* True when adding vcall offset entries to the vtable. False when
95 merely computing the indices. */
96 bool generate_vcall_entries;
99 /* The type of a function passed to walk_subobject_offsets. */
100 typedef int (*subobject_offset_fn) (tree, tree, splay_tree);
102 /* The stack itself. This is a dynamically resized array. The
103 number of elements allocated is CURRENT_CLASS_STACK_SIZE. */
104 static int current_class_stack_size;
105 static class_stack_node_t current_class_stack;
107 /* The size of the largest empty class seen in this translation unit. */
108 static GTY (()) tree sizeof_biggest_empty_class;
110 /* An array of all local classes present in this translation unit, in
111 declaration order. */
112 vec<tree, va_gc> *local_classes;
114 static tree get_vfield_name (tree);
115 static void finish_struct_anon (tree);
116 static tree get_vtable_name (tree);
117 static void get_basefndecls (tree, tree, vec<tree> *);
118 static int build_primary_vtable (tree, tree);
119 static int build_secondary_vtable (tree);
120 static void finish_vtbls (tree);
121 static void modify_vtable_entry (tree, tree, tree, tree, tree *);
122 static void finish_struct_bits (tree);
123 static int alter_access (tree, tree, tree);
124 static void handle_using_decl (tree, tree);
125 static tree dfs_modify_vtables (tree, void *);
126 static tree modify_all_vtables (tree, tree);
127 static void determine_primary_bases (tree);
128 static void finish_struct_methods (tree);
129 static void maybe_warn_about_overly_private_class (tree);
130 static int method_name_cmp (const void *, const void *);
131 static int resort_method_name_cmp (const void *, const void *);
132 static void add_implicitly_declared_members (tree, tree*, int, int);
133 static tree fixed_type_or_null (tree, int *, int *);
134 static tree build_simple_base_path (tree expr, tree binfo);
135 static tree build_vtbl_ref_1 (tree, tree);
136 static void build_vtbl_initializer (tree, tree, tree, tree, int *,
137 vec<constructor_elt, va_gc> **);
138 static int count_fields (tree);
139 static int add_fields_to_record_type (tree, struct sorted_fields_type*, int);
140 static void insert_into_classtype_sorted_fields (tree, tree, int);
141 static bool check_bitfield_decl (tree);
142 static void check_field_decl (tree, tree, int *, int *, int *);
143 static void check_field_decls (tree, tree *, int *, int *);
144 static tree *build_base_field (record_layout_info, tree, splay_tree, tree *);
145 static void build_base_fields (record_layout_info, splay_tree, tree *);
146 static void check_methods (tree);
147 static void remove_zero_width_bit_fields (tree);
148 static bool accessible_nvdtor_p (tree);
150 /* Used by find_flexarrays and related. */
151 struct flexmems_t;
152 static void find_flexarrays (tree, flexmems_t *);
153 static void diagnose_flexarrays (tree, const flexmems_t *);
154 static void check_flexarrays (tree, flexmems_t * = NULL);
155 static void check_bases (tree, int *, int *);
156 static void check_bases_and_members (tree);
157 static tree create_vtable_ptr (tree, tree *);
158 static void include_empty_classes (record_layout_info);
159 static void layout_class_type (tree, tree *);
160 static void propagate_binfo_offsets (tree, tree);
161 static void layout_virtual_bases (record_layout_info, splay_tree);
162 static void build_vbase_offset_vtbl_entries (tree, vtbl_init_data *);
163 static void add_vcall_offset_vtbl_entries_r (tree, vtbl_init_data *);
164 static void add_vcall_offset_vtbl_entries_1 (tree, vtbl_init_data *);
165 static void build_vcall_offset_vtbl_entries (tree, vtbl_init_data *);
166 static void add_vcall_offset (tree, tree, vtbl_init_data *);
167 static void layout_vtable_decl (tree, int);
168 static tree dfs_find_final_overrider_pre (tree, void *);
169 static tree dfs_find_final_overrider_post (tree, void *);
170 static tree find_final_overrider (tree, tree, tree);
171 static int make_new_vtable (tree, tree);
172 static tree get_primary_binfo (tree);
173 static int maybe_indent_hierarchy (FILE *, int, int);
174 static tree dump_class_hierarchy_r (FILE *, int, tree, tree, int);
175 static void dump_class_hierarchy (tree);
176 static void dump_class_hierarchy_1 (FILE *, int, tree);
177 static void dump_array (FILE *, tree);
178 static void dump_vtable (tree, tree, tree);
179 static void dump_vtt (tree, tree);
180 static void dump_thunk (FILE *, int, tree);
181 static tree build_vtable (tree, tree, tree);
182 static void initialize_vtable (tree, vec<constructor_elt, va_gc> *);
183 static void layout_nonempty_base_or_field (record_layout_info,
184 tree, tree, splay_tree);
185 static tree end_of_class (tree, int);
186 static bool layout_empty_base (record_layout_info, tree, tree, splay_tree);
187 static void accumulate_vtbl_inits (tree, tree, tree, tree, tree,
188 vec<constructor_elt, va_gc> **);
189 static void dfs_accumulate_vtbl_inits (tree, tree, tree, tree, tree,
190 vec<constructor_elt, va_gc> **);
191 static void build_rtti_vtbl_entries (tree, vtbl_init_data *);
192 static void build_vcall_and_vbase_vtbl_entries (tree, vtbl_init_data *);
193 static void clone_constructors_and_destructors (tree);
194 static tree build_clone (tree, tree);
195 static void update_vtable_entry_for_fn (tree, tree, tree, tree *, unsigned);
196 static void build_ctor_vtbl_group (tree, tree);
197 static void build_vtt (tree);
198 static tree binfo_ctor_vtable (tree);
199 static void build_vtt_inits (tree, tree, vec<constructor_elt, va_gc> **,
200 tree *);
201 static tree dfs_build_secondary_vptr_vtt_inits (tree, void *);
202 static tree dfs_fixup_binfo_vtbls (tree, void *);
203 static int record_subobject_offset (tree, tree, splay_tree);
204 static int check_subobject_offset (tree, tree, splay_tree);
205 static int walk_subobject_offsets (tree, subobject_offset_fn,
206 tree, splay_tree, tree, int);
207 static void record_subobject_offsets (tree, tree, splay_tree, bool);
208 static int layout_conflict_p (tree, tree, splay_tree, int);
209 static int splay_tree_compare_integer_csts (splay_tree_key k1,
210 splay_tree_key k2);
211 static void warn_about_ambiguous_bases (tree);
212 static bool type_requires_array_cookie (tree);
213 static bool base_derived_from (tree, tree);
214 static int empty_base_at_nonzero_offset_p (tree, tree, splay_tree);
215 static tree end_of_base (tree);
216 static tree get_vcall_index (tree, tree);
218 /* Variables shared between class.c and call.c. */
220 int n_vtables = 0;
221 int n_vtable_entries = 0;
222 int n_vtable_searches = 0;
223 int n_vtable_elems = 0;
224 int n_convert_harshness = 0;
225 int n_compute_conversion_costs = 0;
226 int n_inner_fields_searched = 0;
228 /* Convert to or from a base subobject. EXPR is an expression of type
229 `A' or `A*', an expression of type `B' or `B*' is returned. To
230 convert A to a base B, CODE is PLUS_EXPR and BINFO is the binfo for
231 the B base instance within A. To convert base A to derived B, CODE
232 is MINUS_EXPR and BINFO is the binfo for the A instance within B.
233 In this latter case, A must not be a morally virtual base of B.
234 NONNULL is true if EXPR is known to be non-NULL (this is only
235 needed when EXPR is of pointer type). CV qualifiers are preserved
236 from EXPR. */
238 tree
239 build_base_path (enum tree_code code,
240 tree expr,
241 tree binfo,
242 int nonnull,
243 tsubst_flags_t complain)
245 tree v_binfo = NULL_TREE;
246 tree d_binfo = NULL_TREE;
247 tree probe;
248 tree offset;
249 tree target_type;
250 tree null_test = NULL;
251 tree ptr_target_type;
252 int fixed_type_p;
253 int want_pointer = TYPE_PTR_P (TREE_TYPE (expr));
254 bool has_empty = false;
255 bool virtual_access;
256 bool rvalue = false;
258 if (expr == error_mark_node || binfo == error_mark_node || !binfo)
259 return error_mark_node;
261 for (probe = binfo; probe; probe = BINFO_INHERITANCE_CHAIN (probe))
263 d_binfo = probe;
264 if (is_empty_class (BINFO_TYPE (probe)))
265 has_empty = true;
266 if (!v_binfo && BINFO_VIRTUAL_P (probe))
267 v_binfo = probe;
270 probe = TYPE_MAIN_VARIANT (TREE_TYPE (expr));
271 if (want_pointer)
272 probe = TYPE_MAIN_VARIANT (TREE_TYPE (probe));
274 if (code == PLUS_EXPR
275 && !SAME_BINFO_TYPE_P (BINFO_TYPE (d_binfo), probe))
277 /* This can happen when adjust_result_of_qualified_name_lookup can't
278 find a unique base binfo in a call to a member function. We
279 couldn't give the diagnostic then since we might have been calling
280 a static member function, so we do it now. */
281 if (complain & tf_error)
283 tree base = lookup_base (probe, BINFO_TYPE (d_binfo),
284 ba_unique, NULL, complain);
285 gcc_assert (base == error_mark_node);
287 return error_mark_node;
290 gcc_assert ((code == MINUS_EXPR
291 && SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), probe))
292 || code == PLUS_EXPR);
294 if (binfo == d_binfo)
295 /* Nothing to do. */
296 return expr;
298 if (code == MINUS_EXPR && v_binfo)
300 if (complain & tf_error)
302 if (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), BINFO_TYPE (v_binfo)))
304 if (want_pointer)
305 error ("cannot convert from pointer to base class %qT to "
306 "pointer to derived class %qT because the base is "
307 "virtual", BINFO_TYPE (binfo), BINFO_TYPE (d_binfo));
308 else
309 error ("cannot convert from base class %qT to derived "
310 "class %qT because the base is virtual",
311 BINFO_TYPE (binfo), BINFO_TYPE (d_binfo));
313 else
315 if (want_pointer)
316 error ("cannot convert from pointer to base class %qT to "
317 "pointer to derived class %qT via virtual base %qT",
318 BINFO_TYPE (binfo), BINFO_TYPE (d_binfo),
319 BINFO_TYPE (v_binfo));
320 else
321 error ("cannot convert from base class %qT to derived "
322 "class %qT via virtual base %qT", BINFO_TYPE (binfo),
323 BINFO_TYPE (d_binfo), BINFO_TYPE (v_binfo));
326 return error_mark_node;
329 if (!want_pointer)
331 rvalue = !real_lvalue_p (expr);
332 /* This must happen before the call to save_expr. */
333 expr = cp_build_addr_expr (expr, complain);
335 else
336 expr = mark_rvalue_use (expr);
338 offset = BINFO_OFFSET (binfo);
339 fixed_type_p = resolves_to_fixed_type_p (expr, &nonnull);
340 target_type = code == PLUS_EXPR ? BINFO_TYPE (binfo) : BINFO_TYPE (d_binfo);
341 /* TARGET_TYPE has been extracted from BINFO, and, is therefore always
342 cv-unqualified. Extract the cv-qualifiers from EXPR so that the
343 expression returned matches the input. */
344 target_type = cp_build_qualified_type
345 (target_type, cp_type_quals (TREE_TYPE (TREE_TYPE (expr))));
346 ptr_target_type = build_pointer_type (target_type);
348 /* Do we need to look in the vtable for the real offset? */
349 virtual_access = (v_binfo && fixed_type_p <= 0);
351 /* Don't bother with the calculations inside sizeof; they'll ICE if the
352 source type is incomplete and the pointer value doesn't matter. In a
353 template (even in instantiate_non_dependent_expr), we don't have vtables
354 set up properly yet, and the value doesn't matter there either; we're
355 just interested in the result of overload resolution. */
356 if (cp_unevaluated_operand != 0
357 || in_template_function ())
359 expr = build_nop (ptr_target_type, expr);
360 goto indout;
363 /* If we're in an NSDMI, we don't have the full constructor context yet
364 that we need for converting to a virtual base, so just build a stub
365 CONVERT_EXPR and expand it later in bot_replace. */
366 if (virtual_access && fixed_type_p < 0
367 && current_scope () != current_function_decl)
369 expr = build1 (CONVERT_EXPR, ptr_target_type, expr);
370 CONVERT_EXPR_VBASE_PATH (expr) = true;
371 goto indout;
374 /* Do we need to check for a null pointer? */
375 if (want_pointer && !nonnull)
377 /* If we know the conversion will not actually change the value
378 of EXPR, then we can avoid testing the expression for NULL.
379 We have to avoid generating a COMPONENT_REF for a base class
380 field, because other parts of the compiler know that such
381 expressions are always non-NULL. */
382 if (!virtual_access && integer_zerop (offset))
383 return build_nop (ptr_target_type, expr);
384 null_test = error_mark_node;
387 /* Protect against multiple evaluation if necessary. */
388 if (TREE_SIDE_EFFECTS (expr) && (null_test || virtual_access))
389 expr = save_expr (expr);
391 /* Now that we've saved expr, build the real null test. */
392 if (null_test)
394 tree zero = cp_convert (TREE_TYPE (expr), nullptr_node, complain);
395 null_test = fold_build2_loc (input_location, NE_EXPR, boolean_type_node,
396 expr, zero);
399 /* If this is a simple base reference, express it as a COMPONENT_REF. */
400 if (code == PLUS_EXPR && !virtual_access
401 /* We don't build base fields for empty bases, and they aren't very
402 interesting to the optimizers anyway. */
403 && !has_empty)
405 expr = cp_build_indirect_ref (expr, RO_NULL, complain);
406 expr = build_simple_base_path (expr, binfo);
407 if (rvalue)
408 expr = move (expr);
409 if (want_pointer)
410 expr = build_address (expr);
411 target_type = TREE_TYPE (expr);
412 goto out;
415 if (virtual_access)
417 /* Going via virtual base V_BINFO. We need the static offset
418 from V_BINFO to BINFO, and the dynamic offset from D_BINFO to
419 V_BINFO. That offset is an entry in D_BINFO's vtable. */
420 tree v_offset;
422 if (fixed_type_p < 0 && in_base_initializer)
424 /* In a base member initializer, we cannot rely on the
425 vtable being set up. We have to indirect via the
426 vtt_parm. */
427 tree t;
429 t = TREE_TYPE (TYPE_VFIELD (current_class_type));
430 t = build_pointer_type (t);
431 v_offset = fold_convert (t, current_vtt_parm);
432 v_offset = cp_build_indirect_ref (v_offset, RO_NULL, complain);
434 else
436 tree t = expr;
437 if ((flag_sanitize & SANITIZE_VPTR) && fixed_type_p == 0)
439 t = cp_ubsan_maybe_instrument_cast_to_vbase (input_location,
440 probe, expr);
441 if (t == NULL_TREE)
442 t = expr;
444 v_offset = build_vfield_ref (cp_build_indirect_ref (t, RO_NULL,
445 complain),
446 TREE_TYPE (TREE_TYPE (expr)));
449 if (v_offset == error_mark_node)
450 return error_mark_node;
452 v_offset = fold_build_pointer_plus (v_offset, BINFO_VPTR_FIELD (v_binfo));
453 v_offset = build1 (NOP_EXPR,
454 build_pointer_type (ptrdiff_type_node),
455 v_offset);
456 v_offset = cp_build_indirect_ref (v_offset, RO_NULL, complain);
457 TREE_CONSTANT (v_offset) = 1;
459 offset = convert_to_integer (ptrdiff_type_node,
460 size_diffop_loc (input_location, offset,
461 BINFO_OFFSET (v_binfo)));
463 if (!integer_zerop (offset))
464 v_offset = build2 (code, ptrdiff_type_node, v_offset, offset);
466 if (fixed_type_p < 0)
467 /* Negative fixed_type_p means this is a constructor or destructor;
468 virtual base layout is fixed in in-charge [cd]tors, but not in
469 base [cd]tors. */
470 offset = build3 (COND_EXPR, ptrdiff_type_node,
471 build2 (EQ_EXPR, boolean_type_node,
472 current_in_charge_parm, integer_zero_node),
473 v_offset,
474 convert_to_integer (ptrdiff_type_node,
475 BINFO_OFFSET (binfo)));
476 else
477 offset = v_offset;
480 if (want_pointer)
481 target_type = ptr_target_type;
483 expr = build1 (NOP_EXPR, ptr_target_type, expr);
485 if (!integer_zerop (offset))
487 offset = fold_convert (sizetype, offset);
488 if (code == MINUS_EXPR)
489 offset = fold_build1_loc (input_location, NEGATE_EXPR, sizetype, offset);
490 expr = fold_build_pointer_plus (expr, offset);
492 else
493 null_test = NULL;
495 indout:
496 if (!want_pointer)
498 expr = cp_build_indirect_ref (expr, RO_NULL, complain);
499 if (rvalue)
500 expr = move (expr);
503 out:
504 if (null_test)
505 expr = fold_build3_loc (input_location, COND_EXPR, target_type, null_test, expr,
506 build_zero_cst (target_type));
508 return expr;
511 /* Subroutine of build_base_path; EXPR and BINFO are as in that function.
512 Perform a derived-to-base conversion by recursively building up a
513 sequence of COMPONENT_REFs to the appropriate base fields. */
515 static tree
516 build_simple_base_path (tree expr, tree binfo)
518 tree type = BINFO_TYPE (binfo);
519 tree d_binfo = BINFO_INHERITANCE_CHAIN (binfo);
520 tree field;
522 if (d_binfo == NULL_TREE)
524 tree temp;
526 gcc_assert (TYPE_MAIN_VARIANT (TREE_TYPE (expr)) == type);
528 /* Transform `(a, b).x' into `(*(a, &b)).x', `(a ? b : c).x'
529 into `(*(a ? &b : &c)).x', and so on. A COND_EXPR is only
530 an lvalue in the front end; only _DECLs and _REFs are lvalues
531 in the back end. */
532 temp = unary_complex_lvalue (ADDR_EXPR, expr);
533 if (temp)
534 expr = cp_build_indirect_ref (temp, RO_NULL, tf_warning_or_error);
536 return expr;
539 /* Recurse. */
540 expr = build_simple_base_path (expr, d_binfo);
542 for (field = TYPE_FIELDS (BINFO_TYPE (d_binfo));
543 field; field = DECL_CHAIN (field))
544 /* Is this the base field created by build_base_field? */
545 if (TREE_CODE (field) == FIELD_DECL
546 && DECL_FIELD_IS_BASE (field)
547 && TREE_TYPE (field) == type
548 /* If we're looking for a field in the most-derived class,
549 also check the field offset; we can have two base fields
550 of the same type if one is an indirect virtual base and one
551 is a direct non-virtual base. */
552 && (BINFO_INHERITANCE_CHAIN (d_binfo)
553 || tree_int_cst_equal (byte_position (field),
554 BINFO_OFFSET (binfo))))
556 /* We don't use build_class_member_access_expr here, as that
557 has unnecessary checks, and more importantly results in
558 recursive calls to dfs_walk_once. */
559 int type_quals = cp_type_quals (TREE_TYPE (expr));
561 expr = build3 (COMPONENT_REF,
562 cp_build_qualified_type (type, type_quals),
563 expr, field, NULL_TREE);
564 /* Mark the expression const or volatile, as appropriate.
565 Even though we've dealt with the type above, we still have
566 to mark the expression itself. */
567 if (type_quals & TYPE_QUAL_CONST)
568 TREE_READONLY (expr) = 1;
569 if (type_quals & TYPE_QUAL_VOLATILE)
570 TREE_THIS_VOLATILE (expr) = 1;
572 return expr;
575 /* Didn't find the base field?!? */
576 gcc_unreachable ();
579 /* Convert OBJECT to the base TYPE. OBJECT is an expression whose
580 type is a class type or a pointer to a class type. In the former
581 case, TYPE is also a class type; in the latter it is another
582 pointer type. If CHECK_ACCESS is true, an error message is emitted
583 if TYPE is inaccessible. If OBJECT has pointer type, the value is
584 assumed to be non-NULL. */
586 tree
587 convert_to_base (tree object, tree type, bool check_access, bool nonnull,
588 tsubst_flags_t complain)
590 tree binfo;
591 tree object_type;
593 if (TYPE_PTR_P (TREE_TYPE (object)))
595 object_type = TREE_TYPE (TREE_TYPE (object));
596 type = TREE_TYPE (type);
598 else
599 object_type = TREE_TYPE (object);
601 binfo = lookup_base (object_type, type, check_access ? ba_check : ba_unique,
602 NULL, complain);
603 if (!binfo || binfo == error_mark_node)
604 return error_mark_node;
606 return build_base_path (PLUS_EXPR, object, binfo, nonnull, complain);
609 /* EXPR is an expression with unqualified class type. BASE is a base
610 binfo of that class type. Returns EXPR, converted to the BASE
611 type. This function assumes that EXPR is the most derived class;
612 therefore virtual bases can be found at their static offsets. */
614 tree
615 convert_to_base_statically (tree expr, tree base)
617 tree expr_type;
619 expr_type = TREE_TYPE (expr);
620 if (!SAME_BINFO_TYPE_P (BINFO_TYPE (base), expr_type))
622 /* If this is a non-empty base, use a COMPONENT_REF. */
623 if (!is_empty_class (BINFO_TYPE (base)))
624 return build_simple_base_path (expr, base);
626 /* We use fold_build2 and fold_convert below to simplify the trees
627 provided to the optimizers. It is not safe to call these functions
628 when processing a template because they do not handle C++-specific
629 trees. */
630 gcc_assert (!processing_template_decl);
631 expr = cp_build_addr_expr (expr, tf_warning_or_error);
632 if (!integer_zerop (BINFO_OFFSET (base)))
633 expr = fold_build_pointer_plus_loc (input_location,
634 expr, BINFO_OFFSET (base));
635 expr = fold_convert (build_pointer_type (BINFO_TYPE (base)), expr);
636 expr = build_fold_indirect_ref_loc (input_location, expr);
639 return expr;
643 tree
644 build_vfield_ref (tree datum, tree type)
646 tree vfield, vcontext;
648 if (datum == error_mark_node
649 /* Can happen in case of duplicate base types (c++/59082). */
650 || !TYPE_VFIELD (type))
651 return error_mark_node;
653 /* First, convert to the requested type. */
654 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (datum), type))
655 datum = convert_to_base (datum, type, /*check_access=*/false,
656 /*nonnull=*/true, tf_warning_or_error);
658 /* Second, the requested type may not be the owner of its own vptr.
659 If not, convert to the base class that owns it. We cannot use
660 convert_to_base here, because VCONTEXT may appear more than once
661 in the inheritance hierarchy of TYPE, and thus direct conversion
662 between the types may be ambiguous. Following the path back up
663 one step at a time via primary bases avoids the problem. */
664 vfield = TYPE_VFIELD (type);
665 vcontext = DECL_CONTEXT (vfield);
666 while (!same_type_ignoring_top_level_qualifiers_p (vcontext, type))
668 datum = build_simple_base_path (datum, CLASSTYPE_PRIMARY_BINFO (type));
669 type = TREE_TYPE (datum);
672 return build3 (COMPONENT_REF, TREE_TYPE (vfield), datum, vfield, NULL_TREE);
675 /* Given an object INSTANCE, return an expression which yields the
676 vtable element corresponding to INDEX. There are many special
677 cases for INSTANCE which we take care of here, mainly to avoid
678 creating extra tree nodes when we don't have to. */
680 static tree
681 build_vtbl_ref_1 (tree instance, tree idx)
683 tree aref;
684 tree vtbl = NULL_TREE;
686 /* Try to figure out what a reference refers to, and
687 access its virtual function table directly. */
689 int cdtorp = 0;
690 tree fixed_type = fixed_type_or_null (instance, NULL, &cdtorp);
692 tree basetype = non_reference (TREE_TYPE (instance));
694 if (fixed_type && !cdtorp)
696 tree binfo = lookup_base (fixed_type, basetype,
697 ba_unique, NULL, tf_none);
698 if (binfo && binfo != error_mark_node)
699 vtbl = unshare_expr (BINFO_VTABLE (binfo));
702 if (!vtbl)
703 vtbl = build_vfield_ref (instance, basetype);
705 aref = build_array_ref (input_location, vtbl, idx);
706 TREE_CONSTANT (aref) |= TREE_CONSTANT (vtbl) && TREE_CONSTANT (idx);
708 return aref;
711 tree
712 build_vtbl_ref (tree instance, tree idx)
714 tree aref = build_vtbl_ref_1 (instance, idx);
716 return aref;
719 /* Given a stable object pointer INSTANCE_PTR, return an expression which
720 yields a function pointer corresponding to vtable element INDEX. */
722 tree
723 build_vfn_ref (tree instance_ptr, tree idx)
725 tree aref;
727 aref = build_vtbl_ref_1 (cp_build_indirect_ref (instance_ptr, RO_NULL,
728 tf_warning_or_error),
729 idx);
731 /* When using function descriptors, the address of the
732 vtable entry is treated as a function pointer. */
733 if (TARGET_VTABLE_USES_DESCRIPTORS)
734 aref = build1 (NOP_EXPR, TREE_TYPE (aref),
735 cp_build_addr_expr (aref, tf_warning_or_error));
737 /* Remember this as a method reference, for later devirtualization. */
738 aref = build3 (OBJ_TYPE_REF, TREE_TYPE (aref), aref, instance_ptr, idx);
740 return aref;
743 /* Return the name of the virtual function table (as an IDENTIFIER_NODE)
744 for the given TYPE. */
746 static tree
747 get_vtable_name (tree type)
749 return mangle_vtbl_for_type (type);
752 /* DECL is an entity associated with TYPE, like a virtual table or an
753 implicitly generated constructor. Determine whether or not DECL
754 should have external or internal linkage at the object file
755 level. This routine does not deal with COMDAT linkage and other
756 similar complexities; it simply sets TREE_PUBLIC if it possible for
757 entities in other translation units to contain copies of DECL, in
758 the abstract. */
760 void
761 set_linkage_according_to_type (tree /*type*/, tree decl)
763 TREE_PUBLIC (decl) = 1;
764 determine_visibility (decl);
767 /* Create a VAR_DECL for a primary or secondary vtable for CLASS_TYPE.
768 (For a secondary vtable for B-in-D, CLASS_TYPE should be D, not B.)
769 Use NAME for the name of the vtable, and VTABLE_TYPE for its type. */
771 static tree
772 build_vtable (tree class_type, tree name, tree vtable_type)
774 tree decl;
776 decl = build_lang_decl (VAR_DECL, name, vtable_type);
777 /* vtable names are already mangled; give them their DECL_ASSEMBLER_NAME
778 now to avoid confusion in mangle_decl. */
779 SET_DECL_ASSEMBLER_NAME (decl, name);
780 DECL_CONTEXT (decl) = class_type;
781 DECL_ARTIFICIAL (decl) = 1;
782 TREE_STATIC (decl) = 1;
783 TREE_READONLY (decl) = 1;
784 DECL_VIRTUAL_P (decl) = 1;
785 DECL_ALIGN (decl) = TARGET_VTABLE_ENTRY_ALIGN;
786 DECL_USER_ALIGN (decl) = true;
787 DECL_VTABLE_OR_VTT_P (decl) = 1;
788 set_linkage_according_to_type (class_type, decl);
789 /* The vtable has not been defined -- yet. */
790 DECL_EXTERNAL (decl) = 1;
791 DECL_NOT_REALLY_EXTERN (decl) = 1;
793 /* Mark the VAR_DECL node representing the vtable itself as a
794 "gratuitous" one, thereby forcing dwarfout.c to ignore it. It
795 is rather important that such things be ignored because any
796 effort to actually generate DWARF for them will run into
797 trouble when/if we encounter code like:
799 #pragma interface
800 struct S { virtual void member (); };
802 because the artificial declaration of the vtable itself (as
803 manufactured by the g++ front end) will say that the vtable is
804 a static member of `S' but only *after* the debug output for
805 the definition of `S' has already been output. This causes
806 grief because the DWARF entry for the definition of the vtable
807 will try to refer back to an earlier *declaration* of the
808 vtable as a static member of `S' and there won't be one. We
809 might be able to arrange to have the "vtable static member"
810 attached to the member list for `S' before the debug info for
811 `S' get written (which would solve the problem) but that would
812 require more intrusive changes to the g++ front end. */
813 DECL_IGNORED_P (decl) = 1;
815 return decl;
818 /* Get the VAR_DECL of the vtable for TYPE. TYPE need not be polymorphic,
819 or even complete. If this does not exist, create it. If COMPLETE is
820 nonzero, then complete the definition of it -- that will render it
821 impossible to actually build the vtable, but is useful to get at those
822 which are known to exist in the runtime. */
824 tree
825 get_vtable_decl (tree type, int complete)
827 tree decl;
829 if (CLASSTYPE_VTABLES (type))
830 return CLASSTYPE_VTABLES (type);
832 decl = build_vtable (type, get_vtable_name (type), vtbl_type_node);
833 CLASSTYPE_VTABLES (type) = decl;
835 if (complete)
837 DECL_EXTERNAL (decl) = 1;
838 cp_finish_decl (decl, NULL_TREE, false, NULL_TREE, 0);
841 return decl;
844 /* Build the primary virtual function table for TYPE. If BINFO is
845 non-NULL, build the vtable starting with the initial approximation
846 that it is the same as the one which is the head of the association
847 list. Returns a nonzero value if a new vtable is actually
848 created. */
850 static int
851 build_primary_vtable (tree binfo, tree type)
853 tree decl;
854 tree virtuals;
856 decl = get_vtable_decl (type, /*complete=*/0);
858 if (binfo)
860 if (BINFO_NEW_VTABLE_MARKED (binfo))
861 /* We have already created a vtable for this base, so there's
862 no need to do it again. */
863 return 0;
865 virtuals = copy_list (BINFO_VIRTUALS (binfo));
866 TREE_TYPE (decl) = TREE_TYPE (get_vtbl_decl_for_binfo (binfo));
867 DECL_SIZE (decl) = TYPE_SIZE (TREE_TYPE (decl));
868 DECL_SIZE_UNIT (decl) = TYPE_SIZE_UNIT (TREE_TYPE (decl));
870 else
872 gcc_assert (TREE_TYPE (decl) == vtbl_type_node);
873 virtuals = NULL_TREE;
876 if (GATHER_STATISTICS)
878 n_vtables += 1;
879 n_vtable_elems += list_length (virtuals);
882 /* Initialize the association list for this type, based
883 on our first approximation. */
884 BINFO_VTABLE (TYPE_BINFO (type)) = decl;
885 BINFO_VIRTUALS (TYPE_BINFO (type)) = virtuals;
886 SET_BINFO_NEW_VTABLE_MARKED (TYPE_BINFO (type));
887 return 1;
890 /* Give BINFO a new virtual function table which is initialized
891 with a skeleton-copy of its original initialization. The only
892 entry that changes is the `delta' entry, so we can really
893 share a lot of structure.
895 FOR_TYPE is the most derived type which caused this table to
896 be needed.
898 Returns nonzero if we haven't met BINFO before.
900 The order in which vtables are built (by calling this function) for
901 an object must remain the same, otherwise a binary incompatibility
902 can result. */
904 static int
905 build_secondary_vtable (tree binfo)
907 if (BINFO_NEW_VTABLE_MARKED (binfo))
908 /* We already created a vtable for this base. There's no need to
909 do it again. */
910 return 0;
912 /* Remember that we've created a vtable for this BINFO, so that we
913 don't try to do so again. */
914 SET_BINFO_NEW_VTABLE_MARKED (binfo);
916 /* Make fresh virtual list, so we can smash it later. */
917 BINFO_VIRTUALS (binfo) = copy_list (BINFO_VIRTUALS (binfo));
919 /* Secondary vtables are laid out as part of the same structure as
920 the primary vtable. */
921 BINFO_VTABLE (binfo) = NULL_TREE;
922 return 1;
925 /* Create a new vtable for BINFO which is the hierarchy dominated by
926 T. Return nonzero if we actually created a new vtable. */
928 static int
929 make_new_vtable (tree t, tree binfo)
931 if (binfo == TYPE_BINFO (t))
932 /* In this case, it is *type*'s vtable we are modifying. We start
933 with the approximation that its vtable is that of the
934 immediate base class. */
935 return build_primary_vtable (binfo, t);
936 else
937 /* This is our very own copy of `basetype' to play with. Later,
938 we will fill in all the virtual functions that override the
939 virtual functions in these base classes which are not defined
940 by the current type. */
941 return build_secondary_vtable (binfo);
944 /* Make *VIRTUALS, an entry on the BINFO_VIRTUALS list for BINFO
945 (which is in the hierarchy dominated by T) list FNDECL as its
946 BV_FN. DELTA is the required constant adjustment from the `this'
947 pointer where the vtable entry appears to the `this' required when
948 the function is actually called. */
950 static void
951 modify_vtable_entry (tree t,
952 tree binfo,
953 tree fndecl,
954 tree delta,
955 tree *virtuals)
957 tree v;
959 v = *virtuals;
961 if (fndecl != BV_FN (v)
962 || !tree_int_cst_equal (delta, BV_DELTA (v)))
964 /* We need a new vtable for BINFO. */
965 if (make_new_vtable (t, binfo))
967 /* If we really did make a new vtable, we also made a copy
968 of the BINFO_VIRTUALS list. Now, we have to find the
969 corresponding entry in that list. */
970 *virtuals = BINFO_VIRTUALS (binfo);
971 while (BV_FN (*virtuals) != BV_FN (v))
972 *virtuals = TREE_CHAIN (*virtuals);
973 v = *virtuals;
976 BV_DELTA (v) = delta;
977 BV_VCALL_INDEX (v) = NULL_TREE;
978 BV_FN (v) = fndecl;
983 /* Add method METHOD to class TYPE. If USING_DECL is non-null, it is
984 the USING_DECL naming METHOD. Returns true if the method could be
985 added to the method vec. */
987 bool
988 add_method (tree type, tree method, tree using_decl)
990 unsigned slot;
991 tree overload;
992 bool template_conv_p = false;
993 bool conv_p;
994 vec<tree, va_gc> *method_vec;
995 bool complete_p;
996 bool insert_p = false;
997 tree current_fns;
998 tree fns;
1000 if (method == error_mark_node)
1001 return false;
1003 complete_p = COMPLETE_TYPE_P (type);
1004 conv_p = DECL_CONV_FN_P (method);
1005 if (conv_p)
1006 template_conv_p = (TREE_CODE (method) == TEMPLATE_DECL
1007 && DECL_TEMPLATE_CONV_FN_P (method));
1009 method_vec = CLASSTYPE_METHOD_VEC (type);
1010 if (!method_vec)
1012 /* Make a new method vector. We start with 8 entries. We must
1013 allocate at least two (for constructors and destructors), and
1014 we're going to end up with an assignment operator at some
1015 point as well. */
1016 vec_alloc (method_vec, 8);
1017 /* Create slots for constructors and destructors. */
1018 method_vec->quick_push (NULL_TREE);
1019 method_vec->quick_push (NULL_TREE);
1020 CLASSTYPE_METHOD_VEC (type) = method_vec;
1023 /* Maintain TYPE_HAS_USER_CONSTRUCTOR, etc. */
1024 grok_special_member_properties (method);
1026 /* Constructors and destructors go in special slots. */
1027 if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (method))
1028 slot = CLASSTYPE_CONSTRUCTOR_SLOT;
1029 else if (DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (method))
1031 slot = CLASSTYPE_DESTRUCTOR_SLOT;
1033 if (TYPE_FOR_JAVA (type))
1035 if (!DECL_ARTIFICIAL (method))
1036 error ("Java class %qT cannot have a destructor", type);
1037 else if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
1038 error ("Java class %qT cannot have an implicit non-trivial "
1039 "destructor",
1040 type);
1043 else
1045 tree m;
1047 insert_p = true;
1048 /* See if we already have an entry with this name. */
1049 for (slot = CLASSTYPE_FIRST_CONVERSION_SLOT;
1050 vec_safe_iterate (method_vec, slot, &m);
1051 ++slot)
1053 m = OVL_CURRENT (m);
1054 if (template_conv_p)
1056 if (TREE_CODE (m) == TEMPLATE_DECL
1057 && DECL_TEMPLATE_CONV_FN_P (m))
1058 insert_p = false;
1059 break;
1061 if (conv_p && !DECL_CONV_FN_P (m))
1062 break;
1063 if (DECL_NAME (m) == DECL_NAME (method))
1065 insert_p = false;
1066 break;
1068 if (complete_p
1069 && !DECL_CONV_FN_P (m)
1070 && DECL_NAME (m) > DECL_NAME (method))
1071 break;
1074 current_fns = insert_p ? NULL_TREE : (*method_vec)[slot];
1076 /* Check to see if we've already got this method. */
1077 for (fns = current_fns; fns; fns = OVL_NEXT (fns))
1079 tree fn = OVL_CURRENT (fns);
1080 tree fn_type;
1081 tree method_type;
1082 tree parms1;
1083 tree parms2;
1085 if (TREE_CODE (fn) != TREE_CODE (method))
1086 continue;
1088 /* [over.load] Member function declarations with the
1089 same name and the same parameter types cannot be
1090 overloaded if any of them is a static member
1091 function declaration.
1093 [over.load] Member function declarations with the same name and
1094 the same parameter-type-list as well as member function template
1095 declarations with the same name, the same parameter-type-list, and
1096 the same template parameter lists cannot be overloaded if any of
1097 them, but not all, have a ref-qualifier.
1099 [namespace.udecl] When a using-declaration brings names
1100 from a base class into a derived class scope, member
1101 functions in the derived class override and/or hide member
1102 functions with the same name and parameter types in a base
1103 class (rather than conflicting). */
1104 fn_type = TREE_TYPE (fn);
1105 method_type = TREE_TYPE (method);
1106 parms1 = TYPE_ARG_TYPES (fn_type);
1107 parms2 = TYPE_ARG_TYPES (method_type);
1109 /* Compare the quals on the 'this' parm. Don't compare
1110 the whole types, as used functions are treated as
1111 coming from the using class in overload resolution. */
1112 if (! DECL_STATIC_FUNCTION_P (fn)
1113 && ! DECL_STATIC_FUNCTION_P (method)
1114 /* Either both or neither need to be ref-qualified for
1115 differing quals to allow overloading. */
1116 && (FUNCTION_REF_QUALIFIED (fn_type)
1117 == FUNCTION_REF_QUALIFIED (method_type))
1118 && (type_memfn_quals (fn_type) != type_memfn_quals (method_type)
1119 || type_memfn_rqual (fn_type) != type_memfn_rqual (method_type)))
1120 continue;
1122 /* For templates, the return type and template parameters
1123 must be identical. */
1124 if (TREE_CODE (fn) == TEMPLATE_DECL
1125 && (!same_type_p (TREE_TYPE (fn_type),
1126 TREE_TYPE (method_type))
1127 || !comp_template_parms (DECL_TEMPLATE_PARMS (fn),
1128 DECL_TEMPLATE_PARMS (method))))
1129 continue;
1131 if (! DECL_STATIC_FUNCTION_P (fn))
1132 parms1 = TREE_CHAIN (parms1);
1133 if (! DECL_STATIC_FUNCTION_P (method))
1134 parms2 = TREE_CHAIN (parms2);
1136 if (compparms (parms1, parms2)
1137 && (!DECL_CONV_FN_P (fn)
1138 || same_type_p (TREE_TYPE (fn_type),
1139 TREE_TYPE (method_type)))
1140 && equivalently_constrained (fn, method))
1142 /* For function versions, their parms and types match
1143 but they are not duplicates. Record function versions
1144 as and when they are found. extern "C" functions are
1145 not treated as versions. */
1146 if (TREE_CODE (fn) == FUNCTION_DECL
1147 && TREE_CODE (method) == FUNCTION_DECL
1148 && !DECL_EXTERN_C_P (fn)
1149 && !DECL_EXTERN_C_P (method)
1150 && targetm.target_option.function_versions (fn, method))
1152 /* Mark functions as versions if necessary. Modify the mangled
1153 decl name if necessary. */
1154 if (!DECL_FUNCTION_VERSIONED (fn))
1156 DECL_FUNCTION_VERSIONED (fn) = 1;
1157 if (DECL_ASSEMBLER_NAME_SET_P (fn))
1158 mangle_decl (fn);
1160 if (!DECL_FUNCTION_VERSIONED (method))
1162 DECL_FUNCTION_VERSIONED (method) = 1;
1163 if (DECL_ASSEMBLER_NAME_SET_P (method))
1164 mangle_decl (method);
1166 cgraph_node::record_function_versions (fn, method);
1167 continue;
1169 if (DECL_INHERITED_CTOR_BASE (method))
1171 if (DECL_INHERITED_CTOR_BASE (fn))
1173 error_at (DECL_SOURCE_LOCATION (method),
1174 "%q#D inherited from %qT", method,
1175 DECL_INHERITED_CTOR_BASE (method));
1176 error_at (DECL_SOURCE_LOCATION (fn),
1177 "conflicts with version inherited from %qT",
1178 DECL_INHERITED_CTOR_BASE (fn));
1180 /* Otherwise defer to the other function. */
1181 return false;
1183 if (using_decl)
1185 if (DECL_CONTEXT (fn) == type)
1186 /* Defer to the local function. */
1187 return false;
1189 else
1191 error ("%q+#D cannot be overloaded", method);
1192 error ("with %q+#D", fn);
1195 /* We don't call duplicate_decls here to merge the
1196 declarations because that will confuse things if the
1197 methods have inline definitions. In particular, we
1198 will crash while processing the definitions. */
1199 return false;
1203 /* A class should never have more than one destructor. */
1204 if (current_fns && DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (method))
1205 return false;
1207 /* Add the new binding. */
1208 if (using_decl)
1210 overload = ovl_cons (method, current_fns);
1211 OVL_USED (overload) = true;
1213 else
1214 overload = build_overload (method, current_fns);
1216 if (conv_p)
1217 TYPE_HAS_CONVERSION (type) = 1;
1218 else if (slot >= CLASSTYPE_FIRST_CONVERSION_SLOT && !complete_p)
1219 push_class_level_binding (DECL_NAME (method), overload);
1221 if (insert_p)
1223 bool reallocated;
1225 /* We only expect to add few methods in the COMPLETE_P case, so
1226 just make room for one more method in that case. */
1227 if (complete_p)
1228 reallocated = vec_safe_reserve_exact (method_vec, 1);
1229 else
1230 reallocated = vec_safe_reserve (method_vec, 1);
1231 if (reallocated)
1232 CLASSTYPE_METHOD_VEC (type) = method_vec;
1233 if (slot == method_vec->length ())
1234 method_vec->quick_push (overload);
1235 else
1236 method_vec->quick_insert (slot, overload);
1238 else
1239 /* Replace the current slot. */
1240 (*method_vec)[slot] = overload;
1241 return true;
1244 /* Subroutines of finish_struct. */
1246 /* Change the access of FDECL to ACCESS in T. Return 1 if change was
1247 legit, otherwise return 0. */
1249 static int
1250 alter_access (tree t, tree fdecl, tree access)
1252 tree elem;
1254 if (!DECL_LANG_SPECIFIC (fdecl))
1255 retrofit_lang_decl (fdecl);
1257 gcc_assert (!DECL_DISCRIMINATOR_P (fdecl));
1259 elem = purpose_member (t, DECL_ACCESS (fdecl));
1260 if (elem)
1262 if (TREE_VALUE (elem) != access)
1264 if (TREE_CODE (TREE_TYPE (fdecl)) == FUNCTION_DECL)
1265 error ("conflicting access specifications for method"
1266 " %q+D, ignored", TREE_TYPE (fdecl));
1267 else
1268 error ("conflicting access specifications for field %qE, ignored",
1269 DECL_NAME (fdecl));
1271 else
1273 /* They're changing the access to the same thing they changed
1274 it to before. That's OK. */
1278 else
1280 perform_or_defer_access_check (TYPE_BINFO (t), fdecl, fdecl,
1281 tf_warning_or_error);
1282 DECL_ACCESS (fdecl) = tree_cons (t, access, DECL_ACCESS (fdecl));
1283 return 1;
1285 return 0;
1288 /* Process the USING_DECL, which is a member of T. */
1290 static void
1291 handle_using_decl (tree using_decl, tree t)
1293 tree decl = USING_DECL_DECLS (using_decl);
1294 tree name = DECL_NAME (using_decl);
1295 tree access
1296 = TREE_PRIVATE (using_decl) ? access_private_node
1297 : TREE_PROTECTED (using_decl) ? access_protected_node
1298 : access_public_node;
1299 tree flist = NULL_TREE;
1300 tree old_value;
1302 gcc_assert (!processing_template_decl && decl);
1304 old_value = lookup_member (t, name, /*protect=*/0, /*want_type=*/false,
1305 tf_warning_or_error);
1306 if (old_value)
1308 if (is_overloaded_fn (old_value))
1309 old_value = OVL_CURRENT (old_value);
1311 if (DECL_P (old_value) && DECL_CONTEXT (old_value) == t)
1312 /* OK */;
1313 else
1314 old_value = NULL_TREE;
1317 cp_emit_debug_info_for_using (decl, t);
1319 if (is_overloaded_fn (decl))
1320 flist = decl;
1322 if (! old_value)
1324 else if (is_overloaded_fn (old_value))
1326 if (flist)
1327 /* It's OK to use functions from a base when there are functions with
1328 the same name already present in the current class. */;
1329 else
1331 error ("%q+D invalid in %q#T", using_decl, t);
1332 error (" because of local method %q+#D with same name",
1333 OVL_CURRENT (old_value));
1334 return;
1337 else if (!DECL_ARTIFICIAL (old_value))
1339 error ("%q+D invalid in %q#T", using_decl, t);
1340 error (" because of local member %q+#D with same name", old_value);
1341 return;
1344 /* Make type T see field decl FDECL with access ACCESS. */
1345 if (flist)
1346 for (; flist; flist = OVL_NEXT (flist))
1348 add_method (t, OVL_CURRENT (flist), using_decl);
1349 alter_access (t, OVL_CURRENT (flist), access);
1351 else
1352 alter_access (t, decl, access);
1355 /* Data structure for find_abi_tags_r, below. */
1357 struct abi_tag_data
1359 tree t; // The type that we're checking for missing tags.
1360 tree subob; // The subobject of T that we're getting tags from.
1361 tree tags; // error_mark_node for diagnostics, or a list of missing tags.
1364 /* Subroutine of find_abi_tags_r. Handle a single TAG found on the class TP
1365 in the context of P. TAG can be either an identifier (the DECL_NAME of
1366 a tag NAMESPACE_DECL) or a STRING_CST (a tag attribute). */
1368 static void
1369 check_tag (tree tag, tree id, tree *tp, abi_tag_data *p)
1371 if (!IDENTIFIER_MARKED (id))
1373 if (p->tags != error_mark_node)
1375 /* We're collecting tags from template arguments or from
1376 the type of a variable or function return type. */
1377 p->tags = tree_cons (NULL_TREE, tag, p->tags);
1379 /* Don't inherit this tag multiple times. */
1380 IDENTIFIER_MARKED (id) = true;
1382 if (TYPE_P (p->t))
1384 /* Tags inherited from type template arguments are only used
1385 to avoid warnings. */
1386 ABI_TAG_IMPLICIT (p->tags) = true;
1387 return;
1389 /* For functions and variables we want to warn, too. */
1392 /* Otherwise we're diagnosing missing tags. */
1393 if (TREE_CODE (p->t) == FUNCTION_DECL)
1395 if (warning (OPT_Wabi_tag, "%qD inherits the %E ABI tag "
1396 "that %qT (used in its return type) has",
1397 p->t, tag, *tp))
1398 inform (location_of (*tp), "%qT declared here", *tp);
1400 else if (VAR_P (p->t))
1402 if (warning (OPT_Wabi_tag, "%qD inherits the %E ABI tag "
1403 "that %qT (used in its type) has", p->t, tag, *tp))
1404 inform (location_of (*tp), "%qT declared here", *tp);
1406 else if (TYPE_P (p->subob))
1408 if (warning (OPT_Wabi_tag, "%qT does not have the %E ABI tag "
1409 "that base %qT has", p->t, tag, p->subob))
1410 inform (location_of (p->subob), "%qT declared here",
1411 p->subob);
1413 else
1415 if (warning (OPT_Wabi_tag, "%qT does not have the %E ABI tag "
1416 "that %qT (used in the type of %qD) has",
1417 p->t, tag, *tp, p->subob))
1419 inform (location_of (p->subob), "%qD declared here",
1420 p->subob);
1421 inform (location_of (*tp), "%qT declared here", *tp);
1427 /* Find all the ABI tags in the attribute list ATTR and either call
1428 check_tag (if TP is non-null) or set IDENTIFIER_MARKED to val. */
1430 static void
1431 mark_or_check_attr_tags (tree attr, tree *tp, abi_tag_data *p, bool val)
1433 if (!attr)
1434 return;
1435 for (; (attr = lookup_attribute ("abi_tag", attr));
1436 attr = TREE_CHAIN (attr))
1437 for (tree list = TREE_VALUE (attr); list;
1438 list = TREE_CHAIN (list))
1440 tree tag = TREE_VALUE (list);
1441 tree id = get_identifier (TREE_STRING_POINTER (tag));
1442 if (tp)
1443 check_tag (tag, id, tp, p);
1444 else
1445 IDENTIFIER_MARKED (id) = val;
1449 /* Find all the ABI tags on T and its enclosing scopes and either call
1450 check_tag (if TP is non-null) or set IDENTIFIER_MARKED to val. */
1452 static void
1453 mark_or_check_tags (tree t, tree *tp, abi_tag_data *p, bool val)
1455 while (t != global_namespace)
1457 tree attr;
1458 if (TYPE_P (t))
1460 attr = TYPE_ATTRIBUTES (t);
1461 t = CP_TYPE_CONTEXT (t);
1463 else
1465 attr = DECL_ATTRIBUTES (t);
1466 t = CP_DECL_CONTEXT (t);
1468 mark_or_check_attr_tags (attr, tp, p, val);
1472 /* walk_tree callback for check_abi_tags: if the type at *TP involves any
1473 types with ABI tags, add the corresponding identifiers to the VEC in
1474 *DATA and set IDENTIFIER_MARKED. */
1476 static tree
1477 find_abi_tags_r (tree *tp, int *walk_subtrees, void *data)
1479 if (!OVERLOAD_TYPE_P (*tp))
1480 return NULL_TREE;
1482 /* walk_tree shouldn't be walking into any subtrees of a RECORD_TYPE
1483 anyway, but let's make sure of it. */
1484 *walk_subtrees = false;
1486 abi_tag_data *p = static_cast<struct abi_tag_data*>(data);
1488 mark_or_check_tags (*tp, tp, p, false);
1490 return NULL_TREE;
1493 /* walk_tree callback for mark_abi_tags: if *TP is a class, set
1494 IDENTIFIER_MARKED on its ABI tags. */
1496 static tree
1497 mark_abi_tags_r (tree *tp, int *walk_subtrees, void *data)
1499 if (!OVERLOAD_TYPE_P (*tp))
1500 return NULL_TREE;
1502 /* walk_tree shouldn't be walking into any subtrees of a RECORD_TYPE
1503 anyway, but let's make sure of it. */
1504 *walk_subtrees = false;
1506 bool *valp = static_cast<bool*>(data);
1508 mark_or_check_tags (*tp, NULL, NULL, *valp);
1510 return NULL_TREE;
1513 /* Set IDENTIFIER_MARKED on all the ABI tags on T and its enclosing
1514 scopes. */
1516 static void
1517 mark_abi_tags (tree t, bool val)
1519 mark_or_check_tags (t, NULL, NULL, val);
1520 if (DECL_P (t))
1522 if (DECL_LANG_SPECIFIC (t) && DECL_USE_TEMPLATE (t)
1523 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (t)))
1525 /* Template arguments are part of the signature. */
1526 tree level = INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (t));
1527 for (int j = 0; j < TREE_VEC_LENGTH (level); ++j)
1529 tree arg = TREE_VEC_ELT (level, j);
1530 cp_walk_tree_without_duplicates (&arg, mark_abi_tags_r, &val);
1533 if (TREE_CODE (t) == FUNCTION_DECL)
1534 /* A function's parameter types are part of the signature, so
1535 we don't need to inherit any tags that are also in them. */
1536 for (tree arg = FUNCTION_FIRST_USER_PARMTYPE (t); arg;
1537 arg = TREE_CHAIN (arg))
1538 cp_walk_tree_without_duplicates (&TREE_VALUE (arg),
1539 mark_abi_tags_r, &val);
1543 /* Check that T has all the ABI tags that subobject SUBOB has, or
1544 warn if not. If T is a (variable or function) declaration, also
1545 add any missing tags. */
1547 static void
1548 check_abi_tags (tree t, tree subob)
1550 bool inherit = DECL_P (t);
1552 if (!inherit && !warn_abi_tag)
1553 return;
1555 tree decl = TYPE_P (t) ? TYPE_NAME (t) : t;
1556 if (!TREE_PUBLIC (decl))
1557 /* No need to worry about things local to this TU. */
1558 return;
1560 mark_abi_tags (t, true);
1562 tree subtype = TYPE_P (subob) ? subob : TREE_TYPE (subob);
1563 struct abi_tag_data data = { t, subob, error_mark_node };
1564 if (inherit)
1565 data.tags = NULL_TREE;
1567 cp_walk_tree_without_duplicates (&subtype, find_abi_tags_r, &data);
1569 if (inherit && data.tags)
1571 tree attr = lookup_attribute ("abi_tag", DECL_ATTRIBUTES (t));
1572 if (attr)
1573 TREE_VALUE (attr) = chainon (data.tags, TREE_VALUE (attr));
1574 else
1575 DECL_ATTRIBUTES (t)
1576 = tree_cons (get_identifier ("abi_tag"), data.tags,
1577 DECL_ATTRIBUTES (t));
1580 mark_abi_tags (t, false);
1583 /* Check that DECL has all the ABI tags that are used in parts of its type
1584 that are not reflected in its mangled name. */
1586 void
1587 check_abi_tags (tree decl)
1589 if (VAR_P (decl))
1590 check_abi_tags (decl, TREE_TYPE (decl));
1591 else if (TREE_CODE (decl) == FUNCTION_DECL
1592 && !mangle_return_type_p (decl))
1593 check_abi_tags (decl, TREE_TYPE (TREE_TYPE (decl)));
1596 void
1597 inherit_targ_abi_tags (tree t)
1599 if (!CLASS_TYPE_P (t)
1600 || CLASSTYPE_TEMPLATE_INFO (t) == NULL_TREE)
1601 return;
1603 mark_abi_tags (t, true);
1605 tree args = CLASSTYPE_TI_ARGS (t);
1606 struct abi_tag_data data = { t, NULL_TREE, NULL_TREE };
1607 for (int i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
1609 tree level = TMPL_ARGS_LEVEL (args, i+1);
1610 for (int j = 0; j < TREE_VEC_LENGTH (level); ++j)
1612 tree arg = TREE_VEC_ELT (level, j);
1613 data.subob = arg;
1614 cp_walk_tree_without_duplicates (&arg, find_abi_tags_r, &data);
1618 // If we found some tags on our template arguments, add them to our
1619 // abi_tag attribute.
1620 if (data.tags)
1622 tree attr = lookup_attribute ("abi_tag", TYPE_ATTRIBUTES (t));
1623 if (attr)
1624 TREE_VALUE (attr) = chainon (data.tags, TREE_VALUE (attr));
1625 else
1626 TYPE_ATTRIBUTES (t)
1627 = tree_cons (get_identifier ("abi_tag"), data.tags,
1628 TYPE_ATTRIBUTES (t));
1631 mark_abi_tags (t, false);
1634 /* Return true, iff class T has a non-virtual destructor that is
1635 accessible from outside the class heirarchy (i.e. is public, or
1636 there's a suitable friend. */
1638 static bool
1639 accessible_nvdtor_p (tree t)
1641 tree dtor = CLASSTYPE_DESTRUCTORS (t);
1643 /* An implicitly declared destructor is always public. And,
1644 if it were virtual, we would have created it by now. */
1645 if (!dtor)
1646 return true;
1648 if (DECL_VINDEX (dtor))
1649 return false; /* Virtual */
1651 if (!TREE_PRIVATE (dtor) && !TREE_PROTECTED (dtor))
1652 return true; /* Public */
1654 if (CLASSTYPE_FRIEND_CLASSES (t)
1655 || DECL_FRIENDLIST (TYPE_MAIN_DECL (t)))
1656 return true; /* Has friends */
1658 return false;
1661 /* Run through the base classes of T, updating CANT_HAVE_CONST_CTOR_P,
1662 and NO_CONST_ASN_REF_P. Also set flag bits in T based on
1663 properties of the bases. */
1665 static void
1666 check_bases (tree t,
1667 int* cant_have_const_ctor_p,
1668 int* no_const_asn_ref_p)
1670 int i;
1671 bool seen_non_virtual_nearly_empty_base_p = 0;
1672 int seen_tm_mask = 0;
1673 tree base_binfo;
1674 tree binfo;
1675 tree field = NULL_TREE;
1677 if (!CLASSTYPE_NON_STD_LAYOUT (t))
1678 for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
1679 if (TREE_CODE (field) == FIELD_DECL)
1680 break;
1682 for (binfo = TYPE_BINFO (t), i = 0;
1683 BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
1685 tree basetype = TREE_TYPE (base_binfo);
1687 gcc_assert (COMPLETE_TYPE_P (basetype));
1689 if (CLASSTYPE_FINAL (basetype))
1690 error ("cannot derive from %<final%> base %qT in derived type %qT",
1691 basetype, t);
1693 /* If any base class is non-literal, so is the derived class. */
1694 if (!CLASSTYPE_LITERAL_P (basetype))
1695 CLASSTYPE_LITERAL_P (t) = false;
1697 /* If the base class doesn't have copy constructors or
1698 assignment operators that take const references, then the
1699 derived class cannot have such a member automatically
1700 generated. */
1701 if (TYPE_HAS_COPY_CTOR (basetype)
1702 && ! TYPE_HAS_CONST_COPY_CTOR (basetype))
1703 *cant_have_const_ctor_p = 1;
1704 if (TYPE_HAS_COPY_ASSIGN (basetype)
1705 && !TYPE_HAS_CONST_COPY_ASSIGN (basetype))
1706 *no_const_asn_ref_p = 1;
1708 if (BINFO_VIRTUAL_P (base_binfo))
1709 /* A virtual base does not effect nearly emptiness. */
1711 else if (CLASSTYPE_NEARLY_EMPTY_P (basetype))
1713 if (seen_non_virtual_nearly_empty_base_p)
1714 /* And if there is more than one nearly empty base, then the
1715 derived class is not nearly empty either. */
1716 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
1717 else
1718 /* Remember we've seen one. */
1719 seen_non_virtual_nearly_empty_base_p = 1;
1721 else if (!is_empty_class (basetype))
1722 /* If the base class is not empty or nearly empty, then this
1723 class cannot be nearly empty. */
1724 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
1726 /* A lot of properties from the bases also apply to the derived
1727 class. */
1728 TYPE_NEEDS_CONSTRUCTING (t) |= TYPE_NEEDS_CONSTRUCTING (basetype);
1729 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t)
1730 |= TYPE_HAS_NONTRIVIAL_DESTRUCTOR (basetype);
1731 TYPE_HAS_COMPLEX_COPY_ASSIGN (t)
1732 |= (TYPE_HAS_COMPLEX_COPY_ASSIGN (basetype)
1733 || !TYPE_HAS_COPY_ASSIGN (basetype));
1734 TYPE_HAS_COMPLEX_COPY_CTOR (t) |= (TYPE_HAS_COMPLEX_COPY_CTOR (basetype)
1735 || !TYPE_HAS_COPY_CTOR (basetype));
1736 TYPE_HAS_COMPLEX_MOVE_ASSIGN (t)
1737 |= TYPE_HAS_COMPLEX_MOVE_ASSIGN (basetype);
1738 TYPE_HAS_COMPLEX_MOVE_CTOR (t) |= TYPE_HAS_COMPLEX_MOVE_CTOR (basetype);
1739 TYPE_POLYMORPHIC_P (t) |= TYPE_POLYMORPHIC_P (basetype);
1740 CLASSTYPE_CONTAINS_EMPTY_CLASS_P (t)
1741 |= CLASSTYPE_CONTAINS_EMPTY_CLASS_P (basetype);
1742 TYPE_HAS_COMPLEX_DFLT (t) |= (!TYPE_HAS_DEFAULT_CONSTRUCTOR (basetype)
1743 || TYPE_HAS_COMPLEX_DFLT (basetype));
1744 SET_CLASSTYPE_READONLY_FIELDS_NEED_INIT
1745 (t, CLASSTYPE_READONLY_FIELDS_NEED_INIT (t)
1746 | CLASSTYPE_READONLY_FIELDS_NEED_INIT (basetype));
1747 SET_CLASSTYPE_REF_FIELDS_NEED_INIT
1748 (t, CLASSTYPE_REF_FIELDS_NEED_INIT (t)
1749 | CLASSTYPE_REF_FIELDS_NEED_INIT (basetype));
1751 /* A standard-layout class is a class that:
1753 * has no non-standard-layout base classes, */
1754 CLASSTYPE_NON_STD_LAYOUT (t) |= CLASSTYPE_NON_STD_LAYOUT (basetype);
1755 if (!CLASSTYPE_NON_STD_LAYOUT (t))
1757 tree basefield;
1758 /* ...has no base classes of the same type as the first non-static
1759 data member... */
1760 if (field && DECL_CONTEXT (field) == t
1761 && (same_type_ignoring_top_level_qualifiers_p
1762 (TREE_TYPE (field), basetype)))
1763 CLASSTYPE_NON_STD_LAYOUT (t) = 1;
1764 else
1765 /* ...either has no non-static data members in the most-derived
1766 class and at most one base class with non-static data
1767 members, or has no base classes with non-static data
1768 members */
1769 for (basefield = TYPE_FIELDS (basetype); basefield;
1770 basefield = DECL_CHAIN (basefield))
1771 if (TREE_CODE (basefield) == FIELD_DECL)
1773 if (field)
1774 CLASSTYPE_NON_STD_LAYOUT (t) = 1;
1775 else
1776 field = basefield;
1777 break;
1781 /* Don't bother collecting tm attributes if transactional memory
1782 support is not enabled. */
1783 if (flag_tm)
1785 tree tm_attr = find_tm_attribute (TYPE_ATTRIBUTES (basetype));
1786 if (tm_attr)
1787 seen_tm_mask |= tm_attr_to_mask (tm_attr);
1790 check_abi_tags (t, basetype);
1793 /* If one of the base classes had TM attributes, and the current class
1794 doesn't define its own, then the current class inherits one. */
1795 if (seen_tm_mask && !find_tm_attribute (TYPE_ATTRIBUTES (t)))
1797 tree tm_attr = tm_mask_to_attr (seen_tm_mask & -seen_tm_mask);
1798 TYPE_ATTRIBUTES (t) = tree_cons (tm_attr, NULL, TYPE_ATTRIBUTES (t));
1802 /* Determine all the primary bases within T. Sets BINFO_PRIMARY_BASE_P for
1803 those that are primaries. Sets BINFO_LOST_PRIMARY_P for those
1804 that have had a nearly-empty virtual primary base stolen by some
1805 other base in the hierarchy. Determines CLASSTYPE_PRIMARY_BASE for
1806 T. */
1808 static void
1809 determine_primary_bases (tree t)
1811 unsigned i;
1812 tree primary = NULL_TREE;
1813 tree type_binfo = TYPE_BINFO (t);
1814 tree base_binfo;
1816 /* Determine the primary bases of our bases. */
1817 for (base_binfo = TREE_CHAIN (type_binfo); base_binfo;
1818 base_binfo = TREE_CHAIN (base_binfo))
1820 tree primary = CLASSTYPE_PRIMARY_BINFO (BINFO_TYPE (base_binfo));
1822 /* See if we're the non-virtual primary of our inheritance
1823 chain. */
1824 if (!BINFO_VIRTUAL_P (base_binfo))
1826 tree parent = BINFO_INHERITANCE_CHAIN (base_binfo);
1827 tree parent_primary = CLASSTYPE_PRIMARY_BINFO (BINFO_TYPE (parent));
1829 if (parent_primary
1830 && SAME_BINFO_TYPE_P (BINFO_TYPE (base_binfo),
1831 BINFO_TYPE (parent_primary)))
1832 /* We are the primary binfo. */
1833 BINFO_PRIMARY_P (base_binfo) = 1;
1835 /* Determine if we have a virtual primary base, and mark it so.
1837 if (primary && BINFO_VIRTUAL_P (primary))
1839 tree this_primary = copied_binfo (primary, base_binfo);
1841 if (BINFO_PRIMARY_P (this_primary))
1842 /* Someone already claimed this base. */
1843 BINFO_LOST_PRIMARY_P (base_binfo) = 1;
1844 else
1846 tree delta;
1848 BINFO_PRIMARY_P (this_primary) = 1;
1849 BINFO_INHERITANCE_CHAIN (this_primary) = base_binfo;
1851 /* A virtual binfo might have been copied from within
1852 another hierarchy. As we're about to use it as a
1853 primary base, make sure the offsets match. */
1854 delta = size_diffop_loc (input_location,
1855 fold_convert (ssizetype,
1856 BINFO_OFFSET (base_binfo)),
1857 fold_convert (ssizetype,
1858 BINFO_OFFSET (this_primary)));
1860 propagate_binfo_offsets (this_primary, delta);
1865 /* First look for a dynamic direct non-virtual base. */
1866 for (i = 0; BINFO_BASE_ITERATE (type_binfo, i, base_binfo); i++)
1868 tree basetype = BINFO_TYPE (base_binfo);
1870 if (TYPE_CONTAINS_VPTR_P (basetype) && !BINFO_VIRTUAL_P (base_binfo))
1872 primary = base_binfo;
1873 goto found;
1877 /* A "nearly-empty" virtual base class can be the primary base
1878 class, if no non-virtual polymorphic base can be found. Look for
1879 a nearly-empty virtual dynamic base that is not already a primary
1880 base of something in the hierarchy. If there is no such base,
1881 just pick the first nearly-empty virtual base. */
1883 for (base_binfo = TREE_CHAIN (type_binfo); base_binfo;
1884 base_binfo = TREE_CHAIN (base_binfo))
1885 if (BINFO_VIRTUAL_P (base_binfo)
1886 && CLASSTYPE_NEARLY_EMPTY_P (BINFO_TYPE (base_binfo)))
1888 if (!BINFO_PRIMARY_P (base_binfo))
1890 /* Found one that is not primary. */
1891 primary = base_binfo;
1892 goto found;
1894 else if (!primary)
1895 /* Remember the first candidate. */
1896 primary = base_binfo;
1899 found:
1900 /* If we've got a primary base, use it. */
1901 if (primary)
1903 tree basetype = BINFO_TYPE (primary);
1905 CLASSTYPE_PRIMARY_BINFO (t) = primary;
1906 if (BINFO_PRIMARY_P (primary))
1907 /* We are stealing a primary base. */
1908 BINFO_LOST_PRIMARY_P (BINFO_INHERITANCE_CHAIN (primary)) = 1;
1909 BINFO_PRIMARY_P (primary) = 1;
1910 if (BINFO_VIRTUAL_P (primary))
1912 tree delta;
1914 BINFO_INHERITANCE_CHAIN (primary) = type_binfo;
1915 /* A virtual binfo might have been copied from within
1916 another hierarchy. As we're about to use it as a primary
1917 base, make sure the offsets match. */
1918 delta = size_diffop_loc (input_location, ssize_int (0),
1919 fold_convert (ssizetype, BINFO_OFFSET (primary)));
1921 propagate_binfo_offsets (primary, delta);
1924 primary = TYPE_BINFO (basetype);
1926 TYPE_VFIELD (t) = TYPE_VFIELD (basetype);
1927 BINFO_VTABLE (type_binfo) = BINFO_VTABLE (primary);
1928 BINFO_VIRTUALS (type_binfo) = BINFO_VIRTUALS (primary);
1932 /* Update the variant types of T. */
1934 void
1935 fixup_type_variants (tree t)
1937 tree variants;
1939 if (!t)
1940 return;
1942 for (variants = TYPE_NEXT_VARIANT (t);
1943 variants;
1944 variants = TYPE_NEXT_VARIANT (variants))
1946 /* These fields are in the _TYPE part of the node, not in
1947 the TYPE_LANG_SPECIFIC component, so they are not shared. */
1948 TYPE_HAS_USER_CONSTRUCTOR (variants) = TYPE_HAS_USER_CONSTRUCTOR (t);
1949 TYPE_NEEDS_CONSTRUCTING (variants) = TYPE_NEEDS_CONSTRUCTING (t);
1950 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (variants)
1951 = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t);
1953 TYPE_POLYMORPHIC_P (variants) = TYPE_POLYMORPHIC_P (t);
1955 TYPE_BINFO (variants) = TYPE_BINFO (t);
1957 /* Copy whatever these are holding today. */
1958 TYPE_VFIELD (variants) = TYPE_VFIELD (t);
1959 TYPE_FIELDS (variants) = TYPE_FIELDS (t);
1963 /* Early variant fixups: we apply attributes at the beginning of the class
1964 definition, and we need to fix up any variants that have already been
1965 made via elaborated-type-specifier so that check_qualified_type works. */
1967 void
1968 fixup_attribute_variants (tree t)
1970 tree variants;
1972 if (!t)
1973 return;
1975 tree attrs = TYPE_ATTRIBUTES (t);
1976 unsigned align = TYPE_ALIGN (t);
1977 bool user_align = TYPE_USER_ALIGN (t);
1979 for (variants = TYPE_NEXT_VARIANT (t);
1980 variants;
1981 variants = TYPE_NEXT_VARIANT (variants))
1983 /* These are the two fields that check_qualified_type looks at and
1984 are affected by attributes. */
1985 TYPE_ATTRIBUTES (variants) = attrs;
1986 unsigned valign = align;
1987 if (TYPE_USER_ALIGN (variants))
1988 valign = MAX (valign, TYPE_ALIGN (variants));
1989 else
1990 TYPE_USER_ALIGN (variants) = user_align;
1991 TYPE_ALIGN (variants) = valign;
1995 /* Set memoizing fields and bits of T (and its variants) for later
1996 use. */
1998 static void
1999 finish_struct_bits (tree t)
2001 /* Fix up variants (if any). */
2002 fixup_type_variants (t);
2004 if (BINFO_N_BASE_BINFOS (TYPE_BINFO (t)) && TYPE_POLYMORPHIC_P (t))
2005 /* For a class w/o baseclasses, 'finish_struct' has set
2006 CLASSTYPE_PURE_VIRTUALS correctly (by definition).
2007 Similarly for a class whose base classes do not have vtables.
2008 When neither of these is true, we might have removed abstract
2009 virtuals (by providing a definition), added some (by declaring
2010 new ones), or redeclared ones from a base class. We need to
2011 recalculate what's really an abstract virtual at this point (by
2012 looking in the vtables). */
2013 get_pure_virtuals (t);
2015 /* If this type has a copy constructor or a destructor, force its
2016 mode to be BLKmode, and force its TREE_ADDRESSABLE bit to be
2017 nonzero. This will cause it to be passed by invisible reference
2018 and prevent it from being returned in a register. */
2019 if (type_has_nontrivial_copy_init (t)
2020 || TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t))
2022 tree variants;
2023 DECL_MODE (TYPE_MAIN_DECL (t)) = BLKmode;
2024 for (variants = t; variants; variants = TYPE_NEXT_VARIANT (variants))
2026 SET_TYPE_MODE (variants, BLKmode);
2027 TREE_ADDRESSABLE (variants) = 1;
2032 /* Issue warnings about T having private constructors, but no friends,
2033 and so forth.
2035 HAS_NONPRIVATE_METHOD is nonzero if T has any non-private methods or
2036 static members. HAS_NONPRIVATE_STATIC_FN is nonzero if T has any
2037 non-private static member functions. */
2039 static void
2040 maybe_warn_about_overly_private_class (tree t)
2042 int has_member_fn = 0;
2043 int has_nonprivate_method = 0;
2044 tree fn;
2046 if (!warn_ctor_dtor_privacy
2047 /* If the class has friends, those entities might create and
2048 access instances, so we should not warn. */
2049 || (CLASSTYPE_FRIEND_CLASSES (t)
2050 || DECL_FRIENDLIST (TYPE_MAIN_DECL (t)))
2051 /* We will have warned when the template was declared; there's
2052 no need to warn on every instantiation. */
2053 || CLASSTYPE_TEMPLATE_INSTANTIATION (t))
2054 /* There's no reason to even consider warning about this
2055 class. */
2056 return;
2058 /* We only issue one warning, if more than one applies, because
2059 otherwise, on code like:
2061 class A {
2062 // Oops - forgot `public:'
2063 A();
2064 A(const A&);
2065 ~A();
2068 we warn several times about essentially the same problem. */
2070 /* Check to see if all (non-constructor, non-destructor) member
2071 functions are private. (Since there are no friends or
2072 non-private statics, we can't ever call any of the private member
2073 functions.) */
2074 for (fn = TYPE_METHODS (t); fn; fn = DECL_CHAIN (fn))
2075 /* We're not interested in compiler-generated methods; they don't
2076 provide any way to call private members. */
2077 if (!DECL_ARTIFICIAL (fn))
2079 if (!TREE_PRIVATE (fn))
2081 if (DECL_STATIC_FUNCTION_P (fn))
2082 /* A non-private static member function is just like a
2083 friend; it can create and invoke private member
2084 functions, and be accessed without a class
2085 instance. */
2086 return;
2088 has_nonprivate_method = 1;
2089 /* Keep searching for a static member function. */
2091 else if (!DECL_CONSTRUCTOR_P (fn) && !DECL_DESTRUCTOR_P (fn))
2092 has_member_fn = 1;
2095 if (!has_nonprivate_method && has_member_fn)
2097 /* There are no non-private methods, and there's at least one
2098 private member function that isn't a constructor or
2099 destructor. (If all the private members are
2100 constructors/destructors we want to use the code below that
2101 issues error messages specifically referring to
2102 constructors/destructors.) */
2103 unsigned i;
2104 tree binfo = TYPE_BINFO (t);
2106 for (i = 0; i != BINFO_N_BASE_BINFOS (binfo); i++)
2107 if (BINFO_BASE_ACCESS (binfo, i) != access_private_node)
2109 has_nonprivate_method = 1;
2110 break;
2112 if (!has_nonprivate_method)
2114 warning (OPT_Wctor_dtor_privacy,
2115 "all member functions in class %qT are private", t);
2116 return;
2120 /* Even if some of the member functions are non-private, the class
2121 won't be useful for much if all the constructors or destructors
2122 are private: such an object can never be created or destroyed. */
2123 fn = CLASSTYPE_DESTRUCTORS (t);
2124 if (fn && TREE_PRIVATE (fn))
2126 warning (OPT_Wctor_dtor_privacy,
2127 "%q#T only defines a private destructor and has no friends",
2129 return;
2132 /* Warn about classes that have private constructors and no friends. */
2133 if (TYPE_HAS_USER_CONSTRUCTOR (t)
2134 /* Implicitly generated constructors are always public. */
2135 && (!CLASSTYPE_LAZY_DEFAULT_CTOR (t)
2136 || !CLASSTYPE_LAZY_COPY_CTOR (t)))
2138 int nonprivate_ctor = 0;
2140 /* If a non-template class does not define a copy
2141 constructor, one is defined for it, enabling it to avoid
2142 this warning. For a template class, this does not
2143 happen, and so we would normally get a warning on:
2145 template <class T> class C { private: C(); };
2147 To avoid this asymmetry, we check TYPE_HAS_COPY_CTOR. All
2148 complete non-template or fully instantiated classes have this
2149 flag set. */
2150 if (!TYPE_HAS_COPY_CTOR (t))
2151 nonprivate_ctor = 1;
2152 else
2153 for (fn = CLASSTYPE_CONSTRUCTORS (t); fn; fn = OVL_NEXT (fn))
2155 tree ctor = OVL_CURRENT (fn);
2156 /* Ideally, we wouldn't count copy constructors (or, in
2157 fact, any constructor that takes an argument of the
2158 class type as a parameter) because such things cannot
2159 be used to construct an instance of the class unless
2160 you already have one. But, for now at least, we're
2161 more generous. */
2162 if (! TREE_PRIVATE (ctor))
2164 nonprivate_ctor = 1;
2165 break;
2169 if (nonprivate_ctor == 0)
2171 warning (OPT_Wctor_dtor_privacy,
2172 "%q#T only defines private constructors and has no friends",
2174 return;
2179 static struct {
2180 gt_pointer_operator new_value;
2181 void *cookie;
2182 } resort_data;
2184 /* Comparison function to compare two TYPE_METHOD_VEC entries by name. */
2186 static int
2187 method_name_cmp (const void* m1_p, const void* m2_p)
2189 const tree *const m1 = (const tree *) m1_p;
2190 const tree *const m2 = (const tree *) m2_p;
2192 if (*m1 == NULL_TREE && *m2 == NULL_TREE)
2193 return 0;
2194 if (*m1 == NULL_TREE)
2195 return -1;
2196 if (*m2 == NULL_TREE)
2197 return 1;
2198 if (DECL_NAME (OVL_CURRENT (*m1)) < DECL_NAME (OVL_CURRENT (*m2)))
2199 return -1;
2200 return 1;
2203 /* This routine compares two fields like method_name_cmp but using the
2204 pointer operator in resort_field_decl_data. */
2206 static int
2207 resort_method_name_cmp (const void* m1_p, const void* m2_p)
2209 const tree *const m1 = (const tree *) m1_p;
2210 const tree *const m2 = (const tree *) m2_p;
2211 if (*m1 == NULL_TREE && *m2 == NULL_TREE)
2212 return 0;
2213 if (*m1 == NULL_TREE)
2214 return -1;
2215 if (*m2 == NULL_TREE)
2216 return 1;
2218 tree d1 = DECL_NAME (OVL_CURRENT (*m1));
2219 tree d2 = DECL_NAME (OVL_CURRENT (*m2));
2220 resort_data.new_value (&d1, resort_data.cookie);
2221 resort_data.new_value (&d2, resort_data.cookie);
2222 if (d1 < d2)
2223 return -1;
2225 return 1;
2228 /* Resort TYPE_METHOD_VEC because pointers have been reordered. */
2230 void
2231 resort_type_method_vec (void* obj,
2232 void* /*orig_obj*/,
2233 gt_pointer_operator new_value,
2234 void* cookie)
2236 vec<tree, va_gc> *method_vec = (vec<tree, va_gc> *) obj;
2237 int len = vec_safe_length (method_vec);
2238 size_t slot;
2239 tree fn;
2241 /* The type conversion ops have to live at the front of the vec, so we
2242 can't sort them. */
2243 for (slot = CLASSTYPE_FIRST_CONVERSION_SLOT;
2244 vec_safe_iterate (method_vec, slot, &fn);
2245 ++slot)
2246 if (!DECL_CONV_FN_P (OVL_CURRENT (fn)))
2247 break;
2249 if (len - slot > 1)
2251 resort_data.new_value = new_value;
2252 resort_data.cookie = cookie;
2253 qsort (method_vec->address () + slot, len - slot, sizeof (tree),
2254 resort_method_name_cmp);
2258 /* Warn about duplicate methods in fn_fields.
2260 Sort methods that are not special (i.e., constructors, destructors,
2261 and type conversion operators) so that we can find them faster in
2262 search. */
2264 static void
2265 finish_struct_methods (tree t)
2267 tree fn_fields;
2268 vec<tree, va_gc> *method_vec;
2269 int slot, len;
2271 method_vec = CLASSTYPE_METHOD_VEC (t);
2272 if (!method_vec)
2273 return;
2275 len = method_vec->length ();
2277 /* Clear DECL_IN_AGGR_P for all functions. */
2278 for (fn_fields = TYPE_METHODS (t); fn_fields;
2279 fn_fields = DECL_CHAIN (fn_fields))
2280 DECL_IN_AGGR_P (fn_fields) = 0;
2282 /* Issue warnings about private constructors and such. If there are
2283 no methods, then some public defaults are generated. */
2284 maybe_warn_about_overly_private_class (t);
2286 /* The type conversion ops have to live at the front of the vec, so we
2287 can't sort them. */
2288 for (slot = CLASSTYPE_FIRST_CONVERSION_SLOT;
2289 method_vec->iterate (slot, &fn_fields);
2290 ++slot)
2291 if (!DECL_CONV_FN_P (OVL_CURRENT (fn_fields)))
2292 break;
2293 if (len - slot > 1)
2294 qsort (method_vec->address () + slot,
2295 len-slot, sizeof (tree), method_name_cmp);
2298 /* Make BINFO's vtable have N entries, including RTTI entries,
2299 vbase and vcall offsets, etc. Set its type and call the back end
2300 to lay it out. */
2302 static void
2303 layout_vtable_decl (tree binfo, int n)
2305 tree atype;
2306 tree vtable;
2308 atype = build_array_of_n_type (vtable_entry_type, n);
2309 layout_type (atype);
2311 /* We may have to grow the vtable. */
2312 vtable = get_vtbl_decl_for_binfo (binfo);
2313 if (!same_type_p (TREE_TYPE (vtable), atype))
2315 TREE_TYPE (vtable) = atype;
2316 DECL_SIZE (vtable) = DECL_SIZE_UNIT (vtable) = NULL_TREE;
2317 layout_decl (vtable, 0);
2321 /* True iff FNDECL and BASE_FNDECL (both non-static member functions)
2322 have the same signature. */
2325 same_signature_p (const_tree fndecl, const_tree base_fndecl)
2327 /* One destructor overrides another if they are the same kind of
2328 destructor. */
2329 if (DECL_DESTRUCTOR_P (base_fndecl) && DECL_DESTRUCTOR_P (fndecl)
2330 && special_function_p (base_fndecl) == special_function_p (fndecl))
2331 return 1;
2332 /* But a non-destructor never overrides a destructor, nor vice
2333 versa, nor do different kinds of destructors override
2334 one-another. For example, a complete object destructor does not
2335 override a deleting destructor. */
2336 if (DECL_DESTRUCTOR_P (base_fndecl) || DECL_DESTRUCTOR_P (fndecl))
2337 return 0;
2339 if (DECL_NAME (fndecl) == DECL_NAME (base_fndecl)
2340 || (DECL_CONV_FN_P (fndecl)
2341 && DECL_CONV_FN_P (base_fndecl)
2342 && same_type_p (DECL_CONV_FN_TYPE (fndecl),
2343 DECL_CONV_FN_TYPE (base_fndecl))))
2345 tree fntype = TREE_TYPE (fndecl);
2346 tree base_fntype = TREE_TYPE (base_fndecl);
2347 if (type_memfn_quals (fntype) == type_memfn_quals (base_fntype)
2348 && type_memfn_rqual (fntype) == type_memfn_rqual (base_fntype)
2349 && compparms (FUNCTION_FIRST_USER_PARMTYPE (fndecl),
2350 FUNCTION_FIRST_USER_PARMTYPE (base_fndecl)))
2351 return 1;
2353 return 0;
2356 /* Returns TRUE if DERIVED is a binfo containing the binfo BASE as a
2357 subobject. */
2359 static bool
2360 base_derived_from (tree derived, tree base)
2362 tree probe;
2364 for (probe = base; probe; probe = BINFO_INHERITANCE_CHAIN (probe))
2366 if (probe == derived)
2367 return true;
2368 else if (BINFO_VIRTUAL_P (probe))
2369 /* If we meet a virtual base, we can't follow the inheritance
2370 any more. See if the complete type of DERIVED contains
2371 such a virtual base. */
2372 return (binfo_for_vbase (BINFO_TYPE (probe), BINFO_TYPE (derived))
2373 != NULL_TREE);
2375 return false;
2378 struct find_final_overrider_data {
2379 /* The function for which we are trying to find a final overrider. */
2380 tree fn;
2381 /* The base class in which the function was declared. */
2382 tree declaring_base;
2383 /* The candidate overriders. */
2384 tree candidates;
2385 /* Path to most derived. */
2386 vec<tree> path;
2389 /* Add the overrider along the current path to FFOD->CANDIDATES.
2390 Returns true if an overrider was found; false otherwise. */
2392 static bool
2393 dfs_find_final_overrider_1 (tree binfo,
2394 find_final_overrider_data *ffod,
2395 unsigned depth)
2397 tree method;
2399 /* If BINFO is not the most derived type, try a more derived class.
2400 A definition there will overrider a definition here. */
2401 if (depth)
2403 depth--;
2404 if (dfs_find_final_overrider_1
2405 (ffod->path[depth], ffod, depth))
2406 return true;
2409 method = look_for_overrides_here (BINFO_TYPE (binfo), ffod->fn);
2410 if (method)
2412 tree *candidate = &ffod->candidates;
2414 /* Remove any candidates overridden by this new function. */
2415 while (*candidate)
2417 /* If *CANDIDATE overrides METHOD, then METHOD
2418 cannot override anything else on the list. */
2419 if (base_derived_from (TREE_VALUE (*candidate), binfo))
2420 return true;
2421 /* If METHOD overrides *CANDIDATE, remove *CANDIDATE. */
2422 if (base_derived_from (binfo, TREE_VALUE (*candidate)))
2423 *candidate = TREE_CHAIN (*candidate);
2424 else
2425 candidate = &TREE_CHAIN (*candidate);
2428 /* Add the new function. */
2429 ffod->candidates = tree_cons (method, binfo, ffod->candidates);
2430 return true;
2433 return false;
2436 /* Called from find_final_overrider via dfs_walk. */
2438 static tree
2439 dfs_find_final_overrider_pre (tree binfo, void *data)
2441 find_final_overrider_data *ffod = (find_final_overrider_data *) data;
2443 if (binfo == ffod->declaring_base)
2444 dfs_find_final_overrider_1 (binfo, ffod, ffod->path.length ());
2445 ffod->path.safe_push (binfo);
2447 return NULL_TREE;
2450 static tree
2451 dfs_find_final_overrider_post (tree /*binfo*/, void *data)
2453 find_final_overrider_data *ffod = (find_final_overrider_data *) data;
2454 ffod->path.pop ();
2456 return NULL_TREE;
2459 /* Returns a TREE_LIST whose TREE_PURPOSE is the final overrider for
2460 FN and whose TREE_VALUE is the binfo for the base where the
2461 overriding occurs. BINFO (in the hierarchy dominated by the binfo
2462 DERIVED) is the base object in which FN is declared. */
2464 static tree
2465 find_final_overrider (tree derived, tree binfo, tree fn)
2467 find_final_overrider_data ffod;
2469 /* Getting this right is a little tricky. This is valid:
2471 struct S { virtual void f (); };
2472 struct T { virtual void f (); };
2473 struct U : public S, public T { };
2475 even though calling `f' in `U' is ambiguous. But,
2477 struct R { virtual void f(); };
2478 struct S : virtual public R { virtual void f (); };
2479 struct T : virtual public R { virtual void f (); };
2480 struct U : public S, public T { };
2482 is not -- there's no way to decide whether to put `S::f' or
2483 `T::f' in the vtable for `R'.
2485 The solution is to look at all paths to BINFO. If we find
2486 different overriders along any two, then there is a problem. */
2487 if (DECL_THUNK_P (fn))
2488 fn = THUNK_TARGET (fn);
2490 /* Determine the depth of the hierarchy. */
2491 ffod.fn = fn;
2492 ffod.declaring_base = binfo;
2493 ffod.candidates = NULL_TREE;
2494 ffod.path.create (30);
2496 dfs_walk_all (derived, dfs_find_final_overrider_pre,
2497 dfs_find_final_overrider_post, &ffod);
2499 ffod.path.release ();
2501 /* If there was no winner, issue an error message. */
2502 if (!ffod.candidates || TREE_CHAIN (ffod.candidates))
2503 return error_mark_node;
2505 return ffod.candidates;
2508 /* Return the index of the vcall offset for FN when TYPE is used as a
2509 virtual base. */
2511 static tree
2512 get_vcall_index (tree fn, tree type)
2514 vec<tree_pair_s, va_gc> *indices = CLASSTYPE_VCALL_INDICES (type);
2515 tree_pair_p p;
2516 unsigned ix;
2518 FOR_EACH_VEC_SAFE_ELT (indices, ix, p)
2519 if ((DECL_DESTRUCTOR_P (fn) && DECL_DESTRUCTOR_P (p->purpose))
2520 || same_signature_p (fn, p->purpose))
2521 return p->value;
2523 /* There should always be an appropriate index. */
2524 gcc_unreachable ();
2527 /* Update an entry in the vtable for BINFO, which is in the hierarchy
2528 dominated by T. FN is the old function; VIRTUALS points to the
2529 corresponding position in the new BINFO_VIRTUALS list. IX is the index
2530 of that entry in the list. */
2532 static void
2533 update_vtable_entry_for_fn (tree t, tree binfo, tree fn, tree* virtuals,
2534 unsigned ix)
2536 tree b;
2537 tree overrider;
2538 tree delta;
2539 tree virtual_base;
2540 tree first_defn;
2541 tree overrider_fn, overrider_target;
2542 tree target_fn = DECL_THUNK_P (fn) ? THUNK_TARGET (fn) : fn;
2543 tree over_return, base_return;
2544 bool lost = false;
2546 /* Find the nearest primary base (possibly binfo itself) which defines
2547 this function; this is the class the caller will convert to when
2548 calling FN through BINFO. */
2549 for (b = binfo; ; b = get_primary_binfo (b))
2551 gcc_assert (b);
2552 if (look_for_overrides_here (BINFO_TYPE (b), target_fn))
2553 break;
2555 /* The nearest definition is from a lost primary. */
2556 if (BINFO_LOST_PRIMARY_P (b))
2557 lost = true;
2559 first_defn = b;
2561 /* Find the final overrider. */
2562 overrider = find_final_overrider (TYPE_BINFO (t), b, target_fn);
2563 if (overrider == error_mark_node)
2565 error ("no unique final overrider for %qD in %qT", target_fn, t);
2566 return;
2568 overrider_target = overrider_fn = TREE_PURPOSE (overrider);
2570 /* Check for adjusting covariant return types. */
2571 over_return = TREE_TYPE (TREE_TYPE (overrider_target));
2572 base_return = TREE_TYPE (TREE_TYPE (target_fn));
2574 if (POINTER_TYPE_P (over_return)
2575 && TREE_CODE (over_return) == TREE_CODE (base_return)
2576 && CLASS_TYPE_P (TREE_TYPE (over_return))
2577 && CLASS_TYPE_P (TREE_TYPE (base_return))
2578 /* If the overrider is invalid, don't even try. */
2579 && !DECL_INVALID_OVERRIDER_P (overrider_target))
2581 /* If FN is a covariant thunk, we must figure out the adjustment
2582 to the final base FN was converting to. As OVERRIDER_TARGET might
2583 also be converting to the return type of FN, we have to
2584 combine the two conversions here. */
2585 tree fixed_offset, virtual_offset;
2587 over_return = TREE_TYPE (over_return);
2588 base_return = TREE_TYPE (base_return);
2590 if (DECL_THUNK_P (fn))
2592 gcc_assert (DECL_RESULT_THUNK_P (fn));
2593 fixed_offset = ssize_int (THUNK_FIXED_OFFSET (fn));
2594 virtual_offset = THUNK_VIRTUAL_OFFSET (fn);
2596 else
2597 fixed_offset = virtual_offset = NULL_TREE;
2599 if (virtual_offset)
2600 /* Find the equivalent binfo within the return type of the
2601 overriding function. We will want the vbase offset from
2602 there. */
2603 virtual_offset = binfo_for_vbase (BINFO_TYPE (virtual_offset),
2604 over_return);
2605 else if (!same_type_ignoring_top_level_qualifiers_p
2606 (over_return, base_return))
2608 /* There was no existing virtual thunk (which takes
2609 precedence). So find the binfo of the base function's
2610 return type within the overriding function's return type.
2611 Fortunately we know the covariancy is valid (it
2612 has already been checked), so we can just iterate along
2613 the binfos, which have been chained in inheritance graph
2614 order. Of course it is lame that we have to repeat the
2615 search here anyway -- we should really be caching pieces
2616 of the vtable and avoiding this repeated work. */
2617 tree thunk_binfo, base_binfo;
2619 /* Find the base binfo within the overriding function's
2620 return type. We will always find a thunk_binfo, except
2621 when the covariancy is invalid (which we will have
2622 already diagnosed). */
2623 for (base_binfo = TYPE_BINFO (base_return),
2624 thunk_binfo = TYPE_BINFO (over_return);
2625 thunk_binfo;
2626 thunk_binfo = TREE_CHAIN (thunk_binfo))
2627 if (SAME_BINFO_TYPE_P (BINFO_TYPE (thunk_binfo),
2628 BINFO_TYPE (base_binfo)))
2629 break;
2631 /* See if virtual inheritance is involved. */
2632 for (virtual_offset = thunk_binfo;
2633 virtual_offset;
2634 virtual_offset = BINFO_INHERITANCE_CHAIN (virtual_offset))
2635 if (BINFO_VIRTUAL_P (virtual_offset))
2636 break;
2638 if (virtual_offset
2639 || (thunk_binfo && !BINFO_OFFSET_ZEROP (thunk_binfo)))
2641 tree offset = fold_convert (ssizetype, BINFO_OFFSET (thunk_binfo));
2643 if (virtual_offset)
2645 /* We convert via virtual base. Adjust the fixed
2646 offset to be from there. */
2647 offset =
2648 size_diffop (offset,
2649 fold_convert (ssizetype,
2650 BINFO_OFFSET (virtual_offset)));
2652 if (fixed_offset)
2653 /* There was an existing fixed offset, this must be
2654 from the base just converted to, and the base the
2655 FN was thunking to. */
2656 fixed_offset = size_binop (PLUS_EXPR, fixed_offset, offset);
2657 else
2658 fixed_offset = offset;
2662 if (fixed_offset || virtual_offset)
2663 /* Replace the overriding function with a covariant thunk. We
2664 will emit the overriding function in its own slot as
2665 well. */
2666 overrider_fn = make_thunk (overrider_target, /*this_adjusting=*/0,
2667 fixed_offset, virtual_offset);
2669 else
2670 gcc_assert (DECL_INVALID_OVERRIDER_P (overrider_target) ||
2671 !DECL_THUNK_P (fn));
2673 /* If we need a covariant thunk, then we may need to adjust first_defn.
2674 The ABI specifies that the thunks emitted with a function are
2675 determined by which bases the function overrides, so we need to be
2676 sure that we're using a thunk for some overridden base; even if we
2677 know that the necessary this adjustment is zero, there may not be an
2678 appropriate zero-this-adjusment thunk for us to use since thunks for
2679 overriding virtual bases always use the vcall offset.
2681 Furthermore, just choosing any base that overrides this function isn't
2682 quite right, as this slot won't be used for calls through a type that
2683 puts a covariant thunk here. Calling the function through such a type
2684 will use a different slot, and that slot is the one that determines
2685 the thunk emitted for that base.
2687 So, keep looking until we find the base that we're really overriding
2688 in this slot: the nearest primary base that doesn't use a covariant
2689 thunk in this slot. */
2690 if (overrider_target != overrider_fn)
2692 if (BINFO_TYPE (b) == DECL_CONTEXT (overrider_target))
2693 /* We already know that the overrider needs a covariant thunk. */
2694 b = get_primary_binfo (b);
2695 for (; ; b = get_primary_binfo (b))
2697 tree main_binfo = TYPE_BINFO (BINFO_TYPE (b));
2698 tree bv = chain_index (ix, BINFO_VIRTUALS (main_binfo));
2699 if (!DECL_THUNK_P (TREE_VALUE (bv)))
2700 break;
2701 if (BINFO_LOST_PRIMARY_P (b))
2702 lost = true;
2704 first_defn = b;
2707 /* Assume that we will produce a thunk that convert all the way to
2708 the final overrider, and not to an intermediate virtual base. */
2709 virtual_base = NULL_TREE;
2711 /* See if we can convert to an intermediate virtual base first, and then
2712 use the vcall offset located there to finish the conversion. */
2713 for (; b; b = BINFO_INHERITANCE_CHAIN (b))
2715 /* If we find the final overrider, then we can stop
2716 walking. */
2717 if (SAME_BINFO_TYPE_P (BINFO_TYPE (b),
2718 BINFO_TYPE (TREE_VALUE (overrider))))
2719 break;
2721 /* If we find a virtual base, and we haven't yet found the
2722 overrider, then there is a virtual base between the
2723 declaring base (first_defn) and the final overrider. */
2724 if (BINFO_VIRTUAL_P (b))
2726 virtual_base = b;
2727 break;
2731 /* Compute the constant adjustment to the `this' pointer. The
2732 `this' pointer, when this function is called, will point at BINFO
2733 (or one of its primary bases, which are at the same offset). */
2734 if (virtual_base)
2735 /* The `this' pointer needs to be adjusted from the declaration to
2736 the nearest virtual base. */
2737 delta = size_diffop_loc (input_location,
2738 fold_convert (ssizetype, BINFO_OFFSET (virtual_base)),
2739 fold_convert (ssizetype, BINFO_OFFSET (first_defn)));
2740 else if (lost)
2741 /* If the nearest definition is in a lost primary, we don't need an
2742 entry in our vtable. Except possibly in a constructor vtable,
2743 if we happen to get our primary back. In that case, the offset
2744 will be zero, as it will be a primary base. */
2745 delta = size_zero_node;
2746 else
2747 /* The `this' pointer needs to be adjusted from pointing to
2748 BINFO to pointing at the base where the final overrider
2749 appears. */
2750 delta = size_diffop_loc (input_location,
2751 fold_convert (ssizetype,
2752 BINFO_OFFSET (TREE_VALUE (overrider))),
2753 fold_convert (ssizetype, BINFO_OFFSET (binfo)));
2755 modify_vtable_entry (t, binfo, overrider_fn, delta, virtuals);
2757 if (virtual_base)
2758 BV_VCALL_INDEX (*virtuals)
2759 = get_vcall_index (overrider_target, BINFO_TYPE (virtual_base));
2760 else
2761 BV_VCALL_INDEX (*virtuals) = NULL_TREE;
2763 BV_LOST_PRIMARY (*virtuals) = lost;
2766 /* Called from modify_all_vtables via dfs_walk. */
2768 static tree
2769 dfs_modify_vtables (tree binfo, void* data)
2771 tree t = (tree) data;
2772 tree virtuals;
2773 tree old_virtuals;
2774 unsigned ix;
2776 if (!TYPE_CONTAINS_VPTR_P (BINFO_TYPE (binfo)))
2777 /* A base without a vtable needs no modification, and its bases
2778 are uninteresting. */
2779 return dfs_skip_bases;
2781 if (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), t)
2782 && !CLASSTYPE_HAS_PRIMARY_BASE_P (t))
2783 /* Don't do the primary vtable, if it's new. */
2784 return NULL_TREE;
2786 if (BINFO_PRIMARY_P (binfo) && !BINFO_VIRTUAL_P (binfo))
2787 /* There's no need to modify the vtable for a non-virtual primary
2788 base; we're not going to use that vtable anyhow. We do still
2789 need to do this for virtual primary bases, as they could become
2790 non-primary in a construction vtable. */
2791 return NULL_TREE;
2793 make_new_vtable (t, binfo);
2795 /* Now, go through each of the virtual functions in the virtual
2796 function table for BINFO. Find the final overrider, and update
2797 the BINFO_VIRTUALS list appropriately. */
2798 for (ix = 0, virtuals = BINFO_VIRTUALS (binfo),
2799 old_virtuals = BINFO_VIRTUALS (TYPE_BINFO (BINFO_TYPE (binfo)));
2800 virtuals;
2801 ix++, virtuals = TREE_CHAIN (virtuals),
2802 old_virtuals = TREE_CHAIN (old_virtuals))
2803 update_vtable_entry_for_fn (t,
2804 binfo,
2805 BV_FN (old_virtuals),
2806 &virtuals, ix);
2808 return NULL_TREE;
2811 /* Update all of the primary and secondary vtables for T. Create new
2812 vtables as required, and initialize their RTTI information. Each
2813 of the functions in VIRTUALS is declared in T and may override a
2814 virtual function from a base class; find and modify the appropriate
2815 entries to point to the overriding functions. Returns a list, in
2816 declaration order, of the virtual functions that are declared in T,
2817 but do not appear in the primary base class vtable, and which
2818 should therefore be appended to the end of the vtable for T. */
2820 static tree
2821 modify_all_vtables (tree t, tree virtuals)
2823 tree binfo = TYPE_BINFO (t);
2824 tree *fnsp;
2826 /* Mangle the vtable name before entering dfs_walk (c++/51884). */
2827 if (TYPE_CONTAINS_VPTR_P (t))
2828 get_vtable_decl (t, false);
2830 /* Update all of the vtables. */
2831 dfs_walk_once (binfo, dfs_modify_vtables, NULL, t);
2833 /* Add virtual functions not already in our primary vtable. These
2834 will be both those introduced by this class, and those overridden
2835 from secondary bases. It does not include virtuals merely
2836 inherited from secondary bases. */
2837 for (fnsp = &virtuals; *fnsp; )
2839 tree fn = TREE_VALUE (*fnsp);
2841 if (!value_member (fn, BINFO_VIRTUALS (binfo))
2842 || DECL_VINDEX (fn) == error_mark_node)
2844 /* We don't need to adjust the `this' pointer when
2845 calling this function. */
2846 BV_DELTA (*fnsp) = integer_zero_node;
2847 BV_VCALL_INDEX (*fnsp) = NULL_TREE;
2849 /* This is a function not already in our vtable. Keep it. */
2850 fnsp = &TREE_CHAIN (*fnsp);
2852 else
2853 /* We've already got an entry for this function. Skip it. */
2854 *fnsp = TREE_CHAIN (*fnsp);
2857 return virtuals;
2860 /* Get the base virtual function declarations in T that have the
2861 indicated NAME. */
2863 static void
2864 get_basefndecls (tree name, tree t, vec<tree> *base_fndecls)
2866 tree methods;
2867 int n_baseclasses = BINFO_N_BASE_BINFOS (TYPE_BINFO (t));
2868 int i;
2870 /* Find virtual functions in T with the indicated NAME. */
2871 i = lookup_fnfields_1 (t, name);
2872 bool found_decls = false;
2873 if (i != -1)
2874 for (methods = (*CLASSTYPE_METHOD_VEC (t))[i];
2875 methods;
2876 methods = OVL_NEXT (methods))
2878 tree method = OVL_CURRENT (methods);
2880 if (TREE_CODE (method) == FUNCTION_DECL
2881 && DECL_VINDEX (method))
2883 base_fndecls->safe_push (method);
2884 found_decls = true;
2888 if (found_decls)
2889 return;
2891 for (i = 0; i < n_baseclasses; i++)
2893 tree basetype = BINFO_TYPE (BINFO_BASE_BINFO (TYPE_BINFO (t), i));
2894 get_basefndecls (name, basetype, base_fndecls);
2898 /* If this declaration supersedes the declaration of
2899 a method declared virtual in the base class, then
2900 mark this field as being virtual as well. */
2902 void
2903 check_for_override (tree decl, tree ctype)
2905 bool overrides_found = false;
2906 if (TREE_CODE (decl) == TEMPLATE_DECL)
2907 /* In [temp.mem] we have:
2909 A specialization of a member function template does not
2910 override a virtual function from a base class. */
2911 return;
2912 if ((DECL_DESTRUCTOR_P (decl)
2913 || IDENTIFIER_VIRTUAL_P (DECL_NAME (decl))
2914 || DECL_CONV_FN_P (decl))
2915 && look_for_overrides (ctype, decl)
2916 && !DECL_STATIC_FUNCTION_P (decl))
2917 /* Set DECL_VINDEX to a value that is neither an INTEGER_CST nor
2918 the error_mark_node so that we know it is an overriding
2919 function. */
2921 DECL_VINDEX (decl) = decl;
2922 overrides_found = true;
2923 if (warn_override && !DECL_OVERRIDE_P (decl)
2924 && !DECL_DESTRUCTOR_P (decl))
2925 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wsuggest_override,
2926 "%qD can be marked override", decl);
2929 if (DECL_VIRTUAL_P (decl))
2931 if (!DECL_VINDEX (decl))
2932 DECL_VINDEX (decl) = error_mark_node;
2933 IDENTIFIER_VIRTUAL_P (DECL_NAME (decl)) = 1;
2934 if (DECL_DESTRUCTOR_P (decl))
2935 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (ctype) = true;
2937 else if (DECL_FINAL_P (decl))
2938 error ("%q+#D marked %<final%>, but is not virtual", decl);
2939 if (DECL_OVERRIDE_P (decl) && !overrides_found)
2940 error ("%q+#D marked %<override%>, but does not override", decl);
2943 /* Warn about hidden virtual functions that are not overridden in t.
2944 We know that constructors and destructors don't apply. */
2946 static void
2947 warn_hidden (tree t)
2949 vec<tree, va_gc> *method_vec = CLASSTYPE_METHOD_VEC (t);
2950 tree fns;
2951 size_t i;
2953 /* We go through each separately named virtual function. */
2954 for (i = CLASSTYPE_FIRST_CONVERSION_SLOT;
2955 vec_safe_iterate (method_vec, i, &fns);
2956 ++i)
2958 tree fn;
2959 tree name;
2960 tree fndecl;
2961 tree base_binfo;
2962 tree binfo;
2963 int j;
2965 /* All functions in this slot in the CLASSTYPE_METHOD_VEC will
2966 have the same name. Figure out what name that is. */
2967 name = DECL_NAME (OVL_CURRENT (fns));
2968 /* There are no possibly hidden functions yet. */
2969 auto_vec<tree, 20> base_fndecls;
2970 /* Iterate through all of the base classes looking for possibly
2971 hidden functions. */
2972 for (binfo = TYPE_BINFO (t), j = 0;
2973 BINFO_BASE_ITERATE (binfo, j, base_binfo); j++)
2975 tree basetype = BINFO_TYPE (base_binfo);
2976 get_basefndecls (name, basetype, &base_fndecls);
2979 /* If there are no functions to hide, continue. */
2980 if (base_fndecls.is_empty ())
2981 continue;
2983 /* Remove any overridden functions. */
2984 for (fn = fns; fn; fn = OVL_NEXT (fn))
2986 fndecl = OVL_CURRENT (fn);
2987 if (TREE_CODE (fndecl) == FUNCTION_DECL
2988 && DECL_VINDEX (fndecl))
2990 /* If the method from the base class has the same
2991 signature as the method from the derived class, it
2992 has been overridden. */
2993 for (size_t k = 0; k < base_fndecls.length (); k++)
2994 if (base_fndecls[k]
2995 && same_signature_p (fndecl, base_fndecls[k]))
2996 base_fndecls[k] = NULL_TREE;
3000 /* Now give a warning for all base functions without overriders,
3001 as they are hidden. */
3002 size_t k;
3003 tree base_fndecl;
3004 FOR_EACH_VEC_ELT (base_fndecls, k, base_fndecl)
3005 if (base_fndecl)
3007 /* Here we know it is a hider, and no overrider exists. */
3008 warning_at (location_of (base_fndecl),
3009 OPT_Woverloaded_virtual,
3010 "%qD was hidden", base_fndecl);
3011 warning_at (location_of (fns),
3012 OPT_Woverloaded_virtual, " by %qD", fns);
3017 /* Recursive helper for finish_struct_anon. */
3019 static void
3020 finish_struct_anon_r (tree field, bool complain)
3022 bool is_union = TREE_CODE (TREE_TYPE (field)) == UNION_TYPE;
3023 tree elt = TYPE_FIELDS (TREE_TYPE (field));
3024 for (; elt; elt = DECL_CHAIN (elt))
3026 /* We're generally only interested in entities the user
3027 declared, but we also find nested classes by noticing
3028 the TYPE_DECL that we create implicitly. You're
3029 allowed to put one anonymous union inside another,
3030 though, so we explicitly tolerate that. We use
3031 TYPE_ANONYMOUS_P rather than ANON_AGGR_TYPE_P so that
3032 we also allow unnamed types used for defining fields. */
3033 if (DECL_ARTIFICIAL (elt)
3034 && (!DECL_IMPLICIT_TYPEDEF_P (elt)
3035 || TYPE_ANONYMOUS_P (TREE_TYPE (elt))))
3036 continue;
3038 if (TREE_CODE (elt) != FIELD_DECL)
3040 /* We already complained about static data members in
3041 finish_static_data_member_decl. */
3042 if (complain && !VAR_P (elt))
3044 if (is_union)
3045 permerror (DECL_SOURCE_LOCATION (elt),
3046 "%q#D invalid; an anonymous union can "
3047 "only have non-static data members", elt);
3048 else
3049 permerror (DECL_SOURCE_LOCATION (elt),
3050 "%q#D invalid; an anonymous struct can "
3051 "only have non-static data members", elt);
3053 continue;
3056 if (complain)
3058 if (TREE_PRIVATE (elt))
3060 if (is_union)
3061 permerror (DECL_SOURCE_LOCATION (elt),
3062 "private member %q#D in anonymous union", elt);
3063 else
3064 permerror (DECL_SOURCE_LOCATION (elt),
3065 "private member %q#D in anonymous struct", elt);
3067 else if (TREE_PROTECTED (elt))
3069 if (is_union)
3070 permerror (DECL_SOURCE_LOCATION (elt),
3071 "protected member %q#D in anonymous union", elt);
3072 else
3073 permerror (DECL_SOURCE_LOCATION (elt),
3074 "protected member %q#D in anonymous struct", elt);
3078 TREE_PRIVATE (elt) = TREE_PRIVATE (field);
3079 TREE_PROTECTED (elt) = TREE_PROTECTED (field);
3081 /* Recurse into the anonymous aggregates to handle correctly
3082 access control (c++/24926):
3084 class A {
3085 union {
3086 union {
3087 int i;
3092 int j=A().i; */
3093 if (DECL_NAME (elt) == NULL_TREE
3094 && ANON_AGGR_TYPE_P (TREE_TYPE (elt)))
3095 finish_struct_anon_r (elt, /*complain=*/false);
3099 /* Check for things that are invalid. There are probably plenty of other
3100 things we should check for also. */
3102 static void
3103 finish_struct_anon (tree t)
3105 for (tree field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
3107 if (TREE_STATIC (field))
3108 continue;
3109 if (TREE_CODE (field) != FIELD_DECL)
3110 continue;
3112 if (DECL_NAME (field) == NULL_TREE
3113 && ANON_AGGR_TYPE_P (TREE_TYPE (field)))
3114 finish_struct_anon_r (field, /*complain=*/true);
3118 /* Add T to CLASSTYPE_DECL_LIST of current_class_type which
3119 will be used later during class template instantiation.
3120 When FRIEND_P is zero, T can be a static member data (VAR_DECL),
3121 a non-static member data (FIELD_DECL), a member function
3122 (FUNCTION_DECL), a nested type (RECORD_TYPE, ENUM_TYPE),
3123 a typedef (TYPE_DECL) or a member class template (TEMPLATE_DECL)
3124 When FRIEND_P is nonzero, T is either a friend class
3125 (RECORD_TYPE, TEMPLATE_DECL) or a friend function
3126 (FUNCTION_DECL, TEMPLATE_DECL). */
3128 void
3129 maybe_add_class_template_decl_list (tree type, tree t, int friend_p)
3131 /* Save some memory by not creating TREE_LIST if TYPE is not template. */
3132 if (CLASSTYPE_TEMPLATE_INFO (type))
3133 CLASSTYPE_DECL_LIST (type)
3134 = tree_cons (friend_p ? NULL_TREE : type,
3135 t, CLASSTYPE_DECL_LIST (type));
3138 /* This function is called from declare_virt_assop_and_dtor via
3139 dfs_walk_all.
3141 DATA is a type that direcly or indirectly inherits the base
3142 represented by BINFO. If BINFO contains a virtual assignment [copy
3143 assignment or move assigment] operator or a virtual constructor,
3144 declare that function in DATA if it hasn't been already declared. */
3146 static tree
3147 dfs_declare_virt_assop_and_dtor (tree binfo, void *data)
3149 tree bv, fn, t = (tree)data;
3150 tree opname = ansi_assopname (NOP_EXPR);
3152 gcc_assert (t && CLASS_TYPE_P (t));
3153 gcc_assert (binfo && TREE_CODE (binfo) == TREE_BINFO);
3155 if (!TYPE_CONTAINS_VPTR_P (BINFO_TYPE (binfo)))
3156 /* A base without a vtable needs no modification, and its bases
3157 are uninteresting. */
3158 return dfs_skip_bases;
3160 if (BINFO_PRIMARY_P (binfo))
3161 /* If this is a primary base, then we have already looked at the
3162 virtual functions of its vtable. */
3163 return NULL_TREE;
3165 for (bv = BINFO_VIRTUALS (binfo); bv; bv = TREE_CHAIN (bv))
3167 fn = BV_FN (bv);
3169 if (DECL_NAME (fn) == opname)
3171 if (CLASSTYPE_LAZY_COPY_ASSIGN (t))
3172 lazily_declare_fn (sfk_copy_assignment, t);
3173 if (CLASSTYPE_LAZY_MOVE_ASSIGN (t))
3174 lazily_declare_fn (sfk_move_assignment, t);
3176 else if (DECL_DESTRUCTOR_P (fn)
3177 && CLASSTYPE_LAZY_DESTRUCTOR (t))
3178 lazily_declare_fn (sfk_destructor, t);
3181 return NULL_TREE;
3184 /* If the class type T has a direct or indirect base that contains a
3185 virtual assignment operator or a virtual destructor, declare that
3186 function in T if it hasn't been already declared. */
3188 static void
3189 declare_virt_assop_and_dtor (tree t)
3191 if (!(TYPE_POLYMORPHIC_P (t)
3192 && (CLASSTYPE_LAZY_COPY_ASSIGN (t)
3193 || CLASSTYPE_LAZY_MOVE_ASSIGN (t)
3194 || CLASSTYPE_LAZY_DESTRUCTOR (t))))
3195 return;
3197 dfs_walk_all (TYPE_BINFO (t),
3198 dfs_declare_virt_assop_and_dtor,
3199 NULL, t);
3202 /* Declare the inheriting constructor for class T inherited from base
3203 constructor CTOR with the parameter array PARMS of size NPARMS. */
3205 static void
3206 one_inheriting_sig (tree t, tree ctor, tree *parms, int nparms)
3208 /* We don't declare an inheriting ctor that would be a default,
3209 copy or move ctor for derived or base. */
3210 if (nparms == 0)
3211 return;
3212 if (nparms == 1
3213 && TREE_CODE (parms[0]) == REFERENCE_TYPE)
3215 tree parm = TYPE_MAIN_VARIANT (TREE_TYPE (parms[0]));
3216 if (parm == t || parm == DECL_CONTEXT (ctor))
3217 return;
3220 tree parmlist = void_list_node;
3221 for (int i = nparms - 1; i >= 0; i--)
3222 parmlist = tree_cons (NULL_TREE, parms[i], parmlist);
3223 tree fn = implicitly_declare_fn (sfk_inheriting_constructor,
3224 t, false, ctor, parmlist);
3225 gcc_assert (TYPE_MAIN_VARIANT (t) == t);
3226 if (add_method (t, fn, NULL_TREE))
3228 DECL_CHAIN (fn) = TYPE_METHODS (t);
3229 TYPE_METHODS (t) = fn;
3233 /* Declare all the inheriting constructors for class T inherited from base
3234 constructor CTOR. */
3236 static void
3237 one_inherited_ctor (tree ctor, tree t)
3239 tree parms = FUNCTION_FIRST_USER_PARMTYPE (ctor);
3241 tree *new_parms = XALLOCAVEC (tree, list_length (parms));
3242 int i = 0;
3243 for (; parms && parms != void_list_node; parms = TREE_CHAIN (parms))
3245 if (TREE_PURPOSE (parms))
3246 one_inheriting_sig (t, ctor, new_parms, i);
3247 new_parms[i++] = TREE_VALUE (parms);
3249 one_inheriting_sig (t, ctor, new_parms, i);
3250 if (parms == NULL_TREE)
3252 if (warning (OPT_Winherited_variadic_ctor,
3253 "the ellipsis in %qD is not inherited", ctor))
3254 inform (DECL_SOURCE_LOCATION (ctor), "%qD declared here", ctor);
3258 /* Create default constructors, assignment operators, and so forth for
3259 the type indicated by T, if they are needed. CANT_HAVE_CONST_CTOR,
3260 and CANT_HAVE_CONST_ASSIGNMENT are nonzero if, for whatever reason,
3261 the class cannot have a default constructor, copy constructor
3262 taking a const reference argument, or an assignment operator taking
3263 a const reference, respectively. */
3265 static void
3266 add_implicitly_declared_members (tree t, tree* access_decls,
3267 int cant_have_const_cctor,
3268 int cant_have_const_assignment)
3270 bool move_ok = false;
3272 if (cxx_dialect >= cxx11 && !CLASSTYPE_DESTRUCTORS (t)
3273 && !TYPE_HAS_COPY_CTOR (t) && !TYPE_HAS_COPY_ASSIGN (t)
3274 && !type_has_move_constructor (t) && !type_has_move_assign (t))
3275 move_ok = true;
3277 /* Destructor. */
3278 if (!CLASSTYPE_DESTRUCTORS (t))
3280 /* In general, we create destructors lazily. */
3281 CLASSTYPE_LAZY_DESTRUCTOR (t) = 1;
3283 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t)
3284 && TYPE_FOR_JAVA (t))
3285 /* But if this is a Java class, any non-trivial destructor is
3286 invalid, even if compiler-generated. Therefore, if the
3287 destructor is non-trivial we create it now. */
3288 lazily_declare_fn (sfk_destructor, t);
3291 /* [class.ctor]
3293 If there is no user-declared constructor for a class, a default
3294 constructor is implicitly declared. */
3295 if (! TYPE_HAS_USER_CONSTRUCTOR (t))
3297 TYPE_HAS_DEFAULT_CONSTRUCTOR (t) = 1;
3298 CLASSTYPE_LAZY_DEFAULT_CTOR (t) = 1;
3299 if (cxx_dialect >= cxx11)
3300 TYPE_HAS_CONSTEXPR_CTOR (t)
3301 /* This might force the declaration. */
3302 = type_has_constexpr_default_constructor (t);
3305 /* [class.ctor]
3307 If a class definition does not explicitly declare a copy
3308 constructor, one is declared implicitly. */
3309 if (! TYPE_HAS_COPY_CTOR (t) && ! TYPE_FOR_JAVA (t))
3311 TYPE_HAS_COPY_CTOR (t) = 1;
3312 TYPE_HAS_CONST_COPY_CTOR (t) = !cant_have_const_cctor;
3313 CLASSTYPE_LAZY_COPY_CTOR (t) = 1;
3314 if (move_ok)
3315 CLASSTYPE_LAZY_MOVE_CTOR (t) = 1;
3318 /* If there is no assignment operator, one will be created if and
3319 when it is needed. For now, just record whether or not the type
3320 of the parameter to the assignment operator will be a const or
3321 non-const reference. */
3322 if (!TYPE_HAS_COPY_ASSIGN (t) && !TYPE_FOR_JAVA (t))
3324 TYPE_HAS_COPY_ASSIGN (t) = 1;
3325 TYPE_HAS_CONST_COPY_ASSIGN (t) = !cant_have_const_assignment;
3326 CLASSTYPE_LAZY_COPY_ASSIGN (t) = 1;
3327 if (move_ok && !LAMBDA_TYPE_P (t))
3328 CLASSTYPE_LAZY_MOVE_ASSIGN (t) = 1;
3331 /* We can't be lazy about declaring functions that might override
3332 a virtual function from a base class. */
3333 declare_virt_assop_and_dtor (t);
3335 while (*access_decls)
3337 tree using_decl = TREE_VALUE (*access_decls);
3338 tree decl = USING_DECL_DECLS (using_decl);
3339 if (DECL_NAME (using_decl) == ctor_identifier)
3341 /* declare, then remove the decl */
3342 tree ctor_list = decl;
3343 location_t loc = input_location;
3344 input_location = DECL_SOURCE_LOCATION (using_decl);
3345 if (ctor_list)
3346 for (; ctor_list; ctor_list = OVL_NEXT (ctor_list))
3347 one_inherited_ctor (OVL_CURRENT (ctor_list), t);
3348 *access_decls = TREE_CHAIN (*access_decls);
3349 input_location = loc;
3351 else
3352 access_decls = &TREE_CHAIN (*access_decls);
3356 /* Subroutine of insert_into_classtype_sorted_fields. Recursively
3357 count the number of fields in TYPE, including anonymous union
3358 members. */
3360 static int
3361 count_fields (tree fields)
3363 tree x;
3364 int n_fields = 0;
3365 for (x = fields; x; x = DECL_CHAIN (x))
3367 if (TREE_CODE (x) == FIELD_DECL && ANON_AGGR_TYPE_P (TREE_TYPE (x)))
3368 n_fields += count_fields (TYPE_FIELDS (TREE_TYPE (x)));
3369 else
3370 n_fields += 1;
3372 return n_fields;
3375 /* Subroutine of insert_into_classtype_sorted_fields. Recursively add
3376 all the fields in the TREE_LIST FIELDS to the SORTED_FIELDS_TYPE
3377 elts, starting at offset IDX. */
3379 static int
3380 add_fields_to_record_type (tree fields, struct sorted_fields_type *field_vec, int idx)
3382 tree x;
3383 for (x = fields; x; x = DECL_CHAIN (x))
3385 if (TREE_CODE (x) == FIELD_DECL && ANON_AGGR_TYPE_P (TREE_TYPE (x)))
3386 idx = add_fields_to_record_type (TYPE_FIELDS (TREE_TYPE (x)), field_vec, idx);
3387 else
3388 field_vec->elts[idx++] = x;
3390 return idx;
3393 /* Add all of the enum values of ENUMTYPE, to the FIELD_VEC elts,
3394 starting at offset IDX. */
3396 static int
3397 add_enum_fields_to_record_type (tree enumtype,
3398 struct sorted_fields_type *field_vec,
3399 int idx)
3401 tree values;
3402 for (values = TYPE_VALUES (enumtype); values; values = TREE_CHAIN (values))
3403 field_vec->elts[idx++] = TREE_VALUE (values);
3404 return idx;
3407 /* FIELD is a bit-field. We are finishing the processing for its
3408 enclosing type. Issue any appropriate messages and set appropriate
3409 flags. Returns false if an error has been diagnosed. */
3411 static bool
3412 check_bitfield_decl (tree field)
3414 tree type = TREE_TYPE (field);
3415 tree w;
3417 /* Extract the declared width of the bitfield, which has been
3418 temporarily stashed in DECL_INITIAL. */
3419 w = DECL_INITIAL (field);
3420 gcc_assert (w != NULL_TREE);
3421 /* Remove the bit-field width indicator so that the rest of the
3422 compiler does not treat that value as an initializer. */
3423 DECL_INITIAL (field) = NULL_TREE;
3425 /* Detect invalid bit-field type. */
3426 if (!INTEGRAL_OR_ENUMERATION_TYPE_P (type))
3428 error ("bit-field %q+#D with non-integral type", field);
3429 w = error_mark_node;
3431 else
3433 location_t loc = input_location;
3434 /* Avoid the non_lvalue wrapper added by fold for PLUS_EXPRs. */
3435 STRIP_NOPS (w);
3437 /* detect invalid field size. */
3438 input_location = DECL_SOURCE_LOCATION (field);
3439 w = cxx_constant_value (w);
3440 input_location = loc;
3442 if (TREE_CODE (w) != INTEGER_CST)
3444 error ("bit-field %q+D width not an integer constant", field);
3445 w = error_mark_node;
3447 else if (tree_int_cst_sgn (w) < 0)
3449 error ("negative width in bit-field %q+D", field);
3450 w = error_mark_node;
3452 else if (integer_zerop (w) && DECL_NAME (field) != 0)
3454 error ("zero width for bit-field %q+D", field);
3455 w = error_mark_node;
3457 else if ((TREE_CODE (type) != ENUMERAL_TYPE
3458 && TREE_CODE (type) != BOOLEAN_TYPE
3459 && compare_tree_int (w, TYPE_PRECISION (type)) > 0)
3460 || ((TREE_CODE (type) == ENUMERAL_TYPE
3461 || TREE_CODE (type) == BOOLEAN_TYPE)
3462 && tree_int_cst_lt (TYPE_SIZE (type), w)))
3463 warning_at (DECL_SOURCE_LOCATION (field), 0,
3464 "width of %qD exceeds its type", field);
3465 else if (TREE_CODE (type) == ENUMERAL_TYPE
3466 && (0 > (compare_tree_int
3467 (w, TYPE_PRECISION (ENUM_UNDERLYING_TYPE (type))))))
3468 warning_at (DECL_SOURCE_LOCATION (field), 0,
3469 "%qD is too small to hold all values of %q#T",
3470 field, type);
3473 if (w != error_mark_node)
3475 DECL_SIZE (field) = fold_convert (bitsizetype, w);
3476 DECL_BIT_FIELD (field) = 1;
3477 return true;
3479 else
3481 /* Non-bit-fields are aligned for their type. */
3482 DECL_BIT_FIELD (field) = 0;
3483 CLEAR_DECL_C_BIT_FIELD (field);
3484 return false;
3488 /* FIELD is a non bit-field. We are finishing the processing for its
3489 enclosing type T. Issue any appropriate messages and set appropriate
3490 flags. */
3492 static void
3493 check_field_decl (tree field,
3494 tree t,
3495 int* cant_have_const_ctor,
3496 int* no_const_asn_ref,
3497 int* any_default_members)
3499 tree type = strip_array_types (TREE_TYPE (field));
3501 /* In C++98 an anonymous union cannot contain any fields which would change
3502 the settings of CANT_HAVE_CONST_CTOR and friends. */
3503 if (ANON_UNION_TYPE_P (type) && cxx_dialect < cxx11)
3505 /* And, we don't set TYPE_HAS_CONST_COPY_CTOR, etc., for anonymous
3506 structs. So, we recurse through their fields here. */
3507 else if (ANON_AGGR_TYPE_P (type))
3509 tree fields;
3511 for (fields = TYPE_FIELDS (type); fields; fields = DECL_CHAIN (fields))
3512 if (TREE_CODE (fields) == FIELD_DECL && !DECL_C_BIT_FIELD (field))
3513 check_field_decl (fields, t, cant_have_const_ctor,
3514 no_const_asn_ref, any_default_members);
3516 /* Check members with class type for constructors, destructors,
3517 etc. */
3518 else if (CLASS_TYPE_P (type))
3520 /* Never let anything with uninheritable virtuals
3521 make it through without complaint. */
3522 abstract_virtuals_error (field, type);
3524 if (TREE_CODE (t) == UNION_TYPE && cxx_dialect < cxx11)
3526 static bool warned;
3527 int oldcount = errorcount;
3528 if (TYPE_NEEDS_CONSTRUCTING (type))
3529 error ("member %q+#D with constructor not allowed in union",
3530 field);
3531 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
3532 error ("member %q+#D with destructor not allowed in union", field);
3533 if (TYPE_HAS_COMPLEX_COPY_ASSIGN (type))
3534 error ("member %q+#D with copy assignment operator not allowed in union",
3535 field);
3536 if (!warned && errorcount > oldcount)
3538 inform (DECL_SOURCE_LOCATION (field), "unrestricted unions "
3539 "only available with -std=c++11 or -std=gnu++11");
3540 warned = true;
3543 else
3545 TYPE_NEEDS_CONSTRUCTING (t) |= TYPE_NEEDS_CONSTRUCTING (type);
3546 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t)
3547 |= TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type);
3548 TYPE_HAS_COMPLEX_COPY_ASSIGN (t)
3549 |= (TYPE_HAS_COMPLEX_COPY_ASSIGN (type)
3550 || !TYPE_HAS_COPY_ASSIGN (type));
3551 TYPE_HAS_COMPLEX_COPY_CTOR (t) |= (TYPE_HAS_COMPLEX_COPY_CTOR (type)
3552 || !TYPE_HAS_COPY_CTOR (type));
3553 TYPE_HAS_COMPLEX_MOVE_ASSIGN (t) |= TYPE_HAS_COMPLEX_MOVE_ASSIGN (type);
3554 TYPE_HAS_COMPLEX_MOVE_CTOR (t) |= TYPE_HAS_COMPLEX_MOVE_CTOR (type);
3555 TYPE_HAS_COMPLEX_DFLT (t) |= (!TYPE_HAS_DEFAULT_CONSTRUCTOR (type)
3556 || TYPE_HAS_COMPLEX_DFLT (type));
3559 if (TYPE_HAS_COPY_CTOR (type)
3560 && !TYPE_HAS_CONST_COPY_CTOR (type))
3561 *cant_have_const_ctor = 1;
3563 if (TYPE_HAS_COPY_ASSIGN (type)
3564 && !TYPE_HAS_CONST_COPY_ASSIGN (type))
3565 *no_const_asn_ref = 1;
3568 check_abi_tags (t, field);
3570 if (DECL_INITIAL (field) != NULL_TREE)
3572 /* `build_class_init_list' does not recognize
3573 non-FIELD_DECLs. */
3574 if (TREE_CODE (t) == UNION_TYPE && *any_default_members != 0)
3575 error ("multiple fields in union %qT initialized", t);
3576 *any_default_members = 1;
3580 /* Check the data members (both static and non-static), class-scoped
3581 typedefs, etc., appearing in the declaration of T. Issue
3582 appropriate diagnostics. Sets ACCESS_DECLS to a list (in
3583 declaration order) of access declarations; each TREE_VALUE in this
3584 list is a USING_DECL.
3586 In addition, set the following flags:
3588 EMPTY_P
3589 The class is empty, i.e., contains no non-static data members.
3591 CANT_HAVE_CONST_CTOR_P
3592 This class cannot have an implicitly generated copy constructor
3593 taking a const reference.
3595 CANT_HAVE_CONST_ASN_REF
3596 This class cannot have an implicitly generated assignment
3597 operator taking a const reference.
3599 All of these flags should be initialized before calling this
3600 function.
3602 Returns a pointer to the end of the TYPE_FIELDs chain; additional
3603 fields can be added by adding to this chain. */
3605 static void
3606 check_field_decls (tree t, tree *access_decls,
3607 int *cant_have_const_ctor_p,
3608 int *no_const_asn_ref_p)
3610 tree *field;
3611 tree *next;
3612 bool has_pointers;
3613 int any_default_members;
3614 int cant_pack = 0;
3615 int field_access = -1;
3617 /* Assume there are no access declarations. */
3618 *access_decls = NULL_TREE;
3619 /* Assume this class has no pointer members. */
3620 has_pointers = false;
3621 /* Assume none of the members of this class have default
3622 initializations. */
3623 any_default_members = 0;
3625 for (field = &TYPE_FIELDS (t); *field; field = next)
3627 tree x = *field;
3628 tree type = TREE_TYPE (x);
3629 int this_field_access;
3631 next = &DECL_CHAIN (x);
3633 if (TREE_CODE (x) == USING_DECL)
3635 /* Save the access declarations for our caller. */
3636 *access_decls = tree_cons (NULL_TREE, x, *access_decls);
3637 continue;
3640 if (TREE_CODE (x) == TYPE_DECL
3641 || TREE_CODE (x) == TEMPLATE_DECL)
3642 continue;
3644 /* If we've gotten this far, it's a data member, possibly static,
3645 or an enumerator. */
3646 if (TREE_CODE (x) != CONST_DECL)
3647 DECL_CONTEXT (x) = t;
3649 /* When this goes into scope, it will be a non-local reference. */
3650 DECL_NONLOCAL (x) = 1;
3652 if (TREE_CODE (t) == UNION_TYPE
3653 && cxx_dialect < cxx11)
3655 /* [class.union] (C++98)
3657 If a union contains a static data member, or a member of
3658 reference type, the program is ill-formed.
3660 In C++11 this limitation doesn't exist anymore. */
3661 if (VAR_P (x))
3663 error ("in C++98 %q+D may not be static because it is "
3664 "a member of a union", x);
3665 continue;
3667 if (TREE_CODE (type) == REFERENCE_TYPE)
3669 error ("in C++98 %q+D may not have reference type %qT "
3670 "because it is a member of a union", x, type);
3671 continue;
3675 /* Perform error checking that did not get done in
3676 grokdeclarator. */
3677 if (TREE_CODE (type) == FUNCTION_TYPE)
3679 error ("field %q+D invalidly declared function type", x);
3680 type = build_pointer_type (type);
3681 TREE_TYPE (x) = type;
3683 else if (TREE_CODE (type) == METHOD_TYPE)
3685 error ("field %q+D invalidly declared method type", x);
3686 type = build_pointer_type (type);
3687 TREE_TYPE (x) = type;
3690 if (type == error_mark_node)
3691 continue;
3693 if (TREE_CODE (x) == CONST_DECL || VAR_P (x))
3694 continue;
3696 /* Now it can only be a FIELD_DECL. */
3698 if (TREE_PRIVATE (x) || TREE_PROTECTED (x))
3699 CLASSTYPE_NON_AGGREGATE (t) = 1;
3701 /* If at least one non-static data member is non-literal, the whole
3702 class becomes non-literal. Per Core/1453, volatile non-static
3703 data members and base classes are also not allowed.
3704 Note: if the type is incomplete we will complain later on. */
3705 if (COMPLETE_TYPE_P (type)
3706 && (!literal_type_p (type) || CP_TYPE_VOLATILE_P (type)))
3707 CLASSTYPE_LITERAL_P (t) = false;
3709 /* A standard-layout class is a class that:
3711 has the same access control (Clause 11) for all non-static data members,
3712 ... */
3713 this_field_access = TREE_PROTECTED (x) ? 1 : TREE_PRIVATE (x) ? 2 : 0;
3714 if (field_access == -1)
3715 field_access = this_field_access;
3716 else if (this_field_access != field_access)
3717 CLASSTYPE_NON_STD_LAYOUT (t) = 1;
3719 /* If this is of reference type, check if it needs an init. */
3720 if (TREE_CODE (type) == REFERENCE_TYPE)
3722 CLASSTYPE_NON_LAYOUT_POD_P (t) = 1;
3723 CLASSTYPE_NON_STD_LAYOUT (t) = 1;
3724 if (DECL_INITIAL (x) == NULL_TREE)
3725 SET_CLASSTYPE_REF_FIELDS_NEED_INIT (t, 1);
3726 if (cxx_dialect < cxx11)
3728 /* ARM $12.6.2: [A member initializer list] (or, for an
3729 aggregate, initialization by a brace-enclosed list) is the
3730 only way to initialize nonstatic const and reference
3731 members. */
3732 TYPE_HAS_COMPLEX_COPY_ASSIGN (t) = 1;
3733 TYPE_HAS_COMPLEX_MOVE_ASSIGN (t) = 1;
3737 type = strip_array_types (type);
3739 if (TYPE_PACKED (t))
3741 if (!layout_pod_type_p (type) && !TYPE_PACKED (type))
3743 warning_at
3744 (DECL_SOURCE_LOCATION (x), 0,
3745 "ignoring packed attribute because of unpacked non-POD field %q#D",
3747 cant_pack = 1;
3749 else if (DECL_C_BIT_FIELD (x)
3750 || TYPE_ALIGN (TREE_TYPE (x)) > BITS_PER_UNIT)
3751 DECL_PACKED (x) = 1;
3754 if (DECL_C_BIT_FIELD (x) && integer_zerop (DECL_INITIAL (x)))
3755 /* We don't treat zero-width bitfields as making a class
3756 non-empty. */
3758 else
3760 /* The class is non-empty. */
3761 CLASSTYPE_EMPTY_P (t) = 0;
3762 /* The class is not even nearly empty. */
3763 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
3764 /* If one of the data members contains an empty class,
3765 so does T. */
3766 if (CLASS_TYPE_P (type)
3767 && CLASSTYPE_CONTAINS_EMPTY_CLASS_P (type))
3768 CLASSTYPE_CONTAINS_EMPTY_CLASS_P (t) = 1;
3771 /* This is used by -Weffc++ (see below). Warn only for pointers
3772 to members which might hold dynamic memory. So do not warn
3773 for pointers to functions or pointers to members. */
3774 if (TYPE_PTR_P (type)
3775 && !TYPE_PTRFN_P (type))
3776 has_pointers = true;
3778 if (CLASS_TYPE_P (type))
3780 if (CLASSTYPE_REF_FIELDS_NEED_INIT (type))
3781 SET_CLASSTYPE_REF_FIELDS_NEED_INIT (t, 1);
3782 if (CLASSTYPE_READONLY_FIELDS_NEED_INIT (type))
3783 SET_CLASSTYPE_READONLY_FIELDS_NEED_INIT (t, 1);
3786 if (DECL_MUTABLE_P (x) || TYPE_HAS_MUTABLE_P (type))
3787 CLASSTYPE_HAS_MUTABLE (t) = 1;
3789 if (DECL_MUTABLE_P (x))
3791 if (CP_TYPE_CONST_P (type))
3793 error ("member %q+D cannot be declared both %<const%> "
3794 "and %<mutable%>", x);
3795 continue;
3797 if (TREE_CODE (type) == REFERENCE_TYPE)
3799 error ("member %q+D cannot be declared as a %<mutable%> "
3800 "reference", x);
3801 continue;
3805 if (! layout_pod_type_p (type))
3806 /* DR 148 now allows pointers to members (which are POD themselves),
3807 to be allowed in POD structs. */
3808 CLASSTYPE_NON_LAYOUT_POD_P (t) = 1;
3810 if (!std_layout_type_p (type))
3811 CLASSTYPE_NON_STD_LAYOUT (t) = 1;
3813 if (! zero_init_p (type))
3814 CLASSTYPE_NON_ZERO_INIT_P (t) = 1;
3816 /* We set DECL_C_BIT_FIELD in grokbitfield.
3817 If the type and width are valid, we'll also set DECL_BIT_FIELD. */
3818 if (! DECL_C_BIT_FIELD (x) || ! check_bitfield_decl (x))
3819 check_field_decl (x, t,
3820 cant_have_const_ctor_p,
3821 no_const_asn_ref_p,
3822 &any_default_members);
3824 /* Now that we've removed bit-field widths from DECL_INITIAL,
3825 anything left in DECL_INITIAL is an NSDMI that makes the class
3826 non-aggregate in C++11. */
3827 if (DECL_INITIAL (x) && cxx_dialect < cxx14)
3828 CLASSTYPE_NON_AGGREGATE (t) = true;
3830 /* If any field is const, the structure type is pseudo-const. */
3831 if (CP_TYPE_CONST_P (type))
3833 C_TYPE_FIELDS_READONLY (t) = 1;
3834 if (DECL_INITIAL (x) == NULL_TREE)
3835 SET_CLASSTYPE_READONLY_FIELDS_NEED_INIT (t, 1);
3836 if (cxx_dialect < cxx11)
3838 /* ARM $12.6.2: [A member initializer list] (or, for an
3839 aggregate, initialization by a brace-enclosed list) is the
3840 only way to initialize nonstatic const and reference
3841 members. */
3842 TYPE_HAS_COMPLEX_COPY_ASSIGN (t) = 1;
3843 TYPE_HAS_COMPLEX_MOVE_ASSIGN (t) = 1;
3846 /* A field that is pseudo-const makes the structure likewise. */
3847 else if (CLASS_TYPE_P (type))
3849 C_TYPE_FIELDS_READONLY (t) |= C_TYPE_FIELDS_READONLY (type);
3850 SET_CLASSTYPE_READONLY_FIELDS_NEED_INIT (t,
3851 CLASSTYPE_READONLY_FIELDS_NEED_INIT (t)
3852 | CLASSTYPE_READONLY_FIELDS_NEED_INIT (type));
3855 /* Core issue 80: A nonstatic data member is required to have a
3856 different name from the class iff the class has a
3857 user-declared constructor. */
3858 if (constructor_name_p (DECL_NAME (x), t)
3859 && TYPE_HAS_USER_CONSTRUCTOR (t))
3860 permerror (DECL_SOURCE_LOCATION (x),
3861 "field %q#D with same name as class", x);
3864 /* Effective C++ rule 11: if a class has dynamic memory held by pointers,
3865 it should also define a copy constructor and an assignment operator to
3866 implement the correct copy semantic (deep vs shallow, etc.). As it is
3867 not feasible to check whether the constructors do allocate dynamic memory
3868 and store it within members, we approximate the warning like this:
3870 -- Warn only if there are members which are pointers
3871 -- Warn only if there is a non-trivial constructor (otherwise,
3872 there cannot be memory allocated).
3873 -- Warn only if there is a non-trivial destructor. We assume that the
3874 user at least implemented the cleanup correctly, and a destructor
3875 is needed to free dynamic memory.
3877 This seems enough for practical purposes. */
3878 if (warn_ecpp
3879 && has_pointers
3880 && TYPE_HAS_USER_CONSTRUCTOR (t)
3881 && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t)
3882 && !(TYPE_HAS_COPY_CTOR (t) && TYPE_HAS_COPY_ASSIGN (t)))
3884 warning (OPT_Weffc__, "%q#T has pointer data members", t);
3886 if (! TYPE_HAS_COPY_CTOR (t))
3888 warning (OPT_Weffc__,
3889 " but does not override %<%T(const %T&)%>", t, t);
3890 if (!TYPE_HAS_COPY_ASSIGN (t))
3891 warning (OPT_Weffc__, " or %<operator=(const %T&)%>", t);
3893 else if (! TYPE_HAS_COPY_ASSIGN (t))
3894 warning (OPT_Weffc__,
3895 " but does not override %<operator=(const %T&)%>", t);
3898 /* Non-static data member initializers make the default constructor
3899 non-trivial. */
3900 if (any_default_members)
3902 TYPE_NEEDS_CONSTRUCTING (t) = true;
3903 TYPE_HAS_COMPLEX_DFLT (t) = true;
3906 /* If any of the fields couldn't be packed, unset TYPE_PACKED. */
3907 if (cant_pack)
3908 TYPE_PACKED (t) = 0;
3910 /* Check anonymous struct/anonymous union fields. */
3911 finish_struct_anon (t);
3913 /* We've built up the list of access declarations in reverse order.
3914 Fix that now. */
3915 *access_decls = nreverse (*access_decls);
3918 /* If TYPE is an empty class type, records its OFFSET in the table of
3919 OFFSETS. */
3921 static int
3922 record_subobject_offset (tree type, tree offset, splay_tree offsets)
3924 splay_tree_node n;
3926 if (!is_empty_class (type))
3927 return 0;
3929 /* Record the location of this empty object in OFFSETS. */
3930 n = splay_tree_lookup (offsets, (splay_tree_key) offset);
3931 if (!n)
3932 n = splay_tree_insert (offsets,
3933 (splay_tree_key) offset,
3934 (splay_tree_value) NULL_TREE);
3935 n->value = ((splay_tree_value)
3936 tree_cons (NULL_TREE,
3937 type,
3938 (tree) n->value));
3940 return 0;
3943 /* Returns nonzero if TYPE is an empty class type and there is
3944 already an entry in OFFSETS for the same TYPE as the same OFFSET. */
3946 static int
3947 check_subobject_offset (tree type, tree offset, splay_tree offsets)
3949 splay_tree_node n;
3950 tree t;
3952 if (!is_empty_class (type))
3953 return 0;
3955 /* Record the location of this empty object in OFFSETS. */
3956 n = splay_tree_lookup (offsets, (splay_tree_key) offset);
3957 if (!n)
3958 return 0;
3960 for (t = (tree) n->value; t; t = TREE_CHAIN (t))
3961 if (same_type_p (TREE_VALUE (t), type))
3962 return 1;
3964 return 0;
3967 /* Walk through all the subobjects of TYPE (located at OFFSET). Call
3968 F for every subobject, passing it the type, offset, and table of
3969 OFFSETS. If VBASES_P is one, then virtual non-primary bases should
3970 be traversed.
3972 If MAX_OFFSET is non-NULL, then subobjects with an offset greater
3973 than MAX_OFFSET will not be walked.
3975 If F returns a nonzero value, the traversal ceases, and that value
3976 is returned. Otherwise, returns zero. */
3978 static int
3979 walk_subobject_offsets (tree type,
3980 subobject_offset_fn f,
3981 tree offset,
3982 splay_tree offsets,
3983 tree max_offset,
3984 int vbases_p)
3986 int r = 0;
3987 tree type_binfo = NULL_TREE;
3989 /* If this OFFSET is bigger than the MAX_OFFSET, then we should
3990 stop. */
3991 if (max_offset && tree_int_cst_lt (max_offset, offset))
3992 return 0;
3994 if (type == error_mark_node)
3995 return 0;
3997 if (!TYPE_P (type))
3999 type_binfo = type;
4000 type = BINFO_TYPE (type);
4003 if (CLASS_TYPE_P (type))
4005 tree field;
4006 tree binfo;
4007 int i;
4009 /* Avoid recursing into objects that are not interesting. */
4010 if (!CLASSTYPE_CONTAINS_EMPTY_CLASS_P (type))
4011 return 0;
4013 /* Record the location of TYPE. */
4014 r = (*f) (type, offset, offsets);
4015 if (r)
4016 return r;
4018 /* Iterate through the direct base classes of TYPE. */
4019 if (!type_binfo)
4020 type_binfo = TYPE_BINFO (type);
4021 for (i = 0; BINFO_BASE_ITERATE (type_binfo, i, binfo); i++)
4023 tree binfo_offset;
4025 if (BINFO_VIRTUAL_P (binfo))
4026 continue;
4028 tree orig_binfo;
4029 /* We cannot rely on BINFO_OFFSET being set for the base
4030 class yet, but the offsets for direct non-virtual
4031 bases can be calculated by going back to the TYPE. */
4032 orig_binfo = BINFO_BASE_BINFO (TYPE_BINFO (type), i);
4033 binfo_offset = size_binop (PLUS_EXPR,
4034 offset,
4035 BINFO_OFFSET (orig_binfo));
4037 r = walk_subobject_offsets (binfo,
4039 binfo_offset,
4040 offsets,
4041 max_offset,
4042 /*vbases_p=*/0);
4043 if (r)
4044 return r;
4047 if (CLASSTYPE_VBASECLASSES (type))
4049 unsigned ix;
4050 vec<tree, va_gc> *vbases;
4052 /* Iterate through the virtual base classes of TYPE. In G++
4053 3.2, we included virtual bases in the direct base class
4054 loop above, which results in incorrect results; the
4055 correct offsets for virtual bases are only known when
4056 working with the most derived type. */
4057 if (vbases_p)
4058 for (vbases = CLASSTYPE_VBASECLASSES (type), ix = 0;
4059 vec_safe_iterate (vbases, ix, &binfo); ix++)
4061 r = walk_subobject_offsets (binfo,
4063 size_binop (PLUS_EXPR,
4064 offset,
4065 BINFO_OFFSET (binfo)),
4066 offsets,
4067 max_offset,
4068 /*vbases_p=*/0);
4069 if (r)
4070 return r;
4072 else
4074 /* We still have to walk the primary base, if it is
4075 virtual. (If it is non-virtual, then it was walked
4076 above.) */
4077 tree vbase = get_primary_binfo (type_binfo);
4079 if (vbase && BINFO_VIRTUAL_P (vbase)
4080 && BINFO_PRIMARY_P (vbase)
4081 && BINFO_INHERITANCE_CHAIN (vbase) == type_binfo)
4083 r = (walk_subobject_offsets
4084 (vbase, f, offset,
4085 offsets, max_offset, /*vbases_p=*/0));
4086 if (r)
4087 return r;
4092 /* Iterate through the fields of TYPE. */
4093 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
4094 if (TREE_CODE (field) == FIELD_DECL
4095 && TREE_TYPE (field) != error_mark_node
4096 && !DECL_ARTIFICIAL (field))
4098 tree field_offset;
4100 field_offset = byte_position (field);
4102 r = walk_subobject_offsets (TREE_TYPE (field),
4104 size_binop (PLUS_EXPR,
4105 offset,
4106 field_offset),
4107 offsets,
4108 max_offset,
4109 /*vbases_p=*/1);
4110 if (r)
4111 return r;
4114 else if (TREE_CODE (type) == ARRAY_TYPE)
4116 tree element_type = strip_array_types (type);
4117 tree domain = TYPE_DOMAIN (type);
4118 tree index;
4120 /* Avoid recursing into objects that are not interesting. */
4121 if (!CLASS_TYPE_P (element_type)
4122 || !CLASSTYPE_CONTAINS_EMPTY_CLASS_P (element_type)
4123 || !domain)
4124 return 0;
4126 /* Step through each of the elements in the array. */
4127 for (index = size_zero_node;
4128 !tree_int_cst_lt (TYPE_MAX_VALUE (domain), index);
4129 index = size_binop (PLUS_EXPR, index, size_one_node))
4131 r = walk_subobject_offsets (TREE_TYPE (type),
4133 offset,
4134 offsets,
4135 max_offset,
4136 /*vbases_p=*/1);
4137 if (r)
4138 return r;
4139 offset = size_binop (PLUS_EXPR, offset,
4140 TYPE_SIZE_UNIT (TREE_TYPE (type)));
4141 /* If this new OFFSET is bigger than the MAX_OFFSET, then
4142 there's no point in iterating through the remaining
4143 elements of the array. */
4144 if (max_offset && tree_int_cst_lt (max_offset, offset))
4145 break;
4149 return 0;
4152 /* Record all of the empty subobjects of TYPE (either a type or a
4153 binfo). If IS_DATA_MEMBER is true, then a non-static data member
4154 is being placed at OFFSET; otherwise, it is a base class that is
4155 being placed at OFFSET. */
4157 static void
4158 record_subobject_offsets (tree type,
4159 tree offset,
4160 splay_tree offsets,
4161 bool is_data_member)
4163 tree max_offset;
4164 /* If recording subobjects for a non-static data member or a
4165 non-empty base class , we do not need to record offsets beyond
4166 the size of the biggest empty class. Additional data members
4167 will go at the end of the class. Additional base classes will go
4168 either at offset zero (if empty, in which case they cannot
4169 overlap with offsets past the size of the biggest empty class) or
4170 at the end of the class.
4172 However, if we are placing an empty base class, then we must record
4173 all offsets, as either the empty class is at offset zero (where
4174 other empty classes might later be placed) or at the end of the
4175 class (where other objects might then be placed, so other empty
4176 subobjects might later overlap). */
4177 if (is_data_member
4178 || !is_empty_class (BINFO_TYPE (type)))
4179 max_offset = sizeof_biggest_empty_class;
4180 else
4181 max_offset = NULL_TREE;
4182 walk_subobject_offsets (type, record_subobject_offset, offset,
4183 offsets, max_offset, is_data_member);
4186 /* Returns nonzero if any of the empty subobjects of TYPE (located at
4187 OFFSET) conflict with entries in OFFSETS. If VBASES_P is nonzero,
4188 virtual bases of TYPE are examined. */
4190 static int
4191 layout_conflict_p (tree type,
4192 tree offset,
4193 splay_tree offsets,
4194 int vbases_p)
4196 splay_tree_node max_node;
4198 /* Get the node in OFFSETS that indicates the maximum offset where
4199 an empty subobject is located. */
4200 max_node = splay_tree_max (offsets);
4201 /* If there aren't any empty subobjects, then there's no point in
4202 performing this check. */
4203 if (!max_node)
4204 return 0;
4206 return walk_subobject_offsets (type, check_subobject_offset, offset,
4207 offsets, (tree) (max_node->key),
4208 vbases_p);
4211 /* DECL is a FIELD_DECL corresponding either to a base subobject of a
4212 non-static data member of the type indicated by RLI. BINFO is the
4213 binfo corresponding to the base subobject, OFFSETS maps offsets to
4214 types already located at those offsets. This function determines
4215 the position of the DECL. */
4217 static void
4218 layout_nonempty_base_or_field (record_layout_info rli,
4219 tree decl,
4220 tree binfo,
4221 splay_tree offsets)
4223 tree offset = NULL_TREE;
4224 bool field_p;
4225 tree type;
4227 if (binfo)
4229 /* For the purposes of determining layout conflicts, we want to
4230 use the class type of BINFO; TREE_TYPE (DECL) will be the
4231 CLASSTYPE_AS_BASE version, which does not contain entries for
4232 zero-sized bases. */
4233 type = TREE_TYPE (binfo);
4234 field_p = false;
4236 else
4238 type = TREE_TYPE (decl);
4239 field_p = true;
4242 /* Try to place the field. It may take more than one try if we have
4243 a hard time placing the field without putting two objects of the
4244 same type at the same address. */
4245 while (1)
4247 struct record_layout_info_s old_rli = *rli;
4249 /* Place this field. */
4250 place_field (rli, decl);
4251 offset = byte_position (decl);
4253 /* We have to check to see whether or not there is already
4254 something of the same type at the offset we're about to use.
4255 For example, consider:
4257 struct S {};
4258 struct T : public S { int i; };
4259 struct U : public S, public T {};
4261 Here, we put S at offset zero in U. Then, we can't put T at
4262 offset zero -- its S component would be at the same address
4263 as the S we already allocated. So, we have to skip ahead.
4264 Since all data members, including those whose type is an
4265 empty class, have nonzero size, any overlap can happen only
4266 with a direct or indirect base-class -- it can't happen with
4267 a data member. */
4268 /* In a union, overlap is permitted; all members are placed at
4269 offset zero. */
4270 if (TREE_CODE (rli->t) == UNION_TYPE)
4271 break;
4272 if (layout_conflict_p (field_p ? type : binfo, offset,
4273 offsets, field_p))
4275 /* Strip off the size allocated to this field. That puts us
4276 at the first place we could have put the field with
4277 proper alignment. */
4278 *rli = old_rli;
4280 /* Bump up by the alignment required for the type. */
4281 rli->bitpos
4282 = size_binop (PLUS_EXPR, rli->bitpos,
4283 bitsize_int (binfo
4284 ? CLASSTYPE_ALIGN (type)
4285 : TYPE_ALIGN (type)));
4286 normalize_rli (rli);
4288 else if (TREE_CODE (type) == NULLPTR_TYPE
4289 && warn_abi && abi_version_crosses (9))
4291 /* Before ABI v9, we were giving nullptr_t alignment of 1; if
4292 the offset wasn't aligned like a pointer when we started to
4293 layout this field, that affects its position. */
4294 tree pos = rli_size_unit_so_far (&old_rli);
4295 if (int_cst_value (pos) % TYPE_ALIGN_UNIT (ptr_type_node) != 0)
4297 if (abi_version_at_least (9))
4298 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wabi,
4299 "alignment of %qD increased in -fabi-version=9 "
4300 "(GCC 5.2)", decl);
4301 else
4302 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wabi, "alignment "
4303 "of %qD will increase in -fabi-version=9", decl);
4305 break;
4307 else
4308 /* There was no conflict. We're done laying out this field. */
4309 break;
4312 /* Now that we know where it will be placed, update its
4313 BINFO_OFFSET. */
4314 if (binfo && CLASS_TYPE_P (BINFO_TYPE (binfo)))
4315 /* Indirect virtual bases may have a nonzero BINFO_OFFSET at
4316 this point because their BINFO_OFFSET is copied from another
4317 hierarchy. Therefore, we may not need to add the entire
4318 OFFSET. */
4319 propagate_binfo_offsets (binfo,
4320 size_diffop_loc (input_location,
4321 fold_convert (ssizetype, offset),
4322 fold_convert (ssizetype,
4323 BINFO_OFFSET (binfo))));
4326 /* Returns true if TYPE is empty and OFFSET is nonzero. */
4328 static int
4329 empty_base_at_nonzero_offset_p (tree type,
4330 tree offset,
4331 splay_tree /*offsets*/)
4333 return is_empty_class (type) && !integer_zerop (offset);
4336 /* Layout the empty base BINFO. EOC indicates the byte currently just
4337 past the end of the class, and should be correctly aligned for a
4338 class of the type indicated by BINFO; OFFSETS gives the offsets of
4339 the empty bases allocated so far. T is the most derived
4340 type. Return nonzero iff we added it at the end. */
4342 static bool
4343 layout_empty_base (record_layout_info rli, tree binfo,
4344 tree eoc, splay_tree offsets)
4346 tree alignment;
4347 tree basetype = BINFO_TYPE (binfo);
4348 bool atend = false;
4350 /* This routine should only be used for empty classes. */
4351 gcc_assert (is_empty_class (basetype));
4352 alignment = ssize_int (CLASSTYPE_ALIGN_UNIT (basetype));
4354 if (!integer_zerop (BINFO_OFFSET (binfo)))
4355 propagate_binfo_offsets
4356 (binfo, size_diffop_loc (input_location,
4357 size_zero_node, BINFO_OFFSET (binfo)));
4359 /* This is an empty base class. We first try to put it at offset
4360 zero. */
4361 if (layout_conflict_p (binfo,
4362 BINFO_OFFSET (binfo),
4363 offsets,
4364 /*vbases_p=*/0))
4366 /* That didn't work. Now, we move forward from the next
4367 available spot in the class. */
4368 atend = true;
4369 propagate_binfo_offsets (binfo, fold_convert (ssizetype, eoc));
4370 while (1)
4372 if (!layout_conflict_p (binfo,
4373 BINFO_OFFSET (binfo),
4374 offsets,
4375 /*vbases_p=*/0))
4376 /* We finally found a spot where there's no overlap. */
4377 break;
4379 /* There's overlap here, too. Bump along to the next spot. */
4380 propagate_binfo_offsets (binfo, alignment);
4384 if (CLASSTYPE_USER_ALIGN (basetype))
4386 rli->record_align = MAX (rli->record_align, CLASSTYPE_ALIGN (basetype));
4387 if (warn_packed)
4388 rli->unpacked_align = MAX (rli->unpacked_align, CLASSTYPE_ALIGN (basetype));
4389 TYPE_USER_ALIGN (rli->t) = 1;
4392 return atend;
4395 /* Layout the base given by BINFO in the class indicated by RLI.
4396 *BASE_ALIGN is a running maximum of the alignments of
4397 any base class. OFFSETS gives the location of empty base
4398 subobjects. T is the most derived type. Return nonzero if the new
4399 object cannot be nearly-empty. A new FIELD_DECL is inserted at
4400 *NEXT_FIELD, unless BINFO is for an empty base class.
4402 Returns the location at which the next field should be inserted. */
4404 static tree *
4405 build_base_field (record_layout_info rli, tree binfo,
4406 splay_tree offsets, tree *next_field)
4408 tree t = rli->t;
4409 tree basetype = BINFO_TYPE (binfo);
4411 if (!COMPLETE_TYPE_P (basetype))
4412 /* This error is now reported in xref_tag, thus giving better
4413 location information. */
4414 return next_field;
4416 /* Place the base class. */
4417 if (!is_empty_class (basetype))
4419 tree decl;
4421 /* The containing class is non-empty because it has a non-empty
4422 base class. */
4423 CLASSTYPE_EMPTY_P (t) = 0;
4425 /* Create the FIELD_DECL. */
4426 decl = build_decl (input_location,
4427 FIELD_DECL, NULL_TREE, CLASSTYPE_AS_BASE (basetype));
4428 DECL_ARTIFICIAL (decl) = 1;
4429 DECL_IGNORED_P (decl) = 1;
4430 DECL_FIELD_CONTEXT (decl) = t;
4431 if (CLASSTYPE_AS_BASE (basetype))
4433 DECL_SIZE (decl) = CLASSTYPE_SIZE (basetype);
4434 DECL_SIZE_UNIT (decl) = CLASSTYPE_SIZE_UNIT (basetype);
4435 DECL_ALIGN (decl) = CLASSTYPE_ALIGN (basetype);
4436 DECL_USER_ALIGN (decl) = CLASSTYPE_USER_ALIGN (basetype);
4437 DECL_MODE (decl) = TYPE_MODE (basetype);
4438 DECL_FIELD_IS_BASE (decl) = 1;
4440 /* Try to place the field. It may take more than one try if we
4441 have a hard time placing the field without putting two
4442 objects of the same type at the same address. */
4443 layout_nonempty_base_or_field (rli, decl, binfo, offsets);
4444 /* Add the new FIELD_DECL to the list of fields for T. */
4445 DECL_CHAIN (decl) = *next_field;
4446 *next_field = decl;
4447 next_field = &DECL_CHAIN (decl);
4450 else
4452 tree eoc;
4453 bool atend;
4455 /* On some platforms (ARM), even empty classes will not be
4456 byte-aligned. */
4457 eoc = round_up_loc (input_location,
4458 rli_size_unit_so_far (rli),
4459 CLASSTYPE_ALIGN_UNIT (basetype));
4460 atend = layout_empty_base (rli, binfo, eoc, offsets);
4461 /* A nearly-empty class "has no proper base class that is empty,
4462 not morally virtual, and at an offset other than zero." */
4463 if (!BINFO_VIRTUAL_P (binfo) && CLASSTYPE_NEARLY_EMPTY_P (t))
4465 if (atend)
4466 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
4467 /* The check above (used in G++ 3.2) is insufficient because
4468 an empty class placed at offset zero might itself have an
4469 empty base at a nonzero offset. */
4470 else if (walk_subobject_offsets (basetype,
4471 empty_base_at_nonzero_offset_p,
4472 size_zero_node,
4473 /*offsets=*/NULL,
4474 /*max_offset=*/NULL_TREE,
4475 /*vbases_p=*/true))
4476 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
4479 /* We do not create a FIELD_DECL for empty base classes because
4480 it might overlap some other field. We want to be able to
4481 create CONSTRUCTORs for the class by iterating over the
4482 FIELD_DECLs, and the back end does not handle overlapping
4483 FIELD_DECLs. */
4485 /* An empty virtual base causes a class to be non-empty
4486 -- but in that case we do not need to clear CLASSTYPE_EMPTY_P
4487 here because that was already done when the virtual table
4488 pointer was created. */
4491 /* Record the offsets of BINFO and its base subobjects. */
4492 record_subobject_offsets (binfo,
4493 BINFO_OFFSET (binfo),
4494 offsets,
4495 /*is_data_member=*/false);
4497 return next_field;
4500 /* Layout all of the non-virtual base classes. Record empty
4501 subobjects in OFFSETS. T is the most derived type. Return nonzero
4502 if the type cannot be nearly empty. The fields created
4503 corresponding to the base classes will be inserted at
4504 *NEXT_FIELD. */
4506 static void
4507 build_base_fields (record_layout_info rli,
4508 splay_tree offsets, tree *next_field)
4510 /* Chain to hold all the new FIELD_DECLs which stand in for base class
4511 subobjects. */
4512 tree t = rli->t;
4513 int n_baseclasses = BINFO_N_BASE_BINFOS (TYPE_BINFO (t));
4514 int i;
4516 /* The primary base class is always allocated first. */
4517 if (CLASSTYPE_HAS_PRIMARY_BASE_P (t))
4518 next_field = build_base_field (rli, CLASSTYPE_PRIMARY_BINFO (t),
4519 offsets, next_field);
4521 /* Now allocate the rest of the bases. */
4522 for (i = 0; i < n_baseclasses; ++i)
4524 tree base_binfo;
4526 base_binfo = BINFO_BASE_BINFO (TYPE_BINFO (t), i);
4528 /* The primary base was already allocated above, so we don't
4529 need to allocate it again here. */
4530 if (base_binfo == CLASSTYPE_PRIMARY_BINFO (t))
4531 continue;
4533 /* Virtual bases are added at the end (a primary virtual base
4534 will have already been added). */
4535 if (BINFO_VIRTUAL_P (base_binfo))
4536 continue;
4538 next_field = build_base_field (rli, base_binfo,
4539 offsets, next_field);
4543 /* Go through the TYPE_METHODS of T issuing any appropriate
4544 diagnostics, figuring out which methods override which other
4545 methods, and so forth. */
4547 static void
4548 check_methods (tree t)
4550 tree x;
4552 for (x = TYPE_METHODS (t); x; x = DECL_CHAIN (x))
4554 check_for_override (x, t);
4555 if (DECL_PURE_VIRTUAL_P (x) && (TREE_CODE (x) != FUNCTION_DECL || ! DECL_VINDEX (x)))
4556 error ("initializer specified for non-virtual method %q+D", x);
4557 /* The name of the field is the original field name
4558 Save this in auxiliary field for later overloading. */
4559 if (TREE_CODE (x) == FUNCTION_DECL && DECL_VINDEX (x))
4561 TYPE_POLYMORPHIC_P (t) = 1;
4562 if (DECL_PURE_VIRTUAL_P (x))
4563 vec_safe_push (CLASSTYPE_PURE_VIRTUALS (t), x);
4565 /* All user-provided destructors are non-trivial.
4566 Constructors and assignment ops are handled in
4567 grok_special_member_properties. */
4568 if (DECL_DESTRUCTOR_P (x) && user_provided_p (x))
4569 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t) = 1;
4570 if (!DECL_VIRTUAL_P (x)
4571 && lookup_attribute ("transaction_safe_dynamic", DECL_ATTRIBUTES (x)))
4572 error_at (DECL_SOURCE_LOCATION (x),
4573 "%<transaction_safe_dynamic%> may only be specified for "
4574 "a virtual function");
4578 /* FN is a constructor or destructor. Clone the declaration to create
4579 a specialized in-charge or not-in-charge version, as indicated by
4580 NAME. */
4582 static tree
4583 build_clone (tree fn, tree name)
4585 tree parms;
4586 tree clone;
4588 /* Copy the function. */
4589 clone = copy_decl (fn);
4590 /* Reset the function name. */
4591 DECL_NAME (clone) = name;
4592 /* Remember where this function came from. */
4593 DECL_ABSTRACT_ORIGIN (clone) = fn;
4594 /* Make it easy to find the CLONE given the FN. */
4595 DECL_CHAIN (clone) = DECL_CHAIN (fn);
4596 DECL_CHAIN (fn) = clone;
4598 /* If this is a template, do the rest on the DECL_TEMPLATE_RESULT. */
4599 if (TREE_CODE (clone) == TEMPLATE_DECL)
4601 tree result = build_clone (DECL_TEMPLATE_RESULT (clone), name);
4602 DECL_TEMPLATE_RESULT (clone) = result;
4603 DECL_TEMPLATE_INFO (result) = copy_node (DECL_TEMPLATE_INFO (result));
4604 DECL_TI_TEMPLATE (result) = clone;
4605 TREE_TYPE (clone) = TREE_TYPE (result);
4606 return clone;
4608 else
4610 // Clone constraints.
4611 if (flag_concepts)
4612 if (tree ci = get_constraints (fn))
4613 set_constraints (clone, copy_node (ci));
4617 SET_DECL_ASSEMBLER_NAME (clone, NULL_TREE);
4618 DECL_CLONED_FUNCTION (clone) = fn;
4619 /* There's no pending inline data for this function. */
4620 DECL_PENDING_INLINE_INFO (clone) = NULL;
4621 DECL_PENDING_INLINE_P (clone) = 0;
4623 /* The base-class destructor is not virtual. */
4624 if (name == base_dtor_identifier)
4626 DECL_VIRTUAL_P (clone) = 0;
4627 if (TREE_CODE (clone) != TEMPLATE_DECL)
4628 DECL_VINDEX (clone) = NULL_TREE;
4631 /* If there was an in-charge parameter, drop it from the function
4632 type. */
4633 if (DECL_HAS_IN_CHARGE_PARM_P (clone))
4635 tree basetype;
4636 tree parmtypes;
4637 tree exceptions;
4639 exceptions = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (clone));
4640 basetype = TYPE_METHOD_BASETYPE (TREE_TYPE (clone));
4641 parmtypes = TYPE_ARG_TYPES (TREE_TYPE (clone));
4642 /* Skip the `this' parameter. */
4643 parmtypes = TREE_CHAIN (parmtypes);
4644 /* Skip the in-charge parameter. */
4645 parmtypes = TREE_CHAIN (parmtypes);
4646 /* And the VTT parm, in a complete [cd]tor. */
4647 if (DECL_HAS_VTT_PARM_P (fn)
4648 && ! DECL_NEEDS_VTT_PARM_P (clone))
4649 parmtypes = TREE_CHAIN (parmtypes);
4650 /* If this is subobject constructor or destructor, add the vtt
4651 parameter. */
4652 TREE_TYPE (clone)
4653 = build_method_type_directly (basetype,
4654 TREE_TYPE (TREE_TYPE (clone)),
4655 parmtypes);
4656 if (exceptions)
4657 TREE_TYPE (clone) = build_exception_variant (TREE_TYPE (clone),
4658 exceptions);
4659 TREE_TYPE (clone)
4660 = cp_build_type_attribute_variant (TREE_TYPE (clone),
4661 TYPE_ATTRIBUTES (TREE_TYPE (fn)));
4664 /* Copy the function parameters. */
4665 DECL_ARGUMENTS (clone) = copy_list (DECL_ARGUMENTS (clone));
4666 /* Remove the in-charge parameter. */
4667 if (DECL_HAS_IN_CHARGE_PARM_P (clone))
4669 DECL_CHAIN (DECL_ARGUMENTS (clone))
4670 = DECL_CHAIN (DECL_CHAIN (DECL_ARGUMENTS (clone)));
4671 DECL_HAS_IN_CHARGE_PARM_P (clone) = 0;
4673 /* And the VTT parm, in a complete [cd]tor. */
4674 if (DECL_HAS_VTT_PARM_P (fn))
4676 if (DECL_NEEDS_VTT_PARM_P (clone))
4677 DECL_HAS_VTT_PARM_P (clone) = 1;
4678 else
4680 DECL_CHAIN (DECL_ARGUMENTS (clone))
4681 = DECL_CHAIN (DECL_CHAIN (DECL_ARGUMENTS (clone)));
4682 DECL_HAS_VTT_PARM_P (clone) = 0;
4686 for (parms = DECL_ARGUMENTS (clone); parms; parms = DECL_CHAIN (parms))
4688 DECL_CONTEXT (parms) = clone;
4689 cxx_dup_lang_specific_decl (parms);
4692 /* Create the RTL for this function. */
4693 SET_DECL_RTL (clone, NULL);
4694 rest_of_decl_compilation (clone, /*top_level=*/1, at_eof);
4696 return clone;
4699 /* Implementation of DECL_CLONED_FUNCTION and DECL_CLONED_FUNCTION_P, do
4700 not invoke this function directly.
4702 For a non-thunk function, returns the address of the slot for storing
4703 the function it is a clone of. Otherwise returns NULL_TREE.
4705 If JUST_TESTING, looks through TEMPLATE_DECL and returns NULL if
4706 cloned_function is unset. This is to support the separate
4707 DECL_CLONED_FUNCTION and DECL_CLONED_FUNCTION_P modes; using the latter
4708 on a template makes sense, but not the former. */
4710 tree *
4711 decl_cloned_function_p (const_tree decl, bool just_testing)
4713 tree *ptr;
4714 if (just_testing)
4715 decl = STRIP_TEMPLATE (decl);
4717 if (TREE_CODE (decl) != FUNCTION_DECL
4718 || !DECL_LANG_SPECIFIC (decl)
4719 || DECL_LANG_SPECIFIC (decl)->u.fn.thunk_p)
4721 #if defined ENABLE_TREE_CHECKING && (GCC_VERSION >= 2007)
4722 if (!just_testing)
4723 lang_check_failed (__FILE__, __LINE__, __FUNCTION__);
4724 else
4725 #endif
4726 return NULL;
4729 ptr = &DECL_LANG_SPECIFIC (decl)->u.fn.u5.cloned_function;
4730 if (just_testing && *ptr == NULL_TREE)
4731 return NULL;
4732 else
4733 return ptr;
4736 /* Produce declarations for all appropriate clones of FN. If
4737 UPDATE_METHOD_VEC_P is nonzero, the clones are added to the
4738 CLASTYPE_METHOD_VEC as well. */
4740 void
4741 clone_function_decl (tree fn, int update_method_vec_p)
4743 tree clone;
4745 /* Avoid inappropriate cloning. */
4746 if (DECL_CHAIN (fn)
4747 && DECL_CLONED_FUNCTION_P (DECL_CHAIN (fn)))
4748 return;
4750 if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (fn))
4752 /* For each constructor, we need two variants: an in-charge version
4753 and a not-in-charge version. */
4754 clone = build_clone (fn, complete_ctor_identifier);
4755 if (update_method_vec_p)
4756 add_method (DECL_CONTEXT (clone), clone, NULL_TREE);
4757 clone = build_clone (fn, base_ctor_identifier);
4758 if (update_method_vec_p)
4759 add_method (DECL_CONTEXT (clone), clone, NULL_TREE);
4761 else
4763 gcc_assert (DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (fn));
4765 /* For each destructor, we need three variants: an in-charge
4766 version, a not-in-charge version, and an in-charge deleting
4767 version. We clone the deleting version first because that
4768 means it will go second on the TYPE_METHODS list -- and that
4769 corresponds to the correct layout order in the virtual
4770 function table.
4772 For a non-virtual destructor, we do not build a deleting
4773 destructor. */
4774 if (DECL_VIRTUAL_P (fn))
4776 clone = build_clone (fn, deleting_dtor_identifier);
4777 if (update_method_vec_p)
4778 add_method (DECL_CONTEXT (clone), clone, NULL_TREE);
4780 clone = build_clone (fn, complete_dtor_identifier);
4781 if (update_method_vec_p)
4782 add_method (DECL_CONTEXT (clone), clone, NULL_TREE);
4783 clone = build_clone (fn, base_dtor_identifier);
4784 if (update_method_vec_p)
4785 add_method (DECL_CONTEXT (clone), clone, NULL_TREE);
4788 /* Note that this is an abstract function that is never emitted. */
4789 DECL_ABSTRACT_P (fn) = true;
4792 /* DECL is an in charge constructor, which is being defined. This will
4793 have had an in class declaration, from whence clones were
4794 declared. An out-of-class definition can specify additional default
4795 arguments. As it is the clones that are involved in overload
4796 resolution, we must propagate the information from the DECL to its
4797 clones. */
4799 void
4800 adjust_clone_args (tree decl)
4802 tree clone;
4804 for (clone = DECL_CHAIN (decl); clone && DECL_CLONED_FUNCTION_P (clone);
4805 clone = DECL_CHAIN (clone))
4807 tree orig_clone_parms = TYPE_ARG_TYPES (TREE_TYPE (clone));
4808 tree orig_decl_parms = TYPE_ARG_TYPES (TREE_TYPE (decl));
4809 tree decl_parms, clone_parms;
4811 clone_parms = orig_clone_parms;
4813 /* Skip the 'this' parameter. */
4814 orig_clone_parms = TREE_CHAIN (orig_clone_parms);
4815 orig_decl_parms = TREE_CHAIN (orig_decl_parms);
4817 if (DECL_HAS_IN_CHARGE_PARM_P (decl))
4818 orig_decl_parms = TREE_CHAIN (orig_decl_parms);
4819 if (DECL_HAS_VTT_PARM_P (decl))
4820 orig_decl_parms = TREE_CHAIN (orig_decl_parms);
4822 clone_parms = orig_clone_parms;
4823 if (DECL_HAS_VTT_PARM_P (clone))
4824 clone_parms = TREE_CHAIN (clone_parms);
4826 for (decl_parms = orig_decl_parms; decl_parms;
4827 decl_parms = TREE_CHAIN (decl_parms),
4828 clone_parms = TREE_CHAIN (clone_parms))
4830 gcc_assert (same_type_p (TREE_TYPE (decl_parms),
4831 TREE_TYPE (clone_parms)));
4833 if (TREE_PURPOSE (decl_parms) && !TREE_PURPOSE (clone_parms))
4835 /* A default parameter has been added. Adjust the
4836 clone's parameters. */
4837 tree exceptions = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (clone));
4838 tree attrs = TYPE_ATTRIBUTES (TREE_TYPE (clone));
4839 tree basetype = TYPE_METHOD_BASETYPE (TREE_TYPE (clone));
4840 tree type;
4842 clone_parms = orig_decl_parms;
4844 if (DECL_HAS_VTT_PARM_P (clone))
4846 clone_parms = tree_cons (TREE_PURPOSE (orig_clone_parms),
4847 TREE_VALUE (orig_clone_parms),
4848 clone_parms);
4849 TREE_TYPE (clone_parms) = TREE_TYPE (orig_clone_parms);
4851 type = build_method_type_directly (basetype,
4852 TREE_TYPE (TREE_TYPE (clone)),
4853 clone_parms);
4854 if (exceptions)
4855 type = build_exception_variant (type, exceptions);
4856 if (attrs)
4857 type = cp_build_type_attribute_variant (type, attrs);
4858 TREE_TYPE (clone) = type;
4860 clone_parms = NULL_TREE;
4861 break;
4864 gcc_assert (!clone_parms);
4868 /* For each of the constructors and destructors in T, create an
4869 in-charge and not-in-charge variant. */
4871 static void
4872 clone_constructors_and_destructors (tree t)
4874 tree fns;
4876 /* If for some reason we don't have a CLASSTYPE_METHOD_VEC, we bail
4877 out now. */
4878 if (!CLASSTYPE_METHOD_VEC (t))
4879 return;
4881 for (fns = CLASSTYPE_CONSTRUCTORS (t); fns; fns = OVL_NEXT (fns))
4882 clone_function_decl (OVL_CURRENT (fns), /*update_method_vec_p=*/1);
4883 for (fns = CLASSTYPE_DESTRUCTORS (t); fns; fns = OVL_NEXT (fns))
4884 clone_function_decl (OVL_CURRENT (fns), /*update_method_vec_p=*/1);
4887 /* Deduce noexcept for a destructor DTOR. */
4889 void
4890 deduce_noexcept_on_destructor (tree dtor)
4892 if (!TYPE_RAISES_EXCEPTIONS (TREE_TYPE (dtor)))
4894 tree eh_spec = unevaluated_noexcept_spec ();
4895 TREE_TYPE (dtor) = build_exception_variant (TREE_TYPE (dtor), eh_spec);
4899 /* For each destructor in T, deduce noexcept:
4901 12.4/3: A declaration of a destructor that does not have an
4902 exception-specification is implicitly considered to have the
4903 same exception-specification as an implicit declaration (15.4). */
4905 static void
4906 deduce_noexcept_on_destructors (tree t)
4908 /* If for some reason we don't have a CLASSTYPE_METHOD_VEC, we bail
4909 out now. */
4910 if (!CLASSTYPE_METHOD_VEC (t))
4911 return;
4913 for (tree fns = CLASSTYPE_DESTRUCTORS (t); fns; fns = OVL_NEXT (fns))
4914 deduce_noexcept_on_destructor (OVL_CURRENT (fns));
4917 /* Subroutine of set_one_vmethod_tm_attributes. Search base classes
4918 of TYPE for virtual functions which FNDECL overrides. Return a
4919 mask of the tm attributes found therein. */
4921 static int
4922 look_for_tm_attr_overrides (tree type, tree fndecl)
4924 tree binfo = TYPE_BINFO (type);
4925 tree base_binfo;
4926 int ix, found = 0;
4928 for (ix = 0; BINFO_BASE_ITERATE (binfo, ix, base_binfo); ++ix)
4930 tree o, basetype = BINFO_TYPE (base_binfo);
4932 if (!TYPE_POLYMORPHIC_P (basetype))
4933 continue;
4935 o = look_for_overrides_here (basetype, fndecl);
4936 if (o)
4938 if (lookup_attribute ("transaction_safe_dynamic",
4939 DECL_ATTRIBUTES (o)))
4940 /* transaction_safe_dynamic is not inherited. */;
4941 else
4942 found |= tm_attr_to_mask (find_tm_attribute
4943 (TYPE_ATTRIBUTES (TREE_TYPE (o))));
4945 else
4946 found |= look_for_tm_attr_overrides (basetype, fndecl);
4949 return found;
4952 /* Subroutine of set_method_tm_attributes. Handle the checks and
4953 inheritance for one virtual method FNDECL. */
4955 static void
4956 set_one_vmethod_tm_attributes (tree type, tree fndecl)
4958 tree tm_attr;
4959 int found, have;
4961 found = look_for_tm_attr_overrides (type, fndecl);
4963 /* If FNDECL doesn't actually override anything (i.e. T is the
4964 class that first declares FNDECL virtual), then we're done. */
4965 if (found == 0)
4966 return;
4968 tm_attr = find_tm_attribute (TYPE_ATTRIBUTES (TREE_TYPE (fndecl)));
4969 have = tm_attr_to_mask (tm_attr);
4971 /* Intel STM Language Extension 3.0, Section 4.2 table 4:
4972 tm_pure must match exactly, otherwise no weakening of
4973 tm_safe > tm_callable > nothing. */
4974 /* ??? The tm_pure attribute didn't make the transition to the
4975 multivendor language spec. */
4976 if (have == TM_ATTR_PURE)
4978 if (found != TM_ATTR_PURE)
4980 found &= -found;
4981 goto err_override;
4984 /* If the overridden function is tm_pure, then FNDECL must be. */
4985 else if (found == TM_ATTR_PURE && tm_attr)
4986 goto err_override;
4987 /* Look for base class combinations that cannot be satisfied. */
4988 else if (found != TM_ATTR_PURE && (found & TM_ATTR_PURE))
4990 found &= ~TM_ATTR_PURE;
4991 found &= -found;
4992 error_at (DECL_SOURCE_LOCATION (fndecl),
4993 "method overrides both %<transaction_pure%> and %qE methods",
4994 tm_mask_to_attr (found));
4996 /* If FNDECL did not declare an attribute, then inherit the most
4997 restrictive one. */
4998 else if (tm_attr == NULL)
5000 apply_tm_attr (fndecl, tm_mask_to_attr (found & -found));
5002 /* Otherwise validate that we're not weaker than a function
5003 that is being overridden. */
5004 else
5006 found &= -found;
5007 if (found <= TM_ATTR_CALLABLE && have > found)
5008 goto err_override;
5010 return;
5012 err_override:
5013 error_at (DECL_SOURCE_LOCATION (fndecl),
5014 "method declared %qE overriding %qE method",
5015 tm_attr, tm_mask_to_attr (found));
5018 /* For each of the methods in T, propagate a class-level tm attribute. */
5020 static void
5021 set_method_tm_attributes (tree t)
5023 tree class_tm_attr, fndecl;
5025 /* Don't bother collecting tm attributes if transactional memory
5026 support is not enabled. */
5027 if (!flag_tm)
5028 return;
5030 /* Process virtual methods first, as they inherit directly from the
5031 base virtual function and also require validation of new attributes. */
5032 if (TYPE_CONTAINS_VPTR_P (t))
5034 tree vchain;
5035 for (vchain = BINFO_VIRTUALS (TYPE_BINFO (t)); vchain;
5036 vchain = TREE_CHAIN (vchain))
5038 fndecl = BV_FN (vchain);
5039 if (DECL_THUNK_P (fndecl))
5040 fndecl = THUNK_TARGET (fndecl);
5041 set_one_vmethod_tm_attributes (t, fndecl);
5045 /* If the class doesn't have an attribute, nothing more to do. */
5046 class_tm_attr = find_tm_attribute (TYPE_ATTRIBUTES (t));
5047 if (class_tm_attr == NULL)
5048 return;
5050 /* Any method that does not yet have a tm attribute inherits
5051 the one from the class. */
5052 for (fndecl = TYPE_METHODS (t); fndecl; fndecl = TREE_CHAIN (fndecl))
5054 if (!find_tm_attribute (TYPE_ATTRIBUTES (TREE_TYPE (fndecl))))
5055 apply_tm_attr (fndecl, class_tm_attr);
5059 /* Returns true iff class T has a user-defined constructor other than
5060 the default constructor. */
5062 bool
5063 type_has_user_nondefault_constructor (tree t)
5065 tree fns;
5067 if (!TYPE_HAS_USER_CONSTRUCTOR (t))
5068 return false;
5070 for (fns = CLASSTYPE_CONSTRUCTORS (t); fns; fns = OVL_NEXT (fns))
5072 tree fn = OVL_CURRENT (fns);
5073 if (!DECL_ARTIFICIAL (fn)
5074 && (TREE_CODE (fn) == TEMPLATE_DECL
5075 || (skip_artificial_parms_for (fn, DECL_ARGUMENTS (fn))
5076 != NULL_TREE)))
5077 return true;
5080 return false;
5083 /* Returns the defaulted constructor if T has one. Otherwise, returns
5084 NULL_TREE. */
5086 tree
5087 in_class_defaulted_default_constructor (tree t)
5089 tree fns, args;
5091 if (!TYPE_HAS_USER_CONSTRUCTOR (t))
5092 return NULL_TREE;
5094 for (fns = CLASSTYPE_CONSTRUCTORS (t); fns; fns = OVL_NEXT (fns))
5096 tree fn = OVL_CURRENT (fns);
5098 if (DECL_DEFAULTED_IN_CLASS_P (fn))
5100 args = FUNCTION_FIRST_USER_PARMTYPE (fn);
5101 while (args && TREE_PURPOSE (args))
5102 args = TREE_CHAIN (args);
5103 if (!args || args == void_list_node)
5104 return fn;
5108 return NULL_TREE;
5111 /* Returns true iff FN is a user-provided function, i.e. user-declared
5112 and not defaulted at its first declaration; or explicit, private,
5113 protected, or non-const. */
5115 bool
5116 user_provided_p (tree fn)
5118 if (TREE_CODE (fn) == TEMPLATE_DECL)
5119 return true;
5120 else
5121 return (!DECL_ARTIFICIAL (fn)
5122 && !(DECL_INITIALIZED_IN_CLASS_P (fn)
5123 && (DECL_DEFAULTED_FN (fn) || DECL_DELETED_FN (fn))));
5126 /* Returns true iff class T has a user-provided constructor. */
5128 bool
5129 type_has_user_provided_constructor (tree t)
5131 tree fns;
5133 if (!CLASS_TYPE_P (t))
5134 return false;
5136 if (!TYPE_HAS_USER_CONSTRUCTOR (t))
5137 return false;
5139 /* This can happen in error cases; avoid crashing. */
5140 if (!CLASSTYPE_METHOD_VEC (t))
5141 return false;
5143 for (fns = CLASSTYPE_CONSTRUCTORS (t); fns; fns = OVL_NEXT (fns))
5144 if (user_provided_p (OVL_CURRENT (fns)))
5145 return true;
5147 return false;
5150 /* Returns true iff class T has a user-provided or explicit constructor. */
5152 bool
5153 type_has_user_provided_or_explicit_constructor (tree t)
5155 tree fns;
5157 if (!CLASS_TYPE_P (t))
5158 return false;
5160 if (!TYPE_HAS_USER_CONSTRUCTOR (t))
5161 return false;
5163 /* This can happen in error cases; avoid crashing. */
5164 if (!CLASSTYPE_METHOD_VEC (t))
5165 return false;
5167 for (fns = CLASSTYPE_CONSTRUCTORS (t); fns; fns = OVL_NEXT (fns))
5169 tree fn = OVL_CURRENT (fns);
5170 if (user_provided_p (fn) || DECL_NONCONVERTING_P (fn))
5171 return true;
5174 return false;
5177 /* Returns true iff class T has a non-user-provided (i.e. implicitly
5178 declared or explicitly defaulted in the class body) default
5179 constructor. */
5181 bool
5182 type_has_non_user_provided_default_constructor (tree t)
5184 tree fns;
5186 if (!TYPE_HAS_DEFAULT_CONSTRUCTOR (t))
5187 return false;
5188 if (CLASSTYPE_LAZY_DEFAULT_CTOR (t))
5189 return true;
5191 for (fns = CLASSTYPE_CONSTRUCTORS (t); fns; fns = OVL_NEXT (fns))
5193 tree fn = OVL_CURRENT (fns);
5194 if (TREE_CODE (fn) == FUNCTION_DECL
5195 && !user_provided_p (fn)
5196 && sufficient_parms_p (FUNCTION_FIRST_USER_PARMTYPE (fn)))
5197 return true;
5200 return false;
5203 /* TYPE is being used as a virtual base, and has a non-trivial move
5204 assignment. Return true if this is due to there being a user-provided
5205 move assignment in TYPE or one of its subobjects; if there isn't, then
5206 multiple move assignment can't cause any harm. */
5208 bool
5209 vbase_has_user_provided_move_assign (tree type)
5211 /* Does the type itself have a user-provided move assignment operator? */
5212 for (tree fns
5213 = lookup_fnfields_slot_nolazy (type, ansi_assopname (NOP_EXPR));
5214 fns; fns = OVL_NEXT (fns))
5216 tree fn = OVL_CURRENT (fns);
5217 if (move_fn_p (fn) && user_provided_p (fn))
5218 return true;
5221 /* Do any of its bases? */
5222 tree binfo = TYPE_BINFO (type);
5223 tree base_binfo;
5224 for (int i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
5225 if (vbase_has_user_provided_move_assign (BINFO_TYPE (base_binfo)))
5226 return true;
5228 /* Or non-static data members? */
5229 for (tree field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
5231 if (TREE_CODE (field) == FIELD_DECL
5232 && CLASS_TYPE_P (TREE_TYPE (field))
5233 && vbase_has_user_provided_move_assign (TREE_TYPE (field)))
5234 return true;
5237 /* Seems not. */
5238 return false;
5241 /* If default-initialization leaves part of TYPE uninitialized, returns
5242 a DECL for the field or TYPE itself (DR 253). */
5244 tree
5245 default_init_uninitialized_part (tree type)
5247 tree t, r, binfo;
5248 int i;
5250 type = strip_array_types (type);
5251 if (!CLASS_TYPE_P (type))
5252 return type;
5253 if (!type_has_non_user_provided_default_constructor (type))
5254 return NULL_TREE;
5255 for (binfo = TYPE_BINFO (type), i = 0;
5256 BINFO_BASE_ITERATE (binfo, i, t); ++i)
5258 r = default_init_uninitialized_part (BINFO_TYPE (t));
5259 if (r)
5260 return r;
5262 for (t = TYPE_FIELDS (type); t; t = DECL_CHAIN (t))
5263 if (TREE_CODE (t) == FIELD_DECL
5264 && !DECL_ARTIFICIAL (t)
5265 && !DECL_INITIAL (t))
5267 r = default_init_uninitialized_part (TREE_TYPE (t));
5268 if (r)
5269 return DECL_P (r) ? r : t;
5272 return NULL_TREE;
5275 /* Returns true iff for class T, a trivial synthesized default constructor
5276 would be constexpr. */
5278 bool
5279 trivial_default_constructor_is_constexpr (tree t)
5281 /* A defaulted trivial default constructor is constexpr
5282 if there is nothing to initialize. */
5283 gcc_assert (!TYPE_HAS_COMPLEX_DFLT (t));
5284 return is_really_empty_class (t);
5287 /* Returns true iff class T has a constexpr default constructor. */
5289 bool
5290 type_has_constexpr_default_constructor (tree t)
5292 tree fns;
5294 if (!CLASS_TYPE_P (t))
5296 /* The caller should have stripped an enclosing array. */
5297 gcc_assert (TREE_CODE (t) != ARRAY_TYPE);
5298 return false;
5300 if (CLASSTYPE_LAZY_DEFAULT_CTOR (t))
5302 if (!TYPE_HAS_COMPLEX_DFLT (t))
5303 return trivial_default_constructor_is_constexpr (t);
5304 /* Non-trivial, we need to check subobject constructors. */
5305 lazily_declare_fn (sfk_constructor, t);
5307 fns = locate_ctor (t);
5308 return (fns && DECL_DECLARED_CONSTEXPR_P (fns));
5311 /* Returns true iff class TYPE has a virtual destructor. */
5313 bool
5314 type_has_virtual_destructor (tree type)
5316 tree dtor;
5318 if (!CLASS_TYPE_P (type))
5319 return false;
5321 gcc_assert (COMPLETE_TYPE_P (type));
5322 dtor = CLASSTYPE_DESTRUCTORS (type);
5323 return (dtor && DECL_VIRTUAL_P (dtor));
5326 /* Returns true iff class T has a move constructor. */
5328 bool
5329 type_has_move_constructor (tree t)
5331 tree fns;
5333 if (CLASSTYPE_LAZY_MOVE_CTOR (t))
5335 gcc_assert (COMPLETE_TYPE_P (t));
5336 lazily_declare_fn (sfk_move_constructor, t);
5339 if (!CLASSTYPE_METHOD_VEC (t))
5340 return false;
5342 for (fns = CLASSTYPE_CONSTRUCTORS (t); fns; fns = OVL_NEXT (fns))
5343 if (move_fn_p (OVL_CURRENT (fns)))
5344 return true;
5346 return false;
5349 /* Returns true iff class T has a move assignment operator. */
5351 bool
5352 type_has_move_assign (tree t)
5354 tree fns;
5356 if (CLASSTYPE_LAZY_MOVE_ASSIGN (t))
5358 gcc_assert (COMPLETE_TYPE_P (t));
5359 lazily_declare_fn (sfk_move_assignment, t);
5362 for (fns = lookup_fnfields_slot_nolazy (t, ansi_assopname (NOP_EXPR));
5363 fns; fns = OVL_NEXT (fns))
5364 if (move_fn_p (OVL_CURRENT (fns)))
5365 return true;
5367 return false;
5370 /* Returns true iff class T has a move constructor that was explicitly
5371 declared in the class body. Note that this is different from
5372 "user-provided", which doesn't include functions that are defaulted in
5373 the class. */
5375 bool
5376 type_has_user_declared_move_constructor (tree t)
5378 tree fns;
5380 if (CLASSTYPE_LAZY_MOVE_CTOR (t))
5381 return false;
5383 if (!CLASSTYPE_METHOD_VEC (t))
5384 return false;
5386 for (fns = CLASSTYPE_CONSTRUCTORS (t); fns; fns = OVL_NEXT (fns))
5388 tree fn = OVL_CURRENT (fns);
5389 if (move_fn_p (fn) && !DECL_ARTIFICIAL (fn))
5390 return true;
5393 return false;
5396 /* Returns true iff class T has a move assignment operator that was
5397 explicitly declared in the class body. */
5399 bool
5400 type_has_user_declared_move_assign (tree t)
5402 tree fns;
5404 if (CLASSTYPE_LAZY_MOVE_ASSIGN (t))
5405 return false;
5407 for (fns = lookup_fnfields_slot_nolazy (t, ansi_assopname (NOP_EXPR));
5408 fns; fns = OVL_NEXT (fns))
5410 tree fn = OVL_CURRENT (fns);
5411 if (move_fn_p (fn) && !DECL_ARTIFICIAL (fn))
5412 return true;
5415 return false;
5418 /* Nonzero if we need to build up a constructor call when initializing an
5419 object of this class, either because it has a user-declared constructor
5420 or because it doesn't have a default constructor (so we need to give an
5421 error if no initializer is provided). Use TYPE_NEEDS_CONSTRUCTING when
5422 what you care about is whether or not an object can be produced by a
5423 constructor (e.g. so we don't set TREE_READONLY on const variables of
5424 such type); use this function when what you care about is whether or not
5425 to try to call a constructor to create an object. The latter case is
5426 the former plus some cases of constructors that cannot be called. */
5428 bool
5429 type_build_ctor_call (tree t)
5431 tree inner;
5432 if (TYPE_NEEDS_CONSTRUCTING (t))
5433 return true;
5434 inner = strip_array_types (t);
5435 if (!CLASS_TYPE_P (inner) || ANON_AGGR_TYPE_P (inner))
5436 return false;
5437 if (!TYPE_HAS_DEFAULT_CONSTRUCTOR (inner))
5438 return true;
5439 if (cxx_dialect < cxx11)
5440 return false;
5441 /* A user-declared constructor might be private, and a constructor might
5442 be trivial but deleted. */
5443 for (tree fns = lookup_fnfields_slot (inner, complete_ctor_identifier);
5444 fns; fns = OVL_NEXT (fns))
5446 tree fn = OVL_CURRENT (fns);
5447 if (!DECL_ARTIFICIAL (fn)
5448 || DECL_DELETED_FN (fn))
5449 return true;
5451 return false;
5454 /* Like type_build_ctor_call, but for destructors. */
5456 bool
5457 type_build_dtor_call (tree t)
5459 tree inner;
5460 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t))
5461 return true;
5462 inner = strip_array_types (t);
5463 if (!CLASS_TYPE_P (inner) || ANON_AGGR_TYPE_P (inner)
5464 || !COMPLETE_TYPE_P (inner))
5465 return false;
5466 if (cxx_dialect < cxx11)
5467 return false;
5468 /* A user-declared destructor might be private, and a destructor might
5469 be trivial but deleted. */
5470 for (tree fns = lookup_fnfields_slot (inner, complete_dtor_identifier);
5471 fns; fns = OVL_NEXT (fns))
5473 tree fn = OVL_CURRENT (fns);
5474 if (!DECL_ARTIFICIAL (fn)
5475 || DECL_DELETED_FN (fn))
5476 return true;
5478 return false;
5481 /* Remove all zero-width bit-fields from T. */
5483 static void
5484 remove_zero_width_bit_fields (tree t)
5486 tree *fieldsp;
5488 fieldsp = &TYPE_FIELDS (t);
5489 while (*fieldsp)
5491 if (TREE_CODE (*fieldsp) == FIELD_DECL
5492 && DECL_C_BIT_FIELD (*fieldsp)
5493 /* We should not be confused by the fact that grokbitfield
5494 temporarily sets the width of the bit field into
5495 DECL_INITIAL (*fieldsp).
5496 check_bitfield_decl eventually sets DECL_SIZE (*fieldsp)
5497 to that width. */
5498 && (DECL_SIZE (*fieldsp) == NULL_TREE
5499 || integer_zerop (DECL_SIZE (*fieldsp))))
5500 *fieldsp = DECL_CHAIN (*fieldsp);
5501 else
5502 fieldsp = &DECL_CHAIN (*fieldsp);
5506 /* Returns TRUE iff we need a cookie when dynamically allocating an
5507 array whose elements have the indicated class TYPE. */
5509 static bool
5510 type_requires_array_cookie (tree type)
5512 tree fns;
5513 bool has_two_argument_delete_p = false;
5515 gcc_assert (CLASS_TYPE_P (type));
5517 /* If there's a non-trivial destructor, we need a cookie. In order
5518 to iterate through the array calling the destructor for each
5519 element, we'll have to know how many elements there are. */
5520 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
5521 return true;
5523 /* If the usual deallocation function is a two-argument whose second
5524 argument is of type `size_t', then we have to pass the size of
5525 the array to the deallocation function, so we will need to store
5526 a cookie. */
5527 fns = lookup_fnfields (TYPE_BINFO (type),
5528 ansi_opname (VEC_DELETE_EXPR),
5529 /*protect=*/0);
5530 /* If there are no `operator []' members, or the lookup is
5531 ambiguous, then we don't need a cookie. */
5532 if (!fns || fns == error_mark_node)
5533 return false;
5534 /* Loop through all of the functions. */
5535 for (fns = BASELINK_FUNCTIONS (fns); fns; fns = OVL_NEXT (fns))
5537 tree fn;
5538 tree second_parm;
5540 /* Select the current function. */
5541 fn = OVL_CURRENT (fns);
5542 /* See if this function is a one-argument delete function. If
5543 it is, then it will be the usual deallocation function. */
5544 second_parm = TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (fn)));
5545 if (second_parm == void_list_node)
5546 return false;
5547 /* Do not consider this function if its second argument is an
5548 ellipsis. */
5549 if (!second_parm)
5550 continue;
5551 /* Otherwise, if we have a two-argument function and the second
5552 argument is `size_t', it will be the usual deallocation
5553 function -- unless there is one-argument function, too. */
5554 if (TREE_CHAIN (second_parm) == void_list_node
5555 && same_type_p (TREE_VALUE (second_parm), size_type_node))
5556 has_two_argument_delete_p = true;
5559 return has_two_argument_delete_p;
5562 /* Finish computing the `literal type' property of class type T.
5564 At this point, we have already processed base classes and
5565 non-static data members. We need to check whether the copy
5566 constructor is trivial, the destructor is trivial, and there
5567 is a trivial default constructor or at least one constexpr
5568 constructor other than the copy constructor. */
5570 static void
5571 finalize_literal_type_property (tree t)
5573 tree fn;
5575 if (cxx_dialect < cxx11
5576 || TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t))
5577 CLASSTYPE_LITERAL_P (t) = false;
5578 else if (CLASSTYPE_LITERAL_P (t) && !TYPE_HAS_TRIVIAL_DFLT (t)
5579 && CLASSTYPE_NON_AGGREGATE (t)
5580 && !TYPE_HAS_CONSTEXPR_CTOR (t))
5581 CLASSTYPE_LITERAL_P (t) = false;
5583 if (!CLASSTYPE_LITERAL_P (t))
5584 for (fn = TYPE_METHODS (t); fn; fn = DECL_CHAIN (fn))
5585 if (DECL_DECLARED_CONSTEXPR_P (fn)
5586 && TREE_CODE (fn) != TEMPLATE_DECL
5587 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
5588 && !DECL_CONSTRUCTOR_P (fn))
5590 DECL_DECLARED_CONSTEXPR_P (fn) = false;
5591 if (!DECL_GENERATED_P (fn))
5593 error ("enclosing class of constexpr non-static member "
5594 "function %q+#D is not a literal type", fn);
5595 explain_non_literal_class (t);
5600 /* T is a non-literal type used in a context which requires a constant
5601 expression. Explain why it isn't literal. */
5603 void
5604 explain_non_literal_class (tree t)
5606 static hash_set<tree> *diagnosed;
5608 if (!CLASS_TYPE_P (t))
5609 return;
5610 t = TYPE_MAIN_VARIANT (t);
5612 if (diagnosed == NULL)
5613 diagnosed = new hash_set<tree>;
5614 if (diagnosed->add (t))
5615 /* Already explained. */
5616 return;
5618 inform (0, "%q+T is not literal because:", t);
5619 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t))
5620 inform (0, " %q+T has a non-trivial destructor", t);
5621 else if (CLASSTYPE_NON_AGGREGATE (t)
5622 && !TYPE_HAS_TRIVIAL_DFLT (t)
5623 && !TYPE_HAS_CONSTEXPR_CTOR (t))
5625 inform (0, " %q+T is not an aggregate, does not have a trivial "
5626 "default constructor, and has no constexpr constructor that "
5627 "is not a copy or move constructor", t);
5628 if (type_has_non_user_provided_default_constructor (t))
5630 /* Note that we can't simply call locate_ctor because when the
5631 constructor is deleted it just returns NULL_TREE. */
5632 tree fns;
5633 for (fns = CLASSTYPE_CONSTRUCTORS (t); fns; fns = OVL_NEXT (fns))
5635 tree fn = OVL_CURRENT (fns);
5636 tree parms = TYPE_ARG_TYPES (TREE_TYPE (fn));
5638 parms = skip_artificial_parms_for (fn, parms);
5640 if (sufficient_parms_p (parms))
5642 if (DECL_DELETED_FN (fn))
5643 maybe_explain_implicit_delete (fn);
5644 else
5645 explain_invalid_constexpr_fn (fn);
5646 break;
5651 else
5653 tree binfo, base_binfo, field; int i;
5654 for (binfo = TYPE_BINFO (t), i = 0;
5655 BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
5657 tree basetype = TREE_TYPE (base_binfo);
5658 if (!CLASSTYPE_LITERAL_P (basetype))
5660 inform (0, " base class %qT of %q+T is non-literal",
5661 basetype, t);
5662 explain_non_literal_class (basetype);
5663 return;
5666 for (field = TYPE_FIELDS (t); field; field = TREE_CHAIN (field))
5668 tree ftype;
5669 if (TREE_CODE (field) != FIELD_DECL)
5670 continue;
5671 ftype = TREE_TYPE (field);
5672 if (!literal_type_p (ftype))
5674 inform (DECL_SOURCE_LOCATION (field),
5675 " non-static data member %qD has non-literal type",
5676 field);
5677 if (CLASS_TYPE_P (ftype))
5678 explain_non_literal_class (ftype);
5680 if (CP_TYPE_VOLATILE_P (ftype))
5681 inform (DECL_SOURCE_LOCATION (field),
5682 " non-static data member %qD has volatile type", field);
5687 /* Check the validity of the bases and members declared in T. Add any
5688 implicitly-generated functions (like copy-constructors and
5689 assignment operators). Compute various flag bits (like
5690 CLASSTYPE_NON_LAYOUT_POD_T) for T. This routine works purely at the C++
5691 level: i.e., independently of the ABI in use. */
5693 static void
5694 check_bases_and_members (tree t)
5696 /* Nonzero if the implicitly generated copy constructor should take
5697 a non-const reference argument. */
5698 int cant_have_const_ctor;
5699 /* Nonzero if the implicitly generated assignment operator
5700 should take a non-const reference argument. */
5701 int no_const_asn_ref;
5702 tree access_decls;
5703 bool saved_complex_asn_ref;
5704 bool saved_nontrivial_dtor;
5705 tree fn;
5707 /* By default, we use const reference arguments and generate default
5708 constructors. */
5709 cant_have_const_ctor = 0;
5710 no_const_asn_ref = 0;
5712 /* Check all the base-classes and set FMEM members to point to arrays
5713 of potential interest. */
5714 check_bases (t, &cant_have_const_ctor, &no_const_asn_ref);
5716 /* Deduce noexcept on destructors. This needs to happen after we've set
5717 triviality flags appropriately for our bases. */
5718 if (cxx_dialect >= cxx11)
5719 deduce_noexcept_on_destructors (t);
5721 /* Check all the method declarations. */
5722 check_methods (t);
5724 /* Save the initial values of these flags which only indicate whether
5725 or not the class has user-provided functions. As we analyze the
5726 bases and members we can set these flags for other reasons. */
5727 saved_complex_asn_ref = TYPE_HAS_COMPLEX_COPY_ASSIGN (t);
5728 saved_nontrivial_dtor = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t);
5730 /* Check all the data member declarations. We cannot call
5731 check_field_decls until we have called check_bases check_methods,
5732 as check_field_decls depends on TYPE_HAS_NONTRIVIAL_DESTRUCTOR
5733 being set appropriately. */
5734 check_field_decls (t, &access_decls,
5735 &cant_have_const_ctor,
5736 &no_const_asn_ref);
5738 /* A nearly-empty class has to be vptr-containing; a nearly empty
5739 class contains just a vptr. */
5740 if (!TYPE_CONTAINS_VPTR_P (t))
5741 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
5743 /* Do some bookkeeping that will guide the generation of implicitly
5744 declared member functions. */
5745 TYPE_HAS_COMPLEX_COPY_CTOR (t) |= TYPE_CONTAINS_VPTR_P (t);
5746 TYPE_HAS_COMPLEX_MOVE_CTOR (t) |= TYPE_CONTAINS_VPTR_P (t);
5747 /* We need to call a constructor for this class if it has a
5748 user-provided constructor, or if the default constructor is going
5749 to initialize the vptr. (This is not an if-and-only-if;
5750 TYPE_NEEDS_CONSTRUCTING is set elsewhere if bases or members
5751 themselves need constructing.) */
5752 TYPE_NEEDS_CONSTRUCTING (t)
5753 |= (type_has_user_provided_constructor (t) || TYPE_CONTAINS_VPTR_P (t));
5754 /* [dcl.init.aggr]
5756 An aggregate is an array or a class with no user-provided
5757 constructors ... and no virtual functions.
5759 Again, other conditions for being an aggregate are checked
5760 elsewhere. */
5761 CLASSTYPE_NON_AGGREGATE (t)
5762 |= (type_has_user_provided_or_explicit_constructor (t)
5763 || TYPE_POLYMORPHIC_P (t));
5764 /* This is the C++98/03 definition of POD; it changed in C++0x, but we
5765 retain the old definition internally for ABI reasons. */
5766 CLASSTYPE_NON_LAYOUT_POD_P (t)
5767 |= (CLASSTYPE_NON_AGGREGATE (t)
5768 || saved_nontrivial_dtor || saved_complex_asn_ref);
5769 CLASSTYPE_NON_STD_LAYOUT (t) |= TYPE_CONTAINS_VPTR_P (t);
5770 TYPE_HAS_COMPLEX_COPY_ASSIGN (t) |= TYPE_CONTAINS_VPTR_P (t);
5771 TYPE_HAS_COMPLEX_MOVE_ASSIGN (t) |= TYPE_CONTAINS_VPTR_P (t);
5772 TYPE_HAS_COMPLEX_DFLT (t) |= TYPE_CONTAINS_VPTR_P (t);
5774 /* If the only explicitly declared default constructor is user-provided,
5775 set TYPE_HAS_COMPLEX_DFLT. */
5776 if (!TYPE_HAS_COMPLEX_DFLT (t)
5777 && TYPE_HAS_DEFAULT_CONSTRUCTOR (t)
5778 && !type_has_non_user_provided_default_constructor (t))
5779 TYPE_HAS_COMPLEX_DFLT (t) = true;
5781 /* Warn if a public base of a polymorphic type has an accessible
5782 non-virtual destructor. It is only now that we know the class is
5783 polymorphic. Although a polymorphic base will have a already
5784 been diagnosed during its definition, we warn on use too. */
5785 if (TYPE_POLYMORPHIC_P (t) && warn_nonvdtor)
5787 tree binfo = TYPE_BINFO (t);
5788 vec<tree, va_gc> *accesses = BINFO_BASE_ACCESSES (binfo);
5789 tree base_binfo;
5790 unsigned i;
5792 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
5794 tree basetype = TREE_TYPE (base_binfo);
5796 if ((*accesses)[i] == access_public_node
5797 && (TYPE_POLYMORPHIC_P (basetype) || warn_ecpp)
5798 && accessible_nvdtor_p (basetype))
5799 warning (OPT_Wnon_virtual_dtor,
5800 "base class %q#T has accessible non-virtual destructor",
5801 basetype);
5805 /* If the class has no user-declared constructor, but does have
5806 non-static const or reference data members that can never be
5807 initialized, issue a warning. */
5808 if (warn_uninitialized
5809 /* Classes with user-declared constructors are presumed to
5810 initialize these members. */
5811 && !TYPE_HAS_USER_CONSTRUCTOR (t)
5812 /* Aggregates can be initialized with brace-enclosed
5813 initializers. */
5814 && CLASSTYPE_NON_AGGREGATE (t))
5816 tree field;
5818 for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
5820 tree type;
5822 if (TREE_CODE (field) != FIELD_DECL
5823 || DECL_INITIAL (field) != NULL_TREE)
5824 continue;
5826 type = TREE_TYPE (field);
5827 if (TREE_CODE (type) == REFERENCE_TYPE)
5828 warning_at (DECL_SOURCE_LOCATION (field),
5829 OPT_Wuninitialized, "non-static reference %q#D "
5830 "in class without a constructor", field);
5831 else if (CP_TYPE_CONST_P (type)
5832 && (!CLASS_TYPE_P (type)
5833 || !TYPE_HAS_DEFAULT_CONSTRUCTOR (type)))
5834 warning_at (DECL_SOURCE_LOCATION (field),
5835 OPT_Wuninitialized, "non-static const member %q#D "
5836 "in class without a constructor", field);
5840 /* Synthesize any needed methods. */
5841 add_implicitly_declared_members (t, &access_decls,
5842 cant_have_const_ctor,
5843 no_const_asn_ref);
5845 /* Check defaulted declarations here so we have cant_have_const_ctor
5846 and don't need to worry about clones. */
5847 for (fn = TYPE_METHODS (t); fn; fn = DECL_CHAIN (fn))
5848 if (!DECL_ARTIFICIAL (fn) && DECL_DEFAULTED_IN_CLASS_P (fn))
5850 int copy = copy_fn_p (fn);
5851 if (copy > 0)
5853 bool imp_const_p
5854 = (DECL_CONSTRUCTOR_P (fn) ? !cant_have_const_ctor
5855 : !no_const_asn_ref);
5856 bool fn_const_p = (copy == 2);
5858 if (fn_const_p && !imp_const_p)
5859 /* If the function is defaulted outside the class, we just
5860 give the synthesis error. */
5861 error ("%q+D declared to take const reference, but implicit "
5862 "declaration would take non-const", fn);
5864 defaulted_late_check (fn);
5867 if (LAMBDA_TYPE_P (t))
5869 /* "This class type is not an aggregate." */
5870 CLASSTYPE_NON_AGGREGATE (t) = 1;
5873 /* Compute the 'literal type' property before we
5874 do anything with non-static member functions. */
5875 finalize_literal_type_property (t);
5877 /* Create the in-charge and not-in-charge variants of constructors
5878 and destructors. */
5879 clone_constructors_and_destructors (t);
5881 /* Process the using-declarations. */
5882 for (; access_decls; access_decls = TREE_CHAIN (access_decls))
5883 handle_using_decl (TREE_VALUE (access_decls), t);
5885 /* Build and sort the CLASSTYPE_METHOD_VEC. */
5886 finish_struct_methods (t);
5888 /* Figure out whether or not we will need a cookie when dynamically
5889 allocating an array of this type. */
5890 TYPE_LANG_SPECIFIC (t)->u.c.vec_new_uses_cookie
5891 = type_requires_array_cookie (t);
5894 /* If T needs a pointer to its virtual function table, set TYPE_VFIELD
5895 accordingly. If a new vfield was created (because T doesn't have a
5896 primary base class), then the newly created field is returned. It
5897 is not added to the TYPE_FIELDS list; it is the caller's
5898 responsibility to do that. Accumulate declared virtual functions
5899 on VIRTUALS_P. */
5901 static tree
5902 create_vtable_ptr (tree t, tree* virtuals_p)
5904 tree fn;
5906 /* Collect the virtual functions declared in T. */
5907 for (fn = TYPE_METHODS (t); fn; fn = DECL_CHAIN (fn))
5908 if (TREE_CODE (fn) == FUNCTION_DECL
5909 && DECL_VINDEX (fn) && !DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (fn)
5910 && TREE_CODE (DECL_VINDEX (fn)) != INTEGER_CST)
5912 tree new_virtual = make_node (TREE_LIST);
5914 BV_FN (new_virtual) = fn;
5915 BV_DELTA (new_virtual) = integer_zero_node;
5916 BV_VCALL_INDEX (new_virtual) = NULL_TREE;
5918 TREE_CHAIN (new_virtual) = *virtuals_p;
5919 *virtuals_p = new_virtual;
5922 /* If we couldn't find an appropriate base class, create a new field
5923 here. Even if there weren't any new virtual functions, we might need a
5924 new virtual function table if we're supposed to include vptrs in
5925 all classes that need them. */
5926 if (!TYPE_VFIELD (t) && (*virtuals_p || TYPE_CONTAINS_VPTR_P (t)))
5928 /* We build this decl with vtbl_ptr_type_node, which is a
5929 `vtable_entry_type*'. It might seem more precise to use
5930 `vtable_entry_type (*)[N]' where N is the number of virtual
5931 functions. However, that would require the vtable pointer in
5932 base classes to have a different type than the vtable pointer
5933 in derived classes. We could make that happen, but that
5934 still wouldn't solve all the problems. In particular, the
5935 type-based alias analysis code would decide that assignments
5936 to the base class vtable pointer can't alias assignments to
5937 the derived class vtable pointer, since they have different
5938 types. Thus, in a derived class destructor, where the base
5939 class constructor was inlined, we could generate bad code for
5940 setting up the vtable pointer.
5942 Therefore, we use one type for all vtable pointers. We still
5943 use a type-correct type; it's just doesn't indicate the array
5944 bounds. That's better than using `void*' or some such; it's
5945 cleaner, and it let's the alias analysis code know that these
5946 stores cannot alias stores to void*! */
5947 tree field;
5949 field = build_decl (input_location,
5950 FIELD_DECL, get_vfield_name (t), vtbl_ptr_type_node);
5951 DECL_VIRTUAL_P (field) = 1;
5952 DECL_ARTIFICIAL (field) = 1;
5953 DECL_FIELD_CONTEXT (field) = t;
5954 DECL_FCONTEXT (field) = t;
5955 if (TYPE_PACKED (t))
5956 DECL_PACKED (field) = 1;
5958 TYPE_VFIELD (t) = field;
5960 /* This class is non-empty. */
5961 CLASSTYPE_EMPTY_P (t) = 0;
5963 return field;
5966 return NULL_TREE;
5969 /* Add OFFSET to all base types of BINFO which is a base in the
5970 hierarchy dominated by T.
5972 OFFSET, which is a type offset, is number of bytes. */
5974 static void
5975 propagate_binfo_offsets (tree binfo, tree offset)
5977 int i;
5978 tree primary_binfo;
5979 tree base_binfo;
5981 /* Update BINFO's offset. */
5982 BINFO_OFFSET (binfo)
5983 = fold_convert (sizetype,
5984 size_binop (PLUS_EXPR,
5985 fold_convert (ssizetype, BINFO_OFFSET (binfo)),
5986 offset));
5988 /* Find the primary base class. */
5989 primary_binfo = get_primary_binfo (binfo);
5991 if (primary_binfo && BINFO_INHERITANCE_CHAIN (primary_binfo) == binfo)
5992 propagate_binfo_offsets (primary_binfo, offset);
5994 /* Scan all of the bases, pushing the BINFO_OFFSET adjust
5995 downwards. */
5996 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
5998 /* Don't do the primary base twice. */
5999 if (base_binfo == primary_binfo)
6000 continue;
6002 if (BINFO_VIRTUAL_P (base_binfo))
6003 continue;
6005 propagate_binfo_offsets (base_binfo, offset);
6009 /* Set BINFO_OFFSET for all of the virtual bases for RLI->T. Update
6010 TYPE_ALIGN and TYPE_SIZE for T. OFFSETS gives the location of
6011 empty subobjects of T. */
6013 static void
6014 layout_virtual_bases (record_layout_info rli, splay_tree offsets)
6016 tree vbase;
6017 tree t = rli->t;
6018 tree *next_field;
6020 if (BINFO_N_BASE_BINFOS (TYPE_BINFO (t)) == 0)
6021 return;
6023 /* Find the last field. The artificial fields created for virtual
6024 bases will go after the last extant field to date. */
6025 next_field = &TYPE_FIELDS (t);
6026 while (*next_field)
6027 next_field = &DECL_CHAIN (*next_field);
6029 /* Go through the virtual bases, allocating space for each virtual
6030 base that is not already a primary base class. These are
6031 allocated in inheritance graph order. */
6032 for (vbase = TYPE_BINFO (t); vbase; vbase = TREE_CHAIN (vbase))
6034 if (!BINFO_VIRTUAL_P (vbase))
6035 continue;
6037 if (!BINFO_PRIMARY_P (vbase))
6039 /* This virtual base is not a primary base of any class in the
6040 hierarchy, so we have to add space for it. */
6041 next_field = build_base_field (rli, vbase,
6042 offsets, next_field);
6047 /* Returns the offset of the byte just past the end of the base class
6048 BINFO. */
6050 static tree
6051 end_of_base (tree binfo)
6053 tree size;
6055 if (!CLASSTYPE_AS_BASE (BINFO_TYPE (binfo)))
6056 size = TYPE_SIZE_UNIT (char_type_node);
6057 else if (is_empty_class (BINFO_TYPE (binfo)))
6058 /* An empty class has zero CLASSTYPE_SIZE_UNIT, but we need to
6059 allocate some space for it. It cannot have virtual bases, so
6060 TYPE_SIZE_UNIT is fine. */
6061 size = TYPE_SIZE_UNIT (BINFO_TYPE (binfo));
6062 else
6063 size = CLASSTYPE_SIZE_UNIT (BINFO_TYPE (binfo));
6065 return size_binop (PLUS_EXPR, BINFO_OFFSET (binfo), size);
6068 /* Returns the offset of the byte just past the end of the base class
6069 with the highest offset in T. If INCLUDE_VIRTUALS_P is zero, then
6070 only non-virtual bases are included. */
6072 static tree
6073 end_of_class (tree t, int include_virtuals_p)
6075 tree result = size_zero_node;
6076 vec<tree, va_gc> *vbases;
6077 tree binfo;
6078 tree base_binfo;
6079 tree offset;
6080 int i;
6082 for (binfo = TYPE_BINFO (t), i = 0;
6083 BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
6085 if (!include_virtuals_p
6086 && BINFO_VIRTUAL_P (base_binfo)
6087 && (!BINFO_PRIMARY_P (base_binfo)
6088 || BINFO_INHERITANCE_CHAIN (base_binfo) != TYPE_BINFO (t)))
6089 continue;
6091 offset = end_of_base (base_binfo);
6092 if (tree_int_cst_lt (result, offset))
6093 result = offset;
6096 if (include_virtuals_p)
6097 for (vbases = CLASSTYPE_VBASECLASSES (t), i = 0;
6098 vec_safe_iterate (vbases, i, &base_binfo); i++)
6100 offset = end_of_base (base_binfo);
6101 if (tree_int_cst_lt (result, offset))
6102 result = offset;
6105 return result;
6108 /* Warn about bases of T that are inaccessible because they are
6109 ambiguous. For example:
6111 struct S {};
6112 struct T : public S {};
6113 struct U : public S, public T {};
6115 Here, `(S*) new U' is not allowed because there are two `S'
6116 subobjects of U. */
6118 static void
6119 warn_about_ambiguous_bases (tree t)
6121 int i;
6122 vec<tree, va_gc> *vbases;
6123 tree basetype;
6124 tree binfo;
6125 tree base_binfo;
6127 /* If there are no repeated bases, nothing can be ambiguous. */
6128 if (!CLASSTYPE_REPEATED_BASE_P (t))
6129 return;
6131 /* Check direct bases. */
6132 for (binfo = TYPE_BINFO (t), i = 0;
6133 BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
6135 basetype = BINFO_TYPE (base_binfo);
6137 if (!uniquely_derived_from_p (basetype, t))
6138 warning (0, "direct base %qT inaccessible in %qT due to ambiguity",
6139 basetype, t);
6142 /* Check for ambiguous virtual bases. */
6143 if (extra_warnings)
6144 for (vbases = CLASSTYPE_VBASECLASSES (t), i = 0;
6145 vec_safe_iterate (vbases, i, &binfo); i++)
6147 basetype = BINFO_TYPE (binfo);
6149 if (!uniquely_derived_from_p (basetype, t))
6150 warning (OPT_Wextra, "virtual base %qT inaccessible in %qT due "
6151 "to ambiguity", basetype, t);
6155 /* Compare two INTEGER_CSTs K1 and K2. */
6157 static int
6158 splay_tree_compare_integer_csts (splay_tree_key k1, splay_tree_key k2)
6160 return tree_int_cst_compare ((tree) k1, (tree) k2);
6163 /* Increase the size indicated in RLI to account for empty classes
6164 that are "off the end" of the class. */
6166 static void
6167 include_empty_classes (record_layout_info rli)
6169 tree eoc;
6170 tree rli_size;
6172 /* It might be the case that we grew the class to allocate a
6173 zero-sized base class. That won't be reflected in RLI, yet,
6174 because we are willing to overlay multiple bases at the same
6175 offset. However, now we need to make sure that RLI is big enough
6176 to reflect the entire class. */
6177 eoc = end_of_class (rli->t,
6178 CLASSTYPE_AS_BASE (rli->t) != NULL_TREE);
6179 rli_size = rli_size_unit_so_far (rli);
6180 if (TREE_CODE (rli_size) == INTEGER_CST
6181 && tree_int_cst_lt (rli_size, eoc))
6183 /* The size should have been rounded to a whole byte. */
6184 gcc_assert (tree_int_cst_equal
6185 (rli->bitpos, round_down (rli->bitpos, BITS_PER_UNIT)));
6186 rli->bitpos
6187 = size_binop (PLUS_EXPR,
6188 rli->bitpos,
6189 size_binop (MULT_EXPR,
6190 fold_convert (bitsizetype,
6191 size_binop (MINUS_EXPR,
6192 eoc, rli_size)),
6193 bitsize_int (BITS_PER_UNIT)));
6194 normalize_rli (rli);
6198 /* Calculate the TYPE_SIZE, TYPE_ALIGN, etc for T. Calculate
6199 BINFO_OFFSETs for all of the base-classes. Position the vtable
6200 pointer. Accumulate declared virtual functions on VIRTUALS_P. */
6202 static void
6203 layout_class_type (tree t, tree *virtuals_p)
6205 tree non_static_data_members;
6206 tree field;
6207 tree vptr;
6208 record_layout_info rli;
6209 /* Maps offsets (represented as INTEGER_CSTs) to a TREE_LIST of
6210 types that appear at that offset. */
6211 splay_tree empty_base_offsets;
6212 /* True if the last field laid out was a bit-field. */
6213 bool last_field_was_bitfield = false;
6214 /* The location at which the next field should be inserted. */
6215 tree *next_field;
6216 /* T, as a base class. */
6217 tree base_t;
6219 /* Keep track of the first non-static data member. */
6220 non_static_data_members = TYPE_FIELDS (t);
6222 /* Start laying out the record. */
6223 rli = start_record_layout (t);
6225 /* Mark all the primary bases in the hierarchy. */
6226 determine_primary_bases (t);
6228 /* Create a pointer to our virtual function table. */
6229 vptr = create_vtable_ptr (t, virtuals_p);
6231 /* The vptr is always the first thing in the class. */
6232 if (vptr)
6234 DECL_CHAIN (vptr) = TYPE_FIELDS (t);
6235 TYPE_FIELDS (t) = vptr;
6236 next_field = &DECL_CHAIN (vptr);
6237 place_field (rli, vptr);
6239 else
6240 next_field = &TYPE_FIELDS (t);
6242 /* Build FIELD_DECLs for all of the non-virtual base-types. */
6243 empty_base_offsets = splay_tree_new (splay_tree_compare_integer_csts,
6244 NULL, NULL);
6245 build_base_fields (rli, empty_base_offsets, next_field);
6247 /* Layout the non-static data members. */
6248 for (field = non_static_data_members; field; field = DECL_CHAIN (field))
6250 tree type;
6251 tree padding;
6253 /* We still pass things that aren't non-static data members to
6254 the back end, in case it wants to do something with them. */
6255 if (TREE_CODE (field) != FIELD_DECL)
6257 place_field (rli, field);
6258 /* If the static data member has incomplete type, keep track
6259 of it so that it can be completed later. (The handling
6260 of pending statics in finish_record_layout is
6261 insufficient; consider:
6263 struct S1;
6264 struct S2 { static S1 s1; };
6266 At this point, finish_record_layout will be called, but
6267 S1 is still incomplete.) */
6268 if (VAR_P (field))
6270 maybe_register_incomplete_var (field);
6271 /* The visibility of static data members is determined
6272 at their point of declaration, not their point of
6273 definition. */
6274 determine_visibility (field);
6276 continue;
6279 type = TREE_TYPE (field);
6280 if (type == error_mark_node)
6281 continue;
6283 padding = NULL_TREE;
6285 /* If this field is a bit-field whose width is greater than its
6286 type, then there are some special rules for allocating
6287 it. */
6288 if (DECL_C_BIT_FIELD (field)
6289 && tree_int_cst_lt (TYPE_SIZE (type), DECL_SIZE (field)))
6291 unsigned int itk;
6292 tree integer_type;
6293 bool was_unnamed_p = false;
6294 /* We must allocate the bits as if suitably aligned for the
6295 longest integer type that fits in this many bits. type
6296 of the field. Then, we are supposed to use the left over
6297 bits as additional padding. */
6298 for (itk = itk_char; itk != itk_none; ++itk)
6299 if (integer_types[itk] != NULL_TREE
6300 && (tree_int_cst_lt (size_int (MAX_FIXED_MODE_SIZE),
6301 TYPE_SIZE (integer_types[itk]))
6302 || tree_int_cst_lt (DECL_SIZE (field),
6303 TYPE_SIZE (integer_types[itk]))))
6304 break;
6306 /* ITK now indicates a type that is too large for the
6307 field. We have to back up by one to find the largest
6308 type that fits. */
6311 --itk;
6312 integer_type = integer_types[itk];
6313 } while (itk > 0 && integer_type == NULL_TREE);
6315 /* Figure out how much additional padding is required. */
6316 if (tree_int_cst_lt (TYPE_SIZE (integer_type), DECL_SIZE (field)))
6318 if (TREE_CODE (t) == UNION_TYPE)
6319 /* In a union, the padding field must have the full width
6320 of the bit-field; all fields start at offset zero. */
6321 padding = DECL_SIZE (field);
6322 else
6323 padding = size_binop (MINUS_EXPR, DECL_SIZE (field),
6324 TYPE_SIZE (integer_type));
6327 /* An unnamed bitfield does not normally affect the
6328 alignment of the containing class on a target where
6329 PCC_BITFIELD_TYPE_MATTERS. But, the C++ ABI does not
6330 make any exceptions for unnamed bitfields when the
6331 bitfields are longer than their types. Therefore, we
6332 temporarily give the field a name. */
6333 if (PCC_BITFIELD_TYPE_MATTERS && !DECL_NAME (field))
6335 was_unnamed_p = true;
6336 DECL_NAME (field) = make_anon_name ();
6339 DECL_SIZE (field) = TYPE_SIZE (integer_type);
6340 DECL_ALIGN (field) = TYPE_ALIGN (integer_type);
6341 DECL_USER_ALIGN (field) = TYPE_USER_ALIGN (integer_type);
6342 layout_nonempty_base_or_field (rli, field, NULL_TREE,
6343 empty_base_offsets);
6344 if (was_unnamed_p)
6345 DECL_NAME (field) = NULL_TREE;
6346 /* Now that layout has been performed, set the size of the
6347 field to the size of its declared type; the rest of the
6348 field is effectively invisible. */
6349 DECL_SIZE (field) = TYPE_SIZE (type);
6350 /* We must also reset the DECL_MODE of the field. */
6351 DECL_MODE (field) = TYPE_MODE (type);
6353 else
6354 layout_nonempty_base_or_field (rli, field, NULL_TREE,
6355 empty_base_offsets);
6357 /* Remember the location of any empty classes in FIELD. */
6358 record_subobject_offsets (TREE_TYPE (field),
6359 byte_position(field),
6360 empty_base_offsets,
6361 /*is_data_member=*/true);
6363 /* If a bit-field does not immediately follow another bit-field,
6364 and yet it starts in the middle of a byte, we have failed to
6365 comply with the ABI. */
6366 if (warn_abi
6367 && DECL_C_BIT_FIELD (field)
6368 /* The TREE_NO_WARNING flag gets set by Objective-C when
6369 laying out an Objective-C class. The ObjC ABI differs
6370 from the C++ ABI, and so we do not want a warning
6371 here. */
6372 && !TREE_NO_WARNING (field)
6373 && !last_field_was_bitfield
6374 && !integer_zerop (size_binop (TRUNC_MOD_EXPR,
6375 DECL_FIELD_BIT_OFFSET (field),
6376 bitsize_unit_node)))
6377 warning_at (DECL_SOURCE_LOCATION (field), OPT_Wabi,
6378 "offset of %qD is not ABI-compliant and may "
6379 "change in a future version of GCC", field);
6381 /* The middle end uses the type of expressions to determine the
6382 possible range of expression values. In order to optimize
6383 "x.i > 7" to "false" for a 2-bit bitfield "i", the middle end
6384 must be made aware of the width of "i", via its type.
6386 Because C++ does not have integer types of arbitrary width,
6387 we must (for the purposes of the front end) convert from the
6388 type assigned here to the declared type of the bitfield
6389 whenever a bitfield expression is used as an rvalue.
6390 Similarly, when assigning a value to a bitfield, the value
6391 must be converted to the type given the bitfield here. */
6392 if (DECL_C_BIT_FIELD (field))
6394 unsigned HOST_WIDE_INT width;
6395 tree ftype = TREE_TYPE (field);
6396 width = tree_to_uhwi (DECL_SIZE (field));
6397 if (width != TYPE_PRECISION (ftype))
6399 TREE_TYPE (field)
6400 = c_build_bitfield_integer_type (width,
6401 TYPE_UNSIGNED (ftype));
6402 TREE_TYPE (field)
6403 = cp_build_qualified_type (TREE_TYPE (field),
6404 cp_type_quals (ftype));
6408 /* If we needed additional padding after this field, add it
6409 now. */
6410 if (padding)
6412 tree padding_field;
6414 padding_field = build_decl (input_location,
6415 FIELD_DECL,
6416 NULL_TREE,
6417 char_type_node);
6418 DECL_BIT_FIELD (padding_field) = 1;
6419 DECL_SIZE (padding_field) = padding;
6420 DECL_CONTEXT (padding_field) = t;
6421 DECL_ARTIFICIAL (padding_field) = 1;
6422 DECL_IGNORED_P (padding_field) = 1;
6423 layout_nonempty_base_or_field (rli, padding_field,
6424 NULL_TREE,
6425 empty_base_offsets);
6428 last_field_was_bitfield = DECL_C_BIT_FIELD (field);
6431 if (!integer_zerop (rli->bitpos))
6433 /* Make sure that we are on a byte boundary so that the size of
6434 the class without virtual bases will always be a round number
6435 of bytes. */
6436 rli->bitpos = round_up_loc (input_location, rli->bitpos, BITS_PER_UNIT);
6437 normalize_rli (rli);
6440 /* Delete all zero-width bit-fields from the list of fields. Now
6441 that the type is laid out they are no longer important. */
6442 remove_zero_width_bit_fields (t);
6444 /* Create the version of T used for virtual bases. We do not use
6445 make_class_type for this version; this is an artificial type. For
6446 a POD type, we just reuse T. */
6447 if (CLASSTYPE_NON_LAYOUT_POD_P (t) || CLASSTYPE_EMPTY_P (t))
6449 base_t = make_node (TREE_CODE (t));
6451 /* Set the size and alignment for the new type. */
6452 tree eoc;
6454 /* If the ABI version is not at least two, and the last
6455 field was a bit-field, RLI may not be on a byte
6456 boundary. In particular, rli_size_unit_so_far might
6457 indicate the last complete byte, while rli_size_so_far
6458 indicates the total number of bits used. Therefore,
6459 rli_size_so_far, rather than rli_size_unit_so_far, is
6460 used to compute TYPE_SIZE_UNIT. */
6461 eoc = end_of_class (t, /*include_virtuals_p=*/0);
6462 TYPE_SIZE_UNIT (base_t)
6463 = size_binop (MAX_EXPR,
6464 fold_convert (sizetype,
6465 size_binop (CEIL_DIV_EXPR,
6466 rli_size_so_far (rli),
6467 bitsize_int (BITS_PER_UNIT))),
6468 eoc);
6469 TYPE_SIZE (base_t)
6470 = size_binop (MAX_EXPR,
6471 rli_size_so_far (rli),
6472 size_binop (MULT_EXPR,
6473 fold_convert (bitsizetype, eoc),
6474 bitsize_int (BITS_PER_UNIT)));
6475 TYPE_ALIGN (base_t) = rli->record_align;
6476 TYPE_USER_ALIGN (base_t) = TYPE_USER_ALIGN (t);
6478 /* Copy the fields from T. */
6479 next_field = &TYPE_FIELDS (base_t);
6480 for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
6481 if (TREE_CODE (field) == FIELD_DECL)
6483 *next_field = copy_node (field);
6484 DECL_CONTEXT (*next_field) = base_t;
6485 next_field = &DECL_CHAIN (*next_field);
6487 *next_field = NULL_TREE;
6489 /* Record the base version of the type. */
6490 CLASSTYPE_AS_BASE (t) = base_t;
6491 TYPE_CONTEXT (base_t) = t;
6493 else
6494 CLASSTYPE_AS_BASE (t) = t;
6496 /* Every empty class contains an empty class. */
6497 if (CLASSTYPE_EMPTY_P (t))
6498 CLASSTYPE_CONTAINS_EMPTY_CLASS_P (t) = 1;
6500 /* Set the TYPE_DECL for this type to contain the right
6501 value for DECL_OFFSET, so that we can use it as part
6502 of a COMPONENT_REF for multiple inheritance. */
6503 layout_decl (TYPE_MAIN_DECL (t), 0);
6505 /* Now fix up any virtual base class types that we left lying
6506 around. We must get these done before we try to lay out the
6507 virtual function table. As a side-effect, this will remove the
6508 base subobject fields. */
6509 layout_virtual_bases (rli, empty_base_offsets);
6511 /* Make sure that empty classes are reflected in RLI at this
6512 point. */
6513 include_empty_classes(rli);
6515 /* Make sure not to create any structures with zero size. */
6516 if (integer_zerop (rli_size_unit_so_far (rli)) && CLASSTYPE_EMPTY_P (t))
6517 place_field (rli,
6518 build_decl (input_location,
6519 FIELD_DECL, NULL_TREE, char_type_node));
6521 /* If this is a non-POD, declaring it packed makes a difference to how it
6522 can be used as a field; don't let finalize_record_size undo it. */
6523 if (TYPE_PACKED (t) && !layout_pod_type_p (t))
6524 rli->packed_maybe_necessary = true;
6526 /* Let the back end lay out the type. */
6527 finish_record_layout (rli, /*free_p=*/true);
6529 if (TYPE_SIZE_UNIT (t)
6530 && TREE_CODE (TYPE_SIZE_UNIT (t)) == INTEGER_CST
6531 && !TREE_OVERFLOW (TYPE_SIZE_UNIT (t))
6532 && !valid_constant_size_p (TYPE_SIZE_UNIT (t)))
6533 error ("size of type %qT is too large (%qE bytes)", t, TYPE_SIZE_UNIT (t));
6535 /* Warn about bases that can't be talked about due to ambiguity. */
6536 warn_about_ambiguous_bases (t);
6538 /* Now that we're done with layout, give the base fields the real types. */
6539 for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
6540 if (DECL_ARTIFICIAL (field) && IS_FAKE_BASE_TYPE (TREE_TYPE (field)))
6541 TREE_TYPE (field) = TYPE_CONTEXT (TREE_TYPE (field));
6543 /* Clean up. */
6544 splay_tree_delete (empty_base_offsets);
6546 if (CLASSTYPE_EMPTY_P (t)
6547 && tree_int_cst_lt (sizeof_biggest_empty_class,
6548 TYPE_SIZE_UNIT (t)))
6549 sizeof_biggest_empty_class = TYPE_SIZE_UNIT (t);
6552 /* Determine the "key method" for the class type indicated by TYPE,
6553 and set CLASSTYPE_KEY_METHOD accordingly. */
6555 void
6556 determine_key_method (tree type)
6558 tree method;
6560 if (TYPE_FOR_JAVA (type)
6561 || processing_template_decl
6562 || CLASSTYPE_TEMPLATE_INSTANTIATION (type)
6563 || CLASSTYPE_INTERFACE_KNOWN (type))
6564 return;
6566 /* The key method is the first non-pure virtual function that is not
6567 inline at the point of class definition. On some targets the
6568 key function may not be inline; those targets should not call
6569 this function until the end of the translation unit. */
6570 for (method = TYPE_METHODS (type); method != NULL_TREE;
6571 method = DECL_CHAIN (method))
6572 if (TREE_CODE (method) == FUNCTION_DECL
6573 && DECL_VINDEX (method) != NULL_TREE
6574 && ! DECL_DECLARED_INLINE_P (method)
6575 && ! DECL_PURE_VIRTUAL_P (method))
6577 CLASSTYPE_KEY_METHOD (type) = method;
6578 break;
6581 return;
6585 /* Allocate and return an instance of struct sorted_fields_type with
6586 N fields. */
6588 static struct sorted_fields_type *
6589 sorted_fields_type_new (int n)
6591 struct sorted_fields_type *sft;
6592 sft = (sorted_fields_type *) ggc_internal_alloc (sizeof (sorted_fields_type)
6593 + n * sizeof (tree));
6594 sft->len = n;
6596 return sft;
6599 /* Helper of find_flexarrays. Return true when FLD refers to a non-static
6600 class data member of non-zero size, otherwise false. */
6602 static inline bool
6603 field_nonempty_p (const_tree fld)
6605 if (TREE_CODE (fld) == ERROR_MARK)
6606 return false;
6608 tree type = TREE_TYPE (fld);
6609 if (TREE_CODE (fld) == FIELD_DECL
6610 && TREE_CODE (type) != ERROR_MARK
6611 && (DECL_NAME (fld) || RECORD_OR_UNION_TYPE_P (type)))
6613 return TYPE_SIZE (type)
6614 && (TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST
6615 || !tree_int_cst_equal (size_zero_node, TYPE_SIZE (type)));
6618 return false;
6621 /* Used by find_flexarrays and related. */
6622 struct flexmems_t {
6623 /* The first flexible array member or non-zero array member found
6624 in order of layout. */
6625 tree array;
6626 /* First non-static non-empty data member in the class or its bases. */
6627 tree first;
6628 /* First non-static non-empty data member following either the flexible
6629 array member, if found, or the zero-length array member. */
6630 tree after;
6633 /* Find either the first flexible array member or the first zero-length
6634 array, in that order or preference, among members of class T (but not
6635 its base classes), and set members of FMEM accordingly. */
6637 static void
6638 find_flexarrays (tree t, flexmems_t *fmem)
6640 for (tree fld = TYPE_FIELDS (t), next; fld; fld = next)
6642 /* Find the next non-static data member if it exists. */
6643 for (next = fld;
6644 (next = DECL_CHAIN (next))
6645 && TREE_CODE (next) != FIELD_DECL; );
6647 tree fldtype = TREE_TYPE (fld);
6648 if (TREE_CODE (fld) != TYPE_DECL
6649 && RECORD_OR_UNION_TYPE_P (fldtype)
6650 && TYPE_ANONYMOUS_P (fldtype))
6652 /* Members of anonymous structs and unions are treated as if
6653 they were members of the containing class. Descend into
6654 the anonymous struct or union and find a flexible array
6655 member or zero-length array among its fields. */
6656 find_flexarrays (fldtype, fmem);
6657 continue;
6660 /* Skip anything that's not a (non-static) data member. */
6661 if (TREE_CODE (fld) != FIELD_DECL)
6662 continue;
6664 /* Skip virtual table pointers. */
6665 if (DECL_ARTIFICIAL (fld))
6666 continue;
6668 if (field_nonempty_p (fld))
6670 /* Remember the first non-static data member. */
6671 if (!fmem->first)
6672 fmem->first = fld;
6674 /* Remember the first non-static data member after the flexible
6675 array member, if one has been found, or the zero-length array
6676 if it has been found. */
6677 if (!fmem->after && fmem->array)
6678 fmem->after = fld;
6681 /* Skip non-arrays. */
6682 if (TREE_CODE (fldtype) != ARRAY_TYPE)
6683 continue;
6685 /* Determine the upper bound of the array if it has one. */
6686 if (TYPE_DOMAIN (fldtype))
6688 if (fmem->array)
6690 /* Make a record of the zero-length array if either one
6691 such field or a flexible array member has been seen to
6692 handle the pathological and unlikely case of multiple
6693 such members. */
6694 if (!fmem->after)
6695 fmem->after = fld;
6697 else if (integer_all_onesp (TYPE_MAX_VALUE (TYPE_DOMAIN (fldtype))))
6698 /* Remember the first zero-length array unless a flexible array
6699 member has already been seen. */
6700 fmem->array = fld;
6702 else
6704 /* Flexible array members have no upper bound. */
6705 if (fmem->array)
6707 /* Replace the zero-length array if it's been stored and
6708 reset the after pointer. */
6709 if (TYPE_DOMAIN (TREE_TYPE (fmem->array)))
6711 fmem->array = fld;
6712 fmem->after = NULL_TREE;
6715 else
6716 fmem->array = fld;
6721 /* Issue diagnostics for invalid flexible array members or zero-length
6722 arrays that are not the last elements of the containing class or its
6723 base classes or that are its sole members. */
6725 static void
6726 diagnose_flexarrays (tree t, const flexmems_t *fmem)
6728 /* Members of anonymous structs and unions are considered to be members
6729 of the containing struct or union. */
6730 if (TYPE_ANONYMOUS_P (t) || !fmem->array)
6731 return;
6733 const char *msg = 0;
6735 if (TYPE_DOMAIN (TREE_TYPE (fmem->array)))
6737 if (fmem->after)
6738 msg = G_("zero-size array member %qD not at end of %q#T");
6739 else if (!fmem->first)
6740 msg = G_("zero-size array member %qD in an otherwise empty %q#T");
6742 if (msg && pedwarn (DECL_SOURCE_LOCATION (fmem->array),
6743 OPT_Wpedantic, msg, fmem->array, t))
6745 inform (location_of (t), "in the definition of %q#T", t);
6747 else
6749 if (fmem->after)
6750 msg = G_("flexible array member %qD not at end of %q#T");
6751 else if (!fmem->first)
6752 msg = G_("flexible array member %qD in an otherwise empty %q#T");
6754 if (msg)
6756 error_at (DECL_SOURCE_LOCATION (fmem->array), msg,
6757 fmem->array, t);
6759 /* In the unlikely event that the member following the flexible
6760 array member is declared in a different class, point to it.
6761 Otherwise it should be obvious. */
6762 if (fmem->after
6763 && (DECL_CONTEXT (fmem->after) != DECL_CONTEXT (fmem->array)))
6764 inform (DECL_SOURCE_LOCATION (fmem->after),
6765 "next member %q#D declared here",
6766 fmem->after);
6768 inform (location_of (t), "in the definition of %q#T", t);
6774 /* Recursively check to make sure that any flexible array or zero-length
6775 array members of class T or its bases are valid (i.e., not the sole
6776 non-static data member of T and, if one exists, that it is the last
6777 non-static data member of T and its base classes. FMEM is expected
6778 to be initially null and is used internally by recursive calls to
6779 the function. Issue the appropriate diagnostics for the array member
6780 that fails the checks. */
6782 static void
6783 check_flexarrays (tree t, flexmems_t *fmem /* = NULL */)
6785 /* Initialize the result of a search for flexible array and zero-length
6786 array members. Avoid doing any work if the most interesting FMEM data
6787 have already been populated. */
6788 flexmems_t flexmems = flexmems_t ();
6789 if (!fmem)
6790 fmem = &flexmems;
6791 else if (fmem->array && fmem->first && fmem->after)
6792 return;
6794 /* Recursively check the primary base class first. */
6795 if (CLASSTYPE_HAS_PRIMARY_BASE_P (t))
6797 tree basetype = BINFO_TYPE (CLASSTYPE_PRIMARY_BINFO (t));
6798 check_flexarrays (basetype, fmem);
6801 /* Recursively check the base classes. */
6802 int nbases = BINFO_N_BASE_BINFOS (TYPE_BINFO (t));
6803 for (int i = 0; i < nbases; ++i)
6805 tree base_binfo = BINFO_BASE_BINFO (TYPE_BINFO (t), i);
6807 /* The primary base class was already checked above. */
6808 if (base_binfo == CLASSTYPE_PRIMARY_BINFO (t))
6809 continue;
6811 /* Virtual base classes are at the end. */
6812 if (BINFO_VIRTUAL_P (base_binfo))
6813 continue;
6815 /* Check the base class. */
6816 check_flexarrays (BINFO_TYPE (base_binfo), fmem);
6819 if (fmem == &flexmems)
6821 /* Check virtual base classes only once per derived class.
6822 I.e., this check is not performed recursively for base
6823 classes. */
6824 int i;
6825 tree base_binfo;
6826 vec<tree, va_gc> *vbases;
6827 for (vbases = CLASSTYPE_VBASECLASSES (t), i = 0;
6828 vec_safe_iterate (vbases, i, &base_binfo); i++)
6830 /* Check the virtual base class. */
6831 tree basetype = TREE_TYPE (base_binfo);
6833 check_flexarrays (basetype, fmem);
6837 /* Search the members of the current (derived) class. */
6838 find_flexarrays (t, fmem);
6840 if (fmem == &flexmems)
6842 /* Issue diagnostics for invalid flexible and zero-length array members
6843 found in base classes or among the members of the current class. */
6844 diagnose_flexarrays (t, fmem);
6848 /* Perform processing required when the definition of T (a class type)
6849 is complete. Diagnose invalid definitions of flexible array members
6850 and zero-size arrays. */
6852 void
6853 finish_struct_1 (tree t)
6855 tree x;
6856 /* A TREE_LIST. The TREE_VALUE of each node is a FUNCTION_DECL. */
6857 tree virtuals = NULL_TREE;
6859 if (COMPLETE_TYPE_P (t))
6861 gcc_assert (MAYBE_CLASS_TYPE_P (t));
6862 error ("redefinition of %q#T", t);
6863 popclass ();
6864 return;
6867 /* If this type was previously laid out as a forward reference,
6868 make sure we lay it out again. */
6869 TYPE_SIZE (t) = NULL_TREE;
6870 CLASSTYPE_PRIMARY_BINFO (t) = NULL_TREE;
6872 /* Make assumptions about the class; we'll reset the flags if
6873 necessary. */
6874 CLASSTYPE_EMPTY_P (t) = 1;
6875 CLASSTYPE_NEARLY_EMPTY_P (t) = 1;
6876 CLASSTYPE_CONTAINS_EMPTY_CLASS_P (t) = 0;
6877 CLASSTYPE_LITERAL_P (t) = true;
6879 /* Do end-of-class semantic processing: checking the validity of the
6880 bases and members and add implicitly generated methods. */
6881 check_bases_and_members (t);
6883 /* Find the key method. */
6884 if (TYPE_CONTAINS_VPTR_P (t))
6886 /* The Itanium C++ ABI permits the key method to be chosen when
6887 the class is defined -- even though the key method so
6888 selected may later turn out to be an inline function. On
6889 some systems (such as ARM Symbian OS) the key method cannot
6890 be determined until the end of the translation unit. On such
6891 systems, we leave CLASSTYPE_KEY_METHOD set to NULL, which
6892 will cause the class to be added to KEYED_CLASSES. Then, in
6893 finish_file we will determine the key method. */
6894 if (targetm.cxx.key_method_may_be_inline ())
6895 determine_key_method (t);
6897 /* If a polymorphic class has no key method, we may emit the vtable
6898 in every translation unit where the class definition appears. If
6899 we're devirtualizing, we can look into the vtable even if we
6900 aren't emitting it. */
6901 if (CLASSTYPE_KEY_METHOD (t) == NULL_TREE)
6902 keyed_classes = tree_cons (NULL_TREE, t, keyed_classes);
6905 /* Layout the class itself. */
6906 layout_class_type (t, &virtuals);
6907 if (CLASSTYPE_AS_BASE (t) != t)
6908 /* We use the base type for trivial assignments, and hence it
6909 needs a mode. */
6910 compute_record_mode (CLASSTYPE_AS_BASE (t));
6912 /* With the layout complete, check for flexible array members and
6913 zero-length arrays that might overlap other members in the final
6914 layout. */
6915 check_flexarrays (t);
6917 virtuals = modify_all_vtables (t, nreverse (virtuals));
6919 /* If necessary, create the primary vtable for this class. */
6920 if (virtuals || TYPE_CONTAINS_VPTR_P (t))
6922 /* We must enter these virtuals into the table. */
6923 if (!CLASSTYPE_HAS_PRIMARY_BASE_P (t))
6924 build_primary_vtable (NULL_TREE, t);
6925 else if (! BINFO_NEW_VTABLE_MARKED (TYPE_BINFO (t)))
6926 /* Here we know enough to change the type of our virtual
6927 function table, but we will wait until later this function. */
6928 build_primary_vtable (CLASSTYPE_PRIMARY_BINFO (t), t);
6930 /* If we're warning about ABI tags, check the types of the new
6931 virtual functions. */
6932 if (warn_abi_tag)
6933 for (tree v = virtuals; v; v = TREE_CHAIN (v))
6934 check_abi_tags (t, TREE_VALUE (v));
6937 if (TYPE_CONTAINS_VPTR_P (t))
6939 int vindex;
6940 tree fn;
6942 if (BINFO_VTABLE (TYPE_BINFO (t)))
6943 gcc_assert (DECL_VIRTUAL_P (BINFO_VTABLE (TYPE_BINFO (t))));
6944 if (!CLASSTYPE_HAS_PRIMARY_BASE_P (t))
6945 gcc_assert (BINFO_VIRTUALS (TYPE_BINFO (t)) == NULL_TREE);
6947 /* Add entries for virtual functions introduced by this class. */
6948 BINFO_VIRTUALS (TYPE_BINFO (t))
6949 = chainon (BINFO_VIRTUALS (TYPE_BINFO (t)), virtuals);
6951 /* Set DECL_VINDEX for all functions declared in this class. */
6952 for (vindex = 0, fn = BINFO_VIRTUALS (TYPE_BINFO (t));
6954 fn = TREE_CHAIN (fn),
6955 vindex += (TARGET_VTABLE_USES_DESCRIPTORS
6956 ? TARGET_VTABLE_USES_DESCRIPTORS : 1))
6958 tree fndecl = BV_FN (fn);
6960 if (DECL_THUNK_P (fndecl))
6961 /* A thunk. We should never be calling this entry directly
6962 from this vtable -- we'd use the entry for the non
6963 thunk base function. */
6964 DECL_VINDEX (fndecl) = NULL_TREE;
6965 else if (TREE_CODE (DECL_VINDEX (fndecl)) != INTEGER_CST)
6966 DECL_VINDEX (fndecl) = build_int_cst (NULL_TREE, vindex);
6970 finish_struct_bits (t);
6971 set_method_tm_attributes (t);
6972 if (flag_openmp || flag_openmp_simd)
6973 finish_omp_declare_simd_methods (t);
6975 /* Complete the rtl for any static member objects of the type we're
6976 working on. */
6977 for (x = TYPE_FIELDS (t); x; x = DECL_CHAIN (x))
6978 if (VAR_P (x) && TREE_STATIC (x)
6979 && TREE_TYPE (x) != error_mark_node
6980 && same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (x)), t))
6981 DECL_MODE (x) = TYPE_MODE (t);
6983 /* Done with FIELDS...now decide whether to sort these for
6984 faster lookups later.
6986 We use a small number because most searches fail (succeeding
6987 ultimately as the search bores through the inheritance
6988 hierarchy), and we want this failure to occur quickly. */
6990 insert_into_classtype_sorted_fields (TYPE_FIELDS (t), t, 8);
6992 /* Complain if one of the field types requires lower visibility. */
6993 constrain_class_visibility (t);
6995 /* Make the rtl for any new vtables we have created, and unmark
6996 the base types we marked. */
6997 finish_vtbls (t);
6999 /* Build the VTT for T. */
7000 build_vtt (t);
7002 /* This warning does not make sense for Java classes, since they
7003 cannot have destructors. */
7004 if (!TYPE_FOR_JAVA (t) && warn_nonvdtor
7005 && TYPE_POLYMORPHIC_P (t) && accessible_nvdtor_p (t)
7006 && !CLASSTYPE_FINAL (t))
7007 warning (OPT_Wnon_virtual_dtor,
7008 "%q#T has virtual functions and accessible"
7009 " non-virtual destructor", t);
7011 complete_vars (t);
7013 if (warn_overloaded_virtual)
7014 warn_hidden (t);
7016 /* Class layout, assignment of virtual table slots, etc., is now
7017 complete. Give the back end a chance to tweak the visibility of
7018 the class or perform any other required target modifications. */
7019 targetm.cxx.adjust_class_at_definition (t);
7021 maybe_suppress_debug_info (t);
7023 if (flag_vtable_verify)
7024 vtv_save_class_info (t);
7026 dump_class_hierarchy (t);
7028 /* Finish debugging output for this type. */
7029 rest_of_type_compilation (t, ! LOCAL_CLASS_P (t));
7031 if (TYPE_TRANSPARENT_AGGR (t))
7033 tree field = first_field (t);
7034 if (field == NULL_TREE || error_operand_p (field))
7036 error ("type transparent %q#T does not have any fields", t);
7037 TYPE_TRANSPARENT_AGGR (t) = 0;
7039 else if (DECL_ARTIFICIAL (field))
7041 if (DECL_FIELD_IS_BASE (field))
7042 error ("type transparent class %qT has base classes", t);
7043 else
7045 gcc_checking_assert (DECL_VIRTUAL_P (field));
7046 error ("type transparent class %qT has virtual functions", t);
7048 TYPE_TRANSPARENT_AGGR (t) = 0;
7050 else if (TYPE_MODE (t) != DECL_MODE (field))
7052 error ("type transparent %q#T cannot be made transparent because "
7053 "the type of the first field has a different ABI from the "
7054 "class overall", t);
7055 TYPE_TRANSPARENT_AGGR (t) = 0;
7060 /* Insert FIELDS into T for the sorted case if the FIELDS count is
7061 equal to THRESHOLD or greater than THRESHOLD. */
7063 static void
7064 insert_into_classtype_sorted_fields (tree fields, tree t, int threshold)
7066 int n_fields = count_fields (fields);
7067 if (n_fields >= threshold)
7069 struct sorted_fields_type *field_vec = sorted_fields_type_new (n_fields);
7070 add_fields_to_record_type (fields, field_vec, 0);
7071 qsort (field_vec->elts, n_fields, sizeof (tree), field_decl_cmp);
7072 CLASSTYPE_SORTED_FIELDS (t) = field_vec;
7076 /* Insert lately defined enum ENUMTYPE into T for the sorted case. */
7078 void
7079 insert_late_enum_def_into_classtype_sorted_fields (tree enumtype, tree t)
7081 struct sorted_fields_type *sorted_fields = CLASSTYPE_SORTED_FIELDS (t);
7082 if (sorted_fields)
7084 int i;
7085 int n_fields
7086 = list_length (TYPE_VALUES (enumtype)) + sorted_fields->len;
7087 struct sorted_fields_type *field_vec = sorted_fields_type_new (n_fields);
7089 for (i = 0; i < sorted_fields->len; ++i)
7090 field_vec->elts[i] = sorted_fields->elts[i];
7092 add_enum_fields_to_record_type (enumtype, field_vec,
7093 sorted_fields->len);
7094 qsort (field_vec->elts, n_fields, sizeof (tree), field_decl_cmp);
7095 CLASSTYPE_SORTED_FIELDS (t) = field_vec;
7099 /* When T was built up, the member declarations were added in reverse
7100 order. Rearrange them to declaration order. */
7102 void
7103 unreverse_member_declarations (tree t)
7105 tree next;
7106 tree prev;
7107 tree x;
7109 /* The following lists are all in reverse order. Put them in
7110 declaration order now. */
7111 TYPE_METHODS (t) = nreverse (TYPE_METHODS (t));
7112 CLASSTYPE_DECL_LIST (t) = nreverse (CLASSTYPE_DECL_LIST (t));
7114 /* Actually, for the TYPE_FIELDS, only the non TYPE_DECLs are in
7115 reverse order, so we can't just use nreverse. */
7116 prev = NULL_TREE;
7117 for (x = TYPE_FIELDS (t);
7118 x && TREE_CODE (x) != TYPE_DECL;
7119 x = next)
7121 next = DECL_CHAIN (x);
7122 DECL_CHAIN (x) = prev;
7123 prev = x;
7125 if (prev)
7127 DECL_CHAIN (TYPE_FIELDS (t)) = x;
7128 if (prev)
7129 TYPE_FIELDS (t) = prev;
7133 tree
7134 finish_struct (tree t, tree attributes)
7136 location_t saved_loc = input_location;
7138 /* Now that we've got all the field declarations, reverse everything
7139 as necessary. */
7140 unreverse_member_declarations (t);
7142 cplus_decl_attributes (&t, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
7143 fixup_attribute_variants (t);
7145 /* Nadger the current location so that diagnostics point to the start of
7146 the struct, not the end. */
7147 input_location = DECL_SOURCE_LOCATION (TYPE_NAME (t));
7149 if (processing_template_decl)
7151 tree x;
7153 finish_struct_methods (t);
7154 TYPE_SIZE (t) = bitsize_zero_node;
7155 TYPE_SIZE_UNIT (t) = size_zero_node;
7157 /* We need to emit an error message if this type was used as a parameter
7158 and it is an abstract type, even if it is a template. We construct
7159 a simple CLASSTYPE_PURE_VIRTUALS list without taking bases into
7160 account and we call complete_vars with this type, which will check
7161 the PARM_DECLS. Note that while the type is being defined,
7162 CLASSTYPE_PURE_VIRTUALS contains the list of the inline friends
7163 (see CLASSTYPE_INLINE_FRIENDS) so we need to clear it. */
7164 CLASSTYPE_PURE_VIRTUALS (t) = NULL;
7165 for (x = TYPE_METHODS (t); x; x = DECL_CHAIN (x))
7166 if (DECL_PURE_VIRTUAL_P (x))
7167 vec_safe_push (CLASSTYPE_PURE_VIRTUALS (t), x);
7168 complete_vars (t);
7169 /* We need to add the target functions to the CLASSTYPE_METHOD_VEC if
7170 an enclosing scope is a template class, so that this function be
7171 found by lookup_fnfields_1 when the using declaration is not
7172 instantiated yet. */
7173 for (x = TYPE_FIELDS (t); x; x = DECL_CHAIN (x))
7174 if (TREE_CODE (x) == USING_DECL)
7176 tree fn = strip_using_decl (x);
7177 if (is_overloaded_fn (fn))
7178 for (; fn; fn = OVL_NEXT (fn))
7179 add_method (t, OVL_CURRENT (fn), x);
7182 /* Remember current #pragma pack value. */
7183 TYPE_PRECISION (t) = maximum_field_alignment;
7185 /* Fix up any variants we've already built. */
7186 for (x = TYPE_NEXT_VARIANT (t); x; x = TYPE_NEXT_VARIANT (x))
7188 TYPE_SIZE (x) = TYPE_SIZE (t);
7189 TYPE_SIZE_UNIT (x) = TYPE_SIZE_UNIT (t);
7190 TYPE_FIELDS (x) = TYPE_FIELDS (t);
7191 TYPE_METHODS (x) = TYPE_METHODS (t);
7194 else
7195 finish_struct_1 (t);
7197 if (is_std_init_list (t))
7199 /* People keep complaining that the compiler crashes on an invalid
7200 definition of initializer_list, so I guess we should explicitly
7201 reject it. What the compiler internals care about is that it's a
7202 template and has a pointer field followed by an integer field. */
7203 bool ok = false;
7204 if (processing_template_decl)
7206 tree f = next_initializable_field (TYPE_FIELDS (t));
7207 if (f && TREE_CODE (TREE_TYPE (f)) == POINTER_TYPE)
7209 f = next_initializable_field (DECL_CHAIN (f));
7210 if (f && same_type_p (TREE_TYPE (f), size_type_node))
7211 ok = true;
7214 if (!ok)
7215 fatal_error (input_location,
7216 "definition of std::initializer_list does not match "
7217 "#include <initializer_list>");
7220 input_location = saved_loc;
7222 TYPE_BEING_DEFINED (t) = 0;
7224 if (current_class_type)
7225 popclass ();
7226 else
7227 error ("trying to finish struct, but kicked out due to previous parse errors");
7229 if (processing_template_decl && at_function_scope_p ()
7230 /* Lambdas are defined by the LAMBDA_EXPR. */
7231 && !LAMBDA_TYPE_P (t))
7232 add_stmt (build_min (TAG_DEFN, t));
7234 return t;
7237 /* Hash table to avoid endless recursion when handling references. */
7238 static hash_table<nofree_ptr_hash<tree_node> > *fixed_type_or_null_ref_ht;
7240 /* Return the dynamic type of INSTANCE, if known.
7241 Used to determine whether the virtual function table is needed
7242 or not.
7244 *NONNULL is set iff INSTANCE can be known to be nonnull, regardless
7245 of our knowledge of its type. *NONNULL should be initialized
7246 before this function is called. */
7248 static tree
7249 fixed_type_or_null (tree instance, int *nonnull, int *cdtorp)
7251 #define RECUR(T) fixed_type_or_null((T), nonnull, cdtorp)
7253 switch (TREE_CODE (instance))
7255 case INDIRECT_REF:
7256 if (POINTER_TYPE_P (TREE_TYPE (instance)))
7257 return NULL_TREE;
7258 else
7259 return RECUR (TREE_OPERAND (instance, 0));
7261 case CALL_EXPR:
7262 /* This is a call to a constructor, hence it's never zero. */
7263 if (TREE_HAS_CONSTRUCTOR (instance))
7265 if (nonnull)
7266 *nonnull = 1;
7267 return TREE_TYPE (instance);
7269 return NULL_TREE;
7271 case SAVE_EXPR:
7272 /* This is a call to a constructor, hence it's never zero. */
7273 if (TREE_HAS_CONSTRUCTOR (instance))
7275 if (nonnull)
7276 *nonnull = 1;
7277 return TREE_TYPE (instance);
7279 return RECUR (TREE_OPERAND (instance, 0));
7281 case POINTER_PLUS_EXPR:
7282 case PLUS_EXPR:
7283 case MINUS_EXPR:
7284 if (TREE_CODE (TREE_OPERAND (instance, 0)) == ADDR_EXPR)
7285 return RECUR (TREE_OPERAND (instance, 0));
7286 if (TREE_CODE (TREE_OPERAND (instance, 1)) == INTEGER_CST)
7287 /* Propagate nonnull. */
7288 return RECUR (TREE_OPERAND (instance, 0));
7290 return NULL_TREE;
7292 CASE_CONVERT:
7293 return RECUR (TREE_OPERAND (instance, 0));
7295 case ADDR_EXPR:
7296 instance = TREE_OPERAND (instance, 0);
7297 if (nonnull)
7299 /* Just because we see an ADDR_EXPR doesn't mean we're dealing
7300 with a real object -- given &p->f, p can still be null. */
7301 tree t = get_base_address (instance);
7302 /* ??? Probably should check DECL_WEAK here. */
7303 if (t && DECL_P (t))
7304 *nonnull = 1;
7306 return RECUR (instance);
7308 case COMPONENT_REF:
7309 /* If this component is really a base class reference, then the field
7310 itself isn't definitive. */
7311 if (DECL_FIELD_IS_BASE (TREE_OPERAND (instance, 1)))
7312 return RECUR (TREE_OPERAND (instance, 0));
7313 return RECUR (TREE_OPERAND (instance, 1));
7315 case VAR_DECL:
7316 case FIELD_DECL:
7317 if (TREE_CODE (TREE_TYPE (instance)) == ARRAY_TYPE
7318 && MAYBE_CLASS_TYPE_P (TREE_TYPE (TREE_TYPE (instance))))
7320 if (nonnull)
7321 *nonnull = 1;
7322 return TREE_TYPE (TREE_TYPE (instance));
7324 /* fall through... */
7325 case TARGET_EXPR:
7326 case PARM_DECL:
7327 case RESULT_DECL:
7328 if (MAYBE_CLASS_TYPE_P (TREE_TYPE (instance)))
7330 if (nonnull)
7331 *nonnull = 1;
7332 return TREE_TYPE (instance);
7334 else if (instance == current_class_ptr)
7336 if (nonnull)
7337 *nonnull = 1;
7339 /* if we're in a ctor or dtor, we know our type. If
7340 current_class_ptr is set but we aren't in a function, we're in
7341 an NSDMI (and therefore a constructor). */
7342 if (current_scope () != current_function_decl
7343 || (DECL_LANG_SPECIFIC (current_function_decl)
7344 && (DECL_CONSTRUCTOR_P (current_function_decl)
7345 || DECL_DESTRUCTOR_P (current_function_decl))))
7347 if (cdtorp)
7348 *cdtorp = 1;
7349 return TREE_TYPE (TREE_TYPE (instance));
7352 else if (TREE_CODE (TREE_TYPE (instance)) == REFERENCE_TYPE)
7354 /* We only need one hash table because it is always left empty. */
7355 if (!fixed_type_or_null_ref_ht)
7356 fixed_type_or_null_ref_ht
7357 = new hash_table<nofree_ptr_hash<tree_node> > (37);
7359 /* Reference variables should be references to objects. */
7360 if (nonnull)
7361 *nonnull = 1;
7363 /* Enter the INSTANCE in a table to prevent recursion; a
7364 variable's initializer may refer to the variable
7365 itself. */
7366 if (VAR_P (instance)
7367 && DECL_INITIAL (instance)
7368 && !type_dependent_expression_p_push (DECL_INITIAL (instance))
7369 && !fixed_type_or_null_ref_ht->find (instance))
7371 tree type;
7372 tree_node **slot;
7374 slot = fixed_type_or_null_ref_ht->find_slot (instance, INSERT);
7375 *slot = instance;
7376 type = RECUR (DECL_INITIAL (instance));
7377 fixed_type_or_null_ref_ht->remove_elt (instance);
7379 return type;
7382 return NULL_TREE;
7384 default:
7385 return NULL_TREE;
7387 #undef RECUR
7390 /* Return nonzero if the dynamic type of INSTANCE is known, and
7391 equivalent to the static type. We also handle the case where
7392 INSTANCE is really a pointer. Return negative if this is a
7393 ctor/dtor. There the dynamic type is known, but this might not be
7394 the most derived base of the original object, and hence virtual
7395 bases may not be laid out according to this type.
7397 Used to determine whether the virtual function table is needed
7398 or not.
7400 *NONNULL is set iff INSTANCE can be known to be nonnull, regardless
7401 of our knowledge of its type. *NONNULL should be initialized
7402 before this function is called. */
7405 resolves_to_fixed_type_p (tree instance, int* nonnull)
7407 tree t = TREE_TYPE (instance);
7408 int cdtorp = 0;
7409 tree fixed;
7411 /* processing_template_decl can be false in a template if we're in
7412 instantiate_non_dependent_expr, but we still want to suppress
7413 this check. */
7414 if (in_template_function ())
7416 /* In a template we only care about the type of the result. */
7417 if (nonnull)
7418 *nonnull = true;
7419 return true;
7422 fixed = fixed_type_or_null (instance, nonnull, &cdtorp);
7423 if (fixed == NULL_TREE)
7424 return 0;
7425 if (POINTER_TYPE_P (t))
7426 t = TREE_TYPE (t);
7427 if (!same_type_ignoring_top_level_qualifiers_p (t, fixed))
7428 return 0;
7429 return cdtorp ? -1 : 1;
7433 void
7434 init_class_processing (void)
7436 current_class_depth = 0;
7437 current_class_stack_size = 10;
7438 current_class_stack
7439 = XNEWVEC (struct class_stack_node, current_class_stack_size);
7440 vec_alloc (local_classes, 8);
7441 sizeof_biggest_empty_class = size_zero_node;
7443 ridpointers[(int) RID_PUBLIC] = access_public_node;
7444 ridpointers[(int) RID_PRIVATE] = access_private_node;
7445 ridpointers[(int) RID_PROTECTED] = access_protected_node;
7448 /* Restore the cached PREVIOUS_CLASS_LEVEL. */
7450 static void
7451 restore_class_cache (void)
7453 tree type;
7455 /* We are re-entering the same class we just left, so we don't
7456 have to search the whole inheritance matrix to find all the
7457 decls to bind again. Instead, we install the cached
7458 class_shadowed list and walk through it binding names. */
7459 push_binding_level (previous_class_level);
7460 class_binding_level = previous_class_level;
7461 /* Restore IDENTIFIER_TYPE_VALUE. */
7462 for (type = class_binding_level->type_shadowed;
7463 type;
7464 type = TREE_CHAIN (type))
7465 SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (type), TREE_TYPE (type));
7468 /* Set global variables CURRENT_CLASS_NAME and CURRENT_CLASS_TYPE as
7469 appropriate for TYPE.
7471 So that we may avoid calls to lookup_name, we cache the _TYPE
7472 nodes of local TYPE_DECLs in the TREE_TYPE field of the name.
7474 For multiple inheritance, we perform a two-pass depth-first search
7475 of the type lattice. */
7477 void
7478 pushclass (tree type)
7480 class_stack_node_t csn;
7482 type = TYPE_MAIN_VARIANT (type);
7484 /* Make sure there is enough room for the new entry on the stack. */
7485 if (current_class_depth + 1 >= current_class_stack_size)
7487 current_class_stack_size *= 2;
7488 current_class_stack
7489 = XRESIZEVEC (struct class_stack_node, current_class_stack,
7490 current_class_stack_size);
7493 /* Insert a new entry on the class stack. */
7494 csn = current_class_stack + current_class_depth;
7495 csn->name = current_class_name;
7496 csn->type = current_class_type;
7497 csn->access = current_access_specifier;
7498 csn->names_used = 0;
7499 csn->hidden = 0;
7500 current_class_depth++;
7502 /* Now set up the new type. */
7503 current_class_name = TYPE_NAME (type);
7504 if (TREE_CODE (current_class_name) == TYPE_DECL)
7505 current_class_name = DECL_NAME (current_class_name);
7506 current_class_type = type;
7508 /* By default, things in classes are private, while things in
7509 structures or unions are public. */
7510 current_access_specifier = (CLASSTYPE_DECLARED_CLASS (type)
7511 ? access_private_node
7512 : access_public_node);
7514 if (previous_class_level
7515 && type != previous_class_level->this_entity
7516 && current_class_depth == 1)
7518 /* Forcibly remove any old class remnants. */
7519 invalidate_class_lookup_cache ();
7522 if (!previous_class_level
7523 || type != previous_class_level->this_entity
7524 || current_class_depth > 1)
7525 pushlevel_class ();
7526 else
7527 restore_class_cache ();
7530 /* When we exit a toplevel class scope, we save its binding level so
7531 that we can restore it quickly. Here, we've entered some other
7532 class, so we must invalidate our cache. */
7534 void
7535 invalidate_class_lookup_cache (void)
7537 previous_class_level = NULL;
7540 /* Get out of the current class scope. If we were in a class scope
7541 previously, that is the one popped to. */
7543 void
7544 popclass (void)
7546 poplevel_class ();
7548 current_class_depth--;
7549 current_class_name = current_class_stack[current_class_depth].name;
7550 current_class_type = current_class_stack[current_class_depth].type;
7551 current_access_specifier = current_class_stack[current_class_depth].access;
7552 if (current_class_stack[current_class_depth].names_used)
7553 splay_tree_delete (current_class_stack[current_class_depth].names_used);
7556 /* Mark the top of the class stack as hidden. */
7558 void
7559 push_class_stack (void)
7561 if (current_class_depth)
7562 ++current_class_stack[current_class_depth - 1].hidden;
7565 /* Mark the top of the class stack as un-hidden. */
7567 void
7568 pop_class_stack (void)
7570 if (current_class_depth)
7571 --current_class_stack[current_class_depth - 1].hidden;
7574 /* Returns 1 if the class type currently being defined is either T or
7575 a nested type of T. Returns the type from the current_class_stack,
7576 which might be equivalent to but not equal to T in case of
7577 constrained partial specializations. */
7579 tree
7580 currently_open_class (tree t)
7582 int i;
7584 if (!CLASS_TYPE_P (t))
7585 return NULL_TREE;
7587 t = TYPE_MAIN_VARIANT (t);
7589 /* We start looking from 1 because entry 0 is from global scope,
7590 and has no type. */
7591 for (i = current_class_depth; i > 0; --i)
7593 tree c;
7594 if (i == current_class_depth)
7595 c = current_class_type;
7596 else
7598 if (current_class_stack[i].hidden)
7599 break;
7600 c = current_class_stack[i].type;
7602 if (!c)
7603 continue;
7604 if (same_type_p (c, t))
7605 return c;
7607 return NULL_TREE;
7610 /* If either current_class_type or one of its enclosing classes are derived
7611 from T, return the appropriate type. Used to determine how we found
7612 something via unqualified lookup. */
7614 tree
7615 currently_open_derived_class (tree t)
7617 int i;
7619 /* The bases of a dependent type are unknown. */
7620 if (dependent_type_p (t))
7621 return NULL_TREE;
7623 if (!current_class_type)
7624 return NULL_TREE;
7626 if (DERIVED_FROM_P (t, current_class_type))
7627 return current_class_type;
7629 for (i = current_class_depth - 1; i > 0; --i)
7631 if (current_class_stack[i].hidden)
7632 break;
7633 if (DERIVED_FROM_P (t, current_class_stack[i].type))
7634 return current_class_stack[i].type;
7637 return NULL_TREE;
7640 /* Return the outermost enclosing class type that is still open, or
7641 NULL_TREE. */
7643 tree
7644 outermost_open_class (void)
7646 if (!current_class_type)
7647 return NULL_TREE;
7648 tree r = NULL_TREE;
7649 if (TYPE_BEING_DEFINED (current_class_type))
7650 r = current_class_type;
7651 for (int i = current_class_depth - 1; i > 0; --i)
7653 if (current_class_stack[i].hidden)
7654 break;
7655 tree t = current_class_stack[i].type;
7656 if (!TYPE_BEING_DEFINED (t))
7657 break;
7658 r = t;
7660 return r;
7663 /* Returns the innermost class type which is not a lambda closure type. */
7665 tree
7666 current_nonlambda_class_type (void)
7668 int i;
7670 /* We start looking from 1 because entry 0 is from global scope,
7671 and has no type. */
7672 for (i = current_class_depth; i > 0; --i)
7674 tree c;
7675 if (i == current_class_depth)
7676 c = current_class_type;
7677 else
7679 if (current_class_stack[i].hidden)
7680 break;
7681 c = current_class_stack[i].type;
7683 if (!c)
7684 continue;
7685 if (!LAMBDA_TYPE_P (c))
7686 return c;
7688 return NULL_TREE;
7691 /* When entering a class scope, all enclosing class scopes' names with
7692 static meaning (static variables, static functions, types and
7693 enumerators) have to be visible. This recursive function calls
7694 pushclass for all enclosing class contexts until global or a local
7695 scope is reached. TYPE is the enclosed class. */
7697 void
7698 push_nested_class (tree type)
7700 /* A namespace might be passed in error cases, like A::B:C. */
7701 if (type == NULL_TREE
7702 || !CLASS_TYPE_P (type))
7703 return;
7705 push_nested_class (DECL_CONTEXT (TYPE_MAIN_DECL (type)));
7707 pushclass (type);
7710 /* Undoes a push_nested_class call. */
7712 void
7713 pop_nested_class (void)
7715 tree context = DECL_CONTEXT (TYPE_MAIN_DECL (current_class_type));
7717 popclass ();
7718 if (context && CLASS_TYPE_P (context))
7719 pop_nested_class ();
7722 /* Returns the number of extern "LANG" blocks we are nested within. */
7725 current_lang_depth (void)
7727 return vec_safe_length (current_lang_base);
7730 /* Set global variables CURRENT_LANG_NAME to appropriate value
7731 so that behavior of name-mangling machinery is correct. */
7733 void
7734 push_lang_context (tree name)
7736 vec_safe_push (current_lang_base, current_lang_name);
7738 if (name == lang_name_cplusplus)
7740 current_lang_name = name;
7742 else if (name == lang_name_java)
7744 current_lang_name = name;
7745 /* DECL_IGNORED_P is initially set for these types, to avoid clutter.
7746 (See record_builtin_java_type in decl.c.) However, that causes
7747 incorrect debug entries if these types are actually used.
7748 So we re-enable debug output after extern "Java". */
7749 DECL_IGNORED_P (TYPE_NAME (java_byte_type_node)) = 0;
7750 DECL_IGNORED_P (TYPE_NAME (java_short_type_node)) = 0;
7751 DECL_IGNORED_P (TYPE_NAME (java_int_type_node)) = 0;
7752 DECL_IGNORED_P (TYPE_NAME (java_long_type_node)) = 0;
7753 DECL_IGNORED_P (TYPE_NAME (java_float_type_node)) = 0;
7754 DECL_IGNORED_P (TYPE_NAME (java_double_type_node)) = 0;
7755 DECL_IGNORED_P (TYPE_NAME (java_char_type_node)) = 0;
7756 DECL_IGNORED_P (TYPE_NAME (java_boolean_type_node)) = 0;
7758 else if (name == lang_name_c)
7760 current_lang_name = name;
7762 else
7763 error ("language string %<\"%E\"%> not recognized", name);
7766 /* Get out of the current language scope. */
7768 void
7769 pop_lang_context (void)
7771 current_lang_name = current_lang_base->pop ();
7774 /* Type instantiation routines. */
7776 /* Given an OVERLOAD and a TARGET_TYPE, return the function that
7777 matches the TARGET_TYPE. If there is no satisfactory match, return
7778 error_mark_node, and issue an error & warning messages under
7779 control of FLAGS. Permit pointers to member function if FLAGS
7780 permits. If TEMPLATE_ONLY, the name of the overloaded function was
7781 a template-id, and EXPLICIT_TARGS are the explicitly provided
7782 template arguments.
7784 If OVERLOAD is for one or more member functions, then ACCESS_PATH
7785 is the base path used to reference those member functions. If
7786 the address is resolved to a member function, access checks will be
7787 performed and errors issued if appropriate. */
7789 static tree
7790 resolve_address_of_overloaded_function (tree target_type,
7791 tree overload,
7792 tsubst_flags_t complain,
7793 bool template_only,
7794 tree explicit_targs,
7795 tree access_path)
7797 /* Here's what the standard says:
7799 [over.over]
7801 If the name is a function template, template argument deduction
7802 is done, and if the argument deduction succeeds, the deduced
7803 arguments are used to generate a single template function, which
7804 is added to the set of overloaded functions considered.
7806 Non-member functions and static member functions match targets of
7807 type "pointer-to-function" or "reference-to-function." Nonstatic
7808 member functions match targets of type "pointer-to-member
7809 function;" the function type of the pointer to member is used to
7810 select the member function from the set of overloaded member
7811 functions. If a nonstatic member function is selected, the
7812 reference to the overloaded function name is required to have the
7813 form of a pointer to member as described in 5.3.1.
7815 If more than one function is selected, any template functions in
7816 the set are eliminated if the set also contains a non-template
7817 function, and any given template function is eliminated if the
7818 set contains a second template function that is more specialized
7819 than the first according to the partial ordering rules 14.5.5.2.
7820 After such eliminations, if any, there shall remain exactly one
7821 selected function. */
7823 int is_ptrmem = 0;
7824 /* We store the matches in a TREE_LIST rooted here. The functions
7825 are the TREE_PURPOSE, not the TREE_VALUE, in this list, for easy
7826 interoperability with most_specialized_instantiation. */
7827 tree matches = NULL_TREE;
7828 tree fn;
7829 tree target_fn_type;
7831 /* By the time we get here, we should be seeing only real
7832 pointer-to-member types, not the internal POINTER_TYPE to
7833 METHOD_TYPE representation. */
7834 gcc_assert (!TYPE_PTR_P (target_type)
7835 || TREE_CODE (TREE_TYPE (target_type)) != METHOD_TYPE);
7837 gcc_assert (is_overloaded_fn (overload));
7839 /* Check that the TARGET_TYPE is reasonable. */
7840 if (TYPE_PTRFN_P (target_type)
7841 || TYPE_REFFN_P (target_type))
7842 /* This is OK. */;
7843 else if (TYPE_PTRMEMFUNC_P (target_type))
7844 /* This is OK, too. */
7845 is_ptrmem = 1;
7846 else if (TREE_CODE (target_type) == FUNCTION_TYPE)
7847 /* This is OK, too. This comes from a conversion to reference
7848 type. */
7849 target_type = build_reference_type (target_type);
7850 else
7852 if (complain & tf_error)
7853 error ("cannot resolve overloaded function %qD based on"
7854 " conversion to type %qT",
7855 DECL_NAME (OVL_FUNCTION (overload)), target_type);
7856 return error_mark_node;
7859 /* Non-member functions and static member functions match targets of type
7860 "pointer-to-function" or "reference-to-function." Nonstatic member
7861 functions match targets of type "pointer-to-member-function;" the
7862 function type of the pointer to member is used to select the member
7863 function from the set of overloaded member functions.
7865 So figure out the FUNCTION_TYPE that we want to match against. */
7866 target_fn_type = static_fn_type (target_type);
7868 /* If we can find a non-template function that matches, we can just
7869 use it. There's no point in generating template instantiations
7870 if we're just going to throw them out anyhow. But, of course, we
7871 can only do this when we don't *need* a template function. */
7872 if (!template_only)
7874 tree fns;
7876 for (fns = overload; fns; fns = OVL_NEXT (fns))
7878 tree fn = OVL_CURRENT (fns);
7880 if (TREE_CODE (fn) == TEMPLATE_DECL)
7881 /* We're not looking for templates just yet. */
7882 continue;
7884 if ((TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
7885 != is_ptrmem)
7886 /* We're looking for a non-static member, and this isn't
7887 one, or vice versa. */
7888 continue;
7890 /* Ignore functions which haven't been explicitly
7891 declared. */
7892 if (DECL_ANTICIPATED (fn))
7893 continue;
7895 /* See if there's a match. */
7896 tree fntype = static_fn_type (fn);
7897 if (same_type_p (target_fn_type, fntype)
7898 || can_convert_tx_safety (target_fn_type, fntype))
7899 matches = tree_cons (fn, NULL_TREE, matches);
7903 /* Now, if we've already got a match (or matches), there's no need
7904 to proceed to the template functions. But, if we don't have a
7905 match we need to look at them, too. */
7906 if (!matches)
7908 tree target_arg_types;
7909 tree target_ret_type;
7910 tree fns;
7911 tree *args;
7912 unsigned int nargs, ia;
7913 tree arg;
7915 target_arg_types = TYPE_ARG_TYPES (target_fn_type);
7916 target_ret_type = TREE_TYPE (target_fn_type);
7918 nargs = list_length (target_arg_types);
7919 args = XALLOCAVEC (tree, nargs);
7920 for (arg = target_arg_types, ia = 0;
7921 arg != NULL_TREE && arg != void_list_node;
7922 arg = TREE_CHAIN (arg), ++ia)
7923 args[ia] = TREE_VALUE (arg);
7924 nargs = ia;
7926 for (fns = overload; fns; fns = OVL_NEXT (fns))
7928 tree fn = OVL_CURRENT (fns);
7929 tree instantiation;
7930 tree targs;
7932 if (TREE_CODE (fn) != TEMPLATE_DECL)
7933 /* We're only looking for templates. */
7934 continue;
7936 if ((TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
7937 != is_ptrmem)
7938 /* We're not looking for a non-static member, and this is
7939 one, or vice versa. */
7940 continue;
7942 tree ret = target_ret_type;
7944 /* If the template has a deduced return type, don't expose it to
7945 template argument deduction. */
7946 if (undeduced_auto_decl (fn))
7947 ret = NULL_TREE;
7949 /* Try to do argument deduction. */
7950 targs = make_tree_vec (DECL_NTPARMS (fn));
7951 instantiation = fn_type_unification (fn, explicit_targs, targs, args,
7952 nargs, ret,
7953 DEDUCE_EXACT, LOOKUP_NORMAL,
7954 false, false);
7955 if (instantiation == error_mark_node)
7956 /* Instantiation failed. */
7957 continue;
7959 /* Constraints must be satisfied. This is done before
7960 return type deduction since that instantiates the
7961 function. */
7962 if (flag_concepts && !constraints_satisfied_p (instantiation))
7963 continue;
7965 /* And now force instantiation to do return type deduction. */
7966 if (undeduced_auto_decl (instantiation))
7968 ++function_depth;
7969 instantiate_decl (instantiation, /*defer*/false, /*class*/false);
7970 --function_depth;
7972 require_deduced_type (instantiation);
7975 /* See if there's a match. */
7976 tree fntype = static_fn_type (instantiation);
7977 if (same_type_p (target_fn_type, fntype)
7978 || can_convert_tx_safety (target_fn_type, fntype))
7979 matches = tree_cons (instantiation, fn, matches);
7982 /* Now, remove all but the most specialized of the matches. */
7983 if (matches)
7985 tree match = most_specialized_instantiation (matches);
7987 if (match != error_mark_node)
7988 matches = tree_cons (TREE_PURPOSE (match),
7989 NULL_TREE,
7990 NULL_TREE);
7994 /* Now we should have exactly one function in MATCHES. */
7995 if (matches == NULL_TREE)
7997 /* There were *no* matches. */
7998 if (complain & tf_error)
8000 error ("no matches converting function %qD to type %q#T",
8001 DECL_NAME (OVL_CURRENT (overload)),
8002 target_type);
8004 print_candidates (overload);
8006 return error_mark_node;
8008 else if (TREE_CHAIN (matches))
8010 /* There were too many matches. First check if they're all
8011 the same function. */
8012 tree match = NULL_TREE;
8014 fn = TREE_PURPOSE (matches);
8016 /* For multi-versioned functions, more than one match is just fine and
8017 decls_match will return false as they are different. */
8018 for (match = TREE_CHAIN (matches); match; match = TREE_CHAIN (match))
8019 if (!decls_match (fn, TREE_PURPOSE (match))
8020 && !targetm.target_option.function_versions
8021 (fn, TREE_PURPOSE (match)))
8022 break;
8024 if (match)
8026 if (complain & tf_error)
8028 error ("converting overloaded function %qD to type %q#T is ambiguous",
8029 DECL_NAME (OVL_FUNCTION (overload)),
8030 target_type);
8032 /* Since print_candidates expects the functions in the
8033 TREE_VALUE slot, we flip them here. */
8034 for (match = matches; match; match = TREE_CHAIN (match))
8035 TREE_VALUE (match) = TREE_PURPOSE (match);
8037 print_candidates (matches);
8040 return error_mark_node;
8044 /* Good, exactly one match. Now, convert it to the correct type. */
8045 fn = TREE_PURPOSE (matches);
8047 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
8048 && !(complain & tf_ptrmem_ok) && !flag_ms_extensions)
8050 static int explained;
8052 if (!(complain & tf_error))
8053 return error_mark_node;
8055 permerror (input_location, "assuming pointer to member %qD", fn);
8056 if (!explained)
8058 inform (input_location, "(a pointer to member can only be formed with %<&%E%>)", fn);
8059 explained = 1;
8063 /* If a pointer to a function that is multi-versioned is requested, the
8064 pointer to the dispatcher function is returned instead. This works
8065 well because indirectly calling the function will dispatch the right
8066 function version at run-time. */
8067 if (DECL_FUNCTION_VERSIONED (fn))
8069 fn = get_function_version_dispatcher (fn);
8070 if (fn == NULL)
8071 return error_mark_node;
8072 /* Mark all the versions corresponding to the dispatcher as used. */
8073 if (!(complain & tf_conv))
8074 mark_versions_used (fn);
8077 /* If we're doing overload resolution purely for the purpose of
8078 determining conversion sequences, we should not consider the
8079 function used. If this conversion sequence is selected, the
8080 function will be marked as used at this point. */
8081 if (!(complain & tf_conv))
8083 /* Make =delete work with SFINAE. */
8084 if (DECL_DELETED_FN (fn) && !(complain & tf_error))
8085 return error_mark_node;
8086 if (!mark_used (fn, complain) && !(complain & tf_error))
8087 return error_mark_node;
8090 /* We could not check access to member functions when this
8091 expression was originally created since we did not know at that
8092 time to which function the expression referred. */
8093 if (DECL_FUNCTION_MEMBER_P (fn))
8095 gcc_assert (access_path);
8096 perform_or_defer_access_check (access_path, fn, fn, complain);
8099 if (TYPE_PTRFN_P (target_type) || TYPE_PTRMEMFUNC_P (target_type))
8100 return cp_build_addr_expr (fn, complain);
8101 else
8103 /* The target must be a REFERENCE_TYPE. Above, cp_build_unary_op
8104 will mark the function as addressed, but here we must do it
8105 explicitly. */
8106 cxx_mark_addressable (fn);
8108 return fn;
8112 /* This function will instantiate the type of the expression given in
8113 RHS to match the type of LHSTYPE. If errors exist, then return
8114 error_mark_node. COMPLAIN is a bit mask. If TF_ERROR is set, then
8115 we complain on errors. If we are not complaining, never modify rhs,
8116 as overload resolution wants to try many possible instantiations, in
8117 the hope that at least one will work.
8119 For non-recursive calls, LHSTYPE should be a function, pointer to
8120 function, or a pointer to member function. */
8122 tree
8123 instantiate_type (tree lhstype, tree rhs, tsubst_flags_t complain)
8125 tsubst_flags_t complain_in = complain;
8126 tree access_path = NULL_TREE;
8128 complain &= ~tf_ptrmem_ok;
8130 if (lhstype == unknown_type_node)
8132 if (complain & tf_error)
8133 error ("not enough type information");
8134 return error_mark_node;
8137 if (TREE_TYPE (rhs) != NULL_TREE && ! (type_unknown_p (rhs)))
8139 tree fntype = non_reference (lhstype);
8140 if (same_type_p (fntype, TREE_TYPE (rhs)))
8141 return rhs;
8142 if (flag_ms_extensions
8143 && TYPE_PTRMEMFUNC_P (fntype)
8144 && !TYPE_PTRMEMFUNC_P (TREE_TYPE (rhs)))
8145 /* Microsoft allows `A::f' to be resolved to a
8146 pointer-to-member. */
8148 else
8150 if (complain & tf_error)
8151 error ("cannot convert %qE from type %qT to type %qT",
8152 rhs, TREE_TYPE (rhs), fntype);
8153 return error_mark_node;
8157 if (BASELINK_P (rhs))
8159 access_path = BASELINK_ACCESS_BINFO (rhs);
8160 rhs = BASELINK_FUNCTIONS (rhs);
8163 /* If we are in a template, and have a NON_DEPENDENT_EXPR, we cannot
8164 deduce any type information. */
8165 if (TREE_CODE (rhs) == NON_DEPENDENT_EXPR)
8167 if (complain & tf_error)
8168 error ("not enough type information");
8169 return error_mark_node;
8172 /* There only a few kinds of expressions that may have a type
8173 dependent on overload resolution. */
8174 gcc_assert (TREE_CODE (rhs) == ADDR_EXPR
8175 || TREE_CODE (rhs) == COMPONENT_REF
8176 || is_overloaded_fn (rhs)
8177 || (flag_ms_extensions && TREE_CODE (rhs) == FUNCTION_DECL));
8179 /* This should really only be used when attempting to distinguish
8180 what sort of a pointer to function we have. For now, any
8181 arithmetic operation which is not supported on pointers
8182 is rejected as an error. */
8184 switch (TREE_CODE (rhs))
8186 case COMPONENT_REF:
8188 tree member = TREE_OPERAND (rhs, 1);
8190 member = instantiate_type (lhstype, member, complain);
8191 if (member != error_mark_node
8192 && TREE_SIDE_EFFECTS (TREE_OPERAND (rhs, 0)))
8193 /* Do not lose object's side effects. */
8194 return build2 (COMPOUND_EXPR, TREE_TYPE (member),
8195 TREE_OPERAND (rhs, 0), member);
8196 return member;
8199 case OFFSET_REF:
8200 rhs = TREE_OPERAND (rhs, 1);
8201 if (BASELINK_P (rhs))
8202 return instantiate_type (lhstype, rhs, complain_in);
8204 /* This can happen if we are forming a pointer-to-member for a
8205 member template. */
8206 gcc_assert (TREE_CODE (rhs) == TEMPLATE_ID_EXPR);
8208 /* Fall through. */
8210 case TEMPLATE_ID_EXPR:
8212 tree fns = TREE_OPERAND (rhs, 0);
8213 tree args = TREE_OPERAND (rhs, 1);
8215 return
8216 resolve_address_of_overloaded_function (lhstype, fns, complain_in,
8217 /*template_only=*/true,
8218 args, access_path);
8221 case OVERLOAD:
8222 case FUNCTION_DECL:
8223 return
8224 resolve_address_of_overloaded_function (lhstype, rhs, complain_in,
8225 /*template_only=*/false,
8226 /*explicit_targs=*/NULL_TREE,
8227 access_path);
8229 case ADDR_EXPR:
8231 if (PTRMEM_OK_P (rhs))
8232 complain |= tf_ptrmem_ok;
8234 return instantiate_type (lhstype, TREE_OPERAND (rhs, 0), complain);
8237 case ERROR_MARK:
8238 return error_mark_node;
8240 default:
8241 gcc_unreachable ();
8243 return error_mark_node;
8246 /* Return the name of the virtual function pointer field
8247 (as an IDENTIFIER_NODE) for the given TYPE. Note that
8248 this may have to look back through base types to find the
8249 ultimate field name. (For single inheritance, these could
8250 all be the same name. Who knows for multiple inheritance). */
8252 static tree
8253 get_vfield_name (tree type)
8255 tree binfo, base_binfo;
8256 char *buf;
8258 for (binfo = TYPE_BINFO (type);
8259 BINFO_N_BASE_BINFOS (binfo);
8260 binfo = base_binfo)
8262 base_binfo = BINFO_BASE_BINFO (binfo, 0);
8264 if (BINFO_VIRTUAL_P (base_binfo)
8265 || !TYPE_CONTAINS_VPTR_P (BINFO_TYPE (base_binfo)))
8266 break;
8269 type = BINFO_TYPE (binfo);
8270 buf = (char *) alloca (sizeof (VFIELD_NAME_FORMAT)
8271 + TYPE_NAME_LENGTH (type) + 2);
8272 sprintf (buf, VFIELD_NAME_FORMAT,
8273 IDENTIFIER_POINTER (constructor_name (type)));
8274 return get_identifier (buf);
8277 void
8278 print_class_statistics (void)
8280 if (! GATHER_STATISTICS)
8281 return;
8283 fprintf (stderr, "convert_harshness = %d\n", n_convert_harshness);
8284 fprintf (stderr, "compute_conversion_costs = %d\n", n_compute_conversion_costs);
8285 if (n_vtables)
8287 fprintf (stderr, "vtables = %d; vtable searches = %d\n",
8288 n_vtables, n_vtable_searches);
8289 fprintf (stderr, "vtable entries = %d; vtable elems = %d\n",
8290 n_vtable_entries, n_vtable_elems);
8294 /* Build a dummy reference to ourselves so Derived::Base (and A::A) works,
8295 according to [class]:
8296 The class-name is also inserted
8297 into the scope of the class itself. For purposes of access checking,
8298 the inserted class name is treated as if it were a public member name. */
8300 void
8301 build_self_reference (void)
8303 tree name = constructor_name (current_class_type);
8304 tree value = build_lang_decl (TYPE_DECL, name, current_class_type);
8305 tree saved_cas;
8307 DECL_NONLOCAL (value) = 1;
8308 DECL_CONTEXT (value) = current_class_type;
8309 DECL_ARTIFICIAL (value) = 1;
8310 SET_DECL_SELF_REFERENCE_P (value);
8311 set_underlying_type (value);
8313 if (processing_template_decl)
8314 value = push_template_decl (value);
8316 saved_cas = current_access_specifier;
8317 current_access_specifier = access_public_node;
8318 finish_member_declaration (value);
8319 current_access_specifier = saved_cas;
8322 /* Returns 1 if TYPE contains only padding bytes. */
8325 is_empty_class (tree type)
8327 if (type == error_mark_node)
8328 return 0;
8330 if (! CLASS_TYPE_P (type))
8331 return 0;
8333 return CLASSTYPE_EMPTY_P (type);
8336 /* Returns true if TYPE contains no actual data, just various
8337 possible combinations of empty classes and possibly a vptr. */
8339 bool
8340 is_really_empty_class (tree type)
8342 if (CLASS_TYPE_P (type))
8344 tree field;
8345 tree binfo;
8346 tree base_binfo;
8347 int i;
8349 /* CLASSTYPE_EMPTY_P isn't set properly until the class is actually laid
8350 out, but we'd like to be able to check this before then. */
8351 if (COMPLETE_TYPE_P (type) && is_empty_class (type))
8352 return true;
8354 for (binfo = TYPE_BINFO (type), i = 0;
8355 BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
8356 if (!is_really_empty_class (BINFO_TYPE (base_binfo)))
8357 return false;
8358 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
8359 if (TREE_CODE (field) == FIELD_DECL
8360 && !DECL_ARTIFICIAL (field)
8361 && !is_really_empty_class (TREE_TYPE (field)))
8362 return false;
8363 return true;
8365 else if (TREE_CODE (type) == ARRAY_TYPE)
8366 return is_really_empty_class (TREE_TYPE (type));
8367 return false;
8370 /* Note that NAME was looked up while the current class was being
8371 defined and that the result of that lookup was DECL. */
8373 void
8374 maybe_note_name_used_in_class (tree name, tree decl)
8376 splay_tree names_used;
8378 /* If we're not defining a class, there's nothing to do. */
8379 if (!(innermost_scope_kind() == sk_class
8380 && TYPE_BEING_DEFINED (current_class_type)
8381 && !LAMBDA_TYPE_P (current_class_type)))
8382 return;
8384 /* If there's already a binding for this NAME, then we don't have
8385 anything to worry about. */
8386 if (lookup_member (current_class_type, name,
8387 /*protect=*/0, /*want_type=*/false, tf_warning_or_error))
8388 return;
8390 if (!current_class_stack[current_class_depth - 1].names_used)
8391 current_class_stack[current_class_depth - 1].names_used
8392 = splay_tree_new (splay_tree_compare_pointers, 0, 0);
8393 names_used = current_class_stack[current_class_depth - 1].names_used;
8395 splay_tree_insert (names_used,
8396 (splay_tree_key) name,
8397 (splay_tree_value) decl);
8400 /* Note that NAME was declared (as DECL) in the current class. Check
8401 to see that the declaration is valid. */
8403 void
8404 note_name_declared_in_class (tree name, tree decl)
8406 splay_tree names_used;
8407 splay_tree_node n;
8409 /* Look to see if we ever used this name. */
8410 names_used
8411 = current_class_stack[current_class_depth - 1].names_used;
8412 if (!names_used)
8413 return;
8414 /* The C language allows members to be declared with a type of the same
8415 name, and the C++ standard says this diagnostic is not required. So
8416 allow it in extern "C" blocks unless predantic is specified.
8417 Allow it in all cases if -ms-extensions is specified. */
8418 if ((!pedantic && current_lang_name == lang_name_c)
8419 || flag_ms_extensions)
8420 return;
8421 n = splay_tree_lookup (names_used, (splay_tree_key) name);
8422 if (n)
8424 /* [basic.scope.class]
8426 A name N used in a class S shall refer to the same declaration
8427 in its context and when re-evaluated in the completed scope of
8428 S. */
8429 permerror (input_location, "declaration of %q#D", decl);
8430 permerror (location_of ((tree) n->value),
8431 "changes meaning of %qD from %q#D",
8432 DECL_NAME (OVL_CURRENT (decl)), (tree) n->value);
8436 /* Returns the VAR_DECL for the complete vtable associated with BINFO.
8437 Secondary vtables are merged with primary vtables; this function
8438 will return the VAR_DECL for the primary vtable. */
8440 tree
8441 get_vtbl_decl_for_binfo (tree binfo)
8443 tree decl;
8445 decl = BINFO_VTABLE (binfo);
8446 if (decl && TREE_CODE (decl) == POINTER_PLUS_EXPR)
8448 gcc_assert (TREE_CODE (TREE_OPERAND (decl, 0)) == ADDR_EXPR);
8449 decl = TREE_OPERAND (TREE_OPERAND (decl, 0), 0);
8451 if (decl)
8452 gcc_assert (VAR_P (decl));
8453 return decl;
8457 /* Returns the binfo for the primary base of BINFO. If the resulting
8458 BINFO is a virtual base, and it is inherited elsewhere in the
8459 hierarchy, then the returned binfo might not be the primary base of
8460 BINFO in the complete object. Check BINFO_PRIMARY_P or
8461 BINFO_LOST_PRIMARY_P to be sure. */
8463 static tree
8464 get_primary_binfo (tree binfo)
8466 tree primary_base;
8468 primary_base = CLASSTYPE_PRIMARY_BINFO (BINFO_TYPE (binfo));
8469 if (!primary_base)
8470 return NULL_TREE;
8472 return copied_binfo (primary_base, binfo);
8475 /* If INDENTED_P is zero, indent to INDENT. Return nonzero. */
8477 static int
8478 maybe_indent_hierarchy (FILE * stream, int indent, int indented_p)
8480 if (!indented_p)
8481 fprintf (stream, "%*s", indent, "");
8482 return 1;
8485 /* Dump the offsets of all the bases rooted at BINFO to STREAM.
8486 INDENT should be zero when called from the top level; it is
8487 incremented recursively. IGO indicates the next expected BINFO in
8488 inheritance graph ordering. */
8490 static tree
8491 dump_class_hierarchy_r (FILE *stream,
8492 int flags,
8493 tree binfo,
8494 tree igo,
8495 int indent)
8497 int indented = 0;
8498 tree base_binfo;
8499 int i;
8501 indented = maybe_indent_hierarchy (stream, indent, 0);
8502 fprintf (stream, "%s (0x" HOST_WIDE_INT_PRINT_HEX ") ",
8503 type_as_string (BINFO_TYPE (binfo), TFF_PLAIN_IDENTIFIER),
8504 (HOST_WIDE_INT) (uintptr_t) binfo);
8505 if (binfo != igo)
8507 fprintf (stream, "alternative-path\n");
8508 return igo;
8510 igo = TREE_CHAIN (binfo);
8512 fprintf (stream, HOST_WIDE_INT_PRINT_DEC,
8513 tree_to_shwi (BINFO_OFFSET (binfo)));
8514 if (is_empty_class (BINFO_TYPE (binfo)))
8515 fprintf (stream, " empty");
8516 else if (CLASSTYPE_NEARLY_EMPTY_P (BINFO_TYPE (binfo)))
8517 fprintf (stream, " nearly-empty");
8518 if (BINFO_VIRTUAL_P (binfo))
8519 fprintf (stream, " virtual");
8520 fprintf (stream, "\n");
8522 indented = 0;
8523 if (BINFO_PRIMARY_P (binfo))
8525 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
8526 fprintf (stream, " primary-for %s (0x" HOST_WIDE_INT_PRINT_HEX ")",
8527 type_as_string (BINFO_TYPE (BINFO_INHERITANCE_CHAIN (binfo)),
8528 TFF_PLAIN_IDENTIFIER),
8529 (HOST_WIDE_INT) (uintptr_t) BINFO_INHERITANCE_CHAIN (binfo));
8531 if (BINFO_LOST_PRIMARY_P (binfo))
8533 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
8534 fprintf (stream, " lost-primary");
8536 if (indented)
8537 fprintf (stream, "\n");
8539 if (!(flags & TDF_SLIM))
8541 int indented = 0;
8543 if (BINFO_SUBVTT_INDEX (binfo))
8545 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
8546 fprintf (stream, " subvttidx=%s",
8547 expr_as_string (BINFO_SUBVTT_INDEX (binfo),
8548 TFF_PLAIN_IDENTIFIER));
8550 if (BINFO_VPTR_INDEX (binfo))
8552 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
8553 fprintf (stream, " vptridx=%s",
8554 expr_as_string (BINFO_VPTR_INDEX (binfo),
8555 TFF_PLAIN_IDENTIFIER));
8557 if (BINFO_VPTR_FIELD (binfo))
8559 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
8560 fprintf (stream, " vbaseoffset=%s",
8561 expr_as_string (BINFO_VPTR_FIELD (binfo),
8562 TFF_PLAIN_IDENTIFIER));
8564 if (BINFO_VTABLE (binfo))
8566 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
8567 fprintf (stream, " vptr=%s",
8568 expr_as_string (BINFO_VTABLE (binfo),
8569 TFF_PLAIN_IDENTIFIER));
8572 if (indented)
8573 fprintf (stream, "\n");
8576 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
8577 igo = dump_class_hierarchy_r (stream, flags, base_binfo, igo, indent + 2);
8579 return igo;
8582 /* Dump the BINFO hierarchy for T. */
8584 static void
8585 dump_class_hierarchy_1 (FILE *stream, int flags, tree t)
8587 fprintf (stream, "Class %s\n", type_as_string (t, TFF_PLAIN_IDENTIFIER));
8588 fprintf (stream, " size=%lu align=%lu\n",
8589 (unsigned long)(tree_to_shwi (TYPE_SIZE (t)) / BITS_PER_UNIT),
8590 (unsigned long)(TYPE_ALIGN (t) / BITS_PER_UNIT));
8591 fprintf (stream, " base size=%lu base align=%lu\n",
8592 (unsigned long)(tree_to_shwi (TYPE_SIZE (CLASSTYPE_AS_BASE (t)))
8593 / BITS_PER_UNIT),
8594 (unsigned long)(TYPE_ALIGN (CLASSTYPE_AS_BASE (t))
8595 / BITS_PER_UNIT));
8596 dump_class_hierarchy_r (stream, flags, TYPE_BINFO (t), TYPE_BINFO (t), 0);
8597 fprintf (stream, "\n");
8600 /* Debug interface to hierarchy dumping. */
8602 void
8603 debug_class (tree t)
8605 dump_class_hierarchy_1 (stderr, TDF_SLIM, t);
8608 static void
8609 dump_class_hierarchy (tree t)
8611 int flags;
8612 FILE *stream = get_dump_info (TDI_class, &flags);
8614 if (stream)
8616 dump_class_hierarchy_1 (stream, flags, t);
8620 static void
8621 dump_array (FILE * stream, tree decl)
8623 tree value;
8624 unsigned HOST_WIDE_INT ix;
8625 HOST_WIDE_INT elt;
8626 tree size = TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (decl)));
8628 elt = (tree_to_shwi (TYPE_SIZE (TREE_TYPE (TREE_TYPE (decl))))
8629 / BITS_PER_UNIT);
8630 fprintf (stream, "%s:", decl_as_string (decl, TFF_PLAIN_IDENTIFIER));
8631 fprintf (stream, " %s entries",
8632 expr_as_string (size_binop (PLUS_EXPR, size, size_one_node),
8633 TFF_PLAIN_IDENTIFIER));
8634 fprintf (stream, "\n");
8636 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (DECL_INITIAL (decl)),
8637 ix, value)
8638 fprintf (stream, "%-4ld %s\n", (long)(ix * elt),
8639 expr_as_string (value, TFF_PLAIN_IDENTIFIER));
8642 static void
8643 dump_vtable (tree t, tree binfo, tree vtable)
8645 int flags;
8646 FILE *stream = get_dump_info (TDI_class, &flags);
8648 if (!stream)
8649 return;
8651 if (!(flags & TDF_SLIM))
8653 int ctor_vtbl_p = TYPE_BINFO (t) != binfo;
8655 fprintf (stream, "%s for %s",
8656 ctor_vtbl_p ? "Construction vtable" : "Vtable",
8657 type_as_string (BINFO_TYPE (binfo), TFF_PLAIN_IDENTIFIER));
8658 if (ctor_vtbl_p)
8660 if (!BINFO_VIRTUAL_P (binfo))
8661 fprintf (stream, " (0x" HOST_WIDE_INT_PRINT_HEX " instance)",
8662 (HOST_WIDE_INT) (uintptr_t) binfo);
8663 fprintf (stream, " in %s", type_as_string (t, TFF_PLAIN_IDENTIFIER));
8665 fprintf (stream, "\n");
8666 dump_array (stream, vtable);
8667 fprintf (stream, "\n");
8671 static void
8672 dump_vtt (tree t, tree vtt)
8674 int flags;
8675 FILE *stream = get_dump_info (TDI_class, &flags);
8677 if (!stream)
8678 return;
8680 if (!(flags & TDF_SLIM))
8682 fprintf (stream, "VTT for %s\n",
8683 type_as_string (t, TFF_PLAIN_IDENTIFIER));
8684 dump_array (stream, vtt);
8685 fprintf (stream, "\n");
8689 /* Dump a function or thunk and its thunkees. */
8691 static void
8692 dump_thunk (FILE *stream, int indent, tree thunk)
8694 static const char spaces[] = " ";
8695 tree name = DECL_NAME (thunk);
8696 tree thunks;
8698 fprintf (stream, "%.*s%p %s %s", indent, spaces,
8699 (void *)thunk,
8700 !DECL_THUNK_P (thunk) ? "function"
8701 : DECL_THIS_THUNK_P (thunk) ? "this-thunk" : "covariant-thunk",
8702 name ? IDENTIFIER_POINTER (name) : "<unset>");
8703 if (DECL_THUNK_P (thunk))
8705 HOST_WIDE_INT fixed_adjust = THUNK_FIXED_OFFSET (thunk);
8706 tree virtual_adjust = THUNK_VIRTUAL_OFFSET (thunk);
8708 fprintf (stream, " fixed=" HOST_WIDE_INT_PRINT_DEC, fixed_adjust);
8709 if (!virtual_adjust)
8710 /*NOP*/;
8711 else if (DECL_THIS_THUNK_P (thunk))
8712 fprintf (stream, " vcall=" HOST_WIDE_INT_PRINT_DEC,
8713 tree_to_shwi (virtual_adjust));
8714 else
8715 fprintf (stream, " vbase=" HOST_WIDE_INT_PRINT_DEC "(%s)",
8716 tree_to_shwi (BINFO_VPTR_FIELD (virtual_adjust)),
8717 type_as_string (BINFO_TYPE (virtual_adjust), TFF_SCOPE));
8718 if (THUNK_ALIAS (thunk))
8719 fprintf (stream, " alias to %p", (void *)THUNK_ALIAS (thunk));
8721 fprintf (stream, "\n");
8722 for (thunks = DECL_THUNKS (thunk); thunks; thunks = TREE_CHAIN (thunks))
8723 dump_thunk (stream, indent + 2, thunks);
8726 /* Dump the thunks for FN. */
8728 void
8729 debug_thunks (tree fn)
8731 dump_thunk (stderr, 0, fn);
8734 /* Virtual function table initialization. */
8736 /* Create all the necessary vtables for T and its base classes. */
8738 static void
8739 finish_vtbls (tree t)
8741 tree vbase;
8742 vec<constructor_elt, va_gc> *v = NULL;
8743 tree vtable = BINFO_VTABLE (TYPE_BINFO (t));
8745 /* We lay out the primary and secondary vtables in one contiguous
8746 vtable. The primary vtable is first, followed by the non-virtual
8747 secondary vtables in inheritance graph order. */
8748 accumulate_vtbl_inits (TYPE_BINFO (t), TYPE_BINFO (t), TYPE_BINFO (t),
8749 vtable, t, &v);
8751 /* Then come the virtual bases, also in inheritance graph order. */
8752 for (vbase = TYPE_BINFO (t); vbase; vbase = TREE_CHAIN (vbase))
8754 if (!BINFO_VIRTUAL_P (vbase))
8755 continue;
8756 accumulate_vtbl_inits (vbase, vbase, TYPE_BINFO (t), vtable, t, &v);
8759 if (BINFO_VTABLE (TYPE_BINFO (t)))
8760 initialize_vtable (TYPE_BINFO (t), v);
8763 /* Initialize the vtable for BINFO with the INITS. */
8765 static void
8766 initialize_vtable (tree binfo, vec<constructor_elt, va_gc> *inits)
8768 tree decl;
8770 layout_vtable_decl (binfo, vec_safe_length (inits));
8771 decl = get_vtbl_decl_for_binfo (binfo);
8772 initialize_artificial_var (decl, inits);
8773 dump_vtable (BINFO_TYPE (binfo), binfo, decl);
8776 /* Build the VTT (virtual table table) for T.
8777 A class requires a VTT if it has virtual bases.
8779 This holds
8780 1 - primary virtual pointer for complete object T
8781 2 - secondary VTTs for each direct non-virtual base of T which requires a
8783 3 - secondary virtual pointers for each direct or indirect base of T which
8784 has virtual bases or is reachable via a virtual path from T.
8785 4 - secondary VTTs for each direct or indirect virtual base of T.
8787 Secondary VTTs look like complete object VTTs without part 4. */
8789 static void
8790 build_vtt (tree t)
8792 tree type;
8793 tree vtt;
8794 tree index;
8795 vec<constructor_elt, va_gc> *inits;
8797 /* Build up the initializers for the VTT. */
8798 inits = NULL;
8799 index = size_zero_node;
8800 build_vtt_inits (TYPE_BINFO (t), t, &inits, &index);
8802 /* If we didn't need a VTT, we're done. */
8803 if (!inits)
8804 return;
8806 /* Figure out the type of the VTT. */
8807 type = build_array_of_n_type (const_ptr_type_node,
8808 inits->length ());
8810 /* Now, build the VTT object itself. */
8811 vtt = build_vtable (t, mangle_vtt_for_type (t), type);
8812 initialize_artificial_var (vtt, inits);
8813 /* Add the VTT to the vtables list. */
8814 DECL_CHAIN (vtt) = DECL_CHAIN (CLASSTYPE_VTABLES (t));
8815 DECL_CHAIN (CLASSTYPE_VTABLES (t)) = vtt;
8817 dump_vtt (t, vtt);
8820 /* When building a secondary VTT, BINFO_VTABLE is set to a TREE_LIST with
8821 PURPOSE the RTTI_BINFO, VALUE the real vtable pointer for this binfo,
8822 and CHAIN the vtable pointer for this binfo after construction is
8823 complete. VALUE can also be another BINFO, in which case we recurse. */
8825 static tree
8826 binfo_ctor_vtable (tree binfo)
8828 tree vt;
8830 while (1)
8832 vt = BINFO_VTABLE (binfo);
8833 if (TREE_CODE (vt) == TREE_LIST)
8834 vt = TREE_VALUE (vt);
8835 if (TREE_CODE (vt) == TREE_BINFO)
8836 binfo = vt;
8837 else
8838 break;
8841 return vt;
8844 /* Data for secondary VTT initialization. */
8845 struct secondary_vptr_vtt_init_data
8847 /* Is this the primary VTT? */
8848 bool top_level_p;
8850 /* Current index into the VTT. */
8851 tree index;
8853 /* Vector of initializers built up. */
8854 vec<constructor_elt, va_gc> *inits;
8856 /* The type being constructed by this secondary VTT. */
8857 tree type_being_constructed;
8860 /* Recursively build the VTT-initializer for BINFO (which is in the
8861 hierarchy dominated by T). INITS points to the end of the initializer
8862 list to date. INDEX is the VTT index where the next element will be
8863 replaced. Iff BINFO is the binfo for T, this is the top level VTT (i.e.
8864 not a subvtt for some base of T). When that is so, we emit the sub-VTTs
8865 for virtual bases of T. When it is not so, we build the constructor
8866 vtables for the BINFO-in-T variant. */
8868 static void
8869 build_vtt_inits (tree binfo, tree t, vec<constructor_elt, va_gc> **inits,
8870 tree *index)
8872 int i;
8873 tree b;
8874 tree init;
8875 secondary_vptr_vtt_init_data data;
8876 int top_level_p = SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), t);
8878 /* We only need VTTs for subobjects with virtual bases. */
8879 if (!CLASSTYPE_VBASECLASSES (BINFO_TYPE (binfo)))
8880 return;
8882 /* We need to use a construction vtable if this is not the primary
8883 VTT. */
8884 if (!top_level_p)
8886 build_ctor_vtbl_group (binfo, t);
8888 /* Record the offset in the VTT where this sub-VTT can be found. */
8889 BINFO_SUBVTT_INDEX (binfo) = *index;
8892 /* Add the address of the primary vtable for the complete object. */
8893 init = binfo_ctor_vtable (binfo);
8894 CONSTRUCTOR_APPEND_ELT (*inits, NULL_TREE, init);
8895 if (top_level_p)
8897 gcc_assert (!BINFO_VPTR_INDEX (binfo));
8898 BINFO_VPTR_INDEX (binfo) = *index;
8900 *index = size_binop (PLUS_EXPR, *index, TYPE_SIZE_UNIT (ptr_type_node));
8902 /* Recursively add the secondary VTTs for non-virtual bases. */
8903 for (i = 0; BINFO_BASE_ITERATE (binfo, i, b); ++i)
8904 if (!BINFO_VIRTUAL_P (b))
8905 build_vtt_inits (b, t, inits, index);
8907 /* Add secondary virtual pointers for all subobjects of BINFO with
8908 either virtual bases or reachable along a virtual path, except
8909 subobjects that are non-virtual primary bases. */
8910 data.top_level_p = top_level_p;
8911 data.index = *index;
8912 data.inits = *inits;
8913 data.type_being_constructed = BINFO_TYPE (binfo);
8915 dfs_walk_once (binfo, dfs_build_secondary_vptr_vtt_inits, NULL, &data);
8917 *index = data.index;
8919 /* data.inits might have grown as we added secondary virtual pointers.
8920 Make sure our caller knows about the new vector. */
8921 *inits = data.inits;
8923 if (top_level_p)
8924 /* Add the secondary VTTs for virtual bases in inheritance graph
8925 order. */
8926 for (b = TYPE_BINFO (BINFO_TYPE (binfo)); b; b = TREE_CHAIN (b))
8928 if (!BINFO_VIRTUAL_P (b))
8929 continue;
8931 build_vtt_inits (b, t, inits, index);
8933 else
8934 /* Remove the ctor vtables we created. */
8935 dfs_walk_all (binfo, dfs_fixup_binfo_vtbls, NULL, binfo);
8938 /* Called from build_vtt_inits via dfs_walk. BINFO is the binfo for the base
8939 in most derived. DATA is a SECONDARY_VPTR_VTT_INIT_DATA structure. */
8941 static tree
8942 dfs_build_secondary_vptr_vtt_inits (tree binfo, void *data_)
8944 secondary_vptr_vtt_init_data *data = (secondary_vptr_vtt_init_data *)data_;
8946 /* We don't care about bases that don't have vtables. */
8947 if (!TYPE_VFIELD (BINFO_TYPE (binfo)))
8948 return dfs_skip_bases;
8950 /* We're only interested in proper subobjects of the type being
8951 constructed. */
8952 if (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), data->type_being_constructed))
8953 return NULL_TREE;
8955 /* We're only interested in bases with virtual bases or reachable
8956 via a virtual path from the type being constructed. */
8957 if (!(CLASSTYPE_VBASECLASSES (BINFO_TYPE (binfo))
8958 || binfo_via_virtual (binfo, data->type_being_constructed)))
8959 return dfs_skip_bases;
8961 /* We're not interested in non-virtual primary bases. */
8962 if (!BINFO_VIRTUAL_P (binfo) && BINFO_PRIMARY_P (binfo))
8963 return NULL_TREE;
8965 /* Record the index where this secondary vptr can be found. */
8966 if (data->top_level_p)
8968 gcc_assert (!BINFO_VPTR_INDEX (binfo));
8969 BINFO_VPTR_INDEX (binfo) = data->index;
8971 if (BINFO_VIRTUAL_P (binfo))
8973 /* It's a primary virtual base, and this is not a
8974 construction vtable. Find the base this is primary of in
8975 the inheritance graph, and use that base's vtable
8976 now. */
8977 while (BINFO_PRIMARY_P (binfo))
8978 binfo = BINFO_INHERITANCE_CHAIN (binfo);
8982 /* Add the initializer for the secondary vptr itself. */
8983 CONSTRUCTOR_APPEND_ELT (data->inits, NULL_TREE, binfo_ctor_vtable (binfo));
8985 /* Advance the vtt index. */
8986 data->index = size_binop (PLUS_EXPR, data->index,
8987 TYPE_SIZE_UNIT (ptr_type_node));
8989 return NULL_TREE;
8992 /* Called from build_vtt_inits via dfs_walk. After building
8993 constructor vtables and generating the sub-vtt from them, we need
8994 to restore the BINFO_VTABLES that were scribbled on. DATA is the
8995 binfo of the base whose sub vtt was generated. */
8997 static tree
8998 dfs_fixup_binfo_vtbls (tree binfo, void* data)
9000 tree vtable = BINFO_VTABLE (binfo);
9002 if (!TYPE_CONTAINS_VPTR_P (BINFO_TYPE (binfo)))
9003 /* If this class has no vtable, none of its bases do. */
9004 return dfs_skip_bases;
9006 if (!vtable)
9007 /* This might be a primary base, so have no vtable in this
9008 hierarchy. */
9009 return NULL_TREE;
9011 /* If we scribbled the construction vtable vptr into BINFO, clear it
9012 out now. */
9013 if (TREE_CODE (vtable) == TREE_LIST
9014 && (TREE_PURPOSE (vtable) == (tree) data))
9015 BINFO_VTABLE (binfo) = TREE_CHAIN (vtable);
9017 return NULL_TREE;
9020 /* Build the construction vtable group for BINFO which is in the
9021 hierarchy dominated by T. */
9023 static void
9024 build_ctor_vtbl_group (tree binfo, tree t)
9026 tree type;
9027 tree vtbl;
9028 tree id;
9029 tree vbase;
9030 vec<constructor_elt, va_gc> *v;
9032 /* See if we've already created this construction vtable group. */
9033 id = mangle_ctor_vtbl_for_type (t, binfo);
9034 if (IDENTIFIER_GLOBAL_VALUE (id))
9035 return;
9037 gcc_assert (!SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), t));
9038 /* Build a version of VTBL (with the wrong type) for use in
9039 constructing the addresses of secondary vtables in the
9040 construction vtable group. */
9041 vtbl = build_vtable (t, id, ptr_type_node);
9042 DECL_CONSTRUCTION_VTABLE_P (vtbl) = 1;
9043 /* Don't export construction vtables from shared libraries. Even on
9044 targets that don't support hidden visibility, this tells
9045 can_refer_decl_in_current_unit_p not to assume that it's safe to
9046 access from a different compilation unit (bz 54314). */
9047 DECL_VISIBILITY (vtbl) = VISIBILITY_HIDDEN;
9048 DECL_VISIBILITY_SPECIFIED (vtbl) = true;
9050 v = NULL;
9051 accumulate_vtbl_inits (binfo, TYPE_BINFO (TREE_TYPE (binfo)),
9052 binfo, vtbl, t, &v);
9054 /* Add the vtables for each of our virtual bases using the vbase in T
9055 binfo. */
9056 for (vbase = TYPE_BINFO (BINFO_TYPE (binfo));
9057 vbase;
9058 vbase = TREE_CHAIN (vbase))
9060 tree b;
9062 if (!BINFO_VIRTUAL_P (vbase))
9063 continue;
9064 b = copied_binfo (vbase, binfo);
9066 accumulate_vtbl_inits (b, vbase, binfo, vtbl, t, &v);
9069 /* Figure out the type of the construction vtable. */
9070 type = build_array_of_n_type (vtable_entry_type, v->length ());
9071 layout_type (type);
9072 TREE_TYPE (vtbl) = type;
9073 DECL_SIZE (vtbl) = DECL_SIZE_UNIT (vtbl) = NULL_TREE;
9074 layout_decl (vtbl, 0);
9076 /* Initialize the construction vtable. */
9077 CLASSTYPE_VTABLES (t) = chainon (CLASSTYPE_VTABLES (t), vtbl);
9078 initialize_artificial_var (vtbl, v);
9079 dump_vtable (t, binfo, vtbl);
9082 /* Add the vtbl initializers for BINFO (and its bases other than
9083 non-virtual primaries) to the list of INITS. BINFO is in the
9084 hierarchy dominated by T. RTTI_BINFO is the binfo within T of
9085 the constructor the vtbl inits should be accumulated for. (If this
9086 is the complete object vtbl then RTTI_BINFO will be TYPE_BINFO (T).)
9087 ORIG_BINFO is the binfo for this object within BINFO_TYPE (RTTI_BINFO).
9088 BINFO is the active base equivalent of ORIG_BINFO in the inheritance
9089 graph of T. Both BINFO and ORIG_BINFO will have the same BINFO_TYPE,
9090 but are not necessarily the same in terms of layout. */
9092 static void
9093 accumulate_vtbl_inits (tree binfo,
9094 tree orig_binfo,
9095 tree rtti_binfo,
9096 tree vtbl,
9097 tree t,
9098 vec<constructor_elt, va_gc> **inits)
9100 int i;
9101 tree base_binfo;
9102 int ctor_vtbl_p = !SAME_BINFO_TYPE_P (BINFO_TYPE (rtti_binfo), t);
9104 gcc_assert (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), BINFO_TYPE (orig_binfo)));
9106 /* If it doesn't have a vptr, we don't do anything. */
9107 if (!TYPE_CONTAINS_VPTR_P (BINFO_TYPE (binfo)))
9108 return;
9110 /* If we're building a construction vtable, we're not interested in
9111 subobjects that don't require construction vtables. */
9112 if (ctor_vtbl_p
9113 && !CLASSTYPE_VBASECLASSES (BINFO_TYPE (binfo))
9114 && !binfo_via_virtual (orig_binfo, BINFO_TYPE (rtti_binfo)))
9115 return;
9117 /* Build the initializers for the BINFO-in-T vtable. */
9118 dfs_accumulate_vtbl_inits (binfo, orig_binfo, rtti_binfo, vtbl, t, inits);
9120 /* Walk the BINFO and its bases. We walk in preorder so that as we
9121 initialize each vtable we can figure out at what offset the
9122 secondary vtable lies from the primary vtable. We can't use
9123 dfs_walk here because we need to iterate through bases of BINFO
9124 and RTTI_BINFO simultaneously. */
9125 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
9127 /* Skip virtual bases. */
9128 if (BINFO_VIRTUAL_P (base_binfo))
9129 continue;
9130 accumulate_vtbl_inits (base_binfo,
9131 BINFO_BASE_BINFO (orig_binfo, i),
9132 rtti_binfo, vtbl, t,
9133 inits);
9137 /* Called from accumulate_vtbl_inits. Adds the initializers for the
9138 BINFO vtable to L. */
9140 static void
9141 dfs_accumulate_vtbl_inits (tree binfo,
9142 tree orig_binfo,
9143 tree rtti_binfo,
9144 tree orig_vtbl,
9145 tree t,
9146 vec<constructor_elt, va_gc> **l)
9148 tree vtbl = NULL_TREE;
9149 int ctor_vtbl_p = !SAME_BINFO_TYPE_P (BINFO_TYPE (rtti_binfo), t);
9150 int n_inits;
9152 if (ctor_vtbl_p
9153 && BINFO_VIRTUAL_P (orig_binfo) && BINFO_PRIMARY_P (orig_binfo))
9155 /* In the hierarchy of BINFO_TYPE (RTTI_BINFO), this is a
9156 primary virtual base. If it is not the same primary in
9157 the hierarchy of T, we'll need to generate a ctor vtable
9158 for it, to place at its location in T. If it is the same
9159 primary, we still need a VTT entry for the vtable, but it
9160 should point to the ctor vtable for the base it is a
9161 primary for within the sub-hierarchy of RTTI_BINFO.
9163 There are three possible cases:
9165 1) We are in the same place.
9166 2) We are a primary base within a lost primary virtual base of
9167 RTTI_BINFO.
9168 3) We are primary to something not a base of RTTI_BINFO. */
9170 tree b;
9171 tree last = NULL_TREE;
9173 /* First, look through the bases we are primary to for RTTI_BINFO
9174 or a virtual base. */
9175 b = binfo;
9176 while (BINFO_PRIMARY_P (b))
9178 b = BINFO_INHERITANCE_CHAIN (b);
9179 last = b;
9180 if (BINFO_VIRTUAL_P (b) || b == rtti_binfo)
9181 goto found;
9183 /* If we run out of primary links, keep looking down our
9184 inheritance chain; we might be an indirect primary. */
9185 for (b = last; b; b = BINFO_INHERITANCE_CHAIN (b))
9186 if (BINFO_VIRTUAL_P (b) || b == rtti_binfo)
9187 break;
9188 found:
9190 /* If we found RTTI_BINFO, this is case 1. If we found a virtual
9191 base B and it is a base of RTTI_BINFO, this is case 2. In
9192 either case, we share our vtable with LAST, i.e. the
9193 derived-most base within B of which we are a primary. */
9194 if (b == rtti_binfo
9195 || (b && binfo_for_vbase (BINFO_TYPE (b), BINFO_TYPE (rtti_binfo))))
9196 /* Just set our BINFO_VTABLE to point to LAST, as we may not have
9197 set LAST's BINFO_VTABLE yet. We'll extract the actual vptr in
9198 binfo_ctor_vtable after everything's been set up. */
9199 vtbl = last;
9201 /* Otherwise, this is case 3 and we get our own. */
9203 else if (!BINFO_NEW_VTABLE_MARKED (orig_binfo))
9204 return;
9206 n_inits = vec_safe_length (*l);
9208 if (!vtbl)
9210 tree index;
9211 int non_fn_entries;
9213 /* Add the initializer for this vtable. */
9214 build_vtbl_initializer (binfo, orig_binfo, t, rtti_binfo,
9215 &non_fn_entries, l);
9217 /* Figure out the position to which the VPTR should point. */
9218 vtbl = build1 (ADDR_EXPR, vtbl_ptr_type_node, orig_vtbl);
9219 index = size_binop (MULT_EXPR,
9220 TYPE_SIZE_UNIT (vtable_entry_type),
9221 size_int (non_fn_entries + n_inits));
9222 vtbl = fold_build_pointer_plus (vtbl, index);
9225 if (ctor_vtbl_p)
9226 /* For a construction vtable, we can't overwrite BINFO_VTABLE.
9227 So, we make a TREE_LIST. Later, dfs_fixup_binfo_vtbls will
9228 straighten this out. */
9229 BINFO_VTABLE (binfo) = tree_cons (rtti_binfo, vtbl, BINFO_VTABLE (binfo));
9230 else if (BINFO_PRIMARY_P (binfo) && BINFO_VIRTUAL_P (binfo))
9231 /* Throw away any unneeded intializers. */
9232 (*l)->truncate (n_inits);
9233 else
9234 /* For an ordinary vtable, set BINFO_VTABLE. */
9235 BINFO_VTABLE (binfo) = vtbl;
9238 static GTY(()) tree abort_fndecl_addr;
9240 /* Construct the initializer for BINFO's virtual function table. BINFO
9241 is part of the hierarchy dominated by T. If we're building a
9242 construction vtable, the ORIG_BINFO is the binfo we should use to
9243 find the actual function pointers to put in the vtable - but they
9244 can be overridden on the path to most-derived in the graph that
9245 ORIG_BINFO belongs. Otherwise,
9246 ORIG_BINFO should be the same as BINFO. The RTTI_BINFO is the
9247 BINFO that should be indicated by the RTTI information in the
9248 vtable; it will be a base class of T, rather than T itself, if we
9249 are building a construction vtable.
9251 The value returned is a TREE_LIST suitable for wrapping in a
9252 CONSTRUCTOR to use as the DECL_INITIAL for a vtable. If
9253 NON_FN_ENTRIES_P is not NULL, *NON_FN_ENTRIES_P is set to the
9254 number of non-function entries in the vtable.
9256 It might seem that this function should never be called with a
9257 BINFO for which BINFO_PRIMARY_P holds, the vtable for such a
9258 base is always subsumed by a derived class vtable. However, when
9259 we are building construction vtables, we do build vtables for
9260 primary bases; we need these while the primary base is being
9261 constructed. */
9263 static void
9264 build_vtbl_initializer (tree binfo,
9265 tree orig_binfo,
9266 tree t,
9267 tree rtti_binfo,
9268 int* non_fn_entries_p,
9269 vec<constructor_elt, va_gc> **inits)
9271 tree v;
9272 vtbl_init_data vid;
9273 unsigned ix, jx;
9274 tree vbinfo;
9275 vec<tree, va_gc> *vbases;
9276 constructor_elt *e;
9278 /* Initialize VID. */
9279 memset (&vid, 0, sizeof (vid));
9280 vid.binfo = binfo;
9281 vid.derived = t;
9282 vid.rtti_binfo = rtti_binfo;
9283 vid.primary_vtbl_p = SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), t);
9284 vid.ctor_vtbl_p = !SAME_BINFO_TYPE_P (BINFO_TYPE (rtti_binfo), t);
9285 vid.generate_vcall_entries = true;
9286 /* The first vbase or vcall offset is at index -3 in the vtable. */
9287 vid.index = ssize_int(-3 * TARGET_VTABLE_DATA_ENTRY_DISTANCE);
9289 /* Add entries to the vtable for RTTI. */
9290 build_rtti_vtbl_entries (binfo, &vid);
9292 /* Create an array for keeping track of the functions we've
9293 processed. When we see multiple functions with the same
9294 signature, we share the vcall offsets. */
9295 vec_alloc (vid.fns, 32);
9296 /* Add the vcall and vbase offset entries. */
9297 build_vcall_and_vbase_vtbl_entries (binfo, &vid);
9299 /* Clear BINFO_VTABLE_PATH_MARKED; it's set by
9300 build_vbase_offset_vtbl_entries. */
9301 for (vbases = CLASSTYPE_VBASECLASSES (t), ix = 0;
9302 vec_safe_iterate (vbases, ix, &vbinfo); ix++)
9303 BINFO_VTABLE_PATH_MARKED (vbinfo) = 0;
9305 /* If the target requires padding between data entries, add that now. */
9306 if (TARGET_VTABLE_DATA_ENTRY_DISTANCE > 1)
9308 int n_entries = vec_safe_length (vid.inits);
9310 vec_safe_grow (vid.inits, TARGET_VTABLE_DATA_ENTRY_DISTANCE * n_entries);
9312 /* Move data entries into their new positions and add padding
9313 after the new positions. Iterate backwards so we don't
9314 overwrite entries that we would need to process later. */
9315 for (ix = n_entries - 1;
9316 vid.inits->iterate (ix, &e);
9317 ix--)
9319 int j;
9320 int new_position = (TARGET_VTABLE_DATA_ENTRY_DISTANCE * ix
9321 + (TARGET_VTABLE_DATA_ENTRY_DISTANCE - 1));
9323 (*vid.inits)[new_position] = *e;
9325 for (j = 1; j < TARGET_VTABLE_DATA_ENTRY_DISTANCE; ++j)
9327 constructor_elt *f = &(*vid.inits)[new_position - j];
9328 f->index = NULL_TREE;
9329 f->value = build1 (NOP_EXPR, vtable_entry_type,
9330 null_pointer_node);
9335 if (non_fn_entries_p)
9336 *non_fn_entries_p = vec_safe_length (vid.inits);
9338 /* The initializers for virtual functions were built up in reverse
9339 order. Straighten them out and add them to the running list in one
9340 step. */
9341 jx = vec_safe_length (*inits);
9342 vec_safe_grow (*inits, jx + vid.inits->length ());
9344 for (ix = vid.inits->length () - 1;
9345 vid.inits->iterate (ix, &e);
9346 ix--, jx++)
9347 (**inits)[jx] = *e;
9349 /* Go through all the ordinary virtual functions, building up
9350 initializers. */
9351 for (v = BINFO_VIRTUALS (orig_binfo); v; v = TREE_CHAIN (v))
9353 tree delta;
9354 tree vcall_index;
9355 tree fn, fn_original;
9356 tree init = NULL_TREE;
9358 fn = BV_FN (v);
9359 fn_original = fn;
9360 if (DECL_THUNK_P (fn))
9362 if (!DECL_NAME (fn))
9363 finish_thunk (fn);
9364 if (THUNK_ALIAS (fn))
9366 fn = THUNK_ALIAS (fn);
9367 BV_FN (v) = fn;
9369 fn_original = THUNK_TARGET (fn);
9372 /* If the only definition of this function signature along our
9373 primary base chain is from a lost primary, this vtable slot will
9374 never be used, so just zero it out. This is important to avoid
9375 requiring extra thunks which cannot be generated with the function.
9377 We first check this in update_vtable_entry_for_fn, so we handle
9378 restored primary bases properly; we also need to do it here so we
9379 zero out unused slots in ctor vtables, rather than filling them
9380 with erroneous values (though harmless, apart from relocation
9381 costs). */
9382 if (BV_LOST_PRIMARY (v))
9383 init = size_zero_node;
9385 if (! init)
9387 /* Pull the offset for `this', and the function to call, out of
9388 the list. */
9389 delta = BV_DELTA (v);
9390 vcall_index = BV_VCALL_INDEX (v);
9392 gcc_assert (TREE_CODE (delta) == INTEGER_CST);
9393 gcc_assert (TREE_CODE (fn) == FUNCTION_DECL);
9395 /* You can't call an abstract virtual function; it's abstract.
9396 So, we replace these functions with __pure_virtual. */
9397 if (DECL_PURE_VIRTUAL_P (fn_original))
9399 fn = abort_fndecl;
9400 if (!TARGET_VTABLE_USES_DESCRIPTORS)
9402 if (abort_fndecl_addr == NULL)
9403 abort_fndecl_addr
9404 = fold_convert (vfunc_ptr_type_node,
9405 build_fold_addr_expr (fn));
9406 init = abort_fndecl_addr;
9409 /* Likewise for deleted virtuals. */
9410 else if (DECL_DELETED_FN (fn_original))
9412 fn = get_identifier ("__cxa_deleted_virtual");
9413 if (!get_global_value_if_present (fn, &fn))
9414 fn = push_library_fn (fn, (build_function_type_list
9415 (void_type_node, NULL_TREE)),
9416 NULL_TREE, ECF_NORETURN);
9417 if (!TARGET_VTABLE_USES_DESCRIPTORS)
9418 init = fold_convert (vfunc_ptr_type_node,
9419 build_fold_addr_expr (fn));
9421 else
9423 if (!integer_zerop (delta) || vcall_index)
9425 fn = make_thunk (fn, /*this_adjusting=*/1, delta, vcall_index);
9426 if (!DECL_NAME (fn))
9427 finish_thunk (fn);
9429 /* Take the address of the function, considering it to be of an
9430 appropriate generic type. */
9431 if (!TARGET_VTABLE_USES_DESCRIPTORS)
9432 init = fold_convert (vfunc_ptr_type_node,
9433 build_fold_addr_expr (fn));
9434 /* Don't refer to a virtual destructor from a constructor
9435 vtable or a vtable for an abstract class, since destroying
9436 an object under construction is undefined behavior and we
9437 don't want it to be considered a candidate for speculative
9438 devirtualization. But do create the thunk for ABI
9439 compliance. */
9440 if (DECL_DESTRUCTOR_P (fn_original)
9441 && (CLASSTYPE_PURE_VIRTUALS (DECL_CONTEXT (fn_original))
9442 || orig_binfo != binfo))
9443 init = size_zero_node;
9447 /* And add it to the chain of initializers. */
9448 if (TARGET_VTABLE_USES_DESCRIPTORS)
9450 int i;
9451 if (init == size_zero_node)
9452 for (i = 0; i < TARGET_VTABLE_USES_DESCRIPTORS; ++i)
9453 CONSTRUCTOR_APPEND_ELT (*inits, NULL_TREE, init);
9454 else
9455 for (i = 0; i < TARGET_VTABLE_USES_DESCRIPTORS; ++i)
9457 tree fdesc = build2 (FDESC_EXPR, vfunc_ptr_type_node,
9458 fn, build_int_cst (NULL_TREE, i));
9459 TREE_CONSTANT (fdesc) = 1;
9461 CONSTRUCTOR_APPEND_ELT (*inits, NULL_TREE, fdesc);
9464 else
9465 CONSTRUCTOR_APPEND_ELT (*inits, NULL_TREE, init);
9469 /* Adds to vid->inits the initializers for the vbase and vcall
9470 offsets in BINFO, which is in the hierarchy dominated by T. */
9472 static void
9473 build_vcall_and_vbase_vtbl_entries (tree binfo, vtbl_init_data* vid)
9475 tree b;
9477 /* If this is a derived class, we must first create entries
9478 corresponding to the primary base class. */
9479 b = get_primary_binfo (binfo);
9480 if (b)
9481 build_vcall_and_vbase_vtbl_entries (b, vid);
9483 /* Add the vbase entries for this base. */
9484 build_vbase_offset_vtbl_entries (binfo, vid);
9485 /* Add the vcall entries for this base. */
9486 build_vcall_offset_vtbl_entries (binfo, vid);
9489 /* Returns the initializers for the vbase offset entries in the vtable
9490 for BINFO (which is part of the class hierarchy dominated by T), in
9491 reverse order. VBASE_OFFSET_INDEX gives the vtable index
9492 where the next vbase offset will go. */
9494 static void
9495 build_vbase_offset_vtbl_entries (tree binfo, vtbl_init_data* vid)
9497 tree vbase;
9498 tree t;
9499 tree non_primary_binfo;
9501 /* If there are no virtual baseclasses, then there is nothing to
9502 do. */
9503 if (!CLASSTYPE_VBASECLASSES (BINFO_TYPE (binfo)))
9504 return;
9506 t = vid->derived;
9508 /* We might be a primary base class. Go up the inheritance hierarchy
9509 until we find the most derived class of which we are a primary base:
9510 it is the offset of that which we need to use. */
9511 non_primary_binfo = binfo;
9512 while (BINFO_INHERITANCE_CHAIN (non_primary_binfo))
9514 tree b;
9516 /* If we have reached a virtual base, then it must be a primary
9517 base (possibly multi-level) of vid->binfo, or we wouldn't
9518 have called build_vcall_and_vbase_vtbl_entries for it. But it
9519 might be a lost primary, so just skip down to vid->binfo. */
9520 if (BINFO_VIRTUAL_P (non_primary_binfo))
9522 non_primary_binfo = vid->binfo;
9523 break;
9526 b = BINFO_INHERITANCE_CHAIN (non_primary_binfo);
9527 if (get_primary_binfo (b) != non_primary_binfo)
9528 break;
9529 non_primary_binfo = b;
9532 /* Go through the virtual bases, adding the offsets. */
9533 for (vbase = TYPE_BINFO (BINFO_TYPE (binfo));
9534 vbase;
9535 vbase = TREE_CHAIN (vbase))
9537 tree b;
9538 tree delta;
9540 if (!BINFO_VIRTUAL_P (vbase))
9541 continue;
9543 /* Find the instance of this virtual base in the complete
9544 object. */
9545 b = copied_binfo (vbase, binfo);
9547 /* If we've already got an offset for this virtual base, we
9548 don't need another one. */
9549 if (BINFO_VTABLE_PATH_MARKED (b))
9550 continue;
9551 BINFO_VTABLE_PATH_MARKED (b) = 1;
9553 /* Figure out where we can find this vbase offset. */
9554 delta = size_binop (MULT_EXPR,
9555 vid->index,
9556 fold_convert (ssizetype,
9557 TYPE_SIZE_UNIT (vtable_entry_type)));
9558 if (vid->primary_vtbl_p)
9559 BINFO_VPTR_FIELD (b) = delta;
9561 if (binfo != TYPE_BINFO (t))
9562 /* The vbase offset had better be the same. */
9563 gcc_assert (tree_int_cst_equal (delta, BINFO_VPTR_FIELD (vbase)));
9565 /* The next vbase will come at a more negative offset. */
9566 vid->index = size_binop (MINUS_EXPR, vid->index,
9567 ssize_int (TARGET_VTABLE_DATA_ENTRY_DISTANCE));
9569 /* The initializer is the delta from BINFO to this virtual base.
9570 The vbase offsets go in reverse inheritance-graph order, and
9571 we are walking in inheritance graph order so these end up in
9572 the right order. */
9573 delta = size_diffop_loc (input_location,
9574 BINFO_OFFSET (b), BINFO_OFFSET (non_primary_binfo));
9576 CONSTRUCTOR_APPEND_ELT (vid->inits, NULL_TREE,
9577 fold_build1_loc (input_location, NOP_EXPR,
9578 vtable_entry_type, delta));
9582 /* Adds the initializers for the vcall offset entries in the vtable
9583 for BINFO (which is part of the class hierarchy dominated by VID->DERIVED)
9584 to VID->INITS. */
9586 static void
9587 build_vcall_offset_vtbl_entries (tree binfo, vtbl_init_data* vid)
9589 /* We only need these entries if this base is a virtual base. We
9590 compute the indices -- but do not add to the vtable -- when
9591 building the main vtable for a class. */
9592 if (binfo == TYPE_BINFO (vid->derived)
9593 || (BINFO_VIRTUAL_P (binfo)
9594 /* If BINFO is RTTI_BINFO, then (since BINFO does not
9595 correspond to VID->DERIVED), we are building a primary
9596 construction virtual table. Since this is a primary
9597 virtual table, we do not need the vcall offsets for
9598 BINFO. */
9599 && binfo != vid->rtti_binfo))
9601 /* We need a vcall offset for each of the virtual functions in this
9602 vtable. For example:
9604 class A { virtual void f (); };
9605 class B1 : virtual public A { virtual void f (); };
9606 class B2 : virtual public A { virtual void f (); };
9607 class C: public B1, public B2 { virtual void f (); };
9609 A C object has a primary base of B1, which has a primary base of A. A
9610 C also has a secondary base of B2, which no longer has a primary base
9611 of A. So the B2-in-C construction vtable needs a secondary vtable for
9612 A, which will adjust the A* to a B2* to call f. We have no way of
9613 knowing what (or even whether) this offset will be when we define B2,
9614 so we store this "vcall offset" in the A sub-vtable and look it up in
9615 a "virtual thunk" for B2::f.
9617 We need entries for all the functions in our primary vtable and
9618 in our non-virtual bases' secondary vtables. */
9619 vid->vbase = binfo;
9620 /* If we are just computing the vcall indices -- but do not need
9621 the actual entries -- not that. */
9622 if (!BINFO_VIRTUAL_P (binfo))
9623 vid->generate_vcall_entries = false;
9624 /* Now, walk through the non-virtual bases, adding vcall offsets. */
9625 add_vcall_offset_vtbl_entries_r (binfo, vid);
9629 /* Build vcall offsets, starting with those for BINFO. */
9631 static void
9632 add_vcall_offset_vtbl_entries_r (tree binfo, vtbl_init_data* vid)
9634 int i;
9635 tree primary_binfo;
9636 tree base_binfo;
9638 /* Don't walk into virtual bases -- except, of course, for the
9639 virtual base for which we are building vcall offsets. Any
9640 primary virtual base will have already had its offsets generated
9641 through the recursion in build_vcall_and_vbase_vtbl_entries. */
9642 if (BINFO_VIRTUAL_P (binfo) && vid->vbase != binfo)
9643 return;
9645 /* If BINFO has a primary base, process it first. */
9646 primary_binfo = get_primary_binfo (binfo);
9647 if (primary_binfo)
9648 add_vcall_offset_vtbl_entries_r (primary_binfo, vid);
9650 /* Add BINFO itself to the list. */
9651 add_vcall_offset_vtbl_entries_1 (binfo, vid);
9653 /* Scan the non-primary bases of BINFO. */
9654 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
9655 if (base_binfo != primary_binfo)
9656 add_vcall_offset_vtbl_entries_r (base_binfo, vid);
9659 /* Called from build_vcall_offset_vtbl_entries_r. */
9661 static void
9662 add_vcall_offset_vtbl_entries_1 (tree binfo, vtbl_init_data* vid)
9664 /* Make entries for the rest of the virtuals. */
9665 tree orig_fn;
9667 /* The ABI requires that the methods be processed in declaration
9668 order. */
9669 for (orig_fn = TYPE_METHODS (BINFO_TYPE (binfo));
9670 orig_fn;
9671 orig_fn = DECL_CHAIN (orig_fn))
9672 if (TREE_CODE (orig_fn) == FUNCTION_DECL && DECL_VINDEX (orig_fn))
9673 add_vcall_offset (orig_fn, binfo, vid);
9676 /* Add a vcall offset entry for ORIG_FN to the vtable. */
9678 static void
9679 add_vcall_offset (tree orig_fn, tree binfo, vtbl_init_data *vid)
9681 size_t i;
9682 tree vcall_offset;
9683 tree derived_entry;
9685 /* If there is already an entry for a function with the same
9686 signature as FN, then we do not need a second vcall offset.
9687 Check the list of functions already present in the derived
9688 class vtable. */
9689 FOR_EACH_VEC_SAFE_ELT (vid->fns, i, derived_entry)
9691 if (same_signature_p (derived_entry, orig_fn)
9692 /* We only use one vcall offset for virtual destructors,
9693 even though there are two virtual table entries. */
9694 || (DECL_DESTRUCTOR_P (derived_entry)
9695 && DECL_DESTRUCTOR_P (orig_fn)))
9696 return;
9699 /* If we are building these vcall offsets as part of building
9700 the vtable for the most derived class, remember the vcall
9701 offset. */
9702 if (vid->binfo == TYPE_BINFO (vid->derived))
9704 tree_pair_s elt = {orig_fn, vid->index};
9705 vec_safe_push (CLASSTYPE_VCALL_INDICES (vid->derived), elt);
9708 /* The next vcall offset will be found at a more negative
9709 offset. */
9710 vid->index = size_binop (MINUS_EXPR, vid->index,
9711 ssize_int (TARGET_VTABLE_DATA_ENTRY_DISTANCE));
9713 /* Keep track of this function. */
9714 vec_safe_push (vid->fns, orig_fn);
9716 if (vid->generate_vcall_entries)
9718 tree base;
9719 tree fn;
9721 /* Find the overriding function. */
9722 fn = find_final_overrider (vid->rtti_binfo, binfo, orig_fn);
9723 if (fn == error_mark_node)
9724 vcall_offset = build_zero_cst (vtable_entry_type);
9725 else
9727 base = TREE_VALUE (fn);
9729 /* The vbase we're working on is a primary base of
9730 vid->binfo. But it might be a lost primary, so its
9731 BINFO_OFFSET might be wrong, so we just use the
9732 BINFO_OFFSET from vid->binfo. */
9733 vcall_offset = size_diffop_loc (input_location,
9734 BINFO_OFFSET (base),
9735 BINFO_OFFSET (vid->binfo));
9736 vcall_offset = fold_build1_loc (input_location,
9737 NOP_EXPR, vtable_entry_type,
9738 vcall_offset);
9740 /* Add the initializer to the vtable. */
9741 CONSTRUCTOR_APPEND_ELT (vid->inits, NULL_TREE, vcall_offset);
9745 /* Return vtbl initializers for the RTTI entries corresponding to the
9746 BINFO's vtable. The RTTI entries should indicate the object given
9747 by VID->rtti_binfo. */
9749 static void
9750 build_rtti_vtbl_entries (tree binfo, vtbl_init_data* vid)
9752 tree b;
9753 tree t;
9754 tree offset;
9755 tree decl;
9756 tree init;
9758 t = BINFO_TYPE (vid->rtti_binfo);
9760 /* To find the complete object, we will first convert to our most
9761 primary base, and then add the offset in the vtbl to that value. */
9762 b = binfo;
9763 while (CLASSTYPE_HAS_PRIMARY_BASE_P (BINFO_TYPE (b))
9764 && !BINFO_LOST_PRIMARY_P (b))
9766 tree primary_base;
9768 primary_base = get_primary_binfo (b);
9769 gcc_assert (BINFO_PRIMARY_P (primary_base)
9770 && BINFO_INHERITANCE_CHAIN (primary_base) == b);
9771 b = primary_base;
9773 offset = size_diffop_loc (input_location,
9774 BINFO_OFFSET (vid->rtti_binfo), BINFO_OFFSET (b));
9776 /* The second entry is the address of the typeinfo object. */
9777 if (flag_rtti)
9778 decl = build_address (get_tinfo_decl (t));
9779 else
9780 decl = integer_zero_node;
9782 /* Convert the declaration to a type that can be stored in the
9783 vtable. */
9784 init = build_nop (vfunc_ptr_type_node, decl);
9785 CONSTRUCTOR_APPEND_ELT (vid->inits, NULL_TREE, init);
9787 /* Add the offset-to-top entry. It comes earlier in the vtable than
9788 the typeinfo entry. Convert the offset to look like a
9789 function pointer, so that we can put it in the vtable. */
9790 init = build_nop (vfunc_ptr_type_node, offset);
9791 CONSTRUCTOR_APPEND_ELT (vid->inits, NULL_TREE, init);
9794 /* TRUE iff TYPE is uniquely derived from PARENT. Ignores
9795 accessibility. */
9797 bool
9798 uniquely_derived_from_p (tree parent, tree type)
9800 tree base = lookup_base (type, parent, ba_unique, NULL, tf_none);
9801 return base && base != error_mark_node;
9804 /* TRUE iff TYPE is publicly & uniquely derived from PARENT. */
9806 bool
9807 publicly_uniquely_derived_p (tree parent, tree type)
9809 tree base = lookup_base (type, parent, ba_ignore_scope | ba_check,
9810 NULL, tf_none);
9811 return base && base != error_mark_node;
9814 /* CTX1 and CTX2 are declaration contexts. Return the innermost common
9815 class between them, if any. */
9817 tree
9818 common_enclosing_class (tree ctx1, tree ctx2)
9820 if (!TYPE_P (ctx1) || !TYPE_P (ctx2))
9821 return NULL_TREE;
9822 gcc_assert (ctx1 == TYPE_MAIN_VARIANT (ctx1)
9823 && ctx2 == TYPE_MAIN_VARIANT (ctx2));
9824 if (ctx1 == ctx2)
9825 return ctx1;
9826 for (tree t = ctx1; TYPE_P (t); t = TYPE_CONTEXT (t))
9827 TYPE_MARKED_P (t) = true;
9828 tree found = NULL_TREE;
9829 for (tree t = ctx2; TYPE_P (t); t = TYPE_CONTEXT (t))
9830 if (TYPE_MARKED_P (t))
9832 found = t;
9833 break;
9835 for (tree t = ctx1; TYPE_P (t); t = TYPE_CONTEXT (t))
9836 TYPE_MARKED_P (t) = false;
9837 return found;
9840 #include "gt-cp-class.h"