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