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