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 207392591 : set_current_access_from_decl (tree decl)
215 : {
216 207392591 : if (TREE_PRIVATE (decl))
217 19518950 : current_access_specifier = access_private_node;
218 187873641 : else if (TREE_PROTECTED (decl))
219 11674611 : current_access_specifier = access_protected_node;
220 : else
221 176199030 : current_access_specifier = access_public_node;
222 207392591 : }
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 155376 : build_if_in_charge (tree true_stmt, tree false_stmt)
230 : {
231 155376 : gcc_assert (DECL_HAS_IN_CHARGE_PARM_P (current_function_decl));
232 310752 : tree cmp = build2 (NE_EXPR, boolean_type_node,
233 155376 : current_in_charge_parm, integer_zero_node);
234 155376 : tree type = unlowered_expr_type (true_stmt);
235 155376 : if (VOID_TYPE_P (type))
236 7310 : type = unlowered_expr_type (false_stmt);
237 155376 : tree cond = build3 (COND_EXPR, type,
238 : cmp, true_stmt, false_stmt);
239 155376 : 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 57842567 : build_base_path (enum tree_code code,
254 : tree expr,
255 : tree binfo,
256 : int nonnull,
257 : tsubst_flags_t complain)
258 : {
259 57842567 : tree v_binfo = NULL_TREE;
260 57842567 : tree d_binfo = NULL_TREE;
261 57842567 : tree probe;
262 57842567 : tree offset;
263 57842567 : tree target_type;
264 57842567 : tree null_test = NULL;
265 57842567 : tree ptr_target_type;
266 57842567 : int fixed_type_p;
267 57842567 : int want_pointer = TYPE_PTR_P (TREE_TYPE (expr));
268 57842567 : bool has_empty = false;
269 57842567 : bool virtual_access;
270 57842567 : bool rvalue = false;
271 :
272 57842567 : if (expr == error_mark_node || binfo == error_mark_node || !binfo)
273 : return error_mark_node;
274 :
275 124403856 : for (probe = binfo; probe; probe = BINFO_INHERITANCE_CHAIN (probe))
276 : {
277 66561304 : d_binfo = probe;
278 66561304 : if (is_empty_class (BINFO_TYPE (probe)))
279 9132405 : has_empty = true;
280 133030388 : if (!v_binfo && BINFO_VIRTUAL_P (probe))
281 : v_binfo = probe;
282 : }
283 :
284 57842552 : probe = TYPE_MAIN_VARIANT (TREE_TYPE (expr));
285 57842552 : if (want_pointer)
286 54046125 : probe = TYPE_MAIN_VARIANT (TREE_TYPE (probe));
287 57842552 : if (dependent_type_p (probe))
288 279503 : if (tree open = currently_open_class (probe))
289 57842552 : probe = open;
290 :
291 57842552 : if (code == PLUS_EXPR
292 112341242 : && !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 61186406 : gcc_assert ((code == MINUS_EXPR
309 : && SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), probe))
310 : || code == PLUS_EXPR);
311 :
312 57842544 : if (binfo == d_binfo)
313 : /* Nothing to do. */
314 : return expr;
315 :
316 8052614 : 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 23079429 : bool uneval = (cp_unevaluated_operand != 0
348 7312886 : || processing_template_decl
349 15026987 : || in_template_context);
350 :
351 6974225 : if (!uneval)
352 6974225 : 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 8052602 : 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 8052602 : if (code == PLUS_EXPR
362 8052602 : && !want_pointer
363 : && !has_empty
364 3796052 : && !uneval
365 2237038 : && !virtual_access)
366 2231781 : return build_simple_base_path (expr, binfo);
367 :
368 5820821 : if (!want_pointer)
369 : {
370 1564271 : rvalue = !lvalue_p (expr);
371 : /* This must happen before the call to save_expr. */
372 1564271 : expr = cp_build_addr_expr (expr, complain);
373 : }
374 : else
375 4256550 : expr = mark_rvalue_use (expr);
376 :
377 5820821 : offset = BINFO_OFFSET (binfo);
378 5868846 : 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 5820821 : target_type = cp_build_qualified_type
383 5820821 : (target_type, cp_type_quals (TREE_TYPE (TREE_TYPE (expr))));
384 5820821 : 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 5820821 : if (uneval)
392 : {
393 1078377 : expr = build_nop (ptr_target_type, expr);
394 1078377 : goto indout;
395 : }
396 :
397 4742444 : 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 4742438 : if (virtual_access && fixed_type_p < 0
410 4742438 : && 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 4742405 : 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 66212 : if (!virtual_access && integer_zerop (offset))
426 64305 : return build_nop (ptr_target_type, expr);
427 1907 : null_test = error_mark_node;
428 : }
429 :
430 : /* Protect against multiple evaluation if necessary. */
431 4678100 : if (TREE_SIDE_EFFECTS (expr) && (null_test || virtual_access))
432 912 : expr = save_expr (expr);
433 :
434 : /* Store EXPR and build the real null test just before returning. */
435 4678100 : if (null_test)
436 1907 : null_test = expr;
437 :
438 : /* If this is a simple base reference, express it as a COMPONENT_REF. */
439 4678100 : 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 4569987 : && !has_empty)
443 : {
444 2678205 : expr = cp_build_fold_indirect_ref (expr);
445 2678205 : expr = build_simple_base_path (expr, binfo);
446 2678205 : if (rvalue && lvalue_p (expr))
447 0 : expr = move (expr);
448 2678205 : if (want_pointer)
449 2678205 : expr = build_address (expr);
450 2678205 : target_type = TREE_TYPE (expr);
451 2678205 : goto out;
452 : }
453 :
454 1999895 : 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 71268 : tree v_offset;
460 :
461 71268 : 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 71262 : tree t = expr;
476 71262 : if (sanitize_flags_p (SANITIZE_VPTR)
477 71262 : && 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 71218 : t = expr;
483 : }
484 71262 : v_offset = build_vfield_ref (cp_build_fold_indirect_ref (t),
485 71262 : TREE_TYPE (TREE_TYPE (expr)));
486 : }
487 :
488 71268 : if (v_offset == error_mark_node)
489 : return error_mark_node;
490 :
491 71268 : v_offset = fold_build_pointer_plus (v_offset, BINFO_VPTR_FIELD (v_binfo));
492 71268 : v_offset = build1 (NOP_EXPR,
493 : build_pointer_type (ptrdiff_type_node),
494 : v_offset);
495 71268 : v_offset = cp_build_fold_indirect_ref (v_offset);
496 71268 : TREE_CONSTANT (v_offset) = 1;
497 :
498 142536 : offset = convert_to_integer (ptrdiff_type_node,
499 : size_diffop_loc (input_location, offset,
500 71268 : BINFO_OFFSET (v_binfo)));
501 :
502 71268 : if (!integer_zerop (offset))
503 502 : v_offset = build2 (code, ptrdiff_type_node, v_offset, offset);
504 :
505 71268 : 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 44428 : offset = build_if_in_charge
510 44428 : (convert_to_integer (ptrdiff_type_node, BINFO_OFFSET (binfo)),
511 : v_offset);
512 : else
513 : offset = v_offset;
514 : }
515 :
516 1999895 : if (want_pointer)
517 1999895 : target_type = ptr_target_type;
518 :
519 1999895 : if (!integer_zerop (offset))
520 : {
521 72183 : offset = fold_convert (sizetype, offset);
522 72183 : if (code == MINUS_EXPR)
523 439 : offset = fold_build1_loc (input_location, NEGATE_EXPR, sizetype, offset);
524 72183 : expr = fold_build_pointer_plus (expr, offset);
525 : }
526 : else
527 : null_test = NULL;
528 :
529 1999895 : expr = build1 (NOP_EXPR, ptr_target_type, expr);
530 :
531 3078305 : indout:
532 3078305 : if (!want_pointer)
533 : {
534 1564271 : expr = cp_build_fold_indirect_ref (expr);
535 1564271 : if (rvalue)
536 239394 : expr = move (expr);
537 : }
538 :
539 1514034 : out:
540 5756510 : if (null_test)
541 : /* Wrap EXPR in a null test. */
542 1907 : 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 16801163 : build_simple_base_path (tree expr, tree binfo)
553 : {
554 16801163 : tree type = BINFO_TYPE (binfo);
555 16801163 : tree d_binfo = BINFO_INHERITANCE_CHAIN (binfo);
556 16801163 : tree field;
557 :
558 16801163 : if (d_binfo == NULL_TREE)
559 : {
560 8218396 : tree temp;
561 :
562 8218396 : 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 8218396 : temp = unary_complex_lvalue (ADDR_EXPR, expr);
569 8218396 : if (temp)
570 0 : expr = cp_build_fold_indirect_ref (temp);
571 :
572 8218396 : return expr;
573 : }
574 :
575 : /* Recurse. */
576 8582767 : expr = build_simple_base_path (expr, d_binfo);
577 :
578 8582767 : for (field = TYPE_FIELDS (BINFO_TYPE (d_binfo));
579 43418681 : field; field = DECL_CHAIN (field))
580 : /* Is this the base field created by build_base_field? */
581 43418681 : if (TREE_CODE (field) == FIELD_DECL
582 9771566 : && DECL_FIELD_IS_BASE (field)
583 9728918 : && 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 52001571 : && (BINFO_INHERITANCE_CHAIN (d_binfo)
589 8218378 : || tree_int_cst_equal (byte_position (field),
590 8218378 : 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 8582767 : int type_quals = cp_type_quals (TREE_TYPE (expr));
596 :
597 8582767 : 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 8582767 : if (type_quals & TYPE_QUAL_CONST)
604 760241 : TREE_READONLY (expr) = 1;
605 8582767 : if (type_quals & TYPE_QUAL_VOLATILE)
606 28334 : TREE_THIS_VOLATILE (expr) = 1;
607 :
608 8582767 : 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 1915029 : convert_to_base (tree object, tree type, bool check_access, bool nonnull,
624 : tsubst_flags_t complain)
625 : {
626 1915029 : tree binfo;
627 1915029 : tree object_type;
628 :
629 1915029 : if (TYPE_PTR_P (TREE_TYPE (object)))
630 : {
631 207061 : object_type = TREE_TYPE (TREE_TYPE (object));
632 207061 : type = TREE_TYPE (type);
633 : }
634 : else
635 1707968 : object_type = TREE_TYPE (object);
636 :
637 1917681 : binfo = lookup_base (object_type, type, check_access ? ba_check : ba_unique,
638 : NULL, complain);
639 1915029 : if (!binfo || binfo == error_mark_node)
640 87 : return error_mark_node;
641 :
642 1914942 : 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 1566047 : convert_to_base_statically (tree expr, tree base)
652 : {
653 1566047 : tree expr_type;
654 :
655 1566047 : expr_type = TREE_TYPE (expr);
656 1566047 : if (!SAME_BINFO_TYPE_P (BINFO_TYPE (base), expr_type))
657 : {
658 : /* If this is a non-empty base, use a COMPONENT_REF. */
659 1566047 : if (!is_empty_class (BINFO_TYPE (base)))
660 1342578 : 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 223469 : gcc_assert (!processing_template_decl);
667 223469 : expr = cp_build_addr_expr (expr, tf_warning_or_error);
668 223469 : if (!integer_zerop (BINFO_OFFSET (base)))
669 189 : expr = fold_build_pointer_plus_loc (input_location,
670 189 : expr, BINFO_OFFSET (base));
671 223469 : expr = fold_convert (build_pointer_type (BINFO_TYPE (base)), expr);
672 223469 : 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 39261940 : is_empty_base_ref (tree expr)
685 : {
686 39261940 : if (INDIRECT_REF_P (expr))
687 31502835 : expr = TREE_OPERAND (expr, 0);
688 39261940 : if (TREE_CODE (expr) != NOP_EXPR)
689 : return false;
690 1107858 : tree type = TREE_TYPE (expr);
691 1107858 : if (!POINTER_TYPE_P (type))
692 : return false;
693 1107858 : type = TREE_TYPE (type);
694 1107858 : if (!is_empty_class (type))
695 : return false;
696 464120 : STRIP_NOPS (expr);
697 464120 : tree fromtype = TREE_TYPE (expr);
698 464120 : if (!POINTER_TYPE_P (fromtype))
699 : return false;
700 464120 : fromtype = TREE_TYPE (fromtype);
701 455757 : return (CLASS_TYPE_P (fromtype)
702 455757 : && !same_type_ignoring_top_level_qualifiers_p (fromtype, type)
703 919179 : && DERIVED_FROM_P (type, fromtype));
704 : }
705 :
706 : tree
707 1660865 : build_vfield_ref (tree datum, tree type)
708 : {
709 1660865 : tree vfield, vcontext;
710 :
711 1660865 : if (datum == error_mark_node
712 : /* Can happen in case of duplicate base types (c++/59082). */
713 1660865 : || !TYPE_VFIELD (type))
714 : return error_mark_node;
715 :
716 : /* First, convert to the requested type. */
717 1660865 : 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 1660865 : vfield = TYPE_VFIELD (type);
728 1660865 : vcontext = DECL_CONTEXT (vfield);
729 3626697 : while (!same_type_ignoring_top_level_qualifiers_p (vcontext, type))
730 : {
731 1965832 : datum = build_simple_base_path (datum, CLASSTYPE_PRIMARY_BINFO (type));
732 1965832 : type = TREE_TYPE (datum);
733 : }
734 :
735 1660865 : 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 475274 : build_vtbl_ref (tree instance, tree idx)
745 : {
746 475274 : tree aref;
747 475274 : 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 475274 : int cdtorp = 0;
753 475274 : tree fixed_type = fixed_type_or_null (instance, NULL, &cdtorp);
754 :
755 475274 : tree basetype = non_reference (TREE_TYPE (instance));
756 :
757 475274 : 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 475243 : vtbl = build_vfield_ref (instance, basetype);
767 :
768 475274 : aref = build_array_ref (input_location, vtbl, idx);
769 950517 : TREE_CONSTANT (aref) |= TREE_CONSTANT (vtbl) && TREE_CONSTANT (idx);
770 :
771 475274 : 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 474363 : build_vfn_ref (tree instance_ptr, tree idx)
779 : {
780 474363 : tree aref;
781 :
782 474363 : 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 474363 : 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 474363 : aref = build3 (OBJ_TYPE_REF, TREE_TYPE (aref), aref, instance_ptr,
792 474363 : fold_convert (TREE_TYPE (instance_ptr), idx));
793 :
794 474363 : 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 1780669 : 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 36605801 : set_linkage_according_to_type (tree /*type*/, tree decl)
816 : {
817 36605801 : TREE_PUBLIC (decl) = 1;
818 36605801 : determine_visibility (decl);
819 36605801 : }
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 2209871 : build_vtable (tree class_type, tree name, tree vtable_type)
827 : {
828 2209871 : tree decl;
829 :
830 2209871 : 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 2209871 : SET_DECL_ASSEMBLER_NAME (decl, name);
834 2209871 : DECL_CONTEXT (decl) = class_type;
835 2209871 : DECL_ARTIFICIAL (decl) = 1;
836 2209871 : TREE_STATIC (decl) = 1;
837 2209871 : TREE_READONLY (decl) = 1;
838 2209871 : DECL_VIRTUAL_P (decl) = 1;
839 2221336 : SET_DECL_ALIGN (decl, TARGET_VTABLE_ENTRY_ALIGN);
840 2209871 : DECL_USER_ALIGN (decl) = true;
841 2209871 : DECL_VTABLE_OR_VTT_P (decl) = 1;
842 2209871 : set_linkage_according_to_type (class_type, decl);
843 : /* The vtable has not been defined -- yet. */
844 2209871 : DECL_EXTERNAL (decl) = 1;
845 2209871 : 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 2209871 : DECL_IGNORED_P (decl) = 1;
868 :
869 2209871 : 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 8405447 : get_vtable_decl (tree type, int complete)
880 : {
881 8405447 : tree decl;
882 :
883 8405447 : if (CLASSTYPE_VTABLES (type))
884 6624778 : return CLASSTYPE_VTABLES (type);
885 :
886 1780669 : decl = build_vtable (type, get_vtable_name (type), vtbl_type_node);
887 1780669 : CLASSTYPE_VTABLES (type) = decl;
888 :
889 1780669 : if (complete)
890 : {
891 39600 : DECL_EXTERNAL (decl) = 1;
892 39600 : 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 6614011 : build_primary_vtable (tree binfo, tree type)
906 : {
907 6614011 : tree decl;
908 6614011 : tree virtuals;
909 :
910 6614011 : decl = get_vtable_decl (type, /*complete=*/0);
911 :
912 6614011 : if (binfo)
913 : {
914 6364657 : 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 1491715 : virtuals = copy_list (BINFO_VIRTUALS (binfo));
920 1491715 : TREE_TYPE (decl) = TREE_TYPE (get_vtbl_decl_for_binfo (binfo));
921 1491715 : DECL_SIZE (decl) = TYPE_SIZE (TREE_TYPE (decl));
922 1491715 : DECL_SIZE_UNIT (decl) = TYPE_SIZE_UNIT (TREE_TYPE (decl));
923 : }
924 : else
925 : {
926 249354 : 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 1741069 : BINFO_VTABLE (TYPE_BINFO (type)) = decl;
933 1741069 : BINFO_VIRTUALS (TYPE_BINFO (type)) = virtuals;
934 1741069 : 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 727535 : build_secondary_vtable (tree binfo)
954 : {
955 727535 : 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 252688 : SET_BINFO_NEW_VTABLE_MARKED (binfo);
963 :
964 : /* Make fresh virtual list, so we can smash it later. */
965 252688 : 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 252688 : BINFO_VTABLE (binfo) = NULL_TREE;
970 252688 : 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 7092192 : make_new_vtable (tree t, tree binfo)
978 : {
979 7092192 : 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 6364657 : 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 727535 : 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 7484402 : modify_vtable_entry (tree t,
1000 : tree binfo,
1001 : tree fndecl,
1002 : tree delta,
1003 : tree *virtuals)
1004 : {
1005 7484402 : tree v;
1006 :
1007 7484402 : v = *virtuals;
1008 :
1009 7484402 : if (fndecl != BV_FN (v)
1010 7484402 : || !tree_int_cst_equal (delta, BV_DELTA (v)))
1011 : {
1012 : /* We need a new vtable for BINFO. */
1013 5347789 : 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 5347789 : BV_DELTA (v) = delta;
1025 5347789 : BV_VCALL_INDEX (v) = NULL_TREE;
1026 5347789 : BV_FN (v) = fndecl;
1027 : }
1028 7484402 : }
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 285923 : iobj_parm_corresponds_to (tree iobj_fn, tree xobj_param, tree context)
1037 : {
1038 285923 : 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 285923 : if (!same_type_ignoring_top_level_qualifiers_p
1108 285923 : (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 285914 : cp_ref_qualifier const iobj_ref_qual = type_memfn_rqual (iobj_fn_type);
1121 : /* We only care about cv qualifiers when determining correspondence. */
1122 285914 : static constexpr cp_cv_quals cv_bits = TYPE_QUAL_VOLATILE
1123 : | TYPE_QUAL_CONST;
1124 285914 : 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 285914 : 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 570606 : cp_ref_qualifier const xobj_ref_qual = [&](){
1183 285303 : 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 285303 : }(); /* 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 285303 : cp_cv_quals const xobj_cv_quals
1193 285303 : = 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 285303 : if (iobj_ref_qual != xobj_ref_qual
1198 284919 : || 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 419791099 : object_parms_correspond (tree fn, tree method, tree context)
1215 : {
1216 419791099 : tree fn_type = TREE_TYPE (fn);
1217 419791099 : 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 419791099 : if (DECL_IOBJ_MEMBER_FUNCTION_P (fn)
1223 419791099 : && 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 417591431 : if ((cxx_dialect >= cxx20
1228 3605681 : || (FUNCTION_REF_QUALIFIED (fn_type)
1229 3605681 : == FUNCTION_REF_QUALIFIED (method_type)))
1230 421197104 : && (type_memfn_quals (fn_type) != type_memfn_quals (method_type)
1231 399539554 : || type_memfn_rqual (fn_type) != type_memfn_rqual (method_type)))
1232 18424835 : return false;
1233 399166596 : return true;
1234 : }
1235 : /* Treat a static member function as corresponding to any object parm. */
1236 2199668 : 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 9229 : else if (DECL_XOBJ_MEMBER_FUNCTION_P (fn)
1242 17718 : && DECL_XOBJ_MEMBER_FUNCTION_P (method))
1243 : {
1244 23319 : auto get_object_param = [] (tree fn)
1245 : {
1246 15546 : 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 7773 : tree fn_param = get_object_param (fn);
1251 7773 : tree method_param = get_object_param (method);
1252 7773 : if (!same_type_p (fn_param, method_param))
1253 2005 : return false;
1254 : }
1255 : else
1256 : {
1257 1456 : tree xobj_fn = DECL_XOBJ_MEMBER_FUNCTION_P (fn) ? fn : method;
1258 1456 : tree iobj_fn = xobj_fn != fn ? fn : method;
1259 1456 : tree xobj_param = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (xobj_fn)));
1260 :
1261 1456 : return iobj_parm_corresponds_to (iobj_fn, xobj_param, context);
1262 : }
1263 :
1264 5768 : 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 346144338 : add_method (tree type, tree method, bool via_using)
1273 : {
1274 346144338 : if (method == error_mark_node)
1275 : return false;
1276 :
1277 346144338 : gcc_assert (!DECL_EXTERN_C_P (method));
1278 :
1279 346144338 : tree *slot = find_member_slot (type, DECL_NAME (method));
1280 346144338 : tree current_fns = slot ? *slot : NULL_TREE;
1281 :
1282 : /* See below. */
1283 346144338 : int losem = -1;
1284 :
1285 : /* Check to see if we've already got this method. */
1286 1374753614 : for (ovl_iterator iter (current_fns); iter; ++iter)
1287 : {
1288 579139205 : tree fn = *iter;
1289 :
1290 579139205 : if (TREE_CODE (fn) != TREE_CODE (method))
1291 157879435 : continue;
1292 :
1293 : /* Two using-declarations can coexist, we'll complain about ambiguity in
1294 : overload resolution. */
1295 3792935 : if (via_using && iter.using_p ()
1296 : /* Except handle inherited constructors specially. */
1297 425443304 : && ! DECL_CONSTRUCTOR_P (fn))
1298 : {
1299 1535045 : if (fn == method)
1300 : /* Don't add the same one twice. */
1301 12567 : return false;
1302 1535036 : 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 419724725 : if (!object_parms_correspond (fn, method, type))
1322 18427913 : continue;
1323 :
1324 401296812 : tree fn_type = TREE_TYPE (fn);
1325 401296812 : tree method_type = TREE_TYPE (method);
1326 :
1327 401296812 : tree real_fn = fn;
1328 401296812 : tree real_method = method;
1329 :
1330 : /* Templates and conversion ops must match return types. */
1331 802436193 : if ((DECL_CONV_FN_P (fn) || TREE_CODE (fn) == TEMPLATE_DECL)
1332 554185206 : && !same_type_p (TREE_TYPE (fn_type), TREE_TYPE (method_type)))
1333 4310556 : continue;
1334 :
1335 : /* For templates, the template parameters must be identical. */
1336 396986256 : if (TREE_CODE (fn) == TEMPLATE_DECL)
1337 : {
1338 297470382 : if (!comp_template_parms (DECL_TEMPLATE_PARMS (fn),
1339 148735191 : DECL_TEMPLATE_PARMS (method)))
1340 103278712 : continue;
1341 :
1342 45456479 : real_fn = DECL_TEMPLATE_RESULT (fn);
1343 45456479 : real_method = DECL_TEMPLATE_RESULT (method);
1344 : }
1345 :
1346 293707544 : tree parms1 = TYPE_ARG_TYPES (fn_type);
1347 293707544 : tree parms2 = TYPE_ARG_TYPES (method_type);
1348 293707544 : if (! DECL_STATIC_FUNCTION_P (real_fn))
1349 292315359 : parms1 = TREE_CHAIN (parms1);
1350 293707544 : if (! DECL_STATIC_FUNCTION_P (real_method))
1351 292307180 : 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 293707544 : if (ctor_omit_inherited_parms (real_fn))
1356 729 : parms1 = FUNCTION_FIRST_USER_PARMTYPE (DECL_CLONED_FUNCTION (real_fn));
1357 293707544 : if (ctor_omit_inherited_parms (real_method))
1358 330 : parms2 = (FUNCTION_FIRST_USER_PARMTYPE
1359 : (DECL_CLONED_FUNCTION (real_method)));
1360 :
1361 293707544 : if (!compparms (parms1, parms2))
1362 290164830 : continue;
1363 :
1364 3542714 : tree fn_constraints = get_constraints (fn);
1365 3542714 : tree method_constraints = get_constraints (method);
1366 :
1367 3542714 : if (fn_constraints && method_constraints
1368 2982386 : && DECL_CONTEXT (fn) != type
1369 3542726 : && !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 3542714 : if (!equivalent_constraints (fn_constraints, method_constraints))
1388 : {
1389 3352823 : if (processing_template_decl)
1390 : /* We can't check satisfaction in dependent context, wait until
1391 : the class is instantiated. */
1392 499916 : continue;
1393 :
1394 2852907 : special_function_kind sfk = special_memfn_p (method);
1395 :
1396 5564428 : if (sfk == sfk_none
1397 632000 : || DECL_INHERITED_CTOR (fn)
1398 3168830 : || 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 2711521 : 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 141386 : bool dtor = (sfk == sfk_destructor);
1411 141386 : if (losem == -1)
1412 279909 : losem = ((!dtor && DECL_DELETED_FN (method))
1413 279632 : || !constraints_satisfied_p (method));
1414 139079 : bool losef = ((!dtor && DECL_DELETED_FN (fn))
1415 151310 : || !constraints_satisfied_p (fn));
1416 141386 : int win;
1417 141386 : if (losem || losef)
1418 137616 : win = losem - losef;
1419 : else
1420 3770 : win = more_constrained (fn, method);
1421 141386 : if (win > 0)
1422 : /* Leave FN in the method vec, discard METHOD. */
1423 : return false;
1424 132262 : else if (win < 0)
1425 : {
1426 : /* Remove FN, add METHOD. */
1427 119035 : current_fns = iter.remove_node (current_fns);
1428 119035 : continue;
1429 : }
1430 : else
1431 : /* Let them coexist for now. */
1432 13227 : continue;
1433 : }
1434 :
1435 : /* If these are versions of the same function, process and
1436 : move on. */
1437 189918 : if (TREE_CODE (fn) == FUNCTION_DECL
1438 189891 : && maybe_version_functions (method, fn))
1439 27 : continue;
1440 :
1441 379728 : if (DECL_INHERITED_CTOR (method))
1442 : {
1443 6346 : if (!DECL_INHERITED_CTOR (fn))
1444 : /* Defer to the other function. */
1445 3101 : 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 186691 : if (via_using)
1478 : /* Defer to the local function. */
1479 : return false;
1480 186679 : else if (iter.using_p ()
1481 186679 : || (flag_new_inheriting_ctors
1482 235562 : && DECL_INHERITED_CTOR (fn)))
1483 : {
1484 : /* Remove the inherited function. */
1485 186358 : current_fns = iter.remove_node (current_fns);
1486 186358 : 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 346131771 : current_fns = ovl_insert (method, current_fns, via_using);
1500 :
1501 625606365 : if (!COMPLETE_TYPE_P (type) && !DECL_CONV_FN_P (method)
1502 622918547 : && !push_class_level_binding (DECL_NAME (method), current_fns))
1503 : return false;
1504 :
1505 346131753 : if (!slot)
1506 152739353 : slot = add_member_slot (type, DECL_NAME (method));
1507 :
1508 : /* Maintain TYPE_HAS_USER_CONSTRUCTOR, etc. */
1509 346131753 : grok_special_member_properties (method);
1510 :
1511 346131753 : *slot = current_fns;
1512 :
1513 346131753 : 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 3032140 : alter_access (tree t, tree fdecl, tree access)
1523 : {
1524 3032140 : tree elem;
1525 :
1526 3032140 : retrofit_lang_decl (fdecl);
1527 :
1528 3032140 : gcc_assert (!DECL_DISCRIMINATOR_P (fdecl));
1529 :
1530 3032140 : elem = purpose_member (t, DECL_ACCESS (fdecl));
1531 3032140 : 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 3032140 : perform_or_defer_access_check (TYPE_BINFO (t), fdecl, fdecl,
1552 : tf_warning_or_error);
1553 3032140 : DECL_ACCESS (fdecl) = tree_cons (t, access, DECL_ACCESS (fdecl));
1554 3032140 : 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 399696 : declared_access (tree decl)
1563 : {
1564 399696 : return (TREE_PRIVATE (decl) ? access_private_node
1565 389773 : : TREE_PROTECTED (decl) ? access_protected_node
1566 399696 : : 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 191053695 : maybe_push_used_methods (tree decl)
1575 : {
1576 191053695 : if (TREE_CODE (decl) != USING_DECL)
1577 : return false;
1578 3217567 : tree used = strip_using_decl (decl);
1579 3217567 : 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 6223962 : for (tree f : ovl_range (used))
1585 : {
1586 5876252 : if (DECL_CONSTRUCTOR_P (f))
1587 : /* Inheriting constructors are handled separately. */
1588 102646 : return false;
1589 :
1590 2835480 : bool added = add_method (current_class_type, f, true);
1591 :
1592 2835480 : if (added)
1593 2835459 : 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 1842204 : return true;
1608 : }
1609 :
1610 : /* Process the USING_DECL, which is a member of T. */
1611 :
1612 : static void
1613 2223274 : handle_using_decl (tree using_decl, tree t)
1614 : {
1615 2223274 : tree decl = USING_DECL_DECLS (using_decl);
1616 :
1617 2223274 : gcc_assert (!processing_template_decl && decl);
1618 :
1619 2223274 : cp_emit_debug_info_for_using (decl, t);
1620 :
1621 2223274 : if (is_overloaded_fn (decl))
1622 : /* Handled in maybe_push_used_methods. */
1623 2223274 : return;
1624 :
1625 381236 : tree name = DECL_NAME (using_decl);
1626 381236 : tree old_value = lookup_member (t, name, /*protect=*/0, /*want_type=*/false,
1627 : tf_warning_or_error);
1628 381236 : if (old_value)
1629 : {
1630 381236 : old_value = OVL_FIRST (old_value);
1631 :
1632 381236 : 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 381236 : iloc_sentinel ils (DECL_SOURCE_LOCATION (using_decl));
1662 381236 : tree access = declared_access (using_decl);
1663 :
1664 : /* Make type T see field decl FDECL with access ACCESS. */
1665 381236 : 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 184555 : gcc_assert (TREE_CODE (decl) == CONST_DECL);
1671 :
1672 184555 : auto cas = make_temp_override (current_access_specifier, access);
1673 184555 : tree copy = copy_decl (decl);
1674 184555 : DECL_CONTEXT (copy) = t;
1675 184555 : 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 184555 : DECL_IGNORED_P (copy) = true;
1679 184555 : DECL_SOURCE_LOCATION (copy) = DECL_SOURCE_LOCATION (using_decl);
1680 184555 : finish_member_declaration (copy);
1681 184555 : DECL_ABSTRACT_ORIGIN (copy) = decl;
1682 184555 : }
1683 : else
1684 196681 : alter_access (t, decl, access);
1685 381236 : }
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 17248706 : check_tag (tree tag, tree id, tree *tp, abi_tag_data *p)
1702 : {
1703 17248706 : if (!IDENTIFIER_MARKED (id))
1704 : {
1705 5813215 : 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 5813197 : p->tags = tree_cons (NULL_TREE, tag, p->tags);
1710 :
1711 : /* Don't inherit this tag multiple times. */
1712 5813197 : IDENTIFIER_MARKED (id) = true;
1713 :
1714 5813197 : ABI_TAG_INHERITED (p->tags) = true;
1715 5813197 : if (TYPE_P (p->t))
1716 : {
1717 : /* Tags inherited from type template arguments are only used
1718 : to avoid warnings. */
1719 5769574 : ABI_TAG_NOT_MANGLED (p->tags) = true;
1720 5769574 : return;
1721 : }
1722 : /* For functions and variables we want to warn, too. */
1723 : }
1724 :
1725 : /* Otherwise we're diagnosing missing tags. */
1726 43641 : if (TREE_CODE (p->t) == FUNCTION_DECL)
1727 : {
1728 43227 : auto_diagnostic_group d;
1729 43227 : if (warning (OPT_Wabi_tag, "%qD inherits the %E ABI tag "
1730 : "that %qT (used in its return type) has",
1731 : p->t, tag, *tp))
1732 3 : inform (location_of (*tp), "%qT declared here", *tp);
1733 43227 : }
1734 414 : else if (VAR_P (p->t))
1735 : {
1736 396 : auto_diagnostic_group d;
1737 396 : if (warning (OPT_Wabi_tag, "%qD inherits the %E ABI tag "
1738 : "that %qT (used in its type) has", p->t, tag, *tp))
1739 3 : inform (location_of (*tp), "%qT declared here", *tp);
1740 396 : }
1741 18 : else if (TYPE_P (p->subob))
1742 : {
1743 6 : auto_diagnostic_group d;
1744 6 : if (warning (OPT_Wabi_tag, "%qT does not have the %E ABI tag "
1745 : "that base %qT has", p->t, tag, p->subob))
1746 6 : inform (location_of (p->subob), "%qT declared here",
1747 : p->subob);
1748 6 : }
1749 : else
1750 : {
1751 12 : auto_diagnostic_group d;
1752 12 : if (warning (OPT_Wabi_tag, "%qT does not have the %E ABI tag "
1753 : "that %qT (used in the type of %qD) has",
1754 : p->t, tag, *tp, p->subob))
1755 : {
1756 12 : inform (location_of (p->subob), "%qD declared here",
1757 : p->subob);
1758 12 : inform (location_of (*tp), "%qT declared here", *tp);
1759 : }
1760 12 : }
1761 : }
1762 : }
1763 :
1764 : /* Find all the ABI tags in the attribute list ATTR and either call
1765 : check_tag (if TP is non-null) or set IDENTIFIER_MARKED to val. */
1766 :
1767 : static void
1768 2523245990 : mark_or_check_attr_tags (tree attr, tree *tp, abi_tag_data *p, bool val)
1769 : {
1770 2523245990 : if (!attr)
1771 : return;
1772 302201970 : for (; (attr = lookup_attribute ("abi_tag", attr));
1773 105774423 : attr = TREE_CHAIN (attr))
1774 211548876 : for (tree list = TREE_VALUE (attr); list;
1775 105774453 : list = TREE_CHAIN (list))
1776 : {
1777 105774453 : tree tag = TREE_VALUE (list);
1778 105774453 : tree id = get_identifier (TREE_STRING_POINTER (tag));
1779 105774453 : if (tp)
1780 17248706 : check_tag (tag, id, tp, p);
1781 : else
1782 88525747 : IDENTIFIER_MARKED (id) = val;
1783 : }
1784 : }
1785 :
1786 : /* Find all the ABI tags on T and its enclosing scopes and either call
1787 : check_tag (if TP is non-null) or set IDENTIFIER_MARKED to val. */
1788 :
1789 : static void
1790 936231758 : mark_or_check_tags (tree t, tree *tp, abi_tag_data *p, bool val)
1791 : {
1792 3459477748 : while (t != global_namespace)
1793 : {
1794 2523245990 : tree attr;
1795 2523245990 : if (TYPE_P (t))
1796 : {
1797 932679380 : attr = TYPE_ATTRIBUTES (t);
1798 932679380 : t = CP_TYPE_CONTEXT (t);
1799 : }
1800 : else
1801 : {
1802 1590566610 : attr = DECL_ATTRIBUTES (t);
1803 1590566610 : t = CP_DECL_CONTEXT (t);
1804 : }
1805 2523245990 : mark_or_check_attr_tags (attr, tp, p, val);
1806 : }
1807 936231758 : }
1808 :
1809 : /* walk_tree callback for check_abi_tags: if the type at *TP involves any
1810 : types with ABI tags, add the corresponding identifiers to the VEC in
1811 : *DATA and set IDENTIFIER_MARKED. */
1812 :
1813 : static tree
1814 777891269 : find_abi_tags_r (tree *tp, int *walk_subtrees, void *data)
1815 : {
1816 777891269 : if (TYPE_P (*tp) && *walk_subtrees == 1 && flag_abi_version != 14)
1817 : /* Tell cp_walk_subtrees to look though typedefs. [PR98481] */
1818 502400427 : *walk_subtrees = 2;
1819 :
1820 777891269 : if (!OVERLOAD_TYPE_P (*tp))
1821 : return NULL_TREE;
1822 :
1823 : /* walk_tree shouldn't be walking into any subtrees of a RECORD_TYPE
1824 : anyway, but let's make sure of it. */
1825 141232524 : *walk_subtrees = false;
1826 :
1827 141232524 : abi_tag_data *p = static_cast<struct abi_tag_data*>(data);
1828 :
1829 141232524 : mark_or_check_tags (*tp, tp, p, false);
1830 :
1831 141232524 : return NULL_TREE;
1832 : }
1833 :
1834 : /* walk_tree callback for mark_abi_tags: if *TP is a class, set
1835 : IDENTIFIER_MARKED on its ABI tags. */
1836 :
1837 : static tree
1838 1328491008 : mark_abi_tags_r (tree *tp, int *walk_subtrees, void *data)
1839 : {
1840 1328491008 : if (TYPE_P (*tp) && *walk_subtrees == 1 && flag_abi_version != 14)
1841 : /* Tell cp_walk_subtrees to look though typedefs. */
1842 940959412 : *walk_subtrees = 2;
1843 :
1844 1328491008 : if (!OVERLOAD_TYPE_P (*tp))
1845 : return NULL_TREE;
1846 :
1847 : /* walk_tree shouldn't be walking into any subtrees of a RECORD_TYPE
1848 : anyway, but let's make sure of it. */
1849 178871352 : *walk_subtrees = false;
1850 :
1851 178871352 : bool *valp = static_cast<bool*>(data);
1852 :
1853 178871352 : mark_or_check_tags (*tp, NULL, NULL, *valp);
1854 :
1855 178871352 : return NULL_TREE;
1856 : }
1857 :
1858 : /* Set IDENTIFIER_MARKED on all the ABI tags on T and its enclosing
1859 : scopes. */
1860 :
1861 : static void
1862 616127882 : mark_abi_tags (tree t, bool val)
1863 : {
1864 616127882 : mark_or_check_tags (t, NULL, NULL, val);
1865 616127882 : if (DECL_P (t))
1866 : {
1867 798064532 : if (DECL_LANG_SPECIFIC (t) && DECL_USE_TEMPLATE (t)
1868 688724584 : && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (t)))
1869 : {
1870 : /* Template arguments are part of the signature. */
1871 31138172 : tree level = INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (t));
1872 77656400 : for (int j = 0; j < TREE_VEC_LENGTH (level); ++j)
1873 : {
1874 46518228 : tree arg = TREE_VEC_ELT (level, j);
1875 46518228 : cp_walk_tree_without_duplicates (&arg, mark_abi_tags_r, &val);
1876 : }
1877 : }
1878 400284740 : if (TREE_CODE (t) == FUNCTION_DECL)
1879 : /* A function's parameter types are part of the signature, so
1880 : we don't need to inherit any tags that are also in them. */
1881 1030030140 : for (tree arg = FUNCTION_FIRST_USER_PARMTYPE (t); arg;
1882 692873662 : arg = TREE_CHAIN (arg))
1883 692873662 : cp_walk_tree_without_duplicates (&TREE_VALUE (arg),
1884 : mark_abi_tags_r, &val);
1885 : }
1886 616127882 : }
1887 :
1888 : /* Check that T has all the ABI tags that subobject SUBOB has, or
1889 : warn if not. If T is a (variable or function) declaration, also
1890 : return any missing tags, and add them to T if JUST_CHECKING is false. */
1891 :
1892 : static tree
1893 241751316 : check_abi_tags (tree t, tree subob, bool just_checking = false)
1894 : {
1895 241751316 : bool inherit = DECL_P (t);
1896 :
1897 241751316 : if (!inherit && !warn_abi_tag)
1898 : return NULL_TREE;
1899 :
1900 200321361 : tree decl = TYPE_P (t) ? TYPE_NAME (t) : t;
1901 200321361 : if (!TREE_PUBLIC (decl))
1902 : /* No need to worry about things local to this TU. */
1903 : return NULL_TREE;
1904 :
1905 200142391 : mark_abi_tags (t, true);
1906 :
1907 200142391 : tree subtype = TYPE_P (subob) ? subob : TREE_TYPE (subob);
1908 200142391 : struct abi_tag_data data = { t, subob, error_mark_node };
1909 200142391 : if (inherit)
1910 200142370 : data.tags = NULL_TREE;
1911 :
1912 200142391 : cp_walk_tree_without_duplicates (&subtype, find_abi_tags_r, &data);
1913 :
1914 200142391 : if (!(inherit && data.tags))
1915 : /* We don't need to do anything with data.tags. */;
1916 43608 : else if (just_checking)
1917 75 : for (tree t = data.tags; t; t = TREE_CHAIN (t))
1918 : {
1919 42 : tree id = get_identifier (TREE_STRING_POINTER (TREE_VALUE (t)));
1920 42 : IDENTIFIER_MARKED (id) = false;
1921 : }
1922 : else
1923 : {
1924 43575 : tree attr = lookup_attribute ("abi_tag", DECL_ATTRIBUTES (t));
1925 43575 : if (attr)
1926 0 : TREE_VALUE (attr) = chainon (data.tags, TREE_VALUE (attr));
1927 : else
1928 43575 : DECL_ATTRIBUTES (t)
1929 87150 : = tree_cons (abi_tag_identifier, data.tags, DECL_ATTRIBUTES (t));
1930 : }
1931 :
1932 200142391 : mark_abi_tags (t, false);
1933 :
1934 200142391 : return data.tags;
1935 : }
1936 :
1937 : /* Check that DECL has all the ABI tags that are used in parts of its type
1938 : that are not reflected in its mangled name. */
1939 :
1940 : void
1941 213034883 : check_abi_tags (tree decl)
1942 : {
1943 213034883 : if (VAR_P (decl))
1944 31671854 : check_abi_tags (decl, TREE_TYPE (decl));
1945 181363029 : else if (TREE_CODE (decl) == FUNCTION_DECL
1946 181359709 : && !DECL_CONV_FN_P (decl)
1947 360069463 : && !mangle_return_type_p (decl))
1948 168565902 : check_abi_tags (decl, TREE_TYPE (TREE_TYPE (decl)));
1949 213034883 : }
1950 :
1951 : /* Return any ABI tags that are used in parts of the type of DECL
1952 : that are not reflected in its mangled name. This function is only
1953 : used in backward-compatible mangling for ABI <11. */
1954 :
1955 : tree
1956 84309 : missing_abi_tags (tree decl)
1957 : {
1958 84309 : if (VAR_P (decl))
1959 1114 : return check_abi_tags (decl, TREE_TYPE (decl), true);
1960 83195 : else if (TREE_CODE (decl) == FUNCTION_DECL
1961 : /* Don't check DECL_CONV_FN_P here like we do in check_abi_tags, so
1962 : that we can use this function for setting need_abi_warning
1963 : regardless of the current flag_abi_version. */
1964 83195 : && !mangle_return_type_p (decl))
1965 82467 : return check_abi_tags (decl, TREE_TYPE (TREE_TYPE (decl)), true);
1966 : else
1967 728 : return NULL_TREE;
1968 : }
1969 :
1970 : void
1971 108287622 : inherit_targ_abi_tags (tree t)
1972 : {
1973 107921550 : if (!CLASS_TYPE_P (t)
1974 216209172 : || CLASSTYPE_TEMPLATE_INFO (t) == NULL_TREE)
1975 366072 : return;
1976 :
1977 107921550 : mark_abi_tags (t, true);
1978 :
1979 107921550 : tree args = CLASSTYPE_TI_ARGS (t);
1980 107921550 : struct abi_tag_data data = { t, NULL_TREE, NULL_TREE };
1981 647529482 : for (int i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
1982 : {
1983 221075396 : tree level = TMPL_ARGS_LEVEL (args, i+1);
1984 304143968 : for (int j = 0; j < TREE_VEC_LENGTH (level); ++j)
1985 : {
1986 193606270 : tree arg = TREE_VEC_ELT (level, j);
1987 193606270 : data.subob = arg;
1988 193606270 : cp_walk_tree_without_duplicates (&arg, find_abi_tags_r, &data);
1989 : }
1990 : }
1991 :
1992 : // If we found some tags on our template arguments, add them to our
1993 : // abi_tag attribute.
1994 107921550 : if (data.tags)
1995 : {
1996 5769574 : tree attr = lookup_attribute ("abi_tag", TYPE_ATTRIBUTES (t));
1997 5769574 : if (attr)
1998 3 : TREE_VALUE (attr) = chainon (data.tags, TREE_VALUE (attr));
1999 : else
2000 5769571 : TYPE_ATTRIBUTES (t)
2001 11539142 : = tree_cons (abi_tag_identifier, data.tags, TYPE_ATTRIBUTES (t));
2002 : }
2003 :
2004 107921550 : mark_abi_tags (t, false);
2005 : }
2006 :
2007 : /* Return true, iff class T has a non-virtual destructor that is
2008 : accessible from outside the class heirarchy (i.e. is public, or
2009 : there's a suitable friend. */
2010 :
2011 : static bool
2012 99 : accessible_nvdtor_p (tree t)
2013 : {
2014 99 : tree dtor = CLASSTYPE_DESTRUCTOR (t);
2015 :
2016 : /* An implicitly declared destructor is always public. And,
2017 : if it were virtual, we would have created it by now. */
2018 99 : if (!dtor)
2019 : return true;
2020 :
2021 78 : if (DECL_VINDEX (dtor))
2022 : return false; /* Virtual */
2023 :
2024 42 : if (!TREE_PRIVATE (dtor) && !TREE_PROTECTED (dtor))
2025 : return true; /* Public */
2026 :
2027 30 : if (CLASSTYPE_FRIEND_CLASSES (t)
2028 30 : || DECL_FRIENDLIST (TYPE_MAIN_DECL (t)))
2029 12 : return true; /* Has friends */
2030 :
2031 : return false;
2032 : }
2033 :
2034 : /* Run through the base classes of T, updating CANT_HAVE_CONST_CTOR_P,
2035 : and NO_CONST_ASN_REF_P. Also set flag bits in T based on
2036 : properties of the bases. */
2037 :
2038 : static void
2039 49904436 : check_bases (tree t,
2040 : int* cant_have_const_ctor_p,
2041 : int* no_const_asn_ref_p)
2042 : {
2043 49904436 : int i;
2044 49904436 : bool seen_non_virtual_nearly_empty_base_p = 0;
2045 49904436 : int seen_tm_mask = 0;
2046 49904436 : tree base_binfo;
2047 49904436 : tree binfo;
2048 49904436 : tree field = NULL_TREE;
2049 :
2050 49904436 : if (!CLASSTYPE_NON_STD_LAYOUT (t))
2051 225864248 : for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
2052 182817420 : if (TREE_CODE (field) == FIELD_DECL)
2053 : break;
2054 :
2055 73584690 : for (binfo = TYPE_BINFO (t), i = 0;
2056 73584690 : BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
2057 : {
2058 23680254 : tree basetype = TREE_TYPE (base_binfo);
2059 :
2060 23680254 : gcc_assert (COMPLETE_TYPE_P (basetype));
2061 :
2062 23680254 : if (CLASSTYPE_FINAL (basetype))
2063 14 : error ("cannot derive from %<final%> base %qT in derived type %qT",
2064 : basetype, t);
2065 :
2066 : /* If any base class is non-literal, so is the derived class. */
2067 23680254 : if (!CLASSTYPE_LITERAL_P (basetype))
2068 1687062 : CLASSTYPE_LITERAL_P (t) = false;
2069 :
2070 : /* If the base class doesn't have copy constructors or
2071 : assignment operators that take const references, then the
2072 : derived class cannot have such a member automatically
2073 : generated. */
2074 23680254 : if (TYPE_HAS_COPY_CTOR (basetype)
2075 23680254 : && ! TYPE_HAS_CONST_COPY_CTOR (basetype))
2076 60 : *cant_have_const_ctor_p = 1;
2077 23680254 : if (TYPE_HAS_COPY_ASSIGN (basetype)
2078 23680254 : && !TYPE_HAS_CONST_COPY_ASSIGN (basetype))
2079 18 : *no_const_asn_ref_p = 1;
2080 :
2081 23680254 : if (BINFO_VIRTUAL_P (base_binfo))
2082 : /* A virtual base does not effect nearly emptiness. */
2083 : ;
2084 23633441 : else if (CLASSTYPE_NEARLY_EMPTY_P (basetype))
2085 : {
2086 269764 : if (seen_non_virtual_nearly_empty_base_p)
2087 : /* And if there is more than one nearly empty base, then the
2088 : derived class is not nearly empty either. */
2089 1084 : CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
2090 : else
2091 : /* Remember we've seen one. */
2092 : seen_non_virtual_nearly_empty_base_p = 1;
2093 : }
2094 23363677 : else if (!is_empty_class (basetype))
2095 : /* If the base class is not empty or nearly empty, then this
2096 : class cannot be nearly empty. */
2097 2117420 : CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
2098 :
2099 : /* A lot of properties from the bases also apply to the derived
2100 : class. */
2101 23680254 : TYPE_NEEDS_CONSTRUCTING (t) |= TYPE_NEEDS_CONSTRUCTING (basetype);
2102 71040762 : TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t)
2103 23680254 : |= TYPE_HAS_NONTRIVIAL_DESTRUCTOR (basetype);
2104 23680254 : TYPE_HAS_COMPLEX_COPY_ASSIGN (t)
2105 23680254 : |= (TYPE_HAS_COMPLEX_COPY_ASSIGN (basetype)
2106 23680254 : || !TYPE_HAS_COPY_ASSIGN (basetype));
2107 47360508 : TYPE_HAS_COMPLEX_COPY_CTOR (t) |= (TYPE_HAS_COMPLEX_COPY_CTOR (basetype)
2108 23680254 : || !TYPE_HAS_COPY_CTOR (basetype));
2109 71040762 : TYPE_HAS_COMPLEX_MOVE_ASSIGN (t)
2110 23680254 : |= TYPE_HAS_COMPLEX_MOVE_ASSIGN (basetype);
2111 23680254 : TYPE_HAS_COMPLEX_MOVE_CTOR (t) |= TYPE_HAS_COMPLEX_MOVE_CTOR (basetype);
2112 23680254 : TYPE_POLYMORPHIC_P (t) |= TYPE_POLYMORPHIC_P (basetype);
2113 71040762 : CLASSTYPE_CONTAINS_EMPTY_CLASS_P (t)
2114 23680254 : |= CLASSTYPE_CONTAINS_EMPTY_CLASS_P (basetype);
2115 47360508 : TYPE_HAS_COMPLEX_DFLT (t) |= (!TYPE_HAS_DEFAULT_CONSTRUCTOR (basetype)
2116 23680254 : || TYPE_HAS_COMPLEX_DFLT (basetype));
2117 23680254 : SET_CLASSTYPE_READONLY_FIELDS_NEED_INIT
2118 : (t, CLASSTYPE_READONLY_FIELDS_NEED_INIT (t)
2119 : | CLASSTYPE_READONLY_FIELDS_NEED_INIT (basetype));
2120 23680254 : SET_CLASSTYPE_REF_FIELDS_NEED_INIT
2121 : (t, CLASSTYPE_REF_FIELDS_NEED_INIT (t)
2122 : | CLASSTYPE_REF_FIELDS_NEED_INIT (basetype));
2123 23680254 : if (TYPE_HAS_MUTABLE_P (basetype))
2124 972704 : CLASSTYPE_HAS_MUTABLE (t) = 1;
2125 :
2126 : /* A standard-layout class is a class that:
2127 : ...
2128 : * has no non-standard-layout base classes, */
2129 23680254 : CLASSTYPE_NON_STD_LAYOUT (t) |= CLASSTYPE_NON_STD_LAYOUT (basetype);
2130 23680254 : if (!CLASSTYPE_NON_STD_LAYOUT (t))
2131 : {
2132 21743282 : tree basefield;
2133 : /* ...has no base classes of the same type as the first non-static
2134 : data member... */
2135 507396 : if (field && DECL_CONTEXT (field) == t
2136 22196792 : && (same_type_ignoring_top_level_qualifiers_p
2137 453510 : (TREE_TYPE (field), basetype)))
2138 129 : CLASSTYPE_NON_STD_LAYOUT (t) = 1;
2139 : /* DR 1813:
2140 : ...has at most one base class subobject of any given type... */
2141 21743153 : else if (CLASSTYPE_REPEATED_BASE_P (t))
2142 376 : CLASSTYPE_NON_STD_LAYOUT (t) = 1;
2143 : else
2144 : /* ...has all non-static data members and bit-fields in the class
2145 : and its base classes first declared in the same class. */
2146 313150849 : for (basefield = TYPE_FIELDS (basetype); basefield;
2147 291408072 : basefield = DECL_CHAIN (basefield))
2148 292345760 : if (TREE_CODE (basefield) == FIELD_DECL
2149 293658970 : && !(DECL_FIELD_IS_BASE (basefield)
2150 1313210 : && is_empty_field (basefield)))
2151 : {
2152 937688 : if (field)
2153 127898 : CLASSTYPE_NON_STD_LAYOUT (t) = 1;
2154 : else
2155 : field = basefield;
2156 : break;
2157 : }
2158 : }
2159 :
2160 : /* Don't bother collecting tm attributes if transactional memory
2161 : support is not enabled. */
2162 23680254 : if (flag_tm)
2163 : {
2164 1575 : tree tm_attr = find_tm_attribute (TYPE_ATTRIBUTES (basetype));
2165 1575 : if (tm_attr)
2166 15 : seen_tm_mask |= tm_attr_to_mask (tm_attr);
2167 : }
2168 :
2169 23680254 : check_abi_tags (t, basetype);
2170 : }
2171 :
2172 : /* If one of the base classes had TM attributes, and the current class
2173 : doesn't define its own, then the current class inherits one. */
2174 49904436 : if (seen_tm_mask && !find_tm_attribute (TYPE_ATTRIBUTES (t)))
2175 : {
2176 12 : tree tm_attr = tm_mask_to_attr (least_bit_hwi (seen_tm_mask));
2177 12 : TYPE_ATTRIBUTES (t) = tree_cons (tm_attr, NULL, TYPE_ATTRIBUTES (t));
2178 : }
2179 49904436 : }
2180 :
2181 : /* Determine all the primary bases within T. Sets BINFO_PRIMARY_BASE_P for
2182 : those that are primaries. Sets BINFO_LOST_PRIMARY_P for those
2183 : that have had a nearly-empty virtual primary base stolen by some
2184 : other base in the hierarchy. Determines CLASSTYPE_PRIMARY_BASE for
2185 : T. */
2186 :
2187 : static void
2188 49904436 : determine_primary_bases (tree t)
2189 : {
2190 49904436 : unsigned i;
2191 49904436 : tree primary = NULL_TREE;
2192 49904436 : tree type_binfo = TYPE_BINFO (t);
2193 49904436 : tree base_binfo;
2194 :
2195 : /* Determine the primary bases of our bases. */
2196 77336395 : for (base_binfo = TREE_CHAIN (type_binfo); base_binfo;
2197 27431959 : base_binfo = TREE_CHAIN (base_binfo))
2198 : {
2199 27431959 : tree primary = CLASSTYPE_PRIMARY_BINFO (BINFO_TYPE (base_binfo));
2200 :
2201 : /* See if we're the non-virtual primary of our inheritance
2202 : chain. */
2203 27431959 : if (!BINFO_VIRTUAL_P (base_binfo))
2204 : {
2205 27237275 : tree parent = BINFO_INHERITANCE_CHAIN (base_binfo);
2206 27237275 : tree parent_primary = CLASSTYPE_PRIMARY_BINFO (BINFO_TYPE (parent));
2207 :
2208 27237275 : if (parent_primary
2209 27237275 : && SAME_BINFO_TYPE_P (BINFO_TYPE (base_binfo),
2210 : BINFO_TYPE (parent_primary)))
2211 : /* We are the primary binfo. */
2212 1101094 : BINFO_PRIMARY_P (base_binfo) = 1;
2213 : }
2214 : /* Determine if we have a virtual primary base, and mark it so.
2215 : */
2216 28540188 : if (primary && BINFO_VIRTUAL_P (primary))
2217 : {
2218 7135 : tree this_primary = copied_binfo (primary, base_binfo);
2219 :
2220 7135 : if (BINFO_PRIMARY_P (this_primary))
2221 : /* Someone already claimed this base. */
2222 636 : BINFO_LOST_PRIMARY_P (base_binfo) = 1;
2223 : else
2224 : {
2225 6499 : tree delta;
2226 :
2227 6499 : BINFO_PRIMARY_P (this_primary) = 1;
2228 6499 : BINFO_INHERITANCE_CHAIN (this_primary) = base_binfo;
2229 :
2230 : /* A virtual binfo might have been copied from within
2231 : another hierarchy. As we're about to use it as a
2232 : primary base, make sure the offsets match. */
2233 6499 : delta = size_diffop_loc (input_location,
2234 6499 : fold_convert (ssizetype,
2235 : BINFO_OFFSET (base_binfo)),
2236 6499 : fold_convert (ssizetype,
2237 : BINFO_OFFSET (this_primary)));
2238 :
2239 6499 : propagate_binfo_offsets (this_primary, delta);
2240 : }
2241 : }
2242 : }
2243 :
2244 : /* First look for a dynamic direct non-virtual base. */
2245 71907855 : for (i = 0; BINFO_BASE_ITERATE (type_binfo, i, base_binfo); i++)
2246 : {
2247 23493485 : tree basetype = BINFO_TYPE (base_binfo);
2248 :
2249 23493485 : if (TYPE_CONTAINS_VPTR_P (basetype) && !BINFO_VIRTUAL_P (base_binfo))
2250 : {
2251 1490066 : primary = base_binfo;
2252 1490066 : goto found;
2253 : }
2254 : }
2255 :
2256 : /* A "nearly-empty" virtual base class can be the primary base
2257 : class, if no non-virtual polymorphic base can be found. Look for
2258 : a nearly-empty virtual dynamic base that is not already a primary
2259 : base of something in the hierarchy. If there is no such base,
2260 : just pick the first nearly-empty virtual base. */
2261 :
2262 72507199 : for (base_binfo = TREE_CHAIN (type_binfo); base_binfo;
2263 24092829 : base_binfo = TREE_CHAIN (base_binfo))
2264 24094397 : if (BINFO_VIRTUAL_P (base_binfo)
2265 24094397 : && CLASSTYPE_NEARLY_EMPTY_P (BINFO_TYPE (base_binfo)))
2266 : {
2267 1701 : if (!BINFO_PRIMARY_P (base_binfo))
2268 : {
2269 : /* Found one that is not primary. */
2270 1568 : primary = base_binfo;
2271 1568 : goto found;
2272 : }
2273 133 : else if (!primary)
2274 : /* Remember the first candidate. */
2275 24092829 : primary = base_binfo;
2276 : }
2277 :
2278 48412802 : found:
2279 : /* If we've got a primary base, use it. */
2280 49904436 : if (primary)
2281 : {
2282 1491715 : tree basetype = BINFO_TYPE (primary);
2283 :
2284 1491715 : CLASSTYPE_PRIMARY_BINFO (t) = primary;
2285 1491715 : if (BINFO_PRIMARY_P (primary))
2286 : /* We are stealing a primary base. */
2287 81 : BINFO_LOST_PRIMARY_P (BINFO_INHERITANCE_CHAIN (primary)) = 1;
2288 1491715 : BINFO_PRIMARY_P (primary) = 1;
2289 1491715 : if (BINFO_VIRTUAL_P (primary))
2290 : {
2291 1649 : tree delta;
2292 :
2293 1649 : BINFO_INHERITANCE_CHAIN (primary) = type_binfo;
2294 : /* A virtual binfo might have been copied from within
2295 : another hierarchy. As we're about to use it as a primary
2296 : base, make sure the offsets match. */
2297 1649 : delta = size_diffop_loc (input_location, ssize_int (0),
2298 1649 : fold_convert (ssizetype, BINFO_OFFSET (primary)));
2299 :
2300 1649 : propagate_binfo_offsets (primary, delta);
2301 : }
2302 :
2303 1491715 : primary = TYPE_BINFO (basetype);
2304 :
2305 1491715 : TYPE_VFIELD (t) = TYPE_VFIELD (basetype);
2306 1491715 : BINFO_VTABLE (type_binfo) = BINFO_VTABLE (primary);
2307 1491715 : BINFO_VIRTUALS (type_binfo) = BINFO_VIRTUALS (primary);
2308 : }
2309 49904436 : }
2310 :
2311 : /* Update the variant types of T. */
2312 :
2313 : void
2314 162200042 : fixup_type_variants (tree type)
2315 : {
2316 162200042 : if (!type)
2317 : return;
2318 :
2319 162200042 : for (tree variant = TYPE_NEXT_VARIANT (type);
2320 325752754 : variant;
2321 163552712 : variant = TYPE_NEXT_VARIANT (variant))
2322 : {
2323 : /* These fields are in the _TYPE part of the node, not in
2324 : the TYPE_LANG_SPECIFIC component, so they are not shared. */
2325 163552712 : TYPE_HAS_USER_CONSTRUCTOR (variant) = TYPE_HAS_USER_CONSTRUCTOR (type);
2326 163552712 : TYPE_NEEDS_CONSTRUCTING (variant) = TYPE_NEEDS_CONSTRUCTING (type);
2327 327105424 : TYPE_HAS_NONTRIVIAL_DESTRUCTOR (variant)
2328 163552712 : = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type);
2329 :
2330 163552712 : TYPE_POLYMORPHIC_P (variant) = TYPE_POLYMORPHIC_P (type);
2331 163552712 : CLASSTYPE_FINAL (variant) = CLASSTYPE_FINAL (type);
2332 :
2333 163552712 : TYPE_BINFO (variant) = TYPE_BINFO (type);
2334 :
2335 : /* Copy whatever these are holding today. */
2336 163552712 : TYPE_VFIELD (variant) = TYPE_VFIELD (type);
2337 163552712 : TYPE_FIELDS (variant) = TYPE_FIELDS (type);
2338 :
2339 163552712 : TYPE_SIZE (variant) = TYPE_SIZE (type);
2340 163552712 : TYPE_SIZE_UNIT (variant) = TYPE_SIZE_UNIT (type);
2341 :
2342 163552712 : if (!TYPE_USER_ALIGN (variant)
2343 2734620 : || TYPE_NAME (variant) == TYPE_NAME (type)
2344 164942346 : || TYPE_ALIGN_RAW (variant) < TYPE_ALIGN_RAW (type))
2345 : {
2346 162163078 : TYPE_ALIGN_RAW (variant) = TYPE_ALIGN_RAW (type);
2347 162163078 : TYPE_USER_ALIGN (variant) = TYPE_USER_ALIGN (type);
2348 : }
2349 :
2350 163552712 : TYPE_PRECISION (variant) = TYPE_PRECISION (type);
2351 163552712 : TYPE_MODE_RAW (variant) = TYPE_MODE_RAW (type);
2352 163552712 : TYPE_EMPTY_P (variant) = TYPE_EMPTY_P (type);
2353 163552712 : TREE_ADDRESSABLE (variant) = TREE_ADDRESSABLE (type);
2354 : }
2355 : }
2356 :
2357 : /* KLASS is a class that we're applying may_alias to after the body is
2358 : parsed. Fixup any POINTER_TO and REFERENCE_TO types. The
2359 : canonical type(s) will be implicitly updated. */
2360 :
2361 : static void
2362 37671 : fixup_may_alias (tree klass)
2363 : {
2364 37671 : tree t, v;
2365 :
2366 56136 : for (t = TYPE_POINTER_TO (klass); t; t = TYPE_NEXT_PTR_TO (t))
2367 55380 : for (v = TYPE_MAIN_VARIANT (t); v; v = TYPE_NEXT_VARIANT (v))
2368 36915 : TYPE_REF_CAN_ALIAS_ALL (v) = true;
2369 37692 : for (t = TYPE_REFERENCE_TO (klass); t; t = TYPE_NEXT_REF_TO (t))
2370 42 : for (v = TYPE_MAIN_VARIANT (t); v; v = TYPE_NEXT_VARIANT (v))
2371 21 : TYPE_REF_CAN_ALIAS_ALL (v) = true;
2372 37671 : }
2373 :
2374 : /* Early variant fixups: we apply attributes at the beginning of the class
2375 : definition, and we need to fix up any variants that have already been
2376 : made via elaborated-type-specifier so that check_qualified_type works. */
2377 :
2378 : void
2379 97352214 : fixup_attribute_variants (tree t)
2380 : {
2381 97352214 : tree variants;
2382 :
2383 97352214 : if (!t)
2384 : return;
2385 :
2386 97352214 : tree attrs = TYPE_ATTRIBUTES (t);
2387 97352214 : unsigned align = TYPE_ALIGN (t);
2388 97352214 : bool user_align = TYPE_USER_ALIGN (t);
2389 97352214 : bool may_alias = lookup_attribute ("may_alias", attrs);
2390 97352214 : bool packed = TYPE_PACKED (t);
2391 :
2392 97352214 : if (may_alias)
2393 18949 : fixup_may_alias (t);
2394 :
2395 97352214 : for (variants = TYPE_NEXT_VARIANT (t);
2396 146707500 : variants;
2397 49355286 : variants = TYPE_NEXT_VARIANT (variants))
2398 : {
2399 : /* These are the two fields that check_qualified_type looks at and
2400 : are affected by attributes. */
2401 49355286 : TYPE_ATTRIBUTES (variants) = attrs;
2402 49355286 : unsigned valign = align;
2403 49355286 : if (TYPE_USER_ALIGN (variants))
2404 25015 : valign = MAX (valign, TYPE_ALIGN (variants));
2405 : else
2406 49330271 : TYPE_USER_ALIGN (variants) = user_align;
2407 49355286 : SET_TYPE_ALIGN (variants, valign);
2408 49355286 : TYPE_PACKED (variants) = packed;
2409 49355286 : if (may_alias)
2410 18722 : fixup_may_alias (variants);
2411 : }
2412 : }
2413 :
2414 : /* Set memoizing fields and bits of T (and its variants) for later
2415 : use. */
2416 :
2417 : static void
2418 49904436 : finish_struct_bits (tree t)
2419 : {
2420 : /* If this type has a copy constructor or a destructor, force its
2421 : mode to be BLKmode, and force its TREE_ADDRESSABLE bit to be
2422 : nonzero. This will cause it to be passed by invisible reference
2423 : and prevent it from being returned in a register. */
2424 49904436 : if (!has_trivial_abi_attribute (t)
2425 49904436 : && (type_has_nontrivial_copy_init (t)
2426 45343050 : || TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t)))
2427 : {
2428 5066512 : SET_DECL_MODE (TYPE_MAIN_DECL (t), BLKmode);
2429 5066512 : SET_TYPE_MODE (t, BLKmode);
2430 5066512 : TREE_ADDRESSABLE (t) = 1;
2431 : }
2432 :
2433 49904436 : if (BINFO_N_BASE_BINFOS (TYPE_BINFO (t)) && TYPE_POLYMORPHIC_P (t))
2434 : /* For a class w/o baseclasses, 'finish_struct' has set
2435 : CLASSTYPE_PURE_VIRTUALS correctly (by definition).
2436 : Similarly for a class whose base classes do not have vtables.
2437 : When neither of these is true, we might have removed abstract
2438 : virtuals (by providing a definition), added some (by declaring
2439 : new ones), or redeclared ones from a base class. We need to
2440 : recalculate what's really an abstract virtual at this point (by
2441 : looking in the vtables). */
2442 1561387 : get_pure_virtuals (t);
2443 :
2444 : /* Fix up variants (if any). */
2445 49904436 : fixup_type_variants (t);
2446 49904436 : }
2447 :
2448 : /* Issue warnings about T having private constructors, but no friends,
2449 : and so forth.
2450 :
2451 : HAS_NONPRIVATE_METHOD is nonzero if T has any non-private methods or
2452 : static members. HAS_NONPRIVATE_STATIC_FN is nonzero if T has any
2453 : non-private static member functions. */
2454 :
2455 : static void
2456 29881679 : maybe_warn_about_overly_private_class (tree t)
2457 : {
2458 29881679 : int has_member_fn = 0;
2459 29881679 : int has_nonprivate_method = 0;
2460 29881679 : bool nonprivate_ctor = false;
2461 :
2462 29881679 : if (!warn_ctor_dtor_privacy
2463 : /* If the class has friends, those entities might create and
2464 : access instances, so we should not warn. */
2465 48 : || (CLASSTYPE_FRIEND_CLASSES (t)
2466 48 : || DECL_FRIENDLIST (TYPE_MAIN_DECL (t)))
2467 : /* We will have warned when the template was declared; there's
2468 : no need to warn on every instantiation. */
2469 29881727 : || CLASSTYPE_TEMPLATE_INSTANTIATION (t))
2470 : /* There's no reason to even consider warning about this
2471 : class. */
2472 : return;
2473 :
2474 : /* We only issue one warning, if more than one applies, because
2475 : otherwise, on code like:
2476 :
2477 : class A {
2478 : // Oops - forgot `public:'
2479 : A();
2480 : A(const A&);
2481 : ~A();
2482 : };
2483 :
2484 : we warn several times about essentially the same problem. */
2485 :
2486 : /* Check to see if all (non-constructor, non-destructor) member
2487 : functions are private. (Since there are no friends or
2488 : non-private statics, we can't ever call any of the private member
2489 : functions.) */
2490 216 : for (tree fn = TYPE_FIELDS (t); fn; fn = DECL_CHAIN (fn))
2491 168 : if (TREE_CODE (fn) == USING_DECL
2492 18 : && DECL_NAME (fn) == ctor_identifier
2493 171 : && !TREE_PRIVATE (fn))
2494 : nonprivate_ctor = true;
2495 165 : else if (!DECL_DECLARES_FUNCTION_P (fn))
2496 : /* Not a function. */;
2497 96 : else if (DECL_ARTIFICIAL (fn))
2498 : /* We're not interested in compiler-generated methods; they don't
2499 : provide any way to call private members. */;
2500 96 : else if (!TREE_PRIVATE (fn))
2501 : {
2502 48 : if (DECL_STATIC_FUNCTION_P (fn))
2503 : /* A non-private static member function is just like a
2504 : friend; it can create and invoke private member
2505 : functions, and be accessed without a class
2506 : instance. */
2507 : return;
2508 :
2509 : has_nonprivate_method = 1;
2510 : /* Keep searching for a static member function. */
2511 : }
2512 96 : else if (!DECL_CONSTRUCTOR_P (fn) && !DECL_DESTRUCTOR_P (fn))
2513 : has_member_fn = 1;
2514 :
2515 48 : if (!has_nonprivate_method && has_member_fn)
2516 : {
2517 : /* There are no non-private methods, and there's at least one
2518 : private member function that isn't a constructor or
2519 : destructor. (If all the private members are
2520 : constructors/destructors we want to use the code below that
2521 : issues error messages specifically referring to
2522 : constructors/destructors.) */
2523 12 : unsigned i;
2524 12 : tree binfo = TYPE_BINFO (t);
2525 :
2526 12 : for (i = 0; i != BINFO_N_BASE_BINFOS (binfo); i++)
2527 0 : if (BINFO_BASE_ACCESS (binfo, i) != access_private_node)
2528 : {
2529 : has_nonprivate_method = 1;
2530 : break;
2531 : }
2532 12 : if (!has_nonprivate_method)
2533 : {
2534 12 : warning (OPT_Wctor_dtor_privacy,
2535 : "all member functions in class %qT are private", t);
2536 12 : return;
2537 : }
2538 : }
2539 :
2540 : /* Even if some of the member functions are non-private, the class
2541 : won't be useful for much if all the constructors or destructors
2542 : are private: such an object can never be created or destroyed. */
2543 36 : if (tree dtor = CLASSTYPE_DESTRUCTOR (t))
2544 6 : if (TREE_PRIVATE (dtor))
2545 : {
2546 6 : warning (OPT_Wctor_dtor_privacy,
2547 : "%q#T only defines a private destructor and has no friends",
2548 : t);
2549 6 : return;
2550 : }
2551 :
2552 : /* Warn about classes that have private constructors and no friends. */
2553 30 : if (TYPE_HAS_USER_CONSTRUCTOR (t)
2554 : /* Implicitly generated constructors are always public. */
2555 30 : && !CLASSTYPE_LAZY_DEFAULT_CTOR (t))
2556 : {
2557 21 : tree copy_or_move = NULL_TREE;
2558 :
2559 : /* If a non-template class does not define a copy
2560 : constructor, one is defined for it, enabling it to avoid
2561 : this warning. For a template class, this does not
2562 : happen, and so we would normally get a warning on:
2563 :
2564 : template <class T> class C { private: C(); };
2565 :
2566 : To avoid this asymmetry, we check TYPE_HAS_COPY_CTOR. All
2567 : complete non-template or fully instantiated classes have this
2568 : flag set. */
2569 21 : if (!TYPE_HAS_COPY_CTOR (t))
2570 : nonprivate_ctor = true;
2571 : else
2572 51 : for (tree fn : ovl_range (CLASSTYPE_CONSTRUCTORS (t)))
2573 27 : if (TREE_PRIVATE (fn))
2574 12 : continue;
2575 15 : else if (copy_fn_p (fn) || move_fn_p (fn))
2576 : /* Ideally, we wouldn't count any constructor that takes
2577 : an argument of the class type as a parameter, because
2578 : such things cannot be used to construct an instance of
2579 : the class unless you already have one. */
2580 : copy_or_move = fn;
2581 : else
2582 : {
2583 : nonprivate_ctor = true;
2584 : break;
2585 : }
2586 :
2587 21 : if (!nonprivate_ctor)
2588 : {
2589 15 : auto_diagnostic_group d;
2590 15 : bool w = warning (OPT_Wctor_dtor_privacy,
2591 : "%q#T only defines private constructors and has "
2592 : "no friends", t);
2593 15 : if (w && copy_or_move)
2594 6 : inform (DECL_SOURCE_LOCATION (copy_or_move),
2595 : "%q#D is public, but requires an existing %q#T object",
2596 : copy_or_move, t);
2597 15 : return;
2598 15 : }
2599 : }
2600 : }
2601 :
2602 : /* Make BINFO's vtable have N entries, including RTTI entries,
2603 : vbase and vcall offsets, etc. Set its type and call the back end
2604 : to lay it out. */
2605 :
2606 : static void
2607 1741069 : layout_vtable_decl (tree binfo, int n)
2608 : {
2609 1741069 : tree atype;
2610 1741069 : tree vtable;
2611 :
2612 1741069 : atype = build_array_of_n_type (vtable_entry_type, n);
2613 1741069 : layout_type (atype);
2614 :
2615 : /* We may have to grow the vtable. */
2616 1741069 : vtable = get_vtbl_decl_for_binfo (binfo);
2617 1741069 : if (!same_type_p (TREE_TYPE (vtable), atype))
2618 : {
2619 587528 : TREE_TYPE (vtable) = atype;
2620 587528 : DECL_SIZE (vtable) = DECL_SIZE_UNIT (vtable) = NULL_TREE;
2621 587528 : layout_decl (vtable, 0);
2622 : }
2623 1741069 : }
2624 :
2625 : /* True iff FNDECL and BASE_FNDECL (both non-static member functions)
2626 : have the same signature. */
2627 :
2628 : int
2629 79160834 : same_signature_p (const_tree fndecl, const_tree base_fndecl)
2630 : {
2631 : /* One destructor overrides another if they are the same kind of
2632 : destructor. */
2633 121335492 : if (DECL_DESTRUCTOR_P (base_fndecl) && DECL_DESTRUCTOR_P (fndecl)
2634 100144441 : && special_function_p (base_fndecl) == special_function_p (fndecl))
2635 : return 1;
2636 : /* But a non-destructor never overrides a destructor, nor vice
2637 : versa, nor do different kinds of destructors override
2638 : one-another. For example, a complete object destructor does not
2639 : override a deleting destructor. */
2640 193574635 : if (DECL_DESTRUCTOR_P (base_fndecl) || DECL_DESTRUCTOR_P (fndecl))
2641 : return 0;
2642 :
2643 48109397 : if (DECL_NAME (fndecl) == DECL_NAME (base_fndecl)
2644 48109397 : || (DECL_CONV_FN_P (fndecl)
2645 111 : && DECL_CONV_FN_P (base_fndecl)
2646 90 : && same_type_p (DECL_CONV_FN_TYPE (fndecl),
2647 : DECL_CONV_FN_TYPE (base_fndecl))))
2648 : {
2649 16987780 : tree fntype = TREE_TYPE (fndecl);
2650 16987780 : tree base_fntype = TREE_TYPE (base_fndecl);
2651 16987780 : if (type_memfn_quals (fntype) == type_memfn_quals (base_fntype)
2652 16987407 : && type_memfn_rqual (fntype) == type_memfn_rqual (base_fntype)
2653 33975184 : && compparms (FUNCTION_FIRST_USER_PARMTYPE (fndecl),
2654 16987404 : FUNCTION_FIRST_USER_PARMTYPE (base_fndecl)))
2655 : return 1;
2656 : }
2657 : return 0;
2658 : }
2659 :
2660 : /* Returns TRUE if DERIVED is a binfo containing the binfo BASE as a
2661 : subobject. */
2662 :
2663 : static bool
2664 231051 : base_derived_from (tree derived, tree base)
2665 : {
2666 231051 : tree probe;
2667 :
2668 231327 : for (probe = base; probe; probe = BINFO_INHERITANCE_CHAIN (probe))
2669 : {
2670 231222 : if (probe == derived)
2671 : return true;
2672 1551 : else if (BINFO_VIRTUAL_P (probe))
2673 : /* If we meet a virtual base, we can't follow the inheritance
2674 : any more. See if the complete type of DERIVED contains
2675 : such a virtual base. */
2676 1275 : return (binfo_for_vbase (BINFO_TYPE (probe), BINFO_TYPE (derived))
2677 1275 : != NULL_TREE);
2678 : }
2679 : return false;
2680 : }
2681 :
2682 7907853 : struct find_final_overrider_data {
2683 : /* The function for which we are trying to find a final overrider. */
2684 : tree fn;
2685 : /* The base class in which the function was declared. */
2686 : tree declaring_base;
2687 : /* The candidate overriders. */
2688 : tree candidates;
2689 : /* Path to most derived. */
2690 : auto_vec<tree> path;
2691 : };
2692 :
2693 : /* Add the overrider along the current path to FFOD->CANDIDATES.
2694 : Returns true if an overrider was found; false otherwise. */
2695 :
2696 : static bool
2697 13037561 : dfs_find_final_overrider_1 (tree binfo,
2698 : find_final_overrider_data *ffod,
2699 : unsigned depth)
2700 : {
2701 13037561 : tree method;
2702 :
2703 : /* If BINFO is not the most derived type, try a more derived class.
2704 : A definition there will overrider a definition here. */
2705 13037561 : if (depth)
2706 : {
2707 4899197 : depth--;
2708 4899197 : if (dfs_find_final_overrider_1
2709 4899197 : (ffod->path[depth], ffod, depth))
2710 : return true;
2711 : }
2712 :
2713 10438340 : method = look_for_overrides_here (BINFO_TYPE (binfo), ffod->fn);
2714 10438340 : if (method)
2715 : {
2716 8138364 : tree *candidate = &ffod->candidates;
2717 :
2718 : /* Remove any candidates overridden by this new function. */
2719 8138886 : while (*candidate)
2720 : {
2721 : /* If *CANDIDATE overrides METHOD, then METHOD
2722 : cannot override anything else on the list. */
2723 230529 : if (base_derived_from (TREE_VALUE (*candidate), binfo))
2724 : return true;
2725 : /* If METHOD overrides *CANDIDATE, remove *CANDIDATE. */
2726 522 : if (base_derived_from (binfo, TREE_VALUE (*candidate)))
2727 468 : *candidate = TREE_CHAIN (*candidate);
2728 : else
2729 54 : candidate = &TREE_CHAIN (*candidate);
2730 : }
2731 :
2732 : /* Add the new function. */
2733 7908357 : ffod->candidates = tree_cons (method, binfo, ffod->candidates);
2734 7908357 : return true;
2735 : }
2736 :
2737 : return false;
2738 : }
2739 :
2740 : /* Called from find_final_overrider via dfs_walk. */
2741 :
2742 : static tree
2743 29892530 : dfs_find_final_overrider_pre (tree binfo, void *data)
2744 : {
2745 29892530 : find_final_overrider_data *ffod = (find_final_overrider_data *) data;
2746 :
2747 29892530 : if (binfo == ffod->declaring_base)
2748 16276728 : dfs_find_final_overrider_1 (binfo, ffod, ffod->path.length ());
2749 29892530 : ffod->path.safe_push (binfo);
2750 :
2751 29892530 : return NULL_TREE;
2752 : }
2753 :
2754 : static tree
2755 29892530 : dfs_find_final_overrider_post (tree /*binfo*/, void *data)
2756 : {
2757 29892530 : find_final_overrider_data *ffod = (find_final_overrider_data *) data;
2758 29892530 : ffod->path.pop ();
2759 :
2760 29892530 : return NULL_TREE;
2761 : }
2762 :
2763 : /* Returns a TREE_LIST whose TREE_PURPOSE is the final overrider for
2764 : FN and whose TREE_VALUE is the binfo for the base where the
2765 : overriding occurs. BINFO (in the hierarchy dominated by the binfo
2766 : DERIVED) is the base object in which FN is declared. */
2767 :
2768 : static tree
2769 7907853 : find_final_overrider (tree derived, tree binfo, tree fn)
2770 : {
2771 7907853 : find_final_overrider_data ffod;
2772 :
2773 : /* Getting this right is a little tricky. This is valid:
2774 :
2775 : struct S { virtual void f (); };
2776 : struct T { virtual void f (); };
2777 : struct U : public S, public T { };
2778 :
2779 : even though calling `f' in `U' is ambiguous. But,
2780 :
2781 : struct R { virtual void f(); };
2782 : struct S : virtual public R { virtual void f (); };
2783 : struct T : virtual public R { virtual void f (); };
2784 : struct U : public S, public T { };
2785 :
2786 : is not -- there's no way to decide whether to put `S::f' or
2787 : `T::f' in the vtable for `R'.
2788 :
2789 : The solution is to look at all paths to BINFO. If we find
2790 : different overriders along any two, then there is a problem. */
2791 7907853 : if (DECL_THUNK_P (fn))
2792 0 : fn = THUNK_TARGET (fn);
2793 :
2794 : /* Determine the depth of the hierarchy. */
2795 7907853 : ffod.fn = fn;
2796 7907853 : ffod.declaring_base = binfo;
2797 7907853 : ffod.candidates = NULL_TREE;
2798 7907853 : ffod.path.create (30);
2799 :
2800 7907853 : dfs_walk_all (derived, dfs_find_final_overrider_pre,
2801 : dfs_find_final_overrider_post, &ffod);
2802 :
2803 : /* If there was no winner, issue an error message. */
2804 7907853 : if (!ffod.candidates || TREE_CHAIN (ffod.candidates))
2805 36 : return error_mark_node;
2806 :
2807 : return ffod.candidates;
2808 7907853 : }
2809 :
2810 : /* Return the index of the vcall offset for FN when TYPE is used as a
2811 : virtual base. */
2812 :
2813 : static tree
2814 356565 : get_vcall_index (tree fn, tree type)
2815 : {
2816 356565 : vec<tree_pair_s, va_gc> *indices = CLASSTYPE_VCALL_INDICES (type);
2817 356565 : tree_pair_p p;
2818 356565 : unsigned ix;
2819 :
2820 362518 : FOR_EACH_VEC_SAFE_ELT (indices, ix, p)
2821 1084158 : if ((DECL_DESTRUCTOR_P (fn) && DECL_DESTRUCTOR_P (p->purpose))
2822 368166 : || same_signature_p (fn, p->purpose))
2823 356565 : return p->value;
2824 :
2825 : /* There should always be an appropriate index. */
2826 0 : gcc_unreachable ();
2827 : }
2828 :
2829 : /* Given a DECL_VINDEX of a virtual function found in BINFO, return the final
2830 : overrider at that index in the vtable. This should only be used when we
2831 : know that BINFO is correct for the dynamic type of the object. */
2832 :
2833 : tree
2834 1069 : lookup_vfn_in_binfo (tree idx, tree binfo)
2835 : {
2836 1069 : int ix = tree_to_shwi (idx);
2837 1069 : if (TARGET_VTABLE_USES_DESCRIPTORS)
2838 : ix /= MAX (TARGET_VTABLE_USES_DESCRIPTORS, 1);
2839 1358 : while (BINFO_PRIMARY_P (binfo))
2840 : /* BINFO_VIRTUALS in a primary base isn't accurate, find the derived
2841 : class that actually owns the vtable. */
2842 289 : binfo = BINFO_INHERITANCE_CHAIN (binfo);
2843 1069 : tree virtuals = BINFO_VIRTUALS (binfo);
2844 1069 : return TREE_VALUE (chain_index (ix, virtuals));
2845 : }
2846 :
2847 : /* Update an entry in the vtable for BINFO, which is in the hierarchy
2848 : dominated by T. FN is the old function; VIRTUALS points to the
2849 : corresponding position in the new BINFO_VIRTUALS list. IX is the index
2850 : of that entry in the list. */
2851 :
2852 : static void
2853 7484411 : update_vtable_entry_for_fn (tree t, tree binfo, tree fn, tree* virtuals,
2854 : unsigned ix)
2855 : {
2856 7484411 : tree b;
2857 7484411 : tree overrider;
2858 7484411 : tree delta;
2859 7484411 : tree virtual_base;
2860 7484411 : tree first_defn;
2861 7484411 : tree overrider_fn, overrider_target;
2862 7484411 : tree target_fn = DECL_THUNK_P (fn) ? THUNK_TARGET (fn) : fn;
2863 7484411 : tree over_return, base_return;
2864 7484411 : bool lost = false;
2865 :
2866 : /* Find the nearest primary base (possibly binfo itself) which defines
2867 : this function; this is the class the caller will convert to when
2868 : calling FN through BINFO. */
2869 9725085 : for (b = binfo; ; b = get_primary_binfo (b))
2870 : {
2871 9725085 : gcc_assert (b);
2872 9725085 : if (look_for_overrides_here (BINFO_TYPE (b), target_fn))
2873 : break;
2874 :
2875 : /* The nearest definition is from a lost primary. */
2876 2240674 : if (BINFO_LOST_PRIMARY_P (b))
2877 412 : lost = true;
2878 : }
2879 7484411 : first_defn = b;
2880 :
2881 : /* Find the final overrider. */
2882 7484411 : overrider = find_final_overrider (TYPE_BINFO (t), b, target_fn);
2883 7484411 : if (overrider == error_mark_node)
2884 : {
2885 9 : error ("no unique final overrider for %qD in %qT", target_fn, t);
2886 9 : return;
2887 : }
2888 7484402 : overrider_target = overrider_fn = TREE_PURPOSE (overrider);
2889 :
2890 : /* Check for adjusting covariant return types. */
2891 7484402 : over_return = TREE_TYPE (TREE_TYPE (overrider_target));
2892 7484402 : base_return = TREE_TYPE (TREE_TYPE (target_fn));
2893 :
2894 7484402 : if (INDIRECT_TYPE_P (over_return)
2895 651233 : && TREE_CODE (over_return) == TREE_CODE (base_return)
2896 651224 : && CLASS_TYPE_P (TREE_TYPE (over_return))
2897 41871 : && CLASS_TYPE_P (TREE_TYPE (base_return))
2898 : /* If the overrider is invalid, don't even try. */
2899 7526267 : && !DECL_INVALID_OVERRIDER_P (overrider_target))
2900 : {
2901 : /* If FN is a covariant thunk, we must figure out the adjustment
2902 : to the final base FN was converting to. As OVERRIDER_TARGET might
2903 : also be converting to the return type of FN, we have to
2904 : combine the two conversions here. */
2905 41832 : tree fixed_offset, virtual_offset;
2906 :
2907 41832 : over_return = TREE_TYPE (over_return);
2908 41832 : base_return = TREE_TYPE (base_return);
2909 :
2910 41832 : if (DECL_THUNK_P (fn))
2911 : {
2912 60 : gcc_assert (DECL_RESULT_THUNK_P (fn));
2913 60 : fixed_offset = ssize_int (THUNK_FIXED_OFFSET (fn));
2914 60 : virtual_offset = THUNK_VIRTUAL_OFFSET (fn);
2915 : }
2916 : else
2917 : fixed_offset = virtual_offset = NULL_TREE;
2918 :
2919 60 : if (virtual_offset)
2920 : /* Find the equivalent binfo within the return type of the
2921 : overriding function. We will want the vbase offset from
2922 : there. */
2923 54 : virtual_offset = binfo_for_vbase (BINFO_TYPE (virtual_offset),
2924 : over_return);
2925 41778 : else if (!same_type_ignoring_top_level_qualifiers_p
2926 41778 : (over_return, base_return))
2927 : {
2928 : /* There was no existing virtual thunk (which takes
2929 : precedence). So find the binfo of the base function's
2930 : return type within the overriding function's return type.
2931 : Fortunately we know the covariancy is valid (it
2932 : has already been checked), so we can just iterate along
2933 : the binfos, which have been chained in inheritance graph
2934 : order. Of course it is lame that we have to repeat the
2935 : search here anyway -- we should really be caching pieces
2936 : of the vtable and avoiding this repeated work. */
2937 379 : tree thunk_binfo = NULL_TREE;
2938 379 : tree base_binfo = TYPE_BINFO (base_return);
2939 :
2940 : /* Find the base binfo within the overriding function's
2941 : return type. We will always find a thunk_binfo, except
2942 : when the covariancy is invalid (which we will have
2943 : already diagnosed). */
2944 379 : if (base_binfo)
2945 984 : for (thunk_binfo = TYPE_BINFO (over_return); thunk_binfo;
2946 608 : thunk_binfo = TREE_CHAIN (thunk_binfo))
2947 984 : if (SAME_BINFO_TYPE_P (BINFO_TYPE (thunk_binfo),
2948 : BINFO_TYPE (base_binfo)))
2949 : break;
2950 379 : gcc_assert (thunk_binfo || errorcount);
2951 :
2952 : /* See if virtual inheritance is involved. */
2953 379 : for (virtual_offset = thunk_binfo;
2954 671 : virtual_offset;
2955 292 : virtual_offset = BINFO_INHERITANCE_CHAIN (virtual_offset))
2956 537 : if (BINFO_VIRTUAL_P (virtual_offset))
2957 : break;
2958 :
2959 379 : if (virtual_offset
2960 510 : || (thunk_binfo && !BINFO_OFFSET_ZEROP (thunk_binfo)))
2961 : {
2962 320 : tree offset = fold_convert (ssizetype, BINFO_OFFSET (thunk_binfo));
2963 :
2964 320 : if (virtual_offset)
2965 : {
2966 : /* We convert via virtual base. Adjust the fixed
2967 : offset to be from there. */
2968 245 : offset =
2969 245 : size_diffop (offset,
2970 : fold_convert (ssizetype,
2971 : BINFO_OFFSET (virtual_offset)));
2972 : }
2973 320 : if (fixed_offset)
2974 : /* There was an existing fixed offset, this must be
2975 : from the base just converted to, and the base the
2976 : FN was thunking to. */
2977 6 : fixed_offset = size_binop (PLUS_EXPR, fixed_offset, offset);
2978 : else
2979 : fixed_offset = offset;
2980 : }
2981 : }
2982 :
2983 41832 : if (fixed_offset || virtual_offset)
2984 : /* Replace the overriding function with a covariant thunk. We
2985 : will emit the overriding function in its own slot as
2986 : well. */
2987 374 : overrider_fn = make_thunk (overrider_target, /*this_adjusting=*/0,
2988 : fixed_offset, virtual_offset);
2989 : }
2990 : else
2991 7442570 : gcc_assert (DECL_INVALID_OVERRIDER_P (overrider_target) ||
2992 : !DECL_THUNK_P (fn));
2993 :
2994 : /* If we need a covariant thunk, then we may need to adjust first_defn.
2995 : The ABI specifies that the thunks emitted with a function are
2996 : determined by which bases the function overrides, so we need to be
2997 : sure that we're using a thunk for some overridden base; even if we
2998 : know that the necessary this adjustment is zero, there may not be an
2999 : appropriate zero-this-adjustment thunk for us to use since thunks for
3000 : overriding virtual bases always use the vcall offset.
3001 :
3002 : Furthermore, just choosing any base that overrides this function isn't
3003 : quite right, as this slot won't be used for calls through a type that
3004 : puts a covariant thunk here. Calling the function through such a type
3005 : will use a different slot, and that slot is the one that determines
3006 : the thunk emitted for that base.
3007 :
3008 : So, keep looking until we find the base that we're really overriding
3009 : in this slot: the nearest primary base that doesn't use a covariant
3010 : thunk in this slot. */
3011 7484402 : if (overrider_target != overrider_fn)
3012 : {
3013 374 : if (BINFO_TYPE (b) == DECL_CONTEXT (overrider_target))
3014 : /* We already know that the overrider needs a covariant thunk. */
3015 132 : b = get_primary_binfo (b);
3016 30 : for (; ; b = get_primary_binfo (b))
3017 : {
3018 404 : tree main_binfo = TYPE_BINFO (BINFO_TYPE (b));
3019 404 : tree bv = chain_index (ix, BINFO_VIRTUALS (main_binfo));
3020 404 : if (!DECL_THUNK_P (TREE_VALUE (bv)))
3021 : break;
3022 30 : if (BINFO_LOST_PRIMARY_P (b))
3023 6 : lost = true;
3024 30 : }
3025 : first_defn = b;
3026 : }
3027 :
3028 : /* Assume that we will produce a thunk that convert all the way to
3029 : the final overrider, and not to an intermediate virtual base. */
3030 7484402 : virtual_base = NULL_TREE;
3031 :
3032 : /* See if we can convert to an intermediate virtual base first, and then
3033 : use the vcall offset located there to finish the conversion. */
3034 7679844 : for (; b; b = BINFO_INHERITANCE_CHAIN (b))
3035 : {
3036 : /* If we find the final overrider, then we can stop
3037 : walking. */
3038 7679844 : if (SAME_BINFO_TYPE_P (BINFO_TYPE (b),
3039 : BINFO_TYPE (TREE_VALUE (overrider))))
3040 : break;
3041 :
3042 : /* If we find a virtual base, and we haven't yet found the
3043 : overrider, then there is a virtual base between the
3044 : declaring base (first_defn) and the final overrider. */
3045 552007 : if (BINFO_VIRTUAL_P (b))
3046 : {
3047 : virtual_base = b;
3048 : break;
3049 : }
3050 : }
3051 :
3052 : /* Compute the constant adjustment to the `this' pointer. The
3053 : `this' pointer, when this function is called, will point at BINFO
3054 : (or one of its primary bases, which are at the same offset). */
3055 7484402 : if (virtual_base)
3056 : /* The `this' pointer needs to be adjusted from the declaration to
3057 : the nearest virtual base. */
3058 1069695 : delta = size_diffop_loc (input_location,
3059 356565 : fold_convert (ssizetype, BINFO_OFFSET (virtual_base)),
3060 356565 : fold_convert (ssizetype, BINFO_OFFSET (first_defn)));
3061 7127837 : else if (lost)
3062 : /* If the nearest definition is in a lost primary, we don't need an
3063 : entry in our vtable. Except possibly in a constructor vtable,
3064 : if we happen to get our primary back. In that case, the offset
3065 : will be zero, as it will be a primary base. */
3066 367 : delta = size_zero_node;
3067 : else
3068 : /* The `this' pointer needs to be adjusted from pointing to
3069 : BINFO to pointing at the base where the final overrider
3070 : appears. */
3071 21382410 : delta = size_diffop_loc (input_location,
3072 7127470 : fold_convert (ssizetype,
3073 : BINFO_OFFSET (TREE_VALUE (overrider))),
3074 7127470 : fold_convert (ssizetype, BINFO_OFFSET (binfo)));
3075 :
3076 7484402 : modify_vtable_entry (t, binfo, overrider_fn, delta, virtuals);
3077 :
3078 7484402 : if (virtual_base)
3079 713130 : BV_VCALL_INDEX (*virtuals)
3080 713130 : = get_vcall_index (overrider_target, BINFO_TYPE (virtual_base));
3081 : else
3082 7127837 : BV_VCALL_INDEX (*virtuals) = NULL_TREE;
3083 :
3084 7484402 : BV_LOST_PRIMARY (*virtuals) = lost;
3085 : }
3086 :
3087 : /* Called from modify_all_vtables via dfs_walk. */
3088 :
3089 : static tree
3090 53347621 : dfs_modify_vtables (tree binfo, void* data)
3091 : {
3092 53347621 : tree t = (tree) data;
3093 53347621 : tree virtuals;
3094 53347621 : tree old_virtuals;
3095 53347621 : unsigned ix;
3096 :
3097 53347621 : if (!TYPE_CONTAINS_VPTR_P (BINFO_TYPE (binfo)))
3098 : /* A base without a vtable needs no modification, and its bases
3099 : are uninteresting. */
3100 : return dfs_skip_bases;
3101 :
3102 4584917 : if (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), t)
3103 4584917 : && !CLASSTYPE_HAS_PRIMARY_BASE_P (t))
3104 : /* Don't do the primary vtable, if it's new. */
3105 : return NULL_TREE;
3106 :
3107 4335563 : if (BINFO_PRIMARY_P (binfo) && !BINFO_VIRTUAL_P (binfo))
3108 : /* There's no need to modify the vtable for a non-virtual primary
3109 : base; we're not going to use that vtable anyhow. We do still
3110 : need to do this for virtual primary bases, as they could become
3111 : non-primary in a construction vtable. */
3112 : return NULL_TREE;
3113 :
3114 1744403 : make_new_vtable (t, binfo);
3115 :
3116 : /* Now, go through each of the virtual functions in the virtual
3117 : function table for BINFO. Find the final overrider, and update
3118 : the BINFO_VIRTUALS list appropriately. */
3119 1744403 : for (ix = 0, virtuals = BINFO_VIRTUALS (binfo),
3120 1744403 : old_virtuals = BINFO_VIRTUALS (TYPE_BINFO (BINFO_TYPE (binfo)));
3121 9228814 : virtuals;
3122 7484411 : ix++, virtuals = TREE_CHAIN (virtuals),
3123 7484411 : old_virtuals = TREE_CHAIN (old_virtuals))
3124 7484411 : update_vtable_entry_for_fn (t,
3125 : binfo,
3126 7484411 : BV_FN (old_virtuals),
3127 : &virtuals, ix);
3128 :
3129 : return NULL_TREE;
3130 : }
3131 :
3132 : /* Update all of the primary and secondary vtables for T. Create new
3133 : vtables as required, and initialize their RTTI information. Each
3134 : of the functions in VIRTUALS is declared in T and may override a
3135 : virtual function from a base class; find and modify the appropriate
3136 : entries to point to the overriding functions. Returns a list, in
3137 : declaration order, of the virtual functions that are declared in T,
3138 : but do not appear in the primary base class vtable, and which
3139 : should therefore be appended to the end of the vtable for T. */
3140 :
3141 : static tree
3142 49904436 : modify_all_vtables (tree t, tree virtuals)
3143 : {
3144 49904436 : tree binfo = TYPE_BINFO (t);
3145 49904436 : tree *fnsp;
3146 :
3147 : /* Mangle the vtable name before entering dfs_walk (c++/51884). */
3148 49904436 : if (TYPE_CONTAINS_VPTR_P (t))
3149 1741069 : get_vtable_decl (t, false);
3150 :
3151 : /* Update all of the vtables. */
3152 49904436 : dfs_walk_once (binfo, dfs_modify_vtables, NULL, t);
3153 :
3154 : /* Add virtual functions not already in our primary vtable. These
3155 : will be both those introduced by this class, and those overridden
3156 : from secondary bases. It does not include virtuals merely
3157 : inherited from secondary bases. */
3158 57800328 : for (fnsp = &virtuals; *fnsp; )
3159 : {
3160 7895892 : tree fn = TREE_VALUE (*fnsp);
3161 :
3162 7895892 : if (!value_member (fn, BINFO_VIRTUALS (binfo))
3163 12768659 : || DECL_VINDEX (fn) == error_mark_node)
3164 : {
3165 : /* We don't need to adjust the `this' pointer when
3166 : calling this function. */
3167 3023125 : BV_DELTA (*fnsp) = integer_zero_node;
3168 3023125 : BV_VCALL_INDEX (*fnsp) = NULL_TREE;
3169 :
3170 : /* This is a function not already in our vtable. Keep it. */
3171 3023125 : fnsp = &TREE_CHAIN (*fnsp);
3172 : }
3173 : else
3174 : /* We've already got an entry for this function. Skip it. */
3175 4872767 : *fnsp = TREE_CHAIN (*fnsp);
3176 : }
3177 :
3178 49904436 : return virtuals;
3179 : }
3180 :
3181 : /* Get the base virtual function declarations in T that have the
3182 : indicated NAME. */
3183 :
3184 : static void
3185 238452 : get_basefndecls (tree name, tree t, vec<tree> *base_fndecls)
3186 : {
3187 238452 : bool found_decls = false;
3188 :
3189 : /* Find virtual functions in T with the indicated NAME. */
3190 341969 : for (tree method : ovl_range (get_class_binding (t, name)))
3191 : {
3192 79236 : if (TREE_CODE (method) == FUNCTION_DECL && DECL_VINDEX (method))
3193 : {
3194 20664 : base_fndecls->safe_push (method);
3195 20664 : found_decls = true;
3196 : }
3197 : }
3198 :
3199 238452 : if (found_decls)
3200 : return;
3201 :
3202 219024 : int n_baseclasses = BINFO_N_BASE_BINFOS (TYPE_BINFO (t));
3203 249241 : for (int i = 0; i < n_baseclasses; i++)
3204 : {
3205 30217 : tree basetype = BINFO_TYPE (BINFO_BASE_BINFO (TYPE_BINFO (t), i));
3206 30217 : get_basefndecls (name, basetype, base_fndecls);
3207 : }
3208 : }
3209 :
3210 : /* If this method overrides a virtual method from a base, then mark
3211 : this member function as being virtual as well. Do 'final' and
3212 : 'override' checks too. */
3213 :
3214 : void
3215 146418118 : check_for_override (tree decl, tree ctype)
3216 : {
3217 146418118 : if (TREE_CODE (decl) == TEMPLATE_DECL)
3218 : /* In [temp.mem] we have:
3219 :
3220 : A specialization of a member function template does not
3221 : override a virtual function from a base class. */
3222 : return;
3223 :
3224 : /* IDENTIFIER_VIRTUAL_P indicates whether the name has ever been
3225 : used for a vfunc. That avoids the expensive look_for_overrides
3226 : call that when we know there's nothing to find. As conversion
3227 : operators for the same type can have distinct identifiers, we
3228 : cannot optimize those in that way. */
3229 119599430 : if ((IDENTIFIER_VIRTUAL_P (DECL_NAME (decl))
3230 105149651 : || DECL_CONV_FN_P (decl))
3231 16033646 : && look_for_overrides (ctype, decl)
3232 : /* Check staticness after we've checked if we 'override'. */
3233 123110925 : && !DECL_STATIC_FUNCTION_P (decl))
3234 : {
3235 : /* Set DECL_VINDEX to a value that is neither an INTEGER_CST nor
3236 : the error_mark_node so that we know it is an overriding
3237 : function. */
3238 3511489 : DECL_VINDEX (decl) = decl;
3239 :
3240 3511489 : if (warn_override
3241 6 : && !DECL_OVERRIDE_P (decl)
3242 5 : && !DECL_FINAL_P (decl)
3243 3511493 : && !DECL_DESTRUCTOR_P (decl))
3244 3 : warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wsuggest_override,
3245 : "%qD can be marked override", decl);
3246 : }
3247 116087941 : else if (DECL_OVERRIDE_P (decl))
3248 20 : error ("%q+#D marked %<override%>, but does not override", decl);
3249 :
3250 119599430 : if (DECL_VIRTUAL_P (decl))
3251 : {
3252 : /* Remember this identifier is virtual name. */
3253 6271889 : IDENTIFIER_VIRTUAL_P (DECL_NAME (decl)) = true;
3254 :
3255 6271889 : if (!DECL_VINDEX (decl))
3256 : /* It's a new vfunc. */
3257 2760416 : DECL_VINDEX (decl) = error_mark_node;
3258 :
3259 6271889 : if (DECL_DESTRUCTOR_P (decl))
3260 1623975 : TYPE_HAS_NONTRIVIAL_DESTRUCTOR (ctype) = true;
3261 :
3262 6271889 : if (DECL_HAS_CONTRACTS_P (decl))
3263 12 : error_at (DECL_SOURCE_LOCATION (decl),
3264 : "contracts cannot be added to virtual functions");
3265 : }
3266 113327541 : else if (DECL_FINAL_P (decl))
3267 19 : error ("%q+#D marked %<final%>, but is not virtual", decl);
3268 : }
3269 :
3270 : /* Warn about hidden virtual functions that are not overridden in t.
3271 : We know that constructors and destructors don't apply. */
3272 :
3273 : static void
3274 424488 : warn_hidden (tree t)
3275 : {
3276 424488 : if (vec<tree, va_gc> *member_vec = CLASSTYPE_MEMBER_VEC (t))
3277 2118869 : for (unsigned ix = member_vec->length (); ix--;)
3278 : {
3279 1975761 : tree fns = (*member_vec)[ix];
3280 :
3281 1975761 : if (!OVL_P (fns))
3282 1975603 : continue;
3283 :
3284 1084946 : tree name = OVL_NAME (fns);
3285 1084946 : size_t num_fns = 0; /* The number of fndecls in fns. */
3286 1084946 : auto_vec<tree, 20> base_fndecls;
3287 1084946 : tree base_binfo;
3288 1084946 : tree binfo;
3289 1084946 : unsigned j;
3290 1084946 : size_t num_overriders = 0;
3291 1084946 : hash_set<tree> overriden_base_fndecls;
3292 : /* base_fndecls that are hidden but not overriden. The "value"
3293 : contains the last fndecl we saw that hides the "key". */
3294 1084946 : hash_map<tree, tree> hidden_base_fndecls;
3295 :
3296 1084946 : if (IDENTIFIER_CDTOR_P (name))
3297 358681 : continue;
3298 :
3299 : /* Iterate through all of the base classes looking for possibly
3300 : hidden functions. */
3301 934500 : for (binfo = TYPE_BINFO (t), j = 0;
3302 934500 : BINFO_BASE_ITERATE (binfo, j, base_binfo); j++)
3303 : {
3304 208235 : tree basetype = BINFO_TYPE (base_binfo);
3305 208235 : get_basefndecls (name, basetype, &base_fndecls);
3306 : }
3307 :
3308 : /* If there are no functions to hide, continue. */
3309 726265 : if (base_fndecls.is_empty ())
3310 706843 : continue;
3311 :
3312 : /* Find all the base_fndecls that are overridden, as well as those
3313 : that are hidden, in T. */
3314 41164 : for (tree fndecl : ovl_range (fns))
3315 : {
3316 20575 : bool template_p = TREE_CODE (fndecl) == TEMPLATE_DECL;
3317 20575 : bool fndecl_overrides_p = false;
3318 20575 : fndecl = STRIP_TEMPLATE (fndecl);
3319 20575 : if (TREE_CODE (fndecl) != FUNCTION_DECL
3320 20575 : || fndecl == conv_op_marker)
3321 15 : continue;
3322 20560 : num_fns++;
3323 43423 : for (size_t k = 0; k < base_fndecls.length (); k++)
3324 : {
3325 22863 : if (!base_fndecls[k] || !DECL_VINDEX (base_fndecls[k]))
3326 0 : continue;
3327 22863 : if (IDENTIFIER_CONV_OP_P (name)
3328 22863 : && !same_type_p (DECL_CONV_FN_TYPE (fndecl),
3329 : DECL_CONV_FN_TYPE (base_fndecls[k])))
3330 : /* If base_fndecl[k] and fndecl are conversion operators
3331 : to different types, they're unrelated. */
3332 : ;
3333 22830 : else if (!template_p /* Template methods don't override. */
3334 22830 : && same_signature_p (fndecl, base_fndecls[k]))
3335 : {
3336 20447 : overriden_base_fndecls.add (base_fndecls[k]);
3337 20447 : fndecl_overrides_p = true;
3338 : }
3339 : else
3340 : {
3341 : /* fndecl hides base_fndecls[k]. */
3342 2383 : hidden_base_fndecls.put (base_fndecls[k], fndecl);
3343 : }
3344 : }
3345 20560 : if (fndecl_overrides_p)
3346 20441 : ++num_overriders;
3347 : }
3348 :
3349 19422 : if (warn_overloaded_virtual == 1 && num_overriders == num_fns)
3350 : /* All the fns override a base virtual. */
3351 19264 : continue;
3352 :
3353 : /* Now give a warning for all hidden methods. Note that a method that
3354 : is both in hidden_base_fndecls and overriden_base_fndecls is not
3355 : hidden. */
3356 620 : for (auto hidden_base_fndecl : hidden_base_fndecls)
3357 : {
3358 152 : tree hidden_fndecl = hidden_base_fndecl.first;
3359 265 : if (!hidden_fndecl
3360 152 : || overriden_base_fndecls.contains (hidden_fndecl))
3361 113 : continue;
3362 39 : auto_diagnostic_group d;
3363 39 : if (warning_at (location_of (hidden_fndecl),
3364 39 : OPT_Woverloaded_virtual_,
3365 : "%qD was hidden", hidden_fndecl))
3366 : {
3367 36 : tree hider = hidden_base_fndecl.second;
3368 36 : inform (location_of (hider), " by %qD", hider);
3369 : }
3370 39 : }
3371 1084946 : }
3372 424488 : }
3373 :
3374 : /* Recursive helper for finish_struct_anon. */
3375 :
3376 : static void
3377 219936 : finish_struct_anon_r (tree field)
3378 : {
3379 982006 : for (tree elt = TYPE_FIELDS (TREE_TYPE (field)); elt; elt = DECL_CHAIN (elt))
3380 : {
3381 : /* We're generally only interested in entities the user
3382 : declared, but we also find nested classes by noticing
3383 : the TYPE_DECL that we create implicitly. You're
3384 : allowed to put one anonymous union inside another,
3385 : though, so we explicitly tolerate that. We use
3386 : TYPE_UNNAMED_P rather than ANON_AGGR_TYPE_P so that
3387 : we also allow unnamed types used for defining fields. */
3388 762070 : if (DECL_ARTIFICIAL (elt)
3389 762070 : && (!DECL_IMPLICIT_TYPEDEF_P (elt)
3390 111750 : || TYPE_UNNAMED_P (TREE_TYPE (elt))))
3391 257185 : continue;
3392 :
3393 504885 : TREE_PRIVATE (elt) = TREE_PRIVATE (field);
3394 504885 : TREE_PROTECTED (elt) = TREE_PROTECTED (field);
3395 :
3396 : /* Recurse into the anonymous aggregates to correctly handle
3397 : access control (c++/24926):
3398 :
3399 : class A {
3400 : union {
3401 : union {
3402 : int i;
3403 : };
3404 : };
3405 : };
3406 :
3407 : int j=A().i; */
3408 504885 : if (DECL_NAME (elt) == NULL_TREE
3409 504885 : && ANON_AGGR_TYPE_P (TREE_TYPE (elt)))
3410 36455 : finish_struct_anon_r (elt);
3411 : }
3412 219936 : }
3413 :
3414 : /* Fix up any anonymous union/struct members of T. */
3415 :
3416 : static void
3417 49904436 : finish_struct_anon (tree t)
3418 : {
3419 323568063 : for (tree field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
3420 : {
3421 273663627 : if (TREE_STATIC (field))
3422 14100362 : continue;
3423 259563265 : if (TREE_CODE (field) != FIELD_DECL)
3424 242316143 : continue;
3425 :
3426 17247122 : if (DECL_NAME (field) == NULL_TREE
3427 17247122 : && ANON_AGGR_TYPE_P (TREE_TYPE (field)))
3428 183481 : finish_struct_anon_r (field);
3429 : }
3430 49904436 : }
3431 :
3432 : /* Add T to CLASSTYPE_DECL_LIST of current_class_type which
3433 : will be used later during class template instantiation.
3434 : When FRIEND_P is zero, T can be a static member data (VAR_DECL),
3435 : a non-static member data (FIELD_DECL), a member function
3436 : (FUNCTION_DECL), a nested type (RECORD_TYPE, ENUM_TYPE),
3437 : a typedef (TYPE_DECL) or a member class template (TEMPLATE_DECL)
3438 : When FRIEND_P is nonzero, T is either a friend class
3439 : (RECORD_TYPE, TEMPLATE_DECL) or a friend function
3440 : (FUNCTION_DECL, TEMPLATE_DECL). */
3441 :
3442 : void
3443 447305203 : maybe_add_class_template_decl_list (tree type, tree t, int friend_p)
3444 : {
3445 447305203 : if (CLASSTYPE_TEMPLATE_INFO (type)
3446 447305203 : && TREE_CODE (t) != CONST_DECL)
3447 : {
3448 388071681 : tree purpose = friend_p ? NULL_TREE : type;
3449 :
3450 388071681 : CLASSTYPE_DECL_LIST (type)
3451 776143362 : = tree_cons (purpose, t, CLASSTYPE_DECL_LIST (type));
3452 : }
3453 447305203 : }
3454 :
3455 : /* This function is called from declare_virt_assop_and_dtor via
3456 : dfs_walk_all.
3457 :
3458 : DATA is a type that direcly or indirectly inherits the base
3459 : represented by BINFO. If BINFO contains a virtual assignment [copy
3460 : assignment or move assigment] operator or a virtual constructor,
3461 : declare that function in DATA if it hasn't been already declared. */
3462 :
3463 : static tree
3464 3671675 : dfs_declare_virt_assop_and_dtor (tree binfo, void *data)
3465 : {
3466 3671675 : tree bv, fn, t = (tree)data;
3467 3671675 : tree opname = assign_op_identifier;
3468 :
3469 3671675 : gcc_assert (t && CLASS_TYPE_P (t));
3470 3671675 : gcc_assert (binfo && TREE_CODE (binfo) == TREE_BINFO);
3471 :
3472 3671675 : if (!TYPE_CONTAINS_VPTR_P (BINFO_TYPE (binfo)))
3473 : /* A base without a vtable needs no modification, and its bases
3474 : are uninteresting. */
3475 : return dfs_skip_bases;
3476 :
3477 3104762 : if (BINFO_PRIMARY_P (binfo))
3478 : /* If this is a primary base, then we have already looked at the
3479 : virtual functions of its vtable. */
3480 : return NULL_TREE;
3481 :
3482 12277672 : for (bv = BINFO_VIRTUALS (binfo); bv; bv = TREE_CHAIN (bv))
3483 : {
3484 9172910 : fn = BV_FN (bv);
3485 :
3486 9172910 : if (DECL_NAME (fn) == opname)
3487 : {
3488 21 : if (CLASSTYPE_LAZY_COPY_ASSIGN (t))
3489 21 : lazily_declare_fn (sfk_copy_assignment, t);
3490 21 : if (CLASSTYPE_LAZY_MOVE_ASSIGN (t))
3491 14 : lazily_declare_fn (sfk_move_assignment, t);
3492 : }
3493 9172889 : else if (DECL_DESTRUCTOR_P (fn)
3494 9172889 : && CLASSTYPE_LAZY_DESTRUCTOR (t))
3495 183229 : lazily_declare_fn (sfk_destructor, t);
3496 : }
3497 :
3498 : return NULL_TREE;
3499 : }
3500 :
3501 : /* If the class type T has a direct or indirect base that contains a
3502 : virtual assignment operator or a virtual destructor, declare that
3503 : function in T if it hasn't been already declared. */
3504 :
3505 : static void
3506 49904436 : declare_virt_assop_and_dtor (tree t)
3507 : {
3508 49904436 : if (!(TYPE_POLYMORPHIC_P (t)
3509 1738582 : && (CLASSTYPE_LAZY_COPY_ASSIGN (t)
3510 620884 : || CLASSTYPE_LAZY_MOVE_ASSIGN (t)
3511 620884 : || CLASSTYPE_LAZY_DESTRUCTOR (t))))
3512 : return;
3513 :
3514 1184439 : dfs_walk_all (TYPE_BINFO (t),
3515 : dfs_declare_virt_assop_and_dtor,
3516 : NULL, t);
3517 : }
3518 :
3519 : /* Declare the inheriting constructor for class T inherited from base
3520 : constructor CTOR with the parameter array PARMS of size NPARMS. */
3521 :
3522 : static void
3523 90 : one_inheriting_sig (tree t, tree ctor, tree *parms, int nparms)
3524 : {
3525 90 : gcc_assert (TYPE_MAIN_VARIANT (t) == t);
3526 :
3527 : /* We don't declare an inheriting ctor that would be a default,
3528 : copy or move ctor for derived or base. */
3529 90 : if (nparms == 0)
3530 : return;
3531 90 : if (nparms == 1
3532 87 : && TYPE_REF_P (parms[0]))
3533 : {
3534 54 : tree parm = TYPE_MAIN_VARIANT (TREE_TYPE (parms[0]));
3535 54 : if (parm == t || parm == DECL_CONTEXT (ctor))
3536 : return;
3537 : }
3538 :
3539 36 : tree parmlist = void_list_node;
3540 75 : for (int i = nparms - 1; i >= 0; i--)
3541 39 : parmlist = tree_cons (NULL_TREE, parms[i], parmlist);
3542 36 : tree fn = implicitly_declare_fn (sfk_inheriting_constructor,
3543 : t, false, ctor, parmlist);
3544 :
3545 36 : if (add_method (t, fn, false))
3546 : {
3547 27 : DECL_CHAIN (fn) = TYPE_FIELDS (t);
3548 27 : TYPE_FIELDS (t) = fn;
3549 : }
3550 : }
3551 :
3552 : /* Declare all the inheriting constructors for class T inherited from base
3553 : constructor CTOR. */
3554 :
3555 : static void
3556 437244 : one_inherited_ctor (tree ctor, tree t, tree using_decl)
3557 : {
3558 437244 : tree parms = FUNCTION_FIRST_USER_PARMTYPE (ctor);
3559 :
3560 437244 : if (flag_new_inheriting_ctors)
3561 : {
3562 437154 : ctor = implicitly_declare_fn (sfk_inheriting_constructor,
3563 : t, /*const*/false, ctor, parms);
3564 437154 : add_method (t, ctor, using_decl != NULL_TREE);
3565 437154 : return;
3566 : }
3567 :
3568 90 : tree *new_parms = XALLOCAVEC (tree, list_length (parms));
3569 90 : int i = 0;
3570 183 : for (; parms && parms != void_list_node; parms = TREE_CHAIN (parms))
3571 : {
3572 93 : if (TREE_PURPOSE (parms))
3573 0 : one_inheriting_sig (t, ctor, new_parms, i);
3574 93 : new_parms[i++] = TREE_VALUE (parms);
3575 : }
3576 90 : one_inheriting_sig (t, ctor, new_parms, i);
3577 90 : if (parms == NULL_TREE)
3578 : {
3579 3 : auto_diagnostic_group d;
3580 3 : if (warning (OPT_Winherited_variadic_ctor,
3581 : "the ellipsis in %qD is not inherited", ctor))
3582 3 : inform (DECL_SOURCE_LOCATION (ctor), "%qD declared here", ctor);
3583 3 : }
3584 : }
3585 :
3586 : /* Implicitly declare T(). */
3587 :
3588 : static void
3589 43104092 : add_implicit_default_ctor (tree t)
3590 : {
3591 43104092 : TYPE_HAS_DEFAULT_CONSTRUCTOR (t) = 1;
3592 43104092 : CLASSTYPE_LAZY_DEFAULT_CTOR (t) = 1;
3593 43104092 : if (cxx_dialect >= cxx11)
3594 43011819 : TYPE_HAS_CONSTEXPR_CTOR (t)
3595 : /* Don't force the declaration to get a hard answer; if the
3596 : definition would have made the class non-literal, it will still be
3597 : non-literal because of the base or member in question, and that
3598 : gives a better diagnostic. */
3599 86023638 : = type_maybe_constexpr_default_constructor (t);
3600 43104092 : }
3601 :
3602 : /* Create default constructors, assignment operators, and so forth for
3603 : the type indicated by T, if they are needed. CANT_HAVE_CONST_CTOR,
3604 : and CANT_HAVE_CONST_ASSIGNMENT are nonzero if, for whatever reason,
3605 : the class cannot have a default constructor, copy constructor
3606 : taking a const reference argument, or an assignment operator taking
3607 : a const reference, respectively. */
3608 :
3609 : static void
3610 49904436 : add_implicitly_declared_members (tree t, tree* access_decls,
3611 : int cant_have_const_cctor,
3612 : int cant_have_const_assignment)
3613 : {
3614 : /* Destructor. */
3615 49904436 : if (!CLASSTYPE_DESTRUCTOR (t))
3616 : /* In general, we create destructors lazily. */
3617 46257914 : CLASSTYPE_LAZY_DESTRUCTOR (t) = 1;
3618 :
3619 49904436 : bool move_ok = false;
3620 49780436 : if (cxx_dialect >= cxx11 && CLASSTYPE_LAZY_DESTRUCTOR (t)
3621 46157586 : && !TYPE_HAS_COPY_CTOR (t) && !TYPE_HAS_COPY_ASSIGN (t)
3622 94709211 : && !classtype_has_move_assign_or_move_ctor_p (t, false))
3623 : move_ok = true;
3624 :
3625 : /* [class.ctor]
3626 :
3627 : If there is no user-declared constructor for a class, a default
3628 : constructor is implicitly declared. */
3629 49904436 : if (! TYPE_HAS_USER_CONSTRUCTOR (t))
3630 43047259 : add_implicit_default_ctor (t);
3631 :
3632 : /* [class.ctor]
3633 :
3634 : If a class definition does not explicitly declare a copy
3635 : constructor, one is declared implicitly. */
3636 49904436 : if (! TYPE_HAS_COPY_CTOR (t))
3637 : {
3638 46474968 : TYPE_HAS_COPY_CTOR (t) = 1;
3639 46474968 : TYPE_HAS_CONST_COPY_CTOR (t) = !cant_have_const_cctor;
3640 46474968 : CLASSTYPE_LAZY_COPY_CTOR (t) = 1;
3641 46474968 : if (move_ok)
3642 44492821 : CLASSTYPE_LAZY_MOVE_CTOR (t) = 1;
3643 : }
3644 :
3645 : /* If there is no assignment operator, one will be created if and
3646 : when it is needed. For now, just record whether or not the type
3647 : of the parameter to the assignment operator will be a const or
3648 : non-const reference. */
3649 49904436 : if (!TYPE_HAS_COPY_ASSIGN (t))
3650 : {
3651 46603358 : TYPE_HAS_COPY_ASSIGN (t) = 1;
3652 46603358 : TYPE_HAS_CONST_COPY_ASSIGN (t) = !cant_have_const_assignment;
3653 46603358 : CLASSTYPE_LAZY_COPY_ASSIGN (t) = 1;
3654 90693286 : if (move_ok && !LAMBDA_TYPE_P (t))
3655 44149438 : CLASSTYPE_LAZY_MOVE_ASSIGN (t) = 1;
3656 : }
3657 :
3658 : /* We can't be lazy about declaring functions that might override
3659 : a virtual function from a base class. */
3660 49904436 : declare_virt_assop_and_dtor (t);
3661 :
3662 : /* If the class definition does not explicitly declare an == operator
3663 : function, but declares a defaulted three-way comparison operator function,
3664 : an == operator function is declared implicitly. */
3665 49904436 : if (!classtype_has_op (t, EQ_EXPR))
3666 48625919 : if (tree space = classtype_has_defaulted_op (t, SPACESHIP_EXPR))
3667 : {
3668 675 : tree eq = implicitly_declare_fn (sfk_comparison, t, false, space,
3669 : NULL_TREE);
3670 675 : bool is_friend = DECL_CONTEXT (space) != t;
3671 675 : if (is_friend)
3672 73 : do_friend (NULL_TREE, DECL_NAME (eq), eq,
3673 : NO_SPECIAL, true);
3674 : else
3675 : {
3676 602 : add_method (t, eq, false);
3677 602 : DECL_CHAIN (eq) = TYPE_FIELDS (t);
3678 602 : TYPE_FIELDS (t) = eq;
3679 : }
3680 675 : maybe_add_class_template_decl_list (t, eq, is_friend);
3681 : }
3682 :
3683 52229074 : while (*access_decls)
3684 : {
3685 2324638 : tree using_decl = TREE_VALUE (*access_decls);
3686 2324638 : tree decl = USING_DECL_DECLS (using_decl);
3687 2324638 : if (DECL_NAME (using_decl) == ctor_identifier)
3688 : {
3689 : /* declare, then remove the decl */
3690 101364 : tree ctor_list = decl;
3691 101364 : location_t loc = input_location;
3692 101364 : input_location = DECL_SOURCE_LOCATION (using_decl);
3693 875548 : for (tree fn : ovl_range (ctor_list))
3694 : {
3695 437244 : if (!TYPE_HAS_DEFAULT_CONSTRUCTOR (t) && default_ctor_p (fn))
3696 : /* CWG2799: Inheriting a default constructor gives us a default
3697 : constructor, not just an inherited constructor. */
3698 56833 : add_implicit_default_ctor (t);
3699 437244 : one_inherited_ctor (fn, t, using_decl);
3700 : }
3701 101364 : *access_decls = TREE_CHAIN (*access_decls);
3702 101364 : input_location = loc;
3703 : }
3704 : else
3705 2223274 : access_decls = &TREE_CHAIN (*access_decls);
3706 : }
3707 49904436 : }
3708 :
3709 : /* Cache of enum_min_precision values. */
3710 : static GTY((deletable)) hash_map<tree, int> *enum_to_min_precision;
3711 :
3712 : /* Return the minimum precision of a bit-field needed to store all
3713 : enumerators of ENUMERAL_TYPE TYPE. */
3714 :
3715 : static int
3716 101858 : enum_min_precision (tree type)
3717 : {
3718 101858 : type = TYPE_MAIN_VARIANT (type);
3719 : /* For unscoped enums without fixed underlying type and without mode
3720 : attribute we can just use precision of the underlying type. */
3721 101858 : if (UNSCOPED_ENUM_P (type)
3722 45897 : && !ENUM_FIXED_UNDERLYING_TYPE_P (type)
3723 110648 : && !lookup_attribute ("mode", TYPE_ATTRIBUTES (type)))
3724 8778 : return TYPE_PRECISION (ENUM_UNDERLYING_TYPE (type));
3725 :
3726 93080 : if (enum_to_min_precision == NULL)
3727 9364 : enum_to_min_precision = hash_map<tree, int>::create_ggc (37);
3728 :
3729 93080 : bool existed;
3730 93080 : int &prec = enum_to_min_precision->get_or_insert (type, &existed);
3731 93080 : if (existed)
3732 55861 : return prec;
3733 :
3734 37219 : tree minnode, maxnode;
3735 37219 : if (TYPE_VALUES (type))
3736 : {
3737 : minnode = maxnode = NULL_TREE;
3738 288072 : for (tree values = TYPE_VALUES (type);
3739 325288 : values; values = TREE_CHAIN (values))
3740 : {
3741 288072 : tree decl = TREE_VALUE (values);
3742 288072 : tree value = DECL_INITIAL (decl);
3743 288072 : if (value == error_mark_node)
3744 0 : value = integer_zero_node;
3745 288072 : if (!minnode)
3746 : minnode = maxnode = value;
3747 250856 : else if (tree_int_cst_lt (maxnode, value))
3748 : maxnode = value;
3749 92800 : else if (tree_int_cst_lt (value, minnode))
3750 0 : minnode = value;
3751 : }
3752 : }
3753 : else
3754 3 : minnode = maxnode = integer_zero_node;
3755 :
3756 37219 : signop sgn = tree_int_cst_sgn (minnode) >= 0 ? UNSIGNED : SIGNED;
3757 37219 : int lowprec = tree_int_cst_min_precision (minnode, sgn);
3758 37219 : int highprec = tree_int_cst_min_precision (maxnode, sgn);
3759 37219 : prec = MAX (lowprec, highprec);
3760 37219 : return prec;
3761 : }
3762 :
3763 : /* FIELD is a bit-field. We are finishing the processing for its
3764 : enclosing type. Issue any appropriate messages and set appropriate
3765 : flags. Returns false if an error has been diagnosed. */
3766 :
3767 : static bool
3768 643863 : check_bitfield_decl (tree field)
3769 : {
3770 643863 : tree type = TREE_TYPE (field);
3771 643863 : tree w;
3772 :
3773 : /* Extract the declared width of the bitfield, which has been
3774 : temporarily stashed in DECL_BIT_FIELD_REPRESENTATIVE by grokbitfield. */
3775 643863 : w = DECL_BIT_FIELD_REPRESENTATIVE (field);
3776 643863 : gcc_assert (w != NULL_TREE);
3777 : /* Remove the bit-field width indicator so that the rest of the
3778 : compiler does not treat that value as a qualifier. */
3779 643863 : DECL_BIT_FIELD_REPRESENTATIVE (field) = NULL_TREE;
3780 :
3781 : /* Detect invalid bit-field type. */
3782 643863 : if (!INTEGRAL_OR_ENUMERATION_TYPE_P (type))
3783 : {
3784 3 : error_at (DECL_SOURCE_LOCATION (field),
3785 : "bit-field %q#D with non-integral type %qT", field, type);
3786 3 : w = error_mark_node;
3787 : }
3788 : else
3789 : {
3790 643860 : location_t loc = input_location;
3791 : /* Avoid the non_lvalue wrapper added by fold for PLUS_EXPRs. */
3792 643860 : STRIP_NOPS (w);
3793 :
3794 : /* detect invalid field size. */
3795 643860 : input_location = DECL_SOURCE_LOCATION (field);
3796 643860 : w = cxx_constant_value (w);
3797 643860 : input_location = loc;
3798 :
3799 643860 : if (TREE_CODE (w) != INTEGER_CST)
3800 : {
3801 15 : error ("bit-field %q+D width not an integer constant", field);
3802 15 : w = error_mark_node;
3803 : }
3804 643845 : else if (tree_int_cst_sgn (w) < 0)
3805 : {
3806 0 : error ("negative width in bit-field %q+D", field);
3807 0 : w = error_mark_node;
3808 : }
3809 643845 : else if (integer_zerop (w) && DECL_NAME (field) != 0)
3810 : {
3811 15 : error ("zero width for bit-field %q+D", field);
3812 15 : w = error_mark_node;
3813 : }
3814 643830 : else if ((TREE_CODE (type) != ENUMERAL_TYPE
3815 643830 : && TREE_CODE (type) != BOOLEAN_TYPE
3816 540487 : && compare_tree_int (w, TYPE_PRECISION (type)) > 0)
3817 643830 : || ((TREE_CODE (type) == ENUMERAL_TYPE
3818 643396 : || TREE_CODE (type) == BOOLEAN_TYPE)
3819 103343 : && tree_int_cst_lt (TYPE_SIZE (type), w)))
3820 774 : warning_at (DECL_SOURCE_LOCATION (field), 0,
3821 : "width of %qD exceeds its type", field);
3822 643056 : else if (TREE_CODE (type) == ENUMERAL_TYPE)
3823 : {
3824 101858 : int prec = enum_min_precision (type);
3825 101858 : if (compare_tree_int (w, prec) < 0)
3826 48 : warning_at (DECL_SOURCE_LOCATION (field), 0,
3827 : "%qD is too small to hold all values of %q#T",
3828 : field, type);
3829 : }
3830 : }
3831 :
3832 643863 : if (w != error_mark_node)
3833 : {
3834 643830 : DECL_SIZE (field) = fold_convert (bitsizetype, w);
3835 643830 : DECL_BIT_FIELD (field) = 1;
3836 643830 : return true;
3837 : }
3838 : else
3839 : {
3840 : /* Non-bit-fields are aligned for their type. */
3841 33 : DECL_BIT_FIELD (field) = 0;
3842 33 : CLEAR_DECL_C_BIT_FIELD (field);
3843 33 : return false;
3844 : }
3845 : }
3846 :
3847 : /* FIELD is a non bit-field. We are finishing the processing for its
3848 : enclosing type T. Issue any appropriate messages and set appropriate
3849 : flags. */
3850 :
3851 : static bool
3852 17749722 : check_field_decl (tree field,
3853 : tree t,
3854 : int* cant_have_const_ctor,
3855 : int* no_const_asn_ref)
3856 : {
3857 17749722 : tree type = strip_array_types (TREE_TYPE (field));
3858 17749722 : bool any_default_members = false;
3859 :
3860 : /* In C++98 an anonymous union cannot contain any fields which would change
3861 : the settings of CANT_HAVE_CONST_CTOR and friends. */
3862 17749722 : if (ANON_UNION_TYPE_P (type) && cxx_dialect < cxx11)
3863 : ;
3864 : /* And, we don't set TYPE_HAS_CONST_COPY_CTOR, etc., for anonymous
3865 : structs. So, we recurse through their fields here. */
3866 17748679 : else if (ANON_AGGR_TYPE_P (type))
3867 : {
3868 977737 : for (tree fields = TYPE_FIELDS (type); fields;
3869 758861 : fields = DECL_CHAIN (fields))
3870 758861 : if (TREE_CODE (fields) == FIELD_DECL)
3871 502765 : any_default_members |= check_field_decl (fields, t,
3872 : cant_have_const_ctor,
3873 : no_const_asn_ref);
3874 : }
3875 : /* Check members with class type for constructors, destructors,
3876 : etc. */
3877 17529803 : else if (CLASS_TYPE_P (type))
3878 : {
3879 : /* Never let anything with uninheritable virtuals
3880 : make it through without complaint. */
3881 3366976 : abstract_virtuals_error (field, type);
3882 :
3883 3366976 : if (TREE_CODE (t) == UNION_TYPE && cxx_dialect < cxx11)
3884 : {
3885 3189 : static bool warned;
3886 3189 : auto_diagnostic_group d;
3887 3189 : int oldcount = errorcount;
3888 3189 : if (TYPE_NEEDS_CONSTRUCTING (type))
3889 2 : error ("member %q+#D with constructor not allowed in union",
3890 : field);
3891 3189 : if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
3892 0 : error ("member %q+#D with destructor not allowed in union", field);
3893 3189 : if (TYPE_HAS_COMPLEX_COPY_ASSIGN (type))
3894 3 : error ("member %q+#D with copy assignment operator not allowed in union",
3895 : field);
3896 3189 : if (!warned && errorcount > oldcount)
3897 : {
3898 4 : inform (DECL_SOURCE_LOCATION (field), "unrestricted unions "
3899 : "only available with %<-std=c++11%> or %<-std=gnu++11%>");
3900 4 : warned = true;
3901 : }
3902 3189 : }
3903 : else
3904 : {
3905 3363787 : TYPE_NEEDS_CONSTRUCTING (t) |= TYPE_NEEDS_CONSTRUCTING (type);
3906 3363787 : TYPE_HAS_COMPLEX_COPY_ASSIGN (t)
3907 3363787 : |= (TYPE_HAS_COMPLEX_COPY_ASSIGN (type)
3908 3363787 : || !TYPE_HAS_COPY_ASSIGN (type));
3909 6727574 : TYPE_HAS_COMPLEX_COPY_CTOR (t) |= (TYPE_HAS_COMPLEX_COPY_CTOR (type)
3910 3363787 : || !TYPE_HAS_COPY_CTOR (type));
3911 10091361 : TYPE_HAS_COMPLEX_MOVE_ASSIGN (t)
3912 3363787 : |= TYPE_HAS_COMPLEX_MOVE_ASSIGN (type);
3913 3363787 : TYPE_HAS_COMPLEX_MOVE_CTOR (t) |= TYPE_HAS_COMPLEX_MOVE_CTOR (type);
3914 : /* In C++26, triviality of default ctor or dtor of a variant member
3915 : doesn't matter for triviality of the t's default ctor or dtor.
3916 : Before C++26, non-trivial default ctor or dtor of a variant
3917 : member makes it deleted with the exception of default ctor
3918 : when DMI is present, but in that case default ctor is
3919 : non-trivial. */
3920 3363787 : if (TREE_CODE (DECL_CONTEXT (field)) != UNION_TYPE)
3921 : {
3922 9371844 : TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t)
3923 3123948 : |= TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type);
3924 3123948 : TYPE_HAS_COMPLEX_DFLT (t)
3925 6247896 : |= (!TYPE_HAS_DEFAULT_CONSTRUCTOR (type)
3926 3123948 : || TYPE_HAS_COMPLEX_DFLT (type));
3927 : }
3928 : }
3929 :
3930 3366976 : if (TYPE_HAS_COPY_CTOR (type)
3931 3366976 : && !TYPE_HAS_CONST_COPY_CTOR (type))
3932 9246 : *cant_have_const_ctor = 1;
3933 :
3934 3366976 : if (TYPE_HAS_COPY_ASSIGN (type)
3935 3366976 : && !TYPE_HAS_CONST_COPY_ASSIGN (type))
3936 55 : *no_const_asn_ref = 1;
3937 : }
3938 :
3939 17749722 : check_abi_tags (t, field);
3940 :
3941 17749722 : if (DECL_INITIAL (field) != NULL_TREE)
3942 : /* `build_class_init_list' does not recognize
3943 : non-FIELD_DECLs. */
3944 1405876 : any_default_members = true;
3945 :
3946 17749722 : return any_default_members;
3947 : }
3948 :
3949 : /* Check the data members (both static and non-static), class-scoped
3950 : typedefs, etc., appearing in the declaration of T. Issue
3951 : appropriate diagnostics. Sets ACCESS_DECLS to a list (in
3952 : declaration order) of access declarations; each TREE_VALUE in this
3953 : list is a USING_DECL.
3954 :
3955 : In addition, set the following flags:
3956 :
3957 : EMPTY_P
3958 : The class is empty, i.e., contains no non-static data members.
3959 :
3960 : CANT_HAVE_CONST_CTOR_P
3961 : This class cannot have an implicitly generated copy constructor
3962 : taking a const reference.
3963 :
3964 : CANT_HAVE_CONST_ASN_REF
3965 : This class cannot have an implicitly generated assignment
3966 : operator taking a const reference.
3967 :
3968 : All of these flags should be initialized before calling this
3969 : function. */
3970 :
3971 : static void
3972 49904436 : check_field_decls (tree t, tree *access_decls,
3973 : int *cant_have_const_ctor_p,
3974 : int *no_const_asn_ref_p)
3975 : {
3976 49904436 : int cant_pack = 0;
3977 :
3978 : /* Assume there are no access declarations. */
3979 49904436 : *access_decls = NULL_TREE;
3980 : /* Effective C has things to say about classes with pointer members. */
3981 49904436 : tree pointer_member = NULL_TREE;
3982 : /* Default initialized members affect the whole class. */
3983 49904436 : tree default_init_member = NULL_TREE;
3984 : /* Lack of any non-static data member of non-volatile literal
3985 : type affects a union. */
3986 49904436 : bool found_nv_literal_p = false;
3987 : /* Standard layout requires all FIELDS have same access. */
3988 49904436 : int field_access = -1;
3989 :
3990 323568063 : for (tree field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
3991 : {
3992 273663627 : tree type = TREE_TYPE (field);
3993 :
3994 273663627 : switch (TREE_CODE (field))
3995 : {
3996 0 : default:
3997 0 : gcc_unreachable ();
3998 :
3999 2324638 : case USING_DECL:
4000 : /* Save the access declarations for our caller. */
4001 2324638 : *access_decls = tree_cons (NULL_TREE, field, *access_decls);
4002 2324638 : break;
4003 :
4004 : case TYPE_DECL:
4005 : case TEMPLATE_DECL:
4006 : break;
4007 :
4008 : case FUNCTION_DECL:
4009 : /* FIXME: We should fold in the checking from check_methods. */
4010 : break;
4011 :
4012 1694401 : case CONST_DECL:
4013 1694401 : DECL_NONLOCAL (field) = 1;
4014 1694401 : break;
4015 :
4016 13752676 : case VAR_DECL:
4017 13752676 : if (TREE_CODE (t) == UNION_TYPE
4018 46 : && cxx_dialect < cxx11)
4019 : {
4020 : /* [class.union]
4021 :
4022 : (C++98) If a union contains a static data member,
4023 : ... the program is ill-formed. */
4024 1 : if (cxx_dialect < cxx11)
4025 1 : error ("in C++98 %q+D may not be static because it is "
4026 : "a member of a union", field);
4027 : }
4028 13752676 : goto data_member;
4029 :
4030 17247122 : case FIELD_DECL:
4031 17247122 : if (TREE_CODE (t) == UNION_TYPE)
4032 : {
4033 : /* [class.union]
4034 :
4035 : If a union contains ... or a [non-static data] member
4036 : of reference type, the program is ill-formed. */
4037 1053255 : if (TYPE_REF_P (type))
4038 52 : error ("non-static data member %q+D in a union may not "
4039 : "have reference type %qT", field, type);
4040 : }
4041 :
4042 16193867 : data_member:
4043 : /* Common VAR_DECL & FIELD_DECL processing. */
4044 30999798 : DECL_CONTEXT (field) = t;
4045 30999798 : DECL_NONLOCAL (field) = 1;
4046 :
4047 : /* Template instantiation can cause this. Perhaps this
4048 : should be a specific instantiation check? */
4049 30999798 : if (TREE_CODE (type) == FUNCTION_TYPE)
4050 : {
4051 11 : error ("data member %q+D invalidly declared function type", field);
4052 11 : type = build_pointer_type (type);
4053 11 : TREE_TYPE (field) = type;
4054 : }
4055 30999787 : else if (TREE_CODE (type) == METHOD_TYPE)
4056 : {
4057 0 : error ("data member %q+D invalidly declared method type", field);
4058 0 : type = build_pointer_type (type);
4059 0 : TREE_TYPE (field) = type;
4060 : }
4061 :
4062 : break;
4063 : }
4064 :
4065 273663627 : if (TREE_CODE (field) != FIELD_DECL)
4066 256416505 : continue;
4067 :
4068 17247122 : if (type == error_mark_node)
4069 165 : continue;
4070 :
4071 : /* If it is not a union and at least one non-static data member is
4072 : non-literal, the whole class becomes non-literal. Per Core/1453,
4073 : volatile non-static data members and base classes are also not allowed.
4074 : If it is a union, we might set CLASSTYPE_LITERAL_P after we've seen all
4075 : members.
4076 : Note: if the type is incomplete we will complain later on. */
4077 17246957 : if (COMPLETE_TYPE_P (type))
4078 : {
4079 17245567 : if (!literal_type_p (type) || CP_TYPE_VOLATILE_P (type))
4080 966904 : CLASSTYPE_LITERAL_P (t) = false;
4081 : else
4082 : found_nv_literal_p = true;
4083 : }
4084 :
4085 17246957 : int this_field_access = (TREE_PROTECTED (field) ? 1
4086 15522526 : : TREE_PRIVATE (field) ? 2 : 0);
4087 17246957 : if (field_access != this_field_access)
4088 : {
4089 : /* A standard-layout class is a class that:
4090 :
4091 : ... has the same access control (Clause 11) for all
4092 : non-static data members, */
4093 6896791 : if (field_access < 0)
4094 : field_access = this_field_access;
4095 : else
4096 39318 : CLASSTYPE_NON_STD_LAYOUT (t) = 1;
4097 :
4098 : /* Aggregates must be public. */
4099 6896791 : if (this_field_access)
4100 3334085 : CLASSTYPE_NON_AGGREGATE (t) = 1;
4101 : }
4102 :
4103 : /* If this is of reference type, check if it needs an init. */
4104 17246957 : if (TYPE_REF_P (type))
4105 : {
4106 348259 : CLASSTYPE_NON_LAYOUT_POD_P (t) = 1;
4107 348259 : CLASSTYPE_NON_STD_LAYOUT (t) = 1;
4108 348259 : if (DECL_INITIAL (field) == NULL_TREE)
4109 348130 : SET_CLASSTYPE_REF_FIELDS_NEED_INIT (t, 1);
4110 348259 : if (cxx_dialect < cxx11)
4111 : {
4112 : /* ARM $12.6.2: [A member initializer list] (or, for an
4113 : aggregate, initialization by a brace-enclosed list) is the
4114 : only way to initialize non-static const and reference
4115 : members. */
4116 1221 : TYPE_HAS_COMPLEX_COPY_ASSIGN (t) = 1;
4117 1221 : TYPE_HAS_COMPLEX_MOVE_ASSIGN (t) = 1;
4118 : }
4119 : }
4120 :
4121 17246957 : type = strip_array_types (type);
4122 :
4123 17246957 : if (TYPE_PACKED (t))
4124 : {
4125 1085 : if (!layout_pod_type_p (type) && !TYPE_PACKED (type))
4126 : {
4127 18 : warning_at (DECL_SOURCE_LOCATION (field), 0,
4128 : "ignoring packed attribute because of"
4129 : " unpacked non-POD field %q#D", field);
4130 18 : cant_pack = 1;
4131 : }
4132 1067 : else if (DECL_C_BIT_FIELD (field)
4133 1067 : || TYPE_ALIGN (TREE_TYPE (field)) > BITS_PER_UNIT)
4134 821 : DECL_PACKED (field) = 1;
4135 : }
4136 :
4137 17246957 : if (DECL_C_BIT_FIELD (field)
4138 17246957 : && integer_zerop (DECL_BIT_FIELD_REPRESENTATIVE (field)))
4139 : /* We don't treat zero-width bitfields as making a class
4140 : non-empty. */
4141 : ;
4142 17245788 : else if (field_poverlapping_p (field)
4143 17245788 : && is_empty_class (TREE_TYPE (field)))
4144 : /* Empty data members also don't make a class non-empty. */
4145 225393 : CLASSTYPE_CONTAINS_EMPTY_CLASS_P (t) = 1;
4146 : else
4147 : {
4148 : /* The class is non-empty. */
4149 17020395 : CLASSTYPE_EMPTY_P (t) = 0;
4150 : /* The class is not even nearly empty. */
4151 17020395 : CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
4152 : /* If one of the data members contains an empty class, so
4153 : does T. */
4154 3298115 : if (CLASS_TYPE_P (type)
4155 20307834 : && CLASSTYPE_CONTAINS_EMPTY_CLASS_P (type))
4156 920158 : CLASSTYPE_CONTAINS_EMPTY_CLASS_P (t) = 1;
4157 : }
4158 :
4159 : /* This is used by -Weffc++ (see below). Warn only for pointers
4160 : to members which might hold dynamic memory. So do not warn
4161 : for pointers to functions or pointers to members. */
4162 17246957 : if (TYPE_PTR_P (type)
4163 17246957 : && !TYPE_PTRFN_P (type))
4164 : pointer_member = field;
4165 :
4166 17246957 : if (CLASS_TYPE_P (type))
4167 : {
4168 3512832 : if (CLASSTYPE_REF_FIELDS_NEED_INIT (type))
4169 3004 : SET_CLASSTYPE_REF_FIELDS_NEED_INIT (t, 1);
4170 3512832 : if (CLASSTYPE_READONLY_FIELDS_NEED_INIT (type))
4171 28314 : SET_CLASSTYPE_READONLY_FIELDS_NEED_INIT (t, 1);
4172 : }
4173 :
4174 17246957 : if (DECL_MUTABLE_P (field) || TYPE_HAS_MUTABLE_P (type))
4175 208785 : CLASSTYPE_HAS_MUTABLE (t) = 1;
4176 :
4177 17246957 : if (DECL_MUTABLE_P (field))
4178 : {
4179 130205 : if (TYPE_REF_P (type))
4180 3 : error ("member %q+D cannot be declared as a %<mutable%> "
4181 : "reference", field);
4182 130202 : else if (CP_TYPE_CONST_P (type))
4183 3 : error ("member %q+D cannot be declared both %<const%> "
4184 : "and %<mutable%>", field);
4185 : }
4186 :
4187 17246957 : if (! layout_pod_type_p (type))
4188 : /* DR 148 now allows pointers to members (which are POD themselves),
4189 : to be allowed in POD structs. */
4190 2867624 : CLASSTYPE_NON_LAYOUT_POD_P (t) = 1;
4191 :
4192 17246957 : if (field_poverlapping_p (field))
4193 : /* A potentially-overlapping non-static data member makes the class
4194 : non-layout-POD. */
4195 279488 : CLASSTYPE_NON_LAYOUT_POD_P (t) = 1;
4196 :
4197 17246957 : if (!std_layout_type_p (type))
4198 572533 : CLASSTYPE_NON_STD_LAYOUT (t) = 1;
4199 :
4200 17246957 : if (! zero_init_p (type))
4201 161 : CLASSTYPE_NON_ZERO_INIT_P (t) = 1;
4202 :
4203 : /* We set DECL_C_BIT_FIELD in grokbitfield.
4204 : If the type and width are valid, we'll also set DECL_BIT_FIELD. */
4205 17246957 : if (DECL_C_BIT_FIELD (field))
4206 643863 : check_bitfield_decl (field);
4207 :
4208 17246957 : if (check_field_decl (field, t,
4209 : cant_have_const_ctor_p, no_const_asn_ref_p))
4210 : {
4211 1405855 : if (default_init_member
4212 564355 : && TREE_CODE (t) == UNION_TYPE)
4213 : {
4214 12 : auto_diagnostic_group d;
4215 12 : error ("multiple fields in union %qT initialized", t);
4216 12 : inform (DECL_SOURCE_LOCATION (default_init_member),
4217 : "initialized member %q+D declared here",
4218 : default_init_member);
4219 12 : }
4220 : default_init_member = field;
4221 : }
4222 :
4223 : /* Now that we've removed bit-field widths from DECL_INITIAL,
4224 : anything left in DECL_INITIAL is an NSDMI that makes the class
4225 : non-aggregate in C++11, and non-layout-POD always. */
4226 17246957 : if (DECL_INITIAL (field))
4227 : {
4228 1387446 : if (cxx_dialect < cxx14)
4229 1011 : CLASSTYPE_NON_AGGREGATE (t) = true;
4230 : else
4231 1386435 : CLASSTYPE_NON_POD_AGGREGATE (t) = true;
4232 : }
4233 :
4234 17246957 : if (CP_TYPE_CONST_P (type))
4235 : {
4236 : /* If any field is const, the structure type is pseudo-const. */
4237 273044 : C_TYPE_FIELDS_READONLY (t) = 1;
4238 273044 : if (DECL_INITIAL (field) == NULL_TREE)
4239 227152 : SET_CLASSTYPE_READONLY_FIELDS_NEED_INIT (t, 1);
4240 273044 : if (cxx_dialect < cxx11)
4241 : {
4242 : /* ARM $12.6.2: [A member initializer list] (or, for an
4243 : aggregate, initialization by a brace-enclosed list) is the
4244 : only way to initialize non-static const and reference
4245 : members. */
4246 835 : TYPE_HAS_COMPLEX_COPY_ASSIGN (t) = 1;
4247 835 : TYPE_HAS_COMPLEX_MOVE_ASSIGN (t) = 1;
4248 : }
4249 : }
4250 : /* A field that is pseudo-const makes the structure likewise. */
4251 16973913 : else if (CLASS_TYPE_P (type))
4252 : {
4253 3466216 : C_TYPE_FIELDS_READONLY (t) |= C_TYPE_FIELDS_READONLY (type);
4254 3466216 : SET_CLASSTYPE_READONLY_FIELDS_NEED_INIT (t,
4255 : CLASSTYPE_READONLY_FIELDS_NEED_INIT (t)
4256 : | CLASSTYPE_READONLY_FIELDS_NEED_INIT (type));
4257 : }
4258 :
4259 : /* Core issue 80: A non-static data member is required to have a
4260 : different name from the class iff the class has a
4261 : user-declared constructor. */
4262 17246957 : if (constructor_name_p (DECL_NAME (field), t)
4263 17246957 : && TYPE_HAS_USER_CONSTRUCTOR (t))
4264 12 : permerror (DECL_SOURCE_LOCATION (field),
4265 : "field %q#D with same name as class", field);
4266 : }
4267 :
4268 : /* Per CWG 2096, a type is a literal type if it is a union, and at least
4269 : one of its non-static data members is of non-volatile literal type. */
4270 49904436 : if (TREE_CODE (t) == UNION_TYPE && found_nv_literal_p)
4271 421843 : CLASSTYPE_LITERAL_P (t) = true;
4272 :
4273 : /* Effective C++ rule 11: if a class has dynamic memory held by pointers,
4274 : it should also define a copy constructor and an assignment operator to
4275 : implement the correct copy semantic (deep vs shallow, etc.). As it is
4276 : not feasible to check whether the constructors do allocate dynamic memory
4277 : and store it within members, we approximate the warning like this:
4278 :
4279 : -- Warn only if there are members which are pointers
4280 : -- Warn only if there is a non-trivial constructor (otherwise,
4281 : there cannot be memory allocated).
4282 : -- Warn only if there is a non-trivial destructor. We assume that the
4283 : user at least implemented the cleanup correctly, and a destructor
4284 : is needed to free dynamic memory.
4285 :
4286 : This seems enough for practical purposes. */
4287 49904436 : if (warn_ecpp
4288 138 : && pointer_member
4289 15 : && TYPE_HAS_USER_CONSTRUCTOR (t)
4290 15 : && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t)
4291 49904448 : && !(TYPE_HAS_COPY_CTOR (t) && TYPE_HAS_COPY_ASSIGN (t)))
4292 : {
4293 9 : auto_diagnostic_group d;
4294 9 : if (warning (OPT_Weffc__, "%q#T has pointer data members", t))
4295 : {
4296 9 : if (! TYPE_HAS_COPY_CTOR (t))
4297 : {
4298 6 : warning (OPT_Weffc__,
4299 : " but does not declare %<%T(const %T&)%>", t, t);
4300 6 : if (!TYPE_HAS_COPY_ASSIGN (t))
4301 3 : warning (OPT_Weffc__, " or %<operator=(const %T&)%>", t);
4302 : }
4303 3 : else if (! TYPE_HAS_COPY_ASSIGN (t))
4304 3 : warning (OPT_Weffc__,
4305 : " but does not declare %<operator=(const %T&)%>", t);
4306 9 : inform (DECL_SOURCE_LOCATION (pointer_member),
4307 : "pointer member %q+D declared here", pointer_member);
4308 : }
4309 9 : }
4310 :
4311 : /* Non-static data member initializers make the default constructor
4312 : non-trivial. */
4313 49904436 : if (default_init_member)
4314 : {
4315 841500 : TYPE_NEEDS_CONSTRUCTING (t) = true;
4316 841500 : TYPE_HAS_COMPLEX_DFLT (t) = true;
4317 : }
4318 :
4319 : /* If any of the fields couldn't be packed, unset TYPE_PACKED. */
4320 49904436 : if (cant_pack)
4321 12 : TYPE_PACKED (t) = 0;
4322 :
4323 : /* Check anonymous struct/anonymous union fields. */
4324 49904436 : finish_struct_anon (t);
4325 :
4326 : /* We've built up the list of access declarations in reverse order.
4327 : Fix that now. */
4328 49904436 : *access_decls = nreverse (*access_decls);
4329 49904436 : }
4330 :
4331 : /* If TYPE is an empty class type, records its OFFSET in the table of
4332 : OFFSETS. */
4333 :
4334 : static int
4335 29551499 : record_subobject_offset (tree type, tree offset, splay_tree offsets)
4336 : {
4337 29551499 : splay_tree_node n;
4338 :
4339 29551499 : if (!is_empty_class (type))
4340 : return 0;
4341 :
4342 : /* Record the location of this empty object in OFFSETS. */
4343 26782628 : n = splay_tree_lookup (offsets, (splay_tree_key) offset);
4344 26782628 : if (!n)
4345 22581623 : n = splay_tree_insert (offsets,
4346 : (splay_tree_key) offset,
4347 : (splay_tree_value) NULL_TREE);
4348 53565256 : n->value = ((splay_tree_value)
4349 26782628 : tree_cons (NULL_TREE,
4350 : type,
4351 26782628 : (tree) n->value));
4352 :
4353 26782628 : return 0;
4354 : }
4355 :
4356 : /* Returns nonzero if TYPE is an empty class type and there is
4357 : already an entry in OFFSETS for the same TYPE as the same OFFSET. */
4358 :
4359 : static int
4360 635531 : check_subobject_offset (tree type, tree offset, splay_tree offsets)
4361 : {
4362 635531 : splay_tree_node n;
4363 635531 : tree t;
4364 :
4365 635531 : if (!is_empty_class (type))
4366 : return 0;
4367 :
4368 : /* Record the location of this empty object in OFFSETS. */
4369 543602 : n = splay_tree_lookup (offsets, (splay_tree_key) offset);
4370 543602 : if (!n)
4371 : return 0;
4372 :
4373 543504 : enum { ignore, fast, slow, warn }
4374 543504 : cv_check = (abi_version_crosses (19) ? slow
4375 543471 : : abi_version_at_least (19) ? fast
4376 0 : : ignore);
4377 3919098 : for (t = (tree) n->value; t; t = TREE_CHAIN (t))
4378 : {
4379 3378796 : tree elt = TREE_VALUE (t);
4380 :
4381 3378796 : if (same_type_p (elt, type))
4382 : return 1;
4383 :
4384 3375594 : if (cv_check != ignore
4385 3375594 : && similar_type_p (elt, type))
4386 : {
4387 12 : if (cv_check == fast)
4388 : return 1;
4389 : cv_check = warn;
4390 : }
4391 : }
4392 :
4393 540302 : if (cv_check == warn)
4394 : {
4395 12 : warning (OPT_Wabi, "layout of %qs member of type %qT changes in %qs",
4396 : "[[no_unique_address]]", type, "-fabi-version=19");
4397 12 : if (abi_version_at_least (19))
4398 : return 1;
4399 : }
4400 :
4401 : return 0;
4402 : }
4403 :
4404 : /* Walk through all the subobjects of TYPE (located at OFFSET). Call
4405 : F for every subobject, passing it the type, offset, and table of
4406 : OFFSETS. If VBASES_P is one, then virtual non-primary bases should
4407 : be traversed.
4408 :
4409 : If MAX_OFFSET is non-NULL, then subobjects with an offset greater
4410 : than MAX_OFFSET will not be walked.
4411 :
4412 : If F returns a nonzero value, the traversal ceases, and that value
4413 : is returned. Otherwise, returns zero. */
4414 :
4415 : static int
4416 54010611 : walk_subobject_offsets (tree type,
4417 : subobject_offset_fn f,
4418 : tree offset,
4419 : splay_tree offsets,
4420 : tree max_offset,
4421 : int vbases_p)
4422 : {
4423 54010611 : int r = 0;
4424 54010611 : tree type_binfo = NULL_TREE;
4425 :
4426 : /* If this OFFSET is bigger than the MAX_OFFSET, then we should
4427 : stop. */
4428 54010611 : if (max_offset && tree_int_cst_lt (max_offset, offset))
4429 : return 0;
4430 :
4431 45534296 : if (type == error_mark_node)
4432 : return 0;
4433 :
4434 45534296 : if (!TYPE_P (type))
4435 : {
4436 5905383 : type_binfo = type;
4437 5905383 : type = BINFO_TYPE (type);
4438 : }
4439 :
4440 45534296 : if (CLASS_TYPE_P (type))
4441 : {
4442 34935762 : tree field;
4443 34935762 : tree binfo;
4444 34935762 : int i;
4445 :
4446 : /* Avoid recursing into objects that are not interesting. */
4447 34935762 : if (!CLASSTYPE_CONTAINS_EMPTY_CLASS_P (type))
4448 : return 0;
4449 :
4450 : /* Record the location of TYPE. */
4451 30187465 : r = (*f) (type, offset, offsets);
4452 30187465 : if (r)
4453 : return r;
4454 :
4455 : /* Iterate through the direct base classes of TYPE. */
4456 30184251 : if (!type_binfo)
4457 25286208 : type_binfo = TYPE_BINFO (type);
4458 35932158 : for (i = 0; BINFO_BASE_ITERATE (type_binfo, i, binfo); i++)
4459 : {
4460 5749060 : tree binfo_offset;
4461 :
4462 5749060 : if (BINFO_VIRTUAL_P (binfo))
4463 1680 : continue;
4464 :
4465 5747380 : tree orig_binfo;
4466 : /* We cannot rely on BINFO_OFFSET being set for the base
4467 : class yet, but the offsets for direct non-virtual
4468 : bases can be calculated by going back to the TYPE. */
4469 5747380 : orig_binfo = BINFO_BASE_BINFO (TYPE_BINFO (type), i);
4470 5747380 : binfo_offset = size_binop (PLUS_EXPR,
4471 : offset,
4472 : BINFO_OFFSET (orig_binfo));
4473 :
4474 5747380 : r = walk_subobject_offsets (binfo,
4475 : f,
4476 : binfo_offset,
4477 : offsets,
4478 : max_offset,
4479 : /*vbases_p=*/0);
4480 5747380 : if (r)
4481 : return r;
4482 : }
4483 :
4484 30183098 : if (CLASSTYPE_VBASECLASSES (type))
4485 : {
4486 1369 : unsigned ix;
4487 1369 : vec<tree, va_gc> *vbases;
4488 :
4489 : /* Iterate through the virtual base classes of TYPE. In G++
4490 : 3.2, we included virtual bases in the direct base class
4491 : loop above, which results in incorrect results; the
4492 : correct offsets for virtual bases are only known when
4493 : working with the most derived type. */
4494 1369 : if (vbases_p)
4495 30 : for (vbases = CLASSTYPE_VBASECLASSES (type), ix = 0;
4496 30 : vec_safe_iterate (vbases, ix, &binfo); ix++)
4497 : {
4498 15 : r = walk_subobject_offsets (binfo,
4499 : f,
4500 15 : size_binop (PLUS_EXPR,
4501 : offset,
4502 : BINFO_OFFSET (binfo)),
4503 : offsets,
4504 : max_offset,
4505 : /*vbases_p=*/0);
4506 15 : if (r)
4507 : return r;
4508 : }
4509 : else
4510 : {
4511 : /* We still have to walk the primary base, if it is
4512 : virtual. (If it is non-virtual, then it was walked
4513 : above.) */
4514 1354 : tree vbase = get_primary_binfo (type_binfo);
4515 :
4516 423 : if (vbase && BINFO_VIRTUAL_P (vbase)
4517 262 : && BINFO_PRIMARY_P (vbase)
4518 1616 : && BINFO_INHERITANCE_CHAIN (vbase) == type_binfo)
4519 : {
4520 228 : r = (walk_subobject_offsets
4521 228 : (vbase, f, offset,
4522 : offsets, max_offset, /*vbases_p=*/0));
4523 228 : if (r)
4524 : return r;
4525 : }
4526 : }
4527 : }
4528 :
4529 : /* Iterate through the fields of TYPE. */
4530 578967219 : for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
4531 548786537 : if (TREE_CODE (field) == FIELD_DECL
4532 10596592 : && TREE_TYPE (field) != error_mark_node
4533 559383085 : && !DECL_ARTIFICIAL (field))
4534 : {
4535 4824740 : tree field_offset;
4536 :
4537 4824740 : field_offset = byte_position (field);
4538 :
4539 4824740 : r = walk_subobject_offsets (TREE_TYPE (field),
4540 : f,
4541 : size_binop (PLUS_EXPR,
4542 : offset,
4543 : field_offset),
4544 : offsets,
4545 : max_offset,
4546 : /*vbases_p=*/1);
4547 4824740 : if (r)
4548 : return r;
4549 : }
4550 : }
4551 10598534 : else if (TREE_CODE (type) == ARRAY_TYPE)
4552 : {
4553 676095 : tree element_type = strip_array_types (type);
4554 676095 : tree domain = TYPE_DOMAIN (type);
4555 676095 : tree index;
4556 :
4557 : /* Avoid recursing into objects that are not interesting. */
4558 26541 : if (!CLASS_TYPE_P (element_type)
4559 26529 : || !CLASSTYPE_CONTAINS_EMPTY_CLASS_P (element_type)
4560 12010 : || !domain
4561 688086 : || integer_minus_onep (TYPE_MAX_VALUE (domain)))
4562 664321 : return 0;
4563 :
4564 : /* Step through each of the elements in the array. */
4565 11774 : for (index = size_zero_node;
4566 34328 : !tree_int_cst_lt (TYPE_MAX_VALUE (domain), index);
4567 22554 : index = size_binop (PLUS_EXPR, index, size_one_node))
4568 : {
4569 33360 : r = walk_subobject_offsets (TREE_TYPE (type),
4570 : f,
4571 : offset,
4572 : offsets,
4573 : max_offset,
4574 : /*vbases_p=*/1);
4575 33360 : if (r)
4576 : return r;
4577 33360 : offset = size_binop (PLUS_EXPR, offset,
4578 : TYPE_SIZE_UNIT (TREE_TYPE (type)));
4579 : /* If this new OFFSET is bigger than the MAX_OFFSET, then
4580 : there's no point in iterating through the remaining
4581 : elements of the array. */
4582 33360 : if (max_offset && tree_int_cst_lt (max_offset, offset))
4583 : break;
4584 : }
4585 : }
4586 :
4587 : return 0;
4588 : }
4589 :
4590 : /* Return true iff FIELD_DECL DECL is potentially overlapping. */
4591 :
4592 : static bool
4593 85466220 : field_poverlapping_p (tree decl)
4594 : {
4595 : /* Base fields are actually potentially overlapping, but C++ bases go through
4596 : a different code path based on binfos, and ObjC++ base fields are laid out
4597 : in objc-act, so we don't want layout_class_type to mess with them. */
4598 85466220 : if (DECL_FIELD_IS_BASE (decl))
4599 : {
4600 0 : gcc_checking_assert (c_dialect_objc ());
4601 : return false;
4602 : }
4603 :
4604 85466220 : return lookup_attribute ("no_unique_address",
4605 170932440 : DECL_ATTRIBUTES (decl));
4606 : }
4607 :
4608 : /* Return true iff DECL is an empty field, either for an empty base or a
4609 : [[no_unique_address]] data member. */
4610 :
4611 : bool
4612 489595062 : is_empty_field (tree decl)
4613 : {
4614 489595062 : if (!decl || TREE_CODE (decl) != FIELD_DECL)
4615 : return false;
4616 :
4617 99154714 : bool r = (is_empty_class (TREE_TYPE (decl))
4618 102447969 : && (DECL_FIELD_IS_BASE (decl)
4619 781726 : || field_poverlapping_p (decl)));
4620 :
4621 : /* Empty fields should have size zero. */
4622 3132692 : gcc_checking_assert (!r || integer_zerop (DECL_SIZE (decl)));
4623 :
4624 : return r;
4625 : }
4626 :
4627 : /* Record all of the empty subobjects of DECL_OR_BINFO. */
4628 :
4629 : static void
4630 41068664 : record_subobject_offsets (tree decl_or_binfo,
4631 : splay_tree offsets)
4632 : {
4633 41068664 : tree type, offset;
4634 41068664 : bool overlapping, vbases_p;
4635 :
4636 41068664 : if (DECL_P (decl_or_binfo))
4637 : {
4638 17246957 : tree decl = decl_or_binfo;
4639 17246957 : type = TREE_TYPE (decl);
4640 17246957 : offset = byte_position (decl);
4641 17246957 : overlapping = field_poverlapping_p (decl);
4642 17246957 : vbases_p = true;
4643 : }
4644 : else
4645 : {
4646 23821707 : type = BINFO_TYPE (decl_or_binfo);
4647 23821707 : offset = BINFO_OFFSET (decl_or_binfo);
4648 23821707 : overlapping = true;
4649 23821707 : vbases_p = false;
4650 : }
4651 :
4652 41068664 : tree max_offset;
4653 : /* If recording subobjects for a non-static data member or a
4654 : non-empty base class, we do not need to record offsets beyond
4655 : the size of the biggest empty class. Additional data members
4656 : will go at the end of the class. Additional base classes will go
4657 : either at offset zero (if empty, in which case they cannot
4658 : overlap with offsets past the size of the biggest empty class) or
4659 : at the end of the class.
4660 :
4661 : However, if we are placing an empty base class, then we must record
4662 : all offsets, as either the empty class is at offset zero (where
4663 : other empty classes might later be placed) or at the end of the
4664 : class (where other objects might then be placed, so other empty
4665 : subobjects might later overlap). */
4666 23821707 : if (!overlapping
4667 41068664 : || !is_empty_class (type))
4668 19594790 : max_offset = sizeof_biggest_empty_class;
4669 : else
4670 : max_offset = NULL_TREE;
4671 41068664 : walk_subobject_offsets (type, record_subobject_offset, offset,
4672 : offsets, max_offset, vbases_p);
4673 41068664 : }
4674 :
4675 : /* Returns nonzero if any of the empty subobjects of TYPE (located at
4676 : OFFSET) conflict with entries in OFFSETS. If VBASES_P is nonzero,
4677 : virtual bases of TYPE are examined. */
4678 :
4679 : static int
4680 40019333 : layout_conflict_p (tree type,
4681 : tree offset,
4682 : splay_tree offsets,
4683 : int vbases_p)
4684 : {
4685 40019333 : splay_tree_node max_node;
4686 :
4687 : /* Get the node in OFFSETS that indicates the maximum offset where
4688 : an empty subobject is located. */
4689 40019333 : max_node = splay_tree_max (offsets);
4690 : /* If there aren't any empty subobjects, then there's no point in
4691 : performing this check. */
4692 40019333 : if (!max_node)
4693 : return 0;
4694 :
4695 2335892 : return walk_subobject_offsets (type, check_subobject_offset, offset,
4696 2335892 : offsets, (tree) (max_node->key),
4697 2335892 : vbases_p);
4698 : }
4699 :
4700 : /* DECL is a FIELD_DECL corresponding either to a base subobject of a
4701 : non-static data member of the type indicated by RLI. BINFO is the
4702 : binfo corresponding to the base subobject, OFFSETS maps offsets to
4703 : types already located at those offsets. This function determines
4704 : the position of the DECL. */
4705 :
4706 : static void
4707 19595558 : layout_nonempty_base_or_field (record_layout_info rli,
4708 : tree decl,
4709 : tree binfo,
4710 : splay_tree offsets)
4711 : {
4712 19595558 : tree offset = NULL_TREE;
4713 19595558 : bool field_p;
4714 19595558 : tree type;
4715 :
4716 19595558 : if (binfo)
4717 : {
4718 : /* For the purposes of determining layout conflicts, we want to
4719 : use the class type of BINFO; TREE_TYPE (DECL) will be the
4720 : CLASSTYPE_AS_BASE version, which does not contain entries for
4721 : zero-sized bases. */
4722 2573226 : type = TREE_TYPE (binfo);
4723 2573226 : field_p = false;
4724 : }
4725 : else
4726 : {
4727 17022332 : type = TREE_TYPE (decl);
4728 17022332 : field_p = true;
4729 : }
4730 :
4731 : /* Try to place the field. It may take more than one try if we have
4732 : a hard time placing the field without putting two objects of the
4733 : same type at the same address. */
4734 233 : while (1)
4735 : {
4736 19595791 : struct record_layout_info_s old_rli = *rli;
4737 :
4738 : /* Place this field. */
4739 19595791 : place_field (rli, decl);
4740 19595791 : offset = byte_position (decl);
4741 :
4742 : /* We have to check to see whether or not there is already
4743 : something of the same type at the offset we're about to use.
4744 : For example, consider:
4745 :
4746 : struct S {};
4747 : struct T : public S { int i; };
4748 : struct U : public S, public T {};
4749 :
4750 : Here, we put S at offset zero in U. Then, we can't put T at
4751 : offset zero -- its S component would be at the same address
4752 : as the S we already allocated. So, we have to skip ahead.
4753 : Since all data members, including those whose type is an
4754 : empty class, have nonzero size, any overlap can happen only
4755 : with a direct or indirect base-class -- it can't happen with
4756 : a data member. */
4757 : /* In a union, overlap is permitted; all members are placed at
4758 : offset zero. */
4759 19595791 : if (TREE_CODE (rli->t) == UNION_TYPE)
4760 : break;
4761 21116979 : if (layout_conflict_p (field_p ? type : binfo, offset,
4762 : offsets, field_p))
4763 : {
4764 : /* Strip off the size allocated to this field. That puts us
4765 : at the first place we could have put the field with
4766 : proper alignment. */
4767 233 : *rli = old_rli;
4768 :
4769 : /* Bump up by the alignment required for the type. */
4770 233 : rli->bitpos
4771 233 : = size_binop (PLUS_EXPR, rli->bitpos,
4772 : bitsize_int (binfo
4773 : ? CLASSTYPE_ALIGN (type)
4774 : : TYPE_ALIGN (type)));
4775 233 : normalize_rli (rli);
4776 : }
4777 18543461 : else if (TREE_CODE (type) == NULLPTR_TYPE
4778 83770 : && warn_abi && abi_version_crosses (9))
4779 : {
4780 : /* Before ABI v9, we were giving nullptr_t alignment of 1; if
4781 : the offset wasn't aligned like a pointer when we started to
4782 : layout this field, that affects its position. */
4783 12 : tree pos = rli_size_unit_so_far (&old_rli);
4784 12 : if (int_cst_value (pos) % TYPE_ALIGN_UNIT (ptr_type_node) != 0)
4785 : {
4786 3 : if (abi_version_at_least (9))
4787 3 : warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wabi,
4788 : "alignment of %qD increased in %<-fabi-version=9%> "
4789 : "(GCC 5.2)", decl);
4790 : else
4791 0 : warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wabi, "alignment "
4792 : "of %qD will increase in %<-fabi-version=9%>",
4793 : decl);
4794 : }
4795 : break;
4796 : }
4797 : else
4798 : /* There was no conflict. We're done laying out this field. */
4799 : break;
4800 : }
4801 :
4802 : /* Now that we know where it will be placed, update its
4803 : BINFO_OFFSET. */
4804 22168784 : if (binfo && CLASS_TYPE_P (BINFO_TYPE (binfo)))
4805 : /* Indirect virtual bases may have a nonzero BINFO_OFFSET at
4806 : this point because their BINFO_OFFSET is copied from another
4807 : hierarchy. Therefore, we may not need to add the entire
4808 : OFFSET. */
4809 2573226 : propagate_binfo_offsets (binfo,
4810 : size_diffop_loc (input_location,
4811 : fold_convert (ssizetype, offset),
4812 2573226 : fold_convert (ssizetype,
4813 : BINFO_OFFSET (binfo))));
4814 19595558 : }
4815 :
4816 : /* Returns true if TYPE is empty and OFFSET is nonzero. */
4817 :
4818 : static int
4819 435 : empty_base_at_nonzero_offset_p (tree type,
4820 : tree offset,
4821 : splay_tree /*offsets*/)
4822 : {
4823 435 : return is_empty_class (type) && !integer_zerop (offset);
4824 : }
4825 :
4826 : /* Layout the empty base BINFO. EOC indicates the byte currently just
4827 : past the end of the class, and should be correctly aligned for a
4828 : class of the type indicated by BINFO; OFFSETS gives the offsets of
4829 : the empty bases allocated so far. T is the most derived
4830 : type. Return nonzero iff we added it at the end. */
4831 :
4832 : static bool
4833 21473874 : layout_empty_base_or_field (record_layout_info rli, tree binfo_or_decl,
4834 : splay_tree offsets)
4835 : {
4836 21473874 : tree alignment;
4837 21473874 : bool atend = false;
4838 21473874 : tree binfo = NULL_TREE;
4839 21473874 : tree decl = NULL_TREE;
4840 21473874 : tree type;
4841 21473874 : if (TREE_CODE (binfo_or_decl) == TREE_BINFO)
4842 : {
4843 21248481 : binfo = binfo_or_decl;
4844 21248481 : type = BINFO_TYPE (binfo);
4845 : }
4846 : else
4847 : {
4848 225393 : decl = binfo_or_decl;
4849 225393 : type = TREE_TYPE (decl);
4850 : }
4851 :
4852 : /* On some platforms (ARM), even empty classes will not be
4853 : byte-aligned. */
4854 21473874 : tree eoc = round_up_loc (input_location,
4855 : rli_size_unit_so_far (rli),
4856 21473874 : CLASSTYPE_ALIGN_UNIT (type));
4857 :
4858 : /* This routine should only be used for empty classes. */
4859 21473874 : gcc_assert (is_empty_class (type));
4860 :
4861 21473874 : if (decl && DECL_USER_ALIGN (decl))
4862 3 : alignment = size_int (DECL_ALIGN_UNIT (decl));
4863 : else
4864 21473871 : alignment = size_int (CLASSTYPE_ALIGN_UNIT (type));
4865 :
4866 : /* This is an empty base class. We first try to put it at offset
4867 : zero. */
4868 21473874 : tree offset = size_zero_node;
4869 21473874 : if (TREE_CODE (rli->t) != UNION_TYPE
4870 21473874 : && layout_conflict_p (type,
4871 : offset,
4872 : offsets,
4873 : /*vbases_p=*/0))
4874 : {
4875 : /* That didn't work. Now, we move forward from the next
4876 : available spot in the class. */
4877 2975 : atend = true;
4878 : offset = eoc;
4879 4363 : while (1)
4880 : {
4881 2975 : if (!layout_conflict_p (type,
4882 : offset,
4883 : offsets,
4884 : /*vbases_p=*/0))
4885 : /* We finally found a spot where there's no overlap. */
4886 : break;
4887 :
4888 : /* There's overlap here, too. Bump along to the next spot. */
4889 1388 : offset = size_binop (PLUS_EXPR, offset, alignment);
4890 : }
4891 : }
4892 :
4893 21473874 : if (decl && DECL_USER_ALIGN (decl))
4894 : {
4895 3 : rli->record_align = MAX (rli->record_align, DECL_ALIGN (decl));
4896 3 : if (warn_packed)
4897 0 : rli->unpacked_align = MAX (rli->unpacked_align, DECL_ALIGN (decl));
4898 3 : TYPE_USER_ALIGN (rli->t) = 1;
4899 : }
4900 21473871 : else if (CLASSTYPE_USER_ALIGN (type))
4901 : {
4902 21 : rli->record_align = MAX (rli->record_align, CLASSTYPE_ALIGN (type));
4903 21 : if (warn_packed)
4904 0 : rli->unpacked_align = MAX (rli->unpacked_align, CLASSTYPE_ALIGN (type));
4905 21 : TYPE_USER_ALIGN (rli->t) = 1;
4906 : }
4907 :
4908 21473874 : if (binfo)
4909 : /* Adjust BINFO_OFFSET (binfo) to be exactly OFFSET. */
4910 42496962 : propagate_binfo_offsets (binfo,
4911 21248481 : size_diffop (offset, BINFO_OFFSET (binfo)));
4912 : else
4913 : {
4914 225393 : DECL_FIELD_OFFSET (decl) = offset;
4915 225393 : DECL_FIELD_BIT_OFFSET (decl) = bitsize_zero_node;
4916 225393 : SET_DECL_OFFSET_ALIGN (decl, BITS_PER_UNIT);
4917 : }
4918 :
4919 21473874 : return atend;
4920 : }
4921 :
4922 : /* Build the FIELD_DECL for BASETYPE as a base of T, add it to the chain of
4923 : fields at NEXT_FIELD, and return it. */
4924 :
4925 : static tree
4926 23742627 : build_base_field_1 (tree t, tree binfo, tree access, tree *&next_field)
4927 : {
4928 : /* Create the FIELD_DECL. */
4929 23742627 : tree basetype = BINFO_TYPE (binfo);
4930 23742627 : tree as_base = CLASSTYPE_AS_BASE (basetype);
4931 23742627 : gcc_assert (as_base);
4932 23742627 : tree decl = build_decl (input_location, FIELD_DECL, NULL_TREE, as_base);
4933 :
4934 23742627 : DECL_ARTIFICIAL (decl) = 1;
4935 23742627 : DECL_IGNORED_P (decl) = 1;
4936 23742627 : DECL_FIELD_CONTEXT (decl) = t;
4937 23742627 : if (is_empty_class (basetype))
4938 : /* CLASSTYPE_SIZE is one byte, but the field needs to have size zero. */
4939 21169401 : DECL_SIZE (decl) = DECL_SIZE_UNIT (decl) = size_zero_node;
4940 : else
4941 : {
4942 2573226 : DECL_SIZE (decl) = CLASSTYPE_SIZE (basetype);
4943 2573226 : DECL_SIZE_UNIT (decl) = CLASSTYPE_SIZE_UNIT (basetype);
4944 : }
4945 23742627 : SET_DECL_ALIGN (decl, CLASSTYPE_ALIGN (basetype));
4946 23742627 : DECL_USER_ALIGN (decl) = CLASSTYPE_USER_ALIGN (basetype);
4947 23742627 : SET_DECL_MODE (decl, TYPE_MODE (basetype));
4948 23742627 : DECL_FIELD_IS_BASE (decl) = 1;
4949 :
4950 23742627 : if (access == access_private_node)
4951 443028 : TREE_PRIVATE (decl) = true;
4952 23299599 : else if (access == access_protected_node)
4953 87038 : TREE_PROTECTED (decl) = true;
4954 :
4955 : /* Add the new FIELD_DECL to the list of fields for T. */
4956 23742627 : DECL_CHAIN (decl) = *next_field;
4957 23742627 : *next_field = decl;
4958 23742627 : next_field = &DECL_CHAIN (decl);
4959 :
4960 23742627 : return decl;
4961 : }
4962 :
4963 : /* Layout the base given by BINFO in the class indicated by RLI.
4964 : *BASE_ALIGN is a running maximum of the alignments of
4965 : any base class. OFFSETS gives the location of empty base
4966 : subobjects. T is the most derived type. Return nonzero if the new
4967 : object cannot be nearly-empty. A new FIELD_DECL is inserted at
4968 : *NEXT_FIELD, unless BINFO is for an empty base class.
4969 :
4970 : Returns the location at which the next field should be inserted. */
4971 :
4972 : static tree *
4973 23821707 : build_base_field (record_layout_info rli, tree binfo, tree access,
4974 : splay_tree offsets, tree *next_field)
4975 : {
4976 23821707 : tree t = rli->t;
4977 23821707 : tree basetype = BINFO_TYPE (binfo);
4978 :
4979 23821707 : if (!COMPLETE_TYPE_P (basetype))
4980 : /* This error is now reported in xref_tag, thus giving better
4981 : location information. */
4982 0 : return next_field;
4983 :
4984 : /* Place the base class. */
4985 23821707 : if (!is_empty_class (basetype))
4986 : {
4987 2573226 : tree decl;
4988 :
4989 : /* The containing class is non-empty because it has a non-empty
4990 : base class. */
4991 2573226 : CLASSTYPE_EMPTY_P (t) = 0;
4992 :
4993 : /* Create the FIELD_DECL. */
4994 2573226 : decl = build_base_field_1 (t, binfo, access, next_field);
4995 :
4996 : /* Try to place the field. It may take more than one try if we
4997 : have a hard time placing the field without putting two
4998 : objects of the same type at the same address. */
4999 2573226 : layout_nonempty_base_or_field (rli, decl, binfo, offsets);
5000 : }
5001 : else
5002 : {
5003 21248481 : bool atend = layout_empty_base_or_field (rli, binfo, offsets);
5004 : /* A nearly-empty class "has no proper base class that is empty,
5005 : not morally virtual, and at an offset other than zero." */
5006 21248481 : if (!BINFO_VIRTUAL_P (binfo) && CLASSTYPE_NEARLY_EMPTY_P (t))
5007 : {
5008 426 : if (atend)
5009 94 : CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
5010 : /* The check above (used in G++ 3.2) is insufficient because
5011 : an empty class placed at offset zero might itself have an
5012 : empty base at a nonzero offset. */
5013 332 : else if (walk_subobject_offsets (basetype,
5014 : empty_base_at_nonzero_offset_p,
5015 : size_zero_node,
5016 : /*offsets=*/NULL,
5017 : /*max_offset=*/NULL_TREE,
5018 : /*vbases_p=*/true))
5019 6 : CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
5020 : }
5021 :
5022 : /* We used to not create a FIELD_DECL for empty base classes because of
5023 : back end issues with overlapping FIELD_DECLs, but that doesn't seem to
5024 : be a problem anymore. We need them to handle initialization of C++17
5025 : aggregate bases. */
5026 42419583 : if (cxx_dialect >= cxx17 && !BINFO_VIRTUAL_P (binfo))
5027 : {
5028 21169401 : tree decl = build_base_field_1 (t, binfo, access, next_field);
5029 21169401 : DECL_FIELD_OFFSET (decl) = BINFO_OFFSET (binfo);
5030 21169401 : DECL_FIELD_BIT_OFFSET (decl) = bitsize_zero_node;
5031 21169401 : SET_DECL_OFFSET_ALIGN (decl, BITS_PER_UNIT);
5032 21169401 : SET_DECL_FIELD_ABI_IGNORED (decl, 1);
5033 : }
5034 :
5035 : /* An empty virtual base causes a class to be non-empty
5036 : -- but in that case we do not need to clear CLASSTYPE_EMPTY_P
5037 : here because that was already done when the virtual table
5038 : pointer was created. */
5039 : }
5040 :
5041 : /* Record the offsets of BINFO and its base subobjects. */
5042 23821707 : record_subobject_offsets (binfo, offsets);
5043 :
5044 23821707 : return next_field;
5045 : }
5046 :
5047 : /* Layout all of the non-virtual base classes. Record empty
5048 : subobjects in OFFSETS. T is the most derived type. Return nonzero
5049 : if the type cannot be nearly empty. The fields created
5050 : corresponding to the base classes will be inserted at
5051 : *NEXT_FIELD. */
5052 :
5053 : static void
5054 49904436 : build_base_fields (record_layout_info rli,
5055 : splay_tree offsets, tree *next_field)
5056 : {
5057 : /* Chain to hold all the new FIELD_DECLs which stand in for base class
5058 : subobjects. */
5059 49904436 : tree t = rli->t;
5060 49904436 : tree binfo = TYPE_BINFO (t);
5061 49904436 : int n_baseclasses = BINFO_N_BASE_BINFOS (binfo);
5062 :
5063 : /* The primary base class is always allocated first. */
5064 49904436 : const tree primary_binfo = CLASSTYPE_PRIMARY_BINFO (t);
5065 49904436 : if (primary_binfo)
5066 : {
5067 : /* We need to walk BINFO_BASE_BINFO to find the access of the primary
5068 : base, if it is direct. Indirect base fields are private. */
5069 1491715 : tree primary_access = access_private_node;
5070 1492596 : for (int i = 0; i < n_baseclasses; ++i)
5071 : {
5072 1492525 : tree base_binfo = BINFO_BASE_BINFO (binfo, i);
5073 1492525 : if (base_binfo == primary_binfo)
5074 : {
5075 1491644 : primary_access = BINFO_BASE_ACCESS (binfo, i);
5076 1491644 : break;
5077 : }
5078 : }
5079 1491715 : next_field = build_base_field (rli, primary_binfo,
5080 : primary_access,
5081 : offsets, next_field);
5082 : }
5083 :
5084 : /* Now allocate the rest of the bases. */
5085 73584690 : for (int i = 0; i < n_baseclasses; ++i)
5086 : {
5087 23680254 : tree base_binfo = BINFO_BASE_BINFO (binfo, i);
5088 :
5089 : /* The primary base was already allocated above, so we don't
5090 : need to allocate it again here. */
5091 23680254 : if (base_binfo == primary_binfo)
5092 1491644 : continue;
5093 :
5094 : /* Virtual bases are added at the end (a primary virtual base
5095 : will have already been added). */
5096 22188610 : if (BINFO_VIRTUAL_P (base_binfo))
5097 45235 : continue;
5098 :
5099 22143375 : next_field = build_base_field (rli, base_binfo,
5100 22143375 : BINFO_BASE_ACCESS (binfo, i),
5101 : offsets, next_field);
5102 : }
5103 49904436 : }
5104 :
5105 : /* Go through the TYPE_FIELDS of T issuing any appropriate
5106 : diagnostics, figuring out which methods override which other
5107 : methods, and so forth. */
5108 :
5109 : static void
5110 49904436 : check_methods (tree t)
5111 : {
5112 323568063 : for (tree x = TYPE_FIELDS (t); x; x = DECL_CHAIN (x))
5113 273663627 : if (DECL_DECLARES_FUNCTION_P (x))
5114 : {
5115 136699312 : check_for_override (x, t);
5116 :
5117 136699312 : if (DECL_PURE_VIRTUAL_P (x)
5118 136699312 : && (TREE_CODE (x) != FUNCTION_DECL || ! DECL_VINDEX (x)))
5119 3 : error ("initializer specified for non-virtual method %q+D", x);
5120 : /* The name of the field is the original field name
5121 : Save this in auxiliary field for later overloading. */
5122 136699312 : if (TREE_CODE (x) == FUNCTION_DECL && DECL_VINDEX (x))
5123 : {
5124 6088673 : TYPE_POLYMORPHIC_P (t) = 1;
5125 6088673 : if (DECL_PURE_VIRTUAL_P (x))
5126 737817 : vec_safe_push (CLASSTYPE_PURE_VIRTUALS (t), x);
5127 : }
5128 :
5129 136699312 : if (!DECL_VIRTUAL_P (x)
5130 267309967 : && lookup_attribute ("transaction_safe_dynamic",
5131 130610655 : DECL_ATTRIBUTES (x)))
5132 2 : error_at (DECL_SOURCE_LOCATION (x),
5133 : "%<transaction_safe_dynamic%> may only be specified for "
5134 : "a virtual function");
5135 : }
5136 :
5137 : /* Check whether the eligible special member functions (P0848) are
5138 : user-provided. add_method arranged that the CLASSTYPE_MEMBER_VEC only
5139 : has the eligible ones, unless none are eligible; TYPE_FIELDS also contains
5140 : ineligible overloads, which is why this needs to be separate from the loop
5141 : above. */
5142 :
5143 49904436 : if (tree dtor = CLASSTYPE_DESTRUCTOR (t))
5144 : {
5145 3646522 : if (TREE_CODE (dtor) == OVERLOAD)
5146 : {
5147 : /* P0848: At the end of the definition of a class, overload
5148 : resolution is performed among the prospective destructors declared
5149 : in that class with an empty argument list to select the destructor
5150 : for the class, also known as the selected destructor. The program
5151 : is ill-formed if overload resolution fails. */
5152 6 : int viable = 0;
5153 24 : for (tree fn : ovl_range (dtor))
5154 12 : if (constraints_satisfied_p (fn))
5155 6 : ++viable;
5156 6 : gcc_checking_assert (viable != 1);
5157 :
5158 6 : auto_diagnostic_group d;
5159 6 : if (viable == 0)
5160 3 : error_at (location_of (t), "no viable destructor for %qT", t);
5161 : else
5162 3 : error_at (location_of (t), "destructor for %qT is ambiguous", t);
5163 6 : print_candidates (location_of (t), dtor);
5164 :
5165 : /* Arbitrarily prune the overload set to a single function for
5166 : sake of error recovery. */
5167 6 : tree *slot = find_member_slot (t, dtor_identifier);
5168 6 : *slot = get_first_fn (dtor);
5169 6 : }
5170 3646516 : else if (user_provided_p (dtor))
5171 2831162 : TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t) = true;
5172 : }
5173 :
5174 99275316 : for (tree fn : ovl_range (CLASSTYPE_CONSTRUCTORS (t)))
5175 : {
5176 28036737 : if (!user_provided_p (fn))
5177 : /* Might be trivial. */;
5178 22023358 : else if (TREE_CODE (fn) == TEMPLATE_DECL)
5179 : /* Templates are never special members. */;
5180 14258825 : else if (copy_fn_p (fn)
5181 14258825 : && constraints_satisfied_p (fn))
5182 1027379 : TYPE_HAS_COMPLEX_COPY_CTOR (t) = true;
5183 13231446 : else if (move_fn_p (fn)
5184 13231446 : && constraints_satisfied_p (fn))
5185 915063 : TYPE_HAS_COMPLEX_MOVE_CTOR (t) = true;
5186 : }
5187 :
5188 62378511 : for (tree fn : ovl_range (get_class_binding_direct (t, assign_op_identifier)))
5189 : {
5190 8001026 : if (!user_provided_p (fn))
5191 : /* Might be trivial. */;
5192 4171180 : else if (TREE_CODE (fn) == TEMPLATE_DECL)
5193 : /* Templates are never special members. */;
5194 3066459 : else if (copy_fn_p (fn)
5195 3066459 : && constraints_satisfied_p (fn))
5196 653550 : TYPE_HAS_COMPLEX_COPY_ASSIGN (t) = true;
5197 2412909 : else if (move_fn_p (fn)
5198 2412909 : && constraints_satisfied_p (fn))
5199 867507 : TYPE_HAS_COMPLEX_MOVE_ASSIGN (t) = true;
5200 : }
5201 49904436 : }
5202 :
5203 : /* FN is constructor, destructor or operator function. Clone the
5204 : declaration to create a NAME'd variant. NEED_VTT_PARM_P and
5205 : OMIT_INHERITED_PARMS_P are relevant if it's a cdtor. */
5206 :
5207 : static tree
5208 128520259 : copy_fndecl_with_name (tree fn, tree name, tree_code code,
5209 : bool need_vtt_parm_p, bool omit_inherited_parms_p)
5210 : {
5211 : /* Copy the function. */
5212 128520259 : tree clone = copy_decl (fn);
5213 : /* Reset the function name. */
5214 128520259 : DECL_NAME (clone) = name;
5215 :
5216 128520259 : if (flag_concepts)
5217 : /* Clone constraints. */
5218 127190407 : if (tree ci = get_constraints (fn))
5219 11858270 : set_constraints (clone, copy_node (ci));
5220 :
5221 128520259 : SET_DECL_ASSEMBLER_NAME (clone, NULL_TREE);
5222 128520259 : DECL_ABSTRACT_P (clone) = false;
5223 : /* There's no pending inline data for this function. */
5224 128520259 : DECL_PENDING_INLINE_INFO (clone) = NULL;
5225 128520259 : DECL_PENDING_INLINE_P (clone) = 0;
5226 :
5227 128520259 : if (name == base_dtor_identifier)
5228 : {
5229 : /* The base-class destructor is not virtual. */
5230 10727806 : DECL_VIRTUAL_P (clone) = 0;
5231 10727806 : DECL_VINDEX (clone) = NULL_TREE;
5232 : }
5233 117792453 : else if (code != ERROR_MARK)
5234 : {
5235 : /* Set the operator code. */
5236 675 : const ovl_op_info_t *ovl_op = OVL_OP_INFO (false, code);
5237 675 : DECL_OVERLOADED_OPERATOR_CODE_RAW (clone) = ovl_op->ovl_op_code;
5238 :
5239 : /* The operator could be virtual. */
5240 675 : if (DECL_VIRTUAL_P (clone))
5241 12 : IDENTIFIER_VIRTUAL_P (name) = true;
5242 : }
5243 :
5244 128520259 : if (omit_inherited_parms_p)
5245 120 : gcc_assert (DECL_HAS_IN_CHARGE_PARM_P (clone));
5246 :
5247 : /* If there was an in-charge parameter, drop it from the function
5248 : type. */
5249 128520259 : if (DECL_HAS_IN_CHARGE_PARM_P (clone))
5250 : {
5251 2448206 : tree basetype = TYPE_METHOD_BASETYPE (TREE_TYPE (clone));
5252 2448206 : tree parmtypes = TYPE_ARG_TYPES (TREE_TYPE (clone));
5253 : /* Skip the `this' parameter. */
5254 2448206 : parmtypes = TREE_CHAIN (parmtypes);
5255 : /* Skip the in-charge parameter. */
5256 2448206 : parmtypes = TREE_CHAIN (parmtypes);
5257 : /* And the VTT parm, in a complete [cd]tor. */
5258 2448206 : if (DECL_HAS_VTT_PARM_P (fn) && !need_vtt_parm_p)
5259 1312634 : parmtypes = TREE_CHAIN (parmtypes);
5260 2448206 : if (omit_inherited_parms_p)
5261 : {
5262 : /* If we're omitting inherited parms, that just leaves the VTT. */
5263 120 : gcc_assert (need_vtt_parm_p);
5264 120 : parmtypes = tree_cons (NULL_TREE, vtt_parm_type, void_list_node);
5265 : }
5266 2448206 : TREE_TYPE (clone)
5267 2448206 : = build_method_type_directly (basetype,
5268 2448206 : TREE_TYPE (TREE_TYPE (clone)),
5269 : parmtypes);
5270 2448206 : TREE_TYPE (clone)
5271 2448206 : = cp_build_type_attribute_variant (TREE_TYPE (clone),
5272 2448206 : TYPE_ATTRIBUTES (TREE_TYPE (fn)));
5273 2448206 : TREE_TYPE (clone)
5274 4896412 : = cxx_copy_lang_qualifiers (TREE_TYPE (clone), TREE_TYPE (fn));
5275 : }
5276 :
5277 : /* Copy the function parameters. */
5278 128520259 : DECL_ARGUMENTS (clone) = copy_list (DECL_ARGUMENTS (clone));
5279 :
5280 : /* Remove the in-charge parameter. */
5281 128520259 : if (DECL_HAS_IN_CHARGE_PARM_P (clone))
5282 : {
5283 2448206 : DECL_CHAIN (DECL_ARGUMENTS (clone))
5284 2448206 : = DECL_CHAIN (DECL_CHAIN (DECL_ARGUMENTS (clone)));
5285 2448206 : DECL_HAS_IN_CHARGE_PARM_P (clone) = 0;
5286 : }
5287 :
5288 : /* And the VTT parm, in a complete [cd]tor. */
5289 128520259 : if (DECL_HAS_VTT_PARM_P (fn))
5290 : {
5291 2448206 : if (need_vtt_parm_p)
5292 1135572 : DECL_HAS_VTT_PARM_P (clone) = 1;
5293 : else
5294 : {
5295 2625268 : DECL_CHAIN (DECL_ARGUMENTS (clone))
5296 1312634 : = DECL_CHAIN (DECL_CHAIN (DECL_ARGUMENTS (clone)));
5297 1312634 : DECL_HAS_VTT_PARM_P (clone) = 0;
5298 : }
5299 : }
5300 :
5301 : /* A base constructor inheriting from a virtual base doesn't get the
5302 : arguments. */
5303 128520259 : if (omit_inherited_parms_p)
5304 120 : DECL_CHAIN (DECL_CHAIN (DECL_ARGUMENTS (clone))) = NULL_TREE;
5305 :
5306 395066334 : for (tree parms = DECL_ARGUMENTS (clone); parms; parms = DECL_CHAIN (parms))
5307 : {
5308 266546075 : DECL_CONTEXT (parms) = clone;
5309 266546075 : cxx_dup_lang_specific_decl (parms);
5310 : }
5311 :
5312 : /* Create the RTL for this function. */
5313 128520259 : SET_DECL_RTL (clone, NULL);
5314 :
5315 : /* Regardless of the current scope, this is a member function, so
5316 : not at namespace scope. */
5317 128520259 : rest_of_decl_compilation (clone, /*top_level=*/0, at_eof);
5318 :
5319 128520259 : return clone;
5320 : }
5321 :
5322 : /* FN is an operator function, create a variant for CODE. */
5323 :
5324 : tree
5325 675 : copy_operator_fn (tree fn, tree_code code)
5326 : {
5327 675 : return copy_fndecl_with_name (fn, ovl_op_identifier (code),
5328 675 : code, false, false);
5329 : }
5330 :
5331 : /* FN is a constructor or destructor. Clone the declaration to create
5332 : a specialized in-charge or not-in-charge version, as indicated by
5333 : NAME. */
5334 :
5335 : static tree
5336 146042888 : build_clone (tree fn, tree name, bool need_vtt_parm_p,
5337 : bool omit_inherited_parms_p)
5338 : {
5339 146042888 : tree clone;
5340 :
5341 : /* If this is a template, do the rest on the DECL_TEMPLATE_RESULT. */
5342 146042888 : if (TREE_CODE (fn) == TEMPLATE_DECL)
5343 : {
5344 17523304 : clone = copy_decl (fn);
5345 17523304 : DECL_NAME (clone) = name;
5346 :
5347 17523304 : tree result = build_clone (DECL_TEMPLATE_RESULT (clone), name,
5348 : need_vtt_parm_p, omit_inherited_parms_p);
5349 17523304 : DECL_TEMPLATE_RESULT (clone) = result;
5350 :
5351 17523304 : DECL_TEMPLATE_INFO (result) = copy_node (DECL_TEMPLATE_INFO (result));
5352 17523304 : DECL_TI_TEMPLATE (result) = clone;
5353 :
5354 17523304 : TREE_TYPE (clone) = TREE_TYPE (result);
5355 : }
5356 : else
5357 : {
5358 128519584 : clone = copy_fndecl_with_name (fn, name, ERROR_MARK,
5359 : need_vtt_parm_p, omit_inherited_parms_p);
5360 128519584 : DECL_CLONED_FUNCTION (clone) = fn;
5361 :
5362 128519584 : maybe_prepare_return_this (clone);
5363 : }
5364 :
5365 : /* Remember where this function came from. */
5366 146042888 : DECL_ABSTRACT_ORIGIN (clone) = fn;
5367 :
5368 : /* Make it easy to find the CLONE given the FN. Note the
5369 : template_result of a template will be chained this way too. */
5370 146042888 : DECL_CHAIN (clone) = DECL_CHAIN (fn);
5371 146042888 : DECL_CHAIN (fn) = clone;
5372 :
5373 146042888 : return clone;
5374 : }
5375 :
5376 : /* Build the clones of FN, return the number of clones built. These
5377 : will be inserted onto DECL_CHAIN of FN. */
5378 :
5379 : void
5380 63447108 : build_cdtor_clones (tree fn, bool needs_vtt_p, bool base_omits_inherited_p,
5381 : bool update_methods)
5382 : {
5383 63447108 : unsigned count = 0;
5384 :
5385 63447108 : if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (fn))
5386 : {
5387 : /* For each constructor, we need two variants: an in-charge version
5388 : and a not-in-charge version. */
5389 52719302 : build_clone (fn, complete_ctor_identifier, false, false);
5390 52719302 : build_clone (fn, base_ctor_identifier, needs_vtt_p,
5391 : base_omits_inherited_p);
5392 52719302 : count += 2;
5393 : }
5394 : else
5395 : {
5396 10727806 : gcc_assert (DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (fn));
5397 :
5398 : /* For each destructor, we need three variants: an in-charge
5399 : version, a not-in-charge version, and an in-charge deleting
5400 : version. We clone the deleting version first because that
5401 : means it will go second on the TYPE_FIELDS list -- and that
5402 : corresponds to the correct layout order in the virtual
5403 : function table.
5404 :
5405 : For a non-virtual destructor, we do not build a deleting
5406 : destructor. */
5407 10727806 : if (DECL_VIRTUAL_P (fn))
5408 : {
5409 1625368 : build_clone (fn, deleting_dtor_identifier, false, false);
5410 1625368 : count++;
5411 : }
5412 10727806 : build_clone (fn, complete_dtor_identifier, false, false);
5413 10727806 : build_clone (fn, base_dtor_identifier, needs_vtt_p, false);
5414 10727806 : count += 2;
5415 : }
5416 :
5417 : /* The original is now an abstract function that is never
5418 : emitted. */
5419 63447108 : DECL_ABSTRACT_P (fn) = true;
5420 :
5421 63447108 : if (update_methods)
5422 162536358 : for (tree clone = fn; count--;)
5423 : {
5424 108898897 : clone = DECL_CHAIN (clone);
5425 108898897 : add_method (DECL_CONTEXT (clone), clone, false);
5426 : }
5427 63447108 : }
5428 :
5429 : /* Produce declarations for all appropriate clones of FN. If
5430 : UPDATE_METHODS is true, the clones are added to the
5431 : CLASSTYPE_MEMBER_VEC. */
5432 :
5433 : void
5434 63760896 : clone_cdtor (tree fn, bool update_methods)
5435 : {
5436 : /* Avoid inappropriate cloning. */
5437 63760896 : if (DECL_CHAIN (fn)
5438 63760896 : && DECL_CLONED_FUNCTION_P (DECL_CHAIN (fn)))
5439 : return;
5440 :
5441 : /* Base cdtors need a vtt parm if there are virtual bases. */
5442 63401838 : bool vtt = CLASSTYPE_VBASECLASSES (DECL_CONTEXT (fn));
5443 :
5444 : /* Base ctor omits inherited parms it needs a vttparm and inherited
5445 : from a virtual nase ctor. */
5446 63401838 : bool base_omits_inherited = (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (fn)
5447 63401838 : && base_ctor_omit_inherited_parms (fn));
5448 :
5449 63401838 : build_cdtor_clones (fn, vtt, base_omits_inherited, update_methods);
5450 : }
5451 :
5452 : /* DECL is an in charge constructor, which is being defined. This will
5453 : have had an in class declaration, from whence clones were
5454 : declared. An out-of-class definition can specify additional default
5455 : arguments. As it is the clones that are involved in overload
5456 : resolution, we must propagate the information from the DECL to its
5457 : clones. */
5458 :
5459 : void
5460 19879350 : adjust_clone_args (tree decl)
5461 : {
5462 19879350 : tree clone;
5463 :
5464 32700586 : for (clone = DECL_CHAIN (decl); clone && DECL_CLONED_FUNCTION_P (clone);
5465 12821236 : clone = DECL_CHAIN (clone))
5466 : {
5467 12821236 : tree orig_clone_parms = TYPE_ARG_TYPES (TREE_TYPE (clone));
5468 12821236 : tree orig_decl_parms = TYPE_ARG_TYPES (TREE_TYPE (decl));
5469 12821236 : tree decl_parms, clone_parms;
5470 :
5471 : /* Skip the 'this' parameter. */
5472 12821236 : orig_clone_parms = TREE_CHAIN (orig_clone_parms);
5473 12821236 : orig_decl_parms = TREE_CHAIN (orig_decl_parms);
5474 :
5475 12821236 : if (DECL_HAS_IN_CHARGE_PARM_P (decl))
5476 19164 : orig_decl_parms = TREE_CHAIN (orig_decl_parms);
5477 12821236 : if (DECL_HAS_VTT_PARM_P (decl))
5478 19164 : orig_decl_parms = TREE_CHAIN (orig_decl_parms);
5479 :
5480 12821236 : clone_parms = orig_clone_parms;
5481 12821236 : if (DECL_HAS_VTT_PARM_P (clone))
5482 9582 : clone_parms = TREE_CHAIN (clone_parms);
5483 :
5484 25899728 : for (decl_parms = orig_decl_parms; decl_parms;
5485 13078492 : decl_parms = TREE_CHAIN (decl_parms),
5486 13078492 : clone_parms = TREE_CHAIN (clone_parms))
5487 : {
5488 25898084 : if (clone_parms == void_list_node)
5489 : {
5490 12819568 : gcc_assert (decl_parms == clone_parms
5491 : || ctor_omit_inherited_parms (clone));
5492 : break;
5493 : }
5494 :
5495 13078516 : gcc_checking_assert (same_type_p (TREE_VALUE (decl_parms),
5496 : TREE_VALUE (clone_parms)));
5497 :
5498 14215382 : if (TREE_PURPOSE (decl_parms) && !TREE_PURPOSE (clone_parms))
5499 : {
5500 : /* A default parameter has been added. Adjust the
5501 : clone's parameters. */
5502 24 : clone_parms = orig_decl_parms;
5503 :
5504 24 : if (DECL_HAS_VTT_PARM_P (clone))
5505 : {
5506 3 : clone_parms = tree_cons (TREE_PURPOSE (orig_clone_parms),
5507 3 : TREE_VALUE (orig_clone_parms),
5508 : clone_parms);
5509 3 : TREE_TYPE (clone_parms) = TREE_TYPE (orig_clone_parms);
5510 : }
5511 :
5512 24 : tree basetype = TYPE_METHOD_BASETYPE (TREE_TYPE (clone));
5513 24 : tree type
5514 24 : = build_method_type_directly (basetype,
5515 24 : TREE_TYPE (TREE_TYPE (clone)),
5516 : clone_parms);
5517 24 : if (tree attrs = TYPE_ATTRIBUTES (TREE_TYPE (clone)))
5518 0 : type = cp_build_type_attribute_variant (type, attrs);
5519 24 : type = cxx_copy_lang_qualifiers (type, TREE_TYPE (clone));
5520 24 : TREE_TYPE (clone) = type;
5521 :
5522 24 : clone_parms = NULL_TREE;
5523 24 : break;
5524 : }
5525 : }
5526 12821236 : gcc_assert (!clone_parms || clone_parms == void_list_node);
5527 : }
5528 19879350 : }
5529 :
5530 : /* For each of the constructors and destructors in T, create an
5531 : in-charge and not-in-charge variant. */
5532 :
5533 : static void
5534 49904436 : clone_constructors_and_destructors (tree t)
5535 : {
5536 : /* We do not need to propagate the usingness to the clone, at this
5537 : point that is not needed. */
5538 100143485 : for (tree fn : ovl_range (CLASSTYPE_CONSTRUCTORS (t)))
5539 28470826 : clone_cdtor (fn, /*update_methods=*/true);
5540 :
5541 49904436 : if (tree dtor = CLASSTYPE_DESTRUCTOR (t))
5542 3829751 : clone_cdtor (dtor, /*update_methods=*/true);
5543 49904436 : }
5544 :
5545 : /* Deduce noexcept for a destructor DTOR. */
5546 :
5547 : void
5548 3706833 : deduce_noexcept_on_destructor (tree dtor)
5549 : {
5550 3706833 : if (!TYPE_RAISES_EXCEPTIONS (TREE_TYPE (dtor)))
5551 2495524 : TREE_TYPE (dtor) = build_exception_variant (TREE_TYPE (dtor),
5552 : noexcept_deferred_spec);
5553 3706833 : }
5554 :
5555 : /* Subroutine of set_one_vmethod_tm_attributes. Search base classes
5556 : of TYPE for virtual functions which FNDECL overrides. Return a
5557 : mask of the tm attributes found therein. */
5558 :
5559 : static int
5560 925 : look_for_tm_attr_overrides (tree type, tree fndecl)
5561 : {
5562 925 : tree binfo = TYPE_BINFO (type);
5563 925 : tree base_binfo;
5564 925 : int ix, found = 0;
5565 :
5566 1549 : for (ix = 0; BINFO_BASE_ITERATE (binfo, ix, base_binfo); ++ix)
5567 : {
5568 624 : tree o, basetype = BINFO_TYPE (base_binfo);
5569 :
5570 624 : if (!TYPE_POLYMORPHIC_P (basetype))
5571 48 : continue;
5572 :
5573 576 : o = look_for_overrides_here (basetype, fndecl);
5574 576 : if (o)
5575 : {
5576 411 : if (lookup_attribute ("transaction_safe_dynamic",
5577 411 : DECL_ATTRIBUTES (o)))
5578 : /* transaction_safe_dynamic is not inherited. */;
5579 : else
5580 311 : found |= tm_attr_to_mask (find_tm_attribute
5581 311 : (TYPE_ATTRIBUTES (TREE_TYPE (o))));
5582 : }
5583 : else
5584 165 : found |= look_for_tm_attr_overrides (basetype, fndecl);
5585 : }
5586 :
5587 925 : return found;
5588 : }
5589 :
5590 : /* Subroutine of set_method_tm_attributes. Handle the checks and
5591 : inheritance for one virtual method FNDECL. */
5592 :
5593 : static void
5594 760 : set_one_vmethod_tm_attributes (tree type, tree fndecl)
5595 : {
5596 760 : tree tm_attr;
5597 760 : int found, have;
5598 :
5599 760 : found = look_for_tm_attr_overrides (type, fndecl);
5600 :
5601 : /* If FNDECL doesn't actually override anything (i.e. T is the
5602 : class that first declares FNDECL virtual), then we're done. */
5603 760 : if (found == 0)
5604 : return;
5605 :
5606 45 : tm_attr = find_tm_attribute (TYPE_ATTRIBUTES (TREE_TYPE (fndecl)));
5607 45 : have = tm_attr_to_mask (tm_attr);
5608 :
5609 : /* Intel STM Language Extension 3.0, Section 4.2 table 4:
5610 : tm_pure must match exactly, otherwise no weakening of
5611 : tm_safe > tm_callable > nothing. */
5612 : /* ??? The tm_pure attribute didn't make the transition to the
5613 : multivendor language spec. */
5614 45 : if (have == TM_ATTR_PURE)
5615 : {
5616 0 : if (found != TM_ATTR_PURE)
5617 : {
5618 0 : found &= -found;
5619 0 : goto err_override;
5620 : }
5621 : }
5622 : /* If the overridden function is tm_pure, then FNDECL must be. */
5623 45 : else if (found == TM_ATTR_PURE && tm_attr)
5624 6 : goto err_override;
5625 : /* Look for base class combinations that cannot be satisfied. */
5626 39 : else if (found != TM_ATTR_PURE && (found & TM_ATTR_PURE))
5627 : {
5628 0 : found &= ~TM_ATTR_PURE;
5629 0 : found &= -found;
5630 0 : error_at (DECL_SOURCE_LOCATION (fndecl),
5631 : "method overrides both %<transaction_pure%> and %qE methods",
5632 : tm_mask_to_attr (found));
5633 : }
5634 : /* If FNDECL did not declare an attribute, then inherit the most
5635 : restrictive one. */
5636 39 : else if (tm_attr == NULL)
5637 : {
5638 14 : apply_tm_attr (fndecl, tm_mask_to_attr (least_bit_hwi (found)));
5639 : }
5640 : /* Otherwise validate that we're not weaker than a function
5641 : that is being overridden. */
5642 : else
5643 : {
5644 25 : found &= -found;
5645 25 : if (found <= TM_ATTR_CALLABLE && have > found)
5646 9 : goto err_override;
5647 : }
5648 : return;
5649 :
5650 15 : err_override:
5651 15 : error_at (DECL_SOURCE_LOCATION (fndecl),
5652 : "method declared %qE overriding %qE method",
5653 : tm_attr, tm_mask_to_attr (found));
5654 : }
5655 :
5656 : /* For each of the methods in T, propagate a class-level tm attribute. */
5657 :
5658 : static void
5659 49904436 : set_method_tm_attributes (tree t)
5660 : {
5661 49904436 : tree class_tm_attr, fndecl;
5662 :
5663 : /* Don't bother collecting tm attributes if transactional memory
5664 : support is not enabled. */
5665 49904436 : if (!flag_tm)
5666 : return;
5667 :
5668 : /* Process virtual methods first, as they inherit directly from the
5669 : base virtual function and also require validation of new attributes. */
5670 4802 : if (TYPE_CONTAINS_VPTR_P (t))
5671 : {
5672 200 : tree vchain;
5673 960 : for (vchain = BINFO_VIRTUALS (TYPE_BINFO (t)); vchain;
5674 760 : vchain = TREE_CHAIN (vchain))
5675 : {
5676 760 : fndecl = BV_FN (vchain);
5677 760 : if (DECL_THUNK_P (fndecl))
5678 3 : fndecl = THUNK_TARGET (fndecl);
5679 760 : set_one_vmethod_tm_attributes (t, fndecl);
5680 : }
5681 : }
5682 :
5683 : /* If the class doesn't have an attribute, nothing more to do. */
5684 4802 : class_tm_attr = find_tm_attribute (TYPE_ATTRIBUTES (t));
5685 4802 : if (class_tm_attr == NULL)
5686 : return;
5687 :
5688 : /* Any method that does not yet have a tm attribute inherits
5689 : the one from the class. */
5690 108 : for (fndecl = TYPE_FIELDS (t); fndecl; fndecl = DECL_CHAIN (fndecl))
5691 45 : if (DECL_DECLARES_FUNCTION_P (fndecl)
5692 81 : && !find_tm_attribute (TYPE_ATTRIBUTES (TREE_TYPE (fndecl))))
5693 21 : apply_tm_attr (fndecl, class_tm_attr);
5694 : }
5695 :
5696 : /* Returns true if FN is a default constructor. */
5697 :
5698 : bool
5699 12041254 : default_ctor_p (const_tree fn)
5700 : {
5701 12041254 : return (DECL_CONSTRUCTOR_P (fn)
5702 12041254 : && sufficient_parms_p (FUNCTION_FIRST_USER_PARMTYPE (fn)));
5703 : }
5704 :
5705 : /* Returns true iff class T has a user-provided constructor that can be called
5706 : with more than zero arguments. */
5707 :
5708 : bool
5709 55 : type_has_user_nondefault_constructor (tree t)
5710 : {
5711 55 : if (!TYPE_HAS_USER_CONSTRUCTOR (t))
5712 : return false;
5713 :
5714 65 : for (tree fn : ovl_range (CLASSTYPE_CONSTRUCTORS (t)))
5715 : {
5716 34 : if (user_provided_p (fn)
5717 34 : && (TREE_CODE (fn) == TEMPLATE_DECL
5718 18 : || (skip_artificial_parms_for (fn, DECL_ARGUMENTS (fn))
5719 : != NULL_TREE)))
5720 3 : return true;
5721 : }
5722 :
5723 18 : return false;
5724 : }
5725 :
5726 : /* Returns the defaulted constructor if T has one. Otherwise, returns
5727 : NULL_TREE. */
5728 :
5729 : tree
5730 62 : in_class_defaulted_default_constructor (tree t)
5731 : {
5732 62 : if (!TYPE_HAS_USER_CONSTRUCTOR (t))
5733 : return NULL_TREE;
5734 :
5735 123 : for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (t)); iter; ++iter)
5736 : {
5737 78 : tree fn = *iter;
5738 :
5739 156 : if (DECL_DEFAULTED_IN_CLASS_P (fn)
5740 114 : && default_ctor_p (fn))
5741 33 : return fn;
5742 : }
5743 :
5744 0 : return NULL_TREE;
5745 : }
5746 :
5747 : /* Returns true iff FN is a user-provided function, i.e. user-declared
5748 : and not explicitly defaulted or deleted on its first declaration. */
5749 :
5750 : bool
5751 111842233 : user_provided_p (tree fn)
5752 : {
5753 111842233 : fn = STRIP_TEMPLATE (fn);
5754 111842233 : return (!DECL_ARTIFICIAL (fn)
5755 102833086 : && !(DECL_INITIALIZED_IN_CLASS_P (fn)
5756 92918470 : && (DECL_DEFAULTED_FN (fn) || DECL_DELETED_FN (fn)))
5757 : /* At namespace scope,
5758 : void f () = delete;
5759 : is *not* user-provided (and any function deleted after its first
5760 : declaration is ill-formed). */
5761 196924563 : && !(DECL_NAMESPACE_SCOPE_P (fn) && DECL_DELETED_FN (fn)));
5762 : }
5763 :
5764 : /* Returns true iff class T has a user-provided constructor. */
5765 :
5766 : bool
5767 49909852 : type_has_user_provided_constructor (tree t)
5768 : {
5769 49909852 : if (!CLASS_TYPE_P (t))
5770 : return false;
5771 :
5772 49909794 : if (!TYPE_HAS_USER_CONSTRUCTOR (t))
5773 : return false;
5774 :
5775 9605377 : for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (t)); iter; ++iter)
5776 8023302 : if (user_provided_p (*iter))
5777 6441709 : return true;
5778 :
5779 418234 : return false;
5780 : }
5781 :
5782 : /* Returns true iff class T has a constructor that accepts a single argument
5783 : and does not have a single parameter of type reference to T.
5784 :
5785 : This does not exclude explicit constructors because they are still
5786 : considered for conversions within { } even though choosing one is
5787 : ill-formed. */
5788 :
5789 : bool
5790 1545910 : type_has_converting_constructor (tree t)
5791 : {
5792 1545910 : if (!CLASS_TYPE_P (t))
5793 : return false;
5794 :
5795 1545910 : if (!TYPE_HAS_USER_CONSTRUCTOR (t))
5796 : return false;
5797 :
5798 5020542 : for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (t)); iter; ++iter)
5799 : {
5800 2958674 : tree fn = *iter;
5801 2958674 : tree parm = FUNCTION_FIRST_USER_PARMTYPE (fn);
5802 2958674 : if (parm == NULL_TREE)
5803 : /* Varargs. */
5804 896806 : return true;
5805 3812458 : if (parm == void_list_node
5806 2958672 : || !sufficient_parms_p (TREE_CHAIN (parm)))
5807 : /* Can't accept a single argument, so won't be considered for
5808 : conversion. */
5809 853786 : continue;
5810 2104886 : if (TREE_CODE (fn) == TEMPLATE_DECL
5811 2104886 : || TREE_CHAIN (parm) != void_list_node)
5812 : /* Not a simple single parameter. */
5813 : return true;
5814 1603153 : if (TYPE_MAIN_VARIANT (non_reference (TREE_VALUE (parm)))
5815 1603153 : != DECL_CONTEXT (fn))
5816 : /* The single parameter has the wrong type. */
5817 : return true;
5818 1208082 : if (get_constraints (fn))
5819 : /* Constrained. */
5820 : return true;
5821 : }
5822 :
5823 317026 : return false;
5824 : }
5825 :
5826 : /* Returns true iff class T has a user-provided or explicit constructor. */
5827 :
5828 : bool
5829 50340823 : type_has_user_provided_or_explicit_constructor (tree t)
5830 : {
5831 50340823 : if (!CLASS_TYPE_P (t))
5832 : return false;
5833 :
5834 50340823 : if (!TYPE_HAS_USER_CONSTRUCTOR (t))
5835 : return false;
5836 :
5837 9479631 : for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (t)); iter; ++iter)
5838 : {
5839 8044136 : tree fn = *iter;
5840 8044136 : if (user_provided_p (fn) || DECL_NONCONVERTING_P (fn))
5841 6609124 : return true;
5842 : }
5843 :
5844 270178 : return false;
5845 : }
5846 :
5847 : /* Returns true iff class T has a non-user-provided (i.e. implicitly
5848 : declared or explicitly defaulted in the class body) default
5849 : constructor. */
5850 :
5851 : bool
5852 3216270 : type_has_non_user_provided_default_constructor (tree t)
5853 : {
5854 3216270 : if (!TYPE_HAS_DEFAULT_CONSTRUCTOR (t))
5855 : return false;
5856 3216244 : if (CLASSTYPE_LAZY_DEFAULT_CTOR (t))
5857 : return true;
5858 :
5859 23210522 : for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (t)); iter; ++iter)
5860 : {
5861 12239400 : tree fn = *iter;
5862 12239400 : if (TREE_CODE (fn) == FUNCTION_DECL
5863 8384957 : && default_ctor_p (fn)
5864 15453452 : && !user_provided_p (fn))
5865 1269467 : return true;
5866 : }
5867 :
5868 1945850 : return false;
5869 : }
5870 :
5871 : /* TYPE is being used as a virtual base, and has a non-trivial move
5872 : assignment. Return true if this is due to there being a user-provided
5873 : move assignment in TYPE or one of its subobjects; if there isn't, then
5874 : multiple move assignment can't cause any harm. */
5875 :
5876 : bool
5877 27 : vbase_has_user_provided_move_assign (tree type)
5878 : {
5879 : /* Does the type itself have a user-provided move assignment operator? */
5880 27 : if (!CLASSTYPE_LAZY_MOVE_ASSIGN (type))
5881 30 : for (ovl_iterator iter (get_class_binding_direct
5882 27 : (type, assign_op_identifier));
5883 57 : iter; ++iter)
5884 48 : if (user_provided_p (*iter) && move_fn_p (*iter))
5885 18 : return true;
5886 :
5887 : /* Do any of its bases? */
5888 9 : tree binfo = TYPE_BINFO (type);
5889 9 : tree base_binfo;
5890 12 : for (int i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
5891 6 : if (vbase_has_user_provided_move_assign (BINFO_TYPE (base_binfo)))
5892 : return true;
5893 :
5894 : /* Or non-static data members? */
5895 27 : for (tree field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
5896 : {
5897 21 : if (TREE_CODE (field) == FIELD_DECL
5898 3 : && CLASS_TYPE_P (TREE_TYPE (field))
5899 21 : && vbase_has_user_provided_move_assign (TREE_TYPE (field)))
5900 : return true;
5901 : }
5902 :
5903 : /* Seems not. */
5904 : return false;
5905 : }
5906 :
5907 : /* If default-initialization leaves part of TYPE uninitialized, returns
5908 : a DECL for the field or TYPE itself (DR 253). */
5909 :
5910 : tree
5911 772501 : default_init_uninitialized_part (tree type)
5912 : {
5913 772501 : tree t, r, binfo;
5914 772501 : int i;
5915 :
5916 772501 : type = strip_array_types (type);
5917 772501 : if (!CLASS_TYPE_P (type))
5918 : return type;
5919 766951 : if (!type_has_non_user_provided_default_constructor (type))
5920 : return NULL_TREE;
5921 709737 : for (binfo = TYPE_BINFO (type), i = 0;
5922 709737 : BINFO_BASE_ITERATE (binfo, i, t); ++i)
5923 : {
5924 137366 : r = default_init_uninitialized_part (BINFO_TYPE (t));
5925 137366 : if (r)
5926 : return r;
5927 : }
5928 710254 : for (t = next_aggregate_field (TYPE_FIELDS (type)); t;
5929 137883 : t = next_aggregate_field (DECL_CHAIN (t)))
5930 138114 : if (!DECL_INITIAL (t) && !DECL_ARTIFICIAL (t))
5931 : {
5932 304 : r = default_init_uninitialized_part (TREE_TYPE (t));
5933 304 : if (r)
5934 449 : return DECL_P (r) ? r : t;
5935 : }
5936 :
5937 : return NULL_TREE;
5938 : }
5939 :
5940 : /* Returns true iff for class T, a trivial synthesized default constructor
5941 : would be constexpr. */
5942 :
5943 : bool
5944 46462696 : trivial_default_constructor_is_constexpr (tree t)
5945 : {
5946 : /* A defaulted trivial default constructor is constexpr
5947 : if there is nothing to initialize. */
5948 46462696 : gcc_assert (!TYPE_HAS_COMPLEX_DFLT (t));
5949 : /* A class with a vptr doesn't have a trivial default ctor.
5950 : In C++20, a class can have transient uninitialized members, e.g.:
5951 :
5952 : struct S { int i; constexpr S() = default; };
5953 :
5954 : should work. */
5955 46462696 : return (cxx_dialect >= cxx20
5956 46462696 : || is_really_empty_class (t, /*ignore_vptr*/true));
5957 : }
5958 :
5959 : /* Returns true iff class T has a constexpr default constructor. */
5960 :
5961 : bool
5962 42596848 : type_has_constexpr_default_constructor (tree t)
5963 : {
5964 42596848 : tree fns;
5965 :
5966 42596848 : if (!CLASS_TYPE_P (t))
5967 : {
5968 : /* The caller should have stripped an enclosing array. */
5969 232 : gcc_assert (TREE_CODE (t) != ARRAY_TYPE);
5970 : return false;
5971 : }
5972 42596616 : if (CLASSTYPE_LAZY_DEFAULT_CTOR (t))
5973 : {
5974 42595127 : if (!TYPE_HAS_COMPLEX_DFLT (t))
5975 42595064 : return trivial_default_constructor_is_constexpr (t);
5976 : /* Non-trivial, we need to check subobject constructors. */
5977 63 : lazily_declare_fn (sfk_constructor, t);
5978 : }
5979 1552 : fns = locate_ctor (t);
5980 1552 : return (fns && DECL_DECLARED_CONSTEXPR_P (fns));
5981 : }
5982 :
5983 : /* Returns true iff class T has a constexpr default constructor or has an
5984 : implicitly declared default constructor that we can't tell if it's constexpr
5985 : without forcing a lazy declaration (which might cause undesired
5986 : instantiations). */
5987 :
5988 : static bool
5989 43011819 : type_maybe_constexpr_default_constructor (tree t)
5990 : {
5991 43011819 : if (CLASS_TYPE_P (t) && CLASSTYPE_LAZY_DEFAULT_CTOR (t)
5992 86023638 : && TYPE_HAS_COMPLEX_DFLT (t))
5993 : /* Assume it's constexpr. */
5994 : return true;
5995 42595064 : return type_has_constexpr_default_constructor (t);
5996 : }
5997 :
5998 : /* Returns true iff class T has a constexpr destructor. */
5999 :
6000 : bool
6001 370 : type_has_constexpr_destructor (tree t)
6002 : {
6003 370 : tree fns;
6004 :
6005 370 : if (CLASSTYPE_LAZY_DESTRUCTOR (t))
6006 : /* Non-trivial, we need to check subobject destructors. */
6007 13 : lazily_declare_fn (sfk_destructor, t);
6008 370 : fns = CLASSTYPE_DESTRUCTOR (t);
6009 370 : return (fns && DECL_DECLARED_CONSTEXPR_P (fns));
6010 : }
6011 :
6012 : /* Returns true iff class T has a constexpr destructor or has an
6013 : implicitly declared destructor that we can't tell if it's constexpr
6014 : without forcing a lazy declaration (which might cause undesired
6015 : instantiations). */
6016 :
6017 : static bool
6018 47615337 : type_maybe_constexpr_destructor (tree t)
6019 : {
6020 : /* Until C++20, only trivial destruction is constexpr. */
6021 47615337 : if (TYPE_HAS_TRIVIAL_DESTRUCTOR (t))
6022 : return true;
6023 1620839 : if (cxx_dialect < cxx20)
6024 : return false;
6025 1597265 : if (CLASS_TYPE_P (t) && CLASSTYPE_LAZY_DESTRUCTOR (t))
6026 : /* Assume it's constexpr. */
6027 : return true;
6028 1362245 : tree fn = CLASSTYPE_DESTRUCTOR (t);
6029 1362245 : return (fn && maybe_constexpr_fn (fn));
6030 : }
6031 :
6032 : /* Returns true iff class TYPE has a virtual destructor. */
6033 :
6034 : bool
6035 3448679 : type_has_virtual_destructor (tree type)
6036 : {
6037 3448679 : tree dtor;
6038 :
6039 3448679 : if (!NON_UNION_CLASS_TYPE_P (type))
6040 : return false;
6041 :
6042 3448547 : gcc_assert (COMPLETE_TYPE_P (type));
6043 3448547 : dtor = CLASSTYPE_DESTRUCTOR (type);
6044 3448547 : return (dtor && DECL_VIRTUAL_P (dtor));
6045 : }
6046 :
6047 : /* True iff class TYPE has a non-deleted trivial default
6048 : constructor. */
6049 :
6050 : bool
6051 19532 : type_has_non_deleted_trivial_default_ctor (tree type)
6052 : {
6053 19532 : return TYPE_HAS_TRIVIAL_DFLT (type) && locate_ctor (type);
6054 : }
6055 :
6056 : /* Returns true iff T, a class, has a move-assignment or
6057 : move-constructor. Does not lazily declare either.
6058 : If USER_P is false, any move function will do. If it is true, the
6059 : move function must be user-declared.
6060 :
6061 : Note that user-declared here is different from "user-provided",
6062 : which doesn't include functions that are defaulted in the
6063 : class. */
6064 :
6065 : bool
6066 52811945 : classtype_has_move_assign_or_move_ctor_p (tree t, bool user_p)
6067 : {
6068 52811945 : gcc_assert (user_p
6069 : || (!CLASSTYPE_LAZY_MOVE_CTOR (t)
6070 : && !CLASSTYPE_LAZY_MOVE_ASSIGN (t)));
6071 :
6072 52811945 : if (!CLASSTYPE_LAZY_MOVE_CTOR (t))
6073 62780163 : for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (t)); iter; ++iter)
6074 10512930 : if ((!user_p || !DECL_ARTIFICIAL (*iter)) && move_fn_p (*iter))
6075 1230327 : return true;
6076 :
6077 51581618 : if (!CLASSTYPE_LAZY_MOVE_ASSIGN (t))
6078 1069603 : for (ovl_iterator iter (get_class_binding_direct
6079 46239955 : (t, assign_op_identifier));
6080 47309558 : iter; ++iter)
6081 913086 : if ((!user_p || !DECL_ARTIFICIAL (*iter))
6082 347314 : && DECL_CONTEXT (*iter) == t
6083 1211727 : && move_fn_p (*iter))
6084 1666 : return true;
6085 :
6086 : return false;
6087 : }
6088 :
6089 : /* True iff T has a move constructor that is not deleted. */
6090 :
6091 : bool
6092 6 : classtype_has_non_deleted_move_ctor (tree t)
6093 : {
6094 6 : if (CLASSTYPE_LAZY_MOVE_CTOR (t))
6095 0 : lazily_declare_fn (sfk_move_constructor, t);
6096 6 : for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (t)); iter; ++iter)
6097 6 : if (move_fn_p (*iter) && !DECL_DELETED_FN (*iter))
6098 6 : return true;
6099 0 : return false;
6100 : }
6101 :
6102 : /* True iff T has a copy or move constructor that is not deleted. */
6103 :
6104 : bool
6105 99 : classtype_has_non_deleted_copy_or_move_ctor (tree t)
6106 : {
6107 99 : if (CLASSTYPE_LAZY_COPY_CTOR (t))
6108 81 : lazily_declare_fn (sfk_copy_constructor, t);
6109 99 : if (CLASSTYPE_LAZY_MOVE_CTOR (t))
6110 27 : lazily_declare_fn (sfk_move_constructor, t);
6111 132 : for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (t)); iter; ++iter)
6112 : {
6113 111 : tree fn = *iter;
6114 111 : if ((copy_fn_p (fn) || move_fn_p (fn)) && !DECL_DELETED_FN (fn))
6115 90 : return true;
6116 : }
6117 9 : return false;
6118 : }
6119 :
6120 : /* If T, a class, has a user-provided copy constructor, copy assignment
6121 : operator, or destructor, returns that function. Otherwise, null. */
6122 :
6123 : tree
6124 6553820 : classtype_has_depr_implicit_copy (tree t)
6125 : {
6126 6553820 : if (!CLASSTYPE_LAZY_COPY_CTOR (t))
6127 18001601 : for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (t)); iter; ++iter)
6128 : {
6129 8969067 : tree fn = *iter;
6130 8969067 : if (user_provided_p (fn) && copy_fn_p (fn))
6131 770 : return fn;
6132 : }
6133 :
6134 6553050 : if (!CLASSTYPE_LAZY_COPY_ASSIGN (t))
6135 755982 : for (ovl_iterator iter (get_class_binding_direct
6136 1441721 : (t, assign_op_identifier));
6137 2197703 : iter; ++iter)
6138 : {
6139 756381 : tree fn = *iter;
6140 756381 : if (DECL_CONTEXT (fn) == t
6141 756381 : && user_provided_p (fn) && copy_fn_p (fn))
6142 399 : return fn;
6143 : }
6144 :
6145 6552651 : if (!CLASSTYPE_LAZY_DESTRUCTOR (t))
6146 : {
6147 2043772 : tree fn = CLASSTYPE_DESTRUCTOR (t);
6148 2043772 : if (user_provided_p (fn))
6149 : return fn;
6150 : }
6151 :
6152 : return NULL_TREE;
6153 : }
6154 :
6155 : /* True iff T has a member or friend declaration of operator OP. */
6156 :
6157 : bool
6158 49904436 : classtype_has_op (tree t, tree_code op)
6159 : {
6160 49904436 : tree name = ovl_op_identifier (op);
6161 49904436 : if (get_class_binding (t, name))
6162 : return true;
6163 51851590 : for (tree f = DECL_FRIENDLIST (TYPE_MAIN_DECL (t)); f; f = TREE_CHAIN (f))
6164 3225671 : if (FRIEND_NAME (f) == name)
6165 : return true;
6166 : return false;
6167 : }
6168 :
6169 :
6170 : /* If T has a defaulted member or friend declaration of OP, return it. */
6171 :
6172 : tree
6173 48625919 : classtype_has_defaulted_op (tree t, tree_code op)
6174 : {
6175 48625919 : tree name = ovl_op_identifier (op);
6176 48625973 : for (ovl_iterator oi (get_class_binding (t, name)); oi; ++oi)
6177 : {
6178 645 : tree fn = *oi;
6179 645 : if (DECL_DEFAULTED_FN (fn))
6180 602 : return fn;
6181 : }
6182 49427257 : for (tree f = DECL_FRIENDLIST (TYPE_MAIN_DECL (t)); f; f = TREE_CHAIN (f))
6183 802013 : if (FRIEND_NAME (f) == name)
6184 1898 : for (tree l = FRIEND_DECLS (f); l; l = TREE_CHAIN (l))
6185 : {
6186 1054 : tree fn = TREE_VALUE (l);
6187 1054 : if (DECL_DEFAULTED_FN (fn))
6188 : return fn;
6189 : }
6190 : return NULL_TREE;
6191 : }
6192 :
6193 : /* Nonzero if we need to build up a constructor call when initializing an
6194 : object of this class, either because it has a user-declared constructor
6195 : or because it doesn't have a default constructor (so we need to give an
6196 : error if no initializer is provided). Use TYPE_NEEDS_CONSTRUCTING when
6197 : what you care about is whether or not an object can be produced by a
6198 : constructor (e.g. so we don't set TREE_READONLY on const variables of
6199 : such type); use this function when what you care about is whether or not
6200 : to try to call a constructor to create an object. The latter case is
6201 : the former plus some cases of constructors that cannot be called. */
6202 :
6203 : bool
6204 67369185 : type_build_ctor_call (tree t)
6205 : {
6206 67369185 : tree inner;
6207 67369185 : if (TYPE_NEEDS_CONSTRUCTING (t))
6208 : return true;
6209 60152389 : inner = strip_array_types (t);
6210 60152389 : if (!CLASS_TYPE_P (inner) || ANON_AGGR_TYPE_P (inner))
6211 : return false;
6212 5393566 : if (!TYPE_HAS_DEFAULT_CONSTRUCTOR (inner))
6213 : return true;
6214 4965850 : if (cxx_dialect < cxx11)
6215 : return false;
6216 : /* A user-declared constructor might be private, and a constructor might
6217 : be trivial but deleted. */
6218 19130823 : for (ovl_iterator iter (get_class_binding (inner, complete_ctor_identifier));
6219 19130823 : iter; ++iter)
6220 : {
6221 14637472 : tree fn = *iter;
6222 14637472 : if (!DECL_ARTIFICIAL (fn)
6223 14417719 : || TREE_DEPRECATED (fn)
6224 14302582 : || TREE_UNAVAILABLE (fn)
6225 28940054 : || DECL_DELETED_FN (fn))
6226 463234 : return true;
6227 : }
6228 4493351 : return false;
6229 : }
6230 :
6231 : /* Like type_build_ctor_call, but for destructors. */
6232 :
6233 : bool
6234 126449527 : type_build_dtor_call (tree t)
6235 : {
6236 126449527 : tree inner;
6237 126449527 : if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t))
6238 : return true;
6239 100180355 : inner = strip_array_types (t);
6240 43558970 : if (!CLASS_TYPE_P (inner) || ANON_AGGR_TYPE_P (inner)
6241 143567978 : || !COMPLETE_TYPE_P (inner))
6242 : return false;
6243 43387600 : if (cxx_dialect < cxx11)
6244 : return false;
6245 : /* A user-declared destructor might be private, and a destructor might
6246 : be trivial but deleted. */
6247 74713938 : for (ovl_iterator iter (get_class_binding (inner, complete_dtor_identifier));
6248 74713938 : iter; ++iter)
6249 : {
6250 42922874 : tree fn = *iter;
6251 42922874 : if (!DECL_ARTIFICIAL (fn)
6252 31366771 : || TREE_DEPRECATED (fn)
6253 31366771 : || TREE_UNAVAILABLE (fn)
6254 74289645 : || DECL_DELETED_FN (fn))
6255 11556151 : return true;
6256 : }
6257 31791064 : return false;
6258 : }
6259 :
6260 : /* Returns TRUE iff we need a cookie when dynamically allocating an
6261 : array whose elements have the indicated class TYPE. */
6262 :
6263 : static bool
6264 49904436 : type_requires_array_cookie (tree type)
6265 : {
6266 49904436 : tree fns;
6267 49904436 : bool has_two_argument_delete_p = false;
6268 :
6269 49904436 : gcc_assert (CLASS_TYPE_P (type));
6270 :
6271 : /* If there's a non-trivial destructor, we need a cookie. In order
6272 : to iterate through the array calling the destructor for each
6273 : element, we'll have to know how many elements there are. */
6274 49904436 : if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
6275 : return true;
6276 :
6277 : /* If the usual deallocation function is a two-argument whose second
6278 : argument is of type `size_t', then we have to pass the size of
6279 : the array to the deallocation function, so we will need to store
6280 : a cookie. */
6281 46242408 : fns = lookup_fnfields (TYPE_BINFO (type),
6282 : ovl_op_identifier (false, VEC_DELETE_EXPR),
6283 : /*protect=*/0, tf_warning_or_error);
6284 : /* If there are no `operator []' members, or the lookup is
6285 : ambiguous, then we don't need a cookie. */
6286 46242408 : if (!fns || fns == error_mark_node)
6287 : return false;
6288 : /* Loop through all of the functions. */
6289 239 : for (lkp_iterator iter (BASELINK_FUNCTIONS (fns)); iter; ++iter)
6290 : {
6291 226 : tree fn = *iter;
6292 :
6293 : /* See if this function is a one-argument delete function. If
6294 : it is, then it will be the usual deallocation function. */
6295 226 : tree second_parm = TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (fn)));
6296 226 : if (second_parm == void_list_node)
6297 102 : return false;
6298 : /* Do not consider this function if its second argument is an
6299 : ellipsis. */
6300 124 : if (!second_parm)
6301 3 : continue;
6302 : /* Otherwise, if we have a two-argument function and the second
6303 : argument is `size_t', it will be the usual deallocation
6304 : function -- unless there is one-argument function, too. */
6305 121 : if (TREE_CHAIN (second_parm) == void_list_node
6306 121 : && same_type_p (TREE_VALUE (second_parm), size_type_node))
6307 : has_two_argument_delete_p = true;
6308 : }
6309 :
6310 13 : return has_two_argument_delete_p;
6311 : }
6312 :
6313 : /* Finish computing the `literal type' property of class type T.
6314 :
6315 : At this point, we have already processed base classes and
6316 : non-static data members. We need to check whether the copy
6317 : constructor is trivial, the destructor is trivial, and there
6318 : is a trivial default constructor or at least one constexpr
6319 : constructor other than the copy constructor. */
6320 :
6321 : static void
6322 49904436 : finalize_literal_type_property (tree t)
6323 : {
6324 49904436 : tree fn;
6325 :
6326 49904436 : if (cxx_dialect < cxx11)
6327 124000 : CLASSTYPE_LITERAL_P (t) = false;
6328 49780436 : else if (CLASSTYPE_LITERAL_P (t)
6329 49780436 : && !type_maybe_constexpr_destructor (t))
6330 714870 : CLASSTYPE_LITERAL_P (t) = false;
6331 95552411 : else if (CLASSTYPE_LITERAL_P (t) && LAMBDA_TYPE_P (t))
6332 343202 : CLASSTYPE_LITERAL_P (t) = (cxx_dialect >= cxx17);
6333 95279612 : else if (CLASSTYPE_LITERAL_P (t) && !TYPE_HAS_TRIVIAL_DFLT (t)
6334 3753729 : && CLASSTYPE_NON_AGGREGATE (t)
6335 52323264 : && !TYPE_HAS_CONSTEXPR_CTOR (t))
6336 542800 : CLASSTYPE_LITERAL_P (t) = false;
6337 :
6338 : /* C++14 DR 1684 removed this restriction. */
6339 49904436 : if (cxx_dialect < cxx14
6340 50042275 : && !CLASSTYPE_LITERAL_P (t) && !LAMBDA_TYPE_P (t))
6341 1505704 : for (fn = TYPE_FIELDS (t); fn; fn = DECL_CHAIN (fn))
6342 1360721 : if (TREE_CODE (fn) == FUNCTION_DECL
6343 654414 : && DECL_DECLARED_CONSTEXPR_P (fn)
6344 2707 : && DECL_IOBJ_MEMBER_FUNCTION_P (fn)
6345 1362501 : && !DECL_CONSTRUCTOR_P (fn))
6346 : {
6347 23 : DECL_DECLARED_CONSTEXPR_P (fn) = false;
6348 23 : if (!DECL_GENERATED_P (fn))
6349 : {
6350 7 : auto_diagnostic_group d;
6351 7 : if (pedwarn (DECL_SOURCE_LOCATION (fn), OPT_Wpedantic,
6352 : "enclosing class of %<constexpr%> non-static "
6353 : "member function %q+#D is not a literal type", fn))
6354 5 : explain_non_literal_class (t);
6355 7 : }
6356 : }
6357 49904436 : }
6358 :
6359 : /* T is a non-literal type used in a context which requires a constant
6360 : expression. Explain why it isn't literal. */
6361 :
6362 : void
6363 140 : explain_non_literal_class (tree t)
6364 : {
6365 140 : static hash_set<tree> *diagnosed;
6366 :
6367 140 : if (!CLASS_TYPE_P (t))
6368 52 : return;
6369 137 : t = TYPE_MAIN_VARIANT (t);
6370 :
6371 137 : if (diagnosed == NULL)
6372 74 : diagnosed = new hash_set<tree>;
6373 137 : if (diagnosed->add (t))
6374 : /* Already explained. */
6375 : return;
6376 :
6377 101 : auto_diagnostic_group d;
6378 101 : inform (UNKNOWN_LOCATION, "%q+T is not literal because:", t);
6379 134 : if (cxx_dialect < cxx17 && LAMBDA_TYPE_P (t))
6380 2 : inform (UNKNOWN_LOCATION,
6381 : " %qT is a closure type, which is only literal in "
6382 : "C++17 and later", t);
6383 99 : else if (cxx_dialect < cxx20 && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t))
6384 5 : inform (UNKNOWN_LOCATION, " %q+T has a non-trivial destructor", t);
6385 94 : else if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t)
6386 94 : && !type_maybe_constexpr_destructor (t))
6387 17 : inform (UNKNOWN_LOCATION, " %q+T does not have %<constexpr%> destructor",
6388 : t);
6389 77 : else if (CLASSTYPE_NON_AGGREGATE (t)
6390 69 : && !TYPE_HAS_TRIVIAL_DFLT (t)
6391 135 : && !LAMBDA_TYPE_P (t)
6392 146 : && !TYPE_HAS_CONSTEXPR_CTOR (t))
6393 : {
6394 45 : inform (UNKNOWN_LOCATION,
6395 : " %q+T is not an aggregate, does not have a trivial "
6396 : "default constructor, and has no %<constexpr%> constructor that "
6397 : "is not a copy or move constructor", t);
6398 45 : if (type_has_non_user_provided_default_constructor (t))
6399 : /* Note that we can't simply call locate_ctor because when the
6400 : constructor is deleted it just returns NULL_TREE. */
6401 0 : for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (t)); iter; ++iter)
6402 : {
6403 0 : tree fn = *iter;
6404 0 : tree parms = TYPE_ARG_TYPES (TREE_TYPE (fn));
6405 :
6406 0 : parms = skip_artificial_parms_for (fn, parms);
6407 :
6408 0 : if (sufficient_parms_p (parms))
6409 : {
6410 0 : if (DECL_DELETED_FN (fn))
6411 0 : maybe_explain_implicit_delete (fn);
6412 : else
6413 0 : explain_invalid_constexpr_fn (fn);
6414 : break;
6415 : }
6416 : }
6417 : }
6418 : else
6419 : {
6420 32 : tree binfo, base_binfo, field; int i;
6421 32 : for (binfo = TYPE_BINFO (t), i = 0;
6422 32 : BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
6423 : {
6424 13 : tree basetype = TREE_TYPE (base_binfo);
6425 13 : if (!CLASSTYPE_LITERAL_P (basetype))
6426 : {
6427 13 : inform (UNKNOWN_LOCATION,
6428 : " base class %qT of %q+T is non-literal",
6429 : basetype, t);
6430 13 : explain_non_literal_class (basetype);
6431 13 : return;
6432 : }
6433 : }
6434 153 : for (field = TYPE_FIELDS (t); field; field = TREE_CHAIN (field))
6435 : {
6436 134 : tree ftype;
6437 134 : if (TREE_CODE (field) != FIELD_DECL)
6438 115 : continue;
6439 19 : ftype = TREE_TYPE (field);
6440 19 : if (!literal_type_p (ftype))
6441 : {
6442 6 : inform (DECL_SOURCE_LOCATION (field),
6443 : " non-static data member %qD has non-literal type",
6444 : field);
6445 6 : if (CLASS_TYPE_P (ftype))
6446 6 : explain_non_literal_class (ftype);
6447 : }
6448 19 : if (CP_TYPE_VOLATILE_P (ftype))
6449 13 : inform (DECL_SOURCE_LOCATION (field),
6450 : " non-static data member %qD has volatile type", field);
6451 : }
6452 : }
6453 101 : }
6454 :
6455 : /* Check the validity of the bases and members declared in T. Add any
6456 : implicitly-generated functions (like copy-constructors and
6457 : assignment operators). Compute various flag bits (like
6458 : CLASSTYPE_NON_LAYOUT_POD_T) for T. This routine works purely at the C++
6459 : level: i.e., independently of the ABI in use. */
6460 :
6461 : static void
6462 49904436 : check_bases_and_members (tree t)
6463 : {
6464 : /* Nonzero if the implicitly generated copy constructor should take
6465 : a non-const reference argument. */
6466 49904436 : int cant_have_const_ctor;
6467 : /* Nonzero if the implicitly generated assignment operator
6468 : should take a non-const reference argument. */
6469 49904436 : int no_const_asn_ref;
6470 49904436 : tree access_decls;
6471 49904436 : bool saved_complex_asn_ref;
6472 49904436 : bool saved_nontrivial_dtor;
6473 49904436 : tree fn;
6474 :
6475 : /* By default, we use const reference arguments and generate default
6476 : constructors. */
6477 49904436 : cant_have_const_ctor = 0;
6478 49904436 : no_const_asn_ref = 0;
6479 :
6480 : /* Check all the base-classes and set FMEM members to point to arrays
6481 : of potential interest. */
6482 49904436 : check_bases (t, &cant_have_const_ctor, &no_const_asn_ref);
6483 :
6484 : /* Deduce noexcept on destructor. This needs to happen after we've set
6485 : triviality flags appropriately for our bases, and before checking
6486 : overriden virtual functions via check_methods. */
6487 49904436 : if (cxx_dialect >= cxx11)
6488 49780436 : if (tree dtor = CLASSTYPE_DESTRUCTOR (t))
6489 7245712 : for (tree fn : ovl_range (dtor))
6490 3622856 : deduce_noexcept_on_destructor (fn);
6491 :
6492 : /* Check all the method declarations. */
6493 49904436 : check_methods (t);
6494 :
6495 : /* Save the initial values of these flags which only indicate whether
6496 : or not the class has user-provided functions. As we analyze the
6497 : bases and members we can set these flags for other reasons. */
6498 49904436 : saved_complex_asn_ref = TYPE_HAS_COMPLEX_COPY_ASSIGN (t);
6499 49904436 : saved_nontrivial_dtor = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t);
6500 :
6501 : /* Check all the data member declarations. We cannot call
6502 : check_field_decls until we have called check_bases check_methods,
6503 : as check_field_decls depends on TYPE_HAS_NONTRIVIAL_DESTRUCTOR
6504 : being set appropriately. */
6505 49904436 : check_field_decls (t, &access_decls,
6506 : &cant_have_const_ctor,
6507 : &no_const_asn_ref);
6508 :
6509 : /* A nearly-empty class has to be vptr-containing; a nearly empty
6510 : class contains just a vptr. */
6511 49904436 : if (!TYPE_CONTAINS_VPTR_P (t))
6512 48163367 : CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
6513 :
6514 : /* Do some bookkeeping that will guide the generation of implicitly
6515 : declared member functions. */
6516 51645505 : TYPE_HAS_COMPLEX_COPY_CTOR (t) |= TYPE_CONTAINS_VPTR_P (t);
6517 51645505 : TYPE_HAS_COMPLEX_MOVE_CTOR (t) |= TYPE_CONTAINS_VPTR_P (t);
6518 : /* We need to call a constructor for this class if it has a
6519 : user-provided constructor, or if the default constructor is going
6520 : to initialize the vptr. (This is not an if-and-only-if;
6521 : TYPE_NEEDS_CONSTRUCTING is set elsewhere if bases or members
6522 : themselves need constructing.) */
6523 49904436 : TYPE_NEEDS_CONSTRUCTING (t)
6524 49904436 : |= (type_has_user_provided_constructor (t) || TYPE_CONTAINS_VPTR_P (t));
6525 : /* [dcl.init.aggr]
6526 :
6527 : An aggregate is an array or a class with no user-provided
6528 : constructors ... and no virtual functions.
6529 :
6530 : Again, other conditions for being an aggregate are checked
6531 : elsewhere. */
6532 49904436 : CLASSTYPE_NON_AGGREGATE (t)
6533 49904436 : |= (type_has_user_provided_or_explicit_constructor (t)
6534 49904436 : || TYPE_POLYMORPHIC_P (t));
6535 : /* This is the C++98/03 definition of POD; it changed in C++0x, but we
6536 : retain the old definition internally for ABI reasons. */
6537 49904436 : CLASSTYPE_NON_LAYOUT_POD_P (t)
6538 49904436 : |= (CLASSTYPE_NON_AGGREGATE (t)
6539 49904436 : || saved_nontrivial_dtor || saved_complex_asn_ref);
6540 51645505 : CLASSTYPE_NON_STD_LAYOUT (t) |= TYPE_CONTAINS_VPTR_P (t);
6541 51645505 : TYPE_HAS_COMPLEX_COPY_ASSIGN (t) |= TYPE_CONTAINS_VPTR_P (t);
6542 51645505 : TYPE_HAS_COMPLEX_MOVE_ASSIGN (t) |= TYPE_CONTAINS_VPTR_P (t);
6543 51645505 : TYPE_HAS_COMPLEX_DFLT (t) |= TYPE_CONTAINS_VPTR_P (t);
6544 :
6545 : /* Is this class non-layout-POD because it wasn't an aggregate in C++98? */
6546 49904436 : if (CLASSTYPE_NON_POD_AGGREGATE (t))
6547 : {
6548 831555 : if (CLASSTYPE_NON_LAYOUT_POD_P (t))
6549 : /* It's non-POD for another reason. */
6550 762947 : CLASSTYPE_NON_POD_AGGREGATE (t) = false;
6551 68608 : else if (abi_version_at_least (17))
6552 68606 : CLASSTYPE_NON_LAYOUT_POD_P (t) = true;
6553 : }
6554 :
6555 : /* P1008: Prohibit aggregates with user-declared constructors. */
6556 49904436 : if (cxx_dialect >= cxx20 && TYPE_HAS_USER_CONSTRUCTOR (t))
6557 : {
6558 6750502 : CLASSTYPE_NON_AGGREGATE (t) = true;
6559 6750502 : if (!CLASSTYPE_NON_LAYOUT_POD_P (t))
6560 : {
6561 : /* c++/120012: The C++20 aggregate change affected layout. */
6562 4474 : if (!abi_version_at_least (21))
6563 6 : CLASSTYPE_NON_LAYOUT_POD_P (t) = true;
6564 9438 : if (abi_version_crosses (21))
6565 3984 : CLASSTYPE_NON_AGGREGATE_POD (t) = true;
6566 : }
6567 : }
6568 :
6569 : /* If the only explicitly declared default constructor is user-provided,
6570 : set TYPE_HAS_COMPLEX_DFLT. */
6571 49904436 : if (!TYPE_HAS_COMPLEX_DFLT (t)
6572 45339487 : && TYPE_HAS_DEFAULT_CONSTRUCTOR (t)
6573 51947614 : && !type_has_non_user_provided_default_constructor (t))
6574 1478726 : TYPE_HAS_COMPLEX_DFLT (t) = true;
6575 :
6576 : /* Warn if a public base of a polymorphic type has an accessible
6577 : non-virtual destructor. It is only now that we know the class is
6578 : polymorphic. Although a polymorphic base will have a already
6579 : been diagnosed during its definition, we warn on use too. */
6580 49904436 : if (TYPE_POLYMORPHIC_P (t) && warn_nonvdtor)
6581 : {
6582 84 : tree binfo = TYPE_BINFO (t);
6583 84 : vec<tree, va_gc> *accesses = BINFO_BASE_ACCESSES (binfo);
6584 84 : tree base_binfo;
6585 84 : unsigned i;
6586 :
6587 114 : for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
6588 : {
6589 30 : tree basetype = TREE_TYPE (base_binfo);
6590 :
6591 30 : if ((*accesses)[i] == access_public_node
6592 18 : && (TYPE_POLYMORPHIC_P (basetype) || warn_ecpp)
6593 45 : && accessible_nvdtor_p (basetype))
6594 9 : warning (OPT_Wnon_virtual_dtor,
6595 : "base class %q#T has accessible non-virtual destructor",
6596 : basetype);
6597 : }
6598 : }
6599 :
6600 : /* If the class has no user-declared constructor, but does have
6601 : non-static const or reference data members that can never be
6602 : initialized, issue a warning. */
6603 49904436 : if (warn_uninitialized
6604 : /* Classes with user-declared constructors are presumed to
6605 : initialize these members. */
6606 434019 : && !TYPE_HAS_USER_CONSTRUCTOR (t)
6607 : /* Aggregates can be initialized with brace-enclosed
6608 : initializers. */
6609 50270373 : && CLASSTYPE_NON_AGGREGATE (t))
6610 : {
6611 38925 : tree field;
6612 :
6613 243705 : for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
6614 : {
6615 204780 : tree type;
6616 :
6617 388296 : if (TREE_CODE (field) != FIELD_DECL
6618 204780 : || DECL_INITIAL (field) != NULL_TREE)
6619 183516 : continue;
6620 :
6621 21264 : type = TREE_TYPE (field);
6622 21264 : if (TYPE_REF_P (type))
6623 6 : warning_at (DECL_SOURCE_LOCATION (field),
6624 6 : OPT_Wuninitialized, "non-static reference %q#D "
6625 : "in class without a constructor", field);
6626 21258 : else if (CP_TYPE_CONST_P (type)
6627 21258 : && (!CLASS_TYPE_P (type)
6628 3 : || !TYPE_HAS_DEFAULT_CONSTRUCTOR (type)))
6629 9 : warning_at (DECL_SOURCE_LOCATION (field),
6630 9 : OPT_Wuninitialized, "non-static const member %q#D "
6631 : "in class without a constructor", field);
6632 : }
6633 : }
6634 :
6635 : /* Synthesize any needed methods. */
6636 49904436 : add_implicitly_declared_members (t, &access_decls,
6637 : cant_have_const_ctor,
6638 : no_const_asn_ref);
6639 :
6640 : /* Check defaulted declarations here so we have cant_have_const_ctor
6641 : and don't need to worry about clones. */
6642 324301643 : for (fn = TYPE_FIELDS (t); fn; fn = DECL_CHAIN (fn))
6643 163783012 : if (DECL_DECLARES_FUNCTION_P (fn)
6644 137432892 : && !DECL_ARTIFICIAL (fn)
6645 136395208 : && DECL_DEFAULTED_IN_CLASS_P (fn)
6646 : /* ...except handle comparisons later, in finish_struct_1. */
6647 280678911 : && special_function_p (fn) != sfk_comparison)
6648 : {
6649 6244740 : bool imp_const_p
6650 12489480 : = (DECL_CONSTRUCTOR_P (fn) ? !cant_have_const_ctor
6651 6244740 : : !no_const_asn_ref);
6652 6244818 : defaulted_late_check (fn, imp_const_p);
6653 : }
6654 :
6655 99384181 : if (LAMBDA_TYPE_P (t))
6656 : /* "This class type is not an aggregate." */
6657 343387 : CLASSTYPE_NON_AGGREGATE (t) = 1;
6658 :
6659 : /* Compute the 'literal type' property before we
6660 : do anything with non-static member functions. */
6661 49904436 : finalize_literal_type_property (t);
6662 :
6663 : /* Create the in-charge and not-in-charge variants of constructors
6664 : and destructors. */
6665 49904436 : clone_constructors_and_destructors (t);
6666 :
6667 : /* Process the using-declarations. */
6668 102032146 : for (; access_decls; access_decls = TREE_CHAIN (access_decls))
6669 2223274 : handle_using_decl (TREE_VALUE (access_decls), t);
6670 :
6671 : /* Figure out whether or not we will need a cookie when dynamically
6672 : allocating an array of this type. */
6673 49904436 : LANG_TYPE_CLASS_CHECK (t)->vec_new_uses_cookie
6674 49904436 : = type_requires_array_cookie (t);
6675 :
6676 : /* Classes marked hot or cold propagate the attribute to all members. We
6677 : may do this now that methods are declared. This does miss some lazily
6678 : declared special member functions (CLASSTYPE_LAZY_*), which are handled
6679 : in lazily_declare_fn later on. */
6680 49904436 : propagate_class_warmth_attribute (t);
6681 49904436 : }
6682 :
6683 : /* If T needs a pointer to its virtual function table, set TYPE_VFIELD
6684 : accordingly. If a new vfield was created (because T doesn't have a
6685 : primary base class), then the newly created field is returned. It
6686 : is not added to the TYPE_FIELDS list; it is the caller's
6687 : responsibility to do that. Accumulate declared virtual functions
6688 : on VIRTUALS_P. */
6689 :
6690 : static tree
6691 49904436 : create_vtable_ptr (tree t, tree* virtuals_p)
6692 : {
6693 49904436 : tree fn;
6694 :
6695 : /* Collect the virtual functions declared in T. */
6696 389293516 : for (fn = TYPE_FIELDS (t); fn; fn = DECL_CHAIN (fn))
6697 339389080 : if (TREE_CODE (fn) == FUNCTION_DECL
6698 158042663 : && DECL_VINDEX (fn) && !DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (fn)
6699 347284972 : && TREE_CODE (DECL_VINDEX (fn)) != INTEGER_CST)
6700 : {
6701 7895892 : tree new_virtual = make_node (TREE_LIST);
6702 :
6703 7895892 : BV_FN (new_virtual) = fn;
6704 7895892 : BV_DELTA (new_virtual) = integer_zero_node;
6705 7895892 : BV_VCALL_INDEX (new_virtual) = NULL_TREE;
6706 :
6707 7895892 : TREE_CHAIN (new_virtual) = *virtuals_p;
6708 7895892 : *virtuals_p = new_virtual;
6709 : }
6710 :
6711 : /* If we couldn't find an appropriate base class, create a new field
6712 : here. Even if there weren't any new virtual functions, we might need a
6713 : new virtual function table if we're supposed to include vptrs in
6714 : all classes that need them. */
6715 49904436 : if (!TYPE_VFIELD (t) && (*virtuals_p || TYPE_CONTAINS_VPTR_P (t)))
6716 : {
6717 : /* We build this decl with vtbl_ptr_type_node, which is a
6718 : `vtable_entry_type*'. It might seem more precise to use
6719 : `vtable_entry_type (*)[N]' where N is the number of virtual
6720 : functions. However, that would require the vtable pointer in
6721 : base classes to have a different type than the vtable pointer
6722 : in derived classes. We could make that happen, but that
6723 : still wouldn't solve all the problems. In particular, the
6724 : type-based alias analysis code would decide that assignments
6725 : to the base class vtable pointer can't alias assignments to
6726 : the derived class vtable pointer, since they have different
6727 : types. Thus, in a derived class destructor, where the base
6728 : class constructor was inlined, we could generate bad code for
6729 : setting up the vtable pointer.
6730 :
6731 : Therefore, we use one type for all vtable pointers. We still
6732 : use a type-correct type; it's just doesn't indicate the array
6733 : bounds. That's better than using `void*' or some such; it's
6734 : cleaner, and it let's the alias analysis code know that these
6735 : stores cannot alias stores to void*! */
6736 249354 : tree field;
6737 :
6738 249354 : field = build_decl (input_location,
6739 : FIELD_DECL, get_vfield_name (t), vtbl_ptr_type_node);
6740 249354 : DECL_VIRTUAL_P (field) = 1;
6741 249354 : DECL_ARTIFICIAL (field) = 1;
6742 249354 : DECL_FIELD_CONTEXT (field) = t;
6743 249354 : DECL_FCONTEXT (field) = t;
6744 249354 : if (TYPE_PACKED (t))
6745 3 : DECL_PACKED (field) = 1;
6746 :
6747 249354 : TYPE_VFIELD (t) = field;
6748 :
6749 : /* This class is non-empty. */
6750 249354 : CLASSTYPE_EMPTY_P (t) = 0;
6751 :
6752 249354 : return field;
6753 : }
6754 :
6755 : return NULL_TREE;
6756 : }
6757 :
6758 : /* Add OFFSET to all base types of BINFO which is a base in the
6759 : hierarchy dominated by T.
6760 :
6761 : OFFSET, which is a type offset, is number of bytes. */
6762 :
6763 : static void
6764 27440999 : propagate_binfo_offsets (tree binfo, tree offset)
6765 : {
6766 27440999 : int i;
6767 27440999 : tree primary_binfo;
6768 27440999 : tree base_binfo;
6769 :
6770 : /* Update BINFO's offset. */
6771 27440999 : BINFO_OFFSET (binfo)
6772 27440999 : = fold_convert (sizetype,
6773 : size_binop (PLUS_EXPR,
6774 : fold_convert (ssizetype, BINFO_OFFSET (binfo)),
6775 : offset));
6776 :
6777 : /* Find the primary base class. */
6778 27440999 : primary_binfo = get_primary_binfo (binfo);
6779 :
6780 28550216 : if (primary_binfo && BINFO_INHERITANCE_CHAIN (primary_binfo) == binfo)
6781 1108217 : propagate_binfo_offsets (primary_binfo, offset);
6782 :
6783 : /* Scan all of the bases, pushing the BINFO_OFFSET adjust
6784 : downwards. */
6785 31268635 : for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
6786 : {
6787 : /* Don't do the primary base twice. */
6788 4936769 : if (base_binfo == primary_binfo)
6789 1109133 : continue;
6790 :
6791 2718503 : if (BINFO_VIRTUAL_P (base_binfo))
6792 215576 : continue;
6793 :
6794 2502927 : propagate_binfo_offsets (base_binfo, offset);
6795 : }
6796 27440999 : }
6797 :
6798 : /* Set BINFO_OFFSET for all of the virtual bases for RLI->T. Update
6799 : TYPE_ALIGN and TYPE_SIZE for T. OFFSETS gives the location of
6800 : empty subobjects of T. */
6801 :
6802 : static void
6803 49904436 : layout_virtual_bases (record_layout_info rli, splay_tree offsets)
6804 : {
6805 49904436 : tree vbase;
6806 49904436 : tree t = rli->t;
6807 49904436 : tree *next_field;
6808 :
6809 49904436 : if (BINFO_N_BASE_BINFOS (TYPE_BINFO (t)) == 0)
6810 : return;
6811 :
6812 : /* Find the last field. The artificial fields created for virtual
6813 : bases will go after the last extant field to date. */
6814 23118720 : next_field = &TYPE_FIELDS (t);
6815 176672061 : while (*next_field)
6816 153553341 : next_field = &DECL_CHAIN (*next_field);
6817 :
6818 : /* Go through the virtual bases, allocating space for each virtual
6819 : base that is not already a primary base class. These are
6820 : allocated in inheritance graph order. */
6821 73669399 : for (vbase = TYPE_BINFO (t); vbase; vbase = TREE_CHAIN (vbase))
6822 : {
6823 50550679 : if (!BINFO_VIRTUAL_P (vbase))
6824 50355995 : continue;
6825 :
6826 194684 : if (!BINFO_PRIMARY_P (vbase))
6827 : {
6828 : /* This virtual base is not a primary base of any class in the
6829 : hierarchy, so we have to add space for it. */
6830 186617 : tree access = access_private_node;
6831 186617 : if (publicly_virtually_derived_p (BINFO_TYPE (vbase), t))
6832 185734 : access = access_public_node;
6833 186617 : next_field = build_base_field (rli, vbase, access, offsets,
6834 : next_field);
6835 : }
6836 : }
6837 : }
6838 :
6839 : /* Returns the offset of the byte just past the end of the base class
6840 : BINFO. */
6841 :
6842 : static tree
6843 47466607 : end_of_base (tree binfo)
6844 : {
6845 47466607 : tree size;
6846 :
6847 47466607 : if (!CLASSTYPE_AS_BASE (BINFO_TYPE (binfo)))
6848 0 : size = TYPE_SIZE_UNIT (char_type_node);
6849 47466607 : else if (is_empty_class (BINFO_TYPE (binfo)))
6850 : /* An empty class has zero CLASSTYPE_SIZE_UNIT, but we need to
6851 : allocate some space for it. It cannot have virtual bases, so
6852 : TYPE_SIZE_UNIT is fine. */
6853 42495840 : size = TYPE_SIZE_UNIT (BINFO_TYPE (binfo));
6854 : else
6855 4970767 : size = CLASSTYPE_SIZE_UNIT (BINFO_TYPE (binfo));
6856 :
6857 47466607 : return size_binop (PLUS_EXPR, BINFO_OFFSET (binfo), size);
6858 : }
6859 :
6860 : /* Returns one of three variations of the ending offset of T. If MODE is
6861 : eoc_nvsize, the result is the ABI "nvsize" (i.e. sizeof before allocating
6862 : vbases). If MODE is eoc_vsize, the result is the sizeof after allocating
6863 : vbases but before rounding, which is not named in the ABI. If MODE is
6864 : eoc_nv_or_dsize, the result is the greater of "nvsize" and "dsize" (the size
6865 : of the actual data in the class, kinda), as used for allocation of
6866 : potentially-overlapping fields. */
6867 :
6868 : enum eoc_mode { eoc_nvsize, eoc_vsize, eoc_nv_or_dsize };
6869 : static tree
6870 98013368 : end_of_class (tree t, eoc_mode mode)
6871 : {
6872 98013368 : tree result = size_zero_node;
6873 98013368 : vec<tree, va_gc> *vbases;
6874 98013368 : tree binfo;
6875 98013368 : tree base_binfo;
6876 98013368 : tree offset;
6877 98013368 : int i;
6878 :
6879 98013368 : for (binfo = TYPE_BINFO (t), i = 0;
6880 145375770 : BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
6881 : {
6882 47362402 : if (BINFO_VIRTUAL_P (base_binfo)
6883 47362402 : && (!BINFO_PRIMARY_P (base_binfo)
6884 3390 : || BINFO_INHERITANCE_CHAIN (base_binfo) != TYPE_BINFO (t)))
6885 90479 : continue;
6886 :
6887 47271923 : offset = end_of_base (base_binfo);
6888 47271923 : if (tree_int_cst_lt (result, offset))
6889 47362402 : result = offset;
6890 : }
6891 :
6892 815341868 : for (tree field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
6893 717328500 : if (TREE_CODE (field) == FIELD_DECL
6894 717328500 : && !DECL_FIELD_IS_BASE (field))
6895 : {
6896 28326928 : tree size = DECL_SIZE_UNIT (field);
6897 28326928 : if (!size)
6898 : /* DECL_SIZE_UNIT can be null for a flexible array. */
6899 1995 : continue;
6900 :
6901 28324933 : if (is_empty_field (field))
6902 : /* For empty fields DECL_SIZE_UNIT is 0, but we want the
6903 : size of the type (usually 1) for computing nvsize. */
6904 451334 : size = TYPE_SIZE_UNIT (TREE_TYPE (field));
6905 :
6906 28324933 : if (DECL_BIT_FIELD_TYPE (field))
6907 : {
6908 909557 : offset = size_binop (PLUS_EXPR, bit_position (field),
6909 : DECL_SIZE (field));
6910 909557 : offset = size_binop (CEIL_DIV_EXPR, offset, bitsize_unit_node);
6911 909557 : offset = fold_convert (sizetype, offset);
6912 : }
6913 : else
6914 27415376 : offset = size_binop (PLUS_EXPR, byte_position (field), size);
6915 28324933 : if (tree_int_cst_lt (result, offset))
6916 717328500 : result = offset;
6917 : }
6918 :
6919 98013368 : if (mode != eoc_nvsize)
6920 50129762 : for (vbases = CLASSTYPE_VBASECLASSES (t), i = 0;
6921 50129762 : vec_safe_iterate (vbases, i, &base_binfo); i++)
6922 : {
6923 194693 : if (mode == eoc_nv_or_dsize)
6924 : /* For dsize, don't count trailing empty bases. */
6925 9 : offset = size_binop (PLUS_EXPR, BINFO_OFFSET (base_binfo),
6926 : CLASSTYPE_SIZE_UNIT (BINFO_TYPE (base_binfo)));
6927 : else
6928 194684 : offset = end_of_base (base_binfo);
6929 194693 : if (tree_int_cst_lt (result, offset))
6930 184272 : result = offset;
6931 : }
6932 :
6933 98013368 : return result;
6934 : }
6935 :
6936 : /* Warn as appropriate about the change in whether we pack into the tail
6937 : padding of FIELD, a base field which has a C++14 aggregate type with default
6938 : member initializers. */
6939 :
6940 : static void
6941 363196668 : check_non_pod_aggregate (tree field)
6942 : {
6943 1089421521 : if ((!abi_version_crosses (17) || cxx_dialect < cxx14)
6944 744944668 : && (!abi_version_crosses (21) || cxx_dialect < cxx20))
6945 : return;
6946 342431215 : if (TREE_CODE (field) != FIELD_DECL
6947 342431215 : || (!DECL_FIELD_IS_BASE (field)
6948 15697835 : && !field_poverlapping_p (field)))
6949 319779818 : return;
6950 22651397 : tree next = DECL_CHAIN (field);
6951 113392076 : while (next && TREE_CODE (next) != FIELD_DECL) next = DECL_CHAIN (next);
6952 22651397 : if (!next)
6953 : return;
6954 1682655 : tree type = TREE_TYPE (field);
6955 1682655 : if (TYPE_IDENTIFIER (type) == as_base_identifier)
6956 917193 : type = TYPE_CONTEXT (type);
6957 1661999 : if (!CLASS_TYPE_P (type)
6958 1661998 : || is_empty_class (type)
6959 2545807 : || (!CLASSTYPE_NON_POD_AGGREGATE (type)
6960 861598 : && !CLASSTYPE_NON_AGGREGATE_POD (type)))
6961 1681093 : return;
6962 1578 : tree size = end_of_class (type, (DECL_FIELD_IS_BASE (field)
6963 : ? eoc_nvsize : eoc_nv_or_dsize));
6964 1562 : tree rounded = round_up_loc (input_location, size, DECL_ALIGN_UNIT (next));
6965 1562 : if (tree_int_cst_lt (rounded, TYPE_SIZE_UNIT (type)))
6966 : {
6967 30 : location_t loc = DECL_SOURCE_LOCATION (next);
6968 30 : if (DECL_FIELD_IS_BASE (next))
6969 : {
6970 9 : if (abi_version_crosses (17)
6971 6 : && CLASSTYPE_NON_POD_AGGREGATE (type))
6972 6 : warning_at (loc, OPT_Wabi,"offset of %qT base class for "
6973 : "%<-std=c++14%> and up changes in "
6974 6 : "%<-fabi-version=17%> (GCC 12)", TREE_TYPE (next));
6975 0 : else if (abi_version_crosses (21)
6976 0 : && CLASSTYPE_NON_AGGREGATE_POD (type))
6977 0 : warning_at (loc, OPT_Wabi,"offset of %qT base class for "
6978 : "%<-std=c++20%> and up changes in "
6979 0 : "%<-fabi-version=21%> (GCC 16)", TREE_TYPE (next));
6980 : }
6981 : else
6982 : {
6983 45 : if (abi_version_crosses (17)
6984 24 : && CLASSTYPE_NON_POD_AGGREGATE (type))
6985 12 : warning_at (loc, OPT_Wabi, "offset of %qD for "
6986 : "%<-std=c++14%> and up changes in "
6987 : "%<-fabi-version=17%> (GCC 12)", next);
6988 22 : else if (abi_version_crosses (21)
6989 12 : && CLASSTYPE_NON_AGGREGATE_POD (type))
6990 8 : warning_at (loc, OPT_Wabi, "offset of %qD for "
6991 : "%<-std=c++20%> and up changes in "
6992 : "%<-fabi-version=21%> (GCC 16)", next);
6993 : }
6994 : }
6995 : }
6996 :
6997 : /* Warn about bases of T that are inaccessible because they are
6998 : ambiguous. For example:
6999 :
7000 : struct S {};
7001 : struct T : public S {};
7002 : struct U : public S, public T {};
7003 :
7004 : Here, `(S*) new U' is not allowed because there are two `S'
7005 : subobjects of U. */
7006 :
7007 : static void
7008 49904436 : maybe_warn_about_inaccessible_bases (tree t)
7009 : {
7010 49904436 : int i;
7011 49904436 : vec<tree, va_gc> *vbases;
7012 49904436 : tree basetype;
7013 49904436 : tree binfo;
7014 49904436 : tree base_binfo;
7015 :
7016 : /* If not checking for warning then return early. */
7017 49904436 : if (!warn_inaccessible_base)
7018 49904436 : return;
7019 :
7020 : /* If there are no repeated bases, nothing can be ambiguous. */
7021 49904280 : if (!CLASSTYPE_REPEATED_BASE_P (t))
7022 : return;
7023 :
7024 : /* Check direct bases. */
7025 3821 : for (binfo = TYPE_BINFO (t), i = 0;
7026 3821 : BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
7027 : {
7028 2764 : basetype = BINFO_TYPE (base_binfo);
7029 :
7030 2764 : if (!uniquely_derived_from_p (basetype, t))
7031 787 : warning (OPT_Winaccessible_base, "direct base %qT inaccessible "
7032 : "in %qT due to ambiguity", basetype, t);
7033 : }
7034 :
7035 : /* Check for ambiguous virtual bases. */
7036 1057 : if (extra_warnings)
7037 6 : for (vbases = CLASSTYPE_VBASECLASSES (t), i = 0;
7038 6 : vec_safe_iterate (vbases, i, &binfo); i++)
7039 : {
7040 3 : basetype = BINFO_TYPE (binfo);
7041 :
7042 3 : if (!uniquely_derived_from_p (basetype, t))
7043 3 : warning (OPT_Winaccessible_base, "virtual base %qT inaccessible in "
7044 : "%qT due to ambiguity", basetype, t);
7045 : }
7046 : }
7047 :
7048 : /* Compare two INTEGER_CSTs K1 and K2. */
7049 :
7050 : static int
7051 9749824 : splay_tree_compare_integer_csts (splay_tree_key k1, splay_tree_key k2)
7052 : {
7053 9749824 : return tree_int_cst_compare ((tree) k1, (tree) k2);
7054 : }
7055 :
7056 : /* Increase the size indicated in RLI to account for empty classes
7057 : that are "off the end" of the class. */
7058 :
7059 : static void
7060 49904436 : include_empty_classes (record_layout_info rli)
7061 : {
7062 49904436 : tree eoc;
7063 49904436 : tree rli_size;
7064 :
7065 : /* It might be the case that we grew the class to allocate a
7066 : zero-sized base class. That won't be reflected in RLI, yet,
7067 : because we are willing to overlay multiple bases at the same
7068 : offset. However, now we need to make sure that RLI is big enough
7069 : to reflect the entire class. */
7070 49904436 : eoc = end_of_class (rli->t, eoc_vsize);
7071 49904436 : rli_size = rli_size_unit_so_far (rli);
7072 49904436 : if (TREE_CODE (rli_size) == INTEGER_CST
7073 49904436 : && tree_int_cst_lt (rli_size, eoc))
7074 : {
7075 : /* The size should have been rounded to a whole byte. */
7076 20509658 : gcc_assert (tree_int_cst_equal
7077 : (rli->bitpos, round_down (rli->bitpos, BITS_PER_UNIT)));
7078 20509658 : rli->bitpos
7079 20509658 : = size_binop (PLUS_EXPR,
7080 : rli->bitpos,
7081 : size_binop (MULT_EXPR,
7082 : fold_convert (bitsizetype,
7083 : size_binop (MINUS_EXPR,
7084 : eoc, rli_size)),
7085 : bitsize_int (BITS_PER_UNIT)));
7086 20509658 : normalize_rli (rli);
7087 : }
7088 49904436 : }
7089 :
7090 : /* Calculate the TYPE_SIZE, TYPE_ALIGN, etc for T. Calculate
7091 : BINFO_OFFSETs for all of the base-classes. Position the vtable
7092 : pointer. Accumulate declared virtual functions on VIRTUALS_P. */
7093 :
7094 : static void
7095 49904436 : layout_class_type (tree t, tree *virtuals_p)
7096 : {
7097 49904436 : tree non_static_data_members;
7098 49904436 : tree field;
7099 49904436 : tree vptr;
7100 49904436 : record_layout_info rli;
7101 : /* Maps offsets (represented as INTEGER_CSTs) to a TREE_LIST of
7102 : types that appear at that offset. */
7103 49904436 : splay_tree empty_base_offsets;
7104 : /* True if the last field laid out was a bit-field. */
7105 49904436 : bool last_field_was_bitfield = false;
7106 : /* The location at which the next field should be inserted. */
7107 49904436 : tree *next_field;
7108 :
7109 : /* Keep track of the first non-static data member. */
7110 49904436 : non_static_data_members = TYPE_FIELDS (t);
7111 :
7112 : /* Start laying out the record. */
7113 49904436 : rli = start_record_layout (t);
7114 :
7115 : /* Mark all the primary bases in the hierarchy. */
7116 49904436 : determine_primary_bases (t);
7117 :
7118 : /* Create a pointer to our virtual function table. */
7119 49904436 : vptr = create_vtable_ptr (t, virtuals_p);
7120 :
7121 : /* The vptr is always the first thing in the class. */
7122 49904436 : if (vptr)
7123 : {
7124 249354 : DECL_CHAIN (vptr) = TYPE_FIELDS (t);
7125 249354 : TYPE_FIELDS (t) = vptr;
7126 249354 : next_field = &DECL_CHAIN (vptr);
7127 249354 : place_field (rli, vptr);
7128 : }
7129 : else
7130 49655082 : next_field = &TYPE_FIELDS (t);
7131 :
7132 : /* Build FIELD_DECLs for all of the non-virtual base-types. */
7133 49904436 : empty_base_offsets = splay_tree_new (splay_tree_compare_integer_csts,
7134 : NULL, NULL);
7135 49904436 : build_base_fields (rli, empty_base_offsets, next_field);
7136 :
7137 : /* Layout the non-static data members. */
7138 389293516 : for (field = non_static_data_members; field; field = DECL_CHAIN (field))
7139 : {
7140 339389080 : tree type;
7141 339389080 : tree padding;
7142 :
7143 : /* We still pass things that aren't non-static data members to
7144 : the back end, in case it wants to do something with them. */
7145 339389080 : if (TREE_CODE (field) != FIELD_DECL)
7146 : {
7147 322141958 : place_field (rli, field);
7148 : /* If the static data member has incomplete type, keep track
7149 : of it so that it can be completed later. (The handling
7150 : of pending statics in finish_record_layout is
7151 : insufficient; consider:
7152 :
7153 : struct S1;
7154 : struct S2 { static S1 s1; };
7155 :
7156 : At this point, finish_record_layout will be called, but
7157 : S1 is still incomplete.) */
7158 322141958 : if (VAR_P (field))
7159 : {
7160 13752676 : maybe_register_incomplete_var (field);
7161 : /* The visibility of static data members is determined
7162 : at their point of declaration, not their point of
7163 : definition. */
7164 13752676 : determine_visibility (field);
7165 : }
7166 322141958 : continue;
7167 : }
7168 :
7169 17247122 : type = TREE_TYPE (field);
7170 17247122 : if (type == error_mark_node)
7171 165 : continue;
7172 :
7173 17246957 : padding = NULL_TREE;
7174 :
7175 17246957 : bool might_overlap = field_poverlapping_p (field);
7176 :
7177 279488 : if (might_overlap && CLASS_TYPE_P (type)
7178 17503100 : && (CLASSTYPE_NON_LAYOUT_POD_P (type) || CLASSTYPE_EMPTY_P (type)))
7179 : {
7180 : /* if D is a potentially-overlapping data member, update sizeof(C) to
7181 : max (sizeof(C), offset(D)+max (nvsize(D), dsize(D))). */
7182 256010 : if (CLASSTYPE_EMPTY_P (type))
7183 225393 : DECL_SIZE (field) = DECL_SIZE_UNIT (field) = size_zero_node;
7184 : else
7185 : {
7186 30617 : tree size = end_of_class (type, eoc_nv_or_dsize);
7187 30617 : DECL_SIZE_UNIT (field) = size;
7188 30617 : DECL_SIZE (field) = bit_from_pos (size, bitsize_zero_node);
7189 : }
7190 : }
7191 :
7192 : /* If this field is a bit-field whose width is greater than its
7193 : type, then there are some special rules for allocating
7194 : it. */
7195 17246957 : if (DECL_C_BIT_FIELD (field)
7196 17246957 : && tree_int_cst_lt (TYPE_SIZE (type), DECL_SIZE (field)))
7197 : {
7198 774 : bool was_unnamed_p = false;
7199 : /* We must allocate the bits as if suitably aligned for the
7200 : longest integer type that fits in this many bits. Then,
7201 : we are supposed to use the left over bits as additional
7202 : padding. */
7203 :
7204 : /* Do not pick a type bigger than MAX_FIXED_MODE_SIZE. */
7205 1548 : tree limit = size_int (MAX_FIXED_MODE_SIZE);
7206 774 : if (tree_int_cst_lt (DECL_SIZE (field), limit))
7207 753 : limit = DECL_SIZE (field);
7208 :
7209 774 : tree integer_type = integer_types[itk_char];
7210 6620 : for (unsigned itk = itk_char; itk != itk_none; itk++)
7211 6599 : if (tree next = integer_types[itk])
7212 : {
7213 6473 : if (tree_int_cst_lt (limit, TYPE_SIZE (next)))
7214 : /* Too big, so our current guess is what we want. */
7215 : break;
7216 : /* Not bigger than limit, ok */
7217 : integer_type = next;
7218 : }
7219 :
7220 : /* Figure out how much additional padding is required. */
7221 774 : if (TREE_CODE (t) == UNION_TYPE)
7222 : /* In a union, the padding field must have the full width
7223 : of the bit-field; all fields start at offset zero. */
7224 77 : padding = DECL_SIZE (field);
7225 : else
7226 697 : padding = size_binop (MINUS_EXPR, DECL_SIZE (field),
7227 : TYPE_SIZE (integer_type));
7228 :
7229 774 : if (integer_zerop (padding))
7230 6 : padding = NULL_TREE;
7231 :
7232 : /* An unnamed bitfield does not normally affect the
7233 : alignment of the containing class on a target where
7234 : PCC_BITFIELD_TYPE_MATTERS. But, the C++ ABI does not
7235 : make any exceptions for unnamed bitfields when the
7236 : bitfields are longer than their types. Therefore, we
7237 : temporarily give the field a name. */
7238 774 : if (PCC_BITFIELD_TYPE_MATTERS && !DECL_NAME (field))
7239 : {
7240 0 : was_unnamed_p = true;
7241 0 : DECL_NAME (field) = make_anon_name ();
7242 : }
7243 :
7244 774 : DECL_SIZE (field) = TYPE_SIZE (integer_type);
7245 774 : SET_DECL_ALIGN (field, TYPE_ALIGN (integer_type));
7246 774 : DECL_USER_ALIGN (field) = TYPE_USER_ALIGN (integer_type);
7247 774 : layout_nonempty_base_or_field (rli, field, NULL_TREE,
7248 : empty_base_offsets);
7249 774 : if (was_unnamed_p)
7250 0 : DECL_NAME (field) = NULL_TREE;
7251 : /* Now that layout has been performed, set the size of the
7252 : field to the size of its declared type; the rest of the
7253 : field is effectively invisible. */
7254 774 : DECL_SIZE (field) = TYPE_SIZE (type);
7255 : /* We must also reset the DECL_MODE of the field. */
7256 774 : SET_DECL_MODE (field, TYPE_MODE (type));
7257 : }
7258 17246183 : else if (might_overlap && is_empty_class (type))
7259 : {
7260 225393 : SET_DECL_FIELD_ABI_IGNORED (field, 1);
7261 225393 : layout_empty_base_or_field (rli, field, empty_base_offsets);
7262 : }
7263 : else
7264 17020790 : layout_nonempty_base_or_field (rli, field, NULL_TREE,
7265 : empty_base_offsets);
7266 :
7267 : /* Remember the location of any empty classes in FIELD. */
7268 17246957 : record_subobject_offsets (field, empty_base_offsets);
7269 :
7270 : /* If a bit-field does not immediately follow another bit-field,
7271 : and yet it starts in the middle of a byte, we have failed to
7272 : comply with the ABI. */
7273 17246957 : if (warn_abi
7274 15634361 : && DECL_C_BIT_FIELD (field)
7275 : /* The TREE_NO_WARNING flag gets set by Objective-C when
7276 : laying out an Objective-C class. The ObjC ABI differs
7277 : from the C++ ABI, and so we do not want a warning
7278 : here. */
7279 593033 : && !warning_suppressed_p (field, OPT_Wabi)
7280 593033 : && !last_field_was_bitfield
7281 17372475 : && !integer_zerop (size_binop (TRUNC_MOD_EXPR,
7282 : DECL_FIELD_BIT_OFFSET (field),
7283 : bitsize_unit_node)))
7284 0 : warning_at (DECL_SOURCE_LOCATION (field), OPT_Wabi,
7285 : "offset of %qD is not ABI-compliant and may "
7286 : "change in a future version of GCC", field);
7287 :
7288 : /* The middle end uses the type of expressions to determine the
7289 : possible range of expression values. In order to optimize
7290 : "x.i > 7" to "false" for a 2-bit bitfield "i", the middle end
7291 : must be made aware of the width of "i", via its type.
7292 :
7293 : Because C++ does not have integer types of arbitrary width,
7294 : we must (for the purposes of the front end) convert from the
7295 : type assigned here to the declared type of the bitfield
7296 : whenever a bitfield expression is used as an rvalue.
7297 : Similarly, when assigning a value to a bitfield, the value
7298 : must be converted to the type given the bitfield here. */
7299 17246957 : if (DECL_C_BIT_FIELD (field))
7300 : {
7301 643830 : unsigned HOST_WIDE_INT width;
7302 643830 : tree ftype = TREE_TYPE (field);
7303 643830 : width = tree_to_uhwi (DECL_SIZE (field));
7304 643830 : if (width != TYPE_PRECISION (ftype))
7305 : {
7306 506041 : TREE_TYPE (field)
7307 1012082 : = c_build_bitfield_integer_type (width,
7308 506041 : TYPE_UNSIGNED (ftype));
7309 506041 : TREE_TYPE (field)
7310 1012082 : = cp_build_qualified_type (TREE_TYPE (field),
7311 : cp_type_quals (ftype));
7312 : }
7313 : }
7314 :
7315 : /* If we needed additional padding after this field, add it
7316 : now. */
7317 17246957 : if (padding)
7318 : {
7319 768 : tree padding_field;
7320 :
7321 768 : padding_field = build_decl (input_location,
7322 : FIELD_DECL,
7323 : NULL_TREE,
7324 : char_type_node);
7325 768 : DECL_BIT_FIELD (padding_field) = 1;
7326 768 : DECL_SIZE (padding_field) = padding;
7327 768 : DECL_CONTEXT (padding_field) = t;
7328 768 : DECL_ARTIFICIAL (padding_field) = 1;
7329 768 : DECL_IGNORED_P (padding_field) = 1;
7330 768 : DECL_PADDING_P (padding_field) = 1;
7331 768 : layout_nonempty_base_or_field (rli, padding_field,
7332 : NULL_TREE,
7333 : empty_base_offsets);
7334 : }
7335 :
7336 17246957 : last_field_was_bitfield = DECL_C_BIT_FIELD (field);
7337 : }
7338 :
7339 49904436 : if (!integer_zerop (rli->bitpos))
7340 : {
7341 : /* Make sure that we are on a byte boundary so that the size of
7342 : the class without virtual bases will always be a round number
7343 : of bytes. */
7344 5318827 : rli->bitpos = round_up_loc (input_location, rli->bitpos, BITS_PER_UNIT);
7345 5318827 : normalize_rli (rli);
7346 : }
7347 :
7348 : /* We used to remove zero width bitfields at this point since PR42217,
7349 : while the C FE never did that. That caused ABI differences on various
7350 : targets. Set the DECL_FIELD_CXX_ZERO_WIDTH_BIT_FIELD flag on them
7351 : instead, so that the backends can emit -Wpsabi warnings in the cases
7352 : where the ABI changed. */
7353 413101104 : for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
7354 : {
7355 363196668 : if (TREE_CODE (field) == FIELD_DECL
7356 41054710 : && DECL_C_BIT_FIELD (field)
7357 : /* We should not be confused by the fact that grokbitfield
7358 : temporarily sets the width of the bit field into
7359 : DECL_BIT_FIELD_REPRESENTATIVE (field).
7360 : check_bitfield_decl eventually sets DECL_SIZE (field)
7361 : to that width. */
7362 643830 : && (DECL_SIZE (field) == NULL_TREE
7363 643830 : || integer_zerop (DECL_SIZE (field)))
7364 363197837 : && TREE_TYPE (field) != error_mark_node)
7365 1169 : SET_DECL_FIELD_CXX_ZERO_WIDTH_BIT_FIELD (field, 1);
7366 363196668 : check_non_pod_aggregate (field);
7367 : }
7368 :
7369 49904436 : if (CLASSTYPE_NON_LAYOUT_POD_P (t) || CLASSTYPE_EMPTY_P (t))
7370 : {
7371 : /* T needs a different layout as a base (eliding virtual bases
7372 : or whatever). Create that version. */
7373 48076753 : tree base_t = make_node (TREE_CODE (t));
7374 48076753 : tree base_d = create_implicit_typedef (as_base_identifier, base_t);
7375 :
7376 48076753 : TYPE_CONTEXT (base_t) = t;
7377 48076753 : DECL_CONTEXT (base_d) = t;
7378 :
7379 48076753 : set_instantiating_module (base_d);
7380 :
7381 : /* If the ABI version is not at least two, and the last
7382 : field was a bit-field, RLI may not be on a byte
7383 : boundary. In particular, rli_size_unit_so_far might
7384 : indicate the last complete byte, while rli_size_so_far
7385 : indicates the total number of bits used. Therefore,
7386 : rli_size_so_far, rather than rli_size_unit_so_far, is
7387 : used to compute TYPE_SIZE_UNIT. */
7388 :
7389 : /* Set the size and alignment for the new type. */
7390 48076753 : tree eoc = end_of_class (t, eoc_nvsize);
7391 48076753 : TYPE_SIZE_UNIT (base_t)
7392 48076753 : = size_binop (MAX_EXPR,
7393 : fold_convert (sizetype,
7394 : size_binop (CEIL_DIV_EXPR,
7395 : rli_size_so_far (rli),
7396 : bitsize_int (BITS_PER_UNIT))),
7397 : eoc);
7398 48076753 : TYPE_SIZE (base_t)
7399 48076753 : = size_binop (MAX_EXPR,
7400 : rli_size_so_far (rli),
7401 : size_binop (MULT_EXPR,
7402 : fold_convert (bitsizetype, eoc),
7403 : bitsize_int (BITS_PER_UNIT)));
7404 48076753 : SET_TYPE_ALIGN (base_t, rli->record_align);
7405 48076753 : TYPE_USER_ALIGN (base_t) = TYPE_USER_ALIGN (t);
7406 48076753 : TYPE_TYPELESS_STORAGE (base_t) = TYPE_TYPELESS_STORAGE (t);
7407 48076753 : TYPE_CXX_ODR_P (base_t) = TYPE_CXX_ODR_P (t);
7408 :
7409 : /* Copy the non-static data members of T. This will include its
7410 : direct non-virtual bases & vtable. */
7411 48076753 : next_field = &TYPE_FIELDS (base_t);
7412 401303244 : for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
7413 353226491 : if (TREE_CODE (field) == FIELD_DECL)
7414 : {
7415 34349669 : *next_field = copy_node (field);
7416 : /* Zap any NSDMI, it's not needed and might be a deferred
7417 : parse. */
7418 34349669 : DECL_INITIAL (*next_field) = NULL_TREE;
7419 34349669 : DECL_CONTEXT (*next_field) = base_t;
7420 34349669 : next_field = &DECL_CHAIN (*next_field);
7421 : }
7422 48076753 : *next_field = NULL_TREE;
7423 :
7424 : /* We use the base type for trivial assignments, and hence it
7425 : needs a mode. */
7426 48076753 : compute_record_mode (base_t);
7427 :
7428 : /* Record the base version of the type. */
7429 48076753 : CLASSTYPE_AS_BASE (t) = base_t;
7430 : }
7431 : else
7432 1827683 : CLASSTYPE_AS_BASE (t) = t;
7433 :
7434 : /* Every empty class contains an empty class. */
7435 49904436 : if (CLASSTYPE_EMPTY_P (t))
7436 41531118 : CLASSTYPE_CONTAINS_EMPTY_CLASS_P (t) = 1;
7437 :
7438 : /* Set the TYPE_DECL for this type to contain the right
7439 : value for DECL_OFFSET, so that we can use it as part
7440 : of a COMPONENT_REF for multiple inheritance. */
7441 49904436 : layout_decl (TYPE_MAIN_DECL (t), 0);
7442 :
7443 : /* Now fix up any virtual base class types that we left lying
7444 : around. We must get these done before we try to lay out the
7445 : virtual function table. As a side-effect, this will remove the
7446 : base subobject fields. */
7447 49904436 : layout_virtual_bases (rli, empty_base_offsets);
7448 :
7449 : /* Make sure that empty classes are reflected in RLI at this
7450 : point. */
7451 49904436 : include_empty_classes (rli);
7452 :
7453 : /* Make sure not to create any structures with zero size. */
7454 49904436 : if (integer_zerop (rli_size_unit_so_far (rli)) && CLASSTYPE_EMPTY_P (t))
7455 21021757 : place_field (rli,
7456 : build_decl (input_location,
7457 : FIELD_DECL, NULL_TREE, char_type_node));
7458 :
7459 : /* If this is a non-POD, declaring it packed makes a difference to how it
7460 : can be used as a field; don't let finalize_record_size undo it. */
7461 49904436 : if (TYPE_PACKED (t) && !layout_pod_type_p (t))
7462 81 : rli->packed_maybe_necessary = true;
7463 :
7464 : /* Let the back end lay out the type. */
7465 49904436 : finish_record_layout (rli, /*free_p=*/true);
7466 :
7467 : /* If we didn't end up needing an as-base type, don't use it. */
7468 49904436 : if (CLASSTYPE_AS_BASE (t) != t
7469 : /* If T's CLASSTYPE_AS_BASE is TYPE_USER_ALIGN, but T is not,
7470 : replacing the as-base type would change CLASSTYPE_USER_ALIGN,
7471 : causing us to lose the user-specified alignment as in PR94050. */
7472 48076753 : && TYPE_USER_ALIGN (t) == TYPE_USER_ALIGN (CLASSTYPE_AS_BASE (t))
7473 97981175 : && tree_int_cst_equal (TYPE_SIZE (t),
7474 48076739 : TYPE_SIZE (CLASSTYPE_AS_BASE (t))))
7475 26043367 : CLASSTYPE_AS_BASE (t) = t;
7476 :
7477 49904436 : if (TYPE_SIZE_UNIT (t)
7478 49904436 : && TREE_CODE (TYPE_SIZE_UNIT (t)) == INTEGER_CST
7479 49904436 : && !TREE_OVERFLOW (TYPE_SIZE_UNIT (t))
7480 99808863 : && !valid_constant_size_p (TYPE_SIZE_UNIT (t)))
7481 0 : error ("size of type %qT is too large (%qE bytes)", t, TYPE_SIZE_UNIT (t));
7482 :
7483 : /* Warn about bases that can't be talked about due to ambiguity. */
7484 49904436 : maybe_warn_about_inaccessible_bases (t);
7485 :
7486 : /* Now that we're done with layout, give the base fields the real types. */
7487 413285497 : for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
7488 363381061 : if (DECL_ARTIFICIAL (field) && IS_FAKE_BASE_TYPE (TREE_TYPE (field)))
7489 20737292 : TREE_TYPE (field) = TYPE_CONTEXT (TREE_TYPE (field));
7490 :
7491 : /* Clean up. */
7492 49904436 : splay_tree_delete (empty_base_offsets);
7493 :
7494 49904436 : if (CLASSTYPE_EMPTY_P (t)
7495 91435554 : && tree_int_cst_lt (sizeof_biggest_empty_class,
7496 41531118 : TYPE_SIZE_UNIT (t)))
7497 61581 : sizeof_biggest_empty_class = TYPE_SIZE_UNIT (t);
7498 49904436 : }
7499 :
7500 : /* Determine the "key method" for the class type indicated by TYPE,
7501 : and set CLASSTYPE_KEY_METHOD accordingly. */
7502 :
7503 : void
7504 1741069 : determine_key_method (tree type)
7505 : {
7506 1741069 : tree method;
7507 :
7508 1741069 : if (processing_template_decl
7509 1741069 : || CLASSTYPE_TEMPLATE_INSTANTIATION (type)
7510 2507693 : || CLASSTYPE_INTERFACE_KNOWN (type))
7511 : return;
7512 :
7513 : /* The key method is the first non-pure virtual function that is not
7514 : inline at the point of class definition. On some targets the
7515 : key function may not be inline; those targets should not call
7516 : this function until the end of the translation unit. */
7517 7787750 : for (method = TYPE_FIELDS (type); method; method = DECL_CHAIN (method))
7518 7597134 : if (TREE_CODE (method) == FUNCTION_DECL
7519 6191301 : && DECL_VINDEX (method) != NULL_TREE
7520 : /* [[gnu::gnu_inline]] virtual inline/constexpr methods will
7521 : have out of line bodies emitted in some other TU and so
7522 : those can be key methods and vtable emitted in the TU with
7523 : the actual out of line definition. */
7524 1260339 : && ! DECL_NONGNU_INLINE_P (method)
7525 8194594 : && ! DECL_PURE_VIRTUAL_P (method))
7526 : {
7527 575979 : SET_CLASSTYPE_KEY_METHOD (type, method);
7528 575979 : break;
7529 : }
7530 :
7531 : return;
7532 : }
7533 :
7534 : /* Helper of find_flexarrays. Return true when FLD refers to a non-static
7535 : class data member of non-zero size, otherwise false. */
7536 :
7537 : static inline bool
7538 41381232 : field_nonempty_p (const_tree fld)
7539 : {
7540 41381232 : if (TREE_CODE (fld) == ERROR_MARK)
7541 : return false;
7542 :
7543 41381232 : tree type = TREE_TYPE (fld);
7544 41381232 : if (TREE_CODE (fld) == FIELD_DECL
7545 41381232 : && TREE_CODE (type) != ERROR_MARK
7546 82762464 : && (DECL_NAME (fld) || RECORD_OR_UNION_TYPE_P (type)))
7547 41247882 : return TYPE_SIZE (type) && !integer_zerop (TYPE_SIZE (type));
7548 :
7549 : return false;
7550 : }
7551 :
7552 : /* Used by find_flexarrays and related functions. */
7553 :
7554 : struct flexmems_t
7555 : {
7556 : /* The first flexible array member or non-zero array member found
7557 : in the order of layout. */
7558 : tree array;
7559 : /* True if there is a non-static non-empty data member in the class or
7560 : its bases. */
7561 : bool first;
7562 : /* The first non-static non-empty data member following either
7563 : the flexible array member, if found, or the zero-length array member
7564 : otherwise. AFTER[1] refers to the first such data member of a union
7565 : of which the struct containing the flexible array member or zero-length
7566 : array is a member, or NULL when no such union exists. This element is
7567 : only used during searching, not for diagnosing problems. AFTER[0]
7568 : refers to the first such data member that is not a member of such
7569 : a union. */
7570 : tree after[2];
7571 :
7572 : /* Refers to a struct (not union) in which the struct of which the flexible
7573 : array is member is defined. Used to diagnose strictly (according to C)
7574 : invalid uses of the latter structs. */
7575 : tree enclosing;
7576 : };
7577 :
7578 : /* Find either the first flexible array member or the first zero-length
7579 : array, in that order of preference, among members of class T (but not
7580 : its base classes), and set members of FMEM accordingly.
7581 : BASE_P is true if T is a base class of another class.
7582 : PUN is true when inside of a union (perhaps recursively).
7583 : PSTR is set to a data member of the outermost struct of
7584 : which the flexible array is a member if one such struct exists,
7585 : otherwise to NULL. NESTED_P is true for recursive calls except ones
7586 : handling anonymous aggregates. Those types are expected to be diagnosed
7587 : on its own already and so only the last member is checked vs. what
7588 : follows it in the outer type. */
7589 :
7590 : static void
7591 83513873 : find_flexarrays (tree t, flexmems_t *fmem, bool base_p,
7592 : bool nested_p /* = false */, bool pun /* = false */,
7593 : tree pstr /* = NULL_TREE */)
7594 : {
7595 83513873 : if (TREE_CODE (t) == UNION_TYPE)
7596 1288829 : pun = true;
7597 :
7598 83513873 : tree fld = TYPE_FIELDS (t);
7599 83513873 : if ((base_p || nested_p) && TREE_CODE (t) == RECORD_TYPE)
7600 : {
7601 : /* In bases or in nested structures, only process the last
7602 : non-static data member. If we have say
7603 : struct A { int a; int b[]; int c; };
7604 : struct B { int d; int e[]; int f; };
7605 : struct C : A { int g; B h, i; int j; };
7606 : then the A::b followed by A::c should have been diagnosed
7607 : already when completing struct A, and B::e followed by B::f
7608 : when completing struct B, so no need to repeat that when completing
7609 : struct C. So, only look at the last member so we cover e.g.
7610 : struct D { int k; int l[]; };
7611 : struct E : D { int m; };
7612 : struct F { D n; int o; };
7613 : where flexible array member at the end of D is fine, but it isn't
7614 : correct that E::m in E or F::o in F follows it. */
7615 : tree last_fld = NULL_TREE;
7616 56902183 : for (; (fld = next_subobject_field (fld)); fld = DECL_CHAIN (fld))
7617 24196471 : if (DECL_ARTIFICIAL (fld))
7618 7320464 : continue;
7619 16876007 : else if (TREE_TYPE (fld) == error_mark_node)
7620 : return;
7621 16875980 : else if (TREE_CODE (TREE_TYPE (fld)) == ARRAY_TYPE
7622 16875980 : || field_nonempty_p (fld))
7623 : last_fld = fld;
7624 : fld = last_fld;
7625 : }
7626 636056845 : for (; fld; fld = DECL_CHAIN (fld))
7627 : {
7628 552543572 : if (fld == error_mark_node)
7629 : return;
7630 :
7631 : /* Is FLD a typedef for an anonymous struct? */
7632 :
7633 : /* FIXME: Note that typedefs (as well as arrays) need to be fully
7634 : handled elsewhere so that errors like the following are detected
7635 : as well:
7636 : typedef struct { int i, a[], j; } S; // bug c++/72753
7637 : S s [2]; // bug c++/68489
7638 : */
7639 553413020 : if (TREE_CODE (fld) == TYPE_DECL
7640 128432715 : && DECL_IMPLICIT_TYPEDEF_P (fld)
7641 8107540 : && CLASS_TYPE_P (TREE_TYPE (fld))
7642 558707997 : && (IDENTIFIER_ANON_P (DECL_NAME (fld))
7643 5294988 : || TYPE_DECL_WAS_UNNAMED (fld)))
7644 : {
7645 : /* Check the nested unnamed type referenced via a typedef
7646 : independently of FMEM (since it's not a data member of
7647 : the enclosing class). */
7648 869448 : check_flexarrays (TREE_TYPE (fld));
7649 869448 : continue;
7650 : }
7651 :
7652 : /* Skip anything that's GCC-generated or not a (non-static) data
7653 : member. */
7654 551674124 : if (DECL_ARTIFICIAL (fld) || TREE_CODE (fld) != FIELD_DECL)
7655 525400554 : continue;
7656 :
7657 : /* Type of the member. */
7658 26273570 : tree fldtype = TREE_TYPE (fld);
7659 26273570 : if (fldtype == error_mark_node)
7660 : return;
7661 :
7662 : /* Determine the type of the array element or object referenced
7663 : by the member so that it can be checked for flexible array
7664 : members if it hasn't been yet. */
7665 : tree eltype = fldtype;
7666 35902651 : while (TREE_CODE (eltype) == ARRAY_TYPE
7667 35902651 : || INDIRECT_TYPE_P (eltype))
7668 9629226 : eltype = TREE_TYPE (eltype);
7669 :
7670 26273425 : if (RECORD_OR_UNION_TYPE_P (eltype))
7671 : {
7672 9892894 : if (fmem->array && !fmem->after[pun])
7673 : {
7674 : /* Once the member after the flexible array has been found
7675 : we're done. */
7676 428 : fmem->after[pun] = fld;
7677 428 : break;
7678 : }
7679 :
7680 13298122 : if (eltype == fldtype || TYPE_UNNAMED_P (eltype))
7681 : {
7682 : /* Descend into the non-static member struct or union and try
7683 : to find a flexible array member or zero-length array among
7684 : its members. This is only necessary for anonymous types
7685 : and types in whose context the current type T has not been
7686 : defined (the latter must not be checked again because they
7687 : are already in the process of being checked by one of the
7688 : recursive calls). */
7689 :
7690 6503652 : bool first = fmem->first;
7691 6503652 : tree array = fmem->array;
7692 13821334 : bool maybe_anon_p = TYPE_UNNAMED_P (eltype);
7693 841732 : if (tree ctx = maybe_anon_p ? TYPE_CONTEXT (eltype) : NULL_TREE)
7694 841732 : maybe_anon_p = RECORD_OR_UNION_TYPE_P (ctx);
7695 :
7696 : /* If this member isn't anonymous and a prior non-flexible array
7697 : member has been seen in one of the enclosing structs, clear
7698 : the FIRST member since it doesn't contribute to the flexible
7699 : array struct's members. */
7700 6503652 : if (first && !array && !ANON_AGGR_TYPE_P (eltype))
7701 2012542 : fmem->first = false;
7702 :
7703 11218368 : find_flexarrays (eltype, fmem, false, !maybe_anon_p, pun,
7704 4714716 : !pstr && TREE_CODE (t) == RECORD_TYPE
7705 : ? fld : pstr);
7706 :
7707 6503652 : if (fmem->array != array)
7708 : {
7709 : /* If the recursive call passed true to nested_p,
7710 : it only looked at the last field and we do not
7711 : want to diagnose in that case the "in otherwise empty"
7712 : case, just if it is followed by some other non-empty
7713 : member. So set fmem->first. */
7714 1039 : if (!maybe_anon_p)
7715 258 : fmem->first = true;
7716 1039 : continue;
7717 : }
7718 :
7719 6502613 : if (first && !array && !ANON_AGGR_TYPE_P (eltype))
7720 : /* Restore the FIRST member reset above if no flexible
7721 : array member has been found in this member's struct. */
7722 2012160 : fmem->first = first;
7723 :
7724 : /* If the member struct contains the first flexible array
7725 : member, or if this member is a base class, continue to
7726 : the next member and avoid setting the FMEM->AFTER pointer
7727 : to point to it. */
7728 6502613 : if (base_p)
7729 1149240 : continue;
7730 : }
7731 : }
7732 :
7733 25122718 : if (field_nonempty_p (fld))
7734 : {
7735 : /* Remember the first non-static data member. */
7736 24984977 : if (!fmem->first)
7737 9715795 : fmem->first = true;
7738 :
7739 : /* Remember the first non-static data member after the flexible
7740 : array member, if one has been found, or the zero-length array
7741 : if it has been found. */
7742 24984977 : if (fmem->array && !fmem->after[pun])
7743 1508 : fmem->after[pun] = fld;
7744 : }
7745 :
7746 : /* Skip non-arrays. */
7747 25122718 : if (TREE_CODE (fldtype) != ARRAY_TYPE)
7748 23194173 : continue;
7749 :
7750 : /* Determine the upper bound of the array if it has one. */
7751 1928545 : if (TYPE_DOMAIN (fldtype))
7752 : {
7753 1926922 : if (fmem->array)
7754 : {
7755 : /* Make a record of the zero-length array if either one
7756 : such field or a flexible array member has been seen to
7757 : handle the pathological and unlikely case of multiple
7758 : such members. */
7759 1040 : if (!fmem->after[pun])
7760 83 : fmem->after[pun] = fld;
7761 : }
7762 1925882 : else if (integer_all_onesp (TYPE_MAX_VALUE (TYPE_DOMAIN (fldtype))))
7763 : {
7764 : /* Remember the first zero-length array unless a flexible array
7765 : member has already been seen. */
7766 2461 : fmem->array = fld;
7767 2461 : fmem->enclosing = pstr;
7768 : }
7769 : }
7770 : else
7771 : {
7772 : /* Flexible array members have no upper bound. */
7773 1623 : if (fmem->array)
7774 : {
7775 79 : if (TYPE_DOMAIN (TREE_TYPE (fmem->array)))
7776 : {
7777 : /* Replace the zero-length array if it's been stored and
7778 : reset the after pointer. */
7779 31 : fmem->after[pun] = NULL_TREE;
7780 31 : fmem->array = fld;
7781 31 : fmem->enclosing = pstr;
7782 : }
7783 48 : else if (!fmem->after[pun])
7784 : /* Make a record of another flexible array member. */
7785 39 : fmem->after[pun] = fld;
7786 : }
7787 : else
7788 : {
7789 1544 : fmem->array = fld;
7790 1544 : fmem->enclosing = pstr;
7791 : }
7792 : }
7793 : }
7794 : }
7795 :
7796 : /* Diagnose a strictly (by the C standard) invalid use of a struct with
7797 : a flexible array member (or the zero-length array extension). */
7798 :
7799 : static void
7800 2156 : diagnose_invalid_flexarray (const flexmems_t *fmem)
7801 : {
7802 2156 : if (fmem->array && fmem->enclosing)
7803 : {
7804 655 : auto_diagnostic_group d;
7805 655 : if (pedwarn (location_of (fmem->enclosing), OPT_Wpedantic,
7806 655 : TYPE_DOMAIN (TREE_TYPE (fmem->array))
7807 : ? G_("invalid use of %q#T with a zero-size array in %q#D")
7808 : : G_("invalid use of %q#T with a flexible array member "
7809 : "in %q#T"),
7810 655 : DECL_CONTEXT (fmem->array),
7811 655 : DECL_CONTEXT (fmem->enclosing)))
7812 78 : inform (DECL_SOURCE_LOCATION (fmem->array),
7813 78 : "array member %q#D declared here", fmem->array);
7814 655 : }
7815 2156 : }
7816 :
7817 : /* Issue diagnostics for invalid flexible array members or zero-length
7818 : arrays that are not the last elements of the containing class or its
7819 : base classes or that are its sole members. */
7820 :
7821 : static void
7822 49566293 : diagnose_flexarrays (tree t, const flexmems_t *fmem)
7823 : {
7824 49566293 : if (!fmem->array)
7825 : return;
7826 :
7827 4005 : if (fmem->first && !fmem->after[0])
7828 : {
7829 1874 : diagnose_invalid_flexarray (fmem);
7830 1874 : return;
7831 : }
7832 :
7833 : /* Has a diagnostic been issued? */
7834 2131 : bool diagd = false;
7835 :
7836 2131 : const char *msg = 0;
7837 2131 : const char *msg_fam = 0;
7838 :
7839 2131 : if (TYPE_DOMAIN (TREE_TYPE (fmem->array)))
7840 : {
7841 1615 : if (fmem->after[0])
7842 : msg = G_("zero-size array member %qD not at end of %q#T");
7843 256 : else if (!fmem->first)
7844 : msg = G_("zero-size array member %qD in an otherwise empty %q#T");
7845 :
7846 : if (msg)
7847 : {
7848 1615 : location_t loc = DECL_SOURCE_LOCATION (fmem->array);
7849 :
7850 1615 : auto_diagnostic_group d;
7851 1615 : if (pedwarn (loc, OPT_Wpedantic, msg, fmem->array, t))
7852 : {
7853 138 : inform (location_of (t), "in the definition of %q#T", t);
7854 138 : diagd = true;
7855 : }
7856 1615 : }
7857 : }
7858 : else
7859 : {
7860 516 : if (fmem->after[0])
7861 : msg = G_("flexible array member %qD not at end of %q#T");
7862 318 : else if (!fmem->first)
7863 318 : msg_fam = G_("flexible array member %qD in an otherwise"
7864 : " empty %q#T is a GCC extension");
7865 :
7866 516 : if (msg || msg_fam)
7867 : {
7868 516 : location_t loc = DECL_SOURCE_LOCATION (fmem->array);
7869 516 : diagd = true;
7870 :
7871 516 : auto_diagnostic_group d;
7872 516 : if (msg)
7873 198 : error_at (loc, msg, fmem->array, t);
7874 : else
7875 318 : pedwarn (loc, OPT_Wpedantic, msg_fam, fmem->array, t);
7876 :
7877 : /* In the unlikely event that the member following the flexible
7878 : array member is declared in a different class, or the member
7879 : overlaps another member of a common union, point to it.
7880 : Otherwise it should be obvious. */
7881 516 : if (fmem->after[0]
7882 516 : && (DECL_CONTEXT (fmem->after[0])
7883 198 : != DECL_CONTEXT (fmem->array)))
7884 : {
7885 78 : inform (DECL_SOURCE_LOCATION (fmem->after[0]),
7886 : "next member %q#D declared here",
7887 : fmem->after[0]);
7888 78 : inform (location_of (t), "in the definition of %q#T", t);
7889 : }
7890 516 : }
7891 : }
7892 :
7893 2131 : if (!diagd && fmem->array && fmem->enclosing)
7894 282 : diagnose_invalid_flexarray (fmem);
7895 : }
7896 :
7897 :
7898 : /* Recursively check to make sure that any flexible array or zero-length
7899 : array members of class T or its bases are valid (i.e., not the sole
7900 : non-static data member of T and, if one exists, that it is the last
7901 : non-static data member of T and its base classes. FMEM is expected
7902 : to be initially null and is used internally by recursive calls to
7903 : the function. Issue the appropriate diagnostics for the array member
7904 : that fails the checks. */
7905 :
7906 : static void
7907 78217812 : check_flexarrays (tree t, flexmems_t *fmem /* = NULL */,
7908 : bool base_p /* = false */)
7909 : {
7910 : /* Initialize the result of a search for flexible array and zero-length
7911 : array members. Avoid doing any work if the most interesting FMEM data
7912 : have already been populated. */
7913 78217812 : flexmems_t flexmems = flexmems_t ();
7914 78217812 : if (!fmem)
7915 : fmem = &flexmems;
7916 27443928 : else if (fmem->array && fmem->first && fmem->after[0])
7917 0 : return;
7918 :
7919 78217812 : tree fam = fmem->array;
7920 :
7921 : /* Recursively check the primary base class first. */
7922 78217812 : if (CLASSTYPE_HAS_PRIMARY_BASE_P (t))
7923 : {
7924 2601312 : tree basetype = BINFO_TYPE (CLASSTYPE_PRIMARY_BINFO (t));
7925 2601312 : check_flexarrays (basetype, fmem, true);
7926 : }
7927 :
7928 : /* Recursively check the base classes. */
7929 78217812 : int nbases = TYPE_BINFO (t) ? BINFO_N_BASE_BINFOS (TYPE_BINFO (t)) : 0;
7930 105727994 : for (int i = 0; i < nbases; ++i)
7931 : {
7932 27510182 : tree base_binfo = BINFO_BASE_BINFO (TYPE_BINFO (t), i);
7933 :
7934 : /* The primary base class was already checked above. */
7935 27510182 : if (base_binfo == CLASSTYPE_PRIMARY_BINFO (t))
7936 2601157 : continue;
7937 :
7938 : /* Virtual base classes are at the end. */
7939 24909025 : if (BINFO_VIRTUAL_P (base_binfo))
7940 261096 : continue;
7941 :
7942 : /* Check the base class. */
7943 24647929 : check_flexarrays (BINFO_TYPE (base_binfo), fmem, /*base_p=*/true);
7944 : }
7945 :
7946 78217812 : if (fmem == &flexmems)
7947 : {
7948 : /* Check virtual base classes only once per derived class.
7949 : I.e., this check is not performed recursively for base
7950 : classes. */
7951 50773884 : int i;
7952 50773884 : tree base_binfo;
7953 50773884 : vec<tree, va_gc> *vbases;
7954 50968571 : for (vbases = CLASSTYPE_VBASECLASSES (t), i = 0;
7955 50968571 : vec_safe_iterate (vbases, i, &base_binfo); i++)
7956 : {
7957 : /* Check the virtual base class. */
7958 194687 : tree basetype = TREE_TYPE (base_binfo);
7959 :
7960 194687 : check_flexarrays (basetype, fmem, /*base_p=*/true);
7961 : }
7962 : }
7963 :
7964 : /* Is the type unnamed (and therefore a member of it potentially
7965 : an anonymous struct or union)? */
7966 158384632 : bool maybe_anon_p = TYPE_UNNAMED_P (t);
7967 1598678 : if (tree ctx = maybe_anon_p ? TYPE_CONTEXT (t) : NULL_TREE)
7968 1598678 : maybe_anon_p = RECORD_OR_UNION_TYPE_P (ctx);
7969 :
7970 : /* Search the members of the current (possibly derived) class, skipping
7971 : unnamed structs and unions since those could be anonymous. */
7972 78217812 : if (fmem != &flexmems || !maybe_anon_p)
7973 104454327 : find_flexarrays (t, fmem, base_p || fam != fmem->array);
7974 :
7975 78217812 : if (fmem == &flexmems && !maybe_anon_p)
7976 : /* Issue diagnostics for invalid flexible and zero-length array
7977 : members found in base classes or among the members of the current
7978 : class. Ignore anonymous structs and unions whose members are
7979 : considered to be members of the enclosing class and thus will
7980 : be diagnosed when checking it. */
7981 49566293 : diagnose_flexarrays (t, fmem);
7982 : }
7983 :
7984 : /* Perform processing required when the definition of T (a class type)
7985 : is complete. Diagnose invalid definitions of flexible array members
7986 : and zero-size arrays. */
7987 :
7988 : void
7989 49904436 : finish_struct_1 (tree t)
7990 : {
7991 49904436 : tree x;
7992 : /* A TREE_LIST. The TREE_VALUE of each node is a FUNCTION_DECL. */
7993 49904436 : tree virtuals = NULL_TREE;
7994 :
7995 49904436 : if (COMPLETE_TYPE_P (t))
7996 : {
7997 0 : gcc_assert (MAYBE_CLASS_TYPE_P (t));
7998 0 : error ("redefinition of %q#T", t);
7999 0 : popclass ();
8000 0 : return;
8001 : }
8002 :
8003 49904436 : if (location_t fcloc = failed_completion_location (t))
8004 : {
8005 11 : auto_diagnostic_group adg;
8006 11 : if (warning (OPT_Wsfinae_incomplete_,
8007 : "defining %qT, which previously failed to be complete "
8008 : "in a SFINAE context", t)
8009 11 : && warn_sfinae_incomplete == 1)
8010 11 : inform (fcloc, "here. Use %qs for a diagnostic at that point",
8011 : "-Wsfinae-incomplete=2");
8012 11 : }
8013 :
8014 : /* If this type was previously laid out as a forward reference,
8015 : make sure we lay it out again. */
8016 49904436 : TYPE_SIZE (t) = NULL_TREE;
8017 49904436 : CLASSTYPE_PRIMARY_BINFO (t) = NULL_TREE;
8018 :
8019 : /* Make assumptions about the class; we'll reset the flags if
8020 : necessary. */
8021 49904436 : CLASSTYPE_EMPTY_P (t) = 1;
8022 49904436 : CLASSTYPE_NEARLY_EMPTY_P (t) = 1;
8023 49904436 : CLASSTYPE_CONTAINS_EMPTY_CLASS_P (t) = 0;
8024 49904436 : CLASSTYPE_LITERAL_P (t) = true;
8025 :
8026 : /* Do end-of-class semantic processing: checking the validity of the
8027 : bases and members and add implicitly generated methods. */
8028 49904436 : check_bases_and_members (t);
8029 :
8030 : /* Find the key method. */
8031 49904436 : if (TYPE_CONTAINS_VPTR_P (t))
8032 : {
8033 : /* The Itanium C++ ABI permits the key method to be chosen when
8034 : the class is defined -- even though the key method so
8035 : selected may later turn out to be an inline function. On
8036 : some systems (such as ARM Symbian OS) the key method cannot
8037 : be determined until the end of the translation unit. On such
8038 : systems, we leave CLASSTYPE_KEY_METHOD set to NULL, which
8039 : will cause the class to be added to KEYED_CLASSES. Then, in
8040 : finish_file we will determine the key method. */
8041 1741069 : if (targetm.cxx.key_method_may_be_inline ())
8042 1741069 : determine_key_method (t);
8043 :
8044 : /* If a polymorphic class has no key method, we may emit the vtable
8045 : in every translation unit where the class definition appears. If
8046 : we're devirtualizing, we can look into the vtable even if we
8047 : aren't emitting it.
8048 :
8049 : Additionally, if the class is attached to a named module, make sure
8050 : to always emit the vtable in this TU. */
8051 1741069 : if (!CLASSTYPE_KEY_METHOD (t) || module_attach_p ())
8052 1165135 : vec_safe_push (keyed_classes, t);
8053 : }
8054 :
8055 : /* Layout the class itself. */
8056 49904436 : layout_class_type (t, &virtuals);
8057 : /* COMPLETE_TYPE_P is now true. */
8058 :
8059 49904436 : set_class_bindings (t);
8060 :
8061 : /* With the layout complete, check for flexible array members and
8062 : zero-length arrays that might overlap other members in the final
8063 : layout. */
8064 49904436 : check_flexarrays (t);
8065 :
8066 49904436 : virtuals = modify_all_vtables (t, nreverse (virtuals));
8067 :
8068 : /* If necessary, create the primary vtable for this class. */
8069 49904436 : if (virtuals || TYPE_CONTAINS_VPTR_P (t))
8070 : {
8071 : /* We must enter these virtuals into the table. */
8072 1741069 : if (!CLASSTYPE_HAS_PRIMARY_BASE_P (t))
8073 249354 : build_primary_vtable (NULL_TREE, t);
8074 1491715 : else if (! BINFO_NEW_VTABLE_MARKED (TYPE_BINFO (t)))
8075 : /* Here we know enough to change the type of our virtual
8076 : function table, but we will wait until later this function. */
8077 0 : build_primary_vtable (CLASSTYPE_PRIMARY_BINFO (t), t);
8078 :
8079 : /* If we're warning about ABI tags, check the types of the new
8080 : virtual functions. */
8081 1741069 : if (warn_abi_tag)
8082 6 : for (tree v = virtuals; v; v = TREE_CHAIN (v))
8083 3 : check_abi_tags (t, TREE_VALUE (v));
8084 : }
8085 :
8086 49904436 : if (TYPE_CONTAINS_VPTR_P (t))
8087 : {
8088 1741069 : int vindex;
8089 1741069 : tree fn;
8090 :
8091 1741069 : if (BINFO_VTABLE (TYPE_BINFO (t)))
8092 1741069 : gcc_assert (DECL_VIRTUAL_P (BINFO_VTABLE (TYPE_BINFO (t))));
8093 1741069 : if (!CLASSTYPE_HAS_PRIMARY_BASE_P (t))
8094 249354 : gcc_assert (BINFO_VIRTUALS (TYPE_BINFO (t)) == NULL_TREE);
8095 :
8096 : /* Add entries for virtual functions introduced by this class. */
8097 1741069 : BINFO_VIRTUALS (TYPE_BINFO (t))
8098 1741069 : = chainon (BINFO_VIRTUALS (TYPE_BINFO (t)), virtuals);
8099 :
8100 : /* Set DECL_VINDEX for all functions declared in this class. */
8101 1741069 : for (vindex = 0, fn = BINFO_VIRTUALS (TYPE_BINFO (t));
8102 11767933 : fn;
8103 10026864 : fn = TREE_CHAIN (fn),
8104 10026864 : vindex += (TARGET_VTABLE_USES_DESCRIPTORS
8105 : ? TARGET_VTABLE_USES_DESCRIPTORS : 1))
8106 : {
8107 10026864 : tree fndecl = BV_FN (fn);
8108 :
8109 10026864 : if (DECL_THUNK_P (fndecl))
8110 : /* A thunk. We should never be calling this entry directly
8111 : from this vtable -- we'd use the entry for the non
8112 : thunk base function. */
8113 126 : DECL_VINDEX (fndecl) = NULL_TREE;
8114 10026738 : else if (TREE_CODE (DECL_VINDEX (fndecl)) != INTEGER_CST)
8115 7895892 : DECL_VINDEX (fndecl) = build_int_cst (NULL_TREE, vindex);
8116 : }
8117 : }
8118 :
8119 49904436 : validate_trivial_abi_attribute (t);
8120 :
8121 49904436 : finish_struct_bits (t);
8122 :
8123 49904436 : set_method_tm_attributes (t);
8124 49904436 : if (flag_openmp || flag_openmp_simd)
8125 118255 : finish_omp_declare_simd_methods (t);
8126 :
8127 : /* Clear DECL_IN_AGGR_P for all member functions. Complete the rtl
8128 : for any static member objects of the type we're working on. */
8129 413285821 : for (x = TYPE_FIELDS (t); x; x = DECL_CHAIN (x))
8130 363381385 : if (DECL_DECLARES_FUNCTION_P (x))
8131 : {
8132 : /* Synthesize constexpr defaulted comparisons. */
8133 202240534 : if (!DECL_ARTIFICIAL (x)
8134 201202472 : && DECL_DEFAULTED_IN_CLASS_P (x)
8135 217779409 : && special_function_p (x) == sfk_comparison)
8136 36964 : defaulted_late_check (x);
8137 202240534 : DECL_IN_AGGR_P (x) = false;
8138 : }
8139 13752676 : else if (VAR_P (x) && TREE_STATIC (x)
8140 13752676 : && TREE_TYPE (x) != error_mark_node
8141 174893462 : && same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (x)), t))
8142 145425 : SET_DECL_MODE (x, TYPE_MODE (t));
8143 :
8144 : /* Complain if one of the field types requires lower visibility. */
8145 49904436 : constrain_class_visibility (t);
8146 :
8147 : /* Make the rtl for any new vtables we have created, and unmark
8148 : the base types we marked. */
8149 49904436 : finish_vtbls (t);
8150 :
8151 : /* Build the VTT for T. */
8152 49904436 : build_vtt (t);
8153 :
8154 49904436 : if (warn_nonvdtor
8155 147 : && TYPE_POLYMORPHIC_P (t) && accessible_nvdtor_p (t)
8156 49904472 : && !CLASSTYPE_FINAL (t))
8157 33 : warning (OPT_Wnon_virtual_dtor,
8158 : "%q#T has virtual functions and accessible"
8159 : " non-virtual destructor", t);
8160 :
8161 49904436 : complete_vars (t);
8162 :
8163 49904436 : if (warn_overloaded_virtual)
8164 424488 : warn_hidden (t);
8165 :
8166 : /* Class layout, assignment of virtual table slots, etc., is now
8167 : complete. Give the back end a chance to tweak the visibility of
8168 : the class or perform any other required target modifications. */
8169 49904436 : targetm.cxx.adjust_class_at_definition (t);
8170 :
8171 49904436 : maybe_suppress_debug_info (t);
8172 :
8173 49904436 : if (flag_vtable_verify)
8174 12 : vtv_save_class_info (t);
8175 :
8176 49904436 : dump_class_hierarchy (t);
8177 :
8178 : /* Finish debugging output for this type. */
8179 49904436 : rest_of_type_compilation (t, ! LOCAL_CLASS_P (t));
8180 :
8181 49904436 : if (TYPE_TRANSPARENT_AGGR (t))
8182 : {
8183 163 : tree field = first_field (t);
8184 163 : if (field == NULL_TREE || error_operand_p (field))
8185 : {
8186 9 : error ("type transparent %q#T does not have any fields", t);
8187 9 : TYPE_TRANSPARENT_AGGR (t) = 0;
8188 : }
8189 154 : else if (DECL_ARTIFICIAL (field))
8190 : {
8191 3 : if (DECL_FIELD_IS_BASE (field))
8192 3 : error ("type transparent class %qT has base classes", t);
8193 : else
8194 : {
8195 0 : gcc_checking_assert (DECL_VIRTUAL_P (field));
8196 0 : error ("type transparent class %qT has virtual functions", t);
8197 : }
8198 3 : TYPE_TRANSPARENT_AGGR (t) = 0;
8199 : }
8200 151 : else if (TYPE_MODE (t) != DECL_MODE (field))
8201 : {
8202 3 : error ("type transparent %q#T cannot be made transparent because "
8203 : "the type of the first field has a different ABI from the "
8204 : "class overall", t);
8205 3 : TYPE_TRANSPARENT_AGGR (t) = 0;
8206 : }
8207 : }
8208 : }
8209 :
8210 : /* When T was built up, the member declarations were added in reverse
8211 : order. Rearrange them to declaration order. */
8212 :
8213 : void
8214 68930760 : unreverse_member_declarations (tree t)
8215 : {
8216 68930760 : tree next;
8217 68930760 : tree prev;
8218 68930760 : tree x;
8219 :
8220 : /* The following lists are all in reverse order. Put them in
8221 : declaration order now. */
8222 68930760 : CLASSTYPE_DECL_LIST (t) = nreverse (CLASSTYPE_DECL_LIST (t));
8223 :
8224 : /* For the TYPE_FIELDS, only the non TYPE_DECLs are in reverse
8225 : order, so we can't just use nreverse. Due to stat_hack
8226 : chicanery in finish_member_declaration. */
8227 68930760 : prev = NULL_TREE;
8228 68930760 : for (x = TYPE_FIELDS (t);
8229 333958462 : x && TREE_CODE (x) != TYPE_DECL;
8230 265027702 : x = next)
8231 : {
8232 265027702 : next = DECL_CHAIN (x);
8233 265027702 : DECL_CHAIN (x) = prev;
8234 265027702 : prev = x;
8235 : }
8236 :
8237 68930760 : if (prev)
8238 : {
8239 27638193 : DECL_CHAIN (TYPE_FIELDS (t)) = x;
8240 27638193 : TYPE_FIELDS (t) = prev;
8241 : }
8242 68930760 : }
8243 :
8244 : /* Classes, structs or unions T marked with hotness attributes propagate
8245 : the attribute to all methods. */
8246 :
8247 : void
8248 49904436 : propagate_class_warmth_attribute (tree t)
8249 : {
8250 49904436 : if (t == NULL_TREE
8251 49904436 : || !(TREE_CODE (t) == RECORD_TYPE
8252 : || TREE_CODE (t) == UNION_TYPE))
8253 : return;
8254 :
8255 49904436 : tree class_has_cold_attr
8256 49904436 : = lookup_attribute ("cold", TYPE_ATTRIBUTES (t));
8257 49904436 : tree class_has_hot_attr
8258 49904436 : = lookup_attribute ("hot", TYPE_ATTRIBUTES (t));
8259 :
8260 49904436 : if (class_has_cold_attr || class_has_hot_attr)
8261 42 : for (tree f = TYPE_FIELDS (t); f; f = DECL_CHAIN (f))
8262 30 : if (DECL_DECLARES_FUNCTION_P (f))
8263 18 : maybe_propagate_warmth_attributes (STRIP_TEMPLATE (f), t);
8264 : }
8265 :
8266 : tree
8267 29881679 : finish_struct (tree t, tree attributes)
8268 : {
8269 29881679 : location_t saved_loc = input_location;
8270 :
8271 : /* Now that we've got all the field declarations, reverse everything
8272 : as necessary. */
8273 29881679 : unreverse_member_declarations (t);
8274 :
8275 29881679 : cplus_decl_attributes (&t, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
8276 29881679 : fixup_attribute_variants (t);
8277 :
8278 : /* Nadger the current location so that diagnostics point to the start of
8279 : the struct, not the end. */
8280 29881679 : input_location = DECL_SOURCE_LOCATION (TYPE_NAME (t));
8281 :
8282 29881679 : if (processing_template_decl)
8283 : {
8284 19026324 : tree x;
8285 :
8286 148618861 : for (x = TYPE_FIELDS (t); x; x = DECL_CHAIN (x))
8287 129592537 : if (DECL_DECLARES_FUNCTION_P (x))
8288 : {
8289 72308139 : DECL_IN_AGGR_P (x) = false;
8290 72308139 : if (DECL_VIRTUAL_P (x))
8291 1845292 : CLASSTYPE_NON_AGGREGATE (t) = true;
8292 : }
8293 57284398 : else if (TREE_CODE (x) == FIELD_DECL)
8294 : {
8295 12075737 : if (TREE_PROTECTED (x) || TREE_PRIVATE (x))
8296 6234210 : CLASSTYPE_NON_AGGREGATE (t) = true;
8297 : }
8298 :
8299 : /* Also add a USING_DECL for operator=. We know there'll be (at
8300 : least) one, but we don't know the signature(s). We want name
8301 : lookup not to fail or recurse into bases. This isn't added
8302 : to the template decl list so we drop this at instantiation
8303 : time. */
8304 19026324 : tree ass_op = build_lang_decl (USING_DECL, assign_op_identifier,
8305 : NULL_TREE);
8306 19026324 : DECL_CONTEXT (ass_op) = t;
8307 19026324 : USING_DECL_SCOPE (ass_op) = t;
8308 19026324 : DECL_DEPENDENT_P (ass_op) = true;
8309 19026324 : DECL_ARTIFICIAL (ass_op) = true;
8310 19026324 : DECL_CHAIN (ass_op) = TYPE_FIELDS (t);
8311 19026324 : TYPE_FIELDS (t) = ass_op;
8312 :
8313 19026324 : TYPE_SIZE (t) = bitsize_zero_node;
8314 19026324 : TYPE_SIZE_UNIT (t) = size_zero_node;
8315 : /* COMPLETE_TYPE_P is now true. */
8316 :
8317 19026324 : set_class_bindings (t);
8318 :
8319 : /* We need to emit an error message if this type was used as a parameter
8320 : and it is an abstract type, even if it is a template. We construct
8321 : a simple CLASSTYPE_PURE_VIRTUALS list without taking bases into
8322 : account and we call complete_vars with this type, which will check
8323 : the PARM_DECLS. Note that while the type is being defined,
8324 : CLASSTYPE_PURE_VIRTUALS contains the list of the inline friends
8325 : (see CLASSTYPE_INLINE_FRIENDS) so we need to clear it. */
8326 19026324 : CLASSTYPE_PURE_VIRTUALS (t) = NULL;
8327 167645185 : for (x = TYPE_FIELDS (t); x; x = DECL_CHAIN (x))
8328 148618861 : if (TREE_CODE (x) == FUNCTION_DECL && DECL_PURE_VIRTUAL_P (x))
8329 269292 : vec_safe_push (CLASSTYPE_PURE_VIRTUALS (t), x);
8330 19026324 : complete_vars (t);
8331 :
8332 : /* Remember current #pragma pack value. */
8333 19026324 : TYPE_PRECISION (t) = maximum_field_alignment;
8334 :
8335 19026324 : if (cxx_dialect < cxx20)
8336 : {
8337 591297 : if (!CLASSTYPE_NON_AGGREGATE (t)
8338 591297 : && type_has_user_provided_or_explicit_constructor (t))
8339 19584 : CLASSTYPE_NON_AGGREGATE (t) = 1;
8340 : }
8341 18435027 : else if (TYPE_HAS_USER_CONSTRUCTOR (t))
8342 5628626 : CLASSTYPE_NON_AGGREGATE (t) = 1;
8343 :
8344 : /* Fix up any variants we've already built. */
8345 19026324 : fixup_type_variants (t);
8346 : }
8347 : else
8348 10855355 : finish_struct_1 (t);
8349 : /* COMPLETE_TYPE_P is now true. */
8350 :
8351 29881679 : maybe_warn_about_overly_private_class (t);
8352 :
8353 29881679 : if (is_std_init_list (t))
8354 : {
8355 : /* People keep complaining that the compiler crashes on an invalid
8356 : definition of initializer_list, so I guess we should explicitly
8357 : reject it. What the compiler internals care about is that it's a
8358 : template and has a pointer field followed by size_type field. */
8359 14100 : bool ok = false;
8360 14100 : if (processing_template_decl)
8361 : {
8362 14100 : tree f = next_aggregate_field (TYPE_FIELDS (t));
8363 14100 : if (f && TYPE_PTR_P (TREE_TYPE (f)))
8364 : {
8365 14097 : f = next_aggregate_field (DECL_CHAIN (f));
8366 14097 : if (f && same_type_p (TREE_TYPE (f), size_type_node))
8367 : ok = true;
8368 : }
8369 : }
8370 : /* It also cannot be a union. */
8371 14100 : ok &= NON_UNION_CLASS_TYPE_P (t);
8372 14097 : if (!ok)
8373 9 : fatal_error (input_location, "definition of %qD does not match "
8374 9 : "%<#include <initializer_list>%>", TYPE_NAME (t));
8375 : }
8376 :
8377 29881670 : input_location = saved_loc;
8378 :
8379 29881670 : TYPE_BEING_DEFINED (t) = 0;
8380 :
8381 29881670 : if (current_class_type)
8382 29881670 : popclass ();
8383 : else
8384 0 : error ("trying to finish struct, but kicked out due to previous parse errors");
8385 :
8386 29881670 : if (flag_openmp)
8387 717810 : for (tree decl = TYPE_FIELDS (t); decl; decl = DECL_CHAIN (decl))
8388 604643 : if (TREE_CODE (decl) == FUNCTION_DECL
8389 604643 : && DECL_OBJECT_MEMBER_FUNCTION_P (decl))
8390 126665 : if (tree attr = lookup_attribute ("omp declare variant base",
8391 126665 : DECL_ATTRIBUTES (decl)))
8392 120 : omp_declare_variant_finalize (decl, attr);
8393 :
8394 19026315 : if (processing_template_decl && at_function_scope_p ()
8395 : /* Lambdas are defined by the LAMBDA_EXPR. */
8396 32279945 : && !LAMBDA_TYPE_P (t))
8397 242436 : add_stmt (build_min (TAG_DEFN, t));
8398 :
8399 : /* Lambdas will be keyed by their LAMBDA_TYPE_EXTRA_SCOPE when that
8400 : gets set; other local types might need keying anyway though. */
8401 31421744 : if (at_function_scope_p () && !LAMBDA_TYPE_P (t))
8402 249626 : maybe_key_decl (current_scope (), TYPE_NAME (t));
8403 :
8404 29881670 : return t;
8405 : }
8406 :
8407 : /* Hash table to avoid endless recursion when handling references. */
8408 : static hash_table<nofree_ptr_hash<tree_node> > *fixed_type_or_null_ref_ht;
8409 :
8410 : /* Return the dynamic type of INSTANCE, if known.
8411 : Used to determine whether the virtual function table is needed
8412 : or not.
8413 :
8414 : *NONNULL is set iff INSTANCE can be known to be nonnull, regardless
8415 : of our knowledge of its type. *NONNULL should be initialized
8416 : before this function is called. */
8417 :
8418 : static tree
8419 15454480 : fixed_type_or_null (tree instance, int *nonnull, int *cdtorp)
8420 : {
8421 : #define RECUR(T) fixed_type_or_null((T), nonnull, cdtorp)
8422 :
8423 15454480 : switch (TREE_CODE (instance))
8424 : {
8425 2273614 : case INDIRECT_REF:
8426 2273614 : if (INDIRECT_TYPE_P (TREE_TYPE (instance)))
8427 : return NULL_TREE;
8428 : else
8429 2260695 : return RECUR (TREE_OPERAND (instance, 0));
8430 :
8431 138547 : case CALL_EXPR:
8432 : /* This is a call to a constructor, hence it's never zero. */
8433 138547 : if (CALL_EXPR_FN (instance)
8434 138547 : && TREE_HAS_CONSTRUCTOR (instance))
8435 : {
8436 0 : if (nonnull)
8437 0 : *nonnull = 1;
8438 0 : return TREE_TYPE (instance);
8439 : }
8440 138547 : if (CLASS_TYPE_P (TREE_TYPE (instance)))
8441 : {
8442 : /* We missed a build_cplus_new somewhere, likely due to tf_decltype
8443 : mishandling. */
8444 0 : gcc_checking_assert (false);
8445 : if (nonnull)
8446 : *nonnull = 1;
8447 : return TREE_TYPE (instance);
8448 : }
8449 : return NULL_TREE;
8450 :
8451 46317 : case SAVE_EXPR:
8452 : /* This is a call to a constructor, hence it's never zero. */
8453 46317 : if (TREE_HAS_CONSTRUCTOR (instance))
8454 : {
8455 0 : if (nonnull)
8456 0 : *nonnull = 1;
8457 0 : return TREE_TYPE (instance);
8458 : }
8459 46317 : return RECUR (TREE_OPERAND (instance, 0));
8460 :
8461 261 : case POINTER_PLUS_EXPR:
8462 261 : case PLUS_EXPR:
8463 261 : case MINUS_EXPR:
8464 261 : if (TREE_CODE (TREE_OPERAND (instance, 0)) == ADDR_EXPR)
8465 24 : return RECUR (TREE_OPERAND (instance, 0));
8466 237 : if (TREE_CODE (TREE_OPERAND (instance, 1)) == INTEGER_CST)
8467 : /* Propagate nonnull. */
8468 33 : return RECUR (TREE_OPERAND (instance, 0));
8469 :
8470 : return NULL_TREE;
8471 :
8472 2924656 : CASE_CONVERT:
8473 2924656 : return RECUR (TREE_OPERAND (instance, 0));
8474 :
8475 803050 : case ADDR_EXPR:
8476 803050 : instance = TREE_OPERAND (instance, 0);
8477 803050 : if (nonnull)
8478 : {
8479 : /* Just because we see an ADDR_EXPR doesn't mean we're dealing
8480 : with a real object -- given &p->f, p can still be null. */
8481 801371 : tree t = get_base_address (instance);
8482 : /* ??? Probably should check DECL_WEAK here. */
8483 801371 : if (t && DECL_P (t))
8484 117593 : *nonnull = 1;
8485 : }
8486 803050 : return RECUR (instance);
8487 :
8488 1104906 : case COMPONENT_REF:
8489 : /* If this component is really a base class reference, then the field
8490 : itself isn't definitive. */
8491 1104906 : if (DECL_FIELD_IS_BASE (TREE_OPERAND (instance, 1)))
8492 1264 : return RECUR (TREE_OPERAND (instance, 0));
8493 1103642 : return RECUR (TREE_OPERAND (instance, 1));
8494 :
8495 1479923 : case VAR_DECL:
8496 1479923 : case FIELD_DECL:
8497 1479923 : if (TREE_CODE (TREE_TYPE (instance)) == ARRAY_TYPE
8498 1479923 : && MAYBE_CLASS_TYPE_P (TREE_TYPE (TREE_TYPE (instance))))
8499 : {
8500 45 : if (nonnull)
8501 45 : *nonnull = 1;
8502 45 : return TREE_TYPE (TREE_TYPE (instance));
8503 : }
8504 : /* fall through. */
8505 7574161 : case TARGET_EXPR:
8506 7574161 : case PARM_DECL:
8507 7574161 : case RESULT_DECL:
8508 7574161 : if (MAYBE_CLASS_TYPE_P (TREE_TYPE (instance)))
8509 : {
8510 1861650 : if (nonnull)
8511 1860196 : *nonnull = 1;
8512 1861650 : return TREE_TYPE (instance);
8513 : }
8514 5712511 : else if (instance == current_class_ptr)
8515 : {
8516 4861204 : if (nonnull)
8517 4209752 : *nonnull = 1;
8518 :
8519 : /* if we're in a ctor or dtor, we know our type. If
8520 : current_class_ptr is set but we aren't in a function, we're in
8521 : an NSDMI (and therefore a constructor). */
8522 4861204 : if (current_scope () != current_function_decl
8523 4861204 : || (DECL_LANG_SPECIFIC (current_function_decl)
8524 9722152 : && (DECL_CONSTRUCTOR_P (current_function_decl)
8525 2370456 : || DECL_DESTRUCTOR_P (current_function_decl))))
8526 : {
8527 2580199 : if (cdtorp)
8528 2580199 : *cdtorp = 1;
8529 2580199 : return TREE_TYPE (TREE_TYPE (instance));
8530 : }
8531 : }
8532 851307 : else if (TYPE_REF_P (TREE_TYPE (instance)))
8533 : {
8534 : /* We only need one hash table because it is always left empty. */
8535 680202 : if (!fixed_type_or_null_ref_ht)
8536 13694 : fixed_type_or_null_ref_ht
8537 13694 : = new hash_table<nofree_ptr_hash<tree_node> > (37);
8538 :
8539 : /* Reference variables should be references to objects. */
8540 680202 : if (nonnull)
8541 677742 : *nonnull = 1;
8542 :
8543 : /* Enter the INSTANCE in a table to prevent recursion; a
8544 : variable's initializer may refer to the variable
8545 : itself. */
8546 680202 : if (VAR_P (instance)
8547 22222 : && DECL_INITIAL (instance)
8548 2452 : && !type_dependent_expression_p_push (DECL_INITIAL (instance))
8549 682654 : && !fixed_type_or_null_ref_ht->find (instance))
8550 : {
8551 2396 : tree type;
8552 2396 : tree_node **slot;
8553 :
8554 2396 : slot = fixed_type_or_null_ref_ht->find_slot (instance, INSERT);
8555 2396 : *slot = instance;
8556 2396 : type = RECUR (DECL_INITIAL (instance));
8557 2396 : fixed_type_or_null_ref_ht->remove_elt (instance);
8558 :
8559 2396 : return type;
8560 : }
8561 : }
8562 : return NULL_TREE;
8563 :
8564 357632 : case VIEW_CONVERT_EXPR:
8565 357632 : if (location_wrapper_p (instance))
8566 357632 : return RECUR (TREE_OPERAND (instance, 0));
8567 : else
8568 : /* TODO: Recursion may be correct for some non-location-wrapper
8569 : uses of VIEW_CONVERT_EXPR. */
8570 : return NULL_TREE;
8571 :
8572 : default:
8573 : return NULL_TREE;
8574 : }
8575 : #undef RECUR
8576 : }
8577 :
8578 : /* Return nonzero if the dynamic type of INSTANCE is known, and
8579 : equivalent to the static type. We also handle the case where
8580 : INSTANCE is really a pointer. Return negative if this is a
8581 : ctor/dtor. There the dynamic type is known, but this might not be
8582 : the most derived base of the original object, and hence virtual
8583 : bases may not be laid out according to this type.
8584 :
8585 : Used to determine whether the virtual function table is needed
8586 : or not.
8587 :
8588 : *NONNULL is set iff INSTANCE can be known to be nonnull, regardless
8589 : of our knowledge of its type. *NONNULL should be initialized
8590 : before this function is called. */
8591 :
8592 : int
8593 7479510 : resolves_to_fixed_type_p (tree instance, int* nonnull)
8594 : {
8595 7479510 : tree t = TREE_TYPE (instance);
8596 7479510 : int cdtorp = 0;
8597 7479510 : tree fixed;
8598 :
8599 : /* processing_template_decl can be false in a template if we're in
8600 : instantiate_non_dependent_expr, but we still want to suppress
8601 : this check. */
8602 7479510 : if (in_template_context)
8603 : {
8604 : /* In a template we only care about the type of the result. */
8605 13 : if (nonnull)
8606 2 : *nonnull = true;
8607 13 : return true;
8608 : }
8609 :
8610 7479497 : fixed = fixed_type_or_null (instance, nonnull, &cdtorp);
8611 7479497 : if (INDIRECT_TYPE_P (t))
8612 4634924 : t = TREE_TYPE (t);
8613 7479497 : if (CLASS_TYPE_P (t) && CLASSTYPE_FINAL (t))
8614 : return 1;
8615 7416909 : if (fixed == NULL_TREE)
8616 : return 0;
8617 4406175 : if (!same_type_ignoring_top_level_qualifiers_p (t, fixed))
8618 : return 0;
8619 4405709 : return cdtorp ? -1 : 1;
8620 : }
8621 :
8622 :
8623 : void
8624 98453 : init_class_processing (void)
8625 : {
8626 98453 : current_class_depth = 0;
8627 98453 : current_class_stack_size = 10;
8628 98453 : current_class_stack
8629 98453 : = XNEWVEC (struct class_stack_node, current_class_stack_size);
8630 98453 : sizeof_biggest_empty_class = size_zero_node;
8631 :
8632 98453 : ridpointers[(int) RID_PUBLIC] = access_public_node;
8633 98453 : ridpointers[(int) RID_PRIVATE] = access_private_node;
8634 98453 : ridpointers[(int) RID_PROTECTED] = access_protected_node;
8635 98453 : }
8636 :
8637 : /* Restore the cached PREVIOUS_CLASS_LEVEL. */
8638 :
8639 : static void
8640 203094729 : restore_class_cache (void)
8641 : {
8642 203094729 : tree type;
8643 :
8644 : /* We are re-entering the same class we just left, so we don't
8645 : have to search the whole inheritance matrix to find all the
8646 : decls to bind again. Instead, we install the cached
8647 : class_shadowed list and walk through it binding names. */
8648 203094729 : push_binding_level (previous_class_level);
8649 203094729 : class_binding_level = previous_class_level;
8650 : /* Restore IDENTIFIER_TYPE_VALUE. */
8651 203094729 : for (type = class_binding_level->type_shadowed;
8652 1048905707 : type;
8653 845810978 : type = TREE_CHAIN (type))
8654 845810978 : SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (type), TREE_TYPE (type));
8655 203094729 : }
8656 :
8657 : /* Set global variables CURRENT_CLASS_NAME and CURRENT_CLASS_TYPE as
8658 : appropriate for TYPE.
8659 :
8660 : So that we may avoid calls to lookup_name, we cache the _TYPE
8661 : nodes of local TYPE_DECLs in the TREE_TYPE field of the name.
8662 :
8663 : For multiple inheritance, we perform a two-pass depth-first search
8664 : of the type lattice. */
8665 :
8666 : void
8667 439836813 : pushclass (tree type)
8668 : {
8669 439836813 : class_stack_node_t csn;
8670 :
8671 439836813 : type = TYPE_MAIN_VARIANT (type);
8672 :
8673 : /* Make sure there is enough room for the new entry on the stack. */
8674 439836813 : if (current_class_depth + 1 >= current_class_stack_size)
8675 : {
8676 11286 : current_class_stack_size *= 2;
8677 11286 : current_class_stack
8678 11286 : = XRESIZEVEC (struct class_stack_node, current_class_stack,
8679 : current_class_stack_size);
8680 : }
8681 :
8682 : /* Insert a new entry on the class stack. */
8683 439836813 : csn = current_class_stack + current_class_depth;
8684 439836813 : csn->name = current_class_name;
8685 439836813 : csn->type = current_class_type;
8686 439836813 : csn->access = current_access_specifier;
8687 439836813 : csn->names_used = 0;
8688 439836813 : csn->hidden = 0;
8689 439836813 : current_class_depth++;
8690 :
8691 : /* Now set up the new type. */
8692 439836813 : current_class_name = TYPE_NAME (type);
8693 439836813 : if (TREE_CODE (current_class_name) == TYPE_DECL)
8694 439836813 : current_class_name = DECL_NAME (current_class_name);
8695 439836813 : current_class_type = type;
8696 :
8697 : /* By default, things in classes are private, while things in
8698 : structures or unions are public. */
8699 439836813 : current_access_specifier = (CLASSTYPE_DECLARED_CLASS (type)
8700 439836813 : ? access_private_node
8701 : : access_public_node);
8702 :
8703 439836813 : if (previous_class_level
8704 263080286 : && type != previous_class_level->this_entity
8705 55311834 : && current_class_depth == 1)
8706 : {
8707 : /* Forcibly remove any old class remnants. */
8708 29611903 : invalidate_class_lookup_cache ();
8709 : }
8710 :
8711 439836813 : if (!previous_class_level
8712 233468383 : || type != previous_class_level->this_entity
8713 207768452 : || current_class_depth > 1)
8714 236742084 : pushlevel_class ();
8715 : else
8716 203094729 : restore_class_cache ();
8717 439836813 : }
8718 :
8719 : /* Get out of the current class scope. If we were in a class scope
8720 : previously, that is the one popped to. */
8721 :
8722 : void
8723 439820550 : popclass (void)
8724 : {
8725 439820550 : poplevel_class ();
8726 :
8727 439820547 : current_class_depth--;
8728 439820547 : current_class_name = current_class_stack[current_class_depth].name;
8729 439820547 : current_class_type = current_class_stack[current_class_depth].type;
8730 439820547 : current_access_specifier = current_class_stack[current_class_depth].access;
8731 439820547 : if (current_class_stack[current_class_depth].names_used)
8732 16697880 : splay_tree_delete (current_class_stack[current_class_depth].names_used);
8733 439820547 : }
8734 :
8735 : /* Mark the top of the class stack as hidden. */
8736 :
8737 : void
8738 495140653 : push_class_stack (void)
8739 : {
8740 495140653 : if (current_class_depth)
8741 335901018 : ++current_class_stack[current_class_depth - 1].hidden;
8742 495140653 : }
8743 :
8744 : /* Mark the top of the class stack as un-hidden. */
8745 :
8746 : void
8747 495015110 : pop_class_stack (void)
8748 : {
8749 495015110 : if (current_class_depth)
8750 335887482 : --current_class_stack[current_class_depth - 1].hidden;
8751 495015110 : }
8752 :
8753 : /* If the class type currently being defined is either T or
8754 : a nested type of T, returns the type from the current_class_stack,
8755 : which might be equivalent to but not equal to T in case of
8756 : constrained partial specializations. */
8757 :
8758 : tree
8759 5008149971 : currently_open_class (tree t)
8760 : {
8761 5008149971 : int i;
8762 :
8763 5008149971 : if (!CLASS_TYPE_P (t))
8764 : return NULL_TREE;
8765 :
8766 4826176246 : t = TYPE_MAIN_VARIANT (t);
8767 :
8768 : /* We start looking from 1 because entry 0 is from global scope,
8769 : and has no type. */
8770 5993498617 : for (i = current_class_depth; i > 0; --i)
8771 : {
8772 5020700547 : tree c;
8773 5020700547 : if (i == current_class_depth)
8774 4427226483 : c = current_class_type;
8775 : else
8776 : {
8777 593474064 : if (current_class_stack[i].hidden)
8778 : break;
8779 444015510 : c = current_class_stack[i].type;
8780 : }
8781 4871241993 : if (!c)
8782 289454795 : continue;
8783 4581787198 : if (same_type_p (c, t))
8784 : return c;
8785 : }
8786 : return NULL_TREE;
8787 : }
8788 :
8789 : /* If either current_class_type or one of its enclosing classes are derived
8790 : from T, return the appropriate type. Used to determine how we found
8791 : something via unqualified lookup. */
8792 :
8793 : tree
8794 161622883 : currently_open_derived_class (tree t)
8795 : {
8796 161622883 : int i;
8797 :
8798 : /* The bases of a dependent type are unknown. */
8799 161622883 : if (dependent_type_p (t))
8800 : return NULL_TREE;
8801 :
8802 155491947 : if (!current_class_type)
8803 : return NULL_TREE;
8804 :
8805 154098734 : if (DERIVED_FROM_P (t, current_class_type))
8806 140667177 : return current_class_type;
8807 :
8808 17935260 : for (i = current_class_depth - 1; i > 0; --i)
8809 : {
8810 7668742 : if (current_class_stack[i].hidden)
8811 : break;
8812 7668742 : if (DERIVED_FROM_P (t, current_class_stack[i].type))
8813 3165039 : return current_class_stack[i].type;
8814 : }
8815 :
8816 : return NULL_TREE;
8817 : }
8818 :
8819 : /* Return the outermost enclosing class type that is still open, or
8820 : NULL_TREE. */
8821 :
8822 : tree
8823 21 : outermost_open_class (void)
8824 : {
8825 21 : if (!current_class_type)
8826 : return NULL_TREE;
8827 21 : tree r = NULL_TREE;
8828 21 : if (TYPE_BEING_DEFINED (current_class_type))
8829 21 : r = current_class_type;
8830 21 : for (int i = current_class_depth - 1; i > 0; --i)
8831 : {
8832 0 : if (current_class_stack[i].hidden)
8833 : break;
8834 0 : tree t = current_class_stack[i].type;
8835 0 : if (!TYPE_BEING_DEFINED (t))
8836 : break;
8837 0 : r = t;
8838 : }
8839 : return r;
8840 : }
8841 :
8842 : /* Returns the innermost class type which is not a lambda closure type. */
8843 :
8844 : tree
8845 1320810117 : current_nonlambda_class_type (void)
8846 : {
8847 1320810117 : tree type = current_class_type;
8848 2357309351 : while (type && LAMBDA_TYPE_P (type))
8849 2936714 : type = decl_type_context (TYPE_NAME (type));
8850 1320810117 : return type;
8851 : }
8852 :
8853 : /* When entering a class scope, all enclosing class scopes' names with
8854 : static meaning (static variables, static functions, types and
8855 : enumerators) have to be visible. This recursive function calls
8856 : pushclass for all enclosing class contexts until global or a local
8857 : scope is reached. TYPE is the enclosed class. */
8858 :
8859 : void
8860 721023485 : push_nested_class (tree type)
8861 : {
8862 : /* A namespace might be passed in error cases, like A::B:C. */
8863 721023485 : if (type == NULL_TREE
8864 721023485 : || !CLASS_TYPE_P (type))
8865 : return;
8866 :
8867 370638592 : push_nested_class (DECL_CONTEXT (TYPE_MAIN_DECL (type)));
8868 :
8869 370638592 : pushclass (type);
8870 : }
8871 :
8872 : /* Undoes a push_nested_class call. */
8873 :
8874 : void
8875 350371345 : pop_nested_class (void)
8876 : {
8877 370622344 : tree context = DECL_CONTEXT (TYPE_MAIN_DECL (current_class_type));
8878 :
8879 370622344 : popclass ();
8880 370622344 : if (context && CLASS_TYPE_P (context))
8881 : pop_nested_class ();
8882 350371345 : }
8883 :
8884 : /* Returns the number of extern "LANG" blocks we are nested within. */
8885 :
8886 : int
8887 3495 : current_lang_depth (void)
8888 : {
8889 3495 : return vec_safe_length (current_lang_base);
8890 : }
8891 :
8892 : /* Set global variables CURRENT_LANG_NAME to appropriate value
8893 : so that behavior of name-mangling machinery is correct. */
8894 :
8895 : void
8896 1173751 : push_lang_context (tree name)
8897 : {
8898 1173751 : vec_safe_push (current_lang_base, current_lang_name);
8899 :
8900 1173751 : if (name == lang_name_cplusplus)
8901 620488 : current_lang_name = name;
8902 553263 : else if (name == lang_name_c)
8903 553263 : current_lang_name = name;
8904 : else
8905 0 : error ("language string %<\"%E\"%> not recognized", name);
8906 1173751 : }
8907 :
8908 : /* Get out of the current language scope. */
8909 :
8910 : void
8911 1173751 : pop_lang_context (void)
8912 : {
8913 1173751 : current_lang_name = current_lang_base->pop ();
8914 1173751 : }
8915 :
8916 : /* Type instantiation routines. */
8917 :
8918 : /* Given an OVERLOAD and a TARGET_TYPE, return the function that
8919 : matches the TARGET_TYPE. If there is no satisfactory match, return
8920 : error_mark_node, and issue an error & warning messages under
8921 : control of FLAGS. Permit pointers to member function if FLAGS
8922 : permits. If TEMPLATE_ONLY, the name of the overloaded function was
8923 : a template-id, and EXPLICIT_TARGS are the explicitly provided
8924 : template arguments.
8925 :
8926 : If OVERLOAD is for one or more member functions, then ACCESS_PATH
8927 : is the base path used to reference those member functions. If
8928 : the address is resolved to a member function, access checks will be
8929 : performed and errors issued if appropriate. */
8930 :
8931 : static tree
8932 692940 : resolve_address_of_overloaded_function (tree target_type,
8933 : tree overload,
8934 : tsubst_flags_t complain,
8935 : bool template_only,
8936 : tree explicit_targs,
8937 : tree access_path)
8938 : {
8939 : /* Here's what the standard says:
8940 :
8941 : [over.over]
8942 :
8943 : If the name is a function template, template argument deduction
8944 : is done, and if the argument deduction succeeds, the deduced
8945 : arguments are used to generate a single template function, which
8946 : is added to the set of overloaded functions considered.
8947 :
8948 : Non-member functions and static member functions match targets of
8949 : type "pointer-to-function" or "reference-to-function." Nonstatic
8950 : member functions match targets of type "pointer-to-member
8951 : function;" the function type of the pointer to member is used to
8952 : select the member function from the set of overloaded member
8953 : functions. If a non-static member function is selected, the
8954 : reference to the overloaded function name is required to have the
8955 : form of a pointer to member as described in 5.3.1.
8956 :
8957 : If more than one function is selected, any template functions in
8958 : the set are eliminated if the set also contains a non-template
8959 : function, and any given template function is eliminated if the
8960 : set contains a second template function that is more specialized
8961 : than the first according to the partial ordering rules 14.5.5.2.
8962 : After such eliminations, if any, there shall remain exactly one
8963 : selected function. */
8964 :
8965 692940 : int is_ptrmem = 0;
8966 : /* We store the matches in a TREE_LIST rooted here. The functions
8967 : are the TREE_PURPOSE, not the TREE_VALUE, in this list, for easy
8968 : interoperability with most_specialized_instantiation. */
8969 692940 : tree matches = NULL_TREE;
8970 692940 : tree fn;
8971 692940 : tree target_fn_type;
8972 :
8973 : /* By the time we get here, we should be seeing only real
8974 : pointer-to-member types, not the internal POINTER_TYPE to
8975 : METHOD_TYPE representation. */
8976 692940 : gcc_assert (!TYPE_PTR_P (target_type)
8977 : || TREE_CODE (TREE_TYPE (target_type)) != METHOD_TYPE);
8978 :
8979 692940 : gcc_assert (is_overloaded_fn (overload));
8980 :
8981 : /* Check that the TARGET_TYPE is reasonable. */
8982 52819 : if (TYPE_PTRFN_P (target_type)
8983 692944 : || TYPE_REFFN_P (target_type))
8984 : /* This is OK. */;
8985 640123 : else if (TYPE_PTRMEMFUNC_P (target_type))
8986 : /* This is OK, too. */
8987 : is_ptrmem = 1;
8988 638863 : else if (TREE_CODE (target_type) == FUNCTION_TYPE)
8989 : /* This is OK, too. This comes from a conversion to reference
8990 : type. */
8991 638820 : target_type = build_reference_type (target_type);
8992 : else
8993 : {
8994 43 : if (complain & tf_error)
8995 34 : error ("cannot resolve overloaded function %qD based on"
8996 : " conversion to type %qT",
8997 34 : OVL_NAME (overload), target_type);
8998 43 : return error_mark_node;
8999 : }
9000 :
9001 : /* Non-member functions and static member functions match targets of type
9002 : "pointer-to-function" or "reference-to-function." Nonstatic member
9003 : functions match targets of type "pointer-to-member-function;" the
9004 : function type of the pointer to member is used to select the member
9005 : function from the set of overloaded member functions.
9006 :
9007 : So figure out the FUNCTION_TYPE that we want to match against. */
9008 692897 : target_fn_type = static_fn_type (target_type);
9009 :
9010 : /* If we can find a non-template function that matches, we can just
9011 : use it. There's no point in generating template instantiations
9012 : if we're just going to throw them out anyhow. But, of course, we
9013 : can only do this when we don't *need* a template function. */
9014 692897 : if (!template_only)
9015 5170632 : for (lkp_iterator iter (overload); iter; ++iter)
9016 : {
9017 4501867 : tree fn = *iter;
9018 :
9019 4501867 : if (TREE_CODE (fn) == TEMPLATE_DECL)
9020 : /* We're not looking for templates just yet. */
9021 28792 : continue;
9022 :
9023 4473075 : if ((TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE) != is_ptrmem)
9024 : /* We're looking for a non-static member, and this isn't
9025 : one, or vice versa. */
9026 580 : continue;
9027 :
9028 : /* Constraints must be satisfied. This is done before
9029 : return type deduction since that instantiates the
9030 : function. */
9031 4472495 : if (!constraints_satisfied_p (fn))
9032 27 : continue;
9033 :
9034 : /* For target_version semantics, never resolve a non-default
9035 : version. */
9036 4472468 : if (!TARGET_HAS_FMV_TARGET_ATTRIBUTE
9037 : && TREE_CODE (fn) == FUNCTION_DECL
9038 : && !is_function_default_version (fn))
9039 : continue;
9040 :
9041 4472468 : if (undeduced_auto_decl (fn))
9042 : {
9043 : /* Force instantiation to do return type deduction. */
9044 18 : maybe_instantiate_decl (fn);
9045 18 : require_deduced_type (fn);
9046 : }
9047 :
9048 : /* In C++17 we need the noexcept-qualifier to compare types. */
9049 4472468 : if (flag_noexcept_type
9050 4472468 : && !maybe_instantiate_noexcept (fn, complain))
9051 0 : continue;
9052 :
9053 : /* See if there's a match. */
9054 4472468 : tree fntype = static_fn_type (fn);
9055 4472468 : if (same_type_p (target_fn_type, fntype)
9056 4472468 : || fnptr_conv_p (target_fn_type, fntype))
9057 640580 : matches = tree_cons (fn, NULL_TREE, matches);
9058 : }
9059 :
9060 : /* Now, if we've already got a match (or matches), there's no need
9061 : to proceed to the template functions. But, if we don't have a
9062 : match we need to look at them, too. */
9063 668765 : if (!matches)
9064 : {
9065 52890 : tree target_arg_types;
9066 52890 : tree target_ret_type;
9067 52890 : tree *args;
9068 52890 : unsigned int nargs, ia;
9069 52890 : tree arg;
9070 :
9071 52890 : target_arg_types = TYPE_ARG_TYPES (target_fn_type);
9072 52890 : target_ret_type = TREE_TYPE (target_fn_type);
9073 :
9074 52890 : nargs = list_length (target_arg_types);
9075 52890 : args = XALLOCAVEC (tree, nargs);
9076 52890 : for (arg = target_arg_types, ia = 0;
9077 165631 : arg != NULL_TREE;
9078 112741 : arg = TREE_CHAIN (arg), ++ia)
9079 112741 : args[ia] = TREE_VALUE (arg);
9080 52890 : nargs = ia;
9081 :
9082 109086 : for (lkp_iterator iter (overload); iter; ++iter)
9083 : {
9084 56196 : tree fn = *iter;
9085 56196 : tree instantiation;
9086 56196 : tree targs;
9087 :
9088 56196 : if (TREE_CODE (fn) != TEMPLATE_DECL)
9089 : /* We're only looking for templates. */
9090 3519 : continue;
9091 :
9092 52677 : if ((TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
9093 : != is_ptrmem)
9094 : /* We're not looking for a non-static member, and this is
9095 : one, or vice versa. */
9096 12 : continue;
9097 :
9098 52665 : tree ret = target_ret_type;
9099 :
9100 : /* If the template has a deduced return type, don't expose it to
9101 : template argument deduction. */
9102 52665 : if (undeduced_auto_decl (fn))
9103 230 : ret = NULL_TREE;
9104 :
9105 : /* Try to do argument deduction. */
9106 52665 : targs = make_tree_vec (DECL_NTPARMS (fn));
9107 52665 : instantiation = fn_type_unification (fn, explicit_targs, targs, args,
9108 : nargs, ret,
9109 : DEDUCE_EXACT, LOOKUP_NORMAL,
9110 : NULL, false, false);
9111 52665 : if (instantiation == error_mark_node)
9112 : /* Instantiation failed. */
9113 13745 : continue;
9114 :
9115 : /* Constraints must be satisfied. This is done before
9116 : return type deduction since that instantiates the
9117 : function. */
9118 38920 : if (flag_concepts && !constraints_satisfied_p (instantiation))
9119 0 : continue;
9120 :
9121 : /* And now force instantiation to do return type deduction. */
9122 38920 : if (undeduced_auto_decl (instantiation))
9123 : {
9124 68 : ++function_depth;
9125 68 : instantiate_decl (instantiation, /*defer*/false, /*class*/false);
9126 68 : --function_depth;
9127 :
9128 68 : require_deduced_type (instantiation);
9129 : }
9130 :
9131 : /* In C++17 we need the noexcept-qualifier to compare types. */
9132 38920 : if (flag_noexcept_type)
9133 37988 : maybe_instantiate_noexcept (instantiation, complain);
9134 :
9135 : /* See if there's a match. */
9136 38920 : tree fntype = static_fn_type (instantiation);
9137 38920 : if (same_type_p (target_fn_type, fntype)
9138 38920 : || fnptr_conv_p (target_fn_type, fntype))
9139 38902 : matches = tree_cons (instantiation, fn, matches);
9140 : }
9141 :
9142 : /* Now, remove all but the most specialized of the matches. */
9143 52890 : if (matches)
9144 : {
9145 38791 : tree match = most_specialized_instantiation (matches);
9146 :
9147 38791 : if (match != error_mark_node)
9148 : {
9149 38764 : matches = match;
9150 38764 : TREE_CHAIN (match) = NULL_TREE;
9151 : }
9152 : }
9153 : }
9154 640007 : else if (flag_concepts && TREE_CHAIN (matches))
9155 97 : if (tree match = most_constrained_function (matches))
9156 : {
9157 9 : matches = match;
9158 9 : TREE_CHAIN (match) = NULL_TREE;
9159 : }
9160 :
9161 : /* Now we should have exactly one function in MATCHES. */
9162 678798 : if (matches == NULL_TREE)
9163 : {
9164 : /* There were *no* matches. */
9165 14099 : if (complain & tf_error)
9166 : {
9167 87 : auto_diagnostic_group d;
9168 174 : error ("no matches converting function %qD to type %q#T",
9169 87 : OVL_NAME (overload), target_type);
9170 :
9171 87 : print_candidates (input_location, overload);
9172 87 : }
9173 14099 : return error_mark_node;
9174 : }
9175 678798 : else if (TREE_CHAIN (matches))
9176 : {
9177 : /* There were too many matches. First check if they're all
9178 : the same function. */
9179 159 : tree match = NULL_TREE;
9180 :
9181 159 : fn = TREE_PURPOSE (matches);
9182 :
9183 : /* For multi-versioned functions, more than one match is just fine and
9184 : decls_match will return false as they are different. */
9185 723 : for (match = TREE_CHAIN (matches); match; match = TREE_CHAIN (match))
9186 591 : if (!decls_match (fn, TREE_PURPOSE (match))
9187 1167 : && !disjoint_version_decls (fn, TREE_PURPOSE (match)))
9188 : break;
9189 :
9190 159 : if (match)
9191 : {
9192 27 : if (complain & tf_error)
9193 : {
9194 9 : auto_diagnostic_group d;
9195 9 : error ("converting overloaded function %qD to type %q#T is ambiguous",
9196 9 : OVL_NAME (overload), target_type);
9197 :
9198 : /* Since print_candidates expects the functions in the
9199 : TREE_VALUE slot, we flip them here. */
9200 36 : for (match = matches; match; match = TREE_CHAIN (match))
9201 18 : TREE_VALUE (match) = TREE_PURPOSE (match);
9202 :
9203 9 : print_candidates (input_location, matches);
9204 9 : }
9205 :
9206 27 : return error_mark_node;
9207 : }
9208 : }
9209 :
9210 : /* Good, exactly one match. Now, convert it to the correct type. */
9211 678771 : fn = TREE_PURPOSE (matches);
9212 :
9213 1356353 : if (DECL_OBJECT_MEMBER_FUNCTION_P (fn)
9214 679035 : && !(complain & tf_ptrmem_ok))
9215 : {
9216 : /* Previously we allowed this behavior for iobj member functions when the
9217 : -fms-extensions flag is passed as MSVC allows this as a language
9218 : extension. MSVC also allows this for xobj member functions, but the
9219 : documentation for -fms-extensions states it's purpose is to support
9220 : the use of microsoft headers. Until otherwise demonstrated, we should
9221 : assume xobj member functions are not used in this manner in microsoft
9222 : headers and forbid the incorrect syntax instead of supporting it for
9223 : non-legacy uses. This should hopefully encourage conformance going
9224 : forward.
9225 : This comment is referred to in typeck.cc:cp_build_addr_expr_1. */
9226 270 : if (DECL_IOBJ_MEMBER_FUNCTION_P (fn) && flag_ms_extensions)
9227 : /* Early escape. */;
9228 249 : else if (!(complain & tf_error))
9229 179 : return error_mark_node;
9230 70 : else if (DECL_XOBJ_MEMBER_FUNCTION_P (fn))
9231 : {
9232 34 : auto_diagnostic_group d;
9233 : /* Should match the error in typeck.cc:cp_build_addr_expr_1.
9234 : We seem to lack the details here to match that diagnostic exactly,
9235 : perhaps this could be fixed in the future? See PR113075 bug 2. */
9236 34 : error_at (input_location,
9237 : "ISO C++ forbids taking the address of an unqualified"
9238 : " or parenthesized non-static member function to form"
9239 : " a pointer to explicit object member function.");
9240 : /* This is incorrect, see PR113075 bug 3. */
9241 34 : inform (input_location,
9242 : "a pointer to explicit object member function can only be "
9243 : "formed with %<&%E%>", fn);
9244 34 : }
9245 : else
9246 : {
9247 36 : static int explained;
9248 36 : gcc_assert (DECL_IOBJ_MEMBER_FUNCTION_P (fn) && !flag_ms_extensions);
9249 : /* Is there a reason this error message doesn't match the one in
9250 : typeck.cc:cp_build_addr_expr_1? */
9251 36 : auto_diagnostic_group d;
9252 36 : if (permerror (input_location, "assuming pointer to member %qD", fn)
9253 36 : && !explained)
9254 : {
9255 18 : inform (input_location, "(a pointer to member can only be "
9256 : "formed with %<&%E%>)", fn);
9257 18 : explained = 1;
9258 : }
9259 36 : }
9260 : }
9261 :
9262 : /* If a pointer to a function that is multi-versioned is requested, the
9263 : pointer to the dispatcher function is returned instead. This works
9264 : well because indirectly calling the function will dispatch the right
9265 : function version at run-time.
9266 : This is done at multiple_target.cc for target_version semantics. */
9267 :
9268 678592 : if (DECL_FUNCTION_VERSIONED (fn) && TARGET_HAS_FMV_TARGET_ATTRIBUTE)
9269 : {
9270 117 : fn = get_function_version_dispatcher (fn);
9271 117 : if (fn == NULL)
9272 9 : return error_mark_node;
9273 : /* Mark all the versions corresponding to the dispatcher as used. */
9274 108 : if (!(complain & tf_conv))
9275 36 : mark_versions_used (fn);
9276 : }
9277 :
9278 : /* If we're doing overload resolution purely for the purpose of
9279 : determining conversion sequences, we should not consider the
9280 : function used. If this conversion sequence is selected, the
9281 : function will be marked as used at this point. */
9282 678511 : if (!(complain & tf_conv))
9283 : {
9284 : /* Make =delete work with SFINAE. */
9285 657281 : if (DECL_DELETED_FN (fn) && !(complain & tf_error))
9286 0 : return error_mark_node;
9287 657281 : if (!mark_used (fn, complain) && !(complain & tf_error))
9288 0 : return error_mark_node;
9289 : }
9290 :
9291 : /* We could not check access to member functions when this
9292 : expression was originally created since we did not know at that
9293 : time to which function the expression referred. */
9294 678583 : if (DECL_FUNCTION_MEMBER_P (fn))
9295 : {
9296 6236 : gcc_assert (access_path);
9297 6236 : perform_or_defer_access_check (access_path, fn, fn, complain);
9298 : }
9299 :
9300 678583 : if (TYPE_PTRFN_P (target_type) || TYPE_PTRMEMFUNC_P (target_type))
9301 40432 : return cp_build_addr_expr (fn, complain);
9302 : else
9303 : {
9304 : /* The target must be a REFERENCE_TYPE. Above, cp_build_unary_op
9305 : will mark the function as addressed, but here we must do it
9306 : explicitly. */
9307 638151 : cxx_mark_addressable (fn);
9308 :
9309 638151 : return fn;
9310 : }
9311 : }
9312 :
9313 : /* This function will instantiate the type of the expression given in
9314 : RHS to match the type of LHSTYPE. If errors exist, then return
9315 : error_mark_node. COMPLAIN is a bit mask. If TF_ERROR is set, then
9316 : we complain on errors. If we are not complaining, never modify rhs,
9317 : as overload resolution wants to try many possible instantiations, in
9318 : the hope that at least one will work.
9319 :
9320 : For non-recursive calls, LHSTYPE should be a function, pointer to
9321 : function, or a pointer to member function. */
9322 :
9323 : tree
9324 695254 : instantiate_type (tree lhstype, tree rhs, tsubst_flags_t complain)
9325 : {
9326 704456 : tsubst_flags_t complain_in = complain;
9327 704456 : tree access_path = NULL_TREE;
9328 :
9329 704456 : complain &= ~tf_ptrmem_ok;
9330 :
9331 704456 : STRIP_ANY_LOCATION_WRAPPER (rhs);
9332 :
9333 704456 : if (lhstype == unknown_type_node)
9334 : {
9335 0 : if (complain & tf_error)
9336 0 : error ("not enough type information");
9337 0 : return error_mark_node;
9338 : }
9339 :
9340 704456 : if (TREE_TYPE (rhs) != NULL_TREE && ! (type_unknown_p (rhs)))
9341 : {
9342 1723 : tree fntype = non_reference (lhstype);
9343 1723 : if (same_type_p (fntype, TREE_TYPE (rhs)))
9344 : return rhs;
9345 324 : if (fnptr_conv_p (fntype, TREE_TYPE (rhs)))
9346 : return rhs;
9347 321 : if (flag_ms_extensions
9348 18 : && TYPE_PTRMEMFUNC_P (fntype)
9349 339 : && !TYPE_PTRMEMFUNC_P (TREE_TYPE (rhs)))
9350 : /* Microsoft allows `A::f' to be resolved to a
9351 : pointer-to-member. */
9352 : ;
9353 : else
9354 : {
9355 303 : if (complain & tf_error)
9356 43 : error ("cannot convert %qE from type %qT to type %qT",
9357 43 : rhs, TREE_TYPE (rhs), fntype);
9358 303 : return error_mark_node;
9359 : }
9360 : }
9361 :
9362 : /* If we instantiate a template, and it is a A ?: C expression
9363 : with omitted B, look through the SAVE_EXPR. */
9364 702751 : if (TREE_CODE (rhs) == SAVE_EXPR)
9365 9 : rhs = TREE_OPERAND (rhs, 0);
9366 :
9367 702751 : if (BASELINK_P (rhs))
9368 : {
9369 6571 : access_path = BASELINK_ACCESS_BINFO (rhs);
9370 6571 : rhs = BASELINK_FUNCTIONS (rhs);
9371 : }
9372 :
9373 : /* There are only a few kinds of expressions that may have a type
9374 : dependent on overload resolution. */
9375 702751 : gcc_assert (TREE_CODE (rhs) == ADDR_EXPR
9376 : || TREE_CODE (rhs) == COMPONENT_REF
9377 : || is_overloaded_fn (rhs)
9378 : || (flag_ms_extensions && TREE_CODE (rhs) == FUNCTION_DECL));
9379 :
9380 : /* This should really only be used when attempting to distinguish
9381 : what sort of a pointer to function we have. For now, any
9382 : arithmetic operation which is not supported on pointers
9383 : is rejected as an error. */
9384 :
9385 702751 : switch (TREE_CODE (rhs))
9386 : {
9387 609 : case COMPONENT_REF:
9388 609 : {
9389 609 : tree member = TREE_OPERAND (rhs, 1);
9390 :
9391 609 : member = instantiate_type (lhstype, member, complain);
9392 609 : if (member != error_mark_node
9393 609 : && TREE_SIDE_EFFECTS (TREE_OPERAND (rhs, 0)))
9394 : /* Do not lose object's side effects. */
9395 27 : return build2 (COMPOUND_EXPR, TREE_TYPE (member),
9396 54 : TREE_OPERAND (rhs, 0), member);
9397 : return member;
9398 : }
9399 :
9400 2092 : case OFFSET_REF:
9401 2092 : rhs = TREE_OPERAND (rhs, 1);
9402 2092 : if (BASELINK_P (rhs))
9403 : return instantiate_type (lhstype, rhs, complain_in);
9404 :
9405 : /* This can happen if we are forming a pointer-to-member for a
9406 : member template. */
9407 0 : gcc_assert (TREE_CODE (rhs) == TEMPLATE_ID_EXPR);
9408 :
9409 : /* Fall through. */
9410 :
9411 24144 : case TEMPLATE_ID_EXPR:
9412 24144 : {
9413 24144 : tree fns = TREE_OPERAND (rhs, 0);
9414 24144 : tree args = TREE_OPERAND (rhs, 1);
9415 :
9416 24144 : return
9417 24144 : resolve_address_of_overloaded_function (lhstype, fns, complain_in,
9418 : /*template_only=*/true,
9419 24144 : args, access_path);
9420 : }
9421 :
9422 668796 : case OVERLOAD:
9423 668796 : case FUNCTION_DECL:
9424 668796 : return
9425 668796 : resolve_address_of_overloaded_function (lhstype, rhs, complain_in,
9426 : /*template_only=*/false,
9427 : /*explicit_targs=*/NULL_TREE,
9428 668796 : access_path);
9429 :
9430 7110 : case ADDR_EXPR:
9431 7110 : {
9432 7110 : if (PTRMEM_OK_P (rhs))
9433 2092 : complain |= tf_ptrmem_ok;
9434 :
9435 7110 : return instantiate_type (lhstype, TREE_OPERAND (rhs, 0), complain);
9436 : }
9437 :
9438 0 : case ERROR_MARK:
9439 0 : return error_mark_node;
9440 :
9441 0 : default:
9442 0 : gcc_unreachable ();
9443 : }
9444 : return error_mark_node;
9445 : }
9446 :
9447 : /* Return the name of the virtual function pointer field
9448 : (as an IDENTIFIER_NODE) for the given TYPE. Note that
9449 : this may have to look back through base types to find the
9450 : ultimate field name. (For single inheritance, these could
9451 : all be the same name. Who knows for multiple inheritance). */
9452 :
9453 : static tree
9454 249354 : get_vfield_name (tree type)
9455 : {
9456 249354 : tree binfo, base_binfo;
9457 :
9458 249354 : for (binfo = TYPE_BINFO (type);
9459 249354 : BINFO_N_BASE_BINFOS (binfo);
9460 : binfo = base_binfo)
9461 : {
9462 72159 : base_binfo = BINFO_BASE_BINFO (binfo, 0);
9463 :
9464 144318 : if (BINFO_VIRTUAL_P (base_binfo)
9465 72159 : || !TYPE_CONTAINS_VPTR_P (BINFO_TYPE (base_binfo)))
9466 : break;
9467 : }
9468 :
9469 249354 : type = BINFO_TYPE (binfo);
9470 249354 : tree ctor_name = constructor_name (type);
9471 249354 : char *buf = (char *) alloca (sizeof (VFIELD_NAME_FORMAT)
9472 : + IDENTIFIER_LENGTH (ctor_name) + 2);
9473 249354 : sprintf (buf, VFIELD_NAME_FORMAT, IDENTIFIER_POINTER (ctor_name));
9474 249354 : return get_identifier (buf);
9475 : }
9476 :
9477 : /* Build a dummy reference to ourselves so Derived::Base (and A::A) works,
9478 : according to [class]:
9479 : The class-name is also inserted
9480 : into the scope of the class itself. For purposes of access checking,
9481 : the inserted class name is treated as if it were a public member name. */
9482 :
9483 : void
9484 29881688 : build_self_reference (void)
9485 : {
9486 29881688 : tree name = DECL_NAME (TYPE_NAME (current_class_type));
9487 29881688 : tree decl = build_lang_decl (TYPE_DECL, name, current_class_type);
9488 :
9489 29881688 : DECL_NONLOCAL (decl) = 1;
9490 29881688 : DECL_CONTEXT (decl) = current_class_type;
9491 29881688 : DECL_ARTIFICIAL (decl) = 1;
9492 29881688 : SET_DECL_SELF_REFERENCE_P (decl);
9493 29881688 : set_underlying_type (decl);
9494 29881688 : set_instantiating_module (decl);
9495 :
9496 29881688 : if (processing_template_decl)
9497 19026324 : decl = push_template_decl (decl);
9498 :
9499 29881688 : tree saved_cas = current_access_specifier;
9500 29881688 : current_access_specifier = access_public_node;
9501 29881688 : finish_member_declaration (decl);
9502 29881688 : current_access_specifier = saved_cas;
9503 29881688 : }
9504 :
9505 : /* Returns 1 if TYPE contains only padding bytes. */
9506 :
9507 : int
9508 595942000 : is_empty_class (tree type)
9509 : {
9510 595942000 : if (type == error_mark_node)
9511 : return 0;
9512 :
9513 595941997 : if (! CLASS_TYPE_P (type))
9514 : return 0;
9515 :
9516 361110756 : return CLASSTYPE_EMPTY_P (type);
9517 : }
9518 :
9519 : /* Returns true if TYPE contains no actual data, just various
9520 : possible combinations of empty classes. If IGNORE_VPTR is true,
9521 : a vptr doesn't prevent the class from being considered empty. Typically
9522 : we want to ignore the vptr on assignment, and not on initialization. */
9523 :
9524 : bool
9525 377981535 : is_really_empty_class (tree type, bool ignore_vptr)
9526 : {
9527 377981535 : if (CLASS_TYPE_P (type))
9528 : {
9529 31279033 : tree field;
9530 31279033 : tree binfo;
9531 31279033 : tree base_binfo;
9532 31279033 : int i;
9533 :
9534 : /* CLASSTYPE_EMPTY_P isn't set properly until the class is actually laid
9535 : out, but we'd like to be able to check this before then. */
9536 31279033 : if (COMPLETE_TYPE_P (type) && is_empty_class (type))
9537 : return true;
9538 :
9539 24142656 : if (!ignore_vptr && TYPE_CONTAINS_VPTR_P (type))
9540 : return false;
9541 :
9542 26474271 : for (binfo = TYPE_BINFO (type), i = 0;
9543 26474271 : BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
9544 3781960 : if (!is_really_empty_class (BINFO_TYPE (base_binfo), ignore_vptr))
9545 : return false;
9546 419855970 : for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
9547 419387235 : if (TREE_CODE (field) == FIELD_DECL
9548 24510558 : && !DECL_ARTIFICIAL (field)
9549 : /* An unnamed bit-field is not a data member. */
9550 22347847 : && !DECL_UNNAMED_BIT_FIELD (field)
9551 441732744 : && !is_really_empty_class (TREE_TYPE (field), ignore_vptr))
9552 : return false;
9553 : return true;
9554 : }
9555 346702502 : else if (TREE_CODE (type) == ARRAY_TYPE)
9556 1354981 : return (integer_zerop (array_type_nelts_top (type))
9557 1354981 : || is_really_empty_class (TREE_TYPE (type), ignore_vptr));
9558 : return false;
9559 : }
9560 :
9561 : /* Note that NAME was looked up while the current class was being
9562 : defined and that the result of that lookup was DECL. */
9563 :
9564 : void
9565 2141955255 : maybe_note_name_used_in_class (tree name, tree decl)
9566 : {
9567 : /* If we're not defining a class, there's nothing to do. */
9568 2141955255 : if (!(innermost_scope_kind() == sk_class
9569 292630937 : && TYPE_BEING_DEFINED (current_class_type)
9570 520296387 : && !LAMBDA_TYPE_P (current_class_type)))
9571 : return;
9572 :
9573 261041353 : const cp_binding_level *blev = nullptr;
9574 261041353 : if (const cxx_binding *binding = IDENTIFIER_BINDING (name))
9575 216183948 : blev = binding->scope;
9576 261041353 : const cp_binding_level *lev = current_binding_level;
9577 :
9578 : /* Record the binding in the names_used tables for classes inside blev. */
9579 389078018 : for (int i = current_class_depth; i > 0; --i)
9580 : {
9581 554841626 : tree type = (i == current_class_depth
9582 277420813 : ? current_class_type
9583 16379460 : : current_class_stack[i].type);
9584 :
9585 300129126 : for (; lev; lev = lev->level_chain)
9586 : {
9587 300129126 : if (lev == blev)
9588 : /* We found the declaration. */
9589 : return;
9590 150744978 : if (lev->kind == sk_class && lev->this_entity == type)
9591 : /* This class is inside the declaration scope. */
9592 : break;
9593 : }
9594 :
9595 128036665 : auto &names_used = current_class_stack[i-1].names_used;
9596 128036665 : if (!names_used)
9597 16697889 : names_used = splay_tree_new (splay_tree_compare_pointers, 0, 0);
9598 :
9599 128036665 : tree use = build1_loc (input_location, VIEW_CONVERT_EXPR,
9600 128036665 : TREE_TYPE (decl), decl);
9601 128036665 : EXPR_LOCATION_WRAPPER_P (use) = 1;
9602 128036665 : splay_tree_insert (names_used,
9603 : (splay_tree_key) name,
9604 : (splay_tree_value) use);
9605 : }
9606 : }
9607 :
9608 : /* Note that NAME was declared (as DECL) in the current class. Check
9609 : to see that the declaration is valid under [class.member.lookup]:
9610 :
9611 : If [the result of a search in T for N at point P] differs from the result of
9612 : a search in T for N from immediately after the class-specifier of T, the
9613 : program is ill-formed, no diagnostic required. */
9614 :
9615 : void
9616 325456825 : note_name_declared_in_class (tree name, tree decl)
9617 : {
9618 325456825 : splay_tree names_used;
9619 325456825 : splay_tree_node n;
9620 :
9621 : /* Look to see if we ever used this name. */
9622 325456825 : names_used
9623 325456825 : = current_class_stack[current_class_depth - 1].names_used;
9624 325456825 : if (!names_used)
9625 : return;
9626 : /* The C language allows members to be declared with a type of the same
9627 : name, and the C++ standard says this diagnostic is not required. So
9628 : allow it in extern "C" blocks unless pedantic is specified.
9629 : Allow it in all cases if -ms-extensions is specified. */
9630 100340926 : if ((!pedantic && current_lang_name == lang_name_c)
9631 98254576 : || flag_ms_extensions)
9632 : return;
9633 98254570 : n = splay_tree_lookup (names_used, (splay_tree_key) name);
9634 98254570 : if (n)
9635 : {
9636 60 : tree use = (tree) n->value;
9637 60 : location_t loc = EXPR_LOCATION (use);
9638 60 : tree olddecl = OVL_FIRST (TREE_OPERAND (use, 0));
9639 : /* [basic.scope.class]
9640 :
9641 : A name N used in a class S shall refer to the same declaration
9642 : in its context and when re-evaluated in the completed scope of
9643 : S. */
9644 60 : auto ov = make_temp_override (global_dc->m_pedantic_errors);
9645 60 : if (TREE_CODE (decl) == TYPE_DECL
9646 21 : && TREE_CODE (olddecl) == TYPE_DECL
9647 81 : && same_type_p (TREE_TYPE (decl), TREE_TYPE (olddecl)))
9648 : /* Different declaration, but same meaning; just warn. */;
9649 54 : else if (flag_permissive)
9650 : /* Let -fpermissive make it a warning like past versions. */;
9651 : else
9652 : /* Make it an error. */
9653 33 : global_dc->m_pedantic_errors = 1;
9654 :
9655 60 : auto_diagnostic_group d;
9656 120 : if (pedwarn (location_of (decl), OPT_Wchanges_meaning,
9657 : "declaration of %q#D changes meaning of %qD",
9658 60 : decl, OVL_NAME (decl)))
9659 : {
9660 51 : inform (loc, "used here to mean %q#D", olddecl);
9661 51 : inform (location_of (olddecl), "declared here" );
9662 : }
9663 60 : }
9664 : }
9665 :
9666 : /* Returns the VAR_DECL for the complete vtable associated with BINFO.
9667 : Secondary vtables are merged with primary vtables; this function
9668 : will return the VAR_DECL for the primary vtable. */
9669 :
9670 : tree
9671 6085629 : get_vtbl_decl_for_binfo (tree binfo)
9672 : {
9673 6085629 : tree decl;
9674 :
9675 6085629 : decl = BINFO_VTABLE (binfo);
9676 6085629 : if (decl && TREE_CODE (decl) == POINTER_PLUS_EXPR)
9677 : {
9678 6085626 : gcc_assert (TREE_CODE (TREE_OPERAND (decl, 0)) == ADDR_EXPR);
9679 6085626 : decl = TREE_OPERAND (TREE_OPERAND (decl, 0), 0);
9680 : }
9681 6085626 : if (decl)
9682 6085626 : gcc_assert (VAR_P (decl));
9683 6085629 : return decl;
9684 : }
9685 :
9686 :
9687 : /* Returns the binfo for the primary base of BINFO. If the resulting
9688 : BINFO is a virtual base, and it is inherited elsewhere in the
9689 : hierarchy, then the returned binfo might not be the primary base of
9690 : BINFO in the complete object. Check BINFO_PRIMARY_P or
9691 : BINFO_LOST_PRIMARY_P to be sure. */
9692 :
9693 : static tree
9694 44675125 : get_primary_binfo (tree binfo)
9695 : {
9696 44675125 : tree primary_base;
9697 :
9698 44675125 : primary_base = CLASSTYPE_PRIMARY_BINFO (BINFO_TYPE (binfo));
9699 44675125 : if (!primary_base)
9700 : return NULL_TREE;
9701 :
9702 12831733 : return copied_binfo (primary_base, binfo);
9703 : }
9704 :
9705 : /* As above, but iterate until we reach the binfo that actually provides the
9706 : vptr for BINFO. */
9707 :
9708 : static tree
9709 2585003 : most_primary_binfo (tree binfo)
9710 : {
9711 2585003 : tree b = binfo;
9712 8119985 : while (CLASSTYPE_HAS_PRIMARY_BASE_P (BINFO_TYPE (b))
9713 11069964 : && !BINFO_LOST_PRIMARY_P (b))
9714 : {
9715 2949979 : tree primary_base = get_primary_binfo (b);
9716 2949979 : gcc_assert (BINFO_PRIMARY_P (primary_base)
9717 : && BINFO_INHERITANCE_CHAIN (primary_base) == b);
9718 : b = primary_base;
9719 : }
9720 2585003 : return b;
9721 : }
9722 :
9723 : /* Returns true if BINFO gets its vptr from a virtual base of the most derived
9724 : type. Note that the virtual inheritance might be above or below BINFO in
9725 : the hierarchy. */
9726 :
9727 : bool
9728 165 : vptr_via_virtual_p (tree binfo)
9729 : {
9730 165 : if (TYPE_P (binfo))
9731 0 : binfo = TYPE_BINFO (binfo);
9732 165 : tree primary = most_primary_binfo (binfo);
9733 : /* Don't limit binfo_via_virtual, we want to return true when BINFO itself is
9734 : a morally virtual base. */
9735 165 : tree virt = binfo_via_virtual (primary, NULL_TREE);
9736 165 : return virt != NULL_TREE;
9737 : }
9738 :
9739 : /* If INDENTED_P is zero, indent to INDENT. Return nonzero. */
9740 :
9741 : static int
9742 129 : maybe_indent_hierarchy (FILE * stream, int indent, int indented_p)
9743 : {
9744 0 : if (!indented_p)
9745 39 : fprintf (stream, "%*s", indent, "");
9746 129 : return 1;
9747 : }
9748 :
9749 : /* Dump the offsets of all the bases rooted at BINFO to STREAM.
9750 : INDENT should be zero when called from the top level; it is
9751 : incremented recursively. IGO indicates the next expected BINFO in
9752 : inheritance graph ordering. */
9753 :
9754 : static tree
9755 102 : dump_class_hierarchy_r (FILE *stream,
9756 : dump_flags_t flags,
9757 : tree binfo,
9758 : tree igo,
9759 : int indent)
9760 : {
9761 102 : int indented = 0;
9762 102 : tree base_binfo;
9763 102 : int i;
9764 :
9765 204 : fprintf (stream, "%s (0x" HOST_WIDE_INT_PRINT_HEX ") ",
9766 102 : type_as_string (BINFO_TYPE (binfo), TFF_PLAIN_IDENTIFIER),
9767 : (HOST_WIDE_INT) (uintptr_t) binfo);
9768 102 : if (binfo != igo)
9769 : {
9770 27 : fprintf (stream, "alternative-path\n");
9771 27 : return igo;
9772 : }
9773 75 : igo = TREE_CHAIN (binfo);
9774 :
9775 75 : fprintf (stream, HOST_WIDE_INT_PRINT_DEC,
9776 75 : tree_to_shwi (BINFO_OFFSET (binfo)));
9777 75 : if (is_empty_class (BINFO_TYPE (binfo)))
9778 18 : fprintf (stream, " empty");
9779 57 : else if (CLASSTYPE_NEARLY_EMPTY_P (BINFO_TYPE (binfo)))
9780 12 : fprintf (stream, " nearly-empty");
9781 75 : if (BINFO_VIRTUAL_P (binfo))
9782 27 : fprintf (stream, " virtual");
9783 75 : fprintf (stream, "\n");
9784 :
9785 75 : if (BINFO_PRIMARY_P (binfo))
9786 : {
9787 15 : indented = maybe_indent_hierarchy (stream, indent + 3, indented);
9788 30 : fprintf (stream, " primary-for %s (0x" HOST_WIDE_INT_PRINT_HEX ")",
9789 15 : type_as_string (BINFO_TYPE (BINFO_INHERITANCE_CHAIN (binfo)),
9790 : TFF_PLAIN_IDENTIFIER),
9791 15 : (HOST_WIDE_INT) (uintptr_t) BINFO_INHERITANCE_CHAIN (binfo));
9792 : }
9793 75 : if (BINFO_LOST_PRIMARY_P (binfo))
9794 : {
9795 9 : indented = maybe_indent_hierarchy (stream, indent + 3, indented);
9796 9 : fprintf (stream, " lost-primary");
9797 : }
9798 75 : if (indented)
9799 24 : fprintf (stream, "\n");
9800 :
9801 75 : if (!(flags & TDF_SLIM))
9802 : {
9803 75 : int indented = 0;
9804 :
9805 75 : if (BINFO_SUBVTT_INDEX (binfo))
9806 : {
9807 21 : indented = maybe_indent_hierarchy (stream, indent + 3, indented);
9808 42 : fprintf (stream, " subvttidx=%s",
9809 21 : expr_as_string (BINFO_SUBVTT_INDEX (binfo),
9810 : TFF_PLAIN_IDENTIFIER));
9811 : }
9812 75 : if (BINFO_VPTR_INDEX (binfo))
9813 : {
9814 30 : indented = maybe_indent_hierarchy (stream, indent + 3, indented);
9815 60 : fprintf (stream, " vptridx=%s",
9816 30 : expr_as_string (BINFO_VPTR_INDEX (binfo),
9817 : TFF_PLAIN_IDENTIFIER));
9818 : }
9819 75 : if (BINFO_VPTR_FIELD (binfo))
9820 : {
9821 27 : indented = maybe_indent_hierarchy (stream, indent + 3, indented);
9822 54 : fprintf (stream, " vbaseoffset=%s",
9823 27 : expr_as_string (BINFO_VPTR_FIELD (binfo),
9824 : TFF_PLAIN_IDENTIFIER));
9825 : }
9826 75 : if (BINFO_VTABLE (binfo))
9827 : {
9828 27 : indented = maybe_indent_hierarchy (stream, indent + 3, indented);
9829 27 : fprintf (stream, " vptr=%s",
9830 27 : expr_as_string (BINFO_VTABLE (binfo),
9831 : TFF_PLAIN_IDENTIFIER));
9832 : }
9833 :
9834 75 : if (indented)
9835 51 : fprintf (stream, "\n");
9836 : }
9837 :
9838 153 : for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
9839 78 : igo = dump_class_hierarchy_r (stream, flags, base_binfo, igo, indent + 2);
9840 :
9841 : return igo;
9842 : }
9843 :
9844 : /* Dump the BINFO hierarchy for T. */
9845 :
9846 : static void
9847 24 : dump_class_hierarchy_1 (FILE *stream, dump_flags_t flags, tree t)
9848 : {
9849 24 : fprintf (stream, "Class %s\n", type_as_string (t, TFF_PLAIN_IDENTIFIER));
9850 96 : fprintf (stream, " size=" HOST_WIDE_INT_PRINT_UNSIGNED " align=%u\n",
9851 24 : tree_to_shwi (TYPE_SIZE (t)) / BITS_PER_UNIT,
9852 24 : TYPE_ALIGN (t) / BITS_PER_UNIT);
9853 24 : if (tree as_base = CLASSTYPE_AS_BASE (t))
9854 96 : fprintf (stream, " base size=" HOST_WIDE_INT_PRINT_UNSIGNED
9855 : " base align=%u\n",
9856 24 : tree_to_shwi (TYPE_SIZE (as_base)) / BITS_PER_UNIT,
9857 24 : TYPE_ALIGN (as_base) / BITS_PER_UNIT);
9858 24 : dump_class_hierarchy_r (stream, flags, TYPE_BINFO (t), TYPE_BINFO (t), 0);
9859 24 : fprintf (stream, "\n");
9860 24 : }
9861 :
9862 : /* Debug interface to hierarchy dumping. */
9863 :
9864 : void
9865 0 : debug_class (tree t)
9866 : {
9867 0 : dump_class_hierarchy_1 (stderr, TDF_SLIM, t);
9868 0 : }
9869 :
9870 : static void
9871 49904436 : dump_class_hierarchy (tree t)
9872 : {
9873 49904436 : dump_flags_t flags;
9874 49904436 : if (FILE *stream = dump_begin (class_dump_id, &flags))
9875 : {
9876 24 : dump_class_hierarchy_1 (stream, flags, t);
9877 24 : dump_end (class_dump_id, stream);
9878 : }
9879 49904436 : }
9880 :
9881 : static void
9882 51 : dump_array (FILE * stream, tree decl)
9883 : {
9884 51 : tree value;
9885 51 : unsigned HOST_WIDE_INT ix;
9886 51 : HOST_WIDE_INT elt;
9887 51 : tree size = TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (decl)));
9888 :
9889 51 : elt = (tree_to_shwi (TYPE_SIZE (TREE_TYPE (TREE_TYPE (decl))))
9890 : / BITS_PER_UNIT);
9891 51 : fprintf (stream, "%s:", decl_as_string (decl, TFF_PLAIN_IDENTIFIER));
9892 51 : fprintf (stream, " %s entries",
9893 : expr_as_string (size_binop (PLUS_EXPR, size, size_one_node),
9894 : TFF_PLAIN_IDENTIFIER));
9895 51 : fprintf (stream, "\n");
9896 :
9897 519 : FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (DECL_INITIAL (decl)),
9898 : ix, value)
9899 417 : fprintf (stream, "%-4ld %s\n", (long)(ix * elt),
9900 : expr_as_string (value, TFF_PLAIN_IDENTIFIER));
9901 51 : }
9902 :
9903 : static void
9904 1989341 : dump_vtable (tree t, tree binfo, tree vtable)
9905 : {
9906 1989341 : dump_flags_t flags;
9907 1989341 : FILE *stream = dump_begin (class_dump_id, &flags);
9908 :
9909 1989341 : if (!stream)
9910 1989302 : return;
9911 :
9912 39 : if (!(flags & TDF_SLIM))
9913 : {
9914 39 : int ctor_vtbl_p = TYPE_BINFO (t) != binfo;
9915 :
9916 96 : fprintf (stream, "%s for %s",
9917 : ctor_vtbl_p ? "Construction vtable" : "Vtable",
9918 39 : type_as_string (BINFO_TYPE (binfo), TFF_PLAIN_IDENTIFIER));
9919 39 : if (ctor_vtbl_p)
9920 : {
9921 21 : if (!BINFO_VIRTUAL_P (binfo))
9922 6 : fprintf (stream, " (0x" HOST_WIDE_INT_PRINT_HEX " instance)",
9923 : (HOST_WIDE_INT) (uintptr_t) binfo);
9924 21 : fprintf (stream, " in %s", type_as_string (t, TFF_PLAIN_IDENTIFIER));
9925 : }
9926 39 : fprintf (stream, "\n");
9927 39 : dump_array (stream, vtable);
9928 39 : fprintf (stream, "\n");
9929 : }
9930 :
9931 39 : dump_end (class_dump_id, stream);
9932 : }
9933 :
9934 : static void
9935 180930 : dump_vtt (tree t, tree vtt)
9936 : {
9937 180930 : dump_flags_t flags;
9938 180930 : FILE *stream = dump_begin (class_dump_id, &flags);
9939 :
9940 180930 : if (!stream)
9941 180918 : return;
9942 :
9943 12 : if (!(flags & TDF_SLIM))
9944 : {
9945 12 : fprintf (stream, "VTT for %s\n",
9946 : type_as_string (t, TFF_PLAIN_IDENTIFIER));
9947 12 : dump_array (stream, vtt);
9948 12 : fprintf (stream, "\n");
9949 : }
9950 :
9951 12 : dump_end (class_dump_id, stream);
9952 : }
9953 :
9954 : /* Dump a function or thunk and its thunkees. */
9955 :
9956 : static void
9957 0 : dump_thunk (FILE *stream, int indent, tree thunk)
9958 : {
9959 0 : static const char spaces[] = " ";
9960 0 : tree name = DECL_NAME (thunk);
9961 0 : tree thunks;
9962 :
9963 0 : fprintf (stream, "%.*s%p %s %s", indent, spaces,
9964 : (void *)thunk,
9965 0 : !DECL_THUNK_P (thunk) ? "function"
9966 0 : : DECL_THIS_THUNK_P (thunk) ? "this-thunk" : "covariant-thunk",
9967 0 : name ? IDENTIFIER_POINTER (name) : "<unset>");
9968 0 : if (DECL_THUNK_P (thunk))
9969 : {
9970 0 : HOST_WIDE_INT fixed_adjust = THUNK_FIXED_OFFSET (thunk);
9971 0 : tree virtual_adjust = THUNK_VIRTUAL_OFFSET (thunk);
9972 :
9973 0 : fprintf (stream, " fixed=" HOST_WIDE_INT_PRINT_DEC, fixed_adjust);
9974 0 : if (!virtual_adjust)
9975 : /*NOP*/;
9976 0 : else if (DECL_THIS_THUNK_P (thunk))
9977 0 : fprintf (stream, " vcall=" HOST_WIDE_INT_PRINT_DEC,
9978 : tree_to_shwi (virtual_adjust));
9979 : else
9980 0 : fprintf (stream, " vbase=" HOST_WIDE_INT_PRINT_DEC "(%s)",
9981 0 : tree_to_shwi (BINFO_VPTR_FIELD (virtual_adjust)),
9982 0 : type_as_string (BINFO_TYPE (virtual_adjust), TFF_SCOPE));
9983 0 : if (THUNK_ALIAS (thunk))
9984 0 : fprintf (stream, " alias to %p", (void *)THUNK_ALIAS (thunk));
9985 : }
9986 0 : fprintf (stream, "\n");
9987 0 : for (thunks = DECL_THUNKS (thunk); thunks; thunks = TREE_CHAIN (thunks))
9988 0 : dump_thunk (stream, indent + 2, thunks);
9989 0 : }
9990 :
9991 : /* Dump the thunks for FN. */
9992 :
9993 : void
9994 0 : debug_thunks (tree fn)
9995 : {
9996 0 : dump_thunk (stderr, 0, fn);
9997 0 : }
9998 :
9999 : /* Virtual function table initialization. */
10000 :
10001 : /* Create all the necessary vtables for T and its base classes. */
10002 :
10003 : static void
10004 49904436 : finish_vtbls (tree t)
10005 : {
10006 49904436 : tree vbase;
10007 49904436 : vec<constructor_elt, va_gc> *v = NULL;
10008 49904436 : tree vtable = BINFO_VTABLE (TYPE_BINFO (t));
10009 :
10010 : /* We lay out the primary and secondary vtables in one contiguous
10011 : vtable. The primary vtable is first, followed by the non-virtual
10012 : secondary vtables in inheritance graph order. */
10013 49904436 : accumulate_vtbl_inits (TYPE_BINFO (t), TYPE_BINFO (t), TYPE_BINFO (t),
10014 : vtable, t, &v);
10015 :
10016 : /* Then come the virtual bases, also in inheritance graph order. */
10017 127240831 : for (vbase = TYPE_BINFO (t); vbase; vbase = TREE_CHAIN (vbase))
10018 : {
10019 77336395 : if (!BINFO_VIRTUAL_P (vbase))
10020 77141711 : continue;
10021 194684 : accumulate_vtbl_inits (vbase, vbase, TYPE_BINFO (t), vtable, t, &v);
10022 : }
10023 :
10024 49904436 : if (BINFO_VTABLE (TYPE_BINFO (t)))
10025 1741069 : initialize_vtable (TYPE_BINFO (t), v);
10026 49904436 : }
10027 :
10028 : /* Initialize the vtable for BINFO with the INITS. */
10029 :
10030 : static void
10031 1741069 : initialize_vtable (tree binfo, vec<constructor_elt, va_gc> *inits)
10032 : {
10033 1741069 : tree decl;
10034 :
10035 3482138 : layout_vtable_decl (binfo, vec_safe_length (inits));
10036 1741069 : decl = get_vtbl_decl_for_binfo (binfo);
10037 1741069 : initialize_artificial_var (decl, inits);
10038 1741069 : dump_vtable (BINFO_TYPE (binfo), binfo, decl);
10039 1741069 : }
10040 :
10041 : /* Build the VTT (virtual table table) for T.
10042 : A class requires a VTT if it has virtual bases.
10043 :
10044 : This holds
10045 : 1 - primary virtual pointer for complete object T
10046 : 2 - secondary VTTs for each direct non-virtual base of T which requires a
10047 : VTT
10048 : 3 - secondary virtual pointers for each direct or indirect base of T which
10049 : has virtual bases or is reachable via a virtual path from T.
10050 : 4 - secondary VTTs for each direct or indirect virtual base of T.
10051 :
10052 : Secondary VTTs look like complete object VTTs without part 4. */
10053 :
10054 : static void
10055 49904436 : build_vtt (tree t)
10056 : {
10057 49904436 : tree type;
10058 49904436 : tree vtt;
10059 49904436 : tree index;
10060 49904436 : vec<constructor_elt, va_gc> *inits;
10061 :
10062 : /* Build up the initializers for the VTT. */
10063 49904436 : inits = NULL;
10064 49904436 : index = size_zero_node;
10065 49904436 : build_vtt_inits (TYPE_BINFO (t), t, &inits, &index);
10066 :
10067 : /* If we didn't need a VTT, we're done. */
10068 49904436 : if (!inits)
10069 49723506 : return;
10070 :
10071 : /* Figure out the type of the VTT. */
10072 361860 : type = build_array_of_n_type (const_ptr_type_node,
10073 180930 : inits->length ());
10074 :
10075 : /* Now, build the VTT object itself. */
10076 180930 : vtt = build_vtable (t, mangle_vtt_for_type (t), type);
10077 180930 : initialize_artificial_var (vtt, inits);
10078 : /* Add the VTT to the vtables list. */
10079 180930 : DECL_CHAIN (vtt) = DECL_CHAIN (CLASSTYPE_VTABLES (t));
10080 180930 : DECL_CHAIN (CLASSTYPE_VTABLES (t)) = vtt;
10081 :
10082 180930 : dump_vtt (t, vtt);
10083 : }
10084 :
10085 : /* When building a secondary VTT, BINFO_VTABLE is set to a TREE_LIST with
10086 : PURPOSE the RTTI_BINFO, VALUE the real vtable pointer for this binfo,
10087 : and CHAIN the vtable pointer for this binfo after construction is
10088 : complete. VALUE can also be another BINFO, in which case we recurse. */
10089 :
10090 : static tree
10091 1094955 : binfo_ctor_vtable (tree binfo)
10092 : {
10093 1166942 : tree vt;
10094 :
10095 1166942 : while (1)
10096 : {
10097 1166942 : vt = BINFO_VTABLE (binfo);
10098 1166942 : if (TREE_CODE (vt) == TREE_LIST)
10099 734521 : vt = TREE_VALUE (vt);
10100 1166942 : if (TREE_CODE (vt) == TREE_BINFO)
10101 : binfo = vt;
10102 : else
10103 : break;
10104 : }
10105 :
10106 1094955 : return vt;
10107 : }
10108 :
10109 : /* Data for secondary VTT initialization. */
10110 : struct secondary_vptr_vtt_init_data
10111 : {
10112 : /* Is this the primary VTT? */
10113 : bool top_level_p;
10114 :
10115 : /* Current index into the VTT. */
10116 : tree index;
10117 :
10118 : /* Vector of initializers built up. */
10119 : vec<constructor_elt, va_gc> *inits;
10120 :
10121 : /* The type being constructed by this secondary VTT. */
10122 : tree type_being_constructed;
10123 : };
10124 :
10125 : /* Recursively build the VTT-initializer for BINFO (which is in the
10126 : hierarchy dominated by T). INITS points to the end of the initializer
10127 : list to date. INDEX is the VTT index where the next element will be
10128 : replaced. Iff BINFO is the binfo for T, this is the top level VTT (i.e.
10129 : not a subvtt for some base of T). When that is so, we emit the sub-VTTs
10130 : for virtual bases of T. When it is not so, we build the constructor
10131 : vtables for the BINFO-in-T variant. */
10132 :
10133 : static void
10134 50335914 : build_vtt_inits (tree binfo, tree t, vec<constructor_elt, va_gc> **inits,
10135 : tree *index)
10136 : {
10137 50335914 : int i;
10138 50335914 : tree b;
10139 50335914 : tree init;
10140 50335914 : secondary_vptr_vtt_init_data data;
10141 50335914 : int top_level_p = SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), t);
10142 :
10143 : /* We only need VTTs for subobjects with virtual bases. */
10144 50335914 : if (!CLASSTYPE_VBASECLASSES (BINFO_TYPE (binfo)))
10145 49906712 : return;
10146 :
10147 : /* We need to use a construction vtable if this is not the primary
10148 : VTT. */
10149 429202 : if (!top_level_p)
10150 : {
10151 248272 : build_ctor_vtbl_group (binfo, t);
10152 :
10153 : /* Record the offset in the VTT where this sub-VTT can be found. */
10154 248272 : BINFO_SUBVTT_INDEX (binfo) = *index;
10155 : }
10156 :
10157 : /* Add the address of the primary vtable for the complete object. */
10158 429202 : init = binfo_ctor_vtable (binfo);
10159 429202 : CONSTRUCTOR_APPEND_ELT (*inits, NULL_TREE, init);
10160 429202 : if (top_level_p)
10161 : {
10162 180930 : gcc_assert (!BINFO_VPTR_INDEX (binfo));
10163 180930 : BINFO_VPTR_INDEX (binfo) = *index;
10164 : }
10165 429202 : *index = size_binop (PLUS_EXPR, *index, TYPE_SIZE_UNIT (ptr_type_node));
10166 :
10167 : /* Recursively add the secondary VTTs for non-virtual bases. */
10168 929227 : for (i = 0; BINFO_BASE_ITERATE (binfo, i, b); ++i)
10169 500025 : if (!BINFO_VIRTUAL_P (b))
10170 236794 : build_vtt_inits (b, t, inits, index);
10171 :
10172 : /* Add secondary virtual pointers for all subobjects of BINFO with
10173 : either virtual bases or reachable along a virtual path, except
10174 : subobjects that are non-virtual primary bases. */
10175 429202 : data.top_level_p = top_level_p;
10176 429202 : data.index = *index;
10177 429202 : data.inits = *inits;
10178 429202 : data.type_being_constructed = BINFO_TYPE (binfo);
10179 :
10180 429202 : dfs_walk_once (binfo, dfs_build_secondary_vptr_vtt_inits, NULL, &data);
10181 :
10182 429202 : *index = data.index;
10183 :
10184 : /* data.inits might have grown as we added secondary virtual pointers.
10185 : Make sure our caller knows about the new vector. */
10186 429202 : *inits = data.inits;
10187 :
10188 429202 : if (top_level_p)
10189 : /* Add the secondary VTTs for virtual bases in inheritance graph
10190 : order. */
10191 971019 : for (b = TYPE_BINFO (BINFO_TYPE (binfo)); b; b = TREE_CHAIN (b))
10192 : {
10193 790089 : if (!BINFO_VIRTUAL_P (b))
10194 595405 : continue;
10195 :
10196 194684 : build_vtt_inits (b, t, inits, index);
10197 : }
10198 : else
10199 : /* Remove the ctor vtables we created. */
10200 248272 : dfs_walk_all (binfo, dfs_fixup_binfo_vtbls, NULL, binfo);
10201 : }
10202 :
10203 : /* Called from build_vtt_inits via dfs_walk. BINFO is the binfo for the base
10204 : in most derived. DATA is a SECONDARY_VPTR_VTT_INIT_DATA structure. */
10205 :
10206 : static tree
10207 1748351 : dfs_build_secondary_vptr_vtt_inits (tree binfo, void *data_)
10208 : {
10209 1748351 : secondary_vptr_vtt_init_data *data = (secondary_vptr_vtt_init_data *)data_;
10210 :
10211 : /* We don't care about bases that don't have vtables. */
10212 1748351 : if (!TYPE_VFIELD (BINFO_TYPE (binfo)))
10213 : return dfs_skip_bases;
10214 :
10215 : /* We're only interested in proper subobjects of the type being
10216 : constructed. */
10217 1724674 : if (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), data->type_being_constructed))
10218 : return NULL_TREE;
10219 :
10220 : /* We're only interested in bases with virtual bases or reachable
10221 : via a virtual path from the type being constructed. */
10222 2115596 : if (!(CLASSTYPE_VBASECLASSES (BINFO_TYPE (binfo))
10223 820124 : || binfo_via_virtual (binfo, data->type_being_constructed)))
10224 : return dfs_skip_bases;
10225 :
10226 : /* We're not interested in non-virtual primary bases. */
10227 1294946 : if (!BINFO_VIRTUAL_P (binfo) && BINFO_PRIMARY_P (binfo))
10228 : return NULL_TREE;
10229 :
10230 : /* Record the index where this secondary vptr can be found. */
10231 665753 : if (data->top_level_p)
10232 : {
10233 251491 : gcc_assert (!BINFO_VPTR_INDEX (binfo));
10234 251491 : BINFO_VPTR_INDEX (binfo) = data->index;
10235 :
10236 251491 : if (BINFO_VIRTUAL_P (binfo))
10237 : {
10238 : /* It's a primary virtual base, and this is not a
10239 : construction vtable. Find the base this is primary of in
10240 : the inheritance graph, and use that base's vtable
10241 : now. */
10242 201182 : while (BINFO_PRIMARY_P (binfo))
10243 9494 : binfo = BINFO_INHERITANCE_CHAIN (binfo);
10244 : }
10245 : }
10246 :
10247 : /* Add the initializer for the secondary vptr itself. */
10248 665753 : CONSTRUCTOR_APPEND_ELT (data->inits, NULL_TREE, binfo_ctor_vtable (binfo));
10249 :
10250 : /* Advance the vtt index. */
10251 665753 : data->index = size_binop (PLUS_EXPR, data->index,
10252 : TYPE_SIZE_UNIT (ptr_type_node));
10253 :
10254 665753 : return NULL_TREE;
10255 : }
10256 :
10257 : /* Called from build_vtt_inits via dfs_walk. After building
10258 : constructor vtables and generating the sub-vtt from them, we need
10259 : to restore the BINFO_VTABLES that were scribbled on. DATA is the
10260 : binfo of the base whose sub vtt was generated. */
10261 :
10262 : static tree
10263 1115662 : dfs_fixup_binfo_vtbls (tree binfo, void* data)
10264 : {
10265 1115662 : tree vtable = BINFO_VTABLE (binfo);
10266 :
10267 1115662 : if (!TYPE_CONTAINS_VPTR_P (BINFO_TYPE (binfo)))
10268 : /* If this class has no vtable, none of its bases do. */
10269 : return dfs_skip_bases;
10270 :
10271 1020342 : if (!vtable)
10272 : /* This might be a primary base, so have no vtable in this
10273 : hierarchy. */
10274 : return NULL_TREE;
10275 :
10276 : /* If we scribbled the construction vtable vptr into BINFO, clear it
10277 : out now. */
10278 701989 : if (TREE_CODE (vtable) == TREE_LIST
10279 701989 : && (TREE_PURPOSE (vtable) == (tree) data))
10280 662534 : BINFO_VTABLE (binfo) = TREE_CHAIN (vtable);
10281 :
10282 : return NULL_TREE;
10283 : }
10284 :
10285 : /* Build the construction vtable group for BINFO which is in the
10286 : hierarchy dominated by T. */
10287 :
10288 : static void
10289 248272 : build_ctor_vtbl_group (tree binfo, tree t)
10290 : {
10291 248272 : tree type;
10292 248272 : tree vtbl;
10293 248272 : tree id;
10294 248272 : tree vbase;
10295 248272 : vec<constructor_elt, va_gc> *v;
10296 :
10297 : /* See if we've already created this construction vtable group. */
10298 248272 : id = mangle_ctor_vtbl_for_type (t, binfo);
10299 248272 : if (get_global_binding (id))
10300 0 : return;
10301 :
10302 248272 : gcc_assert (!SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), t));
10303 : /* Build a version of VTBL (with the wrong type) for use in
10304 : constructing the addresses of secondary vtables in the
10305 : construction vtable group. */
10306 248272 : vtbl = build_vtable (t, id, ptr_type_node);
10307 :
10308 : /* Don't export construction vtables from shared libraries. Even on
10309 : targets that don't support hidden visibility, this tells
10310 : can_refer_decl_in_current_unit_p not to assume that it's safe to
10311 : access from a different compilation unit (bz 54314). */
10312 248272 : DECL_VISIBILITY (vtbl) = VISIBILITY_HIDDEN;
10313 248272 : DECL_VISIBILITY_SPECIFIED (vtbl) = true;
10314 :
10315 248272 : v = NULL;
10316 248272 : accumulate_vtbl_inits (binfo, TYPE_BINFO (TREE_TYPE (binfo)),
10317 : binfo, vtbl, t, &v);
10318 :
10319 : /* Add the vtables for each of our virtual bases using the vbase in T
10320 : binfo. */
10321 248272 : for (vbase = TYPE_BINFO (BINFO_TYPE (binfo));
10322 1210555 : vbase;
10323 962283 : vbase = TREE_CHAIN (vbase))
10324 : {
10325 962283 : tree b;
10326 :
10327 962283 : if (!BINFO_VIRTUAL_P (vbase))
10328 571605 : continue;
10329 390678 : b = copied_binfo (vbase, binfo);
10330 :
10331 390678 : accumulate_vtbl_inits (b, vbase, binfo, vtbl, t, &v);
10332 : }
10333 :
10334 : /* Figure out the type of the construction vtable. */
10335 248272 : type = build_array_of_n_type (vtable_entry_type, v->length ());
10336 248272 : layout_type (type);
10337 248272 : TREE_TYPE (vtbl) = type;
10338 248272 : DECL_SIZE (vtbl) = DECL_SIZE_UNIT (vtbl) = NULL_TREE;
10339 248272 : layout_decl (vtbl, 0);
10340 :
10341 : /* Initialize the construction vtable. */
10342 248272 : CLASSTYPE_VTABLES (t) = chainon (CLASSTYPE_VTABLES (t), vtbl);
10343 248272 : initialize_artificial_var (vtbl, v);
10344 248272 : dump_vtable (t, binfo, vtbl);
10345 : }
10346 :
10347 : /* Add the vtbl initializers for BINFO (and its bases other than
10348 : non-virtual primaries) to the list of INITS. BINFO is in the
10349 : hierarchy dominated by T. RTTI_BINFO is the binfo within T of
10350 : the constructor the vtbl inits should be accumulated for. (If this
10351 : is the complete object vtbl then RTTI_BINFO will be TYPE_BINFO (T).)
10352 : ORIG_BINFO is the binfo for this object within BINFO_TYPE (RTTI_BINFO).
10353 : BINFO is the active base equivalent of ORIG_BINFO in the inheritance
10354 : graph of T. Both BINFO and ORIG_BINFO will have the same BINFO_TYPE,
10355 : but are not necessarily the same in terms of layout. */
10356 :
10357 : static void
10358 54307255 : accumulate_vtbl_inits (tree binfo,
10359 : tree orig_binfo,
10360 : tree rtti_binfo,
10361 : tree vtbl,
10362 : tree t,
10363 : vec<constructor_elt, va_gc> **inits)
10364 : {
10365 54307255 : int i;
10366 54307255 : tree base_binfo;
10367 54307255 : int ctor_vtbl_p = !SAME_BINFO_TYPE_P (BINFO_TYPE (rtti_binfo), t);
10368 :
10369 54307255 : gcc_assert (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), BINFO_TYPE (orig_binfo)));
10370 :
10371 : /* If it doesn't have a vptr, we don't do anything. */
10372 54307255 : if (!TYPE_CONTAINS_VPTR_P (BINFO_TYPE (binfo)))
10373 : return;
10374 :
10375 : /* If we're building a construction vtable, we're not interested in
10376 : subobjects that don't require construction vtables. */
10377 5525592 : if (ctor_vtbl_p
10378 940675 : && !CLASSTYPE_VBASECLASSES (BINFO_TYPE (binfo))
10379 5990919 : && !binfo_via_virtual (orig_binfo, BINFO_TYPE (rtti_binfo)))
10380 : return;
10381 :
10382 : /* Build the initializers for the BINFO-in-T vtable. */
10383 5525391 : dfs_accumulate_vtbl_inits (binfo, orig_binfo, rtti_binfo, vtbl, t, inits);
10384 :
10385 : /* Walk the BINFO and its bases. We walk in preorder so that as we
10386 : initialize each vtable we can figure out at what offset the
10387 : secondary vtable lies from the primary vtable. We can't use
10388 : dfs_walk here because we need to iterate through bases of BINFO
10389 : and RTTI_BINFO simultaneously. */
10390 15386699 : for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
10391 : {
10392 : /* Skip virtual bases. */
10393 4335917 : if (BINFO_VIRTUAL_P (base_binfo))
10394 766732 : continue;
10395 7138370 : accumulate_vtbl_inits (base_binfo,
10396 3569185 : BINFO_BASE_BINFO (orig_binfo, i),
10397 : rtti_binfo, vtbl, t,
10398 : inits);
10399 : }
10400 : }
10401 :
10402 : /* Called from accumulate_vtbl_inits. Adds the initializers for the
10403 : BINFO vtable to L. */
10404 :
10405 : static void
10406 5525391 : dfs_accumulate_vtbl_inits (tree binfo,
10407 : tree orig_binfo,
10408 : tree rtti_binfo,
10409 : tree orig_vtbl,
10410 : tree t,
10411 : vec<constructor_elt, va_gc> **l)
10412 : {
10413 5525391 : tree vtbl = NULL_TREE;
10414 5525391 : int ctor_vtbl_p = !SAME_BINFO_TYPE_P (BINFO_TYPE (rtti_binfo), t);
10415 5525391 : int n_inits;
10416 :
10417 5525391 : if (ctor_vtbl_p
10418 6465865 : && BINFO_VIRTUAL_P (orig_binfo) && BINFO_PRIMARY_P (orig_binfo))
10419 : {
10420 : /* In the hierarchy of BINFO_TYPE (RTTI_BINFO), this is a
10421 : primary virtual base. If it is not the same primary in
10422 : the hierarchy of T, we'll need to generate a ctor vtable
10423 : for it, to place at its location in T. If it is the same
10424 : primary, we still need a VTT entry for the vtable, but it
10425 : should point to the ctor vtable for the base it is a
10426 : primary for within the sub-hierarchy of RTTI_BINFO.
10427 :
10428 : There are three possible cases:
10429 :
10430 : 1) We are in the same place.
10431 : 2) We are a primary base within a lost primary virtual base of
10432 : RTTI_BINFO.
10433 : 3) We are primary to something not a base of RTTI_BINFO. */
10434 :
10435 : tree b;
10436 : tree last = NULL_TREE;
10437 :
10438 : /* First, look through the bases we are primary to for RTTI_BINFO
10439 : or a virtual base. */
10440 : b = binfo;
10441 73444 : while (BINFO_PRIMARY_P (b))
10442 : {
10443 73028 : b = BINFO_INHERITANCE_CHAIN (b);
10444 73028 : last = b;
10445 144989 : if (BINFO_VIRTUAL_P (b) || b == rtti_binfo)
10446 71961 : goto found;
10447 : }
10448 : /* If we run out of primary links, keep looking down our
10449 : inheritance chain; we might be an indirect primary. */
10450 865 : for (b = last; b; b = BINFO_INHERITANCE_CHAIN (b))
10451 461 : if (BINFO_VIRTUAL_P (b) || b == rtti_binfo)
10452 : break;
10453 404 : found:
10454 :
10455 : /* If we found RTTI_BINFO, this is case 1. If we found a virtual
10456 : base B and it is a base of RTTI_BINFO, this is case 2. In
10457 : either case, we share our vtable with LAST, i.e. the
10458 : derived-most base within B of which we are a primary. */
10459 72377 : if (b == rtti_binfo
10460 72377 : || (b && binfo_for_vbase (BINFO_TYPE (b), BINFO_TYPE (rtti_binfo))))
10461 : /* Just set our BINFO_VTABLE to point to LAST, as we may not have
10462 : set LAST's BINFO_VTABLE yet. We'll extract the actual vptr in
10463 : binfo_ctor_vtable after everything's been set up. */
10464 : vtbl = last;
10465 :
10466 : /* Otherwise, this is case 3 and we get our own. */
10467 : }
10468 5453014 : else if (!BINFO_NEW_VTABLE_MARKED (orig_binfo))
10469 : return;
10470 :
10471 2656291 : n_inits = vec_safe_length (*l);
10472 :
10473 2656291 : if (!vtbl)
10474 : {
10475 2584838 : tree index;
10476 2584838 : int non_fn_entries;
10477 :
10478 : /* Add the initializer for this vtable. */
10479 2584838 : build_vtbl_initializer (binfo, orig_binfo, t, rtti_binfo,
10480 : &non_fn_entries, l);
10481 :
10482 : /* Figure out the position to which the VPTR should point. */
10483 2584838 : vtbl = build1 (ADDR_EXPR, vtbl_ptr_type_node, orig_vtbl);
10484 2584838 : index = size_binop (MULT_EXPR,
10485 : TYPE_SIZE_UNIT (vtable_entry_type),
10486 : size_int (non_fn_entries + n_inits));
10487 2584838 : vtbl = fold_build_pointer_plus (vtbl, index);
10488 : }
10489 :
10490 2656291 : if (ctor_vtbl_p)
10491 : /* For a construction vtable, we can't overwrite BINFO_VTABLE.
10492 : So, we make a TREE_LIST. Later, dfs_fixup_binfo_vtbls will
10493 : straighten this out. */
10494 662534 : BINFO_VTABLE (binfo) = tree_cons (rtti_binfo, vtbl, BINFO_VTABLE (binfo));
10495 1993757 : else if (BINFO_PRIMARY_P (binfo) && BINFO_VIRTUAL_P (binfo))
10496 : /* Throw away any unneeded intializers. */
10497 8067 : (*l)->truncate (n_inits);
10498 : else
10499 : /* For an ordinary vtable, set BINFO_VTABLE. */
10500 1985690 : BINFO_VTABLE (binfo) = vtbl;
10501 : }
10502 :
10503 : static GTY(()) tree abort_fndecl_addr;
10504 : static GTY(()) tree dvirt_fn;
10505 :
10506 : /* Construct the initializer for BINFO's virtual function table. BINFO
10507 : is part of the hierarchy dominated by T. If we're building a
10508 : construction vtable, the ORIG_BINFO is the binfo we should use to
10509 : find the actual function pointers to put in the vtable - but they
10510 : can be overridden on the path to most-derived in the graph that
10511 : ORIG_BINFO belongs. Otherwise,
10512 : ORIG_BINFO should be the same as BINFO. The RTTI_BINFO is the
10513 : BINFO that should be indicated by the RTTI information in the
10514 : vtable; it will be a base class of T, rather than T itself, if we
10515 : are building a construction vtable.
10516 :
10517 : The value returned is a TREE_LIST suitable for wrapping in a
10518 : CONSTRUCTOR to use as the DECL_INITIAL for a vtable. If
10519 : NON_FN_ENTRIES_P is not NULL, *NON_FN_ENTRIES_P is set to the
10520 : number of non-function entries in the vtable.
10521 :
10522 : It might seem that this function should never be called with a
10523 : BINFO for which BINFO_PRIMARY_P holds, the vtable for such a
10524 : base is always subsumed by a derived class vtable. However, when
10525 : we are building construction vtables, we do build vtables for
10526 : primary bases; we need these while the primary base is being
10527 : constructed. */
10528 :
10529 : static void
10530 2584838 : build_vtbl_initializer (tree binfo,
10531 : tree orig_binfo,
10532 : tree t,
10533 : tree rtti_binfo,
10534 : int* non_fn_entries_p,
10535 : vec<constructor_elt, va_gc> **inits)
10536 : {
10537 2584838 : tree v;
10538 2584838 : vtbl_init_data vid;
10539 2584838 : unsigned ix, jx;
10540 2584838 : tree vbinfo;
10541 2584838 : vec<tree, va_gc> *vbases;
10542 2584838 : constructor_elt *e;
10543 :
10544 : /* Initialize VID. */
10545 2584838 : memset (&vid, 0, sizeof (vid));
10546 2584838 : vid.binfo = binfo;
10547 2584838 : vid.derived = t;
10548 2584838 : vid.rtti_binfo = rtti_binfo;
10549 2584838 : vid.primary_vtbl_p = SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), t);
10550 2584838 : vid.ctor_vtbl_p = !SAME_BINFO_TYPE_P (BINFO_TYPE (rtti_binfo), t);
10551 2584838 : vid.generate_vcall_entries = true;
10552 : /* The first vbase or vcall offset is at index -3 in the vtable. */
10553 2584838 : vid.index = ssize_int(-3 * TARGET_VTABLE_DATA_ENTRY_DISTANCE);
10554 :
10555 : /* Add entries to the vtable for RTTI. */
10556 2584838 : build_rtti_vtbl_entries (binfo, &vid);
10557 :
10558 : /* Create an array for keeping track of the functions we've
10559 : processed. When we see multiple functions with the same
10560 : signature, we share the vcall offsets. */
10561 2584838 : vec_alloc (vid.fns, 32);
10562 : /* Add the vcall and vbase offset entries. */
10563 2584838 : build_vcall_and_vbase_vtbl_entries (binfo, &vid);
10564 :
10565 : /* Clear BINFO_VTABLE_PATH_MARKED; it's set by
10566 : build_vbase_offset_vtbl_entries. */
10567 2584838 : for (vbases = CLASSTYPE_VBASECLASSES (t), ix = 0;
10568 8956144 : vec_safe_iterate (vbases, ix, &vbinfo); ix++)
10569 6371306 : BINFO_VTABLE_PATH_MARKED (vbinfo) = 0;
10570 :
10571 : /* If the target requires padding between data entries, add that now. */
10572 2584838 : if (TARGET_VTABLE_DATA_ENTRY_DISTANCE > 1)
10573 : {
10574 : int n_entries = vec_safe_length (vid.inits);
10575 :
10576 : vec_safe_grow (vid.inits, TARGET_VTABLE_DATA_ENTRY_DISTANCE * n_entries,
10577 : true);
10578 :
10579 : /* Move data entries into their new positions and add padding
10580 : after the new positions. Iterate backwards so we don't
10581 : overwrite entries that we would need to process later. */
10582 : for (ix = n_entries - 1;
10583 : vid.inits->iterate (ix, &e);
10584 : ix--)
10585 : {
10586 : int j;
10587 : int new_position = (TARGET_VTABLE_DATA_ENTRY_DISTANCE * ix
10588 : + (TARGET_VTABLE_DATA_ENTRY_DISTANCE - 1));
10589 :
10590 : (*vid.inits)[new_position] = *e;
10591 :
10592 : for (j = 1; j < TARGET_VTABLE_DATA_ENTRY_DISTANCE; ++j)
10593 : {
10594 : constructor_elt *f = &(*vid.inits)[new_position - j];
10595 : f->index = NULL_TREE;
10596 : f->value = build1 (NOP_EXPR, vtable_entry_type,
10597 : null_pointer_node);
10598 : }
10599 : }
10600 : }
10601 :
10602 2584838 : if (non_fn_entries_p)
10603 5169676 : *non_fn_entries_p = vec_safe_length (vid.inits);
10604 :
10605 : /* The initializers for virtual functions were built up in reverse
10606 : order. Straighten them out and add them to the running list in one
10607 : step. */
10608 2584838 : jx = vec_safe_length (*inits);
10609 2584838 : vec_safe_grow (*inits, jx + vid.inits->length (), true);
10610 :
10611 2584838 : for (ix = vid.inits->length () - 1;
10612 10370134 : vid.inits->iterate (ix, &e);
10613 7785296 : ix--, jx++)
10614 7785296 : (**inits)[jx] = *e;
10615 :
10616 : /* Go through all the ordinary virtual functions, building up
10617 : initializers. */
10618 14101304 : for (v = BINFO_VIRTUALS (orig_binfo); v; v = TREE_CHAIN (v))
10619 : {
10620 11516466 : tree delta;
10621 11516466 : tree vcall_index;
10622 11516466 : tree fn, fn_original;
10623 11516466 : tree init = NULL_TREE;
10624 :
10625 11516466 : fn = BV_FN (v);
10626 11516466 : fn_original = fn;
10627 11516466 : if (DECL_THUNK_P (fn))
10628 : {
10629 476 : if (!DECL_NAME (fn))
10630 214 : finish_thunk (fn);
10631 476 : if (THUNK_ALIAS (fn))
10632 : {
10633 0 : fn = THUNK_ALIAS (fn);
10634 0 : BV_FN (v) = fn;
10635 : }
10636 476 : fn_original = THUNK_TARGET (fn);
10637 : }
10638 :
10639 : /* If the only definition of this function signature along our
10640 : primary base chain is from a lost primary, this vtable slot will
10641 : never be used, so just zero it out. This is important to avoid
10642 : requiring extra thunks which cannot be generated with the function.
10643 :
10644 : We first check this in update_vtable_entry_for_fn, so we handle
10645 : restored primary bases properly; we also need to do it here so we
10646 : zero out unused slots in ctor vtables, rather than filling them
10647 : with erroneous values (though harmless, apart from relocation
10648 : costs). */
10649 11516466 : if (BV_LOST_PRIMARY (v))
10650 745 : init = size_zero_node;
10651 :
10652 745 : if (! init)
10653 : {
10654 : /* Pull the offset for `this', and the function to call, out of
10655 : the list. */
10656 11515721 : delta = BV_DELTA (v);
10657 11515721 : vcall_index = BV_VCALL_INDEX (v);
10658 :
10659 11515721 : gcc_assert (TREE_CODE (delta) == INTEGER_CST);
10660 11515721 : gcc_assert (TREE_CODE (fn) == FUNCTION_DECL);
10661 :
10662 : /* You can't call an abstract virtual function; it's abstract.
10663 : So, we replace these functions with __pure_virtual. */
10664 11515721 : if (DECL_PURE_VIRTUAL_P (fn_original))
10665 : {
10666 758362 : fn = abort_fndecl;
10667 758362 : if (!TARGET_VTABLE_USES_DESCRIPTORS)
10668 : {
10669 758362 : if (abort_fndecl_addr == NULL)
10670 13785 : abort_fndecl_addr
10671 13785 : = fold_convert (vfunc_ptr_type_node,
10672 : build_fold_addr_expr (fn));
10673 758362 : init = abort_fndecl_addr;
10674 : }
10675 : }
10676 : /* Likewise for deleted virtuals. */
10677 10757359 : else if (DECL_DELETED_FN (fn_original))
10678 : {
10679 47 : if (!dvirt_fn)
10680 : {
10681 23 : tree name = get_identifier ("__cxa_deleted_virtual");
10682 23 : dvirt_fn = get_global_binding (name);
10683 23 : if (!dvirt_fn)
10684 23 : dvirt_fn = push_library_fn
10685 23 : (name,
10686 : build_function_type_list (void_type_node, NULL_TREE),
10687 : NULL_TREE, ECF_NORETURN | ECF_COLD);
10688 : }
10689 47 : fn = dvirt_fn;
10690 47 : if (!TARGET_VTABLE_USES_DESCRIPTORS)
10691 47 : init = fold_convert (vfunc_ptr_type_node,
10692 : build_fold_addr_expr (fn));
10693 : }
10694 : else
10695 : {
10696 10757312 : if (!integer_zerop (delta) || vcall_index)
10697 : {
10698 1015049 : fn = make_thunk (fn, /*this_adjusting=*/1,
10699 : delta, vcall_index);
10700 1015049 : if (!DECL_NAME (fn))
10701 473216 : finish_thunk (fn);
10702 : }
10703 : /* Take the address of the function, considering it to be of an
10704 : appropriate generic type. */
10705 10757312 : if (!TARGET_VTABLE_USES_DESCRIPTORS)
10706 10757312 : init = fold_convert (vfunc_ptr_type_node,
10707 : build_fold_addr_expr (fn));
10708 : /* Don't refer to a virtual destructor from a constructor
10709 : vtable or a vtable for an abstract class, since destroying
10710 : an object under construction is undefined behavior and we
10711 : don't want it to be considered a candidate for speculative
10712 : devirtualization. But do create the thunk for ABI
10713 : compliance. */
10714 10757312 : if (DECL_DESTRUCTOR_P (fn_original)
10715 10757312 : && (CLASSTYPE_PURE_VIRTUALS (DECL_CONTEXT (fn_original))
10716 4411702 : || orig_binfo != binfo))
10717 1312848 : init = size_zero_node;
10718 : }
10719 : }
10720 :
10721 : /* And add it to the chain of initializers. */
10722 11516466 : if (TARGET_VTABLE_USES_DESCRIPTORS)
10723 : {
10724 : int i;
10725 : if (init == size_zero_node)
10726 : for (i = 0; i < TARGET_VTABLE_USES_DESCRIPTORS; ++i)
10727 : CONSTRUCTOR_APPEND_ELT (*inits, NULL_TREE, init);
10728 : else
10729 : for (i = 0; i < TARGET_VTABLE_USES_DESCRIPTORS; ++i)
10730 : {
10731 : tree fdesc = build2 (FDESC_EXPR, vfunc_ptr_type_node,
10732 : fn, build_int_cst (NULL_TREE, i));
10733 : TREE_CONSTANT (fdesc) = 1;
10734 :
10735 : CONSTRUCTOR_APPEND_ELT (*inits, NULL_TREE, fdesc);
10736 : }
10737 : }
10738 : else
10739 11516466 : CONSTRUCTOR_APPEND_ELT (*inits, NULL_TREE, init);
10740 : }
10741 2584838 : }
10742 :
10743 : /* Adds to vid->inits the initializers for the vbase and vcall
10744 : offsets in BINFO, which is in the hierarchy dominated by T. */
10745 :
10746 : static void
10747 5537826 : build_vcall_and_vbase_vtbl_entries (tree binfo, vtbl_init_data* vid)
10748 : {
10749 5537826 : tree b;
10750 :
10751 : /* If this is a derived class, we must first create entries
10752 : corresponding to the primary base class. */
10753 5537826 : b = get_primary_binfo (binfo);
10754 5537826 : if (b)
10755 2952988 : build_vcall_and_vbase_vtbl_entries (b, vid);
10756 :
10757 : /* Add the vbase entries for this base. */
10758 5537826 : build_vbase_offset_vtbl_entries (binfo, vid);
10759 : /* Add the vcall entries for this base. */
10760 5537826 : build_vcall_offset_vtbl_entries (binfo, vid);
10761 5537826 : }
10762 :
10763 : /* Returns the initializers for the vbase offset entries in the vtable
10764 : for BINFO (which is part of the class hierarchy dominated by T), in
10765 : reverse order. VBASE_OFFSET_INDEX gives the vtable index
10766 : where the next vbase offset will go. */
10767 :
10768 : static void
10769 5537826 : build_vbase_offset_vtbl_entries (tree binfo, vtbl_init_data* vid)
10770 : {
10771 5537826 : tree vbase;
10772 5537826 : tree t;
10773 5537826 : tree non_primary_binfo;
10774 :
10775 : /* If there are no virtual baseclasses, then there is nothing to
10776 : do. */
10777 5537826 : if (!CLASSTYPE_VBASECLASSES (BINFO_TYPE (binfo)))
10778 : return;
10779 :
10780 913111 : t = vid->derived;
10781 :
10782 : /* We might be a primary base class. Go up the inheritance hierarchy
10783 : until we find the most derived class of which we are a primary base:
10784 : it is the offset of that which we need to use. */
10785 913111 : non_primary_binfo = binfo;
10786 1425637 : while (BINFO_INHERITANCE_CHAIN (non_primary_binfo))
10787 : {
10788 858723 : tree b;
10789 :
10790 : /* If we have reached a virtual base, then it must be a primary
10791 : base (possibly multi-level) of vid->binfo, or we wouldn't
10792 : have called build_vcall_and_vbase_vtbl_entries for it. But it
10793 : might be a lost primary, so just skip down to vid->binfo. */
10794 858723 : if (BINFO_VIRTUAL_P (non_primary_binfo))
10795 : {
10796 185278 : non_primary_binfo = vid->binfo;
10797 185278 : break;
10798 : }
10799 :
10800 673445 : b = BINFO_INHERITANCE_CHAIN (non_primary_binfo);
10801 673445 : if (get_primary_binfo (b) != non_primary_binfo)
10802 : break;
10803 : non_primary_binfo = b;
10804 : }
10805 :
10806 : /* Go through the virtual bases, adding the offsets. */
10807 913111 : for (vbase = TYPE_BINFO (BINFO_TYPE (binfo));
10808 5542392 : vbase;
10809 4629281 : vbase = TREE_CHAIN (vbase))
10810 : {
10811 4629281 : tree b;
10812 4629281 : tree delta;
10813 :
10814 4629281 : if (!BINFO_VIRTUAL_P (vbase))
10815 2133003 : continue;
10816 :
10817 : /* Find the instance of this virtual base in the complete
10818 : object. */
10819 2496278 : b = copied_binfo (vbase, binfo);
10820 :
10821 : /* If we've already got an offset for this virtual base, we
10822 : don't need another one. */
10823 2496278 : if (BINFO_VTABLE_PATH_MARKED (b))
10824 304100 : continue;
10825 2192178 : BINFO_VTABLE_PATH_MARKED (b) = 1;
10826 :
10827 : /* Figure out where we can find this vbase offset. */
10828 2192178 : delta = size_binop (MULT_EXPR,
10829 : vid->index,
10830 : fold_convert (ssizetype,
10831 : TYPE_SIZE_UNIT (vtable_entry_type)));
10832 2192178 : if (vid->primary_vtbl_p)
10833 194684 : BINFO_VPTR_FIELD (b) = delta;
10834 :
10835 2192178 : if (binfo != TYPE_BINFO (t))
10836 : /* The vbase offset had better be the same. */
10837 2134814 : gcc_assert (tree_int_cst_equal (delta, BINFO_VPTR_FIELD (vbase)));
10838 :
10839 : /* The next vbase will come at a more negative offset. */
10840 2192178 : vid->index = size_binop (MINUS_EXPR, vid->index,
10841 : ssize_int (TARGET_VTABLE_DATA_ENTRY_DISTANCE));
10842 :
10843 : /* The initializer is the delta from BINFO to this virtual base.
10844 : The vbase offsets go in reverse inheritance-graph order, and
10845 : we are walking in inheritance graph order so these end up in
10846 : the right order. */
10847 2192178 : delta = size_diffop_loc (input_location,
10848 2192178 : BINFO_OFFSET (b), BINFO_OFFSET (non_primary_binfo));
10849 :
10850 6821459 : CONSTRUCTOR_APPEND_ELT (vid->inits, NULL_TREE,
10851 : fold_build1_loc (input_location, NOP_EXPR,
10852 : vtable_entry_type, delta));
10853 : }
10854 : }
10855 :
10856 : /* Adds the initializers for the vcall offset entries in the vtable
10857 : for BINFO (which is part of the class hierarchy dominated by VID->DERIVED)
10858 : to VID->INITS. */
10859 :
10860 : static void
10861 5537826 : build_vcall_offset_vtbl_entries (tree binfo, vtbl_init_data* vid)
10862 : {
10863 : /* We only need these entries if this base is a virtual base. We
10864 : compute the indices -- but do not add to the vtable -- when
10865 : building the main vtable for a class. */
10866 5537826 : if (binfo == TYPE_BINFO (vid->derived)
10867 9334583 : || (BINFO_VIRTUAL_P (binfo)
10868 : /* If BINFO is RTTI_BINFO, then (since BINFO does not
10869 : correspond to VID->DERIVED), we are building a primary
10870 : construction virtual table. Since this is a primary
10871 : virtual table, we do not need the vcall offsets for
10872 : BINFO. */
10873 592367 : && binfo != vid->rtti_binfo))
10874 : {
10875 : /* We need a vcall offset for each of the virtual functions in this
10876 : vtable. For example:
10877 :
10878 : class A { virtual void f (); };
10879 : class B1 : virtual public A { virtual void f (); };
10880 : class B2 : virtual public A { virtual void f (); };
10881 : class C: public B1, public B2 { virtual void f (); };
10882 :
10883 : A C object has a primary base of B1, which has a primary base of A. A
10884 : C also has a secondary base of B2, which no longer has a primary base
10885 : of A. So the B2-in-C construction vtable needs a secondary vtable for
10886 : A, which will adjust the A* to a B2* to call f. We have no way of
10887 : knowing what (or even whether) this offset will be when we define B2,
10888 : so we store this "vcall offset" in the A sub-vtable and look it up in
10889 : a "virtual thunk" for B2::f.
10890 :
10891 : We need entries for all the functions in our primary vtable and
10892 : in our non-virtual bases' secondary vtables. */
10893 2320025 : vid->vbase = binfo;
10894 : /* If we are just computing the vcall indices -- but do not need
10895 : the actual entries -- not that. */
10896 2320025 : if (!BINFO_VIRTUAL_P (binfo))
10897 1741069 : vid->generate_vcall_entries = false;
10898 : /* Now, walk through the non-virtual bases, adding vcall offsets. */
10899 2320025 : add_vcall_offset_vtbl_entries_r (binfo, vid);
10900 : }
10901 5537826 : }
10902 :
10903 : /* Build vcall offsets, starting with those for BINFO. */
10904 :
10905 : static void
10906 6310788 : add_vcall_offset_vtbl_entries_r (tree binfo, vtbl_init_data* vid)
10907 : {
10908 6310788 : int i;
10909 6310788 : tree primary_binfo;
10910 6310788 : tree base_binfo;
10911 :
10912 : /* Don't walk into virtual bases -- except, of course, for the
10913 : virtual base for which we are building vcall offsets. Any
10914 : primary virtual base will have already had its offsets generated
10915 : through the recursion in build_vcall_and_vbase_vtbl_entries. */
10916 6310788 : if (BINFO_VIRTUAL_P (binfo) && vid->vbase != binfo)
10917 6310788 : return;
10918 :
10919 : /* If BINFO has a primary base, process it first. */
10920 5830686 : primary_binfo = get_primary_binfo (binfo);
10921 5830686 : if (primary_binfo)
10922 2904845 : add_vcall_offset_vtbl_entries_r (primary_binfo, vid);
10923 :
10924 : /* Add BINFO itself to the list. */
10925 5830686 : add_vcall_offset_vtbl_entries_1 (binfo, vid);
10926 :
10927 : /* Scan the non-primary bases of BINFO. */
10928 15651955 : for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
10929 3990583 : if (base_binfo != primary_binfo)
10930 1085918 : add_vcall_offset_vtbl_entries_r (base_binfo, vid);
10931 : }
10932 :
10933 : /* Called from build_vcall_offset_vtbl_entries_r. */
10934 :
10935 : static void
10936 5830686 : add_vcall_offset_vtbl_entries_1 (tree binfo, vtbl_init_data* vid)
10937 : {
10938 : /* Make entries for the rest of the virtuals. */
10939 5830686 : tree orig_fn;
10940 :
10941 : /* The ABI requires that the methods be processed in declaration
10942 : order. */
10943 5830686 : for (orig_fn = TYPE_FIELDS (BINFO_TYPE (binfo));
10944 199169759 : orig_fn;
10945 193339073 : orig_fn = DECL_CHAIN (orig_fn))
10946 193339073 : if (TREE_CODE (orig_fn) == FUNCTION_DECL && DECL_VINDEX (orig_fn))
10947 24981746 : add_vcall_offset (orig_fn, binfo, vid);
10948 5830686 : }
10949 :
10950 : /* Add a vcall offset entry for ORIG_FN to the vtable. */
10951 :
10952 : static void
10953 24981746 : add_vcall_offset (tree orig_fn, tree binfo, vtbl_init_data *vid)
10954 : {
10955 24981746 : size_t i;
10956 24981746 : tree vcall_offset;
10957 24981746 : tree derived_entry;
10958 :
10959 : /* If there is already an entry for a function with the same
10960 : signature as FN, then we do not need a second vcall offset.
10961 : Check the list of functions already present in the derived
10962 : class vtable. */
10963 68422460 : FOR_EACH_VEC_SAFE_ELT (vid->fns, i, derived_entry)
10964 : {
10965 59598047 : if (same_signature_p (derived_entry, orig_fn)
10966 : /* We only use one vcall offset for virtual destructors,
10967 : even though there are two virtual table entries. */
10968 112612099 : || (DECL_DESTRUCTOR_P (derived_entry)
10969 39063596 : && DECL_DESTRUCTOR_P (orig_fn)))
10970 24981746 : return;
10971 : }
10972 :
10973 : /* If we are building these vcall offsets as part of building
10974 : the vtable for the most derived class, remember the vcall
10975 : offset. */
10976 8824413 : if (vid->binfo == TYPE_BINFO (vid->derived))
10977 : {
10978 8403069 : tree_pair_s elt = {orig_fn, vid->index};
10979 8403069 : vec_safe_push (CLASSTYPE_VCALL_INDICES (vid->derived), elt);
10980 : }
10981 :
10982 : /* The next vcall offset will be found at a more negative
10983 : offset. */
10984 8824413 : vid->index = size_binop (MINUS_EXPR, vid->index,
10985 : ssize_int (TARGET_VTABLE_DATA_ENTRY_DISTANCE));
10986 :
10987 : /* Keep track of this function. */
10988 8824413 : vec_safe_push (vid->fns, orig_fn);
10989 :
10990 8824413 : if (vid->generate_vcall_entries)
10991 : {
10992 423442 : tree base;
10993 423442 : tree fn;
10994 :
10995 : /* Find the overriding function. */
10996 423442 : fn = find_final_overrider (vid->rtti_binfo, binfo, orig_fn);
10997 423442 : if (fn == error_mark_node)
10998 27 : vcall_offset = build_zero_cst (vtable_entry_type);
10999 : else
11000 : {
11001 423415 : base = TREE_VALUE (fn);
11002 :
11003 : /* The vbase we're working on is a primary base of
11004 : vid->binfo. But it might be a lost primary, so its
11005 : BINFO_OFFSET might be wrong, so we just use the
11006 : BINFO_OFFSET from vid->binfo. */
11007 1693660 : vcall_offset = size_diffop_loc (input_location,
11008 423415 : BINFO_OFFSET (base),
11009 423415 : BINFO_OFFSET (vid->binfo));
11010 423415 : vcall_offset = fold_build1_loc (input_location,
11011 : NOP_EXPR, vtable_entry_type,
11012 : vcall_offset);
11013 : }
11014 : /* Add the initializer to the vtable. */
11015 423442 : CONSTRUCTOR_APPEND_ELT (vid->inits, NULL_TREE, vcall_offset);
11016 : }
11017 : }
11018 :
11019 : /* Return vtbl initializers for the RTTI entries corresponding to the
11020 : BINFO's vtable. The RTTI entries should indicate the object given
11021 : by VID->rtti_binfo. */
11022 :
11023 : static void
11024 2584838 : build_rtti_vtbl_entries (tree binfo, vtbl_init_data* vid)
11025 : {
11026 2584838 : tree b;
11027 2584838 : tree t;
11028 2584838 : tree offset;
11029 2584838 : tree decl;
11030 2584838 : tree init;
11031 :
11032 2584838 : t = BINFO_TYPE (vid->rtti_binfo);
11033 :
11034 : /* To find the complete object, we will first convert to our most
11035 : primary base, and then add the offset in the vtbl to that value. */
11036 2584838 : b = most_primary_binfo (binfo);
11037 2584838 : offset = size_diffop_loc (input_location,
11038 2584838 : BINFO_OFFSET (vid->rtti_binfo), BINFO_OFFSET (b));
11039 :
11040 : /* The second entry is the address of the typeinfo object. */
11041 2584838 : if (flag_rtti)
11042 2581729 : decl = build_address (get_tinfo_decl (t));
11043 : else
11044 3109 : decl = integer_zero_node;
11045 :
11046 : /* Convert the declaration to a type that can be stored in the
11047 : vtable. */
11048 2584838 : init = build_nop (vfunc_ptr_type_node, decl);
11049 2584838 : CONSTRUCTOR_APPEND_ELT (vid->inits, NULL_TREE, init);
11050 :
11051 : /* Add the offset-to-top entry. It comes earlier in the vtable than
11052 : the typeinfo entry. Convert the offset to look like a
11053 : function pointer, so that we can put it in the vtable. */
11054 2584838 : init = build_nop (vfunc_ptr_type_node, offset);
11055 2584838 : CONSTRUCTOR_APPEND_ELT (vid->inits, NULL_TREE, init);
11056 2584838 : }
11057 :
11058 : /* TRUE iff TYPE is uniquely derived from PARENT. Ignores
11059 : accessibility. */
11060 :
11061 : bool
11062 2871 : uniquely_derived_from_p (tree parent, tree type)
11063 : {
11064 2871 : tree base = lookup_base (type, parent, ba_unique, NULL, tf_none);
11065 2871 : return base && base != error_mark_node;
11066 : }
11067 :
11068 : /* TRUE iff TYPE is publicly & uniquely derived from PARENT. */
11069 :
11070 : bool
11071 3742 : publicly_uniquely_derived_p (tree parent, tree type)
11072 : {
11073 3742 : tree base = lookup_base (type, parent, ba_ignore_scope | ba_check,
11074 : NULL, tf_none);
11075 3742 : return base && base != error_mark_node;
11076 : }
11077 :
11078 : /* TRUE iff TYPE is publicly & virtually derived from PARENT. */
11079 :
11080 : bool
11081 186617 : publicly_virtually_derived_p (tree parent, tree type)
11082 : {
11083 186617 : tree base = lookup_base (type, parent,
11084 : ba_ignore_scope | ba_check | ba_require_virtual,
11085 : NULL, tf_none);
11086 186617 : return base && base != error_mark_node;
11087 : }
11088 :
11089 : /* CTX1 and CTX2 are declaration contexts. Return the innermost common
11090 : class between them, if any. */
11091 :
11092 : tree
11093 9406 : common_enclosing_class (tree ctx1, tree ctx2)
11094 : {
11095 9406 : if (!TYPE_P (ctx1) || !TYPE_P (ctx2))
11096 : return NULL_TREE;
11097 9406 : gcc_assert (ctx1 == TYPE_MAIN_VARIANT (ctx1)
11098 : && ctx2 == TYPE_MAIN_VARIANT (ctx2));
11099 9406 : if (ctx1 == ctx2)
11100 : return ctx1;
11101 267 : for (tree t = ctx1; TYPE_P (t); t = TYPE_CONTEXT (t))
11102 195 : TYPE_MARKED_P (t) = true;
11103 129 : tree found = NULL_TREE;
11104 129 : for (tree t = ctx2; TYPE_P (t); t = TYPE_CONTEXT (t))
11105 129 : if (TYPE_MARKED_P (t))
11106 : {
11107 : found = t;
11108 : break;
11109 : }
11110 267 : for (tree t = ctx1; TYPE_P (t); t = TYPE_CONTEXT (t))
11111 195 : TYPE_MARKED_P (t) = false;
11112 : return found;
11113 : }
11114 :
11115 : #include "gt-cp-class.h"
|