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