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