1/* Functions related to building classes and their related objects.
2 Copyright (C) 1987-2017 Free Software Foundation, Inc.
3 Contributed by Michael Tiemann (tiemann@cygnus.com)
4
5This file is part of GCC.
6
7GCC is free software; you can redistribute it and/or modify
8it under the terms of the GNU General Public License as published by
9the Free Software Foundation; either version 3, or (at your option)
10any later version.
11
12GCC is distributed in the hope that it will be useful,
13but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15GNU General Public License for more details.
16
17You should have received a copy of the GNU General Public License
18along 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. */
42int 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
47int 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
53typedef 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
72struct 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. */
104typedef 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. */
108static int current_class_stack_size;
109static class_stack_node_t current_class_stack;
110
111/* The size of the largest empty class seen in this translation unit. */
112static GTY (()) tree sizeof_biggest_empty_class;
113
114/* An array of all local classes present in this translation unit, in
115 declaration order. */
116vec<tree, va_gc> *local_classes;
117
118static tree get_vfield_name (tree);
119static void finish_struct_anon (tree);
120static tree get_vtable_name (tree);
121static void get_basefndecls (tree, tree, vec<tree> *);
122static int build_primary_vtable (tree, tree);
123static int build_secondary_vtable (tree);
124static void finish_vtbls (tree);
125static void modify_vtable_entry (tree, tree, tree, tree, tree *);
126static void finish_struct_bits (tree);
127static int alter_access (tree, tree, tree);
128static void handle_using_decl (tree, tree);
129static tree dfs_modify_vtables (tree, void *);
130static tree modify_all_vtables (tree, tree);
131static void determine_primary_bases (tree);
132static void maybe_warn_about_overly_private_class (tree);
133static void add_implicitly_declared_members (tree, tree*, int, int);
134static tree fixed_type_or_null (tree, int *, int *);
135static tree build_simple_base_path (tree expr, tree binfo);
136static tree build_vtbl_ref_1 (tree, tree);
137static void build_vtbl_initializer (tree, tree, tree, tree, int *,
138 vec<constructor_elt, va_gc> **);
139static bool check_bitfield_decl (tree);
140static bool check_field_decl (tree, tree, int *, int *);
141static void check_field_decls (tree, tree *, int *, int *);
142static tree *build_base_field (record_layout_info, tree, splay_tree, tree *);
143static void build_base_fields (record_layout_info, splay_tree, tree *);
144static void check_methods (tree);
145static void remove_zero_width_bit_fields (tree);
146static bool accessible_nvdtor_p (tree);
147
148/* Used by find_flexarrays and related functions. */
149struct flexmems_t;
150static void diagnose_flexarrays (tree, const flexmems_t *);
151static void find_flexarrays (tree, flexmems_t *, bool = false,
152 tree = NULL_TREE, tree = NULL_TREE);
153static void check_flexarrays (tree, flexmems_t * = NULL, bool = false);
154static void check_bases (tree, int *, int *);
155static void check_bases_and_members (tree);
156static tree create_vtable_ptr (tree, tree *);
157static void include_empty_classes (record_layout_info);
158static void layout_class_type (tree, tree *);
159static void propagate_binfo_offsets (tree, tree);
160static void layout_virtual_bases (record_layout_info, splay_tree);
161static void build_vbase_offset_vtbl_entries (tree, vtbl_init_data *);
162static void add_vcall_offset_vtbl_entries_r (tree, vtbl_init_data *);
163static void add_vcall_offset_vtbl_entries_1 (tree, vtbl_init_data *);
164static void build_vcall_offset_vtbl_entries (tree, vtbl_init_data *);
165static void add_vcall_offset (tree, tree, vtbl_init_data *);
166static void layout_vtable_decl (tree, int);
167static tree dfs_find_final_overrider_pre (tree, void *);
168static tree dfs_find_final_overrider_post (tree, void *);
169static tree find_final_overrider (tree, tree, tree);
170static int make_new_vtable (tree, tree);
171static tree get_primary_binfo (tree);
172static int maybe_indent_hierarchy (FILE *, int, int);
173static tree dump_class_hierarchy_r (FILE *, dump_flags_t, tree, tree, int);
174static void dump_class_hierarchy (tree);
175static void dump_class_hierarchy_1 (FILE *, dump_flags_t, tree);
176static void dump_array (FILE *, tree);
177static void dump_vtable (tree, tree, tree);
178static void dump_vtt (tree, tree);
179static void dump_thunk (FILE *, int, tree);
180static tree build_vtable (tree, tree, tree);
181static void initialize_vtable (tree, vec<constructor_elt, va_gc> *);
182static void layout_nonempty_base_or_field (record_layout_info,
183 tree, tree, splay_tree);
184static tree end_of_class (tree, int);
185static bool layout_empty_base (record_layout_info, tree, tree, splay_tree);
186static void accumulate_vtbl_inits (tree, tree, tree, tree, tree,
187 vec<constructor_elt, va_gc> **);
188static void dfs_accumulate_vtbl_inits (tree, tree, tree, tree, tree,
189 vec<constructor_elt, va_gc> **);
190static void build_rtti_vtbl_entries (tree, vtbl_init_data *);
191static void build_vcall_and_vbase_vtbl_entries (tree, vtbl_init_data *);
192static void clone_constructors_and_destructors (tree);
193static tree build_clone (tree, tree);
194static void update_vtable_entry_for_fn (tree, tree, tree, tree *, unsigned);
195static void build_ctor_vtbl_group (tree, tree);
196static void build_vtt (tree);
197static tree binfo_ctor_vtable (tree);
198static void build_vtt_inits (tree, tree, vec<constructor_elt, va_gc> **,
199 tree *);
200static tree dfs_build_secondary_vptr_vtt_inits (tree, void *);
201static tree dfs_fixup_binfo_vtbls (tree, void *);
202static int record_subobject_offset (tree, tree, splay_tree);
203static int check_subobject_offset (tree, tree, splay_tree);
204static int walk_subobject_offsets (tree, subobject_offset_fn,
205 tree, splay_tree, tree, int);
206static void record_subobject_offsets (tree, tree, splay_tree, bool);
207static int layout_conflict_p (tree, tree, splay_tree, int);
208static int splay_tree_compare_integer_csts (splay_tree_key k1,
209 splay_tree_key k2);
210static void warn_about_ambiguous_bases (tree);
211static bool type_requires_array_cookie (tree);
212static bool base_derived_from (tree, tree);
213static int empty_base_at_nonzero_offset_p (tree, tree, splay_tree);
214static tree end_of_base (tree);
215static tree get_vcall_index (tree, tree);
216static bool type_maybe_constexpr_default_constructor (tree);
217
218/* Variables shared between class.c and call.c. */
219
220int n_vtables = 0;
221int n_vtable_entries = 0;
222int n_vtable_searches = 0;
223int n_vtable_elems = 0;
224int n_convert_harshness = 0;
225int n_compute_conversion_costs = 0;
226int n_inner_fields_searched = 0;
227
228/* Return a COND_EXPR that executes TRUE_STMT if this execution of the
229 'structor is in charge of 'structing virtual bases, or FALSE_STMT
230 otherwise. */
231
232tree
233build_if_in_charge (tree true_stmt, tree false_stmt)
234{
235 gcc_assert (DECL_HAS_IN_CHARGE_PARM_P (current_function_decl));
236 tree cmp = build2 (NE_EXPR, boolean_type_node,
237 current_in_charge_parm, integer_zero_node);
238 tree type = unlowered_expr_type (true_stmt);
239 if (VOID_TYPE_P (type))
240 type = unlowered_expr_type (false_stmt);
241 tree cond = build3 (COND_EXPR, type,
242 cmp, true_stmt, false_stmt);
243 return cond;
244}
245
246/* Convert to or from a base subobject. EXPR is an expression of type
247 `A' or `A*', an expression of type `B' or `B*' is returned. To
248 convert A to a base B, CODE is PLUS_EXPR and BINFO is the binfo for
249 the B base instance within A. To convert base A to derived B, CODE
250 is MINUS_EXPR and BINFO is the binfo for the A instance within B.
251 In this latter case, A must not be a morally virtual base of B.
252 NONNULL is true if EXPR is known to be non-NULL (this is only
253 needed when EXPR is of pointer type). CV qualifiers are preserved
254 from EXPR. */
255
256tree
257build_base_path (enum tree_code code,
258 tree expr,
259 tree binfo,
260 int nonnull,
261 tsubst_flags_t complain)
262{
263 tree v_binfo = NULL_TREE;
264 tree d_binfo = NULL_TREE;
265 tree probe;
266 tree offset;
267 tree target_type;
268 tree null_test = NULL;
269 tree ptr_target_type;
270 int fixed_type_p;
271 int want_pointer = TYPE_PTR_P (TREE_TYPE (expr));
272 bool has_empty = false;
273 bool virtual_access;
274 bool rvalue = false;
275
276 if (expr == error_mark_node || binfo == error_mark_node || !binfo)
277 return error_mark_node;
278
279 for (probe = binfo; probe; probe = BINFO_INHERITANCE_CHAIN (probe))
280 {
281 d_binfo = probe;
282 if (is_empty_class (BINFO_TYPE (probe)))
283 has_empty = true;
284 if (!v_binfo && BINFO_VIRTUAL_P (probe))
285 v_binfo = probe;
286 }
287
288 probe = TYPE_MAIN_VARIANT (TREE_TYPE (expr));
289 if (want_pointer)
290 probe = TYPE_MAIN_VARIANT (TREE_TYPE (probe));
291
292 if (code == PLUS_EXPR
293 && !SAME_BINFO_TYPE_P (BINFO_TYPE (d_binfo), probe))
294 {
295 /* This can happen when adjust_result_of_qualified_name_lookup can't
296 find a unique base binfo in a call to a member function. We
297 couldn't give the diagnostic then since we might have been calling
298 a static member function, so we do it now. In other cases, eg.
299 during error recovery (c++/71979), we may not have a base at all. */
300 if (complain & tf_error)
301 {
302 tree base = lookup_base (probe, BINFO_TYPE (d_binfo),
303 ba_unique, NULL, complain);
304 gcc_assert (base == error_mark_node || !base);
305 }
306 return error_mark_node;
307 }
308
309 gcc_assert ((code == MINUS_EXPR
310 && SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), probe))
311 || code == PLUS_EXPR);
312
313 if (binfo == d_binfo)
314 /* Nothing to do. */
315 return expr;
316
317 if (code == MINUS_EXPR && v_binfo)
318 {
319 if (complain & tf_error)
320 {
321 if (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), BINFO_TYPE (v_binfo)))
322 {
323 if (want_pointer)
324 error ("cannot convert from pointer to base class %qT to "
325 "pointer to derived class %qT because the base is "
326 "virtual", BINFO_TYPE (binfo), BINFO_TYPE (d_binfo));
327 else
328 error ("cannot convert from base class %qT to derived "
329 "class %qT because the base is virtual",
330 BINFO_TYPE (binfo), BINFO_TYPE (d_binfo));
331 }
332 else
333 {
334 if (want_pointer)
335 error ("cannot convert from pointer to base class %qT to "
336 "pointer to derived class %qT via virtual base %qT",
337 BINFO_TYPE (binfo), BINFO_TYPE (d_binfo),
338 BINFO_TYPE (v_binfo));
339 else
340 error ("cannot convert from base class %qT to derived "
341 "class %qT via virtual base %qT", BINFO_TYPE (binfo),
342 BINFO_TYPE (d_binfo), BINFO_TYPE (v_binfo));
343 }
344 }
345 return error_mark_node;
346 }
347
348 if (!want_pointer)
349 {
350 rvalue = !lvalue_p (expr);
351 /* This must happen before the call to save_expr. */
352 expr = cp_build_addr_expr (expr, complain);
353 }
354 else
355 expr = mark_rvalue_use (expr);
356
357 offset = BINFO_OFFSET (binfo);
358 fixed_type_p = resolves_to_fixed_type_p (expr, &nonnull);
359 target_type = code == PLUS_EXPR ? BINFO_TYPE (binfo) : BINFO_TYPE (d_binfo);
360 /* TARGET_TYPE has been extracted from BINFO, and, is therefore always
361 cv-unqualified. Extract the cv-qualifiers from EXPR so that the
362 expression returned matches the input. */
363 target_type = cp_build_qualified_type
364 (target_type, cp_type_quals (TREE_TYPE (TREE_TYPE (expr))));
365 ptr_target_type = build_pointer_type (target_type);
366
367 /* Do we need to look in the vtable for the real offset? */
368 virtual_access = (v_binfo && fixed_type_p <= 0);
369
370 /* Don't bother with the calculations inside sizeof; they'll ICE if the
371 source type is incomplete and the pointer value doesn't matter. In a
372 template (even in instantiate_non_dependent_expr), we don't have vtables
373 set up properly yet, and the value doesn't matter there either; we're
374 just interested in the result of overload resolution. */
375 if (cp_unevaluated_operand != 0
376 || processing_template_decl
377 || in_template_function ())
378 {
379 expr = build_nop (ptr_target_type, expr);
380 goto indout;
381 }
382
383 /* If we're in an NSDMI, we don't have the full constructor context yet
384 that we need for converting to a virtual base, so just build a stub
385 CONVERT_EXPR and expand it later in bot_replace. */
386 if (virtual_access && fixed_type_p < 0
387 && current_scope () != current_function_decl)
388 {
389 expr = build1 (CONVERT_EXPR, ptr_target_type, expr);
390 CONVERT_EXPR_VBASE_PATH (expr) = true;
391 goto indout;
392 }
393
394 /* Do we need to check for a null pointer? */
395 if (want_pointer && !nonnull)
396 {
397 /* If we know the conversion will not actually change the value
398 of EXPR, then we can avoid testing the expression for NULL.
399 We have to avoid generating a COMPONENT_REF for a base class
400 field, because other parts of the compiler know that such
401 expressions are always non-NULL. */
402 if (!virtual_access && integer_zerop (offset))
403 return build_nop (ptr_target_type, expr);
404 null_test = error_mark_node;
405 }
406
407 /* Protect against multiple evaluation if necessary. */
408 if (TREE_SIDE_EFFECTS (expr) && (null_test || virtual_access))
409 expr = save_expr (expr);
410
411 /* Now that we've saved expr, build the real null test. */
412 if (null_test)
413 {
414 tree zero = cp_convert (TREE_TYPE (expr), nullptr_node, complain);
415 null_test = build2_loc (input_location, NE_EXPR, boolean_type_node,
416 expr, zero);
417 /* This is a compiler generated comparison, don't emit
418 e.g. -Wnonnull-compare warning for it. */
419 TREE_NO_WARNING (null_test) = 1;
420 }
421
422 /* If this is a simple base reference, express it as a COMPONENT_REF. */
423 if (code == PLUS_EXPR && !virtual_access
424 /* We don't build base fields for empty bases, and they aren't very
425 interesting to the optimizers anyway. */
426 && !has_empty)
427 {
428 expr = cp_build_fold_indirect_ref (expr);
429 expr = build_simple_base_path (expr, binfo);
430 if (rvalue)
431 expr = move (expr);
432 if (want_pointer)
433 expr = build_address (expr);
434 target_type = TREE_TYPE (expr);
435 goto out;
436 }
437
438 if (virtual_access)
439 {
440 /* Going via virtual base V_BINFO. We need the static offset
441 from V_BINFO to BINFO, and the dynamic offset from D_BINFO to
442 V_BINFO. That offset is an entry in D_BINFO's vtable. */
443 tree v_offset;
444
445 if (fixed_type_p < 0 && in_base_initializer)
446 {
447 /* In a base member initializer, we cannot rely on the
448 vtable being set up. We have to indirect via the
449 vtt_parm. */
450 tree t;
451
452 t = TREE_TYPE (TYPE_VFIELD (current_class_type));
453 t = build_pointer_type (t);
454 v_offset = fold_convert (t, current_vtt_parm);
455 v_offset = cp_build_fold_indirect_ref (v_offset);
456 }
457 else
458 {
459 tree t = expr;
460 if (sanitize_flags_p (SANITIZE_VPTR)
461 && fixed_type_p == 0)
462 {
463 t = cp_ubsan_maybe_instrument_cast_to_vbase (input_location,
464 probe, expr);
465 if (t == NULL_TREE)
466 t = expr;
467 }
468 v_offset = build_vfield_ref (cp_build_fold_indirect_ref (t),
469 TREE_TYPE (TREE_TYPE (expr)));
470 }
471
472 if (v_offset == error_mark_node)
473 return error_mark_node;
474
475 v_offset = fold_build_pointer_plus (v_offset, BINFO_VPTR_FIELD (v_binfo));
476 v_offset = build1 (NOP_EXPR,
477 build_pointer_type (ptrdiff_type_node),
478 v_offset);
479 v_offset = cp_build_fold_indirect_ref (v_offset);
480 TREE_CONSTANT (v_offset) = 1;
481
482 offset = convert_to_integer (ptrdiff_type_node,
483 size_diffop_loc (input_location, offset,
484 BINFO_OFFSET (v_binfo)));
485
486 if (!integer_zerop (offset))
487 v_offset = build2 (code, ptrdiff_type_node, v_offset, offset);
488
489 if (fixed_type_p < 0)
490 /* Negative fixed_type_p means this is a constructor or destructor;
491 virtual base layout is fixed in in-charge [cd]tors, but not in
492 base [cd]tors. */
493 offset = build_if_in_charge
494 (convert_to_integer (ptrdiff_type_node, BINFO_OFFSET (binfo)),
495 v_offset);
496 else
497 offset = v_offset;
498 }
499
500 if (want_pointer)
501 target_type = ptr_target_type;
502
503 expr = build1 (NOP_EXPR, ptr_target_type, expr);
504
505 if (!integer_zerop (offset))
506 {
507 offset = fold_convert (sizetype, offset);
508 if (code == MINUS_EXPR)
509 offset = fold_build1_loc (input_location, NEGATE_EXPR, sizetype, offset);
510 expr = fold_build_pointer_plus (expr, offset);
511 }
512 else
513 null_test = NULL;
514
515 indout:
516 if (!want_pointer)
517 {
518 expr = cp_build_fold_indirect_ref (expr);
519 if (rvalue)
520 expr = move (expr);
521 }
522
523 out:
524 if (null_test)
525 expr = fold_build3_loc (input_location, COND_EXPR, target_type, null_test, expr,
526 build_zero_cst (target_type));
527
528 return expr;
529}
530
531/* Subroutine of build_base_path; EXPR and BINFO are as in that function.
532 Perform a derived-to-base conversion by recursively building up a
533 sequence of COMPONENT_REFs to the appropriate base fields. */
534
535static tree
536build_simple_base_path (tree expr, tree binfo)
537{
538 tree type = BINFO_TYPE (binfo);
539 tree d_binfo = BINFO_INHERITANCE_CHAIN (binfo);
540 tree field;
541
542 if (d_binfo == NULL_TREE)
543 {
544 tree temp;
545
546 gcc_assert (TYPE_MAIN_VARIANT (TREE_TYPE (expr)) == type);
547
548 /* Transform `(a, b).x' into `(*(a, &b)).x', `(a ? b : c).x'
549 into `(*(a ? &b : &c)).x', and so on. A COND_EXPR is only
550 an lvalue in the front end; only _DECLs and _REFs are lvalues
551 in the back end. */
552 temp = unary_complex_lvalue (ADDR_EXPR, expr);
553 if (temp)
554 expr = cp_build_fold_indirect_ref (temp);
555
556 return expr;
557 }
558
559 /* Recurse. */
560 expr = build_simple_base_path (expr, d_binfo);
561
562 for (field = TYPE_FIELDS (BINFO_TYPE (d_binfo));
563 field; field = DECL_CHAIN (field))
564 /* Is this the base field created by build_base_field? */
565 if (TREE_CODE (field) == FIELD_DECL
566 && DECL_FIELD_IS_BASE (field)
567 && TREE_TYPE (field) == type
568 /* If we're looking for a field in the most-derived class,
569 also check the field offset; we can have two base fields
570 of the same type if one is an indirect virtual base and one
571 is a direct non-virtual base. */
572 && (BINFO_INHERITANCE_CHAIN (d_binfo)
573 || tree_int_cst_equal (byte_position (field),
574 BINFO_OFFSET (binfo))))
575 {
576 /* We don't use build_class_member_access_expr here, as that
577 has unnecessary checks, and more importantly results in
578 recursive calls to dfs_walk_once. */
579 int type_quals = cp_type_quals (TREE_TYPE (expr));
580
581 expr = build3 (COMPONENT_REF,
582 cp_build_qualified_type (type, type_quals),
583 expr, field, NULL_TREE);
584 /* Mark the expression const or volatile, as appropriate.
585 Even though we've dealt with the type above, we still have
586 to mark the expression itself. */
587 if (type_quals & TYPE_QUAL_CONST)
588 TREE_READONLY (expr) = 1;
589 if (type_quals & TYPE_QUAL_VOLATILE)
590 TREE_THIS_VOLATILE (expr) = 1;
591
592 return expr;
593 }
594
595 /* Didn't find the base field?!? */
596 gcc_unreachable ();
597}
598
599/* Convert OBJECT to the base TYPE. OBJECT is an expression whose
600 type is a class type or a pointer to a class type. In the former
601 case, TYPE is also a class type; in the latter it is another
602 pointer type. If CHECK_ACCESS is true, an error message is emitted
603 if TYPE is inaccessible. If OBJECT has pointer type, the value is
604 assumed to be non-NULL. */
605
606tree
607convert_to_base (tree object, tree type, bool check_access, bool nonnull,
608 tsubst_flags_t complain)
609{
610 tree binfo;
611 tree object_type;
612
613 if (TYPE_PTR_P (TREE_TYPE (object)))
614 {
615 object_type = TREE_TYPE (TREE_TYPE (object));
616 type = TREE_TYPE (type);
617 }
618 else
619 object_type = TREE_TYPE (object);
620
621 binfo = lookup_base (object_type, type, check_access ? ba_check : ba_unique,
622 NULL, complain);
623 if (!binfo || binfo == error_mark_node)
624 return error_mark_node;
625
626 return build_base_path (PLUS_EXPR, object, binfo, nonnull, complain);
627}
628
629/* EXPR is an expression with unqualified class type. BASE is a base
630 binfo of that class type. Returns EXPR, converted to the BASE
631 type. This function assumes that EXPR is the most derived class;
632 therefore virtual bases can be found at their static offsets. */
633
634tree
635convert_to_base_statically (tree expr, tree base)
636{
637 tree expr_type;
638
639 expr_type = TREE_TYPE (expr);
640 if (!SAME_BINFO_TYPE_P (BINFO_TYPE (base), expr_type))
641 {
642 /* If this is a non-empty base, use a COMPONENT_REF. */
643 if (!is_empty_class (BINFO_TYPE (base)))
644 return build_simple_base_path (expr, base);
645
646 /* We use fold_build2 and fold_convert below to simplify the trees
647 provided to the optimizers. It is not safe to call these functions
648 when processing a template because they do not handle C++-specific
649 trees. */
650 gcc_assert (!processing_template_decl);
651 expr = cp_build_addr_expr (expr, tf_warning_or_error);
652 if (!integer_zerop (BINFO_OFFSET (base)))
653 expr = fold_build_pointer_plus_loc (input_location,
654 expr, BINFO_OFFSET (base));
655 expr = fold_convert (build_pointer_type (BINFO_TYPE (base)), expr);
656 expr = build_fold_indirect_ref_loc (input_location, expr);
657 }
658
659 return expr;
660}
661
662
663tree
664build_vfield_ref (tree datum, tree type)
665{
666 tree vfield, vcontext;
667
668 if (datum == error_mark_node
669 /* Can happen in case of duplicate base types (c++/59082). */
670 || !TYPE_VFIELD (type))
671 return error_mark_node;
672
673 /* First, convert to the requested type. */
674 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (datum), type))
675 datum = convert_to_base (datum, type, /*check_access=*/false,
676 /*nonnull=*/true, tf_warning_or_error);
677
678 /* Second, the requested type may not be the owner of its own vptr.
679 If not, convert to the base class that owns it. We cannot use
680 convert_to_base here, because VCONTEXT may appear more than once
681 in the inheritance hierarchy of TYPE, and thus direct conversion
682 between the types may be ambiguous. Following the path back up
683 one step at a time via primary bases avoids the problem. */
684 vfield = TYPE_VFIELD (type);
685 vcontext = DECL_CONTEXT (vfield);
686 while (!same_type_ignoring_top_level_qualifiers_p (vcontext, type))
687 {
688 datum = build_simple_base_path (datum, CLASSTYPE_PRIMARY_BINFO (type));
689 type = TREE_TYPE (datum);
690 }
691
692 return build3 (COMPONENT_REF, TREE_TYPE (vfield), datum, vfield, NULL_TREE);
693}
694
695/* Given an object INSTANCE, return an expression which yields the
696 vtable element corresponding to INDEX. There are many special
697 cases for INSTANCE which we take care of here, mainly to avoid
698 creating extra tree nodes when we don't have to. */
699
700static tree
701build_vtbl_ref_1 (tree instance, tree idx)
702{
703 tree aref;
704 tree vtbl = NULL_TREE;
705
706 /* Try to figure out what a reference refers to, and
707 access its virtual function table directly. */
708
709 int cdtorp = 0;
710 tree fixed_type = fixed_type_or_null (instance, NULL, &cdtorp);
711
712 tree basetype = non_reference (TREE_TYPE (instance));
713
714 if (fixed_type && !cdtorp)
715 {
716 tree binfo = lookup_base (fixed_type, basetype,
717 ba_unique, NULL, tf_none);
718 if (binfo && binfo != error_mark_node)
719 vtbl = unshare_expr (BINFO_VTABLE (binfo));
720 }
721
722 if (!vtbl)
723 vtbl = build_vfield_ref (instance, basetype);
724
725 aref = build_array_ref (input_location, vtbl, idx);
726 TREE_CONSTANT (aref) |= TREE_CONSTANT (vtbl) && TREE_CONSTANT (idx);
727
728 return aref;
729}
730
731tree
732build_vtbl_ref (tree instance, tree idx)
733{
734 tree aref = build_vtbl_ref_1 (instance, idx);
735
736 return aref;
737}
738
739/* Given a stable object pointer INSTANCE_PTR, return an expression which
740 yields a function pointer corresponding to vtable element INDEX. */
741
742tree
743build_vfn_ref (tree instance_ptr, tree idx)
744{
745 tree aref;
746
747 aref = build_vtbl_ref_1 (cp_build_fold_indirect_ref (instance_ptr),
748 idx);
749
750 /* When using function descriptors, the address of the
751 vtable entry is treated as a function pointer. */
752 if (TARGET_VTABLE_USES_DESCRIPTORS)
753 aref = build1 (NOP_EXPR, TREE_TYPE (aref),
754 cp_build_addr_expr (aref, tf_warning_or_error));
755
756 /* Remember this as a method reference, for later devirtualization. */
757 aref = build3 (OBJ_TYPE_REF, TREE_TYPE (aref), aref, instance_ptr, idx);
758
759 return aref;
760}
761
762/* Return the name of the virtual function table (as an IDENTIFIER_NODE)
763 for the given TYPE. */
764
765static tree
766get_vtable_name (tree type)
767{
768 return mangle_vtbl_for_type (type);
769}
770
771/* DECL is an entity associated with TYPE, like a virtual table or an
772 implicitly generated constructor. Determine whether or not DECL
773 should have external or internal linkage at the object file
774 level. This routine does not deal with COMDAT linkage and other
775 similar complexities; it simply sets TREE_PUBLIC if it possible for
776 entities in other translation units to contain copies of DECL, in
777 the abstract. */
778
779void
780set_linkage_according_to_type (tree /*type*/, tree decl)
781{
782 TREE_PUBLIC (decl) = 1;
783 determine_visibility (decl);
784}
785
786/* Create a VAR_DECL for a primary or secondary vtable for CLASS_TYPE.
787 (For a secondary vtable for B-in-D, CLASS_TYPE should be D, not B.)
788 Use NAME for the name of the vtable, and VTABLE_TYPE for its type. */
789
790static tree
791build_vtable (tree class_type, tree name, tree vtable_type)
792{
793 tree decl;
794
795 decl = build_lang_decl (VAR_DECL, name, vtable_type);
796 /* vtable names are already mangled; give them their DECL_ASSEMBLER_NAME
797 now to avoid confusion in mangle_decl. */
798 SET_DECL_ASSEMBLER_NAME (decl, name);
799 DECL_CONTEXT (decl) = class_type;
800 DECL_ARTIFICIAL (decl) = 1;
801 TREE_STATIC (decl) = 1;
802 TREE_READONLY (decl) = 1;
803 DECL_VIRTUAL_P (decl) = 1;
804 SET_DECL_ALIGN (decl, TARGET_VTABLE_ENTRY_ALIGN);
805 DECL_USER_ALIGN (decl) = true;
806 DECL_VTABLE_OR_VTT_P (decl) = 1;
807 set_linkage_according_to_type (class_type, decl);
808 /* The vtable has not been defined -- yet. */
809 DECL_EXTERNAL (decl) = 1;
810 DECL_NOT_REALLY_EXTERN (decl) = 1;
811
812 /* Mark the VAR_DECL node representing the vtable itself as a
813 "gratuitous" one, thereby forcing dwarfout.c to ignore it. It
814 is rather important that such things be ignored because any
815 effort to actually generate DWARF for them will run into
816 trouble when/if we encounter code like:
817
818 #pragma interface
819 struct S { virtual void member (); };
820
821 because the artificial declaration of the vtable itself (as
822 manufactured by the g++ front end) will say that the vtable is
823 a static member of `S' but only *after* the debug output for
824 the definition of `S' has already been output. This causes
825 grief because the DWARF entry for the definition of the vtable
826 will try to refer back to an earlier *declaration* of the
827 vtable as a static member of `S' and there won't be one. We
828 might be able to arrange to have the "vtable static member"
829 attached to the member list for `S' before the debug info for
830 `S' get written (which would solve the problem) but that would
831 require more intrusive changes to the g++ front end. */
832 DECL_IGNORED_P (decl) = 1;
833
834 return decl;
835}
836
837/* Get the VAR_DECL of the vtable for TYPE. TYPE need not be polymorphic,
838 or even complete. If this does not exist, create it. If COMPLETE is
839 nonzero, then complete the definition of it -- that will render it
840 impossible to actually build the vtable, but is useful to get at those
841 which are known to exist in the runtime. */
842
843tree
844get_vtable_decl (tree type, int complete)
845{
846 tree decl;
847
848 if (CLASSTYPE_VTABLES (type))
849 return CLASSTYPE_VTABLES (type);
850
851 decl = build_vtable (type, get_vtable_name (type), vtbl_type_node);
852 CLASSTYPE_VTABLES (type) = decl;
853
854 if (complete)
855 {
856 DECL_EXTERNAL (decl) = 1;
857 cp_finish_decl (decl, NULL_TREE, false, NULL_TREE, 0);
858 }
859
860 return decl;
861}
862
863/* Build the primary virtual function table for TYPE. If BINFO is
864 non-NULL, build the vtable starting with the initial approximation
865 that it is the same as the one which is the head of the association
866 list. Returns a nonzero value if a new vtable is actually
867 created. */
868
869static int
870build_primary_vtable (tree binfo, tree type)
871{
872 tree decl;
873 tree virtuals;
874
875 decl = get_vtable_decl (type, /*complete=*/0);
876
877 if (binfo)
878 {
879 if (BINFO_NEW_VTABLE_MARKED (binfo))
880 /* We have already created a vtable for this base, so there's
881 no need to do it again. */
882 return 0;
883
884 virtuals = copy_list (BINFO_VIRTUALS (binfo));
885 TREE_TYPE (decl) = TREE_TYPE (get_vtbl_decl_for_binfo (binfo));
886 DECL_SIZE (decl) = TYPE_SIZE (TREE_TYPE (decl));
887 DECL_SIZE_UNIT (decl) = TYPE_SIZE_UNIT (TREE_TYPE (decl));
888 }
889 else
890 {
891 gcc_assert (TREE_TYPE (decl) == vtbl_type_node);
892 virtuals = NULL_TREE;
893 }
894
895 if (GATHER_STATISTICS)
896 {
897 n_vtables += 1;
898 n_vtable_elems += list_length (virtuals);
899 }
900
901 /* Initialize the association list for this type, based
902 on our first approximation. */
903 BINFO_VTABLE (TYPE_BINFO (type)) = decl;
904 BINFO_VIRTUALS (TYPE_BINFO (type)) = virtuals;
905 SET_BINFO_NEW_VTABLE_MARKED (TYPE_BINFO (type));
906 return 1;
907}
908
909/* Give BINFO a new virtual function table which is initialized
910 with a skeleton-copy of its original initialization. The only
911 entry that changes is the `delta' entry, so we can really
912 share a lot of structure.
913
914 FOR_TYPE is the most derived type which caused this table to
915 be needed.
916
917 Returns nonzero if we haven't met BINFO before.
918
919 The order in which vtables are built (by calling this function) for
920 an object must remain the same, otherwise a binary incompatibility
921 can result. */
922
923static int
924build_secondary_vtable (tree binfo)
925{
926 if (BINFO_NEW_VTABLE_MARKED (binfo))
927 /* We already created a vtable for this base. There's no need to
928 do it again. */
929 return 0;
930
931 /* Remember that we've created a vtable for this BINFO, so that we
932 don't try to do so again. */
933 SET_BINFO_NEW_VTABLE_MARKED (binfo);
934
935 /* Make fresh virtual list, so we can smash it later. */
936 BINFO_VIRTUALS (binfo) = copy_list (BINFO_VIRTUALS (binfo));
937
938 /* Secondary vtables are laid out as part of the same structure as
939 the primary vtable. */
940 BINFO_VTABLE (binfo) = NULL_TREE;
941 return 1;
942}
943
944/* Create a new vtable for BINFO which is the hierarchy dominated by
945 T. Return nonzero if we actually created a new vtable. */
946
947static int
948make_new_vtable (tree t, tree binfo)
949{
950 if (binfo == TYPE_BINFO (t))
951 /* In this case, it is *type*'s vtable we are modifying. We start
952 with the approximation that its vtable is that of the
953 immediate base class. */
954 return build_primary_vtable (binfo, t);
955 else
956 /* This is our very own copy of `basetype' to play with. Later,
957 we will fill in all the virtual functions that override the
958 virtual functions in these base classes which are not defined
959 by the current type. */
960 return build_secondary_vtable (binfo);
961}
962
963/* Make *VIRTUALS, an entry on the BINFO_VIRTUALS list for BINFO
964 (which is in the hierarchy dominated by T) list FNDECL as its
965 BV_FN. DELTA is the required constant adjustment from the `this'
966 pointer where the vtable entry appears to the `this' required when
967 the function is actually called. */
968
969static void
970modify_vtable_entry (tree t,
971 tree binfo,
972 tree fndecl,
973 tree delta,
974 tree *virtuals)
975{
976 tree v;
977
978 v = *virtuals;
979
980 if (fndecl != BV_FN (v)
981 || !tree_int_cst_equal (delta, BV_DELTA (v)))
982 {
983 /* We need a new vtable for BINFO. */
984 if (make_new_vtable (t, binfo))
985 {
986 /* If we really did make a new vtable, we also made a copy
987 of the BINFO_VIRTUALS list. Now, we have to find the
988 corresponding entry in that list. */
989 *virtuals = BINFO_VIRTUALS (binfo);
990 while (BV_FN (*virtuals) != BV_FN (v))
991 *virtuals = TREE_CHAIN (*virtuals);
992 v = *virtuals;
993 }
994
995 BV_DELTA (v) = delta;
996 BV_VCALL_INDEX (v) = NULL_TREE;
997 BV_FN (v) = fndecl;
998 }
999}
1000
1001
1002/* Add method METHOD to class TYPE. If VIA_USING indicates whether
1003 METHOD is being injected via a using_decl. Returns true if the
1004 method could be added to the method vec. */
1005
1006bool
1007add_method (tree type, tree method, bool via_using)
1008{
1009 if (method == error_mark_node)
1010 return false;
1011
1012 /* Maintain TYPE_HAS_USER_CONSTRUCTOR, etc. */
1013 grok_special_member_properties (method);
1014
1015 tree *slot = get_member_slot (type, DECL_NAME (method));
1016 tree current_fns = *slot;
1017
1018 gcc_assert (!DECL_EXTERN_C_P (method));
1019
1020 /* Check to see if we've already got this method. */
1021 for (ovl_iterator iter (current_fns); iter; ++iter)
1022 {
1023 tree fn = *iter;
1024 tree fn_type;
1025 tree method_type;
1026 tree parms1;
1027 tree parms2;
1028
1029 if (TREE_CODE (fn) != TREE_CODE (method))
1030 continue;
1031
1032 /* Two using-declarations can coexist, we'll complain about ambiguity in
1033 overload resolution. */
1034 if (via_using && iter.using_p ()
1035 /* Except handle inherited constructors specially. */
1036 && ! DECL_CONSTRUCTOR_P (fn))
1037 continue;
1038
1039 /* [over.load] Member function declarations with the
1040 same name and the same parameter types cannot be
1041 overloaded if any of them is a static member
1042 function declaration.
1043
1044 [over.load] Member function declarations with the same name and
1045 the same parameter-type-list as well as member function template
1046 declarations with the same name, the same parameter-type-list, and
1047 the same template parameter lists cannot be overloaded if any of
1048 them, but not all, have a ref-qualifier.
1049
1050 [namespace.udecl] When a using-declaration brings names
1051 from a base class into a derived class scope, member
1052 functions in the derived class override and/or hide member
1053 functions with the same name and parameter types in a base
1054 class (rather than conflicting). */
1055 fn_type = TREE_TYPE (fn);
1056 method_type = TREE_TYPE (method);
1057 parms1 = TYPE_ARG_TYPES (fn_type);
1058 parms2 = TYPE_ARG_TYPES (method_type);
1059
1060 /* Compare the quals on the 'this' parm. Don't compare
1061 the whole types, as used functions are treated as
1062 coming from the using class in overload resolution. */
1063 if (! DECL_STATIC_FUNCTION_P (fn)
1064 && ! DECL_STATIC_FUNCTION_P (method)
1065 /* Either both or neither need to be ref-qualified for
1066 differing quals to allow overloading. */
1067 && (FUNCTION_REF_QUALIFIED (fn_type)
1068 == FUNCTION_REF_QUALIFIED (method_type))
1069 && (type_memfn_quals (fn_type) != type_memfn_quals (method_type)
1070 || type_memfn_rqual (fn_type) != type_memfn_rqual (method_type)))
1071 continue;
1072
1073 /* For templates, the return type and template parameters
1074 must be identical. */
1075 if (TREE_CODE (fn) == TEMPLATE_DECL
1076 && (!same_type_p (TREE_TYPE (fn_type),
1077 TREE_TYPE (method_type))
1078 || !comp_template_parms (DECL_TEMPLATE_PARMS (fn),
1079 DECL_TEMPLATE_PARMS (method))))
1080 continue;
1081
1082 if (! DECL_STATIC_FUNCTION_P (fn))
1083 parms1 = TREE_CHAIN (parms1);
1084 if (! DECL_STATIC_FUNCTION_P (method))
1085 parms2 = TREE_CHAIN (parms2);
1086
1087 /* Bring back parameters omitted from an inherited ctor. */
1088 if (ctor_omit_inherited_parms (fn))
1089 parms1 = FUNCTION_FIRST_USER_PARMTYPE (DECL_ORIGIN (fn));
1090 if (ctor_omit_inherited_parms (method))
1091 parms2 = FUNCTION_FIRST_USER_PARMTYPE (DECL_ORIGIN (method));
1092
1093 if (compparms (parms1, parms2)
1094 && (!DECL_CONV_FN_P (fn)
1095 || same_type_p (TREE_TYPE (fn_type),
1096 TREE_TYPE (method_type)))
1097 && equivalently_constrained (fn, method))
1098 {
1099 /* If these are versions of the same function, process and
1100 move on. */
1101 if (TREE_CODE (fn) == FUNCTION_DECL
1102 && maybe_version_functions (method, fn))
1103 continue;
1104
1105 if (DECL_INHERITED_CTOR (method))
1106 {
1107 if (DECL_INHERITED_CTOR (fn))
1108 {
1109 tree basem = DECL_INHERITED_CTOR_BASE (method);
1110 tree basef = DECL_INHERITED_CTOR_BASE (fn);
1111 if (flag_new_inheriting_ctors)
1112 {
1113 if (basem == basef)
1114 {
1115 /* Inheriting the same constructor along different
1116 paths, combine them. */
1117 SET_DECL_INHERITED_CTOR
1118 (fn, ovl_make (DECL_INHERITED_CTOR (method),
1119 DECL_INHERITED_CTOR (fn)));
1120 /* And discard the new one. */
1121 return false;
1122 }
1123 else
1124 /* Inherited ctors can coexist until overload
1125 resolution. */
1126 continue;
1127 }
1128 error_at (DECL_SOURCE_LOCATION (method),
1129 "%q#D conflicts with version inherited from %qT",
1130 method, basef);
1131 inform (DECL_SOURCE_LOCATION (fn),
1132 "version inherited from %qT declared here",
1133 basef);
1134 }
1135 /* Otherwise defer to the other function. */
1136 return false;
1137 }
1138
1139 if (via_using)
1140 /* Defer to the local function. */
1141 return false;
1142 else if (flag_new_inheriting_ctors
1143 && DECL_INHERITED_CTOR (fn))
1144 {
1145 /* Remove the inherited constructor. */
1146 current_fns = iter.remove_node (current_fns);
1147 continue;
1148 }
1149 else
1150 {
1151 error_at (DECL_SOURCE_LOCATION (method),
1152 "%q#D cannot be overloaded with %q#D", method, fn);
1153 inform (DECL_SOURCE_LOCATION (fn),
1154 "previous declaration %q#D", fn);
1155 return false;
1156 }
1157 }
1158 }
1159
1160 /* A class should never have more than one destructor. */
1161 gcc_assert (!current_fns || !DECL_DESTRUCTOR_P (method));
1162
1163 current_fns = ovl_insert (method, current_fns, via_using);
1164
1165 if (!DECL_CONV_FN_P (method) && !COMPLETE_TYPE_P (type))
1166 push_class_level_binding (DECL_NAME (method), current_fns);
1167
1168 *slot = current_fns;
1169
1170 return true;
1171}
1172
1173/* Subroutines of finish_struct. */
1174
1175/* Change the access of FDECL to ACCESS in T. Return 1 if change was
1176 legit, otherwise return 0. */
1177
1178static int
1179alter_access (tree t, tree fdecl, tree access)
1180{
1181 tree elem;
1182
1183 retrofit_lang_decl (fdecl);
1184
1185 gcc_assert (!DECL_DISCRIMINATOR_P (fdecl));
1186
1187 elem = purpose_member (t, DECL_ACCESS (fdecl));
1188 if (elem)
1189 {
1190 if (TREE_VALUE (elem) != access)
1191 {
1192 if (TREE_CODE (TREE_TYPE (fdecl)) == FUNCTION_DECL)
1193 error ("conflicting access specifications for method"
1194 " %q+D, ignored", TREE_TYPE (fdecl));
1195 else
1196 error ("conflicting access specifications for field %qE, ignored",
1197 DECL_NAME (fdecl));
1198 }
1199 else
1200 {
1201 /* They're changing the access to the same thing they changed
1202 it to before. That's OK. */
1203 ;
1204 }
1205 }
1206 else
1207 {
1208 perform_or_defer_access_check (TYPE_BINFO (t), fdecl, fdecl,
1209 tf_warning_or_error);
1210 DECL_ACCESS (fdecl) = tree_cons (t, access, DECL_ACCESS (fdecl));
1211 return 1;
1212 }
1213 return 0;
1214}
1215
1216/* Return the access node for DECL's access in its enclosing class. */
1217
1218tree
1219declared_access (tree decl)
1220{
1221 return (TREE_PRIVATE (decl) ? access_private_node
1222 : TREE_PROTECTED (decl) ? access_protected_node
1223 : access_public_node);
1224}
1225
1226/* Process the USING_DECL, which is a member of T. */
1227
1228static void
1229handle_using_decl (tree using_decl, tree t)
1230{
1231 tree decl = USING_DECL_DECLS (using_decl);
1232 tree name = DECL_NAME (using_decl);
1233 tree access = declared_access (using_decl);
1234 tree flist = NULL_TREE;
1235 tree old_value;
1236
1237 gcc_assert (!processing_template_decl && decl);
1238
1239 old_value = lookup_member (t, name, /*protect=*/0, /*want_type=*/false,
1240 tf_warning_or_error);
1241 if (old_value)
1242 {
1243 old_value = OVL_FIRST (old_value);
1244
1245 if (DECL_P (old_value) && DECL_CONTEXT (old_value) == t)
1246 /* OK */;
1247 else
1248 old_value = NULL_TREE;
1249 }
1250
1251 cp_emit_debug_info_for_using (decl, t);
1252
1253 if (is_overloaded_fn (decl))
1254 flist = decl;
1255
1256 if (! old_value)
1257 ;
1258 else if (is_overloaded_fn (old_value))
1259 {
1260 if (flist)
1261 /* It's OK to use functions from a base when there are functions with
1262 the same name already present in the current class. */;
1263 else
1264 {
1265 error_at (DECL_SOURCE_LOCATION (using_decl), "%qD invalid in %q#T "
1266 "because of local method %q#D with same name",
1267 using_decl, t, old_value);
1268 inform (DECL_SOURCE_LOCATION (old_value),
1269 "local method %q#D declared here", old_value);
1270 return;
1271 }
1272 }
1273 else if (!DECL_ARTIFICIAL (old_value))
1274 {
1275 error_at (DECL_SOURCE_LOCATION (using_decl), "%qD invalid in %q#T "
1276 "because of local member %q#D with same name",
1277 using_decl, t, old_value);
1278 inform (DECL_SOURCE_LOCATION (old_value),
1279 "local member %q#D declared here", old_value);
1280 return;
1281 }
1282
1283 /* Make type T see field decl FDECL with access ACCESS. */
1284 if (flist)
1285 for (ovl_iterator iter (flist); iter; ++iter)
1286 {
1287 add_method (t, *iter, true);
1288 alter_access (t, *iter, access);
1289 }
1290 else
1291 alter_access (t, decl, access);
1292}
1293
1294/* Data structure for find_abi_tags_r, below. */
1295
1296struct abi_tag_data
1297{
1298 tree t; // The type that we're checking for missing tags.
1299 tree subob; // The subobject of T that we're getting tags from.
1300 tree tags; // error_mark_node for diagnostics, or a list of missing tags.
1301};
1302
1303/* Subroutine of find_abi_tags_r. Handle a single TAG found on the class TP
1304 in the context of P. TAG can be either an identifier (the DECL_NAME of
1305 a tag NAMESPACE_DECL) or a STRING_CST (a tag attribute). */
1306
1307static void
1308check_tag (tree tag, tree id, tree *tp, abi_tag_data *p)
1309{
1310 if (!IDENTIFIER_MARKED (id))
1311 {
1312 if (p->tags != error_mark_node)
1313 {
1314 /* We're collecting tags from template arguments or from
1315 the type of a variable or function return type. */
1316 p->tags = tree_cons (NULL_TREE, tag, p->tags);
1317
1318 /* Don't inherit this tag multiple times. */
1319 IDENTIFIER_MARKED (id) = true;
1320
1321 if (TYPE_P (p->t))
1322 {
1323 /* Tags inherited from type template arguments are only used
1324 to avoid warnings. */
1325 ABI_TAG_IMPLICIT (p->tags) = true;
1326 return;
1327 }
1328 /* For functions and variables we want to warn, too. */
1329 }
1330
1331 /* Otherwise we're diagnosing missing tags. */
1332 if (TREE_CODE (p->t) == FUNCTION_DECL)
1333 {
1334 if (warning (OPT_Wabi_tag, "%qD inherits the %E ABI tag "
1335 "that %qT (used in its return type) has",
1336 p->t, tag, *tp))
1337 inform (location_of (*tp), "%qT declared here", *tp);
1338 }
1339 else if (VAR_P (p->t))
1340 {
1341 if (warning (OPT_Wabi_tag, "%qD inherits the %E ABI tag "
1342 "that %qT (used in its type) has", p->t, tag, *tp))
1343 inform (location_of (*tp), "%qT declared here", *tp);
1344 }
1345 else if (TYPE_P (p->subob))
1346 {
1347 if (warning (OPT_Wabi_tag, "%qT does not have the %E ABI tag "
1348 "that base %qT has", p->t, tag, p->subob))
1349 inform (location_of (p->subob), "%qT declared here",
1350 p->subob);
1351 }
1352 else
1353 {
1354 if (warning (OPT_Wabi_tag, "%qT does not have the %E ABI tag "
1355 "that %qT (used in the type of %qD) has",
1356 p->t, tag, *tp, p->subob))
1357 {
1358 inform (location_of (p->subob), "%qD declared here",
1359 p->subob);
1360 inform (location_of (*tp), "%qT declared here", *tp);
1361 }
1362 }
1363 }
1364}
1365
1366/* Find all the ABI tags in the attribute list ATTR and either call
1367 check_tag (if TP is non-null) or set IDENTIFIER_MARKED to val. */
1368
1369static void
1370mark_or_check_attr_tags (tree attr, tree *tp, abi_tag_data *p, bool val)
1371{
1372 if (!attr)
1373 return;
1374 for (; (attr = lookup_attribute ("abi_tag", attr));
1375 attr = TREE_CHAIN (attr))
1376 for (tree list = TREE_VALUE (attr); list;
1377 list = TREE_CHAIN (list))
1378 {
1379 tree tag = TREE_VALUE (list);
1380 tree id = get_identifier (TREE_STRING_POINTER (tag));
1381 if (tp)
1382 check_tag (tag, id, tp, p);
1383 else
1384 IDENTIFIER_MARKED (id) = val;
1385 }
1386}
1387
1388/* Find all the ABI tags on T and its enclosing scopes and either call
1389 check_tag (if TP is non-null) or set IDENTIFIER_MARKED to val. */
1390
1391static void
1392mark_or_check_tags (tree t, tree *tp, abi_tag_data *p, bool val)
1393{
1394 while (t != global_namespace)
1395 {
1396 tree attr;
1397 if (TYPE_P (t))
1398 {
1399 attr = TYPE_ATTRIBUTES (t);
1400 t = CP_TYPE_CONTEXT (t);
1401 }
1402 else
1403 {
1404 attr = DECL_ATTRIBUTES (t);
1405 t = CP_DECL_CONTEXT (t);
1406 }
1407 mark_or_check_attr_tags (attr, tp, p, val);
1408 }
1409}
1410
1411/* walk_tree callback for check_abi_tags: if the type at *TP involves any
1412 types with ABI tags, add the corresponding identifiers to the VEC in
1413 *DATA and set IDENTIFIER_MARKED. */
1414
1415static tree
1416find_abi_tags_r (tree *tp, int *walk_subtrees, void *data)
1417{
1418 if (!OVERLOAD_TYPE_P (*tp))
1419 return NULL_TREE;
1420
1421 /* walk_tree shouldn't be walking into any subtrees of a RECORD_TYPE
1422 anyway, but let's make sure of it. */
1423 *walk_subtrees = false;
1424
1425 abi_tag_data *p = static_cast<struct abi_tag_data*>(data);
1426
1427 mark_or_check_tags (*tp, tp, p, false);
1428
1429 return NULL_TREE;
1430}
1431
1432/* walk_tree callback for mark_abi_tags: if *TP is a class, set
1433 IDENTIFIER_MARKED on its ABI tags. */
1434
1435static tree
1436mark_abi_tags_r (tree *tp, int *walk_subtrees, void *data)
1437{
1438 if (!OVERLOAD_TYPE_P (*tp))
1439 return NULL_TREE;
1440
1441 /* walk_tree shouldn't be walking into any subtrees of a RECORD_TYPE
1442 anyway, but let's make sure of it. */
1443 *walk_subtrees = false;
1444
1445 bool *valp = static_cast<bool*>(data);
1446
1447 mark_or_check_tags (*tp, NULL, NULL, *valp);
1448
1449 return NULL_TREE;
1450}
1451
1452/* Set IDENTIFIER_MARKED on all the ABI tags on T and its enclosing
1453 scopes. */
1454
1455static void
1456mark_abi_tags (tree t, bool val)
1457{
1458 mark_or_check_tags (t, NULL, NULL, val);
1459 if (DECL_P (t))
1460 {
1461 if (DECL_LANG_SPECIFIC (t) && DECL_USE_TEMPLATE (t)
1462 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (t)))
1463 {
1464 /* Template arguments are part of the signature. */
1465 tree level = INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (t));
1466 for (int j = 0; j < TREE_VEC_LENGTH (level); ++j)
1467 {
1468 tree arg = TREE_VEC_ELT (level, j);
1469 cp_walk_tree_without_duplicates (&arg, mark_abi_tags_r, &val);
1470 }
1471 }
1472 if (TREE_CODE (t) == FUNCTION_DECL)
1473 /* A function's parameter types are part of the signature, so
1474 we don't need to inherit any tags that are also in them. */
1475 for (tree arg = FUNCTION_FIRST_USER_PARMTYPE (t); arg;
1476 arg = TREE_CHAIN (arg))
1477 cp_walk_tree_without_duplicates (&TREE_VALUE (arg),
1478 mark_abi_tags_r, &val);
1479 }
1480}
1481
1482/* Check that T has all the ABI tags that subobject SUBOB has, or
1483 warn if not. If T is a (variable or function) declaration, also
1484 return any missing tags, and add them to T if JUST_CHECKING is false. */
1485
1486static tree
1487check_abi_tags (tree t, tree subob, bool just_checking = false)
1488{
1489 bool inherit = DECL_P (t);
1490
1491 if (!inherit && !warn_abi_tag)
1492 return NULL_TREE;
1493
1494 tree decl = TYPE_P (t) ? TYPE_NAME (t) : t;
1495 if (!TREE_PUBLIC (decl))
1496 /* No need to worry about things local to this TU. */
1497 return NULL_TREE;
1498
1499 mark_abi_tags (t, true);
1500
1501 tree subtype = TYPE_P (subob) ? subob : TREE_TYPE (subob);
1502 struct abi_tag_data data = { t, subob, error_mark_node };
1503 if (inherit)
1504 data.tags = NULL_TREE;
1505
1506 cp_walk_tree_without_duplicates (&subtype, find_abi_tags_r, &data);
1507
1508 if (!(inherit && data.tags))
1509 /* We don't need to do anything with data.tags. */;
1510 else if (just_checking)
1511 for (tree t = data.tags; t; t = TREE_CHAIN (t))
1512 {
1513 tree id = get_identifier (TREE_STRING_POINTER (TREE_VALUE (t)));
1514 IDENTIFIER_MARKED (id) = false;
1515 }
1516 else
1517 {
1518 tree attr = lookup_attribute ("abi_tag", DECL_ATTRIBUTES (t));
1519 if (attr)
1520 TREE_VALUE (attr) = chainon (data.tags, TREE_VALUE (attr));
1521 else
1522 DECL_ATTRIBUTES (t)
1523 = tree_cons (get_identifier ("abi_tag"), data.tags,
1524 DECL_ATTRIBUTES (t));
1525 }
1526
1527 mark_abi_tags (t, false);
1528
1529 return data.tags;
1530}
1531
1532/* Check that DECL has all the ABI tags that are used in parts of its type
1533 that are not reflected in its mangled name. */
1534
1535void
1536check_abi_tags (tree decl)
1537{
1538 if (VAR_P (decl))
1539 check_abi_tags (decl, TREE_TYPE (decl));
1540 else if (TREE_CODE (decl) == FUNCTION_DECL
1541 && !DECL_CONV_FN_P (decl)
1542 && !mangle_return_type_p (decl))
1543 check_abi_tags (decl, TREE_TYPE (TREE_TYPE (decl)));
1544}
1545
1546/* Return any ABI tags that are used in parts of the type of DECL
1547 that are not reflected in its mangled name. This function is only
1548 used in backward-compatible mangling for ABI <11. */
1549
1550tree
1551missing_abi_tags (tree decl)
1552{
1553 if (VAR_P (decl))
1554 return check_abi_tags (decl, TREE_TYPE (decl), true);
1555 else if (TREE_CODE (decl) == FUNCTION_DECL
1556 /* Don't check DECL_CONV_FN_P here like we do in check_abi_tags, so
1557 that we can use this function for setting need_abi_warning
1558 regardless of the current flag_abi_version. */
1559 && !mangle_return_type_p (decl))
1560 return check_abi_tags (decl, TREE_TYPE (TREE_TYPE (decl)), true);
1561 else
1562 return NULL_TREE;
1563}
1564
1565void
1566inherit_targ_abi_tags (tree t)
1567{
1568 if (!CLASS_TYPE_P (t)
1569 || CLASSTYPE_TEMPLATE_INFO (t) == NULL_TREE)
1570 return;
1571
1572 mark_abi_tags (t, true);
1573
1574 tree args = CLASSTYPE_TI_ARGS (t);
1575 struct abi_tag_data data = { t, NULL_TREE, NULL_TREE };
1576 for (int i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
1577 {
1578 tree level = TMPL_ARGS_LEVEL (args, i+1);
1579 for (int j = 0; j < TREE_VEC_LENGTH (level); ++j)
1580 {
1581 tree arg = TREE_VEC_ELT (level, j);
1582 data.subob = arg;
1583 cp_walk_tree_without_duplicates (&arg, find_abi_tags_r, &data);
1584 }
1585 }
1586
1587 // If we found some tags on our template arguments, add them to our
1588 // abi_tag attribute.
1589 if (data.tags)
1590 {
1591 tree attr = lookup_attribute ("abi_tag", TYPE_ATTRIBUTES (t));
1592 if (attr)
1593 TREE_VALUE (attr) = chainon (data.tags, TREE_VALUE (attr));
1594 else
1595 TYPE_ATTRIBUTES (t)
1596 = tree_cons (get_identifier ("abi_tag"), data.tags,
1597 TYPE_ATTRIBUTES (t));
1598 }
1599
1600 mark_abi_tags (t, false);
1601}
1602
1603/* Return true, iff class T has a non-virtual destructor that is
1604 accessible from outside the class heirarchy (i.e. is public, or
1605 there's a suitable friend. */
1606
1607static bool
1608accessible_nvdtor_p (tree t)
1609{
1610 tree dtor = CLASSTYPE_DESTRUCTOR (t);
1611
1612 /* An implicitly declared destructor is always public. And,
1613 if it were virtual, we would have created it by now. */
1614 if (!dtor)
1615 return true;
1616
1617 if (DECL_VINDEX (dtor))
1618 return false; /* Virtual */
1619
1620 if (!TREE_PRIVATE (dtor) && !TREE_PROTECTED (dtor))
1621 return true; /* Public */
1622
1623 if (CLASSTYPE_FRIEND_CLASSES (t)
1624 || DECL_FRIENDLIST (TYPE_MAIN_DECL (t)))
1625 return true; /* Has friends */
1626
1627 return false;
1628}
1629
1630/* Run through the base classes of T, updating CANT_HAVE_CONST_CTOR_P,
1631 and NO_CONST_ASN_REF_P. Also set flag bits in T based on
1632 properties of the bases. */
1633
1634static void
1635check_bases (tree t,
1636 int* cant_have_const_ctor_p,
1637 int* no_const_asn_ref_p)
1638{
1639 int i;
1640 bool seen_non_virtual_nearly_empty_base_p = 0;
1641 int seen_tm_mask = 0;
1642 tree base_binfo;
1643 tree binfo;
1644 tree field = NULL_TREE;
1645
1646 if (!CLASSTYPE_NON_STD_LAYOUT (t))
1647 for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
1648 if (TREE_CODE (field) == FIELD_DECL)
1649 break;
1650
1651 for (binfo = TYPE_BINFO (t), i = 0;
1652 BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
1653 {
1654 tree basetype = TREE_TYPE (base_binfo);
1655
1656 gcc_assert (COMPLETE_TYPE_P (basetype));
1657
1658 if (CLASSTYPE_FINAL (basetype))
1659 error ("cannot derive from %<final%> base %qT in derived type %qT",
1660 basetype, t);
1661
1662 /* If any base class is non-literal, so is the derived class. */
1663 if (!CLASSTYPE_LITERAL_P (basetype))
1664 CLASSTYPE_LITERAL_P (t) = false;
1665
1666 /* If the base class doesn't have copy constructors or
1667 assignment operators that take const references, then the
1668 derived class cannot have such a member automatically
1669 generated. */
1670 if (TYPE_HAS_COPY_CTOR (basetype)
1671 && ! TYPE_HAS_CONST_COPY_CTOR (basetype))
1672 *cant_have_const_ctor_p = 1;
1673 if (TYPE_HAS_COPY_ASSIGN (basetype)
1674 && !TYPE_HAS_CONST_COPY_ASSIGN (basetype))
1675 *no_const_asn_ref_p = 1;
1676
1677 if (BINFO_VIRTUAL_P (base_binfo))
1678 /* A virtual base does not effect nearly emptiness. */
1679 ;
1680 else if (CLASSTYPE_NEARLY_EMPTY_P (basetype))
1681 {
1682 if (seen_non_virtual_nearly_empty_base_p)
1683 /* And if there is more than one nearly empty base, then the
1684 derived class is not nearly empty either. */
1685 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
1686 else
1687 /* Remember we've seen one. */
1688 seen_non_virtual_nearly_empty_base_p = 1;
1689 }
1690 else if (!is_empty_class (basetype))
1691 /* If the base class is not empty or nearly empty, then this
1692 class cannot be nearly empty. */
1693 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
1694
1695 /* A lot of properties from the bases also apply to the derived
1696 class. */
1697 TYPE_NEEDS_CONSTRUCTING (t) |= TYPE_NEEDS_CONSTRUCTING (basetype);
1698 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t)
1699 |= TYPE_HAS_NONTRIVIAL_DESTRUCTOR (basetype);
1700 TYPE_HAS_COMPLEX_COPY_ASSIGN (t)
1701 |= (TYPE_HAS_COMPLEX_COPY_ASSIGN (basetype)
1702 || !TYPE_HAS_COPY_ASSIGN (basetype));
1703 TYPE_HAS_COMPLEX_COPY_CTOR (t) |= (TYPE_HAS_COMPLEX_COPY_CTOR (basetype)
1704 || !TYPE_HAS_COPY_CTOR (basetype));
1705 TYPE_HAS_COMPLEX_MOVE_ASSIGN (t)
1706 |= TYPE_HAS_COMPLEX_MOVE_ASSIGN (basetype);
1707 TYPE_HAS_COMPLEX_MOVE_CTOR (t) |= TYPE_HAS_COMPLEX_MOVE_CTOR (basetype);
1708 TYPE_POLYMORPHIC_P (t) |= TYPE_POLYMORPHIC_P (basetype);
1709 CLASSTYPE_CONTAINS_EMPTY_CLASS_P (t)
1710 |= CLASSTYPE_CONTAINS_EMPTY_CLASS_P (basetype);
1711 TYPE_HAS_COMPLEX_DFLT (t) |= (!TYPE_HAS_DEFAULT_CONSTRUCTOR (basetype)
1712 || TYPE_HAS_COMPLEX_DFLT (basetype));
1713 SET_CLASSTYPE_READONLY_FIELDS_NEED_INIT
1714 (t, CLASSTYPE_READONLY_FIELDS_NEED_INIT (t)
1715 | CLASSTYPE_READONLY_FIELDS_NEED_INIT (basetype));
1716 SET_CLASSTYPE_REF_FIELDS_NEED_INIT
1717 (t, CLASSTYPE_REF_FIELDS_NEED_INIT (t)
1718 | CLASSTYPE_REF_FIELDS_NEED_INIT (basetype));
1719 if (TYPE_HAS_MUTABLE_P (basetype))
1720 CLASSTYPE_HAS_MUTABLE (t) = 1;
1721
1722 /* A standard-layout class is a class that:
1723 ...
1724 * has no non-standard-layout base classes, */
1725 CLASSTYPE_NON_STD_LAYOUT (t) |= CLASSTYPE_NON_STD_LAYOUT (basetype);
1726 if (!CLASSTYPE_NON_STD_LAYOUT (t))
1727 {
1728 tree basefield;
1729 /* ...has no base classes of the same type as the first non-static
1730 data member... */
1731 if (field && DECL_CONTEXT (field) == t
1732 && (same_type_ignoring_top_level_qualifiers_p
1733 (TREE_TYPE (field), basetype)))
1734 CLASSTYPE_NON_STD_LAYOUT (t) = 1;
1735 else
1736 /* ...either has no non-static data members in the most-derived
1737 class and at most one base class with non-static data
1738 members, or has no base classes with non-static data
1739 members */
1740 for (basefield = TYPE_FIELDS (basetype); basefield;
1741 basefield = DECL_CHAIN (basefield))
1742 if (TREE_CODE (basefield) == FIELD_DECL
1743 && !(DECL_FIELD_IS_BASE (basefield)
1744 && integer_zerop (DECL_SIZE (basefield))))
1745 {
1746 if (field)
1747 CLASSTYPE_NON_STD_LAYOUT (t) = 1;
1748 else
1749 field = basefield;
1750 break;
1751 }
1752 }
1753
1754 /* Don't bother collecting tm attributes if transactional memory
1755 support is not enabled. */
1756 if (flag_tm)
1757 {
1758 tree tm_attr = find_tm_attribute (TYPE_ATTRIBUTES (basetype));
1759 if (tm_attr)
1760 seen_tm_mask |= tm_attr_to_mask (tm_attr);
1761 }
1762
1763 check_abi_tags (t, basetype);
1764 }
1765
1766 /* If one of the base classes had TM attributes, and the current class
1767 doesn't define its own, then the current class inherits one. */
1768 if (seen_tm_mask && !find_tm_attribute (TYPE_ATTRIBUTES (t)))
1769 {
1770 tree tm_attr = tm_mask_to_attr (least_bit_hwi (seen_tm_mask));
1771 TYPE_ATTRIBUTES (t) = tree_cons (tm_attr, NULL, TYPE_ATTRIBUTES (t));
1772 }
1773}
1774
1775/* Determine all the primary bases within T. Sets BINFO_PRIMARY_BASE_P for
1776 those that are primaries. Sets BINFO_LOST_PRIMARY_P for those
1777 that have had a nearly-empty virtual primary base stolen by some
1778 other base in the hierarchy. Determines CLASSTYPE_PRIMARY_BASE for
1779 T. */
1780
1781static void
1782determine_primary_bases (tree t)
1783{
1784 unsigned i;
1785 tree primary = NULL_TREE;
1786 tree type_binfo = TYPE_BINFO (t);
1787 tree base_binfo;
1788
1789 /* Determine the primary bases of our bases. */
1790 for (base_binfo = TREE_CHAIN (type_binfo); base_binfo;
1791 base_binfo = TREE_CHAIN (base_binfo))
1792 {
1793 tree primary = CLASSTYPE_PRIMARY_BINFO (BINFO_TYPE (base_binfo));
1794
1795 /* See if we're the non-virtual primary of our inheritance
1796 chain. */
1797 if (!BINFO_VIRTUAL_P (base_binfo))
1798 {
1799 tree parent = BINFO_INHERITANCE_CHAIN (base_binfo);
1800 tree parent_primary = CLASSTYPE_PRIMARY_BINFO (BINFO_TYPE (parent));
1801
1802 if (parent_primary
1803 && SAME_BINFO_TYPE_P (BINFO_TYPE (base_binfo),
1804 BINFO_TYPE (parent_primary)))
1805 /* We are the primary binfo. */
1806 BINFO_PRIMARY_P (base_binfo) = 1;
1807 }
1808 /* Determine if we have a virtual primary base, and mark it so.
1809 */
1810 if (primary && BINFO_VIRTUAL_P (primary))
1811 {
1812 tree this_primary = copied_binfo (primary, base_binfo);
1813
1814 if (BINFO_PRIMARY_P (this_primary))
1815 /* Someone already claimed this base. */
1816 BINFO_LOST_PRIMARY_P (base_binfo) = 1;
1817 else
1818 {
1819 tree delta;
1820
1821 BINFO_PRIMARY_P (this_primary) = 1;
1822 BINFO_INHERITANCE_CHAIN (this_primary) = base_binfo;
1823
1824 /* A virtual binfo might have been copied from within
1825 another hierarchy. As we're about to use it as a
1826 primary base, make sure the offsets match. */
1827 delta = size_diffop_loc (input_location,
1828 fold_convert (ssizetype,
1829 BINFO_OFFSET (base_binfo)),
1830 fold_convert (ssizetype,
1831 BINFO_OFFSET (this_primary)));
1832
1833 propagate_binfo_offsets (this_primary, delta);
1834 }
1835 }
1836 }
1837
1838 /* First look for a dynamic direct non-virtual base. */
1839 for (i = 0; BINFO_BASE_ITERATE (type_binfo, i, base_binfo); i++)
1840 {
1841 tree basetype = BINFO_TYPE (base_binfo);
1842
1843 if (TYPE_CONTAINS_VPTR_P (basetype) && !BINFO_VIRTUAL_P (base_binfo))
1844 {
1845 primary = base_binfo;
1846 goto found;
1847 }
1848 }
1849
1850 /* A "nearly-empty" virtual base class can be the primary base
1851 class, if no non-virtual polymorphic base can be found. Look for
1852 a nearly-empty virtual dynamic base that is not already a primary
1853 base of something in the hierarchy. If there is no such base,
1854 just pick the first nearly-empty virtual base. */
1855
1856 for (base_binfo = TREE_CHAIN (type_binfo); base_binfo;
1857 base_binfo = TREE_CHAIN (base_binfo))
1858 if (BINFO_VIRTUAL_P (base_binfo)
1859 && CLASSTYPE_NEARLY_EMPTY_P (BINFO_TYPE (base_binfo)))
1860 {
1861 if (!BINFO_PRIMARY_P (base_binfo))
1862 {
1863 /* Found one that is not primary. */
1864 primary = base_binfo;
1865 goto found;
1866 }
1867 else if (!primary)
1868 /* Remember the first candidate. */
1869 primary = base_binfo;
1870 }
1871
1872 found:
1873 /* If we've got a primary base, use it. */
1874 if (primary)
1875 {
1876 tree basetype = BINFO_TYPE (primary);
1877
1878 CLASSTYPE_PRIMARY_BINFO (t) = primary;
1879 if (BINFO_PRIMARY_P (primary))
1880 /* We are stealing a primary base. */
1881 BINFO_LOST_PRIMARY_P (BINFO_INHERITANCE_CHAIN (primary)) = 1;
1882 BINFO_PRIMARY_P (primary) = 1;
1883 if (BINFO_VIRTUAL_P (primary))
1884 {
1885 tree delta;
1886
1887 BINFO_INHERITANCE_CHAIN (primary) = type_binfo;
1888 /* A virtual binfo might have been copied from within
1889 another hierarchy. As we're about to use it as a primary
1890 base, make sure the offsets match. */
1891 delta = size_diffop_loc (input_location, ssize_int (0),
1892 fold_convert (ssizetype, BINFO_OFFSET (primary)));
1893
1894 propagate_binfo_offsets (primary, delta);
1895 }
1896
1897 primary = TYPE_BINFO (basetype);
1898
1899 TYPE_VFIELD (t) = TYPE_VFIELD (basetype);
1900 BINFO_VTABLE (type_binfo) = BINFO_VTABLE (primary);
1901 BINFO_VIRTUALS (type_binfo) = BINFO_VIRTUALS (primary);
1902 }
1903}
1904
1905/* Update the variant types of T. */
1906
1907void
1908fixup_type_variants (tree t)
1909{
1910 tree variants;
1911
1912 if (!t)
1913 return;
1914
1915 for (variants = TYPE_NEXT_VARIANT (t);
1916 variants;
1917 variants = TYPE_NEXT_VARIANT (variants))
1918 {
1919 /* These fields are in the _TYPE part of the node, not in
1920 the TYPE_LANG_SPECIFIC component, so they are not shared. */
1921 TYPE_HAS_USER_CONSTRUCTOR (variants) = TYPE_HAS_USER_CONSTRUCTOR (t);
1922 TYPE_NEEDS_CONSTRUCTING (variants) = TYPE_NEEDS_CONSTRUCTING (t);
1923 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (variants)
1924 = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t);
1925
1926 TYPE_POLYMORPHIC_P (variants) = TYPE_POLYMORPHIC_P (t);
1927
1928 TYPE_BINFO (variants) = TYPE_BINFO (t);
1929
1930 /* Copy whatever these are holding today. */
1931 TYPE_VFIELD (variants) = TYPE_VFIELD (t);
1932 TYPE_FIELDS (variants) = TYPE_FIELDS (t);
1933 }
1934}
1935
1936/* KLASS is a class that we're applying may_alias to after the body is
1937 parsed. Fixup any POINTER_TO and REFERENCE_TO types. The
1938 canonical type(s) will be implicitly updated. */
1939
1940static void
1941fixup_may_alias (tree klass)
1942{
1943 tree t, v;
1944
1945 for (t = TYPE_POINTER_TO (klass); t; t = TYPE_NEXT_PTR_TO (t))
1946 for (v = TYPE_MAIN_VARIANT (t); v; v = TYPE_NEXT_VARIANT (v))
1947 TYPE_REF_CAN_ALIAS_ALL (v) = true;
1948 for (t = TYPE_REFERENCE_TO (klass); t; t = TYPE_NEXT_REF_TO (t))
1949 for (v = TYPE_MAIN_VARIANT (t); v; v = TYPE_NEXT_VARIANT (v))
1950 TYPE_REF_CAN_ALIAS_ALL (v) = true;
1951}
1952
1953/* Early variant fixups: we apply attributes at the beginning of the class
1954 definition, and we need to fix up any variants that have already been
1955 made via elaborated-type-specifier so that check_qualified_type works. */
1956
1957void
1958fixup_attribute_variants (tree t)
1959{
1960 tree variants;
1961
1962 if (!t)
1963 return;
1964
1965 tree attrs = TYPE_ATTRIBUTES (t);
1966 unsigned align = TYPE_ALIGN (t);
1967 bool user_align = TYPE_USER_ALIGN (t);
1968 bool may_alias = lookup_attribute ("may_alias", attrs);
1969
1970 if (may_alias)
1971 fixup_may_alias (t);
1972
1973 for (variants = TYPE_NEXT_VARIANT (t);
1974 variants;
1975 variants = TYPE_NEXT_VARIANT (variants))
1976 {
1977 /* These are the two fields that check_qualified_type looks at and
1978 are affected by attributes. */
1979 TYPE_ATTRIBUTES (variants) = attrs;
1980 unsigned valign = align;
1981 if (TYPE_USER_ALIGN (variants))
1982 valign = MAX (valign, TYPE_ALIGN (variants));
1983 else
1984 TYPE_USER_ALIGN (variants) = user_align;
1985 SET_TYPE_ALIGN (variants, valign);
1986 if (may_alias)
1987 fixup_may_alias (variants);
1988 }
1989}
1990
1991/* Set memoizing fields and bits of T (and its variants) for later
1992 use. */
1993
1994static void
1995finish_struct_bits (tree t)
1996{
1997 /* Fix up variants (if any). */
1998 fixup_type_variants (t);
1999
2000 if (BINFO_N_BASE_BINFOS (TYPE_BINFO (t)) && TYPE_POLYMORPHIC_P (t))
2001 /* For a class w/o baseclasses, 'finish_struct' has set
2002 CLASSTYPE_PURE_VIRTUALS correctly (by definition).
2003 Similarly for a class whose base classes do not have vtables.
2004 When neither of these is true, we might have removed abstract
2005 virtuals (by providing a definition), added some (by declaring
2006 new ones), or redeclared ones from a base class. We need to
2007 recalculate what's really an abstract virtual at this point (by
2008 looking in the vtables). */
2009 get_pure_virtuals (t);
2010
2011 /* If this type has a copy constructor or a destructor, force its
2012 mode to be BLKmode, and force its TREE_ADDRESSABLE bit to be
2013 nonzero. This will cause it to be passed by invisible reference
2014 and prevent it from being returned in a register. */
2015 if (type_has_nontrivial_copy_init (t)
2016 || TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t))
2017 {
2018 tree variants;
2019 SET_DECL_MODE (TYPE_MAIN_DECL (t), BLKmode);
2020 for (variants = t; variants; variants = TYPE_NEXT_VARIANT (variants))
2021 {
2022 SET_TYPE_MODE (variants, BLKmode);
2023 TREE_ADDRESSABLE (variants) = 1;
2024 }
2025 }
2026}
2027
2028/* Issue warnings about T having private constructors, but no friends,
2029 and so forth.
2030
2031 HAS_NONPRIVATE_METHOD is nonzero if T has any non-private methods or
2032 static members. HAS_NONPRIVATE_STATIC_FN is nonzero if T has any
2033 non-private static member functions. */
2034
2035static void
2036maybe_warn_about_overly_private_class (tree t)
2037{
2038 int has_member_fn = 0;
2039 int has_nonprivate_method = 0;
2040
2041 if (!warn_ctor_dtor_privacy
2042 /* If the class has friends, those entities might create and
2043 access instances, so we should not warn. */
2044 || (CLASSTYPE_FRIEND_CLASSES (t)
2045 || DECL_FRIENDLIST (TYPE_MAIN_DECL (t)))
2046 /* We will have warned when the template was declared; there's
2047 no need to warn on every instantiation. */
2048 || CLASSTYPE_TEMPLATE_INSTANTIATION (t))
2049 /* There's no reason to even consider warning about this
2050 class. */
2051 return;
2052
2053 /* We only issue one warning, if more than one applies, because
2054 otherwise, on code like:
2055
2056 class A {
2057 // Oops - forgot `public:'
2058 A();
2059 A(const A&);
2060 ~A();
2061 };
2062
2063 we warn several times about essentially the same problem. */
2064
2065 /* Check to see if all (non-constructor, non-destructor) member
2066 functions are private. (Since there are no friends or
2067 non-private statics, we can't ever call any of the private member
2068 functions.) */
2069 for (tree fn = TYPE_FIELDS (t); fn; fn = DECL_CHAIN (fn))
2070 if (!DECL_DECLARES_FUNCTION_P (fn))
2071 /* Not a function. */;
2072 else if (DECL_ARTIFICIAL (fn))
2073 /* We're not interested in compiler-generated methods; they don't
2074 provide any way to call private members. */;
2075 else if (!TREE_PRIVATE (fn))
2076 {
2077 if (DECL_STATIC_FUNCTION_P (fn))
2078 /* A non-private static member function is just like a
2079 friend; it can create and invoke private member
2080 functions, and be accessed without a class
2081 instance. */
2082 return;
2083
2084 has_nonprivate_method = 1;
2085 /* Keep searching for a static member function. */
2086 }
2087 else if (!DECL_CONSTRUCTOR_P (fn) && !DECL_DESTRUCTOR_P (fn))
2088 has_member_fn = 1;
2089
2090 if (!has_nonprivate_method && has_member_fn)
2091 {
2092 /* There are no non-private methods, and there's at least one
2093 private member function that isn't a constructor or
2094 destructor. (If all the private members are
2095 constructors/destructors we want to use the code below that
2096 issues error messages specifically referring to
2097 constructors/destructors.) */
2098 unsigned i;
2099 tree binfo = TYPE_BINFO (t);
2100
2101 for (i = 0; i != BINFO_N_BASE_BINFOS (binfo); i++)
2102 if (BINFO_BASE_ACCESS (binfo, i) != access_private_node)
2103 {
2104 has_nonprivate_method = 1;
2105 break;
2106 }
2107 if (!has_nonprivate_method)
2108 {
2109 warning (OPT_Wctor_dtor_privacy,
2110 "all member functions in class %qT are private", t);
2111 return;
2112 }
2113 }
2114
2115 /* Even if some of the member functions are non-private, the class
2116 won't be useful for much if all the constructors or destructors
2117 are private: such an object can never be created or destroyed. */
2118 if (tree dtor = CLASSTYPE_DESTRUCTOR (t))
2119 if (TREE_PRIVATE (dtor))
2120 {
2121 warning (OPT_Wctor_dtor_privacy,
2122 "%q#T only defines a private destructor and has no friends",
2123 t);
2124 return;
2125 }
2126
2127 /* Warn about classes that have private constructors and no friends. */
2128 if (TYPE_HAS_USER_CONSTRUCTOR (t)
2129 /* Implicitly generated constructors are always public. */
2130 && !CLASSTYPE_LAZY_DEFAULT_CTOR (t))
2131 {
2132 bool nonprivate_ctor = false;
2133 tree copy_or_move = NULL_TREE;
2134
2135 /* If a non-template class does not define a copy
2136 constructor, one is defined for it, enabling it to avoid
2137 this warning. For a template class, this does not
2138 happen, and so we would normally get a warning on:
2139
2140 template <class T> class C { private: C(); };
2141
2142 To avoid this asymmetry, we check TYPE_HAS_COPY_CTOR. All
2143 complete non-template or fully instantiated classes have this
2144 flag set. */
2145 if (!TYPE_HAS_COPY_CTOR (t))
2146 nonprivate_ctor = true;
2147 else
2148 for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (t));
2149 !nonprivate_ctor && iter; ++iter)
2150 if (TREE_PRIVATE (*iter))
2151 continue;
2152 else if (copy_fn_p (*iter) || move_fn_p (*iter))
2153 /* Ideally, we wouldn't count any constructor that takes
2154 an argument of the class type as a parameter, because
2155 such things cannot be used to construct an instance of
2156 the class unless you already have one. */
2157 copy_or_move = *iter;
2158 else
2159 nonprivate_ctor = true;
2160
2161 if (!nonprivate_ctor)
2162 {
2163 warning (OPT_Wctor_dtor_privacy,
2164 "%q#T only defines private constructors and has no friends",
2165 t);
2166 if (copy_or_move)
2167 inform (DECL_SOURCE_LOCATION (copy_or_move),
2168 "%q#D is public, but requires an existing %q#T object",
2169 copy_or_move, t);
2170 return;
2171 }
2172 }
2173}
2174
2175/* Make BINFO's vtable have N entries, including RTTI entries,
2176 vbase and vcall offsets, etc. Set its type and call the back end
2177 to lay it out. */
2178
2179static void
2180layout_vtable_decl (tree binfo, int n)
2181{
2182 tree atype;
2183 tree vtable;
2184
2185 atype = build_array_of_n_type (vtable_entry_type, n);
2186 layout_type (atype);
2187
2188 /* We may have to grow the vtable. */
2189 vtable = get_vtbl_decl_for_binfo (binfo);
2190 if (!same_type_p (TREE_TYPE (vtable), atype))
2191 {
2192 TREE_TYPE (vtable) = atype;
2193 DECL_SIZE (vtable) = DECL_SIZE_UNIT (vtable) = NULL_TREE;
2194 layout_decl (vtable, 0);
2195 }
2196}
2197
2198/* True iff FNDECL and BASE_FNDECL (both non-static member functions)
2199 have the same signature. */
2200
2201int
2202same_signature_p (const_tree fndecl, const_tree base_fndecl)
2203{
2204 /* One destructor overrides another if they are the same kind of
2205 destructor. */
2206 if (DECL_DESTRUCTOR_P (base_fndecl) && DECL_DESTRUCTOR_P (fndecl)
2207 && special_function_p (base_fndecl) == special_function_p (fndecl))
2208 return 1;
2209 /* But a non-destructor never overrides a destructor, nor vice
2210 versa, nor do different kinds of destructors override
2211 one-another. For example, a complete object destructor does not
2212 override a deleting destructor. */
2213 if (DECL_DESTRUCTOR_P (base_fndecl) || DECL_DESTRUCTOR_P (fndecl))
2214 return 0;
2215
2216 if (DECL_NAME (fndecl) == DECL_NAME (base_fndecl)
2217 || (DECL_CONV_FN_P (fndecl)
2218 && DECL_CONV_FN_P (base_fndecl)
2219 && same_type_p (DECL_CONV_FN_TYPE (fndecl),
2220 DECL_CONV_FN_TYPE (base_fndecl))))
2221 {
2222 tree fntype = TREE_TYPE (fndecl);
2223 tree base_fntype = TREE_TYPE (base_fndecl);
2224 if (type_memfn_quals (fntype) == type_memfn_quals (base_fntype)
2225 && type_memfn_rqual (fntype) == type_memfn_rqual (base_fntype)
2226 && compparms (FUNCTION_FIRST_USER_PARMTYPE (fndecl),
2227 FUNCTION_FIRST_USER_PARMTYPE (base_fndecl)))
2228 return 1;
2229 }
2230 return 0;
2231}
2232
2233/* Returns TRUE if DERIVED is a binfo containing the binfo BASE as a
2234 subobject. */
2235
2236static bool
2237base_derived_from (tree derived, tree base)
2238{
2239 tree probe;
2240
2241 for (probe = base; probe; probe = BINFO_INHERITANCE_CHAIN (probe))
2242 {
2243 if (probe == derived)
2244 return true;
2245 else if (BINFO_VIRTUAL_P (probe))
2246 /* If we meet a virtual base, we can't follow the inheritance
2247 any more. See if the complete type of DERIVED contains
2248 such a virtual base. */
2249 return (binfo_for_vbase (BINFO_TYPE (probe), BINFO_TYPE (derived))
2250 != NULL_TREE);
2251 }
2252 return false;
2253}
2254
2255struct find_final_overrider_data {
2256 /* The function for which we are trying to find a final overrider. */
2257 tree fn;
2258 /* The base class in which the function was declared. */
2259 tree declaring_base;
2260 /* The candidate overriders. */
2261 tree candidates;
2262 /* Path to most derived. */
2263 vec<tree> path;
2264};
2265
2266/* Add the overrider along the current path to FFOD->CANDIDATES.
2267 Returns true if an overrider was found; false otherwise. */
2268
2269static bool
2270dfs_find_final_overrider_1 (tree binfo,
2271 find_final_overrider_data *ffod,
2272 unsigned depth)
2273{
2274 tree method;
2275
2276 /* If BINFO is not the most derived type, try a more derived class.
2277 A definition there will overrider a definition here. */
2278 if (depth)
2279 {
2280 depth--;
2281 if (dfs_find_final_overrider_1
2282 (ffod->path[depth], ffod, depth))
2283 return true;
2284 }
2285
2286 method = look_for_overrides_here (BINFO_TYPE (binfo), ffod->fn);
2287 if (method)
2288 {
2289 tree *candidate = &ffod->candidates;
2290
2291 /* Remove any candidates overridden by this new function. */
2292 while (*candidate)
2293 {
2294 /* If *CANDIDATE overrides METHOD, then METHOD
2295 cannot override anything else on the list. */
2296 if (base_derived_from (TREE_VALUE (*candidate), binfo))
2297 return true;
2298 /* If METHOD overrides *CANDIDATE, remove *CANDIDATE. */
2299 if (base_derived_from (binfo, TREE_VALUE (*candidate)))
2300 *candidate = TREE_CHAIN (*candidate);
2301 else
2302 candidate = &TREE_CHAIN (*candidate);
2303 }
2304
2305 /* Add the new function. */
2306 ffod->candidates = tree_cons (method, binfo, ffod->candidates);
2307 return true;
2308 }
2309
2310 return false;
2311}
2312
2313/* Called from find_final_overrider via dfs_walk. */
2314
2315static tree
2316dfs_find_final_overrider_pre (tree binfo, void *data)
2317{
2318 find_final_overrider_data *ffod = (find_final_overrider_data *) data;
2319
2320 if (binfo == ffod->declaring_base)
2321 dfs_find_final_overrider_1 (binfo, ffod, ffod->path.length ());
2322 ffod->path.safe_push (binfo);
2323
2324 return NULL_TREE;
2325}
2326
2327static tree
2328dfs_find_final_overrider_post (tree /*binfo*/, void *data)
2329{
2330 find_final_overrider_data *ffod = (find_final_overrider_data *) data;
2331 ffod->path.pop ();
2332
2333 return NULL_TREE;
2334}
2335
2336/* Returns a TREE_LIST whose TREE_PURPOSE is the final overrider for
2337 FN and whose TREE_VALUE is the binfo for the base where the
2338 overriding occurs. BINFO (in the hierarchy dominated by the binfo
2339 DERIVED) is the base object in which FN is declared. */
2340
2341static tree
2342find_final_overrider (tree derived, tree binfo, tree fn)
2343{
2344 find_final_overrider_data ffod;
2345
2346 /* Getting this right is a little tricky. This is valid:
2347
2348 struct S { virtual void f (); };
2349 struct T { virtual void f (); };
2350 struct U : public S, public T { };
2351
2352 even though calling `f' in `U' is ambiguous. But,
2353
2354 struct R { virtual void f(); };
2355 struct S : virtual public R { virtual void f (); };
2356 struct T : virtual public R { virtual void f (); };
2357 struct U : public S, public T { };
2358
2359 is not -- there's no way to decide whether to put `S::f' or
2360 `T::f' in the vtable for `R'.
2361
2362 The solution is to look at all paths to BINFO. If we find
2363 different overriders along any two, then there is a problem. */
2364 if (DECL_THUNK_P (fn))
2365 fn = THUNK_TARGET (fn);
2366
2367 /* Determine the depth of the hierarchy. */
2368 ffod.fn = fn;
2369 ffod.declaring_base = binfo;
2370 ffod.candidates = NULL_TREE;
2371 ffod.path.create (30);
2372
2373 dfs_walk_all (derived, dfs_find_final_overrider_pre,
2374 dfs_find_final_overrider_post, &ffod);
2375
2376 ffod.path.release ();
2377
2378 /* If there was no winner, issue an error message. */
2379 if (!ffod.candidates || TREE_CHAIN (ffod.candidates))
2380 return error_mark_node;
2381
2382 return ffod.candidates;
2383}
2384
2385/* Return the index of the vcall offset for FN when TYPE is used as a
2386 virtual base. */
2387
2388static tree
2389get_vcall_index (tree fn, tree type)
2390{
2391 vec<tree_pair_s, va_gc> *indices = CLASSTYPE_VCALL_INDICES (type);
2392 tree_pair_p p;
2393 unsigned ix;
2394
2395 FOR_EACH_VEC_SAFE_ELT (indices, ix, p)
2396 if ((DECL_DESTRUCTOR_P (fn) && DECL_DESTRUCTOR_P (p->purpose))
2397 || same_signature_p (fn, p->purpose))
2398 return p->value;
2399
2400 /* There should always be an appropriate index. */
2401 gcc_unreachable ();
2402}
2403
2404/* Update an entry in the vtable for BINFO, which is in the hierarchy
2405 dominated by T. FN is the old function; VIRTUALS points to the
2406 corresponding position in the new BINFO_VIRTUALS list. IX is the index
2407 of that entry in the list. */
2408
2409static void
2410update_vtable_entry_for_fn (tree t, tree binfo, tree fn, tree* virtuals,
2411 unsigned ix)
2412{
2413 tree b;
2414 tree overrider;
2415 tree delta;
2416 tree virtual_base;
2417 tree first_defn;
2418 tree overrider_fn, overrider_target;
2419 tree target_fn = DECL_THUNK_P (fn) ? THUNK_TARGET (fn) : fn;
2420 tree over_return, base_return;
2421 bool lost = false;
2422
2423 /* Find the nearest primary base (possibly binfo itself) which defines
2424 this function; this is the class the caller will convert to when
2425 calling FN through BINFO. */
2426 for (b = binfo; ; b = get_primary_binfo (b))
2427 {
2428 gcc_assert (b);
2429 if (look_for_overrides_here (BINFO_TYPE (b), target_fn))
2430 break;
2431
2432 /* The nearest definition is from a lost primary. */
2433 if (BINFO_LOST_PRIMARY_P (b))
2434 lost = true;
2435 }
2436 first_defn = b;
2437
2438 /* Find the final overrider. */
2439 overrider = find_final_overrider (TYPE_BINFO (t), b, target_fn);
2440 if (overrider == error_mark_node)
2441 {
2442 error ("no unique final overrider for %qD in %qT", target_fn, t);
2443 return;
2444 }
2445 overrider_target = overrider_fn = TREE_PURPOSE (overrider);
2446
2447 /* Check for adjusting covariant return types. */
2448 over_return = TREE_TYPE (TREE_TYPE (overrider_target));
2449 base_return = TREE_TYPE (TREE_TYPE (target_fn));
2450
2451 if (POINTER_TYPE_P (over_return)
2452 && TREE_CODE (over_return) == TREE_CODE (base_return)
2453 && CLASS_TYPE_P (TREE_TYPE (over_return))
2454 && CLASS_TYPE_P (TREE_TYPE (base_return))
2455 /* If the overrider is invalid, don't even try. */
2456 && !DECL_INVALID_OVERRIDER_P (overrider_target))
2457 {
2458 /* If FN is a covariant thunk, we must figure out the adjustment
2459 to the final base FN was converting to. As OVERRIDER_TARGET might
2460 also be converting to the return type of FN, we have to
2461 combine the two conversions here. */
2462 tree fixed_offset, virtual_offset;
2463
2464 over_return = TREE_TYPE (over_return);
2465 base_return = TREE_TYPE (base_return);
2466
2467 if (DECL_THUNK_P (fn))
2468 {
2469 gcc_assert (DECL_RESULT_THUNK_P (fn));
2470 fixed_offset = ssize_int (THUNK_FIXED_OFFSET (fn));
2471 virtual_offset = THUNK_VIRTUAL_OFFSET (fn);
2472 }
2473 else
2474 fixed_offset = virtual_offset = NULL_TREE;
2475
2476 if (virtual_offset)
2477 /* Find the equivalent binfo within the return type of the
2478 overriding function. We will want the vbase offset from
2479 there. */
2480 virtual_offset = binfo_for_vbase (BINFO_TYPE (virtual_offset),
2481 over_return);
2482 else if (!same_type_ignoring_top_level_qualifiers_p
2483 (over_return, base_return))
2484 {
2485 /* There was no existing virtual thunk (which takes
2486 precedence). So find the binfo of the base function's
2487 return type within the overriding function's return type.
2488 Fortunately we know the covariancy is valid (it
2489 has already been checked), so we can just iterate along
2490 the binfos, which have been chained in inheritance graph
2491 order. Of course it is lame that we have to repeat the
2492 search here anyway -- we should really be caching pieces
2493 of the vtable and avoiding this repeated work. */
2494 tree thunk_binfo, base_binfo;
2495
2496 /* Find the base binfo within the overriding function's
2497 return type. We will always find a thunk_binfo, except
2498 when the covariancy is invalid (which we will have
2499 already diagnosed). */
2500 for (base_binfo = TYPE_BINFO (base_return),
2501 thunk_binfo = TYPE_BINFO (over_return);
2502 thunk_binfo;
2503 thunk_binfo = TREE_CHAIN (thunk_binfo))
2504 if (SAME_BINFO_TYPE_P (BINFO_TYPE (thunk_binfo),
2505 BINFO_TYPE (base_binfo)))
2506 break;
2507
2508 /* See if virtual inheritance is involved. */
2509 for (virtual_offset = thunk_binfo;
2510 virtual_offset;
2511 virtual_offset = BINFO_INHERITANCE_CHAIN (virtual_offset))
2512 if (BINFO_VIRTUAL_P (virtual_offset))
2513 break;
2514
2515 if (virtual_offset
2516 || (thunk_binfo && !BINFO_OFFSET_ZEROP (thunk_binfo)))
2517 {
2518 tree offset = fold_convert (ssizetype, BINFO_OFFSET (thunk_binfo));
2519
2520 if (virtual_offset)
2521 {
2522 /* We convert via virtual base. Adjust the fixed
2523 offset to be from there. */
2524 offset =
2525 size_diffop (offset,
2526 fold_convert (ssizetype,
2527 BINFO_OFFSET (virtual_offset)));
2528 }
2529 if (fixed_offset)
2530 /* There was an existing fixed offset, this must be
2531 from the base just converted to, and the base the
2532 FN was thunking to. */
2533 fixed_offset = size_binop (PLUS_EXPR, fixed_offset, offset);
2534 else
2535 fixed_offset = offset;
2536 }
2537 }
2538
2539 if (fixed_offset || virtual_offset)
2540 /* Replace the overriding function with a covariant thunk. We
2541 will emit the overriding function in its own slot as
2542 well. */
2543 overrider_fn = make_thunk (overrider_target, /*this_adjusting=*/0,
2544 fixed_offset, virtual_offset);
2545 }
2546 else
2547 gcc_assert (DECL_INVALID_OVERRIDER_P (overrider_target) ||
2548 !DECL_THUNK_P (fn));
2549
2550 /* If we need a covariant thunk, then we may need to adjust first_defn.
2551 The ABI specifies that the thunks emitted with a function are
2552 determined by which bases the function overrides, so we need to be
2553 sure that we're using a thunk for some overridden base; even if we
2554 know that the necessary this adjustment is zero, there may not be an
2555 appropriate zero-this-adjustment thunk for us to use since thunks for
2556 overriding virtual bases always use the vcall offset.
2557
2558 Furthermore, just choosing any base that overrides this function isn't
2559 quite right, as this slot won't be used for calls through a type that
2560 puts a covariant thunk here. Calling the function through such a type
2561 will use a different slot, and that slot is the one that determines
2562 the thunk emitted for that base.
2563
2564 So, keep looking until we find the base that we're really overriding
2565 in this slot: the nearest primary base that doesn't use a covariant
2566 thunk in this slot. */
2567 if (overrider_target != overrider_fn)
2568 {
2569 if (BINFO_TYPE (b) == DECL_CONTEXT (overrider_target))
2570 /* We already know that the overrider needs a covariant thunk. */
2571 b = get_primary_binfo (b);
2572 for (; ; b = get_primary_binfo (b))
2573 {
2574 tree main_binfo = TYPE_BINFO (BINFO_TYPE (b));
2575 tree bv = chain_index (ix, BINFO_VIRTUALS (main_binfo));
2576 if (!DECL_THUNK_P (TREE_VALUE (bv)))
2577 break;
2578 if (BINFO_LOST_PRIMARY_P (b))
2579 lost = true;
2580 }
2581 first_defn = b;
2582 }
2583
2584 /* Assume that we will produce a thunk that convert all the way to
2585 the final overrider, and not to an intermediate virtual base. */
2586 virtual_base = NULL_TREE;
2587
2588 /* See if we can convert to an intermediate virtual base first, and then
2589 use the vcall offset located there to finish the conversion. */
2590 for (; b; b = BINFO_INHERITANCE_CHAIN (b))
2591 {
2592 /* If we find the final overrider, then we can stop
2593 walking. */
2594 if (SAME_BINFO_TYPE_P (BINFO_TYPE (b),
2595 BINFO_TYPE (TREE_VALUE (overrider))))
2596 break;
2597
2598 /* If we find a virtual base, and we haven't yet found the
2599 overrider, then there is a virtual base between the
2600 declaring base (first_defn) and the final overrider. */
2601 if (BINFO_VIRTUAL_P (b))
2602 {
2603 virtual_base = b;
2604 break;
2605 }
2606 }
2607
2608 /* Compute the constant adjustment to the `this' pointer. The
2609 `this' pointer, when this function is called, will point at BINFO
2610 (or one of its primary bases, which are at the same offset). */
2611 if (virtual_base)
2612 /* The `this' pointer needs to be adjusted from the declaration to
2613 the nearest virtual base. */
2614 delta = size_diffop_loc (input_location,
2615 fold_convert (ssizetype, BINFO_OFFSET (virtual_base)),
2616 fold_convert (ssizetype, BINFO_OFFSET (first_defn)));
2617 else if (lost)
2618 /* If the nearest definition is in a lost primary, we don't need an
2619 entry in our vtable. Except possibly in a constructor vtable,
2620 if we happen to get our primary back. In that case, the offset
2621 will be zero, as it will be a primary base. */
2622 delta = size_zero_node;
2623 else
2624 /* The `this' pointer needs to be adjusted from pointing to
2625 BINFO to pointing at the base where the final overrider
2626 appears. */
2627 delta = size_diffop_loc (input_location,
2628 fold_convert (ssizetype,
2629 BINFO_OFFSET (TREE_VALUE (overrider))),
2630 fold_convert (ssizetype, BINFO_OFFSET (binfo)));
2631
2632 modify_vtable_entry (t, binfo, overrider_fn, delta, virtuals);
2633
2634 if (virtual_base)
2635 BV_VCALL_INDEX (*virtuals)
2636 = get_vcall_index (overrider_target, BINFO_TYPE (virtual_base));
2637 else
2638 BV_VCALL_INDEX (*virtuals) = NULL_TREE;
2639
2640 BV_LOST_PRIMARY (*virtuals) = lost;
2641}
2642
2643/* Called from modify_all_vtables via dfs_walk. */
2644
2645static tree
2646dfs_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
2697static tree
2698modify_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
2740static void
2741get_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
2772void
2773check_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
2816static void
2817warn_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
2881static void
2882finish_struct_anon_r (tree field, bool complain)
2883{
2884 bool is_union = TREE_CODE (TREE_TYPE (field)) == UNION_TYPE;
2885 tree elt = TYPE_FIELDS (TREE_TYPE (field));
2886 for (; elt; elt = DECL_CHAIN (elt))
2887 {
2888 /* We're generally only interested in entities the user
2889 declared, but we also find nested classes by noticing
2890 the TYPE_DECL that we create implicitly. You're
2891 allowed to put one anonymous union inside another,
2892 though, so we explicitly tolerate that. We use
2893 TYPE_UNNAMED_P rather than ANON_AGGR_TYPE_P so that
2894 we also allow unnamed types used for defining fields. */
2895 if (DECL_ARTIFICIAL (elt)
2896 && (!DECL_IMPLICIT_TYPEDEF_P (elt)
2897 || TYPE_UNNAMED_P (TREE_TYPE (elt))))
2898 continue;
2899
2900 if (TREE_CODE (elt) != FIELD_DECL)
2901 {
2902 /* We already complained about static data members in
2903 finish_static_data_member_decl. */
2904 if (complain && !VAR_P (elt))
2905 {
2906 if (is_union)
2907 permerror (DECL_SOURCE_LOCATION (elt),
2908 "%q#D invalid; an anonymous union can "
2909 "only have non-static data members", elt);
2910 else
2911 permerror (DECL_SOURCE_LOCATION (elt),
2912 "%q#D invalid; an anonymous struct can "
2913 "only have non-static data members", elt);
2914 }
2915 continue;
2916 }
2917
2918 if (complain)
2919 {
2920 if (TREE_PRIVATE (elt))
2921 {
2922 if (is_union)
2923 permerror (DECL_SOURCE_LOCATION (elt),
2924 "private member %q#D in anonymous union", elt);
2925 else
2926 permerror (DECL_SOURCE_LOCATION (elt),
2927 "private member %q#D in anonymous struct", elt);
2928 }
2929 else if (TREE_PROTECTED (elt))
2930 {
2931 if (is_union)
2932 permerror (DECL_SOURCE_LOCATION (elt),
2933 "protected member %q#D in anonymous union", elt);
2934 else
2935 permerror (DECL_SOURCE_LOCATION (elt),
2936 "protected member %q#D in anonymous struct", elt);
2937 }
2938 }
2939
2940 TREE_PRIVATE (elt) = TREE_PRIVATE (field);
2941 TREE_PROTECTED (elt) = TREE_PROTECTED (field);
2942
2943 /* Recurse into the anonymous aggregates to handle correctly
2944 access control (c++/24926):
2945
2946 class A {
2947 union {
2948 union {
2949 int i;
2950 };
2951 };
2952 };
2953
2954 int j=A().i; */
2955 if (DECL_NAME (elt) == NULL_TREE
2956 && ANON_AGGR_TYPE_P (TREE_TYPE (elt)))
2957 finish_struct_anon_r (elt, /*complain=*/false);
2958 }
2959}
2960
2961/* Check for things that are invalid. There are probably plenty of other
2962 things we should check for also. */
2963
2964static void
2965finish_struct_anon (tree t)
2966{
2967 for (tree field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
2968 {
2969 if (TREE_STATIC (field))
2970 continue;
2971 if (TREE_CODE (field) != FIELD_DECL)
2972 continue;
2973
2974 if (DECL_NAME (field) == NULL_TREE
2975 && ANON_AGGR_TYPE_P (TREE_TYPE (field)))
2976 finish_struct_anon_r (field, /*complain=*/true);
2977 }
2978}
2979
2980/* Add T to CLASSTYPE_DECL_LIST of current_class_type which
2981 will be used later during class template instantiation.
2982 When FRIEND_P is zero, T can be a static member data (VAR_DECL),
2983 a non-static member data (FIELD_DECL), a member function
2984 (FUNCTION_DECL), a nested type (RECORD_TYPE, ENUM_TYPE),
2985 a typedef (TYPE_DECL) or a member class template (TEMPLATE_DECL)
2986 When FRIEND_P is nonzero, T is either a friend class
2987 (RECORD_TYPE, TEMPLATE_DECL) or a friend function
2988 (FUNCTION_DECL, TEMPLATE_DECL). */
2989
2990void
2991maybe_add_class_template_decl_list (tree type, tree t, int friend_p)
2992{
2993 /* Save some memory by not creating TREE_LIST if TYPE is not template. */
2994 if (CLASSTYPE_TEMPLATE_INFO (type))
2995 CLASSTYPE_DECL_LIST (type)
2996 = tree_cons (friend_p ? NULL_TREE : type,
2997 t, CLASSTYPE_DECL_LIST (type));
2998}
2999
3000/* This function is called from declare_virt_assop_and_dtor via
3001 dfs_walk_all.
3002
3003 DATA is a type that direcly or indirectly inherits the base
3004 represented by BINFO. If BINFO contains a virtual assignment [copy
3005 assignment or move assigment] operator or a virtual constructor,
3006 declare that function in DATA if it hasn't been already declared. */
3007
3008static tree
3009dfs_declare_virt_assop_and_dtor (tree binfo, void *data)
3010{
3011 tree bv, fn, t = (tree)data;
3012 tree opname = assign_op_identifier;
3013
3014 gcc_assert (t && CLASS_TYPE_P (t));
3015 gcc_assert (binfo && TREE_CODE (binfo) == TREE_BINFO);
3016
3017 if (!TYPE_CONTAINS_VPTR_P (BINFO_TYPE (binfo)))
3018 /* A base without a vtable needs no modification, and its bases
3019 are uninteresting. */
3020 return dfs_skip_bases;
3021
3022 if (BINFO_PRIMARY_P (binfo))
3023 /* If this is a primary base, then we have already looked at the
3024 virtual functions of its vtable. */
3025 return NULL_TREE;
3026
3027 for (bv = BINFO_VIRTUALS (binfo); bv; bv = TREE_CHAIN (bv))
3028 {
3029 fn = BV_FN (bv);
3030
3031 if (DECL_NAME (fn) == opname)
3032 {
3033 if (CLASSTYPE_LAZY_COPY_ASSIGN (t))
3034 lazily_declare_fn (sfk_copy_assignment, t);
3035 if (CLASSTYPE_LAZY_MOVE_ASSIGN (t))
3036 lazily_declare_fn (sfk_move_assignment, t);
3037 }
3038 else if (DECL_DESTRUCTOR_P (fn)
3039 && CLASSTYPE_LAZY_DESTRUCTOR (t))
3040 lazily_declare_fn (sfk_destructor, t);
3041 }
3042
3043 return NULL_TREE;
3044}
3045
3046/* If the class type T has a direct or indirect base that contains a
3047 virtual assignment operator or a virtual destructor, declare that
3048 function in T if it hasn't been already declared. */
3049
3050static void
3051declare_virt_assop_and_dtor (tree t)
3052{
3053 if (!(TYPE_POLYMORPHIC_P (t)
3054 && (CLASSTYPE_LAZY_COPY_ASSIGN (t)
3055 || CLASSTYPE_LAZY_MOVE_ASSIGN (t)
3056 || CLASSTYPE_LAZY_DESTRUCTOR (t))))
3057 return;
3058
3059 dfs_walk_all (TYPE_BINFO (t),
3060 dfs_declare_virt_assop_and_dtor,
3061 NULL, t);
3062}
3063
3064/* Declare the inheriting constructor for class T inherited from base
3065 constructor CTOR with the parameter array PARMS of size NPARMS. */
3066
3067static void
3068one_inheriting_sig (tree t, tree ctor, tree *parms, int nparms)
3069{
3070 gcc_assert (TYPE_MAIN_VARIANT (t) == t);
3071
3072 /* We don't declare an inheriting ctor that would be a default,
3073 copy or move ctor for derived or base. */
3074 if (nparms == 0)
3075 return;
3076 if (nparms == 1
3077 && TREE_CODE (parms[0]) == REFERENCE_TYPE)
3078 {
3079 tree parm = TYPE_MAIN_VARIANT (TREE_TYPE (parms[0]));
3080 if (parm == t || parm == DECL_CONTEXT (ctor))
3081 return;
3082 }
3083
3084 tree parmlist = void_list_node;
3085 for (int i = nparms - 1; i >= 0; i--)
3086 parmlist = tree_cons (NULL_TREE, parms[i], parmlist);
3087 tree fn = implicitly_declare_fn (sfk_inheriting_constructor,
3088 t, false, ctor, parmlist);
3089
3090 if (add_method (t, fn, false))
3091 {
3092 DECL_CHAIN (fn) = TYPE_FIELDS (t);
3093 TYPE_FIELDS (t) = fn;
3094 }
3095}
3096
3097/* Declare all the inheriting constructors for class T inherited from base
3098 constructor CTOR. */
3099
3100static void
3101one_inherited_ctor (tree ctor, tree t, tree using_decl)
3102{
3103 tree parms = FUNCTION_FIRST_USER_PARMTYPE (ctor);
3104
3105 if (flag_new_inheriting_ctors)
3106 {
3107 ctor = implicitly_declare_fn (sfk_inheriting_constructor,
3108 t, /*const*/false, ctor, parms);
3109 add_method (t, ctor, using_decl != NULL_TREE);
3110 TYPE_HAS_USER_CONSTRUCTOR (t) = true;
3111 return;
3112 }
3113
3114 tree *new_parms = XALLOCAVEC (tree, list_length (parms));
3115 int i = 0;
3116 for (; parms && parms != void_list_node; parms = TREE_CHAIN (parms))
3117 {
3118 if (TREE_PURPOSE (parms))
3119 one_inheriting_sig (t, ctor, new_parms, i);
3120 new_parms[i++] = TREE_VALUE (parms);
3121 }
3122 one_inheriting_sig (t, ctor, new_parms, i);
3123 if (parms == NULL_TREE)
3124 {
3125 if (warning (OPT_Winherited_variadic_ctor,
3126 "the ellipsis in %qD is not inherited", ctor))
3127 inform (DECL_SOURCE_LOCATION (ctor), "%qD declared here", ctor);
3128 }
3129}
3130
3131/* Create default constructors, assignment operators, and so forth for
3132 the type indicated by T, if they are needed. CANT_HAVE_CONST_CTOR,
3133 and CANT_HAVE_CONST_ASSIGNMENT are nonzero if, for whatever reason,
3134 the class cannot have a default constructor, copy constructor
3135 taking a const reference argument, or an assignment operator taking
3136 a const reference, respectively. */
3137
3138static void
3139add_implicitly_declared_members (tree t, tree* access_decls,
3140 int cant_have_const_cctor,
3141 int cant_have_const_assignment)
3142{
3143 /* Destructor. */
3144 if (!CLASSTYPE_DESTRUCTOR (t))
3145 /* In general, we create destructors lazily. */
3146 CLASSTYPE_LAZY_DESTRUCTOR (t) = 1;
3147
3148 bool move_ok = false;
3149 if (cxx_dialect >= cxx11 && CLASSTYPE_LAZY_DESTRUCTOR (t)
3150 && !TYPE_HAS_COPY_CTOR (t) && !TYPE_HAS_COPY_ASSIGN (t)
3151 && !classtype_has_move_assign_or_move_ctor_p (t, false))
3152 move_ok = true;
3153
3154 /* [class.ctor]
3155
3156 If there is no user-declared constructor for a class, a default
3157 constructor is implicitly declared. */
3158 if (! TYPE_HAS_USER_CONSTRUCTOR (t))
3159 {
3160 TYPE_HAS_DEFAULT_CONSTRUCTOR (t) = 1;
3161 CLASSTYPE_LAZY_DEFAULT_CTOR (t) = 1;
3162 if (cxx_dialect >= cxx11)
3163 TYPE_HAS_CONSTEXPR_CTOR (t)
3164 /* Don't force the declaration to get a hard answer; if the
3165 definition would have made the class non-literal, it will still be
3166 non-literal because of the base or member in question, and that
3167 gives a better diagnostic. */
3168 = type_maybe_constexpr_default_constructor (t);
3169 }
3170
3171 /* [class.ctor]
3172
3173 If a class definition does not explicitly declare a copy
3174 constructor, one is declared implicitly. */
3175 if (! TYPE_HAS_COPY_CTOR (t))
3176 {
3177 TYPE_HAS_COPY_CTOR (t) = 1;
3178 TYPE_HAS_CONST_COPY_CTOR (t) = !cant_have_const_cctor;
3179 CLASSTYPE_LAZY_COPY_CTOR (t) = 1;
3180 if (move_ok)
3181 CLASSTYPE_LAZY_MOVE_CTOR (t) = 1;
3182 }
3183
3184 /* If there is no assignment operator, one will be created if and
3185 when it is needed. For now, just record whether or not the type
3186 of the parameter to the assignment operator will be a const or
3187 non-const reference. */
3188 if (!TYPE_HAS_COPY_ASSIGN (t))
3189 {
3190 TYPE_HAS_COPY_ASSIGN (t) = 1;
3191 TYPE_HAS_CONST_COPY_ASSIGN (t) = !cant_have_const_assignment;
3192 CLASSTYPE_LAZY_COPY_ASSIGN (t) = 1;
3193 if (move_ok && !LAMBDA_TYPE_P (t))
3194 CLASSTYPE_LAZY_MOVE_ASSIGN (t) = 1;
3195 }
3196
3197 /* We can't be lazy about declaring functions that might override
3198 a virtual function from a base class. */
3199 declare_virt_assop_and_dtor (t);
3200
3201 while (*access_decls)
3202 {
3203 tree using_decl = TREE_VALUE (*access_decls);
3204 tree decl = USING_DECL_DECLS (using_decl);
3205 if (DECL_NAME (using_decl) == ctor_identifier)
3206 {
3207 /* declare, then remove the decl */
3208 tree ctor_list = decl;
3209 location_t loc = input_location;
3210 input_location = DECL_SOURCE_LOCATION (using_decl);
3211 for (ovl_iterator iter (ctor_list); iter; ++iter)
3212 one_inherited_ctor (*iter, t, using_decl);
3213 *access_decls = TREE_CHAIN (*access_decls);
3214 input_location = loc;
3215 }
3216 else
3217 access_decls = &TREE_CHAIN (*access_decls);
3218 }
3219}
3220
3221/* FIELD is a bit-field. We are finishing the processing for its
3222 enclosing type. Issue any appropriate messages and set appropriate
3223 flags. Returns false if an error has been diagnosed. */
3224
3225static bool
3226check_bitfield_decl (tree field)
3227{
3228 tree type = TREE_TYPE (field);
3229 tree w;
3230
3231 /* Extract the declared width of the bitfield, which has been
3232 temporarily stashed in DECL_BIT_FIELD_REPRESENTATIVE by grokbitfield. */
3233 w = DECL_BIT_FIELD_REPRESENTATIVE (field);
3234 gcc_assert (w != NULL_TREE);
3235 /* Remove the bit-field width indicator so that the rest of the
3236 compiler does not treat that value as a qualifier. */
3237 DECL_BIT_FIELD_REPRESENTATIVE (field) = NULL_TREE;
3238
3239 /* Detect invalid bit-field type. */
3240 if (!INTEGRAL_OR_ENUMERATION_TYPE_P (type))
3241 {
3242 error ("bit-field %q+#D with non-integral type", field);
3243 w = error_mark_node;
3244 }
3245 else
3246 {
3247 location_t loc = input_location;
3248 /* Avoid the non_lvalue wrapper added by fold for PLUS_EXPRs. */
3249 STRIP_NOPS (w);
3250
3251 /* detect invalid field size. */
3252 input_location = DECL_SOURCE_LOCATION (field);
3253 w = cxx_constant_value (w);
3254 input_location = loc;
3255
3256 if (TREE_CODE (w) != INTEGER_CST)
3257 {
3258 error ("bit-field %q+D width not an integer constant", field);
3259 w = error_mark_node;
3260 }
3261 else if (tree_int_cst_sgn (w) < 0)
3262 {
3263 error ("negative width in bit-field %q+D", field);
3264 w = error_mark_node;
3265 }
3266 else if (integer_zerop (w) && DECL_NAME (field) != 0)
3267 {
3268 error ("zero width for bit-field %q+D", field);
3269 w = error_mark_node;
3270 }
3271 else if ((TREE_CODE (type) != ENUMERAL_TYPE
3272 && TREE_CODE (type) != BOOLEAN_TYPE
3273 && compare_tree_int (w, TYPE_PRECISION (type)) > 0)
3274 || ((TREE_CODE (type) == ENUMERAL_TYPE
3275 || TREE_CODE (type) == BOOLEAN_TYPE)
3276 && tree_int_cst_lt (TYPE_SIZE (type), w)))
3277 warning_at (DECL_SOURCE_LOCATION (field), 0,
3278 "width of %qD exceeds its type", field);
3279 else if (TREE_CODE (type) == ENUMERAL_TYPE
3280 && (0 > (compare_tree_int
3281 (w, TYPE_PRECISION (ENUM_UNDERLYING_TYPE (type))))))
3282 warning_at (DECL_SOURCE_LOCATION (field), 0,
3283 "%qD is too small to hold all values of %q#T",
3284 field, type);
3285 }
3286
3287 if (w != error_mark_node)
3288 {
3289 DECL_SIZE (field) = fold_convert (bitsizetype, w);
3290 DECL_BIT_FIELD (field) = 1;
3291 return true;
3292 }
3293 else
3294 {
3295 /* Non-bit-fields are aligned for their type. */
3296 DECL_BIT_FIELD (field) = 0;
3297 CLEAR_DECL_C_BIT_FIELD (field);
3298 return false;
3299 }
3300}
3301
3302/* FIELD is a non bit-field. We are finishing the processing for its
3303 enclosing type T. Issue any appropriate messages and set appropriate
3304 flags. */
3305
3306static bool
3307check_field_decl (tree field,
3308 tree t,
3309 int* cant_have_const_ctor,
3310 int* no_const_asn_ref)
3311{
3312 tree type = strip_array_types (TREE_TYPE (field));
3313 bool any_default_members = false;
3314
3315 /* In C++98 an anonymous union cannot contain any fields which would change
3316 the settings of CANT_HAVE_CONST_CTOR and friends. */
3317 if (ANON_UNION_TYPE_P (type) && cxx_dialect < cxx11)
3318 ;
3319 /* And, we don't set TYPE_HAS_CONST_COPY_CTOR, etc., for anonymous
3320 structs. So, we recurse through their fields here. */
3321 else if (ANON_AGGR_TYPE_P (type))
3322 {
3323 for (tree fields = TYPE_FIELDS (type); fields;
3324 fields = DECL_CHAIN (fields))
3325 if (TREE_CODE (fields) == FIELD_DECL)
3326 any_default_members |= check_field_decl (fields, t,
3327 cant_have_const_ctor,
3328 no_const_asn_ref);
3329 }
3330 /* Check members with class type for constructors, destructors,
3331 etc. */
3332 else if (CLASS_TYPE_P (type))
3333 {
3334 /* Never let anything with uninheritable virtuals
3335 make it through without complaint. */
3336 abstract_virtuals_error (field, type);
3337
3338 if (TREE_CODE (t) == UNION_TYPE && cxx_dialect < cxx11)
3339 {
3340 static bool warned;
3341 int oldcount = errorcount;
3342 if (TYPE_NEEDS_CONSTRUCTING (type))
3343 error ("member %q+#D with constructor not allowed in union",
3344 field);
3345 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
3346 error ("member %q+#D with destructor not allowed in union", field);
3347 if (TYPE_HAS_COMPLEX_COPY_ASSIGN (type))
3348 error ("member %q+#D with copy assignment operator not allowed in union",
3349 field);
3350 if (!warned && errorcount > oldcount)
3351 {
3352 inform (DECL_SOURCE_LOCATION (field), "unrestricted unions "
3353 "only available with -std=c++11 or -std=gnu++11");
3354 warned = true;
3355 }
3356 }
3357 else
3358 {
3359 TYPE_NEEDS_CONSTRUCTING (t) |= TYPE_NEEDS_CONSTRUCTING (type);
3360 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t)
3361 |= TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type);
3362 TYPE_HAS_COMPLEX_COPY_ASSIGN (t)
3363 |= (TYPE_HAS_COMPLEX_COPY_ASSIGN (type)
3364 || !TYPE_HAS_COPY_ASSIGN (type));
3365 TYPE_HAS_COMPLEX_COPY_CTOR (t) |= (TYPE_HAS_COMPLEX_COPY_CTOR (type)
3366 || !TYPE_HAS_COPY_CTOR (type));
3367 TYPE_HAS_COMPLEX_MOVE_ASSIGN (t) |= TYPE_HAS_COMPLEX_MOVE_ASSIGN (type);
3368 TYPE_HAS_COMPLEX_MOVE_CTOR (t) |= TYPE_HAS_COMPLEX_MOVE_CTOR (type);
3369 TYPE_HAS_COMPLEX_DFLT (t) |= (!TYPE_HAS_DEFAULT_CONSTRUCTOR (type)
3370 || TYPE_HAS_COMPLEX_DFLT (type));
3371 }
3372
3373 if (TYPE_HAS_COPY_CTOR (type)
3374 && !TYPE_HAS_CONST_COPY_CTOR (type))
3375 *cant_have_const_ctor = 1;
3376
3377 if (TYPE_HAS_COPY_ASSIGN (type)
3378 && !TYPE_HAS_CONST_COPY_ASSIGN (type))
3379 *no_const_asn_ref = 1;
3380 }
3381
3382 check_abi_tags (t, field);
3383
3384 if (DECL_INITIAL (field) != NULL_TREE)
3385 /* `build_class_init_list' does not recognize
3386 non-FIELD_DECLs. */
3387 any_default_members = true;
3388
3389 return any_default_members;
3390}
3391
3392/* Check the data members (both static and non-static), class-scoped
3393 typedefs, etc., appearing in the declaration of T. Issue
3394 appropriate diagnostics. Sets ACCESS_DECLS to a list (in
3395 declaration order) of access declarations; each TREE_VALUE in this
3396 list is a USING_DECL.
3397
3398 In addition, set the following flags:
3399
3400 EMPTY_P
3401 The class is empty, i.e., contains no non-static data members.
3402
3403 CANT_HAVE_CONST_CTOR_P
3404 This class cannot have an implicitly generated copy constructor
3405 taking a const reference.
3406
3407 CANT_HAVE_CONST_ASN_REF
3408 This class cannot have an implicitly generated assignment
3409 operator taking a const reference.
3410
3411 All of these flags should be initialized before calling this
3412 function.
3413
3414 Returns a pointer to the end of the TYPE_FIELDs chain; additional
3415 fields can be added by adding to this chain. */
3416
3417static void
3418check_field_decls (tree t, tree *access_decls,
3419 int *cant_have_const_ctor_p,
3420 int *no_const_asn_ref_p)
3421{
3422 tree *field;
3423 tree *next;
3424 bool has_pointers;
3425 bool any_default_members;
3426 int cant_pack = 0;
3427 int field_access = -1;
3428
3429 /* Assume there are no access declarations. */
3430 *access_decls = NULL_TREE;
3431 /* Assume this class has no pointer members. */
3432 has_pointers = false;
3433 /* Assume none of the members of this class have default
3434 initializations. */
3435 any_default_members = false;
3436
3437 for (field = &TYPE_FIELDS (t); *field; field = next)
3438 {
3439 tree x = *field;
3440 tree type = TREE_TYPE (x);
3441 int this_field_access;
3442
3443 next = &DECL_CHAIN (x);
3444
3445 if (TREE_CODE (x) == USING_DECL)
3446 {
3447 /* Save the access declarations for our caller. */
3448 *access_decls = tree_cons (NULL_TREE, x, *access_decls);
3449 continue;
3450 }
3451
3452 if (TREE_CODE (x) == TYPE_DECL
3453 || TREE_CODE (x) == TEMPLATE_DECL)
3454 continue;
3455
3456 if (TREE_CODE (x) == FUNCTION_DECL)
3457 /* FIXME: We should fold in the checking from check_methods. */
3458 continue;
3459
3460 /* If we've gotten this far, it's a data member, possibly static,
3461 or an enumerator. */
3462 if (TREE_CODE (x) != CONST_DECL)
3463 DECL_CONTEXT (x) = t;
3464
3465 /* When this goes into scope, it will be a non-local reference. */
3466 DECL_NONLOCAL (x) = 1;
3467
3468 if (TREE_CODE (t) == UNION_TYPE)
3469 {
3470 /* [class.union] (C++98)
3471
3472 If a union contains a static data member, or a member of
3473 reference type, the program is ill-formed.
3474
3475 In C++11 [class.union] says:
3476 If a union contains a non-static data member of reference type
3477 the program is ill-formed. */
3478 if (VAR_P (x) && cxx_dialect < cxx11)
3479 {
3480 error ("in C++98 %q+D may not be static because it is "
3481 "a member of a union", x);
3482 continue;
3483 }
3484 if (TREE_CODE (type) == REFERENCE_TYPE
3485 && TREE_CODE (x) == FIELD_DECL)
3486 {
3487 error ("non-static data member %q+D in a union may not "
3488 "have reference type %qT", x, type);
3489 continue;
3490 }
3491 }
3492
3493 /* Perform error checking that did not get done in
3494 grokdeclarator. */
3495 if (TREE_CODE (type) == FUNCTION_TYPE)
3496 {
3497 error ("field %q+D invalidly declared function type", x);
3498 type = build_pointer_type (type);
3499 TREE_TYPE (x) = type;
3500 }
3501 else if (TREE_CODE (type) == METHOD_TYPE)
3502 {
3503 error ("field %q+D invalidly declared method type", x);
3504 type = build_pointer_type (type);
3505 TREE_TYPE (x) = type;
3506 }
3507
3508 if (type == error_mark_node)
3509 continue;
3510
3511 if (TREE_CODE (x) == CONST_DECL || VAR_P (x))
3512 continue;
3513
3514 /* Now it can only be a FIELD_DECL. */
3515
3516 if (TREE_PRIVATE (x) || TREE_PROTECTED (x))
3517 CLASSTYPE_NON_AGGREGATE (t) = 1;
3518
3519 /* If at least one non-static data member is non-literal, the whole
3520 class becomes non-literal. Per Core/1453, volatile non-static
3521 data members and base classes are also not allowed.
3522 Note: if the type is incomplete we will complain later on. */
3523 if (COMPLETE_TYPE_P (type)
3524 && (!literal_type_p (type) || CP_TYPE_VOLATILE_P (type)))
3525 CLASSTYPE_LITERAL_P (t) = false;
3526
3527 /* A standard-layout class is a class that:
3528 ...
3529 has the same access control (Clause 11) for all non-static data members,
3530 ... */
3531 this_field_access = TREE_PROTECTED (x) ? 1 : TREE_PRIVATE (x) ? 2 : 0;
3532 if (field_access == -1)
3533 field_access = this_field_access;
3534 else if (this_field_access != field_access)
3535 CLASSTYPE_NON_STD_LAYOUT (t) = 1;
3536
3537 /* If this is of reference type, check if it needs an init. */
3538 if (TREE_CODE (type) == REFERENCE_TYPE)
3539 {
3540 CLASSTYPE_NON_LAYOUT_POD_P (t) = 1;
3541 CLASSTYPE_NON_STD_LAYOUT (t) = 1;
3542 if (DECL_INITIAL (x) == NULL_TREE)
3543 SET_CLASSTYPE_REF_FIELDS_NEED_INIT (t, 1);
3544 if (cxx_dialect < cxx11)
3545 {
3546 /* ARM $12.6.2: [A member initializer list] (or, for an
3547 aggregate, initialization by a brace-enclosed list) is the
3548 only way to initialize nonstatic const and reference
3549 members. */
3550 TYPE_HAS_COMPLEX_COPY_ASSIGN (t) = 1;
3551 TYPE_HAS_COMPLEX_MOVE_ASSIGN (t) = 1;
3552 }
3553 }
3554
3555 type = strip_array_types (type);
3556
3557 if (TYPE_PACKED (t))
3558 {
3559 if (!layout_pod_type_p (type) && !TYPE_PACKED (type))
3560 {
3561 warning_at
3562 (DECL_SOURCE_LOCATION (x), 0,
3563 "ignoring packed attribute because of unpacked non-POD field %q#D",
3564 x);
3565 cant_pack = 1;
3566 }
3567 else if (DECL_C_BIT_FIELD (x)
3568 || TYPE_ALIGN (TREE_TYPE (x)) > BITS_PER_UNIT)
3569 DECL_PACKED (x) = 1;
3570 }
3571
3572 if (DECL_C_BIT_FIELD (x)
3573 && integer_zerop (DECL_BIT_FIELD_REPRESENTATIVE (x)))
3574 /* We don't treat zero-width bitfields as making a class
3575 non-empty. */
3576 ;
3577 else
3578 {
3579 /* The class is non-empty. */
3580 CLASSTYPE_EMPTY_P (t) = 0;
3581 /* The class is not even nearly empty. */
3582 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
3583 /* If one of the data members contains an empty class,
3584 so does T. */
3585 if (CLASS_TYPE_P (type)
3586 && CLASSTYPE_CONTAINS_EMPTY_CLASS_P (type))
3587 CLASSTYPE_CONTAINS_EMPTY_CLASS_P (t) = 1;
3588 }
3589
3590 /* This is used by -Weffc++ (see below). Warn only for pointers
3591 to members which might hold dynamic memory. So do not warn
3592 for pointers to functions or pointers to members. */
3593 if (TYPE_PTR_P (type)
3594 && !TYPE_PTRFN_P (type))
3595 has_pointers = true;
3596
3597 if (CLASS_TYPE_P (type))
3598 {
3599 if (CLASSTYPE_REF_FIELDS_NEED_INIT (type))
3600 SET_CLASSTYPE_REF_FIELDS_NEED_INIT (t, 1);
3601 if (CLASSTYPE_READONLY_FIELDS_NEED_INIT (type))
3602 SET_CLASSTYPE_READONLY_FIELDS_NEED_INIT (t, 1);
3603 }
3604
3605 if (DECL_MUTABLE_P (x) || TYPE_HAS_MUTABLE_P (type))
3606 CLASSTYPE_HAS_MUTABLE (t) = 1;
3607
3608 if (DECL_MUTABLE_P (x))
3609 {
3610 if (CP_TYPE_CONST_P (type))
3611 {
3612 error ("member %q+D cannot be declared both %<const%> "
3613 "and %<mutable%>", x);
3614 continue;
3615 }
3616 if (TREE_CODE (type) == REFERENCE_TYPE)
3617 {
3618 error ("member %q+D cannot be declared as a %<mutable%> "
3619 "reference", x);
3620 continue;
3621 }
3622 }
3623
3624 if (! layout_pod_type_p (type))
3625 /* DR 148 now allows pointers to members (which are POD themselves),
3626 to be allowed in POD structs. */
3627 CLASSTYPE_NON_LAYOUT_POD_P (t) = 1;
3628
3629 if (!std_layout_type_p (type))
3630 CLASSTYPE_NON_STD_LAYOUT (t) = 1;
3631
3632 if (! zero_init_p (type))
3633 CLASSTYPE_NON_ZERO_INIT_P (t) = 1;
3634
3635 /* We set DECL_C_BIT_FIELD in grokbitfield.
3636 If the type and width are valid, we'll also set DECL_BIT_FIELD. */
3637 if (DECL_C_BIT_FIELD (x))
3638 check_bitfield_decl (x);
3639
3640 if (check_field_decl (x, t, cant_have_const_ctor_p, no_const_asn_ref_p))
3641 {
3642 if (any_default_members
3643 && TREE_CODE (t) == UNION_TYPE)
3644 error ("multiple fields in union %qT initialized", t);
3645 any_default_members = true;
3646 }
3647
3648 /* Now that we've removed bit-field widths from DECL_INITIAL,
3649 anything left in DECL_INITIAL is an NSDMI that makes the class
3650 non-aggregate in C++11. */
3651 if (DECL_INITIAL (x) && cxx_dialect < cxx14)
3652 CLASSTYPE_NON_AGGREGATE (t) = true;
3653
3654 /* If any field is const, the structure type is pseudo-const. */
3655 if (CP_TYPE_CONST_P (type))
3656 {
3657 C_TYPE_FIELDS_READONLY (t) = 1;
3658 if (DECL_INITIAL (x) == NULL_TREE)
3659 SET_CLASSTYPE_READONLY_FIELDS_NEED_INIT (t, 1);
3660 if (cxx_dialect < cxx11)
3661 {
3662 /* ARM $12.6.2: [A member initializer list] (or, for an
3663 aggregate, initialization by a brace-enclosed list) is the
3664 only way to initialize nonstatic const and reference
3665 members. */
3666 TYPE_HAS_COMPLEX_COPY_ASSIGN (t) = 1;
3667 TYPE_HAS_COMPLEX_MOVE_ASSIGN (t) = 1;
3668 }
3669 }
3670 /* A field that is pseudo-const makes the structure likewise. */
3671 else if (CLASS_TYPE_P (type))
3672 {
3673 C_TYPE_FIELDS_READONLY (t) |= C_TYPE_FIELDS_READONLY (type);
3674 SET_CLASSTYPE_READONLY_FIELDS_NEED_INIT (t,
3675 CLASSTYPE_READONLY_FIELDS_NEED_INIT (t)
3676 | CLASSTYPE_READONLY_FIELDS_NEED_INIT (type));
3677 }
3678
3679 /* Core issue 80: A nonstatic data member is required to have a
3680 different name from the class iff the class has a
3681 user-declared constructor. */
3682 if (constructor_name_p (DECL_NAME (x), t)
3683 && TYPE_HAS_USER_CONSTRUCTOR (t))
3684 permerror (DECL_SOURCE_LOCATION (x),
3685 "field %q#D with same name as class", x);
3686 }
3687
3688 /* Effective C++ rule 11: if a class has dynamic memory held by pointers,
3689 it should also define a copy constructor and an assignment operator to
3690 implement the correct copy semantic (deep vs shallow, etc.). As it is
3691 not feasible to check whether the constructors do allocate dynamic memory
3692 and store it within members, we approximate the warning like this:
3693
3694 -- Warn only if there are members which are pointers
3695 -- Warn only if there is a non-trivial constructor (otherwise,
3696 there cannot be memory allocated).
3697 -- Warn only if there is a non-trivial destructor. We assume that the
3698 user at least implemented the cleanup correctly, and a destructor
3699 is needed to free dynamic memory.
3700
3701 This seems enough for practical purposes. */
3702 if (warn_ecpp
3703 && has_pointers
3704 && TYPE_HAS_USER_CONSTRUCTOR (t)
3705 && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t)
3706 && !(TYPE_HAS_COPY_CTOR (t) && TYPE_HAS_COPY_ASSIGN (t)))
3707 {
3708 warning (OPT_Weffc__, "%q#T has pointer data members", t);
3709
3710 if (! TYPE_HAS_COPY_CTOR (t))
3711 {
3712 warning (OPT_Weffc__,
3713 " but does not override %<%T(const %T&)%>", t, t);
3714 if (!TYPE_HAS_COPY_ASSIGN (t))
3715 warning (OPT_Weffc__, " or %<operator=(const %T&)%>", t);
3716 }
3717 else if (! TYPE_HAS_COPY_ASSIGN (t))
3718 warning (OPT_Weffc__,
3719 " but does not override %<operator=(const %T&)%>", t);
3720 }
3721
3722 /* Non-static data member initializers make the default constructor
3723 non-trivial. */
3724 if (any_default_members)
3725 {
3726 TYPE_NEEDS_CONSTRUCTING (t) = true;
3727 TYPE_HAS_COMPLEX_DFLT (t) = true;
3728 }
3729
3730 /* If any of the fields couldn't be packed, unset TYPE_PACKED. */
3731 if (cant_pack)
3732 TYPE_PACKED (t) = 0;
3733
3734 /* Check anonymous struct/anonymous union fields. */
3735 finish_struct_anon (t);
3736
3737 /* We've built up the list of access declarations in reverse order.
3738 Fix that now. */
3739 *access_decls = nreverse (*access_decls);
3740}
3741
3742/* If TYPE is an empty class type, records its OFFSET in the table of
3743 OFFSETS. */
3744
3745static int
3746record_subobject_offset (tree type, tree offset, splay_tree offsets)
3747{
3748 splay_tree_node n;
3749
3750 if (!is_empty_class (type))
3751 return 0;
3752
3753 /* Record the location of this empty object in OFFSETS. */
3754 n = splay_tree_lookup (offsets, (splay_tree_key) offset);
3755 if (!n)
3756 n = splay_tree_insert (offsets,
3757 (splay_tree_key) offset,
3758 (splay_tree_value) NULL_TREE);
3759 n->value = ((splay_tree_value)
3760 tree_cons (NULL_TREE,
3761 type,
3762 (tree) n->value));
3763
3764 return 0;
3765}
3766
3767/* Returns nonzero if TYPE is an empty class type and there is
3768 already an entry in OFFSETS for the same TYPE as the same OFFSET. */
3769
3770static int
3771check_subobject_offset (tree type, tree offset, splay_tree offsets)
3772{
3773 splay_tree_node n;
3774 tree t;
3775
3776 if (!is_empty_class (type))
3777 return 0;
3778
3779 /* Record the location of this empty object in OFFSETS. */
3780 n = splay_tree_lookup (offsets, (splay_tree_key) offset);
3781 if (!n)
3782 return 0;
3783
3784 for (t = (tree) n->value; t; t = TREE_CHAIN (t))
3785 if (same_type_p (TREE_VALUE (t), type))
3786 return 1;
3787
3788 return 0;
3789}
3790
3791/* Walk through all the subobjects of TYPE (located at OFFSET). Call
3792 F for every subobject, passing it the type, offset, and table of
3793 OFFSETS. If VBASES_P is one, then virtual non-primary bases should
3794 be traversed.
3795
3796 If MAX_OFFSET is non-NULL, then subobjects with an offset greater
3797 than MAX_OFFSET will not be walked.
3798
3799 If F returns a nonzero value, the traversal ceases, and that value
3800 is returned. Otherwise, returns zero. */
3801
3802static int
3803walk_subobject_offsets (tree type,
3804 subobject_offset_fn