PR c++/77375
[official-gcc.git] / gcc / cp / class.c
blob7362c732646bf6af5f16fcad13d8fcb185de673e
1 /* Functions related to building classes and their related objects.
2 Copyright (C) 1987-2016 Free Software Foundation, Inc.
3 Contributed by Michael Tiemann (tiemann@cygnus.com)
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
10 any later version.
12 GCC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
22 /* High-level class interface. */
24 #include "config.h"
25 #include "system.h"
26 #include "coretypes.h"
27 #include "target.h"
28 #include "cp-tree.h"
29 #include "stringpool.h"
30 #include "cgraph.h"
31 #include "stor-layout.h"
32 #include "attribs.h"
33 #include "flags.h"
34 #include "toplev.h"
35 #include "convert.h"
36 #include "dumpfile.h"
37 #include "gimplify.h"
38 #include "intl.h"
40 /* The number of nested classes being processed. If we are not in the
41 scope of any class, this is zero. */
43 int current_class_depth;
45 /* In order to deal with nested classes, we keep a stack of classes.
46 The topmost entry is the innermost class, and is the entry at index
47 CURRENT_CLASS_DEPTH */
49 typedef struct class_stack_node {
50 /* The name of the class. */
51 tree name;
53 /* The _TYPE node for the class. */
54 tree type;
56 /* The access specifier pending for new declarations in the scope of
57 this class. */
58 tree access;
60 /* If were defining TYPE, the names used in this class. */
61 splay_tree names_used;
63 /* Nonzero if this class is no longer open, because of a call to
64 push_to_top_level. */
65 size_t hidden;
66 }* class_stack_node_t;
68 struct vtbl_init_data
70 /* The base for which we're building initializers. */
71 tree binfo;
72 /* The type of the most-derived type. */
73 tree derived;
74 /* The binfo for the dynamic type. This will be TYPE_BINFO (derived),
75 unless ctor_vtbl_p is true. */
76 tree rtti_binfo;
77 /* The negative-index vtable initializers built up so far. These
78 are in order from least negative index to most negative index. */
79 vec<constructor_elt, va_gc> *inits;
80 /* The binfo for the virtual base for which we're building
81 vcall offset initializers. */
82 tree vbase;
83 /* The functions in vbase for which we have already provided vcall
84 offsets. */
85 vec<tree, va_gc> *fns;
86 /* The vtable index of the next vcall or vbase offset. */
87 tree index;
88 /* Nonzero if we are building the initializer for the primary
89 vtable. */
90 int primary_vtbl_p;
91 /* Nonzero if we are building the initializer for a construction
92 vtable. */
93 int ctor_vtbl_p;
94 /* True when adding vcall offset entries to the vtable. False when
95 merely computing the indices. */
96 bool generate_vcall_entries;
99 /* The type of a function passed to walk_subobject_offsets. */
100 typedef int (*subobject_offset_fn) (tree, tree, splay_tree);
102 /* The stack itself. This is a dynamically resized array. The
103 number of elements allocated is CURRENT_CLASS_STACK_SIZE. */
104 static int current_class_stack_size;
105 static class_stack_node_t current_class_stack;
107 /* The size of the largest empty class seen in this translation unit. */
108 static GTY (()) tree sizeof_biggest_empty_class;
110 /* An array of all local classes present in this translation unit, in
111 declaration order. */
112 vec<tree, va_gc> *local_classes;
114 static tree get_vfield_name (tree);
115 static void finish_struct_anon (tree);
116 static tree get_vtable_name (tree);
117 static void get_basefndecls (tree, tree, vec<tree> *);
118 static int build_primary_vtable (tree, tree);
119 static int build_secondary_vtable (tree);
120 static void finish_vtbls (tree);
121 static void modify_vtable_entry (tree, tree, tree, tree, tree *);
122 static void finish_struct_bits (tree);
123 static int alter_access (tree, tree, tree);
124 static void handle_using_decl (tree, tree);
125 static tree dfs_modify_vtables (tree, void *);
126 static tree modify_all_vtables (tree, tree);
127 static void determine_primary_bases (tree);
128 static void finish_struct_methods (tree);
129 static void maybe_warn_about_overly_private_class (tree);
130 static int method_name_cmp (const void *, const void *);
131 static int resort_method_name_cmp (const void *, const void *);
132 static void add_implicitly_declared_members (tree, tree*, int, int);
133 static tree fixed_type_or_null (tree, int *, int *);
134 static tree build_simple_base_path (tree expr, tree binfo);
135 static tree build_vtbl_ref_1 (tree, tree);
136 static void build_vtbl_initializer (tree, tree, tree, tree, int *,
137 vec<constructor_elt, va_gc> **);
138 static int count_fields (tree);
139 static int add_fields_to_record_type (tree, struct sorted_fields_type*, int);
140 static void insert_into_classtype_sorted_fields (tree, tree, int);
141 static bool check_bitfield_decl (tree);
142 static bool check_field_decl (tree, tree, int *, int *);
143 static void check_field_decls (tree, tree *, int *, int *);
144 static tree *build_base_field (record_layout_info, tree, splay_tree, tree *);
145 static void build_base_fields (record_layout_info, splay_tree, tree *);
146 static void check_methods (tree);
147 static void remove_zero_width_bit_fields (tree);
148 static bool accessible_nvdtor_p (tree);
150 /* Used by find_flexarrays and related. */
151 struct flexmems_t;
152 static void find_flexarrays (tree, flexmems_t *);
153 static void diagnose_flexarrays (tree, const flexmems_t *);
154 static void check_flexarrays (tree, flexmems_t * = NULL);
155 static void check_bases (tree, int *, int *);
156 static void check_bases_and_members (tree);
157 static tree create_vtable_ptr (tree, tree *);
158 static void include_empty_classes (record_layout_info);
159 static void layout_class_type (tree, tree *);
160 static void propagate_binfo_offsets (tree, tree);
161 static void layout_virtual_bases (record_layout_info, splay_tree);
162 static void build_vbase_offset_vtbl_entries (tree, vtbl_init_data *);
163 static void add_vcall_offset_vtbl_entries_r (tree, vtbl_init_data *);
164 static void add_vcall_offset_vtbl_entries_1 (tree, vtbl_init_data *);
165 static void build_vcall_offset_vtbl_entries (tree, vtbl_init_data *);
166 static void add_vcall_offset (tree, tree, vtbl_init_data *);
167 static void layout_vtable_decl (tree, int);
168 static tree dfs_find_final_overrider_pre (tree, void *);
169 static tree dfs_find_final_overrider_post (tree, void *);
170 static tree find_final_overrider (tree, tree, tree);
171 static int make_new_vtable (tree, tree);
172 static tree get_primary_binfo (tree);
173 static int maybe_indent_hierarchy (FILE *, int, int);
174 static tree dump_class_hierarchy_r (FILE *, int, tree, tree, int);
175 static void dump_class_hierarchy (tree);
176 static void dump_class_hierarchy_1 (FILE *, int, tree);
177 static void dump_array (FILE *, tree);
178 static void dump_vtable (tree, tree, tree);
179 static void dump_vtt (tree, tree);
180 static void dump_thunk (FILE *, int, tree);
181 static tree build_vtable (tree, tree, tree);
182 static void initialize_vtable (tree, vec<constructor_elt, va_gc> *);
183 static void layout_nonempty_base_or_field (record_layout_info,
184 tree, tree, splay_tree);
185 static tree end_of_class (tree, int);
186 static bool layout_empty_base (record_layout_info, tree, tree, splay_tree);
187 static void accumulate_vtbl_inits (tree, tree, tree, tree, tree,
188 vec<constructor_elt, va_gc> **);
189 static void dfs_accumulate_vtbl_inits (tree, tree, tree, tree, tree,
190 vec<constructor_elt, va_gc> **);
191 static void build_rtti_vtbl_entries (tree, vtbl_init_data *);
192 static void build_vcall_and_vbase_vtbl_entries (tree, vtbl_init_data *);
193 static void clone_constructors_and_destructors (tree);
194 static tree build_clone (tree, tree);
195 static void update_vtable_entry_for_fn (tree, tree, tree, tree *, unsigned);
196 static void build_ctor_vtbl_group (tree, tree);
197 static void build_vtt (tree);
198 static tree binfo_ctor_vtable (tree);
199 static void build_vtt_inits (tree, tree, vec<constructor_elt, va_gc> **,
200 tree *);
201 static tree dfs_build_secondary_vptr_vtt_inits (tree, void *);
202 static tree dfs_fixup_binfo_vtbls (tree, void *);
203 static int record_subobject_offset (tree, tree, splay_tree);
204 static int check_subobject_offset (tree, tree, splay_tree);
205 static int walk_subobject_offsets (tree, subobject_offset_fn,
206 tree, splay_tree, tree, int);
207 static void record_subobject_offsets (tree, tree, splay_tree, bool);
208 static int layout_conflict_p (tree, tree, splay_tree, int);
209 static int splay_tree_compare_integer_csts (splay_tree_key k1,
210 splay_tree_key k2);
211 static void warn_about_ambiguous_bases (tree);
212 static bool type_requires_array_cookie (tree);
213 static bool base_derived_from (tree, tree);
214 static int empty_base_at_nonzero_offset_p (tree, tree, splay_tree);
215 static tree end_of_base (tree);
216 static tree get_vcall_index (tree, tree);
217 static bool type_maybe_constexpr_default_constructor (tree);
219 /* Variables shared between class.c and call.c. */
221 int n_vtables = 0;
222 int n_vtable_entries = 0;
223 int n_vtable_searches = 0;
224 int n_vtable_elems = 0;
225 int n_convert_harshness = 0;
226 int n_compute_conversion_costs = 0;
227 int n_inner_fields_searched = 0;
229 /* Return a COND_EXPR that executes TRUE_STMT if this execution of the
230 'structor is in charge of 'structing virtual bases, or FALSE_STMT
231 otherwise. */
233 tree
234 build_if_in_charge (tree true_stmt, tree false_stmt)
236 gcc_assert (DECL_HAS_IN_CHARGE_PARM_P (current_function_decl));
237 tree cmp = build2 (NE_EXPR, boolean_type_node,
238 current_in_charge_parm, integer_zero_node);
239 tree type = unlowered_expr_type (true_stmt);
240 if (VOID_TYPE_P (type))
241 type = unlowered_expr_type (false_stmt);
242 tree cond = build3 (COND_EXPR, type,
243 cmp, true_stmt, false_stmt);
244 return cond;
247 /* Convert to or from a base subobject. EXPR is an expression of type
248 `A' or `A*', an expression of type `B' or `B*' is returned. To
249 convert A to a base B, CODE is PLUS_EXPR and BINFO is the binfo for
250 the B base instance within A. To convert base A to derived B, CODE
251 is MINUS_EXPR and BINFO is the binfo for the A instance within B.
252 In this latter case, A must not be a morally virtual base of B.
253 NONNULL is true if EXPR is known to be non-NULL (this is only
254 needed when EXPR is of pointer type). CV qualifiers are preserved
255 from EXPR. */
257 tree
258 build_base_path (enum tree_code code,
259 tree expr,
260 tree binfo,
261 int nonnull,
262 tsubst_flags_t complain)
264 tree v_binfo = NULL_TREE;
265 tree d_binfo = NULL_TREE;
266 tree probe;
267 tree offset;
268 tree target_type;
269 tree null_test = NULL;
270 tree ptr_target_type;
271 int fixed_type_p;
272 int want_pointer = TYPE_PTR_P (TREE_TYPE (expr));
273 bool has_empty = false;
274 bool virtual_access;
275 bool rvalue = false;
277 if (expr == error_mark_node || binfo == error_mark_node || !binfo)
278 return error_mark_node;
280 for (probe = binfo; probe; probe = BINFO_INHERITANCE_CHAIN (probe))
282 d_binfo = probe;
283 if (is_empty_class (BINFO_TYPE (probe)))
284 has_empty = true;
285 if (!v_binfo && BINFO_VIRTUAL_P (probe))
286 v_binfo = probe;
289 probe = TYPE_MAIN_VARIANT (TREE_TYPE (expr));
290 if (want_pointer)
291 probe = TYPE_MAIN_VARIANT (TREE_TYPE (probe));
293 if (code == PLUS_EXPR
294 && !SAME_BINFO_TYPE_P (BINFO_TYPE (d_binfo), probe))
296 /* This can happen when adjust_result_of_qualified_name_lookup can't
297 find a unique base binfo in a call to a member function. We
298 couldn't give the diagnostic then since we might have been calling
299 a static member function, so we do it now. */
300 if (complain & tf_error)
302 tree base = lookup_base (probe, BINFO_TYPE (d_binfo),
303 ba_unique, NULL, complain);
304 gcc_assert (base == error_mark_node);
306 return error_mark_node;
309 gcc_assert ((code == MINUS_EXPR
310 && SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), probe))
311 || code == PLUS_EXPR);
313 if (binfo == d_binfo)
314 /* Nothing to do. */
315 return expr;
317 if (code == MINUS_EXPR && v_binfo)
319 if (complain & tf_error)
321 if (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), BINFO_TYPE (v_binfo)))
323 if (want_pointer)
324 error ("cannot convert from pointer to base class %qT to "
325 "pointer to derived class %qT because the base is "
326 "virtual", BINFO_TYPE (binfo), BINFO_TYPE (d_binfo));
327 else
328 error ("cannot convert from base class %qT to derived "
329 "class %qT because the base is virtual",
330 BINFO_TYPE (binfo), BINFO_TYPE (d_binfo));
332 else
334 if (want_pointer)
335 error ("cannot convert from pointer to base class %qT to "
336 "pointer to derived class %qT via virtual base %qT",
337 BINFO_TYPE (binfo), BINFO_TYPE (d_binfo),
338 BINFO_TYPE (v_binfo));
339 else
340 error ("cannot convert from base class %qT to derived "
341 "class %qT via virtual base %qT", BINFO_TYPE (binfo),
342 BINFO_TYPE (d_binfo), BINFO_TYPE (v_binfo));
345 return error_mark_node;
348 if (!want_pointer)
350 rvalue = !lvalue_p (expr);
351 /* This must happen before the call to save_expr. */
352 expr = cp_build_addr_expr (expr, complain);
354 else
355 expr = mark_rvalue_use (expr);
357 offset = BINFO_OFFSET (binfo);
358 fixed_type_p = resolves_to_fixed_type_p (expr, &nonnull);
359 target_type = code == PLUS_EXPR ? BINFO_TYPE (binfo) : BINFO_TYPE (d_binfo);
360 /* TARGET_TYPE has been extracted from BINFO, and, is therefore always
361 cv-unqualified. Extract the cv-qualifiers from EXPR so that the
362 expression returned matches the input. */
363 target_type = cp_build_qualified_type
364 (target_type, cp_type_quals (TREE_TYPE (TREE_TYPE (expr))));
365 ptr_target_type = build_pointer_type (target_type);
367 /* Do we need to look in the vtable for the real offset? */
368 virtual_access = (v_binfo && fixed_type_p <= 0);
370 /* Don't bother with the calculations inside sizeof; they'll ICE if the
371 source type is incomplete and the pointer value doesn't matter. In a
372 template (even in instantiate_non_dependent_expr), we don't have vtables
373 set up properly yet, and the value doesn't matter there either; we're
374 just interested in the result of overload resolution. */
375 if (cp_unevaluated_operand != 0
376 || in_template_function ())
378 expr = build_nop (ptr_target_type, expr);
379 goto indout;
382 /* If we're in an NSDMI, we don't have the full constructor context yet
383 that we need for converting to a virtual base, so just build a stub
384 CONVERT_EXPR and expand it later in bot_replace. */
385 if (virtual_access && fixed_type_p < 0
386 && current_scope () != current_function_decl)
388 expr = build1 (CONVERT_EXPR, ptr_target_type, expr);
389 CONVERT_EXPR_VBASE_PATH (expr) = true;
390 goto indout;
393 /* Do we need to check for a null pointer? */
394 if (want_pointer && !nonnull)
396 /* If we know the conversion will not actually change the value
397 of EXPR, then we can avoid testing the expression for NULL.
398 We have to avoid generating a COMPONENT_REF for a base class
399 field, because other parts of the compiler know that such
400 expressions are always non-NULL. */
401 if (!virtual_access && integer_zerop (offset))
402 return build_nop (ptr_target_type, expr);
403 null_test = error_mark_node;
406 /* Protect against multiple evaluation if necessary. */
407 if (TREE_SIDE_EFFECTS (expr) && (null_test || virtual_access))
408 expr = save_expr (expr);
410 /* Now that we've saved expr, build the real null test. */
411 if (null_test)
413 tree zero = cp_convert (TREE_TYPE (expr), nullptr_node, complain);
414 null_test = build2_loc (input_location, NE_EXPR, boolean_type_node,
415 expr, zero);
416 /* This is a compiler generated comparison, don't emit
417 e.g. -Wnonnull-compare warning for it. */
418 TREE_NO_WARNING (null_test) = 1;
421 /* If this is a simple base reference, express it as a COMPONENT_REF. */
422 if (code == PLUS_EXPR && !virtual_access
423 /* We don't build base fields for empty bases, and they aren't very
424 interesting to the optimizers anyway. */
425 && !has_empty)
427 expr = cp_build_indirect_ref (expr, RO_NULL, complain);
428 expr = build_simple_base_path (expr, binfo);
429 if (rvalue)
430 expr = move (expr);
431 if (want_pointer)
432 expr = build_address (expr);
433 target_type = TREE_TYPE (expr);
434 goto out;
437 if (virtual_access)
439 /* Going via virtual base V_BINFO. We need the static offset
440 from V_BINFO to BINFO, and the dynamic offset from D_BINFO to
441 V_BINFO. That offset is an entry in D_BINFO's vtable. */
442 tree v_offset;
444 if (fixed_type_p < 0 && in_base_initializer)
446 /* In a base member initializer, we cannot rely on the
447 vtable being set up. We have to indirect via the
448 vtt_parm. */
449 tree t;
451 t = TREE_TYPE (TYPE_VFIELD (current_class_type));
452 t = build_pointer_type (t);
453 v_offset = fold_convert (t, current_vtt_parm);
454 v_offset = cp_build_indirect_ref (v_offset, RO_NULL, complain);
456 else
458 tree t = expr;
459 if ((flag_sanitize & SANITIZE_VPTR) && fixed_type_p == 0)
461 t = cp_ubsan_maybe_instrument_cast_to_vbase (input_location,
462 probe, expr);
463 if (t == NULL_TREE)
464 t = expr;
466 v_offset = build_vfield_ref (cp_build_indirect_ref (t, RO_NULL,
467 complain),
468 TREE_TYPE (TREE_TYPE (expr)));
471 if (v_offset == error_mark_node)
472 return error_mark_node;
474 v_offset = fold_build_pointer_plus (v_offset, BINFO_VPTR_FIELD (v_binfo));
475 v_offset = build1 (NOP_EXPR,
476 build_pointer_type (ptrdiff_type_node),
477 v_offset);
478 v_offset = cp_build_indirect_ref (v_offset, RO_NULL, complain);
479 TREE_CONSTANT (v_offset) = 1;
481 offset = convert_to_integer (ptrdiff_type_node,
482 size_diffop_loc (input_location, offset,
483 BINFO_OFFSET (v_binfo)));
485 if (!integer_zerop (offset))
486 v_offset = build2 (code, ptrdiff_type_node, v_offset, offset);
488 if (fixed_type_p < 0)
489 /* Negative fixed_type_p means this is a constructor or destructor;
490 virtual base layout is fixed in in-charge [cd]tors, but not in
491 base [cd]tors. */
492 offset = build_if_in_charge
493 (convert_to_integer (ptrdiff_type_node, BINFO_OFFSET (binfo)),
494 v_offset);
495 else
496 offset = v_offset;
499 if (want_pointer)
500 target_type = ptr_target_type;
502 expr = build1 (NOP_EXPR, ptr_target_type, expr);
504 if (!integer_zerop (offset))
506 offset = fold_convert (sizetype, offset);
507 if (code == MINUS_EXPR)
508 offset = fold_build1_loc (input_location, NEGATE_EXPR, sizetype, offset);
509 expr = fold_build_pointer_plus (expr, offset);
511 else
512 null_test = NULL;
514 indout:
515 if (!want_pointer)
517 expr = cp_build_indirect_ref (expr, RO_NULL, complain);
518 if (rvalue)
519 expr = move (expr);
522 out:
523 if (null_test)
524 expr = fold_build3_loc (input_location, COND_EXPR, target_type, null_test, expr,
525 build_zero_cst (target_type));
527 return expr;
530 /* Subroutine of build_base_path; EXPR and BINFO are as in that function.
531 Perform a derived-to-base conversion by recursively building up a
532 sequence of COMPONENT_REFs to the appropriate base fields. */
534 static tree
535 build_simple_base_path (tree expr, tree binfo)
537 tree type = BINFO_TYPE (binfo);
538 tree d_binfo = BINFO_INHERITANCE_CHAIN (binfo);
539 tree field;
541 if (d_binfo == NULL_TREE)
543 tree temp;
545 gcc_assert (TYPE_MAIN_VARIANT (TREE_TYPE (expr)) == type);
547 /* Transform `(a, b).x' into `(*(a, &b)).x', `(a ? b : c).x'
548 into `(*(a ? &b : &c)).x', and so on. A COND_EXPR is only
549 an lvalue in the front end; only _DECLs and _REFs are lvalues
550 in the back end. */
551 temp = unary_complex_lvalue (ADDR_EXPR, expr);
552 if (temp)
553 expr = cp_build_indirect_ref (temp, RO_NULL, tf_warning_or_error);
555 return expr;
558 /* Recurse. */
559 expr = build_simple_base_path (expr, d_binfo);
561 for (field = TYPE_FIELDS (BINFO_TYPE (d_binfo));
562 field; field = DECL_CHAIN (field))
563 /* Is this the base field created by build_base_field? */
564 if (TREE_CODE (field) == FIELD_DECL
565 && DECL_FIELD_IS_BASE (field)
566 && TREE_TYPE (field) == type
567 /* If we're looking for a field in the most-derived class,
568 also check the field offset; we can have two base fields
569 of the same type if one is an indirect virtual base and one
570 is a direct non-virtual base. */
571 && (BINFO_INHERITANCE_CHAIN (d_binfo)
572 || tree_int_cst_equal (byte_position (field),
573 BINFO_OFFSET (binfo))))
575 /* We don't use build_class_member_access_expr here, as that
576 has unnecessary checks, and more importantly results in
577 recursive calls to dfs_walk_once. */
578 int type_quals = cp_type_quals (TREE_TYPE (expr));
580 expr = build3 (COMPONENT_REF,
581 cp_build_qualified_type (type, type_quals),
582 expr, field, NULL_TREE);
583 /* Mark the expression const or volatile, as appropriate.
584 Even though we've dealt with the type above, we still have
585 to mark the expression itself. */
586 if (type_quals & TYPE_QUAL_CONST)
587 TREE_READONLY (expr) = 1;
588 if (type_quals & TYPE_QUAL_VOLATILE)
589 TREE_THIS_VOLATILE (expr) = 1;
591 return expr;
594 /* Didn't find the base field?!? */
595 gcc_unreachable ();
598 /* Convert OBJECT to the base TYPE. OBJECT is an expression whose
599 type is a class type or a pointer to a class type. In the former
600 case, TYPE is also a class type; in the latter it is another
601 pointer type. If CHECK_ACCESS is true, an error message is emitted
602 if TYPE is inaccessible. If OBJECT has pointer type, the value is
603 assumed to be non-NULL. */
605 tree
606 convert_to_base (tree object, tree type, bool check_access, bool nonnull,
607 tsubst_flags_t complain)
609 tree binfo;
610 tree object_type;
612 if (TYPE_PTR_P (TREE_TYPE (object)))
614 object_type = TREE_TYPE (TREE_TYPE (object));
615 type = TREE_TYPE (type);
617 else
618 object_type = TREE_TYPE (object);
620 binfo = lookup_base (object_type, type, check_access ? ba_check : ba_unique,
621 NULL, complain);
622 if (!binfo || binfo == error_mark_node)
623 return error_mark_node;
625 return build_base_path (PLUS_EXPR, object, binfo, nonnull, complain);
628 /* EXPR is an expression with unqualified class type. BASE is a base
629 binfo of that class type. Returns EXPR, converted to the BASE
630 type. This function assumes that EXPR is the most derived class;
631 therefore virtual bases can be found at their static offsets. */
633 tree
634 convert_to_base_statically (tree expr, tree base)
636 tree expr_type;
638 expr_type = TREE_TYPE (expr);
639 if (!SAME_BINFO_TYPE_P (BINFO_TYPE (base), expr_type))
641 /* If this is a non-empty base, use a COMPONENT_REF. */
642 if (!is_empty_class (BINFO_TYPE (base)))
643 return build_simple_base_path (expr, base);
645 /* We use fold_build2 and fold_convert below to simplify the trees
646 provided to the optimizers. It is not safe to call these functions
647 when processing a template because they do not handle C++-specific
648 trees. */
649 gcc_assert (!processing_template_decl);
650 expr = cp_build_addr_expr (expr, tf_warning_or_error);
651 if (!integer_zerop (BINFO_OFFSET (base)))
652 expr = fold_build_pointer_plus_loc (input_location,
653 expr, BINFO_OFFSET (base));
654 expr = fold_convert (build_pointer_type (BINFO_TYPE (base)), expr);
655 expr = build_fold_indirect_ref_loc (input_location, expr);
658 return expr;
662 tree
663 build_vfield_ref (tree datum, tree type)
665 tree vfield, vcontext;
667 if (datum == error_mark_node
668 /* Can happen in case of duplicate base types (c++/59082). */
669 || !TYPE_VFIELD (type))
670 return error_mark_node;
672 /* First, convert to the requested type. */
673 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (datum), type))
674 datum = convert_to_base (datum, type, /*check_access=*/false,
675 /*nonnull=*/true, tf_warning_or_error);
677 /* Second, the requested type may not be the owner of its own vptr.
678 If not, convert to the base class that owns it. We cannot use
679 convert_to_base here, because VCONTEXT may appear more than once
680 in the inheritance hierarchy of TYPE, and thus direct conversion
681 between the types may be ambiguous. Following the path back up
682 one step at a time via primary bases avoids the problem. */
683 vfield = TYPE_VFIELD (type);
684 vcontext = DECL_CONTEXT (vfield);
685 while (!same_type_ignoring_top_level_qualifiers_p (vcontext, type))
687 datum = build_simple_base_path (datum, CLASSTYPE_PRIMARY_BINFO (type));
688 type = TREE_TYPE (datum);
691 return build3 (COMPONENT_REF, TREE_TYPE (vfield), datum, vfield, NULL_TREE);
694 /* Given an object INSTANCE, return an expression which yields the
695 vtable element corresponding to INDEX. There are many special
696 cases for INSTANCE which we take care of here, mainly to avoid
697 creating extra tree nodes when we don't have to. */
699 static tree
700 build_vtbl_ref_1 (tree instance, tree idx)
702 tree aref;
703 tree vtbl = NULL_TREE;
705 /* Try to figure out what a reference refers to, and
706 access its virtual function table directly. */
708 int cdtorp = 0;
709 tree fixed_type = fixed_type_or_null (instance, NULL, &cdtorp);
711 tree basetype = non_reference (TREE_TYPE (instance));
713 if (fixed_type && !cdtorp)
715 tree binfo = lookup_base (fixed_type, basetype,
716 ba_unique, NULL, tf_none);
717 if (binfo && binfo != error_mark_node)
718 vtbl = unshare_expr (BINFO_VTABLE (binfo));
721 if (!vtbl)
722 vtbl = build_vfield_ref (instance, basetype);
724 aref = build_array_ref (input_location, vtbl, idx);
725 TREE_CONSTANT (aref) |= TREE_CONSTANT (vtbl) && TREE_CONSTANT (idx);
727 return aref;
730 tree
731 build_vtbl_ref (tree instance, tree idx)
733 tree aref = build_vtbl_ref_1 (instance, idx);
735 return aref;
738 /* Given a stable object pointer INSTANCE_PTR, return an expression which
739 yields a function pointer corresponding to vtable element INDEX. */
741 tree
742 build_vfn_ref (tree instance_ptr, tree idx)
744 tree aref;
746 aref = build_vtbl_ref_1 (cp_build_indirect_ref (instance_ptr, RO_NULL,
747 tf_warning_or_error),
748 idx);
750 /* When using function descriptors, the address of the
751 vtable entry is treated as a function pointer. */
752 if (TARGET_VTABLE_USES_DESCRIPTORS)
753 aref = build1 (NOP_EXPR, TREE_TYPE (aref),
754 cp_build_addr_expr (aref, tf_warning_or_error));
756 /* Remember this as a method reference, for later devirtualization. */
757 aref = build3 (OBJ_TYPE_REF, TREE_TYPE (aref), aref, instance_ptr, idx);
759 return aref;
762 /* Return the name of the virtual function table (as an IDENTIFIER_NODE)
763 for the given TYPE. */
765 static tree
766 get_vtable_name (tree type)
768 return mangle_vtbl_for_type (type);
771 /* DECL is an entity associated with TYPE, like a virtual table or an
772 implicitly generated constructor. Determine whether or not DECL
773 should have external or internal linkage at the object file
774 level. This routine does not deal with COMDAT linkage and other
775 similar complexities; it simply sets TREE_PUBLIC if it possible for
776 entities in other translation units to contain copies of DECL, in
777 the abstract. */
779 void
780 set_linkage_according_to_type (tree /*type*/, tree decl)
782 TREE_PUBLIC (decl) = 1;
783 determine_visibility (decl);
786 /* Create a VAR_DECL for a primary or secondary vtable for CLASS_TYPE.
787 (For a secondary vtable for B-in-D, CLASS_TYPE should be D, not B.)
788 Use NAME for the name of the vtable, and VTABLE_TYPE for its type. */
790 static tree
791 build_vtable (tree class_type, tree name, tree vtable_type)
793 tree decl;
795 decl = build_lang_decl (VAR_DECL, name, vtable_type);
796 /* vtable names are already mangled; give them their DECL_ASSEMBLER_NAME
797 now to avoid confusion in mangle_decl. */
798 SET_DECL_ASSEMBLER_NAME (decl, name);
799 DECL_CONTEXT (decl) = class_type;
800 DECL_ARTIFICIAL (decl) = 1;
801 TREE_STATIC (decl) = 1;
802 TREE_READONLY (decl) = 1;
803 DECL_VIRTUAL_P (decl) = 1;
804 SET_DECL_ALIGN (decl, TARGET_VTABLE_ENTRY_ALIGN);
805 DECL_USER_ALIGN (decl) = true;
806 DECL_VTABLE_OR_VTT_P (decl) = 1;
807 set_linkage_according_to_type (class_type, decl);
808 /* The vtable has not been defined -- yet. */
809 DECL_EXTERNAL (decl) = 1;
810 DECL_NOT_REALLY_EXTERN (decl) = 1;
812 /* Mark the VAR_DECL node representing the vtable itself as a
813 "gratuitous" one, thereby forcing dwarfout.c to ignore it. It
814 is rather important that such things be ignored because any
815 effort to actually generate DWARF for them will run into
816 trouble when/if we encounter code like:
818 #pragma interface
819 struct S { virtual void member (); };
821 because the artificial declaration of the vtable itself (as
822 manufactured by the g++ front end) will say that the vtable is
823 a static member of `S' but only *after* the debug output for
824 the definition of `S' has already been output. This causes
825 grief because the DWARF entry for the definition of the vtable
826 will try to refer back to an earlier *declaration* of the
827 vtable as a static member of `S' and there won't be one. We
828 might be able to arrange to have the "vtable static member"
829 attached to the member list for `S' before the debug info for
830 `S' get written (which would solve the problem) but that would
831 require more intrusive changes to the g++ front end. */
832 DECL_IGNORED_P (decl) = 1;
834 return decl;
837 /* Get the VAR_DECL of the vtable for TYPE. TYPE need not be polymorphic,
838 or even complete. If this does not exist, create it. If COMPLETE is
839 nonzero, then complete the definition of it -- that will render it
840 impossible to actually build the vtable, but is useful to get at those
841 which are known to exist in the runtime. */
843 tree
844 get_vtable_decl (tree type, int complete)
846 tree decl;
848 if (CLASSTYPE_VTABLES (type))
849 return CLASSTYPE_VTABLES (type);
851 decl = build_vtable (type, get_vtable_name (type), vtbl_type_node);
852 CLASSTYPE_VTABLES (type) = decl;
854 if (complete)
856 DECL_EXTERNAL (decl) = 1;
857 cp_finish_decl (decl, NULL_TREE, false, NULL_TREE, 0);
860 return decl;
863 /* Build the primary virtual function table for TYPE. If BINFO is
864 non-NULL, build the vtable starting with the initial approximation
865 that it is the same as the one which is the head of the association
866 list. Returns a nonzero value if a new vtable is actually
867 created. */
869 static int
870 build_primary_vtable (tree binfo, tree type)
872 tree decl;
873 tree virtuals;
875 decl = get_vtable_decl (type, /*complete=*/0);
877 if (binfo)
879 if (BINFO_NEW_VTABLE_MARKED (binfo))
880 /* We have already created a vtable for this base, so there's
881 no need to do it again. */
882 return 0;
884 virtuals = copy_list (BINFO_VIRTUALS (binfo));
885 TREE_TYPE (decl) = TREE_TYPE (get_vtbl_decl_for_binfo (binfo));
886 DECL_SIZE (decl) = TYPE_SIZE (TREE_TYPE (decl));
887 DECL_SIZE_UNIT (decl) = TYPE_SIZE_UNIT (TREE_TYPE (decl));
889 else
891 gcc_assert (TREE_TYPE (decl) == vtbl_type_node);
892 virtuals = NULL_TREE;
895 if (GATHER_STATISTICS)
897 n_vtables += 1;
898 n_vtable_elems += list_length (virtuals);
901 /* Initialize the association list for this type, based
902 on our first approximation. */
903 BINFO_VTABLE (TYPE_BINFO (type)) = decl;
904 BINFO_VIRTUALS (TYPE_BINFO (type)) = virtuals;
905 SET_BINFO_NEW_VTABLE_MARKED (TYPE_BINFO (type));
906 return 1;
909 /* Give BINFO a new virtual function table which is initialized
910 with a skeleton-copy of its original initialization. The only
911 entry that changes is the `delta' entry, so we can really
912 share a lot of structure.
914 FOR_TYPE is the most derived type which caused this table to
915 be needed.
917 Returns nonzero if we haven't met BINFO before.
919 The order in which vtables are built (by calling this function) for
920 an object must remain the same, otherwise a binary incompatibility
921 can result. */
923 static int
924 build_secondary_vtable (tree binfo)
926 if (BINFO_NEW_VTABLE_MARKED (binfo))
927 /* We already created a vtable for this base. There's no need to
928 do it again. */
929 return 0;
931 /* Remember that we've created a vtable for this BINFO, so that we
932 don't try to do so again. */
933 SET_BINFO_NEW_VTABLE_MARKED (binfo);
935 /* Make fresh virtual list, so we can smash it later. */
936 BINFO_VIRTUALS (binfo) = copy_list (BINFO_VIRTUALS (binfo));
938 /* Secondary vtables are laid out as part of the same structure as
939 the primary vtable. */
940 BINFO_VTABLE (binfo) = NULL_TREE;
941 return 1;
944 /* Create a new vtable for BINFO which is the hierarchy dominated by
945 T. Return nonzero if we actually created a new vtable. */
947 static int
948 make_new_vtable (tree t, tree binfo)
950 if (binfo == TYPE_BINFO (t))
951 /* In this case, it is *type*'s vtable we are modifying. We start
952 with the approximation that its vtable is that of the
953 immediate base class. */
954 return build_primary_vtable (binfo, t);
955 else
956 /* This is our very own copy of `basetype' to play with. Later,
957 we will fill in all the virtual functions that override the
958 virtual functions in these base classes which are not defined
959 by the current type. */
960 return build_secondary_vtable (binfo);
963 /* Make *VIRTUALS, an entry on the BINFO_VIRTUALS list for BINFO
964 (which is in the hierarchy dominated by T) list FNDECL as its
965 BV_FN. DELTA is the required constant adjustment from the `this'
966 pointer where the vtable entry appears to the `this' required when
967 the function is actually called. */
969 static void
970 modify_vtable_entry (tree t,
971 tree binfo,
972 tree fndecl,
973 tree delta,
974 tree *virtuals)
976 tree v;
978 v = *virtuals;
980 if (fndecl != BV_FN (v)
981 || !tree_int_cst_equal (delta, BV_DELTA (v)))
983 /* We need a new vtable for BINFO. */
984 if (make_new_vtable (t, binfo))
986 /* If we really did make a new vtable, we also made a copy
987 of the BINFO_VIRTUALS list. Now, we have to find the
988 corresponding entry in that list. */
989 *virtuals = BINFO_VIRTUALS (binfo);
990 while (BV_FN (*virtuals) != BV_FN (v))
991 *virtuals = TREE_CHAIN (*virtuals);
992 v = *virtuals;
995 BV_DELTA (v) = delta;
996 BV_VCALL_INDEX (v) = NULL_TREE;
997 BV_FN (v) = fndecl;
1002 /* Add method METHOD to class TYPE. If USING_DECL is non-null, it is
1003 the USING_DECL naming METHOD. Returns true if the method could be
1004 added to the method vec. */
1006 bool
1007 add_method (tree type, tree method, tree using_decl)
1009 unsigned slot;
1010 tree overload;
1011 bool template_conv_p = false;
1012 bool conv_p;
1013 vec<tree, va_gc> *method_vec;
1014 bool complete_p;
1015 bool insert_p = false;
1016 tree current_fns;
1017 tree fns;
1019 if (method == error_mark_node)
1020 return false;
1022 complete_p = COMPLETE_TYPE_P (type);
1023 conv_p = DECL_CONV_FN_P (method);
1024 if (conv_p)
1025 template_conv_p = (TREE_CODE (method) == TEMPLATE_DECL
1026 && DECL_TEMPLATE_CONV_FN_P (method));
1028 method_vec = CLASSTYPE_METHOD_VEC (type);
1029 if (!method_vec)
1031 /* Make a new method vector. We start with 8 entries. We must
1032 allocate at least two (for constructors and destructors), and
1033 we're going to end up with an assignment operator at some
1034 point as well. */
1035 vec_alloc (method_vec, 8);
1036 /* Create slots for constructors and destructors. */
1037 method_vec->quick_push (NULL_TREE);
1038 method_vec->quick_push (NULL_TREE);
1039 CLASSTYPE_METHOD_VEC (type) = method_vec;
1042 /* Maintain TYPE_HAS_USER_CONSTRUCTOR, etc. */
1043 grok_special_member_properties (method);
1045 /* Constructors and destructors go in special slots. */
1046 if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (method))
1047 slot = CLASSTYPE_CONSTRUCTOR_SLOT;
1048 else if (DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (method))
1050 slot = CLASSTYPE_DESTRUCTOR_SLOT;
1052 if (TYPE_FOR_JAVA (type))
1054 if (!DECL_ARTIFICIAL (method))
1055 error ("Java class %qT cannot have a destructor", type);
1056 else if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
1057 error ("Java class %qT cannot have an implicit non-trivial "
1058 "destructor",
1059 type);
1062 else
1064 tree m;
1066 insert_p = true;
1067 /* See if we already have an entry with this name. */
1068 for (slot = CLASSTYPE_FIRST_CONVERSION_SLOT;
1069 vec_safe_iterate (method_vec, slot, &m);
1070 ++slot)
1072 m = OVL_CURRENT (m);
1073 if (template_conv_p)
1075 if (TREE_CODE (m) == TEMPLATE_DECL
1076 && DECL_TEMPLATE_CONV_FN_P (m))
1077 insert_p = false;
1078 break;
1080 if (conv_p && !DECL_CONV_FN_P (m))
1081 break;
1082 if (DECL_NAME (m) == DECL_NAME (method))
1084 insert_p = false;
1085 break;
1087 if (complete_p
1088 && !DECL_CONV_FN_P (m)
1089 && DECL_NAME (m) > DECL_NAME (method))
1090 break;
1093 current_fns = insert_p ? NULL_TREE : (*method_vec)[slot];
1095 /* Check to see if we've already got this method. */
1096 for (fns = current_fns; fns; fns = OVL_NEXT (fns))
1098 tree fn = OVL_CURRENT (fns);
1099 tree fn_type;
1100 tree method_type;
1101 tree parms1;
1102 tree parms2;
1104 if (TREE_CODE (fn) != TREE_CODE (method))
1105 continue;
1107 /* [over.load] Member function declarations with the
1108 same name and the same parameter types cannot be
1109 overloaded if any of them is a static member
1110 function declaration.
1112 [over.load] Member function declarations with the same name and
1113 the same parameter-type-list as well as member function template
1114 declarations with the same name, the same parameter-type-list, and
1115 the same template parameter lists cannot be overloaded if any of
1116 them, but not all, have a ref-qualifier.
1118 [namespace.udecl] When a using-declaration brings names
1119 from a base class into a derived class scope, member
1120 functions in the derived class override and/or hide member
1121 functions with the same name and parameter types in a base
1122 class (rather than conflicting). */
1123 fn_type = TREE_TYPE (fn);
1124 method_type = TREE_TYPE (method);
1125 parms1 = TYPE_ARG_TYPES (fn_type);
1126 parms2 = TYPE_ARG_TYPES (method_type);
1128 /* Compare the quals on the 'this' parm. Don't compare
1129 the whole types, as used functions are treated as
1130 coming from the using class in overload resolution. */
1131 if (! DECL_STATIC_FUNCTION_P (fn)
1132 && ! DECL_STATIC_FUNCTION_P (method)
1133 /* Either both or neither need to be ref-qualified for
1134 differing quals to allow overloading. */
1135 && (FUNCTION_REF_QUALIFIED (fn_type)
1136 == FUNCTION_REF_QUALIFIED (method_type))
1137 && (type_memfn_quals (fn_type) != type_memfn_quals (method_type)
1138 || type_memfn_rqual (fn_type) != type_memfn_rqual (method_type)))
1139 continue;
1141 /* For templates, the return type and template parameters
1142 must be identical. */
1143 if (TREE_CODE (fn) == TEMPLATE_DECL
1144 && (!same_type_p (TREE_TYPE (fn_type),
1145 TREE_TYPE (method_type))
1146 || !comp_template_parms (DECL_TEMPLATE_PARMS (fn),
1147 DECL_TEMPLATE_PARMS (method))))
1148 continue;
1150 if (! DECL_STATIC_FUNCTION_P (fn))
1151 parms1 = TREE_CHAIN (parms1);
1152 if (! DECL_STATIC_FUNCTION_P (method))
1153 parms2 = TREE_CHAIN (parms2);
1155 if (compparms (parms1, parms2)
1156 && (!DECL_CONV_FN_P (fn)
1157 || same_type_p (TREE_TYPE (fn_type),
1158 TREE_TYPE (method_type)))
1159 && equivalently_constrained (fn, method))
1161 /* For function versions, their parms and types match
1162 but they are not duplicates. Record function versions
1163 as and when they are found. extern "C" functions are
1164 not treated as versions. */
1165 if (TREE_CODE (fn) == FUNCTION_DECL
1166 && TREE_CODE (method) == FUNCTION_DECL
1167 && !DECL_EXTERN_C_P (fn)
1168 && !DECL_EXTERN_C_P (method)
1169 && targetm.target_option.function_versions (fn, method))
1171 /* Mark functions as versions if necessary. Modify the mangled
1172 decl name if necessary. */
1173 if (!DECL_FUNCTION_VERSIONED (fn))
1175 DECL_FUNCTION_VERSIONED (fn) = 1;
1176 if (DECL_ASSEMBLER_NAME_SET_P (fn))
1177 mangle_decl (fn);
1179 if (!DECL_FUNCTION_VERSIONED (method))
1181 DECL_FUNCTION_VERSIONED (method) = 1;
1182 if (DECL_ASSEMBLER_NAME_SET_P (method))
1183 mangle_decl (method);
1185 cgraph_node::record_function_versions (fn, method);
1186 continue;
1188 if (DECL_INHERITED_CTOR_BASE (method))
1190 if (DECL_INHERITED_CTOR_BASE (fn))
1192 error_at (DECL_SOURCE_LOCATION (method),
1193 "%q#D inherited from %qT", method,
1194 DECL_INHERITED_CTOR_BASE (method));
1195 error_at (DECL_SOURCE_LOCATION (fn),
1196 "conflicts with version inherited from %qT",
1197 DECL_INHERITED_CTOR_BASE (fn));
1199 /* Otherwise defer to the other function. */
1200 return false;
1202 if (using_decl)
1204 if (DECL_CONTEXT (fn) == type)
1205 /* Defer to the local function. */
1206 return false;
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, t);
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 /* Data structure for find_abi_tags_r, below. */
1376 struct abi_tag_data
1378 tree t; // The type that we're checking for missing tags.
1379 tree subob; // The subobject of T that we're getting tags from.
1380 tree tags; // error_mark_node for diagnostics, or a list of missing tags.
1383 /* Subroutine of find_abi_tags_r. Handle a single TAG found on the class TP
1384 in the context of P. TAG can be either an identifier (the DECL_NAME of
1385 a tag NAMESPACE_DECL) or a STRING_CST (a tag attribute). */
1387 static void
1388 check_tag (tree tag, tree id, tree *tp, abi_tag_data *p)
1390 if (!IDENTIFIER_MARKED (id))
1392 if (p->tags != error_mark_node)
1394 /* We're collecting tags from template arguments or from
1395 the type of a variable or function return type. */
1396 p->tags = tree_cons (NULL_TREE, tag, p->tags);
1398 /* Don't inherit this tag multiple times. */
1399 IDENTIFIER_MARKED (id) = true;
1401 if (TYPE_P (p->t))
1403 /* Tags inherited from type template arguments are only used
1404 to avoid warnings. */
1405 ABI_TAG_IMPLICIT (p->tags) = true;
1406 return;
1408 /* For functions and variables we want to warn, too. */
1411 /* Otherwise we're diagnosing missing tags. */
1412 if (TREE_CODE (p->t) == FUNCTION_DECL)
1414 if (warning (OPT_Wabi_tag, "%qD inherits the %E ABI tag "
1415 "that %qT (used in its return type) has",
1416 p->t, tag, *tp))
1417 inform (location_of (*tp), "%qT declared here", *tp);
1419 else if (VAR_P (p->t))
1421 if (warning (OPT_Wabi_tag, "%qD inherits the %E ABI tag "
1422 "that %qT (used in its type) has", p->t, tag, *tp))
1423 inform (location_of (*tp), "%qT declared here", *tp);
1425 else if (TYPE_P (p->subob))
1427 if (warning (OPT_Wabi_tag, "%qT does not have the %E ABI tag "
1428 "that base %qT has", p->t, tag, p->subob))
1429 inform (location_of (p->subob), "%qT declared here",
1430 p->subob);
1432 else
1434 if (warning (OPT_Wabi_tag, "%qT does not have the %E ABI tag "
1435 "that %qT (used in the type of %qD) has",
1436 p->t, tag, *tp, p->subob))
1438 inform (location_of (p->subob), "%qD declared here",
1439 p->subob);
1440 inform (location_of (*tp), "%qT declared here", *tp);
1446 /* Find all the ABI tags in the attribute list ATTR and either call
1447 check_tag (if TP is non-null) or set IDENTIFIER_MARKED to val. */
1449 static void
1450 mark_or_check_attr_tags (tree attr, tree *tp, abi_tag_data *p, bool val)
1452 if (!attr)
1453 return;
1454 for (; (attr = lookup_attribute ("abi_tag", attr));
1455 attr = TREE_CHAIN (attr))
1456 for (tree list = TREE_VALUE (attr); list;
1457 list = TREE_CHAIN (list))
1459 tree tag = TREE_VALUE (list);
1460 tree id = get_identifier (TREE_STRING_POINTER (tag));
1461 if (tp)
1462 check_tag (tag, id, tp, p);
1463 else
1464 IDENTIFIER_MARKED (id) = val;
1468 /* Find all the ABI tags on T and its enclosing scopes and either call
1469 check_tag (if TP is non-null) or set IDENTIFIER_MARKED to val. */
1471 static void
1472 mark_or_check_tags (tree t, tree *tp, abi_tag_data *p, bool val)
1474 while (t != global_namespace)
1476 tree attr;
1477 if (TYPE_P (t))
1479 attr = TYPE_ATTRIBUTES (t);
1480 t = CP_TYPE_CONTEXT (t);
1482 else
1484 attr = DECL_ATTRIBUTES (t);
1485 t = CP_DECL_CONTEXT (t);
1487 mark_or_check_attr_tags (attr, tp, p, val);
1491 /* walk_tree callback for check_abi_tags: if the type at *TP involves any
1492 types with ABI tags, add the corresponding identifiers to the VEC in
1493 *DATA and set IDENTIFIER_MARKED. */
1495 static tree
1496 find_abi_tags_r (tree *tp, int *walk_subtrees, void *data)
1498 if (!OVERLOAD_TYPE_P (*tp))
1499 return NULL_TREE;
1501 /* walk_tree shouldn't be walking into any subtrees of a RECORD_TYPE
1502 anyway, but let's make sure of it. */
1503 *walk_subtrees = false;
1505 abi_tag_data *p = static_cast<struct abi_tag_data*>(data);
1507 mark_or_check_tags (*tp, tp, p, false);
1509 return NULL_TREE;
1512 /* walk_tree callback for mark_abi_tags: if *TP is a class, set
1513 IDENTIFIER_MARKED on its ABI tags. */
1515 static tree
1516 mark_abi_tags_r (tree *tp, int *walk_subtrees, void *data)
1518 if (!OVERLOAD_TYPE_P (*tp))
1519 return NULL_TREE;
1521 /* walk_tree shouldn't be walking into any subtrees of a RECORD_TYPE
1522 anyway, but let's make sure of it. */
1523 *walk_subtrees = false;
1525 bool *valp = static_cast<bool*>(data);
1527 mark_or_check_tags (*tp, NULL, NULL, *valp);
1529 return NULL_TREE;
1532 /* Set IDENTIFIER_MARKED on all the ABI tags on T and its enclosing
1533 scopes. */
1535 static void
1536 mark_abi_tags (tree t, bool val)
1538 mark_or_check_tags (t, NULL, NULL, val);
1539 if (DECL_P (t))
1541 if (DECL_LANG_SPECIFIC (t) && DECL_USE_TEMPLATE (t)
1542 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (t)))
1544 /* Template arguments are part of the signature. */
1545 tree level = INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (t));
1546 for (int j = 0; j < TREE_VEC_LENGTH (level); ++j)
1548 tree arg = TREE_VEC_ELT (level, j);
1549 cp_walk_tree_without_duplicates (&arg, mark_abi_tags_r, &val);
1552 if (TREE_CODE (t) == FUNCTION_DECL)
1553 /* A function's parameter types are part of the signature, so
1554 we don't need to inherit any tags that are also in them. */
1555 for (tree arg = FUNCTION_FIRST_USER_PARMTYPE (t); arg;
1556 arg = TREE_CHAIN (arg))
1557 cp_walk_tree_without_duplicates (&TREE_VALUE (arg),
1558 mark_abi_tags_r, &val);
1562 /* Check that T has all the ABI tags that subobject SUBOB has, or
1563 warn if not. If T is a (variable or function) declaration, also
1564 return any missing tags, and add them to T if JUST_CHECKING is false. */
1566 static tree
1567 check_abi_tags (tree t, tree subob, bool just_checking = false)
1569 bool inherit = DECL_P (t);
1571 if (!inherit && !warn_abi_tag)
1572 return NULL_TREE;
1574 tree decl = TYPE_P (t) ? TYPE_NAME (t) : t;
1575 if (!TREE_PUBLIC (decl))
1576 /* No need to worry about things local to this TU. */
1577 return NULL_TREE;
1579 mark_abi_tags (t, true);
1581 tree subtype = TYPE_P (subob) ? subob : TREE_TYPE (subob);
1582 struct abi_tag_data data = { t, subob, error_mark_node };
1583 if (inherit)
1584 data.tags = NULL_TREE;
1586 cp_walk_tree_without_duplicates (&subtype, find_abi_tags_r, &data);
1588 if (!(inherit && data.tags))
1589 /* We don't need to do anything with data.tags. */;
1590 else if (just_checking)
1591 for (tree t = data.tags; t; t = TREE_CHAIN (t))
1593 tree id = get_identifier (TREE_STRING_POINTER (TREE_VALUE (t)));
1594 IDENTIFIER_MARKED (id) = false;
1596 else
1598 tree attr = lookup_attribute ("abi_tag", DECL_ATTRIBUTES (t));
1599 if (attr)
1600 TREE_VALUE (attr) = chainon (data.tags, TREE_VALUE (attr));
1601 else
1602 DECL_ATTRIBUTES (t)
1603 = tree_cons (get_identifier ("abi_tag"), data.tags,
1604 DECL_ATTRIBUTES (t));
1607 mark_abi_tags (t, false);
1609 return data.tags;
1612 /* Check that DECL has all the ABI tags that are used in parts of its type
1613 that are not reflected in its mangled name. */
1615 void
1616 check_abi_tags (tree decl)
1618 if (VAR_P (decl))
1619 check_abi_tags (decl, TREE_TYPE (decl));
1620 else if (TREE_CODE (decl) == FUNCTION_DECL
1621 && !DECL_CONV_FN_P (decl)
1622 && !mangle_return_type_p (decl))
1623 check_abi_tags (decl, TREE_TYPE (TREE_TYPE (decl)));
1626 /* Return any ABI tags that are used in parts of the type of DECL
1627 that are not reflected in its mangled name. This function is only
1628 used in backward-compatible mangling for ABI <11. */
1630 tree
1631 missing_abi_tags (tree decl)
1633 if (VAR_P (decl))
1634 return check_abi_tags (decl, TREE_TYPE (decl), true);
1635 else if (TREE_CODE (decl) == FUNCTION_DECL
1636 /* Don't check DECL_CONV_FN_P here like we do in check_abi_tags, so
1637 that we can use this function for setting need_abi_warning
1638 regardless of the current flag_abi_version. */
1639 && !mangle_return_type_p (decl))
1640 return check_abi_tags (decl, TREE_TYPE (TREE_TYPE (decl)), true);
1641 else
1642 return NULL_TREE;
1645 void
1646 inherit_targ_abi_tags (tree t)
1648 if (!CLASS_TYPE_P (t)
1649 || CLASSTYPE_TEMPLATE_INFO (t) == NULL_TREE)
1650 return;
1652 mark_abi_tags (t, true);
1654 tree args = CLASSTYPE_TI_ARGS (t);
1655 struct abi_tag_data data = { t, NULL_TREE, NULL_TREE };
1656 for (int i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
1658 tree level = TMPL_ARGS_LEVEL (args, i+1);
1659 for (int j = 0; j < TREE_VEC_LENGTH (level); ++j)
1661 tree arg = TREE_VEC_ELT (level, j);
1662 data.subob = arg;
1663 cp_walk_tree_without_duplicates (&arg, find_abi_tags_r, &data);
1667 // If we found some tags on our template arguments, add them to our
1668 // abi_tag attribute.
1669 if (data.tags)
1671 tree attr = lookup_attribute ("abi_tag", TYPE_ATTRIBUTES (t));
1672 if (attr)
1673 TREE_VALUE (attr) = chainon (data.tags, TREE_VALUE (attr));
1674 else
1675 TYPE_ATTRIBUTES (t)
1676 = tree_cons (get_identifier ("abi_tag"), data.tags,
1677 TYPE_ATTRIBUTES (t));
1680 mark_abi_tags (t, false);
1683 /* Return true, iff class T has a non-virtual destructor that is
1684 accessible from outside the class heirarchy (i.e. is public, or
1685 there's a suitable friend. */
1687 static bool
1688 accessible_nvdtor_p (tree t)
1690 tree dtor = CLASSTYPE_DESTRUCTORS (t);
1692 /* An implicitly declared destructor is always public. And,
1693 if it were virtual, we would have created it by now. */
1694 if (!dtor)
1695 return true;
1697 if (DECL_VINDEX (dtor))
1698 return false; /* Virtual */
1700 if (!TREE_PRIVATE (dtor) && !TREE_PROTECTED (dtor))
1701 return true; /* Public */
1703 if (CLASSTYPE_FRIEND_CLASSES (t)
1704 || DECL_FRIENDLIST (TYPE_MAIN_DECL (t)))
1705 return true; /* Has friends */
1707 return false;
1710 /* Run through the base classes of T, updating CANT_HAVE_CONST_CTOR_P,
1711 and NO_CONST_ASN_REF_P. Also set flag bits in T based on
1712 properties of the bases. */
1714 static void
1715 check_bases (tree t,
1716 int* cant_have_const_ctor_p,
1717 int* no_const_asn_ref_p)
1719 int i;
1720 bool seen_non_virtual_nearly_empty_base_p = 0;
1721 int seen_tm_mask = 0;
1722 tree base_binfo;
1723 tree binfo;
1724 tree field = NULL_TREE;
1726 if (!CLASSTYPE_NON_STD_LAYOUT (t))
1727 for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
1728 if (TREE_CODE (field) == FIELD_DECL)
1729 break;
1731 for (binfo = TYPE_BINFO (t), i = 0;
1732 BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
1734 tree basetype = TREE_TYPE (base_binfo);
1736 gcc_assert (COMPLETE_TYPE_P (basetype));
1738 if (CLASSTYPE_FINAL (basetype))
1739 error ("cannot derive from %<final%> base %qT in derived type %qT",
1740 basetype, t);
1742 /* If any base class is non-literal, so is the derived class. */
1743 if (!CLASSTYPE_LITERAL_P (basetype))
1744 CLASSTYPE_LITERAL_P (t) = false;
1746 /* If the base class doesn't have copy constructors or
1747 assignment operators that take const references, then the
1748 derived class cannot have such a member automatically
1749 generated. */
1750 if (TYPE_HAS_COPY_CTOR (basetype)
1751 && ! TYPE_HAS_CONST_COPY_CTOR (basetype))
1752 *cant_have_const_ctor_p = 1;
1753 if (TYPE_HAS_COPY_ASSIGN (basetype)
1754 && !TYPE_HAS_CONST_COPY_ASSIGN (basetype))
1755 *no_const_asn_ref_p = 1;
1757 if (BINFO_VIRTUAL_P (base_binfo))
1758 /* A virtual base does not effect nearly emptiness. */
1760 else if (CLASSTYPE_NEARLY_EMPTY_P (basetype))
1762 if (seen_non_virtual_nearly_empty_base_p)
1763 /* And if there is more than one nearly empty base, then the
1764 derived class is not nearly empty either. */
1765 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
1766 else
1767 /* Remember we've seen one. */
1768 seen_non_virtual_nearly_empty_base_p = 1;
1770 else if (!is_empty_class (basetype))
1771 /* If the base class is not empty or nearly empty, then this
1772 class cannot be nearly empty. */
1773 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
1775 /* A lot of properties from the bases also apply to the derived
1776 class. */
1777 TYPE_NEEDS_CONSTRUCTING (t) |= TYPE_NEEDS_CONSTRUCTING (basetype);
1778 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t)
1779 |= TYPE_HAS_NONTRIVIAL_DESTRUCTOR (basetype);
1780 TYPE_HAS_COMPLEX_COPY_ASSIGN (t)
1781 |= (TYPE_HAS_COMPLEX_COPY_ASSIGN (basetype)
1782 || !TYPE_HAS_COPY_ASSIGN (basetype));
1783 TYPE_HAS_COMPLEX_COPY_CTOR (t) |= (TYPE_HAS_COMPLEX_COPY_CTOR (basetype)
1784 || !TYPE_HAS_COPY_CTOR (basetype));
1785 TYPE_HAS_COMPLEX_MOVE_ASSIGN (t)
1786 |= TYPE_HAS_COMPLEX_MOVE_ASSIGN (basetype);
1787 TYPE_HAS_COMPLEX_MOVE_CTOR (t) |= TYPE_HAS_COMPLEX_MOVE_CTOR (basetype);
1788 TYPE_POLYMORPHIC_P (t) |= TYPE_POLYMORPHIC_P (basetype);
1789 CLASSTYPE_CONTAINS_EMPTY_CLASS_P (t)
1790 |= CLASSTYPE_CONTAINS_EMPTY_CLASS_P (basetype);
1791 TYPE_HAS_COMPLEX_DFLT (t) |= (!TYPE_HAS_DEFAULT_CONSTRUCTOR (basetype)
1792 || TYPE_HAS_COMPLEX_DFLT (basetype));
1793 SET_CLASSTYPE_READONLY_FIELDS_NEED_INIT
1794 (t, CLASSTYPE_READONLY_FIELDS_NEED_INIT (t)
1795 | CLASSTYPE_READONLY_FIELDS_NEED_INIT (basetype));
1796 SET_CLASSTYPE_REF_FIELDS_NEED_INIT
1797 (t, CLASSTYPE_REF_FIELDS_NEED_INIT (t)
1798 | CLASSTYPE_REF_FIELDS_NEED_INIT (basetype));
1799 if (TYPE_HAS_MUTABLE_P (basetype))
1800 CLASSTYPE_HAS_MUTABLE (t) = 1;
1802 /* A standard-layout class is a class that:
1804 * has no non-standard-layout base classes, */
1805 CLASSTYPE_NON_STD_LAYOUT (t) |= CLASSTYPE_NON_STD_LAYOUT (basetype);
1806 if (!CLASSTYPE_NON_STD_LAYOUT (t))
1808 tree basefield;
1809 /* ...has no base classes of the same type as the first non-static
1810 data member... */
1811 if (field && DECL_CONTEXT (field) == t
1812 && (same_type_ignoring_top_level_qualifiers_p
1813 (TREE_TYPE (field), basetype)))
1814 CLASSTYPE_NON_STD_LAYOUT (t) = 1;
1815 else
1816 /* ...either has no non-static data members in the most-derived
1817 class and at most one base class with non-static data
1818 members, or has no base classes with non-static data
1819 members */
1820 for (basefield = TYPE_FIELDS (basetype); basefield;
1821 basefield = DECL_CHAIN (basefield))
1822 if (TREE_CODE (basefield) == FIELD_DECL)
1824 if (field)
1825 CLASSTYPE_NON_STD_LAYOUT (t) = 1;
1826 else
1827 field = basefield;
1828 break;
1832 /* Don't bother collecting tm attributes if transactional memory
1833 support is not enabled. */
1834 if (flag_tm)
1836 tree tm_attr = find_tm_attribute (TYPE_ATTRIBUTES (basetype));
1837 if (tm_attr)
1838 seen_tm_mask |= tm_attr_to_mask (tm_attr);
1841 check_abi_tags (t, basetype);
1844 /* If one of the base classes had TM attributes, and the current class
1845 doesn't define its own, then the current class inherits one. */
1846 if (seen_tm_mask && !find_tm_attribute (TYPE_ATTRIBUTES (t)))
1848 tree tm_attr = tm_mask_to_attr (least_bit_hwi (seen_tm_mask));
1849 TYPE_ATTRIBUTES (t) = tree_cons (tm_attr, NULL, TYPE_ATTRIBUTES (t));
1853 /* Determine all the primary bases within T. Sets BINFO_PRIMARY_BASE_P for
1854 those that are primaries. Sets BINFO_LOST_PRIMARY_P for those
1855 that have had a nearly-empty virtual primary base stolen by some
1856 other base in the hierarchy. Determines CLASSTYPE_PRIMARY_BASE for
1857 T. */
1859 static void
1860 determine_primary_bases (tree t)
1862 unsigned i;
1863 tree primary = NULL_TREE;
1864 tree type_binfo = TYPE_BINFO (t);
1865 tree base_binfo;
1867 /* Determine the primary bases of our bases. */
1868 for (base_binfo = TREE_CHAIN (type_binfo); base_binfo;
1869 base_binfo = TREE_CHAIN (base_binfo))
1871 tree primary = CLASSTYPE_PRIMARY_BINFO (BINFO_TYPE (base_binfo));
1873 /* See if we're the non-virtual primary of our inheritance
1874 chain. */
1875 if (!BINFO_VIRTUAL_P (base_binfo))
1877 tree parent = BINFO_INHERITANCE_CHAIN (base_binfo);
1878 tree parent_primary = CLASSTYPE_PRIMARY_BINFO (BINFO_TYPE (parent));
1880 if (parent_primary
1881 && SAME_BINFO_TYPE_P (BINFO_TYPE (base_binfo),
1882 BINFO_TYPE (parent_primary)))
1883 /* We are the primary binfo. */
1884 BINFO_PRIMARY_P (base_binfo) = 1;
1886 /* Determine if we have a virtual primary base, and mark it so.
1888 if (primary && BINFO_VIRTUAL_P (primary))
1890 tree this_primary = copied_binfo (primary, base_binfo);
1892 if (BINFO_PRIMARY_P (this_primary))
1893 /* Someone already claimed this base. */
1894 BINFO_LOST_PRIMARY_P (base_binfo) = 1;
1895 else
1897 tree delta;
1899 BINFO_PRIMARY_P (this_primary) = 1;
1900 BINFO_INHERITANCE_CHAIN (this_primary) = base_binfo;
1902 /* A virtual binfo might have been copied from within
1903 another hierarchy. As we're about to use it as a
1904 primary base, make sure the offsets match. */
1905 delta = size_diffop_loc (input_location,
1906 fold_convert (ssizetype,
1907 BINFO_OFFSET (base_binfo)),
1908 fold_convert (ssizetype,
1909 BINFO_OFFSET (this_primary)));
1911 propagate_binfo_offsets (this_primary, delta);
1916 /* First look for a dynamic direct non-virtual base. */
1917 for (i = 0; BINFO_BASE_ITERATE (type_binfo, i, base_binfo); i++)
1919 tree basetype = BINFO_TYPE (base_binfo);
1921 if (TYPE_CONTAINS_VPTR_P (basetype) && !BINFO_VIRTUAL_P (base_binfo))
1923 primary = base_binfo;
1924 goto found;
1928 /* A "nearly-empty" virtual base class can be the primary base
1929 class, if no non-virtual polymorphic base can be found. Look for
1930 a nearly-empty virtual dynamic base that is not already a primary
1931 base of something in the hierarchy. If there is no such base,
1932 just pick the first nearly-empty virtual base. */
1934 for (base_binfo = TREE_CHAIN (type_binfo); base_binfo;
1935 base_binfo = TREE_CHAIN (base_binfo))
1936 if (BINFO_VIRTUAL_P (base_binfo)
1937 && CLASSTYPE_NEARLY_EMPTY_P (BINFO_TYPE (base_binfo)))
1939 if (!BINFO_PRIMARY_P (base_binfo))
1941 /* Found one that is not primary. */
1942 primary = base_binfo;
1943 goto found;
1945 else if (!primary)
1946 /* Remember the first candidate. */
1947 primary = base_binfo;
1950 found:
1951 /* If we've got a primary base, use it. */
1952 if (primary)
1954 tree basetype = BINFO_TYPE (primary);
1956 CLASSTYPE_PRIMARY_BINFO (t) = primary;
1957 if (BINFO_PRIMARY_P (primary))
1958 /* We are stealing a primary base. */
1959 BINFO_LOST_PRIMARY_P (BINFO_INHERITANCE_CHAIN (primary)) = 1;
1960 BINFO_PRIMARY_P (primary) = 1;
1961 if (BINFO_VIRTUAL_P (primary))
1963 tree delta;
1965 BINFO_INHERITANCE_CHAIN (primary) = type_binfo;
1966 /* A virtual binfo might have been copied from within
1967 another hierarchy. As we're about to use it as a primary
1968 base, make sure the offsets match. */
1969 delta = size_diffop_loc (input_location, ssize_int (0),
1970 fold_convert (ssizetype, BINFO_OFFSET (primary)));
1972 propagate_binfo_offsets (primary, delta);
1975 primary = TYPE_BINFO (basetype);
1977 TYPE_VFIELD (t) = TYPE_VFIELD (basetype);
1978 BINFO_VTABLE (type_binfo) = BINFO_VTABLE (primary);
1979 BINFO_VIRTUALS (type_binfo) = BINFO_VIRTUALS (primary);
1983 /* Update the variant types of T. */
1985 void
1986 fixup_type_variants (tree t)
1988 tree variants;
1990 if (!t)
1991 return;
1993 for (variants = TYPE_NEXT_VARIANT (t);
1994 variants;
1995 variants = TYPE_NEXT_VARIANT (variants))
1997 /* These fields are in the _TYPE part of the node, not in
1998 the TYPE_LANG_SPECIFIC component, so they are not shared. */
1999 TYPE_HAS_USER_CONSTRUCTOR (variants) = TYPE_HAS_USER_CONSTRUCTOR (t);
2000 TYPE_NEEDS_CONSTRUCTING (variants) = TYPE_NEEDS_CONSTRUCTING (t);
2001 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (variants)
2002 = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t);
2004 TYPE_POLYMORPHIC_P (variants) = TYPE_POLYMORPHIC_P (t);
2006 TYPE_BINFO (variants) = TYPE_BINFO (t);
2008 /* Copy whatever these are holding today. */
2009 TYPE_VFIELD (variants) = TYPE_VFIELD (t);
2010 TYPE_FIELDS (variants) = TYPE_FIELDS (t);
2014 /* KLASS is a class that we're applying may_alias to after the body is
2015 parsed. Fixup any POINTER_TO and REFERENCE_TO types. The
2016 canonical type(s) will be implicitly updated. */
2018 static void
2019 fixup_may_alias (tree klass)
2021 tree t;
2023 for (t = TYPE_POINTER_TO (klass); t; t = TYPE_NEXT_PTR_TO (t))
2024 TYPE_REF_CAN_ALIAS_ALL (t) = true;
2025 for (t = TYPE_REFERENCE_TO (klass); t; t = TYPE_NEXT_REF_TO (t))
2026 TYPE_REF_CAN_ALIAS_ALL (t) = true;
2029 /* Early variant fixups: we apply attributes at the beginning of the class
2030 definition, and we need to fix up any variants that have already been
2031 made via elaborated-type-specifier so that check_qualified_type works. */
2033 void
2034 fixup_attribute_variants (tree t)
2036 tree variants;
2038 if (!t)
2039 return;
2041 tree attrs = TYPE_ATTRIBUTES (t);
2042 unsigned align = TYPE_ALIGN (t);
2043 bool user_align = TYPE_USER_ALIGN (t);
2044 bool may_alias = lookup_attribute ("may_alias", attrs);
2046 if (may_alias)
2047 fixup_may_alias (t);
2049 for (variants = TYPE_NEXT_VARIANT (t);
2050 variants;
2051 variants = TYPE_NEXT_VARIANT (variants))
2053 /* These are the two fields that check_qualified_type looks at and
2054 are affected by attributes. */
2055 TYPE_ATTRIBUTES (variants) = attrs;
2056 unsigned valign = align;
2057 if (TYPE_USER_ALIGN (variants))
2058 valign = MAX (valign, TYPE_ALIGN (variants));
2059 else
2060 TYPE_USER_ALIGN (variants) = user_align;
2061 SET_TYPE_ALIGN (variants, valign);
2062 if (may_alias)
2063 fixup_may_alias (variants);
2067 /* Set memoizing fields and bits of T (and its variants) for later
2068 use. */
2070 static void
2071 finish_struct_bits (tree t)
2073 /* Fix up variants (if any). */
2074 fixup_type_variants (t);
2076 if (BINFO_N_BASE_BINFOS (TYPE_BINFO (t)) && TYPE_POLYMORPHIC_P (t))
2077 /* For a class w/o baseclasses, 'finish_struct' has set
2078 CLASSTYPE_PURE_VIRTUALS correctly (by definition).
2079 Similarly for a class whose base classes do not have vtables.
2080 When neither of these is true, we might have removed abstract
2081 virtuals (by providing a definition), added some (by declaring
2082 new ones), or redeclared ones from a base class. We need to
2083 recalculate what's really an abstract virtual at this point (by
2084 looking in the vtables). */
2085 get_pure_virtuals (t);
2087 /* If this type has a copy constructor or a destructor, force its
2088 mode to be BLKmode, and force its TREE_ADDRESSABLE bit to be
2089 nonzero. This will cause it to be passed by invisible reference
2090 and prevent it from being returned in a register. */
2091 if (type_has_nontrivial_copy_init (t)
2092 || TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t))
2094 tree variants;
2095 DECL_MODE (TYPE_MAIN_DECL (t)) = BLKmode;
2096 for (variants = t; variants; variants = TYPE_NEXT_VARIANT (variants))
2098 SET_TYPE_MODE (variants, BLKmode);
2099 TREE_ADDRESSABLE (variants) = 1;
2104 /* Issue warnings about T having private constructors, but no friends,
2105 and so forth.
2107 HAS_NONPRIVATE_METHOD is nonzero if T has any non-private methods or
2108 static members. HAS_NONPRIVATE_STATIC_FN is nonzero if T has any
2109 non-private static member functions. */
2111 static void
2112 maybe_warn_about_overly_private_class (tree t)
2114 int has_member_fn = 0;
2115 int has_nonprivate_method = 0;
2116 tree fn;
2118 if (!warn_ctor_dtor_privacy
2119 /* If the class has friends, those entities might create and
2120 access instances, so we should not warn. */
2121 || (CLASSTYPE_FRIEND_CLASSES (t)
2122 || DECL_FRIENDLIST (TYPE_MAIN_DECL (t)))
2123 /* We will have warned when the template was declared; there's
2124 no need to warn on every instantiation. */
2125 || CLASSTYPE_TEMPLATE_INSTANTIATION (t))
2126 /* There's no reason to even consider warning about this
2127 class. */
2128 return;
2130 /* We only issue one warning, if more than one applies, because
2131 otherwise, on code like:
2133 class A {
2134 // Oops - forgot `public:'
2135 A();
2136 A(const A&);
2137 ~A();
2140 we warn several times about essentially the same problem. */
2142 /* Check to see if all (non-constructor, non-destructor) member
2143 functions are private. (Since there are no friends or
2144 non-private statics, we can't ever call any of the private member
2145 functions.) */
2146 for (fn = TYPE_METHODS (t); fn; fn = DECL_CHAIN (fn))
2147 /* We're not interested in compiler-generated methods; they don't
2148 provide any way to call private members. */
2149 if (!DECL_ARTIFICIAL (fn))
2151 if (!TREE_PRIVATE (fn))
2153 if (DECL_STATIC_FUNCTION_P (fn))
2154 /* A non-private static member function is just like a
2155 friend; it can create and invoke private member
2156 functions, and be accessed without a class
2157 instance. */
2158 return;
2160 has_nonprivate_method = 1;
2161 /* Keep searching for a static member function. */
2163 else if (!DECL_CONSTRUCTOR_P (fn) && !DECL_DESTRUCTOR_P (fn))
2164 has_member_fn = 1;
2167 if (!has_nonprivate_method && has_member_fn)
2169 /* There are no non-private methods, and there's at least one
2170 private member function that isn't a constructor or
2171 destructor. (If all the private members are
2172 constructors/destructors we want to use the code below that
2173 issues error messages specifically referring to
2174 constructors/destructors.) */
2175 unsigned i;
2176 tree binfo = TYPE_BINFO (t);
2178 for (i = 0; i != BINFO_N_BASE_BINFOS (binfo); i++)
2179 if (BINFO_BASE_ACCESS (binfo, i) != access_private_node)
2181 has_nonprivate_method = 1;
2182 break;
2184 if (!has_nonprivate_method)
2186 warning (OPT_Wctor_dtor_privacy,
2187 "all member functions in class %qT are private", t);
2188 return;
2192 /* Even if some of the member functions are non-private, the class
2193 won't be useful for much if all the constructors or destructors
2194 are private: such an object can never be created or destroyed. */
2195 fn = CLASSTYPE_DESTRUCTORS (t);
2196 if (fn && TREE_PRIVATE (fn))
2198 warning (OPT_Wctor_dtor_privacy,
2199 "%q#T only defines a private destructor and has no friends",
2201 return;
2204 /* Warn about classes that have private constructors and no friends. */
2205 if (TYPE_HAS_USER_CONSTRUCTOR (t)
2206 /* Implicitly generated constructors are always public. */
2207 && (!CLASSTYPE_LAZY_DEFAULT_CTOR (t)
2208 || !CLASSTYPE_LAZY_COPY_CTOR (t)))
2210 int nonprivate_ctor = 0;
2212 /* If a non-template class does not define a copy
2213 constructor, one is defined for it, enabling it to avoid
2214 this warning. For a template class, this does not
2215 happen, and so we would normally get a warning on:
2217 template <class T> class C { private: C(); };
2219 To avoid this asymmetry, we check TYPE_HAS_COPY_CTOR. All
2220 complete non-template or fully instantiated classes have this
2221 flag set. */
2222 if (!TYPE_HAS_COPY_CTOR (t))
2223 nonprivate_ctor = 1;
2224 else
2225 for (fn = CLASSTYPE_CONSTRUCTORS (t); fn; fn = OVL_NEXT (fn))
2227 tree ctor = OVL_CURRENT (fn);
2228 /* Ideally, we wouldn't count copy constructors (or, in
2229 fact, any constructor that takes an argument of the
2230 class type as a parameter) because such things cannot
2231 be used to construct an instance of the class unless
2232 you already have one. But, for now at least, we're
2233 more generous. */
2234 if (! TREE_PRIVATE (ctor))
2236 nonprivate_ctor = 1;
2237 break;
2241 if (nonprivate_ctor == 0)
2243 warning (OPT_Wctor_dtor_privacy,
2244 "%q#T only defines private constructors and has no friends",
2246 return;
2251 static struct {
2252 gt_pointer_operator new_value;
2253 void *cookie;
2254 } resort_data;
2256 /* Comparison function to compare two TYPE_METHOD_VEC entries by name. */
2258 static int
2259 method_name_cmp (const void* m1_p, const void* m2_p)
2261 const tree *const m1 = (const tree *) m1_p;
2262 const tree *const m2 = (const tree *) m2_p;
2264 if (*m1 == NULL_TREE && *m2 == NULL_TREE)
2265 return 0;
2266 if (*m1 == NULL_TREE)
2267 return -1;
2268 if (*m2 == NULL_TREE)
2269 return 1;
2270 if (DECL_NAME (OVL_CURRENT (*m1)) < DECL_NAME (OVL_CURRENT (*m2)))
2271 return -1;
2272 return 1;
2275 /* This routine compares two fields like method_name_cmp but using the
2276 pointer operator in resort_field_decl_data. */
2278 static int
2279 resort_method_name_cmp (const void* m1_p, const void* m2_p)
2281 const tree *const m1 = (const tree *) m1_p;
2282 const tree *const m2 = (const tree *) m2_p;
2283 if (*m1 == NULL_TREE && *m2 == NULL_TREE)
2284 return 0;
2285 if (*m1 == NULL_TREE)
2286 return -1;
2287 if (*m2 == NULL_TREE)
2288 return 1;
2290 tree d1 = DECL_NAME (OVL_CURRENT (*m1));
2291 tree d2 = DECL_NAME (OVL_CURRENT (*m2));
2292 resort_data.new_value (&d1, resort_data.cookie);
2293 resort_data.new_value (&d2, resort_data.cookie);
2294 if (d1 < d2)
2295 return -1;
2297 return 1;
2300 /* Resort TYPE_METHOD_VEC because pointers have been reordered. */
2302 void
2303 resort_type_method_vec (void* obj,
2304 void* /*orig_obj*/,
2305 gt_pointer_operator new_value,
2306 void* cookie)
2308 vec<tree, va_gc> *method_vec = (vec<tree, va_gc> *) obj;
2309 int len = vec_safe_length (method_vec);
2310 size_t slot;
2311 tree fn;
2313 /* The type conversion ops have to live at the front of the vec, so we
2314 can't sort them. */
2315 for (slot = CLASSTYPE_FIRST_CONVERSION_SLOT;
2316 vec_safe_iterate (method_vec, slot, &fn);
2317 ++slot)
2318 if (!DECL_CONV_FN_P (OVL_CURRENT (fn)))
2319 break;
2321 if (len - slot > 1)
2323 resort_data.new_value = new_value;
2324 resort_data.cookie = cookie;
2325 qsort (method_vec->address () + slot, len - slot, sizeof (tree),
2326 resort_method_name_cmp);
2330 /* Warn about duplicate methods in fn_fields.
2332 Sort methods that are not special (i.e., constructors, destructors,
2333 and type conversion operators) so that we can find them faster in
2334 search. */
2336 static void
2337 finish_struct_methods (tree t)
2339 tree fn_fields;
2340 vec<tree, va_gc> *method_vec;
2341 int slot, len;
2343 method_vec = CLASSTYPE_METHOD_VEC (t);
2344 if (!method_vec)
2345 return;
2347 len = method_vec->length ();
2349 /* Clear DECL_IN_AGGR_P for all functions. */
2350 for (fn_fields = TYPE_METHODS (t); fn_fields;
2351 fn_fields = DECL_CHAIN (fn_fields))
2352 DECL_IN_AGGR_P (fn_fields) = 0;
2354 /* Issue warnings about private constructors and such. If there are
2355 no methods, then some public defaults are generated. */
2356 maybe_warn_about_overly_private_class (t);
2358 /* The type conversion ops have to live at the front of the vec, so we
2359 can't sort them. */
2360 for (slot = CLASSTYPE_FIRST_CONVERSION_SLOT;
2361 method_vec->iterate (slot, &fn_fields);
2362 ++slot)
2363 if (!DECL_CONV_FN_P (OVL_CURRENT (fn_fields)))
2364 break;
2365 if (len - slot > 1)
2366 qsort (method_vec->address () + slot,
2367 len-slot, sizeof (tree), method_name_cmp);
2370 /* Make BINFO's vtable have N entries, including RTTI entries,
2371 vbase and vcall offsets, etc. Set its type and call the back end
2372 to lay it out. */
2374 static void
2375 layout_vtable_decl (tree binfo, int n)
2377 tree atype;
2378 tree vtable;
2380 atype = build_array_of_n_type (vtable_entry_type, n);
2381 layout_type (atype);
2383 /* We may have to grow the vtable. */
2384 vtable = get_vtbl_decl_for_binfo (binfo);
2385 if (!same_type_p (TREE_TYPE (vtable), atype))
2387 TREE_TYPE (vtable) = atype;
2388 DECL_SIZE (vtable) = DECL_SIZE_UNIT (vtable) = NULL_TREE;
2389 layout_decl (vtable, 0);
2393 /* True iff FNDECL and BASE_FNDECL (both non-static member functions)
2394 have the same signature. */
2397 same_signature_p (const_tree fndecl, const_tree base_fndecl)
2399 /* One destructor overrides another if they are the same kind of
2400 destructor. */
2401 if (DECL_DESTRUCTOR_P (base_fndecl) && DECL_DESTRUCTOR_P (fndecl)
2402 && special_function_p (base_fndecl) == special_function_p (fndecl))
2403 return 1;
2404 /* But a non-destructor never overrides a destructor, nor vice
2405 versa, nor do different kinds of destructors override
2406 one-another. For example, a complete object destructor does not
2407 override a deleting destructor. */
2408 if (DECL_DESTRUCTOR_P (base_fndecl) || DECL_DESTRUCTOR_P (fndecl))
2409 return 0;
2411 if (DECL_NAME (fndecl) == DECL_NAME (base_fndecl)
2412 || (DECL_CONV_FN_P (fndecl)
2413 && DECL_CONV_FN_P (base_fndecl)
2414 && same_type_p (DECL_CONV_FN_TYPE (fndecl),
2415 DECL_CONV_FN_TYPE (base_fndecl))))
2417 tree fntype = TREE_TYPE (fndecl);
2418 tree base_fntype = TREE_TYPE (base_fndecl);
2419 if (type_memfn_quals (fntype) == type_memfn_quals (base_fntype)
2420 && type_memfn_rqual (fntype) == type_memfn_rqual (base_fntype)
2421 && compparms (FUNCTION_FIRST_USER_PARMTYPE (fndecl),
2422 FUNCTION_FIRST_USER_PARMTYPE (base_fndecl)))
2423 return 1;
2425 return 0;
2428 /* Returns TRUE if DERIVED is a binfo containing the binfo BASE as a
2429 subobject. */
2431 static bool
2432 base_derived_from (tree derived, tree base)
2434 tree probe;
2436 for (probe = base; probe; probe = BINFO_INHERITANCE_CHAIN (probe))
2438 if (probe == derived)
2439 return true;
2440 else if (BINFO_VIRTUAL_P (probe))
2441 /* If we meet a virtual base, we can't follow the inheritance
2442 any more. See if the complete type of DERIVED contains
2443 such a virtual base. */
2444 return (binfo_for_vbase (BINFO_TYPE (probe), BINFO_TYPE (derived))
2445 != NULL_TREE);
2447 return false;
2450 struct find_final_overrider_data {
2451 /* The function for which we are trying to find a final overrider. */
2452 tree fn;
2453 /* The base class in which the function was declared. */
2454 tree declaring_base;
2455 /* The candidate overriders. */
2456 tree candidates;
2457 /* Path to most derived. */
2458 vec<tree> path;
2461 /* Add the overrider along the current path to FFOD->CANDIDATES.
2462 Returns true if an overrider was found; false otherwise. */
2464 static bool
2465 dfs_find_final_overrider_1 (tree binfo,
2466 find_final_overrider_data *ffod,
2467 unsigned depth)
2469 tree method;
2471 /* If BINFO is not the most derived type, try a more derived class.
2472 A definition there will overrider a definition here. */
2473 if (depth)
2475 depth--;
2476 if (dfs_find_final_overrider_1
2477 (ffod->path[depth], ffod, depth))
2478 return true;
2481 method = look_for_overrides_here (BINFO_TYPE (binfo), ffod->fn);
2482 if (method)
2484 tree *candidate = &ffod->candidates;
2486 /* Remove any candidates overridden by this new function. */
2487 while (*candidate)
2489 /* If *CANDIDATE overrides METHOD, then METHOD
2490 cannot override anything else on the list. */
2491 if (base_derived_from (TREE_VALUE (*candidate), binfo))
2492 return true;
2493 /* If METHOD overrides *CANDIDATE, remove *CANDIDATE. */
2494 if (base_derived_from (binfo, TREE_VALUE (*candidate)))
2495 *candidate = TREE_CHAIN (*candidate);
2496 else
2497 candidate = &TREE_CHAIN (*candidate);
2500 /* Add the new function. */
2501 ffod->candidates = tree_cons (method, binfo, ffod->candidates);
2502 return true;
2505 return false;
2508 /* Called from find_final_overrider via dfs_walk. */
2510 static tree
2511 dfs_find_final_overrider_pre (tree binfo, void *data)
2513 find_final_overrider_data *ffod = (find_final_overrider_data *) data;
2515 if (binfo == ffod->declaring_base)
2516 dfs_find_final_overrider_1 (binfo, ffod, ffod->path.length ());
2517 ffod->path.safe_push (binfo);
2519 return NULL_TREE;
2522 static tree
2523 dfs_find_final_overrider_post (tree /*binfo*/, void *data)
2525 find_final_overrider_data *ffod = (find_final_overrider_data *) data;
2526 ffod->path.pop ();
2528 return NULL_TREE;
2531 /* Returns a TREE_LIST whose TREE_PURPOSE is the final overrider for
2532 FN and whose TREE_VALUE is the binfo for the base where the
2533 overriding occurs. BINFO (in the hierarchy dominated by the binfo
2534 DERIVED) is the base object in which FN is declared. */
2536 static tree
2537 find_final_overrider (tree derived, tree binfo, tree fn)
2539 find_final_overrider_data ffod;
2541 /* Getting this right is a little tricky. This is valid:
2543 struct S { virtual void f (); };
2544 struct T { virtual void f (); };
2545 struct U : public S, public T { };
2547 even though calling `f' in `U' is ambiguous. But,
2549 struct R { virtual void f(); };
2550 struct S : virtual public R { virtual void f (); };
2551 struct T : virtual public R { virtual void f (); };
2552 struct U : public S, public T { };
2554 is not -- there's no way to decide whether to put `S::f' or
2555 `T::f' in the vtable for `R'.
2557 The solution is to look at all paths to BINFO. If we find
2558 different overriders along any two, then there is a problem. */
2559 if (DECL_THUNK_P (fn))
2560 fn = THUNK_TARGET (fn);
2562 /* Determine the depth of the hierarchy. */
2563 ffod.fn = fn;
2564 ffod.declaring_base = binfo;
2565 ffod.candidates = NULL_TREE;
2566 ffod.path.create (30);
2568 dfs_walk_all (derived, dfs_find_final_overrider_pre,
2569 dfs_find_final_overrider_post, &ffod);
2571 ffod.path.release ();
2573 /* If there was no winner, issue an error message. */
2574 if (!ffod.candidates || TREE_CHAIN (ffod.candidates))
2575 return error_mark_node;
2577 return ffod.candidates;
2580 /* Return the index of the vcall offset for FN when TYPE is used as a
2581 virtual base. */
2583 static tree
2584 get_vcall_index (tree fn, tree type)
2586 vec<tree_pair_s, va_gc> *indices = CLASSTYPE_VCALL_INDICES (type);
2587 tree_pair_p p;
2588 unsigned ix;
2590 FOR_EACH_VEC_SAFE_ELT (indices, ix, p)
2591 if ((DECL_DESTRUCTOR_P (fn) && DECL_DESTRUCTOR_P (p->purpose))
2592 || same_signature_p (fn, p->purpose))
2593 return p->value;
2595 /* There should always be an appropriate index. */
2596 gcc_unreachable ();
2599 /* Update an entry in the vtable for BINFO, which is in the hierarchy
2600 dominated by T. FN is the old function; VIRTUALS points to the
2601 corresponding position in the new BINFO_VIRTUALS list. IX is the index
2602 of that entry in the list. */
2604 static void
2605 update_vtable_entry_for_fn (tree t, tree binfo, tree fn, tree* virtuals,
2606 unsigned ix)
2608 tree b;
2609 tree overrider;
2610 tree delta;
2611 tree virtual_base;
2612 tree first_defn;
2613 tree overrider_fn, overrider_target;
2614 tree target_fn = DECL_THUNK_P (fn) ? THUNK_TARGET (fn) : fn;
2615 tree over_return, base_return;
2616 bool lost = false;
2618 /* Find the nearest primary base (possibly binfo itself) which defines
2619 this function; this is the class the caller will convert to when
2620 calling FN through BINFO. */
2621 for (b = binfo; ; b = get_primary_binfo (b))
2623 gcc_assert (b);
2624 if (look_for_overrides_here (BINFO_TYPE (b), target_fn))
2625 break;
2627 /* The nearest definition is from a lost primary. */
2628 if (BINFO_LOST_PRIMARY_P (b))
2629 lost = true;
2631 first_defn = b;
2633 /* Find the final overrider. */
2634 overrider = find_final_overrider (TYPE_BINFO (t), b, target_fn);
2635 if (overrider == error_mark_node)
2637 error ("no unique final overrider for %qD in %qT", target_fn, t);
2638 return;
2640 overrider_target = overrider_fn = TREE_PURPOSE (overrider);
2642 /* Check for adjusting covariant return types. */
2643 over_return = TREE_TYPE (TREE_TYPE (overrider_target));
2644 base_return = TREE_TYPE (TREE_TYPE (target_fn));
2646 if (POINTER_TYPE_P (over_return)
2647 && TREE_CODE (over_return) == TREE_CODE (base_return)
2648 && CLASS_TYPE_P (TREE_TYPE (over_return))
2649 && CLASS_TYPE_P (TREE_TYPE (base_return))
2650 /* If the overrider is invalid, don't even try. */
2651 && !DECL_INVALID_OVERRIDER_P (overrider_target))
2653 /* If FN is a covariant thunk, we must figure out the adjustment
2654 to the final base FN was converting to. As OVERRIDER_TARGET might
2655 also be converting to the return type of FN, we have to
2656 combine the two conversions here. */
2657 tree fixed_offset, virtual_offset;
2659 over_return = TREE_TYPE (over_return);
2660 base_return = TREE_TYPE (base_return);
2662 if (DECL_THUNK_P (fn))
2664 gcc_assert (DECL_RESULT_THUNK_P (fn));
2665 fixed_offset = ssize_int (THUNK_FIXED_OFFSET (fn));
2666 virtual_offset = THUNK_VIRTUAL_OFFSET (fn);
2668 else
2669 fixed_offset = virtual_offset = NULL_TREE;
2671 if (virtual_offset)
2672 /* Find the equivalent binfo within the return type of the
2673 overriding function. We will want the vbase offset from
2674 there. */
2675 virtual_offset = binfo_for_vbase (BINFO_TYPE (virtual_offset),
2676 over_return);
2677 else if (!same_type_ignoring_top_level_qualifiers_p
2678 (over_return, base_return))
2680 /* There was no existing virtual thunk (which takes
2681 precedence). So find the binfo of the base function's
2682 return type within the overriding function's return type.
2683 Fortunately we know the covariancy is valid (it
2684 has already been checked), so we can just iterate along
2685 the binfos, which have been chained in inheritance graph
2686 order. Of course it is lame that we have to repeat the
2687 search here anyway -- we should really be caching pieces
2688 of the vtable and avoiding this repeated work. */
2689 tree thunk_binfo, base_binfo;
2691 /* Find the base binfo within the overriding function's
2692 return type. We will always find a thunk_binfo, except
2693 when the covariancy is invalid (which we will have
2694 already diagnosed). */
2695 for (base_binfo = TYPE_BINFO (base_return),
2696 thunk_binfo = TYPE_BINFO (over_return);
2697 thunk_binfo;
2698 thunk_binfo = TREE_CHAIN (thunk_binfo))
2699 if (SAME_BINFO_TYPE_P (BINFO_TYPE (thunk_binfo),
2700 BINFO_TYPE (base_binfo)))
2701 break;
2703 /* See if virtual inheritance is involved. */
2704 for (virtual_offset = thunk_binfo;
2705 virtual_offset;
2706 virtual_offset = BINFO_INHERITANCE_CHAIN (virtual_offset))
2707 if (BINFO_VIRTUAL_P (virtual_offset))
2708 break;
2710 if (virtual_offset
2711 || (thunk_binfo && !BINFO_OFFSET_ZEROP (thunk_binfo)))
2713 tree offset = fold_convert (ssizetype, BINFO_OFFSET (thunk_binfo));
2715 if (virtual_offset)
2717 /* We convert via virtual base. Adjust the fixed
2718 offset to be from there. */
2719 offset =
2720 size_diffop (offset,
2721 fold_convert (ssizetype,
2722 BINFO_OFFSET (virtual_offset)));
2724 if (fixed_offset)
2725 /* There was an existing fixed offset, this must be
2726 from the base just converted to, and the base the
2727 FN was thunking to. */
2728 fixed_offset = size_binop (PLUS_EXPR, fixed_offset, offset);
2729 else
2730 fixed_offset = offset;
2734 if (fixed_offset || virtual_offset)
2735 /* Replace the overriding function with a covariant thunk. We
2736 will emit the overriding function in its own slot as
2737 well. */
2738 overrider_fn = make_thunk (overrider_target, /*this_adjusting=*/0,
2739 fixed_offset, virtual_offset);
2741 else
2742 gcc_assert (DECL_INVALID_OVERRIDER_P (overrider_target) ||
2743 !DECL_THUNK_P (fn));
2745 /* If we need a covariant thunk, then we may need to adjust first_defn.
2746 The ABI specifies that the thunks emitted with a function are
2747 determined by which bases the function overrides, so we need to be
2748 sure that we're using a thunk for some overridden base; even if we
2749 know that the necessary this adjustment is zero, there may not be an
2750 appropriate zero-this-adjusment thunk for us to use since thunks for
2751 overriding virtual bases always use the vcall offset.
2753 Furthermore, just choosing any base that overrides this function isn't
2754 quite right, as this slot won't be used for calls through a type that
2755 puts a covariant thunk here. Calling the function through such a type
2756 will use a different slot, and that slot is the one that determines
2757 the thunk emitted for that base.
2759 So, keep looking until we find the base that we're really overriding
2760 in this slot: the nearest primary base that doesn't use a covariant
2761 thunk in this slot. */
2762 if (overrider_target != overrider_fn)
2764 if (BINFO_TYPE (b) == DECL_CONTEXT (overrider_target))
2765 /* We already know that the overrider needs a covariant thunk. */
2766 b = get_primary_binfo (b);
2767 for (; ; b = get_primary_binfo (b))
2769 tree main_binfo = TYPE_BINFO (BINFO_TYPE (b));
2770 tree bv = chain_index (ix, BINFO_VIRTUALS (main_binfo));
2771 if (!DECL_THUNK_P (TREE_VALUE (bv)))
2772 break;
2773 if (BINFO_LOST_PRIMARY_P (b))
2774 lost = true;
2776 first_defn = b;
2779 /* Assume that we will produce a thunk that convert all the way to
2780 the final overrider, and not to an intermediate virtual base. */
2781 virtual_base = NULL_TREE;
2783 /* See if we can convert to an intermediate virtual base first, and then
2784 use the vcall offset located there to finish the conversion. */
2785 for (; b; b = BINFO_INHERITANCE_CHAIN (b))
2787 /* If we find the final overrider, then we can stop
2788 walking. */
2789 if (SAME_BINFO_TYPE_P (BINFO_TYPE (b),
2790 BINFO_TYPE (TREE_VALUE (overrider))))
2791 break;
2793 /* If we find a virtual base, and we haven't yet found the
2794 overrider, then there is a virtual base between the
2795 declaring base (first_defn) and the final overrider. */
2796 if (BINFO_VIRTUAL_P (b))
2798 virtual_base = b;
2799 break;
2803 /* Compute the constant adjustment to the `this' pointer. The
2804 `this' pointer, when this function is called, will point at BINFO
2805 (or one of its primary bases, which are at the same offset). */
2806 if (virtual_base)
2807 /* The `this' pointer needs to be adjusted from the declaration to
2808 the nearest virtual base. */
2809 delta = size_diffop_loc (input_location,
2810 fold_convert (ssizetype, BINFO_OFFSET (virtual_base)),
2811 fold_convert (ssizetype, BINFO_OFFSET (first_defn)));
2812 else if (lost)
2813 /* If the nearest definition is in a lost primary, we don't need an
2814 entry in our vtable. Except possibly in a constructor vtable,
2815 if we happen to get our primary back. In that case, the offset
2816 will be zero, as it will be a primary base. */
2817 delta = size_zero_node;
2818 else
2819 /* The `this' pointer needs to be adjusted from pointing to
2820 BINFO to pointing at the base where the final overrider
2821 appears. */
2822 delta = size_diffop_loc (input_location,
2823 fold_convert (ssizetype,
2824 BINFO_OFFSET (TREE_VALUE (overrider))),
2825 fold_convert (ssizetype, BINFO_OFFSET (binfo)));
2827 modify_vtable_entry (t, binfo, overrider_fn, delta, virtuals);
2829 if (virtual_base)
2830 BV_VCALL_INDEX (*virtuals)
2831 = get_vcall_index (overrider_target, BINFO_TYPE (virtual_base));
2832 else
2833 BV_VCALL_INDEX (*virtuals) = NULL_TREE;
2835 BV_LOST_PRIMARY (*virtuals) = lost;
2838 /* Called from modify_all_vtables via dfs_walk. */
2840 static tree
2841 dfs_modify_vtables (tree binfo, void* data)
2843 tree t = (tree) data;
2844 tree virtuals;
2845 tree old_virtuals;
2846 unsigned ix;
2848 if (!TYPE_CONTAINS_VPTR_P (BINFO_TYPE (binfo)))
2849 /* A base without a vtable needs no modification, and its bases
2850 are uninteresting. */
2851 return dfs_skip_bases;
2853 if (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), t)
2854 && !CLASSTYPE_HAS_PRIMARY_BASE_P (t))
2855 /* Don't do the primary vtable, if it's new. */
2856 return NULL_TREE;
2858 if (BINFO_PRIMARY_P (binfo) && !BINFO_VIRTUAL_P (binfo))
2859 /* There's no need to modify the vtable for a non-virtual primary
2860 base; we're not going to use that vtable anyhow. We do still
2861 need to do this for virtual primary bases, as they could become
2862 non-primary in a construction vtable. */
2863 return NULL_TREE;
2865 make_new_vtable (t, binfo);
2867 /* Now, go through each of the virtual functions in the virtual
2868 function table for BINFO. Find the final overrider, and update
2869 the BINFO_VIRTUALS list appropriately. */
2870 for (ix = 0, virtuals = BINFO_VIRTUALS (binfo),
2871 old_virtuals = BINFO_VIRTUALS (TYPE_BINFO (BINFO_TYPE (binfo)));
2872 virtuals;
2873 ix++, virtuals = TREE_CHAIN (virtuals),
2874 old_virtuals = TREE_CHAIN (old_virtuals))
2875 update_vtable_entry_for_fn (t,
2876 binfo,
2877 BV_FN (old_virtuals),
2878 &virtuals, ix);
2880 return NULL_TREE;
2883 /* Update all of the primary and secondary vtables for T. Create new
2884 vtables as required, and initialize their RTTI information. Each
2885 of the functions in VIRTUALS is declared in T and may override a
2886 virtual function from a base class; find and modify the appropriate
2887 entries to point to the overriding functions. Returns a list, in
2888 declaration order, of the virtual functions that are declared in T,
2889 but do not appear in the primary base class vtable, and which
2890 should therefore be appended to the end of the vtable for T. */
2892 static tree
2893 modify_all_vtables (tree t, tree virtuals)
2895 tree binfo = TYPE_BINFO (t);
2896 tree *fnsp;
2898 /* Mangle the vtable name before entering dfs_walk (c++/51884). */
2899 if (TYPE_CONTAINS_VPTR_P (t))
2900 get_vtable_decl (t, false);
2902 /* Update all of the vtables. */
2903 dfs_walk_once (binfo, dfs_modify_vtables, NULL, t);
2905 /* Add virtual functions not already in our primary vtable. These
2906 will be both those introduced by this class, and those overridden
2907 from secondary bases. It does not include virtuals merely
2908 inherited from secondary bases. */
2909 for (fnsp = &virtuals; *fnsp; )
2911 tree fn = TREE_VALUE (*fnsp);
2913 if (!value_member (fn, BINFO_VIRTUALS (binfo))
2914 || DECL_VINDEX (fn) == error_mark_node)
2916 /* We don't need to adjust the `this' pointer when
2917 calling this function. */
2918 BV_DELTA (*fnsp) = integer_zero_node;
2919 BV_VCALL_INDEX (*fnsp) = NULL_TREE;
2921 /* This is a function not already in our vtable. Keep it. */
2922 fnsp = &TREE_CHAIN (*fnsp);
2924 else
2925 /* We've already got an entry for this function. Skip it. */
2926 *fnsp = TREE_CHAIN (*fnsp);
2929 return virtuals;
2932 /* Get the base virtual function declarations in T that have the
2933 indicated NAME. */
2935 static void
2936 get_basefndecls (tree name, tree t, vec<tree> *base_fndecls)
2938 tree methods;
2939 int n_baseclasses = BINFO_N_BASE_BINFOS (TYPE_BINFO (t));
2940 int i;
2942 /* Find virtual functions in T with the indicated NAME. */
2943 i = lookup_fnfields_1 (t, name);
2944 bool found_decls = false;
2945 if (i != -1)
2946 for (methods = (*CLASSTYPE_METHOD_VEC (t))[i];
2947 methods;
2948 methods = OVL_NEXT (methods))
2950 tree method = OVL_CURRENT (methods);
2952 if (TREE_CODE (method) == FUNCTION_DECL
2953 && DECL_VINDEX (method))
2955 base_fndecls->safe_push (method);
2956 found_decls = true;
2960 if (found_decls)
2961 return;
2963 for (i = 0; i < n_baseclasses; i++)
2965 tree basetype = BINFO_TYPE (BINFO_BASE_BINFO (TYPE_BINFO (t), i));
2966 get_basefndecls (name, basetype, base_fndecls);
2970 /* If this declaration supersedes the declaration of
2971 a method declared virtual in the base class, then
2972 mark this field as being virtual as well. */
2974 void
2975 check_for_override (tree decl, tree ctype)
2977 bool overrides_found = false;
2978 if (TREE_CODE (decl) == TEMPLATE_DECL)
2979 /* In [temp.mem] we have:
2981 A specialization of a member function template does not
2982 override a virtual function from a base class. */
2983 return;
2984 if ((DECL_DESTRUCTOR_P (decl)
2985 || IDENTIFIER_VIRTUAL_P (DECL_NAME (decl))
2986 || DECL_CONV_FN_P (decl))
2987 && look_for_overrides (ctype, decl)
2988 && !DECL_STATIC_FUNCTION_P (decl))
2989 /* Set DECL_VINDEX to a value that is neither an INTEGER_CST nor
2990 the error_mark_node so that we know it is an overriding
2991 function. */
2993 DECL_VINDEX (decl) = decl;
2994 overrides_found = true;
2995 if (warn_override && !DECL_OVERRIDE_P (decl)
2996 && !DECL_DESTRUCTOR_P (decl))
2997 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wsuggest_override,
2998 "%qD can be marked override", decl);
3001 if (DECL_VIRTUAL_P (decl))
3003 if (!DECL_VINDEX (decl))
3004 DECL_VINDEX (decl) = error_mark_node;
3005 IDENTIFIER_VIRTUAL_P (DECL_NAME (decl)) = 1;
3006 if (DECL_DESTRUCTOR_P (decl))
3007 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (ctype) = true;
3009 else if (DECL_FINAL_P (decl))
3010 error ("%q+#D marked %<final%>, but is not virtual", decl);
3011 if (DECL_OVERRIDE_P (decl) && !overrides_found)
3012 error ("%q+#D marked %<override%>, but does not override", decl);
3015 /* Warn about hidden virtual functions that are not overridden in t.
3016 We know that constructors and destructors don't apply. */
3018 static void
3019 warn_hidden (tree t)
3021 vec<tree, va_gc> *method_vec = CLASSTYPE_METHOD_VEC (t);
3022 tree fns;
3023 size_t i;
3025 /* We go through each separately named virtual function. */
3026 for (i = CLASSTYPE_FIRST_CONVERSION_SLOT;
3027 vec_safe_iterate (method_vec, i, &fns);
3028 ++i)
3030 tree fn;
3031 tree name;
3032 tree fndecl;
3033 tree base_binfo;
3034 tree binfo;
3035 int j;
3037 /* All functions in this slot in the CLASSTYPE_METHOD_VEC will
3038 have the same name. Figure out what name that is. */
3039 name = DECL_NAME (OVL_CURRENT (fns));
3040 /* There are no possibly hidden functions yet. */
3041 auto_vec<tree, 20> base_fndecls;
3042 /* Iterate through all of the base classes looking for possibly
3043 hidden functions. */
3044 for (binfo = TYPE_BINFO (t), j = 0;
3045 BINFO_BASE_ITERATE (binfo, j, base_binfo); j++)
3047 tree basetype = BINFO_TYPE (base_binfo);
3048 get_basefndecls (name, basetype, &base_fndecls);
3051 /* If there are no functions to hide, continue. */
3052 if (base_fndecls.is_empty ())
3053 continue;
3055 /* Remove any overridden functions. */
3056 for (fn = fns; fn; fn = OVL_NEXT (fn))
3058 fndecl = OVL_CURRENT (fn);
3059 if (TREE_CODE (fndecl) == FUNCTION_DECL
3060 && DECL_VINDEX (fndecl))
3062 /* If the method from the base class has the same
3063 signature as the method from the derived class, it
3064 has been overridden. */
3065 for (size_t k = 0; k < base_fndecls.length (); k++)
3066 if (base_fndecls[k]
3067 && same_signature_p (fndecl, base_fndecls[k]))
3068 base_fndecls[k] = NULL_TREE;
3072 /* Now give a warning for all base functions without overriders,
3073 as they are hidden. */
3074 size_t k;
3075 tree base_fndecl;
3076 FOR_EACH_VEC_ELT (base_fndecls, k, base_fndecl)
3077 if (base_fndecl)
3079 /* Here we know it is a hider, and no overrider exists. */
3080 warning_at (location_of (base_fndecl),
3081 OPT_Woverloaded_virtual,
3082 "%qD was hidden", base_fndecl);
3083 warning_at (location_of (fns),
3084 OPT_Woverloaded_virtual, " by %qD", fns);
3089 /* Recursive helper for finish_struct_anon. */
3091 static void
3092 finish_struct_anon_r (tree field, bool complain)
3094 bool is_union = TREE_CODE (TREE_TYPE (field)) == UNION_TYPE;
3095 tree elt = TYPE_FIELDS (TREE_TYPE (field));
3096 for (; elt; elt = DECL_CHAIN (elt))
3098 /* We're generally only interested in entities the user
3099 declared, but we also find nested classes by noticing
3100 the TYPE_DECL that we create implicitly. You're
3101 allowed to put one anonymous union inside another,
3102 though, so we explicitly tolerate that. We use
3103 TYPE_UNNAMED_P rather than ANON_AGGR_TYPE_P so that
3104 we also allow unnamed types used for defining fields. */
3105 if (DECL_ARTIFICIAL (elt)
3106 && (!DECL_IMPLICIT_TYPEDEF_P (elt)
3107 || TYPE_UNNAMED_P (TREE_TYPE (elt))))
3108 continue;
3110 if (TREE_CODE (elt) != FIELD_DECL)
3112 /* We already complained about static data members in
3113 finish_static_data_member_decl. */
3114 if (complain && !VAR_P (elt))
3116 if (is_union)
3117 permerror (DECL_SOURCE_LOCATION (elt),
3118 "%q#D invalid; an anonymous union can "
3119 "only have non-static data members", elt);
3120 else
3121 permerror (DECL_SOURCE_LOCATION (elt),
3122 "%q#D invalid; an anonymous struct can "
3123 "only have non-static data members", elt);
3125 continue;
3128 if (complain)
3130 if (TREE_PRIVATE (elt))
3132 if (is_union)
3133 permerror (DECL_SOURCE_LOCATION (elt),
3134 "private member %q#D in anonymous union", elt);
3135 else
3136 permerror (DECL_SOURCE_LOCATION (elt),
3137 "private member %q#D in anonymous struct", elt);
3139 else if (TREE_PROTECTED (elt))
3141 if (is_union)
3142 permerror (DECL_SOURCE_LOCATION (elt),
3143 "protected member %q#D in anonymous union", elt);
3144 else
3145 permerror (DECL_SOURCE_LOCATION (elt),
3146 "protected member %q#D in anonymous struct", elt);
3150 TREE_PRIVATE (elt) = TREE_PRIVATE (field);
3151 TREE_PROTECTED (elt) = TREE_PROTECTED (field);
3153 /* Recurse into the anonymous aggregates to handle correctly
3154 access control (c++/24926):
3156 class A {
3157 union {
3158 union {
3159 int i;
3164 int j=A().i; */
3165 if (DECL_NAME (elt) == NULL_TREE
3166 && ANON_AGGR_TYPE_P (TREE_TYPE (elt)))
3167 finish_struct_anon_r (elt, /*complain=*/false);
3171 /* Check for things that are invalid. There are probably plenty of other
3172 things we should check for also. */
3174 static void
3175 finish_struct_anon (tree t)
3177 for (tree field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
3179 if (TREE_STATIC (field))
3180 continue;
3181 if (TREE_CODE (field) != FIELD_DECL)
3182 continue;
3184 if (DECL_NAME (field) == NULL_TREE
3185 && ANON_AGGR_TYPE_P (TREE_TYPE (field)))
3186 finish_struct_anon_r (field, /*complain=*/true);
3190 /* Add T to CLASSTYPE_DECL_LIST of current_class_type which
3191 will be used later during class template instantiation.
3192 When FRIEND_P is zero, T can be a static member data (VAR_DECL),
3193 a non-static member data (FIELD_DECL), a member function
3194 (FUNCTION_DECL), a nested type (RECORD_TYPE, ENUM_TYPE),
3195 a typedef (TYPE_DECL) or a member class template (TEMPLATE_DECL)
3196 When FRIEND_P is nonzero, T is either a friend class
3197 (RECORD_TYPE, TEMPLATE_DECL) or a friend function
3198 (FUNCTION_DECL, TEMPLATE_DECL). */
3200 void
3201 maybe_add_class_template_decl_list (tree type, tree t, int friend_p)
3203 /* Save some memory by not creating TREE_LIST if TYPE is not template. */
3204 if (CLASSTYPE_TEMPLATE_INFO (type))
3205 CLASSTYPE_DECL_LIST (type)
3206 = tree_cons (friend_p ? NULL_TREE : type,
3207 t, CLASSTYPE_DECL_LIST (type));
3210 /* This function is called from declare_virt_assop_and_dtor via
3211 dfs_walk_all.
3213 DATA is a type that direcly or indirectly inherits the base
3214 represented by BINFO. If BINFO contains a virtual assignment [copy
3215 assignment or move assigment] operator or a virtual constructor,
3216 declare that function in DATA if it hasn't been already declared. */
3218 static tree
3219 dfs_declare_virt_assop_and_dtor (tree binfo, void *data)
3221 tree bv, fn, t = (tree)data;
3222 tree opname = ansi_assopname (NOP_EXPR);
3224 gcc_assert (t && CLASS_TYPE_P (t));
3225 gcc_assert (binfo && TREE_CODE (binfo) == TREE_BINFO);
3227 if (!TYPE_CONTAINS_VPTR_P (BINFO_TYPE (binfo)))
3228 /* A base without a vtable needs no modification, and its bases
3229 are uninteresting. */
3230 return dfs_skip_bases;
3232 if (BINFO_PRIMARY_P (binfo))
3233 /* If this is a primary base, then we have already looked at the
3234 virtual functions of its vtable. */
3235 return NULL_TREE;
3237 for (bv = BINFO_VIRTUALS (binfo); bv; bv = TREE_CHAIN (bv))
3239 fn = BV_FN (bv);
3241 if (DECL_NAME (fn) == opname)
3243 if (CLASSTYPE_LAZY_COPY_ASSIGN (t))
3244 lazily_declare_fn (sfk_copy_assignment, t);
3245 if (CLASSTYPE_LAZY_MOVE_ASSIGN (t))
3246 lazily_declare_fn (sfk_move_assignment, t);
3248 else if (DECL_DESTRUCTOR_P (fn)
3249 && CLASSTYPE_LAZY_DESTRUCTOR (t))
3250 lazily_declare_fn (sfk_destructor, t);
3253 return NULL_TREE;
3256 /* If the class type T has a direct or indirect base that contains a
3257 virtual assignment operator or a virtual destructor, declare that
3258 function in T if it hasn't been already declared. */
3260 static void
3261 declare_virt_assop_and_dtor (tree t)
3263 if (!(TYPE_POLYMORPHIC_P (t)
3264 && (CLASSTYPE_LAZY_COPY_ASSIGN (t)
3265 || CLASSTYPE_LAZY_MOVE_ASSIGN (t)
3266 || CLASSTYPE_LAZY_DESTRUCTOR (t))))
3267 return;
3269 dfs_walk_all (TYPE_BINFO (t),
3270 dfs_declare_virt_assop_and_dtor,
3271 NULL, t);
3274 /* Declare the inheriting constructor for class T inherited from base
3275 constructor CTOR with the parameter array PARMS of size NPARMS. */
3277 static void
3278 one_inheriting_sig (tree t, tree ctor, tree *parms, int nparms)
3280 /* We don't declare an inheriting ctor that would be a default,
3281 copy or move ctor for derived or base. */
3282 if (nparms == 0)
3283 return;
3284 if (nparms == 1
3285 && TREE_CODE (parms[0]) == REFERENCE_TYPE)
3287 tree parm = TYPE_MAIN_VARIANT (TREE_TYPE (parms[0]));
3288 if (parm == t || parm == DECL_CONTEXT (ctor))
3289 return;
3292 tree parmlist = void_list_node;
3293 for (int i = nparms - 1; i >= 0; i--)
3294 parmlist = tree_cons (NULL_TREE, parms[i], parmlist);
3295 tree fn = implicitly_declare_fn (sfk_inheriting_constructor,
3296 t, false, ctor, parmlist);
3297 gcc_assert (TYPE_MAIN_VARIANT (t) == t);
3298 if (add_method (t, fn, NULL_TREE))
3300 DECL_CHAIN (fn) = TYPE_METHODS (t);
3301 TYPE_METHODS (t) = fn;
3305 /* Declare all the inheriting constructors for class T inherited from base
3306 constructor CTOR. */
3308 static void
3309 one_inherited_ctor (tree ctor, tree t)
3311 tree parms = FUNCTION_FIRST_USER_PARMTYPE (ctor);
3313 tree *new_parms = XALLOCAVEC (tree, list_length (parms));
3314 int i = 0;
3315 for (; parms && parms != void_list_node; parms = TREE_CHAIN (parms))
3317 if (TREE_PURPOSE (parms))
3318 one_inheriting_sig (t, ctor, new_parms, i);
3319 new_parms[i++] = TREE_VALUE (parms);
3321 one_inheriting_sig (t, ctor, new_parms, i);
3322 if (parms == NULL_TREE)
3324 if (warning (OPT_Winherited_variadic_ctor,
3325 "the ellipsis in %qD is not inherited", ctor))
3326 inform (DECL_SOURCE_LOCATION (ctor), "%qD declared here", ctor);
3330 /* Create default constructors, assignment operators, and so forth for
3331 the type indicated by T, if they are needed. CANT_HAVE_CONST_CTOR,
3332 and CANT_HAVE_CONST_ASSIGNMENT are nonzero if, for whatever reason,
3333 the class cannot have a default constructor, copy constructor
3334 taking a const reference argument, or an assignment operator taking
3335 a const reference, respectively. */
3337 static void
3338 add_implicitly_declared_members (tree t, tree* access_decls,
3339 int cant_have_const_cctor,
3340 int cant_have_const_assignment)
3342 bool move_ok = false;
3344 if (cxx_dialect >= cxx11 && !CLASSTYPE_DESTRUCTORS (t)
3345 && !TYPE_HAS_COPY_CTOR (t) && !TYPE_HAS_COPY_ASSIGN (t)
3346 && !type_has_move_constructor (t) && !type_has_move_assign (t))
3347 move_ok = true;
3349 /* Destructor. */
3350 if (!CLASSTYPE_DESTRUCTORS (t))
3352 /* In general, we create destructors lazily. */
3353 CLASSTYPE_LAZY_DESTRUCTOR (t) = 1;
3355 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t)
3356 && TYPE_FOR_JAVA (t))
3357 /* But if this is a Java class, any non-trivial destructor is
3358 invalid, even if compiler-generated. Therefore, if the
3359 destructor is non-trivial we create it now. */
3360 lazily_declare_fn (sfk_destructor, t);
3363 /* [class.ctor]
3365 If there is no user-declared constructor for a class, a default
3366 constructor is implicitly declared. */
3367 if (! TYPE_HAS_USER_CONSTRUCTOR (t))
3369 TYPE_HAS_DEFAULT_CONSTRUCTOR (t) = 1;
3370 CLASSTYPE_LAZY_DEFAULT_CTOR (t) = 1;
3371 if (cxx_dialect >= cxx11)
3372 TYPE_HAS_CONSTEXPR_CTOR (t)
3373 /* Don't force the declaration to get a hard answer; if the
3374 definition would have made the class non-literal, it will still be
3375 non-literal because of the base or member in question, and that
3376 gives a better diagnostic. */
3377 = type_maybe_constexpr_default_constructor (t);
3380 /* [class.ctor]
3382 If a class definition does not explicitly declare a copy
3383 constructor, one is declared implicitly. */
3384 if (! TYPE_HAS_COPY_CTOR (t) && ! TYPE_FOR_JAVA (t))
3386 TYPE_HAS_COPY_CTOR (t) = 1;
3387 TYPE_HAS_CONST_COPY_CTOR (t) = !cant_have_const_cctor;
3388 CLASSTYPE_LAZY_COPY_CTOR (t) = 1;
3389 if (move_ok)
3390 CLASSTYPE_LAZY_MOVE_CTOR (t) = 1;
3393 /* If there is no assignment operator, one will be created if and
3394 when it is needed. For now, just record whether or not the type
3395 of the parameter to the assignment operator will be a const or
3396 non-const reference. */
3397 if (!TYPE_HAS_COPY_ASSIGN (t) && !TYPE_FOR_JAVA (t))
3399 TYPE_HAS_COPY_ASSIGN (t) = 1;
3400 TYPE_HAS_CONST_COPY_ASSIGN (t) = !cant_have_const_assignment;
3401 CLASSTYPE_LAZY_COPY_ASSIGN (t) = 1;
3402 if (move_ok && !LAMBDA_TYPE_P (t))
3403 CLASSTYPE_LAZY_MOVE_ASSIGN (t) = 1;
3406 /* We can't be lazy about declaring functions that might override
3407 a virtual function from a base class. */
3408 declare_virt_assop_and_dtor (t);
3410 while (*access_decls)
3412 tree using_decl = TREE_VALUE (*access_decls);
3413 tree decl = USING_DECL_DECLS (using_decl);
3414 if (DECL_NAME (using_decl) == ctor_identifier)
3416 /* declare, then remove the decl */
3417 tree ctor_list = decl;
3418 location_t loc = input_location;
3419 input_location = DECL_SOURCE_LOCATION (using_decl);
3420 if (ctor_list)
3421 for (; ctor_list; ctor_list = OVL_NEXT (ctor_list))
3422 one_inherited_ctor (OVL_CURRENT (ctor_list), t);
3423 *access_decls = TREE_CHAIN (*access_decls);
3424 input_location = loc;
3426 else
3427 access_decls = &TREE_CHAIN (*access_decls);
3431 /* Subroutine of insert_into_classtype_sorted_fields. Recursively
3432 count the number of fields in TYPE, including anonymous union
3433 members. */
3435 static int
3436 count_fields (tree fields)
3438 tree x;
3439 int n_fields = 0;
3440 for (x = fields; x; x = DECL_CHAIN (x))
3442 if (TREE_CODE (x) == FIELD_DECL && ANON_AGGR_TYPE_P (TREE_TYPE (x)))
3443 n_fields += count_fields (TYPE_FIELDS (TREE_TYPE (x)));
3444 else
3445 n_fields += 1;
3447 return n_fields;
3450 /* Subroutine of insert_into_classtype_sorted_fields. Recursively add
3451 all the fields in the TREE_LIST FIELDS to the SORTED_FIELDS_TYPE
3452 elts, starting at offset IDX. */
3454 static int
3455 add_fields_to_record_type (tree fields, struct sorted_fields_type *field_vec, int idx)
3457 tree x;
3458 for (x = fields; x; x = DECL_CHAIN (x))
3460 if (TREE_CODE (x) == FIELD_DECL && ANON_AGGR_TYPE_P (TREE_TYPE (x)))
3461 idx = add_fields_to_record_type (TYPE_FIELDS (TREE_TYPE (x)), field_vec, idx);
3462 else
3463 field_vec->elts[idx++] = x;
3465 return idx;
3468 /* Add all of the enum values of ENUMTYPE, to the FIELD_VEC elts,
3469 starting at offset IDX. */
3471 static int
3472 add_enum_fields_to_record_type (tree enumtype,
3473 struct sorted_fields_type *field_vec,
3474 int idx)
3476 tree values;
3477 for (values = TYPE_VALUES (enumtype); values; values = TREE_CHAIN (values))
3478 field_vec->elts[idx++] = TREE_VALUE (values);
3479 return idx;
3482 /* FIELD is a bit-field. We are finishing the processing for its
3483 enclosing type. Issue any appropriate messages and set appropriate
3484 flags. Returns false if an error has been diagnosed. */
3486 static bool
3487 check_bitfield_decl (tree field)
3489 tree type = TREE_TYPE (field);
3490 tree w;
3492 /* Extract the declared width of the bitfield, which has been
3493 temporarily stashed in DECL_INITIAL. */
3494 w = DECL_INITIAL (field);
3495 gcc_assert (w != NULL_TREE);
3496 /* Remove the bit-field width indicator so that the rest of the
3497 compiler does not treat that value as an initializer. */
3498 DECL_INITIAL (field) = NULL_TREE;
3500 /* Detect invalid bit-field type. */
3501 if (!INTEGRAL_OR_ENUMERATION_TYPE_P (type))
3503 error ("bit-field %q+#D with non-integral type", field);
3504 w = error_mark_node;
3506 else
3508 location_t loc = input_location;
3509 /* Avoid the non_lvalue wrapper added by fold for PLUS_EXPRs. */
3510 STRIP_NOPS (w);
3512 /* detect invalid field size. */
3513 input_location = DECL_SOURCE_LOCATION (field);
3514 w = cxx_constant_value (w);
3515 input_location = loc;
3517 if (TREE_CODE (w) != INTEGER_CST)
3519 error ("bit-field %q+D width not an integer constant", field);
3520 w = error_mark_node;
3522 else if (tree_int_cst_sgn (w) < 0)
3524 error ("negative width in bit-field %q+D", field);
3525 w = error_mark_node;
3527 else if (integer_zerop (w) && DECL_NAME (field) != 0)
3529 error ("zero width for bit-field %q+D", field);
3530 w = error_mark_node;
3532 else if ((TREE_CODE (type) != ENUMERAL_TYPE
3533 && TREE_CODE (type) != BOOLEAN_TYPE
3534 && compare_tree_int (w, TYPE_PRECISION (type)) > 0)
3535 || ((TREE_CODE (type) == ENUMERAL_TYPE
3536 || TREE_CODE (type) == BOOLEAN_TYPE)
3537 && tree_int_cst_lt (TYPE_SIZE (type), w)))
3538 warning_at (DECL_SOURCE_LOCATION (field), 0,
3539 "width of %qD exceeds its type", field);
3540 else if (TREE_CODE (type) == ENUMERAL_TYPE
3541 && (0 > (compare_tree_int
3542 (w, TYPE_PRECISION (ENUM_UNDERLYING_TYPE (type))))))
3543 warning_at (DECL_SOURCE_LOCATION (field), 0,
3544 "%qD is too small to hold all values of %q#T",
3545 field, type);
3548 if (w != error_mark_node)
3550 DECL_SIZE (field) = fold_convert (bitsizetype, w);
3551 DECL_BIT_FIELD (field) = 1;
3552 return true;
3554 else
3556 /* Non-bit-fields are aligned for their type. */
3557 DECL_BIT_FIELD (field) = 0;
3558 CLEAR_DECL_C_BIT_FIELD (field);
3559 return false;
3563 /* FIELD is a non bit-field. We are finishing the processing for its
3564 enclosing type T. Issue any appropriate messages and set appropriate
3565 flags. */
3567 static bool
3568 check_field_decl (tree field,
3569 tree t,
3570 int* cant_have_const_ctor,
3571 int* no_const_asn_ref)
3573 tree type = strip_array_types (TREE_TYPE (field));
3574 bool any_default_members = false;
3576 /* In C++98 an anonymous union cannot contain any fields which would change
3577 the settings of CANT_HAVE_CONST_CTOR and friends. */
3578 if (ANON_UNION_TYPE_P (type) && cxx_dialect < cxx11)
3580 /* And, we don't set TYPE_HAS_CONST_COPY_CTOR, etc., for anonymous
3581 structs. So, we recurse through their fields here. */
3582 else if (ANON_AGGR_TYPE_P (type))
3584 for (tree fields = TYPE_FIELDS (type); fields;
3585 fields = DECL_CHAIN (fields))
3586 if (TREE_CODE (fields) == FIELD_DECL && !DECL_C_BIT_FIELD (field))
3587 any_default_members |= check_field_decl (fields, t,
3588 cant_have_const_ctor,
3589 no_const_asn_ref);
3591 /* Check members with class type for constructors, destructors,
3592 etc. */
3593 else if (CLASS_TYPE_P (type))
3595 /* Never let anything with uninheritable virtuals
3596 make it through without complaint. */
3597 abstract_virtuals_error (field, type);
3599 if (TREE_CODE (t) == UNION_TYPE && cxx_dialect < cxx11)
3601 static bool warned;
3602 int oldcount = errorcount;
3603 if (TYPE_NEEDS_CONSTRUCTING (type))
3604 error ("member %q+#D with constructor not allowed in union",
3605 field);
3606 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
3607 error ("member %q+#D with destructor not allowed in union", field);
3608 if (TYPE_HAS_COMPLEX_COPY_ASSIGN (type))
3609 error ("member %q+#D with copy assignment operator not allowed in union",
3610 field);
3611 if (!warned && errorcount > oldcount)
3613 inform (DECL_SOURCE_LOCATION (field), "unrestricted unions "
3614 "only available with -std=c++11 or -std=gnu++11");
3615 warned = true;
3618 else
3620 TYPE_NEEDS_CONSTRUCTING (t) |= TYPE_NEEDS_CONSTRUCTING (type);
3621 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t)
3622 |= TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type);
3623 TYPE_HAS_COMPLEX_COPY_ASSIGN (t)
3624 |= (TYPE_HAS_COMPLEX_COPY_ASSIGN (type)
3625 || !TYPE_HAS_COPY_ASSIGN (type));
3626 TYPE_HAS_COMPLEX_COPY_CTOR (t) |= (TYPE_HAS_COMPLEX_COPY_CTOR (type)
3627 || !TYPE_HAS_COPY_CTOR (type));
3628 TYPE_HAS_COMPLEX_MOVE_ASSIGN (t) |= TYPE_HAS_COMPLEX_MOVE_ASSIGN (type);
3629 TYPE_HAS_COMPLEX_MOVE_CTOR (t) |= TYPE_HAS_COMPLEX_MOVE_CTOR (type);
3630 TYPE_HAS_COMPLEX_DFLT (t) |= (!TYPE_HAS_DEFAULT_CONSTRUCTOR (type)
3631 || TYPE_HAS_COMPLEX_DFLT (type));
3634 if (TYPE_HAS_COPY_CTOR (type)
3635 && !TYPE_HAS_CONST_COPY_CTOR (type))
3636 *cant_have_const_ctor = 1;
3638 if (TYPE_HAS_COPY_ASSIGN (type)
3639 && !TYPE_HAS_CONST_COPY_ASSIGN (type))
3640 *no_const_asn_ref = 1;
3643 check_abi_tags (t, field);
3645 if (DECL_INITIAL (field) != NULL_TREE)
3646 /* `build_class_init_list' does not recognize
3647 non-FIELD_DECLs. */
3648 any_default_members = true;
3650 return any_default_members;
3653 /* Check the data members (both static and non-static), class-scoped
3654 typedefs, etc., appearing in the declaration of T. Issue
3655 appropriate diagnostics. Sets ACCESS_DECLS to a list (in
3656 declaration order) of access declarations; each TREE_VALUE in this
3657 list is a USING_DECL.
3659 In addition, set the following flags:
3661 EMPTY_P
3662 The class is empty, i.e., contains no non-static data members.
3664 CANT_HAVE_CONST_CTOR_P
3665 This class cannot have an implicitly generated copy constructor
3666 taking a const reference.
3668 CANT_HAVE_CONST_ASN_REF
3669 This class cannot have an implicitly generated assignment
3670 operator taking a const reference.
3672 All of these flags should be initialized before calling this
3673 function.
3675 Returns a pointer to the end of the TYPE_FIELDs chain; additional
3676 fields can be added by adding to this chain. */
3678 static void
3679 check_field_decls (tree t, tree *access_decls,
3680 int *cant_have_const_ctor_p,
3681 int *no_const_asn_ref_p)
3683 tree *field;
3684 tree *next;
3685 bool has_pointers;
3686 bool any_default_members;
3687 int cant_pack = 0;
3688 int field_access = -1;
3690 /* Assume there are no access declarations. */
3691 *access_decls = NULL_TREE;
3692 /* Assume this class has no pointer members. */
3693 has_pointers = false;
3694 /* Assume none of the members of this class have default
3695 initializations. */
3696 any_default_members = false;
3698 for (field = &TYPE_FIELDS (t); *field; field = next)
3700 tree x = *field;
3701 tree type = TREE_TYPE (x);
3702 int this_field_access;
3704 next = &DECL_CHAIN (x);
3706 if (TREE_CODE (x) == USING_DECL)
3708 /* Save the access declarations for our caller. */
3709 *access_decls = tree_cons (NULL_TREE, x, *access_decls);
3710 continue;
3713 if (TREE_CODE (x) == TYPE_DECL
3714 || TREE_CODE (x) == TEMPLATE_DECL)
3715 continue;
3717 /* If we've gotten this far, it's a data member, possibly static,
3718 or an enumerator. */
3719 if (TREE_CODE (x) != CONST_DECL)
3720 DECL_CONTEXT (x) = t;
3722 /* When this goes into scope, it will be a non-local reference. */
3723 DECL_NONLOCAL (x) = 1;
3725 if (TREE_CODE (t) == UNION_TYPE
3726 && cxx_dialect < cxx11)
3728 /* [class.union] (C++98)
3730 If a union contains a static data member, or a member of
3731 reference type, the program is ill-formed.
3733 In C++11 this limitation doesn't exist anymore. */
3734 if (VAR_P (x))
3736 error ("in C++98 %q+D may not be static because it is "
3737 "a member of a union", x);
3738 continue;
3740 if (TREE_CODE (type) == REFERENCE_TYPE)
3742 error ("in C++98 %q+D may not have reference type %qT "
3743 "because it is a member of a union", x, type);
3744 continue;
3748 /* Perform error checking that did not get done in
3749 grokdeclarator. */
3750 if (TREE_CODE (type) == FUNCTION_TYPE)
3752 error ("field %q+D invalidly declared function type", x);
3753 type = build_pointer_type (type);
3754 TREE_TYPE (x) = type;
3756 else if (TREE_CODE (type) == METHOD_TYPE)
3758 error ("field %q+D invalidly declared method type", x);
3759 type = build_pointer_type (type);
3760 TREE_TYPE (x) = type;
3763 if (type == error_mark_node)
3764 continue;
3766 if (TREE_CODE (x) == CONST_DECL || VAR_P (x))
3767 continue;
3769 /* Now it can only be a FIELD_DECL. */
3771 if (TREE_PRIVATE (x) || TREE_PROTECTED (x))
3772 CLASSTYPE_NON_AGGREGATE (t) = 1;
3774 /* If at least one non-static data member is non-literal, the whole
3775 class becomes non-literal. Per Core/1453, volatile non-static
3776 data members and base classes are also not allowed.
3777 Note: if the type is incomplete we will complain later on. */
3778 if (COMPLETE_TYPE_P (type)
3779 && (!literal_type_p (type) || CP_TYPE_VOLATILE_P (type)))
3780 CLASSTYPE_LITERAL_P (t) = false;
3782 /* A standard-layout class is a class that:
3784 has the same access control (Clause 11) for all non-static data members,
3785 ... */
3786 this_field_access = TREE_PROTECTED (x) ? 1 : TREE_PRIVATE (x) ? 2 : 0;
3787 if (field_access == -1)
3788 field_access = this_field_access;
3789 else if (this_field_access != field_access)
3790 CLASSTYPE_NON_STD_LAYOUT (t) = 1;
3792 /* If this is of reference type, check if it needs an init. */
3793 if (TREE_CODE (type) == REFERENCE_TYPE)
3795 CLASSTYPE_NON_LAYOUT_POD_P (t) = 1;
3796 CLASSTYPE_NON_STD_LAYOUT (t) = 1;
3797 if (DECL_INITIAL (x) == NULL_TREE)
3798 SET_CLASSTYPE_REF_FIELDS_NEED_INIT (t, 1);
3799 if (cxx_dialect < cxx11)
3801 /* ARM $12.6.2: [A member initializer list] (or, for an
3802 aggregate, initialization by a brace-enclosed list) is the
3803 only way to initialize nonstatic const and reference
3804 members. */
3805 TYPE_HAS_COMPLEX_COPY_ASSIGN (t) = 1;
3806 TYPE_HAS_COMPLEX_MOVE_ASSIGN (t) = 1;
3810 type = strip_array_types (type);
3812 if (TYPE_PACKED (t))
3814 if (!layout_pod_type_p (type) && !TYPE_PACKED (type))
3816 warning_at
3817 (DECL_SOURCE_LOCATION (x), 0,
3818 "ignoring packed attribute because of unpacked non-POD field %q#D",
3820 cant_pack = 1;
3822 else if (DECL_C_BIT_FIELD (x)
3823 || TYPE_ALIGN (TREE_TYPE (x)) > BITS_PER_UNIT)
3824 DECL_PACKED (x) = 1;
3827 if (DECL_C_BIT_FIELD (x) && integer_zerop (DECL_INITIAL (x)))
3828 /* We don't treat zero-width bitfields as making a class
3829 non-empty. */
3831 else
3833 /* The class is non-empty. */
3834 CLASSTYPE_EMPTY_P (t) = 0;
3835 /* The class is not even nearly empty. */
3836 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
3837 /* If one of the data members contains an empty class,
3838 so does T. */
3839 if (CLASS_TYPE_P (type)
3840 && CLASSTYPE_CONTAINS_EMPTY_CLASS_P (type))
3841 CLASSTYPE_CONTAINS_EMPTY_CLASS_P (t) = 1;
3844 /* This is used by -Weffc++ (see below). Warn only for pointers
3845 to members which might hold dynamic memory. So do not warn
3846 for pointers to functions or pointers to members. */
3847 if (TYPE_PTR_P (type)
3848 && !TYPE_PTRFN_P (type))
3849 has_pointers = true;
3851 if (CLASS_TYPE_P (type))
3853 if (CLASSTYPE_REF_FIELDS_NEED_INIT (type))
3854 SET_CLASSTYPE_REF_FIELDS_NEED_INIT (t, 1);
3855 if (CLASSTYPE_READONLY_FIELDS_NEED_INIT (type))
3856 SET_CLASSTYPE_READONLY_FIELDS_NEED_INIT (t, 1);
3859 if (DECL_MUTABLE_P (x) || TYPE_HAS_MUTABLE_P (type))
3860 CLASSTYPE_HAS_MUTABLE (t) = 1;
3862 if (DECL_MUTABLE_P (x))
3864 if (CP_TYPE_CONST_P (type))
3866 error ("member %q+D cannot be declared both %<const%> "
3867 "and %<mutable%>", x);
3868 continue;
3870 if (TREE_CODE (type) == REFERENCE_TYPE)
3872 error ("member %q+D cannot be declared as a %<mutable%> "
3873 "reference", x);
3874 continue;
3878 if (! layout_pod_type_p (type))
3879 /* DR 148 now allows pointers to members (which are POD themselves),
3880 to be allowed in POD structs. */
3881 CLASSTYPE_NON_LAYOUT_POD_P (t) = 1;
3883 if (!std_layout_type_p (type))
3884 CLASSTYPE_NON_STD_LAYOUT (t) = 1;
3886 if (! zero_init_p (type))
3887 CLASSTYPE_NON_ZERO_INIT_P (t) = 1;
3889 /* We set DECL_C_BIT_FIELD in grokbitfield.
3890 If the type and width are valid, we'll also set DECL_BIT_FIELD. */
3891 if ((! DECL_C_BIT_FIELD (x) || ! check_bitfield_decl (x))
3892 && check_field_decl (x, t,
3893 cant_have_const_ctor_p,
3894 no_const_asn_ref_p))
3896 if (any_default_members
3897 && TREE_CODE (t) == UNION_TYPE)
3898 error ("multiple fields in union %qT initialized", t);
3899 any_default_members = true;
3902 /* Now that we've removed bit-field widths from DECL_INITIAL,
3903 anything left in DECL_INITIAL is an NSDMI that makes the class
3904 non-aggregate in C++11. */
3905 if (DECL_INITIAL (x) && cxx_dialect < cxx14)
3906 CLASSTYPE_NON_AGGREGATE (t) = true;
3908 /* If any field is const, the structure type is pseudo-const. */
3909 if (CP_TYPE_CONST_P (type))
3911 C_TYPE_FIELDS_READONLY (t) = 1;
3912 if (DECL_INITIAL (x) == NULL_TREE)
3913 SET_CLASSTYPE_READONLY_FIELDS_NEED_INIT (t, 1);
3914 if (cxx_dialect < cxx11)
3916 /* ARM $12.6.2: [A member initializer list] (or, for an
3917 aggregate, initialization by a brace-enclosed list) is the
3918 only way to initialize nonstatic const and reference
3919 members. */
3920 TYPE_HAS_COMPLEX_COPY_ASSIGN (t) = 1;
3921 TYPE_HAS_COMPLEX_MOVE_ASSIGN (t) = 1;
3924 /* A field that is pseudo-const makes the structure likewise. */
3925 else if (CLASS_TYPE_P (type))
3927 C_TYPE_FIELDS_READONLY (t) |= C_TYPE_FIELDS_READONLY (type);
3928 SET_CLASSTYPE_READONLY_FIELDS_NEED_INIT (t,
3929 CLASSTYPE_READONLY_FIELDS_NEED_INIT (t)
3930 | CLASSTYPE_READONLY_FIELDS_NEED_INIT (type));
3933 /* Core issue 80: A nonstatic data member is required to have a
3934 different name from the class iff the class has a
3935 user-declared constructor. */
3936 if (constructor_name_p (DECL_NAME (x), t)
3937 && TYPE_HAS_USER_CONSTRUCTOR (t))
3938 permerror (DECL_SOURCE_LOCATION (x),
3939 "field %q#D with same name as class", x);
3942 /* Effective C++ rule 11: if a class has dynamic memory held by pointers,
3943 it should also define a copy constructor and an assignment operator to
3944 implement the correct copy semantic (deep vs shallow, etc.). As it is
3945 not feasible to check whether the constructors do allocate dynamic memory
3946 and store it within members, we approximate the warning like this:
3948 -- Warn only if there are members which are pointers
3949 -- Warn only if there is a non-trivial constructor (otherwise,
3950 there cannot be memory allocated).
3951 -- Warn only if there is a non-trivial destructor. We assume that the
3952 user at least implemented the cleanup correctly, and a destructor
3953 is needed to free dynamic memory.
3955 This seems enough for practical purposes. */
3956 if (warn_ecpp
3957 && has_pointers
3958 && TYPE_HAS_USER_CONSTRUCTOR (t)
3959 && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t)
3960 && !(TYPE_HAS_COPY_CTOR (t) && TYPE_HAS_COPY_ASSIGN (t)))
3962 warning (OPT_Weffc__, "%q#T has pointer data members", t);
3964 if (! TYPE_HAS_COPY_CTOR (t))
3966 warning (OPT_Weffc__,
3967 " but does not override %<%T(const %T&)%>", t, t);
3968 if (!TYPE_HAS_COPY_ASSIGN (t))
3969 warning (OPT_Weffc__, " or %<operator=(const %T&)%>", t);
3971 else if (! TYPE_HAS_COPY_ASSIGN (t))
3972 warning (OPT_Weffc__,
3973 " but does not override %<operator=(const %T&)%>", t);
3976 /* Non-static data member initializers make the default constructor
3977 non-trivial. */
3978 if (any_default_members)
3980 TYPE_NEEDS_CONSTRUCTING (t) = true;
3981 TYPE_HAS_COMPLEX_DFLT (t) = true;
3984 /* If any of the fields couldn't be packed, unset TYPE_PACKED. */
3985 if (cant_pack)
3986 TYPE_PACKED (t) = 0;
3988 /* Check anonymous struct/anonymous union fields. */
3989 finish_struct_anon (t);
3991 /* We've built up the list of access declarations in reverse order.
3992 Fix that now. */
3993 *access_decls = nreverse (*access_decls);
3996 /* If TYPE is an empty class type, records its OFFSET in the table of
3997 OFFSETS. */
3999 static int
4000 record_subobject_offset (tree type, tree offset, splay_tree offsets)
4002 splay_tree_node n;
4004 if (!is_empty_class (type))
4005 return 0;
4007 /* Record the location of this empty object in OFFSETS. */
4008 n = splay_tree_lookup (offsets, (splay_tree_key) offset);
4009 if (!n)
4010 n = splay_tree_insert (offsets,
4011 (splay_tree_key) offset,
4012 (splay_tree_value) NULL_TREE);
4013 n->value = ((splay_tree_value)
4014 tree_cons (NULL_TREE,
4015 type,
4016 (tree) n->value));
4018 return 0;
4021 /* Returns nonzero if TYPE is an empty class type and there is
4022 already an entry in OFFSETS for the same TYPE as the same OFFSET. */
4024 static int
4025 check_subobject_offset (tree type, tree offset, splay_tree offsets)
4027 splay_tree_node n;
4028 tree t;
4030 if (!is_empty_class (type))
4031 return 0;
4033 /* Record the location of this empty object in OFFSETS. */
4034 n = splay_tree_lookup (offsets, (splay_tree_key) offset);
4035 if (!n)
4036 return 0;
4038 for (t = (tree) n->value; t; t = TREE_CHAIN (t))
4039 if (same_type_p (TREE_VALUE (t), type))
4040 return 1;
4042 return 0;
4045 /* Walk through all the subobjects of TYPE (located at OFFSET). Call
4046 F for every subobject, passing it the type, offset, and table of
4047 OFFSETS. If VBASES_P is one, then virtual non-primary bases should
4048 be traversed.
4050 If MAX_OFFSET is non-NULL, then subobjects with an offset greater
4051 than MAX_OFFSET will not be walked.
4053 If F returns a nonzero value, the traversal ceases, and that value
4054 is returned. Otherwise, returns zero. */
4056 static int
4057 walk_subobject_offsets (tree type,
4058 subobject_offset_fn f,
4059 tree offset,
4060 splay_tree offsets,
4061 tree max_offset,
4062 int vbases_p)
4064 int r = 0;
4065 tree type_binfo = NULL_TREE;
4067 /* If this OFFSET is bigger than the MAX_OFFSET, then we should
4068 stop. */
4069 if (max_offset && tree_int_cst_lt (max_offset, offset))
4070 return 0;
4072 if (type == error_mark_node)
4073 return 0;
4075 if (!TYPE_P (type))
4077 type_binfo = type;
4078 type = BINFO_TYPE (type);
4081 if (CLASS_TYPE_P (type))
4083 tree field;
4084 tree binfo;
4085 int i;
4087 /* Avoid recursing into objects that are not interesting. */
4088 if (!CLASSTYPE_CONTAINS_EMPTY_CLASS_P (type))
4089 return 0;
4091 /* Record the location of TYPE. */
4092 r = (*f) (type, offset, offsets);
4093 if (r)
4094 return r;
4096 /* Iterate through the direct base classes of TYPE. */
4097 if (!type_binfo)
4098 type_binfo = TYPE_BINFO (type);
4099 for (i = 0; BINFO_BASE_ITERATE (type_binfo, i, binfo); i++)
4101 tree binfo_offset;
4103 if (BINFO_VIRTUAL_P (binfo))
4104 continue;
4106 tree orig_binfo;
4107 /* We cannot rely on BINFO_OFFSET being set for the base
4108 class yet, but the offsets for direct non-virtual
4109 bases can be calculated by going back to the TYPE. */
4110 orig_binfo = BINFO_BASE_BINFO (TYPE_BINFO (type), i);
4111 binfo_offset = size_binop (PLUS_EXPR,
4112 offset,
4113 BINFO_OFFSET (orig_binfo));
4115 r = walk_subobject_offsets (binfo,
4117 binfo_offset,
4118 offsets,
4119 max_offset,
4120 /*vbases_p=*/0);
4121 if (r)
4122 return r;
4125 if (CLASSTYPE_VBASECLASSES (type))
4127 unsigned ix;
4128 vec<tree, va_gc> *vbases;
4130 /* Iterate through the virtual base classes of TYPE. In G++
4131 3.2, we included virtual bases in the direct base class
4132 loop above, which results in incorrect results; the
4133 correct offsets for virtual bases are only known when
4134 working with the most derived type. */
4135 if (vbases_p)
4136 for (vbases = CLASSTYPE_VBASECLASSES (type), ix = 0;
4137 vec_safe_iterate (vbases, ix, &binfo); ix++)
4139 r = walk_subobject_offsets (binfo,
4141 size_binop (PLUS_EXPR,
4142 offset,
4143 BINFO_OFFSET (binfo)),
4144 offsets,
4145 max_offset,
4146 /*vbases_p=*/0);
4147 if (r)
4148 return r;
4150 else
4152 /* We still have to walk the primary base, if it is
4153 virtual. (If it is non-virtual, then it was walked
4154 above.) */
4155 tree vbase = get_primary_binfo (type_binfo);
4157 if (vbase && BINFO_VIRTUAL_P (vbase)
4158 && BINFO_PRIMARY_P (vbase)
4159 && BINFO_INHERITANCE_CHAIN (vbase) == type_binfo)
4161 r = (walk_subobject_offsets
4162 (vbase, f, offset,
4163 offsets, max_offset, /*vbases_p=*/0));
4164 if (r)
4165 return r;
4170 /* Iterate through the fields of TYPE. */
4171 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
4172 if (TREE_CODE (field) == FIELD_DECL
4173 && TREE_TYPE (field) != error_mark_node
4174 && !DECL_ARTIFICIAL (field))
4176 tree field_offset;
4178 field_offset = byte_position (field);
4180 r = walk_subobject_offsets (TREE_TYPE (field),
4182 size_binop (PLUS_EXPR,
4183 offset,
4184 field_offset),
4185 offsets,
4186 max_offset,
4187 /*vbases_p=*/1);
4188 if (r)
4189 return r;
4192 else if (TREE_CODE (type) == ARRAY_TYPE)
4194 tree element_type = strip_array_types (type);
4195 tree domain = TYPE_DOMAIN (type);
4196 tree index;
4198 /* Avoid recursing into objects that are not interesting. */
4199 if (!CLASS_TYPE_P (element_type)
4200 || !CLASSTYPE_CONTAINS_EMPTY_CLASS_P (element_type)
4201 || !domain
4202 || integer_minus_onep (TYPE_MAX_VALUE (domain)))
4203 return 0;
4205 /* Step through each of the elements in the array. */
4206 for (index = size_zero_node;
4207 !tree_int_cst_lt (TYPE_MAX_VALUE (domain), index);
4208 index = size_binop (PLUS_EXPR, index, size_one_node))
4210 r = walk_subobject_offsets (TREE_TYPE (type),
4212 offset,
4213 offsets,
4214 max_offset,
4215 /*vbases_p=*/1);
4216 if (r)
4217 return r;
4218 offset = size_binop (PLUS_EXPR, offset,
4219 TYPE_SIZE_UNIT (TREE_TYPE (type)));
4220 /* If this new OFFSET is bigger than the MAX_OFFSET, then
4221 there's no point in iterating through the remaining
4222 elements of the array. */
4223 if (max_offset && tree_int_cst_lt (max_offset, offset))
4224 break;
4228 return 0;
4231 /* Record all of the empty subobjects of TYPE (either a type or a
4232 binfo). If IS_DATA_MEMBER is true, then a non-static data member
4233 is being placed at OFFSET; otherwise, it is a base class that is
4234 being placed at OFFSET. */
4236 static void
4237 record_subobject_offsets (tree type,
4238 tree offset,
4239 splay_tree offsets,
4240 bool is_data_member)
4242 tree max_offset;
4243 /* If recording subobjects for a non-static data member or a
4244 non-empty base class , we do not need to record offsets beyond
4245 the size of the biggest empty class. Additional data members
4246 will go at the end of the class. Additional base classes will go
4247 either at offset zero (if empty, in which case they cannot
4248 overlap with offsets past the size of the biggest empty class) or
4249 at the end of the class.
4251 However, if we are placing an empty base class, then we must record
4252 all offsets, as either the empty class is at offset zero (where
4253 other empty classes might later be placed) or at the end of the
4254 class (where other objects might then be placed, so other empty
4255 subobjects might later overlap). */
4256 if (is_data_member
4257 || !is_empty_class (BINFO_TYPE (type)))
4258 max_offset = sizeof_biggest_empty_class;
4259 else
4260 max_offset = NULL_TREE;
4261 walk_subobject_offsets (type, record_subobject_offset, offset,
4262 offsets, max_offset, is_data_member);
4265 /* Returns nonzero if any of the empty subobjects of TYPE (located at
4266 OFFSET) conflict with entries in OFFSETS. If VBASES_P is nonzero,
4267 virtual bases of TYPE are examined. */
4269 static int
4270 layout_conflict_p (tree type,
4271 tree offset,
4272 splay_tree offsets,
4273 int vbases_p)
4275 splay_tree_node max_node;
4277 /* Get the node in OFFSETS that indicates the maximum offset where
4278 an empty subobject is located. */
4279 max_node = splay_tree_max (offsets);
4280 /* If there aren't any empty subobjects, then there's no point in
4281 performing this check. */
4282 if (!max_node)
4283 return 0;
4285 return walk_subobject_offsets (type, check_subobject_offset, offset,
4286 offsets, (tree) (max_node->key),
4287 vbases_p);
4290 /* DECL is a FIELD_DECL corresponding either to a base subobject of a
4291 non-static data member of the type indicated by RLI. BINFO is the
4292 binfo corresponding to the base subobject, OFFSETS maps offsets to
4293 types already located at those offsets. This function determines
4294 the position of the DECL. */
4296 static void
4297 layout_nonempty_base_or_field (record_layout_info rli,
4298 tree decl,
4299 tree binfo,
4300 splay_tree offsets)
4302 tree offset = NULL_TREE;
4303 bool field_p;
4304 tree type;
4306 if (binfo)
4308 /* For the purposes of determining layout conflicts, we want to
4309 use the class type of BINFO; TREE_TYPE (DECL) will be the
4310 CLASSTYPE_AS_BASE version, which does not contain entries for
4311 zero-sized bases. */
4312 type = TREE_TYPE (binfo);
4313 field_p = false;
4315 else
4317 type = TREE_TYPE (decl);
4318 field_p = true;
4321 /* Try to place the field. It may take more than one try if we have
4322 a hard time placing the field without putting two objects of the
4323 same type at the same address. */
4324 while (1)
4326 struct record_layout_info_s old_rli = *rli;
4328 /* Place this field. */
4329 place_field (rli, decl);
4330 offset = byte_position (decl);
4332 /* We have to check to see whether or not there is already
4333 something of the same type at the offset we're about to use.
4334 For example, consider:
4336 struct S {};
4337 struct T : public S { int i; };
4338 struct U : public S, public T {};
4340 Here, we put S at offset zero in U. Then, we can't put T at
4341 offset zero -- its S component would be at the same address
4342 as the S we already allocated. So, we have to skip ahead.
4343 Since all data members, including those whose type is an
4344 empty class, have nonzero size, any overlap can happen only
4345 with a direct or indirect base-class -- it can't happen with
4346 a data member. */
4347 /* In a union, overlap is permitted; all members are placed at
4348 offset zero. */
4349 if (TREE_CODE (rli->t) == UNION_TYPE)
4350 break;
4351 if (layout_conflict_p (field_p ? type : binfo, offset,
4352 offsets, field_p))
4354 /* Strip off the size allocated to this field. That puts us
4355 at the first place we could have put the field with
4356 proper alignment. */
4357 *rli = old_rli;
4359 /* Bump up by the alignment required for the type. */
4360 rli->bitpos
4361 = size_binop (PLUS_EXPR, rli->bitpos,
4362 bitsize_int (binfo
4363 ? CLASSTYPE_ALIGN (type)
4364 : TYPE_ALIGN (type)));
4365 normalize_rli (rli);
4367 else if (TREE_CODE (type) == NULLPTR_TYPE
4368 && warn_abi && abi_version_crosses (9))
4370 /* Before ABI v9, we were giving nullptr_t alignment of 1; if
4371 the offset wasn't aligned like a pointer when we started to
4372 layout this field, that affects its position. */
4373 tree pos = rli_size_unit_so_far (&old_rli);
4374 if (int_cst_value (pos) % TYPE_ALIGN_UNIT (ptr_type_node) != 0)
4376 if (abi_version_at_least (9))
4377 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wabi,
4378 "alignment of %qD increased in -fabi-version=9 "
4379 "(GCC 5.2)", decl);
4380 else
4381 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wabi, "alignment "
4382 "of %qD will increase in -fabi-version=9", decl);
4384 break;
4386 else
4387 /* There was no conflict. We're done laying out this field. */
4388 break;
4391 /* Now that we know where it will be placed, update its
4392 BINFO_OFFSET. */
4393 if (binfo && CLASS_TYPE_P (BINFO_TYPE (binfo)))
4394 /* Indirect virtual bases may have a nonzero BINFO_OFFSET at
4395 this point because their BINFO_OFFSET is copied from another
4396 hierarchy. Therefore, we may not need to add the entire
4397 OFFSET. */
4398 propagate_binfo_offsets (binfo,
4399 size_diffop_loc (input_location,
4400 fold_convert (ssizetype, offset),
4401 fold_convert (ssizetype,
4402 BINFO_OFFSET (binfo))));
4405 /* Returns true if TYPE is empty and OFFSET is nonzero. */
4407 static int
4408 empty_base_at_nonzero_offset_p (tree type,
4409 tree offset,
4410 splay_tree /*offsets*/)
4412 return is_empty_class (type) && !integer_zerop (offset);
4415 /* Layout the empty base BINFO. EOC indicates the byte currently just
4416 past the end of the class, and should be correctly aligned for a
4417 class of the type indicated by BINFO; OFFSETS gives the offsets of
4418 the empty bases allocated so far. T is the most derived
4419 type. Return nonzero iff we added it at the end. */
4421 static bool
4422 layout_empty_base (record_layout_info rli, tree binfo,
4423 tree eoc, splay_tree offsets)
4425 tree alignment;
4426 tree basetype = BINFO_TYPE (binfo);
4427 bool atend = false;
4429 /* This routine should only be used for empty classes. */
4430 gcc_assert (is_empty_class (basetype));
4431 alignment = ssize_int (CLASSTYPE_ALIGN_UNIT (basetype));
4433 if (!integer_zerop (BINFO_OFFSET (binfo)))
4434 propagate_binfo_offsets
4435 (binfo, size_diffop_loc (input_location,
4436 size_zero_node, BINFO_OFFSET (binfo)));
4438 /* This is an empty base class. We first try to put it at offset
4439 zero. */
4440 if (layout_conflict_p (binfo,
4441 BINFO_OFFSET (binfo),
4442 offsets,
4443 /*vbases_p=*/0))
4445 /* That didn't work. Now, we move forward from the next
4446 available spot in the class. */
4447 atend = true;
4448 propagate_binfo_offsets (binfo, fold_convert (ssizetype, eoc));
4449 while (1)
4451 if (!layout_conflict_p (binfo,
4452 BINFO_OFFSET (binfo),
4453 offsets,
4454 /*vbases_p=*/0))
4455 /* We finally found a spot where there's no overlap. */
4456 break;
4458 /* There's overlap here, too. Bump along to the next spot. */
4459 propagate_binfo_offsets (binfo, alignment);
4463 if (CLASSTYPE_USER_ALIGN (basetype))
4465 rli->record_align = MAX (rli->record_align, CLASSTYPE_ALIGN (basetype));
4466 if (warn_packed)
4467 rli->unpacked_align = MAX (rli->unpacked_align, CLASSTYPE_ALIGN (basetype));
4468 TYPE_USER_ALIGN (rli->t) = 1;
4471 return atend;
4474 /* Layout the base given by BINFO in the class indicated by RLI.
4475 *BASE_ALIGN is a running maximum of the alignments of
4476 any base class. OFFSETS gives the location of empty base
4477 subobjects. T is the most derived type. Return nonzero if the new
4478 object cannot be nearly-empty. A new FIELD_DECL is inserted at
4479 *NEXT_FIELD, unless BINFO is for an empty base class.
4481 Returns the location at which the next field should be inserted. */
4483 static tree *
4484 build_base_field (record_layout_info rli, tree binfo,
4485 splay_tree offsets, tree *next_field)
4487 tree t = rli->t;
4488 tree basetype = BINFO_TYPE (binfo);
4490 if (!COMPLETE_TYPE_P (basetype))
4491 /* This error is now reported in xref_tag, thus giving better
4492 location information. */
4493 return next_field;
4495 /* Place the base class. */
4496 if (!is_empty_class (basetype))
4498 tree decl;
4500 /* The containing class is non-empty because it has a non-empty
4501 base class. */
4502 CLASSTYPE_EMPTY_P (t) = 0;
4504 /* Create the FIELD_DECL. */
4505 decl = build_decl (input_location,
4506 FIELD_DECL, NULL_TREE, CLASSTYPE_AS_BASE (basetype));
4507 DECL_ARTIFICIAL (decl) = 1;
4508 DECL_IGNORED_P (decl) = 1;
4509 DECL_FIELD_CONTEXT (decl) = t;
4510 if (CLASSTYPE_AS_BASE (basetype))
4512 DECL_SIZE (decl) = CLASSTYPE_SIZE (basetype);
4513 DECL_SIZE_UNIT (decl) = CLASSTYPE_SIZE_UNIT (basetype);
4514 SET_DECL_ALIGN (decl, CLASSTYPE_ALIGN (basetype));
4515 DECL_USER_ALIGN (decl) = CLASSTYPE_USER_ALIGN (basetype);
4516 DECL_MODE (decl) = TYPE_MODE (basetype);
4517 DECL_FIELD_IS_BASE (decl) = 1;
4519 /* Try to place the field. It may take more than one try if we
4520 have a hard time placing the field without putting two
4521 objects of the same type at the same address. */
4522 layout_nonempty_base_or_field (rli, decl, binfo, offsets);
4523 /* Add the new FIELD_DECL to the list of fields for T. */
4524 DECL_CHAIN (decl) = *next_field;
4525 *next_field = decl;
4526 next_field = &DECL_CHAIN (decl);
4529 else
4531 tree eoc;
4532 bool atend;
4534 /* On some platforms (ARM), even empty classes will not be
4535 byte-aligned. */
4536 eoc = round_up_loc (input_location,
4537 rli_size_unit_so_far (rli),
4538 CLASSTYPE_ALIGN_UNIT (basetype));
4539 atend = layout_empty_base (rli, binfo, eoc, offsets);
4540 /* A nearly-empty class "has no proper base class that is empty,
4541 not morally virtual, and at an offset other than zero." */
4542 if (!BINFO_VIRTUAL_P (binfo) && CLASSTYPE_NEARLY_EMPTY_P (t))
4544 if (atend)
4545 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
4546 /* The check above (used in G++ 3.2) is insufficient because
4547 an empty class placed at offset zero might itself have an
4548 empty base at a nonzero offset. */
4549 else if (walk_subobject_offsets (basetype,
4550 empty_base_at_nonzero_offset_p,
4551 size_zero_node,
4552 /*offsets=*/NULL,
4553 /*max_offset=*/NULL_TREE,
4554 /*vbases_p=*/true))
4555 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
4558 /* We do not create a FIELD_DECL for empty base classes because
4559 it might overlap some other field. We want to be able to
4560 create CONSTRUCTORs for the class by iterating over the
4561 FIELD_DECLs, and the back end does not handle overlapping
4562 FIELD_DECLs. */
4564 /* An empty virtual base causes a class to be non-empty
4565 -- but in that case we do not need to clear CLASSTYPE_EMPTY_P
4566 here because that was already done when the virtual table
4567 pointer was created. */
4570 /* Record the offsets of BINFO and its base subobjects. */
4571 record_subobject_offsets (binfo,
4572 BINFO_OFFSET (binfo),
4573 offsets,
4574 /*is_data_member=*/false);
4576 return next_field;
4579 /* Layout all of the non-virtual base classes. Record empty
4580 subobjects in OFFSETS. T is the most derived type. Return nonzero
4581 if the type cannot be nearly empty. The fields created
4582 corresponding to the base classes will be inserted at
4583 *NEXT_FIELD. */
4585 static void
4586 build_base_fields (record_layout_info rli,
4587 splay_tree offsets, tree *next_field)
4589 /* Chain to hold all the new FIELD_DECLs which stand in for base class
4590 subobjects. */
4591 tree t = rli->t;
4592 int n_baseclasses = BINFO_N_BASE_BINFOS (TYPE_BINFO (t));
4593 int i;
4595 /* The primary base class is always allocated first. */
4596 if (CLASSTYPE_HAS_PRIMARY_BASE_P (t))
4597 next_field = build_base_field (rli, CLASSTYPE_PRIMARY_BINFO (t),
4598 offsets, next_field);
4600 /* Now allocate the rest of the bases. */
4601 for (i = 0; i < n_baseclasses; ++i)
4603 tree base_binfo;
4605 base_binfo = BINFO_BASE_BINFO (TYPE_BINFO (t), i);
4607 /* The primary base was already allocated above, so we don't
4608 need to allocate it again here. */
4609 if (base_binfo == CLASSTYPE_PRIMARY_BINFO (t))
4610 continue;
4612 /* Virtual bases are added at the end (a primary virtual base
4613 will have already been added). */
4614 if (BINFO_VIRTUAL_P (base_binfo))
4615 continue;
4617 next_field = build_base_field (rli, base_binfo,
4618 offsets, next_field);
4622 /* Go through the TYPE_METHODS of T issuing any appropriate
4623 diagnostics, figuring out which methods override which other
4624 methods, and so forth. */
4626 static void
4627 check_methods (tree t)
4629 tree x;
4631 for (x = TYPE_METHODS (t); x; x = DECL_CHAIN (x))
4633 check_for_override (x, t);
4634 if (DECL_PURE_VIRTUAL_P (x) && (TREE_CODE (x) != FUNCTION_DECL || ! DECL_VINDEX (x)))
4635 error ("initializer specified for non-virtual method %q+D", x);
4636 /* The name of the field is the original field name
4637 Save this in auxiliary field for later overloading. */
4638 if (TREE_CODE (x) == FUNCTION_DECL && DECL_VINDEX (x))
4640 TYPE_POLYMORPHIC_P (t) = 1;
4641 if (DECL_PURE_VIRTUAL_P (x))
4642 vec_safe_push (CLASSTYPE_PURE_VIRTUALS (t), x);
4644 /* All user-provided destructors are non-trivial.
4645 Constructors and assignment ops are handled in
4646 grok_special_member_properties. */
4647 if (DECL_DESTRUCTOR_P (x) && user_provided_p (x))
4648 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t) = 1;
4649 if (!DECL_VIRTUAL_P (x)
4650 && lookup_attribute ("transaction_safe_dynamic", DECL_ATTRIBUTES (x)))
4651 error_at (DECL_SOURCE_LOCATION (x),
4652 "%<transaction_safe_dynamic%> may only be specified for "
4653 "a virtual function");
4657 /* FN is a constructor or destructor. Clone the declaration to create
4658 a specialized in-charge or not-in-charge version, as indicated by
4659 NAME. */
4661 static tree
4662 build_clone (tree fn, tree name)
4664 tree parms;
4665 tree clone;
4667 /* Copy the function. */
4668 clone = copy_decl (fn);
4669 /* Reset the function name. */
4670 DECL_NAME (clone) = name;
4671 /* Remember where this function came from. */
4672 DECL_ABSTRACT_ORIGIN (clone) = fn;
4673 /* Make it easy to find the CLONE given the FN. */
4674 DECL_CHAIN (clone) = DECL_CHAIN (fn);
4675 DECL_CHAIN (fn) = clone;
4677 /* If this is a template, do the rest on the DECL_TEMPLATE_RESULT. */
4678 if (TREE_CODE (clone) == TEMPLATE_DECL)
4680 tree result = build_clone (DECL_TEMPLATE_RESULT (clone), name);
4681 DECL_TEMPLATE_RESULT (clone) = result;
4682 DECL_TEMPLATE_INFO (result) = copy_node (DECL_TEMPLATE_INFO (result));
4683 DECL_TI_TEMPLATE (result) = clone;
4684 TREE_TYPE (clone) = TREE_TYPE (result);
4685 return clone;
4687 else
4689 // Clone constraints.
4690 if (flag_concepts)
4691 if (tree ci = get_constraints (fn))
4692 set_constraints (clone, copy_node (ci));
4696 SET_DECL_ASSEMBLER_NAME (clone, NULL_TREE);
4697 DECL_CLONED_FUNCTION (clone) = fn;
4698 /* There's no pending inline data for this function. */
4699 DECL_PENDING_INLINE_INFO (clone) = NULL;
4700 DECL_PENDING_INLINE_P (clone) = 0;
4702 /* The base-class destructor is not virtual. */
4703 if (name == base_dtor_identifier)
4705 DECL_VIRTUAL_P (clone) = 0;
4706 if (TREE_CODE (clone) != TEMPLATE_DECL)
4707 DECL_VINDEX (clone) = NULL_TREE;
4710 /* If there was an in-charge parameter, drop it from the function
4711 type. */
4712 if (DECL_HAS_IN_CHARGE_PARM_P (clone))
4714 tree basetype;
4715 tree parmtypes;
4716 tree exceptions;
4718 exceptions = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (clone));
4719 basetype = TYPE_METHOD_BASETYPE (TREE_TYPE (clone));
4720 parmtypes = TYPE_ARG_TYPES (TREE_TYPE (clone));
4721 /* Skip the `this' parameter. */
4722 parmtypes = TREE_CHAIN (parmtypes);
4723 /* Skip the in-charge parameter. */
4724 parmtypes = TREE_CHAIN (parmtypes);
4725 /* And the VTT parm, in a complete [cd]tor. */
4726 if (DECL_HAS_VTT_PARM_P (fn)
4727 && ! DECL_NEEDS_VTT_PARM_P (clone))
4728 parmtypes = TREE_CHAIN (parmtypes);
4729 /* If this is subobject constructor or destructor, add the vtt
4730 parameter. */
4731 TREE_TYPE (clone)
4732 = build_method_type_directly (basetype,
4733 TREE_TYPE (TREE_TYPE (clone)),
4734 parmtypes);
4735 if (exceptions)
4736 TREE_TYPE (clone) = build_exception_variant (TREE_TYPE (clone),
4737 exceptions);
4738 TREE_TYPE (clone)
4739 = cp_build_type_attribute_variant (TREE_TYPE (clone),
4740 TYPE_ATTRIBUTES (TREE_TYPE (fn)));
4743 /* Copy the function parameters. */
4744 DECL_ARGUMENTS (clone) = copy_list (DECL_ARGUMENTS (clone));
4745 /* Remove the in-charge parameter. */
4746 if (DECL_HAS_IN_CHARGE_PARM_P (clone))
4748 DECL_CHAIN (DECL_ARGUMENTS (clone))
4749 = DECL_CHAIN (DECL_CHAIN (DECL_ARGUMENTS (clone)));
4750 DECL_HAS_IN_CHARGE_PARM_P (clone) = 0;
4752 /* And the VTT parm, in a complete [cd]tor. */
4753 if (DECL_HAS_VTT_PARM_P (fn))
4755 if (DECL_NEEDS_VTT_PARM_P (clone))
4756 DECL_HAS_VTT_PARM_P (clone) = 1;
4757 else
4759 DECL_CHAIN (DECL_ARGUMENTS (clone))
4760 = DECL_CHAIN (DECL_CHAIN (DECL_ARGUMENTS (clone)));
4761 DECL_HAS_VTT_PARM_P (clone) = 0;
4765 for (parms = DECL_ARGUMENTS (clone); parms; parms = DECL_CHAIN (parms))
4767 DECL_CONTEXT (parms) = clone;
4768 cxx_dup_lang_specific_decl (parms);
4771 /* Create the RTL for this function. */
4772 SET_DECL_RTL (clone, NULL);
4773 rest_of_decl_compilation (clone, /*top_level=*/1, at_eof);
4775 return clone;
4778 /* Implementation of DECL_CLONED_FUNCTION and DECL_CLONED_FUNCTION_P, do
4779 not invoke this function directly.
4781 For a non-thunk function, returns the address of the slot for storing
4782 the function it is a clone of. Otherwise returns NULL_TREE.
4784 If JUST_TESTING, looks through TEMPLATE_DECL and returns NULL if
4785 cloned_function is unset. This is to support the separate
4786 DECL_CLONED_FUNCTION and DECL_CLONED_FUNCTION_P modes; using the latter
4787 on a template makes sense, but not the former. */
4789 tree *
4790 decl_cloned_function_p (const_tree decl, bool just_testing)
4792 tree *ptr;
4793 if (just_testing)
4794 decl = STRIP_TEMPLATE (decl);
4796 if (TREE_CODE (decl) != FUNCTION_DECL
4797 || !DECL_LANG_SPECIFIC (decl)
4798 || DECL_LANG_SPECIFIC (decl)->u.fn.thunk_p)
4800 #if defined ENABLE_TREE_CHECKING && (GCC_VERSION >= 2007)
4801 if (!just_testing)
4802 lang_check_failed (__FILE__, __LINE__, __FUNCTION__);
4803 else
4804 #endif
4805 return NULL;
4808 ptr = &DECL_LANG_SPECIFIC (decl)->u.fn.u5.cloned_function;
4809 if (just_testing && *ptr == NULL_TREE)
4810 return NULL;
4811 else
4812 return ptr;
4815 /* Produce declarations for all appropriate clones of FN. If
4816 UPDATE_METHOD_VEC_P is nonzero, the clones are added to the
4817 CLASTYPE_METHOD_VEC as well. */
4819 void
4820 clone_function_decl (tree fn, int update_method_vec_p)
4822 tree clone;
4824 /* Avoid inappropriate cloning. */
4825 if (DECL_CHAIN (fn)
4826 && DECL_CLONED_FUNCTION_P (DECL_CHAIN (fn)))
4827 return;
4829 if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (fn))
4831 /* For each constructor, we need two variants: an in-charge version
4832 and a not-in-charge version. */
4833 clone = build_clone (fn, complete_ctor_identifier);
4834 if (update_method_vec_p)
4835 add_method (DECL_CONTEXT (clone), clone, NULL_TREE);
4836 clone = build_clone (fn, base_ctor_identifier);
4837 if (update_method_vec_p)
4838 add_method (DECL_CONTEXT (clone), clone, NULL_TREE);
4840 else
4842 gcc_assert (DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (fn));
4844 /* For each destructor, we need three variants: an in-charge
4845 version, a not-in-charge version, and an in-charge deleting
4846 version. We clone the deleting version first because that
4847 means it will go second on the TYPE_METHODS list -- and that
4848 corresponds to the correct layout order in the virtual
4849 function table.
4851 For a non-virtual destructor, we do not build a deleting
4852 destructor. */
4853 if (DECL_VIRTUAL_P (fn))
4855 clone = build_clone (fn, deleting_dtor_identifier);
4856 if (update_method_vec_p)
4857 add_method (DECL_CONTEXT (clone), clone, NULL_TREE);
4859 clone = build_clone (fn, complete_dtor_identifier);
4860 if (update_method_vec_p)
4861 add_method (DECL_CONTEXT (clone), clone, NULL_TREE);
4862 clone = build_clone (fn, base_dtor_identifier);
4863 if (update_method_vec_p)
4864 add_method (DECL_CONTEXT (clone), clone, NULL_TREE);
4867 /* Note that this is an abstract function that is never emitted. */
4868 DECL_ABSTRACT_P (fn) = true;
4871 /* DECL is an in charge constructor, which is being defined. This will
4872 have had an in class declaration, from whence clones were
4873 declared. An out-of-class definition can specify additional default
4874 arguments. As it is the clones that are involved in overload
4875 resolution, we must propagate the information from the DECL to its
4876 clones. */
4878 void
4879 adjust_clone_args (tree decl)
4881 tree clone;
4883 for (clone = DECL_CHAIN (decl); clone && DECL_CLONED_FUNCTION_P (clone);
4884 clone = DECL_CHAIN (clone))
4886 tree orig_clone_parms = TYPE_ARG_TYPES (TREE_TYPE (clone));
4887 tree orig_decl_parms = TYPE_ARG_TYPES (TREE_TYPE (decl));
4888 tree decl_parms, clone_parms;
4890 clone_parms = orig_clone_parms;
4892 /* Skip the 'this' parameter. */
4893 orig_clone_parms = TREE_CHAIN (orig_clone_parms);
4894 orig_decl_parms = TREE_CHAIN (orig_decl_parms);
4896 if (DECL_HAS_IN_CHARGE_PARM_P (decl))
4897 orig_decl_parms = TREE_CHAIN (orig_decl_parms);
4898 if (DECL_HAS_VTT_PARM_P (decl))
4899 orig_decl_parms = TREE_CHAIN (orig_decl_parms);
4901 clone_parms = orig_clone_parms;
4902 if (DECL_HAS_VTT_PARM_P (clone))
4903 clone_parms = TREE_CHAIN (clone_parms);
4905 for (decl_parms = orig_decl_parms; decl_parms;
4906 decl_parms = TREE_CHAIN (decl_parms),
4907 clone_parms = TREE_CHAIN (clone_parms))
4909 gcc_assert (same_type_p (TREE_TYPE (decl_parms),
4910 TREE_TYPE (clone_parms)));
4912 if (TREE_PURPOSE (decl_parms) && !TREE_PURPOSE (clone_parms))
4914 /* A default parameter has been added. Adjust the
4915 clone's parameters. */
4916 tree exceptions = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (clone));
4917 tree attrs = TYPE_ATTRIBUTES (TREE_TYPE (clone));
4918 tree basetype = TYPE_METHOD_BASETYPE (TREE_TYPE (clone));
4919 tree type;
4921 clone_parms = orig_decl_parms;
4923 if (DECL_HAS_VTT_PARM_P (clone))
4925 clone_parms = tree_cons (TREE_PURPOSE (orig_clone_parms),
4926 TREE_VALUE (orig_clone_parms),
4927 clone_parms);
4928 TREE_TYPE (clone_parms) = TREE_TYPE (orig_clone_parms);
4930 type = build_method_type_directly (basetype,
4931 TREE_TYPE (TREE_TYPE (clone)),
4932 clone_parms);
4933 if (exceptions)
4934 type = build_exception_variant (type, exceptions);
4935 if (attrs)
4936 type = cp_build_type_attribute_variant (type, attrs);
4937 TREE_TYPE (clone) = type;
4939 clone_parms = NULL_TREE;
4940 break;
4943 gcc_assert (!clone_parms);
4947 /* For each of the constructors and destructors in T, create an
4948 in-charge and not-in-charge variant. */
4950 static void
4951 clone_constructors_and_destructors (tree t)
4953 tree fns;
4955 /* If for some reason we don't have a CLASSTYPE_METHOD_VEC, we bail
4956 out now. */
4957 if (!CLASSTYPE_METHOD_VEC (t))
4958 return;
4960 for (fns = CLASSTYPE_CONSTRUCTORS (t); fns; fns = OVL_NEXT (fns))
4961 clone_function_decl (OVL_CURRENT (fns), /*update_method_vec_p=*/1);
4962 for (fns = CLASSTYPE_DESTRUCTORS (t); fns; fns = OVL_NEXT (fns))
4963 clone_function_decl (OVL_CURRENT (fns), /*update_method_vec_p=*/1);
4966 /* Deduce noexcept for a destructor DTOR. */
4968 void
4969 deduce_noexcept_on_destructor (tree dtor)
4971 if (!TYPE_RAISES_EXCEPTIONS (TREE_TYPE (dtor)))
4973 tree eh_spec = unevaluated_noexcept_spec ();
4974 TREE_TYPE (dtor) = build_exception_variant (TREE_TYPE (dtor), eh_spec);
4978 /* For each destructor in T, deduce noexcept:
4980 12.4/3: A declaration of a destructor that does not have an
4981 exception-specification is implicitly considered to have the
4982 same exception-specification as an implicit declaration (15.4). */
4984 static void
4985 deduce_noexcept_on_destructors (tree t)
4987 /* If for some reason we don't have a CLASSTYPE_METHOD_VEC, we bail
4988 out now. */
4989 if (!CLASSTYPE_METHOD_VEC (t))
4990 return;
4992 for (tree fns = CLASSTYPE_DESTRUCTORS (t); fns; fns = OVL_NEXT (fns))
4993 deduce_noexcept_on_destructor (OVL_CURRENT (fns));
4996 /* Subroutine of set_one_vmethod_tm_attributes. Search base classes
4997 of TYPE for virtual functions which FNDECL overrides. Return a
4998 mask of the tm attributes found therein. */
5000 static int
5001 look_for_tm_attr_overrides (tree type, tree fndecl)
5003 tree binfo = TYPE_BINFO (type);
5004 tree base_binfo;
5005 int ix, found = 0;
5007 for (ix = 0; BINFO_BASE_ITERATE (binfo, ix, base_binfo); ++ix)
5009 tree o, basetype = BINFO_TYPE (base_binfo);
5011 if (!TYPE_POLYMORPHIC_P (basetype))
5012 continue;
5014 o = look_for_overrides_here (basetype, fndecl);
5015 if (o)
5017 if (lookup_attribute ("transaction_safe_dynamic",
5018 DECL_ATTRIBUTES (o)))
5019 /* transaction_safe_dynamic is not inherited. */;
5020 else
5021 found |= tm_attr_to_mask (find_tm_attribute
5022 (TYPE_ATTRIBUTES (TREE_TYPE (o))));
5024 else
5025 found |= look_for_tm_attr_overrides (basetype, fndecl);
5028 return found;
5031 /* Subroutine of set_method_tm_attributes. Handle the checks and
5032 inheritance for one virtual method FNDECL. */
5034 static void
5035 set_one_vmethod_tm_attributes (tree type, tree fndecl)
5037 tree tm_attr;
5038 int found, have;
5040 found = look_for_tm_attr_overrides (type, fndecl);
5042 /* If FNDECL doesn't actually override anything (i.e. T is the
5043 class that first declares FNDECL virtual), then we're done. */
5044 if (found == 0)
5045 return;
5047 tm_attr = find_tm_attribute (TYPE_ATTRIBUTES (TREE_TYPE (fndecl)));
5048 have = tm_attr_to_mask (tm_attr);
5050 /* Intel STM Language Extension 3.0, Section 4.2 table 4:
5051 tm_pure must match exactly, otherwise no weakening of
5052 tm_safe > tm_callable > nothing. */
5053 /* ??? The tm_pure attribute didn't make the transition to the
5054 multivendor language spec. */
5055 if (have == TM_ATTR_PURE)
5057 if (found != TM_ATTR_PURE)
5059 found &= -found;
5060 goto err_override;
5063 /* If the overridden function is tm_pure, then FNDECL must be. */
5064 else if (found == TM_ATTR_PURE && tm_attr)
5065 goto err_override;
5066 /* Look for base class combinations that cannot be satisfied. */
5067 else if (found != TM_ATTR_PURE && (found & TM_ATTR_PURE))
5069 found &= ~TM_ATTR_PURE;
5070 found &= -found;
5071 error_at (DECL_SOURCE_LOCATION (fndecl),
5072 "method overrides both %<transaction_pure%> and %qE methods",
5073 tm_mask_to_attr (found));
5075 /* If FNDECL did not declare an attribute, then inherit the most
5076 restrictive one. */
5077 else if (tm_attr == NULL)
5079 apply_tm_attr (fndecl, tm_mask_to_attr (least_bit_hwi (found)));
5081 /* Otherwise validate that we're not weaker than a function
5082 that is being overridden. */
5083 else
5085 found &= -found;
5086 if (found <= TM_ATTR_CALLABLE && have > found)
5087 goto err_override;
5089 return;
5091 err_override:
5092 error_at (DECL_SOURCE_LOCATION (fndecl),
5093 "method declared %qE overriding %qE method",
5094 tm_attr, tm_mask_to_attr (found));
5097 /* For each of the methods in T, propagate a class-level tm attribute. */
5099 static void
5100 set_method_tm_attributes (tree t)
5102 tree class_tm_attr, fndecl;
5104 /* Don't bother collecting tm attributes if transactional memory
5105 support is not enabled. */
5106 if (!flag_tm)
5107 return;
5109 /* Process virtual methods first, as they inherit directly from the
5110 base virtual function and also require validation of new attributes. */
5111 if (TYPE_CONTAINS_VPTR_P (t))
5113 tree vchain;
5114 for (vchain = BINFO_VIRTUALS (TYPE_BINFO (t)); vchain;
5115 vchain = TREE_CHAIN (vchain))
5117 fndecl = BV_FN (vchain);
5118 if (DECL_THUNK_P (fndecl))
5119 fndecl = THUNK_TARGET (fndecl);
5120 set_one_vmethod_tm_attributes (t, fndecl);
5124 /* If the class doesn't have an attribute, nothing more to do. */
5125 class_tm_attr = find_tm_attribute (TYPE_ATTRIBUTES (t));
5126 if (class_tm_attr == NULL)
5127 return;
5129 /* Any method that does not yet have a tm attribute inherits
5130 the one from the class. */
5131 for (fndecl = TYPE_METHODS (t); fndecl; fndecl = TREE_CHAIN (fndecl))
5133 if (!find_tm_attribute (TYPE_ATTRIBUTES (TREE_TYPE (fndecl))))
5134 apply_tm_attr (fndecl, class_tm_attr);
5138 /* Returns true if FN is a default constructor. */
5140 bool
5141 default_ctor_p (tree fn)
5143 return (DECL_CONSTRUCTOR_P (fn)
5144 && sufficient_parms_p (FUNCTION_FIRST_USER_PARMTYPE (fn)));
5147 /* Returns true iff class T has a user-defined constructor that can be called
5148 with more than zero arguments. */
5150 bool
5151 type_has_user_nondefault_constructor (tree t)
5153 tree fns;
5155 if (!TYPE_HAS_USER_CONSTRUCTOR (t))
5156 return false;
5158 for (fns = CLASSTYPE_CONSTRUCTORS (t); fns; fns = OVL_NEXT (fns))
5160 tree fn = OVL_CURRENT (fns);
5161 if (!DECL_ARTIFICIAL (fn)
5162 && (TREE_CODE (fn) == TEMPLATE_DECL
5163 || (skip_artificial_parms_for (fn, DECL_ARGUMENTS (fn))
5164 != NULL_TREE)))
5165 return true;
5168 return false;
5171 /* Returns the defaulted constructor if T has one. Otherwise, returns
5172 NULL_TREE. */
5174 tree
5175 in_class_defaulted_default_constructor (tree t)
5177 if (!TYPE_HAS_USER_CONSTRUCTOR (t))
5178 return NULL_TREE;
5180 for (tree fns = CLASSTYPE_CONSTRUCTORS (t); fns; fns = OVL_NEXT (fns))
5182 tree fn = OVL_CURRENT (fns);
5184 if (DECL_DEFAULTED_IN_CLASS_P (fn)
5185 && default_ctor_p (fn))
5186 return fn;
5189 return NULL_TREE;
5192 /* Returns true iff FN is a user-provided function, i.e. user-declared
5193 and not defaulted at its first declaration. */
5195 bool
5196 user_provided_p (tree fn)
5198 if (TREE_CODE (fn) == TEMPLATE_DECL)
5199 return true;
5200 else
5201 return (!DECL_ARTIFICIAL (fn)
5202 && !(DECL_INITIALIZED_IN_CLASS_P (fn)
5203 && (DECL_DEFAULTED_FN (fn) || DECL_DELETED_FN (fn))));
5206 /* Returns true iff class T has a user-provided constructor. */
5208 bool
5209 type_has_user_provided_constructor (tree t)
5211 tree fns;
5213 if (!CLASS_TYPE_P (t))
5214 return false;
5216 if (!TYPE_HAS_USER_CONSTRUCTOR (t))
5217 return false;
5219 /* This can happen in error cases; avoid crashing. */
5220 if (!CLASSTYPE_METHOD_VEC (t))
5221 return false;
5223 for (fns = CLASSTYPE_CONSTRUCTORS (t); fns; fns = OVL_NEXT (fns))
5224 if (user_provided_p (OVL_CURRENT (fns)))
5225 return true;
5227 return false;
5230 /* Returns true iff class T has a user-provided or explicit constructor. */
5232 bool
5233 type_has_user_provided_or_explicit_constructor (tree t)
5235 tree fns;
5237 if (!CLASS_TYPE_P (t))
5238 return false;
5240 if (!TYPE_HAS_USER_CONSTRUCTOR (t))
5241 return false;
5243 /* This can happen in error cases; avoid crashing. */
5244 if (!CLASSTYPE_METHOD_VEC (t))
5245 return false;
5247 for (fns = CLASSTYPE_CONSTRUCTORS (t); fns; fns = OVL_NEXT (fns))
5249 tree fn = OVL_CURRENT (fns);
5250 if (user_provided_p (fn) || DECL_NONCONVERTING_P (fn))
5251 return true;
5254 return false;
5257 /* Returns true iff class T has a non-user-provided (i.e. implicitly
5258 declared or explicitly defaulted in the class body) default
5259 constructor. */
5261 bool
5262 type_has_non_user_provided_default_constructor (tree t)
5264 tree fns;
5266 if (!TYPE_HAS_DEFAULT_CONSTRUCTOR (t))
5267 return false;
5268 if (CLASSTYPE_LAZY_DEFAULT_CTOR (t))
5269 return true;
5271 for (fns = CLASSTYPE_CONSTRUCTORS (t); fns; fns = OVL_NEXT (fns))
5273 tree fn = OVL_CURRENT (fns);
5274 if (TREE_CODE (fn) == FUNCTION_DECL
5275 && default_ctor_p (fn)
5276 && !user_provided_p (fn))
5277 return true;
5280 return false;
5283 /* TYPE is being used as a virtual base, and has a non-trivial move
5284 assignment. Return true if this is due to there being a user-provided
5285 move assignment in TYPE or one of its subobjects; if there isn't, then
5286 multiple move assignment can't cause any harm. */
5288 bool
5289 vbase_has_user_provided_move_assign (tree type)
5291 /* Does the type itself have a user-provided move assignment operator? */
5292 for (tree fns
5293 = lookup_fnfields_slot_nolazy (type, ansi_assopname (NOP_EXPR));
5294 fns; fns = OVL_NEXT (fns))
5296 tree fn = OVL_CURRENT (fns);
5297 if (move_fn_p (fn) && user_provided_p (fn))
5298 return true;
5301 /* Do any of its bases? */
5302 tree binfo = TYPE_BINFO (type);
5303 tree base_binfo;
5304 for (int i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
5305 if (vbase_has_user_provided_move_assign (BINFO_TYPE (base_binfo)))
5306 return true;
5308 /* Or non-static data members? */
5309 for (tree field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
5311 if (TREE_CODE (field) == FIELD_DECL
5312 && CLASS_TYPE_P (TREE_TYPE (field))
5313 && vbase_has_user_provided_move_assign (TREE_TYPE (field)))
5314 return true;
5317 /* Seems not. */
5318 return false;
5321 /* If default-initialization leaves part of TYPE uninitialized, returns
5322 a DECL for the field or TYPE itself (DR 253). */
5324 tree
5325 default_init_uninitialized_part (tree type)
5327 tree t, r, binfo;
5328 int i;
5330 type = strip_array_types (type);
5331 if (!CLASS_TYPE_P (type))
5332 return type;
5333 if (!type_has_non_user_provided_default_constructor (type))
5334 return NULL_TREE;
5335 for (binfo = TYPE_BINFO (type), i = 0;
5336 BINFO_BASE_ITERATE (binfo, i, t); ++i)
5338 r = default_init_uninitialized_part (BINFO_TYPE (t));
5339 if (r)
5340 return r;
5342 for (t = TYPE_FIELDS (type); t; t = DECL_CHAIN (t))
5343 if (TREE_CODE (t) == FIELD_DECL
5344 && !DECL_ARTIFICIAL (t)
5345 && !DECL_INITIAL (t))
5347 r = default_init_uninitialized_part (TREE_TYPE (t));
5348 if (r)
5349 return DECL_P (r) ? r : t;
5352 return NULL_TREE;
5355 /* Returns true iff for class T, a trivial synthesized default constructor
5356 would be constexpr. */
5358 bool
5359 trivial_default_constructor_is_constexpr (tree t)
5361 /* A defaulted trivial default constructor is constexpr
5362 if there is nothing to initialize. */
5363 gcc_assert (!TYPE_HAS_COMPLEX_DFLT (t));
5364 return is_really_empty_class (t);
5367 /* Returns true iff class T has a constexpr default constructor. */
5369 bool
5370 type_has_constexpr_default_constructor (tree t)
5372 tree fns;
5374 if (!CLASS_TYPE_P (t))
5376 /* The caller should have stripped an enclosing array. */
5377 gcc_assert (TREE_CODE (t) != ARRAY_TYPE);
5378 return false;
5380 if (CLASSTYPE_LAZY_DEFAULT_CTOR (t))
5382 if (!TYPE_HAS_COMPLEX_DFLT (t))
5383 return trivial_default_constructor_is_constexpr (t);
5384 /* Non-trivial, we need to check subobject constructors. */
5385 lazily_declare_fn (sfk_constructor, t);
5387 fns = locate_ctor (t);
5388 return (fns && DECL_DECLARED_CONSTEXPR_P (fns));
5391 /* Returns true iff class T has a constexpr default constructor or has an
5392 implicitly declared default constructor that we can't tell if it's constexpr
5393 without forcing a lazy declaration (which might cause undesired
5394 instantiations). */
5396 bool
5397 type_maybe_constexpr_default_constructor (tree t)
5399 if (CLASS_TYPE_P (t) && CLASSTYPE_LAZY_DEFAULT_CTOR (t)
5400 && TYPE_HAS_COMPLEX_DFLT (t))
5401 /* Assume it's constexpr. */
5402 return true;
5403 return type_has_constexpr_default_constructor (t);
5406 /* Returns true iff class TYPE has a virtual destructor. */
5408 bool
5409 type_has_virtual_destructor (tree type)
5411 tree dtor;
5413 if (!CLASS_TYPE_P (type))
5414 return false;
5416 gcc_assert (COMPLETE_TYPE_P (type));
5417 dtor = CLASSTYPE_DESTRUCTORS (type);
5418 return (dtor && DECL_VIRTUAL_P (dtor));
5421 /* Returns true iff class T has a move constructor. */
5423 bool
5424 type_has_move_constructor (tree t)
5426 tree fns;
5428 if (CLASSTYPE_LAZY_MOVE_CTOR (t))
5430 gcc_assert (COMPLETE_TYPE_P (t));
5431 lazily_declare_fn (sfk_move_constructor, t);
5434 if (!CLASSTYPE_METHOD_VEC (t))
5435 return false;
5437 for (fns = CLASSTYPE_CONSTRUCTORS (t); fns; fns = OVL_NEXT (fns))
5438 if (move_fn_p (OVL_CURRENT (fns)))
5439 return true;
5441 return false;
5444 /* Returns true iff class T has a move assignment operator. */
5446 bool
5447 type_has_move_assign (tree t)
5449 tree fns;
5451 if (CLASSTYPE_LAZY_MOVE_ASSIGN (t))
5453 gcc_assert (COMPLETE_TYPE_P (t));
5454 lazily_declare_fn (sfk_move_assignment, t);
5457 for (fns = lookup_fnfields_slot_nolazy (t, ansi_assopname (NOP_EXPR));
5458 fns; fns = OVL_NEXT (fns))
5459 if (move_fn_p (OVL_CURRENT (fns)))
5460 return true;
5462 return false;
5465 /* Returns true iff class T has a move constructor that was explicitly
5466 declared in the class body. Note that this is different from
5467 "user-provided", which doesn't include functions that are defaulted in
5468 the class. */
5470 bool
5471 type_has_user_declared_move_constructor (tree t)
5473 tree fns;
5475 if (CLASSTYPE_LAZY_MOVE_CTOR (t))
5476 return false;
5478 if (!CLASSTYPE_METHOD_VEC (t))
5479 return false;
5481 for (fns = CLASSTYPE_CONSTRUCTORS (t); fns; fns = OVL_NEXT (fns))
5483 tree fn = OVL_CURRENT (fns);
5484 if (move_fn_p (fn) && !DECL_ARTIFICIAL (fn))
5485 return true;
5488 return false;
5491 /* Returns true iff class T has a move assignment operator that was
5492 explicitly declared in the class body. */
5494 bool
5495 type_has_user_declared_move_assign (tree t)
5497 tree fns;
5499 if (CLASSTYPE_LAZY_MOVE_ASSIGN (t))
5500 return false;
5502 for (fns = lookup_fnfields_slot_nolazy (t, ansi_assopname (NOP_EXPR));
5503 fns; fns = OVL_NEXT (fns))
5505 tree fn = OVL_CURRENT (fns);
5506 if (move_fn_p (fn) && !DECL_ARTIFICIAL (fn))
5507 return true;
5510 return false;
5513 /* Nonzero if we need to build up a constructor call when initializing an
5514 object of this class, either because it has a user-declared constructor
5515 or because it doesn't have a default constructor (so we need to give an
5516 error if no initializer is provided). Use TYPE_NEEDS_CONSTRUCTING when
5517 what you care about is whether or not an object can be produced by a
5518 constructor (e.g. so we don't set TREE_READONLY on const variables of
5519 such type); use this function when what you care about is whether or not
5520 to try to call a constructor to create an object. The latter case is
5521 the former plus some cases of constructors that cannot be called. */
5523 bool
5524 type_build_ctor_call (tree t)
5526 tree inner;
5527 if (TYPE_NEEDS_CONSTRUCTING (t))
5528 return true;
5529 inner = strip_array_types (t);
5530 if (!CLASS_TYPE_P (inner) || ANON_AGGR_TYPE_P (inner))
5531 return false;
5532 if (!TYPE_HAS_DEFAULT_CONSTRUCTOR (inner))
5533 return true;
5534 if (cxx_dialect < cxx11)
5535 return false;
5536 /* A user-declared constructor might be private, and a constructor might
5537 be trivial but deleted. */
5538 for (tree fns = lookup_fnfields_slot (inner, complete_ctor_identifier);
5539 fns; fns = OVL_NEXT (fns))
5541 tree fn = OVL_CURRENT (fns);
5542 if (!DECL_ARTIFICIAL (fn)
5543 || DECL_DELETED_FN (fn))
5544 return true;
5546 return false;
5549 /* Like type_build_ctor_call, but for destructors. */
5551 bool
5552 type_build_dtor_call (tree t)
5554 tree inner;
5555 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t))
5556 return true;
5557 inner = strip_array_types (t);
5558 if (!CLASS_TYPE_P (inner) || ANON_AGGR_TYPE_P (inner)
5559 || !COMPLETE_TYPE_P (inner))
5560 return false;
5561 if (cxx_dialect < cxx11)
5562 return false;
5563 /* A user-declared destructor might be private, and a destructor might
5564 be trivial but deleted. */
5565 for (tree fns = lookup_fnfields_slot (inner, complete_dtor_identifier);
5566 fns; fns = OVL_NEXT (fns))
5568 tree fn = OVL_CURRENT (fns);
5569 if (!DECL_ARTIFICIAL (fn)
5570 || DECL_DELETED_FN (fn))
5571 return true;
5573 return false;
5576 /* Remove all zero-width bit-fields from T. */
5578 static void
5579 remove_zero_width_bit_fields (tree t)
5581 tree *fieldsp;
5583 fieldsp = &TYPE_FIELDS (t);
5584 while (*fieldsp)
5586 if (TREE_CODE (*fieldsp) == FIELD_DECL
5587 && DECL_C_BIT_FIELD (*fieldsp)
5588 /* We should not be confused by the fact that grokbitfield
5589 temporarily sets the width of the bit field into
5590 DECL_INITIAL (*fieldsp).
5591 check_bitfield_decl eventually sets DECL_SIZE (*fieldsp)
5592 to that width. */
5593 && (DECL_SIZE (*fieldsp) == NULL_TREE
5594 || integer_zerop (DECL_SIZE (*fieldsp))))
5595 *fieldsp = DECL_CHAIN (*fieldsp);
5596 else
5597 fieldsp = &DECL_CHAIN (*fieldsp);
5601 /* Returns TRUE iff we need a cookie when dynamically allocating an
5602 array whose elements have the indicated class TYPE. */
5604 static bool
5605 type_requires_array_cookie (tree type)
5607 tree fns;
5608 bool has_two_argument_delete_p = false;
5610 gcc_assert (CLASS_TYPE_P (type));
5612 /* If there's a non-trivial destructor, we need a cookie. In order
5613 to iterate through the array calling the destructor for each
5614 element, we'll have to know how many elements there are. */
5615 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
5616 return true;
5618 /* If the usual deallocation function is a two-argument whose second
5619 argument is of type `size_t', then we have to pass the size of
5620 the array to the deallocation function, so we will need to store
5621 a cookie. */
5622 fns = lookup_fnfields (TYPE_BINFO (type),
5623 ansi_opname (VEC_DELETE_EXPR),
5624 /*protect=*/0);
5625 /* If there are no `operator []' members, or the lookup is
5626 ambiguous, then we don't need a cookie. */
5627 if (!fns || fns == error_mark_node)
5628 return false;
5629 /* Loop through all of the functions. */
5630 for (fns = BASELINK_FUNCTIONS (fns); fns; fns = OVL_NEXT (fns))
5632 tree fn;
5633 tree second_parm;
5635 /* Select the current function. */
5636 fn = OVL_CURRENT (fns);
5637 /* See if this function is a one-argument delete function. If
5638 it is, then it will be the usual deallocation function. */
5639 second_parm = TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (fn)));
5640 if (second_parm == void_list_node)
5641 return false;
5642 /* Do not consider this function if its second argument is an
5643 ellipsis. */
5644 if (!second_parm)
5645 continue;
5646 /* Otherwise, if we have a two-argument function and the second
5647 argument is `size_t', it will be the usual deallocation
5648 function -- unless there is one-argument function, too. */
5649 if (TREE_CHAIN (second_parm) == void_list_node
5650 && same_type_p (TREE_VALUE (second_parm), size_type_node))
5651 has_two_argument_delete_p = true;
5654 return has_two_argument_delete_p;
5657 /* Finish computing the `literal type' property of class type T.
5659 At this point, we have already processed base classes and
5660 non-static data members. We need to check whether the copy
5661 constructor is trivial, the destructor is trivial, and there
5662 is a trivial default constructor or at least one constexpr
5663 constructor other than the copy constructor. */
5665 static void
5666 finalize_literal_type_property (tree t)
5668 tree fn;
5670 if (cxx_dialect < cxx11
5671 || TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t))
5672 CLASSTYPE_LITERAL_P (t) = false;
5673 else if (CLASSTYPE_LITERAL_P (t) && !TYPE_HAS_TRIVIAL_DFLT (t)
5674 && CLASSTYPE_NON_AGGREGATE (t)
5675 && !TYPE_HAS_CONSTEXPR_CTOR (t))
5676 CLASSTYPE_LITERAL_P (t) = false;
5678 if (!CLASSTYPE_LITERAL_P (t))
5679 for (fn = TYPE_METHODS (t); fn; fn = DECL_CHAIN (fn))
5680 if (DECL_DECLARED_CONSTEXPR_P (fn)
5681 && TREE_CODE (fn) != TEMPLATE_DECL
5682 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
5683 && !DECL_CONSTRUCTOR_P (fn))
5685 DECL_DECLARED_CONSTEXPR_P (fn) = false;
5686 if (!DECL_GENERATED_P (fn) && !LAMBDA_TYPE_P (t))
5688 error ("enclosing class of constexpr non-static member "
5689 "function %q+#D is not a literal type", fn);
5690 explain_non_literal_class (t);
5695 /* T is a non-literal type used in a context which requires a constant
5696 expression. Explain why it isn't literal. */
5698 void
5699 explain_non_literal_class (tree t)
5701 static hash_set<tree> *diagnosed;
5703 if (!CLASS_TYPE_P (t))
5704 return;
5705 t = TYPE_MAIN_VARIANT (t);
5707 if (diagnosed == NULL)
5708 diagnosed = new hash_set<tree>;
5709 if (diagnosed->add (t))
5710 /* Already explained. */
5711 return;
5713 inform (0, "%q+T is not literal because:", t);
5714 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t))
5715 inform (0, " %q+T has a non-trivial destructor", t);
5716 else if (CLASSTYPE_NON_AGGREGATE (t)
5717 && !TYPE_HAS_TRIVIAL_DFLT (t)
5718 && !TYPE_HAS_CONSTEXPR_CTOR (t))
5720 inform (0, " %q+T is not an aggregate, does not have a trivial "
5721 "default constructor, and has no constexpr constructor that "
5722 "is not a copy or move constructor", t);
5723 if (type_has_non_user_provided_default_constructor (t))
5725 /* Note that we can't simply call locate_ctor because when the
5726 constructor is deleted it just returns NULL_TREE. */
5727 tree fns;
5728 for (fns = CLASSTYPE_CONSTRUCTORS (t); fns; fns = OVL_NEXT (fns))
5730 tree fn = OVL_CURRENT (fns);
5731 tree parms = TYPE_ARG_TYPES (TREE_TYPE (fn));
5733 parms = skip_artificial_parms_for (fn, parms);
5735 if (sufficient_parms_p (parms))
5737 if (DECL_DELETED_FN (fn))
5738 maybe_explain_implicit_delete (fn);
5739 else
5740 explain_invalid_constexpr_fn (fn);
5741 break;
5746 else
5748 tree binfo, base_binfo, field; int i;
5749 for (binfo = TYPE_BINFO (t), i = 0;
5750 BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
5752 tree basetype = TREE_TYPE (base_binfo);
5753 if (!CLASSTYPE_LITERAL_P (basetype))
5755 inform (0, " base class %qT of %q+T is non-literal",
5756 basetype, t);
5757 explain_non_literal_class (basetype);
5758 return;
5761 for (field = TYPE_FIELDS (t); field; field = TREE_CHAIN (field))
5763 tree ftype;
5764 if (TREE_CODE (field) != FIELD_DECL)
5765 continue;
5766 ftype = TREE_TYPE (field);
5767 if (!literal_type_p (ftype))
5769 inform (DECL_SOURCE_LOCATION (field),
5770 " non-static data member %qD has non-literal type",
5771 field);
5772 if (CLASS_TYPE_P (ftype))
5773 explain_non_literal_class (ftype);
5775 if (CP_TYPE_VOLATILE_P (ftype))
5776 inform (DECL_SOURCE_LOCATION (field),
5777 " non-static data member %qD has volatile type", field);
5782 /* Check the validity of the bases and members declared in T. Add any
5783 implicitly-generated functions (like copy-constructors and
5784 assignment operators). Compute various flag bits (like
5785 CLASSTYPE_NON_LAYOUT_POD_T) for T. This routine works purely at the C++
5786 level: i.e., independently of the ABI in use. */
5788 static void
5789 check_bases_and_members (tree t)
5791 /* Nonzero if the implicitly generated copy constructor should take
5792 a non-const reference argument. */
5793 int cant_have_const_ctor;
5794 /* Nonzero if the implicitly generated assignment operator
5795 should take a non-const reference argument. */
5796 int no_const_asn_ref;
5797 tree access_decls;
5798 bool saved_complex_asn_ref;
5799 bool saved_nontrivial_dtor;
5800 tree fn;
5802 /* By default, we use const reference arguments and generate default
5803 constructors. */
5804 cant_have_const_ctor = 0;
5805 no_const_asn_ref = 0;
5807 /* Check all the base-classes and set FMEM members to point to arrays
5808 of potential interest. */
5809 check_bases (t, &cant_have_const_ctor, &no_const_asn_ref);
5811 /* Deduce noexcept on destructors. This needs to happen after we've set
5812 triviality flags appropriately for our bases. */
5813 if (cxx_dialect >= cxx11)
5814 deduce_noexcept_on_destructors (t);
5816 /* Check all the method declarations. */
5817 check_methods (t);
5819 /* Save the initial values of these flags which only indicate whether
5820 or not the class has user-provided functions. As we analyze the
5821 bases and members we can set these flags for other reasons. */
5822 saved_complex_asn_ref = TYPE_HAS_COMPLEX_COPY_ASSIGN (t);
5823 saved_nontrivial_dtor = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t);
5825 /* Check all the data member declarations. We cannot call
5826 check_field_decls until we have called check_bases check_methods,
5827 as check_field_decls depends on TYPE_HAS_NONTRIVIAL_DESTRUCTOR
5828 being set appropriately. */
5829 check_field_decls (t, &access_decls,
5830 &cant_have_const_ctor,
5831 &no_const_asn_ref);
5833 /* A nearly-empty class has to be vptr-containing; a nearly empty
5834 class contains just a vptr. */
5835 if (!TYPE_CONTAINS_VPTR_P (t))
5836 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
5838 /* Do some bookkeeping that will guide the generation of implicitly
5839 declared member functions. */
5840 TYPE_HAS_COMPLEX_COPY_CTOR (t) |= TYPE_CONTAINS_VPTR_P (t);
5841 TYPE_HAS_COMPLEX_MOVE_CTOR (t) |= TYPE_CONTAINS_VPTR_P (t);
5842 /* We need to call a constructor for this class if it has a
5843 user-provided constructor, or if the default constructor is going
5844 to initialize the vptr. (This is not an if-and-only-if;
5845 TYPE_NEEDS_CONSTRUCTING is set elsewhere if bases or members
5846 themselves need constructing.) */
5847 TYPE_NEEDS_CONSTRUCTING (t)
5848 |= (type_has_user_provided_constructor (t) || TYPE_CONTAINS_VPTR_P (t));
5849 /* [dcl.init.aggr]
5851 An aggregate is an array or a class with no user-provided
5852 constructors ... and no virtual functions.
5854 Again, other conditions for being an aggregate are checked
5855 elsewhere. */
5856 CLASSTYPE_NON_AGGREGATE (t)
5857 |= (type_has_user_provided_or_explicit_constructor (t)
5858 || TYPE_POLYMORPHIC_P (t));
5859 /* This is the C++98/03 definition of POD; it changed in C++0x, but we
5860 retain the old definition internally for ABI reasons. */
5861 CLASSTYPE_NON_LAYOUT_POD_P (t)
5862 |= (CLASSTYPE_NON_AGGREGATE (t)
5863 || saved_nontrivial_dtor || saved_complex_asn_ref);
5864 CLASSTYPE_NON_STD_LAYOUT (t) |= TYPE_CONTAINS_VPTR_P (t);
5865 TYPE_HAS_COMPLEX_COPY_ASSIGN (t) |= TYPE_CONTAINS_VPTR_P (t);
5866 TYPE_HAS_COMPLEX_MOVE_ASSIGN (t) |= TYPE_CONTAINS_VPTR_P (t);
5867 TYPE_HAS_COMPLEX_DFLT (t) |= TYPE_CONTAINS_VPTR_P (t);
5869 /* If the only explicitly declared default constructor is user-provided,
5870 set TYPE_HAS_COMPLEX_DFLT. */
5871 if (!TYPE_HAS_COMPLEX_DFLT (t)
5872 && TYPE_HAS_DEFAULT_CONSTRUCTOR (t)
5873 && !type_has_non_user_provided_default_constructor (t))
5874 TYPE_HAS_COMPLEX_DFLT (t) = true;
5876 /* Warn if a public base of a polymorphic type has an accessible
5877 non-virtual destructor. It is only now that we know the class is
5878 polymorphic. Although a polymorphic base will have a already
5879 been diagnosed during its definition, we warn on use too. */
5880 if (TYPE_POLYMORPHIC_P (t) && warn_nonvdtor)
5882 tree binfo = TYPE_BINFO (t);
5883 vec<tree, va_gc> *accesses = BINFO_BASE_ACCESSES (binfo);
5884 tree base_binfo;
5885 unsigned i;
5887 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
5889 tree basetype = TREE_TYPE (base_binfo);
5891 if ((*accesses)[i] == access_public_node
5892 && (TYPE_POLYMORPHIC_P (basetype) || warn_ecpp)
5893 && accessible_nvdtor_p (basetype))
5894 warning (OPT_Wnon_virtual_dtor,
5895 "base class %q#T has accessible non-virtual destructor",
5896 basetype);
5900 /* If the class has no user-declared constructor, but does have
5901 non-static const or reference data members that can never be
5902 initialized, issue a warning. */
5903 if (warn_uninitialized
5904 /* Classes with user-declared constructors are presumed to
5905 initialize these members. */
5906 && !TYPE_HAS_USER_CONSTRUCTOR (t)
5907 /* Aggregates can be initialized with brace-enclosed
5908 initializers. */
5909 && CLASSTYPE_NON_AGGREGATE (t))
5911 tree field;
5913 for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
5915 tree type;
5917 if (TREE_CODE (field) != FIELD_DECL
5918 || DECL_INITIAL (field) != NULL_TREE)
5919 continue;
5921 type = TREE_TYPE (field);
5922 if (TREE_CODE (type) == REFERENCE_TYPE)
5923 warning_at (DECL_SOURCE_LOCATION (field),
5924 OPT_Wuninitialized, "non-static reference %q#D "
5925 "in class without a constructor", field);
5926 else if (CP_TYPE_CONST_P (type)
5927 && (!CLASS_TYPE_P (type)
5928 || !TYPE_HAS_DEFAULT_CONSTRUCTOR (type)))
5929 warning_at (DECL_SOURCE_LOCATION (field),
5930 OPT_Wuninitialized, "non-static const member %q#D "
5931 "in class without a constructor", field);
5935 /* Synthesize any needed methods. */
5936 add_implicitly_declared_members (t, &access_decls,
5937 cant_have_const_ctor,
5938 no_const_asn_ref);
5940 /* Check defaulted declarations here so we have cant_have_const_ctor
5941 and don't need to worry about clones. */
5942 for (fn = TYPE_METHODS (t); fn; fn = DECL_CHAIN (fn))
5943 if (!DECL_ARTIFICIAL (fn) && DECL_DEFAULTED_IN_CLASS_P (fn))
5945 int copy = copy_fn_p (fn);
5946 if (copy > 0)
5948 bool imp_const_p
5949 = (DECL_CONSTRUCTOR_P (fn) ? !cant_have_const_ctor
5950 : !no_const_asn_ref);
5951 bool fn_const_p = (copy == 2);
5953 if (fn_const_p && !imp_const_p)
5954 /* If the function is defaulted outside the class, we just
5955 give the synthesis error. */
5956 error ("%q+D declared to take const reference, but implicit "
5957 "declaration would take non-const", fn);
5959 defaulted_late_check (fn);
5962 if (LAMBDA_TYPE_P (t))
5964 /* "This class type is not an aggregate." */
5965 CLASSTYPE_NON_AGGREGATE (t) = 1;
5968 /* Compute the 'literal type' property before we
5969 do anything with non-static member functions. */
5970 finalize_literal_type_property (t);
5972 /* Create the in-charge and not-in-charge variants of constructors
5973 and destructors. */
5974 clone_constructors_and_destructors (t);
5976 /* Process the using-declarations. */
5977 for (; access_decls; access_decls = TREE_CHAIN (access_decls))
5978 handle_using_decl (TREE_VALUE (access_decls), t);
5980 /* Build and sort the CLASSTYPE_METHOD_VEC. */
5981 finish_struct_methods (t);
5983 /* Figure out whether or not we will need a cookie when dynamically
5984 allocating an array of this type. */
5985 TYPE_LANG_SPECIFIC (t)->u.c.vec_new_uses_cookie
5986 = type_requires_array_cookie (t);
5989 /* If T needs a pointer to its virtual function table, set TYPE_VFIELD
5990 accordingly. If a new vfield was created (because T doesn't have a
5991 primary base class), then the newly created field is returned. It
5992 is not added to the TYPE_FIELDS list; it is the caller's
5993 responsibility to do that. Accumulate declared virtual functions
5994 on VIRTUALS_P. */
5996 static tree
5997 create_vtable_ptr (tree t, tree* virtuals_p)
5999 tree fn;
6001 /* Collect the virtual functions declared in T. */
6002 for (fn = TYPE_METHODS (t); fn; fn = DECL_CHAIN (fn))
6003 if (TREE_CODE (fn) == FUNCTION_DECL
6004 && DECL_VINDEX (fn) && !DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (fn)
6005 && TREE_CODE (DECL_VINDEX (fn)) != INTEGER_CST)
6007 tree new_virtual = make_node (TREE_LIST);
6009 BV_FN (new_virtual) = fn;
6010 BV_DELTA (new_virtual) = integer_zero_node;
6011 BV_VCALL_INDEX (new_virtual) = NULL_TREE;
6013 TREE_CHAIN (new_virtual) = *virtuals_p;
6014 *virtuals_p = new_virtual;
6017 /* If we couldn't find an appropriate base class, create a new field
6018 here. Even if there weren't any new virtual functions, we might need a
6019 new virtual function table if we're supposed to include vptrs in
6020 all classes that need them. */
6021 if (!TYPE_VFIELD (t) && (*virtuals_p || TYPE_CONTAINS_VPTR_P (t)))
6023 /* We build this decl with vtbl_ptr_type_node, which is a
6024 `vtable_entry_type*'. It might seem more precise to use
6025 `vtable_entry_type (*)[N]' where N is the number of virtual
6026 functions. However, that would require the vtable pointer in
6027 base classes to have a different type than the vtable pointer
6028 in derived classes. We could make that happen, but that
6029 still wouldn't solve all the problems. In particular, the
6030 type-based alias analysis code would decide that assignments
6031 to the base class vtable pointer can't alias assignments to
6032 the derived class vtable pointer, since they have different
6033 types. Thus, in a derived class destructor, where the base
6034 class constructor was inlined, we could generate bad code for
6035 setting up the vtable pointer.
6037 Therefore, we use one type for all vtable pointers. We still
6038 use a type-correct type; it's just doesn't indicate the array
6039 bounds. That's better than using `void*' or some such; it's
6040 cleaner, and it let's the alias analysis code know that these
6041 stores cannot alias stores to void*! */
6042 tree field;
6044 field = build_decl (input_location,
6045 FIELD_DECL, get_vfield_name (t), vtbl_ptr_type_node);
6046 DECL_VIRTUAL_P (field) = 1;
6047 DECL_ARTIFICIAL (field) = 1;
6048 DECL_FIELD_CONTEXT (field) = t;
6049 DECL_FCONTEXT (field) = t;
6050 if (TYPE_PACKED (t))
6051 DECL_PACKED (field) = 1;
6053 TYPE_VFIELD (t) = field;
6055 /* This class is non-empty. */
6056 CLASSTYPE_EMPTY_P (t) = 0;
6058 return field;
6061 return NULL_TREE;
6064 /* Add OFFSET to all base types of BINFO which is a base in the
6065 hierarchy dominated by T.
6067 OFFSET, which is a type offset, is number of bytes. */
6069 static void
6070 propagate_binfo_offsets (tree binfo, tree offset)
6072 int i;
6073 tree primary_binfo;
6074 tree base_binfo;
6076 /* Update BINFO's offset. */
6077 BINFO_OFFSET (binfo)
6078 = fold_convert (sizetype,
6079 size_binop (PLUS_EXPR,
6080 fold_convert (ssizetype, BINFO_OFFSET (binfo)),
6081 offset));
6083 /* Find the primary base class. */
6084 primary_binfo = get_primary_binfo (binfo);
6086 if (primary_binfo && BINFO_INHERITANCE_CHAIN (primary_binfo) == binfo)
6087 propagate_binfo_offsets (primary_binfo, offset);
6089 /* Scan all of the bases, pushing the BINFO_OFFSET adjust
6090 downwards. */
6091 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
6093 /* Don't do the primary base twice. */
6094 if (base_binfo == primary_binfo)
6095 continue;
6097 if (BINFO_VIRTUAL_P (base_binfo))
6098 continue;
6100 propagate_binfo_offsets (base_binfo, offset);
6104 /* Set BINFO_OFFSET for all of the virtual bases for RLI->T. Update
6105 TYPE_ALIGN and TYPE_SIZE for T. OFFSETS gives the location of
6106 empty subobjects of T. */
6108 static void
6109 layout_virtual_bases (record_layout_info rli, splay_tree offsets)
6111 tree vbase;
6112 tree t = rli->t;
6113 tree *next_field;
6115 if (BINFO_N_BASE_BINFOS (TYPE_BINFO (t)) == 0)
6116 return;
6118 /* Find the last field. The artificial fields created for virtual
6119 bases will go after the last extant field to date. */
6120 next_field = &TYPE_FIELDS (t);
6121 while (*next_field)
6122 next_field = &DECL_CHAIN (*next_field);
6124 /* Go through the virtual bases, allocating space for each virtual
6125 base that is not already a primary base class. These are
6126 allocated in inheritance graph order. */
6127 for (vbase = TYPE_BINFO (t); vbase; vbase = TREE_CHAIN (vbase))
6129 if (!BINFO_VIRTUAL_P (vbase))
6130 continue;
6132 if (!BINFO_PRIMARY_P (vbase))
6134 /* This virtual base is not a primary base of any class in the
6135 hierarchy, so we have to add space for it. */
6136 next_field = build_base_field (rli, vbase,
6137 offsets, next_field);
6142 /* Returns the offset of the byte just past the end of the base class
6143 BINFO. */
6145 static tree
6146 end_of_base (tree binfo)
6148 tree size;
6150 if (!CLASSTYPE_AS_BASE (BINFO_TYPE (binfo)))
6151 size = TYPE_SIZE_UNIT (char_type_node);
6152 else if (is_empty_class (BINFO_TYPE (binfo)))
6153 /* An empty class has zero CLASSTYPE_SIZE_UNIT, but we need to
6154 allocate some space for it. It cannot have virtual bases, so
6155 TYPE_SIZE_UNIT is fine. */
6156 size = TYPE_SIZE_UNIT (BINFO_TYPE (binfo));
6157 else
6158 size = CLASSTYPE_SIZE_UNIT (BINFO_TYPE (binfo));
6160 return size_binop (PLUS_EXPR, BINFO_OFFSET (binfo), size);
6163 /* Returns the offset of the byte just past the end of the base class
6164 with the highest offset in T. If INCLUDE_VIRTUALS_P is zero, then
6165 only non-virtual bases are included. */
6167 static tree
6168 end_of_class (tree t, int include_virtuals_p)
6170 tree result = size_zero_node;
6171 vec<tree, va_gc> *vbases;
6172 tree binfo;
6173 tree base_binfo;
6174 tree offset;
6175 int i;
6177 for (binfo = TYPE_BINFO (t), i = 0;
6178 BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
6180 if (!include_virtuals_p
6181 && BINFO_VIRTUAL_P (base_binfo)
6182 && (!BINFO_PRIMARY_P (base_binfo)
6183 || BINFO_INHERITANCE_CHAIN (base_binfo) != TYPE_BINFO (t)))
6184 continue;
6186 offset = end_of_base (base_binfo);
6187 if (tree_int_cst_lt (result, offset))
6188 result = offset;
6191 if (include_virtuals_p)
6192 for (vbases = CLASSTYPE_VBASECLASSES (t), i = 0;
6193 vec_safe_iterate (vbases, i, &base_binfo); i++)
6195 offset = end_of_base (base_binfo);
6196 if (tree_int_cst_lt (result, offset))
6197 result = offset;
6200 return result;
6203 /* Warn about bases of T that are inaccessible because they are
6204 ambiguous. For example:
6206 struct S {};
6207 struct T : public S {};
6208 struct U : public S, public T {};
6210 Here, `(S*) new U' is not allowed because there are two `S'
6211 subobjects of U. */
6213 static void
6214 warn_about_ambiguous_bases (tree t)
6216 int i;
6217 vec<tree, va_gc> *vbases;
6218 tree basetype;
6219 tree binfo;
6220 tree base_binfo;
6222 /* If there are no repeated bases, nothing can be ambiguous. */
6223 if (!CLASSTYPE_REPEATED_BASE_P (t))
6224 return;
6226 /* Check direct bases. */
6227 for (binfo = TYPE_BINFO (t), i = 0;
6228 BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
6230 basetype = BINFO_TYPE (base_binfo);
6232 if (!uniquely_derived_from_p (basetype, t))
6233 warning (0, "direct base %qT inaccessible in %qT due to ambiguity",
6234 basetype, t);
6237 /* Check for ambiguous virtual bases. */
6238 if (extra_warnings)
6239 for (vbases = CLASSTYPE_VBASECLASSES (t), i = 0;
6240 vec_safe_iterate (vbases, i, &binfo); i++)
6242 basetype = BINFO_TYPE (binfo);
6244 if (!uniquely_derived_from_p (basetype, t))
6245 warning (OPT_Wextra, "virtual base %qT inaccessible in %qT due "
6246 "to ambiguity", basetype, t);
6250 /* Compare two INTEGER_CSTs K1 and K2. */
6252 static int
6253 splay_tree_compare_integer_csts (splay_tree_key k1, splay_tree_key k2)
6255 return tree_int_cst_compare ((tree) k1, (tree) k2);
6258 /* Increase the size indicated in RLI to account for empty classes
6259 that are "off the end" of the class. */
6261 static void
6262 include_empty_classes (record_layout_info rli)
6264 tree eoc;
6265 tree rli_size;
6267 /* It might be the case that we grew the class to allocate a
6268 zero-sized base class. That won't be reflected in RLI, yet,
6269 because we are willing to overlay multiple bases at the same
6270 offset. However, now we need to make sure that RLI is big enough
6271 to reflect the entire class. */
6272 eoc = end_of_class (rli->t,
6273 CLASSTYPE_AS_BASE (rli->t) != NULL_TREE);
6274 rli_size = rli_size_unit_so_far (rli);
6275 if (TREE_CODE (rli_size) == INTEGER_CST
6276 && tree_int_cst_lt (rli_size, eoc))
6278 /* The size should have been rounded to a whole byte. */
6279 gcc_assert (tree_int_cst_equal
6280 (rli->bitpos, round_down (rli->bitpos, BITS_PER_UNIT)));
6281 rli->bitpos
6282 = size_binop (PLUS_EXPR,
6283 rli->bitpos,
6284 size_binop (MULT_EXPR,
6285 fold_convert (bitsizetype,
6286 size_binop (MINUS_EXPR,
6287 eoc, rli_size)),
6288 bitsize_int (BITS_PER_UNIT)));
6289 normalize_rli (rli);
6293 /* Calculate the TYPE_SIZE, TYPE_ALIGN, etc for T. Calculate
6294 BINFO_OFFSETs for all of the base-classes. Position the vtable
6295 pointer. Accumulate declared virtual functions on VIRTUALS_P. */
6297 static void
6298 layout_class_type (tree t, tree *virtuals_p)
6300 tree non_static_data_members;
6301 tree field;
6302 tree vptr;
6303 record_layout_info rli;
6304 /* Maps offsets (represented as INTEGER_CSTs) to a TREE_LIST of
6305 types that appear at that offset. */
6306 splay_tree empty_base_offsets;
6307 /* True if the last field laid out was a bit-field. */
6308 bool last_field_was_bitfield = false;
6309 /* The location at which the next field should be inserted. */
6310 tree *next_field;
6311 /* T, as a base class. */
6312 tree base_t;
6314 /* Keep track of the first non-static data member. */
6315 non_static_data_members = TYPE_FIELDS (t);
6317 /* Start laying out the record. */
6318 rli = start_record_layout (t);
6320 /* Mark all the primary bases in the hierarchy. */
6321 determine_primary_bases (t);
6323 /* Create a pointer to our virtual function table. */
6324 vptr = create_vtable_ptr (t, virtuals_p);
6326 /* The vptr is always the first thing in the class. */
6327 if (vptr)
6329 DECL_CHAIN (vptr) = TYPE_FIELDS (t);
6330 TYPE_FIELDS (t) = vptr;
6331 next_field = &DECL_CHAIN (vptr);
6332 place_field (rli, vptr);
6334 else
6335 next_field = &TYPE_FIELDS (t);
6337 /* Build FIELD_DECLs for all of the non-virtual base-types. */
6338 empty_base_offsets = splay_tree_new (splay_tree_compare_integer_csts,
6339 NULL, NULL);
6340 build_base_fields (rli, empty_base_offsets, next_field);
6342 /* Layout the non-static data members. */
6343 for (field = non_static_data_members; field; field = DECL_CHAIN (field))
6345 tree type;
6346 tree padding;
6348 /* We still pass things that aren't non-static data members to
6349 the back end, in case it wants to do something with them. */
6350 if (TREE_CODE (field) != FIELD_DECL)
6352 place_field (rli, field);
6353 /* If the static data member has incomplete type, keep track
6354 of it so that it can be completed later. (The handling
6355 of pending statics in finish_record_layout is
6356 insufficient; consider:
6358 struct S1;
6359 struct S2 { static S1 s1; };
6361 At this point, finish_record_layout will be called, but
6362 S1 is still incomplete.) */
6363 if (VAR_P (field))
6365 maybe_register_incomplete_var (field);
6366 /* The visibility of static data members is determined
6367 at their point of declaration, not their point of
6368 definition. */
6369 determine_visibility (field);
6371 continue;
6374 type = TREE_TYPE (field);
6375 if (type == error_mark_node)
6376 continue;
6378 padding = NULL_TREE;
6380 /* If this field is a bit-field whose width is greater than its
6381 type, then there are some special rules for allocating
6382 it. */
6383 if (DECL_C_BIT_FIELD (field)
6384 && tree_int_cst_lt (TYPE_SIZE (type), DECL_SIZE (field)))
6386 unsigned int itk;
6387 tree integer_type;
6388 bool was_unnamed_p = false;
6389 /* We must allocate the bits as if suitably aligned for the
6390 longest integer type that fits in this many bits. type
6391 of the field. Then, we are supposed to use the left over
6392 bits as additional padding. */
6393 for (itk = itk_char; itk != itk_none; ++itk)
6394 if (integer_types[itk] != NULL_TREE
6395 && (tree_int_cst_lt (size_int (MAX_FIXED_MODE_SIZE),
6396 TYPE_SIZE (integer_types[itk]))
6397 || tree_int_cst_lt (DECL_SIZE (field),
6398 TYPE_SIZE (integer_types[itk]))))
6399 break;
6401 /* ITK now indicates a type that is too large for the
6402 field. We have to back up by one to find the largest
6403 type that fits. */
6406 --itk;
6407 integer_type = integer_types[itk];
6408 } while (itk > 0 && integer_type == NULL_TREE);
6410 /* Figure out how much additional padding is required. */
6411 if (tree_int_cst_lt (TYPE_SIZE (integer_type), DECL_SIZE (field)))
6413 if (TREE_CODE (t) == UNION_TYPE)
6414 /* In a union, the padding field must have the full width
6415 of the bit-field; all fields start at offset zero. */
6416 padding = DECL_SIZE (field);
6417 else
6418 padding = size_binop (MINUS_EXPR, DECL_SIZE (field),
6419 TYPE_SIZE (integer_type));
6422 /* An unnamed bitfield does not normally affect the
6423 alignment of the containing class on a target where
6424 PCC_BITFIELD_TYPE_MATTERS. But, the C++ ABI does not
6425 make any exceptions for unnamed bitfields when the
6426 bitfields are longer than their types. Therefore, we
6427 temporarily give the field a name. */
6428 if (PCC_BITFIELD_TYPE_MATTERS && !DECL_NAME (field))
6430 was_unnamed_p = true;
6431 DECL_NAME (field) = make_anon_name ();
6434 DECL_SIZE (field) = TYPE_SIZE (integer_type);
6435 SET_DECL_ALIGN (field, TYPE_ALIGN (integer_type));
6436 DECL_USER_ALIGN (field) = TYPE_USER_ALIGN (integer_type);
6437 layout_nonempty_base_or_field (rli, field, NULL_TREE,
6438 empty_base_offsets);
6439 if (was_unnamed_p)
6440 DECL_NAME (field) = NULL_TREE;
6441 /* Now that layout has been performed, set the size of the
6442 field to the size of its declared type; the rest of the
6443 field is effectively invisible. */
6444 DECL_SIZE (field) = TYPE_SIZE (type);
6445 /* We must also reset the DECL_MODE of the field. */
6446 DECL_MODE (field) = TYPE_MODE (type);
6448 else
6449 layout_nonempty_base_or_field (rli, field, NULL_TREE,
6450 empty_base_offsets);
6452 /* Remember the location of any empty classes in FIELD. */
6453 record_subobject_offsets (TREE_TYPE (field),
6454 byte_position(field),
6455 empty_base_offsets,
6456 /*is_data_member=*/true);
6458 /* If a bit-field does not immediately follow another bit-field,
6459 and yet it starts in the middle of a byte, we have failed to
6460 comply with the ABI. */
6461 if (warn_abi
6462 && DECL_C_BIT_FIELD (field)
6463 /* The TREE_NO_WARNING flag gets set by Objective-C when
6464 laying out an Objective-C class. The ObjC ABI differs
6465 from the C++ ABI, and so we do not want a warning
6466 here. */
6467 && !TREE_NO_WARNING (field)
6468 && !last_field_was_bitfield
6469 && !integer_zerop (size_binop (TRUNC_MOD_EXPR,
6470 DECL_FIELD_BIT_OFFSET (field),
6471 bitsize_unit_node)))
6472 warning_at (DECL_SOURCE_LOCATION (field), OPT_Wabi,
6473 "offset of %qD is not ABI-compliant and may "
6474 "change in a future version of GCC", field);
6476 /* The middle end uses the type of expressions to determine the
6477 possible range of expression values. In order to optimize
6478 "x.i > 7" to "false" for a 2-bit bitfield "i", the middle end
6479 must be made aware of the width of "i", via its type.
6481 Because C++ does not have integer types of arbitrary width,
6482 we must (for the purposes of the front end) convert from the
6483 type assigned here to the declared type of the bitfield
6484 whenever a bitfield expression is used as an rvalue.
6485 Similarly, when assigning a value to a bitfield, the value
6486 must be converted to the type given the bitfield here. */
6487 if (DECL_C_BIT_FIELD (field))
6489 unsigned HOST_WIDE_INT width;
6490 tree ftype = TREE_TYPE (field);
6491 width = tree_to_uhwi (DECL_SIZE (field));
6492 if (width != TYPE_PRECISION (ftype))
6494 TREE_TYPE (field)
6495 = c_build_bitfield_integer_type (width,
6496 TYPE_UNSIGNED (ftype));
6497 TREE_TYPE (field)
6498 = cp_build_qualified_type (TREE_TYPE (field),
6499 cp_type_quals (ftype));
6503 /* If we needed additional padding after this field, add it
6504 now. */
6505 if (padding)
6507 tree padding_field;
6509 padding_field = build_decl (input_location,
6510 FIELD_DECL,
6511 NULL_TREE,
6512 char_type_node);
6513 DECL_BIT_FIELD (padding_field) = 1;
6514 DECL_SIZE (padding_field) = padding;
6515 DECL_CONTEXT (padding_field) = t;
6516 DECL_ARTIFICIAL (padding_field) = 1;
6517 DECL_IGNORED_P (padding_field) = 1;
6518 layout_nonempty_base_or_field (rli, padding_field,
6519 NULL_TREE,
6520 empty_base_offsets);
6523 last_field_was_bitfield = DECL_C_BIT_FIELD (field);
6526 if (!integer_zerop (rli->bitpos))
6528 /* Make sure that we are on a byte boundary so that the size of
6529 the class without virtual bases will always be a round number
6530 of bytes. */
6531 rli->bitpos = round_up_loc (input_location, rli->bitpos, BITS_PER_UNIT);
6532 normalize_rli (rli);
6535 /* Delete all zero-width bit-fields from the list of fields. Now
6536 that the type is laid out they are no longer important. */
6537 remove_zero_width_bit_fields (t);
6539 /* Create the version of T used for virtual bases. We do not use
6540 make_class_type for this version; this is an artificial type. For
6541 a POD type, we just reuse T. */
6542 if (CLASSTYPE_NON_LAYOUT_POD_P (t) || CLASSTYPE_EMPTY_P (t))
6544 base_t = make_node (TREE_CODE (t));
6546 /* Set the size and alignment for the new type. */
6547 tree eoc;
6549 /* If the ABI version is not at least two, and the last
6550 field was a bit-field, RLI may not be on a byte
6551 boundary. In particular, rli_size_unit_so_far might
6552 indicate the last complete byte, while rli_size_so_far
6553 indicates the total number of bits used. Therefore,
6554 rli_size_so_far, rather than rli_size_unit_so_far, is
6555 used to compute TYPE_SIZE_UNIT. */
6556 eoc = end_of_class (t, /*include_virtuals_p=*/0);
6557 TYPE_SIZE_UNIT (base_t)
6558 = size_binop (MAX_EXPR,
6559 fold_convert (sizetype,
6560 size_binop (CEIL_DIV_EXPR,
6561 rli_size_so_far (rli),
6562 bitsize_int (BITS_PER_UNIT))),
6563 eoc);
6564 TYPE_SIZE (base_t)
6565 = size_binop (MAX_EXPR,
6566 rli_size_so_far (rli),
6567 size_binop (MULT_EXPR,
6568 fold_convert (bitsizetype, eoc),
6569 bitsize_int (BITS_PER_UNIT)));
6570 SET_TYPE_ALIGN (base_t, rli->record_align);
6571 TYPE_USER_ALIGN (base_t) = TYPE_USER_ALIGN (t);
6573 /* Copy the fields from T. */
6574 next_field = &TYPE_FIELDS (base_t);
6575 for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
6576 if (TREE_CODE (field) == FIELD_DECL)
6578 *next_field = copy_node (field);
6579 DECL_CONTEXT (*next_field) = base_t;
6580 next_field = &DECL_CHAIN (*next_field);
6582 *next_field = NULL_TREE;
6584 /* Record the base version of the type. */
6585 CLASSTYPE_AS_BASE (t) = base_t;
6586 TYPE_CONTEXT (base_t) = t;
6588 else
6589 CLASSTYPE_AS_BASE (t) = t;
6591 /* Every empty class contains an empty class. */
6592 if (CLASSTYPE_EMPTY_P (t))
6593 CLASSTYPE_CONTAINS_EMPTY_CLASS_P (t) = 1;
6595 /* Set the TYPE_DECL for this type to contain the right
6596 value for DECL_OFFSET, so that we can use it as part
6597 of a COMPONENT_REF for multiple inheritance. */
6598 layout_decl (TYPE_MAIN_DECL (t), 0);
6600 /* Now fix up any virtual base class types that we left lying
6601 around. We must get these done before we try to lay out the
6602 virtual function table. As a side-effect, this will remove the
6603 base subobject fields. */
6604 layout_virtual_bases (rli, empty_base_offsets);
6606 /* Make sure that empty classes are reflected in RLI at this
6607 point. */
6608 include_empty_classes(rli);
6610 /* Make sure not to create any structures with zero size. */
6611 if (integer_zerop (rli_size_unit_so_far (rli)) && CLASSTYPE_EMPTY_P (t))
6612 place_field (rli,
6613 build_decl (input_location,
6614 FIELD_DECL, NULL_TREE, char_type_node));
6616 /* If this is a non-POD, declaring it packed makes a difference to how it
6617 can be used as a field; don't let finalize_record_size undo it. */
6618 if (TYPE_PACKED (t) && !layout_pod_type_p (t))
6619 rli->packed_maybe_necessary = true;
6621 /* Let the back end lay out the type. */
6622 finish_record_layout (rli, /*free_p=*/true);
6624 if (TYPE_SIZE_UNIT (t)
6625 && TREE_CODE (TYPE_SIZE_UNIT (t)) == INTEGER_CST
6626 && !TREE_OVERFLOW (TYPE_SIZE_UNIT (t))
6627 && !valid_constant_size_p (TYPE_SIZE_UNIT (t)))
6628 error ("size of type %qT is too large (%qE bytes)", t, TYPE_SIZE_UNIT (t));
6630 /* Warn about bases that can't be talked about due to ambiguity. */
6631 warn_about_ambiguous_bases (t);
6633 /* Now that we're done with layout, give the base fields the real types. */
6634 for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
6635 if (DECL_ARTIFICIAL (field) && IS_FAKE_BASE_TYPE (TREE_TYPE (field)))
6636 TREE_TYPE (field) = TYPE_CONTEXT (TREE_TYPE (field));
6638 /* Clean up. */
6639 splay_tree_delete (empty_base_offsets);
6641 if (CLASSTYPE_EMPTY_P (t)
6642 && tree_int_cst_lt (sizeof_biggest_empty_class,
6643 TYPE_SIZE_UNIT (t)))
6644 sizeof_biggest_empty_class = TYPE_SIZE_UNIT (t);
6647 /* Determine the "key method" for the class type indicated by TYPE,
6648 and set CLASSTYPE_KEY_METHOD accordingly. */
6650 void
6651 determine_key_method (tree type)
6653 tree method;
6655 if (TYPE_FOR_JAVA (type)
6656 || processing_template_decl
6657 || CLASSTYPE_TEMPLATE_INSTANTIATION (type)
6658 || CLASSTYPE_INTERFACE_KNOWN (type))
6659 return;
6661 /* The key method is the first non-pure virtual function that is not
6662 inline at the point of class definition. On some targets the
6663 key function may not be inline; those targets should not call
6664 this function until the end of the translation unit. */
6665 for (method = TYPE_METHODS (type); method != NULL_TREE;
6666 method = DECL_CHAIN (method))
6667 if (TREE_CODE (method) == FUNCTION_DECL
6668 && DECL_VINDEX (method) != NULL_TREE
6669 && ! DECL_DECLARED_INLINE_P (method)
6670 && ! DECL_PURE_VIRTUAL_P (method))
6672 CLASSTYPE_KEY_METHOD (type) = method;
6673 break;
6676 return;
6680 /* Allocate and return an instance of struct sorted_fields_type with
6681 N fields. */
6683 static struct sorted_fields_type *
6684 sorted_fields_type_new (int n)
6686 struct sorted_fields_type *sft;
6687 sft = (sorted_fields_type *) ggc_internal_alloc (sizeof (sorted_fields_type)
6688 + n * sizeof (tree));
6689 sft->len = n;
6691 return sft;
6694 /* Helper of find_flexarrays. Return true when FLD refers to a non-static
6695 class data member of non-zero size, otherwise false. */
6697 static inline bool
6698 field_nonempty_p (const_tree fld)
6700 if (TREE_CODE (fld) == ERROR_MARK)
6701 return false;
6703 tree type = TREE_TYPE (fld);
6704 if (TREE_CODE (fld) == FIELD_DECL
6705 && TREE_CODE (type) != ERROR_MARK
6706 && (DECL_NAME (fld) || RECORD_OR_UNION_TYPE_P (type)))
6708 return TYPE_SIZE (type)
6709 && (TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST
6710 || !tree_int_cst_equal (size_zero_node, TYPE_SIZE (type)));
6713 return false;
6716 /* Used by find_flexarrays and related. */
6717 struct flexmems_t {
6718 /* The first flexible array member or non-zero array member found
6719 in order of layout. */
6720 tree array;
6721 /* First non-static non-empty data member in the class or its bases. */
6722 tree first;
6723 /* First non-static non-empty data member following either the flexible
6724 array member, if found, or the zero-length array member. */
6725 tree after;
6728 /* Find either the first flexible array member or the first zero-length
6729 array, in that order or preference, among members of class T (but not
6730 its base classes), and set members of FMEM accordingly. */
6732 static void
6733 find_flexarrays (tree t, flexmems_t *fmem)
6735 for (tree fld = TYPE_FIELDS (t), next; fld; fld = next)
6737 /* Find the next non-static data member if it exists. */
6738 for (next = fld;
6739 (next = DECL_CHAIN (next))
6740 && TREE_CODE (next) != FIELD_DECL; );
6742 tree fldtype = TREE_TYPE (fld);
6743 if (TREE_CODE (fld) != TYPE_DECL
6744 && RECORD_OR_UNION_TYPE_P (fldtype)
6745 && TYPE_UNNAMED_P (fldtype))
6747 /* Members of anonymous structs and unions are treated as if
6748 they were members of the containing class. Descend into
6749 the anonymous struct or union and find a flexible array
6750 member or zero-length array among its fields. */
6751 find_flexarrays (fldtype, fmem);
6752 continue;
6755 /* Skip anything that's not a (non-static) data member. */
6756 if (TREE_CODE (fld) != FIELD_DECL)
6757 continue;
6759 /* Skip virtual table pointers. */
6760 if (DECL_ARTIFICIAL (fld))
6761 continue;
6763 if (field_nonempty_p (fld))
6765 /* Remember the first non-static data member. */
6766 if (!fmem->first)
6767 fmem->first = fld;
6769 /* Remember the first non-static data member after the flexible
6770 array member, if one has been found, or the zero-length array
6771 if it has been found. */
6772 if (!fmem->after && fmem->array)
6773 fmem->after = fld;
6776 /* Skip non-arrays. */
6777 if (TREE_CODE (fldtype) != ARRAY_TYPE)
6778 continue;
6780 /* Determine the upper bound of the array if it has one. */
6781 if (TYPE_DOMAIN (fldtype))
6783 if (fmem->array)
6785 /* Make a record of the zero-length array if either one
6786 such field or a flexible array member has been seen to
6787 handle the pathological and unlikely case of multiple
6788 such members. */
6789 if (!fmem->after)
6790 fmem->after = fld;
6792 else if (integer_all_onesp (TYPE_MAX_VALUE (TYPE_DOMAIN (fldtype))))
6793 /* Remember the first zero-length array unless a flexible array
6794 member has already been seen. */
6795 fmem->array = fld;
6797 else
6799 /* Flexible array members have no upper bound. */
6800 if (fmem->array)
6802 /* Replace the zero-length array if it's been stored and
6803 reset the after pointer. */
6804 if (TYPE_DOMAIN (TREE_TYPE (fmem->array)))
6806 fmem->array = fld;
6807 fmem->after = NULL_TREE;
6810 else
6811 fmem->array = fld;
6816 /* Issue diagnostics for invalid flexible array members or zero-length
6817 arrays that are not the last elements of the containing class or its
6818 base classes or that are its sole members. */
6820 static void
6821 diagnose_flexarrays (tree t, const flexmems_t *fmem)
6823 /* Members of anonymous structs and unions are considered to be members
6824 of the containing struct or union. */
6825 if (TYPE_UNNAMED_P (t) || !fmem->array)
6826 return;
6828 const char *msg = 0;
6830 if (TYPE_DOMAIN (TREE_TYPE (fmem->array)))
6832 if (fmem->after)
6833 msg = G_("zero-size array member %qD not at end of %q#T");
6834 else if (!fmem->first)
6835 msg = G_("zero-size array member %qD in an otherwise empty %q#T");
6837 if (msg && pedwarn (DECL_SOURCE_LOCATION (fmem->array),
6838 OPT_Wpedantic, msg, fmem->array, t))
6840 inform (location_of (t), "in the definition of %q#T", t);
6842 else
6844 if (fmem->after)
6845 msg = G_("flexible array member %qD not at end of %q#T");
6846 else if (!fmem->first)
6847 msg = G_("flexible array member %qD in an otherwise empty %q#T");
6849 if (msg)
6851 error_at (DECL_SOURCE_LOCATION (fmem->array), msg,
6852 fmem->array, t);
6854 /* In the unlikely event that the member following the flexible
6855 array member is declared in a different class, point to it.
6856 Otherwise it should be obvious. */
6857 if (fmem->after
6858 && (DECL_CONTEXT (fmem->after) != DECL_CONTEXT (fmem->array)))
6859 inform (DECL_SOURCE_LOCATION (fmem->after),
6860 "next member %q#D declared here",
6861 fmem->after);
6863 inform (location_of (t), "in the definition of %q#T", t);
6869 /* Recursively check to make sure that any flexible array or zero-length
6870 array members of class T or its bases are valid (i.e., not the sole
6871 non-static data member of T and, if one exists, that it is the last
6872 non-static data member of T and its base classes. FMEM is expected
6873 to be initially null and is used internally by recursive calls to
6874 the function. Issue the appropriate diagnostics for the array member
6875 that fails the checks. */
6877 static void
6878 check_flexarrays (tree t, flexmems_t *fmem /* = NULL */)
6880 /* Initialize the result of a search for flexible array and zero-length
6881 array members. Avoid doing any work if the most interesting FMEM data
6882 have already been populated. */
6883 flexmems_t flexmems = flexmems_t ();
6884 if (!fmem)
6885 fmem = &flexmems;
6886 else if (fmem->array && fmem->first && fmem->after)
6887 return;
6889 /* Recursively check the primary base class first. */
6890 if (CLASSTYPE_HAS_PRIMARY_BASE_P (t))
6892 tree basetype = BINFO_TYPE (CLASSTYPE_PRIMARY_BINFO (t));
6893 check_flexarrays (basetype, fmem);
6896 /* Recursively check the base classes. */
6897 int nbases = BINFO_N_BASE_BINFOS (TYPE_BINFO (t));
6898 for (int i = 0; i < nbases; ++i)
6900 tree base_binfo = BINFO_BASE_BINFO (TYPE_BINFO (t), i);
6902 /* The primary base class was already checked above. */
6903 if (base_binfo == CLASSTYPE_PRIMARY_BINFO (t))
6904 continue;
6906 /* Virtual base classes are at the end. */
6907 if (BINFO_VIRTUAL_P (base_binfo))
6908 continue;
6910 /* Check the base class. */
6911 check_flexarrays (BINFO_TYPE (base_binfo), fmem);
6914 if (fmem == &flexmems)
6916 /* Check virtual base classes only once per derived class.
6917 I.e., this check is not performed recursively for base
6918 classes. */
6919 int i;
6920 tree base_binfo;
6921 vec<tree, va_gc> *vbases;
6922 for (vbases = CLASSTYPE_VBASECLASSES (t), i = 0;
6923 vec_safe_iterate (vbases, i, &base_binfo); i++)
6925 /* Check the virtual base class. */
6926 tree basetype = TREE_TYPE (base_binfo);
6928 check_flexarrays (basetype, fmem);
6932 /* Search the members of the current (derived) class. */
6933 find_flexarrays (t, fmem);
6935 if (fmem == &flexmems)
6937 /* Issue diagnostics for invalid flexible and zero-length array members
6938 found in base classes or among the members of the current class. */
6939 diagnose_flexarrays (t, fmem);
6943 /* Perform processing required when the definition of T (a class type)
6944 is complete. Diagnose invalid definitions of flexible array members
6945 and zero-size arrays. */
6947 void
6948 finish_struct_1 (tree t)
6950 tree x;
6951 /* A TREE_LIST. The TREE_VALUE of each node is a FUNCTION_DECL. */
6952 tree virtuals = NULL_TREE;
6954 if (COMPLETE_TYPE_P (t))
6956 gcc_assert (MAYBE_CLASS_TYPE_P (t));
6957 error ("redefinition of %q#T", t);
6958 popclass ();
6959 return;
6962 /* If this type was previously laid out as a forward reference,
6963 make sure we lay it out again. */
6964 TYPE_SIZE (t) = NULL_TREE;
6965 CLASSTYPE_PRIMARY_BINFO (t) = NULL_TREE;
6967 /* Make assumptions about the class; we'll reset the flags if
6968 necessary. */
6969 CLASSTYPE_EMPTY_P (t) = 1;
6970 CLASSTYPE_NEARLY_EMPTY_P (t) = 1;
6971 CLASSTYPE_CONTAINS_EMPTY_CLASS_P (t) = 0;
6972 CLASSTYPE_LITERAL_P (t) = true;
6974 /* Do end-of-class semantic processing: checking the validity of the
6975 bases and members and add implicitly generated methods. */
6976 check_bases_and_members (t);
6978 /* Find the key method. */
6979 if (TYPE_CONTAINS_VPTR_P (t))
6981 /* The Itanium C++ ABI permits the key method to be chosen when
6982 the class is defined -- even though the key method so
6983 selected may later turn out to be an inline function. On
6984 some systems (such as ARM Symbian OS) the key method cannot
6985 be determined until the end of the translation unit. On such
6986 systems, we leave CLASSTYPE_KEY_METHOD set to NULL, which
6987 will cause the class to be added to KEYED_CLASSES. Then, in
6988 finish_file we will determine the key method. */
6989 if (targetm.cxx.key_method_may_be_inline ())
6990 determine_key_method (t);
6992 /* If a polymorphic class has no key method, we may emit the vtable
6993 in every translation unit where the class definition appears. If
6994 we're devirtualizing, we can look into the vtable even if we
6995 aren't emitting it. */
6996 if (CLASSTYPE_KEY_METHOD (t) == NULL_TREE)
6997 keyed_classes = tree_cons (NULL_TREE, t, keyed_classes);
7000 /* Layout the class itself. */
7001 layout_class_type (t, &virtuals);
7002 if (CLASSTYPE_AS_BASE (t) != t)
7003 /* We use the base type for trivial assignments, and hence it
7004 needs a mode. */
7005 compute_record_mode (CLASSTYPE_AS_BASE (t));
7007 /* With the layout complete, check for flexible array members and
7008 zero-length arrays that might overlap other members in the final
7009 layout. */
7010 check_flexarrays (t);
7012 virtuals = modify_all_vtables (t, nreverse (virtuals));
7014 /* If necessary, create the primary vtable for this class. */
7015 if (virtuals || TYPE_CONTAINS_VPTR_P (t))
7017 /* We must enter these virtuals into the table. */
7018 if (!CLASSTYPE_HAS_PRIMARY_BASE_P (t))
7019 build_primary_vtable (NULL_TREE, t);
7020 else if (! BINFO_NEW_VTABLE_MARKED (TYPE_BINFO (t)))
7021 /* Here we know enough to change the type of our virtual
7022 function table, but we will wait until later this function. */
7023 build_primary_vtable (CLASSTYPE_PRIMARY_BINFO (t), t);
7025 /* If we're warning about ABI tags, check the types of the new
7026 virtual functions. */
7027 if (warn_abi_tag)
7028 for (tree v = virtuals; v; v = TREE_CHAIN (v))
7029 check_abi_tags (t, TREE_VALUE (v));
7032 if (TYPE_CONTAINS_VPTR_P (t))
7034 int vindex;
7035 tree fn;
7037 if (BINFO_VTABLE (TYPE_BINFO (t)))
7038 gcc_assert (DECL_VIRTUAL_P (BINFO_VTABLE (TYPE_BINFO (t))));
7039 if (!CLASSTYPE_HAS_PRIMARY_BASE_P (t))
7040 gcc_assert (BINFO_VIRTUALS (TYPE_BINFO (t)) == NULL_TREE);
7042 /* Add entries for virtual functions introduced by this class. */
7043 BINFO_VIRTUALS (TYPE_BINFO (t))
7044 = chainon (BINFO_VIRTUALS (TYPE_BINFO (t)), virtuals);
7046 /* Set DECL_VINDEX for all functions declared in this class. */
7047 for (vindex = 0, fn = BINFO_VIRTUALS (TYPE_BINFO (t));
7049 fn = TREE_CHAIN (fn),
7050 vindex += (TARGET_VTABLE_USES_DESCRIPTORS
7051 ? TARGET_VTABLE_USES_DESCRIPTORS : 1))
7053 tree fndecl = BV_FN (fn);
7055 if (DECL_THUNK_P (fndecl))
7056 /* A thunk. We should never be calling this entry directly
7057 from this vtable -- we'd use the entry for the non
7058 thunk base function. */
7059 DECL_VINDEX (fndecl) = NULL_TREE;
7060 else if (TREE_CODE (DECL_VINDEX (fndecl)) != INTEGER_CST)
7061 DECL_VINDEX (fndecl) = build_int_cst (NULL_TREE, vindex);
7065 finish_struct_bits (t);
7066 set_method_tm_attributes (t);
7067 if (flag_openmp || flag_openmp_simd)
7068 finish_omp_declare_simd_methods (t);
7070 /* Complete the rtl for any static member objects of the type we're
7071 working on. */
7072 for (x = TYPE_FIELDS (t); x; x = DECL_CHAIN (x))
7073 if (VAR_P (x) && TREE_STATIC (x)
7074 && TREE_TYPE (x) != error_mark_node
7075 && same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (x)), t))
7076 DECL_MODE (x) = TYPE_MODE (t);
7078 /* Done with FIELDS...now decide whether to sort these for
7079 faster lookups later.
7081 We use a small number because most searches fail (succeeding
7082 ultimately as the search bores through the inheritance
7083 hierarchy), and we want this failure to occur quickly. */
7085 insert_into_classtype_sorted_fields (TYPE_FIELDS (t), t, 8);
7087 /* Complain if one of the field types requires lower visibility. */
7088 constrain_class_visibility (t);
7090 /* Make the rtl for any new vtables we have created, and unmark
7091 the base types we marked. */
7092 finish_vtbls (t);
7094 /* Build the VTT for T. */
7095 build_vtt (t);
7097 /* This warning does not make sense for Java classes, since they
7098 cannot have destructors. */
7099 if (!TYPE_FOR_JAVA (t) && warn_nonvdtor
7100 && TYPE_POLYMORPHIC_P (t) && accessible_nvdtor_p (t)
7101 && !CLASSTYPE_FINAL (t))
7102 warning (OPT_Wnon_virtual_dtor,
7103 "%q#T has virtual functions and accessible"
7104 " non-virtual destructor", t);
7106 complete_vars (t);
7108 if (warn_overloaded_virtual)
7109 warn_hidden (t);
7111 /* Class layout, assignment of virtual table slots, etc., is now
7112 complete. Give the back end a chance to tweak the visibility of
7113 the class or perform any other required target modifications. */
7114 targetm.cxx.adjust_class_at_definition (t);
7116 maybe_suppress_debug_info (t);
7118 if (flag_vtable_verify)
7119 vtv_save_class_info (t);
7121 dump_class_hierarchy (t);
7123 /* Finish debugging output for this type. */
7124 rest_of_type_compilation (t, ! LOCAL_CLASS_P (t));
7126 if (TYPE_TRANSPARENT_AGGR (t))
7128 tree field = first_field (t);
7129 if (field == NULL_TREE || error_operand_p (field))
7131 error ("type transparent %q#T does not have any fields", t);
7132 TYPE_TRANSPARENT_AGGR (t) = 0;
7134 else if (DECL_ARTIFICIAL (field))
7136 if (DECL_FIELD_IS_BASE (field))
7137 error ("type transparent class %qT has base classes", t);
7138 else
7140 gcc_checking_assert (DECL_VIRTUAL_P (field));
7141 error ("type transparent class %qT has virtual functions", t);
7143 TYPE_TRANSPARENT_AGGR (t) = 0;
7145 else if (TYPE_MODE (t) != DECL_MODE (field))
7147 error ("type transparent %q#T cannot be made transparent because "
7148 "the type of the first field has a different ABI from the "
7149 "class overall", t);
7150 TYPE_TRANSPARENT_AGGR (t) = 0;
7155 /* Insert FIELDS into T for the sorted case if the FIELDS count is
7156 equal to THRESHOLD or greater than THRESHOLD. */
7158 static void
7159 insert_into_classtype_sorted_fields (tree fields, tree t, int threshold)
7161 int n_fields = count_fields (fields);
7162 if (n_fields >= threshold)
7164 struct sorted_fields_type *field_vec = sorted_fields_type_new (n_fields);
7165 add_fields_to_record_type (fields, field_vec, 0);
7166 qsort (field_vec->elts, n_fields, sizeof (tree), field_decl_cmp);
7167 CLASSTYPE_SORTED_FIELDS (t) = field_vec;
7171 /* Insert lately defined enum ENUMTYPE into T for the sorted case. */
7173 void
7174 insert_late_enum_def_into_classtype_sorted_fields (tree enumtype, tree t)
7176 struct sorted_fields_type *sorted_fields = CLASSTYPE_SORTED_FIELDS (t);
7177 if (sorted_fields)
7179 int i;
7180 int n_fields
7181 = list_length (TYPE_VALUES (enumtype)) + sorted_fields->len;
7182 struct sorted_fields_type *field_vec = sorted_fields_type_new (n_fields);
7184 for (i = 0; i < sorted_fields->len; ++i)
7185 field_vec->elts[i] = sorted_fields->elts[i];
7187 add_enum_fields_to_record_type (enumtype, field_vec,
7188 sorted_fields->len);
7189 qsort (field_vec->elts, n_fields, sizeof (tree), field_decl_cmp);
7190 CLASSTYPE_SORTED_FIELDS (t) = field_vec;
7194 /* When T was built up, the member declarations were added in reverse
7195 order. Rearrange them to declaration order. */
7197 void
7198 unreverse_member_declarations (tree t)
7200 tree next;
7201 tree prev;
7202 tree x;
7204 /* The following lists are all in reverse order. Put them in
7205 declaration order now. */
7206 TYPE_METHODS (t) = nreverse (TYPE_METHODS (t));
7207 CLASSTYPE_DECL_LIST (t) = nreverse (CLASSTYPE_DECL_LIST (t));
7209 /* Actually, for the TYPE_FIELDS, only the non TYPE_DECLs are in
7210 reverse order, so we can't just use nreverse. */
7211 prev = NULL_TREE;
7212 for (x = TYPE_FIELDS (t);
7213 x && TREE_CODE (x) != TYPE_DECL;
7214 x = next)
7216 next = DECL_CHAIN (x);
7217 DECL_CHAIN (x) = prev;
7218 prev = x;
7220 if (prev)
7222 DECL_CHAIN (TYPE_FIELDS (t)) = x;
7223 if (prev)
7224 TYPE_FIELDS (t) = prev;
7228 tree
7229 finish_struct (tree t, tree attributes)
7231 location_t saved_loc = input_location;
7233 /* Now that we've got all the field declarations, reverse everything
7234 as necessary. */
7235 unreverse_member_declarations (t);
7237 cplus_decl_attributes (&t, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
7238 fixup_attribute_variants (t);
7240 /* Nadger the current location so that diagnostics point to the start of
7241 the struct, not the end. */
7242 input_location = DECL_SOURCE_LOCATION (TYPE_NAME (t));
7244 if (processing_template_decl)
7246 tree x;
7248 finish_struct_methods (t);
7249 TYPE_SIZE (t) = bitsize_zero_node;
7250 TYPE_SIZE_UNIT (t) = size_zero_node;
7252 /* We need to emit an error message if this type was used as a parameter
7253 and it is an abstract type, even if it is a template. We construct
7254 a simple CLASSTYPE_PURE_VIRTUALS list without taking bases into
7255 account and we call complete_vars with this type, which will check
7256 the PARM_DECLS. Note that while the type is being defined,
7257 CLASSTYPE_PURE_VIRTUALS contains the list of the inline friends
7258 (see CLASSTYPE_INLINE_FRIENDS) so we need to clear it. */
7259 CLASSTYPE_PURE_VIRTUALS (t) = NULL;
7260 for (x = TYPE_METHODS (t); x; x = DECL_CHAIN (x))
7261 if (DECL_PURE_VIRTUAL_P (x))
7262 vec_safe_push (CLASSTYPE_PURE_VIRTUALS (t), x);
7263 complete_vars (t);
7264 /* We need to add the target functions to the CLASSTYPE_METHOD_VEC if
7265 an enclosing scope is a template class, so that this function be
7266 found by lookup_fnfields_1 when the using declaration is not
7267 instantiated yet. */
7268 for (x = TYPE_FIELDS (t); x; x = DECL_CHAIN (x))
7269 if (TREE_CODE (x) == USING_DECL)
7271 tree fn = strip_using_decl (x);
7272 if (is_overloaded_fn (fn))
7273 for (; fn; fn = OVL_NEXT (fn))
7274 add_method (t, OVL_CURRENT (fn), x);
7277 /* Remember current #pragma pack value. */
7278 TYPE_PRECISION (t) = maximum_field_alignment;
7280 /* Fix up any variants we've already built. */
7281 for (x = TYPE_NEXT_VARIANT (t); x; x = TYPE_NEXT_VARIANT (x))
7283 TYPE_SIZE (x) = TYPE_SIZE (t);
7284 TYPE_SIZE_UNIT (x) = TYPE_SIZE_UNIT (t);
7285 TYPE_FIELDS (x) = TYPE_FIELDS (t);
7286 TYPE_METHODS (x) = TYPE_METHODS (t);
7289 else
7290 finish_struct_1 (t);
7292 if (is_std_init_list (t))
7294 /* People keep complaining that the compiler crashes on an invalid
7295 definition of initializer_list, so I guess we should explicitly
7296 reject it. What the compiler internals care about is that it's a
7297 template and has a pointer field followed by an integer field. */
7298 bool ok = false;
7299 if (processing_template_decl)
7301 tree f = next_initializable_field (TYPE_FIELDS (t));
7302 if (f && TREE_CODE (TREE_TYPE (f)) == POINTER_TYPE)
7304 f = next_initializable_field (DECL_CHAIN (f));
7305 if (f && same_type_p (TREE_TYPE (f), size_type_node))
7306 ok = true;
7309 if (!ok)
7310 fatal_error (input_location,
7311 "definition of std::initializer_list does not match "
7312 "#include <initializer_list>");
7315 input_location = saved_loc;
7317 TYPE_BEING_DEFINED (t) = 0;
7319 if (current_class_type)
7320 popclass ();
7321 else
7322 error ("trying to finish struct, but kicked out due to previous parse errors");
7324 if (processing_template_decl && at_function_scope_p ()
7325 /* Lambdas are defined by the LAMBDA_EXPR. */
7326 && !LAMBDA_TYPE_P (t))
7327 add_stmt (build_min (TAG_DEFN, t));
7329 return t;
7332 /* Hash table to avoid endless recursion when handling references. */
7333 static hash_table<nofree_ptr_hash<tree_node> > *fixed_type_or_null_ref_ht;
7335 /* Return the dynamic type of INSTANCE, if known.
7336 Used to determine whether the virtual function table is needed
7337 or not.
7339 *NONNULL is set iff INSTANCE can be known to be nonnull, regardless
7340 of our knowledge of its type. *NONNULL should be initialized
7341 before this function is called. */
7343 static tree
7344 fixed_type_or_null (tree instance, int *nonnull, int *cdtorp)
7346 #define RECUR(T) fixed_type_or_null((T), nonnull, cdtorp)
7348 switch (TREE_CODE (instance))
7350 case INDIRECT_REF:
7351 if (POINTER_TYPE_P (TREE_TYPE (instance)))
7352 return NULL_TREE;
7353 else
7354 return RECUR (TREE_OPERAND (instance, 0));
7356 case CALL_EXPR:
7357 /* This is a call to a constructor, hence it's never zero. */
7358 if (TREE_HAS_CONSTRUCTOR (instance))
7360 if (nonnull)
7361 *nonnull = 1;
7362 return TREE_TYPE (instance);
7364 return NULL_TREE;
7366 case SAVE_EXPR:
7367 /* This is a call to a constructor, hence it's never zero. */
7368 if (TREE_HAS_CONSTRUCTOR (instance))
7370 if (nonnull)
7371 *nonnull = 1;
7372 return TREE_TYPE (instance);
7374 return RECUR (TREE_OPERAND (instance, 0));
7376 case POINTER_PLUS_EXPR:
7377 case PLUS_EXPR:
7378 case MINUS_EXPR:
7379 if (TREE_CODE (TREE_OPERAND (instance, 0)) == ADDR_EXPR)
7380 return RECUR (TREE_OPERAND (instance, 0));
7381 if (TREE_CODE (TREE_OPERAND (instance, 1)) == INTEGER_CST)
7382 /* Propagate nonnull. */
7383 return RECUR (TREE_OPERAND (instance, 0));
7385 return NULL_TREE;
7387 CASE_CONVERT:
7388 return RECUR (TREE_OPERAND (instance, 0));
7390 case ADDR_EXPR:
7391 instance = TREE_OPERAND (instance, 0);
7392 if (nonnull)
7394 /* Just because we see an ADDR_EXPR doesn't mean we're dealing
7395 with a real object -- given &p->f, p can still be null. */
7396 tree t = get_base_address (instance);
7397 /* ??? Probably should check DECL_WEAK here. */
7398 if (t && DECL_P (t))
7399 *nonnull = 1;
7401 return RECUR (instance);
7403 case COMPONENT_REF:
7404 /* If this component is really a base class reference, then the field
7405 itself isn't definitive. */
7406 if (DECL_FIELD_IS_BASE (TREE_OPERAND (instance, 1)))
7407 return RECUR (TREE_OPERAND (instance, 0));
7408 return RECUR (TREE_OPERAND (instance, 1));
7410 case VAR_DECL:
7411 case FIELD_DECL:
7412 if (TREE_CODE (TREE_TYPE (instance)) == ARRAY_TYPE
7413 && MAYBE_CLASS_TYPE_P (TREE_TYPE (TREE_TYPE (instance))))
7415 if (nonnull)
7416 *nonnull = 1;
7417 return TREE_TYPE (TREE_TYPE (instance));
7419 /* fall through. */
7420 case TARGET_EXPR:
7421 case PARM_DECL:
7422 case RESULT_DECL:
7423 if (MAYBE_CLASS_TYPE_P (TREE_TYPE (instance)))
7425 if (nonnull)
7426 *nonnull = 1;
7427 return TREE_TYPE (instance);
7429 else if (instance == current_class_ptr)
7431 if (nonnull)
7432 *nonnull = 1;
7434 /* if we're in a ctor or dtor, we know our type. If
7435 current_class_ptr is set but we aren't in a function, we're in
7436 an NSDMI (and therefore a constructor). */
7437 if (current_scope () != current_function_decl
7438 || (DECL_LANG_SPECIFIC (current_function_decl)
7439 && (DECL_CONSTRUCTOR_P (current_function_decl)
7440 || DECL_DESTRUCTOR_P (current_function_decl))))
7442 if (cdtorp)
7443 *cdtorp = 1;
7444 return TREE_TYPE (TREE_TYPE (instance));
7447 else if (TREE_CODE (TREE_TYPE (instance)) == REFERENCE_TYPE)
7449 /* We only need one hash table because it is always left empty. */
7450 if (!fixed_type_or_null_ref_ht)
7451 fixed_type_or_null_ref_ht
7452 = new hash_table<nofree_ptr_hash<tree_node> > (37);
7454 /* Reference variables should be references to objects. */
7455 if (nonnull)
7456 *nonnull = 1;
7458 /* Enter the INSTANCE in a table to prevent recursion; a
7459 variable's initializer may refer to the variable
7460 itself. */
7461 if (VAR_P (instance)
7462 && DECL_INITIAL (instance)
7463 && !type_dependent_expression_p_push (DECL_INITIAL (instance))
7464 && !fixed_type_or_null_ref_ht->find (instance))
7466 tree type;
7467 tree_node **slot;
7469 slot = fixed_type_or_null_ref_ht->find_slot (instance, INSERT);
7470 *slot = instance;
7471 type = RECUR (DECL_INITIAL (instance));
7472 fixed_type_or_null_ref_ht->remove_elt (instance);
7474 return type;
7477 return NULL_TREE;
7479 default:
7480 return NULL_TREE;
7482 #undef RECUR
7485 /* Return nonzero if the dynamic type of INSTANCE is known, and
7486 equivalent to the static type. We also handle the case where
7487 INSTANCE is really a pointer. Return negative if this is a
7488 ctor/dtor. There the dynamic type is known, but this might not be
7489 the most derived base of the original object, and hence virtual
7490 bases may not be laid out according to this type.
7492 Used to determine whether the virtual function table is needed
7493 or not.
7495 *NONNULL is set iff INSTANCE can be known to be nonnull, regardless
7496 of our knowledge of its type. *NONNULL should be initialized
7497 before this function is called. */
7500 resolves_to_fixed_type_p (tree instance, int* nonnull)
7502 tree t = TREE_TYPE (instance);
7503 int cdtorp = 0;
7504 tree fixed;
7506 /* processing_template_decl can be false in a template if we're in
7507 instantiate_non_dependent_expr, but we still want to suppress
7508 this check. */
7509 if (in_template_function ())
7511 /* In a template we only care about the type of the result. */
7512 if (nonnull)
7513 *nonnull = true;
7514 return true;
7517 fixed = fixed_type_or_null (instance, nonnull, &cdtorp);
7518 if (fixed == NULL_TREE)
7519 return 0;
7520 if (POINTER_TYPE_P (t))
7521 t = TREE_TYPE (t);
7522 if (!same_type_ignoring_top_level_qualifiers_p (t, fixed))
7523 return 0;
7524 return cdtorp ? -1 : 1;
7528 void
7529 init_class_processing (void)
7531 current_class_depth = 0;
7532 current_class_stack_size = 10;
7533 current_class_stack
7534 = XNEWVEC (struct class_stack_node, current_class_stack_size);
7535 vec_alloc (local_classes, 8);
7536 sizeof_biggest_empty_class = size_zero_node;
7538 ridpointers[(int) RID_PUBLIC] = access_public_node;
7539 ridpointers[(int) RID_PRIVATE] = access_private_node;
7540 ridpointers[(int) RID_PROTECTED] = access_protected_node;
7543 /* Restore the cached PREVIOUS_CLASS_LEVEL. */
7545 static void
7546 restore_class_cache (void)
7548 tree type;
7550 /* We are re-entering the same class we just left, so we don't
7551 have to search the whole inheritance matrix to find all the
7552 decls to bind again. Instead, we install the cached
7553 class_shadowed list and walk through it binding names. */
7554 push_binding_level (previous_class_level);
7555 class_binding_level = previous_class_level;
7556 /* Restore IDENTIFIER_TYPE_VALUE. */
7557 for (type = class_binding_level->type_shadowed;
7558 type;
7559 type = TREE_CHAIN (type))
7560 SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (type), TREE_TYPE (type));
7563 /* Set global variables CURRENT_CLASS_NAME and CURRENT_CLASS_TYPE as
7564 appropriate for TYPE.
7566 So that we may avoid calls to lookup_name, we cache the _TYPE
7567 nodes of local TYPE_DECLs in the TREE_TYPE field of the name.
7569 For multiple inheritance, we perform a two-pass depth-first search
7570 of the type lattice. */
7572 void
7573 pushclass (tree type)
7575 class_stack_node_t csn;
7577 type = TYPE_MAIN_VARIANT (type);
7579 /* Make sure there is enough room for the new entry on the stack. */
7580 if (current_class_depth + 1 >= current_class_stack_size)
7582 current_class_stack_size *= 2;
7583 current_class_stack
7584 = XRESIZEVEC (struct class_stack_node, current_class_stack,
7585 current_class_stack_size);
7588 /* Insert a new entry on the class stack. */
7589 csn = current_class_stack + current_class_depth;
7590 csn->name = current_class_name;
7591 csn->type = current_class_type;
7592 csn->access = current_access_specifier;
7593 csn->names_used = 0;
7594 csn->hidden = 0;
7595 current_class_depth++;
7597 /* Now set up the new type. */
7598 current_class_name = TYPE_NAME (type);
7599 if (TREE_CODE (current_class_name) == TYPE_DECL)
7600 current_class_name = DECL_NAME (current_class_name);
7601 current_class_type = type;
7603 /* By default, things in classes are private, while things in
7604 structures or unions are public. */
7605 current_access_specifier = (CLASSTYPE_DECLARED_CLASS (type)
7606 ? access_private_node
7607 : access_public_node);
7609 if (previous_class_level
7610 && type != previous_class_level->this_entity
7611 && current_class_depth == 1)
7613 /* Forcibly remove any old class remnants. */
7614 invalidate_class_lookup_cache ();
7617 if (!previous_class_level
7618 || type != previous_class_level->this_entity
7619 || current_class_depth > 1)
7620 pushlevel_class ();
7621 else
7622 restore_class_cache ();
7625 /* When we exit a toplevel class scope, we save its binding level so
7626 that we can restore it quickly. Here, we've entered some other
7627 class, so we must invalidate our cache. */
7629 void
7630 invalidate_class_lookup_cache (void)
7632 previous_class_level = NULL;
7635 /* Get out of the current class scope. If we were in a class scope
7636 previously, that is the one popped to. */
7638 void
7639 popclass (void)
7641 poplevel_class ();
7643 current_class_depth--;
7644 current_class_name = current_class_stack[current_class_depth].name;
7645 current_class_type = current_class_stack[current_class_depth].type;
7646 current_access_specifier = current_class_stack[current_class_depth].access;
7647 if (current_class_stack[current_class_depth].names_used)
7648 splay_tree_delete (current_class_stack[current_class_depth].names_used);
7651 /* Mark the top of the class stack as hidden. */
7653 void
7654 push_class_stack (void)
7656 if (current_class_depth)
7657 ++current_class_stack[current_class_depth - 1].hidden;
7660 /* Mark the top of the class stack as un-hidden. */
7662 void
7663 pop_class_stack (void)
7665 if (current_class_depth)
7666 --current_class_stack[current_class_depth - 1].hidden;
7669 /* Returns 1 if the class type currently being defined is either T or
7670 a nested type of T. Returns the type from the current_class_stack,
7671 which might be equivalent to but not equal to T in case of
7672 constrained partial specializations. */
7674 tree
7675 currently_open_class (tree t)
7677 int i;
7679 if (!CLASS_TYPE_P (t))
7680 return NULL_TREE;
7682 t = TYPE_MAIN_VARIANT (t);
7684 /* We start looking from 1 because entry 0 is from global scope,
7685 and has no type. */
7686 for (i = current_class_depth; i > 0; --i)
7688 tree c;
7689 if (i == current_class_depth)
7690 c = current_class_type;
7691 else
7693 if (current_class_stack[i].hidden)
7694 break;
7695 c = current_class_stack[i].type;
7697 if (!c)
7698 continue;
7699 if (same_type_p (c, t))
7700 return c;
7702 return NULL_TREE;
7705 /* If either current_class_type or one of its enclosing classes are derived
7706 from T, return the appropriate type. Used to determine how we found
7707 something via unqualified lookup. */
7709 tree
7710 currently_open_derived_class (tree t)
7712 int i;
7714 /* The bases of a dependent type are unknown. */
7715 if (dependent_type_p (t))
7716 return NULL_TREE;
7718 if (!current_class_type)
7719 return NULL_TREE;
7721 if (DERIVED_FROM_P (t, current_class_type))
7722 return current_class_type;
7724 for (i = current_class_depth - 1; i > 0; --i)
7726 if (current_class_stack[i].hidden)
7727 break;
7728 if (DERIVED_FROM_P (t, current_class_stack[i].type))
7729 return current_class_stack[i].type;
7732 return NULL_TREE;
7735 /* Return the outermost enclosing class type that is still open, or
7736 NULL_TREE. */
7738 tree
7739 outermost_open_class (void)
7741 if (!current_class_type)
7742 return NULL_TREE;
7743 tree r = NULL_TREE;
7744 if (TYPE_BEING_DEFINED (current_class_type))
7745 r = current_class_type;
7746 for (int i = current_class_depth - 1; i > 0; --i)
7748 if (current_class_stack[i].hidden)
7749 break;
7750 tree t = current_class_stack[i].type;
7751 if (!TYPE_BEING_DEFINED (t))
7752 break;
7753 r = t;
7755 return r;
7758 /* Returns the innermost class type which is not a lambda closure type. */
7760 tree
7761 current_nonlambda_class_type (void)
7763 int i;
7765 /* We start looking from 1 because entry 0 is from global scope,
7766 and has no type. */
7767 for (i = current_class_depth; i > 0; --i)
7769 tree c;
7770 if (i == current_class_depth)
7771 c = current_class_type;
7772 else
7774 if (current_class_stack[i].hidden)
7775 break;
7776 c = current_class_stack[i].type;
7778 if (!c)
7779 continue;
7780 if (!LAMBDA_TYPE_P (c))
7781 return c;
7783 return NULL_TREE;
7786 /* When entering a class scope, all enclosing class scopes' names with
7787 static meaning (static variables, static functions, types and
7788 enumerators) have to be visible. This recursive function calls
7789 pushclass for all enclosing class contexts until global or a local
7790 scope is reached. TYPE is the enclosed class. */
7792 void
7793 push_nested_class (tree type)
7795 /* A namespace might be passed in error cases, like A::B:C. */
7796 if (type == NULL_TREE
7797 || !CLASS_TYPE_P (type))
7798 return;
7800 push_nested_class (DECL_CONTEXT (TYPE_MAIN_DECL (type)));
7802 pushclass (type);
7805 /* Undoes a push_nested_class call. */
7807 void
7808 pop_nested_class (void)
7810 tree context = DECL_CONTEXT (TYPE_MAIN_DECL (current_class_type));
7812 popclass ();
7813 if (context && CLASS_TYPE_P (context))
7814 pop_nested_class ();
7817 /* Returns the number of extern "LANG" blocks we are nested within. */
7820 current_lang_depth (void)
7822 return vec_safe_length (current_lang_base);
7825 /* Set global variables CURRENT_LANG_NAME to appropriate value
7826 so that behavior of name-mangling machinery is correct. */
7828 void
7829 push_lang_context (tree name)
7831 vec_safe_push (current_lang_base, current_lang_name);
7833 if (name == lang_name_cplusplus)
7835 current_lang_name = name;
7837 else if (name == lang_name_java)
7839 current_lang_name = name;
7840 /* DECL_IGNORED_P is initially set for these types, to avoid clutter.
7841 (See record_builtin_java_type in decl.c.) However, that causes
7842 incorrect debug entries if these types are actually used.
7843 So we re-enable debug output after extern "Java". */
7844 DECL_IGNORED_P (TYPE_NAME (java_byte_type_node)) = 0;
7845 DECL_IGNORED_P (TYPE_NAME (java_short_type_node)) = 0;
7846 DECL_IGNORED_P (TYPE_NAME (java_int_type_node)) = 0;
7847 DECL_IGNORED_P (TYPE_NAME (java_long_type_node)) = 0;
7848 DECL_IGNORED_P (TYPE_NAME (java_float_type_node)) = 0;
7849 DECL_IGNORED_P (TYPE_NAME (java_double_type_node)) = 0;
7850 DECL_IGNORED_P (TYPE_NAME (java_char_type_node)) = 0;
7851 DECL_IGNORED_P (TYPE_NAME (java_boolean_type_node)) = 0;
7853 else if (name == lang_name_c)
7855 current_lang_name = name;
7857 else
7858 error ("language string %<\"%E\"%> not recognized", name);
7861 /* Get out of the current language scope. */
7863 void
7864 pop_lang_context (void)
7866 current_lang_name = current_lang_base->pop ();
7869 /* Type instantiation routines. */
7871 /* Given an OVERLOAD and a TARGET_TYPE, return the function that
7872 matches the TARGET_TYPE. If there is no satisfactory match, return
7873 error_mark_node, and issue an error & warning messages under
7874 control of FLAGS. Permit pointers to member function if FLAGS
7875 permits. If TEMPLATE_ONLY, the name of the overloaded function was
7876 a template-id, and EXPLICIT_TARGS are the explicitly provided
7877 template arguments.
7879 If OVERLOAD is for one or more member functions, then ACCESS_PATH
7880 is the base path used to reference those member functions. If
7881 the address is resolved to a member function, access checks will be
7882 performed and errors issued if appropriate. */
7884 static tree
7885 resolve_address_of_overloaded_function (tree target_type,
7886 tree overload,
7887 tsubst_flags_t complain,
7888 bool template_only,
7889 tree explicit_targs,
7890 tree access_path)
7892 /* Here's what the standard says:
7894 [over.over]
7896 If the name is a function template, template argument deduction
7897 is done, and if the argument deduction succeeds, the deduced
7898 arguments are used to generate a single template function, which
7899 is added to the set of overloaded functions considered.
7901 Non-member functions and static member functions match targets of
7902 type "pointer-to-function" or "reference-to-function." Nonstatic
7903 member functions match targets of type "pointer-to-member
7904 function;" the function type of the pointer to member is used to
7905 select the member function from the set of overloaded member
7906 functions. If a nonstatic member function is selected, the
7907 reference to the overloaded function name is required to have the
7908 form of a pointer to member as described in 5.3.1.
7910 If more than one function is selected, any template functions in
7911 the set are eliminated if the set also contains a non-template
7912 function, and any given template function is eliminated if the
7913 set contains a second template function that is more specialized
7914 than the first according to the partial ordering rules 14.5.5.2.
7915 After such eliminations, if any, there shall remain exactly one
7916 selected function. */
7918 int is_ptrmem = 0;
7919 /* We store the matches in a TREE_LIST rooted here. The functions
7920 are the TREE_PURPOSE, not the TREE_VALUE, in this list, for easy
7921 interoperability with most_specialized_instantiation. */
7922 tree matches = NULL_TREE;
7923 tree fn;
7924 tree target_fn_type;
7926 /* By the time we get here, we should be seeing only real
7927 pointer-to-member types, not the internal POINTER_TYPE to
7928 METHOD_TYPE representation. */
7929 gcc_assert (!TYPE_PTR_P (target_type)
7930 || TREE_CODE (TREE_TYPE (target_type)) != METHOD_TYPE);
7932 gcc_assert (is_overloaded_fn (overload));
7934 /* Check that the TARGET_TYPE is reasonable. */
7935 if (TYPE_PTRFN_P (target_type)
7936 || TYPE_REFFN_P (target_type))
7937 /* This is OK. */;
7938 else if (TYPE_PTRMEMFUNC_P (target_type))
7939 /* This is OK, too. */
7940 is_ptrmem = 1;
7941 else if (TREE_CODE (target_type) == FUNCTION_TYPE)
7942 /* This is OK, too. This comes from a conversion to reference
7943 type. */
7944 target_type = build_reference_type (target_type);
7945 else
7947 if (complain & tf_error)
7948 error ("cannot resolve overloaded function %qD based on"
7949 " conversion to type %qT",
7950 DECL_NAME (OVL_FUNCTION (overload)), target_type);
7951 return error_mark_node;
7954 /* Non-member functions and static member functions match targets of type
7955 "pointer-to-function" or "reference-to-function." Nonstatic member
7956 functions match targets of type "pointer-to-member-function;" the
7957 function type of the pointer to member is used to select the member
7958 function from the set of overloaded member functions.
7960 So figure out the FUNCTION_TYPE that we want to match against. */
7961 target_fn_type = static_fn_type (target_type);
7963 /* If we can find a non-template function that matches, we can just
7964 use it. There's no point in generating template instantiations
7965 if we're just going to throw them out anyhow. But, of course, we
7966 can only do this when we don't *need* a template function. */
7967 if (!template_only)
7969 tree fns;
7971 for (fns = overload; fns; fns = OVL_NEXT (fns))
7973 tree fn = OVL_CURRENT (fns);
7975 if (TREE_CODE (fn) == TEMPLATE_DECL)
7976 /* We're not looking for templates just yet. */
7977 continue;
7979 if ((TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
7980 != is_ptrmem)
7981 /* We're looking for a non-static member, and this isn't
7982 one, or vice versa. */
7983 continue;
7985 /* Ignore functions which haven't been explicitly
7986 declared. */
7987 if (DECL_ANTICIPATED (fn))
7988 continue;
7990 /* See if there's a match. */
7991 tree fntype = static_fn_type (fn);
7992 if (same_type_p (target_fn_type, fntype)
7993 || can_convert_tx_safety (target_fn_type, fntype))
7994 matches = tree_cons (fn, NULL_TREE, matches);
7998 /* Now, if we've already got a match (or matches), there's no need
7999 to proceed to the template functions. But, if we don't have a
8000 match we need to look at them, too. */
8001 if (!matches)
8003 tree target_arg_types;
8004 tree target_ret_type;
8005 tree fns;
8006 tree *args;
8007 unsigned int nargs, ia;
8008 tree arg;
8010 target_arg_types = TYPE_ARG_TYPES (target_fn_type);
8011 target_ret_type = TREE_TYPE (target_fn_type);
8013 nargs = list_length (target_arg_types);
8014 args = XALLOCAVEC (tree, nargs);
8015 for (arg = target_arg_types, ia = 0;
8016 arg != NULL_TREE && arg != void_list_node;
8017 arg = TREE_CHAIN (arg), ++ia)
8018 args[ia] = TREE_VALUE (arg);
8019 nargs = ia;
8021 for (fns = overload; fns; fns = OVL_NEXT (fns))
8023 tree fn = OVL_CURRENT (fns);
8024 tree instantiation;
8025 tree targs;
8027 if (TREE_CODE (fn) != TEMPLATE_DECL)
8028 /* We're only looking for templates. */
8029 continue;
8031 if ((TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
8032 != is_ptrmem)
8033 /* We're not looking for a non-static member, and this is
8034 one, or vice versa. */
8035 continue;
8037 tree ret = target_ret_type;
8039 /* If the template has a deduced return type, don't expose it to
8040 template argument deduction. */
8041 if (undeduced_auto_decl (fn))
8042 ret = NULL_TREE;
8044 /* Try to do argument deduction. */
8045 targs = make_tree_vec (DECL_NTPARMS (fn));
8046 instantiation = fn_type_unification (fn, explicit_targs, targs, args,
8047 nargs, ret,
8048 DEDUCE_EXACT, LOOKUP_NORMAL,
8049 false, false);
8050 if (instantiation == error_mark_node)
8051 /* Instantiation failed. */
8052 continue;
8054 /* Constraints must be satisfied. This is done before
8055 return type deduction since that instantiates the
8056 function. */
8057 if (flag_concepts && !constraints_satisfied_p (instantiation))
8058 continue;
8060 /* And now force instantiation to do return type deduction. */
8061 if (undeduced_auto_decl (instantiation))
8063 ++function_depth;
8064 instantiate_decl (instantiation, /*defer*/false, /*class*/false);
8065 --function_depth;
8067 require_deduced_type (instantiation);
8070 /* See if there's a match. */
8071 tree fntype = static_fn_type (instantiation);
8072 if (same_type_p (target_fn_type, fntype)
8073 || can_convert_tx_safety (target_fn_type, fntype))
8074 matches = tree_cons (instantiation, fn, matches);
8077 /* Now, remove all but the most specialized of the matches. */
8078 if (matches)
8080 tree match = most_specialized_instantiation (matches);
8082 if (match != error_mark_node)
8083 matches = tree_cons (TREE_PURPOSE (match),
8084 NULL_TREE,
8085 NULL_TREE);
8089 /* Now we should have exactly one function in MATCHES. */
8090 if (matches == NULL_TREE)
8092 /* There were *no* matches. */
8093 if (complain & tf_error)
8095 error ("no matches converting function %qD to type %q#T",
8096 DECL_NAME (OVL_CURRENT (overload)),
8097 target_type);
8099 print_candidates (overload);
8101 return error_mark_node;
8103 else if (TREE_CHAIN (matches))
8105 /* There were too many matches. First check if they're all
8106 the same function. */
8107 tree match = NULL_TREE;
8109 fn = TREE_PURPOSE (matches);
8111 /* For multi-versioned functions, more than one match is just fine and
8112 decls_match will return false as they are different. */
8113 for (match = TREE_CHAIN (matches); match; match = TREE_CHAIN (match))
8114 if (!decls_match (fn, TREE_PURPOSE (match))
8115 && !targetm.target_option.function_versions
8116 (fn, TREE_PURPOSE (match)))
8117 break;
8119 if (match)
8121 if (complain & tf_error)
8123 error ("converting overloaded function %qD to type %q#T is ambiguous",
8124 DECL_NAME (OVL_FUNCTION (overload)),
8125 target_type);
8127 /* Since print_candidates expects the functions in the
8128 TREE_VALUE slot, we flip them here. */
8129 for (match = matches; match; match = TREE_CHAIN (match))
8130 TREE_VALUE (match) = TREE_PURPOSE (match);
8132 print_candidates (matches);
8135 return error_mark_node;
8139 /* Good, exactly one match. Now, convert it to the correct type. */
8140 fn = TREE_PURPOSE (matches);
8142 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
8143 && !(complain & tf_ptrmem_ok) && !flag_ms_extensions)
8145 static int explained;
8147 if (!(complain & tf_error))
8148 return error_mark_node;
8150 permerror (input_location, "assuming pointer to member %qD", fn);
8151 if (!explained)
8153 inform (input_location, "(a pointer to member can only be formed with %<&%E%>)", fn);
8154 explained = 1;
8158 /* If a pointer to a function that is multi-versioned is requested, the
8159 pointer to the dispatcher function is returned instead. This works
8160 well because indirectly calling the function will dispatch the right
8161 function version at run-time. */
8162 if (DECL_FUNCTION_VERSIONED (fn))
8164 fn = get_function_version_dispatcher (fn);
8165 if (fn == NULL)
8166 return error_mark_node;
8167 /* Mark all the versions corresponding to the dispatcher as used. */
8168 if (!(complain & tf_conv))
8169 mark_versions_used (fn);
8172 /* If we're doing overload resolution purely for the purpose of
8173 determining conversion sequences, we should not consider the
8174 function used. If this conversion sequence is selected, the
8175 function will be marked as used at this point. */
8176 if (!(complain & tf_conv))
8178 /* Make =delete work with SFINAE. */
8179 if (DECL_DELETED_FN (fn) && !(complain & tf_error))
8180 return error_mark_node;
8181 if (!mark_used (fn, complain) && !(complain & tf_error))
8182 return error_mark_node;
8185 /* We could not check access to member functions when this
8186 expression was originally created since we did not know at that
8187 time to which function the expression referred. */
8188 if (DECL_FUNCTION_MEMBER_P (fn))
8190 gcc_assert (access_path);
8191 perform_or_defer_access_check (access_path, fn, fn, complain);
8194 if (TYPE_PTRFN_P (target_type) || TYPE_PTRMEMFUNC_P (target_type))
8195 return cp_build_addr_expr (fn, complain);
8196 else
8198 /* The target must be a REFERENCE_TYPE. Above, cp_build_unary_op
8199 will mark the function as addressed, but here we must do it
8200 explicitly. */
8201 cxx_mark_addressable (fn);
8203 return fn;
8207 /* This function will instantiate the type of the expression given in
8208 RHS to match the type of LHSTYPE. If errors exist, then return
8209 error_mark_node. COMPLAIN is a bit mask. If TF_ERROR is set, then
8210 we complain on errors. If we are not complaining, never modify rhs,
8211 as overload resolution wants to try many possible instantiations, in
8212 the hope that at least one will work.
8214 For non-recursive calls, LHSTYPE should be a function, pointer to
8215 function, or a pointer to member function. */
8217 tree
8218 instantiate_type (tree lhstype, tree rhs, tsubst_flags_t complain)
8220 tsubst_flags_t complain_in = complain;
8221 tree access_path = NULL_TREE;
8223 complain &= ~tf_ptrmem_ok;
8225 if (lhstype == unknown_type_node)
8227 if (complain & tf_error)
8228 error ("not enough type information");
8229 return error_mark_node;
8232 if (TREE_TYPE (rhs) != NULL_TREE && ! (type_unknown_p (rhs)))
8234 tree fntype = non_reference (lhstype);
8235 if (same_type_p (fntype, TREE_TYPE (rhs)))
8236 return rhs;
8237 if (flag_ms_extensions
8238 && TYPE_PTRMEMFUNC_P (fntype)
8239 && !TYPE_PTRMEMFUNC_P (TREE_TYPE (rhs)))
8240 /* Microsoft allows `A::f' to be resolved to a
8241 pointer-to-member. */
8243 else
8245 if (complain & tf_error)
8246 error ("cannot convert %qE from type %qT to type %qT",
8247 rhs, TREE_TYPE (rhs), fntype);
8248 return error_mark_node;
8252 if (BASELINK_P (rhs))
8254 access_path = BASELINK_ACCESS_BINFO (rhs);
8255 rhs = BASELINK_FUNCTIONS (rhs);
8258 /* If we are in a template, and have a NON_DEPENDENT_EXPR, we cannot
8259 deduce any type information. */
8260 if (TREE_CODE (rhs) == NON_DEPENDENT_EXPR)
8262 if (complain & tf_error)
8263 error ("not enough type information");
8264 return error_mark_node;
8267 /* If we instantiate a template, and it is a A ?: C expression
8268 with omitted B, look through the SAVE_EXPR. */
8269 if (TREE_CODE (rhs) == SAVE_EXPR)
8270 rhs = TREE_OPERAND (rhs, 0);
8272 /* There are only a few kinds of expressions that may have a type
8273 dependent on overload resolution. */
8274 gcc_assert (TREE_CODE (rhs) == ADDR_EXPR
8275 || TREE_CODE (rhs) == COMPONENT_REF
8276 || is_overloaded_fn (rhs)
8277 || (flag_ms_extensions && TREE_CODE (rhs) == FUNCTION_DECL));
8279 /* This should really only be used when attempting to distinguish
8280 what sort of a pointer to function we have. For now, any
8281 arithmetic operation which is not supported on pointers
8282 is rejected as an error. */
8284 switch (TREE_CODE (rhs))
8286 case COMPONENT_REF:
8288 tree member = TREE_OPERAND (rhs, 1);
8290 member = instantiate_type (lhstype, member, complain);
8291 if (member != error_mark_node
8292 && TREE_SIDE_EFFECTS (TREE_OPERAND (rhs, 0)))
8293 /* Do not lose object's side effects. */
8294 return build2 (COMPOUND_EXPR, TREE_TYPE (member),
8295 TREE_OPERAND (rhs, 0), member);
8296 return member;
8299 case OFFSET_REF:
8300 rhs = TREE_OPERAND (rhs, 1);
8301 if (BASELINK_P (rhs))
8302 return instantiate_type (lhstype, rhs, complain_in);
8304 /* This can happen if we are forming a pointer-to-member for a
8305 member template. */
8306 gcc_assert (TREE_CODE (rhs) == TEMPLATE_ID_EXPR);
8308 /* Fall through. */
8310 case TEMPLATE_ID_EXPR:
8312 tree fns = TREE_OPERAND (rhs, 0);
8313 tree args = TREE_OPERAND (rhs, 1);
8315 return
8316 resolve_address_of_overloaded_function (lhstype, fns, complain_in,
8317 /*template_only=*/true,
8318 args, access_path);
8321 case OVERLOAD:
8322 case FUNCTION_DECL:
8323 return
8324 resolve_address_of_overloaded_function (lhstype, rhs, complain_in,
8325 /*template_only=*/false,
8326 /*explicit_targs=*/NULL_TREE,
8327 access_path);
8329 case ADDR_EXPR:
8331 if (PTRMEM_OK_P (rhs))
8332 complain |= tf_ptrmem_ok;
8334 return instantiate_type (lhstype, TREE_OPERAND (rhs, 0), complain);
8337 case ERROR_MARK:
8338 return error_mark_node;
8340 default:
8341 gcc_unreachable ();
8343 return error_mark_node;
8346 /* Return the name of the virtual function pointer field
8347 (as an IDENTIFIER_NODE) for the given TYPE. Note that
8348 this may have to look back through base types to find the
8349 ultimate field name. (For single inheritance, these could
8350 all be the same name. Who knows for multiple inheritance). */
8352 static tree
8353 get_vfield_name (tree type)
8355 tree binfo, base_binfo;
8356 char *buf;
8358 for (binfo = TYPE_BINFO (type);
8359 BINFO_N_BASE_BINFOS (binfo);
8360 binfo = base_binfo)
8362 base_binfo = BINFO_BASE_BINFO (binfo, 0);
8364 if (BINFO_VIRTUAL_P (base_binfo)
8365 || !TYPE_CONTAINS_VPTR_P (BINFO_TYPE (base_binfo)))
8366 break;
8369 type = BINFO_TYPE (binfo);
8370 buf = (char *) alloca (sizeof (VFIELD_NAME_FORMAT)
8371 + TYPE_NAME_LENGTH (type) + 2);
8372 sprintf (buf, VFIELD_NAME_FORMAT,
8373 IDENTIFIER_POINTER (constructor_name (type)));
8374 return get_identifier (buf);
8377 void
8378 print_class_statistics (void)
8380 if (! GATHER_STATISTICS)
8381 return;
8383 fprintf (stderr, "convert_harshness = %d\n", n_convert_harshness);
8384 fprintf (stderr, "compute_conversion_costs = %d\n", n_compute_conversion_costs);
8385 if (n_vtables)
8387 fprintf (stderr, "vtables = %d; vtable searches = %d\n",
8388 n_vtables, n_vtable_searches);
8389 fprintf (stderr, "vtable entries = %d; vtable elems = %d\n",
8390 n_vtable_entries, n_vtable_elems);
8394 /* Build a dummy reference to ourselves so Derived::Base (and A::A) works,
8395 according to [class]:
8396 The class-name is also inserted
8397 into the scope of the class itself. For purposes of access checking,
8398 the inserted class name is treated as if it were a public member name. */
8400 void
8401 build_self_reference (void)
8403 tree name = constructor_name (current_class_type);
8404 tree value = build_lang_decl (TYPE_DECL, name, current_class_type);
8405 tree saved_cas;
8407 DECL_NONLOCAL (value) = 1;
8408 DECL_CONTEXT (value) = current_class_type;
8409 DECL_ARTIFICIAL (value) = 1;
8410 SET_DECL_SELF_REFERENCE_P (value);
8411 set_underlying_type (value);
8413 if (processing_template_decl)
8414 value = push_template_decl (value);
8416 saved_cas = current_access_specifier;
8417 current_access_specifier = access_public_node;
8418 finish_member_declaration (value);
8419 current_access_specifier = saved_cas;
8422 /* Returns 1 if TYPE contains only padding bytes. */
8425 is_empty_class (tree type)
8427 if (type == error_mark_node)
8428 return 0;
8430 if (! CLASS_TYPE_P (type))
8431 return 0;
8433 return CLASSTYPE_EMPTY_P (type);
8436 /* Returns true if TYPE contains no actual data, just various
8437 possible combinations of empty classes and possibly a vptr. */
8439 bool
8440 is_really_empty_class (tree type)
8442 if (CLASS_TYPE_P (type))
8444 tree field;
8445 tree binfo;
8446 tree base_binfo;
8447 int i;
8449 /* CLASSTYPE_EMPTY_P isn't set properly until the class is actually laid
8450 out, but we'd like to be able to check this before then. */
8451 if (COMPLETE_TYPE_P (type) && is_empty_class (type))
8452 return true;
8454 for (binfo = TYPE_BINFO (type), i = 0;
8455 BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
8456 if (!is_really_empty_class (BINFO_TYPE (base_binfo)))
8457 return false;
8458 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
8459 if (TREE_CODE (field) == FIELD_DECL
8460 && !DECL_ARTIFICIAL (field)
8461 /* An unnamed bit-field is not a data member. */
8462 && (DECL_NAME (field) || !DECL_C_BIT_FIELD (field))
8463 && !is_really_empty_class (TREE_TYPE (field)))
8464 return false;
8465 return true;
8467 else if (TREE_CODE (type) == ARRAY_TYPE)
8468 return (integer_zerop (array_type_nelts_top (type))
8469 || is_really_empty_class (TREE_TYPE (type)));
8470 return false;
8473 /* Note that NAME was looked up while the current class was being
8474 defined and that the result of that lookup was DECL. */
8476 void
8477 maybe_note_name_used_in_class (tree name, tree decl)
8479 splay_tree names_used;
8481 /* If we're not defining a class, there's nothing to do. */
8482 if (!(innermost_scope_kind() == sk_class
8483 && TYPE_BEING_DEFINED (current_class_type)
8484 && !LAMBDA_TYPE_P (current_class_type)))
8485 return;
8487 /* If there's already a binding for this NAME, then we don't have
8488 anything to worry about. */
8489 if (lookup_member (current_class_type, name,
8490 /*protect=*/0, /*want_type=*/false, tf_warning_or_error))
8491 return;
8493 if (!current_class_stack[current_class_depth - 1].names_used)
8494 current_class_stack[current_class_depth - 1].names_used
8495 = splay_tree_new (splay_tree_compare_pointers, 0, 0);
8496 names_used = current_class_stack[current_class_depth - 1].names_used;
8498 splay_tree_insert (names_used,
8499 (splay_tree_key) name,
8500 (splay_tree_value) decl);
8503 /* Note that NAME was declared (as DECL) in the current class. Check
8504 to see that the declaration is valid. */
8506 void
8507 note_name_declared_in_class (tree name, tree decl)
8509 splay_tree names_used;
8510 splay_tree_node n;
8512 /* Look to see if we ever used this name. */
8513 names_used
8514 = current_class_stack[current_class_depth - 1].names_used;
8515 if (!names_used)
8516 return;
8517 /* The C language allows members to be declared with a type of the same
8518 name, and the C++ standard says this diagnostic is not required. So
8519 allow it in extern "C" blocks unless predantic is specified.
8520 Allow it in all cases if -ms-extensions is specified. */
8521 if ((!pedantic && current_lang_name == lang_name_c)
8522 || flag_ms_extensions)
8523 return;
8524 n = splay_tree_lookup (names_used, (splay_tree_key) name);
8525 if (n)
8527 /* [basic.scope.class]
8529 A name N used in a class S shall refer to the same declaration
8530 in its context and when re-evaluated in the completed scope of
8531 S. */
8532 permerror (input_location, "declaration of %q#D", decl);
8533 permerror (location_of ((tree) n->value),
8534 "changes meaning of %qD from %q#D",
8535 DECL_NAME (OVL_CURRENT (decl)), (tree) n->value);
8539 /* Returns the VAR_DECL for the complete vtable associated with BINFO.
8540 Secondary vtables are merged with primary vtables; this function
8541 will return the VAR_DECL for the primary vtable. */
8543 tree
8544 get_vtbl_decl_for_binfo (tree binfo)
8546 tree decl;
8548 decl = BINFO_VTABLE (binfo);
8549 if (decl && TREE_CODE (decl) == POINTER_PLUS_EXPR)
8551 gcc_assert (TREE_CODE (TREE_OPERAND (decl, 0)) == ADDR_EXPR);
8552 decl = TREE_OPERAND (TREE_OPERAND (decl, 0), 0);
8554 if (decl)
8555 gcc_assert (VAR_P (decl));
8556 return decl;
8560 /* Returns the binfo for the primary base of BINFO. If the resulting
8561 BINFO is a virtual base, and it is inherited elsewhere in the
8562 hierarchy, then the returned binfo might not be the primary base of
8563 BINFO in the complete object. Check BINFO_PRIMARY_P or
8564 BINFO_LOST_PRIMARY_P to be sure. */
8566 static tree
8567 get_primary_binfo (tree binfo)
8569 tree primary_base;
8571 primary_base = CLASSTYPE_PRIMARY_BINFO (BINFO_TYPE (binfo));
8572 if (!primary_base)
8573 return NULL_TREE;
8575 return copied_binfo (primary_base, binfo);
8578 /* As above, but iterate until we reach the binfo that actually provides the
8579 vptr for BINFO. */
8581 static tree
8582 most_primary_binfo (tree binfo)
8584 tree b = binfo;
8585 while (CLASSTYPE_HAS_PRIMARY_BASE_P (BINFO_TYPE (b))
8586 && !BINFO_LOST_PRIMARY_P (b))
8588 tree primary_base = get_primary_binfo (b);
8589 gcc_assert (BINFO_PRIMARY_P (primary_base)
8590 && BINFO_INHERITANCE_CHAIN (primary_base) == b);
8591 b = primary_base;
8593 return b;
8596 /* Returns true if BINFO gets its vptr from a virtual base of the most derived
8597 type. Note that the virtual inheritance might be above or below BINFO in
8598 the hierarchy. */
8600 bool
8601 vptr_via_virtual_p (tree binfo)
8603 if (TYPE_P (binfo))
8604 binfo = TYPE_BINFO (binfo);
8605 tree primary = most_primary_binfo (binfo);
8606 /* Don't limit binfo_via_virtual, we want to return true when BINFO itself is
8607 a morally virtual base. */
8608 tree virt = binfo_via_virtual (primary, NULL_TREE);
8609 return virt != NULL_TREE;
8612 /* If INDENTED_P is zero, indent to INDENT. Return nonzero. */
8614 static int
8615 maybe_indent_hierarchy (FILE * stream, int indent, int indented_p)
8617 if (!indented_p)
8618 fprintf (stream, "%*s", indent, "");
8619 return 1;
8622 /* Dump the offsets of all the bases rooted at BINFO to STREAM.
8623 INDENT should be zero when called from the top level; it is
8624 incremented recursively. IGO indicates the next expected BINFO in
8625 inheritance graph ordering. */
8627 static tree
8628 dump_class_hierarchy_r (FILE *stream,
8629 int flags,
8630 tree binfo,
8631 tree igo,
8632 int indent)
8634 int indented = 0;
8635 tree base_binfo;
8636 int i;
8638 indented = maybe_indent_hierarchy (stream, indent, 0);
8639 fprintf (stream, "%s (0x" HOST_WIDE_INT_PRINT_HEX ") ",
8640 type_as_string (BINFO_TYPE (binfo), TFF_PLAIN_IDENTIFIER),
8641 (HOST_WIDE_INT) (uintptr_t) binfo);
8642 if (binfo != igo)
8644 fprintf (stream, "alternative-path\n");
8645 return igo;
8647 igo = TREE_CHAIN (binfo);
8649 fprintf (stream, HOST_WIDE_INT_PRINT_DEC,
8650 tree_to_shwi (BINFO_OFFSET (binfo)));
8651 if (is_empty_class (BINFO_TYPE (binfo)))
8652 fprintf (stream, " empty");
8653 else if (CLASSTYPE_NEARLY_EMPTY_P (BINFO_TYPE (binfo)))
8654 fprintf (stream, " nearly-empty");
8655 if (BINFO_VIRTUAL_P (binfo))
8656 fprintf (stream, " virtual");
8657 fprintf (stream, "\n");
8659 indented = 0;
8660 if (BINFO_PRIMARY_P (binfo))
8662 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
8663 fprintf (stream, " primary-for %s (0x" HOST_WIDE_INT_PRINT_HEX ")",
8664 type_as_string (BINFO_TYPE (BINFO_INHERITANCE_CHAIN (binfo)),
8665 TFF_PLAIN_IDENTIFIER),
8666 (HOST_WIDE_INT) (uintptr_t) BINFO_INHERITANCE_CHAIN (binfo));
8668 if (BINFO_LOST_PRIMARY_P (binfo))
8670 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
8671 fprintf (stream, " lost-primary");
8673 if (indented)
8674 fprintf (stream, "\n");
8676 if (!(flags & TDF_SLIM))
8678 int indented = 0;
8680 if (BINFO_SUBVTT_INDEX (binfo))
8682 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
8683 fprintf (stream, " subvttidx=%s",
8684 expr_as_string (BINFO_SUBVTT_INDEX (binfo),
8685 TFF_PLAIN_IDENTIFIER));
8687 if (BINFO_VPTR_INDEX (binfo))
8689 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
8690 fprintf (stream, " vptridx=%s",
8691 expr_as_string (BINFO_VPTR_INDEX (binfo),
8692 TFF_PLAIN_IDENTIFIER));
8694 if (BINFO_VPTR_FIELD (binfo))
8696 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
8697 fprintf (stream, " vbaseoffset=%s",
8698 expr_as_string (BINFO_VPTR_FIELD (binfo),
8699 TFF_PLAIN_IDENTIFIER));
8701 if (BINFO_VTABLE (binfo))
8703 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
8704 fprintf (stream, " vptr=%s",
8705 expr_as_string (BINFO_VTABLE (binfo),
8706 TFF_PLAIN_IDENTIFIER));
8709 if (indented)
8710 fprintf (stream, "\n");
8713 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
8714 igo = dump_class_hierarchy_r (stream, flags, base_binfo, igo, indent + 2);
8716 return igo;
8719 /* Dump the BINFO hierarchy for T. */
8721 static void
8722 dump_class_hierarchy_1 (FILE *stream, int flags, tree t)
8724 fprintf (stream, "Class %s\n", type_as_string (t, TFF_PLAIN_IDENTIFIER));
8725 fprintf (stream, " size=%lu align=%lu\n",
8726 (unsigned long)(tree_to_shwi (TYPE_SIZE (t)) / BITS_PER_UNIT),
8727 (unsigned long)(TYPE_ALIGN (t) / BITS_PER_UNIT));
8728 fprintf (stream, " base size=%lu base align=%lu\n",
8729 (unsigned long)(tree_to_shwi (TYPE_SIZE (CLASSTYPE_AS_BASE (t)))
8730 / BITS_PER_UNIT),
8731 (unsigned long)(TYPE_ALIGN (CLASSTYPE_AS_BASE (t))
8732 / BITS_PER_UNIT));
8733 dump_class_hierarchy_r (stream, flags, TYPE_BINFO (t), TYPE_BINFO (t), 0);
8734 fprintf (stream, "\n");
8737 /* Debug interface to hierarchy dumping. */
8739 void
8740 debug_class (tree t)
8742 dump_class_hierarchy_1 (stderr, TDF_SLIM, t);
8745 static void
8746 dump_class_hierarchy (tree t)
8748 int flags;
8749 FILE *stream = get_dump_info (TDI_class, &flags);
8751 if (stream)
8753 dump_class_hierarchy_1 (stream, flags, t);
8757 static void
8758 dump_array (FILE * stream, tree decl)
8760 tree value;
8761 unsigned HOST_WIDE_INT ix;
8762 HOST_WIDE_INT elt;
8763 tree size = TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (decl)));
8765 elt = (tree_to_shwi (TYPE_SIZE (TREE_TYPE (TREE_TYPE (decl))))
8766 / BITS_PER_UNIT);
8767 fprintf (stream, "%s:", decl_as_string (decl, TFF_PLAIN_IDENTIFIER));
8768 fprintf (stream, " %s entries",
8769 expr_as_string (size_binop (PLUS_EXPR, size, size_one_node),
8770 TFF_PLAIN_IDENTIFIER));
8771 fprintf (stream, "\n");
8773 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (DECL_INITIAL (decl)),
8774 ix, value)
8775 fprintf (stream, "%-4ld %s\n", (long)(ix * elt),
8776 expr_as_string (value, TFF_PLAIN_IDENTIFIER));
8779 static void
8780 dump_vtable (tree t, tree binfo, tree vtable)
8782 int flags;
8783 FILE *stream = get_dump_info (TDI_class, &flags);
8785 if (!stream)
8786 return;
8788 if (!(flags & TDF_SLIM))
8790 int ctor_vtbl_p = TYPE_BINFO (t) != binfo;
8792 fprintf (stream, "%s for %s",
8793 ctor_vtbl_p ? "Construction vtable" : "Vtable",
8794 type_as_string (BINFO_TYPE (binfo), TFF_PLAIN_IDENTIFIER));
8795 if (ctor_vtbl_p)
8797 if (!BINFO_VIRTUAL_P (binfo))
8798 fprintf (stream, " (0x" HOST_WIDE_INT_PRINT_HEX " instance)",
8799 (HOST_WIDE_INT) (uintptr_t) binfo);
8800 fprintf (stream, " in %s", type_as_string (t, TFF_PLAIN_IDENTIFIER));
8802 fprintf (stream, "\n");
8803 dump_array (stream, vtable);
8804 fprintf (stream, "\n");
8808 static void
8809 dump_vtt (tree t, tree vtt)
8811 int flags;
8812 FILE *stream = get_dump_info (TDI_class, &flags);
8814 if (!stream)
8815 return;
8817 if (!(flags & TDF_SLIM))
8819 fprintf (stream, "VTT for %s\n",
8820 type_as_string (t, TFF_PLAIN_IDENTIFIER));
8821 dump_array (stream, vtt);
8822 fprintf (stream, "\n");
8826 /* Dump a function or thunk and its thunkees. */
8828 static void
8829 dump_thunk (FILE *stream, int indent, tree thunk)
8831 static const char spaces[] = " ";
8832 tree name = DECL_NAME (thunk);
8833 tree thunks;
8835 fprintf (stream, "%.*s%p %s %s", indent, spaces,
8836 (void *)thunk,
8837 !DECL_THUNK_P (thunk) ? "function"
8838 : DECL_THIS_THUNK_P (thunk) ? "this-thunk" : "covariant-thunk",
8839 name ? IDENTIFIER_POINTER (name) : "<unset>");
8840 if (DECL_THUNK_P (thunk))
8842 HOST_WIDE_INT fixed_adjust = THUNK_FIXED_OFFSET (thunk);
8843 tree virtual_adjust = THUNK_VIRTUAL_OFFSET (thunk);
8845 fprintf (stream, " fixed=" HOST_WIDE_INT_PRINT_DEC, fixed_adjust);
8846 if (!virtual_adjust)
8847 /*NOP*/;
8848 else if (DECL_THIS_THUNK_P (thunk))
8849 fprintf (stream, " vcall=" HOST_WIDE_INT_PRINT_DEC,
8850 tree_to_shwi (virtual_adjust));
8851 else
8852 fprintf (stream, " vbase=" HOST_WIDE_INT_PRINT_DEC "(%s)",
8853 tree_to_shwi (BINFO_VPTR_FIELD (virtual_adjust)),
8854 type_as_string (BINFO_TYPE (virtual_adjust), TFF_SCOPE));
8855 if (THUNK_ALIAS (thunk))
8856 fprintf (stream, " alias to %p", (void *)THUNK_ALIAS (thunk));
8858 fprintf (stream, "\n");
8859 for (thunks = DECL_THUNKS (thunk); thunks; thunks = TREE_CHAIN (thunks))
8860 dump_thunk (stream, indent + 2, thunks);
8863 /* Dump the thunks for FN. */
8865 void
8866 debug_thunks (tree fn)
8868 dump_thunk (stderr, 0, fn);
8871 /* Virtual function table initialization. */
8873 /* Create all the necessary vtables for T and its base classes. */
8875 static void
8876 finish_vtbls (tree t)
8878 tree vbase;
8879 vec<constructor_elt, va_gc> *v = NULL;
8880 tree vtable = BINFO_VTABLE (TYPE_BINFO (t));
8882 /* We lay out the primary and secondary vtables in one contiguous
8883 vtable. The primary vtable is first, followed by the non-virtual
8884 secondary vtables in inheritance graph order. */
8885 accumulate_vtbl_inits (TYPE_BINFO (t), TYPE_BINFO (t), TYPE_BINFO (t),
8886 vtable, t, &v);
8888 /* Then come the virtual bases, also in inheritance graph order. */
8889 for (vbase = TYPE_BINFO (t); vbase; vbase = TREE_CHAIN (vbase))
8891 if (!BINFO_VIRTUAL_P (vbase))
8892 continue;
8893 accumulate_vtbl_inits (vbase, vbase, TYPE_BINFO (t), vtable, t, &v);
8896 if (BINFO_VTABLE (TYPE_BINFO (t)))
8897 initialize_vtable (TYPE_BINFO (t), v);
8900 /* Initialize the vtable for BINFO with the INITS. */
8902 static void
8903 initialize_vtable (tree binfo, vec<constructor_elt, va_gc> *inits)
8905 tree decl;
8907 layout_vtable_decl (binfo, vec_safe_length (inits));
8908 decl = get_vtbl_decl_for_binfo (binfo);
8909 initialize_artificial_var (decl, inits);
8910 dump_vtable (BINFO_TYPE (binfo), binfo, decl);
8913 /* Build the VTT (virtual table table) for T.
8914 A class requires a VTT if it has virtual bases.
8916 This holds
8917 1 - primary virtual pointer for complete object T
8918 2 - secondary VTTs for each direct non-virtual base of T which requires a
8920 3 - secondary virtual pointers for each direct or indirect base of T which
8921 has virtual bases or is reachable via a virtual path from T.
8922 4 - secondary VTTs for each direct or indirect virtual base of T.
8924 Secondary VTTs look like complete object VTTs without part 4. */
8926 static void
8927 build_vtt (tree t)
8929 tree type;
8930 tree vtt;
8931 tree index;
8932 vec<constructor_elt, va_gc> *inits;
8934 /* Build up the initializers for the VTT. */
8935 inits = NULL;
8936 index = size_zero_node;
8937 build_vtt_inits (TYPE_BINFO (t), t, &inits, &index);
8939 /* If we didn't need a VTT, we're done. */
8940 if (!inits)
8941 return;
8943 /* Figure out the type of the VTT. */
8944 type = build_array_of_n_type (const_ptr_type_node,
8945 inits->length ());
8947 /* Now, build the VTT object itself. */
8948 vtt = build_vtable (t, mangle_vtt_for_type (t), type);
8949 initialize_artificial_var (vtt, inits);
8950 /* Add the VTT to the vtables list. */
8951 DECL_CHAIN (vtt) = DECL_CHAIN (CLASSTYPE_VTABLES (t));
8952 DECL_CHAIN (CLASSTYPE_VTABLES (t)) = vtt;
8954 dump_vtt (t, vtt);
8957 /* When building a secondary VTT, BINFO_VTABLE is set to a TREE_LIST with
8958 PURPOSE the RTTI_BINFO, VALUE the real vtable pointer for this binfo,
8959 and CHAIN the vtable pointer for this binfo after construction is
8960 complete. VALUE can also be another BINFO, in which case we recurse. */
8962 static tree
8963 binfo_ctor_vtable (tree binfo)
8965 tree vt;
8967 while (1)
8969 vt = BINFO_VTABLE (binfo);
8970 if (TREE_CODE (vt) == TREE_LIST)
8971 vt = TREE_VALUE (vt);
8972 if (TREE_CODE (vt) == TREE_BINFO)
8973 binfo = vt;
8974 else
8975 break;
8978 return vt;
8981 /* Data for secondary VTT initialization. */
8982 struct secondary_vptr_vtt_init_data
8984 /* Is this the primary VTT? */
8985 bool top_level_p;
8987 /* Current index into the VTT. */
8988 tree index;
8990 /* Vector of initializers built up. */
8991 vec<constructor_elt, va_gc> *inits;
8993 /* The type being constructed by this secondary VTT. */
8994 tree type_being_constructed;
8997 /* Recursively build the VTT-initializer for BINFO (which is in the
8998 hierarchy dominated by T). INITS points to the end of the initializer
8999 list to date. INDEX is the VTT index where the next element will be
9000 replaced. Iff BINFO is the binfo for T, this is the top level VTT (i.e.
9001 not a subvtt for some base of T). When that is so, we emit the sub-VTTs
9002 for virtual bases of T. When it is not so, we build the constructor
9003 vtables for the BINFO-in-T variant. */
9005 static void
9006 build_vtt_inits (tree binfo, tree t, vec<constructor_elt, va_gc> **inits,
9007 tree *index)
9009 int i;
9010 tree b;
9011 tree init;
9012 secondary_vptr_vtt_init_data data;
9013 int top_level_p = SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), t);
9015 /* We only need VTTs for subobjects with virtual bases. */
9016 if (!CLASSTYPE_VBASECLASSES (BINFO_TYPE (binfo)))
9017 return;
9019 /* We need to use a construction vtable if this is not the primary
9020 VTT. */
9021 if (!top_level_p)
9023 build_ctor_vtbl_group (binfo, t);
9025 /* Record the offset in the VTT where this sub-VTT can be found. */
9026 BINFO_SUBVTT_INDEX (binfo) = *index;
9029 /* Add the address of the primary vtable for the complete object. */
9030 init = binfo_ctor_vtable (binfo);
9031 CONSTRUCTOR_APPEND_ELT (*inits, NULL_TREE, init);
9032 if (top_level_p)
9034 gcc_assert (!BINFO_VPTR_INDEX (binfo));
9035 BINFO_VPTR_INDEX (binfo) = *index;
9037 *index = size_binop (PLUS_EXPR, *index, TYPE_SIZE_UNIT (ptr_type_node));
9039 /* Recursively add the secondary VTTs for non-virtual bases. */
9040 for (i = 0; BINFO_BASE_ITERATE (binfo, i, b); ++i)
9041 if (!BINFO_VIRTUAL_P (b))
9042 build_vtt_inits (b, t, inits, index);
9044 /* Add secondary virtual pointers for all subobjects of BINFO with
9045 either virtual bases or reachable along a virtual path, except
9046 subobjects that are non-virtual primary bases. */
9047 data.top_level_p = top_level_p;
9048 data.index = *index;
9049 data.inits = *inits;
9050 data.type_being_constructed = BINFO_TYPE (binfo);
9052 dfs_walk_once (binfo, dfs_build_secondary_vptr_vtt_inits, NULL, &data);
9054 *index = data.index;
9056 /* data.inits might have grown as we added secondary virtual pointers.
9057 Make sure our caller knows about the new vector. */
9058 *inits = data.inits;
9060 if (top_level_p)
9061 /* Add the secondary VTTs for virtual bases in inheritance graph
9062 order. */
9063 for (b = TYPE_BINFO (BINFO_TYPE (binfo)); b; b = TREE_CHAIN (b))
9065 if (!BINFO_VIRTUAL_P (b))
9066 continue;
9068 build_vtt_inits (b, t, inits, index);
9070 else
9071 /* Remove the ctor vtables we created. */
9072 dfs_walk_all (binfo, dfs_fixup_binfo_vtbls, NULL, binfo);
9075 /* Called from build_vtt_inits via dfs_walk. BINFO is the binfo for the base
9076 in most derived. DATA is a SECONDARY_VPTR_VTT_INIT_DATA structure. */
9078 static tree
9079 dfs_build_secondary_vptr_vtt_inits (tree binfo, void *data_)
9081 secondary_vptr_vtt_init_data *data = (secondary_vptr_vtt_init_data *)data_;
9083 /* We don't care about bases that don't have vtables. */
9084 if (!TYPE_VFIELD (BINFO_TYPE (binfo)))
9085 return dfs_skip_bases;
9087 /* We're only interested in proper subobjects of the type being
9088 constructed. */
9089 if (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), data->type_being_constructed))
9090 return NULL_TREE;
9092 /* We're only interested in bases with virtual bases or reachable
9093 via a virtual path from the type being constructed. */
9094 if (!(CLASSTYPE_VBASECLASSES (BINFO_TYPE (binfo))
9095 || binfo_via_virtual (binfo, data->type_being_constructed)))
9096 return dfs_skip_bases;
9098 /* We're not interested in non-virtual primary bases. */
9099 if (!BINFO_VIRTUAL_P (binfo) && BINFO_PRIMARY_P (binfo))
9100 return NULL_TREE;
9102 /* Record the index where this secondary vptr can be found. */
9103 if (data->top_level_p)
9105 gcc_assert (!BINFO_VPTR_INDEX (binfo));
9106 BINFO_VPTR_INDEX (binfo) = data->index;
9108 if (BINFO_VIRTUAL_P (binfo))
9110 /* It's a primary virtual base, and this is not a
9111 construction vtable. Find the base this is primary of in
9112 the inheritance graph, and use that base's vtable
9113 now. */
9114 while (BINFO_PRIMARY_P (binfo))
9115 binfo = BINFO_INHERITANCE_CHAIN (binfo);
9119 /* Add the initializer for the secondary vptr itself. */
9120 CONSTRUCTOR_APPEND_ELT (data->inits, NULL_TREE, binfo_ctor_vtable (binfo));
9122 /* Advance the vtt index. */
9123 data->index = size_binop (PLUS_EXPR, data->index,
9124 TYPE_SIZE_UNIT (ptr_type_node));
9126 return NULL_TREE;
9129 /* Called from build_vtt_inits via dfs_walk. After building
9130 constructor vtables and generating the sub-vtt from them, we need
9131 to restore the BINFO_VTABLES that were scribbled on. DATA is the
9132 binfo of the base whose sub vtt was generated. */
9134 static tree
9135 dfs_fixup_binfo_vtbls (tree binfo, void* data)
9137 tree vtable = BINFO_VTABLE (binfo);
9139 if (!TYPE_CONTAINS_VPTR_P (BINFO_TYPE (binfo)))
9140 /* If this class has no vtable, none of its bases do. */
9141 return dfs_skip_bases;
9143 if (!vtable)
9144 /* This might be a primary base, so have no vtable in this
9145 hierarchy. */
9146 return NULL_TREE;
9148 /* If we scribbled the construction vtable vptr into BINFO, clear it
9149 out now. */
9150 if (TREE_CODE (vtable) == TREE_LIST
9151 && (TREE_PURPOSE (vtable) == (tree) data))
9152 BINFO_VTABLE (binfo) = TREE_CHAIN (vtable);
9154 return NULL_TREE;
9157 /* Build the construction vtable group for BINFO which is in the
9158 hierarchy dominated by T. */
9160 static void
9161 build_ctor_vtbl_group (tree binfo, tree t)
9163 tree type;
9164 tree vtbl;
9165 tree id;
9166 tree vbase;
9167 vec<constructor_elt, va_gc> *v;
9169 /* See if we've already created this construction vtable group. */
9170 id = mangle_ctor_vtbl_for_type (t, binfo);
9171 if (IDENTIFIER_GLOBAL_VALUE (id))
9172 return;
9174 gcc_assert (!SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), t));
9175 /* Build a version of VTBL (with the wrong type) for use in
9176 constructing the addresses of secondary vtables in the
9177 construction vtable group. */
9178 vtbl = build_vtable (t, id, ptr_type_node);
9179 DECL_CONSTRUCTION_VTABLE_P (vtbl) = 1;
9180 /* Don't export construction vtables from shared libraries. Even on
9181 targets that don't support hidden visibility, this tells
9182 can_refer_decl_in_current_unit_p not to assume that it's safe to
9183 access from a different compilation unit (bz 54314). */
9184 DECL_VISIBILITY (vtbl) = VISIBILITY_HIDDEN;
9185 DECL_VISIBILITY_SPECIFIED (vtbl) = true;
9187 v = NULL;
9188 accumulate_vtbl_inits (binfo, TYPE_BINFO (TREE_TYPE (binfo)),
9189 binfo, vtbl, t, &v);
9191 /* Add the vtables for each of our virtual bases using the vbase in T
9192 binfo. */
9193 for (vbase = TYPE_BINFO (BINFO_TYPE (binfo));
9194 vbase;
9195 vbase = TREE_CHAIN (vbase))
9197 tree b;
9199 if (!BINFO_VIRTUAL_P (vbase))
9200 continue;
9201 b = copied_binfo (vbase, binfo);
9203 accumulate_vtbl_inits (b, vbase, binfo, vtbl, t, &v);
9206 /* Figure out the type of the construction vtable. */
9207 type = build_array_of_n_type (vtable_entry_type, v->length ());
9208 layout_type (type);
9209 TREE_TYPE (vtbl) = type;
9210 DECL_SIZE (vtbl) = DECL_SIZE_UNIT (vtbl) = NULL_TREE;
9211 layout_decl (vtbl, 0);
9213 /* Initialize the construction vtable. */
9214 CLASSTYPE_VTABLES (t) = chainon (CLASSTYPE_VTABLES (t), vtbl);
9215 initialize_artificial_var (vtbl, v);
9216 dump_vtable (t, binfo, vtbl);
9219 /* Add the vtbl initializers for BINFO (and its bases other than
9220 non-virtual primaries) to the list of INITS. BINFO is in the
9221 hierarchy dominated by T. RTTI_BINFO is the binfo within T of
9222 the constructor the vtbl inits should be accumulated for. (If this
9223 is the complete object vtbl then RTTI_BINFO will be TYPE_BINFO (T).)
9224 ORIG_BINFO is the binfo for this object within BINFO_TYPE (RTTI_BINFO).
9225 BINFO is the active base equivalent of ORIG_BINFO in the inheritance
9226 graph of T. Both BINFO and ORIG_BINFO will have the same BINFO_TYPE,
9227 but are not necessarily the same in terms of layout. */
9229 static void
9230 accumulate_vtbl_inits (tree binfo,
9231 tree orig_binfo,
9232 tree rtti_binfo,
9233 tree vtbl,
9234 tree t,
9235 vec<constructor_elt, va_gc> **inits)
9237 int i;
9238 tree base_binfo;
9239 int ctor_vtbl_p = !SAME_BINFO_TYPE_P (BINFO_TYPE (rtti_binfo), t);
9241 gcc_assert (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), BINFO_TYPE (orig_binfo)));
9243 /* If it doesn't have a vptr, we don't do anything. */
9244 if (!TYPE_CONTAINS_VPTR_P (BINFO_TYPE (binfo)))
9245 return;
9247 /* If we're building a construction vtable, we're not interested in
9248 subobjects that don't require construction vtables. */
9249 if (ctor_vtbl_p
9250 && !CLASSTYPE_VBASECLASSES (BINFO_TYPE (binfo))
9251 && !binfo_via_virtual (orig_binfo, BINFO_TYPE (rtti_binfo)))
9252 return;
9254 /* Build the initializers for the BINFO-in-T vtable. */
9255 dfs_accumulate_vtbl_inits (binfo, orig_binfo, rtti_binfo, vtbl, t, inits);
9257 /* Walk the BINFO and its bases. We walk in preorder so that as we
9258 initialize each vtable we can figure out at what offset the
9259 secondary vtable lies from the primary vtable. We can't use
9260 dfs_walk here because we need to iterate through bases of BINFO
9261 and RTTI_BINFO simultaneously. */
9262 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
9264 /* Skip virtual bases. */
9265 if (BINFO_VIRTUAL_P (base_binfo))
9266 continue;
9267 accumulate_vtbl_inits (base_binfo,
9268 BINFO_BASE_BINFO (orig_binfo, i),
9269 rtti_binfo, vtbl, t,
9270 inits);
9274 /* Called from accumulate_vtbl_inits. Adds the initializers for the
9275 BINFO vtable to L. */
9277 static void
9278 dfs_accumulate_vtbl_inits (tree binfo,
9279 tree orig_binfo,
9280 tree rtti_binfo,
9281 tree orig_vtbl,
9282 tree t,
9283 vec<constructor_elt, va_gc> **l)
9285 tree vtbl = NULL_TREE;
9286 int ctor_vtbl_p = !SAME_BINFO_TYPE_P (BINFO_TYPE (rtti_binfo), t);
9287 int n_inits;
9289 if (ctor_vtbl_p
9290 && BINFO_VIRTUAL_P (orig_binfo) && BINFO_PRIMARY_P (orig_binfo))
9292 /* In the hierarchy of BINFO_TYPE (RTTI_BINFO), this is a
9293 primary virtual base. If it is not the same primary in
9294 the hierarchy of T, we'll need to generate a ctor vtable
9295 for it, to place at its location in T. If it is the same
9296 primary, we still need a VTT entry for the vtable, but it
9297 should point to the ctor vtable for the base it is a
9298 primary for within the sub-hierarchy of RTTI_BINFO.
9300 There are three possible cases:
9302 1) We are in the same place.
9303 2) We are a primary base within a lost primary virtual base of
9304 RTTI_BINFO.
9305 3) We are primary to something not a base of RTTI_BINFO. */
9307 tree b;
9308 tree last = NULL_TREE;
9310 /* First, look through the bases we are primary to for RTTI_BINFO
9311 or a virtual base. */
9312 b = binfo;
9313 while (BINFO_PRIMARY_P (b))
9315 b = BINFO_INHERITANCE_CHAIN (b);
9316 last = b;
9317 if (BINFO_VIRTUAL_P (b) || b == rtti_binfo)
9318 goto found;
9320 /* If we run out of primary links, keep looking down our
9321 inheritance chain; we might be an indirect primary. */
9322 for (b = last; b; b = BINFO_INHERITANCE_CHAIN (b))
9323 if (BINFO_VIRTUAL_P (b) || b == rtti_binfo)
9324 break;
9325 found:
9327 /* If we found RTTI_BINFO, this is case 1. If we found a virtual
9328 base B and it is a base of RTTI_BINFO, this is case 2. In
9329 either case, we share our vtable with LAST, i.e. the
9330 derived-most base within B of which we are a primary. */
9331 if (b == rtti_binfo
9332 || (b && binfo_for_vbase (BINFO_TYPE (b), BINFO_TYPE (rtti_binfo))))
9333 /* Just set our BINFO_VTABLE to point to LAST, as we may not have
9334 set LAST's BINFO_VTABLE yet. We'll extract the actual vptr in
9335 binfo_ctor_vtable after everything's been set up. */
9336 vtbl = last;
9338 /* Otherwise, this is case 3 and we get our own. */
9340 else if (!BINFO_NEW_VTABLE_MARKED (orig_binfo))
9341 return;
9343 n_inits = vec_safe_length (*l);
9345 if (!vtbl)
9347 tree index;
9348 int non_fn_entries;
9350 /* Add the initializer for this vtable. */
9351 build_vtbl_initializer (binfo, orig_binfo, t, rtti_binfo,
9352 &non_fn_entries, l);
9354 /* Figure out the position to which the VPTR should point. */
9355 vtbl = build1 (ADDR_EXPR, vtbl_ptr_type_node, orig_vtbl);
9356 index = size_binop (MULT_EXPR,
9357 TYPE_SIZE_UNIT (vtable_entry_type),
9358 size_int (non_fn_entries + n_inits));
9359 vtbl = fold_build_pointer_plus (vtbl, index);
9362 if (ctor_vtbl_p)
9363 /* For a construction vtable, we can't overwrite BINFO_VTABLE.
9364 So, we make a TREE_LIST. Later, dfs_fixup_binfo_vtbls will
9365 straighten this out. */
9366 BINFO_VTABLE (binfo) = tree_cons (rtti_binfo, vtbl, BINFO_VTABLE (binfo));
9367 else if (BINFO_PRIMARY_P (binfo) && BINFO_VIRTUAL_P (binfo))
9368 /* Throw away any unneeded intializers. */
9369 (*l)->truncate (n_inits);
9370 else
9371 /* For an ordinary vtable, set BINFO_VTABLE. */
9372 BINFO_VTABLE (binfo) = vtbl;
9375 static GTY(()) tree abort_fndecl_addr;
9377 /* Construct the initializer for BINFO's virtual function table. BINFO
9378 is part of the hierarchy dominated by T. If we're building a
9379 construction vtable, the ORIG_BINFO is the binfo we should use to
9380 find the actual function pointers to put in the vtable - but they
9381 can be overridden on the path to most-derived in the graph that
9382 ORIG_BINFO belongs. Otherwise,
9383 ORIG_BINFO should be the same as BINFO. The RTTI_BINFO is the
9384 BINFO that should be indicated by the RTTI information in the
9385 vtable; it will be a base class of T, rather than T itself, if we
9386 are building a construction vtable.
9388 The value returned is a TREE_LIST suitable for wrapping in a
9389 CONSTRUCTOR to use as the DECL_INITIAL for a vtable. If
9390 NON_FN_ENTRIES_P is not NULL, *NON_FN_ENTRIES_P is set to the
9391 number of non-function entries in the vtable.
9393 It might seem that this function should never be called with a
9394 BINFO for which BINFO_PRIMARY_P holds, the vtable for such a
9395 base is always subsumed by a derived class vtable. However, when
9396 we are building construction vtables, we do build vtables for
9397 primary bases; we need these while the primary base is being
9398 constructed. */
9400 static void
9401 build_vtbl_initializer (tree binfo,
9402 tree orig_binfo,
9403 tree t,
9404 tree rtti_binfo,
9405 int* non_fn_entries_p,
9406 vec<constructor_elt, va_gc> **inits)
9408 tree v;
9409 vtbl_init_data vid;
9410 unsigned ix, jx;
9411 tree vbinfo;
9412 vec<tree, va_gc> *vbases;
9413 constructor_elt *e;
9415 /* Initialize VID. */
9416 memset (&vid, 0, sizeof (vid));
9417 vid.binfo = binfo;
9418 vid.derived = t;
9419 vid.rtti_binfo = rtti_binfo;
9420 vid.primary_vtbl_p = SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), t);
9421 vid.ctor_vtbl_p = !SAME_BINFO_TYPE_P (BINFO_TYPE (rtti_binfo), t);
9422 vid.generate_vcall_entries = true;
9423 /* The first vbase or vcall offset is at index -3 in the vtable. */
9424 vid.index = ssize_int(-3 * TARGET_VTABLE_DATA_ENTRY_DISTANCE);
9426 /* Add entries to the vtable for RTTI. */
9427 build_rtti_vtbl_entries (binfo, &vid);
9429 /* Create an array for keeping track of the functions we've
9430 processed. When we see multiple functions with the same
9431 signature, we share the vcall offsets. */
9432 vec_alloc (vid.fns, 32);
9433 /* Add the vcall and vbase offset entries. */
9434 build_vcall_and_vbase_vtbl_entries (binfo, &vid);
9436 /* Clear BINFO_VTABLE_PATH_MARKED; it's set by
9437 build_vbase_offset_vtbl_entries. */
9438 for (vbases = CLASSTYPE_VBASECLASSES (t), ix = 0;
9439 vec_safe_iterate (vbases, ix, &vbinfo); ix++)
9440 BINFO_VTABLE_PATH_MARKED (vbinfo) = 0;
9442 /* If the target requires padding between data entries, add that now. */
9443 if (TARGET_VTABLE_DATA_ENTRY_DISTANCE > 1)
9445 int n_entries = vec_safe_length (vid.inits);
9447 vec_safe_grow (vid.inits, TARGET_VTABLE_DATA_ENTRY_DISTANCE * n_entries);
9449 /* Move data entries into their new positions and add padding
9450 after the new positions. Iterate backwards so we don't
9451 overwrite entries that we would need to process later. */
9452 for (ix = n_entries - 1;
9453 vid.inits->iterate (ix, &e);
9454 ix--)
9456 int j;
9457 int new_position = (TARGET_VTABLE_DATA_ENTRY_DISTANCE * ix
9458 + (TARGET_VTABLE_DATA_ENTRY_DISTANCE - 1));
9460 (*vid.inits)[new_position] = *e;
9462 for (j = 1; j < TARGET_VTABLE_DATA_ENTRY_DISTANCE; ++j)
9464 constructor_elt *f = &(*vid.inits)[new_position - j];
9465 f->index = NULL_TREE;
9466 f->value = build1 (NOP_EXPR, vtable_entry_type,
9467 null_pointer_node);
9472 if (non_fn_entries_p)
9473 *non_fn_entries_p = vec_safe_length (vid.inits);
9475 /* The initializers for virtual functions were built up in reverse
9476 order. Straighten them out and add them to the running list in one
9477 step. */
9478 jx = vec_safe_length (*inits);
9479 vec_safe_grow (*inits, jx + vid.inits->length ());
9481 for (ix = vid.inits->length () - 1;
9482 vid.inits->iterate (ix, &e);
9483 ix--, jx++)
9484 (**inits)[jx] = *e;
9486 /* Go through all the ordinary virtual functions, building up
9487 initializers. */
9488 for (v = BINFO_VIRTUALS (orig_binfo); v; v = TREE_CHAIN (v))
9490 tree delta;
9491 tree vcall_index;
9492 tree fn, fn_original;
9493 tree init = NULL_TREE;
9495 fn = BV_FN (v);
9496 fn_original = fn;
9497 if (DECL_THUNK_P (fn))
9499 if (!DECL_NAME (fn))
9500 finish_thunk (fn);
9501 if (THUNK_ALIAS (fn))
9503 fn = THUNK_ALIAS (fn);
9504 BV_FN (v) = fn;
9506 fn_original = THUNK_TARGET (fn);
9509 /* If the only definition of this function signature along our
9510 primary base chain is from a lost primary, this vtable slot will
9511 never be used, so just zero it out. This is important to avoid
9512 requiring extra thunks which cannot be generated with the function.
9514 We first check this in update_vtable_entry_for_fn, so we handle
9515 restored primary bases properly; we also need to do it here so we
9516 zero out unused slots in ctor vtables, rather than filling them
9517 with erroneous values (though harmless, apart from relocation
9518 costs). */
9519 if (BV_LOST_PRIMARY (v))
9520 init = size_zero_node;
9522 if (! init)
9524 /* Pull the offset for `this', and the function to call, out of
9525 the list. */
9526 delta = BV_DELTA (v);
9527 vcall_index = BV_VCALL_INDEX (v);
9529 gcc_assert (TREE_CODE (delta) == INTEGER_CST);
9530 gcc_assert (TREE_CODE (fn) == FUNCTION_DECL);
9532 /* You can't call an abstract virtual function; it's abstract.
9533 So, we replace these functions with __pure_virtual. */
9534 if (DECL_PURE_VIRTUAL_P (fn_original))
9536 fn = abort_fndecl;
9537 if (!TARGET_VTABLE_USES_DESCRIPTORS)
9539 if (abort_fndecl_addr == NULL)
9540 abort_fndecl_addr
9541 = fold_convert (vfunc_ptr_type_node,
9542 build_fold_addr_expr (fn));
9543 init = abort_fndecl_addr;
9546 /* Likewise for deleted virtuals. */
9547 else if (DECL_DELETED_FN (fn_original))
9549 fn = get_identifier ("__cxa_deleted_virtual");
9550 if (!get_global_value_if_present (fn, &fn))
9551 fn = push_library_fn (fn, (build_function_type_list
9552 (void_type_node, NULL_TREE)),
9553 NULL_TREE, ECF_NORETURN);
9554 if (!TARGET_VTABLE_USES_DESCRIPTORS)
9555 init = fold_convert (vfunc_ptr_type_node,
9556 build_fold_addr_expr (fn));
9558 else
9560 if (!integer_zerop (delta) || vcall_index)
9562 fn = make_thunk (fn, /*this_adjusting=*/1, delta, vcall_index);
9563 if (!DECL_NAME (fn))
9564 finish_thunk (fn);
9566 /* Take the address of the function, considering it to be of an
9567 appropriate generic type. */
9568 if (!TARGET_VTABLE_USES_DESCRIPTORS)
9569 init = fold_convert (vfunc_ptr_type_node,
9570 build_fold_addr_expr (fn));
9571 /* Don't refer to a virtual destructor from a constructor
9572 vtable or a vtable for an abstract class, since destroying
9573 an object under construction is undefined behavior and we
9574 don't want it to be considered a candidate for speculative
9575 devirtualization. But do create the thunk for ABI
9576 compliance. */
9577 if (DECL_DESTRUCTOR_P (fn_original)
9578 && (CLASSTYPE_PURE_VIRTUALS (DECL_CONTEXT (fn_original))
9579 || orig_binfo != binfo))
9580 init = size_zero_node;
9584 /* And add it to the chain of initializers. */
9585 if (TARGET_VTABLE_USES_DESCRIPTORS)
9587 int i;
9588 if (init == size_zero_node)
9589 for (i = 0; i < TARGET_VTABLE_USES_DESCRIPTORS; ++i)
9590 CONSTRUCTOR_APPEND_ELT (*inits, NULL_TREE, init);
9591 else
9592 for (i = 0; i < TARGET_VTABLE_USES_DESCRIPTORS; ++i)
9594 tree fdesc = build2 (FDESC_EXPR, vfunc_ptr_type_node,
9595 fn, build_int_cst (NULL_TREE, i));
9596 TREE_CONSTANT (fdesc) = 1;
9598 CONSTRUCTOR_APPEND_ELT (*inits, NULL_TREE, fdesc);
9601 else
9602 CONSTRUCTOR_APPEND_ELT (*inits, NULL_TREE, init);
9606 /* Adds to vid->inits the initializers for the vbase and vcall
9607 offsets in BINFO, which is in the hierarchy dominated by T. */
9609 static void
9610 build_vcall_and_vbase_vtbl_entries (tree binfo, vtbl_init_data* vid)
9612 tree b;
9614 /* If this is a derived class, we must first create entries
9615 corresponding to the primary base class. */
9616 b = get_primary_binfo (binfo);
9617 if (b)
9618 build_vcall_and_vbase_vtbl_entries (b, vid);
9620 /* Add the vbase entries for this base. */
9621 build_vbase_offset_vtbl_entries (binfo, vid);
9622 /* Add the vcall entries for this base. */
9623 build_vcall_offset_vtbl_entries (binfo, vid);
9626 /* Returns the initializers for the vbase offset entries in the vtable
9627 for BINFO (which is part of the class hierarchy dominated by T), in
9628 reverse order. VBASE_OFFSET_INDEX gives the vtable index
9629 where the next vbase offset will go. */
9631 static void
9632 build_vbase_offset_vtbl_entries (tree binfo, vtbl_init_data* vid)
9634 tree vbase;
9635 tree t;
9636 tree non_primary_binfo;
9638 /* If there are no virtual baseclasses, then there is nothing to
9639 do. */
9640 if (!CLASSTYPE_VBASECLASSES (BINFO_TYPE (binfo)))
9641 return;
9643 t = vid->derived;
9645 /* We might be a primary base class. Go up the inheritance hierarchy
9646 until we find the most derived class of which we are a primary base:
9647 it is the offset of that which we need to use. */
9648 non_primary_binfo = binfo;
9649 while (BINFO_INHERITANCE_CHAIN (non_primary_binfo))
9651 tree b;
9653 /* If we have reached a virtual base, then it must be a primary
9654 base (possibly multi-level) of vid->binfo, or we wouldn't
9655 have called build_vcall_and_vbase_vtbl_entries for it. But it
9656 might be a lost primary, so just skip down to vid->binfo. */
9657 if (BINFO_VIRTUAL_P (non_primary_binfo))
9659 non_primary_binfo = vid->binfo;
9660 break;
9663 b = BINFO_INHERITANCE_CHAIN (non_primary_binfo);
9664 if (get_primary_binfo (b) != non_primary_binfo)
9665 break;
9666 non_primary_binfo = b;
9669 /* Go through the virtual bases, adding the offsets. */
9670 for (vbase = TYPE_BINFO (BINFO_TYPE (binfo));
9671 vbase;
9672 vbase = TREE_CHAIN (vbase))
9674 tree b;
9675 tree delta;
9677 if (!BINFO_VIRTUAL_P (vbase))
9678 continue;
9680 /* Find the instance of this virtual base in the complete
9681 object. */
9682 b = copied_binfo (vbase, binfo);
9684 /* If we've already got an offset for this virtual base, we
9685 don't need another one. */
9686 if (BINFO_VTABLE_PATH_MARKED (b))
9687 continue;
9688 BINFO_VTABLE_PATH_MARKED (b) = 1;
9690 /* Figure out where we can find this vbase offset. */
9691 delta = size_binop (MULT_EXPR,
9692 vid->index,
9693 fold_convert (ssizetype,
9694 TYPE_SIZE_UNIT (vtable_entry_type)));
9695 if (vid->primary_vtbl_p)
9696 BINFO_VPTR_FIELD (b) = delta;
9698 if (binfo != TYPE_BINFO (t))
9699 /* The vbase offset had better be the same. */
9700 gcc_assert (tree_int_cst_equal (delta, BINFO_VPTR_FIELD (vbase)));
9702 /* The next vbase will come at a more negative offset. */
9703 vid->index = size_binop (MINUS_EXPR, vid->index,
9704 ssize_int (TARGET_VTABLE_DATA_ENTRY_DISTANCE));
9706 /* The initializer is the delta from BINFO to this virtual base.
9707 The vbase offsets go in reverse inheritance-graph order, and
9708 we are walking in inheritance graph order so these end up in
9709 the right order. */
9710 delta = size_diffop_loc (input_location,
9711 BINFO_OFFSET (b), BINFO_OFFSET (non_primary_binfo));
9713 CONSTRUCTOR_APPEND_ELT (vid->inits, NULL_TREE,
9714 fold_build1_loc (input_location, NOP_EXPR,
9715 vtable_entry_type, delta));
9719 /* Adds the initializers for the vcall offset entries in the vtable
9720 for BINFO (which is part of the class hierarchy dominated by VID->DERIVED)
9721 to VID->INITS. */
9723 static void
9724 build_vcall_offset_vtbl_entries (tree binfo, vtbl_init_data* vid)
9726 /* We only need these entries if this base is a virtual base. We
9727 compute the indices -- but do not add to the vtable -- when
9728 building the main vtable for a class. */
9729 if (binfo == TYPE_BINFO (vid->derived)
9730 || (BINFO_VIRTUAL_P (binfo)
9731 /* If BINFO is RTTI_BINFO, then (since BINFO does not
9732 correspond to VID->DERIVED), we are building a primary
9733 construction virtual table. Since this is a primary
9734 virtual table, we do not need the vcall offsets for
9735 BINFO. */
9736 && binfo != vid->rtti_binfo))
9738 /* We need a vcall offset for each of the virtual functions in this
9739 vtable. For example:
9741 class A { virtual void f (); };
9742 class B1 : virtual public A { virtual void f (); };
9743 class B2 : virtual public A { virtual void f (); };
9744 class C: public B1, public B2 { virtual void f (); };
9746 A C object has a primary base of B1, which has a primary base of A. A
9747 C also has a secondary base of B2, which no longer has a primary base
9748 of A. So the B2-in-C construction vtable needs a secondary vtable for
9749 A, which will adjust the A* to a B2* to call f. We have no way of
9750 knowing what (or even whether) this offset will be when we define B2,
9751 so we store this "vcall offset" in the A sub-vtable and look it up in
9752 a "virtual thunk" for B2::f.
9754 We need entries for all the functions in our primary vtable and
9755 in our non-virtual bases' secondary vtables. */
9756 vid->vbase = binfo;
9757 /* If we are just computing the vcall indices -- but do not need
9758 the actual entries -- not that. */
9759 if (!BINFO_VIRTUAL_P (binfo))
9760 vid->generate_vcall_entries = false;
9761 /* Now, walk through the non-virtual bases, adding vcall offsets. */
9762 add_vcall_offset_vtbl_entries_r (binfo, vid);
9766 /* Build vcall offsets, starting with those for BINFO. */
9768 static void
9769 add_vcall_offset_vtbl_entries_r (tree binfo, vtbl_init_data* vid)
9771 int i;
9772 tree primary_binfo;
9773 tree base_binfo;
9775 /* Don't walk into virtual bases -- except, of course, for the
9776 virtual base for which we are building vcall offsets. Any
9777 primary virtual base will have already had its offsets generated
9778 through the recursion in build_vcall_and_vbase_vtbl_entries. */
9779 if (BINFO_VIRTUAL_P (binfo) && vid->vbase != binfo)
9780 return;
9782 /* If BINFO has a primary base, process it first. */
9783 primary_binfo = get_primary_binfo (binfo);
9784 if (primary_binfo)
9785 add_vcall_offset_vtbl_entries_r (primary_binfo, vid);
9787 /* Add BINFO itself to the list. */
9788 add_vcall_offset_vtbl_entries_1 (binfo, vid);
9790 /* Scan the non-primary bases of BINFO. */
9791 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
9792 if (base_binfo != primary_binfo)
9793 add_vcall_offset_vtbl_entries_r (base_binfo, vid);
9796 /* Called from build_vcall_offset_vtbl_entries_r. */
9798 static void
9799 add_vcall_offset_vtbl_entries_1 (tree binfo, vtbl_init_data* vid)
9801 /* Make entries for the rest of the virtuals. */
9802 tree orig_fn;
9804 /* The ABI requires that the methods be processed in declaration
9805 order. */
9806 for (orig_fn = TYPE_METHODS (BINFO_TYPE (binfo));
9807 orig_fn;
9808 orig_fn = DECL_CHAIN (orig_fn))
9809 if (TREE_CODE (orig_fn) == FUNCTION_DECL && DECL_VINDEX (orig_fn))
9810 add_vcall_offset (orig_fn, binfo, vid);
9813 /* Add a vcall offset entry for ORIG_FN to the vtable. */
9815 static void
9816 add_vcall_offset (tree orig_fn, tree binfo, vtbl_init_data *vid)
9818 size_t i;
9819 tree vcall_offset;
9820 tree derived_entry;
9822 /* If there is already an entry for a function with the same
9823 signature as FN, then we do not need a second vcall offset.
9824 Check the list of functions already present in the derived
9825 class vtable. */
9826 FOR_EACH_VEC_SAFE_ELT (vid->fns, i, derived_entry)
9828 if (same_signature_p (derived_entry, orig_fn)
9829 /* We only use one vcall offset for virtual destructors,
9830 even though there are two virtual table entries. */
9831 || (DECL_DESTRUCTOR_P (derived_entry)
9832 && DECL_DESTRUCTOR_P (orig_fn)))
9833 return;
9836 /* If we are building these vcall offsets as part of building
9837 the vtable for the most derived class, remember the vcall
9838 offset. */
9839 if (vid->binfo == TYPE_BINFO (vid->derived))
9841 tree_pair_s elt = {orig_fn, vid->index};
9842 vec_safe_push (CLASSTYPE_VCALL_INDICES (vid->derived), elt);
9845 /* The next vcall offset will be found at a more negative
9846 offset. */
9847 vid->index = size_binop (MINUS_EXPR, vid->index,
9848 ssize_int (TARGET_VTABLE_DATA_ENTRY_DISTANCE));
9850 /* Keep track of this function. */
9851 vec_safe_push (vid->fns, orig_fn);
9853 if (vid->generate_vcall_entries)
9855 tree base;
9856 tree fn;
9858 /* Find the overriding function. */
9859 fn = find_final_overrider (vid->rtti_binfo, binfo, orig_fn);
9860 if (fn == error_mark_node)
9861 vcall_offset = build_zero_cst (vtable_entry_type);
9862 else
9864 base = TREE_VALUE (fn);
9866 /* The vbase we're working on is a primary base of
9867 vid->binfo. But it might be a lost primary, so its
9868 BINFO_OFFSET might be wrong, so we just use the
9869 BINFO_OFFSET from vid->binfo. */
9870 vcall_offset = size_diffop_loc (input_location,
9871 BINFO_OFFSET (base),
9872 BINFO_OFFSET (vid->binfo));
9873 vcall_offset = fold_build1_loc (input_location,
9874 NOP_EXPR, vtable_entry_type,
9875 vcall_offset);
9877 /* Add the initializer to the vtable. */
9878 CONSTRUCTOR_APPEND_ELT (vid->inits, NULL_TREE, vcall_offset);
9882 /* Return vtbl initializers for the RTTI entries corresponding to the
9883 BINFO's vtable. The RTTI entries should indicate the object given
9884 by VID->rtti_binfo. */
9886 static void
9887 build_rtti_vtbl_entries (tree binfo, vtbl_init_data* vid)
9889 tree b;
9890 tree t;
9891 tree offset;
9892 tree decl;
9893 tree init;
9895 t = BINFO_TYPE (vid->rtti_binfo);
9897 /* To find the complete object, we will first convert to our most
9898 primary base, and then add the offset in the vtbl to that value. */
9899 b = most_primary_binfo (binfo);
9900 offset = size_diffop_loc (input_location,
9901 BINFO_OFFSET (vid->rtti_binfo), BINFO_OFFSET (b));
9903 /* The second entry is the address of the typeinfo object. */
9904 if (flag_rtti)
9905 decl = build_address (get_tinfo_decl (t));
9906 else
9907 decl = integer_zero_node;
9909 /* Convert the declaration to a type that can be stored in the
9910 vtable. */
9911 init = build_nop (vfunc_ptr_type_node, decl);
9912 CONSTRUCTOR_APPEND_ELT (vid->inits, NULL_TREE, init);
9914 /* Add the offset-to-top entry. It comes earlier in the vtable than
9915 the typeinfo entry. Convert the offset to look like a
9916 function pointer, so that we can put it in the vtable. */
9917 init = build_nop (vfunc_ptr_type_node, offset);
9918 CONSTRUCTOR_APPEND_ELT (vid->inits, NULL_TREE, init);
9921 /* TRUE iff TYPE is uniquely derived from PARENT. Ignores
9922 accessibility. */
9924 bool
9925 uniquely_derived_from_p (tree parent, tree type)
9927 tree base = lookup_base (type, parent, ba_unique, NULL, tf_none);
9928 return base && base != error_mark_node;
9931 /* TRUE iff TYPE is publicly & uniquely derived from PARENT. */
9933 bool
9934 publicly_uniquely_derived_p (tree parent, tree type)
9936 tree base = lookup_base (type, parent, ba_ignore_scope | ba_check,
9937 NULL, tf_none);
9938 return base && base != error_mark_node;
9941 /* CTX1 and CTX2 are declaration contexts. Return the innermost common
9942 class between them, if any. */
9944 tree
9945 common_enclosing_class (tree ctx1, tree ctx2)
9947 if (!TYPE_P (ctx1) || !TYPE_P (ctx2))
9948 return NULL_TREE;
9949 gcc_assert (ctx1 == TYPE_MAIN_VARIANT (ctx1)
9950 && ctx2 == TYPE_MAIN_VARIANT (ctx2));
9951 if (ctx1 == ctx2)
9952 return ctx1;
9953 for (tree t = ctx1; TYPE_P (t); t = TYPE_CONTEXT (t))
9954 TYPE_MARKED_P (t) = true;
9955 tree found = NULL_TREE;
9956 for (tree t = ctx2; TYPE_P (t); t = TYPE_CONTEXT (t))
9957 if (TYPE_MARKED_P (t))
9959 found = t;
9960 break;
9962 for (tree t = ctx1; TYPE_P (t); t = TYPE_CONTEXT (t))
9963 TYPE_MARKED_P (t) = false;
9964 return found;
9967 #include "gt-cp-class.h"