Reverting merge from trunk
[official-gcc.git] / gcc / cp / class.c
blob18ce2f86e0a47b4f7af20bbc52e61e06a1eff3a2
1 /* Functions related to building classes and their related objects.
2 Copyright (C) 1987-2013 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 "tm.h"
28 #include "tree.h"
29 #include "cp-tree.h"
30 #include "flags.h"
31 #include "toplev.h"
32 #include "target.h"
33 #include "convert.h"
34 #include "cgraph.h"
35 #include "dumpfile.h"
36 #include "splay-tree.h"
37 #include "pointer-set.h"
38 #include "hash-table.h"
39 #include "gimple.h"
40 #include "gimplify.h"
42 /* The number of nested classes being processed. If we are not in the
43 scope of any class, this is zero. */
45 int current_class_depth;
47 /* In order to deal with nested classes, we keep a stack of classes.
48 The topmost entry is the innermost class, and is the entry at index
49 CURRENT_CLASS_DEPTH */
51 typedef struct class_stack_node {
52 /* The name of the class. */
53 tree name;
55 /* The _TYPE node for the class. */
56 tree type;
58 /* The access specifier pending for new declarations in the scope of
59 this class. */
60 tree access;
62 /* If were defining TYPE, the names used in this class. */
63 splay_tree names_used;
65 /* Nonzero if this class is no longer open, because of a call to
66 push_to_top_level. */
67 size_t hidden;
68 }* class_stack_node_t;
70 typedef struct vtbl_init_data_s
72 /* The base for which we're building initializers. */
73 tree binfo;
74 /* The type of the most-derived type. */
75 tree derived;
76 /* The binfo for the dynamic type. This will be TYPE_BINFO (derived),
77 unless ctor_vtbl_p is true. */
78 tree rtti_binfo;
79 /* The negative-index vtable initializers built up so far. These
80 are in order from least negative index to most negative index. */
81 vec<constructor_elt, va_gc> *inits;
82 /* The binfo for the virtual base for which we're building
83 vcall offset initializers. */
84 tree vbase;
85 /* The functions in vbase for which we have already provided vcall
86 offsets. */
87 vec<tree, va_gc> *fns;
88 /* The vtable index of the next vcall or vbase offset. */
89 tree index;
90 /* Nonzero if we are building the initializer for the primary
91 vtable. */
92 int primary_vtbl_p;
93 /* Nonzero if we are building the initializer for a construction
94 vtable. */
95 int ctor_vtbl_p;
96 /* True when adding vcall offset entries to the vtable. False when
97 merely computing the indices. */
98 bool generate_vcall_entries;
99 } vtbl_init_data;
101 /* The type of a function passed to walk_subobject_offsets. */
102 typedef int (*subobject_offset_fn) (tree, tree, splay_tree);
104 /* The stack itself. This is a dynamically resized array. The
105 number of elements allocated is CURRENT_CLASS_STACK_SIZE. */
106 static int current_class_stack_size;
107 static class_stack_node_t current_class_stack;
109 /* The size of the largest empty class seen in this translation unit. */
110 static GTY (()) tree sizeof_biggest_empty_class;
112 /* An array of all local classes present in this translation unit, in
113 declaration order. */
114 vec<tree, va_gc> *local_classes;
116 static tree get_vfield_name (tree);
117 static void finish_struct_anon (tree);
118 static tree get_vtable_name (tree);
119 static tree get_basefndecls (tree, tree);
120 static int build_primary_vtable (tree, tree);
121 static int build_secondary_vtable (tree);
122 static void finish_vtbls (tree);
123 static void modify_vtable_entry (tree, tree, tree, tree, tree *);
124 static void finish_struct_bits (tree);
125 static int alter_access (tree, tree, tree);
126 static void handle_using_decl (tree, tree);
127 static tree dfs_modify_vtables (tree, void *);
128 static tree modify_all_vtables (tree, tree);
129 static void determine_primary_bases (tree);
130 static void finish_struct_methods (tree);
131 static void maybe_warn_about_overly_private_class (tree);
132 static int method_name_cmp (const void *, const void *);
133 static int resort_method_name_cmp (const void *, const void *);
134 static void add_implicitly_declared_members (tree, tree*, int, int);
135 static tree fixed_type_or_null (tree, int *, int *);
136 static tree build_simple_base_path (tree expr, tree binfo);
137 static tree build_vtbl_ref_1 (tree, tree);
138 static void build_vtbl_initializer (tree, tree, tree, tree, int *,
139 vec<constructor_elt, va_gc> **);
140 static int count_fields (tree);
141 static int add_fields_to_record_type (tree, struct sorted_fields_type*, int);
142 static void insert_into_classtype_sorted_fields (tree, tree, int);
143 static bool check_bitfield_decl (tree);
144 static void check_field_decl (tree, tree, int *, int *, int *);
145 static void check_field_decls (tree, tree *, int *, int *);
146 static tree *build_base_field (record_layout_info, tree, splay_tree, tree *);
147 static void build_base_fields (record_layout_info, splay_tree, tree *);
148 static void check_methods (tree);
149 static void remove_zero_width_bit_fields (tree);
150 static void check_bases (tree, int *, int *);
151 static void check_bases_and_members (tree);
152 static tree create_vtable_ptr (tree, tree *);
153 static void include_empty_classes (record_layout_info);
154 static void layout_class_type (tree, tree *);
155 static void propagate_binfo_offsets (tree, tree);
156 static void layout_virtual_bases (record_layout_info, splay_tree);
157 static void build_vbase_offset_vtbl_entries (tree, vtbl_init_data *);
158 static void add_vcall_offset_vtbl_entries_r (tree, vtbl_init_data *);
159 static void add_vcall_offset_vtbl_entries_1 (tree, vtbl_init_data *);
160 static void build_vcall_offset_vtbl_entries (tree, vtbl_init_data *);
161 static void add_vcall_offset (tree, tree, vtbl_init_data *);
162 static void layout_vtable_decl (tree, int);
163 static tree dfs_find_final_overrider_pre (tree, void *);
164 static tree dfs_find_final_overrider_post (tree, void *);
165 static tree find_final_overrider (tree, tree, tree);
166 static int make_new_vtable (tree, tree);
167 static tree get_primary_binfo (tree);
168 static int maybe_indent_hierarchy (FILE *, int, int);
169 static tree dump_class_hierarchy_r (FILE *, int, tree, tree, int);
170 static void dump_class_hierarchy (tree);
171 static void dump_class_hierarchy_1 (FILE *, int, tree);
172 static void dump_array (FILE *, tree);
173 static void dump_vtable (tree, tree, tree);
174 static void dump_vtt (tree, tree);
175 static void dump_thunk (FILE *, int, tree);
176 static tree build_vtable (tree, tree, tree);
177 static void initialize_vtable (tree, vec<constructor_elt, va_gc> *);
178 static void layout_nonempty_base_or_field (record_layout_info,
179 tree, tree, splay_tree);
180 static tree end_of_class (tree, int);
181 static bool layout_empty_base (record_layout_info, tree, tree, splay_tree);
182 static void accumulate_vtbl_inits (tree, tree, tree, tree, tree,
183 vec<constructor_elt, va_gc> **);
184 static void dfs_accumulate_vtbl_inits (tree, tree, tree, tree, tree,
185 vec<constructor_elt, va_gc> **);
186 static void build_rtti_vtbl_entries (tree, vtbl_init_data *);
187 static void build_vcall_and_vbase_vtbl_entries (tree, vtbl_init_data *);
188 static void clone_constructors_and_destructors (tree);
189 static tree build_clone (tree, tree);
190 static void update_vtable_entry_for_fn (tree, tree, tree, tree *, unsigned);
191 static void build_ctor_vtbl_group (tree, tree);
192 static void build_vtt (tree);
193 static tree binfo_ctor_vtable (tree);
194 static void build_vtt_inits (tree, tree, vec<constructor_elt, va_gc> **,
195 tree *);
196 static tree dfs_build_secondary_vptr_vtt_inits (tree, void *);
197 static tree dfs_fixup_binfo_vtbls (tree, void *);
198 static int record_subobject_offset (tree, tree, splay_tree);
199 static int check_subobject_offset (tree, tree, splay_tree);
200 static int walk_subobject_offsets (tree, subobject_offset_fn,
201 tree, splay_tree, tree, int);
202 static void record_subobject_offsets (tree, tree, splay_tree, bool);
203 static int layout_conflict_p (tree, tree, splay_tree, int);
204 static int splay_tree_compare_integer_csts (splay_tree_key k1,
205 splay_tree_key k2);
206 static void warn_about_ambiguous_bases (tree);
207 static bool type_requires_array_cookie (tree);
208 static bool contains_empty_class_p (tree);
209 static bool base_derived_from (tree, tree);
210 static int empty_base_at_nonzero_offset_p (tree, tree, splay_tree);
211 static tree end_of_base (tree);
212 static tree get_vcall_index (tree, tree);
214 /* Variables shared between class.c and call.c. */
216 int n_vtables = 0;
217 int n_vtable_entries = 0;
218 int n_vtable_searches = 0;
219 int n_vtable_elems = 0;
220 int n_convert_harshness = 0;
221 int n_compute_conversion_costs = 0;
222 int n_inner_fields_searched = 0;
224 /* Convert to or from a base subobject. EXPR is an expression of type
225 `A' or `A*', an expression of type `B' or `B*' is returned. To
226 convert A to a base B, CODE is PLUS_EXPR and BINFO is the binfo for
227 the B base instance within A. To convert base A to derived B, CODE
228 is MINUS_EXPR and BINFO is the binfo for the A instance within B.
229 In this latter case, A must not be a morally virtual base of B.
230 NONNULL is true if EXPR is known to be non-NULL (this is only
231 needed when EXPR is of pointer type). CV qualifiers are preserved
232 from EXPR. */
234 tree
235 build_base_path (enum tree_code code,
236 tree expr,
237 tree binfo,
238 int nonnull,
239 tsubst_flags_t complain)
241 tree v_binfo = NULL_TREE;
242 tree d_binfo = NULL_TREE;
243 tree probe;
244 tree offset;
245 tree target_type;
246 tree null_test = NULL;
247 tree ptr_target_type;
248 int fixed_type_p;
249 int want_pointer = TYPE_PTR_P (TREE_TYPE (expr));
250 bool has_empty = false;
251 bool virtual_access;
253 if (expr == error_mark_node || binfo == error_mark_node || !binfo)
254 return error_mark_node;
256 for (probe = binfo; probe; probe = BINFO_INHERITANCE_CHAIN (probe))
258 d_binfo = probe;
259 if (is_empty_class (BINFO_TYPE (probe)))
260 has_empty = true;
261 if (!v_binfo && BINFO_VIRTUAL_P (probe))
262 v_binfo = probe;
265 probe = TYPE_MAIN_VARIANT (TREE_TYPE (expr));
266 if (want_pointer)
267 probe = TYPE_MAIN_VARIANT (TREE_TYPE (probe));
269 if (code == PLUS_EXPR
270 && !SAME_BINFO_TYPE_P (BINFO_TYPE (d_binfo), probe))
272 /* This can happen when adjust_result_of_qualified_name_lookup can't
273 find a unique base binfo in a call to a member function. We
274 couldn't give the diagnostic then since we might have been calling
275 a static member function, so we do it now. */
276 if (complain & tf_error)
278 tree base = lookup_base (probe, BINFO_TYPE (d_binfo),
279 ba_unique, NULL, complain);
280 gcc_assert (base == error_mark_node);
282 return error_mark_node;
285 gcc_assert ((code == MINUS_EXPR
286 && SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), probe))
287 || code == PLUS_EXPR);
289 if (binfo == d_binfo)
290 /* Nothing to do. */
291 return expr;
293 if (code == MINUS_EXPR && v_binfo)
295 if (complain & tf_error)
297 if (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), BINFO_TYPE (v_binfo)))
299 if (want_pointer)
300 error ("cannot convert from pointer to base class %qT to "
301 "pointer to derived class %qT because the base is "
302 "virtual", BINFO_TYPE (binfo), BINFO_TYPE (d_binfo));
303 else
304 error ("cannot convert from base class %qT to derived "
305 "class %qT because the base is virtual",
306 BINFO_TYPE (binfo), BINFO_TYPE (d_binfo));
308 else
310 if (want_pointer)
311 error ("cannot convert from pointer to base class %qT to "
312 "pointer to derived class %qT via virtual base %qT",
313 BINFO_TYPE (binfo), BINFO_TYPE (d_binfo),
314 BINFO_TYPE (v_binfo));
315 else
316 error ("cannot convert from base class %qT to derived "
317 "class %qT via virtual base %qT", BINFO_TYPE (binfo),
318 BINFO_TYPE (d_binfo), BINFO_TYPE (v_binfo));
321 return error_mark_node;
324 if (!want_pointer)
325 /* This must happen before the call to save_expr. */
326 expr = cp_build_addr_expr (expr, complain);
327 else
328 expr = mark_rvalue_use (expr);
330 offset = BINFO_OFFSET (binfo);
331 fixed_type_p = resolves_to_fixed_type_p (expr, &nonnull);
332 target_type = code == PLUS_EXPR ? BINFO_TYPE (binfo) : BINFO_TYPE (d_binfo);
333 /* TARGET_TYPE has been extracted from BINFO, and, is therefore always
334 cv-unqualified. Extract the cv-qualifiers from EXPR so that the
335 expression returned matches the input. */
336 target_type = cp_build_qualified_type
337 (target_type, cp_type_quals (TREE_TYPE (TREE_TYPE (expr))));
338 ptr_target_type = build_pointer_type (target_type);
340 /* Do we need to look in the vtable for the real offset? */
341 virtual_access = (v_binfo && fixed_type_p <= 0);
343 /* Don't bother with the calculations inside sizeof; they'll ICE if the
344 source type is incomplete and the pointer value doesn't matter. In a
345 template (even in fold_non_dependent_expr), we don't have vtables set
346 up properly yet, and the value doesn't matter there either; we're just
347 interested in the result of overload resolution. */
348 if (cp_unevaluated_operand != 0
349 || in_template_function ())
351 expr = build_nop (ptr_target_type, expr);
352 if (!want_pointer)
353 expr = build_indirect_ref (EXPR_LOCATION (expr), expr, RO_NULL);
354 return expr;
357 /* If we're in an NSDMI, we don't have the full constructor context yet
358 that we need for converting to a virtual base, so just build a stub
359 CONVERT_EXPR and expand it later in bot_replace. */
360 if (virtual_access && fixed_type_p < 0
361 && current_scope () != current_function_decl)
363 expr = build1 (CONVERT_EXPR, ptr_target_type, expr);
364 CONVERT_EXPR_VBASE_PATH (expr) = true;
365 if (!want_pointer)
366 expr = build_indirect_ref (EXPR_LOCATION (expr), expr, RO_NULL);
367 return expr;
370 /* Do we need to check for a null pointer? */
371 if (want_pointer && !nonnull)
373 /* If we know the conversion will not actually change the value
374 of EXPR, then we can avoid testing the expression for NULL.
375 We have to avoid generating a COMPONENT_REF for a base class
376 field, because other parts of the compiler know that such
377 expressions are always non-NULL. */
378 if (!virtual_access && integer_zerop (offset))
379 return build_nop (ptr_target_type, expr);
380 null_test = error_mark_node;
383 /* Protect against multiple evaluation if necessary. */
384 if (TREE_SIDE_EFFECTS (expr) && (null_test || virtual_access))
385 expr = save_expr (expr);
387 /* Now that we've saved expr, build the real null test. */
388 if (null_test)
390 tree zero = cp_convert (TREE_TYPE (expr), nullptr_node, complain);
391 null_test = fold_build2_loc (input_location, NE_EXPR, boolean_type_node,
392 expr, zero);
395 /* If this is a simple base reference, express it as a COMPONENT_REF. */
396 if (code == PLUS_EXPR && !virtual_access
397 /* We don't build base fields for empty bases, and they aren't very
398 interesting to the optimizers anyway. */
399 && !has_empty)
401 expr = cp_build_indirect_ref (expr, RO_NULL, complain);
402 expr = build_simple_base_path (expr, binfo);
403 if (want_pointer)
404 expr = build_address (expr);
405 target_type = TREE_TYPE (expr);
406 goto out;
409 if (virtual_access)
411 /* Going via virtual base V_BINFO. We need the static offset
412 from V_BINFO to BINFO, and the dynamic offset from D_BINFO to
413 V_BINFO. That offset is an entry in D_BINFO's vtable. */
414 tree v_offset;
416 if (fixed_type_p < 0 && in_base_initializer)
418 /* In a base member initializer, we cannot rely on the
419 vtable being set up. We have to indirect via the
420 vtt_parm. */
421 tree t;
423 t = TREE_TYPE (TYPE_VFIELD (current_class_type));
424 t = build_pointer_type (t);
425 v_offset = convert (t, current_vtt_parm);
426 v_offset = cp_build_indirect_ref (v_offset, RO_NULL, complain);
428 else
429 v_offset = build_vfield_ref (cp_build_indirect_ref (expr, RO_NULL,
430 complain),
431 TREE_TYPE (TREE_TYPE (expr)));
433 v_offset = fold_build_pointer_plus (v_offset, BINFO_VPTR_FIELD (v_binfo));
434 v_offset = build1 (NOP_EXPR,
435 build_pointer_type (ptrdiff_type_node),
436 v_offset);
437 v_offset = cp_build_indirect_ref (v_offset, RO_NULL, complain);
438 TREE_CONSTANT (v_offset) = 1;
440 offset = convert_to_integer (ptrdiff_type_node,
441 size_diffop_loc (input_location, offset,
442 BINFO_OFFSET (v_binfo)));
444 if (!integer_zerop (offset))
445 v_offset = build2 (code, ptrdiff_type_node, v_offset, offset);
447 if (fixed_type_p < 0)
448 /* Negative fixed_type_p means this is a constructor or destructor;
449 virtual base layout is fixed in in-charge [cd]tors, but not in
450 base [cd]tors. */
451 offset = build3 (COND_EXPR, ptrdiff_type_node,
452 build2 (EQ_EXPR, boolean_type_node,
453 current_in_charge_parm, integer_zero_node),
454 v_offset,
455 convert_to_integer (ptrdiff_type_node,
456 BINFO_OFFSET (binfo)));
457 else
458 offset = v_offset;
461 if (want_pointer)
462 target_type = ptr_target_type;
464 expr = build1 (NOP_EXPR, ptr_target_type, expr);
466 if (!integer_zerop (offset))
468 offset = fold_convert (sizetype, offset);
469 if (code == MINUS_EXPR)
470 offset = fold_build1_loc (input_location, NEGATE_EXPR, sizetype, offset);
471 expr = fold_build_pointer_plus (expr, offset);
473 else
474 null_test = NULL;
476 if (!want_pointer)
477 expr = cp_build_indirect_ref (expr, RO_NULL, complain);
479 out:
480 if (null_test)
481 expr = fold_build3_loc (input_location, COND_EXPR, target_type, null_test, expr,
482 build_zero_cst (target_type));
484 return expr;
487 /* Subroutine of build_base_path; EXPR and BINFO are as in that function.
488 Perform a derived-to-base conversion by recursively building up a
489 sequence of COMPONENT_REFs to the appropriate base fields. */
491 static tree
492 build_simple_base_path (tree expr, tree binfo)
494 tree type = BINFO_TYPE (binfo);
495 tree d_binfo = BINFO_INHERITANCE_CHAIN (binfo);
496 tree field;
498 if (d_binfo == NULL_TREE)
500 tree temp;
502 gcc_assert (TYPE_MAIN_VARIANT (TREE_TYPE (expr)) == type);
504 /* Transform `(a, b).x' into `(*(a, &b)).x', `(a ? b : c).x'
505 into `(*(a ? &b : &c)).x', and so on. A COND_EXPR is only
506 an lvalue in the front end; only _DECLs and _REFs are lvalues
507 in the back end. */
508 temp = unary_complex_lvalue (ADDR_EXPR, expr);
509 if (temp)
510 expr = cp_build_indirect_ref (temp, RO_NULL, tf_warning_or_error);
512 return expr;
515 /* Recurse. */
516 expr = build_simple_base_path (expr, d_binfo);
518 for (field = TYPE_FIELDS (BINFO_TYPE (d_binfo));
519 field; field = DECL_CHAIN (field))
520 /* Is this the base field created by build_base_field? */
521 if (TREE_CODE (field) == FIELD_DECL
522 && DECL_FIELD_IS_BASE (field)
523 && TREE_TYPE (field) == type
524 /* If we're looking for a field in the most-derived class,
525 also check the field offset; we can have two base fields
526 of the same type if one is an indirect virtual base and one
527 is a direct non-virtual base. */
528 && (BINFO_INHERITANCE_CHAIN (d_binfo)
529 || tree_int_cst_equal (byte_position (field),
530 BINFO_OFFSET (binfo))))
532 /* We don't use build_class_member_access_expr here, as that
533 has unnecessary checks, and more importantly results in
534 recursive calls to dfs_walk_once. */
535 int type_quals = cp_type_quals (TREE_TYPE (expr));
537 expr = build3 (COMPONENT_REF,
538 cp_build_qualified_type (type, type_quals),
539 expr, field, NULL_TREE);
540 expr = fold_if_not_in_template (expr);
542 /* Mark the expression const or volatile, as appropriate.
543 Even though we've dealt with the type above, we still have
544 to mark the expression itself. */
545 if (type_quals & TYPE_QUAL_CONST)
546 TREE_READONLY (expr) = 1;
547 if (type_quals & TYPE_QUAL_VOLATILE)
548 TREE_THIS_VOLATILE (expr) = 1;
550 return expr;
553 /* Didn't find the base field?!? */
554 gcc_unreachable ();
557 /* Convert OBJECT to the base TYPE. OBJECT is an expression whose
558 type is a class type or a pointer to a class type. In the former
559 case, TYPE is also a class type; in the latter it is another
560 pointer type. If CHECK_ACCESS is true, an error message is emitted
561 if TYPE is inaccessible. If OBJECT has pointer type, the value is
562 assumed to be non-NULL. */
564 tree
565 convert_to_base (tree object, tree type, bool check_access, bool nonnull,
566 tsubst_flags_t complain)
568 tree binfo;
569 tree object_type;
571 if (TYPE_PTR_P (TREE_TYPE (object)))
573 object_type = TREE_TYPE (TREE_TYPE (object));
574 type = TREE_TYPE (type);
576 else
577 object_type = TREE_TYPE (object);
579 binfo = lookup_base (object_type, type, check_access ? ba_check : ba_unique,
580 NULL, complain);
581 if (!binfo || binfo == error_mark_node)
582 return error_mark_node;
584 return build_base_path (PLUS_EXPR, object, binfo, nonnull, complain);
587 /* EXPR is an expression with unqualified class type. BASE is a base
588 binfo of that class type. Returns EXPR, converted to the BASE
589 type. This function assumes that EXPR is the most derived class;
590 therefore virtual bases can be found at their static offsets. */
592 tree
593 convert_to_base_statically (tree expr, tree base)
595 tree expr_type;
597 expr_type = TREE_TYPE (expr);
598 if (!SAME_BINFO_TYPE_P (BINFO_TYPE (base), expr_type))
600 /* If this is a non-empty base, use a COMPONENT_REF. */
601 if (!is_empty_class (BINFO_TYPE (base)))
602 return build_simple_base_path (expr, base);
604 /* We use fold_build2 and fold_convert below to simplify the trees
605 provided to the optimizers. It is not safe to call these functions
606 when processing a template because they do not handle C++-specific
607 trees. */
608 gcc_assert (!processing_template_decl);
609 expr = cp_build_addr_expr (expr, tf_warning_or_error);
610 if (!integer_zerop (BINFO_OFFSET (base)))
611 expr = fold_build_pointer_plus_loc (input_location,
612 expr, BINFO_OFFSET (base));
613 expr = fold_convert (build_pointer_type (BINFO_TYPE (base)), expr);
614 expr = build_fold_indirect_ref_loc (input_location, expr);
617 return expr;
621 tree
622 build_vfield_ref (tree datum, tree type)
624 tree vfield, vcontext;
626 if (datum == error_mark_node)
627 return error_mark_node;
629 /* First, convert to the requested type. */
630 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (datum), type))
631 datum = convert_to_base (datum, type, /*check_access=*/false,
632 /*nonnull=*/true, tf_warning_or_error);
634 /* Second, the requested type may not be the owner of its own vptr.
635 If not, convert to the base class that owns it. We cannot use
636 convert_to_base here, because VCONTEXT may appear more than once
637 in the inheritance hierarchy of TYPE, and thus direct conversion
638 between the types may be ambiguous. Following the path back up
639 one step at a time via primary bases avoids the problem. */
640 vfield = TYPE_VFIELD (type);
641 vcontext = DECL_CONTEXT (vfield);
642 while (!same_type_ignoring_top_level_qualifiers_p (vcontext, type))
644 datum = build_simple_base_path (datum, CLASSTYPE_PRIMARY_BINFO (type));
645 type = TREE_TYPE (datum);
648 return build3 (COMPONENT_REF, TREE_TYPE (vfield), datum, vfield, NULL_TREE);
651 /* Given an object INSTANCE, return an expression which yields the
652 vtable element corresponding to INDEX. There are many special
653 cases for INSTANCE which we take care of here, mainly to avoid
654 creating extra tree nodes when we don't have to. */
656 static tree
657 build_vtbl_ref_1 (tree instance, tree idx)
659 tree aref;
660 tree vtbl = NULL_TREE;
662 /* Try to figure out what a reference refers to, and
663 access its virtual function table directly. */
665 int cdtorp = 0;
666 tree fixed_type = fixed_type_or_null (instance, NULL, &cdtorp);
668 tree basetype = non_reference (TREE_TYPE (instance));
670 if (fixed_type && !cdtorp)
672 tree binfo = lookup_base (fixed_type, basetype,
673 ba_unique, NULL, tf_none);
674 if (binfo && binfo != error_mark_node)
675 vtbl = unshare_expr (BINFO_VTABLE (binfo));
678 if (!vtbl)
679 vtbl = build_vfield_ref (instance, basetype);
681 aref = build_array_ref (input_location, vtbl, idx);
682 TREE_CONSTANT (aref) |= TREE_CONSTANT (vtbl) && TREE_CONSTANT (idx);
684 return aref;
687 tree
688 build_vtbl_ref (tree instance, tree idx)
690 tree aref = build_vtbl_ref_1 (instance, idx);
692 return aref;
695 /* Given a stable object pointer INSTANCE_PTR, return an expression which
696 yields a function pointer corresponding to vtable element INDEX. */
698 tree
699 build_vfn_ref (tree instance_ptr, tree idx)
701 tree aref;
703 aref = build_vtbl_ref_1 (cp_build_indirect_ref (instance_ptr, RO_NULL,
704 tf_warning_or_error),
705 idx);
707 /* When using function descriptors, the address of the
708 vtable entry is treated as a function pointer. */
709 if (TARGET_VTABLE_USES_DESCRIPTORS)
710 aref = build1 (NOP_EXPR, TREE_TYPE (aref),
711 cp_build_addr_expr (aref, tf_warning_or_error));
713 /* Remember this as a method reference, for later devirtualization. */
714 aref = build3 (OBJ_TYPE_REF, TREE_TYPE (aref), aref, instance_ptr, idx);
716 return aref;
719 /* Return the name of the virtual function table (as an IDENTIFIER_NODE)
720 for the given TYPE. */
722 static tree
723 get_vtable_name (tree type)
725 return mangle_vtbl_for_type (type);
728 /* DECL is an entity associated with TYPE, like a virtual table or an
729 implicitly generated constructor. Determine whether or not DECL
730 should have external or internal linkage at the object file
731 level. This routine does not deal with COMDAT linkage and other
732 similar complexities; it simply sets TREE_PUBLIC if it possible for
733 entities in other translation units to contain copies of DECL, in
734 the abstract. */
736 void
737 set_linkage_according_to_type (tree /*type*/, tree decl)
739 TREE_PUBLIC (decl) = 1;
740 determine_visibility (decl);
743 /* Create a VAR_DECL for a primary or secondary vtable for CLASS_TYPE.
744 (For a secondary vtable for B-in-D, CLASS_TYPE should be D, not B.)
745 Use NAME for the name of the vtable, and VTABLE_TYPE for its type. */
747 static tree
748 build_vtable (tree class_type, tree name, tree vtable_type)
750 tree decl;
752 decl = build_lang_decl (VAR_DECL, name, vtable_type);
753 /* vtable names are already mangled; give them their DECL_ASSEMBLER_NAME
754 now to avoid confusion in mangle_decl. */
755 SET_DECL_ASSEMBLER_NAME (decl, name);
756 DECL_CONTEXT (decl) = class_type;
757 DECL_ARTIFICIAL (decl) = 1;
758 TREE_STATIC (decl) = 1;
759 TREE_READONLY (decl) = 1;
760 DECL_VIRTUAL_P (decl) = 1;
761 DECL_ALIGN (decl) = TARGET_VTABLE_ENTRY_ALIGN;
762 DECL_VTABLE_OR_VTT_P (decl) = 1;
763 /* At one time the vtable info was grabbed 2 words at a time. This
764 fails on sparc unless you have 8-byte alignment. (tiemann) */
765 DECL_ALIGN (decl) = MAX (TYPE_ALIGN (double_type_node),
766 DECL_ALIGN (decl));
767 set_linkage_according_to_type (class_type, decl);
768 /* The vtable has not been defined -- yet. */
769 DECL_EXTERNAL (decl) = 1;
770 DECL_NOT_REALLY_EXTERN (decl) = 1;
772 /* Mark the VAR_DECL node representing the vtable itself as a
773 "gratuitous" one, thereby forcing dwarfout.c to ignore it. It
774 is rather important that such things be ignored because any
775 effort to actually generate DWARF for them will run into
776 trouble when/if we encounter code like:
778 #pragma interface
779 struct S { virtual void member (); };
781 because the artificial declaration of the vtable itself (as
782 manufactured by the g++ front end) will say that the vtable is
783 a static member of `S' but only *after* the debug output for
784 the definition of `S' has already been output. This causes
785 grief because the DWARF entry for the definition of the vtable
786 will try to refer back to an earlier *declaration* of the
787 vtable as a static member of `S' and there won't be one. We
788 might be able to arrange to have the "vtable static member"
789 attached to the member list for `S' before the debug info for
790 `S' get written (which would solve the problem) but that would
791 require more intrusive changes to the g++ front end. */
792 DECL_IGNORED_P (decl) = 1;
794 return decl;
797 /* Get the VAR_DECL of the vtable for TYPE. TYPE need not be polymorphic,
798 or even complete. If this does not exist, create it. If COMPLETE is
799 nonzero, then complete the definition of it -- that will render it
800 impossible to actually build the vtable, but is useful to get at those
801 which are known to exist in the runtime. */
803 tree
804 get_vtable_decl (tree type, int complete)
806 tree decl;
808 if (CLASSTYPE_VTABLES (type))
809 return CLASSTYPE_VTABLES (type);
811 decl = build_vtable (type, get_vtable_name (type), vtbl_type_node);
812 CLASSTYPE_VTABLES (type) = decl;
814 if (complete)
816 DECL_EXTERNAL (decl) = 1;
817 cp_finish_decl (decl, NULL_TREE, false, NULL_TREE, 0);
820 return decl;
823 /* Build the primary virtual function table for TYPE. If BINFO is
824 non-NULL, build the vtable starting with the initial approximation
825 that it is the same as the one which is the head of the association
826 list. Returns a nonzero value if a new vtable is actually
827 created. */
829 static int
830 build_primary_vtable (tree binfo, tree type)
832 tree decl;
833 tree virtuals;
835 decl = get_vtable_decl (type, /*complete=*/0);
837 if (binfo)
839 if (BINFO_NEW_VTABLE_MARKED (binfo))
840 /* We have already created a vtable for this base, so there's
841 no need to do it again. */
842 return 0;
844 virtuals = copy_list (BINFO_VIRTUALS (binfo));
845 TREE_TYPE (decl) = TREE_TYPE (get_vtbl_decl_for_binfo (binfo));
846 DECL_SIZE (decl) = TYPE_SIZE (TREE_TYPE (decl));
847 DECL_SIZE_UNIT (decl) = TYPE_SIZE_UNIT (TREE_TYPE (decl));
849 else
851 gcc_assert (TREE_TYPE (decl) == vtbl_type_node);
852 virtuals = NULL_TREE;
855 if (GATHER_STATISTICS)
857 n_vtables += 1;
858 n_vtable_elems += list_length (virtuals);
861 /* Initialize the association list for this type, based
862 on our first approximation. */
863 BINFO_VTABLE (TYPE_BINFO (type)) = decl;
864 BINFO_VIRTUALS (TYPE_BINFO (type)) = virtuals;
865 SET_BINFO_NEW_VTABLE_MARKED (TYPE_BINFO (type));
866 return 1;
869 /* Give BINFO a new virtual function table which is initialized
870 with a skeleton-copy of its original initialization. The only
871 entry that changes is the `delta' entry, so we can really
872 share a lot of structure.
874 FOR_TYPE is the most derived type which caused this table to
875 be needed.
877 Returns nonzero if we haven't met BINFO before.
879 The order in which vtables are built (by calling this function) for
880 an object must remain the same, otherwise a binary incompatibility
881 can result. */
883 static int
884 build_secondary_vtable (tree binfo)
886 if (BINFO_NEW_VTABLE_MARKED (binfo))
887 /* We already created a vtable for this base. There's no need to
888 do it again. */
889 return 0;
891 /* Remember that we've created a vtable for this BINFO, so that we
892 don't try to do so again. */
893 SET_BINFO_NEW_VTABLE_MARKED (binfo);
895 /* Make fresh virtual list, so we can smash it later. */
896 BINFO_VIRTUALS (binfo) = copy_list (BINFO_VIRTUALS (binfo));
898 /* Secondary vtables are laid out as part of the same structure as
899 the primary vtable. */
900 BINFO_VTABLE (binfo) = NULL_TREE;
901 return 1;
904 /* Create a new vtable for BINFO which is the hierarchy dominated by
905 T. Return nonzero if we actually created a new vtable. */
907 static int
908 make_new_vtable (tree t, tree binfo)
910 if (binfo == TYPE_BINFO (t))
911 /* In this case, it is *type*'s vtable we are modifying. We start
912 with the approximation that its vtable is that of the
913 immediate base class. */
914 return build_primary_vtable (binfo, t);
915 else
916 /* This is our very own copy of `basetype' to play with. Later,
917 we will fill in all the virtual functions that override the
918 virtual functions in these base classes which are not defined
919 by the current type. */
920 return build_secondary_vtable (binfo);
923 /* Make *VIRTUALS, an entry on the BINFO_VIRTUALS list for BINFO
924 (which is in the hierarchy dominated by T) list FNDECL as its
925 BV_FN. DELTA is the required constant adjustment from the `this'
926 pointer where the vtable entry appears to the `this' required when
927 the function is actually called. */
929 static void
930 modify_vtable_entry (tree t,
931 tree binfo,
932 tree fndecl,
933 tree delta,
934 tree *virtuals)
936 tree v;
938 v = *virtuals;
940 if (fndecl != BV_FN (v)
941 || !tree_int_cst_equal (delta, BV_DELTA (v)))
943 /* We need a new vtable for BINFO. */
944 if (make_new_vtable (t, binfo))
946 /* If we really did make a new vtable, we also made a copy
947 of the BINFO_VIRTUALS list. Now, we have to find the
948 corresponding entry in that list. */
949 *virtuals = BINFO_VIRTUALS (binfo);
950 while (BV_FN (*virtuals) != BV_FN (v))
951 *virtuals = TREE_CHAIN (*virtuals);
952 v = *virtuals;
955 BV_DELTA (v) = delta;
956 BV_VCALL_INDEX (v) = NULL_TREE;
957 BV_FN (v) = fndecl;
961 // Returns the template associated with the member FN or
962 // NULL if the declaration is neither a template nor temploid.
963 static inline tree
964 get_member_fn_template (tree fn)
966 if (TREE_CODE (fn) == TEMPLATE_DECL)
967 return fn;
968 if (TREE_CODE (fn) == FUNCTION_DECL && DECL_TEMPLATE_INFO (fn))
969 return DECL_TI_TEMPLATE (fn);
970 return NULL_TREE;
973 // Returns true if NEWDECL and OLDDECL are member functions with with
974 // different constraints. If NEWDECL and OLDDECL are non-template members
975 // or specializations of non-template members, the overloads are
976 // differentiated by the template constraints.
978 // Note that the types of the functions are assumed to be equivalent.
979 static inline bool
980 are_constrained_member_overloads (tree newdecl, tree olddecl)
982 newdecl = get_member_fn_template (newdecl);
983 olddecl = get_member_fn_template (olddecl);
985 // If neither is a template or temploid, then they cannot
986 // be constrained declarations.
987 if (!newdecl && !olddecl)
988 return false;
989 else
990 return !equivalently_constrained (newdecl, olddecl);
994 /* Add method METHOD to class TYPE. If USING_DECL is non-null, it is
995 the USING_DECL naming METHOD. Returns true if the method could be
996 added to the method vec. */
998 bool
999 add_method (tree type, tree method, tree using_decl)
1001 unsigned slot;
1002 tree overload;
1003 bool template_conv_p = false;
1004 bool conv_p;
1005 vec<tree, va_gc> *method_vec;
1006 bool complete_p;
1007 bool insert_p = false;
1008 tree current_fns;
1009 tree fns;
1011 if (method == error_mark_node)
1012 return false;
1014 complete_p = COMPLETE_TYPE_P (type);
1015 conv_p = DECL_CONV_FN_P (method);
1016 if (conv_p)
1017 template_conv_p = (TREE_CODE (method) == TEMPLATE_DECL
1018 && DECL_TEMPLATE_CONV_FN_P (method));
1020 method_vec = CLASSTYPE_METHOD_VEC (type);
1021 if (!method_vec)
1023 /* Make a new method vector. We start with 8 entries. We must
1024 allocate at least two (for constructors and destructors), and
1025 we're going to end up with an assignment operator at some
1026 point as well. */
1027 vec_alloc (method_vec, 8);
1028 /* Create slots for constructors and destructors. */
1029 method_vec->quick_push (NULL_TREE);
1030 method_vec->quick_push (NULL_TREE);
1031 CLASSTYPE_METHOD_VEC (type) = method_vec;
1034 /* Maintain TYPE_HAS_USER_CONSTRUCTOR, etc. */
1035 grok_special_member_properties (method);
1037 /* Constructors and destructors go in special slots. */
1038 if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (method))
1039 slot = CLASSTYPE_CONSTRUCTOR_SLOT;
1040 else if (DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (method))
1042 slot = CLASSTYPE_DESTRUCTOR_SLOT;
1044 if (TYPE_FOR_JAVA (type))
1046 if (!DECL_ARTIFICIAL (method))
1047 error ("Java class %qT cannot have a destructor", type);
1048 else if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
1049 error ("Java class %qT cannot have an implicit non-trivial "
1050 "destructor",
1051 type);
1054 else
1056 tree m;
1058 insert_p = true;
1059 /* See if we already have an entry with this name. */
1060 for (slot = CLASSTYPE_FIRST_CONVERSION_SLOT;
1061 vec_safe_iterate (method_vec, slot, &m);
1062 ++slot)
1064 m = OVL_CURRENT (m);
1065 if (template_conv_p)
1067 if (TREE_CODE (m) == TEMPLATE_DECL
1068 && DECL_TEMPLATE_CONV_FN_P (m))
1069 insert_p = false;
1070 break;
1072 if (conv_p && !DECL_CONV_FN_P (m))
1073 break;
1074 if (DECL_NAME (m) == DECL_NAME (method))
1076 insert_p = false;
1077 break;
1079 if (complete_p
1080 && !DECL_CONV_FN_P (m)
1081 && DECL_NAME (m) > DECL_NAME (method))
1082 break;
1085 current_fns = insert_p ? NULL_TREE : (*method_vec)[slot];
1087 /* Check to see if we've already got this method. */
1088 for (fns = current_fns; fns; fns = OVL_NEXT (fns))
1090 tree fn = OVL_CURRENT (fns);
1091 tree fn_type;
1092 tree method_type;
1093 tree parms1;
1094 tree parms2;
1096 if (TREE_CODE (fn) != TREE_CODE (method))
1097 continue;
1099 /* [over.load] Member function declarations with the
1100 same name and the same parameter types cannot be
1101 overloaded if any of them is a static member
1102 function declaration.
1104 [over.load] Member function declarations with the same name and
1105 the same parameter-type-list as well as member function template
1106 declarations with the same name, the same parameter-type-list, and
1107 the same template parameter lists cannot be overloaded if any of
1108 them, but not all, have a ref-qualifier.
1110 [namespace.udecl] When a using-declaration brings names
1111 from a base class into a derived class scope, member
1112 functions in the derived class override and/or hide member
1113 functions with the same name and parameter types in a base
1114 class (rather than conflicting). */
1115 fn_type = TREE_TYPE (fn);
1116 method_type = TREE_TYPE (method);
1117 parms1 = TYPE_ARG_TYPES (fn_type);
1118 parms2 = TYPE_ARG_TYPES (method_type);
1120 /* Compare the quals on the 'this' parm. Don't compare
1121 the whole types, as used functions are treated as
1122 coming from the using class in overload resolution. */
1123 if (! DECL_STATIC_FUNCTION_P (fn)
1124 && ! DECL_STATIC_FUNCTION_P (method)
1125 /* Either both or neither need to be ref-qualified for
1126 differing quals to allow overloading. */
1127 && (FUNCTION_REF_QUALIFIED (fn_type)
1128 == FUNCTION_REF_QUALIFIED (method_type))
1129 && (type_memfn_quals (fn_type) != type_memfn_quals (method_type)
1130 || type_memfn_rqual (fn_type) != type_memfn_rqual (method_type)))
1131 continue;
1133 /* For templates, the return type and template parameters
1134 must be identical. */
1135 if (TREE_CODE (fn) == TEMPLATE_DECL
1136 && (!same_type_p (TREE_TYPE (fn_type),
1137 TREE_TYPE (method_type))
1138 || !comp_template_parms (DECL_TEMPLATE_PARMS (fn),
1139 DECL_TEMPLATE_PARMS (method))))
1140 continue;
1142 if (! DECL_STATIC_FUNCTION_P (fn))
1143 parms1 = TREE_CHAIN (parms1);
1144 if (! DECL_STATIC_FUNCTION_P (method))
1145 parms2 = TREE_CHAIN (parms2);
1147 if (compparms (parms1, parms2)
1148 && (!DECL_CONV_FN_P (fn)
1149 || same_type_p (TREE_TYPE (fn_type),
1150 TREE_TYPE (method_type))))
1152 /* For function versions, their parms and types match
1153 but they are not duplicates. Record function versions
1154 as and when they are found. extern "C" functions are
1155 not treated as versions. */
1156 if (TREE_CODE (fn) == FUNCTION_DECL
1157 && TREE_CODE (method) == FUNCTION_DECL
1158 && !DECL_EXTERN_C_P (fn)
1159 && !DECL_EXTERN_C_P (method)
1160 && targetm.target_option.function_versions (fn, method))
1162 /* Mark functions as versions if necessary. Modify the mangled
1163 decl name if necessary. */
1164 if (!DECL_FUNCTION_VERSIONED (fn))
1166 DECL_FUNCTION_VERSIONED (fn) = 1;
1167 if (DECL_ASSEMBLER_NAME_SET_P (fn))
1168 mangle_decl (fn);
1170 if (!DECL_FUNCTION_VERSIONED (method))
1172 DECL_FUNCTION_VERSIONED (method) = 1;
1173 if (DECL_ASSEMBLER_NAME_SET_P (method))
1174 mangle_decl (method);
1176 record_function_versions (fn, method);
1177 continue;
1179 if (DECL_INHERITED_CTOR_BASE (method))
1181 if (DECL_INHERITED_CTOR_BASE (fn))
1183 error_at (DECL_SOURCE_LOCATION (method),
1184 "%q#D inherited from %qT", method,
1185 DECL_INHERITED_CTOR_BASE (method));
1186 error_at (DECL_SOURCE_LOCATION (fn),
1187 "conflicts with version inherited from %qT",
1188 DECL_INHERITED_CTOR_BASE (fn));
1190 /* Otherwise defer to the other function. */
1191 return false;
1193 if (using_decl)
1195 if (DECL_CONTEXT (fn) == type)
1196 /* Defer to the local function. */
1197 return false;
1199 else if (are_constrained_member_overloads (fn, method))
1200 continue;
1201 else
1203 error ("%q+#D cannot be overloaded", method);
1204 error ("with %q+#D", fn);
1207 /* We don't call duplicate_decls here to merge the
1208 declarations because that will confuse things if the
1209 methods have inline definitions. In particular, we
1210 will crash while processing the definitions. */
1211 return false;
1215 /* A class should never have more than one destructor. */
1216 if (current_fns && DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (method))
1217 return false;
1219 /* Add the new binding. */
1220 if (using_decl)
1222 overload = ovl_cons (method, current_fns);
1223 OVL_USED (overload) = true;
1225 else
1226 overload = build_overload (method, current_fns);
1228 if (conv_p)
1229 TYPE_HAS_CONVERSION (type) = 1;
1230 else if (slot >= CLASSTYPE_FIRST_CONVERSION_SLOT && !complete_p)
1231 push_class_level_binding (DECL_NAME (method), overload);
1233 if (insert_p)
1235 bool reallocated;
1237 /* We only expect to add few methods in the COMPLETE_P case, so
1238 just make room for one more method in that case. */
1239 if (complete_p)
1240 reallocated = vec_safe_reserve_exact (method_vec, 1);
1241 else
1242 reallocated = vec_safe_reserve (method_vec, 1);
1243 if (reallocated)
1244 CLASSTYPE_METHOD_VEC (type) = method_vec;
1245 if (slot == method_vec->length ())
1246 method_vec->quick_push (overload);
1247 else
1248 method_vec->quick_insert (slot, overload);
1250 else
1251 /* Replace the current slot. */
1252 (*method_vec)[slot] = overload;
1253 return true;
1256 /* Subroutines of finish_struct. */
1258 /* Change the access of FDECL to ACCESS in T. Return 1 if change was
1259 legit, otherwise return 0. */
1261 static int
1262 alter_access (tree t, tree fdecl, tree access)
1264 tree elem;
1266 if (!DECL_LANG_SPECIFIC (fdecl))
1267 retrofit_lang_decl (fdecl);
1269 gcc_assert (!DECL_DISCRIMINATOR_P (fdecl));
1271 elem = purpose_member (t, DECL_ACCESS (fdecl));
1272 if (elem)
1274 if (TREE_VALUE (elem) != access)
1276 if (TREE_CODE (TREE_TYPE (fdecl)) == FUNCTION_DECL)
1277 error ("conflicting access specifications for method"
1278 " %q+D, ignored", TREE_TYPE (fdecl));
1279 else
1280 error ("conflicting access specifications for field %qE, ignored",
1281 DECL_NAME (fdecl));
1283 else
1285 /* They're changing the access to the same thing they changed
1286 it to before. That's OK. */
1290 else
1292 perform_or_defer_access_check (TYPE_BINFO (t), fdecl, fdecl,
1293 tf_warning_or_error);
1294 DECL_ACCESS (fdecl) = tree_cons (t, access, DECL_ACCESS (fdecl));
1295 return 1;
1297 return 0;
1300 /* Process the USING_DECL, which is a member of T. */
1302 static void
1303 handle_using_decl (tree using_decl, tree t)
1305 tree decl = USING_DECL_DECLS (using_decl);
1306 tree name = DECL_NAME (using_decl);
1307 tree access
1308 = TREE_PRIVATE (using_decl) ? access_private_node
1309 : TREE_PROTECTED (using_decl) ? access_protected_node
1310 : access_public_node;
1311 tree flist = NULL_TREE;
1312 tree old_value;
1314 gcc_assert (!processing_template_decl && decl);
1316 old_value = lookup_member (t, name, /*protect=*/0, /*want_type=*/false,
1317 tf_warning_or_error);
1318 if (old_value)
1320 if (is_overloaded_fn (old_value))
1321 old_value = OVL_CURRENT (old_value);
1323 if (DECL_P (old_value) && DECL_CONTEXT (old_value) == t)
1324 /* OK */;
1325 else
1326 old_value = NULL_TREE;
1329 cp_emit_debug_info_for_using (decl, USING_DECL_SCOPE (using_decl));
1331 if (is_overloaded_fn (decl))
1332 flist = decl;
1334 if (! old_value)
1336 else if (is_overloaded_fn (old_value))
1338 if (flist)
1339 /* It's OK to use functions from a base when there are functions with
1340 the same name already present in the current class. */;
1341 else
1343 error ("%q+D invalid in %q#T", using_decl, t);
1344 error (" because of local method %q+#D with same name",
1345 OVL_CURRENT (old_value));
1346 return;
1349 else if (!DECL_ARTIFICIAL (old_value))
1351 error ("%q+D invalid in %q#T", using_decl, t);
1352 error (" because of local member %q+#D with same name", old_value);
1353 return;
1356 /* Make type T see field decl FDECL with access ACCESS. */
1357 if (flist)
1358 for (; flist; flist = OVL_NEXT (flist))
1360 add_method (t, OVL_CURRENT (flist), using_decl);
1361 alter_access (t, OVL_CURRENT (flist), access);
1363 else
1364 alter_access (t, decl, access);
1367 /* walk_tree callback for check_abi_tags: if the type at *TP involves any
1368 types with abi tags, add the corresponding identifiers to the VEC in
1369 *DATA and set IDENTIFIER_MARKED. */
1371 struct abi_tag_data
1373 tree t;
1374 tree subob;
1377 static tree
1378 find_abi_tags_r (tree *tp, int */*walk_subtrees*/, void *data)
1380 if (!OVERLOAD_TYPE_P (*tp))
1381 return NULL_TREE;
1383 if (tree attributes = lookup_attribute ("abi_tag", TYPE_ATTRIBUTES (*tp)))
1385 struct abi_tag_data *p = static_cast<struct abi_tag_data*>(data);
1386 for (tree list = TREE_VALUE (attributes); list;
1387 list = TREE_CHAIN (list))
1389 tree tag = TREE_VALUE (list);
1390 tree id = get_identifier (TREE_STRING_POINTER (tag));
1391 if (!IDENTIFIER_MARKED (id))
1393 if (TYPE_P (p->subob))
1395 warning (OPT_Wabi_tag, "%qT does not have the %E abi tag "
1396 "that base %qT has", p->t, tag, p->subob);
1397 inform (location_of (p->subob), "%qT declared here",
1398 p->subob);
1400 else
1402 warning (OPT_Wabi_tag, "%qT does not have the %E abi tag "
1403 "that %qT (used in the type of %qD) has",
1404 p->t, tag, *tp, p->subob);
1405 inform (location_of (p->subob), "%qD declared here",
1406 p->subob);
1407 inform (location_of (*tp), "%qT declared here", *tp);
1412 return NULL_TREE;
1415 /* Set IDENTIFIER_MARKED on all the ABI tags on T and its (transitively
1416 complete) template arguments. */
1418 static void
1419 mark_type_abi_tags (tree t, bool val)
1421 tree attributes = lookup_attribute ("abi_tag", TYPE_ATTRIBUTES (t));
1422 if (attributes)
1424 for (tree list = TREE_VALUE (attributes); list;
1425 list = TREE_CHAIN (list))
1427 tree tag = TREE_VALUE (list);
1428 tree id = get_identifier (TREE_STRING_POINTER (tag));
1429 IDENTIFIER_MARKED (id) = val;
1433 /* Also mark ABI tags from template arguments. */
1434 if (CLASSTYPE_TEMPLATE_INFO (t))
1436 tree args = CLASSTYPE_TI_ARGS (t);
1437 for (int i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
1439 tree level = TMPL_ARGS_LEVEL (args, i+1);
1440 for (int j = 0; j < TREE_VEC_LENGTH (level); ++j)
1442 tree arg = TREE_VEC_ELT (level, j);
1443 if (CLASS_TYPE_P (arg))
1444 mark_type_abi_tags (arg, val);
1450 /* Check that class T has all the abi tags that subobject SUBOB has, or
1451 warn if not. */
1453 static void
1454 check_abi_tags (tree t, tree subob)
1456 mark_type_abi_tags (t, true);
1458 tree subtype = TYPE_P (subob) ? subob : TREE_TYPE (subob);
1459 struct abi_tag_data data = { t, subob };
1461 cp_walk_tree_without_duplicates (&subtype, find_abi_tags_r, &data);
1463 mark_type_abi_tags (t, false);
1466 /* Run through the base classes of T, updating CANT_HAVE_CONST_CTOR_P,
1467 and NO_CONST_ASN_REF_P. Also set flag bits in T based on
1468 properties of the bases. */
1470 static void
1471 check_bases (tree t,
1472 int* cant_have_const_ctor_p,
1473 int* no_const_asn_ref_p)
1475 int i;
1476 bool seen_non_virtual_nearly_empty_base_p = 0;
1477 int seen_tm_mask = 0;
1478 tree base_binfo;
1479 tree binfo;
1480 tree field = NULL_TREE;
1482 if (!CLASSTYPE_NON_STD_LAYOUT (t))
1483 for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
1484 if (TREE_CODE (field) == FIELD_DECL)
1485 break;
1487 for (binfo = TYPE_BINFO (t), i = 0;
1488 BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
1490 tree basetype = TREE_TYPE (base_binfo);
1492 gcc_assert (COMPLETE_TYPE_P (basetype));
1494 if (CLASSTYPE_FINAL (basetype))
1495 error ("cannot derive from %<final%> base %qT in derived type %qT",
1496 basetype, t);
1498 /* If any base class is non-literal, so is the derived class. */
1499 if (!CLASSTYPE_LITERAL_P (basetype))
1500 CLASSTYPE_LITERAL_P (t) = false;
1502 /* Effective C++ rule 14. We only need to check TYPE_POLYMORPHIC_P
1503 here because the case of virtual functions but non-virtual
1504 dtor is handled in finish_struct_1. */
1505 if (!TYPE_POLYMORPHIC_P (basetype))
1506 warning (OPT_Weffc__,
1507 "base class %q#T has a non-virtual destructor", basetype);
1509 /* If the base class doesn't have copy constructors or
1510 assignment operators that take const references, then the
1511 derived class cannot have such a member automatically
1512 generated. */
1513 if (TYPE_HAS_COPY_CTOR (basetype)
1514 && ! TYPE_HAS_CONST_COPY_CTOR (basetype))
1515 *cant_have_const_ctor_p = 1;
1516 if (TYPE_HAS_COPY_ASSIGN (basetype)
1517 && !TYPE_HAS_CONST_COPY_ASSIGN (basetype))
1518 *no_const_asn_ref_p = 1;
1520 if (BINFO_VIRTUAL_P (base_binfo))
1521 /* A virtual base does not effect nearly emptiness. */
1523 else if (CLASSTYPE_NEARLY_EMPTY_P (basetype))
1525 if (seen_non_virtual_nearly_empty_base_p)
1526 /* And if there is more than one nearly empty base, then the
1527 derived class is not nearly empty either. */
1528 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
1529 else
1530 /* Remember we've seen one. */
1531 seen_non_virtual_nearly_empty_base_p = 1;
1533 else if (!is_empty_class (basetype))
1534 /* If the base class is not empty or nearly empty, then this
1535 class cannot be nearly empty. */
1536 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
1538 /* A lot of properties from the bases also apply to the derived
1539 class. */
1540 TYPE_NEEDS_CONSTRUCTING (t) |= TYPE_NEEDS_CONSTRUCTING (basetype);
1541 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t)
1542 |= TYPE_HAS_NONTRIVIAL_DESTRUCTOR (basetype);
1543 TYPE_HAS_COMPLEX_COPY_ASSIGN (t)
1544 |= (TYPE_HAS_COMPLEX_COPY_ASSIGN (basetype)
1545 || !TYPE_HAS_COPY_ASSIGN (basetype));
1546 TYPE_HAS_COMPLEX_COPY_CTOR (t) |= (TYPE_HAS_COMPLEX_COPY_CTOR (basetype)
1547 || !TYPE_HAS_COPY_CTOR (basetype));
1548 TYPE_HAS_COMPLEX_MOVE_ASSIGN (t)
1549 |= TYPE_HAS_COMPLEX_MOVE_ASSIGN (basetype);
1550 TYPE_HAS_COMPLEX_MOVE_CTOR (t) |= TYPE_HAS_COMPLEX_MOVE_CTOR (basetype);
1551 TYPE_POLYMORPHIC_P (t) |= TYPE_POLYMORPHIC_P (basetype);
1552 CLASSTYPE_CONTAINS_EMPTY_CLASS_P (t)
1553 |= CLASSTYPE_CONTAINS_EMPTY_CLASS_P (basetype);
1554 TYPE_HAS_COMPLEX_DFLT (t) |= (!TYPE_HAS_DEFAULT_CONSTRUCTOR (basetype)
1555 || TYPE_HAS_COMPLEX_DFLT (basetype));
1556 SET_CLASSTYPE_READONLY_FIELDS_NEED_INIT
1557 (t, CLASSTYPE_READONLY_FIELDS_NEED_INIT (t)
1558 | CLASSTYPE_READONLY_FIELDS_NEED_INIT (basetype));
1559 SET_CLASSTYPE_REF_FIELDS_NEED_INIT
1560 (t, CLASSTYPE_REF_FIELDS_NEED_INIT (t)
1561 | CLASSTYPE_REF_FIELDS_NEED_INIT (basetype));
1563 /* A standard-layout class is a class that:
1565 * has no non-standard-layout base classes, */
1566 CLASSTYPE_NON_STD_LAYOUT (t) |= CLASSTYPE_NON_STD_LAYOUT (basetype);
1567 if (!CLASSTYPE_NON_STD_LAYOUT (t))
1569 tree basefield;
1570 /* ...has no base classes of the same type as the first non-static
1571 data member... */
1572 if (field && DECL_CONTEXT (field) == t
1573 && (same_type_ignoring_top_level_qualifiers_p
1574 (TREE_TYPE (field), basetype)))
1575 CLASSTYPE_NON_STD_LAYOUT (t) = 1;
1576 else
1577 /* ...either has no non-static data members in the most-derived
1578 class and at most one base class with non-static data
1579 members, or has no base classes with non-static data
1580 members */
1581 for (basefield = TYPE_FIELDS (basetype); basefield;
1582 basefield = DECL_CHAIN (basefield))
1583 if (TREE_CODE (basefield) == FIELD_DECL)
1585 if (field)
1586 CLASSTYPE_NON_STD_LAYOUT (t) = 1;
1587 else
1588 field = basefield;
1589 break;
1593 /* Don't bother collecting tm attributes if transactional memory
1594 support is not enabled. */
1595 if (flag_tm)
1597 tree tm_attr = find_tm_attribute (TYPE_ATTRIBUTES (basetype));
1598 if (tm_attr)
1599 seen_tm_mask |= tm_attr_to_mask (tm_attr);
1602 check_abi_tags (t, basetype);
1605 /* If one of the base classes had TM attributes, and the current class
1606 doesn't define its own, then the current class inherits one. */
1607 if (seen_tm_mask && !find_tm_attribute (TYPE_ATTRIBUTES (t)))
1609 tree tm_attr = tm_mask_to_attr (seen_tm_mask & -seen_tm_mask);
1610 TYPE_ATTRIBUTES (t) = tree_cons (tm_attr, NULL, TYPE_ATTRIBUTES (t));
1614 /* Determine all the primary bases within T. Sets BINFO_PRIMARY_BASE_P for
1615 those that are primaries. Sets BINFO_LOST_PRIMARY_P for those
1616 that have had a nearly-empty virtual primary base stolen by some
1617 other base in the hierarchy. Determines CLASSTYPE_PRIMARY_BASE for
1618 T. */
1620 static void
1621 determine_primary_bases (tree t)
1623 unsigned i;
1624 tree primary = NULL_TREE;
1625 tree type_binfo = TYPE_BINFO (t);
1626 tree base_binfo;
1628 /* Determine the primary bases of our bases. */
1629 for (base_binfo = TREE_CHAIN (type_binfo); base_binfo;
1630 base_binfo = TREE_CHAIN (base_binfo))
1632 tree primary = CLASSTYPE_PRIMARY_BINFO (BINFO_TYPE (base_binfo));
1634 /* See if we're the non-virtual primary of our inheritance
1635 chain. */
1636 if (!BINFO_VIRTUAL_P (base_binfo))
1638 tree parent = BINFO_INHERITANCE_CHAIN (base_binfo);
1639 tree parent_primary = CLASSTYPE_PRIMARY_BINFO (BINFO_TYPE (parent));
1641 if (parent_primary
1642 && SAME_BINFO_TYPE_P (BINFO_TYPE (base_binfo),
1643 BINFO_TYPE (parent_primary)))
1644 /* We are the primary binfo. */
1645 BINFO_PRIMARY_P (base_binfo) = 1;
1647 /* Determine if we have a virtual primary base, and mark it so.
1649 if (primary && BINFO_VIRTUAL_P (primary))
1651 tree this_primary = copied_binfo (primary, base_binfo);
1653 if (BINFO_PRIMARY_P (this_primary))
1654 /* Someone already claimed this base. */
1655 BINFO_LOST_PRIMARY_P (base_binfo) = 1;
1656 else
1658 tree delta;
1660 BINFO_PRIMARY_P (this_primary) = 1;
1661 BINFO_INHERITANCE_CHAIN (this_primary) = base_binfo;
1663 /* A virtual binfo might have been copied from within
1664 another hierarchy. As we're about to use it as a
1665 primary base, make sure the offsets match. */
1666 delta = size_diffop_loc (input_location,
1667 convert (ssizetype,
1668 BINFO_OFFSET (base_binfo)),
1669 convert (ssizetype,
1670 BINFO_OFFSET (this_primary)));
1672 propagate_binfo_offsets (this_primary, delta);
1677 /* First look for a dynamic direct non-virtual base. */
1678 for (i = 0; BINFO_BASE_ITERATE (type_binfo, i, base_binfo); i++)
1680 tree basetype = BINFO_TYPE (base_binfo);
1682 if (TYPE_CONTAINS_VPTR_P (basetype) && !BINFO_VIRTUAL_P (base_binfo))
1684 primary = base_binfo;
1685 goto found;
1689 /* A "nearly-empty" virtual base class can be the primary base
1690 class, if no non-virtual polymorphic base can be found. Look for
1691 a nearly-empty virtual dynamic base that is not already a primary
1692 base of something in the hierarchy. If there is no such base,
1693 just pick the first nearly-empty virtual base. */
1695 for (base_binfo = TREE_CHAIN (type_binfo); base_binfo;
1696 base_binfo = TREE_CHAIN (base_binfo))
1697 if (BINFO_VIRTUAL_P (base_binfo)
1698 && CLASSTYPE_NEARLY_EMPTY_P (BINFO_TYPE (base_binfo)))
1700 if (!BINFO_PRIMARY_P (base_binfo))
1702 /* Found one that is not primary. */
1703 primary = base_binfo;
1704 goto found;
1706 else if (!primary)
1707 /* Remember the first candidate. */
1708 primary = base_binfo;
1711 found:
1712 /* If we've got a primary base, use it. */
1713 if (primary)
1715 tree basetype = BINFO_TYPE (primary);
1717 CLASSTYPE_PRIMARY_BINFO (t) = primary;
1718 if (BINFO_PRIMARY_P (primary))
1719 /* We are stealing a primary base. */
1720 BINFO_LOST_PRIMARY_P (BINFO_INHERITANCE_CHAIN (primary)) = 1;
1721 BINFO_PRIMARY_P (primary) = 1;
1722 if (BINFO_VIRTUAL_P (primary))
1724 tree delta;
1726 BINFO_INHERITANCE_CHAIN (primary) = type_binfo;
1727 /* A virtual binfo might have been copied from within
1728 another hierarchy. As we're about to use it as a primary
1729 base, make sure the offsets match. */
1730 delta = size_diffop_loc (input_location, ssize_int (0),
1731 convert (ssizetype, BINFO_OFFSET (primary)));
1733 propagate_binfo_offsets (primary, delta);
1736 primary = TYPE_BINFO (basetype);
1738 TYPE_VFIELD (t) = TYPE_VFIELD (basetype);
1739 BINFO_VTABLE (type_binfo) = BINFO_VTABLE (primary);
1740 BINFO_VIRTUALS (type_binfo) = BINFO_VIRTUALS (primary);
1744 /* Update the variant types of T. */
1746 void
1747 fixup_type_variants (tree t)
1749 tree variants;
1751 if (!t)
1752 return;
1754 for (variants = TYPE_NEXT_VARIANT (t);
1755 variants;
1756 variants = TYPE_NEXT_VARIANT (variants))
1758 /* These fields are in the _TYPE part of the node, not in
1759 the TYPE_LANG_SPECIFIC component, so they are not shared. */
1760 TYPE_HAS_USER_CONSTRUCTOR (variants) = TYPE_HAS_USER_CONSTRUCTOR (t);
1761 TYPE_NEEDS_CONSTRUCTING (variants) = TYPE_NEEDS_CONSTRUCTING (t);
1762 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (variants)
1763 = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t);
1765 TYPE_POLYMORPHIC_P (variants) = TYPE_POLYMORPHIC_P (t);
1767 TYPE_BINFO (variants) = TYPE_BINFO (t);
1769 /* Copy whatever these are holding today. */
1770 TYPE_VFIELD (variants) = TYPE_VFIELD (t);
1771 TYPE_METHODS (variants) = TYPE_METHODS (t);
1772 TYPE_FIELDS (variants) = TYPE_FIELDS (t);
1776 /* Early variant fixups: we apply attributes at the beginning of the class
1777 definition, and we need to fix up any variants that have already been
1778 made via elaborated-type-specifier so that check_qualified_type works. */
1780 void
1781 fixup_attribute_variants (tree t)
1783 tree variants;
1785 if (!t)
1786 return;
1788 for (variants = TYPE_NEXT_VARIANT (t);
1789 variants;
1790 variants = TYPE_NEXT_VARIANT (variants))
1792 /* These are the two fields that check_qualified_type looks at and
1793 are affected by attributes. */
1794 TYPE_ATTRIBUTES (variants) = TYPE_ATTRIBUTES (t);
1795 TYPE_ALIGN (variants) = TYPE_ALIGN (t);
1799 /* Set memoizing fields and bits of T (and its variants) for later
1800 use. */
1802 static void
1803 finish_struct_bits (tree t)
1805 /* Fix up variants (if any). */
1806 fixup_type_variants (t);
1808 if (BINFO_N_BASE_BINFOS (TYPE_BINFO (t)) && TYPE_POLYMORPHIC_P (t))
1809 /* For a class w/o baseclasses, 'finish_struct' has set
1810 CLASSTYPE_PURE_VIRTUALS correctly (by definition).
1811 Similarly for a class whose base classes do not have vtables.
1812 When neither of these is true, we might have removed abstract
1813 virtuals (by providing a definition), added some (by declaring
1814 new ones), or redeclared ones from a base class. We need to
1815 recalculate what's really an abstract virtual at this point (by
1816 looking in the vtables). */
1817 get_pure_virtuals (t);
1819 /* If this type has a copy constructor or a destructor, force its
1820 mode to be BLKmode, and force its TREE_ADDRESSABLE bit to be
1821 nonzero. This will cause it to be passed by invisible reference
1822 and prevent it from being returned in a register. */
1823 if (type_has_nontrivial_copy_init (t)
1824 || TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t))
1826 tree variants;
1827 DECL_MODE (TYPE_MAIN_DECL (t)) = BLKmode;
1828 for (variants = t; variants; variants = TYPE_NEXT_VARIANT (variants))
1830 SET_TYPE_MODE (variants, BLKmode);
1831 TREE_ADDRESSABLE (variants) = 1;
1836 /* Issue warnings about T having private constructors, but no friends,
1837 and so forth.
1839 HAS_NONPRIVATE_METHOD is nonzero if T has any non-private methods or
1840 static members. HAS_NONPRIVATE_STATIC_FN is nonzero if T has any
1841 non-private static member functions. */
1843 static void
1844 maybe_warn_about_overly_private_class (tree t)
1846 int has_member_fn = 0;
1847 int has_nonprivate_method = 0;
1848 tree fn;
1850 if (!warn_ctor_dtor_privacy
1851 /* If the class has friends, those entities might create and
1852 access instances, so we should not warn. */
1853 || (CLASSTYPE_FRIEND_CLASSES (t)
1854 || DECL_FRIENDLIST (TYPE_MAIN_DECL (t)))
1855 /* We will have warned when the template was declared; there's
1856 no need to warn on every instantiation. */
1857 || CLASSTYPE_TEMPLATE_INSTANTIATION (t))
1858 /* There's no reason to even consider warning about this
1859 class. */
1860 return;
1862 /* We only issue one warning, if more than one applies, because
1863 otherwise, on code like:
1865 class A {
1866 // Oops - forgot `public:'
1867 A();
1868 A(const A&);
1869 ~A();
1872 we warn several times about essentially the same problem. */
1874 /* Check to see if all (non-constructor, non-destructor) member
1875 functions are private. (Since there are no friends or
1876 non-private statics, we can't ever call any of the private member
1877 functions.) */
1878 for (fn = TYPE_METHODS (t); fn; fn = DECL_CHAIN (fn))
1879 /* We're not interested in compiler-generated methods; they don't
1880 provide any way to call private members. */
1881 if (!DECL_ARTIFICIAL (fn))
1883 if (!TREE_PRIVATE (fn))
1885 if (DECL_STATIC_FUNCTION_P (fn))
1886 /* A non-private static member function is just like a
1887 friend; it can create and invoke private member
1888 functions, and be accessed without a class
1889 instance. */
1890 return;
1892 has_nonprivate_method = 1;
1893 /* Keep searching for a static member function. */
1895 else if (!DECL_CONSTRUCTOR_P (fn) && !DECL_DESTRUCTOR_P (fn))
1896 has_member_fn = 1;
1899 if (!has_nonprivate_method && has_member_fn)
1901 /* There are no non-private methods, and there's at least one
1902 private member function that isn't a constructor or
1903 destructor. (If all the private members are
1904 constructors/destructors we want to use the code below that
1905 issues error messages specifically referring to
1906 constructors/destructors.) */
1907 unsigned i;
1908 tree binfo = TYPE_BINFO (t);
1910 for (i = 0; i != BINFO_N_BASE_BINFOS (binfo); i++)
1911 if (BINFO_BASE_ACCESS (binfo, i) != access_private_node)
1913 has_nonprivate_method = 1;
1914 break;
1916 if (!has_nonprivate_method)
1918 warning (OPT_Wctor_dtor_privacy,
1919 "all member functions in class %qT are private", t);
1920 return;
1924 /* Even if some of the member functions are non-private, the class
1925 won't be useful for much if all the constructors or destructors
1926 are private: such an object can never be created or destroyed. */
1927 fn = CLASSTYPE_DESTRUCTORS (t);
1928 if (fn && TREE_PRIVATE (fn))
1930 warning (OPT_Wctor_dtor_privacy,
1931 "%q#T only defines a private destructor and has no friends",
1933 return;
1936 /* Warn about classes that have private constructors and no friends. */
1937 if (TYPE_HAS_USER_CONSTRUCTOR (t)
1938 /* Implicitly generated constructors are always public. */
1939 && (!CLASSTYPE_LAZY_DEFAULT_CTOR (t)
1940 || !CLASSTYPE_LAZY_COPY_CTOR (t)))
1942 int nonprivate_ctor = 0;
1944 /* If a non-template class does not define a copy
1945 constructor, one is defined for it, enabling it to avoid
1946 this warning. For a template class, this does not
1947 happen, and so we would normally get a warning on:
1949 template <class T> class C { private: C(); };
1951 To avoid this asymmetry, we check TYPE_HAS_COPY_CTOR. All
1952 complete non-template or fully instantiated classes have this
1953 flag set. */
1954 if (!TYPE_HAS_COPY_CTOR (t))
1955 nonprivate_ctor = 1;
1956 else
1957 for (fn = CLASSTYPE_CONSTRUCTORS (t); fn; fn = OVL_NEXT (fn))
1959 tree ctor = OVL_CURRENT (fn);
1960 /* Ideally, we wouldn't count copy constructors (or, in
1961 fact, any constructor that takes an argument of the
1962 class type as a parameter) because such things cannot
1963 be used to construct an instance of the class unless
1964 you already have one. But, for now at least, we're
1965 more generous. */
1966 if (! TREE_PRIVATE (ctor))
1968 nonprivate_ctor = 1;
1969 break;
1973 if (nonprivate_ctor == 0)
1975 warning (OPT_Wctor_dtor_privacy,
1976 "%q#T only defines private constructors and has no friends",
1978 return;
1983 static struct {
1984 gt_pointer_operator new_value;
1985 void *cookie;
1986 } resort_data;
1988 /* Comparison function to compare two TYPE_METHOD_VEC entries by name. */
1990 static int
1991 method_name_cmp (const void* m1_p, const void* m2_p)
1993 const tree *const m1 = (const tree *) m1_p;
1994 const tree *const m2 = (const tree *) m2_p;
1996 if (*m1 == NULL_TREE && *m2 == NULL_TREE)
1997 return 0;
1998 if (*m1 == NULL_TREE)
1999 return -1;
2000 if (*m2 == NULL_TREE)
2001 return 1;
2002 if (DECL_NAME (OVL_CURRENT (*m1)) < DECL_NAME (OVL_CURRENT (*m2)))
2003 return -1;
2004 return 1;
2007 /* This routine compares two fields like method_name_cmp but using the
2008 pointer operator in resort_field_decl_data. */
2010 static int
2011 resort_method_name_cmp (const void* m1_p, const void* m2_p)
2013 const tree *const m1 = (const tree *) m1_p;
2014 const tree *const m2 = (const tree *) m2_p;
2015 if (*m1 == NULL_TREE && *m2 == NULL_TREE)
2016 return 0;
2017 if (*m1 == NULL_TREE)
2018 return -1;
2019 if (*m2 == NULL_TREE)
2020 return 1;
2022 tree d1 = DECL_NAME (OVL_CURRENT (*m1));
2023 tree d2 = DECL_NAME (OVL_CURRENT (*m2));
2024 resort_data.new_value (&d1, resort_data.cookie);
2025 resort_data.new_value (&d2, resort_data.cookie);
2026 if (d1 < d2)
2027 return -1;
2029 return 1;
2032 /* Resort TYPE_METHOD_VEC because pointers have been reordered. */
2034 void
2035 resort_type_method_vec (void* obj,
2036 void* /*orig_obj*/,
2037 gt_pointer_operator new_value,
2038 void* cookie)
2040 vec<tree, va_gc> *method_vec = (vec<tree, va_gc> *) obj;
2041 int len = vec_safe_length (method_vec);
2042 size_t slot;
2043 tree fn;
2045 /* The type conversion ops have to live at the front of the vec, so we
2046 can't sort them. */
2047 for (slot = CLASSTYPE_FIRST_CONVERSION_SLOT;
2048 vec_safe_iterate (method_vec, slot, &fn);
2049 ++slot)
2050 if (!DECL_CONV_FN_P (OVL_CURRENT (fn)))
2051 break;
2053 if (len - slot > 1)
2055 resort_data.new_value = new_value;
2056 resort_data.cookie = cookie;
2057 qsort (method_vec->address () + slot, len - slot, sizeof (tree),
2058 resort_method_name_cmp);
2062 /* Warn about duplicate methods in fn_fields.
2064 Sort methods that are not special (i.e., constructors, destructors,
2065 and type conversion operators) so that we can find them faster in
2066 search. */
2068 static void
2069 finish_struct_methods (tree t)
2071 tree fn_fields;
2072 vec<tree, va_gc> *method_vec;
2073 int slot, len;
2075 method_vec = CLASSTYPE_METHOD_VEC (t);
2076 if (!method_vec)
2077 return;
2079 len = method_vec->length ();
2081 /* Clear DECL_IN_AGGR_P for all functions. */
2082 for (fn_fields = TYPE_METHODS (t); fn_fields;
2083 fn_fields = DECL_CHAIN (fn_fields))
2084 DECL_IN_AGGR_P (fn_fields) = 0;
2086 /* Issue warnings about private constructors and such. If there are
2087 no methods, then some public defaults are generated. */
2088 maybe_warn_about_overly_private_class (t);
2090 /* The type conversion ops have to live at the front of the vec, so we
2091 can't sort them. */
2092 for (slot = CLASSTYPE_FIRST_CONVERSION_SLOT;
2093 method_vec->iterate (slot, &fn_fields);
2094 ++slot)
2095 if (!DECL_CONV_FN_P (OVL_CURRENT (fn_fields)))
2096 break;
2097 if (len - slot > 1)
2098 qsort (method_vec->address () + slot,
2099 len-slot, sizeof (tree), method_name_cmp);
2102 /* Make BINFO's vtable have N entries, including RTTI entries,
2103 vbase and vcall offsets, etc. Set its type and call the back end
2104 to lay it out. */
2106 static void
2107 layout_vtable_decl (tree binfo, int n)
2109 tree atype;
2110 tree vtable;
2112 atype = build_array_of_n_type (vtable_entry_type, n);
2113 layout_type (atype);
2115 /* We may have to grow the vtable. */
2116 vtable = get_vtbl_decl_for_binfo (binfo);
2117 if (!same_type_p (TREE_TYPE (vtable), atype))
2119 TREE_TYPE (vtable) = atype;
2120 DECL_SIZE (vtable) = DECL_SIZE_UNIT (vtable) = NULL_TREE;
2121 layout_decl (vtable, 0);
2125 /* True iff FNDECL and BASE_FNDECL (both non-static member functions)
2126 have the same signature. */
2129 same_signature_p (const_tree fndecl, const_tree base_fndecl)
2131 /* One destructor overrides another if they are the same kind of
2132 destructor. */
2133 if (DECL_DESTRUCTOR_P (base_fndecl) && DECL_DESTRUCTOR_P (fndecl)
2134 && special_function_p (base_fndecl) == special_function_p (fndecl))
2135 return 1;
2136 /* But a non-destructor never overrides a destructor, nor vice
2137 versa, nor do different kinds of destructors override
2138 one-another. For example, a complete object destructor does not
2139 override a deleting destructor. */
2140 if (DECL_DESTRUCTOR_P (base_fndecl) || DECL_DESTRUCTOR_P (fndecl))
2141 return 0;
2143 if (DECL_NAME (fndecl) == DECL_NAME (base_fndecl)
2144 || (DECL_CONV_FN_P (fndecl)
2145 && DECL_CONV_FN_P (base_fndecl)
2146 && same_type_p (DECL_CONV_FN_TYPE (fndecl),
2147 DECL_CONV_FN_TYPE (base_fndecl))))
2149 tree fntype = TREE_TYPE (fndecl);
2150 tree base_fntype = TREE_TYPE (base_fndecl);
2151 if (type_memfn_quals (fntype) == type_memfn_quals (base_fntype)
2152 && type_memfn_rqual (fntype) == type_memfn_rqual (base_fntype)
2153 && compparms (FUNCTION_FIRST_USER_PARMTYPE (fndecl),
2154 FUNCTION_FIRST_USER_PARMTYPE (base_fndecl)))
2155 return 1;
2157 return 0;
2160 /* Returns TRUE if DERIVED is a binfo containing the binfo BASE as a
2161 subobject. */
2163 static bool
2164 base_derived_from (tree derived, tree base)
2166 tree probe;
2168 for (probe = base; probe; probe = BINFO_INHERITANCE_CHAIN (probe))
2170 if (probe == derived)
2171 return true;
2172 else if (BINFO_VIRTUAL_P (probe))
2173 /* If we meet a virtual base, we can't follow the inheritance
2174 any more. See if the complete type of DERIVED contains
2175 such a virtual base. */
2176 return (binfo_for_vbase (BINFO_TYPE (probe), BINFO_TYPE (derived))
2177 != NULL_TREE);
2179 return false;
2182 typedef struct find_final_overrider_data_s {
2183 /* The function for which we are trying to find a final overrider. */
2184 tree fn;
2185 /* The base class in which the function was declared. */
2186 tree declaring_base;
2187 /* The candidate overriders. */
2188 tree candidates;
2189 /* Path to most derived. */
2190 vec<tree> path;
2191 } find_final_overrider_data;
2193 /* Add the overrider along the current path to FFOD->CANDIDATES.
2194 Returns true if an overrider was found; false otherwise. */
2196 static bool
2197 dfs_find_final_overrider_1 (tree binfo,
2198 find_final_overrider_data *ffod,
2199 unsigned depth)
2201 tree method;
2203 /* If BINFO is not the most derived type, try a more derived class.
2204 A definition there will overrider a definition here. */
2205 if (depth)
2207 depth--;
2208 if (dfs_find_final_overrider_1
2209 (ffod->path[depth], ffod, depth))
2210 return true;
2213 method = look_for_overrides_here (BINFO_TYPE (binfo), ffod->fn);
2214 if (method)
2216 tree *candidate = &ffod->candidates;
2218 /* Remove any candidates overridden by this new function. */
2219 while (*candidate)
2221 /* If *CANDIDATE overrides METHOD, then METHOD
2222 cannot override anything else on the list. */
2223 if (base_derived_from (TREE_VALUE (*candidate), binfo))
2224 return true;
2225 /* If METHOD overrides *CANDIDATE, remove *CANDIDATE. */
2226 if (base_derived_from (binfo, TREE_VALUE (*candidate)))
2227 *candidate = TREE_CHAIN (*candidate);
2228 else
2229 candidate = &TREE_CHAIN (*candidate);
2232 /* Add the new function. */
2233 ffod->candidates = tree_cons (method, binfo, ffod->candidates);
2234 return true;
2237 return false;
2240 /* Called from find_final_overrider via dfs_walk. */
2242 static tree
2243 dfs_find_final_overrider_pre (tree binfo, void *data)
2245 find_final_overrider_data *ffod = (find_final_overrider_data *) data;
2247 if (binfo == ffod->declaring_base)
2248 dfs_find_final_overrider_1 (binfo, ffod, ffod->path.length ());
2249 ffod->path.safe_push (binfo);
2251 return NULL_TREE;
2254 static tree
2255 dfs_find_final_overrider_post (tree /*binfo*/, void *data)
2257 find_final_overrider_data *ffod = (find_final_overrider_data *) data;
2258 ffod->path.pop ();
2260 return NULL_TREE;
2263 /* Returns a TREE_LIST whose TREE_PURPOSE is the final overrider for
2264 FN and whose TREE_VALUE is the binfo for the base where the
2265 overriding occurs. BINFO (in the hierarchy dominated by the binfo
2266 DERIVED) is the base object in which FN is declared. */
2268 static tree
2269 find_final_overrider (tree derived, tree binfo, tree fn)
2271 find_final_overrider_data ffod;
2273 /* Getting this right is a little tricky. This is valid:
2275 struct S { virtual void f (); };
2276 struct T { virtual void f (); };
2277 struct U : public S, public T { };
2279 even though calling `f' in `U' is ambiguous. But,
2281 struct R { virtual void f(); };
2282 struct S : virtual public R { virtual void f (); };
2283 struct T : virtual public R { virtual void f (); };
2284 struct U : public S, public T { };
2286 is not -- there's no way to decide whether to put `S::f' or
2287 `T::f' in the vtable for `R'.
2289 The solution is to look at all paths to BINFO. If we find
2290 different overriders along any two, then there is a problem. */
2291 if (DECL_THUNK_P (fn))
2292 fn = THUNK_TARGET (fn);
2294 /* Determine the depth of the hierarchy. */
2295 ffod.fn = fn;
2296 ffod.declaring_base = binfo;
2297 ffod.candidates = NULL_TREE;
2298 ffod.path.create (30);
2300 dfs_walk_all (derived, dfs_find_final_overrider_pre,
2301 dfs_find_final_overrider_post, &ffod);
2303 ffod.path.release ();
2305 /* If there was no winner, issue an error message. */
2306 if (!ffod.candidates || TREE_CHAIN (ffod.candidates))
2307 return error_mark_node;
2309 return ffod.candidates;
2312 /* Return the index of the vcall offset for FN when TYPE is used as a
2313 virtual base. */
2315 static tree
2316 get_vcall_index (tree fn, tree type)
2318 vec<tree_pair_s, va_gc> *indices = CLASSTYPE_VCALL_INDICES (type);
2319 tree_pair_p p;
2320 unsigned ix;
2322 FOR_EACH_VEC_SAFE_ELT (indices, ix, p)
2323 if ((DECL_DESTRUCTOR_P (fn) && DECL_DESTRUCTOR_P (p->purpose))
2324 || same_signature_p (fn, p->purpose))
2325 return p->value;
2327 /* There should always be an appropriate index. */
2328 gcc_unreachable ();
2331 /* Update an entry in the vtable for BINFO, which is in the hierarchy
2332 dominated by T. FN is the old function; VIRTUALS points to the
2333 corresponding position in the new BINFO_VIRTUALS list. IX is the index
2334 of that entry in the list. */
2336 static void
2337 update_vtable_entry_for_fn (tree t, tree binfo, tree fn, tree* virtuals,
2338 unsigned ix)
2340 tree b;
2341 tree overrider;
2342 tree delta;
2343 tree virtual_base;
2344 tree first_defn;
2345 tree overrider_fn, overrider_target;
2346 tree target_fn = DECL_THUNK_P (fn) ? THUNK_TARGET (fn) : fn;
2347 tree over_return, base_return;
2348 bool lost = false;
2350 /* Find the nearest primary base (possibly binfo itself) which defines
2351 this function; this is the class the caller will convert to when
2352 calling FN through BINFO. */
2353 for (b = binfo; ; b = get_primary_binfo (b))
2355 gcc_assert (b);
2356 if (look_for_overrides_here (BINFO_TYPE (b), target_fn))
2357 break;
2359 /* The nearest definition is from a lost primary. */
2360 if (BINFO_LOST_PRIMARY_P (b))
2361 lost = true;
2363 first_defn = b;
2365 /* Find the final overrider. */
2366 overrider = find_final_overrider (TYPE_BINFO (t), b, target_fn);
2367 if (overrider == error_mark_node)
2369 error ("no unique final overrider for %qD in %qT", target_fn, t);
2370 return;
2372 overrider_target = overrider_fn = TREE_PURPOSE (overrider);
2374 /* Check for adjusting covariant return types. */
2375 over_return = TREE_TYPE (TREE_TYPE (overrider_target));
2376 base_return = TREE_TYPE (TREE_TYPE (target_fn));
2378 if (POINTER_TYPE_P (over_return)
2379 && TREE_CODE (over_return) == TREE_CODE (base_return)
2380 && CLASS_TYPE_P (TREE_TYPE (over_return))
2381 && CLASS_TYPE_P (TREE_TYPE (base_return))
2382 /* If the overrider is invalid, don't even try. */
2383 && !DECL_INVALID_OVERRIDER_P (overrider_target))
2385 /* If FN is a covariant thunk, we must figure out the adjustment
2386 to the final base FN was converting to. As OVERRIDER_TARGET might
2387 also be converting to the return type of FN, we have to
2388 combine the two conversions here. */
2389 tree fixed_offset, virtual_offset;
2391 over_return = TREE_TYPE (over_return);
2392 base_return = TREE_TYPE (base_return);
2394 if (DECL_THUNK_P (fn))
2396 gcc_assert (DECL_RESULT_THUNK_P (fn));
2397 fixed_offset = ssize_int (THUNK_FIXED_OFFSET (fn));
2398 virtual_offset = THUNK_VIRTUAL_OFFSET (fn);
2400 else
2401 fixed_offset = virtual_offset = NULL_TREE;
2403 if (virtual_offset)
2404 /* Find the equivalent binfo within the return type of the
2405 overriding function. We will want the vbase offset from
2406 there. */
2407 virtual_offset = binfo_for_vbase (BINFO_TYPE (virtual_offset),
2408 over_return);
2409 else if (!same_type_ignoring_top_level_qualifiers_p
2410 (over_return, base_return))
2412 /* There was no existing virtual thunk (which takes
2413 precedence). So find the binfo of the base function's
2414 return type within the overriding function's return type.
2415 We cannot call lookup base here, because we're inside a
2416 dfs_walk, and will therefore clobber the BINFO_MARKED
2417 flags. Fortunately we know the covariancy is valid (it
2418 has already been checked), so we can just iterate along
2419 the binfos, which have been chained in inheritance graph
2420 order. Of course it is lame that we have to repeat the
2421 search here anyway -- we should really be caching pieces
2422 of the vtable and avoiding this repeated work. */
2423 tree thunk_binfo, base_binfo;
2425 /* Find the base binfo within the overriding function's
2426 return type. We will always find a thunk_binfo, except
2427 when the covariancy is invalid (which we will have
2428 already diagnosed). */
2429 for (base_binfo = TYPE_BINFO (base_return),
2430 thunk_binfo = TYPE_BINFO (over_return);
2431 thunk_binfo;
2432 thunk_binfo = TREE_CHAIN (thunk_binfo))
2433 if (SAME_BINFO_TYPE_P (BINFO_TYPE (thunk_binfo),
2434 BINFO_TYPE (base_binfo)))
2435 break;
2437 /* See if virtual inheritance is involved. */
2438 for (virtual_offset = thunk_binfo;
2439 virtual_offset;
2440 virtual_offset = BINFO_INHERITANCE_CHAIN (virtual_offset))
2441 if (BINFO_VIRTUAL_P (virtual_offset))
2442 break;
2444 if (virtual_offset
2445 || (thunk_binfo && !BINFO_OFFSET_ZEROP (thunk_binfo)))
2447 tree offset = convert (ssizetype, BINFO_OFFSET (thunk_binfo));
2449 if (virtual_offset)
2451 /* We convert via virtual base. Adjust the fixed
2452 offset to be from there. */
2453 offset =
2454 size_diffop (offset,
2455 convert (ssizetype,
2456 BINFO_OFFSET (virtual_offset)));
2458 if (fixed_offset)
2459 /* There was an existing fixed offset, this must be
2460 from the base just converted to, and the base the
2461 FN was thunking to. */
2462 fixed_offset = size_binop (PLUS_EXPR, fixed_offset, offset);
2463 else
2464 fixed_offset = offset;
2468 if (fixed_offset || virtual_offset)
2469 /* Replace the overriding function with a covariant thunk. We
2470 will emit the overriding function in its own slot as
2471 well. */
2472 overrider_fn = make_thunk (overrider_target, /*this_adjusting=*/0,
2473 fixed_offset, virtual_offset);
2475 else
2476 gcc_assert (DECL_INVALID_OVERRIDER_P (overrider_target) ||
2477 !DECL_THUNK_P (fn));
2479 /* If we need a covariant thunk, then we may need to adjust first_defn.
2480 The ABI specifies that the thunks emitted with a function are
2481 determined by which bases the function overrides, so we need to be
2482 sure that we're using a thunk for some overridden base; even if we
2483 know that the necessary this adjustment is zero, there may not be an
2484 appropriate zero-this-adjusment thunk for us to use since thunks for
2485 overriding virtual bases always use the vcall offset.
2487 Furthermore, just choosing any base that overrides this function isn't
2488 quite right, as this slot won't be used for calls through a type that
2489 puts a covariant thunk here. Calling the function through such a type
2490 will use a different slot, and that slot is the one that determines
2491 the thunk emitted for that base.
2493 So, keep looking until we find the base that we're really overriding
2494 in this slot: the nearest primary base that doesn't use a covariant
2495 thunk in this slot. */
2496 if (overrider_target != overrider_fn)
2498 if (BINFO_TYPE (b) == DECL_CONTEXT (overrider_target))
2499 /* We already know that the overrider needs a covariant thunk. */
2500 b = get_primary_binfo (b);
2501 for (; ; b = get_primary_binfo (b))
2503 tree main_binfo = TYPE_BINFO (BINFO_TYPE (b));
2504 tree bv = chain_index (ix, BINFO_VIRTUALS (main_binfo));
2505 if (!DECL_THUNK_P (TREE_VALUE (bv)))
2506 break;
2507 if (BINFO_LOST_PRIMARY_P (b))
2508 lost = true;
2510 first_defn = b;
2513 /* Assume that we will produce a thunk that convert all the way to
2514 the final overrider, and not to an intermediate virtual base. */
2515 virtual_base = NULL_TREE;
2517 /* See if we can convert to an intermediate virtual base first, and then
2518 use the vcall offset located there to finish the conversion. */
2519 for (; b; b = BINFO_INHERITANCE_CHAIN (b))
2521 /* If we find the final overrider, then we can stop
2522 walking. */
2523 if (SAME_BINFO_TYPE_P (BINFO_TYPE (b),
2524 BINFO_TYPE (TREE_VALUE (overrider))))
2525 break;
2527 /* If we find a virtual base, and we haven't yet found the
2528 overrider, then there is a virtual base between the
2529 declaring base (first_defn) and the final overrider. */
2530 if (BINFO_VIRTUAL_P (b))
2532 virtual_base = b;
2533 break;
2537 /* Compute the constant adjustment to the `this' pointer. The
2538 `this' pointer, when this function is called, will point at BINFO
2539 (or one of its primary bases, which are at the same offset). */
2540 if (virtual_base)
2541 /* The `this' pointer needs to be adjusted from the declaration to
2542 the nearest virtual base. */
2543 delta = size_diffop_loc (input_location,
2544 convert (ssizetype, BINFO_OFFSET (virtual_base)),
2545 convert (ssizetype, BINFO_OFFSET (first_defn)));
2546 else if (lost)
2547 /* If the nearest definition is in a lost primary, we don't need an
2548 entry in our vtable. Except possibly in a constructor vtable,
2549 if we happen to get our primary back. In that case, the offset
2550 will be zero, as it will be a primary base. */
2551 delta = size_zero_node;
2552 else
2553 /* The `this' pointer needs to be adjusted from pointing to
2554 BINFO to pointing at the base where the final overrider
2555 appears. */
2556 delta = size_diffop_loc (input_location,
2557 convert (ssizetype,
2558 BINFO_OFFSET (TREE_VALUE (overrider))),
2559 convert (ssizetype, BINFO_OFFSET (binfo)));
2561 modify_vtable_entry (t, binfo, overrider_fn, delta, virtuals);
2563 if (virtual_base)
2564 BV_VCALL_INDEX (*virtuals)
2565 = get_vcall_index (overrider_target, BINFO_TYPE (virtual_base));
2566 else
2567 BV_VCALL_INDEX (*virtuals) = NULL_TREE;
2569 BV_LOST_PRIMARY (*virtuals) = lost;
2572 /* Called from modify_all_vtables via dfs_walk. */
2574 static tree
2575 dfs_modify_vtables (tree binfo, void* data)
2577 tree t = (tree) data;
2578 tree virtuals;
2579 tree old_virtuals;
2580 unsigned ix;
2582 if (!TYPE_CONTAINS_VPTR_P (BINFO_TYPE (binfo)))
2583 /* A base without a vtable needs no modification, and its bases
2584 are uninteresting. */
2585 return dfs_skip_bases;
2587 if (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), t)
2588 && !CLASSTYPE_HAS_PRIMARY_BASE_P (t))
2589 /* Don't do the primary vtable, if it's new. */
2590 return NULL_TREE;
2592 if (BINFO_PRIMARY_P (binfo) && !BINFO_VIRTUAL_P (binfo))
2593 /* There's no need to modify the vtable for a non-virtual primary
2594 base; we're not going to use that vtable anyhow. We do still
2595 need to do this for virtual primary bases, as they could become
2596 non-primary in a construction vtable. */
2597 return NULL_TREE;
2599 make_new_vtable (t, binfo);
2601 /* Now, go through each of the virtual functions in the virtual
2602 function table for BINFO. Find the final overrider, and update
2603 the BINFO_VIRTUALS list appropriately. */
2604 for (ix = 0, virtuals = BINFO_VIRTUALS (binfo),
2605 old_virtuals = BINFO_VIRTUALS (TYPE_BINFO (BINFO_TYPE (binfo)));
2606 virtuals;
2607 ix++, virtuals = TREE_CHAIN (virtuals),
2608 old_virtuals = TREE_CHAIN (old_virtuals))
2609 update_vtable_entry_for_fn (t,
2610 binfo,
2611 BV_FN (old_virtuals),
2612 &virtuals, ix);
2614 return NULL_TREE;
2617 /* Update all of the primary and secondary vtables for T. Create new
2618 vtables as required, and initialize their RTTI information. Each
2619 of the functions in VIRTUALS is declared in T and may override a
2620 virtual function from a base class; find and modify the appropriate
2621 entries to point to the overriding functions. Returns a list, in
2622 declaration order, of the virtual functions that are declared in T,
2623 but do not appear in the primary base class vtable, and which
2624 should therefore be appended to the end of the vtable for T. */
2626 static tree
2627 modify_all_vtables (tree t, tree virtuals)
2629 tree binfo = TYPE_BINFO (t);
2630 tree *fnsp;
2632 /* Mangle the vtable name before entering dfs_walk (c++/51884). */
2633 if (TYPE_CONTAINS_VPTR_P (t))
2634 get_vtable_decl (t, false);
2636 /* Update all of the vtables. */
2637 dfs_walk_once (binfo, dfs_modify_vtables, NULL, t);
2639 /* Add virtual functions not already in our primary vtable. These
2640 will be both those introduced by this class, and those overridden
2641 from secondary bases. It does not include virtuals merely
2642 inherited from secondary bases. */
2643 for (fnsp = &virtuals; *fnsp; )
2645 tree fn = TREE_VALUE (*fnsp);
2647 if (!value_member (fn, BINFO_VIRTUALS (binfo))
2648 || DECL_VINDEX (fn) == error_mark_node)
2650 /* We don't need to adjust the `this' pointer when
2651 calling this function. */
2652 BV_DELTA (*fnsp) = integer_zero_node;
2653 BV_VCALL_INDEX (*fnsp) = NULL_TREE;
2655 /* This is a function not already in our vtable. Keep it. */
2656 fnsp = &TREE_CHAIN (*fnsp);
2658 else
2659 /* We've already got an entry for this function. Skip it. */
2660 *fnsp = TREE_CHAIN (*fnsp);
2663 return virtuals;
2666 /* Get the base virtual function declarations in T that have the
2667 indicated NAME. */
2669 static tree
2670 get_basefndecls (tree name, tree t)
2672 tree methods;
2673 tree base_fndecls = NULL_TREE;
2674 int n_baseclasses = BINFO_N_BASE_BINFOS (TYPE_BINFO (t));
2675 int i;
2677 /* Find virtual functions in T with the indicated NAME. */
2678 i = lookup_fnfields_1 (t, name);
2679 if (i != -1)
2680 for (methods = (*CLASSTYPE_METHOD_VEC (t))[i];
2681 methods;
2682 methods = OVL_NEXT (methods))
2684 tree method = OVL_CURRENT (methods);
2686 if (TREE_CODE (method) == FUNCTION_DECL
2687 && DECL_VINDEX (method))
2688 base_fndecls = tree_cons (NULL_TREE, method, base_fndecls);
2691 if (base_fndecls)
2692 return base_fndecls;
2694 for (i = 0; i < n_baseclasses; i++)
2696 tree basetype = BINFO_TYPE (BINFO_BASE_BINFO (TYPE_BINFO (t), i));
2697 base_fndecls = chainon (get_basefndecls (name, basetype),
2698 base_fndecls);
2701 return base_fndecls;
2704 /* If this declaration supersedes the declaration of
2705 a method declared virtual in the base class, then
2706 mark this field as being virtual as well. */
2708 void
2709 check_for_override (tree decl, tree ctype)
2711 bool overrides_found = false;
2712 if (TREE_CODE (decl) == TEMPLATE_DECL)
2713 /* In [temp.mem] we have:
2715 A specialization of a member function template does not
2716 override a virtual function from a base class. */
2717 return;
2718 if ((DECL_DESTRUCTOR_P (decl)
2719 || IDENTIFIER_VIRTUAL_P (DECL_NAME (decl))
2720 || DECL_CONV_FN_P (decl))
2721 && look_for_overrides (ctype, decl)
2722 && !DECL_STATIC_FUNCTION_P (decl))
2723 /* Set DECL_VINDEX to a value that is neither an INTEGER_CST nor
2724 the error_mark_node so that we know it is an overriding
2725 function. */
2727 DECL_VINDEX (decl) = decl;
2728 overrides_found = true;
2731 if (DECL_VIRTUAL_P (decl))
2733 if (!DECL_VINDEX (decl))
2734 DECL_VINDEX (decl) = error_mark_node;
2735 IDENTIFIER_VIRTUAL_P (DECL_NAME (decl)) = 1;
2736 if (DECL_DESTRUCTOR_P (decl))
2737 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (ctype) = true;
2739 else if (DECL_FINAL_P (decl))
2740 error ("%q+#D marked final, but is not virtual", decl);
2741 if (DECL_OVERRIDE_P (decl) && !overrides_found)
2742 error ("%q+#D marked override, but does not override", decl);
2745 /* Warn about hidden virtual functions that are not overridden in t.
2746 We know that constructors and destructors don't apply. */
2748 static void
2749 warn_hidden (tree t)
2751 vec<tree, va_gc> *method_vec = CLASSTYPE_METHOD_VEC (t);
2752 tree fns;
2753 size_t i;
2755 /* We go through each separately named virtual function. */
2756 for (i = CLASSTYPE_FIRST_CONVERSION_SLOT;
2757 vec_safe_iterate (method_vec, i, &fns);
2758 ++i)
2760 tree fn;
2761 tree name;
2762 tree fndecl;
2763 tree base_fndecls;
2764 tree base_binfo;
2765 tree binfo;
2766 int j;
2768 /* All functions in this slot in the CLASSTYPE_METHOD_VEC will
2769 have the same name. Figure out what name that is. */
2770 name = DECL_NAME (OVL_CURRENT (fns));
2771 /* There are no possibly hidden functions yet. */
2772 base_fndecls = NULL_TREE;
2773 /* Iterate through all of the base classes looking for possibly
2774 hidden functions. */
2775 for (binfo = TYPE_BINFO (t), j = 0;
2776 BINFO_BASE_ITERATE (binfo, j, base_binfo); j++)
2778 tree basetype = BINFO_TYPE (base_binfo);
2779 base_fndecls = chainon (get_basefndecls (name, basetype),
2780 base_fndecls);
2783 /* If there are no functions to hide, continue. */
2784 if (!base_fndecls)
2785 continue;
2787 /* Remove any overridden functions. */
2788 for (fn = fns; fn; fn = OVL_NEXT (fn))
2790 fndecl = OVL_CURRENT (fn);
2791 if (DECL_VINDEX (fndecl))
2793 tree *prev = &base_fndecls;
2795 while (*prev)
2796 /* If the method from the base class has the same
2797 signature as the method from the derived class, it
2798 has been overridden. */
2799 if (same_signature_p (fndecl, TREE_VALUE (*prev)))
2800 *prev = TREE_CHAIN (*prev);
2801 else
2802 prev = &TREE_CHAIN (*prev);
2806 /* Now give a warning for all base functions without overriders,
2807 as they are hidden. */
2808 while (base_fndecls)
2810 /* Here we know it is a hider, and no overrider exists. */
2811 warning (OPT_Woverloaded_virtual, "%q+D was hidden", TREE_VALUE (base_fndecls));
2812 warning (OPT_Woverloaded_virtual, " by %q+D", fns);
2813 base_fndecls = TREE_CHAIN (base_fndecls);
2818 /* Recursive helper for finish_struct_anon. */
2820 static void
2821 finish_struct_anon_r (tree field, bool complain)
2823 bool is_union = TREE_CODE (TREE_TYPE (field)) == UNION_TYPE;
2824 tree elt = TYPE_FIELDS (TREE_TYPE (field));
2825 for (; elt; elt = DECL_CHAIN (elt))
2827 /* We're generally only interested in entities the user
2828 declared, but we also find nested classes by noticing
2829 the TYPE_DECL that we create implicitly. You're
2830 allowed to put one anonymous union inside another,
2831 though, so we explicitly tolerate that. We use
2832 TYPE_ANONYMOUS_P rather than ANON_AGGR_TYPE_P so that
2833 we also allow unnamed types used for defining fields. */
2834 if (DECL_ARTIFICIAL (elt)
2835 && (!DECL_IMPLICIT_TYPEDEF_P (elt)
2836 || TYPE_ANONYMOUS_P (TREE_TYPE (elt))))
2837 continue;
2839 if (TREE_CODE (elt) != FIELD_DECL)
2841 if (complain)
2843 if (is_union)
2844 permerror (input_location,
2845 "%q+#D invalid; an anonymous union can "
2846 "only have non-static data members", elt);
2847 else
2848 permerror (input_location,
2849 "%q+#D invalid; an anonymous struct can "
2850 "only have non-static data members", elt);
2852 continue;
2855 if (complain)
2857 if (TREE_PRIVATE (elt))
2859 if (is_union)
2860 permerror (input_location,
2861 "private member %q+#D in anonymous union", elt);
2862 else
2863 permerror (input_location,
2864 "private member %q+#D in anonymous struct", elt);
2866 else if (TREE_PROTECTED (elt))
2868 if (is_union)
2869 permerror (input_location,
2870 "protected member %q+#D in anonymous union", elt);
2871 else
2872 permerror (input_location,
2873 "protected member %q+#D in anonymous struct", elt);
2877 TREE_PRIVATE (elt) = TREE_PRIVATE (field);
2878 TREE_PROTECTED (elt) = TREE_PROTECTED (field);
2880 /* Recurse into the anonymous aggregates to handle correctly
2881 access control (c++/24926):
2883 class A {
2884 union {
2885 union {
2886 int i;
2891 int j=A().i; */
2892 if (DECL_NAME (elt) == NULL_TREE
2893 && ANON_AGGR_TYPE_P (TREE_TYPE (elt)))
2894 finish_struct_anon_r (elt, /*complain=*/false);
2898 /* Check for things that are invalid. There are probably plenty of other
2899 things we should check for also. */
2901 static void
2902 finish_struct_anon (tree t)
2904 for (tree field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
2906 if (TREE_STATIC (field))
2907 continue;
2908 if (TREE_CODE (field) != FIELD_DECL)
2909 continue;
2911 if (DECL_NAME (field) == NULL_TREE
2912 && ANON_AGGR_TYPE_P (TREE_TYPE (field)))
2913 finish_struct_anon_r (field, /*complain=*/true);
2917 /* Add T to CLASSTYPE_DECL_LIST of current_class_type which
2918 will be used later during class template instantiation.
2919 When FRIEND_P is zero, T can be a static member data (VAR_DECL),
2920 a non-static member data (FIELD_DECL), a member function
2921 (FUNCTION_DECL), a nested type (RECORD_TYPE, ENUM_TYPE),
2922 a typedef (TYPE_DECL) or a member class template (TEMPLATE_DECL)
2923 When FRIEND_P is nonzero, T is either a friend class
2924 (RECORD_TYPE, TEMPLATE_DECL) or a friend function
2925 (FUNCTION_DECL, TEMPLATE_DECL). */
2927 void
2928 maybe_add_class_template_decl_list (tree type, tree t, int friend_p)
2930 /* Save some memory by not creating TREE_LIST if TYPE is not template. */
2931 if (CLASSTYPE_TEMPLATE_INFO (type))
2932 CLASSTYPE_DECL_LIST (type)
2933 = tree_cons (friend_p ? NULL_TREE : type,
2934 t, CLASSTYPE_DECL_LIST (type));
2937 /* This function is called from declare_virt_assop_and_dtor via
2938 dfs_walk_all.
2940 DATA is a type that direcly or indirectly inherits the base
2941 represented by BINFO. If BINFO contains a virtual assignment [copy
2942 assignment or move assigment] operator or a virtual constructor,
2943 declare that function in DATA if it hasn't been already declared. */
2945 static tree
2946 dfs_declare_virt_assop_and_dtor (tree binfo, void *data)
2948 tree bv, fn, t = (tree)data;
2949 tree opname = ansi_assopname (NOP_EXPR);
2951 gcc_assert (t && CLASS_TYPE_P (t));
2952 gcc_assert (binfo && TREE_CODE (binfo) == TREE_BINFO);
2954 if (!TYPE_CONTAINS_VPTR_P (BINFO_TYPE (binfo)))
2955 /* A base without a vtable needs no modification, and its bases
2956 are uninteresting. */
2957 return dfs_skip_bases;
2959 if (BINFO_PRIMARY_P (binfo))
2960 /* If this is a primary base, then we have already looked at the
2961 virtual functions of its vtable. */
2962 return NULL_TREE;
2964 for (bv = BINFO_VIRTUALS (binfo); bv; bv = TREE_CHAIN (bv))
2966 fn = BV_FN (bv);
2968 if (DECL_NAME (fn) == opname)
2970 if (CLASSTYPE_LAZY_COPY_ASSIGN (t))
2971 lazily_declare_fn (sfk_copy_assignment, t);
2972 if (CLASSTYPE_LAZY_MOVE_ASSIGN (t))
2973 lazily_declare_fn (sfk_move_assignment, t);
2975 else if (DECL_DESTRUCTOR_P (fn)
2976 && CLASSTYPE_LAZY_DESTRUCTOR (t))
2977 lazily_declare_fn (sfk_destructor, t);
2980 return NULL_TREE;
2983 /* If the class type T has a direct or indirect base that contains a
2984 virtual assignment operator or a virtual destructor, declare that
2985 function in T if it hasn't been already declared. */
2987 static void
2988 declare_virt_assop_and_dtor (tree t)
2990 if (!(TYPE_POLYMORPHIC_P (t)
2991 && (CLASSTYPE_LAZY_COPY_ASSIGN (t)
2992 || CLASSTYPE_LAZY_MOVE_ASSIGN (t)
2993 || CLASSTYPE_LAZY_DESTRUCTOR (t))))
2994 return;
2996 dfs_walk_all (TYPE_BINFO (t),
2997 dfs_declare_virt_assop_and_dtor,
2998 NULL, t);
3001 /* Declare the inheriting constructor for class T inherited from base
3002 constructor CTOR with the parameter array PARMS of size NPARMS. */
3004 static void
3005 one_inheriting_sig (tree t, tree ctor, tree *parms, int nparms)
3007 /* We don't declare an inheriting ctor that would be a default,
3008 copy or move ctor for derived or base. */
3009 if (nparms == 0)
3010 return;
3011 if (nparms == 1
3012 && TREE_CODE (parms[0]) == REFERENCE_TYPE)
3014 tree parm = TYPE_MAIN_VARIANT (TREE_TYPE (parms[0]));
3015 if (parm == t || parm == DECL_CONTEXT (ctor))
3016 return;
3019 tree parmlist = void_list_node;
3020 for (int i = nparms - 1; i >= 0; i--)
3021 parmlist = tree_cons (NULL_TREE, parms[i], parmlist);
3022 tree fn = implicitly_declare_fn (sfk_inheriting_constructor,
3023 t, false, ctor, parmlist);
3024 if (add_method (t, fn, NULL_TREE))
3026 DECL_CHAIN (fn) = TYPE_METHODS (t);
3027 TYPE_METHODS (t) = fn;
3031 /* Declare all the inheriting constructors for class T inherited from base
3032 constructor CTOR. */
3034 static void
3035 one_inherited_ctor (tree ctor, tree t)
3037 tree parms = FUNCTION_FIRST_USER_PARMTYPE (ctor);
3039 tree *new_parms = XALLOCAVEC (tree, list_length (parms));
3040 int i = 0;
3041 for (; parms && parms != void_list_node; parms = TREE_CHAIN (parms))
3043 if (TREE_PURPOSE (parms))
3044 one_inheriting_sig (t, ctor, new_parms, i);
3045 new_parms[i++] = TREE_VALUE (parms);
3047 one_inheriting_sig (t, ctor, new_parms, i);
3048 if (parms == NULL_TREE)
3050 warning (OPT_Winherited_variadic_ctor,
3051 "the ellipsis in %qD is not inherited", ctor);
3052 inform (DECL_SOURCE_LOCATION (ctor), "%qD declared here", ctor);
3056 /* Create default constructors, assignment operators, and so forth for
3057 the type indicated by T, if they are needed. CANT_HAVE_CONST_CTOR,
3058 and CANT_HAVE_CONST_ASSIGNMENT are nonzero if, for whatever reason,
3059 the class cannot have a default constructor, copy constructor
3060 taking a const reference argument, or an assignment operator taking
3061 a const reference, respectively. */
3063 static void
3064 add_implicitly_declared_members (tree t, tree* access_decls,
3065 int cant_have_const_cctor,
3066 int cant_have_const_assignment)
3068 bool move_ok = false;
3070 if (cxx_dialect >= cxx11 && !CLASSTYPE_DESTRUCTORS (t)
3071 && !TYPE_HAS_COPY_CTOR (t) && !TYPE_HAS_COPY_ASSIGN (t)
3072 && !type_has_move_constructor (t) && !type_has_move_assign (t))
3073 move_ok = true;
3075 /* Destructor. */
3076 if (!CLASSTYPE_DESTRUCTORS (t))
3078 /* In general, we create destructors lazily. */
3079 CLASSTYPE_LAZY_DESTRUCTOR (t) = 1;
3081 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t)
3082 && TYPE_FOR_JAVA (t))
3083 /* But if this is a Java class, any non-trivial destructor is
3084 invalid, even if compiler-generated. Therefore, if the
3085 destructor is non-trivial we create it now. */
3086 lazily_declare_fn (sfk_destructor, t);
3089 /* [class.ctor]
3091 If there is no user-declared constructor for a class, a default
3092 constructor is implicitly declared. */
3093 if (! TYPE_HAS_USER_CONSTRUCTOR (t))
3095 TYPE_HAS_DEFAULT_CONSTRUCTOR (t) = 1;
3096 CLASSTYPE_LAZY_DEFAULT_CTOR (t) = 1;
3097 if (cxx_dialect >= cxx11)
3098 TYPE_HAS_CONSTEXPR_CTOR (t)
3099 /* This might force the declaration. */
3100 = type_has_constexpr_default_constructor (t);
3103 /* [class.ctor]
3105 If a class definition does not explicitly declare a copy
3106 constructor, one is declared implicitly. */
3107 if (! TYPE_HAS_COPY_CTOR (t) && ! TYPE_FOR_JAVA (t))
3109 TYPE_HAS_COPY_CTOR (t) = 1;
3110 TYPE_HAS_CONST_COPY_CTOR (t) = !cant_have_const_cctor;
3111 CLASSTYPE_LAZY_COPY_CTOR (t) = 1;
3112 if (move_ok)
3113 CLASSTYPE_LAZY_MOVE_CTOR (t) = 1;
3116 /* If there is no assignment operator, one will be created if and
3117 when it is needed. For now, just record whether or not the type
3118 of the parameter to the assignment operator will be a const or
3119 non-const reference. */
3120 if (!TYPE_HAS_COPY_ASSIGN (t) && !TYPE_FOR_JAVA (t))
3122 TYPE_HAS_COPY_ASSIGN (t) = 1;
3123 TYPE_HAS_CONST_COPY_ASSIGN (t) = !cant_have_const_assignment;
3124 CLASSTYPE_LAZY_COPY_ASSIGN (t) = 1;
3125 if (move_ok)
3126 CLASSTYPE_LAZY_MOVE_ASSIGN (t) = 1;
3129 /* We can't be lazy about declaring functions that might override
3130 a virtual function from a base class. */
3131 declare_virt_assop_and_dtor (t);
3133 while (*access_decls)
3135 tree using_decl = TREE_VALUE (*access_decls);
3136 tree decl = USING_DECL_DECLS (using_decl);
3137 if (DECL_NAME (using_decl) == ctor_identifier)
3139 /* declare, then remove the decl */
3140 tree ctor_list = decl;
3141 location_t loc = input_location;
3142 input_location = DECL_SOURCE_LOCATION (using_decl);
3143 if (ctor_list)
3144 for (; ctor_list; ctor_list = OVL_NEXT (ctor_list))
3145 one_inherited_ctor (OVL_CURRENT (ctor_list), t);
3146 *access_decls = TREE_CHAIN (*access_decls);
3147 input_location = loc;
3149 else
3150 access_decls = &TREE_CHAIN (*access_decls);
3154 /* Subroutine of insert_into_classtype_sorted_fields. Recursively
3155 count the number of fields in TYPE, including anonymous union
3156 members. */
3158 static int
3159 count_fields (tree fields)
3161 tree x;
3162 int n_fields = 0;
3163 for (x = fields; x; x = DECL_CHAIN (x))
3165 if (TREE_CODE (x) == FIELD_DECL && ANON_AGGR_TYPE_P (TREE_TYPE (x)))
3166 n_fields += count_fields (TYPE_FIELDS (TREE_TYPE (x)));
3167 else
3168 n_fields += 1;
3170 return n_fields;
3173 /* Subroutine of insert_into_classtype_sorted_fields. Recursively add
3174 all the fields in the TREE_LIST FIELDS to the SORTED_FIELDS_TYPE
3175 elts, starting at offset IDX. */
3177 static int
3178 add_fields_to_record_type (tree fields, struct sorted_fields_type *field_vec, int idx)
3180 tree x;
3181 for (x = fields; x; x = DECL_CHAIN (x))
3183 if (TREE_CODE (x) == FIELD_DECL && ANON_AGGR_TYPE_P (TREE_TYPE (x)))
3184 idx = add_fields_to_record_type (TYPE_FIELDS (TREE_TYPE (x)), field_vec, idx);
3185 else
3186 field_vec->elts[idx++] = x;
3188 return idx;
3191 /* Add all of the enum values of ENUMTYPE, to the FIELD_VEC elts,
3192 starting at offset IDX. */
3194 static int
3195 add_enum_fields_to_record_type (tree enumtype,
3196 struct sorted_fields_type *field_vec,
3197 int idx)
3199 tree values;
3200 for (values = TYPE_VALUES (enumtype); values; values = TREE_CHAIN (values))
3201 field_vec->elts[idx++] = TREE_VALUE (values);
3202 return idx;
3205 /* FIELD is a bit-field. We are finishing the processing for its
3206 enclosing type. Issue any appropriate messages and set appropriate
3207 flags. Returns false if an error has been diagnosed. */
3209 static bool
3210 check_bitfield_decl (tree field)
3212 tree type = TREE_TYPE (field);
3213 tree w;
3215 /* Extract the declared width of the bitfield, which has been
3216 temporarily stashed in DECL_INITIAL. */
3217 w = DECL_INITIAL (field);
3218 gcc_assert (w != NULL_TREE);
3219 /* Remove the bit-field width indicator so that the rest of the
3220 compiler does not treat that value as an initializer. */
3221 DECL_INITIAL (field) = NULL_TREE;
3223 /* Detect invalid bit-field type. */
3224 if (!INTEGRAL_OR_ENUMERATION_TYPE_P (type))
3226 error ("bit-field %q+#D with non-integral type", field);
3227 w = error_mark_node;
3229 else
3231 location_t loc = input_location;
3232 /* Avoid the non_lvalue wrapper added by fold for PLUS_EXPRs. */
3233 STRIP_NOPS (w);
3235 /* detect invalid field size. */
3236 input_location = DECL_SOURCE_LOCATION (field);
3237 w = cxx_constant_value (w);
3238 input_location = loc;
3240 if (TREE_CODE (w) != INTEGER_CST)
3242 error ("bit-field %q+D width not an integer constant", field);
3243 w = error_mark_node;
3245 else if (tree_int_cst_sgn (w) < 0)
3247 error ("negative width in bit-field %q+D", field);
3248 w = error_mark_node;
3250 else if (integer_zerop (w) && DECL_NAME (field) != 0)
3252 error ("zero width for bit-field %q+D", field);
3253 w = error_mark_node;
3255 else if ((TREE_CODE (type) != ENUMERAL_TYPE
3256 && TREE_CODE (type) != BOOLEAN_TYPE
3257 && compare_tree_int (w, TYPE_PRECISION (type)) > 0)
3258 || ((TREE_CODE (type) == ENUMERAL_TYPE
3259 || TREE_CODE (type) == BOOLEAN_TYPE)
3260 && tree_int_cst_lt (TYPE_SIZE (type), w)))
3261 warning (0, "width of %q+D exceeds its type", field);
3262 else if (TREE_CODE (type) == ENUMERAL_TYPE
3263 && (0 > (compare_tree_int
3264 (w, TYPE_PRECISION (ENUM_UNDERLYING_TYPE (type))))))
3265 warning (0, "%q+D is too small to hold all values of %q#T", field, type);
3268 if (w != error_mark_node)
3270 DECL_SIZE (field) = convert (bitsizetype, w);
3271 DECL_BIT_FIELD (field) = 1;
3272 return true;
3274 else
3276 /* Non-bit-fields are aligned for their type. */
3277 DECL_BIT_FIELD (field) = 0;
3278 CLEAR_DECL_C_BIT_FIELD (field);
3279 return false;
3283 /* FIELD is a non bit-field. We are finishing the processing for its
3284 enclosing type T. Issue any appropriate messages and set appropriate
3285 flags. */
3287 static void
3288 check_field_decl (tree field,
3289 tree t,
3290 int* cant_have_const_ctor,
3291 int* no_const_asn_ref,
3292 int* any_default_members)
3294 tree type = strip_array_types (TREE_TYPE (field));
3296 /* In C++98 an anonymous union cannot contain any fields which would change
3297 the settings of CANT_HAVE_CONST_CTOR and friends. */
3298 if (ANON_UNION_TYPE_P (type) && cxx_dialect < cxx11)
3300 /* And, we don't set TYPE_HAS_CONST_COPY_CTOR, etc., for anonymous
3301 structs. So, we recurse through their fields here. */
3302 else if (ANON_AGGR_TYPE_P (type))
3304 tree fields;
3306 for (fields = TYPE_FIELDS (type); fields; fields = DECL_CHAIN (fields))
3307 if (TREE_CODE (fields) == FIELD_DECL && !DECL_C_BIT_FIELD (field))
3308 check_field_decl (fields, t, cant_have_const_ctor,
3309 no_const_asn_ref, any_default_members);
3311 /* Check members with class type for constructors, destructors,
3312 etc. */
3313 else if (CLASS_TYPE_P (type))
3315 /* Never let anything with uninheritable virtuals
3316 make it through without complaint. */
3317 abstract_virtuals_error (field, type);
3319 if (TREE_CODE (t) == UNION_TYPE && cxx_dialect < cxx11)
3321 static bool warned;
3322 int oldcount = errorcount;
3323 if (TYPE_NEEDS_CONSTRUCTING (type))
3324 error ("member %q+#D with constructor not allowed in union",
3325 field);
3326 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
3327 error ("member %q+#D with destructor not allowed in union", field);
3328 if (TYPE_HAS_COMPLEX_COPY_ASSIGN (type))
3329 error ("member %q+#D with copy assignment operator not allowed in union",
3330 field);
3331 if (!warned && errorcount > oldcount)
3333 inform (DECL_SOURCE_LOCATION (field), "unrestricted unions "
3334 "only available with -std=c++11 or -std=gnu++11");
3335 warned = true;
3338 else
3340 TYPE_NEEDS_CONSTRUCTING (t) |= TYPE_NEEDS_CONSTRUCTING (type);
3341 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t)
3342 |= TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type);
3343 TYPE_HAS_COMPLEX_COPY_ASSIGN (t)
3344 |= (TYPE_HAS_COMPLEX_COPY_ASSIGN (type)
3345 || !TYPE_HAS_COPY_ASSIGN (type));
3346 TYPE_HAS_COMPLEX_COPY_CTOR (t) |= (TYPE_HAS_COMPLEX_COPY_CTOR (type)
3347 || !TYPE_HAS_COPY_CTOR (type));
3348 TYPE_HAS_COMPLEX_MOVE_ASSIGN (t) |= TYPE_HAS_COMPLEX_MOVE_ASSIGN (type);
3349 TYPE_HAS_COMPLEX_MOVE_CTOR (t) |= TYPE_HAS_COMPLEX_MOVE_CTOR (type);
3350 TYPE_HAS_COMPLEX_DFLT (t) |= (!TYPE_HAS_DEFAULT_CONSTRUCTOR (type)
3351 || TYPE_HAS_COMPLEX_DFLT (type));
3354 if (TYPE_HAS_COPY_CTOR (type)
3355 && !TYPE_HAS_CONST_COPY_CTOR (type))
3356 *cant_have_const_ctor = 1;
3358 if (TYPE_HAS_COPY_ASSIGN (type)
3359 && !TYPE_HAS_CONST_COPY_ASSIGN (type))
3360 *no_const_asn_ref = 1;
3363 check_abi_tags (t, field);
3365 if (DECL_INITIAL (field) != NULL_TREE)
3367 /* `build_class_init_list' does not recognize
3368 non-FIELD_DECLs. */
3369 if (TREE_CODE (t) == UNION_TYPE && *any_default_members != 0)
3370 error ("multiple fields in union %qT initialized", t);
3371 *any_default_members = 1;
3375 /* Check the data members (both static and non-static), class-scoped
3376 typedefs, etc., appearing in the declaration of T. Issue
3377 appropriate diagnostics. Sets ACCESS_DECLS to a list (in
3378 declaration order) of access declarations; each TREE_VALUE in this
3379 list is a USING_DECL.
3381 In addition, set the following flags:
3383 EMPTY_P
3384 The class is empty, i.e., contains no non-static data members.
3386 CANT_HAVE_CONST_CTOR_P
3387 This class cannot have an implicitly generated copy constructor
3388 taking a const reference.
3390 CANT_HAVE_CONST_ASN_REF
3391 This class cannot have an implicitly generated assignment
3392 operator taking a const reference.
3394 All of these flags should be initialized before calling this
3395 function.
3397 Returns a pointer to the end of the TYPE_FIELDs chain; additional
3398 fields can be added by adding to this chain. */
3400 static void
3401 check_field_decls (tree t, tree *access_decls,
3402 int *cant_have_const_ctor_p,
3403 int *no_const_asn_ref_p)
3405 tree *field;
3406 tree *next;
3407 bool has_pointers;
3408 int any_default_members;
3409 int cant_pack = 0;
3410 int field_access = -1;
3412 /* Assume there are no access declarations. */
3413 *access_decls = NULL_TREE;
3414 /* Assume this class has no pointer members. */
3415 has_pointers = false;
3416 /* Assume none of the members of this class have default
3417 initializations. */
3418 any_default_members = 0;
3420 for (field = &TYPE_FIELDS (t); *field; field = next)
3422 tree x = *field;
3423 tree type = TREE_TYPE (x);
3424 int this_field_access;
3426 next = &DECL_CHAIN (x);
3428 if (TREE_CODE (x) == USING_DECL)
3430 /* Save the access declarations for our caller. */
3431 *access_decls = tree_cons (NULL_TREE, x, *access_decls);
3432 continue;
3435 if (TREE_CODE (x) == TYPE_DECL
3436 || TREE_CODE (x) == TEMPLATE_DECL)
3437 continue;
3439 /* If we've gotten this far, it's a data member, possibly static,
3440 or an enumerator. */
3441 if (TREE_CODE (x) != CONST_DECL)
3442 DECL_CONTEXT (x) = t;
3444 /* When this goes into scope, it will be a non-local reference. */
3445 DECL_NONLOCAL (x) = 1;
3447 if (TREE_CODE (t) == UNION_TYPE)
3449 /* [class.union]
3451 If a union contains a static data member, or a member of
3452 reference type, the program is ill-formed. */
3453 if (VAR_P (x))
3455 error ("%q+D may not be static because it is a member of a union", x);
3456 continue;
3458 if (TREE_CODE (type) == REFERENCE_TYPE)
3460 error ("%q+D may not have reference type %qT because"
3461 " it is a member of a union",
3462 x, type);
3463 continue;
3467 /* Perform error checking that did not get done in
3468 grokdeclarator. */
3469 if (TREE_CODE (type) == FUNCTION_TYPE)
3471 error ("field %q+D invalidly declared function type", x);
3472 type = build_pointer_type (type);
3473 TREE_TYPE (x) = type;
3475 else if (TREE_CODE (type) == METHOD_TYPE)
3477 error ("field %q+D invalidly declared method type", x);
3478 type = build_pointer_type (type);
3479 TREE_TYPE (x) = type;
3482 if (type == error_mark_node)
3483 continue;
3485 if (TREE_CODE (x) == CONST_DECL || VAR_P (x))
3486 continue;
3488 /* Now it can only be a FIELD_DECL. */
3490 if (TREE_PRIVATE (x) || TREE_PROTECTED (x))
3491 CLASSTYPE_NON_AGGREGATE (t) = 1;
3493 /* If at least one non-static data member is non-literal, the whole
3494 class becomes non-literal. Note: if the type is incomplete we
3495 will complain later on. */
3496 if (COMPLETE_TYPE_P (type) && !literal_type_p (type))
3497 CLASSTYPE_LITERAL_P (t) = false;
3499 /* A standard-layout class is a class that:
3501 has the same access control (Clause 11) for all non-static data members,
3502 ... */
3503 this_field_access = TREE_PROTECTED (x) ? 1 : TREE_PRIVATE (x) ? 2 : 0;
3504 if (field_access == -1)
3505 field_access = this_field_access;
3506 else if (this_field_access != field_access)
3507 CLASSTYPE_NON_STD_LAYOUT (t) = 1;
3509 /* If this is of reference type, check if it needs an init. */
3510 if (TREE_CODE (type) == REFERENCE_TYPE)
3512 CLASSTYPE_NON_LAYOUT_POD_P (t) = 1;
3513 CLASSTYPE_NON_STD_LAYOUT (t) = 1;
3514 if (DECL_INITIAL (x) == NULL_TREE)
3515 SET_CLASSTYPE_REF_FIELDS_NEED_INIT (t, 1);
3517 /* ARM $12.6.2: [A member initializer list] (or, for an
3518 aggregate, initialization by a brace-enclosed list) is the
3519 only way to initialize nonstatic const and reference
3520 members. */
3521 TYPE_HAS_COMPLEX_COPY_ASSIGN (t) = 1;
3522 TYPE_HAS_COMPLEX_MOVE_ASSIGN (t) = 1;
3525 type = strip_array_types (type);
3527 if (TYPE_PACKED (t))
3529 if (!layout_pod_type_p (type) && !TYPE_PACKED (type))
3531 warning
3533 "ignoring packed attribute because of unpacked non-POD field %q+#D",
3535 cant_pack = 1;
3537 else if (DECL_C_BIT_FIELD (x)
3538 || TYPE_ALIGN (TREE_TYPE (x)) > BITS_PER_UNIT)
3539 DECL_PACKED (x) = 1;
3542 if (DECL_C_BIT_FIELD (x) && integer_zerop (DECL_INITIAL (x)))
3543 /* We don't treat zero-width bitfields as making a class
3544 non-empty. */
3546 else
3548 /* The class is non-empty. */
3549 CLASSTYPE_EMPTY_P (t) = 0;
3550 /* The class is not even nearly empty. */
3551 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
3552 /* If one of the data members contains an empty class,
3553 so does T. */
3554 if (CLASS_TYPE_P (type)
3555 && CLASSTYPE_CONTAINS_EMPTY_CLASS_P (type))
3556 CLASSTYPE_CONTAINS_EMPTY_CLASS_P (t) = 1;
3559 /* This is used by -Weffc++ (see below). Warn only for pointers
3560 to members which might hold dynamic memory. So do not warn
3561 for pointers to functions or pointers to members. */
3562 if (TYPE_PTR_P (type)
3563 && !TYPE_PTRFN_P (type))
3564 has_pointers = true;
3566 if (CLASS_TYPE_P (type))
3568 if (CLASSTYPE_REF_FIELDS_NEED_INIT (type))
3569 SET_CLASSTYPE_REF_FIELDS_NEED_INIT (t, 1);
3570 if (CLASSTYPE_READONLY_FIELDS_NEED_INIT (type))
3571 SET_CLASSTYPE_READONLY_FIELDS_NEED_INIT (t, 1);
3574 if (DECL_MUTABLE_P (x) || TYPE_HAS_MUTABLE_P (type))
3575 CLASSTYPE_HAS_MUTABLE (t) = 1;
3577 if (DECL_MUTABLE_P (x))
3579 if (CP_TYPE_CONST_P (type))
3581 error ("member %q+D cannot be declared both %<const%> "
3582 "and %<mutable%>", x);
3583 continue;
3585 if (TREE_CODE (type) == REFERENCE_TYPE)
3587 error ("member %q+D cannot be declared as a %<mutable%> "
3588 "reference", x);
3589 continue;
3593 if (! layout_pod_type_p (type))
3594 /* DR 148 now allows pointers to members (which are POD themselves),
3595 to be allowed in POD structs. */
3596 CLASSTYPE_NON_LAYOUT_POD_P (t) = 1;
3598 if (!std_layout_type_p (type))
3599 CLASSTYPE_NON_STD_LAYOUT (t) = 1;
3601 if (! zero_init_p (type))
3602 CLASSTYPE_NON_ZERO_INIT_P (t) = 1;
3604 /* We set DECL_C_BIT_FIELD in grokbitfield.
3605 If the type and width are valid, we'll also set DECL_BIT_FIELD. */
3606 if (! DECL_C_BIT_FIELD (x) || ! check_bitfield_decl (x))
3607 check_field_decl (x, t,
3608 cant_have_const_ctor_p,
3609 no_const_asn_ref_p,
3610 &any_default_members);
3612 /* Now that we've removed bit-field widths from DECL_INITIAL,
3613 anything left in DECL_INITIAL is an NSDMI that makes the class
3614 non-aggregate. */
3615 if (DECL_INITIAL (x))
3616 CLASSTYPE_NON_AGGREGATE (t) = true;
3618 /* If any field is const, the structure type is pseudo-const. */
3619 if (CP_TYPE_CONST_P (type))
3621 C_TYPE_FIELDS_READONLY (t) = 1;
3622 if (DECL_INITIAL (x) == NULL_TREE)
3623 SET_CLASSTYPE_READONLY_FIELDS_NEED_INIT (t, 1);
3625 /* ARM $12.6.2: [A member initializer list] (or, for an
3626 aggregate, initialization by a brace-enclosed list) is the
3627 only way to initialize nonstatic const and reference
3628 members. */
3629 TYPE_HAS_COMPLEX_COPY_ASSIGN (t) = 1;
3630 TYPE_HAS_COMPLEX_MOVE_ASSIGN (t) = 1;
3632 /* A field that is pseudo-const makes the structure likewise. */
3633 else if (CLASS_TYPE_P (type))
3635 C_TYPE_FIELDS_READONLY (t) |= C_TYPE_FIELDS_READONLY (type);
3636 SET_CLASSTYPE_READONLY_FIELDS_NEED_INIT (t,
3637 CLASSTYPE_READONLY_FIELDS_NEED_INIT (t)
3638 | CLASSTYPE_READONLY_FIELDS_NEED_INIT (type));
3641 /* Core issue 80: A nonstatic data member is required to have a
3642 different name from the class iff the class has a
3643 user-declared constructor. */
3644 if (constructor_name_p (DECL_NAME (x), t)
3645 && TYPE_HAS_USER_CONSTRUCTOR (t))
3646 permerror (input_location, "field %q+#D with same name as class", x);
3649 /* Effective C++ rule 11: if a class has dynamic memory held by pointers,
3650 it should also define a copy constructor and an assignment operator to
3651 implement the correct copy semantic (deep vs shallow, etc.). As it is
3652 not feasible to check whether the constructors do allocate dynamic memory
3653 and store it within members, we approximate the warning like this:
3655 -- Warn only if there are members which are pointers
3656 -- Warn only if there is a non-trivial constructor (otherwise,
3657 there cannot be memory allocated).
3658 -- Warn only if there is a non-trivial destructor. We assume that the
3659 user at least implemented the cleanup correctly, and a destructor
3660 is needed to free dynamic memory.
3662 This seems enough for practical purposes. */
3663 if (warn_ecpp
3664 && has_pointers
3665 && TYPE_HAS_USER_CONSTRUCTOR (t)
3666 && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t)
3667 && !(TYPE_HAS_COPY_CTOR (t) && TYPE_HAS_COPY_ASSIGN (t)))
3669 warning (OPT_Weffc__, "%q#T has pointer data members", t);
3671 if (! TYPE_HAS_COPY_CTOR (t))
3673 warning (OPT_Weffc__,
3674 " but does not override %<%T(const %T&)%>", t, t);
3675 if (!TYPE_HAS_COPY_ASSIGN (t))
3676 warning (OPT_Weffc__, " or %<operator=(const %T&)%>", t);
3678 else if (! TYPE_HAS_COPY_ASSIGN (t))
3679 warning (OPT_Weffc__,
3680 " but does not override %<operator=(const %T&)%>", t);
3683 /* Non-static data member initializers make the default constructor
3684 non-trivial. */
3685 if (any_default_members)
3687 TYPE_NEEDS_CONSTRUCTING (t) = true;
3688 TYPE_HAS_COMPLEX_DFLT (t) = true;
3691 /* If any of the fields couldn't be packed, unset TYPE_PACKED. */
3692 if (cant_pack)
3693 TYPE_PACKED (t) = 0;
3695 /* Check anonymous struct/anonymous union fields. */
3696 finish_struct_anon (t);
3698 /* We've built up the list of access declarations in reverse order.
3699 Fix that now. */
3700 *access_decls = nreverse (*access_decls);
3703 /* If TYPE is an empty class type, records its OFFSET in the table of
3704 OFFSETS. */
3706 static int
3707 record_subobject_offset (tree type, tree offset, splay_tree offsets)
3709 splay_tree_node n;
3711 if (!is_empty_class (type))
3712 return 0;
3714 /* Record the location of this empty object in OFFSETS. */
3715 n = splay_tree_lookup (offsets, (splay_tree_key) offset);
3716 if (!n)
3717 n = splay_tree_insert (offsets,
3718 (splay_tree_key) offset,
3719 (splay_tree_value) NULL_TREE);
3720 n->value = ((splay_tree_value)
3721 tree_cons (NULL_TREE,
3722 type,
3723 (tree) n->value));
3725 return 0;
3728 /* Returns nonzero if TYPE is an empty class type and there is
3729 already an entry in OFFSETS for the same TYPE as the same OFFSET. */
3731 static int
3732 check_subobject_offset (tree type, tree offset, splay_tree offsets)
3734 splay_tree_node n;
3735 tree t;
3737 if (!is_empty_class (type))
3738 return 0;
3740 /* Record the location of this empty object in OFFSETS. */
3741 n = splay_tree_lookup (offsets, (splay_tree_key) offset);
3742 if (!n)
3743 return 0;
3745 for (t = (tree) n->value; t; t = TREE_CHAIN (t))
3746 if (same_type_p (TREE_VALUE (t), type))
3747 return 1;
3749 return 0;
3752 /* Walk through all the subobjects of TYPE (located at OFFSET). Call
3753 F for every subobject, passing it the type, offset, and table of
3754 OFFSETS. If VBASES_P is one, then virtual non-primary bases should
3755 be traversed.
3757 If MAX_OFFSET is non-NULL, then subobjects with an offset greater
3758 than MAX_OFFSET will not be walked.
3760 If F returns a nonzero value, the traversal ceases, and that value
3761 is returned. Otherwise, returns zero. */
3763 static int
3764 walk_subobject_offsets (tree type,
3765 subobject_offset_fn f,
3766 tree offset,
3767 splay_tree offsets,
3768 tree max_offset,
3769 int vbases_p)
3771 int r = 0;
3772 tree type_binfo = NULL_TREE;
3774 /* If this OFFSET is bigger than the MAX_OFFSET, then we should
3775 stop. */
3776 if (max_offset && INT_CST_LT (max_offset, offset))
3777 return 0;
3779 if (type == error_mark_node)
3780 return 0;
3782 if (!TYPE_P (type))
3784 if (abi_version_at_least (2))
3785 type_binfo = type;
3786 type = BINFO_TYPE (type);
3789 if (CLASS_TYPE_P (type))
3791 tree field;
3792 tree binfo;
3793 int i;
3795 /* Avoid recursing into objects that are not interesting. */
3796 if (!CLASSTYPE_CONTAINS_EMPTY_CLASS_P (type))
3797 return 0;
3799 /* Record the location of TYPE. */
3800 r = (*f) (type, offset, offsets);
3801 if (r)
3802 return r;
3804 /* Iterate through the direct base classes of TYPE. */
3805 if (!type_binfo)
3806 type_binfo = TYPE_BINFO (type);
3807 for (i = 0; BINFO_BASE_ITERATE (type_binfo, i, binfo); i++)
3809 tree binfo_offset;
3811 if (abi_version_at_least (2)
3812 && BINFO_VIRTUAL_P (binfo))
3813 continue;
3815 if (!vbases_p
3816 && BINFO_VIRTUAL_P (binfo)
3817 && !BINFO_PRIMARY_P (binfo))
3818 continue;
3820 if (!abi_version_at_least (2))
3821 binfo_offset = size_binop (PLUS_EXPR,
3822 offset,
3823 BINFO_OFFSET (binfo));
3824 else
3826 tree orig_binfo;
3827 /* We cannot rely on BINFO_OFFSET being set for the base
3828 class yet, but the offsets for direct non-virtual
3829 bases can be calculated by going back to the TYPE. */
3830 orig_binfo = BINFO_BASE_BINFO (TYPE_BINFO (type), i);
3831 binfo_offset = size_binop (PLUS_EXPR,
3832 offset,
3833 BINFO_OFFSET (orig_binfo));
3836 r = walk_subobject_offsets (binfo,
3838 binfo_offset,
3839 offsets,
3840 max_offset,
3841 (abi_version_at_least (2)
3842 ? /*vbases_p=*/0 : vbases_p));
3843 if (r)
3844 return r;
3847 if (abi_version_at_least (2) && CLASSTYPE_VBASECLASSES (type))
3849 unsigned ix;
3850 vec<tree, va_gc> *vbases;
3852 /* Iterate through the virtual base classes of TYPE. In G++
3853 3.2, we included virtual bases in the direct base class
3854 loop above, which results in incorrect results; the
3855 correct offsets for virtual bases are only known when
3856 working with the most derived type. */
3857 if (vbases_p)
3858 for (vbases = CLASSTYPE_VBASECLASSES (type), ix = 0;
3859 vec_safe_iterate (vbases, ix, &binfo); ix++)
3861 r = walk_subobject_offsets (binfo,
3863 size_binop (PLUS_EXPR,
3864 offset,
3865 BINFO_OFFSET (binfo)),
3866 offsets,
3867 max_offset,
3868 /*vbases_p=*/0);
3869 if (r)
3870 return r;
3872 else
3874 /* We still have to walk the primary base, if it is
3875 virtual. (If it is non-virtual, then it was walked
3876 above.) */
3877 tree vbase = get_primary_binfo (type_binfo);
3879 if (vbase && BINFO_VIRTUAL_P (vbase)
3880 && BINFO_PRIMARY_P (vbase)
3881 && BINFO_INHERITANCE_CHAIN (vbase) == type_binfo)
3883 r = (walk_subobject_offsets
3884 (vbase, f, offset,
3885 offsets, max_offset, /*vbases_p=*/0));
3886 if (r)
3887 return r;
3892 /* Iterate through the fields of TYPE. */
3893 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
3894 if (TREE_CODE (field) == FIELD_DECL
3895 && TREE_TYPE (field) != error_mark_node
3896 && !DECL_ARTIFICIAL (field))
3898 tree field_offset;
3900 if (abi_version_at_least (2))
3901 field_offset = byte_position (field);
3902 else
3903 /* In G++ 3.2, DECL_FIELD_OFFSET was used. */
3904 field_offset = DECL_FIELD_OFFSET (field);
3906 r = walk_subobject_offsets (TREE_TYPE (field),
3908 size_binop (PLUS_EXPR,
3909 offset,
3910 field_offset),
3911 offsets,
3912 max_offset,
3913 /*vbases_p=*/1);
3914 if (r)
3915 return r;
3918 else if (TREE_CODE (type) == ARRAY_TYPE)
3920 tree element_type = strip_array_types (type);
3921 tree domain = TYPE_DOMAIN (type);
3922 tree index;
3924 /* Avoid recursing into objects that are not interesting. */
3925 if (!CLASS_TYPE_P (element_type)
3926 || !CLASSTYPE_CONTAINS_EMPTY_CLASS_P (element_type))
3927 return 0;
3929 /* Step through each of the elements in the array. */
3930 for (index = size_zero_node;
3931 /* G++ 3.2 had an off-by-one error here. */
3932 (abi_version_at_least (2)
3933 ? !INT_CST_LT (TYPE_MAX_VALUE (domain), index)
3934 : INT_CST_LT (index, TYPE_MAX_VALUE (domain)));
3935 index = size_binop (PLUS_EXPR, index, size_one_node))
3937 r = walk_subobject_offsets (TREE_TYPE (type),
3939 offset,
3940 offsets,
3941 max_offset,
3942 /*vbases_p=*/1);
3943 if (r)
3944 return r;
3945 offset = size_binop (PLUS_EXPR, offset,
3946 TYPE_SIZE_UNIT (TREE_TYPE (type)));
3947 /* If this new OFFSET is bigger than the MAX_OFFSET, then
3948 there's no point in iterating through the remaining
3949 elements of the array. */
3950 if (max_offset && INT_CST_LT (max_offset, offset))
3951 break;
3955 return 0;
3958 /* Record all of the empty subobjects of TYPE (either a type or a
3959 binfo). If IS_DATA_MEMBER is true, then a non-static data member
3960 is being placed at OFFSET; otherwise, it is a base class that is
3961 being placed at OFFSET. */
3963 static void
3964 record_subobject_offsets (tree type,
3965 tree offset,
3966 splay_tree offsets,
3967 bool is_data_member)
3969 tree max_offset;
3970 /* If recording subobjects for a non-static data member or a
3971 non-empty base class , we do not need to record offsets beyond
3972 the size of the biggest empty class. Additional data members
3973 will go at the end of the class. Additional base classes will go
3974 either at offset zero (if empty, in which case they cannot
3975 overlap with offsets past the size of the biggest empty class) or
3976 at the end of the class.
3978 However, if we are placing an empty base class, then we must record
3979 all offsets, as either the empty class is at offset zero (where
3980 other empty classes might later be placed) or at the end of the
3981 class (where other objects might then be placed, so other empty
3982 subobjects might later overlap). */
3983 if (is_data_member
3984 || !is_empty_class (BINFO_TYPE (type)))
3985 max_offset = sizeof_biggest_empty_class;
3986 else
3987 max_offset = NULL_TREE;
3988 walk_subobject_offsets (type, record_subobject_offset, offset,
3989 offsets, max_offset, is_data_member);
3992 /* Returns nonzero if any of the empty subobjects of TYPE (located at
3993 OFFSET) conflict with entries in OFFSETS. If VBASES_P is nonzero,
3994 virtual bases of TYPE are examined. */
3996 static int
3997 layout_conflict_p (tree type,
3998 tree offset,
3999 splay_tree offsets,
4000 int vbases_p)
4002 splay_tree_node max_node;
4004 /* Get the node in OFFSETS that indicates the maximum offset where
4005 an empty subobject is located. */
4006 max_node = splay_tree_max (offsets);
4007 /* If there aren't any empty subobjects, then there's no point in
4008 performing this check. */
4009 if (!max_node)
4010 return 0;
4012 return walk_subobject_offsets (type, check_subobject_offset, offset,
4013 offsets, (tree) (max_node->key),
4014 vbases_p);
4017 /* DECL is a FIELD_DECL corresponding either to a base subobject of a
4018 non-static data member of the type indicated by RLI. BINFO is the
4019 binfo corresponding to the base subobject, OFFSETS maps offsets to
4020 types already located at those offsets. This function determines
4021 the position of the DECL. */
4023 static void
4024 layout_nonempty_base_or_field (record_layout_info rli,
4025 tree decl,
4026 tree binfo,
4027 splay_tree offsets)
4029 tree offset = NULL_TREE;
4030 bool field_p;
4031 tree type;
4033 if (binfo)
4035 /* For the purposes of determining layout conflicts, we want to
4036 use the class type of BINFO; TREE_TYPE (DECL) will be the
4037 CLASSTYPE_AS_BASE version, which does not contain entries for
4038 zero-sized bases. */
4039 type = TREE_TYPE (binfo);
4040 field_p = false;
4042 else
4044 type = TREE_TYPE (decl);
4045 field_p = true;
4048 /* Try to place the field. It may take more than one try if we have
4049 a hard time placing the field without putting two objects of the
4050 same type at the same address. */
4051 while (1)
4053 struct record_layout_info_s old_rli = *rli;
4055 /* Place this field. */
4056 place_field (rli, decl);
4057 offset = byte_position (decl);
4059 /* We have to check to see whether or not there is already
4060 something of the same type at the offset we're about to use.
4061 For example, consider:
4063 struct S {};
4064 struct T : public S { int i; };
4065 struct U : public S, public T {};
4067 Here, we put S at offset zero in U. Then, we can't put T at
4068 offset zero -- its S component would be at the same address
4069 as the S we already allocated. So, we have to skip ahead.
4070 Since all data members, including those whose type is an
4071 empty class, have nonzero size, any overlap can happen only
4072 with a direct or indirect base-class -- it can't happen with
4073 a data member. */
4074 /* In a union, overlap is permitted; all members are placed at
4075 offset zero. */
4076 if (TREE_CODE (rli->t) == UNION_TYPE)
4077 break;
4078 /* G++ 3.2 did not check for overlaps when placing a non-empty
4079 virtual base. */
4080 if (!abi_version_at_least (2) && binfo && BINFO_VIRTUAL_P (binfo))
4081 break;
4082 if (layout_conflict_p (field_p ? type : binfo, offset,
4083 offsets, field_p))
4085 /* Strip off the size allocated to this field. That puts us
4086 at the first place we could have put the field with
4087 proper alignment. */
4088 *rli = old_rli;
4090 /* Bump up by the alignment required for the type. */
4091 rli->bitpos
4092 = size_binop (PLUS_EXPR, rli->bitpos,
4093 bitsize_int (binfo
4094 ? CLASSTYPE_ALIGN (type)
4095 : TYPE_ALIGN (type)));
4096 normalize_rli (rli);
4098 else
4099 /* There was no conflict. We're done laying out this field. */
4100 break;
4103 /* Now that we know where it will be placed, update its
4104 BINFO_OFFSET. */
4105 if (binfo && CLASS_TYPE_P (BINFO_TYPE (binfo)))
4106 /* Indirect virtual bases may have a nonzero BINFO_OFFSET at
4107 this point because their BINFO_OFFSET is copied from another
4108 hierarchy. Therefore, we may not need to add the entire
4109 OFFSET. */
4110 propagate_binfo_offsets (binfo,
4111 size_diffop_loc (input_location,
4112 convert (ssizetype, offset),
4113 convert (ssizetype,
4114 BINFO_OFFSET (binfo))));
4117 /* Returns true if TYPE is empty and OFFSET is nonzero. */
4119 static int
4120 empty_base_at_nonzero_offset_p (tree type,
4121 tree offset,
4122 splay_tree /*offsets*/)
4124 return is_empty_class (type) && !integer_zerop (offset);
4127 /* Layout the empty base BINFO. EOC indicates the byte currently just
4128 past the end of the class, and should be correctly aligned for a
4129 class of the type indicated by BINFO; OFFSETS gives the offsets of
4130 the empty bases allocated so far. T is the most derived
4131 type. Return nonzero iff we added it at the end. */
4133 static bool
4134 layout_empty_base (record_layout_info rli, tree binfo,
4135 tree eoc, splay_tree offsets)
4137 tree alignment;
4138 tree basetype = BINFO_TYPE (binfo);
4139 bool atend = false;
4141 /* This routine should only be used for empty classes. */
4142 gcc_assert (is_empty_class (basetype));
4143 alignment = ssize_int (CLASSTYPE_ALIGN_UNIT (basetype));
4145 if (!integer_zerop (BINFO_OFFSET (binfo)))
4147 if (abi_version_at_least (2))
4148 propagate_binfo_offsets
4149 (binfo, size_diffop_loc (input_location,
4150 size_zero_node, BINFO_OFFSET (binfo)));
4151 else
4152 warning (OPT_Wabi,
4153 "offset of empty base %qT may not be ABI-compliant and may"
4154 "change in a future version of GCC",
4155 BINFO_TYPE (binfo));
4158 /* This is an empty base class. We first try to put it at offset
4159 zero. */
4160 if (layout_conflict_p (binfo,
4161 BINFO_OFFSET (binfo),
4162 offsets,
4163 /*vbases_p=*/0))
4165 /* That didn't work. Now, we move forward from the next
4166 available spot in the class. */
4167 atend = true;
4168 propagate_binfo_offsets (binfo, convert (ssizetype, eoc));
4169 while (1)
4171 if (!layout_conflict_p (binfo,
4172 BINFO_OFFSET (binfo),
4173 offsets,
4174 /*vbases_p=*/0))
4175 /* We finally found a spot where there's no overlap. */
4176 break;
4178 /* There's overlap here, too. Bump along to the next spot. */
4179 propagate_binfo_offsets (binfo, alignment);
4183 if (CLASSTYPE_USER_ALIGN (basetype))
4185 rli->record_align = MAX (rli->record_align, CLASSTYPE_ALIGN (basetype));
4186 if (warn_packed)
4187 rli->unpacked_align = MAX (rli->unpacked_align, CLASSTYPE_ALIGN (basetype));
4188 TYPE_USER_ALIGN (rli->t) = 1;
4191 return atend;
4194 /* Layout the base given by BINFO in the class indicated by RLI.
4195 *BASE_ALIGN is a running maximum of the alignments of
4196 any base class. OFFSETS gives the location of empty base
4197 subobjects. T is the most derived type. Return nonzero if the new
4198 object cannot be nearly-empty. A new FIELD_DECL is inserted at
4199 *NEXT_FIELD, unless BINFO is for an empty base class.
4201 Returns the location at which the next field should be inserted. */
4203 static tree *
4204 build_base_field (record_layout_info rli, tree binfo,
4205 splay_tree offsets, tree *next_field)
4207 tree t = rli->t;
4208 tree basetype = BINFO_TYPE (binfo);
4210 if (!COMPLETE_TYPE_P (basetype))
4211 /* This error is now reported in xref_tag, thus giving better
4212 location information. */
4213 return next_field;
4215 /* Place the base class. */
4216 if (!is_empty_class (basetype))
4218 tree decl;
4220 /* The containing class is non-empty because it has a non-empty
4221 base class. */
4222 CLASSTYPE_EMPTY_P (t) = 0;
4224 /* Create the FIELD_DECL. */
4225 decl = build_decl (input_location,
4226 FIELD_DECL, NULL_TREE, CLASSTYPE_AS_BASE (basetype));
4227 DECL_ARTIFICIAL (decl) = 1;
4228 DECL_IGNORED_P (decl) = 1;
4229 DECL_FIELD_CONTEXT (decl) = t;
4230 if (CLASSTYPE_AS_BASE (basetype))
4232 DECL_SIZE (decl) = CLASSTYPE_SIZE (basetype);
4233 DECL_SIZE_UNIT (decl) = CLASSTYPE_SIZE_UNIT (basetype);
4234 DECL_ALIGN (decl) = CLASSTYPE_ALIGN (basetype);
4235 DECL_USER_ALIGN (decl) = CLASSTYPE_USER_ALIGN (basetype);
4236 DECL_MODE (decl) = TYPE_MODE (basetype);
4237 DECL_FIELD_IS_BASE (decl) = 1;
4239 /* Try to place the field. It may take more than one try if we
4240 have a hard time placing the field without putting two
4241 objects of the same type at the same address. */
4242 layout_nonempty_base_or_field (rli, decl, binfo, offsets);
4243 /* Add the new FIELD_DECL to the list of fields for T. */
4244 DECL_CHAIN (decl) = *next_field;
4245 *next_field = decl;
4246 next_field = &DECL_CHAIN (decl);
4249 else
4251 tree eoc;
4252 bool atend;
4254 /* On some platforms (ARM), even empty classes will not be
4255 byte-aligned. */
4256 eoc = round_up_loc (input_location,
4257 rli_size_unit_so_far (rli),
4258 CLASSTYPE_ALIGN_UNIT (basetype));
4259 atend = layout_empty_base (rli, binfo, eoc, offsets);
4260 /* A nearly-empty class "has no proper base class that is empty,
4261 not morally virtual, and at an offset other than zero." */
4262 if (!BINFO_VIRTUAL_P (binfo) && CLASSTYPE_NEARLY_EMPTY_P (t))
4264 if (atend)
4265 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
4266 /* The check above (used in G++ 3.2) is insufficient because
4267 an empty class placed at offset zero might itself have an
4268 empty base at a nonzero offset. */
4269 else if (walk_subobject_offsets (basetype,
4270 empty_base_at_nonzero_offset_p,
4271 size_zero_node,
4272 /*offsets=*/NULL,
4273 /*max_offset=*/NULL_TREE,
4274 /*vbases_p=*/true))
4276 if (abi_version_at_least (2))
4277 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
4278 else
4279 warning (OPT_Wabi,
4280 "class %qT will be considered nearly empty in a "
4281 "future version of GCC", t);
4285 /* We do not create a FIELD_DECL for empty base classes because
4286 it might overlap some other field. We want to be able to
4287 create CONSTRUCTORs for the class by iterating over the
4288 FIELD_DECLs, and the back end does not handle overlapping
4289 FIELD_DECLs. */
4291 /* An empty virtual base causes a class to be non-empty
4292 -- but in that case we do not need to clear CLASSTYPE_EMPTY_P
4293 here because that was already done when the virtual table
4294 pointer was created. */
4297 /* Record the offsets of BINFO and its base subobjects. */
4298 record_subobject_offsets (binfo,
4299 BINFO_OFFSET (binfo),
4300 offsets,
4301 /*is_data_member=*/false);
4303 return next_field;
4306 /* Layout all of the non-virtual base classes. Record empty
4307 subobjects in OFFSETS. T is the most derived type. Return nonzero
4308 if the type cannot be nearly empty. The fields created
4309 corresponding to the base classes will be inserted at
4310 *NEXT_FIELD. */
4312 static void
4313 build_base_fields (record_layout_info rli,
4314 splay_tree offsets, tree *next_field)
4316 /* Chain to hold all the new FIELD_DECLs which stand in for base class
4317 subobjects. */
4318 tree t = rli->t;
4319 int n_baseclasses = BINFO_N_BASE_BINFOS (TYPE_BINFO (t));
4320 int i;
4322 /* The primary base class is always allocated first. */
4323 if (CLASSTYPE_HAS_PRIMARY_BASE_P (t))
4324 next_field = build_base_field (rli, CLASSTYPE_PRIMARY_BINFO (t),
4325 offsets, next_field);
4327 /* Now allocate the rest of the bases. */
4328 for (i = 0; i < n_baseclasses; ++i)
4330 tree base_binfo;
4332 base_binfo = BINFO_BASE_BINFO (TYPE_BINFO (t), i);
4334 /* The primary base was already allocated above, so we don't
4335 need to allocate it again here. */
4336 if (base_binfo == CLASSTYPE_PRIMARY_BINFO (t))
4337 continue;
4339 /* Virtual bases are added at the end (a primary virtual base
4340 will have already been added). */
4341 if (BINFO_VIRTUAL_P (base_binfo))
4342 continue;
4344 next_field = build_base_field (rli, base_binfo,
4345 offsets, next_field);
4349 /* Go through the TYPE_METHODS of T issuing any appropriate
4350 diagnostics, figuring out which methods override which other
4351 methods, and so forth. */
4353 static void
4354 check_methods (tree t)
4356 tree x;
4358 for (x = TYPE_METHODS (t); x; x = DECL_CHAIN (x))
4360 check_for_override (x, t);
4361 if (DECL_PURE_VIRTUAL_P (x) && ! DECL_VINDEX (x))
4362 error ("initializer specified for non-virtual method %q+D", x);
4363 /* The name of the field is the original field name
4364 Save this in auxiliary field for later overloading. */
4365 if (DECL_VINDEX (x))
4367 TYPE_POLYMORPHIC_P (t) = 1;
4368 if (DECL_PURE_VIRTUAL_P (x))
4369 vec_safe_push (CLASSTYPE_PURE_VIRTUALS (t), x);
4371 /* All user-provided destructors are non-trivial.
4372 Constructors and assignment ops are handled in
4373 grok_special_member_properties. */
4374 if (DECL_DESTRUCTOR_P (x) && user_provided_p (x))
4375 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t) = 1;
4379 /* FN is a constructor or destructor. Clone the declaration to create
4380 a specialized in-charge or not-in-charge version, as indicated by
4381 NAME. */
4383 static tree
4384 build_clone (tree fn, tree name)
4386 tree parms;
4387 tree clone;
4389 /* Copy the function. */
4390 clone = copy_decl (fn);
4391 /* Reset the function name. */
4392 DECL_NAME (clone) = name;
4393 SET_DECL_ASSEMBLER_NAME (clone, NULL_TREE);
4394 /* Remember where this function came from. */
4395 DECL_ABSTRACT_ORIGIN (clone) = fn;
4396 /* Make it easy to find the CLONE given the FN. */
4397 DECL_CHAIN (clone) = DECL_CHAIN (fn);
4398 DECL_CHAIN (fn) = clone;
4400 /* If this is a template, do the rest on the DECL_TEMPLATE_RESULT. */
4401 if (TREE_CODE (clone) == TEMPLATE_DECL)
4403 tree result = build_clone (DECL_TEMPLATE_RESULT (clone), name);
4404 DECL_TEMPLATE_RESULT (clone) = result;
4405 DECL_TEMPLATE_INFO (result) = copy_node (DECL_TEMPLATE_INFO (result));
4406 DECL_TI_TEMPLATE (result) = clone;
4407 TREE_TYPE (clone) = TREE_TYPE (result);
4408 return clone;
4411 DECL_CLONED_FUNCTION (clone) = fn;
4412 /* There's no pending inline data for this function. */
4413 DECL_PENDING_INLINE_INFO (clone) = NULL;
4414 DECL_PENDING_INLINE_P (clone) = 0;
4416 /* The base-class destructor is not virtual. */
4417 if (name == base_dtor_identifier)
4419 DECL_VIRTUAL_P (clone) = 0;
4420 if (TREE_CODE (clone) != TEMPLATE_DECL)
4421 DECL_VINDEX (clone) = NULL_TREE;
4424 /* If there was an in-charge parameter, drop it from the function
4425 type. */
4426 if (DECL_HAS_IN_CHARGE_PARM_P (clone))
4428 tree basetype;
4429 tree parmtypes;
4430 tree exceptions;
4432 exceptions = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (clone));
4433 basetype = TYPE_METHOD_BASETYPE (TREE_TYPE (clone));
4434 parmtypes = TYPE_ARG_TYPES (TREE_TYPE (clone));
4435 /* Skip the `this' parameter. */
4436 parmtypes = TREE_CHAIN (parmtypes);
4437 /* Skip the in-charge parameter. */
4438 parmtypes = TREE_CHAIN (parmtypes);
4439 /* And the VTT parm, in a complete [cd]tor. */
4440 if (DECL_HAS_VTT_PARM_P (fn)
4441 && ! DECL_NEEDS_VTT_PARM_P (clone))
4442 parmtypes = TREE_CHAIN (parmtypes);
4443 /* If this is subobject constructor or destructor, add the vtt
4444 parameter. */
4445 TREE_TYPE (clone)
4446 = build_method_type_directly (basetype,
4447 TREE_TYPE (TREE_TYPE (clone)),
4448 parmtypes);
4449 if (exceptions)
4450 TREE_TYPE (clone) = build_exception_variant (TREE_TYPE (clone),
4451 exceptions);
4452 TREE_TYPE (clone)
4453 = cp_build_type_attribute_variant (TREE_TYPE (clone),
4454 TYPE_ATTRIBUTES (TREE_TYPE (fn)));
4457 /* Copy the function parameters. */
4458 DECL_ARGUMENTS (clone) = copy_list (DECL_ARGUMENTS (clone));
4459 /* Remove the in-charge parameter. */
4460 if (DECL_HAS_IN_CHARGE_PARM_P (clone))
4462 DECL_CHAIN (DECL_ARGUMENTS (clone))
4463 = DECL_CHAIN (DECL_CHAIN (DECL_ARGUMENTS (clone)));
4464 DECL_HAS_IN_CHARGE_PARM_P (clone) = 0;
4466 /* And the VTT parm, in a complete [cd]tor. */
4467 if (DECL_HAS_VTT_PARM_P (fn))
4469 if (DECL_NEEDS_VTT_PARM_P (clone))
4470 DECL_HAS_VTT_PARM_P (clone) = 1;
4471 else
4473 DECL_CHAIN (DECL_ARGUMENTS (clone))
4474 = DECL_CHAIN (DECL_CHAIN (DECL_ARGUMENTS (clone)));
4475 DECL_HAS_VTT_PARM_P (clone) = 0;
4479 for (parms = DECL_ARGUMENTS (clone); parms; parms = DECL_CHAIN (parms))
4481 DECL_CONTEXT (parms) = clone;
4482 cxx_dup_lang_specific_decl (parms);
4485 /* Create the RTL for this function. */
4486 SET_DECL_RTL (clone, NULL);
4487 rest_of_decl_compilation (clone, /*top_level=*/1, at_eof);
4489 if (pch_file)
4490 note_decl_for_pch (clone);
4492 return clone;
4495 /* Implementation of DECL_CLONED_FUNCTION and DECL_CLONED_FUNCTION_P, do
4496 not invoke this function directly.
4498 For a non-thunk function, returns the address of the slot for storing
4499 the function it is a clone of. Otherwise returns NULL_TREE.
4501 If JUST_TESTING, looks through TEMPLATE_DECL and returns NULL if
4502 cloned_function is unset. This is to support the separate
4503 DECL_CLONED_FUNCTION and DECL_CLONED_FUNCTION_P modes; using the latter
4504 on a template makes sense, but not the former. */
4506 tree *
4507 decl_cloned_function_p (const_tree decl, bool just_testing)
4509 tree *ptr;
4510 if (just_testing)
4511 decl = STRIP_TEMPLATE (decl);
4513 if (TREE_CODE (decl) != FUNCTION_DECL
4514 || !DECL_LANG_SPECIFIC (decl)
4515 || DECL_LANG_SPECIFIC (decl)->u.fn.thunk_p)
4517 #if defined ENABLE_TREE_CHECKING && (GCC_VERSION >= 2007)
4518 if (!just_testing)
4519 lang_check_failed (__FILE__, __LINE__, __FUNCTION__);
4520 else
4521 #endif
4522 return NULL;
4525 ptr = &DECL_LANG_SPECIFIC (decl)->u.fn.u5.cloned_function;
4526 if (just_testing && *ptr == NULL_TREE)
4527 return NULL;
4528 else
4529 return ptr;
4532 /* Produce declarations for all appropriate clones of FN. If
4533 UPDATE_METHOD_VEC_P is nonzero, the clones are added to the
4534 CLASTYPE_METHOD_VEC as well. */
4536 void
4537 clone_function_decl (tree fn, int update_method_vec_p)
4539 tree clone;
4541 /* Avoid inappropriate cloning. */
4542 if (DECL_CHAIN (fn)
4543 && DECL_CLONED_FUNCTION_P (DECL_CHAIN (fn)))
4544 return;
4546 if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (fn))
4548 /* For each constructor, we need two variants: an in-charge version
4549 and a not-in-charge version. */
4550 clone = build_clone (fn, complete_ctor_identifier);
4551 if (update_method_vec_p)
4552 add_method (DECL_CONTEXT (clone), clone, NULL_TREE);
4553 clone = build_clone (fn, base_ctor_identifier);
4554 if (update_method_vec_p)
4555 add_method (DECL_CONTEXT (clone), clone, NULL_TREE);
4557 else
4559 gcc_assert (DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (fn));
4561 /* For each destructor, we need three variants: an in-charge
4562 version, a not-in-charge version, and an in-charge deleting
4563 version. We clone the deleting version first because that
4564 means it will go second on the TYPE_METHODS list -- and that
4565 corresponds to the correct layout order in the virtual
4566 function table.
4568 For a non-virtual destructor, we do not build a deleting
4569 destructor. */
4570 if (DECL_VIRTUAL_P (fn))
4572 clone = build_clone (fn, deleting_dtor_identifier);
4573 if (update_method_vec_p)
4574 add_method (DECL_CONTEXT (clone), clone, NULL_TREE);
4576 clone = build_clone (fn, complete_dtor_identifier);
4577 if (update_method_vec_p)
4578 add_method (DECL_CONTEXT (clone), clone, NULL_TREE);
4579 clone = build_clone (fn, base_dtor_identifier);
4580 if (update_method_vec_p)
4581 add_method (DECL_CONTEXT (clone), clone, NULL_TREE);
4584 /* Note that this is an abstract function that is never emitted. */
4585 DECL_ABSTRACT (fn) = 1;
4588 /* DECL is an in charge constructor, which is being defined. This will
4589 have had an in class declaration, from whence clones were
4590 declared. An out-of-class definition can specify additional default
4591 arguments. As it is the clones that are involved in overload
4592 resolution, we must propagate the information from the DECL to its
4593 clones. */
4595 void
4596 adjust_clone_args (tree decl)
4598 tree clone;
4600 for (clone = DECL_CHAIN (decl); clone && DECL_CLONED_FUNCTION_P (clone);
4601 clone = DECL_CHAIN (clone))
4603 tree orig_clone_parms = TYPE_ARG_TYPES (TREE_TYPE (clone));
4604 tree orig_decl_parms = TYPE_ARG_TYPES (TREE_TYPE (decl));
4605 tree decl_parms, clone_parms;
4607 clone_parms = orig_clone_parms;
4609 /* Skip the 'this' parameter. */
4610 orig_clone_parms = TREE_CHAIN (orig_clone_parms);
4611 orig_decl_parms = TREE_CHAIN (orig_decl_parms);
4613 if (DECL_HAS_IN_CHARGE_PARM_P (decl))
4614 orig_decl_parms = TREE_CHAIN (orig_decl_parms);
4615 if (DECL_HAS_VTT_PARM_P (decl))
4616 orig_decl_parms = TREE_CHAIN (orig_decl_parms);
4618 clone_parms = orig_clone_parms;
4619 if (DECL_HAS_VTT_PARM_P (clone))
4620 clone_parms = TREE_CHAIN (clone_parms);
4622 for (decl_parms = orig_decl_parms; decl_parms;
4623 decl_parms = TREE_CHAIN (decl_parms),
4624 clone_parms = TREE_CHAIN (clone_parms))
4626 gcc_assert (same_type_p (TREE_TYPE (decl_parms),
4627 TREE_TYPE (clone_parms)));
4629 if (TREE_PURPOSE (decl_parms) && !TREE_PURPOSE (clone_parms))
4631 /* A default parameter has been added. Adjust the
4632 clone's parameters. */
4633 tree exceptions = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (clone));
4634 tree attrs = TYPE_ATTRIBUTES (TREE_TYPE (clone));
4635 tree basetype = TYPE_METHOD_BASETYPE (TREE_TYPE (clone));
4636 tree type;
4638 clone_parms = orig_decl_parms;
4640 if (DECL_HAS_VTT_PARM_P (clone))
4642 clone_parms = tree_cons (TREE_PURPOSE (orig_clone_parms),
4643 TREE_VALUE (orig_clone_parms),
4644 clone_parms);
4645 TREE_TYPE (clone_parms) = TREE_TYPE (orig_clone_parms);
4647 type = build_method_type_directly (basetype,
4648 TREE_TYPE (TREE_TYPE (clone)),
4649 clone_parms);
4650 if (exceptions)
4651 type = build_exception_variant (type, exceptions);
4652 if (attrs)
4653 type = cp_build_type_attribute_variant (type, attrs);
4654 TREE_TYPE (clone) = type;
4656 clone_parms = NULL_TREE;
4657 break;
4660 gcc_assert (!clone_parms);
4664 /* For each of the constructors and destructors in T, create an
4665 in-charge and not-in-charge variant. */
4667 static void
4668 clone_constructors_and_destructors (tree t)
4670 tree fns;
4672 /* If for some reason we don't have a CLASSTYPE_METHOD_VEC, we bail
4673 out now. */
4674 if (!CLASSTYPE_METHOD_VEC (t))
4675 return;
4677 for (fns = CLASSTYPE_CONSTRUCTORS (t); fns; fns = OVL_NEXT (fns))
4678 clone_function_decl (OVL_CURRENT (fns), /*update_method_vec_p=*/1);
4679 for (fns = CLASSTYPE_DESTRUCTORS (t); fns; fns = OVL_NEXT (fns))
4680 clone_function_decl (OVL_CURRENT (fns), /*update_method_vec_p=*/1);
4683 /* Deduce noexcept for a destructor DTOR. */
4685 void
4686 deduce_noexcept_on_destructor (tree dtor)
4688 if (!TYPE_RAISES_EXCEPTIONS (TREE_TYPE (dtor)))
4690 tree ctx = DECL_CONTEXT (dtor);
4691 tree implicit_fn = implicitly_declare_fn (sfk_destructor, ctx,
4692 /*const_p=*/false,
4693 NULL, NULL);
4694 tree eh_spec = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (implicit_fn));
4695 TREE_TYPE (dtor) = build_exception_variant (TREE_TYPE (dtor), eh_spec);
4699 /* For each destructor in T, deduce noexcept:
4701 12.4/3: A declaration of a destructor that does not have an
4702 exception-specification is implicitly considered to have the
4703 same exception-specification as an implicit declaration (15.4). */
4705 static void
4706 deduce_noexcept_on_destructors (tree t)
4708 /* If for some reason we don't have a CLASSTYPE_METHOD_VEC, we bail
4709 out now. */
4710 if (!CLASSTYPE_METHOD_VEC (t))
4711 return;
4713 for (tree fns = CLASSTYPE_DESTRUCTORS (t); fns; fns = OVL_NEXT (fns))
4714 deduce_noexcept_on_destructor (OVL_CURRENT (fns));
4717 /* Subroutine of set_one_vmethod_tm_attributes. Search base classes
4718 of TYPE for virtual functions which FNDECL overrides. Return a
4719 mask of the tm attributes found therein. */
4721 static int
4722 look_for_tm_attr_overrides (tree type, tree fndecl)
4724 tree binfo = TYPE_BINFO (type);
4725 tree base_binfo;
4726 int ix, found = 0;
4728 for (ix = 0; BINFO_BASE_ITERATE (binfo, ix, base_binfo); ++ix)
4730 tree o, basetype = BINFO_TYPE (base_binfo);
4732 if (!TYPE_POLYMORPHIC_P (basetype))
4733 continue;
4735 o = look_for_overrides_here (basetype, fndecl);
4736 if (o)
4737 found |= tm_attr_to_mask (find_tm_attribute
4738 (TYPE_ATTRIBUTES (TREE_TYPE (o))));
4739 else
4740 found |= look_for_tm_attr_overrides (basetype, fndecl);
4743 return found;
4746 /* Subroutine of set_method_tm_attributes. Handle the checks and
4747 inheritance for one virtual method FNDECL. */
4749 static void
4750 set_one_vmethod_tm_attributes (tree type, tree fndecl)
4752 tree tm_attr;
4753 int found, have;
4755 found = look_for_tm_attr_overrides (type, fndecl);
4757 /* If FNDECL doesn't actually override anything (i.e. T is the
4758 class that first declares FNDECL virtual), then we're done. */
4759 if (found == 0)
4760 return;
4762 tm_attr = find_tm_attribute (TYPE_ATTRIBUTES (TREE_TYPE (fndecl)));
4763 have = tm_attr_to_mask (tm_attr);
4765 /* Intel STM Language Extension 3.0, Section 4.2 table 4:
4766 tm_pure must match exactly, otherwise no weakening of
4767 tm_safe > tm_callable > nothing. */
4768 /* ??? The tm_pure attribute didn't make the transition to the
4769 multivendor language spec. */
4770 if (have == TM_ATTR_PURE)
4772 if (found != TM_ATTR_PURE)
4774 found &= -found;
4775 goto err_override;
4778 /* If the overridden function is tm_pure, then FNDECL must be. */
4779 else if (found == TM_ATTR_PURE && tm_attr)
4780 goto err_override;
4781 /* Look for base class combinations that cannot be satisfied. */
4782 else if (found != TM_ATTR_PURE && (found & TM_ATTR_PURE))
4784 found &= ~TM_ATTR_PURE;
4785 found &= -found;
4786 error_at (DECL_SOURCE_LOCATION (fndecl),
4787 "method overrides both %<transaction_pure%> and %qE methods",
4788 tm_mask_to_attr (found));
4790 /* If FNDECL did not declare an attribute, then inherit the most
4791 restrictive one. */
4792 else if (tm_attr == NULL)
4794 apply_tm_attr (fndecl, tm_mask_to_attr (found & -found));
4796 /* Otherwise validate that we're not weaker than a function
4797 that is being overridden. */
4798 else
4800 found &= -found;
4801 if (found <= TM_ATTR_CALLABLE && have > found)
4802 goto err_override;
4804 return;
4806 err_override:
4807 error_at (DECL_SOURCE_LOCATION (fndecl),
4808 "method declared %qE overriding %qE method",
4809 tm_attr, tm_mask_to_attr (found));
4812 /* For each of the methods in T, propagate a class-level tm attribute. */
4814 static void
4815 set_method_tm_attributes (tree t)
4817 tree class_tm_attr, fndecl;
4819 /* Don't bother collecting tm attributes if transactional memory
4820 support is not enabled. */
4821 if (!flag_tm)
4822 return;
4824 /* Process virtual methods first, as they inherit directly from the
4825 base virtual function and also require validation of new attributes. */
4826 if (TYPE_CONTAINS_VPTR_P (t))
4828 tree vchain;
4829 for (vchain = BINFO_VIRTUALS (TYPE_BINFO (t)); vchain;
4830 vchain = TREE_CHAIN (vchain))
4832 fndecl = BV_FN (vchain);
4833 if (DECL_THUNK_P (fndecl))
4834 fndecl = THUNK_TARGET (fndecl);
4835 set_one_vmethod_tm_attributes (t, fndecl);
4839 /* If the class doesn't have an attribute, nothing more to do. */
4840 class_tm_attr = find_tm_attribute (TYPE_ATTRIBUTES (t));
4841 if (class_tm_attr == NULL)
4842 return;
4844 /* Any method that does not yet have a tm attribute inherits
4845 the one from the class. */
4846 for (fndecl = TYPE_METHODS (t); fndecl; fndecl = TREE_CHAIN (fndecl))
4848 if (!find_tm_attribute (TYPE_ATTRIBUTES (TREE_TYPE (fndecl))))
4849 apply_tm_attr (fndecl, class_tm_attr);
4853 /* Returns true iff class T has a user-defined constructor other than
4854 the default constructor. */
4856 bool
4857 type_has_user_nondefault_constructor (tree t)
4859 tree fns;
4861 if (!TYPE_HAS_USER_CONSTRUCTOR (t))
4862 return false;
4864 for (fns = CLASSTYPE_CONSTRUCTORS (t); fns; fns = OVL_NEXT (fns))
4866 tree fn = OVL_CURRENT (fns);
4867 if (!DECL_ARTIFICIAL (fn)
4868 && (TREE_CODE (fn) == TEMPLATE_DECL
4869 || (skip_artificial_parms_for (fn, DECL_ARGUMENTS (fn))
4870 != NULL_TREE)))
4871 return true;
4874 return false;
4877 /* Returns the defaulted constructor if T has one. Otherwise, returns
4878 NULL_TREE. */
4880 tree
4881 in_class_defaulted_default_constructor (tree t)
4883 tree fns, args;
4885 if (!TYPE_HAS_USER_CONSTRUCTOR (t))
4886 return NULL_TREE;
4888 for (fns = CLASSTYPE_CONSTRUCTORS (t); fns; fns = OVL_NEXT (fns))
4890 tree fn = OVL_CURRENT (fns);
4892 if (DECL_DEFAULTED_IN_CLASS_P (fn))
4894 args = FUNCTION_FIRST_USER_PARMTYPE (fn);
4895 while (args && TREE_PURPOSE (args))
4896 args = TREE_CHAIN (args);
4897 if (!args || args == void_list_node)
4898 return fn;
4902 return NULL_TREE;
4905 /* Returns true iff FN is a user-provided function, i.e. user-declared
4906 and not defaulted at its first declaration; or explicit, private,
4907 protected, or non-const. */
4909 bool
4910 user_provided_p (tree fn)
4912 if (TREE_CODE (fn) == TEMPLATE_DECL)
4913 return true;
4914 else
4915 return (!DECL_ARTIFICIAL (fn)
4916 && !(DECL_INITIALIZED_IN_CLASS_P (fn)
4917 && (DECL_DEFAULTED_FN (fn) || DECL_DELETED_FN (fn))));
4920 /* Returns true iff class T has a user-provided constructor. */
4922 bool
4923 type_has_user_provided_constructor (tree t)
4925 tree fns;
4927 if (!CLASS_TYPE_P (t))
4928 return false;
4930 if (!TYPE_HAS_USER_CONSTRUCTOR (t))
4931 return false;
4933 /* This can happen in error cases; avoid crashing. */
4934 if (!CLASSTYPE_METHOD_VEC (t))
4935 return false;
4937 for (fns = CLASSTYPE_CONSTRUCTORS (t); fns; fns = OVL_NEXT (fns))
4938 if (user_provided_p (OVL_CURRENT (fns)))
4939 return true;
4941 return false;
4944 /* Returns true iff class T has a user-provided default constructor. */
4946 bool
4947 type_has_user_provided_default_constructor (tree t)
4949 tree fns;
4951 if (!TYPE_HAS_USER_CONSTRUCTOR (t))
4952 return false;
4954 for (fns = CLASSTYPE_CONSTRUCTORS (t); fns; fns = OVL_NEXT (fns))
4956 tree fn = OVL_CURRENT (fns);
4957 if (TREE_CODE (fn) == FUNCTION_DECL
4958 && user_provided_p (fn)
4959 && sufficient_parms_p (FUNCTION_FIRST_USER_PARMTYPE (fn)))
4960 return true;
4963 return false;
4966 /* TYPE is being used as a virtual base, and has a non-trivial move
4967 assignment. Return true if this is due to there being a user-provided
4968 move assignment in TYPE or one of its subobjects; if there isn't, then
4969 multiple move assignment can't cause any harm. */
4971 bool
4972 vbase_has_user_provided_move_assign (tree type)
4974 /* Does the type itself have a user-provided move assignment operator? */
4975 for (tree fns
4976 = lookup_fnfields_slot_nolazy (type, ansi_assopname (NOP_EXPR));
4977 fns; fns = OVL_NEXT (fns))
4979 tree fn = OVL_CURRENT (fns);
4980 if (move_fn_p (fn) && user_provided_p (fn))
4981 return true;
4984 /* Do any of its bases? */
4985 tree binfo = TYPE_BINFO (type);
4986 tree base_binfo;
4987 for (int i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
4988 if (vbase_has_user_provided_move_assign (BINFO_TYPE (base_binfo)))
4989 return true;
4991 /* Or non-static data members? */
4992 for (tree field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
4994 if (TREE_CODE (field) == FIELD_DECL
4995 && CLASS_TYPE_P (TREE_TYPE (field))
4996 && vbase_has_user_provided_move_assign (TREE_TYPE (field)))
4997 return true;
5000 /* Seems not. */
5001 return false;
5004 /* If default-initialization leaves part of TYPE uninitialized, returns
5005 a DECL for the field or TYPE itself (DR 253). */
5007 tree
5008 default_init_uninitialized_part (tree type)
5010 tree t, r, binfo;
5011 int i;
5013 type = strip_array_types (type);
5014 if (!CLASS_TYPE_P (type))
5015 return type;
5016 if (type_has_user_provided_default_constructor (type))
5017 return NULL_TREE;
5018 for (binfo = TYPE_BINFO (type), i = 0;
5019 BINFO_BASE_ITERATE (binfo, i, t); ++i)
5021 r = default_init_uninitialized_part (BINFO_TYPE (t));
5022 if (r)
5023 return r;
5025 for (t = TYPE_FIELDS (type); t; t = DECL_CHAIN (t))
5026 if (TREE_CODE (t) == FIELD_DECL
5027 && !DECL_ARTIFICIAL (t)
5028 && !DECL_INITIAL (t))
5030 r = default_init_uninitialized_part (TREE_TYPE (t));
5031 if (r)
5032 return DECL_P (r) ? r : t;
5035 return NULL_TREE;
5038 /* Returns true iff for class T, a trivial synthesized default constructor
5039 would be constexpr. */
5041 bool
5042 trivial_default_constructor_is_constexpr (tree t)
5044 /* A defaulted trivial default constructor is constexpr
5045 if there is nothing to initialize. */
5046 gcc_assert (!TYPE_HAS_COMPLEX_DFLT (t));
5047 return is_really_empty_class (t);
5050 /* Returns true iff class T has a constexpr default constructor. */
5052 bool
5053 type_has_constexpr_default_constructor (tree t)
5055 tree fns;
5057 if (!CLASS_TYPE_P (t))
5059 /* The caller should have stripped an enclosing array. */
5060 gcc_assert (TREE_CODE (t) != ARRAY_TYPE);
5061 return false;
5063 if (CLASSTYPE_LAZY_DEFAULT_CTOR (t))
5065 if (!TYPE_HAS_COMPLEX_DFLT (t))
5066 return trivial_default_constructor_is_constexpr (t);
5067 /* Non-trivial, we need to check subobject constructors. */
5068 lazily_declare_fn (sfk_constructor, t);
5070 fns = locate_ctor (t);
5071 return (fns && DECL_DECLARED_CONSTEXPR_P (fns));
5074 /* Returns true iff class TYPE has a virtual destructor. */
5076 bool
5077 type_has_virtual_destructor (tree type)
5079 tree dtor;
5081 if (!CLASS_TYPE_P (type))
5082 return false;
5084 gcc_assert (COMPLETE_TYPE_P (type));
5085 dtor = CLASSTYPE_DESTRUCTORS (type);
5086 return (dtor && DECL_VIRTUAL_P (dtor));
5089 /* Returns true iff class T has a move constructor. */
5091 bool
5092 type_has_move_constructor (tree t)
5094 tree fns;
5096 if (CLASSTYPE_LAZY_MOVE_CTOR (t))
5098 gcc_assert (COMPLETE_TYPE_P (t));
5099 lazily_declare_fn (sfk_move_constructor, t);
5102 if (!CLASSTYPE_METHOD_VEC (t))
5103 return false;
5105 for (fns = CLASSTYPE_CONSTRUCTORS (t); fns; fns = OVL_NEXT (fns))
5106 if (move_fn_p (OVL_CURRENT (fns)))
5107 return true;
5109 return false;
5112 /* Returns true iff class T has a move assignment operator. */
5114 bool
5115 type_has_move_assign (tree t)
5117 tree fns;
5119 if (CLASSTYPE_LAZY_MOVE_ASSIGN (t))
5121 gcc_assert (COMPLETE_TYPE_P (t));
5122 lazily_declare_fn (sfk_move_assignment, t);
5125 for (fns = lookup_fnfields_slot_nolazy (t, ansi_assopname (NOP_EXPR));
5126 fns; fns = OVL_NEXT (fns))
5127 if (move_fn_p (OVL_CURRENT (fns)))
5128 return true;
5130 return false;
5133 /* Returns true iff class T has a move constructor that was explicitly
5134 declared in the class body. Note that this is different from
5135 "user-provided", which doesn't include functions that are defaulted in
5136 the class. */
5138 bool
5139 type_has_user_declared_move_constructor (tree t)
5141 tree fns;
5143 if (CLASSTYPE_LAZY_MOVE_CTOR (t))
5144 return false;
5146 if (!CLASSTYPE_METHOD_VEC (t))
5147 return false;
5149 for (fns = CLASSTYPE_CONSTRUCTORS (t); fns; fns = OVL_NEXT (fns))
5151 tree fn = OVL_CURRENT (fns);
5152 if (move_fn_p (fn) && !DECL_ARTIFICIAL (fn))
5153 return true;
5156 return false;
5159 /* Returns true iff class T has a move assignment operator that was
5160 explicitly declared in the class body. */
5162 bool
5163 type_has_user_declared_move_assign (tree t)
5165 tree fns;
5167 if (CLASSTYPE_LAZY_MOVE_ASSIGN (t))
5168 return false;
5170 for (fns = lookup_fnfields_slot_nolazy (t, ansi_assopname (NOP_EXPR));
5171 fns; fns = OVL_NEXT (fns))
5173 tree fn = OVL_CURRENT (fns);
5174 if (move_fn_p (fn) && !DECL_ARTIFICIAL (fn))
5175 return true;
5178 return false;
5181 /* Nonzero if we need to build up a constructor call when initializing an
5182 object of this class, either because it has a user-declared constructor
5183 or because it doesn't have a default constructor (so we need to give an
5184 error if no initializer is provided). Use TYPE_NEEDS_CONSTRUCTING when
5185 what you care about is whether or not an object can be produced by a
5186 constructor (e.g. so we don't set TREE_READONLY on const variables of
5187 such type); use this function when what you care about is whether or not
5188 to try to call a constructor to create an object. The latter case is
5189 the former plus some cases of constructors that cannot be called. */
5191 bool
5192 type_build_ctor_call (tree t)
5194 tree inner;
5195 if (TYPE_NEEDS_CONSTRUCTING (t))
5196 return true;
5197 inner = strip_array_types (t);
5198 if (!CLASS_TYPE_P (inner) || ANON_AGGR_TYPE_P (inner))
5199 return false;
5200 if (!TYPE_HAS_DEFAULT_CONSTRUCTOR (inner))
5201 return true;
5202 if (cxx_dialect < cxx11)
5203 return false;
5204 /* A user-declared constructor might be private, and a constructor might
5205 be trivial but deleted. */
5206 for (tree fns = lookup_fnfields_slot (inner, complete_ctor_identifier);
5207 fns; fns = OVL_NEXT (fns))
5209 tree fn = OVL_CURRENT (fns);
5210 if (!DECL_ARTIFICIAL (fn)
5211 || DECL_DELETED_FN (fn))
5212 return true;
5214 return false;
5217 /* Like type_build_ctor_call, but for destructors. */
5219 bool
5220 type_build_dtor_call (tree t)
5222 tree inner;
5223 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t))
5224 return true;
5225 inner = strip_array_types (t);
5226 if (!CLASS_TYPE_P (inner) || ANON_AGGR_TYPE_P (inner)
5227 || !COMPLETE_TYPE_P (inner))
5228 return false;
5229 if (cxx_dialect < cxx11)
5230 return false;
5231 /* A user-declared destructor might be private, and a destructor might
5232 be trivial but deleted. */
5233 for (tree fns = lookup_fnfields_slot (inner, complete_dtor_identifier);
5234 fns; fns = OVL_NEXT (fns))
5236 tree fn = OVL_CURRENT (fns);
5237 if (!DECL_ARTIFICIAL (fn)
5238 || DECL_DELETED_FN (fn))
5239 return true;
5241 return false;
5244 /* Remove all zero-width bit-fields from T. */
5246 static void
5247 remove_zero_width_bit_fields (tree t)
5249 tree *fieldsp;
5251 fieldsp = &TYPE_FIELDS (t);
5252 while (*fieldsp)
5254 if (TREE_CODE (*fieldsp) == FIELD_DECL
5255 && DECL_C_BIT_FIELD (*fieldsp)
5256 /* We should not be confused by the fact that grokbitfield
5257 temporarily sets the width of the bit field into
5258 DECL_INITIAL (*fieldsp).
5259 check_bitfield_decl eventually sets DECL_SIZE (*fieldsp)
5260 to that width. */
5261 && integer_zerop (DECL_SIZE (*fieldsp)))
5262 *fieldsp = DECL_CHAIN (*fieldsp);
5263 else
5264 fieldsp = &DECL_CHAIN (*fieldsp);
5268 /* Returns TRUE iff we need a cookie when dynamically allocating an
5269 array whose elements have the indicated class TYPE. */
5271 static bool
5272 type_requires_array_cookie (tree type)
5274 tree fns;
5275 bool has_two_argument_delete_p = false;
5277 gcc_assert (CLASS_TYPE_P (type));
5279 /* If there's a non-trivial destructor, we need a cookie. In order
5280 to iterate through the array calling the destructor for each
5281 element, we'll have to know how many elements there are. */
5282 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
5283 return true;
5285 /* If the usual deallocation function is a two-argument whose second
5286 argument is of type `size_t', then we have to pass the size of
5287 the array to the deallocation function, so we will need to store
5288 a cookie. */
5289 fns = lookup_fnfields (TYPE_BINFO (type),
5290 ansi_opname (VEC_DELETE_EXPR),
5291 /*protect=*/0);
5292 /* If there are no `operator []' members, or the lookup is
5293 ambiguous, then we don't need a cookie. */
5294 if (!fns || fns == error_mark_node)
5295 return false;
5296 /* Loop through all of the functions. */
5297 for (fns = BASELINK_FUNCTIONS (fns); fns; fns = OVL_NEXT (fns))
5299 tree fn;
5300 tree second_parm;
5302 /* Select the current function. */
5303 fn = OVL_CURRENT (fns);
5304 /* See if this function is a one-argument delete function. If
5305 it is, then it will be the usual deallocation function. */
5306 second_parm = TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (fn)));
5307 if (second_parm == void_list_node)
5308 return false;
5309 /* Do not consider this function if its second argument is an
5310 ellipsis. */
5311 if (!second_parm)
5312 continue;
5313 /* Otherwise, if we have a two-argument function and the second
5314 argument is `size_t', it will be the usual deallocation
5315 function -- unless there is one-argument function, too. */
5316 if (TREE_CHAIN (second_parm) == void_list_node
5317 && same_type_p (TREE_VALUE (second_parm), size_type_node))
5318 has_two_argument_delete_p = true;
5321 return has_two_argument_delete_p;
5324 /* Finish computing the `literal type' property of class type T.
5326 At this point, we have already processed base classes and
5327 non-static data members. We need to check whether the copy
5328 constructor is trivial, the destructor is trivial, and there
5329 is a trivial default constructor or at least one constexpr
5330 constructor other than the copy constructor. */
5332 static void
5333 finalize_literal_type_property (tree t)
5335 tree fn;
5337 if (cxx_dialect < cxx11
5338 || TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t))
5339 CLASSTYPE_LITERAL_P (t) = false;
5340 else if (CLASSTYPE_LITERAL_P (t) && !TYPE_HAS_TRIVIAL_DFLT (t)
5341 && CLASSTYPE_NON_AGGREGATE (t)
5342 && !TYPE_HAS_CONSTEXPR_CTOR (t))
5343 CLASSTYPE_LITERAL_P (t) = false;
5345 if (!CLASSTYPE_LITERAL_P (t))
5346 for (fn = TYPE_METHODS (t); fn; fn = DECL_CHAIN (fn))
5347 if (DECL_DECLARED_CONSTEXPR_P (fn)
5348 && TREE_CODE (fn) != TEMPLATE_DECL
5349 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
5350 && !DECL_CONSTRUCTOR_P (fn))
5352 DECL_DECLARED_CONSTEXPR_P (fn) = false;
5353 if (!DECL_GENERATED_P (fn))
5355 error ("enclosing class of constexpr non-static member "
5356 "function %q+#D is not a literal type", fn);
5357 explain_non_literal_class (t);
5362 /* T is a non-literal type used in a context which requires a constant
5363 expression. Explain why it isn't literal. */
5365 void
5366 explain_non_literal_class (tree t)
5368 static struct pointer_set_t *diagnosed;
5370 if (!CLASS_TYPE_P (t))
5371 return;
5372 t = TYPE_MAIN_VARIANT (t);
5374 if (diagnosed == NULL)
5375 diagnosed = pointer_set_create ();
5376 if (pointer_set_insert (diagnosed, t) != 0)
5377 /* Already explained. */
5378 return;
5380 inform (0, "%q+T is not literal because:", t);
5381 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t))
5382 inform (0, " %q+T has a non-trivial destructor", t);
5383 else if (CLASSTYPE_NON_AGGREGATE (t)
5384 && !TYPE_HAS_TRIVIAL_DFLT (t)
5385 && !TYPE_HAS_CONSTEXPR_CTOR (t))
5387 inform (0, " %q+T is not an aggregate, does not have a trivial "
5388 "default constructor, and has no constexpr constructor that "
5389 "is not a copy or move constructor", t);
5390 if (TYPE_HAS_DEFAULT_CONSTRUCTOR (t)
5391 && !type_has_user_provided_default_constructor (t))
5393 /* Note that we can't simply call locate_ctor because when the
5394 constructor is deleted it just returns NULL_TREE. */
5395 tree fns;
5396 for (fns = CLASSTYPE_CONSTRUCTORS (t); fns; fns = OVL_NEXT (fns))
5398 tree fn = OVL_CURRENT (fns);
5399 tree parms = TYPE_ARG_TYPES (TREE_TYPE (fn));
5401 parms = skip_artificial_parms_for (fn, parms);
5403 if (sufficient_parms_p (parms))
5405 if (DECL_DELETED_FN (fn))
5406 maybe_explain_implicit_delete (fn);
5407 else
5408 explain_invalid_constexpr_fn (fn);
5409 break;
5414 else
5416 tree binfo, base_binfo, field; int i;
5417 for (binfo = TYPE_BINFO (t), i = 0;
5418 BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
5420 tree basetype = TREE_TYPE (base_binfo);
5421 if (!CLASSTYPE_LITERAL_P (basetype))
5423 inform (0, " base class %qT of %q+T is non-literal",
5424 basetype, t);
5425 explain_non_literal_class (basetype);
5426 return;
5429 for (field = TYPE_FIELDS (t); field; field = TREE_CHAIN (field))
5431 tree ftype;
5432 if (TREE_CODE (field) != FIELD_DECL)
5433 continue;
5434 ftype = TREE_TYPE (field);
5435 if (!literal_type_p (ftype))
5437 inform (0, " non-static data member %q+D has "
5438 "non-literal type", field);
5439 if (CLASS_TYPE_P (ftype))
5440 explain_non_literal_class (ftype);
5446 /* Check the validity of the bases and members declared in T. Add any
5447 implicitly-generated functions (like copy-constructors and
5448 assignment operators). Compute various flag bits (like
5449 CLASSTYPE_NON_LAYOUT_POD_T) for T. This routine works purely at the C++
5450 level: i.e., independently of the ABI in use. */
5452 static void
5453 check_bases_and_members (tree t)
5455 /* Nonzero if the implicitly generated copy constructor should take
5456 a non-const reference argument. */
5457 int cant_have_const_ctor;
5458 /* Nonzero if the implicitly generated assignment operator
5459 should take a non-const reference argument. */
5460 int no_const_asn_ref;
5461 tree access_decls;
5462 bool saved_complex_asn_ref;
5463 bool saved_nontrivial_dtor;
5464 tree fn;
5466 /* By default, we use const reference arguments and generate default
5467 constructors. */
5468 cant_have_const_ctor = 0;
5469 no_const_asn_ref = 0;
5471 /* Check all the base-classes. */
5472 check_bases (t, &cant_have_const_ctor,
5473 &no_const_asn_ref);
5475 /* Deduce noexcept on destructors. This needs to happen after we've set
5476 triviality flags appropriately for our bases. */
5477 if (cxx_dialect >= cxx11)
5478 deduce_noexcept_on_destructors (t);
5480 /* Check all the method declarations. */
5481 check_methods (t);
5483 /* Save the initial values of these flags which only indicate whether
5484 or not the class has user-provided functions. As we analyze the
5485 bases and members we can set these flags for other reasons. */
5486 saved_complex_asn_ref = TYPE_HAS_COMPLEX_COPY_ASSIGN (t);
5487 saved_nontrivial_dtor = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t);
5489 /* Check all the data member declarations. We cannot call
5490 check_field_decls until we have called check_bases check_methods,
5491 as check_field_decls depends on TYPE_HAS_NONTRIVIAL_DESTRUCTOR
5492 being set appropriately. */
5493 check_field_decls (t, &access_decls,
5494 &cant_have_const_ctor,
5495 &no_const_asn_ref);
5497 /* A nearly-empty class has to be vptr-containing; a nearly empty
5498 class contains just a vptr. */
5499 if (!TYPE_CONTAINS_VPTR_P (t))
5500 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
5502 /* Do some bookkeeping that will guide the generation of implicitly
5503 declared member functions. */
5504 TYPE_HAS_COMPLEX_COPY_CTOR (t) |= TYPE_CONTAINS_VPTR_P (t);
5505 TYPE_HAS_COMPLEX_MOVE_CTOR (t) |= TYPE_CONTAINS_VPTR_P (t);
5506 /* We need to call a constructor for this class if it has a
5507 user-provided constructor, or if the default constructor is going
5508 to initialize the vptr. (This is not an if-and-only-if;
5509 TYPE_NEEDS_CONSTRUCTING is set elsewhere if bases or members
5510 themselves need constructing.) */
5511 TYPE_NEEDS_CONSTRUCTING (t)
5512 |= (type_has_user_provided_constructor (t) || TYPE_CONTAINS_VPTR_P (t));
5513 /* [dcl.init.aggr]
5515 An aggregate is an array or a class with no user-provided
5516 constructors ... and no virtual functions.
5518 Again, other conditions for being an aggregate are checked
5519 elsewhere. */
5520 CLASSTYPE_NON_AGGREGATE (t)
5521 |= (type_has_user_provided_constructor (t) || TYPE_POLYMORPHIC_P (t));
5522 /* This is the C++98/03 definition of POD; it changed in C++0x, but we
5523 retain the old definition internally for ABI reasons. */
5524 CLASSTYPE_NON_LAYOUT_POD_P (t)
5525 |= (CLASSTYPE_NON_AGGREGATE (t)
5526 || saved_nontrivial_dtor || saved_complex_asn_ref);
5527 CLASSTYPE_NON_STD_LAYOUT (t) |= TYPE_CONTAINS_VPTR_P (t);
5528 TYPE_HAS_COMPLEX_COPY_ASSIGN (t) |= TYPE_CONTAINS_VPTR_P (t);
5529 TYPE_HAS_COMPLEX_MOVE_ASSIGN (t) |= TYPE_CONTAINS_VPTR_P (t);
5530 TYPE_HAS_COMPLEX_DFLT (t) |= TYPE_CONTAINS_VPTR_P (t);
5532 /* If the class has no user-declared constructor, but does have
5533 non-static const or reference data members that can never be
5534 initialized, issue a warning. */
5535 if (warn_uninitialized
5536 /* Classes with user-declared constructors are presumed to
5537 initialize these members. */
5538 && !TYPE_HAS_USER_CONSTRUCTOR (t)
5539 /* Aggregates can be initialized with brace-enclosed
5540 initializers. */
5541 && CLASSTYPE_NON_AGGREGATE (t))
5543 tree field;
5545 for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
5547 tree type;
5549 if (TREE_CODE (field) != FIELD_DECL
5550 || DECL_INITIAL (field) != NULL_TREE)
5551 continue;
5553 type = TREE_TYPE (field);
5554 if (TREE_CODE (type) == REFERENCE_TYPE)
5555 warning (OPT_Wuninitialized, "non-static reference %q+#D "
5556 "in class without a constructor", field);
5557 else if (CP_TYPE_CONST_P (type)
5558 && (!CLASS_TYPE_P (type)
5559 || !TYPE_HAS_DEFAULT_CONSTRUCTOR (type)))
5560 warning (OPT_Wuninitialized, "non-static const member %q+#D "
5561 "in class without a constructor", field);
5565 /* Synthesize any needed methods. */
5566 add_implicitly_declared_members (t, &access_decls,
5567 cant_have_const_ctor,
5568 no_const_asn_ref);
5570 /* Check defaulted declarations here so we have cant_have_const_ctor
5571 and don't need to worry about clones. */
5572 for (fn = TYPE_METHODS (t); fn; fn = DECL_CHAIN (fn))
5573 if (!DECL_ARTIFICIAL (fn) && DECL_DEFAULTED_IN_CLASS_P (fn))
5575 int copy = copy_fn_p (fn);
5576 if (copy > 0)
5578 bool imp_const_p
5579 = (DECL_CONSTRUCTOR_P (fn) ? !cant_have_const_ctor
5580 : !no_const_asn_ref);
5581 bool fn_const_p = (copy == 2);
5583 if (fn_const_p && !imp_const_p)
5584 /* If the function is defaulted outside the class, we just
5585 give the synthesis error. */
5586 error ("%q+D declared to take const reference, but implicit "
5587 "declaration would take non-const", fn);
5589 defaulted_late_check (fn);
5592 if (LAMBDA_TYPE_P (t))
5594 /* "The closure type associated with a lambda-expression has a deleted
5595 default constructor and a deleted copy assignment operator." */
5596 TYPE_NEEDS_CONSTRUCTING (t) = 1;
5597 TYPE_HAS_COMPLEX_DFLT (t) = 1;
5598 TYPE_HAS_COMPLEX_COPY_ASSIGN (t) = 1;
5599 CLASSTYPE_LAZY_MOVE_ASSIGN (t) = 0;
5601 /* "This class type is not an aggregate." */
5602 CLASSTYPE_NON_AGGREGATE (t) = 1;
5605 /* Compute the 'literal type' property before we
5606 do anything with non-static member functions. */
5607 finalize_literal_type_property (t);
5609 /* Create the in-charge and not-in-charge variants of constructors
5610 and destructors. */
5611 clone_constructors_and_destructors (t);
5613 /* Process the using-declarations. */
5614 for (; access_decls; access_decls = TREE_CHAIN (access_decls))
5615 handle_using_decl (TREE_VALUE (access_decls), t);
5617 /* Build and sort the CLASSTYPE_METHOD_VEC. */
5618 finish_struct_methods (t);
5620 /* Figure out whether or not we will need a cookie when dynamically
5621 allocating an array of this type. */
5622 TYPE_LANG_SPECIFIC (t)->u.c.vec_new_uses_cookie
5623 = type_requires_array_cookie (t);
5626 /* If T needs a pointer to its virtual function table, set TYPE_VFIELD
5627 accordingly. If a new vfield was created (because T doesn't have a
5628 primary base class), then the newly created field is returned. It
5629 is not added to the TYPE_FIELDS list; it is the caller's
5630 responsibility to do that. Accumulate declared virtual functions
5631 on VIRTUALS_P. */
5633 static tree
5634 create_vtable_ptr (tree t, tree* virtuals_p)
5636 tree fn;
5638 /* Collect the virtual functions declared in T. */
5639 for (fn = TYPE_METHODS (t); fn; fn = DECL_CHAIN (fn))
5640 if (DECL_VINDEX (fn) && !DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (fn)
5641 && TREE_CODE (DECL_VINDEX (fn)) != INTEGER_CST)
5643 tree new_virtual = make_node (TREE_LIST);
5645 BV_FN (new_virtual) = fn;
5646 BV_DELTA (new_virtual) = integer_zero_node;
5647 BV_VCALL_INDEX (new_virtual) = NULL_TREE;
5649 TREE_CHAIN (new_virtual) = *virtuals_p;
5650 *virtuals_p = new_virtual;
5653 /* If we couldn't find an appropriate base class, create a new field
5654 here. Even if there weren't any new virtual functions, we might need a
5655 new virtual function table if we're supposed to include vptrs in
5656 all classes that need them. */
5657 if (!TYPE_VFIELD (t) && (*virtuals_p || TYPE_CONTAINS_VPTR_P (t)))
5659 /* We build this decl with vtbl_ptr_type_node, which is a
5660 `vtable_entry_type*'. It might seem more precise to use
5661 `vtable_entry_type (*)[N]' where N is the number of virtual
5662 functions. However, that would require the vtable pointer in
5663 base classes to have a different type than the vtable pointer
5664 in derived classes. We could make that happen, but that
5665 still wouldn't solve all the problems. In particular, the
5666 type-based alias analysis code would decide that assignments
5667 to the base class vtable pointer can't alias assignments to
5668 the derived class vtable pointer, since they have different
5669 types. Thus, in a derived class destructor, where the base
5670 class constructor was inlined, we could generate bad code for
5671 setting up the vtable pointer.
5673 Therefore, we use one type for all vtable pointers. We still
5674 use a type-correct type; it's just doesn't indicate the array
5675 bounds. That's better than using `void*' or some such; it's
5676 cleaner, and it let's the alias analysis code know that these
5677 stores cannot alias stores to void*! */
5678 tree field;
5680 field = build_decl (input_location,
5681 FIELD_DECL, get_vfield_name (t), vtbl_ptr_type_node);
5682 DECL_VIRTUAL_P (field) = 1;
5683 DECL_ARTIFICIAL (field) = 1;
5684 DECL_FIELD_CONTEXT (field) = t;
5685 DECL_FCONTEXT (field) = t;
5686 if (TYPE_PACKED (t))
5687 DECL_PACKED (field) = 1;
5689 TYPE_VFIELD (t) = field;
5691 /* This class is non-empty. */
5692 CLASSTYPE_EMPTY_P (t) = 0;
5694 return field;
5697 return NULL_TREE;
5700 /* Add OFFSET to all base types of BINFO which is a base in the
5701 hierarchy dominated by T.
5703 OFFSET, which is a type offset, is number of bytes. */
5705 static void
5706 propagate_binfo_offsets (tree binfo, tree offset)
5708 int i;
5709 tree primary_binfo;
5710 tree base_binfo;
5712 /* Update BINFO's offset. */
5713 BINFO_OFFSET (binfo)
5714 = convert (sizetype,
5715 size_binop (PLUS_EXPR,
5716 convert (ssizetype, BINFO_OFFSET (binfo)),
5717 offset));
5719 /* Find the primary base class. */
5720 primary_binfo = get_primary_binfo (binfo);
5722 if (primary_binfo && BINFO_INHERITANCE_CHAIN (primary_binfo) == binfo)
5723 propagate_binfo_offsets (primary_binfo, offset);
5725 /* Scan all of the bases, pushing the BINFO_OFFSET adjust
5726 downwards. */
5727 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
5729 /* Don't do the primary base twice. */
5730 if (base_binfo == primary_binfo)
5731 continue;
5733 if (BINFO_VIRTUAL_P (base_binfo))
5734 continue;
5736 propagate_binfo_offsets (base_binfo, offset);
5740 /* Set BINFO_OFFSET for all of the virtual bases for RLI->T. Update
5741 TYPE_ALIGN and TYPE_SIZE for T. OFFSETS gives the location of
5742 empty subobjects of T. */
5744 static void
5745 layout_virtual_bases (record_layout_info rli, splay_tree offsets)
5747 tree vbase;
5748 tree t = rli->t;
5749 bool first_vbase = true;
5750 tree *next_field;
5752 if (BINFO_N_BASE_BINFOS (TYPE_BINFO (t)) == 0)
5753 return;
5755 if (!abi_version_at_least(2))
5757 /* In G++ 3.2, we incorrectly rounded the size before laying out
5758 the virtual bases. */
5759 finish_record_layout (rli, /*free_p=*/false);
5760 #ifdef STRUCTURE_SIZE_BOUNDARY
5761 /* Packed structures don't need to have minimum size. */
5762 if (! TYPE_PACKED (t))
5763 TYPE_ALIGN (t) = MAX (TYPE_ALIGN (t), (unsigned) STRUCTURE_SIZE_BOUNDARY);
5764 #endif
5765 rli->offset = TYPE_SIZE_UNIT (t);
5766 rli->bitpos = bitsize_zero_node;
5767 rli->record_align = TYPE_ALIGN (t);
5770 /* Find the last field. The artificial fields created for virtual
5771 bases will go after the last extant field to date. */
5772 next_field = &TYPE_FIELDS (t);
5773 while (*next_field)
5774 next_field = &DECL_CHAIN (*next_field);
5776 /* Go through the virtual bases, allocating space for each virtual
5777 base that is not already a primary base class. These are
5778 allocated in inheritance graph order. */
5779 for (vbase = TYPE_BINFO (t); vbase; vbase = TREE_CHAIN (vbase))
5781 if (!BINFO_VIRTUAL_P (vbase))
5782 continue;
5784 if (!BINFO_PRIMARY_P (vbase))
5786 tree basetype = TREE_TYPE (vbase);
5788 /* This virtual base is not a primary base of any class in the
5789 hierarchy, so we have to add space for it. */
5790 next_field = build_base_field (rli, vbase,
5791 offsets, next_field);
5793 /* If the first virtual base might have been placed at a
5794 lower address, had we started from CLASSTYPE_SIZE, rather
5795 than TYPE_SIZE, issue a warning. There can be both false
5796 positives and false negatives from this warning in rare
5797 cases; to deal with all the possibilities would probably
5798 require performing both layout algorithms and comparing
5799 the results which is not particularly tractable. */
5800 if (warn_abi
5801 && first_vbase
5802 && (tree_int_cst_lt
5803 (size_binop (CEIL_DIV_EXPR,
5804 round_up_loc (input_location,
5805 CLASSTYPE_SIZE (t),
5806 CLASSTYPE_ALIGN (basetype)),
5807 bitsize_unit_node),
5808 BINFO_OFFSET (vbase))))
5809 warning (OPT_Wabi,
5810 "offset of virtual base %qT is not ABI-compliant and "
5811 "may change in a future version of GCC",
5812 basetype);
5814 first_vbase = false;
5819 /* Returns the offset of the byte just past the end of the base class
5820 BINFO. */
5822 static tree
5823 end_of_base (tree binfo)
5825 tree size;
5827 if (!CLASSTYPE_AS_BASE (BINFO_TYPE (binfo)))
5828 size = TYPE_SIZE_UNIT (char_type_node);
5829 else if (is_empty_class (BINFO_TYPE (binfo)))
5830 /* An empty class has zero CLASSTYPE_SIZE_UNIT, but we need to
5831 allocate some space for it. It cannot have virtual bases, so
5832 TYPE_SIZE_UNIT is fine. */
5833 size = TYPE_SIZE_UNIT (BINFO_TYPE (binfo));
5834 else
5835 size = CLASSTYPE_SIZE_UNIT (BINFO_TYPE (binfo));
5837 return size_binop (PLUS_EXPR, BINFO_OFFSET (binfo), size);
5840 /* Returns the offset of the byte just past the end of the base class
5841 with the highest offset in T. If INCLUDE_VIRTUALS_P is zero, then
5842 only non-virtual bases are included. */
5844 static tree
5845 end_of_class (tree t, int include_virtuals_p)
5847 tree result = size_zero_node;
5848 vec<tree, va_gc> *vbases;
5849 tree binfo;
5850 tree base_binfo;
5851 tree offset;
5852 int i;
5854 for (binfo = TYPE_BINFO (t), i = 0;
5855 BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
5857 if (!include_virtuals_p
5858 && BINFO_VIRTUAL_P (base_binfo)
5859 && (!BINFO_PRIMARY_P (base_binfo)
5860 || BINFO_INHERITANCE_CHAIN (base_binfo) != TYPE_BINFO (t)))
5861 continue;
5863 offset = end_of_base (base_binfo);
5864 if (INT_CST_LT_UNSIGNED (result, offset))
5865 result = offset;
5868 /* G++ 3.2 did not check indirect virtual bases. */
5869 if (abi_version_at_least (2) && include_virtuals_p)
5870 for (vbases = CLASSTYPE_VBASECLASSES (t), i = 0;
5871 vec_safe_iterate (vbases, i, &base_binfo); i++)
5873 offset = end_of_base (base_binfo);
5874 if (INT_CST_LT_UNSIGNED (result, offset))
5875 result = offset;
5878 return result;
5881 /* Warn about bases of T that are inaccessible because they are
5882 ambiguous. For example:
5884 struct S {};
5885 struct T : public S {};
5886 struct U : public S, public T {};
5888 Here, `(S*) new U' is not allowed because there are two `S'
5889 subobjects of U. */
5891 static void
5892 warn_about_ambiguous_bases (tree t)
5894 int i;
5895 vec<tree, va_gc> *vbases;
5896 tree basetype;
5897 tree binfo;
5898 tree base_binfo;
5900 /* If there are no repeated bases, nothing can be ambiguous. */
5901 if (!CLASSTYPE_REPEATED_BASE_P (t))
5902 return;
5904 /* Check direct bases. */
5905 for (binfo = TYPE_BINFO (t), i = 0;
5906 BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
5908 basetype = BINFO_TYPE (base_binfo);
5910 if (!uniquely_derived_from_p (basetype, t))
5911 warning (0, "direct base %qT inaccessible in %qT due to ambiguity",
5912 basetype, t);
5915 /* Check for ambiguous virtual bases. */
5916 if (extra_warnings)
5917 for (vbases = CLASSTYPE_VBASECLASSES (t), i = 0;
5918 vec_safe_iterate (vbases, i, &binfo); i++)
5920 basetype = BINFO_TYPE (binfo);
5922 if (!uniquely_derived_from_p (basetype, t))
5923 warning (OPT_Wextra, "virtual base %qT inaccessible in %qT due "
5924 "to ambiguity", basetype, t);
5928 /* Compare two INTEGER_CSTs K1 and K2. */
5930 static int
5931 splay_tree_compare_integer_csts (splay_tree_key k1, splay_tree_key k2)
5933 return tree_int_cst_compare ((tree) k1, (tree) k2);
5936 /* Increase the size indicated in RLI to account for empty classes
5937 that are "off the end" of the class. */
5939 static void
5940 include_empty_classes (record_layout_info rli)
5942 tree eoc;
5943 tree rli_size;
5945 /* It might be the case that we grew the class to allocate a
5946 zero-sized base class. That won't be reflected in RLI, yet,
5947 because we are willing to overlay multiple bases at the same
5948 offset. However, now we need to make sure that RLI is big enough
5949 to reflect the entire class. */
5950 eoc = end_of_class (rli->t,
5951 CLASSTYPE_AS_BASE (rli->t) != NULL_TREE);
5952 rli_size = rli_size_unit_so_far (rli);
5953 if (TREE_CODE (rli_size) == INTEGER_CST
5954 && INT_CST_LT_UNSIGNED (rli_size, eoc))
5956 if (!abi_version_at_least (2))
5957 /* In version 1 of the ABI, the size of a class that ends with
5958 a bitfield was not rounded up to a whole multiple of a
5959 byte. Because rli_size_unit_so_far returns only the number
5960 of fully allocated bytes, any extra bits were not included
5961 in the size. */
5962 rli->bitpos = round_down (rli->bitpos, BITS_PER_UNIT);
5963 else
5964 /* The size should have been rounded to a whole byte. */
5965 gcc_assert (tree_int_cst_equal
5966 (rli->bitpos, round_down (rli->bitpos, BITS_PER_UNIT)));
5967 rli->bitpos
5968 = size_binop (PLUS_EXPR,
5969 rli->bitpos,
5970 size_binop (MULT_EXPR,
5971 convert (bitsizetype,
5972 size_binop (MINUS_EXPR,
5973 eoc, rli_size)),
5974 bitsize_int (BITS_PER_UNIT)));
5975 normalize_rli (rli);
5979 /* Calculate the TYPE_SIZE, TYPE_ALIGN, etc for T. Calculate
5980 BINFO_OFFSETs for all of the base-classes. Position the vtable
5981 pointer. Accumulate declared virtual functions on VIRTUALS_P. */
5983 static void
5984 layout_class_type (tree t, tree *virtuals_p)
5986 tree non_static_data_members;
5987 tree field;
5988 tree vptr;
5989 record_layout_info rli;
5990 /* Maps offsets (represented as INTEGER_CSTs) to a TREE_LIST of
5991 types that appear at that offset. */
5992 splay_tree empty_base_offsets;
5993 /* True if the last field laid out was a bit-field. */
5994 bool last_field_was_bitfield = false;
5995 /* The location at which the next field should be inserted. */
5996 tree *next_field;
5997 /* T, as a base class. */
5998 tree base_t;
6000 /* Keep track of the first non-static data member. */
6001 non_static_data_members = TYPE_FIELDS (t);
6003 /* Start laying out the record. */
6004 rli = start_record_layout (t);
6006 /* Mark all the primary bases in the hierarchy. */
6007 determine_primary_bases (t);
6009 /* Create a pointer to our virtual function table. */
6010 vptr = create_vtable_ptr (t, virtuals_p);
6012 /* The vptr is always the first thing in the class. */
6013 if (vptr)
6015 DECL_CHAIN (vptr) = TYPE_FIELDS (t);
6016 TYPE_FIELDS (t) = vptr;
6017 next_field = &DECL_CHAIN (vptr);
6018 place_field (rli, vptr);
6020 else
6021 next_field = &TYPE_FIELDS (t);
6023 /* Build FIELD_DECLs for all of the non-virtual base-types. */
6024 empty_base_offsets = splay_tree_new (splay_tree_compare_integer_csts,
6025 NULL, NULL);
6026 build_base_fields (rli, empty_base_offsets, next_field);
6028 /* Layout the non-static data members. */
6029 for (field = non_static_data_members; field; field = DECL_CHAIN (field))
6031 tree type;
6032 tree padding;
6034 /* We still pass things that aren't non-static data members to
6035 the back end, in case it wants to do something with them. */
6036 if (TREE_CODE (field) != FIELD_DECL)
6038 place_field (rli, field);
6039 /* If the static data member has incomplete type, keep track
6040 of it so that it can be completed later. (The handling
6041 of pending statics in finish_record_layout is
6042 insufficient; consider:
6044 struct S1;
6045 struct S2 { static S1 s1; };
6047 At this point, finish_record_layout will be called, but
6048 S1 is still incomplete.) */
6049 if (VAR_P (field))
6051 maybe_register_incomplete_var (field);
6052 /* The visibility of static data members is determined
6053 at their point of declaration, not their point of
6054 definition. */
6055 determine_visibility (field);
6057 continue;
6060 type = TREE_TYPE (field);
6061 if (type == error_mark_node)
6062 continue;
6064 padding = NULL_TREE;
6066 /* If this field is a bit-field whose width is greater than its
6067 type, then there are some special rules for allocating
6068 it. */
6069 if (DECL_C_BIT_FIELD (field)
6070 && INT_CST_LT (TYPE_SIZE (type), DECL_SIZE (field)))
6072 unsigned int itk;
6073 tree integer_type;
6074 bool was_unnamed_p = false;
6075 /* We must allocate the bits as if suitably aligned for the
6076 longest integer type that fits in this many bits. type
6077 of the field. Then, we are supposed to use the left over
6078 bits as additional padding. */
6079 for (itk = itk_char; itk != itk_none; ++itk)
6080 if (integer_types[itk] != NULL_TREE
6081 && (INT_CST_LT (size_int (MAX_FIXED_MODE_SIZE),
6082 TYPE_SIZE (integer_types[itk]))
6083 || INT_CST_LT (DECL_SIZE (field),
6084 TYPE_SIZE (integer_types[itk]))))
6085 break;
6087 /* ITK now indicates a type that is too large for the
6088 field. We have to back up by one to find the largest
6089 type that fits. */
6092 --itk;
6093 integer_type = integer_types[itk];
6094 } while (itk > 0 && integer_type == NULL_TREE);
6096 /* Figure out how much additional padding is required. GCC
6097 3.2 always created a padding field, even if it had zero
6098 width. */
6099 if (!abi_version_at_least (2)
6100 || INT_CST_LT (TYPE_SIZE (integer_type), DECL_SIZE (field)))
6102 if (abi_version_at_least (2) && TREE_CODE (t) == UNION_TYPE)
6103 /* In a union, the padding field must have the full width
6104 of the bit-field; all fields start at offset zero. */
6105 padding = DECL_SIZE (field);
6106 else
6108 if (TREE_CODE (t) == UNION_TYPE)
6109 warning (OPT_Wabi, "size assigned to %qT may not be "
6110 "ABI-compliant and may change in a future "
6111 "version of GCC",
6113 padding = size_binop (MINUS_EXPR, DECL_SIZE (field),
6114 TYPE_SIZE (integer_type));
6117 #ifdef PCC_BITFIELD_TYPE_MATTERS
6118 /* An unnamed bitfield does not normally affect the
6119 alignment of the containing class on a target where
6120 PCC_BITFIELD_TYPE_MATTERS. But, the C++ ABI does not
6121 make any exceptions for unnamed bitfields when the
6122 bitfields are longer than their types. Therefore, we
6123 temporarily give the field a name. */
6124 if (PCC_BITFIELD_TYPE_MATTERS && !DECL_NAME (field))
6126 was_unnamed_p = true;
6127 DECL_NAME (field) = make_anon_name ();
6129 #endif
6130 DECL_SIZE (field) = TYPE_SIZE (integer_type);
6131 DECL_ALIGN (field) = TYPE_ALIGN (integer_type);
6132 DECL_USER_ALIGN (field) = TYPE_USER_ALIGN (integer_type);
6133 layout_nonempty_base_or_field (rli, field, NULL_TREE,
6134 empty_base_offsets);
6135 if (was_unnamed_p)
6136 DECL_NAME (field) = NULL_TREE;
6137 /* Now that layout has been performed, set the size of the
6138 field to the size of its declared type; the rest of the
6139 field is effectively invisible. */
6140 DECL_SIZE (field) = TYPE_SIZE (type);
6141 /* We must also reset the DECL_MODE of the field. */
6142 if (abi_version_at_least (2))
6143 DECL_MODE (field) = TYPE_MODE (type);
6144 else if (warn_abi
6145 && DECL_MODE (field) != TYPE_MODE (type))
6146 /* Versions of G++ before G++ 3.4 did not reset the
6147 DECL_MODE. */
6148 warning (OPT_Wabi,
6149 "the offset of %qD may not be ABI-compliant and may "
6150 "change in a future version of GCC", field);
6152 else
6153 layout_nonempty_base_or_field (rli, field, NULL_TREE,
6154 empty_base_offsets);
6156 /* Remember the location of any empty classes in FIELD. */
6157 if (abi_version_at_least (2))
6158 record_subobject_offsets (TREE_TYPE (field),
6159 byte_position(field),
6160 empty_base_offsets,
6161 /*is_data_member=*/true);
6163 /* If a bit-field does not immediately follow another bit-field,
6164 and yet it starts in the middle of a byte, we have failed to
6165 comply with the ABI. */
6166 if (warn_abi
6167 && DECL_C_BIT_FIELD (field)
6168 /* The TREE_NO_WARNING flag gets set by Objective-C when
6169 laying out an Objective-C class. The ObjC ABI differs
6170 from the C++ ABI, and so we do not want a warning
6171 here. */
6172 && !TREE_NO_WARNING (field)
6173 && !last_field_was_bitfield
6174 && !integer_zerop (size_binop (TRUNC_MOD_EXPR,
6175 DECL_FIELD_BIT_OFFSET (field),
6176 bitsize_unit_node)))
6177 warning (OPT_Wabi, "offset of %q+D is not ABI-compliant and may "
6178 "change in a future version of GCC", field);
6180 /* G++ used to use DECL_FIELD_OFFSET as if it were the byte
6181 offset of the field. */
6182 if (warn_abi
6183 && !abi_version_at_least (2)
6184 && !tree_int_cst_equal (DECL_FIELD_OFFSET (field),
6185 byte_position (field))
6186 && contains_empty_class_p (TREE_TYPE (field)))
6187 warning (OPT_Wabi, "%q+D contains empty classes which may cause base "
6188 "classes to be placed at different locations in a "
6189 "future version of GCC", field);
6191 /* The middle end uses the type of expressions to determine the
6192 possible range of expression values. In order to optimize
6193 "x.i > 7" to "false" for a 2-bit bitfield "i", the middle end
6194 must be made aware of the width of "i", via its type.
6196 Because C++ does not have integer types of arbitrary width,
6197 we must (for the purposes of the front end) convert from the
6198 type assigned here to the declared type of the bitfield
6199 whenever a bitfield expression is used as an rvalue.
6200 Similarly, when assigning a value to a bitfield, the value
6201 must be converted to the type given the bitfield here. */
6202 if (DECL_C_BIT_FIELD (field))
6204 unsigned HOST_WIDE_INT width;
6205 tree ftype = TREE_TYPE (field);
6206 width = tree_low_cst (DECL_SIZE (field), /*unsignedp=*/1);
6207 if (width != TYPE_PRECISION (ftype))
6209 TREE_TYPE (field)
6210 = c_build_bitfield_integer_type (width,
6211 TYPE_UNSIGNED (ftype));
6212 TREE_TYPE (field)
6213 = cp_build_qualified_type (TREE_TYPE (field),
6214 cp_type_quals (ftype));
6218 /* If we needed additional padding after this field, add it
6219 now. */
6220 if (padding)
6222 tree padding_field;
6224 padding_field = build_decl (input_location,
6225 FIELD_DECL,
6226 NULL_TREE,
6227 char_type_node);
6228 DECL_BIT_FIELD (padding_field) = 1;
6229 DECL_SIZE (padding_field) = padding;
6230 DECL_CONTEXT (padding_field) = t;
6231 DECL_ARTIFICIAL (padding_field) = 1;
6232 DECL_IGNORED_P (padding_field) = 1;
6233 layout_nonempty_base_or_field (rli, padding_field,
6234 NULL_TREE,
6235 empty_base_offsets);
6238 last_field_was_bitfield = DECL_C_BIT_FIELD (field);
6241 if (abi_version_at_least (2) && !integer_zerop (rli->bitpos))
6243 /* Make sure that we are on a byte boundary so that the size of
6244 the class without virtual bases will always be a round number
6245 of bytes. */
6246 rli->bitpos = round_up_loc (input_location, rli->bitpos, BITS_PER_UNIT);
6247 normalize_rli (rli);
6250 /* G++ 3.2 does not allow virtual bases to be overlaid with tail
6251 padding. */
6252 if (!abi_version_at_least (2))
6253 include_empty_classes(rli);
6255 /* Delete all zero-width bit-fields from the list of fields. Now
6256 that the type is laid out they are no longer important. */
6257 remove_zero_width_bit_fields (t);
6259 /* Create the version of T used for virtual bases. We do not use
6260 make_class_type for this version; this is an artificial type. For
6261 a POD type, we just reuse T. */
6262 if (CLASSTYPE_NON_LAYOUT_POD_P (t) || CLASSTYPE_EMPTY_P (t))
6264 base_t = make_node (TREE_CODE (t));
6266 /* Set the size and alignment for the new type. In G++ 3.2, all
6267 empty classes were considered to have size zero when used as
6268 base classes. */
6269 if (!abi_version_at_least (2) && CLASSTYPE_EMPTY_P (t))
6271 TYPE_SIZE (base_t) = bitsize_zero_node;
6272 TYPE_SIZE_UNIT (base_t) = size_zero_node;
6273 if (warn_abi && !integer_zerop (rli_size_unit_so_far (rli)))
6274 warning (OPT_Wabi,
6275 "layout of classes derived from empty class %qT "
6276 "may change in a future version of GCC",
6279 else
6281 tree eoc;
6283 /* If the ABI version is not at least two, and the last
6284 field was a bit-field, RLI may not be on a byte
6285 boundary. In particular, rli_size_unit_so_far might
6286 indicate the last complete byte, while rli_size_so_far
6287 indicates the total number of bits used. Therefore,
6288 rli_size_so_far, rather than rli_size_unit_so_far, is
6289 used to compute TYPE_SIZE_UNIT. */
6290 eoc = end_of_class (t, /*include_virtuals_p=*/0);
6291 TYPE_SIZE_UNIT (base_t)
6292 = size_binop (MAX_EXPR,
6293 convert (sizetype,
6294 size_binop (CEIL_DIV_EXPR,
6295 rli_size_so_far (rli),
6296 bitsize_int (BITS_PER_UNIT))),
6297 eoc);
6298 TYPE_SIZE (base_t)
6299 = size_binop (MAX_EXPR,
6300 rli_size_so_far (rli),
6301 size_binop (MULT_EXPR,
6302 convert (bitsizetype, eoc),
6303 bitsize_int (BITS_PER_UNIT)));
6305 TYPE_ALIGN (base_t) = rli->record_align;
6306 TYPE_USER_ALIGN (base_t) = TYPE_USER_ALIGN (t);
6308 /* Copy the fields from T. */
6309 next_field = &TYPE_FIELDS (base_t);
6310 for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
6311 if (TREE_CODE (field) == FIELD_DECL)
6313 *next_field = build_decl (input_location,
6314 FIELD_DECL,
6315 DECL_NAME (field),
6316 TREE_TYPE (field));
6317 DECL_CONTEXT (*next_field) = base_t;
6318 DECL_FIELD_OFFSET (*next_field) = DECL_FIELD_OFFSET (field);
6319 DECL_FIELD_BIT_OFFSET (*next_field)
6320 = DECL_FIELD_BIT_OFFSET (field);
6321 DECL_SIZE (*next_field) = DECL_SIZE (field);
6322 DECL_MODE (*next_field) = DECL_MODE (field);
6323 next_field = &DECL_CHAIN (*next_field);
6326 /* Record the base version of the type. */
6327 CLASSTYPE_AS_BASE (t) = base_t;
6328 TYPE_CONTEXT (base_t) = t;
6330 else
6331 CLASSTYPE_AS_BASE (t) = t;
6333 /* Every empty class contains an empty class. */
6334 if (CLASSTYPE_EMPTY_P (t))
6335 CLASSTYPE_CONTAINS_EMPTY_CLASS_P (t) = 1;
6337 /* Set the TYPE_DECL for this type to contain the right
6338 value for DECL_OFFSET, so that we can use it as part
6339 of a COMPONENT_REF for multiple inheritance. */
6340 layout_decl (TYPE_MAIN_DECL (t), 0);
6342 /* Now fix up any virtual base class types that we left lying
6343 around. We must get these done before we try to lay out the
6344 virtual function table. As a side-effect, this will remove the
6345 base subobject fields. */
6346 layout_virtual_bases (rli, empty_base_offsets);
6348 /* Make sure that empty classes are reflected in RLI at this
6349 point. */
6350 include_empty_classes(rli);
6352 /* Make sure not to create any structures with zero size. */
6353 if (integer_zerop (rli_size_unit_so_far (rli)) && CLASSTYPE_EMPTY_P (t))
6354 place_field (rli,
6355 build_decl (input_location,
6356 FIELD_DECL, NULL_TREE, char_type_node));
6358 /* If this is a non-POD, declaring it packed makes a difference to how it
6359 can be used as a field; don't let finalize_record_size undo it. */
6360 if (TYPE_PACKED (t) && !layout_pod_type_p (t))
6361 rli->packed_maybe_necessary = true;
6363 /* Let the back end lay out the type. */
6364 finish_record_layout (rli, /*free_p=*/true);
6366 if (TYPE_SIZE_UNIT (t)
6367 && TREE_CODE (TYPE_SIZE_UNIT (t)) == INTEGER_CST
6368 && !TREE_OVERFLOW (TYPE_SIZE_UNIT (t))
6369 && !valid_constant_size_p (TYPE_SIZE_UNIT (t)))
6370 error ("type %qT is too large", t);
6372 /* Warn about bases that can't be talked about due to ambiguity. */
6373 warn_about_ambiguous_bases (t);
6375 /* Now that we're done with layout, give the base fields the real types. */
6376 for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
6377 if (DECL_ARTIFICIAL (field) && IS_FAKE_BASE_TYPE (TREE_TYPE (field)))
6378 TREE_TYPE (field) = TYPE_CONTEXT (TREE_TYPE (field));
6380 /* Clean up. */
6381 splay_tree_delete (empty_base_offsets);
6383 if (CLASSTYPE_EMPTY_P (t)
6384 && tree_int_cst_lt (sizeof_biggest_empty_class,
6385 TYPE_SIZE_UNIT (t)))
6386 sizeof_biggest_empty_class = TYPE_SIZE_UNIT (t);
6389 /* Determine the "key method" for the class type indicated by TYPE,
6390 and set CLASSTYPE_KEY_METHOD accordingly. */
6392 void
6393 determine_key_method (tree type)
6395 tree method;
6397 if (TYPE_FOR_JAVA (type)
6398 || processing_template_decl
6399 || CLASSTYPE_TEMPLATE_INSTANTIATION (type)
6400 || CLASSTYPE_INTERFACE_KNOWN (type))
6401 return;
6403 /* The key method is the first non-pure virtual function that is not
6404 inline at the point of class definition. On some targets the
6405 key function may not be inline; those targets should not call
6406 this function until the end of the translation unit. */
6407 for (method = TYPE_METHODS (type); method != NULL_TREE;
6408 method = DECL_CHAIN (method))
6409 if (DECL_VINDEX (method) != NULL_TREE
6410 && ! DECL_DECLARED_INLINE_P (method)
6411 && ! DECL_PURE_VIRTUAL_P (method))
6413 CLASSTYPE_KEY_METHOD (type) = method;
6414 break;
6417 return;
6421 /* Allocate and return an instance of struct sorted_fields_type with
6422 N fields. */
6424 static struct sorted_fields_type *
6425 sorted_fields_type_new (int n)
6427 struct sorted_fields_type *sft;
6428 sft = ggc_alloc_sorted_fields_type (sizeof (struct sorted_fields_type)
6429 + n * sizeof (tree));
6430 sft->len = n;
6432 return sft;
6436 /* Perform processing required when the definition of T (a class type)
6437 is complete. */
6439 void
6440 finish_struct_1 (tree t)
6442 tree x;
6443 /* A TREE_LIST. The TREE_VALUE of each node is a FUNCTION_DECL. */
6444 tree virtuals = NULL_TREE;
6446 if (COMPLETE_TYPE_P (t))
6448 gcc_assert (MAYBE_CLASS_TYPE_P (t));
6449 error ("redefinition of %q#T", t);
6450 popclass ();
6451 return;
6454 /* If this type was previously laid out as a forward reference,
6455 make sure we lay it out again. */
6456 TYPE_SIZE (t) = NULL_TREE;
6457 CLASSTYPE_PRIMARY_BINFO (t) = NULL_TREE;
6459 /* Make assumptions about the class; we'll reset the flags if
6460 necessary. */
6461 CLASSTYPE_EMPTY_P (t) = 1;
6462 CLASSTYPE_NEARLY_EMPTY_P (t) = 1;
6463 CLASSTYPE_CONTAINS_EMPTY_CLASS_P (t) = 0;
6464 CLASSTYPE_LITERAL_P (t) = true;
6466 /* Do end-of-class semantic processing: checking the validity of the
6467 bases and members and add implicitly generated methods. */
6468 check_bases_and_members (t);
6470 /* Find the key method. */
6471 if (TYPE_CONTAINS_VPTR_P (t))
6473 /* The Itanium C++ ABI permits the key method to be chosen when
6474 the class is defined -- even though the key method so
6475 selected may later turn out to be an inline function. On
6476 some systems (such as ARM Symbian OS) the key method cannot
6477 be determined until the end of the translation unit. On such
6478 systems, we leave CLASSTYPE_KEY_METHOD set to NULL, which
6479 will cause the class to be added to KEYED_CLASSES. Then, in
6480 finish_file we will determine the key method. */
6481 if (targetm.cxx.key_method_may_be_inline ())
6482 determine_key_method (t);
6484 /* If a polymorphic class has no key method, we may emit the vtable
6485 in every translation unit where the class definition appears. */
6486 if (CLASSTYPE_KEY_METHOD (t) == NULL_TREE)
6487 keyed_classes = tree_cons (NULL_TREE, t, keyed_classes);
6490 /* Layout the class itself. */
6491 layout_class_type (t, &virtuals);
6492 if (CLASSTYPE_AS_BASE (t) != t)
6493 /* We use the base type for trivial assignments, and hence it
6494 needs a mode. */
6495 compute_record_mode (CLASSTYPE_AS_BASE (t));
6497 virtuals = modify_all_vtables (t, nreverse (virtuals));
6499 /* If necessary, create the primary vtable for this class. */
6500 if (virtuals || TYPE_CONTAINS_VPTR_P (t))
6502 /* We must enter these virtuals into the table. */
6503 if (!CLASSTYPE_HAS_PRIMARY_BASE_P (t))
6504 build_primary_vtable (NULL_TREE, t);
6505 else if (! BINFO_NEW_VTABLE_MARKED (TYPE_BINFO (t)))
6506 /* Here we know enough to change the type of our virtual
6507 function table, but we will wait until later this function. */
6508 build_primary_vtable (CLASSTYPE_PRIMARY_BINFO (t), t);
6510 /* If we're warning about ABI tags, check the types of the new
6511 virtual functions. */
6512 if (warn_abi_tag)
6513 for (tree v = virtuals; v; v = TREE_CHAIN (v))
6514 check_abi_tags (t, TREE_VALUE (v));
6517 if (TYPE_CONTAINS_VPTR_P (t))
6519 int vindex;
6520 tree fn;
6522 if (BINFO_VTABLE (TYPE_BINFO (t)))
6523 gcc_assert (DECL_VIRTUAL_P (BINFO_VTABLE (TYPE_BINFO (t))));
6524 if (!CLASSTYPE_HAS_PRIMARY_BASE_P (t))
6525 gcc_assert (BINFO_VIRTUALS (TYPE_BINFO (t)) == NULL_TREE);
6527 /* Add entries for virtual functions introduced by this class. */
6528 BINFO_VIRTUALS (TYPE_BINFO (t))
6529 = chainon (BINFO_VIRTUALS (TYPE_BINFO (t)), virtuals);
6531 /* Set DECL_VINDEX for all functions declared in this class. */
6532 for (vindex = 0, fn = BINFO_VIRTUALS (TYPE_BINFO (t));
6534 fn = TREE_CHAIN (fn),
6535 vindex += (TARGET_VTABLE_USES_DESCRIPTORS
6536 ? TARGET_VTABLE_USES_DESCRIPTORS : 1))
6538 tree fndecl = BV_FN (fn);
6540 if (DECL_THUNK_P (fndecl))
6541 /* A thunk. We should never be calling this entry directly
6542 from this vtable -- we'd use the entry for the non
6543 thunk base function. */
6544 DECL_VINDEX (fndecl) = NULL_TREE;
6545 else if (TREE_CODE (DECL_VINDEX (fndecl)) != INTEGER_CST)
6546 DECL_VINDEX (fndecl) = build_int_cst (NULL_TREE, vindex);
6550 finish_struct_bits (t);
6551 set_method_tm_attributes (t);
6553 /* Complete the rtl for any static member objects of the type we're
6554 working on. */
6555 for (x = TYPE_FIELDS (t); x; x = DECL_CHAIN (x))
6556 if (VAR_P (x) && TREE_STATIC (x)
6557 && TREE_TYPE (x) != error_mark_node
6558 && same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (x)), t))
6559 DECL_MODE (x) = TYPE_MODE (t);
6561 /* Done with FIELDS...now decide whether to sort these for
6562 faster lookups later.
6564 We use a small number because most searches fail (succeeding
6565 ultimately as the search bores through the inheritance
6566 hierarchy), and we want this failure to occur quickly. */
6568 insert_into_classtype_sorted_fields (TYPE_FIELDS (t), t, 8);
6570 /* Complain if one of the field types requires lower visibility. */
6571 constrain_class_visibility (t);
6573 /* Make the rtl for any new vtables we have created, and unmark
6574 the base types we marked. */
6575 finish_vtbls (t);
6577 /* Build the VTT for T. */
6578 build_vtt (t);
6580 /* This warning does not make sense for Java classes, since they
6581 cannot have destructors. */
6582 if (!TYPE_FOR_JAVA (t) && warn_nonvdtor && TYPE_POLYMORPHIC_P (t))
6584 tree dtor;
6586 dtor = CLASSTYPE_DESTRUCTORS (t);
6587 if (/* An implicitly declared destructor is always public. And,
6588 if it were virtual, we would have created it by now. */
6589 !dtor
6590 || (!DECL_VINDEX (dtor)
6591 && (/* public non-virtual */
6592 (!TREE_PRIVATE (dtor) && !TREE_PROTECTED (dtor))
6593 || (/* non-public non-virtual with friends */
6594 (TREE_PRIVATE (dtor) || TREE_PROTECTED (dtor))
6595 && (CLASSTYPE_FRIEND_CLASSES (t)
6596 || DECL_FRIENDLIST (TYPE_MAIN_DECL (t)))))))
6597 warning (OPT_Wnon_virtual_dtor,
6598 "%q#T has virtual functions and accessible"
6599 " non-virtual destructor", t);
6602 complete_vars (t);
6604 if (warn_overloaded_virtual)
6605 warn_hidden (t);
6607 /* Class layout, assignment of virtual table slots, etc., is now
6608 complete. Give the back end a chance to tweak the visibility of
6609 the class or perform any other required target modifications. */
6610 targetm.cxx.adjust_class_at_definition (t);
6612 maybe_suppress_debug_info (t);
6614 if (flag_vtable_verify)
6615 vtv_save_class_info (t);
6617 dump_class_hierarchy (t);
6619 /* Finish debugging output for this type. */
6620 rest_of_type_compilation (t, ! LOCAL_CLASS_P (t));
6622 if (TYPE_TRANSPARENT_AGGR (t))
6624 tree field = first_field (t);
6625 if (field == NULL_TREE || error_operand_p (field))
6627 error ("type transparent %q#T does not have any fields", t);
6628 TYPE_TRANSPARENT_AGGR (t) = 0;
6630 else if (DECL_ARTIFICIAL (field))
6632 if (DECL_FIELD_IS_BASE (field))
6633 error ("type transparent class %qT has base classes", t);
6634 else
6636 gcc_checking_assert (DECL_VIRTUAL_P (field));
6637 error ("type transparent class %qT has virtual functions", t);
6639 TYPE_TRANSPARENT_AGGR (t) = 0;
6641 else if (TYPE_MODE (t) != DECL_MODE (field))
6643 error ("type transparent %q#T cannot be made transparent because "
6644 "the type of the first field has a different ABI from the "
6645 "class overall", t);
6646 TYPE_TRANSPARENT_AGGR (t) = 0;
6651 /* Insert FIELDS into T for the sorted case if the FIELDS count is
6652 equal to THRESHOLD or greater than THRESHOLD. */
6654 static void
6655 insert_into_classtype_sorted_fields (tree fields, tree t, int threshold)
6657 int n_fields = count_fields (fields);
6658 if (n_fields >= threshold)
6660 struct sorted_fields_type *field_vec = sorted_fields_type_new (n_fields);
6661 add_fields_to_record_type (fields, field_vec, 0);
6662 qsort (field_vec->elts, n_fields, sizeof (tree), field_decl_cmp);
6663 CLASSTYPE_SORTED_FIELDS (t) = field_vec;
6667 /* Insert lately defined enum ENUMTYPE into T for the sorted case. */
6669 void
6670 insert_late_enum_def_into_classtype_sorted_fields (tree enumtype, tree t)
6672 struct sorted_fields_type *sorted_fields = CLASSTYPE_SORTED_FIELDS (t);
6673 if (sorted_fields)
6675 int i;
6676 int n_fields
6677 = list_length (TYPE_VALUES (enumtype)) + sorted_fields->len;
6678 struct sorted_fields_type *field_vec = sorted_fields_type_new (n_fields);
6680 for (i = 0; i < sorted_fields->len; ++i)
6681 field_vec->elts[i] = sorted_fields->elts[i];
6683 add_enum_fields_to_record_type (enumtype, field_vec,
6684 sorted_fields->len);
6685 qsort (field_vec->elts, n_fields, sizeof (tree), field_decl_cmp);
6686 CLASSTYPE_SORTED_FIELDS (t) = field_vec;
6690 /* When T was built up, the member declarations were added in reverse
6691 order. Rearrange them to declaration order. */
6693 void
6694 unreverse_member_declarations (tree t)
6696 tree next;
6697 tree prev;
6698 tree x;
6700 /* The following lists are all in reverse order. Put them in
6701 declaration order now. */
6702 TYPE_METHODS (t) = nreverse (TYPE_METHODS (t));
6703 CLASSTYPE_DECL_LIST (t) = nreverse (CLASSTYPE_DECL_LIST (t));
6705 /* Actually, for the TYPE_FIELDS, only the non TYPE_DECLs are in
6706 reverse order, so we can't just use nreverse. */
6707 prev = NULL_TREE;
6708 for (x = TYPE_FIELDS (t);
6709 x && TREE_CODE (x) != TYPE_DECL;
6710 x = next)
6712 next = DECL_CHAIN (x);
6713 DECL_CHAIN (x) = prev;
6714 prev = x;
6716 if (prev)
6718 DECL_CHAIN (TYPE_FIELDS (t)) = x;
6719 if (prev)
6720 TYPE_FIELDS (t) = prev;
6724 tree
6725 finish_struct (tree t, tree attributes)
6727 location_t saved_loc = input_location;
6729 /* Now that we've got all the field declarations, reverse everything
6730 as necessary. */
6731 unreverse_member_declarations (t);
6733 cplus_decl_attributes (&t, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
6735 /* Nadger the current location so that diagnostics point to the start of
6736 the struct, not the end. */
6737 input_location = DECL_SOURCE_LOCATION (TYPE_NAME (t));
6739 if (processing_template_decl)
6741 tree x;
6743 finish_struct_methods (t);
6744 TYPE_SIZE (t) = bitsize_zero_node;
6745 TYPE_SIZE_UNIT (t) = size_zero_node;
6747 /* We need to emit an error message if this type was used as a parameter
6748 and it is an abstract type, even if it is a template. We construct
6749 a simple CLASSTYPE_PURE_VIRTUALS list without taking bases into
6750 account and we call complete_vars with this type, which will check
6751 the PARM_DECLS. Note that while the type is being defined,
6752 CLASSTYPE_PURE_VIRTUALS contains the list of the inline friends
6753 (see CLASSTYPE_INLINE_FRIENDS) so we need to clear it. */
6754 CLASSTYPE_PURE_VIRTUALS (t) = NULL;
6755 for (x = TYPE_METHODS (t); x; x = DECL_CHAIN (x))
6756 if (DECL_PURE_VIRTUAL_P (x))
6757 vec_safe_push (CLASSTYPE_PURE_VIRTUALS (t), x);
6758 complete_vars (t);
6759 /* We need to add the target functions to the CLASSTYPE_METHOD_VEC if
6760 an enclosing scope is a template class, so that this function be
6761 found by lookup_fnfields_1 when the using declaration is not
6762 instantiated yet. */
6763 for (x = TYPE_FIELDS (t); x; x = DECL_CHAIN (x))
6764 if (TREE_CODE (x) == USING_DECL)
6766 tree fn = strip_using_decl (x);
6767 if (is_overloaded_fn (fn))
6768 for (; fn; fn = OVL_NEXT (fn))
6769 add_method (t, OVL_CURRENT (fn), x);
6772 /* Remember current #pragma pack value. */
6773 TYPE_PRECISION (t) = maximum_field_alignment;
6775 /* Fix up any variants we've already built. */
6776 for (x = TYPE_NEXT_VARIANT (t); x; x = TYPE_NEXT_VARIANT (x))
6778 TYPE_SIZE (x) = TYPE_SIZE (t);
6779 TYPE_SIZE_UNIT (x) = TYPE_SIZE_UNIT (t);
6780 TYPE_FIELDS (x) = TYPE_FIELDS (t);
6781 TYPE_METHODS (x) = TYPE_METHODS (t);
6784 else
6785 finish_struct_1 (t);
6787 input_location = saved_loc;
6789 TYPE_BEING_DEFINED (t) = 0;
6791 if (current_class_type)
6792 popclass ();
6793 else
6794 error ("trying to finish struct, but kicked out due to previous parse errors");
6796 if (processing_template_decl && at_function_scope_p ()
6797 /* Lambdas are defined by the LAMBDA_EXPR. */
6798 && !LAMBDA_TYPE_P (t))
6799 add_stmt (build_min (TAG_DEFN, t));
6801 return t;
6804 /* Hash table to avoid endless recursion when handling references. */
6805 static hash_table <pointer_hash <tree_node> > fixed_type_or_null_ref_ht;
6807 /* Return the dynamic type of INSTANCE, if known.
6808 Used to determine whether the virtual function table is needed
6809 or not.
6811 *NONNULL is set iff INSTANCE can be known to be nonnull, regardless
6812 of our knowledge of its type. *NONNULL should be initialized
6813 before this function is called. */
6815 static tree
6816 fixed_type_or_null (tree instance, int *nonnull, int *cdtorp)
6818 #define RECUR(T) fixed_type_or_null((T), nonnull, cdtorp)
6820 switch (TREE_CODE (instance))
6822 case INDIRECT_REF:
6823 if (POINTER_TYPE_P (TREE_TYPE (instance)))
6824 return NULL_TREE;
6825 else
6826 return RECUR (TREE_OPERAND (instance, 0));
6828 case CALL_EXPR:
6829 /* This is a call to a constructor, hence it's never zero. */
6830 if (TREE_HAS_CONSTRUCTOR (instance))
6832 if (nonnull)
6833 *nonnull = 1;
6834 return TREE_TYPE (instance);
6836 return NULL_TREE;
6838 case SAVE_EXPR:
6839 /* This is a call to a constructor, hence it's never zero. */
6840 if (TREE_HAS_CONSTRUCTOR (instance))
6842 if (nonnull)
6843 *nonnull = 1;
6844 return TREE_TYPE (instance);
6846 return RECUR (TREE_OPERAND (instance, 0));
6848 case POINTER_PLUS_EXPR:
6849 case PLUS_EXPR:
6850 case MINUS_EXPR:
6851 if (TREE_CODE (TREE_OPERAND (instance, 0)) == ADDR_EXPR)
6852 return RECUR (TREE_OPERAND (instance, 0));
6853 if (TREE_CODE (TREE_OPERAND (instance, 1)) == INTEGER_CST)
6854 /* Propagate nonnull. */
6855 return RECUR (TREE_OPERAND (instance, 0));
6857 return NULL_TREE;
6859 CASE_CONVERT:
6860 return RECUR (TREE_OPERAND (instance, 0));
6862 case ADDR_EXPR:
6863 instance = TREE_OPERAND (instance, 0);
6864 if (nonnull)
6866 /* Just because we see an ADDR_EXPR doesn't mean we're dealing
6867 with a real object -- given &p->f, p can still be null. */
6868 tree t = get_base_address (instance);
6869 /* ??? Probably should check DECL_WEAK here. */
6870 if (t && DECL_P (t))
6871 *nonnull = 1;
6873 return RECUR (instance);
6875 case COMPONENT_REF:
6876 /* If this component is really a base class reference, then the field
6877 itself isn't definitive. */
6878 if (DECL_FIELD_IS_BASE (TREE_OPERAND (instance, 1)))
6879 return RECUR (TREE_OPERAND (instance, 0));
6880 return RECUR (TREE_OPERAND (instance, 1));
6882 case VAR_DECL:
6883 case FIELD_DECL:
6884 if (TREE_CODE (TREE_TYPE (instance)) == ARRAY_TYPE
6885 && MAYBE_CLASS_TYPE_P (TREE_TYPE (TREE_TYPE (instance))))
6887 if (nonnull)
6888 *nonnull = 1;
6889 return TREE_TYPE (TREE_TYPE (instance));
6891 /* fall through... */
6892 case TARGET_EXPR:
6893 case PARM_DECL:
6894 case RESULT_DECL:
6895 if (MAYBE_CLASS_TYPE_P (TREE_TYPE (instance)))
6897 if (nonnull)
6898 *nonnull = 1;
6899 return TREE_TYPE (instance);
6901 else if (instance == current_class_ptr)
6903 if (nonnull)
6904 *nonnull = 1;
6906 /* if we're in a ctor or dtor, we know our type. If
6907 current_class_ptr is set but we aren't in a function, we're in
6908 an NSDMI (and therefore a constructor). */
6909 if (current_scope () != current_function_decl
6910 || (DECL_LANG_SPECIFIC (current_function_decl)
6911 && (DECL_CONSTRUCTOR_P (current_function_decl)
6912 || DECL_DESTRUCTOR_P (current_function_decl))))
6914 if (cdtorp)
6915 *cdtorp = 1;
6916 return TREE_TYPE (TREE_TYPE (instance));
6919 else if (TREE_CODE (TREE_TYPE (instance)) == REFERENCE_TYPE)
6921 /* We only need one hash table because it is always left empty. */
6922 if (!fixed_type_or_null_ref_ht.is_created ())
6923 fixed_type_or_null_ref_ht.create (37);
6925 /* Reference variables should be references to objects. */
6926 if (nonnull)
6927 *nonnull = 1;
6929 /* Enter the INSTANCE in a table to prevent recursion; a
6930 variable's initializer may refer to the variable
6931 itself. */
6932 if (VAR_P (instance)
6933 && DECL_INITIAL (instance)
6934 && !type_dependent_expression_p_push (DECL_INITIAL (instance))
6935 && !fixed_type_or_null_ref_ht.find (instance))
6937 tree type;
6938 tree_node **slot;
6940 slot = fixed_type_or_null_ref_ht.find_slot (instance, INSERT);
6941 *slot = instance;
6942 type = RECUR (DECL_INITIAL (instance));
6943 fixed_type_or_null_ref_ht.remove_elt (instance);
6945 return type;
6948 return NULL_TREE;
6950 default:
6951 return NULL_TREE;
6953 #undef RECUR
6956 /* Return nonzero if the dynamic type of INSTANCE is known, and
6957 equivalent to the static type. We also handle the case where
6958 INSTANCE is really a pointer. Return negative if this is a
6959 ctor/dtor. There the dynamic type is known, but this might not be
6960 the most derived base of the original object, and hence virtual
6961 bases may not be laid out according to this type.
6963 Used to determine whether the virtual function table is needed
6964 or not.
6966 *NONNULL is set iff INSTANCE can be known to be nonnull, regardless
6967 of our knowledge of its type. *NONNULL should be initialized
6968 before this function is called. */
6971 resolves_to_fixed_type_p (tree instance, int* nonnull)
6973 tree t = TREE_TYPE (instance);
6974 int cdtorp = 0;
6975 tree fixed;
6977 /* processing_template_decl can be false in a template if we're in
6978 fold_non_dependent_expr, but we still want to suppress this check. */
6979 if (in_template_function ())
6981 /* In a template we only care about the type of the result. */
6982 if (nonnull)
6983 *nonnull = true;
6984 return true;
6987 fixed = fixed_type_or_null (instance, nonnull, &cdtorp);
6988 if (fixed == NULL_TREE)
6989 return 0;
6990 if (POINTER_TYPE_P (t))
6991 t = TREE_TYPE (t);
6992 if (!same_type_ignoring_top_level_qualifiers_p (t, fixed))
6993 return 0;
6994 return cdtorp ? -1 : 1;
6998 void
6999 init_class_processing (void)
7001 current_class_depth = 0;
7002 current_class_stack_size = 10;
7003 current_class_stack
7004 = XNEWVEC (struct class_stack_node, current_class_stack_size);
7005 vec_alloc (local_classes, 8);
7006 sizeof_biggest_empty_class = size_zero_node;
7008 ridpointers[(int) RID_PUBLIC] = access_public_node;
7009 ridpointers[(int) RID_PRIVATE] = access_private_node;
7010 ridpointers[(int) RID_PROTECTED] = access_protected_node;
7013 /* Restore the cached PREVIOUS_CLASS_LEVEL. */
7015 static void
7016 restore_class_cache (void)
7018 tree type;
7020 /* We are re-entering the same class we just left, so we don't
7021 have to search the whole inheritance matrix to find all the
7022 decls to bind again. Instead, we install the cached
7023 class_shadowed list and walk through it binding names. */
7024 push_binding_level (previous_class_level);
7025 class_binding_level = previous_class_level;
7026 /* Restore IDENTIFIER_TYPE_VALUE. */
7027 for (type = class_binding_level->type_shadowed;
7028 type;
7029 type = TREE_CHAIN (type))
7030 SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (type), TREE_TYPE (type));
7033 /* Set global variables CURRENT_CLASS_NAME and CURRENT_CLASS_TYPE as
7034 appropriate for TYPE.
7036 So that we may avoid calls to lookup_name, we cache the _TYPE
7037 nodes of local TYPE_DECLs in the TREE_TYPE field of the name.
7039 For multiple inheritance, we perform a two-pass depth-first search
7040 of the type lattice. */
7042 void
7043 pushclass (tree type)
7045 class_stack_node_t csn;
7047 type = TYPE_MAIN_VARIANT (type);
7049 /* Make sure there is enough room for the new entry on the stack. */
7050 if (current_class_depth + 1 >= current_class_stack_size)
7052 current_class_stack_size *= 2;
7053 current_class_stack
7054 = XRESIZEVEC (struct class_stack_node, current_class_stack,
7055 current_class_stack_size);
7058 /* Insert a new entry on the class stack. */
7059 csn = current_class_stack + current_class_depth;
7060 csn->name = current_class_name;
7061 csn->type = current_class_type;
7062 csn->access = current_access_specifier;
7063 csn->names_used = 0;
7064 csn->hidden = 0;
7065 current_class_depth++;
7067 /* Now set up the new type. */
7068 current_class_name = TYPE_NAME (type);
7069 if (TREE_CODE (current_class_name) == TYPE_DECL)
7070 current_class_name = DECL_NAME (current_class_name);
7071 current_class_type = type;
7073 /* By default, things in classes are private, while things in
7074 structures or unions are public. */
7075 current_access_specifier = (CLASSTYPE_DECLARED_CLASS (type)
7076 ? access_private_node
7077 : access_public_node);
7079 if (previous_class_level
7080 && type != previous_class_level->this_entity
7081 && current_class_depth == 1)
7083 /* Forcibly remove any old class remnants. */
7084 invalidate_class_lookup_cache ();
7087 if (!previous_class_level
7088 || type != previous_class_level->this_entity
7089 || current_class_depth > 1)
7090 pushlevel_class ();
7091 else
7092 restore_class_cache ();
7095 /* When we exit a toplevel class scope, we save its binding level so
7096 that we can restore it quickly. Here, we've entered some other
7097 class, so we must invalidate our cache. */
7099 void
7100 invalidate_class_lookup_cache (void)
7102 previous_class_level = NULL;
7105 /* Get out of the current class scope. If we were in a class scope
7106 previously, that is the one popped to. */
7108 void
7109 popclass (void)
7111 poplevel_class ();
7113 current_class_depth--;
7114 current_class_name = current_class_stack[current_class_depth].name;
7115 current_class_type = current_class_stack[current_class_depth].type;
7116 current_access_specifier = current_class_stack[current_class_depth].access;
7117 if (current_class_stack[current_class_depth].names_used)
7118 splay_tree_delete (current_class_stack[current_class_depth].names_used);
7121 /* Mark the top of the class stack as hidden. */
7123 void
7124 push_class_stack (void)
7126 if (current_class_depth)
7127 ++current_class_stack[current_class_depth - 1].hidden;
7130 /* Mark the top of the class stack as un-hidden. */
7132 void
7133 pop_class_stack (void)
7135 if (current_class_depth)
7136 --current_class_stack[current_class_depth - 1].hidden;
7139 /* Returns 1 if the class type currently being defined is either T or
7140 a nested type of T. */
7142 bool
7143 currently_open_class (tree t)
7145 int i;
7147 if (!CLASS_TYPE_P (t))
7148 return false;
7150 t = TYPE_MAIN_VARIANT (t);
7152 /* We start looking from 1 because entry 0 is from global scope,
7153 and has no type. */
7154 for (i = current_class_depth; i > 0; --i)
7156 tree c;
7157 if (i == current_class_depth)
7158 c = current_class_type;
7159 else
7161 if (current_class_stack[i].hidden)
7162 break;
7163 c = current_class_stack[i].type;
7165 if (!c)
7166 continue;
7167 if (same_type_p (c, t))
7168 return true;
7170 return false;
7173 /* If either current_class_type or one of its enclosing classes are derived
7174 from T, return the appropriate type. Used to determine how we found
7175 something via unqualified lookup. */
7177 tree
7178 currently_open_derived_class (tree t)
7180 int i;
7182 /* The bases of a dependent type are unknown. */
7183 if (dependent_type_p (t))
7184 return NULL_TREE;
7186 if (!current_class_type)
7187 return NULL_TREE;
7189 if (DERIVED_FROM_P (t, current_class_type))
7190 return current_class_type;
7192 for (i = current_class_depth - 1; i > 0; --i)
7194 if (current_class_stack[i].hidden)
7195 break;
7196 if (DERIVED_FROM_P (t, current_class_stack[i].type))
7197 return current_class_stack[i].type;
7200 return NULL_TREE;
7203 /* Returns the innermost class type which is not a lambda closure type. */
7205 tree
7206 current_nonlambda_class_type (void)
7208 int i;
7210 /* We start looking from 1 because entry 0 is from global scope,
7211 and has no type. */
7212 for (i = current_class_depth; i > 0; --i)
7214 tree c;
7215 if (i == current_class_depth)
7216 c = current_class_type;
7217 else
7219 if (current_class_stack[i].hidden)
7220 break;
7221 c = current_class_stack[i].type;
7223 if (!c)
7224 continue;
7225 if (!LAMBDA_TYPE_P (c))
7226 return c;
7228 return NULL_TREE;
7231 /* When entering a class scope, all enclosing class scopes' names with
7232 static meaning (static variables, static functions, types and
7233 enumerators) have to be visible. This recursive function calls
7234 pushclass for all enclosing class contexts until global or a local
7235 scope is reached. TYPE is the enclosed class. */
7237 void
7238 push_nested_class (tree type)
7240 /* A namespace might be passed in error cases, like A::B:C. */
7241 if (type == NULL_TREE
7242 || !CLASS_TYPE_P (type))
7243 return;
7245 push_nested_class (DECL_CONTEXT (TYPE_MAIN_DECL (type)));
7247 pushclass (type);
7250 /* Undoes a push_nested_class call. */
7252 void
7253 pop_nested_class (void)
7255 tree context = DECL_CONTEXT (TYPE_MAIN_DECL (current_class_type));
7257 popclass ();
7258 if (context && CLASS_TYPE_P (context))
7259 pop_nested_class ();
7262 /* Returns the number of extern "LANG" blocks we are nested within. */
7265 current_lang_depth (void)
7267 return vec_safe_length (current_lang_base);
7270 /* Set global variables CURRENT_LANG_NAME to appropriate value
7271 so that behavior of name-mangling machinery is correct. */
7273 void
7274 push_lang_context (tree name)
7276 vec_safe_push (current_lang_base, current_lang_name);
7278 if (name == lang_name_cplusplus)
7280 current_lang_name = name;
7282 else if (name == lang_name_java)
7284 current_lang_name = name;
7285 /* DECL_IGNORED_P is initially set for these types, to avoid clutter.
7286 (See record_builtin_java_type in decl.c.) However, that causes
7287 incorrect debug entries if these types are actually used.
7288 So we re-enable debug output after extern "Java". */
7289 DECL_IGNORED_P (TYPE_NAME (java_byte_type_node)) = 0;
7290 DECL_IGNORED_P (TYPE_NAME (java_short_type_node)) = 0;
7291 DECL_IGNORED_P (TYPE_NAME (java_int_type_node)) = 0;
7292 DECL_IGNORED_P (TYPE_NAME (java_long_type_node)) = 0;
7293 DECL_IGNORED_P (TYPE_NAME (java_float_type_node)) = 0;
7294 DECL_IGNORED_P (TYPE_NAME (java_double_type_node)) = 0;
7295 DECL_IGNORED_P (TYPE_NAME (java_char_type_node)) = 0;
7296 DECL_IGNORED_P (TYPE_NAME (java_boolean_type_node)) = 0;
7298 else if (name == lang_name_c)
7300 current_lang_name = name;
7302 else
7303 error ("language string %<\"%E\"%> not recognized", name);
7306 /* Get out of the current language scope. */
7308 void
7309 pop_lang_context (void)
7311 current_lang_name = current_lang_base->pop ();
7314 /* Type instantiation routines. */
7316 /* Given an OVERLOAD and a TARGET_TYPE, return the function that
7317 matches the TARGET_TYPE. If there is no satisfactory match, return
7318 error_mark_node, and issue an error & warning messages under
7319 control of FLAGS. Permit pointers to member function if FLAGS
7320 permits. If TEMPLATE_ONLY, the name of the overloaded function was
7321 a template-id, and EXPLICIT_TARGS are the explicitly provided
7322 template arguments.
7324 If OVERLOAD is for one or more member functions, then ACCESS_PATH
7325 is the base path used to reference those member functions. If
7326 the address is resolved to a member function, access checks will be
7327 performed and errors issued if appropriate. */
7329 static tree
7330 resolve_address_of_overloaded_function (tree target_type,
7331 tree overload,
7332 tsubst_flags_t flags,
7333 bool template_only,
7334 tree explicit_targs,
7335 tree access_path)
7337 /* Here's what the standard says:
7339 [over.over]
7341 If the name is a function template, template argument deduction
7342 is done, and if the argument deduction succeeds, the deduced
7343 arguments are used to generate a single template function, which
7344 is added to the set of overloaded functions considered.
7346 Non-member functions and static member functions match targets of
7347 type "pointer-to-function" or "reference-to-function." Nonstatic
7348 member functions match targets of type "pointer-to-member
7349 function;" the function type of the pointer to member is used to
7350 select the member function from the set of overloaded member
7351 functions. If a nonstatic member function is selected, the
7352 reference to the overloaded function name is required to have the
7353 form of a pointer to member as described in 5.3.1.
7355 If more than one function is selected, any template functions in
7356 the set are eliminated if the set also contains a non-template
7357 function, and any given template function is eliminated if the
7358 set contains a second template function that is more specialized
7359 than the first according to the partial ordering rules 14.5.5.2.
7360 After such eliminations, if any, there shall remain exactly one
7361 selected function. */
7363 int is_ptrmem = 0;
7364 /* We store the matches in a TREE_LIST rooted here. The functions
7365 are the TREE_PURPOSE, not the TREE_VALUE, in this list, for easy
7366 interoperability with most_specialized_instantiation. */
7367 tree matches = NULL_TREE;
7368 tree fn;
7369 tree target_fn_type;
7371 /* By the time we get here, we should be seeing only real
7372 pointer-to-member types, not the internal POINTER_TYPE to
7373 METHOD_TYPE representation. */
7374 gcc_assert (!TYPE_PTR_P (target_type)
7375 || TREE_CODE (TREE_TYPE (target_type)) != METHOD_TYPE);
7377 gcc_assert (is_overloaded_fn (overload));
7379 /* Check that the TARGET_TYPE is reasonable. */
7380 if (TYPE_PTRFN_P (target_type)
7381 || TYPE_REFFN_P (target_type))
7382 /* This is OK. */;
7383 else if (TYPE_PTRMEMFUNC_P (target_type))
7384 /* This is OK, too. */
7385 is_ptrmem = 1;
7386 else if (TREE_CODE (target_type) == FUNCTION_TYPE)
7387 /* This is OK, too. This comes from a conversion to reference
7388 type. */
7389 target_type = build_reference_type (target_type);
7390 else
7392 if (flags & tf_error)
7393 error ("cannot resolve overloaded function %qD based on"
7394 " conversion to type %qT",
7395 DECL_NAME (OVL_FUNCTION (overload)), target_type);
7396 return error_mark_node;
7399 /* Non-member functions and static member functions match targets of type
7400 "pointer-to-function" or "reference-to-function." Nonstatic member
7401 functions match targets of type "pointer-to-member-function;" the
7402 function type of the pointer to member is used to select the member
7403 function from the set of overloaded member functions.
7405 So figure out the FUNCTION_TYPE that we want to match against. */
7406 target_fn_type = static_fn_type (target_type);
7408 /* If we can find a non-template function that matches, we can just
7409 use it. There's no point in generating template instantiations
7410 if we're just going to throw them out anyhow. But, of course, we
7411 can only do this when we don't *need* a template function. */
7412 if (!template_only)
7414 tree fns;
7416 for (fns = overload; fns; fns = OVL_NEXT (fns))
7418 tree fn = OVL_CURRENT (fns);
7420 if (TREE_CODE (fn) == TEMPLATE_DECL)
7421 /* We're not looking for templates just yet. */
7422 continue;
7424 if ((TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
7425 != is_ptrmem)
7426 /* We're looking for a non-static member, and this isn't
7427 one, or vice versa. */
7428 continue;
7430 /* Ignore functions which haven't been explicitly
7431 declared. */
7432 if (DECL_ANTICIPATED (fn))
7433 continue;
7435 /* See if there's a match. */
7436 if (same_type_p (target_fn_type, static_fn_type (fn)))
7437 matches = tree_cons (fn, NULL_TREE, matches);
7441 /* Now, if we've already got a match (or matches), there's no need
7442 to proceed to the template functions. But, if we don't have a
7443 match we need to look at them, too. */
7444 if (!matches)
7446 tree target_arg_types;
7447 tree target_ret_type;
7448 tree fns;
7449 tree *args;
7450 unsigned int nargs, ia;
7451 tree arg;
7453 target_arg_types = TYPE_ARG_TYPES (target_fn_type);
7454 target_ret_type = TREE_TYPE (target_fn_type);
7456 nargs = list_length (target_arg_types);
7457 args = XALLOCAVEC (tree, nargs);
7458 for (arg = target_arg_types, ia = 0;
7459 arg != NULL_TREE && arg != void_list_node;
7460 arg = TREE_CHAIN (arg), ++ia)
7461 args[ia] = TREE_VALUE (arg);
7462 nargs = ia;
7464 for (fns = overload; fns; fns = OVL_NEXT (fns))
7466 tree fn = OVL_CURRENT (fns);
7467 tree instantiation;
7468 tree targs;
7470 if (TREE_CODE (fn) != TEMPLATE_DECL)
7471 /* We're only looking for templates. */
7472 continue;
7474 if ((TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
7475 != is_ptrmem)
7476 /* We're not looking for a non-static member, and this is
7477 one, or vice versa. */
7478 continue;
7480 tree ret = target_ret_type;
7482 /* If the template has a deduced return type, don't expose it to
7483 template argument deduction. */
7484 if (undeduced_auto_decl (fn))
7485 ret = NULL_TREE;
7487 /* Try to do argument deduction. */
7488 targs = make_tree_vec (DECL_NTPARMS (fn));
7489 instantiation = fn_type_unification (fn, explicit_targs, targs, args,
7490 nargs, ret,
7491 DEDUCE_EXACT, LOOKUP_NORMAL,
7492 false, false);
7493 if (instantiation == error_mark_node)
7494 /* Instantiation failed. */
7495 continue;
7497 /* And now force instantiation to do return type deduction. */
7498 if (undeduced_auto_decl (instantiation))
7500 ++function_depth;
7501 instantiate_decl (instantiation, /*defer*/false, /*class*/false);
7502 --function_depth;
7504 require_deduced_type (instantiation);
7507 /* See if there's a match. */
7508 if (same_type_p (target_fn_type, static_fn_type (instantiation)))
7509 matches = tree_cons (instantiation, fn, matches);
7511 ggc_free (targs);
7514 /* Now, remove all but the most specialized of the matches. */
7515 if (matches)
7517 tree match = most_specialized_instantiation (matches);
7519 if (match != error_mark_node)
7520 matches = tree_cons (TREE_PURPOSE (match),
7521 NULL_TREE,
7522 NULL_TREE);
7526 /* Now we should have exactly one function in MATCHES. */
7527 if (matches == NULL_TREE)
7529 /* There were *no* matches. */
7530 if (flags & tf_error)
7532 error ("no matches converting function %qD to type %q#T",
7533 DECL_NAME (OVL_CURRENT (overload)),
7534 target_type);
7536 print_candidates (overload);
7538 return error_mark_node;
7540 else if (TREE_CHAIN (matches))
7542 /* There were too many matches. First check if they're all
7543 the same function. */
7544 tree match = NULL_TREE;
7546 fn = TREE_PURPOSE (matches);
7548 /* For multi-versioned functions, more than one match is just fine and
7549 decls_match will return false as they are different. */
7550 for (match = TREE_CHAIN (matches); match; match = TREE_CHAIN (match))
7551 if (!decls_match (fn, TREE_PURPOSE (match))
7552 && !targetm.target_option.function_versions
7553 (fn, TREE_PURPOSE (match)))
7554 break;
7556 if (match)
7558 if (flags & tf_error)
7560 error ("converting overloaded function %qD to type %q#T is ambiguous",
7561 DECL_NAME (OVL_FUNCTION (overload)),
7562 target_type);
7564 /* Since print_candidates expects the functions in the
7565 TREE_VALUE slot, we flip them here. */
7566 for (match = matches; match; match = TREE_CHAIN (match))
7567 TREE_VALUE (match) = TREE_PURPOSE (match);
7569 print_candidates (matches);
7572 return error_mark_node;
7576 /* Good, exactly one match. Now, convert it to the correct type. */
7577 fn = TREE_PURPOSE (matches);
7579 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
7580 && !(flags & tf_ptrmem_ok) && !flag_ms_extensions)
7582 static int explained;
7584 if (!(flags & tf_error))
7585 return error_mark_node;
7587 permerror (input_location, "assuming pointer to member %qD", fn);
7588 if (!explained)
7590 inform (input_location, "(a pointer to member can only be formed with %<&%E%>)", fn);
7591 explained = 1;
7595 /* If a pointer to a function that is multi-versioned is requested, the
7596 pointer to the dispatcher function is returned instead. This works
7597 well because indirectly calling the function will dispatch the right
7598 function version at run-time. */
7599 if (DECL_FUNCTION_VERSIONED (fn))
7601 fn = get_function_version_dispatcher (fn);
7602 if (fn == NULL)
7603 return error_mark_node;
7604 /* Mark all the versions corresponding to the dispatcher as used. */
7605 if (!(flags & tf_conv))
7606 mark_versions_used (fn);
7609 /* If we're doing overload resolution purely for the purpose of
7610 determining conversion sequences, we should not consider the
7611 function used. If this conversion sequence is selected, the
7612 function will be marked as used at this point. */
7613 if (!(flags & tf_conv))
7615 /* Make =delete work with SFINAE. */
7616 if (DECL_DELETED_FN (fn) && !(flags & tf_error))
7617 return error_mark_node;
7619 mark_used (fn);
7622 /* We could not check access to member functions when this
7623 expression was originally created since we did not know at that
7624 time to which function the expression referred. */
7625 if (DECL_FUNCTION_MEMBER_P (fn))
7627 gcc_assert (access_path);
7628 perform_or_defer_access_check (access_path, fn, fn, flags);
7631 if (TYPE_PTRFN_P (target_type) || TYPE_PTRMEMFUNC_P (target_type))
7632 return cp_build_addr_expr (fn, flags);
7633 else
7635 /* The target must be a REFERENCE_TYPE. Above, cp_build_unary_op
7636 will mark the function as addressed, but here we must do it
7637 explicitly. */
7638 cxx_mark_addressable (fn);
7640 return fn;
7644 /* This function will instantiate the type of the expression given in
7645 RHS to match the type of LHSTYPE. If errors exist, then return
7646 error_mark_node. FLAGS is a bit mask. If TF_ERROR is set, then
7647 we complain on errors. If we are not complaining, never modify rhs,
7648 as overload resolution wants to try many possible instantiations, in
7649 the hope that at least one will work.
7651 For non-recursive calls, LHSTYPE should be a function, pointer to
7652 function, or a pointer to member function. */
7654 tree
7655 instantiate_type (tree lhstype, tree rhs, tsubst_flags_t flags)
7657 tsubst_flags_t flags_in = flags;
7658 tree access_path = NULL_TREE;
7660 flags &= ~tf_ptrmem_ok;
7662 if (lhstype == unknown_type_node)
7664 if (flags & tf_error)
7665 error ("not enough type information");
7666 return error_mark_node;
7669 if (TREE_TYPE (rhs) != NULL_TREE && ! (type_unknown_p (rhs)))
7671 tree fntype = non_reference (lhstype);
7672 if (same_type_p (fntype, TREE_TYPE (rhs)))
7673 return rhs;
7674 if (flag_ms_extensions
7675 && TYPE_PTRMEMFUNC_P (fntype)
7676 && !TYPE_PTRMEMFUNC_P (TREE_TYPE (rhs)))
7677 /* Microsoft allows `A::f' to be resolved to a
7678 pointer-to-member. */
7680 else
7682 if (flags & tf_error)
7683 error ("cannot convert %qE from type %qT to type %qT",
7684 rhs, TREE_TYPE (rhs), fntype);
7685 return error_mark_node;
7689 if (BASELINK_P (rhs))
7691 access_path = BASELINK_ACCESS_BINFO (rhs);
7692 rhs = BASELINK_FUNCTIONS (rhs);
7695 /* If we are in a template, and have a NON_DEPENDENT_EXPR, we cannot
7696 deduce any type information. */
7697 if (TREE_CODE (rhs) == NON_DEPENDENT_EXPR)
7699 if (flags & tf_error)
7700 error ("not enough type information");
7701 return error_mark_node;
7704 /* There only a few kinds of expressions that may have a type
7705 dependent on overload resolution. */
7706 gcc_assert (TREE_CODE (rhs) == ADDR_EXPR
7707 || TREE_CODE (rhs) == COMPONENT_REF
7708 || is_overloaded_fn (rhs)
7709 || (flag_ms_extensions && TREE_CODE (rhs) == FUNCTION_DECL));
7711 /* This should really only be used when attempting to distinguish
7712 what sort of a pointer to function we have. For now, any
7713 arithmetic operation which is not supported on pointers
7714 is rejected as an error. */
7716 switch (TREE_CODE (rhs))
7718 case COMPONENT_REF:
7720 tree member = TREE_OPERAND (rhs, 1);
7722 member = instantiate_type (lhstype, member, flags);
7723 if (member != error_mark_node
7724 && TREE_SIDE_EFFECTS (TREE_OPERAND (rhs, 0)))
7725 /* Do not lose object's side effects. */
7726 return build2 (COMPOUND_EXPR, TREE_TYPE (member),
7727 TREE_OPERAND (rhs, 0), member);
7728 return member;
7731 case OFFSET_REF:
7732 rhs = TREE_OPERAND (rhs, 1);
7733 if (BASELINK_P (rhs))
7734 return instantiate_type (lhstype, rhs, flags_in);
7736 /* This can happen if we are forming a pointer-to-member for a
7737 member template. */
7738 gcc_assert (TREE_CODE (rhs) == TEMPLATE_ID_EXPR);
7740 /* Fall through. */
7742 case TEMPLATE_ID_EXPR:
7744 tree fns = TREE_OPERAND (rhs, 0);
7745 tree args = TREE_OPERAND (rhs, 1);
7747 return
7748 resolve_address_of_overloaded_function (lhstype, fns, flags_in,
7749 /*template_only=*/true,
7750 args, access_path);
7753 case OVERLOAD:
7754 case FUNCTION_DECL:
7755 return
7756 resolve_address_of_overloaded_function (lhstype, rhs, flags_in,
7757 /*template_only=*/false,
7758 /*explicit_targs=*/NULL_TREE,
7759 access_path);
7761 case ADDR_EXPR:
7763 if (PTRMEM_OK_P (rhs))
7764 flags |= tf_ptrmem_ok;
7766 return instantiate_type (lhstype, TREE_OPERAND (rhs, 0), flags);
7769 case ERROR_MARK:
7770 return error_mark_node;
7772 default:
7773 gcc_unreachable ();
7775 return error_mark_node;
7778 /* Return the name of the virtual function pointer field
7779 (as an IDENTIFIER_NODE) for the given TYPE. Note that
7780 this may have to look back through base types to find the
7781 ultimate field name. (For single inheritance, these could
7782 all be the same name. Who knows for multiple inheritance). */
7784 static tree
7785 get_vfield_name (tree type)
7787 tree binfo, base_binfo;
7788 char *buf;
7790 for (binfo = TYPE_BINFO (type);
7791 BINFO_N_BASE_BINFOS (binfo);
7792 binfo = base_binfo)
7794 base_binfo = BINFO_BASE_BINFO (binfo, 0);
7796 if (BINFO_VIRTUAL_P (base_binfo)
7797 || !TYPE_CONTAINS_VPTR_P (BINFO_TYPE (base_binfo)))
7798 break;
7801 type = BINFO_TYPE (binfo);
7802 buf = (char *) alloca (sizeof (VFIELD_NAME_FORMAT)
7803 + TYPE_NAME_LENGTH (type) + 2);
7804 sprintf (buf, VFIELD_NAME_FORMAT,
7805 IDENTIFIER_POINTER (constructor_name (type)));
7806 return get_identifier (buf);
7809 void
7810 print_class_statistics (void)
7812 if (! GATHER_STATISTICS)
7813 return;
7815 fprintf (stderr, "convert_harshness = %d\n", n_convert_harshness);
7816 fprintf (stderr, "compute_conversion_costs = %d\n", n_compute_conversion_costs);
7817 if (n_vtables)
7819 fprintf (stderr, "vtables = %d; vtable searches = %d\n",
7820 n_vtables, n_vtable_searches);
7821 fprintf (stderr, "vtable entries = %d; vtable elems = %d\n",
7822 n_vtable_entries, n_vtable_elems);
7826 /* Build a dummy reference to ourselves so Derived::Base (and A::A) works,
7827 according to [class]:
7828 The class-name is also inserted
7829 into the scope of the class itself. For purposes of access checking,
7830 the inserted class name is treated as if it were a public member name. */
7832 void
7833 build_self_reference (void)
7835 tree name = constructor_name (current_class_type);
7836 tree value = build_lang_decl (TYPE_DECL, name, current_class_type);
7837 tree saved_cas;
7839 DECL_NONLOCAL (value) = 1;
7840 DECL_CONTEXT (value) = current_class_type;
7841 DECL_ARTIFICIAL (value) = 1;
7842 SET_DECL_SELF_REFERENCE_P (value);
7843 set_underlying_type (value);
7845 if (processing_template_decl)
7846 value = push_template_decl (value);
7848 saved_cas = current_access_specifier;
7849 current_access_specifier = access_public_node;
7850 finish_member_declaration (value);
7851 current_access_specifier = saved_cas;
7854 /* Returns 1 if TYPE contains only padding bytes. */
7857 is_empty_class (tree type)
7859 if (type == error_mark_node)
7860 return 0;
7862 if (! CLASS_TYPE_P (type))
7863 return 0;
7865 /* In G++ 3.2, whether or not a class was empty was determined by
7866 looking at its size. */
7867 if (abi_version_at_least (2))
7868 return CLASSTYPE_EMPTY_P (type);
7869 else
7870 return integer_zerop (CLASSTYPE_SIZE (type));
7873 /* Returns true if TYPE contains an empty class. */
7875 static bool
7876 contains_empty_class_p (tree type)
7878 if (is_empty_class (type))
7879 return true;
7880 if (CLASS_TYPE_P (type))
7882 tree field;
7883 tree binfo;
7884 tree base_binfo;
7885 int i;
7887 for (binfo = TYPE_BINFO (type), i = 0;
7888 BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
7889 if (contains_empty_class_p (BINFO_TYPE (base_binfo)))
7890 return true;
7891 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
7892 if (TREE_CODE (field) == FIELD_DECL
7893 && !DECL_ARTIFICIAL (field)
7894 && is_empty_class (TREE_TYPE (field)))
7895 return true;
7897 else if (TREE_CODE (type) == ARRAY_TYPE)
7898 return contains_empty_class_p (TREE_TYPE (type));
7899 return false;
7902 /* Returns true if TYPE contains no actual data, just various
7903 possible combinations of empty classes and possibly a vptr. */
7905 bool
7906 is_really_empty_class (tree type)
7908 if (CLASS_TYPE_P (type))
7910 tree field;
7911 tree binfo;
7912 tree base_binfo;
7913 int i;
7915 /* CLASSTYPE_EMPTY_P isn't set properly until the class is actually laid
7916 out, but we'd like to be able to check this before then. */
7917 if (COMPLETE_TYPE_P (type) && is_empty_class (type))
7918 return true;
7920 for (binfo = TYPE_BINFO (type), i = 0;
7921 BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
7922 if (!is_really_empty_class (BINFO_TYPE (base_binfo)))
7923 return false;
7924 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
7925 if (TREE_CODE (field) == FIELD_DECL
7926 && !DECL_ARTIFICIAL (field)
7927 && !is_really_empty_class (TREE_TYPE (field)))
7928 return false;
7929 return true;
7931 else if (TREE_CODE (type) == ARRAY_TYPE)
7932 return is_really_empty_class (TREE_TYPE (type));
7933 return false;
7936 /* Note that NAME was looked up while the current class was being
7937 defined and that the result of that lookup was DECL. */
7939 void
7940 maybe_note_name_used_in_class (tree name, tree decl)
7942 splay_tree names_used;
7944 /* If we're not defining a class, there's nothing to do. */
7945 if (!(innermost_scope_kind() == sk_class
7946 && TYPE_BEING_DEFINED (current_class_type)
7947 && !LAMBDA_TYPE_P (current_class_type)))
7948 return;
7950 /* If there's already a binding for this NAME, then we don't have
7951 anything to worry about. */
7952 if (lookup_member (current_class_type, name,
7953 /*protect=*/0, /*want_type=*/false, tf_warning_or_error))
7954 return;
7956 if (!current_class_stack[current_class_depth - 1].names_used)
7957 current_class_stack[current_class_depth - 1].names_used
7958 = splay_tree_new (splay_tree_compare_pointers, 0, 0);
7959 names_used = current_class_stack[current_class_depth - 1].names_used;
7961 splay_tree_insert (names_used,
7962 (splay_tree_key) name,
7963 (splay_tree_value) decl);
7966 /* Note that NAME was declared (as DECL) in the current class. Check
7967 to see that the declaration is valid. */
7969 void
7970 note_name_declared_in_class (tree name, tree decl)
7972 splay_tree names_used;
7973 splay_tree_node n;
7975 /* Look to see if we ever used this name. */
7976 names_used
7977 = current_class_stack[current_class_depth - 1].names_used;
7978 if (!names_used)
7979 return;
7980 /* The C language allows members to be declared with a type of the same
7981 name, and the C++ standard says this diagnostic is not required. So
7982 allow it in extern "C" blocks unless predantic is specified.
7983 Allow it in all cases if -ms-extensions is specified. */
7984 if ((!pedantic && current_lang_name == lang_name_c)
7985 || flag_ms_extensions)
7986 return;
7987 n = splay_tree_lookup (names_used, (splay_tree_key) name);
7988 if (n)
7990 /* [basic.scope.class]
7992 A name N used in a class S shall refer to the same declaration
7993 in its context and when re-evaluated in the completed scope of
7994 S. */
7995 permerror (input_location, "declaration of %q#D", decl);
7996 permerror (input_location, "changes meaning of %qD from %q+#D",
7997 DECL_NAME (OVL_CURRENT (decl)), (tree) n->value);
8001 /* Returns the VAR_DECL for the complete vtable associated with BINFO.
8002 Secondary vtables are merged with primary vtables; this function
8003 will return the VAR_DECL for the primary vtable. */
8005 tree
8006 get_vtbl_decl_for_binfo (tree binfo)
8008 tree decl;
8010 decl = BINFO_VTABLE (binfo);
8011 if (decl && TREE_CODE (decl) == POINTER_PLUS_EXPR)
8013 gcc_assert (TREE_CODE (TREE_OPERAND (decl, 0)) == ADDR_EXPR);
8014 decl = TREE_OPERAND (TREE_OPERAND (decl, 0), 0);
8016 if (decl)
8017 gcc_assert (VAR_P (decl));
8018 return decl;
8022 /* Returns the binfo for the primary base of BINFO. If the resulting
8023 BINFO is a virtual base, and it is inherited elsewhere in the
8024 hierarchy, then the returned binfo might not be the primary base of
8025 BINFO in the complete object. Check BINFO_PRIMARY_P or
8026 BINFO_LOST_PRIMARY_P to be sure. */
8028 static tree
8029 get_primary_binfo (tree binfo)
8031 tree primary_base;
8033 primary_base = CLASSTYPE_PRIMARY_BINFO (BINFO_TYPE (binfo));
8034 if (!primary_base)
8035 return NULL_TREE;
8037 return copied_binfo (primary_base, binfo);
8040 /* If INDENTED_P is zero, indent to INDENT. Return nonzero. */
8042 static int
8043 maybe_indent_hierarchy (FILE * stream, int indent, int indented_p)
8045 if (!indented_p)
8046 fprintf (stream, "%*s", indent, "");
8047 return 1;
8050 /* Dump the offsets of all the bases rooted at BINFO to STREAM.
8051 INDENT should be zero when called from the top level; it is
8052 incremented recursively. IGO indicates the next expected BINFO in
8053 inheritance graph ordering. */
8055 static tree
8056 dump_class_hierarchy_r (FILE *stream,
8057 int flags,
8058 tree binfo,
8059 tree igo,
8060 int indent)
8062 int indented = 0;
8063 tree base_binfo;
8064 int i;
8066 indented = maybe_indent_hierarchy (stream, indent, 0);
8067 fprintf (stream, "%s (0x" HOST_WIDE_INT_PRINT_HEX ") ",
8068 type_as_string (BINFO_TYPE (binfo), TFF_PLAIN_IDENTIFIER),
8069 (HOST_WIDE_INT) (uintptr_t) binfo);
8070 if (binfo != igo)
8072 fprintf (stream, "alternative-path\n");
8073 return igo;
8075 igo = TREE_CHAIN (binfo);
8077 fprintf (stream, HOST_WIDE_INT_PRINT_DEC,
8078 tree_low_cst (BINFO_OFFSET (binfo), 0));
8079 if (is_empty_class (BINFO_TYPE (binfo)))
8080 fprintf (stream, " empty");
8081 else if (CLASSTYPE_NEARLY_EMPTY_P (BINFO_TYPE (binfo)))
8082 fprintf (stream, " nearly-empty");
8083 if (BINFO_VIRTUAL_P (binfo))
8084 fprintf (stream, " virtual");
8085 fprintf (stream, "\n");
8087 indented = 0;
8088 if (BINFO_PRIMARY_P (binfo))
8090 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
8091 fprintf (stream, " primary-for %s (0x" HOST_WIDE_INT_PRINT_HEX ")",
8092 type_as_string (BINFO_TYPE (BINFO_INHERITANCE_CHAIN (binfo)),
8093 TFF_PLAIN_IDENTIFIER),
8094 (HOST_WIDE_INT) (uintptr_t) BINFO_INHERITANCE_CHAIN (binfo));
8096 if (BINFO_LOST_PRIMARY_P (binfo))
8098 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
8099 fprintf (stream, " lost-primary");
8101 if (indented)
8102 fprintf (stream, "\n");
8104 if (!(flags & TDF_SLIM))
8106 int indented = 0;
8108 if (BINFO_SUBVTT_INDEX (binfo))
8110 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
8111 fprintf (stream, " subvttidx=%s",
8112 expr_as_string (BINFO_SUBVTT_INDEX (binfo),
8113 TFF_PLAIN_IDENTIFIER));
8115 if (BINFO_VPTR_INDEX (binfo))
8117 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
8118 fprintf (stream, " vptridx=%s",
8119 expr_as_string (BINFO_VPTR_INDEX (binfo),
8120 TFF_PLAIN_IDENTIFIER));
8122 if (BINFO_VPTR_FIELD (binfo))
8124 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
8125 fprintf (stream, " vbaseoffset=%s",
8126 expr_as_string (BINFO_VPTR_FIELD (binfo),
8127 TFF_PLAIN_IDENTIFIER));
8129 if (BINFO_VTABLE (binfo))
8131 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
8132 fprintf (stream, " vptr=%s",
8133 expr_as_string (BINFO_VTABLE (binfo),
8134 TFF_PLAIN_IDENTIFIER));
8137 if (indented)
8138 fprintf (stream, "\n");
8141 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
8142 igo = dump_class_hierarchy_r (stream, flags, base_binfo, igo, indent + 2);
8144 return igo;
8147 /* Dump the BINFO hierarchy for T. */
8149 static void
8150 dump_class_hierarchy_1 (FILE *stream, int flags, tree t)
8152 fprintf (stream, "Class %s\n", type_as_string (t, TFF_PLAIN_IDENTIFIER));
8153 fprintf (stream, " size=%lu align=%lu\n",
8154 (unsigned long)(tree_low_cst (TYPE_SIZE (t), 0) / BITS_PER_UNIT),
8155 (unsigned long)(TYPE_ALIGN (t) / BITS_PER_UNIT));
8156 fprintf (stream, " base size=%lu base align=%lu\n",
8157 (unsigned long)(tree_low_cst (TYPE_SIZE (CLASSTYPE_AS_BASE (t)), 0)
8158 / BITS_PER_UNIT),
8159 (unsigned long)(TYPE_ALIGN (CLASSTYPE_AS_BASE (t))
8160 / BITS_PER_UNIT));
8161 dump_class_hierarchy_r (stream, flags, TYPE_BINFO (t), TYPE_BINFO (t), 0);
8162 fprintf (stream, "\n");
8165 /* Debug interface to hierarchy dumping. */
8167 void
8168 debug_class (tree t)
8170 dump_class_hierarchy_1 (stderr, TDF_SLIM, t);
8173 static void
8174 dump_class_hierarchy (tree t)
8176 int flags;
8177 FILE *stream = dump_begin (TDI_class, &flags);
8179 if (stream)
8181 dump_class_hierarchy_1 (stream, flags, t);
8182 dump_end (TDI_class, stream);
8186 static void
8187 dump_array (FILE * stream, tree decl)
8189 tree value;
8190 unsigned HOST_WIDE_INT ix;
8191 HOST_WIDE_INT elt;
8192 tree size = TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (decl)));
8194 elt = (tree_low_cst (TYPE_SIZE (TREE_TYPE (TREE_TYPE (decl))), 0)
8195 / BITS_PER_UNIT);
8196 fprintf (stream, "%s:", decl_as_string (decl, TFF_PLAIN_IDENTIFIER));
8197 fprintf (stream, " %s entries",
8198 expr_as_string (size_binop (PLUS_EXPR, size, size_one_node),
8199 TFF_PLAIN_IDENTIFIER));
8200 fprintf (stream, "\n");
8202 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (DECL_INITIAL (decl)),
8203 ix, value)
8204 fprintf (stream, "%-4ld %s\n", (long)(ix * elt),
8205 expr_as_string (value, TFF_PLAIN_IDENTIFIER));
8208 static void
8209 dump_vtable (tree t, tree binfo, tree vtable)
8211 int flags;
8212 FILE *stream = dump_begin (TDI_class, &flags);
8214 if (!stream)
8215 return;
8217 if (!(flags & TDF_SLIM))
8219 int ctor_vtbl_p = TYPE_BINFO (t) != binfo;
8221 fprintf (stream, "%s for %s",
8222 ctor_vtbl_p ? "Construction vtable" : "Vtable",
8223 type_as_string (BINFO_TYPE (binfo), TFF_PLAIN_IDENTIFIER));
8224 if (ctor_vtbl_p)
8226 if (!BINFO_VIRTUAL_P (binfo))
8227 fprintf (stream, " (0x" HOST_WIDE_INT_PRINT_HEX " instance)",
8228 (HOST_WIDE_INT) (uintptr_t) binfo);
8229 fprintf (stream, " in %s", type_as_string (t, TFF_PLAIN_IDENTIFIER));
8231 fprintf (stream, "\n");
8232 dump_array (stream, vtable);
8233 fprintf (stream, "\n");
8236 dump_end (TDI_class, stream);
8239 static void
8240 dump_vtt (tree t, tree vtt)
8242 int flags;
8243 FILE *stream = dump_begin (TDI_class, &flags);
8245 if (!stream)
8246 return;
8248 if (!(flags & TDF_SLIM))
8250 fprintf (stream, "VTT for %s\n",
8251 type_as_string (t, TFF_PLAIN_IDENTIFIER));
8252 dump_array (stream, vtt);
8253 fprintf (stream, "\n");
8256 dump_end (TDI_class, stream);
8259 /* Dump a function or thunk and its thunkees. */
8261 static void
8262 dump_thunk (FILE *stream, int indent, tree thunk)
8264 static const char spaces[] = " ";
8265 tree name = DECL_NAME (thunk);
8266 tree thunks;
8268 fprintf (stream, "%.*s%p %s %s", indent, spaces,
8269 (void *)thunk,
8270 !DECL_THUNK_P (thunk) ? "function"
8271 : DECL_THIS_THUNK_P (thunk) ? "this-thunk" : "covariant-thunk",
8272 name ? IDENTIFIER_POINTER (name) : "<unset>");
8273 if (DECL_THUNK_P (thunk))
8275 HOST_WIDE_INT fixed_adjust = THUNK_FIXED_OFFSET (thunk);
8276 tree virtual_adjust = THUNK_VIRTUAL_OFFSET (thunk);
8278 fprintf (stream, " fixed=" HOST_WIDE_INT_PRINT_DEC, fixed_adjust);
8279 if (!virtual_adjust)
8280 /*NOP*/;
8281 else if (DECL_THIS_THUNK_P (thunk))
8282 fprintf (stream, " vcall=" HOST_WIDE_INT_PRINT_DEC,
8283 tree_low_cst (virtual_adjust, 0));
8284 else
8285 fprintf (stream, " vbase=" HOST_WIDE_INT_PRINT_DEC "(%s)",
8286 tree_low_cst (BINFO_VPTR_FIELD (virtual_adjust), 0),
8287 type_as_string (BINFO_TYPE (virtual_adjust), TFF_SCOPE));
8288 if (THUNK_ALIAS (thunk))
8289 fprintf (stream, " alias to %p", (void *)THUNK_ALIAS (thunk));
8291 fprintf (stream, "\n");
8292 for (thunks = DECL_THUNKS (thunk); thunks; thunks = TREE_CHAIN (thunks))
8293 dump_thunk (stream, indent + 2, thunks);
8296 /* Dump the thunks for FN. */
8298 void
8299 debug_thunks (tree fn)
8301 dump_thunk (stderr, 0, fn);
8304 /* Virtual function table initialization. */
8306 /* Create all the necessary vtables for T and its base classes. */
8308 static void
8309 finish_vtbls (tree t)
8311 tree vbase;
8312 vec<constructor_elt, va_gc> *v = NULL;
8313 tree vtable = BINFO_VTABLE (TYPE_BINFO (t));
8315 /* We lay out the primary and secondary vtables in one contiguous
8316 vtable. The primary vtable is first, followed by the non-virtual
8317 secondary vtables in inheritance graph order. */
8318 accumulate_vtbl_inits (TYPE_BINFO (t), TYPE_BINFO (t), TYPE_BINFO (t),
8319 vtable, t, &v);
8321 /* Then come the virtual bases, also in inheritance graph order. */
8322 for (vbase = TYPE_BINFO (t); vbase; vbase = TREE_CHAIN (vbase))
8324 if (!BINFO_VIRTUAL_P (vbase))
8325 continue;
8326 accumulate_vtbl_inits (vbase, vbase, TYPE_BINFO (t), vtable, t, &v);
8329 if (BINFO_VTABLE (TYPE_BINFO (t)))
8330 initialize_vtable (TYPE_BINFO (t), v);
8333 /* Initialize the vtable for BINFO with the INITS. */
8335 static void
8336 initialize_vtable (tree binfo, vec<constructor_elt, va_gc> *inits)
8338 tree decl;
8340 layout_vtable_decl (binfo, vec_safe_length (inits));
8341 decl = get_vtbl_decl_for_binfo (binfo);
8342 initialize_artificial_var (decl, inits);
8343 dump_vtable (BINFO_TYPE (binfo), binfo, decl);
8346 /* Build the VTT (virtual table table) for T.
8347 A class requires a VTT if it has virtual bases.
8349 This holds
8350 1 - primary virtual pointer for complete object T
8351 2 - secondary VTTs for each direct non-virtual base of T which requires a
8353 3 - secondary virtual pointers for each direct or indirect base of T which
8354 has virtual bases or is reachable via a virtual path from T.
8355 4 - secondary VTTs for each direct or indirect virtual base of T.
8357 Secondary VTTs look like complete object VTTs without part 4. */
8359 static void
8360 build_vtt (tree t)
8362 tree type;
8363 tree vtt;
8364 tree index;
8365 vec<constructor_elt, va_gc> *inits;
8367 /* Build up the initializers for the VTT. */
8368 inits = NULL;
8369 index = size_zero_node;
8370 build_vtt_inits (TYPE_BINFO (t), t, &inits, &index);
8372 /* If we didn't need a VTT, we're done. */
8373 if (!inits)
8374 return;
8376 /* Figure out the type of the VTT. */
8377 type = build_array_of_n_type (const_ptr_type_node,
8378 inits->length ());
8380 /* Now, build the VTT object itself. */
8381 vtt = build_vtable (t, mangle_vtt_for_type (t), type);
8382 initialize_artificial_var (vtt, inits);
8383 /* Add the VTT to the vtables list. */
8384 DECL_CHAIN (vtt) = DECL_CHAIN (CLASSTYPE_VTABLES (t));
8385 DECL_CHAIN (CLASSTYPE_VTABLES (t)) = vtt;
8387 dump_vtt (t, vtt);
8390 /* When building a secondary VTT, BINFO_VTABLE is set to a TREE_LIST with
8391 PURPOSE the RTTI_BINFO, VALUE the real vtable pointer for this binfo,
8392 and CHAIN the vtable pointer for this binfo after construction is
8393 complete. VALUE can also be another BINFO, in which case we recurse. */
8395 static tree
8396 binfo_ctor_vtable (tree binfo)
8398 tree vt;
8400 while (1)
8402 vt = BINFO_VTABLE (binfo);
8403 if (TREE_CODE (vt) == TREE_LIST)
8404 vt = TREE_VALUE (vt);
8405 if (TREE_CODE (vt) == TREE_BINFO)
8406 binfo = vt;
8407 else
8408 break;
8411 return vt;
8414 /* Data for secondary VTT initialization. */
8415 typedef struct secondary_vptr_vtt_init_data_s
8417 /* Is this the primary VTT? */
8418 bool top_level_p;
8420 /* Current index into the VTT. */
8421 tree index;
8423 /* Vector of initializers built up. */
8424 vec<constructor_elt, va_gc> *inits;
8426 /* The type being constructed by this secondary VTT. */
8427 tree type_being_constructed;
8428 } secondary_vptr_vtt_init_data;
8430 /* Recursively build the VTT-initializer for BINFO (which is in the
8431 hierarchy dominated by T). INITS points to the end of the initializer
8432 list to date. INDEX is the VTT index where the next element will be
8433 replaced. Iff BINFO is the binfo for T, this is the top level VTT (i.e.
8434 not a subvtt for some base of T). When that is so, we emit the sub-VTTs
8435 for virtual bases of T. When it is not so, we build the constructor
8436 vtables for the BINFO-in-T variant. */
8438 static void
8439 build_vtt_inits (tree binfo, tree t, vec<constructor_elt, va_gc> **inits,
8440 tree *index)
8442 int i;
8443 tree b;
8444 tree init;
8445 secondary_vptr_vtt_init_data data;
8446 int top_level_p = SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), t);
8448 /* We only need VTTs for subobjects with virtual bases. */
8449 if (!CLASSTYPE_VBASECLASSES (BINFO_TYPE (binfo)))
8450 return;
8452 /* We need to use a construction vtable if this is not the primary
8453 VTT. */
8454 if (!top_level_p)
8456 build_ctor_vtbl_group (binfo, t);
8458 /* Record the offset in the VTT where this sub-VTT can be found. */
8459 BINFO_SUBVTT_INDEX (binfo) = *index;
8462 /* Add the address of the primary vtable for the complete object. */
8463 init = binfo_ctor_vtable (binfo);
8464 CONSTRUCTOR_APPEND_ELT (*inits, NULL_TREE, init);
8465 if (top_level_p)
8467 gcc_assert (!BINFO_VPTR_INDEX (binfo));
8468 BINFO_VPTR_INDEX (binfo) = *index;
8470 *index = size_binop (PLUS_EXPR, *index, TYPE_SIZE_UNIT (ptr_type_node));
8472 /* Recursively add the secondary VTTs for non-virtual bases. */
8473 for (i = 0; BINFO_BASE_ITERATE (binfo, i, b); ++i)
8474 if (!BINFO_VIRTUAL_P (b))
8475 build_vtt_inits (b, t, inits, index);
8477 /* Add secondary virtual pointers for all subobjects of BINFO with
8478 either virtual bases or reachable along a virtual path, except
8479 subobjects that are non-virtual primary bases. */
8480 data.top_level_p = top_level_p;
8481 data.index = *index;
8482 data.inits = *inits;
8483 data.type_being_constructed = BINFO_TYPE (binfo);
8485 dfs_walk_once (binfo, dfs_build_secondary_vptr_vtt_inits, NULL, &data);
8487 *index = data.index;
8489 /* data.inits might have grown as we added secondary virtual pointers.
8490 Make sure our caller knows about the new vector. */
8491 *inits = data.inits;
8493 if (top_level_p)
8494 /* Add the secondary VTTs for virtual bases in inheritance graph
8495 order. */
8496 for (b = TYPE_BINFO (BINFO_TYPE (binfo)); b; b = TREE_CHAIN (b))
8498 if (!BINFO_VIRTUAL_P (b))
8499 continue;
8501 build_vtt_inits (b, t, inits, index);
8503 else
8504 /* Remove the ctor vtables we created. */
8505 dfs_walk_all (binfo, dfs_fixup_binfo_vtbls, NULL, binfo);
8508 /* Called from build_vtt_inits via dfs_walk. BINFO is the binfo for the base
8509 in most derived. DATA is a SECONDARY_VPTR_VTT_INIT_DATA structure. */
8511 static tree
8512 dfs_build_secondary_vptr_vtt_inits (tree binfo, void *data_)
8514 secondary_vptr_vtt_init_data *data = (secondary_vptr_vtt_init_data *)data_;
8516 /* We don't care about bases that don't have vtables. */
8517 if (!TYPE_VFIELD (BINFO_TYPE (binfo)))
8518 return dfs_skip_bases;
8520 /* We're only interested in proper subobjects of the type being
8521 constructed. */
8522 if (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), data->type_being_constructed))
8523 return NULL_TREE;
8525 /* We're only interested in bases with virtual bases or reachable
8526 via a virtual path from the type being constructed. */
8527 if (!(CLASSTYPE_VBASECLASSES (BINFO_TYPE (binfo))
8528 || binfo_via_virtual (binfo, data->type_being_constructed)))
8529 return dfs_skip_bases;
8531 /* We're not interested in non-virtual primary bases. */
8532 if (!BINFO_VIRTUAL_P (binfo) && BINFO_PRIMARY_P (binfo))
8533 return NULL_TREE;
8535 /* Record the index where this secondary vptr can be found. */
8536 if (data->top_level_p)
8538 gcc_assert (!BINFO_VPTR_INDEX (binfo));
8539 BINFO_VPTR_INDEX (binfo) = data->index;
8541 if (BINFO_VIRTUAL_P (binfo))
8543 /* It's a primary virtual base, and this is not a
8544 construction vtable. Find the base this is primary of in
8545 the inheritance graph, and use that base's vtable
8546 now. */
8547 while (BINFO_PRIMARY_P (binfo))
8548 binfo = BINFO_INHERITANCE_CHAIN (binfo);
8552 /* Add the initializer for the secondary vptr itself. */
8553 CONSTRUCTOR_APPEND_ELT (data->inits, NULL_TREE, binfo_ctor_vtable (binfo));
8555 /* Advance the vtt index. */
8556 data->index = size_binop (PLUS_EXPR, data->index,
8557 TYPE_SIZE_UNIT (ptr_type_node));
8559 return NULL_TREE;
8562 /* Called from build_vtt_inits via dfs_walk. After building
8563 constructor vtables and generating the sub-vtt from them, we need
8564 to restore the BINFO_VTABLES that were scribbled on. DATA is the
8565 binfo of the base whose sub vtt was generated. */
8567 static tree
8568 dfs_fixup_binfo_vtbls (tree binfo, void* data)
8570 tree vtable = BINFO_VTABLE (binfo);
8572 if (!TYPE_CONTAINS_VPTR_P (BINFO_TYPE (binfo)))
8573 /* If this class has no vtable, none of its bases do. */
8574 return dfs_skip_bases;
8576 if (!vtable)
8577 /* This might be a primary base, so have no vtable in this
8578 hierarchy. */
8579 return NULL_TREE;
8581 /* If we scribbled the construction vtable vptr into BINFO, clear it
8582 out now. */
8583 if (TREE_CODE (vtable) == TREE_LIST
8584 && (TREE_PURPOSE (vtable) == (tree) data))
8585 BINFO_VTABLE (binfo) = TREE_CHAIN (vtable);
8587 return NULL_TREE;
8590 /* Build the construction vtable group for BINFO which is in the
8591 hierarchy dominated by T. */
8593 static void
8594 build_ctor_vtbl_group (tree binfo, tree t)
8596 tree type;
8597 tree vtbl;
8598 tree id;
8599 tree vbase;
8600 vec<constructor_elt, va_gc> *v;
8602 /* See if we've already created this construction vtable group. */
8603 id = mangle_ctor_vtbl_for_type (t, binfo);
8604 if (IDENTIFIER_GLOBAL_VALUE (id))
8605 return;
8607 gcc_assert (!SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), t));
8608 /* Build a version of VTBL (with the wrong type) for use in
8609 constructing the addresses of secondary vtables in the
8610 construction vtable group. */
8611 vtbl = build_vtable (t, id, ptr_type_node);
8612 DECL_CONSTRUCTION_VTABLE_P (vtbl) = 1;
8613 /* Don't export construction vtables from shared libraries. Even on
8614 targets that don't support hidden visibility, this tells
8615 can_refer_decl_in_current_unit_p not to assume that it's safe to
8616 access from a different compilation unit (bz 54314). */
8617 DECL_VISIBILITY (vtbl) = VISIBILITY_HIDDEN;
8618 DECL_VISIBILITY_SPECIFIED (vtbl) = true;
8620 v = NULL;
8621 accumulate_vtbl_inits (binfo, TYPE_BINFO (TREE_TYPE (binfo)),
8622 binfo, vtbl, t, &v);
8624 /* Add the vtables for each of our virtual bases using the vbase in T
8625 binfo. */
8626 for (vbase = TYPE_BINFO (BINFO_TYPE (binfo));
8627 vbase;
8628 vbase = TREE_CHAIN (vbase))
8630 tree b;
8632 if (!BINFO_VIRTUAL_P (vbase))
8633 continue;
8634 b = copied_binfo (vbase, binfo);
8636 accumulate_vtbl_inits (b, vbase, binfo, vtbl, t, &v);
8639 /* Figure out the type of the construction vtable. */
8640 type = build_array_of_n_type (vtable_entry_type, v->length ());
8641 layout_type (type);
8642 TREE_TYPE (vtbl) = type;
8643 DECL_SIZE (vtbl) = DECL_SIZE_UNIT (vtbl) = NULL_TREE;
8644 layout_decl (vtbl, 0);
8646 /* Initialize the construction vtable. */
8647 CLASSTYPE_VTABLES (t) = chainon (CLASSTYPE_VTABLES (t), vtbl);
8648 initialize_artificial_var (vtbl, v);
8649 dump_vtable (t, binfo, vtbl);
8652 /* Add the vtbl initializers for BINFO (and its bases other than
8653 non-virtual primaries) to the list of INITS. BINFO is in the
8654 hierarchy dominated by T. RTTI_BINFO is the binfo within T of
8655 the constructor the vtbl inits should be accumulated for. (If this
8656 is the complete object vtbl then RTTI_BINFO will be TYPE_BINFO (T).)
8657 ORIG_BINFO is the binfo for this object within BINFO_TYPE (RTTI_BINFO).
8658 BINFO is the active base equivalent of ORIG_BINFO in the inheritance
8659 graph of T. Both BINFO and ORIG_BINFO will have the same BINFO_TYPE,
8660 but are not necessarily the same in terms of layout. */
8662 static void
8663 accumulate_vtbl_inits (tree binfo,
8664 tree orig_binfo,
8665 tree rtti_binfo,
8666 tree vtbl,
8667 tree t,
8668 vec<constructor_elt, va_gc> **inits)
8670 int i;
8671 tree base_binfo;
8672 int ctor_vtbl_p = !SAME_BINFO_TYPE_P (BINFO_TYPE (rtti_binfo), t);
8674 gcc_assert (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), BINFO_TYPE (orig_binfo)));
8676 /* If it doesn't have a vptr, we don't do anything. */
8677 if (!TYPE_CONTAINS_VPTR_P (BINFO_TYPE (binfo)))
8678 return;
8680 /* If we're building a construction vtable, we're not interested in
8681 subobjects that don't require construction vtables. */
8682 if (ctor_vtbl_p
8683 && !CLASSTYPE_VBASECLASSES (BINFO_TYPE (binfo))
8684 && !binfo_via_virtual (orig_binfo, BINFO_TYPE (rtti_binfo)))
8685 return;
8687 /* Build the initializers for the BINFO-in-T vtable. */
8688 dfs_accumulate_vtbl_inits (binfo, orig_binfo, rtti_binfo, vtbl, t, inits);
8690 /* Walk the BINFO and its bases. We walk in preorder so that as we
8691 initialize each vtable we can figure out at what offset the
8692 secondary vtable lies from the primary vtable. We can't use
8693 dfs_walk here because we need to iterate through bases of BINFO
8694 and RTTI_BINFO simultaneously. */
8695 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
8697 /* Skip virtual bases. */
8698 if (BINFO_VIRTUAL_P (base_binfo))
8699 continue;
8700 accumulate_vtbl_inits (base_binfo,
8701 BINFO_BASE_BINFO (orig_binfo, i),
8702 rtti_binfo, vtbl, t,
8703 inits);
8707 /* Called from accumulate_vtbl_inits. Adds the initializers for the
8708 BINFO vtable to L. */
8710 static void
8711 dfs_accumulate_vtbl_inits (tree binfo,
8712 tree orig_binfo,
8713 tree rtti_binfo,
8714 tree orig_vtbl,
8715 tree t,
8716 vec<constructor_elt, va_gc> **l)
8718 tree vtbl = NULL_TREE;
8719 int ctor_vtbl_p = !SAME_BINFO_TYPE_P (BINFO_TYPE (rtti_binfo), t);
8720 int n_inits;
8722 if (ctor_vtbl_p
8723 && BINFO_VIRTUAL_P (orig_binfo) && BINFO_PRIMARY_P (orig_binfo))
8725 /* In the hierarchy of BINFO_TYPE (RTTI_BINFO), this is a
8726 primary virtual base. If it is not the same primary in
8727 the hierarchy of T, we'll need to generate a ctor vtable
8728 for it, to place at its location in T. If it is the same
8729 primary, we still need a VTT entry for the vtable, but it
8730 should point to the ctor vtable for the base it is a
8731 primary for within the sub-hierarchy of RTTI_BINFO.
8733 There are three possible cases:
8735 1) We are in the same place.
8736 2) We are a primary base within a lost primary virtual base of
8737 RTTI_BINFO.
8738 3) We are primary to something not a base of RTTI_BINFO. */
8740 tree b;
8741 tree last = NULL_TREE;
8743 /* First, look through the bases we are primary to for RTTI_BINFO
8744 or a virtual base. */
8745 b = binfo;
8746 while (BINFO_PRIMARY_P (b))
8748 b = BINFO_INHERITANCE_CHAIN (b);
8749 last = b;
8750 if (BINFO_VIRTUAL_P (b) || b == rtti_binfo)
8751 goto found;
8753 /* If we run out of primary links, keep looking down our
8754 inheritance chain; we might be an indirect primary. */
8755 for (b = last; b; b = BINFO_INHERITANCE_CHAIN (b))
8756 if (BINFO_VIRTUAL_P (b) || b == rtti_binfo)
8757 break;
8758 found:
8760 /* If we found RTTI_BINFO, this is case 1. If we found a virtual
8761 base B and it is a base of RTTI_BINFO, this is case 2. In
8762 either case, we share our vtable with LAST, i.e. the
8763 derived-most base within B of which we are a primary. */
8764 if (b == rtti_binfo
8765 || (b && binfo_for_vbase (BINFO_TYPE (b), BINFO_TYPE (rtti_binfo))))
8766 /* Just set our BINFO_VTABLE to point to LAST, as we may not have
8767 set LAST's BINFO_VTABLE yet. We'll extract the actual vptr in
8768 binfo_ctor_vtable after everything's been set up. */
8769 vtbl = last;
8771 /* Otherwise, this is case 3 and we get our own. */
8773 else if (!BINFO_NEW_VTABLE_MARKED (orig_binfo))
8774 return;
8776 n_inits = vec_safe_length (*l);
8778 if (!vtbl)
8780 tree index;
8781 int non_fn_entries;
8783 /* Add the initializer for this vtable. */
8784 build_vtbl_initializer (binfo, orig_binfo, t, rtti_binfo,
8785 &non_fn_entries, l);
8787 /* Figure out the position to which the VPTR should point. */
8788 vtbl = build1 (ADDR_EXPR, vtbl_ptr_type_node, orig_vtbl);
8789 index = size_binop (MULT_EXPR,
8790 TYPE_SIZE_UNIT (vtable_entry_type),
8791 size_int (non_fn_entries + n_inits));
8792 vtbl = fold_build_pointer_plus (vtbl, index);
8795 if (ctor_vtbl_p)
8796 /* For a construction vtable, we can't overwrite BINFO_VTABLE.
8797 So, we make a TREE_LIST. Later, dfs_fixup_binfo_vtbls will
8798 straighten this out. */
8799 BINFO_VTABLE (binfo) = tree_cons (rtti_binfo, vtbl, BINFO_VTABLE (binfo));
8800 else if (BINFO_PRIMARY_P (binfo) && BINFO_VIRTUAL_P (binfo))
8801 /* Throw away any unneeded intializers. */
8802 (*l)->truncate (n_inits);
8803 else
8804 /* For an ordinary vtable, set BINFO_VTABLE. */
8805 BINFO_VTABLE (binfo) = vtbl;
8808 static GTY(()) tree abort_fndecl_addr;
8810 /* Construct the initializer for BINFO's virtual function table. BINFO
8811 is part of the hierarchy dominated by T. If we're building a
8812 construction vtable, the ORIG_BINFO is the binfo we should use to
8813 find the actual function pointers to put in the vtable - but they
8814 can be overridden on the path to most-derived in the graph that
8815 ORIG_BINFO belongs. Otherwise,
8816 ORIG_BINFO should be the same as BINFO. The RTTI_BINFO is the
8817 BINFO that should be indicated by the RTTI information in the
8818 vtable; it will be a base class of T, rather than T itself, if we
8819 are building a construction vtable.
8821 The value returned is a TREE_LIST suitable for wrapping in a
8822 CONSTRUCTOR to use as the DECL_INITIAL for a vtable. If
8823 NON_FN_ENTRIES_P is not NULL, *NON_FN_ENTRIES_P is set to the
8824 number of non-function entries in the vtable.
8826 It might seem that this function should never be called with a
8827 BINFO for which BINFO_PRIMARY_P holds, the vtable for such a
8828 base is always subsumed by a derived class vtable. However, when
8829 we are building construction vtables, we do build vtables for
8830 primary bases; we need these while the primary base is being
8831 constructed. */
8833 static void
8834 build_vtbl_initializer (tree binfo,
8835 tree orig_binfo,
8836 tree t,
8837 tree rtti_binfo,
8838 int* non_fn_entries_p,
8839 vec<constructor_elt, va_gc> **inits)
8841 tree v;
8842 vtbl_init_data vid;
8843 unsigned ix, jx;
8844 tree vbinfo;
8845 vec<tree, va_gc> *vbases;
8846 constructor_elt *e;
8848 /* Initialize VID. */
8849 memset (&vid, 0, sizeof (vid));
8850 vid.binfo = binfo;
8851 vid.derived = t;
8852 vid.rtti_binfo = rtti_binfo;
8853 vid.primary_vtbl_p = SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), t);
8854 vid.ctor_vtbl_p = !SAME_BINFO_TYPE_P (BINFO_TYPE (rtti_binfo), t);
8855 vid.generate_vcall_entries = true;
8856 /* The first vbase or vcall offset is at index -3 in the vtable. */
8857 vid.index = ssize_int(-3 * TARGET_VTABLE_DATA_ENTRY_DISTANCE);
8859 /* Add entries to the vtable for RTTI. */
8860 build_rtti_vtbl_entries (binfo, &vid);
8862 /* Create an array for keeping track of the functions we've
8863 processed. When we see multiple functions with the same
8864 signature, we share the vcall offsets. */
8865 vec_alloc (vid.fns, 32);
8866 /* Add the vcall and vbase offset entries. */
8867 build_vcall_and_vbase_vtbl_entries (binfo, &vid);
8869 /* Clear BINFO_VTABLE_PATH_MARKED; it's set by
8870 build_vbase_offset_vtbl_entries. */
8871 for (vbases = CLASSTYPE_VBASECLASSES (t), ix = 0;
8872 vec_safe_iterate (vbases, ix, &vbinfo); ix++)
8873 BINFO_VTABLE_PATH_MARKED (vbinfo) = 0;
8875 /* If the target requires padding between data entries, add that now. */
8876 if (TARGET_VTABLE_DATA_ENTRY_DISTANCE > 1)
8878 int n_entries = vec_safe_length (vid.inits);
8880 vec_safe_grow (vid.inits, TARGET_VTABLE_DATA_ENTRY_DISTANCE * n_entries);
8882 /* Move data entries into their new positions and add padding
8883 after the new positions. Iterate backwards so we don't
8884 overwrite entries that we would need to process later. */
8885 for (ix = n_entries - 1;
8886 vid.inits->iterate (ix, &e);
8887 ix--)
8889 int j;
8890 int new_position = (TARGET_VTABLE_DATA_ENTRY_DISTANCE * ix
8891 + (TARGET_VTABLE_DATA_ENTRY_DISTANCE - 1));
8893 (*vid.inits)[new_position] = *e;
8895 for (j = 1; j < TARGET_VTABLE_DATA_ENTRY_DISTANCE; ++j)
8897 constructor_elt *f = &(*vid.inits)[new_position - j];
8898 f->index = NULL_TREE;
8899 f->value = build1 (NOP_EXPR, vtable_entry_type,
8900 null_pointer_node);
8905 if (non_fn_entries_p)
8906 *non_fn_entries_p = vec_safe_length (vid.inits);
8908 /* The initializers for virtual functions were built up in reverse
8909 order. Straighten them out and add them to the running list in one
8910 step. */
8911 jx = vec_safe_length (*inits);
8912 vec_safe_grow (*inits, jx + vid.inits->length ());
8914 for (ix = vid.inits->length () - 1;
8915 vid.inits->iterate (ix, &e);
8916 ix--, jx++)
8917 (**inits)[jx] = *e;
8919 /* Go through all the ordinary virtual functions, building up
8920 initializers. */
8921 for (v = BINFO_VIRTUALS (orig_binfo); v; v = TREE_CHAIN (v))
8923 tree delta;
8924 tree vcall_index;
8925 tree fn, fn_original;
8926 tree init = NULL_TREE;
8928 fn = BV_FN (v);
8929 fn_original = fn;
8930 if (DECL_THUNK_P (fn))
8932 if (!DECL_NAME (fn))
8933 finish_thunk (fn);
8934 if (THUNK_ALIAS (fn))
8936 fn = THUNK_ALIAS (fn);
8937 BV_FN (v) = fn;
8939 fn_original = THUNK_TARGET (fn);
8942 /* If the only definition of this function signature along our
8943 primary base chain is from a lost primary, this vtable slot will
8944 never be used, so just zero it out. This is important to avoid
8945 requiring extra thunks which cannot be generated with the function.
8947 We first check this in update_vtable_entry_for_fn, so we handle
8948 restored primary bases properly; we also need to do it here so we
8949 zero out unused slots in ctor vtables, rather than filling them
8950 with erroneous values (though harmless, apart from relocation
8951 costs). */
8952 if (BV_LOST_PRIMARY (v))
8953 init = size_zero_node;
8955 if (! init)
8957 /* Pull the offset for `this', and the function to call, out of
8958 the list. */
8959 delta = BV_DELTA (v);
8960 vcall_index = BV_VCALL_INDEX (v);
8962 gcc_assert (TREE_CODE (delta) == INTEGER_CST);
8963 gcc_assert (TREE_CODE (fn) == FUNCTION_DECL);
8965 /* You can't call an abstract virtual function; it's abstract.
8966 So, we replace these functions with __pure_virtual. */
8967 if (DECL_PURE_VIRTUAL_P (fn_original))
8969 fn = abort_fndecl;
8970 if (!TARGET_VTABLE_USES_DESCRIPTORS)
8972 if (abort_fndecl_addr == NULL)
8973 abort_fndecl_addr
8974 = fold_convert (vfunc_ptr_type_node,
8975 build_fold_addr_expr (fn));
8976 init = abort_fndecl_addr;
8979 /* Likewise for deleted virtuals. */
8980 else if (DECL_DELETED_FN (fn_original))
8982 fn = get_identifier ("__cxa_deleted_virtual");
8983 if (!get_global_value_if_present (fn, &fn))
8984 fn = push_library_fn (fn, (build_function_type_list
8985 (void_type_node, NULL_TREE)),
8986 NULL_TREE, ECF_NORETURN);
8987 if (!TARGET_VTABLE_USES_DESCRIPTORS)
8988 init = fold_convert (vfunc_ptr_type_node,
8989 build_fold_addr_expr (fn));
8991 else
8993 if (!integer_zerop (delta) || vcall_index)
8995 fn = make_thunk (fn, /*this_adjusting=*/1, delta, vcall_index);
8996 if (!DECL_NAME (fn))
8997 finish_thunk (fn);
8999 /* Take the address of the function, considering it to be of an
9000 appropriate generic type. */
9001 if (!TARGET_VTABLE_USES_DESCRIPTORS)
9002 init = fold_convert (vfunc_ptr_type_node,
9003 build_fold_addr_expr (fn));
9007 /* And add it to the chain of initializers. */
9008 if (TARGET_VTABLE_USES_DESCRIPTORS)
9010 int i;
9011 if (init == size_zero_node)
9012 for (i = 0; i < TARGET_VTABLE_USES_DESCRIPTORS; ++i)
9013 CONSTRUCTOR_APPEND_ELT (*inits, NULL_TREE, init);
9014 else
9015 for (i = 0; i < TARGET_VTABLE_USES_DESCRIPTORS; ++i)
9017 tree fdesc = build2 (FDESC_EXPR, vfunc_ptr_type_node,
9018 fn, build_int_cst (NULL_TREE, i));
9019 TREE_CONSTANT (fdesc) = 1;
9021 CONSTRUCTOR_APPEND_ELT (*inits, NULL_TREE, fdesc);
9024 else
9025 CONSTRUCTOR_APPEND_ELT (*inits, NULL_TREE, init);
9029 /* Adds to vid->inits the initializers for the vbase and vcall
9030 offsets in BINFO, which is in the hierarchy dominated by T. */
9032 static void
9033 build_vcall_and_vbase_vtbl_entries (tree binfo, vtbl_init_data* vid)
9035 tree b;
9037 /* If this is a derived class, we must first create entries
9038 corresponding to the primary base class. */
9039 b = get_primary_binfo (binfo);
9040 if (b)
9041 build_vcall_and_vbase_vtbl_entries (b, vid);
9043 /* Add the vbase entries for this base. */
9044 build_vbase_offset_vtbl_entries (binfo, vid);
9045 /* Add the vcall entries for this base. */
9046 build_vcall_offset_vtbl_entries (binfo, vid);
9049 /* Returns the initializers for the vbase offset entries in the vtable
9050 for BINFO (which is part of the class hierarchy dominated by T), in
9051 reverse order. VBASE_OFFSET_INDEX gives the vtable index
9052 where the next vbase offset will go. */
9054 static void
9055 build_vbase_offset_vtbl_entries (tree binfo, vtbl_init_data* vid)
9057 tree vbase;
9058 tree t;
9059 tree non_primary_binfo;
9061 /* If there are no virtual baseclasses, then there is nothing to
9062 do. */
9063 if (!CLASSTYPE_VBASECLASSES (BINFO_TYPE (binfo)))
9064 return;
9066 t = vid->derived;
9068 /* We might be a primary base class. Go up the inheritance hierarchy
9069 until we find the most derived class of which we are a primary base:
9070 it is the offset of that which we need to use. */
9071 non_primary_binfo = binfo;
9072 while (BINFO_INHERITANCE_CHAIN (non_primary_binfo))
9074 tree b;
9076 /* If we have reached a virtual base, then it must be a primary
9077 base (possibly multi-level) of vid->binfo, or we wouldn't
9078 have called build_vcall_and_vbase_vtbl_entries for it. But it
9079 might be a lost primary, so just skip down to vid->binfo. */
9080 if (BINFO_VIRTUAL_P (non_primary_binfo))
9082 non_primary_binfo = vid->binfo;
9083 break;
9086 b = BINFO_INHERITANCE_CHAIN (non_primary_binfo);
9087 if (get_primary_binfo (b) != non_primary_binfo)
9088 break;
9089 non_primary_binfo = b;
9092 /* Go through the virtual bases, adding the offsets. */
9093 for (vbase = TYPE_BINFO (BINFO_TYPE (binfo));
9094 vbase;
9095 vbase = TREE_CHAIN (vbase))
9097 tree b;
9098 tree delta;
9100 if (!BINFO_VIRTUAL_P (vbase))
9101 continue;
9103 /* Find the instance of this virtual base in the complete
9104 object. */
9105 b = copied_binfo (vbase, binfo);
9107 /* If we've already got an offset for this virtual base, we
9108 don't need another one. */
9109 if (BINFO_VTABLE_PATH_MARKED (b))
9110 continue;
9111 BINFO_VTABLE_PATH_MARKED (b) = 1;
9113 /* Figure out where we can find this vbase offset. */
9114 delta = size_binop (MULT_EXPR,
9115 vid->index,
9116 convert (ssizetype,
9117 TYPE_SIZE_UNIT (vtable_entry_type)));
9118 if (vid->primary_vtbl_p)
9119 BINFO_VPTR_FIELD (b) = delta;
9121 if (binfo != TYPE_BINFO (t))
9122 /* The vbase offset had better be the same. */
9123 gcc_assert (tree_int_cst_equal (delta, BINFO_VPTR_FIELD (vbase)));
9125 /* The next vbase will come at a more negative offset. */
9126 vid->index = size_binop (MINUS_EXPR, vid->index,
9127 ssize_int (TARGET_VTABLE_DATA_ENTRY_DISTANCE));
9129 /* The initializer is the delta from BINFO to this virtual base.
9130 The vbase offsets go in reverse inheritance-graph order, and
9131 we are walking in inheritance graph order so these end up in
9132 the right order. */
9133 delta = size_diffop_loc (input_location,
9134 BINFO_OFFSET (b), BINFO_OFFSET (non_primary_binfo));
9136 CONSTRUCTOR_APPEND_ELT (vid->inits, NULL_TREE,
9137 fold_build1_loc (input_location, NOP_EXPR,
9138 vtable_entry_type, delta));
9142 /* Adds the initializers for the vcall offset entries in the vtable
9143 for BINFO (which is part of the class hierarchy dominated by VID->DERIVED)
9144 to VID->INITS. */
9146 static void
9147 build_vcall_offset_vtbl_entries (tree binfo, vtbl_init_data* vid)
9149 /* We only need these entries if this base is a virtual base. We
9150 compute the indices -- but do not add to the vtable -- when
9151 building the main vtable for a class. */
9152 if (binfo == TYPE_BINFO (vid->derived)
9153 || (BINFO_VIRTUAL_P (binfo)
9154 /* If BINFO is RTTI_BINFO, then (since BINFO does not
9155 correspond to VID->DERIVED), we are building a primary
9156 construction virtual table. Since this is a primary
9157 virtual table, we do not need the vcall offsets for
9158 BINFO. */
9159 && binfo != vid->rtti_binfo))
9161 /* We need a vcall offset for each of the virtual functions in this
9162 vtable. For example:
9164 class A { virtual void f (); };
9165 class B1 : virtual public A { virtual void f (); };
9166 class B2 : virtual public A { virtual void f (); };
9167 class C: public B1, public B2 { virtual void f (); };
9169 A C object has a primary base of B1, which has a primary base of A. A
9170 C also has a secondary base of B2, which no longer has a primary base
9171 of A. So the B2-in-C construction vtable needs a secondary vtable for
9172 A, which will adjust the A* to a B2* to call f. We have no way of
9173 knowing what (or even whether) this offset will be when we define B2,
9174 so we store this "vcall offset" in the A sub-vtable and look it up in
9175 a "virtual thunk" for B2::f.
9177 We need entries for all the functions in our primary vtable and
9178 in our non-virtual bases' secondary vtables. */
9179 vid->vbase = binfo;
9180 /* If we are just computing the vcall indices -- but do not need
9181 the actual entries -- not that. */
9182 if (!BINFO_VIRTUAL_P (binfo))
9183 vid->generate_vcall_entries = false;
9184 /* Now, walk through the non-virtual bases, adding vcall offsets. */
9185 add_vcall_offset_vtbl_entries_r (binfo, vid);
9189 /* Build vcall offsets, starting with those for BINFO. */
9191 static void
9192 add_vcall_offset_vtbl_entries_r (tree binfo, vtbl_init_data* vid)
9194 int i;
9195 tree primary_binfo;
9196 tree base_binfo;
9198 /* Don't walk into virtual bases -- except, of course, for the
9199 virtual base for which we are building vcall offsets. Any
9200 primary virtual base will have already had its offsets generated
9201 through the recursion in build_vcall_and_vbase_vtbl_entries. */
9202 if (BINFO_VIRTUAL_P (binfo) && vid->vbase != binfo)
9203 return;
9205 /* If BINFO has a primary base, process it first. */
9206 primary_binfo = get_primary_binfo (binfo);
9207 if (primary_binfo)
9208 add_vcall_offset_vtbl_entries_r (primary_binfo, vid);
9210 /* Add BINFO itself to the list. */
9211 add_vcall_offset_vtbl_entries_1 (binfo, vid);
9213 /* Scan the non-primary bases of BINFO. */
9214 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
9215 if (base_binfo != primary_binfo)
9216 add_vcall_offset_vtbl_entries_r (base_binfo, vid);
9219 /* Called from build_vcall_offset_vtbl_entries_r. */
9221 static void
9222 add_vcall_offset_vtbl_entries_1 (tree binfo, vtbl_init_data* vid)
9224 /* Make entries for the rest of the virtuals. */
9225 if (abi_version_at_least (2))
9227 tree orig_fn;
9229 /* The ABI requires that the methods be processed in declaration
9230 order. G++ 3.2 used the order in the vtable. */
9231 for (orig_fn = TYPE_METHODS (BINFO_TYPE (binfo));
9232 orig_fn;
9233 orig_fn = DECL_CHAIN (orig_fn))
9234 if (DECL_VINDEX (orig_fn))
9235 add_vcall_offset (orig_fn, binfo, vid);
9237 else
9239 tree derived_virtuals;
9240 tree base_virtuals;
9241 tree orig_virtuals;
9242 /* If BINFO is a primary base, the most derived class which has
9243 BINFO as a primary base; otherwise, just BINFO. */
9244 tree non_primary_binfo;
9246 /* We might be a primary base class. Go up the inheritance hierarchy
9247 until we find the most derived class of which we are a primary base:
9248 it is the BINFO_VIRTUALS there that we need to consider. */
9249 non_primary_binfo = binfo;
9250 while (BINFO_INHERITANCE_CHAIN (non_primary_binfo))
9252 tree b;
9254 /* If we have reached a virtual base, then it must be vid->vbase,
9255 because we ignore other virtual bases in
9256 add_vcall_offset_vtbl_entries_r. In turn, it must be a primary
9257 base (possibly multi-level) of vid->binfo, or we wouldn't
9258 have called build_vcall_and_vbase_vtbl_entries for it. But it
9259 might be a lost primary, so just skip down to vid->binfo. */
9260 if (BINFO_VIRTUAL_P (non_primary_binfo))
9262 gcc_assert (non_primary_binfo == vid->vbase);
9263 non_primary_binfo = vid->binfo;
9264 break;
9267 b = BINFO_INHERITANCE_CHAIN (non_primary_binfo);
9268 if (get_primary_binfo (b) != non_primary_binfo)
9269 break;
9270 non_primary_binfo = b;
9273 if (vid->ctor_vtbl_p)
9274 /* For a ctor vtable we need the equivalent binfo within the hierarchy
9275 where rtti_binfo is the most derived type. */
9276 non_primary_binfo
9277 = original_binfo (non_primary_binfo, vid->rtti_binfo);
9279 for (base_virtuals = BINFO_VIRTUALS (binfo),
9280 derived_virtuals = BINFO_VIRTUALS (non_primary_binfo),
9281 orig_virtuals = BINFO_VIRTUALS (TYPE_BINFO (BINFO_TYPE (binfo)));
9282 base_virtuals;
9283 base_virtuals = TREE_CHAIN (base_virtuals),
9284 derived_virtuals = TREE_CHAIN (derived_virtuals),
9285 orig_virtuals = TREE_CHAIN (orig_virtuals))
9287 tree orig_fn;
9289 /* Find the declaration that originally caused this function to
9290 be present in BINFO_TYPE (binfo). */
9291 orig_fn = BV_FN (orig_virtuals);
9293 /* When processing BINFO, we only want to generate vcall slots for
9294 function slots introduced in BINFO. So don't try to generate
9295 one if the function isn't even defined in BINFO. */
9296 if (!SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), DECL_CONTEXT (orig_fn)))
9297 continue;
9299 add_vcall_offset (orig_fn, binfo, vid);
9304 /* Add a vcall offset entry for ORIG_FN to the vtable. */
9306 static void
9307 add_vcall_offset (tree orig_fn, tree binfo, vtbl_init_data *vid)
9309 size_t i;
9310 tree vcall_offset;
9311 tree derived_entry;
9313 /* If there is already an entry for a function with the same
9314 signature as FN, then we do not need a second vcall offset.
9315 Check the list of functions already present in the derived
9316 class vtable. */
9317 FOR_EACH_VEC_SAFE_ELT (vid->fns, i, derived_entry)
9319 if (same_signature_p (derived_entry, orig_fn)
9320 /* We only use one vcall offset for virtual destructors,
9321 even though there are two virtual table entries. */
9322 || (DECL_DESTRUCTOR_P (derived_entry)
9323 && DECL_DESTRUCTOR_P (orig_fn)))
9324 return;
9327 /* If we are building these vcall offsets as part of building
9328 the vtable for the most derived class, remember the vcall
9329 offset. */
9330 if (vid->binfo == TYPE_BINFO (vid->derived))
9332 tree_pair_s elt = {orig_fn, vid->index};
9333 vec_safe_push (CLASSTYPE_VCALL_INDICES (vid->derived), elt);
9336 /* The next vcall offset will be found at a more negative
9337 offset. */
9338 vid->index = size_binop (MINUS_EXPR, vid->index,
9339 ssize_int (TARGET_VTABLE_DATA_ENTRY_DISTANCE));
9341 /* Keep track of this function. */
9342 vec_safe_push (vid->fns, orig_fn);
9344 if (vid->generate_vcall_entries)
9346 tree base;
9347 tree fn;
9349 /* Find the overriding function. */
9350 fn = find_final_overrider (vid->rtti_binfo, binfo, orig_fn);
9351 if (fn == error_mark_node)
9352 vcall_offset = build_zero_cst (vtable_entry_type);
9353 else
9355 base = TREE_VALUE (fn);
9357 /* The vbase we're working on is a primary base of
9358 vid->binfo. But it might be a lost primary, so its
9359 BINFO_OFFSET might be wrong, so we just use the
9360 BINFO_OFFSET from vid->binfo. */
9361 vcall_offset = size_diffop_loc (input_location,
9362 BINFO_OFFSET (base),
9363 BINFO_OFFSET (vid->binfo));
9364 vcall_offset = fold_build1_loc (input_location,
9365 NOP_EXPR, vtable_entry_type,
9366 vcall_offset);
9368 /* Add the initializer to the vtable. */
9369 CONSTRUCTOR_APPEND_ELT (vid->inits, NULL_TREE, vcall_offset);
9373 /* Return vtbl initializers for the RTTI entries corresponding to the
9374 BINFO's vtable. The RTTI entries should indicate the object given
9375 by VID->rtti_binfo. */
9377 static void
9378 build_rtti_vtbl_entries (tree binfo, vtbl_init_data* vid)
9380 tree b;
9381 tree t;
9382 tree offset;
9383 tree decl;
9384 tree init;
9386 t = BINFO_TYPE (vid->rtti_binfo);
9388 /* To find the complete object, we will first convert to our most
9389 primary base, and then add the offset in the vtbl to that value. */
9390 b = binfo;
9391 while (CLASSTYPE_HAS_PRIMARY_BASE_P (BINFO_TYPE (b))
9392 && !BINFO_LOST_PRIMARY_P (b))
9394 tree primary_base;
9396 primary_base = get_primary_binfo (b);
9397 gcc_assert (BINFO_PRIMARY_P (primary_base)
9398 && BINFO_INHERITANCE_CHAIN (primary_base) == b);
9399 b = primary_base;
9401 offset = size_diffop_loc (input_location,
9402 BINFO_OFFSET (vid->rtti_binfo), BINFO_OFFSET (b));
9404 /* The second entry is the address of the typeinfo object. */
9405 if (flag_rtti)
9406 decl = build_address (get_tinfo_decl (t));
9407 else
9408 decl = integer_zero_node;
9410 /* Convert the declaration to a type that can be stored in the
9411 vtable. */
9412 init = build_nop (vfunc_ptr_type_node, decl);
9413 CONSTRUCTOR_APPEND_ELT (vid->inits, NULL_TREE, init);
9415 /* Add the offset-to-top entry. It comes earlier in the vtable than
9416 the typeinfo entry. Convert the offset to look like a
9417 function pointer, so that we can put it in the vtable. */
9418 init = build_nop (vfunc_ptr_type_node, offset);
9419 CONSTRUCTOR_APPEND_ELT (vid->inits, NULL_TREE, init);
9422 /* TRUE iff TYPE is uniquely derived from PARENT. Ignores
9423 accessibility. */
9425 bool
9426 uniquely_derived_from_p (tree parent, tree type)
9428 tree base = lookup_base (type, parent, ba_unique, NULL, tf_none);
9429 return base && base != error_mark_node;
9432 /* TRUE iff TYPE is publicly & uniquely derived from PARENT. */
9434 bool
9435 publicly_uniquely_derived_p (tree parent, tree type)
9437 tree base = lookup_base (type, parent, ba_ignore_scope | ba_check,
9438 NULL, tf_none);
9439 return base && base != error_mark_node;
9442 /* CTX1 and CTX2 are declaration contexts. Return the innermost common
9443 class between them, if any. */
9445 tree
9446 common_enclosing_class (tree ctx1, tree ctx2)
9448 if (!TYPE_P (ctx1) || !TYPE_P (ctx2))
9449 return NULL_TREE;
9450 gcc_assert (ctx1 == TYPE_MAIN_VARIANT (ctx1)
9451 && ctx2 == TYPE_MAIN_VARIANT (ctx2));
9452 if (ctx1 == ctx2)
9453 return ctx1;
9454 for (tree t = ctx1; TYPE_P (t); t = TYPE_CONTEXT (t))
9455 TYPE_MARKED_P (t) = true;
9456 tree found = NULL_TREE;
9457 for (tree t = ctx2; TYPE_P (t); t = TYPE_CONTEXT (t))
9458 if (TYPE_MARKED_P (t))
9460 found = t;
9461 break;
9463 for (tree t = ctx1; TYPE_P (t); t = TYPE_CONTEXT (t))
9464 TYPE_MARKED_P (t) = false;
9465 return found;
9468 #include "gt-cp-class.h"