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