* arm.c (arm_get_frame_offsets): Validate architecture supports
[official-gcc.git] / gcc / cp / class.c
blob3d34b92cfb1b22ecc5cc4da5e34f775bd54a3fce
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"
40 /* The number of nested classes being processed. If we are not in the
41 scope of any class, this is zero. */
43 int current_class_depth;
45 /* In order to deal with nested classes, we keep a stack of classes.
46 The topmost entry is the innermost class, and is the entry at index
47 CURRENT_CLASS_DEPTH */
49 typedef struct class_stack_node {
50 /* The name of the class. */
51 tree name;
53 /* The _TYPE node for the class. */
54 tree type;
56 /* The access specifier pending for new declarations in the scope of
57 this class. */
58 tree access;
60 /* If were defining TYPE, the names used in this class. */
61 splay_tree names_used;
63 /* Nonzero if this class is no longer open, because of a call to
64 push_to_top_level. */
65 size_t hidden;
66 }* class_stack_node_t;
68 typedef struct vtbl_init_data_s
70 /* The base for which we're building initializers. */
71 tree binfo;
72 /* The type of the most-derived type. */
73 tree derived;
74 /* The binfo for the dynamic type. This will be TYPE_BINFO (derived),
75 unless ctor_vtbl_p is true. */
76 tree rtti_binfo;
77 /* The negative-index vtable initializers built up so far. These
78 are in order from least negative index to most negative index. */
79 vec<constructor_elt, va_gc> *inits;
80 /* The binfo for the virtual base for which we're building
81 vcall offset initializers. */
82 tree vbase;
83 /* The functions in vbase for which we have already provided vcall
84 offsets. */
85 vec<tree, va_gc> *fns;
86 /* The vtable index of the next vcall or vbase offset. */
87 tree index;
88 /* Nonzero if we are building the initializer for the primary
89 vtable. */
90 int primary_vtbl_p;
91 /* Nonzero if we are building the initializer for a construction
92 vtable. */
93 int ctor_vtbl_p;
94 /* True when adding vcall offset entries to the vtable. False when
95 merely computing the indices. */
96 bool generate_vcall_entries;
97 } vtbl_init_data;
99 /* The type of a function passed to walk_subobject_offsets. */
100 typedef int (*subobject_offset_fn) (tree, tree, splay_tree);
102 /* The stack itself. This is a dynamically resized array. The
103 number of elements allocated is CURRENT_CLASS_STACK_SIZE. */
104 static int current_class_stack_size;
105 static class_stack_node_t current_class_stack;
107 /* The size of the largest empty class seen in this translation unit. */
108 static GTY (()) tree sizeof_biggest_empty_class;
110 /* An array of all local classes present in this translation unit, in
111 declaration order. */
112 vec<tree, va_gc> *local_classes;
114 static tree get_vfield_name (tree);
115 static void finish_struct_anon (tree);
116 static tree get_vtable_name (tree);
117 static tree get_basefndecls (tree, tree);
118 static int build_primary_vtable (tree, tree);
119 static int build_secondary_vtable (tree);
120 static void finish_vtbls (tree);
121 static void modify_vtable_entry (tree, tree, tree, tree, tree *);
122 static void finish_struct_bits (tree);
123 static int alter_access (tree, tree, tree);
124 static void handle_using_decl (tree, tree);
125 static tree dfs_modify_vtables (tree, void *);
126 static tree modify_all_vtables (tree, tree);
127 static void determine_primary_bases (tree);
128 static void finish_struct_methods (tree);
129 static void maybe_warn_about_overly_private_class (tree);
130 static int method_name_cmp (const void *, const void *);
131 static int resort_method_name_cmp (const void *, const void *);
132 static void add_implicitly_declared_members (tree, tree*, int, int);
133 static tree fixed_type_or_null (tree, int *, int *);
134 static tree build_simple_base_path (tree expr, tree binfo);
135 static tree build_vtbl_ref_1 (tree, tree);
136 static void build_vtbl_initializer (tree, tree, tree, tree, int *,
137 vec<constructor_elt, va_gc> **);
138 static int count_fields (tree);
139 static int add_fields_to_record_type (tree, struct sorted_fields_type*, int);
140 static void insert_into_classtype_sorted_fields (tree, tree, int);
141 static bool check_bitfield_decl (tree);
142 static void check_field_decl (tree, tree, int *, int *, int *);
143 static void check_field_decls (tree, tree *, int *, int *);
144 static tree *build_base_field (record_layout_info, tree, splay_tree, tree *);
145 static void build_base_fields (record_layout_info, splay_tree, tree *);
146 static void check_methods (tree);
147 static void remove_zero_width_bit_fields (tree);
148 static void check_bases (tree, int *, int *);
149 static void check_bases_and_members (tree);
150 static tree create_vtable_ptr (tree, tree *);
151 static void include_empty_classes (record_layout_info);
152 static void layout_class_type (tree, tree *);
153 static void propagate_binfo_offsets (tree, tree);
154 static void layout_virtual_bases (record_layout_info, splay_tree);
155 static void build_vbase_offset_vtbl_entries (tree, vtbl_init_data *);
156 static void add_vcall_offset_vtbl_entries_r (tree, vtbl_init_data *);
157 static void add_vcall_offset_vtbl_entries_1 (tree, vtbl_init_data *);
158 static void build_vcall_offset_vtbl_entries (tree, vtbl_init_data *);
159 static void add_vcall_offset (tree, tree, vtbl_init_data *);
160 static void layout_vtable_decl (tree, int);
161 static tree dfs_find_final_overrider_pre (tree, void *);
162 static tree dfs_find_final_overrider_post (tree, void *);
163 static tree find_final_overrider (tree, tree, tree);
164 static int make_new_vtable (tree, tree);
165 static tree get_primary_binfo (tree);
166 static int maybe_indent_hierarchy (FILE *, int, int);
167 static tree dump_class_hierarchy_r (FILE *, int, tree, tree, int);
168 static void dump_class_hierarchy (tree);
169 static void dump_class_hierarchy_1 (FILE *, int, tree);
170 static void dump_array (FILE *, tree);
171 static void dump_vtable (tree, tree, tree);
172 static void dump_vtt (tree, tree);
173 static void dump_thunk (FILE *, int, tree);
174 static tree build_vtable (tree, tree, tree);
175 static void initialize_vtable (tree, vec<constructor_elt, va_gc> *);
176 static void layout_nonempty_base_or_field (record_layout_info,
177 tree, tree, splay_tree);
178 static tree end_of_class (tree, int);
179 static bool layout_empty_base (record_layout_info, tree, tree, splay_tree);
180 static void accumulate_vtbl_inits (tree, tree, tree, tree, tree,
181 vec<constructor_elt, va_gc> **);
182 static void dfs_accumulate_vtbl_inits (tree, tree, tree, tree, tree,
183 vec<constructor_elt, va_gc> **);
184 static void build_rtti_vtbl_entries (tree, vtbl_init_data *);
185 static void build_vcall_and_vbase_vtbl_entries (tree, vtbl_init_data *);
186 static void clone_constructors_and_destructors (tree);
187 static tree build_clone (tree, tree);
188 static void update_vtable_entry_for_fn (tree, tree, tree, tree *, unsigned);
189 static void build_ctor_vtbl_group (tree, tree);
190 static void build_vtt (tree);
191 static tree binfo_ctor_vtable (tree);
192 static void build_vtt_inits (tree, tree, vec<constructor_elt, va_gc> **,
193 tree *);
194 static tree dfs_build_secondary_vptr_vtt_inits (tree, void *);
195 static tree dfs_fixup_binfo_vtbls (tree, void *);
196 static int record_subobject_offset (tree, tree, splay_tree);
197 static int check_subobject_offset (tree, tree, splay_tree);
198 static int walk_subobject_offsets (tree, subobject_offset_fn,
199 tree, splay_tree, tree, int);
200 static void record_subobject_offsets (tree, tree, splay_tree, bool);
201 static int layout_conflict_p (tree, tree, splay_tree, int);
202 static int splay_tree_compare_integer_csts (splay_tree_key k1,
203 splay_tree_key k2);
204 static void warn_about_ambiguous_bases (tree);
205 static bool type_requires_array_cookie (tree);
206 static bool contains_empty_class_p (tree);
207 static bool base_derived_from (tree, tree);
208 static int empty_base_at_nonzero_offset_p (tree, tree, splay_tree);
209 static tree end_of_base (tree);
210 static tree get_vcall_index (tree, tree);
212 /* Variables shared between class.c and call.c. */
214 int n_vtables = 0;
215 int n_vtable_entries = 0;
216 int n_vtable_searches = 0;
217 int n_vtable_elems = 0;
218 int n_convert_harshness = 0;
219 int n_compute_conversion_costs = 0;
220 int n_inner_fields_searched = 0;
222 /* Convert to or from a base subobject. EXPR is an expression of type
223 `A' or `A*', an expression of type `B' or `B*' is returned. To
224 convert A to a base B, CODE is PLUS_EXPR and BINFO is the binfo for
225 the B base instance within A. To convert base A to derived B, CODE
226 is MINUS_EXPR and BINFO is the binfo for the A instance within B.
227 In this latter case, A must not be a morally virtual base of B.
228 NONNULL is true if EXPR is known to be non-NULL (this is only
229 needed when EXPR is of pointer type). CV qualifiers are preserved
230 from EXPR. */
232 tree
233 build_base_path (enum tree_code code,
234 tree expr,
235 tree binfo,
236 int nonnull,
237 tsubst_flags_t complain)
239 tree v_binfo = NULL_TREE;
240 tree d_binfo = NULL_TREE;
241 tree probe;
242 tree offset;
243 tree target_type;
244 tree null_test = NULL;
245 tree ptr_target_type;
246 int fixed_type_p;
247 int want_pointer = TYPE_PTR_P (TREE_TYPE (expr));
248 bool has_empty = false;
249 bool virtual_access;
251 if (expr == error_mark_node || binfo == error_mark_node || !binfo)
252 return error_mark_node;
254 for (probe = binfo; probe; probe = BINFO_INHERITANCE_CHAIN (probe))
256 d_binfo = probe;
257 if (is_empty_class (BINFO_TYPE (probe)))
258 has_empty = true;
259 if (!v_binfo && BINFO_VIRTUAL_P (probe))
260 v_binfo = probe;
263 probe = TYPE_MAIN_VARIANT (TREE_TYPE (expr));
264 if (want_pointer)
265 probe = TYPE_MAIN_VARIANT (TREE_TYPE (probe));
267 if (code == PLUS_EXPR
268 && !SAME_BINFO_TYPE_P (BINFO_TYPE (d_binfo), probe))
270 /* This can happen when adjust_result_of_qualified_name_lookup can't
271 find a unique base binfo in a call to a member function. We
272 couldn't give the diagnostic then since we might have been calling
273 a static member function, so we do it now. */
274 if (complain & tf_error)
276 tree base = lookup_base (probe, BINFO_TYPE (d_binfo),
277 ba_unique, NULL, complain);
278 gcc_assert (base == error_mark_node);
280 return error_mark_node;
283 gcc_assert ((code == MINUS_EXPR
284 && SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), probe))
285 || code == PLUS_EXPR);
287 if (binfo == d_binfo)
288 /* Nothing to do. */
289 return expr;
291 if (code == MINUS_EXPR && v_binfo)
293 if (complain & tf_error)
295 if (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), BINFO_TYPE (v_binfo)))
297 if (want_pointer)
298 error ("cannot convert from pointer to base class %qT to "
299 "pointer to derived class %qT because the base is "
300 "virtual", BINFO_TYPE (binfo), BINFO_TYPE (d_binfo));
301 else
302 error ("cannot convert from base class %qT to derived "
303 "class %qT because the base is virtual",
304 BINFO_TYPE (binfo), BINFO_TYPE (d_binfo));
306 else
308 if (want_pointer)
309 error ("cannot convert from pointer to base class %qT to "
310 "pointer to derived class %qT via virtual base %qT",
311 BINFO_TYPE (binfo), BINFO_TYPE (d_binfo),
312 BINFO_TYPE (v_binfo));
313 else
314 error ("cannot convert from base class %qT to derived "
315 "class %qT via virtual base %qT", BINFO_TYPE (binfo),
316 BINFO_TYPE (d_binfo), BINFO_TYPE (v_binfo));
319 return error_mark_node;
322 if (!want_pointer)
323 /* This must happen before the call to save_expr. */
324 expr = cp_build_addr_expr (expr, complain);
325 else
326 expr = mark_rvalue_use (expr);
328 offset = BINFO_OFFSET (binfo);
329 fixed_type_p = resolves_to_fixed_type_p (expr, &nonnull);
330 target_type = code == PLUS_EXPR ? BINFO_TYPE (binfo) : BINFO_TYPE (d_binfo);
331 /* TARGET_TYPE has been extracted from BINFO, and, is therefore always
332 cv-unqualified. Extract the cv-qualifiers from EXPR so that the
333 expression returned matches the input. */
334 target_type = cp_build_qualified_type
335 (target_type, cp_type_quals (TREE_TYPE (TREE_TYPE (expr))));
336 ptr_target_type = build_pointer_type (target_type);
338 /* Do we need to look in the vtable for the real offset? */
339 virtual_access = (v_binfo && fixed_type_p <= 0);
341 /* Don't bother with the calculations inside sizeof; they'll ICE if the
342 source type is incomplete and the pointer value doesn't matter. In a
343 template (even in fold_non_dependent_expr), we don't have vtables set
344 up properly yet, and the value doesn't matter there either; we're just
345 interested in the result of overload resolution. */
346 if (cp_unevaluated_operand != 0
347 || in_template_function ())
349 expr = build_nop (ptr_target_type, expr);
350 if (!want_pointer)
351 expr = build_indirect_ref (EXPR_LOCATION (expr), expr, RO_NULL);
352 return expr;
355 /* If we're in an NSDMI, we don't have the full constructor context yet
356 that we need for converting to a virtual base, so just build a stub
357 CONVERT_EXPR and expand it later in bot_replace. */
358 if (virtual_access && fixed_type_p < 0
359 && current_scope () != current_function_decl)
361 expr = build1 (CONVERT_EXPR, ptr_target_type, expr);
362 CONVERT_EXPR_VBASE_PATH (expr) = true;
363 if (!want_pointer)
364 expr = build_indirect_ref (EXPR_LOCATION (expr), expr, RO_NULL);
365 return expr;
368 /* Do we need to check for a null pointer? */
369 if (want_pointer && !nonnull)
371 /* If we know the conversion will not actually change the value
372 of EXPR, then we can avoid testing the expression for NULL.
373 We have to avoid generating a COMPONENT_REF for a base class
374 field, because other parts of the compiler know that such
375 expressions are always non-NULL. */
376 if (!virtual_access && integer_zerop (offset))
377 return build_nop (ptr_target_type, expr);
378 null_test = error_mark_node;
381 /* Protect against multiple evaluation if necessary. */
382 if (TREE_SIDE_EFFECTS (expr) && (null_test || virtual_access))
383 expr = save_expr (expr);
385 /* Now that we've saved expr, build the real null test. */
386 if (null_test)
388 tree zero = cp_convert (TREE_TYPE (expr), nullptr_node, complain);
389 null_test = fold_build2_loc (input_location, NE_EXPR, boolean_type_node,
390 expr, zero);
393 /* If this is a simple base reference, express it as a COMPONENT_REF. */
394 if (code == PLUS_EXPR && !virtual_access
395 /* We don't build base fields for empty bases, and they aren't very
396 interesting to the optimizers anyway. */
397 && !has_empty)
399 expr = cp_build_indirect_ref (expr, RO_NULL, complain);
400 expr = build_simple_base_path (expr, binfo);
401 if (want_pointer)
402 expr = build_address (expr);
403 target_type = TREE_TYPE (expr);
404 goto out;
407 if (virtual_access)
409 /* Going via virtual base V_BINFO. We need the static offset
410 from V_BINFO to BINFO, and the dynamic offset from D_BINFO to
411 V_BINFO. That offset is an entry in D_BINFO's vtable. */
412 tree v_offset;
414 if (fixed_type_p < 0 && in_base_initializer)
416 /* In a base member initializer, we cannot rely on the
417 vtable being set up. We have to indirect via the
418 vtt_parm. */
419 tree t;
421 t = TREE_TYPE (TYPE_VFIELD (current_class_type));
422 t = build_pointer_type (t);
423 v_offset = convert (t, current_vtt_parm);
424 v_offset = cp_build_indirect_ref (v_offset, RO_NULL, complain);
426 else
427 v_offset = build_vfield_ref (cp_build_indirect_ref (expr, RO_NULL,
428 complain),
429 TREE_TYPE (TREE_TYPE (expr)));
431 v_offset = fold_build_pointer_plus (v_offset, BINFO_VPTR_FIELD (v_binfo));
432 v_offset = build1 (NOP_EXPR,
433 build_pointer_type (ptrdiff_type_node),
434 v_offset);
435 v_offset = cp_build_indirect_ref (v_offset, RO_NULL, complain);
436 TREE_CONSTANT (v_offset) = 1;
438 offset = convert_to_integer (ptrdiff_type_node,
439 size_diffop_loc (input_location, offset,
440 BINFO_OFFSET (v_binfo)));
442 if (!integer_zerop (offset))
443 v_offset = build2 (code, ptrdiff_type_node, v_offset, offset);
445 if (fixed_type_p < 0)
446 /* Negative fixed_type_p means this is a constructor or destructor;
447 virtual base layout is fixed in in-charge [cd]tors, but not in
448 base [cd]tors. */
449 offset = build3 (COND_EXPR, ptrdiff_type_node,
450 build2 (EQ_EXPR, boolean_type_node,
451 current_in_charge_parm, integer_zero_node),
452 v_offset,
453 convert_to_integer (ptrdiff_type_node,
454 BINFO_OFFSET (binfo)));
455 else
456 offset = v_offset;
459 if (want_pointer)
460 target_type = ptr_target_type;
462 expr = build1 (NOP_EXPR, ptr_target_type, expr);
464 if (!integer_zerop (offset))
466 offset = fold_convert (sizetype, offset);
467 if (code == MINUS_EXPR)
468 offset = fold_build1_loc (input_location, NEGATE_EXPR, sizetype, offset);
469 expr = fold_build_pointer_plus (expr, offset);
471 else
472 null_test = NULL;
474 if (!want_pointer)
475 expr = cp_build_indirect_ref (expr, RO_NULL, complain);
477 out:
478 if (null_test)
479 expr = fold_build3_loc (input_location, COND_EXPR, target_type, null_test, expr,
480 build_zero_cst (target_type));
482 return expr;
485 /* Subroutine of build_base_path; EXPR and BINFO are as in that function.
486 Perform a derived-to-base conversion by recursively building up a
487 sequence of COMPONENT_REFs to the appropriate base fields. */
489 static tree
490 build_simple_base_path (tree expr, tree binfo)
492 tree type = BINFO_TYPE (binfo);
493 tree d_binfo = BINFO_INHERITANCE_CHAIN (binfo);
494 tree field;
496 if (d_binfo == NULL_TREE)
498 tree temp;
500 gcc_assert (TYPE_MAIN_VARIANT (TREE_TYPE (expr)) == type);
502 /* Transform `(a, b).x' into `(*(a, &b)).x', `(a ? b : c).x'
503 into `(*(a ? &b : &c)).x', and so on. A COND_EXPR is only
504 an lvalue in the front end; only _DECLs and _REFs are lvalues
505 in the back end. */
506 temp = unary_complex_lvalue (ADDR_EXPR, expr);
507 if (temp)
508 expr = cp_build_indirect_ref (temp, RO_NULL, tf_warning_or_error);
510 return expr;
513 /* Recurse. */
514 expr = build_simple_base_path (expr, d_binfo);
516 for (field = TYPE_FIELDS (BINFO_TYPE (d_binfo));
517 field; field = DECL_CHAIN (field))
518 /* Is this the base field created by build_base_field? */
519 if (TREE_CODE (field) == FIELD_DECL
520 && DECL_FIELD_IS_BASE (field)
521 && TREE_TYPE (field) == type
522 /* If we're looking for a field in the most-derived class,
523 also check the field offset; we can have two base fields
524 of the same type if one is an indirect virtual base and one
525 is a direct non-virtual base. */
526 && (BINFO_INHERITANCE_CHAIN (d_binfo)
527 || tree_int_cst_equal (byte_position (field),
528 BINFO_OFFSET (binfo))))
530 /* We don't use build_class_member_access_expr here, as that
531 has unnecessary checks, and more importantly results in
532 recursive calls to dfs_walk_once. */
533 int type_quals = cp_type_quals (TREE_TYPE (expr));
535 expr = build3 (COMPONENT_REF,
536 cp_build_qualified_type (type, type_quals),
537 expr, field, NULL_TREE);
538 expr = fold_if_not_in_template (expr);
540 /* Mark the expression const or volatile, as appropriate.
541 Even though we've dealt with the type above, we still have
542 to mark the expression itself. */
543 if (type_quals & TYPE_QUAL_CONST)
544 TREE_READONLY (expr) = 1;
545 if (type_quals & TYPE_QUAL_VOLATILE)
546 TREE_THIS_VOLATILE (expr) = 1;
548 return expr;
551 /* Didn't find the base field?!? */
552 gcc_unreachable ();
555 /* Convert OBJECT to the base TYPE. OBJECT is an expression whose
556 type is a class type or a pointer to a class type. In the former
557 case, TYPE is also a class type; in the latter it is another
558 pointer type. If CHECK_ACCESS is true, an error message is emitted
559 if TYPE is inaccessible. If OBJECT has pointer type, the value is
560 assumed to be non-NULL. */
562 tree
563 convert_to_base (tree object, tree type, bool check_access, bool nonnull,
564 tsubst_flags_t complain)
566 tree binfo;
567 tree object_type;
569 if (TYPE_PTR_P (TREE_TYPE (object)))
571 object_type = TREE_TYPE (TREE_TYPE (object));
572 type = TREE_TYPE (type);
574 else
575 object_type = TREE_TYPE (object);
577 binfo = lookup_base (object_type, type, check_access ? ba_check : ba_unique,
578 NULL, complain);
579 if (!binfo || binfo == error_mark_node)
580 return error_mark_node;
582 return build_base_path (PLUS_EXPR, object, binfo, nonnull, complain);
585 /* EXPR is an expression with unqualified class type. BASE is a base
586 binfo of that class type. Returns EXPR, converted to the BASE
587 type. This function assumes that EXPR is the most derived class;
588 therefore virtual bases can be found at their static offsets. */
590 tree
591 convert_to_base_statically (tree expr, tree base)
593 tree expr_type;
595 expr_type = TREE_TYPE (expr);
596 if (!SAME_BINFO_TYPE_P (BINFO_TYPE (base), expr_type))
598 /* If this is a non-empty base, use a COMPONENT_REF. */
599 if (!is_empty_class (BINFO_TYPE (base)))
600 return build_simple_base_path (expr, base);
602 /* We use fold_build2 and fold_convert below to simplify the trees
603 provided to the optimizers. It is not safe to call these functions
604 when processing a template because they do not handle C++-specific
605 trees. */
606 gcc_assert (!processing_template_decl);
607 expr = cp_build_addr_expr (expr, tf_warning_or_error);
608 if (!integer_zerop (BINFO_OFFSET (base)))
609 expr = fold_build_pointer_plus_loc (input_location,
610 expr, BINFO_OFFSET (base));
611 expr = fold_convert (build_pointer_type (BINFO_TYPE (base)), expr);
612 expr = build_fold_indirect_ref_loc (input_location, expr);
615 return expr;
619 tree
620 build_vfield_ref (tree datum, tree type)
622 tree vfield, vcontext;
624 if (datum == error_mark_node)
625 return error_mark_node;
627 /* First, convert to the requested type. */
628 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (datum), type))
629 datum = convert_to_base (datum, type, /*check_access=*/false,
630 /*nonnull=*/true, tf_warning_or_error);
632 /* Second, the requested type may not be the owner of its own vptr.
633 If not, convert to the base class that owns it. We cannot use
634 convert_to_base here, because VCONTEXT may appear more than once
635 in the inheritance hierarchy of TYPE, and thus direct conversion
636 between the types may be ambiguous. Following the path back up
637 one step at a time via primary bases avoids the problem. */
638 vfield = TYPE_VFIELD (type);
639 vcontext = DECL_CONTEXT (vfield);
640 while (!same_type_ignoring_top_level_qualifiers_p (vcontext, type))
642 datum = build_simple_base_path (datum, CLASSTYPE_PRIMARY_BINFO (type));
643 type = TREE_TYPE (datum);
646 return build3 (COMPONENT_REF, TREE_TYPE (vfield), datum, vfield, NULL_TREE);
649 /* Given an object INSTANCE, return an expression which yields the
650 vtable element corresponding to INDEX. There are many special
651 cases for INSTANCE which we take care of here, mainly to avoid
652 creating extra tree nodes when we don't have to. */
654 static tree
655 build_vtbl_ref_1 (tree instance, tree idx)
657 tree aref;
658 tree vtbl = NULL_TREE;
660 /* Try to figure out what a reference refers to, and
661 access its virtual function table directly. */
663 int cdtorp = 0;
664 tree fixed_type = fixed_type_or_null (instance, NULL, &cdtorp);
666 tree basetype = non_reference (TREE_TYPE (instance));
668 if (fixed_type && !cdtorp)
670 tree binfo = lookup_base (fixed_type, basetype,
671 ba_unique, NULL, tf_none);
672 if (binfo && binfo != error_mark_node)
673 vtbl = unshare_expr (BINFO_VTABLE (binfo));
676 if (!vtbl)
677 vtbl = build_vfield_ref (instance, basetype);
679 aref = build_array_ref (input_location, vtbl, idx);
680 TREE_CONSTANT (aref) |= TREE_CONSTANT (vtbl) && TREE_CONSTANT (idx);
682 return aref;
685 tree
686 build_vtbl_ref (tree instance, tree idx)
688 tree aref = build_vtbl_ref_1 (instance, idx);
690 return aref;
693 /* Given a stable object pointer INSTANCE_PTR, return an expression which
694 yields a function pointer corresponding to vtable element INDEX. */
696 tree
697 build_vfn_ref (tree instance_ptr, tree idx)
699 tree aref;
701 aref = build_vtbl_ref_1 (cp_build_indirect_ref (instance_ptr, RO_NULL,
702 tf_warning_or_error),
703 idx);
705 /* When using function descriptors, the address of the
706 vtable entry is treated as a function pointer. */
707 if (TARGET_VTABLE_USES_DESCRIPTORS)
708 aref = build1 (NOP_EXPR, TREE_TYPE (aref),
709 cp_build_addr_expr (aref, tf_warning_or_error));
711 /* Remember this as a method reference, for later devirtualization. */
712 aref = build3 (OBJ_TYPE_REF, TREE_TYPE (aref), aref, instance_ptr, idx);
714 return aref;
717 /* Return the name of the virtual function table (as an IDENTIFIER_NODE)
718 for the given TYPE. */
720 static tree
721 get_vtable_name (tree type)
723 return mangle_vtbl_for_type (type);
726 /* DECL is an entity associated with TYPE, like a virtual table or an
727 implicitly generated constructor. Determine whether or not DECL
728 should have external or internal linkage at the object file
729 level. This routine does not deal with COMDAT linkage and other
730 similar complexities; it simply sets TREE_PUBLIC if it possible for
731 entities in other translation units to contain copies of DECL, in
732 the abstract. */
734 void
735 set_linkage_according_to_type (tree /*type*/, tree decl)
737 TREE_PUBLIC (decl) = 1;
738 determine_visibility (decl);
741 /* Create a VAR_DECL for a primary or secondary vtable for CLASS_TYPE.
742 (For a secondary vtable for B-in-D, CLASS_TYPE should be D, not B.)
743 Use NAME for the name of the vtable, and VTABLE_TYPE for its type. */
745 static tree
746 build_vtable (tree class_type, tree name, tree vtable_type)
748 tree decl;
750 decl = build_lang_decl (VAR_DECL, name, vtable_type);
751 /* vtable names are already mangled; give them their DECL_ASSEMBLER_NAME
752 now to avoid confusion in mangle_decl. */
753 SET_DECL_ASSEMBLER_NAME (decl, name);
754 DECL_CONTEXT (decl) = class_type;
755 DECL_ARTIFICIAL (decl) = 1;
756 TREE_STATIC (decl) = 1;
757 TREE_READONLY (decl) = 1;
758 DECL_VIRTUAL_P (decl) = 1;
759 DECL_ALIGN (decl) = TARGET_VTABLE_ENTRY_ALIGN;
760 DECL_VTABLE_OR_VTT_P (decl) = 1;
761 /* At one time the vtable info was grabbed 2 words at a time. This
762 fails on sparc unless you have 8-byte alignment. (tiemann) */
763 DECL_ALIGN (decl) = MAX (TYPE_ALIGN (double_type_node),
764 DECL_ALIGN (decl));
765 set_linkage_according_to_type (class_type, decl);
766 /* The vtable has not been defined -- yet. */
767 DECL_EXTERNAL (decl) = 1;
768 DECL_NOT_REALLY_EXTERN (decl) = 1;
770 /* Mark the VAR_DECL node representing the vtable itself as a
771 "gratuitous" one, thereby forcing dwarfout.c to ignore it. It
772 is rather important that such things be ignored because any
773 effort to actually generate DWARF for them will run into
774 trouble when/if we encounter code like:
776 #pragma interface
777 struct S { virtual void member (); };
779 because the artificial declaration of the vtable itself (as
780 manufactured by the g++ front end) will say that the vtable is
781 a static member of `S' but only *after* the debug output for
782 the definition of `S' has already been output. This causes
783 grief because the DWARF entry for the definition of the vtable
784 will try to refer back to an earlier *declaration* of the
785 vtable as a static member of `S' and there won't be one. We
786 might be able to arrange to have the "vtable static member"
787 attached to the member list for `S' before the debug info for
788 `S' get written (which would solve the problem) but that would
789 require more intrusive changes to the g++ front end. */
790 DECL_IGNORED_P (decl) = 1;
792 return decl;
795 /* Get the VAR_DECL of the vtable for TYPE. TYPE need not be polymorphic,
796 or even complete. If this does not exist, create it. If COMPLETE is
797 nonzero, then complete the definition of it -- that will render it
798 impossible to actually build the vtable, but is useful to get at those
799 which are known to exist in the runtime. */
801 tree
802 get_vtable_decl (tree type, int complete)
804 tree decl;
806 if (CLASSTYPE_VTABLES (type))
807 return CLASSTYPE_VTABLES (type);
809 decl = build_vtable (type, get_vtable_name (type), vtbl_type_node);
810 CLASSTYPE_VTABLES (type) = decl;
812 if (complete)
814 DECL_EXTERNAL (decl) = 1;
815 cp_finish_decl (decl, NULL_TREE, false, NULL_TREE, 0);
818 return decl;
821 /* Build the primary virtual function table for TYPE. If BINFO is
822 non-NULL, build the vtable starting with the initial approximation
823 that it is the same as the one which is the head of the association
824 list. Returns a nonzero value if a new vtable is actually
825 created. */
827 static int
828 build_primary_vtable (tree binfo, tree type)
830 tree decl;
831 tree virtuals;
833 decl = get_vtable_decl (type, /*complete=*/0);
835 if (binfo)
837 if (BINFO_NEW_VTABLE_MARKED (binfo))
838 /* We have already created a vtable for this base, so there's
839 no need to do it again. */
840 return 0;
842 virtuals = copy_list (BINFO_VIRTUALS (binfo));
843 TREE_TYPE (decl) = TREE_TYPE (get_vtbl_decl_for_binfo (binfo));
844 DECL_SIZE (decl) = TYPE_SIZE (TREE_TYPE (decl));
845 DECL_SIZE_UNIT (decl) = TYPE_SIZE_UNIT (TREE_TYPE (decl));
847 else
849 gcc_assert (TREE_TYPE (decl) == vtbl_type_node);
850 virtuals = NULL_TREE;
853 if (GATHER_STATISTICS)
855 n_vtables += 1;
856 n_vtable_elems += list_length (virtuals);
859 /* Initialize the association list for this type, based
860 on our first approximation. */
861 BINFO_VTABLE (TYPE_BINFO (type)) = decl;
862 BINFO_VIRTUALS (TYPE_BINFO (type)) = virtuals;
863 SET_BINFO_NEW_VTABLE_MARKED (TYPE_BINFO (type));
864 return 1;
867 /* Give BINFO a new virtual function table which is initialized
868 with a skeleton-copy of its original initialization. The only
869 entry that changes is the `delta' entry, so we can really
870 share a lot of structure.
872 FOR_TYPE is the most derived type which caused this table to
873 be needed.
875 Returns nonzero if we haven't met BINFO before.
877 The order in which vtables are built (by calling this function) for
878 an object must remain the same, otherwise a binary incompatibility
879 can result. */
881 static int
882 build_secondary_vtable (tree binfo)
884 if (BINFO_NEW_VTABLE_MARKED (binfo))
885 /* We already created a vtable for this base. There's no need to
886 do it again. */
887 return 0;
889 /* Remember that we've created a vtable for this BINFO, so that we
890 don't try to do so again. */
891 SET_BINFO_NEW_VTABLE_MARKED (binfo);
893 /* Make fresh virtual list, so we can smash it later. */
894 BINFO_VIRTUALS (binfo) = copy_list (BINFO_VIRTUALS (binfo));
896 /* Secondary vtables are laid out as part of the same structure as
897 the primary vtable. */
898 BINFO_VTABLE (binfo) = NULL_TREE;
899 return 1;
902 /* Create a new vtable for BINFO which is the hierarchy dominated by
903 T. Return nonzero if we actually created a new vtable. */
905 static int
906 make_new_vtable (tree t, tree binfo)
908 if (binfo == TYPE_BINFO (t))
909 /* In this case, it is *type*'s vtable we are modifying. We start
910 with the approximation that its vtable is that of the
911 immediate base class. */
912 return build_primary_vtable (binfo, t);
913 else
914 /* This is our very own copy of `basetype' to play with. Later,
915 we will fill in all the virtual functions that override the
916 virtual functions in these base classes which are not defined
917 by the current type. */
918 return build_secondary_vtable (binfo);
921 /* Make *VIRTUALS, an entry on the BINFO_VIRTUALS list for BINFO
922 (which is in the hierarchy dominated by T) list FNDECL as its
923 BV_FN. DELTA is the required constant adjustment from the `this'
924 pointer where the vtable entry appears to the `this' required when
925 the function is actually called. */
927 static void
928 modify_vtable_entry (tree t,
929 tree binfo,
930 tree fndecl,
931 tree delta,
932 tree *virtuals)
934 tree v;
936 v = *virtuals;
938 if (fndecl != BV_FN (v)
939 || !tree_int_cst_equal (delta, BV_DELTA (v)))
941 /* We need a new vtable for BINFO. */
942 if (make_new_vtable (t, binfo))
944 /* If we really did make a new vtable, we also made a copy
945 of the BINFO_VIRTUALS list. Now, we have to find the
946 corresponding entry in that list. */
947 *virtuals = BINFO_VIRTUALS (binfo);
948 while (BV_FN (*virtuals) != BV_FN (v))
949 *virtuals = TREE_CHAIN (*virtuals);
950 v = *virtuals;
953 BV_DELTA (v) = delta;
954 BV_VCALL_INDEX (v) = NULL_TREE;
955 BV_FN (v) = fndecl;
960 /* Add method METHOD to class TYPE. If USING_DECL is non-null, it is
961 the USING_DECL naming METHOD. Returns true if the method could be
962 added to the method vec. */
964 bool
965 add_method (tree type, tree method, tree using_decl)
967 unsigned slot;
968 tree overload;
969 bool template_conv_p = false;
970 bool conv_p;
971 vec<tree, va_gc> *method_vec;
972 bool complete_p;
973 bool insert_p = false;
974 tree current_fns;
975 tree fns;
977 if (method == error_mark_node)
978 return false;
980 complete_p = COMPLETE_TYPE_P (type);
981 conv_p = DECL_CONV_FN_P (method);
982 if (conv_p)
983 template_conv_p = (TREE_CODE (method) == TEMPLATE_DECL
984 && DECL_TEMPLATE_CONV_FN_P (method));
986 method_vec = CLASSTYPE_METHOD_VEC (type);
987 if (!method_vec)
989 /* Make a new method vector. We start with 8 entries. We must
990 allocate at least two (for constructors and destructors), and
991 we're going to end up with an assignment operator at some
992 point as well. */
993 vec_alloc (method_vec, 8);
994 /* Create slots for constructors and destructors. */
995 method_vec->quick_push (NULL_TREE);
996 method_vec->quick_push (NULL_TREE);
997 CLASSTYPE_METHOD_VEC (type) = method_vec;
1000 /* Maintain TYPE_HAS_USER_CONSTRUCTOR, etc. */
1001 grok_special_member_properties (method);
1003 /* Constructors and destructors go in special slots. */
1004 if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (method))
1005 slot = CLASSTYPE_CONSTRUCTOR_SLOT;
1006 else if (DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (method))
1008 slot = CLASSTYPE_DESTRUCTOR_SLOT;
1010 if (TYPE_FOR_JAVA (type))
1012 if (!DECL_ARTIFICIAL (method))
1013 error ("Java class %qT cannot have a destructor", type);
1014 else if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
1015 error ("Java class %qT cannot have an implicit non-trivial "
1016 "destructor",
1017 type);
1020 else
1022 tree m;
1024 insert_p = true;
1025 /* See if we already have an entry with this name. */
1026 for (slot = CLASSTYPE_FIRST_CONVERSION_SLOT;
1027 vec_safe_iterate (method_vec, slot, &m);
1028 ++slot)
1030 m = OVL_CURRENT (m);
1031 if (template_conv_p)
1033 if (TREE_CODE (m) == TEMPLATE_DECL
1034 && DECL_TEMPLATE_CONV_FN_P (m))
1035 insert_p = false;
1036 break;
1038 if (conv_p && !DECL_CONV_FN_P (m))
1039 break;
1040 if (DECL_NAME (m) == DECL_NAME (method))
1042 insert_p = false;
1043 break;
1045 if (complete_p
1046 && !DECL_CONV_FN_P (m)
1047 && DECL_NAME (m) > DECL_NAME (method))
1048 break;
1051 current_fns = insert_p ? NULL_TREE : (*method_vec)[slot];
1053 /* Check to see if we've already got this method. */
1054 for (fns = current_fns; fns; fns = OVL_NEXT (fns))
1056 tree fn = OVL_CURRENT (fns);
1057 tree fn_type;
1058 tree method_type;
1059 tree parms1;
1060 tree parms2;
1062 if (TREE_CODE (fn) != TREE_CODE (method))
1063 continue;
1065 /* [over.load] Member function declarations with the
1066 same name and the same parameter types cannot be
1067 overloaded if any of them is a static member
1068 function declaration.
1070 [over.load] Member function declarations with the same name and
1071 the same parameter-type-list as well as member function template
1072 declarations with the same name, the same parameter-type-list, and
1073 the same template parameter lists cannot be overloaded if any of
1074 them, but not all, have a ref-qualifier.
1076 [namespace.udecl] When a using-declaration brings names
1077 from a base class into a derived class scope, member
1078 functions in the derived class override and/or hide member
1079 functions with the same name and parameter types in a base
1080 class (rather than conflicting). */
1081 fn_type = TREE_TYPE (fn);
1082 method_type = TREE_TYPE (method);
1083 parms1 = TYPE_ARG_TYPES (fn_type);
1084 parms2 = TYPE_ARG_TYPES (method_type);
1086 /* Compare the quals on the 'this' parm. Don't compare
1087 the whole types, as used functions are treated as
1088 coming from the using class in overload resolution. */
1089 if (! DECL_STATIC_FUNCTION_P (fn)
1090 && ! DECL_STATIC_FUNCTION_P (method)
1091 /* Either both or neither need to be ref-qualified for
1092 differing quals to allow overloading. */
1093 && (FUNCTION_REF_QUALIFIED (fn_type)
1094 == FUNCTION_REF_QUALIFIED (method_type))
1095 && (type_memfn_quals (fn_type) != type_memfn_quals (method_type)
1096 || type_memfn_rqual (fn_type) != type_memfn_rqual (method_type)))
1097 continue;
1099 /* For templates, the return type and template parameters
1100 must be identical. */
1101 if (TREE_CODE (fn) == TEMPLATE_DECL
1102 && (!same_type_p (TREE_TYPE (fn_type),
1103 TREE_TYPE (method_type))
1104 || !comp_template_parms (DECL_TEMPLATE_PARMS (fn),
1105 DECL_TEMPLATE_PARMS (method))))
1106 continue;
1108 if (! DECL_STATIC_FUNCTION_P (fn))
1109 parms1 = TREE_CHAIN (parms1);
1110 if (! DECL_STATIC_FUNCTION_P (method))
1111 parms2 = TREE_CHAIN (parms2);
1113 if (compparms (parms1, parms2)
1114 && (!DECL_CONV_FN_P (fn)
1115 || same_type_p (TREE_TYPE (fn_type),
1116 TREE_TYPE (method_type))))
1118 /* For function versions, their parms and types match
1119 but they are not duplicates. Record function versions
1120 as and when they are found. extern "C" functions are
1121 not treated as versions. */
1122 if (TREE_CODE (fn) == FUNCTION_DECL
1123 && TREE_CODE (method) == FUNCTION_DECL
1124 && !DECL_EXTERN_C_P (fn)
1125 && !DECL_EXTERN_C_P (method)
1126 && targetm.target_option.function_versions (fn, method))
1128 /* Mark functions as versions if necessary. Modify the mangled
1129 decl name if necessary. */
1130 if (!DECL_FUNCTION_VERSIONED (fn))
1132 DECL_FUNCTION_VERSIONED (fn) = 1;
1133 if (DECL_ASSEMBLER_NAME_SET_P (fn))
1134 mangle_decl (fn);
1136 if (!DECL_FUNCTION_VERSIONED (method))
1138 DECL_FUNCTION_VERSIONED (method) = 1;
1139 if (DECL_ASSEMBLER_NAME_SET_P (method))
1140 mangle_decl (method);
1142 record_function_versions (fn, method);
1143 continue;
1145 if (DECL_INHERITED_CTOR_BASE (method))
1147 if (DECL_INHERITED_CTOR_BASE (fn))
1149 error_at (DECL_SOURCE_LOCATION (method),
1150 "%q#D inherited from %qT", method,
1151 DECL_INHERITED_CTOR_BASE (method));
1152 error_at (DECL_SOURCE_LOCATION (fn),
1153 "conflicts with version inherited from %qT",
1154 DECL_INHERITED_CTOR_BASE (fn));
1156 /* Otherwise defer to the other function. */
1157 return false;
1159 if (using_decl)
1161 if (DECL_CONTEXT (fn) == type)
1162 /* Defer to the local function. */
1163 return false;
1165 else
1167 error ("%q+#D cannot be overloaded", method);
1168 error ("with %q+#D", fn);
1171 /* We don't call duplicate_decls here to merge the
1172 declarations because that will confuse things if the
1173 methods have inline definitions. In particular, we
1174 will crash while processing the definitions. */
1175 return false;
1179 /* A class should never have more than one destructor. */
1180 if (current_fns && DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (method))
1181 return false;
1183 /* Add the new binding. */
1184 if (using_decl)
1186 overload = ovl_cons (method, current_fns);
1187 OVL_USED (overload) = true;
1189 else
1190 overload = build_overload (method, current_fns);
1192 if (conv_p)
1193 TYPE_HAS_CONVERSION (type) = 1;
1194 else if (slot >= CLASSTYPE_FIRST_CONVERSION_SLOT && !complete_p)
1195 push_class_level_binding (DECL_NAME (method), overload);
1197 if (insert_p)
1199 bool reallocated;
1201 /* We only expect to add few methods in the COMPLETE_P case, so
1202 just make room for one more method in that case. */
1203 if (complete_p)
1204 reallocated = vec_safe_reserve_exact (method_vec, 1);
1205 else
1206 reallocated = vec_safe_reserve (method_vec, 1);
1207 if (reallocated)
1208 CLASSTYPE_METHOD_VEC (type) = method_vec;
1209 if (slot == method_vec->length ())
1210 method_vec->quick_push (overload);
1211 else
1212 method_vec->quick_insert (slot, overload);
1214 else
1215 /* Replace the current slot. */
1216 (*method_vec)[slot] = overload;
1217 return true;
1220 /* Subroutines of finish_struct. */
1222 /* Change the access of FDECL to ACCESS in T. Return 1 if change was
1223 legit, otherwise return 0. */
1225 static int
1226 alter_access (tree t, tree fdecl, tree access)
1228 tree elem;
1230 if (!DECL_LANG_SPECIFIC (fdecl))
1231 retrofit_lang_decl (fdecl);
1233 gcc_assert (!DECL_DISCRIMINATOR_P (fdecl));
1235 elem = purpose_member (t, DECL_ACCESS (fdecl));
1236 if (elem)
1238 if (TREE_VALUE (elem) != access)
1240 if (TREE_CODE (TREE_TYPE (fdecl)) == FUNCTION_DECL)
1241 error ("conflicting access specifications for method"
1242 " %q+D, ignored", TREE_TYPE (fdecl));
1243 else
1244 error ("conflicting access specifications for field %qE, ignored",
1245 DECL_NAME (fdecl));
1247 else
1249 /* They're changing the access to the same thing they changed
1250 it to before. That's OK. */
1254 else
1256 perform_or_defer_access_check (TYPE_BINFO (t), fdecl, fdecl,
1257 tf_warning_or_error);
1258 DECL_ACCESS (fdecl) = tree_cons (t, access, DECL_ACCESS (fdecl));
1259 return 1;
1261 return 0;
1264 /* Process the USING_DECL, which is a member of T. */
1266 static void
1267 handle_using_decl (tree using_decl, tree t)
1269 tree decl = USING_DECL_DECLS (using_decl);
1270 tree name = DECL_NAME (using_decl);
1271 tree access
1272 = TREE_PRIVATE (using_decl) ? access_private_node
1273 : TREE_PROTECTED (using_decl) ? access_protected_node
1274 : access_public_node;
1275 tree flist = NULL_TREE;
1276 tree old_value;
1278 gcc_assert (!processing_template_decl && decl);
1280 old_value = lookup_member (t, name, /*protect=*/0, /*want_type=*/false,
1281 tf_warning_or_error);
1282 if (old_value)
1284 if (is_overloaded_fn (old_value))
1285 old_value = OVL_CURRENT (old_value);
1287 if (DECL_P (old_value) && DECL_CONTEXT (old_value) == t)
1288 /* OK */;
1289 else
1290 old_value = NULL_TREE;
1293 cp_emit_debug_info_for_using (decl, USING_DECL_SCOPE (using_decl));
1295 if (is_overloaded_fn (decl))
1296 flist = decl;
1298 if (! old_value)
1300 else if (is_overloaded_fn (old_value))
1302 if (flist)
1303 /* It's OK to use functions from a base when there are functions with
1304 the same name already present in the current class. */;
1305 else
1307 error ("%q+D invalid in %q#T", using_decl, t);
1308 error (" because of local method %q+#D with same name",
1309 OVL_CURRENT (old_value));
1310 return;
1313 else if (!DECL_ARTIFICIAL (old_value))
1315 error ("%q+D invalid in %q#T", using_decl, t);
1316 error (" because of local member %q+#D with same name", old_value);
1317 return;
1320 /* Make type T see field decl FDECL with access ACCESS. */
1321 if (flist)
1322 for (; flist; flist = OVL_NEXT (flist))
1324 add_method (t, OVL_CURRENT (flist), using_decl);
1325 alter_access (t, OVL_CURRENT (flist), access);
1327 else
1328 alter_access (t, decl, access);
1331 /* walk_tree callback for check_abi_tags: if the type at *TP involves any
1332 types with abi tags, add the corresponding identifiers to the VEC in
1333 *DATA and set IDENTIFIER_MARKED. */
1335 struct abi_tag_data
1337 tree t;
1338 tree subob;
1341 static tree
1342 find_abi_tags_r (tree *tp, int */*walk_subtrees*/, void *data)
1344 if (!OVERLOAD_TYPE_P (*tp))
1345 return NULL_TREE;
1347 if (tree attributes = lookup_attribute ("abi_tag", TYPE_ATTRIBUTES (*tp)))
1349 struct abi_tag_data *p = static_cast<struct abi_tag_data*>(data);
1350 for (tree list = TREE_VALUE (attributes); list;
1351 list = TREE_CHAIN (list))
1353 tree tag = TREE_VALUE (list);
1354 tree id = get_identifier (TREE_STRING_POINTER (tag));
1355 if (!IDENTIFIER_MARKED (id))
1357 if (TYPE_P (p->subob))
1359 warning (OPT_Wabi_tag, "%qT does not have the %E abi tag "
1360 "that base %qT has", p->t, tag, p->subob);
1361 inform (location_of (p->subob), "%qT declared here",
1362 p->subob);
1364 else
1366 warning (OPT_Wabi_tag, "%qT does not have the %E abi tag "
1367 "that %qT (used in the type of %qD) has",
1368 p->t, tag, *tp, p->subob);
1369 inform (location_of (p->subob), "%qD declared here",
1370 p->subob);
1371 inform (location_of (*tp), "%qT declared here", *tp);
1376 return NULL_TREE;
1379 /* Set IDENTIFIER_MARKED on all the ABI tags on T and its (transitively
1380 complete) template arguments. */
1382 static void
1383 mark_type_abi_tags (tree t, bool val)
1385 tree attributes = lookup_attribute ("abi_tag", TYPE_ATTRIBUTES (t));
1386 if (attributes)
1388 for (tree list = TREE_VALUE (attributes); list;
1389 list = TREE_CHAIN (list))
1391 tree tag = TREE_VALUE (list);
1392 tree id = get_identifier (TREE_STRING_POINTER (tag));
1393 IDENTIFIER_MARKED (id) = val;
1397 /* Also mark ABI tags from template arguments. */
1398 if (CLASSTYPE_TEMPLATE_INFO (t))
1400 tree args = CLASSTYPE_TI_ARGS (t);
1401 for (int i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
1403 tree level = TMPL_ARGS_LEVEL (args, i+1);
1404 for (int j = 0; j < TREE_VEC_LENGTH (level); ++j)
1406 tree arg = TREE_VEC_ELT (level, j);
1407 if (CLASS_TYPE_P (arg))
1408 mark_type_abi_tags (arg, val);
1414 /* Check that class T has all the abi tags that subobject SUBOB has, or
1415 warn if not. */
1417 static void
1418 check_abi_tags (tree t, tree subob)
1420 mark_type_abi_tags (t, true);
1422 tree subtype = TYPE_P (subob) ? subob : TREE_TYPE (subob);
1423 struct abi_tag_data data = { t, subob };
1425 cp_walk_tree_without_duplicates (&subtype, find_abi_tags_r, &data);
1427 mark_type_abi_tags (t, false);
1430 /* Run through the base classes of T, updating CANT_HAVE_CONST_CTOR_P,
1431 and NO_CONST_ASN_REF_P. Also set flag bits in T based on
1432 properties of the bases. */
1434 static void
1435 check_bases (tree t,
1436 int* cant_have_const_ctor_p,
1437 int* no_const_asn_ref_p)
1439 int i;
1440 bool seen_non_virtual_nearly_empty_base_p = 0;
1441 int seen_tm_mask = 0;
1442 tree base_binfo;
1443 tree binfo;
1444 tree field = NULL_TREE;
1446 if (!CLASSTYPE_NON_STD_LAYOUT (t))
1447 for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
1448 if (TREE_CODE (field) == FIELD_DECL)
1449 break;
1451 for (binfo = TYPE_BINFO (t), i = 0;
1452 BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
1454 tree basetype = TREE_TYPE (base_binfo);
1456 gcc_assert (COMPLETE_TYPE_P (basetype));
1458 if (CLASSTYPE_FINAL (basetype))
1459 error ("cannot derive from %<final%> base %qT in derived type %qT",
1460 basetype, t);
1462 /* If any base class is non-literal, so is the derived class. */
1463 if (!CLASSTYPE_LITERAL_P (basetype))
1464 CLASSTYPE_LITERAL_P (t) = false;
1466 /* Effective C++ rule 14. We only need to check TYPE_POLYMORPHIC_P
1467 here because the case of virtual functions but non-virtual
1468 dtor is handled in finish_struct_1. */
1469 if (!TYPE_POLYMORPHIC_P (basetype))
1470 warning (OPT_Weffc__,
1471 "base class %q#T has a non-virtual destructor", basetype);
1473 /* If the base class doesn't have copy constructors or
1474 assignment operators that take const references, then the
1475 derived class cannot have such a member automatically
1476 generated. */
1477 if (TYPE_HAS_COPY_CTOR (basetype)
1478 && ! TYPE_HAS_CONST_COPY_CTOR (basetype))
1479 *cant_have_const_ctor_p = 1;
1480 if (TYPE_HAS_COPY_ASSIGN (basetype)
1481 && !TYPE_HAS_CONST_COPY_ASSIGN (basetype))
1482 *no_const_asn_ref_p = 1;
1484 if (BINFO_VIRTUAL_P (base_binfo))
1485 /* A virtual base does not effect nearly emptiness. */
1487 else if (CLASSTYPE_NEARLY_EMPTY_P (basetype))
1489 if (seen_non_virtual_nearly_empty_base_p)
1490 /* And if there is more than one nearly empty base, then the
1491 derived class is not nearly empty either. */
1492 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
1493 else
1494 /* Remember we've seen one. */
1495 seen_non_virtual_nearly_empty_base_p = 1;
1497 else if (!is_empty_class (basetype))
1498 /* If the base class is not empty or nearly empty, then this
1499 class cannot be nearly empty. */
1500 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
1502 /* A lot of properties from the bases also apply to the derived
1503 class. */
1504 TYPE_NEEDS_CONSTRUCTING (t) |= TYPE_NEEDS_CONSTRUCTING (basetype);
1505 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t)
1506 |= TYPE_HAS_NONTRIVIAL_DESTRUCTOR (basetype);
1507 TYPE_HAS_COMPLEX_COPY_ASSIGN (t)
1508 |= (TYPE_HAS_COMPLEX_COPY_ASSIGN (basetype)
1509 || !TYPE_HAS_COPY_ASSIGN (basetype));
1510 TYPE_HAS_COMPLEX_COPY_CTOR (t) |= (TYPE_HAS_COMPLEX_COPY_CTOR (basetype)
1511 || !TYPE_HAS_COPY_CTOR (basetype));
1512 TYPE_HAS_COMPLEX_MOVE_ASSIGN (t)
1513 |= TYPE_HAS_COMPLEX_MOVE_ASSIGN (basetype);
1514 TYPE_HAS_COMPLEX_MOVE_CTOR (t) |= TYPE_HAS_COMPLEX_MOVE_CTOR (basetype);
1515 TYPE_POLYMORPHIC_P (t) |= TYPE_POLYMORPHIC_P (basetype);
1516 CLASSTYPE_CONTAINS_EMPTY_CLASS_P (t)
1517 |= CLASSTYPE_CONTAINS_EMPTY_CLASS_P (basetype);
1518 TYPE_HAS_COMPLEX_DFLT (t) |= (!TYPE_HAS_DEFAULT_CONSTRUCTOR (basetype)
1519 || TYPE_HAS_COMPLEX_DFLT (basetype));
1521 /* A standard-layout class is a class that:
1523 * has no non-standard-layout base classes, */
1524 CLASSTYPE_NON_STD_LAYOUT (t) |= CLASSTYPE_NON_STD_LAYOUT (basetype);
1525 if (!CLASSTYPE_NON_STD_LAYOUT (t))
1527 tree basefield;
1528 /* ...has no base classes of the same type as the first non-static
1529 data member... */
1530 if (field && DECL_CONTEXT (field) == t
1531 && (same_type_ignoring_top_level_qualifiers_p
1532 (TREE_TYPE (field), basetype)))
1533 CLASSTYPE_NON_STD_LAYOUT (t) = 1;
1534 else
1535 /* ...either has no non-static data members in the most-derived
1536 class and at most one base class with non-static data
1537 members, or has no base classes with non-static data
1538 members */
1539 for (basefield = TYPE_FIELDS (basetype); basefield;
1540 basefield = DECL_CHAIN (basefield))
1541 if (TREE_CODE (basefield) == FIELD_DECL)
1543 if (field)
1544 CLASSTYPE_NON_STD_LAYOUT (t) = 1;
1545 else
1546 field = basefield;
1547 break;
1551 /* Don't bother collecting tm attributes if transactional memory
1552 support is not enabled. */
1553 if (flag_tm)
1555 tree tm_attr = find_tm_attribute (TYPE_ATTRIBUTES (basetype));
1556 if (tm_attr)
1557 seen_tm_mask |= tm_attr_to_mask (tm_attr);
1560 check_abi_tags (t, basetype);
1563 /* If one of the base classes had TM attributes, and the current class
1564 doesn't define its own, then the current class inherits one. */
1565 if (seen_tm_mask && !find_tm_attribute (TYPE_ATTRIBUTES (t)))
1567 tree tm_attr = tm_mask_to_attr (seen_tm_mask & -seen_tm_mask);
1568 TYPE_ATTRIBUTES (t) = tree_cons (tm_attr, NULL, TYPE_ATTRIBUTES (t));
1572 /* Determine all the primary bases within T. Sets BINFO_PRIMARY_BASE_P for
1573 those that are primaries. Sets BINFO_LOST_PRIMARY_P for those
1574 that have had a nearly-empty virtual primary base stolen by some
1575 other base in the hierarchy. Determines CLASSTYPE_PRIMARY_BASE for
1576 T. */
1578 static void
1579 determine_primary_bases (tree t)
1581 unsigned i;
1582 tree primary = NULL_TREE;
1583 tree type_binfo = TYPE_BINFO (t);
1584 tree base_binfo;
1586 /* Determine the primary bases of our bases. */
1587 for (base_binfo = TREE_CHAIN (type_binfo); base_binfo;
1588 base_binfo = TREE_CHAIN (base_binfo))
1590 tree primary = CLASSTYPE_PRIMARY_BINFO (BINFO_TYPE (base_binfo));
1592 /* See if we're the non-virtual primary of our inheritance
1593 chain. */
1594 if (!BINFO_VIRTUAL_P (base_binfo))
1596 tree parent = BINFO_INHERITANCE_CHAIN (base_binfo);
1597 tree parent_primary = CLASSTYPE_PRIMARY_BINFO (BINFO_TYPE (parent));
1599 if (parent_primary
1600 && SAME_BINFO_TYPE_P (BINFO_TYPE (base_binfo),
1601 BINFO_TYPE (parent_primary)))
1602 /* We are the primary binfo. */
1603 BINFO_PRIMARY_P (base_binfo) = 1;
1605 /* Determine if we have a virtual primary base, and mark it so.
1607 if (primary && BINFO_VIRTUAL_P (primary))
1609 tree this_primary = copied_binfo (primary, base_binfo);
1611 if (BINFO_PRIMARY_P (this_primary))
1612 /* Someone already claimed this base. */
1613 BINFO_LOST_PRIMARY_P (base_binfo) = 1;
1614 else
1616 tree delta;
1618 BINFO_PRIMARY_P (this_primary) = 1;
1619 BINFO_INHERITANCE_CHAIN (this_primary) = base_binfo;
1621 /* A virtual binfo might have been copied from within
1622 another hierarchy. As we're about to use it as a
1623 primary base, make sure the offsets match. */
1624 delta = size_diffop_loc (input_location,
1625 convert (ssizetype,
1626 BINFO_OFFSET (base_binfo)),
1627 convert (ssizetype,
1628 BINFO_OFFSET (this_primary)));
1630 propagate_binfo_offsets (this_primary, delta);
1635 /* First look for a dynamic direct non-virtual base. */
1636 for (i = 0; BINFO_BASE_ITERATE (type_binfo, i, base_binfo); i++)
1638 tree basetype = BINFO_TYPE (base_binfo);
1640 if (TYPE_CONTAINS_VPTR_P (basetype) && !BINFO_VIRTUAL_P (base_binfo))
1642 primary = base_binfo;
1643 goto found;
1647 /* A "nearly-empty" virtual base class can be the primary base
1648 class, if no non-virtual polymorphic base can be found. Look for
1649 a nearly-empty virtual dynamic base that is not already a primary
1650 base of something in the hierarchy. If there is no such base,
1651 just pick the first nearly-empty virtual base. */
1653 for (base_binfo = TREE_CHAIN (type_binfo); base_binfo;
1654 base_binfo = TREE_CHAIN (base_binfo))
1655 if (BINFO_VIRTUAL_P (base_binfo)
1656 && CLASSTYPE_NEARLY_EMPTY_P (BINFO_TYPE (base_binfo)))
1658 if (!BINFO_PRIMARY_P (base_binfo))
1660 /* Found one that is not primary. */
1661 primary = base_binfo;
1662 goto found;
1664 else if (!primary)
1665 /* Remember the first candidate. */
1666 primary = base_binfo;
1669 found:
1670 /* If we've got a primary base, use it. */
1671 if (primary)
1673 tree basetype = BINFO_TYPE (primary);
1675 CLASSTYPE_PRIMARY_BINFO (t) = primary;
1676 if (BINFO_PRIMARY_P (primary))
1677 /* We are stealing a primary base. */
1678 BINFO_LOST_PRIMARY_P (BINFO_INHERITANCE_CHAIN (primary)) = 1;
1679 BINFO_PRIMARY_P (primary) = 1;
1680 if (BINFO_VIRTUAL_P (primary))
1682 tree delta;
1684 BINFO_INHERITANCE_CHAIN (primary) = type_binfo;
1685 /* A virtual binfo might have been copied from within
1686 another hierarchy. As we're about to use it as a primary
1687 base, make sure the offsets match. */
1688 delta = size_diffop_loc (input_location, ssize_int (0),
1689 convert (ssizetype, BINFO_OFFSET (primary)));
1691 propagate_binfo_offsets (primary, delta);
1694 primary = TYPE_BINFO (basetype);
1696 TYPE_VFIELD (t) = TYPE_VFIELD (basetype);
1697 BINFO_VTABLE (type_binfo) = BINFO_VTABLE (primary);
1698 BINFO_VIRTUALS (type_binfo) = BINFO_VIRTUALS (primary);
1702 /* Update the variant types of T. */
1704 void
1705 fixup_type_variants (tree t)
1707 tree variants;
1709 if (!t)
1710 return;
1712 for (variants = TYPE_NEXT_VARIANT (t);
1713 variants;
1714 variants = TYPE_NEXT_VARIANT (variants))
1716 /* These fields are in the _TYPE part of the node, not in
1717 the TYPE_LANG_SPECIFIC component, so they are not shared. */
1718 TYPE_HAS_USER_CONSTRUCTOR (variants) = TYPE_HAS_USER_CONSTRUCTOR (t);
1719 TYPE_NEEDS_CONSTRUCTING (variants) = TYPE_NEEDS_CONSTRUCTING (t);
1720 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (variants)
1721 = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t);
1723 TYPE_POLYMORPHIC_P (variants) = TYPE_POLYMORPHIC_P (t);
1725 TYPE_BINFO (variants) = TYPE_BINFO (t);
1727 /* Copy whatever these are holding today. */
1728 TYPE_VFIELD (variants) = TYPE_VFIELD (t);
1729 TYPE_METHODS (variants) = TYPE_METHODS (t);
1730 TYPE_FIELDS (variants) = TYPE_FIELDS (t);
1734 /* Early variant fixups: we apply attributes at the beginning of the class
1735 definition, and we need to fix up any variants that have already been
1736 made via elaborated-type-specifier so that check_qualified_type works. */
1738 void
1739 fixup_attribute_variants (tree t)
1741 tree variants;
1743 if (!t)
1744 return;
1746 for (variants = TYPE_NEXT_VARIANT (t);
1747 variants;
1748 variants = TYPE_NEXT_VARIANT (variants))
1750 /* These are the two fields that check_qualified_type looks at and
1751 are affected by attributes. */
1752 TYPE_ATTRIBUTES (variants) = TYPE_ATTRIBUTES (t);
1753 TYPE_ALIGN (variants) = TYPE_ALIGN (t);
1757 /* Set memoizing fields and bits of T (and its variants) for later
1758 use. */
1760 static void
1761 finish_struct_bits (tree t)
1763 /* Fix up variants (if any). */
1764 fixup_type_variants (t);
1766 if (BINFO_N_BASE_BINFOS (TYPE_BINFO (t)) && TYPE_POLYMORPHIC_P (t))
1767 /* For a class w/o baseclasses, 'finish_struct' has set
1768 CLASSTYPE_PURE_VIRTUALS correctly (by definition).
1769 Similarly for a class whose base classes do not have vtables.
1770 When neither of these is true, we might have removed abstract
1771 virtuals (by providing a definition), added some (by declaring
1772 new ones), or redeclared ones from a base class. We need to
1773 recalculate what's really an abstract virtual at this point (by
1774 looking in the vtables). */
1775 get_pure_virtuals (t);
1777 /* If this type has a copy constructor or a destructor, force its
1778 mode to be BLKmode, and force its TREE_ADDRESSABLE bit to be
1779 nonzero. This will cause it to be passed by invisible reference
1780 and prevent it from being returned in a register. */
1781 if (type_has_nontrivial_copy_init (t)
1782 || TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t))
1784 tree variants;
1785 DECL_MODE (TYPE_MAIN_DECL (t)) = BLKmode;
1786 for (variants = t; variants; variants = TYPE_NEXT_VARIANT (variants))
1788 SET_TYPE_MODE (variants, BLKmode);
1789 TREE_ADDRESSABLE (variants) = 1;
1794 /* Issue warnings about T having private constructors, but no friends,
1795 and so forth.
1797 HAS_NONPRIVATE_METHOD is nonzero if T has any non-private methods or
1798 static members. HAS_NONPRIVATE_STATIC_FN is nonzero if T has any
1799 non-private static member functions. */
1801 static void
1802 maybe_warn_about_overly_private_class (tree t)
1804 int has_member_fn = 0;
1805 int has_nonprivate_method = 0;
1806 tree fn;
1808 if (!warn_ctor_dtor_privacy
1809 /* If the class has friends, those entities might create and
1810 access instances, so we should not warn. */
1811 || (CLASSTYPE_FRIEND_CLASSES (t)
1812 || DECL_FRIENDLIST (TYPE_MAIN_DECL (t)))
1813 /* We will have warned when the template was declared; there's
1814 no need to warn on every instantiation. */
1815 || CLASSTYPE_TEMPLATE_INSTANTIATION (t))
1816 /* There's no reason to even consider warning about this
1817 class. */
1818 return;
1820 /* We only issue one warning, if more than one applies, because
1821 otherwise, on code like:
1823 class A {
1824 // Oops - forgot `public:'
1825 A();
1826 A(const A&);
1827 ~A();
1830 we warn several times about essentially the same problem. */
1832 /* Check to see if all (non-constructor, non-destructor) member
1833 functions are private. (Since there are no friends or
1834 non-private statics, we can't ever call any of the private member
1835 functions.) */
1836 for (fn = TYPE_METHODS (t); fn; fn = DECL_CHAIN (fn))
1837 /* We're not interested in compiler-generated methods; they don't
1838 provide any way to call private members. */
1839 if (!DECL_ARTIFICIAL (fn))
1841 if (!TREE_PRIVATE (fn))
1843 if (DECL_STATIC_FUNCTION_P (fn))
1844 /* A non-private static member function is just like a
1845 friend; it can create and invoke private member
1846 functions, and be accessed without a class
1847 instance. */
1848 return;
1850 has_nonprivate_method = 1;
1851 /* Keep searching for a static member function. */
1853 else if (!DECL_CONSTRUCTOR_P (fn) && !DECL_DESTRUCTOR_P (fn))
1854 has_member_fn = 1;
1857 if (!has_nonprivate_method && has_member_fn)
1859 /* There are no non-private methods, and there's at least one
1860 private member function that isn't a constructor or
1861 destructor. (If all the private members are
1862 constructors/destructors we want to use the code below that
1863 issues error messages specifically referring to
1864 constructors/destructors.) */
1865 unsigned i;
1866 tree binfo = TYPE_BINFO (t);
1868 for (i = 0; i != BINFO_N_BASE_BINFOS (binfo); i++)
1869 if (BINFO_BASE_ACCESS (binfo, i) != access_private_node)
1871 has_nonprivate_method = 1;
1872 break;
1874 if (!has_nonprivate_method)
1876 warning (OPT_Wctor_dtor_privacy,
1877 "all member functions in class %qT are private", t);
1878 return;
1882 /* Even if some of the member functions are non-private, the class
1883 won't be useful for much if all the constructors or destructors
1884 are private: such an object can never be created or destroyed. */
1885 fn = CLASSTYPE_DESTRUCTORS (t);
1886 if (fn && TREE_PRIVATE (fn))
1888 warning (OPT_Wctor_dtor_privacy,
1889 "%q#T only defines a private destructor and has no friends",
1891 return;
1894 /* Warn about classes that have private constructors and no friends. */
1895 if (TYPE_HAS_USER_CONSTRUCTOR (t)
1896 /* Implicitly generated constructors are always public. */
1897 && (!CLASSTYPE_LAZY_DEFAULT_CTOR (t)
1898 || !CLASSTYPE_LAZY_COPY_CTOR (t)))
1900 int nonprivate_ctor = 0;
1902 /* If a non-template class does not define a copy
1903 constructor, one is defined for it, enabling it to avoid
1904 this warning. For a template class, this does not
1905 happen, and so we would normally get a warning on:
1907 template <class T> class C { private: C(); };
1909 To avoid this asymmetry, we check TYPE_HAS_COPY_CTOR. All
1910 complete non-template or fully instantiated classes have this
1911 flag set. */
1912 if (!TYPE_HAS_COPY_CTOR (t))
1913 nonprivate_ctor = 1;
1914 else
1915 for (fn = CLASSTYPE_CONSTRUCTORS (t); fn; fn = OVL_NEXT (fn))
1917 tree ctor = OVL_CURRENT (fn);
1918 /* Ideally, we wouldn't count copy constructors (or, in
1919 fact, any constructor that takes an argument of the
1920 class type as a parameter) because such things cannot
1921 be used to construct an instance of the class unless
1922 you already have one. But, for now at least, we're
1923 more generous. */
1924 if (! TREE_PRIVATE (ctor))
1926 nonprivate_ctor = 1;
1927 break;
1931 if (nonprivate_ctor == 0)
1933 warning (OPT_Wctor_dtor_privacy,
1934 "%q#T only defines private constructors and has no friends",
1936 return;
1941 static struct {
1942 gt_pointer_operator new_value;
1943 void *cookie;
1944 } resort_data;
1946 /* Comparison function to compare two TYPE_METHOD_VEC entries by name. */
1948 static int
1949 method_name_cmp (const void* m1_p, const void* m2_p)
1951 const tree *const m1 = (const tree *) m1_p;
1952 const tree *const m2 = (const tree *) m2_p;
1954 if (*m1 == NULL_TREE && *m2 == NULL_TREE)
1955 return 0;
1956 if (*m1 == NULL_TREE)
1957 return -1;
1958 if (*m2 == NULL_TREE)
1959 return 1;
1960 if (DECL_NAME (OVL_CURRENT (*m1)) < DECL_NAME (OVL_CURRENT (*m2)))
1961 return -1;
1962 return 1;
1965 /* This routine compares two fields like method_name_cmp but using the
1966 pointer operator in resort_field_decl_data. */
1968 static int
1969 resort_method_name_cmp (const void* m1_p, const void* m2_p)
1971 const tree *const m1 = (const tree *) m1_p;
1972 const tree *const m2 = (const tree *) m2_p;
1973 if (*m1 == NULL_TREE && *m2 == NULL_TREE)
1974 return 0;
1975 if (*m1 == NULL_TREE)
1976 return -1;
1977 if (*m2 == NULL_TREE)
1978 return 1;
1980 tree d1 = DECL_NAME (OVL_CURRENT (*m1));
1981 tree d2 = DECL_NAME (OVL_CURRENT (*m2));
1982 resort_data.new_value (&d1, resort_data.cookie);
1983 resort_data.new_value (&d2, resort_data.cookie);
1984 if (d1 < d2)
1985 return -1;
1987 return 1;
1990 /* Resort TYPE_METHOD_VEC because pointers have been reordered. */
1992 void
1993 resort_type_method_vec (void* obj,
1994 void* /*orig_obj*/,
1995 gt_pointer_operator new_value,
1996 void* cookie)
1998 vec<tree, va_gc> *method_vec = (vec<tree, va_gc> *) obj;
1999 int len = vec_safe_length (method_vec);
2000 size_t slot;
2001 tree fn;
2003 /* The type conversion ops have to live at the front of the vec, so we
2004 can't sort them. */
2005 for (slot = CLASSTYPE_FIRST_CONVERSION_SLOT;
2006 vec_safe_iterate (method_vec, slot, &fn);
2007 ++slot)
2008 if (!DECL_CONV_FN_P (OVL_CURRENT (fn)))
2009 break;
2011 if (len - slot > 1)
2013 resort_data.new_value = new_value;
2014 resort_data.cookie = cookie;
2015 qsort (method_vec->address () + slot, len - slot, sizeof (tree),
2016 resort_method_name_cmp);
2020 /* Warn about duplicate methods in fn_fields.
2022 Sort methods that are not special (i.e., constructors, destructors,
2023 and type conversion operators) so that we can find them faster in
2024 search. */
2026 static void
2027 finish_struct_methods (tree t)
2029 tree fn_fields;
2030 vec<tree, va_gc> *method_vec;
2031 int slot, len;
2033 method_vec = CLASSTYPE_METHOD_VEC (t);
2034 if (!method_vec)
2035 return;
2037 len = method_vec->length ();
2039 /* Clear DECL_IN_AGGR_P for all functions. */
2040 for (fn_fields = TYPE_METHODS (t); fn_fields;
2041 fn_fields = DECL_CHAIN (fn_fields))
2042 DECL_IN_AGGR_P (fn_fields) = 0;
2044 /* Issue warnings about private constructors and such. If there are
2045 no methods, then some public defaults are generated. */
2046 maybe_warn_about_overly_private_class (t);
2048 /* The type conversion ops have to live at the front of the vec, so we
2049 can't sort them. */
2050 for (slot = CLASSTYPE_FIRST_CONVERSION_SLOT;
2051 method_vec->iterate (slot, &fn_fields);
2052 ++slot)
2053 if (!DECL_CONV_FN_P (OVL_CURRENT (fn_fields)))
2054 break;
2055 if (len - slot > 1)
2056 qsort (method_vec->address () + slot,
2057 len-slot, sizeof (tree), method_name_cmp);
2060 /* Make BINFO's vtable have N entries, including RTTI entries,
2061 vbase and vcall offsets, etc. Set its type and call the back end
2062 to lay it out. */
2064 static void
2065 layout_vtable_decl (tree binfo, int n)
2067 tree atype;
2068 tree vtable;
2070 atype = build_array_of_n_type (vtable_entry_type, n);
2071 layout_type (atype);
2073 /* We may have to grow the vtable. */
2074 vtable = get_vtbl_decl_for_binfo (binfo);
2075 if (!same_type_p (TREE_TYPE (vtable), atype))
2077 TREE_TYPE (vtable) = atype;
2078 DECL_SIZE (vtable) = DECL_SIZE_UNIT (vtable) = NULL_TREE;
2079 layout_decl (vtable, 0);
2083 /* True iff FNDECL and BASE_FNDECL (both non-static member functions)
2084 have the same signature. */
2087 same_signature_p (const_tree fndecl, const_tree base_fndecl)
2089 /* One destructor overrides another if they are the same kind of
2090 destructor. */
2091 if (DECL_DESTRUCTOR_P (base_fndecl) && DECL_DESTRUCTOR_P (fndecl)
2092 && special_function_p (base_fndecl) == special_function_p (fndecl))
2093 return 1;
2094 /* But a non-destructor never overrides a destructor, nor vice
2095 versa, nor do different kinds of destructors override
2096 one-another. For example, a complete object destructor does not
2097 override a deleting destructor. */
2098 if (DECL_DESTRUCTOR_P (base_fndecl) || DECL_DESTRUCTOR_P (fndecl))
2099 return 0;
2101 if (DECL_NAME (fndecl) == DECL_NAME (base_fndecl)
2102 || (DECL_CONV_FN_P (fndecl)
2103 && DECL_CONV_FN_P (base_fndecl)
2104 && same_type_p (DECL_CONV_FN_TYPE (fndecl),
2105 DECL_CONV_FN_TYPE (base_fndecl))))
2107 tree fntype = TREE_TYPE (fndecl);
2108 tree base_fntype = TREE_TYPE (base_fndecl);
2109 if (type_memfn_quals (fntype) == type_memfn_quals (base_fntype)
2110 && type_memfn_rqual (fntype) == type_memfn_rqual (base_fntype)
2111 && compparms (FUNCTION_FIRST_USER_PARMTYPE (fndecl),
2112 FUNCTION_FIRST_USER_PARMTYPE (base_fndecl)))
2113 return 1;
2115 return 0;
2118 /* Returns TRUE if DERIVED is a binfo containing the binfo BASE as a
2119 subobject. */
2121 static bool
2122 base_derived_from (tree derived, tree base)
2124 tree probe;
2126 for (probe = base; probe; probe = BINFO_INHERITANCE_CHAIN (probe))
2128 if (probe == derived)
2129 return true;
2130 else if (BINFO_VIRTUAL_P (probe))
2131 /* If we meet a virtual base, we can't follow the inheritance
2132 any more. See if the complete type of DERIVED contains
2133 such a virtual base. */
2134 return (binfo_for_vbase (BINFO_TYPE (probe), BINFO_TYPE (derived))
2135 != NULL_TREE);
2137 return false;
2140 typedef struct find_final_overrider_data_s {
2141 /* The function for which we are trying to find a final overrider. */
2142 tree fn;
2143 /* The base class in which the function was declared. */
2144 tree declaring_base;
2145 /* The candidate overriders. */
2146 tree candidates;
2147 /* Path to most derived. */
2148 vec<tree> path;
2149 } find_final_overrider_data;
2151 /* Add the overrider along the current path to FFOD->CANDIDATES.
2152 Returns true if an overrider was found; false otherwise. */
2154 static bool
2155 dfs_find_final_overrider_1 (tree binfo,
2156 find_final_overrider_data *ffod,
2157 unsigned depth)
2159 tree method;
2161 /* If BINFO is not the most derived type, try a more derived class.
2162 A definition there will overrider a definition here. */
2163 if (depth)
2165 depth--;
2166 if (dfs_find_final_overrider_1
2167 (ffod->path[depth], ffod, depth))
2168 return true;
2171 method = look_for_overrides_here (BINFO_TYPE (binfo), ffod->fn);
2172 if (method)
2174 tree *candidate = &ffod->candidates;
2176 /* Remove any candidates overridden by this new function. */
2177 while (*candidate)
2179 /* If *CANDIDATE overrides METHOD, then METHOD
2180 cannot override anything else on the list. */
2181 if (base_derived_from (TREE_VALUE (*candidate), binfo))
2182 return true;
2183 /* If METHOD overrides *CANDIDATE, remove *CANDIDATE. */
2184 if (base_derived_from (binfo, TREE_VALUE (*candidate)))
2185 *candidate = TREE_CHAIN (*candidate);
2186 else
2187 candidate = &TREE_CHAIN (*candidate);
2190 /* Add the new function. */
2191 ffod->candidates = tree_cons (method, binfo, ffod->candidates);
2192 return true;
2195 return false;
2198 /* Called from find_final_overrider via dfs_walk. */
2200 static tree
2201 dfs_find_final_overrider_pre (tree binfo, void *data)
2203 find_final_overrider_data *ffod = (find_final_overrider_data *) data;
2205 if (binfo == ffod->declaring_base)
2206 dfs_find_final_overrider_1 (binfo, ffod, ffod->path.length ());
2207 ffod->path.safe_push (binfo);
2209 return NULL_TREE;
2212 static tree
2213 dfs_find_final_overrider_post (tree /*binfo*/, void *data)
2215 find_final_overrider_data *ffod = (find_final_overrider_data *) data;
2216 ffod->path.pop ();
2218 return NULL_TREE;
2221 /* Returns a TREE_LIST whose TREE_PURPOSE is the final overrider for
2222 FN and whose TREE_VALUE is the binfo for the base where the
2223 overriding occurs. BINFO (in the hierarchy dominated by the binfo
2224 DERIVED) is the base object in which FN is declared. */
2226 static tree
2227 find_final_overrider (tree derived, tree binfo, tree fn)
2229 find_final_overrider_data ffod;
2231 /* Getting this right is a little tricky. This is valid:
2233 struct S { virtual void f (); };
2234 struct T { virtual void f (); };
2235 struct U : public S, public T { };
2237 even though calling `f' in `U' is ambiguous. But,
2239 struct R { virtual void f(); };
2240 struct S : virtual public R { virtual void f (); };
2241 struct T : virtual public R { virtual void f (); };
2242 struct U : public S, public T { };
2244 is not -- there's no way to decide whether to put `S::f' or
2245 `T::f' in the vtable for `R'.
2247 The solution is to look at all paths to BINFO. If we find
2248 different overriders along any two, then there is a problem. */
2249 if (DECL_THUNK_P (fn))
2250 fn = THUNK_TARGET (fn);
2252 /* Determine the depth of the hierarchy. */
2253 ffod.fn = fn;
2254 ffod.declaring_base = binfo;
2255 ffod.candidates = NULL_TREE;
2256 ffod.path.create (30);
2258 dfs_walk_all (derived, dfs_find_final_overrider_pre,
2259 dfs_find_final_overrider_post, &ffod);
2261 ffod.path.release ();
2263 /* If there was no winner, issue an error message. */
2264 if (!ffod.candidates || TREE_CHAIN (ffod.candidates))
2265 return error_mark_node;
2267 return ffod.candidates;
2270 /* Return the index of the vcall offset for FN when TYPE is used as a
2271 virtual base. */
2273 static tree
2274 get_vcall_index (tree fn, tree type)
2276 vec<tree_pair_s, va_gc> *indices = CLASSTYPE_VCALL_INDICES (type);
2277 tree_pair_p p;
2278 unsigned ix;
2280 FOR_EACH_VEC_SAFE_ELT (indices, ix, p)
2281 if ((DECL_DESTRUCTOR_P (fn) && DECL_DESTRUCTOR_P (p->purpose))
2282 || same_signature_p (fn, p->purpose))
2283 return p->value;
2285 /* There should always be an appropriate index. */
2286 gcc_unreachable ();
2289 /* Update an entry in the vtable for BINFO, which is in the hierarchy
2290 dominated by T. FN is the old function; VIRTUALS points to the
2291 corresponding position in the new BINFO_VIRTUALS list. IX is the index
2292 of that entry in the list. */
2294 static void
2295 update_vtable_entry_for_fn (tree t, tree binfo, tree fn, tree* virtuals,
2296 unsigned ix)
2298 tree b;
2299 tree overrider;
2300 tree delta;
2301 tree virtual_base;
2302 tree first_defn;
2303 tree overrider_fn, overrider_target;
2304 tree target_fn = DECL_THUNK_P (fn) ? THUNK_TARGET (fn) : fn;
2305 tree over_return, base_return;
2306 bool lost = false;
2308 /* Find the nearest primary base (possibly binfo itself) which defines
2309 this function; this is the class the caller will convert to when
2310 calling FN through BINFO. */
2311 for (b = binfo; ; b = get_primary_binfo (b))
2313 gcc_assert (b);
2314 if (look_for_overrides_here (BINFO_TYPE (b), target_fn))
2315 break;
2317 /* The nearest definition is from a lost primary. */
2318 if (BINFO_LOST_PRIMARY_P (b))
2319 lost = true;
2321 first_defn = b;
2323 /* Find the final overrider. */
2324 overrider = find_final_overrider (TYPE_BINFO (t), b, target_fn);
2325 if (overrider == error_mark_node)
2327 error ("no unique final overrider for %qD in %qT", target_fn, t);
2328 return;
2330 overrider_target = overrider_fn = TREE_PURPOSE (overrider);
2332 /* Check for adjusting covariant return types. */
2333 over_return = TREE_TYPE (TREE_TYPE (overrider_target));
2334 base_return = TREE_TYPE (TREE_TYPE (target_fn));
2336 if (POINTER_TYPE_P (over_return)
2337 && TREE_CODE (over_return) == TREE_CODE (base_return)
2338 && CLASS_TYPE_P (TREE_TYPE (over_return))
2339 && CLASS_TYPE_P (TREE_TYPE (base_return))
2340 /* If the overrider is invalid, don't even try. */
2341 && !DECL_INVALID_OVERRIDER_P (overrider_target))
2343 /* If FN is a covariant thunk, we must figure out the adjustment
2344 to the final base FN was converting to. As OVERRIDER_TARGET might
2345 also be converting to the return type of FN, we have to
2346 combine the two conversions here. */
2347 tree fixed_offset, virtual_offset;
2349 over_return = TREE_TYPE (over_return);
2350 base_return = TREE_TYPE (base_return);
2352 if (DECL_THUNK_P (fn))
2354 gcc_assert (DECL_RESULT_THUNK_P (fn));
2355 fixed_offset = ssize_int (THUNK_FIXED_OFFSET (fn));
2356 virtual_offset = THUNK_VIRTUAL_OFFSET (fn);
2358 else
2359 fixed_offset = virtual_offset = NULL_TREE;
2361 if (virtual_offset)
2362 /* Find the equivalent binfo within the return type of the
2363 overriding function. We will want the vbase offset from
2364 there. */
2365 virtual_offset = binfo_for_vbase (BINFO_TYPE (virtual_offset),
2366 over_return);
2367 else if (!same_type_ignoring_top_level_qualifiers_p
2368 (over_return, base_return))
2370 /* There was no existing virtual thunk (which takes
2371 precedence). So find the binfo of the base function's
2372 return type within the overriding function's return type.
2373 We cannot call lookup base here, because we're inside a
2374 dfs_walk, and will therefore clobber the BINFO_MARKED
2375 flags. Fortunately we know the covariancy is valid (it
2376 has already been checked), so we can just iterate along
2377 the binfos, which have been chained in inheritance graph
2378 order. Of course it is lame that we have to repeat the
2379 search here anyway -- we should really be caching pieces
2380 of the vtable and avoiding this repeated work. */
2381 tree thunk_binfo, base_binfo;
2383 /* Find the base binfo within the overriding function's
2384 return type. We will always find a thunk_binfo, except
2385 when the covariancy is invalid (which we will have
2386 already diagnosed). */
2387 for (base_binfo = TYPE_BINFO (base_return),
2388 thunk_binfo = TYPE_BINFO (over_return);
2389 thunk_binfo;
2390 thunk_binfo = TREE_CHAIN (thunk_binfo))
2391 if (SAME_BINFO_TYPE_P (BINFO_TYPE (thunk_binfo),
2392 BINFO_TYPE (base_binfo)))
2393 break;
2395 /* See if virtual inheritance is involved. */
2396 for (virtual_offset = thunk_binfo;
2397 virtual_offset;
2398 virtual_offset = BINFO_INHERITANCE_CHAIN (virtual_offset))
2399 if (BINFO_VIRTUAL_P (virtual_offset))
2400 break;
2402 if (virtual_offset
2403 || (thunk_binfo && !BINFO_OFFSET_ZEROP (thunk_binfo)))
2405 tree offset = convert (ssizetype, BINFO_OFFSET (thunk_binfo));
2407 if (virtual_offset)
2409 /* We convert via virtual base. Adjust the fixed
2410 offset to be from there. */
2411 offset =
2412 size_diffop (offset,
2413 convert (ssizetype,
2414 BINFO_OFFSET (virtual_offset)));
2416 if (fixed_offset)
2417 /* There was an existing fixed offset, this must be
2418 from the base just converted to, and the base the
2419 FN was thunking to. */
2420 fixed_offset = size_binop (PLUS_EXPR, fixed_offset, offset);
2421 else
2422 fixed_offset = offset;
2426 if (fixed_offset || virtual_offset)
2427 /* Replace the overriding function with a covariant thunk. We
2428 will emit the overriding function in its own slot as
2429 well. */
2430 overrider_fn = make_thunk (overrider_target, /*this_adjusting=*/0,
2431 fixed_offset, virtual_offset);
2433 else
2434 gcc_assert (DECL_INVALID_OVERRIDER_P (overrider_target) ||
2435 !DECL_THUNK_P (fn));
2437 /* If we need a covariant thunk, then we may need to adjust first_defn.
2438 The ABI specifies that the thunks emitted with a function are
2439 determined by which bases the function overrides, so we need to be
2440 sure that we're using a thunk for some overridden base; even if we
2441 know that the necessary this adjustment is zero, there may not be an
2442 appropriate zero-this-adjusment thunk for us to use since thunks for
2443 overriding virtual bases always use the vcall offset.
2445 Furthermore, just choosing any base that overrides this function isn't
2446 quite right, as this slot won't be used for calls through a type that
2447 puts a covariant thunk here. Calling the function through such a type
2448 will use a different slot, and that slot is the one that determines
2449 the thunk emitted for that base.
2451 So, keep looking until we find the base that we're really overriding
2452 in this slot: the nearest primary base that doesn't use a covariant
2453 thunk in this slot. */
2454 if (overrider_target != overrider_fn)
2456 if (BINFO_TYPE (b) == DECL_CONTEXT (overrider_target))
2457 /* We already know that the overrider needs a covariant thunk. */
2458 b = get_primary_binfo (b);
2459 for (; ; b = get_primary_binfo (b))
2461 tree main_binfo = TYPE_BINFO (BINFO_TYPE (b));
2462 tree bv = chain_index (ix, BINFO_VIRTUALS (main_binfo));
2463 if (!DECL_THUNK_P (TREE_VALUE (bv)))
2464 break;
2465 if (BINFO_LOST_PRIMARY_P (b))
2466 lost = true;
2468 first_defn = b;
2471 /* Assume that we will produce a thunk that convert all the way to
2472 the final overrider, and not to an intermediate virtual base. */
2473 virtual_base = NULL_TREE;
2475 /* See if we can convert to an intermediate virtual base first, and then
2476 use the vcall offset located there to finish the conversion. */
2477 for (; b; b = BINFO_INHERITANCE_CHAIN (b))
2479 /* If we find the final overrider, then we can stop
2480 walking. */
2481 if (SAME_BINFO_TYPE_P (BINFO_TYPE (b),
2482 BINFO_TYPE (TREE_VALUE (overrider))))
2483 break;
2485 /* If we find a virtual base, and we haven't yet found the
2486 overrider, then there is a virtual base between the
2487 declaring base (first_defn) and the final overrider. */
2488 if (BINFO_VIRTUAL_P (b))
2490 virtual_base = b;
2491 break;
2495 /* Compute the constant adjustment to the `this' pointer. The
2496 `this' pointer, when this function is called, will point at BINFO
2497 (or one of its primary bases, which are at the same offset). */
2498 if (virtual_base)
2499 /* The `this' pointer needs to be adjusted from the declaration to
2500 the nearest virtual base. */
2501 delta = size_diffop_loc (input_location,
2502 convert (ssizetype, BINFO_OFFSET (virtual_base)),
2503 convert (ssizetype, BINFO_OFFSET (first_defn)));
2504 else if (lost)
2505 /* If the nearest definition is in a lost primary, we don't need an
2506 entry in our vtable. Except possibly in a constructor vtable,
2507 if we happen to get our primary back. In that case, the offset
2508 will be zero, as it will be a primary base. */
2509 delta = size_zero_node;
2510 else
2511 /* The `this' pointer needs to be adjusted from pointing to
2512 BINFO to pointing at the base where the final overrider
2513 appears. */
2514 delta = size_diffop_loc (input_location,
2515 convert (ssizetype,
2516 BINFO_OFFSET (TREE_VALUE (overrider))),
2517 convert (ssizetype, BINFO_OFFSET (binfo)));
2519 modify_vtable_entry (t, binfo, overrider_fn, delta, virtuals);
2521 if (virtual_base)
2522 BV_VCALL_INDEX (*virtuals)
2523 = get_vcall_index (overrider_target, BINFO_TYPE (virtual_base));
2524 else
2525 BV_VCALL_INDEX (*virtuals) = NULL_TREE;
2527 BV_LOST_PRIMARY (*virtuals) = lost;
2530 /* Called from modify_all_vtables via dfs_walk. */
2532 static tree
2533 dfs_modify_vtables (tree binfo, void* data)
2535 tree t = (tree) data;
2536 tree virtuals;
2537 tree old_virtuals;
2538 unsigned ix;
2540 if (!TYPE_CONTAINS_VPTR_P (BINFO_TYPE (binfo)))
2541 /* A base without a vtable needs no modification, and its bases
2542 are uninteresting. */
2543 return dfs_skip_bases;
2545 if (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), t)
2546 && !CLASSTYPE_HAS_PRIMARY_BASE_P (t))
2547 /* Don't do the primary vtable, if it's new. */
2548 return NULL_TREE;
2550 if (BINFO_PRIMARY_P (binfo) && !BINFO_VIRTUAL_P (binfo))
2551 /* There's no need to modify the vtable for a non-virtual primary
2552 base; we're not going to use that vtable anyhow. We do still
2553 need to do this for virtual primary bases, as they could become
2554 non-primary in a construction vtable. */
2555 return NULL_TREE;
2557 make_new_vtable (t, binfo);
2559 /* Now, go through each of the virtual functions in the virtual
2560 function table for BINFO. Find the final overrider, and update
2561 the BINFO_VIRTUALS list appropriately. */
2562 for (ix = 0, virtuals = BINFO_VIRTUALS (binfo),
2563 old_virtuals = BINFO_VIRTUALS (TYPE_BINFO (BINFO_TYPE (binfo)));
2564 virtuals;
2565 ix++, virtuals = TREE_CHAIN (virtuals),
2566 old_virtuals = TREE_CHAIN (old_virtuals))
2567 update_vtable_entry_for_fn (t,
2568 binfo,
2569 BV_FN (old_virtuals),
2570 &virtuals, ix);
2572 return NULL_TREE;
2575 /* Update all of the primary and secondary vtables for T. Create new
2576 vtables as required, and initialize their RTTI information. Each
2577 of the functions in VIRTUALS is declared in T and may override a
2578 virtual function from a base class; find and modify the appropriate
2579 entries to point to the overriding functions. Returns a list, in
2580 declaration order, of the virtual functions that are declared in T,
2581 but do not appear in the primary base class vtable, and which
2582 should therefore be appended to the end of the vtable for T. */
2584 static tree
2585 modify_all_vtables (tree t, tree virtuals)
2587 tree binfo = TYPE_BINFO (t);
2588 tree *fnsp;
2590 /* Mangle the vtable name before entering dfs_walk (c++/51884). */
2591 if (TYPE_CONTAINS_VPTR_P (t))
2592 get_vtable_decl (t, false);
2594 /* Update all of the vtables. */
2595 dfs_walk_once (binfo, dfs_modify_vtables, NULL, t);
2597 /* Add virtual functions not already in our primary vtable. These
2598 will be both those introduced by this class, and those overridden
2599 from secondary bases. It does not include virtuals merely
2600 inherited from secondary bases. */
2601 for (fnsp = &virtuals; *fnsp; )
2603 tree fn = TREE_VALUE (*fnsp);
2605 if (!value_member (fn, BINFO_VIRTUALS (binfo))
2606 || DECL_VINDEX (fn) == error_mark_node)
2608 /* We don't need to adjust the `this' pointer when
2609 calling this function. */
2610 BV_DELTA (*fnsp) = integer_zero_node;
2611 BV_VCALL_INDEX (*fnsp) = NULL_TREE;
2613 /* This is a function not already in our vtable. Keep it. */
2614 fnsp = &TREE_CHAIN (*fnsp);
2616 else
2617 /* We've already got an entry for this function. Skip it. */
2618 *fnsp = TREE_CHAIN (*fnsp);
2621 return virtuals;
2624 /* Get the base virtual function declarations in T that have the
2625 indicated NAME. */
2627 static tree
2628 get_basefndecls (tree name, tree t)
2630 tree methods;
2631 tree base_fndecls = NULL_TREE;
2632 int n_baseclasses = BINFO_N_BASE_BINFOS (TYPE_BINFO (t));
2633 int i;
2635 /* Find virtual functions in T with the indicated NAME. */
2636 i = lookup_fnfields_1 (t, name);
2637 if (i != -1)
2638 for (methods = (*CLASSTYPE_METHOD_VEC (t))[i];
2639 methods;
2640 methods = OVL_NEXT (methods))
2642 tree method = OVL_CURRENT (methods);
2644 if (TREE_CODE (method) == FUNCTION_DECL
2645 && DECL_VINDEX (method))
2646 base_fndecls = tree_cons (NULL_TREE, method, base_fndecls);
2649 if (base_fndecls)
2650 return base_fndecls;
2652 for (i = 0; i < n_baseclasses; i++)
2654 tree basetype = BINFO_TYPE (BINFO_BASE_BINFO (TYPE_BINFO (t), i));
2655 base_fndecls = chainon (get_basefndecls (name, basetype),
2656 base_fndecls);
2659 return base_fndecls;
2662 /* If this declaration supersedes the declaration of
2663 a method declared virtual in the base class, then
2664 mark this field as being virtual as well. */
2666 void
2667 check_for_override (tree decl, tree ctype)
2669 bool overrides_found = false;
2670 if (TREE_CODE (decl) == TEMPLATE_DECL)
2671 /* In [temp.mem] we have:
2673 A specialization of a member function template does not
2674 override a virtual function from a base class. */
2675 return;
2676 if ((DECL_DESTRUCTOR_P (decl)
2677 || IDENTIFIER_VIRTUAL_P (DECL_NAME (decl))
2678 || DECL_CONV_FN_P (decl))
2679 && look_for_overrides (ctype, decl)
2680 && !DECL_STATIC_FUNCTION_P (decl))
2681 /* Set DECL_VINDEX to a value that is neither an INTEGER_CST nor
2682 the error_mark_node so that we know it is an overriding
2683 function. */
2685 DECL_VINDEX (decl) = decl;
2686 overrides_found = true;
2689 if (DECL_VIRTUAL_P (decl))
2691 if (!DECL_VINDEX (decl))
2692 DECL_VINDEX (decl) = error_mark_node;
2693 IDENTIFIER_VIRTUAL_P (DECL_NAME (decl)) = 1;
2694 if (DECL_DESTRUCTOR_P (decl))
2695 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (ctype) = true;
2697 else if (DECL_FINAL_P (decl))
2698 error ("%q+#D marked final, but is not virtual", decl);
2699 if (DECL_OVERRIDE_P (decl) && !overrides_found)
2700 error ("%q+#D marked override, but does not override", decl);
2703 /* Warn about hidden virtual functions that are not overridden in t.
2704 We know that constructors and destructors don't apply. */
2706 static void
2707 warn_hidden (tree t)
2709 vec<tree, va_gc> *method_vec = CLASSTYPE_METHOD_VEC (t);
2710 tree fns;
2711 size_t i;
2713 /* We go through each separately named virtual function. */
2714 for (i = CLASSTYPE_FIRST_CONVERSION_SLOT;
2715 vec_safe_iterate (method_vec, i, &fns);
2716 ++i)
2718 tree fn;
2719 tree name;
2720 tree fndecl;
2721 tree base_fndecls;
2722 tree base_binfo;
2723 tree binfo;
2724 int j;
2726 /* All functions in this slot in the CLASSTYPE_METHOD_VEC will
2727 have the same name. Figure out what name that is. */
2728 name = DECL_NAME (OVL_CURRENT (fns));
2729 /* There are no possibly hidden functions yet. */
2730 base_fndecls = NULL_TREE;
2731 /* Iterate through all of the base classes looking for possibly
2732 hidden functions. */
2733 for (binfo = TYPE_BINFO (t), j = 0;
2734 BINFO_BASE_ITERATE (binfo, j, base_binfo); j++)
2736 tree basetype = BINFO_TYPE (base_binfo);
2737 base_fndecls = chainon (get_basefndecls (name, basetype),
2738 base_fndecls);
2741 /* If there are no functions to hide, continue. */
2742 if (!base_fndecls)
2743 continue;
2745 /* Remove any overridden functions. */
2746 for (fn = fns; fn; fn = OVL_NEXT (fn))
2748 fndecl = OVL_CURRENT (fn);
2749 if (DECL_VINDEX (fndecl))
2751 tree *prev = &base_fndecls;
2753 while (*prev)
2754 /* If the method from the base class has the same
2755 signature as the method from the derived class, it
2756 has been overridden. */
2757 if (same_signature_p (fndecl, TREE_VALUE (*prev)))
2758 *prev = TREE_CHAIN (*prev);
2759 else
2760 prev = &TREE_CHAIN (*prev);
2764 /* Now give a warning for all base functions without overriders,
2765 as they are hidden. */
2766 while (base_fndecls)
2768 /* Here we know it is a hider, and no overrider exists. */
2769 warning (OPT_Woverloaded_virtual, "%q+D was hidden", TREE_VALUE (base_fndecls));
2770 warning (OPT_Woverloaded_virtual, " by %q+D", fns);
2771 base_fndecls = TREE_CHAIN (base_fndecls);
2776 /* Recursive helper for finish_struct_anon. */
2778 static void
2779 finish_struct_anon_r (tree field, bool complain)
2781 bool is_union = TREE_CODE (TREE_TYPE (field)) == UNION_TYPE;
2782 tree elt = TYPE_FIELDS (TREE_TYPE (field));
2783 for (; elt; elt = DECL_CHAIN (elt))
2785 /* We're generally only interested in entities the user
2786 declared, but we also find nested classes by noticing
2787 the TYPE_DECL that we create implicitly. You're
2788 allowed to put one anonymous union inside another,
2789 though, so we explicitly tolerate that. We use
2790 TYPE_ANONYMOUS_P rather than ANON_AGGR_TYPE_P so that
2791 we also allow unnamed types used for defining fields. */
2792 if (DECL_ARTIFICIAL (elt)
2793 && (!DECL_IMPLICIT_TYPEDEF_P (elt)
2794 || TYPE_ANONYMOUS_P (TREE_TYPE (elt))))
2795 continue;
2797 if (TREE_CODE (elt) != FIELD_DECL)
2799 if (complain)
2801 if (is_union)
2802 permerror (input_location,
2803 "%q+#D invalid; an anonymous union can "
2804 "only have non-static data members", elt);
2805 else
2806 permerror (input_location,
2807 "%q+#D invalid; an anonymous struct can "
2808 "only have non-static data members", elt);
2810 continue;
2813 if (complain)
2815 if (TREE_PRIVATE (elt))
2817 if (is_union)
2818 permerror (input_location,
2819 "private member %q+#D in anonymous union", elt);
2820 else
2821 permerror (input_location,
2822 "private member %q+#D in anonymous struct", elt);
2824 else if (TREE_PROTECTED (elt))
2826 if (is_union)
2827 permerror (input_location,
2828 "protected member %q+#D in anonymous union", elt);
2829 else
2830 permerror (input_location,
2831 "protected member %q+#D in anonymous struct", elt);
2835 TREE_PRIVATE (elt) = TREE_PRIVATE (field);
2836 TREE_PROTECTED (elt) = TREE_PROTECTED (field);
2838 /* Recurse into the anonymous aggregates to handle correctly
2839 access control (c++/24926):
2841 class A {
2842 union {
2843 union {
2844 int i;
2849 int j=A().i; */
2850 if (DECL_NAME (elt) == NULL_TREE
2851 && ANON_AGGR_TYPE_P (TREE_TYPE (elt)))
2852 finish_struct_anon_r (elt, /*complain=*/false);
2856 /* Check for things that are invalid. There are probably plenty of other
2857 things we should check for also. */
2859 static void
2860 finish_struct_anon (tree t)
2862 for (tree field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
2864 if (TREE_STATIC (field))
2865 continue;
2866 if (TREE_CODE (field) != FIELD_DECL)
2867 continue;
2869 if (DECL_NAME (field) == NULL_TREE
2870 && ANON_AGGR_TYPE_P (TREE_TYPE (field)))
2871 finish_struct_anon_r (field, /*complain=*/true);
2875 /* Add T to CLASSTYPE_DECL_LIST of current_class_type which
2876 will be used later during class template instantiation.
2877 When FRIEND_P is zero, T can be a static member data (VAR_DECL),
2878 a non-static member data (FIELD_DECL), a member function
2879 (FUNCTION_DECL), a nested type (RECORD_TYPE, ENUM_TYPE),
2880 a typedef (TYPE_DECL) or a member class template (TEMPLATE_DECL)
2881 When FRIEND_P is nonzero, T is either a friend class
2882 (RECORD_TYPE, TEMPLATE_DECL) or a friend function
2883 (FUNCTION_DECL, TEMPLATE_DECL). */
2885 void
2886 maybe_add_class_template_decl_list (tree type, tree t, int friend_p)
2888 /* Save some memory by not creating TREE_LIST if TYPE is not template. */
2889 if (CLASSTYPE_TEMPLATE_INFO (type))
2890 CLASSTYPE_DECL_LIST (type)
2891 = tree_cons (friend_p ? NULL_TREE : type,
2892 t, CLASSTYPE_DECL_LIST (type));
2895 /* This function is called from declare_virt_assop_and_dtor via
2896 dfs_walk_all.
2898 DATA is a type that direcly or indirectly inherits the base
2899 represented by BINFO. If BINFO contains a virtual assignment [copy
2900 assignment or move assigment] operator or a virtual constructor,
2901 declare that function in DATA if it hasn't been already declared. */
2903 static tree
2904 dfs_declare_virt_assop_and_dtor (tree binfo, void *data)
2906 tree bv, fn, t = (tree)data;
2907 tree opname = ansi_assopname (NOP_EXPR);
2909 gcc_assert (t && CLASS_TYPE_P (t));
2910 gcc_assert (binfo && TREE_CODE (binfo) == TREE_BINFO);
2912 if (!TYPE_CONTAINS_VPTR_P (BINFO_TYPE (binfo)))
2913 /* A base without a vtable needs no modification, and its bases
2914 are uninteresting. */
2915 return dfs_skip_bases;
2917 if (BINFO_PRIMARY_P (binfo))
2918 /* If this is a primary base, then we have already looked at the
2919 virtual functions of its vtable. */
2920 return NULL_TREE;
2922 for (bv = BINFO_VIRTUALS (binfo); bv; bv = TREE_CHAIN (bv))
2924 fn = BV_FN (bv);
2926 if (DECL_NAME (fn) == opname)
2928 if (CLASSTYPE_LAZY_COPY_ASSIGN (t))
2929 lazily_declare_fn (sfk_copy_assignment, t);
2930 if (CLASSTYPE_LAZY_MOVE_ASSIGN (t))
2931 lazily_declare_fn (sfk_move_assignment, t);
2933 else if (DECL_DESTRUCTOR_P (fn)
2934 && CLASSTYPE_LAZY_DESTRUCTOR (t))
2935 lazily_declare_fn (sfk_destructor, t);
2938 return NULL_TREE;
2941 /* If the class type T has a direct or indirect base that contains a
2942 virtual assignment operator or a virtual destructor, declare that
2943 function in T if it hasn't been already declared. */
2945 static void
2946 declare_virt_assop_and_dtor (tree t)
2948 if (!(TYPE_POLYMORPHIC_P (t)
2949 && (CLASSTYPE_LAZY_COPY_ASSIGN (t)
2950 || CLASSTYPE_LAZY_MOVE_ASSIGN (t)
2951 || CLASSTYPE_LAZY_DESTRUCTOR (t))))
2952 return;
2954 dfs_walk_all (TYPE_BINFO (t),
2955 dfs_declare_virt_assop_and_dtor,
2956 NULL, t);
2959 /* Declare the inheriting constructor for class T inherited from base
2960 constructor CTOR with the parameter array PARMS of size NPARMS. */
2962 static void
2963 one_inheriting_sig (tree t, tree ctor, tree *parms, int nparms)
2965 /* We don't declare an inheriting ctor that would be a default,
2966 copy or move ctor for derived or base. */
2967 if (nparms == 0)
2968 return;
2969 if (nparms == 1
2970 && TREE_CODE (parms[0]) == REFERENCE_TYPE)
2972 tree parm = TYPE_MAIN_VARIANT (TREE_TYPE (parms[0]));
2973 if (parm == t || parm == DECL_CONTEXT (ctor))
2974 return;
2977 tree parmlist = void_list_node;
2978 for (int i = nparms - 1; i >= 0; i--)
2979 parmlist = tree_cons (NULL_TREE, parms[i], parmlist);
2980 tree fn = implicitly_declare_fn (sfk_inheriting_constructor,
2981 t, false, ctor, parmlist);
2982 if (add_method (t, fn, NULL_TREE))
2984 DECL_CHAIN (fn) = TYPE_METHODS (t);
2985 TYPE_METHODS (t) = fn;
2989 /* Declare all the inheriting constructors for class T inherited from base
2990 constructor CTOR. */
2992 static void
2993 one_inherited_ctor (tree ctor, tree t)
2995 tree parms = FUNCTION_FIRST_USER_PARMTYPE (ctor);
2997 tree *new_parms = XALLOCAVEC (tree, list_length (parms));
2998 int i = 0;
2999 for (; parms && parms != void_list_node; parms = TREE_CHAIN (parms))
3001 if (TREE_PURPOSE (parms))
3002 one_inheriting_sig (t, ctor, new_parms, i);
3003 new_parms[i++] = TREE_VALUE (parms);
3005 one_inheriting_sig (t, ctor, new_parms, i);
3006 if (parms == NULL_TREE)
3008 warning (OPT_Winherited_variadic_ctor,
3009 "the ellipsis in %qD is not inherited", ctor);
3010 inform (DECL_SOURCE_LOCATION (ctor), "%qD declared here", ctor);
3014 /* Create default constructors, assignment operators, and so forth for
3015 the type indicated by T, if they are needed. CANT_HAVE_CONST_CTOR,
3016 and CANT_HAVE_CONST_ASSIGNMENT are nonzero if, for whatever reason,
3017 the class cannot have a default constructor, copy constructor
3018 taking a const reference argument, or an assignment operator taking
3019 a const reference, respectively. */
3021 static void
3022 add_implicitly_declared_members (tree t, tree* access_decls,
3023 int cant_have_const_cctor,
3024 int cant_have_const_assignment)
3026 bool move_ok = false;
3028 if (cxx_dialect >= cxx11 && !CLASSTYPE_DESTRUCTORS (t)
3029 && !TYPE_HAS_COPY_CTOR (t) && !TYPE_HAS_COPY_ASSIGN (t)
3030 && !type_has_move_constructor (t) && !type_has_move_assign (t))
3031 move_ok = true;
3033 /* Destructor. */
3034 if (!CLASSTYPE_DESTRUCTORS (t))
3036 /* In general, we create destructors lazily. */
3037 CLASSTYPE_LAZY_DESTRUCTOR (t) = 1;
3039 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t)
3040 && TYPE_FOR_JAVA (t))
3041 /* But if this is a Java class, any non-trivial destructor is
3042 invalid, even if compiler-generated. Therefore, if the
3043 destructor is non-trivial we create it now. */
3044 lazily_declare_fn (sfk_destructor, t);
3047 /* [class.ctor]
3049 If there is no user-declared constructor for a class, a default
3050 constructor is implicitly declared. */
3051 if (! TYPE_HAS_USER_CONSTRUCTOR (t))
3053 TYPE_HAS_DEFAULT_CONSTRUCTOR (t) = 1;
3054 CLASSTYPE_LAZY_DEFAULT_CTOR (t) = 1;
3055 if (cxx_dialect >= cxx11)
3056 TYPE_HAS_CONSTEXPR_CTOR (t)
3057 /* This might force the declaration. */
3058 = type_has_constexpr_default_constructor (t);
3061 /* [class.ctor]
3063 If a class definition does not explicitly declare a copy
3064 constructor, one is declared implicitly. */
3065 if (! TYPE_HAS_COPY_CTOR (t) && ! TYPE_FOR_JAVA (t))
3067 TYPE_HAS_COPY_CTOR (t) = 1;
3068 TYPE_HAS_CONST_COPY_CTOR (t) = !cant_have_const_cctor;
3069 CLASSTYPE_LAZY_COPY_CTOR (t) = 1;
3070 if (move_ok)
3071 CLASSTYPE_LAZY_MOVE_CTOR (t) = 1;
3074 /* If there is no assignment operator, one will be created if and
3075 when it is needed. For now, just record whether or not the type
3076 of the parameter to the assignment operator will be a const or
3077 non-const reference. */
3078 if (!TYPE_HAS_COPY_ASSIGN (t) && !TYPE_FOR_JAVA (t))
3080 TYPE_HAS_COPY_ASSIGN (t) = 1;
3081 TYPE_HAS_CONST_COPY_ASSIGN (t) = !cant_have_const_assignment;
3082 CLASSTYPE_LAZY_COPY_ASSIGN (t) = 1;
3083 if (move_ok)
3084 CLASSTYPE_LAZY_MOVE_ASSIGN (t) = 1;
3087 /* We can't be lazy about declaring functions that might override
3088 a virtual function from a base class. */
3089 declare_virt_assop_and_dtor (t);
3091 while (*access_decls)
3093 tree using_decl = TREE_VALUE (*access_decls);
3094 tree decl = USING_DECL_DECLS (using_decl);
3095 if (DECL_NAME (using_decl) == ctor_identifier)
3097 /* declare, then remove the decl */
3098 tree ctor_list = decl;
3099 location_t loc = input_location;
3100 input_location = DECL_SOURCE_LOCATION (using_decl);
3101 if (ctor_list)
3102 for (; ctor_list; ctor_list = OVL_NEXT (ctor_list))
3103 one_inherited_ctor (OVL_CURRENT (ctor_list), t);
3104 *access_decls = TREE_CHAIN (*access_decls);
3105 input_location = loc;
3107 else
3108 access_decls = &TREE_CHAIN (*access_decls);
3112 /* Subroutine of insert_into_classtype_sorted_fields. Recursively
3113 count the number of fields in TYPE, including anonymous union
3114 members. */
3116 static int
3117 count_fields (tree fields)
3119 tree x;
3120 int n_fields = 0;
3121 for (x = fields; x; x = DECL_CHAIN (x))
3123 if (TREE_CODE (x) == FIELD_DECL && ANON_AGGR_TYPE_P (TREE_TYPE (x)))
3124 n_fields += count_fields (TYPE_FIELDS (TREE_TYPE (x)));
3125 else
3126 n_fields += 1;
3128 return n_fields;
3131 /* Subroutine of insert_into_classtype_sorted_fields. Recursively add
3132 all the fields in the TREE_LIST FIELDS to the SORTED_FIELDS_TYPE
3133 elts, starting at offset IDX. */
3135 static int
3136 add_fields_to_record_type (tree fields, struct sorted_fields_type *field_vec, int idx)
3138 tree x;
3139 for (x = fields; x; x = DECL_CHAIN (x))
3141 if (TREE_CODE (x) == FIELD_DECL && ANON_AGGR_TYPE_P (TREE_TYPE (x)))
3142 idx = add_fields_to_record_type (TYPE_FIELDS (TREE_TYPE (x)), field_vec, idx);
3143 else
3144 field_vec->elts[idx++] = x;
3146 return idx;
3149 /* Add all of the enum values of ENUMTYPE, to the FIELD_VEC elts,
3150 starting at offset IDX. */
3152 static int
3153 add_enum_fields_to_record_type (tree enumtype,
3154 struct sorted_fields_type *field_vec,
3155 int idx)
3157 tree values;
3158 for (values = TYPE_VALUES (enumtype); values; values = TREE_CHAIN (values))
3159 field_vec->elts[idx++] = TREE_VALUE (values);
3160 return idx;
3163 /* FIELD is a bit-field. We are finishing the processing for its
3164 enclosing type. Issue any appropriate messages and set appropriate
3165 flags. Returns false if an error has been diagnosed. */
3167 static bool
3168 check_bitfield_decl (tree field)
3170 tree type = TREE_TYPE (field);
3171 tree w;
3173 /* Extract the declared width of the bitfield, which has been
3174 temporarily stashed in DECL_INITIAL. */
3175 w = DECL_INITIAL (field);
3176 gcc_assert (w != NULL_TREE);
3177 /* Remove the bit-field width indicator so that the rest of the
3178 compiler does not treat that value as an initializer. */
3179 DECL_INITIAL (field) = NULL_TREE;
3181 /* Detect invalid bit-field type. */
3182 if (!INTEGRAL_OR_ENUMERATION_TYPE_P (type))
3184 error ("bit-field %q+#D with non-integral type", field);
3185 w = error_mark_node;
3187 else
3189 location_t loc = input_location;
3190 /* Avoid the non_lvalue wrapper added by fold for PLUS_EXPRs. */
3191 STRIP_NOPS (w);
3193 /* detect invalid field size. */
3194 input_location = DECL_SOURCE_LOCATION (field);
3195 w = cxx_constant_value (w);
3196 input_location = loc;
3198 if (TREE_CODE (w) != INTEGER_CST)
3200 error ("bit-field %q+D width not an integer constant", field);
3201 w = error_mark_node;
3203 else if (tree_int_cst_sgn (w) < 0)
3205 error ("negative width in bit-field %q+D", field);
3206 w = error_mark_node;
3208 else if (integer_zerop (w) && DECL_NAME (field) != 0)
3210 error ("zero width for bit-field %q+D", field);
3211 w = error_mark_node;
3213 else if ((TREE_CODE (type) != ENUMERAL_TYPE
3214 && TREE_CODE (type) != BOOLEAN_TYPE
3215 && compare_tree_int (w, TYPE_PRECISION (type)) > 0)
3216 || ((TREE_CODE (type) == ENUMERAL_TYPE
3217 || TREE_CODE (type) == BOOLEAN_TYPE)
3218 && tree_int_cst_lt (TYPE_SIZE (type), w)))
3219 warning (0, "width of %q+D exceeds its type", field);
3220 else if (TREE_CODE (type) == ENUMERAL_TYPE
3221 && (0 > (compare_tree_int
3222 (w, TYPE_PRECISION (ENUM_UNDERLYING_TYPE (type))))))
3223 warning (0, "%q+D is too small to hold all values of %q#T", field, type);
3226 if (w != error_mark_node)
3228 DECL_SIZE (field) = convert (bitsizetype, w);
3229 DECL_BIT_FIELD (field) = 1;
3230 return true;
3232 else
3234 /* Non-bit-fields are aligned for their type. */
3235 DECL_BIT_FIELD (field) = 0;
3236 CLEAR_DECL_C_BIT_FIELD (field);
3237 return false;
3241 /* FIELD is a non bit-field. We are finishing the processing for its
3242 enclosing type T. Issue any appropriate messages and set appropriate
3243 flags. */
3245 static void
3246 check_field_decl (tree field,
3247 tree t,
3248 int* cant_have_const_ctor,
3249 int* no_const_asn_ref,
3250 int* any_default_members)
3252 tree type = strip_array_types (TREE_TYPE (field));
3254 /* In C++98 an anonymous union cannot contain any fields which would change
3255 the settings of CANT_HAVE_CONST_CTOR and friends. */
3256 if (ANON_UNION_TYPE_P (type) && cxx_dialect < cxx11)
3258 /* And, we don't set TYPE_HAS_CONST_COPY_CTOR, etc., for anonymous
3259 structs. So, we recurse through their fields here. */
3260 else if (ANON_AGGR_TYPE_P (type))
3262 tree fields;
3264 for (fields = TYPE_FIELDS (type); fields; fields = DECL_CHAIN (fields))
3265 if (TREE_CODE (fields) == FIELD_DECL && !DECL_C_BIT_FIELD (field))
3266 check_field_decl (fields, t, cant_have_const_ctor,
3267 no_const_asn_ref, any_default_members);
3269 /* Check members with class type for constructors, destructors,
3270 etc. */
3271 else if (CLASS_TYPE_P (type))
3273 /* Never let anything with uninheritable virtuals
3274 make it through without complaint. */
3275 abstract_virtuals_error (field, type);
3277 if (TREE_CODE (t) == UNION_TYPE && cxx_dialect < cxx11)
3279 static bool warned;
3280 int oldcount = errorcount;
3281 if (TYPE_NEEDS_CONSTRUCTING (type))
3282 error ("member %q+#D with constructor not allowed in union",
3283 field);
3284 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
3285 error ("member %q+#D with destructor not allowed in union", field);
3286 if (TYPE_HAS_COMPLEX_COPY_ASSIGN (type))
3287 error ("member %q+#D with copy assignment operator not allowed in union",
3288 field);
3289 if (!warned && errorcount > oldcount)
3291 inform (DECL_SOURCE_LOCATION (field), "unrestricted unions "
3292 "only available with -std=c++11 or -std=gnu++11");
3293 warned = true;
3296 else
3298 TYPE_NEEDS_CONSTRUCTING (t) |= TYPE_NEEDS_CONSTRUCTING (type);
3299 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t)
3300 |= TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type);
3301 TYPE_HAS_COMPLEX_COPY_ASSIGN (t)
3302 |= (TYPE_HAS_COMPLEX_COPY_ASSIGN (type)
3303 || !TYPE_HAS_COPY_ASSIGN (type));
3304 TYPE_HAS_COMPLEX_COPY_CTOR (t) |= (TYPE_HAS_COMPLEX_COPY_CTOR (type)
3305 || !TYPE_HAS_COPY_CTOR (type));
3306 TYPE_HAS_COMPLEX_MOVE_ASSIGN (t) |= TYPE_HAS_COMPLEX_MOVE_ASSIGN (type);
3307 TYPE_HAS_COMPLEX_MOVE_CTOR (t) |= TYPE_HAS_COMPLEX_MOVE_CTOR (type);
3308 TYPE_HAS_COMPLEX_DFLT (t) |= (!TYPE_HAS_DEFAULT_CONSTRUCTOR (type)
3309 || TYPE_HAS_COMPLEX_DFLT (type));
3312 if (TYPE_HAS_COPY_CTOR (type)
3313 && !TYPE_HAS_CONST_COPY_CTOR (type))
3314 *cant_have_const_ctor = 1;
3316 if (TYPE_HAS_COPY_ASSIGN (type)
3317 && !TYPE_HAS_CONST_COPY_ASSIGN (type))
3318 *no_const_asn_ref = 1;
3321 check_abi_tags (t, field);
3323 if (DECL_INITIAL (field) != NULL_TREE)
3325 /* `build_class_init_list' does not recognize
3326 non-FIELD_DECLs. */
3327 if (TREE_CODE (t) == UNION_TYPE && *any_default_members != 0)
3328 error ("multiple fields in union %qT initialized", t);
3329 *any_default_members = 1;
3333 /* Check the data members (both static and non-static), class-scoped
3334 typedefs, etc., appearing in the declaration of T. Issue
3335 appropriate diagnostics. Sets ACCESS_DECLS to a list (in
3336 declaration order) of access declarations; each TREE_VALUE in this
3337 list is a USING_DECL.
3339 In addition, set the following flags:
3341 EMPTY_P
3342 The class is empty, i.e., contains no non-static data members.
3344 CANT_HAVE_CONST_CTOR_P
3345 This class cannot have an implicitly generated copy constructor
3346 taking a const reference.
3348 CANT_HAVE_CONST_ASN_REF
3349 This class cannot have an implicitly generated assignment
3350 operator taking a const reference.
3352 All of these flags should be initialized before calling this
3353 function.
3355 Returns a pointer to the end of the TYPE_FIELDs chain; additional
3356 fields can be added by adding to this chain. */
3358 static void
3359 check_field_decls (tree t, tree *access_decls,
3360 int *cant_have_const_ctor_p,
3361 int *no_const_asn_ref_p)
3363 tree *field;
3364 tree *next;
3365 bool has_pointers;
3366 int any_default_members;
3367 int cant_pack = 0;
3368 int field_access = -1;
3370 /* Assume there are no access declarations. */
3371 *access_decls = NULL_TREE;
3372 /* Assume this class has no pointer members. */
3373 has_pointers = false;
3374 /* Assume none of the members of this class have default
3375 initializations. */
3376 any_default_members = 0;
3378 for (field = &TYPE_FIELDS (t); *field; field = next)
3380 tree x = *field;
3381 tree type = TREE_TYPE (x);
3382 int this_field_access;
3384 next = &DECL_CHAIN (x);
3386 if (TREE_CODE (x) == USING_DECL)
3388 /* Save the access declarations for our caller. */
3389 *access_decls = tree_cons (NULL_TREE, x, *access_decls);
3390 continue;
3393 if (TREE_CODE (x) == TYPE_DECL
3394 || TREE_CODE (x) == TEMPLATE_DECL)
3395 continue;
3397 /* If we've gotten this far, it's a data member, possibly static,
3398 or an enumerator. */
3399 if (TREE_CODE (x) != CONST_DECL)
3400 DECL_CONTEXT (x) = t;
3402 /* When this goes into scope, it will be a non-local reference. */
3403 DECL_NONLOCAL (x) = 1;
3405 if (TREE_CODE (t) == UNION_TYPE)
3407 /* [class.union]
3409 If a union contains a static data member, or a member of
3410 reference type, the program is ill-formed. */
3411 if (VAR_P (x))
3413 error ("%q+D may not be static because it is a member of a union", x);
3414 continue;
3416 if (TREE_CODE (type) == REFERENCE_TYPE)
3418 error ("%q+D may not have reference type %qT because"
3419 " it is a member of a union",
3420 x, type);
3421 continue;
3425 /* Perform error checking that did not get done in
3426 grokdeclarator. */
3427 if (TREE_CODE (type) == FUNCTION_TYPE)
3429 error ("field %q+D invalidly declared function type", x);
3430 type = build_pointer_type (type);
3431 TREE_TYPE (x) = type;
3433 else if (TREE_CODE (type) == METHOD_TYPE)
3435 error ("field %q+D invalidly declared method type", x);
3436 type = build_pointer_type (type);
3437 TREE_TYPE (x) = type;
3440 if (type == error_mark_node)
3441 continue;
3443 if (TREE_CODE (x) == CONST_DECL || VAR_P (x))
3444 continue;
3446 /* Now it can only be a FIELD_DECL. */
3448 if (TREE_PRIVATE (x) || TREE_PROTECTED (x))
3449 CLASSTYPE_NON_AGGREGATE (t) = 1;
3451 /* If at least one non-static data member is non-literal, the whole
3452 class becomes non-literal. Note: if the type is incomplete we
3453 will complain later on. */
3454 if (COMPLETE_TYPE_P (type) && !literal_type_p (type))
3455 CLASSTYPE_LITERAL_P (t) = false;
3457 /* A standard-layout class is a class that:
3459 has the same access control (Clause 11) for all non-static data members,
3460 ... */
3461 this_field_access = TREE_PROTECTED (x) ? 1 : TREE_PRIVATE (x) ? 2 : 0;
3462 if (field_access == -1)
3463 field_access = this_field_access;
3464 else if (this_field_access != field_access)
3465 CLASSTYPE_NON_STD_LAYOUT (t) = 1;
3467 /* If this is of reference type, check if it needs an init. */
3468 if (TREE_CODE (type) == REFERENCE_TYPE)
3470 CLASSTYPE_NON_LAYOUT_POD_P (t) = 1;
3471 CLASSTYPE_NON_STD_LAYOUT (t) = 1;
3472 if (DECL_INITIAL (x) == NULL_TREE)
3473 SET_CLASSTYPE_REF_FIELDS_NEED_INIT (t, 1);
3475 /* ARM $12.6.2: [A member initializer list] (or, for an
3476 aggregate, initialization by a brace-enclosed list) is the
3477 only way to initialize nonstatic const and reference
3478 members. */
3479 TYPE_HAS_COMPLEX_COPY_ASSIGN (t) = 1;
3480 TYPE_HAS_COMPLEX_MOVE_ASSIGN (t) = 1;
3483 type = strip_array_types (type);
3485 if (TYPE_PACKED (t))
3487 if (!layout_pod_type_p (type) && !TYPE_PACKED (type))
3489 warning
3491 "ignoring packed attribute because of unpacked non-POD field %q+#D",
3493 cant_pack = 1;
3495 else if (DECL_C_BIT_FIELD (x)
3496 || TYPE_ALIGN (TREE_TYPE (x)) > BITS_PER_UNIT)
3497 DECL_PACKED (x) = 1;
3500 if (DECL_C_BIT_FIELD (x) && integer_zerop (DECL_INITIAL (x)))
3501 /* We don't treat zero-width bitfields as making a class
3502 non-empty. */
3504 else
3506 /* The class is non-empty. */
3507 CLASSTYPE_EMPTY_P (t) = 0;
3508 /* The class is not even nearly empty. */
3509 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
3510 /* If one of the data members contains an empty class,
3511 so does T. */
3512 if (CLASS_TYPE_P (type)
3513 && CLASSTYPE_CONTAINS_EMPTY_CLASS_P (type))
3514 CLASSTYPE_CONTAINS_EMPTY_CLASS_P (t) = 1;
3517 /* This is used by -Weffc++ (see below). Warn only for pointers
3518 to members which might hold dynamic memory. So do not warn
3519 for pointers to functions or pointers to members. */
3520 if (TYPE_PTR_P (type)
3521 && !TYPE_PTRFN_P (type))
3522 has_pointers = true;
3524 if (CLASS_TYPE_P (type))
3526 if (CLASSTYPE_REF_FIELDS_NEED_INIT (type))
3527 SET_CLASSTYPE_REF_FIELDS_NEED_INIT (t, 1);
3528 if (CLASSTYPE_READONLY_FIELDS_NEED_INIT (type))
3529 SET_CLASSTYPE_READONLY_FIELDS_NEED_INIT (t, 1);
3532 if (DECL_MUTABLE_P (x) || TYPE_HAS_MUTABLE_P (type))
3533 CLASSTYPE_HAS_MUTABLE (t) = 1;
3535 if (DECL_MUTABLE_P (x))
3537 if (CP_TYPE_CONST_P (type))
3539 error ("member %q+D cannot be declared both %<const%> "
3540 "and %<mutable%>", x);
3541 continue;
3543 if (TREE_CODE (type) == REFERENCE_TYPE)
3545 error ("member %q+D cannot be declared as a %<mutable%> "
3546 "reference", x);
3547 continue;
3551 if (! layout_pod_type_p (type))
3552 /* DR 148 now allows pointers to members (which are POD themselves),
3553 to be allowed in POD structs. */
3554 CLASSTYPE_NON_LAYOUT_POD_P (t) = 1;
3556 if (!std_layout_type_p (type))
3557 CLASSTYPE_NON_STD_LAYOUT (t) = 1;
3559 if (! zero_init_p (type))
3560 CLASSTYPE_NON_ZERO_INIT_P (t) = 1;
3562 /* We set DECL_C_BIT_FIELD in grokbitfield.
3563 If the type and width are valid, we'll also set DECL_BIT_FIELD. */
3564 if (! DECL_C_BIT_FIELD (x) || ! check_bitfield_decl (x))
3565 check_field_decl (x, t,
3566 cant_have_const_ctor_p,
3567 no_const_asn_ref_p,
3568 &any_default_members);
3570 /* Now that we've removed bit-field widths from DECL_INITIAL,
3571 anything left in DECL_INITIAL is an NSDMI that makes the class
3572 non-aggregate. */
3573 if (DECL_INITIAL (x))
3574 CLASSTYPE_NON_AGGREGATE (t) = true;
3576 /* If any field is const, the structure type is pseudo-const. */
3577 if (CP_TYPE_CONST_P (type))
3579 C_TYPE_FIELDS_READONLY (t) = 1;
3580 if (DECL_INITIAL (x) == NULL_TREE)
3581 SET_CLASSTYPE_READONLY_FIELDS_NEED_INIT (t, 1);
3583 /* ARM $12.6.2: [A member initializer list] (or, for an
3584 aggregate, initialization by a brace-enclosed list) is the
3585 only way to initialize nonstatic const and reference
3586 members. */
3587 TYPE_HAS_COMPLEX_COPY_ASSIGN (t) = 1;
3588 TYPE_HAS_COMPLEX_MOVE_ASSIGN (t) = 1;
3590 /* A field that is pseudo-const makes the structure likewise. */
3591 else if (CLASS_TYPE_P (type))
3593 C_TYPE_FIELDS_READONLY (t) |= C_TYPE_FIELDS_READONLY (type);
3594 SET_CLASSTYPE_READONLY_FIELDS_NEED_INIT (t,
3595 CLASSTYPE_READONLY_FIELDS_NEED_INIT (t)
3596 | CLASSTYPE_READONLY_FIELDS_NEED_INIT (type));
3599 /* Core issue 80: A nonstatic data member is required to have a
3600 different name from the class iff the class has a
3601 user-declared constructor. */
3602 if (constructor_name_p (DECL_NAME (x), t)
3603 && TYPE_HAS_USER_CONSTRUCTOR (t))
3604 permerror (input_location, "field %q+#D with same name as class", x);
3607 /* Effective C++ rule 11: if a class has dynamic memory held by pointers,
3608 it should also define a copy constructor and an assignment operator to
3609 implement the correct copy semantic (deep vs shallow, etc.). As it is
3610 not feasible to check whether the constructors do allocate dynamic memory
3611 and store it within members, we approximate the warning like this:
3613 -- Warn only if there are members which are pointers
3614 -- Warn only if there is a non-trivial constructor (otherwise,
3615 there cannot be memory allocated).
3616 -- Warn only if there is a non-trivial destructor. We assume that the
3617 user at least implemented the cleanup correctly, and a destructor
3618 is needed to free dynamic memory.
3620 This seems enough for practical purposes. */
3621 if (warn_ecpp
3622 && has_pointers
3623 && TYPE_HAS_USER_CONSTRUCTOR (t)
3624 && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t)
3625 && !(TYPE_HAS_COPY_CTOR (t) && TYPE_HAS_COPY_ASSIGN (t)))
3627 warning (OPT_Weffc__, "%q#T has pointer data members", t);
3629 if (! TYPE_HAS_COPY_CTOR (t))
3631 warning (OPT_Weffc__,
3632 " but does not override %<%T(const %T&)%>", t, t);
3633 if (!TYPE_HAS_COPY_ASSIGN (t))
3634 warning (OPT_Weffc__, " or %<operator=(const %T&)%>", t);
3636 else if (! TYPE_HAS_COPY_ASSIGN (t))
3637 warning (OPT_Weffc__,
3638 " but does not override %<operator=(const %T&)%>", t);
3641 /* Non-static data member initializers make the default constructor
3642 non-trivial. */
3643 if (any_default_members)
3645 TYPE_NEEDS_CONSTRUCTING (t) = true;
3646 TYPE_HAS_COMPLEX_DFLT (t) = true;
3649 /* If any of the fields couldn't be packed, unset TYPE_PACKED. */
3650 if (cant_pack)
3651 TYPE_PACKED (t) = 0;
3653 /* Check anonymous struct/anonymous union fields. */
3654 finish_struct_anon (t);
3656 /* We've built up the list of access declarations in reverse order.
3657 Fix that now. */
3658 *access_decls = nreverse (*access_decls);
3661 /* If TYPE is an empty class type, records its OFFSET in the table of
3662 OFFSETS. */
3664 static int
3665 record_subobject_offset (tree type, tree offset, splay_tree offsets)
3667 splay_tree_node n;
3669 if (!is_empty_class (type))
3670 return 0;
3672 /* Record the location of this empty object in OFFSETS. */
3673 n = splay_tree_lookup (offsets, (splay_tree_key) offset);
3674 if (!n)
3675 n = splay_tree_insert (offsets,
3676 (splay_tree_key) offset,
3677 (splay_tree_value) NULL_TREE);
3678 n->value = ((splay_tree_value)
3679 tree_cons (NULL_TREE,
3680 type,
3681 (tree) n->value));
3683 return 0;
3686 /* Returns nonzero if TYPE is an empty class type and there is
3687 already an entry in OFFSETS for the same TYPE as the same OFFSET. */
3689 static int
3690 check_subobject_offset (tree type, tree offset, splay_tree offsets)
3692 splay_tree_node n;
3693 tree t;
3695 if (!is_empty_class (type))
3696 return 0;
3698 /* Record the location of this empty object in OFFSETS. */
3699 n = splay_tree_lookup (offsets, (splay_tree_key) offset);
3700 if (!n)
3701 return 0;
3703 for (t = (tree) n->value; t; t = TREE_CHAIN (t))
3704 if (same_type_p (TREE_VALUE (t), type))
3705 return 1;
3707 return 0;
3710 /* Walk through all the subobjects of TYPE (located at OFFSET). Call
3711 F for every subobject, passing it the type, offset, and table of
3712 OFFSETS. If VBASES_P is one, then virtual non-primary bases should
3713 be traversed.
3715 If MAX_OFFSET is non-NULL, then subobjects with an offset greater
3716 than MAX_OFFSET will not be walked.
3718 If F returns a nonzero value, the traversal ceases, and that value
3719 is returned. Otherwise, returns zero. */
3721 static int
3722 walk_subobject_offsets (tree type,
3723 subobject_offset_fn f,
3724 tree offset,
3725 splay_tree offsets,
3726 tree max_offset,
3727 int vbases_p)
3729 int r = 0;
3730 tree type_binfo = NULL_TREE;
3732 /* If this OFFSET is bigger than the MAX_OFFSET, then we should
3733 stop. */
3734 if (max_offset && INT_CST_LT (max_offset, offset))
3735 return 0;
3737 if (type == error_mark_node)
3738 return 0;
3740 if (!TYPE_P (type))
3742 if (abi_version_at_least (2))
3743 type_binfo = type;
3744 type = BINFO_TYPE (type);
3747 if (CLASS_TYPE_P (type))
3749 tree field;
3750 tree binfo;
3751 int i;
3753 /* Avoid recursing into objects that are not interesting. */
3754 if (!CLASSTYPE_CONTAINS_EMPTY_CLASS_P (type))
3755 return 0;
3757 /* Record the location of TYPE. */
3758 r = (*f) (type, offset, offsets);
3759 if (r)
3760 return r;
3762 /* Iterate through the direct base classes of TYPE. */
3763 if (!type_binfo)
3764 type_binfo = TYPE_BINFO (type);
3765 for (i = 0; BINFO_BASE_ITERATE (type_binfo, i, binfo); i++)
3767 tree binfo_offset;
3769 if (abi_version_at_least (2)
3770 && BINFO_VIRTUAL_P (binfo))
3771 continue;
3773 if (!vbases_p
3774 && BINFO_VIRTUAL_P (binfo)
3775 && !BINFO_PRIMARY_P (binfo))
3776 continue;
3778 if (!abi_version_at_least (2))
3779 binfo_offset = size_binop (PLUS_EXPR,
3780 offset,
3781 BINFO_OFFSET (binfo));
3782 else
3784 tree orig_binfo;
3785 /* We cannot rely on BINFO_OFFSET being set for the base
3786 class yet, but the offsets for direct non-virtual
3787 bases can be calculated by going back to the TYPE. */
3788 orig_binfo = BINFO_BASE_BINFO (TYPE_BINFO (type), i);
3789 binfo_offset = size_binop (PLUS_EXPR,
3790 offset,
3791 BINFO_OFFSET (orig_binfo));
3794 r = walk_subobject_offsets (binfo,
3796 binfo_offset,
3797 offsets,
3798 max_offset,
3799 (abi_version_at_least (2)
3800 ? /*vbases_p=*/0 : vbases_p));
3801 if (r)
3802 return r;
3805 if (abi_version_at_least (2) && CLASSTYPE_VBASECLASSES (type))
3807 unsigned ix;
3808 vec<tree, va_gc> *vbases;
3810 /* Iterate through the virtual base classes of TYPE. In G++
3811 3.2, we included virtual bases in the direct base class
3812 loop above, which results in incorrect results; the
3813 correct offsets for virtual bases are only known when
3814 working with the most derived type. */
3815 if (vbases_p)
3816 for (vbases = CLASSTYPE_VBASECLASSES (type), ix = 0;
3817 vec_safe_iterate (vbases, ix, &binfo); ix++)
3819 r = walk_subobject_offsets (binfo,
3821 size_binop (PLUS_EXPR,
3822 offset,
3823 BINFO_OFFSET (binfo)),
3824 offsets,
3825 max_offset,
3826 /*vbases_p=*/0);
3827 if (r)
3828 return r;
3830 else
3832 /* We still have to walk the primary base, if it is
3833 virtual. (If it is non-virtual, then it was walked
3834 above.) */
3835 tree vbase = get_primary_binfo (type_binfo);
3837 if (vbase && BINFO_VIRTUAL_P (vbase)
3838 && BINFO_PRIMARY_P (vbase)
3839 && BINFO_INHERITANCE_CHAIN (vbase) == type_binfo)
3841 r = (walk_subobject_offsets
3842 (vbase, f, offset,
3843 offsets, max_offset, /*vbases_p=*/0));
3844 if (r)
3845 return r;
3850 /* Iterate through the fields of TYPE. */
3851 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
3852 if (TREE_CODE (field) == FIELD_DECL
3853 && TREE_TYPE (field) != error_mark_node
3854 && !DECL_ARTIFICIAL (field))
3856 tree field_offset;
3858 if (abi_version_at_least (2))
3859 field_offset = byte_position (field);
3860 else
3861 /* In G++ 3.2, DECL_FIELD_OFFSET was used. */
3862 field_offset = DECL_FIELD_OFFSET (field);
3864 r = walk_subobject_offsets (TREE_TYPE (field),
3866 size_binop (PLUS_EXPR,
3867 offset,
3868 field_offset),
3869 offsets,
3870 max_offset,
3871 /*vbases_p=*/1);
3872 if (r)
3873 return r;
3876 else if (TREE_CODE (type) == ARRAY_TYPE)
3878 tree element_type = strip_array_types (type);
3879 tree domain = TYPE_DOMAIN (type);
3880 tree index;
3882 /* Avoid recursing into objects that are not interesting. */
3883 if (!CLASS_TYPE_P (element_type)
3884 || !CLASSTYPE_CONTAINS_EMPTY_CLASS_P (element_type))
3885 return 0;
3887 /* Step through each of the elements in the array. */
3888 for (index = size_zero_node;
3889 /* G++ 3.2 had an off-by-one error here. */
3890 (abi_version_at_least (2)
3891 ? !INT_CST_LT (TYPE_MAX_VALUE (domain), index)
3892 : INT_CST_LT (index, TYPE_MAX_VALUE (domain)));
3893 index = size_binop (PLUS_EXPR, index, size_one_node))
3895 r = walk_subobject_offsets (TREE_TYPE (type),
3897 offset,
3898 offsets,
3899 max_offset,
3900 /*vbases_p=*/1);
3901 if (r)
3902 return r;
3903 offset = size_binop (PLUS_EXPR, offset,
3904 TYPE_SIZE_UNIT (TREE_TYPE (type)));
3905 /* If this new OFFSET is bigger than the MAX_OFFSET, then
3906 there's no point in iterating through the remaining
3907 elements of the array. */
3908 if (max_offset && INT_CST_LT (max_offset, offset))
3909 break;
3913 return 0;
3916 /* Record all of the empty subobjects of TYPE (either a type or a
3917 binfo). If IS_DATA_MEMBER is true, then a non-static data member
3918 is being placed at OFFSET; otherwise, it is a base class that is
3919 being placed at OFFSET. */
3921 static void
3922 record_subobject_offsets (tree type,
3923 tree offset,
3924 splay_tree offsets,
3925 bool is_data_member)
3927 tree max_offset;
3928 /* If recording subobjects for a non-static data member or a
3929 non-empty base class , we do not need to record offsets beyond
3930 the size of the biggest empty class. Additional data members
3931 will go at the end of the class. Additional base classes will go
3932 either at offset zero (if empty, in which case they cannot
3933 overlap with offsets past the size of the biggest empty class) or
3934 at the end of the class.
3936 However, if we are placing an empty base class, then we must record
3937 all offsets, as either the empty class is at offset zero (where
3938 other empty classes might later be placed) or at the end of the
3939 class (where other objects might then be placed, so other empty
3940 subobjects might later overlap). */
3941 if (is_data_member
3942 || !is_empty_class (BINFO_TYPE (type)))
3943 max_offset = sizeof_biggest_empty_class;
3944 else
3945 max_offset = NULL_TREE;
3946 walk_subobject_offsets (type, record_subobject_offset, offset,
3947 offsets, max_offset, is_data_member);
3950 /* Returns nonzero if any of the empty subobjects of TYPE (located at
3951 OFFSET) conflict with entries in OFFSETS. If VBASES_P is nonzero,
3952 virtual bases of TYPE are examined. */
3954 static int
3955 layout_conflict_p (tree type,
3956 tree offset,
3957 splay_tree offsets,
3958 int vbases_p)
3960 splay_tree_node max_node;
3962 /* Get the node in OFFSETS that indicates the maximum offset where
3963 an empty subobject is located. */
3964 max_node = splay_tree_max (offsets);
3965 /* If there aren't any empty subobjects, then there's no point in
3966 performing this check. */
3967 if (!max_node)
3968 return 0;
3970 return walk_subobject_offsets (type, check_subobject_offset, offset,
3971 offsets, (tree) (max_node->key),
3972 vbases_p);
3975 /* DECL is a FIELD_DECL corresponding either to a base subobject of a
3976 non-static data member of the type indicated by RLI. BINFO is the
3977 binfo corresponding to the base subobject, OFFSETS maps offsets to
3978 types already located at those offsets. This function determines
3979 the position of the DECL. */
3981 static void
3982 layout_nonempty_base_or_field (record_layout_info rli,
3983 tree decl,
3984 tree binfo,
3985 splay_tree offsets)
3987 tree offset = NULL_TREE;
3988 bool field_p;
3989 tree type;
3991 if (binfo)
3993 /* For the purposes of determining layout conflicts, we want to
3994 use the class type of BINFO; TREE_TYPE (DECL) will be the
3995 CLASSTYPE_AS_BASE version, which does not contain entries for
3996 zero-sized bases. */
3997 type = TREE_TYPE (binfo);
3998 field_p = false;
4000 else
4002 type = TREE_TYPE (decl);
4003 field_p = true;
4006 /* Try to place the field. It may take more than one try if we have
4007 a hard time placing the field without putting two objects of the
4008 same type at the same address. */
4009 while (1)
4011 struct record_layout_info_s old_rli = *rli;
4013 /* Place this field. */
4014 place_field (rli, decl);
4015 offset = byte_position (decl);
4017 /* We have to check to see whether or not there is already
4018 something of the same type at the offset we're about to use.
4019 For example, consider:
4021 struct S {};
4022 struct T : public S { int i; };
4023 struct U : public S, public T {};
4025 Here, we put S at offset zero in U. Then, we can't put T at
4026 offset zero -- its S component would be at the same address
4027 as the S we already allocated. So, we have to skip ahead.
4028 Since all data members, including those whose type is an
4029 empty class, have nonzero size, any overlap can happen only
4030 with a direct or indirect base-class -- it can't happen with
4031 a data member. */
4032 /* In a union, overlap is permitted; all members are placed at
4033 offset zero. */
4034 if (TREE_CODE (rli->t) == UNION_TYPE)
4035 break;
4036 /* G++ 3.2 did not check for overlaps when placing a non-empty
4037 virtual base. */
4038 if (!abi_version_at_least (2) && binfo && BINFO_VIRTUAL_P (binfo))
4039 break;
4040 if (layout_conflict_p (field_p ? type : binfo, offset,
4041 offsets, field_p))
4043 /* Strip off the size allocated to this field. That puts us
4044 at the first place we could have put the field with
4045 proper alignment. */
4046 *rli = old_rli;
4048 /* Bump up by the alignment required for the type. */
4049 rli->bitpos
4050 = size_binop (PLUS_EXPR, rli->bitpos,
4051 bitsize_int (binfo
4052 ? CLASSTYPE_ALIGN (type)
4053 : TYPE_ALIGN (type)));
4054 normalize_rli (rli);
4056 else
4057 /* There was no conflict. We're done laying out this field. */
4058 break;
4061 /* Now that we know where it will be placed, update its
4062 BINFO_OFFSET. */
4063 if (binfo && CLASS_TYPE_P (BINFO_TYPE (binfo)))
4064 /* Indirect virtual bases may have a nonzero BINFO_OFFSET at
4065 this point because their BINFO_OFFSET is copied from another
4066 hierarchy. Therefore, we may not need to add the entire
4067 OFFSET. */
4068 propagate_binfo_offsets (binfo,
4069 size_diffop_loc (input_location,
4070 convert (ssizetype, offset),
4071 convert (ssizetype,
4072 BINFO_OFFSET (binfo))));
4075 /* Returns true if TYPE is empty and OFFSET is nonzero. */
4077 static int
4078 empty_base_at_nonzero_offset_p (tree type,
4079 tree offset,
4080 splay_tree /*offsets*/)
4082 return is_empty_class (type) && !integer_zerop (offset);
4085 /* Layout the empty base BINFO. EOC indicates the byte currently just
4086 past the end of the class, and should be correctly aligned for a
4087 class of the type indicated by BINFO; OFFSETS gives the offsets of
4088 the empty bases allocated so far. T is the most derived
4089 type. Return nonzero iff we added it at the end. */
4091 static bool
4092 layout_empty_base (record_layout_info rli, tree binfo,
4093 tree eoc, splay_tree offsets)
4095 tree alignment;
4096 tree basetype = BINFO_TYPE (binfo);
4097 bool atend = false;
4099 /* This routine should only be used for empty classes. */
4100 gcc_assert (is_empty_class (basetype));
4101 alignment = ssize_int (CLASSTYPE_ALIGN_UNIT (basetype));
4103 if (!integer_zerop (BINFO_OFFSET (binfo)))
4105 if (abi_version_at_least (2))
4106 propagate_binfo_offsets
4107 (binfo, size_diffop_loc (input_location,
4108 size_zero_node, BINFO_OFFSET (binfo)));
4109 else
4110 warning (OPT_Wabi,
4111 "offset of empty base %qT may not be ABI-compliant and may"
4112 "change in a future version of GCC",
4113 BINFO_TYPE (binfo));
4116 /* This is an empty base class. We first try to put it at offset
4117 zero. */
4118 if (layout_conflict_p (binfo,
4119 BINFO_OFFSET (binfo),
4120 offsets,
4121 /*vbases_p=*/0))
4123 /* That didn't work. Now, we move forward from the next
4124 available spot in the class. */
4125 atend = true;
4126 propagate_binfo_offsets (binfo, convert (ssizetype, eoc));
4127 while (1)
4129 if (!layout_conflict_p (binfo,
4130 BINFO_OFFSET (binfo),
4131 offsets,
4132 /*vbases_p=*/0))
4133 /* We finally found a spot where there's no overlap. */
4134 break;
4136 /* There's overlap here, too. Bump along to the next spot. */
4137 propagate_binfo_offsets (binfo, alignment);
4141 if (CLASSTYPE_USER_ALIGN (basetype))
4143 rli->record_align = MAX (rli->record_align, CLASSTYPE_ALIGN (basetype));
4144 if (warn_packed)
4145 rli->unpacked_align = MAX (rli->unpacked_align, CLASSTYPE_ALIGN (basetype));
4146 TYPE_USER_ALIGN (rli->t) = 1;
4149 return atend;
4152 /* Layout the base given by BINFO in the class indicated by RLI.
4153 *BASE_ALIGN is a running maximum of the alignments of
4154 any base class. OFFSETS gives the location of empty base
4155 subobjects. T is the most derived type. Return nonzero if the new
4156 object cannot be nearly-empty. A new FIELD_DECL is inserted at
4157 *NEXT_FIELD, unless BINFO is for an empty base class.
4159 Returns the location at which the next field should be inserted. */
4161 static tree *
4162 build_base_field (record_layout_info rli, tree binfo,
4163 splay_tree offsets, tree *next_field)
4165 tree t = rli->t;
4166 tree basetype = BINFO_TYPE (binfo);
4168 if (!COMPLETE_TYPE_P (basetype))
4169 /* This error is now reported in xref_tag, thus giving better
4170 location information. */
4171 return next_field;
4173 /* Place the base class. */
4174 if (!is_empty_class (basetype))
4176 tree decl;
4178 /* The containing class is non-empty because it has a non-empty
4179 base class. */
4180 CLASSTYPE_EMPTY_P (t) = 0;
4182 /* Create the FIELD_DECL. */
4183 decl = build_decl (input_location,
4184 FIELD_DECL, NULL_TREE, CLASSTYPE_AS_BASE (basetype));
4185 DECL_ARTIFICIAL (decl) = 1;
4186 DECL_IGNORED_P (decl) = 1;
4187 DECL_FIELD_CONTEXT (decl) = t;
4188 if (CLASSTYPE_AS_BASE (basetype))
4190 DECL_SIZE (decl) = CLASSTYPE_SIZE (basetype);
4191 DECL_SIZE_UNIT (decl) = CLASSTYPE_SIZE_UNIT (basetype);
4192 DECL_ALIGN (decl) = CLASSTYPE_ALIGN (basetype);
4193 DECL_USER_ALIGN (decl) = CLASSTYPE_USER_ALIGN (basetype);
4194 DECL_MODE (decl) = TYPE_MODE (basetype);
4195 DECL_FIELD_IS_BASE (decl) = 1;
4197 /* Try to place the field. It may take more than one try if we
4198 have a hard time placing the field without putting two
4199 objects of the same type at the same address. */
4200 layout_nonempty_base_or_field (rli, decl, binfo, offsets);
4201 /* Add the new FIELD_DECL to the list of fields for T. */
4202 DECL_CHAIN (decl) = *next_field;
4203 *next_field = decl;
4204 next_field = &DECL_CHAIN (decl);
4207 else
4209 tree eoc;
4210 bool atend;
4212 /* On some platforms (ARM), even empty classes will not be
4213 byte-aligned. */
4214 eoc = round_up_loc (input_location,
4215 rli_size_unit_so_far (rli),
4216 CLASSTYPE_ALIGN_UNIT (basetype));
4217 atend = layout_empty_base (rli, binfo, eoc, offsets);
4218 /* A nearly-empty class "has no proper base class that is empty,
4219 not morally virtual, and at an offset other than zero." */
4220 if (!BINFO_VIRTUAL_P (binfo) && CLASSTYPE_NEARLY_EMPTY_P (t))
4222 if (atend)
4223 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
4224 /* The check above (used in G++ 3.2) is insufficient because
4225 an empty class placed at offset zero might itself have an
4226 empty base at a nonzero offset. */
4227 else if (walk_subobject_offsets (basetype,
4228 empty_base_at_nonzero_offset_p,
4229 size_zero_node,
4230 /*offsets=*/NULL,
4231 /*max_offset=*/NULL_TREE,
4232 /*vbases_p=*/true))
4234 if (abi_version_at_least (2))
4235 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
4236 else
4237 warning (OPT_Wabi,
4238 "class %qT will be considered nearly empty in a "
4239 "future version of GCC", t);
4243 /* We do not create a FIELD_DECL for empty base classes because
4244 it might overlap some other field. We want to be able to
4245 create CONSTRUCTORs for the class by iterating over the
4246 FIELD_DECLs, and the back end does not handle overlapping
4247 FIELD_DECLs. */
4249 /* An empty virtual base causes a class to be non-empty
4250 -- but in that case we do not need to clear CLASSTYPE_EMPTY_P
4251 here because that was already done when the virtual table
4252 pointer was created. */
4255 /* Record the offsets of BINFO and its base subobjects. */
4256 record_subobject_offsets (binfo,
4257 BINFO_OFFSET (binfo),
4258 offsets,
4259 /*is_data_member=*/false);
4261 return next_field;
4264 /* Layout all of the non-virtual base classes. Record empty
4265 subobjects in OFFSETS. T is the most derived type. Return nonzero
4266 if the type cannot be nearly empty. The fields created
4267 corresponding to the base classes will be inserted at
4268 *NEXT_FIELD. */
4270 static void
4271 build_base_fields (record_layout_info rli,
4272 splay_tree offsets, tree *next_field)
4274 /* Chain to hold all the new FIELD_DECLs which stand in for base class
4275 subobjects. */
4276 tree t = rli->t;
4277 int n_baseclasses = BINFO_N_BASE_BINFOS (TYPE_BINFO (t));
4278 int i;
4280 /* The primary base class is always allocated first. */
4281 if (CLASSTYPE_HAS_PRIMARY_BASE_P (t))
4282 next_field = build_base_field (rli, CLASSTYPE_PRIMARY_BINFO (t),
4283 offsets, next_field);
4285 /* Now allocate the rest of the bases. */
4286 for (i = 0; i < n_baseclasses; ++i)
4288 tree base_binfo;
4290 base_binfo = BINFO_BASE_BINFO (TYPE_BINFO (t), i);
4292 /* The primary base was already allocated above, so we don't
4293 need to allocate it again here. */
4294 if (base_binfo == CLASSTYPE_PRIMARY_BINFO (t))
4295 continue;
4297 /* Virtual bases are added at the end (a primary virtual base
4298 will have already been added). */
4299 if (BINFO_VIRTUAL_P (base_binfo))
4300 continue;
4302 next_field = build_base_field (rli, base_binfo,
4303 offsets, next_field);
4307 /* Go through the TYPE_METHODS of T issuing any appropriate
4308 diagnostics, figuring out which methods override which other
4309 methods, and so forth. */
4311 static void
4312 check_methods (tree t)
4314 tree x;
4316 for (x = TYPE_METHODS (t); x; x = DECL_CHAIN (x))
4318 check_for_override (x, t);
4319 if (DECL_PURE_VIRTUAL_P (x) && ! DECL_VINDEX (x))
4320 error ("initializer specified for non-virtual method %q+D", x);
4321 /* The name of the field is the original field name
4322 Save this in auxiliary field for later overloading. */
4323 if (DECL_VINDEX (x))
4325 TYPE_POLYMORPHIC_P (t) = 1;
4326 if (DECL_PURE_VIRTUAL_P (x))
4327 vec_safe_push (CLASSTYPE_PURE_VIRTUALS (t), x);
4329 /* All user-provided destructors are non-trivial.
4330 Constructors and assignment ops are handled in
4331 grok_special_member_properties. */
4332 if (DECL_DESTRUCTOR_P (x) && user_provided_p (x))
4333 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t) = 1;
4337 /* FN is a constructor or destructor. Clone the declaration to create
4338 a specialized in-charge or not-in-charge version, as indicated by
4339 NAME. */
4341 static tree
4342 build_clone (tree fn, tree name)
4344 tree parms;
4345 tree clone;
4347 /* Copy the function. */
4348 clone = copy_decl (fn);
4349 /* Reset the function name. */
4350 DECL_NAME (clone) = name;
4351 SET_DECL_ASSEMBLER_NAME (clone, NULL_TREE);
4352 /* Remember where this function came from. */
4353 DECL_ABSTRACT_ORIGIN (clone) = fn;
4354 /* Make it easy to find the CLONE given the FN. */
4355 DECL_CHAIN (clone) = DECL_CHAIN (fn);
4356 DECL_CHAIN (fn) = clone;
4358 /* If this is a template, do the rest on the DECL_TEMPLATE_RESULT. */
4359 if (TREE_CODE (clone) == TEMPLATE_DECL)
4361 tree result = build_clone (DECL_TEMPLATE_RESULT (clone), name);
4362 DECL_TEMPLATE_RESULT (clone) = result;
4363 DECL_TEMPLATE_INFO (result) = copy_node (DECL_TEMPLATE_INFO (result));
4364 DECL_TI_TEMPLATE (result) = clone;
4365 TREE_TYPE (clone) = TREE_TYPE (result);
4366 return clone;
4369 DECL_CLONED_FUNCTION (clone) = fn;
4370 /* There's no pending inline data for this function. */
4371 DECL_PENDING_INLINE_INFO (clone) = NULL;
4372 DECL_PENDING_INLINE_P (clone) = 0;
4374 /* The base-class destructor is not virtual. */
4375 if (name == base_dtor_identifier)
4377 DECL_VIRTUAL_P (clone) = 0;
4378 if (TREE_CODE (clone) != TEMPLATE_DECL)
4379 DECL_VINDEX (clone) = NULL_TREE;
4382 /* If there was an in-charge parameter, drop it from the function
4383 type. */
4384 if (DECL_HAS_IN_CHARGE_PARM_P (clone))
4386 tree basetype;
4387 tree parmtypes;
4388 tree exceptions;
4390 exceptions = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (clone));
4391 basetype = TYPE_METHOD_BASETYPE (TREE_TYPE (clone));
4392 parmtypes = TYPE_ARG_TYPES (TREE_TYPE (clone));
4393 /* Skip the `this' parameter. */
4394 parmtypes = TREE_CHAIN (parmtypes);
4395 /* Skip the in-charge parameter. */
4396 parmtypes = TREE_CHAIN (parmtypes);
4397 /* And the VTT parm, in a complete [cd]tor. */
4398 if (DECL_HAS_VTT_PARM_P (fn)
4399 && ! DECL_NEEDS_VTT_PARM_P (clone))
4400 parmtypes = TREE_CHAIN (parmtypes);
4401 /* If this is subobject constructor or destructor, add the vtt
4402 parameter. */
4403 TREE_TYPE (clone)
4404 = build_method_type_directly (basetype,
4405 TREE_TYPE (TREE_TYPE (clone)),
4406 parmtypes);
4407 if (exceptions)
4408 TREE_TYPE (clone) = build_exception_variant (TREE_TYPE (clone),
4409 exceptions);
4410 TREE_TYPE (clone)
4411 = cp_build_type_attribute_variant (TREE_TYPE (clone),
4412 TYPE_ATTRIBUTES (TREE_TYPE (fn)));
4415 /* Copy the function parameters. */
4416 DECL_ARGUMENTS (clone) = copy_list (DECL_ARGUMENTS (clone));
4417 /* Remove the in-charge parameter. */
4418 if (DECL_HAS_IN_CHARGE_PARM_P (clone))
4420 DECL_CHAIN (DECL_ARGUMENTS (clone))
4421 = DECL_CHAIN (DECL_CHAIN (DECL_ARGUMENTS (clone)));
4422 DECL_HAS_IN_CHARGE_PARM_P (clone) = 0;
4424 /* And the VTT parm, in a complete [cd]tor. */
4425 if (DECL_HAS_VTT_PARM_P (fn))
4427 if (DECL_NEEDS_VTT_PARM_P (clone))
4428 DECL_HAS_VTT_PARM_P (clone) = 1;
4429 else
4431 DECL_CHAIN (DECL_ARGUMENTS (clone))
4432 = DECL_CHAIN (DECL_CHAIN (DECL_ARGUMENTS (clone)));
4433 DECL_HAS_VTT_PARM_P (clone) = 0;
4437 for (parms = DECL_ARGUMENTS (clone); parms; parms = DECL_CHAIN (parms))
4439 DECL_CONTEXT (parms) = clone;
4440 cxx_dup_lang_specific_decl (parms);
4443 /* Create the RTL for this function. */
4444 SET_DECL_RTL (clone, NULL);
4445 rest_of_decl_compilation (clone, /*top_level=*/1, at_eof);
4447 if (pch_file)
4448 note_decl_for_pch (clone);
4450 return clone;
4453 /* Implementation of DECL_CLONED_FUNCTION and DECL_CLONED_FUNCTION_P, do
4454 not invoke this function directly.
4456 For a non-thunk function, returns the address of the slot for storing
4457 the function it is a clone of. Otherwise returns NULL_TREE.
4459 If JUST_TESTING, looks through TEMPLATE_DECL and returns NULL if
4460 cloned_function is unset. This is to support the separate
4461 DECL_CLONED_FUNCTION and DECL_CLONED_FUNCTION_P modes; using the latter
4462 on a template makes sense, but not the former. */
4464 tree *
4465 decl_cloned_function_p (const_tree decl, bool just_testing)
4467 tree *ptr;
4468 if (just_testing)
4469 decl = STRIP_TEMPLATE (decl);
4471 if (TREE_CODE (decl) != FUNCTION_DECL
4472 || !DECL_LANG_SPECIFIC (decl)
4473 || DECL_LANG_SPECIFIC (decl)->u.fn.thunk_p)
4475 #if defined ENABLE_TREE_CHECKING && (GCC_VERSION >= 2007)
4476 if (!just_testing)
4477 lang_check_failed (__FILE__, __LINE__, __FUNCTION__);
4478 else
4479 #endif
4480 return NULL;
4483 ptr = &DECL_LANG_SPECIFIC (decl)->u.fn.u5.cloned_function;
4484 if (just_testing && *ptr == NULL_TREE)
4485 return NULL;
4486 else
4487 return ptr;
4490 /* Produce declarations for all appropriate clones of FN. If
4491 UPDATE_METHOD_VEC_P is nonzero, the clones are added to the
4492 CLASTYPE_METHOD_VEC as well. */
4494 void
4495 clone_function_decl (tree fn, int update_method_vec_p)
4497 tree clone;
4499 /* Avoid inappropriate cloning. */
4500 if (DECL_CHAIN (fn)
4501 && DECL_CLONED_FUNCTION_P (DECL_CHAIN (fn)))
4502 return;
4504 if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (fn))
4506 /* For each constructor, we need two variants: an in-charge version
4507 and a not-in-charge version. */
4508 clone = build_clone (fn, complete_ctor_identifier);
4509 if (update_method_vec_p)
4510 add_method (DECL_CONTEXT (clone), clone, NULL_TREE);
4511 clone = build_clone (fn, base_ctor_identifier);
4512 if (update_method_vec_p)
4513 add_method (DECL_CONTEXT (clone), clone, NULL_TREE);
4515 else
4517 gcc_assert (DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (fn));
4519 /* For each destructor, we need three variants: an in-charge
4520 version, a not-in-charge version, and an in-charge deleting
4521 version. We clone the deleting version first because that
4522 means it will go second on the TYPE_METHODS list -- and that
4523 corresponds to the correct layout order in the virtual
4524 function table.
4526 For a non-virtual destructor, we do not build a deleting
4527 destructor. */
4528 if (DECL_VIRTUAL_P (fn))
4530 clone = build_clone (fn, deleting_dtor_identifier);
4531 if (update_method_vec_p)
4532 add_method (DECL_CONTEXT (clone), clone, NULL_TREE);
4534 clone = build_clone (fn, complete_dtor_identifier);
4535 if (update_method_vec_p)
4536 add_method (DECL_CONTEXT (clone), clone, NULL_TREE);
4537 clone = build_clone (fn, base_dtor_identifier);
4538 if (update_method_vec_p)
4539 add_method (DECL_CONTEXT (clone), clone, NULL_TREE);
4542 /* Note that this is an abstract function that is never emitted. */
4543 DECL_ABSTRACT (fn) = 1;
4546 /* DECL is an in charge constructor, which is being defined. This will
4547 have had an in class declaration, from whence clones were
4548 declared. An out-of-class definition can specify additional default
4549 arguments. As it is the clones that are involved in overload
4550 resolution, we must propagate the information from the DECL to its
4551 clones. */
4553 void
4554 adjust_clone_args (tree decl)
4556 tree clone;
4558 for (clone = DECL_CHAIN (decl); clone && DECL_CLONED_FUNCTION_P (clone);
4559 clone = DECL_CHAIN (clone))
4561 tree orig_clone_parms = TYPE_ARG_TYPES (TREE_TYPE (clone));
4562 tree orig_decl_parms = TYPE_ARG_TYPES (TREE_TYPE (decl));
4563 tree decl_parms, clone_parms;
4565 clone_parms = orig_clone_parms;
4567 /* Skip the 'this' parameter. */
4568 orig_clone_parms = TREE_CHAIN (orig_clone_parms);
4569 orig_decl_parms = TREE_CHAIN (orig_decl_parms);
4571 if (DECL_HAS_IN_CHARGE_PARM_P (decl))
4572 orig_decl_parms = TREE_CHAIN (orig_decl_parms);
4573 if (DECL_HAS_VTT_PARM_P (decl))
4574 orig_decl_parms = TREE_CHAIN (orig_decl_parms);
4576 clone_parms = orig_clone_parms;
4577 if (DECL_HAS_VTT_PARM_P (clone))
4578 clone_parms = TREE_CHAIN (clone_parms);
4580 for (decl_parms = orig_decl_parms; decl_parms;
4581 decl_parms = TREE_CHAIN (decl_parms),
4582 clone_parms = TREE_CHAIN (clone_parms))
4584 gcc_assert (same_type_p (TREE_TYPE (decl_parms),
4585 TREE_TYPE (clone_parms)));
4587 if (TREE_PURPOSE (decl_parms) && !TREE_PURPOSE (clone_parms))
4589 /* A default parameter has been added. Adjust the
4590 clone's parameters. */
4591 tree exceptions = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (clone));
4592 tree attrs = TYPE_ATTRIBUTES (TREE_TYPE (clone));
4593 tree basetype = TYPE_METHOD_BASETYPE (TREE_TYPE (clone));
4594 tree type;
4596 clone_parms = orig_decl_parms;
4598 if (DECL_HAS_VTT_PARM_P (clone))
4600 clone_parms = tree_cons (TREE_PURPOSE (orig_clone_parms),
4601 TREE_VALUE (orig_clone_parms),
4602 clone_parms);
4603 TREE_TYPE (clone_parms) = TREE_TYPE (orig_clone_parms);
4605 type = build_method_type_directly (basetype,
4606 TREE_TYPE (TREE_TYPE (clone)),
4607 clone_parms);
4608 if (exceptions)
4609 type = build_exception_variant (type, exceptions);
4610 if (attrs)
4611 type = cp_build_type_attribute_variant (type, attrs);
4612 TREE_TYPE (clone) = type;
4614 clone_parms = NULL_TREE;
4615 break;
4618 gcc_assert (!clone_parms);
4622 /* For each of the constructors and destructors in T, create an
4623 in-charge and not-in-charge variant. */
4625 static void
4626 clone_constructors_and_destructors (tree t)
4628 tree fns;
4630 /* If for some reason we don't have a CLASSTYPE_METHOD_VEC, we bail
4631 out now. */
4632 if (!CLASSTYPE_METHOD_VEC (t))
4633 return;
4635 for (fns = CLASSTYPE_CONSTRUCTORS (t); fns; fns = OVL_NEXT (fns))
4636 clone_function_decl (OVL_CURRENT (fns), /*update_method_vec_p=*/1);
4637 for (fns = CLASSTYPE_DESTRUCTORS (t); fns; fns = OVL_NEXT (fns))
4638 clone_function_decl (OVL_CURRENT (fns), /*update_method_vec_p=*/1);
4641 /* Deduce noexcept for a destructor DTOR. */
4643 void
4644 deduce_noexcept_on_destructor (tree dtor)
4646 if (!TYPE_RAISES_EXCEPTIONS (TREE_TYPE (dtor)))
4648 tree ctx = DECL_CONTEXT (dtor);
4649 tree implicit_fn = implicitly_declare_fn (sfk_destructor, ctx,
4650 /*const_p=*/false,
4651 NULL, NULL);
4652 tree eh_spec = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (implicit_fn));
4653 TREE_TYPE (dtor) = build_exception_variant (TREE_TYPE (dtor), eh_spec);
4657 /* For each destructor in T, deduce noexcept:
4659 12.4/3: A declaration of a destructor that does not have an
4660 exception-specification is implicitly considered to have the
4661 same exception-specification as an implicit declaration (15.4). */
4663 static void
4664 deduce_noexcept_on_destructors (tree t)
4666 /* If for some reason we don't have a CLASSTYPE_METHOD_VEC, we bail
4667 out now. */
4668 if (!CLASSTYPE_METHOD_VEC (t))
4669 return;
4671 bool saved_nontrivial_dtor = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t);
4673 /* Avoid early exit from synthesized_method_walk (c++/57645). */
4674 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t) = true;
4676 for (tree fns = CLASSTYPE_DESTRUCTORS (t); fns; fns = OVL_NEXT (fns))
4677 deduce_noexcept_on_destructor (OVL_CURRENT (fns));
4679 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t) = saved_nontrivial_dtor;
4682 /* Subroutine of set_one_vmethod_tm_attributes. Search base classes
4683 of TYPE for virtual functions which FNDECL overrides. Return a
4684 mask of the tm attributes found therein. */
4686 static int
4687 look_for_tm_attr_overrides (tree type, tree fndecl)
4689 tree binfo = TYPE_BINFO (type);
4690 tree base_binfo;
4691 int ix, found = 0;
4693 for (ix = 0; BINFO_BASE_ITERATE (binfo, ix, base_binfo); ++ix)
4695 tree o, basetype = BINFO_TYPE (base_binfo);
4697 if (!TYPE_POLYMORPHIC_P (basetype))
4698 continue;
4700 o = look_for_overrides_here (basetype, fndecl);
4701 if (o)
4702 found |= tm_attr_to_mask (find_tm_attribute
4703 (TYPE_ATTRIBUTES (TREE_TYPE (o))));
4704 else
4705 found |= look_for_tm_attr_overrides (basetype, fndecl);
4708 return found;
4711 /* Subroutine of set_method_tm_attributes. Handle the checks and
4712 inheritance for one virtual method FNDECL. */
4714 static void
4715 set_one_vmethod_tm_attributes (tree type, tree fndecl)
4717 tree tm_attr;
4718 int found, have;
4720 found = look_for_tm_attr_overrides (type, fndecl);
4722 /* If FNDECL doesn't actually override anything (i.e. T is the
4723 class that first declares FNDECL virtual), then we're done. */
4724 if (found == 0)
4725 return;
4727 tm_attr = find_tm_attribute (TYPE_ATTRIBUTES (TREE_TYPE (fndecl)));
4728 have = tm_attr_to_mask (tm_attr);
4730 /* Intel STM Language Extension 3.0, Section 4.2 table 4:
4731 tm_pure must match exactly, otherwise no weakening of
4732 tm_safe > tm_callable > nothing. */
4733 /* ??? The tm_pure attribute didn't make the transition to the
4734 multivendor language spec. */
4735 if (have == TM_ATTR_PURE)
4737 if (found != TM_ATTR_PURE)
4739 found &= -found;
4740 goto err_override;
4743 /* If the overridden function is tm_pure, then FNDECL must be. */
4744 else if (found == TM_ATTR_PURE && tm_attr)
4745 goto err_override;
4746 /* Look for base class combinations that cannot be satisfied. */
4747 else if (found != TM_ATTR_PURE && (found & TM_ATTR_PURE))
4749 found &= ~TM_ATTR_PURE;
4750 found &= -found;
4751 error_at (DECL_SOURCE_LOCATION (fndecl),
4752 "method overrides both %<transaction_pure%> and %qE methods",
4753 tm_mask_to_attr (found));
4755 /* If FNDECL did not declare an attribute, then inherit the most
4756 restrictive one. */
4757 else if (tm_attr == NULL)
4759 apply_tm_attr (fndecl, tm_mask_to_attr (found & -found));
4761 /* Otherwise validate that we're not weaker than a function
4762 that is being overridden. */
4763 else
4765 found &= -found;
4766 if (found <= TM_ATTR_CALLABLE && have > found)
4767 goto err_override;
4769 return;
4771 err_override:
4772 error_at (DECL_SOURCE_LOCATION (fndecl),
4773 "method declared %qE overriding %qE method",
4774 tm_attr, tm_mask_to_attr (found));
4777 /* For each of the methods in T, propagate a class-level tm attribute. */
4779 static void
4780 set_method_tm_attributes (tree t)
4782 tree class_tm_attr, fndecl;
4784 /* Don't bother collecting tm attributes if transactional memory
4785 support is not enabled. */
4786 if (!flag_tm)
4787 return;
4789 /* Process virtual methods first, as they inherit directly from the
4790 base virtual function and also require validation of new attributes. */
4791 if (TYPE_CONTAINS_VPTR_P (t))
4793 tree vchain;
4794 for (vchain = BINFO_VIRTUALS (TYPE_BINFO (t)); vchain;
4795 vchain = TREE_CHAIN (vchain))
4797 fndecl = BV_FN (vchain);
4798 if (DECL_THUNK_P (fndecl))
4799 fndecl = THUNK_TARGET (fndecl);
4800 set_one_vmethod_tm_attributes (t, fndecl);
4804 /* If the class doesn't have an attribute, nothing more to do. */
4805 class_tm_attr = find_tm_attribute (TYPE_ATTRIBUTES (t));
4806 if (class_tm_attr == NULL)
4807 return;
4809 /* Any method that does not yet have a tm attribute inherits
4810 the one from the class. */
4811 for (fndecl = TYPE_METHODS (t); fndecl; fndecl = TREE_CHAIN (fndecl))
4813 if (!find_tm_attribute (TYPE_ATTRIBUTES (TREE_TYPE (fndecl))))
4814 apply_tm_attr (fndecl, class_tm_attr);
4818 /* Returns true iff class T has a user-defined constructor other than
4819 the default constructor. */
4821 bool
4822 type_has_user_nondefault_constructor (tree t)
4824 tree fns;
4826 if (!TYPE_HAS_USER_CONSTRUCTOR (t))
4827 return false;
4829 for (fns = CLASSTYPE_CONSTRUCTORS (t); fns; fns = OVL_NEXT (fns))
4831 tree fn = OVL_CURRENT (fns);
4832 if (!DECL_ARTIFICIAL (fn)
4833 && (TREE_CODE (fn) == TEMPLATE_DECL
4834 || (skip_artificial_parms_for (fn, DECL_ARGUMENTS (fn))
4835 != NULL_TREE)))
4836 return true;
4839 return false;
4842 /* Returns the defaulted constructor if T has one. Otherwise, returns
4843 NULL_TREE. */
4845 tree
4846 in_class_defaulted_default_constructor (tree t)
4848 tree fns, args;
4850 if (!TYPE_HAS_USER_CONSTRUCTOR (t))
4851 return NULL_TREE;
4853 for (fns = CLASSTYPE_CONSTRUCTORS (t); fns; fns = OVL_NEXT (fns))
4855 tree fn = OVL_CURRENT (fns);
4857 if (DECL_DEFAULTED_IN_CLASS_P (fn))
4859 args = FUNCTION_FIRST_USER_PARMTYPE (fn);
4860 while (args && TREE_PURPOSE (args))
4861 args = TREE_CHAIN (args);
4862 if (!args || args == void_list_node)
4863 return fn;
4867 return NULL_TREE;
4870 /* Returns true iff FN is a user-provided function, i.e. user-declared
4871 and not defaulted at its first declaration; or explicit, private,
4872 protected, or non-const. */
4874 bool
4875 user_provided_p (tree fn)
4877 if (TREE_CODE (fn) == TEMPLATE_DECL)
4878 return true;
4879 else
4880 return (!DECL_ARTIFICIAL (fn)
4881 && !DECL_DEFAULTED_IN_CLASS_P (fn));
4884 /* Returns true iff class T has a user-provided constructor. */
4886 bool
4887 type_has_user_provided_constructor (tree t)
4889 tree fns;
4891 if (!CLASS_TYPE_P (t))
4892 return false;
4894 if (!TYPE_HAS_USER_CONSTRUCTOR (t))
4895 return false;
4897 /* This can happen in error cases; avoid crashing. */
4898 if (!CLASSTYPE_METHOD_VEC (t))
4899 return false;
4901 for (fns = CLASSTYPE_CONSTRUCTORS (t); fns; fns = OVL_NEXT (fns))
4902 if (user_provided_p (OVL_CURRENT (fns)))
4903 return true;
4905 return false;
4908 /* Returns true iff class T has a user-provided default constructor. */
4910 bool
4911 type_has_user_provided_default_constructor (tree t)
4913 tree fns;
4915 if (!TYPE_HAS_USER_CONSTRUCTOR (t))
4916 return false;
4918 for (fns = CLASSTYPE_CONSTRUCTORS (t); fns; fns = OVL_NEXT (fns))
4920 tree fn = OVL_CURRENT (fns);
4921 if (TREE_CODE (fn) == FUNCTION_DECL
4922 && user_provided_p (fn)
4923 && sufficient_parms_p (FUNCTION_FIRST_USER_PARMTYPE (fn)))
4924 return true;
4927 return false;
4930 /* TYPE is being used as a virtual base, and has a non-trivial move
4931 assignment. Return true if this is due to there being a user-provided
4932 move assignment in TYPE or one of its subobjects; if there isn't, then
4933 multiple move assignment can't cause any harm. */
4935 bool
4936 vbase_has_user_provided_move_assign (tree type)
4938 /* Does the type itself have a user-provided move assignment operator? */
4939 for (tree fns
4940 = lookup_fnfields_slot_nolazy (type, ansi_assopname (NOP_EXPR));
4941 fns; fns = OVL_NEXT (fns))
4943 tree fn = OVL_CURRENT (fns);
4944 if (move_fn_p (fn) && user_provided_p (fn))
4945 return true;
4948 /* Do any of its bases? */
4949 tree binfo = TYPE_BINFO (type);
4950 tree base_binfo;
4951 for (int i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
4952 if (vbase_has_user_provided_move_assign (BINFO_TYPE (base_binfo)))
4953 return true;
4955 /* Or non-static data members? */
4956 for (tree field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
4958 if (TREE_CODE (field) == FIELD_DECL
4959 && CLASS_TYPE_P (TREE_TYPE (field))
4960 && vbase_has_user_provided_move_assign (TREE_TYPE (field)))
4961 return true;
4964 /* Seems not. */
4965 return false;
4968 /* If default-initialization leaves part of TYPE uninitialized, returns
4969 a DECL for the field or TYPE itself (DR 253). */
4971 tree
4972 default_init_uninitialized_part (tree type)
4974 tree t, r, binfo;
4975 int i;
4977 type = strip_array_types (type);
4978 if (!CLASS_TYPE_P (type))
4979 return type;
4980 if (type_has_user_provided_default_constructor (type))
4981 return NULL_TREE;
4982 for (binfo = TYPE_BINFO (type), i = 0;
4983 BINFO_BASE_ITERATE (binfo, i, t); ++i)
4985 r = default_init_uninitialized_part (BINFO_TYPE (t));
4986 if (r)
4987 return r;
4989 for (t = TYPE_FIELDS (type); t; t = DECL_CHAIN (t))
4990 if (TREE_CODE (t) == FIELD_DECL
4991 && !DECL_ARTIFICIAL (t)
4992 && !DECL_INITIAL (t))
4994 r = default_init_uninitialized_part (TREE_TYPE (t));
4995 if (r)
4996 return DECL_P (r) ? r : t;
4999 return NULL_TREE;
5002 /* Returns true iff for class T, a trivial synthesized default constructor
5003 would be constexpr. */
5005 bool
5006 trivial_default_constructor_is_constexpr (tree t)
5008 /* A defaulted trivial default constructor is constexpr
5009 if there is nothing to initialize. */
5010 gcc_assert (!TYPE_HAS_COMPLEX_DFLT (t));
5011 return is_really_empty_class (t);
5014 /* Returns true iff class T has a constexpr default constructor. */
5016 bool
5017 type_has_constexpr_default_constructor (tree t)
5019 tree fns;
5021 if (!CLASS_TYPE_P (t))
5023 /* The caller should have stripped an enclosing array. */
5024 gcc_assert (TREE_CODE (t) != ARRAY_TYPE);
5025 return false;
5027 if (CLASSTYPE_LAZY_DEFAULT_CTOR (t))
5029 if (!TYPE_HAS_COMPLEX_DFLT (t))
5030 return trivial_default_constructor_is_constexpr (t);
5031 /* Non-trivial, we need to check subobject constructors. */
5032 lazily_declare_fn (sfk_constructor, t);
5034 fns = locate_ctor (t);
5035 return (fns && DECL_DECLARED_CONSTEXPR_P (fns));
5038 /* Returns true iff class TYPE has a virtual destructor. */
5040 bool
5041 type_has_virtual_destructor (tree type)
5043 tree dtor;
5045 if (!CLASS_TYPE_P (type))
5046 return false;
5048 gcc_assert (COMPLETE_TYPE_P (type));
5049 dtor = CLASSTYPE_DESTRUCTORS (type);
5050 return (dtor && DECL_VIRTUAL_P (dtor));
5053 /* Returns true iff class T has a move constructor. */
5055 bool
5056 type_has_move_constructor (tree t)
5058 tree fns;
5060 if (CLASSTYPE_LAZY_MOVE_CTOR (t))
5062 gcc_assert (COMPLETE_TYPE_P (t));
5063 lazily_declare_fn (sfk_move_constructor, t);
5066 if (!CLASSTYPE_METHOD_VEC (t))
5067 return false;
5069 for (fns = CLASSTYPE_CONSTRUCTORS (t); fns; fns = OVL_NEXT (fns))
5070 if (move_fn_p (OVL_CURRENT (fns)))
5071 return true;
5073 return false;
5076 /* Returns true iff class T has a move assignment operator. */
5078 bool
5079 type_has_move_assign (tree t)
5081 tree fns;
5083 if (CLASSTYPE_LAZY_MOVE_ASSIGN (t))
5085 gcc_assert (COMPLETE_TYPE_P (t));
5086 lazily_declare_fn (sfk_move_assignment, t);
5089 for (fns = lookup_fnfields_slot_nolazy (t, ansi_assopname (NOP_EXPR));
5090 fns; fns = OVL_NEXT (fns))
5091 if (move_fn_p (OVL_CURRENT (fns)))
5092 return true;
5094 return false;
5097 /* Returns true iff class T has a move constructor that was explicitly
5098 declared in the class body. Note that this is different from
5099 "user-provided", which doesn't include functions that are defaulted in
5100 the class. */
5102 bool
5103 type_has_user_declared_move_constructor (tree t)
5105 tree fns;
5107 if (CLASSTYPE_LAZY_MOVE_CTOR (t))
5108 return false;
5110 if (!CLASSTYPE_METHOD_VEC (t))
5111 return false;
5113 for (fns = CLASSTYPE_CONSTRUCTORS (t); fns; fns = OVL_NEXT (fns))
5115 tree fn = OVL_CURRENT (fns);
5116 if (move_fn_p (fn) && !DECL_ARTIFICIAL (fn))
5117 return true;
5120 return false;
5123 /* Returns true iff class T has a move assignment operator that was
5124 explicitly declared in the class body. */
5126 bool
5127 type_has_user_declared_move_assign (tree t)
5129 tree fns;
5131 if (CLASSTYPE_LAZY_MOVE_ASSIGN (t))
5132 return false;
5134 for (fns = lookup_fnfields_slot_nolazy (t, ansi_assopname (NOP_EXPR));
5135 fns; fns = OVL_NEXT (fns))
5137 tree fn = OVL_CURRENT (fns);
5138 if (move_fn_p (fn) && !DECL_ARTIFICIAL (fn))
5139 return true;
5142 return false;
5145 /* Nonzero if we need to build up a constructor call when initializing an
5146 object of this class, either because it has a user-provided constructor
5147 or because it doesn't have a default constructor (so we need to give an
5148 error if no initializer is provided). Use TYPE_NEEDS_CONSTRUCTING when
5149 what you care about is whether or not an object can be produced by a
5150 constructor (e.g. so we don't set TREE_READONLY on const variables of
5151 such type); use this function when what you care about is whether or not
5152 to try to call a constructor to create an object. The latter case is
5153 the former plus some cases of constructors that cannot be called. */
5155 bool
5156 type_build_ctor_call (tree t)
5158 tree inner;
5159 if (TYPE_NEEDS_CONSTRUCTING (t))
5160 return true;
5161 inner = strip_array_types (t);
5162 return (CLASS_TYPE_P (inner) && !TYPE_HAS_DEFAULT_CONSTRUCTOR (inner)
5163 && !ANON_AGGR_TYPE_P (inner));
5166 /* Remove all zero-width bit-fields from T. */
5168 static void
5169 remove_zero_width_bit_fields (tree t)
5171 tree *fieldsp;
5173 fieldsp = &TYPE_FIELDS (t);
5174 while (*fieldsp)
5176 if (TREE_CODE (*fieldsp) == FIELD_DECL
5177 && DECL_C_BIT_FIELD (*fieldsp)
5178 /* We should not be confused by the fact that grokbitfield
5179 temporarily sets the width of the bit field into
5180 DECL_INITIAL (*fieldsp).
5181 check_bitfield_decl eventually sets DECL_SIZE (*fieldsp)
5182 to that width. */
5183 && integer_zerop (DECL_SIZE (*fieldsp)))
5184 *fieldsp = DECL_CHAIN (*fieldsp);
5185 else
5186 fieldsp = &DECL_CHAIN (*fieldsp);
5190 /* Returns TRUE iff we need a cookie when dynamically allocating an
5191 array whose elements have the indicated class TYPE. */
5193 static bool
5194 type_requires_array_cookie (tree type)
5196 tree fns;
5197 bool has_two_argument_delete_p = false;
5199 gcc_assert (CLASS_TYPE_P (type));
5201 /* If there's a non-trivial destructor, we need a cookie. In order
5202 to iterate through the array calling the destructor for each
5203 element, we'll have to know how many elements there are. */
5204 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
5205 return true;
5207 /* If the usual deallocation function is a two-argument whose second
5208 argument is of type `size_t', then we have to pass the size of
5209 the array to the deallocation function, so we will need to store
5210 a cookie. */
5211 fns = lookup_fnfields (TYPE_BINFO (type),
5212 ansi_opname (VEC_DELETE_EXPR),
5213 /*protect=*/0);
5214 /* If there are no `operator []' members, or the lookup is
5215 ambiguous, then we don't need a cookie. */
5216 if (!fns || fns == error_mark_node)
5217 return false;
5218 /* Loop through all of the functions. */
5219 for (fns = BASELINK_FUNCTIONS (fns); fns; fns = OVL_NEXT (fns))
5221 tree fn;
5222 tree second_parm;
5224 /* Select the current function. */
5225 fn = OVL_CURRENT (fns);
5226 /* See if this function is a one-argument delete function. If
5227 it is, then it will be the usual deallocation function. */
5228 second_parm = TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (fn)));
5229 if (second_parm == void_list_node)
5230 return false;
5231 /* Do not consider this function if its second argument is an
5232 ellipsis. */
5233 if (!second_parm)
5234 continue;
5235 /* Otherwise, if we have a two-argument function and the second
5236 argument is `size_t', it will be the usual deallocation
5237 function -- unless there is one-argument function, too. */
5238 if (TREE_CHAIN (second_parm) == void_list_node
5239 && same_type_p (TREE_VALUE (second_parm), size_type_node))
5240 has_two_argument_delete_p = true;
5243 return has_two_argument_delete_p;
5246 /* Finish computing the `literal type' property of class type T.
5248 At this point, we have already processed base classes and
5249 non-static data members. We need to check whether the copy
5250 constructor is trivial, the destructor is trivial, and there
5251 is a trivial default constructor or at least one constexpr
5252 constructor other than the copy constructor. */
5254 static void
5255 finalize_literal_type_property (tree t)
5257 tree fn;
5259 if (cxx_dialect < cxx11
5260 || TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t))
5261 CLASSTYPE_LITERAL_P (t) = false;
5262 else if (CLASSTYPE_LITERAL_P (t) && !TYPE_HAS_TRIVIAL_DFLT (t)
5263 && CLASSTYPE_NON_AGGREGATE (t)
5264 && !TYPE_HAS_CONSTEXPR_CTOR (t))
5265 CLASSTYPE_LITERAL_P (t) = false;
5267 if (!CLASSTYPE_LITERAL_P (t))
5268 for (fn = TYPE_METHODS (t); fn; fn = DECL_CHAIN (fn))
5269 if (DECL_DECLARED_CONSTEXPR_P (fn)
5270 && TREE_CODE (fn) != TEMPLATE_DECL
5271 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
5272 && !DECL_CONSTRUCTOR_P (fn))
5274 DECL_DECLARED_CONSTEXPR_P (fn) = false;
5275 if (!DECL_GENERATED_P (fn))
5277 error ("enclosing class of constexpr non-static member "
5278 "function %q+#D is not a literal type", fn);
5279 explain_non_literal_class (t);
5284 /* T is a non-literal type used in a context which requires a constant
5285 expression. Explain why it isn't literal. */
5287 void
5288 explain_non_literal_class (tree t)
5290 static struct pointer_set_t *diagnosed;
5292 if (!CLASS_TYPE_P (t))
5293 return;
5294 t = TYPE_MAIN_VARIANT (t);
5296 if (diagnosed == NULL)
5297 diagnosed = pointer_set_create ();
5298 if (pointer_set_insert (diagnosed, t) != 0)
5299 /* Already explained. */
5300 return;
5302 inform (0, "%q+T is not literal because:", t);
5303 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t))
5304 inform (0, " %q+T has a non-trivial destructor", t);
5305 else if (CLASSTYPE_NON_AGGREGATE (t)
5306 && !TYPE_HAS_TRIVIAL_DFLT (t)
5307 && !TYPE_HAS_CONSTEXPR_CTOR (t))
5309 inform (0, " %q+T is not an aggregate, does not have a trivial "
5310 "default constructor, and has no constexpr constructor that "
5311 "is not a copy or move constructor", t);
5312 if (TYPE_HAS_DEFAULT_CONSTRUCTOR (t)
5313 && !type_has_user_provided_default_constructor (t))
5315 /* Note that we can't simply call locate_ctor because when the
5316 constructor is deleted it just returns NULL_TREE. */
5317 tree fns;
5318 for (fns = CLASSTYPE_CONSTRUCTORS (t); fns; fns = OVL_NEXT (fns))
5320 tree fn = OVL_CURRENT (fns);
5321 tree parms = TYPE_ARG_TYPES (TREE_TYPE (fn));
5323 parms = skip_artificial_parms_for (fn, parms);
5325 if (sufficient_parms_p (parms))
5327 if (DECL_DELETED_FN (fn))
5328 maybe_explain_implicit_delete (fn);
5329 else
5330 explain_invalid_constexpr_fn (fn);
5331 break;
5336 else
5338 tree binfo, base_binfo, field; int i;
5339 for (binfo = TYPE_BINFO (t), i = 0;
5340 BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
5342 tree basetype = TREE_TYPE (base_binfo);
5343 if (!CLASSTYPE_LITERAL_P (basetype))
5345 inform (0, " base class %qT of %q+T is non-literal",
5346 basetype, t);
5347 explain_non_literal_class (basetype);
5348 return;
5351 for (field = TYPE_FIELDS (t); field; field = TREE_CHAIN (field))
5353 tree ftype;
5354 if (TREE_CODE (field) != FIELD_DECL)
5355 continue;
5356 ftype = TREE_TYPE (field);
5357 if (!literal_type_p (ftype))
5359 inform (0, " non-static data member %q+D has "
5360 "non-literal type", field);
5361 if (CLASS_TYPE_P (ftype))
5362 explain_non_literal_class (ftype);
5368 /* Check the validity of the bases and members declared in T. Add any
5369 implicitly-generated functions (like copy-constructors and
5370 assignment operators). Compute various flag bits (like
5371 CLASSTYPE_NON_LAYOUT_POD_T) for T. This routine works purely at the C++
5372 level: i.e., independently of the ABI in use. */
5374 static void
5375 check_bases_and_members (tree t)
5377 /* Nonzero if the implicitly generated copy constructor should take
5378 a non-const reference argument. */
5379 int cant_have_const_ctor;
5380 /* Nonzero if the implicitly generated assignment operator
5381 should take a non-const reference argument. */
5382 int no_const_asn_ref;
5383 tree access_decls;
5384 bool saved_complex_asn_ref;
5385 bool saved_nontrivial_dtor;
5386 tree fn;
5388 /* By default, we use const reference arguments and generate default
5389 constructors. */
5390 cant_have_const_ctor = 0;
5391 no_const_asn_ref = 0;
5393 /* Check all the base-classes. */
5394 check_bases (t, &cant_have_const_ctor,
5395 &no_const_asn_ref);
5397 /* Deduce noexcept on destructors. This needs to happen after we've set
5398 triviality flags appropriately for our bases. */
5399 if (cxx_dialect >= cxx11)
5400 deduce_noexcept_on_destructors (t);
5402 /* Check all the method declarations. */
5403 check_methods (t);
5405 /* Save the initial values of these flags which only indicate whether
5406 or not the class has user-provided functions. As we analyze the
5407 bases and members we can set these flags for other reasons. */
5408 saved_complex_asn_ref = TYPE_HAS_COMPLEX_COPY_ASSIGN (t);
5409 saved_nontrivial_dtor = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t);
5411 /* Check all the data member declarations. We cannot call
5412 check_field_decls until we have called check_bases check_methods,
5413 as check_field_decls depends on TYPE_HAS_NONTRIVIAL_DESTRUCTOR
5414 being set appropriately. */
5415 check_field_decls (t, &access_decls,
5416 &cant_have_const_ctor,
5417 &no_const_asn_ref);
5419 /* A nearly-empty class has to be vptr-containing; a nearly empty
5420 class contains just a vptr. */
5421 if (!TYPE_CONTAINS_VPTR_P (t))
5422 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
5424 /* Do some bookkeeping that will guide the generation of implicitly
5425 declared member functions. */
5426 TYPE_HAS_COMPLEX_COPY_CTOR (t) |= TYPE_CONTAINS_VPTR_P (t);
5427 TYPE_HAS_COMPLEX_MOVE_CTOR (t) |= TYPE_CONTAINS_VPTR_P (t);
5428 /* We need to call a constructor for this class if it has a
5429 user-provided constructor, or if the default constructor is going
5430 to initialize the vptr. (This is not an if-and-only-if;
5431 TYPE_NEEDS_CONSTRUCTING is set elsewhere if bases or members
5432 themselves need constructing.) */
5433 TYPE_NEEDS_CONSTRUCTING (t)
5434 |= (type_has_user_provided_constructor (t) || TYPE_CONTAINS_VPTR_P (t));
5435 /* [dcl.init.aggr]
5437 An aggregate is an array or a class with no user-provided
5438 constructors ... and no virtual functions.
5440 Again, other conditions for being an aggregate are checked
5441 elsewhere. */
5442 CLASSTYPE_NON_AGGREGATE (t)
5443 |= (type_has_user_provided_constructor (t) || TYPE_POLYMORPHIC_P (t));
5444 /* This is the C++98/03 definition of POD; it changed in C++0x, but we
5445 retain the old definition internally for ABI reasons. */
5446 CLASSTYPE_NON_LAYOUT_POD_P (t)
5447 |= (CLASSTYPE_NON_AGGREGATE (t)
5448 || saved_nontrivial_dtor || saved_complex_asn_ref);
5449 CLASSTYPE_NON_STD_LAYOUT (t) |= TYPE_CONTAINS_VPTR_P (t);
5450 TYPE_HAS_COMPLEX_COPY_ASSIGN (t) |= TYPE_CONTAINS_VPTR_P (t);
5451 TYPE_HAS_COMPLEX_MOVE_ASSIGN (t) |= TYPE_CONTAINS_VPTR_P (t);
5452 TYPE_HAS_COMPLEX_DFLT (t) |= TYPE_CONTAINS_VPTR_P (t);
5454 /* If the class has no user-declared constructor, but does have
5455 non-static const or reference data members that can never be
5456 initialized, issue a warning. */
5457 if (warn_uninitialized
5458 /* Classes with user-declared constructors are presumed to
5459 initialize these members. */
5460 && !TYPE_HAS_USER_CONSTRUCTOR (t)
5461 /* Aggregates can be initialized with brace-enclosed
5462 initializers. */
5463 && CLASSTYPE_NON_AGGREGATE (t))
5465 tree field;
5467 for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
5469 tree type;
5471 if (TREE_CODE (field) != FIELD_DECL
5472 || DECL_INITIAL (field) != NULL_TREE)
5473 continue;
5475 type = TREE_TYPE (field);
5476 if (TREE_CODE (type) == REFERENCE_TYPE)
5477 warning (OPT_Wuninitialized, "non-static reference %q+#D "
5478 "in class without a constructor", field);
5479 else if (CP_TYPE_CONST_P (type)
5480 && (!CLASS_TYPE_P (type)
5481 || !TYPE_HAS_DEFAULT_CONSTRUCTOR (type)))
5482 warning (OPT_Wuninitialized, "non-static const member %q+#D "
5483 "in class without a constructor", field);
5487 /* Synthesize any needed methods. */
5488 add_implicitly_declared_members (t, &access_decls,
5489 cant_have_const_ctor,
5490 no_const_asn_ref);
5492 /* Check defaulted declarations here so we have cant_have_const_ctor
5493 and don't need to worry about clones. */
5494 for (fn = TYPE_METHODS (t); fn; fn = DECL_CHAIN (fn))
5495 if (!DECL_ARTIFICIAL (fn) && DECL_DEFAULTED_IN_CLASS_P (fn))
5497 int copy = copy_fn_p (fn);
5498 if (copy > 0)
5500 bool imp_const_p
5501 = (DECL_CONSTRUCTOR_P (fn) ? !cant_have_const_ctor
5502 : !no_const_asn_ref);
5503 bool fn_const_p = (copy == 2);
5505 if (fn_const_p && !imp_const_p)
5506 /* If the function is defaulted outside the class, we just
5507 give the synthesis error. */
5508 error ("%q+D declared to take const reference, but implicit "
5509 "declaration would take non-const", fn);
5511 defaulted_late_check (fn);
5514 if (LAMBDA_TYPE_P (t))
5516 /* "The closure type associated with a lambda-expression has a deleted
5517 default constructor and a deleted copy assignment operator." */
5518 TYPE_NEEDS_CONSTRUCTING (t) = 1;
5519 TYPE_HAS_COMPLEX_DFLT (t) = 1;
5520 TYPE_HAS_COMPLEX_COPY_ASSIGN (t) = 1;
5521 CLASSTYPE_LAZY_MOVE_ASSIGN (t) = 0;
5523 /* "This class type is not an aggregate." */
5524 CLASSTYPE_NON_AGGREGATE (t) = 1;
5527 /* Compute the 'literal type' property before we
5528 do anything with non-static member functions. */
5529 finalize_literal_type_property (t);
5531 /* Create the in-charge and not-in-charge variants of constructors
5532 and destructors. */
5533 clone_constructors_and_destructors (t);
5535 /* Process the using-declarations. */
5536 for (; access_decls; access_decls = TREE_CHAIN (access_decls))
5537 handle_using_decl (TREE_VALUE (access_decls), t);
5539 /* Build and sort the CLASSTYPE_METHOD_VEC. */
5540 finish_struct_methods (t);
5542 /* Figure out whether or not we will need a cookie when dynamically
5543 allocating an array of this type. */
5544 TYPE_LANG_SPECIFIC (t)->u.c.vec_new_uses_cookie
5545 = type_requires_array_cookie (t);
5548 /* If T needs a pointer to its virtual function table, set TYPE_VFIELD
5549 accordingly. If a new vfield was created (because T doesn't have a
5550 primary base class), then the newly created field is returned. It
5551 is not added to the TYPE_FIELDS list; it is the caller's
5552 responsibility to do that. Accumulate declared virtual functions
5553 on VIRTUALS_P. */
5555 static tree
5556 create_vtable_ptr (tree t, tree* virtuals_p)
5558 tree fn;
5560 /* Collect the virtual functions declared in T. */
5561 for (fn = TYPE_METHODS (t); fn; fn = DECL_CHAIN (fn))
5562 if (DECL_VINDEX (fn) && !DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (fn)
5563 && TREE_CODE (DECL_VINDEX (fn)) != INTEGER_CST)
5565 tree new_virtual = make_node (TREE_LIST);
5567 BV_FN (new_virtual) = fn;
5568 BV_DELTA (new_virtual) = integer_zero_node;
5569 BV_VCALL_INDEX (new_virtual) = NULL_TREE;
5571 TREE_CHAIN (new_virtual) = *virtuals_p;
5572 *virtuals_p = new_virtual;
5575 /* If we couldn't find an appropriate base class, create a new field
5576 here. Even if there weren't any new virtual functions, we might need a
5577 new virtual function table if we're supposed to include vptrs in
5578 all classes that need them. */
5579 if (!TYPE_VFIELD (t) && (*virtuals_p || TYPE_CONTAINS_VPTR_P (t)))
5581 /* We build this decl with vtbl_ptr_type_node, which is a
5582 `vtable_entry_type*'. It might seem more precise to use
5583 `vtable_entry_type (*)[N]' where N is the number of virtual
5584 functions. However, that would require the vtable pointer in
5585 base classes to have a different type than the vtable pointer
5586 in derived classes. We could make that happen, but that
5587 still wouldn't solve all the problems. In particular, the
5588 type-based alias analysis code would decide that assignments
5589 to the base class vtable pointer can't alias assignments to
5590 the derived class vtable pointer, since they have different
5591 types. Thus, in a derived class destructor, where the base
5592 class constructor was inlined, we could generate bad code for
5593 setting up the vtable pointer.
5595 Therefore, we use one type for all vtable pointers. We still
5596 use a type-correct type; it's just doesn't indicate the array
5597 bounds. That's better than using `void*' or some such; it's
5598 cleaner, and it let's the alias analysis code know that these
5599 stores cannot alias stores to void*! */
5600 tree field;
5602 field = build_decl (input_location,
5603 FIELD_DECL, get_vfield_name (t), vtbl_ptr_type_node);
5604 DECL_VIRTUAL_P (field) = 1;
5605 DECL_ARTIFICIAL (field) = 1;
5606 DECL_FIELD_CONTEXT (field) = t;
5607 DECL_FCONTEXT (field) = t;
5608 if (TYPE_PACKED (t))
5609 DECL_PACKED (field) = 1;
5611 TYPE_VFIELD (t) = field;
5613 /* This class is non-empty. */
5614 CLASSTYPE_EMPTY_P (t) = 0;
5616 return field;
5619 return NULL_TREE;
5622 /* Add OFFSET to all base types of BINFO which is a base in the
5623 hierarchy dominated by T.
5625 OFFSET, which is a type offset, is number of bytes. */
5627 static void
5628 propagate_binfo_offsets (tree binfo, tree offset)
5630 int i;
5631 tree primary_binfo;
5632 tree base_binfo;
5634 /* Update BINFO's offset. */
5635 BINFO_OFFSET (binfo)
5636 = convert (sizetype,
5637 size_binop (PLUS_EXPR,
5638 convert (ssizetype, BINFO_OFFSET (binfo)),
5639 offset));
5641 /* Find the primary base class. */
5642 primary_binfo = get_primary_binfo (binfo);
5644 if (primary_binfo && BINFO_INHERITANCE_CHAIN (primary_binfo) == binfo)
5645 propagate_binfo_offsets (primary_binfo, offset);
5647 /* Scan all of the bases, pushing the BINFO_OFFSET adjust
5648 downwards. */
5649 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
5651 /* Don't do the primary base twice. */
5652 if (base_binfo == primary_binfo)
5653 continue;
5655 if (BINFO_VIRTUAL_P (base_binfo))
5656 continue;
5658 propagate_binfo_offsets (base_binfo, offset);
5662 /* Set BINFO_OFFSET for all of the virtual bases for RLI->T. Update
5663 TYPE_ALIGN and TYPE_SIZE for T. OFFSETS gives the location of
5664 empty subobjects of T. */
5666 static void
5667 layout_virtual_bases (record_layout_info rli, splay_tree offsets)
5669 tree vbase;
5670 tree t = rli->t;
5671 bool first_vbase = true;
5672 tree *next_field;
5674 if (BINFO_N_BASE_BINFOS (TYPE_BINFO (t)) == 0)
5675 return;
5677 if (!abi_version_at_least(2))
5679 /* In G++ 3.2, we incorrectly rounded the size before laying out
5680 the virtual bases. */
5681 finish_record_layout (rli, /*free_p=*/false);
5682 #ifdef STRUCTURE_SIZE_BOUNDARY
5683 /* Packed structures don't need to have minimum size. */
5684 if (! TYPE_PACKED (t))
5685 TYPE_ALIGN (t) = MAX (TYPE_ALIGN (t), (unsigned) STRUCTURE_SIZE_BOUNDARY);
5686 #endif
5687 rli->offset = TYPE_SIZE_UNIT (t);
5688 rli->bitpos = bitsize_zero_node;
5689 rli->record_align = TYPE_ALIGN (t);
5692 /* Find the last field. The artificial fields created for virtual
5693 bases will go after the last extant field to date. */
5694 next_field = &TYPE_FIELDS (t);
5695 while (*next_field)
5696 next_field = &DECL_CHAIN (*next_field);
5698 /* Go through the virtual bases, allocating space for each virtual
5699 base that is not already a primary base class. These are
5700 allocated in inheritance graph order. */
5701 for (vbase = TYPE_BINFO (t); vbase; vbase = TREE_CHAIN (vbase))
5703 if (!BINFO_VIRTUAL_P (vbase))
5704 continue;
5706 if (!BINFO_PRIMARY_P (vbase))
5708 tree basetype = TREE_TYPE (vbase);
5710 /* This virtual base is not a primary base of any class in the
5711 hierarchy, so we have to add space for it. */
5712 next_field = build_base_field (rli, vbase,
5713 offsets, next_field);
5715 /* If the first virtual base might have been placed at a
5716 lower address, had we started from CLASSTYPE_SIZE, rather
5717 than TYPE_SIZE, issue a warning. There can be both false
5718 positives and false negatives from this warning in rare
5719 cases; to deal with all the possibilities would probably
5720 require performing both layout algorithms and comparing
5721 the results which is not particularly tractable. */
5722 if (warn_abi
5723 && first_vbase
5724 && (tree_int_cst_lt
5725 (size_binop (CEIL_DIV_EXPR,
5726 round_up_loc (input_location,
5727 CLASSTYPE_SIZE (t),
5728 CLASSTYPE_ALIGN (basetype)),
5729 bitsize_unit_node),
5730 BINFO_OFFSET (vbase))))
5731 warning (OPT_Wabi,
5732 "offset of virtual base %qT is not ABI-compliant and "
5733 "may change in a future version of GCC",
5734 basetype);
5736 first_vbase = false;
5741 /* Returns the offset of the byte just past the end of the base class
5742 BINFO. */
5744 static tree
5745 end_of_base (tree binfo)
5747 tree size;
5749 if (!CLASSTYPE_AS_BASE (BINFO_TYPE (binfo)))
5750 size = TYPE_SIZE_UNIT (char_type_node);
5751 else if (is_empty_class (BINFO_TYPE (binfo)))
5752 /* An empty class has zero CLASSTYPE_SIZE_UNIT, but we need to
5753 allocate some space for it. It cannot have virtual bases, so
5754 TYPE_SIZE_UNIT is fine. */
5755 size = TYPE_SIZE_UNIT (BINFO_TYPE (binfo));
5756 else
5757 size = CLASSTYPE_SIZE_UNIT (BINFO_TYPE (binfo));
5759 return size_binop (PLUS_EXPR, BINFO_OFFSET (binfo), size);
5762 /* Returns the offset of the byte just past the end of the base class
5763 with the highest offset in T. If INCLUDE_VIRTUALS_P is zero, then
5764 only non-virtual bases are included. */
5766 static tree
5767 end_of_class (tree t, int include_virtuals_p)
5769 tree result = size_zero_node;
5770 vec<tree, va_gc> *vbases;
5771 tree binfo;
5772 tree base_binfo;
5773 tree offset;
5774 int i;
5776 for (binfo = TYPE_BINFO (t), i = 0;
5777 BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
5779 if (!include_virtuals_p
5780 && BINFO_VIRTUAL_P (base_binfo)
5781 && (!BINFO_PRIMARY_P (base_binfo)
5782 || BINFO_INHERITANCE_CHAIN (base_binfo) != TYPE_BINFO (t)))
5783 continue;
5785 offset = end_of_base (base_binfo);
5786 if (INT_CST_LT_UNSIGNED (result, offset))
5787 result = offset;
5790 /* G++ 3.2 did not check indirect virtual bases. */
5791 if (abi_version_at_least (2) && include_virtuals_p)
5792 for (vbases = CLASSTYPE_VBASECLASSES (t), i = 0;
5793 vec_safe_iterate (vbases, i, &base_binfo); i++)
5795 offset = end_of_base (base_binfo);
5796 if (INT_CST_LT_UNSIGNED (result, offset))
5797 result = offset;
5800 return result;
5803 /* Warn about bases of T that are inaccessible because they are
5804 ambiguous. For example:
5806 struct S {};
5807 struct T : public S {};
5808 struct U : public S, public T {};
5810 Here, `(S*) new U' is not allowed because there are two `S'
5811 subobjects of U. */
5813 static void
5814 warn_about_ambiguous_bases (tree t)
5816 int i;
5817 vec<tree, va_gc> *vbases;
5818 tree basetype;
5819 tree binfo;
5820 tree base_binfo;
5822 /* If there are no repeated bases, nothing can be ambiguous. */
5823 if (!CLASSTYPE_REPEATED_BASE_P (t))
5824 return;
5826 /* Check direct bases. */
5827 for (binfo = TYPE_BINFO (t), i = 0;
5828 BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
5830 basetype = BINFO_TYPE (base_binfo);
5832 if (!uniquely_derived_from_p (basetype, t))
5833 warning (0, "direct base %qT inaccessible in %qT due to ambiguity",
5834 basetype, t);
5837 /* Check for ambiguous virtual bases. */
5838 if (extra_warnings)
5839 for (vbases = CLASSTYPE_VBASECLASSES (t), i = 0;
5840 vec_safe_iterate (vbases, i, &binfo); i++)
5842 basetype = BINFO_TYPE (binfo);
5844 if (!uniquely_derived_from_p (basetype, t))
5845 warning (OPT_Wextra, "virtual base %qT inaccessible in %qT due "
5846 "to ambiguity", basetype, t);
5850 /* Compare two INTEGER_CSTs K1 and K2. */
5852 static int
5853 splay_tree_compare_integer_csts (splay_tree_key k1, splay_tree_key k2)
5855 return tree_int_cst_compare ((tree) k1, (tree) k2);
5858 /* Increase the size indicated in RLI to account for empty classes
5859 that are "off the end" of the class. */
5861 static void
5862 include_empty_classes (record_layout_info rli)
5864 tree eoc;
5865 tree rli_size;
5867 /* It might be the case that we grew the class to allocate a
5868 zero-sized base class. That won't be reflected in RLI, yet,
5869 because we are willing to overlay multiple bases at the same
5870 offset. However, now we need to make sure that RLI is big enough
5871 to reflect the entire class. */
5872 eoc = end_of_class (rli->t,
5873 CLASSTYPE_AS_BASE (rli->t) != NULL_TREE);
5874 rli_size = rli_size_unit_so_far (rli);
5875 if (TREE_CODE (rli_size) == INTEGER_CST
5876 && INT_CST_LT_UNSIGNED (rli_size, eoc))
5878 if (!abi_version_at_least (2))
5879 /* In version 1 of the ABI, the size of a class that ends with
5880 a bitfield was not rounded up to a whole multiple of a
5881 byte. Because rli_size_unit_so_far returns only the number
5882 of fully allocated bytes, any extra bits were not included
5883 in the size. */
5884 rli->bitpos = round_down (rli->bitpos, BITS_PER_UNIT);
5885 else
5886 /* The size should have been rounded to a whole byte. */
5887 gcc_assert (tree_int_cst_equal
5888 (rli->bitpos, round_down (rli->bitpos, BITS_PER_UNIT)));
5889 rli->bitpos
5890 = size_binop (PLUS_EXPR,
5891 rli->bitpos,
5892 size_binop (MULT_EXPR,
5893 convert (bitsizetype,
5894 size_binop (MINUS_EXPR,
5895 eoc, rli_size)),
5896 bitsize_int (BITS_PER_UNIT)));
5897 normalize_rli (rli);
5901 /* Calculate the TYPE_SIZE, TYPE_ALIGN, etc for T. Calculate
5902 BINFO_OFFSETs for all of the base-classes. Position the vtable
5903 pointer. Accumulate declared virtual functions on VIRTUALS_P. */
5905 static void
5906 layout_class_type (tree t, tree *virtuals_p)
5908 tree non_static_data_members;
5909 tree field;
5910 tree vptr;
5911 record_layout_info rli;
5912 /* Maps offsets (represented as INTEGER_CSTs) to a TREE_LIST of
5913 types that appear at that offset. */
5914 splay_tree empty_base_offsets;
5915 /* True if the last field laid out was a bit-field. */
5916 bool last_field_was_bitfield = false;
5917 /* The location at which the next field should be inserted. */
5918 tree *next_field;
5919 /* T, as a base class. */
5920 tree base_t;
5922 /* Keep track of the first non-static data member. */
5923 non_static_data_members = TYPE_FIELDS (t);
5925 /* Start laying out the record. */
5926 rli = start_record_layout (t);
5928 /* Mark all the primary bases in the hierarchy. */
5929 determine_primary_bases (t);
5931 /* Create a pointer to our virtual function table. */
5932 vptr = create_vtable_ptr (t, virtuals_p);
5934 /* The vptr is always the first thing in the class. */
5935 if (vptr)
5937 DECL_CHAIN (vptr) = TYPE_FIELDS (t);
5938 TYPE_FIELDS (t) = vptr;
5939 next_field = &DECL_CHAIN (vptr);
5940 place_field (rli, vptr);
5942 else
5943 next_field = &TYPE_FIELDS (t);
5945 /* Build FIELD_DECLs for all of the non-virtual base-types. */
5946 empty_base_offsets = splay_tree_new (splay_tree_compare_integer_csts,
5947 NULL, NULL);
5948 build_base_fields (rli, empty_base_offsets, next_field);
5950 /* Layout the non-static data members. */
5951 for (field = non_static_data_members; field; field = DECL_CHAIN (field))
5953 tree type;
5954 tree padding;
5956 /* We still pass things that aren't non-static data members to
5957 the back end, in case it wants to do something with them. */
5958 if (TREE_CODE (field) != FIELD_DECL)
5960 place_field (rli, field);
5961 /* If the static data member has incomplete type, keep track
5962 of it so that it can be completed later. (The handling
5963 of pending statics in finish_record_layout is
5964 insufficient; consider:
5966 struct S1;
5967 struct S2 { static S1 s1; };
5969 At this point, finish_record_layout will be called, but
5970 S1 is still incomplete.) */
5971 if (VAR_P (field))
5973 maybe_register_incomplete_var (field);
5974 /* The visibility of static data members is determined
5975 at their point of declaration, not their point of
5976 definition. */
5977 determine_visibility (field);
5979 continue;
5982 type = TREE_TYPE (field);
5983 if (type == error_mark_node)
5984 continue;
5986 padding = NULL_TREE;
5988 /* If this field is a bit-field whose width is greater than its
5989 type, then there are some special rules for allocating
5990 it. */
5991 if (DECL_C_BIT_FIELD (field)
5992 && INT_CST_LT (TYPE_SIZE (type), DECL_SIZE (field)))
5994 unsigned int itk;
5995 tree integer_type;
5996 bool was_unnamed_p = false;
5997 /* We must allocate the bits as if suitably aligned for the
5998 longest integer type that fits in this many bits. type
5999 of the field. Then, we are supposed to use the left over
6000 bits as additional padding. */
6001 for (itk = itk_char; itk != itk_none; ++itk)
6002 if (integer_types[itk] != NULL_TREE
6003 && (INT_CST_LT (size_int (MAX_FIXED_MODE_SIZE),
6004 TYPE_SIZE (integer_types[itk]))
6005 || INT_CST_LT (DECL_SIZE (field),
6006 TYPE_SIZE (integer_types[itk]))))
6007 break;
6009 /* ITK now indicates a type that is too large for the
6010 field. We have to back up by one to find the largest
6011 type that fits. */
6014 --itk;
6015 integer_type = integer_types[itk];
6016 } while (itk > 0 && integer_type == NULL_TREE);
6018 /* Figure out how much additional padding is required. GCC
6019 3.2 always created a padding field, even if it had zero
6020 width. */
6021 if (!abi_version_at_least (2)
6022 || INT_CST_LT (TYPE_SIZE (integer_type), DECL_SIZE (field)))
6024 if (abi_version_at_least (2) && TREE_CODE (t) == UNION_TYPE)
6025 /* In a union, the padding field must have the full width
6026 of the bit-field; all fields start at offset zero. */
6027 padding = DECL_SIZE (field);
6028 else
6030 if (TREE_CODE (t) == UNION_TYPE)
6031 warning (OPT_Wabi, "size assigned to %qT may not be "
6032 "ABI-compliant and may change in a future "
6033 "version of GCC",
6035 padding = size_binop (MINUS_EXPR, DECL_SIZE (field),
6036 TYPE_SIZE (integer_type));
6039 #ifdef PCC_BITFIELD_TYPE_MATTERS
6040 /* An unnamed bitfield does not normally affect the
6041 alignment of the containing class on a target where
6042 PCC_BITFIELD_TYPE_MATTERS. But, the C++ ABI does not
6043 make any exceptions for unnamed bitfields when the
6044 bitfields are longer than their types. Therefore, we
6045 temporarily give the field a name. */
6046 if (PCC_BITFIELD_TYPE_MATTERS && !DECL_NAME (field))
6048 was_unnamed_p = true;
6049 DECL_NAME (field) = make_anon_name ();
6051 #endif
6052 DECL_SIZE (field) = TYPE_SIZE (integer_type);
6053 DECL_ALIGN (field) = TYPE_ALIGN (integer_type);
6054 DECL_USER_ALIGN (field) = TYPE_USER_ALIGN (integer_type);
6055 layout_nonempty_base_or_field (rli, field, NULL_TREE,
6056 empty_base_offsets);
6057 if (was_unnamed_p)
6058 DECL_NAME (field) = NULL_TREE;
6059 /* Now that layout has been performed, set the size of the
6060 field to the size of its declared type; the rest of the
6061 field is effectively invisible. */
6062 DECL_SIZE (field) = TYPE_SIZE (type);
6063 /* We must also reset the DECL_MODE of the field. */
6064 if (abi_version_at_least (2))
6065 DECL_MODE (field) = TYPE_MODE (type);
6066 else if (warn_abi
6067 && DECL_MODE (field) != TYPE_MODE (type))
6068 /* Versions of G++ before G++ 3.4 did not reset the
6069 DECL_MODE. */
6070 warning (OPT_Wabi,
6071 "the offset of %qD may not be ABI-compliant and may "
6072 "change in a future version of GCC", field);
6074 else
6075 layout_nonempty_base_or_field (rli, field, NULL_TREE,
6076 empty_base_offsets);
6078 /* Remember the location of any empty classes in FIELD. */
6079 if (abi_version_at_least (2))
6080 record_subobject_offsets (TREE_TYPE (field),
6081 byte_position(field),
6082 empty_base_offsets,
6083 /*is_data_member=*/true);
6085 /* If a bit-field does not immediately follow another bit-field,
6086 and yet it starts in the middle of a byte, we have failed to
6087 comply with the ABI. */
6088 if (warn_abi
6089 && DECL_C_BIT_FIELD (field)
6090 /* The TREE_NO_WARNING flag gets set by Objective-C when
6091 laying out an Objective-C class. The ObjC ABI differs
6092 from the C++ ABI, and so we do not want a warning
6093 here. */
6094 && !TREE_NO_WARNING (field)
6095 && !last_field_was_bitfield
6096 && !integer_zerop (size_binop (TRUNC_MOD_EXPR,
6097 DECL_FIELD_BIT_OFFSET (field),
6098 bitsize_unit_node)))
6099 warning (OPT_Wabi, "offset of %q+D is not ABI-compliant and may "
6100 "change in a future version of GCC", field);
6102 /* G++ used to use DECL_FIELD_OFFSET as if it were the byte
6103 offset of the field. */
6104 if (warn_abi
6105 && !abi_version_at_least (2)
6106 && !tree_int_cst_equal (DECL_FIELD_OFFSET (field),
6107 byte_position (field))
6108 && contains_empty_class_p (TREE_TYPE (field)))
6109 warning (OPT_Wabi, "%q+D contains empty classes which may cause base "
6110 "classes to be placed at different locations in a "
6111 "future version of GCC", field);
6113 /* The middle end uses the type of expressions to determine the
6114 possible range of expression values. In order to optimize
6115 "x.i > 7" to "false" for a 2-bit bitfield "i", the middle end
6116 must be made aware of the width of "i", via its type.
6118 Because C++ does not have integer types of arbitrary width,
6119 we must (for the purposes of the front end) convert from the
6120 type assigned here to the declared type of the bitfield
6121 whenever a bitfield expression is used as an rvalue.
6122 Similarly, when assigning a value to a bitfield, the value
6123 must be converted to the type given the bitfield here. */
6124 if (DECL_C_BIT_FIELD (field))
6126 unsigned HOST_WIDE_INT width;
6127 tree ftype = TREE_TYPE (field);
6128 width = tree_low_cst (DECL_SIZE (field), /*unsignedp=*/1);
6129 if (width != TYPE_PRECISION (ftype))
6131 TREE_TYPE (field)
6132 = c_build_bitfield_integer_type (width,
6133 TYPE_UNSIGNED (ftype));
6134 TREE_TYPE (field)
6135 = cp_build_qualified_type (TREE_TYPE (field),
6136 cp_type_quals (ftype));
6140 /* If we needed additional padding after this field, add it
6141 now. */
6142 if (padding)
6144 tree padding_field;
6146 padding_field = build_decl (input_location,
6147 FIELD_DECL,
6148 NULL_TREE,
6149 char_type_node);
6150 DECL_BIT_FIELD (padding_field) = 1;
6151 DECL_SIZE (padding_field) = padding;
6152 DECL_CONTEXT (padding_field) = t;
6153 DECL_ARTIFICIAL (padding_field) = 1;
6154 DECL_IGNORED_P (padding_field) = 1;
6155 layout_nonempty_base_or_field (rli, padding_field,
6156 NULL_TREE,
6157 empty_base_offsets);
6160 last_field_was_bitfield = DECL_C_BIT_FIELD (field);
6163 if (abi_version_at_least (2) && !integer_zerop (rli->bitpos))
6165 /* Make sure that we are on a byte boundary so that the size of
6166 the class without virtual bases will always be a round number
6167 of bytes. */
6168 rli->bitpos = round_up_loc (input_location, rli->bitpos, BITS_PER_UNIT);
6169 normalize_rli (rli);
6172 /* G++ 3.2 does not allow virtual bases to be overlaid with tail
6173 padding. */
6174 if (!abi_version_at_least (2))
6175 include_empty_classes(rli);
6177 /* Delete all zero-width bit-fields from the list of fields. Now
6178 that the type is laid out they are no longer important. */
6179 remove_zero_width_bit_fields (t);
6181 /* Create the version of T used for virtual bases. We do not use
6182 make_class_type for this version; this is an artificial type. For
6183 a POD type, we just reuse T. */
6184 if (CLASSTYPE_NON_LAYOUT_POD_P (t) || CLASSTYPE_EMPTY_P (t))
6186 base_t = make_node (TREE_CODE (t));
6188 /* Set the size and alignment for the new type. In G++ 3.2, all
6189 empty classes were considered to have size zero when used as
6190 base classes. */
6191 if (!abi_version_at_least (2) && CLASSTYPE_EMPTY_P (t))
6193 TYPE_SIZE (base_t) = bitsize_zero_node;
6194 TYPE_SIZE_UNIT (base_t) = size_zero_node;
6195 if (warn_abi && !integer_zerop (rli_size_unit_so_far (rli)))
6196 warning (OPT_Wabi,
6197 "layout of classes derived from empty class %qT "
6198 "may change in a future version of GCC",
6201 else
6203 tree eoc;
6205 /* If the ABI version is not at least two, and the last
6206 field was a bit-field, RLI may not be on a byte
6207 boundary. In particular, rli_size_unit_so_far might
6208 indicate the last complete byte, while rli_size_so_far
6209 indicates the total number of bits used. Therefore,
6210 rli_size_so_far, rather than rli_size_unit_so_far, is
6211 used to compute TYPE_SIZE_UNIT. */
6212 eoc = end_of_class (t, /*include_virtuals_p=*/0);
6213 TYPE_SIZE_UNIT (base_t)
6214 = size_binop (MAX_EXPR,
6215 convert (sizetype,
6216 size_binop (CEIL_DIV_EXPR,
6217 rli_size_so_far (rli),
6218 bitsize_int (BITS_PER_UNIT))),
6219 eoc);
6220 TYPE_SIZE (base_t)
6221 = size_binop (MAX_EXPR,
6222 rli_size_so_far (rli),
6223 size_binop (MULT_EXPR,
6224 convert (bitsizetype, eoc),
6225 bitsize_int (BITS_PER_UNIT)));
6227 TYPE_ALIGN (base_t) = rli->record_align;
6228 TYPE_USER_ALIGN (base_t) = TYPE_USER_ALIGN (t);
6230 /* Copy the fields from T. */
6231 next_field = &TYPE_FIELDS (base_t);
6232 for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
6233 if (TREE_CODE (field) == FIELD_DECL)
6235 *next_field = build_decl (input_location,
6236 FIELD_DECL,
6237 DECL_NAME (field),
6238 TREE_TYPE (field));
6239 DECL_CONTEXT (*next_field) = base_t;
6240 DECL_FIELD_OFFSET (*next_field) = DECL_FIELD_OFFSET (field);
6241 DECL_FIELD_BIT_OFFSET (*next_field)
6242 = DECL_FIELD_BIT_OFFSET (field);
6243 DECL_SIZE (*next_field) = DECL_SIZE (field);
6244 DECL_MODE (*next_field) = DECL_MODE (field);
6245 next_field = &DECL_CHAIN (*next_field);
6248 /* Record the base version of the type. */
6249 CLASSTYPE_AS_BASE (t) = base_t;
6250 TYPE_CONTEXT (base_t) = t;
6252 else
6253 CLASSTYPE_AS_BASE (t) = t;
6255 /* Every empty class contains an empty class. */
6256 if (CLASSTYPE_EMPTY_P (t))
6257 CLASSTYPE_CONTAINS_EMPTY_CLASS_P (t) = 1;
6259 /* Set the TYPE_DECL for this type to contain the right
6260 value for DECL_OFFSET, so that we can use it as part
6261 of a COMPONENT_REF for multiple inheritance. */
6262 layout_decl (TYPE_MAIN_DECL (t), 0);
6264 /* Now fix up any virtual base class types that we left lying
6265 around. We must get these done before we try to lay out the
6266 virtual function table. As a side-effect, this will remove the
6267 base subobject fields. */
6268 layout_virtual_bases (rli, empty_base_offsets);
6270 /* Make sure that empty classes are reflected in RLI at this
6271 point. */
6272 include_empty_classes(rli);
6274 /* Make sure not to create any structures with zero size. */
6275 if (integer_zerop (rli_size_unit_so_far (rli)) && CLASSTYPE_EMPTY_P (t))
6276 place_field (rli,
6277 build_decl (input_location,
6278 FIELD_DECL, NULL_TREE, char_type_node));
6280 /* If this is a non-POD, declaring it packed makes a difference to how it
6281 can be used as a field; don't let finalize_record_size undo it. */
6282 if (TYPE_PACKED (t) && !layout_pod_type_p (t))
6283 rli->packed_maybe_necessary = true;
6285 /* Let the back end lay out the type. */
6286 finish_record_layout (rli, /*free_p=*/true);
6288 if (TYPE_SIZE_UNIT (t)
6289 && TREE_CODE (TYPE_SIZE_UNIT (t)) == INTEGER_CST
6290 && !TREE_OVERFLOW (TYPE_SIZE_UNIT (t))
6291 && !valid_constant_size_p (TYPE_SIZE_UNIT (t)))
6292 error ("type %qT is too large", t);
6294 /* Warn about bases that can't be talked about due to ambiguity. */
6295 warn_about_ambiguous_bases (t);
6297 /* Now that we're done with layout, give the base fields the real types. */
6298 for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
6299 if (DECL_ARTIFICIAL (field) && IS_FAKE_BASE_TYPE (TREE_TYPE (field)))
6300 TREE_TYPE (field) = TYPE_CONTEXT (TREE_TYPE (field));
6302 /* Clean up. */
6303 splay_tree_delete (empty_base_offsets);
6305 if (CLASSTYPE_EMPTY_P (t)
6306 && tree_int_cst_lt (sizeof_biggest_empty_class,
6307 TYPE_SIZE_UNIT (t)))
6308 sizeof_biggest_empty_class = TYPE_SIZE_UNIT (t);
6311 /* Determine the "key method" for the class type indicated by TYPE,
6312 and set CLASSTYPE_KEY_METHOD accordingly. */
6314 void
6315 determine_key_method (tree type)
6317 tree method;
6319 if (TYPE_FOR_JAVA (type)
6320 || processing_template_decl
6321 || CLASSTYPE_TEMPLATE_INSTANTIATION (type)
6322 || CLASSTYPE_INTERFACE_KNOWN (type))
6323 return;
6325 /* The key method is the first non-pure virtual function that is not
6326 inline at the point of class definition. On some targets the
6327 key function may not be inline; those targets should not call
6328 this function until the end of the translation unit. */
6329 for (method = TYPE_METHODS (type); method != NULL_TREE;
6330 method = DECL_CHAIN (method))
6331 if (DECL_VINDEX (method) != NULL_TREE
6332 && ! DECL_DECLARED_INLINE_P (method)
6333 && ! DECL_PURE_VIRTUAL_P (method))
6335 CLASSTYPE_KEY_METHOD (type) = method;
6336 break;
6339 return;
6343 /* Allocate and return an instance of struct sorted_fields_type with
6344 N fields. */
6346 static struct sorted_fields_type *
6347 sorted_fields_type_new (int n)
6349 struct sorted_fields_type *sft;
6350 sft = ggc_alloc_sorted_fields_type (sizeof (struct sorted_fields_type)
6351 + n * sizeof (tree));
6352 sft->len = n;
6354 return sft;
6358 /* Perform processing required when the definition of T (a class type)
6359 is complete. */
6361 void
6362 finish_struct_1 (tree t)
6364 tree x;
6365 /* A TREE_LIST. The TREE_VALUE of each node is a FUNCTION_DECL. */
6366 tree virtuals = NULL_TREE;
6368 if (COMPLETE_TYPE_P (t))
6370 gcc_assert (MAYBE_CLASS_TYPE_P (t));
6371 error ("redefinition of %q#T", t);
6372 popclass ();
6373 return;
6376 /* If this type was previously laid out as a forward reference,
6377 make sure we lay it out again. */
6378 TYPE_SIZE (t) = NULL_TREE;
6379 CLASSTYPE_PRIMARY_BINFO (t) = NULL_TREE;
6381 /* Make assumptions about the class; we'll reset the flags if
6382 necessary. */
6383 CLASSTYPE_EMPTY_P (t) = 1;
6384 CLASSTYPE_NEARLY_EMPTY_P (t) = 1;
6385 CLASSTYPE_CONTAINS_EMPTY_CLASS_P (t) = 0;
6386 CLASSTYPE_LITERAL_P (t) = true;
6388 /* Do end-of-class semantic processing: checking the validity of the
6389 bases and members and add implicitly generated methods. */
6390 check_bases_and_members (t);
6392 /* Find the key method. */
6393 if (TYPE_CONTAINS_VPTR_P (t))
6395 /* The Itanium C++ ABI permits the key method to be chosen when
6396 the class is defined -- even though the key method so
6397 selected may later turn out to be an inline function. On
6398 some systems (such as ARM Symbian OS) the key method cannot
6399 be determined until the end of the translation unit. On such
6400 systems, we leave CLASSTYPE_KEY_METHOD set to NULL, which
6401 will cause the class to be added to KEYED_CLASSES. Then, in
6402 finish_file we will determine the key method. */
6403 if (targetm.cxx.key_method_may_be_inline ())
6404 determine_key_method (t);
6406 /* If a polymorphic class has no key method, we may emit the vtable
6407 in every translation unit where the class definition appears. */
6408 if (CLASSTYPE_KEY_METHOD (t) == NULL_TREE)
6409 keyed_classes = tree_cons (NULL_TREE, t, keyed_classes);
6412 /* Layout the class itself. */
6413 layout_class_type (t, &virtuals);
6414 if (CLASSTYPE_AS_BASE (t) != t)
6415 /* We use the base type for trivial assignments, and hence it
6416 needs a mode. */
6417 compute_record_mode (CLASSTYPE_AS_BASE (t));
6419 virtuals = modify_all_vtables (t, nreverse (virtuals));
6421 /* If necessary, create the primary vtable for this class. */
6422 if (virtuals || TYPE_CONTAINS_VPTR_P (t))
6424 /* We must enter these virtuals into the table. */
6425 if (!CLASSTYPE_HAS_PRIMARY_BASE_P (t))
6426 build_primary_vtable (NULL_TREE, t);
6427 else if (! BINFO_NEW_VTABLE_MARKED (TYPE_BINFO (t)))
6428 /* Here we know enough to change the type of our virtual
6429 function table, but we will wait until later this function. */
6430 build_primary_vtable (CLASSTYPE_PRIMARY_BINFO (t), t);
6432 /* If we're warning about ABI tags, check the types of the new
6433 virtual functions. */
6434 if (warn_abi_tag)
6435 for (tree v = virtuals; v; v = TREE_CHAIN (v))
6436 check_abi_tags (t, TREE_VALUE (v));
6439 if (TYPE_CONTAINS_VPTR_P (t))
6441 int vindex;
6442 tree fn;
6444 if (BINFO_VTABLE (TYPE_BINFO (t)))
6445 gcc_assert (DECL_VIRTUAL_P (BINFO_VTABLE (TYPE_BINFO (t))));
6446 if (!CLASSTYPE_HAS_PRIMARY_BASE_P (t))
6447 gcc_assert (BINFO_VIRTUALS (TYPE_BINFO (t)) == NULL_TREE);
6449 /* Add entries for virtual functions introduced by this class. */
6450 BINFO_VIRTUALS (TYPE_BINFO (t))
6451 = chainon (BINFO_VIRTUALS (TYPE_BINFO (t)), virtuals);
6453 /* Set DECL_VINDEX for all functions declared in this class. */
6454 for (vindex = 0, fn = BINFO_VIRTUALS (TYPE_BINFO (t));
6456 fn = TREE_CHAIN (fn),
6457 vindex += (TARGET_VTABLE_USES_DESCRIPTORS
6458 ? TARGET_VTABLE_USES_DESCRIPTORS : 1))
6460 tree fndecl = BV_FN (fn);
6462 if (DECL_THUNK_P (fndecl))
6463 /* A thunk. We should never be calling this entry directly
6464 from this vtable -- we'd use the entry for the non
6465 thunk base function. */
6466 DECL_VINDEX (fndecl) = NULL_TREE;
6467 else if (TREE_CODE (DECL_VINDEX (fndecl)) != INTEGER_CST)
6468 DECL_VINDEX (fndecl) = build_int_cst (NULL_TREE, vindex);
6472 finish_struct_bits (t);
6473 set_method_tm_attributes (t);
6475 /* Complete the rtl for any static member objects of the type we're
6476 working on. */
6477 for (x = TYPE_FIELDS (t); x; x = DECL_CHAIN (x))
6478 if (VAR_P (x) && TREE_STATIC (x)
6479 && TREE_TYPE (x) != error_mark_node
6480 && same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (x)), t))
6481 DECL_MODE (x) = TYPE_MODE (t);
6483 /* Done with FIELDS...now decide whether to sort these for
6484 faster lookups later.
6486 We use a small number because most searches fail (succeeding
6487 ultimately as the search bores through the inheritance
6488 hierarchy), and we want this failure to occur quickly. */
6490 insert_into_classtype_sorted_fields (TYPE_FIELDS (t), t, 8);
6492 /* Complain if one of the field types requires lower visibility. */
6493 constrain_class_visibility (t);
6495 /* Make the rtl for any new vtables we have created, and unmark
6496 the base types we marked. */
6497 finish_vtbls (t);
6499 /* Build the VTT for T. */
6500 build_vtt (t);
6502 /* This warning does not make sense for Java classes, since they
6503 cannot have destructors. */
6504 if (!TYPE_FOR_JAVA (t) && warn_nonvdtor && TYPE_POLYMORPHIC_P (t))
6506 tree dtor;
6508 dtor = CLASSTYPE_DESTRUCTORS (t);
6509 if (/* An implicitly declared destructor is always public. And,
6510 if it were virtual, we would have created it by now. */
6511 !dtor
6512 || (!DECL_VINDEX (dtor)
6513 && (/* public non-virtual */
6514 (!TREE_PRIVATE (dtor) && !TREE_PROTECTED (dtor))
6515 || (/* non-public non-virtual with friends */
6516 (TREE_PRIVATE (dtor) || TREE_PROTECTED (dtor))
6517 && (CLASSTYPE_FRIEND_CLASSES (t)
6518 || DECL_FRIENDLIST (TYPE_MAIN_DECL (t)))))))
6519 warning (OPT_Wnon_virtual_dtor,
6520 "%q#T has virtual functions and accessible"
6521 " non-virtual destructor", t);
6524 complete_vars (t);
6526 if (warn_overloaded_virtual)
6527 warn_hidden (t);
6529 /* Class layout, assignment of virtual table slots, etc., is now
6530 complete. Give the back end a chance to tweak the visibility of
6531 the class or perform any other required target modifications. */
6532 targetm.cxx.adjust_class_at_definition (t);
6534 maybe_suppress_debug_info (t);
6536 if (flag_vtable_verify)
6537 vtv_save_class_info (t);
6539 dump_class_hierarchy (t);
6541 /* Finish debugging output for this type. */
6542 rest_of_type_compilation (t, ! LOCAL_CLASS_P (t));
6544 if (TYPE_TRANSPARENT_AGGR (t))
6546 tree field = first_field (t);
6547 if (field == NULL_TREE || error_operand_p (field))
6549 error ("type transparent %q#T does not have any fields", t);
6550 TYPE_TRANSPARENT_AGGR (t) = 0;
6552 else if (DECL_ARTIFICIAL (field))
6554 if (DECL_FIELD_IS_BASE (field))
6555 error ("type transparent class %qT has base classes", t);
6556 else
6558 gcc_checking_assert (DECL_VIRTUAL_P (field));
6559 error ("type transparent class %qT has virtual functions", t);
6561 TYPE_TRANSPARENT_AGGR (t) = 0;
6563 else if (TYPE_MODE (t) != DECL_MODE (field))
6565 error ("type transparent %q#T cannot be made transparent because "
6566 "the type of the first field has a different ABI from the "
6567 "class overall", t);
6568 TYPE_TRANSPARENT_AGGR (t) = 0;
6573 /* Insert FIELDS into T for the sorted case if the FIELDS count is
6574 equal to THRESHOLD or greater than THRESHOLD. */
6576 static void
6577 insert_into_classtype_sorted_fields (tree fields, tree t, int threshold)
6579 int n_fields = count_fields (fields);
6580 if (n_fields >= threshold)
6582 struct sorted_fields_type *field_vec = sorted_fields_type_new (n_fields);
6583 add_fields_to_record_type (fields, field_vec, 0);
6584 qsort (field_vec->elts, n_fields, sizeof (tree), field_decl_cmp);
6585 CLASSTYPE_SORTED_FIELDS (t) = field_vec;
6589 /* Insert lately defined enum ENUMTYPE into T for the sorted case. */
6591 void
6592 insert_late_enum_def_into_classtype_sorted_fields (tree enumtype, tree t)
6594 struct sorted_fields_type *sorted_fields = CLASSTYPE_SORTED_FIELDS (t);
6595 if (sorted_fields)
6597 int i;
6598 int n_fields
6599 = list_length (TYPE_VALUES (enumtype)) + sorted_fields->len;
6600 struct sorted_fields_type *field_vec = sorted_fields_type_new (n_fields);
6602 for (i = 0; i < sorted_fields->len; ++i)
6603 field_vec->elts[i] = sorted_fields->elts[i];
6605 add_enum_fields_to_record_type (enumtype, field_vec,
6606 sorted_fields->len);
6607 qsort (field_vec->elts, n_fields, sizeof (tree), field_decl_cmp);
6608 CLASSTYPE_SORTED_FIELDS (t) = field_vec;
6612 /* When T was built up, the member declarations were added in reverse
6613 order. Rearrange them to declaration order. */
6615 void
6616 unreverse_member_declarations (tree t)
6618 tree next;
6619 tree prev;
6620 tree x;
6622 /* The following lists are all in reverse order. Put them in
6623 declaration order now. */
6624 TYPE_METHODS (t) = nreverse (TYPE_METHODS (t));
6625 CLASSTYPE_DECL_LIST (t) = nreverse (CLASSTYPE_DECL_LIST (t));
6627 /* Actually, for the TYPE_FIELDS, only the non TYPE_DECLs are in
6628 reverse order, so we can't just use nreverse. */
6629 prev = NULL_TREE;
6630 for (x = TYPE_FIELDS (t);
6631 x && TREE_CODE (x) != TYPE_DECL;
6632 x = next)
6634 next = DECL_CHAIN (x);
6635 DECL_CHAIN (x) = prev;
6636 prev = x;
6638 if (prev)
6640 DECL_CHAIN (TYPE_FIELDS (t)) = x;
6641 if (prev)
6642 TYPE_FIELDS (t) = prev;
6646 tree
6647 finish_struct (tree t, tree attributes)
6649 location_t saved_loc = input_location;
6651 /* Now that we've got all the field declarations, reverse everything
6652 as necessary. */
6653 unreverse_member_declarations (t);
6655 cplus_decl_attributes (&t, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
6657 /* Nadger the current location so that diagnostics point to the start of
6658 the struct, not the end. */
6659 input_location = DECL_SOURCE_LOCATION (TYPE_NAME (t));
6661 if (processing_template_decl)
6663 tree x;
6665 finish_struct_methods (t);
6666 TYPE_SIZE (t) = bitsize_zero_node;
6667 TYPE_SIZE_UNIT (t) = size_zero_node;
6669 /* We need to emit an error message if this type was used as a parameter
6670 and it is an abstract type, even if it is a template. We construct
6671 a simple CLASSTYPE_PURE_VIRTUALS list without taking bases into
6672 account and we call complete_vars with this type, which will check
6673 the PARM_DECLS. Note that while the type is being defined,
6674 CLASSTYPE_PURE_VIRTUALS contains the list of the inline friends
6675 (see CLASSTYPE_INLINE_FRIENDS) so we need to clear it. */
6676 CLASSTYPE_PURE_VIRTUALS (t) = NULL;
6677 for (x = TYPE_METHODS (t); x; x = DECL_CHAIN (x))
6678 if (DECL_PURE_VIRTUAL_P (x))
6679 vec_safe_push (CLASSTYPE_PURE_VIRTUALS (t), x);
6680 complete_vars (t);
6681 /* We need to add the target functions to the CLASSTYPE_METHOD_VEC if
6682 an enclosing scope is a template class, so that this function be
6683 found by lookup_fnfields_1 when the using declaration is not
6684 instantiated yet. */
6685 for (x = TYPE_FIELDS (t); x; x = DECL_CHAIN (x))
6686 if (TREE_CODE (x) == USING_DECL)
6688 tree fn = strip_using_decl (x);
6689 if (is_overloaded_fn (fn))
6690 for (; fn; fn = OVL_NEXT (fn))
6691 add_method (t, OVL_CURRENT (fn), x);
6694 /* Remember current #pragma pack value. */
6695 TYPE_PRECISION (t) = maximum_field_alignment;
6697 /* Fix up any variants we've already built. */
6698 for (x = TYPE_NEXT_VARIANT (t); x; x = TYPE_NEXT_VARIANT (x))
6700 TYPE_SIZE (x) = TYPE_SIZE (t);
6701 TYPE_SIZE_UNIT (x) = TYPE_SIZE_UNIT (t);
6702 TYPE_FIELDS (x) = TYPE_FIELDS (t);
6703 TYPE_METHODS (x) = TYPE_METHODS (t);
6706 else
6707 finish_struct_1 (t);
6709 input_location = saved_loc;
6711 TYPE_BEING_DEFINED (t) = 0;
6713 if (current_class_type)
6714 popclass ();
6715 else
6716 error ("trying to finish struct, but kicked out due to previous parse errors");
6718 if (processing_template_decl && at_function_scope_p ()
6719 /* Lambdas are defined by the LAMBDA_EXPR. */
6720 && !LAMBDA_TYPE_P (t))
6721 add_stmt (build_min (TAG_DEFN, t));
6723 return t;
6726 /* Hash table to avoid endless recursion when handling references. */
6727 static hash_table <pointer_hash <tree_node> > fixed_type_or_null_ref_ht;
6729 /* Return the dynamic type of INSTANCE, if known.
6730 Used to determine whether the virtual function table is needed
6731 or not.
6733 *NONNULL is set iff INSTANCE can be known to be nonnull, regardless
6734 of our knowledge of its type. *NONNULL should be initialized
6735 before this function is called. */
6737 static tree
6738 fixed_type_or_null (tree instance, int *nonnull, int *cdtorp)
6740 #define RECUR(T) fixed_type_or_null((T), nonnull, cdtorp)
6742 switch (TREE_CODE (instance))
6744 case INDIRECT_REF:
6745 if (POINTER_TYPE_P (TREE_TYPE (instance)))
6746 return NULL_TREE;
6747 else
6748 return RECUR (TREE_OPERAND (instance, 0));
6750 case CALL_EXPR:
6751 /* This is a call to a constructor, hence it's never zero. */
6752 if (TREE_HAS_CONSTRUCTOR (instance))
6754 if (nonnull)
6755 *nonnull = 1;
6756 return TREE_TYPE (instance);
6758 return NULL_TREE;
6760 case SAVE_EXPR:
6761 /* This is a call to a constructor, hence it's never zero. */
6762 if (TREE_HAS_CONSTRUCTOR (instance))
6764 if (nonnull)
6765 *nonnull = 1;
6766 return TREE_TYPE (instance);
6768 return RECUR (TREE_OPERAND (instance, 0));
6770 case POINTER_PLUS_EXPR:
6771 case PLUS_EXPR:
6772 case MINUS_EXPR:
6773 if (TREE_CODE (TREE_OPERAND (instance, 0)) == ADDR_EXPR)
6774 return RECUR (TREE_OPERAND (instance, 0));
6775 if (TREE_CODE (TREE_OPERAND (instance, 1)) == INTEGER_CST)
6776 /* Propagate nonnull. */
6777 return RECUR (TREE_OPERAND (instance, 0));
6779 return NULL_TREE;
6781 CASE_CONVERT:
6782 return RECUR (TREE_OPERAND (instance, 0));
6784 case ADDR_EXPR:
6785 instance = TREE_OPERAND (instance, 0);
6786 if (nonnull)
6788 /* Just because we see an ADDR_EXPR doesn't mean we're dealing
6789 with a real object -- given &p->f, p can still be null. */
6790 tree t = get_base_address (instance);
6791 /* ??? Probably should check DECL_WEAK here. */
6792 if (t && DECL_P (t))
6793 *nonnull = 1;
6795 return RECUR (instance);
6797 case COMPONENT_REF:
6798 /* If this component is really a base class reference, then the field
6799 itself isn't definitive. */
6800 if (DECL_FIELD_IS_BASE (TREE_OPERAND (instance, 1)))
6801 return RECUR (TREE_OPERAND (instance, 0));
6802 return RECUR (TREE_OPERAND (instance, 1));
6804 case VAR_DECL:
6805 case FIELD_DECL:
6806 if (TREE_CODE (TREE_TYPE (instance)) == ARRAY_TYPE
6807 && MAYBE_CLASS_TYPE_P (TREE_TYPE (TREE_TYPE (instance))))
6809 if (nonnull)
6810 *nonnull = 1;
6811 return TREE_TYPE (TREE_TYPE (instance));
6813 /* fall through... */
6814 case TARGET_EXPR:
6815 case PARM_DECL:
6816 case RESULT_DECL:
6817 if (MAYBE_CLASS_TYPE_P (TREE_TYPE (instance)))
6819 if (nonnull)
6820 *nonnull = 1;
6821 return TREE_TYPE (instance);
6823 else if (instance == current_class_ptr)
6825 if (nonnull)
6826 *nonnull = 1;
6828 /* if we're in a ctor or dtor, we know our type. If
6829 current_class_ptr is set but we aren't in a function, we're in
6830 an NSDMI (and therefore a constructor). */
6831 if (current_scope () != current_function_decl
6832 || (DECL_LANG_SPECIFIC (current_function_decl)
6833 && (DECL_CONSTRUCTOR_P (current_function_decl)
6834 || DECL_DESTRUCTOR_P (current_function_decl))))
6836 if (cdtorp)
6837 *cdtorp = 1;
6838 return TREE_TYPE (TREE_TYPE (instance));
6841 else if (TREE_CODE (TREE_TYPE (instance)) == REFERENCE_TYPE)
6843 /* We only need one hash table because it is always left empty. */
6844 if (!fixed_type_or_null_ref_ht.is_created ())
6845 fixed_type_or_null_ref_ht.create (37);
6847 /* Reference variables should be references to objects. */
6848 if (nonnull)
6849 *nonnull = 1;
6851 /* Enter the INSTANCE in a table to prevent recursion; a
6852 variable's initializer may refer to the variable
6853 itself. */
6854 if (VAR_P (instance)
6855 && DECL_INITIAL (instance)
6856 && !type_dependent_expression_p_push (DECL_INITIAL (instance))
6857 && !fixed_type_or_null_ref_ht.find (instance))
6859 tree type;
6860 tree_node **slot;
6862 slot = fixed_type_or_null_ref_ht.find_slot (instance, INSERT);
6863 *slot = instance;
6864 type = RECUR (DECL_INITIAL (instance));
6865 fixed_type_or_null_ref_ht.remove_elt (instance);
6867 return type;
6870 return NULL_TREE;
6872 default:
6873 return NULL_TREE;
6875 #undef RECUR
6878 /* Return nonzero if the dynamic type of INSTANCE is known, and
6879 equivalent to the static type. We also handle the case where
6880 INSTANCE is really a pointer. Return negative if this is a
6881 ctor/dtor. There the dynamic type is known, but this might not be
6882 the most derived base of the original object, and hence virtual
6883 bases may not be laid out according to this type.
6885 Used to determine whether the virtual function table is needed
6886 or not.
6888 *NONNULL is set iff INSTANCE can be known to be nonnull, regardless
6889 of our knowledge of its type. *NONNULL should be initialized
6890 before this function is called. */
6893 resolves_to_fixed_type_p (tree instance, int* nonnull)
6895 tree t = TREE_TYPE (instance);
6896 int cdtorp = 0;
6897 tree fixed;
6899 /* processing_template_decl can be false in a template if we're in
6900 fold_non_dependent_expr, but we still want to suppress this check. */
6901 if (in_template_function ())
6903 /* In a template we only care about the type of the result. */
6904 if (nonnull)
6905 *nonnull = true;
6906 return true;
6909 fixed = fixed_type_or_null (instance, nonnull, &cdtorp);
6910 if (fixed == NULL_TREE)
6911 return 0;
6912 if (POINTER_TYPE_P (t))
6913 t = TREE_TYPE (t);
6914 if (!same_type_ignoring_top_level_qualifiers_p (t, fixed))
6915 return 0;
6916 return cdtorp ? -1 : 1;
6920 void
6921 init_class_processing (void)
6923 current_class_depth = 0;
6924 current_class_stack_size = 10;
6925 current_class_stack
6926 = XNEWVEC (struct class_stack_node, current_class_stack_size);
6927 vec_alloc (local_classes, 8);
6928 sizeof_biggest_empty_class = size_zero_node;
6930 ridpointers[(int) RID_PUBLIC] = access_public_node;
6931 ridpointers[(int) RID_PRIVATE] = access_private_node;
6932 ridpointers[(int) RID_PROTECTED] = access_protected_node;
6935 /* Restore the cached PREVIOUS_CLASS_LEVEL. */
6937 static void
6938 restore_class_cache (void)
6940 tree type;
6942 /* We are re-entering the same class we just left, so we don't
6943 have to search the whole inheritance matrix to find all the
6944 decls to bind again. Instead, we install the cached
6945 class_shadowed list and walk through it binding names. */
6946 push_binding_level (previous_class_level);
6947 class_binding_level = previous_class_level;
6948 /* Restore IDENTIFIER_TYPE_VALUE. */
6949 for (type = class_binding_level->type_shadowed;
6950 type;
6951 type = TREE_CHAIN (type))
6952 SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (type), TREE_TYPE (type));
6955 /* Set global variables CURRENT_CLASS_NAME and CURRENT_CLASS_TYPE as
6956 appropriate for TYPE.
6958 So that we may avoid calls to lookup_name, we cache the _TYPE
6959 nodes of local TYPE_DECLs in the TREE_TYPE field of the name.
6961 For multiple inheritance, we perform a two-pass depth-first search
6962 of the type lattice. */
6964 void
6965 pushclass (tree type)
6967 class_stack_node_t csn;
6969 type = TYPE_MAIN_VARIANT (type);
6971 /* Make sure there is enough room for the new entry on the stack. */
6972 if (current_class_depth + 1 >= current_class_stack_size)
6974 current_class_stack_size *= 2;
6975 current_class_stack
6976 = XRESIZEVEC (struct class_stack_node, current_class_stack,
6977 current_class_stack_size);
6980 /* Insert a new entry on the class stack. */
6981 csn = current_class_stack + current_class_depth;
6982 csn->name = current_class_name;
6983 csn->type = current_class_type;
6984 csn->access = current_access_specifier;
6985 csn->names_used = 0;
6986 csn->hidden = 0;
6987 current_class_depth++;
6989 /* Now set up the new type. */
6990 current_class_name = TYPE_NAME (type);
6991 if (TREE_CODE (current_class_name) == TYPE_DECL)
6992 current_class_name = DECL_NAME (current_class_name);
6993 current_class_type = type;
6995 /* By default, things in classes are private, while things in
6996 structures or unions are public. */
6997 current_access_specifier = (CLASSTYPE_DECLARED_CLASS (type)
6998 ? access_private_node
6999 : access_public_node);
7001 if (previous_class_level
7002 && type != previous_class_level->this_entity
7003 && current_class_depth == 1)
7005 /* Forcibly remove any old class remnants. */
7006 invalidate_class_lookup_cache ();
7009 if (!previous_class_level
7010 || type != previous_class_level->this_entity
7011 || current_class_depth > 1)
7012 pushlevel_class ();
7013 else
7014 restore_class_cache ();
7017 /* When we exit a toplevel class scope, we save its binding level so
7018 that we can restore it quickly. Here, we've entered some other
7019 class, so we must invalidate our cache. */
7021 void
7022 invalidate_class_lookup_cache (void)
7024 previous_class_level = NULL;
7027 /* Get out of the current class scope. If we were in a class scope
7028 previously, that is the one popped to. */
7030 void
7031 popclass (void)
7033 poplevel_class ();
7035 current_class_depth--;
7036 current_class_name = current_class_stack[current_class_depth].name;
7037 current_class_type = current_class_stack[current_class_depth].type;
7038 current_access_specifier = current_class_stack[current_class_depth].access;
7039 if (current_class_stack[current_class_depth].names_used)
7040 splay_tree_delete (current_class_stack[current_class_depth].names_used);
7043 /* Mark the top of the class stack as hidden. */
7045 void
7046 push_class_stack (void)
7048 if (current_class_depth)
7049 ++current_class_stack[current_class_depth - 1].hidden;
7052 /* Mark the top of the class stack as un-hidden. */
7054 void
7055 pop_class_stack (void)
7057 if (current_class_depth)
7058 --current_class_stack[current_class_depth - 1].hidden;
7061 /* Returns 1 if the class type currently being defined is either T or
7062 a nested type of T. */
7064 bool
7065 currently_open_class (tree t)
7067 int i;
7069 if (!CLASS_TYPE_P (t))
7070 return false;
7072 t = TYPE_MAIN_VARIANT (t);
7074 /* We start looking from 1 because entry 0 is from global scope,
7075 and has no type. */
7076 for (i = current_class_depth; i > 0; --i)
7078 tree c;
7079 if (i == current_class_depth)
7080 c = current_class_type;
7081 else
7083 if (current_class_stack[i].hidden)
7084 break;
7085 c = current_class_stack[i].type;
7087 if (!c)
7088 continue;
7089 if (same_type_p (c, t))
7090 return true;
7092 return false;
7095 /* If either current_class_type or one of its enclosing classes are derived
7096 from T, return the appropriate type. Used to determine how we found
7097 something via unqualified lookup. */
7099 tree
7100 currently_open_derived_class (tree t)
7102 int i;
7104 /* The bases of a dependent type are unknown. */
7105 if (dependent_type_p (t))
7106 return NULL_TREE;
7108 if (!current_class_type)
7109 return NULL_TREE;
7111 if (DERIVED_FROM_P (t, current_class_type))
7112 return current_class_type;
7114 for (i = current_class_depth - 1; i > 0; --i)
7116 if (current_class_stack[i].hidden)
7117 break;
7118 if (DERIVED_FROM_P (t, current_class_stack[i].type))
7119 return current_class_stack[i].type;
7122 return NULL_TREE;
7125 /* Returns the innermost class type which is not a lambda closure type. */
7127 tree
7128 current_nonlambda_class_type (void)
7130 int i;
7132 /* We start looking from 1 because entry 0 is from global scope,
7133 and has no type. */
7134 for (i = current_class_depth; i > 0; --i)
7136 tree c;
7137 if (i == current_class_depth)
7138 c = current_class_type;
7139 else
7141 if (current_class_stack[i].hidden)
7142 break;
7143 c = current_class_stack[i].type;
7145 if (!c)
7146 continue;
7147 if (!LAMBDA_TYPE_P (c))
7148 return c;
7150 return NULL_TREE;
7153 /* When entering a class scope, all enclosing class scopes' names with
7154 static meaning (static variables, static functions, types and
7155 enumerators) have to be visible. This recursive function calls
7156 pushclass for all enclosing class contexts until global or a local
7157 scope is reached. TYPE is the enclosed class. */
7159 void
7160 push_nested_class (tree type)
7162 /* A namespace might be passed in error cases, like A::B:C. */
7163 if (type == NULL_TREE
7164 || !CLASS_TYPE_P (type))
7165 return;
7167 push_nested_class (DECL_CONTEXT (TYPE_MAIN_DECL (type)));
7169 pushclass (type);
7172 /* Undoes a push_nested_class call. */
7174 void
7175 pop_nested_class (void)
7177 tree context = DECL_CONTEXT (TYPE_MAIN_DECL (current_class_type));
7179 popclass ();
7180 if (context && CLASS_TYPE_P (context))
7181 pop_nested_class ();
7184 /* Returns the number of extern "LANG" blocks we are nested within. */
7187 current_lang_depth (void)
7189 return vec_safe_length (current_lang_base);
7192 /* Set global variables CURRENT_LANG_NAME to appropriate value
7193 so that behavior of name-mangling machinery is correct. */
7195 void
7196 push_lang_context (tree name)
7198 vec_safe_push (current_lang_base, current_lang_name);
7200 if (name == lang_name_cplusplus)
7202 current_lang_name = name;
7204 else if (name == lang_name_java)
7206 current_lang_name = name;
7207 /* DECL_IGNORED_P is initially set for these types, to avoid clutter.
7208 (See record_builtin_java_type in decl.c.) However, that causes
7209 incorrect debug entries if these types are actually used.
7210 So we re-enable debug output after extern "Java". */
7211 DECL_IGNORED_P (TYPE_NAME (java_byte_type_node)) = 0;
7212 DECL_IGNORED_P (TYPE_NAME (java_short_type_node)) = 0;
7213 DECL_IGNORED_P (TYPE_NAME (java_int_type_node)) = 0;
7214 DECL_IGNORED_P (TYPE_NAME (java_long_type_node)) = 0;
7215 DECL_IGNORED_P (TYPE_NAME (java_float_type_node)) = 0;
7216 DECL_IGNORED_P (TYPE_NAME (java_double_type_node)) = 0;
7217 DECL_IGNORED_P (TYPE_NAME (java_char_type_node)) = 0;
7218 DECL_IGNORED_P (TYPE_NAME (java_boolean_type_node)) = 0;
7220 else if (name == lang_name_c)
7222 current_lang_name = name;
7224 else
7225 error ("language string %<\"%E\"%> not recognized", name);
7228 /* Get out of the current language scope. */
7230 void
7231 pop_lang_context (void)
7233 current_lang_name = current_lang_base->pop ();
7236 /* Type instantiation routines. */
7238 /* Given an OVERLOAD and a TARGET_TYPE, return the function that
7239 matches the TARGET_TYPE. If there is no satisfactory match, return
7240 error_mark_node, and issue an error & warning messages under
7241 control of FLAGS. Permit pointers to member function if FLAGS
7242 permits. If TEMPLATE_ONLY, the name of the overloaded function was
7243 a template-id, and EXPLICIT_TARGS are the explicitly provided
7244 template arguments.
7246 If OVERLOAD is for one or more member functions, then ACCESS_PATH
7247 is the base path used to reference those member functions. If
7248 the address is resolved to a member function, access checks will be
7249 performed and errors issued if appropriate. */
7251 static tree
7252 resolve_address_of_overloaded_function (tree target_type,
7253 tree overload,
7254 tsubst_flags_t flags,
7255 bool template_only,
7256 tree explicit_targs,
7257 tree access_path)
7259 /* Here's what the standard says:
7261 [over.over]
7263 If the name is a function template, template argument deduction
7264 is done, and if the argument deduction succeeds, the deduced
7265 arguments are used to generate a single template function, which
7266 is added to the set of overloaded functions considered.
7268 Non-member functions and static member functions match targets of
7269 type "pointer-to-function" or "reference-to-function." Nonstatic
7270 member functions match targets of type "pointer-to-member
7271 function;" the function type of the pointer to member is used to
7272 select the member function from the set of overloaded member
7273 functions. If a nonstatic member function is selected, the
7274 reference to the overloaded function name is required to have the
7275 form of a pointer to member as described in 5.3.1.
7277 If more than one function is selected, any template functions in
7278 the set are eliminated if the set also contains a non-template
7279 function, and any given template function is eliminated if the
7280 set contains a second template function that is more specialized
7281 than the first according to the partial ordering rules 14.5.5.2.
7282 After such eliminations, if any, there shall remain exactly one
7283 selected function. */
7285 int is_ptrmem = 0;
7286 /* We store the matches in a TREE_LIST rooted here. The functions
7287 are the TREE_PURPOSE, not the TREE_VALUE, in this list, for easy
7288 interoperability with most_specialized_instantiation. */
7289 tree matches = NULL_TREE;
7290 tree fn;
7291 tree target_fn_type;
7293 /* By the time we get here, we should be seeing only real
7294 pointer-to-member types, not the internal POINTER_TYPE to
7295 METHOD_TYPE representation. */
7296 gcc_assert (!TYPE_PTR_P (target_type)
7297 || TREE_CODE (TREE_TYPE (target_type)) != METHOD_TYPE);
7299 gcc_assert (is_overloaded_fn (overload));
7301 /* Check that the TARGET_TYPE is reasonable. */
7302 if (TYPE_PTRFN_P (target_type)
7303 || TYPE_REFFN_P (target_type))
7304 /* This is OK. */;
7305 else if (TYPE_PTRMEMFUNC_P (target_type))
7306 /* This is OK, too. */
7307 is_ptrmem = 1;
7308 else if (TREE_CODE (target_type) == FUNCTION_TYPE)
7309 /* This is OK, too. This comes from a conversion to reference
7310 type. */
7311 target_type = build_reference_type (target_type);
7312 else
7314 if (flags & tf_error)
7315 error ("cannot resolve overloaded function %qD based on"
7316 " conversion to type %qT",
7317 DECL_NAME (OVL_FUNCTION (overload)), target_type);
7318 return error_mark_node;
7321 /* Non-member functions and static member functions match targets of type
7322 "pointer-to-function" or "reference-to-function." Nonstatic member
7323 functions match targets of type "pointer-to-member-function;" the
7324 function type of the pointer to member is used to select the member
7325 function from the set of overloaded member functions.
7327 So figure out the FUNCTION_TYPE that we want to match against. */
7328 target_fn_type = static_fn_type (target_type);
7330 /* If we can find a non-template function that matches, we can just
7331 use it. There's no point in generating template instantiations
7332 if we're just going to throw them out anyhow. But, of course, we
7333 can only do this when we don't *need* a template function. */
7334 if (!template_only)
7336 tree fns;
7338 for (fns = overload; fns; fns = OVL_NEXT (fns))
7340 tree fn = OVL_CURRENT (fns);
7342 if (TREE_CODE (fn) == TEMPLATE_DECL)
7343 /* We're not looking for templates just yet. */
7344 continue;
7346 if ((TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
7347 != is_ptrmem)
7348 /* We're looking for a non-static member, and this isn't
7349 one, or vice versa. */
7350 continue;
7352 /* Ignore functions which haven't been explicitly
7353 declared. */
7354 if (DECL_ANTICIPATED (fn))
7355 continue;
7357 /* See if there's a match. */
7358 if (same_type_p (target_fn_type, static_fn_type (fn)))
7359 matches = tree_cons (fn, NULL_TREE, matches);
7363 /* Now, if we've already got a match (or matches), there's no need
7364 to proceed to the template functions. But, if we don't have a
7365 match we need to look at them, too. */
7366 if (!matches)
7368 tree target_arg_types;
7369 tree target_ret_type;
7370 tree fns;
7371 tree *args;
7372 unsigned int nargs, ia;
7373 tree arg;
7375 target_arg_types = TYPE_ARG_TYPES (target_fn_type);
7376 target_ret_type = TREE_TYPE (target_fn_type);
7378 nargs = list_length (target_arg_types);
7379 args = XALLOCAVEC (tree, nargs);
7380 for (arg = target_arg_types, ia = 0;
7381 arg != NULL_TREE && arg != void_list_node;
7382 arg = TREE_CHAIN (arg), ++ia)
7383 args[ia] = TREE_VALUE (arg);
7384 nargs = ia;
7386 for (fns = overload; fns; fns = OVL_NEXT (fns))
7388 tree fn = OVL_CURRENT (fns);
7389 tree instantiation;
7390 tree targs;
7392 if (TREE_CODE (fn) != TEMPLATE_DECL)
7393 /* We're only looking for templates. */
7394 continue;
7396 if ((TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
7397 != is_ptrmem)
7398 /* We're not looking for a non-static member, and this is
7399 one, or vice versa. */
7400 continue;
7402 tree ret = target_ret_type;
7404 /* If the template has a deduced return type, don't expose it to
7405 template argument deduction. */
7406 if (undeduced_auto_decl (fn))
7407 ret = NULL_TREE;
7409 /* Try to do argument deduction. */
7410 targs = make_tree_vec (DECL_NTPARMS (fn));
7411 instantiation = fn_type_unification (fn, explicit_targs, targs, args,
7412 nargs, ret,
7413 DEDUCE_EXACT, LOOKUP_NORMAL,
7414 false, false);
7415 if (instantiation == error_mark_node)
7416 /* Instantiation failed. */
7417 continue;
7419 /* And now force instantiation to do return type deduction. */
7420 if (undeduced_auto_decl (instantiation))
7422 ++function_depth;
7423 instantiate_decl (instantiation, /*defer*/false, /*class*/false);
7424 --function_depth;
7426 require_deduced_type (instantiation);
7429 /* See if there's a match. */
7430 if (same_type_p (target_fn_type, static_fn_type (instantiation)))
7431 matches = tree_cons (instantiation, fn, matches);
7433 ggc_free (targs);
7436 /* Now, remove all but the most specialized of the matches. */
7437 if (matches)
7439 tree match = most_specialized_instantiation (matches);
7441 if (match != error_mark_node)
7442 matches = tree_cons (TREE_PURPOSE (match),
7443 NULL_TREE,
7444 NULL_TREE);
7448 /* Now we should have exactly one function in MATCHES. */
7449 if (matches == NULL_TREE)
7451 /* There were *no* matches. */
7452 if (flags & tf_error)
7454 error ("no matches converting function %qD to type %q#T",
7455 DECL_NAME (OVL_CURRENT (overload)),
7456 target_type);
7458 print_candidates (overload);
7460 return error_mark_node;
7462 else if (TREE_CHAIN (matches))
7464 /* There were too many matches. First check if they're all
7465 the same function. */
7466 tree match = NULL_TREE;
7468 fn = TREE_PURPOSE (matches);
7470 /* For multi-versioned functions, more than one match is just fine and
7471 decls_match will return false as they are different. */
7472 for (match = TREE_CHAIN (matches); match; match = TREE_CHAIN (match))
7473 if (!decls_match (fn, TREE_PURPOSE (match))
7474 && !targetm.target_option.function_versions
7475 (fn, TREE_PURPOSE (match)))
7476 break;
7478 if (match)
7480 if (flags & tf_error)
7482 error ("converting overloaded function %qD to type %q#T is ambiguous",
7483 DECL_NAME (OVL_FUNCTION (overload)),
7484 target_type);
7486 /* Since print_candidates expects the functions in the
7487 TREE_VALUE slot, we flip them here. */
7488 for (match = matches; match; match = TREE_CHAIN (match))
7489 TREE_VALUE (match) = TREE_PURPOSE (match);
7491 print_candidates (matches);
7494 return error_mark_node;
7498 /* Good, exactly one match. Now, convert it to the correct type. */
7499 fn = TREE_PURPOSE (matches);
7501 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
7502 && !(flags & tf_ptrmem_ok) && !flag_ms_extensions)
7504 static int explained;
7506 if (!(flags & tf_error))
7507 return error_mark_node;
7509 permerror (input_location, "assuming pointer to member %qD", fn);
7510 if (!explained)
7512 inform (input_location, "(a pointer to member can only be formed with %<&%E%>)", fn);
7513 explained = 1;
7517 /* If a pointer to a function that is multi-versioned is requested, the
7518 pointer to the dispatcher function is returned instead. This works
7519 well because indirectly calling the function will dispatch the right
7520 function version at run-time. */
7521 if (DECL_FUNCTION_VERSIONED (fn))
7523 fn = get_function_version_dispatcher (fn);
7524 if (fn == NULL)
7525 return error_mark_node;
7526 /* Mark all the versions corresponding to the dispatcher as used. */
7527 if (!(flags & tf_conv))
7528 mark_versions_used (fn);
7531 /* If we're doing overload resolution purely for the purpose of
7532 determining conversion sequences, we should not consider the
7533 function used. If this conversion sequence is selected, the
7534 function will be marked as used at this point. */
7535 if (!(flags & tf_conv))
7537 /* Make =delete work with SFINAE. */
7538 if (DECL_DELETED_FN (fn) && !(flags & tf_error))
7539 return error_mark_node;
7541 mark_used (fn);
7544 /* We could not check access to member functions when this
7545 expression was originally created since we did not know at that
7546 time to which function the expression referred. */
7547 if (DECL_FUNCTION_MEMBER_P (fn))
7549 gcc_assert (access_path);
7550 perform_or_defer_access_check (access_path, fn, fn, flags);
7553 if (TYPE_PTRFN_P (target_type) || TYPE_PTRMEMFUNC_P (target_type))
7554 return cp_build_addr_expr (fn, flags);
7555 else
7557 /* The target must be a REFERENCE_TYPE. Above, cp_build_unary_op
7558 will mark the function as addressed, but here we must do it
7559 explicitly. */
7560 cxx_mark_addressable (fn);
7562 return fn;
7566 /* This function will instantiate the type of the expression given in
7567 RHS to match the type of LHSTYPE. If errors exist, then return
7568 error_mark_node. FLAGS is a bit mask. If TF_ERROR is set, then
7569 we complain on errors. If we are not complaining, never modify rhs,
7570 as overload resolution wants to try many possible instantiations, in
7571 the hope that at least one will work.
7573 For non-recursive calls, LHSTYPE should be a function, pointer to
7574 function, or a pointer to member function. */
7576 tree
7577 instantiate_type (tree lhstype, tree rhs, tsubst_flags_t flags)
7579 tsubst_flags_t flags_in = flags;
7580 tree access_path = NULL_TREE;
7582 flags &= ~tf_ptrmem_ok;
7584 if (lhstype == unknown_type_node)
7586 if (flags & tf_error)
7587 error ("not enough type information");
7588 return error_mark_node;
7591 if (TREE_TYPE (rhs) != NULL_TREE && ! (type_unknown_p (rhs)))
7593 tree fntype = non_reference (lhstype);
7594 if (same_type_p (fntype, TREE_TYPE (rhs)))
7595 return rhs;
7596 if (flag_ms_extensions
7597 && TYPE_PTRMEMFUNC_P (fntype)
7598 && !TYPE_PTRMEMFUNC_P (TREE_TYPE (rhs)))
7599 /* Microsoft allows `A::f' to be resolved to a
7600 pointer-to-member. */
7602 else
7604 if (flags & tf_error)
7605 error ("cannot convert %qE from type %qT to type %qT",
7606 rhs, TREE_TYPE (rhs), fntype);
7607 return error_mark_node;
7611 if (BASELINK_P (rhs))
7613 access_path = BASELINK_ACCESS_BINFO (rhs);
7614 rhs = BASELINK_FUNCTIONS (rhs);
7617 /* If we are in a template, and have a NON_DEPENDENT_EXPR, we cannot
7618 deduce any type information. */
7619 if (TREE_CODE (rhs) == NON_DEPENDENT_EXPR)
7621 if (flags & tf_error)
7622 error ("not enough type information");
7623 return error_mark_node;
7626 /* There only a few kinds of expressions that may have a type
7627 dependent on overload resolution. */
7628 gcc_assert (TREE_CODE (rhs) == ADDR_EXPR
7629 || TREE_CODE (rhs) == COMPONENT_REF
7630 || really_overloaded_fn (rhs)
7631 || (flag_ms_extensions && TREE_CODE (rhs) == FUNCTION_DECL));
7633 /* This should really only be used when attempting to distinguish
7634 what sort of a pointer to function we have. For now, any
7635 arithmetic operation which is not supported on pointers
7636 is rejected as an error. */
7638 switch (TREE_CODE (rhs))
7640 case COMPONENT_REF:
7642 tree member = TREE_OPERAND (rhs, 1);
7644 member = instantiate_type (lhstype, member, flags);
7645 if (member != error_mark_node
7646 && TREE_SIDE_EFFECTS (TREE_OPERAND (rhs, 0)))
7647 /* Do not lose object's side effects. */
7648 return build2 (COMPOUND_EXPR, TREE_TYPE (member),
7649 TREE_OPERAND (rhs, 0), member);
7650 return member;
7653 case OFFSET_REF:
7654 rhs = TREE_OPERAND (rhs, 1);
7655 if (BASELINK_P (rhs))
7656 return instantiate_type (lhstype, rhs, flags_in);
7658 /* This can happen if we are forming a pointer-to-member for a
7659 member template. */
7660 gcc_assert (TREE_CODE (rhs) == TEMPLATE_ID_EXPR);
7662 /* Fall through. */
7664 case TEMPLATE_ID_EXPR:
7666 tree fns = TREE_OPERAND (rhs, 0);
7667 tree args = TREE_OPERAND (rhs, 1);
7669 return
7670 resolve_address_of_overloaded_function (lhstype, fns, flags_in,
7671 /*template_only=*/true,
7672 args, access_path);
7675 case OVERLOAD:
7676 case FUNCTION_DECL:
7677 return
7678 resolve_address_of_overloaded_function (lhstype, rhs, flags_in,
7679 /*template_only=*/false,
7680 /*explicit_targs=*/NULL_TREE,
7681 access_path);
7683 case ADDR_EXPR:
7685 if (PTRMEM_OK_P (rhs))
7686 flags |= tf_ptrmem_ok;
7688 return instantiate_type (lhstype, TREE_OPERAND (rhs, 0), flags);
7691 case ERROR_MARK:
7692 return error_mark_node;
7694 default:
7695 gcc_unreachable ();
7697 return error_mark_node;
7700 /* Return the name of the virtual function pointer field
7701 (as an IDENTIFIER_NODE) for the given TYPE. Note that
7702 this may have to look back through base types to find the
7703 ultimate field name. (For single inheritance, these could
7704 all be the same name. Who knows for multiple inheritance). */
7706 static tree
7707 get_vfield_name (tree type)
7709 tree binfo, base_binfo;
7710 char *buf;
7712 for (binfo = TYPE_BINFO (type);
7713 BINFO_N_BASE_BINFOS (binfo);
7714 binfo = base_binfo)
7716 base_binfo = BINFO_BASE_BINFO (binfo, 0);
7718 if (BINFO_VIRTUAL_P (base_binfo)
7719 || !TYPE_CONTAINS_VPTR_P (BINFO_TYPE (base_binfo)))
7720 break;
7723 type = BINFO_TYPE (binfo);
7724 buf = (char *) alloca (sizeof (VFIELD_NAME_FORMAT)
7725 + TYPE_NAME_LENGTH (type) + 2);
7726 sprintf (buf, VFIELD_NAME_FORMAT,
7727 IDENTIFIER_POINTER (constructor_name (type)));
7728 return get_identifier (buf);
7731 void
7732 print_class_statistics (void)
7734 if (! GATHER_STATISTICS)
7735 return;
7737 fprintf (stderr, "convert_harshness = %d\n", n_convert_harshness);
7738 fprintf (stderr, "compute_conversion_costs = %d\n", n_compute_conversion_costs);
7739 if (n_vtables)
7741 fprintf (stderr, "vtables = %d; vtable searches = %d\n",
7742 n_vtables, n_vtable_searches);
7743 fprintf (stderr, "vtable entries = %d; vtable elems = %d\n",
7744 n_vtable_entries, n_vtable_elems);
7748 /* Build a dummy reference to ourselves so Derived::Base (and A::A) works,
7749 according to [class]:
7750 The class-name is also inserted
7751 into the scope of the class itself. For purposes of access checking,
7752 the inserted class name is treated as if it were a public member name. */
7754 void
7755 build_self_reference (void)
7757 tree name = constructor_name (current_class_type);
7758 tree value = build_lang_decl (TYPE_DECL, name, current_class_type);
7759 tree saved_cas;
7761 DECL_NONLOCAL (value) = 1;
7762 DECL_CONTEXT (value) = current_class_type;
7763 DECL_ARTIFICIAL (value) = 1;
7764 SET_DECL_SELF_REFERENCE_P (value);
7765 set_underlying_type (value);
7767 if (processing_template_decl)
7768 value = push_template_decl (value);
7770 saved_cas = current_access_specifier;
7771 current_access_specifier = access_public_node;
7772 finish_member_declaration (value);
7773 current_access_specifier = saved_cas;
7776 /* Returns 1 if TYPE contains only padding bytes. */
7779 is_empty_class (tree type)
7781 if (type == error_mark_node)
7782 return 0;
7784 if (! CLASS_TYPE_P (type))
7785 return 0;
7787 /* In G++ 3.2, whether or not a class was empty was determined by
7788 looking at its size. */
7789 if (abi_version_at_least (2))
7790 return CLASSTYPE_EMPTY_P (type);
7791 else
7792 return integer_zerop (CLASSTYPE_SIZE (type));
7795 /* Returns true if TYPE contains an empty class. */
7797 static bool
7798 contains_empty_class_p (tree type)
7800 if (is_empty_class (type))
7801 return true;
7802 if (CLASS_TYPE_P (type))
7804 tree field;
7805 tree binfo;
7806 tree base_binfo;
7807 int i;
7809 for (binfo = TYPE_BINFO (type), i = 0;
7810 BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
7811 if (contains_empty_class_p (BINFO_TYPE (base_binfo)))
7812 return true;
7813 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
7814 if (TREE_CODE (field) == FIELD_DECL
7815 && !DECL_ARTIFICIAL (field)
7816 && is_empty_class (TREE_TYPE (field)))
7817 return true;
7819 else if (TREE_CODE (type) == ARRAY_TYPE)
7820 return contains_empty_class_p (TREE_TYPE (type));
7821 return false;
7824 /* Returns true if TYPE contains no actual data, just various
7825 possible combinations of empty classes and possibly a vptr. */
7827 bool
7828 is_really_empty_class (tree type)
7830 if (CLASS_TYPE_P (type))
7832 tree field;
7833 tree binfo;
7834 tree base_binfo;
7835 int i;
7837 /* CLASSTYPE_EMPTY_P isn't set properly until the class is actually laid
7838 out, but we'd like to be able to check this before then. */
7839 if (COMPLETE_TYPE_P (type) && is_empty_class (type))
7840 return true;
7842 for (binfo = TYPE_BINFO (type), i = 0;
7843 BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
7844 if (!is_really_empty_class (BINFO_TYPE (base_binfo)))
7845 return false;
7846 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
7847 if (TREE_CODE (field) == FIELD_DECL
7848 && !DECL_ARTIFICIAL (field)
7849 && !is_really_empty_class (TREE_TYPE (field)))
7850 return false;
7851 return true;
7853 else if (TREE_CODE (type) == ARRAY_TYPE)
7854 return is_really_empty_class (TREE_TYPE (type));
7855 return false;
7858 /* Note that NAME was looked up while the current class was being
7859 defined and that the result of that lookup was DECL. */
7861 void
7862 maybe_note_name_used_in_class (tree name, tree decl)
7864 splay_tree names_used;
7866 /* If we're not defining a class, there's nothing to do. */
7867 if (!(innermost_scope_kind() == sk_class
7868 && TYPE_BEING_DEFINED (current_class_type)
7869 && !LAMBDA_TYPE_P (current_class_type)))
7870 return;
7872 /* If there's already a binding for this NAME, then we don't have
7873 anything to worry about. */
7874 if (lookup_member (current_class_type, name,
7875 /*protect=*/0, /*want_type=*/false, tf_warning_or_error))
7876 return;
7878 if (!current_class_stack[current_class_depth - 1].names_used)
7879 current_class_stack[current_class_depth - 1].names_used
7880 = splay_tree_new (splay_tree_compare_pointers, 0, 0);
7881 names_used = current_class_stack[current_class_depth - 1].names_used;
7883 splay_tree_insert (names_used,
7884 (splay_tree_key) name,
7885 (splay_tree_value) decl);
7888 /* Note that NAME was declared (as DECL) in the current class. Check
7889 to see that the declaration is valid. */
7891 void
7892 note_name_declared_in_class (tree name, tree decl)
7894 splay_tree names_used;
7895 splay_tree_node n;
7897 /* Look to see if we ever used this name. */
7898 names_used
7899 = current_class_stack[current_class_depth - 1].names_used;
7900 if (!names_used)
7901 return;
7902 /* The C language allows members to be declared with a type of the same
7903 name, and the C++ standard says this diagnostic is not required. So
7904 allow it in extern "C" blocks unless predantic is specified.
7905 Allow it in all cases if -ms-extensions is specified. */
7906 if ((!pedantic && current_lang_name == lang_name_c)
7907 || flag_ms_extensions)
7908 return;
7909 n = splay_tree_lookup (names_used, (splay_tree_key) name);
7910 if (n)
7912 /* [basic.scope.class]
7914 A name N used in a class S shall refer to the same declaration
7915 in its context and when re-evaluated in the completed scope of
7916 S. */
7917 permerror (input_location, "declaration of %q#D", decl);
7918 permerror (input_location, "changes meaning of %qD from %q+#D",
7919 DECL_NAME (OVL_CURRENT (decl)), (tree) n->value);
7923 /* Returns the VAR_DECL for the complete vtable associated with BINFO.
7924 Secondary vtables are merged with primary vtables; this function
7925 will return the VAR_DECL for the primary vtable. */
7927 tree
7928 get_vtbl_decl_for_binfo (tree binfo)
7930 tree decl;
7932 decl = BINFO_VTABLE (binfo);
7933 if (decl && TREE_CODE (decl) == POINTER_PLUS_EXPR)
7935 gcc_assert (TREE_CODE (TREE_OPERAND (decl, 0)) == ADDR_EXPR);
7936 decl = TREE_OPERAND (TREE_OPERAND (decl, 0), 0);
7938 if (decl)
7939 gcc_assert (VAR_P (decl));
7940 return decl;
7944 /* Returns the binfo for the primary base of BINFO. If the resulting
7945 BINFO is a virtual base, and it is inherited elsewhere in the
7946 hierarchy, then the returned binfo might not be the primary base of
7947 BINFO in the complete object. Check BINFO_PRIMARY_P or
7948 BINFO_LOST_PRIMARY_P to be sure. */
7950 static tree
7951 get_primary_binfo (tree binfo)
7953 tree primary_base;
7955 primary_base = CLASSTYPE_PRIMARY_BINFO (BINFO_TYPE (binfo));
7956 if (!primary_base)
7957 return NULL_TREE;
7959 return copied_binfo (primary_base, binfo);
7962 /* If INDENTED_P is zero, indent to INDENT. Return nonzero. */
7964 static int
7965 maybe_indent_hierarchy (FILE * stream, int indent, int indented_p)
7967 if (!indented_p)
7968 fprintf (stream, "%*s", indent, "");
7969 return 1;
7972 /* Dump the offsets of all the bases rooted at BINFO to STREAM.
7973 INDENT should be zero when called from the top level; it is
7974 incremented recursively. IGO indicates the next expected BINFO in
7975 inheritance graph ordering. */
7977 static tree
7978 dump_class_hierarchy_r (FILE *stream,
7979 int flags,
7980 tree binfo,
7981 tree igo,
7982 int indent)
7984 int indented = 0;
7985 tree base_binfo;
7986 int i;
7988 indented = maybe_indent_hierarchy (stream, indent, 0);
7989 fprintf (stream, "%s (0x" HOST_WIDE_INT_PRINT_HEX ") ",
7990 type_as_string (BINFO_TYPE (binfo), TFF_PLAIN_IDENTIFIER),
7991 (HOST_WIDE_INT) (uintptr_t) binfo);
7992 if (binfo != igo)
7994 fprintf (stream, "alternative-path\n");
7995 return igo;
7997 igo = TREE_CHAIN (binfo);
7999 fprintf (stream, HOST_WIDE_INT_PRINT_DEC,
8000 tree_low_cst (BINFO_OFFSET (binfo), 0));
8001 if (is_empty_class (BINFO_TYPE (binfo)))
8002 fprintf (stream, " empty");
8003 else if (CLASSTYPE_NEARLY_EMPTY_P (BINFO_TYPE (binfo)))
8004 fprintf (stream, " nearly-empty");
8005 if (BINFO_VIRTUAL_P (binfo))
8006 fprintf (stream, " virtual");
8007 fprintf (stream, "\n");
8009 indented = 0;
8010 if (BINFO_PRIMARY_P (binfo))
8012 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
8013 fprintf (stream, " primary-for %s (0x" HOST_WIDE_INT_PRINT_HEX ")",
8014 type_as_string (BINFO_TYPE (BINFO_INHERITANCE_CHAIN (binfo)),
8015 TFF_PLAIN_IDENTIFIER),
8016 (HOST_WIDE_INT) (uintptr_t) BINFO_INHERITANCE_CHAIN (binfo));
8018 if (BINFO_LOST_PRIMARY_P (binfo))
8020 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
8021 fprintf (stream, " lost-primary");
8023 if (indented)
8024 fprintf (stream, "\n");
8026 if (!(flags & TDF_SLIM))
8028 int indented = 0;
8030 if (BINFO_SUBVTT_INDEX (binfo))
8032 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
8033 fprintf (stream, " subvttidx=%s",
8034 expr_as_string (BINFO_SUBVTT_INDEX (binfo),
8035 TFF_PLAIN_IDENTIFIER));
8037 if (BINFO_VPTR_INDEX (binfo))
8039 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
8040 fprintf (stream, " vptridx=%s",
8041 expr_as_string (BINFO_VPTR_INDEX (binfo),
8042 TFF_PLAIN_IDENTIFIER));
8044 if (BINFO_VPTR_FIELD (binfo))
8046 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
8047 fprintf (stream, " vbaseoffset=%s",
8048 expr_as_string (BINFO_VPTR_FIELD (binfo),
8049 TFF_PLAIN_IDENTIFIER));
8051 if (BINFO_VTABLE (binfo))
8053 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
8054 fprintf (stream, " vptr=%s",
8055 expr_as_string (BINFO_VTABLE (binfo),
8056 TFF_PLAIN_IDENTIFIER));
8059 if (indented)
8060 fprintf (stream, "\n");
8063 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
8064 igo = dump_class_hierarchy_r (stream, flags, base_binfo, igo, indent + 2);
8066 return igo;
8069 /* Dump the BINFO hierarchy for T. */
8071 static void
8072 dump_class_hierarchy_1 (FILE *stream, int flags, tree t)
8074 fprintf (stream, "Class %s\n", type_as_string (t, TFF_PLAIN_IDENTIFIER));
8075 fprintf (stream, " size=%lu align=%lu\n",
8076 (unsigned long)(tree_low_cst (TYPE_SIZE (t), 0) / BITS_PER_UNIT),
8077 (unsigned long)(TYPE_ALIGN (t) / BITS_PER_UNIT));
8078 fprintf (stream, " base size=%lu base align=%lu\n",
8079 (unsigned long)(tree_low_cst (TYPE_SIZE (CLASSTYPE_AS_BASE (t)), 0)
8080 / BITS_PER_UNIT),
8081 (unsigned long)(TYPE_ALIGN (CLASSTYPE_AS_BASE (t))
8082 / BITS_PER_UNIT));
8083 dump_class_hierarchy_r (stream, flags, TYPE_BINFO (t), TYPE_BINFO (t), 0);
8084 fprintf (stream, "\n");
8087 /* Debug interface to hierarchy dumping. */
8089 void
8090 debug_class (tree t)
8092 dump_class_hierarchy_1 (stderr, TDF_SLIM, t);
8095 static void
8096 dump_class_hierarchy (tree t)
8098 int flags;
8099 FILE *stream = dump_begin (TDI_class, &flags);
8101 if (stream)
8103 dump_class_hierarchy_1 (stream, flags, t);
8104 dump_end (TDI_class, stream);
8108 static void
8109 dump_array (FILE * stream, tree decl)
8111 tree value;
8112 unsigned HOST_WIDE_INT ix;
8113 HOST_WIDE_INT elt;
8114 tree size = TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (decl)));
8116 elt = (tree_low_cst (TYPE_SIZE (TREE_TYPE (TREE_TYPE (decl))), 0)
8117 / BITS_PER_UNIT);
8118 fprintf (stream, "%s:", decl_as_string (decl, TFF_PLAIN_IDENTIFIER));
8119 fprintf (stream, " %s entries",
8120 expr_as_string (size_binop (PLUS_EXPR, size, size_one_node),
8121 TFF_PLAIN_IDENTIFIER));
8122 fprintf (stream, "\n");
8124 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (DECL_INITIAL (decl)),
8125 ix, value)
8126 fprintf (stream, "%-4ld %s\n", (long)(ix * elt),
8127 expr_as_string (value, TFF_PLAIN_IDENTIFIER));
8130 static void
8131 dump_vtable (tree t, tree binfo, tree vtable)
8133 int flags;
8134 FILE *stream = dump_begin (TDI_class, &flags);
8136 if (!stream)
8137 return;
8139 if (!(flags & TDF_SLIM))
8141 int ctor_vtbl_p = TYPE_BINFO (t) != binfo;
8143 fprintf (stream, "%s for %s",
8144 ctor_vtbl_p ? "Construction vtable" : "Vtable",
8145 type_as_string (BINFO_TYPE (binfo), TFF_PLAIN_IDENTIFIER));
8146 if (ctor_vtbl_p)
8148 if (!BINFO_VIRTUAL_P (binfo))
8149 fprintf (stream, " (0x" HOST_WIDE_INT_PRINT_HEX " instance)",
8150 (HOST_WIDE_INT) (uintptr_t) binfo);
8151 fprintf (stream, " in %s", type_as_string (t, TFF_PLAIN_IDENTIFIER));
8153 fprintf (stream, "\n");
8154 dump_array (stream, vtable);
8155 fprintf (stream, "\n");
8158 dump_end (TDI_class, stream);
8161 static void
8162 dump_vtt (tree t, tree vtt)
8164 int flags;
8165 FILE *stream = dump_begin (TDI_class, &flags);
8167 if (!stream)
8168 return;
8170 if (!(flags & TDF_SLIM))
8172 fprintf (stream, "VTT for %s\n",
8173 type_as_string (t, TFF_PLAIN_IDENTIFIER));
8174 dump_array (stream, vtt);
8175 fprintf (stream, "\n");
8178 dump_end (TDI_class, stream);
8181 /* Dump a function or thunk and its thunkees. */
8183 static void
8184 dump_thunk (FILE *stream, int indent, tree thunk)
8186 static const char spaces[] = " ";
8187 tree name = DECL_NAME (thunk);
8188 tree thunks;
8190 fprintf (stream, "%.*s%p %s %s", indent, spaces,
8191 (void *)thunk,
8192 !DECL_THUNK_P (thunk) ? "function"
8193 : DECL_THIS_THUNK_P (thunk) ? "this-thunk" : "covariant-thunk",
8194 name ? IDENTIFIER_POINTER (name) : "<unset>");
8195 if (DECL_THUNK_P (thunk))
8197 HOST_WIDE_INT fixed_adjust = THUNK_FIXED_OFFSET (thunk);
8198 tree virtual_adjust = THUNK_VIRTUAL_OFFSET (thunk);
8200 fprintf (stream, " fixed=" HOST_WIDE_INT_PRINT_DEC, fixed_adjust);
8201 if (!virtual_adjust)
8202 /*NOP*/;
8203 else if (DECL_THIS_THUNK_P (thunk))
8204 fprintf (stream, " vcall=" HOST_WIDE_INT_PRINT_DEC,
8205 tree_low_cst (virtual_adjust, 0));
8206 else
8207 fprintf (stream, " vbase=" HOST_WIDE_INT_PRINT_DEC "(%s)",
8208 tree_low_cst (BINFO_VPTR_FIELD (virtual_adjust), 0),
8209 type_as_string (BINFO_TYPE (virtual_adjust), TFF_SCOPE));
8210 if (THUNK_ALIAS (thunk))
8211 fprintf (stream, " alias to %p", (void *)THUNK_ALIAS (thunk));
8213 fprintf (stream, "\n");
8214 for (thunks = DECL_THUNKS (thunk); thunks; thunks = TREE_CHAIN (thunks))
8215 dump_thunk (stream, indent + 2, thunks);
8218 /* Dump the thunks for FN. */
8220 void
8221 debug_thunks (tree fn)
8223 dump_thunk (stderr, 0, fn);
8226 /* Virtual function table initialization. */
8228 /* Create all the necessary vtables for T and its base classes. */
8230 static void
8231 finish_vtbls (tree t)
8233 tree vbase;
8234 vec<constructor_elt, va_gc> *v = NULL;
8235 tree vtable = BINFO_VTABLE (TYPE_BINFO (t));
8237 /* We lay out the primary and secondary vtables in one contiguous
8238 vtable. The primary vtable is first, followed by the non-virtual
8239 secondary vtables in inheritance graph order. */
8240 accumulate_vtbl_inits (TYPE_BINFO (t), TYPE_BINFO (t), TYPE_BINFO (t),
8241 vtable, t, &v);
8243 /* Then come the virtual bases, also in inheritance graph order. */
8244 for (vbase = TYPE_BINFO (t); vbase; vbase = TREE_CHAIN (vbase))
8246 if (!BINFO_VIRTUAL_P (vbase))
8247 continue;
8248 accumulate_vtbl_inits (vbase, vbase, TYPE_BINFO (t), vtable, t, &v);
8251 if (BINFO_VTABLE (TYPE_BINFO (t)))
8252 initialize_vtable (TYPE_BINFO (t), v);
8255 /* Initialize the vtable for BINFO with the INITS. */
8257 static void
8258 initialize_vtable (tree binfo, vec<constructor_elt, va_gc> *inits)
8260 tree decl;
8262 layout_vtable_decl (binfo, vec_safe_length (inits));
8263 decl = get_vtbl_decl_for_binfo (binfo);
8264 initialize_artificial_var (decl, inits);
8265 dump_vtable (BINFO_TYPE (binfo), binfo, decl);
8268 /* Build the VTT (virtual table table) for T.
8269 A class requires a VTT if it has virtual bases.
8271 This holds
8272 1 - primary virtual pointer for complete object T
8273 2 - secondary VTTs for each direct non-virtual base of T which requires a
8275 3 - secondary virtual pointers for each direct or indirect base of T which
8276 has virtual bases or is reachable via a virtual path from T.
8277 4 - secondary VTTs for each direct or indirect virtual base of T.
8279 Secondary VTTs look like complete object VTTs without part 4. */
8281 static void
8282 build_vtt (tree t)
8284 tree type;
8285 tree vtt;
8286 tree index;
8287 vec<constructor_elt, va_gc> *inits;
8289 /* Build up the initializers for the VTT. */
8290 inits = NULL;
8291 index = size_zero_node;
8292 build_vtt_inits (TYPE_BINFO (t), t, &inits, &index);
8294 /* If we didn't need a VTT, we're done. */
8295 if (!inits)
8296 return;
8298 /* Figure out the type of the VTT. */
8299 type = build_array_of_n_type (const_ptr_type_node,
8300 inits->length ());
8302 /* Now, build the VTT object itself. */
8303 vtt = build_vtable (t, mangle_vtt_for_type (t), type);
8304 initialize_artificial_var (vtt, inits);
8305 /* Add the VTT to the vtables list. */
8306 DECL_CHAIN (vtt) = DECL_CHAIN (CLASSTYPE_VTABLES (t));
8307 DECL_CHAIN (CLASSTYPE_VTABLES (t)) = vtt;
8309 dump_vtt (t, vtt);
8312 /* When building a secondary VTT, BINFO_VTABLE is set to a TREE_LIST with
8313 PURPOSE the RTTI_BINFO, VALUE the real vtable pointer for this binfo,
8314 and CHAIN the vtable pointer for this binfo after construction is
8315 complete. VALUE can also be another BINFO, in which case we recurse. */
8317 static tree
8318 binfo_ctor_vtable (tree binfo)
8320 tree vt;
8322 while (1)
8324 vt = BINFO_VTABLE (binfo);
8325 if (TREE_CODE (vt) == TREE_LIST)
8326 vt = TREE_VALUE (vt);
8327 if (TREE_CODE (vt) == TREE_BINFO)
8328 binfo = vt;
8329 else
8330 break;
8333 return vt;
8336 /* Data for secondary VTT initialization. */
8337 typedef struct secondary_vptr_vtt_init_data_s
8339 /* Is this the primary VTT? */
8340 bool top_level_p;
8342 /* Current index into the VTT. */
8343 tree index;
8345 /* Vector of initializers built up. */
8346 vec<constructor_elt, va_gc> *inits;
8348 /* The type being constructed by this secondary VTT. */
8349 tree type_being_constructed;
8350 } secondary_vptr_vtt_init_data;
8352 /* Recursively build the VTT-initializer for BINFO (which is in the
8353 hierarchy dominated by T). INITS points to the end of the initializer
8354 list to date. INDEX is the VTT index where the next element will be
8355 replaced. Iff BINFO is the binfo for T, this is the top level VTT (i.e.
8356 not a subvtt for some base of T). When that is so, we emit the sub-VTTs
8357 for virtual bases of T. When it is not so, we build the constructor
8358 vtables for the BINFO-in-T variant. */
8360 static void
8361 build_vtt_inits (tree binfo, tree t, vec<constructor_elt, va_gc> **inits,
8362 tree *index)
8364 int i;
8365 tree b;
8366 tree init;
8367 secondary_vptr_vtt_init_data data;
8368 int top_level_p = SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), t);
8370 /* We only need VTTs for subobjects with virtual bases. */
8371 if (!CLASSTYPE_VBASECLASSES (BINFO_TYPE (binfo)))
8372 return;
8374 /* We need to use a construction vtable if this is not the primary
8375 VTT. */
8376 if (!top_level_p)
8378 build_ctor_vtbl_group (binfo, t);
8380 /* Record the offset in the VTT where this sub-VTT can be found. */
8381 BINFO_SUBVTT_INDEX (binfo) = *index;
8384 /* Add the address of the primary vtable for the complete object. */
8385 init = binfo_ctor_vtable (binfo);
8386 CONSTRUCTOR_APPEND_ELT (*inits, NULL_TREE, init);
8387 if (top_level_p)
8389 gcc_assert (!BINFO_VPTR_INDEX (binfo));
8390 BINFO_VPTR_INDEX (binfo) = *index;
8392 *index = size_binop (PLUS_EXPR, *index, TYPE_SIZE_UNIT (ptr_type_node));
8394 /* Recursively add the secondary VTTs for non-virtual bases. */
8395 for (i = 0; BINFO_BASE_ITERATE (binfo, i, b); ++i)
8396 if (!BINFO_VIRTUAL_P (b))
8397 build_vtt_inits (b, t, inits, index);
8399 /* Add secondary virtual pointers for all subobjects of BINFO with
8400 either virtual bases or reachable along a virtual path, except
8401 subobjects that are non-virtual primary bases. */
8402 data.top_level_p = top_level_p;
8403 data.index = *index;
8404 data.inits = *inits;
8405 data.type_being_constructed = BINFO_TYPE (binfo);
8407 dfs_walk_once (binfo, dfs_build_secondary_vptr_vtt_inits, NULL, &data);
8409 *index = data.index;
8411 /* data.inits might have grown as we added secondary virtual pointers.
8412 Make sure our caller knows about the new vector. */
8413 *inits = data.inits;
8415 if (top_level_p)
8416 /* Add the secondary VTTs for virtual bases in inheritance graph
8417 order. */
8418 for (b = TYPE_BINFO (BINFO_TYPE (binfo)); b; b = TREE_CHAIN (b))
8420 if (!BINFO_VIRTUAL_P (b))
8421 continue;
8423 build_vtt_inits (b, t, inits, index);
8425 else
8426 /* Remove the ctor vtables we created. */
8427 dfs_walk_all (binfo, dfs_fixup_binfo_vtbls, NULL, binfo);
8430 /* Called from build_vtt_inits via dfs_walk. BINFO is the binfo for the base
8431 in most derived. DATA is a SECONDARY_VPTR_VTT_INIT_DATA structure. */
8433 static tree
8434 dfs_build_secondary_vptr_vtt_inits (tree binfo, void *data_)
8436 secondary_vptr_vtt_init_data *data = (secondary_vptr_vtt_init_data *)data_;
8438 /* We don't care about bases that don't have vtables. */
8439 if (!TYPE_VFIELD (BINFO_TYPE (binfo)))
8440 return dfs_skip_bases;
8442 /* We're only interested in proper subobjects of the type being
8443 constructed. */
8444 if (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), data->type_being_constructed))
8445 return NULL_TREE;
8447 /* We're only interested in bases with virtual bases or reachable
8448 via a virtual path from the type being constructed. */
8449 if (!(CLASSTYPE_VBASECLASSES (BINFO_TYPE (binfo))
8450 || binfo_via_virtual (binfo, data->type_being_constructed)))
8451 return dfs_skip_bases;
8453 /* We're not interested in non-virtual primary bases. */
8454 if (!BINFO_VIRTUAL_P (binfo) && BINFO_PRIMARY_P (binfo))
8455 return NULL_TREE;
8457 /* Record the index where this secondary vptr can be found. */
8458 if (data->top_level_p)
8460 gcc_assert (!BINFO_VPTR_INDEX (binfo));
8461 BINFO_VPTR_INDEX (binfo) = data->index;
8463 if (BINFO_VIRTUAL_P (binfo))
8465 /* It's a primary virtual base, and this is not a
8466 construction vtable. Find the base this is primary of in
8467 the inheritance graph, and use that base's vtable
8468 now. */
8469 while (BINFO_PRIMARY_P (binfo))
8470 binfo = BINFO_INHERITANCE_CHAIN (binfo);
8474 /* Add the initializer for the secondary vptr itself. */
8475 CONSTRUCTOR_APPEND_ELT (data->inits, NULL_TREE, binfo_ctor_vtable (binfo));
8477 /* Advance the vtt index. */
8478 data->index = size_binop (PLUS_EXPR, data->index,
8479 TYPE_SIZE_UNIT (ptr_type_node));
8481 return NULL_TREE;
8484 /* Called from build_vtt_inits via dfs_walk. After building
8485 constructor vtables and generating the sub-vtt from them, we need
8486 to restore the BINFO_VTABLES that were scribbled on. DATA is the
8487 binfo of the base whose sub vtt was generated. */
8489 static tree
8490 dfs_fixup_binfo_vtbls (tree binfo, void* data)
8492 tree vtable = BINFO_VTABLE (binfo);
8494 if (!TYPE_CONTAINS_VPTR_P (BINFO_TYPE (binfo)))
8495 /* If this class has no vtable, none of its bases do. */
8496 return dfs_skip_bases;
8498 if (!vtable)
8499 /* This might be a primary base, so have no vtable in this
8500 hierarchy. */
8501 return NULL_TREE;
8503 /* If we scribbled the construction vtable vptr into BINFO, clear it
8504 out now. */
8505 if (TREE_CODE (vtable) == TREE_LIST
8506 && (TREE_PURPOSE (vtable) == (tree) data))
8507 BINFO_VTABLE (binfo) = TREE_CHAIN (vtable);
8509 return NULL_TREE;
8512 /* Build the construction vtable group for BINFO which is in the
8513 hierarchy dominated by T. */
8515 static void
8516 build_ctor_vtbl_group (tree binfo, tree t)
8518 tree type;
8519 tree vtbl;
8520 tree id;
8521 tree vbase;
8522 vec<constructor_elt, va_gc> *v;
8524 /* See if we've already created this construction vtable group. */
8525 id = mangle_ctor_vtbl_for_type (t, binfo);
8526 if (IDENTIFIER_GLOBAL_VALUE (id))
8527 return;
8529 gcc_assert (!SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), t));
8530 /* Build a version of VTBL (with the wrong type) for use in
8531 constructing the addresses of secondary vtables in the
8532 construction vtable group. */
8533 vtbl = build_vtable (t, id, ptr_type_node);
8534 DECL_CONSTRUCTION_VTABLE_P (vtbl) = 1;
8535 /* Don't export construction vtables from shared libraries. Even on
8536 targets that don't support hidden visibility, this tells
8537 can_refer_decl_in_current_unit_p not to assume that it's safe to
8538 access from a different compilation unit (bz 54314). */
8539 DECL_VISIBILITY (vtbl) = VISIBILITY_HIDDEN;
8540 DECL_VISIBILITY_SPECIFIED (vtbl) = true;
8542 v = NULL;
8543 accumulate_vtbl_inits (binfo, TYPE_BINFO (TREE_TYPE (binfo)),
8544 binfo, vtbl, t, &v);
8546 /* Add the vtables for each of our virtual bases using the vbase in T
8547 binfo. */
8548 for (vbase = TYPE_BINFO (BINFO_TYPE (binfo));
8549 vbase;
8550 vbase = TREE_CHAIN (vbase))
8552 tree b;
8554 if (!BINFO_VIRTUAL_P (vbase))
8555 continue;
8556 b = copied_binfo (vbase, binfo);
8558 accumulate_vtbl_inits (b, vbase, binfo, vtbl, t, &v);
8561 /* Figure out the type of the construction vtable. */
8562 type = build_array_of_n_type (vtable_entry_type, v->length ());
8563 layout_type (type);
8564 TREE_TYPE (vtbl) = type;
8565 DECL_SIZE (vtbl) = DECL_SIZE_UNIT (vtbl) = NULL_TREE;
8566 layout_decl (vtbl, 0);
8568 /* Initialize the construction vtable. */
8569 CLASSTYPE_VTABLES (t) = chainon (CLASSTYPE_VTABLES (t), vtbl);
8570 initialize_artificial_var (vtbl, v);
8571 dump_vtable (t, binfo, vtbl);
8574 /* Add the vtbl initializers for BINFO (and its bases other than
8575 non-virtual primaries) to the list of INITS. BINFO is in the
8576 hierarchy dominated by T. RTTI_BINFO is the binfo within T of
8577 the constructor the vtbl inits should be accumulated for. (If this
8578 is the complete object vtbl then RTTI_BINFO will be TYPE_BINFO (T).)
8579 ORIG_BINFO is the binfo for this object within BINFO_TYPE (RTTI_BINFO).
8580 BINFO is the active base equivalent of ORIG_BINFO in the inheritance
8581 graph of T. Both BINFO and ORIG_BINFO will have the same BINFO_TYPE,
8582 but are not necessarily the same in terms of layout. */
8584 static void
8585 accumulate_vtbl_inits (tree binfo,
8586 tree orig_binfo,
8587 tree rtti_binfo,
8588 tree vtbl,
8589 tree t,
8590 vec<constructor_elt, va_gc> **inits)
8592 int i;
8593 tree base_binfo;
8594 int ctor_vtbl_p = !SAME_BINFO_TYPE_P (BINFO_TYPE (rtti_binfo), t);
8596 gcc_assert (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), BINFO_TYPE (orig_binfo)));
8598 /* If it doesn't have a vptr, we don't do anything. */
8599 if (!TYPE_CONTAINS_VPTR_P (BINFO_TYPE (binfo)))
8600 return;
8602 /* If we're building a construction vtable, we're not interested in
8603 subobjects that don't require construction vtables. */
8604 if (ctor_vtbl_p
8605 && !CLASSTYPE_VBASECLASSES (BINFO_TYPE (binfo))
8606 && !binfo_via_virtual (orig_binfo, BINFO_TYPE (rtti_binfo)))
8607 return;
8609 /* Build the initializers for the BINFO-in-T vtable. */
8610 dfs_accumulate_vtbl_inits (binfo, orig_binfo, rtti_binfo, vtbl, t, inits);
8612 /* Walk the BINFO and its bases. We walk in preorder so that as we
8613 initialize each vtable we can figure out at what offset the
8614 secondary vtable lies from the primary vtable. We can't use
8615 dfs_walk here because we need to iterate through bases of BINFO
8616 and RTTI_BINFO simultaneously. */
8617 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
8619 /* Skip virtual bases. */
8620 if (BINFO_VIRTUAL_P (base_binfo))
8621 continue;
8622 accumulate_vtbl_inits (base_binfo,
8623 BINFO_BASE_BINFO (orig_binfo, i),
8624 rtti_binfo, vtbl, t,
8625 inits);
8629 /* Called from accumulate_vtbl_inits. Adds the initializers for the
8630 BINFO vtable to L. */
8632 static void
8633 dfs_accumulate_vtbl_inits (tree binfo,
8634 tree orig_binfo,
8635 tree rtti_binfo,
8636 tree orig_vtbl,
8637 tree t,
8638 vec<constructor_elt, va_gc> **l)
8640 tree vtbl = NULL_TREE;
8641 int ctor_vtbl_p = !SAME_BINFO_TYPE_P (BINFO_TYPE (rtti_binfo), t);
8642 int n_inits;
8644 if (ctor_vtbl_p
8645 && BINFO_VIRTUAL_P (orig_binfo) && BINFO_PRIMARY_P (orig_binfo))
8647 /* In the hierarchy of BINFO_TYPE (RTTI_BINFO), this is a
8648 primary virtual base. If it is not the same primary in
8649 the hierarchy of T, we'll need to generate a ctor vtable
8650 for it, to place at its location in T. If it is the same
8651 primary, we still need a VTT entry for the vtable, but it
8652 should point to the ctor vtable for the base it is a
8653 primary for within the sub-hierarchy of RTTI_BINFO.
8655 There are three possible cases:
8657 1) We are in the same place.
8658 2) We are a primary base within a lost primary virtual base of
8659 RTTI_BINFO.
8660 3) We are primary to something not a base of RTTI_BINFO. */
8662 tree b;
8663 tree last = NULL_TREE;
8665 /* First, look through the bases we are primary to for RTTI_BINFO
8666 or a virtual base. */
8667 b = binfo;
8668 while (BINFO_PRIMARY_P (b))
8670 b = BINFO_INHERITANCE_CHAIN (b);
8671 last = b;
8672 if (BINFO_VIRTUAL_P (b) || b == rtti_binfo)
8673 goto found;
8675 /* If we run out of primary links, keep looking down our
8676 inheritance chain; we might be an indirect primary. */
8677 for (b = last; b; b = BINFO_INHERITANCE_CHAIN (b))
8678 if (BINFO_VIRTUAL_P (b) || b == rtti_binfo)
8679 break;
8680 found:
8682 /* If we found RTTI_BINFO, this is case 1. If we found a virtual
8683 base B and it is a base of RTTI_BINFO, this is case 2. In
8684 either case, we share our vtable with LAST, i.e. the
8685 derived-most base within B of which we are a primary. */
8686 if (b == rtti_binfo
8687 || (b && binfo_for_vbase (BINFO_TYPE (b), BINFO_TYPE (rtti_binfo))))
8688 /* Just set our BINFO_VTABLE to point to LAST, as we may not have
8689 set LAST's BINFO_VTABLE yet. We'll extract the actual vptr in
8690 binfo_ctor_vtable after everything's been set up. */
8691 vtbl = last;
8693 /* Otherwise, this is case 3 and we get our own. */
8695 else if (!BINFO_NEW_VTABLE_MARKED (orig_binfo))
8696 return;
8698 n_inits = vec_safe_length (*l);
8700 if (!vtbl)
8702 tree index;
8703 int non_fn_entries;
8705 /* Add the initializer for this vtable. */
8706 build_vtbl_initializer (binfo, orig_binfo, t, rtti_binfo,
8707 &non_fn_entries, l);
8709 /* Figure out the position to which the VPTR should point. */
8710 vtbl = build1 (ADDR_EXPR, vtbl_ptr_type_node, orig_vtbl);
8711 index = size_binop (MULT_EXPR,
8712 TYPE_SIZE_UNIT (vtable_entry_type),
8713 size_int (non_fn_entries + n_inits));
8714 vtbl = fold_build_pointer_plus (vtbl, index);
8717 if (ctor_vtbl_p)
8718 /* For a construction vtable, we can't overwrite BINFO_VTABLE.
8719 So, we make a TREE_LIST. Later, dfs_fixup_binfo_vtbls will
8720 straighten this out. */
8721 BINFO_VTABLE (binfo) = tree_cons (rtti_binfo, vtbl, BINFO_VTABLE (binfo));
8722 else if (BINFO_PRIMARY_P (binfo) && BINFO_VIRTUAL_P (binfo))
8723 /* Throw away any unneeded intializers. */
8724 (*l)->truncate (n_inits);
8725 else
8726 /* For an ordinary vtable, set BINFO_VTABLE. */
8727 BINFO_VTABLE (binfo) = vtbl;
8730 static GTY(()) tree abort_fndecl_addr;
8732 /* Construct the initializer for BINFO's virtual function table. BINFO
8733 is part of the hierarchy dominated by T. If we're building a
8734 construction vtable, the ORIG_BINFO is the binfo we should use to
8735 find the actual function pointers to put in the vtable - but they
8736 can be overridden on the path to most-derived in the graph that
8737 ORIG_BINFO belongs. Otherwise,
8738 ORIG_BINFO should be the same as BINFO. The RTTI_BINFO is the
8739 BINFO that should be indicated by the RTTI information in the
8740 vtable; it will be a base class of T, rather than T itself, if we
8741 are building a construction vtable.
8743 The value returned is a TREE_LIST suitable for wrapping in a
8744 CONSTRUCTOR to use as the DECL_INITIAL for a vtable. If
8745 NON_FN_ENTRIES_P is not NULL, *NON_FN_ENTRIES_P is set to the
8746 number of non-function entries in the vtable.
8748 It might seem that this function should never be called with a
8749 BINFO for which BINFO_PRIMARY_P holds, the vtable for such a
8750 base is always subsumed by a derived class vtable. However, when
8751 we are building construction vtables, we do build vtables for
8752 primary bases; we need these while the primary base is being
8753 constructed. */
8755 static void
8756 build_vtbl_initializer (tree binfo,
8757 tree orig_binfo,
8758 tree t,
8759 tree rtti_binfo,
8760 int* non_fn_entries_p,
8761 vec<constructor_elt, va_gc> **inits)
8763 tree v;
8764 vtbl_init_data vid;
8765 unsigned ix, jx;
8766 tree vbinfo;
8767 vec<tree, va_gc> *vbases;
8768 constructor_elt *e;
8770 /* Initialize VID. */
8771 memset (&vid, 0, sizeof (vid));
8772 vid.binfo = binfo;
8773 vid.derived = t;
8774 vid.rtti_binfo = rtti_binfo;
8775 vid.primary_vtbl_p = SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), t);
8776 vid.ctor_vtbl_p = !SAME_BINFO_TYPE_P (BINFO_TYPE (rtti_binfo), t);
8777 vid.generate_vcall_entries = true;
8778 /* The first vbase or vcall offset is at index -3 in the vtable. */
8779 vid.index = ssize_int(-3 * TARGET_VTABLE_DATA_ENTRY_DISTANCE);
8781 /* Add entries to the vtable for RTTI. */
8782 build_rtti_vtbl_entries (binfo, &vid);
8784 /* Create an array for keeping track of the functions we've
8785 processed. When we see multiple functions with the same
8786 signature, we share the vcall offsets. */
8787 vec_alloc (vid.fns, 32);
8788 /* Add the vcall and vbase offset entries. */
8789 build_vcall_and_vbase_vtbl_entries (binfo, &vid);
8791 /* Clear BINFO_VTABLE_PATH_MARKED; it's set by
8792 build_vbase_offset_vtbl_entries. */
8793 for (vbases = CLASSTYPE_VBASECLASSES (t), ix = 0;
8794 vec_safe_iterate (vbases, ix, &vbinfo); ix++)
8795 BINFO_VTABLE_PATH_MARKED (vbinfo) = 0;
8797 /* If the target requires padding between data entries, add that now. */
8798 if (TARGET_VTABLE_DATA_ENTRY_DISTANCE > 1)
8800 int n_entries = vec_safe_length (vid.inits);
8802 vec_safe_grow (vid.inits, TARGET_VTABLE_DATA_ENTRY_DISTANCE * n_entries);
8804 /* Move data entries into their new positions and add padding
8805 after the new positions. Iterate backwards so we don't
8806 overwrite entries that we would need to process later. */
8807 for (ix = n_entries - 1;
8808 vid.inits->iterate (ix, &e);
8809 ix--)
8811 int j;
8812 int new_position = (TARGET_VTABLE_DATA_ENTRY_DISTANCE * ix
8813 + (TARGET_VTABLE_DATA_ENTRY_DISTANCE - 1));
8815 (*vid.inits)[new_position] = *e;
8817 for (j = 1; j < TARGET_VTABLE_DATA_ENTRY_DISTANCE; ++j)
8819 constructor_elt *f = &(*vid.inits)[new_position - j];
8820 f->index = NULL_TREE;
8821 f->value = build1 (NOP_EXPR, vtable_entry_type,
8822 null_pointer_node);
8827 if (non_fn_entries_p)
8828 *non_fn_entries_p = vec_safe_length (vid.inits);
8830 /* The initializers for virtual functions were built up in reverse
8831 order. Straighten them out and add them to the running list in one
8832 step. */
8833 jx = vec_safe_length (*inits);
8834 vec_safe_grow (*inits, jx + vid.inits->length ());
8836 for (ix = vid.inits->length () - 1;
8837 vid.inits->iterate (ix, &e);
8838 ix--, jx++)
8839 (**inits)[jx] = *e;
8841 /* Go through all the ordinary virtual functions, building up
8842 initializers. */
8843 for (v = BINFO_VIRTUALS (orig_binfo); v; v = TREE_CHAIN (v))
8845 tree delta;
8846 tree vcall_index;
8847 tree fn, fn_original;
8848 tree init = NULL_TREE;
8850 fn = BV_FN (v);
8851 fn_original = fn;
8852 if (DECL_THUNK_P (fn))
8854 if (!DECL_NAME (fn))
8855 finish_thunk (fn);
8856 if (THUNK_ALIAS (fn))
8858 fn = THUNK_ALIAS (fn);
8859 BV_FN (v) = fn;
8861 fn_original = THUNK_TARGET (fn);
8864 /* If the only definition of this function signature along our
8865 primary base chain is from a lost primary, this vtable slot will
8866 never be used, so just zero it out. This is important to avoid
8867 requiring extra thunks which cannot be generated with the function.
8869 We first check this in update_vtable_entry_for_fn, so we handle
8870 restored primary bases properly; we also need to do it here so we
8871 zero out unused slots in ctor vtables, rather than filling them
8872 with erroneous values (though harmless, apart from relocation
8873 costs). */
8874 if (BV_LOST_PRIMARY (v))
8875 init = size_zero_node;
8877 if (! init)
8879 /* Pull the offset for `this', and the function to call, out of
8880 the list. */
8881 delta = BV_DELTA (v);
8882 vcall_index = BV_VCALL_INDEX (v);
8884 gcc_assert (TREE_CODE (delta) == INTEGER_CST);
8885 gcc_assert (TREE_CODE (fn) == FUNCTION_DECL);
8887 /* You can't call an abstract virtual function; it's abstract.
8888 So, we replace these functions with __pure_virtual. */
8889 if (DECL_PURE_VIRTUAL_P (fn_original))
8891 fn = abort_fndecl;
8892 if (!TARGET_VTABLE_USES_DESCRIPTORS)
8894 if (abort_fndecl_addr == NULL)
8895 abort_fndecl_addr
8896 = fold_convert (vfunc_ptr_type_node,
8897 build_fold_addr_expr (fn));
8898 init = abort_fndecl_addr;
8901 /* Likewise for deleted virtuals. */
8902 else if (DECL_DELETED_FN (fn_original))
8904 fn = get_identifier ("__cxa_deleted_virtual");
8905 if (!get_global_value_if_present (fn, &fn))
8906 fn = push_library_fn (fn, (build_function_type_list
8907 (void_type_node, NULL_TREE)),
8908 NULL_TREE, ECF_NORETURN);
8909 if (!TARGET_VTABLE_USES_DESCRIPTORS)
8910 init = fold_convert (vfunc_ptr_type_node,
8911 build_fold_addr_expr (fn));
8913 else
8915 if (!integer_zerop (delta) || vcall_index)
8917 fn = make_thunk (fn, /*this_adjusting=*/1, delta, vcall_index);
8918 if (!DECL_NAME (fn))
8919 finish_thunk (fn);
8921 /* Take the address of the function, considering it to be of an
8922 appropriate generic type. */
8923 if (!TARGET_VTABLE_USES_DESCRIPTORS)
8924 init = fold_convert (vfunc_ptr_type_node,
8925 build_fold_addr_expr (fn));
8929 /* And add it to the chain of initializers. */
8930 if (TARGET_VTABLE_USES_DESCRIPTORS)
8932 int i;
8933 if (init == size_zero_node)
8934 for (i = 0; i < TARGET_VTABLE_USES_DESCRIPTORS; ++i)
8935 CONSTRUCTOR_APPEND_ELT (*inits, NULL_TREE, init);
8936 else
8937 for (i = 0; i < TARGET_VTABLE_USES_DESCRIPTORS; ++i)
8939 tree fdesc = build2 (FDESC_EXPR, vfunc_ptr_type_node,
8940 fn, build_int_cst (NULL_TREE, i));
8941 TREE_CONSTANT (fdesc) = 1;
8943 CONSTRUCTOR_APPEND_ELT (*inits, NULL_TREE, fdesc);
8946 else
8947 CONSTRUCTOR_APPEND_ELT (*inits, NULL_TREE, init);
8951 /* Adds to vid->inits the initializers for the vbase and vcall
8952 offsets in BINFO, which is in the hierarchy dominated by T. */
8954 static void
8955 build_vcall_and_vbase_vtbl_entries (tree binfo, vtbl_init_data* vid)
8957 tree b;
8959 /* If this is a derived class, we must first create entries
8960 corresponding to the primary base class. */
8961 b = get_primary_binfo (binfo);
8962 if (b)
8963 build_vcall_and_vbase_vtbl_entries (b, vid);
8965 /* Add the vbase entries for this base. */
8966 build_vbase_offset_vtbl_entries (binfo, vid);
8967 /* Add the vcall entries for this base. */
8968 build_vcall_offset_vtbl_entries (binfo, vid);
8971 /* Returns the initializers for the vbase offset entries in the vtable
8972 for BINFO (which is part of the class hierarchy dominated by T), in
8973 reverse order. VBASE_OFFSET_INDEX gives the vtable index
8974 where the next vbase offset will go. */
8976 static void
8977 build_vbase_offset_vtbl_entries (tree binfo, vtbl_init_data* vid)
8979 tree vbase;
8980 tree t;
8981 tree non_primary_binfo;
8983 /* If there are no virtual baseclasses, then there is nothing to
8984 do. */
8985 if (!CLASSTYPE_VBASECLASSES (BINFO_TYPE (binfo)))
8986 return;
8988 t = vid->derived;
8990 /* We might be a primary base class. Go up the inheritance hierarchy
8991 until we find the most derived class of which we are a primary base:
8992 it is the offset of that which we need to use. */
8993 non_primary_binfo = binfo;
8994 while (BINFO_INHERITANCE_CHAIN (non_primary_binfo))
8996 tree b;
8998 /* If we have reached a virtual base, then it must be a primary
8999 base (possibly multi-level) of vid->binfo, or we wouldn't
9000 have called build_vcall_and_vbase_vtbl_entries for it. But it
9001 might be a lost primary, so just skip down to vid->binfo. */
9002 if (BINFO_VIRTUAL_P (non_primary_binfo))
9004 non_primary_binfo = vid->binfo;
9005 break;
9008 b = BINFO_INHERITANCE_CHAIN (non_primary_binfo);
9009 if (get_primary_binfo (b) != non_primary_binfo)
9010 break;
9011 non_primary_binfo = b;
9014 /* Go through the virtual bases, adding the offsets. */
9015 for (vbase = TYPE_BINFO (BINFO_TYPE (binfo));
9016 vbase;
9017 vbase = TREE_CHAIN (vbase))
9019 tree b;
9020 tree delta;
9022 if (!BINFO_VIRTUAL_P (vbase))
9023 continue;
9025 /* Find the instance of this virtual base in the complete
9026 object. */
9027 b = copied_binfo (vbase, binfo);
9029 /* If we've already got an offset for this virtual base, we
9030 don't need another one. */
9031 if (BINFO_VTABLE_PATH_MARKED (b))
9032 continue;
9033 BINFO_VTABLE_PATH_MARKED (b) = 1;
9035 /* Figure out where we can find this vbase offset. */
9036 delta = size_binop (MULT_EXPR,
9037 vid->index,
9038 convert (ssizetype,
9039 TYPE_SIZE_UNIT (vtable_entry_type)));
9040 if (vid->primary_vtbl_p)
9041 BINFO_VPTR_FIELD (b) = delta;
9043 if (binfo != TYPE_BINFO (t))
9044 /* The vbase offset had better be the same. */
9045 gcc_assert (tree_int_cst_equal (delta, BINFO_VPTR_FIELD (vbase)));
9047 /* The next vbase will come at a more negative offset. */
9048 vid->index = size_binop (MINUS_EXPR, vid->index,
9049 ssize_int (TARGET_VTABLE_DATA_ENTRY_DISTANCE));
9051 /* The initializer is the delta from BINFO to this virtual base.
9052 The vbase offsets go in reverse inheritance-graph order, and
9053 we are walking in inheritance graph order so these end up in
9054 the right order. */
9055 delta = size_diffop_loc (input_location,
9056 BINFO_OFFSET (b), BINFO_OFFSET (non_primary_binfo));
9058 CONSTRUCTOR_APPEND_ELT (vid->inits, NULL_TREE,
9059 fold_build1_loc (input_location, NOP_EXPR,
9060 vtable_entry_type, delta));
9064 /* Adds the initializers for the vcall offset entries in the vtable
9065 for BINFO (which is part of the class hierarchy dominated by VID->DERIVED)
9066 to VID->INITS. */
9068 static void
9069 build_vcall_offset_vtbl_entries (tree binfo, vtbl_init_data* vid)
9071 /* We only need these entries if this base is a virtual base. We
9072 compute the indices -- but do not add to the vtable -- when
9073 building the main vtable for a class. */
9074 if (binfo == TYPE_BINFO (vid->derived)
9075 || (BINFO_VIRTUAL_P (binfo)
9076 /* If BINFO is RTTI_BINFO, then (since BINFO does not
9077 correspond to VID->DERIVED), we are building a primary
9078 construction virtual table. Since this is a primary
9079 virtual table, we do not need the vcall offsets for
9080 BINFO. */
9081 && binfo != vid->rtti_binfo))
9083 /* We need a vcall offset for each of the virtual functions in this
9084 vtable. For example:
9086 class A { virtual void f (); };
9087 class B1 : virtual public A { virtual void f (); };
9088 class B2 : virtual public A { virtual void f (); };
9089 class C: public B1, public B2 { virtual void f (); };
9091 A C object has a primary base of B1, which has a primary base of A. A
9092 C also has a secondary base of B2, which no longer has a primary base
9093 of A. So the B2-in-C construction vtable needs a secondary vtable for
9094 A, which will adjust the A* to a B2* to call f. We have no way of
9095 knowing what (or even whether) this offset will be when we define B2,
9096 so we store this "vcall offset" in the A sub-vtable and look it up in
9097 a "virtual thunk" for B2::f.
9099 We need entries for all the functions in our primary vtable and
9100 in our non-virtual bases' secondary vtables. */
9101 vid->vbase = binfo;
9102 /* If we are just computing the vcall indices -- but do not need
9103 the actual entries -- not that. */
9104 if (!BINFO_VIRTUAL_P (binfo))
9105 vid->generate_vcall_entries = false;
9106 /* Now, walk through the non-virtual bases, adding vcall offsets. */
9107 add_vcall_offset_vtbl_entries_r (binfo, vid);
9111 /* Build vcall offsets, starting with those for BINFO. */
9113 static void
9114 add_vcall_offset_vtbl_entries_r (tree binfo, vtbl_init_data* vid)
9116 int i;
9117 tree primary_binfo;
9118 tree base_binfo;
9120 /* Don't walk into virtual bases -- except, of course, for the
9121 virtual base for which we are building vcall offsets. Any
9122 primary virtual base will have already had its offsets generated
9123 through the recursion in build_vcall_and_vbase_vtbl_entries. */
9124 if (BINFO_VIRTUAL_P (binfo) && vid->vbase != binfo)
9125 return;
9127 /* If BINFO has a primary base, process it first. */
9128 primary_binfo = get_primary_binfo (binfo);
9129 if (primary_binfo)
9130 add_vcall_offset_vtbl_entries_r (primary_binfo, vid);
9132 /* Add BINFO itself to the list. */
9133 add_vcall_offset_vtbl_entries_1 (binfo, vid);
9135 /* Scan the non-primary bases of BINFO. */
9136 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
9137 if (base_binfo != primary_binfo)
9138 add_vcall_offset_vtbl_entries_r (base_binfo, vid);
9141 /* Called from build_vcall_offset_vtbl_entries_r. */
9143 static void
9144 add_vcall_offset_vtbl_entries_1 (tree binfo, vtbl_init_data* vid)
9146 /* Make entries for the rest of the virtuals. */
9147 if (abi_version_at_least (2))
9149 tree orig_fn;
9151 /* The ABI requires that the methods be processed in declaration
9152 order. G++ 3.2 used the order in the vtable. */
9153 for (orig_fn = TYPE_METHODS (BINFO_TYPE (binfo));
9154 orig_fn;
9155 orig_fn = DECL_CHAIN (orig_fn))
9156 if (DECL_VINDEX (orig_fn))
9157 add_vcall_offset (orig_fn, binfo, vid);
9159 else
9161 tree derived_virtuals;
9162 tree base_virtuals;
9163 tree orig_virtuals;
9164 /* If BINFO is a primary base, the most derived class which has
9165 BINFO as a primary base; otherwise, just BINFO. */
9166 tree non_primary_binfo;
9168 /* We might be a primary base class. Go up the inheritance hierarchy
9169 until we find the most derived class of which we are a primary base:
9170 it is the BINFO_VIRTUALS there that we need to consider. */
9171 non_primary_binfo = binfo;
9172 while (BINFO_INHERITANCE_CHAIN (non_primary_binfo))
9174 tree b;
9176 /* If we have reached a virtual base, then it must be vid->vbase,
9177 because we ignore other virtual bases in
9178 add_vcall_offset_vtbl_entries_r. In turn, it must be a primary
9179 base (possibly multi-level) of vid->binfo, or we wouldn't
9180 have called build_vcall_and_vbase_vtbl_entries for it. But it
9181 might be a lost primary, so just skip down to vid->binfo. */
9182 if (BINFO_VIRTUAL_P (non_primary_binfo))
9184 gcc_assert (non_primary_binfo == vid->vbase);
9185 non_primary_binfo = vid->binfo;
9186 break;
9189 b = BINFO_INHERITANCE_CHAIN (non_primary_binfo);
9190 if (get_primary_binfo (b) != non_primary_binfo)
9191 break;
9192 non_primary_binfo = b;
9195 if (vid->ctor_vtbl_p)
9196 /* For a ctor vtable we need the equivalent binfo within the hierarchy
9197 where rtti_binfo is the most derived type. */
9198 non_primary_binfo
9199 = original_binfo (non_primary_binfo, vid->rtti_binfo);
9201 for (base_virtuals = BINFO_VIRTUALS (binfo),
9202 derived_virtuals = BINFO_VIRTUALS (non_primary_binfo),
9203 orig_virtuals = BINFO_VIRTUALS (TYPE_BINFO (BINFO_TYPE (binfo)));
9204 base_virtuals;
9205 base_virtuals = TREE_CHAIN (base_virtuals),
9206 derived_virtuals = TREE_CHAIN (derived_virtuals),
9207 orig_virtuals = TREE_CHAIN (orig_virtuals))
9209 tree orig_fn;
9211 /* Find the declaration that originally caused this function to
9212 be present in BINFO_TYPE (binfo). */
9213 orig_fn = BV_FN (orig_virtuals);
9215 /* When processing BINFO, we only want to generate vcall slots for
9216 function slots introduced in BINFO. So don't try to generate
9217 one if the function isn't even defined in BINFO. */
9218 if (!SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), DECL_CONTEXT (orig_fn)))
9219 continue;
9221 add_vcall_offset (orig_fn, binfo, vid);
9226 /* Add a vcall offset entry for ORIG_FN to the vtable. */
9228 static void
9229 add_vcall_offset (tree orig_fn, tree binfo, vtbl_init_data *vid)
9231 size_t i;
9232 tree vcall_offset;
9233 tree derived_entry;
9235 /* If there is already an entry for a function with the same
9236 signature as FN, then we do not need a second vcall offset.
9237 Check the list of functions already present in the derived
9238 class vtable. */
9239 FOR_EACH_VEC_SAFE_ELT (vid->fns, i, derived_entry)
9241 if (same_signature_p (derived_entry, orig_fn)
9242 /* We only use one vcall offset for virtual destructors,
9243 even though there are two virtual table entries. */
9244 || (DECL_DESTRUCTOR_P (derived_entry)
9245 && DECL_DESTRUCTOR_P (orig_fn)))
9246 return;
9249 /* If we are building these vcall offsets as part of building
9250 the vtable for the most derived class, remember the vcall
9251 offset. */
9252 if (vid->binfo == TYPE_BINFO (vid->derived))
9254 tree_pair_s elt = {orig_fn, vid->index};
9255 vec_safe_push (CLASSTYPE_VCALL_INDICES (vid->derived), elt);
9258 /* The next vcall offset will be found at a more negative
9259 offset. */
9260 vid->index = size_binop (MINUS_EXPR, vid->index,
9261 ssize_int (TARGET_VTABLE_DATA_ENTRY_DISTANCE));
9263 /* Keep track of this function. */
9264 vec_safe_push (vid->fns, orig_fn);
9266 if (vid->generate_vcall_entries)
9268 tree base;
9269 tree fn;
9271 /* Find the overriding function. */
9272 fn = find_final_overrider (vid->rtti_binfo, binfo, orig_fn);
9273 if (fn == error_mark_node)
9274 vcall_offset = build_zero_cst (vtable_entry_type);
9275 else
9277 base = TREE_VALUE (fn);
9279 /* The vbase we're working on is a primary base of
9280 vid->binfo. But it might be a lost primary, so its
9281 BINFO_OFFSET might be wrong, so we just use the
9282 BINFO_OFFSET from vid->binfo. */
9283 vcall_offset = size_diffop_loc (input_location,
9284 BINFO_OFFSET (base),
9285 BINFO_OFFSET (vid->binfo));
9286 vcall_offset = fold_build1_loc (input_location,
9287 NOP_EXPR, vtable_entry_type,
9288 vcall_offset);
9290 /* Add the initializer to the vtable. */
9291 CONSTRUCTOR_APPEND_ELT (vid->inits, NULL_TREE, vcall_offset);
9295 /* Return vtbl initializers for the RTTI entries corresponding to the
9296 BINFO's vtable. The RTTI entries should indicate the object given
9297 by VID->rtti_binfo. */
9299 static void
9300 build_rtti_vtbl_entries (tree binfo, vtbl_init_data* vid)
9302 tree b;
9303 tree t;
9304 tree offset;
9305 tree decl;
9306 tree init;
9308 t = BINFO_TYPE (vid->rtti_binfo);
9310 /* To find the complete object, we will first convert to our most
9311 primary base, and then add the offset in the vtbl to that value. */
9312 b = binfo;
9313 while (CLASSTYPE_HAS_PRIMARY_BASE_P (BINFO_TYPE (b))
9314 && !BINFO_LOST_PRIMARY_P (b))
9316 tree primary_base;
9318 primary_base = get_primary_binfo (b);
9319 gcc_assert (BINFO_PRIMARY_P (primary_base)
9320 && BINFO_INHERITANCE_CHAIN (primary_base) == b);
9321 b = primary_base;
9323 offset = size_diffop_loc (input_location,
9324 BINFO_OFFSET (vid->rtti_binfo), BINFO_OFFSET (b));
9326 /* The second entry is the address of the typeinfo object. */
9327 if (flag_rtti)
9328 decl = build_address (get_tinfo_decl (t));
9329 else
9330 decl = integer_zero_node;
9332 /* Convert the declaration to a type that can be stored in the
9333 vtable. */
9334 init = build_nop (vfunc_ptr_type_node, decl);
9335 CONSTRUCTOR_APPEND_ELT (vid->inits, NULL_TREE, init);
9337 /* Add the offset-to-top entry. It comes earlier in the vtable than
9338 the typeinfo entry. Convert the offset to look like a
9339 function pointer, so that we can put it in the vtable. */
9340 init = build_nop (vfunc_ptr_type_node, offset);
9341 CONSTRUCTOR_APPEND_ELT (vid->inits, NULL_TREE, init);
9344 /* TRUE iff TYPE is uniquely derived from PARENT. Ignores
9345 accessibility. */
9347 bool
9348 uniquely_derived_from_p (tree parent, tree type)
9350 tree base = lookup_base (type, parent, ba_unique, NULL, tf_none);
9351 return base && base != error_mark_node;
9354 /* TRUE iff TYPE is publicly & uniquely derived from PARENT. */
9356 bool
9357 publicly_uniquely_derived_p (tree parent, tree type)
9359 tree base = lookup_base (type, parent, ba_ignore_scope | ba_check,
9360 NULL, tf_none);
9361 return base && base != error_mark_node;
9364 /* CTX1 and CTX2 are declaration contexts. Return the innermost common
9365 class between them, if any. */
9367 tree
9368 common_enclosing_class (tree ctx1, tree ctx2)
9370 if (!TYPE_P (ctx1) || !TYPE_P (ctx2))
9371 return NULL_TREE;
9372 gcc_assert (ctx1 == TYPE_MAIN_VARIANT (ctx1)
9373 && ctx2 == TYPE_MAIN_VARIANT (ctx2));
9374 if (ctx1 == ctx2)
9375 return ctx1;
9376 for (tree t = ctx1; TYPE_P (t); t = TYPE_CONTEXT (t))
9377 TYPE_MARKED_P (t) = true;
9378 tree found = NULL_TREE;
9379 for (tree t = ctx2; TYPE_P (t); t = TYPE_CONTEXT (t))
9380 if (TYPE_MARKED_P (t))
9382 found = t;
9383 break;
9385 for (tree t = ctx1; TYPE_P (t); t = TYPE_CONTEXT (t))
9386 TYPE_MARKED_P (t) = false;
9387 return found;
9390 #include "gt-cp-class.h"