Merge from trunk
[official-gcc.git] / gcc / cp / class.c
blobf5b9010c3a993a02d55ea27665e445a9ba144027
1 /* Functions related to building classes and their related objects.
2 Copyright (C) 1987-2014 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 "stringpool.h"
30 #include "stor-layout.h"
31 #include "attribs.h"
32 #include "pointer-set.h"
33 #include "hash-table.h"
34 #include "cp-tree.h"
35 #include "flags.h"
36 #include "toplev.h"
37 #include "target.h"
38 #include "convert.h"
39 #include "cgraph.h"
40 #include "dumpfile.h"
41 #include "splay-tree.h"
42 #include "gimplify.h"
44 /* The number of nested classes being processed. If we are not in the
45 scope of any class, this is zero. */
47 int current_class_depth;
49 /* In order to deal with nested classes, we keep a stack of classes.
50 The topmost entry is the innermost class, and is the entry at index
51 CURRENT_CLASS_DEPTH */
53 typedef struct class_stack_node {
54 /* The name of the class. */
55 tree name;
57 /* The _TYPE node for the class. */
58 tree type;
60 /* The access specifier pending for new declarations in the scope of
61 this class. */
62 tree access;
64 /* If were defining TYPE, the names used in this class. */
65 splay_tree names_used;
67 /* Nonzero if this class is no longer open, because of a call to
68 push_to_top_level. */
69 size_t hidden;
70 }* class_stack_node_t;
72 typedef struct vtbl_init_data_s
74 /* The base for which we're building initializers. */
75 tree binfo;
76 /* The type of the most-derived type. */
77 tree derived;
78 /* The binfo for the dynamic type. This will be TYPE_BINFO (derived),
79 unless ctor_vtbl_p is true. */
80 tree rtti_binfo;
81 /* The negative-index vtable initializers built up so far. These
82 are in order from least negative index to most negative index. */
83 vec<constructor_elt, va_gc> *inits;
84 /* The binfo for the virtual base for which we're building
85 vcall offset initializers. */
86 tree vbase;
87 /* The functions in vbase for which we have already provided vcall
88 offsets. */
89 vec<tree, va_gc> *fns;
90 /* The vtable index of the next vcall or vbase offset. */
91 tree index;
92 /* Nonzero if we are building the initializer for the primary
93 vtable. */
94 int primary_vtbl_p;
95 /* Nonzero if we are building the initializer for a construction
96 vtable. */
97 int ctor_vtbl_p;
98 /* True when adding vcall offset entries to the vtable. False when
99 merely computing the indices. */
100 bool generate_vcall_entries;
101 } vtbl_init_data;
103 /* The type of a function passed to walk_subobject_offsets. */
104 typedef int (*subobject_offset_fn) (tree, tree, splay_tree);
106 /* The stack itself. This is a dynamically resized array. The
107 number of elements allocated is CURRENT_CLASS_STACK_SIZE. */
108 static int current_class_stack_size;
109 static class_stack_node_t current_class_stack;
111 /* The size of the largest empty class seen in this translation unit. */
112 static GTY (()) tree sizeof_biggest_empty_class;
114 /* An array of all local classes present in this translation unit, in
115 declaration order. */
116 vec<tree, va_gc> *local_classes;
118 static tree get_vfield_name (tree);
119 static void finish_struct_anon (tree);
120 static tree get_vtable_name (tree);
121 static tree get_basefndecls (tree, tree);
122 static int build_primary_vtable (tree, tree);
123 static int build_secondary_vtable (tree);
124 static void finish_vtbls (tree);
125 static void modify_vtable_entry (tree, tree, tree, tree, tree *);
126 static void finish_struct_bits (tree);
127 static int alter_access (tree, tree, tree);
128 static void handle_using_decl (tree, tree);
129 static tree dfs_modify_vtables (tree, void *);
130 static tree modify_all_vtables (tree, tree);
131 static void determine_primary_bases (tree);
132 static void finish_struct_methods (tree);
133 static void maybe_warn_about_overly_private_class (tree);
134 static int method_name_cmp (const void *, const void *);
135 static int resort_method_name_cmp (const void *, const void *);
136 static void add_implicitly_declared_members (tree, tree*, int, int);
137 static tree fixed_type_or_null (tree, int *, int *);
138 static tree build_simple_base_path (tree expr, tree binfo);
139 static tree build_vtbl_ref_1 (tree, tree);
140 static void build_vtbl_initializer (tree, tree, tree, tree, int *,
141 vec<constructor_elt, va_gc> **);
142 static int count_fields (tree);
143 static int add_fields_to_record_type (tree, struct sorted_fields_type*, int);
144 static void insert_into_classtype_sorted_fields (tree, tree, int);
145 static bool check_bitfield_decl (tree);
146 static void check_field_decl (tree, tree, int *, int *, int *);
147 static void check_field_decls (tree, tree *, int *, int *);
148 static tree *build_base_field (record_layout_info, tree, splay_tree, tree *);
149 static void build_base_fields (record_layout_info, splay_tree, tree *);
150 static void check_methods (tree);
151 static void remove_zero_width_bit_fields (tree);
152 static void check_bases (tree, int *, int *);
153 static void check_bases_and_members (tree);
154 static tree create_vtable_ptr (tree, tree *);
155 static void include_empty_classes (record_layout_info);
156 static void layout_class_type (tree, tree *);
157 static void propagate_binfo_offsets (tree, tree);
158 static void layout_virtual_bases (record_layout_info, splay_tree);
159 static void build_vbase_offset_vtbl_entries (tree, vtbl_init_data *);
160 static void add_vcall_offset_vtbl_entries_r (tree, vtbl_init_data *);
161 static void add_vcall_offset_vtbl_entries_1 (tree, vtbl_init_data *);
162 static void build_vcall_offset_vtbl_entries (tree, vtbl_init_data *);
163 static void add_vcall_offset (tree, tree, vtbl_init_data *);
164 static void layout_vtable_decl (tree, int);
165 static tree dfs_find_final_overrider_pre (tree, void *);
166 static tree dfs_find_final_overrider_post (tree, void *);
167 static tree find_final_overrider (tree, tree, tree);
168 static int make_new_vtable (tree, tree);
169 static tree get_primary_binfo (tree);
170 static int maybe_indent_hierarchy (FILE *, int, int);
171 static tree dump_class_hierarchy_r (FILE *, int, tree, tree, int);
172 static void dump_class_hierarchy (tree);
173 static void dump_class_hierarchy_1 (FILE *, int, tree);
174 static void dump_array (FILE *, tree);
175 static void dump_vtable (tree, tree, tree);
176 static void dump_vtt (tree, tree);
177 static void dump_thunk (FILE *, int, tree);
178 static tree build_vtable (tree, tree, tree);
179 static void initialize_vtable (tree, vec<constructor_elt, va_gc> *);
180 static void layout_nonempty_base_or_field (record_layout_info,
181 tree, tree, splay_tree);
182 static tree end_of_class (tree, int);
183 static bool layout_empty_base (record_layout_info, tree, tree, splay_tree);
184 static void accumulate_vtbl_inits (tree, tree, tree, tree, tree,
185 vec<constructor_elt, va_gc> **);
186 static void dfs_accumulate_vtbl_inits (tree, tree, tree, tree, tree,
187 vec<constructor_elt, va_gc> **);
188 static void build_rtti_vtbl_entries (tree, vtbl_init_data *);
189 static void build_vcall_and_vbase_vtbl_entries (tree, vtbl_init_data *);
190 static void clone_constructors_and_destructors (tree);
191 static tree build_clone (tree, tree);
192 static void update_vtable_entry_for_fn (tree, tree, tree, tree *, unsigned);
193 static void build_ctor_vtbl_group (tree, tree);
194 static void build_vtt (tree);
195 static tree binfo_ctor_vtable (tree);
196 static void build_vtt_inits (tree, tree, vec<constructor_elt, va_gc> **,
197 tree *);
198 static tree dfs_build_secondary_vptr_vtt_inits (tree, void *);
199 static tree dfs_fixup_binfo_vtbls (tree, void *);
200 static int record_subobject_offset (tree, tree, splay_tree);
201 static int check_subobject_offset (tree, tree, splay_tree);
202 static int walk_subobject_offsets (tree, subobject_offset_fn,
203 tree, splay_tree, tree, int);
204 static void record_subobject_offsets (tree, tree, splay_tree, bool);
205 static int layout_conflict_p (tree, tree, splay_tree, int);
206 static int splay_tree_compare_integer_csts (splay_tree_key k1,
207 splay_tree_key k2);
208 static void warn_about_ambiguous_bases (tree);
209 static bool type_requires_array_cookie (tree);
210 static bool contains_empty_class_p (tree);
211 static bool base_derived_from (tree, tree);
212 static int empty_base_at_nonzero_offset_p (tree, tree, splay_tree);
213 static tree end_of_base (tree);
214 static tree get_vcall_index (tree, tree);
216 /* Variables shared between class.c and call.c. */
218 int n_vtables = 0;
219 int n_vtable_entries = 0;
220 int n_vtable_searches = 0;
221 int n_vtable_elems = 0;
222 int n_convert_harshness = 0;
223 int n_compute_conversion_costs = 0;
224 int n_inner_fields_searched = 0;
226 /* Convert to or from a base subobject. EXPR is an expression of type
227 `A' or `A*', an expression of type `B' or `B*' is returned. To
228 convert A to a base B, CODE is PLUS_EXPR and BINFO is the binfo for
229 the B base instance within A. To convert base A to derived B, CODE
230 is MINUS_EXPR and BINFO is the binfo for the A instance within B.
231 In this latter case, A must not be a morally virtual base of B.
232 NONNULL is true if EXPR is known to be non-NULL (this is only
233 needed when EXPR is of pointer type). CV qualifiers are preserved
234 from EXPR. */
236 tree
237 build_base_path (enum tree_code code,
238 tree expr,
239 tree binfo,
240 int nonnull,
241 tsubst_flags_t complain)
243 tree v_binfo = NULL_TREE;
244 tree d_binfo = NULL_TREE;
245 tree probe;
246 tree offset;
247 tree target_type;
248 tree null_test = NULL;
249 tree ptr_target_type;
250 int fixed_type_p;
251 int want_pointer = TYPE_PTR_P (TREE_TYPE (expr));
252 bool has_empty = false;
253 bool virtual_access;
255 if (expr == error_mark_node || binfo == error_mark_node || !binfo)
256 return error_mark_node;
258 for (probe = binfo; probe; probe = BINFO_INHERITANCE_CHAIN (probe))
260 d_binfo = probe;
261 if (is_empty_class (BINFO_TYPE (probe)))
262 has_empty = true;
263 if (!v_binfo && BINFO_VIRTUAL_P (probe))
264 v_binfo = probe;
267 probe = TYPE_MAIN_VARIANT (TREE_TYPE (expr));
268 if (want_pointer)
269 probe = TYPE_MAIN_VARIANT (TREE_TYPE (probe));
271 if (code == PLUS_EXPR
272 && !SAME_BINFO_TYPE_P (BINFO_TYPE (d_binfo), probe))
274 /* This can happen when adjust_result_of_qualified_name_lookup can't
275 find a unique base binfo in a call to a member function. We
276 couldn't give the diagnostic then since we might have been calling
277 a static member function, so we do it now. */
278 if (complain & tf_error)
280 tree base = lookup_base (probe, BINFO_TYPE (d_binfo),
281 ba_unique, NULL, complain);
282 gcc_assert (base == error_mark_node);
284 return error_mark_node;
287 gcc_assert ((code == MINUS_EXPR
288 && SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), probe))
289 || code == PLUS_EXPR);
291 if (binfo == d_binfo)
292 /* Nothing to do. */
293 return expr;
295 if (code == MINUS_EXPR && v_binfo)
297 if (complain & tf_error)
299 if (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), BINFO_TYPE (v_binfo)))
301 if (want_pointer)
302 error ("cannot convert from pointer to base class %qT to "
303 "pointer to derived class %qT because the base is "
304 "virtual", BINFO_TYPE (binfo), BINFO_TYPE (d_binfo));
305 else
306 error ("cannot convert from base class %qT to derived "
307 "class %qT because the base is virtual",
308 BINFO_TYPE (binfo), BINFO_TYPE (d_binfo));
310 else
312 if (want_pointer)
313 error ("cannot convert from pointer to base class %qT to "
314 "pointer to derived class %qT via virtual base %qT",
315 BINFO_TYPE (binfo), BINFO_TYPE (d_binfo),
316 BINFO_TYPE (v_binfo));
317 else
318 error ("cannot convert from base class %qT to derived "
319 "class %qT via virtual base %qT", BINFO_TYPE (binfo),
320 BINFO_TYPE (d_binfo), BINFO_TYPE (v_binfo));
323 return error_mark_node;
326 if (!want_pointer)
327 /* This must happen before the call to save_expr. */
328 expr = cp_build_addr_expr (expr, complain);
329 else
330 expr = mark_rvalue_use (expr);
332 offset = BINFO_OFFSET (binfo);
333 fixed_type_p = resolves_to_fixed_type_p (expr, &nonnull);
334 target_type = code == PLUS_EXPR ? BINFO_TYPE (binfo) : BINFO_TYPE (d_binfo);
335 /* TARGET_TYPE has been extracted from BINFO, and, is therefore always
336 cv-unqualified. Extract the cv-qualifiers from EXPR so that the
337 expression returned matches the input. */
338 target_type = cp_build_qualified_type
339 (target_type, cp_type_quals (TREE_TYPE (TREE_TYPE (expr))));
340 ptr_target_type = build_pointer_type (target_type);
342 /* Do we need to look in the vtable for the real offset? */
343 virtual_access = (v_binfo && fixed_type_p <= 0);
345 /* Don't bother with the calculations inside sizeof; they'll ICE if the
346 source type is incomplete and the pointer value doesn't matter. In a
347 template (even in fold_non_dependent_expr), we don't have vtables set
348 up properly yet, and the value doesn't matter there either; we're just
349 interested in the result of overload resolution. */
350 if (cp_unevaluated_operand != 0
351 || in_template_function ())
353 expr = build_nop (ptr_target_type, expr);
354 if (!want_pointer)
355 expr = build_indirect_ref (EXPR_LOCATION (expr), expr, RO_NULL);
356 return expr;
359 /* If we're in an NSDMI, we don't have the full constructor context yet
360 that we need for converting to a virtual base, so just build a stub
361 CONVERT_EXPR and expand it later in bot_replace. */
362 if (virtual_access && fixed_type_p < 0
363 && current_scope () != current_function_decl)
365 expr = build1 (CONVERT_EXPR, ptr_target_type, expr);
366 CONVERT_EXPR_VBASE_PATH (expr) = true;
367 if (!want_pointer)
368 expr = build_indirect_ref (EXPR_LOCATION (expr), expr, RO_NULL);
369 return expr;
372 /* Do we need to check for a null pointer? */
373 if (want_pointer && !nonnull)
375 /* If we know the conversion will not actually change the value
376 of EXPR, then we can avoid testing the expression for NULL.
377 We have to avoid generating a COMPONENT_REF for a base class
378 field, because other parts of the compiler know that such
379 expressions are always non-NULL. */
380 if (!virtual_access && integer_zerop (offset))
381 return build_nop (ptr_target_type, expr);
382 null_test = error_mark_node;
385 /* Protect against multiple evaluation if necessary. */
386 if (TREE_SIDE_EFFECTS (expr) && (null_test || virtual_access))
387 expr = save_expr (expr);
389 /* Now that we've saved expr, build the real null test. */
390 if (null_test)
392 tree zero = cp_convert (TREE_TYPE (expr), nullptr_node, complain);
393 null_test = fold_build2_loc (input_location, NE_EXPR, boolean_type_node,
394 expr, zero);
397 /* If this is a simple base reference, express it as a COMPONENT_REF. */
398 if (code == PLUS_EXPR && !virtual_access
399 /* We don't build base fields for empty bases, and they aren't very
400 interesting to the optimizers anyway. */
401 && !has_empty)
403 expr = cp_build_indirect_ref (expr, RO_NULL, complain);
404 expr = build_simple_base_path (expr, binfo);
405 if (want_pointer)
406 expr = build_address (expr);
407 target_type = TREE_TYPE (expr);
408 goto out;
411 if (virtual_access)
413 /* Going via virtual base V_BINFO. We need the static offset
414 from V_BINFO to BINFO, and the dynamic offset from D_BINFO to
415 V_BINFO. That offset is an entry in D_BINFO's vtable. */
416 tree v_offset;
418 if (fixed_type_p < 0 && in_base_initializer)
420 /* In a base member initializer, we cannot rely on the
421 vtable being set up. We have to indirect via the
422 vtt_parm. */
423 tree t;
425 t = TREE_TYPE (TYPE_VFIELD (current_class_type));
426 t = build_pointer_type (t);
427 v_offset = convert (t, current_vtt_parm);
428 v_offset = cp_build_indirect_ref (v_offset, RO_NULL, complain);
430 else
431 v_offset = build_vfield_ref (cp_build_indirect_ref (expr, RO_NULL,
432 complain),
433 TREE_TYPE (TREE_TYPE (expr)));
435 if (v_offset == error_mark_node)
436 return error_mark_node;
438 v_offset = fold_build_pointer_plus (v_offset, BINFO_VPTR_FIELD (v_binfo));
439 v_offset = build1 (NOP_EXPR,
440 build_pointer_type (ptrdiff_type_node),
441 v_offset);
442 v_offset = cp_build_indirect_ref (v_offset, RO_NULL, complain);
443 TREE_CONSTANT (v_offset) = 1;
445 offset = convert_to_integer (ptrdiff_type_node,
446 size_diffop_loc (input_location, offset,
447 BINFO_OFFSET (v_binfo)));
449 if (!integer_zerop (offset))
450 v_offset = build2 (code, ptrdiff_type_node, v_offset, offset);
452 if (fixed_type_p < 0)
453 /* Negative fixed_type_p means this is a constructor or destructor;
454 virtual base layout is fixed in in-charge [cd]tors, but not in
455 base [cd]tors. */
456 offset = build3 (COND_EXPR, ptrdiff_type_node,
457 build2 (EQ_EXPR, boolean_type_node,
458 current_in_charge_parm, integer_zero_node),
459 v_offset,
460 convert_to_integer (ptrdiff_type_node,
461 BINFO_OFFSET (binfo)));
462 else
463 offset = v_offset;
466 if (want_pointer)
467 target_type = ptr_target_type;
469 expr = build1 (NOP_EXPR, ptr_target_type, expr);
471 if (!integer_zerop (offset))
473 offset = fold_convert (sizetype, offset);
474 if (code == MINUS_EXPR)
475 offset = fold_build1_loc (input_location, NEGATE_EXPR, sizetype, offset);
476 expr = fold_build_pointer_plus (expr, offset);
478 else
479 null_test = NULL;
481 if (!want_pointer)
482 expr = cp_build_indirect_ref (expr, RO_NULL, complain);
484 out:
485 if (null_test)
486 expr = fold_build3_loc (input_location, COND_EXPR, target_type, null_test, expr,
487 build_zero_cst (target_type));
489 return expr;
492 /* Subroutine of build_base_path; EXPR and BINFO are as in that function.
493 Perform a derived-to-base conversion by recursively building up a
494 sequence of COMPONENT_REFs to the appropriate base fields. */
496 static tree
497 build_simple_base_path (tree expr, tree binfo)
499 tree type = BINFO_TYPE (binfo);
500 tree d_binfo = BINFO_INHERITANCE_CHAIN (binfo);
501 tree field;
503 if (d_binfo == NULL_TREE)
505 tree temp;
507 gcc_assert (TYPE_MAIN_VARIANT (TREE_TYPE (expr)) == type);
509 /* Transform `(a, b).x' into `(*(a, &b)).x', `(a ? b : c).x'
510 into `(*(a ? &b : &c)).x', and so on. A COND_EXPR is only
511 an lvalue in the front end; only _DECLs and _REFs are lvalues
512 in the back end. */
513 temp = unary_complex_lvalue (ADDR_EXPR, expr);
514 if (temp)
515 expr = cp_build_indirect_ref (temp, RO_NULL, tf_warning_or_error);
517 return expr;
520 /* Recurse. */
521 expr = build_simple_base_path (expr, d_binfo);
523 for (field = TYPE_FIELDS (BINFO_TYPE (d_binfo));
524 field; field = DECL_CHAIN (field))
525 /* Is this the base field created by build_base_field? */
526 if (TREE_CODE (field) == FIELD_DECL
527 && DECL_FIELD_IS_BASE (field)
528 && TREE_TYPE (field) == type
529 /* If we're looking for a field in the most-derived class,
530 also check the field offset; we can have two base fields
531 of the same type if one is an indirect virtual base and one
532 is a direct non-virtual base. */
533 && (BINFO_INHERITANCE_CHAIN (d_binfo)
534 || tree_int_cst_equal (byte_position (field),
535 BINFO_OFFSET (binfo))))
537 /* We don't use build_class_member_access_expr here, as that
538 has unnecessary checks, and more importantly results in
539 recursive calls to dfs_walk_once. */
540 int type_quals = cp_type_quals (TREE_TYPE (expr));
542 expr = build3 (COMPONENT_REF,
543 cp_build_qualified_type (type, type_quals),
544 expr, field, NULL_TREE);
545 expr = fold_if_not_in_template (expr);
547 /* Mark the expression const or volatile, as appropriate.
548 Even though we've dealt with the type above, we still have
549 to mark the expression itself. */
550 if (type_quals & TYPE_QUAL_CONST)
551 TREE_READONLY (expr) = 1;
552 if (type_quals & TYPE_QUAL_VOLATILE)
553 TREE_THIS_VOLATILE (expr) = 1;
555 return expr;
558 /* Didn't find the base field?!? */
559 gcc_unreachable ();
562 /* Convert OBJECT to the base TYPE. OBJECT is an expression whose
563 type is a class type or a pointer to a class type. In the former
564 case, TYPE is also a class type; in the latter it is another
565 pointer type. If CHECK_ACCESS is true, an error message is emitted
566 if TYPE is inaccessible. If OBJECT has pointer type, the value is
567 assumed to be non-NULL. */
569 tree
570 convert_to_base (tree object, tree type, bool check_access, bool nonnull,
571 tsubst_flags_t complain)
573 tree binfo;
574 tree object_type;
576 if (TYPE_PTR_P (TREE_TYPE (object)))
578 object_type = TREE_TYPE (TREE_TYPE (object));
579 type = TREE_TYPE (type);
581 else
582 object_type = TREE_TYPE (object);
584 binfo = lookup_base (object_type, type, check_access ? ba_check : ba_unique,
585 NULL, complain);
586 if (!binfo || binfo == error_mark_node)
587 return error_mark_node;
589 return build_base_path (PLUS_EXPR, object, binfo, nonnull, complain);
592 /* EXPR is an expression with unqualified class type. BASE is a base
593 binfo of that class type. Returns EXPR, converted to the BASE
594 type. This function assumes that EXPR is the most derived class;
595 therefore virtual bases can be found at their static offsets. */
597 tree
598 convert_to_base_statically (tree expr, tree base)
600 tree expr_type;
602 expr_type = TREE_TYPE (expr);
603 if (!SAME_BINFO_TYPE_P (BINFO_TYPE (base), expr_type))
605 /* If this is a non-empty base, use a COMPONENT_REF. */
606 if (!is_empty_class (BINFO_TYPE (base)))
607 return build_simple_base_path (expr, base);
609 /* We use fold_build2 and fold_convert below to simplify the trees
610 provided to the optimizers. It is not safe to call these functions
611 when processing a template because they do not handle C++-specific
612 trees. */
613 gcc_assert (!processing_template_decl);
614 expr = cp_build_addr_expr (expr, tf_warning_or_error);
615 if (!integer_zerop (BINFO_OFFSET (base)))
616 expr = fold_build_pointer_plus_loc (input_location,
617 expr, BINFO_OFFSET (base));
618 expr = fold_convert (build_pointer_type (BINFO_TYPE (base)), expr);
619 expr = build_fold_indirect_ref_loc (input_location, expr);
622 return expr;
626 tree
627 build_vfield_ref (tree datum, tree type)
629 tree vfield, vcontext;
631 if (datum == error_mark_node
632 /* Can happen in case of duplicate base types (c++/59082). */
633 || !TYPE_VFIELD (type))
634 return error_mark_node;
636 /* First, convert to the requested type. */
637 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (datum), type))
638 datum = convert_to_base (datum, type, /*check_access=*/false,
639 /*nonnull=*/true, tf_warning_or_error);
641 /* Second, the requested type may not be the owner of its own vptr.
642 If not, convert to the base class that owns it. We cannot use
643 convert_to_base here, because VCONTEXT may appear more than once
644 in the inheritance hierarchy of TYPE, and thus direct conversion
645 between the types may be ambiguous. Following the path back up
646 one step at a time via primary bases avoids the problem. */
647 vfield = TYPE_VFIELD (type);
648 vcontext = DECL_CONTEXT (vfield);
649 while (!same_type_ignoring_top_level_qualifiers_p (vcontext, type))
651 datum = build_simple_base_path (datum, CLASSTYPE_PRIMARY_BINFO (type));
652 type = TREE_TYPE (datum);
655 return build3 (COMPONENT_REF, TREE_TYPE (vfield), datum, vfield, NULL_TREE);
658 /* Given an object INSTANCE, return an expression which yields the
659 vtable element corresponding to INDEX. There are many special
660 cases for INSTANCE which we take care of here, mainly to avoid
661 creating extra tree nodes when we don't have to. */
663 static tree
664 build_vtbl_ref_1 (tree instance, tree idx)
666 tree aref;
667 tree vtbl = NULL_TREE;
669 /* Try to figure out what a reference refers to, and
670 access its virtual function table directly. */
672 int cdtorp = 0;
673 tree fixed_type = fixed_type_or_null (instance, NULL, &cdtorp);
675 tree basetype = non_reference (TREE_TYPE (instance));
677 if (fixed_type && !cdtorp)
679 tree binfo = lookup_base (fixed_type, basetype,
680 ba_unique, NULL, tf_none);
681 if (binfo && binfo != error_mark_node)
682 vtbl = unshare_expr (BINFO_VTABLE (binfo));
685 if (!vtbl)
686 vtbl = build_vfield_ref (instance, basetype);
688 aref = build_array_ref (input_location, vtbl, idx);
689 TREE_CONSTANT (aref) |= TREE_CONSTANT (vtbl) && TREE_CONSTANT (idx);
691 return aref;
694 tree
695 build_vtbl_ref (tree instance, tree idx)
697 tree aref = build_vtbl_ref_1 (instance, idx);
699 return aref;
702 /* Given a stable object pointer INSTANCE_PTR, return an expression which
703 yields a function pointer corresponding to vtable element INDEX. */
705 tree
706 build_vfn_ref (tree instance_ptr, tree idx)
708 tree aref;
710 aref = build_vtbl_ref_1 (cp_build_indirect_ref (instance_ptr, RO_NULL,
711 tf_warning_or_error),
712 idx);
714 /* When using function descriptors, the address of the
715 vtable entry is treated as a function pointer. */
716 if (TARGET_VTABLE_USES_DESCRIPTORS)
717 aref = build1 (NOP_EXPR, TREE_TYPE (aref),
718 cp_build_addr_expr (aref, tf_warning_or_error));
720 /* Remember this as a method reference, for later devirtualization. */
721 aref = build3 (OBJ_TYPE_REF, TREE_TYPE (aref), aref, instance_ptr, idx);
723 return aref;
726 /* Return the name of the virtual function table (as an IDENTIFIER_NODE)
727 for the given TYPE. */
729 static tree
730 get_vtable_name (tree type)
732 return mangle_vtbl_for_type (type);
735 /* DECL is an entity associated with TYPE, like a virtual table or an
736 implicitly generated constructor. Determine whether or not DECL
737 should have external or internal linkage at the object file
738 level. This routine does not deal with COMDAT linkage and other
739 similar complexities; it simply sets TREE_PUBLIC if it possible for
740 entities in other translation units to contain copies of DECL, in
741 the abstract. */
743 void
744 set_linkage_according_to_type (tree /*type*/, tree decl)
746 TREE_PUBLIC (decl) = 1;
747 determine_visibility (decl);
750 /* Create a VAR_DECL for a primary or secondary vtable for CLASS_TYPE.
751 (For a secondary vtable for B-in-D, CLASS_TYPE should be D, not B.)
752 Use NAME for the name of the vtable, and VTABLE_TYPE for its type. */
754 static tree
755 build_vtable (tree class_type, tree name, tree vtable_type)
757 tree decl;
759 decl = build_lang_decl (VAR_DECL, name, vtable_type);
760 /* vtable names are already mangled; give them their DECL_ASSEMBLER_NAME
761 now to avoid confusion in mangle_decl. */
762 SET_DECL_ASSEMBLER_NAME (decl, name);
763 DECL_CONTEXT (decl) = class_type;
764 DECL_ARTIFICIAL (decl) = 1;
765 TREE_STATIC (decl) = 1;
766 TREE_READONLY (decl) = 1;
767 DECL_VIRTUAL_P (decl) = 1;
768 DECL_ALIGN (decl) = TARGET_VTABLE_ENTRY_ALIGN;
769 DECL_VTABLE_OR_VTT_P (decl) = 1;
770 /* At one time the vtable info was grabbed 2 words at a time. This
771 fails on sparc unless you have 8-byte alignment. (tiemann) */
772 DECL_ALIGN (decl) = MAX (TYPE_ALIGN (double_type_node),
773 DECL_ALIGN (decl));
774 set_linkage_according_to_type (class_type, decl);
775 /* The vtable has not been defined -- yet. */
776 DECL_EXTERNAL (decl) = 1;
777 DECL_NOT_REALLY_EXTERN (decl) = 1;
779 /* Mark the VAR_DECL node representing the vtable itself as a
780 "gratuitous" one, thereby forcing dwarfout.c to ignore it. It
781 is rather important that such things be ignored because any
782 effort to actually generate DWARF for them will run into
783 trouble when/if we encounter code like:
785 #pragma interface
786 struct S { virtual void member (); };
788 because the artificial declaration of the vtable itself (as
789 manufactured by the g++ front end) will say that the vtable is
790 a static member of `S' but only *after* the debug output for
791 the definition of `S' has already been output. This causes
792 grief because the DWARF entry for the definition of the vtable
793 will try to refer back to an earlier *declaration* of the
794 vtable as a static member of `S' and there won't be one. We
795 might be able to arrange to have the "vtable static member"
796 attached to the member list for `S' before the debug info for
797 `S' get written (which would solve the problem) but that would
798 require more intrusive changes to the g++ front end. */
799 DECL_IGNORED_P (decl) = 1;
801 return decl;
804 /* Get the VAR_DECL of the vtable for TYPE. TYPE need not be polymorphic,
805 or even complete. If this does not exist, create it. If COMPLETE is
806 nonzero, then complete the definition of it -- that will render it
807 impossible to actually build the vtable, but is useful to get at those
808 which are known to exist in the runtime. */
810 tree
811 get_vtable_decl (tree type, int complete)
813 tree decl;
815 if (CLASSTYPE_VTABLES (type))
816 return CLASSTYPE_VTABLES (type);
818 decl = build_vtable (type, get_vtable_name (type), vtbl_type_node);
819 CLASSTYPE_VTABLES (type) = decl;
821 if (complete)
823 DECL_EXTERNAL (decl) = 1;
824 cp_finish_decl (decl, NULL_TREE, false, NULL_TREE, 0);
827 return decl;
830 /* Build the primary virtual function table for TYPE. If BINFO is
831 non-NULL, build the vtable starting with the initial approximation
832 that it is the same as the one which is the head of the association
833 list. Returns a nonzero value if a new vtable is actually
834 created. */
836 static int
837 build_primary_vtable (tree binfo, tree type)
839 tree decl;
840 tree virtuals;
842 decl = get_vtable_decl (type, /*complete=*/0);
844 if (binfo)
846 if (BINFO_NEW_VTABLE_MARKED (binfo))
847 /* We have already created a vtable for this base, so there's
848 no need to do it again. */
849 return 0;
851 virtuals = copy_list (BINFO_VIRTUALS (binfo));
852 TREE_TYPE (decl) = TREE_TYPE (get_vtbl_decl_for_binfo (binfo));
853 DECL_SIZE (decl) = TYPE_SIZE (TREE_TYPE (decl));
854 DECL_SIZE_UNIT (decl) = TYPE_SIZE_UNIT (TREE_TYPE (decl));
856 else
858 gcc_assert (TREE_TYPE (decl) == vtbl_type_node);
859 virtuals = NULL_TREE;
862 if (GATHER_STATISTICS)
864 n_vtables += 1;
865 n_vtable_elems += list_length (virtuals);
868 /* Initialize the association list for this type, based
869 on our first approximation. */
870 BINFO_VTABLE (TYPE_BINFO (type)) = decl;
871 BINFO_VIRTUALS (TYPE_BINFO (type)) = virtuals;
872 SET_BINFO_NEW_VTABLE_MARKED (TYPE_BINFO (type));
873 return 1;
876 /* Give BINFO a new virtual function table which is initialized
877 with a skeleton-copy of its original initialization. The only
878 entry that changes is the `delta' entry, so we can really
879 share a lot of structure.
881 FOR_TYPE is the most derived type which caused this table to
882 be needed.
884 Returns nonzero if we haven't met BINFO before.
886 The order in which vtables are built (by calling this function) for
887 an object must remain the same, otherwise a binary incompatibility
888 can result. */
890 static int
891 build_secondary_vtable (tree binfo)
893 if (BINFO_NEW_VTABLE_MARKED (binfo))
894 /* We already created a vtable for this base. There's no need to
895 do it again. */
896 return 0;
898 /* Remember that we've created a vtable for this BINFO, so that we
899 don't try to do so again. */
900 SET_BINFO_NEW_VTABLE_MARKED (binfo);
902 /* Make fresh virtual list, so we can smash it later. */
903 BINFO_VIRTUALS (binfo) = copy_list (BINFO_VIRTUALS (binfo));
905 /* Secondary vtables are laid out as part of the same structure as
906 the primary vtable. */
907 BINFO_VTABLE (binfo) = NULL_TREE;
908 return 1;
911 /* Create a new vtable for BINFO which is the hierarchy dominated by
912 T. Return nonzero if we actually created a new vtable. */
914 static int
915 make_new_vtable (tree t, tree binfo)
917 if (binfo == TYPE_BINFO (t))
918 /* In this case, it is *type*'s vtable we are modifying. We start
919 with the approximation that its vtable is that of the
920 immediate base class. */
921 return build_primary_vtable (binfo, t);
922 else
923 /* This is our very own copy of `basetype' to play with. Later,
924 we will fill in all the virtual functions that override the
925 virtual functions in these base classes which are not defined
926 by the current type. */
927 return build_secondary_vtable (binfo);
930 /* Make *VIRTUALS, an entry on the BINFO_VIRTUALS list for BINFO
931 (which is in the hierarchy dominated by T) list FNDECL as its
932 BV_FN. DELTA is the required constant adjustment from the `this'
933 pointer where the vtable entry appears to the `this' required when
934 the function is actually called. */
936 static void
937 modify_vtable_entry (tree t,
938 tree binfo,
939 tree fndecl,
940 tree delta,
941 tree *virtuals)
943 tree v;
945 v = *virtuals;
947 if (fndecl != BV_FN (v)
948 || !tree_int_cst_equal (delta, BV_DELTA (v)))
950 /* We need a new vtable for BINFO. */
951 if (make_new_vtable (t, binfo))
953 /* If we really did make a new vtable, we also made a copy
954 of the BINFO_VIRTUALS list. Now, we have to find the
955 corresponding entry in that list. */
956 *virtuals = BINFO_VIRTUALS (binfo);
957 while (BV_FN (*virtuals) != BV_FN (v))
958 *virtuals = TREE_CHAIN (*virtuals);
959 v = *virtuals;
962 BV_DELTA (v) = delta;
963 BV_VCALL_INDEX (v) = NULL_TREE;
964 BV_FN (v) = fndecl;
968 // Returns the template associated with the member FN or
969 // NULL if the declaration is neither a template nor temploid.
970 static inline tree
971 get_member_fn_template (tree fn)
973 if (TREE_CODE (fn) == TEMPLATE_DECL)
974 return fn;
975 if (TREE_CODE (fn) == FUNCTION_DECL && DECL_TEMPLATE_INFO (fn))
976 return DECL_TI_TEMPLATE (fn);
977 return NULL_TREE;
980 // Returns true if NEWDECL and OLDDECL are member functions with with
981 // different constraints. If NEWDECL and OLDDECL are non-template members
982 // or specializations of non-template members, the overloads are
983 // differentiated by the template constraints.
985 // Note that the types of the functions are assumed to be equivalent.
986 static inline bool
987 are_constrained_member_overloads (tree newdecl, tree olddecl)
989 newdecl = get_member_fn_template (newdecl);
990 olddecl = get_member_fn_template (olddecl);
992 // If neither is a template or temploid, then they cannot
993 // be constrained declarations.
994 if (!newdecl && !olddecl)
995 return false;
996 else
997 return !equivalently_constrained (newdecl, olddecl);
1001 /* Add method METHOD to class TYPE. If USING_DECL is non-null, it is
1002 the USING_DECL naming METHOD. Returns true if the method could be
1003 added to the method vec. */
1005 bool
1006 add_method (tree type, tree method, tree using_decl)
1008 unsigned slot;
1009 tree overload;
1010 bool template_conv_p = false;
1011 bool conv_p;
1012 vec<tree, va_gc> *method_vec;
1013 bool complete_p;
1014 bool insert_p = false;
1015 tree current_fns;
1016 tree fns;
1018 if (method == error_mark_node)
1019 return false;
1021 complete_p = COMPLETE_TYPE_P (type);
1022 conv_p = DECL_CONV_FN_P (method);
1023 if (conv_p)
1024 template_conv_p = (TREE_CODE (method) == TEMPLATE_DECL
1025 && DECL_TEMPLATE_CONV_FN_P (method));
1027 method_vec = CLASSTYPE_METHOD_VEC (type);
1028 if (!method_vec)
1030 /* Make a new method vector. We start with 8 entries. We must
1031 allocate at least two (for constructors and destructors), and
1032 we're going to end up with an assignment operator at some
1033 point as well. */
1034 vec_alloc (method_vec, 8);
1035 /* Create slots for constructors and destructors. */
1036 method_vec->quick_push (NULL_TREE);
1037 method_vec->quick_push (NULL_TREE);
1038 CLASSTYPE_METHOD_VEC (type) = method_vec;
1041 /* Maintain TYPE_HAS_USER_CONSTRUCTOR, etc. */
1042 grok_special_member_properties (method);
1044 /* Constructors and destructors go in special slots. */
1045 if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (method))
1046 slot = CLASSTYPE_CONSTRUCTOR_SLOT;
1047 else if (DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (method))
1049 slot = CLASSTYPE_DESTRUCTOR_SLOT;
1051 if (TYPE_FOR_JAVA (type))
1053 if (!DECL_ARTIFICIAL (method))
1054 error ("Java class %qT cannot have a destructor", type);
1055 else if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
1056 error ("Java class %qT cannot have an implicit non-trivial "
1057 "destructor",
1058 type);
1061 else
1063 tree m;
1065 insert_p = true;
1066 /* See if we already have an entry with this name. */
1067 for (slot = CLASSTYPE_FIRST_CONVERSION_SLOT;
1068 vec_safe_iterate (method_vec, slot, &m);
1069 ++slot)
1071 m = OVL_CURRENT (m);
1072 if (template_conv_p)
1074 if (TREE_CODE (m) == TEMPLATE_DECL
1075 && DECL_TEMPLATE_CONV_FN_P (m))
1076 insert_p = false;
1077 break;
1079 if (conv_p && !DECL_CONV_FN_P (m))
1080 break;
1081 if (DECL_NAME (m) == DECL_NAME (method))
1083 insert_p = false;
1084 break;
1086 if (complete_p
1087 && !DECL_CONV_FN_P (m)
1088 && DECL_NAME (m) > DECL_NAME (method))
1089 break;
1092 current_fns = insert_p ? NULL_TREE : (*method_vec)[slot];
1094 /* Check to see if we've already got this method. */
1095 for (fns = current_fns; fns; fns = OVL_NEXT (fns))
1097 tree fn = OVL_CURRENT (fns);
1098 tree fn_type;
1099 tree method_type;
1100 tree parms1;
1101 tree parms2;
1103 if (TREE_CODE (fn) != TREE_CODE (method))
1104 continue;
1106 /* [over.load] Member function declarations with the
1107 same name and the same parameter types cannot be
1108 overloaded if any of them is a static member
1109 function declaration.
1111 [over.load] Member function declarations with the same name and
1112 the same parameter-type-list as well as member function template
1113 declarations with the same name, the same parameter-type-list, and
1114 the same template parameter lists cannot be overloaded if any of
1115 them, but not all, have a ref-qualifier.
1117 [namespace.udecl] When a using-declaration brings names
1118 from a base class into a derived class scope, member
1119 functions in the derived class override and/or hide member
1120 functions with the same name and parameter types in a base
1121 class (rather than conflicting). */
1122 fn_type = TREE_TYPE (fn);
1123 method_type = TREE_TYPE (method);
1124 parms1 = TYPE_ARG_TYPES (fn_type);
1125 parms2 = TYPE_ARG_TYPES (method_type);
1127 /* Compare the quals on the 'this' parm. Don't compare
1128 the whole types, as used functions are treated as
1129 coming from the using class in overload resolution. */
1130 if (! DECL_STATIC_FUNCTION_P (fn)
1131 && ! DECL_STATIC_FUNCTION_P (method)
1132 /* Either both or neither need to be ref-qualified for
1133 differing quals to allow overloading. */
1134 && (FUNCTION_REF_QUALIFIED (fn_type)
1135 == FUNCTION_REF_QUALIFIED (method_type))
1136 && (type_memfn_quals (fn_type) != type_memfn_quals (method_type)
1137 || type_memfn_rqual (fn_type) != type_memfn_rqual (method_type)))
1138 continue;
1140 /* For templates, the return type and template parameters
1141 must be identical. */
1142 if (TREE_CODE (fn) == TEMPLATE_DECL
1143 && (!same_type_p (TREE_TYPE (fn_type),
1144 TREE_TYPE (method_type))
1145 || !comp_template_parms (DECL_TEMPLATE_PARMS (fn),
1146 DECL_TEMPLATE_PARMS (method))))
1147 continue;
1149 if (! DECL_STATIC_FUNCTION_P (fn))
1150 parms1 = TREE_CHAIN (parms1);
1151 if (! DECL_STATIC_FUNCTION_P (method))
1152 parms2 = TREE_CHAIN (parms2);
1154 if (compparms (parms1, parms2)
1155 && (!DECL_CONV_FN_P (fn)
1156 || same_type_p (TREE_TYPE (fn_type),
1157 TREE_TYPE (method_type))))
1159 /* For function versions, their parms and types match
1160 but they are not duplicates. Record function versions
1161 as and when they are found. extern "C" functions are
1162 not treated as versions. */
1163 if (TREE_CODE (fn) == FUNCTION_DECL
1164 && TREE_CODE (method) == FUNCTION_DECL
1165 && !DECL_EXTERN_C_P (fn)
1166 && !DECL_EXTERN_C_P (method)
1167 && targetm.target_option.function_versions (fn, method))
1169 /* Mark functions as versions if necessary. Modify the mangled
1170 decl name if necessary. */
1171 if (!DECL_FUNCTION_VERSIONED (fn))
1173 DECL_FUNCTION_VERSIONED (fn) = 1;
1174 if (DECL_ASSEMBLER_NAME_SET_P (fn))
1175 mangle_decl (fn);
1177 if (!DECL_FUNCTION_VERSIONED (method))
1179 DECL_FUNCTION_VERSIONED (method) = 1;
1180 if (DECL_ASSEMBLER_NAME_SET_P (method))
1181 mangle_decl (method);
1183 record_function_versions (fn, method);
1184 continue;
1186 if (DECL_INHERITED_CTOR_BASE (method))
1188 if (DECL_INHERITED_CTOR_BASE (fn))
1190 error_at (DECL_SOURCE_LOCATION (method),
1191 "%q#D inherited from %qT", method,
1192 DECL_INHERITED_CTOR_BASE (method));
1193 error_at (DECL_SOURCE_LOCATION (fn),
1194 "conflicts with version inherited from %qT",
1195 DECL_INHERITED_CTOR_BASE (fn));
1197 /* Otherwise defer to the other function. */
1198 return false;
1200 if (using_decl)
1202 if (DECL_CONTEXT (fn) == type)
1203 /* Defer to the local function. */
1204 return false;
1206 else if (are_constrained_member_overloads (fn, method))
1207 continue;
1208 else
1210 error ("%q+#D cannot be overloaded", method);
1211 error ("with %q+#D", fn);
1214 /* We don't call duplicate_decls here to merge the
1215 declarations because that will confuse things if the
1216 methods have inline definitions. In particular, we
1217 will crash while processing the definitions. */
1218 return false;
1222 /* A class should never have more than one destructor. */
1223 if (current_fns && DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (method))
1224 return false;
1226 /* Add the new binding. */
1227 if (using_decl)
1229 overload = ovl_cons (method, current_fns);
1230 OVL_USED (overload) = true;
1232 else
1233 overload = build_overload (method, current_fns);
1235 if (conv_p)
1236 TYPE_HAS_CONVERSION (type) = 1;
1237 else if (slot >= CLASSTYPE_FIRST_CONVERSION_SLOT && !complete_p)
1238 push_class_level_binding (DECL_NAME (method), overload);
1240 if (insert_p)
1242 bool reallocated;
1244 /* We only expect to add few methods in the COMPLETE_P case, so
1245 just make room for one more method in that case. */
1246 if (complete_p)
1247 reallocated = vec_safe_reserve_exact (method_vec, 1);
1248 else
1249 reallocated = vec_safe_reserve (method_vec, 1);
1250 if (reallocated)
1251 CLASSTYPE_METHOD_VEC (type) = method_vec;
1252 if (slot == method_vec->length ())
1253 method_vec->quick_push (overload);
1254 else
1255 method_vec->quick_insert (slot, overload);
1257 else
1258 /* Replace the current slot. */
1259 (*method_vec)[slot] = overload;
1260 return true;
1263 /* Subroutines of finish_struct. */
1265 /* Change the access of FDECL to ACCESS in T. Return 1 if change was
1266 legit, otherwise return 0. */
1268 static int
1269 alter_access (tree t, tree fdecl, tree access)
1271 tree elem;
1273 if (!DECL_LANG_SPECIFIC (fdecl))
1274 retrofit_lang_decl (fdecl);
1276 gcc_assert (!DECL_DISCRIMINATOR_P (fdecl));
1278 elem = purpose_member (t, DECL_ACCESS (fdecl));
1279 if (elem)
1281 if (TREE_VALUE (elem) != access)
1283 if (TREE_CODE (TREE_TYPE (fdecl)) == FUNCTION_DECL)
1284 error ("conflicting access specifications for method"
1285 " %q+D, ignored", TREE_TYPE (fdecl));
1286 else
1287 error ("conflicting access specifications for field %qE, ignored",
1288 DECL_NAME (fdecl));
1290 else
1292 /* They're changing the access to the same thing they changed
1293 it to before. That's OK. */
1297 else
1299 perform_or_defer_access_check (TYPE_BINFO (t), fdecl, fdecl,
1300 tf_warning_or_error);
1301 DECL_ACCESS (fdecl) = tree_cons (t, access, DECL_ACCESS (fdecl));
1302 return 1;
1304 return 0;
1307 /* Process the USING_DECL, which is a member of T. */
1309 static void
1310 handle_using_decl (tree using_decl, tree t)
1312 tree decl = USING_DECL_DECLS (using_decl);
1313 tree name = DECL_NAME (using_decl);
1314 tree access
1315 = TREE_PRIVATE (using_decl) ? access_private_node
1316 : TREE_PROTECTED (using_decl) ? access_protected_node
1317 : access_public_node;
1318 tree flist = NULL_TREE;
1319 tree old_value;
1321 gcc_assert (!processing_template_decl && decl);
1323 old_value = lookup_member (t, name, /*protect=*/0, /*want_type=*/false,
1324 tf_warning_or_error);
1325 if (old_value)
1327 if (is_overloaded_fn (old_value))
1328 old_value = OVL_CURRENT (old_value);
1330 if (DECL_P (old_value) && DECL_CONTEXT (old_value) == t)
1331 /* OK */;
1332 else
1333 old_value = NULL_TREE;
1336 cp_emit_debug_info_for_using (decl, USING_DECL_SCOPE (using_decl));
1338 if (is_overloaded_fn (decl))
1339 flist = decl;
1341 if (! old_value)
1343 else if (is_overloaded_fn (old_value))
1345 if (flist)
1346 /* It's OK to use functions from a base when there are functions with
1347 the same name already present in the current class. */;
1348 else
1350 error ("%q+D invalid in %q#T", using_decl, t);
1351 error (" because of local method %q+#D with same name",
1352 OVL_CURRENT (old_value));
1353 return;
1356 else if (!DECL_ARTIFICIAL (old_value))
1358 error ("%q+D invalid in %q#T", using_decl, t);
1359 error (" because of local member %q+#D with same name", old_value);
1360 return;
1363 /* Make type T see field decl FDECL with access ACCESS. */
1364 if (flist)
1365 for (; flist; flist = OVL_NEXT (flist))
1367 add_method (t, OVL_CURRENT (flist), using_decl);
1368 alter_access (t, OVL_CURRENT (flist), access);
1370 else
1371 alter_access (t, decl, access);
1374 /* walk_tree callback for check_abi_tags: if the type at *TP involves any
1375 types with abi tags, add the corresponding identifiers to the VEC in
1376 *DATA and set IDENTIFIER_MARKED. */
1378 struct abi_tag_data
1380 tree t;
1381 tree subob;
1382 // error_mark_node to get diagnostics; otherwise collect missing tags here
1383 tree tags;
1386 static tree
1387 find_abi_tags_r (tree *tp, int *walk_subtrees, void *data)
1389 if (!OVERLOAD_TYPE_P (*tp))
1390 return NULL_TREE;
1392 /* walk_tree shouldn't be walking into any subtrees of a RECORD_TYPE
1393 anyway, but let's make sure of it. */
1394 *walk_subtrees = false;
1396 if (tree attributes = lookup_attribute ("abi_tag", TYPE_ATTRIBUTES (*tp)))
1398 struct abi_tag_data *p = static_cast<struct abi_tag_data*>(data);
1399 for (tree list = TREE_VALUE (attributes); list;
1400 list = TREE_CHAIN (list))
1402 tree tag = TREE_VALUE (list);
1403 tree id = get_identifier (TREE_STRING_POINTER (tag));
1404 if (!IDENTIFIER_MARKED (id))
1406 if (p->tags != error_mark_node)
1408 /* We're collecting tags from template arguments. */
1409 tree str = build_string (IDENTIFIER_LENGTH (id),
1410 IDENTIFIER_POINTER (id));
1411 p->tags = tree_cons (NULL_TREE, str, p->tags);
1412 ABI_TAG_IMPLICIT (p->tags) = true;
1414 /* Don't inherit this tag multiple times. */
1415 IDENTIFIER_MARKED (id) = true;
1418 /* Otherwise we're diagnosing missing tags. */
1419 else if (TYPE_P (p->subob))
1421 warning (OPT_Wabi_tag, "%qT does not have the %E abi tag "
1422 "that base %qT has", p->t, tag, p->subob);
1423 inform (location_of (p->subob), "%qT declared here",
1424 p->subob);
1426 else
1428 warning (OPT_Wabi_tag, "%qT does not have the %E abi tag "
1429 "that %qT (used in the type of %qD) has",
1430 p->t, tag, *tp, p->subob);
1431 inform (location_of (p->subob), "%qD declared here",
1432 p->subob);
1433 inform (location_of (*tp), "%qT declared here", *tp);
1438 return NULL_TREE;
1441 /* Set IDENTIFIER_MARKED on all the ABI tags on T and its (transitively
1442 complete) template arguments. */
1444 static void
1445 mark_type_abi_tags (tree t, bool val)
1447 tree attributes = lookup_attribute ("abi_tag", TYPE_ATTRIBUTES (t));
1448 if (attributes)
1450 for (tree list = TREE_VALUE (attributes); list;
1451 list = TREE_CHAIN (list))
1453 tree tag = TREE_VALUE (list);
1454 tree id = get_identifier (TREE_STRING_POINTER (tag));
1455 IDENTIFIER_MARKED (id) = val;
1460 /* Check that class T has all the abi tags that subobject SUBOB has, or
1461 warn if not. */
1463 static void
1464 check_abi_tags (tree t, tree subob)
1466 mark_type_abi_tags (t, true);
1468 tree subtype = TYPE_P (subob) ? subob : TREE_TYPE (subob);
1469 struct abi_tag_data data = { t, subob, error_mark_node };
1471 cp_walk_tree_without_duplicates (&subtype, find_abi_tags_r, &data);
1473 mark_type_abi_tags (t, false);
1476 void
1477 inherit_targ_abi_tags (tree t)
1479 if (CLASSTYPE_TEMPLATE_INFO (t) == NULL_TREE)
1480 return;
1482 mark_type_abi_tags (t, true);
1484 tree args = CLASSTYPE_TI_ARGS (t);
1485 struct abi_tag_data data = { t, NULL_TREE, NULL_TREE };
1486 for (int i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
1488 tree level = TMPL_ARGS_LEVEL (args, i+1);
1489 for (int j = 0; j < TREE_VEC_LENGTH (level); ++j)
1491 tree arg = TREE_VEC_ELT (level, j);
1492 data.subob = arg;
1493 cp_walk_tree_without_duplicates (&arg, find_abi_tags_r, &data);
1497 // If we found some tags on our template arguments, add them to our
1498 // abi_tag attribute.
1499 if (data.tags)
1501 tree attr = lookup_attribute ("abi_tag", TYPE_ATTRIBUTES (t));
1502 if (attr)
1503 TREE_VALUE (attr) = chainon (data.tags, TREE_VALUE (attr));
1504 else
1505 TYPE_ATTRIBUTES (t)
1506 = tree_cons (get_identifier ("abi_tag"), data.tags,
1507 TYPE_ATTRIBUTES (t));
1510 mark_type_abi_tags (t, false);
1513 /* Run through the base classes of T, updating CANT_HAVE_CONST_CTOR_P,
1514 and NO_CONST_ASN_REF_P. Also set flag bits in T based on
1515 properties of the bases. */
1517 static void
1518 check_bases (tree t,
1519 int* cant_have_const_ctor_p,
1520 int* no_const_asn_ref_p)
1522 int i;
1523 bool seen_non_virtual_nearly_empty_base_p = 0;
1524 int seen_tm_mask = 0;
1525 tree base_binfo;
1526 tree binfo;
1527 tree field = NULL_TREE;
1529 if (!CLASSTYPE_NON_STD_LAYOUT (t))
1530 for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
1531 if (TREE_CODE (field) == FIELD_DECL)
1532 break;
1534 for (binfo = TYPE_BINFO (t), i = 0;
1535 BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
1537 tree basetype = TREE_TYPE (base_binfo);
1539 gcc_assert (COMPLETE_TYPE_P (basetype));
1541 if (CLASSTYPE_FINAL (basetype))
1542 error ("cannot derive from %<final%> base %qT in derived type %qT",
1543 basetype, t);
1545 /* If any base class is non-literal, so is the derived class. */
1546 if (!CLASSTYPE_LITERAL_P (basetype))
1547 CLASSTYPE_LITERAL_P (t) = false;
1549 /* Effective C++ rule 14. We only need to check TYPE_POLYMORPHIC_P
1550 here because the case of virtual functions but non-virtual
1551 dtor is handled in finish_struct_1. */
1552 if (!TYPE_POLYMORPHIC_P (basetype))
1553 warning (OPT_Weffc__,
1554 "base class %q#T has a non-virtual destructor", basetype);
1556 /* If the base class doesn't have copy constructors or
1557 assignment operators that take const references, then the
1558 derived class cannot have such a member automatically
1559 generated. */
1560 if (TYPE_HAS_COPY_CTOR (basetype)
1561 && ! TYPE_HAS_CONST_COPY_CTOR (basetype))
1562 *cant_have_const_ctor_p = 1;
1563 if (TYPE_HAS_COPY_ASSIGN (basetype)
1564 && !TYPE_HAS_CONST_COPY_ASSIGN (basetype))
1565 *no_const_asn_ref_p = 1;
1567 if (BINFO_VIRTUAL_P (base_binfo))
1568 /* A virtual base does not effect nearly emptiness. */
1570 else if (CLASSTYPE_NEARLY_EMPTY_P (basetype))
1572 if (seen_non_virtual_nearly_empty_base_p)
1573 /* And if there is more than one nearly empty base, then the
1574 derived class is not nearly empty either. */
1575 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
1576 else
1577 /* Remember we've seen one. */
1578 seen_non_virtual_nearly_empty_base_p = 1;
1580 else if (!is_empty_class (basetype))
1581 /* If the base class is not empty or nearly empty, then this
1582 class cannot be nearly empty. */
1583 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
1585 /* A lot of properties from the bases also apply to the derived
1586 class. */
1587 TYPE_NEEDS_CONSTRUCTING (t) |= TYPE_NEEDS_CONSTRUCTING (basetype);
1588 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t)
1589 |= TYPE_HAS_NONTRIVIAL_DESTRUCTOR (basetype);
1590 TYPE_HAS_COMPLEX_COPY_ASSIGN (t)
1591 |= (TYPE_HAS_COMPLEX_COPY_ASSIGN (basetype)
1592 || !TYPE_HAS_COPY_ASSIGN (basetype));
1593 TYPE_HAS_COMPLEX_COPY_CTOR (t) |= (TYPE_HAS_COMPLEX_COPY_CTOR (basetype)
1594 || !TYPE_HAS_COPY_CTOR (basetype));
1595 TYPE_HAS_COMPLEX_MOVE_ASSIGN (t)
1596 |= TYPE_HAS_COMPLEX_MOVE_ASSIGN (basetype);
1597 TYPE_HAS_COMPLEX_MOVE_CTOR (t) |= TYPE_HAS_COMPLEX_MOVE_CTOR (basetype);
1598 TYPE_POLYMORPHIC_P (t) |= TYPE_POLYMORPHIC_P (basetype);
1599 CLASSTYPE_CONTAINS_EMPTY_CLASS_P (t)
1600 |= CLASSTYPE_CONTAINS_EMPTY_CLASS_P (basetype);
1601 TYPE_HAS_COMPLEX_DFLT (t) |= (!TYPE_HAS_DEFAULT_CONSTRUCTOR (basetype)
1602 || TYPE_HAS_COMPLEX_DFLT (basetype));
1603 SET_CLASSTYPE_READONLY_FIELDS_NEED_INIT
1604 (t, CLASSTYPE_READONLY_FIELDS_NEED_INIT (t)
1605 | CLASSTYPE_READONLY_FIELDS_NEED_INIT (basetype));
1606 SET_CLASSTYPE_REF_FIELDS_NEED_INIT
1607 (t, CLASSTYPE_REF_FIELDS_NEED_INIT (t)
1608 | CLASSTYPE_REF_FIELDS_NEED_INIT (basetype));
1610 /* A standard-layout class is a class that:
1612 * has no non-standard-layout base classes, */
1613 CLASSTYPE_NON_STD_LAYOUT (t) |= CLASSTYPE_NON_STD_LAYOUT (basetype);
1614 if (!CLASSTYPE_NON_STD_LAYOUT (t))
1616 tree basefield;
1617 /* ...has no base classes of the same type as the first non-static
1618 data member... */
1619 if (field && DECL_CONTEXT (field) == t
1620 && (same_type_ignoring_top_level_qualifiers_p
1621 (TREE_TYPE (field), basetype)))
1622 CLASSTYPE_NON_STD_LAYOUT (t) = 1;
1623 else
1624 /* ...either has no non-static data members in the most-derived
1625 class and at most one base class with non-static data
1626 members, or has no base classes with non-static data
1627 members */
1628 for (basefield = TYPE_FIELDS (basetype); basefield;
1629 basefield = DECL_CHAIN (basefield))
1630 if (TREE_CODE (basefield) == FIELD_DECL)
1632 if (field)
1633 CLASSTYPE_NON_STD_LAYOUT (t) = 1;
1634 else
1635 field = basefield;
1636 break;
1640 /* Don't bother collecting tm attributes if transactional memory
1641 support is not enabled. */
1642 if (flag_tm)
1644 tree tm_attr = find_tm_attribute (TYPE_ATTRIBUTES (basetype));
1645 if (tm_attr)
1646 seen_tm_mask |= tm_attr_to_mask (tm_attr);
1649 check_abi_tags (t, basetype);
1652 /* If one of the base classes had TM attributes, and the current class
1653 doesn't define its own, then the current class inherits one. */
1654 if (seen_tm_mask && !find_tm_attribute (TYPE_ATTRIBUTES (t)))
1656 tree tm_attr = tm_mask_to_attr (seen_tm_mask & -seen_tm_mask);
1657 TYPE_ATTRIBUTES (t) = tree_cons (tm_attr, NULL, TYPE_ATTRIBUTES (t));
1661 /* Determine all the primary bases within T. Sets BINFO_PRIMARY_BASE_P for
1662 those that are primaries. Sets BINFO_LOST_PRIMARY_P for those
1663 that have had a nearly-empty virtual primary base stolen by some
1664 other base in the hierarchy. Determines CLASSTYPE_PRIMARY_BASE for
1665 T. */
1667 static void
1668 determine_primary_bases (tree t)
1670 unsigned i;
1671 tree primary = NULL_TREE;
1672 tree type_binfo = TYPE_BINFO (t);
1673 tree base_binfo;
1675 /* Determine the primary bases of our bases. */
1676 for (base_binfo = TREE_CHAIN (type_binfo); base_binfo;
1677 base_binfo = TREE_CHAIN (base_binfo))
1679 tree primary = CLASSTYPE_PRIMARY_BINFO (BINFO_TYPE (base_binfo));
1681 /* See if we're the non-virtual primary of our inheritance
1682 chain. */
1683 if (!BINFO_VIRTUAL_P (base_binfo))
1685 tree parent = BINFO_INHERITANCE_CHAIN (base_binfo);
1686 tree parent_primary = CLASSTYPE_PRIMARY_BINFO (BINFO_TYPE (parent));
1688 if (parent_primary
1689 && SAME_BINFO_TYPE_P (BINFO_TYPE (base_binfo),
1690 BINFO_TYPE (parent_primary)))
1691 /* We are the primary binfo. */
1692 BINFO_PRIMARY_P (base_binfo) = 1;
1694 /* Determine if we have a virtual primary base, and mark it so.
1696 if (primary && BINFO_VIRTUAL_P (primary))
1698 tree this_primary = copied_binfo (primary, base_binfo);
1700 if (BINFO_PRIMARY_P (this_primary))
1701 /* Someone already claimed this base. */
1702 BINFO_LOST_PRIMARY_P (base_binfo) = 1;
1703 else
1705 tree delta;
1707 BINFO_PRIMARY_P (this_primary) = 1;
1708 BINFO_INHERITANCE_CHAIN (this_primary) = base_binfo;
1710 /* A virtual binfo might have been copied from within
1711 another hierarchy. As we're about to use it as a
1712 primary base, make sure the offsets match. */
1713 delta = size_diffop_loc (input_location,
1714 convert (ssizetype,
1715 BINFO_OFFSET (base_binfo)),
1716 convert (ssizetype,
1717 BINFO_OFFSET (this_primary)));
1719 propagate_binfo_offsets (this_primary, delta);
1724 /* First look for a dynamic direct non-virtual base. */
1725 for (i = 0; BINFO_BASE_ITERATE (type_binfo, i, base_binfo); i++)
1727 tree basetype = BINFO_TYPE (base_binfo);
1729 if (TYPE_CONTAINS_VPTR_P (basetype) && !BINFO_VIRTUAL_P (base_binfo))
1731 primary = base_binfo;
1732 goto found;
1736 /* A "nearly-empty" virtual base class can be the primary base
1737 class, if no non-virtual polymorphic base can be found. Look for
1738 a nearly-empty virtual dynamic base that is not already a primary
1739 base of something in the hierarchy. If there is no such base,
1740 just pick the first nearly-empty virtual base. */
1742 for (base_binfo = TREE_CHAIN (type_binfo); base_binfo;
1743 base_binfo = TREE_CHAIN (base_binfo))
1744 if (BINFO_VIRTUAL_P (base_binfo)
1745 && CLASSTYPE_NEARLY_EMPTY_P (BINFO_TYPE (base_binfo)))
1747 if (!BINFO_PRIMARY_P (base_binfo))
1749 /* Found one that is not primary. */
1750 primary = base_binfo;
1751 goto found;
1753 else if (!primary)
1754 /* Remember the first candidate. */
1755 primary = base_binfo;
1758 found:
1759 /* If we've got a primary base, use it. */
1760 if (primary)
1762 tree basetype = BINFO_TYPE (primary);
1764 CLASSTYPE_PRIMARY_BINFO (t) = primary;
1765 if (BINFO_PRIMARY_P (primary))
1766 /* We are stealing a primary base. */
1767 BINFO_LOST_PRIMARY_P (BINFO_INHERITANCE_CHAIN (primary)) = 1;
1768 BINFO_PRIMARY_P (primary) = 1;
1769 if (BINFO_VIRTUAL_P (primary))
1771 tree delta;
1773 BINFO_INHERITANCE_CHAIN (primary) = type_binfo;
1774 /* A virtual binfo might have been copied from within
1775 another hierarchy. As we're about to use it as a primary
1776 base, make sure the offsets match. */
1777 delta = size_diffop_loc (input_location, ssize_int (0),
1778 convert (ssizetype, BINFO_OFFSET (primary)));
1780 propagate_binfo_offsets (primary, delta);
1783 primary = TYPE_BINFO (basetype);
1785 TYPE_VFIELD (t) = TYPE_VFIELD (basetype);
1786 BINFO_VTABLE (type_binfo) = BINFO_VTABLE (primary);
1787 BINFO_VIRTUALS (type_binfo) = BINFO_VIRTUALS (primary);
1791 /* Update the variant types of T. */
1793 void
1794 fixup_type_variants (tree t)
1796 tree variants;
1798 if (!t)
1799 return;
1801 for (variants = TYPE_NEXT_VARIANT (t);
1802 variants;
1803 variants = TYPE_NEXT_VARIANT (variants))
1805 /* These fields are in the _TYPE part of the node, not in
1806 the TYPE_LANG_SPECIFIC component, so they are not shared. */
1807 TYPE_HAS_USER_CONSTRUCTOR (variants) = TYPE_HAS_USER_CONSTRUCTOR (t);
1808 TYPE_NEEDS_CONSTRUCTING (variants) = TYPE_NEEDS_CONSTRUCTING (t);
1809 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (variants)
1810 = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t);
1812 TYPE_POLYMORPHIC_P (variants) = TYPE_POLYMORPHIC_P (t);
1814 TYPE_BINFO (variants) = TYPE_BINFO (t);
1816 /* Copy whatever these are holding today. */
1817 TYPE_VFIELD (variants) = TYPE_VFIELD (t);
1818 TYPE_METHODS (variants) = TYPE_METHODS (t);
1819 TYPE_FIELDS (variants) = TYPE_FIELDS (t);
1823 /* Early variant fixups: we apply attributes at the beginning of the class
1824 definition, and we need to fix up any variants that have already been
1825 made via elaborated-type-specifier so that check_qualified_type works. */
1827 void
1828 fixup_attribute_variants (tree t)
1830 tree variants;
1832 if (!t)
1833 return;
1835 for (variants = TYPE_NEXT_VARIANT (t);
1836 variants;
1837 variants = TYPE_NEXT_VARIANT (variants))
1839 /* These are the two fields that check_qualified_type looks at and
1840 are affected by attributes. */
1841 TYPE_ATTRIBUTES (variants) = TYPE_ATTRIBUTES (t);
1842 TYPE_ALIGN (variants) = TYPE_ALIGN (t);
1846 /* Set memoizing fields and bits of T (and its variants) for later
1847 use. */
1849 static void
1850 finish_struct_bits (tree t)
1852 /* Fix up variants (if any). */
1853 fixup_type_variants (t);
1855 if (BINFO_N_BASE_BINFOS (TYPE_BINFO (t)) && TYPE_POLYMORPHIC_P (t))
1856 /* For a class w/o baseclasses, 'finish_struct' has set
1857 CLASSTYPE_PURE_VIRTUALS correctly (by definition).
1858 Similarly for a class whose base classes do not have vtables.
1859 When neither of these is true, we might have removed abstract
1860 virtuals (by providing a definition), added some (by declaring
1861 new ones), or redeclared ones from a base class. We need to
1862 recalculate what's really an abstract virtual at this point (by
1863 looking in the vtables). */
1864 get_pure_virtuals (t);
1866 /* If this type has a copy constructor or a destructor, force its
1867 mode to be BLKmode, and force its TREE_ADDRESSABLE bit to be
1868 nonzero. This will cause it to be passed by invisible reference
1869 and prevent it from being returned in a register. */
1870 if (type_has_nontrivial_copy_init (t)
1871 || TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t))
1873 tree variants;
1874 DECL_MODE (TYPE_MAIN_DECL (t)) = BLKmode;
1875 for (variants = t; variants; variants = TYPE_NEXT_VARIANT (variants))
1877 SET_TYPE_MODE (variants, BLKmode);
1878 TREE_ADDRESSABLE (variants) = 1;
1883 /* Issue warnings about T having private constructors, but no friends,
1884 and so forth.
1886 HAS_NONPRIVATE_METHOD is nonzero if T has any non-private methods or
1887 static members. HAS_NONPRIVATE_STATIC_FN is nonzero if T has any
1888 non-private static member functions. */
1890 static void
1891 maybe_warn_about_overly_private_class (tree t)
1893 int has_member_fn = 0;
1894 int has_nonprivate_method = 0;
1895 tree fn;
1897 if (!warn_ctor_dtor_privacy
1898 /* If the class has friends, those entities might create and
1899 access instances, so we should not warn. */
1900 || (CLASSTYPE_FRIEND_CLASSES (t)
1901 || DECL_FRIENDLIST (TYPE_MAIN_DECL (t)))
1902 /* We will have warned when the template was declared; there's
1903 no need to warn on every instantiation. */
1904 || CLASSTYPE_TEMPLATE_INSTANTIATION (t))
1905 /* There's no reason to even consider warning about this
1906 class. */
1907 return;
1909 /* We only issue one warning, if more than one applies, because
1910 otherwise, on code like:
1912 class A {
1913 // Oops - forgot `public:'
1914 A();
1915 A(const A&);
1916 ~A();
1919 we warn several times about essentially the same problem. */
1921 /* Check to see if all (non-constructor, non-destructor) member
1922 functions are private. (Since there are no friends or
1923 non-private statics, we can't ever call any of the private member
1924 functions.) */
1925 for (fn = TYPE_METHODS (t); fn; fn = DECL_CHAIN (fn))
1926 /* We're not interested in compiler-generated methods; they don't
1927 provide any way to call private members. */
1928 if (!DECL_ARTIFICIAL (fn))
1930 if (!TREE_PRIVATE (fn))
1932 if (DECL_STATIC_FUNCTION_P (fn))
1933 /* A non-private static member function is just like a
1934 friend; it can create and invoke private member
1935 functions, and be accessed without a class
1936 instance. */
1937 return;
1939 has_nonprivate_method = 1;
1940 /* Keep searching for a static member function. */
1942 else if (!DECL_CONSTRUCTOR_P (fn) && !DECL_DESTRUCTOR_P (fn))
1943 has_member_fn = 1;
1946 if (!has_nonprivate_method && has_member_fn)
1948 /* There are no non-private methods, and there's at least one
1949 private member function that isn't a constructor or
1950 destructor. (If all the private members are
1951 constructors/destructors we want to use the code below that
1952 issues error messages specifically referring to
1953 constructors/destructors.) */
1954 unsigned i;
1955 tree binfo = TYPE_BINFO (t);
1957 for (i = 0; i != BINFO_N_BASE_BINFOS (binfo); i++)
1958 if (BINFO_BASE_ACCESS (binfo, i) != access_private_node)
1960 has_nonprivate_method = 1;
1961 break;
1963 if (!has_nonprivate_method)
1965 warning (OPT_Wctor_dtor_privacy,
1966 "all member functions in class %qT are private", t);
1967 return;
1971 /* Even if some of the member functions are non-private, the class
1972 won't be useful for much if all the constructors or destructors
1973 are private: such an object can never be created or destroyed. */
1974 fn = CLASSTYPE_DESTRUCTORS (t);
1975 if (fn && TREE_PRIVATE (fn))
1977 warning (OPT_Wctor_dtor_privacy,
1978 "%q#T only defines a private destructor and has no friends",
1980 return;
1983 /* Warn about classes that have private constructors and no friends. */
1984 if (TYPE_HAS_USER_CONSTRUCTOR (t)
1985 /* Implicitly generated constructors are always public. */
1986 && (!CLASSTYPE_LAZY_DEFAULT_CTOR (t)
1987 || !CLASSTYPE_LAZY_COPY_CTOR (t)))
1989 int nonprivate_ctor = 0;
1991 /* If a non-template class does not define a copy
1992 constructor, one is defined for it, enabling it to avoid
1993 this warning. For a template class, this does not
1994 happen, and so we would normally get a warning on:
1996 template <class T> class C { private: C(); };
1998 To avoid this asymmetry, we check TYPE_HAS_COPY_CTOR. All
1999 complete non-template or fully instantiated classes have this
2000 flag set. */
2001 if (!TYPE_HAS_COPY_CTOR (t))
2002 nonprivate_ctor = 1;
2003 else
2004 for (fn = CLASSTYPE_CONSTRUCTORS (t); fn; fn = OVL_NEXT (fn))
2006 tree ctor = OVL_CURRENT (fn);
2007 /* Ideally, we wouldn't count copy constructors (or, in
2008 fact, any constructor that takes an argument of the
2009 class type as a parameter) because such things cannot
2010 be used to construct an instance of the class unless
2011 you already have one. But, for now at least, we're
2012 more generous. */
2013 if (! TREE_PRIVATE (ctor))
2015 nonprivate_ctor = 1;
2016 break;
2020 if (nonprivate_ctor == 0)
2022 warning (OPT_Wctor_dtor_privacy,
2023 "%q#T only defines private constructors and has no friends",
2025 return;
2030 static struct {
2031 gt_pointer_operator new_value;
2032 void *cookie;
2033 } resort_data;
2035 /* Comparison function to compare two TYPE_METHOD_VEC entries by name. */
2037 static int
2038 method_name_cmp (const void* m1_p, const void* m2_p)
2040 const tree *const m1 = (const tree *) m1_p;
2041 const tree *const m2 = (const tree *) m2_p;
2043 if (*m1 == NULL_TREE && *m2 == NULL_TREE)
2044 return 0;
2045 if (*m1 == NULL_TREE)
2046 return -1;
2047 if (*m2 == NULL_TREE)
2048 return 1;
2049 if (DECL_NAME (OVL_CURRENT (*m1)) < DECL_NAME (OVL_CURRENT (*m2)))
2050 return -1;
2051 return 1;
2054 /* This routine compares two fields like method_name_cmp but using the
2055 pointer operator in resort_field_decl_data. */
2057 static int
2058 resort_method_name_cmp (const void* m1_p, const void* m2_p)
2060 const tree *const m1 = (const tree *) m1_p;
2061 const tree *const m2 = (const tree *) m2_p;
2062 if (*m1 == NULL_TREE && *m2 == NULL_TREE)
2063 return 0;
2064 if (*m1 == NULL_TREE)
2065 return -1;
2066 if (*m2 == NULL_TREE)
2067 return 1;
2069 tree d1 = DECL_NAME (OVL_CURRENT (*m1));
2070 tree d2 = DECL_NAME (OVL_CURRENT (*m2));
2071 resort_data.new_value (&d1, resort_data.cookie);
2072 resort_data.new_value (&d2, resort_data.cookie);
2073 if (d1 < d2)
2074 return -1;
2076 return 1;
2079 /* Resort TYPE_METHOD_VEC because pointers have been reordered. */
2081 void
2082 resort_type_method_vec (void* obj,
2083 void* /*orig_obj*/,
2084 gt_pointer_operator new_value,
2085 void* cookie)
2087 vec<tree, va_gc> *method_vec = (vec<tree, va_gc> *) obj;
2088 int len = vec_safe_length (method_vec);
2089 size_t slot;
2090 tree fn;
2092 /* The type conversion ops have to live at the front of the vec, so we
2093 can't sort them. */
2094 for (slot = CLASSTYPE_FIRST_CONVERSION_SLOT;
2095 vec_safe_iterate (method_vec, slot, &fn);
2096 ++slot)
2097 if (!DECL_CONV_FN_P (OVL_CURRENT (fn)))
2098 break;
2100 if (len - slot > 1)
2102 resort_data.new_value = new_value;
2103 resort_data.cookie = cookie;
2104 qsort (method_vec->address () + slot, len - slot, sizeof (tree),
2105 resort_method_name_cmp);
2109 /* Warn about duplicate methods in fn_fields.
2111 Sort methods that are not special (i.e., constructors, destructors,
2112 and type conversion operators) so that we can find them faster in
2113 search. */
2115 static void
2116 finish_struct_methods (tree t)
2118 tree fn_fields;
2119 vec<tree, va_gc> *method_vec;
2120 int slot, len;
2122 method_vec = CLASSTYPE_METHOD_VEC (t);
2123 if (!method_vec)
2124 return;
2126 len = method_vec->length ();
2128 /* Clear DECL_IN_AGGR_P for all functions. */
2129 for (fn_fields = TYPE_METHODS (t); fn_fields;
2130 fn_fields = DECL_CHAIN (fn_fields))
2131 DECL_IN_AGGR_P (fn_fields) = 0;
2133 /* Issue warnings about private constructors and such. If there are
2134 no methods, then some public defaults are generated. */
2135 maybe_warn_about_overly_private_class (t);
2137 /* The type conversion ops have to live at the front of the vec, so we
2138 can't sort them. */
2139 for (slot = CLASSTYPE_FIRST_CONVERSION_SLOT;
2140 method_vec->iterate (slot, &fn_fields);
2141 ++slot)
2142 if (!DECL_CONV_FN_P (OVL_CURRENT (fn_fields)))
2143 break;
2144 if (len - slot > 1)
2145 qsort (method_vec->address () + slot,
2146 len-slot, sizeof (tree), method_name_cmp);
2149 /* Make BINFO's vtable have N entries, including RTTI entries,
2150 vbase and vcall offsets, etc. Set its type and call the back end
2151 to lay it out. */
2153 static void
2154 layout_vtable_decl (tree binfo, int n)
2156 tree atype;
2157 tree vtable;
2159 atype = build_array_of_n_type (vtable_entry_type, n);
2160 layout_type (atype);
2162 /* We may have to grow the vtable. */
2163 vtable = get_vtbl_decl_for_binfo (binfo);
2164 if (!same_type_p (TREE_TYPE (vtable), atype))
2166 TREE_TYPE (vtable) = atype;
2167 DECL_SIZE (vtable) = DECL_SIZE_UNIT (vtable) = NULL_TREE;
2168 layout_decl (vtable, 0);
2172 /* True iff FNDECL and BASE_FNDECL (both non-static member functions)
2173 have the same signature. */
2176 same_signature_p (const_tree fndecl, const_tree base_fndecl)
2178 /* One destructor overrides another if they are the same kind of
2179 destructor. */
2180 if (DECL_DESTRUCTOR_P (base_fndecl) && DECL_DESTRUCTOR_P (fndecl)
2181 && special_function_p (base_fndecl) == special_function_p (fndecl))
2182 return 1;
2183 /* But a non-destructor never overrides a destructor, nor vice
2184 versa, nor do different kinds of destructors override
2185 one-another. For example, a complete object destructor does not
2186 override a deleting destructor. */
2187 if (DECL_DESTRUCTOR_P (base_fndecl) || DECL_DESTRUCTOR_P (fndecl))
2188 return 0;
2190 if (DECL_NAME (fndecl) == DECL_NAME (base_fndecl)
2191 || (DECL_CONV_FN_P (fndecl)
2192 && DECL_CONV_FN_P (base_fndecl)
2193 && same_type_p (DECL_CONV_FN_TYPE (fndecl),
2194 DECL_CONV_FN_TYPE (base_fndecl))))
2196 tree fntype = TREE_TYPE (fndecl);
2197 tree base_fntype = TREE_TYPE (base_fndecl);
2198 if (type_memfn_quals (fntype) == type_memfn_quals (base_fntype)
2199 && type_memfn_rqual (fntype) == type_memfn_rqual (base_fntype)
2200 && compparms (FUNCTION_FIRST_USER_PARMTYPE (fndecl),
2201 FUNCTION_FIRST_USER_PARMTYPE (base_fndecl)))
2202 return 1;
2204 return 0;
2207 /* Returns TRUE if DERIVED is a binfo containing the binfo BASE as a
2208 subobject. */
2210 static bool
2211 base_derived_from (tree derived, tree base)
2213 tree probe;
2215 for (probe = base; probe; probe = BINFO_INHERITANCE_CHAIN (probe))
2217 if (probe == derived)
2218 return true;
2219 else if (BINFO_VIRTUAL_P (probe))
2220 /* If we meet a virtual base, we can't follow the inheritance
2221 any more. See if the complete type of DERIVED contains
2222 such a virtual base. */
2223 return (binfo_for_vbase (BINFO_TYPE (probe), BINFO_TYPE (derived))
2224 != NULL_TREE);
2226 return false;
2229 typedef struct find_final_overrider_data_s {
2230 /* The function for which we are trying to find a final overrider. */
2231 tree fn;
2232 /* The base class in which the function was declared. */
2233 tree declaring_base;
2234 /* The candidate overriders. */
2235 tree candidates;
2236 /* Path to most derived. */
2237 vec<tree> path;
2238 } find_final_overrider_data;
2240 /* Add the overrider along the current path to FFOD->CANDIDATES.
2241 Returns true if an overrider was found; false otherwise. */
2243 static bool
2244 dfs_find_final_overrider_1 (tree binfo,
2245 find_final_overrider_data *ffod,
2246 unsigned depth)
2248 tree method;
2250 /* If BINFO is not the most derived type, try a more derived class.
2251 A definition there will overrider a definition here. */
2252 if (depth)
2254 depth--;
2255 if (dfs_find_final_overrider_1
2256 (ffod->path[depth], ffod, depth))
2257 return true;
2260 method = look_for_overrides_here (BINFO_TYPE (binfo), ffod->fn);
2261 if (method)
2263 tree *candidate = &ffod->candidates;
2265 /* Remove any candidates overridden by this new function. */
2266 while (*candidate)
2268 /* If *CANDIDATE overrides METHOD, then METHOD
2269 cannot override anything else on the list. */
2270 if (base_derived_from (TREE_VALUE (*candidate), binfo))
2271 return true;
2272 /* If METHOD overrides *CANDIDATE, remove *CANDIDATE. */
2273 if (base_derived_from (binfo, TREE_VALUE (*candidate)))
2274 *candidate = TREE_CHAIN (*candidate);
2275 else
2276 candidate = &TREE_CHAIN (*candidate);
2279 /* Add the new function. */
2280 ffod->candidates = tree_cons (method, binfo, ffod->candidates);
2281 return true;
2284 return false;
2287 /* Called from find_final_overrider via dfs_walk. */
2289 static tree
2290 dfs_find_final_overrider_pre (tree binfo, void *data)
2292 find_final_overrider_data *ffod = (find_final_overrider_data *) data;
2294 if (binfo == ffod->declaring_base)
2295 dfs_find_final_overrider_1 (binfo, ffod, ffod->path.length ());
2296 ffod->path.safe_push (binfo);
2298 return NULL_TREE;
2301 static tree
2302 dfs_find_final_overrider_post (tree /*binfo*/, void *data)
2304 find_final_overrider_data *ffod = (find_final_overrider_data *) data;
2305 ffod->path.pop ();
2307 return NULL_TREE;
2310 /* Returns a TREE_LIST whose TREE_PURPOSE is the final overrider for
2311 FN and whose TREE_VALUE is the binfo for the base where the
2312 overriding occurs. BINFO (in the hierarchy dominated by the binfo
2313 DERIVED) is the base object in which FN is declared. */
2315 static tree
2316 find_final_overrider (tree derived, tree binfo, tree fn)
2318 find_final_overrider_data ffod;
2320 /* Getting this right is a little tricky. This is valid:
2322 struct S { virtual void f (); };
2323 struct T { virtual void f (); };
2324 struct U : public S, public T { };
2326 even though calling `f' in `U' is ambiguous. But,
2328 struct R { virtual void f(); };
2329 struct S : virtual public R { virtual void f (); };
2330 struct T : virtual public R { virtual void f (); };
2331 struct U : public S, public T { };
2333 is not -- there's no way to decide whether to put `S::f' or
2334 `T::f' in the vtable for `R'.
2336 The solution is to look at all paths to BINFO. If we find
2337 different overriders along any two, then there is a problem. */
2338 if (DECL_THUNK_P (fn))
2339 fn = THUNK_TARGET (fn);
2341 /* Determine the depth of the hierarchy. */
2342 ffod.fn = fn;
2343 ffod.declaring_base = binfo;
2344 ffod.candidates = NULL_TREE;
2345 ffod.path.create (30);
2347 dfs_walk_all (derived, dfs_find_final_overrider_pre,
2348 dfs_find_final_overrider_post, &ffod);
2350 ffod.path.release ();
2352 /* If there was no winner, issue an error message. */
2353 if (!ffod.candidates || TREE_CHAIN (ffod.candidates))
2354 return error_mark_node;
2356 return ffod.candidates;
2359 /* Return the index of the vcall offset for FN when TYPE is used as a
2360 virtual base. */
2362 static tree
2363 get_vcall_index (tree fn, tree type)
2365 vec<tree_pair_s, va_gc> *indices = CLASSTYPE_VCALL_INDICES (type);
2366 tree_pair_p p;
2367 unsigned ix;
2369 FOR_EACH_VEC_SAFE_ELT (indices, ix, p)
2370 if ((DECL_DESTRUCTOR_P (fn) && DECL_DESTRUCTOR_P (p->purpose))
2371 || same_signature_p (fn, p->purpose))
2372 return p->value;
2374 /* There should always be an appropriate index. */
2375 gcc_unreachable ();
2378 /* Update an entry in the vtable for BINFO, which is in the hierarchy
2379 dominated by T. FN is the old function; VIRTUALS points to the
2380 corresponding position in the new BINFO_VIRTUALS list. IX is the index
2381 of that entry in the list. */
2383 static void
2384 update_vtable_entry_for_fn (tree t, tree binfo, tree fn, tree* virtuals,
2385 unsigned ix)
2387 tree b;
2388 tree overrider;
2389 tree delta;
2390 tree virtual_base;
2391 tree first_defn;
2392 tree overrider_fn, overrider_target;
2393 tree target_fn = DECL_THUNK_P (fn) ? THUNK_TARGET (fn) : fn;
2394 tree over_return, base_return;
2395 bool lost = false;
2397 /* Find the nearest primary base (possibly binfo itself) which defines
2398 this function; this is the class the caller will convert to when
2399 calling FN through BINFO. */
2400 for (b = binfo; ; b = get_primary_binfo (b))
2402 gcc_assert (b);
2403 if (look_for_overrides_here (BINFO_TYPE (b), target_fn))
2404 break;
2406 /* The nearest definition is from a lost primary. */
2407 if (BINFO_LOST_PRIMARY_P (b))
2408 lost = true;
2410 first_defn = b;
2412 /* Find the final overrider. */
2413 overrider = find_final_overrider (TYPE_BINFO (t), b, target_fn);
2414 if (overrider == error_mark_node)
2416 error ("no unique final overrider for %qD in %qT", target_fn, t);
2417 return;
2419 overrider_target = overrider_fn = TREE_PURPOSE (overrider);
2421 /* Check for adjusting covariant return types. */
2422 over_return = TREE_TYPE (TREE_TYPE (overrider_target));
2423 base_return = TREE_TYPE (TREE_TYPE (target_fn));
2425 if (POINTER_TYPE_P (over_return)
2426 && TREE_CODE (over_return) == TREE_CODE (base_return)
2427 && CLASS_TYPE_P (TREE_TYPE (over_return))
2428 && CLASS_TYPE_P (TREE_TYPE (base_return))
2429 /* If the overrider is invalid, don't even try. */
2430 && !DECL_INVALID_OVERRIDER_P (overrider_target))
2432 /* If FN is a covariant thunk, we must figure out the adjustment
2433 to the final base FN was converting to. As OVERRIDER_TARGET might
2434 also be converting to the return type of FN, we have to
2435 combine the two conversions here. */
2436 tree fixed_offset, virtual_offset;
2438 over_return = TREE_TYPE (over_return);
2439 base_return = TREE_TYPE (base_return);
2441 if (DECL_THUNK_P (fn))
2443 gcc_assert (DECL_RESULT_THUNK_P (fn));
2444 fixed_offset = ssize_int (THUNK_FIXED_OFFSET (fn));
2445 virtual_offset = THUNK_VIRTUAL_OFFSET (fn);
2447 else
2448 fixed_offset = virtual_offset = NULL_TREE;
2450 if (virtual_offset)
2451 /* Find the equivalent binfo within the return type of the
2452 overriding function. We will want the vbase offset from
2453 there. */
2454 virtual_offset = binfo_for_vbase (BINFO_TYPE (virtual_offset),
2455 over_return);
2456 else if (!same_type_ignoring_top_level_qualifiers_p
2457 (over_return, base_return))
2459 /* There was no existing virtual thunk (which takes
2460 precedence). So find the binfo of the base function's
2461 return type within the overriding function's return type.
2462 We cannot call lookup base here, because we're inside a
2463 dfs_walk, and will therefore clobber the BINFO_MARKED
2464 flags. Fortunately we know the covariancy is valid (it
2465 has already been checked), so we can just iterate along
2466 the binfos, which have been chained in inheritance graph
2467 order. Of course it is lame that we have to repeat the
2468 search here anyway -- we should really be caching pieces
2469 of the vtable and avoiding this repeated work. */
2470 tree thunk_binfo, base_binfo;
2472 /* Find the base binfo within the overriding function's
2473 return type. We will always find a thunk_binfo, except
2474 when the covariancy is invalid (which we will have
2475 already diagnosed). */
2476 for (base_binfo = TYPE_BINFO (base_return),
2477 thunk_binfo = TYPE_BINFO (over_return);
2478 thunk_binfo;
2479 thunk_binfo = TREE_CHAIN (thunk_binfo))
2480 if (SAME_BINFO_TYPE_P (BINFO_TYPE (thunk_binfo),
2481 BINFO_TYPE (base_binfo)))
2482 break;
2484 /* See if virtual inheritance is involved. */
2485 for (virtual_offset = thunk_binfo;
2486 virtual_offset;
2487 virtual_offset = BINFO_INHERITANCE_CHAIN (virtual_offset))
2488 if (BINFO_VIRTUAL_P (virtual_offset))
2489 break;
2491 if (virtual_offset
2492 || (thunk_binfo && !BINFO_OFFSET_ZEROP (thunk_binfo)))
2494 tree offset = convert (ssizetype, BINFO_OFFSET (thunk_binfo));
2496 if (virtual_offset)
2498 /* We convert via virtual base. Adjust the fixed
2499 offset to be from there. */
2500 offset =
2501 size_diffop (offset,
2502 convert (ssizetype,
2503 BINFO_OFFSET (virtual_offset)));
2505 if (fixed_offset)
2506 /* There was an existing fixed offset, this must be
2507 from the base just converted to, and the base the
2508 FN was thunking to. */
2509 fixed_offset = size_binop (PLUS_EXPR, fixed_offset, offset);
2510 else
2511 fixed_offset = offset;
2515 if (fixed_offset || virtual_offset)
2516 /* Replace the overriding function with a covariant thunk. We
2517 will emit the overriding function in its own slot as
2518 well. */
2519 overrider_fn = make_thunk (overrider_target, /*this_adjusting=*/0,
2520 fixed_offset, virtual_offset);
2522 else
2523 gcc_assert (DECL_INVALID_OVERRIDER_P (overrider_target) ||
2524 !DECL_THUNK_P (fn));
2526 /* If we need a covariant thunk, then we may need to adjust first_defn.
2527 The ABI specifies that the thunks emitted with a function are
2528 determined by which bases the function overrides, so we need to be
2529 sure that we're using a thunk for some overridden base; even if we
2530 know that the necessary this adjustment is zero, there may not be an
2531 appropriate zero-this-adjusment thunk for us to use since thunks for
2532 overriding virtual bases always use the vcall offset.
2534 Furthermore, just choosing any base that overrides this function isn't
2535 quite right, as this slot won't be used for calls through a type that
2536 puts a covariant thunk here. Calling the function through such a type
2537 will use a different slot, and that slot is the one that determines
2538 the thunk emitted for that base.
2540 So, keep looking until we find the base that we're really overriding
2541 in this slot: the nearest primary base that doesn't use a covariant
2542 thunk in this slot. */
2543 if (overrider_target != overrider_fn)
2545 if (BINFO_TYPE (b) == DECL_CONTEXT (overrider_target))
2546 /* We already know that the overrider needs a covariant thunk. */
2547 b = get_primary_binfo (b);
2548 for (; ; b = get_primary_binfo (b))
2550 tree main_binfo = TYPE_BINFO (BINFO_TYPE (b));
2551 tree bv = chain_index (ix, BINFO_VIRTUALS (main_binfo));
2552 if (!DECL_THUNK_P (TREE_VALUE (bv)))
2553 break;
2554 if (BINFO_LOST_PRIMARY_P (b))
2555 lost = true;
2557 first_defn = b;
2560 /* Assume that we will produce a thunk that convert all the way to
2561 the final overrider, and not to an intermediate virtual base. */
2562 virtual_base = NULL_TREE;
2564 /* See if we can convert to an intermediate virtual base first, and then
2565 use the vcall offset located there to finish the conversion. */
2566 for (; b; b = BINFO_INHERITANCE_CHAIN (b))
2568 /* If we find the final overrider, then we can stop
2569 walking. */
2570 if (SAME_BINFO_TYPE_P (BINFO_TYPE (b),
2571 BINFO_TYPE (TREE_VALUE (overrider))))
2572 break;
2574 /* If we find a virtual base, and we haven't yet found the
2575 overrider, then there is a virtual base between the
2576 declaring base (first_defn) and the final overrider. */
2577 if (BINFO_VIRTUAL_P (b))
2579 virtual_base = b;
2580 break;
2584 /* Compute the constant adjustment to the `this' pointer. The
2585 `this' pointer, when this function is called, will point at BINFO
2586 (or one of its primary bases, which are at the same offset). */
2587 if (virtual_base)
2588 /* The `this' pointer needs to be adjusted from the declaration to
2589 the nearest virtual base. */
2590 delta = size_diffop_loc (input_location,
2591 convert (ssizetype, BINFO_OFFSET (virtual_base)),
2592 convert (ssizetype, BINFO_OFFSET (first_defn)));
2593 else if (lost)
2594 /* If the nearest definition is in a lost primary, we don't need an
2595 entry in our vtable. Except possibly in a constructor vtable,
2596 if we happen to get our primary back. In that case, the offset
2597 will be zero, as it will be a primary base. */
2598 delta = size_zero_node;
2599 else
2600 /* The `this' pointer needs to be adjusted from pointing to
2601 BINFO to pointing at the base where the final overrider
2602 appears. */
2603 delta = size_diffop_loc (input_location,
2604 convert (ssizetype,
2605 BINFO_OFFSET (TREE_VALUE (overrider))),
2606 convert (ssizetype, BINFO_OFFSET (binfo)));
2608 modify_vtable_entry (t, binfo, overrider_fn, delta, virtuals);
2610 if (virtual_base)
2611 BV_VCALL_INDEX (*virtuals)
2612 = get_vcall_index (overrider_target, BINFO_TYPE (virtual_base));
2613 else
2614 BV_VCALL_INDEX (*virtuals) = NULL_TREE;
2616 BV_LOST_PRIMARY (*virtuals) = lost;
2619 /* Called from modify_all_vtables via dfs_walk. */
2621 static tree
2622 dfs_modify_vtables (tree binfo, void* data)
2624 tree t = (tree) data;
2625 tree virtuals;
2626 tree old_virtuals;
2627 unsigned ix;
2629 if (!TYPE_CONTAINS_VPTR_P (BINFO_TYPE (binfo)))
2630 /* A base without a vtable needs no modification, and its bases
2631 are uninteresting. */
2632 return dfs_skip_bases;
2634 if (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), t)
2635 && !CLASSTYPE_HAS_PRIMARY_BASE_P (t))
2636 /* Don't do the primary vtable, if it's new. */
2637 return NULL_TREE;
2639 if (BINFO_PRIMARY_P (binfo) && !BINFO_VIRTUAL_P (binfo))
2640 /* There's no need to modify the vtable for a non-virtual primary
2641 base; we're not going to use that vtable anyhow. We do still
2642 need to do this for virtual primary bases, as they could become
2643 non-primary in a construction vtable. */
2644 return NULL_TREE;
2646 make_new_vtable (t, binfo);
2648 /* Now, go through each of the virtual functions in the virtual
2649 function table for BINFO. Find the final overrider, and update
2650 the BINFO_VIRTUALS list appropriately. */
2651 for (ix = 0, virtuals = BINFO_VIRTUALS (binfo),
2652 old_virtuals = BINFO_VIRTUALS (TYPE_BINFO (BINFO_TYPE (binfo)));
2653 virtuals;
2654 ix++, virtuals = TREE_CHAIN (virtuals),
2655 old_virtuals = TREE_CHAIN (old_virtuals))
2656 update_vtable_entry_for_fn (t,
2657 binfo,
2658 BV_FN (old_virtuals),
2659 &virtuals, ix);
2661 return NULL_TREE;
2664 /* Update all of the primary and secondary vtables for T. Create new
2665 vtables as required, and initialize their RTTI information. Each
2666 of the functions in VIRTUALS is declared in T and may override a
2667 virtual function from a base class; find and modify the appropriate
2668 entries to point to the overriding functions. Returns a list, in
2669 declaration order, of the virtual functions that are declared in T,
2670 but do not appear in the primary base class vtable, and which
2671 should therefore be appended to the end of the vtable for T. */
2673 static tree
2674 modify_all_vtables (tree t, tree virtuals)
2676 tree binfo = TYPE_BINFO (t);
2677 tree *fnsp;
2679 /* Mangle the vtable name before entering dfs_walk (c++/51884). */
2680 if (TYPE_CONTAINS_VPTR_P (t))
2681 get_vtable_decl (t, false);
2683 /* Update all of the vtables. */
2684 dfs_walk_once (binfo, dfs_modify_vtables, NULL, t);
2686 /* Add virtual functions not already in our primary vtable. These
2687 will be both those introduced by this class, and those overridden
2688 from secondary bases. It does not include virtuals merely
2689 inherited from secondary bases. */
2690 for (fnsp = &virtuals; *fnsp; )
2692 tree fn = TREE_VALUE (*fnsp);
2694 if (!value_member (fn, BINFO_VIRTUALS (binfo))
2695 || DECL_VINDEX (fn) == error_mark_node)
2697 /* We don't need to adjust the `this' pointer when
2698 calling this function. */
2699 BV_DELTA (*fnsp) = integer_zero_node;
2700 BV_VCALL_INDEX (*fnsp) = NULL_TREE;
2702 /* This is a function not already in our vtable. Keep it. */
2703 fnsp = &TREE_CHAIN (*fnsp);
2705 else
2706 /* We've already got an entry for this function. Skip it. */
2707 *fnsp = TREE_CHAIN (*fnsp);
2710 return virtuals;
2713 /* Get the base virtual function declarations in T that have the
2714 indicated NAME. */
2716 static tree
2717 get_basefndecls (tree name, tree t)
2719 tree methods;
2720 tree base_fndecls = NULL_TREE;
2721 int n_baseclasses = BINFO_N_BASE_BINFOS (TYPE_BINFO (t));
2722 int i;
2724 /* Find virtual functions in T with the indicated NAME. */
2725 i = lookup_fnfields_1 (t, name);
2726 if (i != -1)
2727 for (methods = (*CLASSTYPE_METHOD_VEC (t))[i];
2728 methods;
2729 methods = OVL_NEXT (methods))
2731 tree method = OVL_CURRENT (methods);
2733 if (TREE_CODE (method) == FUNCTION_DECL
2734 && DECL_VINDEX (method))
2735 base_fndecls = tree_cons (NULL_TREE, method, base_fndecls);
2738 if (base_fndecls)
2739 return base_fndecls;
2741 for (i = 0; i < n_baseclasses; i++)
2743 tree basetype = BINFO_TYPE (BINFO_BASE_BINFO (TYPE_BINFO (t), i));
2744 base_fndecls = chainon (get_basefndecls (name, basetype),
2745 base_fndecls);
2748 return base_fndecls;
2751 /* If this declaration supersedes the declaration of
2752 a method declared virtual in the base class, then
2753 mark this field as being virtual as well. */
2755 void
2756 check_for_override (tree decl, tree ctype)
2758 bool overrides_found = false;
2759 if (TREE_CODE (decl) == TEMPLATE_DECL)
2760 /* In [temp.mem] we have:
2762 A specialization of a member function template does not
2763 override a virtual function from a base class. */
2764 return;
2765 if ((DECL_DESTRUCTOR_P (decl)
2766 || IDENTIFIER_VIRTUAL_P (DECL_NAME (decl))
2767 || DECL_CONV_FN_P (decl))
2768 && look_for_overrides (ctype, decl)
2769 && !DECL_STATIC_FUNCTION_P (decl))
2770 /* Set DECL_VINDEX to a value that is neither an INTEGER_CST nor
2771 the error_mark_node so that we know it is an overriding
2772 function. */
2774 DECL_VINDEX (decl) = decl;
2775 overrides_found = true;
2778 if (DECL_VIRTUAL_P (decl))
2780 if (!DECL_VINDEX (decl))
2781 DECL_VINDEX (decl) = error_mark_node;
2782 IDENTIFIER_VIRTUAL_P (DECL_NAME (decl)) = 1;
2783 if (DECL_DESTRUCTOR_P (decl))
2784 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (ctype) = true;
2786 else if (DECL_FINAL_P (decl))
2787 error ("%q+#D marked final, but is not virtual", decl);
2788 if (DECL_OVERRIDE_P (decl) && !overrides_found)
2789 error ("%q+#D marked override, but does not override", decl);
2792 /* Warn about hidden virtual functions that are not overridden in t.
2793 We know that constructors and destructors don't apply. */
2795 static void
2796 warn_hidden (tree t)
2798 vec<tree, va_gc> *method_vec = CLASSTYPE_METHOD_VEC (t);
2799 tree fns;
2800 size_t i;
2802 /* We go through each separately named virtual function. */
2803 for (i = CLASSTYPE_FIRST_CONVERSION_SLOT;
2804 vec_safe_iterate (method_vec, i, &fns);
2805 ++i)
2807 tree fn;
2808 tree name;
2809 tree fndecl;
2810 tree base_fndecls;
2811 tree base_binfo;
2812 tree binfo;
2813 int j;
2815 /* All functions in this slot in the CLASSTYPE_METHOD_VEC will
2816 have the same name. Figure out what name that is. */
2817 name = DECL_NAME (OVL_CURRENT (fns));
2818 /* There are no possibly hidden functions yet. */
2819 base_fndecls = NULL_TREE;
2820 /* Iterate through all of the base classes looking for possibly
2821 hidden functions. */
2822 for (binfo = TYPE_BINFO (t), j = 0;
2823 BINFO_BASE_ITERATE (binfo, j, base_binfo); j++)
2825 tree basetype = BINFO_TYPE (base_binfo);
2826 base_fndecls = chainon (get_basefndecls (name, basetype),
2827 base_fndecls);
2830 /* If there are no functions to hide, continue. */
2831 if (!base_fndecls)
2832 continue;
2834 /* Remove any overridden functions. */
2835 for (fn = fns; fn; fn = OVL_NEXT (fn))
2837 fndecl = OVL_CURRENT (fn);
2838 if (DECL_VINDEX (fndecl))
2840 tree *prev = &base_fndecls;
2842 while (*prev)
2843 /* If the method from the base class has the same
2844 signature as the method from the derived class, it
2845 has been overridden. */
2846 if (same_signature_p (fndecl, TREE_VALUE (*prev)))
2847 *prev = TREE_CHAIN (*prev);
2848 else
2849 prev = &TREE_CHAIN (*prev);
2853 /* Now give a warning for all base functions without overriders,
2854 as they are hidden. */
2855 while (base_fndecls)
2857 /* Here we know it is a hider, and no overrider exists. */
2858 warning (OPT_Woverloaded_virtual, "%q+D was hidden", TREE_VALUE (base_fndecls));
2859 warning (OPT_Woverloaded_virtual, " by %q+D", fns);
2860 base_fndecls = TREE_CHAIN (base_fndecls);
2865 /* Recursive helper for finish_struct_anon. */
2867 static void
2868 finish_struct_anon_r (tree field, bool complain)
2870 bool is_union = TREE_CODE (TREE_TYPE (field)) == UNION_TYPE;
2871 tree elt = TYPE_FIELDS (TREE_TYPE (field));
2872 for (; elt; elt = DECL_CHAIN (elt))
2874 /* We're generally only interested in entities the user
2875 declared, but we also find nested classes by noticing
2876 the TYPE_DECL that we create implicitly. You're
2877 allowed to put one anonymous union inside another,
2878 though, so we explicitly tolerate that. We use
2879 TYPE_ANONYMOUS_P rather than ANON_AGGR_TYPE_P so that
2880 we also allow unnamed types used for defining fields. */
2881 if (DECL_ARTIFICIAL (elt)
2882 && (!DECL_IMPLICIT_TYPEDEF_P (elt)
2883 || TYPE_ANONYMOUS_P (TREE_TYPE (elt))))
2884 continue;
2886 if (TREE_CODE (elt) != FIELD_DECL)
2888 /* We already complained about static data members in
2889 finish_static_data_member_decl. */
2890 if (complain && TREE_CODE (elt) != VAR_DECL)
2892 if (is_union)
2893 permerror (input_location,
2894 "%q+#D invalid; an anonymous union can "
2895 "only have non-static data members", elt);
2896 else
2897 permerror (input_location,
2898 "%q+#D invalid; an anonymous struct can "
2899 "only have non-static data members", elt);
2901 continue;
2904 if (complain)
2906 if (TREE_PRIVATE (elt))
2908 if (is_union)
2909 permerror (input_location,
2910 "private member %q+#D in anonymous union", elt);
2911 else
2912 permerror (input_location,
2913 "private member %q+#D in anonymous struct", elt);
2915 else if (TREE_PROTECTED (elt))
2917 if (is_union)
2918 permerror (input_location,
2919 "protected member %q+#D in anonymous union", elt);
2920 else
2921 permerror (input_location,
2922 "protected member %q+#D in anonymous struct", elt);
2926 TREE_PRIVATE (elt) = TREE_PRIVATE (field);
2927 TREE_PROTECTED (elt) = TREE_PROTECTED (field);
2929 /* Recurse into the anonymous aggregates to handle correctly
2930 access control (c++/24926):
2932 class A {
2933 union {
2934 union {
2935 int i;
2940 int j=A().i; */
2941 if (DECL_NAME (elt) == NULL_TREE
2942 && ANON_AGGR_TYPE_P (TREE_TYPE (elt)))
2943 finish_struct_anon_r (elt, /*complain=*/false);
2947 /* Check for things that are invalid. There are probably plenty of other
2948 things we should check for also. */
2950 static void
2951 finish_struct_anon (tree t)
2953 for (tree field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
2955 if (TREE_STATIC (field))
2956 continue;
2957 if (TREE_CODE (field) != FIELD_DECL)
2958 continue;
2960 if (DECL_NAME (field) == NULL_TREE
2961 && ANON_AGGR_TYPE_P (TREE_TYPE (field)))
2962 finish_struct_anon_r (field, /*complain=*/true);
2966 /* Add T to CLASSTYPE_DECL_LIST of current_class_type which
2967 will be used later during class template instantiation.
2968 When FRIEND_P is zero, T can be a static member data (VAR_DECL),
2969 a non-static member data (FIELD_DECL), a member function
2970 (FUNCTION_DECL), a nested type (RECORD_TYPE, ENUM_TYPE),
2971 a typedef (TYPE_DECL) or a member class template (TEMPLATE_DECL)
2972 When FRIEND_P is nonzero, T is either a friend class
2973 (RECORD_TYPE, TEMPLATE_DECL) or a friend function
2974 (FUNCTION_DECL, TEMPLATE_DECL). */
2976 void
2977 maybe_add_class_template_decl_list (tree type, tree t, int friend_p)
2979 /* Save some memory by not creating TREE_LIST if TYPE is not template. */
2980 if (CLASSTYPE_TEMPLATE_INFO (type))
2981 CLASSTYPE_DECL_LIST (type)
2982 = tree_cons (friend_p ? NULL_TREE : type,
2983 t, CLASSTYPE_DECL_LIST (type));
2986 /* This function is called from declare_virt_assop_and_dtor via
2987 dfs_walk_all.
2989 DATA is a type that direcly or indirectly inherits the base
2990 represented by BINFO. If BINFO contains a virtual assignment [copy
2991 assignment or move assigment] operator or a virtual constructor,
2992 declare that function in DATA if it hasn't been already declared. */
2994 static tree
2995 dfs_declare_virt_assop_and_dtor (tree binfo, void *data)
2997 tree bv, fn, t = (tree)data;
2998 tree opname = ansi_assopname (NOP_EXPR);
3000 gcc_assert (t && CLASS_TYPE_P (t));
3001 gcc_assert (binfo && TREE_CODE (binfo) == TREE_BINFO);
3003 if (!TYPE_CONTAINS_VPTR_P (BINFO_TYPE (binfo)))
3004 /* A base without a vtable needs no modification, and its bases
3005 are uninteresting. */
3006 return dfs_skip_bases;
3008 if (BINFO_PRIMARY_P (binfo))
3009 /* If this is a primary base, then we have already looked at the
3010 virtual functions of its vtable. */
3011 return NULL_TREE;
3013 for (bv = BINFO_VIRTUALS (binfo); bv; bv = TREE_CHAIN (bv))
3015 fn = BV_FN (bv);
3017 if (DECL_NAME (fn) == opname)
3019 if (CLASSTYPE_LAZY_COPY_ASSIGN (t))
3020 lazily_declare_fn (sfk_copy_assignment, t);
3021 if (CLASSTYPE_LAZY_MOVE_ASSIGN (t))
3022 lazily_declare_fn (sfk_move_assignment, t);
3024 else if (DECL_DESTRUCTOR_P (fn)
3025 && CLASSTYPE_LAZY_DESTRUCTOR (t))
3026 lazily_declare_fn (sfk_destructor, t);
3029 return NULL_TREE;
3032 /* If the class type T has a direct or indirect base that contains a
3033 virtual assignment operator or a virtual destructor, declare that
3034 function in T if it hasn't been already declared. */
3036 static void
3037 declare_virt_assop_and_dtor (tree t)
3039 if (!(TYPE_POLYMORPHIC_P (t)
3040 && (CLASSTYPE_LAZY_COPY_ASSIGN (t)
3041 || CLASSTYPE_LAZY_MOVE_ASSIGN (t)
3042 || CLASSTYPE_LAZY_DESTRUCTOR (t))))
3043 return;
3045 dfs_walk_all (TYPE_BINFO (t),
3046 dfs_declare_virt_assop_and_dtor,
3047 NULL, t);
3050 /* Declare the inheriting constructor for class T inherited from base
3051 constructor CTOR with the parameter array PARMS of size NPARMS. */
3053 static void
3054 one_inheriting_sig (tree t, tree ctor, tree *parms, int nparms)
3056 /* We don't declare an inheriting ctor that would be a default,
3057 copy or move ctor for derived or base. */
3058 if (nparms == 0)
3059 return;
3060 if (nparms == 1
3061 && TREE_CODE (parms[0]) == REFERENCE_TYPE)
3063 tree parm = TYPE_MAIN_VARIANT (TREE_TYPE (parms[0]));
3064 if (parm == t || parm == DECL_CONTEXT (ctor))
3065 return;
3068 tree parmlist = void_list_node;
3069 for (int i = nparms - 1; i >= 0; i--)
3070 parmlist = tree_cons (NULL_TREE, parms[i], parmlist);
3071 tree fn = implicitly_declare_fn (sfk_inheriting_constructor,
3072 t, false, ctor, parmlist);
3073 if (add_method (t, fn, NULL_TREE))
3075 DECL_CHAIN (fn) = TYPE_METHODS (t);
3076 TYPE_METHODS (t) = fn;
3080 /* Declare all the inheriting constructors for class T inherited from base
3081 constructor CTOR. */
3083 static void
3084 one_inherited_ctor (tree ctor, tree t)
3086 tree parms = FUNCTION_FIRST_USER_PARMTYPE (ctor);
3088 tree *new_parms = XALLOCAVEC (tree, list_length (parms));
3089 int i = 0;
3090 for (; parms && parms != void_list_node; parms = TREE_CHAIN (parms))
3092 if (TREE_PURPOSE (parms))
3093 one_inheriting_sig (t, ctor, new_parms, i);
3094 new_parms[i++] = TREE_VALUE (parms);
3096 one_inheriting_sig (t, ctor, new_parms, i);
3097 if (parms == NULL_TREE)
3099 warning (OPT_Winherited_variadic_ctor,
3100 "the ellipsis in %qD is not inherited", ctor);
3101 inform (DECL_SOURCE_LOCATION (ctor), "%qD declared here", ctor);
3105 /* Create default constructors, assignment operators, and so forth for
3106 the type indicated by T, if they are needed. CANT_HAVE_CONST_CTOR,
3107 and CANT_HAVE_CONST_ASSIGNMENT are nonzero if, for whatever reason,
3108 the class cannot have a default constructor, copy constructor
3109 taking a const reference argument, or an assignment operator taking
3110 a const reference, respectively. */
3112 static void
3113 add_implicitly_declared_members (tree t, tree* access_decls,
3114 int cant_have_const_cctor,
3115 int cant_have_const_assignment)
3117 bool move_ok = false;
3119 if (cxx_dialect >= cxx11 && !CLASSTYPE_DESTRUCTORS (t)
3120 && !TYPE_HAS_COPY_CTOR (t) && !TYPE_HAS_COPY_ASSIGN (t)
3121 && !type_has_move_constructor (t) && !type_has_move_assign (t))
3122 move_ok = true;
3124 /* Destructor. */
3125 if (!CLASSTYPE_DESTRUCTORS (t))
3127 /* In general, we create destructors lazily. */
3128 CLASSTYPE_LAZY_DESTRUCTOR (t) = 1;
3130 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t)
3131 && TYPE_FOR_JAVA (t))
3132 /* But if this is a Java class, any non-trivial destructor is
3133 invalid, even if compiler-generated. Therefore, if the
3134 destructor is non-trivial we create it now. */
3135 lazily_declare_fn (sfk_destructor, t);
3138 /* [class.ctor]
3140 If there is no user-declared constructor for a class, a default
3141 constructor is implicitly declared. */
3142 if (! TYPE_HAS_USER_CONSTRUCTOR (t))
3144 TYPE_HAS_DEFAULT_CONSTRUCTOR (t) = 1;
3145 CLASSTYPE_LAZY_DEFAULT_CTOR (t) = 1;
3146 if (cxx_dialect >= cxx11)
3147 TYPE_HAS_CONSTEXPR_CTOR (t)
3148 /* This might force the declaration. */
3149 = type_has_constexpr_default_constructor (t);
3152 /* [class.ctor]
3154 If a class definition does not explicitly declare a copy
3155 constructor, one is declared implicitly. */
3156 if (! TYPE_HAS_COPY_CTOR (t) && ! TYPE_FOR_JAVA (t))
3158 TYPE_HAS_COPY_CTOR (t) = 1;
3159 TYPE_HAS_CONST_COPY_CTOR (t) = !cant_have_const_cctor;
3160 CLASSTYPE_LAZY_COPY_CTOR (t) = 1;
3161 if (move_ok)
3162 CLASSTYPE_LAZY_MOVE_CTOR (t) = 1;
3165 /* If there is no assignment operator, one will be created if and
3166 when it is needed. For now, just record whether or not the type
3167 of the parameter to the assignment operator will be a const or
3168 non-const reference. */
3169 if (!TYPE_HAS_COPY_ASSIGN (t) && !TYPE_FOR_JAVA (t))
3171 TYPE_HAS_COPY_ASSIGN (t) = 1;
3172 TYPE_HAS_CONST_COPY_ASSIGN (t) = !cant_have_const_assignment;
3173 CLASSTYPE_LAZY_COPY_ASSIGN (t) = 1;
3174 if (move_ok)
3175 CLASSTYPE_LAZY_MOVE_ASSIGN (t) = 1;
3178 /* We can't be lazy about declaring functions that might override
3179 a virtual function from a base class. */
3180 declare_virt_assop_and_dtor (t);
3182 while (*access_decls)
3184 tree using_decl = TREE_VALUE (*access_decls);
3185 tree decl = USING_DECL_DECLS (using_decl);
3186 if (DECL_NAME (using_decl) == ctor_identifier)
3188 /* declare, then remove the decl */
3189 tree ctor_list = decl;
3190 location_t loc = input_location;
3191 input_location = DECL_SOURCE_LOCATION (using_decl);
3192 if (ctor_list)
3193 for (; ctor_list; ctor_list = OVL_NEXT (ctor_list))
3194 one_inherited_ctor (OVL_CURRENT (ctor_list), t);
3195 *access_decls = TREE_CHAIN (*access_decls);
3196 input_location = loc;
3198 else
3199 access_decls = &TREE_CHAIN (*access_decls);
3203 /* Subroutine of insert_into_classtype_sorted_fields. Recursively
3204 count the number of fields in TYPE, including anonymous union
3205 members. */
3207 static int
3208 count_fields (tree fields)
3210 tree x;
3211 int n_fields = 0;
3212 for (x = fields; x; x = DECL_CHAIN (x))
3214 if (TREE_CODE (x) == FIELD_DECL && ANON_AGGR_TYPE_P (TREE_TYPE (x)))
3215 n_fields += count_fields (TYPE_FIELDS (TREE_TYPE (x)));
3216 else
3217 n_fields += 1;
3219 return n_fields;
3222 /* Subroutine of insert_into_classtype_sorted_fields. Recursively add
3223 all the fields in the TREE_LIST FIELDS to the SORTED_FIELDS_TYPE
3224 elts, starting at offset IDX. */
3226 static int
3227 add_fields_to_record_type (tree fields, struct sorted_fields_type *field_vec, int idx)
3229 tree x;
3230 for (x = fields; x; x = DECL_CHAIN (x))
3232 if (TREE_CODE (x) == FIELD_DECL && ANON_AGGR_TYPE_P (TREE_TYPE (x)))
3233 idx = add_fields_to_record_type (TYPE_FIELDS (TREE_TYPE (x)), field_vec, idx);
3234 else
3235 field_vec->elts[idx++] = x;
3237 return idx;
3240 /* Add all of the enum values of ENUMTYPE, to the FIELD_VEC elts,
3241 starting at offset IDX. */
3243 static int
3244 add_enum_fields_to_record_type (tree enumtype,
3245 struct sorted_fields_type *field_vec,
3246 int idx)
3248 tree values;
3249 for (values = TYPE_VALUES (enumtype); values; values = TREE_CHAIN (values))
3250 field_vec->elts[idx++] = TREE_VALUE (values);
3251 return idx;
3254 /* FIELD is a bit-field. We are finishing the processing for its
3255 enclosing type. Issue any appropriate messages and set appropriate
3256 flags. Returns false if an error has been diagnosed. */
3258 static bool
3259 check_bitfield_decl (tree field)
3261 tree type = TREE_TYPE (field);
3262 tree w;
3264 /* Extract the declared width of the bitfield, which has been
3265 temporarily stashed in DECL_INITIAL. */
3266 w = DECL_INITIAL (field);
3267 gcc_assert (w != NULL_TREE);
3268 /* Remove the bit-field width indicator so that the rest of the
3269 compiler does not treat that value as an initializer. */
3270 DECL_INITIAL (field) = NULL_TREE;
3272 /* Detect invalid bit-field type. */
3273 if (!INTEGRAL_OR_ENUMERATION_TYPE_P (type))
3275 error ("bit-field %q+#D with non-integral type", field);
3276 w = error_mark_node;
3278 else
3280 location_t loc = input_location;
3281 /* Avoid the non_lvalue wrapper added by fold for PLUS_EXPRs. */
3282 STRIP_NOPS (w);
3284 /* detect invalid field size. */
3285 input_location = DECL_SOURCE_LOCATION (field);
3286 w = cxx_constant_value (w);
3287 input_location = loc;
3289 if (TREE_CODE (w) != INTEGER_CST)
3291 error ("bit-field %q+D width not an integer constant", field);
3292 w = error_mark_node;
3294 else if (tree_int_cst_sgn (w) < 0)
3296 error ("negative width in bit-field %q+D", field);
3297 w = error_mark_node;
3299 else if (integer_zerop (w) && DECL_NAME (field) != 0)
3301 error ("zero width for bit-field %q+D", field);
3302 w = error_mark_node;
3304 else if ((TREE_CODE (type) != ENUMERAL_TYPE
3305 && TREE_CODE (type) != BOOLEAN_TYPE
3306 && compare_tree_int (w, TYPE_PRECISION (type)) > 0)
3307 || ((TREE_CODE (type) == ENUMERAL_TYPE
3308 || TREE_CODE (type) == BOOLEAN_TYPE)
3309 && tree_int_cst_lt (TYPE_SIZE (type), w)))
3310 warning (0, "width of %q+D exceeds its type", field);
3311 else if (TREE_CODE (type) == ENUMERAL_TYPE
3312 && (0 > (compare_tree_int
3313 (w, TYPE_PRECISION (ENUM_UNDERLYING_TYPE (type))))))
3314 warning (0, "%q+D is too small to hold all values of %q#T", field, type);
3317 if (w != error_mark_node)
3319 DECL_SIZE (field) = convert (bitsizetype, w);
3320 DECL_BIT_FIELD (field) = 1;
3321 return true;
3323 else
3325 /* Non-bit-fields are aligned for their type. */
3326 DECL_BIT_FIELD (field) = 0;
3327 CLEAR_DECL_C_BIT_FIELD (field);
3328 return false;
3332 /* FIELD is a non bit-field. We are finishing the processing for its
3333 enclosing type T. Issue any appropriate messages and set appropriate
3334 flags. */
3336 static void
3337 check_field_decl (tree field,
3338 tree t,
3339 int* cant_have_const_ctor,
3340 int* no_const_asn_ref,
3341 int* any_default_members)
3343 tree type = strip_array_types (TREE_TYPE (field));
3345 /* In C++98 an anonymous union cannot contain any fields which would change
3346 the settings of CANT_HAVE_CONST_CTOR and friends. */
3347 if (ANON_UNION_TYPE_P (type) && cxx_dialect < cxx11)
3349 /* And, we don't set TYPE_HAS_CONST_COPY_CTOR, etc., for anonymous
3350 structs. So, we recurse through their fields here. */
3351 else if (ANON_AGGR_TYPE_P (type))
3353 tree fields;
3355 for (fields = TYPE_FIELDS (type); fields; fields = DECL_CHAIN (fields))
3356 if (TREE_CODE (fields) == FIELD_DECL && !DECL_C_BIT_FIELD (field))
3357 check_field_decl (fields, t, cant_have_const_ctor,
3358 no_const_asn_ref, any_default_members);
3360 /* Check members with class type for constructors, destructors,
3361 etc. */
3362 else if (CLASS_TYPE_P (type))
3364 /* Never let anything with uninheritable virtuals
3365 make it through without complaint. */
3366 abstract_virtuals_error (field, type);
3368 if (TREE_CODE (t) == UNION_TYPE && cxx_dialect < cxx11)
3370 static bool warned;
3371 int oldcount = errorcount;
3372 if (TYPE_NEEDS_CONSTRUCTING (type))
3373 error ("member %q+#D with constructor not allowed in union",
3374 field);
3375 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
3376 error ("member %q+#D with destructor not allowed in union", field);
3377 if (TYPE_HAS_COMPLEX_COPY_ASSIGN (type))
3378 error ("member %q+#D with copy assignment operator not allowed in union",
3379 field);
3380 if (!warned && errorcount > oldcount)
3382 inform (DECL_SOURCE_LOCATION (field), "unrestricted unions "
3383 "only available with -std=c++11 or -std=gnu++11");
3384 warned = true;
3387 else
3389 TYPE_NEEDS_CONSTRUCTING (t) |= TYPE_NEEDS_CONSTRUCTING (type);
3390 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t)
3391 |= TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type);
3392 TYPE_HAS_COMPLEX_COPY_ASSIGN (t)
3393 |= (TYPE_HAS_COMPLEX_COPY_ASSIGN (type)
3394 || !TYPE_HAS_COPY_ASSIGN (type));
3395 TYPE_HAS_COMPLEX_COPY_CTOR (t) |= (TYPE_HAS_COMPLEX_COPY_CTOR (type)
3396 || !TYPE_HAS_COPY_CTOR (type));
3397 TYPE_HAS_COMPLEX_MOVE_ASSIGN (t) |= TYPE_HAS_COMPLEX_MOVE_ASSIGN (type);
3398 TYPE_HAS_COMPLEX_MOVE_CTOR (t) |= TYPE_HAS_COMPLEX_MOVE_CTOR (type);
3399 TYPE_HAS_COMPLEX_DFLT (t) |= (!TYPE_HAS_DEFAULT_CONSTRUCTOR (type)
3400 || TYPE_HAS_COMPLEX_DFLT (type));
3403 if (TYPE_HAS_COPY_CTOR (type)
3404 && !TYPE_HAS_CONST_COPY_CTOR (type))
3405 *cant_have_const_ctor = 1;
3407 if (TYPE_HAS_COPY_ASSIGN (type)
3408 && !TYPE_HAS_CONST_COPY_ASSIGN (type))
3409 *no_const_asn_ref = 1;
3412 check_abi_tags (t, field);
3414 if (DECL_INITIAL (field) != NULL_TREE)
3416 /* `build_class_init_list' does not recognize
3417 non-FIELD_DECLs. */
3418 if (TREE_CODE (t) == UNION_TYPE && *any_default_members != 0)
3419 error ("multiple fields in union %qT initialized", t);
3420 *any_default_members = 1;
3424 /* Check the data members (both static and non-static), class-scoped
3425 typedefs, etc., appearing in the declaration of T. Issue
3426 appropriate diagnostics. Sets ACCESS_DECLS to a list (in
3427 declaration order) of access declarations; each TREE_VALUE in this
3428 list is a USING_DECL.
3430 In addition, set the following flags:
3432 EMPTY_P
3433 The class is empty, i.e., contains no non-static data members.
3435 CANT_HAVE_CONST_CTOR_P
3436 This class cannot have an implicitly generated copy constructor
3437 taking a const reference.
3439 CANT_HAVE_CONST_ASN_REF
3440 This class cannot have an implicitly generated assignment
3441 operator taking a const reference.
3443 All of these flags should be initialized before calling this
3444 function.
3446 Returns a pointer to the end of the TYPE_FIELDs chain; additional
3447 fields can be added by adding to this chain. */
3449 static void
3450 check_field_decls (tree t, tree *access_decls,
3451 int *cant_have_const_ctor_p,
3452 int *no_const_asn_ref_p)
3454 tree *field;
3455 tree *next;
3456 bool has_pointers;
3457 int any_default_members;
3458 int cant_pack = 0;
3459 int field_access = -1;
3461 /* Assume there are no access declarations. */
3462 *access_decls = NULL_TREE;
3463 /* Assume this class has no pointer members. */
3464 has_pointers = false;
3465 /* Assume none of the members of this class have default
3466 initializations. */
3467 any_default_members = 0;
3469 for (field = &TYPE_FIELDS (t); *field; field = next)
3471 tree x = *field;
3472 tree type = TREE_TYPE (x);
3473 int this_field_access;
3475 next = &DECL_CHAIN (x);
3477 if (TREE_CODE (x) == USING_DECL)
3479 /* Save the access declarations for our caller. */
3480 *access_decls = tree_cons (NULL_TREE, x, *access_decls);
3481 continue;
3484 if (TREE_CODE (x) == TYPE_DECL
3485 || TREE_CODE (x) == TEMPLATE_DECL)
3486 continue;
3488 /* If we've gotten this far, it's a data member, possibly static,
3489 or an enumerator. */
3490 if (TREE_CODE (x) != CONST_DECL)
3491 DECL_CONTEXT (x) = t;
3493 /* When this goes into scope, it will be a non-local reference. */
3494 DECL_NONLOCAL (x) = 1;
3496 if (TREE_CODE (t) == UNION_TYPE)
3498 /* [class.union]
3500 If a union contains a static data member, or a member of
3501 reference type, the program is ill-formed. */
3502 if (VAR_P (x))
3504 error ("%q+D may not be static because it is a member of a union", x);
3505 continue;
3507 if (TREE_CODE (type) == REFERENCE_TYPE)
3509 error ("%q+D may not have reference type %qT because"
3510 " it is a member of a union",
3511 x, type);
3512 continue;
3516 /* Perform error checking that did not get done in
3517 grokdeclarator. */
3518 if (TREE_CODE (type) == FUNCTION_TYPE)
3520 error ("field %q+D invalidly declared function type", x);
3521 type = build_pointer_type (type);
3522 TREE_TYPE (x) = type;
3524 else if (TREE_CODE (type) == METHOD_TYPE)
3526 error ("field %q+D invalidly declared method type", x);
3527 type = build_pointer_type (type);
3528 TREE_TYPE (x) = type;
3531 if (type == error_mark_node)
3532 continue;
3534 if (TREE_CODE (x) == CONST_DECL || VAR_P (x))
3535 continue;
3537 /* Now it can only be a FIELD_DECL. */
3539 if (TREE_PRIVATE (x) || TREE_PROTECTED (x))
3540 CLASSTYPE_NON_AGGREGATE (t) = 1;
3542 /* If at least one non-static data member is non-literal, the whole
3543 class becomes non-literal. Note: if the type is incomplete we
3544 will complain later on. */
3545 if (COMPLETE_TYPE_P (type) && !literal_type_p (type))
3546 CLASSTYPE_LITERAL_P (t) = false;
3548 /* A standard-layout class is a class that:
3550 has the same access control (Clause 11) for all non-static data members,
3551 ... */
3552 this_field_access = TREE_PROTECTED (x) ? 1 : TREE_PRIVATE (x) ? 2 : 0;
3553 if (field_access == -1)
3554 field_access = this_field_access;
3555 else if (this_field_access != field_access)
3556 CLASSTYPE_NON_STD_LAYOUT (t) = 1;
3558 /* If this is of reference type, check if it needs an init. */
3559 if (TREE_CODE (type) == REFERENCE_TYPE)
3561 CLASSTYPE_NON_LAYOUT_POD_P (t) = 1;
3562 CLASSTYPE_NON_STD_LAYOUT (t) = 1;
3563 if (DECL_INITIAL (x) == NULL_TREE)
3564 SET_CLASSTYPE_REF_FIELDS_NEED_INIT (t, 1);
3566 /* ARM $12.6.2: [A member initializer list] (or, for an
3567 aggregate, initialization by a brace-enclosed list) is the
3568 only way to initialize nonstatic const and reference
3569 members. */
3570 TYPE_HAS_COMPLEX_COPY_ASSIGN (t) = 1;
3571 TYPE_HAS_COMPLEX_MOVE_ASSIGN (t) = 1;
3574 type = strip_array_types (type);
3576 if (TYPE_PACKED (t))
3578 if (!layout_pod_type_p (type) && !TYPE_PACKED (type))
3580 warning
3582 "ignoring packed attribute because of unpacked non-POD field %q+#D",
3584 cant_pack = 1;
3586 else if (DECL_C_BIT_FIELD (x)
3587 || TYPE_ALIGN (TREE_TYPE (x)) > BITS_PER_UNIT)
3588 DECL_PACKED (x) = 1;
3591 if (DECL_C_BIT_FIELD (x) && integer_zerop (DECL_INITIAL (x)))
3592 /* We don't treat zero-width bitfields as making a class
3593 non-empty. */
3595 else
3597 /* The class is non-empty. */
3598 CLASSTYPE_EMPTY_P (t) = 0;
3599 /* The class is not even nearly empty. */
3600 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
3601 /* If one of the data members contains an empty class,
3602 so does T. */
3603 if (CLASS_TYPE_P (type)
3604 && CLASSTYPE_CONTAINS_EMPTY_CLASS_P (type))
3605 CLASSTYPE_CONTAINS_EMPTY_CLASS_P (t) = 1;
3608 /* This is used by -Weffc++ (see below). Warn only for pointers
3609 to members which might hold dynamic memory. So do not warn
3610 for pointers to functions or pointers to members. */
3611 if (TYPE_PTR_P (type)
3612 && !TYPE_PTRFN_P (type))
3613 has_pointers = true;
3615 if (CLASS_TYPE_P (type))
3617 if (CLASSTYPE_REF_FIELDS_NEED_INIT (type))
3618 SET_CLASSTYPE_REF_FIELDS_NEED_INIT (t, 1);
3619 if (CLASSTYPE_READONLY_FIELDS_NEED_INIT (type))
3620 SET_CLASSTYPE_READONLY_FIELDS_NEED_INIT (t, 1);
3623 if (DECL_MUTABLE_P (x) || TYPE_HAS_MUTABLE_P (type))
3624 CLASSTYPE_HAS_MUTABLE (t) = 1;
3626 if (DECL_MUTABLE_P (x))
3628 if (CP_TYPE_CONST_P (type))
3630 error ("member %q+D cannot be declared both %<const%> "
3631 "and %<mutable%>", x);
3632 continue;
3634 if (TREE_CODE (type) == REFERENCE_TYPE)
3636 error ("member %q+D cannot be declared as a %<mutable%> "
3637 "reference", x);
3638 continue;
3642 if (! layout_pod_type_p (type))
3643 /* DR 148 now allows pointers to members (which are POD themselves),
3644 to be allowed in POD structs. */
3645 CLASSTYPE_NON_LAYOUT_POD_P (t) = 1;
3647 if (!std_layout_type_p (type))
3648 CLASSTYPE_NON_STD_LAYOUT (t) = 1;
3650 if (! zero_init_p (type))
3651 CLASSTYPE_NON_ZERO_INIT_P (t) = 1;
3653 /* We set DECL_C_BIT_FIELD in grokbitfield.
3654 If the type and width are valid, we'll also set DECL_BIT_FIELD. */
3655 if (! DECL_C_BIT_FIELD (x) || ! check_bitfield_decl (x))
3656 check_field_decl (x, t,
3657 cant_have_const_ctor_p,
3658 no_const_asn_ref_p,
3659 &any_default_members);
3661 /* Now that we've removed bit-field widths from DECL_INITIAL,
3662 anything left in DECL_INITIAL is an NSDMI that makes the class
3663 non-aggregate. */
3664 if (DECL_INITIAL (x))
3665 CLASSTYPE_NON_AGGREGATE (t) = true;
3667 /* If any field is const, the structure type is pseudo-const. */
3668 if (CP_TYPE_CONST_P (type))
3670 C_TYPE_FIELDS_READONLY (t) = 1;
3671 if (DECL_INITIAL (x) == NULL_TREE)
3672 SET_CLASSTYPE_READONLY_FIELDS_NEED_INIT (t, 1);
3674 /* ARM $12.6.2: [A member initializer list] (or, for an
3675 aggregate, initialization by a brace-enclosed list) is the
3676 only way to initialize nonstatic const and reference
3677 members. */
3678 TYPE_HAS_COMPLEX_COPY_ASSIGN (t) = 1;
3679 TYPE_HAS_COMPLEX_MOVE_ASSIGN (t) = 1;
3681 /* A field that is pseudo-const makes the structure likewise. */
3682 else if (CLASS_TYPE_P (type))
3684 C_TYPE_FIELDS_READONLY (t) |= C_TYPE_FIELDS_READONLY (type);
3685 SET_CLASSTYPE_READONLY_FIELDS_NEED_INIT (t,
3686 CLASSTYPE_READONLY_FIELDS_NEED_INIT (t)
3687 | CLASSTYPE_READONLY_FIELDS_NEED_INIT (type));
3690 /* Core issue 80: A nonstatic data member is required to have a
3691 different name from the class iff the class has a
3692 user-declared constructor. */
3693 if (constructor_name_p (DECL_NAME (x), t)
3694 && TYPE_HAS_USER_CONSTRUCTOR (t))
3695 permerror (input_location, "field %q+#D with same name as class", x);
3698 /* Effective C++ rule 11: if a class has dynamic memory held by pointers,
3699 it should also define a copy constructor and an assignment operator to
3700 implement the correct copy semantic (deep vs shallow, etc.). As it is
3701 not feasible to check whether the constructors do allocate dynamic memory
3702 and store it within members, we approximate the warning like this:
3704 -- Warn only if there are members which are pointers
3705 -- Warn only if there is a non-trivial constructor (otherwise,
3706 there cannot be memory allocated).
3707 -- Warn only if there is a non-trivial destructor. We assume that the
3708 user at least implemented the cleanup correctly, and a destructor
3709 is needed to free dynamic memory.
3711 This seems enough for practical purposes. */
3712 if (warn_ecpp
3713 && has_pointers
3714 && TYPE_HAS_USER_CONSTRUCTOR (t)
3715 && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t)
3716 && !(TYPE_HAS_COPY_CTOR (t) && TYPE_HAS_COPY_ASSIGN (t)))
3718 warning (OPT_Weffc__, "%q#T has pointer data members", t);
3720 if (! TYPE_HAS_COPY_CTOR (t))
3722 warning (OPT_Weffc__,
3723 " but does not override %<%T(const %T&)%>", t, t);
3724 if (!TYPE_HAS_COPY_ASSIGN (t))
3725 warning (OPT_Weffc__, " or %<operator=(const %T&)%>", t);
3727 else if (! TYPE_HAS_COPY_ASSIGN (t))
3728 warning (OPT_Weffc__,
3729 " but does not override %<operator=(const %T&)%>", t);
3732 /* Non-static data member initializers make the default constructor
3733 non-trivial. */
3734 if (any_default_members)
3736 TYPE_NEEDS_CONSTRUCTING (t) = true;
3737 TYPE_HAS_COMPLEX_DFLT (t) = true;
3740 /* If any of the fields couldn't be packed, unset TYPE_PACKED. */
3741 if (cant_pack)
3742 TYPE_PACKED (t) = 0;
3744 /* Check anonymous struct/anonymous union fields. */
3745 finish_struct_anon (t);
3747 /* We've built up the list of access declarations in reverse order.
3748 Fix that now. */
3749 *access_decls = nreverse (*access_decls);
3752 /* If TYPE is an empty class type, records its OFFSET in the table of
3753 OFFSETS. */
3755 static int
3756 record_subobject_offset (tree type, tree offset, splay_tree offsets)
3758 splay_tree_node n;
3760 if (!is_empty_class (type))
3761 return 0;
3763 /* Record the location of this empty object in OFFSETS. */
3764 n = splay_tree_lookup (offsets, (splay_tree_key) offset);
3765 if (!n)
3766 n = splay_tree_insert (offsets,
3767 (splay_tree_key) offset,
3768 (splay_tree_value) NULL_TREE);
3769 n->value = ((splay_tree_value)
3770 tree_cons (NULL_TREE,
3771 type,
3772 (tree) n->value));
3774 return 0;
3777 /* Returns nonzero if TYPE is an empty class type and there is
3778 already an entry in OFFSETS for the same TYPE as the same OFFSET. */
3780 static int
3781 check_subobject_offset (tree type, tree offset, splay_tree offsets)
3783 splay_tree_node n;
3784 tree t;
3786 if (!is_empty_class (type))
3787 return 0;
3789 /* Record the location of this empty object in OFFSETS. */
3790 n = splay_tree_lookup (offsets, (splay_tree_key) offset);
3791 if (!n)
3792 return 0;
3794 for (t = (tree) n->value; t; t = TREE_CHAIN (t))
3795 if (same_type_p (TREE_VALUE (t), type))
3796 return 1;
3798 return 0;
3801 /* Walk through all the subobjects of TYPE (located at OFFSET). Call
3802 F for every subobject, passing it the type, offset, and table of
3803 OFFSETS. If VBASES_P is one, then virtual non-primary bases should
3804 be traversed.
3806 If MAX_OFFSET is non-NULL, then subobjects with an offset greater
3807 than MAX_OFFSET will not be walked.
3809 If F returns a nonzero value, the traversal ceases, and that value
3810 is returned. Otherwise, returns zero. */
3812 static int
3813 walk_subobject_offsets (tree type,
3814 subobject_offset_fn f,
3815 tree offset,
3816 splay_tree offsets,
3817 tree max_offset,
3818 int vbases_p)
3820 int r = 0;
3821 tree type_binfo = NULL_TREE;
3823 /* If this OFFSET is bigger than the MAX_OFFSET, then we should
3824 stop. */
3825 if (max_offset && INT_CST_LT (max_offset, offset))
3826 return 0;
3828 if (type == error_mark_node)
3829 return 0;
3831 if (!TYPE_P (type))
3833 if (abi_version_at_least (2))
3834 type_binfo = type;
3835 type = BINFO_TYPE (type);
3838 if (CLASS_TYPE_P (type))
3840 tree field;
3841 tree binfo;
3842 int i;
3844 /* Avoid recursing into objects that are not interesting. */
3845 if (!CLASSTYPE_CONTAINS_EMPTY_CLASS_P (type))
3846 return 0;
3848 /* Record the location of TYPE. */
3849 r = (*f) (type, offset, offsets);
3850 if (r)
3851 return r;
3853 /* Iterate through the direct base classes of TYPE. */
3854 if (!type_binfo)
3855 type_binfo = TYPE_BINFO (type);
3856 for (i = 0; BINFO_BASE_ITERATE (type_binfo, i, binfo); i++)
3858 tree binfo_offset;
3860 if (abi_version_at_least (2)
3861 && BINFO_VIRTUAL_P (binfo))
3862 continue;
3864 if (!vbases_p
3865 && BINFO_VIRTUAL_P (binfo)
3866 && !BINFO_PRIMARY_P (binfo))
3867 continue;
3869 if (!abi_version_at_least (2))
3870 binfo_offset = size_binop (PLUS_EXPR,
3871 offset,
3872 BINFO_OFFSET (binfo));
3873 else
3875 tree orig_binfo;
3876 /* We cannot rely on BINFO_OFFSET being set for the base
3877 class yet, but the offsets for direct non-virtual
3878 bases can be calculated by going back to the TYPE. */
3879 orig_binfo = BINFO_BASE_BINFO (TYPE_BINFO (type), i);
3880 binfo_offset = size_binop (PLUS_EXPR,
3881 offset,
3882 BINFO_OFFSET (orig_binfo));
3885 r = walk_subobject_offsets (binfo,
3887 binfo_offset,
3888 offsets,
3889 max_offset,
3890 (abi_version_at_least (2)
3891 ? /*vbases_p=*/0 : vbases_p));
3892 if (r)
3893 return r;
3896 if (abi_version_at_least (2) && CLASSTYPE_VBASECLASSES (type))
3898 unsigned ix;
3899 vec<tree, va_gc> *vbases;
3901 /* Iterate through the virtual base classes of TYPE. In G++
3902 3.2, we included virtual bases in the direct base class
3903 loop above, which results in incorrect results; the
3904 correct offsets for virtual bases are only known when
3905 working with the most derived type. */
3906 if (vbases_p)
3907 for (vbases = CLASSTYPE_VBASECLASSES (type), ix = 0;
3908 vec_safe_iterate (vbases, ix, &binfo); ix++)
3910 r = walk_subobject_offsets (binfo,
3912 size_binop (PLUS_EXPR,
3913 offset,
3914 BINFO_OFFSET (binfo)),
3915 offsets,
3916 max_offset,
3917 /*vbases_p=*/0);
3918 if (r)
3919 return r;
3921 else
3923 /* We still have to walk the primary base, if it is
3924 virtual. (If it is non-virtual, then it was walked
3925 above.) */
3926 tree vbase = get_primary_binfo (type_binfo);
3928 if (vbase && BINFO_VIRTUAL_P (vbase)
3929 && BINFO_PRIMARY_P (vbase)
3930 && BINFO_INHERITANCE_CHAIN (vbase) == type_binfo)
3932 r = (walk_subobject_offsets
3933 (vbase, f, offset,
3934 offsets, max_offset, /*vbases_p=*/0));
3935 if (r)
3936 return r;
3941 /* Iterate through the fields of TYPE. */
3942 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
3943 if (TREE_CODE (field) == FIELD_DECL
3944 && TREE_TYPE (field) != error_mark_node
3945 && !DECL_ARTIFICIAL (field))
3947 tree field_offset;
3949 if (abi_version_at_least (2))
3950 field_offset = byte_position (field);
3951 else
3952 /* In G++ 3.2, DECL_FIELD_OFFSET was used. */
3953 field_offset = DECL_FIELD_OFFSET (field);
3955 r = walk_subobject_offsets (TREE_TYPE (field),
3957 size_binop (PLUS_EXPR,
3958 offset,
3959 field_offset),
3960 offsets,
3961 max_offset,
3962 /*vbases_p=*/1);
3963 if (r)
3964 return r;
3967 else if (TREE_CODE (type) == ARRAY_TYPE)
3969 tree element_type = strip_array_types (type);
3970 tree domain = TYPE_DOMAIN (type);
3971 tree index;
3973 /* Avoid recursing into objects that are not interesting. */
3974 if (!CLASS_TYPE_P (element_type)
3975 || !CLASSTYPE_CONTAINS_EMPTY_CLASS_P (element_type))
3976 return 0;
3978 /* Step through each of the elements in the array. */
3979 for (index = size_zero_node;
3980 /* G++ 3.2 had an off-by-one error here. */
3981 (abi_version_at_least (2)
3982 ? !INT_CST_LT (TYPE_MAX_VALUE (domain), index)
3983 : INT_CST_LT (index, TYPE_MAX_VALUE (domain)));
3984 index = size_binop (PLUS_EXPR, index, size_one_node))
3986 r = walk_subobject_offsets (TREE_TYPE (type),
3988 offset,
3989 offsets,
3990 max_offset,
3991 /*vbases_p=*/1);
3992 if (r)
3993 return r;
3994 offset = size_binop (PLUS_EXPR, offset,
3995 TYPE_SIZE_UNIT (TREE_TYPE (type)));
3996 /* If this new OFFSET is bigger than the MAX_OFFSET, then
3997 there's no point in iterating through the remaining
3998 elements of the array. */
3999 if (max_offset && INT_CST_LT (max_offset, offset))
4000 break;
4004 return 0;
4007 /* Record all of the empty subobjects of TYPE (either a type or a
4008 binfo). If IS_DATA_MEMBER is true, then a non-static data member
4009 is being placed at OFFSET; otherwise, it is a base class that is
4010 being placed at OFFSET. */
4012 static void
4013 record_subobject_offsets (tree type,
4014 tree offset,
4015 splay_tree offsets,
4016 bool is_data_member)
4018 tree max_offset;
4019 /* If recording subobjects for a non-static data member or a
4020 non-empty base class , we do not need to record offsets beyond
4021 the size of the biggest empty class. Additional data members
4022 will go at the end of the class. Additional base classes will go
4023 either at offset zero (if empty, in which case they cannot
4024 overlap with offsets past the size of the biggest empty class) or
4025 at the end of the class.
4027 However, if we are placing an empty base class, then we must record
4028 all offsets, as either the empty class is at offset zero (where
4029 other empty classes might later be placed) or at the end of the
4030 class (where other objects might then be placed, so other empty
4031 subobjects might later overlap). */
4032 if (is_data_member
4033 || !is_empty_class (BINFO_TYPE (type)))
4034 max_offset = sizeof_biggest_empty_class;
4035 else
4036 max_offset = NULL_TREE;
4037 walk_subobject_offsets (type, record_subobject_offset, offset,
4038 offsets, max_offset, is_data_member);
4041 /* Returns nonzero if any of the empty subobjects of TYPE (located at
4042 OFFSET) conflict with entries in OFFSETS. If VBASES_P is nonzero,
4043 virtual bases of TYPE are examined. */
4045 static int
4046 layout_conflict_p (tree type,
4047 tree offset,
4048 splay_tree offsets,
4049 int vbases_p)
4051 splay_tree_node max_node;
4053 /* Get the node in OFFSETS that indicates the maximum offset where
4054 an empty subobject is located. */
4055 max_node = splay_tree_max (offsets);
4056 /* If there aren't any empty subobjects, then there's no point in
4057 performing this check. */
4058 if (!max_node)
4059 return 0;
4061 return walk_subobject_offsets (type, check_subobject_offset, offset,
4062 offsets, (tree) (max_node->key),
4063 vbases_p);
4066 /* DECL is a FIELD_DECL corresponding either to a base subobject of a
4067 non-static data member of the type indicated by RLI. BINFO is the
4068 binfo corresponding to the base subobject, OFFSETS maps offsets to
4069 types already located at those offsets. This function determines
4070 the position of the DECL. */
4072 static void
4073 layout_nonempty_base_or_field (record_layout_info rli,
4074 tree decl,
4075 tree binfo,
4076 splay_tree offsets)
4078 tree offset = NULL_TREE;
4079 bool field_p;
4080 tree type;
4082 if (binfo)
4084 /* For the purposes of determining layout conflicts, we want to
4085 use the class type of BINFO; TREE_TYPE (DECL) will be the
4086 CLASSTYPE_AS_BASE version, which does not contain entries for
4087 zero-sized bases. */
4088 type = TREE_TYPE (binfo);
4089 field_p = false;
4091 else
4093 type = TREE_TYPE (decl);
4094 field_p = true;
4097 /* Try to place the field. It may take more than one try if we have
4098 a hard time placing the field without putting two objects of the
4099 same type at the same address. */
4100 while (1)
4102 struct record_layout_info_s old_rli = *rli;
4104 /* Place this field. */
4105 place_field (rli, decl);
4106 offset = byte_position (decl);
4108 /* We have to check to see whether or not there is already
4109 something of the same type at the offset we're about to use.
4110 For example, consider:
4112 struct S {};
4113 struct T : public S { int i; };
4114 struct U : public S, public T {};
4116 Here, we put S at offset zero in U. Then, we can't put T at
4117 offset zero -- its S component would be at the same address
4118 as the S we already allocated. So, we have to skip ahead.
4119 Since all data members, including those whose type is an
4120 empty class, have nonzero size, any overlap can happen only
4121 with a direct or indirect base-class -- it can't happen with
4122 a data member. */
4123 /* In a union, overlap is permitted; all members are placed at
4124 offset zero. */
4125 if (TREE_CODE (rli->t) == UNION_TYPE)
4126 break;
4127 /* G++ 3.2 did not check for overlaps when placing a non-empty
4128 virtual base. */
4129 if (!abi_version_at_least (2) && binfo && BINFO_VIRTUAL_P (binfo))
4130 break;
4131 if (layout_conflict_p (field_p ? type : binfo, offset,
4132 offsets, field_p))
4134 /* Strip off the size allocated to this field. That puts us
4135 at the first place we could have put the field with
4136 proper alignment. */
4137 *rli = old_rli;
4139 /* Bump up by the alignment required for the type. */
4140 rli->bitpos
4141 = size_binop (PLUS_EXPR, rli->bitpos,
4142 bitsize_int (binfo
4143 ? CLASSTYPE_ALIGN (type)
4144 : TYPE_ALIGN (type)));
4145 normalize_rli (rli);
4147 else
4148 /* There was no conflict. We're done laying out this field. */
4149 break;
4152 /* Now that we know where it will be placed, update its
4153 BINFO_OFFSET. */
4154 if (binfo && CLASS_TYPE_P (BINFO_TYPE (binfo)))
4155 /* Indirect virtual bases may have a nonzero BINFO_OFFSET at
4156 this point because their BINFO_OFFSET is copied from another
4157 hierarchy. Therefore, we may not need to add the entire
4158 OFFSET. */
4159 propagate_binfo_offsets (binfo,
4160 size_diffop_loc (input_location,
4161 convert (ssizetype, offset),
4162 convert (ssizetype,
4163 BINFO_OFFSET (binfo))));
4166 /* Returns true if TYPE is empty and OFFSET is nonzero. */
4168 static int
4169 empty_base_at_nonzero_offset_p (tree type,
4170 tree offset,
4171 splay_tree /*offsets*/)
4173 return is_empty_class (type) && !integer_zerop (offset);
4176 /* Layout the empty base BINFO. EOC indicates the byte currently just
4177 past the end of the class, and should be correctly aligned for a
4178 class of the type indicated by BINFO; OFFSETS gives the offsets of
4179 the empty bases allocated so far. T is the most derived
4180 type. Return nonzero iff we added it at the end. */
4182 static bool
4183 layout_empty_base (record_layout_info rli, tree binfo,
4184 tree eoc, splay_tree offsets)
4186 tree alignment;
4187 tree basetype = BINFO_TYPE (binfo);
4188 bool atend = false;
4190 /* This routine should only be used for empty classes. */
4191 gcc_assert (is_empty_class (basetype));
4192 alignment = ssize_int (CLASSTYPE_ALIGN_UNIT (basetype));
4194 if (!integer_zerop (BINFO_OFFSET (binfo)))
4196 if (abi_version_at_least (2))
4197 propagate_binfo_offsets
4198 (binfo, size_diffop_loc (input_location,
4199 size_zero_node, BINFO_OFFSET (binfo)));
4200 else
4201 warning (OPT_Wabi,
4202 "offset of empty base %qT may not be ABI-compliant and may"
4203 "change in a future version of GCC",
4204 BINFO_TYPE (binfo));
4207 /* This is an empty base class. We first try to put it at offset
4208 zero. */
4209 if (layout_conflict_p (binfo,
4210 BINFO_OFFSET (binfo),
4211 offsets,
4212 /*vbases_p=*/0))
4214 /* That didn't work. Now, we move forward from the next
4215 available spot in the class. */
4216 atend = true;
4217 propagate_binfo_offsets (binfo, convert (ssizetype, eoc));
4218 while (1)
4220 if (!layout_conflict_p (binfo,
4221 BINFO_OFFSET (binfo),
4222 offsets,
4223 /*vbases_p=*/0))
4224 /* We finally found a spot where there's no overlap. */
4225 break;
4227 /* There's overlap here, too. Bump along to the next spot. */
4228 propagate_binfo_offsets (binfo, alignment);
4232 if (CLASSTYPE_USER_ALIGN (basetype))
4234 rli->record_align = MAX (rli->record_align, CLASSTYPE_ALIGN (basetype));
4235 if (warn_packed)
4236 rli->unpacked_align = MAX (rli->unpacked_align, CLASSTYPE_ALIGN (basetype));
4237 TYPE_USER_ALIGN (rli->t) = 1;
4240 return atend;
4243 /* Layout the base given by BINFO in the class indicated by RLI.
4244 *BASE_ALIGN is a running maximum of the alignments of
4245 any base class. OFFSETS gives the location of empty base
4246 subobjects. T is the most derived type. Return nonzero if the new
4247 object cannot be nearly-empty. A new FIELD_DECL is inserted at
4248 *NEXT_FIELD, unless BINFO is for an empty base class.
4250 Returns the location at which the next field should be inserted. */
4252 static tree *
4253 build_base_field (record_layout_info rli, tree binfo,
4254 splay_tree offsets, tree *next_field)
4256 tree t = rli->t;
4257 tree basetype = BINFO_TYPE (binfo);
4259 if (!COMPLETE_TYPE_P (basetype))
4260 /* This error is now reported in xref_tag, thus giving better
4261 location information. */
4262 return next_field;
4264 /* Place the base class. */
4265 if (!is_empty_class (basetype))
4267 tree decl;
4269 /* The containing class is non-empty because it has a non-empty
4270 base class. */
4271 CLASSTYPE_EMPTY_P (t) = 0;
4273 /* Create the FIELD_DECL. */
4274 decl = build_decl (input_location,
4275 FIELD_DECL, NULL_TREE, CLASSTYPE_AS_BASE (basetype));
4276 DECL_ARTIFICIAL (decl) = 1;
4277 DECL_IGNORED_P (decl) = 1;
4278 DECL_FIELD_CONTEXT (decl) = t;
4279 if (CLASSTYPE_AS_BASE (basetype))
4281 DECL_SIZE (decl) = CLASSTYPE_SIZE (basetype);
4282 DECL_SIZE_UNIT (decl) = CLASSTYPE_SIZE_UNIT (basetype);
4283 DECL_ALIGN (decl) = CLASSTYPE_ALIGN (basetype);
4284 DECL_USER_ALIGN (decl) = CLASSTYPE_USER_ALIGN (basetype);
4285 DECL_MODE (decl) = TYPE_MODE (basetype);
4286 DECL_FIELD_IS_BASE (decl) = 1;
4288 /* Try to place the field. It may take more than one try if we
4289 have a hard time placing the field without putting two
4290 objects of the same type at the same address. */
4291 layout_nonempty_base_or_field (rli, decl, binfo, offsets);
4292 /* Add the new FIELD_DECL to the list of fields for T. */
4293 DECL_CHAIN (decl) = *next_field;
4294 *next_field = decl;
4295 next_field = &DECL_CHAIN (decl);
4298 else
4300 tree eoc;
4301 bool atend;
4303 /* On some platforms (ARM), even empty classes will not be
4304 byte-aligned. */
4305 eoc = round_up_loc (input_location,
4306 rli_size_unit_so_far (rli),
4307 CLASSTYPE_ALIGN_UNIT (basetype));
4308 atend = layout_empty_base (rli, binfo, eoc, offsets);
4309 /* A nearly-empty class "has no proper base class that is empty,
4310 not morally virtual, and at an offset other than zero." */
4311 if (!BINFO_VIRTUAL_P (binfo) && CLASSTYPE_NEARLY_EMPTY_P (t))
4313 if (atend)
4314 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
4315 /* The check above (used in G++ 3.2) is insufficient because
4316 an empty class placed at offset zero might itself have an
4317 empty base at a nonzero offset. */
4318 else if (walk_subobject_offsets (basetype,
4319 empty_base_at_nonzero_offset_p,
4320 size_zero_node,
4321 /*offsets=*/NULL,
4322 /*max_offset=*/NULL_TREE,
4323 /*vbases_p=*/true))
4325 if (abi_version_at_least (2))
4326 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
4327 else
4328 warning (OPT_Wabi,
4329 "class %qT will be considered nearly empty in a "
4330 "future version of GCC", t);
4334 /* We do not create a FIELD_DECL for empty base classes because
4335 it might overlap some other field. We want to be able to
4336 create CONSTRUCTORs for the class by iterating over the
4337 FIELD_DECLs, and the back end does not handle overlapping
4338 FIELD_DECLs. */
4340 /* An empty virtual base causes a class to be non-empty
4341 -- but in that case we do not need to clear CLASSTYPE_EMPTY_P
4342 here because that was already done when the virtual table
4343 pointer was created. */
4346 /* Record the offsets of BINFO and its base subobjects. */
4347 record_subobject_offsets (binfo,
4348 BINFO_OFFSET (binfo),
4349 offsets,
4350 /*is_data_member=*/false);
4352 return next_field;
4355 /* Layout all of the non-virtual base classes. Record empty
4356 subobjects in OFFSETS. T is the most derived type. Return nonzero
4357 if the type cannot be nearly empty. The fields created
4358 corresponding to the base classes will be inserted at
4359 *NEXT_FIELD. */
4361 static void
4362 build_base_fields (record_layout_info rli,
4363 splay_tree offsets, tree *next_field)
4365 /* Chain to hold all the new FIELD_DECLs which stand in for base class
4366 subobjects. */
4367 tree t = rli->t;
4368 int n_baseclasses = BINFO_N_BASE_BINFOS (TYPE_BINFO (t));
4369 int i;
4371 /* The primary base class is always allocated first. */
4372 if (CLASSTYPE_HAS_PRIMARY_BASE_P (t))
4373 next_field = build_base_field (rli, CLASSTYPE_PRIMARY_BINFO (t),
4374 offsets, next_field);
4376 /* Now allocate the rest of the bases. */
4377 for (i = 0; i < n_baseclasses; ++i)
4379 tree base_binfo;
4381 base_binfo = BINFO_BASE_BINFO (TYPE_BINFO (t), i);
4383 /* The primary base was already allocated above, so we don't
4384 need to allocate it again here. */
4385 if (base_binfo == CLASSTYPE_PRIMARY_BINFO (t))
4386 continue;
4388 /* Virtual bases are added at the end (a primary virtual base
4389 will have already been added). */
4390 if (BINFO_VIRTUAL_P (base_binfo))
4391 continue;
4393 next_field = build_base_field (rli, base_binfo,
4394 offsets, next_field);
4398 /* Go through the TYPE_METHODS of T issuing any appropriate
4399 diagnostics, figuring out which methods override which other
4400 methods, and so forth. */
4402 static void
4403 check_methods (tree t)
4405 tree x;
4407 for (x = TYPE_METHODS (t); x; x = DECL_CHAIN (x))
4409 check_for_override (x, t);
4410 if (DECL_PURE_VIRTUAL_P (x) && ! DECL_VINDEX (x))
4411 error ("initializer specified for non-virtual method %q+D", x);
4412 /* The name of the field is the original field name
4413 Save this in auxiliary field for later overloading. */
4414 if (DECL_VINDEX (x))
4416 TYPE_POLYMORPHIC_P (t) = 1;
4417 if (DECL_PURE_VIRTUAL_P (x))
4418 vec_safe_push (CLASSTYPE_PURE_VIRTUALS (t), x);
4420 /* All user-provided destructors are non-trivial.
4421 Constructors and assignment ops are handled in
4422 grok_special_member_properties. */
4423 if (DECL_DESTRUCTOR_P (x) && user_provided_p (x))
4424 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t) = 1;
4428 /* FN is a constructor or destructor. Clone the declaration to create
4429 a specialized in-charge or not-in-charge version, as indicated by
4430 NAME. */
4432 static tree
4433 build_clone (tree fn, tree name)
4435 tree parms;
4436 tree clone;
4438 /* Copy the function. */
4439 clone = copy_decl (fn);
4440 /* Reset the function name. */
4441 DECL_NAME (clone) = name;
4442 SET_DECL_ASSEMBLER_NAME (clone, NULL_TREE);
4443 /* Remember where this function came from. */
4444 DECL_ABSTRACT_ORIGIN (clone) = fn;
4445 /* Make it easy to find the CLONE given the FN. */
4446 DECL_CHAIN (clone) = DECL_CHAIN (fn);
4447 DECL_CHAIN (fn) = clone;
4449 /* If this is a template, do the rest on the DECL_TEMPLATE_RESULT. */
4450 if (TREE_CODE (clone) == TEMPLATE_DECL)
4452 tree result = build_clone (DECL_TEMPLATE_RESULT (clone), name);
4453 DECL_TEMPLATE_RESULT (clone) = result;
4454 DECL_TEMPLATE_INFO (result) = copy_node (DECL_TEMPLATE_INFO (result));
4455 DECL_TI_TEMPLATE (result) = clone;
4456 TREE_TYPE (clone) = TREE_TYPE (result);
4457 return clone;
4460 DECL_CLONED_FUNCTION (clone) = fn;
4461 /* There's no pending inline data for this function. */
4462 DECL_PENDING_INLINE_INFO (clone) = NULL;
4463 DECL_PENDING_INLINE_P (clone) = 0;
4465 /* The base-class destructor is not virtual. */
4466 if (name == base_dtor_identifier)
4468 DECL_VIRTUAL_P (clone) = 0;
4469 if (TREE_CODE (clone) != TEMPLATE_DECL)
4470 DECL_VINDEX (clone) = NULL_TREE;
4473 /* If there was an in-charge parameter, drop it from the function
4474 type. */
4475 if (DECL_HAS_IN_CHARGE_PARM_P (clone))
4477 tree basetype;
4478 tree parmtypes;
4479 tree exceptions;
4481 exceptions = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (clone));
4482 basetype = TYPE_METHOD_BASETYPE (TREE_TYPE (clone));
4483 parmtypes = TYPE_ARG_TYPES (TREE_TYPE (clone));
4484 /* Skip the `this' parameter. */
4485 parmtypes = TREE_CHAIN (parmtypes);
4486 /* Skip the in-charge parameter. */
4487 parmtypes = TREE_CHAIN (parmtypes);
4488 /* And the VTT parm, in a complete [cd]tor. */
4489 if (DECL_HAS_VTT_PARM_P (fn)
4490 && ! DECL_NEEDS_VTT_PARM_P (clone))
4491 parmtypes = TREE_CHAIN (parmtypes);
4492 /* If this is subobject constructor or destructor, add the vtt
4493 parameter. */
4494 TREE_TYPE (clone)
4495 = build_method_type_directly (basetype,
4496 TREE_TYPE (TREE_TYPE (clone)),
4497 parmtypes);
4498 if (exceptions)
4499 TREE_TYPE (clone) = build_exception_variant (TREE_TYPE (clone),
4500 exceptions);
4501 TREE_TYPE (clone)
4502 = cp_build_type_attribute_variant (TREE_TYPE (clone),
4503 TYPE_ATTRIBUTES (TREE_TYPE (fn)));
4506 /* Copy the function parameters. */
4507 DECL_ARGUMENTS (clone) = copy_list (DECL_ARGUMENTS (clone));
4508 /* Remove the in-charge parameter. */
4509 if (DECL_HAS_IN_CHARGE_PARM_P (clone))
4511 DECL_CHAIN (DECL_ARGUMENTS (clone))
4512 = DECL_CHAIN (DECL_CHAIN (DECL_ARGUMENTS (clone)));
4513 DECL_HAS_IN_CHARGE_PARM_P (clone) = 0;
4515 /* And the VTT parm, in a complete [cd]tor. */
4516 if (DECL_HAS_VTT_PARM_P (fn))
4518 if (DECL_NEEDS_VTT_PARM_P (clone))
4519 DECL_HAS_VTT_PARM_P (clone) = 1;
4520 else
4522 DECL_CHAIN (DECL_ARGUMENTS (clone))
4523 = DECL_CHAIN (DECL_CHAIN (DECL_ARGUMENTS (clone)));
4524 DECL_HAS_VTT_PARM_P (clone) = 0;
4528 for (parms = DECL_ARGUMENTS (clone); parms; parms = DECL_CHAIN (parms))
4530 DECL_CONTEXT (parms) = clone;
4531 cxx_dup_lang_specific_decl (parms);
4534 /* Create the RTL for this function. */
4535 SET_DECL_RTL (clone, NULL);
4536 rest_of_decl_compilation (clone, /*top_level=*/1, at_eof);
4538 if (pch_file)
4539 note_decl_for_pch (clone);
4541 return clone;
4544 /* Implementation of DECL_CLONED_FUNCTION and DECL_CLONED_FUNCTION_P, do
4545 not invoke this function directly.
4547 For a non-thunk function, returns the address of the slot for storing
4548 the function it is a clone of. Otherwise returns NULL_TREE.
4550 If JUST_TESTING, looks through TEMPLATE_DECL and returns NULL if
4551 cloned_function is unset. This is to support the separate
4552 DECL_CLONED_FUNCTION and DECL_CLONED_FUNCTION_P modes; using the latter
4553 on a template makes sense, but not the former. */
4555 tree *
4556 decl_cloned_function_p (const_tree decl, bool just_testing)
4558 tree *ptr;
4559 if (just_testing)
4560 decl = STRIP_TEMPLATE (decl);
4562 if (TREE_CODE (decl) != FUNCTION_DECL
4563 || !DECL_LANG_SPECIFIC (decl)
4564 || DECL_LANG_SPECIFIC (decl)->u.fn.thunk_p)
4566 #if defined ENABLE_TREE_CHECKING && (GCC_VERSION >= 2007)
4567 if (!just_testing)
4568 lang_check_failed (__FILE__, __LINE__, __FUNCTION__);
4569 else
4570 #endif
4571 return NULL;
4574 ptr = &DECL_LANG_SPECIFIC (decl)->u.fn.u5.cloned_function;
4575 if (just_testing && *ptr == NULL_TREE)
4576 return NULL;
4577 else
4578 return ptr;
4581 /* Produce declarations for all appropriate clones of FN. If
4582 UPDATE_METHOD_VEC_P is nonzero, the clones are added to the
4583 CLASTYPE_METHOD_VEC as well. */
4585 void
4586 clone_function_decl (tree fn, int update_method_vec_p)
4588 tree clone;
4590 /* Avoid inappropriate cloning. */
4591 if (DECL_CHAIN (fn)
4592 && DECL_CLONED_FUNCTION_P (DECL_CHAIN (fn)))
4593 return;
4595 if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (fn))
4597 /* For each constructor, we need two variants: an in-charge version
4598 and a not-in-charge version. */
4599 clone = build_clone (fn, complete_ctor_identifier);
4600 if (update_method_vec_p)
4601 add_method (DECL_CONTEXT (clone), clone, NULL_TREE);
4602 clone = build_clone (fn, base_ctor_identifier);
4603 if (update_method_vec_p)
4604 add_method (DECL_CONTEXT (clone), clone, NULL_TREE);
4606 else
4608 gcc_assert (DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (fn));
4610 /* For each destructor, we need three variants: an in-charge
4611 version, a not-in-charge version, and an in-charge deleting
4612 version. We clone the deleting version first because that
4613 means it will go second on the TYPE_METHODS list -- and that
4614 corresponds to the correct layout order in the virtual
4615 function table.
4617 For a non-virtual destructor, we do not build a deleting
4618 destructor. */
4619 if (DECL_VIRTUAL_P (fn))
4621 clone = build_clone (fn, deleting_dtor_identifier);
4622 if (update_method_vec_p)
4623 add_method (DECL_CONTEXT (clone), clone, NULL_TREE);
4625 clone = build_clone (fn, complete_dtor_identifier);
4626 if (update_method_vec_p)
4627 add_method (DECL_CONTEXT (clone), clone, NULL_TREE);
4628 clone = build_clone (fn, base_dtor_identifier);
4629 if (update_method_vec_p)
4630 add_method (DECL_CONTEXT (clone), clone, NULL_TREE);
4633 /* Note that this is an abstract function that is never emitted. */
4634 DECL_ABSTRACT (fn) = 1;
4637 /* DECL is an in charge constructor, which is being defined. This will
4638 have had an in class declaration, from whence clones were
4639 declared. An out-of-class definition can specify additional default
4640 arguments. As it is the clones that are involved in overload
4641 resolution, we must propagate the information from the DECL to its
4642 clones. */
4644 void
4645 adjust_clone_args (tree decl)
4647 tree clone;
4649 for (clone = DECL_CHAIN (decl); clone && DECL_CLONED_FUNCTION_P (clone);
4650 clone = DECL_CHAIN (clone))
4652 tree orig_clone_parms = TYPE_ARG_TYPES (TREE_TYPE (clone));
4653 tree orig_decl_parms = TYPE_ARG_TYPES (TREE_TYPE (decl));
4654 tree decl_parms, clone_parms;
4656 clone_parms = orig_clone_parms;
4658 /* Skip the 'this' parameter. */
4659 orig_clone_parms = TREE_CHAIN (orig_clone_parms);
4660 orig_decl_parms = TREE_CHAIN (orig_decl_parms);
4662 if (DECL_HAS_IN_CHARGE_PARM_P (decl))
4663 orig_decl_parms = TREE_CHAIN (orig_decl_parms);
4664 if (DECL_HAS_VTT_PARM_P (decl))
4665 orig_decl_parms = TREE_CHAIN (orig_decl_parms);
4667 clone_parms = orig_clone_parms;
4668 if (DECL_HAS_VTT_PARM_P (clone))
4669 clone_parms = TREE_CHAIN (clone_parms);
4671 for (decl_parms = orig_decl_parms; decl_parms;
4672 decl_parms = TREE_CHAIN (decl_parms),
4673 clone_parms = TREE_CHAIN (clone_parms))
4675 gcc_assert (same_type_p (TREE_TYPE (decl_parms),
4676 TREE_TYPE (clone_parms)));
4678 if (TREE_PURPOSE (decl_parms) && !TREE_PURPOSE (clone_parms))
4680 /* A default parameter has been added. Adjust the
4681 clone's parameters. */
4682 tree exceptions = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (clone));
4683 tree attrs = TYPE_ATTRIBUTES (TREE_TYPE (clone));
4684 tree basetype = TYPE_METHOD_BASETYPE (TREE_TYPE (clone));
4685 tree type;
4687 clone_parms = orig_decl_parms;
4689 if (DECL_HAS_VTT_PARM_P (clone))
4691 clone_parms = tree_cons (TREE_PURPOSE (orig_clone_parms),
4692 TREE_VALUE (orig_clone_parms),
4693 clone_parms);
4694 TREE_TYPE (clone_parms) = TREE_TYPE (orig_clone_parms);
4696 type = build_method_type_directly (basetype,
4697 TREE_TYPE (TREE_TYPE (clone)),
4698 clone_parms);
4699 if (exceptions)
4700 type = build_exception_variant (type, exceptions);
4701 if (attrs)
4702 type = cp_build_type_attribute_variant (type, attrs);
4703 TREE_TYPE (clone) = type;
4705 clone_parms = NULL_TREE;
4706 break;
4709 gcc_assert (!clone_parms);
4713 /* For each of the constructors and destructors in T, create an
4714 in-charge and not-in-charge variant. */
4716 static void
4717 clone_constructors_and_destructors (tree t)
4719 tree fns;
4721 /* If for some reason we don't have a CLASSTYPE_METHOD_VEC, we bail
4722 out now. */
4723 if (!CLASSTYPE_METHOD_VEC (t))
4724 return;
4726 for (fns = CLASSTYPE_CONSTRUCTORS (t); fns; fns = OVL_NEXT (fns))
4727 clone_function_decl (OVL_CURRENT (fns), /*update_method_vec_p=*/1);
4728 for (fns = CLASSTYPE_DESTRUCTORS (t); fns; fns = OVL_NEXT (fns))
4729 clone_function_decl (OVL_CURRENT (fns), /*update_method_vec_p=*/1);
4732 /* Deduce noexcept for a destructor DTOR. */
4734 void
4735 deduce_noexcept_on_destructor (tree dtor)
4737 if (!TYPE_RAISES_EXCEPTIONS (TREE_TYPE (dtor)))
4739 tree ctx = DECL_CONTEXT (dtor);
4740 tree implicit_fn = implicitly_declare_fn (sfk_destructor, ctx,
4741 /*const_p=*/false,
4742 NULL, NULL);
4743 tree eh_spec = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (implicit_fn));
4744 TREE_TYPE (dtor) = build_exception_variant (TREE_TYPE (dtor), eh_spec);
4748 /* For each destructor in T, deduce noexcept:
4750 12.4/3: A declaration of a destructor that does not have an
4751 exception-specification is implicitly considered to have the
4752 same exception-specification as an implicit declaration (15.4). */
4754 static void
4755 deduce_noexcept_on_destructors (tree t)
4757 /* If for some reason we don't have a CLASSTYPE_METHOD_VEC, we bail
4758 out now. */
4759 if (!CLASSTYPE_METHOD_VEC (t))
4760 return;
4762 for (tree fns = CLASSTYPE_DESTRUCTORS (t); fns; fns = OVL_NEXT (fns))
4763 deduce_noexcept_on_destructor (OVL_CURRENT (fns));
4766 /* Subroutine of set_one_vmethod_tm_attributes. Search base classes
4767 of TYPE for virtual functions which FNDECL overrides. Return a
4768 mask of the tm attributes found therein. */
4770 static int
4771 look_for_tm_attr_overrides (tree type, tree fndecl)
4773 tree binfo = TYPE_BINFO (type);
4774 tree base_binfo;
4775 int ix, found = 0;
4777 for (ix = 0; BINFO_BASE_ITERATE (binfo, ix, base_binfo); ++ix)
4779 tree o, basetype = BINFO_TYPE (base_binfo);
4781 if (!TYPE_POLYMORPHIC_P (basetype))
4782 continue;
4784 o = look_for_overrides_here (basetype, fndecl);
4785 if (o)
4786 found |= tm_attr_to_mask (find_tm_attribute
4787 (TYPE_ATTRIBUTES (TREE_TYPE (o))));
4788 else
4789 found |= look_for_tm_attr_overrides (basetype, fndecl);
4792 return found;
4795 /* Subroutine of set_method_tm_attributes. Handle the checks and
4796 inheritance for one virtual method FNDECL. */
4798 static void
4799 set_one_vmethod_tm_attributes (tree type, tree fndecl)
4801 tree tm_attr;
4802 int found, have;
4804 found = look_for_tm_attr_overrides (type, fndecl);
4806 /* If FNDECL doesn't actually override anything (i.e. T is the
4807 class that first declares FNDECL virtual), then we're done. */
4808 if (found == 0)
4809 return;
4811 tm_attr = find_tm_attribute (TYPE_ATTRIBUTES (TREE_TYPE (fndecl)));
4812 have = tm_attr_to_mask (tm_attr);
4814 /* Intel STM Language Extension 3.0, Section 4.2 table 4:
4815 tm_pure must match exactly, otherwise no weakening of
4816 tm_safe > tm_callable > nothing. */
4817 /* ??? The tm_pure attribute didn't make the transition to the
4818 multivendor language spec. */
4819 if (have == TM_ATTR_PURE)
4821 if (found != TM_ATTR_PURE)
4823 found &= -found;
4824 goto err_override;
4827 /* If the overridden function is tm_pure, then FNDECL must be. */
4828 else if (found == TM_ATTR_PURE && tm_attr)
4829 goto err_override;
4830 /* Look for base class combinations that cannot be satisfied. */
4831 else if (found != TM_ATTR_PURE && (found & TM_ATTR_PURE))
4833 found &= ~TM_ATTR_PURE;
4834 found &= -found;
4835 error_at (DECL_SOURCE_LOCATION (fndecl),
4836 "method overrides both %<transaction_pure%> and %qE methods",
4837 tm_mask_to_attr (found));
4839 /* If FNDECL did not declare an attribute, then inherit the most
4840 restrictive one. */
4841 else if (tm_attr == NULL)
4843 apply_tm_attr (fndecl, tm_mask_to_attr (found & -found));
4845 /* Otherwise validate that we're not weaker than a function
4846 that is being overridden. */
4847 else
4849 found &= -found;
4850 if (found <= TM_ATTR_CALLABLE && have > found)
4851 goto err_override;
4853 return;
4855 err_override:
4856 error_at (DECL_SOURCE_LOCATION (fndecl),
4857 "method declared %qE overriding %qE method",
4858 tm_attr, tm_mask_to_attr (found));
4861 /* For each of the methods in T, propagate a class-level tm attribute. */
4863 static void
4864 set_method_tm_attributes (tree t)
4866 tree class_tm_attr, fndecl;
4868 /* Don't bother collecting tm attributes if transactional memory
4869 support is not enabled. */
4870 if (!flag_tm)
4871 return;
4873 /* Process virtual methods first, as they inherit directly from the
4874 base virtual function and also require validation of new attributes. */
4875 if (TYPE_CONTAINS_VPTR_P (t))
4877 tree vchain;
4878 for (vchain = BINFO_VIRTUALS (TYPE_BINFO (t)); vchain;
4879 vchain = TREE_CHAIN (vchain))
4881 fndecl = BV_FN (vchain);
4882 if (DECL_THUNK_P (fndecl))
4883 fndecl = THUNK_TARGET (fndecl);
4884 set_one_vmethod_tm_attributes (t, fndecl);
4888 /* If the class doesn't have an attribute, nothing more to do. */
4889 class_tm_attr = find_tm_attribute (TYPE_ATTRIBUTES (t));
4890 if (class_tm_attr == NULL)
4891 return;
4893 /* Any method that does not yet have a tm attribute inherits
4894 the one from the class. */
4895 for (fndecl = TYPE_METHODS (t); fndecl; fndecl = TREE_CHAIN (fndecl))
4897 if (!find_tm_attribute (TYPE_ATTRIBUTES (TREE_TYPE (fndecl))))
4898 apply_tm_attr (fndecl, class_tm_attr);
4902 /* Returns true iff class T has a user-defined constructor other than
4903 the default constructor. */
4905 bool
4906 type_has_user_nondefault_constructor (tree t)
4908 tree fns;
4910 if (!TYPE_HAS_USER_CONSTRUCTOR (t))
4911 return false;
4913 for (fns = CLASSTYPE_CONSTRUCTORS (t); fns; fns = OVL_NEXT (fns))
4915 tree fn = OVL_CURRENT (fns);
4916 if (!DECL_ARTIFICIAL (fn)
4917 && (TREE_CODE (fn) == TEMPLATE_DECL
4918 || (skip_artificial_parms_for (fn, DECL_ARGUMENTS (fn))
4919 != NULL_TREE)))
4920 return true;
4923 return false;
4926 /* Returns the defaulted constructor if T has one. Otherwise, returns
4927 NULL_TREE. */
4929 tree
4930 in_class_defaulted_default_constructor (tree t)
4932 tree fns, args;
4934 if (!TYPE_HAS_USER_CONSTRUCTOR (t))
4935 return NULL_TREE;
4937 for (fns = CLASSTYPE_CONSTRUCTORS (t); fns; fns = OVL_NEXT (fns))
4939 tree fn = OVL_CURRENT (fns);
4941 if (DECL_DEFAULTED_IN_CLASS_P (fn))
4943 args = FUNCTION_FIRST_USER_PARMTYPE (fn);
4944 while (args && TREE_PURPOSE (args))
4945 args = TREE_CHAIN (args);
4946 if (!args || args == void_list_node)
4947 return fn;
4951 return NULL_TREE;
4954 /* Returns true iff FN is a user-provided function, i.e. user-declared
4955 and not defaulted at its first declaration; or explicit, private,
4956 protected, or non-const. */
4958 bool
4959 user_provided_p (tree fn)
4961 if (TREE_CODE (fn) == TEMPLATE_DECL)
4962 return true;
4963 else
4964 return (!DECL_ARTIFICIAL (fn)
4965 && !(DECL_INITIALIZED_IN_CLASS_P (fn)
4966 && (DECL_DEFAULTED_FN (fn) || DECL_DELETED_FN (fn))));
4969 /* Returns true iff class T has a user-provided constructor. */
4971 bool
4972 type_has_user_provided_constructor (tree t)
4974 tree fns;
4976 if (!CLASS_TYPE_P (t))
4977 return false;
4979 if (!TYPE_HAS_USER_CONSTRUCTOR (t))
4980 return false;
4982 /* This can happen in error cases; avoid crashing. */
4983 if (!CLASSTYPE_METHOD_VEC (t))
4984 return false;
4986 for (fns = CLASSTYPE_CONSTRUCTORS (t); fns; fns = OVL_NEXT (fns))
4987 if (user_provided_p (OVL_CURRENT (fns)))
4988 return true;
4990 return false;
4993 /* Returns true iff class T has a user-provided default constructor. */
4995 bool
4996 type_has_user_provided_default_constructor (tree t)
4998 tree fns;
5000 if (!TYPE_HAS_USER_CONSTRUCTOR (t))
5001 return false;
5003 for (fns = CLASSTYPE_CONSTRUCTORS (t); fns; fns = OVL_NEXT (fns))
5005 tree fn = OVL_CURRENT (fns);
5006 if (TREE_CODE (fn) == FUNCTION_DECL
5007 && user_provided_p (fn)
5008 && sufficient_parms_p (FUNCTION_FIRST_USER_PARMTYPE (fn)))
5009 return true;
5012 return false;
5015 /* TYPE is being used as a virtual base, and has a non-trivial move
5016 assignment. Return true if this is due to there being a user-provided
5017 move assignment in TYPE or one of its subobjects; if there isn't, then
5018 multiple move assignment can't cause any harm. */
5020 bool
5021 vbase_has_user_provided_move_assign (tree type)
5023 /* Does the type itself have a user-provided move assignment operator? */
5024 for (tree fns
5025 = lookup_fnfields_slot_nolazy (type, ansi_assopname (NOP_EXPR));
5026 fns; fns = OVL_NEXT (fns))
5028 tree fn = OVL_CURRENT (fns);
5029 if (move_fn_p (fn) && user_provided_p (fn))
5030 return true;
5033 /* Do any of its bases? */
5034 tree binfo = TYPE_BINFO (type);
5035 tree base_binfo;
5036 for (int i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
5037 if (vbase_has_user_provided_move_assign (BINFO_TYPE (base_binfo)))
5038 return true;
5040 /* Or non-static data members? */
5041 for (tree field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
5043 if (TREE_CODE (field) == FIELD_DECL
5044 && CLASS_TYPE_P (TREE_TYPE (field))
5045 && vbase_has_user_provided_move_assign (TREE_TYPE (field)))
5046 return true;
5049 /* Seems not. */
5050 return false;
5053 /* If default-initialization leaves part of TYPE uninitialized, returns
5054 a DECL for the field or TYPE itself (DR 253). */
5056 tree
5057 default_init_uninitialized_part (tree type)
5059 tree t, r, binfo;
5060 int i;
5062 type = strip_array_types (type);
5063 if (!CLASS_TYPE_P (type))
5064 return type;
5065 if (type_has_user_provided_default_constructor (type))
5066 return NULL_TREE;
5067 for (binfo = TYPE_BINFO (type), i = 0;
5068 BINFO_BASE_ITERATE (binfo, i, t); ++i)
5070 r = default_init_uninitialized_part (BINFO_TYPE (t));
5071 if (r)
5072 return r;
5074 for (t = TYPE_FIELDS (type); t; t = DECL_CHAIN (t))
5075 if (TREE_CODE (t) == FIELD_DECL
5076 && !DECL_ARTIFICIAL (t)
5077 && !DECL_INITIAL (t))
5079 r = default_init_uninitialized_part (TREE_TYPE (t));
5080 if (r)
5081 return DECL_P (r) ? r : t;
5084 return NULL_TREE;
5087 /* Returns true iff for class T, a trivial synthesized default constructor
5088 would be constexpr. */
5090 bool
5091 trivial_default_constructor_is_constexpr (tree t)
5093 /* A defaulted trivial default constructor is constexpr
5094 if there is nothing to initialize. */
5095 gcc_assert (!TYPE_HAS_COMPLEX_DFLT (t));
5096 return is_really_empty_class (t);
5099 /* Returns true iff class T has a constexpr default constructor. */
5101 bool
5102 type_has_constexpr_default_constructor (tree t)
5104 tree fns;
5106 if (!CLASS_TYPE_P (t))
5108 /* The caller should have stripped an enclosing array. */
5109 gcc_assert (TREE_CODE (t) != ARRAY_TYPE);
5110 return false;
5112 if (CLASSTYPE_LAZY_DEFAULT_CTOR (t))
5114 if (!TYPE_HAS_COMPLEX_DFLT (t))
5115 return trivial_default_constructor_is_constexpr (t);
5116 /* Non-trivial, we need to check subobject constructors. */
5117 lazily_declare_fn (sfk_constructor, t);
5119 fns = locate_ctor (t);
5120 return (fns && DECL_DECLARED_CONSTEXPR_P (fns));
5123 /* Returns true iff class TYPE has a virtual destructor. */
5125 bool
5126 type_has_virtual_destructor (tree type)
5128 tree dtor;
5130 if (!CLASS_TYPE_P (type))
5131 return false;
5133 gcc_assert (COMPLETE_TYPE_P (type));
5134 dtor = CLASSTYPE_DESTRUCTORS (type);
5135 return (dtor && DECL_VIRTUAL_P (dtor));
5138 /* Returns true iff class T has a move constructor. */
5140 bool
5141 type_has_move_constructor (tree t)
5143 tree fns;
5145 if (CLASSTYPE_LAZY_MOVE_CTOR (t))
5147 gcc_assert (COMPLETE_TYPE_P (t));
5148 lazily_declare_fn (sfk_move_constructor, t);
5151 if (!CLASSTYPE_METHOD_VEC (t))
5152 return false;
5154 for (fns = CLASSTYPE_CONSTRUCTORS (t); fns; fns = OVL_NEXT (fns))
5155 if (move_fn_p (OVL_CURRENT (fns)))
5156 return true;
5158 return false;
5161 /* Returns true iff class T has a move assignment operator. */
5163 bool
5164 type_has_move_assign (tree t)
5166 tree fns;
5168 if (CLASSTYPE_LAZY_MOVE_ASSIGN (t))
5170 gcc_assert (COMPLETE_TYPE_P (t));
5171 lazily_declare_fn (sfk_move_assignment, t);
5174 for (fns = lookup_fnfields_slot_nolazy (t, ansi_assopname (NOP_EXPR));
5175 fns; fns = OVL_NEXT (fns))
5176 if (move_fn_p (OVL_CURRENT (fns)))
5177 return true;
5179 return false;
5182 /* Returns true iff class T has a move constructor that was explicitly
5183 declared in the class body. Note that this is different from
5184 "user-provided", which doesn't include functions that are defaulted in
5185 the class. */
5187 bool
5188 type_has_user_declared_move_constructor (tree t)
5190 tree fns;
5192 if (CLASSTYPE_LAZY_MOVE_CTOR (t))
5193 return false;
5195 if (!CLASSTYPE_METHOD_VEC (t))
5196 return false;
5198 for (fns = CLASSTYPE_CONSTRUCTORS (t); fns; fns = OVL_NEXT (fns))
5200 tree fn = OVL_CURRENT (fns);
5201 if (move_fn_p (fn) && !DECL_ARTIFICIAL (fn))
5202 return true;
5205 return false;
5208 /* Returns true iff class T has a move assignment operator that was
5209 explicitly declared in the class body. */
5211 bool
5212 type_has_user_declared_move_assign (tree t)
5214 tree fns;
5216 if (CLASSTYPE_LAZY_MOVE_ASSIGN (t))
5217 return false;
5219 for (fns = lookup_fnfields_slot_nolazy (t, ansi_assopname (NOP_EXPR));
5220 fns; fns = OVL_NEXT (fns))
5222 tree fn = OVL_CURRENT (fns);
5223 if (move_fn_p (fn) && !DECL_ARTIFICIAL (fn))
5224 return true;
5227 return false;
5230 /* Nonzero if we need to build up a constructor call when initializing an
5231 object of this class, either because it has a user-declared constructor
5232 or because it doesn't have a default constructor (so we need to give an
5233 error if no initializer is provided). Use TYPE_NEEDS_CONSTRUCTING when
5234 what you care about is whether or not an object can be produced by a
5235 constructor (e.g. so we don't set TREE_READONLY on const variables of
5236 such type); use this function when what you care about is whether or not
5237 to try to call a constructor to create an object. The latter case is
5238 the former plus some cases of constructors that cannot be called. */
5240 bool
5241 type_build_ctor_call (tree t)
5243 tree inner;
5244 if (TYPE_NEEDS_CONSTRUCTING (t))
5245 return true;
5246 inner = strip_array_types (t);
5247 if (!CLASS_TYPE_P (inner) || ANON_AGGR_TYPE_P (inner))
5248 return false;
5249 if (!TYPE_HAS_DEFAULT_CONSTRUCTOR (inner))
5250 return true;
5251 if (cxx_dialect < cxx11)
5252 return false;
5253 /* A user-declared constructor might be private, and a constructor might
5254 be trivial but deleted. */
5255 for (tree fns = lookup_fnfields_slot (inner, complete_ctor_identifier);
5256 fns; fns = OVL_NEXT (fns))
5258 tree fn = OVL_CURRENT (fns);
5259 if (!DECL_ARTIFICIAL (fn)
5260 || DECL_DELETED_FN (fn))
5261 return true;
5263 return false;
5266 /* Like type_build_ctor_call, but for destructors. */
5268 bool
5269 type_build_dtor_call (tree t)
5271 tree inner;
5272 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t))
5273 return true;
5274 inner = strip_array_types (t);
5275 if (!CLASS_TYPE_P (inner) || ANON_AGGR_TYPE_P (inner)
5276 || !COMPLETE_TYPE_P (inner))
5277 return false;
5278 if (cxx_dialect < cxx11)
5279 return false;
5280 /* A user-declared destructor might be private, and a destructor might
5281 be trivial but deleted. */
5282 for (tree fns = lookup_fnfields_slot (inner, complete_dtor_identifier);
5283 fns; fns = OVL_NEXT (fns))
5285 tree fn = OVL_CURRENT (fns);
5286 if (!DECL_ARTIFICIAL (fn)
5287 || DECL_DELETED_FN (fn))
5288 return true;
5290 return false;
5293 /* Remove all zero-width bit-fields from T. */
5295 static void
5296 remove_zero_width_bit_fields (tree t)
5298 tree *fieldsp;
5300 fieldsp = &TYPE_FIELDS (t);
5301 while (*fieldsp)
5303 if (TREE_CODE (*fieldsp) == FIELD_DECL
5304 && DECL_C_BIT_FIELD (*fieldsp)
5305 /* We should not be confused by the fact that grokbitfield
5306 temporarily sets the width of the bit field into
5307 DECL_INITIAL (*fieldsp).
5308 check_bitfield_decl eventually sets DECL_SIZE (*fieldsp)
5309 to that width. */
5310 && integer_zerop (DECL_SIZE (*fieldsp)))
5311 *fieldsp = DECL_CHAIN (*fieldsp);
5312 else
5313 fieldsp = &DECL_CHAIN (*fieldsp);
5317 /* Returns TRUE iff we need a cookie when dynamically allocating an
5318 array whose elements have the indicated class TYPE. */
5320 static bool
5321 type_requires_array_cookie (tree type)
5323 tree fns;
5324 bool has_two_argument_delete_p = false;
5326 gcc_assert (CLASS_TYPE_P (type));
5328 /* If there's a non-trivial destructor, we need a cookie. In order
5329 to iterate through the array calling the destructor for each
5330 element, we'll have to know how many elements there are. */
5331 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
5332 return true;
5334 /* If the usual deallocation function is a two-argument whose second
5335 argument is of type `size_t', then we have to pass the size of
5336 the array to the deallocation function, so we will need to store
5337 a cookie. */
5338 fns = lookup_fnfields (TYPE_BINFO (type),
5339 ansi_opname (VEC_DELETE_EXPR),
5340 /*protect=*/0);
5341 /* If there are no `operator []' members, or the lookup is
5342 ambiguous, then we don't need a cookie. */
5343 if (!fns || fns == error_mark_node)
5344 return false;
5345 /* Loop through all of the functions. */
5346 for (fns = BASELINK_FUNCTIONS (fns); fns; fns = OVL_NEXT (fns))
5348 tree fn;
5349 tree second_parm;
5351 /* Select the current function. */
5352 fn = OVL_CURRENT (fns);
5353 /* See if this function is a one-argument delete function. If
5354 it is, then it will be the usual deallocation function. */
5355 second_parm = TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (fn)));
5356 if (second_parm == void_list_node)
5357 return false;
5358 /* Do not consider this function if its second argument is an
5359 ellipsis. */
5360 if (!second_parm)
5361 continue;
5362 /* Otherwise, if we have a two-argument function and the second
5363 argument is `size_t', it will be the usual deallocation
5364 function -- unless there is one-argument function, too. */
5365 if (TREE_CHAIN (second_parm) == void_list_node
5366 && same_type_p (TREE_VALUE (second_parm), size_type_node))
5367 has_two_argument_delete_p = true;
5370 return has_two_argument_delete_p;
5373 /* Finish computing the `literal type' property of class type T.
5375 At this point, we have already processed base classes and
5376 non-static data members. We need to check whether the copy
5377 constructor is trivial, the destructor is trivial, and there
5378 is a trivial default constructor or at least one constexpr
5379 constructor other than the copy constructor. */
5381 static void
5382 finalize_literal_type_property (tree t)
5384 tree fn;
5386 if (cxx_dialect < cxx11
5387 || TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t))
5388 CLASSTYPE_LITERAL_P (t) = false;
5389 else if (CLASSTYPE_LITERAL_P (t) && !TYPE_HAS_TRIVIAL_DFLT (t)
5390 && CLASSTYPE_NON_AGGREGATE (t)
5391 && !TYPE_HAS_CONSTEXPR_CTOR (t))
5392 CLASSTYPE_LITERAL_P (t) = false;
5394 if (!CLASSTYPE_LITERAL_P (t))
5395 for (fn = TYPE_METHODS (t); fn; fn = DECL_CHAIN (fn))
5396 if (DECL_DECLARED_CONSTEXPR_P (fn)
5397 && TREE_CODE (fn) != TEMPLATE_DECL
5398 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
5399 && !DECL_CONSTRUCTOR_P (fn))
5401 DECL_DECLARED_CONSTEXPR_P (fn) = false;
5402 if (!DECL_GENERATED_P (fn))
5404 error ("enclosing class of constexpr non-static member "
5405 "function %q+#D is not a literal type", fn);
5406 explain_non_literal_class (t);
5411 /* T is a non-literal type used in a context which requires a constant
5412 expression. Explain why it isn't literal. */
5414 void
5415 explain_non_literal_class (tree t)
5417 static struct pointer_set_t *diagnosed;
5419 if (!CLASS_TYPE_P (t))
5420 return;
5421 t = TYPE_MAIN_VARIANT (t);
5423 if (diagnosed == NULL)
5424 diagnosed = pointer_set_create ();
5425 if (pointer_set_insert (diagnosed, t) != 0)
5426 /* Already explained. */
5427 return;
5429 inform (0, "%q+T is not literal because:", t);
5430 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t))
5431 inform (0, " %q+T has a non-trivial destructor", t);
5432 else if (CLASSTYPE_NON_AGGREGATE (t)
5433 && !TYPE_HAS_TRIVIAL_DFLT (t)
5434 && !TYPE_HAS_CONSTEXPR_CTOR (t))
5436 inform (0, " %q+T is not an aggregate, does not have a trivial "
5437 "default constructor, and has no constexpr constructor that "
5438 "is not a copy or move constructor", t);
5439 if (TYPE_HAS_DEFAULT_CONSTRUCTOR (t)
5440 && !type_has_user_provided_default_constructor (t))
5442 /* Note that we can't simply call locate_ctor because when the
5443 constructor is deleted it just returns NULL_TREE. */
5444 tree fns;
5445 for (fns = CLASSTYPE_CONSTRUCTORS (t); fns; fns = OVL_NEXT (fns))
5447 tree fn = OVL_CURRENT (fns);
5448 tree parms = TYPE_ARG_TYPES (TREE_TYPE (fn));
5450 parms = skip_artificial_parms_for (fn, parms);
5452 if (sufficient_parms_p (parms))
5454 if (DECL_DELETED_FN (fn))
5455 maybe_explain_implicit_delete (fn);
5456 else
5457 explain_invalid_constexpr_fn (fn);
5458 break;
5463 else
5465 tree binfo, base_binfo, field; int i;
5466 for (binfo = TYPE_BINFO (t), i = 0;
5467 BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
5469 tree basetype = TREE_TYPE (base_binfo);
5470 if (!CLASSTYPE_LITERAL_P (basetype))
5472 inform (0, " base class %qT of %q+T is non-literal",
5473 basetype, t);
5474 explain_non_literal_class (basetype);
5475 return;
5478 for (field = TYPE_FIELDS (t); field; field = TREE_CHAIN (field))
5480 tree ftype;
5481 if (TREE_CODE (field) != FIELD_DECL)
5482 continue;
5483 ftype = TREE_TYPE (field);
5484 if (!literal_type_p (ftype))
5486 inform (0, " non-static data member %q+D has "
5487 "non-literal type", field);
5488 if (CLASS_TYPE_P (ftype))
5489 explain_non_literal_class (ftype);
5495 /* Check the validity of the bases and members declared in T. Add any
5496 implicitly-generated functions (like copy-constructors and
5497 assignment operators). Compute various flag bits (like
5498 CLASSTYPE_NON_LAYOUT_POD_T) for T. This routine works purely at the C++
5499 level: i.e., independently of the ABI in use. */
5501 static void
5502 check_bases_and_members (tree t)
5504 /* Nonzero if the implicitly generated copy constructor should take
5505 a non-const reference argument. */
5506 int cant_have_const_ctor;
5507 /* Nonzero if the implicitly generated assignment operator
5508 should take a non-const reference argument. */
5509 int no_const_asn_ref;
5510 tree access_decls;
5511 bool saved_complex_asn_ref;
5512 bool saved_nontrivial_dtor;
5513 tree fn;
5515 /* Pick up any abi_tags from our template arguments before checking. */
5516 inherit_targ_abi_tags (t);
5518 /* By default, we use const reference arguments and generate default
5519 constructors. */
5520 cant_have_const_ctor = 0;
5521 no_const_asn_ref = 0;
5523 /* Check all the base-classes. */
5524 check_bases (t, &cant_have_const_ctor,
5525 &no_const_asn_ref);
5527 /* Deduce noexcept on destructors. This needs to happen after we've set
5528 triviality flags appropriately for our bases. */
5529 if (cxx_dialect >= cxx11)
5530 deduce_noexcept_on_destructors (t);
5532 /* Check all the method declarations. */
5533 check_methods (t);
5535 /* Save the initial values of these flags which only indicate whether
5536 or not the class has user-provided functions. As we analyze the
5537 bases and members we can set these flags for other reasons. */
5538 saved_complex_asn_ref = TYPE_HAS_COMPLEX_COPY_ASSIGN (t);
5539 saved_nontrivial_dtor = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t);
5541 /* Check all the data member declarations. We cannot call
5542 check_field_decls until we have called check_bases check_methods,
5543 as check_field_decls depends on TYPE_HAS_NONTRIVIAL_DESTRUCTOR
5544 being set appropriately. */
5545 check_field_decls (t, &access_decls,
5546 &cant_have_const_ctor,
5547 &no_const_asn_ref);
5549 /* A nearly-empty class has to be vptr-containing; a nearly empty
5550 class contains just a vptr. */
5551 if (!TYPE_CONTAINS_VPTR_P (t))
5552 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
5554 /* Do some bookkeeping that will guide the generation of implicitly
5555 declared member functions. */
5556 TYPE_HAS_COMPLEX_COPY_CTOR (t) |= TYPE_CONTAINS_VPTR_P (t);
5557 TYPE_HAS_COMPLEX_MOVE_CTOR (t) |= TYPE_CONTAINS_VPTR_P (t);
5558 /* We need to call a constructor for this class if it has a
5559 user-provided constructor, or if the default constructor is going
5560 to initialize the vptr. (This is not an if-and-only-if;
5561 TYPE_NEEDS_CONSTRUCTING is set elsewhere if bases or members
5562 themselves need constructing.) */
5563 TYPE_NEEDS_CONSTRUCTING (t)
5564 |= (type_has_user_provided_constructor (t) || TYPE_CONTAINS_VPTR_P (t));
5565 /* [dcl.init.aggr]
5567 An aggregate is an array or a class with no user-provided
5568 constructors ... and no virtual functions.
5570 Again, other conditions for being an aggregate are checked
5571 elsewhere. */
5572 CLASSTYPE_NON_AGGREGATE (t)
5573 |= (type_has_user_provided_constructor (t) || TYPE_POLYMORPHIC_P (t));
5574 /* This is the C++98/03 definition of POD; it changed in C++0x, but we
5575 retain the old definition internally for ABI reasons. */
5576 CLASSTYPE_NON_LAYOUT_POD_P (t)
5577 |= (CLASSTYPE_NON_AGGREGATE (t)
5578 || saved_nontrivial_dtor || saved_complex_asn_ref);
5579 CLASSTYPE_NON_STD_LAYOUT (t) |= TYPE_CONTAINS_VPTR_P (t);
5580 TYPE_HAS_COMPLEX_COPY_ASSIGN (t) |= TYPE_CONTAINS_VPTR_P (t);
5581 TYPE_HAS_COMPLEX_MOVE_ASSIGN (t) |= TYPE_CONTAINS_VPTR_P (t);
5582 TYPE_HAS_COMPLEX_DFLT (t) |= TYPE_CONTAINS_VPTR_P (t);
5584 /* If the class has no user-declared constructor, but does have
5585 non-static const or reference data members that can never be
5586 initialized, issue a warning. */
5587 if (warn_uninitialized
5588 /* Classes with user-declared constructors are presumed to
5589 initialize these members. */
5590 && !TYPE_HAS_USER_CONSTRUCTOR (t)
5591 /* Aggregates can be initialized with brace-enclosed
5592 initializers. */
5593 && CLASSTYPE_NON_AGGREGATE (t))
5595 tree field;
5597 for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
5599 tree type;
5601 if (TREE_CODE (field) != FIELD_DECL
5602 || DECL_INITIAL (field) != NULL_TREE)
5603 continue;
5605 type = TREE_TYPE (field);
5606 if (TREE_CODE (type) == REFERENCE_TYPE)
5607 warning (OPT_Wuninitialized, "non-static reference %q+#D "
5608 "in class without a constructor", field);
5609 else if (CP_TYPE_CONST_P (type)
5610 && (!CLASS_TYPE_P (type)
5611 || !TYPE_HAS_DEFAULT_CONSTRUCTOR (type)))
5612 warning (OPT_Wuninitialized, "non-static const member %q+#D "
5613 "in class without a constructor", field);
5617 /* Synthesize any needed methods. */
5618 add_implicitly_declared_members (t, &access_decls,
5619 cant_have_const_ctor,
5620 no_const_asn_ref);
5622 /* Check defaulted declarations here so we have cant_have_const_ctor
5623 and don't need to worry about clones. */
5624 for (fn = TYPE_METHODS (t); fn; fn = DECL_CHAIN (fn))
5625 if (!DECL_ARTIFICIAL (fn) && DECL_DEFAULTED_IN_CLASS_P (fn))
5627 int copy = copy_fn_p (fn);
5628 if (copy > 0)
5630 bool imp_const_p
5631 = (DECL_CONSTRUCTOR_P (fn) ? !cant_have_const_ctor
5632 : !no_const_asn_ref);
5633 bool fn_const_p = (copy == 2);
5635 if (fn_const_p && !imp_const_p)
5636 /* If the function is defaulted outside the class, we just
5637 give the synthesis error. */
5638 error ("%q+D declared to take const reference, but implicit "
5639 "declaration would take non-const", fn);
5641 defaulted_late_check (fn);
5644 if (LAMBDA_TYPE_P (t))
5646 /* "The closure type associated with a lambda-expression has a deleted
5647 default constructor and a deleted copy assignment operator." */
5648 TYPE_NEEDS_CONSTRUCTING (t) = 1;
5649 TYPE_HAS_COMPLEX_DFLT (t) = 1;
5650 TYPE_HAS_COMPLEX_COPY_ASSIGN (t) = 1;
5651 CLASSTYPE_LAZY_MOVE_ASSIGN (t) = 0;
5653 /* "This class type is not an aggregate." */
5654 CLASSTYPE_NON_AGGREGATE (t) = 1;
5657 /* Compute the 'literal type' property before we
5658 do anything with non-static member functions. */
5659 finalize_literal_type_property (t);
5661 /* Create the in-charge and not-in-charge variants of constructors
5662 and destructors. */
5663 clone_constructors_and_destructors (t);
5665 /* Process the using-declarations. */
5666 for (; access_decls; access_decls = TREE_CHAIN (access_decls))
5667 handle_using_decl (TREE_VALUE (access_decls), t);
5669 /* Build and sort the CLASSTYPE_METHOD_VEC. */
5670 finish_struct_methods (t);
5672 /* Figure out whether or not we will need a cookie when dynamically
5673 allocating an array of this type. */
5674 TYPE_LANG_SPECIFIC (t)->u.c.vec_new_uses_cookie
5675 = type_requires_array_cookie (t);
5678 /* If T needs a pointer to its virtual function table, set TYPE_VFIELD
5679 accordingly. If a new vfield was created (because T doesn't have a
5680 primary base class), then the newly created field is returned. It
5681 is not added to the TYPE_FIELDS list; it is the caller's
5682 responsibility to do that. Accumulate declared virtual functions
5683 on VIRTUALS_P. */
5685 static tree
5686 create_vtable_ptr (tree t, tree* virtuals_p)
5688 tree fn;
5690 /* Collect the virtual functions declared in T. */
5691 for (fn = TYPE_METHODS (t); fn; fn = DECL_CHAIN (fn))
5692 if (DECL_VINDEX (fn) && !DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (fn)
5693 && TREE_CODE (DECL_VINDEX (fn)) != INTEGER_CST)
5695 tree new_virtual = make_node (TREE_LIST);
5697 BV_FN (new_virtual) = fn;
5698 BV_DELTA (new_virtual) = integer_zero_node;
5699 BV_VCALL_INDEX (new_virtual) = NULL_TREE;
5701 TREE_CHAIN (new_virtual) = *virtuals_p;
5702 *virtuals_p = new_virtual;
5705 /* If we couldn't find an appropriate base class, create a new field
5706 here. Even if there weren't any new virtual functions, we might need a
5707 new virtual function table if we're supposed to include vptrs in
5708 all classes that need them. */
5709 if (!TYPE_VFIELD (t) && (*virtuals_p || TYPE_CONTAINS_VPTR_P (t)))
5711 /* We build this decl with vtbl_ptr_type_node, which is a
5712 `vtable_entry_type*'. It might seem more precise to use
5713 `vtable_entry_type (*)[N]' where N is the number of virtual
5714 functions. However, that would require the vtable pointer in
5715 base classes to have a different type than the vtable pointer
5716 in derived classes. We could make that happen, but that
5717 still wouldn't solve all the problems. In particular, the
5718 type-based alias analysis code would decide that assignments
5719 to the base class vtable pointer can't alias assignments to
5720 the derived class vtable pointer, since they have different
5721 types. Thus, in a derived class destructor, where the base
5722 class constructor was inlined, we could generate bad code for
5723 setting up the vtable pointer.
5725 Therefore, we use one type for all vtable pointers. We still
5726 use a type-correct type; it's just doesn't indicate the array
5727 bounds. That's better than using `void*' or some such; it's
5728 cleaner, and it let's the alias analysis code know that these
5729 stores cannot alias stores to void*! */
5730 tree field;
5732 field = build_decl (input_location,
5733 FIELD_DECL, get_vfield_name (t), vtbl_ptr_type_node);
5734 DECL_VIRTUAL_P (field) = 1;
5735 DECL_ARTIFICIAL (field) = 1;
5736 DECL_FIELD_CONTEXT (field) = t;
5737 DECL_FCONTEXT (field) = t;
5738 if (TYPE_PACKED (t))
5739 DECL_PACKED (field) = 1;
5741 TYPE_VFIELD (t) = field;
5743 /* This class is non-empty. */
5744 CLASSTYPE_EMPTY_P (t) = 0;
5746 return field;
5749 return NULL_TREE;
5752 /* Add OFFSET to all base types of BINFO which is a base in the
5753 hierarchy dominated by T.
5755 OFFSET, which is a type offset, is number of bytes. */
5757 static void
5758 propagate_binfo_offsets (tree binfo, tree offset)
5760 int i;
5761 tree primary_binfo;
5762 tree base_binfo;
5764 /* Update BINFO's offset. */
5765 BINFO_OFFSET (binfo)
5766 = convert (sizetype,
5767 size_binop (PLUS_EXPR,
5768 convert (ssizetype, BINFO_OFFSET (binfo)),
5769 offset));
5771 /* Find the primary base class. */
5772 primary_binfo = get_primary_binfo (binfo);
5774 if (primary_binfo && BINFO_INHERITANCE_CHAIN (primary_binfo) == binfo)
5775 propagate_binfo_offsets (primary_binfo, offset);
5777 /* Scan all of the bases, pushing the BINFO_OFFSET adjust
5778 downwards. */
5779 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
5781 /* Don't do the primary base twice. */
5782 if (base_binfo == primary_binfo)
5783 continue;
5785 if (BINFO_VIRTUAL_P (base_binfo))
5786 continue;
5788 propagate_binfo_offsets (base_binfo, offset);
5792 /* Set BINFO_OFFSET for all of the virtual bases for RLI->T. Update
5793 TYPE_ALIGN and TYPE_SIZE for T. OFFSETS gives the location of
5794 empty subobjects of T. */
5796 static void
5797 layout_virtual_bases (record_layout_info rli, splay_tree offsets)
5799 tree vbase;
5800 tree t = rli->t;
5801 bool first_vbase = true;
5802 tree *next_field;
5804 if (BINFO_N_BASE_BINFOS (TYPE_BINFO (t)) == 0)
5805 return;
5807 if (!abi_version_at_least(2))
5809 /* In G++ 3.2, we incorrectly rounded the size before laying out
5810 the virtual bases. */
5811 finish_record_layout (rli, /*free_p=*/false);
5812 #ifdef STRUCTURE_SIZE_BOUNDARY
5813 /* Packed structures don't need to have minimum size. */
5814 if (! TYPE_PACKED (t))
5815 TYPE_ALIGN (t) = MAX (TYPE_ALIGN (t), (unsigned) STRUCTURE_SIZE_BOUNDARY);
5816 #endif
5817 rli->offset = TYPE_SIZE_UNIT (t);
5818 rli->bitpos = bitsize_zero_node;
5819 rli->record_align = TYPE_ALIGN (t);
5822 /* Find the last field. The artificial fields created for virtual
5823 bases will go after the last extant field to date. */
5824 next_field = &TYPE_FIELDS (t);
5825 while (*next_field)
5826 next_field = &DECL_CHAIN (*next_field);
5828 /* Go through the virtual bases, allocating space for each virtual
5829 base that is not already a primary base class. These are
5830 allocated in inheritance graph order. */
5831 for (vbase = TYPE_BINFO (t); vbase; vbase = TREE_CHAIN (vbase))
5833 if (!BINFO_VIRTUAL_P (vbase))
5834 continue;
5836 if (!BINFO_PRIMARY_P (vbase))
5838 tree basetype = TREE_TYPE (vbase);
5840 /* This virtual base is not a primary base of any class in the
5841 hierarchy, so we have to add space for it. */
5842 next_field = build_base_field (rli, vbase,
5843 offsets, next_field);
5845 /* If the first virtual base might have been placed at a
5846 lower address, had we started from CLASSTYPE_SIZE, rather
5847 than TYPE_SIZE, issue a warning. There can be both false
5848 positives and false negatives from this warning in rare
5849 cases; to deal with all the possibilities would probably
5850 require performing both layout algorithms and comparing
5851 the results which is not particularly tractable. */
5852 if (warn_abi
5853 && first_vbase
5854 && (tree_int_cst_lt
5855 (size_binop (CEIL_DIV_EXPR,
5856 round_up_loc (input_location,
5857 CLASSTYPE_SIZE (t),
5858 CLASSTYPE_ALIGN (basetype)),
5859 bitsize_unit_node),
5860 BINFO_OFFSET (vbase))))
5861 warning (OPT_Wabi,
5862 "offset of virtual base %qT is not ABI-compliant and "
5863 "may change in a future version of GCC",
5864 basetype);
5866 first_vbase = false;
5871 /* Returns the offset of the byte just past the end of the base class
5872 BINFO. */
5874 static tree
5875 end_of_base (tree binfo)
5877 tree size;
5879 if (!CLASSTYPE_AS_BASE (BINFO_TYPE (binfo)))
5880 size = TYPE_SIZE_UNIT (char_type_node);
5881 else if (is_empty_class (BINFO_TYPE (binfo)))
5882 /* An empty class has zero CLASSTYPE_SIZE_UNIT, but we need to
5883 allocate some space for it. It cannot have virtual bases, so
5884 TYPE_SIZE_UNIT is fine. */
5885 size = TYPE_SIZE_UNIT (BINFO_TYPE (binfo));
5886 else
5887 size = CLASSTYPE_SIZE_UNIT (BINFO_TYPE (binfo));
5889 return size_binop (PLUS_EXPR, BINFO_OFFSET (binfo), size);
5892 /* Returns the offset of the byte just past the end of the base class
5893 with the highest offset in T. If INCLUDE_VIRTUALS_P is zero, then
5894 only non-virtual bases are included. */
5896 static tree
5897 end_of_class (tree t, int include_virtuals_p)
5899 tree result = size_zero_node;
5900 vec<tree, va_gc> *vbases;
5901 tree binfo;
5902 tree base_binfo;
5903 tree offset;
5904 int i;
5906 for (binfo = TYPE_BINFO (t), i = 0;
5907 BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
5909 if (!include_virtuals_p
5910 && BINFO_VIRTUAL_P (base_binfo)
5911 && (!BINFO_PRIMARY_P (base_binfo)
5912 || BINFO_INHERITANCE_CHAIN (base_binfo) != TYPE_BINFO (t)))
5913 continue;
5915 offset = end_of_base (base_binfo);
5916 if (INT_CST_LT_UNSIGNED (result, offset))
5917 result = offset;
5920 /* G++ 3.2 did not check indirect virtual bases. */
5921 if (abi_version_at_least (2) && include_virtuals_p)
5922 for (vbases = CLASSTYPE_VBASECLASSES (t), i = 0;
5923 vec_safe_iterate (vbases, i, &base_binfo); i++)
5925 offset = end_of_base (base_binfo);
5926 if (INT_CST_LT_UNSIGNED (result, offset))
5927 result = offset;
5930 return result;
5933 /* Warn about bases of T that are inaccessible because they are
5934 ambiguous. For example:
5936 struct S {};
5937 struct T : public S {};
5938 struct U : public S, public T {};
5940 Here, `(S*) new U' is not allowed because there are two `S'
5941 subobjects of U. */
5943 static void
5944 warn_about_ambiguous_bases (tree t)
5946 int i;
5947 vec<tree, va_gc> *vbases;
5948 tree basetype;
5949 tree binfo;
5950 tree base_binfo;
5952 /* If there are no repeated bases, nothing can be ambiguous. */
5953 if (!CLASSTYPE_REPEATED_BASE_P (t))
5954 return;
5956 /* Check direct bases. */
5957 for (binfo = TYPE_BINFO (t), i = 0;
5958 BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
5960 basetype = BINFO_TYPE (base_binfo);
5962 if (!uniquely_derived_from_p (basetype, t))
5963 warning (0, "direct base %qT inaccessible in %qT due to ambiguity",
5964 basetype, t);
5967 /* Check for ambiguous virtual bases. */
5968 if (extra_warnings)
5969 for (vbases = CLASSTYPE_VBASECLASSES (t), i = 0;
5970 vec_safe_iterate (vbases, i, &binfo); i++)
5972 basetype = BINFO_TYPE (binfo);
5974 if (!uniquely_derived_from_p (basetype, t))
5975 warning (OPT_Wextra, "virtual base %qT inaccessible in %qT due "
5976 "to ambiguity", basetype, t);
5980 /* Compare two INTEGER_CSTs K1 and K2. */
5982 static int
5983 splay_tree_compare_integer_csts (splay_tree_key k1, splay_tree_key k2)
5985 return tree_int_cst_compare ((tree) k1, (tree) k2);
5988 /* Increase the size indicated in RLI to account for empty classes
5989 that are "off the end" of the class. */
5991 static void
5992 include_empty_classes (record_layout_info rli)
5994 tree eoc;
5995 tree rli_size;
5997 /* It might be the case that we grew the class to allocate a
5998 zero-sized base class. That won't be reflected in RLI, yet,
5999 because we are willing to overlay multiple bases at the same
6000 offset. However, now we need to make sure that RLI is big enough
6001 to reflect the entire class. */
6002 eoc = end_of_class (rli->t,
6003 CLASSTYPE_AS_BASE (rli->t) != NULL_TREE);
6004 rli_size = rli_size_unit_so_far (rli);
6005 if (TREE_CODE (rli_size) == INTEGER_CST
6006 && INT_CST_LT_UNSIGNED (rli_size, eoc))
6008 if (!abi_version_at_least (2))
6009 /* In version 1 of the ABI, the size of a class that ends with
6010 a bitfield was not rounded up to a whole multiple of a
6011 byte. Because rli_size_unit_so_far returns only the number
6012 of fully allocated bytes, any extra bits were not included
6013 in the size. */
6014 rli->bitpos = round_down (rli->bitpos, BITS_PER_UNIT);
6015 else
6016 /* The size should have been rounded to a whole byte. */
6017 gcc_assert (tree_int_cst_equal
6018 (rli->bitpos, round_down (rli->bitpos, BITS_PER_UNIT)));
6019 rli->bitpos
6020 = size_binop (PLUS_EXPR,
6021 rli->bitpos,
6022 size_binop (MULT_EXPR,
6023 convert (bitsizetype,
6024 size_binop (MINUS_EXPR,
6025 eoc, rli_size)),
6026 bitsize_int (BITS_PER_UNIT)));
6027 normalize_rli (rli);
6031 /* Calculate the TYPE_SIZE, TYPE_ALIGN, etc for T. Calculate
6032 BINFO_OFFSETs for all of the base-classes. Position the vtable
6033 pointer. Accumulate declared virtual functions on VIRTUALS_P. */
6035 static void
6036 layout_class_type (tree t, tree *virtuals_p)
6038 tree non_static_data_members;
6039 tree field;
6040 tree vptr;
6041 record_layout_info rli;
6042 /* Maps offsets (represented as INTEGER_CSTs) to a TREE_LIST of
6043 types that appear at that offset. */
6044 splay_tree empty_base_offsets;
6045 /* True if the last field laid out was a bit-field. */
6046 bool last_field_was_bitfield = false;
6047 /* The location at which the next field should be inserted. */
6048 tree *next_field;
6049 /* T, as a base class. */
6050 tree base_t;
6052 /* Keep track of the first non-static data member. */
6053 non_static_data_members = TYPE_FIELDS (t);
6055 /* Start laying out the record. */
6056 rli = start_record_layout (t);
6058 /* Mark all the primary bases in the hierarchy. */
6059 determine_primary_bases (t);
6061 /* Create a pointer to our virtual function table. */
6062 vptr = create_vtable_ptr (t, virtuals_p);
6064 /* The vptr is always the first thing in the class. */
6065 if (vptr)
6067 DECL_CHAIN (vptr) = TYPE_FIELDS (t);
6068 TYPE_FIELDS (t) = vptr;
6069 next_field = &DECL_CHAIN (vptr);
6070 place_field (rli, vptr);
6072 else
6073 next_field = &TYPE_FIELDS (t);
6075 /* Build FIELD_DECLs for all of the non-virtual base-types. */
6076 empty_base_offsets = splay_tree_new (splay_tree_compare_integer_csts,
6077 NULL, NULL);
6078 build_base_fields (rli, empty_base_offsets, next_field);
6080 /* Layout the non-static data members. */
6081 for (field = non_static_data_members; field; field = DECL_CHAIN (field))
6083 tree type;
6084 tree padding;
6086 /* We still pass things that aren't non-static data members to
6087 the back end, in case it wants to do something with them. */
6088 if (TREE_CODE (field) != FIELD_DECL)
6090 place_field (rli, field);
6091 /* If the static data member has incomplete type, keep track
6092 of it so that it can be completed later. (The handling
6093 of pending statics in finish_record_layout is
6094 insufficient; consider:
6096 struct S1;
6097 struct S2 { static S1 s1; };
6099 At this point, finish_record_layout will be called, but
6100 S1 is still incomplete.) */
6101 if (VAR_P (field))
6103 maybe_register_incomplete_var (field);
6104 /* The visibility of static data members is determined
6105 at their point of declaration, not their point of
6106 definition. */
6107 determine_visibility (field);
6109 continue;
6112 type = TREE_TYPE (field);
6113 if (type == error_mark_node)
6114 continue;
6116 padding = NULL_TREE;
6118 /* If this field is a bit-field whose width is greater than its
6119 type, then there are some special rules for allocating
6120 it. */
6121 if (DECL_C_BIT_FIELD (field)
6122 && INT_CST_LT (TYPE_SIZE (type), DECL_SIZE (field)))
6124 unsigned int itk;
6125 tree integer_type;
6126 bool was_unnamed_p = false;
6127 /* We must allocate the bits as if suitably aligned for the
6128 longest integer type that fits in this many bits. type
6129 of the field. Then, we are supposed to use the left over
6130 bits as additional padding. */
6131 for (itk = itk_char; itk != itk_none; ++itk)
6132 if (integer_types[itk] != NULL_TREE
6133 && (INT_CST_LT (size_int (MAX_FIXED_MODE_SIZE),
6134 TYPE_SIZE (integer_types[itk]))
6135 || INT_CST_LT (DECL_SIZE (field),
6136 TYPE_SIZE (integer_types[itk]))))
6137 break;
6139 /* ITK now indicates a type that is too large for the
6140 field. We have to back up by one to find the largest
6141 type that fits. */
6144 --itk;
6145 integer_type = integer_types[itk];
6146 } while (itk > 0 && integer_type == NULL_TREE);
6148 /* Figure out how much additional padding is required. GCC
6149 3.2 always created a padding field, even if it had zero
6150 width. */
6151 if (!abi_version_at_least (2)
6152 || INT_CST_LT (TYPE_SIZE (integer_type), DECL_SIZE (field)))
6154 if (abi_version_at_least (2) && TREE_CODE (t) == UNION_TYPE)
6155 /* In a union, the padding field must have the full width
6156 of the bit-field; all fields start at offset zero. */
6157 padding = DECL_SIZE (field);
6158 else
6160 if (TREE_CODE (t) == UNION_TYPE)
6161 warning (OPT_Wabi, "size assigned to %qT may not be "
6162 "ABI-compliant and may change in a future "
6163 "version of GCC",
6165 padding = size_binop (MINUS_EXPR, DECL_SIZE (field),
6166 TYPE_SIZE (integer_type));
6169 #ifdef PCC_BITFIELD_TYPE_MATTERS
6170 /* An unnamed bitfield does not normally affect the
6171 alignment of the containing class on a target where
6172 PCC_BITFIELD_TYPE_MATTERS. But, the C++ ABI does not
6173 make any exceptions for unnamed bitfields when the
6174 bitfields are longer than their types. Therefore, we
6175 temporarily give the field a name. */
6176 if (PCC_BITFIELD_TYPE_MATTERS && !DECL_NAME (field))
6178 was_unnamed_p = true;
6179 DECL_NAME (field) = make_anon_name ();
6181 #endif
6182 DECL_SIZE (field) = TYPE_SIZE (integer_type);
6183 DECL_ALIGN (field) = TYPE_ALIGN (integer_type);
6184 DECL_USER_ALIGN (field) = TYPE_USER_ALIGN (integer_type);
6185 layout_nonempty_base_or_field (rli, field, NULL_TREE,
6186 empty_base_offsets);
6187 if (was_unnamed_p)
6188 DECL_NAME (field) = NULL_TREE;
6189 /* Now that layout has been performed, set the size of the
6190 field to the size of its declared type; the rest of the
6191 field is effectively invisible. */
6192 DECL_SIZE (field) = TYPE_SIZE (type);
6193 /* We must also reset the DECL_MODE of the field. */
6194 if (abi_version_at_least (2))
6195 DECL_MODE (field) = TYPE_MODE (type);
6196 else if (warn_abi
6197 && DECL_MODE (field) != TYPE_MODE (type))
6198 /* Versions of G++ before G++ 3.4 did not reset the
6199 DECL_MODE. */
6200 warning (OPT_Wabi,
6201 "the offset of %qD may not be ABI-compliant and may "
6202 "change in a future version of GCC", field);
6204 else
6205 layout_nonempty_base_or_field (rli, field, NULL_TREE,
6206 empty_base_offsets);
6208 /* Remember the location of any empty classes in FIELD. */
6209 if (abi_version_at_least (2))
6210 record_subobject_offsets (TREE_TYPE (field),
6211 byte_position(field),
6212 empty_base_offsets,
6213 /*is_data_member=*/true);
6215 /* If a bit-field does not immediately follow another bit-field,
6216 and yet it starts in the middle of a byte, we have failed to
6217 comply with the ABI. */
6218 if (warn_abi
6219 && DECL_C_BIT_FIELD (field)
6220 /* The TREE_NO_WARNING flag gets set by Objective-C when
6221 laying out an Objective-C class. The ObjC ABI differs
6222 from the C++ ABI, and so we do not want a warning
6223 here. */
6224 && !TREE_NO_WARNING (field)
6225 && !last_field_was_bitfield
6226 && !integer_zerop (size_binop (TRUNC_MOD_EXPR,
6227 DECL_FIELD_BIT_OFFSET (field),
6228 bitsize_unit_node)))
6229 warning (OPT_Wabi, "offset of %q+D is not ABI-compliant and may "
6230 "change in a future version of GCC", field);
6232 /* G++ used to use DECL_FIELD_OFFSET as if it were the byte
6233 offset of the field. */
6234 if (warn_abi
6235 && !abi_version_at_least (2)
6236 && !tree_int_cst_equal (DECL_FIELD_OFFSET (field),
6237 byte_position (field))
6238 && contains_empty_class_p (TREE_TYPE (field)))
6239 warning (OPT_Wabi, "%q+D contains empty classes which may cause base "
6240 "classes to be placed at different locations in a "
6241 "future version of GCC", field);
6243 /* The middle end uses the type of expressions to determine the
6244 possible range of expression values. In order to optimize
6245 "x.i > 7" to "false" for a 2-bit bitfield "i", the middle end
6246 must be made aware of the width of "i", via its type.
6248 Because C++ does not have integer types of arbitrary width,
6249 we must (for the purposes of the front end) convert from the
6250 type assigned here to the declared type of the bitfield
6251 whenever a bitfield expression is used as an rvalue.
6252 Similarly, when assigning a value to a bitfield, the value
6253 must be converted to the type given the bitfield here. */
6254 if (DECL_C_BIT_FIELD (field))
6256 unsigned HOST_WIDE_INT width;
6257 tree ftype = TREE_TYPE (field);
6258 width = tree_to_uhwi (DECL_SIZE (field));
6259 if (width != TYPE_PRECISION (ftype))
6261 TREE_TYPE (field)
6262 = c_build_bitfield_integer_type (width,
6263 TYPE_UNSIGNED (ftype));
6264 TREE_TYPE (field)
6265 = cp_build_qualified_type (TREE_TYPE (field),
6266 cp_type_quals (ftype));
6270 /* If we needed additional padding after this field, add it
6271 now. */
6272 if (padding)
6274 tree padding_field;
6276 padding_field = build_decl (input_location,
6277 FIELD_DECL,
6278 NULL_TREE,
6279 char_type_node);
6280 DECL_BIT_FIELD (padding_field) = 1;
6281 DECL_SIZE (padding_field) = padding;
6282 DECL_CONTEXT (padding_field) = t;
6283 DECL_ARTIFICIAL (padding_field) = 1;
6284 DECL_IGNORED_P (padding_field) = 1;
6285 layout_nonempty_base_or_field (rli, padding_field,
6286 NULL_TREE,
6287 empty_base_offsets);
6290 last_field_was_bitfield = DECL_C_BIT_FIELD (field);
6293 if (abi_version_at_least (2) && !integer_zerop (rli->bitpos))
6295 /* Make sure that we are on a byte boundary so that the size of
6296 the class without virtual bases will always be a round number
6297 of bytes. */
6298 rli->bitpos = round_up_loc (input_location, rli->bitpos, BITS_PER_UNIT);
6299 normalize_rli (rli);
6302 /* G++ 3.2 does not allow virtual bases to be overlaid with tail
6303 padding. */
6304 if (!abi_version_at_least (2))
6305 include_empty_classes(rli);
6307 /* Delete all zero-width bit-fields from the list of fields. Now
6308 that the type is laid out they are no longer important. */
6309 remove_zero_width_bit_fields (t);
6311 /* Create the version of T used for virtual bases. We do not use
6312 make_class_type for this version; this is an artificial type. For
6313 a POD type, we just reuse T. */
6314 if (CLASSTYPE_NON_LAYOUT_POD_P (t) || CLASSTYPE_EMPTY_P (t))
6316 base_t = make_node (TREE_CODE (t));
6318 /* Set the size and alignment for the new type. In G++ 3.2, all
6319 empty classes were considered to have size zero when used as
6320 base classes. */
6321 if (!abi_version_at_least (2) && CLASSTYPE_EMPTY_P (t))
6323 TYPE_SIZE (base_t) = bitsize_zero_node;
6324 TYPE_SIZE_UNIT (base_t) = size_zero_node;
6325 if (warn_abi && !integer_zerop (rli_size_unit_so_far (rli)))
6326 warning (OPT_Wabi,
6327 "layout of classes derived from empty class %qT "
6328 "may change in a future version of GCC",
6331 else
6333 tree eoc;
6335 /* If the ABI version is not at least two, and the last
6336 field was a bit-field, RLI may not be on a byte
6337 boundary. In particular, rli_size_unit_so_far might
6338 indicate the last complete byte, while rli_size_so_far
6339 indicates the total number of bits used. Therefore,
6340 rli_size_so_far, rather than rli_size_unit_so_far, is
6341 used to compute TYPE_SIZE_UNIT. */
6342 eoc = end_of_class (t, /*include_virtuals_p=*/0);
6343 TYPE_SIZE_UNIT (base_t)
6344 = size_binop (MAX_EXPR,
6345 convert (sizetype,
6346 size_binop (CEIL_DIV_EXPR,
6347 rli_size_so_far (rli),
6348 bitsize_int (BITS_PER_UNIT))),
6349 eoc);
6350 TYPE_SIZE (base_t)
6351 = size_binop (MAX_EXPR,
6352 rli_size_so_far (rli),
6353 size_binop (MULT_EXPR,
6354 convert (bitsizetype, eoc),
6355 bitsize_int (BITS_PER_UNIT)));
6357 TYPE_ALIGN (base_t) = rli->record_align;
6358 TYPE_USER_ALIGN (base_t) = TYPE_USER_ALIGN (t);
6360 /* Copy the fields from T. */
6361 next_field = &TYPE_FIELDS (base_t);
6362 for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
6363 if (TREE_CODE (field) == FIELD_DECL)
6365 *next_field = build_decl (input_location,
6366 FIELD_DECL,
6367 DECL_NAME (field),
6368 TREE_TYPE (field));
6369 DECL_CONTEXT (*next_field) = base_t;
6370 DECL_FIELD_OFFSET (*next_field) = DECL_FIELD_OFFSET (field);
6371 DECL_FIELD_BIT_OFFSET (*next_field)
6372 = DECL_FIELD_BIT_OFFSET (field);
6373 DECL_SIZE (*next_field) = DECL_SIZE (field);
6374 DECL_MODE (*next_field) = DECL_MODE (field);
6375 next_field = &DECL_CHAIN (*next_field);
6378 /* Record the base version of the type. */
6379 CLASSTYPE_AS_BASE (t) = base_t;
6380 TYPE_CONTEXT (base_t) = t;
6382 else
6383 CLASSTYPE_AS_BASE (t) = t;
6385 /* Every empty class contains an empty class. */
6386 if (CLASSTYPE_EMPTY_P (t))
6387 CLASSTYPE_CONTAINS_EMPTY_CLASS_P (t) = 1;
6389 /* Set the TYPE_DECL for this type to contain the right
6390 value for DECL_OFFSET, so that we can use it as part
6391 of a COMPONENT_REF for multiple inheritance. */
6392 layout_decl (TYPE_MAIN_DECL (t), 0);
6394 /* Now fix up any virtual base class types that we left lying
6395 around. We must get these done before we try to lay out the
6396 virtual function table. As a side-effect, this will remove the
6397 base subobject fields. */
6398 layout_virtual_bases (rli, empty_base_offsets);
6400 /* Make sure that empty classes are reflected in RLI at this
6401 point. */
6402 include_empty_classes(rli);
6404 /* Make sure not to create any structures with zero size. */
6405 if (integer_zerop (rli_size_unit_so_far (rli)) && CLASSTYPE_EMPTY_P (t))
6406 place_field (rli,
6407 build_decl (input_location,
6408 FIELD_DECL, NULL_TREE, char_type_node));
6410 /* If this is a non-POD, declaring it packed makes a difference to how it
6411 can be used as a field; don't let finalize_record_size undo it. */
6412 if (TYPE_PACKED (t) && !layout_pod_type_p (t))
6413 rli->packed_maybe_necessary = true;
6415 /* Let the back end lay out the type. */
6416 finish_record_layout (rli, /*free_p=*/true);
6418 if (TYPE_SIZE_UNIT (t)
6419 && TREE_CODE (TYPE_SIZE_UNIT (t)) == INTEGER_CST
6420 && !TREE_OVERFLOW (TYPE_SIZE_UNIT (t))
6421 && !valid_constant_size_p (TYPE_SIZE_UNIT (t)))
6422 error ("type %qT is too large", t);
6424 /* Warn about bases that can't be talked about due to ambiguity. */
6425 warn_about_ambiguous_bases (t);
6427 /* Now that we're done with layout, give the base fields the real types. */
6428 for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
6429 if (DECL_ARTIFICIAL (field) && IS_FAKE_BASE_TYPE (TREE_TYPE (field)))
6430 TREE_TYPE (field) = TYPE_CONTEXT (TREE_TYPE (field));
6432 /* Clean up. */
6433 splay_tree_delete (empty_base_offsets);
6435 if (CLASSTYPE_EMPTY_P (t)
6436 && tree_int_cst_lt (sizeof_biggest_empty_class,
6437 TYPE_SIZE_UNIT (t)))
6438 sizeof_biggest_empty_class = TYPE_SIZE_UNIT (t);
6441 /* Determine the "key method" for the class type indicated by TYPE,
6442 and set CLASSTYPE_KEY_METHOD accordingly. */
6444 void
6445 determine_key_method (tree type)
6447 tree method;
6449 if (TYPE_FOR_JAVA (type)
6450 || processing_template_decl
6451 || CLASSTYPE_TEMPLATE_INSTANTIATION (type)
6452 || CLASSTYPE_INTERFACE_KNOWN (type))
6453 return;
6455 /* The key method is the first non-pure virtual function that is not
6456 inline at the point of class definition. On some targets the
6457 key function may not be inline; those targets should not call
6458 this function until the end of the translation unit. */
6459 for (method = TYPE_METHODS (type); method != NULL_TREE;
6460 method = DECL_CHAIN (method))
6461 if (DECL_VINDEX (method) != NULL_TREE
6462 && ! DECL_DECLARED_INLINE_P (method)
6463 && ! DECL_PURE_VIRTUAL_P (method))
6465 CLASSTYPE_KEY_METHOD (type) = method;
6466 break;
6469 return;
6473 /* Allocate and return an instance of struct sorted_fields_type with
6474 N fields. */
6476 static struct sorted_fields_type *
6477 sorted_fields_type_new (int n)
6479 struct sorted_fields_type *sft;
6480 sft = ggc_alloc_sorted_fields_type (sizeof (struct sorted_fields_type)
6481 + n * sizeof (tree));
6482 sft->len = n;
6484 return sft;
6488 /* Perform processing required when the definition of T (a class type)
6489 is complete. */
6491 void
6492 finish_struct_1 (tree t)
6494 tree x;
6495 /* A TREE_LIST. The TREE_VALUE of each node is a FUNCTION_DECL. */
6496 tree virtuals = NULL_TREE;
6498 if (COMPLETE_TYPE_P (t))
6500 gcc_assert (MAYBE_CLASS_TYPE_P (t));
6501 error ("redefinition of %q#T", t);
6502 popclass ();
6503 return;
6506 /* If this type was previously laid out as a forward reference,
6507 make sure we lay it out again. */
6508 TYPE_SIZE (t) = NULL_TREE;
6509 CLASSTYPE_PRIMARY_BINFO (t) = NULL_TREE;
6511 /* Make assumptions about the class; we'll reset the flags if
6512 necessary. */
6513 CLASSTYPE_EMPTY_P (t) = 1;
6514 CLASSTYPE_NEARLY_EMPTY_P (t) = 1;
6515 CLASSTYPE_CONTAINS_EMPTY_CLASS_P (t) = 0;
6516 CLASSTYPE_LITERAL_P (t) = true;
6518 /* Do end-of-class semantic processing: checking the validity of the
6519 bases and members and add implicitly generated methods. */
6520 check_bases_and_members (t);
6522 /* Find the key method. */
6523 if (TYPE_CONTAINS_VPTR_P (t))
6525 /* The Itanium C++ ABI permits the key method to be chosen when
6526 the class is defined -- even though the key method so
6527 selected may later turn out to be an inline function. On
6528 some systems (such as ARM Symbian OS) the key method cannot
6529 be determined until the end of the translation unit. On such
6530 systems, we leave CLASSTYPE_KEY_METHOD set to NULL, which
6531 will cause the class to be added to KEYED_CLASSES. Then, in
6532 finish_file we will determine the key method. */
6533 if (targetm.cxx.key_method_may_be_inline ())
6534 determine_key_method (t);
6536 /* If a polymorphic class has no key method, we may emit the vtable
6537 in every translation unit where the class definition appears. */
6538 if (CLASSTYPE_KEY_METHOD (t) == NULL_TREE)
6539 keyed_classes = tree_cons (NULL_TREE, t, keyed_classes);
6542 /* Layout the class itself. */
6543 layout_class_type (t, &virtuals);
6544 if (CLASSTYPE_AS_BASE (t) != t)
6545 /* We use the base type for trivial assignments, and hence it
6546 needs a mode. */
6547 compute_record_mode (CLASSTYPE_AS_BASE (t));
6549 virtuals = modify_all_vtables (t, nreverse (virtuals));
6551 /* If necessary, create the primary vtable for this class. */
6552 if (virtuals || TYPE_CONTAINS_VPTR_P (t))
6554 /* We must enter these virtuals into the table. */
6555 if (!CLASSTYPE_HAS_PRIMARY_BASE_P (t))
6556 build_primary_vtable (NULL_TREE, t);
6557 else if (! BINFO_NEW_VTABLE_MARKED (TYPE_BINFO (t)))
6558 /* Here we know enough to change the type of our virtual
6559 function table, but we will wait until later this function. */
6560 build_primary_vtable (CLASSTYPE_PRIMARY_BINFO (t), t);
6562 /* If we're warning about ABI tags, check the types of the new
6563 virtual functions. */
6564 if (warn_abi_tag)
6565 for (tree v = virtuals; v; v = TREE_CHAIN (v))
6566 check_abi_tags (t, TREE_VALUE (v));
6569 if (TYPE_CONTAINS_VPTR_P (t))
6571 int vindex;
6572 tree fn;
6574 if (BINFO_VTABLE (TYPE_BINFO (t)))
6575 gcc_assert (DECL_VIRTUAL_P (BINFO_VTABLE (TYPE_BINFO (t))));
6576 if (!CLASSTYPE_HAS_PRIMARY_BASE_P (t))
6577 gcc_assert (BINFO_VIRTUALS (TYPE_BINFO (t)) == NULL_TREE);
6579 /* Add entries for virtual functions introduced by this class. */
6580 BINFO_VIRTUALS (TYPE_BINFO (t))
6581 = chainon (BINFO_VIRTUALS (TYPE_BINFO (t)), virtuals);
6583 /* Set DECL_VINDEX for all functions declared in this class. */
6584 for (vindex = 0, fn = BINFO_VIRTUALS (TYPE_BINFO (t));
6586 fn = TREE_CHAIN (fn),
6587 vindex += (TARGET_VTABLE_USES_DESCRIPTORS
6588 ? TARGET_VTABLE_USES_DESCRIPTORS : 1))
6590 tree fndecl = BV_FN (fn);
6592 if (DECL_THUNK_P (fndecl))
6593 /* A thunk. We should never be calling this entry directly
6594 from this vtable -- we'd use the entry for the non
6595 thunk base function. */
6596 DECL_VINDEX (fndecl) = NULL_TREE;
6597 else if (TREE_CODE (DECL_VINDEX (fndecl)) != INTEGER_CST)
6598 DECL_VINDEX (fndecl) = build_int_cst (NULL_TREE, vindex);
6602 finish_struct_bits (t);
6603 set_method_tm_attributes (t);
6605 /* Complete the rtl for any static member objects of the type we're
6606 working on. */
6607 for (x = TYPE_FIELDS (t); x; x = DECL_CHAIN (x))
6608 if (VAR_P (x) && TREE_STATIC (x)
6609 && TREE_TYPE (x) != error_mark_node
6610 && same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (x)), t))
6611 DECL_MODE (x) = TYPE_MODE (t);
6613 /* Done with FIELDS...now decide whether to sort these for
6614 faster lookups later.
6616 We use a small number because most searches fail (succeeding
6617 ultimately as the search bores through the inheritance
6618 hierarchy), and we want this failure to occur quickly. */
6620 insert_into_classtype_sorted_fields (TYPE_FIELDS (t), t, 8);
6622 /* Complain if one of the field types requires lower visibility. */
6623 constrain_class_visibility (t);
6625 /* Make the rtl for any new vtables we have created, and unmark
6626 the base types we marked. */
6627 finish_vtbls (t);
6629 /* Build the VTT for T. */
6630 build_vtt (t);
6632 /* This warning does not make sense for Java classes, since they
6633 cannot have destructors. */
6634 if (!TYPE_FOR_JAVA (t) && warn_nonvdtor && TYPE_POLYMORPHIC_P (t))
6636 tree dtor;
6638 dtor = CLASSTYPE_DESTRUCTORS (t);
6639 if (/* An implicitly declared destructor is always public. And,
6640 if it were virtual, we would have created it by now. */
6641 !dtor
6642 || (!DECL_VINDEX (dtor)
6643 && (/* public non-virtual */
6644 (!TREE_PRIVATE (dtor) && !TREE_PROTECTED (dtor))
6645 || (/* non-public non-virtual with friends */
6646 (TREE_PRIVATE (dtor) || TREE_PROTECTED (dtor))
6647 && (CLASSTYPE_FRIEND_CLASSES (t)
6648 || DECL_FRIENDLIST (TYPE_MAIN_DECL (t)))))))
6649 warning (OPT_Wnon_virtual_dtor,
6650 "%q#T has virtual functions and accessible"
6651 " non-virtual destructor", t);
6654 complete_vars (t);
6656 if (warn_overloaded_virtual)
6657 warn_hidden (t);
6659 /* Class layout, assignment of virtual table slots, etc., is now
6660 complete. Give the back end a chance to tweak the visibility of
6661 the class or perform any other required target modifications. */
6662 targetm.cxx.adjust_class_at_definition (t);
6664 maybe_suppress_debug_info (t);
6666 if (flag_vtable_verify)
6667 vtv_save_class_info (t);
6669 dump_class_hierarchy (t);
6671 /* Finish debugging output for this type. */
6672 rest_of_type_compilation (t, ! LOCAL_CLASS_P (t));
6674 if (TYPE_TRANSPARENT_AGGR (t))
6676 tree field = first_field (t);
6677 if (field == NULL_TREE || error_operand_p (field))
6679 error ("type transparent %q#T does not have any fields", t);
6680 TYPE_TRANSPARENT_AGGR (t) = 0;
6682 else if (DECL_ARTIFICIAL (field))
6684 if (DECL_FIELD_IS_BASE (field))
6685 error ("type transparent class %qT has base classes", t);
6686 else
6688 gcc_checking_assert (DECL_VIRTUAL_P (field));
6689 error ("type transparent class %qT has virtual functions", t);
6691 TYPE_TRANSPARENT_AGGR (t) = 0;
6693 else if (TYPE_MODE (t) != DECL_MODE (field))
6695 error ("type transparent %q#T cannot be made transparent because "
6696 "the type of the first field has a different ABI from the "
6697 "class overall", t);
6698 TYPE_TRANSPARENT_AGGR (t) = 0;
6703 /* Insert FIELDS into T for the sorted case if the FIELDS count is
6704 equal to THRESHOLD or greater than THRESHOLD. */
6706 static void
6707 insert_into_classtype_sorted_fields (tree fields, tree t, int threshold)
6709 int n_fields = count_fields (fields);
6710 if (n_fields >= threshold)
6712 struct sorted_fields_type *field_vec = sorted_fields_type_new (n_fields);
6713 add_fields_to_record_type (fields, field_vec, 0);
6714 qsort (field_vec->elts, n_fields, sizeof (tree), field_decl_cmp);
6715 CLASSTYPE_SORTED_FIELDS (t) = field_vec;
6719 /* Insert lately defined enum ENUMTYPE into T for the sorted case. */
6721 void
6722 insert_late_enum_def_into_classtype_sorted_fields (tree enumtype, tree t)
6724 struct sorted_fields_type *sorted_fields = CLASSTYPE_SORTED_FIELDS (t);
6725 if (sorted_fields)
6727 int i;
6728 int n_fields
6729 = list_length (TYPE_VALUES (enumtype)) + sorted_fields->len;
6730 struct sorted_fields_type *field_vec = sorted_fields_type_new (n_fields);
6732 for (i = 0; i < sorted_fields->len; ++i)
6733 field_vec->elts[i] = sorted_fields->elts[i];
6735 add_enum_fields_to_record_type (enumtype, field_vec,
6736 sorted_fields->len);
6737 qsort (field_vec->elts, n_fields, sizeof (tree), field_decl_cmp);
6738 CLASSTYPE_SORTED_FIELDS (t) = field_vec;
6742 /* When T was built up, the member declarations were added in reverse
6743 order. Rearrange them to declaration order. */
6745 void
6746 unreverse_member_declarations (tree t)
6748 tree next;
6749 tree prev;
6750 tree x;
6752 /* The following lists are all in reverse order. Put them in
6753 declaration order now. */
6754 TYPE_METHODS (t) = nreverse (TYPE_METHODS (t));
6755 CLASSTYPE_DECL_LIST (t) = nreverse (CLASSTYPE_DECL_LIST (t));
6757 /* Actually, for the TYPE_FIELDS, only the non TYPE_DECLs are in
6758 reverse order, so we can't just use nreverse. */
6759 prev = NULL_TREE;
6760 for (x = TYPE_FIELDS (t);
6761 x && TREE_CODE (x) != TYPE_DECL;
6762 x = next)
6764 next = DECL_CHAIN (x);
6765 DECL_CHAIN (x) = prev;
6766 prev = x;
6768 if (prev)
6770 DECL_CHAIN (TYPE_FIELDS (t)) = x;
6771 if (prev)
6772 TYPE_FIELDS (t) = prev;
6776 tree
6777 finish_struct (tree t, tree attributes)
6779 location_t saved_loc = input_location;
6781 /* Now that we've got all the field declarations, reverse everything
6782 as necessary. */
6783 unreverse_member_declarations (t);
6785 cplus_decl_attributes (&t, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
6787 /* Nadger the current location so that diagnostics point to the start of
6788 the struct, not the end. */
6789 input_location = DECL_SOURCE_LOCATION (TYPE_NAME (t));
6791 if (processing_template_decl)
6793 tree x;
6795 finish_struct_methods (t);
6796 TYPE_SIZE (t) = bitsize_zero_node;
6797 TYPE_SIZE_UNIT (t) = size_zero_node;
6799 /* We need to emit an error message if this type was used as a parameter
6800 and it is an abstract type, even if it is a template. We construct
6801 a simple CLASSTYPE_PURE_VIRTUALS list without taking bases into
6802 account and we call complete_vars with this type, which will check
6803 the PARM_DECLS. Note that while the type is being defined,
6804 CLASSTYPE_PURE_VIRTUALS contains the list of the inline friends
6805 (see CLASSTYPE_INLINE_FRIENDS) so we need to clear it. */
6806 CLASSTYPE_PURE_VIRTUALS (t) = NULL;
6807 for (x = TYPE_METHODS (t); x; x = DECL_CHAIN (x))
6808 if (DECL_PURE_VIRTUAL_P (x))
6809 vec_safe_push (CLASSTYPE_PURE_VIRTUALS (t), x);
6810 complete_vars (t);
6811 /* We need to add the target functions to the CLASSTYPE_METHOD_VEC if
6812 an enclosing scope is a template class, so that this function be
6813 found by lookup_fnfields_1 when the using declaration is not
6814 instantiated yet. */
6815 for (x = TYPE_FIELDS (t); x; x = DECL_CHAIN (x))
6816 if (TREE_CODE (x) == USING_DECL)
6818 tree fn = strip_using_decl (x);
6819 if (is_overloaded_fn (fn))
6820 for (; fn; fn = OVL_NEXT (fn))
6821 add_method (t, OVL_CURRENT (fn), x);
6824 /* Remember current #pragma pack value. */
6825 TYPE_PRECISION (t) = maximum_field_alignment;
6827 /* Fix up any variants we've already built. */
6828 for (x = TYPE_NEXT_VARIANT (t); x; x = TYPE_NEXT_VARIANT (x))
6830 TYPE_SIZE (x) = TYPE_SIZE (t);
6831 TYPE_SIZE_UNIT (x) = TYPE_SIZE_UNIT (t);
6832 TYPE_FIELDS (x) = TYPE_FIELDS (t);
6833 TYPE_METHODS (x) = TYPE_METHODS (t);
6836 else
6837 finish_struct_1 (t);
6839 input_location = saved_loc;
6841 TYPE_BEING_DEFINED (t) = 0;
6843 if (current_class_type)
6844 popclass ();
6845 else
6846 error ("trying to finish struct, but kicked out due to previous parse errors");
6848 if (processing_template_decl && at_function_scope_p ()
6849 /* Lambdas are defined by the LAMBDA_EXPR. */
6850 && !LAMBDA_TYPE_P (t))
6851 add_stmt (build_min (TAG_DEFN, t));
6853 return t;
6856 /* Hash table to avoid endless recursion when handling references. */
6857 static hash_table <pointer_hash <tree_node> > fixed_type_or_null_ref_ht;
6859 /* Return the dynamic type of INSTANCE, if known.
6860 Used to determine whether the virtual function table is needed
6861 or not.
6863 *NONNULL is set iff INSTANCE can be known to be nonnull, regardless
6864 of our knowledge of its type. *NONNULL should be initialized
6865 before this function is called. */
6867 static tree
6868 fixed_type_or_null (tree instance, int *nonnull, int *cdtorp)
6870 #define RECUR(T) fixed_type_or_null((T), nonnull, cdtorp)
6872 switch (TREE_CODE (instance))
6874 case INDIRECT_REF:
6875 if (POINTER_TYPE_P (TREE_TYPE (instance)))
6876 return NULL_TREE;
6877 else
6878 return RECUR (TREE_OPERAND (instance, 0));
6880 case CALL_EXPR:
6881 /* This is a call to a constructor, hence it's never zero. */
6882 if (TREE_HAS_CONSTRUCTOR (instance))
6884 if (nonnull)
6885 *nonnull = 1;
6886 return TREE_TYPE (instance);
6888 return NULL_TREE;
6890 case SAVE_EXPR:
6891 /* This is a call to a constructor, hence it's never zero. */
6892 if (TREE_HAS_CONSTRUCTOR (instance))
6894 if (nonnull)
6895 *nonnull = 1;
6896 return TREE_TYPE (instance);
6898 return RECUR (TREE_OPERAND (instance, 0));
6900 case POINTER_PLUS_EXPR:
6901 case PLUS_EXPR:
6902 case MINUS_EXPR:
6903 if (TREE_CODE (TREE_OPERAND (instance, 0)) == ADDR_EXPR)
6904 return RECUR (TREE_OPERAND (instance, 0));
6905 if (TREE_CODE (TREE_OPERAND (instance, 1)) == INTEGER_CST)
6906 /* Propagate nonnull. */
6907 return RECUR (TREE_OPERAND (instance, 0));
6909 return NULL_TREE;
6911 CASE_CONVERT:
6912 return RECUR (TREE_OPERAND (instance, 0));
6914 case ADDR_EXPR:
6915 instance = TREE_OPERAND (instance, 0);
6916 if (nonnull)
6918 /* Just because we see an ADDR_EXPR doesn't mean we're dealing
6919 with a real object -- given &p->f, p can still be null. */
6920 tree t = get_base_address (instance);
6921 /* ??? Probably should check DECL_WEAK here. */
6922 if (t && DECL_P (t))
6923 *nonnull = 1;
6925 return RECUR (instance);
6927 case COMPONENT_REF:
6928 /* If this component is really a base class reference, then the field
6929 itself isn't definitive. */
6930 if (DECL_FIELD_IS_BASE (TREE_OPERAND (instance, 1)))
6931 return RECUR (TREE_OPERAND (instance, 0));
6932 return RECUR (TREE_OPERAND (instance, 1));
6934 case VAR_DECL:
6935 case FIELD_DECL:
6936 if (TREE_CODE (TREE_TYPE (instance)) == ARRAY_TYPE
6937 && MAYBE_CLASS_TYPE_P (TREE_TYPE (TREE_TYPE (instance))))
6939 if (nonnull)
6940 *nonnull = 1;
6941 return TREE_TYPE (TREE_TYPE (instance));
6943 /* fall through... */
6944 case TARGET_EXPR:
6945 case PARM_DECL:
6946 case RESULT_DECL:
6947 if (MAYBE_CLASS_TYPE_P (TREE_TYPE (instance)))
6949 if (nonnull)
6950 *nonnull = 1;
6951 return TREE_TYPE (instance);
6953 else if (instance == current_class_ptr)
6955 if (nonnull)
6956 *nonnull = 1;
6958 /* if we're in a ctor or dtor, we know our type. If
6959 current_class_ptr is set but we aren't in a function, we're in
6960 an NSDMI (and therefore a constructor). */
6961 if (current_scope () != current_function_decl
6962 || (DECL_LANG_SPECIFIC (current_function_decl)
6963 && (DECL_CONSTRUCTOR_P (current_function_decl)
6964 || DECL_DESTRUCTOR_P (current_function_decl))))
6966 if (cdtorp)
6967 *cdtorp = 1;
6968 return TREE_TYPE (TREE_TYPE (instance));
6971 else if (TREE_CODE (TREE_TYPE (instance)) == REFERENCE_TYPE)
6973 /* We only need one hash table because it is always left empty. */
6974 if (!fixed_type_or_null_ref_ht.is_created ())
6975 fixed_type_or_null_ref_ht.create (37);
6977 /* Reference variables should be references to objects. */
6978 if (nonnull)
6979 *nonnull = 1;
6981 /* Enter the INSTANCE in a table to prevent recursion; a
6982 variable's initializer may refer to the variable
6983 itself. */
6984 if (VAR_P (instance)
6985 && DECL_INITIAL (instance)
6986 && !type_dependent_expression_p_push (DECL_INITIAL (instance))
6987 && !fixed_type_or_null_ref_ht.find (instance))
6989 tree type;
6990 tree_node **slot;
6992 slot = fixed_type_or_null_ref_ht.find_slot (instance, INSERT);
6993 *slot = instance;
6994 type = RECUR (DECL_INITIAL (instance));
6995 fixed_type_or_null_ref_ht.remove_elt (instance);
6997 return type;
7000 return NULL_TREE;
7002 default:
7003 return NULL_TREE;
7005 #undef RECUR
7008 /* Return nonzero if the dynamic type of INSTANCE is known, and
7009 equivalent to the static type. We also handle the case where
7010 INSTANCE is really a pointer. Return negative if this is a
7011 ctor/dtor. There the dynamic type is known, but this might not be
7012 the most derived base of the original object, and hence virtual
7013 bases may not be laid out according to this type.
7015 Used to determine whether the virtual function table is needed
7016 or not.
7018 *NONNULL is set iff INSTANCE can be known to be nonnull, regardless
7019 of our knowledge of its type. *NONNULL should be initialized
7020 before this function is called. */
7023 resolves_to_fixed_type_p (tree instance, int* nonnull)
7025 tree t = TREE_TYPE (instance);
7026 int cdtorp = 0;
7027 tree fixed;
7029 /* processing_template_decl can be false in a template if we're in
7030 fold_non_dependent_expr, but we still want to suppress this check. */
7031 if (in_template_function ())
7033 /* In a template we only care about the type of the result. */
7034 if (nonnull)
7035 *nonnull = true;
7036 return true;
7039 fixed = fixed_type_or_null (instance, nonnull, &cdtorp);
7040 if (fixed == NULL_TREE)
7041 return 0;
7042 if (POINTER_TYPE_P (t))
7043 t = TREE_TYPE (t);
7044 if (!same_type_ignoring_top_level_qualifiers_p (t, fixed))
7045 return 0;
7046 return cdtorp ? -1 : 1;
7050 void
7051 init_class_processing (void)
7053 current_class_depth = 0;
7054 current_class_stack_size = 10;
7055 current_class_stack
7056 = XNEWVEC (struct class_stack_node, current_class_stack_size);
7057 vec_alloc (local_classes, 8);
7058 sizeof_biggest_empty_class = size_zero_node;
7060 ridpointers[(int) RID_PUBLIC] = access_public_node;
7061 ridpointers[(int) RID_PRIVATE] = access_private_node;
7062 ridpointers[(int) RID_PROTECTED] = access_protected_node;
7065 /* Restore the cached PREVIOUS_CLASS_LEVEL. */
7067 static void
7068 restore_class_cache (void)
7070 tree type;
7072 /* We are re-entering the same class we just left, so we don't
7073 have to search the whole inheritance matrix to find all the
7074 decls to bind again. Instead, we install the cached
7075 class_shadowed list and walk through it binding names. */
7076 push_binding_level (previous_class_level);
7077 class_binding_level = previous_class_level;
7078 /* Restore IDENTIFIER_TYPE_VALUE. */
7079 for (type = class_binding_level->type_shadowed;
7080 type;
7081 type = TREE_CHAIN (type))
7082 SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (type), TREE_TYPE (type));
7085 /* Set global variables CURRENT_CLASS_NAME and CURRENT_CLASS_TYPE as
7086 appropriate for TYPE.
7088 So that we may avoid calls to lookup_name, we cache the _TYPE
7089 nodes of local TYPE_DECLs in the TREE_TYPE field of the name.
7091 For multiple inheritance, we perform a two-pass depth-first search
7092 of the type lattice. */
7094 void
7095 pushclass (tree type)
7097 class_stack_node_t csn;
7099 type = TYPE_MAIN_VARIANT (type);
7101 /* Make sure there is enough room for the new entry on the stack. */
7102 if (current_class_depth + 1 >= current_class_stack_size)
7104 current_class_stack_size *= 2;
7105 current_class_stack
7106 = XRESIZEVEC (struct class_stack_node, current_class_stack,
7107 current_class_stack_size);
7110 /* Insert a new entry on the class stack. */
7111 csn = current_class_stack + current_class_depth;
7112 csn->name = current_class_name;
7113 csn->type = current_class_type;
7114 csn->access = current_access_specifier;
7115 csn->names_used = 0;
7116 csn->hidden = 0;
7117 current_class_depth++;
7119 /* Now set up the new type. */
7120 current_class_name = TYPE_NAME (type);
7121 if (TREE_CODE (current_class_name) == TYPE_DECL)
7122 current_class_name = DECL_NAME (current_class_name);
7123 current_class_type = type;
7125 /* By default, things in classes are private, while things in
7126 structures or unions are public. */
7127 current_access_specifier = (CLASSTYPE_DECLARED_CLASS (type)
7128 ? access_private_node
7129 : access_public_node);
7131 if (previous_class_level
7132 && type != previous_class_level->this_entity
7133 && current_class_depth == 1)
7135 /* Forcibly remove any old class remnants. */
7136 invalidate_class_lookup_cache ();
7139 if (!previous_class_level
7140 || type != previous_class_level->this_entity
7141 || current_class_depth > 1)
7142 pushlevel_class ();
7143 else
7144 restore_class_cache ();
7147 /* When we exit a toplevel class scope, we save its binding level so
7148 that we can restore it quickly. Here, we've entered some other
7149 class, so we must invalidate our cache. */
7151 void
7152 invalidate_class_lookup_cache (void)
7154 previous_class_level = NULL;
7157 /* Get out of the current class scope. If we were in a class scope
7158 previously, that is the one popped to. */
7160 void
7161 popclass (void)
7163 poplevel_class ();
7165 current_class_depth--;
7166 current_class_name = current_class_stack[current_class_depth].name;
7167 current_class_type = current_class_stack[current_class_depth].type;
7168 current_access_specifier = current_class_stack[current_class_depth].access;
7169 if (current_class_stack[current_class_depth].names_used)
7170 splay_tree_delete (current_class_stack[current_class_depth].names_used);
7173 /* Mark the top of the class stack as hidden. */
7175 void
7176 push_class_stack (void)
7178 if (current_class_depth)
7179 ++current_class_stack[current_class_depth - 1].hidden;
7182 /* Mark the top of the class stack as un-hidden. */
7184 void
7185 pop_class_stack (void)
7187 if (current_class_depth)
7188 --current_class_stack[current_class_depth - 1].hidden;
7191 /* Returns 1 if the class type currently being defined is either T or
7192 a nested type of T. */
7194 bool
7195 currently_open_class (tree t)
7197 int i;
7199 if (!CLASS_TYPE_P (t))
7200 return false;
7202 t = TYPE_MAIN_VARIANT (t);
7204 /* We start looking from 1 because entry 0 is from global scope,
7205 and has no type. */
7206 for (i = current_class_depth; i > 0; --i)
7208 tree c;
7209 if (i == current_class_depth)
7210 c = current_class_type;
7211 else
7213 if (current_class_stack[i].hidden)
7214 break;
7215 c = current_class_stack[i].type;
7217 if (!c)
7218 continue;
7219 if (same_type_p (c, t))
7220 return true;
7222 return false;
7225 /* If either current_class_type or one of its enclosing classes are derived
7226 from T, return the appropriate type. Used to determine how we found
7227 something via unqualified lookup. */
7229 tree
7230 currently_open_derived_class (tree t)
7232 int i;
7234 /* The bases of a dependent type are unknown. */
7235 if (dependent_type_p (t))
7236 return NULL_TREE;
7238 if (!current_class_type)
7239 return NULL_TREE;
7241 if (DERIVED_FROM_P (t, current_class_type))
7242 return current_class_type;
7244 for (i = current_class_depth - 1; i > 0; --i)
7246 if (current_class_stack[i].hidden)
7247 break;
7248 if (DERIVED_FROM_P (t, current_class_stack[i].type))
7249 return current_class_stack[i].type;
7252 return NULL_TREE;
7255 /* Returns the innermost class type which is not a lambda closure type. */
7257 tree
7258 current_nonlambda_class_type (void)
7260 int i;
7262 /* We start looking from 1 because entry 0 is from global scope,
7263 and has no type. */
7264 for (i = current_class_depth; i > 0; --i)
7266 tree c;
7267 if (i == current_class_depth)
7268 c = current_class_type;
7269 else
7271 if (current_class_stack[i].hidden)
7272 break;
7273 c = current_class_stack[i].type;
7275 if (!c)
7276 continue;
7277 if (!LAMBDA_TYPE_P (c))
7278 return c;
7280 return NULL_TREE;
7283 /* When entering a class scope, all enclosing class scopes' names with
7284 static meaning (static variables, static functions, types and
7285 enumerators) have to be visible. This recursive function calls
7286 pushclass for all enclosing class contexts until global or a local
7287 scope is reached. TYPE is the enclosed class. */
7289 void
7290 push_nested_class (tree type)
7292 /* A namespace might be passed in error cases, like A::B:C. */
7293 if (type == NULL_TREE
7294 || !CLASS_TYPE_P (type))
7295 return;
7297 push_nested_class (DECL_CONTEXT (TYPE_MAIN_DECL (type)));
7299 pushclass (type);
7302 /* Undoes a push_nested_class call. */
7304 void
7305 pop_nested_class (void)
7307 tree context = DECL_CONTEXT (TYPE_MAIN_DECL (current_class_type));
7309 popclass ();
7310 if (context && CLASS_TYPE_P (context))
7311 pop_nested_class ();
7314 /* Returns the number of extern "LANG" blocks we are nested within. */
7317 current_lang_depth (void)
7319 return vec_safe_length (current_lang_base);
7322 /* Set global variables CURRENT_LANG_NAME to appropriate value
7323 so that behavior of name-mangling machinery is correct. */
7325 void
7326 push_lang_context (tree name)
7328 vec_safe_push (current_lang_base, current_lang_name);
7330 if (name == lang_name_cplusplus)
7332 current_lang_name = name;
7334 else if (name == lang_name_java)
7336 current_lang_name = name;
7337 /* DECL_IGNORED_P is initially set for these types, to avoid clutter.
7338 (See record_builtin_java_type in decl.c.) However, that causes
7339 incorrect debug entries if these types are actually used.
7340 So we re-enable debug output after extern "Java". */
7341 DECL_IGNORED_P (TYPE_NAME (java_byte_type_node)) = 0;
7342 DECL_IGNORED_P (TYPE_NAME (java_short_type_node)) = 0;
7343 DECL_IGNORED_P (TYPE_NAME (java_int_type_node)) = 0;
7344 DECL_IGNORED_P (TYPE_NAME (java_long_type_node)) = 0;
7345 DECL_IGNORED_P (TYPE_NAME (java_float_type_node)) = 0;
7346 DECL_IGNORED_P (TYPE_NAME (java_double_type_node)) = 0;
7347 DECL_IGNORED_P (TYPE_NAME (java_char_type_node)) = 0;
7348 DECL_IGNORED_P (TYPE_NAME (java_boolean_type_node)) = 0;
7350 else if (name == lang_name_c)
7352 current_lang_name = name;
7354 else
7355 error ("language string %<\"%E\"%> not recognized", name);
7358 /* Get out of the current language scope. */
7360 void
7361 pop_lang_context (void)
7363 current_lang_name = current_lang_base->pop ();
7366 /* Type instantiation routines. */
7368 /* Given an OVERLOAD and a TARGET_TYPE, return the function that
7369 matches the TARGET_TYPE. If there is no satisfactory match, return
7370 error_mark_node, and issue an error & warning messages under
7371 control of FLAGS. Permit pointers to member function if FLAGS
7372 permits. If TEMPLATE_ONLY, the name of the overloaded function was
7373 a template-id, and EXPLICIT_TARGS are the explicitly provided
7374 template arguments.
7376 If OVERLOAD is for one or more member functions, then ACCESS_PATH
7377 is the base path used to reference those member functions. If
7378 the address is resolved to a member function, access checks will be
7379 performed and errors issued if appropriate. */
7381 static tree
7382 resolve_address_of_overloaded_function (tree target_type,
7383 tree overload,
7384 tsubst_flags_t flags,
7385 bool template_only,
7386 tree explicit_targs,
7387 tree access_path)
7389 /* Here's what the standard says:
7391 [over.over]
7393 If the name is a function template, template argument deduction
7394 is done, and if the argument deduction succeeds, the deduced
7395 arguments are used to generate a single template function, which
7396 is added to the set of overloaded functions considered.
7398 Non-member functions and static member functions match targets of
7399 type "pointer-to-function" or "reference-to-function." Nonstatic
7400 member functions match targets of type "pointer-to-member
7401 function;" the function type of the pointer to member is used to
7402 select the member function from the set of overloaded member
7403 functions. If a nonstatic member function is selected, the
7404 reference to the overloaded function name is required to have the
7405 form of a pointer to member as described in 5.3.1.
7407 If more than one function is selected, any template functions in
7408 the set are eliminated if the set also contains a non-template
7409 function, and any given template function is eliminated if the
7410 set contains a second template function that is more specialized
7411 than the first according to the partial ordering rules 14.5.5.2.
7412 After such eliminations, if any, there shall remain exactly one
7413 selected function. */
7415 int is_ptrmem = 0;
7416 /* We store the matches in a TREE_LIST rooted here. The functions
7417 are the TREE_PURPOSE, not the TREE_VALUE, in this list, for easy
7418 interoperability with most_specialized_instantiation. */
7419 tree matches = NULL_TREE;
7420 tree fn;
7421 tree target_fn_type;
7423 /* By the time we get here, we should be seeing only real
7424 pointer-to-member types, not the internal POINTER_TYPE to
7425 METHOD_TYPE representation. */
7426 gcc_assert (!TYPE_PTR_P (target_type)
7427 || TREE_CODE (TREE_TYPE (target_type)) != METHOD_TYPE);
7429 gcc_assert (is_overloaded_fn (overload));
7431 /* Check that the TARGET_TYPE is reasonable. */
7432 if (TYPE_PTRFN_P (target_type)
7433 || TYPE_REFFN_P (target_type))
7434 /* This is OK. */;
7435 else if (TYPE_PTRMEMFUNC_P (target_type))
7436 /* This is OK, too. */
7437 is_ptrmem = 1;
7438 else if (TREE_CODE (target_type) == FUNCTION_TYPE)
7439 /* This is OK, too. This comes from a conversion to reference
7440 type. */
7441 target_type = build_reference_type (target_type);
7442 else
7444 if (flags & tf_error)
7445 error ("cannot resolve overloaded function %qD based on"
7446 " conversion to type %qT",
7447 DECL_NAME (OVL_FUNCTION (overload)), target_type);
7448 return error_mark_node;
7451 /* Non-member functions and static member functions match targets of type
7452 "pointer-to-function" or "reference-to-function." Nonstatic member
7453 functions match targets of type "pointer-to-member-function;" the
7454 function type of the pointer to member is used to select the member
7455 function from the set of overloaded member functions.
7457 So figure out the FUNCTION_TYPE that we want to match against. */
7458 target_fn_type = static_fn_type (target_type);
7460 /* If we can find a non-template function that matches, we can just
7461 use it. There's no point in generating template instantiations
7462 if we're just going to throw them out anyhow. But, of course, we
7463 can only do this when we don't *need* a template function. */
7464 if (!template_only)
7466 tree fns;
7468 for (fns = overload; fns; fns = OVL_NEXT (fns))
7470 tree fn = OVL_CURRENT (fns);
7472 if (TREE_CODE (fn) == TEMPLATE_DECL)
7473 /* We're not looking for templates just yet. */
7474 continue;
7476 if ((TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
7477 != is_ptrmem)
7478 /* We're looking for a non-static member, and this isn't
7479 one, or vice versa. */
7480 continue;
7482 /* Ignore functions which haven't been explicitly
7483 declared. */
7484 if (DECL_ANTICIPATED (fn))
7485 continue;
7487 /* See if there's a match. */
7488 if (same_type_p (target_fn_type, static_fn_type (fn)))
7489 matches = tree_cons (fn, NULL_TREE, matches);
7493 /* Now, if we've already got a match (or matches), there's no need
7494 to proceed to the template functions. But, if we don't have a
7495 match we need to look at them, too. */
7496 if (!matches)
7498 tree target_arg_types;
7499 tree target_ret_type;
7500 tree fns;
7501 tree *args;
7502 unsigned int nargs, ia;
7503 tree arg;
7505 target_arg_types = TYPE_ARG_TYPES (target_fn_type);
7506 target_ret_type = TREE_TYPE (target_fn_type);
7508 nargs = list_length (target_arg_types);
7509 args = XALLOCAVEC (tree, nargs);
7510 for (arg = target_arg_types, ia = 0;
7511 arg != NULL_TREE && arg != void_list_node;
7512 arg = TREE_CHAIN (arg), ++ia)
7513 args[ia] = TREE_VALUE (arg);
7514 nargs = ia;
7516 for (fns = overload; fns; fns = OVL_NEXT (fns))
7518 tree fn = OVL_CURRENT (fns);
7519 tree instantiation;
7520 tree targs;
7522 if (TREE_CODE (fn) != TEMPLATE_DECL)
7523 /* We're only looking for templates. */
7524 continue;
7526 if ((TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
7527 != is_ptrmem)
7528 /* We're not looking for a non-static member, and this is
7529 one, or vice versa. */
7530 continue;
7532 tree ret = target_ret_type;
7534 /* If the template has a deduced return type, don't expose it to
7535 template argument deduction. */
7536 if (undeduced_auto_decl (fn))
7537 ret = NULL_TREE;
7539 /* Try to do argument deduction. */
7540 targs = make_tree_vec (DECL_NTPARMS (fn));
7541 instantiation = fn_type_unification (fn, explicit_targs, targs, args,
7542 nargs, ret,
7543 DEDUCE_EXACT, LOOKUP_NORMAL,
7544 false, false);
7545 if (instantiation == error_mark_node)
7546 /* Instantiation failed. */
7547 continue;
7549 /* And now force instantiation to do return type deduction. */
7550 if (undeduced_auto_decl (instantiation))
7552 ++function_depth;
7553 instantiate_decl (instantiation, /*defer*/false, /*class*/false);
7554 --function_depth;
7556 require_deduced_type (instantiation);
7559 /* See if there's a match. */
7560 if (same_type_p (target_fn_type, static_fn_type (instantiation)))
7561 matches = tree_cons (instantiation, fn, matches);
7564 /* Now, remove all but the most specialized of the matches. */
7565 if (matches)
7567 tree match = most_specialized_instantiation (matches);
7569 if (match != error_mark_node)
7570 matches = tree_cons (TREE_PURPOSE (match),
7571 NULL_TREE,
7572 NULL_TREE);
7576 /* Now we should have exactly one function in MATCHES. */
7577 if (matches == NULL_TREE)
7579 /* There were *no* matches. */
7580 if (flags & tf_error)
7582 error ("no matches converting function %qD to type %q#T",
7583 DECL_NAME (OVL_CURRENT (overload)),
7584 target_type);
7586 print_candidates (overload);
7588 return error_mark_node;
7590 else if (TREE_CHAIN (matches))
7592 /* There were too many matches. First check if they're all
7593 the same function. */
7594 tree match = NULL_TREE;
7596 fn = TREE_PURPOSE (matches);
7598 /* For multi-versioned functions, more than one match is just fine and
7599 decls_match will return false as they are different. */
7600 for (match = TREE_CHAIN (matches); match; match = TREE_CHAIN (match))
7601 if (!decls_match (fn, TREE_PURPOSE (match))
7602 && !targetm.target_option.function_versions
7603 (fn, TREE_PURPOSE (match)))
7604 break;
7606 if (match)
7608 if (flags & tf_error)
7610 error ("converting overloaded function %qD to type %q#T is ambiguous",
7611 DECL_NAME (OVL_FUNCTION (overload)),
7612 target_type);
7614 /* Since print_candidates expects the functions in the
7615 TREE_VALUE slot, we flip them here. */
7616 for (match = matches; match; match = TREE_CHAIN (match))
7617 TREE_VALUE (match) = TREE_PURPOSE (match);
7619 print_candidates (matches);
7622 return error_mark_node;
7626 /* Good, exactly one match. Now, convert it to the correct type. */
7627 fn = TREE_PURPOSE (matches);
7629 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
7630 && !(flags & tf_ptrmem_ok) && !flag_ms_extensions)
7632 static int explained;
7634 if (!(flags & tf_error))
7635 return error_mark_node;
7637 permerror (input_location, "assuming pointer to member %qD", fn);
7638 if (!explained)
7640 inform (input_location, "(a pointer to member can only be formed with %<&%E%>)", fn);
7641 explained = 1;
7645 /* If a pointer to a function that is multi-versioned is requested, the
7646 pointer to the dispatcher function is returned instead. This works
7647 well because indirectly calling the function will dispatch the right
7648 function version at run-time. */
7649 if (DECL_FUNCTION_VERSIONED (fn))
7651 fn = get_function_version_dispatcher (fn);
7652 if (fn == NULL)
7653 return error_mark_node;
7654 /* Mark all the versions corresponding to the dispatcher as used. */
7655 if (!(flags & tf_conv))
7656 mark_versions_used (fn);
7659 /* If we're doing overload resolution purely for the purpose of
7660 determining conversion sequences, we should not consider the
7661 function used. If this conversion sequence is selected, the
7662 function will be marked as used at this point. */
7663 if (!(flags & tf_conv))
7665 /* Make =delete work with SFINAE. */
7666 if (DECL_DELETED_FN (fn) && !(flags & tf_error))
7667 return error_mark_node;
7669 mark_used (fn);
7672 /* We could not check access to member functions when this
7673 expression was originally created since we did not know at that
7674 time to which function the expression referred. */
7675 if (DECL_FUNCTION_MEMBER_P (fn))
7677 gcc_assert (access_path);
7678 perform_or_defer_access_check (access_path, fn, fn, flags);
7681 if (TYPE_PTRFN_P (target_type) || TYPE_PTRMEMFUNC_P (target_type))
7682 return cp_build_addr_expr (fn, flags);
7683 else
7685 /* The target must be a REFERENCE_TYPE. Above, cp_build_unary_op
7686 will mark the function as addressed, but here we must do it
7687 explicitly. */
7688 cxx_mark_addressable (fn);
7690 return fn;
7694 /* This function will instantiate the type of the expression given in
7695 RHS to match the type of LHSTYPE. If errors exist, then return
7696 error_mark_node. FLAGS is a bit mask. If TF_ERROR is set, then
7697 we complain on errors. If we are not complaining, never modify rhs,
7698 as overload resolution wants to try many possible instantiations, in
7699 the hope that at least one will work.
7701 For non-recursive calls, LHSTYPE should be a function, pointer to
7702 function, or a pointer to member function. */
7704 tree
7705 instantiate_type (tree lhstype, tree rhs, tsubst_flags_t flags)
7707 tsubst_flags_t flags_in = flags;
7708 tree access_path = NULL_TREE;
7710 flags &= ~tf_ptrmem_ok;
7712 if (lhstype == unknown_type_node)
7714 if (flags & tf_error)
7715 error ("not enough type information");
7716 return error_mark_node;
7719 if (TREE_TYPE (rhs) != NULL_TREE && ! (type_unknown_p (rhs)))
7721 tree fntype = non_reference (lhstype);
7722 if (same_type_p (fntype, TREE_TYPE (rhs)))
7723 return rhs;
7724 if (flag_ms_extensions
7725 && TYPE_PTRMEMFUNC_P (fntype)
7726 && !TYPE_PTRMEMFUNC_P (TREE_TYPE (rhs)))
7727 /* Microsoft allows `A::f' to be resolved to a
7728 pointer-to-member. */
7730 else
7732 if (flags & tf_error)
7733 error ("cannot convert %qE from type %qT to type %qT",
7734 rhs, TREE_TYPE (rhs), fntype);
7735 return error_mark_node;
7739 if (BASELINK_P (rhs))
7741 access_path = BASELINK_ACCESS_BINFO (rhs);
7742 rhs = BASELINK_FUNCTIONS (rhs);
7745 /* If we are in a template, and have a NON_DEPENDENT_EXPR, we cannot
7746 deduce any type information. */
7747 if (TREE_CODE (rhs) == NON_DEPENDENT_EXPR)
7749 if (flags & tf_error)
7750 error ("not enough type information");
7751 return error_mark_node;
7754 /* There only a few kinds of expressions that may have a type
7755 dependent on overload resolution. */
7756 gcc_assert (TREE_CODE (rhs) == ADDR_EXPR
7757 || TREE_CODE (rhs) == COMPONENT_REF
7758 || is_overloaded_fn (rhs)
7759 || (flag_ms_extensions && TREE_CODE (rhs) == FUNCTION_DECL));
7761 /* This should really only be used when attempting to distinguish
7762 what sort of a pointer to function we have. For now, any
7763 arithmetic operation which is not supported on pointers
7764 is rejected as an error. */
7766 switch (TREE_CODE (rhs))
7768 case COMPONENT_REF:
7770 tree member = TREE_OPERAND (rhs, 1);
7772 member = instantiate_type (lhstype, member, flags);
7773 if (member != error_mark_node
7774 && TREE_SIDE_EFFECTS (TREE_OPERAND (rhs, 0)))
7775 /* Do not lose object's side effects. */
7776 return build2 (COMPOUND_EXPR, TREE_TYPE (member),
7777 TREE_OPERAND (rhs, 0), member);
7778 return member;
7781 case OFFSET_REF:
7782 rhs = TREE_OPERAND (rhs, 1);
7783 if (BASELINK_P (rhs))
7784 return instantiate_type (lhstype, rhs, flags_in);
7786 /* This can happen if we are forming a pointer-to-member for a
7787 member template. */
7788 gcc_assert (TREE_CODE (rhs) == TEMPLATE_ID_EXPR);
7790 /* Fall through. */
7792 case TEMPLATE_ID_EXPR:
7794 tree fns = TREE_OPERAND (rhs, 0);
7795 tree args = TREE_OPERAND (rhs, 1);
7797 return
7798 resolve_address_of_overloaded_function (lhstype, fns, flags_in,
7799 /*template_only=*/true,
7800 args, access_path);
7803 case OVERLOAD:
7804 case FUNCTION_DECL:
7805 return
7806 resolve_address_of_overloaded_function (lhstype, rhs, flags_in,
7807 /*template_only=*/false,
7808 /*explicit_targs=*/NULL_TREE,
7809 access_path);
7811 case ADDR_EXPR:
7813 if (PTRMEM_OK_P (rhs))
7814 flags |= tf_ptrmem_ok;
7816 return instantiate_type (lhstype, TREE_OPERAND (rhs, 0), flags);
7819 case ERROR_MARK:
7820 return error_mark_node;
7822 default:
7823 gcc_unreachable ();
7825 return error_mark_node;
7828 /* Return the name of the virtual function pointer field
7829 (as an IDENTIFIER_NODE) for the given TYPE. Note that
7830 this may have to look back through base types to find the
7831 ultimate field name. (For single inheritance, these could
7832 all be the same name. Who knows for multiple inheritance). */
7834 static tree
7835 get_vfield_name (tree type)
7837 tree binfo, base_binfo;
7838 char *buf;
7840 for (binfo = TYPE_BINFO (type);
7841 BINFO_N_BASE_BINFOS (binfo);
7842 binfo = base_binfo)
7844 base_binfo = BINFO_BASE_BINFO (binfo, 0);
7846 if (BINFO_VIRTUAL_P (base_binfo)
7847 || !TYPE_CONTAINS_VPTR_P (BINFO_TYPE (base_binfo)))
7848 break;
7851 type = BINFO_TYPE (binfo);
7852 buf = (char *) alloca (sizeof (VFIELD_NAME_FORMAT)
7853 + TYPE_NAME_LENGTH (type) + 2);
7854 sprintf (buf, VFIELD_NAME_FORMAT,
7855 IDENTIFIER_POINTER (constructor_name (type)));
7856 return get_identifier (buf);
7859 void
7860 print_class_statistics (void)
7862 if (! GATHER_STATISTICS)
7863 return;
7865 fprintf (stderr, "convert_harshness = %d\n", n_convert_harshness);
7866 fprintf (stderr, "compute_conversion_costs = %d\n", n_compute_conversion_costs);
7867 if (n_vtables)
7869 fprintf (stderr, "vtables = %d; vtable searches = %d\n",
7870 n_vtables, n_vtable_searches);
7871 fprintf (stderr, "vtable entries = %d; vtable elems = %d\n",
7872 n_vtable_entries, n_vtable_elems);
7876 /* Build a dummy reference to ourselves so Derived::Base (and A::A) works,
7877 according to [class]:
7878 The class-name is also inserted
7879 into the scope of the class itself. For purposes of access checking,
7880 the inserted class name is treated as if it were a public member name. */
7882 void
7883 build_self_reference (void)
7885 tree name = constructor_name (current_class_type);
7886 tree value = build_lang_decl (TYPE_DECL, name, current_class_type);
7887 tree saved_cas;
7889 DECL_NONLOCAL (value) = 1;
7890 DECL_CONTEXT (value) = current_class_type;
7891 DECL_ARTIFICIAL (value) = 1;
7892 SET_DECL_SELF_REFERENCE_P (value);
7893 set_underlying_type (value);
7895 if (processing_template_decl)
7896 value = push_template_decl (value);
7898 saved_cas = current_access_specifier;
7899 current_access_specifier = access_public_node;
7900 finish_member_declaration (value);
7901 current_access_specifier = saved_cas;
7904 /* Returns 1 if TYPE contains only padding bytes. */
7907 is_empty_class (tree type)
7909 if (type == error_mark_node)
7910 return 0;
7912 if (! CLASS_TYPE_P (type))
7913 return 0;
7915 /* In G++ 3.2, whether or not a class was empty was determined by
7916 looking at its size. */
7917 if (abi_version_at_least (2))
7918 return CLASSTYPE_EMPTY_P (type);
7919 else
7920 return integer_zerop (CLASSTYPE_SIZE (type));
7923 /* Returns true if TYPE contains an empty class. */
7925 static bool
7926 contains_empty_class_p (tree type)
7928 if (is_empty_class (type))
7929 return true;
7930 if (CLASS_TYPE_P (type))
7932 tree field;
7933 tree binfo;
7934 tree base_binfo;
7935 int i;
7937 for (binfo = TYPE_BINFO (type), i = 0;
7938 BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
7939 if (contains_empty_class_p (BINFO_TYPE (base_binfo)))
7940 return true;
7941 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
7942 if (TREE_CODE (field) == FIELD_DECL
7943 && !DECL_ARTIFICIAL (field)
7944 && is_empty_class (TREE_TYPE (field)))
7945 return true;
7947 else if (TREE_CODE (type) == ARRAY_TYPE)
7948 return contains_empty_class_p (TREE_TYPE (type));
7949 return false;
7952 /* Returns true if TYPE contains no actual data, just various
7953 possible combinations of empty classes and possibly a vptr. */
7955 bool
7956 is_really_empty_class (tree type)
7958 if (CLASS_TYPE_P (type))
7960 tree field;
7961 tree binfo;
7962 tree base_binfo;
7963 int i;
7965 /* CLASSTYPE_EMPTY_P isn't set properly until the class is actually laid
7966 out, but we'd like to be able to check this before then. */
7967 if (COMPLETE_TYPE_P (type) && is_empty_class (type))
7968 return true;
7970 for (binfo = TYPE_BINFO (type), i = 0;
7971 BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
7972 if (!is_really_empty_class (BINFO_TYPE (base_binfo)))
7973 return false;
7974 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
7975 if (TREE_CODE (field) == FIELD_DECL
7976 && !DECL_ARTIFICIAL (field)
7977 && !is_really_empty_class (TREE_TYPE (field)))
7978 return false;
7979 return true;
7981 else if (TREE_CODE (type) == ARRAY_TYPE)
7982 return is_really_empty_class (TREE_TYPE (type));
7983 return false;
7986 /* Note that NAME was looked up while the current class was being
7987 defined and that the result of that lookup was DECL. */
7989 void
7990 maybe_note_name_used_in_class (tree name, tree decl)
7992 splay_tree names_used;
7994 /* If we're not defining a class, there's nothing to do. */
7995 if (!(innermost_scope_kind() == sk_class
7996 && TYPE_BEING_DEFINED (current_class_type)
7997 && !LAMBDA_TYPE_P (current_class_type)))
7998 return;
8000 /* If there's already a binding for this NAME, then we don't have
8001 anything to worry about. */
8002 if (lookup_member (current_class_type, name,
8003 /*protect=*/0, /*want_type=*/false, tf_warning_or_error))
8004 return;
8006 if (!current_class_stack[current_class_depth - 1].names_used)
8007 current_class_stack[current_class_depth - 1].names_used
8008 = splay_tree_new (splay_tree_compare_pointers, 0, 0);
8009 names_used = current_class_stack[current_class_depth - 1].names_used;
8011 splay_tree_insert (names_used,
8012 (splay_tree_key) name,
8013 (splay_tree_value) decl);
8016 /* Note that NAME was declared (as DECL) in the current class. Check
8017 to see that the declaration is valid. */
8019 void
8020 note_name_declared_in_class (tree name, tree decl)
8022 splay_tree names_used;
8023 splay_tree_node n;
8025 /* Look to see if we ever used this name. */
8026 names_used
8027 = current_class_stack[current_class_depth - 1].names_used;
8028 if (!names_used)
8029 return;
8030 /* The C language allows members to be declared with a type of the same
8031 name, and the C++ standard says this diagnostic is not required. So
8032 allow it in extern "C" blocks unless predantic is specified.
8033 Allow it in all cases if -ms-extensions is specified. */
8034 if ((!pedantic && current_lang_name == lang_name_c)
8035 || flag_ms_extensions)
8036 return;
8037 n = splay_tree_lookup (names_used, (splay_tree_key) name);
8038 if (n)
8040 /* [basic.scope.class]
8042 A name N used in a class S shall refer to the same declaration
8043 in its context and when re-evaluated in the completed scope of
8044 S. */
8045 permerror (input_location, "declaration of %q#D", decl);
8046 permerror (input_location, "changes meaning of %qD from %q+#D",
8047 DECL_NAME (OVL_CURRENT (decl)), (tree) n->value);
8051 /* Returns the VAR_DECL for the complete vtable associated with BINFO.
8052 Secondary vtables are merged with primary vtables; this function
8053 will return the VAR_DECL for the primary vtable. */
8055 tree
8056 get_vtbl_decl_for_binfo (tree binfo)
8058 tree decl;
8060 decl = BINFO_VTABLE (binfo);
8061 if (decl && TREE_CODE (decl) == POINTER_PLUS_EXPR)
8063 gcc_assert (TREE_CODE (TREE_OPERAND (decl, 0)) == ADDR_EXPR);
8064 decl = TREE_OPERAND (TREE_OPERAND (decl, 0), 0);
8066 if (decl)
8067 gcc_assert (VAR_P (decl));
8068 return decl;
8072 /* Returns the binfo for the primary base of BINFO. If the resulting
8073 BINFO is a virtual base, and it is inherited elsewhere in the
8074 hierarchy, then the returned binfo might not be the primary base of
8075 BINFO in the complete object. Check BINFO_PRIMARY_P or
8076 BINFO_LOST_PRIMARY_P to be sure. */
8078 static tree
8079 get_primary_binfo (tree binfo)
8081 tree primary_base;
8083 primary_base = CLASSTYPE_PRIMARY_BINFO (BINFO_TYPE (binfo));
8084 if (!primary_base)
8085 return NULL_TREE;
8087 return copied_binfo (primary_base, binfo);
8090 /* If INDENTED_P is zero, indent to INDENT. Return nonzero. */
8092 static int
8093 maybe_indent_hierarchy (FILE * stream, int indent, int indented_p)
8095 if (!indented_p)
8096 fprintf (stream, "%*s", indent, "");
8097 return 1;
8100 /* Dump the offsets of all the bases rooted at BINFO to STREAM.
8101 INDENT should be zero when called from the top level; it is
8102 incremented recursively. IGO indicates the next expected BINFO in
8103 inheritance graph ordering. */
8105 static tree
8106 dump_class_hierarchy_r (FILE *stream,
8107 int flags,
8108 tree binfo,
8109 tree igo,
8110 int indent)
8112 int indented = 0;
8113 tree base_binfo;
8114 int i;
8116 indented = maybe_indent_hierarchy (stream, indent, 0);
8117 fprintf (stream, "%s (0x" HOST_WIDE_INT_PRINT_HEX ") ",
8118 type_as_string (BINFO_TYPE (binfo), TFF_PLAIN_IDENTIFIER),
8119 (HOST_WIDE_INT) (uintptr_t) binfo);
8120 if (binfo != igo)
8122 fprintf (stream, "alternative-path\n");
8123 return igo;
8125 igo = TREE_CHAIN (binfo);
8127 fprintf (stream, HOST_WIDE_INT_PRINT_DEC,
8128 tree_to_shwi (BINFO_OFFSET (binfo)));
8129 if (is_empty_class (BINFO_TYPE (binfo)))
8130 fprintf (stream, " empty");
8131 else if (CLASSTYPE_NEARLY_EMPTY_P (BINFO_TYPE (binfo)))
8132 fprintf (stream, " nearly-empty");
8133 if (BINFO_VIRTUAL_P (binfo))
8134 fprintf (stream, " virtual");
8135 fprintf (stream, "\n");
8137 indented = 0;
8138 if (BINFO_PRIMARY_P (binfo))
8140 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
8141 fprintf (stream, " primary-for %s (0x" HOST_WIDE_INT_PRINT_HEX ")",
8142 type_as_string (BINFO_TYPE (BINFO_INHERITANCE_CHAIN (binfo)),
8143 TFF_PLAIN_IDENTIFIER),
8144 (HOST_WIDE_INT) (uintptr_t) BINFO_INHERITANCE_CHAIN (binfo));
8146 if (BINFO_LOST_PRIMARY_P (binfo))
8148 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
8149 fprintf (stream, " lost-primary");
8151 if (indented)
8152 fprintf (stream, "\n");
8154 if (!(flags & TDF_SLIM))
8156 int indented = 0;
8158 if (BINFO_SUBVTT_INDEX (binfo))
8160 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
8161 fprintf (stream, " subvttidx=%s",
8162 expr_as_string (BINFO_SUBVTT_INDEX (binfo),
8163 TFF_PLAIN_IDENTIFIER));
8165 if (BINFO_VPTR_INDEX (binfo))
8167 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
8168 fprintf (stream, " vptridx=%s",
8169 expr_as_string (BINFO_VPTR_INDEX (binfo),
8170 TFF_PLAIN_IDENTIFIER));
8172 if (BINFO_VPTR_FIELD (binfo))
8174 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
8175 fprintf (stream, " vbaseoffset=%s",
8176 expr_as_string (BINFO_VPTR_FIELD (binfo),
8177 TFF_PLAIN_IDENTIFIER));
8179 if (BINFO_VTABLE (binfo))
8181 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
8182 fprintf (stream, " vptr=%s",
8183 expr_as_string (BINFO_VTABLE (binfo),
8184 TFF_PLAIN_IDENTIFIER));
8187 if (indented)
8188 fprintf (stream, "\n");
8191 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
8192 igo = dump_class_hierarchy_r (stream, flags, base_binfo, igo, indent + 2);
8194 return igo;
8197 /* Dump the BINFO hierarchy for T. */
8199 static void
8200 dump_class_hierarchy_1 (FILE *stream, int flags, tree t)
8202 fprintf (stream, "Class %s\n", type_as_string (t, TFF_PLAIN_IDENTIFIER));
8203 fprintf (stream, " size=%lu align=%lu\n",
8204 (unsigned long)(tree_to_shwi (TYPE_SIZE (t)) / BITS_PER_UNIT),
8205 (unsigned long)(TYPE_ALIGN (t) / BITS_PER_UNIT));
8206 fprintf (stream, " base size=%lu base align=%lu\n",
8207 (unsigned long)(tree_to_shwi (TYPE_SIZE (CLASSTYPE_AS_BASE (t)))
8208 / BITS_PER_UNIT),
8209 (unsigned long)(TYPE_ALIGN (CLASSTYPE_AS_BASE (t))
8210 / BITS_PER_UNIT));
8211 dump_class_hierarchy_r (stream, flags, TYPE_BINFO (t), TYPE_BINFO (t), 0);
8212 fprintf (stream, "\n");
8215 /* Debug interface to hierarchy dumping. */
8217 void
8218 debug_class (tree t)
8220 dump_class_hierarchy_1 (stderr, TDF_SLIM, t);
8223 static void
8224 dump_class_hierarchy (tree t)
8226 int flags;
8227 FILE *stream = dump_begin (TDI_class, &flags);
8229 if (stream)
8231 dump_class_hierarchy_1 (stream, flags, t);
8232 dump_end (TDI_class, stream);
8236 static void
8237 dump_array (FILE * stream, tree decl)
8239 tree value;
8240 unsigned HOST_WIDE_INT ix;
8241 HOST_WIDE_INT elt;
8242 tree size = TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (decl)));
8244 elt = (tree_to_shwi (TYPE_SIZE (TREE_TYPE (TREE_TYPE (decl))))
8245 / BITS_PER_UNIT);
8246 fprintf (stream, "%s:", decl_as_string (decl, TFF_PLAIN_IDENTIFIER));
8247 fprintf (stream, " %s entries",
8248 expr_as_string (size_binop (PLUS_EXPR, size, size_one_node),
8249 TFF_PLAIN_IDENTIFIER));
8250 fprintf (stream, "\n");
8252 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (DECL_INITIAL (decl)),
8253 ix, value)
8254 fprintf (stream, "%-4ld %s\n", (long)(ix * elt),
8255 expr_as_string (value, TFF_PLAIN_IDENTIFIER));
8258 static void
8259 dump_vtable (tree t, tree binfo, tree vtable)
8261 int flags;
8262 FILE *stream = dump_begin (TDI_class, &flags);
8264 if (!stream)
8265 return;
8267 if (!(flags & TDF_SLIM))
8269 int ctor_vtbl_p = TYPE_BINFO (t) != binfo;
8271 fprintf (stream, "%s for %s",
8272 ctor_vtbl_p ? "Construction vtable" : "Vtable",
8273 type_as_string (BINFO_TYPE (binfo), TFF_PLAIN_IDENTIFIER));
8274 if (ctor_vtbl_p)
8276 if (!BINFO_VIRTUAL_P (binfo))
8277 fprintf (stream, " (0x" HOST_WIDE_INT_PRINT_HEX " instance)",
8278 (HOST_WIDE_INT) (uintptr_t) binfo);
8279 fprintf (stream, " in %s", type_as_string (t, TFF_PLAIN_IDENTIFIER));
8281 fprintf (stream, "\n");
8282 dump_array (stream, vtable);
8283 fprintf (stream, "\n");
8286 dump_end (TDI_class, stream);
8289 static void
8290 dump_vtt (tree t, tree vtt)
8292 int flags;
8293 FILE *stream = dump_begin (TDI_class, &flags);
8295 if (!stream)
8296 return;
8298 if (!(flags & TDF_SLIM))
8300 fprintf (stream, "VTT for %s\n",
8301 type_as_string (t, TFF_PLAIN_IDENTIFIER));
8302 dump_array (stream, vtt);
8303 fprintf (stream, "\n");
8306 dump_end (TDI_class, stream);
8309 /* Dump a function or thunk and its thunkees. */
8311 static void
8312 dump_thunk (FILE *stream, int indent, tree thunk)
8314 static const char spaces[] = " ";
8315 tree name = DECL_NAME (thunk);
8316 tree thunks;
8318 fprintf (stream, "%.*s%p %s %s", indent, spaces,
8319 (void *)thunk,
8320 !DECL_THUNK_P (thunk) ? "function"
8321 : DECL_THIS_THUNK_P (thunk) ? "this-thunk" : "covariant-thunk",
8322 name ? IDENTIFIER_POINTER (name) : "<unset>");
8323 if (DECL_THUNK_P (thunk))
8325 HOST_WIDE_INT fixed_adjust = THUNK_FIXED_OFFSET (thunk);
8326 tree virtual_adjust = THUNK_VIRTUAL_OFFSET (thunk);
8328 fprintf (stream, " fixed=" HOST_WIDE_INT_PRINT_DEC, fixed_adjust);
8329 if (!virtual_adjust)
8330 /*NOP*/;
8331 else if (DECL_THIS_THUNK_P (thunk))
8332 fprintf (stream, " vcall=" HOST_WIDE_INT_PRINT_DEC,
8333 tree_to_shwi (virtual_adjust));
8334 else
8335 fprintf (stream, " vbase=" HOST_WIDE_INT_PRINT_DEC "(%s)",
8336 tree_to_shwi (BINFO_VPTR_FIELD (virtual_adjust)),
8337 type_as_string (BINFO_TYPE (virtual_adjust), TFF_SCOPE));
8338 if (THUNK_ALIAS (thunk))
8339 fprintf (stream, " alias to %p", (void *)THUNK_ALIAS (thunk));
8341 fprintf (stream, "\n");
8342 for (thunks = DECL_THUNKS (thunk); thunks; thunks = TREE_CHAIN (thunks))
8343 dump_thunk (stream, indent + 2, thunks);
8346 /* Dump the thunks for FN. */
8348 void
8349 debug_thunks (tree fn)
8351 dump_thunk (stderr, 0, fn);
8354 /* Virtual function table initialization. */
8356 /* Create all the necessary vtables for T and its base classes. */
8358 static void
8359 finish_vtbls (tree t)
8361 tree vbase;
8362 vec<constructor_elt, va_gc> *v = NULL;
8363 tree vtable = BINFO_VTABLE (TYPE_BINFO (t));
8365 /* We lay out the primary and secondary vtables in one contiguous
8366 vtable. The primary vtable is first, followed by the non-virtual
8367 secondary vtables in inheritance graph order. */
8368 accumulate_vtbl_inits (TYPE_BINFO (t), TYPE_BINFO (t), TYPE_BINFO (t),
8369 vtable, t, &v);
8371 /* Then come the virtual bases, also in inheritance graph order. */
8372 for (vbase = TYPE_BINFO (t); vbase; vbase = TREE_CHAIN (vbase))
8374 if (!BINFO_VIRTUAL_P (vbase))
8375 continue;
8376 accumulate_vtbl_inits (vbase, vbase, TYPE_BINFO (t), vtable, t, &v);
8379 if (BINFO_VTABLE (TYPE_BINFO (t)))
8380 initialize_vtable (TYPE_BINFO (t), v);
8383 /* Initialize the vtable for BINFO with the INITS. */
8385 static void
8386 initialize_vtable (tree binfo, vec<constructor_elt, va_gc> *inits)
8388 tree decl;
8390 layout_vtable_decl (binfo, vec_safe_length (inits));
8391 decl = get_vtbl_decl_for_binfo (binfo);
8392 initialize_artificial_var (decl, inits);
8393 dump_vtable (BINFO_TYPE (binfo), binfo, decl);
8396 /* Build the VTT (virtual table table) for T.
8397 A class requires a VTT if it has virtual bases.
8399 This holds
8400 1 - primary virtual pointer for complete object T
8401 2 - secondary VTTs for each direct non-virtual base of T which requires a
8403 3 - secondary virtual pointers for each direct or indirect base of T which
8404 has virtual bases or is reachable via a virtual path from T.
8405 4 - secondary VTTs for each direct or indirect virtual base of T.
8407 Secondary VTTs look like complete object VTTs without part 4. */
8409 static void
8410 build_vtt (tree t)
8412 tree type;
8413 tree vtt;
8414 tree index;
8415 vec<constructor_elt, va_gc> *inits;
8417 /* Build up the initializers for the VTT. */
8418 inits = NULL;
8419 index = size_zero_node;
8420 build_vtt_inits (TYPE_BINFO (t), t, &inits, &index);
8422 /* If we didn't need a VTT, we're done. */
8423 if (!inits)
8424 return;
8426 /* Figure out the type of the VTT. */
8427 type = build_array_of_n_type (const_ptr_type_node,
8428 inits->length ());
8430 /* Now, build the VTT object itself. */
8431 vtt = build_vtable (t, mangle_vtt_for_type (t), type);
8432 initialize_artificial_var (vtt, inits);
8433 /* Add the VTT to the vtables list. */
8434 DECL_CHAIN (vtt) = DECL_CHAIN (CLASSTYPE_VTABLES (t));
8435 DECL_CHAIN (CLASSTYPE_VTABLES (t)) = vtt;
8437 dump_vtt (t, vtt);
8440 /* When building a secondary VTT, BINFO_VTABLE is set to a TREE_LIST with
8441 PURPOSE the RTTI_BINFO, VALUE the real vtable pointer for this binfo,
8442 and CHAIN the vtable pointer for this binfo after construction is
8443 complete. VALUE can also be another BINFO, in which case we recurse. */
8445 static tree
8446 binfo_ctor_vtable (tree binfo)
8448 tree vt;
8450 while (1)
8452 vt = BINFO_VTABLE (binfo);
8453 if (TREE_CODE (vt) == TREE_LIST)
8454 vt = TREE_VALUE (vt);
8455 if (TREE_CODE (vt) == TREE_BINFO)
8456 binfo = vt;
8457 else
8458 break;
8461 return vt;
8464 /* Data for secondary VTT initialization. */
8465 typedef struct secondary_vptr_vtt_init_data_s
8467 /* Is this the primary VTT? */
8468 bool top_level_p;
8470 /* Current index into the VTT. */
8471 tree index;
8473 /* Vector of initializers built up. */
8474 vec<constructor_elt, va_gc> *inits;
8476 /* The type being constructed by this secondary VTT. */
8477 tree type_being_constructed;
8478 } secondary_vptr_vtt_init_data;
8480 /* Recursively build the VTT-initializer for BINFO (which is in the
8481 hierarchy dominated by T). INITS points to the end of the initializer
8482 list to date. INDEX is the VTT index where the next element will be
8483 replaced. Iff BINFO is the binfo for T, this is the top level VTT (i.e.
8484 not a subvtt for some base of T). When that is so, we emit the sub-VTTs
8485 for virtual bases of T. When it is not so, we build the constructor
8486 vtables for the BINFO-in-T variant. */
8488 static void
8489 build_vtt_inits (tree binfo, tree t, vec<constructor_elt, va_gc> **inits,
8490 tree *index)
8492 int i;
8493 tree b;
8494 tree init;
8495 secondary_vptr_vtt_init_data data;
8496 int top_level_p = SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), t);
8498 /* We only need VTTs for subobjects with virtual bases. */
8499 if (!CLASSTYPE_VBASECLASSES (BINFO_TYPE (binfo)))
8500 return;
8502 /* We need to use a construction vtable if this is not the primary
8503 VTT. */
8504 if (!top_level_p)
8506 build_ctor_vtbl_group (binfo, t);
8508 /* Record the offset in the VTT where this sub-VTT can be found. */
8509 BINFO_SUBVTT_INDEX (binfo) = *index;
8512 /* Add the address of the primary vtable for the complete object. */
8513 init = binfo_ctor_vtable (binfo);
8514 CONSTRUCTOR_APPEND_ELT (*inits, NULL_TREE, init);
8515 if (top_level_p)
8517 gcc_assert (!BINFO_VPTR_INDEX (binfo));
8518 BINFO_VPTR_INDEX (binfo) = *index;
8520 *index = size_binop (PLUS_EXPR, *index, TYPE_SIZE_UNIT (ptr_type_node));
8522 /* Recursively add the secondary VTTs for non-virtual bases. */
8523 for (i = 0; BINFO_BASE_ITERATE (binfo, i, b); ++i)
8524 if (!BINFO_VIRTUAL_P (b))
8525 build_vtt_inits (b, t, inits, index);
8527 /* Add secondary virtual pointers for all subobjects of BINFO with
8528 either virtual bases or reachable along a virtual path, except
8529 subobjects that are non-virtual primary bases. */
8530 data.top_level_p = top_level_p;
8531 data.index = *index;
8532 data.inits = *inits;
8533 data.type_being_constructed = BINFO_TYPE (binfo);
8535 dfs_walk_once (binfo, dfs_build_secondary_vptr_vtt_inits, NULL, &data);
8537 *index = data.index;
8539 /* data.inits might have grown as we added secondary virtual pointers.
8540 Make sure our caller knows about the new vector. */
8541 *inits = data.inits;
8543 if (top_level_p)
8544 /* Add the secondary VTTs for virtual bases in inheritance graph
8545 order. */
8546 for (b = TYPE_BINFO (BINFO_TYPE (binfo)); b; b = TREE_CHAIN (b))
8548 if (!BINFO_VIRTUAL_P (b))
8549 continue;
8551 build_vtt_inits (b, t, inits, index);
8553 else
8554 /* Remove the ctor vtables we created. */
8555 dfs_walk_all (binfo, dfs_fixup_binfo_vtbls, NULL, binfo);
8558 /* Called from build_vtt_inits via dfs_walk. BINFO is the binfo for the base
8559 in most derived. DATA is a SECONDARY_VPTR_VTT_INIT_DATA structure. */
8561 static tree
8562 dfs_build_secondary_vptr_vtt_inits (tree binfo, void *data_)
8564 secondary_vptr_vtt_init_data *data = (secondary_vptr_vtt_init_data *)data_;
8566 /* We don't care about bases that don't have vtables. */
8567 if (!TYPE_VFIELD (BINFO_TYPE (binfo)))
8568 return dfs_skip_bases;
8570 /* We're only interested in proper subobjects of the type being
8571 constructed. */
8572 if (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), data->type_being_constructed))
8573 return NULL_TREE;
8575 /* We're only interested in bases with virtual bases or reachable
8576 via a virtual path from the type being constructed. */
8577 if (!(CLASSTYPE_VBASECLASSES (BINFO_TYPE (binfo))
8578 || binfo_via_virtual (binfo, data->type_being_constructed)))
8579 return dfs_skip_bases;
8581 /* We're not interested in non-virtual primary bases. */
8582 if (!BINFO_VIRTUAL_P (binfo) && BINFO_PRIMARY_P (binfo))
8583 return NULL_TREE;
8585 /* Record the index where this secondary vptr can be found. */
8586 if (data->top_level_p)
8588 gcc_assert (!BINFO_VPTR_INDEX (binfo));
8589 BINFO_VPTR_INDEX (binfo) = data->index;
8591 if (BINFO_VIRTUAL_P (binfo))
8593 /* It's a primary virtual base, and this is not a
8594 construction vtable. Find the base this is primary of in
8595 the inheritance graph, and use that base's vtable
8596 now. */
8597 while (BINFO_PRIMARY_P (binfo))
8598 binfo = BINFO_INHERITANCE_CHAIN (binfo);
8602 /* Add the initializer for the secondary vptr itself. */
8603 CONSTRUCTOR_APPEND_ELT (data->inits, NULL_TREE, binfo_ctor_vtable (binfo));
8605 /* Advance the vtt index. */
8606 data->index = size_binop (PLUS_EXPR, data->index,
8607 TYPE_SIZE_UNIT (ptr_type_node));
8609 return NULL_TREE;
8612 /* Called from build_vtt_inits via dfs_walk. After building
8613 constructor vtables and generating the sub-vtt from them, we need
8614 to restore the BINFO_VTABLES that were scribbled on. DATA is the
8615 binfo of the base whose sub vtt was generated. */
8617 static tree
8618 dfs_fixup_binfo_vtbls (tree binfo, void* data)
8620 tree vtable = BINFO_VTABLE (binfo);
8622 if (!TYPE_CONTAINS_VPTR_P (BINFO_TYPE (binfo)))
8623 /* If this class has no vtable, none of its bases do. */
8624 return dfs_skip_bases;
8626 if (!vtable)
8627 /* This might be a primary base, so have no vtable in this
8628 hierarchy. */
8629 return NULL_TREE;
8631 /* If we scribbled the construction vtable vptr into BINFO, clear it
8632 out now. */
8633 if (TREE_CODE (vtable) == TREE_LIST
8634 && (TREE_PURPOSE (vtable) == (tree) data))
8635 BINFO_VTABLE (binfo) = TREE_CHAIN (vtable);
8637 return NULL_TREE;
8640 /* Build the construction vtable group for BINFO which is in the
8641 hierarchy dominated by T. */
8643 static void
8644 build_ctor_vtbl_group (tree binfo, tree t)
8646 tree type;
8647 tree vtbl;
8648 tree id;
8649 tree vbase;
8650 vec<constructor_elt, va_gc> *v;
8652 /* See if we've already created this construction vtable group. */
8653 id = mangle_ctor_vtbl_for_type (t, binfo);
8654 if (IDENTIFIER_GLOBAL_VALUE (id))
8655 return;
8657 gcc_assert (!SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), t));
8658 /* Build a version of VTBL (with the wrong type) for use in
8659 constructing the addresses of secondary vtables in the
8660 construction vtable group. */
8661 vtbl = build_vtable (t, id, ptr_type_node);
8662 DECL_CONSTRUCTION_VTABLE_P (vtbl) = 1;
8663 /* Don't export construction vtables from shared libraries. Even on
8664 targets that don't support hidden visibility, this tells
8665 can_refer_decl_in_current_unit_p not to assume that it's safe to
8666 access from a different compilation unit (bz 54314). */
8667 DECL_VISIBILITY (vtbl) = VISIBILITY_HIDDEN;
8668 DECL_VISIBILITY_SPECIFIED (vtbl) = true;
8670 v = NULL;
8671 accumulate_vtbl_inits (binfo, TYPE_BINFO (TREE_TYPE (binfo)),
8672 binfo, vtbl, t, &v);
8674 /* Add the vtables for each of our virtual bases using the vbase in T
8675 binfo. */
8676 for (vbase = TYPE_BINFO (BINFO_TYPE (binfo));
8677 vbase;
8678 vbase = TREE_CHAIN (vbase))
8680 tree b;
8682 if (!BINFO_VIRTUAL_P (vbase))
8683 continue;
8684 b = copied_binfo (vbase, binfo);
8686 accumulate_vtbl_inits (b, vbase, binfo, vtbl, t, &v);
8689 /* Figure out the type of the construction vtable. */
8690 type = build_array_of_n_type (vtable_entry_type, v->length ());
8691 layout_type (type);
8692 TREE_TYPE (vtbl) = type;
8693 DECL_SIZE (vtbl) = DECL_SIZE_UNIT (vtbl) = NULL_TREE;
8694 layout_decl (vtbl, 0);
8696 /* Initialize the construction vtable. */
8697 CLASSTYPE_VTABLES (t) = chainon (CLASSTYPE_VTABLES (t), vtbl);
8698 initialize_artificial_var (vtbl, v);
8699 dump_vtable (t, binfo, vtbl);
8702 /* Add the vtbl initializers for BINFO (and its bases other than
8703 non-virtual primaries) to the list of INITS. BINFO is in the
8704 hierarchy dominated by T. RTTI_BINFO is the binfo within T of
8705 the constructor the vtbl inits should be accumulated for. (If this
8706 is the complete object vtbl then RTTI_BINFO will be TYPE_BINFO (T).)
8707 ORIG_BINFO is the binfo for this object within BINFO_TYPE (RTTI_BINFO).
8708 BINFO is the active base equivalent of ORIG_BINFO in the inheritance
8709 graph of T. Both BINFO and ORIG_BINFO will have the same BINFO_TYPE,
8710 but are not necessarily the same in terms of layout. */
8712 static void
8713 accumulate_vtbl_inits (tree binfo,
8714 tree orig_binfo,
8715 tree rtti_binfo,
8716 tree vtbl,
8717 tree t,
8718 vec<constructor_elt, va_gc> **inits)
8720 int i;
8721 tree base_binfo;
8722 int ctor_vtbl_p = !SAME_BINFO_TYPE_P (BINFO_TYPE (rtti_binfo), t);
8724 gcc_assert (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), BINFO_TYPE (orig_binfo)));
8726 /* If it doesn't have a vptr, we don't do anything. */
8727 if (!TYPE_CONTAINS_VPTR_P (BINFO_TYPE (binfo)))
8728 return;
8730 /* If we're building a construction vtable, we're not interested in
8731 subobjects that don't require construction vtables. */
8732 if (ctor_vtbl_p
8733 && !CLASSTYPE_VBASECLASSES (BINFO_TYPE (binfo))
8734 && !binfo_via_virtual (orig_binfo, BINFO_TYPE (rtti_binfo)))
8735 return;
8737 /* Build the initializers for the BINFO-in-T vtable. */
8738 dfs_accumulate_vtbl_inits (binfo, orig_binfo, rtti_binfo, vtbl, t, inits);
8740 /* Walk the BINFO and its bases. We walk in preorder so that as we
8741 initialize each vtable we can figure out at what offset the
8742 secondary vtable lies from the primary vtable. We can't use
8743 dfs_walk here because we need to iterate through bases of BINFO
8744 and RTTI_BINFO simultaneously. */
8745 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
8747 /* Skip virtual bases. */
8748 if (BINFO_VIRTUAL_P (base_binfo))
8749 continue;
8750 accumulate_vtbl_inits (base_binfo,
8751 BINFO_BASE_BINFO (orig_binfo, i),
8752 rtti_binfo, vtbl, t,
8753 inits);
8757 /* Called from accumulate_vtbl_inits. Adds the initializers for the
8758 BINFO vtable to L. */
8760 static void
8761 dfs_accumulate_vtbl_inits (tree binfo,
8762 tree orig_binfo,
8763 tree rtti_binfo,
8764 tree orig_vtbl,
8765 tree t,
8766 vec<constructor_elt, va_gc> **l)
8768 tree vtbl = NULL_TREE;
8769 int ctor_vtbl_p = !SAME_BINFO_TYPE_P (BINFO_TYPE (rtti_binfo), t);
8770 int n_inits;
8772 if (ctor_vtbl_p
8773 && BINFO_VIRTUAL_P (orig_binfo) && BINFO_PRIMARY_P (orig_binfo))
8775 /* In the hierarchy of BINFO_TYPE (RTTI_BINFO), this is a
8776 primary virtual base. If it is not the same primary in
8777 the hierarchy of T, we'll need to generate a ctor vtable
8778 for it, to place at its location in T. If it is the same
8779 primary, we still need a VTT entry for the vtable, but it
8780 should point to the ctor vtable for the base it is a
8781 primary for within the sub-hierarchy of RTTI_BINFO.
8783 There are three possible cases:
8785 1) We are in the same place.
8786 2) We are a primary base within a lost primary virtual base of
8787 RTTI_BINFO.
8788 3) We are primary to something not a base of RTTI_BINFO. */
8790 tree b;
8791 tree last = NULL_TREE;
8793 /* First, look through the bases we are primary to for RTTI_BINFO
8794 or a virtual base. */
8795 b = binfo;
8796 while (BINFO_PRIMARY_P (b))
8798 b = BINFO_INHERITANCE_CHAIN (b);
8799 last = b;
8800 if (BINFO_VIRTUAL_P (b) || b == rtti_binfo)
8801 goto found;
8803 /* If we run out of primary links, keep looking down our
8804 inheritance chain; we might be an indirect primary. */
8805 for (b = last; b; b = BINFO_INHERITANCE_CHAIN (b))
8806 if (BINFO_VIRTUAL_P (b) || b == rtti_binfo)
8807 break;
8808 found:
8810 /* If we found RTTI_BINFO, this is case 1. If we found a virtual
8811 base B and it is a base of RTTI_BINFO, this is case 2. In
8812 either case, we share our vtable with LAST, i.e. the
8813 derived-most base within B of which we are a primary. */
8814 if (b == rtti_binfo
8815 || (b && binfo_for_vbase (BINFO_TYPE (b), BINFO_TYPE (rtti_binfo))))
8816 /* Just set our BINFO_VTABLE to point to LAST, as we may not have
8817 set LAST's BINFO_VTABLE yet. We'll extract the actual vptr in
8818 binfo_ctor_vtable after everything's been set up. */
8819 vtbl = last;
8821 /* Otherwise, this is case 3 and we get our own. */
8823 else if (!BINFO_NEW_VTABLE_MARKED (orig_binfo))
8824 return;
8826 n_inits = vec_safe_length (*l);
8828 if (!vtbl)
8830 tree index;
8831 int non_fn_entries;
8833 /* Add the initializer for this vtable. */
8834 build_vtbl_initializer (binfo, orig_binfo, t, rtti_binfo,
8835 &non_fn_entries, l);
8837 /* Figure out the position to which the VPTR should point. */
8838 vtbl = build1 (ADDR_EXPR, vtbl_ptr_type_node, orig_vtbl);
8839 index = size_binop (MULT_EXPR,
8840 TYPE_SIZE_UNIT (vtable_entry_type),
8841 size_int (non_fn_entries + n_inits));
8842 vtbl = fold_build_pointer_plus (vtbl, index);
8845 if (ctor_vtbl_p)
8846 /* For a construction vtable, we can't overwrite BINFO_VTABLE.
8847 So, we make a TREE_LIST. Later, dfs_fixup_binfo_vtbls will
8848 straighten this out. */
8849 BINFO_VTABLE (binfo) = tree_cons (rtti_binfo, vtbl, BINFO_VTABLE (binfo));
8850 else if (BINFO_PRIMARY_P (binfo) && BINFO_VIRTUAL_P (binfo))
8851 /* Throw away any unneeded intializers. */
8852 (*l)->truncate (n_inits);
8853 else
8854 /* For an ordinary vtable, set BINFO_VTABLE. */
8855 BINFO_VTABLE (binfo) = vtbl;
8858 static GTY(()) tree abort_fndecl_addr;
8860 /* Construct the initializer for BINFO's virtual function table. BINFO
8861 is part of the hierarchy dominated by T. If we're building a
8862 construction vtable, the ORIG_BINFO is the binfo we should use to
8863 find the actual function pointers to put in the vtable - but they
8864 can be overridden on the path to most-derived in the graph that
8865 ORIG_BINFO belongs. Otherwise,
8866 ORIG_BINFO should be the same as BINFO. The RTTI_BINFO is the
8867 BINFO that should be indicated by the RTTI information in the
8868 vtable; it will be a base class of T, rather than T itself, if we
8869 are building a construction vtable.
8871 The value returned is a TREE_LIST suitable for wrapping in a
8872 CONSTRUCTOR to use as the DECL_INITIAL for a vtable. If
8873 NON_FN_ENTRIES_P is not NULL, *NON_FN_ENTRIES_P is set to the
8874 number of non-function entries in the vtable.
8876 It might seem that this function should never be called with a
8877 BINFO for which BINFO_PRIMARY_P holds, the vtable for such a
8878 base is always subsumed by a derived class vtable. However, when
8879 we are building construction vtables, we do build vtables for
8880 primary bases; we need these while the primary base is being
8881 constructed. */
8883 static void
8884 build_vtbl_initializer (tree binfo,
8885 tree orig_binfo,
8886 tree t,
8887 tree rtti_binfo,
8888 int* non_fn_entries_p,
8889 vec<constructor_elt, va_gc> **inits)
8891 tree v;
8892 vtbl_init_data vid;
8893 unsigned ix, jx;
8894 tree vbinfo;
8895 vec<tree, va_gc> *vbases;
8896 constructor_elt *e;
8898 /* Initialize VID. */
8899 memset (&vid, 0, sizeof (vid));
8900 vid.binfo = binfo;
8901 vid.derived = t;
8902 vid.rtti_binfo = rtti_binfo;
8903 vid.primary_vtbl_p = SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), t);
8904 vid.ctor_vtbl_p = !SAME_BINFO_TYPE_P (BINFO_TYPE (rtti_binfo), t);
8905 vid.generate_vcall_entries = true;
8906 /* The first vbase or vcall offset is at index -3 in the vtable. */
8907 vid.index = ssize_int(-3 * TARGET_VTABLE_DATA_ENTRY_DISTANCE);
8909 /* Add entries to the vtable for RTTI. */
8910 build_rtti_vtbl_entries (binfo, &vid);
8912 /* Create an array for keeping track of the functions we've
8913 processed. When we see multiple functions with the same
8914 signature, we share the vcall offsets. */
8915 vec_alloc (vid.fns, 32);
8916 /* Add the vcall and vbase offset entries. */
8917 build_vcall_and_vbase_vtbl_entries (binfo, &vid);
8919 /* Clear BINFO_VTABLE_PATH_MARKED; it's set by
8920 build_vbase_offset_vtbl_entries. */
8921 for (vbases = CLASSTYPE_VBASECLASSES (t), ix = 0;
8922 vec_safe_iterate (vbases, ix, &vbinfo); ix++)
8923 BINFO_VTABLE_PATH_MARKED (vbinfo) = 0;
8925 /* If the target requires padding between data entries, add that now. */
8926 if (TARGET_VTABLE_DATA_ENTRY_DISTANCE > 1)
8928 int n_entries = vec_safe_length (vid.inits);
8930 vec_safe_grow (vid.inits, TARGET_VTABLE_DATA_ENTRY_DISTANCE * n_entries);
8932 /* Move data entries into their new positions and add padding
8933 after the new positions. Iterate backwards so we don't
8934 overwrite entries that we would need to process later. */
8935 for (ix = n_entries - 1;
8936 vid.inits->iterate (ix, &e);
8937 ix--)
8939 int j;
8940 int new_position = (TARGET_VTABLE_DATA_ENTRY_DISTANCE * ix
8941 + (TARGET_VTABLE_DATA_ENTRY_DISTANCE - 1));
8943 (*vid.inits)[new_position] = *e;
8945 for (j = 1; j < TARGET_VTABLE_DATA_ENTRY_DISTANCE; ++j)
8947 constructor_elt *f = &(*vid.inits)[new_position - j];
8948 f->index = NULL_TREE;
8949 f->value = build1 (NOP_EXPR, vtable_entry_type,
8950 null_pointer_node);
8955 if (non_fn_entries_p)
8956 *non_fn_entries_p = vec_safe_length (vid.inits);
8958 /* The initializers for virtual functions were built up in reverse
8959 order. Straighten them out and add them to the running list in one
8960 step. */
8961 jx = vec_safe_length (*inits);
8962 vec_safe_grow (*inits, jx + vid.inits->length ());
8964 for (ix = vid.inits->length () - 1;
8965 vid.inits->iterate (ix, &e);
8966 ix--, jx++)
8967 (**inits)[jx] = *e;
8969 /* Go through all the ordinary virtual functions, building up
8970 initializers. */
8971 for (v = BINFO_VIRTUALS (orig_binfo); v; v = TREE_CHAIN (v))
8973 tree delta;
8974 tree vcall_index;
8975 tree fn, fn_original;
8976 tree init = NULL_TREE;
8978 fn = BV_FN (v);
8979 fn_original = fn;
8980 if (DECL_THUNK_P (fn))
8982 if (!DECL_NAME (fn))
8983 finish_thunk (fn);
8984 if (THUNK_ALIAS (fn))
8986 fn = THUNK_ALIAS (fn);
8987 BV_FN (v) = fn;
8989 fn_original = THUNK_TARGET (fn);
8992 /* If the only definition of this function signature along our
8993 primary base chain is from a lost primary, this vtable slot will
8994 never be used, so just zero it out. This is important to avoid
8995 requiring extra thunks which cannot be generated with the function.
8997 We first check this in update_vtable_entry_for_fn, so we handle
8998 restored primary bases properly; we also need to do it here so we
8999 zero out unused slots in ctor vtables, rather than filling them
9000 with erroneous values (though harmless, apart from relocation
9001 costs). */
9002 if (BV_LOST_PRIMARY (v))
9003 init = size_zero_node;
9005 if (! init)
9007 /* Pull the offset for `this', and the function to call, out of
9008 the list. */
9009 delta = BV_DELTA (v);
9010 vcall_index = BV_VCALL_INDEX (v);
9012 gcc_assert (TREE_CODE (delta) == INTEGER_CST);
9013 gcc_assert (TREE_CODE (fn) == FUNCTION_DECL);
9015 /* You can't call an abstract virtual function; it's abstract.
9016 So, we replace these functions with __pure_virtual. */
9017 if (DECL_PURE_VIRTUAL_P (fn_original))
9019 fn = abort_fndecl;
9020 if (!TARGET_VTABLE_USES_DESCRIPTORS)
9022 if (abort_fndecl_addr == NULL)
9023 abort_fndecl_addr
9024 = fold_convert (vfunc_ptr_type_node,
9025 build_fold_addr_expr (fn));
9026 init = abort_fndecl_addr;
9029 /* Likewise for deleted virtuals. */
9030 else if (DECL_DELETED_FN (fn_original))
9032 fn = get_identifier ("__cxa_deleted_virtual");
9033 if (!get_global_value_if_present (fn, &fn))
9034 fn = push_library_fn (fn, (build_function_type_list
9035 (void_type_node, NULL_TREE)),
9036 NULL_TREE, ECF_NORETURN);
9037 if (!TARGET_VTABLE_USES_DESCRIPTORS)
9038 init = fold_convert (vfunc_ptr_type_node,
9039 build_fold_addr_expr (fn));
9041 else
9043 if (!integer_zerop (delta) || vcall_index)
9045 fn = make_thunk (fn, /*this_adjusting=*/1, delta, vcall_index);
9046 if (!DECL_NAME (fn))
9047 finish_thunk (fn);
9049 /* Take the address of the function, considering it to be of an
9050 appropriate generic type. */
9051 if (!TARGET_VTABLE_USES_DESCRIPTORS)
9052 init = fold_convert (vfunc_ptr_type_node,
9053 build_fold_addr_expr (fn));
9057 /* And add it to the chain of initializers. */
9058 if (TARGET_VTABLE_USES_DESCRIPTORS)
9060 int i;
9061 if (init == size_zero_node)
9062 for (i = 0; i < TARGET_VTABLE_USES_DESCRIPTORS; ++i)
9063 CONSTRUCTOR_APPEND_ELT (*inits, NULL_TREE, init);
9064 else
9065 for (i = 0; i < TARGET_VTABLE_USES_DESCRIPTORS; ++i)
9067 tree fdesc = build2 (FDESC_EXPR, vfunc_ptr_type_node,
9068 fn, build_int_cst (NULL_TREE, i));
9069 TREE_CONSTANT (fdesc) = 1;
9071 CONSTRUCTOR_APPEND_ELT (*inits, NULL_TREE, fdesc);
9074 else
9075 CONSTRUCTOR_APPEND_ELT (*inits, NULL_TREE, init);
9079 /* Adds to vid->inits the initializers for the vbase and vcall
9080 offsets in BINFO, which is in the hierarchy dominated by T. */
9082 static void
9083 build_vcall_and_vbase_vtbl_entries (tree binfo, vtbl_init_data* vid)
9085 tree b;
9087 /* If this is a derived class, we must first create entries
9088 corresponding to the primary base class. */
9089 b = get_primary_binfo (binfo);
9090 if (b)
9091 build_vcall_and_vbase_vtbl_entries (b, vid);
9093 /* Add the vbase entries for this base. */
9094 build_vbase_offset_vtbl_entries (binfo, vid);
9095 /* Add the vcall entries for this base. */
9096 build_vcall_offset_vtbl_entries (binfo, vid);
9099 /* Returns the initializers for the vbase offset entries in the vtable
9100 for BINFO (which is part of the class hierarchy dominated by T), in
9101 reverse order. VBASE_OFFSET_INDEX gives the vtable index
9102 where the next vbase offset will go. */
9104 static void
9105 build_vbase_offset_vtbl_entries (tree binfo, vtbl_init_data* vid)
9107 tree vbase;
9108 tree t;
9109 tree non_primary_binfo;
9111 /* If there are no virtual baseclasses, then there is nothing to
9112 do. */
9113 if (!CLASSTYPE_VBASECLASSES (BINFO_TYPE (binfo)))
9114 return;
9116 t = vid->derived;
9118 /* We might be a primary base class. Go up the inheritance hierarchy
9119 until we find the most derived class of which we are a primary base:
9120 it is the offset of that which we need to use. */
9121 non_primary_binfo = binfo;
9122 while (BINFO_INHERITANCE_CHAIN (non_primary_binfo))
9124 tree b;
9126 /* If we have reached a virtual base, then it must be a primary
9127 base (possibly multi-level) of vid->binfo, or we wouldn't
9128 have called build_vcall_and_vbase_vtbl_entries for it. But it
9129 might be a lost primary, so just skip down to vid->binfo. */
9130 if (BINFO_VIRTUAL_P (non_primary_binfo))
9132 non_primary_binfo = vid->binfo;
9133 break;
9136 b = BINFO_INHERITANCE_CHAIN (non_primary_binfo);
9137 if (get_primary_binfo (b) != non_primary_binfo)
9138 break;
9139 non_primary_binfo = b;
9142 /* Go through the virtual bases, adding the offsets. */
9143 for (vbase = TYPE_BINFO (BINFO_TYPE (binfo));
9144 vbase;
9145 vbase = TREE_CHAIN (vbase))
9147 tree b;
9148 tree delta;
9150 if (!BINFO_VIRTUAL_P (vbase))
9151 continue;
9153 /* Find the instance of this virtual base in the complete
9154 object. */
9155 b = copied_binfo (vbase, binfo);
9157 /* If we've already got an offset for this virtual base, we
9158 don't need another one. */
9159 if (BINFO_VTABLE_PATH_MARKED (b))
9160 continue;
9161 BINFO_VTABLE_PATH_MARKED (b) = 1;
9163 /* Figure out where we can find this vbase offset. */
9164 delta = size_binop (MULT_EXPR,
9165 vid->index,
9166 convert (ssizetype,
9167 TYPE_SIZE_UNIT (vtable_entry_type)));
9168 if (vid->primary_vtbl_p)
9169 BINFO_VPTR_FIELD (b) = delta;
9171 if (binfo != TYPE_BINFO (t))
9172 /* The vbase offset had better be the same. */
9173 gcc_assert (tree_int_cst_equal (delta, BINFO_VPTR_FIELD (vbase)));
9175 /* The next vbase will come at a more negative offset. */
9176 vid->index = size_binop (MINUS_EXPR, vid->index,
9177 ssize_int (TARGET_VTABLE_DATA_ENTRY_DISTANCE));
9179 /* The initializer is the delta from BINFO to this virtual base.
9180 The vbase offsets go in reverse inheritance-graph order, and
9181 we are walking in inheritance graph order so these end up in
9182 the right order. */
9183 delta = size_diffop_loc (input_location,
9184 BINFO_OFFSET (b), BINFO_OFFSET (non_primary_binfo));
9186 CONSTRUCTOR_APPEND_ELT (vid->inits, NULL_TREE,
9187 fold_build1_loc (input_location, NOP_EXPR,
9188 vtable_entry_type, delta));
9192 /* Adds the initializers for the vcall offset entries in the vtable
9193 for BINFO (which is part of the class hierarchy dominated by VID->DERIVED)
9194 to VID->INITS. */
9196 static void
9197 build_vcall_offset_vtbl_entries (tree binfo, vtbl_init_data* vid)
9199 /* We only need these entries if this base is a virtual base. We
9200 compute the indices -- but do not add to the vtable -- when
9201 building the main vtable for a class. */
9202 if (binfo == TYPE_BINFO (vid->derived)
9203 || (BINFO_VIRTUAL_P (binfo)
9204 /* If BINFO is RTTI_BINFO, then (since BINFO does not
9205 correspond to VID->DERIVED), we are building a primary
9206 construction virtual table. Since this is a primary
9207 virtual table, we do not need the vcall offsets for
9208 BINFO. */
9209 && binfo != vid->rtti_binfo))
9211 /* We need a vcall offset for each of the virtual functions in this
9212 vtable. For example:
9214 class A { virtual void f (); };
9215 class B1 : virtual public A { virtual void f (); };
9216 class B2 : virtual public A { virtual void f (); };
9217 class C: public B1, public B2 { virtual void f (); };
9219 A C object has a primary base of B1, which has a primary base of A. A
9220 C also has a secondary base of B2, which no longer has a primary base
9221 of A. So the B2-in-C construction vtable needs a secondary vtable for
9222 A, which will adjust the A* to a B2* to call f. We have no way of
9223 knowing what (or even whether) this offset will be when we define B2,
9224 so we store this "vcall offset" in the A sub-vtable and look it up in
9225 a "virtual thunk" for B2::f.
9227 We need entries for all the functions in our primary vtable and
9228 in our non-virtual bases' secondary vtables. */
9229 vid->vbase = binfo;
9230 /* If we are just computing the vcall indices -- but do not need
9231 the actual entries -- not that. */
9232 if (!BINFO_VIRTUAL_P (binfo))
9233 vid->generate_vcall_entries = false;
9234 /* Now, walk through the non-virtual bases, adding vcall offsets. */
9235 add_vcall_offset_vtbl_entries_r (binfo, vid);
9239 /* Build vcall offsets, starting with those for BINFO. */
9241 static void
9242 add_vcall_offset_vtbl_entries_r (tree binfo, vtbl_init_data* vid)
9244 int i;
9245 tree primary_binfo;
9246 tree base_binfo;
9248 /* Don't walk into virtual bases -- except, of course, for the
9249 virtual base for which we are building vcall offsets. Any
9250 primary virtual base will have already had its offsets generated
9251 through the recursion in build_vcall_and_vbase_vtbl_entries. */
9252 if (BINFO_VIRTUAL_P (binfo) && vid->vbase != binfo)
9253 return;
9255 /* If BINFO has a primary base, process it first. */
9256 primary_binfo = get_primary_binfo (binfo);
9257 if (primary_binfo)
9258 add_vcall_offset_vtbl_entries_r (primary_binfo, vid);
9260 /* Add BINFO itself to the list. */
9261 add_vcall_offset_vtbl_entries_1 (binfo, vid);
9263 /* Scan the non-primary bases of BINFO. */
9264 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
9265 if (base_binfo != primary_binfo)
9266 add_vcall_offset_vtbl_entries_r (base_binfo, vid);
9269 /* Called from build_vcall_offset_vtbl_entries_r. */
9271 static void
9272 add_vcall_offset_vtbl_entries_1 (tree binfo, vtbl_init_data* vid)
9274 /* Make entries for the rest of the virtuals. */
9275 if (abi_version_at_least (2))
9277 tree orig_fn;
9279 /* The ABI requires that the methods be processed in declaration
9280 order. G++ 3.2 used the order in the vtable. */
9281 for (orig_fn = TYPE_METHODS (BINFO_TYPE (binfo));
9282 orig_fn;
9283 orig_fn = DECL_CHAIN (orig_fn))
9284 if (DECL_VINDEX (orig_fn))
9285 add_vcall_offset (orig_fn, binfo, vid);
9287 else
9289 tree derived_virtuals;
9290 tree base_virtuals;
9291 tree orig_virtuals;
9292 /* If BINFO is a primary base, the most derived class which has
9293 BINFO as a primary base; otherwise, just BINFO. */
9294 tree non_primary_binfo;
9296 /* We might be a primary base class. Go up the inheritance hierarchy
9297 until we find the most derived class of which we are a primary base:
9298 it is the BINFO_VIRTUALS there that we need to consider. */
9299 non_primary_binfo = binfo;
9300 while (BINFO_INHERITANCE_CHAIN (non_primary_binfo))
9302 tree b;
9304 /* If we have reached a virtual base, then it must be vid->vbase,
9305 because we ignore other virtual bases in
9306 add_vcall_offset_vtbl_entries_r. In turn, it must be a primary
9307 base (possibly multi-level) of vid->binfo, or we wouldn't
9308 have called build_vcall_and_vbase_vtbl_entries for it. But it
9309 might be a lost primary, so just skip down to vid->binfo. */
9310 if (BINFO_VIRTUAL_P (non_primary_binfo))
9312 gcc_assert (non_primary_binfo == vid->vbase);
9313 non_primary_binfo = vid->binfo;
9314 break;
9317 b = BINFO_INHERITANCE_CHAIN (non_primary_binfo);
9318 if (get_primary_binfo (b) != non_primary_binfo)
9319 break;
9320 non_primary_binfo = b;
9323 if (vid->ctor_vtbl_p)
9324 /* For a ctor vtable we need the equivalent binfo within the hierarchy
9325 where rtti_binfo is the most derived type. */
9326 non_primary_binfo
9327 = original_binfo (non_primary_binfo, vid->rtti_binfo);
9329 for (base_virtuals = BINFO_VIRTUALS (binfo),
9330 derived_virtuals = BINFO_VIRTUALS (non_primary_binfo),
9331 orig_virtuals = BINFO_VIRTUALS (TYPE_BINFO (BINFO_TYPE (binfo)));
9332 base_virtuals;
9333 base_virtuals = TREE_CHAIN (base_virtuals),
9334 derived_virtuals = TREE_CHAIN (derived_virtuals),
9335 orig_virtuals = TREE_CHAIN (orig_virtuals))
9337 tree orig_fn;
9339 /* Find the declaration that originally caused this function to
9340 be present in BINFO_TYPE (binfo). */
9341 orig_fn = BV_FN (orig_virtuals);
9343 /* When processing BINFO, we only want to generate vcall slots for
9344 function slots introduced in BINFO. So don't try to generate
9345 one if the function isn't even defined in BINFO. */
9346 if (!SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), DECL_CONTEXT (orig_fn)))
9347 continue;
9349 add_vcall_offset (orig_fn, binfo, vid);
9354 /* Add a vcall offset entry for ORIG_FN to the vtable. */
9356 static void
9357 add_vcall_offset (tree orig_fn, tree binfo, vtbl_init_data *vid)
9359 size_t i;
9360 tree vcall_offset;
9361 tree derived_entry;
9363 /* If there is already an entry for a function with the same
9364 signature as FN, then we do not need a second vcall offset.
9365 Check the list of functions already present in the derived
9366 class vtable. */
9367 FOR_EACH_VEC_SAFE_ELT (vid->fns, i, derived_entry)
9369 if (same_signature_p (derived_entry, orig_fn)
9370 /* We only use one vcall offset for virtual destructors,
9371 even though there are two virtual table entries. */
9372 || (DECL_DESTRUCTOR_P (derived_entry)
9373 && DECL_DESTRUCTOR_P (orig_fn)))
9374 return;
9377 /* If we are building these vcall offsets as part of building
9378 the vtable for the most derived class, remember the vcall
9379 offset. */
9380 if (vid->binfo == TYPE_BINFO (vid->derived))
9382 tree_pair_s elt = {orig_fn, vid->index};
9383 vec_safe_push (CLASSTYPE_VCALL_INDICES (vid->derived), elt);
9386 /* The next vcall offset will be found at a more negative
9387 offset. */
9388 vid->index = size_binop (MINUS_EXPR, vid->index,
9389 ssize_int (TARGET_VTABLE_DATA_ENTRY_DISTANCE));
9391 /* Keep track of this function. */
9392 vec_safe_push (vid->fns, orig_fn);
9394 if (vid->generate_vcall_entries)
9396 tree base;
9397 tree fn;
9399 /* Find the overriding function. */
9400 fn = find_final_overrider (vid->rtti_binfo, binfo, orig_fn);
9401 if (fn == error_mark_node)
9402 vcall_offset = build_zero_cst (vtable_entry_type);
9403 else
9405 base = TREE_VALUE (fn);
9407 /* The vbase we're working on is a primary base of
9408 vid->binfo. But it might be a lost primary, so its
9409 BINFO_OFFSET might be wrong, so we just use the
9410 BINFO_OFFSET from vid->binfo. */
9411 vcall_offset = size_diffop_loc (input_location,
9412 BINFO_OFFSET (base),
9413 BINFO_OFFSET (vid->binfo));
9414 vcall_offset = fold_build1_loc (input_location,
9415 NOP_EXPR, vtable_entry_type,
9416 vcall_offset);
9418 /* Add the initializer to the vtable. */
9419 CONSTRUCTOR_APPEND_ELT (vid->inits, NULL_TREE, vcall_offset);
9423 /* Return vtbl initializers for the RTTI entries corresponding to the
9424 BINFO's vtable. The RTTI entries should indicate the object given
9425 by VID->rtti_binfo. */
9427 static void
9428 build_rtti_vtbl_entries (tree binfo, vtbl_init_data* vid)
9430 tree b;
9431 tree t;
9432 tree offset;
9433 tree decl;
9434 tree init;
9436 t = BINFO_TYPE (vid->rtti_binfo);
9438 /* To find the complete object, we will first convert to our most
9439 primary base, and then add the offset in the vtbl to that value. */
9440 b = binfo;
9441 while (CLASSTYPE_HAS_PRIMARY_BASE_P (BINFO_TYPE (b))
9442 && !BINFO_LOST_PRIMARY_P (b))
9444 tree primary_base;
9446 primary_base = get_primary_binfo (b);
9447 gcc_assert (BINFO_PRIMARY_P (primary_base)
9448 && BINFO_INHERITANCE_CHAIN (primary_base) == b);
9449 b = primary_base;
9451 offset = size_diffop_loc (input_location,
9452 BINFO_OFFSET (vid->rtti_binfo), BINFO_OFFSET (b));
9454 /* The second entry is the address of the typeinfo object. */
9455 if (flag_rtti)
9456 decl = build_address (get_tinfo_decl (t));
9457 else
9458 decl = integer_zero_node;
9460 /* Convert the declaration to a type that can be stored in the
9461 vtable. */
9462 init = build_nop (vfunc_ptr_type_node, decl);
9463 CONSTRUCTOR_APPEND_ELT (vid->inits, NULL_TREE, init);
9465 /* Add the offset-to-top entry. It comes earlier in the vtable than
9466 the typeinfo entry. Convert the offset to look like a
9467 function pointer, so that we can put it in the vtable. */
9468 init = build_nop (vfunc_ptr_type_node, offset);
9469 CONSTRUCTOR_APPEND_ELT (vid->inits, NULL_TREE, init);
9472 /* TRUE iff TYPE is uniquely derived from PARENT. Ignores
9473 accessibility. */
9475 bool
9476 uniquely_derived_from_p (tree parent, tree type)
9478 tree base = lookup_base (type, parent, ba_unique, NULL, tf_none);
9479 return base && base != error_mark_node;
9482 /* TRUE iff TYPE is publicly & uniquely derived from PARENT. */
9484 bool
9485 publicly_uniquely_derived_p (tree parent, tree type)
9487 tree base = lookup_base (type, parent, ba_ignore_scope | ba_check,
9488 NULL, tf_none);
9489 return base && base != error_mark_node;
9492 /* CTX1 and CTX2 are declaration contexts. Return the innermost common
9493 class between them, if any. */
9495 tree
9496 common_enclosing_class (tree ctx1, tree ctx2)
9498 if (!TYPE_P (ctx1) || !TYPE_P (ctx2))
9499 return NULL_TREE;
9500 gcc_assert (ctx1 == TYPE_MAIN_VARIANT (ctx1)
9501 && ctx2 == TYPE_MAIN_VARIANT (ctx2));
9502 if (ctx1 == ctx2)
9503 return ctx1;
9504 for (tree t = ctx1; TYPE_P (t); t = TYPE_CONTEXT (t))
9505 TYPE_MARKED_P (t) = true;
9506 tree found = NULL_TREE;
9507 for (tree t = ctx2; TYPE_P (t); t = TYPE_CONTEXT (t))
9508 if (TYPE_MARKED_P (t))
9510 found = t;
9511 break;
9513 for (tree t = ctx1; TYPE_P (t); t = TYPE_CONTEXT (t))
9514 TYPE_MARKED_P (t) = false;
9515 return found;
9518 #include "gt-cp-class.h"