]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/cp/class.c
Update copyright years.
[thirdparty/gcc.git] / gcc / cp / class.c
1 /* Functions related to building classes and their related objects.
2 Copyright (C) 1987-2018 Free Software Foundation, Inc.
3 Contributed by Michael Tiemann (tiemann@cygnus.com)
4
5 This file is part of GCC.
6
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.
11
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.
16
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/>. */
20
21
22 /* High-level class interface. */
23
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"
39 #include "asan.h"
40
41 /* Id for dumping the class hierarchy. */
42 int class_dump_id;
43
44 /* The number of nested classes being processed. If we are not in the
45 scope of any class, this is zero. */
46
47 int current_class_depth;
48
49 /* In order to deal with nested classes, we keep a stack of classes.
50 The topmost entry is the innermost class, and is the entry at index
51 CURRENT_CLASS_DEPTH */
52
53 typedef struct class_stack_node {
54 /* The name of the class. */
55 tree name;
56
57 /* The _TYPE node for the class. */
58 tree type;
59
60 /* The access specifier pending for new declarations in the scope of
61 this class. */
62 tree access;
63
64 /* If were defining TYPE, the names used in this class. */
65 splay_tree names_used;
66
67 /* Nonzero if this class is no longer open, because of a call to
68 push_to_top_level. */
69 size_t hidden;
70 }* class_stack_node_t;
71
72 struct vtbl_init_data
73 {
74 /* The base for which we're building initializers. */
75 tree binfo;
76 /* The type of the most-derived type. */
77 tree derived;
78 /* The binfo for the dynamic type. This will be TYPE_BINFO (derived),
79 unless ctor_vtbl_p is true. */
80 tree rtti_binfo;
81 /* The negative-index vtable initializers built up so far. These
82 are in order from least negative index to most negative index. */
83 vec<constructor_elt, va_gc> *inits;
84 /* The binfo for the virtual base for which we're building
85 vcall offset initializers. */
86 tree vbase;
87 /* The functions in vbase for which we have already provided vcall
88 offsets. */
89 vec<tree, va_gc> *fns;
90 /* The vtable index of the next vcall or vbase offset. */
91 tree index;
92 /* Nonzero if we are building the initializer for the primary
93 vtable. */
94 int primary_vtbl_p;
95 /* Nonzero if we are building the initializer for a construction
96 vtable. */
97 int ctor_vtbl_p;
98 /* True when adding vcall offset entries to the vtable. False when
99 merely computing the indices. */
100 bool generate_vcall_entries;
101 };
102
103 /* The type of a function passed to walk_subobject_offsets. */
104 typedef int (*subobject_offset_fn) (tree, tree, splay_tree);
105
106 /* The stack itself. This is a dynamically resized array. The
107 number of elements allocated is CURRENT_CLASS_STACK_SIZE. */
108 static int current_class_stack_size;
109 static class_stack_node_t current_class_stack;
110
111 /* The size of the largest empty class seen in this translation unit. */
112 static GTY (()) tree sizeof_biggest_empty_class;
113
114 /* An array of all local classes present in this translation unit, in
115 declaration order. */
116 vec<tree, va_gc> *local_classes;
117
118 static tree get_vfield_name (tree);
119 static void finish_struct_anon (tree);
120 static tree get_vtable_name (tree);
121 static void get_basefndecls (tree, tree, vec<tree> *);
122 static int build_primary_vtable (tree, tree);
123 static int build_secondary_vtable (tree);
124 static void finish_vtbls (tree);
125 static void modify_vtable_entry (tree, tree, tree, tree, tree *);
126 static void finish_struct_bits (tree);
127 static int alter_access (tree, tree, tree);
128 static void handle_using_decl (tree, tree);
129 static tree dfs_modify_vtables (tree, void *);
130 static tree modify_all_vtables (tree, tree);
131 static void determine_primary_bases (tree);
132 static void maybe_warn_about_overly_private_class (tree);
133 static void add_implicitly_declared_members (tree, tree*, int, int);
134 static tree fixed_type_or_null (tree, int *, int *);
135 static tree build_simple_base_path (tree expr, tree binfo);
136 static tree build_vtbl_ref_1 (tree, tree);
137 static void build_vtbl_initializer (tree, tree, tree, tree, int *,
138 vec<constructor_elt, va_gc> **);
139 static bool check_bitfield_decl (tree);
140 static bool check_field_decl (tree, tree, int *, int *);
141 static void check_field_decls (tree, tree *, int *, int *);
142 static tree *build_base_field (record_layout_info, tree, splay_tree, tree *);
143 static void build_base_fields (record_layout_info, splay_tree, tree *);
144 static void check_methods (tree);
145 static void remove_zero_width_bit_fields (tree);
146 static bool accessible_nvdtor_p (tree);
147
148 /* Used by find_flexarrays and related functions. */
149 struct flexmems_t;
150 static void diagnose_flexarrays (tree, const flexmems_t *);
151 static void find_flexarrays (tree, flexmems_t *, bool = false,
152 tree = NULL_TREE, tree = NULL_TREE);
153 static void check_flexarrays (tree, flexmems_t * = NULL, bool = false);
154 static void check_bases (tree, int *, int *);
155 static void check_bases_and_members (tree);
156 static tree create_vtable_ptr (tree, tree *);
157 static void include_empty_classes (record_layout_info);
158 static void layout_class_type (tree, tree *);
159 static void propagate_binfo_offsets (tree, tree);
160 static void layout_virtual_bases (record_layout_info, splay_tree);
161 static void build_vbase_offset_vtbl_entries (tree, vtbl_init_data *);
162 static void add_vcall_offset_vtbl_entries_r (tree, vtbl_init_data *);
163 static void add_vcall_offset_vtbl_entries_1 (tree, vtbl_init_data *);
164 static void build_vcall_offset_vtbl_entries (tree, vtbl_init_data *);
165 static void add_vcall_offset (tree, tree, vtbl_init_data *);
166 static void layout_vtable_decl (tree, int);
167 static tree dfs_find_final_overrider_pre (tree, void *);
168 static tree dfs_find_final_overrider_post (tree, void *);
169 static tree find_final_overrider (tree, tree, tree);
170 static int make_new_vtable (tree, tree);
171 static tree get_primary_binfo (tree);
172 static int maybe_indent_hierarchy (FILE *, int, int);
173 static tree dump_class_hierarchy_r (FILE *, dump_flags_t, tree, tree, int);
174 static void dump_class_hierarchy (tree);
175 static void dump_class_hierarchy_1 (FILE *, dump_flags_t, tree);
176 static void dump_array (FILE *, tree);
177 static void dump_vtable (tree, tree, tree);
178 static void dump_vtt (tree, tree);
179 static void dump_thunk (FILE *, int, tree);
180 static tree build_vtable (tree, tree, tree);
181 static void initialize_vtable (tree, vec<constructor_elt, va_gc> *);
182 static void layout_nonempty_base_or_field (record_layout_info,
183 tree, tree, splay_tree);
184 static tree end_of_class (tree, int);
185 static bool layout_empty_base (record_layout_info, tree, tree, splay_tree);
186 static void accumulate_vtbl_inits (tree, tree, tree, tree, tree,
187 vec<constructor_elt, va_gc> **);
188 static void dfs_accumulate_vtbl_inits (tree, tree, tree, tree, tree,
189 vec<constructor_elt, va_gc> **);
190 static void build_rtti_vtbl_entries (tree, vtbl_init_data *);
191 static void build_vcall_and_vbase_vtbl_entries (tree, vtbl_init_data *);
192 static void clone_constructors_and_destructors (tree);
193 static tree build_clone (tree, tree);
194 static void update_vtable_entry_for_fn (tree, tree, tree, tree *, unsigned);
195 static void build_ctor_vtbl_group (tree, tree);
196 static void build_vtt (tree);
197 static tree binfo_ctor_vtable (tree);
198 static void build_vtt_inits (tree, tree, vec<constructor_elt, va_gc> **,
199 tree *);
200 static tree dfs_build_secondary_vptr_vtt_inits (tree, void *);
201 static tree dfs_fixup_binfo_vtbls (tree, void *);
202 static int record_subobject_offset (tree, tree, splay_tree);
203 static int check_subobject_offset (tree, tree, splay_tree);
204 static int walk_subobject_offsets (tree, subobject_offset_fn,
205 tree, splay_tree, tree, int);
206 static void record_subobject_offsets (tree, tree, splay_tree, bool);
207 static int layout_conflict_p (tree, tree, splay_tree, int);
208 static int splay_tree_compare_integer_csts (splay_tree_key k1,
209 splay_tree_key k2);
210 static void warn_about_ambiguous_bases (tree);
211 static bool type_requires_array_cookie (tree);
212 static bool base_derived_from (tree, tree);
213 static int empty_base_at_nonzero_offset_p (tree, tree, splay_tree);
214 static tree end_of_base (tree);
215 static tree get_vcall_index (tree, tree);
216 static bool type_maybe_constexpr_default_constructor (tree);
217
218 /* Variables shared between class.c and call.c. */
219
220 int n_vtables = 0;
221 int n_vtable_entries = 0;
222 int n_vtable_searches = 0;
223 int n_vtable_elems = 0;
224 int n_convert_harshness = 0;
225 int n_compute_conversion_costs = 0;
226 int n_inner_fields_searched = 0;
227
228 /* Return a COND_EXPR that executes TRUE_STMT if this execution of the
229 'structor is in charge of 'structing virtual bases, or FALSE_STMT
230 otherwise. */
231
232 tree
233 build_if_in_charge (tree true_stmt, tree false_stmt)
234 {
235 gcc_assert (DECL_HAS_IN_CHARGE_PARM_P (current_function_decl));
236 tree cmp = build2 (NE_EXPR, boolean_type_node,
237 current_in_charge_parm, integer_zero_node);
238 tree type = unlowered_expr_type (true_stmt);
239 if (VOID_TYPE_P (type))
240 type = unlowered_expr_type (false_stmt);
241 tree cond = build3 (COND_EXPR, type,
242 cmp, true_stmt, false_stmt);
243 return cond;
244 }
245
246 /* Convert to or from a base subobject. EXPR is an expression of type
247 `A' or `A*', an expression of type `B' or `B*' is returned. To
248 convert A to a base B, CODE is PLUS_EXPR and BINFO is the binfo for
249 the B base instance within A. To convert base A to derived B, CODE
250 is MINUS_EXPR and BINFO is the binfo for the A instance within B.
251 In this latter case, A must not be a morally virtual base of B.
252 NONNULL is true if EXPR is known to be non-NULL (this is only
253 needed when EXPR is of pointer type). CV qualifiers are preserved
254 from EXPR. */
255
256 tree
257 build_base_path (enum tree_code code,
258 tree expr,
259 tree binfo,
260 int nonnull,
261 tsubst_flags_t complain)
262 {
263 tree v_binfo = NULL_TREE;
264 tree d_binfo = NULL_TREE;
265 tree probe;
266 tree offset;
267 tree target_type;
268 tree null_test = NULL;
269 tree ptr_target_type;
270 int fixed_type_p;
271 int want_pointer = TYPE_PTR_P (TREE_TYPE (expr));
272 bool has_empty = false;
273 bool virtual_access;
274 bool rvalue = false;
275
276 if (expr == error_mark_node || binfo == error_mark_node || !binfo)
277 return error_mark_node;
278
279 for (probe = binfo; probe; probe = BINFO_INHERITANCE_CHAIN (probe))
280 {
281 d_binfo = probe;
282 if (is_empty_class (BINFO_TYPE (probe)))
283 has_empty = true;
284 if (!v_binfo && BINFO_VIRTUAL_P (probe))
285 v_binfo = probe;
286 }
287
288 probe = TYPE_MAIN_VARIANT (TREE_TYPE (expr));
289 if (want_pointer)
290 probe = TYPE_MAIN_VARIANT (TREE_TYPE (probe));
291
292 if (code == PLUS_EXPR
293 && !SAME_BINFO_TYPE_P (BINFO_TYPE (d_binfo), probe))
294 {
295 /* This can happen when adjust_result_of_qualified_name_lookup can't
296 find a unique base binfo in a call to a member function. We
297 couldn't give the diagnostic then since we might have been calling
298 a static member function, so we do it now. In other cases, eg.
299 during error recovery (c++/71979), we may not have a base at all. */
300 if (complain & tf_error)
301 {
302 tree base = lookup_base (probe, BINFO_TYPE (d_binfo),
303 ba_unique, NULL, complain);
304 gcc_assert (base == error_mark_node || !base);
305 }
306 return error_mark_node;
307 }
308
309 gcc_assert ((code == MINUS_EXPR
310 && SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), probe))
311 || code == PLUS_EXPR);
312
313 if (binfo == d_binfo)
314 /* Nothing to do. */
315 return expr;
316
317 if (code == MINUS_EXPR && v_binfo)
318 {
319 if (complain & tf_error)
320 {
321 if (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), BINFO_TYPE (v_binfo)))
322 {
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));
331 }
332 else
333 {
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));
343 }
344 }
345 return error_mark_node;
346 }
347
348 if (!want_pointer)
349 {
350 rvalue = !lvalue_p (expr);
351 /* This must happen before the call to save_expr. */
352 expr = cp_build_addr_expr (expr, complain);
353 }
354 else
355 expr = mark_rvalue_use (expr);
356
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);
366
367 /* Do we need to look in the vtable for the real offset? */
368 virtual_access = (v_binfo && fixed_type_p <= 0);
369
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 || processing_template_decl
377 || in_template_function ())
378 {
379 expr = build_nop (ptr_target_type, expr);
380 goto indout;
381 }
382
383 /* If we're in an NSDMI, we don't have the full constructor context yet
384 that we need for converting to a virtual base, so just build a stub
385 CONVERT_EXPR and expand it later in bot_replace. */
386 if (virtual_access && fixed_type_p < 0
387 && current_scope () != current_function_decl)
388 {
389 expr = build1 (CONVERT_EXPR, ptr_target_type, expr);
390 CONVERT_EXPR_VBASE_PATH (expr) = true;
391 goto indout;
392 }
393
394 /* Do we need to check for a null pointer? */
395 if (want_pointer && !nonnull)
396 {
397 /* If we know the conversion will not actually change the value
398 of EXPR, then we can avoid testing the expression for NULL.
399 We have to avoid generating a COMPONENT_REF for a base class
400 field, because other parts of the compiler know that such
401 expressions are always non-NULL. */
402 if (!virtual_access && integer_zerop (offset))
403 return build_nop (ptr_target_type, expr);
404 null_test = error_mark_node;
405 }
406
407 /* Protect against multiple evaluation if necessary. */
408 if (TREE_SIDE_EFFECTS (expr) && (null_test || virtual_access))
409 expr = save_expr (expr);
410
411 /* Now that we've saved expr, build the real null test. */
412 if (null_test)
413 {
414 tree zero = cp_convert (TREE_TYPE (expr), nullptr_node, complain);
415 null_test = build2_loc (input_location, NE_EXPR, boolean_type_node,
416 expr, zero);
417 /* This is a compiler generated comparison, don't emit
418 e.g. -Wnonnull-compare warning for it. */
419 TREE_NO_WARNING (null_test) = 1;
420 }
421
422 /* If this is a simple base reference, express it as a COMPONENT_REF. */
423 if (code == PLUS_EXPR && !virtual_access
424 /* We don't build base fields for empty bases, and they aren't very
425 interesting to the optimizers anyway. */
426 && !has_empty)
427 {
428 expr = cp_build_fold_indirect_ref (expr);
429 expr = build_simple_base_path (expr, binfo);
430 if (rvalue)
431 expr = move (expr);
432 if (want_pointer)
433 expr = build_address (expr);
434 target_type = TREE_TYPE (expr);
435 goto out;
436 }
437
438 if (virtual_access)
439 {
440 /* Going via virtual base V_BINFO. We need the static offset
441 from V_BINFO to BINFO, and the dynamic offset from D_BINFO to
442 V_BINFO. That offset is an entry in D_BINFO's vtable. */
443 tree v_offset;
444
445 if (fixed_type_p < 0 && in_base_initializer)
446 {
447 /* In a base member initializer, we cannot rely on the
448 vtable being set up. We have to indirect via the
449 vtt_parm. */
450 tree t;
451
452 t = TREE_TYPE (TYPE_VFIELD (current_class_type));
453 t = build_pointer_type (t);
454 v_offset = fold_convert (t, current_vtt_parm);
455 v_offset = cp_build_fold_indirect_ref (v_offset);
456 }
457 else
458 {
459 tree t = expr;
460 if (sanitize_flags_p (SANITIZE_VPTR)
461 && fixed_type_p == 0)
462 {
463 t = cp_ubsan_maybe_instrument_cast_to_vbase (input_location,
464 probe, expr);
465 if (t == NULL_TREE)
466 t = expr;
467 }
468 v_offset = build_vfield_ref (cp_build_fold_indirect_ref (t),
469 TREE_TYPE (TREE_TYPE (expr)));
470 }
471
472 if (v_offset == error_mark_node)
473 return error_mark_node;
474
475 v_offset = fold_build_pointer_plus (v_offset, BINFO_VPTR_FIELD (v_binfo));
476 v_offset = build1 (NOP_EXPR,
477 build_pointer_type (ptrdiff_type_node),
478 v_offset);
479 v_offset = cp_build_fold_indirect_ref (v_offset);
480 TREE_CONSTANT (v_offset) = 1;
481
482 offset = convert_to_integer (ptrdiff_type_node,
483 size_diffop_loc (input_location, offset,
484 BINFO_OFFSET (v_binfo)));
485
486 if (!integer_zerop (offset))
487 v_offset = build2 (code, ptrdiff_type_node, v_offset, offset);
488
489 if (fixed_type_p < 0)
490 /* Negative fixed_type_p means this is a constructor or destructor;
491 virtual base layout is fixed in in-charge [cd]tors, but not in
492 base [cd]tors. */
493 offset = build_if_in_charge
494 (convert_to_integer (ptrdiff_type_node, BINFO_OFFSET (binfo)),
495 v_offset);
496 else
497 offset = v_offset;
498 }
499
500 if (want_pointer)
501 target_type = ptr_target_type;
502
503 expr = build1 (NOP_EXPR, ptr_target_type, expr);
504
505 if (!integer_zerop (offset))
506 {
507 offset = fold_convert (sizetype, offset);
508 if (code == MINUS_EXPR)
509 offset = fold_build1_loc (input_location, NEGATE_EXPR, sizetype, offset);
510 expr = fold_build_pointer_plus (expr, offset);
511 }
512 else
513 null_test = NULL;
514
515 indout:
516 if (!want_pointer)
517 {
518 expr = cp_build_fold_indirect_ref (expr);
519 if (rvalue)
520 expr = move (expr);
521 }
522
523 out:
524 if (null_test)
525 expr = fold_build3_loc (input_location, COND_EXPR, target_type, null_test, expr,
526 build_zero_cst (target_type));
527
528 return expr;
529 }
530
531 /* Subroutine of build_base_path; EXPR and BINFO are as in that function.
532 Perform a derived-to-base conversion by recursively building up a
533 sequence of COMPONENT_REFs to the appropriate base fields. */
534
535 static tree
536 build_simple_base_path (tree expr, tree binfo)
537 {
538 tree type = BINFO_TYPE (binfo);
539 tree d_binfo = BINFO_INHERITANCE_CHAIN (binfo);
540 tree field;
541
542 if (d_binfo == NULL_TREE)
543 {
544 tree temp;
545
546 gcc_assert (TYPE_MAIN_VARIANT (TREE_TYPE (expr)) == type);
547
548 /* Transform `(a, b).x' into `(*(a, &b)).x', `(a ? b : c).x'
549 into `(*(a ? &b : &c)).x', and so on. A COND_EXPR is only
550 an lvalue in the front end; only _DECLs and _REFs are lvalues
551 in the back end. */
552 temp = unary_complex_lvalue (ADDR_EXPR, expr);
553 if (temp)
554 expr = cp_build_fold_indirect_ref (temp);
555
556 return expr;
557 }
558
559 /* Recurse. */
560 expr = build_simple_base_path (expr, d_binfo);
561
562 for (field = TYPE_FIELDS (BINFO_TYPE (d_binfo));
563 field; field = DECL_CHAIN (field))
564 /* Is this the base field created by build_base_field? */
565 if (TREE_CODE (field) == FIELD_DECL
566 && DECL_FIELD_IS_BASE (field)
567 && TREE_TYPE (field) == type
568 /* If we're looking for a field in the most-derived class,
569 also check the field offset; we can have two base fields
570 of the same type if one is an indirect virtual base and one
571 is a direct non-virtual base. */
572 && (BINFO_INHERITANCE_CHAIN (d_binfo)
573 || tree_int_cst_equal (byte_position (field),
574 BINFO_OFFSET (binfo))))
575 {
576 /* We don't use build_class_member_access_expr here, as that
577 has unnecessary checks, and more importantly results in
578 recursive calls to dfs_walk_once. */
579 int type_quals = cp_type_quals (TREE_TYPE (expr));
580
581 expr = build3 (COMPONENT_REF,
582 cp_build_qualified_type (type, type_quals),
583 expr, field, NULL_TREE);
584 /* Mark the expression const or volatile, as appropriate.
585 Even though we've dealt with the type above, we still have
586 to mark the expression itself. */
587 if (type_quals & TYPE_QUAL_CONST)
588 TREE_READONLY (expr) = 1;
589 if (type_quals & TYPE_QUAL_VOLATILE)
590 TREE_THIS_VOLATILE (expr) = 1;
591
592 return expr;
593 }
594
595 /* Didn't find the base field?!? */
596 gcc_unreachable ();
597 }
598
599 /* Convert OBJECT to the base TYPE. OBJECT is an expression whose
600 type is a class type or a pointer to a class type. In the former
601 case, TYPE is also a class type; in the latter it is another
602 pointer type. If CHECK_ACCESS is true, an error message is emitted
603 if TYPE is inaccessible. If OBJECT has pointer type, the value is
604 assumed to be non-NULL. */
605
606 tree
607 convert_to_base (tree object, tree type, bool check_access, bool nonnull,
608 tsubst_flags_t complain)
609 {
610 tree binfo;
611 tree object_type;
612
613 if (TYPE_PTR_P (TREE_TYPE (object)))
614 {
615 object_type = TREE_TYPE (TREE_TYPE (object));
616 type = TREE_TYPE (type);
617 }
618 else
619 object_type = TREE_TYPE (object);
620
621 binfo = lookup_base (object_type, type, check_access ? ba_check : ba_unique,
622 NULL, complain);
623 if (!binfo || binfo == error_mark_node)
624 return error_mark_node;
625
626 return build_base_path (PLUS_EXPR, object, binfo, nonnull, complain);
627 }
628
629 /* EXPR is an expression with unqualified class type. BASE is a base
630 binfo of that class type. Returns EXPR, converted to the BASE
631 type. This function assumes that EXPR is the most derived class;
632 therefore virtual bases can be found at their static offsets. */
633
634 tree
635 convert_to_base_statically (tree expr, tree base)
636 {
637 tree expr_type;
638
639 expr_type = TREE_TYPE (expr);
640 if (!SAME_BINFO_TYPE_P (BINFO_TYPE (base), expr_type))
641 {
642 /* If this is a non-empty base, use a COMPONENT_REF. */
643 if (!is_empty_class (BINFO_TYPE (base)))
644 return build_simple_base_path (expr, base);
645
646 /* We use fold_build2 and fold_convert below to simplify the trees
647 provided to the optimizers. It is not safe to call these functions
648 when processing a template because they do not handle C++-specific
649 trees. */
650 gcc_assert (!processing_template_decl);
651 expr = cp_build_addr_expr (expr, tf_warning_or_error);
652 if (!integer_zerop (BINFO_OFFSET (base)))
653 expr = fold_build_pointer_plus_loc (input_location,
654 expr, BINFO_OFFSET (base));
655 expr = fold_convert (build_pointer_type (BINFO_TYPE (base)), expr);
656 expr = build_fold_indirect_ref_loc (input_location, expr);
657 }
658
659 return expr;
660 }
661
662 \f
663 tree
664 build_vfield_ref (tree datum, tree type)
665 {
666 tree vfield, vcontext;
667
668 if (datum == error_mark_node
669 /* Can happen in case of duplicate base types (c++/59082). */
670 || !TYPE_VFIELD (type))
671 return error_mark_node;
672
673 /* First, convert to the requested type. */
674 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (datum), type))
675 datum = convert_to_base (datum, type, /*check_access=*/false,
676 /*nonnull=*/true, tf_warning_or_error);
677
678 /* Second, the requested type may not be the owner of its own vptr.
679 If not, convert to the base class that owns it. We cannot use
680 convert_to_base here, because VCONTEXT may appear more than once
681 in the inheritance hierarchy of TYPE, and thus direct conversion
682 between the types may be ambiguous. Following the path back up
683 one step at a time via primary bases avoids the problem. */
684 vfield = TYPE_VFIELD (type);
685 vcontext = DECL_CONTEXT (vfield);
686 while (!same_type_ignoring_top_level_qualifiers_p (vcontext, type))
687 {
688 datum = build_simple_base_path (datum, CLASSTYPE_PRIMARY_BINFO (type));
689 type = TREE_TYPE (datum);
690 }
691
692 return build3 (COMPONENT_REF, TREE_TYPE (vfield), datum, vfield, NULL_TREE);
693 }
694
695 /* Given an object INSTANCE, return an expression which yields the
696 vtable element corresponding to INDEX. There are many special
697 cases for INSTANCE which we take care of here, mainly to avoid
698 creating extra tree nodes when we don't have to. */
699
700 static tree
701 build_vtbl_ref_1 (tree instance, tree idx)
702 {
703 tree aref;
704 tree vtbl = NULL_TREE;
705
706 /* Try to figure out what a reference refers to, and
707 access its virtual function table directly. */
708
709 int cdtorp = 0;
710 tree fixed_type = fixed_type_or_null (instance, NULL, &cdtorp);
711
712 tree basetype = non_reference (TREE_TYPE (instance));
713
714 if (fixed_type && !cdtorp)
715 {
716 tree binfo = lookup_base (fixed_type, basetype,
717 ba_unique, NULL, tf_none);
718 if (binfo && binfo != error_mark_node)
719 vtbl = unshare_expr (BINFO_VTABLE (binfo));
720 }
721
722 if (!vtbl)
723 vtbl = build_vfield_ref (instance, basetype);
724
725 aref = build_array_ref (input_location, vtbl, idx);
726 TREE_CONSTANT (aref) |= TREE_CONSTANT (vtbl) && TREE_CONSTANT (idx);
727
728 return aref;
729 }
730
731 tree
732 build_vtbl_ref (tree instance, tree idx)
733 {
734 tree aref = build_vtbl_ref_1 (instance, idx);
735
736 return aref;
737 }
738
739 /* Given a stable object pointer INSTANCE_PTR, return an expression which
740 yields a function pointer corresponding to vtable element INDEX. */
741
742 tree
743 build_vfn_ref (tree instance_ptr, tree idx)
744 {
745 tree aref;
746
747 aref = build_vtbl_ref_1 (cp_build_fold_indirect_ref (instance_ptr),
748 idx);
749
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));
755
756 /* Remember this as a method reference, for later devirtualization. */
757 aref = build3 (OBJ_TYPE_REF, TREE_TYPE (aref), aref, instance_ptr, idx);
758
759 return aref;
760 }
761
762 /* Return the name of the virtual function table (as an IDENTIFIER_NODE)
763 for the given TYPE. */
764
765 static tree
766 get_vtable_name (tree type)
767 {
768 return mangle_vtbl_for_type (type);
769 }
770
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. */
778
779 void
780 set_linkage_according_to_type (tree /*type*/, tree decl)
781 {
782 TREE_PUBLIC (decl) = 1;
783 determine_visibility (decl);
784 }
785
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. */
789
790 static tree
791 build_vtable (tree class_type, tree name, tree vtable_type)
792 {
793 tree decl;
794
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;
811
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:
817
818 #pragma interface
819 struct S { virtual void member (); };
820
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;
833
834 return decl;
835 }
836
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. */
842
843 tree
844 get_vtable_decl (tree type, int complete)
845 {
846 tree decl;
847
848 if (CLASSTYPE_VTABLES (type))
849 return CLASSTYPE_VTABLES (type);
850
851 decl = build_vtable (type, get_vtable_name (type), vtbl_type_node);
852 CLASSTYPE_VTABLES (type) = decl;
853
854 if (complete)
855 {
856 DECL_EXTERNAL (decl) = 1;
857 cp_finish_decl (decl, NULL_TREE, false, NULL_TREE, 0);
858 }
859
860 return decl;
861 }
862
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. */
868
869 static int
870 build_primary_vtable (tree binfo, tree type)
871 {
872 tree decl;
873 tree virtuals;
874
875 decl = get_vtable_decl (type, /*complete=*/0);
876
877 if (binfo)
878 {
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;
883
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));
888 }
889 else
890 {
891 gcc_assert (TREE_TYPE (decl) == vtbl_type_node);
892 virtuals = NULL_TREE;
893 }
894
895 if (GATHER_STATISTICS)
896 {
897 n_vtables += 1;
898 n_vtable_elems += list_length (virtuals);
899 }
900
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;
907 }
908
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.
913
914 FOR_TYPE is the most derived type which caused this table to
915 be needed.
916
917 Returns nonzero if we haven't met BINFO before.
918
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. */
922
923 static int
924 build_secondary_vtable (tree binfo)
925 {
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;
930
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);
934
935 /* Make fresh virtual list, so we can smash it later. */
936 BINFO_VIRTUALS (binfo) = copy_list (BINFO_VIRTUALS (binfo));
937
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;
942 }
943
944 /* Create a new vtable for BINFO which is the hierarchy dominated by
945 T. Return nonzero if we actually created a new vtable. */
946
947 static int
948 make_new_vtable (tree t, tree binfo)
949 {
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);
961 }
962
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. */
968
969 static void
970 modify_vtable_entry (tree t,
971 tree binfo,
972 tree fndecl,
973 tree delta,
974 tree *virtuals)
975 {
976 tree v;
977
978 v = *virtuals;
979
980 if (fndecl != BV_FN (v)
981 || !tree_int_cst_equal (delta, BV_DELTA (v)))
982 {
983 /* We need a new vtable for BINFO. */
984 if (make_new_vtable (t, binfo))
985 {
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;
993 }
994
995 BV_DELTA (v) = delta;
996 BV_VCALL_INDEX (v) = NULL_TREE;
997 BV_FN (v) = fndecl;
998 }
999 }
1000
1001 \f
1002 /* Add method METHOD to class TYPE. If VIA_USING indicates whether
1003 METHOD is being injected via a using_decl. Returns true if the
1004 method could be added to the method vec. */
1005
1006 bool
1007 add_method (tree type, tree method, bool via_using)
1008 {
1009 if (method == error_mark_node)
1010 return false;
1011
1012 /* Maintain TYPE_HAS_USER_CONSTRUCTOR, etc. */
1013 grok_special_member_properties (method);
1014
1015 tree *slot = get_member_slot (type, DECL_NAME (method));
1016 tree current_fns = *slot;
1017
1018 gcc_assert (!DECL_EXTERN_C_P (method));
1019
1020 /* Check to see if we've already got this method. */
1021 for (ovl_iterator iter (current_fns); iter; ++iter)
1022 {
1023 tree fn = *iter;
1024 tree fn_type;
1025 tree method_type;
1026 tree parms1;
1027 tree parms2;
1028
1029 if (TREE_CODE (fn) != TREE_CODE (method))
1030 continue;
1031
1032 /* Two using-declarations can coexist, we'll complain about ambiguity in
1033 overload resolution. */
1034 if (via_using && iter.using_p ()
1035 /* Except handle inherited constructors specially. */
1036 && ! DECL_CONSTRUCTOR_P (fn))
1037 continue;
1038
1039 /* [over.load] Member function declarations with the
1040 same name and the same parameter types cannot be
1041 overloaded if any of them is a static member
1042 function declaration.
1043
1044 [over.load] Member function declarations with the same name and
1045 the same parameter-type-list as well as member function template
1046 declarations with the same name, the same parameter-type-list, and
1047 the same template parameter lists cannot be overloaded if any of
1048 them, but not all, have a ref-qualifier.
1049
1050 [namespace.udecl] When a using-declaration brings names
1051 from a base class into a derived class scope, member
1052 functions in the derived class override and/or hide member
1053 functions with the same name and parameter types in a base
1054 class (rather than conflicting). */
1055 fn_type = TREE_TYPE (fn);
1056 method_type = TREE_TYPE (method);
1057 parms1 = TYPE_ARG_TYPES (fn_type);
1058 parms2 = TYPE_ARG_TYPES (method_type);
1059
1060 /* Compare the quals on the 'this' parm. Don't compare
1061 the whole types, as used functions are treated as
1062 coming from the using class in overload resolution. */
1063 if (! DECL_STATIC_FUNCTION_P (fn)
1064 && ! DECL_STATIC_FUNCTION_P (method)
1065 /* Either both or neither need to be ref-qualified for
1066 differing quals to allow overloading. */
1067 && (FUNCTION_REF_QUALIFIED (fn_type)
1068 == FUNCTION_REF_QUALIFIED (method_type))
1069 && (type_memfn_quals (fn_type) != type_memfn_quals (method_type)
1070 || type_memfn_rqual (fn_type) != type_memfn_rqual (method_type)))
1071 continue;
1072
1073 /* For templates, the return type and template parameters
1074 must be identical. */
1075 if (TREE_CODE (fn) == TEMPLATE_DECL
1076 && (!same_type_p (TREE_TYPE (fn_type),
1077 TREE_TYPE (method_type))
1078 || !comp_template_parms (DECL_TEMPLATE_PARMS (fn),
1079 DECL_TEMPLATE_PARMS (method))))
1080 continue;
1081
1082 if (! DECL_STATIC_FUNCTION_P (fn))
1083 parms1 = TREE_CHAIN (parms1);
1084 if (! DECL_STATIC_FUNCTION_P (method))
1085 parms2 = TREE_CHAIN (parms2);
1086
1087 /* Bring back parameters omitted from an inherited ctor. */
1088 if (ctor_omit_inherited_parms (fn))
1089 parms1 = FUNCTION_FIRST_USER_PARMTYPE (DECL_ORIGIN (fn));
1090 if (ctor_omit_inherited_parms (method))
1091 parms2 = FUNCTION_FIRST_USER_PARMTYPE (DECL_ORIGIN (method));
1092
1093 if (compparms (parms1, parms2)
1094 && (!DECL_CONV_FN_P (fn)
1095 || same_type_p (TREE_TYPE (fn_type),
1096 TREE_TYPE (method_type)))
1097 && equivalently_constrained (fn, method))
1098 {
1099 /* If these are versions of the same function, process and
1100 move on. */
1101 if (TREE_CODE (fn) == FUNCTION_DECL
1102 && maybe_version_functions (method, fn))
1103 continue;
1104
1105 if (DECL_INHERITED_CTOR (method))
1106 {
1107 if (DECL_INHERITED_CTOR (fn))
1108 {
1109 tree basem = DECL_INHERITED_CTOR_BASE (method);
1110 tree basef = DECL_INHERITED_CTOR_BASE (fn);
1111 if (flag_new_inheriting_ctors)
1112 {
1113 if (basem == basef)
1114 {
1115 /* Inheriting the same constructor along different
1116 paths, combine them. */
1117 SET_DECL_INHERITED_CTOR
1118 (fn, ovl_make (DECL_INHERITED_CTOR (method),
1119 DECL_INHERITED_CTOR (fn)));
1120 /* And discard the new one. */
1121 return false;
1122 }
1123 else
1124 /* Inherited ctors can coexist until overload
1125 resolution. */
1126 continue;
1127 }
1128 error_at (DECL_SOURCE_LOCATION (method),
1129 "%q#D conflicts with version inherited from %qT",
1130 method, basef);
1131 inform (DECL_SOURCE_LOCATION (fn),
1132 "version inherited from %qT declared here",
1133 basef);
1134 }
1135 /* Otherwise defer to the other function. */
1136 return false;
1137 }
1138
1139 if (via_using)
1140 /* Defer to the local function. */
1141 return false;
1142 else if (flag_new_inheriting_ctors
1143 && DECL_INHERITED_CTOR (fn))
1144 {
1145 /* Remove the inherited constructor. */
1146 current_fns = iter.remove_node (current_fns);
1147 continue;
1148 }
1149 else
1150 {
1151 error_at (DECL_SOURCE_LOCATION (method),
1152 "%q#D cannot be overloaded with %q#D", method, fn);
1153 inform (DECL_SOURCE_LOCATION (fn),
1154 "previous declaration %q#D", fn);
1155 return false;
1156 }
1157 }
1158 }
1159
1160 /* A class should never have more than one destructor. */
1161 gcc_assert (!current_fns || !DECL_DESTRUCTOR_P (method));
1162
1163 current_fns = ovl_insert (method, current_fns, via_using);
1164
1165 if (!DECL_CONV_FN_P (method) && !COMPLETE_TYPE_P (type))
1166 push_class_level_binding (DECL_NAME (method), current_fns);
1167
1168 *slot = current_fns;
1169
1170 return true;
1171 }
1172
1173 /* Subroutines of finish_struct. */
1174
1175 /* Change the access of FDECL to ACCESS in T. Return 1 if change was
1176 legit, otherwise return 0. */
1177
1178 static int
1179 alter_access (tree t, tree fdecl, tree access)
1180 {
1181 tree elem;
1182
1183 retrofit_lang_decl (fdecl);
1184
1185 gcc_assert (!DECL_DISCRIMINATOR_P (fdecl));
1186
1187 elem = purpose_member (t, DECL_ACCESS (fdecl));
1188 if (elem)
1189 {
1190 if (TREE_VALUE (elem) != access)
1191 {
1192 if (TREE_CODE (TREE_TYPE (fdecl)) == FUNCTION_DECL)
1193 error ("conflicting access specifications for method"
1194 " %q+D, ignored", TREE_TYPE (fdecl));
1195 else
1196 error ("conflicting access specifications for field %qE, ignored",
1197 DECL_NAME (fdecl));
1198 }
1199 else
1200 {
1201 /* They're changing the access to the same thing they changed
1202 it to before. That's OK. */
1203 ;
1204 }
1205 }
1206 else
1207 {
1208 perform_or_defer_access_check (TYPE_BINFO (t), fdecl, fdecl,
1209 tf_warning_or_error);
1210 DECL_ACCESS (fdecl) = tree_cons (t, access, DECL_ACCESS (fdecl));
1211 return 1;
1212 }
1213 return 0;
1214 }
1215
1216 /* Return the access node for DECL's access in its enclosing class. */
1217
1218 tree
1219 declared_access (tree decl)
1220 {
1221 return (TREE_PRIVATE (decl) ? access_private_node
1222 : TREE_PROTECTED (decl) ? access_protected_node
1223 : access_public_node);
1224 }
1225
1226 /* Process the USING_DECL, which is a member of T. */
1227
1228 static void
1229 handle_using_decl (tree using_decl, tree t)
1230 {
1231 tree decl = USING_DECL_DECLS (using_decl);
1232 tree name = DECL_NAME (using_decl);
1233 tree access = declared_access (using_decl);
1234 tree flist = NULL_TREE;
1235 tree old_value;
1236
1237 gcc_assert (!processing_template_decl && decl);
1238
1239 old_value = lookup_member (t, name, /*protect=*/0, /*want_type=*/false,
1240 tf_warning_or_error);
1241 if (old_value)
1242 {
1243 old_value = OVL_FIRST (old_value);
1244
1245 if (DECL_P (old_value) && DECL_CONTEXT (old_value) == t)
1246 /* OK */;
1247 else
1248 old_value = NULL_TREE;
1249 }
1250
1251 cp_emit_debug_info_for_using (decl, t);
1252
1253 if (is_overloaded_fn (decl))
1254 flist = decl;
1255
1256 if (! old_value)
1257 ;
1258 else if (is_overloaded_fn (old_value))
1259 {
1260 if (flist)
1261 /* It's OK to use functions from a base when there are functions with
1262 the same name already present in the current class. */;
1263 else
1264 {
1265 error_at (DECL_SOURCE_LOCATION (using_decl), "%qD invalid in %q#T "
1266 "because of local method %q#D with same name",
1267 using_decl, t, old_value);
1268 inform (DECL_SOURCE_LOCATION (old_value),
1269 "local method %q#D declared here", old_value);
1270 return;
1271 }
1272 }
1273 else if (!DECL_ARTIFICIAL (old_value))
1274 {
1275 error_at (DECL_SOURCE_LOCATION (using_decl), "%qD invalid in %q#T "
1276 "because of local member %q#D with same name",
1277 using_decl, t, old_value);
1278 inform (DECL_SOURCE_LOCATION (old_value),
1279 "local member %q#D declared here", old_value);
1280 return;
1281 }
1282
1283 /* Make type T see field decl FDECL with access ACCESS. */
1284 if (flist)
1285 for (ovl_iterator iter (flist); iter; ++iter)
1286 {
1287 add_method (t, *iter, true);
1288 alter_access (t, *iter, access);
1289 }
1290 else
1291 alter_access (t, decl, access);
1292 }
1293 \f
1294 /* Data structure for find_abi_tags_r, below. */
1295
1296 struct abi_tag_data
1297 {
1298 tree t; // The type that we're checking for missing tags.
1299 tree subob; // The subobject of T that we're getting tags from.
1300 tree tags; // error_mark_node for diagnostics, or a list of missing tags.
1301 };
1302
1303 /* Subroutine of find_abi_tags_r. Handle a single TAG found on the class TP
1304 in the context of P. TAG can be either an identifier (the DECL_NAME of
1305 a tag NAMESPACE_DECL) or a STRING_CST (a tag attribute). */
1306
1307 static void
1308 check_tag (tree tag, tree id, tree *tp, abi_tag_data *p)
1309 {
1310 if (!IDENTIFIER_MARKED (id))
1311 {
1312 if (p->tags != error_mark_node)
1313 {
1314 /* We're collecting tags from template arguments or from
1315 the type of a variable or function return type. */
1316 p->tags = tree_cons (NULL_TREE, tag, p->tags);
1317
1318 /* Don't inherit this tag multiple times. */
1319 IDENTIFIER_MARKED (id) = true;
1320
1321 if (TYPE_P (p->t))
1322 {
1323 /* Tags inherited from type template arguments are only used
1324 to avoid warnings. */
1325 ABI_TAG_IMPLICIT (p->tags) = true;
1326 return;
1327 }
1328 /* For functions and variables we want to warn, too. */
1329 }
1330
1331 /* Otherwise we're diagnosing missing tags. */
1332 if (TREE_CODE (p->t) == FUNCTION_DECL)
1333 {
1334 if (warning (OPT_Wabi_tag, "%qD inherits the %E ABI tag "
1335 "that %qT (used in its return type) has",
1336 p->t, tag, *tp))
1337 inform (location_of (*tp), "%qT declared here", *tp);
1338 }
1339 else if (VAR_P (p->t))
1340 {
1341 if (warning (OPT_Wabi_tag, "%qD inherits the %E ABI tag "
1342 "that %qT (used in its type) has", p->t, tag, *tp))
1343 inform (location_of (*tp), "%qT declared here", *tp);
1344 }
1345 else if (TYPE_P (p->subob))
1346 {
1347 if (warning (OPT_Wabi_tag, "%qT does not have the %E ABI tag "
1348 "that base %qT has", p->t, tag, p->subob))
1349 inform (location_of (p->subob), "%qT declared here",
1350 p->subob);
1351 }
1352 else
1353 {
1354 if (warning (OPT_Wabi_tag, "%qT does not have the %E ABI tag "
1355 "that %qT (used in the type of %qD) has",
1356 p->t, tag, *tp, p->subob))
1357 {
1358 inform (location_of (p->subob), "%qD declared here",
1359 p->subob);
1360 inform (location_of (*tp), "%qT declared here", *tp);
1361 }
1362 }
1363 }
1364 }
1365
1366 /* Find all the ABI tags in the attribute list ATTR and either call
1367 check_tag (if TP is non-null) or set IDENTIFIER_MARKED to val. */
1368
1369 static void
1370 mark_or_check_attr_tags (tree attr, tree *tp, abi_tag_data *p, bool val)
1371 {
1372 if (!attr)
1373 return;
1374 for (; (attr = lookup_attribute ("abi_tag", attr));
1375 attr = TREE_CHAIN (attr))
1376 for (tree list = TREE_VALUE (attr); list;
1377 list = TREE_CHAIN (list))
1378 {
1379 tree tag = TREE_VALUE (list);
1380 tree id = get_identifier (TREE_STRING_POINTER (tag));
1381 if (tp)
1382 check_tag (tag, id, tp, p);
1383 else
1384 IDENTIFIER_MARKED (id) = val;
1385 }
1386 }
1387
1388 /* Find all the ABI tags on T and its enclosing scopes and either call
1389 check_tag (if TP is non-null) or set IDENTIFIER_MARKED to val. */
1390
1391 static void
1392 mark_or_check_tags (tree t, tree *tp, abi_tag_data *p, bool val)
1393 {
1394 while (t != global_namespace)
1395 {
1396 tree attr;
1397 if (TYPE_P (t))
1398 {
1399 attr = TYPE_ATTRIBUTES (t);
1400 t = CP_TYPE_CONTEXT (t);
1401 }
1402 else
1403 {
1404 attr = DECL_ATTRIBUTES (t);
1405 t = CP_DECL_CONTEXT (t);
1406 }
1407 mark_or_check_attr_tags (attr, tp, p, val);
1408 }
1409 }
1410
1411 /* walk_tree callback for check_abi_tags: if the type at *TP involves any
1412 types with ABI tags, add the corresponding identifiers to the VEC in
1413 *DATA and set IDENTIFIER_MARKED. */
1414
1415 static tree
1416 find_abi_tags_r (tree *tp, int *walk_subtrees, void *data)
1417 {
1418 if (!OVERLOAD_TYPE_P (*tp))
1419 return NULL_TREE;
1420
1421 /* walk_tree shouldn't be walking into any subtrees of a RECORD_TYPE
1422 anyway, but let's make sure of it. */
1423 *walk_subtrees = false;
1424
1425 abi_tag_data *p = static_cast<struct abi_tag_data*>(data);
1426
1427 mark_or_check_tags (*tp, tp, p, false);
1428
1429 return NULL_TREE;
1430 }
1431
1432 /* walk_tree callback for mark_abi_tags: if *TP is a class, set
1433 IDENTIFIER_MARKED on its ABI tags. */
1434
1435 static tree
1436 mark_abi_tags_r (tree *tp, int *walk_subtrees, void *data)
1437 {
1438 if (!OVERLOAD_TYPE_P (*tp))
1439 return NULL_TREE;
1440
1441 /* walk_tree shouldn't be walking into any subtrees of a RECORD_TYPE
1442 anyway, but let's make sure of it. */
1443 *walk_subtrees = false;
1444
1445 bool *valp = static_cast<bool*>(data);
1446
1447 mark_or_check_tags (*tp, NULL, NULL, *valp);
1448
1449 return NULL_TREE;
1450 }
1451
1452 /* Set IDENTIFIER_MARKED on all the ABI tags on T and its enclosing
1453 scopes. */
1454
1455 static void
1456 mark_abi_tags (tree t, bool val)
1457 {
1458 mark_or_check_tags (t, NULL, NULL, val);
1459 if (DECL_P (t))
1460 {
1461 if (DECL_LANG_SPECIFIC (t) && DECL_USE_TEMPLATE (t)
1462 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (t)))
1463 {
1464 /* Template arguments are part of the signature. */
1465 tree level = INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (t));
1466 for (int j = 0; j < TREE_VEC_LENGTH (level); ++j)
1467 {
1468 tree arg = TREE_VEC_ELT (level, j);
1469 cp_walk_tree_without_duplicates (&arg, mark_abi_tags_r, &val);
1470 }
1471 }
1472 if (TREE_CODE (t) == FUNCTION_DECL)
1473 /* A function's parameter types are part of the signature, so
1474 we don't need to inherit any tags that are also in them. */
1475 for (tree arg = FUNCTION_FIRST_USER_PARMTYPE (t); arg;
1476 arg = TREE_CHAIN (arg))
1477 cp_walk_tree_without_duplicates (&TREE_VALUE (arg),
1478 mark_abi_tags_r, &val);
1479 }
1480 }
1481
1482 /* Check that T has all the ABI tags that subobject SUBOB has, or
1483 warn if not. If T is a (variable or function) declaration, also
1484 return any missing tags, and add them to T if JUST_CHECKING is false. */
1485
1486 static tree
1487 check_abi_tags (tree t, tree subob, bool just_checking = false)
1488 {
1489 bool inherit = DECL_P (t);
1490
1491 if (!inherit && !warn_abi_tag)
1492 return NULL_TREE;
1493
1494 tree decl = TYPE_P (t) ? TYPE_NAME (t) : t;
1495 if (!TREE_PUBLIC (decl))
1496 /* No need to worry about things local to this TU. */
1497 return NULL_TREE;
1498
1499 mark_abi_tags (t, true);
1500
1501 tree subtype = TYPE_P (subob) ? subob : TREE_TYPE (subob);
1502 struct abi_tag_data data = { t, subob, error_mark_node };
1503 if (inherit)
1504 data.tags = NULL_TREE;
1505
1506 cp_walk_tree_without_duplicates (&subtype, find_abi_tags_r, &data);
1507
1508 if (!(inherit && data.tags))
1509 /* We don't need to do anything with data.tags. */;
1510 else if (just_checking)
1511 for (tree t = data.tags; t; t = TREE_CHAIN (t))
1512 {
1513 tree id = get_identifier (TREE_STRING_POINTER (TREE_VALUE (t)));
1514 IDENTIFIER_MARKED (id) = false;
1515 }
1516 else
1517 {
1518 tree attr = lookup_attribute ("abi_tag", DECL_ATTRIBUTES (t));
1519 if (attr)
1520 TREE_VALUE (attr) = chainon (data.tags, TREE_VALUE (attr));
1521 else
1522 DECL_ATTRIBUTES (t)
1523 = tree_cons (get_identifier ("abi_tag"), data.tags,
1524 DECL_ATTRIBUTES (t));
1525 }
1526
1527 mark_abi_tags (t, false);
1528
1529 return data.tags;
1530 }
1531
1532 /* Check that DECL has all the ABI tags that are used in parts of its type
1533 that are not reflected in its mangled name. */
1534
1535 void
1536 check_abi_tags (tree decl)
1537 {
1538 if (VAR_P (decl))
1539 check_abi_tags (decl, TREE_TYPE (decl));
1540 else if (TREE_CODE (decl) == FUNCTION_DECL
1541 && !DECL_CONV_FN_P (decl)
1542 && !mangle_return_type_p (decl))
1543 check_abi_tags (decl, TREE_TYPE (TREE_TYPE (decl)));
1544 }
1545
1546 /* Return any ABI tags that are used in parts of the type of DECL
1547 that are not reflected in its mangled name. This function is only
1548 used in backward-compatible mangling for ABI <11. */
1549
1550 tree
1551 missing_abi_tags (tree decl)
1552 {
1553 if (VAR_P (decl))
1554 return check_abi_tags (decl, TREE_TYPE (decl), true);
1555 else if (TREE_CODE (decl) == FUNCTION_DECL
1556 /* Don't check DECL_CONV_FN_P here like we do in check_abi_tags, so
1557 that we can use this function for setting need_abi_warning
1558 regardless of the current flag_abi_version. */
1559 && !mangle_return_type_p (decl))
1560 return check_abi_tags (decl, TREE_TYPE (TREE_TYPE (decl)), true);
1561 else
1562 return NULL_TREE;
1563 }
1564
1565 void
1566 inherit_targ_abi_tags (tree t)
1567 {
1568 if (!CLASS_TYPE_P (t)
1569 || CLASSTYPE_TEMPLATE_INFO (t) == NULL_TREE)
1570 return;
1571
1572 mark_abi_tags (t, true);
1573
1574 tree args = CLASSTYPE_TI_ARGS (t);
1575 struct abi_tag_data data = { t, NULL_TREE, NULL_TREE };
1576 for (int i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
1577 {
1578 tree level = TMPL_ARGS_LEVEL (args, i+1);
1579 for (int j = 0; j < TREE_VEC_LENGTH (level); ++j)
1580 {
1581 tree arg = TREE_VEC_ELT (level, j);
1582 data.subob = arg;
1583 cp_walk_tree_without_duplicates (&arg, find_abi_tags_r, &data);
1584 }
1585 }
1586
1587 // If we found some tags on our template arguments, add them to our
1588 // abi_tag attribute.
1589 if (data.tags)
1590 {
1591 tree attr = lookup_attribute ("abi_tag", TYPE_ATTRIBUTES (t));
1592 if (attr)
1593 TREE_VALUE (attr) = chainon (data.tags, TREE_VALUE (attr));
1594 else
1595 TYPE_ATTRIBUTES (t)
1596 = tree_cons (get_identifier ("abi_tag"), data.tags,
1597 TYPE_ATTRIBUTES (t));
1598 }
1599
1600 mark_abi_tags (t, false);
1601 }
1602
1603 /* Return true, iff class T has a non-virtual destructor that is
1604 accessible from outside the class heirarchy (i.e. is public, or
1605 there's a suitable friend. */
1606
1607 static bool
1608 accessible_nvdtor_p (tree t)
1609 {
1610 tree dtor = CLASSTYPE_DESTRUCTOR (t);
1611
1612 /* An implicitly declared destructor is always public. And,
1613 if it were virtual, we would have created it by now. */
1614 if (!dtor)
1615 return true;
1616
1617 if (DECL_VINDEX (dtor))
1618 return false; /* Virtual */
1619
1620 if (!TREE_PRIVATE (dtor) && !TREE_PROTECTED (dtor))
1621 return true; /* Public */
1622
1623 if (CLASSTYPE_FRIEND_CLASSES (t)
1624 || DECL_FRIENDLIST (TYPE_MAIN_DECL (t)))
1625 return true; /* Has friends */
1626
1627 return false;
1628 }
1629
1630 /* Run through the base classes of T, updating CANT_HAVE_CONST_CTOR_P,
1631 and NO_CONST_ASN_REF_P. Also set flag bits in T based on
1632 properties of the bases. */
1633
1634 static void
1635 check_bases (tree t,
1636 int* cant_have_const_ctor_p,
1637 int* no_const_asn_ref_p)
1638 {
1639 int i;
1640 bool seen_non_virtual_nearly_empty_base_p = 0;
1641 int seen_tm_mask = 0;
1642 tree base_binfo;
1643 tree binfo;
1644 tree field = NULL_TREE;
1645
1646 if (!CLASSTYPE_NON_STD_LAYOUT (t))
1647 for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
1648 if (TREE_CODE (field) == FIELD_DECL)
1649 break;
1650
1651 for (binfo = TYPE_BINFO (t), i = 0;
1652 BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
1653 {
1654 tree basetype = TREE_TYPE (base_binfo);
1655
1656 gcc_assert (COMPLETE_TYPE_P (basetype));
1657
1658 if (CLASSTYPE_FINAL (basetype))
1659 error ("cannot derive from %<final%> base %qT in derived type %qT",
1660 basetype, t);
1661
1662 /* If any base class is non-literal, so is the derived class. */
1663 if (!CLASSTYPE_LITERAL_P (basetype))
1664 CLASSTYPE_LITERAL_P (t) = false;
1665
1666 /* If the base class doesn't have copy constructors or
1667 assignment operators that take const references, then the
1668 derived class cannot have such a member automatically
1669 generated. */
1670 if (TYPE_HAS_COPY_CTOR (basetype)
1671 && ! TYPE_HAS_CONST_COPY_CTOR (basetype))
1672 *cant_have_const_ctor_p = 1;
1673 if (TYPE_HAS_COPY_ASSIGN (basetype)
1674 && !TYPE_HAS_CONST_COPY_ASSIGN (basetype))
1675 *no_const_asn_ref_p = 1;
1676
1677 if (BINFO_VIRTUAL_P (base_binfo))
1678 /* A virtual base does not effect nearly emptiness. */
1679 ;
1680 else if (CLASSTYPE_NEARLY_EMPTY_P (basetype))
1681 {
1682 if (seen_non_virtual_nearly_empty_base_p)
1683 /* And if there is more than one nearly empty base, then the
1684 derived class is not nearly empty either. */
1685 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
1686 else
1687 /* Remember we've seen one. */
1688 seen_non_virtual_nearly_empty_base_p = 1;
1689 }
1690 else if (!is_empty_class (basetype))
1691 /* If the base class is not empty or nearly empty, then this
1692 class cannot be nearly empty. */
1693 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
1694
1695 /* A lot of properties from the bases also apply to the derived
1696 class. */
1697 TYPE_NEEDS_CONSTRUCTING (t) |= TYPE_NEEDS_CONSTRUCTING (basetype);
1698 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t)
1699 |= TYPE_HAS_NONTRIVIAL_DESTRUCTOR (basetype);
1700 TYPE_HAS_COMPLEX_COPY_ASSIGN (t)
1701 |= (TYPE_HAS_COMPLEX_COPY_ASSIGN (basetype)
1702 || !TYPE_HAS_COPY_ASSIGN (basetype));
1703 TYPE_HAS_COMPLEX_COPY_CTOR (t) |= (TYPE_HAS_COMPLEX_COPY_CTOR (basetype)
1704 || !TYPE_HAS_COPY_CTOR (basetype));
1705 TYPE_HAS_COMPLEX_MOVE_ASSIGN (t)
1706 |= TYPE_HAS_COMPLEX_MOVE_ASSIGN (basetype);
1707 TYPE_HAS_COMPLEX_MOVE_CTOR (t) |= TYPE_HAS_COMPLEX_MOVE_CTOR (basetype);
1708 TYPE_POLYMORPHIC_P (t) |= TYPE_POLYMORPHIC_P (basetype);
1709 CLASSTYPE_CONTAINS_EMPTY_CLASS_P (t)
1710 |= CLASSTYPE_CONTAINS_EMPTY_CLASS_P (basetype);
1711 TYPE_HAS_COMPLEX_DFLT (t) |= (!TYPE_HAS_DEFAULT_CONSTRUCTOR (basetype)
1712 || TYPE_HAS_COMPLEX_DFLT (basetype));
1713 SET_CLASSTYPE_READONLY_FIELDS_NEED_INIT
1714 (t, CLASSTYPE_READONLY_FIELDS_NEED_INIT (t)
1715 | CLASSTYPE_READONLY_FIELDS_NEED_INIT (basetype));
1716 SET_CLASSTYPE_REF_FIELDS_NEED_INIT
1717 (t, CLASSTYPE_REF_FIELDS_NEED_INIT (t)
1718 | CLASSTYPE_REF_FIELDS_NEED_INIT (basetype));
1719 if (TYPE_HAS_MUTABLE_P (basetype))
1720 CLASSTYPE_HAS_MUTABLE (t) = 1;
1721
1722 /* A standard-layout class is a class that:
1723 ...
1724 * has no non-standard-layout base classes, */
1725 CLASSTYPE_NON_STD_LAYOUT (t) |= CLASSTYPE_NON_STD_LAYOUT (basetype);
1726 if (!CLASSTYPE_NON_STD_LAYOUT (t))
1727 {
1728 tree basefield;
1729 /* ...has no base classes of the same type as the first non-static
1730 data member... */
1731 if (field && DECL_CONTEXT (field) == t
1732 && (same_type_ignoring_top_level_qualifiers_p
1733 (TREE_TYPE (field), basetype)))
1734 CLASSTYPE_NON_STD_LAYOUT (t) = 1;
1735 else
1736 /* ...either has no non-static data members in the most-derived
1737 class and at most one base class with non-static data
1738 members, or has no base classes with non-static data
1739 members */
1740 for (basefield = TYPE_FIELDS (basetype); basefield;
1741 basefield = DECL_CHAIN (basefield))
1742 if (TREE_CODE (basefield) == FIELD_DECL
1743 && !(DECL_FIELD_IS_BASE (basefield)
1744 && integer_zerop (DECL_SIZE (basefield))))
1745 {
1746 if (field)
1747 CLASSTYPE_NON_STD_LAYOUT (t) = 1;
1748 else
1749 field = basefield;
1750 break;
1751 }
1752 }
1753
1754 /* Don't bother collecting tm attributes if transactional memory
1755 support is not enabled. */
1756 if (flag_tm)
1757 {
1758 tree tm_attr = find_tm_attribute (TYPE_ATTRIBUTES (basetype));
1759 if (tm_attr)
1760 seen_tm_mask |= tm_attr_to_mask (tm_attr);
1761 }
1762
1763 check_abi_tags (t, basetype);
1764 }
1765
1766 /* If one of the base classes had TM attributes, and the current class
1767 doesn't define its own, then the current class inherits one. */
1768 if (seen_tm_mask && !find_tm_attribute (TYPE_ATTRIBUTES (t)))
1769 {
1770 tree tm_attr = tm_mask_to_attr (least_bit_hwi (seen_tm_mask));
1771 TYPE_ATTRIBUTES (t) = tree_cons (tm_attr, NULL, TYPE_ATTRIBUTES (t));
1772 }
1773 }
1774
1775 /* Determine all the primary bases within T. Sets BINFO_PRIMARY_BASE_P for
1776 those that are primaries. Sets BINFO_LOST_PRIMARY_P for those
1777 that have had a nearly-empty virtual primary base stolen by some
1778 other base in the hierarchy. Determines CLASSTYPE_PRIMARY_BASE for
1779 T. */
1780
1781 static void
1782 determine_primary_bases (tree t)
1783 {
1784 unsigned i;
1785 tree primary = NULL_TREE;
1786 tree type_binfo = TYPE_BINFO (t);
1787 tree base_binfo;
1788
1789 /* Determine the primary bases of our bases. */
1790 for (base_binfo = TREE_CHAIN (type_binfo); base_binfo;
1791 base_binfo = TREE_CHAIN (base_binfo))
1792 {
1793 tree primary = CLASSTYPE_PRIMARY_BINFO (BINFO_TYPE (base_binfo));
1794
1795 /* See if we're the non-virtual primary of our inheritance
1796 chain. */
1797 if (!BINFO_VIRTUAL_P (base_binfo))
1798 {
1799 tree parent = BINFO_INHERITANCE_CHAIN (base_binfo);
1800 tree parent_primary = CLASSTYPE_PRIMARY_BINFO (BINFO_TYPE (parent));
1801
1802 if (parent_primary
1803 && SAME_BINFO_TYPE_P (BINFO_TYPE (base_binfo),
1804 BINFO_TYPE (parent_primary)))
1805 /* We are the primary binfo. */
1806 BINFO_PRIMARY_P (base_binfo) = 1;
1807 }
1808 /* Determine if we have a virtual primary base, and mark it so.
1809 */
1810 if (primary && BINFO_VIRTUAL_P (primary))
1811 {
1812 tree this_primary = copied_binfo (primary, base_binfo);
1813
1814 if (BINFO_PRIMARY_P (this_primary))
1815 /* Someone already claimed this base. */
1816 BINFO_LOST_PRIMARY_P (base_binfo) = 1;
1817 else
1818 {
1819 tree delta;
1820
1821 BINFO_PRIMARY_P (this_primary) = 1;
1822 BINFO_INHERITANCE_CHAIN (this_primary) = base_binfo;
1823
1824 /* A virtual binfo might have been copied from within
1825 another hierarchy. As we're about to use it as a
1826 primary base, make sure the offsets match. */
1827 delta = size_diffop_loc (input_location,
1828 fold_convert (ssizetype,
1829 BINFO_OFFSET (base_binfo)),
1830 fold_convert (ssizetype,
1831 BINFO_OFFSET (this_primary)));
1832
1833 propagate_binfo_offsets (this_primary, delta);
1834 }
1835 }
1836 }
1837
1838 /* First look for a dynamic direct non-virtual base. */
1839 for (i = 0; BINFO_BASE_ITERATE (type_binfo, i, base_binfo); i++)
1840 {
1841 tree basetype = BINFO_TYPE (base_binfo);
1842
1843 if (TYPE_CONTAINS_VPTR_P (basetype) && !BINFO_VIRTUAL_P (base_binfo))
1844 {
1845 primary = base_binfo;
1846 goto found;
1847 }
1848 }
1849
1850 /* A "nearly-empty" virtual base class can be the primary base
1851 class, if no non-virtual polymorphic base can be found. Look for
1852 a nearly-empty virtual dynamic base that is not already a primary
1853 base of something in the hierarchy. If there is no such base,
1854 just pick the first nearly-empty virtual base. */
1855
1856 for (base_binfo = TREE_CHAIN (type_binfo); base_binfo;
1857 base_binfo = TREE_CHAIN (base_binfo))
1858 if (BINFO_VIRTUAL_P (base_binfo)
1859 && CLASSTYPE_NEARLY_EMPTY_P (BINFO_TYPE (base_binfo)))
1860 {
1861 if (!BINFO_PRIMARY_P (base_binfo))
1862 {
1863 /* Found one that is not primary. */
1864 primary = base_binfo;
1865 goto found;
1866 }
1867 else if (!primary)
1868 /* Remember the first candidate. */
1869 primary = base_binfo;
1870 }
1871
1872 found:
1873 /* If we've got a primary base, use it. */
1874 if (primary)
1875 {
1876 tree basetype = BINFO_TYPE (primary);
1877
1878 CLASSTYPE_PRIMARY_BINFO (t) = primary;
1879 if (BINFO_PRIMARY_P (primary))
1880 /* We are stealing a primary base. */
1881 BINFO_LOST_PRIMARY_P (BINFO_INHERITANCE_CHAIN (primary)) = 1;
1882 BINFO_PRIMARY_P (primary) = 1;
1883 if (BINFO_VIRTUAL_P (primary))
1884 {
1885 tree delta;
1886
1887 BINFO_INHERITANCE_CHAIN (primary) = type_binfo;
1888 /* A virtual binfo might have been copied from within
1889 another hierarchy. As we're about to use it as a primary
1890 base, make sure the offsets match. */
1891 delta = size_diffop_loc (input_location, ssize_int (0),
1892 fold_convert (ssizetype, BINFO_OFFSET (primary)));
1893
1894 propagate_binfo_offsets (primary, delta);
1895 }
1896
1897 primary = TYPE_BINFO (basetype);
1898
1899 TYPE_VFIELD (t) = TYPE_VFIELD (basetype);
1900 BINFO_VTABLE (type_binfo) = BINFO_VTABLE (primary);
1901 BINFO_VIRTUALS (type_binfo) = BINFO_VIRTUALS (primary);
1902 }
1903 }
1904
1905 /* Update the variant types of T. */
1906
1907 void
1908 fixup_type_variants (tree t)
1909 {
1910 tree variants;
1911
1912 if (!t)
1913 return;
1914
1915 for (variants = TYPE_NEXT_VARIANT (t);
1916 variants;
1917 variants = TYPE_NEXT_VARIANT (variants))
1918 {
1919 /* These fields are in the _TYPE part of the node, not in
1920 the TYPE_LANG_SPECIFIC component, so they are not shared. */
1921 TYPE_HAS_USER_CONSTRUCTOR (variants) = TYPE_HAS_USER_CONSTRUCTOR (t);
1922 TYPE_NEEDS_CONSTRUCTING (variants) = TYPE_NEEDS_CONSTRUCTING (t);
1923 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (variants)
1924 = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t);
1925
1926 TYPE_POLYMORPHIC_P (variants) = TYPE_POLYMORPHIC_P (t);
1927
1928 TYPE_BINFO (variants) = TYPE_BINFO (t);
1929
1930 /* Copy whatever these are holding today. */
1931 TYPE_VFIELD (variants) = TYPE_VFIELD (t);
1932 TYPE_FIELDS (variants) = TYPE_FIELDS (t);
1933 }
1934 }
1935
1936 /* KLASS is a class that we're applying may_alias to after the body is
1937 parsed. Fixup any POINTER_TO and REFERENCE_TO types. The
1938 canonical type(s) will be implicitly updated. */
1939
1940 static void
1941 fixup_may_alias (tree klass)
1942 {
1943 tree t, v;
1944
1945 for (t = TYPE_POINTER_TO (klass); t; t = TYPE_NEXT_PTR_TO (t))
1946 for (v = TYPE_MAIN_VARIANT (t); v; v = TYPE_NEXT_VARIANT (v))
1947 TYPE_REF_CAN_ALIAS_ALL (v) = true;
1948 for (t = TYPE_REFERENCE_TO (klass); t; t = TYPE_NEXT_REF_TO (t))
1949 for (v = TYPE_MAIN_VARIANT (t); v; v = TYPE_NEXT_VARIANT (v))
1950 TYPE_REF_CAN_ALIAS_ALL (v) = true;
1951 }
1952
1953 /* Early variant fixups: we apply attributes at the beginning of the class
1954 definition, and we need to fix up any variants that have already been
1955 made via elaborated-type-specifier so that check_qualified_type works. */
1956
1957 void
1958 fixup_attribute_variants (tree t)
1959 {
1960 tree variants;
1961
1962 if (!t)
1963 return;
1964
1965 tree attrs = TYPE_ATTRIBUTES (t);
1966 unsigned align = TYPE_ALIGN (t);
1967 bool user_align = TYPE_USER_ALIGN (t);
1968 bool may_alias = lookup_attribute ("may_alias", attrs);
1969
1970 if (may_alias)
1971 fixup_may_alias (t);
1972
1973 for (variants = TYPE_NEXT_VARIANT (t);
1974 variants;
1975 variants = TYPE_NEXT_VARIANT (variants))
1976 {
1977 /* These are the two fields that check_qualified_type looks at and
1978 are affected by attributes. */
1979 TYPE_ATTRIBUTES (variants) = attrs;
1980 unsigned valign = align;
1981 if (TYPE_USER_ALIGN (variants))
1982 valign = MAX (valign, TYPE_ALIGN (variants));
1983 else
1984 TYPE_USER_ALIGN (variants) = user_align;
1985 SET_TYPE_ALIGN (variants, valign);
1986 if (may_alias)
1987 fixup_may_alias (variants);
1988 }
1989 }
1990 \f
1991 /* Set memoizing fields and bits of T (and its variants) for later
1992 use. */
1993
1994 static void
1995 finish_struct_bits (tree t)
1996 {
1997 /* Fix up variants (if any). */
1998 fixup_type_variants (t);
1999
2000 if (BINFO_N_BASE_BINFOS (TYPE_BINFO (t)) && TYPE_POLYMORPHIC_P (t))
2001 /* For a class w/o baseclasses, 'finish_struct' has set
2002 CLASSTYPE_PURE_VIRTUALS correctly (by definition).
2003 Similarly for a class whose base classes do not have vtables.
2004 When neither of these is true, we might have removed abstract
2005 virtuals (by providing a definition), added some (by declaring
2006 new ones), or redeclared ones from a base class. We need to
2007 recalculate what's really an abstract virtual at this point (by
2008 looking in the vtables). */
2009 get_pure_virtuals (t);
2010
2011 /* If this type has a copy constructor or a destructor, force its
2012 mode to be BLKmode, and force its TREE_ADDRESSABLE bit to be
2013 nonzero. This will cause it to be passed by invisible reference
2014 and prevent it from being returned in a register. */
2015 if (type_has_nontrivial_copy_init (t)
2016 || TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t))
2017 {
2018 tree variants;
2019 SET_DECL_MODE (TYPE_MAIN_DECL (t), BLKmode);
2020 for (variants = t; variants; variants = TYPE_NEXT_VARIANT (variants))
2021 {
2022 SET_TYPE_MODE (variants, BLKmode);
2023 TREE_ADDRESSABLE (variants) = 1;
2024 }
2025 }
2026 }
2027
2028 /* Issue warnings about T having private constructors, but no friends,
2029 and so forth.
2030
2031 HAS_NONPRIVATE_METHOD is nonzero if T has any non-private methods or
2032 static members. HAS_NONPRIVATE_STATIC_FN is nonzero if T has any
2033 non-private static member functions. */
2034
2035 static void
2036 maybe_warn_about_overly_private_class (tree t)
2037 {
2038 int has_member_fn = 0;
2039 int has_nonprivate_method = 0;
2040
2041 if (!warn_ctor_dtor_privacy
2042 /* If the class has friends, those entities might create and
2043 access instances, so we should not warn. */
2044 || (CLASSTYPE_FRIEND_CLASSES (t)
2045 || DECL_FRIENDLIST (TYPE_MAIN_DECL (t)))
2046 /* We will have warned when the template was declared; there's
2047 no need to warn on every instantiation. */
2048 || CLASSTYPE_TEMPLATE_INSTANTIATION (t))
2049 /* There's no reason to even consider warning about this
2050 class. */
2051 return;
2052
2053 /* We only issue one warning, if more than one applies, because
2054 otherwise, on code like:
2055
2056 class A {
2057 // Oops - forgot `public:'
2058 A();
2059 A(const A&);
2060 ~A();
2061 };
2062
2063 we warn several times about essentially the same problem. */
2064
2065 /* Check to see if all (non-constructor, non-destructor) member
2066 functions are private. (Since there are no friends or
2067 non-private statics, we can't ever call any of the private member
2068 functions.) */
2069 for (tree fn = TYPE_FIELDS (t); fn; fn = DECL_CHAIN (fn))
2070 if (!DECL_DECLARES_FUNCTION_P (fn))
2071 /* Not a function. */;
2072 else if (DECL_ARTIFICIAL (fn))
2073 /* We're not interested in compiler-generated methods; they don't
2074 provide any way to call private members. */;
2075 else if (!TREE_PRIVATE (fn))
2076 {
2077 if (DECL_STATIC_FUNCTION_P (fn))
2078 /* A non-private static member function is just like a
2079 friend; it can create and invoke private member
2080 functions, and be accessed without a class
2081 instance. */
2082 return;
2083
2084 has_nonprivate_method = 1;
2085 /* Keep searching for a static member function. */
2086 }
2087 else if (!DECL_CONSTRUCTOR_P (fn) && !DECL_DESTRUCTOR_P (fn))
2088 has_member_fn = 1;
2089
2090 if (!has_nonprivate_method && has_member_fn)
2091 {
2092 /* There are no non-private methods, and there's at least one
2093 private member function that isn't a constructor or
2094 destructor. (If all the private members are
2095 constructors/destructors we want to use the code below that
2096 issues error messages specifically referring to
2097 constructors/destructors.) */
2098 unsigned i;
2099 tree binfo = TYPE_BINFO (t);
2100
2101 for (i = 0; i != BINFO_N_BASE_BINFOS (binfo); i++)
2102 if (BINFO_BASE_ACCESS (binfo, i) != access_private_node)
2103 {
2104 has_nonprivate_method = 1;
2105 break;
2106 }
2107 if (!has_nonprivate_method)
2108 {
2109 warning (OPT_Wctor_dtor_privacy,
2110 "all member functions in class %qT are private", t);
2111 return;
2112 }
2113 }
2114
2115 /* Even if some of the member functions are non-private, the class
2116 won't be useful for much if all the constructors or destructors
2117 are private: such an object can never be created or destroyed. */
2118 if (tree dtor = CLASSTYPE_DESTRUCTOR (t))
2119 if (TREE_PRIVATE (dtor))
2120 {
2121 warning (OPT_Wctor_dtor_privacy,
2122 "%q#T only defines a private destructor and has no friends",
2123 t);
2124 return;
2125 }
2126
2127 /* Warn about classes that have private constructors and no friends. */
2128 if (TYPE_HAS_USER_CONSTRUCTOR (t)
2129 /* Implicitly generated constructors are always public. */
2130 && !CLASSTYPE_LAZY_DEFAULT_CTOR (t))
2131 {
2132 bool nonprivate_ctor = false;
2133 tree copy_or_move = NULL_TREE;
2134
2135 /* If a non-template class does not define a copy
2136 constructor, one is defined for it, enabling it to avoid
2137 this warning. For a template class, this does not
2138 happen, and so we would normally get a warning on:
2139
2140 template <class T> class C { private: C(); };
2141
2142 To avoid this asymmetry, we check TYPE_HAS_COPY_CTOR. All
2143 complete non-template or fully instantiated classes have this
2144 flag set. */
2145 if (!TYPE_HAS_COPY_CTOR (t))
2146 nonprivate_ctor = true;
2147 else
2148 for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (t));
2149 !nonprivate_ctor && iter; ++iter)
2150 if (TREE_PRIVATE (*iter))
2151 continue;
2152 else if (copy_fn_p (*iter) || move_fn_p (*iter))
2153 /* Ideally, we wouldn't count any constructor that takes
2154 an argument of the class type as a parameter, because
2155 such things cannot be used to construct an instance of
2156 the class unless you already have one. */
2157 copy_or_move = *iter;
2158 else
2159 nonprivate_ctor = true;
2160
2161 if (!nonprivate_ctor)
2162 {
2163 warning (OPT_Wctor_dtor_privacy,
2164 "%q#T only defines private constructors and has no friends",
2165 t);
2166 if (copy_or_move)
2167 inform (DECL_SOURCE_LOCATION (copy_or_move),
2168 "%q#D is public, but requires an existing %q#T object",
2169 copy_or_move, t);
2170 return;
2171 }
2172 }
2173 }
2174
2175 /* Make BINFO's vtable have N entries, including RTTI entries,
2176 vbase and vcall offsets, etc. Set its type and call the back end
2177 to lay it out. */
2178
2179 static void
2180 layout_vtable_decl (tree binfo, int n)
2181 {
2182 tree atype;
2183 tree vtable;
2184
2185 atype = build_array_of_n_type (vtable_entry_type, n);
2186 layout_type (atype);
2187
2188 /* We may have to grow the vtable. */
2189 vtable = get_vtbl_decl_for_binfo (binfo);
2190 if (!same_type_p (TREE_TYPE (vtable), atype))
2191 {
2192 TREE_TYPE (vtable) = atype;
2193 DECL_SIZE (vtable) = DECL_SIZE_UNIT (vtable) = NULL_TREE;
2194 layout_decl (vtable, 0);
2195 }
2196 }
2197
2198 /* True iff FNDECL and BASE_FNDECL (both non-static member functions)
2199 have the same signature. */
2200
2201 int
2202 same_signature_p (const_tree fndecl, const_tree base_fndecl)
2203 {
2204 /* One destructor overrides another if they are the same kind of
2205 destructor. */
2206 if (DECL_DESTRUCTOR_P (base_fndecl) && DECL_DESTRUCTOR_P (fndecl)
2207 && special_function_p (base_fndecl) == special_function_p (fndecl))
2208 return 1;
2209 /* But a non-destructor never overrides a destructor, nor vice
2210 versa, nor do different kinds of destructors override
2211 one-another. For example, a complete object destructor does not
2212 override a deleting destructor. */
2213 if (DECL_DESTRUCTOR_P (base_fndecl) || DECL_DESTRUCTOR_P (fndecl))
2214 return 0;
2215
2216 if (DECL_NAME (fndecl) == DECL_NAME (base_fndecl)
2217 || (DECL_CONV_FN_P (fndecl)
2218 && DECL_CONV_FN_P (base_fndecl)
2219 && same_type_p (DECL_CONV_FN_TYPE (fndecl),
2220 DECL_CONV_FN_TYPE (base_fndecl))))
2221 {
2222 tree fntype = TREE_TYPE (fndecl);
2223 tree base_fntype = TREE_TYPE (base_fndecl);
2224 if (type_memfn_quals (fntype) == type_memfn_quals (base_fntype)
2225 && type_memfn_rqual (fntype) == type_memfn_rqual (base_fntype)
2226 && compparms (FUNCTION_FIRST_USER_PARMTYPE (fndecl),
2227 FUNCTION_FIRST_USER_PARMTYPE (base_fndecl)))
2228 return 1;
2229 }
2230 return 0;
2231 }
2232
2233 /* Returns TRUE if DERIVED is a binfo containing the binfo BASE as a
2234 subobject. */
2235
2236 static bool
2237 base_derived_from (tree derived, tree base)
2238 {
2239 tree probe;
2240
2241 for (probe = base; probe; probe = BINFO_INHERITANCE_CHAIN (probe))
2242 {
2243 if (probe == derived)
2244 return true;
2245 else if (BINFO_VIRTUAL_P (probe))
2246 /* If we meet a virtual base, we can't follow the inheritance
2247 any more. See if the complete type of DERIVED contains
2248 such a virtual base. */
2249 return (binfo_for_vbase (BINFO_TYPE (probe), BINFO_TYPE (derived))
2250 != NULL_TREE);
2251 }
2252 return false;
2253 }
2254
2255 struct find_final_overrider_data {
2256 /* The function for which we are trying to find a final overrider. */
2257 tree fn;
2258 /* The base class in which the function was declared. */
2259 tree declaring_base;
2260 /* The candidate overriders. */
2261 tree candidates;
2262 /* Path to most derived. */
2263 vec<tree> path;
2264 };
2265
2266 /* Add the overrider along the current path to FFOD->CANDIDATES.
2267 Returns true if an overrider was found; false otherwise. */
2268
2269 static bool
2270 dfs_find_final_overrider_1 (tree binfo,
2271 find_final_overrider_data *ffod,
2272 unsigned depth)
2273 {
2274 tree method;
2275
2276 /* If BINFO is not the most derived type, try a more derived class.
2277 A definition there will overrider a definition here. */
2278 if (depth)
2279 {
2280 depth--;
2281 if (dfs_find_final_overrider_1
2282 (ffod->path[depth], ffod, depth))
2283 return true;
2284 }
2285
2286 method = look_for_overrides_here (BINFO_TYPE (binfo), ffod->fn);
2287 if (method)
2288 {
2289 tree *candidate = &ffod->candidates;
2290
2291 /* Remove any candidates overridden by this new function. */
2292 while (*candidate)
2293 {
2294 /* If *CANDIDATE overrides METHOD, then METHOD
2295 cannot override anything else on the list. */
2296 if (base_derived_from (TREE_VALUE (*candidate), binfo))
2297 return true;
2298 /* If METHOD overrides *CANDIDATE, remove *CANDIDATE. */
2299 if (base_derived_from (binfo, TREE_VALUE (*candidate)))
2300 *candidate = TREE_CHAIN (*candidate);
2301 else
2302 candidate = &TREE_CHAIN (*candidate);
2303 }
2304
2305 /* Add the new function. */
2306 ffod->candidates = tree_cons (method, binfo, ffod->candidates);
2307 return true;
2308 }
2309
2310 return false;
2311 }
2312
2313 /* Called from find_final_overrider via dfs_walk. */
2314
2315 static tree
2316 dfs_find_final_overrider_pre (tree binfo, void *data)
2317 {
2318 find_final_overrider_data *ffod = (find_final_overrider_data *) data;
2319
2320 if (binfo == ffod->declaring_base)
2321 dfs_find_final_overrider_1 (binfo, ffod, ffod->path.length ());
2322 ffod->path.safe_push (binfo);
2323
2324 return NULL_TREE;
2325 }
2326
2327 static tree
2328 dfs_find_final_overrider_post (tree /*binfo*/, void *data)
2329 {
2330 find_final_overrider_data *ffod = (find_final_overrider_data *) data;
2331 ffod->path.pop ();
2332
2333 return NULL_TREE;
2334 }
2335
2336 /* Returns a TREE_LIST whose TREE_PURPOSE is the final overrider for
2337 FN and whose TREE_VALUE is the binfo for the base where the
2338 overriding occurs. BINFO (in the hierarchy dominated by the binfo
2339 DERIVED) is the base object in which FN is declared. */
2340
2341 static tree
2342 find_final_overrider (tree derived, tree binfo, tree fn)
2343 {
2344 find_final_overrider_data ffod;
2345
2346 /* Getting this right is a little tricky. This is valid:
2347
2348 struct S { virtual void f (); };
2349 struct T { virtual void f (); };
2350 struct U : public S, public T { };
2351
2352 even though calling `f' in `U' is ambiguous. But,
2353
2354 struct R { virtual void f(); };
2355 struct S : virtual public R { virtual void f (); };
2356 struct T : virtual public R { virtual void f (); };
2357 struct U : public S, public T { };
2358
2359 is not -- there's no way to decide whether to put `S::f' or
2360 `T::f' in the vtable for `R'.
2361
2362 The solution is to look at all paths to BINFO. If we find
2363 different overriders along any two, then there is a problem. */
2364 if (DECL_THUNK_P (fn))
2365 fn = THUNK_TARGET (fn);
2366
2367 /* Determine the depth of the hierarchy. */
2368 ffod.fn = fn;
2369 ffod.declaring_base = binfo;
2370 ffod.candidates = NULL_TREE;
2371 ffod.path.create (30);
2372
2373 dfs_walk_all (derived, dfs_find_final_overrider_pre,
2374 dfs_find_final_overrider_post, &ffod);
2375
2376 ffod.path.release ();
2377
2378 /* If there was no winner, issue an error message. */
2379 if (!ffod.candidates || TREE_CHAIN (ffod.candidates))
2380 return error_mark_node;
2381
2382 return ffod.candidates;
2383 }
2384
2385 /* Return the index of the vcall offset for FN when TYPE is used as a
2386 virtual base. */
2387
2388 static tree
2389 get_vcall_index (tree fn, tree type)
2390 {
2391 vec<tree_pair_s, va_gc> *indices = CLASSTYPE_VCALL_INDICES (type);
2392 tree_pair_p p;
2393 unsigned ix;
2394
2395 FOR_EACH_VEC_SAFE_ELT (indices, ix, p)
2396 if ((DECL_DESTRUCTOR_P (fn) && DECL_DESTRUCTOR_P (p->purpose))
2397 || same_signature_p (fn, p->purpose))
2398 return p->value;
2399
2400 /* There should always be an appropriate index. */
2401 gcc_unreachable ();
2402 }
2403
2404 /* Update an entry in the vtable for BINFO, which is in the hierarchy
2405 dominated by T. FN is the old function; VIRTUALS points to the
2406 corresponding position in the new BINFO_VIRTUALS list. IX is the index
2407 of that entry in the list. */
2408
2409 static void
2410 update_vtable_entry_for_fn (tree t, tree binfo, tree fn, tree* virtuals,
2411 unsigned ix)
2412 {
2413 tree b;
2414 tree overrider;
2415 tree delta;
2416 tree virtual_base;
2417 tree first_defn;
2418 tree overrider_fn, overrider_target;
2419 tree target_fn = DECL_THUNK_P (fn) ? THUNK_TARGET (fn) : fn;
2420 tree over_return, base_return;
2421 bool lost = false;
2422
2423 /* Find the nearest primary base (possibly binfo itself) which defines
2424 this function; this is the class the caller will convert to when
2425 calling FN through BINFO. */
2426 for (b = binfo; ; b = get_primary_binfo (b))
2427 {
2428 gcc_assert (b);
2429 if (look_for_overrides_here (BINFO_TYPE (b), target_fn))
2430 break;
2431
2432 /* The nearest definition is from a lost primary. */
2433 if (BINFO_LOST_PRIMARY_P (b))
2434 lost = true;
2435 }
2436 first_defn = b;
2437
2438 /* Find the final overrider. */
2439 overrider = find_final_overrider (TYPE_BINFO (t), b, target_fn);
2440 if (overrider == error_mark_node)
2441 {
2442 error ("no unique final overrider for %qD in %qT", target_fn, t);
2443 return;
2444 }
2445 overrider_target = overrider_fn = TREE_PURPOSE (overrider);
2446
2447 /* Check for adjusting covariant return types. */
2448 over_return = TREE_TYPE (TREE_TYPE (overrider_target));
2449 base_return = TREE_TYPE (TREE_TYPE (target_fn));
2450
2451 if (POINTER_TYPE_P (over_return)
2452 && TREE_CODE (over_return) == TREE_CODE (base_return)
2453 && CLASS_TYPE_P (TREE_TYPE (over_return))
2454 && CLASS_TYPE_P (TREE_TYPE (base_return))
2455 /* If the overrider is invalid, don't even try. */
2456 && !DECL_INVALID_OVERRIDER_P (overrider_target))
2457 {
2458 /* If FN is a covariant thunk, we must figure out the adjustment
2459 to the final base FN was converting to. As OVERRIDER_TARGET might
2460 also be converting to the return type of FN, we have to
2461 combine the two conversions here. */
2462 tree fixed_offset, virtual_offset;
2463
2464 over_return = TREE_TYPE (over_return);
2465 base_return = TREE_TYPE (base_return);
2466
2467 if (DECL_THUNK_P (fn))
2468 {
2469 gcc_assert (DECL_RESULT_THUNK_P (fn));
2470 fixed_offset = ssize_int (THUNK_FIXED_OFFSET (fn));
2471 virtual_offset = THUNK_VIRTUAL_OFFSET (fn);
2472 }
2473 else
2474 fixed_offset = virtual_offset = NULL_TREE;
2475
2476 if (virtual_offset)
2477 /* Find the equivalent binfo within the return type of the
2478 overriding function. We will want the vbase offset from
2479 there. */
2480 virtual_offset = binfo_for_vbase (BINFO_TYPE (virtual_offset),
2481 over_return);
2482 else if (!same_type_ignoring_top_level_qualifiers_p
2483 (over_return, base_return))
2484 {
2485 /* There was no existing virtual thunk (which takes
2486 precedence). So find the binfo of the base function's
2487 return type within the overriding function's return type.
2488 Fortunately we know the covariancy is valid (it
2489 has already been checked), so we can just iterate along
2490 the binfos, which have been chained in inheritance graph
2491 order. Of course it is lame that we have to repeat the
2492 search here anyway -- we should really be caching pieces
2493 of the vtable and avoiding this repeated work. */
2494 tree thunk_binfo, base_binfo;
2495
2496 /* Find the base binfo within the overriding function's
2497 return type. We will always find a thunk_binfo, except
2498 when the covariancy is invalid (which we will have
2499 already diagnosed). */
2500 for (base_binfo = TYPE_BINFO (base_return),
2501 thunk_binfo = TYPE_BINFO (over_return);
2502 thunk_binfo;
2503 thunk_binfo = TREE_CHAIN (thunk_binfo))
2504 if (SAME_BINFO_TYPE_P (BINFO_TYPE (thunk_binfo),
2505 BINFO_TYPE (base_binfo)))
2506 break;
2507
2508 /* See if virtual inheritance is involved. */
2509 for (virtual_offset = thunk_binfo;
2510 virtual_offset;
2511 virtual_offset = BINFO_INHERITANCE_CHAIN (virtual_offset))
2512 if (BINFO_VIRTUAL_P (virtual_offset))
2513 break;
2514
2515 if (virtual_offset
2516 || (thunk_binfo && !BINFO_OFFSET_ZEROP (thunk_binfo)))
2517 {
2518 tree offset = fold_convert (ssizetype, BINFO_OFFSET (thunk_binfo));
2519
2520 if (virtual_offset)
2521 {
2522 /* We convert via virtual base. Adjust the fixed
2523 offset to be from there. */
2524 offset =
2525 size_diffop (offset,
2526 fold_convert (ssizetype,
2527 BINFO_OFFSET (virtual_offset)));
2528 }
2529 if (fixed_offset)
2530 /* There was an existing fixed offset, this must be
2531 from the base just converted to, and the base the
2532 FN was thunking to. */
2533 fixed_offset = size_binop (PLUS_EXPR, fixed_offset, offset);
2534 else
2535 fixed_offset = offset;
2536 }
2537 }
2538
2539 if (fixed_offset || virtual_offset)
2540 /* Replace the overriding function with a covariant thunk. We
2541 will emit the overriding function in its own slot as
2542 well. */
2543 overrider_fn = make_thunk (overrider_target, /*this_adjusting=*/0,
2544 fixed_offset, virtual_offset);
2545 }
2546 else
2547 gcc_assert (DECL_INVALID_OVERRIDER_P (overrider_target) ||
2548 !DECL_THUNK_P (fn));
2549
2550 /* If we need a covariant thunk, then we may need to adjust first_defn.
2551 The ABI specifies that the thunks emitted with a function are
2552 determined by which bases the function overrides, so we need to be
2553 sure that we're using a thunk for some overridden base; even if we
2554 know that the necessary this adjustment is zero, there may not be an
2555 appropriate zero-this-adjustment thunk for us to use since thunks for
2556 overriding virtual bases always use the vcall offset.
2557
2558 Furthermore, just choosing any base that overrides this function isn't
2559 quite right, as this slot won't be used for calls through a type that
2560 puts a covariant thunk here. Calling the function through such a type
2561 will use a different slot, and that slot is the one that determines
2562 the thunk emitted for that base.
2563
2564 So, keep looking until we find the base that we're really overriding
2565 in this slot: the nearest primary base that doesn't use a covariant
2566 thunk in this slot. */
2567 if (overrider_target != overrider_fn)
2568 {
2569 if (BINFO_TYPE (b) == DECL_CONTEXT (overrider_target))
2570 /* We already know that the overrider needs a covariant thunk. */
2571 b = get_primary_binfo (b);
2572 for (; ; b = get_primary_binfo (b))
2573 {
2574 tree main_binfo = TYPE_BINFO (BINFO_TYPE (b));
2575 tree bv = chain_index (ix, BINFO_VIRTUALS (main_binfo));
2576 if (!DECL_THUNK_P (TREE_VALUE (bv)))
2577 break;
2578 if (BINFO_LOST_PRIMARY_P (b))
2579 lost = true;
2580 }
2581 first_defn = b;
2582 }
2583
2584 /* Assume that we will produce a thunk that convert all the way to
2585 the final overrider, and not to an intermediate virtual base. */
2586 virtual_base = NULL_TREE;
2587
2588 /* See if we can convert to an intermediate virtual base first, and then
2589 use the vcall offset located there to finish the conversion. */
2590 for (; b; b = BINFO_INHERITANCE_CHAIN (b))
2591 {
2592 /* If we find the final overrider, then we can stop
2593 walking. */
2594 if (SAME_BINFO_TYPE_P (BINFO_TYPE (b),
2595 BINFO_TYPE (TREE_VALUE (overrider))))
2596 break;
2597
2598 /* If we find a virtual base, and we haven't yet found the
2599 overrider, then there is a virtual base between the
2600 declaring base (first_defn) and the final overrider. */
2601 if (BINFO_VIRTUAL_P (b))
2602 {
2603 virtual_base = b;
2604 break;
2605 }
2606 }
2607
2608 /* Compute the constant adjustment to the `this' pointer. The
2609 `this' pointer, when this function is called, will point at BINFO
2610 (or one of its primary bases, which are at the same offset). */
2611 if (virtual_base)
2612 /* The `this' pointer needs to be adjusted from the declaration to
2613 the nearest virtual base. */
2614 delta = size_diffop_loc (input_location,
2615 fold_convert (ssizetype, BINFO_OFFSET (virtual_base)),
2616 fold_convert (ssizetype, BINFO_OFFSET (first_defn)));
2617 else if (lost)
2618 /* If the nearest definition is in a lost primary, we don't need an
2619 entry in our vtable. Except possibly in a constructor vtable,
2620 if we happen to get our primary back. In that case, the offset
2621 will be zero, as it will be a primary base. */
2622 delta = size_zero_node;
2623 else
2624 /* The `this' pointer needs to be adjusted from pointing to
2625 BINFO to pointing at the base where the final overrider
2626 appears. */
2627 delta = size_diffop_loc (input_location,
2628 fold_convert (ssizetype,
2629 BINFO_OFFSET (TREE_VALUE (overrider))),
2630 fold_convert (ssizetype, BINFO_OFFSET (binfo)));
2631
2632 modify_vtable_entry (t, binfo, overrider_fn, delta, virtuals);
2633
2634 if (virtual_base)
2635 BV_VCALL_INDEX (*virtuals)
2636 = get_vcall_index (overrider_target, BINFO_TYPE (virtual_base));
2637 else
2638 BV_VCALL_INDEX (*virtuals) = NULL_TREE;
2639
2640 BV_LOST_PRIMARY (*virtuals) = lost;
2641 }
2642
2643 /* Called from modify_all_vtables via dfs_walk. */
2644
2645 static tree
2646 dfs_modify_vtables (tree binfo, void* data)
2647 {
2648 tree t = (tree) data;
2649 tree virtuals;
2650 tree old_virtuals;
2651 unsigned ix;
2652
2653 if (!TYPE_CONTAINS_VPTR_P (BINFO_TYPE (binfo)))
2654 /* A base without a vtable needs no modification, and its bases
2655 are uninteresting. */
2656 return dfs_skip_bases;
2657
2658 if (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), t)
2659 && !CLASSTYPE_HAS_PRIMARY_BASE_P (t))
2660 /* Don't do the primary vtable, if it's new. */
2661 return NULL_TREE;
2662
2663 if (BINFO_PRIMARY_P (binfo) && !BINFO_VIRTUAL_P (binfo))
2664 /* There's no need to modify the vtable for a non-virtual primary
2665 base; we're not going to use that vtable anyhow. We do still
2666 need to do this for virtual primary bases, as they could become
2667 non-primary in a construction vtable. */
2668 return NULL_TREE;
2669
2670 make_new_vtable (t, binfo);
2671
2672 /* Now, go through each of the virtual functions in the virtual
2673 function table for BINFO. Find the final overrider, and update
2674 the BINFO_VIRTUALS list appropriately. */
2675 for (ix = 0, virtuals = BINFO_VIRTUALS (binfo),
2676 old_virtuals = BINFO_VIRTUALS (TYPE_BINFO (BINFO_TYPE (binfo)));
2677 virtuals;
2678 ix++, virtuals = TREE_CHAIN (virtuals),
2679 old_virtuals = TREE_CHAIN (old_virtuals))
2680 update_vtable_entry_for_fn (t,
2681 binfo,
2682 BV_FN (old_virtuals),
2683 &virtuals, ix);
2684
2685 return NULL_TREE;
2686 }
2687
2688 /* Update all of the primary and secondary vtables for T. Create new
2689 vtables as required, and initialize their RTTI information. Each
2690 of the functions in VIRTUALS is declared in T and may override a
2691 virtual function from a base class; find and modify the appropriate
2692 entries to point to the overriding functions. Returns a list, in
2693 declaration order, of the virtual functions that are declared in T,
2694 but do not appear in the primary base class vtable, and which
2695 should therefore be appended to the end of the vtable for T. */
2696
2697 static tree
2698 modify_all_vtables (tree t, tree virtuals)
2699 {
2700 tree binfo = TYPE_BINFO (t);
2701 tree *fnsp;
2702
2703 /* Mangle the vtable name before entering dfs_walk (c++/51884). */
2704 if (TYPE_CONTAINS_VPTR_P (t))
2705 get_vtable_decl (t, false);
2706
2707 /* Update all of the vtables. */
2708 dfs_walk_once (binfo, dfs_modify_vtables, NULL, t);
2709
2710 /* Add virtual functions not already in our primary vtable. These
2711 will be both those introduced by this class, and those overridden
2712 from secondary bases. It does not include virtuals merely
2713 inherited from secondary bases. */
2714 for (fnsp = &virtuals; *fnsp; )
2715 {
2716 tree fn = TREE_VALUE (*fnsp);
2717
2718 if (!value_member (fn, BINFO_VIRTUALS (binfo))
2719 || DECL_VINDEX (fn) == error_mark_node)
2720 {
2721 /* We don't need to adjust the `this' pointer when
2722 calling this function. */
2723 BV_DELTA (*fnsp) = integer_zero_node;
2724 BV_VCALL_INDEX (*fnsp) = NULL_TREE;
2725
2726 /* This is a function not already in our vtable. Keep it. */
2727 fnsp = &TREE_CHAIN (*fnsp);
2728 }
2729 else
2730 /* We've already got an entry for this function. Skip it. */
2731 *fnsp = TREE_CHAIN (*fnsp);
2732 }
2733
2734 return virtuals;
2735 }
2736
2737 /* Get the base virtual function declarations in T that have the
2738 indicated NAME. */
2739
2740 static void
2741 get_basefndecls (tree name, tree t, vec<tree> *base_fndecls)
2742 {
2743 bool found_decls = false;
2744
2745 /* Find virtual functions in T with the indicated NAME. */
2746 for (ovl_iterator iter (get_class_binding (t, name)); iter; ++iter)
2747 {
2748 tree method = *iter;
2749
2750 if (TREE_CODE (method) == FUNCTION_DECL && DECL_VINDEX (method))
2751 {
2752 base_fndecls->safe_push (method);
2753 found_decls = true;
2754 }
2755 }
2756
2757 if (found_decls)
2758 return;
2759
2760 int n_baseclasses = BINFO_N_BASE_BINFOS (TYPE_BINFO (t));
2761 for (int i = 0; i < n_baseclasses; i++)
2762 {
2763 tree basetype = BINFO_TYPE (BINFO_BASE_BINFO (TYPE_BINFO (t), i));
2764 get_basefndecls (name, basetype, base_fndecls);
2765 }
2766 }
2767
2768 /* If this declaration supersedes the declaration of
2769 a method declared virtual in the base class, then
2770 mark this field as being virtual as well. */
2771
2772 void
2773 check_for_override (tree decl, tree ctype)
2774 {
2775 bool overrides_found = false;
2776 if (TREE_CODE (decl) == TEMPLATE_DECL)
2777 /* In [temp.mem] we have:
2778
2779 A specialization of a member function template does not
2780 override a virtual function from a base class. */
2781 return;
2782 if ((DECL_DESTRUCTOR_P (decl)
2783 || IDENTIFIER_VIRTUAL_P (DECL_NAME (decl))
2784 || DECL_CONV_FN_P (decl))
2785 && look_for_overrides (ctype, decl)
2786 && !DECL_STATIC_FUNCTION_P (decl))
2787 /* Set DECL_VINDEX to a value that is neither an INTEGER_CST nor
2788 the error_mark_node so that we know it is an overriding
2789 function. */
2790 {
2791 DECL_VINDEX (decl) = decl;
2792 overrides_found = true;
2793 if (warn_override && !DECL_OVERRIDE_P (decl)
2794 && !DECL_DESTRUCTOR_P (decl))
2795 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wsuggest_override,
2796 "%qD can be marked override", decl);
2797 }
2798
2799 if (DECL_VIRTUAL_P (decl))
2800 {
2801 if (!DECL_VINDEX (decl))
2802 DECL_VINDEX (decl) = error_mark_node;
2803 IDENTIFIER_VIRTUAL_P (DECL_NAME (decl)) = 1;
2804 if (DECL_DESTRUCTOR_P (decl))
2805 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (ctype) = true;
2806 }
2807 else if (DECL_FINAL_P (decl))
2808 error ("%q+#D marked %<final%>, but is not virtual", decl);
2809 if (DECL_OVERRIDE_P (decl) && !overrides_found)
2810 error ("%q+#D marked %<override%>, but does not override", decl);
2811 }
2812
2813 /* Warn about hidden virtual functions that are not overridden in t.
2814 We know that constructors and destructors don't apply. */
2815
2816 static void
2817 warn_hidden (tree t)
2818 {
2819 if (vec<tree, va_gc> *member_vec = CLASSTYPE_MEMBER_VEC (t))
2820 for (unsigned ix = member_vec->length (); ix--;)
2821 {
2822 tree fns = (*member_vec)[ix];
2823
2824 if (!OVL_P (fns))
2825 continue;
2826
2827 tree name = OVL_NAME (fns);
2828 auto_vec<tree, 20> base_fndecls;
2829 tree base_binfo;
2830 tree binfo;
2831 unsigned j;
2832
2833 /* Iterate through all of the base classes looking for possibly
2834 hidden functions. */
2835 for (binfo = TYPE_BINFO (t), j = 0;
2836 BINFO_BASE_ITERATE (binfo, j, base_binfo); j++)
2837 {
2838 tree basetype = BINFO_TYPE (base_binfo);
2839 get_basefndecls (name, basetype, &base_fndecls);
2840 }
2841
2842 /* If there are no functions to hide, continue. */
2843 if (base_fndecls.is_empty ())
2844 continue;
2845
2846 /* Remove any overridden functions. */
2847 for (ovl_iterator iter (fns); iter; ++iter)
2848 {
2849 tree fndecl = *iter;
2850 if (TREE_CODE (fndecl) == FUNCTION_DECL
2851 && DECL_VINDEX (fndecl))
2852 {
2853 /* If the method from the base class has the same
2854 signature as the method from the derived class, it
2855 has been overridden. */
2856 for (size_t k = 0; k < base_fndecls.length (); k++)
2857 if (base_fndecls[k]
2858 && same_signature_p (fndecl, base_fndecls[k]))
2859 base_fndecls[k] = NULL_TREE;
2860 }
2861 }
2862
2863 /* Now give a warning for all base functions without overriders,
2864 as they are hidden. */
2865 tree base_fndecl;
2866 FOR_EACH_VEC_ELT (base_fndecls, j, base_fndecl)
2867 if (base_fndecl)
2868 {
2869 /* Here we know it is a hider, and no overrider exists. */
2870 warning_at (location_of (base_fndecl),
2871 OPT_Woverloaded_virtual,
2872 "%qD was hidden", base_fndecl);
2873 warning_at (location_of (fns),
2874 OPT_Woverloaded_virtual, " by %qD", fns);
2875 }
2876 }
2877 }
2878
2879 /* Recursive helper for finish_struct_anon. */
2880
2881 static void
2882 finish_struct_anon_r (tree field, bool complain)
2883 {
2884 bool is_union = TREE_CODE (TREE_TYPE (field)) == UNION_TYPE;
2885 tree elt = TYPE_FIELDS (TREE_TYPE (field));
2886 for (; elt; elt = DECL_CHAIN (elt))
2887 {
2888 /* We're generally only interested in entities the user
2889 declared, but we also find nested classes by noticing
2890 the TYPE_DECL that we create implicitly. You're
2891 allowed to put one anonymous union inside another,
2892 though, so we explicitly tolerate that. We use
2893 TYPE_UNNAMED_P rather than ANON_AGGR_TYPE_P so that
2894 we also allow unnamed types used for defining fields. */
2895 if (DECL_ARTIFICIAL (elt)
2896 && (!DECL_IMPLICIT_TYPEDEF_P (elt)
2897 || TYPE_UNNAMED_P (TREE_TYPE (elt))))
2898 continue;
2899
2900 if (TREE_CODE (elt) != FIELD_DECL)
2901 {
2902 /* We already complained about static data members in
2903 finish_static_data_member_decl. */
2904 if (complain && !VAR_P (elt))
2905 {
2906 if (is_union)
2907 permerror (DECL_SOURCE_LOCATION (elt),
2908 "%q#D invalid; an anonymous union can "
2909 "only have non-static data members", elt);
2910 else
2911 permerror (DECL_SOURCE_LOCATION (elt),
2912 "%q#D invalid; an anonymous struct can "
2913 "only have non-static data members", elt);
2914 }
2915 continue;
2916 }
2917
2918 if (complain)
2919 {
2920 if (TREE_PRIVATE (elt))
2921 {
2922 if (is_union)
2923 permerror (DECL_SOURCE_LOCATION (elt),
2924 "private member %q#D in anonymous union", elt);
2925 else
2926 permerror (DECL_SOURCE_LOCATION (elt),
2927 "private member %q#D in anonymous struct", elt);
2928 }
2929 else if (TREE_PROTECTED (elt))
2930 {
2931 if (is_union)
2932 permerror (DECL_SOURCE_LOCATION (elt),
2933 "protected member %q#D in anonymous union", elt);
2934 else
2935 permerror (DECL_SOURCE_LOCATION (elt),
2936 "protected member %q#D in anonymous struct", elt);
2937 }
2938 }
2939
2940 TREE_PRIVATE (elt) = TREE_PRIVATE (field);
2941 TREE_PROTECTED (elt) = TREE_PROTECTED (field);
2942
2943 /* Recurse into the anonymous aggregates to handle correctly
2944 access control (c++/24926):
2945
2946 class A {
2947 union {
2948 union {
2949 int i;
2950 };
2951 };
2952 };
2953
2954 int j=A().i; */
2955 if (DECL_NAME (elt) == NULL_TREE
2956 && ANON_AGGR_TYPE_P (TREE_TYPE (elt)))
2957 finish_struct_anon_r (elt, /*complain=*/false);
2958 }
2959 }
2960
2961 /* Check for things that are invalid. There are probably plenty of other
2962 things we should check for also. */
2963
2964 static void
2965 finish_struct_anon (tree t)
2966 {
2967 for (tree field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
2968 {
2969 if (TREE_STATIC (field))
2970 continue;
2971 if (TREE_CODE (field) != FIELD_DECL)
2972 continue;
2973
2974 if (DECL_NAME (field) == NULL_TREE
2975 && ANON_AGGR_TYPE_P (TREE_TYPE (field)))
2976 finish_struct_anon_r (field, /*complain=*/true);
2977 }
2978 }
2979
2980 /* Add T to CLASSTYPE_DECL_LIST of current_class_type which
2981 will be used later during class template instantiation.
2982 When FRIEND_P is zero, T can be a static member data (VAR_DECL),
2983 a non-static member data (FIELD_DECL), a member function
2984 (FUNCTION_DECL), a nested type (RECORD_TYPE, ENUM_TYPE),
2985 a typedef (TYPE_DECL) or a member class template (TEMPLATE_DECL)
2986 When FRIEND_P is nonzero, T is either a friend class
2987 (RECORD_TYPE, TEMPLATE_DECL) or a friend function
2988 (FUNCTION_DECL, TEMPLATE_DECL). */
2989
2990 void
2991 maybe_add_class_template_decl_list (tree type, tree t, int friend_p)
2992 {
2993 /* Save some memory by not creating TREE_LIST if TYPE is not template. */
2994 if (CLASSTYPE_TEMPLATE_INFO (type))
2995 CLASSTYPE_DECL_LIST (type)
2996 = tree_cons (friend_p ? NULL_TREE : type,
2997 t, CLASSTYPE_DECL_LIST (type));
2998 }
2999
3000 /* This function is called from declare_virt_assop_and_dtor via
3001 dfs_walk_all.
3002
3003 DATA is a type that direcly or indirectly inherits the base
3004 represented by BINFO. If BINFO contains a virtual assignment [copy
3005 assignment or move assigment] operator or a virtual constructor,
3006 declare that function in DATA if it hasn't been already declared. */
3007
3008 static tree
3009 dfs_declare_virt_assop_and_dtor (tree binfo, void *data)
3010 {
3011 tree bv, fn, t = (tree)data;
3012 tree opname = assign_op_identifier;
3013
3014 gcc_assert (t && CLASS_TYPE_P (t));
3015 gcc_assert (binfo && TREE_CODE (binfo) == TREE_BINFO);
3016
3017 if (!TYPE_CONTAINS_VPTR_P (BINFO_TYPE (binfo)))
3018 /* A base without a vtable needs no modification, and its bases
3019 are uninteresting. */
3020 return dfs_skip_bases;
3021
3022 if (BINFO_PRIMARY_P (binfo))
3023 /* If this is a primary base, then we have already looked at the
3024 virtual functions of its vtable. */
3025 return NULL_TREE;
3026
3027 for (bv = BINFO_VIRTUALS (binfo); bv; bv = TREE_CHAIN (bv))
3028 {
3029 fn = BV_FN (bv);
3030
3031 if (DECL_NAME (fn) == opname)
3032 {
3033 if (CLASSTYPE_LAZY_COPY_ASSIGN (t))
3034 lazily_declare_fn (sfk_copy_assignment, t);
3035 if (CLASSTYPE_LAZY_MOVE_ASSIGN (t))
3036 lazily_declare_fn (sfk_move_assignment, t);
3037 }
3038 else if (DECL_DESTRUCTOR_P (fn)
3039 && CLASSTYPE_LAZY_DESTRUCTOR (t))
3040 lazily_declare_fn (sfk_destructor, t);
3041 }
3042
3043 return NULL_TREE;
3044 }
3045
3046 /* If the class type T has a direct or indirect base that contains a
3047 virtual assignment operator or a virtual destructor, declare that
3048 function in T if it hasn't been already declared. */
3049
3050 static void
3051 declare_virt_assop_and_dtor (tree t)
3052 {
3053 if (!(TYPE_POLYMORPHIC_P (t)
3054 && (CLASSTYPE_LAZY_COPY_ASSIGN (t)
3055 || CLASSTYPE_LAZY_MOVE_ASSIGN (t)
3056 || CLASSTYPE_LAZY_DESTRUCTOR (t))))
3057 return;
3058
3059 dfs_walk_all (TYPE_BINFO (t),
3060 dfs_declare_virt_assop_and_dtor,
3061 NULL, t);
3062 }
3063
3064 /* Declare the inheriting constructor for class T inherited from base
3065 constructor CTOR with the parameter array PARMS of size NPARMS. */
3066
3067 static void
3068 one_inheriting_sig (tree t, tree ctor, tree *parms, int nparms)
3069 {
3070 gcc_assert (TYPE_MAIN_VARIANT (t) == t);
3071
3072 /* We don't declare an inheriting ctor that would be a default,
3073 copy or move ctor for derived or base. */
3074 if (nparms == 0)
3075 return;
3076 if (nparms == 1
3077 && TREE_CODE (parms[0]) == REFERENCE_TYPE)
3078 {
3079 tree parm = TYPE_MAIN_VARIANT (TREE_TYPE (parms[0]));
3080 if (parm == t || parm == DECL_CONTEXT (ctor))
3081 return;
3082 }
3083
3084 tree parmlist = void_list_node;
3085 for (int i = nparms - 1; i >= 0; i--)
3086 parmlist = tree_cons (NULL_TREE, parms[i], parmlist);
3087 tree fn = implicitly_declare_fn (sfk_inheriting_constructor,
3088 t, false, ctor, parmlist);
3089
3090 if (add_method (t, fn, false))
3091 {
3092 DECL_CHAIN (fn) = TYPE_FIELDS (t);
3093 TYPE_FIELDS (t) = fn;
3094 }
3095 }
3096
3097 /* Declare all the inheriting constructors for class T inherited from base
3098 constructor CTOR. */
3099
3100 static void
3101 one_inherited_ctor (tree ctor, tree t, tree using_decl)
3102 {
3103 tree parms = FUNCTION_FIRST_USER_PARMTYPE (ctor);
3104
3105 if (flag_new_inheriting_ctors)
3106 {
3107 ctor = implicitly_declare_fn (sfk_inheriting_constructor,
3108 t, /*const*/false, ctor, parms);
3109 add_method (t, ctor, using_decl != NULL_TREE);
3110 TYPE_HAS_USER_CONSTRUCTOR (t) = true;
3111 return;
3112 }
3113
3114 tree *new_parms = XALLOCAVEC (tree, list_length (parms));
3115 int i = 0;
3116 for (; parms && parms != void_list_node; parms = TREE_CHAIN (parms))
3117 {
3118 if (TREE_PURPOSE (parms))
3119 one_inheriting_sig (t, ctor, new_parms, i);
3120 new_parms[i++] = TREE_VALUE (parms);
3121 }
3122 one_inheriting_sig (t, ctor, new_parms, i);
3123 if (parms == NULL_TREE)
3124 {
3125 if (warning (OPT_Winherited_variadic_ctor,
3126 "the ellipsis in %qD is not inherited", ctor))
3127 inform (DECL_SOURCE_LOCATION (ctor), "%qD declared here", ctor);
3128 }
3129 }
3130
3131 /* Create default constructors, assignment operators, and so forth for
3132 the type indicated by T, if they are needed. CANT_HAVE_CONST_CTOR,
3133 and CANT_HAVE_CONST_ASSIGNMENT are nonzero if, for whatever reason,
3134 the class cannot have a default constructor, copy constructor
3135 taking a const reference argument, or an assignment operator taking
3136 a const reference, respectively. */
3137
3138 static void
3139 add_implicitly_declared_members (tree t, tree* access_decls,
3140 int cant_have_const_cctor,
3141 int cant_have_const_assignment)
3142 {
3143 /* Destructor. */
3144 if (!CLASSTYPE_DESTRUCTOR (t))
3145 /* In general, we create destructors lazily. */
3146 CLASSTYPE_LAZY_DESTRUCTOR (t) = 1;
3147
3148 bool move_ok = false;
3149 if (cxx_dialect >= cxx11 && CLASSTYPE_LAZY_DESTRUCTOR (t)
3150 && !TYPE_HAS_COPY_CTOR (t) && !TYPE_HAS_COPY_ASSIGN (t)
3151 && !classtype_has_move_assign_or_move_ctor_p (t, false))
3152 move_ok = true;
3153
3154 /* [class.ctor]
3155
3156 If there is no user-declared constructor for a class, a default
3157 constructor is implicitly declared. */
3158 if (! TYPE_HAS_USER_CONSTRUCTOR (t))
3159 {
3160 TYPE_HAS_DEFAULT_CONSTRUCTOR (t) = 1;
3161 CLASSTYPE_LAZY_DEFAULT_CTOR (t) = 1;
3162 if (cxx_dialect >= cxx11)
3163 TYPE_HAS_CONSTEXPR_CTOR (t)
3164 /* Don't force the declaration to get a hard answer; if the
3165 definition would have made the class non-literal, it will still be
3166 non-literal because of the base or member in question, and that
3167 gives a better diagnostic. */
3168 = type_maybe_constexpr_default_constructor (t);
3169 }
3170
3171 /* [class.ctor]
3172
3173 If a class definition does not explicitly declare a copy
3174 constructor, one is declared implicitly. */
3175 if (! TYPE_HAS_COPY_CTOR (t))
3176 {
3177 TYPE_HAS_COPY_CTOR (t) = 1;
3178 TYPE_HAS_CONST_COPY_CTOR (t) = !cant_have_const_cctor;
3179 CLASSTYPE_LAZY_COPY_CTOR (t) = 1;
3180 if (move_ok)
3181 CLASSTYPE_LAZY_MOVE_CTOR (t) = 1;
3182 }
3183
3184 /* If there is no assignment operator, one will be created if and
3185 when it is needed. For now, just record whether or not the type
3186 of the parameter to the assignment operator will be a const or
3187 non-const reference. */
3188 if (!TYPE_HAS_COPY_ASSIGN (t))
3189 {
3190 TYPE_HAS_COPY_ASSIGN (t) = 1;
3191 TYPE_HAS_CONST_COPY_ASSIGN (t) = !cant_have_const_assignment;
3192 CLASSTYPE_LAZY_COPY_ASSIGN (t) = 1;
3193 if (move_ok && !LAMBDA_TYPE_P (t))
3194 CLASSTYPE_LAZY_MOVE_ASSIGN (t) = 1;
3195 }
3196
3197 /* We can't be lazy about declaring functions that might override
3198 a virtual function from a base class. */
3199 declare_virt_assop_and_dtor (t);
3200
3201 while (*access_decls)
3202 {
3203 tree using_decl = TREE_VALUE (*access_decls);
3204 tree decl = USING_DECL_DECLS (using_decl);
3205 if (DECL_NAME (using_decl) == ctor_identifier)
3206 {
3207 /* declare, then remove the decl */
3208 tree ctor_list = decl;
3209 location_t loc = input_location;
3210 input_location = DECL_SOURCE_LOCATION (using_decl);
3211 for (ovl_iterator iter (ctor_list); iter; ++iter)
3212 one_inherited_ctor (*iter, t, using_decl);
3213 *access_decls = TREE_CHAIN (*access_decls);
3214 input_location = loc;
3215 }
3216 else
3217 access_decls = &TREE_CHAIN (*access_decls);
3218 }
3219 }
3220
3221 /* FIELD is a bit-field. We are finishing the processing for its
3222 enclosing type. Issue any appropriate messages and set appropriate
3223 flags. Returns false if an error has been diagnosed. */
3224
3225 static bool
3226 check_bitfield_decl (tree field)
3227 {
3228 tree type = TREE_TYPE (field);
3229 tree w;
3230
3231 /* Extract the declared width of the bitfield, which has been
3232 temporarily stashed in DECL_BIT_FIELD_REPRESENTATIVE by grokbitfield. */
3233 w = DECL_BIT_FIELD_REPRESENTATIVE (field);
3234 gcc_assert (w != NULL_TREE);
3235 /* Remove the bit-field width indicator so that the rest of the
3236 compiler does not treat that value as a qualifier. */
3237 DECL_BIT_FIELD_REPRESENTATIVE (field) = NULL_TREE;
3238
3239 /* Detect invalid bit-field type. */
3240 if (!INTEGRAL_OR_ENUMERATION_TYPE_P (type))
3241 {
3242 error ("bit-field %q+#D with non-integral type", field);
3243 w = error_mark_node;
3244 }
3245 else
3246 {
3247 location_t loc = input_location;
3248 /* Avoid the non_lvalue wrapper added by fold for PLUS_EXPRs. */
3249 STRIP_NOPS (w);
3250
3251 /* detect invalid field size. */
3252 input_location = DECL_SOURCE_LOCATION (field);
3253 w = cxx_constant_value (w);
3254 input_location = loc;
3255
3256 if (TREE_CODE (w) != INTEGER_CST)
3257 {
3258 error ("bit-field %q+D width not an integer constant", field);
3259 w = error_mark_node;
3260 }
3261 else if (tree_int_cst_sgn (w) < 0)
3262 {
3263 error ("negative width in bit-field %q+D", field);
3264 w = error_mark_node;
3265 }
3266 else if (integer_zerop (w) && DECL_NAME (field) != 0)
3267 {
3268 error ("zero width for bit-field %q+D", field);
3269 w = error_mark_node;
3270 }
3271 else if ((TREE_CODE (type) != ENUMERAL_TYPE
3272 && TREE_CODE (type) != BOOLEAN_TYPE
3273 && compare_tree_int (w, TYPE_PRECISION (type)) > 0)
3274 || ((TREE_CODE (type) == ENUMERAL_TYPE
3275 || TREE_CODE (type) == BOOLEAN_TYPE)
3276 && tree_int_cst_lt (TYPE_SIZE (type), w)))
3277 warning_at (DECL_SOURCE_LOCATION (field), 0,
3278 "width of %qD exceeds its type", field);
3279 else if (TREE_CODE (type) == ENUMERAL_TYPE)
3280 {
3281 int prec = TYPE_PRECISION (ENUM_UNDERLYING_TYPE (type));
3282 if (compare_tree_int (w, prec) < 0)
3283 warning_at (DECL_SOURCE_LOCATION (field), 0,
3284 "%qD is too small to hold all values of %q#T",
3285 field, type);
3286 }
3287 }
3288
3289 if (w != error_mark_node)
3290 {
3291 DECL_SIZE (field) = fold_convert (bitsizetype, w);
3292 DECL_BIT_FIELD (field) = 1;
3293 return true;
3294 }
3295 else
3296 {
3297 /* Non-bit-fields are aligned for their type. */
3298 DECL_BIT_FIELD (field) = 0;
3299 CLEAR_DECL_C_BIT_FIELD (field);
3300 return false;
3301 }
3302 }
3303
3304 /* FIELD is a non bit-field. We are finishing the processing for its
3305 enclosing type T. Issue any appropriate messages and set appropriate
3306 flags. */
3307
3308 static bool
3309 check_field_decl (tree field,
3310 tree t,
3311 int* cant_have_const_ctor,
3312 int* no_const_asn_ref)
3313 {
3314 tree type = strip_array_types (TREE_TYPE (field));
3315 bool any_default_members = false;
3316
3317 /* In C++98 an anonymous union cannot contain any fields which would change
3318 the settings of CANT_HAVE_CONST_CTOR and friends. */
3319 if (ANON_UNION_TYPE_P (type) && cxx_dialect < cxx11)
3320 ;
3321 /* And, we don't set TYPE_HAS_CONST_COPY_CTOR, etc., for anonymous
3322 structs. So, we recurse through their fields here. */
3323 else if (ANON_AGGR_TYPE_P (type))
3324 {
3325 for (tree fields = TYPE_FIELDS (type); fields;
3326 fields = DECL_CHAIN (fields))
3327 if (TREE_CODE (fields) == FIELD_DECL)
3328 any_default_members |= check_field_decl (fields, t,
3329 cant_have_const_ctor,
3330 no_const_asn_ref);
3331 }
3332 /* Check members with class type for constructors, destructors,
3333 etc. */
3334 else if (CLASS_TYPE_P (type))
3335 {
3336 /* Never let anything with uninheritable virtuals
3337 make it through without complaint. */
3338 abstract_virtuals_error (field, type);
3339
3340 if (TREE_CODE (t) == UNION_TYPE && cxx_dialect < cxx11)
3341 {
3342 static bool warned;
3343 int oldcount = errorcount;
3344 if (TYPE_NEEDS_CONSTRUCTING (type))
3345 error ("member %q+#D with constructor not allowed in union",
3346 field);
3347 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
3348 error ("member %q+#D with destructor not allowed in union", field);
3349 if (TYPE_HAS_COMPLEX_COPY_ASSIGN (type))
3350 error ("member %q+#D with copy assignment operator not allowed in union",
3351 field);
3352 if (!warned && errorcount > oldcount)
3353 {
3354 inform (DECL_SOURCE_LOCATION (field), "unrestricted unions "
3355 "only available with -std=c++11 or -std=gnu++11");
3356 warned = true;
3357 }
3358 }
3359 else
3360 {
3361 TYPE_NEEDS_CONSTRUCTING (t) |= TYPE_NEEDS_CONSTRUCTING (type);
3362 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t)
3363 |= TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type);
3364 TYPE_HAS_COMPLEX_COPY_ASSIGN (t)
3365 |= (TYPE_HAS_COMPLEX_COPY_ASSIGN (type)
3366 || !TYPE_HAS_COPY_ASSIGN (type));
3367 TYPE_HAS_COMPLEX_COPY_CTOR (t) |= (TYPE_HAS_COMPLEX_COPY_CTOR (type)
3368 || !TYPE_HAS_COPY_CTOR (type));
3369 TYPE_HAS_COMPLEX_MOVE_ASSIGN (t) |= TYPE_HAS_COMPLEX_MOVE_ASSIGN (type);
3370 TYPE_HAS_COMPLEX_MOVE_CTOR (t) |= TYPE_HAS_COMPLEX_MOVE_CTOR (type);
3371 TYPE_HAS_COMPLEX_DFLT (t) |= (!TYPE_HAS_DEFAULT_CONSTRUCTOR (type)
3372 || TYPE_HAS_COMPLEX_DFLT (type));
3373 }
3374
3375 if (TYPE_HAS_COPY_CTOR (type)
3376 && !TYPE_HAS_CONST_COPY_CTOR (type))
3377 *cant_have_const_ctor = 1;
3378
3379 if (TYPE_HAS_COPY_ASSIGN (type)
3380 && !TYPE_HAS_CONST_COPY_ASSIGN (type))
3381 *no_const_asn_ref = 1;
3382 }
3383
3384 check_abi_tags (t, field);
3385
3386 if (DECL_INITIAL (field) != NULL_TREE)
3387 /* `build_class_init_list' does not recognize
3388 non-FIELD_DECLs. */
3389 any_default_members = true;
3390
3391 return any_default_members;
3392 }
3393
3394 /* Check the data members (both static and non-static), class-scoped
3395 typedefs, etc., appearing in the declaration of T. Issue
3396 appropriate diagnostics. Sets ACCESS_DECLS to a list (in
3397 declaration order) of access declarations; each TREE_VALUE in this
3398 list is a USING_DECL.
3399
3400 In addition, set the following flags:
3401
3402 EMPTY_P
3403 The class is empty, i.e., contains no non-static data members.
3404
3405 CANT_HAVE_CONST_CTOR_P
3406 This class cannot have an implicitly generated copy constructor
3407 taking a const reference.
3408
3409 CANT_HAVE_CONST_ASN_REF
3410 This class cannot have an implicitly generated assignment
3411 operator taking a const reference.
3412
3413 All of these flags should be initialized before calling this
3414 function.
3415
3416 Returns a pointer to the end of the TYPE_FIELDs chain; additional
3417 fields can be added by adding to this chain. */
3418
3419 static void
3420 check_field_decls (tree t, tree *access_decls,
3421 int *cant_have_const_ctor_p,
3422 int *no_const_asn_ref_p)
3423 {
3424 tree *field;
3425 tree *next;
3426 bool has_pointers;
3427 bool any_default_members;
3428 int cant_pack = 0;
3429 int field_access = -1;
3430
3431 /* Assume there are no access declarations. */
3432 *access_decls = NULL_TREE;
3433 /* Assume this class has no pointer members. */
3434 has_pointers = false;
3435 /* Assume none of the members of this class have default
3436 initializations. */
3437 any_default_members = false;
3438
3439 for (field = &TYPE_FIELDS (t); *field; field = next)
3440 {
3441 tree x = *field;
3442 tree type = TREE_TYPE (x);
3443 int this_field_access;
3444
3445 next = &DECL_CHAIN (x);
3446
3447 if (TREE_CODE (x) == USING_DECL)
3448 {
3449 /* Save the access declarations for our caller. */
3450 *access_decls = tree_cons (NULL_TREE, x, *access_decls);
3451 continue;
3452 }
3453
3454 if (TREE_CODE (x) == TYPE_DECL
3455 || TREE_CODE (x) == TEMPLATE_DECL)
3456 continue;
3457
3458 if (TREE_CODE (x) == FUNCTION_DECL)
3459 /* FIXME: We should fold in the checking from check_methods. */
3460 continue;
3461
3462 /* If we've gotten this far, it's a data member, possibly static,
3463 or an enumerator. */
3464 if (TREE_CODE (x) != CONST_DECL)
3465 DECL_CONTEXT (x) = t;
3466
3467 /* When this goes into scope, it will be a non-local reference. */
3468 DECL_NONLOCAL (x) = 1;
3469
3470 if (TREE_CODE (t) == UNION_TYPE)
3471 {
3472 /* [class.union] (C++98)
3473
3474 If a union contains a static data member, or a member of
3475 reference type, the program is ill-formed.
3476
3477 In C++11 [class.union] says:
3478 If a union contains a non-static data member of reference type
3479 the program is ill-formed. */
3480 if (VAR_P (x) && cxx_dialect < cxx11)
3481 {
3482 error ("in C++98 %q+D may not be static because it is "
3483 "a member of a union", x);
3484 continue;
3485 }
3486 if (TREE_CODE (type) == REFERENCE_TYPE
3487 && TREE_CODE (x) == FIELD_DECL)
3488 {
3489 error ("non-static data member %q+D in a union may not "
3490 "have reference type %qT", x, type);
3491 continue;
3492 }
3493 }
3494
3495 /* Perform error checking that did not get done in
3496 grokdeclarator. */
3497 if (TREE_CODE (type) == FUNCTION_TYPE)
3498 {
3499 error ("field %q+D invalidly declared function type", x);
3500 type = build_pointer_type (type);
3501 TREE_TYPE (x) = type;
3502 }
3503 else if (TREE_CODE (type) == METHOD_TYPE)
3504 {
3505 error ("field %q+D invalidly declared method type", x);
3506 type = build_pointer_type (type);
3507 TREE_TYPE (x) = type;
3508 }
3509
3510 if (type == error_mark_node)
3511 continue;
3512
3513 if (TREE_CODE (x) == CONST_DECL || VAR_P (x))
3514 continue;
3515
3516 /* Now it can only be a FIELD_DECL. */
3517
3518 if (TREE_PRIVATE (x) || TREE_PROTECTED (x))
3519 CLASSTYPE_NON_AGGREGATE (t) = 1;
3520
3521 /* If at least one non-static data member is non-literal, the whole
3522 class becomes non-literal. Per Core/1453, volatile non-static
3523 data members and base classes are also not allowed.
3524 Note: if the type is incomplete we will complain later on. */
3525 if (COMPLETE_TYPE_P (type)
3526 && (!literal_type_p (type) || CP_TYPE_VOLATILE_P (type)))
3527 CLASSTYPE_LITERAL_P (t) = false;
3528
3529 /* A standard-layout class is a class that:
3530 ...
3531 has the same access control (Clause 11) for all non-static data members,
3532 ... */
3533 this_field_access = TREE_PROTECTED (x) ? 1 : TREE_PRIVATE (x) ? 2 : 0;
3534 if (field_access == -1)
3535 field_access = this_field_access;
3536 else if (this_field_access != field_access)
3537 CLASSTYPE_NON_STD_LAYOUT (t) = 1;
3538
3539 /* If this is of reference type, check if it needs an init. */
3540 if (TREE_CODE (type) == REFERENCE_TYPE)
3541 {
3542 CLASSTYPE_NON_LAYOUT_POD_P (t) = 1;
3543 CLASSTYPE_NON_STD_LAYOUT (t) = 1;
3544 if (DECL_INITIAL (x) == NULL_TREE)
3545 SET_CLASSTYPE_REF_FIELDS_NEED_INIT (t, 1);
3546 if (cxx_dialect < cxx11)
3547 {
3548 /* ARM $12.6.2: [A member initializer list] (or, for an
3549 aggregate, initialization by a brace-enclosed list) is the
3550 only way to initialize nonstatic const and reference
3551 members. */
3552 TYPE_HAS_COMPLEX_COPY_ASSIGN (t) = 1;
3553 TYPE_HAS_COMPLEX_MOVE_ASSIGN (t) = 1;
3554 }
3555 }
3556
3557 type = strip_array_types (type);
3558
3559 if (TYPE_PACKED (t))
3560 {
3561 if (!layout_pod_type_p (type) && !TYPE_PACKED (type))
3562 {
3563 warning_at
3564 (DECL_SOURCE_LOCATION (x), 0,
3565 "ignoring packed attribute because of unpacked non-POD field %q#D",
3566 x);
3567 cant_pack = 1;
3568 }
3569 else if (DECL_C_BIT_FIELD (x)
3570 || TYPE_ALIGN (TREE_TYPE (x)) > BITS_PER_UNIT)
3571 DECL_PACKED (x) = 1;
3572 }
3573
3574 if (DECL_C_BIT_FIELD (x)
3575 && integer_zerop (DECL_BIT_FIELD_REPRESENTATIVE (x)))
3576 /* We don't treat zero-width bitfields as making a class
3577 non-empty. */
3578 ;
3579 else
3580 {
3581 /* The class is non-empty. */
3582 CLASSTYPE_EMPTY_P (t) = 0;
3583 /* The class is not even nearly empty. */
3584 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
3585 /* If one of the data members contains an empty class,
3586 so does T. */
3587 if (CLASS_TYPE_P (type)
3588 && CLASSTYPE_CONTAINS_EMPTY_CLASS_P (type))
3589 CLASSTYPE_CONTAINS_EMPTY_CLASS_P (t) = 1;
3590 }
3591
3592 /* This is used by -Weffc++ (see below). Warn only for pointers
3593 to members which might hold dynamic memory. So do not warn
3594 for pointers to functions or pointers to members. */
3595 if (TYPE_PTR_P (type)
3596 && !TYPE_PTRFN_P (type))
3597 has_pointers = true;
3598
3599 if (CLASS_TYPE_P (type))
3600 {
3601 if (CLASSTYPE_REF_FIELDS_NEED_INIT (type))
3602 SET_CLASSTYPE_REF_FIELDS_NEED_INIT (t, 1);
3603 if (CLASSTYPE_READONLY_FIELDS_NEED_INIT (type))
3604 SET_CLASSTYPE_READONLY_FIELDS_NEED_INIT (t, 1);
3605 }
3606
3607 if (DECL_MUTABLE_P (x) || TYPE_HAS_MUTABLE_P (type))
3608 CLASSTYPE_HAS_MUTABLE (t) = 1;
3609
3610 if (DECL_MUTABLE_P (x))
3611 {
3612 if (CP_TYPE_CONST_P (type))
3613 {
3614 error ("member %q+D cannot be declared both %<const%> "
3615 "and %<mutable%>", x);
3616 continue;
3617 }
3618 if (TREE_CODE (type) == REFERENCE_TYPE)
3619 {
3620 error ("member %q+D cannot be declared as a %<mutable%> "
3621 "reference", x);
3622 continue;
3623 }
3624 }
3625
3626 if (! layout_pod_type_p (type))
3627 /* DR 148 now allows pointers to members (which are POD themselves),
3628 to be allowed in POD structs. */
3629 CLASSTYPE_NON_LAYOUT_POD_P (t) = 1;
3630
3631 if (!std_layout_type_p (type))
3632 CLASSTYPE_NON_STD_LAYOUT (t) = 1;
3633
3634 if (! zero_init_p (type))
3635 CLASSTYPE_NON_ZERO_INIT_P (t) = 1;
3636
3637 /* We set DECL_C_BIT_FIELD in grokbitfield.
3638 If the type and width are valid, we'll also set DECL_BIT_FIELD. */
3639 if (DECL_C_BIT_FIELD (x))
3640 check_bitfield_decl (x);
3641
3642 if (check_field_decl (x, t, cant_have_const_ctor_p, no_const_asn_ref_p))
3643 {
3644 if (any_default_members
3645 && TREE_CODE (t) == UNION_TYPE)
3646 error ("multiple fields in union %qT initialized", t);
3647 any_default_members = true;
3648 }
3649
3650 /* Now that we've removed bit-field widths from DECL_INITIAL,
3651 anything left in DECL_INITIAL is an NSDMI that makes the class
3652 non-aggregate in C++11. */
3653 if (DECL_INITIAL (x) && cxx_dialect < cxx14)
3654 CLASSTYPE_NON_AGGREGATE (t) = true;
3655
3656 /* If any field is const, the structure type is pseudo-const. */
3657 if (CP_TYPE_CONST_P (type))
3658 {
3659 C_TYPE_FIELDS_READONLY (t) = 1;
3660 if (DECL_INITIAL (x) == NULL_TREE)
3661 SET_CLASSTYPE_READONLY_FIELDS_NEED_INIT (t, 1);
3662 if (cxx_dialect < cxx11)
3663 {
3664 /* ARM $12.6.2: [A member initializer list] (or, for an
3665 aggregate, initialization by a brace-enclosed list) is the
3666 only way to initialize nonstatic const and reference
3667 members. */
3668 TYPE_HAS_COMPLEX_COPY_ASSIGN (t) = 1;
3669 TYPE_HAS_COMPLEX_MOVE_ASSIGN (t) = 1;
3670 }
3671 }
3672 /* A field that is pseudo-const makes the structure likewise. */
3673 else if (CLASS_TYPE_P (type))
3674 {
3675 C_TYPE_FIELDS_READONLY (t) |= C_TYPE_FIELDS_READONLY (type);
3676 SET_CLASSTYPE_READONLY_FIELDS_NEED_INIT (t,
3677 CLASSTYPE_READONLY_FIELDS_NEED_INIT (t)
3678 | CLASSTYPE_READONLY_FIELDS_NEED_INIT (type));
3679 }
3680
3681 /* Core issue 80: A nonstatic data member is required to have a
3682 different name from the class iff the class has a
3683 user-declared constructor. */
3684 if (constructor_name_p (DECL_NAME (x), t)
3685 && TYPE_HAS_USER_CONSTRUCTOR (t))
3686 permerror (DECL_SOURCE_LOCATION (x),
3687 "field %q#D with same name as class", x);
3688 }
3689
3690 /* Effective C++ rule 11: if a class has dynamic memory held by pointers,
3691 it should also define a copy constructor and an assignment operator to
3692 implement the correct copy semantic (deep vs shallow, etc.). As it is
3693 not feasible to check whether the constructors do allocate dynamic memory
3694 and store it within members, we approximate the warning like this:
3695
3696 -- Warn only if there are members which are pointers
3697 -- Warn only if there is a non-trivial constructor (otherwise,
3698 there cannot be memory allocated).
3699 -- Warn only if there is a non-trivial destructor. We assume that the
3700 user at least implemented the cleanup correctly, and a destructor
3701 is needed to free dynamic memory.
3702
3703 This seems enough for practical purposes. */
3704 if (warn_ecpp
3705 && has_pointers
3706 && TYPE_HAS_USER_CONSTRUCTOR (t)
3707 && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t)
3708 && !(TYPE_HAS_COPY_CTOR (t) && TYPE_HAS_COPY_ASSIGN (t)))
3709 {
3710 warning (OPT_Weffc__, "%q#T has pointer data members", t);
3711
3712 if (! TYPE_HAS_COPY_CTOR (t))
3713 {
3714 warning (OPT_Weffc__,
3715 " but does not override %<%T(const %T&)%>", t, t);
3716 if (!TYPE_HAS_COPY_ASSIGN (t))
3717 warning (OPT_Weffc__, " or %<operator=(const %T&)%>", t);
3718 }
3719 else if (! TYPE_HAS_COPY_ASSIGN (t))
3720 warning (OPT_Weffc__,
3721 " but does not override %<operator=(const %T&)%>", t);
3722 }
3723
3724 /* Non-static data member initializers make the default constructor
3725 non-trivial. */
3726 if (any_default_members)
3727 {
3728 TYPE_NEEDS_CONSTRUCTING (t) = true;
3729 TYPE_HAS_COMPLEX_DFLT (t) = true;
3730 }
3731
3732 /* If any of the fields couldn't be packed, unset TYPE_PACKED. */
3733 if (cant_pack)
3734 TYPE_PACKED (t) = 0;
3735
3736 /* Check anonymous struct/anonymous union fields. */
3737 finish_struct_anon (t);
3738
3739 /* We've built up the list of access declarations in reverse order.
3740 Fix that now. */
3741 *access_decls = nreverse (*access_decls);
3742 }
3743
3744 /* If TYPE is an empty class type, records its OFFSET in the table of
3745 OFFSETS. */
3746
3747 static int
3748 record_subobject_offset (tree type, tree offset, splay_tree offsets)
3749 {
3750 splay_tree_node n;
3751
3752 if (!is_empty_class (type))
3753 return 0;
3754
3755 /* Record the location of this empty object in OFFSETS. */
3756 n = splay_tree_lookup (offsets, (splay_tree_key) offset);
3757 if (!n)
3758 n = splay_tree_insert (offsets,
3759 (splay_tree_key) offset,
3760 (splay_tree_value) NULL_TREE);
3761 n->value = ((splay_tree_value)
3762 tree_cons (NULL_TREE,
3763 type,
3764 (tree) n->value));
3765
3766 return 0;
3767 }
3768
3769 /* Returns nonzero if TYPE is an empty class type and there is
3770 already an entry in OFFSETS for the same TYPE as the same OFFSET. */
3771
3772 static int
3773 check_subobject_offset (tree type, tree offset, splay_tree offsets)
3774 {
3775 splay_tree_node n;
3776 tree t;
3777
3778 if (!is_empty_class (type))
3779 return 0;
3780
3781 /* Record the location of this empty object in OFFSETS. */
3782 n = splay_tree_lookup (offsets, (splay_tree_key) offset);
3783 if (!n)
3784 return 0;
3785
3786 for (t = (tree) n->value; t; t = TREE_CHAIN (t))
3787 if (same_type_p (TREE_VALUE (t), type))
3788 return 1;
3789
3790 return 0;
3791 }
3792
3793 /* Walk through all the subobjects of TYPE (located at OFFSET). Call
3794 F for every subobject, passing it the type, offset, and table of
3795 OFFSETS. If VBASES_P is one, then virtual non-primary bases should
3796 be traversed.
3797
3798 If MAX_OFFSET is non-NULL, then subobjects with an offset greater
3799 than MAX_OFFSET will not be walked.
3800
3801 If F returns a nonzero value, the traversal ceases, and that value
3802 is returned. Otherwise, returns zero. */
3803
3804 static int
3805 walk_subobject_offsets (tree type,
3806 subobject_offset_fn f,
3807 tree offset,
3808 splay_tree offsets,
3809 tree max_offset,
3810 int vbases_p)
3811 {
3812 int r = 0;
3813 tree type_binfo = NULL_TREE;
3814
3815 /* If this OFFSET is bigger than the MAX_OFFSET, then we should
3816 stop. */
3817 if (max_offset && tree_int_cst_lt (max_offset, offset))
3818 return 0;
3819
3820 if (type == error_mark_node)
3821 return 0;
3822
3823 if (!TYPE_P (type))
3824 {
3825 type_binfo = type;
3826 type = BINFO_TYPE (type);
3827 }
3828
3829 if (CLASS_TYPE_P (type))
3830 {
3831 tree field;
3832 tree binfo;
3833 int i;
3834
3835 /* Avoid recursing into objects that are not interesting. */
3836 if (!CLASSTYPE_CONTAINS_EMPTY_CLASS_P (type))
3837 return 0;
3838
3839 /* Record the location of TYPE. */
3840 r = (*f) (type, offset, offsets);
3841 if (r)
3842 return r;
3843
3844 /* Iterate through the direct base classes of TYPE. */
3845 if (!type_binfo)
3846 type_binfo = TYPE_BINFO (type);
3847 for (i = 0; BINFO_BASE_ITERATE (type_binfo, i, binfo); i++)
3848 {
3849 tree binfo_offset;
3850
3851 if (BINFO_VIRTUAL_P (binfo))
3852 continue;
3853
3854 tree orig_binfo;
3855 /* We cannot rely on BINFO_OFFSET being set for the base
3856 class yet, but the offsets for direct non-virtual
3857 bases can be calculated by going back to the TYPE. */
3858 orig_binfo = BINFO_BASE_BINFO (TYPE_BINFO (type), i);
3859 binfo_offset = size_binop (PLUS_EXPR,
3860 offset,
3861 BINFO_OFFSET (orig_binfo));
3862
3863 r = walk_subobject_offsets (binfo,
3864 f,
3865 binfo_offset,
3866 offsets,
3867 max_offset,
3868 /*vbases_p=*/0);
3869 if (r)
3870 return r;
3871 }
3872
3873 if (CLASSTYPE_VBASECLASSES (type))
3874 {
3875 unsigned ix;
3876 vec<tree, va_gc> *vbases;
3877
3878 /* Iterate through the virtual base classes of TYPE. In G++
3879 3.2, we included virtual bases in the direct base class
3880 loop above, which results in incorrect results; the
3881 correct offsets for virtual bases are only known when
3882 working with the most derived type. */
3883 if (vbases_p)
3884 for (vbases = CLASSTYPE_VBASECLASSES (type), ix = 0;
3885 vec_safe_iterate (vbases, ix, &binfo); ix++)
3886 {
3887 r = walk_subobject_offsets (binfo,
3888 f,
3889 size_binop (PLUS_EXPR,
3890 offset,
3891 BINFO_OFFSET (binfo)),
3892 offsets,
3893 max_offset,
3894 /*vbases_p=*/0);
3895 if (r)
3896 return r;
3897 }
3898 else
3899 {
3900 /* We still have to walk the primary base, if it is
3901 virtual. (If it is non-virtual, then it was walked
3902 above.) */
3903 tree vbase = get_primary_binfo (type_binfo);
3904
3905 if (vbase && BINFO_VIRTUAL_P (vbase)
3906 && BINFO_PRIMARY_P (vbase)
3907 && BINFO_INHERITANCE_CHAIN (vbase) == type_binfo)
3908 {
3909 r = (walk_subobject_offsets
3910 (vbase, f, offset,
3911 offsets, max_offset, /*vbases_p=*/0));
3912 if (r)
3913 return r;
3914 }
3915 }
3916 }
3917
3918 /* Iterate through the fields of TYPE. */
3919 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
3920 if (TREE_CODE (field) == FIELD_DECL
3921 && TREE_TYPE (field) != error_mark_node
3922 && !DECL_ARTIFICIAL (field))
3923 {
3924 tree field_offset;
3925
3926 field_offset = byte_position (field);
3927
3928 r = walk_subobject_offsets (TREE_TYPE (field),
3929 f,
3930 size_binop (PLUS_EXPR,
3931 offset,
3932 field_offset),
3933 offsets,
3934 max_offset,
3935 /*vbases_p=*/1);
3936 if (r)
3937 return r;
3938 }
3939 }
3940 else if (TREE_CODE (type) == ARRAY_TYPE)
3941 {
3942 tree element_type = strip_array_types (type);
3943 tree domain = TYPE_DOMAIN (type);
3944 tree index;
3945
3946 /* Avoid recursing into objects that are not interesting. */
3947 if (!CLASS_TYPE_P (element_type)
3948 || !CLASSTYPE_CONTAINS_EMPTY_CLASS_P (element_type)
3949 || !domain
3950 || integer_minus_onep (TYPE_MAX_VALUE (domain)))
3951 return 0;
3952
3953 /* Step through each of the elements in the array. */
3954 for (index = size_zero_node;
3955 !tree_int_cst_lt (TYPE_MAX_VALUE (domain), index);
3956 index = size_binop (PLUS_EXPR, index, size_one_node))
3957 {
3958 r = walk_subobject_offsets (TREE_TYPE (type),
3959 f,
3960 offset,
3961 offsets,
3962 max_offset,
3963 /*vbases_p=*/1);
3964 if (r)
3965 return r;
3966 offset = size_binop (PLUS_EXPR, offset,
3967 TYPE_SIZE_UNIT (TREE_TYPE (type)));
3968 /* If this new OFFSET is bigger than the MAX_OFFSET, then
3969 there's no point in iterating through the remaining
3970 elements of the array. */
3971 if (max_offset && tree_int_cst_lt (max_offset, offset))
3972 break;
3973 }
3974 }
3975
3976 return 0;
3977 }
3978
3979 /* Record all of the empty subobjects of TYPE (either a type or a
3980 binfo). If IS_DATA_MEMBER is true, then a non-static data member
3981 is being placed at OFFSET; otherwise, it is a base class that is
3982 being placed at OFFSET. */
3983
3984 static void
3985 record_subobject_offsets (tree type,
3986 tree offset,
3987 splay_tree offsets,
3988 bool is_data_member)
3989 {
3990 tree max_offset;
3991 /* If recording subobjects for a non-static data member or a
3992 non-empty base class , we do not need to record offsets beyond
3993 the size of the biggest empty class. Additional data members
3994 will go at the end of the class. Additional base classes will go
3995 either at offset zero (if empty, in which case they cannot
3996 overlap with offsets past the size of the biggest empty class) or
3997 at the end of the class.
3998
3999 However, if we are placing an empty base class, then we must record
4000 all offsets, as either the empty class is at offset zero (where
4001 other empty classes might later be placed) or at the end of the
4002 class (where other objects might then be placed, so other empty
4003 subobjects might later overlap). */
4004 if (is_data_member
4005 || !is_empty_class (BINFO_TYPE (type)))
4006 max_offset = sizeof_biggest_empty_class;
4007 else
4008 max_offset = NULL_TREE;
4009 walk_subobject_offsets (type, record_subobject_offset, offset,
4010 offsets, max_offset, is_data_member);
4011 }
4012
4013 /* Returns nonzero if any of the empty subobjects of TYPE (located at
4014 OFFSET) conflict with entries in OFFSETS. If VBASES_P is nonzero,
4015 virtual bases of TYPE are examined. */
4016
4017 static int
4018 layout_conflict_p (tree type,
4019 tree offset,
4020 splay_tree offsets,
4021 int vbases_p)
4022 {
4023 splay_tree_node max_node;
4024
4025 /* Get the node in OFFSETS that indicates the maximum offset where
4026 an empty subobject is located. */
4027 max_node = splay_tree_max (offsets);
4028 /* If there aren't any empty subobjects, then there's no point in
4029 performing this check. */
4030 if (!max_node)
4031 return 0;
4032
4033 return walk_subobject_offsets (type, check_subobject_offset, offset,
4034 offsets, (tree) (max_node->key),
4035 vbases_p);
4036 }
4037
4038 /* DECL is a FIELD_DECL corresponding either to a base subobject of a
4039 non-static data member of the type indicated by RLI. BINFO is the
4040 binfo corresponding to the base subobject, OFFSETS maps offsets to
4041 types already located at those offsets. This function determines
4042 the position of the DECL. */
4043
4044 static void
4045 layout_nonempty_base_or_field (record_layout_info rli,
4046 tree decl,
4047 tree binfo,
4048 splay_tree offsets)
4049 {
4050 tree offset = NULL_TREE;
4051 bool field_p;
4052 tree type;
4053
4054 if (binfo)
4055 {
4056 /* For the purposes of determining layout conflicts, we want to
4057 use the class type of BINFO; TREE_TYPE (DECL) will be the
4058 CLASSTYPE_AS_BASE version, which does not contain entries for
4059 zero-sized bases. */
4060 type = TREE_TYPE (binfo);
4061 field_p = false;
4062 }
4063 else
4064 {
4065 type = TREE_TYPE (decl);
4066 field_p = true;
4067 }
4068
4069 /* Try to place the field. It may take more than one try if we have
4070 a hard time placing the field without putting two objects of the
4071 same type at the same address. */
4072 while (1)
4073 {
4074 struct record_layout_info_s old_rli = *rli;
4075
4076 /* Place this field. */
4077 place_field (rli, decl);
4078 offset = byte_position (decl);
4079
4080 /* We have to check to see whether or not there is already
4081 something of the same type at the offset we're about to use.
4082 For example, consider:
4083
4084 struct S {};
4085 struct T : public S { int i; };
4086 struct U : public S, public T {};
4087
4088 Here, we put S at offset zero in U. Then, we can't put T at
4089 offset zero -- its S component would be at the same address
4090 as the S we already allocated. So, we have to skip ahead.
4091 Since all data members, including those whose type is an
4092 empty class, have nonzero size, any overlap can happen only
4093 with a direct or indirect base-class -- it can't happen with
4094 a data member. */
4095 /* In a union, overlap is permitted; all members are placed at
4096 offset zero. */
4097 if (TREE_CODE (rli->t) == UNION_TYPE)
4098 break;
4099 if (layout_conflict_p (field_p ? type : binfo, offset,
4100 offsets, field_p))
4101 {
4102 /* Strip off the size allocated to this field. That puts us
4103 at the first place we could have put the field with
4104 proper alignment. */
4105 *rli = old_rli;
4106
4107 /* Bump up by the alignment required for the type. */
4108 rli->bitpos
4109 = size_binop (PLUS_EXPR, rli->bitpos,
4110 bitsize_int (binfo
4111 ? CLASSTYPE_ALIGN (type)
4112 : TYPE_ALIGN (type)));
4113 normalize_rli (rli);
4114 }
4115 else if (TREE_CODE (type) == NULLPTR_TYPE
4116 && warn_abi && abi_version_crosses (9))
4117 {
4118 /* Before ABI v9, we were giving nullptr_t alignment of 1; if
4119 the offset wasn't aligned like a pointer when we started to
4120 layout this field, that affects its position. */
4121 tree pos = rli_size_unit_so_far (&old_rli);
4122 if (int_cst_value (pos) % TYPE_ALIGN_UNIT (ptr_type_node) != 0)
4123 {
4124 if (abi_version_at_least (9))
4125 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wabi,
4126 "alignment of %qD increased in -fabi-version=9 "
4127 "(GCC 5.2)", decl);
4128 else
4129 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wabi, "alignment "
4130 "of %qD will increase in -fabi-version=9", decl);
4131 }
4132 break;
4133 }
4134 else
4135 /* There was no conflict. We're done laying out this field. */
4136 break;
4137 }
4138
4139 /* Now that we know where it will be placed, update its
4140 BINFO_OFFSET. */
4141 if (binfo && CLASS_TYPE_P (BINFO_TYPE (binfo)))
4142 /* Indirect virtual bases may have a nonzero BINFO_OFFSET at
4143 this point because their BINFO_OFFSET is copied from another
4144 hierarchy. Therefore, we may not need to add the entire
4145 OFFSET. */
4146 propagate_binfo_offsets (binfo,
4147 size_diffop_loc (input_location,
4148 fold_convert (ssizetype, offset),
4149 fold_convert (ssizetype,
4150 BINFO_OFFSET (binfo))));
4151 }
4152
4153 /* Returns true if TYPE is empty and OFFSET is nonzero. */
4154
4155 static int
4156 empty_base_at_nonzero_offset_p (tree type,
4157 tree offset,
4158 splay_tree /*offsets*/)
4159 {
4160 return is_empty_class (type) && !integer_zerop (offset);
4161 }
4162
4163 /* Layout the empty base BINFO. EOC indicates the byte currently just
4164 past the end of the class, and should be correctly aligned for a
4165 class of the type indicated by BINFO; OFFSETS gives the offsets of
4166 the empty bases allocated so far. T is the most derived
4167 type. Return nonzero iff we added it at the end. */
4168
4169 static bool
4170 layout_empty_base (record_layout_info rli, tree binfo,
4171 tree eoc, splay_tree offsets)
4172 {
4173 tree alignment;
4174 tree basetype = BINFO_TYPE (binfo);
4175 bool atend = false;
4176
4177 /* This routine should only be used for empty classes. */
4178 gcc_assert (is_empty_class (basetype));
4179 alignment = ssize_int (CLASSTYPE_ALIGN_UNIT (basetype));
4180
4181 if (!integer_zerop (BINFO_OFFSET (binfo)))
4182 propagate_binfo_offsets
4183 (binfo, size_diffop_loc (input_location,
4184 size_zero_node, BINFO_OFFSET (binfo)));
4185
4186 /* This is an empty base class. We first try to put it at offset
4187 zero. */
4188 if (layout_conflict_p (binfo,
4189 BINFO_OFFSET (binfo),
4190 offsets,
4191 /*vbases_p=*/0))
4192 {
4193 /* That didn't work. Now, we move forward from the next
4194 available spot in the class. */
4195 atend = true;
4196 propagate_binfo_offsets (binfo, fold_convert (ssizetype, eoc));
4197 while (1)
4198 {
4199 if (!layout_conflict_p (binfo,
4200 BINFO_OFFSET (binfo),
4201 offsets,
4202 /*vbases_p=*/0))
4203 /* We finally found a spot where there's no overlap. */
4204 break;
4205
4206 /* There's overlap here, too. Bump along to the next spot. */
4207 propagate_binfo_offsets (binfo, alignment);
4208 }
4209 }
4210
4211 if (CLASSTYPE_USER_ALIGN (basetype))
4212 {
4213 rli->record_align = MAX (rli->record_align, CLASSTYPE_ALIGN (basetype));
4214 if (warn_packed)
4215 rli->unpacked_align = MAX (rli->unpacked_align, CLASSTYPE_ALIGN (basetype));
4216 TYPE_USER_ALIGN (rli->t) = 1;
4217 }
4218
4219 return atend;
4220 }
4221
4222 /* Build the FIELD_DECL for BASETYPE as a base of T, add it to the chain of
4223 fields at NEXT_FIELD, and return it. */
4224
4225 static tree
4226 build_base_field_1 (tree t, tree basetype, tree *&next_field)
4227 {
4228 /* Create the FIELD_DECL. */
4229 gcc_assert (CLASSTYPE_AS_BASE (basetype));
4230 tree decl = build_decl (input_location,
4231 FIELD_DECL, NULL_TREE, CLASSTYPE_AS_BASE (basetype));
4232 DECL_ARTIFICIAL (decl) = 1;
4233 DECL_IGNORED_P (decl) = 1;
4234 DECL_FIELD_CONTEXT (decl) = t;
4235 DECL_SIZE (decl) = CLASSTYPE_SIZE (basetype);
4236 DECL_SIZE_UNIT (decl) = CLASSTYPE_SIZE_UNIT (basetype);
4237 SET_DECL_ALIGN (decl, CLASSTYPE_ALIGN (basetype));
4238 DECL_USER_ALIGN (decl) = CLASSTYPE_USER_ALIGN (basetype);
4239 SET_DECL_MODE (decl, TYPE_MODE (basetype));
4240 DECL_FIELD_IS_BASE (decl) = 1;
4241
4242 /* Add the new FIELD_DECL to the list of fields for T. */
4243 DECL_CHAIN (decl) = *next_field;
4244 *next_field = decl;
4245 next_field = &DECL_CHAIN (decl);
4246
4247 return decl;
4248 }
4249
4250 /* Layout the base given by BINFO in the class indicated by RLI.
4251 *BASE_ALIGN is a running maximum of the alignments of
4252 any base class. OFFSETS gives the location of empty base
4253 subobjects. T is the most derived type. Return nonzero if the new
4254 object cannot be nearly-empty. A new FIELD_DECL is inserted at
4255 *NEXT_FIELD, unless BINFO is for an empty base class.
4256
4257 Returns the location at which the next field should be inserted. */
4258
4259 static tree *
4260 build_base_field (record_layout_info rli, tree binfo,
4261 splay_tree offsets, tree *next_field)
4262 {
4263 tree t = rli->t;
4264 tree basetype = BINFO_TYPE (binfo);
4265
4266 if (!COMPLETE_TYPE_P (basetype))
4267 /* This error is now reported in xref_tag, thus giving better
4268 location information. */
4269 return next_field;
4270
4271 /* Place the base class. */
4272 if (!is_empty_class (basetype))
4273 {
4274 tree decl;
4275
4276 /* The containing class is non-empty because it has a non-empty
4277 base class. */
4278 CLASSTYPE_EMPTY_P (t) = 0;
4279
4280 /* Create the FIELD_DECL. */
4281 decl = build_base_field_1 (t, basetype, next_field);
4282
4283 /* Try to place the field. It may take more than one try if we
4284 have a hard time placing the field without putting two
4285 objects of the same type at the same address. */
4286 layout_nonempty_base_or_field (rli, decl, binfo, offsets);
4287 }
4288 else
4289 {
4290 tree eoc;
4291 bool atend;
4292
4293 /* On some platforms (ARM), even empty classes will not be
4294 byte-aligned. */
4295 eoc = round_up_loc (input_location,
4296 rli_size_unit_so_far (rli),
4297 CLASSTYPE_ALIGN_UNIT (basetype));
4298 atend = layout_empty_base (rli, binfo, eoc, offsets);
4299 /* A nearly-empty class "has no proper base class that is empty,
4300 not morally virtual, and at an offset other than zero." */
4301 if (!BINFO_VIRTUAL_P (binfo) && CLASSTYPE_NEARLY_EMPTY_P (t))
4302 {
4303 if (atend)
4304 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
4305 /* The check above (used in G++ 3.2) is insufficient because
4306 an empty class placed at offset zero might itself have an
4307 empty base at a nonzero offset. */
4308 else if (walk_subobject_offsets (basetype,
4309 empty_base_at_nonzero_offset_p,
4310 size_zero_node,
4311 /*offsets=*/NULL,
4312 /*max_offset=*/NULL_TREE,
4313 /*vbases_p=*/true))
4314 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
4315 }
4316
4317 /* We used to not create a FIELD_DECL for empty base classes because of
4318 back end issues with overlapping FIELD_DECLs, but that doesn't seem to
4319 be a problem anymore. We need them to handle initialization of C++17
4320 aggregate bases. */
4321 if (cxx_dialect >= cxx17 && !BINFO_VIRTUAL_P (binfo))
4322 {
4323 tree decl = build_base_field_1 (t, basetype, next_field);
4324 DECL_FIELD_OFFSET (decl) = BINFO_OFFSET (binfo);
4325 DECL_FIELD_BIT_OFFSET (decl) = bitsize_zero_node;
4326 SET_DECL_OFFSET_ALIGN (decl, BITS_PER_UNIT);
4327 }
4328
4329 /* An empty virtual base causes a class to be non-empty
4330 -- but in that case we do not need to clear CLASSTYPE_EMPTY_P
4331 here because that was already done when the virtual table
4332 pointer was created. */
4333 }
4334
4335 /* Record the offsets of BINFO and its base subobjects. */
4336 record_subobject_offsets (binfo,
4337 BINFO_OFFSET (binfo),
4338 offsets,
4339 /*is_data_member=*/false);
4340
4341 return next_field;
4342 }
4343
4344 /* Layout all of the non-virtual base classes. Record empty
4345 subobjects in OFFSETS. T is the most derived type. Return nonzero
4346 if the type cannot be nearly empty. The fields created
4347 corresponding to the base classes will be inserted at
4348 *NEXT_FIELD. */
4349
4350 static void
4351 build_base_fields (record_layout_info rli,
4352 splay_tree offsets, tree *next_field)
4353 {
4354 /* Chain to hold all the new FIELD_DECLs which stand in for base class
4355 subobjects. */
4356 tree t = rli->t;
4357 int n_baseclasses = BINFO_N_BASE_BINFOS (TYPE_BINFO (t));
4358 int i;
4359
4360 /* The primary base class is always allocated first. */
4361 if (CLASSTYPE_HAS_PRIMARY_BASE_P (t))
4362 next_field = build_base_field (rli, CLASSTYPE_PRIMARY_BINFO (t),
4363 offsets, next_field);
4364
4365 /* Now allocate the rest of the bases. */
4366 for (i = 0; i < n_baseclasses; ++i)
4367 {
4368 tree base_binfo;
4369
4370 base_binfo = BINFO_BASE_BINFO (TYPE_BINFO (t), i);
4371
4372 /* The primary base was already allocated above, so we don't
4373 need to allocate it again here. */
4374 if (base_binfo == CLASSTYPE_PRIMARY_BINFO (t))
4375 continue;
4376
4377 /* Virtual bases are added at the end (a primary virtual base
4378 will have already been added). */
4379 if (BINFO_VIRTUAL_P (base_binfo))
4380 continue;
4381
4382 next_field = build_base_field (rli, base_binfo,
4383 offsets, next_field);
4384 }
4385 }
4386
4387 /* Go through the TYPE_FIELDS of T issuing any appropriate
4388 diagnostics, figuring out which methods override which other
4389 methods, and so forth. */
4390
4391 static void
4392 check_methods (tree t)
4393 {
4394 for (tree x = TYPE_FIELDS (t); x; x = DECL_CHAIN (x))
4395 if (DECL_DECLARES_FUNCTION_P (x))
4396 {
4397 check_for_override (x, t);
4398
4399 if (DECL_PURE_VIRTUAL_P (x)
4400 && (TREE_CODE (x) != FUNCTION_DECL || ! DECL_VINDEX (x)))
4401 error ("initializer specified for non-virtual method %q+D", x);
4402 /* The name of the field is the original field name
4403 Save this in auxiliary field for later overloading. */
4404 if (TREE_CODE (x) == FUNCTION_DECL && DECL_VINDEX (x))
4405 {
4406 TYPE_POLYMORPHIC_P (t) = 1;
4407 if (DECL_PURE_VIRTUAL_P (x))
4408 vec_safe_push (CLASSTYPE_PURE_VIRTUALS (t), x);
4409 }
4410
4411 /* All user-provided destructors are non-trivial.
4412 Constructors and assignment ops are handled in
4413 grok_special_member_properties. */
4414 if (DECL_DESTRUCTOR_P (x) && user_provided_p (x))
4415 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t) = 1;
4416 if (!DECL_VIRTUAL_P (x)
4417 && lookup_attribute ("transaction_safe_dynamic",
4418 DECL_ATTRIBUTES (x)))
4419 error_at (DECL_SOURCE_LOCATION (x),
4420 "%<transaction_safe_dynamic%> may only be specified for "
4421 "a virtual function");
4422 }
4423 }
4424
4425 /* FN is a constructor or destructor. Clone the declaration to create
4426 a specialized in-charge or not-in-charge version, as indicated by
4427 NAME. */
4428
4429 static tree
4430 build_clone (tree fn, tree name)
4431 {
4432 tree parms;
4433 tree clone;
4434
4435 /* Copy the function. */
4436 clone = copy_decl (fn);
4437 /* Reset the function name. */
4438 DECL_NAME (clone) = name;
4439 /* Remember where this function came from. */
4440 DECL_ABSTRACT_ORIGIN (clone) = fn;
4441 /* Make it easy to find the CLONE given the FN. */
4442 DECL_CHAIN (clone) = DECL_CHAIN (fn);
4443 DECL_CHAIN (fn) = clone;
4444
4445 /* If this is a template, do the rest on the DECL_TEMPLATE_RESULT. */
4446 if (TREE_CODE (clone) == TEMPLATE_DECL)
4447 {
4448 tree result = build_clone (DECL_TEMPLATE_RESULT (clone), name);
4449 DECL_TEMPLATE_RESULT (clone) = result;
4450 DECL_TEMPLATE_INFO (result) = copy_node (DECL_TEMPLATE_INFO (result));
4451 DECL_TI_TEMPLATE (result) = clone;
4452 TREE_TYPE (clone) = TREE_TYPE (result);
4453 return clone;
4454 }
4455 else
4456 {
4457 // Clone constraints.
4458 if (flag_concepts)
4459 if (tree ci = get_constraints (fn))
4460 set_constraints (clone, copy_node (ci));
4461 }
4462
4463
4464 SET_DECL_ASSEMBLER_NAME (clone, NULL_TREE);
4465 DECL_CLONED_FUNCTION (clone) = fn;
4466 /* There's no pending inline data for this function. */
4467 DECL_PENDING_INLINE_INFO (clone) = NULL;
4468 DECL_PENDING_INLINE_P (clone) = 0;
4469
4470 /* The base-class destructor is not virtual. */
4471 if (name == base_dtor_identifier)
4472 {
4473 DECL_VIRTUAL_P (clone) = 0;
4474 if (TREE_CODE (clone) != TEMPLATE_DECL)
4475 DECL_VINDEX (clone) = NULL_TREE;
4476 }
4477
4478 bool ctor_omit_inherited_parms_p = ctor_omit_inherited_parms (clone);
4479 if (ctor_omit_inherited_parms_p)
4480 gcc_assert (DECL_HAS_IN_CHARGE_PARM_P (clone));
4481
4482 /* If there was an in-charge parameter, drop it from the function
4483 type. */
4484 if (DECL_HAS_IN_CHARGE_PARM_P (clone))
4485 {
4486 tree basetype;
4487 tree parmtypes;
4488 tree exceptions;
4489
4490 exceptions = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (clone));
4491 basetype = TYPE_METHOD_BASETYPE (TREE_TYPE (clone));
4492 parmtypes = TYPE_ARG_TYPES (TREE_TYPE (clone));
4493 /* Skip the `this' parameter. */
4494 parmtypes = TREE_CHAIN (parmtypes);
4495 /* Skip the in-charge parameter. */
4496 parmtypes = TREE_CHAIN (parmtypes);
4497 /* And the VTT parm, in a complete [cd]tor. */
4498 if (DECL_HAS_VTT_PARM_P (fn)
4499 && ! DECL_NEEDS_VTT_PARM_P (clone))
4500 parmtypes = TREE_CHAIN (parmtypes);
4501 if (ctor_omit_inherited_parms_p)
4502 {
4503 /* If we're omitting inherited parms, that just leaves the VTT. */
4504 gcc_assert (DECL_NEEDS_VTT_PARM_P (clone));
4505 parmtypes = tree_cons (NULL_TREE, vtt_parm_type, void_list_node);
4506 }
4507 TREE_TYPE (clone)
4508 = build_method_type_directly (basetype,
4509 TREE_TYPE (TREE_TYPE (clone)),
4510 parmtypes);
4511 if (exceptions)
4512 TREE_TYPE (clone) = build_exception_variant (TREE_TYPE (clone),
4513 exceptions);
4514 TREE_TYPE (clone)
4515 = cp_build_type_attribute_variant (TREE_TYPE (clone),
4516 TYPE_ATTRIBUTES (TREE_TYPE (fn)));
4517 }
4518
4519 /* Copy the function parameters. */
4520 DECL_ARGUMENTS (clone) = copy_list (DECL_ARGUMENTS (clone));
4521 /* Remove the in-charge parameter. */
4522 if (DECL_HAS_IN_CHARGE_PARM_P (clone))
4523 {
4524 DECL_CHAIN (DECL_ARGUMENTS (clone))
4525 = DECL_CHAIN (DECL_CHAIN (DECL_ARGUMENTS (clone)));
4526 DECL_HAS_IN_CHARGE_PARM_P (clone) = 0;
4527 }
4528 /* And the VTT parm, in a complete [cd]tor. */
4529 if (DECL_HAS_VTT_PARM_P (fn))
4530 {
4531 if (DECL_NEEDS_VTT_PARM_P (clone))
4532 DECL_HAS_VTT_PARM_P (clone) = 1;
4533 else
4534 {
4535 DECL_CHAIN (DECL_ARGUMENTS (clone))
4536 = DECL_CHAIN (DECL_CHAIN (DECL_ARGUMENTS (clone)));
4537 DECL_HAS_VTT_PARM_P (clone) = 0;
4538 }
4539 }
4540
4541 /* A base constructor inheriting from a virtual base doesn't get the
4542 arguments. */
4543 if (ctor_omit_inherited_parms_p)
4544 DECL_CHAIN (DECL_CHAIN (DECL_ARGUMENTS (clone))) = NULL_TREE;
4545
4546 for (parms = DECL_ARGUMENTS (clone); parms; parms = DECL_CHAIN (parms))
4547 {
4548 DECL_CONTEXT (parms) = clone;
4549 cxx_dup_lang_specific_decl (parms);
4550 }
4551
4552 /* Create the RTL for this function. */
4553 SET_DECL_RTL (clone, NULL);
4554 rest_of_decl_compilation (clone, /*top_level=*/1, at_eof);
4555
4556 return clone;
4557 }
4558
4559 /* Implementation of DECL_CLONED_FUNCTION and DECL_CLONED_FUNCTION_P, do
4560 not invoke this function directly.
4561
4562 For a non-thunk function, returns the address of the slot for storing
4563 the function it is a clone of. Otherwise returns NULL_TREE.
4564
4565 If JUST_TESTING, looks through TEMPLATE_DECL and returns NULL if
4566 cloned_function is unset. This is to support the separate
4567 DECL_CLONED_FUNCTION and DECL_CLONED_FUNCTION_P modes; using the latter
4568 on a template makes sense, but not the former. */
4569
4570 tree *
4571 decl_cloned_function_p (const_tree decl, bool just_testing)
4572 {
4573 tree *ptr;
4574 if (just_testing)
4575 decl = STRIP_TEMPLATE (decl);
4576
4577 if (TREE_CODE (decl) != FUNCTION_DECL
4578 || !DECL_LANG_SPECIFIC (decl)
4579 || DECL_LANG_SPECIFIC (decl)->u.fn.thunk_p)
4580 {
4581 #if defined ENABLE_TREE_CHECKING && (GCC_VERSION >= 2007)
4582 if (!just_testing)
4583 lang_check_failed (__FILE__, __LINE__, __FUNCTION__);
4584 else
4585 #endif
4586 return NULL;
4587 }
4588
4589 ptr = &DECL_LANG_SPECIFIC (decl)->u.fn.u5.cloned_function;
4590 if (just_testing && *ptr == NULL_TREE)
4591 return NULL;
4592 else
4593 return ptr;
4594 }
4595
4596 /* Produce declarations for all appropriate clones of FN. If
4597 UPDATE_METHODS is true, the clones are added to the
4598 CLASSTYPE_MEMBER_VEC. */
4599
4600 void
4601 clone_function_decl (tree fn, bool update_methods)
4602 {
4603 tree clone;
4604
4605 /* Avoid inappropriate cloning. */
4606 if (DECL_CHAIN (fn)
4607 && DECL_CLONED_FUNCTION_P (DECL_CHAIN (fn)))
4608 return;
4609
4610 if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (fn))
4611 {
4612 /* For each constructor, we need two variants: an in-charge version
4613 and a not-in-charge version. */
4614 clone = build_clone (fn, complete_ctor_identifier);
4615 if (update_methods)
4616 add_method (DECL_CONTEXT (clone), clone, false);
4617 clone = build_clone (fn, base_ctor_identifier);
4618 if (update_methods)
4619 add_method (DECL_CONTEXT (clone), clone, false);
4620 }
4621 else
4622 {
4623 gcc_assert (DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (fn));
4624
4625 /* For each destructor, we need three variants: an in-charge
4626 version, a not-in-charge version, and an in-charge deleting
4627 version. We clone the deleting version first because that
4628 means it will go second on the TYPE_FIELDS list -- and that
4629 corresponds to the correct layout order in the virtual
4630 function table.
4631
4632 For a non-virtual destructor, we do not build a deleting
4633 destructor. */
4634 if (DECL_VIRTUAL_P (fn))
4635 {
4636 clone = build_clone (fn, deleting_dtor_identifier);
4637 if (update_methods)
4638 add_method (DECL_CONTEXT (clone), clone, false);
4639 }
4640 clone = build_clone (fn, complete_dtor_identifier);
4641 if (update_methods)
4642 add_method (DECL_CONTEXT (clone), clone, false);
4643 clone = build_clone (fn, base_dtor_identifier);
4644 if (update_methods)
4645 add_method (DECL_CONTEXT (clone), clone, false);
4646 }
4647
4648 /* Note that this is an abstract function that is never emitted. */
4649 DECL_ABSTRACT_P (fn) = true;
4650 }
4651
4652 /* DECL is an in charge constructor, which is being defined. This will
4653 have had an in class declaration, from whence clones were
4654 declared. An out-of-class definition can specify additional default
4655 arguments. As it is the clones that are involved in overload
4656 resolution, we must propagate the information from the DECL to its
4657 clones. */
4658
4659 void
4660 adjust_clone_args (tree decl)
4661 {
4662 tree clone;
4663
4664 for (clone = DECL_CHAIN (decl); clone && DECL_CLONED_FUNCTION_P (clone);
4665 clone = DECL_CHAIN (clone))
4666 {
4667 tree orig_clone_parms = TYPE_ARG_TYPES (TREE_TYPE (clone));
4668 tree orig_decl_parms = TYPE_ARG_TYPES (TREE_TYPE (decl));
4669 tree decl_parms, clone_parms;
4670
4671 clone_parms = orig_clone_parms;
4672
4673 /* Skip the 'this' parameter. */
4674 orig_clone_parms = TREE_CHAIN (orig_clone_parms);
4675 orig_decl_parms = TREE_CHAIN (orig_decl_parms);
4676
4677 if (DECL_HAS_IN_CHARGE_PARM_P (decl))
4678 orig_decl_parms = TREE_CHAIN (orig_decl_parms);
4679 if (DECL_HAS_VTT_PARM_P (decl))
4680 orig_decl_parms = TREE_CHAIN (orig_decl_parms);
4681
4682 clone_parms = orig_clone_parms;
4683 if (DECL_HAS_VTT_PARM_P (clone))
4684 clone_parms = TREE_CHAIN (clone_parms);
4685
4686 for (decl_parms = orig_decl_parms; decl_parms;
4687 decl_parms = TREE_CHAIN (decl_parms),
4688 clone_parms = TREE_CHAIN (clone_parms))
4689 {
4690 if (clone_parms == void_list_node)
4691 {
4692 gcc_assert (decl_parms == clone_parms
4693 || ctor_omit_inherited_parms (clone));
4694 break;
4695 }
4696
4697 gcc_assert (same_type_p (TREE_TYPE (decl_parms),
4698 TREE_TYPE (clone_parms)));
4699
4700 if (TREE_PURPOSE (decl_parms) && !TREE_PURPOSE (clone_parms))
4701 {
4702 /* A default parameter has been added. Adjust the
4703 clone's parameters. */
4704 tree exceptions = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (clone));
4705 tree attrs = TYPE_ATTRIBUTES (TREE_TYPE (clone));
4706 tree basetype = TYPE_METHOD_BASETYPE (TREE_TYPE (clone));
4707 tree type;
4708
4709 clone_parms = orig_decl_parms;
4710
4711 if (DECL_HAS_VTT_PARM_P (clone))
4712 {
4713 clone_parms = tree_cons (TREE_PURPOSE (orig_clone_parms),
4714 TREE_VALUE (orig_clone_parms),
4715 clone_parms);
4716 TREE_TYPE (clone_parms) = TREE_TYPE (orig_clone_parms);
4717 }
4718 type = build_method_type_directly (basetype,
4719 TREE_TYPE (TREE_TYPE (clone)),
4720 clone_parms);
4721 if (exceptions)
4722 type = build_exception_variant (type, exceptions);
4723 if (attrs)
4724 type = cp_build_type_attribute_variant (type, attrs);
4725 TREE_TYPE (clone) = type;
4726
4727 clone_parms = NULL_TREE;
4728 break;
4729 }
4730 }
4731 gcc_assert (!clone_parms || clone_parms == void_list_node);
4732 }
4733 }
4734
4735 /* For each of the constructors and destructors in T, create an
4736 in-charge and not-in-charge variant. */
4737
4738 static void
4739 clone_constructors_and_destructors (tree t)
4740 {
4741 /* While constructors can be via a using declaration, at this point
4742 we no longer need to know that. */
4743 for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (t)); iter; ++iter)
4744 clone_function_decl (*iter, /*update_methods=*/true);
4745
4746 if (tree dtor = CLASSTYPE_DESTRUCTOR (t))
4747 clone_function_decl (dtor, /*update_methods=*/true);
4748 }
4749
4750 /* Deduce noexcept for a destructor DTOR. */
4751
4752 void
4753 deduce_noexcept_on_destructor (tree dtor)
4754 {
4755 if (!TYPE_RAISES_EXCEPTIONS (TREE_TYPE (dtor)))
4756 TREE_TYPE (dtor) = build_exception_variant (TREE_TYPE (dtor),
4757 noexcept_deferred_spec);
4758 }
4759
4760 /* Subroutine of set_one_vmethod_tm_attributes. Search base classes
4761 of TYPE for virtual functions which FNDECL overrides. Return a
4762 mask of the tm attributes found therein. */
4763
4764 static int
4765 look_for_tm_attr_overrides (tree type, tree fndecl)
4766 {
4767 tree binfo = TYPE_BINFO (type);
4768 tree base_binfo;
4769 int ix, found = 0;
4770
4771 for (ix = 0; BINFO_BASE_ITERATE (binfo, ix, base_binfo); ++ix)
4772 {
4773 tree o, basetype = BINFO_TYPE (base_binfo);
4774
4775 if (!TYPE_POLYMORPHIC_P (basetype))
4776 continue;
4777
4778 o = look_for_overrides_here (basetype, fndecl);
4779 if (o)
4780 {
4781 if (lookup_attribute ("transaction_safe_dynamic",
4782 DECL_ATTRIBUTES (o)))
4783 /* transaction_safe_dynamic is not inherited. */;
4784 else
4785 found |= tm_attr_to_mask (find_tm_attribute
4786 (TYPE_ATTRIBUTES (TREE_TYPE (o))));
4787 }
4788 else
4789 found |= look_for_tm_attr_overrides (basetype, fndecl);
4790 }
4791
4792 return found;
4793 }
4794
4795 /* Subroutine of set_method_tm_attributes. Handle the checks and
4796 inheritance for one virtual method FNDECL. */
4797
4798 static void
4799 set_one_vmethod_tm_attributes (tree type, tree fndecl)
4800 {
4801 tree tm_attr;
4802 int found, have;
4803
4804 found = look_for_tm_attr_overrides (type, fndecl);
4805
4806 /* If FNDECL doesn't actually override anything (i.e. T is the
4807 class that first declares FNDECL virtual), then we're done. */
4808 if (found == 0)
4809 return;
4810
4811 tm_attr = find_tm_attribute (TYPE_ATTRIBUTES (TREE_TYPE (fndecl)));
4812 have = tm_attr_to_mask (tm_attr);
4813
4814 /* Intel STM Language Extension 3.0, Section 4.2 table 4:
4815 tm_pure must match exactly, otherwise no weakening of
4816 tm_safe > tm_callable > nothing. */
4817 /* ??? The tm_pure attribute didn't make the transition to the
4818 multivendor language spec. */
4819 if (have == TM_ATTR_PURE)
4820 {
4821 if (found != TM_ATTR_PURE)
4822 {
4823 found &= -found;
4824 goto err_override;
4825 }
4826 }
4827 /* If the overridden function is tm_pure, then FNDECL must be. */
4828 else if (found == TM_ATTR_PURE && tm_attr)
4829 goto err_override;
4830 /* Look for base class combinations that cannot be satisfied. */
4831 else if (found != TM_ATTR_PURE && (found & TM_ATTR_PURE))
4832 {
4833 found &= ~TM_ATTR_PURE;
4834 found &= -found;
4835 error_at (DECL_SOURCE_LOCATION (fndecl),
4836 "method overrides both %<transaction_pure%> and %qE methods",
4837 tm_mask_to_attr (found));
4838 }
4839 /* If FNDECL did not declare an attribute, then inherit the most
4840 restrictive one. */
4841 else if (tm_attr == NULL)
4842 {
4843 apply_tm_attr (fndecl, tm_mask_to_attr (least_bit_hwi (found)));
4844 }
4845 /* Otherwise validate that we're not weaker than a function
4846 that is being overridden. */
4847 else
4848 {
4849 found &= -found;
4850 if (found <= TM_ATTR_CALLABLE && have > found)
4851 goto err_override;
4852 }
4853 return;
4854
4855 err_override:
4856 error_at (DECL_SOURCE_LOCATION (fndecl),
4857 "method declared %qE overriding %qE method",
4858 tm_attr, tm_mask_to_attr (found));
4859 }
4860
4861 /* For each of the methods in T, propagate a class-level tm attribute. */
4862
4863 static void
4864 set_method_tm_attributes (tree t)
4865 {
4866 tree class_tm_attr, fndecl;
4867
4868 /* Don't bother collecting tm attributes if transactional memory
4869 support is not enabled. */
4870 if (!flag_tm)
4871 return;
4872
4873 /* Process virtual methods first, as they inherit directly from the
4874 base virtual function and also require validation of new attributes. */
4875 if (TYPE_CONTAINS_VPTR_P (t))
4876 {
4877 tree vchain;
4878 for (vchain = BINFO_VIRTUALS (TYPE_BINFO (t)); vchain;
4879 vchain = TREE_CHAIN (vchain))
4880 {
4881 fndecl = BV_FN (vchain);
4882 if (DECL_THUNK_P (fndecl))
4883 fndecl = THUNK_TARGET (fndecl);
4884 set_one_vmethod_tm_attributes (t, fndecl);
4885 }
4886 }
4887
4888 /* If the class doesn't have an attribute, nothing more to do. */
4889 class_tm_attr = find_tm_attribute (TYPE_ATTRIBUTES (t));
4890 if (class_tm_attr == NULL)
4891 return;
4892
4893 /* Any method that does not yet have a tm attribute inherits
4894 the one from the class. */
4895 for (fndecl = TYPE_FIELDS (t); fndecl; fndecl = DECL_CHAIN (fndecl))
4896 if (DECL_DECLARES_FUNCTION_P (fndecl)
4897 && !find_tm_attribute (TYPE_ATTRIBUTES (TREE_TYPE (fndecl))))
4898 apply_tm_attr (fndecl, class_tm_attr);
4899 }
4900
4901 /* Returns true if FN is a default constructor. */
4902
4903 bool
4904 default_ctor_p (tree fn)
4905 {
4906 return (DECL_CONSTRUCTOR_P (fn)
4907 && sufficient_parms_p (FUNCTION_FIRST_USER_PARMTYPE (fn)));
4908 }
4909
4910 /* Returns true iff class T has a user-defined constructor that can be called
4911 with more than zero arguments. */
4912
4913 bool
4914 type_has_user_nondefault_constructor (tree t)
4915 {
4916 if (!TYPE_HAS_USER_CONSTRUCTOR (t))
4917 return false;
4918
4919 for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (t)); iter; ++iter)
4920 {
4921 tree fn = *iter;
4922 if (!DECL_ARTIFICIAL (fn)
4923 && (TREE_CODE (fn) == TEMPLATE_DECL
4924 || (skip_artificial_parms_for (fn, DECL_ARGUMENTS (fn))
4925 != NULL_TREE)))
4926 return true;
4927 }
4928
4929 return false;
4930 }
4931
4932 /* Returns the defaulted constructor if T has one. Otherwise, returns
4933 NULL_TREE. */
4934
4935 tree
4936 in_class_defaulted_default_constructor (tree t)
4937 {
4938 if (!TYPE_HAS_USER_CONSTRUCTOR (t))
4939 return NULL_TREE;
4940
4941 for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (t)); iter; ++iter)
4942 {
4943 tree fn = *iter;
4944
4945 if (DECL_DEFAULTED_IN_CLASS_P (fn)
4946 && default_ctor_p (fn))
4947 return fn;
4948 }
4949
4950 return NULL_TREE;
4951 }
4952
4953 /* Returns true iff FN is a user-provided function, i.e. user-declared
4954 and not defaulted at its first declaration. */
4955
4956 bool
4957 user_provided_p (tree fn)
4958 {
4959 if (TREE_CODE (fn) == TEMPLATE_DECL)
4960 return true;
4961 else
4962 return (!DECL_ARTIFICIAL (fn)
4963 && !(DECL_INITIALIZED_IN_CLASS_P (fn)
4964 && (DECL_DEFAULTED_FN (fn) || DECL_DELETED_FN (fn))));
4965 }
4966
4967 /* Returns true iff class T has a user-provided constructor. */
4968
4969 bool
4970 type_has_user_provided_constructor (tree t)
4971 {
4972 if (!CLASS_TYPE_P (t))
4973 return false;
4974
4975 if (!TYPE_HAS_USER_CONSTRUCTOR (t))
4976 return false;
4977
4978 for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (t)); iter; ++iter)
4979 if (user_provided_p (*iter))
4980 return true;
4981
4982 return false;
4983 }
4984
4985 /* Returns true iff class T has a user-provided or explicit constructor. */
4986
4987 bool
4988 type_has_user_provided_or_explicit_constructor (tree t)
4989 {
4990 if (!CLASS_TYPE_P (t))
4991 return false;
4992
4993 if (!TYPE_HAS_USER_CONSTRUCTOR (t))
4994 return false;
4995
4996 for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (t)); iter; ++iter)
4997 {
4998 tree fn = *iter;
4999 if (user_provided_p (fn) || DECL_NONCONVERTING_P (fn))
5000 return true;
5001 }
5002
5003 return false;
5004 }
5005
5006 /* Returns true iff class T has a non-user-provided (i.e. implicitly
5007 declared or explicitly defaulted in the class body) default
5008 constructor. */
5009
5010 bool
5011 type_has_non_user_provided_default_constructor (tree t)
5012 {
5013 if (!TYPE_HAS_DEFAULT_CONSTRUCTOR (t))
5014 return false;
5015 if (CLASSTYPE_LAZY_DEFAULT_CTOR (t))
5016 return true;
5017
5018 for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (t)); iter; ++iter)
5019 {
5020 tree fn = *iter;
5021 if (TREE_CODE (fn) == FUNCTION_DECL
5022 && default_ctor_p (fn)
5023 && !user_provided_p (fn))
5024 return true;
5025 }
5026
5027 return false;
5028 }
5029
5030 /* TYPE is being used as a virtual base, and has a non-trivial move
5031 assignment. Return true if this is due to there being a user-provided
5032 move assignment in TYPE or one of its subobjects; if there isn't, then
5033 multiple move assignment can't cause any harm. */
5034
5035 bool
5036 vbase_has_user_provided_move_assign (tree type)
5037 {
5038 /* Does the type itself have a user-provided move assignment operator? */
5039 if (!CLASSTYPE_LAZY_MOVE_ASSIGN (type))
5040 for (ovl_iterator iter (get_class_binding_direct
5041 (type, assign_op_identifier));
5042 iter; ++iter)
5043 if (!DECL_ARTIFICIAL (*iter) && move_fn_p (*iter))
5044 return true;
5045
5046 /* Do any of its bases? */
5047 tree binfo = TYPE_BINFO (type);
5048 tree base_binfo;
5049 for (int i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
5050 if (vbase_has_user_provided_move_assign (BINFO_TYPE (base_binfo)))
5051 return true;
5052
5053 /* Or non-static data members? */
5054 for (tree field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
5055 {
5056 if (TREE_CODE (field) == FIELD_DECL
5057 && CLASS_TYPE_P (TREE_TYPE (field))
5058 && vbase_has_user_provided_move_assign (TREE_TYPE (field)))
5059 return true;
5060 }
5061
5062 /* Seems not. */
5063 return false;
5064 }
5065
5066 /* If default-initialization leaves part of TYPE uninitialized, returns
5067 a DECL for the field or TYPE itself (DR 253). */
5068
5069 tree
5070 default_init_uninitialized_part (tree type)
5071 {
5072 tree t, r, binfo;
5073 int i;
5074
5075 type = strip_array_types (type);
5076 if (!CLASS_TYPE_P (type))
5077 return type;
5078 if (!type_has_non_user_provided_default_constructor (type))
5079 return NULL_TREE;
5080 for (binfo = TYPE_BINFO (type), i = 0;
5081 BINFO_BASE_ITERATE (binfo, i, t); ++i)
5082 {
5083 r = default_init_uninitialized_part (BINFO_TYPE (t));
5084 if (r)
5085 return r;
5086 }
5087 for (t = TYPE_FIELDS (type); t; t = DECL_CHAIN (t))
5088 if (TREE_CODE (t) == FIELD_DECL
5089 && !DECL_ARTIFICIAL (t)
5090 && !DECL_INITIAL (t))
5091 {
5092 r = default_init_uninitialized_part (TREE_TYPE (t));
5093 if (r)
5094 return DECL_P (r) ? r : t;
5095 }
5096
5097 return NULL_TREE;
5098 }
5099
5100 /* Returns true iff for class T, a trivial synthesized default constructor
5101 would be constexpr. */
5102
5103 bool
5104 trivial_default_constructor_is_constexpr (tree t)
5105 {
5106 /* A defaulted trivial default constructor is constexpr
5107 if there is nothing to initialize. */
5108 gcc_assert (!TYPE_HAS_COMPLEX_DFLT (t));
5109 return is_really_empty_class (t);
5110 }
5111
5112 /* Returns true iff class T has a constexpr default constructor. */
5113
5114 bool
5115 type_has_constexpr_default_constructor (tree t)
5116 {
5117 tree fns;
5118
5119 if (!CLASS_TYPE_P (t))
5120 {
5121 /* The caller should have stripped an enclosing array. */
5122 gcc_assert (TREE_CODE (t) != ARRAY_TYPE);
5123 return false;
5124 }
5125 if (CLASSTYPE_LAZY_DEFAULT_CTOR (t))
5126 {
5127 if (!TYPE_HAS_COMPLEX_DFLT (t))
5128 return trivial_default_constructor_is_constexpr (t);
5129 /* Non-trivial, we need to check subobject constructors. */
5130 lazily_declare_fn (sfk_constructor, t);
5131 }
5132 fns = locate_ctor (t);
5133 return (fns && DECL_DECLARED_CONSTEXPR_P (fns));
5134 }
5135
5136 /* Returns true iff class T has a constexpr default constructor or has an
5137 implicitly declared default constructor that we can't tell if it's constexpr
5138 without forcing a lazy declaration (which might cause undesired
5139 instantiations). */
5140
5141 bool
5142 type_maybe_constexpr_default_constructor (tree t)
5143 {
5144 if (CLASS_TYPE_P (t) && CLASSTYPE_LAZY_DEFAULT_CTOR (t)
5145 && TYPE_HAS_COMPLEX_DFLT (t))
5146 /* Assume it's constexpr. */
5147 return true;
5148 return type_has_constexpr_default_constructor (t);
5149 }
5150
5151 /* Returns true iff class TYPE has a virtual destructor. */
5152
5153 bool
5154 type_has_virtual_destructor (tree type)
5155 {
5156 tree dtor;
5157
5158 if (!CLASS_TYPE_P (type))
5159 return false;
5160
5161 gcc_assert (COMPLETE_TYPE_P (type));
5162 dtor = CLASSTYPE_DESTRUCTOR (type);
5163 return (dtor && DECL_VIRTUAL_P (dtor));
5164 }
5165
5166 /* Returns true iff T, a class, has a move-assignment or
5167 move-constructor. Does not lazily declare either.
5168 If USER_P is false, any move function will do. If it is true, the
5169 move function must be user-declared.
5170
5171 Note that user-declared here is different from "user-provided",
5172 which doesn't include functions that are defaulted in the
5173 class. */
5174
5175 bool
5176 classtype_has_move_assign_or_move_ctor_p (tree t, bool user_p)
5177 {
5178 gcc_assert (user_p
5179 || (!CLASSTYPE_LAZY_MOVE_CTOR (t)
5180 && !CLASSTYPE_LAZY_MOVE_ASSIGN (t)));
5181
5182 if (!CLASSTYPE_LAZY_MOVE_CTOR (t))
5183 for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (t)); iter; ++iter)
5184 if ((!user_p || !DECL_ARTIFICIAL (*iter)) && move_fn_p (*iter))
5185 return true;
5186
5187 if (!CLASSTYPE_LAZY_MOVE_ASSIGN (t))
5188 for (ovl_iterator iter (get_class_binding_direct
5189 (t, assign_op_identifier));
5190 iter; ++iter)
5191 if ((!user_p || !DECL_ARTIFICIAL (*iter)) && move_fn_p (*iter))
5192 return true;
5193
5194 return false;
5195 }
5196
5197 /* Nonzero if we need to build up a constructor call when initializing an
5198 object of this class, either because it has a user-declared constructor
5199 or because it doesn't have a default constructor (so we need to give an
5200 error if no initializer is provided). Use TYPE_NEEDS_CONSTRUCTING when
5201 what you care about is whether or not an object can be produced by a
5202 constructor (e.g. so we don't set TREE_READONLY on const variables of
5203 such type); use this function when what you care about is whether or not
5204 to try to call a constructor to create an object. The latter case is
5205 the former plus some cases of constructors that cannot be called. */
5206
5207 bool
5208 type_build_ctor_call (tree t)
5209 {
5210 tree inner;
5211 if (TYPE_NEEDS_CONSTRUCTING (t))
5212 return true;
5213 inner = strip_array_types (t);
5214 if (!CLASS_TYPE_P (inner) || ANON_AGGR_TYPE_P (inner))
5215 return false;
5216 if (!TYPE_HAS_DEFAULT_CONSTRUCTOR (inner))
5217 return true;
5218 if (cxx_dialect < cxx11)
5219 return false;
5220 /* A user-declared constructor might be private, and a constructor might
5221 be trivial but deleted. */
5222 for (ovl_iterator iter (get_class_binding (inner, complete_ctor_identifier));
5223 iter; ++iter)
5224 {
5225 tree fn = *iter;
5226 if (!DECL_ARTIFICIAL (fn)
5227 || DECL_DELETED_FN (fn))
5228 return true;
5229 }
5230 return false;
5231 }
5232
5233 /* Like type_build_ctor_call, but for destructors. */
5234
5235 bool
5236 type_build_dtor_call (tree t)
5237 {
5238 tree inner;
5239 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t))
5240 return true;
5241 inner = strip_array_types (t);
5242 if (!CLASS_TYPE_P (inner) || ANON_AGGR_TYPE_P (inner)
5243 || !COMPLETE_TYPE_P (inner))
5244 return false;
5245 if (cxx_dialect < cxx11)
5246 return false;
5247 /* A user-declared destructor might be private, and a destructor might
5248 be trivial but deleted. */
5249 for (ovl_iterator iter (get_class_binding (inner, complete_dtor_identifier));
5250 iter; ++iter)
5251 {
5252 tree fn = *iter;
5253 if (!DECL_ARTIFICIAL (fn)
5254 || DECL_DELETED_FN (fn))
5255 return true;
5256 }
5257 return false;
5258 }
5259
5260 /* Remove all zero-width bit-fields from T. */
5261
5262 static void
5263 remove_zero_width_bit_fields (tree t)
5264 {
5265 tree *fieldsp;
5266
5267 fieldsp = &TYPE_FIELDS (t);
5268 while (*fieldsp)
5269 {
5270 if (TREE_CODE (*fieldsp) == FIELD_DECL
5271 && DECL_C_BIT_FIELD (*fieldsp)
5272 /* We should not be confused by the fact that grokbitfield
5273 temporarily sets the width of the bit field into
5274 DECL_BIT_FIELD_REPRESENTATIVE (*fieldsp).
5275 check_bitfield_decl eventually sets DECL_SIZE (*fieldsp)
5276 to that width. */
5277 && (DECL_SIZE (*fieldsp) == NULL_TREE
5278 || integer_zerop (DECL_SIZE (*fieldsp))))
5279 *fieldsp = DECL_CHAIN (*fieldsp);
5280 else
5281 fieldsp = &DECL_CHAIN (*fieldsp);
5282 }
5283 }
5284
5285 /* Returns TRUE iff we need a cookie when dynamically allocating an
5286 array whose elements have the indicated class TYPE. */
5287
5288 static bool
5289 type_requires_array_cookie (tree type)
5290 {
5291 tree fns;
5292 bool has_two_argument_delete_p = false;
5293
5294 gcc_assert (CLASS_TYPE_P (type));
5295
5296 /* If there's a non-trivial destructor, we need a cookie. In order
5297 to iterate through the array calling the destructor for each
5298 element, we'll have to know how many elements there are. */
5299 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
5300 return true;
5301
5302 /* If the usual deallocation function is a two-argument whose second
5303 argument is of type `size_t', then we have to pass the size of
5304 the array to the deallocation function, so we will need to store
5305 a cookie. */
5306 fns = lookup_fnfields (TYPE_BINFO (type),
5307 ovl_op_identifier (false, VEC_DELETE_EXPR),
5308 /*protect=*/0);
5309 /* If there are no `operator []' members, or the lookup is
5310 ambiguous, then we don't need a cookie. */
5311 if (!fns || fns == error_mark_node)
5312 return false;
5313 /* Loop through all of the functions. */
5314 for (lkp_iterator iter (BASELINK_FUNCTIONS (fns)); iter; ++iter)
5315 {
5316 tree fn = *iter;
5317
5318 /* See if this function is a one-argument delete function. If
5319 it is, then it will be the usual deallocation function. */
5320 tree second_parm = TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (fn)));
5321 if (second_parm == void_list_node)
5322 return false;
5323 /* Do not consider this function if its second argument is an
5324 ellipsis. */
5325 if (!second_parm)
5326 continue;
5327 /* Otherwise, if we have a two-argument function and the second
5328 argument is `size_t', it will be the usual deallocation
5329 function -- unless there is one-argument function, too. */
5330 if (TREE_CHAIN (second_parm) == void_list_node
5331 && same_type_p (TREE_VALUE (second_parm), size_type_node))
5332 has_two_argument_delete_p = true;
5333 }
5334
5335 return has_two_argument_delete_p;
5336 }
5337
5338 /* Finish computing the `literal type' property of class type T.
5339
5340 At this point, we have already processed base classes and
5341 non-static data members. We need to check whether the copy
5342 constructor is trivial, the destructor is trivial, and there
5343 is a trivial default constructor or at least one constexpr
5344 constructor other than the copy constructor. */
5345
5346 static void
5347 finalize_literal_type_property (tree t)
5348 {
5349 tree fn;
5350
5351 if (cxx_dialect < cxx11
5352 || TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t))
5353 CLASSTYPE_LITERAL_P (t) = false;
5354 else if (CLASSTYPE_LITERAL_P (t) && LAMBDA_TYPE_P (t))
5355 CLASSTYPE_LITERAL_P (t) = (cxx_dialect >= cxx17);
5356 else if (CLASSTYPE_LITERAL_P (t) && !TYPE_HAS_TRIVIAL_DFLT (t)
5357 && CLASSTYPE_NON_AGGREGATE (t)
5358 && !TYPE_HAS_CONSTEXPR_CTOR (t))
5359 CLASSTYPE_LITERAL_P (t) = false;
5360
5361 /* C++14 DR 1684 removed this restriction. */
5362 if (cxx_dialect < cxx14
5363 && !CLASSTYPE_LITERAL_P (t) && !LAMBDA_TYPE_P (t))
5364 for (fn = TYPE_FIELDS (t); fn; fn = DECL_CHAIN (fn))
5365 if (TREE_CODE (fn) == FUNCTION_DECL
5366 && DECL_DECLARED_CONSTEXPR_P (fn)
5367 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
5368 && !DECL_CONSTRUCTOR_P (fn))
5369 {
5370 DECL_DECLARED_CONSTEXPR_P (fn) = false;
5371 if (!DECL_GENERATED_P (fn)
5372 && pedwarn (DECL_SOURCE_LOCATION (fn), OPT_Wpedantic,
5373 "enclosing class of %<constexpr%> non-static member "
5374 "function %q+#D is not a literal type", fn))
5375 explain_non_literal_class (t);
5376 }
5377 }
5378
5379 /* T is a non-literal type used in a context which requires a constant
5380 expression. Explain why it isn't literal. */
5381
5382 void
5383 explain_non_literal_class (tree t)
5384 {
5385 static hash_set<tree> *diagnosed;
5386
5387 if (!CLASS_TYPE_P (t))
5388 return;
5389 t = TYPE_MAIN_VARIANT (t);
5390
5391 if (diagnosed == NULL)
5392 diagnosed = new hash_set<tree>;
5393 if (diagnosed->add (t))
5394 /* Already explained. */
5395 return;
5396
5397 inform (UNKNOWN_LOCATION, "%q+T is not literal because:", t);
5398 if (cxx_dialect < cxx17 && LAMBDA_TYPE_P (t))
5399 inform (UNKNOWN_LOCATION,
5400 " %qT is a closure type, which is only literal in "
5401 "C++17 and later", t);
5402 else if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t))
5403 inform (UNKNOWN_LOCATION, " %q+T has a non-trivial destructor", t);
5404 else if (CLASSTYPE_NON_AGGREGATE (t)
5405 && !TYPE_HAS_TRIVIAL_DFLT (t)
5406 && !LAMBDA_TYPE_P (t)
5407 && !TYPE_HAS_CONSTEXPR_CTOR (t))
5408 {
5409 inform (UNKNOWN_LOCATION,
5410 " %q+T is not an aggregate, does not have a trivial "
5411 "default constructor, and has no %<constexpr%> constructor that "
5412 "is not a copy or move constructor", t);
5413 if (type_has_non_user_provided_default_constructor (t))
5414 /* Note that we can't simply call locate_ctor because when the
5415 constructor is deleted it just returns NULL_TREE. */
5416 for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (t)); iter; ++iter)
5417 {
5418 tree fn = *iter;
5419 tree parms = TYPE_ARG_TYPES (TREE_TYPE (fn));
5420
5421 parms = skip_artificial_parms_for (fn, parms);
5422
5423 if (sufficient_parms_p (parms))
5424 {
5425 if (DECL_DELETED_FN (fn))
5426 maybe_explain_implicit_delete (fn);
5427 else
5428 explain_invalid_constexpr_fn (fn);
5429 break;
5430 }
5431 }
5432 }
5433 else
5434 {
5435 tree binfo, base_binfo, field; int i;
5436 for (binfo = TYPE_BINFO (t), i = 0;
5437 BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
5438 {
5439 tree basetype = TREE_TYPE (base_binfo);
5440 if (!CLASSTYPE_LITERAL_P (basetype))
5441 {
5442 inform (UNKNOWN_LOCATION,
5443 " base class %qT of %q+T is non-literal",
5444 basetype, t);
5445 explain_non_literal_class (basetype);
5446 return;
5447 }
5448 }
5449 for (field = TYPE_FIELDS (t); field; field = TREE_CHAIN (field))
5450 {
5451 tree ftype;
5452 if (TREE_CODE (field) != FIELD_DECL)
5453 continue;
5454 ftype = TREE_TYPE (field);
5455 if (!literal_type_p (ftype))
5456 {
5457 inform (DECL_SOURCE_LOCATION (field),
5458 " non-static data member %qD has non-literal type",
5459 field);
5460 if (CLASS_TYPE_P (ftype))
5461 explain_non_literal_class (ftype);
5462 }
5463 if (CP_TYPE_VOLATILE_P (ftype))
5464 inform (DECL_SOURCE_LOCATION (field),
5465 " non-static data member %qD has volatile type", field);
5466 }
5467 }
5468 }
5469
5470 /* Check the validity of the bases and members declared in T. Add any
5471 implicitly-generated functions (like copy-constructors and
5472 assignment operators). Compute various flag bits (like
5473 CLASSTYPE_NON_LAYOUT_POD_T) for T. This routine works purely at the C++
5474 level: i.e., independently of the ABI in use. */
5475
5476 static void
5477 check_bases_and_members (tree t)
5478 {
5479 /* Nonzero if the implicitly generated copy constructor should take
5480 a non-const reference argument. */
5481 int cant_have_const_ctor;
5482 /* Nonzero if the implicitly generated assignment operator
5483 should take a non-const reference argument. */
5484 int no_const_asn_ref;
5485 tree access_decls;
5486 bool saved_complex_asn_ref;
5487 bool saved_nontrivial_dtor;
5488 tree fn;
5489
5490 /* By default, we use const reference arguments and generate default
5491 constructors. */
5492 cant_have_const_ctor = 0;
5493 no_const_asn_ref = 0;
5494
5495 /* Check all the base-classes and set FMEM members to point to arrays
5496 of potential interest. */
5497 check_bases (t, &cant_have_const_ctor, &no_const_asn_ref);
5498
5499 /* Deduce noexcept on destructor. This needs to happen after we've set
5500 triviality flags appropriately for our bases. */
5501 if (cxx_dialect >= cxx11)
5502 if (tree dtor = CLASSTYPE_DESTRUCTOR (t))
5503 deduce_noexcept_on_destructor (dtor);
5504
5505 /* Check all the method declarations. */
5506 check_methods (t);
5507
5508 /* Save the initial values of these flags which only indicate whether
5509 or not the class has user-provided functions. As we analyze the
5510 bases and members we can set these flags for other reasons. */
5511 saved_complex_asn_ref = TYPE_HAS_COMPLEX_COPY_ASSIGN (t);
5512 saved_nontrivial_dtor = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t);
5513
5514 /* Check all the data member declarations. We cannot call
5515 check_field_decls until we have called check_bases check_methods,
5516 as check_field_decls depends on TYPE_HAS_NONTRIVIAL_DESTRUCTOR
5517 being set appropriately. */
5518 check_field_decls (t, &access_decls,
5519 &cant_have_const_ctor,
5520 &no_const_asn_ref);
5521
5522 /* A nearly-empty class has to be vptr-containing; a nearly empty
5523 class contains just a vptr. */
5524 if (!TYPE_CONTAINS_VPTR_P (t))
5525 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
5526
5527 /* Do some bookkeeping that will guide the generation of implicitly
5528 declared member functions. */
5529 TYPE_HAS_COMPLEX_COPY_CTOR (t) |= TYPE_CONTAINS_VPTR_P (t);
5530 TYPE_HAS_COMPLEX_MOVE_CTOR (t) |= TYPE_CONTAINS_VPTR_P (t);
5531 /* We need to call a constructor for this class if it has a
5532 user-provided constructor, or if the default constructor is going
5533 to initialize the vptr. (This is not an if-and-only-if;
5534 TYPE_NEEDS_CONSTRUCTING is set elsewhere if bases or members
5535 themselves need constructing.) */
5536 TYPE_NEEDS_CONSTRUCTING (t)
5537 |= (type_has_user_provided_constructor (t) || TYPE_CONTAINS_VPTR_P (t));
5538 /* [dcl.init.aggr]
5539
5540 An aggregate is an array or a class with no user-provided
5541 constructors ... and no virtual functions.
5542
5543 Again, other conditions for being an aggregate are checked
5544 elsewhere. */
5545 CLASSTYPE_NON_AGGREGATE (t)
5546 |= (type_has_user_provided_or_explicit_constructor (t)
5547 || TYPE_POLYMORPHIC_P (t));
5548 /* This is the C++98/03 definition of POD; it changed in C++0x, but we
5549 retain the old definition internally for ABI reasons. */
5550 CLASSTYPE_NON_LAYOUT_POD_P (t)
5551 |= (CLASSTYPE_NON_AGGREGATE (t)
5552 || saved_nontrivial_dtor || saved_complex_asn_ref);
5553 CLASSTYPE_NON_STD_LAYOUT (t) |= TYPE_CONTAINS_VPTR_P (t);
5554 TYPE_HAS_COMPLEX_COPY_ASSIGN (t) |= TYPE_CONTAINS_VPTR_P (t);
5555 TYPE_HAS_COMPLEX_MOVE_ASSIGN (t) |= TYPE_CONTAINS_VPTR_P (t);
5556 TYPE_HAS_COMPLEX_DFLT (t) |= TYPE_CONTAINS_VPTR_P (t);
5557
5558 /* If the only explicitly declared default constructor is user-provided,
5559 set TYPE_HAS_COMPLEX_DFLT. */
5560 if (!TYPE_HAS_COMPLEX_DFLT (t)
5561 && TYPE_HAS_DEFAULT_CONSTRUCTOR (t)
5562 && !type_has_non_user_provided_default_constructor (t))
5563 TYPE_HAS_COMPLEX_DFLT (t) = true;
5564
5565 /* Warn if a public base of a polymorphic type has an accessible
5566 non-virtual destructor. It is only now that we know the class is
5567 polymorphic. Although a polymorphic base will have a already
5568 been diagnosed during its definition, we warn on use too. */
5569 if (TYPE_POLYMORPHIC_P (t) && warn_nonvdtor)
5570 {
5571 tree binfo = TYPE_BINFO (t);
5572 vec<tree, va_gc> *accesses = BINFO_BASE_ACCESSES (binfo);
5573 tree base_binfo;
5574 unsigned i;
5575
5576 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
5577 {
5578 tree basetype = TREE_TYPE (base_binfo);
5579
5580 if ((*accesses)[i] == access_public_node
5581 && (TYPE_POLYMORPHIC_P (basetype) || warn_ecpp)
5582 && accessible_nvdtor_p (basetype))
5583 warning (OPT_Wnon_virtual_dtor,
5584 "base class %q#T has accessible non-virtual destructor",
5585 basetype);
5586 }
5587 }
5588
5589 /* If the class has no user-declared constructor, but does have
5590 non-static const or reference data members that can never be
5591 initialized, issue a warning. */
5592 if (warn_uninitialized
5593 /* Classes with user-declared constructors are presumed to
5594 initialize these members. */
5595 && !TYPE_HAS_USER_CONSTRUCTOR (t)
5596 /* Aggregates can be initialized with brace-enclosed
5597 initializers. */
5598 && CLASSTYPE_NON_AGGREGATE (t))
5599 {
5600 tree field;
5601
5602 for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
5603 {
5604 tree type;
5605
5606 if (TREE_CODE (field) != FIELD_DECL
5607 || DECL_INITIAL (field) != NULL_TREE)
5608 continue;
5609
5610 type = TREE_TYPE (field);
5611 if (TREE_CODE (type) == REFERENCE_TYPE)
5612 warning_at (DECL_SOURCE_LOCATION (field),
5613 OPT_Wuninitialized, "non-static reference %q#D "
5614 "in class without a constructor", field);
5615 else if (CP_TYPE_CONST_P (type)
5616 && (!CLASS_TYPE_P (type)
5617 || !TYPE_HAS_DEFAULT_CONSTRUCTOR (type)))
5618 warning_at (DECL_SOURCE_LOCATION (field),
5619 OPT_Wuninitialized, "non-static const member %q#D "
5620 "in class without a constructor", field);
5621 }
5622 }
5623
5624 /* Synthesize any needed methods. */
5625 add_implicitly_declared_members (t, &access_decls,
5626 cant_have_const_ctor,
5627 no_const_asn_ref);
5628
5629 /* Check defaulted declarations here so we have cant_have_const_ctor
5630 and don't need to worry about clones. */
5631 for (fn = TYPE_FIELDS (t); fn; fn = DECL_CHAIN (fn))
5632 if (DECL_DECLARES_FUNCTION_P (fn)
5633 && !DECL_ARTIFICIAL (fn)
5634 && DECL_DEFAULTED_IN_CLASS_P (fn))
5635 {
5636 int copy = copy_fn_p (fn);
5637 if (copy > 0)
5638 {
5639 bool imp_const_p
5640 = (DECL_CONSTRUCTOR_P (fn) ? !cant_have_const_ctor
5641 : !no_const_asn_ref);
5642 bool fn_const_p = (copy == 2);
5643
5644 if (fn_const_p && !imp_const_p)
5645 /* If the function is defaulted outside the class, we just
5646 give the synthesis error. */
5647 error ("%q+D declared to take const reference, but implicit "
5648 "declaration would take non-const", fn);
5649 }
5650 defaulted_late_check (fn);
5651 }
5652
5653 if (LAMBDA_TYPE_P (t))
5654 {
5655 /* "This class type is not an aggregate." */
5656 CLASSTYPE_NON_AGGREGATE (t) = 1;
5657 }
5658
5659 /* Compute the 'literal type' property before we
5660 do anything with non-static member functions. */
5661 finalize_literal_type_property (t);
5662
5663 /* Create the in-charge and not-in-charge variants of constructors
5664 and destructors. */
5665 clone_constructors_and_destructors (t);
5666
5667 /* Process the using-declarations. */
5668 for (; access_decls; access_decls = TREE_CHAIN (access_decls))
5669 handle_using_decl (TREE_VALUE (access_decls), t);
5670
5671 /* Figure out whether or not we will need a cookie when dynamically
5672 allocating an array of this type. */
5673 LANG_TYPE_CLASS_CHECK (t)->vec_new_uses_cookie
5674 = type_requires_array_cookie (t);
5675 }
5676
5677 /* If T needs a pointer to its virtual function table, set TYPE_VFIELD
5678 accordingly. If a new vfield was created (because T doesn't have a
5679 primary base class), then the newly created field is returned. It
5680 is not added to the TYPE_FIELDS list; it is the caller's
5681 responsibility to do that. Accumulate declared virtual functions
5682 on VIRTUALS_P. */
5683
5684 static tree
5685 create_vtable_ptr (tree t, tree* virtuals_p)
5686 {
5687 tree fn;
5688
5689 /* Collect the virtual functions declared in T. */
5690 for (fn = TYPE_FIELDS (t); fn; fn = DECL_CHAIN (fn))
5691 if (TREE_CODE (fn) == FUNCTION_DECL
5692 && DECL_VINDEX (fn) && !DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (fn)
5693 && TREE_CODE (DECL_VINDEX (fn)) != INTEGER_CST)
5694 {
5695 tree new_virtual = make_node (TREE_LIST);
5696
5697 BV_FN (new_virtual) = fn;
5698 BV_DELTA (new_virtual) = integer_zero_node;
5699 BV_VCALL_INDEX (new_virtual) = NULL_TREE;
5700
5701 TREE_CHAIN (new_virtual) = *virtuals_p;
5702 *virtuals_p = new_virtual;
5703 }
5704
5705 /* If we couldn't find an appropriate base class, create a new field
5706 here. Even if there weren't any new virtual functions, we might need a
5707 new virtual function table if we're supposed to include vptrs in
5708 all classes that need them. */
5709 if (!TYPE_VFIELD (t) && (*virtuals_p || TYPE_CONTAINS_VPTR_P (t)))
5710 {
5711 /* We build this decl with vtbl_ptr_type_node, which is a
5712 `vtable_entry_type*'. It might seem more precise to use
5713 `vtable_entry_type (*)[N]' where N is the number of virtual
5714 functions. However, that would require the vtable pointer in
5715 base classes to have a different type than the vtable pointer
5716 in derived classes. We could make that happen, but that
5717 still wouldn't solve all the problems. In particular, the
5718 type-based alias analysis code would decide that assignments
5719 to the base class vtable pointer can't alias assignments to
5720 the derived class vtable pointer, since they have different
5721 types. Thus, in a derived class destructor, where the base
5722 class constructor was inlined, we could generate bad code for
5723 setting up the vtable pointer.
5724
5725 Therefore, we use one type for all vtable pointers. We still
5726 use a type-correct type; it's just doesn't indicate the array
5727 bounds. That's better than using `void*' or some such; it's
5728 cleaner, and it let's the alias analysis code know that these
5729 stores cannot alias stores to void*! */
5730 tree field;
5731
5732 field = build_decl (input_location,
5733 FIELD_DECL, get_vfield_name (t), vtbl_ptr_type_node);
5734 DECL_VIRTUAL_P (field) = 1;
5735 DECL_ARTIFICIAL (field) = 1;
5736 DECL_FIELD_CONTEXT (field) = t;
5737 DECL_FCONTEXT (field) = t;
5738 if (TYPE_PACKED (t))
5739 DECL_PACKED (field) = 1;
5740
5741 TYPE_VFIELD (t) = field;
5742
5743 /* This class is non-empty. */
5744 CLASSTYPE_EMPTY_P (t) = 0;
5745
5746 return field;
5747 }
5748
5749 return NULL_TREE;
5750 }
5751
5752 /* Add OFFSET to all base types of BINFO which is a base in the
5753 hierarchy dominated by T.
5754
5755 OFFSET, which is a type offset, is number of bytes. */
5756
5757 static void
5758 propagate_binfo_offsets (tree binfo, tree offset)
5759 {
5760 int i;
5761 tree primary_binfo;
5762 tree base_binfo;
5763
5764 /* Update BINFO's offset. */
5765 BINFO_OFFSET (binfo)
5766 = fold_convert (sizetype,
5767 size_binop (PLUS_EXPR,
5768 fold_convert (ssizetype, BINFO_OFFSET (binfo)),
5769 offset));
5770
5771 /* Find the primary base class. */
5772 primary_binfo = get_primary_binfo (binfo);
5773
5774 if (primary_binfo && BINFO_INHERITANCE_CHAIN (primary_binfo) == binfo)
5775 propagate_binfo_offsets (primary_binfo, offset);
5776
5777 /* Scan all of the bases, pushing the BINFO_OFFSET adjust
5778 downwards. */
5779 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
5780 {
5781 /* Don't do the primary base twice. */
5782 if (base_binfo == primary_binfo)
5783 continue;
5784
5785 if (BINFO_VIRTUAL_P (base_binfo))
5786 continue;
5787
5788 propagate_binfo_offsets (base_binfo, offset);
5789 }
5790 }
5791
5792 /* Set BINFO_OFFSET for all of the virtual bases for RLI->T. Update
5793 TYPE_ALIGN and TYPE_SIZE for T. OFFSETS gives the location of
5794 empty subobjects of T. */
5795
5796 static void
5797 layout_virtual_bases (record_layout_info rli, splay_tree offsets)
5798 {
5799 tree vbase;
5800 tree t = rli->t;
5801 tree *next_field;
5802
5803 if (BINFO_N_BASE_BINFOS (TYPE_BINFO (t)) == 0)
5804 return;
5805
5806 /* Find the last field. The artificial fields created for virtual
5807 bases will go after the last extant field to date. */
5808 next_field = &TYPE_FIELDS (t);
5809 while (*next_field)
5810 next_field = &DECL_CHAIN (*next_field);
5811
5812 /* Go through the virtual bases, allocating space for each virtual
5813 base that is not already a primary base class. These are
5814 allocated in inheritance graph order. */
5815 for (vbase = TYPE_BINFO (t); vbase; vbase = TREE_CHAIN (vbase))
5816 {
5817 if (!BINFO_VIRTUAL_P (vbase))
5818 continue;
5819
5820 if (!BINFO_PRIMARY_P (vbase))
5821 {
5822 /* This virtual base is not a primary base of any class in the
5823 hierarchy, so we have to add space for it. */
5824 next_field = build_base_field (rli, vbase,
5825 offsets, next_field);
5826 }
5827 }
5828 }
5829
5830 /* Returns the offset of the byte just past the end of the base class
5831 BINFO. */
5832
5833 static tree
5834 end_of_base (tree binfo)
5835 {
5836 tree size;
5837
5838 if (!CLASSTYPE_AS_BASE (BINFO_TYPE (binfo)))
5839 size = TYPE_SIZE_UNIT (char_type_node);
5840 else if (is_empty_class (BINFO_TYPE (binfo)))
5841 /* An empty class has zero CLASSTYPE_SIZE_UNIT, but we need to
5842 allocate some space for it. It cannot have virtual bases, so
5843 TYPE_SIZE_UNIT is fine. */
5844 size = TYPE_SIZE_UNIT (BINFO_TYPE (binfo));
5845 else
5846 size = CLASSTYPE_SIZE_UNIT (BINFO_TYPE (binfo));
5847
5848 return size_binop (PLUS_EXPR, BINFO_OFFSET (binfo), size);
5849 }
5850
5851 /* Returns the offset of the byte just past the end of the base class
5852 with the highest offset in T. If INCLUDE_VIRTUALS_P is zero, then
5853 only non-virtual bases are included. */
5854
5855 static tree
5856 end_of_class (tree t, int include_virtuals_p)
5857 {
5858 tree result = size_zero_node;
5859 vec<tree, va_gc> *vbases;
5860 tree binfo;
5861 tree base_binfo;
5862 tree offset;
5863 int i;
5864
5865 for (binfo = TYPE_BINFO (t), i = 0;
5866 BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
5867 {
5868 if (!include_virtuals_p
5869 && BINFO_VIRTUAL_P (base_binfo)
5870 && (!BINFO_PRIMARY_P (base_binfo)
5871 || BINFO_INHERITANCE_CHAIN (base_binfo) != TYPE_BINFO (t)))
5872 continue;
5873
5874 offset = end_of_base (base_binfo);
5875 if (tree_int_cst_lt (result, offset))
5876 result = offset;
5877 }
5878
5879 if (include_virtuals_p)
5880 for (vbases = CLASSTYPE_VBASECLASSES (t), i = 0;
5881 vec_safe_iterate (vbases, i, &base_binfo); i++)
5882 {
5883 offset = end_of_base (base_binfo);
5884 if (tree_int_cst_lt (result, offset))
5885 result = offset;
5886 }
5887
5888 return result;
5889 }
5890
5891 /* Warn about bases of T that are inaccessible because they are
5892 ambiguous. For example:
5893
5894 struct S {};
5895 struct T : public S {};
5896 struct U : public S, public T {};
5897
5898 Here, `(S*) new U' is not allowed because there are two `S'
5899 subobjects of U. */
5900
5901 static void
5902 warn_about_ambiguous_bases (tree t)
5903 {
5904 int i;
5905 vec<tree, va_gc> *vbases;
5906 tree basetype;
5907 tree binfo;
5908 tree base_binfo;
5909
5910 /* If there are no repeated bases, nothing can be ambiguous. */
5911 if (!CLASSTYPE_REPEATED_BASE_P (t))
5912 return;
5913
5914 /* Check direct bases. */
5915 for (binfo = TYPE_BINFO (t), i = 0;
5916 BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
5917 {
5918 basetype = BINFO_TYPE (base_binfo);
5919
5920 if (!uniquely_derived_from_p (basetype, t))
5921 warning (0, "direct base %qT inaccessible in %qT due to ambiguity",
5922 basetype, t);
5923 }
5924
5925 /* Check for ambiguous virtual bases. */
5926 if (extra_warnings)
5927 for (vbases = CLASSTYPE_VBASECLASSES (t), i = 0;
5928 vec_safe_iterate (vbases, i, &binfo); i++)
5929 {
5930 basetype = BINFO_TYPE (binfo);
5931
5932 if (!uniquely_derived_from_p (basetype, t))
5933 warning (OPT_Wextra, "virtual base %qT inaccessible in %qT due "
5934 "to ambiguity", basetype, t);
5935 }
5936 }
5937
5938 /* Compare two INTEGER_CSTs K1 and K2. */
5939
5940 static int
5941 splay_tree_compare_integer_csts (splay_tree_key k1, splay_tree_key k2)
5942 {
5943 return tree_int_cst_compare ((tree) k1, (tree) k2);
5944 }
5945
5946 /* Increase the size indicated in RLI to account for empty classes
5947 that are "off the end" of the class. */
5948
5949 static void
5950 include_empty_classes (record_layout_info rli)
5951 {
5952 tree eoc;
5953 tree rli_size;
5954
5955 /* It might be the case that we grew the class to allocate a
5956 zero-sized base class. That won't be reflected in RLI, yet,
5957 because we are willing to overlay multiple bases at the same
5958 offset. However, now we need to make sure that RLI is big enough
5959 to reflect the entire class. */
5960 eoc = end_of_class (rli->t, CLASSTYPE_AS_BASE (rli->t) != NULL_TREE);
5961 rli_size = rli_size_unit_so_far (rli);
5962 if (TREE_CODE (rli_size) == INTEGER_CST
5963 && tree_int_cst_lt (rli_size, eoc))
5964 {
5965 /* The size should have been rounded to a whole byte. */
5966 gcc_assert (tree_int_cst_equal
5967 (rli->bitpos, round_down (rli->bitpos, BITS_PER_UNIT)));
5968 rli->bitpos
5969 = size_binop (PLUS_EXPR,
5970 rli->bitpos,
5971 size_binop (MULT_EXPR,
5972 fold_convert (bitsizetype,
5973 size_binop (MINUS_EXPR,
5974 eoc, rli_size)),
5975 bitsize_int (BITS_PER_UNIT)));
5976 normalize_rli (rli);
5977 }
5978 }
5979
5980 /* Calculate the TYPE_SIZE, TYPE_ALIGN, etc for T. Calculate
5981 BINFO_OFFSETs for all of the base-classes. Position the vtable
5982 pointer. Accumulate declared virtual functions on VIRTUALS_P. */
5983
5984 static void
5985 layout_class_type (tree t, tree *virtuals_p)
5986 {
5987 tree non_static_data_members;
5988 tree field;
5989 tree vptr;
5990 record_layout_info rli;
5991 /* Maps offsets (represented as INTEGER_CSTs) to a TREE_LIST of
5992 types that appear at that offset. */
5993 splay_tree empty_base_offsets;
5994 /* True if the last field laid out was a bit-field. */
5995 bool last_field_was_bitfield = false;
5996 /* The location at which the next field should be inserted. */
5997 tree *next_field;
5998
5999 /* Keep track of the first non-static data member. */
6000 non_static_data_members = TYPE_FIELDS (t);
6001
6002 /* Start laying out the record. */
6003 rli = start_record_layout (t);
6004
6005 /* Mark all the primary bases in the hierarchy. */
6006 determine_primary_bases (t);
6007
6008 /* Create a pointer to our virtual function table. */
6009 vptr = create_vtable_ptr (t, virtuals_p);
6010
6011 /* The vptr is always the first thing in the class. */
6012 if (vptr)
6013 {
6014 DECL_CHAIN (vptr) = TYPE_FIELDS (t);
6015 TYPE_FIELDS (t) = vptr;
6016 next_field = &DECL_CHAIN (vptr);
6017 place_field (rli, vptr);
6018 }
6019 else
6020 next_field = &TYPE_FIELDS (t);
6021
6022 /* Build FIELD_DECLs for all of the non-virtual base-types. */
6023 empty_base_offsets = splay_tree_new (splay_tree_compare_integer_csts,
6024 NULL, NULL);
6025 build_base_fields (rli, empty_base_offsets, next_field);
6026
6027 /* Layout the non-static data members. */
6028 for (field = non_static_data_members; field; field = DECL_CHAIN (field))
6029 {
6030 tree type;
6031 tree padding;
6032
6033 /* We still pass things that aren't non-static data members to
6034 the back end, in case it wants to do something with them. */
6035 if (TREE_CODE (field) != FIELD_DECL)
6036 {
6037 place_field (rli, field);
6038 /* If the static data member has incomplete type, keep track
6039 of it so that it can be completed later. (The handling
6040 of pending statics in finish_record_layout is
6041 insufficient; consider:
6042
6043 struct S1;
6044 struct S2 { static S1 s1; };
6045
6046 At this point, finish_record_layout will be called, but
6047 S1 is still incomplete.) */
6048 if (VAR_P (field))
6049 {
6050 maybe_register_incomplete_var (field);
6051 /* The visibility of static data members is determined
6052 at their point of declaration, not their point of
6053 definition. */
6054 determine_visibility (field);
6055 }
6056 continue;
6057 }
6058
6059 type = TREE_TYPE (field);
6060 if (type == error_mark_node)
6061 continue;
6062
6063 padding = NULL_TREE;
6064
6065 /* If this field is a bit-field whose width is greater than its
6066 type, then there are some special rules for allocating
6067 it. */
6068 if (DECL_C_BIT_FIELD (field)
6069 && tree_int_cst_lt (TYPE_SIZE (type), DECL_SIZE (field)))
6070 {
6071 bool was_unnamed_p = false;
6072 /* We must allocate the bits as if suitably aligned for the
6073 longest integer type that fits in this many bits. Then,
6074 we are supposed to use the left over bits as additional
6075 padding. */
6076
6077 /* Do not pick a type bigger than MAX_FIXED_MODE_SIZE. */
6078 tree limit = size_int (MAX_FIXED_MODE_SIZE);
6079 if (tree_int_cst_lt (DECL_SIZE (field), limit))
6080 limit = DECL_SIZE (field);
6081
6082 tree integer_type = integer_types[itk_char];
6083 for (unsigned itk = itk_char; itk != itk_none; itk++)
6084 if (tree next = integer_types[itk])
6085 {
6086 if (tree_int_cst_lt (limit, TYPE_SIZE (next)))
6087 /* Too big, so our current guess is what we want. */
6088 break;
6089 /* Not bigger than limit, ok */
6090 integer_type = next;
6091 }
6092
6093 /* Figure out how much additional padding is required. */
6094 if (TREE_CODE (t) == UNION_TYPE)
6095 /* In a union, the padding field must have the full width
6096 of the bit-field; all fields start at offset zero. */
6097 padding = DECL_SIZE (field);
6098 else
6099 padding = size_binop (MINUS_EXPR, DECL_SIZE (field),
6100 TYPE_SIZE (integer_type));
6101
6102 if (integer_zerop (padding))
6103 padding = NULL_TREE;
6104
6105 /* An unnamed bitfield does not normally affect the
6106 alignment of the containing class on a target where
6107 PCC_BITFIELD_TYPE_MATTERS. But, the C++ ABI does not
6108 make any exceptions for unnamed bitfields when the
6109 bitfields are longer than their types. Therefore, we
6110 temporarily give the field a name. */
6111 if (PCC_BITFIELD_TYPE_MATTERS && !DECL_NAME (field))
6112 {
6113 was_unnamed_p = true;
6114 DECL_NAME (field) = make_anon_name ();
6115 }
6116
6117 DECL_SIZE (field) = TYPE_SIZE (integer_type);
6118 SET_DECL_ALIGN (field, TYPE_ALIGN (integer_type));
6119 DECL_USER_ALIGN (field) = TYPE_USER_ALIGN (integer_type);
6120 layout_nonempty_base_or_field (rli, field, NULL_TREE,
6121 empty_base_offsets);
6122 if (was_unnamed_p)
6123 DECL_NAME (field) = NULL_TREE;
6124 /* Now that layout has been performed, set the size of the
6125 field to the size of its declared type; the rest of the
6126 field is effectively invisible. */
6127 DECL_SIZE (field) = TYPE_SIZE (type);
6128 /* We must also reset the DECL_MODE of the field. */
6129 SET_DECL_MODE (field, TYPE_MODE (type));
6130 }
6131 else
6132 layout_nonempty_base_or_field (rli, field, NULL_TREE,
6133 empty_base_offsets);
6134
6135 /* Remember the location of any empty classes in FIELD. */
6136 record_subobject_offsets (TREE_TYPE (field),
6137 byte_position(field),
6138 empty_base_offsets,
6139 /*is_data_member=*/true);
6140
6141 /* If a bit-field does not immediately follow another bit-field,
6142 and yet it starts in the middle of a byte, we have failed to
6143 comply with the ABI. */
6144 if (warn_abi
6145 && DECL_C_BIT_FIELD (field)
6146 /* The TREE_NO_WARNING flag gets set by Objective-C when
6147 laying out an Objective-C class. The ObjC ABI differs
6148 from the C++ ABI, and so we do not want a warning
6149 here. */
6150 && !TREE_NO_WARNING (field)
6151 && !last_field_was_bitfield
6152 && !integer_zerop (size_binop (TRUNC_MOD_EXPR,
6153 DECL_FIELD_BIT_OFFSET (field),
6154 bitsize_unit_node)))
6155 warning_at (DECL_SOURCE_LOCATION (field), OPT_Wabi,
6156 "offset of %qD is not ABI-compliant and may "
6157 "change in a future version of GCC", field);
6158
6159 /* The middle end uses the type of expressions to determine the
6160 possible range of expression values. In order to optimize
6161 "x.i > 7" to "false" for a 2-bit bitfield "i", the middle end
6162 must be made aware of the width of "i", via its type.
6163
6164 Because C++ does not have integer types of arbitrary width,
6165 we must (for the purposes of the front end) convert from the
6166 type assigned here to the declared type of the bitfield
6167 whenever a bitfield expression is used as an rvalue.
6168 Similarly, when assigning a value to a bitfield, the value
6169 must be converted to the type given the bitfield here. */
6170 if (DECL_C_BIT_FIELD (field))
6171 {
6172 unsigned HOST_WIDE_INT width;
6173 tree ftype = TREE_TYPE (field);
6174 width = tree_to_uhwi (DECL_SIZE (field));
6175 if (width != TYPE_PRECISION (ftype))
6176 {
6177 TREE_TYPE (field)
6178 = c_build_bitfield_integer_type (width,
6179 TYPE_UNSIGNED (ftype));
6180 TREE_TYPE (field)
6181 = cp_build_qualified_type (TREE_TYPE (field),
6182 cp_type_quals (ftype));
6183 }
6184 }
6185
6186 /* If we needed additional padding after this field, add it
6187 now. */
6188 if (padding)
6189 {
6190 tree padding_field;
6191
6192 padding_field = build_decl (input_location,
6193 FIELD_DECL,
6194 NULL_TREE,
6195 char_type_node);
6196 DECL_BIT_FIELD (padding_field) = 1;
6197 DECL_SIZE (padding_field) = padding;
6198 DECL_CONTEXT (padding_field) = t;
6199 DECL_ARTIFICIAL (padding_field) = 1;
6200 DECL_IGNORED_P (padding_field) = 1;
6201 DECL_PADDING_P (padding_field) = 1;
6202 layout_nonempty_base_or_field (rli, padding_field,
6203 NULL_TREE,
6204 empty_base_offsets);
6205 }
6206
6207 last_field_was_bitfield = DECL_C_BIT_FIELD (field);
6208 }
6209
6210 if (!integer_zerop (rli->bitpos))
6211 {
6212 /* Make sure that we are on a byte boundary so that the size of
6213 the class without virtual bases will always be a round number
6214 of bytes. */
6215 rli->bitpos = round_up_loc (input_location, rli->bitpos, BITS_PER_UNIT);
6216 normalize_rli (rli);
6217 }
6218
6219 /* Delete all zero-width bit-fields from the list of fields. Now
6220 that the type is laid out they are no longer important. */
6221 remove_zero_width_bit_fields (t);
6222
6223 if (CLASSTYPE_NON_LAYOUT_POD_P (t) || CLASSTYPE_EMPTY_P (t))
6224 {
6225 /* T needs a different layout as a base (eliding virtual bases
6226 or whatever). Create that version. */
6227 tree base_t = make_node (TREE_CODE (t));
6228
6229 /* If the ABI version is not at least two, and the last
6230 field was a bit-field, RLI may not be on a byte
6231 boundary. In particular, rli_size_unit_so_far might
6232 indicate the last complete byte, while rli_size_so_far
6233 indicates the total number of bits used. Therefore,
6234 rli_size_so_far, rather than rli_size_unit_so_far, is
6235 used to compute TYPE_SIZE_UNIT. */
6236 tree eoc = end_of_class (t, /*include_virtuals_p=*/0);
6237 TYPE_SIZE_UNIT (base_t)
6238 = size_binop (MAX_EXPR,
6239 fold_convert (sizetype,
6240 size_binop (CEIL_DIV_EXPR,
6241 rli_size_so_far (rli),
6242 bitsize_int (BITS_PER_UNIT))),
6243 eoc);
6244 TYPE_SIZE (base_t)
6245 = size_binop (MAX_EXPR,
6246 rli_size_so_far (rli),
6247 size_binop (MULT_EXPR,
6248 fold_convert (bitsizetype, eoc),
6249 bitsize_int (BITS_PER_UNIT)));
6250 SET_TYPE_ALIGN (base_t, rli->record_align);
6251 TYPE_USER_ALIGN (base_t) = TYPE_USER_ALIGN (t);
6252
6253 /* Copy the non-static data members of T. This will include its
6254 direct non-virtual bases & vtable. */
6255 next_field = &TYPE_FIELDS (base_t);
6256 for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
6257 if (TREE_CODE (field) == FIELD_DECL)
6258 {
6259 *next_field = copy_node (field);
6260 DECL_CONTEXT (*next_field) = base_t;
6261 next_field = &DECL_CHAIN (*next_field);
6262 }
6263 *next_field = NULL_TREE;
6264
6265 /* We use the base type for trivial assignments, and hence it
6266 needs a mode. */
6267 compute_record_mode (base_t);
6268
6269 TYPE_CONTEXT (base_t) = t;
6270
6271 /* Record the base version of the type. */
6272 CLASSTYPE_AS_BASE (t) = base_t;
6273 }
6274 else
6275 CLASSTYPE_AS_BASE (t) = t;
6276
6277 /* Every empty class contains an empty class. */
6278 if (CLASSTYPE_EMPTY_P (t))
6279 CLASSTYPE_CONTAINS_EMPTY_CLASS_P (t) = 1;
6280
6281 /* Set the TYPE_DECL for this type to contain the right
6282 value for DECL_OFFSET, so that we can use it as part
6283 of a COMPONENT_REF for multiple inheritance. */
6284 layout_decl (TYPE_MAIN_DECL (t), 0);
6285
6286 /* Now fix up any virtual base class types that we left lying
6287 around. We must get these done before we try to lay out the
6288 virtual function table. As a side-effect, this will remove the
6289 base subobject fields. */
6290 layout_virtual_bases (rli, empty_base_offsets);
6291
6292 /* Make sure that empty classes are reflected in RLI at this
6293 point. */
6294 include_empty_classes (rli);
6295
6296 /* Make sure not to create any structures with zero size. */
6297 if (integer_zerop (rli_size_unit_so_far (rli)) && CLASSTYPE_EMPTY_P (t))
6298 place_field (rli,
6299 build_decl (input_location,
6300 FIELD_DECL, NULL_TREE, char_type_node));
6301
6302 /* If this is a non-POD, declaring it packed makes a difference to how it
6303 can be used as a field; don't let finalize_record_size undo it. */
6304 if (TYPE_PACKED (t) && !layout_pod_type_p (t))
6305 rli->packed_maybe_necessary = true;
6306
6307 /* Let the back end lay out the type. */
6308 finish_record_layout (rli, /*free_p=*/true);
6309
6310 if (TYPE_SIZE_UNIT (t)
6311 && TREE_CODE (TYPE_SIZE_UNIT (t)) == INTEGER_CST
6312 && !TREE_OVERFLOW (TYPE_SIZE_UNIT (t))
6313 && !valid_constant_size_p (TYPE_SIZE_UNIT (t)))
6314 error ("size of type %qT is too large (%qE bytes)", t, TYPE_SIZE_UNIT (t));
6315
6316 /* Warn about bases that can't be talked about due to ambiguity. */
6317 warn_about_ambiguous_bases (t);
6318
6319 /* Now that we're done with layout, give the base fields the real types. */
6320 for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
6321 if (DECL_ARTIFICIAL (field) && IS_FAKE_BASE_TYPE (TREE_TYPE (field)))
6322 TREE_TYPE (field) = TYPE_CONTEXT (TREE_TYPE (field));
6323
6324 /* Clean up. */
6325 splay_tree_delete (empty_base_offsets);
6326
6327 if (CLASSTYPE_EMPTY_P (t)
6328 && tree_int_cst_lt (sizeof_biggest_empty_class,
6329 TYPE_SIZE_UNIT (t)))
6330 sizeof_biggest_empty_class = TYPE_SIZE_UNIT (t);
6331 }
6332
6333 /* Determine the "key method" for the class type indicated by TYPE,
6334 and set CLASSTYPE_KEY_METHOD accordingly. */
6335
6336 void
6337 determine_key_method (tree type)
6338 {
6339 tree method;
6340
6341 if (processing_template_decl
6342 || CLASSTYPE_TEMPLATE_INSTANTIATION (type)
6343 || CLASSTYPE_INTERFACE_KNOWN (type))
6344 return;
6345
6346 /* The key method is the first non-pure virtual function that is not
6347 inline at the point of class definition. On some targets the
6348 key function may not be inline; those targets should not call
6349 this function until the end of the translation unit. */
6350 for (method = TYPE_FIELDS (type); method; method = DECL_CHAIN (method))
6351 if (TREE_CODE (method) == FUNCTION_DECL
6352 && DECL_VINDEX (method) != NULL_TREE
6353 && ! DECL_DECLARED_INLINE_P (method)
6354 && ! DECL_PURE_VIRTUAL_P (method))
6355 {
6356 CLASSTYPE_KEY_METHOD (type) = method;
6357 break;
6358 }
6359
6360 return;
6361 }
6362
6363 /* Helper of find_flexarrays. Return true when FLD refers to a non-static
6364 class data member of non-zero size, otherwise false. */
6365
6366 static inline bool
6367 field_nonempty_p (const_tree fld)
6368 {
6369 if (TREE_CODE (fld) == ERROR_MARK)
6370 return false;
6371
6372 tree type = TREE_TYPE (fld);
6373 if (TREE_CODE (fld) == FIELD_DECL
6374 && TREE_CODE (type) != ERROR_MARK
6375 && (DECL_NAME (fld) || RECORD_OR_UNION_TYPE_P (type)))
6376 {
6377 return TYPE_SIZE (type)
6378 && (TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST
6379 || !tree_int_cst_equal (size_zero_node, TYPE_SIZE (type)));
6380 }
6381
6382 return false;
6383 }
6384
6385 /* Used by find_flexarrays and related functions. */
6386
6387 struct flexmems_t
6388 {
6389 /* The first flexible array member or non-zero array member found
6390 in the order of layout. */
6391 tree array;
6392 /* First non-static non-empty data member in the class or its bases. */
6393 tree first;
6394 /* The first non-static non-empty data member following either
6395 the flexible array member, if found, or the zero-length array member
6396 otherwise. AFTER[1] refers to the first such data member of a union
6397 of which the struct containing the flexible array member or zero-length
6398 array is a member, or NULL when no such union exists. This element is
6399 only used during searching, not for diagnosing problems. AFTER[0]
6400 refers to the first such data member that is not a member of such
6401 a union. */
6402 tree after[2];
6403
6404 /* Refers to a struct (not union) in which the struct of which the flexible
6405 array is member is defined. Used to diagnose strictly (according to C)
6406 invalid uses of the latter structs. */
6407 tree enclosing;
6408 };
6409
6410 /* Find either the first flexible array member or the first zero-length
6411 array, in that order of preference, among members of class T (but not
6412 its base classes), and set members of FMEM accordingly.
6413 BASE_P is true if T is a base class of another class.
6414 PUN is set to the outermost union in which the flexible array member
6415 (or zero-length array) is defined if one such union exists, otherwise
6416 to NULL.
6417 Similarly, PSTR is set to a data member of the outermost struct of
6418 which the flexible array is a member if one such struct exists,
6419 otherwise to NULL. */
6420
6421 static void
6422 find_flexarrays (tree t, flexmems_t *fmem, bool base_p,
6423 tree pun /* = NULL_TREE */,
6424 tree pstr /* = NULL_TREE */)
6425 {
6426 /* Set the "pointer" to the outermost enclosing union if not set
6427 yet and maintain it for the remainder of the recursion. */
6428 if (!pun && TREE_CODE (t) == UNION_TYPE)
6429 pun = t;
6430
6431 for (tree fld = TYPE_FIELDS (t); fld; fld = DECL_CHAIN (fld))
6432 {
6433 if (fld == error_mark_node)
6434 return;
6435
6436 /* Is FLD a typedef for an anonymous struct? */
6437
6438 /* FIXME: Note that typedefs (as well as arrays) need to be fully
6439 handled elsewhere so that errors like the following are detected
6440 as well:
6441 typedef struct { int i, a[], j; } S; // bug c++/72753
6442 S s [2]; // bug c++/68489
6443 */
6444 if (TREE_CODE (fld) == TYPE_DECL
6445 && DECL_IMPLICIT_TYPEDEF_P (fld)
6446 && CLASS_TYPE_P (TREE_TYPE (fld))
6447 && anon_aggrname_p (DECL_NAME (fld)))
6448 {
6449 /* Check the nested unnamed type referenced via a typedef
6450 independently of FMEM (since it's not a data member of
6451 the enclosing class). */
6452 check_flexarrays (TREE_TYPE (fld));
6453 continue;
6454 }
6455
6456 /* Skip anything that's GCC-generated or not a (non-static) data
6457 member. */
6458 if (DECL_ARTIFICIAL (fld) || TREE_CODE (fld) != FIELD_DECL)
6459 continue;
6460
6461 /* Type of the member. */
6462 tree fldtype = TREE_TYPE (fld);
6463 if (fldtype == error_mark_node)
6464 return;
6465
6466 /* Determine the type of the array element or object referenced
6467 by the member so that it can be checked for flexible array
6468 members if it hasn't been yet. */
6469 tree eltype = fldtype;
6470 while (TREE_CODE (eltype) == ARRAY_TYPE
6471 || TREE_CODE (eltype) == POINTER_TYPE
6472 || TREE_CODE (eltype) == REFERENCE_TYPE)
6473 eltype = TREE_TYPE (eltype);
6474
6475 if (RECORD_OR_UNION_TYPE_P (eltype))
6476 {
6477 if (fmem->array && !fmem->after[bool (pun)])
6478 {
6479 /* Once the member after the flexible array has been found
6480 we're done. */
6481 fmem->after[bool (pun)] = fld;
6482 break;
6483 }
6484
6485 if (eltype == fldtype || TYPE_UNNAMED_P (eltype))
6486 {
6487 /* Descend into the non-static member struct or union and try
6488 to find a flexible array member or zero-length array among
6489 its members. This is only necessary for anonymous types
6490 and types in whose context the current type T has not been
6491 defined (the latter must not be checked again because they
6492 are already in the process of being checked by one of the
6493 recursive calls). */
6494
6495 tree first = fmem->first;
6496 tree array = fmem->array;
6497
6498 /* If this member isn't anonymous and a prior non-flexible array
6499 member has been seen in one of the enclosing structs, clear
6500 the FIRST member since it doesn't contribute to the flexible
6501 array struct's members. */
6502 if (first && !array && !ANON_AGGR_TYPE_P (eltype))
6503 fmem->first = NULL_TREE;
6504
6505 find_flexarrays (eltype, fmem, false, pun,
6506 !pstr && TREE_CODE (t) == RECORD_TYPE ? fld : pstr);
6507
6508 if (fmem->array != array)
6509 continue;
6510
6511 if (first && !array && !ANON_AGGR_TYPE_P (eltype))
6512 {
6513 /* Restore the FIRST member reset above if no flexible
6514 array member has been found in this member's struct. */
6515 fmem->first = first;
6516 }
6517
6518 /* If the member struct contains the first flexible array
6519 member, or if this member is a base class, continue to
6520 the next member and avoid setting the FMEM->NEXT pointer
6521 to point to it. */
6522 if (base_p)
6523 continue;
6524 }
6525 }
6526
6527 if (field_nonempty_p (fld))
6528 {
6529 /* Remember the first non-static data member. */
6530 if (!fmem->first)
6531 fmem->first = fld;
6532
6533 /* Remember the first non-static data member after the flexible
6534 array member, if one has been found, or the zero-length array
6535 if it has been found. */
6536 if (fmem->array && !fmem->after[bool (pun)])
6537 fmem->after[bool (pun)] = fld;
6538 }
6539
6540 /* Skip non-arrays. */
6541 if (TREE_CODE (fldtype) != ARRAY_TYPE)
6542 continue;
6543
6544 /* Determine the upper bound of the array if it has one. */
6545 if (TYPE_DOMAIN (fldtype))
6546 {
6547 if (fmem->array)
6548 {
6549 /* Make a record of the zero-length array if either one
6550 such field or a flexible array member has been seen to
6551 handle the pathological and unlikely case of multiple
6552 such members. */
6553 if (!fmem->after[bool (pun)])
6554 fmem->after[bool (pun)] = fld;
6555 }
6556 else if (integer_all_onesp (TYPE_MAX_VALUE (TYPE_DOMAIN (fldtype))))
6557 {
6558 /* Remember the first zero-length array unless a flexible array
6559 member has already been seen. */
6560 fmem->array = fld;
6561 fmem->enclosing = pstr;
6562 }
6563 }
6564 else
6565 {
6566 /* Flexible array members have no upper bound. */
6567 if (fmem->array)
6568 {
6569 /* Replace the zero-length array if it's been stored and
6570 reset the after pointer. */
6571 if (TYPE_DOMAIN (TREE_TYPE (fmem->array)))
6572 {
6573 fmem->after[bool (pun)] = NULL_TREE;
6574 fmem->array = fld;
6575 fmem->enclosing = pstr;
6576 }
6577 }
6578 else
6579 {
6580 fmem->array = fld;
6581 fmem->enclosing = pstr;
6582 }
6583 }
6584 }
6585 }
6586
6587 /* Diagnose a strictly (by the C standard) invalid use of a struct with
6588 a flexible array member (or the zero-length array extension). */
6589
6590 static void
6591 diagnose_invalid_flexarray (const flexmems_t *fmem)
6592 {
6593 if (fmem->array && fmem->enclosing
6594 && pedwarn (location_of (fmem->enclosing), OPT_Wpedantic,
6595 TYPE_DOMAIN (TREE_TYPE (fmem->array))
6596 ? G_("invalid use of %q#T with a zero-size array "
6597 "in %q#D")
6598 : G_("invalid use of %q#T with a flexible array member "
6599 "in %q#T"),
6600 DECL_CONTEXT (fmem->array),
6601 DECL_CONTEXT (fmem->enclosing)))
6602 inform (DECL_SOURCE_LOCATION (fmem->array),
6603 "array member %q#D declared here", fmem->array);
6604 }
6605
6606 /* Issue diagnostics for invalid flexible array members or zero-length
6607 arrays that are not the last elements of the containing class or its
6608 base classes or that are its sole members. */
6609
6610 static void
6611 diagnose_flexarrays (tree t, const flexmems_t *fmem)
6612 {
6613 if (!fmem->array)
6614 return;
6615
6616 if (fmem->first && !fmem->after[0])
6617 {
6618 diagnose_invalid_flexarray (fmem);
6619 return;
6620 }
6621
6622 /* Has a diagnostic been issued? */
6623 bool diagd = false;
6624
6625 const char *msg = 0;
6626
6627 if (TYPE_DOMAIN (TREE_TYPE (fmem->array)))
6628 {
6629 if (fmem->after[0])
6630 msg = G_("zero-size array member %qD not at end of %q#T");
6631 else if (!fmem->first)
6632 msg = G_("zero-size array member %qD in an otherwise empty %q#T");
6633
6634 if (msg)
6635 {
6636 location_t loc = DECL_SOURCE_LOCATION (fmem->array);
6637
6638 if (pedwarn (loc, OPT_Wpedantic, msg, fmem->array, t))
6639 {
6640 inform (location_of (t), "in the definition of %q#T", t);
6641 diagd = true;
6642 }
6643 }
6644 }
6645 else
6646 {
6647 if (fmem->after[0])
6648 msg = G_("flexible array member %qD not at end of %q#T");
6649 else if (!fmem->first)
6650 msg = G_("flexible array member %qD in an otherwise empty %q#T");
6651
6652 if (msg)
6653 {
6654 location_t loc = DECL_SOURCE_LOCATION (fmem->array);
6655 diagd = true;
6656
6657 error_at (loc, msg, fmem->array, t);
6658
6659 /* In the unlikely event that the member following the flexible
6660 array member is declared in a different class, or the member
6661 overlaps another member of a common union, point to it.
6662 Otherwise it should be obvious. */
6663 if (fmem->after[0]
6664 && ((DECL_CONTEXT (fmem->after[0])
6665 != DECL_CONTEXT (fmem->array))))
6666 {
6667 inform (DECL_SOURCE_LOCATION (fmem->after[0]),
6668 "next member %q#D declared here",
6669 fmem->after[0]);
6670 inform (location_of (t), "in the definition of %q#T", t);
6671 }
6672 }
6673 }
6674
6675 if (!diagd && fmem->array && fmem->enclosing)
6676 diagnose_invalid_flexarray (fmem);
6677 }
6678
6679
6680 /* Recursively check to make sure that any flexible array or zero-length
6681 array members of class T or its bases are valid (i.e., not the sole
6682 non-static data member of T and, if one exists, that it is the last
6683 non-static data member of T and its base classes. FMEM is expected
6684 to be initially null and is used internally by recursive calls to
6685 the function. Issue the appropriate diagnostics for the array member
6686 that fails the checks. */
6687
6688 static void
6689 check_flexarrays (tree t, flexmems_t *fmem /* = NULL */,
6690 bool base_p /* = false */)
6691 {
6692 /* Initialize the result of a search for flexible array and zero-length
6693 array members. Avoid doing any work if the most interesting FMEM data
6694 have already been populated. */
6695 flexmems_t flexmems = flexmems_t ();
6696 if (!fmem)
6697 fmem = &flexmems;
6698 else if (fmem->array && fmem->first && fmem->after[0])
6699 return;
6700
6701 tree fam = fmem->array;
6702
6703 /* Recursively check the primary base class first. */
6704 if (CLASSTYPE_HAS_PRIMARY_BASE_P (t))
6705 {
6706 tree basetype = BINFO_TYPE (CLASSTYPE_PRIMARY_BINFO (t));
6707 check_flexarrays (basetype, fmem, true);
6708 }
6709
6710 /* Recursively check the base classes. */
6711 int nbases = TYPE_BINFO (t) ? BINFO_N_BASE_BINFOS (TYPE_BINFO (t)) : 0;
6712 for (int i = 0; i < nbases; ++i)
6713 {
6714 tree base_binfo = BINFO_BASE_BINFO (TYPE_BINFO (t), i);
6715
6716 /* The primary base class was already checked above. */
6717 if (base_binfo == CLASSTYPE_PRIMARY_BINFO (t))
6718 continue;
6719
6720 /* Virtual base classes are at the end. */
6721 if (BINFO_VIRTUAL_P (base_binfo))
6722 continue;
6723
6724 /* Check the base class. */
6725 check_flexarrays (BINFO_TYPE (base_binfo), fmem, /*base_p=*/true);
6726 }
6727
6728 if (fmem == &flexmems)
6729 {
6730 /* Check virtual base classes only once per derived class.
6731 I.e., this check is not performed recursively for base
6732 classes. */
6733 int i;
6734 tree base_binfo;
6735 vec<tree, va_gc> *vbases;
6736 for (vbases = CLASSTYPE_VBASECLASSES (t), i = 0;
6737 vec_safe_iterate (vbases, i, &base_binfo); i++)
6738 {
6739 /* Check the virtual base class. */
6740 tree basetype = TREE_TYPE (base_binfo);
6741
6742 check_flexarrays (basetype, fmem, /*base_p=*/true);
6743 }
6744 }
6745
6746 /* Is the type unnamed (and therefore a member of it potentially
6747 an anonymous struct or union)? */
6748 bool maybe_anon_p = TYPE_UNNAMED_P (t);
6749
6750 /* Search the members of the current (possibly derived) class, skipping
6751 unnamed structs and unions since those could be anonymous. */
6752 if (fmem != &flexmems || !maybe_anon_p)
6753 find_flexarrays (t, fmem, base_p || fam != fmem->array);
6754
6755 if (fmem == &flexmems && !maybe_anon_p)
6756 {
6757 /* Issue diagnostics for invalid flexible and zero-length array
6758 members found in base classes or among the members of the current
6759 class. Ignore anonymous structs and unions whose members are
6760 considered to be members of the enclosing class and thus will
6761 be diagnosed when checking it. */
6762 diagnose_flexarrays (t, fmem);
6763 }
6764 }
6765
6766 /* Perform processing required when the definition of T (a class type)
6767 is complete. Diagnose invalid definitions of flexible array members
6768 and zero-size arrays. */
6769
6770 void
6771 finish_struct_1 (tree t)
6772 {
6773 tree x;
6774 /* A TREE_LIST. The TREE_VALUE of each node is a FUNCTION_DECL. */
6775 tree virtuals = NULL_TREE;
6776
6777 if (COMPLETE_TYPE_P (t))
6778 {
6779 gcc_assert (MAYBE_CLASS_TYPE_P (t));
6780 error ("redefinition of %q#T", t);
6781 popclass ();
6782 return;
6783 }
6784
6785 /* If this type was previously laid out as a forward reference,
6786 make sure we lay it out again. */
6787 TYPE_SIZE (t) = NULL_TREE;
6788 CLASSTYPE_PRIMARY_BINFO (t) = NULL_TREE;
6789
6790 /* Make assumptions about the class; we'll reset the flags if
6791 necessary. */
6792 CLASSTYPE_EMPTY_P (t) = 1;
6793 CLASSTYPE_NEARLY_EMPTY_P (t) = 1;
6794 CLASSTYPE_CONTAINS_EMPTY_CLASS_P (t) = 0;
6795 CLASSTYPE_LITERAL_P (t) = true;
6796
6797 /* Do end-of-class semantic processing: checking the validity of the
6798 bases and members and add implicitly generated methods. */
6799 check_bases_and_members (t);
6800
6801 /* Find the key method. */
6802 if (TYPE_CONTAINS_VPTR_P (t))
6803 {
6804 /* The Itanium C++ ABI permits the key method to be chosen when
6805 the class is defined -- even though the key method so
6806 selected may later turn out to be an inline function. On
6807 some systems (such as ARM Symbian OS) the key method cannot
6808 be determined until the end of the translation unit. On such
6809 systems, we leave CLASSTYPE_KEY_METHOD set to NULL, which
6810 will cause the class to be added to KEYED_CLASSES. Then, in
6811 finish_file we will determine the key method. */
6812 if (targetm.cxx.key_method_may_be_inline ())
6813 determine_key_method (t);
6814
6815 /* If a polymorphic class has no key method, we may emit the vtable
6816 in every translation unit where the class definition appears. If
6817 we're devirtualizing, we can look into the vtable even if we
6818 aren't emitting it. */
6819 if (!CLASSTYPE_KEY_METHOD (t))
6820 vec_safe_push (keyed_classes, t);
6821 }
6822
6823 /* Layout the class itself. */
6824 layout_class_type (t, &virtuals);
6825 /* COMPLETE_TYPE_P is now true. */
6826
6827 set_class_bindings (t);
6828
6829 /* With the layout complete, check for flexible array members and
6830 zero-length arrays that might overlap other members in the final
6831 layout. */
6832 check_flexarrays (t);
6833
6834 virtuals = modify_all_vtables (t, nreverse (virtuals));
6835
6836 /* If necessary, create the primary vtable for this class. */
6837 if (virtuals || TYPE_CONTAINS_VPTR_P (t))
6838 {
6839 /* We must enter these virtuals into the table. */
6840 if (!CLASSTYPE_HAS_PRIMARY_BASE_P (t))
6841 build_primary_vtable (NULL_TREE, t);
6842 else if (! BINFO_NEW_VTABLE_MARKED (TYPE_BINFO (t)))
6843 /* Here we know enough to change the type of our virtual
6844 function table, but we will wait until later this function. */
6845 build_primary_vtable (CLASSTYPE_PRIMARY_BINFO (t), t);
6846
6847 /* If we're warning about ABI tags, check the types of the new
6848 virtual functions. */
6849 if (warn_abi_tag)
6850 for (tree v = virtuals; v; v = TREE_CHAIN (v))
6851 check_abi_tags (t, TREE_VALUE (v));
6852 }
6853
6854 if (TYPE_CONTAINS_VPTR_P (t))
6855 {
6856 int vindex;
6857 tree fn;
6858
6859 if (BINFO_VTABLE (TYPE_BINFO (t)))
6860 gcc_assert (DECL_VIRTUAL_P (BINFO_VTABLE (TYPE_BINFO (t))));
6861 if (!CLASSTYPE_HAS_PRIMARY_BASE_P (t))
6862 gcc_assert (BINFO_VIRTUALS (TYPE_BINFO (t)) == NULL_TREE);
6863
6864 /* Add entries for virtual functions introduced by this class. */
6865 BINFO_VIRTUALS (TYPE_BINFO (t))
6866 = chainon (BINFO_VIRTUALS (TYPE_BINFO (t)), virtuals);
6867
6868 /* Set DECL_VINDEX for all functions declared in this class. */
6869 for (vindex = 0, fn = BINFO_VIRTUALS (TYPE_BINFO (t));
6870 fn;
6871 fn = TREE_CHAIN (fn),
6872 vindex += (TARGET_VTABLE_USES_DESCRIPTORS
6873 ? TARGET_VTABLE_USES_DESCRIPTORS : 1))
6874 {
6875 tree fndecl = BV_FN (fn);
6876
6877 if (DECL_THUNK_P (fndecl))
6878 /* A thunk. We should never be calling this entry directly
6879 from this vtable -- we'd use the entry for the non
6880 thunk base function. */
6881 DECL_VINDEX (fndecl) = NULL_TREE;
6882 else if (TREE_CODE (DECL_VINDEX (fndecl)) != INTEGER_CST)
6883 DECL_VINDEX (fndecl) = build_int_cst (NULL_TREE, vindex);
6884 }
6885 }
6886
6887 finish_struct_bits (t);
6888
6889 set_method_tm_attributes (t);
6890 if (flag_openmp || flag_openmp_simd)
6891 finish_omp_declare_simd_methods (t);
6892
6893 /* Clear DECL_IN_AGGR_P for all member functions. Complete the rtl
6894 for any static member objects of the type we're working on. */
6895 for (x = TYPE_FIELDS (t); x; x = DECL_CHAIN (x))
6896 if (DECL_DECLARES_FUNCTION_P (x))
6897 DECL_IN_AGGR_P (x) = false;
6898 else if (VAR_P (x) && TREE_STATIC (x)
6899 && TREE_TYPE (x) != error_mark_node
6900 && same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (x)), t))
6901 SET_DECL_MODE (x, TYPE_MODE (t));
6902
6903 /* Complain if one of the field types requires lower visibility. */
6904 constrain_class_visibility (t);
6905
6906 /* Make the rtl for any new vtables we have created, and unmark
6907 the base types we marked. */
6908 finish_vtbls (t);
6909
6910 /* Build the VTT for T. */
6911 build_vtt (t);
6912
6913 if (warn_nonvdtor
6914 && TYPE_POLYMORPHIC_P (t) && accessible_nvdtor_p (t)
6915 && !CLASSTYPE_FINAL (t))
6916 warning (OPT_Wnon_virtual_dtor,
6917 "%q#T has virtual functions and accessible"
6918 " non-virtual destructor", t);
6919
6920 complete_vars (t);
6921
6922 if (warn_overloaded_virtual)
6923 warn_hidden (t);
6924
6925 /* Class layout, assignment of virtual table slots, etc., is now
6926 complete. Give the back end a chance to tweak the visibility of
6927 the class or perform any other required target modifications. */
6928 targetm.cxx.adjust_class_at_definition (t);
6929
6930 maybe_suppress_debug_info (t);
6931
6932 if (flag_vtable_verify)
6933 vtv_save_class_info (t);
6934
6935 dump_class_hierarchy (t);
6936
6937 /* Finish debugging output for this type. */
6938 rest_of_type_compilation (t, ! LOCAL_CLASS_P (t));
6939
6940 if (TYPE_TRANSPARENT_AGGR (t))
6941 {
6942 tree field = first_field (t);
6943 if (field == NULL_TREE || error_operand_p (field))
6944 {
6945 error ("type transparent %q#T does not have any fields", t);
6946 TYPE_TRANSPARENT_AGGR (t) = 0;
6947 }
6948 else if (DECL_ARTIFICIAL (field))
6949 {
6950 if (DECL_FIELD_IS_BASE (field))
6951 error ("type transparent class %qT has base classes", t);
6952 else
6953 {
6954 gcc_checking_assert (DECL_VIRTUAL_P (field));
6955 error ("type transparent class %qT has virtual functions", t);
6956 }
6957 TYPE_TRANSPARENT_AGGR (t) = 0;
6958 }
6959 else if (TYPE_MODE (t) != DECL_MODE (field))
6960 {
6961 error ("type transparent %q#T cannot be made transparent because "
6962 "the type of the first field has a different ABI from the "
6963 "class overall", t);
6964 TYPE_TRANSPARENT_AGGR (t) = 0;
6965 }
6966 }
6967 }
6968
6969 /* When T was built up, the member declarations were added in reverse
6970 order. Rearrange them to declaration order. */
6971
6972 void
6973 unreverse_member_declarations (tree t)
6974 {
6975 tree next;
6976 tree prev;
6977 tree x;
6978
6979 /* The following lists are all in reverse order. Put them in
6980 declaration order now. */
6981 CLASSTYPE_DECL_LIST (t) = nreverse (CLASSTYPE_DECL_LIST (t));
6982
6983 /* For the TYPE_FIELDS, only the non TYPE_DECLs are in reverse
6984 order, so we can't just use nreverse. Due to stat_hack
6985 chicanery in finish_member_declaration. */
6986 prev = NULL_TREE;
6987 for (x = TYPE_FIELDS (t);
6988 x && TREE_CODE (x) != TYPE_DECL;
6989 x = next)
6990 {
6991 next = DECL_CHAIN (x);
6992 DECL_CHAIN (x) = prev;
6993 prev = x;
6994 }
6995
6996 if (prev)
6997 {
6998 DECL_CHAIN (TYPE_FIELDS (t)) = x;
6999 TYPE_FIELDS (t) = prev;
7000 }
7001 }
7002
7003 tree
7004 finish_struct (tree t, tree attributes)
7005 {
7006 location_t saved_loc = input_location;
7007
7008 /* Now that we've got all the field declarations, reverse everything
7009 as necessary. */
7010 unreverse_member_declarations (t);
7011
7012 cplus_decl_attributes (&t, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
7013 fixup_attribute_variants (t);
7014
7015 /* Nadger the current location so that diagnostics point to the start of
7016 the struct, not the end. */
7017 input_location = DECL_SOURCE_LOCATION (TYPE_NAME (t));
7018
7019 if (processing_template_decl)
7020 {
7021 tree x;
7022
7023 /* We need to add the target functions of USING_DECLS, so that
7024 they can be found when the using declaration is not
7025 instantiated yet. */
7026 for (x = TYPE_FIELDS (t); x; x = DECL_CHAIN (x))
7027 if (TREE_CODE (x) == USING_DECL)
7028 {
7029 tree fn = strip_using_decl (x);
7030 if (OVL_P (fn))
7031 for (lkp_iterator iter (fn); iter; ++iter)
7032 add_method (t, *iter, true);
7033 }
7034 else if (DECL_DECLARES_FUNCTION_P (x))
7035 DECL_IN_AGGR_P (x) = false;
7036
7037 TYPE_SIZE (t) = bitsize_zero_node;
7038 TYPE_SIZE_UNIT (t) = size_zero_node;
7039 /* COMPLETE_TYPE_P is now true. */
7040
7041 set_class_bindings (t);
7042
7043 /* We need to emit an error message if this type was used as a parameter
7044 and it is an abstract type, even if it is a template. We construct
7045 a simple CLASSTYPE_PURE_VIRTUALS list without taking bases into
7046 account and we call complete_vars with this type, which will check
7047 the PARM_DECLS. Note that while the type is being defined,
7048 CLASSTYPE_PURE_VIRTUALS contains the list of the inline friends
7049 (see CLASSTYPE_INLINE_FRIENDS) so we need to clear it. */
7050 CLASSTYPE_PURE_VIRTUALS (t) = NULL;
7051 for (x = TYPE_FIELDS (t); x; x = DECL_CHAIN (x))
7052 if (TREE_CODE (x) == FUNCTION_DECL && DECL_PURE_VIRTUAL_P (x))
7053 vec_safe_push (CLASSTYPE_PURE_VIRTUALS (t), x);
7054 complete_vars (t);
7055
7056 /* Remember current #pragma pack value. */
7057 TYPE_PRECISION (t) = maximum_field_alignment;
7058
7059 /* Fix up any variants we've already built. */
7060 for (x = TYPE_NEXT_VARIANT (t); x; x = TYPE_NEXT_VARIANT (x))
7061 {
7062 TYPE_SIZE (x) = TYPE_SIZE (t);
7063 TYPE_SIZE_UNIT (x) = TYPE_SIZE_UNIT (t);
7064 TYPE_FIELDS (x) = TYPE_FIELDS (t);
7065 }
7066 }
7067 else
7068 finish_struct_1 (t);
7069 /* COMPLETE_TYPE_P is now true. */
7070
7071 maybe_warn_about_overly_private_class (t);
7072
7073 if (is_std_init_list (t))
7074 {
7075 /* People keep complaining that the compiler crashes on an invalid
7076 definition of initializer_list, so I guess we should explicitly
7077 reject it. What the compiler internals care about is that it's a
7078 template and has a pointer field followed by an integer field. */
7079 bool ok = false;
7080 if (processing_template_decl)
7081 {
7082 tree f = next_initializable_field (TYPE_FIELDS (t));
7083 if (f && TREE_CODE (TREE_TYPE (f)) == POINTER_TYPE)
7084 {
7085 f = next_initializable_field (DECL_CHAIN (f));
7086 if (f && same_type_p (TREE_TYPE (f), size_type_node))
7087 ok = true;
7088 }
7089 }
7090 if (!ok)
7091 fatal_error (input_location,
7092 "definition of std::initializer_list does not match "
7093 "#include <initializer_list>");
7094 }
7095
7096 input_location = saved_loc;
7097
7098 TYPE_BEING_DEFINED (t) = 0;
7099
7100 if (current_class_type)
7101 popclass ();
7102 else
7103 error ("trying to finish struct, but kicked out due to previous parse errors");
7104
7105 if (processing_template_decl && at_function_scope_p ()
7106 /* Lambdas are defined by the LAMBDA_EXPR. */
7107 && !LAMBDA_TYPE_P (t))
7108 add_stmt (build_min (TAG_DEFN, t));
7109
7110 return t;
7111 }
7112 \f
7113 /* Hash table to avoid endless recursion when handling references. */
7114 static hash_table<nofree_ptr_hash<tree_node> > *fixed_type_or_null_ref_ht;
7115
7116 /* Return the dynamic type of INSTANCE, if known.
7117 Used to determine whether the virtual function table is needed
7118 or not.
7119
7120 *NONNULL is set iff INSTANCE can be known to be nonnull, regardless
7121 of our knowledge of its type. *NONNULL should be initialized
7122 before this function is called. */
7123
7124 static tree
7125 fixed_type_or_null (tree instance, int *nonnull, int *cdtorp)
7126 {
7127 #define RECUR(T) fixed_type_or_null((T), nonnull, cdtorp)
7128
7129 switch (TREE_CODE (instance))
7130 {
7131 case INDIRECT_REF:
7132 if (POINTER_TYPE_P (TREE_TYPE (instance)))
7133 return NULL_TREE;
7134 else
7135 return RECUR (TREE_OPERAND (instance, 0));
7136
7137 case CALL_EXPR:
7138 /* This is a call to a constructor, hence it's never zero. */
7139 if (TREE_HAS_CONSTRUCTOR (instance))
7140 {
7141 if (nonnull)
7142 *nonnull = 1;
7143 return TREE_TYPE (instance);
7144 }
7145 return NULL_TREE;
7146
7147 case SAVE_EXPR:
7148 /* This is a call to a constructor, hence it's never zero. */
7149 if (TREE_HAS_CONSTRUCTOR (instance))
7150 {
7151 if (nonnull)
7152 *nonnull = 1;
7153 return TREE_TYPE (instance);
7154 }
7155 return RECUR (TREE_OPERAND (instance, 0));
7156
7157 case POINTER_PLUS_EXPR:
7158 case PLUS_EXPR:
7159 case MINUS_EXPR:
7160 if (TREE_CODE (TREE_OPERAND (instance, 0)) == ADDR_EXPR)
7161 return RECUR (TREE_OPERAND (instance, 0));
7162 if (TREE_CODE (TREE_OPERAND (instance, 1)) == INTEGER_CST)
7163 /* Propagate nonnull. */
7164 return RECUR (TREE_OPERAND (instance, 0));
7165
7166 return NULL_TREE;
7167
7168 CASE_CONVERT:
7169 return RECUR (TREE_OPERAND (instance, 0));
7170
7171 case ADDR_EXPR:
7172 instance = TREE_OPERAND (instance, 0);
7173 if (nonnull)
7174 {
7175 /* Just because we see an ADDR_EXPR doesn't mean we're dealing
7176 with a real object -- given &p->f, p can still be null. */
7177 tree t = get_base_address (instance);
7178 /* ??? Probably should check DECL_WEAK here. */
7179 if (t && DECL_P (t))
7180 *nonnull = 1;
7181 }
7182 return RECUR (instance);
7183
7184 case COMPONENT_REF:
7185 /* If this component is really a base class reference, then the field
7186 itself isn't definitive. */
7187 if (DECL_FIELD_IS_BASE (TREE_OPERAND (instance, 1)))
7188 return RECUR (TREE_OPERAND (instance, 0));
7189 return RECUR (TREE_OPERAND (instance, 1));
7190
7191 case VAR_DECL:
7192 case FIELD_DECL:
7193 if (TREE_CODE (TREE_TYPE (instance)) == ARRAY_TYPE
7194 && MAYBE_CLASS_TYPE_P (TREE_TYPE (TREE_TYPE (instance))))
7195 {
7196 if (nonnull)
7197 *nonnull = 1;
7198 return TREE_TYPE (TREE_TYPE (instance));
7199 }
7200 /* fall through. */
7201 case TARGET_EXPR:
7202 case PARM_DECL:
7203 case RESULT_DECL:
7204 if (MAYBE_CLASS_TYPE_P (TREE_TYPE (instance)))
7205 {
7206 if (nonnull)
7207 *nonnull = 1;
7208 return TREE_TYPE (instance);
7209 }
7210 else if (instance == current_class_ptr)
7211 {
7212 if (nonnull)
7213 *nonnull = 1;
7214
7215 /* if we're in a ctor or dtor, we know our type. If
7216 current_class_ptr is set but we aren't in a function, we're in
7217 an NSDMI (and therefore a constructor). */
7218 if (current_scope () != current_function_decl
7219 || (DECL_LANG_SPECIFIC (current_function_decl)
7220 && (DECL_CONSTRUCTOR_P (current_function_decl)
7221 || DECL_DESTRUCTOR_P (current_function_decl))))
7222 {
7223 if (cdtorp)
7224 *cdtorp = 1;
7225 return TREE_TYPE (TREE_TYPE (instance));
7226 }
7227 }
7228 else if (TREE_CODE (TREE_TYPE (instance)) == REFERENCE_TYPE)
7229 {
7230 /* We only need one hash table because it is always left empty. */
7231 if (!fixed_type_or_null_ref_ht)
7232 fixed_type_or_null_ref_ht
7233 = new hash_table<nofree_ptr_hash<tree_node> > (37);
7234
7235 /* Reference variables should be references to objects. */
7236 if (nonnull)
7237 *nonnull = 1;
7238
7239 /* Enter the INSTANCE in a table to prevent recursion; a
7240 variable's initializer may refer to the variable
7241 itself. */
7242 if (VAR_P (instance)
7243 && DECL_INITIAL (instance)
7244 && !type_dependent_expression_p_push (DECL_INITIAL (instance))
7245 && !fixed_type_or_null_ref_ht->find (instance))
7246 {
7247 tree type;
7248 tree_node **slot;
7249
7250 slot = fixed_type_or_null_ref_ht->find_slot (instance, INSERT);
7251 *slot = instance;
7252 type = RECUR (DECL_INITIAL (instance));
7253 fixed_type_or_null_ref_ht->remove_elt (instance);
7254
7255 return type;
7256 }
7257 }
7258 return NULL_TREE;
7259
7260 default:
7261 return NULL_TREE;
7262 }
7263 #undef RECUR
7264 }
7265
7266 /* Return nonzero if the dynamic type of INSTANCE is known, and
7267 equivalent to the static type. We also handle the case where
7268 INSTANCE is really a pointer. Return negative if this is a
7269 ctor/dtor. There the dynamic type is known, but this might not be
7270 the most derived base of the original object, and hence virtual
7271 bases may not be laid out according to this type.
7272
7273 Used to determine whether the virtual function table is needed
7274 or not.
7275
7276 *NONNULL is set iff INSTANCE can be known to be nonnull, regardless
7277 of our knowledge of its type. *NONNULL should be initialized
7278 before this function is called. */
7279
7280 int
7281 resolves_to_fixed_type_p (tree instance, int* nonnull)
7282 {
7283 tree t = TREE_TYPE (instance);
7284 int cdtorp = 0;
7285 tree fixed;
7286
7287 /* processing_template_decl can be false in a template if we're in
7288 instantiate_non_dependent_expr, but we still want to suppress
7289 this check. */
7290 if (in_template_function ())
7291 {
7292 /* In a template we only care about the type of the result. */
7293 if (nonnull)
7294 *nonnull = true;
7295 return true;
7296 }
7297
7298 fixed = fixed_type_or_null (instance, nonnull, &cdtorp);
7299 if (fixed == NULL_TREE)
7300 return 0;
7301 if (POINTER_TYPE_P (t))
7302 t = TREE_TYPE (t);
7303 if (!same_type_ignoring_top_level_qualifiers_p (t, fixed))
7304 return 0;
7305 return cdtorp ? -1 : 1;
7306 }
7307
7308 \f
7309 void
7310 init_class_processing (void)
7311 {
7312 current_class_depth = 0;
7313 current_class_stack_size = 10;
7314 current_class_stack
7315 = XNEWVEC (struct class_stack_node, current_class_stack_size);
7316 vec_alloc (local_classes, 8);
7317 sizeof_biggest_empty_class = size_zero_node;
7318
7319 ridpointers[(int) RID_PUBLIC] = access_public_node;
7320 ridpointers[(int) RID_PRIVATE] = access_private_node;
7321 ridpointers[(int) RID_PROTECTED] = access_protected_node;
7322 }
7323
7324 /* Restore the cached PREVIOUS_CLASS_LEVEL. */
7325
7326 static void
7327 restore_class_cache (void)
7328 {
7329 tree type;
7330
7331 /* We are re-entering the same class we just left, so we don't
7332 have to search the whole inheritance matrix to find all the
7333 decls to bind again. Instead, we install the cached
7334 class_shadowed list and walk through it binding names. */
7335 push_binding_level (previous_class_level);
7336 class_binding_level = previous_class_level;
7337 /* Restore IDENTIFIER_TYPE_VALUE. */
7338 for (type = class_binding_level->type_shadowed;
7339 type;
7340 type = TREE_CHAIN (type))
7341 SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (type), TREE_TYPE (type));
7342 }
7343
7344 /* Set global variables CURRENT_CLASS_NAME and CURRENT_CLASS_TYPE as
7345 appropriate for TYPE.
7346
7347 So that we may avoid calls to lookup_name, we cache the _TYPE
7348 nodes of local TYPE_DECLs in the TREE_TYPE field of the name.
7349
7350 For multiple inheritance, we perform a two-pass depth-first search
7351 of the type lattice. */
7352
7353 void
7354 pushclass (tree type)
7355 {
7356 class_stack_node_t csn;
7357
7358 type = TYPE_MAIN_VARIANT (type);
7359
7360 /* Make sure there is enough room for the new entry on the stack. */
7361 if (current_class_depth + 1 >= current_class_stack_size)
7362 {
7363 current_class_stack_size *= 2;
7364 current_class_stack
7365 = XRESIZEVEC (struct class_stack_node, current_class_stack,
7366 current_class_stack_size);
7367 }
7368
7369 /* Insert a new entry on the class stack. */
7370 csn = current_class_stack + current_class_depth;
7371 csn->name = current_class_name;
7372 csn->type = current_class_type;
7373 csn->access = current_access_specifier;
7374 csn->names_used = 0;
7375 csn->hidden = 0;
7376 current_class_depth++;
7377
7378 /* Now set up the new type. */
7379 current_class_name = TYPE_NAME (type);
7380 if (TREE_CODE (current_class_name) == TYPE_DECL)
7381 current_class_name = DECL_NAME (current_class_name);
7382 current_class_type = type;
7383
7384 /* By default, things in classes are private, while things in
7385 structures or unions are public. */
7386 current_access_specifier = (CLASSTYPE_DECLARED_CLASS (type)
7387 ? access_private_node
7388 : access_public_node);
7389
7390 if (previous_class_level
7391 && type != previous_class_level->this_entity
7392 && current_class_depth == 1)
7393 {
7394 /* Forcibly remove any old class remnants. */
7395 invalidate_class_lookup_cache ();
7396 }
7397
7398 if (!previous_class_level
7399 || type != previous_class_level->this_entity
7400 || current_class_depth > 1)
7401 pushlevel_class ();
7402 else
7403 restore_class_cache ();
7404 }
7405
7406 /* When we exit a toplevel class scope, we save its binding level so
7407 that we can restore it quickly. Here, we've entered some other
7408 class, so we must invalidate our cache. */
7409
7410 void
7411 invalidate_class_lookup_cache (void)
7412 {
7413 previous_class_level = NULL;
7414 }
7415
7416 /* Get out of the current class scope. If we were in a class scope
7417 previously, that is the one popped to. */
7418
7419 void
7420 popclass (void)
7421 {
7422 poplevel_class ();
7423
7424 current_class_depth--;
7425 current_class_name = current_class_stack[current_class_depth].name;
7426 current_class_type = current_class_stack[current_class_depth].type;
7427 current_access_specifier = current_class_stack[current_class_depth].access;
7428 if (current_class_stack[current_class_depth].names_used)
7429 splay_tree_delete (current_class_stack[current_class_depth].names_used);
7430 }
7431
7432 /* Mark the top of the class stack as hidden. */
7433
7434 void
7435 push_class_stack (void)
7436 {
7437 if (current_class_depth)
7438 ++current_class_stack[current_class_depth - 1].hidden;
7439 }
7440
7441 /* Mark the top of the class stack as un-hidden. */
7442
7443 void
7444 pop_class_stack (void)
7445 {
7446 if (current_class_depth)
7447 --current_class_stack[current_class_depth - 1].hidden;
7448 }
7449
7450 /* Returns 1 if the class type currently being defined is either T or
7451 a nested type of T. Returns the type from the current_class_stack,
7452 which might be equivalent to but not equal to T in case of
7453 constrained partial specializations. */
7454
7455 tree
7456 currently_open_class (tree t)
7457 {
7458 int i;
7459
7460 if (!CLASS_TYPE_P (t))
7461 return NULL_TREE;
7462
7463 t = TYPE_MAIN_VARIANT (t);
7464
7465 /* We start looking from 1 because entry 0 is from global scope,
7466 and has no type. */
7467 for (i = current_class_depth; i > 0; --i)
7468 {
7469 tree c;
7470 if (i == current_class_depth)
7471 c = current_class_type;
7472 else
7473 {
7474 if (current_class_stack[i].hidden)
7475 break;
7476 c = current_class_stack[i].type;
7477 }
7478 if (!c)
7479 continue;
7480 if (same_type_p (c, t))
7481 return c;
7482 }
7483 return NULL_TREE;
7484 }
7485
7486 /* If either current_class_type or one of its enclosing classes are derived
7487 from T, return the appropriate type. Used to determine how we found
7488 something via unqualified lookup. */
7489
7490 tree
7491 currently_open_derived_class (tree t)
7492 {
7493 int i;
7494
7495 /* The bases of a dependent type are unknown. */
7496 if (dependent_type_p (t))
7497 return NULL_TREE;
7498
7499 if (!current_class_type)
7500 return NULL_TREE;
7501
7502 if (DERIVED_FROM_P (t, current_class_type))
7503 return current_class_type;
7504
7505 for (i = current_class_depth - 1; i > 0; --i)
7506 {
7507 if (current_class_stack[i].hidden)
7508 break;
7509 if (DERIVED_FROM_P (t, current_class_stack[i].type))
7510 return current_class_stack[i].type;
7511 }
7512
7513 return NULL_TREE;
7514 }
7515
7516 /* Return the outermost enclosing class type that is still open, or
7517 NULL_TREE. */
7518
7519 tree
7520 outermost_open_class (void)
7521 {
7522 if (!current_class_type)
7523 return NULL_TREE;
7524 tree r = NULL_TREE;
7525 if (TYPE_BEING_DEFINED (current_class_type))
7526 r = current_class_type;
7527 for (int i = current_class_depth - 1; i > 0; --i)
7528 {
7529 if (current_class_stack[i].hidden)
7530 break;
7531 tree t = current_class_stack[i].type;
7532 if (!TYPE_BEING_DEFINED (t))
7533 break;
7534 r = t;
7535 }
7536 return r;
7537 }
7538
7539 /* Returns the innermost class type which is not a lambda closure type. */
7540
7541 tree
7542 current_nonlambda_class_type (void)
7543 {
7544 tree type = current_class_type;
7545 while (type && LAMBDA_TYPE_P (type))
7546 type = decl_type_context (TYPE_NAME (type));
7547 return type;
7548 }
7549
7550 /* When entering a class scope, all enclosing class scopes' names with
7551 static meaning (static variables, static functions, types and
7552 enumerators) have to be visible. This recursive function calls
7553 pushclass for all enclosing class contexts until global or a local
7554 scope is reached. TYPE is the enclosed class. */
7555
7556 void
7557 push_nested_class (tree type)
7558 {
7559 /* A namespace might be passed in error cases, like A::B:C. */
7560 if (type == NULL_TREE
7561 || !CLASS_TYPE_P (type))
7562 return;
7563
7564 push_nested_class (DECL_CONTEXT (TYPE_MAIN_DECL (type)));
7565
7566 pushclass (type);
7567 }
7568
7569 /* Undoes a push_nested_class call. */
7570
7571 void
7572 pop_nested_class (void)
7573 {
7574 tree context = DECL_CONTEXT (TYPE_MAIN_DECL (current_class_type));
7575
7576 popclass ();
7577 if (context && CLASS_TYPE_P (context))
7578 pop_nested_class ();
7579 }
7580
7581 /* Returns the number of extern "LANG" blocks we are nested within. */
7582
7583 int
7584 current_lang_depth (void)
7585 {
7586 return vec_safe_length (current_lang_base);
7587 }
7588
7589 /* Set global variables CURRENT_LANG_NAME to appropriate value
7590 so that behavior of name-mangling machinery is correct. */
7591
7592 void
7593 push_lang_context (tree name)
7594 {
7595 vec_safe_push (current_lang_base, current_lang_name);
7596
7597 if (name == lang_name_cplusplus)
7598 current_lang_name = name;
7599 else if (name == lang_name_c)
7600 current_lang_name = name;
7601 else
7602 error ("language string %<\"%E\"%> not recognized", name);
7603 }
7604
7605 /* Get out of the current language scope. */
7606
7607 void
7608 pop_lang_context (void)
7609 {
7610 current_lang_name = current_lang_base->pop ();
7611 }
7612 \f
7613 /* Type instantiation routines. */
7614
7615 /* Given an OVERLOAD and a TARGET_TYPE, return the function that
7616 matches the TARGET_TYPE. If there is no satisfactory match, return
7617 error_mark_node, and issue an error & warning messages under
7618 control of FLAGS. Permit pointers to member function if FLAGS
7619 permits. If TEMPLATE_ONLY, the name of the overloaded function was
7620 a template-id, and EXPLICIT_TARGS are the explicitly provided
7621 template arguments.
7622
7623 If OVERLOAD is for one or more member functions, then ACCESS_PATH
7624 is the base path used to reference those member functions. If
7625 the address is resolved to a member function, access checks will be
7626 performed and errors issued if appropriate. */
7627
7628 static tree
7629 resolve_address_of_overloaded_function (tree target_type,
7630 tree overload,
7631 tsubst_flags_t complain,
7632 bool template_only,
7633 tree explicit_targs,
7634 tree access_path)
7635 {
7636 /* Here's what the standard says:
7637
7638 [over.over]
7639
7640 If the name is a function template, template argument deduction
7641 is done, and if the argument deduction succeeds, the deduced
7642 arguments are used to generate a single template function, which
7643 is added to the set of overloaded functions considered.
7644
7645 Non-member functions and static member functions match targets of
7646 type "pointer-to-function" or "reference-to-function." Nonstatic
7647 member functions match targets of type "pointer-to-member
7648 function;" the function type of the pointer to member is used to
7649 select the member function from the set of overloaded member
7650 functions. If a nonstatic member function is selected, the
7651 reference to the overloaded function name is required to have the
7652 form of a pointer to member as described in 5.3.1.
7653
7654 If more than one function is selected, any template functions in
7655 the set are eliminated if the set also contains a non-template
7656 function, and any given template function is eliminated if the
7657 set contains a second template function that is more specialized
7658 than the first according to the partial ordering rules 14.5.5.2.
7659 After such eliminations, if any, there shall remain exactly one
7660 selected function. */
7661
7662 int is_ptrmem = 0;
7663 /* We store the matches in a TREE_LIST rooted here. The functions
7664 are the TREE_PURPOSE, not the TREE_VALUE, in this list, for easy
7665 interoperability with most_specialized_instantiation. */
7666 tree matches = NULL_TREE;
7667 tree fn;
7668 tree target_fn_type;
7669
7670 /* By the time we get here, we should be seeing only real
7671 pointer-to-member types, not the internal POINTER_TYPE to
7672 METHOD_TYPE representation. */
7673 gcc_assert (!TYPE_PTR_P (target_type)
7674 || TREE_CODE (TREE_TYPE (target_type)) != METHOD_TYPE);
7675
7676 gcc_assert (is_overloaded_fn (overload));
7677
7678 /* Check that the TARGET_TYPE is reasonable. */
7679 if (TYPE_PTRFN_P (target_type)
7680 || TYPE_REFFN_P (target_type))
7681 /* This is OK. */;
7682 else if (TYPE_PTRMEMFUNC_P (target_type))
7683 /* This is OK, too. */
7684 is_ptrmem = 1;
7685 else if (TREE_CODE (target_type) == FUNCTION_TYPE)
7686 /* This is OK, too. This comes from a conversion to reference
7687 type. */
7688 target_type = build_reference_type (target_type);
7689 else
7690 {
7691 if (complain & tf_error)
7692 error ("cannot resolve overloaded function %qD based on"
7693 " conversion to type %qT",
7694 OVL_NAME (overload), target_type);
7695 return error_mark_node;
7696 }
7697
7698 /* Non-member functions and static member functions match targets of type
7699 "pointer-to-function" or "reference-to-function." Nonstatic member
7700 functions match targets of type "pointer-to-member-function;" the
7701 function type of the pointer to member is used to select the member
7702 function from the set of overloaded member functions.
7703
7704 So figure out the FUNCTION_TYPE that we want to match against. */
7705 target_fn_type = static_fn_type (target_type);
7706
7707 /* If we can find a non-template function that matches, we can just
7708 use it. There's no point in generating template instantiations
7709 if we're just going to throw them out anyhow. But, of course, we
7710 can only do this when we don't *need* a template function. */
7711 if (!template_only)
7712 for (lkp_iterator iter (overload); iter; ++iter)
7713 {
7714 tree fn = *iter;
7715
7716 if (TREE_CODE (fn) == TEMPLATE_DECL)
7717 /* We're not looking for templates just yet. */
7718 continue;
7719
7720 if ((TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE) != is_ptrmem)
7721 /* We're looking for a non-static member, and this isn't
7722 one, or vice versa. */
7723 continue;
7724
7725 /* In C++17 we need the noexcept-qualifier to compare types. */
7726 if (flag_noexcept_type
7727 && !maybe_instantiate_noexcept (fn, complain))
7728 continue;
7729
7730 /* See if there's a match. */
7731 tree fntype = static_fn_type (fn);
7732 if (same_type_p (target_fn_type, fntype)
7733 || fnptr_conv_p (target_fn_type, fntype))
7734 matches = tree_cons (fn, NULL_TREE, matches);
7735 }
7736
7737 /* Now, if we've already got a match (or matches), there's no need
7738 to proceed to the template functions. But, if we don't have a
7739 match we need to look at them, too. */
7740 if (!matches)
7741 {
7742 tree target_arg_types;
7743 tree target_ret_type;
7744 tree *args;
7745 unsigned int nargs, ia;
7746 tree arg;
7747
7748 target_arg_types = TYPE_ARG_TYPES (target_fn_type);
7749 target_ret_type = TREE_TYPE (target_fn_type);
7750
7751 nargs = list_length (target_arg_types);
7752 args = XALLOCAVEC (tree, nargs);
7753 for (arg = target_arg_types, ia = 0;
7754 arg != NULL_TREE && arg != void_list_node;
7755 arg = TREE_CHAIN (arg), ++ia)
7756 args[ia] = TREE_VALUE (arg);
7757 nargs = ia;
7758
7759 for (lkp_iterator iter (overload); iter; ++iter)
7760 {
7761 tree fn = *iter;
7762 tree instantiation;
7763 tree targs;
7764
7765 if (TREE_CODE (fn) != TEMPLATE_DECL)
7766 /* We're only looking for templates. */
7767 continue;
7768
7769 if ((TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
7770 != is_ptrmem)
7771 /* We're not looking for a non-static member, and this is
7772 one, or vice versa. */
7773 continue;
7774
7775 tree ret = target_ret_type;
7776
7777 /* If the template has a deduced return type, don't expose it to
7778 template argument deduction. */
7779 if (undeduced_auto_decl (fn))
7780 ret = NULL_TREE;
7781
7782 /* Try to do argument deduction. */
7783 targs = make_tree_vec (DECL_NTPARMS (fn));
7784 instantiation = fn_type_unification (fn, explicit_targs, targs, args,
7785 nargs, ret,
7786 DEDUCE_EXACT, LOOKUP_NORMAL,
7787 false, false);
7788 if (instantiation == error_mark_node)
7789 /* Instantiation failed. */
7790 continue;
7791
7792 /* Constraints must be satisfied. This is done before
7793 return type deduction since that instantiates the
7794 function. */
7795 if (flag_concepts && !constraints_satisfied_p (instantiation))
7796 continue;
7797
7798 /* And now force instantiation to do return type deduction. */
7799 if (undeduced_auto_decl (instantiation))
7800 {
7801 ++function_depth;
7802 instantiate_decl (instantiation, /*defer*/false, /*class*/false);
7803 --function_depth;
7804
7805 require_deduced_type (instantiation);
7806 }
7807
7808 /* In C++17 we need the noexcept-qualifier to compare types. */
7809 if (flag_noexcept_type)
7810 maybe_instantiate_noexcept (instantiation, complain);
7811
7812 /* See if there's a match. */
7813 tree fntype = static_fn_type (instantiation);
7814 if (same_type_p (target_fn_type, fntype)
7815 || fnptr_conv_p (target_fn_type, fntype))
7816 matches = tree_cons (instantiation, fn, matches);
7817 }
7818
7819 /* Now, remove all but the most specialized of the matches. */
7820 if (matches)
7821 {
7822 tree match = most_specialized_instantiation (matches);
7823
7824 if (match != error_mark_node)
7825 matches = tree_cons (TREE_PURPOSE (match),
7826 NULL_TREE,
7827 NULL_TREE);
7828 }
7829 }
7830
7831 /* Now we should have exactly one function in MATCHES. */
7832 if (matches == NULL_TREE)
7833 {
7834 /* There were *no* matches. */
7835 if (complain & tf_error)
7836 {
7837 error ("no matches converting function %qD to type %q#T",
7838 OVL_NAME (overload), target_type);
7839
7840 print_candidates (overload);
7841 }
7842 return error_mark_node;
7843 }
7844 else if (TREE_CHAIN (matches))
7845 {
7846 /* There were too many matches. First check if they're all
7847 the same function. */
7848 tree match = NULL_TREE;
7849
7850 fn = TREE_PURPOSE (matches);
7851
7852 /* For multi-versioned functions, more than one match is just fine and
7853 decls_match will return false as they are different. */
7854 for (match = TREE_CHAIN (matches); match; match = TREE_CHAIN (match))
7855 if (!decls_match (fn, TREE_PURPOSE (match))
7856 && !targetm.target_option.function_versions
7857 (fn, TREE_PURPOSE (match)))
7858 break;
7859
7860 if (match)
7861 {
7862 if (complain & tf_error)
7863 {
7864 error ("converting overloaded function %qD to type %q#T is ambiguous",
7865 OVL_NAME (overload), target_type);
7866
7867 /* Since print_candidates expects the functions in the
7868 TREE_VALUE slot, we flip them here. */
7869 for (match = matches; match; match = TREE_CHAIN (match))
7870 TREE_VALUE (match) = TREE_PURPOSE (match);
7871
7872 print_candidates (matches);
7873 }
7874
7875 return error_mark_node;
7876 }
7877 }
7878
7879 /* Good, exactly one match. Now, convert it to the correct type. */
7880 fn = TREE_PURPOSE (matches);
7881
7882 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
7883 && !(complain & tf_ptrmem_ok) && !flag_ms_extensions)
7884 {
7885 static int explained;
7886
7887 if (!(complain & tf_error))
7888 return error_mark_node;
7889
7890 permerror (input_location, "assuming pointer to member %qD", fn);
7891 if (!explained)
7892 {
7893 inform (input_location, "(a pointer to member can only be formed with %<&%E%>)", fn);
7894 explained = 1;
7895 }
7896 }
7897
7898 /* If a pointer to a function that is multi-versioned is requested, the
7899 pointer to the dispatcher function is returned instead. This works
7900 well because indirectly calling the function will dispatch the right
7901 function version at run-time. */
7902 if (DECL_FUNCTION_VERSIONED (fn))
7903 {
7904 fn = get_function_version_dispatcher (fn);
7905 if (fn == NULL)
7906 return error_mark_node;
7907 /* Mark all the versions corresponding to the dispatcher as used. */
7908 if (!(complain & tf_conv))
7909 mark_versions_used (fn);
7910 }
7911
7912 /* If we're doing overload resolution purely for the purpose of
7913 determining conversion sequences, we should not consider the
7914 function used. If this conversion sequence is selected, the
7915 function will be marked as used at this point. */
7916 if (!(complain & tf_conv))
7917 {
7918 /* Make =delete work with SFINAE. */
7919 if (DECL_DELETED_FN (fn) && !(complain & tf_error))
7920 return error_mark_node;
7921 if (!mark_used (fn, complain) && !(complain & tf_error))
7922 return error_mark_node;
7923 }
7924
7925 /* We could not check access to member functions when this
7926 expression was originally created since we did not know at that
7927 time to which function the expression referred. */
7928 if (DECL_FUNCTION_MEMBER_P (fn))
7929 {
7930 gcc_assert (access_path);
7931 perform_or_defer_access_check (access_path, fn, fn, complain);
7932 }
7933
7934 if (TYPE_PTRFN_P (target_type) || TYPE_PTRMEMFUNC_P (target_type))
7935 return cp_build_addr_expr (fn, complain);
7936 else
7937 {
7938 /* The target must be a REFERENCE_TYPE. Above, cp_build_unary_op
7939 will mark the function as addressed, but here we must do it
7940 explicitly. */
7941 cxx_mark_addressable (fn);
7942
7943 return fn;
7944 }
7945 }
7946
7947 /* This function will instantiate the type of the expression given in
7948 RHS to match the type of LHSTYPE. If errors exist, then return
7949 error_mark_node. COMPLAIN is a bit mask. If TF_ERROR is set, then
7950 we complain on errors. If we are not complaining, never modify rhs,
7951 as overload resolution wants to try many possible instantiations, in
7952 the hope that at least one will work.
7953
7954 For non-recursive calls, LHSTYPE should be a function, pointer to
7955 function, or a pointer to member function. */
7956
7957 tree
7958 instantiate_type (tree lhstype, tree rhs, tsubst_flags_t complain)
7959 {
7960 tsubst_flags_t complain_in = complain;
7961 tree access_path = NULL_TREE;
7962
7963 complain &= ~tf_ptrmem_ok;
7964
7965 if (lhstype == unknown_type_node)
7966 {
7967 if (complain & tf_error)
7968 error ("not enough type information");
7969 return error_mark_node;
7970 }
7971
7972 if (TREE_TYPE (rhs) != NULL_TREE && ! (type_unknown_p (rhs)))
7973 {
7974 tree fntype = non_reference (lhstype);
7975 if (same_type_p (fntype, TREE_TYPE (rhs)))
7976 return rhs;
7977 if (fnptr_conv_p (fntype, TREE_TYPE (rhs)))
7978 return rhs;
7979 if (flag_ms_extensions
7980 && TYPE_PTRMEMFUNC_P (fntype)
7981 && !TYPE_PTRMEMFUNC_P (TREE_TYPE (rhs)))
7982 /* Microsoft allows `A::f' to be resolved to a
7983 pointer-to-member. */
7984 ;
7985 else
7986 {
7987 if (complain & tf_error)
7988 error ("cannot convert %qE from type %qT to type %qT",
7989 rhs, TREE_TYPE (rhs), fntype);
7990 return error_mark_node;
7991 }
7992 }
7993
7994 if (BASELINK_P (rhs))
7995 {
7996 access_path = BASELINK_ACCESS_BINFO (rhs);
7997 rhs = BASELINK_FUNCTIONS (rhs);
7998 }
7999
8000 /* If we are in a template, and have a NON_DEPENDENT_EXPR, we cannot
8001 deduce any type information. */
8002 if (TREE_CODE (rhs) == NON_DEPENDENT_EXPR)
8003 {
8004 if (complain & tf_error)
8005 error ("not enough type information");
8006 return error_mark_node;
8007 }
8008
8009 /* If we instantiate a template, and it is a A ?: C expression
8010 with omitted B, look through the SAVE_EXPR. */
8011 if (TREE_CODE (rhs) == SAVE_EXPR)
8012 rhs = TREE_OPERAND (rhs, 0);
8013
8014 /* There are only a few kinds of expressions that may have a type
8015 dependent on overload resolution. */
8016 gcc_assert (TREE_CODE (rhs) == ADDR_EXPR
8017 || TREE_CODE (rhs) == COMPONENT_REF
8018 || is_overloaded_fn (rhs)
8019 || (flag_ms_extensions && TREE_CODE (rhs) == FUNCTION_DECL));
8020
8021 /* This should really only be used when attempting to distinguish
8022 what sort of a pointer to function we have. For now, any
8023 arithmetic operation which is not supported on pointers
8024 is rejected as an error. */
8025
8026 switch (TREE_CODE (rhs))
8027 {
8028 case COMPONENT_REF:
8029 {
8030 tree member = TREE_OPERAND (rhs, 1);
8031
8032 member = instantiate_type (lhstype, member, complain);
8033 if (member != error_mark_node
8034 && TREE_SIDE_EFFECTS (TREE_OPERAND (rhs, 0)))
8035 /* Do not lose object's side effects. */
8036 return build2 (COMPOUND_EXPR, TREE_TYPE (member),
8037 TREE_OPERAND (rhs, 0), member);
8038 return member;
8039 }
8040
8041 case OFFSET_REF:
8042 rhs = TREE_OPERAND (rhs, 1);
8043 if (BASELINK_P (rhs))
8044 return instantiate_type (lhstype, rhs, complain_in);
8045
8046 /* This can happen if we are forming a pointer-to-member for a
8047 member template. */
8048 gcc_assert (TREE_CODE (rhs) == TEMPLATE_ID_EXPR);
8049
8050 /* Fall through. */
8051
8052 case TEMPLATE_ID_EXPR:
8053 {
8054 tree fns = TREE_OPERAND (rhs, 0);
8055 tree args = TREE_OPERAND (rhs, 1);
8056
8057 return
8058 resolve_address_of_overloaded_function (lhstype, fns, complain_in,
8059 /*template_only=*/true,
8060 args, access_path);
8061 }
8062
8063 case OVERLOAD:
8064 case FUNCTION_DECL:
8065 return
8066 resolve_address_of_overloaded_function (lhstype, rhs, complain_in,
8067 /*template_only=*/false,
8068 /*explicit_targs=*/NULL_TREE,
8069 access_path);
8070
8071 case ADDR_EXPR:
8072 {
8073 if (PTRMEM_OK_P (rhs))
8074 complain |= tf_ptrmem_ok;
8075
8076 return instantiate_type (lhstype, TREE_OPERAND (rhs, 0), complain);
8077 }
8078
8079 case ERROR_MARK:
8080 return error_mark_node;
8081
8082 default:
8083 gcc_unreachable ();
8084 }
8085 return error_mark_node;
8086 }
8087 \f
8088 /* Return the name of the virtual function pointer field
8089 (as an IDENTIFIER_NODE) for the given TYPE. Note that
8090 this may have to look back through base types to find the
8091 ultimate field name. (For single inheritance, these could
8092 all be the same name. Who knows for multiple inheritance). */
8093
8094 static tree
8095 get_vfield_name (tree type)
8096 {
8097 tree binfo, base_binfo;
8098
8099 for (binfo = TYPE_BINFO (type);
8100 BINFO_N_BASE_BINFOS (binfo);
8101 binfo = base_binfo)
8102 {
8103 base_binfo = BINFO_BASE_BINFO (binfo, 0);
8104
8105 if (BINFO_VIRTUAL_P (base_binfo)
8106 || !TYPE_CONTAINS_VPTR_P (BINFO_TYPE (base_binfo)))
8107 break;
8108 }
8109
8110 type = BINFO_TYPE (binfo);
8111 tree ctor_name = constructor_name (type);
8112 char *buf = (char *) alloca (sizeof (VFIELD_NAME_FORMAT)
8113 + IDENTIFIER_LENGTH (ctor_name) + 2);
8114 sprintf (buf, VFIELD_NAME_FORMAT, IDENTIFIER_POINTER (ctor_name));
8115 return get_identifier (buf);
8116 }
8117
8118 void
8119 print_class_statistics (void)
8120 {
8121 if (! GATHER_STATISTICS)
8122 return;
8123
8124 fprintf (stderr, "convert_harshness = %d\n", n_convert_harshness);
8125 fprintf (stderr, "compute_conversion_costs = %d\n", n_compute_conversion_costs);
8126 if (n_vtables)
8127 {
8128 fprintf (stderr, "vtables = %d; vtable searches = %d\n",
8129 n_vtables, n_vtable_searches);
8130 fprintf (stderr, "vtable entries = %d; vtable elems = %d\n",
8131 n_vtable_entries, n_vtable_elems);
8132 }
8133 }
8134
8135 /* Build a dummy reference to ourselves so Derived::Base (and A::A) works,
8136 according to [class]:
8137 The class-name is also inserted
8138 into the scope of the class itself. For purposes of access checking,
8139 the inserted class name is treated as if it were a public member name. */
8140
8141 void
8142 build_self_reference (void)
8143 {
8144 tree name = DECL_NAME (TYPE_NAME (current_class_type));
8145 tree value = build_lang_decl (TYPE_DECL, name, current_class_type);
8146
8147 DECL_NONLOCAL (value) = 1;
8148 DECL_CONTEXT (value) = current_class_type;
8149 DECL_ARTIFICIAL (value) = 1;
8150 SET_DECL_SELF_REFERENCE_P (value);
8151 set_underlying_type (value);
8152
8153 if (processing_template_decl)
8154 value = push_template_decl (value);
8155
8156 tree saved_cas = current_access_specifier;
8157 current_access_specifier = access_public_node;
8158 finish_member_declaration (value);
8159 current_access_specifier = saved_cas;
8160 }
8161
8162 /* Returns 1 if TYPE contains only padding bytes. */
8163
8164 int
8165 is_empty_class (tree type)
8166 {
8167 if (type == error_mark_node)
8168 return 0;
8169
8170 if (! CLASS_TYPE_P (type))
8171 return 0;
8172
8173 return CLASSTYPE_EMPTY_P (type);
8174 }
8175
8176 /* Returns true if TYPE contains no actual data, just various
8177 possible combinations of empty classes and possibly a vptr. */
8178
8179 bool
8180 is_really_empty_class (tree type)
8181 {
8182 if (CLASS_TYPE_P (type))
8183 {
8184 tree field;
8185 tree binfo;
8186 tree base_binfo;
8187 int i;
8188
8189 /* CLASSTYPE_EMPTY_P isn't set properly until the class is actually laid
8190 out, but we'd like to be able to check this before then. */
8191 if (COMPLETE_TYPE_P (type) && is_empty_class (type))
8192 return true;
8193
8194 for (binfo = TYPE_BINFO (type), i = 0;
8195 BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
8196 if (!is_really_empty_class (BINFO_TYPE (base_binfo)))
8197 return false;
8198 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
8199 if (TREE_CODE (field) == FIELD_DECL
8200 && !DECL_ARTIFICIAL (field)
8201 /* An unnamed bit-field is not a data member. */
8202 && (DECL_NAME (field) || !DECL_C_BIT_FIELD (field))
8203 && !is_really_empty_class (TREE_TYPE (field)))
8204 return false;
8205 return true;
8206 }
8207 else if (TREE_CODE (type) == ARRAY_TYPE)
8208 return (integer_zerop (array_type_nelts_top (type))
8209 || is_really_empty_class (TREE_TYPE (type)));
8210 return false;
8211 }
8212
8213 /* Note that NAME was looked up while the current class was being
8214 defined and that the result of that lookup was DECL. */
8215
8216 void
8217 maybe_note_name_used_in_class (tree name, tree decl)
8218 {
8219 splay_tree names_used;
8220
8221 /* If we're not defining a class, there's nothing to do. */
8222 if (!(innermost_scope_kind() == sk_class
8223 && TYPE_BEING_DEFINED (current_class_type)
8224 && !LAMBDA_TYPE_P (current_class_type)))
8225 return;
8226
8227 /* If there's already a binding for this NAME, then we don't have
8228 anything to worry about. */
8229 if (lookup_member (current_class_type, name,
8230 /*protect=*/0, /*want_type=*/false, tf_warning_or_error))
8231 return;
8232
8233 if (!current_class_stack[current_class_depth - 1].names_used)
8234 current_class_stack[current_class_depth - 1].names_used
8235 = splay_tree_new (splay_tree_compare_pointers, 0, 0);
8236 names_used = current_class_stack[current_class_depth - 1].names_used;
8237
8238 splay_tree_insert (names_used,
8239 (splay_tree_key) name,
8240 (splay_tree_value) decl);
8241 }
8242
8243 /* Note that NAME was declared (as DECL) in the current class. Check
8244 to see that the declaration is valid. */
8245
8246 void
8247 note_name_declared_in_class (tree name, tree decl)
8248 {
8249 splay_tree names_used;
8250 splay_tree_node n;
8251
8252 /* Look to see if we ever used this name. */
8253 names_used
8254 = current_class_stack[current_class_depth - 1].names_used;
8255 if (!names_used)
8256 return;
8257 /* The C language allows members to be declared with a type of the same
8258 name, and the C++ standard says this diagnostic is not required. So
8259 allow it in extern "C" blocks unless predantic is specified.
8260 Allow it in all cases if -ms-extensions is specified. */
8261 if ((!pedantic && current_lang_name == lang_name_c)
8262 || flag_ms_extensions)
8263 return;
8264 n = splay_tree_lookup (names_used, (splay_tree_key) name);
8265 if (n)
8266 {
8267 /* [basic.scope.class]
8268
8269 A name N used in a class S shall refer to the same declaration
8270 in its context and when re-evaluated in the completed scope of
8271 S. */
8272 permerror (input_location, "declaration of %q#D", decl);
8273 permerror (location_of ((tree) n->value),
8274 "changes meaning of %qD from %q#D",
8275 OVL_NAME (decl), (tree) n->value);
8276 }
8277 }
8278
8279 /* Returns the VAR_DECL for the complete vtable associated with BINFO.
8280 Secondary vtables are merged with primary vtables; this function
8281 will return the VAR_DECL for the primary vtable. */
8282
8283 tree
8284 get_vtbl_decl_for_binfo (tree binfo)
8285 {
8286 tree decl;
8287
8288 decl = BINFO_VTABLE (binfo);
8289 if (decl && TREE_CODE (decl) == POINTER_PLUS_EXPR)
8290 {
8291 gcc_assert (TREE_CODE (TREE_OPERAND (decl, 0)) == ADDR_EXPR);
8292 decl = TREE_OPERAND (TREE_OPERAND (decl, 0), 0);
8293 }
8294 if (decl)
8295 gcc_assert (VAR_P (decl));
8296 return decl;
8297 }
8298
8299
8300 /* Returns the binfo for the primary base of BINFO. If the resulting
8301 BINFO is a virtual base, and it is inherited elsewhere in the
8302 hierarchy, then the returned binfo might not be the primary base of
8303 BINFO in the complete object. Check BINFO_PRIMARY_P or
8304 BINFO_LOST_PRIMARY_P to be sure. */
8305
8306 static tree
8307 get_primary_binfo (tree binfo)
8308 {
8309 tree primary_base;
8310
8311 primary_base = CLASSTYPE_PRIMARY_BINFO (BINFO_TYPE (binfo));
8312 if (!primary_base)
8313 return NULL_TREE;
8314
8315 return copied_binfo (primary_base, binfo);
8316 }
8317
8318 /* As above, but iterate until we reach the binfo that actually provides the
8319 vptr for BINFO. */
8320
8321 static tree
8322 most_primary_binfo (tree binfo)
8323 {
8324 tree b = binfo;
8325 while (CLASSTYPE_HAS_PRIMARY_BASE_P (BINFO_TYPE (b))
8326 && !BINFO_LOST_PRIMARY_P (b))
8327 {
8328 tree primary_base = get_primary_binfo (b);
8329 gcc_assert (BINFO_PRIMARY_P (primary_base)
8330 && BINFO_INHERITANCE_CHAIN (primary_base) == b);
8331 b = primary_base;
8332 }
8333 return b;
8334 }
8335
8336 /* Returns true if BINFO gets its vptr from a virtual base of the most derived
8337 type. Note that the virtual inheritance might be above or below BINFO in
8338 the hierarchy. */
8339
8340 bool
8341 vptr_via_virtual_p (tree binfo)
8342 {
8343 if (TYPE_P (binfo))
8344 binfo = TYPE_BINFO (binfo);
8345 tree primary = most_primary_binfo (binfo);
8346 /* Don't limit binfo_via_virtual, we want to return true when BINFO itself is
8347 a morally virtual base. */
8348 tree virt = binfo_via_virtual (primary, NULL_TREE);
8349 return virt != NULL_TREE;
8350 }
8351
8352 /* If INDENTED_P is zero, indent to INDENT. Return nonzero. */
8353
8354 static int
8355 maybe_indent_hierarchy (FILE * stream, int indent, int indented_p)
8356 {
8357 if (!indented_p)
8358 fprintf (stream, "%*s", indent, "");
8359 return 1;
8360 }
8361
8362 /* Dump the offsets of all the bases rooted at BINFO to STREAM.
8363 INDENT should be zero when called from the top level; it is
8364 incremented recursively. IGO indicates the next expected BINFO in
8365 inheritance graph ordering. */
8366
8367 static tree
8368 dump_class_hierarchy_r (FILE *stream,
8369 dump_flags_t flags,
8370 tree binfo,
8371 tree igo,
8372 int indent)
8373 {
8374 int indented = 0;
8375 tree base_binfo;
8376 int i;
8377
8378 indented = maybe_indent_hierarchy (stream, indent, 0);
8379 fprintf (stream, "%s (0x" HOST_WIDE_INT_PRINT_HEX ") ",
8380 type_as_string (BINFO_TYPE (binfo), TFF_PLAIN_IDENTIFIER),
8381 (HOST_WIDE_INT) (uintptr_t) binfo);
8382 if (binfo != igo)
8383 {
8384 fprintf (stream, "alternative-path\n");
8385 return igo;
8386 }
8387 igo = TREE_CHAIN (binfo);
8388
8389 fprintf (stream, HOST_WIDE_INT_PRINT_DEC,
8390 tree_to_shwi (BINFO_OFFSET (binfo)));
8391 if (is_empty_class (BINFO_TYPE (binfo)))
8392 fprintf (stream, " empty");
8393 else if (CLASSTYPE_NEARLY_EMPTY_P (BINFO_TYPE (binfo)))
8394 fprintf (stream, " nearly-empty");
8395 if (BINFO_VIRTUAL_P (binfo))
8396 fprintf (stream, " virtual");
8397 fprintf (stream, "\n");
8398
8399 indented = 0;
8400 if (BINFO_PRIMARY_P (binfo))
8401 {
8402 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
8403 fprintf (stream, " primary-for %s (0x" HOST_WIDE_INT_PRINT_HEX ")",
8404 type_as_string (BINFO_TYPE (BINFO_INHERITANCE_CHAIN (binfo)),
8405 TFF_PLAIN_IDENTIFIER),
8406 (HOST_WIDE_INT) (uintptr_t) BINFO_INHERITANCE_CHAIN (binfo));
8407 }
8408 if (BINFO_LOST_PRIMARY_P (binfo))
8409 {
8410 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
8411 fprintf (stream, " lost-primary");
8412 }
8413 if (indented)
8414 fprintf (stream, "\n");
8415
8416 if (!(flags & TDF_SLIM))
8417 {
8418 int indented = 0;
8419
8420 if (BINFO_SUBVTT_INDEX (binfo))
8421 {
8422 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
8423 fprintf (stream, " subvttidx=%s",
8424 expr_as_string (BINFO_SUBVTT_INDEX (binfo),
8425 TFF_PLAIN_IDENTIFIER));
8426 }
8427 if (BINFO_VPTR_INDEX (binfo))
8428 {
8429 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
8430 fprintf (stream, " vptridx=%s",
8431 expr_as_string (BINFO_VPTR_INDEX (binfo),
8432 TFF_PLAIN_IDENTIFIER));
8433 }
8434 if (BINFO_VPTR_FIELD (binfo))
8435 {
8436 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
8437 fprintf (stream, " vbaseoffset=%s",
8438 expr_as_string (BINFO_VPTR_FIELD (binfo),
8439 TFF_PLAIN_IDENTIFIER));
8440 }
8441 if (BINFO_VTABLE (binfo))
8442 {
8443 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
8444 fprintf (stream, " vptr=%s",
8445 expr_as_string (BINFO_VTABLE (binfo),
8446 TFF_PLAIN_IDENTIFIER));
8447 }
8448
8449 if (indented)
8450 fprintf (stream, "\n");
8451 }
8452
8453 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
8454 igo = dump_class_hierarchy_r (stream, flags, base_binfo, igo, indent + 2);
8455
8456 return igo;
8457 }
8458
8459 /* Dump the BINFO hierarchy for T. */
8460
8461 static void
8462 dump_class_hierarchy_1 (FILE *stream, dump_flags_t flags, tree t)
8463 {
8464 fprintf (stream, "Class %s\n", type_as_string (t, TFF_PLAIN_IDENTIFIER));
8465 fprintf (stream, " size=%lu align=%lu\n",
8466 (unsigned long)(tree_to_shwi (TYPE_SIZE (t)) / BITS_PER_UNIT),
8467 (unsigned long)(TYPE_ALIGN (t) / BITS_PER_UNIT));
8468 fprintf (stream, " base size=%lu base align=%lu\n",
8469 (unsigned long)(tree_to_shwi (TYPE_SIZE (CLASSTYPE_AS_BASE (t)))
8470 / BITS_PER_UNIT),
8471 (unsigned long)(TYPE_ALIGN (CLASSTYPE_AS_BASE (t))
8472 / BITS_PER_UNIT));
8473 dump_class_hierarchy_r (stream, flags, TYPE_BINFO (t), TYPE_BINFO (t), 0);
8474 fprintf (stream, "\n");
8475 }
8476
8477 /* Debug interface to hierarchy dumping. */
8478
8479 void
8480 debug_class (tree t)
8481 {
8482 dump_class_hierarchy_1 (stderr, TDF_SLIM, t);
8483 }
8484
8485 static void
8486 dump_class_hierarchy (tree t)
8487 {
8488 dump_flags_t flags;
8489 if (FILE *stream = dump_begin (class_dump_id, &flags))
8490 {
8491 dump_class_hierarchy_1 (stream, flags, t);
8492 dump_end (class_dump_id, stream);
8493 }
8494 }
8495
8496 static void
8497 dump_array (FILE * stream, tree decl)
8498 {
8499 tree value;
8500 unsigned HOST_WIDE_INT ix;
8501 HOST_WIDE_INT elt;
8502 tree size = TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (decl)));
8503
8504 elt = (tree_to_shwi (TYPE_SIZE (TREE_TYPE (TREE_TYPE (decl))))
8505 / BITS_PER_UNIT);
8506 fprintf (stream, "%s:", decl_as_string (decl, TFF_PLAIN_IDENTIFIER));
8507 fprintf (stream, " %s entries",
8508 expr_as_string (size_binop (PLUS_EXPR, size, size_one_node),
8509 TFF_PLAIN_IDENTIFIER));
8510 fprintf (stream, "\n");
8511
8512 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (DECL_INITIAL (decl)),
8513 ix, value)
8514 fprintf (stream, "%-4ld %s\n", (long)(ix * elt),
8515 expr_as_string (value, TFF_PLAIN_IDENTIFIER));
8516 }
8517
8518 static void
8519 dump_vtable (tree t, tree binfo, tree vtable)
8520 {
8521 dump_flags_t flags;
8522 FILE *stream = dump_begin (class_dump_id, &flags);
8523
8524 if (!stream)
8525 return;
8526
8527 if (!(flags & TDF_SLIM))
8528 {
8529 int ctor_vtbl_p = TYPE_BINFO (t) != binfo;
8530
8531 fprintf (stream, "%s for %s",
8532 ctor_vtbl_p ? "Construction vtable" : "Vtable",
8533 type_as_string (BINFO_TYPE (binfo), TFF_PLAIN_IDENTIFIER));
8534 if (ctor_vtbl_p)
8535 {
8536 if (!BINFO_VIRTUAL_P (binfo))
8537 fprintf (stream, " (0x" HOST_WIDE_INT_PRINT_HEX " instance)",
8538 (HOST_WIDE_INT) (uintptr_t) binfo);
8539 fprintf (stream, " in %s", type_as_string (t, TFF_PLAIN_IDENTIFIER));
8540 }
8541 fprintf (stream, "\n");
8542 dump_array (stream, vtable);
8543 fprintf (stream, "\n");
8544 }
8545
8546 dump_end (class_dump_id, stream);
8547 }
8548
8549 static void
8550 dump_vtt (tree t, tree vtt)
8551 {
8552 dump_flags_t flags;
8553 FILE *stream = dump_begin (class_dump_id, &flags);
8554
8555 if (!stream)
8556 return;
8557
8558 if (!(flags & TDF_SLIM))
8559 {
8560 fprintf (stream, "VTT for %s\n",
8561 type_as_string (t, TFF_PLAIN_IDENTIFIER));
8562 dump_array (stream, vtt);
8563 fprintf (stream, "\n");
8564 }
8565
8566 dump_end (class_dump_id, stream);
8567 }
8568
8569 /* Dump a function or thunk and its thunkees. */
8570
8571 static void
8572 dump_thunk (FILE *stream, int indent, tree thunk)
8573 {
8574 static const char spaces[] = " ";
8575 tree name = DECL_NAME (thunk);
8576 tree thunks;
8577
8578 fprintf (stream, "%.*s%p %s %s", indent, spaces,
8579 (void *)thunk,
8580 !DECL_THUNK_P (thunk) ? "function"
8581 : DECL_THIS_THUNK_P (thunk) ? "this-thunk" : "covariant-thunk",
8582 name ? IDENTIFIER_POINTER (name) : "<unset>");
8583 if (DECL_THUNK_P (thunk))
8584 {
8585 HOST_WIDE_INT fixed_adjust = THUNK_FIXED_OFFSET (thunk);
8586 tree virtual_adjust = THUNK_VIRTUAL_OFFSET (thunk);
8587
8588 fprintf (stream, " fixed=" HOST_WIDE_INT_PRINT_DEC, fixed_adjust);
8589 if (!virtual_adjust)
8590 /*NOP*/;
8591 else if (DECL_THIS_THUNK_P (thunk))
8592 fprintf (stream, " vcall=" HOST_WIDE_INT_PRINT_DEC,
8593 tree_to_shwi (virtual_adjust));
8594 else
8595 fprintf (stream, " vbase=" HOST_WIDE_INT_PRINT_DEC "(%s)",
8596 tree_to_shwi (BINFO_VPTR_FIELD (virtual_adjust)),
8597 type_as_string (BINFO_TYPE (virtual_adjust), TFF_SCOPE));
8598 if (THUNK_ALIAS (thunk))
8599 fprintf (stream, " alias to %p", (void *)THUNK_ALIAS (thunk));
8600 }
8601 fprintf (stream, "\n");
8602 for (thunks = DECL_THUNKS (thunk); thunks; thunks = TREE_CHAIN (thunks))
8603 dump_thunk (stream, indent + 2, thunks);
8604 }
8605
8606 /* Dump the thunks for FN. */
8607
8608 void
8609 debug_thunks (tree fn)
8610 {
8611 dump_thunk (stderr, 0, fn);
8612 }
8613
8614 /* Virtual function table initialization. */
8615
8616 /* Create all the necessary vtables for T and its base classes. */
8617
8618 static void
8619 finish_vtbls (tree t)
8620 {
8621 tree vbase;
8622 vec<constructor_elt, va_gc> *v = NULL;
8623 tree vtable = BINFO_VTABLE (TYPE_BINFO (t));
8624
8625 /* We lay out the primary and secondary vtables in one contiguous
8626 vtable. The primary vtable is first, followed by the non-virtual
8627 secondary vtables in inheritance graph order. */
8628 accumulate_vtbl_inits (TYPE_BINFO (t), TYPE_BINFO (t), TYPE_BINFO (t),
8629 vtable, t, &v);
8630
8631 /* Then come the virtual bases, also in inheritance graph order. */
8632 for (vbase = TYPE_BINFO (t); vbase; vbase = TREE_CHAIN (vbase))
8633 {
8634 if (!BINFO_VIRTUAL_P (vbase))
8635 continue;
8636 accumulate_vtbl_inits (vbase, vbase, TYPE_BINFO (t), vtable, t, &v);
8637 }
8638
8639 if (BINFO_VTABLE (TYPE_BINFO (t)))
8640 initialize_vtable (TYPE_BINFO (t), v);
8641 }
8642
8643 /* Initialize the vtable for BINFO with the INITS. */
8644
8645 static void
8646 initialize_vtable (tree binfo, vec<constructor_elt, va_gc> *inits)
8647 {
8648 tree decl;
8649
8650 layout_vtable_decl (binfo, vec_safe_length (inits));
8651 decl = get_vtbl_decl_for_binfo (binfo);
8652 initialize_artificial_var (decl, inits);
8653 dump_vtable (BINFO_TYPE (binfo), binfo, decl);
8654 }
8655
8656 /* Build the VTT (virtual table table) for T.
8657 A class requires a VTT if it has virtual bases.
8658
8659 This holds
8660 1 - primary virtual pointer for complete object T
8661 2 - secondary VTTs for each direct non-virtual base of T which requires a
8662 VTT
8663 3 - secondary virtual pointers for each direct or indirect base of T which
8664 has virtual bases or is reachable via a virtual path from T.
8665 4 - secondary VTTs for each direct or indirect virtual base of T.
8666
8667 Secondary VTTs look like complete object VTTs without part 4. */
8668
8669 static void
8670 build_vtt (tree t)
8671 {
8672 tree type;
8673 tree vtt;
8674 tree index;
8675 vec<constructor_elt, va_gc> *inits;
8676
8677 /* Build up the initializers for the VTT. */
8678 inits = NULL;
8679 index = size_zero_node;
8680 build_vtt_inits (TYPE_BINFO (t), t, &inits, &index);
8681
8682 /* If we didn't need a VTT, we're done. */
8683 if (!inits)
8684 return;
8685
8686 /* Figure out the type of the VTT. */
8687 type = build_array_of_n_type (const_ptr_type_node,
8688 inits->length ());
8689
8690 /* Now, build the VTT object itself. */
8691 vtt = build_vtable (t, mangle_vtt_for_type (t), type);
8692 initialize_artificial_var (vtt, inits);
8693 /* Add the VTT to the vtables list. */
8694 DECL_CHAIN (vtt) = DECL_CHAIN (CLASSTYPE_VTABLES (t));
8695 DECL_CHAIN (CLASSTYPE_VTABLES (t)) = vtt;
8696
8697 dump_vtt (t, vtt);
8698 }
8699
8700 /* When building a secondary VTT, BINFO_VTABLE is set to a TREE_LIST with
8701 PURPOSE the RTTI_BINFO, VALUE the real vtable pointer for this binfo,
8702 and CHAIN the vtable pointer for this binfo after construction is
8703 complete. VALUE can also be another BINFO, in which case we recurse. */
8704
8705 static tree
8706 binfo_ctor_vtable (tree binfo)
8707 {
8708 tree vt;
8709
8710 while (1)
8711 {
8712 vt = BINFO_VTABLE (binfo);
8713 if (TREE_CODE (vt) == TREE_LIST)
8714 vt = TREE_VALUE (vt);
8715 if (TREE_CODE (vt) == TREE_BINFO)
8716 binfo = vt;
8717 else
8718 break;
8719 }
8720
8721 return vt;
8722 }
8723
8724 /* Data for secondary VTT initialization. */
8725 struct secondary_vptr_vtt_init_data
8726 {
8727 /* Is this the primary VTT? */
8728 bool top_level_p;
8729
8730 /* Current index into the VTT. */
8731 tree index;
8732
8733 /* Vector of initializers built up. */
8734 vec<constructor_elt, va_gc> *inits;
8735
8736 /* The type being constructed by this secondary VTT. */
8737 tree type_being_constructed;
8738 };
8739
8740 /* Recursively build the VTT-initializer for BINFO (which is in the
8741 hierarchy dominated by T). INITS points to the end of the initializer
8742 list to date. INDEX is the VTT index where the next element will be
8743 replaced. Iff BINFO is the binfo for T, this is the top level VTT (i.e.
8744 not a subvtt for some base of T). When that is so, we emit the sub-VTTs
8745 for virtual bases of T. When it is not so, we build the constructor
8746 vtables for the BINFO-in-T variant. */
8747
8748 static void
8749 build_vtt_inits (tree binfo, tree t, vec<constructor_elt, va_gc> **inits,
8750 tree *index)
8751 {
8752 int i;
8753 tree b;
8754 tree init;
8755 secondary_vptr_vtt_init_data data;
8756 int top_level_p = SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), t);
8757
8758 /* We only need VTTs for subobjects with virtual bases. */
8759 if (!CLASSTYPE_VBASECLASSES (BINFO_TYPE (binfo)))
8760 return;
8761
8762 /* We need to use a construction vtable if this is not the primary
8763 VTT. */
8764 if (!top_level_p)
8765 {
8766 build_ctor_vtbl_group (binfo, t);
8767
8768 /* Record the offset in the VTT where this sub-VTT can be found. */
8769 BINFO_SUBVTT_INDEX (binfo) = *index;
8770 }
8771
8772 /* Add the address of the primary vtable for the complete object. */
8773 init = binfo_ctor_vtable (binfo);
8774 CONSTRUCTOR_APPEND_ELT (*inits, NULL_TREE, init);
8775 if (top_level_p)
8776 {
8777 gcc_assert (!BINFO_VPTR_INDEX (binfo));
8778 BINFO_VPTR_INDEX (binfo) = *index;
8779 }
8780 *index = size_binop (PLUS_EXPR, *index, TYPE_SIZE_UNIT (ptr_type_node));
8781
8782 /* Recursively add the secondary VTTs for non-virtual bases. */
8783 for (i = 0; BINFO_BASE_ITERATE (binfo, i, b); ++i)
8784 if (!BINFO_VIRTUAL_P (b))
8785 build_vtt_inits (b, t, inits, index);
8786
8787 /* Add secondary virtual pointers for all subobjects of BINFO with
8788 either virtual bases or reachable along a virtual path, except
8789 subobjects that are non-virtual primary bases. */
8790 data.top_level_p = top_level_p;
8791 data.index = *index;
8792 data.inits = *inits;
8793 data.type_being_constructed = BINFO_TYPE (binfo);
8794
8795 dfs_walk_once (binfo, dfs_build_secondary_vptr_vtt_inits, NULL, &data);
8796
8797 *index = data.index;
8798
8799 /* data.inits might have grown as we added secondary virtual pointers.
8800 Make sure our caller knows about the new vector. */
8801 *inits = data.inits;
8802
8803 if (top_level_p)
8804 /* Add the secondary VTTs for virtual bases in inheritance graph
8805 order. */
8806 for (b = TYPE_BINFO (BINFO_TYPE (binfo)); b; b = TREE_CHAIN (b))
8807 {
8808 if (!BINFO_VIRTUAL_P (b))
8809 continue;
8810
8811 build_vtt_inits (b, t, inits, index);
8812 }
8813 else
8814 /* Remove the ctor vtables we created. */
8815 dfs_walk_all (binfo, dfs_fixup_binfo_vtbls, NULL, binfo);
8816 }
8817
8818 /* Called from build_vtt_inits via dfs_walk. BINFO is the binfo for the base
8819 in most derived. DATA is a SECONDARY_VPTR_VTT_INIT_DATA structure. */
8820
8821 static tree
8822 dfs_build_secondary_vptr_vtt_inits (tree binfo, void *data_)
8823 {
8824 secondary_vptr_vtt_init_data *data = (secondary_vptr_vtt_init_data *)data_;
8825
8826 /* We don't care about bases that don't have vtables. */
8827 if (!TYPE_VFIELD (BINFO_TYPE (binfo)))
8828 return dfs_skip_bases;
8829
8830 /* We're only interested in proper subobjects of the type being
8831 constructed. */
8832 if (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), data->type_being_constructed))
8833 return NULL_TREE;
8834
8835 /* We're only interested in bases with virtual bases or reachable
8836 via a virtual path from the type being constructed. */
8837 if (!(CLASSTYPE_VBASECLASSES (BINFO_TYPE (binfo))
8838 || binfo_via_virtual (binfo, data->type_being_constructed)))
8839 return dfs_skip_bases;
8840
8841 /* We're not interested in non-virtual primary bases. */
8842 if (!BINFO_VIRTUAL_P (binfo) && BINFO_PRIMARY_P (binfo))
8843 return NULL_TREE;
8844
8845 /* Record the index where this secondary vptr can be found. */
8846 if (data->top_level_p)
8847 {
8848 gcc_assert (!BINFO_VPTR_INDEX (binfo));
8849 BINFO_VPTR_INDEX (binfo) = data->index;
8850
8851 if (BINFO_VIRTUAL_P (binfo))
8852 {
8853 /* It's a primary virtual base, and this is not a
8854 construction vtable. Find the base this is primary of in
8855 the inheritance graph, and use that base's vtable
8856 now. */
8857 while (BINFO_PRIMARY_P (binfo))
8858 binfo = BINFO_INHERITANCE_CHAIN (binfo);
8859 }
8860 }
8861
8862 /* Add the initializer for the secondary vptr itself. */
8863 CONSTRUCTOR_APPEND_ELT (data->inits, NULL_TREE, binfo_ctor_vtable (binfo));
8864
8865 /* Advance the vtt index. */
8866 data->index = size_binop (PLUS_EXPR, data->index,
8867 TYPE_SIZE_UNIT (ptr_type_node));
8868
8869 return NULL_TREE;
8870 }
8871
8872 /* Called from build_vtt_inits via dfs_walk. After building
8873 constructor vtables and generating the sub-vtt from them, we need
8874 to restore the BINFO_VTABLES that were scribbled on. DATA is the
8875 binfo of the base whose sub vtt was generated. */
8876
8877 static tree
8878 dfs_fixup_binfo_vtbls (tree binfo, void* data)
8879 {
8880 tree vtable = BINFO_VTABLE (binfo);
8881
8882 if (!TYPE_CONTAINS_VPTR_P (BINFO_TYPE (binfo)))
8883 /* If this class has no vtable, none of its bases do. */
8884 return dfs_skip_bases;
8885
8886 if (!vtable)
8887 /* This might be a primary base, so have no vtable in this
8888 hierarchy. */
8889 return NULL_TREE;
8890
8891 /* If we scribbled the construction vtable vptr into BINFO, clear it
8892 out now. */
8893 if (TREE_CODE (vtable) == TREE_LIST
8894 && (TREE_PURPOSE (vtable) == (tree) data))
8895 BINFO_VTABLE (binfo) = TREE_CHAIN (vtable);
8896
8897 return NULL_TREE;
8898 }
8899
8900 /* Build the construction vtable group for BINFO which is in the
8901 hierarchy dominated by T. */
8902
8903 static void
8904 build_ctor_vtbl_group (tree binfo, tree t)
8905 {
8906 tree type;
8907 tree vtbl;
8908 tree id;
8909 tree vbase;
8910 vec<constructor_elt, va_gc> *v;
8911
8912 /* See if we've already created this construction vtable group. */
8913 id = mangle_ctor_vtbl_for_type (t, binfo);
8914 if (get_global_binding (id))
8915 return;
8916
8917 gcc_assert (!SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), t));
8918 /* Build a version of VTBL (with the wrong type) for use in
8919 constructing the addresses of secondary vtables in the
8920 construction vtable group. */
8921 vtbl = build_vtable (t, id, ptr_type_node);
8922 DECL_CONSTRUCTION_VTABLE_P (vtbl) = 1;
8923 /* Don't export construction vtables from shared libraries. Even on
8924 targets that don't support hidden visibility, this tells
8925 can_refer_decl_in_current_unit_p not to assume that it's safe to
8926 access from a different compilation unit (bz 54314). */
8927 DECL_VISIBILITY (vtbl) = VISIBILITY_HIDDEN;
8928 DECL_VISIBILITY_SPECIFIED (vtbl) = true;
8929
8930 v = NULL;
8931 accumulate_vtbl_inits (binfo, TYPE_BINFO (TREE_TYPE (binfo)),
8932 binfo, vtbl, t, &v);
8933
8934 /* Add the vtables for each of our virtual bases using the vbase in T
8935 binfo. */
8936 for (vbase = TYPE_BINFO (BINFO_TYPE (binfo));
8937 vbase;
8938 vbase = TREE_CHAIN (vbase))
8939 {
8940 tree b;
8941
8942 if (!BINFO_VIRTUAL_P (vbase))
8943 continue;
8944 b = copied_binfo (vbase, binfo);
8945
8946 accumulate_vtbl_inits (b, vbase, binfo, vtbl, t, &v);
8947 }
8948
8949 /* Figure out the type of the construction vtable. */
8950 type = build_array_of_n_type (vtable_entry_type, v->length ());
8951 layout_type (type);
8952 TREE_TYPE (vtbl) = type;
8953 DECL_SIZE (vtbl) = DECL_SIZE_UNIT (vtbl) = NULL_TREE;
8954 layout_decl (vtbl, 0);
8955
8956 /* Initialize the construction vtable. */
8957 CLASSTYPE_VTABLES (t) = chainon (CLASSTYPE_VTABLES (t), vtbl);
8958 initialize_artificial_var (vtbl, v);
8959 dump_vtable (t, binfo, vtbl);
8960 }
8961
8962 /* Add the vtbl initializers for BINFO (and its bases other than
8963 non-virtual primaries) to the list of INITS. BINFO is in the
8964 hierarchy dominated by T. RTTI_BINFO is the binfo within T of
8965 the constructor the vtbl inits should be accumulated for. (If this
8966 is the complete object vtbl then RTTI_BINFO will be TYPE_BINFO (T).)
8967 ORIG_BINFO is the binfo for this object within BINFO_TYPE (RTTI_BINFO).
8968 BINFO is the active base equivalent of ORIG_BINFO in the inheritance
8969 graph of T. Both BINFO and ORIG_BINFO will have the same BINFO_TYPE,
8970 but are not necessarily the same in terms of layout. */
8971
8972 static void
8973 accumulate_vtbl_inits (tree binfo,
8974 tree orig_binfo,
8975 tree rtti_binfo,
8976 tree vtbl,
8977 tree t,
8978 vec<constructor_elt, va_gc> **inits)
8979 {
8980 int i;
8981 tree base_binfo;
8982 int ctor_vtbl_p = !SAME_BINFO_TYPE_P (BINFO_TYPE (rtti_binfo), t);
8983
8984 gcc_assert (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), BINFO_TYPE (orig_binfo)));
8985
8986 /* If it doesn't have a vptr, we don't do anything. */
8987 if (!TYPE_CONTAINS_VPTR_P (BINFO_TYPE (binfo)))
8988 return;
8989
8990 /* If we're building a construction vtable, we're not interested in
8991 subobjects that don't require construction vtables. */
8992 if (ctor_vtbl_p
8993 && !CLASSTYPE_VBASECLASSES (BINFO_TYPE (binfo))
8994 && !binfo_via_virtual (orig_binfo, BINFO_TYPE (rtti_binfo)))
8995 return;
8996
8997 /* Build the initializers for the BINFO-in-T vtable. */
8998 dfs_accumulate_vtbl_inits (binfo, orig_binfo, rtti_binfo, vtbl, t, inits);
8999
9000 /* Walk the BINFO and its bases. We walk in preorder so that as we
9001 initialize each vtable we can figure out at what offset the
9002 secondary vtable lies from the primary vtable. We can't use
9003 dfs_walk here because we need to iterate through bases of BINFO
9004 and RTTI_BINFO simultaneously. */
9005 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
9006 {
9007 /* Skip virtual bases. */
9008 if (BINFO_VIRTUAL_P (base_binfo))
9009 continue;
9010 accumulate_vtbl_inits (base_binfo,
9011 BINFO_BASE_BINFO (orig_binfo, i),
9012 rtti_binfo, vtbl, t,
9013 inits);
9014 }
9015 }
9016
9017 /* Called from accumulate_vtbl_inits. Adds the initializers for the
9018 BINFO vtable to L. */
9019
9020 static void
9021 dfs_accumulate_vtbl_inits (tree binfo,
9022 tree orig_binfo,
9023 tree rtti_binfo,
9024 tree orig_vtbl,
9025 tree t,
9026 vec<constructor_elt, va_gc> **l)
9027 {
9028 tree vtbl = NULL_TREE;
9029 int ctor_vtbl_p = !SAME_BINFO_TYPE_P (BINFO_TYPE (rtti_binfo), t);
9030 int n_inits;
9031
9032 if (ctor_vtbl_p
9033 && BINFO_VIRTUAL_P (orig_binfo) && BINFO_PRIMARY_P (orig_binfo))
9034 {
9035 /* In the hierarchy of BINFO_TYPE (RTTI_BINFO), this is a
9036 primary virtual base. If it is not the same primary in
9037 the hierarchy of T, we'll need to generate a ctor vtable
9038 for it, to place at its location in T. If it is the same
9039 primary, we still need a VTT entry for the vtable, but it
9040 should point to the ctor vtable for the base it is a
9041 primary for within the sub-hierarchy of RTTI_BINFO.
9042
9043 There are three possible cases:
9044
9045 1) We are in the same place.
9046 2) We are a primary base within a lost primary virtual base of
9047 RTTI_BINFO.
9048 3) We are primary to something not a base of RTTI_BINFO. */
9049
9050 tree b;
9051 tree last = NULL_TREE;
9052
9053 /* First, look through the bases we are primary to for RTTI_BINFO
9054 or a virtual base. */
9055 b = binfo;
9056 while (BINFO_PRIMARY_P (b))
9057 {
9058 b = BINFO_INHERITANCE_CHAIN (b);
9059 last = b;
9060 if (BINFO_VIRTUAL_P (b) || b == rtti_binfo)
9061 goto found;
9062 }
9063 /* If we run out of primary links, keep looking down our
9064 inheritance chain; we might be an indirect primary. */
9065 for (b = last; b; b = BINFO_INHERITANCE_CHAIN (b))
9066 if (BINFO_VIRTUAL_P (b) || b == rtti_binfo)
9067 break;
9068 found:
9069
9070 /* If we found RTTI_BINFO, this is case 1. If we found a virtual
9071 base B and it is a base of RTTI_BINFO, this is case 2. In
9072 either case, we share our vtable with LAST, i.e. the
9073 derived-most base within B of which we are a primary. */
9074 if (b == rtti_binfo
9075 || (b && binfo_for_vbase (BINFO_TYPE (b), BINFO_TYPE (rtti_binfo))))
9076 /* Just set our BINFO_VTABLE to point to LAST, as we may not have
9077 set LAST's BINFO_VTABLE yet. We'll extract the actual vptr in
9078 binfo_ctor_vtable after everything's been set up. */
9079 vtbl = last;
9080
9081 /* Otherwise, this is case 3 and we get our own. */
9082 }
9083 else if (!BINFO_NEW_VTABLE_MARKED (orig_binfo))
9084 return;
9085
9086 n_inits = vec_safe_length (*l);
9087
9088 if (!vtbl)
9089 {
9090 tree index;
9091 int non_fn_entries;
9092
9093 /* Add the initializer for this vtable. */
9094 build_vtbl_initializer (binfo, orig_binfo, t, rtti_binfo,
9095 &non_fn_entries, l);
9096
9097 /* Figure out the position to which the VPTR should point. */
9098 vtbl = build1 (ADDR_EXPR, vtbl_ptr_type_node, orig_vtbl);
9099 index = size_binop (MULT_EXPR,
9100 TYPE_SIZE_UNIT (vtable_entry_type),
9101 size_int (non_fn_entries + n_inits));
9102 vtbl = fold_build_pointer_plus (vtbl, index);
9103 }
9104
9105 if (ctor_vtbl_p)
9106 /* For a construction vtable, we can't overwrite BINFO_VTABLE.
9107 So, we make a TREE_LIST. Later, dfs_fixup_binfo_vtbls will
9108 straighten this out. */
9109 BINFO_VTABLE (binfo) = tree_cons (rtti_binfo, vtbl, BINFO_VTABLE (binfo));
9110 else if (BINFO_PRIMARY_P (binfo) && BINFO_VIRTUAL_P (binfo))
9111 /* Throw away any unneeded intializers. */
9112 (*l)->truncate (n_inits);
9113 else
9114 /* For an ordinary vtable, set BINFO_VTABLE. */
9115 BINFO_VTABLE (binfo) = vtbl;
9116 }
9117
9118 static GTY(()) tree abort_fndecl_addr;
9119 static GTY(()) tree dvirt_fn;
9120
9121 /* Construct the initializer for BINFO's virtual function table. BINFO
9122 is part of the hierarchy dominated by T. If we're building a
9123 construction vtable, the ORIG_BINFO is the binfo we should use to
9124 find the actual function pointers to put in the vtable - but they
9125 can be overridden on the path to most-derived in the graph that
9126 ORIG_BINFO belongs. Otherwise,
9127 ORIG_BINFO should be the same as BINFO. The RTTI_BINFO is the
9128 BINFO that should be indicated by the RTTI information in the
9129 vtable; it will be a base class of T, rather than T itself, if we
9130 are building a construction vtable.
9131
9132 The value returned is a TREE_LIST suitable for wrapping in a
9133 CONSTRUCTOR to use as the DECL_INITIAL for a vtable. If
9134 NON_FN_ENTRIES_P is not NULL, *NON_FN_ENTRIES_P is set to the
9135 number of non-function entries in the vtable.
9136
9137 It might seem that this function should never be called with a
9138 BINFO for which BINFO_PRIMARY_P holds, the vtable for such a
9139 base is always subsumed by a derived class vtable. However, when
9140 we are building construction vtables, we do build vtables for
9141 primary bases; we need these while the primary base is being
9142 constructed. */
9143
9144 static void
9145 build_vtbl_initializer (tree binfo,
9146 tree orig_binfo,
9147 tree t,
9148 tree rtti_binfo,
9149 int* non_fn_entries_p,
9150 vec<constructor_elt, va_gc> **inits)
9151 {
9152 tree v;
9153 vtbl_init_data vid;
9154 unsigned ix, jx;
9155 tree vbinfo;
9156 vec<tree, va_gc> *vbases;
9157 constructor_elt *e;
9158
9159 /* Initialize VID. */
9160 memset (&vid, 0, sizeof (vid));
9161 vid.binfo = binfo;
9162 vid.derived = t;
9163 vid.rtti_binfo = rtti_binfo;
9164 vid.primary_vtbl_p = SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), t);
9165 vid.ctor_vtbl_p = !SAME_BINFO_TYPE_P (BINFO_TYPE (rtti_binfo), t);
9166 vid.generate_vcall_entries = true;
9167 /* The first vbase or vcall offset is at index -3 in the vtable. */
9168 vid.index = ssize_int(-3 * TARGET_VTABLE_DATA_ENTRY_DISTANCE);
9169
9170 /* Add entries to the vtable for RTTI. */
9171 build_rtti_vtbl_entries (binfo, &vid);
9172
9173 /* Create an array for keeping track of the functions we've
9174 processed. When we see multiple functions with the same
9175 signature, we share the vcall offsets. */
9176 vec_alloc (vid.fns, 32);
9177 /* Add the vcall and vbase offset entries. */
9178 build_vcall_and_vbase_vtbl_entries (binfo, &vid);
9179
9180 /* Clear BINFO_VTABLE_PATH_MARKED; it's set by
9181 build_vbase_offset_vtbl_entries. */
9182 for (vbases = CLASSTYPE_VBASECLASSES (t), ix = 0;
9183 vec_safe_iterate (vbases, ix, &vbinfo); ix++)
9184 BINFO_VTABLE_PATH_MARKED (vbinfo) = 0;
9185
9186 /* If the target requires padding between data entries, add that now. */
9187 if (TARGET_VTABLE_DATA_ENTRY_DISTANCE > 1)
9188 {
9189 int n_entries = vec_safe_length (vid.inits);
9190
9191 vec_safe_grow (vid.inits, TARGET_VTABLE_DATA_ENTRY_DISTANCE * n_entries);
9192
9193 /* Move data entries into their new positions and add padding
9194 after the new positions. Iterate backwards so we don't
9195 overwrite entries that we would need to process later. */
9196 for (ix = n_entries - 1;
9197 vid.inits->iterate (ix, &e);
9198 ix--)
9199 {
9200 int j;
9201 int new_position = (TARGET_VTABLE_DATA_ENTRY_DISTANCE * ix
9202 + (TARGET_VTABLE_DATA_ENTRY_DISTANCE - 1));
9203
9204 (*vid.inits)[new_position] = *e;
9205
9206 for (j = 1; j < TARGET_VTABLE_DATA_ENTRY_DISTANCE; ++j)
9207 {
9208 constructor_elt *f = &(*vid.inits)[new_position - j];
9209 f->index = NULL_TREE;
9210 f->value = build1 (NOP_EXPR, vtable_entry_type,
9211 null_pointer_node);
9212 }
9213 }
9214 }
9215
9216 if (non_fn_entries_p)
9217 *non_fn_entries_p = vec_safe_length (vid.inits);
9218
9219 /* The initializers for virtual functions were built up in reverse
9220 order. Straighten them out and add them to the running list in one
9221 step. */
9222 jx = vec_safe_length (*inits);
9223 vec_safe_grow (*inits, jx + vid.inits->length ());
9224
9225 for (ix = vid.inits->length () - 1;
9226 vid.inits->iterate (ix, &e);
9227 ix--, jx++)
9228 (**inits)[jx] = *e;
9229
9230 /* Go through all the ordinary virtual functions, building up
9231 initializers. */
9232 for (v = BINFO_VIRTUALS (orig_binfo); v; v = TREE_CHAIN (v))
9233 {
9234 tree delta;
9235 tree vcall_index;
9236 tree fn, fn_original;
9237 tree init = NULL_TREE;
9238
9239 fn = BV_FN (v);
9240 fn_original = fn;
9241 if (DECL_THUNK_P (fn))
9242 {
9243 if (!DECL_NAME (fn))
9244 finish_thunk (fn);
9245 if (THUNK_ALIAS (fn))
9246 {
9247 fn = THUNK_ALIAS (fn);
9248 BV_FN (v) = fn;
9249 }
9250 fn_original = THUNK_TARGET (fn);
9251 }
9252
9253 /* If the only definition of this function signature along our
9254 primary base chain is from a lost primary, this vtable slot will
9255 never be used, so just zero it out. This is important to avoid
9256 requiring extra thunks which cannot be generated with the function.
9257
9258 We first check this in update_vtable_entry_for_fn, so we handle
9259 restored primary bases properly; we also need to do it here so we
9260 zero out unused slots in ctor vtables, rather than filling them
9261 with erroneous values (though harmless, apart from relocation
9262 costs). */
9263 if (BV_LOST_PRIMARY (v))
9264 init = size_zero_node;
9265
9266 if (! init)
9267 {
9268 /* Pull the offset for `this', and the function to call, out of
9269 the list. */
9270 delta = BV_DELTA (v);
9271 vcall_index = BV_VCALL_INDEX (v);
9272
9273 gcc_assert (TREE_CODE (delta) == INTEGER_CST);
9274 gcc_assert (TREE_CODE (fn) == FUNCTION_DECL);
9275
9276 /* You can't call an abstract virtual function; it's abstract.
9277 So, we replace these functions with __pure_virtual. */
9278 if (DECL_PURE_VIRTUAL_P (fn_original))
9279 {
9280 fn = abort_fndecl;
9281 if (!TARGET_VTABLE_USES_DESCRIPTORS)
9282 {
9283 if (abort_fndecl_addr == NULL)
9284 abort_fndecl_addr
9285 = fold_convert (vfunc_ptr_type_node,
9286 build_fold_addr_expr (fn));
9287 init = abort_fndecl_addr;
9288 }
9289 }
9290 /* Likewise for deleted virtuals. */
9291 else if (DECL_DELETED_FN (fn_original))
9292 {
9293 if (!dvirt_fn)
9294 {
9295 tree name = get_identifier ("__cxa_deleted_virtual");
9296 dvirt_fn = get_global_binding (name);
9297 if (!dvirt_fn)
9298 dvirt_fn = push_library_fn
9299 (name,
9300 build_function_type_list (void_type_node, NULL_TREE),
9301 NULL_TREE, ECF_NORETURN | ECF_COLD);
9302 }
9303 fn = dvirt_fn;
9304 if (!TARGET_VTABLE_USES_DESCRIPTORS)
9305 init = fold_convert (vfunc_ptr_type_node,
9306 build_fold_addr_expr (fn));
9307 }
9308 else
9309 {
9310 if (!integer_zerop (delta) || vcall_index)
9311 {
9312 fn = make_thunk (fn, /*this_adjusting=*/1,
9313 delta, vcall_index);
9314 if (!DECL_NAME (fn))
9315 finish_thunk (fn);
9316 }
9317 /* Take the address of the function, considering it to be of an
9318 appropriate generic type. */
9319 if (!TARGET_VTABLE_USES_DESCRIPTORS)
9320 init = fold_convert (vfunc_ptr_type_node,
9321 build_fold_addr_expr (fn));
9322 /* Don't refer to a virtual destructor from a constructor
9323 vtable or a vtable for an abstract class, since destroying
9324 an object under construction is undefined behavior and we
9325 don't want it to be considered a candidate for speculative
9326 devirtualization. But do create the thunk for ABI
9327 compliance. */
9328 if (DECL_DESTRUCTOR_P (fn_original)
9329 && (CLASSTYPE_PURE_VIRTUALS (DECL_CONTEXT (fn_original))
9330 || orig_binfo != binfo))
9331 init = size_zero_node;
9332 }
9333 }
9334
9335 /* And add it to the chain of initializers. */
9336 if (TARGET_VTABLE_USES_DESCRIPTORS)
9337 {
9338 int i;
9339 if (init == size_zero_node)
9340 for (i = 0; i < TARGET_VTABLE_USES_DESCRIPTORS; ++i)
9341 CONSTRUCTOR_APPEND_ELT (*inits, NULL_TREE, init);
9342 else
9343 for (i = 0; i < TARGET_VTABLE_USES_DESCRIPTORS; ++i)
9344 {
9345 tree fdesc = build2 (FDESC_EXPR, vfunc_ptr_type_node,
9346 fn, build_int_cst (NULL_TREE, i));
9347 TREE_CONSTANT (fdesc) = 1;
9348
9349 CONSTRUCTOR_APPEND_ELT (*inits, NULL_TREE, fdesc);
9350 }
9351 }
9352 else
9353 CONSTRUCTOR_APPEND_ELT (*inits, NULL_TREE, init);
9354 }
9355 }
9356
9357 /* Adds to vid->inits the initializers for the vbase and vcall
9358 offsets in BINFO, which is in the hierarchy dominated by T. */
9359
9360 static void
9361 build_vcall_and_vbase_vtbl_entries (tree binfo, vtbl_init_data* vid)
9362 {
9363 tree b;
9364
9365 /* If this is a derived class, we must first create entries
9366 corresponding to the primary base class. */
9367 b = get_primary_binfo (binfo);
9368 if (b)
9369 build_vcall_and_vbase_vtbl_entries (b, vid);
9370
9371 /* Add the vbase entries for this base. */
9372 build_vbase_offset_vtbl_entries (binfo, vid);
9373 /* Add the vcall entries for this base. */
9374 build_vcall_offset_vtbl_entries (binfo, vid);
9375 }
9376
9377 /* Returns the initializers for the vbase offset entries in the vtable
9378 for BINFO (which is part of the class hierarchy dominated by T), in
9379 reverse order. VBASE_OFFSET_INDEX gives the vtable index
9380 where the next vbase offset will go. */
9381
9382 static void
9383 build_vbase_offset_vtbl_entries (tree binfo, vtbl_init_data* vid)
9384 {
9385 tree vbase;
9386 tree t;
9387 tree non_primary_binfo;
9388
9389 /* If there are no virtual baseclasses, then there is nothing to
9390 do. */
9391 if (!CLASSTYPE_VBASECLASSES (BINFO_TYPE (binfo)))
9392 return;
9393
9394 t = vid->derived;
9395
9396 /* We might be a primary base class. Go up the inheritance hierarchy
9397 until we find the most derived class of which we are a primary base:
9398 it is the offset of that which we need to use. */
9399 non_primary_binfo = binfo;
9400 while (BINFO_INHERITANCE_CHAIN (non_primary_binfo))
9401 {
9402 tree b;
9403
9404 /* If we have reached a virtual base, then it must be a primary
9405 base (possibly multi-level) of vid->binfo, or we wouldn't
9406 have called build_vcall_and_vbase_vtbl_entries for it. But it
9407 might be a lost primary, so just skip down to vid->binfo. */
9408 if (BINFO_VIRTUAL_P (non_primary_binfo))
9409 {
9410 non_primary_binfo = vid->binfo;
9411 break;
9412 }
9413
9414 b = BINFO_INHERITANCE_CHAIN (non_primary_binfo);
9415 if (get_primary_binfo (b) != non_primary_binfo)
9416 break;
9417 non_primary_binfo = b;
9418 }
9419
9420 /* Go through the virtual bases, adding the offsets. */
9421 for (vbase = TYPE_BINFO (BINFO_TYPE (binfo));
9422 vbase;
9423 vbase = TREE_CHAIN (vbase))
9424 {
9425 tree b;
9426 tree delta;
9427
9428 if (!BINFO_VIRTUAL_P (vbase))
9429 continue;
9430
9431 /* Find the instance of this virtual base in the complete
9432 object. */
9433 b = copied_binfo (vbase, binfo);
9434
9435 /* If we've already got an offset for this virtual base, we
9436 don't need another one. */
9437 if (BINFO_VTABLE_PATH_MARKED (b))
9438 continue;
9439 BINFO_VTABLE_PATH_MARKED (b) = 1;
9440
9441 /* Figure out where we can find this vbase offset. */
9442 delta = size_binop (MULT_EXPR,
9443 vid->index,
9444 fold_convert (ssizetype,
9445 TYPE_SIZE_UNIT (vtable_entry_type)));
9446 if (vid->primary_vtbl_p)
9447 BINFO_VPTR_FIELD (b) = delta;
9448
9449 if (binfo != TYPE_BINFO (t))
9450 /* The vbase offset had better be the same. */
9451 gcc_assert (tree_int_cst_equal (delta, BINFO_VPTR_FIELD (vbase)));
9452
9453 /* The next vbase will come at a more negative offset. */
9454 vid->index = size_binop (MINUS_EXPR, vid->index,
9455 ssize_int (TARGET_VTABLE_DATA_ENTRY_DISTANCE));
9456
9457 /* The initializer is the delta from BINFO to this virtual base.
9458 The vbase offsets go in reverse inheritance-graph order, and
9459 we are walking in inheritance graph order so these end up in
9460 the right order. */
9461 delta = size_diffop_loc (input_location,
9462 BINFO_OFFSET (b), BINFO_OFFSET (non_primary_binfo));
9463
9464 CONSTRUCTOR_APPEND_ELT (vid->inits, NULL_TREE,
9465 fold_build1_loc (input_location, NOP_EXPR,
9466 vtable_entry_type, delta));
9467 }
9468 }
9469
9470 /* Adds the initializers for the vcall offset entries in the vtable
9471 for BINFO (which is part of the class hierarchy dominated by VID->DERIVED)
9472 to VID->INITS. */
9473
9474 static void
9475 build_vcall_offset_vtbl_entries (tree binfo, vtbl_init_data* vid)
9476 {
9477 /* We only need these entries if this base is a virtual base. We
9478 compute the indices -- but do not add to the vtable -- when
9479 building the main vtable for a class. */
9480 if (binfo == TYPE_BINFO (vid->derived)
9481 || (BINFO_VIRTUAL_P (binfo)
9482 /* If BINFO is RTTI_BINFO, then (since BINFO does not
9483 correspond to VID->DERIVED), we are building a primary
9484 construction virtual table. Since this is a primary
9485 virtual table, we do not need the vcall offsets for
9486 BINFO. */
9487 && binfo != vid->rtti_binfo))
9488 {
9489 /* We need a vcall offset for each of the virtual functions in this
9490 vtable. For example:
9491
9492 class A { virtual void f (); };
9493 class B1 : virtual public A { virtual void f (); };
9494 class B2 : virtual public A { virtual void f (); };
9495 class C: public B1, public B2 { virtual void f (); };
9496
9497 A C object has a primary base of B1, which has a primary base of A. A
9498 C also has a secondary base of B2, which no longer has a primary base
9499 of A. So the B2-in-C construction vtable needs a secondary vtable for
9500 A, which will adjust the A* to a B2* to call f. We have no way of
9501 knowing what (or even whether) this offset will be when we define B2,
9502 so we store this "vcall offset" in the A sub-vtable and look it up in
9503 a "virtual thunk" for B2::f.
9504
9505 We need entries for all the functions in our primary vtable and
9506 in our non-virtual bases' secondary vtables. */
9507 vid->vbase = binfo;
9508 /* If we are just computing the vcall indices -- but do not need
9509 the actual entries -- not that. */
9510 if (!BINFO_VIRTUAL_P (binfo))
9511 vid->generate_vcall_entries = false;
9512 /* Now, walk through the non-virtual bases, adding vcall offsets. */
9513 add_vcall_offset_vtbl_entries_r (binfo, vid);
9514 }
9515 }
9516
9517 /* Build vcall offsets, starting with those for BINFO. */
9518
9519 static void
9520 add_vcall_offset_vtbl_entries_r (tree binfo, vtbl_init_data* vid)
9521 {
9522 int i;
9523 tree primary_binfo;
9524 tree base_binfo;
9525
9526 /* Don't walk into virtual bases -- except, of course, for the
9527 virtual base for which we are building vcall offsets. Any
9528 primary virtual base will have already had its offsets generated
9529 through the recursion in build_vcall_and_vbase_vtbl_entries. */
9530 if (BINFO_VIRTUAL_P (binfo) && vid->vbase != binfo)
9531 return;
9532
9533 /* If BINFO has a primary base, process it first. */
9534 primary_binfo = get_primary_binfo (binfo);
9535 if (primary_binfo)
9536 add_vcall_offset_vtbl_entries_r (primary_binfo, vid);
9537
9538 /* Add BINFO itself to the list. */
9539 add_vcall_offset_vtbl_entries_1 (binfo, vid);
9540
9541 /* Scan the non-primary bases of BINFO. */
9542 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
9543 if (base_binfo != primary_binfo)
9544 add_vcall_offset_vtbl_entries_r (base_binfo, vid);
9545 }
9546
9547 /* Called from build_vcall_offset_vtbl_entries_r. */
9548
9549 static void
9550 add_vcall_offset_vtbl_entries_1 (tree binfo, vtbl_init_data* vid)
9551 {
9552 /* Make entries for the rest of the virtuals. */
9553 tree orig_fn;
9554
9555 /* The ABI requires that the methods be processed in declaration
9556 order. */
9557 for (orig_fn = TYPE_FIELDS (BINFO_TYPE (binfo));
9558 orig_fn;
9559 orig_fn = DECL_CHAIN (orig_fn))
9560 if (TREE_CODE (orig_fn) == FUNCTION_DECL && DECL_VINDEX (orig_fn))
9561 add_vcall_offset (orig_fn, binfo, vid);
9562 }
9563
9564 /* Add a vcall offset entry for ORIG_FN to the vtable. */
9565
9566 static void
9567 add_vcall_offset (tree orig_fn, tree binfo, vtbl_init_data *vid)
9568 {
9569 size_t i;
9570 tree vcall_offset;
9571 tree derived_entry;
9572
9573 /* If there is already an entry for a function with the same
9574 signature as FN, then we do not need a second vcall offset.
9575 Check the list of functions already present in the derived
9576 class vtable. */
9577 FOR_EACH_VEC_SAFE_ELT (vid->fns, i, derived_entry)
9578 {
9579 if (same_signature_p (derived_entry, orig_fn)
9580 /* We only use one vcall offset for virtual destructors,
9581 even though there are two virtual table entries. */
9582 || (DECL_DESTRUCTOR_P (derived_entry)
9583 && DECL_DESTRUCTOR_P (orig_fn)))
9584 return;
9585 }
9586
9587 /* If we are building these vcall offsets as part of building
9588 the vtable for the most derived class, remember the vcall
9589 offset. */
9590 if (vid->binfo == TYPE_BINFO (vid->derived))
9591 {
9592 tree_pair_s elt = {orig_fn, vid->index};
9593 vec_safe_push (CLASSTYPE_VCALL_INDICES (vid->derived), elt);
9594 }
9595
9596 /* The next vcall offset will be found at a more negative
9597 offset. */
9598 vid->index = size_binop (MINUS_EXPR, vid->index,
9599 ssize_int (TARGET_VTABLE_DATA_ENTRY_DISTANCE));
9600
9601 /* Keep track of this function. */
9602 vec_safe_push (vid->fns, orig_fn);
9603
9604 if (vid->generate_vcall_entries)
9605 {
9606 tree base;
9607 tree fn;
9608
9609 /* Find the overriding function. */
9610 fn = find_final_overrider (vid->rtti_binfo, binfo, orig_fn);
9611 if (fn == error_mark_node)
9612 vcall_offset = build_zero_cst (vtable_entry_type);
9613 else
9614 {
9615 base = TREE_VALUE (fn);
9616
9617 /* The vbase we're working on is a primary base of
9618 vid->binfo. But it might be a lost primary, so its
9619 BINFO_OFFSET might be wrong, so we just use the
9620 BINFO_OFFSET from vid->binfo. */
9621 vcall_offset = size_diffop_loc (input_location,
9622 BINFO_OFFSET (base),
9623 BINFO_OFFSET (vid->binfo));
9624 vcall_offset = fold_build1_loc (input_location,
9625 NOP_EXPR, vtable_entry_type,
9626 vcall_offset);
9627 }
9628 /* Add the initializer to the vtable. */
9629 CONSTRUCTOR_APPEND_ELT (vid->inits, NULL_TREE, vcall_offset);
9630 }
9631 }
9632
9633 /* Return vtbl initializers for the RTTI entries corresponding to the
9634 BINFO's vtable. The RTTI entries should indicate the object given
9635 by VID->rtti_binfo. */
9636
9637 static void
9638 build_rtti_vtbl_entries (tree binfo, vtbl_init_data* vid)
9639 {
9640 tree b;
9641 tree t;
9642 tree offset;
9643 tree decl;
9644 tree init;
9645
9646 t = BINFO_TYPE (vid->rtti_binfo);
9647
9648 /* To find the complete object, we will first convert to our most
9649 primary base, and then add the offset in the vtbl to that value. */
9650 b = most_primary_binfo (binfo);
9651 offset = size_diffop_loc (input_location,
9652 BINFO_OFFSET (vid->rtti_binfo), BINFO_OFFSET (b));
9653
9654 /* The second entry is the address of the typeinfo object. */
9655 if (flag_rtti)
9656 decl = build_address (get_tinfo_decl (t));
9657 else
9658 decl = integer_zero_node;
9659
9660 /* Convert the declaration to a type that can be stored in the
9661 vtable. */
9662 init = build_nop (vfunc_ptr_type_node, decl);
9663 CONSTRUCTOR_APPEND_ELT (vid->inits, NULL_TREE, init);
9664
9665 /* Add the offset-to-top entry. It comes earlier in the vtable than
9666 the typeinfo entry. Convert the offset to look like a
9667 function pointer, so that we can put it in the vtable. */
9668 init = build_nop (vfunc_ptr_type_node, offset);
9669 CONSTRUCTOR_APPEND_ELT (vid->inits, NULL_TREE, init);
9670 }
9671
9672 /* TRUE iff TYPE is uniquely derived from PARENT. Ignores
9673 accessibility. */
9674
9675 bool
9676 uniquely_derived_from_p (tree parent, tree type)
9677 {
9678 tree base = lookup_base (type, parent, ba_unique, NULL, tf_none);
9679 return base && base != error_mark_node;
9680 }
9681
9682 /* TRUE iff TYPE is publicly & uniquely derived from PARENT. */
9683
9684 bool
9685 publicly_uniquely_derived_p (tree parent, tree type)
9686 {
9687 tree base = lookup_base (type, parent, ba_ignore_scope | ba_check,
9688 NULL, tf_none);
9689 return base && base != error_mark_node;
9690 }
9691
9692 /* CTX1 and CTX2 are declaration contexts. Return the innermost common
9693 class between them, if any. */
9694
9695 tree
9696 common_enclosing_class (tree ctx1, tree ctx2)
9697 {
9698 if (!TYPE_P (ctx1) || !TYPE_P (ctx2))
9699 return NULL_TREE;
9700 gcc_assert (ctx1 == TYPE_MAIN_VARIANT (ctx1)
9701 && ctx2 == TYPE_MAIN_VARIANT (ctx2));
9702 if (ctx1 == ctx2)
9703 return ctx1;
9704 for (tree t = ctx1; TYPE_P (t); t = TYPE_CONTEXT (t))
9705 TYPE_MARKED_P (t) = true;
9706 tree found = NULL_TREE;
9707 for (tree t = ctx2; TYPE_P (t); t = TYPE_CONTEXT (t))
9708 if (TYPE_MARKED_P (t))
9709 {
9710 found = t;
9711 break;
9712 }
9713 for (tree t = ctx1; TYPE_P (t); t = TYPE_CONTEXT (t))
9714 TYPE_MARKED_P (t) = false;
9715 return found;
9716 }
9717
9718 #include "gt-cp-class.h"