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