* gcc.dg/vmx/unpack.c: Use dg-additional-options rather than
[official-gcc.git] / gcc / cp / class.c
blobb5b5b8b5cee9ca84de51618f4dea99c631b556b0
1 /* Functions related to building classes and their related objects.
2 Copyright (C) 1987-2015 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 "alias.h"
28 #include "tree.h"
29 #include "options.h"
30 #include "tm.h"
31 #include "stringpool.h"
32 #include "stor-layout.h"
33 #include "attribs.h"
34 #include "cp-tree.h"
35 #include "flags.h"
36 #include "toplev.h"
37 #include "target.h"
38 #include "convert.h"
39 #include "hard-reg-set.h"
40 #include "function.h"
41 #include "cgraph.h"
42 #include "dumpfile.h"
43 #include "splay-tree.h"
44 #include "gimplify.h"
46 /* The number of nested classes being processed. If we are not in the
47 scope of any class, this is zero. */
49 int current_class_depth;
51 /* In order to deal with nested classes, we keep a stack of classes.
52 The topmost entry is the innermost class, and is the entry at index
53 CURRENT_CLASS_DEPTH */
55 typedef struct class_stack_node {
56 /* The name of the class. */
57 tree name;
59 /* The _TYPE node for the class. */
60 tree type;
62 /* The access specifier pending for new declarations in the scope of
63 this class. */
64 tree access;
66 /* If were defining TYPE, the names used in this class. */
67 splay_tree names_used;
69 /* Nonzero if this class is no longer open, because of a call to
70 push_to_top_level. */
71 size_t hidden;
72 }* class_stack_node_t;
74 typedef struct vtbl_init_data_s
76 /* The base for which we're building initializers. */
77 tree binfo;
78 /* The type of the most-derived type. */
79 tree derived;
80 /* The binfo for the dynamic type. This will be TYPE_BINFO (derived),
81 unless ctor_vtbl_p is true. */
82 tree rtti_binfo;
83 /* The negative-index vtable initializers built up so far. These
84 are in order from least negative index to most negative index. */
85 vec<constructor_elt, va_gc> *inits;
86 /* The binfo for the virtual base for which we're building
87 vcall offset initializers. */
88 tree vbase;
89 /* The functions in vbase for which we have already provided vcall
90 offsets. */
91 vec<tree, va_gc> *fns;
92 /* The vtable index of the next vcall or vbase offset. */
93 tree index;
94 /* Nonzero if we are building the initializer for the primary
95 vtable. */
96 int primary_vtbl_p;
97 /* Nonzero if we are building the initializer for a construction
98 vtable. */
99 int ctor_vtbl_p;
100 /* True when adding vcall offset entries to the vtable. False when
101 merely computing the indices. */
102 bool generate_vcall_entries;
103 } vtbl_init_data;
105 /* The type of a function passed to walk_subobject_offsets. */
106 typedef int (*subobject_offset_fn) (tree, tree, splay_tree);
108 /* The stack itself. This is a dynamically resized array. The
109 number of elements allocated is CURRENT_CLASS_STACK_SIZE. */
110 static int current_class_stack_size;
111 static class_stack_node_t current_class_stack;
113 /* The size of the largest empty class seen in this translation unit. */
114 static GTY (()) tree sizeof_biggest_empty_class;
116 /* An array of all local classes present in this translation unit, in
117 declaration order. */
118 vec<tree, va_gc> *local_classes;
120 static tree get_vfield_name (tree);
121 static void finish_struct_anon (tree);
122 static tree get_vtable_name (tree);
123 static void get_basefndecls (tree, tree, vec<tree> *);
124 static int build_primary_vtable (tree, tree);
125 static int build_secondary_vtable (tree);
126 static void finish_vtbls (tree);
127 static void modify_vtable_entry (tree, tree, tree, tree, tree *);
128 static void finish_struct_bits (tree);
129 static int alter_access (tree, tree, tree);
130 static void handle_using_decl (tree, tree);
131 static tree dfs_modify_vtables (tree, void *);
132 static tree modify_all_vtables (tree, tree);
133 static void determine_primary_bases (tree);
134 static void finish_struct_methods (tree);
135 static void maybe_warn_about_overly_private_class (tree);
136 static int method_name_cmp (const void *, const void *);
137 static int resort_method_name_cmp (const void *, const void *);
138 static void add_implicitly_declared_members (tree, tree*, int, int);
139 static tree fixed_type_or_null (tree, int *, int *);
140 static tree build_simple_base_path (tree expr, tree binfo);
141 static tree build_vtbl_ref_1 (tree, tree);
142 static void build_vtbl_initializer (tree, tree, tree, tree, int *,
143 vec<constructor_elt, va_gc> **);
144 static int count_fields (tree);
145 static int add_fields_to_record_type (tree, struct sorted_fields_type*, int);
146 static void insert_into_classtype_sorted_fields (tree, tree, int);
147 static bool check_bitfield_decl (tree);
148 static void check_field_decl (tree, tree, int *, int *, int *);
149 static void check_field_decls (tree, tree *, int *, int *);
150 static tree *build_base_field (record_layout_info, tree, splay_tree, tree *);
151 static void build_base_fields (record_layout_info, splay_tree, tree *);
152 static void check_methods (tree);
153 static void remove_zero_width_bit_fields (tree);
154 static bool accessible_nvdtor_p (tree);
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 = 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 expr = fold_if_not_in_template (expr);
566 /* Mark the expression const or volatile, as appropriate.
567 Even though we've dealt with the type above, we still have
568 to mark the expression itself. */
569 if (type_quals & TYPE_QUAL_CONST)
570 TREE_READONLY (expr) = 1;
571 if (type_quals & TYPE_QUAL_VOLATILE)
572 TREE_THIS_VOLATILE (expr) = 1;
574 return expr;
577 /* Didn't find the base field?!? */
578 gcc_unreachable ();
581 /* Convert OBJECT to the base TYPE. OBJECT is an expression whose
582 type is a class type or a pointer to a class type. In the former
583 case, TYPE is also a class type; in the latter it is another
584 pointer type. If CHECK_ACCESS is true, an error message is emitted
585 if TYPE is inaccessible. If OBJECT has pointer type, the value is
586 assumed to be non-NULL. */
588 tree
589 convert_to_base (tree object, tree type, bool check_access, bool nonnull,
590 tsubst_flags_t complain)
592 tree binfo;
593 tree object_type;
595 if (TYPE_PTR_P (TREE_TYPE (object)))
597 object_type = TREE_TYPE (TREE_TYPE (object));
598 type = TREE_TYPE (type);
600 else
601 object_type = TREE_TYPE (object);
603 binfo = lookup_base (object_type, type, check_access ? ba_check : ba_unique,
604 NULL, complain);
605 if (!binfo || binfo == error_mark_node)
606 return error_mark_node;
608 return build_base_path (PLUS_EXPR, object, binfo, nonnull, complain);
611 /* EXPR is an expression with unqualified class type. BASE is a base
612 binfo of that class type. Returns EXPR, converted to the BASE
613 type. This function assumes that EXPR is the most derived class;
614 therefore virtual bases can be found at their static offsets. */
616 tree
617 convert_to_base_statically (tree expr, tree base)
619 tree expr_type;
621 expr_type = TREE_TYPE (expr);
622 if (!SAME_BINFO_TYPE_P (BINFO_TYPE (base), expr_type))
624 /* If this is a non-empty base, use a COMPONENT_REF. */
625 if (!is_empty_class (BINFO_TYPE (base)))
626 return build_simple_base_path (expr, base);
628 /* We use fold_build2 and fold_convert below to simplify the trees
629 provided to the optimizers. It is not safe to call these functions
630 when processing a template because they do not handle C++-specific
631 trees. */
632 gcc_assert (!processing_template_decl);
633 expr = cp_build_addr_expr (expr, tf_warning_or_error);
634 if (!integer_zerop (BINFO_OFFSET (base)))
635 expr = fold_build_pointer_plus_loc (input_location,
636 expr, BINFO_OFFSET (base));
637 expr = fold_convert (build_pointer_type (BINFO_TYPE (base)), expr);
638 expr = build_fold_indirect_ref_loc (input_location, expr);
641 return expr;
645 tree
646 build_vfield_ref (tree datum, tree type)
648 tree vfield, vcontext;
650 if (datum == error_mark_node
651 /* Can happen in case of duplicate base types (c++/59082). */
652 || !TYPE_VFIELD (type))
653 return error_mark_node;
655 /* First, convert to the requested type. */
656 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (datum), type))
657 datum = convert_to_base (datum, type, /*check_access=*/false,
658 /*nonnull=*/true, tf_warning_or_error);
660 /* Second, the requested type may not be the owner of its own vptr.
661 If not, convert to the base class that owns it. We cannot use
662 convert_to_base here, because VCONTEXT may appear more than once
663 in the inheritance hierarchy of TYPE, and thus direct conversion
664 between the types may be ambiguous. Following the path back up
665 one step at a time via primary bases avoids the problem. */
666 vfield = TYPE_VFIELD (type);
667 vcontext = DECL_CONTEXT (vfield);
668 while (!same_type_ignoring_top_level_qualifiers_p (vcontext, type))
670 datum = build_simple_base_path (datum, CLASSTYPE_PRIMARY_BINFO (type));
671 type = TREE_TYPE (datum);
674 return build3 (COMPONENT_REF, TREE_TYPE (vfield), datum, vfield, NULL_TREE);
677 /* Given an object INSTANCE, return an expression which yields the
678 vtable element corresponding to INDEX. There are many special
679 cases for INSTANCE which we take care of here, mainly to avoid
680 creating extra tree nodes when we don't have to. */
682 static tree
683 build_vtbl_ref_1 (tree instance, tree idx)
685 tree aref;
686 tree vtbl = NULL_TREE;
688 /* Try to figure out what a reference refers to, and
689 access its virtual function table directly. */
691 int cdtorp = 0;
692 tree fixed_type = fixed_type_or_null (instance, NULL, &cdtorp);
694 tree basetype = non_reference (TREE_TYPE (instance));
696 if (fixed_type && !cdtorp)
698 tree binfo = lookup_base (fixed_type, basetype,
699 ba_unique, NULL, tf_none);
700 if (binfo && binfo != error_mark_node)
701 vtbl = unshare_expr (BINFO_VTABLE (binfo));
704 if (!vtbl)
705 vtbl = build_vfield_ref (instance, basetype);
707 aref = build_array_ref (input_location, vtbl, idx);
708 TREE_CONSTANT (aref) |= TREE_CONSTANT (vtbl) && TREE_CONSTANT (idx);
710 return aref;
713 tree
714 build_vtbl_ref (tree instance, tree idx)
716 tree aref = build_vtbl_ref_1 (instance, idx);
718 return aref;
721 /* Given a stable object pointer INSTANCE_PTR, return an expression which
722 yields a function pointer corresponding to vtable element INDEX. */
724 tree
725 build_vfn_ref (tree instance_ptr, tree idx)
727 tree aref;
729 aref = build_vtbl_ref_1 (cp_build_indirect_ref (instance_ptr, RO_NULL,
730 tf_warning_or_error),
731 idx);
733 /* When using function descriptors, the address of the
734 vtable entry is treated as a function pointer. */
735 if (TARGET_VTABLE_USES_DESCRIPTORS)
736 aref = build1 (NOP_EXPR, TREE_TYPE (aref),
737 cp_build_addr_expr (aref, tf_warning_or_error));
739 /* Remember this as a method reference, for later devirtualization. */
740 aref = build3 (OBJ_TYPE_REF, TREE_TYPE (aref), aref, instance_ptr, idx);
742 return aref;
745 /* Return the name of the virtual function table (as an IDENTIFIER_NODE)
746 for the given TYPE. */
748 static tree
749 get_vtable_name (tree type)
751 return mangle_vtbl_for_type (type);
754 /* DECL is an entity associated with TYPE, like a virtual table or an
755 implicitly generated constructor. Determine whether or not DECL
756 should have external or internal linkage at the object file
757 level. This routine does not deal with COMDAT linkage and other
758 similar complexities; it simply sets TREE_PUBLIC if it possible for
759 entities in other translation units to contain copies of DECL, in
760 the abstract. */
762 void
763 set_linkage_according_to_type (tree /*type*/, tree decl)
765 TREE_PUBLIC (decl) = 1;
766 determine_visibility (decl);
769 /* Create a VAR_DECL for a primary or secondary vtable for CLASS_TYPE.
770 (For a secondary vtable for B-in-D, CLASS_TYPE should be D, not B.)
771 Use NAME for the name of the vtable, and VTABLE_TYPE for its type. */
773 static tree
774 build_vtable (tree class_type, tree name, tree vtable_type)
776 tree decl;
778 decl = build_lang_decl (VAR_DECL, name, vtable_type);
779 /* vtable names are already mangled; give them their DECL_ASSEMBLER_NAME
780 now to avoid confusion in mangle_decl. */
781 SET_DECL_ASSEMBLER_NAME (decl, name);
782 DECL_CONTEXT (decl) = class_type;
783 DECL_ARTIFICIAL (decl) = 1;
784 TREE_STATIC (decl) = 1;
785 TREE_READONLY (decl) = 1;
786 DECL_VIRTUAL_P (decl) = 1;
787 DECL_ALIGN (decl) = TARGET_VTABLE_ENTRY_ALIGN;
788 DECL_USER_ALIGN (decl) = true;
789 DECL_VTABLE_OR_VTT_P (decl) = 1;
790 set_linkage_according_to_type (class_type, decl);
791 /* The vtable has not been defined -- yet. */
792 DECL_EXTERNAL (decl) = 1;
793 DECL_NOT_REALLY_EXTERN (decl) = 1;
795 /* Mark the VAR_DECL node representing the vtable itself as a
796 "gratuitous" one, thereby forcing dwarfout.c to ignore it. It
797 is rather important that such things be ignored because any
798 effort to actually generate DWARF for them will run into
799 trouble when/if we encounter code like:
801 #pragma interface
802 struct S { virtual void member (); };
804 because the artificial declaration of the vtable itself (as
805 manufactured by the g++ front end) will say that the vtable is
806 a static member of `S' but only *after* the debug output for
807 the definition of `S' has already been output. This causes
808 grief because the DWARF entry for the definition of the vtable
809 will try to refer back to an earlier *declaration* of the
810 vtable as a static member of `S' and there won't be one. We
811 might be able to arrange to have the "vtable static member"
812 attached to the member list for `S' before the debug info for
813 `S' get written (which would solve the problem) but that would
814 require more intrusive changes to the g++ front end. */
815 DECL_IGNORED_P (decl) = 1;
817 return decl;
820 /* Get the VAR_DECL of the vtable for TYPE. TYPE need not be polymorphic,
821 or even complete. If this does not exist, create it. If COMPLETE is
822 nonzero, then complete the definition of it -- that will render it
823 impossible to actually build the vtable, but is useful to get at those
824 which are known to exist in the runtime. */
826 tree
827 get_vtable_decl (tree type, int complete)
829 tree decl;
831 if (CLASSTYPE_VTABLES (type))
832 return CLASSTYPE_VTABLES (type);
834 decl = build_vtable (type, get_vtable_name (type), vtbl_type_node);
835 CLASSTYPE_VTABLES (type) = decl;
837 if (complete)
839 DECL_EXTERNAL (decl) = 1;
840 cp_finish_decl (decl, NULL_TREE, false, NULL_TREE, 0);
843 return decl;
846 /* Build the primary virtual function table for TYPE. If BINFO is
847 non-NULL, build the vtable starting with the initial approximation
848 that it is the same as the one which is the head of the association
849 list. Returns a nonzero value if a new vtable is actually
850 created. */
852 static int
853 build_primary_vtable (tree binfo, tree type)
855 tree decl;
856 tree virtuals;
858 decl = get_vtable_decl (type, /*complete=*/0);
860 if (binfo)
862 if (BINFO_NEW_VTABLE_MARKED (binfo))
863 /* We have already created a vtable for this base, so there's
864 no need to do it again. */
865 return 0;
867 virtuals = copy_list (BINFO_VIRTUALS (binfo));
868 TREE_TYPE (decl) = TREE_TYPE (get_vtbl_decl_for_binfo (binfo));
869 DECL_SIZE (decl) = TYPE_SIZE (TREE_TYPE (decl));
870 DECL_SIZE_UNIT (decl) = TYPE_SIZE_UNIT (TREE_TYPE (decl));
872 else
874 gcc_assert (TREE_TYPE (decl) == vtbl_type_node);
875 virtuals = NULL_TREE;
878 if (GATHER_STATISTICS)
880 n_vtables += 1;
881 n_vtable_elems += list_length (virtuals);
884 /* Initialize the association list for this type, based
885 on our first approximation. */
886 BINFO_VTABLE (TYPE_BINFO (type)) = decl;
887 BINFO_VIRTUALS (TYPE_BINFO (type)) = virtuals;
888 SET_BINFO_NEW_VTABLE_MARKED (TYPE_BINFO (type));
889 return 1;
892 /* Give BINFO a new virtual function table which is initialized
893 with a skeleton-copy of its original initialization. The only
894 entry that changes is the `delta' entry, so we can really
895 share a lot of structure.
897 FOR_TYPE is the most derived type which caused this table to
898 be needed.
900 Returns nonzero if we haven't met BINFO before.
902 The order in which vtables are built (by calling this function) for
903 an object must remain the same, otherwise a binary incompatibility
904 can result. */
906 static int
907 build_secondary_vtable (tree binfo)
909 if (BINFO_NEW_VTABLE_MARKED (binfo))
910 /* We already created a vtable for this base. There's no need to
911 do it again. */
912 return 0;
914 /* Remember that we've created a vtable for this BINFO, so that we
915 don't try to do so again. */
916 SET_BINFO_NEW_VTABLE_MARKED (binfo);
918 /* Make fresh virtual list, so we can smash it later. */
919 BINFO_VIRTUALS (binfo) = copy_list (BINFO_VIRTUALS (binfo));
921 /* Secondary vtables are laid out as part of the same structure as
922 the primary vtable. */
923 BINFO_VTABLE (binfo) = NULL_TREE;
924 return 1;
927 /* Create a new vtable for BINFO which is the hierarchy dominated by
928 T. Return nonzero if we actually created a new vtable. */
930 static int
931 make_new_vtable (tree t, tree binfo)
933 if (binfo == TYPE_BINFO (t))
934 /* In this case, it is *type*'s vtable we are modifying. We start
935 with the approximation that its vtable is that of the
936 immediate base class. */
937 return build_primary_vtable (binfo, t);
938 else
939 /* This is our very own copy of `basetype' to play with. Later,
940 we will fill in all the virtual functions that override the
941 virtual functions in these base classes which are not defined
942 by the current type. */
943 return build_secondary_vtable (binfo);
946 /* Make *VIRTUALS, an entry on the BINFO_VIRTUALS list for BINFO
947 (which is in the hierarchy dominated by T) list FNDECL as its
948 BV_FN. DELTA is the required constant adjustment from the `this'
949 pointer where the vtable entry appears to the `this' required when
950 the function is actually called. */
952 static void
953 modify_vtable_entry (tree t,
954 tree binfo,
955 tree fndecl,
956 tree delta,
957 tree *virtuals)
959 tree v;
961 v = *virtuals;
963 if (fndecl != BV_FN (v)
964 || !tree_int_cst_equal (delta, BV_DELTA (v)))
966 /* We need a new vtable for BINFO. */
967 if (make_new_vtable (t, binfo))
969 /* If we really did make a new vtable, we also made a copy
970 of the BINFO_VIRTUALS list. Now, we have to find the
971 corresponding entry in that list. */
972 *virtuals = BINFO_VIRTUALS (binfo);
973 while (BV_FN (*virtuals) != BV_FN (v))
974 *virtuals = TREE_CHAIN (*virtuals);
975 v = *virtuals;
978 BV_DELTA (v) = delta;
979 BV_VCALL_INDEX (v) = NULL_TREE;
980 BV_FN (v) = fndecl;
985 /* Add method METHOD to class TYPE. If USING_DECL is non-null, it is
986 the USING_DECL naming METHOD. Returns true if the method could be
987 added to the method vec. */
989 bool
990 add_method (tree type, tree method, tree using_decl)
992 unsigned slot;
993 tree overload;
994 bool template_conv_p = false;
995 bool conv_p;
996 vec<tree, va_gc> *method_vec;
997 bool complete_p;
998 bool insert_p = false;
999 tree current_fns;
1000 tree fns;
1002 if (method == error_mark_node)
1003 return false;
1005 complete_p = COMPLETE_TYPE_P (type);
1006 conv_p = DECL_CONV_FN_P (method);
1007 if (conv_p)
1008 template_conv_p = (TREE_CODE (method) == TEMPLATE_DECL
1009 && DECL_TEMPLATE_CONV_FN_P (method));
1011 method_vec = CLASSTYPE_METHOD_VEC (type);
1012 if (!method_vec)
1014 /* Make a new method vector. We start with 8 entries. We must
1015 allocate at least two (for constructors and destructors), and
1016 we're going to end up with an assignment operator at some
1017 point as well. */
1018 vec_alloc (method_vec, 8);
1019 /* Create slots for constructors and destructors. */
1020 method_vec->quick_push (NULL_TREE);
1021 method_vec->quick_push (NULL_TREE);
1022 CLASSTYPE_METHOD_VEC (type) = method_vec;
1025 /* Maintain TYPE_HAS_USER_CONSTRUCTOR, etc. */
1026 grok_special_member_properties (method);
1028 /* Constructors and destructors go in special slots. */
1029 if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (method))
1030 slot = CLASSTYPE_CONSTRUCTOR_SLOT;
1031 else if (DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (method))
1033 slot = CLASSTYPE_DESTRUCTOR_SLOT;
1035 if (TYPE_FOR_JAVA (type))
1037 if (!DECL_ARTIFICIAL (method))
1038 error ("Java class %qT cannot have a destructor", type);
1039 else if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
1040 error ("Java class %qT cannot have an implicit non-trivial "
1041 "destructor",
1042 type);
1045 else
1047 tree m;
1049 insert_p = true;
1050 /* See if we already have an entry with this name. */
1051 for (slot = CLASSTYPE_FIRST_CONVERSION_SLOT;
1052 vec_safe_iterate (method_vec, slot, &m);
1053 ++slot)
1055 m = OVL_CURRENT (m);
1056 if (template_conv_p)
1058 if (TREE_CODE (m) == TEMPLATE_DECL
1059 && DECL_TEMPLATE_CONV_FN_P (m))
1060 insert_p = false;
1061 break;
1063 if (conv_p && !DECL_CONV_FN_P (m))
1064 break;
1065 if (DECL_NAME (m) == DECL_NAME (method))
1067 insert_p = false;
1068 break;
1070 if (complete_p
1071 && !DECL_CONV_FN_P (m)
1072 && DECL_NAME (m) > DECL_NAME (method))
1073 break;
1076 current_fns = insert_p ? NULL_TREE : (*method_vec)[slot];
1078 /* Check to see if we've already got this method. */
1079 for (fns = current_fns; fns; fns = OVL_NEXT (fns))
1081 tree fn = OVL_CURRENT (fns);
1082 tree fn_type;
1083 tree method_type;
1084 tree parms1;
1085 tree parms2;
1087 if (TREE_CODE (fn) != TREE_CODE (method))
1088 continue;
1090 /* [over.load] Member function declarations with the
1091 same name and the same parameter types cannot be
1092 overloaded if any of them is a static member
1093 function declaration.
1095 [over.load] Member function declarations with the same name and
1096 the same parameter-type-list as well as member function template
1097 declarations with the same name, the same parameter-type-list, and
1098 the same template parameter lists cannot be overloaded if any of
1099 them, but not all, have a ref-qualifier.
1101 [namespace.udecl] When a using-declaration brings names
1102 from a base class into a derived class scope, member
1103 functions in the derived class override and/or hide member
1104 functions with the same name and parameter types in a base
1105 class (rather than conflicting). */
1106 fn_type = TREE_TYPE (fn);
1107 method_type = TREE_TYPE (method);
1108 parms1 = TYPE_ARG_TYPES (fn_type);
1109 parms2 = TYPE_ARG_TYPES (method_type);
1111 /* Compare the quals on the 'this' parm. Don't compare
1112 the whole types, as used functions are treated as
1113 coming from the using class in overload resolution. */
1114 if (! DECL_STATIC_FUNCTION_P (fn)
1115 && ! DECL_STATIC_FUNCTION_P (method)
1116 /* Either both or neither need to be ref-qualified for
1117 differing quals to allow overloading. */
1118 && (FUNCTION_REF_QUALIFIED (fn_type)
1119 == FUNCTION_REF_QUALIFIED (method_type))
1120 && (type_memfn_quals (fn_type) != type_memfn_quals (method_type)
1121 || type_memfn_rqual (fn_type) != type_memfn_rqual (method_type)))
1122 continue;
1124 /* For templates, the return type and template parameters
1125 must be identical. */
1126 if (TREE_CODE (fn) == TEMPLATE_DECL
1127 && (!same_type_p (TREE_TYPE (fn_type),
1128 TREE_TYPE (method_type))
1129 || !comp_template_parms (DECL_TEMPLATE_PARMS (fn),
1130 DECL_TEMPLATE_PARMS (method))))
1131 continue;
1133 if (! DECL_STATIC_FUNCTION_P (fn))
1134 parms1 = TREE_CHAIN (parms1);
1135 if (! DECL_STATIC_FUNCTION_P (method))
1136 parms2 = TREE_CHAIN (parms2);
1138 if (compparms (parms1, parms2)
1139 && (!DECL_CONV_FN_P (fn)
1140 || same_type_p (TREE_TYPE (fn_type),
1141 TREE_TYPE (method_type))))
1143 /* For function versions, their parms and types match
1144 but they are not duplicates. Record function versions
1145 as and when they are found. extern "C" functions are
1146 not treated as versions. */
1147 if (TREE_CODE (fn) == FUNCTION_DECL
1148 && TREE_CODE (method) == FUNCTION_DECL
1149 && !DECL_EXTERN_C_P (fn)
1150 && !DECL_EXTERN_C_P (method)
1151 && targetm.target_option.function_versions (fn, method))
1153 /* Mark functions as versions if necessary. Modify the mangled
1154 decl name if necessary. */
1155 if (!DECL_FUNCTION_VERSIONED (fn))
1157 DECL_FUNCTION_VERSIONED (fn) = 1;
1158 if (DECL_ASSEMBLER_NAME_SET_P (fn))
1159 mangle_decl (fn);
1161 if (!DECL_FUNCTION_VERSIONED (method))
1163 DECL_FUNCTION_VERSIONED (method) = 1;
1164 if (DECL_ASSEMBLER_NAME_SET_P (method))
1165 mangle_decl (method);
1167 cgraph_node::record_function_versions (fn, method);
1168 continue;
1170 if (DECL_INHERITED_CTOR_BASE (method))
1172 if (DECL_INHERITED_CTOR_BASE (fn))
1174 error_at (DECL_SOURCE_LOCATION (method),
1175 "%q#D inherited from %qT", method,
1176 DECL_INHERITED_CTOR_BASE (method));
1177 error_at (DECL_SOURCE_LOCATION (fn),
1178 "conflicts with version inherited from %qT",
1179 DECL_INHERITED_CTOR_BASE (fn));
1181 /* Otherwise defer to the other function. */
1182 return false;
1184 if (using_decl)
1186 if (DECL_CONTEXT (fn) == type)
1187 /* Defer to the local function. */
1188 return false;
1190 else
1192 error ("%q+#D cannot be overloaded", method);
1193 error ("with %q+#D", fn);
1196 /* We don't call duplicate_decls here to merge the
1197 declarations because that will confuse things if the
1198 methods have inline definitions. In particular, we
1199 will crash while processing the definitions. */
1200 return false;
1204 /* A class should never have more than one destructor. */
1205 if (current_fns && DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (method))
1206 return false;
1208 /* Add the new binding. */
1209 if (using_decl)
1211 overload = ovl_cons (method, current_fns);
1212 OVL_USED (overload) = true;
1214 else
1215 overload = build_overload (method, current_fns);
1217 if (conv_p)
1218 TYPE_HAS_CONVERSION (type) = 1;
1219 else if (slot >= CLASSTYPE_FIRST_CONVERSION_SLOT && !complete_p)
1220 push_class_level_binding (DECL_NAME (method), overload);
1222 if (insert_p)
1224 bool reallocated;
1226 /* We only expect to add few methods in the COMPLETE_P case, so
1227 just make room for one more method in that case. */
1228 if (complete_p)
1229 reallocated = vec_safe_reserve_exact (method_vec, 1);
1230 else
1231 reallocated = vec_safe_reserve (method_vec, 1);
1232 if (reallocated)
1233 CLASSTYPE_METHOD_VEC (type) = method_vec;
1234 if (slot == method_vec->length ())
1235 method_vec->quick_push (overload);
1236 else
1237 method_vec->quick_insert (slot, overload);
1239 else
1240 /* Replace the current slot. */
1241 (*method_vec)[slot] = overload;
1242 return true;
1245 /* Subroutines of finish_struct. */
1247 /* Change the access of FDECL to ACCESS in T. Return 1 if change was
1248 legit, otherwise return 0. */
1250 static int
1251 alter_access (tree t, tree fdecl, tree access)
1253 tree elem;
1255 if (!DECL_LANG_SPECIFIC (fdecl))
1256 retrofit_lang_decl (fdecl);
1258 gcc_assert (!DECL_DISCRIMINATOR_P (fdecl));
1260 elem = purpose_member (t, DECL_ACCESS (fdecl));
1261 if (elem)
1263 if (TREE_VALUE (elem) != access)
1265 if (TREE_CODE (TREE_TYPE (fdecl)) == FUNCTION_DECL)
1266 error ("conflicting access specifications for method"
1267 " %q+D, ignored", TREE_TYPE (fdecl));
1268 else
1269 error ("conflicting access specifications for field %qE, ignored",
1270 DECL_NAME (fdecl));
1272 else
1274 /* They're changing the access to the same thing they changed
1275 it to before. That's OK. */
1279 else
1281 perform_or_defer_access_check (TYPE_BINFO (t), fdecl, fdecl,
1282 tf_warning_or_error);
1283 DECL_ACCESS (fdecl) = tree_cons (t, access, DECL_ACCESS (fdecl));
1284 return 1;
1286 return 0;
1289 /* Process the USING_DECL, which is a member of T. */
1291 static void
1292 handle_using_decl (tree using_decl, tree t)
1294 tree decl = USING_DECL_DECLS (using_decl);
1295 tree name = DECL_NAME (using_decl);
1296 tree access
1297 = TREE_PRIVATE (using_decl) ? access_private_node
1298 : TREE_PROTECTED (using_decl) ? access_protected_node
1299 : access_public_node;
1300 tree flist = NULL_TREE;
1301 tree old_value;
1303 gcc_assert (!processing_template_decl && decl);
1305 old_value = lookup_member (t, name, /*protect=*/0, /*want_type=*/false,
1306 tf_warning_or_error);
1307 if (old_value)
1309 if (is_overloaded_fn (old_value))
1310 old_value = OVL_CURRENT (old_value);
1312 if (DECL_P (old_value) && DECL_CONTEXT (old_value) == t)
1313 /* OK */;
1314 else
1315 old_value = NULL_TREE;
1318 cp_emit_debug_info_for_using (decl, t);
1320 if (is_overloaded_fn (decl))
1321 flist = decl;
1323 if (! old_value)
1325 else if (is_overloaded_fn (old_value))
1327 if (flist)
1328 /* It's OK to use functions from a base when there are functions with
1329 the same name already present in the current class. */;
1330 else
1332 error ("%q+D invalid in %q#T", using_decl, t);
1333 error (" because of local method %q+#D with same name",
1334 OVL_CURRENT (old_value));
1335 return;
1338 else if (!DECL_ARTIFICIAL (old_value))
1340 error ("%q+D invalid in %q#T", using_decl, t);
1341 error (" because of local member %q+#D with same name", old_value);
1342 return;
1345 /* Make type T see field decl FDECL with access ACCESS. */
1346 if (flist)
1347 for (; flist; flist = OVL_NEXT (flist))
1349 add_method (t, OVL_CURRENT (flist), using_decl);
1350 alter_access (t, OVL_CURRENT (flist), access);
1352 else
1353 alter_access (t, decl, access);
1356 /* Data structure for find_abi_tags_r, below. */
1358 struct abi_tag_data
1360 tree t; // The type that we're checking for missing tags.
1361 tree subob; // The subobject of T that we're getting tags from.
1362 tree tags; // error_mark_node for diagnostics, or a list of missing tags.
1365 /* Subroutine of find_abi_tags_r. Handle a single TAG found on the class TP
1366 in the context of P. TAG can be either an identifier (the DECL_NAME of
1367 a tag NAMESPACE_DECL) or a STRING_CST (a tag attribute). */
1369 static void
1370 check_tag (tree tag, tree id, tree *tp, abi_tag_data *p)
1372 if (!IDENTIFIER_MARKED (id))
1374 if (p->tags != error_mark_node)
1376 /* We're collecting tags from template arguments or from
1377 the type of a variable or function return type. */
1378 p->tags = tree_cons (NULL_TREE, tag, p->tags);
1380 /* Don't inherit this tag multiple times. */
1381 IDENTIFIER_MARKED (id) = true;
1383 if (TYPE_P (p->t))
1385 /* Tags inherited from type template arguments are only used
1386 to avoid warnings. */
1387 ABI_TAG_IMPLICIT (p->tags) = true;
1388 return;
1390 /* For functions and variables we want to warn, too. */
1393 /* Otherwise we're diagnosing missing tags. */
1394 if (TREE_CODE (p->t) == FUNCTION_DECL)
1396 if (warning (OPT_Wabi_tag, "%qD inherits the %E ABI tag "
1397 "that %qT (used in its return type) has",
1398 p->t, tag, *tp))
1399 inform (location_of (*tp), "%qT declared here", *tp);
1401 else if (VAR_P (p->t))
1403 if (warning (OPT_Wabi_tag, "%qD inherits the %E ABI tag "
1404 "that %qT (used in its type) has", p->t, tag, *tp))
1405 inform (location_of (*tp), "%qT declared here", *tp);
1407 else if (TYPE_P (p->subob))
1409 if (warning (OPT_Wabi_tag, "%qT does not have the %E ABI tag "
1410 "that base %qT has", p->t, tag, p->subob))
1411 inform (location_of (p->subob), "%qT declared here",
1412 p->subob);
1414 else
1416 if (warning (OPT_Wabi_tag, "%qT does not have the %E ABI tag "
1417 "that %qT (used in the type of %qD) has",
1418 p->t, tag, *tp, p->subob))
1420 inform (location_of (p->subob), "%qD declared here",
1421 p->subob);
1422 inform (location_of (*tp), "%qT declared here", *tp);
1428 /* Find all the ABI tags in the attribute list ATTR and either call
1429 check_tag (if TP is non-null) or set IDENTIFIER_MARKED to val. */
1431 static void
1432 mark_or_check_attr_tags (tree attr, tree *tp, abi_tag_data *p, bool val)
1434 if (!attr)
1435 return;
1436 for (; (attr = lookup_attribute ("abi_tag", attr));
1437 attr = TREE_CHAIN (attr))
1438 for (tree list = TREE_VALUE (attr); list;
1439 list = TREE_CHAIN (list))
1441 tree tag = TREE_VALUE (list);
1442 tree id = get_identifier (TREE_STRING_POINTER (tag));
1443 if (tp)
1444 check_tag (tag, id, tp, p);
1445 else
1446 IDENTIFIER_MARKED (id) = val;
1450 /* Find all the ABI tags on T and its enclosing scopes and either call
1451 check_tag (if TP is non-null) or set IDENTIFIER_MARKED to val. */
1453 static void
1454 mark_or_check_tags (tree t, tree *tp, abi_tag_data *p, bool val)
1456 while (t != global_namespace)
1458 tree attr;
1459 if (TYPE_P (t))
1461 attr = TYPE_ATTRIBUTES (t);
1462 t = CP_TYPE_CONTEXT (t);
1464 else
1466 attr = DECL_ATTRIBUTES (t);
1467 t = CP_DECL_CONTEXT (t);
1469 mark_or_check_attr_tags (attr, tp, p, val);
1473 /* walk_tree callback for check_abi_tags: if the type at *TP involves any
1474 types with ABI tags, add the corresponding identifiers to the VEC in
1475 *DATA and set IDENTIFIER_MARKED. */
1477 static tree
1478 find_abi_tags_r (tree *tp, int *walk_subtrees, void *data)
1480 if (!OVERLOAD_TYPE_P (*tp))
1481 return NULL_TREE;
1483 /* walk_tree shouldn't be walking into any subtrees of a RECORD_TYPE
1484 anyway, but let's make sure of it. */
1485 *walk_subtrees = false;
1487 abi_tag_data *p = static_cast<struct abi_tag_data*>(data);
1489 mark_or_check_tags (*tp, tp, p, false);
1491 return NULL_TREE;
1494 /* walk_tree callback for mark_abi_tags: if *TP is a class, set
1495 IDENTIFIER_MARKED on its ABI tags. */
1497 static tree
1498 mark_abi_tags_r (tree *tp, int *walk_subtrees, void *data)
1500 if (!OVERLOAD_TYPE_P (*tp))
1501 return NULL_TREE;
1503 /* walk_tree shouldn't be walking into any subtrees of a RECORD_TYPE
1504 anyway, but let's make sure of it. */
1505 *walk_subtrees = false;
1507 bool *valp = static_cast<bool*>(data);
1509 mark_or_check_tags (*tp, NULL, NULL, *valp);
1511 return NULL_TREE;
1514 /* Set IDENTIFIER_MARKED on all the ABI tags on T and its enclosing
1515 scopes. */
1517 static void
1518 mark_abi_tags (tree t, bool val)
1520 mark_or_check_tags (t, NULL, NULL, val);
1521 if (DECL_P (t))
1523 if (DECL_LANG_SPECIFIC (t) && DECL_USE_TEMPLATE (t)
1524 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (t)))
1526 /* Template arguments are part of the signature. */
1527 tree level = INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (t));
1528 for (int j = 0; j < TREE_VEC_LENGTH (level); ++j)
1530 tree arg = TREE_VEC_ELT (level, j);
1531 cp_walk_tree_without_duplicates (&arg, mark_abi_tags_r, &val);
1534 if (TREE_CODE (t) == FUNCTION_DECL)
1535 /* A function's parameter types are part of the signature, so
1536 we don't need to inherit any tags that are also in them. */
1537 for (tree arg = FUNCTION_FIRST_USER_PARMTYPE (t); arg;
1538 arg = TREE_CHAIN (arg))
1539 cp_walk_tree_without_duplicates (&TREE_VALUE (arg),
1540 mark_abi_tags_r, &val);
1544 /* Check that T has all the ABI tags that subobject SUBOB has, or
1545 warn if not. If T is a (variable or function) declaration, also
1546 add any missing tags. */
1548 static void
1549 check_abi_tags (tree t, tree subob)
1551 bool inherit = DECL_P (t);
1553 if (!inherit && !warn_abi_tag)
1554 return;
1556 tree decl = TYPE_P (t) ? TYPE_NAME (t) : t;
1557 if (!TREE_PUBLIC (decl))
1558 /* No need to worry about things local to this TU. */
1559 return;
1561 mark_abi_tags (t, true);
1563 tree subtype = TYPE_P (subob) ? subob : TREE_TYPE (subob);
1564 struct abi_tag_data data = { t, subob, error_mark_node };
1565 if (inherit)
1566 data.tags = NULL_TREE;
1568 cp_walk_tree_without_duplicates (&subtype, find_abi_tags_r, &data);
1570 if (inherit && data.tags)
1572 tree attr = lookup_attribute ("abi_tag", DECL_ATTRIBUTES (t));
1573 if (attr)
1574 TREE_VALUE (attr) = chainon (data.tags, TREE_VALUE (attr));
1575 else
1576 DECL_ATTRIBUTES (t)
1577 = tree_cons (get_identifier ("abi_tag"), data.tags,
1578 DECL_ATTRIBUTES (t));
1581 mark_abi_tags (t, false);
1584 /* Check that DECL has all the ABI tags that are used in parts of its type
1585 that are not reflected in its mangled name. */
1587 void
1588 check_abi_tags (tree decl)
1590 if (VAR_P (decl))
1591 check_abi_tags (decl, TREE_TYPE (decl));
1592 else if (TREE_CODE (decl) == FUNCTION_DECL
1593 && !mangle_return_type_p (decl))
1594 check_abi_tags (decl, TREE_TYPE (TREE_TYPE (decl)));
1597 void
1598 inherit_targ_abi_tags (tree t)
1600 if (!CLASS_TYPE_P (t)
1601 || CLASSTYPE_TEMPLATE_INFO (t) == NULL_TREE)
1602 return;
1604 mark_abi_tags (t, true);
1606 tree args = CLASSTYPE_TI_ARGS (t);
1607 struct abi_tag_data data = { t, NULL_TREE, NULL_TREE };
1608 for (int i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
1610 tree level = TMPL_ARGS_LEVEL (args, i+1);
1611 for (int j = 0; j < TREE_VEC_LENGTH (level); ++j)
1613 tree arg = TREE_VEC_ELT (level, j);
1614 data.subob = arg;
1615 cp_walk_tree_without_duplicates (&arg, find_abi_tags_r, &data);
1619 // If we found some tags on our template arguments, add them to our
1620 // abi_tag attribute.
1621 if (data.tags)
1623 tree attr = lookup_attribute ("abi_tag", TYPE_ATTRIBUTES (t));
1624 if (attr)
1625 TREE_VALUE (attr) = chainon (data.tags, TREE_VALUE (attr));
1626 else
1627 TYPE_ATTRIBUTES (t)
1628 = tree_cons (get_identifier ("abi_tag"), data.tags,
1629 TYPE_ATTRIBUTES (t));
1632 mark_abi_tags (t, false);
1635 /* Return true, iff class T has a non-virtual destructor that is
1636 accessible from outside the class heirarchy (i.e. is public, or
1637 there's a suitable friend. */
1639 static bool
1640 accessible_nvdtor_p (tree t)
1642 tree dtor = CLASSTYPE_DESTRUCTORS (t);
1644 /* An implicitly declared destructor is always public. And,
1645 if it were virtual, we would have created it by now. */
1646 if (!dtor)
1647 return true;
1649 if (DECL_VINDEX (dtor))
1650 return false; /* Virtual */
1652 if (!TREE_PRIVATE (dtor) && !TREE_PROTECTED (dtor))
1653 return true; /* Public */
1655 if (CLASSTYPE_FRIEND_CLASSES (t)
1656 || DECL_FRIENDLIST (TYPE_MAIN_DECL (t)))
1657 return true; /* Has friends */
1659 return false;
1662 /* Run through the base classes of T, updating CANT_HAVE_CONST_CTOR_P,
1663 and NO_CONST_ASN_REF_P. Also set flag bits in T based on
1664 properties of the bases. */
1666 static void
1667 check_bases (tree t,
1668 int* cant_have_const_ctor_p,
1669 int* no_const_asn_ref_p)
1671 int i;
1672 bool seen_non_virtual_nearly_empty_base_p = 0;
1673 int seen_tm_mask = 0;
1674 tree base_binfo;
1675 tree binfo;
1676 tree field = NULL_TREE;
1678 if (!CLASSTYPE_NON_STD_LAYOUT (t))
1679 for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
1680 if (TREE_CODE (field) == FIELD_DECL)
1681 break;
1683 for (binfo = TYPE_BINFO (t), i = 0;
1684 BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
1686 tree basetype = TREE_TYPE (base_binfo);
1688 gcc_assert (COMPLETE_TYPE_P (basetype));
1690 if (CLASSTYPE_FINAL (basetype))
1691 error ("cannot derive from %<final%> base %qT in derived type %qT",
1692 basetype, t);
1694 /* If any base class is non-literal, so is the derived class. */
1695 if (!CLASSTYPE_LITERAL_P (basetype))
1696 CLASSTYPE_LITERAL_P (t) = false;
1698 /* If the base class doesn't have copy constructors or
1699 assignment operators that take const references, then the
1700 derived class cannot have such a member automatically
1701 generated. */
1702 if (TYPE_HAS_COPY_CTOR (basetype)
1703 && ! TYPE_HAS_CONST_COPY_CTOR (basetype))
1704 *cant_have_const_ctor_p = 1;
1705 if (TYPE_HAS_COPY_ASSIGN (basetype)
1706 && !TYPE_HAS_CONST_COPY_ASSIGN (basetype))
1707 *no_const_asn_ref_p = 1;
1709 if (BINFO_VIRTUAL_P (base_binfo))
1710 /* A virtual base does not effect nearly emptiness. */
1712 else if (CLASSTYPE_NEARLY_EMPTY_P (basetype))
1714 if (seen_non_virtual_nearly_empty_base_p)
1715 /* And if there is more than one nearly empty base, then the
1716 derived class is not nearly empty either. */
1717 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
1718 else
1719 /* Remember we've seen one. */
1720 seen_non_virtual_nearly_empty_base_p = 1;
1722 else if (!is_empty_class (basetype))
1723 /* If the base class is not empty or nearly empty, then this
1724 class cannot be nearly empty. */
1725 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
1727 /* A lot of properties from the bases also apply to the derived
1728 class. */
1729 TYPE_NEEDS_CONSTRUCTING (t) |= TYPE_NEEDS_CONSTRUCTING (basetype);
1730 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t)
1731 |= TYPE_HAS_NONTRIVIAL_DESTRUCTOR (basetype);
1732 TYPE_HAS_COMPLEX_COPY_ASSIGN (t)
1733 |= (TYPE_HAS_COMPLEX_COPY_ASSIGN (basetype)
1734 || !TYPE_HAS_COPY_ASSIGN (basetype));
1735 TYPE_HAS_COMPLEX_COPY_CTOR (t) |= (TYPE_HAS_COMPLEX_COPY_CTOR (basetype)
1736 || !TYPE_HAS_COPY_CTOR (basetype));
1737 TYPE_HAS_COMPLEX_MOVE_ASSIGN (t)
1738 |= TYPE_HAS_COMPLEX_MOVE_ASSIGN (basetype);
1739 TYPE_HAS_COMPLEX_MOVE_CTOR (t) |= TYPE_HAS_COMPLEX_MOVE_CTOR (basetype);
1740 TYPE_POLYMORPHIC_P (t) |= TYPE_POLYMORPHIC_P (basetype);
1741 CLASSTYPE_CONTAINS_EMPTY_CLASS_P (t)
1742 |= CLASSTYPE_CONTAINS_EMPTY_CLASS_P (basetype);
1743 TYPE_HAS_COMPLEX_DFLT (t) |= (!TYPE_HAS_DEFAULT_CONSTRUCTOR (basetype)
1744 || TYPE_HAS_COMPLEX_DFLT (basetype));
1745 SET_CLASSTYPE_READONLY_FIELDS_NEED_INIT
1746 (t, CLASSTYPE_READONLY_FIELDS_NEED_INIT (t)
1747 | CLASSTYPE_READONLY_FIELDS_NEED_INIT (basetype));
1748 SET_CLASSTYPE_REF_FIELDS_NEED_INIT
1749 (t, CLASSTYPE_REF_FIELDS_NEED_INIT (t)
1750 | CLASSTYPE_REF_FIELDS_NEED_INIT (basetype));
1752 /* A standard-layout class is a class that:
1754 * has no non-standard-layout base classes, */
1755 CLASSTYPE_NON_STD_LAYOUT (t) |= CLASSTYPE_NON_STD_LAYOUT (basetype);
1756 if (!CLASSTYPE_NON_STD_LAYOUT (t))
1758 tree basefield;
1759 /* ...has no base classes of the same type as the first non-static
1760 data member... */
1761 if (field && DECL_CONTEXT (field) == t
1762 && (same_type_ignoring_top_level_qualifiers_p
1763 (TREE_TYPE (field), basetype)))
1764 CLASSTYPE_NON_STD_LAYOUT (t) = 1;
1765 else
1766 /* ...either has no non-static data members in the most-derived
1767 class and at most one base class with non-static data
1768 members, or has no base classes with non-static data
1769 members */
1770 for (basefield = TYPE_FIELDS (basetype); basefield;
1771 basefield = DECL_CHAIN (basefield))
1772 if (TREE_CODE (basefield) == FIELD_DECL)
1774 if (field)
1775 CLASSTYPE_NON_STD_LAYOUT (t) = 1;
1776 else
1777 field = basefield;
1778 break;
1782 /* Don't bother collecting tm attributes if transactional memory
1783 support is not enabled. */
1784 if (flag_tm)
1786 tree tm_attr = find_tm_attribute (TYPE_ATTRIBUTES (basetype));
1787 if (tm_attr)
1788 seen_tm_mask |= tm_attr_to_mask (tm_attr);
1791 check_abi_tags (t, basetype);
1794 /* If one of the base classes had TM attributes, and the current class
1795 doesn't define its own, then the current class inherits one. */
1796 if (seen_tm_mask && !find_tm_attribute (TYPE_ATTRIBUTES (t)))
1798 tree tm_attr = tm_mask_to_attr (seen_tm_mask & -seen_tm_mask);
1799 TYPE_ATTRIBUTES (t) = tree_cons (tm_attr, NULL, TYPE_ATTRIBUTES (t));
1803 /* Determine all the primary bases within T. Sets BINFO_PRIMARY_BASE_P for
1804 those that are primaries. Sets BINFO_LOST_PRIMARY_P for those
1805 that have had a nearly-empty virtual primary base stolen by some
1806 other base in the hierarchy. Determines CLASSTYPE_PRIMARY_BASE for
1807 T. */
1809 static void
1810 determine_primary_bases (tree t)
1812 unsigned i;
1813 tree primary = NULL_TREE;
1814 tree type_binfo = TYPE_BINFO (t);
1815 tree base_binfo;
1817 /* Determine the primary bases of our bases. */
1818 for (base_binfo = TREE_CHAIN (type_binfo); base_binfo;
1819 base_binfo = TREE_CHAIN (base_binfo))
1821 tree primary = CLASSTYPE_PRIMARY_BINFO (BINFO_TYPE (base_binfo));
1823 /* See if we're the non-virtual primary of our inheritance
1824 chain. */
1825 if (!BINFO_VIRTUAL_P (base_binfo))
1827 tree parent = BINFO_INHERITANCE_CHAIN (base_binfo);
1828 tree parent_primary = CLASSTYPE_PRIMARY_BINFO (BINFO_TYPE (parent));
1830 if (parent_primary
1831 && SAME_BINFO_TYPE_P (BINFO_TYPE (base_binfo),
1832 BINFO_TYPE (parent_primary)))
1833 /* We are the primary binfo. */
1834 BINFO_PRIMARY_P (base_binfo) = 1;
1836 /* Determine if we have a virtual primary base, and mark it so.
1838 if (primary && BINFO_VIRTUAL_P (primary))
1840 tree this_primary = copied_binfo (primary, base_binfo);
1842 if (BINFO_PRIMARY_P (this_primary))
1843 /* Someone already claimed this base. */
1844 BINFO_LOST_PRIMARY_P (base_binfo) = 1;
1845 else
1847 tree delta;
1849 BINFO_PRIMARY_P (this_primary) = 1;
1850 BINFO_INHERITANCE_CHAIN (this_primary) = base_binfo;
1852 /* A virtual binfo might have been copied from within
1853 another hierarchy. As we're about to use it as a
1854 primary base, make sure the offsets match. */
1855 delta = size_diffop_loc (input_location,
1856 convert (ssizetype,
1857 BINFO_OFFSET (base_binfo)),
1858 convert (ssizetype,
1859 BINFO_OFFSET (this_primary)));
1861 propagate_binfo_offsets (this_primary, delta);
1866 /* First look for a dynamic direct non-virtual base. */
1867 for (i = 0; BINFO_BASE_ITERATE (type_binfo, i, base_binfo); i++)
1869 tree basetype = BINFO_TYPE (base_binfo);
1871 if (TYPE_CONTAINS_VPTR_P (basetype) && !BINFO_VIRTUAL_P (base_binfo))
1873 primary = base_binfo;
1874 goto found;
1878 /* A "nearly-empty" virtual base class can be the primary base
1879 class, if no non-virtual polymorphic base can be found. Look for
1880 a nearly-empty virtual dynamic base that is not already a primary
1881 base of something in the hierarchy. If there is no such base,
1882 just pick the first nearly-empty virtual base. */
1884 for (base_binfo = TREE_CHAIN (type_binfo); base_binfo;
1885 base_binfo = TREE_CHAIN (base_binfo))
1886 if (BINFO_VIRTUAL_P (base_binfo)
1887 && CLASSTYPE_NEARLY_EMPTY_P (BINFO_TYPE (base_binfo)))
1889 if (!BINFO_PRIMARY_P (base_binfo))
1891 /* Found one that is not primary. */
1892 primary = base_binfo;
1893 goto found;
1895 else if (!primary)
1896 /* Remember the first candidate. */
1897 primary = base_binfo;
1900 found:
1901 /* If we've got a primary base, use it. */
1902 if (primary)
1904 tree basetype = BINFO_TYPE (primary);
1906 CLASSTYPE_PRIMARY_BINFO (t) = primary;
1907 if (BINFO_PRIMARY_P (primary))
1908 /* We are stealing a primary base. */
1909 BINFO_LOST_PRIMARY_P (BINFO_INHERITANCE_CHAIN (primary)) = 1;
1910 BINFO_PRIMARY_P (primary) = 1;
1911 if (BINFO_VIRTUAL_P (primary))
1913 tree delta;
1915 BINFO_INHERITANCE_CHAIN (primary) = type_binfo;
1916 /* A virtual binfo might have been copied from within
1917 another hierarchy. As we're about to use it as a primary
1918 base, make sure the offsets match. */
1919 delta = size_diffop_loc (input_location, ssize_int (0),
1920 convert (ssizetype, BINFO_OFFSET (primary)));
1922 propagate_binfo_offsets (primary, delta);
1925 primary = TYPE_BINFO (basetype);
1927 TYPE_VFIELD (t) = TYPE_VFIELD (basetype);
1928 BINFO_VTABLE (type_binfo) = BINFO_VTABLE (primary);
1929 BINFO_VIRTUALS (type_binfo) = BINFO_VIRTUALS (primary);
1933 /* Update the variant types of T. */
1935 void
1936 fixup_type_variants (tree t)
1938 tree variants;
1940 if (!t)
1941 return;
1943 for (variants = TYPE_NEXT_VARIANT (t);
1944 variants;
1945 variants = TYPE_NEXT_VARIANT (variants))
1947 /* These fields are in the _TYPE part of the node, not in
1948 the TYPE_LANG_SPECIFIC component, so they are not shared. */
1949 TYPE_HAS_USER_CONSTRUCTOR (variants) = TYPE_HAS_USER_CONSTRUCTOR (t);
1950 TYPE_NEEDS_CONSTRUCTING (variants) = TYPE_NEEDS_CONSTRUCTING (t);
1951 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (variants)
1952 = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t);
1954 TYPE_POLYMORPHIC_P (variants) = TYPE_POLYMORPHIC_P (t);
1956 TYPE_BINFO (variants) = TYPE_BINFO (t);
1958 /* Copy whatever these are holding today. */
1959 TYPE_VFIELD (variants) = TYPE_VFIELD (t);
1960 TYPE_FIELDS (variants) = TYPE_FIELDS (t);
1964 /* Early variant fixups: we apply attributes at the beginning of the class
1965 definition, and we need to fix up any variants that have already been
1966 made via elaborated-type-specifier so that check_qualified_type works. */
1968 void
1969 fixup_attribute_variants (tree t)
1971 tree variants;
1973 if (!t)
1974 return;
1976 tree attrs = TYPE_ATTRIBUTES (t);
1977 unsigned align = TYPE_ALIGN (t);
1978 bool user_align = TYPE_USER_ALIGN (t);
1980 for (variants = TYPE_NEXT_VARIANT (t);
1981 variants;
1982 variants = TYPE_NEXT_VARIANT (variants))
1984 /* These are the two fields that check_qualified_type looks at and
1985 are affected by attributes. */
1986 TYPE_ATTRIBUTES (variants) = attrs;
1987 unsigned valign = align;
1988 if (TYPE_USER_ALIGN (variants))
1989 valign = MAX (valign, TYPE_ALIGN (variants));
1990 else
1991 TYPE_USER_ALIGN (variants) = user_align;
1992 TYPE_ALIGN (variants) = valign;
1996 /* Set memoizing fields and bits of T (and its variants) for later
1997 use. */
1999 static void
2000 finish_struct_bits (tree t)
2002 /* Fix up variants (if any). */
2003 fixup_type_variants (t);
2005 if (BINFO_N_BASE_BINFOS (TYPE_BINFO (t)) && TYPE_POLYMORPHIC_P (t))
2006 /* For a class w/o baseclasses, 'finish_struct' has set
2007 CLASSTYPE_PURE_VIRTUALS correctly (by definition).
2008 Similarly for a class whose base classes do not have vtables.
2009 When neither of these is true, we might have removed abstract
2010 virtuals (by providing a definition), added some (by declaring
2011 new ones), or redeclared ones from a base class. We need to
2012 recalculate what's really an abstract virtual at this point (by
2013 looking in the vtables). */
2014 get_pure_virtuals (t);
2016 /* If this type has a copy constructor or a destructor, force its
2017 mode to be BLKmode, and force its TREE_ADDRESSABLE bit to be
2018 nonzero. This will cause it to be passed by invisible reference
2019 and prevent it from being returned in a register. */
2020 if (type_has_nontrivial_copy_init (t)
2021 || TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t))
2023 tree variants;
2024 DECL_MODE (TYPE_MAIN_DECL (t)) = BLKmode;
2025 for (variants = t; variants; variants = TYPE_NEXT_VARIANT (variants))
2027 SET_TYPE_MODE (variants, BLKmode);
2028 TREE_ADDRESSABLE (variants) = 1;
2033 /* Issue warnings about T having private constructors, but no friends,
2034 and so forth.
2036 HAS_NONPRIVATE_METHOD is nonzero if T has any non-private methods or
2037 static members. HAS_NONPRIVATE_STATIC_FN is nonzero if T has any
2038 non-private static member functions. */
2040 static void
2041 maybe_warn_about_overly_private_class (tree t)
2043 int has_member_fn = 0;
2044 int has_nonprivate_method = 0;
2045 tree fn;
2047 if (!warn_ctor_dtor_privacy
2048 /* If the class has friends, those entities might create and
2049 access instances, so we should not warn. */
2050 || (CLASSTYPE_FRIEND_CLASSES (t)
2051 || DECL_FRIENDLIST (TYPE_MAIN_DECL (t)))
2052 /* We will have warned when the template was declared; there's
2053 no need to warn on every instantiation. */
2054 || CLASSTYPE_TEMPLATE_INSTANTIATION (t))
2055 /* There's no reason to even consider warning about this
2056 class. */
2057 return;
2059 /* We only issue one warning, if more than one applies, because
2060 otherwise, on code like:
2062 class A {
2063 // Oops - forgot `public:'
2064 A();
2065 A(const A&);
2066 ~A();
2069 we warn several times about essentially the same problem. */
2071 /* Check to see if all (non-constructor, non-destructor) member
2072 functions are private. (Since there are no friends or
2073 non-private statics, we can't ever call any of the private member
2074 functions.) */
2075 for (fn = TYPE_METHODS (t); fn; fn = DECL_CHAIN (fn))
2076 /* We're not interested in compiler-generated methods; they don't
2077 provide any way to call private members. */
2078 if (!DECL_ARTIFICIAL (fn))
2080 if (!TREE_PRIVATE (fn))
2082 if (DECL_STATIC_FUNCTION_P (fn))
2083 /* A non-private static member function is just like a
2084 friend; it can create and invoke private member
2085 functions, and be accessed without a class
2086 instance. */
2087 return;
2089 has_nonprivate_method = 1;
2090 /* Keep searching for a static member function. */
2092 else if (!DECL_CONSTRUCTOR_P (fn) && !DECL_DESTRUCTOR_P (fn))
2093 has_member_fn = 1;
2096 if (!has_nonprivate_method && has_member_fn)
2098 /* There are no non-private methods, and there's at least one
2099 private member function that isn't a constructor or
2100 destructor. (If all the private members are
2101 constructors/destructors we want to use the code below that
2102 issues error messages specifically referring to
2103 constructors/destructors.) */
2104 unsigned i;
2105 tree binfo = TYPE_BINFO (t);
2107 for (i = 0; i != BINFO_N_BASE_BINFOS (binfo); i++)
2108 if (BINFO_BASE_ACCESS (binfo, i) != access_private_node)
2110 has_nonprivate_method = 1;
2111 break;
2113 if (!has_nonprivate_method)
2115 warning (OPT_Wctor_dtor_privacy,
2116 "all member functions in class %qT are private", t);
2117 return;
2121 /* Even if some of the member functions are non-private, the class
2122 won't be useful for much if all the constructors or destructors
2123 are private: such an object can never be created or destroyed. */
2124 fn = CLASSTYPE_DESTRUCTORS (t);
2125 if (fn && TREE_PRIVATE (fn))
2127 warning (OPT_Wctor_dtor_privacy,
2128 "%q#T only defines a private destructor and has no friends",
2130 return;
2133 /* Warn about classes that have private constructors and no friends. */
2134 if (TYPE_HAS_USER_CONSTRUCTOR (t)
2135 /* Implicitly generated constructors are always public. */
2136 && (!CLASSTYPE_LAZY_DEFAULT_CTOR (t)
2137 || !CLASSTYPE_LAZY_COPY_CTOR (t)))
2139 int nonprivate_ctor = 0;
2141 /* If a non-template class does not define a copy
2142 constructor, one is defined for it, enabling it to avoid
2143 this warning. For a template class, this does not
2144 happen, and so we would normally get a warning on:
2146 template <class T> class C { private: C(); };
2148 To avoid this asymmetry, we check TYPE_HAS_COPY_CTOR. All
2149 complete non-template or fully instantiated classes have this
2150 flag set. */
2151 if (!TYPE_HAS_COPY_CTOR (t))
2152 nonprivate_ctor = 1;
2153 else
2154 for (fn = CLASSTYPE_CONSTRUCTORS (t); fn; fn = OVL_NEXT (fn))
2156 tree ctor = OVL_CURRENT (fn);
2157 /* Ideally, we wouldn't count copy constructors (or, in
2158 fact, any constructor that takes an argument of the
2159 class type as a parameter) because such things cannot
2160 be used to construct an instance of the class unless
2161 you already have one. But, for now at least, we're
2162 more generous. */
2163 if (! TREE_PRIVATE (ctor))
2165 nonprivate_ctor = 1;
2166 break;
2170 if (nonprivate_ctor == 0)
2172 warning (OPT_Wctor_dtor_privacy,
2173 "%q#T only defines private constructors and has no friends",
2175 return;
2180 static struct {
2181 gt_pointer_operator new_value;
2182 void *cookie;
2183 } resort_data;
2185 /* Comparison function to compare two TYPE_METHOD_VEC entries by name. */
2187 static int
2188 method_name_cmp (const void* m1_p, const void* m2_p)
2190 const tree *const m1 = (const tree *) m1_p;
2191 const tree *const m2 = (const tree *) m2_p;
2193 if (*m1 == NULL_TREE && *m2 == NULL_TREE)
2194 return 0;
2195 if (*m1 == NULL_TREE)
2196 return -1;
2197 if (*m2 == NULL_TREE)
2198 return 1;
2199 if (DECL_NAME (OVL_CURRENT (*m1)) < DECL_NAME (OVL_CURRENT (*m2)))
2200 return -1;
2201 return 1;
2204 /* This routine compares two fields like method_name_cmp but using the
2205 pointer operator in resort_field_decl_data. */
2207 static int
2208 resort_method_name_cmp (const void* m1_p, const void* m2_p)
2210 const tree *const m1 = (const tree *) m1_p;
2211 const tree *const m2 = (const tree *) m2_p;
2212 if (*m1 == NULL_TREE && *m2 == NULL_TREE)
2213 return 0;
2214 if (*m1 == NULL_TREE)
2215 return -1;
2216 if (*m2 == NULL_TREE)
2217 return 1;
2219 tree d1 = DECL_NAME (OVL_CURRENT (*m1));
2220 tree d2 = DECL_NAME (OVL_CURRENT (*m2));
2221 resort_data.new_value (&d1, resort_data.cookie);
2222 resort_data.new_value (&d2, resort_data.cookie);
2223 if (d1 < d2)
2224 return -1;
2226 return 1;
2229 /* Resort TYPE_METHOD_VEC because pointers have been reordered. */
2231 void
2232 resort_type_method_vec (void* obj,
2233 void* /*orig_obj*/,
2234 gt_pointer_operator new_value,
2235 void* cookie)
2237 vec<tree, va_gc> *method_vec = (vec<tree, va_gc> *) obj;
2238 int len = vec_safe_length (method_vec);
2239 size_t slot;
2240 tree fn;
2242 /* The type conversion ops have to live at the front of the vec, so we
2243 can't sort them. */
2244 for (slot = CLASSTYPE_FIRST_CONVERSION_SLOT;
2245 vec_safe_iterate (method_vec, slot, &fn);
2246 ++slot)
2247 if (!DECL_CONV_FN_P (OVL_CURRENT (fn)))
2248 break;
2250 if (len - slot > 1)
2252 resort_data.new_value = new_value;
2253 resort_data.cookie = cookie;
2254 qsort (method_vec->address () + slot, len - slot, sizeof (tree),
2255 resort_method_name_cmp);
2259 /* Warn about duplicate methods in fn_fields.
2261 Sort methods that are not special (i.e., constructors, destructors,
2262 and type conversion operators) so that we can find them faster in
2263 search. */
2265 static void
2266 finish_struct_methods (tree t)
2268 tree fn_fields;
2269 vec<tree, va_gc> *method_vec;
2270 int slot, len;
2272 method_vec = CLASSTYPE_METHOD_VEC (t);
2273 if (!method_vec)
2274 return;
2276 len = method_vec->length ();
2278 /* Clear DECL_IN_AGGR_P for all functions. */
2279 for (fn_fields = TYPE_METHODS (t); fn_fields;
2280 fn_fields = DECL_CHAIN (fn_fields))
2281 DECL_IN_AGGR_P (fn_fields) = 0;
2283 /* Issue warnings about private constructors and such. If there are
2284 no methods, then some public defaults are generated. */
2285 maybe_warn_about_overly_private_class (t);
2287 /* The type conversion ops have to live at the front of the vec, so we
2288 can't sort them. */
2289 for (slot = CLASSTYPE_FIRST_CONVERSION_SLOT;
2290 method_vec->iterate (slot, &fn_fields);
2291 ++slot)
2292 if (!DECL_CONV_FN_P (OVL_CURRENT (fn_fields)))
2293 break;
2294 if (len - slot > 1)
2295 qsort (method_vec->address () + slot,
2296 len-slot, sizeof (tree), method_name_cmp);
2299 /* Make BINFO's vtable have N entries, including RTTI entries,
2300 vbase and vcall offsets, etc. Set its type and call the back end
2301 to lay it out. */
2303 static void
2304 layout_vtable_decl (tree binfo, int n)
2306 tree atype;
2307 tree vtable;
2309 atype = build_array_of_n_type (vtable_entry_type, n);
2310 layout_type (atype);
2312 /* We may have to grow the vtable. */
2313 vtable = get_vtbl_decl_for_binfo (binfo);
2314 if (!same_type_p (TREE_TYPE (vtable), atype))
2316 TREE_TYPE (vtable) = atype;
2317 DECL_SIZE (vtable) = DECL_SIZE_UNIT (vtable) = NULL_TREE;
2318 layout_decl (vtable, 0);
2322 /* True iff FNDECL and BASE_FNDECL (both non-static member functions)
2323 have the same signature. */
2326 same_signature_p (const_tree fndecl, const_tree base_fndecl)
2328 /* One destructor overrides another if they are the same kind of
2329 destructor. */
2330 if (DECL_DESTRUCTOR_P (base_fndecl) && DECL_DESTRUCTOR_P (fndecl)
2331 && special_function_p (base_fndecl) == special_function_p (fndecl))
2332 return 1;
2333 /* But a non-destructor never overrides a destructor, nor vice
2334 versa, nor do different kinds of destructors override
2335 one-another. For example, a complete object destructor does not
2336 override a deleting destructor. */
2337 if (DECL_DESTRUCTOR_P (base_fndecl) || DECL_DESTRUCTOR_P (fndecl))
2338 return 0;
2340 if (DECL_NAME (fndecl) == DECL_NAME (base_fndecl)
2341 || (DECL_CONV_FN_P (fndecl)
2342 && DECL_CONV_FN_P (base_fndecl)
2343 && same_type_p (DECL_CONV_FN_TYPE (fndecl),
2344 DECL_CONV_FN_TYPE (base_fndecl))))
2346 tree fntype = TREE_TYPE (fndecl);
2347 tree base_fntype = TREE_TYPE (base_fndecl);
2348 if (type_memfn_quals (fntype) == type_memfn_quals (base_fntype)
2349 && type_memfn_rqual (fntype) == type_memfn_rqual (base_fntype)
2350 && compparms (FUNCTION_FIRST_USER_PARMTYPE (fndecl),
2351 FUNCTION_FIRST_USER_PARMTYPE (base_fndecl)))
2352 return 1;
2354 return 0;
2357 /* Returns TRUE if DERIVED is a binfo containing the binfo BASE as a
2358 subobject. */
2360 static bool
2361 base_derived_from (tree derived, tree base)
2363 tree probe;
2365 for (probe = base; probe; probe = BINFO_INHERITANCE_CHAIN (probe))
2367 if (probe == derived)
2368 return true;
2369 else if (BINFO_VIRTUAL_P (probe))
2370 /* If we meet a virtual base, we can't follow the inheritance
2371 any more. See if the complete type of DERIVED contains
2372 such a virtual base. */
2373 return (binfo_for_vbase (BINFO_TYPE (probe), BINFO_TYPE (derived))
2374 != NULL_TREE);
2376 return false;
2379 typedef struct find_final_overrider_data_s {
2380 /* The function for which we are trying to find a final overrider. */
2381 tree fn;
2382 /* The base class in which the function was declared. */
2383 tree declaring_base;
2384 /* The candidate overriders. */
2385 tree candidates;
2386 /* Path to most derived. */
2387 vec<tree> path;
2388 } find_final_overrider_data;
2390 /* Add the overrider along the current path to FFOD->CANDIDATES.
2391 Returns true if an overrider was found; false otherwise. */
2393 static bool
2394 dfs_find_final_overrider_1 (tree binfo,
2395 find_final_overrider_data *ffod,
2396 unsigned depth)
2398 tree method;
2400 /* If BINFO is not the most derived type, try a more derived class.
2401 A definition there will overrider a definition here. */
2402 if (depth)
2404 depth--;
2405 if (dfs_find_final_overrider_1
2406 (ffod->path[depth], ffod, depth))
2407 return true;
2410 method = look_for_overrides_here (BINFO_TYPE (binfo), ffod->fn);
2411 if (method)
2413 tree *candidate = &ffod->candidates;
2415 /* Remove any candidates overridden by this new function. */
2416 while (*candidate)
2418 /* If *CANDIDATE overrides METHOD, then METHOD
2419 cannot override anything else on the list. */
2420 if (base_derived_from (TREE_VALUE (*candidate), binfo))
2421 return true;
2422 /* If METHOD overrides *CANDIDATE, remove *CANDIDATE. */
2423 if (base_derived_from (binfo, TREE_VALUE (*candidate)))
2424 *candidate = TREE_CHAIN (*candidate);
2425 else
2426 candidate = &TREE_CHAIN (*candidate);
2429 /* Add the new function. */
2430 ffod->candidates = tree_cons (method, binfo, ffod->candidates);
2431 return true;
2434 return false;
2437 /* Called from find_final_overrider via dfs_walk. */
2439 static tree
2440 dfs_find_final_overrider_pre (tree binfo, void *data)
2442 find_final_overrider_data *ffod = (find_final_overrider_data *) data;
2444 if (binfo == ffod->declaring_base)
2445 dfs_find_final_overrider_1 (binfo, ffod, ffod->path.length ());
2446 ffod->path.safe_push (binfo);
2448 return NULL_TREE;
2451 static tree
2452 dfs_find_final_overrider_post (tree /*binfo*/, void *data)
2454 find_final_overrider_data *ffod = (find_final_overrider_data *) data;
2455 ffod->path.pop ();
2457 return NULL_TREE;
2460 /* Returns a TREE_LIST whose TREE_PURPOSE is the final overrider for
2461 FN and whose TREE_VALUE is the binfo for the base where the
2462 overriding occurs. BINFO (in the hierarchy dominated by the binfo
2463 DERIVED) is the base object in which FN is declared. */
2465 static tree
2466 find_final_overrider (tree derived, tree binfo, tree fn)
2468 find_final_overrider_data ffod;
2470 /* Getting this right is a little tricky. This is valid:
2472 struct S { virtual void f (); };
2473 struct T { virtual void f (); };
2474 struct U : public S, public T { };
2476 even though calling `f' in `U' is ambiguous. But,
2478 struct R { virtual void f(); };
2479 struct S : virtual public R { virtual void f (); };
2480 struct T : virtual public R { virtual void f (); };
2481 struct U : public S, public T { };
2483 is not -- there's no way to decide whether to put `S::f' or
2484 `T::f' in the vtable for `R'.
2486 The solution is to look at all paths to BINFO. If we find
2487 different overriders along any two, then there is a problem. */
2488 if (DECL_THUNK_P (fn))
2489 fn = THUNK_TARGET (fn);
2491 /* Determine the depth of the hierarchy. */
2492 ffod.fn = fn;
2493 ffod.declaring_base = binfo;
2494 ffod.candidates = NULL_TREE;
2495 ffod.path.create (30);
2497 dfs_walk_all (derived, dfs_find_final_overrider_pre,
2498 dfs_find_final_overrider_post, &ffod);
2500 ffod.path.release ();
2502 /* If there was no winner, issue an error message. */
2503 if (!ffod.candidates || TREE_CHAIN (ffod.candidates))
2504 return error_mark_node;
2506 return ffod.candidates;
2509 /* Return the index of the vcall offset for FN when TYPE is used as a
2510 virtual base. */
2512 static tree
2513 get_vcall_index (tree fn, tree type)
2515 vec<tree_pair_s, va_gc> *indices = CLASSTYPE_VCALL_INDICES (type);
2516 tree_pair_p p;
2517 unsigned ix;
2519 FOR_EACH_VEC_SAFE_ELT (indices, ix, p)
2520 if ((DECL_DESTRUCTOR_P (fn) && DECL_DESTRUCTOR_P (p->purpose))
2521 || same_signature_p (fn, p->purpose))
2522 return p->value;
2524 /* There should always be an appropriate index. */
2525 gcc_unreachable ();
2528 /* Update an entry in the vtable for BINFO, which is in the hierarchy
2529 dominated by T. FN is the old function; VIRTUALS points to the
2530 corresponding position in the new BINFO_VIRTUALS list. IX is the index
2531 of that entry in the list. */
2533 static void
2534 update_vtable_entry_for_fn (tree t, tree binfo, tree fn, tree* virtuals,
2535 unsigned ix)
2537 tree b;
2538 tree overrider;
2539 tree delta;
2540 tree virtual_base;
2541 tree first_defn;
2542 tree overrider_fn, overrider_target;
2543 tree target_fn = DECL_THUNK_P (fn) ? THUNK_TARGET (fn) : fn;
2544 tree over_return, base_return;
2545 bool lost = false;
2547 /* Find the nearest primary base (possibly binfo itself) which defines
2548 this function; this is the class the caller will convert to when
2549 calling FN through BINFO. */
2550 for (b = binfo; ; b = get_primary_binfo (b))
2552 gcc_assert (b);
2553 if (look_for_overrides_here (BINFO_TYPE (b), target_fn))
2554 break;
2556 /* The nearest definition is from a lost primary. */
2557 if (BINFO_LOST_PRIMARY_P (b))
2558 lost = true;
2560 first_defn = b;
2562 /* Find the final overrider. */
2563 overrider = find_final_overrider (TYPE_BINFO (t), b, target_fn);
2564 if (overrider == error_mark_node)
2566 error ("no unique final overrider for %qD in %qT", target_fn, t);
2567 return;
2569 overrider_target = overrider_fn = TREE_PURPOSE (overrider);
2571 /* Check for adjusting covariant return types. */
2572 over_return = TREE_TYPE (TREE_TYPE (overrider_target));
2573 base_return = TREE_TYPE (TREE_TYPE (target_fn));
2575 if (POINTER_TYPE_P (over_return)
2576 && TREE_CODE (over_return) == TREE_CODE (base_return)
2577 && CLASS_TYPE_P (TREE_TYPE (over_return))
2578 && CLASS_TYPE_P (TREE_TYPE (base_return))
2579 /* If the overrider is invalid, don't even try. */
2580 && !DECL_INVALID_OVERRIDER_P (overrider_target))
2582 /* If FN is a covariant thunk, we must figure out the adjustment
2583 to the final base FN was converting to. As OVERRIDER_TARGET might
2584 also be converting to the return type of FN, we have to
2585 combine the two conversions here. */
2586 tree fixed_offset, virtual_offset;
2588 over_return = TREE_TYPE (over_return);
2589 base_return = TREE_TYPE (base_return);
2591 if (DECL_THUNK_P (fn))
2593 gcc_assert (DECL_RESULT_THUNK_P (fn));
2594 fixed_offset = ssize_int (THUNK_FIXED_OFFSET (fn));
2595 virtual_offset = THUNK_VIRTUAL_OFFSET (fn);
2597 else
2598 fixed_offset = virtual_offset = NULL_TREE;
2600 if (virtual_offset)
2601 /* Find the equivalent binfo within the return type of the
2602 overriding function. We will want the vbase offset from
2603 there. */
2604 virtual_offset = binfo_for_vbase (BINFO_TYPE (virtual_offset),
2605 over_return);
2606 else if (!same_type_ignoring_top_level_qualifiers_p
2607 (over_return, base_return))
2609 /* There was no existing virtual thunk (which takes
2610 precedence). So find the binfo of the base function's
2611 return type within the overriding function's return type.
2612 We cannot call lookup base here, because we're inside a
2613 dfs_walk, and will therefore clobber the BINFO_MARKED
2614 flags. Fortunately we know the covariancy is valid (it
2615 has already been checked), so we can just iterate along
2616 the binfos, which have been chained in inheritance graph
2617 order. Of course it is lame that we have to repeat the
2618 search here anyway -- we should really be caching pieces
2619 of the vtable and avoiding this repeated work. */
2620 tree thunk_binfo, base_binfo;
2622 /* Find the base binfo within the overriding function's
2623 return type. We will always find a thunk_binfo, except
2624 when the covariancy is invalid (which we will have
2625 already diagnosed). */
2626 for (base_binfo = TYPE_BINFO (base_return),
2627 thunk_binfo = TYPE_BINFO (over_return);
2628 thunk_binfo;
2629 thunk_binfo = TREE_CHAIN (thunk_binfo))
2630 if (SAME_BINFO_TYPE_P (BINFO_TYPE (thunk_binfo),
2631 BINFO_TYPE (base_binfo)))
2632 break;
2634 /* See if virtual inheritance is involved. */
2635 for (virtual_offset = thunk_binfo;
2636 virtual_offset;
2637 virtual_offset = BINFO_INHERITANCE_CHAIN (virtual_offset))
2638 if (BINFO_VIRTUAL_P (virtual_offset))
2639 break;
2641 if (virtual_offset
2642 || (thunk_binfo && !BINFO_OFFSET_ZEROP (thunk_binfo)))
2644 tree offset = convert (ssizetype, BINFO_OFFSET (thunk_binfo));
2646 if (virtual_offset)
2648 /* We convert via virtual base. Adjust the fixed
2649 offset to be from there. */
2650 offset =
2651 size_diffop (offset,
2652 convert (ssizetype,
2653 BINFO_OFFSET (virtual_offset)));
2655 if (fixed_offset)
2656 /* There was an existing fixed offset, this must be
2657 from the base just converted to, and the base the
2658 FN was thunking to. */
2659 fixed_offset = size_binop (PLUS_EXPR, fixed_offset, offset);
2660 else
2661 fixed_offset = offset;
2665 if (fixed_offset || virtual_offset)
2666 /* Replace the overriding function with a covariant thunk. We
2667 will emit the overriding function in its own slot as
2668 well. */
2669 overrider_fn = make_thunk (overrider_target, /*this_adjusting=*/0,
2670 fixed_offset, virtual_offset);
2672 else
2673 gcc_assert (DECL_INVALID_OVERRIDER_P (overrider_target) ||
2674 !DECL_THUNK_P (fn));
2676 /* If we need a covariant thunk, then we may need to adjust first_defn.
2677 The ABI specifies that the thunks emitted with a function are
2678 determined by which bases the function overrides, so we need to be
2679 sure that we're using a thunk for some overridden base; even if we
2680 know that the necessary this adjustment is zero, there may not be an
2681 appropriate zero-this-adjusment thunk for us to use since thunks for
2682 overriding virtual bases always use the vcall offset.
2684 Furthermore, just choosing any base that overrides this function isn't
2685 quite right, as this slot won't be used for calls through a type that
2686 puts a covariant thunk here. Calling the function through such a type
2687 will use a different slot, and that slot is the one that determines
2688 the thunk emitted for that base.
2690 So, keep looking until we find the base that we're really overriding
2691 in this slot: the nearest primary base that doesn't use a covariant
2692 thunk in this slot. */
2693 if (overrider_target != overrider_fn)
2695 if (BINFO_TYPE (b) == DECL_CONTEXT (overrider_target))
2696 /* We already know that the overrider needs a covariant thunk. */
2697 b = get_primary_binfo (b);
2698 for (; ; b = get_primary_binfo (b))
2700 tree main_binfo = TYPE_BINFO (BINFO_TYPE (b));
2701 tree bv = chain_index (ix, BINFO_VIRTUALS (main_binfo));
2702 if (!DECL_THUNK_P (TREE_VALUE (bv)))
2703 break;
2704 if (BINFO_LOST_PRIMARY_P (b))
2705 lost = true;
2707 first_defn = b;
2710 /* Assume that we will produce a thunk that convert all the way to
2711 the final overrider, and not to an intermediate virtual base. */
2712 virtual_base = NULL_TREE;
2714 /* See if we can convert to an intermediate virtual base first, and then
2715 use the vcall offset located there to finish the conversion. */
2716 for (; b; b = BINFO_INHERITANCE_CHAIN (b))
2718 /* If we find the final overrider, then we can stop
2719 walking. */
2720 if (SAME_BINFO_TYPE_P (BINFO_TYPE (b),
2721 BINFO_TYPE (TREE_VALUE (overrider))))
2722 break;
2724 /* If we find a virtual base, and we haven't yet found the
2725 overrider, then there is a virtual base between the
2726 declaring base (first_defn) and the final overrider. */
2727 if (BINFO_VIRTUAL_P (b))
2729 virtual_base = b;
2730 break;
2734 /* Compute the constant adjustment to the `this' pointer. The
2735 `this' pointer, when this function is called, will point at BINFO
2736 (or one of its primary bases, which are at the same offset). */
2737 if (virtual_base)
2738 /* The `this' pointer needs to be adjusted from the declaration to
2739 the nearest virtual base. */
2740 delta = size_diffop_loc (input_location,
2741 convert (ssizetype, BINFO_OFFSET (virtual_base)),
2742 convert (ssizetype, BINFO_OFFSET (first_defn)));
2743 else if (lost)
2744 /* If the nearest definition is in a lost primary, we don't need an
2745 entry in our vtable. Except possibly in a constructor vtable,
2746 if we happen to get our primary back. In that case, the offset
2747 will be zero, as it will be a primary base. */
2748 delta = size_zero_node;
2749 else
2750 /* The `this' pointer needs to be adjusted from pointing to
2751 BINFO to pointing at the base where the final overrider
2752 appears. */
2753 delta = size_diffop_loc (input_location,
2754 convert (ssizetype,
2755 BINFO_OFFSET (TREE_VALUE (overrider))),
2756 convert (ssizetype, BINFO_OFFSET (binfo)));
2758 modify_vtable_entry (t, binfo, overrider_fn, delta, virtuals);
2760 if (virtual_base)
2761 BV_VCALL_INDEX (*virtuals)
2762 = get_vcall_index (overrider_target, BINFO_TYPE (virtual_base));
2763 else
2764 BV_VCALL_INDEX (*virtuals) = NULL_TREE;
2766 BV_LOST_PRIMARY (*virtuals) = lost;
2769 /* Called from modify_all_vtables via dfs_walk. */
2771 static tree
2772 dfs_modify_vtables (tree binfo, void* data)
2774 tree t = (tree) data;
2775 tree virtuals;
2776 tree old_virtuals;
2777 unsigned ix;
2779 if (!TYPE_CONTAINS_VPTR_P (BINFO_TYPE (binfo)))
2780 /* A base without a vtable needs no modification, and its bases
2781 are uninteresting. */
2782 return dfs_skip_bases;
2784 if (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), t)
2785 && !CLASSTYPE_HAS_PRIMARY_BASE_P (t))
2786 /* Don't do the primary vtable, if it's new. */
2787 return NULL_TREE;
2789 if (BINFO_PRIMARY_P (binfo) && !BINFO_VIRTUAL_P (binfo))
2790 /* There's no need to modify the vtable for a non-virtual primary
2791 base; we're not going to use that vtable anyhow. We do still
2792 need to do this for virtual primary bases, as they could become
2793 non-primary in a construction vtable. */
2794 return NULL_TREE;
2796 make_new_vtable (t, binfo);
2798 /* Now, go through each of the virtual functions in the virtual
2799 function table for BINFO. Find the final overrider, and update
2800 the BINFO_VIRTUALS list appropriately. */
2801 for (ix = 0, virtuals = BINFO_VIRTUALS (binfo),
2802 old_virtuals = BINFO_VIRTUALS (TYPE_BINFO (BINFO_TYPE (binfo)));
2803 virtuals;
2804 ix++, virtuals = TREE_CHAIN (virtuals),
2805 old_virtuals = TREE_CHAIN (old_virtuals))
2806 update_vtable_entry_for_fn (t,
2807 binfo,
2808 BV_FN (old_virtuals),
2809 &virtuals, ix);
2811 return NULL_TREE;
2814 /* Update all of the primary and secondary vtables for T. Create new
2815 vtables as required, and initialize their RTTI information. Each
2816 of the functions in VIRTUALS is declared in T and may override a
2817 virtual function from a base class; find and modify the appropriate
2818 entries to point to the overriding functions. Returns a list, in
2819 declaration order, of the virtual functions that are declared in T,
2820 but do not appear in the primary base class vtable, and which
2821 should therefore be appended to the end of the vtable for T. */
2823 static tree
2824 modify_all_vtables (tree t, tree virtuals)
2826 tree binfo = TYPE_BINFO (t);
2827 tree *fnsp;
2829 /* Mangle the vtable name before entering dfs_walk (c++/51884). */
2830 if (TYPE_CONTAINS_VPTR_P (t))
2831 get_vtable_decl (t, false);
2833 /* Update all of the vtables. */
2834 dfs_walk_once (binfo, dfs_modify_vtables, NULL, t);
2836 /* Add virtual functions not already in our primary vtable. These
2837 will be both those introduced by this class, and those overridden
2838 from secondary bases. It does not include virtuals merely
2839 inherited from secondary bases. */
2840 for (fnsp = &virtuals; *fnsp; )
2842 tree fn = TREE_VALUE (*fnsp);
2844 if (!value_member (fn, BINFO_VIRTUALS (binfo))
2845 || DECL_VINDEX (fn) == error_mark_node)
2847 /* We don't need to adjust the `this' pointer when
2848 calling this function. */
2849 BV_DELTA (*fnsp) = integer_zero_node;
2850 BV_VCALL_INDEX (*fnsp) = NULL_TREE;
2852 /* This is a function not already in our vtable. Keep it. */
2853 fnsp = &TREE_CHAIN (*fnsp);
2855 else
2856 /* We've already got an entry for this function. Skip it. */
2857 *fnsp = TREE_CHAIN (*fnsp);
2860 return virtuals;
2863 /* Get the base virtual function declarations in T that have the
2864 indicated NAME. */
2866 static void
2867 get_basefndecls (tree name, tree t, vec<tree> *base_fndecls)
2869 tree methods;
2870 int n_baseclasses = BINFO_N_BASE_BINFOS (TYPE_BINFO (t));
2871 int i;
2873 /* Find virtual functions in T with the indicated NAME. */
2874 i = lookup_fnfields_1 (t, name);
2875 bool found_decls = false;
2876 if (i != -1)
2877 for (methods = (*CLASSTYPE_METHOD_VEC (t))[i];
2878 methods;
2879 methods = OVL_NEXT (methods))
2881 tree method = OVL_CURRENT (methods);
2883 if (TREE_CODE (method) == FUNCTION_DECL
2884 && DECL_VINDEX (method))
2886 base_fndecls->safe_push (method);
2887 found_decls = true;
2891 if (found_decls)
2892 return;
2894 for (i = 0; i < n_baseclasses; i++)
2896 tree basetype = BINFO_TYPE (BINFO_BASE_BINFO (TYPE_BINFO (t), i));
2897 get_basefndecls (name, basetype, base_fndecls);
2901 /* If this declaration supersedes the declaration of
2902 a method declared virtual in the base class, then
2903 mark this field as being virtual as well. */
2905 void
2906 check_for_override (tree decl, tree ctype)
2908 bool overrides_found = false;
2909 if (TREE_CODE (decl) == TEMPLATE_DECL)
2910 /* In [temp.mem] we have:
2912 A specialization of a member function template does not
2913 override a virtual function from a base class. */
2914 return;
2915 if ((DECL_DESTRUCTOR_P (decl)
2916 || IDENTIFIER_VIRTUAL_P (DECL_NAME (decl))
2917 || DECL_CONV_FN_P (decl))
2918 && look_for_overrides (ctype, decl)
2919 && !DECL_STATIC_FUNCTION_P (decl))
2920 /* Set DECL_VINDEX to a value that is neither an INTEGER_CST nor
2921 the error_mark_node so that we know it is an overriding
2922 function. */
2924 DECL_VINDEX (decl) = decl;
2925 overrides_found = true;
2926 if (warn_override && !DECL_OVERRIDE_P (decl)
2927 && !DECL_DESTRUCTOR_P (decl))
2928 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wsuggest_override,
2929 "%q+D can be marked override", decl);
2932 if (DECL_VIRTUAL_P (decl))
2934 if (!DECL_VINDEX (decl))
2935 DECL_VINDEX (decl) = error_mark_node;
2936 IDENTIFIER_VIRTUAL_P (DECL_NAME (decl)) = 1;
2937 if (DECL_DESTRUCTOR_P (decl))
2938 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (ctype) = true;
2940 else if (DECL_FINAL_P (decl))
2941 error ("%q+#D marked %<final%>, but is not virtual", decl);
2942 if (DECL_OVERRIDE_P (decl) && !overrides_found)
2943 error ("%q+#D marked %<override%>, but does not override", decl);
2946 /* Warn about hidden virtual functions that are not overridden in t.
2947 We know that constructors and destructors don't apply. */
2949 static void
2950 warn_hidden (tree t)
2952 vec<tree, va_gc> *method_vec = CLASSTYPE_METHOD_VEC (t);
2953 tree fns;
2954 size_t i;
2956 /* We go through each separately named virtual function. */
2957 for (i = CLASSTYPE_FIRST_CONVERSION_SLOT;
2958 vec_safe_iterate (method_vec, i, &fns);
2959 ++i)
2961 tree fn;
2962 tree name;
2963 tree fndecl;
2964 tree base_binfo;
2965 tree binfo;
2966 int j;
2968 /* All functions in this slot in the CLASSTYPE_METHOD_VEC will
2969 have the same name. Figure out what name that is. */
2970 name = DECL_NAME (OVL_CURRENT (fns));
2971 /* There are no possibly hidden functions yet. */
2972 auto_vec<tree, 20> base_fndecls;
2973 /* Iterate through all of the base classes looking for possibly
2974 hidden functions. */
2975 for (binfo = TYPE_BINFO (t), j = 0;
2976 BINFO_BASE_ITERATE (binfo, j, base_binfo); j++)
2978 tree basetype = BINFO_TYPE (base_binfo);
2979 get_basefndecls (name, basetype, &base_fndecls);
2982 /* If there are no functions to hide, continue. */
2983 if (base_fndecls.is_empty ())
2984 continue;
2986 /* Remove any overridden functions. */
2987 for (fn = fns; fn; fn = OVL_NEXT (fn))
2989 fndecl = OVL_CURRENT (fn);
2990 if (TREE_CODE (fndecl) == FUNCTION_DECL
2991 && DECL_VINDEX (fndecl))
2993 /* If the method from the base class has the same
2994 signature as the method from the derived class, it
2995 has been overridden. */
2996 for (size_t k = 0; k < base_fndecls.length (); k++)
2997 if (base_fndecls[k]
2998 && same_signature_p (fndecl, base_fndecls[k]))
2999 base_fndecls[k] = NULL_TREE;
3003 /* Now give a warning for all base functions without overriders,
3004 as they are hidden. */
3005 size_t k;
3006 tree base_fndecl;
3007 FOR_EACH_VEC_ELT (base_fndecls, k, base_fndecl)
3008 if (base_fndecl)
3010 /* Here we know it is a hider, and no overrider exists. */
3011 warning (OPT_Woverloaded_virtual, "%q+D was hidden", base_fndecl);
3012 warning (OPT_Woverloaded_virtual, " by %q+D", 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 (input_location,
3046 "%q+#D invalid; an anonymous union can "
3047 "only have non-static data members", elt);
3048 else
3049 permerror (input_location,
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 (input_location,
3062 "private member %q+#D in anonymous union", elt);
3063 else
3064 permerror (input_location,
3065 "private member %q+#D in anonymous struct", elt);
3067 else if (TREE_PROTECTED (elt))
3069 if (is_union)
3070 permerror (input_location,
3071 "protected member %q+#D in anonymous union", elt);
3072 else
3073 permerror (input_location,
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 (0, "width of %q+D exceeds its type", field);
3464 else if (TREE_CODE (type) == ENUMERAL_TYPE
3465 && (0 > (compare_tree_int
3466 (w, TYPE_PRECISION (ENUM_UNDERLYING_TYPE (type))))))
3467 warning (0, "%q+D is too small to hold all values of %q#T", field, type);
3470 if (w != error_mark_node)
3472 DECL_SIZE (field) = convert (bitsizetype, w);
3473 DECL_BIT_FIELD (field) = 1;
3474 return true;
3476 else
3478 /* Non-bit-fields are aligned for their type. */
3479 DECL_BIT_FIELD (field) = 0;
3480 CLEAR_DECL_C_BIT_FIELD (field);
3481 return false;
3485 /* FIELD is a non bit-field. We are finishing the processing for its
3486 enclosing type T. Issue any appropriate messages and set appropriate
3487 flags. */
3489 static void
3490 check_field_decl (tree field,
3491 tree t,
3492 int* cant_have_const_ctor,
3493 int* no_const_asn_ref,
3494 int* any_default_members)
3496 tree type = strip_array_types (TREE_TYPE (field));
3498 /* In C++98 an anonymous union cannot contain any fields which would change
3499 the settings of CANT_HAVE_CONST_CTOR and friends. */
3500 if (ANON_UNION_TYPE_P (type) && cxx_dialect < cxx11)
3502 /* And, we don't set TYPE_HAS_CONST_COPY_CTOR, etc., for anonymous
3503 structs. So, we recurse through their fields here. */
3504 else if (ANON_AGGR_TYPE_P (type))
3506 tree fields;
3508 for (fields = TYPE_FIELDS (type); fields; fields = DECL_CHAIN (fields))
3509 if (TREE_CODE (fields) == FIELD_DECL && !DECL_C_BIT_FIELD (field))
3510 check_field_decl (fields, t, cant_have_const_ctor,
3511 no_const_asn_ref, any_default_members);
3513 /* Check members with class type for constructors, destructors,
3514 etc. */
3515 else if (CLASS_TYPE_P (type))
3517 /* Never let anything with uninheritable virtuals
3518 make it through without complaint. */
3519 abstract_virtuals_error (field, type);
3521 if (TREE_CODE (t) == UNION_TYPE && cxx_dialect < cxx11)
3523 static bool warned;
3524 int oldcount = errorcount;
3525 if (TYPE_NEEDS_CONSTRUCTING (type))
3526 error ("member %q+#D with constructor not allowed in union",
3527 field);
3528 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
3529 error ("member %q+#D with destructor not allowed in union", field);
3530 if (TYPE_HAS_COMPLEX_COPY_ASSIGN (type))
3531 error ("member %q+#D with copy assignment operator not allowed in union",
3532 field);
3533 if (!warned && errorcount > oldcount)
3535 inform (DECL_SOURCE_LOCATION (field), "unrestricted unions "
3536 "only available with -std=c++11 or -std=gnu++11");
3537 warned = true;
3540 else
3542 TYPE_NEEDS_CONSTRUCTING (t) |= TYPE_NEEDS_CONSTRUCTING (type);
3543 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t)
3544 |= TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type);
3545 TYPE_HAS_COMPLEX_COPY_ASSIGN (t)
3546 |= (TYPE_HAS_COMPLEX_COPY_ASSIGN (type)
3547 || !TYPE_HAS_COPY_ASSIGN (type));
3548 TYPE_HAS_COMPLEX_COPY_CTOR (t) |= (TYPE_HAS_COMPLEX_COPY_CTOR (type)
3549 || !TYPE_HAS_COPY_CTOR (type));
3550 TYPE_HAS_COMPLEX_MOVE_ASSIGN (t) |= TYPE_HAS_COMPLEX_MOVE_ASSIGN (type);
3551 TYPE_HAS_COMPLEX_MOVE_CTOR (t) |= TYPE_HAS_COMPLEX_MOVE_CTOR (type);
3552 TYPE_HAS_COMPLEX_DFLT (t) |= (!TYPE_HAS_DEFAULT_CONSTRUCTOR (type)
3553 || TYPE_HAS_COMPLEX_DFLT (type));
3556 if (TYPE_HAS_COPY_CTOR (type)
3557 && !TYPE_HAS_CONST_COPY_CTOR (type))
3558 *cant_have_const_ctor = 1;
3560 if (TYPE_HAS_COPY_ASSIGN (type)
3561 && !TYPE_HAS_CONST_COPY_ASSIGN (type))
3562 *no_const_asn_ref = 1;
3565 check_abi_tags (t, field);
3567 if (DECL_INITIAL (field) != NULL_TREE)
3569 /* `build_class_init_list' does not recognize
3570 non-FIELD_DECLs. */
3571 if (TREE_CODE (t) == UNION_TYPE && *any_default_members != 0)
3572 error ("multiple fields in union %qT initialized", t);
3573 *any_default_members = 1;
3577 /* Check the data members (both static and non-static), class-scoped
3578 typedefs, etc., appearing in the declaration of T. Issue
3579 appropriate diagnostics. Sets ACCESS_DECLS to a list (in
3580 declaration order) of access declarations; each TREE_VALUE in this
3581 list is a USING_DECL.
3583 In addition, set the following flags:
3585 EMPTY_P
3586 The class is empty, i.e., contains no non-static data members.
3588 CANT_HAVE_CONST_CTOR_P
3589 This class cannot have an implicitly generated copy constructor
3590 taking a const reference.
3592 CANT_HAVE_CONST_ASN_REF
3593 This class cannot have an implicitly generated assignment
3594 operator taking a const reference.
3596 All of these flags should be initialized before calling this
3597 function.
3599 Returns a pointer to the end of the TYPE_FIELDs chain; additional
3600 fields can be added by adding to this chain. */
3602 static void
3603 check_field_decls (tree t, tree *access_decls,
3604 int *cant_have_const_ctor_p,
3605 int *no_const_asn_ref_p)
3607 tree *field;
3608 tree *next;
3609 bool has_pointers;
3610 int any_default_members;
3611 int cant_pack = 0;
3612 int field_access = -1;
3614 /* Assume there are no access declarations. */
3615 *access_decls = NULL_TREE;
3616 /* Assume this class has no pointer members. */
3617 has_pointers = false;
3618 /* Assume none of the members of this class have default
3619 initializations. */
3620 any_default_members = 0;
3622 for (field = &TYPE_FIELDS (t); *field; field = next)
3624 tree x = *field;
3625 tree type = TREE_TYPE (x);
3626 int this_field_access;
3628 next = &DECL_CHAIN (x);
3630 if (TREE_CODE (x) == USING_DECL)
3632 /* Save the access declarations for our caller. */
3633 *access_decls = tree_cons (NULL_TREE, x, *access_decls);
3634 continue;
3637 if (TREE_CODE (x) == TYPE_DECL
3638 || TREE_CODE (x) == TEMPLATE_DECL)
3639 continue;
3641 /* If we've gotten this far, it's a data member, possibly static,
3642 or an enumerator. */
3643 if (TREE_CODE (x) != CONST_DECL)
3644 DECL_CONTEXT (x) = t;
3646 /* When this goes into scope, it will be a non-local reference. */
3647 DECL_NONLOCAL (x) = 1;
3649 if (TREE_CODE (t) == UNION_TYPE
3650 && cxx_dialect < cxx11)
3652 /* [class.union] (C++98)
3654 If a union contains a static data member, or a member of
3655 reference type, the program is ill-formed.
3657 In C++11 this limitation doesn't exist anymore. */
3658 if (VAR_P (x))
3660 error ("in C++98 %q+D may not be static because it is "
3661 "a member of a union", x);
3662 continue;
3664 if (TREE_CODE (type) == REFERENCE_TYPE)
3666 error ("in C++98 %q+D may not have reference type %qT "
3667 "because it is a member of a union", x, type);
3668 continue;
3672 /* Perform error checking that did not get done in
3673 grokdeclarator. */
3674 if (TREE_CODE (type) == FUNCTION_TYPE)
3676 error ("field %q+D invalidly declared function type", x);
3677 type = build_pointer_type (type);
3678 TREE_TYPE (x) = type;
3680 else if (TREE_CODE (type) == METHOD_TYPE)
3682 error ("field %q+D invalidly declared method type", x);
3683 type = build_pointer_type (type);
3684 TREE_TYPE (x) = type;
3687 if (type == error_mark_node)
3688 continue;
3690 if (TREE_CODE (x) == CONST_DECL || VAR_P (x))
3691 continue;
3693 /* Now it can only be a FIELD_DECL. */
3695 if (TREE_PRIVATE (x) || TREE_PROTECTED (x))
3696 CLASSTYPE_NON_AGGREGATE (t) = 1;
3698 /* If at least one non-static data member is non-literal, the whole
3699 class becomes non-literal. Per Core/1453, volatile non-static
3700 data members and base classes are also not allowed.
3701 Note: if the type is incomplete we will complain later on. */
3702 if (COMPLETE_TYPE_P (type)
3703 && (!literal_type_p (type) || CP_TYPE_VOLATILE_P (type)))
3704 CLASSTYPE_LITERAL_P (t) = false;
3706 /* A standard-layout class is a class that:
3708 has the same access control (Clause 11) for all non-static data members,
3709 ... */
3710 this_field_access = TREE_PROTECTED (x) ? 1 : TREE_PRIVATE (x) ? 2 : 0;
3711 if (field_access == -1)
3712 field_access = this_field_access;
3713 else if (this_field_access != field_access)
3714 CLASSTYPE_NON_STD_LAYOUT (t) = 1;
3716 /* If this is of reference type, check if it needs an init. */
3717 if (TREE_CODE (type) == REFERENCE_TYPE)
3719 CLASSTYPE_NON_LAYOUT_POD_P (t) = 1;
3720 CLASSTYPE_NON_STD_LAYOUT (t) = 1;
3721 if (DECL_INITIAL (x) == NULL_TREE)
3722 SET_CLASSTYPE_REF_FIELDS_NEED_INIT (t, 1);
3723 if (cxx_dialect < cxx11)
3725 /* ARM $12.6.2: [A member initializer list] (or, for an
3726 aggregate, initialization by a brace-enclosed list) is the
3727 only way to initialize nonstatic const and reference
3728 members. */
3729 TYPE_HAS_COMPLEX_COPY_ASSIGN (t) = 1;
3730 TYPE_HAS_COMPLEX_MOVE_ASSIGN (t) = 1;
3734 type = strip_array_types (type);
3736 if (TYPE_PACKED (t))
3738 if (!layout_pod_type_p (type) && !TYPE_PACKED (type))
3740 warning
3742 "ignoring packed attribute because of unpacked non-POD field %q+#D",
3744 cant_pack = 1;
3746 else if (DECL_C_BIT_FIELD (x)
3747 || TYPE_ALIGN (TREE_TYPE (x)) > BITS_PER_UNIT)
3748 DECL_PACKED (x) = 1;
3751 if (DECL_C_BIT_FIELD (x) && integer_zerop (DECL_INITIAL (x)))
3752 /* We don't treat zero-width bitfields as making a class
3753 non-empty. */
3755 else
3757 /* The class is non-empty. */
3758 CLASSTYPE_EMPTY_P (t) = 0;
3759 /* The class is not even nearly empty. */
3760 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
3761 /* If one of the data members contains an empty class,
3762 so does T. */
3763 if (CLASS_TYPE_P (type)
3764 && CLASSTYPE_CONTAINS_EMPTY_CLASS_P (type))
3765 CLASSTYPE_CONTAINS_EMPTY_CLASS_P (t) = 1;
3768 /* This is used by -Weffc++ (see below). Warn only for pointers
3769 to members which might hold dynamic memory. So do not warn
3770 for pointers to functions or pointers to members. */
3771 if (TYPE_PTR_P (type)
3772 && !TYPE_PTRFN_P (type))
3773 has_pointers = true;
3775 if (CLASS_TYPE_P (type))
3777 if (CLASSTYPE_REF_FIELDS_NEED_INIT (type))
3778 SET_CLASSTYPE_REF_FIELDS_NEED_INIT (t, 1);
3779 if (CLASSTYPE_READONLY_FIELDS_NEED_INIT (type))
3780 SET_CLASSTYPE_READONLY_FIELDS_NEED_INIT (t, 1);
3783 if (DECL_MUTABLE_P (x) || TYPE_HAS_MUTABLE_P (type))
3784 CLASSTYPE_HAS_MUTABLE (t) = 1;
3786 if (DECL_MUTABLE_P (x))
3788 if (CP_TYPE_CONST_P (type))
3790 error ("member %q+D cannot be declared both %<const%> "
3791 "and %<mutable%>", x);
3792 continue;
3794 if (TREE_CODE (type) == REFERENCE_TYPE)
3796 error ("member %q+D cannot be declared as a %<mutable%> "
3797 "reference", x);
3798 continue;
3802 if (! layout_pod_type_p (type))
3803 /* DR 148 now allows pointers to members (which are POD themselves),
3804 to be allowed in POD structs. */
3805 CLASSTYPE_NON_LAYOUT_POD_P (t) = 1;
3807 if (!std_layout_type_p (type))
3808 CLASSTYPE_NON_STD_LAYOUT (t) = 1;
3810 if (! zero_init_p (type))
3811 CLASSTYPE_NON_ZERO_INIT_P (t) = 1;
3813 /* We set DECL_C_BIT_FIELD in grokbitfield.
3814 If the type and width are valid, we'll also set DECL_BIT_FIELD. */
3815 if (! DECL_C_BIT_FIELD (x) || ! check_bitfield_decl (x))
3816 check_field_decl (x, t,
3817 cant_have_const_ctor_p,
3818 no_const_asn_ref_p,
3819 &any_default_members);
3821 /* Now that we've removed bit-field widths from DECL_INITIAL,
3822 anything left in DECL_INITIAL is an NSDMI that makes the class
3823 non-aggregate in C++11. */
3824 if (DECL_INITIAL (x) && cxx_dialect < cxx14)
3825 CLASSTYPE_NON_AGGREGATE (t) = true;
3827 /* If any field is const, the structure type is pseudo-const. */
3828 if (CP_TYPE_CONST_P (type))
3830 C_TYPE_FIELDS_READONLY (t) = 1;
3831 if (DECL_INITIAL (x) == NULL_TREE)
3832 SET_CLASSTYPE_READONLY_FIELDS_NEED_INIT (t, 1);
3833 if (cxx_dialect < cxx11)
3835 /* ARM $12.6.2: [A member initializer list] (or, for an
3836 aggregate, initialization by a brace-enclosed list) is the
3837 only way to initialize nonstatic const and reference
3838 members. */
3839 TYPE_HAS_COMPLEX_COPY_ASSIGN (t) = 1;
3840 TYPE_HAS_COMPLEX_MOVE_ASSIGN (t) = 1;
3843 /* A field that is pseudo-const makes the structure likewise. */
3844 else if (CLASS_TYPE_P (type))
3846 C_TYPE_FIELDS_READONLY (t) |= C_TYPE_FIELDS_READONLY (type);
3847 SET_CLASSTYPE_READONLY_FIELDS_NEED_INIT (t,
3848 CLASSTYPE_READONLY_FIELDS_NEED_INIT (t)
3849 | CLASSTYPE_READONLY_FIELDS_NEED_INIT (type));
3852 /* Core issue 80: A nonstatic data member is required to have a
3853 different name from the class iff the class has a
3854 user-declared constructor. */
3855 if (constructor_name_p (DECL_NAME (x), t)
3856 && TYPE_HAS_USER_CONSTRUCTOR (t))
3857 permerror (input_location, "field %q+#D with same name as class", x);
3860 /* Effective C++ rule 11: if a class has dynamic memory held by pointers,
3861 it should also define a copy constructor and an assignment operator to
3862 implement the correct copy semantic (deep vs shallow, etc.). As it is
3863 not feasible to check whether the constructors do allocate dynamic memory
3864 and store it within members, we approximate the warning like this:
3866 -- Warn only if there are members which are pointers
3867 -- Warn only if there is a non-trivial constructor (otherwise,
3868 there cannot be memory allocated).
3869 -- Warn only if there is a non-trivial destructor. We assume that the
3870 user at least implemented the cleanup correctly, and a destructor
3871 is needed to free dynamic memory.
3873 This seems enough for practical purposes. */
3874 if (warn_ecpp
3875 && has_pointers
3876 && TYPE_HAS_USER_CONSTRUCTOR (t)
3877 && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t)
3878 && !(TYPE_HAS_COPY_CTOR (t) && TYPE_HAS_COPY_ASSIGN (t)))
3880 warning (OPT_Weffc__, "%q#T has pointer data members", t);
3882 if (! TYPE_HAS_COPY_CTOR (t))
3884 warning (OPT_Weffc__,
3885 " but does not override %<%T(const %T&)%>", t, t);
3886 if (!TYPE_HAS_COPY_ASSIGN (t))
3887 warning (OPT_Weffc__, " or %<operator=(const %T&)%>", t);
3889 else if (! TYPE_HAS_COPY_ASSIGN (t))
3890 warning (OPT_Weffc__,
3891 " but does not override %<operator=(const %T&)%>", t);
3894 /* Non-static data member initializers make the default constructor
3895 non-trivial. */
3896 if (any_default_members)
3898 TYPE_NEEDS_CONSTRUCTING (t) = true;
3899 TYPE_HAS_COMPLEX_DFLT (t) = true;
3902 /* If any of the fields couldn't be packed, unset TYPE_PACKED. */
3903 if (cant_pack)
3904 TYPE_PACKED (t) = 0;
3906 /* Check anonymous struct/anonymous union fields. */
3907 finish_struct_anon (t);
3909 /* We've built up the list of access declarations in reverse order.
3910 Fix that now. */
3911 *access_decls = nreverse (*access_decls);
3914 /* If TYPE is an empty class type, records its OFFSET in the table of
3915 OFFSETS. */
3917 static int
3918 record_subobject_offset (tree type, tree offset, splay_tree offsets)
3920 splay_tree_node n;
3922 if (!is_empty_class (type))
3923 return 0;
3925 /* Record the location of this empty object in OFFSETS. */
3926 n = splay_tree_lookup (offsets, (splay_tree_key) offset);
3927 if (!n)
3928 n = splay_tree_insert (offsets,
3929 (splay_tree_key) offset,
3930 (splay_tree_value) NULL_TREE);
3931 n->value = ((splay_tree_value)
3932 tree_cons (NULL_TREE,
3933 type,
3934 (tree) n->value));
3936 return 0;
3939 /* Returns nonzero if TYPE is an empty class type and there is
3940 already an entry in OFFSETS for the same TYPE as the same OFFSET. */
3942 static int
3943 check_subobject_offset (tree type, tree offset, splay_tree offsets)
3945 splay_tree_node n;
3946 tree t;
3948 if (!is_empty_class (type))
3949 return 0;
3951 /* Record the location of this empty object in OFFSETS. */
3952 n = splay_tree_lookup (offsets, (splay_tree_key) offset);
3953 if (!n)
3954 return 0;
3956 for (t = (tree) n->value; t; t = TREE_CHAIN (t))
3957 if (same_type_p (TREE_VALUE (t), type))
3958 return 1;
3960 return 0;
3963 /* Walk through all the subobjects of TYPE (located at OFFSET). Call
3964 F for every subobject, passing it the type, offset, and table of
3965 OFFSETS. If VBASES_P is one, then virtual non-primary bases should
3966 be traversed.
3968 If MAX_OFFSET is non-NULL, then subobjects with an offset greater
3969 than MAX_OFFSET will not be walked.
3971 If F returns a nonzero value, the traversal ceases, and that value
3972 is returned. Otherwise, returns zero. */
3974 static int
3975 walk_subobject_offsets (tree type,
3976 subobject_offset_fn f,
3977 tree offset,
3978 splay_tree offsets,
3979 tree max_offset,
3980 int vbases_p)
3982 int r = 0;
3983 tree type_binfo = NULL_TREE;
3985 /* If this OFFSET is bigger than the MAX_OFFSET, then we should
3986 stop. */
3987 if (max_offset && tree_int_cst_lt (max_offset, offset))
3988 return 0;
3990 if (type == error_mark_node)
3991 return 0;
3993 if (!TYPE_P (type))
3995 type_binfo = type;
3996 type = BINFO_TYPE (type);
3999 if (CLASS_TYPE_P (type))
4001 tree field;
4002 tree binfo;
4003 int i;
4005 /* Avoid recursing into objects that are not interesting. */
4006 if (!CLASSTYPE_CONTAINS_EMPTY_CLASS_P (type))
4007 return 0;
4009 /* Record the location of TYPE. */
4010 r = (*f) (type, offset, offsets);
4011 if (r)
4012 return r;
4014 /* Iterate through the direct base classes of TYPE. */
4015 if (!type_binfo)
4016 type_binfo = TYPE_BINFO (type);
4017 for (i = 0; BINFO_BASE_ITERATE (type_binfo, i, binfo); i++)
4019 tree binfo_offset;
4021 if (BINFO_VIRTUAL_P (binfo))
4022 continue;
4024 tree orig_binfo;
4025 /* We cannot rely on BINFO_OFFSET being set for the base
4026 class yet, but the offsets for direct non-virtual
4027 bases can be calculated by going back to the TYPE. */
4028 orig_binfo = BINFO_BASE_BINFO (TYPE_BINFO (type), i);
4029 binfo_offset = size_binop (PLUS_EXPR,
4030 offset,
4031 BINFO_OFFSET (orig_binfo));
4033 r = walk_subobject_offsets (binfo,
4035 binfo_offset,
4036 offsets,
4037 max_offset,
4038 /*vbases_p=*/0);
4039 if (r)
4040 return r;
4043 if (CLASSTYPE_VBASECLASSES (type))
4045 unsigned ix;
4046 vec<tree, va_gc> *vbases;
4048 /* Iterate through the virtual base classes of TYPE. In G++
4049 3.2, we included virtual bases in the direct base class
4050 loop above, which results in incorrect results; the
4051 correct offsets for virtual bases are only known when
4052 working with the most derived type. */
4053 if (vbases_p)
4054 for (vbases = CLASSTYPE_VBASECLASSES (type), ix = 0;
4055 vec_safe_iterate (vbases, ix, &binfo); ix++)
4057 r = walk_subobject_offsets (binfo,
4059 size_binop (PLUS_EXPR,
4060 offset,
4061 BINFO_OFFSET (binfo)),
4062 offsets,
4063 max_offset,
4064 /*vbases_p=*/0);
4065 if (r)
4066 return r;
4068 else
4070 /* We still have to walk the primary base, if it is
4071 virtual. (If it is non-virtual, then it was walked
4072 above.) */
4073 tree vbase = get_primary_binfo (type_binfo);
4075 if (vbase && BINFO_VIRTUAL_P (vbase)
4076 && BINFO_PRIMARY_P (vbase)
4077 && BINFO_INHERITANCE_CHAIN (vbase) == type_binfo)
4079 r = (walk_subobject_offsets
4080 (vbase, f, offset,
4081 offsets, max_offset, /*vbases_p=*/0));
4082 if (r)
4083 return r;
4088 /* Iterate through the fields of TYPE. */
4089 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
4090 if (TREE_CODE (field) == FIELD_DECL
4091 && TREE_TYPE (field) != error_mark_node
4092 && !DECL_ARTIFICIAL (field))
4094 tree field_offset;
4096 field_offset = byte_position (field);
4098 r = walk_subobject_offsets (TREE_TYPE (field),
4100 size_binop (PLUS_EXPR,
4101 offset,
4102 field_offset),
4103 offsets,
4104 max_offset,
4105 /*vbases_p=*/1);
4106 if (r)
4107 return r;
4110 else if (TREE_CODE (type) == ARRAY_TYPE)
4112 tree element_type = strip_array_types (type);
4113 tree domain = TYPE_DOMAIN (type);
4114 tree index;
4116 /* Avoid recursing into objects that are not interesting. */
4117 if (!CLASS_TYPE_P (element_type)
4118 || !CLASSTYPE_CONTAINS_EMPTY_CLASS_P (element_type))
4119 return 0;
4121 /* Step through each of the elements in the array. */
4122 for (index = size_zero_node;
4123 !tree_int_cst_lt (TYPE_MAX_VALUE (domain), index);
4124 index = size_binop (PLUS_EXPR, index, size_one_node))
4126 r = walk_subobject_offsets (TREE_TYPE (type),
4128 offset,
4129 offsets,
4130 max_offset,
4131 /*vbases_p=*/1);
4132 if (r)
4133 return r;
4134 offset = size_binop (PLUS_EXPR, offset,
4135 TYPE_SIZE_UNIT (TREE_TYPE (type)));
4136 /* If this new OFFSET is bigger than the MAX_OFFSET, then
4137 there's no point in iterating through the remaining
4138 elements of the array. */
4139 if (max_offset && tree_int_cst_lt (max_offset, offset))
4140 break;
4144 return 0;
4147 /* Record all of the empty subobjects of TYPE (either a type or a
4148 binfo). If IS_DATA_MEMBER is true, then a non-static data member
4149 is being placed at OFFSET; otherwise, it is a base class that is
4150 being placed at OFFSET. */
4152 static void
4153 record_subobject_offsets (tree type,
4154 tree offset,
4155 splay_tree offsets,
4156 bool is_data_member)
4158 tree max_offset;
4159 /* If recording subobjects for a non-static data member or a
4160 non-empty base class , we do not need to record offsets beyond
4161 the size of the biggest empty class. Additional data members
4162 will go at the end of the class. Additional base classes will go
4163 either at offset zero (if empty, in which case they cannot
4164 overlap with offsets past the size of the biggest empty class) or
4165 at the end of the class.
4167 However, if we are placing an empty base class, then we must record
4168 all offsets, as either the empty class is at offset zero (where
4169 other empty classes might later be placed) or at the end of the
4170 class (where other objects might then be placed, so other empty
4171 subobjects might later overlap). */
4172 if (is_data_member
4173 || !is_empty_class (BINFO_TYPE (type)))
4174 max_offset = sizeof_biggest_empty_class;
4175 else
4176 max_offset = NULL_TREE;
4177 walk_subobject_offsets (type, record_subobject_offset, offset,
4178 offsets, max_offset, is_data_member);
4181 /* Returns nonzero if any of the empty subobjects of TYPE (located at
4182 OFFSET) conflict with entries in OFFSETS. If VBASES_P is nonzero,
4183 virtual bases of TYPE are examined. */
4185 static int
4186 layout_conflict_p (tree type,
4187 tree offset,
4188 splay_tree offsets,
4189 int vbases_p)
4191 splay_tree_node max_node;
4193 /* Get the node in OFFSETS that indicates the maximum offset where
4194 an empty subobject is located. */
4195 max_node = splay_tree_max (offsets);
4196 /* If there aren't any empty subobjects, then there's no point in
4197 performing this check. */
4198 if (!max_node)
4199 return 0;
4201 return walk_subobject_offsets (type, check_subobject_offset, offset,
4202 offsets, (tree) (max_node->key),
4203 vbases_p);
4206 /* DECL is a FIELD_DECL corresponding either to a base subobject of a
4207 non-static data member of the type indicated by RLI. BINFO is the
4208 binfo corresponding to the base subobject, OFFSETS maps offsets to
4209 types already located at those offsets. This function determines
4210 the position of the DECL. */
4212 static void
4213 layout_nonempty_base_or_field (record_layout_info rli,
4214 tree decl,
4215 tree binfo,
4216 splay_tree offsets)
4218 tree offset = NULL_TREE;
4219 bool field_p;
4220 tree type;
4222 if (binfo)
4224 /* For the purposes of determining layout conflicts, we want to
4225 use the class type of BINFO; TREE_TYPE (DECL) will be the
4226 CLASSTYPE_AS_BASE version, which does not contain entries for
4227 zero-sized bases. */
4228 type = TREE_TYPE (binfo);
4229 field_p = false;
4231 else
4233 type = TREE_TYPE (decl);
4234 field_p = true;
4237 /* Try to place the field. It may take more than one try if we have
4238 a hard time placing the field without putting two objects of the
4239 same type at the same address. */
4240 while (1)
4242 struct record_layout_info_s old_rli = *rli;
4244 /* Place this field. */
4245 place_field (rli, decl);
4246 offset = byte_position (decl);
4248 /* We have to check to see whether or not there is already
4249 something of the same type at the offset we're about to use.
4250 For example, consider:
4252 struct S {};
4253 struct T : public S { int i; };
4254 struct U : public S, public T {};
4256 Here, we put S at offset zero in U. Then, we can't put T at
4257 offset zero -- its S component would be at the same address
4258 as the S we already allocated. So, we have to skip ahead.
4259 Since all data members, including those whose type is an
4260 empty class, have nonzero size, any overlap can happen only
4261 with a direct or indirect base-class -- it can't happen with
4262 a data member. */
4263 /* In a union, overlap is permitted; all members are placed at
4264 offset zero. */
4265 if (TREE_CODE (rli->t) == UNION_TYPE)
4266 break;
4267 if (layout_conflict_p (field_p ? type : binfo, offset,
4268 offsets, field_p))
4270 /* Strip off the size allocated to this field. That puts us
4271 at the first place we could have put the field with
4272 proper alignment. */
4273 *rli = old_rli;
4275 /* Bump up by the alignment required for the type. */
4276 rli->bitpos
4277 = size_binop (PLUS_EXPR, rli->bitpos,
4278 bitsize_int (binfo
4279 ? CLASSTYPE_ALIGN (type)
4280 : TYPE_ALIGN (type)));
4281 normalize_rli (rli);
4283 else if (TREE_CODE (type) == NULLPTR_TYPE
4284 && warn_abi && abi_version_crosses (9))
4286 /* Before ABI v9, we were giving nullptr_t alignment of 1; if
4287 the offset wasn't aligned like a pointer when we started to
4288 layout this field, that affects its position. */
4289 tree pos = rli_size_unit_so_far (&old_rli);
4290 if (int_cst_value (pos) % TYPE_ALIGN_UNIT (ptr_type_node) != 0)
4292 if (abi_version_at_least (9))
4293 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wabi,
4294 "alignment of %qD increased in -fabi-version=9 "
4295 "(GCC 5.2)", decl);
4296 else
4297 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wabi, "alignment "
4298 "of %qD will increase in -fabi-version=9", decl);
4300 break;
4302 else
4303 /* There was no conflict. We're done laying out this field. */
4304 break;
4307 /* Now that we know where it will be placed, update its
4308 BINFO_OFFSET. */
4309 if (binfo && CLASS_TYPE_P (BINFO_TYPE (binfo)))
4310 /* Indirect virtual bases may have a nonzero BINFO_OFFSET at
4311 this point because their BINFO_OFFSET is copied from another
4312 hierarchy. Therefore, we may not need to add the entire
4313 OFFSET. */
4314 propagate_binfo_offsets (binfo,
4315 size_diffop_loc (input_location,
4316 convert (ssizetype, offset),
4317 convert (ssizetype,
4318 BINFO_OFFSET (binfo))));
4321 /* Returns true if TYPE is empty and OFFSET is nonzero. */
4323 static int
4324 empty_base_at_nonzero_offset_p (tree type,
4325 tree offset,
4326 splay_tree /*offsets*/)
4328 return is_empty_class (type) && !integer_zerop (offset);
4331 /* Layout the empty base BINFO. EOC indicates the byte currently just
4332 past the end of the class, and should be correctly aligned for a
4333 class of the type indicated by BINFO; OFFSETS gives the offsets of
4334 the empty bases allocated so far. T is the most derived
4335 type. Return nonzero iff we added it at the end. */
4337 static bool
4338 layout_empty_base (record_layout_info rli, tree binfo,
4339 tree eoc, splay_tree offsets)
4341 tree alignment;
4342 tree basetype = BINFO_TYPE (binfo);
4343 bool atend = false;
4345 /* This routine should only be used for empty classes. */
4346 gcc_assert (is_empty_class (basetype));
4347 alignment = ssize_int (CLASSTYPE_ALIGN_UNIT (basetype));
4349 if (!integer_zerop (BINFO_OFFSET (binfo)))
4350 propagate_binfo_offsets
4351 (binfo, size_diffop_loc (input_location,
4352 size_zero_node, BINFO_OFFSET (binfo)));
4354 /* This is an empty base class. We first try to put it at offset
4355 zero. */
4356 if (layout_conflict_p (binfo,
4357 BINFO_OFFSET (binfo),
4358 offsets,
4359 /*vbases_p=*/0))
4361 /* That didn't work. Now, we move forward from the next
4362 available spot in the class. */
4363 atend = true;
4364 propagate_binfo_offsets (binfo, convert (ssizetype, eoc));
4365 while (1)
4367 if (!layout_conflict_p (binfo,
4368 BINFO_OFFSET (binfo),
4369 offsets,
4370 /*vbases_p=*/0))
4371 /* We finally found a spot where there's no overlap. */
4372 break;
4374 /* There's overlap here, too. Bump along to the next spot. */
4375 propagate_binfo_offsets (binfo, alignment);
4379 if (CLASSTYPE_USER_ALIGN (basetype))
4381 rli->record_align = MAX (rli->record_align, CLASSTYPE_ALIGN (basetype));
4382 if (warn_packed)
4383 rli->unpacked_align = MAX (rli->unpacked_align, CLASSTYPE_ALIGN (basetype));
4384 TYPE_USER_ALIGN (rli->t) = 1;
4387 return atend;
4390 /* Layout the base given by BINFO in the class indicated by RLI.
4391 *BASE_ALIGN is a running maximum of the alignments of
4392 any base class. OFFSETS gives the location of empty base
4393 subobjects. T is the most derived type. Return nonzero if the new
4394 object cannot be nearly-empty. A new FIELD_DECL is inserted at
4395 *NEXT_FIELD, unless BINFO is for an empty base class.
4397 Returns the location at which the next field should be inserted. */
4399 static tree *
4400 build_base_field (record_layout_info rli, tree binfo,
4401 splay_tree offsets, tree *next_field)
4403 tree t = rli->t;
4404 tree basetype = BINFO_TYPE (binfo);
4406 if (!COMPLETE_TYPE_P (basetype))
4407 /* This error is now reported in xref_tag, thus giving better
4408 location information. */
4409 return next_field;
4411 /* Place the base class. */
4412 if (!is_empty_class (basetype))
4414 tree decl;
4416 /* The containing class is non-empty because it has a non-empty
4417 base class. */
4418 CLASSTYPE_EMPTY_P (t) = 0;
4420 /* Create the FIELD_DECL. */
4421 decl = build_decl (input_location,
4422 FIELD_DECL, NULL_TREE, CLASSTYPE_AS_BASE (basetype));
4423 DECL_ARTIFICIAL (decl) = 1;
4424 DECL_IGNORED_P (decl) = 1;
4425 DECL_FIELD_CONTEXT (decl) = t;
4426 if (CLASSTYPE_AS_BASE (basetype))
4428 DECL_SIZE (decl) = CLASSTYPE_SIZE (basetype);
4429 DECL_SIZE_UNIT (decl) = CLASSTYPE_SIZE_UNIT (basetype);
4430 DECL_ALIGN (decl) = CLASSTYPE_ALIGN (basetype);
4431 DECL_USER_ALIGN (decl) = CLASSTYPE_USER_ALIGN (basetype);
4432 DECL_MODE (decl) = TYPE_MODE (basetype);
4433 DECL_FIELD_IS_BASE (decl) = 1;
4435 /* Try to place the field. It may take more than one try if we
4436 have a hard time placing the field without putting two
4437 objects of the same type at the same address. */
4438 layout_nonempty_base_or_field (rli, decl, binfo, offsets);
4439 /* Add the new FIELD_DECL to the list of fields for T. */
4440 DECL_CHAIN (decl) = *next_field;
4441 *next_field = decl;
4442 next_field = &DECL_CHAIN (decl);
4445 else
4447 tree eoc;
4448 bool atend;
4450 /* On some platforms (ARM), even empty classes will not be
4451 byte-aligned. */
4452 eoc = round_up_loc (input_location,
4453 rli_size_unit_so_far (rli),
4454 CLASSTYPE_ALIGN_UNIT (basetype));
4455 atend = layout_empty_base (rli, binfo, eoc, offsets);
4456 /* A nearly-empty class "has no proper base class that is empty,
4457 not morally virtual, and at an offset other than zero." */
4458 if (!BINFO_VIRTUAL_P (binfo) && CLASSTYPE_NEARLY_EMPTY_P (t))
4460 if (atend)
4461 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
4462 /* The check above (used in G++ 3.2) is insufficient because
4463 an empty class placed at offset zero might itself have an
4464 empty base at a nonzero offset. */
4465 else if (walk_subobject_offsets (basetype,
4466 empty_base_at_nonzero_offset_p,
4467 size_zero_node,
4468 /*offsets=*/NULL,
4469 /*max_offset=*/NULL_TREE,
4470 /*vbases_p=*/true))
4471 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
4474 /* We do not create a FIELD_DECL for empty base classes because
4475 it might overlap some other field. We want to be able to
4476 create CONSTRUCTORs for the class by iterating over the
4477 FIELD_DECLs, and the back end does not handle overlapping
4478 FIELD_DECLs. */
4480 /* An empty virtual base causes a class to be non-empty
4481 -- but in that case we do not need to clear CLASSTYPE_EMPTY_P
4482 here because that was already done when the virtual table
4483 pointer was created. */
4486 /* Record the offsets of BINFO and its base subobjects. */
4487 record_subobject_offsets (binfo,
4488 BINFO_OFFSET (binfo),
4489 offsets,
4490 /*is_data_member=*/false);
4492 return next_field;
4495 /* Layout all of the non-virtual base classes. Record empty
4496 subobjects in OFFSETS. T is the most derived type. Return nonzero
4497 if the type cannot be nearly empty. The fields created
4498 corresponding to the base classes will be inserted at
4499 *NEXT_FIELD. */
4501 static void
4502 build_base_fields (record_layout_info rli,
4503 splay_tree offsets, tree *next_field)
4505 /* Chain to hold all the new FIELD_DECLs which stand in for base class
4506 subobjects. */
4507 tree t = rli->t;
4508 int n_baseclasses = BINFO_N_BASE_BINFOS (TYPE_BINFO (t));
4509 int i;
4511 /* The primary base class is always allocated first. */
4512 if (CLASSTYPE_HAS_PRIMARY_BASE_P (t))
4513 next_field = build_base_field (rli, CLASSTYPE_PRIMARY_BINFO (t),
4514 offsets, next_field);
4516 /* Now allocate the rest of the bases. */
4517 for (i = 0; i < n_baseclasses; ++i)
4519 tree base_binfo;
4521 base_binfo = BINFO_BASE_BINFO (TYPE_BINFO (t), i);
4523 /* The primary base was already allocated above, so we don't
4524 need to allocate it again here. */
4525 if (base_binfo == CLASSTYPE_PRIMARY_BINFO (t))
4526 continue;
4528 /* Virtual bases are added at the end (a primary virtual base
4529 will have already been added). */
4530 if (BINFO_VIRTUAL_P (base_binfo))
4531 continue;
4533 next_field = build_base_field (rli, base_binfo,
4534 offsets, next_field);
4538 /* Go through the TYPE_METHODS of T issuing any appropriate
4539 diagnostics, figuring out which methods override which other
4540 methods, and so forth. */
4542 static void
4543 check_methods (tree t)
4545 tree x;
4547 for (x = TYPE_METHODS (t); x; x = DECL_CHAIN (x))
4549 check_for_override (x, t);
4550 if (DECL_PURE_VIRTUAL_P (x) && (TREE_CODE (x) != FUNCTION_DECL || ! DECL_VINDEX (x)))
4551 error ("initializer specified for non-virtual method %q+D", x);
4552 /* The name of the field is the original field name
4553 Save this in auxiliary field for later overloading. */
4554 if (TREE_CODE (x) == FUNCTION_DECL && DECL_VINDEX (x))
4556 TYPE_POLYMORPHIC_P (t) = 1;
4557 if (DECL_PURE_VIRTUAL_P (x))
4558 vec_safe_push (CLASSTYPE_PURE_VIRTUALS (t), x);
4560 /* All user-provided destructors are non-trivial.
4561 Constructors and assignment ops are handled in
4562 grok_special_member_properties. */
4563 if (DECL_DESTRUCTOR_P (x) && user_provided_p (x))
4564 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t) = 1;
4568 /* FN is a constructor or destructor. Clone the declaration to create
4569 a specialized in-charge or not-in-charge version, as indicated by
4570 NAME. */
4572 static tree
4573 build_clone (tree fn, tree name)
4575 tree parms;
4576 tree clone;
4578 /* Copy the function. */
4579 clone = copy_decl (fn);
4580 /* Reset the function name. */
4581 DECL_NAME (clone) = name;
4582 /* Remember where this function came from. */
4583 DECL_ABSTRACT_ORIGIN (clone) = fn;
4584 /* Make it easy to find the CLONE given the FN. */
4585 DECL_CHAIN (clone) = DECL_CHAIN (fn);
4586 DECL_CHAIN (fn) = clone;
4588 /* If this is a template, do the rest on the DECL_TEMPLATE_RESULT. */
4589 if (TREE_CODE (clone) == TEMPLATE_DECL)
4591 tree result = build_clone (DECL_TEMPLATE_RESULT (clone), name);
4592 DECL_TEMPLATE_RESULT (clone) = result;
4593 DECL_TEMPLATE_INFO (result) = copy_node (DECL_TEMPLATE_INFO (result));
4594 DECL_TI_TEMPLATE (result) = clone;
4595 TREE_TYPE (clone) = TREE_TYPE (result);
4596 return clone;
4599 SET_DECL_ASSEMBLER_NAME (clone, NULL_TREE);
4600 DECL_CLONED_FUNCTION (clone) = fn;
4601 /* There's no pending inline data for this function. */
4602 DECL_PENDING_INLINE_INFO (clone) = NULL;
4603 DECL_PENDING_INLINE_P (clone) = 0;
4605 /* The base-class destructor is not virtual. */
4606 if (name == base_dtor_identifier)
4608 DECL_VIRTUAL_P (clone) = 0;
4609 if (TREE_CODE (clone) != TEMPLATE_DECL)
4610 DECL_VINDEX (clone) = NULL_TREE;
4613 /* If there was an in-charge parameter, drop it from the function
4614 type. */
4615 if (DECL_HAS_IN_CHARGE_PARM_P (clone))
4617 tree basetype;
4618 tree parmtypes;
4619 tree exceptions;
4621 exceptions = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (clone));
4622 basetype = TYPE_METHOD_BASETYPE (TREE_TYPE (clone));
4623 parmtypes = TYPE_ARG_TYPES (TREE_TYPE (clone));
4624 /* Skip the `this' parameter. */
4625 parmtypes = TREE_CHAIN (parmtypes);
4626 /* Skip the in-charge parameter. */
4627 parmtypes = TREE_CHAIN (parmtypes);
4628 /* And the VTT parm, in a complete [cd]tor. */
4629 if (DECL_HAS_VTT_PARM_P (fn)
4630 && ! DECL_NEEDS_VTT_PARM_P (clone))
4631 parmtypes = TREE_CHAIN (parmtypes);
4632 /* If this is subobject constructor or destructor, add the vtt
4633 parameter. */
4634 TREE_TYPE (clone)
4635 = build_method_type_directly (basetype,
4636 TREE_TYPE (TREE_TYPE (clone)),
4637 parmtypes);
4638 if (exceptions)
4639 TREE_TYPE (clone) = build_exception_variant (TREE_TYPE (clone),
4640 exceptions);
4641 TREE_TYPE (clone)
4642 = cp_build_type_attribute_variant (TREE_TYPE (clone),
4643 TYPE_ATTRIBUTES (TREE_TYPE (fn)));
4646 /* Copy the function parameters. */
4647 DECL_ARGUMENTS (clone) = copy_list (DECL_ARGUMENTS (clone));
4648 /* Remove the in-charge parameter. */
4649 if (DECL_HAS_IN_CHARGE_PARM_P (clone))
4651 DECL_CHAIN (DECL_ARGUMENTS (clone))
4652 = DECL_CHAIN (DECL_CHAIN (DECL_ARGUMENTS (clone)));
4653 DECL_HAS_IN_CHARGE_PARM_P (clone) = 0;
4655 /* And the VTT parm, in a complete [cd]tor. */
4656 if (DECL_HAS_VTT_PARM_P (fn))
4658 if (DECL_NEEDS_VTT_PARM_P (clone))
4659 DECL_HAS_VTT_PARM_P (clone) = 1;
4660 else
4662 DECL_CHAIN (DECL_ARGUMENTS (clone))
4663 = DECL_CHAIN (DECL_CHAIN (DECL_ARGUMENTS (clone)));
4664 DECL_HAS_VTT_PARM_P (clone) = 0;
4668 for (parms = DECL_ARGUMENTS (clone); parms; parms = DECL_CHAIN (parms))
4670 DECL_CONTEXT (parms) = clone;
4671 cxx_dup_lang_specific_decl (parms);
4674 /* Create the RTL for this function. */
4675 SET_DECL_RTL (clone, NULL);
4676 rest_of_decl_compilation (clone, /*top_level=*/1, at_eof);
4678 if (pch_file)
4679 note_decl_for_pch (clone);
4681 return clone;
4684 /* Implementation of DECL_CLONED_FUNCTION and DECL_CLONED_FUNCTION_P, do
4685 not invoke this function directly.
4687 For a non-thunk function, returns the address of the slot for storing
4688 the function it is a clone of. Otherwise returns NULL_TREE.
4690 If JUST_TESTING, looks through TEMPLATE_DECL and returns NULL if
4691 cloned_function is unset. This is to support the separate
4692 DECL_CLONED_FUNCTION and DECL_CLONED_FUNCTION_P modes; using the latter
4693 on a template makes sense, but not the former. */
4695 tree *
4696 decl_cloned_function_p (const_tree decl, bool just_testing)
4698 tree *ptr;
4699 if (just_testing)
4700 decl = STRIP_TEMPLATE (decl);
4702 if (TREE_CODE (decl) != FUNCTION_DECL
4703 || !DECL_LANG_SPECIFIC (decl)
4704 || DECL_LANG_SPECIFIC (decl)->u.fn.thunk_p)
4706 #if defined ENABLE_TREE_CHECKING && (GCC_VERSION >= 2007)
4707 if (!just_testing)
4708 lang_check_failed (__FILE__, __LINE__, __FUNCTION__);
4709 else
4710 #endif
4711 return NULL;
4714 ptr = &DECL_LANG_SPECIFIC (decl)->u.fn.u5.cloned_function;
4715 if (just_testing && *ptr == NULL_TREE)
4716 return NULL;
4717 else
4718 return ptr;
4721 /* Produce declarations for all appropriate clones of FN. If
4722 UPDATE_METHOD_VEC_P is nonzero, the clones are added to the
4723 CLASTYPE_METHOD_VEC as well. */
4725 void
4726 clone_function_decl (tree fn, int update_method_vec_p)
4728 tree clone;
4730 /* Avoid inappropriate cloning. */
4731 if (DECL_CHAIN (fn)
4732 && DECL_CLONED_FUNCTION_P (DECL_CHAIN (fn)))
4733 return;
4735 if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (fn))
4737 /* For each constructor, we need two variants: an in-charge version
4738 and a not-in-charge version. */
4739 clone = build_clone (fn, complete_ctor_identifier);
4740 if (update_method_vec_p)
4741 add_method (DECL_CONTEXT (clone), clone, NULL_TREE);
4742 clone = build_clone (fn, base_ctor_identifier);
4743 if (update_method_vec_p)
4744 add_method (DECL_CONTEXT (clone), clone, NULL_TREE);
4746 else
4748 gcc_assert (DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (fn));
4750 /* For each destructor, we need three variants: an in-charge
4751 version, a not-in-charge version, and an in-charge deleting
4752 version. We clone the deleting version first because that
4753 means it will go second on the TYPE_METHODS list -- and that
4754 corresponds to the correct layout order in the virtual
4755 function table.
4757 For a non-virtual destructor, we do not build a deleting
4758 destructor. */
4759 if (DECL_VIRTUAL_P (fn))
4761 clone = build_clone (fn, deleting_dtor_identifier);
4762 if (update_method_vec_p)
4763 add_method (DECL_CONTEXT (clone), clone, NULL_TREE);
4765 clone = build_clone (fn, complete_dtor_identifier);
4766 if (update_method_vec_p)
4767 add_method (DECL_CONTEXT (clone), clone, NULL_TREE);
4768 clone = build_clone (fn, base_dtor_identifier);
4769 if (update_method_vec_p)
4770 add_method (DECL_CONTEXT (clone), clone, NULL_TREE);
4773 /* Note that this is an abstract function that is never emitted. */
4774 DECL_ABSTRACT_P (fn) = true;
4777 /* DECL is an in charge constructor, which is being defined. This will
4778 have had an in class declaration, from whence clones were
4779 declared. An out-of-class definition can specify additional default
4780 arguments. As it is the clones that are involved in overload
4781 resolution, we must propagate the information from the DECL to its
4782 clones. */
4784 void
4785 adjust_clone_args (tree decl)
4787 tree clone;
4789 for (clone = DECL_CHAIN (decl); clone && DECL_CLONED_FUNCTION_P (clone);
4790 clone = DECL_CHAIN (clone))
4792 tree orig_clone_parms = TYPE_ARG_TYPES (TREE_TYPE (clone));
4793 tree orig_decl_parms = TYPE_ARG_TYPES (TREE_TYPE (decl));
4794 tree decl_parms, clone_parms;
4796 clone_parms = orig_clone_parms;
4798 /* Skip the 'this' parameter. */
4799 orig_clone_parms = TREE_CHAIN (orig_clone_parms);
4800 orig_decl_parms = TREE_CHAIN (orig_decl_parms);
4802 if (DECL_HAS_IN_CHARGE_PARM_P (decl))
4803 orig_decl_parms = TREE_CHAIN (orig_decl_parms);
4804 if (DECL_HAS_VTT_PARM_P (decl))
4805 orig_decl_parms = TREE_CHAIN (orig_decl_parms);
4807 clone_parms = orig_clone_parms;
4808 if (DECL_HAS_VTT_PARM_P (clone))
4809 clone_parms = TREE_CHAIN (clone_parms);
4811 for (decl_parms = orig_decl_parms; decl_parms;
4812 decl_parms = TREE_CHAIN (decl_parms),
4813 clone_parms = TREE_CHAIN (clone_parms))
4815 gcc_assert (same_type_p (TREE_TYPE (decl_parms),
4816 TREE_TYPE (clone_parms)));
4818 if (TREE_PURPOSE (decl_parms) && !TREE_PURPOSE (clone_parms))
4820 /* A default parameter has been added. Adjust the
4821 clone's parameters. */
4822 tree exceptions = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (clone));
4823 tree attrs = TYPE_ATTRIBUTES (TREE_TYPE (clone));
4824 tree basetype = TYPE_METHOD_BASETYPE (TREE_TYPE (clone));
4825 tree type;
4827 clone_parms = orig_decl_parms;
4829 if (DECL_HAS_VTT_PARM_P (clone))
4831 clone_parms = tree_cons (TREE_PURPOSE (orig_clone_parms),
4832 TREE_VALUE (orig_clone_parms),
4833 clone_parms);
4834 TREE_TYPE (clone_parms) = TREE_TYPE (orig_clone_parms);
4836 type = build_method_type_directly (basetype,
4837 TREE_TYPE (TREE_TYPE (clone)),
4838 clone_parms);
4839 if (exceptions)
4840 type = build_exception_variant (type, exceptions);
4841 if (attrs)
4842 type = cp_build_type_attribute_variant (type, attrs);
4843 TREE_TYPE (clone) = type;
4845 clone_parms = NULL_TREE;
4846 break;
4849 gcc_assert (!clone_parms);
4853 /* For each of the constructors and destructors in T, create an
4854 in-charge and not-in-charge variant. */
4856 static void
4857 clone_constructors_and_destructors (tree t)
4859 tree fns;
4861 /* If for some reason we don't have a CLASSTYPE_METHOD_VEC, we bail
4862 out now. */
4863 if (!CLASSTYPE_METHOD_VEC (t))
4864 return;
4866 for (fns = CLASSTYPE_CONSTRUCTORS (t); fns; fns = OVL_NEXT (fns))
4867 clone_function_decl (OVL_CURRENT (fns), /*update_method_vec_p=*/1);
4868 for (fns = CLASSTYPE_DESTRUCTORS (t); fns; fns = OVL_NEXT (fns))
4869 clone_function_decl (OVL_CURRENT (fns), /*update_method_vec_p=*/1);
4872 /* Deduce noexcept for a destructor DTOR. */
4874 void
4875 deduce_noexcept_on_destructor (tree dtor)
4877 if (!TYPE_RAISES_EXCEPTIONS (TREE_TYPE (dtor)))
4879 tree eh_spec = unevaluated_noexcept_spec ();
4880 TREE_TYPE (dtor) = build_exception_variant (TREE_TYPE (dtor), eh_spec);
4884 /* For each destructor in T, deduce noexcept:
4886 12.4/3: A declaration of a destructor that does not have an
4887 exception-specification is implicitly considered to have the
4888 same exception-specification as an implicit declaration (15.4). */
4890 static void
4891 deduce_noexcept_on_destructors (tree t)
4893 /* If for some reason we don't have a CLASSTYPE_METHOD_VEC, we bail
4894 out now. */
4895 if (!CLASSTYPE_METHOD_VEC (t))
4896 return;
4898 for (tree fns = CLASSTYPE_DESTRUCTORS (t); fns; fns = OVL_NEXT (fns))
4899 deduce_noexcept_on_destructor (OVL_CURRENT (fns));
4902 /* Subroutine of set_one_vmethod_tm_attributes. Search base classes
4903 of TYPE for virtual functions which FNDECL overrides. Return a
4904 mask of the tm attributes found therein. */
4906 static int
4907 look_for_tm_attr_overrides (tree type, tree fndecl)
4909 tree binfo = TYPE_BINFO (type);
4910 tree base_binfo;
4911 int ix, found = 0;
4913 for (ix = 0; BINFO_BASE_ITERATE (binfo, ix, base_binfo); ++ix)
4915 tree o, basetype = BINFO_TYPE (base_binfo);
4917 if (!TYPE_POLYMORPHIC_P (basetype))
4918 continue;
4920 o = look_for_overrides_here (basetype, fndecl);
4921 if (o)
4922 found |= tm_attr_to_mask (find_tm_attribute
4923 (TYPE_ATTRIBUTES (TREE_TYPE (o))));
4924 else
4925 found |= look_for_tm_attr_overrides (basetype, fndecl);
4928 return found;
4931 /* Subroutine of set_method_tm_attributes. Handle the checks and
4932 inheritance for one virtual method FNDECL. */
4934 static void
4935 set_one_vmethod_tm_attributes (tree type, tree fndecl)
4937 tree tm_attr;
4938 int found, have;
4940 found = look_for_tm_attr_overrides (type, fndecl);
4942 /* If FNDECL doesn't actually override anything (i.e. T is the
4943 class that first declares FNDECL virtual), then we're done. */
4944 if (found == 0)
4945 return;
4947 tm_attr = find_tm_attribute (TYPE_ATTRIBUTES (TREE_TYPE (fndecl)));
4948 have = tm_attr_to_mask (tm_attr);
4950 /* Intel STM Language Extension 3.0, Section 4.2 table 4:
4951 tm_pure must match exactly, otherwise no weakening of
4952 tm_safe > tm_callable > nothing. */
4953 /* ??? The tm_pure attribute didn't make the transition to the
4954 multivendor language spec. */
4955 if (have == TM_ATTR_PURE)
4957 if (found != TM_ATTR_PURE)
4959 found &= -found;
4960 goto err_override;
4963 /* If the overridden function is tm_pure, then FNDECL must be. */
4964 else if (found == TM_ATTR_PURE && tm_attr)
4965 goto err_override;
4966 /* Look for base class combinations that cannot be satisfied. */
4967 else if (found != TM_ATTR_PURE && (found & TM_ATTR_PURE))
4969 found &= ~TM_ATTR_PURE;
4970 found &= -found;
4971 error_at (DECL_SOURCE_LOCATION (fndecl),
4972 "method overrides both %<transaction_pure%> and %qE methods",
4973 tm_mask_to_attr (found));
4975 /* If FNDECL did not declare an attribute, then inherit the most
4976 restrictive one. */
4977 else if (tm_attr == NULL)
4979 apply_tm_attr (fndecl, tm_mask_to_attr (found & -found));
4981 /* Otherwise validate that we're not weaker than a function
4982 that is being overridden. */
4983 else
4985 found &= -found;
4986 if (found <= TM_ATTR_CALLABLE && have > found)
4987 goto err_override;
4989 return;
4991 err_override:
4992 error_at (DECL_SOURCE_LOCATION (fndecl),
4993 "method declared %qE overriding %qE method",
4994 tm_attr, tm_mask_to_attr (found));
4997 /* For each of the methods in T, propagate a class-level tm attribute. */
4999 static void
5000 set_method_tm_attributes (tree t)
5002 tree class_tm_attr, fndecl;
5004 /* Don't bother collecting tm attributes if transactional memory
5005 support is not enabled. */
5006 if (!flag_tm)
5007 return;
5009 /* Process virtual methods first, as they inherit directly from the
5010 base virtual function and also require validation of new attributes. */
5011 if (TYPE_CONTAINS_VPTR_P (t))
5013 tree vchain;
5014 for (vchain = BINFO_VIRTUALS (TYPE_BINFO (t)); vchain;
5015 vchain = TREE_CHAIN (vchain))
5017 fndecl = BV_FN (vchain);
5018 if (DECL_THUNK_P (fndecl))
5019 fndecl = THUNK_TARGET (fndecl);
5020 set_one_vmethod_tm_attributes (t, fndecl);
5024 /* If the class doesn't have an attribute, nothing more to do. */
5025 class_tm_attr = find_tm_attribute (TYPE_ATTRIBUTES (t));
5026 if (class_tm_attr == NULL)
5027 return;
5029 /* Any method that does not yet have a tm attribute inherits
5030 the one from the class. */
5031 for (fndecl = TYPE_METHODS (t); fndecl; fndecl = TREE_CHAIN (fndecl))
5033 if (!find_tm_attribute (TYPE_ATTRIBUTES (TREE_TYPE (fndecl))))
5034 apply_tm_attr (fndecl, class_tm_attr);
5038 /* Returns true iff class T has a user-defined constructor other than
5039 the default constructor. */
5041 bool
5042 type_has_user_nondefault_constructor (tree t)
5044 tree fns;
5046 if (!TYPE_HAS_USER_CONSTRUCTOR (t))
5047 return false;
5049 for (fns = CLASSTYPE_CONSTRUCTORS (t); fns; fns = OVL_NEXT (fns))
5051 tree fn = OVL_CURRENT (fns);
5052 if (!DECL_ARTIFICIAL (fn)
5053 && (TREE_CODE (fn) == TEMPLATE_DECL
5054 || (skip_artificial_parms_for (fn, DECL_ARGUMENTS (fn))
5055 != NULL_TREE)))
5056 return true;
5059 return false;
5062 /* Returns the defaulted constructor if T has one. Otherwise, returns
5063 NULL_TREE. */
5065 tree
5066 in_class_defaulted_default_constructor (tree t)
5068 tree fns, args;
5070 if (!TYPE_HAS_USER_CONSTRUCTOR (t))
5071 return NULL_TREE;
5073 for (fns = CLASSTYPE_CONSTRUCTORS (t); fns; fns = OVL_NEXT (fns))
5075 tree fn = OVL_CURRENT (fns);
5077 if (DECL_DEFAULTED_IN_CLASS_P (fn))
5079 args = FUNCTION_FIRST_USER_PARMTYPE (fn);
5080 while (args && TREE_PURPOSE (args))
5081 args = TREE_CHAIN (args);
5082 if (!args || args == void_list_node)
5083 return fn;
5087 return NULL_TREE;
5090 /* Returns true iff FN is a user-provided function, i.e. user-declared
5091 and not defaulted at its first declaration; or explicit, private,
5092 protected, or non-const. */
5094 bool
5095 user_provided_p (tree fn)
5097 if (TREE_CODE (fn) == TEMPLATE_DECL)
5098 return true;
5099 else
5100 return (!DECL_ARTIFICIAL (fn)
5101 && !(DECL_INITIALIZED_IN_CLASS_P (fn)
5102 && (DECL_DEFAULTED_FN (fn) || DECL_DELETED_FN (fn))));
5105 /* Returns true iff class T has a user-provided constructor. */
5107 bool
5108 type_has_user_provided_constructor (tree t)
5110 tree fns;
5112 if (!CLASS_TYPE_P (t))
5113 return false;
5115 if (!TYPE_HAS_USER_CONSTRUCTOR (t))
5116 return false;
5118 /* This can happen in error cases; avoid crashing. */
5119 if (!CLASSTYPE_METHOD_VEC (t))
5120 return false;
5122 for (fns = CLASSTYPE_CONSTRUCTORS (t); fns; fns = OVL_NEXT (fns))
5123 if (user_provided_p (OVL_CURRENT (fns)))
5124 return true;
5126 return false;
5129 /* Returns true iff class T has a non-user-provided (i.e. implicitly
5130 declared or explicitly defaulted in the class body) default
5131 constructor. */
5133 bool
5134 type_has_non_user_provided_default_constructor (tree t)
5136 tree fns;
5138 if (!TYPE_HAS_DEFAULT_CONSTRUCTOR (t))
5139 return false;
5140 if (CLASSTYPE_LAZY_DEFAULT_CTOR (t))
5141 return true;
5143 for (fns = CLASSTYPE_CONSTRUCTORS (t); fns; fns = OVL_NEXT (fns))
5145 tree fn = OVL_CURRENT (fns);
5146 if (TREE_CODE (fn) == FUNCTION_DECL
5147 && !user_provided_p (fn)
5148 && sufficient_parms_p (FUNCTION_FIRST_USER_PARMTYPE (fn)))
5149 return true;
5152 return false;
5155 /* TYPE is being used as a virtual base, and has a non-trivial move
5156 assignment. Return true if this is due to there being a user-provided
5157 move assignment in TYPE or one of its subobjects; if there isn't, then
5158 multiple move assignment can't cause any harm. */
5160 bool
5161 vbase_has_user_provided_move_assign (tree type)
5163 /* Does the type itself have a user-provided move assignment operator? */
5164 for (tree fns
5165 = lookup_fnfields_slot_nolazy (type, ansi_assopname (NOP_EXPR));
5166 fns; fns = OVL_NEXT (fns))
5168 tree fn = OVL_CURRENT (fns);
5169 if (move_fn_p (fn) && user_provided_p (fn))
5170 return true;
5173 /* Do any of its bases? */
5174 tree binfo = TYPE_BINFO (type);
5175 tree base_binfo;
5176 for (int i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
5177 if (vbase_has_user_provided_move_assign (BINFO_TYPE (base_binfo)))
5178 return true;
5180 /* Or non-static data members? */
5181 for (tree field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
5183 if (TREE_CODE (field) == FIELD_DECL
5184 && CLASS_TYPE_P (TREE_TYPE (field))
5185 && vbase_has_user_provided_move_assign (TREE_TYPE (field)))
5186 return true;
5189 /* Seems not. */
5190 return false;
5193 /* If default-initialization leaves part of TYPE uninitialized, returns
5194 a DECL for the field or TYPE itself (DR 253). */
5196 tree
5197 default_init_uninitialized_part (tree type)
5199 tree t, r, binfo;
5200 int i;
5202 type = strip_array_types (type);
5203 if (!CLASS_TYPE_P (type))
5204 return type;
5205 if (!type_has_non_user_provided_default_constructor (type))
5206 return NULL_TREE;
5207 for (binfo = TYPE_BINFO (type), i = 0;
5208 BINFO_BASE_ITERATE (binfo, i, t); ++i)
5210 r = default_init_uninitialized_part (BINFO_TYPE (t));
5211 if (r)
5212 return r;
5214 for (t = TYPE_FIELDS (type); t; t = DECL_CHAIN (t))
5215 if (TREE_CODE (t) == FIELD_DECL
5216 && !DECL_ARTIFICIAL (t)
5217 && !DECL_INITIAL (t))
5219 r = default_init_uninitialized_part (TREE_TYPE (t));
5220 if (r)
5221 return DECL_P (r) ? r : t;
5224 return NULL_TREE;
5227 /* Returns true iff for class T, a trivial synthesized default constructor
5228 would be constexpr. */
5230 bool
5231 trivial_default_constructor_is_constexpr (tree t)
5233 /* A defaulted trivial default constructor is constexpr
5234 if there is nothing to initialize. */
5235 gcc_assert (!TYPE_HAS_COMPLEX_DFLT (t));
5236 return is_really_empty_class (t);
5239 /* Returns true iff class T has a constexpr default constructor. */
5241 bool
5242 type_has_constexpr_default_constructor (tree t)
5244 tree fns;
5246 if (!CLASS_TYPE_P (t))
5248 /* The caller should have stripped an enclosing array. */
5249 gcc_assert (TREE_CODE (t) != ARRAY_TYPE);
5250 return false;
5252 if (CLASSTYPE_LAZY_DEFAULT_CTOR (t))
5254 if (!TYPE_HAS_COMPLEX_DFLT (t))
5255 return trivial_default_constructor_is_constexpr (t);
5256 /* Non-trivial, we need to check subobject constructors. */
5257 lazily_declare_fn (sfk_constructor, t);
5259 fns = locate_ctor (t);
5260 return (fns && DECL_DECLARED_CONSTEXPR_P (fns));
5263 /* Returns true iff class TYPE has a virtual destructor. */
5265 bool
5266 type_has_virtual_destructor (tree type)
5268 tree dtor;
5270 if (!CLASS_TYPE_P (type))
5271 return false;
5273 gcc_assert (COMPLETE_TYPE_P (type));
5274 dtor = CLASSTYPE_DESTRUCTORS (type);
5275 return (dtor && DECL_VIRTUAL_P (dtor));
5278 /* Returns true iff class T has a move constructor. */
5280 bool
5281 type_has_move_constructor (tree t)
5283 tree fns;
5285 if (CLASSTYPE_LAZY_MOVE_CTOR (t))
5287 gcc_assert (COMPLETE_TYPE_P (t));
5288 lazily_declare_fn (sfk_move_constructor, t);
5291 if (!CLASSTYPE_METHOD_VEC (t))
5292 return false;
5294 for (fns = CLASSTYPE_CONSTRUCTORS (t); fns; fns = OVL_NEXT (fns))
5295 if (move_fn_p (OVL_CURRENT (fns)))
5296 return true;
5298 return false;
5301 /* Returns true iff class T has a move assignment operator. */
5303 bool
5304 type_has_move_assign (tree t)
5306 tree fns;
5308 if (CLASSTYPE_LAZY_MOVE_ASSIGN (t))
5310 gcc_assert (COMPLETE_TYPE_P (t));
5311 lazily_declare_fn (sfk_move_assignment, t);
5314 for (fns = lookup_fnfields_slot_nolazy (t, ansi_assopname (NOP_EXPR));
5315 fns; fns = OVL_NEXT (fns))
5316 if (move_fn_p (OVL_CURRENT (fns)))
5317 return true;
5319 return false;
5322 /* Returns true iff class T has a move constructor that was explicitly
5323 declared in the class body. Note that this is different from
5324 "user-provided", which doesn't include functions that are defaulted in
5325 the class. */
5327 bool
5328 type_has_user_declared_move_constructor (tree t)
5330 tree fns;
5332 if (CLASSTYPE_LAZY_MOVE_CTOR (t))
5333 return false;
5335 if (!CLASSTYPE_METHOD_VEC (t))
5336 return false;
5338 for (fns = CLASSTYPE_CONSTRUCTORS (t); fns; fns = OVL_NEXT (fns))
5340 tree fn = OVL_CURRENT (fns);
5341 if (move_fn_p (fn) && !DECL_ARTIFICIAL (fn))
5342 return true;
5345 return false;
5348 /* Returns true iff class T has a move assignment operator that was
5349 explicitly declared in the class body. */
5351 bool
5352 type_has_user_declared_move_assign (tree t)
5354 tree fns;
5356 if (CLASSTYPE_LAZY_MOVE_ASSIGN (t))
5357 return false;
5359 for (fns = lookup_fnfields_slot_nolazy (t, ansi_assopname (NOP_EXPR));
5360 fns; fns = OVL_NEXT (fns))
5362 tree fn = OVL_CURRENT (fns);
5363 if (move_fn_p (fn) && !DECL_ARTIFICIAL (fn))
5364 return true;
5367 return false;
5370 /* Nonzero if we need to build up a constructor call when initializing an
5371 object of this class, either because it has a user-declared constructor
5372 or because it doesn't have a default constructor (so we need to give an
5373 error if no initializer is provided). Use TYPE_NEEDS_CONSTRUCTING when
5374 what you care about is whether or not an object can be produced by a
5375 constructor (e.g. so we don't set TREE_READONLY on const variables of
5376 such type); use this function when what you care about is whether or not
5377 to try to call a constructor to create an object. The latter case is
5378 the former plus some cases of constructors that cannot be called. */
5380 bool
5381 type_build_ctor_call (tree t)
5383 tree inner;
5384 if (TYPE_NEEDS_CONSTRUCTING (t))
5385 return true;
5386 inner = strip_array_types (t);
5387 if (!CLASS_TYPE_P (inner) || ANON_AGGR_TYPE_P (inner))
5388 return false;
5389 if (!TYPE_HAS_DEFAULT_CONSTRUCTOR (inner))
5390 return true;
5391 if (cxx_dialect < cxx11)
5392 return false;
5393 /* A user-declared constructor might be private, and a constructor might
5394 be trivial but deleted. */
5395 for (tree fns = lookup_fnfields_slot (inner, complete_ctor_identifier);
5396 fns; fns = OVL_NEXT (fns))
5398 tree fn = OVL_CURRENT (fns);
5399 if (!DECL_ARTIFICIAL (fn)
5400 || DECL_DELETED_FN (fn))
5401 return true;
5403 return false;
5406 /* Like type_build_ctor_call, but for destructors. */
5408 bool
5409 type_build_dtor_call (tree t)
5411 tree inner;
5412 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t))
5413 return true;
5414 inner = strip_array_types (t);
5415 if (!CLASS_TYPE_P (inner) || ANON_AGGR_TYPE_P (inner)
5416 || !COMPLETE_TYPE_P (inner))
5417 return false;
5418 if (cxx_dialect < cxx11)
5419 return false;
5420 /* A user-declared destructor might be private, and a destructor might
5421 be trivial but deleted. */
5422 for (tree fns = lookup_fnfields_slot (inner, complete_dtor_identifier);
5423 fns; fns = OVL_NEXT (fns))
5425 tree fn = OVL_CURRENT (fns);
5426 if (!DECL_ARTIFICIAL (fn)
5427 || DECL_DELETED_FN (fn))
5428 return true;
5430 return false;
5433 /* Remove all zero-width bit-fields from T. */
5435 static void
5436 remove_zero_width_bit_fields (tree t)
5438 tree *fieldsp;
5440 fieldsp = &TYPE_FIELDS (t);
5441 while (*fieldsp)
5443 if (TREE_CODE (*fieldsp) == FIELD_DECL
5444 && DECL_C_BIT_FIELD (*fieldsp)
5445 /* We should not be confused by the fact that grokbitfield
5446 temporarily sets the width of the bit field into
5447 DECL_INITIAL (*fieldsp).
5448 check_bitfield_decl eventually sets DECL_SIZE (*fieldsp)
5449 to that width. */
5450 && (DECL_SIZE (*fieldsp) == NULL_TREE
5451 || integer_zerop (DECL_SIZE (*fieldsp))))
5452 *fieldsp = DECL_CHAIN (*fieldsp);
5453 else
5454 fieldsp = &DECL_CHAIN (*fieldsp);
5458 /* Returns TRUE iff we need a cookie when dynamically allocating an
5459 array whose elements have the indicated class TYPE. */
5461 static bool
5462 type_requires_array_cookie (tree type)
5464 tree fns;
5465 bool has_two_argument_delete_p = false;
5467 gcc_assert (CLASS_TYPE_P (type));
5469 /* If there's a non-trivial destructor, we need a cookie. In order
5470 to iterate through the array calling the destructor for each
5471 element, we'll have to know how many elements there are. */
5472 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
5473 return true;
5475 /* If the usual deallocation function is a two-argument whose second
5476 argument is of type `size_t', then we have to pass the size of
5477 the array to the deallocation function, so we will need to store
5478 a cookie. */
5479 fns = lookup_fnfields (TYPE_BINFO (type),
5480 ansi_opname (VEC_DELETE_EXPR),
5481 /*protect=*/0);
5482 /* If there are no `operator []' members, or the lookup is
5483 ambiguous, then we don't need a cookie. */
5484 if (!fns || fns == error_mark_node)
5485 return false;
5486 /* Loop through all of the functions. */
5487 for (fns = BASELINK_FUNCTIONS (fns); fns; fns = OVL_NEXT (fns))
5489 tree fn;
5490 tree second_parm;
5492 /* Select the current function. */
5493 fn = OVL_CURRENT (fns);
5494 /* See if this function is a one-argument delete function. If
5495 it is, then it will be the usual deallocation function. */
5496 second_parm = TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (fn)));
5497 if (second_parm == void_list_node)
5498 return false;
5499 /* Do not consider this function if its second argument is an
5500 ellipsis. */
5501 if (!second_parm)
5502 continue;
5503 /* Otherwise, if we have a two-argument function and the second
5504 argument is `size_t', it will be the usual deallocation
5505 function -- unless there is one-argument function, too. */
5506 if (TREE_CHAIN (second_parm) == void_list_node
5507 && same_type_p (TREE_VALUE (second_parm), size_type_node))
5508 has_two_argument_delete_p = true;
5511 return has_two_argument_delete_p;
5514 /* Finish computing the `literal type' property of class type T.
5516 At this point, we have already processed base classes and
5517 non-static data members. We need to check whether the copy
5518 constructor is trivial, the destructor is trivial, and there
5519 is a trivial default constructor or at least one constexpr
5520 constructor other than the copy constructor. */
5522 static void
5523 finalize_literal_type_property (tree t)
5525 tree fn;
5527 if (cxx_dialect < cxx11
5528 || TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t))
5529 CLASSTYPE_LITERAL_P (t) = false;
5530 else if (CLASSTYPE_LITERAL_P (t) && !TYPE_HAS_TRIVIAL_DFLT (t)
5531 && CLASSTYPE_NON_AGGREGATE (t)
5532 && !TYPE_HAS_CONSTEXPR_CTOR (t))
5533 CLASSTYPE_LITERAL_P (t) = false;
5535 if (!CLASSTYPE_LITERAL_P (t))
5536 for (fn = TYPE_METHODS (t); fn; fn = DECL_CHAIN (fn))
5537 if (DECL_DECLARED_CONSTEXPR_P (fn)
5538 && TREE_CODE (fn) != TEMPLATE_DECL
5539 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
5540 && !DECL_CONSTRUCTOR_P (fn))
5542 DECL_DECLARED_CONSTEXPR_P (fn) = false;
5543 if (!DECL_GENERATED_P (fn))
5545 error ("enclosing class of constexpr non-static member "
5546 "function %q+#D is not a literal type", fn);
5547 explain_non_literal_class (t);
5552 /* T is a non-literal type used in a context which requires a constant
5553 expression. Explain why it isn't literal. */
5555 void
5556 explain_non_literal_class (tree t)
5558 static hash_set<tree> *diagnosed;
5560 if (!CLASS_TYPE_P (t))
5561 return;
5562 t = TYPE_MAIN_VARIANT (t);
5564 if (diagnosed == NULL)
5565 diagnosed = new hash_set<tree>;
5566 if (diagnosed->add (t))
5567 /* Already explained. */
5568 return;
5570 inform (0, "%q+T is not literal because:", t);
5571 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t))
5572 inform (0, " %q+T has a non-trivial destructor", t);
5573 else if (CLASSTYPE_NON_AGGREGATE (t)
5574 && !TYPE_HAS_TRIVIAL_DFLT (t)
5575 && !TYPE_HAS_CONSTEXPR_CTOR (t))
5577 inform (0, " %q+T is not an aggregate, does not have a trivial "
5578 "default constructor, and has no constexpr constructor that "
5579 "is not a copy or move constructor", t);
5580 if (type_has_non_user_provided_default_constructor (t))
5582 /* Note that we can't simply call locate_ctor because when the
5583 constructor is deleted it just returns NULL_TREE. */
5584 tree fns;
5585 for (fns = CLASSTYPE_CONSTRUCTORS (t); fns; fns = OVL_NEXT (fns))
5587 tree fn = OVL_CURRENT (fns);
5588 tree parms = TYPE_ARG_TYPES (TREE_TYPE (fn));
5590 parms = skip_artificial_parms_for (fn, parms);
5592 if (sufficient_parms_p (parms))
5594 if (DECL_DELETED_FN (fn))
5595 maybe_explain_implicit_delete (fn);
5596 else
5597 explain_invalid_constexpr_fn (fn);
5598 break;
5603 else
5605 tree binfo, base_binfo, field; int i;
5606 for (binfo = TYPE_BINFO (t), i = 0;
5607 BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
5609 tree basetype = TREE_TYPE (base_binfo);
5610 if (!CLASSTYPE_LITERAL_P (basetype))
5612 inform (0, " base class %qT of %q+T is non-literal",
5613 basetype, t);
5614 explain_non_literal_class (basetype);
5615 return;
5618 for (field = TYPE_FIELDS (t); field; field = TREE_CHAIN (field))
5620 tree ftype;
5621 if (TREE_CODE (field) != FIELD_DECL)
5622 continue;
5623 ftype = TREE_TYPE (field);
5624 if (!literal_type_p (ftype))
5626 inform (0, " non-static data member %q+D has "
5627 "non-literal type", field);
5628 if (CLASS_TYPE_P (ftype))
5629 explain_non_literal_class (ftype);
5631 if (CP_TYPE_VOLATILE_P (ftype))
5632 inform (0, " non-static data member %q+D has "
5633 "volatile type", field);
5638 /* Check the validity of the bases and members declared in T. Add any
5639 implicitly-generated functions (like copy-constructors and
5640 assignment operators). Compute various flag bits (like
5641 CLASSTYPE_NON_LAYOUT_POD_T) for T. This routine works purely at the C++
5642 level: i.e., independently of the ABI in use. */
5644 static void
5645 check_bases_and_members (tree t)
5647 /* Nonzero if the implicitly generated copy constructor should take
5648 a non-const reference argument. */
5649 int cant_have_const_ctor;
5650 /* Nonzero if the implicitly generated assignment operator
5651 should take a non-const reference argument. */
5652 int no_const_asn_ref;
5653 tree access_decls;
5654 bool saved_complex_asn_ref;
5655 bool saved_nontrivial_dtor;
5656 tree fn;
5658 /* By default, we use const reference arguments and generate default
5659 constructors. */
5660 cant_have_const_ctor = 0;
5661 no_const_asn_ref = 0;
5663 /* Check all the base-classes. */
5664 check_bases (t, &cant_have_const_ctor,
5665 &no_const_asn_ref);
5667 /* Deduce noexcept on destructors. This needs to happen after we've set
5668 triviality flags appropriately for our bases. */
5669 if (cxx_dialect >= cxx11)
5670 deduce_noexcept_on_destructors (t);
5672 /* Check all the method declarations. */
5673 check_methods (t);
5675 /* Save the initial values of these flags which only indicate whether
5676 or not the class has user-provided functions. As we analyze the
5677 bases and members we can set these flags for other reasons. */
5678 saved_complex_asn_ref = TYPE_HAS_COMPLEX_COPY_ASSIGN (t);
5679 saved_nontrivial_dtor = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t);
5681 /* Check all the data member declarations. We cannot call
5682 check_field_decls until we have called check_bases check_methods,
5683 as check_field_decls depends on TYPE_HAS_NONTRIVIAL_DESTRUCTOR
5684 being set appropriately. */
5685 check_field_decls (t, &access_decls,
5686 &cant_have_const_ctor,
5687 &no_const_asn_ref);
5689 /* A nearly-empty class has to be vptr-containing; a nearly empty
5690 class contains just a vptr. */
5691 if (!TYPE_CONTAINS_VPTR_P (t))
5692 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
5694 /* Do some bookkeeping that will guide the generation of implicitly
5695 declared member functions. */
5696 TYPE_HAS_COMPLEX_COPY_CTOR (t) |= TYPE_CONTAINS_VPTR_P (t);
5697 TYPE_HAS_COMPLEX_MOVE_CTOR (t) |= TYPE_CONTAINS_VPTR_P (t);
5698 /* We need to call a constructor for this class if it has a
5699 user-provided constructor, or if the default constructor is going
5700 to initialize the vptr. (This is not an if-and-only-if;
5701 TYPE_NEEDS_CONSTRUCTING is set elsewhere if bases or members
5702 themselves need constructing.) */
5703 TYPE_NEEDS_CONSTRUCTING (t)
5704 |= (type_has_user_provided_constructor (t) || TYPE_CONTAINS_VPTR_P (t));
5705 /* [dcl.init.aggr]
5707 An aggregate is an array or a class with no user-provided
5708 constructors ... and no virtual functions.
5710 Again, other conditions for being an aggregate are checked
5711 elsewhere. */
5712 CLASSTYPE_NON_AGGREGATE (t)
5713 |= (type_has_user_provided_constructor (t) || TYPE_POLYMORPHIC_P (t));
5714 /* This is the C++98/03 definition of POD; it changed in C++0x, but we
5715 retain the old definition internally for ABI reasons. */
5716 CLASSTYPE_NON_LAYOUT_POD_P (t)
5717 |= (CLASSTYPE_NON_AGGREGATE (t)
5718 || saved_nontrivial_dtor || saved_complex_asn_ref);
5719 CLASSTYPE_NON_STD_LAYOUT (t) |= TYPE_CONTAINS_VPTR_P (t);
5720 TYPE_HAS_COMPLEX_COPY_ASSIGN (t) |= TYPE_CONTAINS_VPTR_P (t);
5721 TYPE_HAS_COMPLEX_MOVE_ASSIGN (t) |= TYPE_CONTAINS_VPTR_P (t);
5722 TYPE_HAS_COMPLEX_DFLT (t) |= TYPE_CONTAINS_VPTR_P (t);
5724 /* If the only explicitly declared default constructor is user-provided,
5725 set TYPE_HAS_COMPLEX_DFLT. */
5726 if (!TYPE_HAS_COMPLEX_DFLT (t)
5727 && TYPE_HAS_DEFAULT_CONSTRUCTOR (t)
5728 && !type_has_non_user_provided_default_constructor (t))
5729 TYPE_HAS_COMPLEX_DFLT (t) = true;
5731 /* Warn if a public base of a polymorphic type has an accessible
5732 non-virtual destructor. It is only now that we know the class is
5733 polymorphic. Although a polymorphic base will have a already
5734 been diagnosed during its definition, we warn on use too. */
5735 if (TYPE_POLYMORPHIC_P (t) && warn_nonvdtor)
5737 tree binfo = TYPE_BINFO (t);
5738 vec<tree, va_gc> *accesses = BINFO_BASE_ACCESSES (binfo);
5739 tree base_binfo;
5740 unsigned i;
5742 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
5744 tree basetype = TREE_TYPE (base_binfo);
5746 if ((*accesses)[i] == access_public_node
5747 && (TYPE_POLYMORPHIC_P (basetype) || warn_ecpp)
5748 && accessible_nvdtor_p (basetype))
5749 warning (OPT_Wnon_virtual_dtor,
5750 "base class %q#T has accessible non-virtual destructor",
5751 basetype);
5755 /* If the class has no user-declared constructor, but does have
5756 non-static const or reference data members that can never be
5757 initialized, issue a warning. */
5758 if (warn_uninitialized
5759 /* Classes with user-declared constructors are presumed to
5760 initialize these members. */
5761 && !TYPE_HAS_USER_CONSTRUCTOR (t)
5762 /* Aggregates can be initialized with brace-enclosed
5763 initializers. */
5764 && CLASSTYPE_NON_AGGREGATE (t))
5766 tree field;
5768 for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
5770 tree type;
5772 if (TREE_CODE (field) != FIELD_DECL
5773 || DECL_INITIAL (field) != NULL_TREE)
5774 continue;
5776 type = TREE_TYPE (field);
5777 if (TREE_CODE (type) == REFERENCE_TYPE)
5778 warning (OPT_Wuninitialized, "non-static reference %q+#D "
5779 "in class without a constructor", field);
5780 else if (CP_TYPE_CONST_P (type)
5781 && (!CLASS_TYPE_P (type)
5782 || !TYPE_HAS_DEFAULT_CONSTRUCTOR (type)))
5783 warning (OPT_Wuninitialized, "non-static const member %q+#D "
5784 "in class without a constructor", field);
5788 /* Synthesize any needed methods. */
5789 add_implicitly_declared_members (t, &access_decls,
5790 cant_have_const_ctor,
5791 no_const_asn_ref);
5793 /* Check defaulted declarations here so we have cant_have_const_ctor
5794 and don't need to worry about clones. */
5795 for (fn = TYPE_METHODS (t); fn; fn = DECL_CHAIN (fn))
5796 if (!DECL_ARTIFICIAL (fn) && DECL_DEFAULTED_IN_CLASS_P (fn))
5798 int copy = copy_fn_p (fn);
5799 if (copy > 0)
5801 bool imp_const_p
5802 = (DECL_CONSTRUCTOR_P (fn) ? !cant_have_const_ctor
5803 : !no_const_asn_ref);
5804 bool fn_const_p = (copy == 2);
5806 if (fn_const_p && !imp_const_p)
5807 /* If the function is defaulted outside the class, we just
5808 give the synthesis error. */
5809 error ("%q+D declared to take const reference, but implicit "
5810 "declaration would take non-const", fn);
5812 defaulted_late_check (fn);
5815 if (LAMBDA_TYPE_P (t))
5817 /* "This class type is not an aggregate." */
5818 CLASSTYPE_NON_AGGREGATE (t) = 1;
5821 /* Compute the 'literal type' property before we
5822 do anything with non-static member functions. */
5823 finalize_literal_type_property (t);
5825 /* Create the in-charge and not-in-charge variants of constructors
5826 and destructors. */
5827 clone_constructors_and_destructors (t);
5829 /* Process the using-declarations. */
5830 for (; access_decls; access_decls = TREE_CHAIN (access_decls))
5831 handle_using_decl (TREE_VALUE (access_decls), t);
5833 /* Build and sort the CLASSTYPE_METHOD_VEC. */
5834 finish_struct_methods (t);
5836 /* Figure out whether or not we will need a cookie when dynamically
5837 allocating an array of this type. */
5838 TYPE_LANG_SPECIFIC (t)->u.c.vec_new_uses_cookie
5839 = type_requires_array_cookie (t);
5842 /* If T needs a pointer to its virtual function table, set TYPE_VFIELD
5843 accordingly. If a new vfield was created (because T doesn't have a
5844 primary base class), then the newly created field is returned. It
5845 is not added to the TYPE_FIELDS list; it is the caller's
5846 responsibility to do that. Accumulate declared virtual functions
5847 on VIRTUALS_P. */
5849 static tree
5850 create_vtable_ptr (tree t, tree* virtuals_p)
5852 tree fn;
5854 /* Collect the virtual functions declared in T. */
5855 for (fn = TYPE_METHODS (t); fn; fn = DECL_CHAIN (fn))
5856 if (TREE_CODE (fn) == FUNCTION_DECL
5857 && DECL_VINDEX (fn) && !DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (fn)
5858 && TREE_CODE (DECL_VINDEX (fn)) != INTEGER_CST)
5860 tree new_virtual = make_node (TREE_LIST);
5862 BV_FN (new_virtual) = fn;
5863 BV_DELTA (new_virtual) = integer_zero_node;
5864 BV_VCALL_INDEX (new_virtual) = NULL_TREE;
5866 TREE_CHAIN (new_virtual) = *virtuals_p;
5867 *virtuals_p = new_virtual;
5870 /* If we couldn't find an appropriate base class, create a new field
5871 here. Even if there weren't any new virtual functions, we might need a
5872 new virtual function table if we're supposed to include vptrs in
5873 all classes that need them. */
5874 if (!TYPE_VFIELD (t) && (*virtuals_p || TYPE_CONTAINS_VPTR_P (t)))
5876 /* We build this decl with vtbl_ptr_type_node, which is a
5877 `vtable_entry_type*'. It might seem more precise to use
5878 `vtable_entry_type (*)[N]' where N is the number of virtual
5879 functions. However, that would require the vtable pointer in
5880 base classes to have a different type than the vtable pointer
5881 in derived classes. We could make that happen, but that
5882 still wouldn't solve all the problems. In particular, the
5883 type-based alias analysis code would decide that assignments
5884 to the base class vtable pointer can't alias assignments to
5885 the derived class vtable pointer, since they have different
5886 types. Thus, in a derived class destructor, where the base
5887 class constructor was inlined, we could generate bad code for
5888 setting up the vtable pointer.
5890 Therefore, we use one type for all vtable pointers. We still
5891 use a type-correct type; it's just doesn't indicate the array
5892 bounds. That's better than using `void*' or some such; it's
5893 cleaner, and it let's the alias analysis code know that these
5894 stores cannot alias stores to void*! */
5895 tree field;
5897 field = build_decl (input_location,
5898 FIELD_DECL, get_vfield_name (t), vtbl_ptr_type_node);
5899 DECL_VIRTUAL_P (field) = 1;
5900 DECL_ARTIFICIAL (field) = 1;
5901 DECL_FIELD_CONTEXT (field) = t;
5902 DECL_FCONTEXT (field) = t;
5903 if (TYPE_PACKED (t))
5904 DECL_PACKED (field) = 1;
5906 TYPE_VFIELD (t) = field;
5908 /* This class is non-empty. */
5909 CLASSTYPE_EMPTY_P (t) = 0;
5911 return field;
5914 return NULL_TREE;
5917 /* Add OFFSET to all base types of BINFO which is a base in the
5918 hierarchy dominated by T.
5920 OFFSET, which is a type offset, is number of bytes. */
5922 static void
5923 propagate_binfo_offsets (tree binfo, tree offset)
5925 int i;
5926 tree primary_binfo;
5927 tree base_binfo;
5929 /* Update BINFO's offset. */
5930 BINFO_OFFSET (binfo)
5931 = convert (sizetype,
5932 size_binop (PLUS_EXPR,
5933 convert (ssizetype, BINFO_OFFSET (binfo)),
5934 offset));
5936 /* Find the primary base class. */
5937 primary_binfo = get_primary_binfo (binfo);
5939 if (primary_binfo && BINFO_INHERITANCE_CHAIN (primary_binfo) == binfo)
5940 propagate_binfo_offsets (primary_binfo, offset);
5942 /* Scan all of the bases, pushing the BINFO_OFFSET adjust
5943 downwards. */
5944 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
5946 /* Don't do the primary base twice. */
5947 if (base_binfo == primary_binfo)
5948 continue;
5950 if (BINFO_VIRTUAL_P (base_binfo))
5951 continue;
5953 propagate_binfo_offsets (base_binfo, offset);
5957 /* Set BINFO_OFFSET for all of the virtual bases for RLI->T. Update
5958 TYPE_ALIGN and TYPE_SIZE for T. OFFSETS gives the location of
5959 empty subobjects of T. */
5961 static void
5962 layout_virtual_bases (record_layout_info rli, splay_tree offsets)
5964 tree vbase;
5965 tree t = rli->t;
5966 tree *next_field;
5968 if (BINFO_N_BASE_BINFOS (TYPE_BINFO (t)) == 0)
5969 return;
5971 /* Find the last field. The artificial fields created for virtual
5972 bases will go after the last extant field to date. */
5973 next_field = &TYPE_FIELDS (t);
5974 while (*next_field)
5975 next_field = &DECL_CHAIN (*next_field);
5977 /* Go through the virtual bases, allocating space for each virtual
5978 base that is not already a primary base class. These are
5979 allocated in inheritance graph order. */
5980 for (vbase = TYPE_BINFO (t); vbase; vbase = TREE_CHAIN (vbase))
5982 if (!BINFO_VIRTUAL_P (vbase))
5983 continue;
5985 if (!BINFO_PRIMARY_P (vbase))
5987 /* This virtual base is not a primary base of any class in the
5988 hierarchy, so we have to add space for it. */
5989 next_field = build_base_field (rli, vbase,
5990 offsets, next_field);
5995 /* Returns the offset of the byte just past the end of the base class
5996 BINFO. */
5998 static tree
5999 end_of_base (tree binfo)
6001 tree size;
6003 if (!CLASSTYPE_AS_BASE (BINFO_TYPE (binfo)))
6004 size = TYPE_SIZE_UNIT (char_type_node);
6005 else if (is_empty_class (BINFO_TYPE (binfo)))
6006 /* An empty class has zero CLASSTYPE_SIZE_UNIT, but we need to
6007 allocate some space for it. It cannot have virtual bases, so
6008 TYPE_SIZE_UNIT is fine. */
6009 size = TYPE_SIZE_UNIT (BINFO_TYPE (binfo));
6010 else
6011 size = CLASSTYPE_SIZE_UNIT (BINFO_TYPE (binfo));
6013 return size_binop (PLUS_EXPR, BINFO_OFFSET (binfo), size);
6016 /* Returns the offset of the byte just past the end of the base class
6017 with the highest offset in T. If INCLUDE_VIRTUALS_P is zero, then
6018 only non-virtual bases are included. */
6020 static tree
6021 end_of_class (tree t, int include_virtuals_p)
6023 tree result = size_zero_node;
6024 vec<tree, va_gc> *vbases;
6025 tree binfo;
6026 tree base_binfo;
6027 tree offset;
6028 int i;
6030 for (binfo = TYPE_BINFO (t), i = 0;
6031 BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
6033 if (!include_virtuals_p
6034 && BINFO_VIRTUAL_P (base_binfo)
6035 && (!BINFO_PRIMARY_P (base_binfo)
6036 || BINFO_INHERITANCE_CHAIN (base_binfo) != TYPE_BINFO (t)))
6037 continue;
6039 offset = end_of_base (base_binfo);
6040 if (tree_int_cst_lt (result, offset))
6041 result = offset;
6044 if (include_virtuals_p)
6045 for (vbases = CLASSTYPE_VBASECLASSES (t), i = 0;
6046 vec_safe_iterate (vbases, i, &base_binfo); i++)
6048 offset = end_of_base (base_binfo);
6049 if (tree_int_cst_lt (result, offset))
6050 result = offset;
6053 return result;
6056 /* Warn about bases of T that are inaccessible because they are
6057 ambiguous. For example:
6059 struct S {};
6060 struct T : public S {};
6061 struct U : public S, public T {};
6063 Here, `(S*) new U' is not allowed because there are two `S'
6064 subobjects of U. */
6066 static void
6067 warn_about_ambiguous_bases (tree t)
6069 int i;
6070 vec<tree, va_gc> *vbases;
6071 tree basetype;
6072 tree binfo;
6073 tree base_binfo;
6075 /* If there are no repeated bases, nothing can be ambiguous. */
6076 if (!CLASSTYPE_REPEATED_BASE_P (t))
6077 return;
6079 /* Check direct bases. */
6080 for (binfo = TYPE_BINFO (t), i = 0;
6081 BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
6083 basetype = BINFO_TYPE (base_binfo);
6085 if (!uniquely_derived_from_p (basetype, t))
6086 warning (0, "direct base %qT inaccessible in %qT due to ambiguity",
6087 basetype, t);
6090 /* Check for ambiguous virtual bases. */
6091 if (extra_warnings)
6092 for (vbases = CLASSTYPE_VBASECLASSES (t), i = 0;
6093 vec_safe_iterate (vbases, i, &binfo); i++)
6095 basetype = BINFO_TYPE (binfo);
6097 if (!uniquely_derived_from_p (basetype, t))
6098 warning (OPT_Wextra, "virtual base %qT inaccessible in %qT due "
6099 "to ambiguity", basetype, t);
6103 /* Compare two INTEGER_CSTs K1 and K2. */
6105 static int
6106 splay_tree_compare_integer_csts (splay_tree_key k1, splay_tree_key k2)
6108 return tree_int_cst_compare ((tree) k1, (tree) k2);
6111 /* Increase the size indicated in RLI to account for empty classes
6112 that are "off the end" of the class. */
6114 static void
6115 include_empty_classes (record_layout_info rli)
6117 tree eoc;
6118 tree rli_size;
6120 /* It might be the case that we grew the class to allocate a
6121 zero-sized base class. That won't be reflected in RLI, yet,
6122 because we are willing to overlay multiple bases at the same
6123 offset. However, now we need to make sure that RLI is big enough
6124 to reflect the entire class. */
6125 eoc = end_of_class (rli->t,
6126 CLASSTYPE_AS_BASE (rli->t) != NULL_TREE);
6127 rli_size = rli_size_unit_so_far (rli);
6128 if (TREE_CODE (rli_size) == INTEGER_CST
6129 && tree_int_cst_lt (rli_size, eoc))
6131 /* The size should have been rounded to a whole byte. */
6132 gcc_assert (tree_int_cst_equal
6133 (rli->bitpos, round_down (rli->bitpos, BITS_PER_UNIT)));
6134 rli->bitpos
6135 = size_binop (PLUS_EXPR,
6136 rli->bitpos,
6137 size_binop (MULT_EXPR,
6138 convert (bitsizetype,
6139 size_binop (MINUS_EXPR,
6140 eoc, rli_size)),
6141 bitsize_int (BITS_PER_UNIT)));
6142 normalize_rli (rli);
6146 /* Calculate the TYPE_SIZE, TYPE_ALIGN, etc for T. Calculate
6147 BINFO_OFFSETs for all of the base-classes. Position the vtable
6148 pointer. Accumulate declared virtual functions on VIRTUALS_P. */
6150 static void
6151 layout_class_type (tree t, tree *virtuals_p)
6153 tree non_static_data_members;
6154 tree field;
6155 tree vptr;
6156 record_layout_info rli;
6157 /* Maps offsets (represented as INTEGER_CSTs) to a TREE_LIST of
6158 types that appear at that offset. */
6159 splay_tree empty_base_offsets;
6160 /* True if the last field laid out was a bit-field. */
6161 bool last_field_was_bitfield = false;
6162 /* The location at which the next field should be inserted. */
6163 tree *next_field;
6164 /* T, as a base class. */
6165 tree base_t;
6167 /* Keep track of the first non-static data member. */
6168 non_static_data_members = TYPE_FIELDS (t);
6170 /* Start laying out the record. */
6171 rli = start_record_layout (t);
6173 /* Mark all the primary bases in the hierarchy. */
6174 determine_primary_bases (t);
6176 /* Create a pointer to our virtual function table. */
6177 vptr = create_vtable_ptr (t, virtuals_p);
6179 /* The vptr is always the first thing in the class. */
6180 if (vptr)
6182 DECL_CHAIN (vptr) = TYPE_FIELDS (t);
6183 TYPE_FIELDS (t) = vptr;
6184 next_field = &DECL_CHAIN (vptr);
6185 place_field (rli, vptr);
6187 else
6188 next_field = &TYPE_FIELDS (t);
6190 /* Build FIELD_DECLs for all of the non-virtual base-types. */
6191 empty_base_offsets = splay_tree_new (splay_tree_compare_integer_csts,
6192 NULL, NULL);
6193 build_base_fields (rli, empty_base_offsets, next_field);
6195 /* Layout the non-static data members. */
6196 for (field = non_static_data_members; field; field = DECL_CHAIN (field))
6198 tree type;
6199 tree padding;
6201 /* We still pass things that aren't non-static data members to
6202 the back end, in case it wants to do something with them. */
6203 if (TREE_CODE (field) != FIELD_DECL)
6205 place_field (rli, field);
6206 /* If the static data member has incomplete type, keep track
6207 of it so that it can be completed later. (The handling
6208 of pending statics in finish_record_layout is
6209 insufficient; consider:
6211 struct S1;
6212 struct S2 { static S1 s1; };
6214 At this point, finish_record_layout will be called, but
6215 S1 is still incomplete.) */
6216 if (VAR_P (field))
6218 maybe_register_incomplete_var (field);
6219 /* The visibility of static data members is determined
6220 at their point of declaration, not their point of
6221 definition. */
6222 determine_visibility (field);
6224 continue;
6227 type = TREE_TYPE (field);
6228 if (type == error_mark_node)
6229 continue;
6231 padding = NULL_TREE;
6233 /* If this field is a bit-field whose width is greater than its
6234 type, then there are some special rules for allocating
6235 it. */
6236 if (DECL_C_BIT_FIELD (field)
6237 && tree_int_cst_lt (TYPE_SIZE (type), DECL_SIZE (field)))
6239 unsigned int itk;
6240 tree integer_type;
6241 bool was_unnamed_p = false;
6242 /* We must allocate the bits as if suitably aligned for the
6243 longest integer type that fits in this many bits. type
6244 of the field. Then, we are supposed to use the left over
6245 bits as additional padding. */
6246 for (itk = itk_char; itk != itk_none; ++itk)
6247 if (integer_types[itk] != NULL_TREE
6248 && (tree_int_cst_lt (size_int (MAX_FIXED_MODE_SIZE),
6249 TYPE_SIZE (integer_types[itk]))
6250 || tree_int_cst_lt (DECL_SIZE (field),
6251 TYPE_SIZE (integer_types[itk]))))
6252 break;
6254 /* ITK now indicates a type that is too large for the
6255 field. We have to back up by one to find the largest
6256 type that fits. */
6259 --itk;
6260 integer_type = integer_types[itk];
6261 } while (itk > 0 && integer_type == NULL_TREE);
6263 /* Figure out how much additional padding is required. */
6264 if (tree_int_cst_lt (TYPE_SIZE (integer_type), DECL_SIZE (field)))
6266 if (TREE_CODE (t) == UNION_TYPE)
6267 /* In a union, the padding field must have the full width
6268 of the bit-field; all fields start at offset zero. */
6269 padding = DECL_SIZE (field);
6270 else
6271 padding = size_binop (MINUS_EXPR, DECL_SIZE (field),
6272 TYPE_SIZE (integer_type));
6275 /* An unnamed bitfield does not normally affect the
6276 alignment of the containing class on a target where
6277 PCC_BITFIELD_TYPE_MATTERS. But, the C++ ABI does not
6278 make any exceptions for unnamed bitfields when the
6279 bitfields are longer than their types. Therefore, we
6280 temporarily give the field a name. */
6281 if (PCC_BITFIELD_TYPE_MATTERS && !DECL_NAME (field))
6283 was_unnamed_p = true;
6284 DECL_NAME (field) = make_anon_name ();
6287 DECL_SIZE (field) = TYPE_SIZE (integer_type);
6288 DECL_ALIGN (field) = TYPE_ALIGN (integer_type);
6289 DECL_USER_ALIGN (field) = TYPE_USER_ALIGN (integer_type);
6290 layout_nonempty_base_or_field (rli, field, NULL_TREE,
6291 empty_base_offsets);
6292 if (was_unnamed_p)
6293 DECL_NAME (field) = NULL_TREE;
6294 /* Now that layout has been performed, set the size of the
6295 field to the size of its declared type; the rest of the
6296 field is effectively invisible. */
6297 DECL_SIZE (field) = TYPE_SIZE (type);
6298 /* We must also reset the DECL_MODE of the field. */
6299 DECL_MODE (field) = TYPE_MODE (type);
6301 else
6302 layout_nonempty_base_or_field (rli, field, NULL_TREE,
6303 empty_base_offsets);
6305 /* Remember the location of any empty classes in FIELD. */
6306 record_subobject_offsets (TREE_TYPE (field),
6307 byte_position(field),
6308 empty_base_offsets,
6309 /*is_data_member=*/true);
6311 /* If a bit-field does not immediately follow another bit-field,
6312 and yet it starts in the middle of a byte, we have failed to
6313 comply with the ABI. */
6314 if (warn_abi
6315 && DECL_C_BIT_FIELD (field)
6316 /* The TREE_NO_WARNING flag gets set by Objective-C when
6317 laying out an Objective-C class. The ObjC ABI differs
6318 from the C++ ABI, and so we do not want a warning
6319 here. */
6320 && !TREE_NO_WARNING (field)
6321 && !last_field_was_bitfield
6322 && !integer_zerop (size_binop (TRUNC_MOD_EXPR,
6323 DECL_FIELD_BIT_OFFSET (field),
6324 bitsize_unit_node)))
6325 warning (OPT_Wabi, "offset of %q+D is not ABI-compliant and may "
6326 "change in a future version of GCC", field);
6328 /* The middle end uses the type of expressions to determine the
6329 possible range of expression values. In order to optimize
6330 "x.i > 7" to "false" for a 2-bit bitfield "i", the middle end
6331 must be made aware of the width of "i", via its type.
6333 Because C++ does not have integer types of arbitrary width,
6334 we must (for the purposes of the front end) convert from the
6335 type assigned here to the declared type of the bitfield
6336 whenever a bitfield expression is used as an rvalue.
6337 Similarly, when assigning a value to a bitfield, the value
6338 must be converted to the type given the bitfield here. */
6339 if (DECL_C_BIT_FIELD (field))
6341 unsigned HOST_WIDE_INT width;
6342 tree ftype = TREE_TYPE (field);
6343 width = tree_to_uhwi (DECL_SIZE (field));
6344 if (width != TYPE_PRECISION (ftype))
6346 TREE_TYPE (field)
6347 = c_build_bitfield_integer_type (width,
6348 TYPE_UNSIGNED (ftype));
6349 TREE_TYPE (field)
6350 = cp_build_qualified_type (TREE_TYPE (field),
6351 cp_type_quals (ftype));
6355 /* If we needed additional padding after this field, add it
6356 now. */
6357 if (padding)
6359 tree padding_field;
6361 padding_field = build_decl (input_location,
6362 FIELD_DECL,
6363 NULL_TREE,
6364 char_type_node);
6365 DECL_BIT_FIELD (padding_field) = 1;
6366 DECL_SIZE (padding_field) = padding;
6367 DECL_CONTEXT (padding_field) = t;
6368 DECL_ARTIFICIAL (padding_field) = 1;
6369 DECL_IGNORED_P (padding_field) = 1;
6370 layout_nonempty_base_or_field (rli, padding_field,
6371 NULL_TREE,
6372 empty_base_offsets);
6375 last_field_was_bitfield = DECL_C_BIT_FIELD (field);
6378 if (!integer_zerop (rli->bitpos))
6380 /* Make sure that we are on a byte boundary so that the size of
6381 the class without virtual bases will always be a round number
6382 of bytes. */
6383 rli->bitpos = round_up_loc (input_location, rli->bitpos, BITS_PER_UNIT);
6384 normalize_rli (rli);
6387 /* Delete all zero-width bit-fields from the list of fields. Now
6388 that the type is laid out they are no longer important. */
6389 remove_zero_width_bit_fields (t);
6391 /* Create the version of T used for virtual bases. We do not use
6392 make_class_type for this version; this is an artificial type. For
6393 a POD type, we just reuse T. */
6394 if (CLASSTYPE_NON_LAYOUT_POD_P (t) || CLASSTYPE_EMPTY_P (t))
6396 base_t = make_node (TREE_CODE (t));
6398 /* Set the size and alignment for the new type. */
6399 tree eoc;
6401 /* If the ABI version is not at least two, and the last
6402 field was a bit-field, RLI may not be on a byte
6403 boundary. In particular, rli_size_unit_so_far might
6404 indicate the last complete byte, while rli_size_so_far
6405 indicates the total number of bits used. Therefore,
6406 rli_size_so_far, rather than rli_size_unit_so_far, is
6407 used to compute TYPE_SIZE_UNIT. */
6408 eoc = end_of_class (t, /*include_virtuals_p=*/0);
6409 TYPE_SIZE_UNIT (base_t)
6410 = size_binop (MAX_EXPR,
6411 convert (sizetype,
6412 size_binop (CEIL_DIV_EXPR,
6413 rli_size_so_far (rli),
6414 bitsize_int (BITS_PER_UNIT))),
6415 eoc);
6416 TYPE_SIZE (base_t)
6417 = size_binop (MAX_EXPR,
6418 rli_size_so_far (rli),
6419 size_binop (MULT_EXPR,
6420 convert (bitsizetype, eoc),
6421 bitsize_int (BITS_PER_UNIT)));
6422 TYPE_ALIGN (base_t) = rli->record_align;
6423 TYPE_USER_ALIGN (base_t) = TYPE_USER_ALIGN (t);
6425 /* Copy the fields from T. */
6426 next_field = &TYPE_FIELDS (base_t);
6427 for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
6428 if (TREE_CODE (field) == FIELD_DECL)
6430 *next_field = build_decl (input_location,
6431 FIELD_DECL,
6432 DECL_NAME (field),
6433 TREE_TYPE (field));
6434 DECL_CONTEXT (*next_field) = base_t;
6435 DECL_FIELD_OFFSET (*next_field) = DECL_FIELD_OFFSET (field);
6436 DECL_FIELD_BIT_OFFSET (*next_field)
6437 = DECL_FIELD_BIT_OFFSET (field);
6438 DECL_SIZE (*next_field) = DECL_SIZE (field);
6439 DECL_MODE (*next_field) = DECL_MODE (field);
6440 next_field = &DECL_CHAIN (*next_field);
6443 /* Record the base version of the type. */
6444 CLASSTYPE_AS_BASE (t) = base_t;
6445 TYPE_CONTEXT (base_t) = t;
6447 else
6448 CLASSTYPE_AS_BASE (t) = t;
6450 /* Every empty class contains an empty class. */
6451 if (CLASSTYPE_EMPTY_P (t))
6452 CLASSTYPE_CONTAINS_EMPTY_CLASS_P (t) = 1;
6454 /* Set the TYPE_DECL for this type to contain the right
6455 value for DECL_OFFSET, so that we can use it as part
6456 of a COMPONENT_REF for multiple inheritance. */
6457 layout_decl (TYPE_MAIN_DECL (t), 0);
6459 /* Now fix up any virtual base class types that we left lying
6460 around. We must get these done before we try to lay out the
6461 virtual function table. As a side-effect, this will remove the
6462 base subobject fields. */
6463 layout_virtual_bases (rli, empty_base_offsets);
6465 /* Make sure that empty classes are reflected in RLI at this
6466 point. */
6467 include_empty_classes(rli);
6469 /* Make sure not to create any structures with zero size. */
6470 if (integer_zerop (rli_size_unit_so_far (rli)) && CLASSTYPE_EMPTY_P (t))
6471 place_field (rli,
6472 build_decl (input_location,
6473 FIELD_DECL, NULL_TREE, char_type_node));
6475 /* If this is a non-POD, declaring it packed makes a difference to how it
6476 can be used as a field; don't let finalize_record_size undo it. */
6477 if (TYPE_PACKED (t) && !layout_pod_type_p (t))
6478 rli->packed_maybe_necessary = true;
6480 /* Let the back end lay out the type. */
6481 finish_record_layout (rli, /*free_p=*/true);
6483 if (TYPE_SIZE_UNIT (t)
6484 && TREE_CODE (TYPE_SIZE_UNIT (t)) == INTEGER_CST
6485 && !TREE_OVERFLOW (TYPE_SIZE_UNIT (t))
6486 && !valid_constant_size_p (TYPE_SIZE_UNIT (t)))
6487 error ("type %qT is too large", t);
6489 /* Warn about bases that can't be talked about due to ambiguity. */
6490 warn_about_ambiguous_bases (t);
6492 /* Now that we're done with layout, give the base fields the real types. */
6493 for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
6494 if (DECL_ARTIFICIAL (field) && IS_FAKE_BASE_TYPE (TREE_TYPE (field)))
6495 TREE_TYPE (field) = TYPE_CONTEXT (TREE_TYPE (field));
6497 /* Clean up. */
6498 splay_tree_delete (empty_base_offsets);
6500 if (CLASSTYPE_EMPTY_P (t)
6501 && tree_int_cst_lt (sizeof_biggest_empty_class,
6502 TYPE_SIZE_UNIT (t)))
6503 sizeof_biggest_empty_class = TYPE_SIZE_UNIT (t);
6506 /* Determine the "key method" for the class type indicated by TYPE,
6507 and set CLASSTYPE_KEY_METHOD accordingly. */
6509 void
6510 determine_key_method (tree type)
6512 tree method;
6514 if (TYPE_FOR_JAVA (type)
6515 || processing_template_decl
6516 || CLASSTYPE_TEMPLATE_INSTANTIATION (type)
6517 || CLASSTYPE_INTERFACE_KNOWN (type))
6518 return;
6520 /* The key method is the first non-pure virtual function that is not
6521 inline at the point of class definition. On some targets the
6522 key function may not be inline; those targets should not call
6523 this function until the end of the translation unit. */
6524 for (method = TYPE_METHODS (type); method != NULL_TREE;
6525 method = DECL_CHAIN (method))
6526 if (TREE_CODE (method) == FUNCTION_DECL
6527 && DECL_VINDEX (method) != NULL_TREE
6528 && ! DECL_DECLARED_INLINE_P (method)
6529 && ! DECL_PURE_VIRTUAL_P (method))
6531 CLASSTYPE_KEY_METHOD (type) = method;
6532 break;
6535 return;
6539 /* Allocate and return an instance of struct sorted_fields_type with
6540 N fields. */
6542 static struct sorted_fields_type *
6543 sorted_fields_type_new (int n)
6545 struct sorted_fields_type *sft;
6546 sft = (sorted_fields_type *) ggc_internal_alloc (sizeof (sorted_fields_type)
6547 + n * sizeof (tree));
6548 sft->len = n;
6550 return sft;
6554 /* Perform processing required when the definition of T (a class type)
6555 is complete. */
6557 void
6558 finish_struct_1 (tree t)
6560 tree x;
6561 /* A TREE_LIST. The TREE_VALUE of each node is a FUNCTION_DECL. */
6562 tree virtuals = NULL_TREE;
6564 if (COMPLETE_TYPE_P (t))
6566 gcc_assert (MAYBE_CLASS_TYPE_P (t));
6567 error ("redefinition of %q#T", t);
6568 popclass ();
6569 return;
6572 /* If this type was previously laid out as a forward reference,
6573 make sure we lay it out again. */
6574 TYPE_SIZE (t) = NULL_TREE;
6575 CLASSTYPE_PRIMARY_BINFO (t) = NULL_TREE;
6577 /* Make assumptions about the class; we'll reset the flags if
6578 necessary. */
6579 CLASSTYPE_EMPTY_P (t) = 1;
6580 CLASSTYPE_NEARLY_EMPTY_P (t) = 1;
6581 CLASSTYPE_CONTAINS_EMPTY_CLASS_P (t) = 0;
6582 CLASSTYPE_LITERAL_P (t) = true;
6584 /* Do end-of-class semantic processing: checking the validity of the
6585 bases and members and add implicitly generated methods. */
6586 check_bases_and_members (t);
6588 /* Find the key method. */
6589 if (TYPE_CONTAINS_VPTR_P (t))
6591 /* The Itanium C++ ABI permits the key method to be chosen when
6592 the class is defined -- even though the key method so
6593 selected may later turn out to be an inline function. On
6594 some systems (such as ARM Symbian OS) the key method cannot
6595 be determined until the end of the translation unit. On such
6596 systems, we leave CLASSTYPE_KEY_METHOD set to NULL, which
6597 will cause the class to be added to KEYED_CLASSES. Then, in
6598 finish_file we will determine the key method. */
6599 if (targetm.cxx.key_method_may_be_inline ())
6600 determine_key_method (t);
6602 /* If a polymorphic class has no key method, we may emit the vtable
6603 in every translation unit where the class definition appears. If
6604 we're devirtualizing, we can look into the vtable even if we
6605 aren't emitting it. */
6606 if (CLASSTYPE_KEY_METHOD (t) == NULL_TREE)
6607 keyed_classes = tree_cons (NULL_TREE, t, keyed_classes);
6610 /* Layout the class itself. */
6611 layout_class_type (t, &virtuals);
6612 if (CLASSTYPE_AS_BASE (t) != t)
6613 /* We use the base type for trivial assignments, and hence it
6614 needs a mode. */
6615 compute_record_mode (CLASSTYPE_AS_BASE (t));
6617 virtuals = modify_all_vtables (t, nreverse (virtuals));
6619 /* If necessary, create the primary vtable for this class. */
6620 if (virtuals || TYPE_CONTAINS_VPTR_P (t))
6622 /* We must enter these virtuals into the table. */
6623 if (!CLASSTYPE_HAS_PRIMARY_BASE_P (t))
6624 build_primary_vtable (NULL_TREE, t);
6625 else if (! BINFO_NEW_VTABLE_MARKED (TYPE_BINFO (t)))
6626 /* Here we know enough to change the type of our virtual
6627 function table, but we will wait until later this function. */
6628 build_primary_vtable (CLASSTYPE_PRIMARY_BINFO (t), t);
6630 /* If we're warning about ABI tags, check the types of the new
6631 virtual functions. */
6632 if (warn_abi_tag)
6633 for (tree v = virtuals; v; v = TREE_CHAIN (v))
6634 check_abi_tags (t, TREE_VALUE (v));
6637 if (TYPE_CONTAINS_VPTR_P (t))
6639 int vindex;
6640 tree fn;
6642 if (BINFO_VTABLE (TYPE_BINFO (t)))
6643 gcc_assert (DECL_VIRTUAL_P (BINFO_VTABLE (TYPE_BINFO (t))));
6644 if (!CLASSTYPE_HAS_PRIMARY_BASE_P (t))
6645 gcc_assert (BINFO_VIRTUALS (TYPE_BINFO (t)) == NULL_TREE);
6647 /* Add entries for virtual functions introduced by this class. */
6648 BINFO_VIRTUALS (TYPE_BINFO (t))
6649 = chainon (BINFO_VIRTUALS (TYPE_BINFO (t)), virtuals);
6651 /* Set DECL_VINDEX for all functions declared in this class. */
6652 for (vindex = 0, fn = BINFO_VIRTUALS (TYPE_BINFO (t));
6654 fn = TREE_CHAIN (fn),
6655 vindex += (TARGET_VTABLE_USES_DESCRIPTORS
6656 ? TARGET_VTABLE_USES_DESCRIPTORS : 1))
6658 tree fndecl = BV_FN (fn);
6660 if (DECL_THUNK_P (fndecl))
6661 /* A thunk. We should never be calling this entry directly
6662 from this vtable -- we'd use the entry for the non
6663 thunk base function. */
6664 DECL_VINDEX (fndecl) = NULL_TREE;
6665 else if (TREE_CODE (DECL_VINDEX (fndecl)) != INTEGER_CST)
6666 DECL_VINDEX (fndecl) = build_int_cst (NULL_TREE, vindex);
6670 finish_struct_bits (t);
6671 set_method_tm_attributes (t);
6673 /* Complete the rtl for any static member objects of the type we're
6674 working on. */
6675 for (x = TYPE_FIELDS (t); x; x = DECL_CHAIN (x))
6676 if (VAR_P (x) && TREE_STATIC (x)
6677 && TREE_TYPE (x) != error_mark_node
6678 && same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (x)), t))
6679 DECL_MODE (x) = TYPE_MODE (t);
6681 /* Done with FIELDS...now decide whether to sort these for
6682 faster lookups later.
6684 We use a small number because most searches fail (succeeding
6685 ultimately as the search bores through the inheritance
6686 hierarchy), and we want this failure to occur quickly. */
6688 insert_into_classtype_sorted_fields (TYPE_FIELDS (t), t, 8);
6690 /* Complain if one of the field types requires lower visibility. */
6691 constrain_class_visibility (t);
6693 /* Make the rtl for any new vtables we have created, and unmark
6694 the base types we marked. */
6695 finish_vtbls (t);
6697 /* Build the VTT for T. */
6698 build_vtt (t);
6700 /* This warning does not make sense for Java classes, since they
6701 cannot have destructors. */
6702 if (!TYPE_FOR_JAVA (t) && warn_nonvdtor
6703 && TYPE_POLYMORPHIC_P (t) && accessible_nvdtor_p (t)
6704 && !CLASSTYPE_FINAL (t))
6705 warning (OPT_Wnon_virtual_dtor,
6706 "%q#T has virtual functions and accessible"
6707 " non-virtual destructor", t);
6709 complete_vars (t);
6711 if (warn_overloaded_virtual)
6712 warn_hidden (t);
6714 /* Class layout, assignment of virtual table slots, etc., is now
6715 complete. Give the back end a chance to tweak the visibility of
6716 the class or perform any other required target modifications. */
6717 targetm.cxx.adjust_class_at_definition (t);
6719 maybe_suppress_debug_info (t);
6721 if (flag_vtable_verify)
6722 vtv_save_class_info (t);
6724 dump_class_hierarchy (t);
6726 /* Finish debugging output for this type. */
6727 rest_of_type_compilation (t, ! LOCAL_CLASS_P (t));
6729 if (TYPE_TRANSPARENT_AGGR (t))
6731 tree field = first_field (t);
6732 if (field == NULL_TREE || error_operand_p (field))
6734 error ("type transparent %q#T does not have any fields", t);
6735 TYPE_TRANSPARENT_AGGR (t) = 0;
6737 else if (DECL_ARTIFICIAL (field))
6739 if (DECL_FIELD_IS_BASE (field))
6740 error ("type transparent class %qT has base classes", t);
6741 else
6743 gcc_checking_assert (DECL_VIRTUAL_P (field));
6744 error ("type transparent class %qT has virtual functions", t);
6746 TYPE_TRANSPARENT_AGGR (t) = 0;
6748 else if (TYPE_MODE (t) != DECL_MODE (field))
6750 error ("type transparent %q#T cannot be made transparent because "
6751 "the type of the first field has a different ABI from the "
6752 "class overall", t);
6753 TYPE_TRANSPARENT_AGGR (t) = 0;
6758 /* Insert FIELDS into T for the sorted case if the FIELDS count is
6759 equal to THRESHOLD or greater than THRESHOLD. */
6761 static void
6762 insert_into_classtype_sorted_fields (tree fields, tree t, int threshold)
6764 int n_fields = count_fields (fields);
6765 if (n_fields >= threshold)
6767 struct sorted_fields_type *field_vec = sorted_fields_type_new (n_fields);
6768 add_fields_to_record_type (fields, field_vec, 0);
6769 qsort (field_vec->elts, n_fields, sizeof (tree), field_decl_cmp);
6770 CLASSTYPE_SORTED_FIELDS (t) = field_vec;
6774 /* Insert lately defined enum ENUMTYPE into T for the sorted case. */
6776 void
6777 insert_late_enum_def_into_classtype_sorted_fields (tree enumtype, tree t)
6779 struct sorted_fields_type *sorted_fields = CLASSTYPE_SORTED_FIELDS (t);
6780 if (sorted_fields)
6782 int i;
6783 int n_fields
6784 = list_length (TYPE_VALUES (enumtype)) + sorted_fields->len;
6785 struct sorted_fields_type *field_vec = sorted_fields_type_new (n_fields);
6787 for (i = 0; i < sorted_fields->len; ++i)
6788 field_vec->elts[i] = sorted_fields->elts[i];
6790 add_enum_fields_to_record_type (enumtype, field_vec,
6791 sorted_fields->len);
6792 qsort (field_vec->elts, n_fields, sizeof (tree), field_decl_cmp);
6793 CLASSTYPE_SORTED_FIELDS (t) = field_vec;
6797 /* When T was built up, the member declarations were added in reverse
6798 order. Rearrange them to declaration order. */
6800 void
6801 unreverse_member_declarations (tree t)
6803 tree next;
6804 tree prev;
6805 tree x;
6807 /* The following lists are all in reverse order. Put them in
6808 declaration order now. */
6809 TYPE_METHODS (t) = nreverse (TYPE_METHODS (t));
6810 CLASSTYPE_DECL_LIST (t) = nreverse (CLASSTYPE_DECL_LIST (t));
6812 /* Actually, for the TYPE_FIELDS, only the non TYPE_DECLs are in
6813 reverse order, so we can't just use nreverse. */
6814 prev = NULL_TREE;
6815 for (x = TYPE_FIELDS (t);
6816 x && TREE_CODE (x) != TYPE_DECL;
6817 x = next)
6819 next = DECL_CHAIN (x);
6820 DECL_CHAIN (x) = prev;
6821 prev = x;
6823 if (prev)
6825 DECL_CHAIN (TYPE_FIELDS (t)) = x;
6826 if (prev)
6827 TYPE_FIELDS (t) = prev;
6831 tree
6832 finish_struct (tree t, tree attributes)
6834 location_t saved_loc = input_location;
6836 /* Now that we've got all the field declarations, reverse everything
6837 as necessary. */
6838 unreverse_member_declarations (t);
6840 cplus_decl_attributes (&t, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
6841 fixup_attribute_variants (t);
6843 /* Nadger the current location so that diagnostics point to the start of
6844 the struct, not the end. */
6845 input_location = DECL_SOURCE_LOCATION (TYPE_NAME (t));
6847 if (processing_template_decl)
6849 tree x;
6851 finish_struct_methods (t);
6852 TYPE_SIZE (t) = bitsize_zero_node;
6853 TYPE_SIZE_UNIT (t) = size_zero_node;
6855 /* We need to emit an error message if this type was used as a parameter
6856 and it is an abstract type, even if it is a template. We construct
6857 a simple CLASSTYPE_PURE_VIRTUALS list without taking bases into
6858 account and we call complete_vars with this type, which will check
6859 the PARM_DECLS. Note that while the type is being defined,
6860 CLASSTYPE_PURE_VIRTUALS contains the list of the inline friends
6861 (see CLASSTYPE_INLINE_FRIENDS) so we need to clear it. */
6862 CLASSTYPE_PURE_VIRTUALS (t) = NULL;
6863 for (x = TYPE_METHODS (t); x; x = DECL_CHAIN (x))
6864 if (DECL_PURE_VIRTUAL_P (x))
6865 vec_safe_push (CLASSTYPE_PURE_VIRTUALS (t), x);
6866 complete_vars (t);
6867 /* We need to add the target functions to the CLASSTYPE_METHOD_VEC if
6868 an enclosing scope is a template class, so that this function be
6869 found by lookup_fnfields_1 when the using declaration is not
6870 instantiated yet. */
6871 for (x = TYPE_FIELDS (t); x; x = DECL_CHAIN (x))
6872 if (TREE_CODE (x) == USING_DECL)
6874 tree fn = strip_using_decl (x);
6875 if (is_overloaded_fn (fn))
6876 for (; fn; fn = OVL_NEXT (fn))
6877 add_method (t, OVL_CURRENT (fn), x);
6880 /* Remember current #pragma pack value. */
6881 TYPE_PRECISION (t) = maximum_field_alignment;
6883 /* Fix up any variants we've already built. */
6884 for (x = TYPE_NEXT_VARIANT (t); x; x = TYPE_NEXT_VARIANT (x))
6886 TYPE_SIZE (x) = TYPE_SIZE (t);
6887 TYPE_SIZE_UNIT (x) = TYPE_SIZE_UNIT (t);
6888 TYPE_FIELDS (x) = TYPE_FIELDS (t);
6889 TYPE_METHODS (x) = TYPE_METHODS (t);
6892 else
6893 finish_struct_1 (t);
6895 if (is_std_init_list (t))
6897 /* People keep complaining that the compiler crashes on an invalid
6898 definition of initializer_list, so I guess we should explicitly
6899 reject it. What the compiler internals care about is that it's a
6900 template and has a pointer field followed by an integer field. */
6901 bool ok = false;
6902 if (processing_template_decl)
6904 tree f = next_initializable_field (TYPE_FIELDS (t));
6905 if (f && TREE_CODE (TREE_TYPE (f)) == POINTER_TYPE)
6907 f = next_initializable_field (DECL_CHAIN (f));
6908 if (f && same_type_p (TREE_TYPE (f), size_type_node))
6909 ok = true;
6912 if (!ok)
6913 fatal_error (input_location,
6914 "definition of std::initializer_list does not match "
6915 "#include <initializer_list>");
6918 input_location = saved_loc;
6920 TYPE_BEING_DEFINED (t) = 0;
6922 if (current_class_type)
6923 popclass ();
6924 else
6925 error ("trying to finish struct, but kicked out due to previous parse errors");
6927 if (processing_template_decl && at_function_scope_p ()
6928 /* Lambdas are defined by the LAMBDA_EXPR. */
6929 && !LAMBDA_TYPE_P (t))
6930 add_stmt (build_min (TAG_DEFN, t));
6932 return t;
6935 /* Hash table to avoid endless recursion when handling references. */
6936 static hash_table<nofree_ptr_hash<tree_node> > *fixed_type_or_null_ref_ht;
6938 /* Return the dynamic type of INSTANCE, if known.
6939 Used to determine whether the virtual function table is needed
6940 or not.
6942 *NONNULL is set iff INSTANCE can be known to be nonnull, regardless
6943 of our knowledge of its type. *NONNULL should be initialized
6944 before this function is called. */
6946 static tree
6947 fixed_type_or_null (tree instance, int *nonnull, int *cdtorp)
6949 #define RECUR(T) fixed_type_or_null((T), nonnull, cdtorp)
6951 switch (TREE_CODE (instance))
6953 case INDIRECT_REF:
6954 if (POINTER_TYPE_P (TREE_TYPE (instance)))
6955 return NULL_TREE;
6956 else
6957 return RECUR (TREE_OPERAND (instance, 0));
6959 case CALL_EXPR:
6960 /* This is a call to a constructor, hence it's never zero. */
6961 if (TREE_HAS_CONSTRUCTOR (instance))
6963 if (nonnull)
6964 *nonnull = 1;
6965 return TREE_TYPE (instance);
6967 return NULL_TREE;
6969 case SAVE_EXPR:
6970 /* This is a call to a constructor, hence it's never zero. */
6971 if (TREE_HAS_CONSTRUCTOR (instance))
6973 if (nonnull)
6974 *nonnull = 1;
6975 return TREE_TYPE (instance);
6977 return RECUR (TREE_OPERAND (instance, 0));
6979 case POINTER_PLUS_EXPR:
6980 case PLUS_EXPR:
6981 case MINUS_EXPR:
6982 if (TREE_CODE (TREE_OPERAND (instance, 0)) == ADDR_EXPR)
6983 return RECUR (TREE_OPERAND (instance, 0));
6984 if (TREE_CODE (TREE_OPERAND (instance, 1)) == INTEGER_CST)
6985 /* Propagate nonnull. */
6986 return RECUR (TREE_OPERAND (instance, 0));
6988 return NULL_TREE;
6990 CASE_CONVERT:
6991 return RECUR (TREE_OPERAND (instance, 0));
6993 case ADDR_EXPR:
6994 instance = TREE_OPERAND (instance, 0);
6995 if (nonnull)
6997 /* Just because we see an ADDR_EXPR doesn't mean we're dealing
6998 with a real object -- given &p->f, p can still be null. */
6999 tree t = get_base_address (instance);
7000 /* ??? Probably should check DECL_WEAK here. */
7001 if (t && DECL_P (t))
7002 *nonnull = 1;
7004 return RECUR (instance);
7006 case COMPONENT_REF:
7007 /* If this component is really a base class reference, then the field
7008 itself isn't definitive. */
7009 if (DECL_FIELD_IS_BASE (TREE_OPERAND (instance, 1)))
7010 return RECUR (TREE_OPERAND (instance, 0));
7011 return RECUR (TREE_OPERAND (instance, 1));
7013 case VAR_DECL:
7014 case FIELD_DECL:
7015 if (TREE_CODE (TREE_TYPE (instance)) == ARRAY_TYPE
7016 && MAYBE_CLASS_TYPE_P (TREE_TYPE (TREE_TYPE (instance))))
7018 if (nonnull)
7019 *nonnull = 1;
7020 return TREE_TYPE (TREE_TYPE (instance));
7022 /* fall through... */
7023 case TARGET_EXPR:
7024 case PARM_DECL:
7025 case RESULT_DECL:
7026 if (MAYBE_CLASS_TYPE_P (TREE_TYPE (instance)))
7028 if (nonnull)
7029 *nonnull = 1;
7030 return TREE_TYPE (instance);
7032 else if (instance == current_class_ptr)
7034 if (nonnull)
7035 *nonnull = 1;
7037 /* if we're in a ctor or dtor, we know our type. If
7038 current_class_ptr is set but we aren't in a function, we're in
7039 an NSDMI (and therefore a constructor). */
7040 if (current_scope () != current_function_decl
7041 || (DECL_LANG_SPECIFIC (current_function_decl)
7042 && (DECL_CONSTRUCTOR_P (current_function_decl)
7043 || DECL_DESTRUCTOR_P (current_function_decl))))
7045 if (cdtorp)
7046 *cdtorp = 1;
7047 return TREE_TYPE (TREE_TYPE (instance));
7050 else if (TREE_CODE (TREE_TYPE (instance)) == REFERENCE_TYPE)
7052 /* We only need one hash table because it is always left empty. */
7053 if (!fixed_type_or_null_ref_ht)
7054 fixed_type_or_null_ref_ht
7055 = new hash_table<nofree_ptr_hash<tree_node> > (37);
7057 /* Reference variables should be references to objects. */
7058 if (nonnull)
7059 *nonnull = 1;
7061 /* Enter the INSTANCE in a table to prevent recursion; a
7062 variable's initializer may refer to the variable
7063 itself. */
7064 if (VAR_P (instance)
7065 && DECL_INITIAL (instance)
7066 && !type_dependent_expression_p_push (DECL_INITIAL (instance))
7067 && !fixed_type_or_null_ref_ht->find (instance))
7069 tree type;
7070 tree_node **slot;
7072 slot = fixed_type_or_null_ref_ht->find_slot (instance, INSERT);
7073 *slot = instance;
7074 type = RECUR (DECL_INITIAL (instance));
7075 fixed_type_or_null_ref_ht->remove_elt (instance);
7077 return type;
7080 return NULL_TREE;
7082 default:
7083 return NULL_TREE;
7085 #undef RECUR
7088 /* Return nonzero if the dynamic type of INSTANCE is known, and
7089 equivalent to the static type. We also handle the case where
7090 INSTANCE is really a pointer. Return negative if this is a
7091 ctor/dtor. There the dynamic type is known, but this might not be
7092 the most derived base of the original object, and hence virtual
7093 bases may not be laid out according to this type.
7095 Used to determine whether the virtual function table is needed
7096 or not.
7098 *NONNULL is set iff INSTANCE can be known to be nonnull, regardless
7099 of our knowledge of its type. *NONNULL should be initialized
7100 before this function is called. */
7103 resolves_to_fixed_type_p (tree instance, int* nonnull)
7105 tree t = TREE_TYPE (instance);
7106 int cdtorp = 0;
7107 tree fixed;
7109 /* processing_template_decl can be false in a template if we're in
7110 instantiate_non_dependent_expr, but we still want to suppress
7111 this check. */
7112 if (in_template_function ())
7114 /* In a template we only care about the type of the result. */
7115 if (nonnull)
7116 *nonnull = true;
7117 return true;
7120 fixed = fixed_type_or_null (instance, nonnull, &cdtorp);
7121 if (fixed == NULL_TREE)
7122 return 0;
7123 if (POINTER_TYPE_P (t))
7124 t = TREE_TYPE (t);
7125 if (!same_type_ignoring_top_level_qualifiers_p (t, fixed))
7126 return 0;
7127 return cdtorp ? -1 : 1;
7131 void
7132 init_class_processing (void)
7134 current_class_depth = 0;
7135 current_class_stack_size = 10;
7136 current_class_stack
7137 = XNEWVEC (struct class_stack_node, current_class_stack_size);
7138 vec_alloc (local_classes, 8);
7139 sizeof_biggest_empty_class = size_zero_node;
7141 ridpointers[(int) RID_PUBLIC] = access_public_node;
7142 ridpointers[(int) RID_PRIVATE] = access_private_node;
7143 ridpointers[(int) RID_PROTECTED] = access_protected_node;
7146 /* Restore the cached PREVIOUS_CLASS_LEVEL. */
7148 static void
7149 restore_class_cache (void)
7151 tree type;
7153 /* We are re-entering the same class we just left, so we don't
7154 have to search the whole inheritance matrix to find all the
7155 decls to bind again. Instead, we install the cached
7156 class_shadowed list and walk through it binding names. */
7157 push_binding_level (previous_class_level);
7158 class_binding_level = previous_class_level;
7159 /* Restore IDENTIFIER_TYPE_VALUE. */
7160 for (type = class_binding_level->type_shadowed;
7161 type;
7162 type = TREE_CHAIN (type))
7163 SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (type), TREE_TYPE (type));
7166 /* Set global variables CURRENT_CLASS_NAME and CURRENT_CLASS_TYPE as
7167 appropriate for TYPE.
7169 So that we may avoid calls to lookup_name, we cache the _TYPE
7170 nodes of local TYPE_DECLs in the TREE_TYPE field of the name.
7172 For multiple inheritance, we perform a two-pass depth-first search
7173 of the type lattice. */
7175 void
7176 pushclass (tree type)
7178 class_stack_node_t csn;
7180 type = TYPE_MAIN_VARIANT (type);
7182 /* Make sure there is enough room for the new entry on the stack. */
7183 if (current_class_depth + 1 >= current_class_stack_size)
7185 current_class_stack_size *= 2;
7186 current_class_stack
7187 = XRESIZEVEC (struct class_stack_node, current_class_stack,
7188 current_class_stack_size);
7191 /* Insert a new entry on the class stack. */
7192 csn = current_class_stack + current_class_depth;
7193 csn->name = current_class_name;
7194 csn->type = current_class_type;
7195 csn->access = current_access_specifier;
7196 csn->names_used = 0;
7197 csn->hidden = 0;
7198 current_class_depth++;
7200 /* Now set up the new type. */
7201 current_class_name = TYPE_NAME (type);
7202 if (TREE_CODE (current_class_name) == TYPE_DECL)
7203 current_class_name = DECL_NAME (current_class_name);
7204 current_class_type = type;
7206 /* By default, things in classes are private, while things in
7207 structures or unions are public. */
7208 current_access_specifier = (CLASSTYPE_DECLARED_CLASS (type)
7209 ? access_private_node
7210 : access_public_node);
7212 if (previous_class_level
7213 && type != previous_class_level->this_entity
7214 && current_class_depth == 1)
7216 /* Forcibly remove any old class remnants. */
7217 invalidate_class_lookup_cache ();
7220 if (!previous_class_level
7221 || type != previous_class_level->this_entity
7222 || current_class_depth > 1)
7223 pushlevel_class ();
7224 else
7225 restore_class_cache ();
7228 /* When we exit a toplevel class scope, we save its binding level so
7229 that we can restore it quickly. Here, we've entered some other
7230 class, so we must invalidate our cache. */
7232 void
7233 invalidate_class_lookup_cache (void)
7235 previous_class_level = NULL;
7238 /* Get out of the current class scope. If we were in a class scope
7239 previously, that is the one popped to. */
7241 void
7242 popclass (void)
7244 poplevel_class ();
7246 current_class_depth--;
7247 current_class_name = current_class_stack[current_class_depth].name;
7248 current_class_type = current_class_stack[current_class_depth].type;
7249 current_access_specifier = current_class_stack[current_class_depth].access;
7250 if (current_class_stack[current_class_depth].names_used)
7251 splay_tree_delete (current_class_stack[current_class_depth].names_used);
7254 /* Mark the top of the class stack as hidden. */
7256 void
7257 push_class_stack (void)
7259 if (current_class_depth)
7260 ++current_class_stack[current_class_depth - 1].hidden;
7263 /* Mark the top of the class stack as un-hidden. */
7265 void
7266 pop_class_stack (void)
7268 if (current_class_depth)
7269 --current_class_stack[current_class_depth - 1].hidden;
7272 /* Returns 1 if the class type currently being defined is either T or
7273 a nested type of T. */
7275 bool
7276 currently_open_class (tree t)
7278 int i;
7280 if (!CLASS_TYPE_P (t))
7281 return false;
7283 t = TYPE_MAIN_VARIANT (t);
7285 /* We start looking from 1 because entry 0 is from global scope,
7286 and has no type. */
7287 for (i = current_class_depth; i > 0; --i)
7289 tree c;
7290 if (i == current_class_depth)
7291 c = current_class_type;
7292 else
7294 if (current_class_stack[i].hidden)
7295 break;
7296 c = current_class_stack[i].type;
7298 if (!c)
7299 continue;
7300 if (same_type_p (c, t))
7301 return true;
7303 return false;
7306 /* If either current_class_type or one of its enclosing classes are derived
7307 from T, return the appropriate type. Used to determine how we found
7308 something via unqualified lookup. */
7310 tree
7311 currently_open_derived_class (tree t)
7313 int i;
7315 /* The bases of a dependent type are unknown. */
7316 if (dependent_type_p (t))
7317 return NULL_TREE;
7319 if (!current_class_type)
7320 return NULL_TREE;
7322 if (DERIVED_FROM_P (t, current_class_type))
7323 return current_class_type;
7325 for (i = current_class_depth - 1; i > 0; --i)
7327 if (current_class_stack[i].hidden)
7328 break;
7329 if (DERIVED_FROM_P (t, current_class_stack[i].type))
7330 return current_class_stack[i].type;
7333 return NULL_TREE;
7336 /* Return the outermost enclosing class type that is still open, or
7337 NULL_TREE. */
7339 tree
7340 outermost_open_class (void)
7342 if (!current_class_type)
7343 return NULL_TREE;
7344 tree r = NULL_TREE;
7345 if (TYPE_BEING_DEFINED (current_class_type))
7346 r = current_class_type;
7347 for (int i = current_class_depth - 1; i > 0; --i)
7349 if (current_class_stack[i].hidden)
7350 break;
7351 tree t = current_class_stack[i].type;
7352 if (!TYPE_BEING_DEFINED (t))
7353 break;
7354 r = t;
7356 return r;
7359 /* Returns the innermost class type which is not a lambda closure type. */
7361 tree
7362 current_nonlambda_class_type (void)
7364 int i;
7366 /* We start looking from 1 because entry 0 is from global scope,
7367 and has no type. */
7368 for (i = current_class_depth; i > 0; --i)
7370 tree c;
7371 if (i == current_class_depth)
7372 c = current_class_type;
7373 else
7375 if (current_class_stack[i].hidden)
7376 break;
7377 c = current_class_stack[i].type;
7379 if (!c)
7380 continue;
7381 if (!LAMBDA_TYPE_P (c))
7382 return c;
7384 return NULL_TREE;
7387 /* When entering a class scope, all enclosing class scopes' names with
7388 static meaning (static variables, static functions, types and
7389 enumerators) have to be visible. This recursive function calls
7390 pushclass for all enclosing class contexts until global or a local
7391 scope is reached. TYPE is the enclosed class. */
7393 void
7394 push_nested_class (tree type)
7396 /* A namespace might be passed in error cases, like A::B:C. */
7397 if (type == NULL_TREE
7398 || !CLASS_TYPE_P (type))
7399 return;
7401 push_nested_class (DECL_CONTEXT (TYPE_MAIN_DECL (type)));
7403 pushclass (type);
7406 /* Undoes a push_nested_class call. */
7408 void
7409 pop_nested_class (void)
7411 tree context = DECL_CONTEXT (TYPE_MAIN_DECL (current_class_type));
7413 popclass ();
7414 if (context && CLASS_TYPE_P (context))
7415 pop_nested_class ();
7418 /* Returns the number of extern "LANG" blocks we are nested within. */
7421 current_lang_depth (void)
7423 return vec_safe_length (current_lang_base);
7426 /* Set global variables CURRENT_LANG_NAME to appropriate value
7427 so that behavior of name-mangling machinery is correct. */
7429 void
7430 push_lang_context (tree name)
7432 vec_safe_push (current_lang_base, current_lang_name);
7434 if (name == lang_name_cplusplus)
7436 current_lang_name = name;
7438 else if (name == lang_name_java)
7440 current_lang_name = name;
7441 /* DECL_IGNORED_P is initially set for these types, to avoid clutter.
7442 (See record_builtin_java_type in decl.c.) However, that causes
7443 incorrect debug entries if these types are actually used.
7444 So we re-enable debug output after extern "Java". */
7445 DECL_IGNORED_P (TYPE_NAME (java_byte_type_node)) = 0;
7446 DECL_IGNORED_P (TYPE_NAME (java_short_type_node)) = 0;
7447 DECL_IGNORED_P (TYPE_NAME (java_int_type_node)) = 0;
7448 DECL_IGNORED_P (TYPE_NAME (java_long_type_node)) = 0;
7449 DECL_IGNORED_P (TYPE_NAME (java_float_type_node)) = 0;
7450 DECL_IGNORED_P (TYPE_NAME (java_double_type_node)) = 0;
7451 DECL_IGNORED_P (TYPE_NAME (java_char_type_node)) = 0;
7452 DECL_IGNORED_P (TYPE_NAME (java_boolean_type_node)) = 0;
7454 else if (name == lang_name_c)
7456 current_lang_name = name;
7458 else
7459 error ("language string %<\"%E\"%> not recognized", name);
7462 /* Get out of the current language scope. */
7464 void
7465 pop_lang_context (void)
7467 current_lang_name = current_lang_base->pop ();
7470 /* Type instantiation routines. */
7472 /* Given an OVERLOAD and a TARGET_TYPE, return the function that
7473 matches the TARGET_TYPE. If there is no satisfactory match, return
7474 error_mark_node, and issue an error & warning messages under
7475 control of FLAGS. Permit pointers to member function if FLAGS
7476 permits. If TEMPLATE_ONLY, the name of the overloaded function was
7477 a template-id, and EXPLICIT_TARGS are the explicitly provided
7478 template arguments.
7480 If OVERLOAD is for one or more member functions, then ACCESS_PATH
7481 is the base path used to reference those member functions. If
7482 the address is resolved to a member function, access checks will be
7483 performed and errors issued if appropriate. */
7485 static tree
7486 resolve_address_of_overloaded_function (tree target_type,
7487 tree overload,
7488 tsubst_flags_t complain,
7489 bool template_only,
7490 tree explicit_targs,
7491 tree access_path)
7493 /* Here's what the standard says:
7495 [over.over]
7497 If the name is a function template, template argument deduction
7498 is done, and if the argument deduction succeeds, the deduced
7499 arguments are used to generate a single template function, which
7500 is added to the set of overloaded functions considered.
7502 Non-member functions and static member functions match targets of
7503 type "pointer-to-function" or "reference-to-function." Nonstatic
7504 member functions match targets of type "pointer-to-member
7505 function;" the function type of the pointer to member is used to
7506 select the member function from the set of overloaded member
7507 functions. If a nonstatic member function is selected, the
7508 reference to the overloaded function name is required to have the
7509 form of a pointer to member as described in 5.3.1.
7511 If more than one function is selected, any template functions in
7512 the set are eliminated if the set also contains a non-template
7513 function, and any given template function is eliminated if the
7514 set contains a second template function that is more specialized
7515 than the first according to the partial ordering rules 14.5.5.2.
7516 After such eliminations, if any, there shall remain exactly one
7517 selected function. */
7519 int is_ptrmem = 0;
7520 /* We store the matches in a TREE_LIST rooted here. The functions
7521 are the TREE_PURPOSE, not the TREE_VALUE, in this list, for easy
7522 interoperability with most_specialized_instantiation. */
7523 tree matches = NULL_TREE;
7524 tree fn;
7525 tree target_fn_type;
7527 /* By the time we get here, we should be seeing only real
7528 pointer-to-member types, not the internal POINTER_TYPE to
7529 METHOD_TYPE representation. */
7530 gcc_assert (!TYPE_PTR_P (target_type)
7531 || TREE_CODE (TREE_TYPE (target_type)) != METHOD_TYPE);
7533 gcc_assert (is_overloaded_fn (overload));
7535 /* Check that the TARGET_TYPE is reasonable. */
7536 if (TYPE_PTRFN_P (target_type)
7537 || TYPE_REFFN_P (target_type))
7538 /* This is OK. */;
7539 else if (TYPE_PTRMEMFUNC_P (target_type))
7540 /* This is OK, too. */
7541 is_ptrmem = 1;
7542 else if (TREE_CODE (target_type) == FUNCTION_TYPE)
7543 /* This is OK, too. This comes from a conversion to reference
7544 type. */
7545 target_type = build_reference_type (target_type);
7546 else
7548 if (complain & tf_error)
7549 error ("cannot resolve overloaded function %qD based on"
7550 " conversion to type %qT",
7551 DECL_NAME (OVL_FUNCTION (overload)), target_type);
7552 return error_mark_node;
7555 /* Non-member functions and static member functions match targets of type
7556 "pointer-to-function" or "reference-to-function." Nonstatic member
7557 functions match targets of type "pointer-to-member-function;" the
7558 function type of the pointer to member is used to select the member
7559 function from the set of overloaded member functions.
7561 So figure out the FUNCTION_TYPE that we want to match against. */
7562 target_fn_type = static_fn_type (target_type);
7564 /* If we can find a non-template function that matches, we can just
7565 use it. There's no point in generating template instantiations
7566 if we're just going to throw them out anyhow. But, of course, we
7567 can only do this when we don't *need* a template function. */
7568 if (!template_only)
7570 tree fns;
7572 for (fns = overload; fns; fns = OVL_NEXT (fns))
7574 tree fn = OVL_CURRENT (fns);
7576 if (TREE_CODE (fn) == TEMPLATE_DECL)
7577 /* We're not looking for templates just yet. */
7578 continue;
7580 if ((TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
7581 != is_ptrmem)
7582 /* We're looking for a non-static member, and this isn't
7583 one, or vice versa. */
7584 continue;
7586 /* Ignore functions which haven't been explicitly
7587 declared. */
7588 if (DECL_ANTICIPATED (fn))
7589 continue;
7591 /* See if there's a match. */
7592 if (same_type_p (target_fn_type, static_fn_type (fn)))
7593 matches = tree_cons (fn, NULL_TREE, matches);
7597 /* Now, if we've already got a match (or matches), there's no need
7598 to proceed to the template functions. But, if we don't have a
7599 match we need to look at them, too. */
7600 if (!matches)
7602 tree target_arg_types;
7603 tree target_ret_type;
7604 tree fns;
7605 tree *args;
7606 unsigned int nargs, ia;
7607 tree arg;
7609 target_arg_types = TYPE_ARG_TYPES (target_fn_type);
7610 target_ret_type = TREE_TYPE (target_fn_type);
7612 nargs = list_length (target_arg_types);
7613 args = XALLOCAVEC (tree, nargs);
7614 for (arg = target_arg_types, ia = 0;
7615 arg != NULL_TREE && arg != void_list_node;
7616 arg = TREE_CHAIN (arg), ++ia)
7617 args[ia] = TREE_VALUE (arg);
7618 nargs = ia;
7620 for (fns = overload; fns; fns = OVL_NEXT (fns))
7622 tree fn = OVL_CURRENT (fns);
7623 tree instantiation;
7624 tree targs;
7626 if (TREE_CODE (fn) != TEMPLATE_DECL)
7627 /* We're only looking for templates. */
7628 continue;
7630 if ((TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
7631 != is_ptrmem)
7632 /* We're not looking for a non-static member, and this is
7633 one, or vice versa. */
7634 continue;
7636 tree ret = target_ret_type;
7638 /* If the template has a deduced return type, don't expose it to
7639 template argument deduction. */
7640 if (undeduced_auto_decl (fn))
7641 ret = NULL_TREE;
7643 /* Try to do argument deduction. */
7644 targs = make_tree_vec (DECL_NTPARMS (fn));
7645 instantiation = fn_type_unification (fn, explicit_targs, targs, args,
7646 nargs, ret,
7647 DEDUCE_EXACT, LOOKUP_NORMAL,
7648 false, false);
7649 if (instantiation == error_mark_node)
7650 /* Instantiation failed. */
7651 continue;
7653 /* And now force instantiation to do return type deduction. */
7654 if (undeduced_auto_decl (instantiation))
7656 ++function_depth;
7657 instantiate_decl (instantiation, /*defer*/false, /*class*/false);
7658 --function_depth;
7660 require_deduced_type (instantiation);
7663 /* See if there's a match. */
7664 if (same_type_p (target_fn_type, static_fn_type (instantiation)))
7665 matches = tree_cons (instantiation, fn, matches);
7668 /* Now, remove all but the most specialized of the matches. */
7669 if (matches)
7671 tree match = most_specialized_instantiation (matches);
7673 if (match != error_mark_node)
7674 matches = tree_cons (TREE_PURPOSE (match),
7675 NULL_TREE,
7676 NULL_TREE);
7680 /* Now we should have exactly one function in MATCHES. */
7681 if (matches == NULL_TREE)
7683 /* There were *no* matches. */
7684 if (complain & tf_error)
7686 error ("no matches converting function %qD to type %q#T",
7687 DECL_NAME (OVL_CURRENT (overload)),
7688 target_type);
7690 print_candidates (overload);
7692 return error_mark_node;
7694 else if (TREE_CHAIN (matches))
7696 /* There were too many matches. First check if they're all
7697 the same function. */
7698 tree match = NULL_TREE;
7700 fn = TREE_PURPOSE (matches);
7702 /* For multi-versioned functions, more than one match is just fine and
7703 decls_match will return false as they are different. */
7704 for (match = TREE_CHAIN (matches); match; match = TREE_CHAIN (match))
7705 if (!decls_match (fn, TREE_PURPOSE (match))
7706 && !targetm.target_option.function_versions
7707 (fn, TREE_PURPOSE (match)))
7708 break;
7710 if (match)
7712 if (complain & tf_error)
7714 error ("converting overloaded function %qD to type %q#T is ambiguous",
7715 DECL_NAME (OVL_FUNCTION (overload)),
7716 target_type);
7718 /* Since print_candidates expects the functions in the
7719 TREE_VALUE slot, we flip them here. */
7720 for (match = matches; match; match = TREE_CHAIN (match))
7721 TREE_VALUE (match) = TREE_PURPOSE (match);
7723 print_candidates (matches);
7726 return error_mark_node;
7730 /* Good, exactly one match. Now, convert it to the correct type. */
7731 fn = TREE_PURPOSE (matches);
7733 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
7734 && !(complain & tf_ptrmem_ok) && !flag_ms_extensions)
7736 static int explained;
7738 if (!(complain & tf_error))
7739 return error_mark_node;
7741 permerror (input_location, "assuming pointer to member %qD", fn);
7742 if (!explained)
7744 inform (input_location, "(a pointer to member can only be formed with %<&%E%>)", fn);
7745 explained = 1;
7749 /* If a pointer to a function that is multi-versioned is requested, the
7750 pointer to the dispatcher function is returned instead. This works
7751 well because indirectly calling the function will dispatch the right
7752 function version at run-time. */
7753 if (DECL_FUNCTION_VERSIONED (fn))
7755 fn = get_function_version_dispatcher (fn);
7756 if (fn == NULL)
7757 return error_mark_node;
7758 /* Mark all the versions corresponding to the dispatcher as used. */
7759 if (!(complain & tf_conv))
7760 mark_versions_used (fn);
7763 /* If we're doing overload resolution purely for the purpose of
7764 determining conversion sequences, we should not consider the
7765 function used. If this conversion sequence is selected, the
7766 function will be marked as used at this point. */
7767 if (!(complain & tf_conv))
7769 /* Make =delete work with SFINAE. */
7770 if (DECL_DELETED_FN (fn) && !(complain & tf_error))
7771 return error_mark_node;
7772 if (!mark_used (fn, complain) && !(complain & tf_error))
7773 return error_mark_node;
7776 /* We could not check access to member functions when this
7777 expression was originally created since we did not know at that
7778 time to which function the expression referred. */
7779 if (DECL_FUNCTION_MEMBER_P (fn))
7781 gcc_assert (access_path);
7782 perform_or_defer_access_check (access_path, fn, fn, complain);
7785 if (TYPE_PTRFN_P (target_type) || TYPE_PTRMEMFUNC_P (target_type))
7786 return cp_build_addr_expr (fn, complain);
7787 else
7789 /* The target must be a REFERENCE_TYPE. Above, cp_build_unary_op
7790 will mark the function as addressed, but here we must do it
7791 explicitly. */
7792 cxx_mark_addressable (fn);
7794 return fn;
7798 /* This function will instantiate the type of the expression given in
7799 RHS to match the type of LHSTYPE. If errors exist, then return
7800 error_mark_node. COMPLAIN is a bit mask. If TF_ERROR is set, then
7801 we complain on errors. If we are not complaining, never modify rhs,
7802 as overload resolution wants to try many possible instantiations, in
7803 the hope that at least one will work.
7805 For non-recursive calls, LHSTYPE should be a function, pointer to
7806 function, or a pointer to member function. */
7808 tree
7809 instantiate_type (tree lhstype, tree rhs, tsubst_flags_t complain)
7811 tsubst_flags_t complain_in = complain;
7812 tree access_path = NULL_TREE;
7814 complain &= ~tf_ptrmem_ok;
7816 if (lhstype == unknown_type_node)
7818 if (complain & tf_error)
7819 error ("not enough type information");
7820 return error_mark_node;
7823 if (TREE_TYPE (rhs) != NULL_TREE && ! (type_unknown_p (rhs)))
7825 tree fntype = non_reference (lhstype);
7826 if (same_type_p (fntype, TREE_TYPE (rhs)))
7827 return rhs;
7828 if (flag_ms_extensions
7829 && TYPE_PTRMEMFUNC_P (fntype)
7830 && !TYPE_PTRMEMFUNC_P (TREE_TYPE (rhs)))
7831 /* Microsoft allows `A::f' to be resolved to a
7832 pointer-to-member. */
7834 else
7836 if (complain & tf_error)
7837 error ("cannot convert %qE from type %qT to type %qT",
7838 rhs, TREE_TYPE (rhs), fntype);
7839 return error_mark_node;
7843 if (BASELINK_P (rhs))
7845 access_path = BASELINK_ACCESS_BINFO (rhs);
7846 rhs = BASELINK_FUNCTIONS (rhs);
7849 /* If we are in a template, and have a NON_DEPENDENT_EXPR, we cannot
7850 deduce any type information. */
7851 if (TREE_CODE (rhs) == NON_DEPENDENT_EXPR)
7853 if (complain & tf_error)
7854 error ("not enough type information");
7855 return error_mark_node;
7858 /* There only a few kinds of expressions that may have a type
7859 dependent on overload resolution. */
7860 gcc_assert (TREE_CODE (rhs) == ADDR_EXPR
7861 || TREE_CODE (rhs) == COMPONENT_REF
7862 || is_overloaded_fn (rhs)
7863 || (flag_ms_extensions && TREE_CODE (rhs) == FUNCTION_DECL));
7865 /* This should really only be used when attempting to distinguish
7866 what sort of a pointer to function we have. For now, any
7867 arithmetic operation which is not supported on pointers
7868 is rejected as an error. */
7870 switch (TREE_CODE (rhs))
7872 case COMPONENT_REF:
7874 tree member = TREE_OPERAND (rhs, 1);
7876 member = instantiate_type (lhstype, member, complain);
7877 if (member != error_mark_node
7878 && TREE_SIDE_EFFECTS (TREE_OPERAND (rhs, 0)))
7879 /* Do not lose object's side effects. */
7880 return build2 (COMPOUND_EXPR, TREE_TYPE (member),
7881 TREE_OPERAND (rhs, 0), member);
7882 return member;
7885 case OFFSET_REF:
7886 rhs = TREE_OPERAND (rhs, 1);
7887 if (BASELINK_P (rhs))
7888 return instantiate_type (lhstype, rhs, complain_in);
7890 /* This can happen if we are forming a pointer-to-member for a
7891 member template. */
7892 gcc_assert (TREE_CODE (rhs) == TEMPLATE_ID_EXPR);
7894 /* Fall through. */
7896 case TEMPLATE_ID_EXPR:
7898 tree fns = TREE_OPERAND (rhs, 0);
7899 tree args = TREE_OPERAND (rhs, 1);
7901 return
7902 resolve_address_of_overloaded_function (lhstype, fns, complain_in,
7903 /*template_only=*/true,
7904 args, access_path);
7907 case OVERLOAD:
7908 case FUNCTION_DECL:
7909 return
7910 resolve_address_of_overloaded_function (lhstype, rhs, complain_in,
7911 /*template_only=*/false,
7912 /*explicit_targs=*/NULL_TREE,
7913 access_path);
7915 case ADDR_EXPR:
7917 if (PTRMEM_OK_P (rhs))
7918 complain |= tf_ptrmem_ok;
7920 return instantiate_type (lhstype, TREE_OPERAND (rhs, 0), complain);
7923 case ERROR_MARK:
7924 return error_mark_node;
7926 default:
7927 gcc_unreachable ();
7929 return error_mark_node;
7932 /* Return the name of the virtual function pointer field
7933 (as an IDENTIFIER_NODE) for the given TYPE. Note that
7934 this may have to look back through base types to find the
7935 ultimate field name. (For single inheritance, these could
7936 all be the same name. Who knows for multiple inheritance). */
7938 static tree
7939 get_vfield_name (tree type)
7941 tree binfo, base_binfo;
7942 char *buf;
7944 for (binfo = TYPE_BINFO (type);
7945 BINFO_N_BASE_BINFOS (binfo);
7946 binfo = base_binfo)
7948 base_binfo = BINFO_BASE_BINFO (binfo, 0);
7950 if (BINFO_VIRTUAL_P (base_binfo)
7951 || !TYPE_CONTAINS_VPTR_P (BINFO_TYPE (base_binfo)))
7952 break;
7955 type = BINFO_TYPE (binfo);
7956 buf = (char *) alloca (sizeof (VFIELD_NAME_FORMAT)
7957 + TYPE_NAME_LENGTH (type) + 2);
7958 sprintf (buf, VFIELD_NAME_FORMAT,
7959 IDENTIFIER_POINTER (constructor_name (type)));
7960 return get_identifier (buf);
7963 void
7964 print_class_statistics (void)
7966 if (! GATHER_STATISTICS)
7967 return;
7969 fprintf (stderr, "convert_harshness = %d\n", n_convert_harshness);
7970 fprintf (stderr, "compute_conversion_costs = %d\n", n_compute_conversion_costs);
7971 if (n_vtables)
7973 fprintf (stderr, "vtables = %d; vtable searches = %d\n",
7974 n_vtables, n_vtable_searches);
7975 fprintf (stderr, "vtable entries = %d; vtable elems = %d\n",
7976 n_vtable_entries, n_vtable_elems);
7980 /* Build a dummy reference to ourselves so Derived::Base (and A::A) works,
7981 according to [class]:
7982 The class-name is also inserted
7983 into the scope of the class itself. For purposes of access checking,
7984 the inserted class name is treated as if it were a public member name. */
7986 void
7987 build_self_reference (void)
7989 tree name = constructor_name (current_class_type);
7990 tree value = build_lang_decl (TYPE_DECL, name, current_class_type);
7991 tree saved_cas;
7993 DECL_NONLOCAL (value) = 1;
7994 DECL_CONTEXT (value) = current_class_type;
7995 DECL_ARTIFICIAL (value) = 1;
7996 SET_DECL_SELF_REFERENCE_P (value);
7997 set_underlying_type (value);
7999 if (processing_template_decl)
8000 value = push_template_decl (value);
8002 saved_cas = current_access_specifier;
8003 current_access_specifier = access_public_node;
8004 finish_member_declaration (value);
8005 current_access_specifier = saved_cas;
8008 /* Returns 1 if TYPE contains only padding bytes. */
8011 is_empty_class (tree type)
8013 if (type == error_mark_node)
8014 return 0;
8016 if (! CLASS_TYPE_P (type))
8017 return 0;
8019 return CLASSTYPE_EMPTY_P (type);
8022 /* Returns true if TYPE contains no actual data, just various
8023 possible combinations of empty classes and possibly a vptr. */
8025 bool
8026 is_really_empty_class (tree type)
8028 if (CLASS_TYPE_P (type))
8030 tree field;
8031 tree binfo;
8032 tree base_binfo;
8033 int i;
8035 /* CLASSTYPE_EMPTY_P isn't set properly until the class is actually laid
8036 out, but we'd like to be able to check this before then. */
8037 if (COMPLETE_TYPE_P (type) && is_empty_class (type))
8038 return true;
8040 for (binfo = TYPE_BINFO (type), i = 0;
8041 BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
8042 if (!is_really_empty_class (BINFO_TYPE (base_binfo)))
8043 return false;
8044 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
8045 if (TREE_CODE (field) == FIELD_DECL
8046 && !DECL_ARTIFICIAL (field)
8047 && !is_really_empty_class (TREE_TYPE (field)))
8048 return false;
8049 return true;
8051 else if (TREE_CODE (type) == ARRAY_TYPE)
8052 return is_really_empty_class (TREE_TYPE (type));
8053 return false;
8056 /* Note that NAME was looked up while the current class was being
8057 defined and that the result of that lookup was DECL. */
8059 void
8060 maybe_note_name_used_in_class (tree name, tree decl)
8062 splay_tree names_used;
8064 /* If we're not defining a class, there's nothing to do. */
8065 if (!(innermost_scope_kind() == sk_class
8066 && TYPE_BEING_DEFINED (current_class_type)
8067 && !LAMBDA_TYPE_P (current_class_type)))
8068 return;
8070 /* If there's already a binding for this NAME, then we don't have
8071 anything to worry about. */
8072 if (lookup_member (current_class_type, name,
8073 /*protect=*/0, /*want_type=*/false, tf_warning_or_error))
8074 return;
8076 if (!current_class_stack[current_class_depth - 1].names_used)
8077 current_class_stack[current_class_depth - 1].names_used
8078 = splay_tree_new (splay_tree_compare_pointers, 0, 0);
8079 names_used = current_class_stack[current_class_depth - 1].names_used;
8081 splay_tree_insert (names_used,
8082 (splay_tree_key) name,
8083 (splay_tree_value) decl);
8086 /* Note that NAME was declared (as DECL) in the current class. Check
8087 to see that the declaration is valid. */
8089 void
8090 note_name_declared_in_class (tree name, tree decl)
8092 splay_tree names_used;
8093 splay_tree_node n;
8095 /* Look to see if we ever used this name. */
8096 names_used
8097 = current_class_stack[current_class_depth - 1].names_used;
8098 if (!names_used)
8099 return;
8100 /* The C language allows members to be declared with a type of the same
8101 name, and the C++ standard says this diagnostic is not required. So
8102 allow it in extern "C" blocks unless predantic is specified.
8103 Allow it in all cases if -ms-extensions is specified. */
8104 if ((!pedantic && current_lang_name == lang_name_c)
8105 || flag_ms_extensions)
8106 return;
8107 n = splay_tree_lookup (names_used, (splay_tree_key) name);
8108 if (n)
8110 /* [basic.scope.class]
8112 A name N used in a class S shall refer to the same declaration
8113 in its context and when re-evaluated in the completed scope of
8114 S. */
8115 permerror (input_location, "declaration of %q#D", decl);
8116 permerror (input_location, "changes meaning of %qD from %q+#D",
8117 DECL_NAME (OVL_CURRENT (decl)), (tree) n->value);
8121 /* Returns the VAR_DECL for the complete vtable associated with BINFO.
8122 Secondary vtables are merged with primary vtables; this function
8123 will return the VAR_DECL for the primary vtable. */
8125 tree
8126 get_vtbl_decl_for_binfo (tree binfo)
8128 tree decl;
8130 decl = BINFO_VTABLE (binfo);
8131 if (decl && TREE_CODE (decl) == POINTER_PLUS_EXPR)
8133 gcc_assert (TREE_CODE (TREE_OPERAND (decl, 0)) == ADDR_EXPR);
8134 decl = TREE_OPERAND (TREE_OPERAND (decl, 0), 0);
8136 if (decl)
8137 gcc_assert (VAR_P (decl));
8138 return decl;
8142 /* Returns the binfo for the primary base of BINFO. If the resulting
8143 BINFO is a virtual base, and it is inherited elsewhere in the
8144 hierarchy, then the returned binfo might not be the primary base of
8145 BINFO in the complete object. Check BINFO_PRIMARY_P or
8146 BINFO_LOST_PRIMARY_P to be sure. */
8148 static tree
8149 get_primary_binfo (tree binfo)
8151 tree primary_base;
8153 primary_base = CLASSTYPE_PRIMARY_BINFO (BINFO_TYPE (binfo));
8154 if (!primary_base)
8155 return NULL_TREE;
8157 return copied_binfo (primary_base, binfo);
8160 /* If INDENTED_P is zero, indent to INDENT. Return nonzero. */
8162 static int
8163 maybe_indent_hierarchy (FILE * stream, int indent, int indented_p)
8165 if (!indented_p)
8166 fprintf (stream, "%*s", indent, "");
8167 return 1;
8170 /* Dump the offsets of all the bases rooted at BINFO to STREAM.
8171 INDENT should be zero when called from the top level; it is
8172 incremented recursively. IGO indicates the next expected BINFO in
8173 inheritance graph ordering. */
8175 static tree
8176 dump_class_hierarchy_r (FILE *stream,
8177 int flags,
8178 tree binfo,
8179 tree igo,
8180 int indent)
8182 int indented = 0;
8183 tree base_binfo;
8184 int i;
8186 indented = maybe_indent_hierarchy (stream, indent, 0);
8187 fprintf (stream, "%s (0x" HOST_WIDE_INT_PRINT_HEX ") ",
8188 type_as_string (BINFO_TYPE (binfo), TFF_PLAIN_IDENTIFIER),
8189 (HOST_WIDE_INT) (uintptr_t) binfo);
8190 if (binfo != igo)
8192 fprintf (stream, "alternative-path\n");
8193 return igo;
8195 igo = TREE_CHAIN (binfo);
8197 fprintf (stream, HOST_WIDE_INT_PRINT_DEC,
8198 tree_to_shwi (BINFO_OFFSET (binfo)));
8199 if (is_empty_class (BINFO_TYPE (binfo)))
8200 fprintf (stream, " empty");
8201 else if (CLASSTYPE_NEARLY_EMPTY_P (BINFO_TYPE (binfo)))
8202 fprintf (stream, " nearly-empty");
8203 if (BINFO_VIRTUAL_P (binfo))
8204 fprintf (stream, " virtual");
8205 fprintf (stream, "\n");
8207 indented = 0;
8208 if (BINFO_PRIMARY_P (binfo))
8210 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
8211 fprintf (stream, " primary-for %s (0x" HOST_WIDE_INT_PRINT_HEX ")",
8212 type_as_string (BINFO_TYPE (BINFO_INHERITANCE_CHAIN (binfo)),
8213 TFF_PLAIN_IDENTIFIER),
8214 (HOST_WIDE_INT) (uintptr_t) BINFO_INHERITANCE_CHAIN (binfo));
8216 if (BINFO_LOST_PRIMARY_P (binfo))
8218 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
8219 fprintf (stream, " lost-primary");
8221 if (indented)
8222 fprintf (stream, "\n");
8224 if (!(flags & TDF_SLIM))
8226 int indented = 0;
8228 if (BINFO_SUBVTT_INDEX (binfo))
8230 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
8231 fprintf (stream, " subvttidx=%s",
8232 expr_as_string (BINFO_SUBVTT_INDEX (binfo),
8233 TFF_PLAIN_IDENTIFIER));
8235 if (BINFO_VPTR_INDEX (binfo))
8237 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
8238 fprintf (stream, " vptridx=%s",
8239 expr_as_string (BINFO_VPTR_INDEX (binfo),
8240 TFF_PLAIN_IDENTIFIER));
8242 if (BINFO_VPTR_FIELD (binfo))
8244 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
8245 fprintf (stream, " vbaseoffset=%s",
8246 expr_as_string (BINFO_VPTR_FIELD (binfo),
8247 TFF_PLAIN_IDENTIFIER));
8249 if (BINFO_VTABLE (binfo))
8251 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
8252 fprintf (stream, " vptr=%s",
8253 expr_as_string (BINFO_VTABLE (binfo),
8254 TFF_PLAIN_IDENTIFIER));
8257 if (indented)
8258 fprintf (stream, "\n");
8261 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
8262 igo = dump_class_hierarchy_r (stream, flags, base_binfo, igo, indent + 2);
8264 return igo;
8267 /* Dump the BINFO hierarchy for T. */
8269 static void
8270 dump_class_hierarchy_1 (FILE *stream, int flags, tree t)
8272 fprintf (stream, "Class %s\n", type_as_string (t, TFF_PLAIN_IDENTIFIER));
8273 fprintf (stream, " size=%lu align=%lu\n",
8274 (unsigned long)(tree_to_shwi (TYPE_SIZE (t)) / BITS_PER_UNIT),
8275 (unsigned long)(TYPE_ALIGN (t) / BITS_PER_UNIT));
8276 fprintf (stream, " base size=%lu base align=%lu\n",
8277 (unsigned long)(tree_to_shwi (TYPE_SIZE (CLASSTYPE_AS_BASE (t)))
8278 / BITS_PER_UNIT),
8279 (unsigned long)(TYPE_ALIGN (CLASSTYPE_AS_BASE (t))
8280 / BITS_PER_UNIT));
8281 dump_class_hierarchy_r (stream, flags, TYPE_BINFO (t), TYPE_BINFO (t), 0);
8282 fprintf (stream, "\n");
8285 /* Debug interface to hierarchy dumping. */
8287 void
8288 debug_class (tree t)
8290 dump_class_hierarchy_1 (stderr, TDF_SLIM, t);
8293 static void
8294 dump_class_hierarchy (tree t)
8296 int flags;
8297 FILE *stream = get_dump_info (TDI_class, &flags);
8299 if (stream)
8301 dump_class_hierarchy_1 (stream, flags, t);
8305 static void
8306 dump_array (FILE * stream, tree decl)
8308 tree value;
8309 unsigned HOST_WIDE_INT ix;
8310 HOST_WIDE_INT elt;
8311 tree size = TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (decl)));
8313 elt = (tree_to_shwi (TYPE_SIZE (TREE_TYPE (TREE_TYPE (decl))))
8314 / BITS_PER_UNIT);
8315 fprintf (stream, "%s:", decl_as_string (decl, TFF_PLAIN_IDENTIFIER));
8316 fprintf (stream, " %s entries",
8317 expr_as_string (size_binop (PLUS_EXPR, size, size_one_node),
8318 TFF_PLAIN_IDENTIFIER));
8319 fprintf (stream, "\n");
8321 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (DECL_INITIAL (decl)),
8322 ix, value)
8323 fprintf (stream, "%-4ld %s\n", (long)(ix * elt),
8324 expr_as_string (value, TFF_PLAIN_IDENTIFIER));
8327 static void
8328 dump_vtable (tree t, tree binfo, tree vtable)
8330 int flags;
8331 FILE *stream = get_dump_info (TDI_class, &flags);
8333 if (!stream)
8334 return;
8336 if (!(flags & TDF_SLIM))
8338 int ctor_vtbl_p = TYPE_BINFO (t) != binfo;
8340 fprintf (stream, "%s for %s",
8341 ctor_vtbl_p ? "Construction vtable" : "Vtable",
8342 type_as_string (BINFO_TYPE (binfo), TFF_PLAIN_IDENTIFIER));
8343 if (ctor_vtbl_p)
8345 if (!BINFO_VIRTUAL_P (binfo))
8346 fprintf (stream, " (0x" HOST_WIDE_INT_PRINT_HEX " instance)",
8347 (HOST_WIDE_INT) (uintptr_t) binfo);
8348 fprintf (stream, " in %s", type_as_string (t, TFF_PLAIN_IDENTIFIER));
8350 fprintf (stream, "\n");
8351 dump_array (stream, vtable);
8352 fprintf (stream, "\n");
8356 static void
8357 dump_vtt (tree t, tree vtt)
8359 int flags;
8360 FILE *stream = get_dump_info (TDI_class, &flags);
8362 if (!stream)
8363 return;
8365 if (!(flags & TDF_SLIM))
8367 fprintf (stream, "VTT for %s\n",
8368 type_as_string (t, TFF_PLAIN_IDENTIFIER));
8369 dump_array (stream, vtt);
8370 fprintf (stream, "\n");
8374 /* Dump a function or thunk and its thunkees. */
8376 static void
8377 dump_thunk (FILE *stream, int indent, tree thunk)
8379 static const char spaces[] = " ";
8380 tree name = DECL_NAME (thunk);
8381 tree thunks;
8383 fprintf (stream, "%.*s%p %s %s", indent, spaces,
8384 (void *)thunk,
8385 !DECL_THUNK_P (thunk) ? "function"
8386 : DECL_THIS_THUNK_P (thunk) ? "this-thunk" : "covariant-thunk",
8387 name ? IDENTIFIER_POINTER (name) : "<unset>");
8388 if (DECL_THUNK_P (thunk))
8390 HOST_WIDE_INT fixed_adjust = THUNK_FIXED_OFFSET (thunk);
8391 tree virtual_adjust = THUNK_VIRTUAL_OFFSET (thunk);
8393 fprintf (stream, " fixed=" HOST_WIDE_INT_PRINT_DEC, fixed_adjust);
8394 if (!virtual_adjust)
8395 /*NOP*/;
8396 else if (DECL_THIS_THUNK_P (thunk))
8397 fprintf (stream, " vcall=" HOST_WIDE_INT_PRINT_DEC,
8398 tree_to_shwi (virtual_adjust));
8399 else
8400 fprintf (stream, " vbase=" HOST_WIDE_INT_PRINT_DEC "(%s)",
8401 tree_to_shwi (BINFO_VPTR_FIELD (virtual_adjust)),
8402 type_as_string (BINFO_TYPE (virtual_adjust), TFF_SCOPE));
8403 if (THUNK_ALIAS (thunk))
8404 fprintf (stream, " alias to %p", (void *)THUNK_ALIAS (thunk));
8406 fprintf (stream, "\n");
8407 for (thunks = DECL_THUNKS (thunk); thunks; thunks = TREE_CHAIN (thunks))
8408 dump_thunk (stream, indent + 2, thunks);
8411 /* Dump the thunks for FN. */
8413 void
8414 debug_thunks (tree fn)
8416 dump_thunk (stderr, 0, fn);
8419 /* Virtual function table initialization. */
8421 /* Create all the necessary vtables for T and its base classes. */
8423 static void
8424 finish_vtbls (tree t)
8426 tree vbase;
8427 vec<constructor_elt, va_gc> *v = NULL;
8428 tree vtable = BINFO_VTABLE (TYPE_BINFO (t));
8430 /* We lay out the primary and secondary vtables in one contiguous
8431 vtable. The primary vtable is first, followed by the non-virtual
8432 secondary vtables in inheritance graph order. */
8433 accumulate_vtbl_inits (TYPE_BINFO (t), TYPE_BINFO (t), TYPE_BINFO (t),
8434 vtable, t, &v);
8436 /* Then come the virtual bases, also in inheritance graph order. */
8437 for (vbase = TYPE_BINFO (t); vbase; vbase = TREE_CHAIN (vbase))
8439 if (!BINFO_VIRTUAL_P (vbase))
8440 continue;
8441 accumulate_vtbl_inits (vbase, vbase, TYPE_BINFO (t), vtable, t, &v);
8444 if (BINFO_VTABLE (TYPE_BINFO (t)))
8445 initialize_vtable (TYPE_BINFO (t), v);
8448 /* Initialize the vtable for BINFO with the INITS. */
8450 static void
8451 initialize_vtable (tree binfo, vec<constructor_elt, va_gc> *inits)
8453 tree decl;
8455 layout_vtable_decl (binfo, vec_safe_length (inits));
8456 decl = get_vtbl_decl_for_binfo (binfo);
8457 initialize_artificial_var (decl, inits);
8458 dump_vtable (BINFO_TYPE (binfo), binfo, decl);
8461 /* Build the VTT (virtual table table) for T.
8462 A class requires a VTT if it has virtual bases.
8464 This holds
8465 1 - primary virtual pointer for complete object T
8466 2 - secondary VTTs for each direct non-virtual base of T which requires a
8468 3 - secondary virtual pointers for each direct or indirect base of T which
8469 has virtual bases or is reachable via a virtual path from T.
8470 4 - secondary VTTs for each direct or indirect virtual base of T.
8472 Secondary VTTs look like complete object VTTs without part 4. */
8474 static void
8475 build_vtt (tree t)
8477 tree type;
8478 tree vtt;
8479 tree index;
8480 vec<constructor_elt, va_gc> *inits;
8482 /* Build up the initializers for the VTT. */
8483 inits = NULL;
8484 index = size_zero_node;
8485 build_vtt_inits (TYPE_BINFO (t), t, &inits, &index);
8487 /* If we didn't need a VTT, we're done. */
8488 if (!inits)
8489 return;
8491 /* Figure out the type of the VTT. */
8492 type = build_array_of_n_type (const_ptr_type_node,
8493 inits->length ());
8495 /* Now, build the VTT object itself. */
8496 vtt = build_vtable (t, mangle_vtt_for_type (t), type);
8497 initialize_artificial_var (vtt, inits);
8498 /* Add the VTT to the vtables list. */
8499 DECL_CHAIN (vtt) = DECL_CHAIN (CLASSTYPE_VTABLES (t));
8500 DECL_CHAIN (CLASSTYPE_VTABLES (t)) = vtt;
8502 dump_vtt (t, vtt);
8505 /* When building a secondary VTT, BINFO_VTABLE is set to a TREE_LIST with
8506 PURPOSE the RTTI_BINFO, VALUE the real vtable pointer for this binfo,
8507 and CHAIN the vtable pointer for this binfo after construction is
8508 complete. VALUE can also be another BINFO, in which case we recurse. */
8510 static tree
8511 binfo_ctor_vtable (tree binfo)
8513 tree vt;
8515 while (1)
8517 vt = BINFO_VTABLE (binfo);
8518 if (TREE_CODE (vt) == TREE_LIST)
8519 vt = TREE_VALUE (vt);
8520 if (TREE_CODE (vt) == TREE_BINFO)
8521 binfo = vt;
8522 else
8523 break;
8526 return vt;
8529 /* Data for secondary VTT initialization. */
8530 typedef struct secondary_vptr_vtt_init_data_s
8532 /* Is this the primary VTT? */
8533 bool top_level_p;
8535 /* Current index into the VTT. */
8536 tree index;
8538 /* Vector of initializers built up. */
8539 vec<constructor_elt, va_gc> *inits;
8541 /* The type being constructed by this secondary VTT. */
8542 tree type_being_constructed;
8543 } secondary_vptr_vtt_init_data;
8545 /* Recursively build the VTT-initializer for BINFO (which is in the
8546 hierarchy dominated by T). INITS points to the end of the initializer
8547 list to date. INDEX is the VTT index where the next element will be
8548 replaced. Iff BINFO is the binfo for T, this is the top level VTT (i.e.
8549 not a subvtt for some base of T). When that is so, we emit the sub-VTTs
8550 for virtual bases of T. When it is not so, we build the constructor
8551 vtables for the BINFO-in-T variant. */
8553 static void
8554 build_vtt_inits (tree binfo, tree t, vec<constructor_elt, va_gc> **inits,
8555 tree *index)
8557 int i;
8558 tree b;
8559 tree init;
8560 secondary_vptr_vtt_init_data data;
8561 int top_level_p = SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), t);
8563 /* We only need VTTs for subobjects with virtual bases. */
8564 if (!CLASSTYPE_VBASECLASSES (BINFO_TYPE (binfo)))
8565 return;
8567 /* We need to use a construction vtable if this is not the primary
8568 VTT. */
8569 if (!top_level_p)
8571 build_ctor_vtbl_group (binfo, t);
8573 /* Record the offset in the VTT where this sub-VTT can be found. */
8574 BINFO_SUBVTT_INDEX (binfo) = *index;
8577 /* Add the address of the primary vtable for the complete object. */
8578 init = binfo_ctor_vtable (binfo);
8579 CONSTRUCTOR_APPEND_ELT (*inits, NULL_TREE, init);
8580 if (top_level_p)
8582 gcc_assert (!BINFO_VPTR_INDEX (binfo));
8583 BINFO_VPTR_INDEX (binfo) = *index;
8585 *index = size_binop (PLUS_EXPR, *index, TYPE_SIZE_UNIT (ptr_type_node));
8587 /* Recursively add the secondary VTTs for non-virtual bases. */
8588 for (i = 0; BINFO_BASE_ITERATE (binfo, i, b); ++i)
8589 if (!BINFO_VIRTUAL_P (b))
8590 build_vtt_inits (b, t, inits, index);
8592 /* Add secondary virtual pointers for all subobjects of BINFO with
8593 either virtual bases or reachable along a virtual path, except
8594 subobjects that are non-virtual primary bases. */
8595 data.top_level_p = top_level_p;
8596 data.index = *index;
8597 data.inits = *inits;
8598 data.type_being_constructed = BINFO_TYPE (binfo);
8600 dfs_walk_once (binfo, dfs_build_secondary_vptr_vtt_inits, NULL, &data);
8602 *index = data.index;
8604 /* data.inits might have grown as we added secondary virtual pointers.
8605 Make sure our caller knows about the new vector. */
8606 *inits = data.inits;
8608 if (top_level_p)
8609 /* Add the secondary VTTs for virtual bases in inheritance graph
8610 order. */
8611 for (b = TYPE_BINFO (BINFO_TYPE (binfo)); b; b = TREE_CHAIN (b))
8613 if (!BINFO_VIRTUAL_P (b))
8614 continue;
8616 build_vtt_inits (b, t, inits, index);
8618 else
8619 /* Remove the ctor vtables we created. */
8620 dfs_walk_all (binfo, dfs_fixup_binfo_vtbls, NULL, binfo);
8623 /* Called from build_vtt_inits via dfs_walk. BINFO is the binfo for the base
8624 in most derived. DATA is a SECONDARY_VPTR_VTT_INIT_DATA structure. */
8626 static tree
8627 dfs_build_secondary_vptr_vtt_inits (tree binfo, void *data_)
8629 secondary_vptr_vtt_init_data *data = (secondary_vptr_vtt_init_data *)data_;
8631 /* We don't care about bases that don't have vtables. */
8632 if (!TYPE_VFIELD (BINFO_TYPE (binfo)))
8633 return dfs_skip_bases;
8635 /* We're only interested in proper subobjects of the type being
8636 constructed. */
8637 if (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), data->type_being_constructed))
8638 return NULL_TREE;
8640 /* We're only interested in bases with virtual bases or reachable
8641 via a virtual path from the type being constructed. */
8642 if (!(CLASSTYPE_VBASECLASSES (BINFO_TYPE (binfo))
8643 || binfo_via_virtual (binfo, data->type_being_constructed)))
8644 return dfs_skip_bases;
8646 /* We're not interested in non-virtual primary bases. */
8647 if (!BINFO_VIRTUAL_P (binfo) && BINFO_PRIMARY_P (binfo))
8648 return NULL_TREE;
8650 /* Record the index where this secondary vptr can be found. */
8651 if (data->top_level_p)
8653 gcc_assert (!BINFO_VPTR_INDEX (binfo));
8654 BINFO_VPTR_INDEX (binfo) = data->index;
8656 if (BINFO_VIRTUAL_P (binfo))
8658 /* It's a primary virtual base, and this is not a
8659 construction vtable. Find the base this is primary of in
8660 the inheritance graph, and use that base's vtable
8661 now. */
8662 while (BINFO_PRIMARY_P (binfo))
8663 binfo = BINFO_INHERITANCE_CHAIN (binfo);
8667 /* Add the initializer for the secondary vptr itself. */
8668 CONSTRUCTOR_APPEND_ELT (data->inits, NULL_TREE, binfo_ctor_vtable (binfo));
8670 /* Advance the vtt index. */
8671 data->index = size_binop (PLUS_EXPR, data->index,
8672 TYPE_SIZE_UNIT (ptr_type_node));
8674 return NULL_TREE;
8677 /* Called from build_vtt_inits via dfs_walk. After building
8678 constructor vtables and generating the sub-vtt from them, we need
8679 to restore the BINFO_VTABLES that were scribbled on. DATA is the
8680 binfo of the base whose sub vtt was generated. */
8682 static tree
8683 dfs_fixup_binfo_vtbls (tree binfo, void* data)
8685 tree vtable = BINFO_VTABLE (binfo);
8687 if (!TYPE_CONTAINS_VPTR_P (BINFO_TYPE (binfo)))
8688 /* If this class has no vtable, none of its bases do. */
8689 return dfs_skip_bases;
8691 if (!vtable)
8692 /* This might be a primary base, so have no vtable in this
8693 hierarchy. */
8694 return NULL_TREE;
8696 /* If we scribbled the construction vtable vptr into BINFO, clear it
8697 out now. */
8698 if (TREE_CODE (vtable) == TREE_LIST
8699 && (TREE_PURPOSE (vtable) == (tree) data))
8700 BINFO_VTABLE (binfo) = TREE_CHAIN (vtable);
8702 return NULL_TREE;
8705 /* Build the construction vtable group for BINFO which is in the
8706 hierarchy dominated by T. */
8708 static void
8709 build_ctor_vtbl_group (tree binfo, tree t)
8711 tree type;
8712 tree vtbl;
8713 tree id;
8714 tree vbase;
8715 vec<constructor_elt, va_gc> *v;
8717 /* See if we've already created this construction vtable group. */
8718 id = mangle_ctor_vtbl_for_type (t, binfo);
8719 if (IDENTIFIER_GLOBAL_VALUE (id))
8720 return;
8722 gcc_assert (!SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), t));
8723 /* Build a version of VTBL (with the wrong type) for use in
8724 constructing the addresses of secondary vtables in the
8725 construction vtable group. */
8726 vtbl = build_vtable (t, id, ptr_type_node);
8727 DECL_CONSTRUCTION_VTABLE_P (vtbl) = 1;
8728 /* Don't export construction vtables from shared libraries. Even on
8729 targets that don't support hidden visibility, this tells
8730 can_refer_decl_in_current_unit_p not to assume that it's safe to
8731 access from a different compilation unit (bz 54314). */
8732 DECL_VISIBILITY (vtbl) = VISIBILITY_HIDDEN;
8733 DECL_VISIBILITY_SPECIFIED (vtbl) = true;
8735 v = NULL;
8736 accumulate_vtbl_inits (binfo, TYPE_BINFO (TREE_TYPE (binfo)),
8737 binfo, vtbl, t, &v);
8739 /* Add the vtables for each of our virtual bases using the vbase in T
8740 binfo. */
8741 for (vbase = TYPE_BINFO (BINFO_TYPE (binfo));
8742 vbase;
8743 vbase = TREE_CHAIN (vbase))
8745 tree b;
8747 if (!BINFO_VIRTUAL_P (vbase))
8748 continue;
8749 b = copied_binfo (vbase, binfo);
8751 accumulate_vtbl_inits (b, vbase, binfo, vtbl, t, &v);
8754 /* Figure out the type of the construction vtable. */
8755 type = build_array_of_n_type (vtable_entry_type, v->length ());
8756 layout_type (type);
8757 TREE_TYPE (vtbl) = type;
8758 DECL_SIZE (vtbl) = DECL_SIZE_UNIT (vtbl) = NULL_TREE;
8759 layout_decl (vtbl, 0);
8761 /* Initialize the construction vtable. */
8762 CLASSTYPE_VTABLES (t) = chainon (CLASSTYPE_VTABLES (t), vtbl);
8763 initialize_artificial_var (vtbl, v);
8764 dump_vtable (t, binfo, vtbl);
8767 /* Add the vtbl initializers for BINFO (and its bases other than
8768 non-virtual primaries) to the list of INITS. BINFO is in the
8769 hierarchy dominated by T. RTTI_BINFO is the binfo within T of
8770 the constructor the vtbl inits should be accumulated for. (If this
8771 is the complete object vtbl then RTTI_BINFO will be TYPE_BINFO (T).)
8772 ORIG_BINFO is the binfo for this object within BINFO_TYPE (RTTI_BINFO).
8773 BINFO is the active base equivalent of ORIG_BINFO in the inheritance
8774 graph of T. Both BINFO and ORIG_BINFO will have the same BINFO_TYPE,
8775 but are not necessarily the same in terms of layout. */
8777 static void
8778 accumulate_vtbl_inits (tree binfo,
8779 tree orig_binfo,
8780 tree rtti_binfo,
8781 tree vtbl,
8782 tree t,
8783 vec<constructor_elt, va_gc> **inits)
8785 int i;
8786 tree base_binfo;
8787 int ctor_vtbl_p = !SAME_BINFO_TYPE_P (BINFO_TYPE (rtti_binfo), t);
8789 gcc_assert (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), BINFO_TYPE (orig_binfo)));
8791 /* If it doesn't have a vptr, we don't do anything. */
8792 if (!TYPE_CONTAINS_VPTR_P (BINFO_TYPE (binfo)))
8793 return;
8795 /* If we're building a construction vtable, we're not interested in
8796 subobjects that don't require construction vtables. */
8797 if (ctor_vtbl_p
8798 && !CLASSTYPE_VBASECLASSES (BINFO_TYPE (binfo))
8799 && !binfo_via_virtual (orig_binfo, BINFO_TYPE (rtti_binfo)))
8800 return;
8802 /* Build the initializers for the BINFO-in-T vtable. */
8803 dfs_accumulate_vtbl_inits (binfo, orig_binfo, rtti_binfo, vtbl, t, inits);
8805 /* Walk the BINFO and its bases. We walk in preorder so that as we
8806 initialize each vtable we can figure out at what offset the
8807 secondary vtable lies from the primary vtable. We can't use
8808 dfs_walk here because we need to iterate through bases of BINFO
8809 and RTTI_BINFO simultaneously. */
8810 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
8812 /* Skip virtual bases. */
8813 if (BINFO_VIRTUAL_P (base_binfo))
8814 continue;
8815 accumulate_vtbl_inits (base_binfo,
8816 BINFO_BASE_BINFO (orig_binfo, i),
8817 rtti_binfo, vtbl, t,
8818 inits);
8822 /* Called from accumulate_vtbl_inits. Adds the initializers for the
8823 BINFO vtable to L. */
8825 static void
8826 dfs_accumulate_vtbl_inits (tree binfo,
8827 tree orig_binfo,
8828 tree rtti_binfo,
8829 tree orig_vtbl,
8830 tree t,
8831 vec<constructor_elt, va_gc> **l)
8833 tree vtbl = NULL_TREE;
8834 int ctor_vtbl_p = !SAME_BINFO_TYPE_P (BINFO_TYPE (rtti_binfo), t);
8835 int n_inits;
8837 if (ctor_vtbl_p
8838 && BINFO_VIRTUAL_P (orig_binfo) && BINFO_PRIMARY_P (orig_binfo))
8840 /* In the hierarchy of BINFO_TYPE (RTTI_BINFO), this is a
8841 primary virtual base. If it is not the same primary in
8842 the hierarchy of T, we'll need to generate a ctor vtable
8843 for it, to place at its location in T. If it is the same
8844 primary, we still need a VTT entry for the vtable, but it
8845 should point to the ctor vtable for the base it is a
8846 primary for within the sub-hierarchy of RTTI_BINFO.
8848 There are three possible cases:
8850 1) We are in the same place.
8851 2) We are a primary base within a lost primary virtual base of
8852 RTTI_BINFO.
8853 3) We are primary to something not a base of RTTI_BINFO. */
8855 tree b;
8856 tree last = NULL_TREE;
8858 /* First, look through the bases we are primary to for RTTI_BINFO
8859 or a virtual base. */
8860 b = binfo;
8861 while (BINFO_PRIMARY_P (b))
8863 b = BINFO_INHERITANCE_CHAIN (b);
8864 last = b;
8865 if (BINFO_VIRTUAL_P (b) || b == rtti_binfo)
8866 goto found;
8868 /* If we run out of primary links, keep looking down our
8869 inheritance chain; we might be an indirect primary. */
8870 for (b = last; b; b = BINFO_INHERITANCE_CHAIN (b))
8871 if (BINFO_VIRTUAL_P (b) || b == rtti_binfo)
8872 break;
8873 found:
8875 /* If we found RTTI_BINFO, this is case 1. If we found a virtual
8876 base B and it is a base of RTTI_BINFO, this is case 2. In
8877 either case, we share our vtable with LAST, i.e. the
8878 derived-most base within B of which we are a primary. */
8879 if (b == rtti_binfo
8880 || (b && binfo_for_vbase (BINFO_TYPE (b), BINFO_TYPE (rtti_binfo))))
8881 /* Just set our BINFO_VTABLE to point to LAST, as we may not have
8882 set LAST's BINFO_VTABLE yet. We'll extract the actual vptr in
8883 binfo_ctor_vtable after everything's been set up. */
8884 vtbl = last;
8886 /* Otherwise, this is case 3 and we get our own. */
8888 else if (!BINFO_NEW_VTABLE_MARKED (orig_binfo))
8889 return;
8891 n_inits = vec_safe_length (*l);
8893 if (!vtbl)
8895 tree index;
8896 int non_fn_entries;
8898 /* Add the initializer for this vtable. */
8899 build_vtbl_initializer (binfo, orig_binfo, t, rtti_binfo,
8900 &non_fn_entries, l);
8902 /* Figure out the position to which the VPTR should point. */
8903 vtbl = build1 (ADDR_EXPR, vtbl_ptr_type_node, orig_vtbl);
8904 index = size_binop (MULT_EXPR,
8905 TYPE_SIZE_UNIT (vtable_entry_type),
8906 size_int (non_fn_entries + n_inits));
8907 vtbl = fold_build_pointer_plus (vtbl, index);
8910 if (ctor_vtbl_p)
8911 /* For a construction vtable, we can't overwrite BINFO_VTABLE.
8912 So, we make a TREE_LIST. Later, dfs_fixup_binfo_vtbls will
8913 straighten this out. */
8914 BINFO_VTABLE (binfo) = tree_cons (rtti_binfo, vtbl, BINFO_VTABLE (binfo));
8915 else if (BINFO_PRIMARY_P (binfo) && BINFO_VIRTUAL_P (binfo))
8916 /* Throw away any unneeded intializers. */
8917 (*l)->truncate (n_inits);
8918 else
8919 /* For an ordinary vtable, set BINFO_VTABLE. */
8920 BINFO_VTABLE (binfo) = vtbl;
8923 static GTY(()) tree abort_fndecl_addr;
8925 /* Construct the initializer for BINFO's virtual function table. BINFO
8926 is part of the hierarchy dominated by T. If we're building a
8927 construction vtable, the ORIG_BINFO is the binfo we should use to
8928 find the actual function pointers to put in the vtable - but they
8929 can be overridden on the path to most-derived in the graph that
8930 ORIG_BINFO belongs. Otherwise,
8931 ORIG_BINFO should be the same as BINFO. The RTTI_BINFO is the
8932 BINFO that should be indicated by the RTTI information in the
8933 vtable; it will be a base class of T, rather than T itself, if we
8934 are building a construction vtable.
8936 The value returned is a TREE_LIST suitable for wrapping in a
8937 CONSTRUCTOR to use as the DECL_INITIAL for a vtable. If
8938 NON_FN_ENTRIES_P is not NULL, *NON_FN_ENTRIES_P is set to the
8939 number of non-function entries in the vtable.
8941 It might seem that this function should never be called with a
8942 BINFO for which BINFO_PRIMARY_P holds, the vtable for such a
8943 base is always subsumed by a derived class vtable. However, when
8944 we are building construction vtables, we do build vtables for
8945 primary bases; we need these while the primary base is being
8946 constructed. */
8948 static void
8949 build_vtbl_initializer (tree binfo,
8950 tree orig_binfo,
8951 tree t,
8952 tree rtti_binfo,
8953 int* non_fn_entries_p,
8954 vec<constructor_elt, va_gc> **inits)
8956 tree v;
8957 vtbl_init_data vid;
8958 unsigned ix, jx;
8959 tree vbinfo;
8960 vec<tree, va_gc> *vbases;
8961 constructor_elt *e;
8963 /* Initialize VID. */
8964 memset (&vid, 0, sizeof (vid));
8965 vid.binfo = binfo;
8966 vid.derived = t;
8967 vid.rtti_binfo = rtti_binfo;
8968 vid.primary_vtbl_p = SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), t);
8969 vid.ctor_vtbl_p = !SAME_BINFO_TYPE_P (BINFO_TYPE (rtti_binfo), t);
8970 vid.generate_vcall_entries = true;
8971 /* The first vbase or vcall offset is at index -3 in the vtable. */
8972 vid.index = ssize_int(-3 * TARGET_VTABLE_DATA_ENTRY_DISTANCE);
8974 /* Add entries to the vtable for RTTI. */
8975 build_rtti_vtbl_entries (binfo, &vid);
8977 /* Create an array for keeping track of the functions we've
8978 processed. When we see multiple functions with the same
8979 signature, we share the vcall offsets. */
8980 vec_alloc (vid.fns, 32);
8981 /* Add the vcall and vbase offset entries. */
8982 build_vcall_and_vbase_vtbl_entries (binfo, &vid);
8984 /* Clear BINFO_VTABLE_PATH_MARKED; it's set by
8985 build_vbase_offset_vtbl_entries. */
8986 for (vbases = CLASSTYPE_VBASECLASSES (t), ix = 0;
8987 vec_safe_iterate (vbases, ix, &vbinfo); ix++)
8988 BINFO_VTABLE_PATH_MARKED (vbinfo) = 0;
8990 /* If the target requires padding between data entries, add that now. */
8991 if (TARGET_VTABLE_DATA_ENTRY_DISTANCE > 1)
8993 int n_entries = vec_safe_length (vid.inits);
8995 vec_safe_grow (vid.inits, TARGET_VTABLE_DATA_ENTRY_DISTANCE * n_entries);
8997 /* Move data entries into their new positions and add padding
8998 after the new positions. Iterate backwards so we don't
8999 overwrite entries that we would need to process later. */
9000 for (ix = n_entries - 1;
9001 vid.inits->iterate (ix, &e);
9002 ix--)
9004 int j;
9005 int new_position = (TARGET_VTABLE_DATA_ENTRY_DISTANCE * ix
9006 + (TARGET_VTABLE_DATA_ENTRY_DISTANCE - 1));
9008 (*vid.inits)[new_position] = *e;
9010 for (j = 1; j < TARGET_VTABLE_DATA_ENTRY_DISTANCE; ++j)
9012 constructor_elt *f = &(*vid.inits)[new_position - j];
9013 f->index = NULL_TREE;
9014 f->value = build1 (NOP_EXPR, vtable_entry_type,
9015 null_pointer_node);
9020 if (non_fn_entries_p)
9021 *non_fn_entries_p = vec_safe_length (vid.inits);
9023 /* The initializers for virtual functions were built up in reverse
9024 order. Straighten them out and add them to the running list in one
9025 step. */
9026 jx = vec_safe_length (*inits);
9027 vec_safe_grow (*inits, jx + vid.inits->length ());
9029 for (ix = vid.inits->length () - 1;
9030 vid.inits->iterate (ix, &e);
9031 ix--, jx++)
9032 (**inits)[jx] = *e;
9034 /* Go through all the ordinary virtual functions, building up
9035 initializers. */
9036 for (v = BINFO_VIRTUALS (orig_binfo); v; v = TREE_CHAIN (v))
9038 tree delta;
9039 tree vcall_index;
9040 tree fn, fn_original;
9041 tree init = NULL_TREE;
9043 fn = BV_FN (v);
9044 fn_original = fn;
9045 if (DECL_THUNK_P (fn))
9047 if (!DECL_NAME (fn))
9048 finish_thunk (fn);
9049 if (THUNK_ALIAS (fn))
9051 fn = THUNK_ALIAS (fn);
9052 BV_FN (v) = fn;
9054 fn_original = THUNK_TARGET (fn);
9057 /* If the only definition of this function signature along our
9058 primary base chain is from a lost primary, this vtable slot will
9059 never be used, so just zero it out. This is important to avoid
9060 requiring extra thunks which cannot be generated with the function.
9062 We first check this in update_vtable_entry_for_fn, so we handle
9063 restored primary bases properly; we also need to do it here so we
9064 zero out unused slots in ctor vtables, rather than filling them
9065 with erroneous values (though harmless, apart from relocation
9066 costs). */
9067 if (BV_LOST_PRIMARY (v))
9068 init = size_zero_node;
9070 if (! init)
9072 /* Pull the offset for `this', and the function to call, out of
9073 the list. */
9074 delta = BV_DELTA (v);
9075 vcall_index = BV_VCALL_INDEX (v);
9077 gcc_assert (TREE_CODE (delta) == INTEGER_CST);
9078 gcc_assert (TREE_CODE (fn) == FUNCTION_DECL);
9080 /* You can't call an abstract virtual function; it's abstract.
9081 So, we replace these functions with __pure_virtual. */
9082 if (DECL_PURE_VIRTUAL_P (fn_original))
9084 fn = abort_fndecl;
9085 if (!TARGET_VTABLE_USES_DESCRIPTORS)
9087 if (abort_fndecl_addr == NULL)
9088 abort_fndecl_addr
9089 = fold_convert (vfunc_ptr_type_node,
9090 build_fold_addr_expr (fn));
9091 init = abort_fndecl_addr;
9094 /* Likewise for deleted virtuals. */
9095 else if (DECL_DELETED_FN (fn_original))
9097 fn = get_identifier ("__cxa_deleted_virtual");
9098 if (!get_global_value_if_present (fn, &fn))
9099 fn = push_library_fn (fn, (build_function_type_list
9100 (void_type_node, NULL_TREE)),
9101 NULL_TREE, ECF_NORETURN);
9102 if (!TARGET_VTABLE_USES_DESCRIPTORS)
9103 init = fold_convert (vfunc_ptr_type_node,
9104 build_fold_addr_expr (fn));
9106 else
9108 if (!integer_zerop (delta) || vcall_index)
9110 fn = make_thunk (fn, /*this_adjusting=*/1, delta, vcall_index);
9111 if (!DECL_NAME (fn))
9112 finish_thunk (fn);
9114 /* Take the address of the function, considering it to be of an
9115 appropriate generic type. */
9116 if (!TARGET_VTABLE_USES_DESCRIPTORS)
9117 init = fold_convert (vfunc_ptr_type_node,
9118 build_fold_addr_expr (fn));
9119 /* Don't refer to a virtual destructor from a constructor
9120 vtable or a vtable for an abstract class, since destroying
9121 an object under construction is undefined behavior and we
9122 don't want it to be considered a candidate for speculative
9123 devirtualization. But do create the thunk for ABI
9124 compliance. */
9125 if (DECL_DESTRUCTOR_P (fn_original)
9126 && (CLASSTYPE_PURE_VIRTUALS (DECL_CONTEXT (fn_original))
9127 || orig_binfo != binfo))
9128 init = size_zero_node;
9132 /* And add it to the chain of initializers. */
9133 if (TARGET_VTABLE_USES_DESCRIPTORS)
9135 int i;
9136 if (init == size_zero_node)
9137 for (i = 0; i < TARGET_VTABLE_USES_DESCRIPTORS; ++i)
9138 CONSTRUCTOR_APPEND_ELT (*inits, NULL_TREE, init);
9139 else
9140 for (i = 0; i < TARGET_VTABLE_USES_DESCRIPTORS; ++i)
9142 tree fdesc = build2 (FDESC_EXPR, vfunc_ptr_type_node,
9143 fn, build_int_cst (NULL_TREE, i));
9144 TREE_CONSTANT (fdesc) = 1;
9146 CONSTRUCTOR_APPEND_ELT (*inits, NULL_TREE, fdesc);
9149 else
9150 CONSTRUCTOR_APPEND_ELT (*inits, NULL_TREE, init);
9154 /* Adds to vid->inits the initializers for the vbase and vcall
9155 offsets in BINFO, which is in the hierarchy dominated by T. */
9157 static void
9158 build_vcall_and_vbase_vtbl_entries (tree binfo, vtbl_init_data* vid)
9160 tree b;
9162 /* If this is a derived class, we must first create entries
9163 corresponding to the primary base class. */
9164 b = get_primary_binfo (binfo);
9165 if (b)
9166 build_vcall_and_vbase_vtbl_entries (b, vid);
9168 /* Add the vbase entries for this base. */
9169 build_vbase_offset_vtbl_entries (binfo, vid);
9170 /* Add the vcall entries for this base. */
9171 build_vcall_offset_vtbl_entries (binfo, vid);
9174 /* Returns the initializers for the vbase offset entries in the vtable
9175 for BINFO (which is part of the class hierarchy dominated by T), in
9176 reverse order. VBASE_OFFSET_INDEX gives the vtable index
9177 where the next vbase offset will go. */
9179 static void
9180 build_vbase_offset_vtbl_entries (tree binfo, vtbl_init_data* vid)
9182 tree vbase;
9183 tree t;
9184 tree non_primary_binfo;
9186 /* If there are no virtual baseclasses, then there is nothing to
9187 do. */
9188 if (!CLASSTYPE_VBASECLASSES (BINFO_TYPE (binfo)))
9189 return;
9191 t = vid->derived;
9193 /* We might be a primary base class. Go up the inheritance hierarchy
9194 until we find the most derived class of which we are a primary base:
9195 it is the offset of that which we need to use. */
9196 non_primary_binfo = binfo;
9197 while (BINFO_INHERITANCE_CHAIN (non_primary_binfo))
9199 tree b;
9201 /* If we have reached a virtual base, then it must be a primary
9202 base (possibly multi-level) of vid->binfo, or we wouldn't
9203 have called build_vcall_and_vbase_vtbl_entries for it. But it
9204 might be a lost primary, so just skip down to vid->binfo. */
9205 if (BINFO_VIRTUAL_P (non_primary_binfo))
9207 non_primary_binfo = vid->binfo;
9208 break;
9211 b = BINFO_INHERITANCE_CHAIN (non_primary_binfo);
9212 if (get_primary_binfo (b) != non_primary_binfo)
9213 break;
9214 non_primary_binfo = b;
9217 /* Go through the virtual bases, adding the offsets. */
9218 for (vbase = TYPE_BINFO (BINFO_TYPE (binfo));
9219 vbase;
9220 vbase = TREE_CHAIN (vbase))
9222 tree b;
9223 tree delta;
9225 if (!BINFO_VIRTUAL_P (vbase))
9226 continue;
9228 /* Find the instance of this virtual base in the complete
9229 object. */
9230 b = copied_binfo (vbase, binfo);
9232 /* If we've already got an offset for this virtual base, we
9233 don't need another one. */
9234 if (BINFO_VTABLE_PATH_MARKED (b))
9235 continue;
9236 BINFO_VTABLE_PATH_MARKED (b) = 1;
9238 /* Figure out where we can find this vbase offset. */
9239 delta = size_binop (MULT_EXPR,
9240 vid->index,
9241 convert (ssizetype,
9242 TYPE_SIZE_UNIT (vtable_entry_type)));
9243 if (vid->primary_vtbl_p)
9244 BINFO_VPTR_FIELD (b) = delta;
9246 if (binfo != TYPE_BINFO (t))
9247 /* The vbase offset had better be the same. */
9248 gcc_assert (tree_int_cst_equal (delta, BINFO_VPTR_FIELD (vbase)));
9250 /* The next vbase will come at a more negative offset. */
9251 vid->index = size_binop (MINUS_EXPR, vid->index,
9252 ssize_int (TARGET_VTABLE_DATA_ENTRY_DISTANCE));
9254 /* The initializer is the delta from BINFO to this virtual base.
9255 The vbase offsets go in reverse inheritance-graph order, and
9256 we are walking in inheritance graph order so these end up in
9257 the right order. */
9258 delta = size_diffop_loc (input_location,
9259 BINFO_OFFSET (b), BINFO_OFFSET (non_primary_binfo));
9261 CONSTRUCTOR_APPEND_ELT (vid->inits, NULL_TREE,
9262 fold_build1_loc (input_location, NOP_EXPR,
9263 vtable_entry_type, delta));
9267 /* Adds the initializers for the vcall offset entries in the vtable
9268 for BINFO (which is part of the class hierarchy dominated by VID->DERIVED)
9269 to VID->INITS. */
9271 static void
9272 build_vcall_offset_vtbl_entries (tree binfo, vtbl_init_data* vid)
9274 /* We only need these entries if this base is a virtual base. We
9275 compute the indices -- but do not add to the vtable -- when
9276 building the main vtable for a class. */
9277 if (binfo == TYPE_BINFO (vid->derived)
9278 || (BINFO_VIRTUAL_P (binfo)
9279 /* If BINFO is RTTI_BINFO, then (since BINFO does not
9280 correspond to VID->DERIVED), we are building a primary
9281 construction virtual table. Since this is a primary
9282 virtual table, we do not need the vcall offsets for
9283 BINFO. */
9284 && binfo != vid->rtti_binfo))
9286 /* We need a vcall offset for each of the virtual functions in this
9287 vtable. For example:
9289 class A { virtual void f (); };
9290 class B1 : virtual public A { virtual void f (); };
9291 class B2 : virtual public A { virtual void f (); };
9292 class C: public B1, public B2 { virtual void f (); };
9294 A C object has a primary base of B1, which has a primary base of A. A
9295 C also has a secondary base of B2, which no longer has a primary base
9296 of A. So the B2-in-C construction vtable needs a secondary vtable for
9297 A, which will adjust the A* to a B2* to call f. We have no way of
9298 knowing what (or even whether) this offset will be when we define B2,
9299 so we store this "vcall offset" in the A sub-vtable and look it up in
9300 a "virtual thunk" for B2::f.
9302 We need entries for all the functions in our primary vtable and
9303 in our non-virtual bases' secondary vtables. */
9304 vid->vbase = binfo;
9305 /* If we are just computing the vcall indices -- but do not need
9306 the actual entries -- not that. */
9307 if (!BINFO_VIRTUAL_P (binfo))
9308 vid->generate_vcall_entries = false;
9309 /* Now, walk through the non-virtual bases, adding vcall offsets. */
9310 add_vcall_offset_vtbl_entries_r (binfo, vid);
9314 /* Build vcall offsets, starting with those for BINFO. */
9316 static void
9317 add_vcall_offset_vtbl_entries_r (tree binfo, vtbl_init_data* vid)
9319 int i;
9320 tree primary_binfo;
9321 tree base_binfo;
9323 /* Don't walk into virtual bases -- except, of course, for the
9324 virtual base for which we are building vcall offsets. Any
9325 primary virtual base will have already had its offsets generated
9326 through the recursion in build_vcall_and_vbase_vtbl_entries. */
9327 if (BINFO_VIRTUAL_P (binfo) && vid->vbase != binfo)
9328 return;
9330 /* If BINFO has a primary base, process it first. */
9331 primary_binfo = get_primary_binfo (binfo);
9332 if (primary_binfo)
9333 add_vcall_offset_vtbl_entries_r (primary_binfo, vid);
9335 /* Add BINFO itself to the list. */
9336 add_vcall_offset_vtbl_entries_1 (binfo, vid);
9338 /* Scan the non-primary bases of BINFO. */
9339 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
9340 if (base_binfo != primary_binfo)
9341 add_vcall_offset_vtbl_entries_r (base_binfo, vid);
9344 /* Called from build_vcall_offset_vtbl_entries_r. */
9346 static void
9347 add_vcall_offset_vtbl_entries_1 (tree binfo, vtbl_init_data* vid)
9349 /* Make entries for the rest of the virtuals. */
9350 tree orig_fn;
9352 /* The ABI requires that the methods be processed in declaration
9353 order. */
9354 for (orig_fn = TYPE_METHODS (BINFO_TYPE (binfo));
9355 orig_fn;
9356 orig_fn = DECL_CHAIN (orig_fn))
9357 if (TREE_CODE (orig_fn) == FUNCTION_DECL && DECL_VINDEX (orig_fn))
9358 add_vcall_offset (orig_fn, binfo, vid);
9361 /* Add a vcall offset entry for ORIG_FN to the vtable. */
9363 static void
9364 add_vcall_offset (tree orig_fn, tree binfo, vtbl_init_data *vid)
9366 size_t i;
9367 tree vcall_offset;
9368 tree derived_entry;
9370 /* If there is already an entry for a function with the same
9371 signature as FN, then we do not need a second vcall offset.
9372 Check the list of functions already present in the derived
9373 class vtable. */
9374 FOR_EACH_VEC_SAFE_ELT (vid->fns, i, derived_entry)
9376 if (same_signature_p (derived_entry, orig_fn)
9377 /* We only use one vcall offset for virtual destructors,
9378 even though there are two virtual table entries. */
9379 || (DECL_DESTRUCTOR_P (derived_entry)
9380 && DECL_DESTRUCTOR_P (orig_fn)))
9381 return;
9384 /* If we are building these vcall offsets as part of building
9385 the vtable for the most derived class, remember the vcall
9386 offset. */
9387 if (vid->binfo == TYPE_BINFO (vid->derived))
9389 tree_pair_s elt = {orig_fn, vid->index};
9390 vec_safe_push (CLASSTYPE_VCALL_INDICES (vid->derived), elt);
9393 /* The next vcall offset will be found at a more negative
9394 offset. */
9395 vid->index = size_binop (MINUS_EXPR, vid->index,
9396 ssize_int (TARGET_VTABLE_DATA_ENTRY_DISTANCE));
9398 /* Keep track of this function. */
9399 vec_safe_push (vid->fns, orig_fn);
9401 if (vid->generate_vcall_entries)
9403 tree base;
9404 tree fn;
9406 /* Find the overriding function. */
9407 fn = find_final_overrider (vid->rtti_binfo, binfo, orig_fn);
9408 if (fn == error_mark_node)
9409 vcall_offset = build_zero_cst (vtable_entry_type);
9410 else
9412 base = TREE_VALUE (fn);
9414 /* The vbase we're working on is a primary base of
9415 vid->binfo. But it might be a lost primary, so its
9416 BINFO_OFFSET might be wrong, so we just use the
9417 BINFO_OFFSET from vid->binfo. */
9418 vcall_offset = size_diffop_loc (input_location,
9419 BINFO_OFFSET (base),
9420 BINFO_OFFSET (vid->binfo));
9421 vcall_offset = fold_build1_loc (input_location,
9422 NOP_EXPR, vtable_entry_type,
9423 vcall_offset);
9425 /* Add the initializer to the vtable. */
9426 CONSTRUCTOR_APPEND_ELT (vid->inits, NULL_TREE, vcall_offset);
9430 /* Return vtbl initializers for the RTTI entries corresponding to the
9431 BINFO's vtable. The RTTI entries should indicate the object given
9432 by VID->rtti_binfo. */
9434 static void
9435 build_rtti_vtbl_entries (tree binfo, vtbl_init_data* vid)
9437 tree b;
9438 tree t;
9439 tree offset;
9440 tree decl;
9441 tree init;
9443 t = BINFO_TYPE (vid->rtti_binfo);
9445 /* To find the complete object, we will first convert to our most
9446 primary base, and then add the offset in the vtbl to that value. */
9447 b = binfo;
9448 while (CLASSTYPE_HAS_PRIMARY_BASE_P (BINFO_TYPE (b))
9449 && !BINFO_LOST_PRIMARY_P (b))
9451 tree primary_base;
9453 primary_base = get_primary_binfo (b);
9454 gcc_assert (BINFO_PRIMARY_P (primary_base)
9455 && BINFO_INHERITANCE_CHAIN (primary_base) == b);
9456 b = primary_base;
9458 offset = size_diffop_loc (input_location,
9459 BINFO_OFFSET (vid->rtti_binfo), BINFO_OFFSET (b));
9461 /* The second entry is the address of the typeinfo object. */
9462 if (flag_rtti)
9463 decl = build_address (get_tinfo_decl (t));
9464 else
9465 decl = integer_zero_node;
9467 /* Convert the declaration to a type that can be stored in the
9468 vtable. */
9469 init = build_nop (vfunc_ptr_type_node, decl);
9470 CONSTRUCTOR_APPEND_ELT (vid->inits, NULL_TREE, init);
9472 /* Add the offset-to-top entry. It comes earlier in the vtable than
9473 the typeinfo entry. Convert the offset to look like a
9474 function pointer, so that we can put it in the vtable. */
9475 init = build_nop (vfunc_ptr_type_node, offset);
9476 CONSTRUCTOR_APPEND_ELT (vid->inits, NULL_TREE, init);
9479 /* TRUE iff TYPE is uniquely derived from PARENT. Ignores
9480 accessibility. */
9482 bool
9483 uniquely_derived_from_p (tree parent, tree type)
9485 tree base = lookup_base (type, parent, ba_unique, NULL, tf_none);
9486 return base && base != error_mark_node;
9489 /* TRUE iff TYPE is publicly & uniquely derived from PARENT. */
9491 bool
9492 publicly_uniquely_derived_p (tree parent, tree type)
9494 tree base = lookup_base (type, parent, ba_ignore_scope | ba_check,
9495 NULL, tf_none);
9496 return base && base != error_mark_node;
9499 /* CTX1 and CTX2 are declaration contexts. Return the innermost common
9500 class between them, if any. */
9502 tree
9503 common_enclosing_class (tree ctx1, tree ctx2)
9505 if (!TYPE_P (ctx1) || !TYPE_P (ctx2))
9506 return NULL_TREE;
9507 gcc_assert (ctx1 == TYPE_MAIN_VARIANT (ctx1)
9508 && ctx2 == TYPE_MAIN_VARIANT (ctx2));
9509 if (ctx1 == ctx2)
9510 return ctx1;
9511 for (tree t = ctx1; TYPE_P (t); t = TYPE_CONTEXT (t))
9512 TYPE_MARKED_P (t) = true;
9513 tree found = NULL_TREE;
9514 for (tree t = ctx2; TYPE_P (t); t = TYPE_CONTEXT (t))
9515 if (TYPE_MARKED_P (t))
9517 found = t;
9518 break;
9520 for (tree t = ctx1; TYPE_P (t); t = TYPE_CONTEXT (t))
9521 TYPE_MARKED_P (t) = false;
9522 return found;
9525 #include "gt-cp-class.h"