Branch data Line data Source code
1 : : /* Functions related to building -*- C++ -*- classes and their related objects.
2 : : Copyright (C) 1987-2024 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 : :
41 : : /* Id for dumping the class hierarchy. */
42 : : int class_dump_id;
43 : :
44 : : /* The number of nested classes being processed. If we are not in the
45 : : scope of any class, this is zero. */
46 : :
47 : : int current_class_depth;
48 : :
49 : : /* In order to deal with nested classes, we keep a stack of classes.
50 : : The topmost entry is the innermost class, and is the entry at index
51 : : CURRENT_CLASS_DEPTH */
52 : :
53 : : typedef struct class_stack_node {
54 : : /* The name of the class. */
55 : : tree name;
56 : :
57 : : /* The _TYPE node for the class. */
58 : : tree type;
59 : :
60 : : /* The access specifier pending for new declarations in the scope of
61 : : this class. */
62 : : tree access;
63 : :
64 : : /* If were defining TYPE, the names used in this class. */
65 : : splay_tree names_used;
66 : :
67 : : /* Nonzero if this class is no longer open, because of a call to
68 : : push_to_top_level. */
69 : : size_t hidden;
70 : : }* class_stack_node_t;
71 : :
72 : : struct vtbl_init_data
73 : : {
74 : : /* The base for which we're building initializers. */
75 : : tree binfo;
76 : : /* The type of the most-derived type. */
77 : : tree derived;
78 : : /* The binfo for the dynamic type. This will be TYPE_BINFO (derived),
79 : : unless ctor_vtbl_p is true. */
80 : : tree rtti_binfo;
81 : : /* The negative-index vtable initializers built up so far. These
82 : : are in order from least negative index to most negative index. */
83 : : vec<constructor_elt, va_gc> *inits;
84 : : /* The binfo for the virtual base for which we're building
85 : : vcall offset initializers. */
86 : : tree vbase;
87 : : /* The functions in vbase for which we have already provided vcall
88 : : offsets. */
89 : : vec<tree, va_gc> *fns;
90 : : /* The vtable index of the next vcall or vbase offset. */
91 : : tree index;
92 : : /* Nonzero if we are building the initializer for the primary
93 : : vtable. */
94 : : int primary_vtbl_p;
95 : : /* Nonzero if we are building the initializer for a construction
96 : : vtable. */
97 : : int ctor_vtbl_p;
98 : : /* True when adding vcall offset entries to the vtable. False when
99 : : merely computing the indices. */
100 : : bool generate_vcall_entries;
101 : : };
102 : :
103 : : /* The type of a function passed to walk_subobject_offsets. */
104 : : typedef int (*subobject_offset_fn) (tree, tree, splay_tree);
105 : :
106 : : /* The stack itself. This is a dynamically resized array. The
107 : : number of elements allocated is CURRENT_CLASS_STACK_SIZE. */
108 : : static int current_class_stack_size;
109 : : static class_stack_node_t current_class_stack;
110 : :
111 : : /* The size of the largest empty class seen in this translation unit. */
112 : : static GTY (()) tree sizeof_biggest_empty_class;
113 : :
114 : : static tree get_vfield_name (tree);
115 : : static void finish_struct_anon (tree);
116 : : static tree get_vtable_name (tree);
117 : : static void get_basefndecls (tree, tree, vec<tree> *);
118 : : static int build_primary_vtable (tree, tree);
119 : : static int build_secondary_vtable (tree);
120 : : static void finish_vtbls (tree);
121 : : static void modify_vtable_entry (tree, tree, tree, tree, tree *);
122 : : static void finish_struct_bits (tree);
123 : : static int alter_access (tree, tree, tree);
124 : : static void handle_using_decl (tree, tree);
125 : : static tree dfs_modify_vtables (tree, void *);
126 : : static tree modify_all_vtables (tree, tree);
127 : : static void determine_primary_bases (tree);
128 : : static void maybe_warn_about_overly_private_class (tree);
129 : : static void add_implicitly_declared_members (tree, tree*, int, int);
130 : : static tree fixed_type_or_null (tree, int *, int *);
131 : : static tree build_simple_base_path (tree expr, tree binfo);
132 : : static void build_vtbl_initializer (tree, tree, tree, tree, int *,
133 : : vec<constructor_elt, va_gc> **);
134 : : static bool check_bitfield_decl (tree);
135 : : static bool check_field_decl (tree, tree, int *, int *);
136 : : static void check_field_decls (tree, tree *, int *, int *);
137 : : static void build_base_fields (record_layout_info, splay_tree, tree *);
138 : : static void check_methods (tree);
139 : : static bool accessible_nvdtor_p (tree);
140 : :
141 : : /* Used by find_flexarrays and related functions. */
142 : : struct flexmems_t;
143 : : static void diagnose_flexarrays (tree, const flexmems_t *);
144 : : static void find_flexarrays (tree, flexmems_t *, bool = false,
145 : : tree = NULL_TREE, tree = NULL_TREE);
146 : : static void check_flexarrays (tree, flexmems_t * = NULL, bool = false);
147 : : static void check_bases (tree, int *, int *);
148 : : static void check_bases_and_members (tree);
149 : : static tree create_vtable_ptr (tree, tree *);
150 : : static void include_empty_classes (record_layout_info);
151 : : static void layout_class_type (tree, tree *);
152 : : static void propagate_binfo_offsets (tree, tree);
153 : : static void layout_virtual_bases (record_layout_info, splay_tree);
154 : : static void build_vbase_offset_vtbl_entries (tree, vtbl_init_data *);
155 : : static void add_vcall_offset_vtbl_entries_r (tree, vtbl_init_data *);
156 : : static void add_vcall_offset_vtbl_entries_1 (tree, vtbl_init_data *);
157 : : static void build_vcall_offset_vtbl_entries (tree, vtbl_init_data *);
158 : : static void add_vcall_offset (tree, tree, vtbl_init_data *);
159 : : static void layout_vtable_decl (tree, int);
160 : : static tree dfs_find_final_overrider_pre (tree, void *);
161 : : static tree dfs_find_final_overrider_post (tree, void *);
162 : : static tree find_final_overrider (tree, tree, tree);
163 : : static int make_new_vtable (tree, tree);
164 : : static tree get_primary_binfo (tree);
165 : : static int maybe_indent_hierarchy (FILE *, int, int);
166 : : static tree dump_class_hierarchy_r (FILE *, dump_flags_t, tree, tree, int);
167 : : static void dump_class_hierarchy (tree);
168 : : static void dump_class_hierarchy_1 (FILE *, dump_flags_t, tree);
169 : : static void dump_array (FILE *, tree);
170 : : static void dump_vtable (tree, tree, tree);
171 : : static void dump_vtt (tree, tree);
172 : : static void dump_thunk (FILE *, int, tree);
173 : : static tree build_vtable (tree, tree, tree);
174 : : static void initialize_vtable (tree, vec<constructor_elt, va_gc> *);
175 : : static void layout_nonempty_base_or_field (record_layout_info,
176 : : tree, tree, splay_tree);
177 : : static void accumulate_vtbl_inits (tree, tree, tree, tree, tree,
178 : : vec<constructor_elt, va_gc> **);
179 : : static void dfs_accumulate_vtbl_inits (tree, tree, tree, tree, tree,
180 : : vec<constructor_elt, va_gc> **);
181 : : static void build_rtti_vtbl_entries (tree, vtbl_init_data *);
182 : : static void build_vcall_and_vbase_vtbl_entries (tree, vtbl_init_data *);
183 : : static void clone_constructors_and_destructors (tree);
184 : : static void update_vtable_entry_for_fn (tree, tree, tree, tree *, unsigned);
185 : : static void build_ctor_vtbl_group (tree, tree);
186 : : static void build_vtt (tree);
187 : : static tree binfo_ctor_vtable (tree);
188 : : static void build_vtt_inits (tree, tree, vec<constructor_elt, va_gc> **,
189 : : tree *);
190 : : static tree dfs_build_secondary_vptr_vtt_inits (tree, void *);
191 : : static tree dfs_fixup_binfo_vtbls (tree, void *);
192 : : static int record_subobject_offset (tree, tree, splay_tree);
193 : : static int check_subobject_offset (tree, tree, splay_tree);
194 : : static int walk_subobject_offsets (tree, subobject_offset_fn,
195 : : tree, splay_tree, tree, int);
196 : : static int layout_conflict_p (tree, tree, splay_tree, int);
197 : : static int splay_tree_compare_integer_csts (splay_tree_key k1,
198 : : splay_tree_key k2);
199 : : static void maybe_warn_about_inaccessible_bases (tree);
200 : : static bool type_requires_array_cookie (tree);
201 : : static bool base_derived_from (tree, tree);
202 : : static int empty_base_at_nonzero_offset_p (tree, tree, splay_tree);
203 : : static tree end_of_base (tree);
204 : : static tree get_vcall_index (tree, tree);
205 : : static bool type_maybe_constexpr_default_constructor (tree);
206 : : static bool type_maybe_constexpr_destructor (tree);
207 : : static bool field_poverlapping_p (tree);
208 : : static void propagate_class_warmth_attribute (tree);
209 : :
210 : : /* Set CURRENT_ACCESS_SPECIFIER based on the protection of DECL. */
211 : :
212 : : void
213 : 162289835 : set_current_access_from_decl (tree decl)
214 : : {
215 : 162289835 : if (TREE_PRIVATE (decl))
216 : 14984018 : current_access_specifier = access_private_node;
217 : 147305817 : else if (TREE_PROTECTED (decl))
218 : 10680249 : current_access_specifier = access_protected_node;
219 : : else
220 : 136625568 : current_access_specifier = access_public_node;
221 : 162289835 : }
222 : :
223 : : /* Return a COND_EXPR that executes TRUE_STMT if this execution of the
224 : : 'structor is in charge of 'structing virtual bases, or FALSE_STMT
225 : : otherwise. */
226 : :
227 : : tree
228 : 174643 : build_if_in_charge (tree true_stmt, tree false_stmt)
229 : : {
230 : 174643 : gcc_assert (DECL_HAS_IN_CHARGE_PARM_P (current_function_decl));
231 : 349286 : tree cmp = build2 (NE_EXPR, boolean_type_node,
232 : 174643 : current_in_charge_parm, integer_zero_node);
233 : 174643 : tree type = unlowered_expr_type (true_stmt);
234 : 174643 : if (VOID_TYPE_P (type))
235 : 16590 : type = unlowered_expr_type (false_stmt);
236 : 174643 : tree cond = build3 (COND_EXPR, type,
237 : : cmp, true_stmt, false_stmt);
238 : 174643 : return cond;
239 : : }
240 : :
241 : : /* Convert to or from a base subobject. EXPR is an expression of type
242 : : `A' or `A*', an expression of type `B' or `B*' is returned. To
243 : : convert A to a base B, CODE is PLUS_EXPR and BINFO is the binfo for
244 : : the B base instance within A. To convert base A to derived B, CODE
245 : : is MINUS_EXPR and BINFO is the binfo for the A instance within B.
246 : : In this latter case, A must not be a morally virtual base of B.
247 : : NONNULL is true if EXPR is known to be non-NULL (this is only
248 : : needed when EXPR is of pointer type). CV qualifiers are preserved
249 : : from EXPR. */
250 : :
251 : : tree
252 : 41712866 : build_base_path (enum tree_code code,
253 : : tree expr,
254 : : tree binfo,
255 : : int nonnull,
256 : : tsubst_flags_t complain)
257 : : {
258 : 41712866 : tree v_binfo = NULL_TREE;
259 : 41712866 : tree d_binfo = NULL_TREE;
260 : 41712866 : tree probe;
261 : 41712866 : tree offset;
262 : 41712866 : tree target_type;
263 : 41712866 : tree null_test = NULL;
264 : 41712866 : tree ptr_target_type;
265 : 41712866 : int fixed_type_p;
266 : 41712866 : int want_pointer = TYPE_PTR_P (TREE_TYPE (expr));
267 : 41712866 : bool has_empty = false;
268 : 41712866 : bool virtual_access;
269 : 41712866 : bool rvalue = false;
270 : :
271 : 41712866 : if (expr == error_mark_node || binfo == error_mark_node || !binfo)
272 : : return error_mark_node;
273 : :
274 : 90071846 : for (probe = binfo; probe; probe = BINFO_INHERITANCE_CHAIN (probe))
275 : : {
276 : 48358998 : d_binfo = probe;
277 : 48358998 : if (is_empty_class (BINFO_TYPE (probe)))
278 : 7321687 : has_empty = true;
279 : 96623715 : if (!v_binfo && BINFO_VIRTUAL_P (probe))
280 : : v_binfo = probe;
281 : : }
282 : :
283 : 41712848 : probe = TYPE_MAIN_VARIANT (TREE_TYPE (expr));
284 : 41712848 : if (want_pointer)
285 : 38901189 : probe = TYPE_MAIN_VARIANT (TREE_TYPE (probe));
286 : 41712848 : if (dependent_type_p (probe))
287 : 273415 : if (tree open = currently_open_class (probe))
288 : 41712848 : probe = open;
289 : :
290 : 41712848 : if (code == PLUS_EXPR
291 : 81412562 : && !SAME_BINFO_TYPE_P (BINFO_TYPE (d_binfo), probe))
292 : : {
293 : : /* This can happen when adjust_result_of_qualified_name_lookup can't
294 : : find a unique base binfo in a call to a member function. We
295 : : couldn't give the diagnostic then since we might have been calling
296 : : a static member function, so we do it now. In other cases, eg.
297 : : during error recovery (c++/71979), we may not have a base at all. */
298 : 7 : if (complain & tf_error)
299 : : {
300 : 7 : tree base = lookup_base (probe, BINFO_TYPE (d_binfo),
301 : : ba_unique, NULL, complain);
302 : 7 : gcc_assert (base == error_mark_node || !base);
303 : : }
304 : 7 : return error_mark_node;
305 : : }
306 : :
307 : 43725975 : gcc_assert ((code == MINUS_EXPR
308 : : && SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), probe))
309 : : || code == PLUS_EXPR);
310 : :
311 : 41712841 : if (binfo == d_binfo)
312 : : /* Nothing to do. */
313 : : return expr;
314 : :
315 : 6275883 : if (code == MINUS_EXPR && v_binfo)
316 : : {
317 : 15 : if (complain & tf_error)
318 : : {
319 : 11 : if (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), BINFO_TYPE (v_binfo)))
320 : : {
321 : 11 : if (want_pointer)
322 : 22 : error ("cannot convert from pointer to base class %qT to "
323 : : "pointer to derived class %qT because the base is "
324 : 11 : "virtual", BINFO_TYPE (binfo), BINFO_TYPE (d_binfo));
325 : : else
326 : 0 : error ("cannot convert from base class %qT to derived "
327 : : "class %qT because the base is virtual",
328 : 0 : BINFO_TYPE (binfo), BINFO_TYPE (d_binfo));
329 : : }
330 : : else
331 : : {
332 : 0 : if (want_pointer)
333 : 0 : error ("cannot convert from pointer to base class %qT to "
334 : : "pointer to derived class %qT via virtual base %qT",
335 : 0 : BINFO_TYPE (binfo), BINFO_TYPE (d_binfo),
336 : 0 : BINFO_TYPE (v_binfo));
337 : : else
338 : 0 : error ("cannot convert from base class %qT to derived "
339 : 0 : "class %qT via virtual base %qT", BINFO_TYPE (binfo),
340 : 0 : BINFO_TYPE (d_binfo), BINFO_TYPE (v_binfo));
341 : : }
342 : : }
343 : 15 : return error_mark_node;
344 : : }
345 : :
346 : 12551736 : bool uneval = (cp_unevaluated_operand != 0
347 : 6021075 : || processing_template_decl
348 : 11976337 : || in_template_context);
349 : :
350 : : /* For a non-pointer simple base reference, express it as a COMPONENT_REF
351 : : without taking its address (and so causing lambda capture, 91933). */
352 : 6275868 : if (code == PLUS_EXPR && !v_binfo && !want_pointer && !has_empty && !uneval)
353 : 1787733 : return build_simple_base_path (expr, binfo);
354 : :
355 : 4488135 : if (!want_pointer)
356 : : {
357 : 1013439 : rvalue = !lvalue_p (expr);
358 : : /* This must happen before the call to save_expr. */
359 : 1013439 : expr = cp_build_addr_expr (expr, complain);
360 : : }
361 : : else
362 : 3474696 : expr = mark_rvalue_use (expr);
363 : :
364 : 4488135 : offset = BINFO_OFFSET (binfo);
365 : 4488135 : fixed_type_p = resolves_to_fixed_type_p (expr, &nonnull);
366 : 8976270 : target_type = code == PLUS_EXPR ? BINFO_TYPE (binfo) : BINFO_TYPE (d_binfo);
367 : : /* TARGET_TYPE has been extracted from BINFO, and, is therefore always
368 : : cv-unqualified. Extract the cv-qualifiers from EXPR so that the
369 : : expression returned matches the input. */
370 : 4488135 : target_type = cp_build_qualified_type
371 : 4488135 : (target_type, cp_type_quals (TREE_TYPE (TREE_TYPE (expr))));
372 : 4488135 : ptr_target_type = build_pointer_type (target_type);
373 : :
374 : : /* Do we need to look in the vtable for the real offset? */
375 : 4488135 : virtual_access = (v_binfo && fixed_type_p <= 0);
376 : :
377 : : /* Don't bother with the calculations inside sizeof; they'll ICE if the
378 : : source type is incomplete and the pointer value doesn't matter. In a
379 : : template (even in instantiate_non_dependent_expr), we don't have vtables
380 : : set up properly yet, and the value doesn't matter there either; we're
381 : : just interested in the result of overload resolution. */
382 : 4488135 : if (uneval)
383 : : {
384 : 575402 : expr = build_nop (ptr_target_type, expr);
385 : 575402 : goto indout;
386 : : }
387 : :
388 : 3912733 : if (!COMPLETE_TYPE_P (probe))
389 : : {
390 : 6 : if (complain & tf_error)
391 : 18 : error ("cannot convert from %qT to base class %qT because %qT is "
392 : 6 : "incomplete", BINFO_TYPE (d_binfo), BINFO_TYPE (binfo),
393 : 6 : BINFO_TYPE (d_binfo));
394 : 6 : return error_mark_node;
395 : : }
396 : :
397 : : /* If we're in an NSDMI, we don't have the full constructor context yet
398 : : that we need for converting to a virtual base, so just build a stub
399 : : CONVERT_EXPR and expand it later in bot_replace. */
400 : 3912727 : if (virtual_access && fixed_type_p < 0
401 : 3912727 : && current_scope () != current_function_decl)
402 : : {
403 : 33 : expr = build1 (CONVERT_EXPR, ptr_target_type, expr);
404 : 33 : CONVERT_EXPR_VBASE_PATH (expr) = true;
405 : 33 : goto indout;
406 : : }
407 : :
408 : : /* Do we need to check for a null pointer? */
409 : 3912694 : if (want_pointer && !nonnull)
410 : : {
411 : : /* If we know the conversion will not actually change the value
412 : : of EXPR, then we can avoid testing the expression for NULL.
413 : : We have to avoid generating a COMPONENT_REF for a base class
414 : : field, because other parts of the compiler know that such
415 : : expressions are always non-NULL. */
416 : 82388 : if (!virtual_access && integer_zerop (offset))
417 : 80124 : return build_nop (ptr_target_type, expr);
418 : 2264 : null_test = error_mark_node;
419 : : }
420 : :
421 : : /* Protect against multiple evaluation if necessary. */
422 : 3832570 : if (TREE_SIDE_EFFECTS (expr) && (null_test || virtual_access))
423 : 740 : expr = save_expr (expr);
424 : :
425 : : /* Store EXPR and build the real null test just before returning. */
426 : 3832570 : if (null_test)
427 : 2264 : null_test = expr;
428 : :
429 : : /* If this is a simple base reference, express it as a COMPONENT_REF. */
430 : 3832570 : if (code == PLUS_EXPR && !virtual_access
431 : : /* We don't build base fields for empty bases, and they aren't very
432 : : interesting to the optimizers anyway. */
433 : 3717994 : && !has_empty)
434 : : {
435 : 2072087 : expr = cp_build_fold_indirect_ref (expr);
436 : 2072087 : expr = build_simple_base_path (expr, binfo);
437 : 2072087 : if (rvalue && lvalue_p (expr))
438 : 0 : expr = move (expr);
439 : 2072087 : if (want_pointer)
440 : 2070194 : expr = build_address (expr);
441 : 2072087 : target_type = TREE_TYPE (expr);
442 : 2072087 : goto out;
443 : : }
444 : :
445 : 1760483 : if (virtual_access)
446 : : {
447 : : /* Going via virtual base V_BINFO. We need the static offset
448 : : from V_BINFO to BINFO, and the dynamic offset from D_BINFO to
449 : : V_BINFO. That offset is an entry in D_BINFO's vtable. */
450 : 73657 : tree v_offset;
451 : :
452 : 73657 : if (fixed_type_p < 0 && in_base_initializer)
453 : : {
454 : : /* In a base member initializer, we cannot rely on the
455 : : vtable being set up. We have to indirect via the
456 : : vtt_parm. */
457 : 8 : tree t;
458 : :
459 : 8 : t = TREE_TYPE (TYPE_VFIELD (current_class_type));
460 : 8 : t = build_pointer_type (t);
461 : 8 : v_offset = fold_convert (t, current_vtt_parm);
462 : 8 : v_offset = cp_build_fold_indirect_ref (v_offset);
463 : 8 : }
464 : : else
465 : : {
466 : 73649 : tree t = expr;
467 : 73649 : if (sanitize_flags_p (SANITIZE_VPTR)
468 : 73649 : && fixed_type_p == 0)
469 : : {
470 : 40 : t = cp_ubsan_maybe_instrument_cast_to_vbase (input_location,
471 : : probe, expr);
472 : 40 : if (t == NULL_TREE)
473 : 73609 : t = expr;
474 : : }
475 : 73649 : v_offset = build_vfield_ref (cp_build_fold_indirect_ref (t),
476 : 73649 : TREE_TYPE (TREE_TYPE (expr)));
477 : : }
478 : :
479 : 73657 : if (v_offset == error_mark_node)
480 : : return error_mark_node;
481 : :
482 : 73657 : v_offset = fold_build_pointer_plus (v_offset, BINFO_VPTR_FIELD (v_binfo));
483 : 73657 : v_offset = build1 (NOP_EXPR,
484 : : build_pointer_type (ptrdiff_type_node),
485 : : v_offset);
486 : 73657 : v_offset = cp_build_fold_indirect_ref (v_offset);
487 : 73657 : TREE_CONSTANT (v_offset) = 1;
488 : :
489 : 147314 : offset = convert_to_integer (ptrdiff_type_node,
490 : : size_diffop_loc (input_location, offset,
491 : 73657 : BINFO_OFFSET (v_binfo)));
492 : :
493 : 73657 : if (!integer_zerop (offset))
494 : 657 : v_offset = build2 (code, ptrdiff_type_node, v_offset, offset);
495 : :
496 : 73657 : if (fixed_type_p < 0)
497 : : /* Negative fixed_type_p means this is a constructor or destructor;
498 : : virtual base layout is fixed in in-charge [cd]tors, but not in
499 : : base [cd]tors. */
500 : 47633 : offset = build_if_in_charge
501 : 47633 : (convert_to_integer (ptrdiff_type_node, BINFO_OFFSET (binfo)),
502 : : v_offset);
503 : : else
504 : : offset = v_offset;
505 : : }
506 : :
507 : 1760483 : if (want_pointer)
508 : 1760483 : target_type = ptr_target_type;
509 : :
510 : 1760483 : if (!integer_zerop (offset))
511 : : {
512 : 75426 : offset = fold_convert (sizetype, offset);
513 : 75426 : if (code == MINUS_EXPR)
514 : 364 : offset = fold_build1_loc (input_location, NEGATE_EXPR, sizetype, offset);
515 : 75426 : expr = fold_build_pointer_plus (expr, offset);
516 : : }
517 : : else
518 : : null_test = NULL;
519 : :
520 : 1760483 : expr = build1 (NOP_EXPR, ptr_target_type, expr);
521 : :
522 : 2335918 : indout:
523 : 2335918 : if (!want_pointer)
524 : : {
525 : 1011546 : expr = cp_build_fold_indirect_ref (expr);
526 : 1011546 : if (rvalue)
527 : 218011 : expr = move (expr);
528 : : }
529 : :
530 : 1324372 : out:
531 : 4408005 : if (null_test)
532 : : /* Wrap EXPR in a null test. */
533 : 2264 : expr = build_if_nonnull (null_test, expr, complain);
534 : :
535 : : return expr;
536 : : }
537 : :
538 : : /* Subroutine of build_base_path; EXPR and BINFO are as in that function.
539 : : Perform a derived-to-base conversion by recursively building up a
540 : : sequence of COMPONENT_REFs to the appropriate base fields. */
541 : :
542 : : static tree
543 : 13501152 : build_simple_base_path (tree expr, tree binfo)
544 : : {
545 : 13501152 : tree type = BINFO_TYPE (binfo);
546 : 13501152 : tree d_binfo = BINFO_INHERITANCE_CHAIN (binfo);
547 : 13501152 : tree field;
548 : :
549 : 13501152 : if (d_binfo == NULL_TREE)
550 : : {
551 : 6666626 : tree temp;
552 : :
553 : 6666626 : gcc_assert (TYPE_MAIN_VARIANT (TREE_TYPE (expr)) == type);
554 : :
555 : : /* Transform `(a, b).x' into `(*(a, &b)).x', `(a ? b : c).x'
556 : : into `(*(a ? &b : &c)).x', and so on. A COND_EXPR is only
557 : : an lvalue in the front end; only _DECLs and _REFs are lvalues
558 : : in the back end. */
559 : 6666626 : temp = unary_complex_lvalue (ADDR_EXPR, expr);
560 : 6666626 : if (temp)
561 : 0 : expr = cp_build_fold_indirect_ref (temp);
562 : :
563 : 6666626 : return expr;
564 : : }
565 : :
566 : : /* Recurse. */
567 : 6834526 : expr = build_simple_base_path (expr, d_binfo);
568 : :
569 : 6834526 : for (field = TYPE_FIELDS (BINFO_TYPE (d_binfo));
570 : 32931446 : field; field = DECL_CHAIN (field))
571 : : /* Is this the base field created by build_base_field? */
572 : 32931446 : if (TREE_CODE (field) == FIELD_DECL
573 : 7923487 : && DECL_FIELD_IS_BASE (field)
574 : 7879822 : && TREE_TYPE (field) == type
575 : : /* If we're looking for a field in the most-derived class,
576 : : also check the field offset; we can have two base fields
577 : : of the same type if one is an indirect virtual base and one
578 : : is a direct non-virtual base. */
579 : 39766123 : && (BINFO_INHERITANCE_CHAIN (d_binfo)
580 : 6666777 : || tree_int_cst_equal (byte_position (field),
581 : 6666777 : BINFO_OFFSET (binfo))))
582 : : {
583 : : /* We don't use build_class_member_access_expr here, as that
584 : : has unnecessary checks, and more importantly results in
585 : : recursive calls to dfs_walk_once. */
586 : 6834526 : int type_quals = cp_type_quals (TREE_TYPE (expr));
587 : :
588 : 6834526 : expr = build3 (COMPONENT_REF,
589 : : cp_build_qualified_type (type, type_quals),
590 : : expr, field, NULL_TREE);
591 : : /* Mark the expression const or volatile, as appropriate.
592 : : Even though we've dealt with the type above, we still have
593 : : to mark the expression itself. */
594 : 6834526 : if (type_quals & TYPE_QUAL_CONST)
595 : 728868 : TREE_READONLY (expr) = 1;
596 : 6834526 : if (type_quals & TYPE_QUAL_VOLATILE)
597 : 21314 : TREE_THIS_VOLATILE (expr) = 1;
598 : :
599 : 6834526 : return expr;
600 : : }
601 : :
602 : : /* Didn't find the base field?!? */
603 : 0 : gcc_unreachable ();
604 : : }
605 : :
606 : : /* Convert OBJECT to the base TYPE. OBJECT is an expression whose
607 : : type is a class type or a pointer to a class type. In the former
608 : : case, TYPE is also a class type; in the latter it is another
609 : : pointer type. If CHECK_ACCESS is true, an error message is emitted
610 : : if TYPE is inaccessible. If OBJECT has pointer type, the value is
611 : : assumed to be non-NULL. */
612 : :
613 : : tree
614 : 1174439 : convert_to_base (tree object, tree type, bool check_access, bool nonnull,
615 : : tsubst_flags_t complain)
616 : : {
617 : 1174439 : tree binfo;
618 : 1174439 : tree object_type;
619 : :
620 : 1174439 : if (TYPE_PTR_P (TREE_TYPE (object)))
621 : : {
622 : 160195 : object_type = TREE_TYPE (TREE_TYPE (object));
623 : 160195 : type = TREE_TYPE (type);
624 : : }
625 : : else
626 : 1014244 : object_type = TREE_TYPE (object);
627 : :
628 : 1176411 : binfo = lookup_base (object_type, type, check_access ? ba_check : ba_unique,
629 : : NULL, complain);
630 : 1174439 : if (!binfo || binfo == error_mark_node)
631 : 110 : return error_mark_node;
632 : :
633 : 1174329 : return build_base_path (PLUS_EXPR, object, binfo, nonnull, complain);
634 : : }
635 : :
636 : : /* EXPR is an expression with unqualified class type. BASE is a base
637 : : binfo of that class type. Returns EXPR, converted to the BASE
638 : : type. This function assumes that EXPR is the most derived class;
639 : : therefore virtual bases can be found at their static offsets. */
640 : :
641 : : tree
642 : 1572074 : convert_to_base_statically (tree expr, tree base)
643 : : {
644 : 1572074 : tree expr_type;
645 : :
646 : 1572074 : expr_type = TREE_TYPE (expr);
647 : 1572074 : if (!SAME_BINFO_TYPE_P (BINFO_TYPE (base), expr_type))
648 : : {
649 : : /* If this is a non-empty base, use a COMPONENT_REF. */
650 : 1572074 : if (!is_empty_class (BINFO_TYPE (base)))
651 : 1174224 : return build_simple_base_path (expr, base);
652 : :
653 : : /* We use fold_build2 and fold_convert below to simplify the trees
654 : : provided to the optimizers. It is not safe to call these functions
655 : : when processing a template because they do not handle C++-specific
656 : : trees. */
657 : 397850 : gcc_assert (!processing_template_decl);
658 : 397850 : expr = cp_build_addr_expr (expr, tf_warning_or_error);
659 : 397850 : if (!integer_zerop (BINFO_OFFSET (base)))
660 : 252 : expr = fold_build_pointer_plus_loc (input_location,
661 : 252 : expr, BINFO_OFFSET (base));
662 : 397850 : expr = fold_convert (build_pointer_type (BINFO_TYPE (base)), expr);
663 : 397850 : expr = build_fold_indirect_ref_loc (input_location, expr);
664 : : }
665 : :
666 : : return expr;
667 : : }
668 : :
669 : : /* True IFF EXPR is a reference to an empty base class "subobject", as built in
670 : : convert_to_base_statically. We look for the result of the fold_convert
671 : : call, a NOP_EXPR from one pointer type to another, where the target is an
672 : : empty base of the original type. */
673 : :
674 : : bool
675 : 24504093 : is_empty_base_ref (tree expr)
676 : : {
677 : 24504093 : if (INDIRECT_REF_P (expr))
678 : 19821263 : expr = TREE_OPERAND (expr, 0);
679 : 24504093 : if (TREE_CODE (expr) != NOP_EXPR)
680 : : return false;
681 : 791864 : tree type = TREE_TYPE (expr);
682 : 791864 : if (!POINTER_TYPE_P (type))
683 : : return false;
684 : 791864 : type = TREE_TYPE (type);
685 : 791864 : if (!is_empty_class (type))
686 : : return false;
687 : 440733 : STRIP_NOPS (expr);
688 : 440733 : tree fromtype = TREE_TYPE (expr);
689 : 440733 : if (!POINTER_TYPE_P (fromtype))
690 : : return false;
691 : 440733 : fromtype = TREE_TYPE (fromtype);
692 : 435035 : return (CLASS_TYPE_P (fromtype)
693 : 435035 : && !same_type_ignoring_top_level_qualifiers_p (fromtype, type)
694 : 875248 : && DERIVED_FROM_P (type, fromtype));
695 : : }
696 : :
697 : : tree
698 : 1493878 : build_vfield_ref (tree datum, tree type)
699 : : {
700 : 1493878 : tree vfield, vcontext;
701 : :
702 : 1493878 : if (datum == error_mark_node
703 : : /* Can happen in case of duplicate base types (c++/59082). */
704 : 1493878 : || !TYPE_VFIELD (type))
705 : : return error_mark_node;
706 : :
707 : : /* First, convert to the requested type. */
708 : 1493878 : if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (datum), type))
709 : 0 : datum = convert_to_base (datum, type, /*check_access=*/false,
710 : : /*nonnull=*/true, tf_warning_or_error);
711 : :
712 : : /* Second, the requested type may not be the owner of its own vptr.
713 : : If not, convert to the base class that owns it. We cannot use
714 : : convert_to_base here, because VCONTEXT may appear more than once
715 : : in the inheritance hierarchy of TYPE, and thus direct conversion
716 : : between the types may be ambiguous. Following the path back up
717 : : one step at a time via primary bases avoids the problem. */
718 : 1493878 : vfield = TYPE_VFIELD (type);
719 : 1493878 : vcontext = DECL_CONTEXT (vfield);
720 : 3126460 : while (!same_type_ignoring_top_level_qualifiers_p (vcontext, type))
721 : : {
722 : 1632582 : datum = build_simple_base_path (datum, CLASSTYPE_PRIMARY_BINFO (type));
723 : 1632582 : type = TREE_TYPE (datum);
724 : : }
725 : :
726 : 1493878 : return build3 (COMPONENT_REF, TREE_TYPE (vfield), datum, vfield, NULL_TREE);
727 : : }
728 : :
729 : : /* Given an object INSTANCE, return an expression which yields the
730 : : vtable element corresponding to INDEX. There are many special
731 : : cases for INSTANCE which we take care of here, mainly to avoid
732 : : creating extra tree nodes when we don't have to. */
733 : :
734 : : tree
735 : 507577 : build_vtbl_ref (tree instance, tree idx)
736 : : {
737 : 507577 : tree aref;
738 : 507577 : tree vtbl = NULL_TREE;
739 : :
740 : : /* Try to figure out what a reference refers to, and
741 : : access its virtual function table directly. */
742 : :
743 : 507577 : int cdtorp = 0;
744 : 507577 : tree fixed_type = fixed_type_or_null (instance, NULL, &cdtorp);
745 : :
746 : 507577 : tree basetype = non_reference (TREE_TYPE (instance));
747 : :
748 : 507577 : if (fixed_type && !cdtorp)
749 : : {
750 : 131 : tree binfo = lookup_base (fixed_type, basetype,
751 : : ba_unique, NULL, tf_none);
752 : 131 : if (binfo && binfo != error_mark_node)
753 : 111 : vtbl = unshare_expr (BINFO_VTABLE (binfo));
754 : : }
755 : :
756 : 111 : if (!vtbl)
757 : 507544 : vtbl = build_vfield_ref (instance, basetype);
758 : :
759 : 507577 : aref = build_array_ref (input_location, vtbl, idx);
760 : 1015121 : TREE_CONSTANT (aref) |= TREE_CONSTANT (vtbl) && TREE_CONSTANT (idx);
761 : :
762 : 507577 : return aref;
763 : : }
764 : :
765 : : /* Given a stable object pointer INSTANCE_PTR, return an expression which
766 : : yields a function pointer corresponding to vtable element INDEX. */
767 : :
768 : : tree
769 : 506736 : build_vfn_ref (tree instance_ptr, tree idx)
770 : : {
771 : 506736 : tree aref;
772 : :
773 : 506736 : aref = build_vtbl_ref (cp_build_fold_indirect_ref (instance_ptr), idx);
774 : :
775 : : /* When using function descriptors, the address of the
776 : : vtable entry is treated as a function pointer. */
777 : 506736 : if (TARGET_VTABLE_USES_DESCRIPTORS)
778 : : aref = build1 (NOP_EXPR, TREE_TYPE (aref),
779 : : cp_build_addr_expr (aref, tf_warning_or_error));
780 : :
781 : : /* Remember this as a method reference, for later devirtualization. */
782 : 506736 : aref = build3 (OBJ_TYPE_REF, TREE_TYPE (aref), aref, instance_ptr,
783 : 506736 : fold_convert (TREE_TYPE (instance_ptr), idx));
784 : :
785 : 506736 : return aref;
786 : : }
787 : :
788 : : /* Return the name of the virtual function table (as an IDENTIFIER_NODE)
789 : : for the given TYPE. */
790 : :
791 : : static tree
792 : 1620718 : get_vtable_name (tree type)
793 : : {
794 : 0 : return mangle_vtbl_for_type (type);
795 : : }
796 : :
797 : : /* DECL is an entity associated with TYPE, like a virtual table or an
798 : : implicitly generated constructor. Determine whether or not DECL
799 : : should have external or internal linkage at the object file
800 : : level. This routine does not deal with COMDAT linkage and other
801 : : similar complexities; it simply sets TREE_PUBLIC if it possible for
802 : : entities in other translation units to contain copies of DECL, in
803 : : the abstract. */
804 : :
805 : : void
806 : 25836582 : set_linkage_according_to_type (tree /*type*/, tree decl)
807 : : {
808 : 25836582 : TREE_PUBLIC (decl) = 1;
809 : 25836582 : determine_visibility (decl);
810 : 25836582 : }
811 : :
812 : : /* Create a VAR_DECL for a primary or secondary vtable for CLASS_TYPE.
813 : : (For a secondary vtable for B-in-D, CLASS_TYPE should be D, not B.)
814 : : Use NAME for the name of the vtable, and VTABLE_TYPE for its type. */
815 : :
816 : : static tree
817 : 2053493 : build_vtable (tree class_type, tree name, tree vtable_type)
818 : : {
819 : 2053493 : tree decl;
820 : :
821 : 2053493 : decl = build_lang_decl (VAR_DECL, name, vtable_type);
822 : : /* vtable names are already mangled; give them their DECL_ASSEMBLER_NAME
823 : : now to avoid confusion in mangle_decl. */
824 : 2053493 : SET_DECL_ASSEMBLER_NAME (decl, name);
825 : 2053493 : DECL_CONTEXT (decl) = class_type;
826 : 2053493 : DECL_ARTIFICIAL (decl) = 1;
827 : 2053493 : TREE_STATIC (decl) = 1;
828 : 2053493 : TREE_READONLY (decl) = 1;
829 : 2053493 : DECL_VIRTUAL_P (decl) = 1;
830 : 2063829 : SET_DECL_ALIGN (decl, TARGET_VTABLE_ENTRY_ALIGN);
831 : 2053493 : DECL_USER_ALIGN (decl) = true;
832 : 2053493 : DECL_VTABLE_OR_VTT_P (decl) = 1;
833 : 2053493 : set_linkage_according_to_type (class_type, decl);
834 : : /* The vtable has not been defined -- yet. */
835 : 2053493 : DECL_EXTERNAL (decl) = 1;
836 : 2053493 : DECL_NOT_REALLY_EXTERN (decl) = 1;
837 : :
838 : : /* Mark the VAR_DECL node representing the vtable itself as a
839 : : "gratuitous" one, thereby forcing dwarfout.c to ignore it. It
840 : : is rather important that such things be ignored because any
841 : : effort to actually generate DWARF for them will run into
842 : : trouble when/if we encounter code like:
843 : :
844 : : #pragma interface
845 : : struct S { virtual void member (); };
846 : :
847 : : because the artificial declaration of the vtable itself (as
848 : : manufactured by the g++ front end) will say that the vtable is
849 : : a static member of `S' but only *after* the debug output for
850 : : the definition of `S' has already been output. This causes
851 : : grief because the DWARF entry for the definition of the vtable
852 : : will try to refer back to an earlier *declaration* of the
853 : : vtable as a static member of `S' and there won't be one. We
854 : : might be able to arrange to have the "vtable static member"
855 : : attached to the member list for `S' before the debug info for
856 : : `S' get written (which would solve the problem) but that would
857 : : require more intrusive changes to the g++ front end. */
858 : 2053493 : DECL_IGNORED_P (decl) = 1;
859 : :
860 : 2053493 : return decl;
861 : : }
862 : :
863 : : /* Get the VAR_DECL of the vtable for TYPE. TYPE need not be polymorphic,
864 : : or even complete. If this does not exist, create it. If COMPLETE is
865 : : nonzero, then complete the definition of it -- that will render it
866 : : impossible to actually build the vtable, but is useful to get at those
867 : : which are known to exist in the runtime. */
868 : :
869 : : tree
870 : 7703369 : get_vtable_decl (tree type, int complete)
871 : : {
872 : 7703369 : tree decl;
873 : :
874 : 7703369 : if (CLASSTYPE_VTABLES (type))
875 : 6082651 : return CLASSTYPE_VTABLES (type);
876 : :
877 : 1620718 : decl = build_vtable (type, get_vtable_name (type), vtbl_type_node);
878 : 1620718 : CLASSTYPE_VTABLES (type) = decl;
879 : :
880 : 1620718 : if (complete)
881 : : {
882 : 39797 : DECL_EXTERNAL (decl) = 1;
883 : 39797 : cp_finish_decl (decl, NULL_TREE, false, NULL_TREE, 0);
884 : : }
885 : :
886 : : return decl;
887 : : }
888 : :
889 : : /* Build the primary virtual function table for TYPE. If BINFO is
890 : : non-NULL, build the vtable starting with the initial approximation
891 : : that it is the same as the one which is the head of the association
892 : : list. Returns a nonzero value if a new vtable is actually
893 : : created. */
894 : :
895 : : static int
896 : 6071825 : build_primary_vtable (tree binfo, tree type)
897 : : {
898 : 6071825 : tree decl;
899 : 6071825 : tree virtuals;
900 : :
901 : 6071825 : decl = get_vtable_decl (type, /*complete=*/0);
902 : :
903 : 6071825 : if (binfo)
904 : : {
905 : 5839439 : if (BINFO_NEW_VTABLE_MARKED (binfo))
906 : : /* We have already created a vtable for this base, so there's
907 : : no need to do it again. */
908 : : return 0;
909 : :
910 : 1348535 : virtuals = copy_list (BINFO_VIRTUALS (binfo));
911 : 1348535 : TREE_TYPE (decl) = TREE_TYPE (get_vtbl_decl_for_binfo (binfo));
912 : 1348535 : DECL_SIZE (decl) = TYPE_SIZE (TREE_TYPE (decl));
913 : 1348535 : DECL_SIZE_UNIT (decl) = TYPE_SIZE_UNIT (TREE_TYPE (decl));
914 : : }
915 : : else
916 : : {
917 : 232386 : gcc_assert (TREE_TYPE (decl) == vtbl_type_node);
918 : : virtuals = NULL_TREE;
919 : : }
920 : :
921 : : /* Initialize the association list for this type, based
922 : : on our first approximation. */
923 : 1580921 : BINFO_VTABLE (TYPE_BINFO (type)) = decl;
924 : 1580921 : BINFO_VIRTUALS (TYPE_BINFO (type)) = virtuals;
925 : 1580921 : SET_BINFO_NEW_VTABLE_MARKED (TYPE_BINFO (type));
926 : : return 1;
927 : : }
928 : :
929 : : /* Give BINFO a new virtual function table which is initialized
930 : : with a skeleton-copy of its original initialization. The only
931 : : entry that changes is the `delta' entry, so we can really
932 : : share a lot of structure.
933 : :
934 : : FOR_TYPE is the most derived type which caused this table to
935 : : be needed.
936 : :
937 : : Returns nonzero if we haven't met BINFO before.
938 : :
939 : : The order in which vtables are built (by calling this function) for
940 : : an object must remain the same, otherwise a binary incompatibility
941 : : can result. */
942 : :
943 : : static int
944 : 732590 : build_secondary_vtable (tree binfo)
945 : : {
946 : 732590 : if (BINFO_NEW_VTABLE_MARKED (binfo))
947 : : /* We already created a vtable for this base. There's no need to
948 : : do it again. */
949 : : return 0;
950 : :
951 : : /* Remember that we've created a vtable for this BINFO, so that we
952 : : don't try to do so again. */
953 : 254643 : SET_BINFO_NEW_VTABLE_MARKED (binfo);
954 : :
955 : : /* Make fresh virtual list, so we can smash it later. */
956 : 254643 : BINFO_VIRTUALS (binfo) = copy_list (BINFO_VIRTUALS (binfo));
957 : :
958 : : /* Secondary vtables are laid out as part of the same structure as
959 : : the primary vtable. */
960 : 254643 : BINFO_VTABLE (binfo) = NULL_TREE;
961 : 254643 : return 1;
962 : : }
963 : :
964 : : /* Create a new vtable for BINFO which is the hierarchy dominated by
965 : : T. Return nonzero if we actually created a new vtable. */
966 : :
967 : : static int
968 : 6572029 : make_new_vtable (tree t, tree binfo)
969 : : {
970 : 6572029 : if (binfo == TYPE_BINFO (t))
971 : : /* In this case, it is *type*'s vtable we are modifying. We start
972 : : with the approximation that its vtable is that of the
973 : : immediate base class. */
974 : 5839439 : return build_primary_vtable (binfo, t);
975 : : else
976 : : /* This is our very own copy of `basetype' to play with. Later,
977 : : we will fill in all the virtual functions that override the
978 : : virtual functions in these base classes which are not defined
979 : : by the current type. */
980 : 732590 : return build_secondary_vtable (binfo);
981 : : }
982 : :
983 : : /* Make *VIRTUALS, an entry on the BINFO_VIRTUALS list for BINFO
984 : : (which is in the hierarchy dominated by T) list FNDECL as its
985 : : BV_FN. DELTA is the required constant adjustment from the `this'
986 : : pointer where the vtable entry appears to the `this' required when
987 : : the function is actually called. */
988 : :
989 : : static void
990 : 6790390 : modify_vtable_entry (tree t,
991 : : tree binfo,
992 : : tree fndecl,
993 : : tree delta,
994 : : tree *virtuals)
995 : : {
996 : 6790390 : tree v;
997 : :
998 : 6790390 : v = *virtuals;
999 : :
1000 : 6790390 : if (fndecl != BV_FN (v)
1001 : 6790390 : || !tree_int_cst_equal (delta, BV_DELTA (v)))
1002 : : {
1003 : : /* We need a new vtable for BINFO. */
1004 : 4968851 : if (make_new_vtable (t, binfo))
1005 : : {
1006 : : /* If we really did make a new vtable, we also made a copy
1007 : : of the BINFO_VIRTUALS list. Now, we have to find the
1008 : : corresponding entry in that list. */
1009 : 0 : *virtuals = BINFO_VIRTUALS (binfo);
1010 : 0 : while (BV_FN (*virtuals) != BV_FN (v))
1011 : 0 : *virtuals = TREE_CHAIN (*virtuals);
1012 : : v = *virtuals;
1013 : : }
1014 : :
1015 : 4968851 : BV_DELTA (v) = delta;
1016 : 4968851 : BV_VCALL_INDEX (v) = NULL_TREE;
1017 : 4968851 : BV_FN (v) = fndecl;
1018 : : }
1019 : 6790390 : }
1020 : :
1021 : :
1022 : : /* Check if the object parameter of an iobj member function corresponds to
1023 : : another parameter type. CONTEXT is the class that the implicit object
1024 : : parameter is considered to refer to. */
1025 : :
1026 : : bool
1027 : 54913 : iobj_parm_corresponds_to (tree iobj_fn, tree xobj_param, tree context)
1028 : : {
1029 : 54913 : tree iobj_fn_type = TREE_TYPE (iobj_fn);
1030 : :
1031 : : /* If the iobj member function was introduced with a using declaration, the
1032 : : type of its object parameter is considered to be that of the class it was
1033 : : introduced into.
1034 : :
1035 : : [over.match.funcs.general.4]
1036 : : For non-conversion functions that are implicit object member
1037 : : functions nominated by a using-declaration in a derived class, the
1038 : : function is considered to be a member of the derived class for the purpose
1039 : : of defining the type of the implicit object parameter.
1040 : :
1041 : : Unfortunately, because of this rule, we can't just compare the xobj member
1042 : : function's DECL_CONTEXT to its object parameter.
1043 : :
1044 : : struct S;
1045 : :
1046 : : struct B {
1047 : : int f(this S&) { return 5; }
1048 : : };
1049 : :
1050 : : struct S : B {
1051 : : using B::f;
1052 : : int f() { return 10; }
1053 : : };
1054 : :
1055 : : The using declaration does not change the object parameter of B::f as it
1056 : : is an xobj member function. However, its object parameter still
1057 : : corresponds to S::f as it was declared with an object parameter of type
1058 : : S const&. The DECL_CONTEXT of B::f is B, so if we compare the type of the
1059 : : object parameter to that, it will not match. If we naively assume a
1060 : : different type from the DECL_CONTEXT for an xobj parameter means that the
1061 : : object parameters do not correspond, then the object parameters in the
1062 : : above example will be considered non-corresponding.
1063 : :
1064 : : As a result of this, B::f would incorrectly not be discarded, causing an
1065 : : ambiguity when f is called on an object of type S.
1066 : :
1067 : : This also impacts member functions with constraints as in the following
1068 : : example.
1069 : :
1070 : : template<typename = void>
1071 : : struct S;
1072 : :
1073 : : template<typename = void>
1074 : : struct B {
1075 : : int f(this S<>&) requires true { return 5; }
1076 : : };
1077 : :
1078 : : template<typename>
1079 : : struct S : B<> {
1080 : : using B<>::f;
1081 : : int f() { return 10; }
1082 : : };
1083 : :
1084 : : Once again, if we compare the DECL_CONTEXT of B<>::f to it's xobj
1085 : : parameter, it would not match. If the object parameters do not
1086 : : correspond, constraints are not taken into account, so in this example we
1087 : : would (probably) get an ambiguous lookup instead of correctly picking
1088 : : B<>::f.
1089 : :
1090 : : Because of this caveat, we must actually compare the type of the iobj
1091 : : parameter to the type of the xobj parameter, shortcuts will have these
1092 : : edge cases.
1093 : :
1094 : : Aside from the more complex reasons above, this logic also implicitly
1095 : : handles xobj parameters of pointer type, we don't have to explicitly
1096 : : check for that case. */
1097 : :
1098 : 54913 : if (!same_type_ignoring_top_level_qualifiers_p
1099 : 54913 : (context, non_reference (xobj_param)))
1100 : : return false;
1101 : :
1102 : : /* We don't get to bail yet even if we have a by-value xobj parameter,
1103 : : a by-value xobj parameter can correspond to an iobj parameter provided the
1104 : : iobj member function is not declared with a reference qualifier.
1105 : :
1106 : : From this point on, we know we are dealing with an xobj parameter that has
1107 : : an object parameter of the same type as the class it was declared in.
1108 : : We still don't know if we have a reference or by-value parameter yet
1109 : : though. */
1110 : :
1111 : 54911 : cp_ref_qualifier const iobj_ref_qual = type_memfn_rqual (iobj_fn_type);
1112 : : /* We only care about cv qualifiers when determining correspondence. */
1113 : 54911 : static constexpr cp_cv_quals cv_bits = TYPE_QUAL_VOLATILE
1114 : : | TYPE_QUAL_CONST;
1115 : 54911 : cp_cv_quals const iobj_cv_quals = type_memfn_quals (iobj_fn_type) & cv_bits;
1116 : : /* We need to ignore the ref qualifier of the xobj parameter if the iobj
1117 : : member function lacks a ref qualifier.
1118 : :
1119 : : [basic.scope.scope.3]
1120 : : Two non-static member functions have corresponding object parameters if:
1121 : : -- exactly one is an implicit object member function with no ref-qualifier
1122 : : and the types of their object parameters ([dcl.fct]), after removing
1123 : : top-level references, are the same, or
1124 : : -- their object parameters have the same type.
1125 : :
1126 : : The cv qualifiers of a by-value parameter are supposed to be discarded, so
1127 : : we ignore them.
1128 : :
1129 : : [dcl.fct.5]
1130 : : After producing the list of parameter types, any top-level cv-qualifiers
1131 : : modifying a parameter type are deleted when forming the function type.
1132 : :
1133 : : However, they still need to be taken into account when our xobj parameter
1134 : : is a reference that is being ignored (according to [basic.scope.scope.3]
1135 : : quoted above), but when we are actually dealing with a by-value xobj
1136 : : parameter we can proceed following this table.
1137 : : | iobj | xobj | equal |
1138 : : | none | none | X |
1139 : : | none | c | X |
1140 : : | none | v | X |
1141 : : | none | cv | X |
1142 : : | c | none | O |
1143 : : | c | c | O |
1144 : : | c | v | O |
1145 : : | c | cv | O |
1146 : : | v | none | O |
1147 : : | v | c | O |
1148 : : | v | v | O |
1149 : : | v | cv | O |
1150 : : | cv | none | O |
1151 : : | cv | c | O |
1152 : : | cv | v | O |
1153 : : | cv | cv | O |
1154 : :
1155 : : Additionally, if the iobj member function is ref qualified, we aren't
1156 : : ignoring the ref qualifier of the iobj parameter, so we can't be dealing
1157 : : with correspondence in that case either.
1158 : :
1159 : : So to recap, if we have a by-value xobj parameter, we know for sure that
1160 : : we aren't dealing with corresponding object parameters if the iobj member
1161 : : function has any cv-ref qualifiers. The only case where we might still be
1162 : : dealing with corresponding object parameters is when the iobj member
1163 : : function lacks any cv-ref qualification. */
1164 : 54911 : if (!TYPE_REF_P (xobj_param))
1165 : : {
1166 : 951 : if (iobj_ref_qual || iobj_cv_quals)
1167 : : return false;
1168 : : }
1169 : : else
1170 : : {
1171 : : /* We are dealing with an xobj parameter that is a reference now, but due
1172 : : to [basic.scope.scope.3] we need to ignore its ref qual. */
1173 : 107920 : cp_ref_qualifier const xobj_ref_qual = [&](){
1174 : 53960 : if (!TYPE_REF_P (xobj_param) || !iobj_ref_qual)
1175 : : return REF_QUAL_NONE;
1176 : 816 : return TYPE_REF_IS_RVALUE (xobj_param) ? REF_QUAL_RVALUE
1177 : : : REF_QUAL_LVALUE;
1178 : 53960 : }(); /* IILE. */
1179 : :
1180 : : /* Even if we are ignoring the reference qualifier, the xobj parameter
1181 : : was still a reference so we still take the cv qualifiers into
1182 : : account. */
1183 : 53960 : cp_cv_quals const xobj_cv_quals
1184 : 53960 : = cp_type_quals (TREE_TYPE (xobj_param)) & cv_bits;
1185 : :
1186 : : /* Finally, if the qualifications don't match exactly, the object
1187 : : parameters don't correspond. */
1188 : 53960 : if (iobj_ref_qual != xobj_ref_qual
1189 : 53576 : || iobj_cv_quals != xobj_cv_quals)
1190 : : return false;
1191 : : }
1192 : : /* If we got past everything else, the object parameters of fn1 and fn2
1193 : : definitely correspond. */
1194 : : return true;
1195 : : }
1196 : :
1197 : : /* True if FN and METHOD have corresponding object parms per
1198 : : [basic.scope.scope], or if one of them is a static member function (which
1199 : : are considered to have an object parm that corresponds to any other).
1200 : : CONTEXT is the class that an implicit object member function is considered
1201 : : to be a member of for the purpose of this comparison, per
1202 : : [over.match.funcs]. */
1203 : :
1204 : : bool
1205 : 357366669 : object_parms_correspond (tree fn, tree method, tree context)
1206 : : {
1207 : 357366669 : tree fn_type = TREE_TYPE (fn);
1208 : 357366669 : tree method_type = TREE_TYPE (method);
1209 : :
1210 : : /* Compare the quals on the 'this' parm. Don't compare
1211 : : the whole types, as used functions are treated as
1212 : : coming from the using class in overload resolution. */
1213 : 357366669 : if (DECL_IOBJ_MEMBER_FUNCTION_P (fn)
1214 : 357366669 : && DECL_IOBJ_MEMBER_FUNCTION_P (method))
1215 : : {
1216 : : /* Either both or neither need to be ref-qualified for
1217 : : differing quals to allow overloading. */
1218 : 354730318 : if ((FUNCTION_REF_QUALIFIED (fn_type)
1219 : 354730318 : == FUNCTION_REF_QUALIFIED (method_type))
1220 : 354730318 : && (type_memfn_quals (fn_type) != type_memfn_quals (method_type)
1221 : 339385905 : || type_memfn_rqual (fn_type) != type_memfn_rqual (method_type)))
1222 : 15458875 : return false;
1223 : 339271443 : return true;
1224 : : }
1225 : : /* Treat a static member function as corresponding to any object parm. */
1226 : 2636351 : else if (DECL_STATIC_FUNCTION_P (fn) || DECL_STATIC_FUNCTION_P (method))
1227 : : return true;
1228 : : /* Handle special correspondence rules for xobj vs xobj and xobj vs iobj
1229 : : member function declarations.
1230 : : We don't worry about static member functions here. */
1231 : 3980 : else if (DECL_XOBJ_MEMBER_FUNCTION_P (fn)
1232 : 7226 : && DECL_XOBJ_MEMBER_FUNCTION_P (method))
1233 : : {
1234 : 7596 : auto get_object_param = [] (tree fn)
1235 : : {
1236 : 5064 : return TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fn)));
1237 : : };
1238 : : /* We skip the object parameter below, check it here instead of
1239 : : making changes to that code. */
1240 : 2532 : tree fn_param = get_object_param (fn);
1241 : 2532 : tree method_param = get_object_param (method);
1242 : 2532 : if (!same_type_p (fn_param, method_param))
1243 : 2002 : return false;
1244 : : }
1245 : : else
1246 : : {
1247 : 1448 : tree xobj_fn = DECL_XOBJ_MEMBER_FUNCTION_P (fn) ? fn : method;
1248 : 1448 : tree iobj_fn = xobj_fn != fn ? fn : method;
1249 : 1448 : tree xobj_param = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (xobj_fn)));
1250 : :
1251 : 1448 : return iobj_parm_corresponds_to (iobj_fn, xobj_param, context);
1252 : : }
1253 : :
1254 : 530 : return true;
1255 : : }
1256 : :
1257 : : /* Add method METHOD to class TYPE. If VIA_USING indicates whether
1258 : : METHOD is being injected via a using_decl. Returns true if the
1259 : : method could be added to the method vec. */
1260 : :
1261 : : bool
1262 : 268688689 : add_method (tree type, tree method, bool via_using)
1263 : : {
1264 : 268688689 : if (method == error_mark_node)
1265 : : return false;
1266 : :
1267 : 268688689 : gcc_assert (!DECL_EXTERN_C_P (method));
1268 : :
1269 : 268688689 : tree *slot = find_member_slot (type, DECL_NAME (method));
1270 : 268688689 : tree current_fns = slot ? *slot : NULL_TREE;
1271 : :
1272 : : /* See below. */
1273 : 268688689 : int losem = -1;
1274 : :
1275 : : /* Check to see if we've already got this method. */
1276 : 1121048817 : for (ovl_iterator iter (current_fns); iter; ++iter)
1277 : : {
1278 : 476344166 : tree fn = *iter;
1279 : :
1280 : 476344166 : if (TREE_CODE (fn) != TREE_CODE (method))
1281 : 117662586 : continue;
1282 : :
1283 : : /* Two using-declarations can coexist, we'll complain about ambiguity in
1284 : : overload resolution. */
1285 : 2944472 : if (via_using && iter.using_p ()
1286 : : /* Except handle inherited constructors specially. */
1287 : 361886818 : && ! DECL_CONSTRUCTOR_P (fn))
1288 : : {
1289 : 1326088 : if (fn == method)
1290 : : /* Don't add the same one twice. */
1291 : 4941 : return false;
1292 : 1326077 : continue;
1293 : : }
1294 : :
1295 : : /* [over.load] Member function declarations with the
1296 : : same name and the same parameter types cannot be
1297 : : overloaded if any of them is a static member
1298 : : function declaration.
1299 : :
1300 : : [over.load] Member function declarations with the same name and
1301 : : the same parameter-type-list as well as member function template
1302 : : declarations with the same name, the same parameter-type-list, and
1303 : : the same template parameter lists cannot be overloaded if any of
1304 : : them, but not all, have a ref-qualifier.
1305 : :
1306 : : [namespace.udecl] When a using-declaration brings names
1307 : : from a base class into a derived class scope, member
1308 : : functions in the derived class override and/or hide member
1309 : : functions with the same name and parameter types in a base
1310 : : class (rather than conflicting). */
1311 : 357355492 : if (!object_parms_correspond (fn, method, type))
1312 : 15461935 : continue;
1313 : :
1314 : 341893557 : tree fn_type = TREE_TYPE (fn);
1315 : 341893557 : tree method_type = TREE_TYPE (method);
1316 : :
1317 : 341893557 : tree real_fn = fn;
1318 : 341893557 : tree real_method = method;
1319 : :
1320 : : /* Templates and conversion ops must match return types. */
1321 : 683711914 : if ((DECL_CONV_FN_P (fn) || TREE_CODE (fn) == TEMPLATE_DECL)
1322 : 487853868 : && !same_type_p (TREE_TYPE (fn_type), TREE_TYPE (method_type)))
1323 : 3921680 : continue;
1324 : :
1325 : : /* For templates, the template parameters must be identical. */
1326 : 337971877 : if (TREE_CODE (fn) == TEMPLATE_DECL)
1327 : : {
1328 : 284227624 : if (!comp_template_parms (DECL_TEMPLATE_PARMS (fn),
1329 : 142113812 : DECL_TEMPLATE_PARMS (method)))
1330 : 113785284 : continue;
1331 : :
1332 : 28328528 : real_fn = DECL_TEMPLATE_RESULT (fn);
1333 : 28328528 : real_method = DECL_TEMPLATE_RESULT (method);
1334 : : }
1335 : :
1336 : 224186593 : tree parms1 = TYPE_ARG_TYPES (fn_type);
1337 : 224186593 : tree parms2 = TYPE_ARG_TYPES (method_type);
1338 : 224186593 : if (! DECL_STATIC_FUNCTION_P (real_fn))
1339 : 222428281 : parms1 = TREE_CHAIN (parms1);
1340 : 224186593 : if (! DECL_STATIC_FUNCTION_P (real_method))
1341 : 222422593 : parms2 = TREE_CHAIN (parms2);
1342 : :
1343 : : /* Bring back parameters omitted from an inherited ctor. The
1344 : : method and the function can have different omittedness. */
1345 : 224186593 : if (ctor_omit_inherited_parms (real_fn))
1346 : 729 : parms1 = FUNCTION_FIRST_USER_PARMTYPE (DECL_CLONED_FUNCTION (real_fn));
1347 : 224186593 : if (ctor_omit_inherited_parms (real_method))
1348 : 330 : parms2 = (FUNCTION_FIRST_USER_PARMTYPE
1349 : : (DECL_CLONED_FUNCTION (real_method)));
1350 : :
1351 : 224186593 : if (!compparms (parms1, parms2))
1352 : 223138810 : continue;
1353 : :
1354 : 1047783 : if (!equivalently_constrained (fn, method))
1355 : : {
1356 : 921154 : if (processing_template_decl)
1357 : : /* We can't check satisfaction in dependent context, wait until
1358 : : the class is instantiated. */
1359 : 123262 : continue;
1360 : :
1361 : 797892 : special_function_kind sfk = special_memfn_p (method);
1362 : :
1363 : 1564668 : if (sfk == sfk_none
1364 : 126592 : || DECL_INHERITED_CTOR (fn)
1365 : 861162 : || TREE_CODE (fn) == TEMPLATE_DECL)
1366 : : /* Member function templates and non-special member functions
1367 : : coexist if they are not equivalently constrained. A member
1368 : : function is not hidden by an inherited constructor. */
1369 : 766776 : continue;
1370 : :
1371 : : /* P0848: For special member functions, deleted, unsatisfied, or
1372 : : less constrained overloads are ineligible. We implement this
1373 : : by removing them from CLASSTYPE_MEMBER_VEC. Destructors don't
1374 : : use the notion of eligibility, and the selected destructor can
1375 : : be deleted, but removing unsatisfied or less constrained
1376 : : overloads has the same effect as overload resolution. */
1377 : 31116 : bool dtor = (sfk == sfk_destructor);
1378 : 31116 : if (losem == -1)
1379 : 61797 : losem = ((!dtor && DECL_DELETED_FN (method))
1380 : 61797 : || !constraints_satisfied_p (method));
1381 : 30697 : bool losef = ((!dtor && DECL_DELETED_FN (fn))
1382 : 34833 : || !constraints_satisfied_p (fn));
1383 : 31116 : int win;
1384 : 31116 : if (losem || losef)
1385 : 30932 : win = losem - losef;
1386 : : else
1387 : 184 : win = more_constrained (fn, method);
1388 : 31116 : if (win > 0)
1389 : : /* Leave FN in the method vec, discard METHOD. */
1390 : : return false;
1391 : 27239 : else if (win < 0)
1392 : : {
1393 : : /* Remove FN, add METHOD. */
1394 : 24698 : current_fns = iter.remove_node (current_fns);
1395 : 24698 : continue;
1396 : : }
1397 : : else
1398 : : /* Let them coexist for now. */
1399 : 2541 : continue;
1400 : : }
1401 : :
1402 : : /* If these are versions of the same function, process and
1403 : : move on. */
1404 : 126665 : if (TREE_CODE (fn) == FUNCTION_DECL
1405 : 126629 : && maybe_version_functions (method, fn, true))
1406 : 36 : continue;
1407 : :
1408 : 253186 : if (DECL_INHERITED_CTOR (method))
1409 : : {
1410 : 1468 : if (!DECL_INHERITED_CTOR (fn))
1411 : : /* Defer to the other function. */
1412 : : return false;
1413 : :
1414 : 75 : tree basem = DECL_INHERITED_CTOR_BASE (method);
1415 : 150 : tree basef = DECL_INHERITED_CTOR_BASE (fn);
1416 : 75 : if (flag_new_inheriting_ctors)
1417 : : {
1418 : 72 : if (basem == basef)
1419 : : {
1420 : : /* Inheriting the same constructor along different
1421 : : paths, combine them. */
1422 : 90 : SET_DECL_INHERITED_CTOR
1423 : : (fn, ovl_make (DECL_INHERITED_CTOR (method),
1424 : : DECL_INHERITED_CTOR (fn)));
1425 : : /* And discard the new one. */
1426 : 30 : return false;
1427 : : }
1428 : : else
1429 : : /* Inherited ctors can coexist until overload
1430 : : resolution. */
1431 : 42 : continue;
1432 : : }
1433 : :
1434 : 3 : error_at (DECL_SOURCE_LOCATION (method),
1435 : : "%q#D conflicts with version inherited from %qT",
1436 : : method, basef);
1437 : 3 : inform (DECL_SOURCE_LOCATION (fn),
1438 : : "version inherited from %qT declared here",
1439 : : basef);
1440 : 3 : return false;
1441 : : }
1442 : :
1443 : 125859 : if (via_using)
1444 : : /* Defer to the local function. */
1445 : : return false;
1446 : 125843 : else if (iter.using_p ()
1447 : 125843 : || (flag_new_inheriting_ctors
1448 : 167178 : && DECL_INHERITED_CTOR (fn)))
1449 : : {
1450 : : /* Remove the inherited function. */
1451 : 125498 : current_fns = iter.remove_node (current_fns);
1452 : 125498 : continue;
1453 : : }
1454 : : else
1455 : : {
1456 : 345 : error_at (DECL_SOURCE_LOCATION (method),
1457 : : "%q#D cannot be overloaded with %q#D", method, fn);
1458 : 345 : inform (DECL_SOURCE_LOCATION (fn),
1459 : : "previous declaration %q#D", fn);
1460 : 345 : return false;
1461 : : }
1462 : : }
1463 : :
1464 : 268683748 : current_fns = ovl_insert (method, current_fns, via_using);
1465 : :
1466 : 689562029 : if (!COMPLETE_TYPE_P (type) && !DECL_CONV_FN_P (method)
1467 : 493152097 : && !push_class_level_binding (DECL_NAME (method), current_fns))
1468 : : return false;
1469 : :
1470 : 268683728 : if (!slot)
1471 : 121319012 : slot = add_member_slot (type, DECL_NAME (method));
1472 : :
1473 : : /* Maintain TYPE_HAS_USER_CONSTRUCTOR, etc. */
1474 : 268683728 : grok_special_member_properties (method);
1475 : :
1476 : 268683728 : *slot = current_fns;
1477 : :
1478 : 268683728 : return true;
1479 : : }
1480 : :
1481 : : /* Subroutines of finish_struct. */
1482 : :
1483 : : /* Change the access of FDECL to ACCESS in T. Return 1 if change was
1484 : : legit, otherwise return 0. */
1485 : :
1486 : : static int
1487 : 2407687 : alter_access (tree t, tree fdecl, tree access)
1488 : : {
1489 : 2407687 : tree elem;
1490 : :
1491 : 2407687 : retrofit_lang_decl (fdecl);
1492 : :
1493 : 2407687 : gcc_assert (!DECL_DISCRIMINATOR_P (fdecl));
1494 : :
1495 : 2407687 : elem = purpose_member (t, DECL_ACCESS (fdecl));
1496 : 2407687 : if (elem)
1497 : : {
1498 : 0 : if (TREE_VALUE (elem) != access)
1499 : : {
1500 : 0 : if (TREE_CODE (TREE_TYPE (fdecl)) == FUNCTION_DECL)
1501 : 0 : error ("conflicting access specifications for method"
1502 : 0 : " %q+D, ignored", TREE_TYPE (fdecl));
1503 : : else
1504 : 0 : error ("conflicting access specifications for field %qE, ignored",
1505 : 0 : DECL_NAME (fdecl));
1506 : : }
1507 : : else
1508 : : {
1509 : : /* They're changing the access to the same thing they changed
1510 : : it to before. That's OK. */
1511 : : ;
1512 : : }
1513 : : }
1514 : : else
1515 : : {
1516 : 2407687 : perform_or_defer_access_check (TYPE_BINFO (t), fdecl, fdecl,
1517 : : tf_warning_or_error);
1518 : 2407687 : DECL_ACCESS (fdecl) = tree_cons (t, access, DECL_ACCESS (fdecl));
1519 : 2407687 : return 1;
1520 : : }
1521 : : return 0;
1522 : : }
1523 : :
1524 : : /* Return the access node for DECL's access in its enclosing class. */
1525 : :
1526 : : tree
1527 : 445709 : declared_access (tree decl)
1528 : : {
1529 : 445709 : return (TREE_PRIVATE (decl) ? access_private_node
1530 : 435782 : : TREE_PROTECTED (decl) ? access_protected_node
1531 : 445709 : : access_public_node);
1532 : : }
1533 : :
1534 : : /* If DECL is a non-dependent using of non-ctor function members, push them
1535 : : and return true, otherwise return false. Called from
1536 : : finish_member_declaration. */
1537 : :
1538 : : bool
1539 : 150028962 : maybe_push_used_methods (tree decl)
1540 : : {
1541 : 150028962 : if (TREE_CODE (decl) != USING_DECL)
1542 : : return false;
1543 : 2379873 : tree used = strip_using_decl (decl);
1544 : 2379873 : if (!used || !is_overloaded_fn (used))
1545 : : return false;
1546 : :
1547 : : /* Add the functions to CLASSTYPE_MEMBER_VEC so that overload resolution
1548 : : works within the class body. */
1549 : 4846844 : for (tree f : ovl_range (used))
1550 : : {
1551 : 4583996 : if (DECL_CONSTRUCTOR_P (f))
1552 : : /* Inheriting constructors are handled separately. */
1553 : 44264 : return false;
1554 : :
1555 : 2247734 : bool added = add_method (current_class_type, f, true);
1556 : :
1557 : 2247734 : if (added)
1558 : 2247707 : alter_access (current_class_type, f, current_access_specifier);
1559 : :
1560 : : /* If add_method returns false because f was already declared, look
1561 : : for a duplicate using-declaration. */
1562 : : else
1563 : 93 : for (tree d = TYPE_FIELDS (current_class_type); d; d = DECL_CHAIN (d))
1564 : 74 : if (TREE_CODE (d) == USING_DECL
1565 : 31 : && DECL_NAME (d) == DECL_NAME (decl)
1566 : 85 : && same_type_p (USING_DECL_SCOPE (d), USING_DECL_SCOPE (decl)))
1567 : : {
1568 : 8 : diagnose_name_conflict (decl, d);
1569 : 8 : break;
1570 : : }
1571 : : }
1572 : 1357417 : return true;
1573 : : }
1574 : :
1575 : : /* Process the USING_DECL, which is a member of T. */
1576 : :
1577 : : static void
1578 : 1778624 : handle_using_decl (tree using_decl, tree t)
1579 : : {
1580 : 1778624 : tree decl = USING_DECL_DECLS (using_decl);
1581 : :
1582 : 1778624 : gcc_assert (!processing_template_decl && decl);
1583 : :
1584 : 1778624 : cp_emit_debug_info_for_using (decl, t);
1585 : :
1586 : 1778624 : if (is_overloaded_fn (decl))
1587 : : /* Handled in maybe_push_used_methods. */
1588 : 1778624 : return;
1589 : :
1590 : 421368 : tree name = DECL_NAME (using_decl);
1591 : 421368 : tree old_value = lookup_member (t, name, /*protect=*/0, /*want_type=*/false,
1592 : : tf_warning_or_error);
1593 : 421368 : if (old_value)
1594 : : {
1595 : 421368 : old_value = OVL_FIRST (old_value);
1596 : :
1597 : 421368 : if (DECL_P (old_value) && DECL_CONTEXT (old_value) == t)
1598 : : /* OK */;
1599 : : else
1600 : : old_value = NULL_TREE;
1601 : : }
1602 : :
1603 : 0 : if (! old_value)
1604 : : ;
1605 : 0 : else if (is_overloaded_fn (old_value))
1606 : : {
1607 : 0 : error_at (DECL_SOURCE_LOCATION (using_decl), "%qD invalid in %q#T "
1608 : : "because of local method %q#D with same name",
1609 : : using_decl, t, old_value);
1610 : 0 : inform (DECL_SOURCE_LOCATION (old_value),
1611 : : "local method %q#D declared here", old_value);
1612 : 0 : return;
1613 : : }
1614 : 0 : else if (!DECL_ARTIFICIAL (old_value))
1615 : : {
1616 : 0 : error_at (DECL_SOURCE_LOCATION (using_decl), "%qD invalid in %q#T "
1617 : : "because of local member %q#D with same name",
1618 : : using_decl, t, old_value);
1619 : 0 : inform (DECL_SOURCE_LOCATION (old_value),
1620 : : "local member %q#D declared here", old_value);
1621 : 0 : return;
1622 : : }
1623 : :
1624 : 421368 : iloc_sentinel ils (DECL_SOURCE_LOCATION (using_decl));
1625 : 421368 : tree access = declared_access (using_decl);
1626 : :
1627 : : /* Make type T see field decl FDECL with access ACCESS. */
1628 : 421368 : if (USING_DECL_UNRELATED_P (using_decl))
1629 : : {
1630 : : /* C++20 using enum can import non-inherited enumerators into class
1631 : : scope. We implement that by making a copy of the CONST_DECL for which
1632 : : CONST_DECL_USING_P is true. */
1633 : 261388 : gcc_assert (TREE_CODE (decl) == CONST_DECL);
1634 : :
1635 : 261388 : auto cas = make_temp_override (current_access_specifier, access);
1636 : 261388 : tree copy = copy_decl (decl);
1637 : 261388 : DECL_CONTEXT (copy) = t;
1638 : 261388 : DECL_ARTIFICIAL (copy) = true;
1639 : : /* We emitted debug info for the USING_DECL above; make sure we don't
1640 : : also emit anything for this clone. */
1641 : 261388 : DECL_IGNORED_P (copy) = true;
1642 : 261388 : DECL_SOURCE_LOCATION (copy) = DECL_SOURCE_LOCATION (using_decl);
1643 : 261388 : finish_member_declaration (copy);
1644 : 261388 : DECL_ABSTRACT_ORIGIN (copy) = decl;
1645 : 261388 : }
1646 : : else
1647 : 159980 : alter_access (t, decl, access);
1648 : 421368 : }
1649 : :
1650 : : /* Data structure for find_abi_tags_r, below. */
1651 : :
1652 : : struct abi_tag_data
1653 : : {
1654 : : tree t; // The type that we're checking for missing tags.
1655 : : tree subob; // The subobject of T that we're getting tags from.
1656 : : tree tags; // error_mark_node for diagnostics, or a list of missing tags.
1657 : : };
1658 : :
1659 : : /* Subroutine of find_abi_tags_r. Handle a single TAG found on the class TP
1660 : : in the context of P. TAG can be either an identifier (the DECL_NAME of
1661 : : a tag NAMESPACE_DECL) or a STRING_CST (a tag attribute). */
1662 : :
1663 : : static void
1664 : 11735551 : check_tag (tree tag, tree id, tree *tp, abi_tag_data *p)
1665 : : {
1666 : 11735551 : if (!IDENTIFIER_MARKED (id))
1667 : : {
1668 : 5004502 : if (p->tags != error_mark_node)
1669 : : {
1670 : : /* We're collecting tags from template arguments or from
1671 : : the type of a variable or function return type. */
1672 : 5004478 : p->tags = tree_cons (NULL_TREE, tag, p->tags);
1673 : :
1674 : : /* Don't inherit this tag multiple times. */
1675 : 5004478 : IDENTIFIER_MARKED (id) = true;
1676 : :
1677 : 5004478 : if (TYPE_P (p->t))
1678 : : {
1679 : : /* Tags inherited from type template arguments are only used
1680 : : to avoid warnings. */
1681 : 4986711 : ABI_TAG_IMPLICIT (p->tags) = true;
1682 : 4986711 : return;
1683 : : }
1684 : : /* For functions and variables we want to warn, too. */
1685 : : }
1686 : :
1687 : : /* Otherwise we're diagnosing missing tags. */
1688 : 17791 : if (TREE_CODE (p->t) == FUNCTION_DECL)
1689 : : {
1690 : 17374 : auto_diagnostic_group d;
1691 : 17374 : if (warning (OPT_Wabi_tag, "%qD inherits the %E ABI tag "
1692 : : "that %qT (used in its return type) has",
1693 : : p->t, tag, *tp))
1694 : 4 : inform (location_of (*tp), "%qT declared here", *tp);
1695 : 17374 : }
1696 : 417 : else if (VAR_P (p->t))
1697 : : {
1698 : 393 : auto_diagnostic_group d;
1699 : 393 : if (warning (OPT_Wabi_tag, "%qD inherits the %E ABI tag "
1700 : : "that %qT (used in its type) has", p->t, tag, *tp))
1701 : 4 : inform (location_of (*tp), "%qT declared here", *tp);
1702 : 393 : }
1703 : 24 : else if (TYPE_P (p->subob))
1704 : : {
1705 : 8 : auto_diagnostic_group d;
1706 : 8 : if (warning (OPT_Wabi_tag, "%qT does not have the %E ABI tag "
1707 : : "that base %qT has", p->t, tag, p->subob))
1708 : 8 : inform (location_of (p->subob), "%qT declared here",
1709 : : p->subob);
1710 : 8 : }
1711 : : else
1712 : : {
1713 : 16 : auto_diagnostic_group d;
1714 : 16 : if (warning (OPT_Wabi_tag, "%qT does not have the %E ABI tag "
1715 : : "that %qT (used in the type of %qD) has",
1716 : : p->t, tag, *tp, p->subob))
1717 : : {
1718 : 16 : inform (location_of (p->subob), "%qD declared here",
1719 : : p->subob);
1720 : 16 : inform (location_of (*tp), "%qT declared here", *tp);
1721 : : }
1722 : 16 : }
1723 : : }
1724 : : }
1725 : :
1726 : : /* Find all the ABI tags in the attribute list ATTR and either call
1727 : : check_tag (if TP is non-null) or set IDENTIFIER_MARKED to val. */
1728 : :
1729 : : static void
1730 : 1542868815 : mark_or_check_attr_tags (tree attr, tree *tp, abi_tag_data *p, bool val)
1731 : : {
1732 : 1542868815 : if (!attr)
1733 : : return;
1734 : 168627658 : for (; (attr = lookup_attribute ("abi_tag", attr));
1735 : 66379917 : attr = TREE_CHAIN (attr))
1736 : 132759870 : for (tree list = TREE_VALUE (attr); list;
1737 : 66379953 : list = TREE_CHAIN (list))
1738 : : {
1739 : 66379953 : tree tag = TREE_VALUE (list);
1740 : 66379953 : tree id = get_identifier (TREE_STRING_POINTER (tag));
1741 : 66379953 : if (tp)
1742 : 11735551 : check_tag (tag, id, tp, p);
1743 : : else
1744 : 54644402 : IDENTIFIER_MARKED (id) = val;
1745 : : }
1746 : : }
1747 : :
1748 : : /* Find all the ABI tags on T and its enclosing scopes and either call
1749 : : check_tag (if TP is non-null) or set IDENTIFIER_MARKED to val. */
1750 : :
1751 : : static void
1752 : 589630282 : mark_or_check_tags (tree t, tree *tp, abi_tag_data *p, bool val)
1753 : : {
1754 : 2132499097 : while (t != global_namespace)
1755 : : {
1756 : 1542868815 : tree attr;
1757 : 1542868815 : if (TYPE_P (t))
1758 : : {
1759 : 601056610 : attr = TYPE_ATTRIBUTES (t);
1760 : 601056610 : t = CP_TYPE_CONTEXT (t);
1761 : : }
1762 : : else
1763 : : {
1764 : 941812205 : attr = DECL_ATTRIBUTES (t);
1765 : 941812205 : t = CP_DECL_CONTEXT (t);
1766 : : }
1767 : 1542868815 : mark_or_check_attr_tags (attr, tp, p, val);
1768 : : }
1769 : 589630282 : }
1770 : :
1771 : : /* walk_tree callback for check_abi_tags: if the type at *TP involves any
1772 : : types with ABI tags, add the corresponding identifiers to the VEC in
1773 : : *DATA and set IDENTIFIER_MARKED. */
1774 : :
1775 : : static tree
1776 : 447055184 : find_abi_tags_r (tree *tp, int *walk_subtrees, void *data)
1777 : : {
1778 : 447055184 : if (TYPE_P (*tp) && *walk_subtrees == 1 && flag_abi_version != 14)
1779 : : /* Tell cp_walk_subtrees to look though typedefs. [PR98481] */
1780 : 346826353 : *walk_subtrees = 2;
1781 : :
1782 : 447055184 : if (!OVERLOAD_TYPE_P (*tp))
1783 : : return NULL_TREE;
1784 : :
1785 : : /* walk_tree shouldn't be walking into any subtrees of a RECORD_TYPE
1786 : : anyway, but let's make sure of it. */
1787 : 103009404 : *walk_subtrees = false;
1788 : :
1789 : 103009404 : abi_tag_data *p = static_cast<struct abi_tag_data*>(data);
1790 : :
1791 : 103009404 : mark_or_check_tags (*tp, tp, p, false);
1792 : :
1793 : 103009404 : return NULL_TREE;
1794 : : }
1795 : :
1796 : : /* walk_tree callback for mark_abi_tags: if *TP is a class, set
1797 : : IDENTIFIER_MARKED on its ABI tags. */
1798 : :
1799 : : static tree
1800 : 500614386 : mark_abi_tags_r (tree *tp, int *walk_subtrees, void *data)
1801 : : {
1802 : 500614386 : if (TYPE_P (*tp) && *walk_subtrees == 1 && flag_abi_version != 14)
1803 : : /* Tell cp_walk_subtrees to look though typedefs. */
1804 : 499692636 : *walk_subtrees = 2;
1805 : :
1806 : 500614386 : if (!OVERLOAD_TYPE_P (*tp))
1807 : : return NULL_TREE;
1808 : :
1809 : : /* walk_tree shouldn't be walking into any subtrees of a RECORD_TYPE
1810 : : anyway, but let's make sure of it. */
1811 : 95792298 : *walk_subtrees = false;
1812 : :
1813 : 95792298 : bool *valp = static_cast<bool*>(data);
1814 : :
1815 : 95792298 : mark_or_check_tags (*tp, NULL, NULL, *valp);
1816 : :
1817 : 95792298 : return NULL_TREE;
1818 : : }
1819 : :
1820 : : /* Set IDENTIFIER_MARKED on all the ABI tags on T and its enclosing
1821 : : scopes. */
1822 : :
1823 : : static void
1824 : 390828580 : mark_abi_tags (tree t, bool val)
1825 : : {
1826 : 390828580 : mark_or_check_tags (t, NULL, NULL, val);
1827 : 390828580 : if (DECL_P (t))
1828 : : {
1829 : 428873258 : if (DECL_LANG_SPECIFIC (t) && DECL_USE_TEMPLATE (t)
1830 : 349345022 : && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (t)))
1831 : : {
1832 : : /* Template arguments are part of the signature. */
1833 : 9435700 : tree level = INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (t));
1834 : 24790018 : for (int j = 0; j < TREE_VEC_LENGTH (level); ++j)
1835 : : {
1836 : 15354318 : tree arg = TREE_VEC_ELT (level, j);
1837 : 15354318 : cp_walk_tree_without_duplicates (&arg, mark_abi_tags_r, &val);
1838 : : }
1839 : : }
1840 : 215614256 : if (TREE_CODE (t) == FUNCTION_DECL)
1841 : : /* A function's parameter types are part of the signature, so
1842 : : we don't need to inherit any tags that are also in them. */
1843 : 568679584 : for (tree arg = FUNCTION_FIRST_USER_PARMTYPE (t); arg;
1844 : 380274726 : arg = TREE_CHAIN (arg))
1845 : 380274726 : cp_walk_tree_without_duplicates (&TREE_VALUE (arg),
1846 : : mark_abi_tags_r, &val);
1847 : : }
1848 : 390828580 : }
1849 : :
1850 : : /* Check that T has all the ABI tags that subobject SUBOB has, or
1851 : : warn if not. If T is a (variable or function) declaration, also
1852 : : return any missing tags, and add them to T if JUST_CHECKING is false. */
1853 : :
1854 : : static tree
1855 : 141860121 : check_abi_tags (tree t, tree subob, bool just_checking = false)
1856 : : {
1857 : 141860121 : bool inherit = DECL_P (t);
1858 : :
1859 : 141860121 : if (!inherit && !warn_abi_tag)
1860 : : return NULL_TREE;
1861 : :
1862 : 108035610 : tree decl = TYPE_P (t) ? TYPE_NAME (t) : t;
1863 : 108035610 : if (!TREE_PUBLIC (decl))
1864 : : /* No need to worry about things local to this TU. */
1865 : : return NULL_TREE;
1866 : :
1867 : 107807156 : mark_abi_tags (t, true);
1868 : :
1869 : 107807156 : tree subtype = TYPE_P (subob) ? subob : TREE_TYPE (subob);
1870 : 107807156 : struct abi_tag_data data = { t, subob, error_mark_node };
1871 : 107807156 : if (inherit)
1872 : 107807128 : data.tags = NULL_TREE;
1873 : :
1874 : 107807156 : cp_walk_tree_without_duplicates (&subtype, find_abi_tags_r, &data);
1875 : :
1876 : 107807156 : if (!(inherit && data.tags))
1877 : : /* We don't need to do anything with data.tags. */;
1878 : 17749 : else if (just_checking)
1879 : 524 : for (tree t = data.tags; t; t = TREE_CHAIN (t))
1880 : : {
1881 : 268 : tree id = get_identifier (TREE_STRING_POINTER (TREE_VALUE (t)));
1882 : 268 : IDENTIFIER_MARKED (id) = false;
1883 : : }
1884 : : else
1885 : : {
1886 : 17493 : tree attr = lookup_attribute ("abi_tag", DECL_ATTRIBUTES (t));
1887 : 17493 : if (attr)
1888 : 0 : TREE_VALUE (attr) = chainon (data.tags, TREE_VALUE (attr));
1889 : : else
1890 : 17493 : DECL_ATTRIBUTES (t)
1891 : 34986 : = tree_cons (abi_tag_identifier, data.tags, DECL_ATTRIBUTES (t));
1892 : : }
1893 : :
1894 : 107807156 : mark_abi_tags (t, false);
1895 : :
1896 : 107807156 : return data.tags;
1897 : : }
1898 : :
1899 : : /* Check that DECL has all the ABI tags that are used in parts of its type
1900 : : that are not reflected in its mangled name. */
1901 : :
1902 : : void
1903 : 115662362 : check_abi_tags (tree decl)
1904 : : {
1905 : 115662362 : if (VAR_P (decl))
1906 : 13685898 : check_abi_tags (decl, TREE_TYPE (decl));
1907 : 101976464 : else if (TREE_CODE (decl) == FUNCTION_DECL
1908 : 187794197 : && !DECL_CONV_FN_P (decl)
1909 : 203052238 : && !mangle_return_type_p (decl))
1910 : 93806275 : check_abi_tags (decl, TREE_TYPE (TREE_TYPE (decl)));
1911 : 115662362 : }
1912 : :
1913 : : /* Return any ABI tags that are used in parts of the type of DECL
1914 : : that are not reflected in its mangled name. This function is only
1915 : : used in backward-compatible mangling for ABI <11. */
1916 : :
1917 : : tree
1918 : 614154 : missing_abi_tags (tree decl)
1919 : : {
1920 : 614154 : if (VAR_P (decl))
1921 : 34445 : return check_abi_tags (decl, TREE_TYPE (decl), true);
1922 : 579709 : else if (TREE_CODE (decl) == FUNCTION_DECL
1923 : : /* Don't check DECL_CONV_FN_P here like we do in check_abi_tags, so
1924 : : that we can use this function for setting need_abi_warning
1925 : : regardless of the current flag_abi_version. */
1926 : 579709 : && !mangle_return_type_p (decl))
1927 : 508960 : return check_abi_tags (decl, TREE_TYPE (TREE_TYPE (decl)), true);
1928 : : else
1929 : 70749 : return NULL_TREE;
1930 : : }
1931 : :
1932 : : void
1933 : 87917283 : inherit_targ_abi_tags (tree t)
1934 : : {
1935 : 87607134 : if (!CLASS_TYPE_P (t)
1936 : 175524417 : || CLASSTYPE_TEMPLATE_INFO (t) == NULL_TREE)
1937 : 310149 : return;
1938 : :
1939 : 87607134 : mark_abi_tags (t, true);
1940 : :
1941 : 87607134 : tree args = CLASSTYPE_TI_ARGS (t);
1942 : 87607134 : struct abi_tag_data data = { t, NULL_TREE, NULL_TREE };
1943 : 525643028 : for (int i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
1944 : : {
1945 : 179392320 : tree level = TMPL_ARGS_LEVEL (args, i+1);
1946 : 252579277 : for (int j = 0; j < TREE_VEC_LENGTH (level); ++j)
1947 : : {
1948 : 162883117 : tree arg = TREE_VEC_ELT (level, j);
1949 : 162883117 : data.subob = arg;
1950 : 162883117 : cp_walk_tree_without_duplicates (&arg, find_abi_tags_r, &data);
1951 : : }
1952 : : }
1953 : :
1954 : : // If we found some tags on our template arguments, add them to our
1955 : : // abi_tag attribute.
1956 : 87607134 : if (data.tags)
1957 : : {
1958 : 4986711 : tree attr = lookup_attribute ("abi_tag", TYPE_ATTRIBUTES (t));
1959 : 4986711 : if (attr)
1960 : 4 : TREE_VALUE (attr) = chainon (data.tags, TREE_VALUE (attr));
1961 : : else
1962 : 4986707 : TYPE_ATTRIBUTES (t)
1963 : 9973414 : = tree_cons (abi_tag_identifier, data.tags, TYPE_ATTRIBUTES (t));
1964 : : }
1965 : :
1966 : 87607134 : mark_abi_tags (t, false);
1967 : : }
1968 : :
1969 : : /* Return true, iff class T has a non-virtual destructor that is
1970 : : accessible from outside the class heirarchy (i.e. is public, or
1971 : : there's a suitable friend. */
1972 : :
1973 : : static bool
1974 : 129 : accessible_nvdtor_p (tree t)
1975 : : {
1976 : 129 : tree dtor = CLASSTYPE_DESTRUCTOR (t);
1977 : :
1978 : : /* An implicitly declared destructor is always public. And,
1979 : : if it were virtual, we would have created it by now. */
1980 : 129 : if (!dtor)
1981 : : return true;
1982 : :
1983 : 102 : if (DECL_VINDEX (dtor))
1984 : : return false; /* Virtual */
1985 : :
1986 : 54 : if (!TREE_PRIVATE (dtor) && !TREE_PROTECTED (dtor))
1987 : : return true; /* Public */
1988 : :
1989 : 38 : if (CLASSTYPE_FRIEND_CLASSES (t)
1990 : 38 : || DECL_FRIENDLIST (TYPE_MAIN_DECL (t)))
1991 : 16 : return true; /* Has friends */
1992 : :
1993 : : return false;
1994 : : }
1995 : :
1996 : : /* Run through the base classes of T, updating CANT_HAVE_CONST_CTOR_P,
1997 : : and NO_CONST_ASN_REF_P. Also set flag bits in T based on
1998 : : properties of the bases. */
1999 : :
2000 : : static void
2001 : 36143417 : check_bases (tree t,
2002 : : int* cant_have_const_ctor_p,
2003 : : int* no_const_asn_ref_p)
2004 : : {
2005 : 36143417 : int i;
2006 : 36143417 : bool seen_non_virtual_nearly_empty_base_p = 0;
2007 : 36143417 : int seen_tm_mask = 0;
2008 : 36143417 : tree base_binfo;
2009 : 36143417 : tree binfo;
2010 : 36143417 : tree field = NULL_TREE;
2011 : :
2012 : 36143417 : if (!CLASSTYPE_NON_STD_LAYOUT (t))
2013 : 169426063 : for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
2014 : 138729230 : if (TREE_CODE (field) == FIELD_DECL)
2015 : : break;
2016 : :
2017 : 55258331 : for (binfo = TYPE_BINFO (t), i = 0;
2018 : 55258331 : BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
2019 : : {
2020 : 19114914 : tree basetype = TREE_TYPE (base_binfo);
2021 : :
2022 : 19114914 : gcc_assert (COMPLETE_TYPE_P (basetype));
2023 : :
2024 : 19114914 : if (CLASSTYPE_FINAL (basetype))
2025 : 11 : error ("cannot derive from %<final%> base %qT in derived type %qT",
2026 : : basetype, t);
2027 : :
2028 : : /* If any base class is non-literal, so is the derived class. */
2029 : 19114914 : if (!CLASSTYPE_LITERAL_P (basetype))
2030 : 2117244 : CLASSTYPE_LITERAL_P (t) = false;
2031 : :
2032 : : /* If the base class doesn't have copy constructors or
2033 : : assignment operators that take const references, then the
2034 : : derived class cannot have such a member automatically
2035 : : generated. */
2036 : 19114914 : if (TYPE_HAS_COPY_CTOR (basetype)
2037 : 19114914 : && ! TYPE_HAS_CONST_COPY_CTOR (basetype))
2038 : 50 : *cant_have_const_ctor_p = 1;
2039 : 19114914 : if (TYPE_HAS_COPY_ASSIGN (basetype)
2040 : 19114914 : && !TYPE_HAS_CONST_COPY_ASSIGN (basetype))
2041 : 15 : *no_const_asn_ref_p = 1;
2042 : :
2043 : 19114914 : if (BINFO_VIRTUAL_P (base_binfo))
2044 : : /* A virtual base does not effect nearly emptiness. */
2045 : : ;
2046 : 19067700 : else if (CLASSTYPE_NEARLY_EMPTY_P (basetype))
2047 : : {
2048 : 264279 : if (seen_non_virtual_nearly_empty_base_p)
2049 : : /* And if there is more than one nearly empty base, then the
2050 : : derived class is not nearly empty either. */
2051 : 1238 : CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
2052 : : else
2053 : : /* Remember we've seen one. */
2054 : : seen_non_virtual_nearly_empty_base_p = 1;
2055 : : }
2056 : 18803421 : else if (!is_empty_class (basetype))
2057 : : /* If the base class is not empty or nearly empty, then this
2058 : : class cannot be nearly empty. */
2059 : 1786123 : CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
2060 : :
2061 : : /* A lot of properties from the bases also apply to the derived
2062 : : class. */
2063 : 19114914 : TYPE_NEEDS_CONSTRUCTING (t) |= TYPE_NEEDS_CONSTRUCTING (basetype);
2064 : 57344742 : TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t)
2065 : 19114914 : |= TYPE_HAS_NONTRIVIAL_DESTRUCTOR (basetype);
2066 : 19114914 : TYPE_HAS_COMPLEX_COPY_ASSIGN (t)
2067 : 19114914 : |= (TYPE_HAS_COMPLEX_COPY_ASSIGN (basetype)
2068 : 19114914 : || !TYPE_HAS_COPY_ASSIGN (basetype));
2069 : 38229828 : TYPE_HAS_COMPLEX_COPY_CTOR (t) |= (TYPE_HAS_COMPLEX_COPY_CTOR (basetype)
2070 : 19114914 : || !TYPE_HAS_COPY_CTOR (basetype));
2071 : 57344742 : TYPE_HAS_COMPLEX_MOVE_ASSIGN (t)
2072 : 19114914 : |= TYPE_HAS_COMPLEX_MOVE_ASSIGN (basetype);
2073 : 19114914 : TYPE_HAS_COMPLEX_MOVE_CTOR (t) |= TYPE_HAS_COMPLEX_MOVE_CTOR (basetype);
2074 : 19114914 : TYPE_POLYMORPHIC_P (t) |= TYPE_POLYMORPHIC_P (basetype);
2075 : 57344742 : CLASSTYPE_CONTAINS_EMPTY_CLASS_P (t)
2076 : 19114914 : |= CLASSTYPE_CONTAINS_EMPTY_CLASS_P (basetype);
2077 : 38229828 : TYPE_HAS_COMPLEX_DFLT (t) |= (!TYPE_HAS_DEFAULT_CONSTRUCTOR (basetype)
2078 : 19114914 : || TYPE_HAS_COMPLEX_DFLT (basetype));
2079 : 19114914 : SET_CLASSTYPE_READONLY_FIELDS_NEED_INIT
2080 : : (t, CLASSTYPE_READONLY_FIELDS_NEED_INIT (t)
2081 : : | CLASSTYPE_READONLY_FIELDS_NEED_INIT (basetype));
2082 : 19114914 : SET_CLASSTYPE_REF_FIELDS_NEED_INIT
2083 : : (t, CLASSTYPE_REF_FIELDS_NEED_INIT (t)
2084 : : | CLASSTYPE_REF_FIELDS_NEED_INIT (basetype));
2085 : 19114914 : if (TYPE_HAS_MUTABLE_P (basetype))
2086 : 918272 : CLASSTYPE_HAS_MUTABLE (t) = 1;
2087 : :
2088 : : /* A standard-layout class is a class that:
2089 : : ...
2090 : : * has no non-standard-layout base classes, */
2091 : 19114914 : CLASSTYPE_NON_STD_LAYOUT (t) |= CLASSTYPE_NON_STD_LAYOUT (basetype);
2092 : 19114914 : if (!CLASSTYPE_NON_STD_LAYOUT (t))
2093 : : {
2094 : 17525725 : tree basefield;
2095 : : /* ...has no base classes of the same type as the first non-static
2096 : : data member... */
2097 : 705847 : if (field && DECL_CONTEXT (field) == t
2098 : 18189080 : && (same_type_ignoring_top_level_qualifiers_p
2099 : 663355 : (TREE_TYPE (field), basetype)))
2100 : 132 : CLASSTYPE_NON_STD_LAYOUT (t) = 1;
2101 : : /* DR 1813:
2102 : : ...has at most one base class subobject of any given type... */
2103 : 17525593 : else if (CLASSTYPE_REPEATED_BASE_P (t))
2104 : 413 : CLASSTYPE_NON_STD_LAYOUT (t) = 1;
2105 : : else
2106 : : /* ...has all non-static data members and bit-fields in the class
2107 : : and its base classes first declared in the same class. */
2108 : 238117432 : for (basefield = TYPE_FIELDS (basetype); basefield;
2109 : 220592252 : basefield = DECL_CHAIN (basefield))
2110 : 221275996 : if (TREE_CODE (basefield) == FIELD_DECL
2111 : 222473635 : && !(DECL_FIELD_IS_BASE (basefield)
2112 : 1197639 : && is_empty_field (basefield)))
2113 : : {
2114 : 683744 : if (field)
2115 : 87106 : CLASSTYPE_NON_STD_LAYOUT (t) = 1;
2116 : : else
2117 : : field = basefield;
2118 : : break;
2119 : : }
2120 : : }
2121 : :
2122 : : /* Don't bother collecting tm attributes if transactional memory
2123 : : support is not enabled. */
2124 : 19114914 : if (flag_tm)
2125 : : {
2126 : 1371 : tree tm_attr = find_tm_attribute (TYPE_ATTRIBUTES (basetype));
2127 : 1371 : if (tm_attr)
2128 : 20 : seen_tm_mask |= tm_attr_to_mask (tm_attr);
2129 : : }
2130 : :
2131 : 19114914 : check_abi_tags (t, basetype);
2132 : : }
2133 : :
2134 : : /* If one of the base classes had TM attributes, and the current class
2135 : : doesn't define its own, then the current class inherits one. */
2136 : 36143417 : if (seen_tm_mask && !find_tm_attribute (TYPE_ATTRIBUTES (t)))
2137 : : {
2138 : 16 : tree tm_attr = tm_mask_to_attr (least_bit_hwi (seen_tm_mask));
2139 : 16 : TYPE_ATTRIBUTES (t) = tree_cons (tm_attr, NULL, TYPE_ATTRIBUTES (t));
2140 : : }
2141 : 36143417 : }
2142 : :
2143 : : /* Determine all the primary bases within T. Sets BINFO_PRIMARY_BASE_P for
2144 : : those that are primaries. Sets BINFO_LOST_PRIMARY_P for those
2145 : : that have had a nearly-empty virtual primary base stolen by some
2146 : : other base in the hierarchy. Determines CLASSTYPE_PRIMARY_BASE for
2147 : : T. */
2148 : :
2149 : : static void
2150 : 36143417 : determine_primary_bases (tree t)
2151 : : {
2152 : 36143417 : unsigned i;
2153 : 36143417 : tree primary = NULL_TREE;
2154 : 36143417 : tree type_binfo = TYPE_BINFO (t);
2155 : 36143417 : tree base_binfo;
2156 : :
2157 : : /* Determine the primary bases of our bases. */
2158 : 59040062 : for (base_binfo = TREE_CHAIN (type_binfo); base_binfo;
2159 : 22896645 : base_binfo = TREE_CHAIN (base_binfo))
2160 : : {
2161 : 22896645 : tree primary = CLASSTYPE_PRIMARY_BINFO (BINFO_TYPE (base_binfo));
2162 : :
2163 : : /* See if we're the non-virtual primary of our inheritance
2164 : : chain. */
2165 : 22896645 : if (!BINFO_VIRTUAL_P (base_binfo))
2166 : : {
2167 : 22700303 : tree parent = BINFO_INHERITANCE_CHAIN (base_binfo);
2168 : 22700303 : tree parent_primary = CLASSTYPE_PRIMARY_BINFO (BINFO_TYPE (parent));
2169 : :
2170 : 22700303 : if (parent_primary
2171 : 22700303 : && SAME_BINFO_TYPE_P (BINFO_TYPE (base_binfo),
2172 : : BINFO_TYPE (parent_primary)))
2173 : : /* We are the primary binfo. */
2174 : 995216 : BINFO_PRIMARY_P (base_binfo) = 1;
2175 : : }
2176 : : /* Determine if we have a virtual primary base, and mark it so.
2177 : : */
2178 : 23899415 : if (primary && BINFO_VIRTUAL_P (primary))
2179 : : {
2180 : 7554 : tree this_primary = copied_binfo (primary, base_binfo);
2181 : :
2182 : 7554 : if (BINFO_PRIMARY_P (this_primary))
2183 : : /* Someone already claimed this base. */
2184 : 782 : BINFO_LOST_PRIMARY_P (base_binfo) = 1;
2185 : : else
2186 : : {
2187 : 6772 : tree delta;
2188 : :
2189 : 6772 : BINFO_PRIMARY_P (this_primary) = 1;
2190 : 6772 : BINFO_INHERITANCE_CHAIN (this_primary) = base_binfo;
2191 : :
2192 : : /* A virtual binfo might have been copied from within
2193 : : another hierarchy. As we're about to use it as a
2194 : : primary base, make sure the offsets match. */
2195 : 6772 : delta = size_diffop_loc (input_location,
2196 : 6772 : fold_convert (ssizetype,
2197 : : BINFO_OFFSET (base_binfo)),
2198 : 6772 : fold_convert (ssizetype,
2199 : : BINFO_OFFSET (this_primary)));
2200 : :
2201 : 6772 : propagate_binfo_offsets (this_primary, delta);
2202 : : }
2203 : : }
2204 : : }
2205 : :
2206 : : /* First look for a dynamic direct non-virtual base. */
2207 : 53742995 : for (i = 0; BINFO_BASE_ITERATE (type_binfo, i, base_binfo); i++)
2208 : : {
2209 : 18946207 : tree basetype = BINFO_TYPE (base_binfo);
2210 : :
2211 : 18946207 : if (TYPE_CONTAINS_VPTR_P (basetype) && !BINFO_VIRTUAL_P (base_binfo))
2212 : : {
2213 : 1346629 : primary = base_binfo;
2214 : 1346629 : goto found;
2215 : : }
2216 : : }
2217 : :
2218 : : /* A "nearly-empty" virtual base class can be the primary base
2219 : : class, if no non-virtual polymorphic base can be found. Look for
2220 : : a nearly-empty virtual dynamic base that is not already a primary
2221 : : base of something in the hierarchy. If there is no such base,
2222 : : just pick the first nearly-empty virtual base. */
2223 : :
2224 : 54652582 : for (base_binfo = TREE_CHAIN (type_binfo); base_binfo;
2225 : 19855794 : base_binfo = TREE_CHAIN (base_binfo))
2226 : 19857599 : if (BINFO_VIRTUAL_P (base_binfo)
2227 : 19857599 : && CLASSTYPE_NEARLY_EMPTY_P (BINFO_TYPE (base_binfo)))
2228 : : {
2229 : 1970 : if (!BINFO_PRIMARY_P (base_binfo))
2230 : : {
2231 : : /* Found one that is not primary. */
2232 : 1805 : primary = base_binfo;
2233 : 1805 : goto found;
2234 : : }
2235 : 165 : else if (!primary)
2236 : : /* Remember the first candidate. */
2237 : 19855794 : primary = base_binfo;
2238 : : }
2239 : :
2240 : 34794983 : found:
2241 : : /* If we've got a primary base, use it. */
2242 : 36143417 : if (primary)
2243 : : {
2244 : 1348535 : tree basetype = BINFO_TYPE (primary);
2245 : :
2246 : 1348535 : CLASSTYPE_PRIMARY_BINFO (t) = primary;
2247 : 1348535 : if (BINFO_PRIMARY_P (primary))
2248 : : /* We are stealing a primary base. */
2249 : 101 : BINFO_LOST_PRIMARY_P (BINFO_INHERITANCE_CHAIN (primary)) = 1;
2250 : 1348535 : BINFO_PRIMARY_P (primary) = 1;
2251 : 1348535 : if (BINFO_VIRTUAL_P (primary))
2252 : : {
2253 : 1906 : tree delta;
2254 : :
2255 : 1906 : BINFO_INHERITANCE_CHAIN (primary) = type_binfo;
2256 : : /* A virtual binfo might have been copied from within
2257 : : another hierarchy. As we're about to use it as a primary
2258 : : base, make sure the offsets match. */
2259 : 1906 : delta = size_diffop_loc (input_location, ssize_int (0),
2260 : 1906 : fold_convert (ssizetype, BINFO_OFFSET (primary)));
2261 : :
2262 : 1906 : propagate_binfo_offsets (primary, delta);
2263 : : }
2264 : :
2265 : 1348535 : primary = TYPE_BINFO (basetype);
2266 : :
2267 : 1348535 : TYPE_VFIELD (t) = TYPE_VFIELD (basetype);
2268 : 1348535 : BINFO_VTABLE (type_binfo) = BINFO_VTABLE (primary);
2269 : 1348535 : BINFO_VIRTUALS (type_binfo) = BINFO_VIRTUALS (primary);
2270 : : }
2271 : 36143417 : }
2272 : :
2273 : : /* Update the variant types of T. */
2274 : :
2275 : : void
2276 : 120188148 : fixup_type_variants (tree type)
2277 : : {
2278 : 120188148 : if (!type)
2279 : : return;
2280 : :
2281 : 120188148 : for (tree variant = TYPE_NEXT_VARIANT (type);
2282 : 240944089 : variant;
2283 : 120755941 : variant = TYPE_NEXT_VARIANT (variant))
2284 : : {
2285 : : /* These fields are in the _TYPE part of the node, not in
2286 : : the TYPE_LANG_SPECIFIC component, so they are not shared. */
2287 : 120755941 : TYPE_HAS_USER_CONSTRUCTOR (variant) = TYPE_HAS_USER_CONSTRUCTOR (type);
2288 : 120755941 : TYPE_NEEDS_CONSTRUCTING (variant) = TYPE_NEEDS_CONSTRUCTING (type);
2289 : 241511882 : TYPE_HAS_NONTRIVIAL_DESTRUCTOR (variant)
2290 : 120755941 : = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type);
2291 : :
2292 : 120755941 : TYPE_POLYMORPHIC_P (variant) = TYPE_POLYMORPHIC_P (type);
2293 : 120755941 : CLASSTYPE_FINAL (variant) = CLASSTYPE_FINAL (type);
2294 : :
2295 : 120755941 : TYPE_BINFO (variant) = TYPE_BINFO (type);
2296 : :
2297 : : /* Copy whatever these are holding today. */
2298 : 120755941 : TYPE_VFIELD (variant) = TYPE_VFIELD (type);
2299 : 120755941 : TYPE_FIELDS (variant) = TYPE_FIELDS (type);
2300 : :
2301 : 120755941 : TYPE_SIZE (variant) = TYPE_SIZE (type);
2302 : 120755941 : TYPE_SIZE_UNIT (variant) = TYPE_SIZE_UNIT (type);
2303 : :
2304 : 120755941 : if (!TYPE_USER_ALIGN (variant)
2305 : 2540364 : || TYPE_NAME (variant) == TYPE_NAME (type)
2306 : 122015138 : || TYPE_ALIGN_RAW (variant) < TYPE_ALIGN_RAW (type))
2307 : : {
2308 : 119496744 : TYPE_ALIGN_RAW (variant) = TYPE_ALIGN_RAW (type);
2309 : 119496744 : TYPE_USER_ALIGN (variant) = TYPE_USER_ALIGN (type);
2310 : : }
2311 : :
2312 : 120755941 : TYPE_PRECISION (variant) = TYPE_PRECISION (type);
2313 : 120755941 : TYPE_MODE_RAW (variant) = TYPE_MODE_RAW (type);
2314 : 120755941 : TYPE_EMPTY_P (variant) = TYPE_EMPTY_P (type);
2315 : : }
2316 : : }
2317 : :
2318 : : /* KLASS is a class that we're applying may_alias to after the body is
2319 : : parsed. Fixup any POINTER_TO and REFERENCE_TO types. The
2320 : : canonical type(s) will be implicitly updated. */
2321 : :
2322 : : static void
2323 : 37505 : fixup_may_alias (tree klass)
2324 : : {
2325 : 37505 : tree t, v;
2326 : :
2327 : 56153 : for (t = TYPE_POINTER_TO (klass); t; t = TYPE_NEXT_PTR_TO (t))
2328 : 55924 : for (v = TYPE_MAIN_VARIANT (t); v; v = TYPE_NEXT_VARIANT (v))
2329 : 37276 : TYPE_REF_CAN_ALIAS_ALL (v) = true;
2330 : 37532 : for (t = TYPE_REFERENCE_TO (klass); t; t = TYPE_NEXT_REF_TO (t))
2331 : 54 : for (v = TYPE_MAIN_VARIANT (t); v; v = TYPE_NEXT_VARIANT (v))
2332 : 27 : TYPE_REF_CAN_ALIAS_ALL (v) = true;
2333 : 37505 : }
2334 : :
2335 : : /* Early variant fixups: we apply attributes at the beginning of the class
2336 : : definition, and we need to fix up any variants that have already been
2337 : : made via elaborated-type-specifier so that check_qualified_type works. */
2338 : :
2339 : : void
2340 : 75795332 : fixup_attribute_variants (tree t)
2341 : : {
2342 : 75795332 : tree variants;
2343 : :
2344 : 75795332 : if (!t)
2345 : : return;
2346 : :
2347 : 75795332 : tree attrs = TYPE_ATTRIBUTES (t);
2348 : 75795332 : unsigned align = TYPE_ALIGN (t);
2349 : 75795332 : bool user_align = TYPE_USER_ALIGN (t);
2350 : 75795332 : bool may_alias = lookup_attribute ("may_alias", attrs);
2351 : 75795332 : bool packed = TYPE_PACKED (t);
2352 : :
2353 : 75795332 : if (may_alias)
2354 : 18777 : fixup_may_alias (t);
2355 : :
2356 : 75795332 : for (variants = TYPE_NEXT_VARIANT (t);
2357 : 113494973 : variants;
2358 : 37699641 : variants = TYPE_NEXT_VARIANT (variants))
2359 : : {
2360 : : /* These are the two fields that check_qualified_type looks at and
2361 : : are affected by attributes. */
2362 : 37699641 : TYPE_ATTRIBUTES (variants) = attrs;
2363 : 37699641 : unsigned valign = align;
2364 : 37699641 : if (TYPE_USER_ALIGN (variants))
2365 : 780 : valign = MAX (valign, TYPE_ALIGN (variants));
2366 : : else
2367 : 37698861 : TYPE_USER_ALIGN (variants) = user_align;
2368 : 37699641 : SET_TYPE_ALIGN (variants, valign);
2369 : 37699641 : TYPE_PACKED (variants) = packed;
2370 : 37699641 : if (may_alias)
2371 : 18728 : fixup_may_alias (variants);
2372 : : }
2373 : : }
2374 : :
2375 : : /* Set memoizing fields and bits of T (and its variants) for later
2376 : : use. */
2377 : :
2378 : : static void
2379 : 36143417 : finish_struct_bits (tree t)
2380 : : {
2381 : : /* Fix up variants (if any). */
2382 : 36143417 : fixup_type_variants (t);
2383 : :
2384 : 36143417 : if (BINFO_N_BASE_BINFOS (TYPE_BINFO (t)) && TYPE_POLYMORPHIC_P (t))
2385 : : /* For a class w/o baseclasses, 'finish_struct' has set
2386 : : CLASSTYPE_PURE_VIRTUALS correctly (by definition).
2387 : : Similarly for a class whose base classes do not have vtables.
2388 : : When neither of these is true, we might have removed abstract
2389 : : virtuals (by providing a definition), added some (by declaring
2390 : : new ones), or redeclared ones from a base class. We need to
2391 : : recalculate what's really an abstract virtual at this point (by
2392 : : looking in the vtables). */
2393 : 1418274 : get_pure_virtuals (t);
2394 : :
2395 : : /* If this type has a copy constructor or a destructor, force its
2396 : : mode to be BLKmode, and force its TREE_ADDRESSABLE bit to be
2397 : : nonzero. This will cause it to be passed by invisible reference
2398 : : and prevent it from being returned in a register. */
2399 : 36143417 : if (type_has_nontrivial_copy_init (t)
2400 : 36143417 : || TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t))
2401 : : {
2402 : 4304527 : tree variants;
2403 : 4304527 : SET_DECL_MODE (TYPE_MAIN_DECL (t), BLKmode);
2404 : 18066672 : for (variants = t; variants; variants = TYPE_NEXT_VARIANT (variants))
2405 : : {
2406 : 13762145 : SET_TYPE_MODE (variants, BLKmode);
2407 : 13762145 : TREE_ADDRESSABLE (variants) = 1;
2408 : : }
2409 : : }
2410 : 36143417 : }
2411 : :
2412 : : /* Issue warnings about T having private constructors, but no friends,
2413 : : and so forth.
2414 : :
2415 : : HAS_NONPRIVATE_METHOD is nonzero if T has any non-private methods or
2416 : : static members. HAS_NONPRIVATE_STATIC_FN is nonzero if T has any
2417 : : non-private static member functions. */
2418 : :
2419 : : static void
2420 : 24299840 : maybe_warn_about_overly_private_class (tree t)
2421 : : {
2422 : 24299840 : int has_member_fn = 0;
2423 : 24299840 : int has_nonprivate_method = 0;
2424 : 24299840 : bool nonprivate_ctor = false;
2425 : :
2426 : 24299840 : if (!warn_ctor_dtor_privacy
2427 : : /* If the class has friends, those entities might create and
2428 : : access instances, so we should not warn. */
2429 : 59 : || (CLASSTYPE_FRIEND_CLASSES (t)
2430 : 59 : || DECL_FRIENDLIST (TYPE_MAIN_DECL (t)))
2431 : : /* We will have warned when the template was declared; there's
2432 : : no need to warn on every instantiation. */
2433 : 24299899 : || CLASSTYPE_TEMPLATE_INSTANTIATION (t))
2434 : : /* There's no reason to even consider warning about this
2435 : : class. */
2436 : : return;
2437 : :
2438 : : /* We only issue one warning, if more than one applies, because
2439 : : otherwise, on code like:
2440 : :
2441 : : class A {
2442 : : // Oops - forgot `public:'
2443 : : A();
2444 : : A(const A&);
2445 : : ~A();
2446 : : };
2447 : :
2448 : : we warn several times about essentially the same problem. */
2449 : :
2450 : : /* Check to see if all (non-constructor, non-destructor) member
2451 : : functions are private. (Since there are no friends or
2452 : : non-private statics, we can't ever call any of the private member
2453 : : functions.) */
2454 : 265 : for (tree fn = TYPE_FIELDS (t); fn; fn = DECL_CHAIN (fn))
2455 : 206 : if (TREE_CODE (fn) == USING_DECL
2456 : 21 : && DECL_NAME (fn) == ctor_identifier
2457 : 209 : && !TREE_PRIVATE (fn))
2458 : : nonprivate_ctor = true;
2459 : 203 : else if (!DECL_DECLARES_FUNCTION_P (fn))
2460 : : /* Not a function. */;
2461 : 118 : else if (DECL_ARTIFICIAL (fn))
2462 : : /* We're not interested in compiler-generated methods; they don't
2463 : : provide any way to call private members. */;
2464 : 118 : else if (!TREE_PRIVATE (fn))
2465 : : {
2466 : 54 : if (DECL_STATIC_FUNCTION_P (fn))
2467 : : /* A non-private static member function is just like a
2468 : : friend; it can create and invoke private member
2469 : : functions, and be accessed without a class
2470 : : instance. */
2471 : : return;
2472 : :
2473 : : has_nonprivate_method = 1;
2474 : : /* Keep searching for a static member function. */
2475 : : }
2476 : 128 : else if (!DECL_CONSTRUCTOR_P (fn) && !DECL_DESTRUCTOR_P (fn))
2477 : : has_member_fn = 1;
2478 : :
2479 : 59 : if (!has_nonprivate_method && has_member_fn)
2480 : : {
2481 : : /* There are no non-private methods, and there's at least one
2482 : : private member function that isn't a constructor or
2483 : : destructor. (If all the private members are
2484 : : constructors/destructors we want to use the code below that
2485 : : issues error messages specifically referring to
2486 : : constructors/destructors.) */
2487 : 16 : unsigned i;
2488 : 16 : tree binfo = TYPE_BINFO (t);
2489 : :
2490 : 16 : for (i = 0; i != BINFO_N_BASE_BINFOS (binfo); i++)
2491 : 0 : if (BINFO_BASE_ACCESS (binfo, i) != access_private_node)
2492 : : {
2493 : : has_nonprivate_method = 1;
2494 : : break;
2495 : : }
2496 : 16 : if (!has_nonprivate_method)
2497 : : {
2498 : 16 : warning (OPT_Wctor_dtor_privacy,
2499 : : "all member functions in class %qT are private", t);
2500 : 16 : return;
2501 : : }
2502 : : }
2503 : :
2504 : : /* Even if some of the member functions are non-private, the class
2505 : : won't be useful for much if all the constructors or destructors
2506 : : are private: such an object can never be created or destroyed. */
2507 : 43 : if (tree dtor = CLASSTYPE_DESTRUCTOR (t))
2508 : 8 : if (TREE_PRIVATE (dtor))
2509 : : {
2510 : 8 : warning (OPT_Wctor_dtor_privacy,
2511 : : "%q#T only defines a private destructor and has no friends",
2512 : : t);
2513 : 8 : return;
2514 : : }
2515 : :
2516 : : /* Warn about classes that have private constructors and no friends. */
2517 : 35 : if (TYPE_HAS_USER_CONSTRUCTOR (t)
2518 : : /* Implicitly generated constructors are always public. */
2519 : 35 : && !CLASSTYPE_LAZY_DEFAULT_CTOR (t))
2520 : : {
2521 : 24 : tree copy_or_move = NULL_TREE;
2522 : :
2523 : : /* If a non-template class does not define a copy
2524 : : constructor, one is defined for it, enabling it to avoid
2525 : : this warning. For a template class, this does not
2526 : : happen, and so we would normally get a warning on:
2527 : :
2528 : : template <class T> class C { private: C(); };
2529 : :
2530 : : To avoid this asymmetry, we check TYPE_HAS_COPY_CTOR. All
2531 : : complete non-template or fully instantiated classes have this
2532 : : flag set. */
2533 : 24 : if (!TYPE_HAS_COPY_CTOR (t))
2534 : : nonprivate_ctor = true;
2535 : : else
2536 : 61 : for (tree fn : ovl_range (CLASSTYPE_CONSTRUCTORS (t)))
2537 : 32 : if (TREE_PRIVATE (fn))
2538 : 16 : continue;
2539 : 16 : else if (copy_fn_p (fn) || move_fn_p (fn))
2540 : : /* Ideally, we wouldn't count any constructor that takes
2541 : : an argument of the class type as a parameter, because
2542 : : such things cannot be used to construct an instance of
2543 : : the class unless you already have one. */
2544 : : copy_or_move = fn;
2545 : : else
2546 : : {
2547 : : nonprivate_ctor = true;
2548 : : break;
2549 : : }
2550 : :
2551 : 24 : if (!nonprivate_ctor)
2552 : : {
2553 : 18 : bool w = warning (OPT_Wctor_dtor_privacy,
2554 : : "%q#T only defines private constructors and has "
2555 : : "no friends", t);
2556 : 18 : if (w && copy_or_move)
2557 : 6 : inform (DECL_SOURCE_LOCATION (copy_or_move),
2558 : : "%q#D is public, but requires an existing %q#T object",
2559 : : copy_or_move, t);
2560 : 18 : return;
2561 : : }
2562 : : }
2563 : : }
2564 : :
2565 : : /* Make BINFO's vtable have N entries, including RTTI entries,
2566 : : vbase and vcall offsets, etc. Set its type and call the back end
2567 : : to lay it out. */
2568 : :
2569 : : static void
2570 : 1580921 : layout_vtable_decl (tree binfo, int n)
2571 : : {
2572 : 1580921 : tree atype;
2573 : 1580921 : tree vtable;
2574 : :
2575 : 1580921 : atype = build_array_of_n_type (vtable_entry_type, n);
2576 : 1580921 : layout_type (atype);
2577 : :
2578 : : /* We may have to grow the vtable. */
2579 : 1580921 : vtable = get_vtbl_decl_for_binfo (binfo);
2580 : 1580921 : if (!same_type_p (TREE_TYPE (vtable), atype))
2581 : : {
2582 : 549594 : TREE_TYPE (vtable) = atype;
2583 : 549594 : DECL_SIZE (vtable) = DECL_SIZE_UNIT (vtable) = NULL_TREE;
2584 : 549594 : layout_decl (vtable, 0);
2585 : : }
2586 : 1580921 : }
2587 : :
2588 : : /* True iff FNDECL and BASE_FNDECL (both non-static member functions)
2589 : : have the same signature. */
2590 : :
2591 : : int
2592 : 73559122 : same_signature_p (const_tree fndecl, const_tree base_fndecl)
2593 : : {
2594 : : /* One destructor overrides another if they are the same kind of
2595 : : destructor. */
2596 : 113573940 : if (DECL_DESTRUCTOR_P (base_fndecl) && DECL_DESTRUCTOR_P (fndecl)
2597 : 93455491 : && special_function_p (base_fndecl) == special_function_p (fndecl))
2598 : : return 1;
2599 : : /* But a non-destructor never overrides a destructor, nor vice
2600 : : versa, nor do different kinds of destructors override
2601 : : one-another. For example, a complete object destructor does not
2602 : : override a deleting destructor. */
2603 : 179020767 : if (DECL_DESTRUCTOR_P (base_fndecl) || DECL_DESTRUCTOR_P (fndecl))
2604 : : return 0;
2605 : :
2606 : 44270459 : if (DECL_NAME (fndecl) == DECL_NAME (base_fndecl)
2607 : 73269191 : || (DECL_CONV_FN_P (fndecl)
2608 : 40 : && DECL_CONV_FN_P (base_fndecl)
2609 : 16 : && same_type_p (DECL_CONV_FN_TYPE (fndecl),
2610 : : DECL_CONV_FN_TYPE (base_fndecl))))
2611 : : {
2612 : 15269451 : tree fntype = TREE_TYPE (fndecl);
2613 : 15269451 : tree base_fntype = TREE_TYPE (base_fndecl);
2614 : 15269451 : if (type_memfn_quals (fntype) == type_memfn_quals (base_fntype)
2615 : 15269141 : && type_memfn_rqual (fntype) == type_memfn_rqual (base_fntype)
2616 : 30538589 : && compparms (FUNCTION_FIRST_USER_PARMTYPE (fndecl),
2617 : 15269138 : FUNCTION_FIRST_USER_PARMTYPE (base_fndecl)))
2618 : : return 1;
2619 : : }
2620 : : return 0;
2621 : : }
2622 : :
2623 : : /* Returns TRUE if DERIVED is a binfo containing the binfo BASE as a
2624 : : subobject. */
2625 : :
2626 : : static bool
2627 : 233464 : base_derived_from (tree derived, tree base)
2628 : : {
2629 : 233464 : tree probe;
2630 : :
2631 : 233832 : for (probe = base; probe; probe = BINFO_INHERITANCE_CHAIN (probe))
2632 : : {
2633 : 233692 : if (probe == derived)
2634 : : return true;
2635 : 2068 : else if (BINFO_VIRTUAL_P (probe))
2636 : : /* If we meet a virtual base, we can't follow the inheritance
2637 : : any more. See if the complete type of DERIVED contains
2638 : : such a virtual base. */
2639 : 1700 : return (binfo_for_vbase (BINFO_TYPE (probe), BINFO_TYPE (derived))
2640 : 1700 : != NULL_TREE);
2641 : : }
2642 : : return false;
2643 : : }
2644 : :
2645 : 7219226 : struct find_final_overrider_data {
2646 : : /* The function for which we are trying to find a final overrider. */
2647 : : tree fn;
2648 : : /* The base class in which the function was declared. */
2649 : : tree declaring_base;
2650 : : /* The candidate overriders. */
2651 : : tree candidates;
2652 : : /* Path to most derived. */
2653 : : auto_vec<tree> path;
2654 : : };
2655 : :
2656 : : /* Add the overrider along the current path to FFOD->CANDIDATES.
2657 : : Returns true if an overrider was found; false otherwise. */
2658 : :
2659 : : static bool
2660 : 12034840 : dfs_find_final_overrider_1 (tree binfo,
2661 : : find_final_overrider_data *ffod,
2662 : : unsigned depth)
2663 : : {
2664 : 12034840 : tree method;
2665 : :
2666 : : /* If BINFO is not the most derived type, try a more derived class.
2667 : : A definition there will overrider a definition here. */
2668 : 12034840 : if (depth)
2669 : : {
2670 : 4582870 : depth--;
2671 : 4582870 : if (dfs_find_final_overrider_1
2672 : 4582870 : (ffod->path[depth], ffod, depth))
2673 : : return true;
2674 : : }
2675 : :
2676 : 9415981 : method = look_for_overrides_here (BINFO_TYPE (binfo), ffod->fn);
2677 : 9415981 : if (method)
2678 : : {
2679 : 7451970 : tree *candidate = &ffod->candidates;
2680 : :
2681 : : /* Remove any candidates overridden by this new function. */
2682 : 7452666 : while (*candidate)
2683 : : {
2684 : : /* If *CANDIDATE overrides METHOD, then METHOD
2685 : : cannot override anything else on the list. */
2686 : 232768 : if (base_derived_from (TREE_VALUE (*candidate), binfo))
2687 : : return true;
2688 : : /* If METHOD overrides *CANDIDATE, remove *CANDIDATE. */
2689 : 696 : if (base_derived_from (binfo, TREE_VALUE (*candidate)))
2690 : 624 : *candidate = TREE_CHAIN (*candidate);
2691 : : else
2692 : 72 : candidate = &TREE_CHAIN (*candidate);
2693 : : }
2694 : :
2695 : : /* Add the new function. */
2696 : 7219898 : ffod->candidates = tree_cons (method, binfo, ffod->candidates);
2697 : 7219898 : return true;
2698 : : }
2699 : :
2700 : : return false;
2701 : : }
2702 : :
2703 : : /* Called from find_final_overrider via dfs_walk. */
2704 : :
2705 : : static tree
2706 : 27545796 : dfs_find_final_overrider_pre (tree binfo, void *data)
2707 : : {
2708 : 27545796 : find_final_overrider_data *ffod = (find_final_overrider_data *) data;
2709 : :
2710 : 27545796 : if (binfo == ffod->declaring_base)
2711 : 14903940 : dfs_find_final_overrider_1 (binfo, ffod, ffod->path.length ());
2712 : 27545796 : ffod->path.safe_push (binfo);
2713 : :
2714 : 27545796 : return NULL_TREE;
2715 : : }
2716 : :
2717 : : static tree
2718 : 27545796 : dfs_find_final_overrider_post (tree /*binfo*/, void *data)
2719 : : {
2720 : 27545796 : find_final_overrider_data *ffod = (find_final_overrider_data *) data;
2721 : 27545796 : ffod->path.pop ();
2722 : :
2723 : 27545796 : return NULL_TREE;
2724 : : }
2725 : :
2726 : : /* Returns a TREE_LIST whose TREE_PURPOSE is the final overrider for
2727 : : FN and whose TREE_VALUE is the binfo for the base where the
2728 : : overriding occurs. BINFO (in the hierarchy dominated by the binfo
2729 : : DERIVED) is the base object in which FN is declared. */
2730 : :
2731 : : static tree
2732 : 7219226 : find_final_overrider (tree derived, tree binfo, tree fn)
2733 : : {
2734 : 7219226 : find_final_overrider_data ffod;
2735 : :
2736 : : /* Getting this right is a little tricky. This is valid:
2737 : :
2738 : : struct S { virtual void f (); };
2739 : : struct T { virtual void f (); };
2740 : : struct U : public S, public T { };
2741 : :
2742 : : even though calling `f' in `U' is ambiguous. But,
2743 : :
2744 : : struct R { virtual void f(); };
2745 : : struct S : virtual public R { virtual void f (); };
2746 : : struct T : virtual public R { virtual void f (); };
2747 : : struct U : public S, public T { };
2748 : :
2749 : : is not -- there's no way to decide whether to put `S::f' or
2750 : : `T::f' in the vtable for `R'.
2751 : :
2752 : : The solution is to look at all paths to BINFO. If we find
2753 : : different overriders along any two, then there is a problem. */
2754 : 7219226 : if (DECL_THUNK_P (fn))
2755 : 0 : fn = THUNK_TARGET (fn);
2756 : :
2757 : : /* Determine the depth of the hierarchy. */
2758 : 7219226 : ffod.fn = fn;
2759 : 7219226 : ffod.declaring_base = binfo;
2760 : 7219226 : ffod.candidates = NULL_TREE;
2761 : 7219226 : ffod.path.create (30);
2762 : :
2763 : 7219226 : dfs_walk_all (derived, dfs_find_final_overrider_pre,
2764 : : dfs_find_final_overrider_post, &ffod);
2765 : :
2766 : : /* If there was no winner, issue an error message. */
2767 : 7219226 : if (!ffod.candidates || TREE_CHAIN (ffod.candidates))
2768 : 48 : return error_mark_node;
2769 : :
2770 : : return ffod.candidates;
2771 : 7219226 : }
2772 : :
2773 : : /* Return the index of the vcall offset for FN when TYPE is used as a
2774 : : virtual base. */
2775 : :
2776 : : static tree
2777 : 359038 : get_vcall_index (tree fn, tree type)
2778 : : {
2779 : 359038 : vec<tree_pair_s, va_gc> *indices = CLASSTYPE_VCALL_INDICES (type);
2780 : 359038 : tree_pair_p p;
2781 : 359038 : unsigned ix;
2782 : :
2783 : 366948 : FOR_EACH_VEC_SAFE_ELT (indices, ix, p)
2784 : 1096468 : if ((DECL_DESTRUCTOR_P (fn) && DECL_DESTRUCTOR_P (p->purpose))
2785 : 374460 : || same_signature_p (fn, p->purpose))
2786 : 359038 : return p->value;
2787 : :
2788 : : /* There should always be an appropriate index. */
2789 : 0 : gcc_unreachable ();
2790 : : }
2791 : :
2792 : : /* Given a DECL_VINDEX of a virtual function found in BINFO, return the final
2793 : : overrider at that index in the vtable. This should only be used when we
2794 : : know that BINFO is correct for the dynamic type of the object. */
2795 : :
2796 : : tree
2797 : 921 : lookup_vfn_in_binfo (tree idx, tree binfo)
2798 : : {
2799 : 921 : int ix = tree_to_shwi (idx);
2800 : 921 : if (TARGET_VTABLE_USES_DESCRIPTORS)
2801 : : ix /= MAX (TARGET_VTABLE_USES_DESCRIPTORS, 1);
2802 : 1116 : while (BINFO_PRIMARY_P (binfo))
2803 : : /* BINFO_VIRTUALS in a primary base isn't accurate, find the derived
2804 : : class that actually owns the vtable. */
2805 : 195 : binfo = BINFO_INHERITANCE_CHAIN (binfo);
2806 : 921 : tree virtuals = BINFO_VIRTUALS (binfo);
2807 : 921 : return TREE_VALUE (chain_index (ix, virtuals));
2808 : : }
2809 : :
2810 : : /* Update an entry in the vtable for BINFO, which is in the hierarchy
2811 : : dominated by T. FN is the old function; VIRTUALS points to the
2812 : : corresponding position in the new BINFO_VIRTUALS list. IX is the index
2813 : : of that entry in the list. */
2814 : :
2815 : : static void
2816 : 6790402 : update_vtable_entry_for_fn (tree t, tree binfo, tree fn, tree* virtuals,
2817 : : unsigned ix)
2818 : : {
2819 : 6790402 : tree b;
2820 : 6790402 : tree overrider;
2821 : 6790402 : tree delta;
2822 : 6790402 : tree virtual_base;
2823 : 6790402 : tree first_defn;
2824 : 6790402 : tree overrider_fn, overrider_target;
2825 : 6790402 : tree target_fn = DECL_THUNK_P (fn) ? THUNK_TARGET (fn) : fn;
2826 : 6790402 : tree over_return, base_return;
2827 : 6790402 : bool lost = false;
2828 : :
2829 : : /* Find the nearest primary base (possibly binfo itself) which defines
2830 : : this function; this is the class the caller will convert to when
2831 : : calling FN through BINFO. */
2832 : 8688081 : for (b = binfo; ; b = get_primary_binfo (b))
2833 : : {
2834 : 8688081 : gcc_assert (b);
2835 : 8688081 : if (look_for_overrides_here (BINFO_TYPE (b), target_fn))
2836 : : break;
2837 : :
2838 : : /* The nearest definition is from a lost primary. */
2839 : 1897679 : if (BINFO_LOST_PRIMARY_P (b))
2840 : 488 : lost = true;
2841 : : }
2842 : 6790402 : first_defn = b;
2843 : :
2844 : : /* Find the final overrider. */
2845 : 6790402 : overrider = find_final_overrider (TYPE_BINFO (t), b, target_fn);
2846 : 6790402 : if (overrider == error_mark_node)
2847 : : {
2848 : 12 : error ("no unique final overrider for %qD in %qT", target_fn, t);
2849 : 12 : return;
2850 : : }
2851 : 6790390 : overrider_target = overrider_fn = TREE_PURPOSE (overrider);
2852 : :
2853 : : /* Check for adjusting covariant return types. */
2854 : 6790390 : over_return = TREE_TYPE (TREE_TYPE (overrider_target));
2855 : 6790390 : base_return = TREE_TYPE (TREE_TYPE (target_fn));
2856 : :
2857 : 6790390 : if (INDIRECT_TYPE_P (over_return)
2858 : 614599 : && TREE_CODE (over_return) == TREE_CODE (base_return)
2859 : 614587 : && CLASS_TYPE_P (TREE_TYPE (over_return))
2860 : 41916 : && CLASS_TYPE_P (TREE_TYPE (base_return))
2861 : : /* If the overrider is invalid, don't even try. */
2862 : 6832298 : && !DECL_INVALID_OVERRIDER_P (overrider_target))
2863 : : {
2864 : : /* If FN is a covariant thunk, we must figure out the adjustment
2865 : : to the final base FN was converting to. As OVERRIDER_TARGET might
2866 : : also be converting to the return type of FN, we have to
2867 : : combine the two conversions here. */
2868 : 41866 : tree fixed_offset, virtual_offset;
2869 : :
2870 : 41866 : over_return = TREE_TYPE (over_return);
2871 : 41866 : base_return = TREE_TYPE (base_return);
2872 : :
2873 : 41866 : if (DECL_THUNK_P (fn))
2874 : : {
2875 : 80 : gcc_assert (DECL_RESULT_THUNK_P (fn));
2876 : 80 : fixed_offset = ssize_int (THUNK_FIXED_OFFSET (fn));
2877 : 80 : virtual_offset = THUNK_VIRTUAL_OFFSET (fn);
2878 : : }
2879 : : else
2880 : : fixed_offset = virtual_offset = NULL_TREE;
2881 : :
2882 : 80 : if (virtual_offset)
2883 : : /* Find the equivalent binfo within the return type of the
2884 : : overriding function. We will want the vbase offset from
2885 : : there. */
2886 : 72 : virtual_offset = binfo_for_vbase (BINFO_TYPE (virtual_offset),
2887 : : over_return);
2888 : 41794 : else if (!same_type_ignoring_top_level_qualifiers_p
2889 : 41794 : (over_return, base_return))
2890 : : {
2891 : : /* There was no existing virtual thunk (which takes
2892 : : precedence). So find the binfo of the base function's
2893 : : return type within the overriding function's return type.
2894 : : Fortunately we know the covariancy is valid (it
2895 : : has already been checked), so we can just iterate along
2896 : : the binfos, which have been chained in inheritance graph
2897 : : order. Of course it is lame that we have to repeat the
2898 : : search here anyway -- we should really be caching pieces
2899 : : of the vtable and avoiding this repeated work. */
2900 : 468 : tree thunk_binfo = NULL_TREE;
2901 : 468 : tree base_binfo = TYPE_BINFO (base_return);
2902 : :
2903 : : /* Find the base binfo within the overriding function's
2904 : : return type. We will always find a thunk_binfo, except
2905 : : when the covariancy is invalid (which we will have
2906 : : already diagnosed). */
2907 : 468 : if (base_binfo)
2908 : 1224 : for (thunk_binfo = TYPE_BINFO (over_return); thunk_binfo;
2909 : 760 : thunk_binfo = TREE_CHAIN (thunk_binfo))
2910 : 1224 : if (SAME_BINFO_TYPE_P (BINFO_TYPE (thunk_binfo),
2911 : : BINFO_TYPE (base_binfo)))
2912 : : break;
2913 : 468 : gcc_assert (thunk_binfo || errorcount);
2914 : :
2915 : : /* See if virtual inheritance is involved. */
2916 : 468 : for (virtual_offset = thunk_binfo;
2917 : 792 : virtual_offset;
2918 : 324 : virtual_offset = BINFO_INHERITANCE_CHAIN (virtual_offset))
2919 : 646 : if (BINFO_VIRTUAL_P (virtual_offset))
2920 : : break;
2921 : :
2922 : 468 : if (virtual_offset
2923 : 610 : || (thunk_binfo && !BINFO_OFFSET_ZEROP (thunk_binfo)))
2924 : : {
2925 : 409 : tree offset = fold_convert (ssizetype, BINFO_OFFSET (thunk_binfo));
2926 : :
2927 : 409 : if (virtual_offset)
2928 : : {
2929 : : /* We convert via virtual base. Adjust the fixed
2930 : : offset to be from there. */
2931 : 322 : offset =
2932 : 322 : size_diffop (offset,
2933 : : fold_convert (ssizetype,
2934 : : BINFO_OFFSET (virtual_offset)));
2935 : : }
2936 : 409 : if (fixed_offset)
2937 : : /* There was an existing fixed offset, this must be
2938 : : from the base just converted to, and the base the
2939 : : FN was thunking to. */
2940 : 8 : fixed_offset = size_binop (PLUS_EXPR, fixed_offset, offset);
2941 : : else
2942 : : fixed_offset = offset;
2943 : : }
2944 : : }
2945 : :
2946 : 41866 : if (fixed_offset || virtual_offset)
2947 : : /* Replace the overriding function with a covariant thunk. We
2948 : : will emit the overriding function in its own slot as
2949 : : well. */
2950 : 481 : overrider_fn = make_thunk (overrider_target, /*this_adjusting=*/0,
2951 : : fixed_offset, virtual_offset);
2952 : : }
2953 : : else
2954 : 6748524 : gcc_assert (DECL_INVALID_OVERRIDER_P (overrider_target) ||
2955 : : !DECL_THUNK_P (fn));
2956 : :
2957 : : /* If we need a covariant thunk, then we may need to adjust first_defn.
2958 : : The ABI specifies that the thunks emitted with a function are
2959 : : determined by which bases the function overrides, so we need to be
2960 : : sure that we're using a thunk for some overridden base; even if we
2961 : : know that the necessary this adjustment is zero, there may not be an
2962 : : appropriate zero-this-adjustment thunk for us to use since thunks for
2963 : : overriding virtual bases always use the vcall offset.
2964 : :
2965 : : Furthermore, just choosing any base that overrides this function isn't
2966 : : quite right, as this slot won't be used for calls through a type that
2967 : : puts a covariant thunk here. Calling the function through such a type
2968 : : will use a different slot, and that slot is the one that determines
2969 : : the thunk emitted for that base.
2970 : :
2971 : : So, keep looking until we find the base that we're really overriding
2972 : : in this slot: the nearest primary base that doesn't use a covariant
2973 : : thunk in this slot. */
2974 : 6790390 : if (overrider_target != overrider_fn)
2975 : : {
2976 : 481 : if (BINFO_TYPE (b) == DECL_CONTEXT (overrider_target))
2977 : : /* We already know that the overrider needs a covariant thunk. */
2978 : 166 : b = get_primary_binfo (b);
2979 : 40 : for (; ; b = get_primary_binfo (b))
2980 : : {
2981 : 521 : tree main_binfo = TYPE_BINFO (BINFO_TYPE (b));
2982 : 521 : tree bv = chain_index (ix, BINFO_VIRTUALS (main_binfo));
2983 : 521 : if (!DECL_THUNK_P (TREE_VALUE (bv)))
2984 : : break;
2985 : 40 : if (BINFO_LOST_PRIMARY_P (b))
2986 : 8 : lost = true;
2987 : 40 : }
2988 : : first_defn = b;
2989 : : }
2990 : :
2991 : : /* Assume that we will produce a thunk that convert all the way to
2992 : : the final overrider, and not to an intermediate virtual base. */
2993 : 6790390 : virtual_base = NULL_TREE;
2994 : :
2995 : : /* See if we can convert to an intermediate virtual base first, and then
2996 : : use the vcall offset located there to finish the conversion. */
2997 : 6987270 : for (; b; b = BINFO_INHERITANCE_CHAIN (b))
2998 : : {
2999 : : /* If we find the final overrider, then we can stop
3000 : : walking. */
3001 : 6987270 : if (SAME_BINFO_TYPE_P (BINFO_TYPE (b),
3002 : : BINFO_TYPE (TREE_VALUE (overrider))))
3003 : : break;
3004 : :
3005 : : /* If we find a virtual base, and we haven't yet found the
3006 : : overrider, then there is a virtual base between the
3007 : : declaring base (first_defn) and the final overrider. */
3008 : 555918 : if (BINFO_VIRTUAL_P (b))
3009 : : {
3010 : : virtual_base = b;
3011 : : break;
3012 : : }
3013 : : }
3014 : :
3015 : : /* Compute the constant adjustment to the `this' pointer. The
3016 : : `this' pointer, when this function is called, will point at BINFO
3017 : : (or one of its primary bases, which are at the same offset). */
3018 : 6790390 : if (virtual_base)
3019 : : /* The `this' pointer needs to be adjusted from the declaration to
3020 : : the nearest virtual base. */
3021 : 1077114 : delta = size_diffop_loc (input_location,
3022 : 359038 : fold_convert (ssizetype, BINFO_OFFSET (virtual_base)),
3023 : 359038 : fold_convert (ssizetype, BINFO_OFFSET (first_defn)));
3024 : 6431352 : else if (lost)
3025 : : /* If the nearest definition is in a lost primary, we don't need an
3026 : : entry in our vtable. Except possibly in a constructor vtable,
3027 : : if we happen to get our primary back. In that case, the offset
3028 : : will be zero, as it will be a primary base. */
3029 : 428 : delta = size_zero_node;
3030 : : else
3031 : : /* The `this' pointer needs to be adjusted from pointing to
3032 : : BINFO to pointing at the base where the final overrider
3033 : : appears. */
3034 : 19292772 : delta = size_diffop_loc (input_location,
3035 : 6430924 : fold_convert (ssizetype,
3036 : : BINFO_OFFSET (TREE_VALUE (overrider))),
3037 : 6430924 : fold_convert (ssizetype, BINFO_OFFSET (binfo)));
3038 : :
3039 : 6790390 : modify_vtable_entry (t, binfo, overrider_fn, delta, virtuals);
3040 : :
3041 : 6790390 : if (virtual_base)
3042 : 718076 : BV_VCALL_INDEX (*virtuals)
3043 : 718076 : = get_vcall_index (overrider_target, BINFO_TYPE (virtual_base));
3044 : : else
3045 : 6431352 : BV_VCALL_INDEX (*virtuals) = NULL_TREE;
3046 : :
3047 : 6790390 : BV_LOST_PRIMARY (*virtuals) = lost;
3048 : : }
3049 : :
3050 : : /* Called from modify_all_vtables via dfs_walk. */
3051 : :
3052 : : static tree
3053 : 39288558 : dfs_modify_vtables (tree binfo, void* data)
3054 : : {
3055 : 39288558 : tree t = (tree) data;
3056 : 39288558 : tree virtuals;
3057 : 39288558 : tree old_virtuals;
3058 : 39288558 : unsigned ix;
3059 : :
3060 : 39288558 : if (!TYPE_CONTAINS_VPTR_P (BINFO_TYPE (binfo)))
3061 : : /* A base without a vtable needs no modification, and its bases
3062 : : are uninteresting. */
3063 : : return dfs_skip_bases;
3064 : :
3065 : 4177409 : if (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), t)
3066 : 4177409 : && !CLASSTYPE_HAS_PRIMARY_BASE_P (t))
3067 : : /* Don't do the primary vtable, if it's new. */
3068 : : return NULL_TREE;
3069 : :
3070 : 3945023 : if (BINFO_PRIMARY_P (binfo) && !BINFO_VIRTUAL_P (binfo))
3071 : : /* There's no need to modify the vtable for a non-virtual primary
3072 : : base; we're not going to use that vtable anyhow. We do still
3073 : : need to do this for virtual primary bases, as they could become
3074 : : non-primary in a construction vtable. */
3075 : : return NULL_TREE;
3076 : :
3077 : 1603178 : make_new_vtable (t, binfo);
3078 : :
3079 : : /* Now, go through each of the virtual functions in the virtual
3080 : : function table for BINFO. Find the final overrider, and update
3081 : : the BINFO_VIRTUALS list appropriately. */
3082 : 1603178 : for (ix = 0, virtuals = BINFO_VIRTUALS (binfo),
3083 : 1603178 : old_virtuals = BINFO_VIRTUALS (TYPE_BINFO (BINFO_TYPE (binfo)));
3084 : 8393580 : virtuals;
3085 : 6790402 : ix++, virtuals = TREE_CHAIN (virtuals),
3086 : 6790402 : old_virtuals = TREE_CHAIN (old_virtuals))
3087 : 6790402 : update_vtable_entry_for_fn (t,
3088 : : binfo,
3089 : 6790402 : BV_FN (old_virtuals),
3090 : : &virtuals, ix);
3091 : :
3092 : : return NULL_TREE;
3093 : : }
3094 : :
3095 : : /* Update all of the primary and secondary vtables for T. Create new
3096 : : vtables as required, and initialize their RTTI information. Each
3097 : : of the functions in VIRTUALS is declared in T and may override a
3098 : : virtual function from a base class; find and modify the appropriate
3099 : : entries to point to the overriding functions. Returns a list, in
3100 : : declaration order, of the virtual functions that are declared in T,
3101 : : but do not appear in the primary base class vtable, and which
3102 : : should therefore be appended to the end of the vtable for T. */
3103 : :
3104 : : static tree
3105 : 36143417 : modify_all_vtables (tree t, tree virtuals)
3106 : : {
3107 : 36143417 : tree binfo = TYPE_BINFO (t);
3108 : 36143417 : tree *fnsp;
3109 : :
3110 : : /* Mangle the vtable name before entering dfs_walk (c++/51884). */
3111 : 36143417 : if (TYPE_CONTAINS_VPTR_P (t))
3112 : 1580921 : get_vtable_decl (t, false);
3113 : :
3114 : : /* Update all of the vtables. */
3115 : 36143417 : dfs_walk_once (binfo, dfs_modify_vtables, NULL, t);
3116 : :
3117 : : /* Add virtual functions not already in our primary vtable. These
3118 : : will be both those introduced by this class, and those overridden
3119 : : from secondary bases. It does not include virtuals merely
3120 : : inherited from secondary bases. */
3121 : 43431170 : for (fnsp = &virtuals; *fnsp; )
3122 : : {
3123 : 7287753 : tree fn = TREE_VALUE (*fnsp);
3124 : :
3125 : 7287753 : if (!value_member (fn, BINFO_VIRTUALS (binfo))
3126 : 11778436 : || DECL_VINDEX (fn) == error_mark_node)
3127 : : {
3128 : : /* We don't need to adjust the `this' pointer when
3129 : : calling this function. */
3130 : 2797070 : BV_DELTA (*fnsp) = integer_zero_node;
3131 : 2797070 : BV_VCALL_INDEX (*fnsp) = NULL_TREE;
3132 : :
3133 : : /* This is a function not already in our vtable. Keep it. */
3134 : 2797070 : fnsp = &TREE_CHAIN (*fnsp);
3135 : : }
3136 : : else
3137 : : /* We've already got an entry for this function. Skip it. */
3138 : 4490683 : *fnsp = TREE_CHAIN (*fnsp);
3139 : : }
3140 : :
3141 : 36143417 : return virtuals;
3142 : : }
3143 : :
3144 : : /* Get the base virtual function declarations in T that have the
3145 : : indicated NAME. */
3146 : :
3147 : : static void
3148 : 214403 : get_basefndecls (tree name, tree t, vec<tree> *base_fndecls)
3149 : : {
3150 : 214403 : bool found_decls = false;
3151 : :
3152 : : /* Find virtual functions in T with the indicated NAME. */
3153 : 293459 : for (tree method : ovl_range (get_class_binding (t, name)))
3154 : : {
3155 : 58110 : if (TREE_CODE (method) == FUNCTION_DECL && DECL_VINDEX (method))
3156 : : {
3157 : 6542 : base_fndecls->safe_push (method);
3158 : 6542 : found_decls = true;
3159 : : }
3160 : : }
3161 : :
3162 : 214403 : if (found_decls)
3163 : : return;
3164 : :
3165 : 208142 : int n_baseclasses = BINFO_N_BASE_BINFOS (TYPE_BINFO (t));
3166 : 250832 : for (int i = 0; i < n_baseclasses; i++)
3167 : : {
3168 : 42690 : tree basetype = BINFO_TYPE (BINFO_BASE_BINFO (TYPE_BINFO (t), i));
3169 : 42690 : get_basefndecls (name, basetype, base_fndecls);
3170 : : }
3171 : : }
3172 : :
3173 : : /* If this method overrides a virtual method from a base, then mark
3174 : : this member function as being virtual as well. Do 'final' and
3175 : : 'override' checks too. */
3176 : :
3177 : : void
3178 : 115436777 : check_for_override (tree decl, tree ctype)
3179 : : {
3180 : 115436777 : if (TREE_CODE (decl) == TEMPLATE_DECL)
3181 : : /* In [temp.mem] we have:
3182 : :
3183 : : A specialization of a member function template does not
3184 : : override a virtual function from a base class. */
3185 : : return;
3186 : :
3187 : : /* IDENTIFIER_VIRTUAL_P indicates whether the name has ever been
3188 : : used for a vfunc. That avoids the expensive look_for_overrides
3189 : : call that when we know there's nothing to find. As conversion
3190 : : operators for the same type can have distinct identifiers, we
3191 : : cannot optimize those in that way. */
3192 : 95321211 : if ((IDENTIFIER_VIRTUAL_P (DECL_NAME (decl))
3193 : 149471619 : || DECL_CONV_FN_P (decl))
3194 : 12387132 : && look_for_overrides (ctype, decl)
3195 : : /* Check staticness after we've checked if we 'override'. */
3196 : 98537546 : && !DECL_STATIC_FUNCTION_P (decl))
3197 : : {
3198 : : /* Set DECL_VINDEX to a value that is neither an INTEGER_CST nor
3199 : : the error_mark_node so that we know it is an overriding
3200 : : function. */
3201 : 3216327 : DECL_VINDEX (decl) = decl;
3202 : :
3203 : 3216327 : if (warn_override
3204 : 6 : && !DECL_OVERRIDE_P (decl)
3205 : 5 : && !DECL_FINAL_P (decl)
3206 : 3216331 : && !DECL_DESTRUCTOR_P (decl))
3207 : 3 : warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wsuggest_override,
3208 : : "%qD can be marked override", decl);
3209 : : }
3210 : 92104884 : else if (DECL_OVERRIDE_P (decl))
3211 : 20 : error ("%q+#D marked %<override%>, but does not override", decl);
3212 : :
3213 : 95321211 : if (DECL_VIRTUAL_P (decl))
3214 : : {
3215 : : /* Remember this identifier is virtual name. */
3216 : 5755281 : IDENTIFIER_VIRTUAL_P (DECL_NAME (decl)) = true;
3217 : :
3218 : 5755281 : if (!DECL_VINDEX (decl))
3219 : : /* It's a new vfunc. */
3220 : 2538970 : DECL_VINDEX (decl) = error_mark_node;
3221 : :
3222 : 5755281 : if (DECL_DESTRUCTOR_P (decl))
3223 : 1532452 : TYPE_HAS_NONTRIVIAL_DESTRUCTOR (ctype) = true;
3224 : : }
3225 : 89565930 : else if (DECL_FINAL_P (decl))
3226 : 19 : error ("%q+#D marked %<final%>, but is not virtual", decl);
3227 : : }
3228 : :
3229 : : /* Warn about hidden virtual functions that are not overridden in t.
3230 : : We know that constructors and destructors don't apply. */
3231 : :
3232 : : static void
3233 : 329778 : warn_hidden (tree t)
3234 : : {
3235 : 329778 : if (vec<tree, va_gc> *member_vec = CLASSTYPE_MEMBER_VEC (t))
3236 : 1782923 : for (unsigned ix = member_vec->length (); ix--;)
3237 : : {
3238 : 1664144 : tree fns = (*member_vec)[ix];
3239 : :
3240 : 1664144 : if (!OVL_P (fns))
3241 : 1657887 : continue;
3242 : :
3243 : 923911 : tree name = OVL_NAME (fns);
3244 : 923911 : auto_vec<tree, 20> base_fndecls;
3245 : 923911 : tree base_binfo;
3246 : 923911 : tree binfo;
3247 : 923911 : unsigned j;
3248 : :
3249 : 923911 : if (IDENTIFIER_CDTOR_P (name))
3250 : 311269 : continue;
3251 : :
3252 : : /* Iterate through all of the base classes looking for possibly
3253 : : hidden functions. */
3254 : 784355 : for (binfo = TYPE_BINFO (t), j = 0;
3255 : 784355 : BINFO_BASE_ITERATE (binfo, j, base_binfo); j++)
3256 : : {
3257 : 171713 : tree basetype = BINFO_TYPE (base_binfo);
3258 : 171713 : get_basefndecls (name, basetype, &base_fndecls);
3259 : : }
3260 : :
3261 : : /* If there are no functions to hide, continue. */
3262 : 612642 : if (base_fndecls.is_empty ())
3263 : 606385 : continue;
3264 : :
3265 : : /* Remove any overridden functions. */
3266 : 6257 : bool seen_non_override = false;
3267 : 13100 : for (tree fndecl : ovl_range (fns))
3268 : : {
3269 : 6548 : bool any_override = false;
3270 : 6548 : if (TREE_CODE (fndecl) == FUNCTION_DECL
3271 : 6548 : && DECL_VINDEX (fndecl))
3272 : : {
3273 : : /* If the method from the base class has the same
3274 : : signature as the method from the derived class, it
3275 : : has been overridden. Note that we can't move on
3276 : : after finding one match: fndecl might override
3277 : : multiple base fns. */
3278 : 27120 : for (size_t k = 0; k < base_fndecls.length (); k++)
3279 : 7028 : if (base_fndecls[k]
3280 : 7028 : && same_signature_p (fndecl, base_fndecls[k]))
3281 : : {
3282 : 6446 : base_fndecls[k] = NULL_TREE;
3283 : 6446 : any_override = true;
3284 : : }
3285 : : }
3286 : 6532 : if (!any_override)
3287 : : seen_non_override = true;
3288 : : }
3289 : :
3290 : 6257 : if (!seen_non_override && warn_overloaded_virtual == 1)
3291 : : /* All the derived fns override base virtuals. */
3292 : 6135 : return;
3293 : :
3294 : : /* Now give a warning for all base functions without overriders,
3295 : : as they are hidden. */
3296 : 500 : for (tree base_fndecl : base_fndecls)
3297 : 134 : if (base_fndecl)
3298 : : {
3299 : 30 : auto_diagnostic_group d;
3300 : : /* Here we know it is a hider, and no overrider exists. */
3301 : 30 : if (warning_at (location_of (base_fndecl),
3302 : : OPT_Woverloaded_virtual_,
3303 : : "%qD was hidden", base_fndecl))
3304 : 24 : inform (location_of (fns), " by %qD", fns);
3305 : 30 : }
3306 : 923911 : }
3307 : : }
3308 : :
3309 : : /* Recursive helper for finish_struct_anon. */
3310 : :
3311 : : static void
3312 : 139825 : finish_struct_anon_r (tree field)
3313 : : {
3314 : 676053 : for (tree elt = TYPE_FIELDS (TREE_TYPE (field)); elt; elt = DECL_CHAIN (elt))
3315 : : {
3316 : : /* We're generally only interested in entities the user
3317 : : declared, but we also find nested classes by noticing
3318 : : the TYPE_DECL that we create implicitly. You're
3319 : : allowed to put one anonymous union inside another,
3320 : : though, so we explicitly tolerate that. We use
3321 : : TYPE_UNNAMED_P rather than ANON_AGGR_TYPE_P so that
3322 : : we also allow unnamed types used for defining fields. */
3323 : 536228 : if (DECL_ARTIFICIAL (elt)
3324 : 536228 : && (!DECL_IMPLICIT_TYPEDEF_P (elt)
3325 : 57974 : || TYPE_UNNAMED_P (TREE_TYPE (elt))))
3326 : 159147 : continue;
3327 : :
3328 : 377081 : TREE_PRIVATE (elt) = TREE_PRIVATE (field);
3329 : 377081 : TREE_PROTECTED (elt) = TREE_PROTECTED (field);
3330 : :
3331 : : /* Recurse into the anonymous aggregates to correctly handle
3332 : : access control (c++/24926):
3333 : :
3334 : : class A {
3335 : : union {
3336 : : union {
3337 : : int i;
3338 : : };
3339 : : };
3340 : : };
3341 : :
3342 : : int j=A().i; */
3343 : 377081 : if (DECL_NAME (elt) == NULL_TREE
3344 : 377081 : && ANON_AGGR_TYPE_P (TREE_TYPE (elt)))
3345 : 18582 : finish_struct_anon_r (elt);
3346 : : }
3347 : 139825 : }
3348 : :
3349 : : /* Fix up any anonymous union/struct members of T. */
3350 : :
3351 : : static void
3352 : 36143417 : finish_struct_anon (tree t)
3353 : : {
3354 : 250141662 : for (tree field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
3355 : : {
3356 : 213998245 : if (TREE_STATIC (field))
3357 : 9707958 : continue;
3358 : 204290287 : if (TREE_CODE (field) != FIELD_DECL)
3359 : 189955472 : continue;
3360 : :
3361 : 14334815 : if (DECL_NAME (field) == NULL_TREE
3362 : 14334815 : && ANON_AGGR_TYPE_P (TREE_TYPE (field)))
3363 : 121243 : finish_struct_anon_r (field);
3364 : : }
3365 : 36143417 : }
3366 : :
3367 : : /* Add T to CLASSTYPE_DECL_LIST of current_class_type which
3368 : : will be used later during class template instantiation.
3369 : : When FRIEND_P is zero, T can be a static member data (VAR_DECL),
3370 : : a non-static member data (FIELD_DECL), a member function
3371 : : (FUNCTION_DECL), a nested type (RECORD_TYPE, ENUM_TYPE),
3372 : : a typedef (TYPE_DECL) or a member class template (TEMPLATE_DECL)
3373 : : When FRIEND_P is nonzero, T is either a friend class
3374 : : (RECORD_TYPE, TEMPLATE_DECL) or a friend function
3375 : : (FUNCTION_DECL, TEMPLATE_DECL). */
3376 : :
3377 : : void
3378 : 349458667 : maybe_add_class_template_decl_list (tree type, tree t, int friend_p)
3379 : : {
3380 : 349458667 : if (CLASSTYPE_TEMPLATE_INFO (type)
3381 : 349458667 : && TREE_CODE (t) != CONST_DECL)
3382 : : {
3383 : 308916747 : tree purpose = friend_p ? NULL_TREE : type;
3384 : :
3385 : 308916747 : CLASSTYPE_DECL_LIST (type)
3386 : 617833494 : = tree_cons (purpose, t, CLASSTYPE_DECL_LIST (type));
3387 : : }
3388 : 349458667 : }
3389 : :
3390 : : /* This function is called from declare_virt_assop_and_dtor via
3391 : : dfs_walk_all.
3392 : :
3393 : : DATA is a type that direcly or indirectly inherits the base
3394 : : represented by BINFO. If BINFO contains a virtual assignment [copy
3395 : : assignment or move assigment] operator or a virtual constructor,
3396 : : declare that function in DATA if it hasn't been already declared. */
3397 : :
3398 : : static tree
3399 : 3227210 : dfs_declare_virt_assop_and_dtor (tree binfo, void *data)
3400 : : {
3401 : 3227210 : tree bv, fn, t = (tree)data;
3402 : 3227210 : tree opname = assign_op_identifier;
3403 : :
3404 : 3227210 : gcc_assert (t && CLASS_TYPE_P (t));
3405 : 3227210 : gcc_assert (binfo && TREE_CODE (binfo) == TREE_BINFO);
3406 : :
3407 : 3227210 : if (!TYPE_CONTAINS_VPTR_P (BINFO_TYPE (binfo)))
3408 : : /* A base without a vtable needs no modification, and its bases
3409 : : are uninteresting. */
3410 : : return dfs_skip_bases;
3411 : :
3412 : 2711745 : if (BINFO_PRIMARY_P (binfo))
3413 : : /* If this is a primary base, then we have already looked at the
3414 : : virtual functions of its vtable. */
3415 : : return NULL_TREE;
3416 : :
3417 : 10796128 : for (bv = BINFO_VIRTUALS (binfo); bv; bv = TREE_CHAIN (bv))
3418 : : {
3419 : 8084383 : fn = BV_FN (bv);
3420 : :
3421 : 8084383 : if (DECL_NAME (fn) == opname)
3422 : : {
3423 : 28 : if (CLASSTYPE_LAZY_COPY_ASSIGN (t))
3424 : 28 : lazily_declare_fn (sfk_copy_assignment, t);
3425 : 28 : if (CLASSTYPE_LAZY_MOVE_ASSIGN (t))
3426 : 21 : lazily_declare_fn (sfk_move_assignment, t);
3427 : : }
3428 : 8084355 : else if (DECL_DESTRUCTOR_P (fn)
3429 : 8084355 : && CLASSTYPE_LAZY_DESTRUCTOR (t))
3430 : 163231 : lazily_declare_fn (sfk_destructor, t);
3431 : : }
3432 : :
3433 : : return NULL_TREE;
3434 : : }
3435 : :
3436 : : /* If the class type T has a direct or indirect base that contains a
3437 : : virtual assignment operator or a virtual destructor, declare that
3438 : : function in T if it hasn't been already declared. */
3439 : :
3440 : : static void
3441 : 36143417 : declare_virt_assop_and_dtor (tree t)
3442 : : {
3443 : 36143417 : if (!(TYPE_POLYMORPHIC_P (t)
3444 : 1578157 : && (CLASSTYPE_LAZY_COPY_ASSIGN (t)
3445 : 597321 : || CLASSTYPE_LAZY_MOVE_ASSIGN (t)
3446 : 597321 : || CLASSTYPE_LAZY_DESTRUCTOR (t))))
3447 : : return;
3448 : :
3449 : 1033061 : dfs_walk_all (TYPE_BINFO (t),
3450 : : dfs_declare_virt_assop_and_dtor,
3451 : : NULL, t);
3452 : : }
3453 : :
3454 : : /* Declare the inheriting constructor for class T inherited from base
3455 : : constructor CTOR with the parameter array PARMS of size NPARMS. */
3456 : :
3457 : : static void
3458 : 90 : one_inheriting_sig (tree t, tree ctor, tree *parms, int nparms)
3459 : : {
3460 : 90 : gcc_assert (TYPE_MAIN_VARIANT (t) == t);
3461 : :
3462 : : /* We don't declare an inheriting ctor that would be a default,
3463 : : copy or move ctor for derived or base. */
3464 : 90 : if (nparms == 0)
3465 : : return;
3466 : 90 : if (nparms == 1
3467 : 87 : && TYPE_REF_P (parms[0]))
3468 : : {
3469 : 54 : tree parm = TYPE_MAIN_VARIANT (TREE_TYPE (parms[0]));
3470 : 54 : if (parm == t || parm == DECL_CONTEXT (ctor))
3471 : : return;
3472 : : }
3473 : :
3474 : 36 : tree parmlist = void_list_node;
3475 : 75 : for (int i = nparms - 1; i >= 0; i--)
3476 : 39 : parmlist = tree_cons (NULL_TREE, parms[i], parmlist);
3477 : 36 : tree fn = implicitly_declare_fn (sfk_inheriting_constructor,
3478 : : t, false, ctor, parmlist);
3479 : :
3480 : 36 : if (add_method (t, fn, false))
3481 : : {
3482 : 27 : DECL_CHAIN (fn) = TYPE_FIELDS (t);
3483 : 27 : TYPE_FIELDS (t) = fn;
3484 : : }
3485 : : }
3486 : :
3487 : : /* Declare all the inheriting constructors for class T inherited from base
3488 : : constructor CTOR. */
3489 : :
3490 : : static void
3491 : 217401 : one_inherited_ctor (tree ctor, tree t, tree using_decl)
3492 : : {
3493 : 217401 : tree parms = FUNCTION_FIRST_USER_PARMTYPE (ctor);
3494 : :
3495 : 217401 : if (flag_new_inheriting_ctors)
3496 : : {
3497 : 217311 : ctor = implicitly_declare_fn (sfk_inheriting_constructor,
3498 : : t, /*const*/false, ctor, parms);
3499 : 217311 : add_method (t, ctor, using_decl != NULL_TREE);
3500 : 217311 : return;
3501 : : }
3502 : :
3503 : 90 : tree *new_parms = XALLOCAVEC (tree, list_length (parms));
3504 : 90 : int i = 0;
3505 : 183 : for (; parms && parms != void_list_node; parms = TREE_CHAIN (parms))
3506 : : {
3507 : 93 : if (TREE_PURPOSE (parms))
3508 : 0 : one_inheriting_sig (t, ctor, new_parms, i);
3509 : 93 : new_parms[i++] = TREE_VALUE (parms);
3510 : : }
3511 : 90 : one_inheriting_sig (t, ctor, new_parms, i);
3512 : 90 : if (parms == NULL_TREE)
3513 : : {
3514 : 3 : auto_diagnostic_group d;
3515 : 3 : if (warning (OPT_Winherited_variadic_ctor,
3516 : : "the ellipsis in %qD is not inherited", ctor))
3517 : 3 : inform (DECL_SOURCE_LOCATION (ctor), "%qD declared here", ctor);
3518 : 3 : }
3519 : : }
3520 : :
3521 : : /* Implicitly declare T(). */
3522 : :
3523 : : static void
3524 : 30554521 : add_implicit_default_ctor (tree t)
3525 : : {
3526 : 30554521 : TYPE_HAS_DEFAULT_CONSTRUCTOR (t) = 1;
3527 : 30554521 : CLASSTYPE_LAZY_DEFAULT_CTOR (t) = 1;
3528 : 30554521 : if (cxx_dialect >= cxx11)
3529 : 30471936 : TYPE_HAS_CONSTEXPR_CTOR (t)
3530 : : /* Don't force the declaration to get a hard answer; if the
3531 : : definition would have made the class non-literal, it will still be
3532 : : non-literal because of the base or member in question, and that
3533 : : gives a better diagnostic. */
3534 : 60943872 : = type_maybe_constexpr_default_constructor (t);
3535 : 30554521 : }
3536 : :
3537 : : /* Create default constructors, assignment operators, and so forth for
3538 : : the type indicated by T, if they are needed. CANT_HAVE_CONST_CTOR,
3539 : : and CANT_HAVE_CONST_ASSIGNMENT are nonzero if, for whatever reason,
3540 : : the class cannot have a default constructor, copy constructor
3541 : : taking a const reference argument, or an assignment operator taking
3542 : : a const reference, respectively. */
3543 : :
3544 : : static void
3545 : 36143417 : add_implicitly_declared_members (tree t, tree* access_decls,
3546 : : int cant_have_const_cctor,
3547 : : int cant_have_const_assignment)
3548 : : {
3549 : : /* Destructor. */
3550 : 36143417 : if (!CLASSTYPE_DESTRUCTOR (t))
3551 : : /* In general, we create destructors lazily. */
3552 : 32732767 : CLASSTYPE_LAZY_DESTRUCTOR (t) = 1;
3553 : :
3554 : 36143417 : bool move_ok = false;
3555 : 36032229 : if (cxx_dialect >= cxx11 && CLASSTYPE_LAZY_DESTRUCTOR (t)
3556 : 32642186 : && !TYPE_HAS_COPY_CTOR (t) && !TYPE_HAS_COPY_ASSIGN (t)
3557 : 67929522 : && !classtype_has_move_assign_or_move_ctor_p (t, false))
3558 : : move_ok = true;
3559 : :
3560 : : /* [class.ctor]
3561 : :
3562 : : If there is no user-declared constructor for a class, a default
3563 : : constructor is implicitly declared. */
3564 : 36143417 : if (! TYPE_HAS_USER_CONSTRUCTOR (t))
3565 : 30513888 : add_implicit_default_ctor (t);
3566 : :
3567 : : /* [class.ctor]
3568 : :
3569 : : If a class definition does not explicitly declare a copy
3570 : : constructor, one is declared implicitly. */
3571 : 36143417 : if (! TYPE_HAS_COPY_CTOR (t))
3572 : : {
3573 : 33276527 : TYPE_HAS_COPY_CTOR (t) = 1;
3574 : 33276527 : TYPE_HAS_CONST_COPY_CTOR (t) = !cant_have_const_cctor;
3575 : 33276527 : CLASSTYPE_LAZY_COPY_CTOR (t) = 1;
3576 : 33276527 : if (move_ok)
3577 : 31563655 : CLASSTYPE_LAZY_MOVE_CTOR (t) = 1;
3578 : : }
3579 : :
3580 : : /* If there is no assignment operator, one will be created if and
3581 : : when it is needed. For now, just record whether or not the type
3582 : : of the parameter to the assignment operator will be a const or
3583 : : non-const reference. */
3584 : 36143417 : if (!TYPE_HAS_COPY_ASSIGN (t))
3585 : : {
3586 : 33396287 : TYPE_HAS_COPY_ASSIGN (t) = 1;
3587 : 33396287 : TYPE_HAS_CONST_COPY_ASSIGN (t) = !cant_have_const_assignment;
3588 : 33396287 : CLASSTYPE_LAZY_COPY_ASSIGN (t) = 1;
3589 : 64575969 : if (move_ok && !LAMBDA_TYPE_P (t))
3590 : 31326918 : CLASSTYPE_LAZY_MOVE_ASSIGN (t) = 1;
3591 : : }
3592 : :
3593 : : /* We can't be lazy about declaring functions that might override
3594 : : a virtual function from a base class. */
3595 : 36143417 : declare_virt_assop_and_dtor (t);
3596 : :
3597 : : /* If the class definition does not explicitly declare an == operator
3598 : : function, but declares a defaulted three-way comparison operator function,
3599 : : an == operator function is declared implicitly. */
3600 : 36143417 : if (!classtype_has_op (t, EQ_EXPR))
3601 : 35347582 : if (tree space = classtype_has_defaulted_op (t, SPACESHIP_EXPR))
3602 : : {
3603 : 60 : tree eq = implicitly_declare_fn (sfk_comparison, t, false, space,
3604 : : NULL_TREE);
3605 : 60 : bool is_friend = DECL_CONTEXT (space) != t;
3606 : 60 : if (is_friend)
3607 : 9 : do_friend (NULL_TREE, DECL_NAME (eq), eq,
3608 : : NO_SPECIAL, true);
3609 : : else
3610 : : {
3611 : 51 : add_method (t, eq, false);
3612 : 51 : DECL_CHAIN (eq) = TYPE_FIELDS (t);
3613 : 51 : TYPE_FIELDS (t) = eq;
3614 : : }
3615 : 60 : maybe_add_class_template_decl_list (t, eq, is_friend);
3616 : : }
3617 : :
3618 : 37965322 : while (*access_decls)
3619 : : {
3620 : 1821905 : tree using_decl = TREE_VALUE (*access_decls);
3621 : 1821905 : tree decl = USING_DECL_DECLS (using_decl);
3622 : 1821905 : if (DECL_NAME (using_decl) == ctor_identifier)
3623 : : {
3624 : : /* declare, then remove the decl */
3625 : 43281 : tree ctor_list = decl;
3626 : 43281 : location_t loc = input_location;
3627 : 43281 : input_location = DECL_SOURCE_LOCATION (using_decl);
3628 : 435527 : for (tree fn : ovl_range (ctor_list))
3629 : : {
3630 : 217401 : if (!TYPE_HAS_DEFAULT_CONSTRUCTOR (t) && default_ctor_p (fn))
3631 : : /* CWG2799: Inheriting a default constructor gives us a default
3632 : : constructor, not just an inherited constructor. */
3633 : 40633 : add_implicit_default_ctor (t);
3634 : 217401 : one_inherited_ctor (fn, t, using_decl);
3635 : : }
3636 : 43281 : *access_decls = TREE_CHAIN (*access_decls);
3637 : 43281 : input_location = loc;
3638 : : }
3639 : : else
3640 : 1778624 : access_decls = &TREE_CHAIN (*access_decls);
3641 : : }
3642 : 36143417 : }
3643 : :
3644 : : /* Cache of enum_min_precision values. */
3645 : : static GTY((deletable)) hash_map<tree, int> *enum_to_min_precision;
3646 : :
3647 : : /* Return the minimum precision of a bit-field needed to store all
3648 : : enumerators of ENUMERAL_TYPE TYPE. */
3649 : :
3650 : : static int
3651 : 29134 : enum_min_precision (tree type)
3652 : : {
3653 : 29134 : type = TYPE_MAIN_VARIANT (type);
3654 : : /* For unscoped enums without fixed underlying type and without mode
3655 : : attribute we can just use precision of the underlying type. */
3656 : 29134 : if (UNSCOPED_ENUM_P (type)
3657 : 28795 : && !ENUM_FIXED_UNDERLYING_TYPE_P (type)
3658 : 57912 : && !lookup_attribute ("mode", TYPE_ATTRIBUTES (type)))
3659 : 28766 : return TYPE_PRECISION (ENUM_UNDERLYING_TYPE (type));
3660 : :
3661 : 368 : if (enum_to_min_precision == NULL)
3662 : 72 : enum_to_min_precision = hash_map<tree, int>::create_ggc (37);
3663 : :
3664 : 368 : bool existed;
3665 : 368 : int &prec = enum_to_min_precision->get_or_insert (type, &existed);
3666 : 368 : if (existed)
3667 : 284 : return prec;
3668 : :
3669 : 84 : tree minnode, maxnode;
3670 : 84 : if (TYPE_VALUES (type))
3671 : : {
3672 : : minnode = maxnode = NULL_TREE;
3673 : 372 : for (tree values = TYPE_VALUES (type);
3674 : 454 : values; values = TREE_CHAIN (values))
3675 : : {
3676 : 372 : tree decl = TREE_VALUE (values);
3677 : 372 : tree value = DECL_INITIAL (decl);
3678 : 372 : if (value == error_mark_node)
3679 : 0 : value = integer_zero_node;
3680 : 372 : if (!minnode)
3681 : : minnode = maxnode = value;
3682 : 290 : else if (tree_int_cst_lt (maxnode, value))
3683 : : maxnode = value;
3684 : 0 : else if (tree_int_cst_lt (value, minnode))
3685 : 0 : minnode = value;
3686 : : }
3687 : : }
3688 : : else
3689 : 2 : minnode = maxnode = integer_zero_node;
3690 : :
3691 : 84 : signop sgn = tree_int_cst_sgn (minnode) >= 0 ? UNSIGNED : SIGNED;
3692 : 84 : int lowprec = tree_int_cst_min_precision (minnode, sgn);
3693 : 84 : int highprec = tree_int_cst_min_precision (maxnode, sgn);
3694 : 84 : prec = MAX (lowprec, highprec);
3695 : 84 : return prec;
3696 : : }
3697 : :
3698 : : /* FIELD is a bit-field. We are finishing the processing for its
3699 : : enclosing type. Issue any appropriate messages and set appropriate
3700 : : flags. Returns false if an error has been diagnosed. */
3701 : :
3702 : : static bool
3703 : 367813 : check_bitfield_decl (tree field)
3704 : : {
3705 : 367813 : tree type = TREE_TYPE (field);
3706 : 367813 : tree w;
3707 : :
3708 : : /* Extract the declared width of the bitfield, which has been
3709 : : temporarily stashed in DECL_BIT_FIELD_REPRESENTATIVE by grokbitfield. */
3710 : 367813 : w = DECL_BIT_FIELD_REPRESENTATIVE (field);
3711 : 367813 : gcc_assert (w != NULL_TREE);
3712 : : /* Remove the bit-field width indicator so that the rest of the
3713 : : compiler does not treat that value as a qualifier. */
3714 : 367813 : DECL_BIT_FIELD_REPRESENTATIVE (field) = NULL_TREE;
3715 : :
3716 : : /* Detect invalid bit-field type. */
3717 : 367813 : if (!INTEGRAL_OR_ENUMERATION_TYPE_P (type))
3718 : : {
3719 : 4 : error_at (DECL_SOURCE_LOCATION (field),
3720 : : "bit-field %q#D with non-integral type %qT", field, type);
3721 : 4 : w = error_mark_node;
3722 : : }
3723 : : else
3724 : : {
3725 : 367809 : location_t loc = input_location;
3726 : : /* Avoid the non_lvalue wrapper added by fold for PLUS_EXPRs. */
3727 : 367809 : STRIP_NOPS (w);
3728 : :
3729 : : /* detect invalid field size. */
3730 : 367809 : input_location = DECL_SOURCE_LOCATION (field);
3731 : 367809 : w = cxx_constant_value (w);
3732 : 367809 : input_location = loc;
3733 : :
3734 : 367809 : if (TREE_CODE (w) != INTEGER_CST)
3735 : : {
3736 : 19 : error ("bit-field %q+D width not an integer constant", field);
3737 : 19 : w = error_mark_node;
3738 : : }
3739 : 367790 : else if (tree_int_cst_sgn (w) < 0)
3740 : : {
3741 : 0 : error ("negative width in bit-field %q+D", field);
3742 : 0 : w = error_mark_node;
3743 : : }
3744 : 367790 : else if (integer_zerop (w) && DECL_NAME (field) != 0)
3745 : : {
3746 : 18 : error ("zero width for bit-field %q+D", field);
3747 : 18 : w = error_mark_node;
3748 : : }
3749 : 367772 : else if ((TREE_CODE (type) != ENUMERAL_TYPE
3750 : 367772 : && TREE_CODE (type) != BOOLEAN_TYPE
3751 : 337125 : && compare_tree_int (w, TYPE_PRECISION (type)) > 0)
3752 : 367772 : || ((TREE_CODE (type) == ENUMERAL_TYPE
3753 : 367330 : || TREE_CODE (type) == BOOLEAN_TYPE)
3754 : 30647 : && tree_int_cst_lt (TYPE_SIZE (type), w)))
3755 : 784 : warning_at (DECL_SOURCE_LOCATION (field), 0,
3756 : : "width of %qD exceeds its type", field);
3757 : 366988 : else if (TREE_CODE (type) == ENUMERAL_TYPE)
3758 : : {
3759 : 29134 : int prec = enum_min_precision (type);
3760 : 29134 : if (compare_tree_int (w, prec) < 0)
3761 : 54 : warning_at (DECL_SOURCE_LOCATION (field), 0,
3762 : : "%qD is too small to hold all values of %q#T",
3763 : : field, type);
3764 : : }
3765 : : }
3766 : :
3767 : 367813 : if (w != error_mark_node)
3768 : : {
3769 : 367772 : DECL_SIZE (field) = fold_convert (bitsizetype, w);
3770 : 367772 : DECL_BIT_FIELD (field) = 1;
3771 : 367772 : return true;
3772 : : }
3773 : : else
3774 : : {
3775 : : /* Non-bit-fields are aligned for their type. */
3776 : 41 : DECL_BIT_FIELD (field) = 0;
3777 : 41 : CLEAR_DECL_C_BIT_FIELD (field);
3778 : 41 : return false;
3779 : : }
3780 : : }
3781 : :
3782 : : /* FIELD is a non bit-field. We are finishing the processing for its
3783 : : enclosing type T. Issue any appropriate messages and set appropriate
3784 : : flags. */
3785 : :
3786 : : static bool
3787 : 14709625 : check_field_decl (tree field,
3788 : : tree t,
3789 : : int* cant_have_const_ctor,
3790 : : int* no_const_asn_ref)
3791 : : {
3792 : 14709625 : tree type = strip_array_types (TREE_TYPE (field));
3793 : 14709625 : bool any_default_members = false;
3794 : :
3795 : : /* In C++98 an anonymous union cannot contain any fields which would change
3796 : : the settings of CANT_HAVE_CONST_CTOR and friends. */
3797 : 14709625 : if (ANON_UNION_TYPE_P (type) && cxx_dialect < cxx11)
3798 : : ;
3799 : : /* And, we don't set TYPE_HAS_CONST_COPY_CTOR, etc., for anonymous
3800 : : structs. So, we recurse through their fields here. */
3801 : 14708625 : else if (ANON_AGGR_TYPE_P (type))
3802 : : {
3803 : 671953 : for (tree fields = TYPE_FIELDS (type); fields;
3804 : 533145 : fields = DECL_CHAIN (fields))
3805 : 533145 : if (TREE_CODE (fields) == FIELD_DECL)
3806 : 375031 : any_default_members |= check_field_decl (fields, t,
3807 : : cant_have_const_ctor,
3808 : : no_const_asn_ref);
3809 : : }
3810 : : /* Check members with class type for constructors, destructors,
3811 : : etc. */
3812 : 14569817 : else if (CLASS_TYPE_P (type))
3813 : : {
3814 : : /* Never let anything with uninheritable virtuals
3815 : : make it through without complaint. */
3816 : 2516338 : abstract_virtuals_error (field, type);
3817 : :
3818 : 2516338 : if (TREE_CODE (t) == UNION_TYPE && cxx_dialect < cxx11)
3819 : : {
3820 : 3034 : static bool warned;
3821 : 3034 : int oldcount = errorcount;
3822 : 3034 : if (TYPE_NEEDS_CONSTRUCTING (type))
3823 : 2 : error ("member %q+#D with constructor not allowed in union",
3824 : : field);
3825 : 3034 : if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
3826 : 0 : error ("member %q+#D with destructor not allowed in union", field);
3827 : 3034 : if (TYPE_HAS_COMPLEX_COPY_ASSIGN (type))
3828 : 3 : error ("member %q+#D with copy assignment operator not allowed in union",
3829 : : field);
3830 : 3034 : if (!warned && errorcount > oldcount)
3831 : : {
3832 : 4 : inform (DECL_SOURCE_LOCATION (field), "unrestricted unions "
3833 : : "only available with %<-std=c++11%> or %<-std=gnu++11%>");
3834 : 4 : warned = true;
3835 : : }
3836 : : }
3837 : : else
3838 : : {
3839 : 2513304 : TYPE_NEEDS_CONSTRUCTING (t) |= TYPE_NEEDS_CONSTRUCTING (type);
3840 : 7539912 : TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t)
3841 : 2513304 : |= TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type);
3842 : 2513304 : TYPE_HAS_COMPLEX_COPY_ASSIGN (t)
3843 : 2513304 : |= (TYPE_HAS_COMPLEX_COPY_ASSIGN (type)
3844 : 2513304 : || !TYPE_HAS_COPY_ASSIGN (type));
3845 : 5026608 : TYPE_HAS_COMPLEX_COPY_CTOR (t) |= (TYPE_HAS_COMPLEX_COPY_CTOR (type)
3846 : 2513304 : || !TYPE_HAS_COPY_CTOR (type));
3847 : 2513304 : TYPE_HAS_COMPLEX_MOVE_ASSIGN (t) |= TYPE_HAS_COMPLEX_MOVE_ASSIGN (type);
3848 : 2513304 : TYPE_HAS_COMPLEX_MOVE_CTOR (t) |= TYPE_HAS_COMPLEX_MOVE_CTOR (type);
3849 : 5026608 : TYPE_HAS_COMPLEX_DFLT (t) |= (!TYPE_HAS_DEFAULT_CONSTRUCTOR (type)
3850 : 2513304 : || TYPE_HAS_COMPLEX_DFLT (type));
3851 : : }
3852 : :
3853 : 2516338 : if (TYPE_HAS_COPY_CTOR (type)
3854 : 2516338 : && !TYPE_HAS_CONST_COPY_CTOR (type))
3855 : 127 : *cant_have_const_ctor = 1;
3856 : :
3857 : 2516338 : if (TYPE_HAS_COPY_ASSIGN (type)
3858 : 2516338 : && !TYPE_HAS_CONST_COPY_ASSIGN (type))
3859 : 47 : *no_const_asn_ref = 1;
3860 : : }
3861 : :
3862 : 14709625 : check_abi_tags (t, field);
3863 : :
3864 : 14709625 : if (DECL_INITIAL (field) != NULL_TREE)
3865 : : /* `build_class_init_list' does not recognize
3866 : : non-FIELD_DECLs. */
3867 : 670262 : any_default_members = true;
3868 : :
3869 : 14709625 : return any_default_members;
3870 : : }
3871 : :
3872 : : /* Check the data members (both static and non-static), class-scoped
3873 : : typedefs, etc., appearing in the declaration of T. Issue
3874 : : appropriate diagnostics. Sets ACCESS_DECLS to a list (in
3875 : : declaration order) of access declarations; each TREE_VALUE in this
3876 : : list is a USING_DECL.
3877 : :
3878 : : In addition, set the following flags:
3879 : :
3880 : : EMPTY_P
3881 : : The class is empty, i.e., contains no non-static data members.
3882 : :
3883 : : CANT_HAVE_CONST_CTOR_P
3884 : : This class cannot have an implicitly generated copy constructor
3885 : : taking a const reference.
3886 : :
3887 : : CANT_HAVE_CONST_ASN_REF
3888 : : This class cannot have an implicitly generated assignment
3889 : : operator taking a const reference.
3890 : :
3891 : : All of these flags should be initialized before calling this
3892 : : function. */
3893 : :
3894 : : static void
3895 : 36143417 : check_field_decls (tree t, tree *access_decls,
3896 : : int *cant_have_const_ctor_p,
3897 : : int *no_const_asn_ref_p)
3898 : : {
3899 : 36143417 : int cant_pack = 0;
3900 : :
3901 : : /* Assume there are no access declarations. */
3902 : 36143417 : *access_decls = NULL_TREE;
3903 : : /* Effective C has things to say about classes with pointer members. */
3904 : 36143417 : tree pointer_member = NULL_TREE;
3905 : : /* Default initialized members affect the whole class. */
3906 : 36143417 : tree default_init_member = NULL_TREE;
3907 : : /* Lack of any non-static data member of non-volatile literal
3908 : : type affects a union. */
3909 : 36143417 : bool found_nv_literal_p = false;
3910 : : /* Standard layout requires all FIELDS have same access. */
3911 : 36143417 : int field_access = -1;
3912 : :
3913 : 250141662 : for (tree field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
3914 : : {
3915 : 213998245 : tree type = TREE_TYPE (field);
3916 : :
3917 : 213998245 : switch (TREE_CODE (field))
3918 : : {
3919 : 0 : default:
3920 : 0 : gcc_unreachable ();
3921 : :
3922 : 1821905 : case USING_DECL:
3923 : : /* Save the access declarations for our caller. */
3924 : 1821905 : *access_decls = tree_cons (NULL_TREE, field, *access_decls);
3925 : 1821905 : break;
3926 : :
3927 : : case TYPE_DECL:
3928 : : case TEMPLATE_DECL:
3929 : : break;
3930 : :
3931 : : case FUNCTION_DECL:
3932 : : /* FIXME: We should fold in the checking from check_methods. */
3933 : : break;
3934 : :
3935 : 1541839 : case CONST_DECL:
3936 : 1541839 : DECL_NONLOCAL (field) = 1;
3937 : 1541839 : break;
3938 : :
3939 : 9483582 : case VAR_DECL:
3940 : 9483582 : if (TREE_CODE (t) == UNION_TYPE
3941 : 35 : && cxx_dialect < cxx11)
3942 : : {
3943 : : /* [class.union]
3944 : :
3945 : : (C++98) If a union contains a static data member,
3946 : : ... the program is ill-formed. */
3947 : 1 : if (cxx_dialect < cxx11)
3948 : 1 : error ("in C++98 %q+D may not be static because it is "
3949 : : "a member of a union", field);
3950 : : }
3951 : 9483582 : goto data_member;
3952 : :
3953 : 14334815 : case FIELD_DECL:
3954 : 14334815 : if (TREE_CODE (t) == UNION_TYPE)
3955 : : {
3956 : : /* [class.union]
3957 : :
3958 : : If a union contains ... or a [non-static data] member
3959 : : of reference type, the program is ill-formed. */
3960 : 1023641 : if (TYPE_REF_P (type))
3961 : 35 : error ("non-static data member %q+D in a union may not "
3962 : : "have reference type %qT", field, type);
3963 : : }
3964 : :
3965 : 13311174 : data_member:
3966 : : /* Common VAR_DECL & FIELD_DECL processing. */
3967 : 23818397 : DECL_CONTEXT (field) = t;
3968 : 23818397 : DECL_NONLOCAL (field) = 1;
3969 : :
3970 : : /* Template instantiation can cause this. Perhaps this
3971 : : should be a specific instantiation check? */
3972 : 23818397 : if (TREE_CODE (type) == FUNCTION_TYPE)
3973 : : {
3974 : 11 : error ("data member %q+D invalidly declared function type", field);
3975 : 11 : type = build_pointer_type (type);
3976 : 11 : TREE_TYPE (field) = type;
3977 : : }
3978 : 23818386 : else if (TREE_CODE (type) == METHOD_TYPE)
3979 : : {
3980 : 0 : error ("data member %q+D invalidly declared method type", field);
3981 : 0 : type = build_pointer_type (type);
3982 : 0 : TREE_TYPE (field) = type;
3983 : : }
3984 : :
3985 : : break;
3986 : : }
3987 : :
3988 : 213998245 : if (TREE_CODE (field) != FIELD_DECL)
3989 : 199663430 : continue;
3990 : :
3991 : 14334815 : if (type == error_mark_node)
3992 : 221 : continue;
3993 : :
3994 : : /* If it is not a union and at least one non-static data member is
3995 : : non-literal, the whole class becomes non-literal. Per Core/1453,
3996 : : volatile non-static data members and base classes are also not allowed.
3997 : : If it is a union, we might set CLASSTYPE_LITERAL_P after we've seen all
3998 : : members.
3999 : : Note: if the type is incomplete we will complain later on. */
4000 : 14334594 : if (COMPLETE_TYPE_P (type))
4001 : : {
4002 : 14333111 : if (!literal_type_p (type) || CP_TYPE_VOLATILE_P (type))
4003 : 1015322 : CLASSTYPE_LITERAL_P (t) = false;
4004 : : else
4005 : : found_nv_literal_p = true;
4006 : : }
4007 : :
4008 : 14334594 : int this_field_access = (TREE_PROTECTED (field) ? 1
4009 : 12773558 : : TREE_PRIVATE (field) ? 2 : 0);
4010 : 14334594 : if (field_access != this_field_access)
4011 : : {
4012 : : /* A standard-layout class is a class that:
4013 : :
4014 : : ... has the same access control (Clause 11) for all
4015 : : non-static data members, */
4016 : 5475143 : if (field_access < 0)
4017 : : field_access = this_field_access;
4018 : : else
4019 : 28724 : CLASSTYPE_NON_STD_LAYOUT (t) = 1;
4020 : :
4021 : : /* Aggregates must be public. */
4022 : 5475143 : if (this_field_access)
4023 : 2569690 : CLASSTYPE_NON_AGGREGATE (t) = 1;
4024 : : }
4025 : :
4026 : : /* If this is of reference type, check if it needs an init. */
4027 : 14334594 : if (TYPE_REF_P (type))
4028 : : {
4029 : 346671 : CLASSTYPE_NON_LAYOUT_POD_P (t) = 1;
4030 : 346671 : CLASSTYPE_NON_STD_LAYOUT (t) = 1;
4031 : 346671 : if (DECL_INITIAL (field) == NULL_TREE)
4032 : 346611 : SET_CLASSTYPE_REF_FIELDS_NEED_INIT (t, 1);
4033 : 346671 : if (cxx_dialect < cxx11)
4034 : : {
4035 : : /* ARM $12.6.2: [A member initializer list] (or, for an
4036 : : aggregate, initialization by a brace-enclosed list) is the
4037 : : only way to initialize non-static const and reference
4038 : : members. */
4039 : 660 : TYPE_HAS_COMPLEX_COPY_ASSIGN (t) = 1;
4040 : 660 : TYPE_HAS_COMPLEX_MOVE_ASSIGN (t) = 1;
4041 : : }
4042 : : }
4043 : :
4044 : 14334594 : type = strip_array_types (type);
4045 : :
4046 : 14334594 : if (TYPE_PACKED (t))
4047 : : {
4048 : 1234 : if (!layout_pod_type_p (type) && !TYPE_PACKED (type))
4049 : : {
4050 : 24 : warning_at (DECL_SOURCE_LOCATION (field), 0,
4051 : : "ignoring packed attribute because of"
4052 : : " unpacked non-POD field %q#D", field);
4053 : 24 : cant_pack = 1;
4054 : : }
4055 : 1210 : else if (DECL_C_BIT_FIELD (field)
4056 : 1210 : || TYPE_ALIGN (TREE_TYPE (field)) > BITS_PER_UNIT)
4057 : 932 : DECL_PACKED (field) = 1;
4058 : : }
4059 : :
4060 : 14334594 : if (DECL_C_BIT_FIELD (field)
4061 : 14334594 : && integer_zerop (DECL_BIT_FIELD_REPRESENTATIVE (field)))
4062 : : /* We don't treat zero-width bitfields as making a class
4063 : : non-empty. */
4064 : : ;
4065 : 14333507 : else if (field_poverlapping_p (field)
4066 : 14333507 : && is_empty_class (TREE_TYPE (field)))
4067 : : /* Empty data members also don't make a class non-empty. */
4068 : 39060 : CLASSTYPE_CONTAINS_EMPTY_CLASS_P (t) = 1;
4069 : : else
4070 : : {
4071 : : /* The class is non-empty. */
4072 : 14294447 : CLASSTYPE_EMPTY_P (t) = 0;
4073 : : /* The class is not even nearly empty. */
4074 : 14294447 : CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
4075 : : /* If one of the data members contains an empty class, so
4076 : : does T. */
4077 : 2574948 : if (CLASS_TYPE_P (type)
4078 : 16858254 : && CLASSTYPE_CONTAINS_EMPTY_CLASS_P (type))
4079 : 755529 : CLASSTYPE_CONTAINS_EMPTY_CLASS_P (t) = 1;
4080 : : }
4081 : :
4082 : : /* This is used by -Weffc++ (see below). Warn only for pointers
4083 : : to members which might hold dynamic memory. So do not warn
4084 : : for pointers to functions or pointers to members. */
4085 : 14334594 : if (TYPE_PTR_P (type)
4086 : 14334594 : && !TYPE_PTRFN_P (type))
4087 : : pointer_member = field;
4088 : :
4089 : 14334594 : if (CLASS_TYPE_P (type))
4090 : : {
4091 : 2602867 : if (CLASSTYPE_REF_FIELDS_NEED_INIT (type))
4092 : 3098 : SET_CLASSTYPE_REF_FIELDS_NEED_INIT (t, 1);
4093 : 2602867 : if (CLASSTYPE_READONLY_FIELDS_NEED_INIT (type))
4094 : 29013 : SET_CLASSTYPE_READONLY_FIELDS_NEED_INIT (t, 1);
4095 : : }
4096 : :
4097 : 14334594 : if (DECL_MUTABLE_P (field) || TYPE_HAS_MUTABLE_P (type))
4098 : 206466 : CLASSTYPE_HAS_MUTABLE (t) = 1;
4099 : :
4100 : 14334594 : if (DECL_MUTABLE_P (field))
4101 : : {
4102 : 128171 : if (TYPE_REF_P (type))
4103 : 4 : error ("member %q+D cannot be declared as a %<mutable%> "
4104 : : "reference", field);
4105 : 128167 : else if (CP_TYPE_CONST_P (type))
4106 : 4 : error ("member %q+D cannot be declared both %<const%> "
4107 : : "and %<mutable%>", field);
4108 : : }
4109 : :
4110 : 14334594 : if (! layout_pod_type_p (type))
4111 : : /* DR 148 now allows pointers to members (which are POD themselves),
4112 : : to be allowed in POD structs. */
4113 : 2003309 : CLASSTYPE_NON_LAYOUT_POD_P (t) = 1;
4114 : :
4115 : 14334594 : if (field_poverlapping_p (field))
4116 : : /* A potentially-overlapping non-static data member makes the class
4117 : : non-layout-POD. */
4118 : 67733 : CLASSTYPE_NON_LAYOUT_POD_P (t) = 1;
4119 : :
4120 : 14334594 : if (!std_layout_type_p (type))
4121 : 538967 : CLASSTYPE_NON_STD_LAYOUT (t) = 1;
4122 : :
4123 : 14334594 : if (! zero_init_p (type))
4124 : 161 : CLASSTYPE_NON_ZERO_INIT_P (t) = 1;
4125 : :
4126 : : /* We set DECL_C_BIT_FIELD in grokbitfield.
4127 : : If the type and width are valid, we'll also set DECL_BIT_FIELD. */
4128 : 14334594 : if (DECL_C_BIT_FIELD (field))
4129 : 367813 : check_bitfield_decl (field);
4130 : :
4131 : 14334594 : if (check_field_decl (field, t,
4132 : : cant_have_const_ctor_p, no_const_asn_ref_p))
4133 : : {
4134 : 670241 : if (default_init_member
4135 : 300159 : && TREE_CODE (t) == UNION_TYPE)
4136 : : {
4137 : 12 : error ("multiple fields in union %qT initialized", t);
4138 : 12 : inform (DECL_SOURCE_LOCATION (default_init_member),
4139 : : "initialized member %q+D declared here",
4140 : : default_init_member);
4141 : : }
4142 : : default_init_member = field;
4143 : : }
4144 : :
4145 : : /* Now that we've removed bit-field widths from DECL_INITIAL,
4146 : : anything left in DECL_INITIAL is an NSDMI that makes the class
4147 : : non-aggregate in C++11, and non-layout-POD always. */
4148 : 14334594 : if (DECL_INITIAL (field))
4149 : : {
4150 : 668188 : if (cxx_dialect < cxx14)
4151 : 623 : CLASSTYPE_NON_AGGREGATE (t) = true;
4152 : : else
4153 : 667565 : CLASSTYPE_NON_POD_AGGREGATE (t) = true;
4154 : : }
4155 : :
4156 : 14334594 : if (CP_TYPE_CONST_P (type))
4157 : : {
4158 : : /* If any field is const, the structure type is pseudo-const. */
4159 : 245337 : C_TYPE_FIELDS_READONLY (t) = 1;
4160 : 245337 : if (DECL_INITIAL (field) == NULL_TREE)
4161 : 199315 : SET_CLASSTYPE_READONLY_FIELDS_NEED_INIT (t, 1);
4162 : 245337 : if (cxx_dialect < cxx11)
4163 : : {
4164 : : /* ARM $12.6.2: [A member initializer list] (or, for an
4165 : : aggregate, initialization by a brace-enclosed list) is the
4166 : : only way to initialize non-static const and reference
4167 : : members. */
4168 : 269 : TYPE_HAS_COMPLEX_COPY_ASSIGN (t) = 1;
4169 : 269 : TYPE_HAS_COMPLEX_MOVE_ASSIGN (t) = 1;
4170 : : }
4171 : : }
4172 : : /* A field that is pseudo-const makes the structure likewise. */
4173 : 14089257 : else if (CLASS_TYPE_P (type))
4174 : : {
4175 : 2556115 : C_TYPE_FIELDS_READONLY (t) |= C_TYPE_FIELDS_READONLY (type);
4176 : 2556115 : SET_CLASSTYPE_READONLY_FIELDS_NEED_INIT (t,
4177 : : CLASSTYPE_READONLY_FIELDS_NEED_INIT (t)
4178 : : | CLASSTYPE_READONLY_FIELDS_NEED_INIT (type));
4179 : : }
4180 : :
4181 : : /* Core issue 80: A non-static data member is required to have a
4182 : : different name from the class iff the class has a
4183 : : user-declared constructor. */
4184 : 14334594 : if (constructor_name_p (DECL_NAME (field), t)
4185 : 14334594 : && TYPE_HAS_USER_CONSTRUCTOR (t))
4186 : 16 : permerror (DECL_SOURCE_LOCATION (field),
4187 : : "field %q#D with same name as class", field);
4188 : : }
4189 : :
4190 : : /* Per CWG 2096, a type is a literal type if it is a union, and at least
4191 : : one of its non-static data members is of non-volatile literal type. */
4192 : 36143417 : if (TREE_CODE (t) == UNION_TYPE && found_nv_literal_p)
4193 : 399797 : CLASSTYPE_LITERAL_P (t) = true;
4194 : :
4195 : : /* Effective C++ rule 11: if a class has dynamic memory held by pointers,
4196 : : it should also define a copy constructor and an assignment operator to
4197 : : implement the correct copy semantic (deep vs shallow, etc.). As it is
4198 : : not feasible to check whether the constructors do allocate dynamic memory
4199 : : and store it within members, we approximate the warning like this:
4200 : :
4201 : : -- Warn only if there are members which are pointers
4202 : : -- Warn only if there is a non-trivial constructor (otherwise,
4203 : : there cannot be memory allocated).
4204 : : -- Warn only if there is a non-trivial destructor. We assume that the
4205 : : user at least implemented the cleanup correctly, and a destructor
4206 : : is needed to free dynamic memory.
4207 : :
4208 : : This seems enough for practical purposes. */
4209 : 36143417 : if (warn_ecpp
4210 : 184 : && pointer_member
4211 : 20 : && TYPE_HAS_USER_CONSTRUCTOR (t)
4212 : 20 : && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t)
4213 : 36143433 : && !(TYPE_HAS_COPY_CTOR (t) && TYPE_HAS_COPY_ASSIGN (t)))
4214 : : {
4215 : 12 : if (warning (OPT_Weffc__, "%q#T has pointer data members", t))
4216 : : {
4217 : 12 : if (! TYPE_HAS_COPY_CTOR (t))
4218 : : {
4219 : 8 : warning (OPT_Weffc__,
4220 : : " but does not declare %<%T(const %T&)%>", t, t);
4221 : 8 : if (!TYPE_HAS_COPY_ASSIGN (t))
4222 : 4 : warning (OPT_Weffc__, " or %<operator=(const %T&)%>", t);
4223 : : }
4224 : 4 : else if (! TYPE_HAS_COPY_ASSIGN (t))
4225 : 4 : warning (OPT_Weffc__,
4226 : : " but does not declare %<operator=(const %T&)%>", t);
4227 : 12 : inform (DECL_SOURCE_LOCATION (pointer_member),
4228 : : "pointer member %q+D declared here", pointer_member);
4229 : : }
4230 : : }
4231 : :
4232 : : /* Non-static data member initializers make the default constructor
4233 : : non-trivial. */
4234 : 36143417 : if (default_init_member)
4235 : : {
4236 : 370082 : TYPE_NEEDS_CONSTRUCTING (t) = true;
4237 : 370082 : TYPE_HAS_COMPLEX_DFLT (t) = true;
4238 : : }
4239 : :
4240 : : /* If any of the fields couldn't be packed, unset TYPE_PACKED. */
4241 : 36143417 : if (cant_pack)
4242 : 16 : TYPE_PACKED (t) = 0;
4243 : :
4244 : : /* Check anonymous struct/anonymous union fields. */
4245 : 36143417 : finish_struct_anon (t);
4246 : :
4247 : : /* We've built up the list of access declarations in reverse order.
4248 : : Fix that now. */
4249 : 36143417 : *access_decls = nreverse (*access_decls);
4250 : 36143417 : }
4251 : :
4252 : : /* If TYPE is an empty class type, records its OFFSET in the table of
4253 : : OFFSETS. */
4254 : :
4255 : : static int
4256 : 23705453 : record_subobject_offset (tree type, tree offset, splay_tree offsets)
4257 : : {
4258 : 23705453 : splay_tree_node n;
4259 : :
4260 : 23705453 : if (!is_empty_class (type))
4261 : : return 0;
4262 : :
4263 : : /* Record the location of this empty object in OFFSETS. */
4264 : 21470801 : n = splay_tree_lookup (offsets, (splay_tree_key) offset);
4265 : 21470801 : if (!n)
4266 : 17834371 : n = splay_tree_insert (offsets,
4267 : : (splay_tree_key) offset,
4268 : : (splay_tree_value) NULL_TREE);
4269 : 42941602 : n->value = ((splay_tree_value)
4270 : 21470801 : tree_cons (NULL_TREE,
4271 : : type,
4272 : 21470801 : (tree) n->value));
4273 : :
4274 : 21470801 : return 0;
4275 : : }
4276 : :
4277 : : /* Returns nonzero if TYPE is an empty class type and there is
4278 : : already an entry in OFFSETS for the same TYPE as the same OFFSET. */
4279 : :
4280 : : static int
4281 : 777260 : check_subobject_offset (tree type, tree offset, splay_tree offsets)
4282 : : {
4283 : 777260 : splay_tree_node n;
4284 : 777260 : tree t;
4285 : :
4286 : 777260 : if (!is_empty_class (type))
4287 : : return 0;
4288 : :
4289 : : /* Record the location of this empty object in OFFSETS. */
4290 : 642309 : n = splay_tree_lookup (offsets, (splay_tree_key) offset);
4291 : 642309 : if (!n)
4292 : : return 0;
4293 : :
4294 : 642217 : enum { ignore, fast, slow, warn }
4295 : 642217 : cv_check = (abi_version_crosses (19) ? slow
4296 : 641641 : : abi_version_at_least (19) ? fast
4297 : 642217 : : ignore);
4298 : 5819696 : for (t = (tree) n->value; t; t = TREE_CHAIN (t))
4299 : : {
4300 : 5179239 : tree elt = TREE_VALUE (t);
4301 : :
4302 : 5179239 : if (same_type_p (elt, type))
4303 : : return 1;
4304 : :
4305 : 5177479 : if (cv_check != ignore
4306 : 5177479 : && similar_type_p (elt, type))
4307 : : {
4308 : 12 : if (cv_check == fast)
4309 : : return 1;
4310 : : cv_check = warn;
4311 : : }
4312 : : }
4313 : :
4314 : 640457 : if (cv_check == warn)
4315 : : {
4316 : 12 : warning (OPT_Wabi, "layout of %qs member of type %qT changes in %qs",
4317 : : "[[no_unique_address]]", type, "-fabi-version=19");
4318 : 12 : if (abi_version_at_least (19))
4319 : : return 1;
4320 : : }
4321 : :
4322 : : return 0;
4323 : : }
4324 : :
4325 : : /* Walk through all the subobjects of TYPE (located at OFFSET). Call
4326 : : F for every subobject, passing it the type, offset, and table of
4327 : : OFFSETS. If VBASES_P is one, then virtual non-primary bases should
4328 : : be traversed.
4329 : :
4330 : : If MAX_OFFSET is non-NULL, then subobjects with an offset greater
4331 : : than MAX_OFFSET will not be walked.
4332 : :
4333 : : If F returns a nonzero value, the traversal ceases, and that value
4334 : : is returned. Otherwise, returns zero. */
4335 : :
4336 : : static int
4337 : 43760143 : walk_subobject_offsets (tree type,
4338 : : subobject_offset_fn f,
4339 : : tree offset,
4340 : : splay_tree offsets,
4341 : : tree max_offset,
4342 : : int vbases_p)
4343 : : {
4344 : 43760143 : int r = 0;
4345 : 43760143 : tree type_binfo = NULL_TREE;
4346 : :
4347 : : /* If this OFFSET is bigger than the MAX_OFFSET, then we should
4348 : : stop. */
4349 : 43760143 : if (max_offset && tree_int_cst_lt (max_offset, offset))
4350 : : return 0;
4351 : :
4352 : 33870353 : if (type == error_mark_node)
4353 : : return 0;
4354 : :
4355 : 33870353 : if (!TYPE_P (type))
4356 : : {
4357 : 5547872 : type_binfo = type;
4358 : 5547872 : type = BINFO_TYPE (type);
4359 : : }
4360 : :
4361 : 33870353 : if (CLASS_TYPE_P (type))
4362 : : {
4363 : 27768943 : tree field;
4364 : 27768943 : tree binfo;
4365 : 27768943 : int i;
4366 : :
4367 : : /* Avoid recursing into objects that are not interesting. */
4368 : 27768943 : if (!CLASSTYPE_CONTAINS_EMPTY_CLASS_P (type))
4369 : : return 0;
4370 : :
4371 : : /* Record the location of TYPE. */
4372 : 24483232 : r = (*f) (type, offset, offsets);
4373 : 24483232 : if (r)
4374 : : return r;
4375 : :
4376 : : /* Iterate through the direct base classes of TYPE. */
4377 : 24481458 : if (!type_binfo)
4378 : 19739189 : type_binfo = TYPE_BINFO (type);
4379 : 29909486 : for (i = 0; BINFO_BASE_ITERATE (type_binfo, i, binfo); i++)
4380 : : {
4381 : 5429102 : tree binfo_offset;
4382 : :
4383 : 5429102 : if (BINFO_VIRTUAL_P (binfo))
4384 : 1947 : continue;
4385 : :
4386 : 5427155 : tree orig_binfo;
4387 : : /* We cannot rely on BINFO_OFFSET being set for the base
4388 : : class yet, but the offsets for direct non-virtual
4389 : : bases can be calculated by going back to the TYPE. */
4390 : 5427155 : orig_binfo = BINFO_BASE_BINFO (TYPE_BINFO (type), i);
4391 : 5427155 : binfo_offset = size_binop (PLUS_EXPR,
4392 : : offset,
4393 : : BINFO_OFFSET (orig_binfo));
4394 : :
4395 : 5427155 : r = walk_subobject_offsets (binfo,
4396 : : f,
4397 : : binfo_offset,
4398 : : offsets,
4399 : : max_offset,
4400 : : /*vbases_p=*/0);
4401 : 5427155 : if (r)
4402 : 1074 : return r;
4403 : : }
4404 : :
4405 : 24480384 : if (CLASSTYPE_VBASECLASSES (type))
4406 : : {
4407 : 1561 : unsigned ix;
4408 : 1561 : vec<tree, va_gc> *vbases;
4409 : :
4410 : : /* Iterate through the virtual base classes of TYPE. In G++
4411 : : 3.2, we included virtual bases in the direct base class
4412 : : loop above, which results in incorrect results; the
4413 : : correct offsets for virtual bases are only known when
4414 : : working with the most derived type. */
4415 : 1561 : if (vbases_p)
4416 : 32 : for (vbases = CLASSTYPE_VBASECLASSES (type), ix = 0;
4417 : 32 : vec_safe_iterate (vbases, ix, &binfo); ix++)
4418 : : {
4419 : 16 : r = walk_subobject_offsets (binfo,
4420 : : f,
4421 : 16 : size_binop (PLUS_EXPR,
4422 : : offset,
4423 : : BINFO_OFFSET (binfo)),
4424 : : offsets,
4425 : : max_offset,
4426 : : /*vbases_p=*/0);
4427 : 16 : if (r)
4428 : 0 : return r;
4429 : : }
4430 : : else
4431 : : {
4432 : : /* We still have to walk the primary base, if it is
4433 : : virtual. (If it is non-virtual, then it was walked
4434 : : above.) */
4435 : 1545 : tree vbase = get_primary_binfo (type_binfo);
4436 : :
4437 : 512 : if (vbase && BINFO_VIRTUAL_P (vbase)
4438 : 303 : && BINFO_PRIMARY_P (vbase)
4439 : 1848 : && BINFO_INHERITANCE_CHAIN (vbase) == type_binfo)
4440 : : {
4441 : 260 : r = (walk_subobject_offsets
4442 : 260 : (vbase, f, offset,
4443 : : offsets, max_offset, /*vbases_p=*/0));
4444 : 260 : if (r)
4445 : : return r;
4446 : : }
4447 : : }
4448 : : }
4449 : :
4450 : : /* Iterate through the fields of TYPE. */
4451 : 462153739 : for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
4452 : 437673552 : if (TREE_CODE (field) == FIELD_DECL
4453 : 8177420 : && TREE_TYPE (field) != error_mark_node
4454 : 445850934 : && !DECL_ARTIFICIAL (field))
4455 : : {
4456 : 2730669 : tree field_offset;
4457 : :
4458 : 2730669 : field_offset = byte_position (field);
4459 : :
4460 : 2730669 : r = walk_subobject_offsets (TREE_TYPE (field),
4461 : : f,
4462 : : size_binop (PLUS_EXPR,
4463 : : offset,
4464 : : field_offset),
4465 : : offsets,
4466 : : max_offset,
4467 : : /*vbases_p=*/1);
4468 : 2730669 : if (r)
4469 : 197 : return r;
4470 : : }
4471 : : }
4472 : 6101410 : else if (TREE_CODE (type) == ARRAY_TYPE)
4473 : : {
4474 : 677446 : tree element_type = strip_array_types (type);
4475 : 677446 : tree domain = TYPE_DOMAIN (type);
4476 : 677446 : tree index;
4477 : :
4478 : : /* Avoid recursing into objects that are not interesting. */
4479 : 25578 : if (!CLASS_TYPE_P (element_type)
4480 : 25566 : || !CLASSTYPE_CONTAINS_EMPTY_CLASS_P (element_type)
4481 : 11798 : || !domain
4482 : 689221 : || integer_minus_onep (TYPE_MAX_VALUE (domain)))
4483 : 665897 : return 0;
4484 : :
4485 : : /* Step through each of the elements in the array. */
4486 : 11549 : for (index = size_zero_node;
4487 : 23748 : !tree_int_cst_lt (TYPE_MAX_VALUE (domain), index);
4488 : 12199 : index = size_binop (PLUS_EXPR, index, size_one_node))
4489 : : {
4490 : 23234 : r = walk_subobject_offsets (TREE_TYPE (type),
4491 : : f,
4492 : : offset,
4493 : : offsets,
4494 : : max_offset,
4495 : : /*vbases_p=*/1);
4496 : 23234 : if (r)
4497 : 0 : return r;
4498 : 23234 : offset = size_binop (PLUS_EXPR, offset,
4499 : : TYPE_SIZE_UNIT (TREE_TYPE (type)));
4500 : : /* If this new OFFSET is bigger than the MAX_OFFSET, then
4501 : : there's no point in iterating through the remaining
4502 : : elements of the array. */
4503 : 23234 : if (max_offset && tree_int_cst_lt (max_offset, offset))
4504 : : break;
4505 : : }
4506 : : }
4507 : :
4508 : : return 0;
4509 : : }
4510 : :
4511 : : /* Return true iff FIELD_DECL DECL is potentially overlapping. */
4512 : :
4513 : : static bool
4514 : 57600563 : field_poverlapping_p (tree decl)
4515 : : {
4516 : : /* Base fields are actually potentially overlapping, but C++ bases go through
4517 : : a different code path based on binfos, and ObjC++ base fields are laid out
4518 : : in objc-act, so we don't want layout_class_type to mess with them. */
4519 : 57600563 : if (DECL_FIELD_IS_BASE (decl))
4520 : : {
4521 : 0 : gcc_checking_assert (c_dialect_objc ());
4522 : : return false;
4523 : : }
4524 : :
4525 : 57600563 : return lookup_attribute ("no_unique_address",
4526 : 115201126 : DECL_ATTRIBUTES (decl));
4527 : : }
4528 : :
4529 : : /* Return true iff DECL is an empty field, either for an empty base or a
4530 : : [[no_unique_address]] data member. */
4531 : :
4532 : : bool
4533 : 130807915 : is_empty_field (tree decl)
4534 : : {
4535 : 130807915 : if (!decl || TREE_CODE (decl) != FIELD_DECL)
4536 : : return false;
4537 : :
4538 : 70581916 : bool r = (is_empty_class (TREE_TYPE (decl))
4539 : 72450315 : && (DECL_FIELD_IS_BASE (decl)
4540 : 223687 : || field_poverlapping_p (decl)));
4541 : :
4542 : : /* Empty fields should have size zero. */
4543 : 1727312 : gcc_checking_assert (!r || integer_zerop (DECL_SIZE (decl)));
4544 : :
4545 : : return r;
4546 : : }
4547 : :
4548 : : /* Record all of the empty subobjects of DECL_OR_BINFO. */
4549 : :
4550 : : static void
4551 : 33591965 : record_subobject_offsets (tree decl_or_binfo,
4552 : : splay_tree offsets)
4553 : : {
4554 : 33591965 : tree type, offset;
4555 : 33591965 : bool overlapping, vbases_p;
4556 : :
4557 : 33591965 : if (DECL_P (decl_or_binfo))
4558 : : {
4559 : 14334594 : tree decl = decl_or_binfo;
4560 : 14334594 : type = TREE_TYPE (decl);
4561 : 14334594 : offset = byte_position (decl);
4562 : 14334594 : overlapping = field_poverlapping_p (decl);
4563 : 14334594 : vbases_p = true;
4564 : : }
4565 : : else
4566 : : {
4567 : 19257371 : type = BINFO_TYPE (decl_or_binfo);
4568 : 19257371 : offset = BINFO_OFFSET (decl_or_binfo);
4569 : 19257371 : overlapping = true;
4570 : 19257371 : vbases_p = false;
4571 : : }
4572 : :
4573 : 33591965 : tree max_offset;
4574 : : /* If recording subobjects for a non-static data member or a
4575 : : non-empty base class, we do not need to record offsets beyond
4576 : : the size of the biggest empty class. Additional data members
4577 : : will go at the end of the class. Additional base classes will go
4578 : : either at offset zero (if empty, in which case they cannot
4579 : : overlap with offsets past the size of the biggest empty class) or
4580 : : at the end of the class.
4581 : :
4582 : : However, if we are placing an empty base class, then we must record
4583 : : all offsets, as either the empty class is at offset zero (where
4584 : : other empty classes might later be placed) or at the end of the
4585 : : class (where other objects might then be placed, so other empty
4586 : : subobjects might later overlap). */
4587 : 33591965 : if (!overlapping
4588 : 33591965 : || !is_empty_class (type))
4589 : 16533089 : max_offset = sizeof_biggest_empty_class;
4590 : : else
4591 : : max_offset = NULL_TREE;
4592 : 33591965 : walk_subobject_offsets (type, record_subobject_offset, offset,
4593 : : offsets, max_offset, vbases_p);
4594 : 33591965 : }
4595 : :
4596 : : /* Returns nonzero if any of the empty subobjects of TYPE (located at
4597 : : OFFSET) conflict with entries in OFFSETS. If VBASES_P is nonzero,
4598 : : virtual bases of TYPE are examined. */
4599 : :
4600 : : static int
4601 : 32570844 : layout_conflict_p (tree type,
4602 : : tree offset,
4603 : : splay_tree offsets,
4604 : : int vbases_p)
4605 : : {
4606 : 32570844 : splay_tree_node max_node;
4607 : :
4608 : : /* Get the node in OFFSETS that indicates the maximum offset where
4609 : : an empty subobject is located. */
4610 : 32570844 : max_node = splay_tree_max (offsets);
4611 : : /* If there aren't any empty subobjects, then there's no point in
4612 : : performing this check. */
4613 : 32570844 : if (!max_node)
4614 : : return 0;
4615 : :
4616 : 1986455 : return walk_subobject_offsets (type, check_subobject_offset, offset,
4617 : 1986455 : offsets, (tree) (max_node->key),
4618 : 1986455 : vbases_p);
4619 : : }
4620 : :
4621 : : /* DECL is a FIELD_DECL corresponding either to a base subobject of a
4622 : : non-static data member of the type indicated by RLI. BINFO is the
4623 : : binfo corresponding to the base subobject, OFFSETS maps offsets to
4624 : : types already located at those offsets. This function determines
4625 : : the position of the DECL. */
4626 : :
4627 : : static void
4628 : 16533866 : layout_nonempty_base_or_field (record_layout_info rli,
4629 : : tree decl,
4630 : : tree binfo,
4631 : : splay_tree offsets)
4632 : : {
4633 : 16533866 : tree offset = NULL_TREE;
4634 : 16533866 : bool field_p;
4635 : 16533866 : tree type;
4636 : :
4637 : 16533866 : if (binfo)
4638 : : {
4639 : : /* For the purposes of determining layout conflicts, we want to
4640 : : use the class type of BINFO; TREE_TYPE (DECL) will be the
4641 : : CLASSTYPE_AS_BASE version, which does not contain entries for
4642 : : zero-sized bases. */
4643 : 2237555 : type = TREE_TYPE (binfo);
4644 : 2237555 : field_p = false;
4645 : : }
4646 : : else
4647 : : {
4648 : 14296311 : type = TREE_TYPE (decl);
4649 : 14296311 : field_p = true;
4650 : : }
4651 : :
4652 : : /* Try to place the field. It may take more than one try if we have
4653 : : a hard time placing the field without putting two objects of the
4654 : : same type at the same address. */
4655 : 261 : while (1)
4656 : : {
4657 : 16534127 : struct record_layout_info_s old_rli = *rli;
4658 : :
4659 : : /* Place this field. */
4660 : 16534127 : place_field (rli, decl);
4661 : 16534127 : offset = byte_position (decl);
4662 : :
4663 : : /* We have to check to see whether or not there is already
4664 : : something of the same type at the offset we're about to use.
4665 : : For example, consider:
4666 : :
4667 : : struct S {};
4668 : : struct T : public S { int i; };
4669 : : struct U : public S, public T {};
4670 : :
4671 : : Here, we put S at offset zero in U. Then, we can't put T at
4672 : : offset zero -- its S component would be at the same address
4673 : : as the S we already allocated. So, we have to skip ahead.
4674 : : Since all data members, including those whose type is an
4675 : : empty class, have nonzero size, any overlap can happen only
4676 : : with a direct or indirect base-class -- it can't happen with
4677 : : a data member. */
4678 : : /* In a union, overlap is permitted; all members are placed at
4679 : : offset zero. */
4680 : 16534127 : if (TREE_CODE (rli->t) == UNION_TYPE)
4681 : : break;
4682 : 17749031 : if (layout_conflict_p (field_p ? type : binfo, offset,
4683 : : offsets, field_p))
4684 : : {
4685 : : /* Strip off the size allocated to this field. That puts us
4686 : : at the first place we could have put the field with
4687 : : proper alignment. */
4688 : 261 : *rli = old_rli;
4689 : :
4690 : : /* Bump up by the alignment required for the type. */
4691 : 261 : rli->bitpos
4692 : 261 : = size_binop (PLUS_EXPR, rli->bitpos,
4693 : : bitsize_int (binfo
4694 : : ? CLASSTYPE_ALIGN (type)
4695 : : : TYPE_ALIGN (type)));
4696 : 261 : normalize_rli (rli);
4697 : : }
4698 : 15511140 : else if (TREE_CODE (type) == NULLPTR_TYPE
4699 : 28520 : && warn_abi && abi_version_crosses (9))
4700 : : {
4701 : : /* Before ABI v9, we were giving nullptr_t alignment of 1; if
4702 : : the offset wasn't aligned like a pointer when we started to
4703 : : layout this field, that affects its position. */
4704 : 138 : tree pos = rli_size_unit_so_far (&old_rli);
4705 : 138 : if (int_cst_value (pos) % TYPE_ALIGN_UNIT (ptr_type_node) != 0)
4706 : : {
4707 : 3 : if (abi_version_at_least (9))
4708 : 3 : warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wabi,
4709 : : "alignment of %qD increased in %<-fabi-version=9%> "
4710 : : "(GCC 5.2)", decl);
4711 : : else
4712 : 0 : warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wabi, "alignment "
4713 : : "of %qD will increase in %<-fabi-version=9%>",
4714 : : decl);
4715 : : }
4716 : : break;
4717 : : }
4718 : : else
4719 : : /* There was no conflict. We're done laying out this field. */
4720 : : break;
4721 : : }
4722 : :
4723 : : /* Now that we know where it will be placed, update its
4724 : : BINFO_OFFSET. */
4725 : 18771421 : if (binfo && CLASS_TYPE_P (BINFO_TYPE (binfo)))
4726 : : /* Indirect virtual bases may have a nonzero BINFO_OFFSET at
4727 : : this point because their BINFO_OFFSET is copied from another
4728 : : hierarchy. Therefore, we may not need to add the entire
4729 : : OFFSET. */
4730 : 2237555 : propagate_binfo_offsets (binfo,
4731 : : size_diffop_loc (input_location,
4732 : : fold_convert (ssizetype, offset),
4733 : 2237555 : fold_convert (ssizetype,
4734 : : BINFO_OFFSET (binfo))));
4735 : 16533866 : }
4736 : :
4737 : : /* Returns true if TYPE is empty and OFFSET is nonzero. */
4738 : :
4739 : : static int
4740 : 519 : empty_base_at_nonzero_offset_p (tree type,
4741 : : tree offset,
4742 : : splay_tree /*offsets*/)
4743 : : {
4744 : 519 : return is_empty_class (type) && !integer_zerop (offset);
4745 : : }
4746 : :
4747 : : /* Layout the empty base BINFO. EOC indicates the byte currently just
4748 : : past the end of the class, and should be correctly aligned for a
4749 : : class of the type indicated by BINFO; OFFSETS gives the offsets of
4750 : : the empty bases allocated so far. T is the most derived
4751 : : type. Return nonzero iff we added it at the end. */
4752 : :
4753 : : static bool
4754 : 17058876 : layout_empty_base_or_field (record_layout_info rli, tree binfo_or_decl,
4755 : : splay_tree offsets)
4756 : : {
4757 : 17058876 : tree alignment;
4758 : 17058876 : bool atend = false;
4759 : 17058876 : tree binfo = NULL_TREE;
4760 : 17058876 : tree decl = NULL_TREE;
4761 : 17058876 : tree type;
4762 : 17058876 : if (TREE_CODE (binfo_or_decl) == TREE_BINFO)
4763 : : {
4764 : 17019816 : binfo = binfo_or_decl;
4765 : 17019816 : type = BINFO_TYPE (binfo);
4766 : : }
4767 : : else
4768 : : {
4769 : 39060 : decl = binfo_or_decl;
4770 : 39060 : type = TREE_TYPE (decl);
4771 : : }
4772 : :
4773 : : /* On some platforms (ARM), even empty classes will not be
4774 : : byte-aligned. */
4775 : 17058876 : tree eoc = round_up_loc (input_location,
4776 : : rli_size_unit_so_far (rli),
4777 : 17058876 : CLASSTYPE_ALIGN_UNIT (type));
4778 : :
4779 : : /* This routine should only be used for empty classes. */
4780 : 17058876 : gcc_assert (is_empty_class (type));
4781 : :
4782 : 17058876 : if (decl && DECL_USER_ALIGN (decl))
4783 : 1 : alignment = size_int (DECL_ALIGN_UNIT (decl));
4784 : : else
4785 : 17058875 : alignment = size_int (CLASSTYPE_ALIGN_UNIT (type));
4786 : :
4787 : : /* This is an empty base class. We first try to put it at offset
4788 : : zero. */
4789 : 17058876 : tree offset = size_zero_node;
4790 : 17058876 : if (TREE_CODE (rli->t) != UNION_TYPE
4791 : 17058876 : && layout_conflict_p (type,
4792 : : offset,
4793 : : offsets,
4794 : : /*vbases_p=*/0))
4795 : : {
4796 : : /* That didn't work. Now, we move forward from the next
4797 : : available spot in the class. */
4798 : 1505 : atend = true;
4799 : : offset = eoc;
4800 : 2154 : while (1)
4801 : : {
4802 : 1505 : if (!layout_conflict_p (type,
4803 : : offset,
4804 : : offsets,
4805 : : /*vbases_p=*/0))
4806 : : /* We finally found a spot where there's no overlap. */
4807 : : break;
4808 : :
4809 : : /* There's overlap here, too. Bump along to the next spot. */
4810 : 649 : offset = size_binop (PLUS_EXPR, offset, alignment);
4811 : : }
4812 : : }
4813 : :
4814 : 17058876 : if (decl && DECL_USER_ALIGN (decl))
4815 : : {
4816 : 1 : rli->record_align = MAX (rli->record_align, DECL_ALIGN (decl));
4817 : 1 : if (warn_packed)
4818 : 0 : rli->unpacked_align = MAX (rli->unpacked_align, DECL_ALIGN (decl));
4819 : 1 : TYPE_USER_ALIGN (rli->t) = 1;
4820 : : }
4821 : 17058875 : else if (CLASSTYPE_USER_ALIGN (type))
4822 : : {
4823 : 17 : rli->record_align = MAX (rli->record_align, CLASSTYPE_ALIGN (type));
4824 : 17 : if (warn_packed)
4825 : 0 : rli->unpacked_align = MAX (rli->unpacked_align, CLASSTYPE_ALIGN (type));
4826 : 17 : TYPE_USER_ALIGN (rli->t) = 1;
4827 : : }
4828 : :
4829 : 17058876 : if (binfo)
4830 : : /* Adjust BINFO_OFFSET (binfo) to be exactly OFFSET. */
4831 : 34039632 : propagate_binfo_offsets (binfo,
4832 : 17019816 : size_diffop (offset, BINFO_OFFSET (binfo)));
4833 : : else
4834 : : {
4835 : 39060 : DECL_FIELD_OFFSET (decl) = offset;
4836 : 39060 : DECL_FIELD_BIT_OFFSET (decl) = bitsize_zero_node;
4837 : 39060 : SET_DECL_OFFSET_ALIGN (decl, BITS_PER_UNIT);
4838 : : }
4839 : :
4840 : 17058876 : return atend;
4841 : : }
4842 : :
4843 : : /* Build the FIELD_DECL for BASETYPE as a base of T, add it to the chain of
4844 : : fields at NEXT_FIELD, and return it. */
4845 : :
4846 : : static tree
4847 : 19134413 : build_base_field_1 (tree t, tree binfo, tree access, tree *&next_field)
4848 : : {
4849 : : /* Create the FIELD_DECL. */
4850 : 19134413 : tree basetype = BINFO_TYPE (binfo);
4851 : 19134413 : tree as_base = CLASSTYPE_AS_BASE (basetype);
4852 : 19134413 : gcc_assert (as_base);
4853 : 19134413 : tree decl = build_decl (input_location, FIELD_DECL, NULL_TREE, as_base);
4854 : :
4855 : 19134413 : DECL_ARTIFICIAL (decl) = 1;
4856 : 19134413 : DECL_IGNORED_P (decl) = 1;
4857 : 19134413 : DECL_FIELD_CONTEXT (decl) = t;
4858 : 19134413 : if (is_empty_class (basetype))
4859 : : /* CLASSTYPE_SIZE is one byte, but the field needs to have size zero. */
4860 : 16896858 : DECL_SIZE (decl) = DECL_SIZE_UNIT (decl) = size_zero_node;
4861 : : else
4862 : : {
4863 : 2237555 : DECL_SIZE (decl) = CLASSTYPE_SIZE (basetype);
4864 : 2237555 : DECL_SIZE_UNIT (decl) = CLASSTYPE_SIZE_UNIT (basetype);
4865 : : }
4866 : 19134413 : SET_DECL_ALIGN (decl, CLASSTYPE_ALIGN (basetype));
4867 : 19134413 : DECL_USER_ALIGN (decl) = CLASSTYPE_USER_ALIGN (basetype);
4868 : 19134413 : SET_DECL_MODE (decl, TYPE_MODE (basetype));
4869 : 19134413 : DECL_FIELD_IS_BASE (decl) = 1;
4870 : :
4871 : 19134413 : if (access == access_private_node)
4872 : 740573 : TREE_PRIVATE (decl) = true;
4873 : 18393840 : else if (access == access_protected_node)
4874 : 65363 : TREE_PROTECTED (decl) = true;
4875 : :
4876 : : /* Add the new FIELD_DECL to the list of fields for T. */
4877 : 19134413 : DECL_CHAIN (decl) = *next_field;
4878 : 19134413 : *next_field = decl;
4879 : 19134413 : next_field = &DECL_CHAIN (decl);
4880 : :
4881 : 19134413 : return decl;
4882 : : }
4883 : :
4884 : : /* Layout the base given by BINFO in the class indicated by RLI.
4885 : : *BASE_ALIGN is a running maximum of the alignments of
4886 : : any base class. OFFSETS gives the location of empty base
4887 : : subobjects. T is the most derived type. Return nonzero if the new
4888 : : object cannot be nearly-empty. A new FIELD_DECL is inserted at
4889 : : *NEXT_FIELD, unless BINFO is for an empty base class.
4890 : :
4891 : : Returns the location at which the next field should be inserted. */
4892 : :
4893 : : static tree *
4894 : 19257371 : build_base_field (record_layout_info rli, tree binfo, tree access,
4895 : : splay_tree offsets, tree *next_field)
4896 : : {
4897 : 19257371 : tree t = rli->t;
4898 : 19257371 : tree basetype = BINFO_TYPE (binfo);
4899 : :
4900 : 19257371 : if (!COMPLETE_TYPE_P (basetype))
4901 : : /* This error is now reported in xref_tag, thus giving better
4902 : : location information. */
4903 : 0 : return next_field;
4904 : :
4905 : : /* Place the base class. */
4906 : 19257371 : if (!is_empty_class (basetype))
4907 : : {
4908 : 2237555 : tree decl;
4909 : :
4910 : : /* The containing class is non-empty because it has a non-empty
4911 : : base class. */
4912 : 2237555 : CLASSTYPE_EMPTY_P (t) = 0;
4913 : :
4914 : : /* Create the FIELD_DECL. */
4915 : 2237555 : decl = build_base_field_1 (t, binfo, access, next_field);
4916 : :
4917 : : /* Try to place the field. It may take more than one try if we
4918 : : have a hard time placing the field without putting two
4919 : : objects of the same type at the same address. */
4920 : 2237555 : layout_nonempty_base_or_field (rli, decl, binfo, offsets);
4921 : : }
4922 : : else
4923 : : {
4924 : 17019816 : bool atend = layout_empty_base_or_field (rli, binfo, offsets);
4925 : : /* A nearly-empty class "has no proper base class that is empty,
4926 : : not morally virtual, and at an offset other than zero." */
4927 : 17019816 : if (!BINFO_VIRTUAL_P (binfo) && CLASSTYPE_NEARLY_EMPTY_P (t))
4928 : : {
4929 : 505 : if (atend)
4930 : 116 : CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
4931 : : /* The check above (used in G++ 3.2) is insufficient because
4932 : : an empty class placed at offset zero might itself have an
4933 : : empty base at a nonzero offset. */
4934 : 389 : else if (walk_subobject_offsets (basetype,
4935 : : empty_base_at_nonzero_offset_p,
4936 : : size_zero_node,
4937 : : /*offsets=*/NULL,
4938 : : /*max_offset=*/NULL_TREE,
4939 : : /*vbases_p=*/true))
4940 : 8 : CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
4941 : : }
4942 : :
4943 : : /* We used to not create a FIELD_DECL for empty base classes because of
4944 : : back end issues with overlapping FIELD_DECLs, but that doesn't seem to
4945 : : be a problem anymore. We need them to handle initialization of C++17
4946 : : aggregate bases. */
4947 : 33918288 : if (cxx_dialect >= cxx17 && !BINFO_VIRTUAL_P (binfo))
4948 : : {
4949 : 16896858 : tree decl = build_base_field_1 (t, binfo, access, next_field);
4950 : 16896858 : DECL_FIELD_OFFSET (decl) = BINFO_OFFSET (binfo);
4951 : 16896858 : DECL_FIELD_BIT_OFFSET (decl) = bitsize_zero_node;
4952 : 16896858 : SET_DECL_OFFSET_ALIGN (decl, BITS_PER_UNIT);
4953 : 16896858 : SET_DECL_FIELD_ABI_IGNORED (decl, 1);
4954 : : }
4955 : :
4956 : : /* An empty virtual base causes a class to be non-empty
4957 : : -- but in that case we do not need to clear CLASSTYPE_EMPTY_P
4958 : : here because that was already done when the virtual table
4959 : : pointer was created. */
4960 : : }
4961 : :
4962 : : /* Record the offsets of BINFO and its base subobjects. */
4963 : 19257371 : record_subobject_offsets (binfo, offsets);
4964 : :
4965 : 19257371 : return next_field;
4966 : : }
4967 : :
4968 : : /* Layout all of the non-virtual base classes. Record empty
4969 : : subobjects in OFFSETS. T is the most derived type. Return nonzero
4970 : : if the type cannot be nearly empty. The fields created
4971 : : corresponding to the base classes will be inserted at
4972 : : *NEXT_FIELD. */
4973 : :
4974 : : static void
4975 : 36143417 : build_base_fields (record_layout_info rli,
4976 : : splay_tree offsets, tree *next_field)
4977 : : {
4978 : : /* Chain to hold all the new FIELD_DECLs which stand in for base class
4979 : : subobjects. */
4980 : 36143417 : tree t = rli->t;
4981 : 36143417 : tree binfo = TYPE_BINFO (t);
4982 : 36143417 : int n_baseclasses = BINFO_N_BASE_BINFOS (binfo);
4983 : :
4984 : : /* The primary base class is always allocated first. */
4985 : 36143417 : const tree primary_binfo = CLASSTYPE_PRIMARY_BINFO (t);
4986 : 36143417 : if (primary_binfo)
4987 : : {
4988 : : /* We need to walk BINFO_BASE_BINFO to find the access of the primary
4989 : : base, if it is direct. Indirect base fields are private. */
4990 : 1348535 : tree primary_access = access_private_node;
4991 : 1349569 : for (int i = 0; i < n_baseclasses; ++i)
4992 : : {
4993 : 1349484 : tree base_binfo = BINFO_BASE_BINFO (binfo, i);
4994 : 1349484 : if (base_binfo == primary_binfo)
4995 : : {
4996 : 1348450 : primary_access = BINFO_BASE_ACCESS (binfo, i);
4997 : 1348450 : break;
4998 : : }
4999 : : }
5000 : 1348535 : next_field = build_base_field (rli, primary_binfo,
5001 : : primary_access,
5002 : : offsets, next_field);
5003 : : }
5004 : :
5005 : : /* Now allocate the rest of the bases. */
5006 : 55258331 : for (int i = 0; i < n_baseclasses; ++i)
5007 : : {
5008 : 19114914 : tree base_binfo = BINFO_BASE_BINFO (binfo, i);
5009 : :
5010 : : /* The primary base was already allocated above, so we don't
5011 : : need to allocate it again here. */
5012 : 19114914 : if (base_binfo == primary_binfo)
5013 : 1348450 : continue;
5014 : :
5015 : : /* Virtual bases are added at the end (a primary virtual base
5016 : : will have already been added). */
5017 : 17766464 : if (BINFO_VIRTUAL_P (base_binfo))
5018 : 45393 : continue;
5019 : :
5020 : 17721071 : next_field = build_base_field (rli, base_binfo,
5021 : 17721071 : BINFO_BASE_ACCESS (binfo, i),
5022 : : offsets, next_field);
5023 : : }
5024 : 36143417 : }
5025 : :
5026 : : /* Go through the TYPE_FIELDS of T issuing any appropriate
5027 : : diagnostics, figuring out which methods override which other
5028 : : methods, and so forth. */
5029 : :
5030 : : static void
5031 : 36143417 : check_methods (tree t)
5032 : : {
5033 : 250141662 : for (tree x = TYPE_FIELDS (t); x; x = DECL_CHAIN (x))
5034 : 213998245 : if (DECL_DECLARES_FUNCTION_P (x))
5035 : : {
5036 : 108787428 : check_for_override (x, t);
5037 : :
5038 : 108787428 : if (DECL_PURE_VIRTUAL_P (x)
5039 : 108787428 : && (TREE_CODE (x) != FUNCTION_DECL || ! DECL_VINDEX (x)))
5040 : 4 : error ("initializer specified for non-virtual method %q+D", x);
5041 : : /* The name of the field is the original field name
5042 : : Save this in auxiliary field for later overloading. */
5043 : 108787428 : if (TREE_CODE (x) == FUNCTION_DECL && DECL_VINDEX (x))
5044 : : {
5045 : 5592062 : TYPE_POLYMORPHIC_P (t) = 1;
5046 : 5592062 : if (DECL_PURE_VIRTUAL_P (x))
5047 : 603191 : vec_safe_push (CLASSTYPE_PURE_VIRTUALS (t), x);
5048 : : }
5049 : :
5050 : 108787428 : if (!DECL_VIRTUAL_P (x)
5051 : 211982810 : && lookup_attribute ("transaction_safe_dynamic",
5052 : 103195382 : DECL_ATTRIBUTES (x)))
5053 : 2 : error_at (DECL_SOURCE_LOCATION (x),
5054 : : "%<transaction_safe_dynamic%> may only be specified for "
5055 : : "a virtual function");
5056 : : }
5057 : :
5058 : : /* Check whether the eligible special member functions (P0848) are
5059 : : user-provided. add_method arranged that the CLASSTYPE_MEMBER_VEC only
5060 : : has the eligible ones, unless none are eligible; TYPE_FIELDS also contains
5061 : : ineligible overloads, which is why this needs to be separate from the loop
5062 : : above. */
5063 : :
5064 : 36143417 : if (tree dtor = CLASSTYPE_DESTRUCTOR (t))
5065 : : {
5066 : 3410650 : if (TREE_CODE (dtor) == OVERLOAD)
5067 : : {
5068 : : /* P0848: At the end of the definition of a class, overload
5069 : : resolution is performed among the prospective destructors declared
5070 : : in that class with an empty argument list to select the destructor
5071 : : for the class, also known as the selected destructor. The program
5072 : : is ill-formed if overload resolution fails. */
5073 : 2 : int viable = 0;
5074 : 8 : for (tree fn : ovl_range (dtor))
5075 : 4 : if (constraints_satisfied_p (fn))
5076 : 2 : ++viable;
5077 : 2 : gcc_checking_assert (viable != 1);
5078 : :
5079 : 2 : auto_diagnostic_group d;
5080 : 2 : if (viable == 0)
5081 : 1 : error_at (location_of (t), "no viable destructor for %qT", t);
5082 : : else
5083 : 1 : error_at (location_of (t), "destructor for %qT is ambiguous", t);
5084 : 2 : print_candidates (dtor);
5085 : :
5086 : : /* Arbitrarily prune the overload set to a single function for
5087 : : sake of error recovery. */
5088 : 2 : tree *slot = find_member_slot (t, dtor_identifier);
5089 : 2 : *slot = get_first_fn (dtor);
5090 : 2 : }
5091 : 3410648 : else if (user_provided_p (dtor))
5092 : 2671338 : TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t) = true;
5093 : : }
5094 : :
5095 : 76132879 : for (tree fn : ovl_range (CLASSTYPE_CONSTRUCTORS (t)))
5096 : : {
5097 : 22740241 : if (!user_provided_p (fn))
5098 : : /* Might be trivial. */;
5099 : 18392119 : else if (TREE_CODE (fn) == TEMPLATE_DECL)
5100 : : /* Templates are never special members. */;
5101 : 11728786 : else if (copy_fn_p (fn)
5102 : 11728786 : && constraints_satisfied_p (fn))
5103 : 882556 : TYPE_HAS_COMPLEX_COPY_CTOR (t) = true;
5104 : 10846230 : else if (move_fn_p (fn)
5105 : 10846230 : && constraints_satisfied_p (fn))
5106 : 808664 : TYPE_HAS_COMPLEX_MOVE_CTOR (t) = true;
5107 : : }
5108 : :
5109 : 46515533 : for (tree fn : ovl_range (get_class_binding_direct (t, assign_op_identifier)))
5110 : : {
5111 : 6627463 : if (!user_provided_p (fn))
5112 : : /* Might be trivial. */;
5113 : 3540839 : else if (TREE_CODE (fn) == TEMPLATE_DECL)
5114 : : /* Templates are never special members. */;
5115 : 2603199 : else if (copy_fn_p (fn)
5116 : 2603199 : && constraints_satisfied_p (fn))
5117 : 576135 : TYPE_HAS_COMPLEX_COPY_ASSIGN (t) = true;
5118 : 2027064 : else if (move_fn_p (fn)
5119 : 2027064 : && constraints_satisfied_p (fn))
5120 : 769512 : TYPE_HAS_COMPLEX_MOVE_ASSIGN (t) = true;
5121 : : }
5122 : 36143417 : }
5123 : :
5124 : : /* FN is constructor, destructor or operator function. Clone the
5125 : : declaration to create a NAME'd variant. NEED_VTT_PARM_P and
5126 : : OMIT_INHERITED_PARMS_P are relevant if it's a cdtor. */
5127 : :
5128 : : static tree
5129 : 97045995 : copy_fndecl_with_name (tree fn, tree name, tree_code code,
5130 : : bool need_vtt_parm_p, bool omit_inherited_parms_p)
5131 : : {
5132 : : /* Copy the function. */
5133 : 97045995 : tree clone = copy_decl (fn);
5134 : : /* Reset the function name. */
5135 : 97045995 : DECL_NAME (clone) = name;
5136 : :
5137 : 97045995 : if (flag_concepts)
5138 : : /* Clone constraints. */
5139 : 29105308 : if (tree ci = get_constraints (fn))
5140 : 2751294 : set_constraints (clone, copy_node (ci));
5141 : :
5142 : 97045995 : SET_DECL_ASSEMBLER_NAME (clone, NULL_TREE);
5143 : : /* There's no pending inline data for this function. */
5144 : 97045995 : DECL_PENDING_INLINE_INFO (clone) = NULL;
5145 : 97045995 : DECL_PENDING_INLINE_P (clone) = 0;
5146 : :
5147 : 97045995 : if (name == base_dtor_identifier)
5148 : : {
5149 : : /* The base-class destructor is not virtual. */
5150 : 8025610 : DECL_VIRTUAL_P (clone) = 0;
5151 : 8025610 : DECL_VINDEX (clone) = NULL_TREE;
5152 : : }
5153 : 89020385 : else if (code != ERROR_MARK)
5154 : : {
5155 : : /* Set the operator code. */
5156 : 60 : const ovl_op_info_t *ovl_op = OVL_OP_INFO (false, code);
5157 : 60 : DECL_OVERLOADED_OPERATOR_CODE_RAW (clone) = ovl_op->ovl_op_code;
5158 : :
5159 : : /* The operator could be virtual. */
5160 : 60 : if (DECL_VIRTUAL_P (clone))
5161 : 4 : IDENTIFIER_VIRTUAL_P (name) = true;
5162 : : }
5163 : :
5164 : 97045995 : if (omit_inherited_parms_p)
5165 : 120 : gcc_assert (DECL_HAS_IN_CHARGE_PARM_P (clone));
5166 : :
5167 : : /* If there was an in-charge parameter, drop it from the function
5168 : : type. */
5169 : 97045995 : if (DECL_HAS_IN_CHARGE_PARM_P (clone))
5170 : : {
5171 : 19329827 : tree basetype = TYPE_METHOD_BASETYPE (TREE_TYPE (clone));
5172 : 19329827 : tree parmtypes = TYPE_ARG_TYPES (TREE_TYPE (clone));
5173 : : /* Skip the `this' parameter. */
5174 : 19329827 : parmtypes = TREE_CHAIN (parmtypes);
5175 : : /* Skip the in-charge parameter. */
5176 : 19329827 : parmtypes = TREE_CHAIN (parmtypes);
5177 : : /* And the VTT parm, in a complete [cd]tor. */
5178 : 19329827 : if (DECL_HAS_VTT_PARM_P (fn) && !need_vtt_parm_p)
5179 : 1230933 : parmtypes = TREE_CHAIN (parmtypes);
5180 : 19329827 : if (omit_inherited_parms_p)
5181 : : {
5182 : : /* If we're omitting inherited parms, that just leaves the VTT. */
5183 : 120 : gcc_assert (need_vtt_parm_p);
5184 : 120 : parmtypes = tree_cons (NULL_TREE, vtt_parm_type, void_list_node);
5185 : : }
5186 : 19329827 : TREE_TYPE (clone)
5187 : 19329827 : = build_method_type_directly (basetype,
5188 : 19329827 : TREE_TYPE (TREE_TYPE (clone)),
5189 : : parmtypes);
5190 : 19329827 : TREE_TYPE (clone)
5191 : 19329827 : = cp_build_type_attribute_variant (TREE_TYPE (clone),
5192 : 19329827 : TYPE_ATTRIBUTES (TREE_TYPE (fn)));
5193 : 19329827 : TREE_TYPE (clone)
5194 : 38659654 : = cxx_copy_lang_qualifiers (TREE_TYPE (clone), TREE_TYPE (fn));
5195 : : }
5196 : :
5197 : : /* Copy the function parameters. */
5198 : 97045995 : DECL_ARGUMENTS (clone) = copy_list (DECL_ARGUMENTS (clone));
5199 : :
5200 : : /* Remove the in-charge parameter. */
5201 : 97045995 : if (DECL_HAS_IN_CHARGE_PARM_P (clone))
5202 : : {
5203 : 19329827 : DECL_CHAIN (DECL_ARGUMENTS (clone))
5204 : 19329827 : = DECL_CHAIN (DECL_CHAIN (DECL_ARGUMENTS (clone)));
5205 : 19329827 : DECL_HAS_IN_CHARGE_PARM_P (clone) = 0;
5206 : : }
5207 : :
5208 : : /* And the VTT parm, in a complete [cd]tor. */
5209 : 97045995 : if (DECL_HAS_VTT_PARM_P (fn))
5210 : : {
5211 : 2283855 : if (need_vtt_parm_p)
5212 : 1052922 : DECL_HAS_VTT_PARM_P (clone) = 1;
5213 : : else
5214 : : {
5215 : 2461866 : DECL_CHAIN (DECL_ARGUMENTS (clone))
5216 : 1230933 : = DECL_CHAIN (DECL_CHAIN (DECL_ARGUMENTS (clone)));
5217 : 1230933 : DECL_HAS_VTT_PARM_P (clone) = 0;
5218 : : }
5219 : : }
5220 : :
5221 : : /* A base constructor inheriting from a virtual base doesn't get the
5222 : : arguments. */
5223 : 97045995 : if (omit_inherited_parms_p)
5224 : 120 : DECL_CHAIN (DECL_CHAIN (DECL_ARGUMENTS (clone))) = NULL_TREE;
5225 : :
5226 : 300851609 : for (tree parms = DECL_ARGUMENTS (clone); parms; parms = DECL_CHAIN (parms))
5227 : : {
5228 : 203805614 : DECL_CONTEXT (parms) = clone;
5229 : 203805614 : cxx_dup_lang_specific_decl (parms);
5230 : : }
5231 : :
5232 : : /* Create the RTL for this function. */
5233 : 97045995 : SET_DECL_RTL (clone, NULL);
5234 : :
5235 : : /* Regardless of the current scope, this is a member function, so
5236 : : not at namespace scope. */
5237 : 97045995 : rest_of_decl_compilation (clone, /*top_level=*/0, at_eof);
5238 : :
5239 : 97045995 : return clone;
5240 : : }
5241 : :
5242 : : /* FN is an operator function, create a variant for CODE. */
5243 : :
5244 : : tree
5245 : 60 : copy_operator_fn (tree fn, tree_code code)
5246 : : {
5247 : 60 : return copy_fndecl_with_name (fn, ovl_op_identifier (code),
5248 : 60 : code, false, false);
5249 : : }
5250 : :
5251 : : /* FN is a constructor or destructor. Clone the declaration to create
5252 : : a specialized in-charge or not-in-charge version, as indicated by
5253 : : NAME. */
5254 : :
5255 : : static tree
5256 : 111030261 : build_clone (tree fn, tree name, bool need_vtt_parm_p,
5257 : : bool omit_inherited_parms_p)
5258 : : {
5259 : 111030261 : tree clone;
5260 : :
5261 : : /* If this is a template, do the rest on the DECL_TEMPLATE_RESULT. */
5262 : 111030261 : if (TREE_CODE (fn) == TEMPLATE_DECL)
5263 : : {
5264 : 13984326 : clone = copy_decl (fn);
5265 : 13984326 : DECL_NAME (clone) = name;
5266 : :
5267 : 13984326 : tree result = build_clone (DECL_TEMPLATE_RESULT (clone), name,
5268 : : need_vtt_parm_p, omit_inherited_parms_p);
5269 : 13984326 : DECL_TEMPLATE_RESULT (clone) = result;
5270 : :
5271 : 13984326 : DECL_TEMPLATE_INFO (result) = copy_node (DECL_TEMPLATE_INFO (result));
5272 : 13984326 : DECL_TI_TEMPLATE (result) = clone;
5273 : :
5274 : 13984326 : TREE_TYPE (clone) = TREE_TYPE (result);
5275 : : }
5276 : : else
5277 : : {
5278 : 97045935 : clone = copy_fndecl_with_name (fn, name, ERROR_MARK,
5279 : : need_vtt_parm_p, omit_inherited_parms_p);
5280 : 97045935 : DECL_CLONED_FUNCTION (clone) = fn;
5281 : :
5282 : 97045935 : maybe_prepare_return_this (clone);
5283 : : }
5284 : :
5285 : : /* Remember where this function came from. */
5286 : 111030261 : DECL_ABSTRACT_ORIGIN (clone) = fn;
5287 : :
5288 : : /* Make it easy to find the CLONE given the FN. Note the
5289 : : template_result of a template will be chained this way too. */
5290 : 111030261 : DECL_CHAIN (clone) = DECL_CHAIN (fn);
5291 : 111030261 : DECL_CHAIN (fn) = clone;
5292 : :
5293 : 111030261 : return clone;
5294 : : }
5295 : :
5296 : : /* Build the clones of FN, return the number of clones built. These
5297 : : will be inserted onto DECL_CHAIN of FN. */
5298 : :
5299 : : void
5300 : 47755899 : build_cdtor_clones (tree fn, bool needs_vtt_p, bool base_omits_inherited_p,
5301 : : bool update_methods)
5302 : : {
5303 : 47755899 : unsigned count = 0;
5304 : :
5305 : 47755899 : if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (fn))
5306 : : {
5307 : : /* For each constructor, we need two variants: an in-charge version
5308 : : and a not-in-charge version. */
5309 : 39730289 : build_clone (fn, complete_ctor_identifier, false, false);
5310 : 39730289 : build_clone (fn, base_ctor_identifier, needs_vtt_p,
5311 : : base_omits_inherited_p);
5312 : 39730289 : count += 2;
5313 : : }
5314 : : else
5315 : : {
5316 : 8025610 : gcc_assert (DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (fn));
5317 : :
5318 : : /* For each destructor, we need three variants: an in-charge
5319 : : version, a not-in-charge version, and an in-charge deleting
5320 : : version. We clone the deleting version first because that
5321 : : means it will go second on the TYPE_FIELDS list -- and that
5322 : : corresponds to the correct layout order in the virtual
5323 : : function table.
5324 : :
5325 : : For a non-virtual destructor, we do not build a deleting
5326 : : destructor. */
5327 : 8025610 : if (DECL_VIRTUAL_P (fn))
5328 : : {
5329 : 1534137 : build_clone (fn, deleting_dtor_identifier, false, false);
5330 : 1534137 : count++;
5331 : : }
5332 : 8025610 : build_clone (fn, complete_dtor_identifier, false, false);
5333 : 8025610 : build_clone (fn, base_dtor_identifier, needs_vtt_p, false);
5334 : 8025610 : count += 2;
5335 : : }
5336 : :
5337 : : /* The original is now an abstract function that is never
5338 : : emitted. */
5339 : 47755899 : DECL_ABSTRACT_P (fn) = true;
5340 : :
5341 : 47755899 : if (update_methods)
5342 : 121635961 : for (tree clone = fn; count--;)
5343 : : {
5344 : 81601458 : clone = DECL_CHAIN (clone);
5345 : 81601458 : add_method (DECL_CONTEXT (clone), clone, false);
5346 : : }
5347 : 47755899 : }
5348 : :
5349 : : /* Produce declarations for all appropriate clones of FN. If
5350 : : UPDATE_METHODS is true, the clones are added to the
5351 : : CLASSTYPE_MEMBER_VEC. */
5352 : :
5353 : : void
5354 : 47964344 : clone_cdtor (tree fn, bool update_methods)
5355 : : {
5356 : : /* Avoid inappropriate cloning. */
5357 : 47964344 : if (DECL_CHAIN (fn)
5358 : 47964344 : && DECL_CLONED_FUNCTION_P (DECL_CHAIN (fn)))
5359 : : return;
5360 : :
5361 : : /* Base cdtors need a vtt parm if there are virtual bases. */
5362 : 47714690 : bool vtt = CLASSTYPE_VBASECLASSES (DECL_CONTEXT (fn));
5363 : :
5364 : : /* Base ctor omits inherited parms it needs a vttparm and inherited
5365 : : from a virtual nase ctor. */
5366 : 47714690 : bool base_omits_inherited = (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (fn)
5367 : 47714690 : && base_ctor_omit_inherited_parms (fn));
5368 : :
5369 : 47714690 : build_cdtor_clones (fn, vtt, base_omits_inherited, update_methods);
5370 : : }
5371 : :
5372 : : /* DECL is an in charge constructor, which is being defined. This will
5373 : : have had an in class declaration, from whence clones were
5374 : : declared. An out-of-class definition can specify additional default
5375 : : arguments. As it is the clones that are involved in overload
5376 : : resolution, we must propagate the information from the DECL to its
5377 : : clones. */
5378 : :
5379 : : void
5380 : 16776285 : adjust_clone_args (tree decl)
5381 : : {
5382 : 16776285 : tree clone;
5383 : :
5384 : 26833287 : for (clone = DECL_CHAIN (decl); clone && DECL_CLONED_FUNCTION_P (clone);
5385 : 10057002 : clone = DECL_CHAIN (clone))
5386 : : {
5387 : 10057002 : tree orig_clone_parms = TYPE_ARG_TYPES (TREE_TYPE (clone));
5388 : 10057002 : tree orig_decl_parms = TYPE_ARG_TYPES (TREE_TYPE (decl));
5389 : 10057002 : tree decl_parms, clone_parms;
5390 : :
5391 : : /* Skip the 'this' parameter. */
5392 : 10057002 : orig_clone_parms = TREE_CHAIN (orig_clone_parms);
5393 : 10057002 : orig_decl_parms = TREE_CHAIN (orig_decl_parms);
5394 : :
5395 : 10057002 : if (DECL_HAS_IN_CHARGE_PARM_P (decl))
5396 : 21476 : orig_decl_parms = TREE_CHAIN (orig_decl_parms);
5397 : 10057002 : if (DECL_HAS_VTT_PARM_P (decl))
5398 : 21476 : orig_decl_parms = TREE_CHAIN (orig_decl_parms);
5399 : :
5400 : 10057002 : clone_parms = orig_clone_parms;
5401 : 10057002 : if (DECL_HAS_VTT_PARM_P (clone))
5402 : 10738 : clone_parms = TREE_CHAIN (clone_parms);
5403 : :
5404 : 19888364 : for (decl_parms = orig_decl_parms; decl_parms;
5405 : 9831362 : decl_parms = TREE_CHAIN (decl_parms),
5406 : 9831362 : clone_parms = TREE_CHAIN (clone_parms))
5407 : : {
5408 : 19886740 : if (clone_parms == void_list_node)
5409 : : {
5410 : 10055346 : gcc_assert (decl_parms == clone_parms
5411 : : || ctor_omit_inherited_parms (clone));
5412 : : break;
5413 : : }
5414 : :
5415 : 9831394 : gcc_checking_assert (same_type_p (TREE_VALUE (decl_parms),
5416 : : TREE_VALUE (clone_parms)));
5417 : :
5418 : 10704626 : if (TREE_PURPOSE (decl_parms) && !TREE_PURPOSE (clone_parms))
5419 : : {
5420 : : /* A default parameter has been added. Adjust the
5421 : : clone's parameters. */
5422 : 32 : clone_parms = orig_decl_parms;
5423 : :
5424 : 32 : if (DECL_HAS_VTT_PARM_P (clone))
5425 : : {
5426 : 4 : clone_parms = tree_cons (TREE_PURPOSE (orig_clone_parms),
5427 : 4 : TREE_VALUE (orig_clone_parms),
5428 : : clone_parms);
5429 : 4 : TREE_TYPE (clone_parms) = TREE_TYPE (orig_clone_parms);
5430 : : }
5431 : :
5432 : 32 : tree basetype = TYPE_METHOD_BASETYPE (TREE_TYPE (clone));
5433 : 32 : tree type
5434 : 32 : = build_method_type_directly (basetype,
5435 : 32 : TREE_TYPE (TREE_TYPE (clone)),
5436 : : clone_parms);
5437 : 32 : if (tree attrs = TYPE_ATTRIBUTES (TREE_TYPE (clone)))
5438 : 0 : type = cp_build_type_attribute_variant (type, attrs);
5439 : 32 : type = cxx_copy_lang_qualifiers (type, TREE_TYPE (clone));
5440 : 32 : TREE_TYPE (clone) = type;
5441 : :
5442 : 32 : clone_parms = NULL_TREE;
5443 : 32 : break;
5444 : : }
5445 : : }
5446 : 10057002 : gcc_assert (!clone_parms || clone_parms == void_list_node);
5447 : : }
5448 : 16776285 : }
5449 : :
5450 : : /* For each of the constructors and destructors in T, create an
5451 : : in-charge and not-in-charge variant. */
5452 : :
5453 : : static void
5454 : 36143417 : clone_constructors_and_destructors (tree t)
5455 : : {
5456 : : /* We do not need to propagate the usingness to the clone, at this
5457 : : point that is not needed. */
5458 : 76566180 : for (tree fn : ovl_range (CLASSTYPE_CONSTRUCTORS (t)))
5459 : 22956896 : clone_cdtor (fn, /*update_methods=*/true);
5460 : :
5461 : 36143417 : if (tree dtor = CLASSTYPE_DESTRUCTOR (t))
5462 : 3573881 : clone_cdtor (dtor, /*update_methods=*/true);
5463 : 36143417 : }
5464 : :
5465 : : /* Deduce noexcept for a destructor DTOR. */
5466 : :
5467 : : void
5468 : 3472349 : deduce_noexcept_on_destructor (tree dtor)
5469 : : {
5470 : 3472349 : if (!TYPE_RAISES_EXCEPTIONS (TREE_TYPE (dtor)))
5471 : 2246163 : TREE_TYPE (dtor) = build_exception_variant (TREE_TYPE (dtor),
5472 : : noexcept_deferred_spec);
5473 : 3472349 : }
5474 : :
5475 : : /* Subroutine of set_one_vmethod_tm_attributes. Search base classes
5476 : : of TYPE for virtual functions which FNDECL overrides. Return a
5477 : : mask of the tm attributes found therein. */
5478 : :
5479 : : static int
5480 : 986 : look_for_tm_attr_overrides (tree type, tree fndecl)
5481 : : {
5482 : 986 : tree binfo = TYPE_BINFO (type);
5483 : 986 : tree base_binfo;
5484 : 986 : int ix, found = 0;
5485 : :
5486 : 1648 : for (ix = 0; BINFO_BASE_ITERATE (binfo, ix, base_binfo); ++ix)
5487 : : {
5488 : 662 : tree o, basetype = BINFO_TYPE (base_binfo);
5489 : :
5490 : 662 : if (!TYPE_POLYMORPHIC_P (basetype))
5491 : 48 : continue;
5492 : :
5493 : 614 : o = look_for_overrides_here (basetype, fndecl);
5494 : 614 : if (o)
5495 : : {
5496 : 444 : if (lookup_attribute ("transaction_safe_dynamic",
5497 : 444 : DECL_ATTRIBUTES (o)))
5498 : : /* transaction_safe_dynamic is not inherited. */;
5499 : : else
5500 : 341 : found |= tm_attr_to_mask (find_tm_attribute
5501 : 341 : (TYPE_ATTRIBUTES (TREE_TYPE (o))));
5502 : : }
5503 : : else
5504 : 170 : found |= look_for_tm_attr_overrides (basetype, fndecl);
5505 : : }
5506 : :
5507 : 986 : return found;
5508 : : }
5509 : :
5510 : : /* Subroutine of set_method_tm_attributes. Handle the checks and
5511 : : inheritance for one virtual method FNDECL. */
5512 : :
5513 : : static void
5514 : 816 : set_one_vmethod_tm_attributes (tree type, tree fndecl)
5515 : : {
5516 : 816 : tree tm_attr;
5517 : 816 : int found, have;
5518 : :
5519 : 816 : found = look_for_tm_attr_overrides (type, fndecl);
5520 : :
5521 : : /* If FNDECL doesn't actually override anything (i.e. T is the
5522 : : class that first declares FNDECL virtual), then we're done. */
5523 : 816 : if (found == 0)
5524 : : return;
5525 : :
5526 : 59 : tm_attr = find_tm_attribute (TYPE_ATTRIBUTES (TREE_TYPE (fndecl)));
5527 : 59 : have = tm_attr_to_mask (tm_attr);
5528 : :
5529 : : /* Intel STM Language Extension 3.0, Section 4.2 table 4:
5530 : : tm_pure must match exactly, otherwise no weakening of
5531 : : tm_safe > tm_callable > nothing. */
5532 : : /* ??? The tm_pure attribute didn't make the transition to the
5533 : : multivendor language spec. */
5534 : 59 : if (have == TM_ATTR_PURE)
5535 : : {
5536 : 0 : if (found != TM_ATTR_PURE)
5537 : : {
5538 : 0 : found &= -found;
5539 : 0 : goto err_override;
5540 : : }
5541 : : }
5542 : : /* If the overridden function is tm_pure, then FNDECL must be. */
5543 : 59 : else if (found == TM_ATTR_PURE && tm_attr)
5544 : 8 : goto err_override;
5545 : : /* Look for base class combinations that cannot be satisfied. */
5546 : 51 : else if (found != TM_ATTR_PURE && (found & TM_ATTR_PURE))
5547 : : {
5548 : 0 : found &= ~TM_ATTR_PURE;
5549 : 0 : found &= -found;
5550 : 0 : error_at (DECL_SOURCE_LOCATION (fndecl),
5551 : : "method overrides both %<transaction_pure%> and %qE methods",
5552 : : tm_mask_to_attr (found));
5553 : : }
5554 : : /* If FNDECL did not declare an attribute, then inherit the most
5555 : : restrictive one. */
5556 : 51 : else if (tm_attr == NULL)
5557 : : {
5558 : 18 : apply_tm_attr (fndecl, tm_mask_to_attr (least_bit_hwi (found)));
5559 : : }
5560 : : /* Otherwise validate that we're not weaker than a function
5561 : : that is being overridden. */
5562 : : else
5563 : : {
5564 : 33 : found &= -found;
5565 : 33 : if (found <= TM_ATTR_CALLABLE && have > found)
5566 : 12 : goto err_override;
5567 : : }
5568 : : return;
5569 : :
5570 : 20 : err_override:
5571 : 20 : error_at (DECL_SOURCE_LOCATION (fndecl),
5572 : : "method declared %qE overriding %qE method",
5573 : : tm_attr, tm_mask_to_attr (found));
5574 : : }
5575 : :
5576 : : /* For each of the methods in T, propagate a class-level tm attribute. */
5577 : :
5578 : : static void
5579 : 36143417 : set_method_tm_attributes (tree t)
5580 : : {
5581 : 36143417 : tree class_tm_attr, fndecl;
5582 : :
5583 : : /* Don't bother collecting tm attributes if transactional memory
5584 : : support is not enabled. */
5585 : 36143417 : if (!flag_tm)
5586 : : return;
5587 : :
5588 : : /* Process virtual methods first, as they inherit directly from the
5589 : : base virtual function and also require validation of new attributes. */
5590 : 4132 : if (TYPE_CONTAINS_VPTR_P (t))
5591 : : {
5592 : 228 : tree vchain;
5593 : 1044 : for (vchain = BINFO_VIRTUALS (TYPE_BINFO (t)); vchain;
5594 : 816 : vchain = TREE_CHAIN (vchain))
5595 : : {
5596 : 816 : fndecl = BV_FN (vchain);
5597 : 816 : if (DECL_THUNK_P (fndecl))
5598 : 4 : fndecl = THUNK_TARGET (fndecl);
5599 : 816 : set_one_vmethod_tm_attributes (t, fndecl);
5600 : : }
5601 : : }
5602 : :
5603 : : /* If the class doesn't have an attribute, nothing more to do. */
5604 : 4132 : class_tm_attr = find_tm_attribute (TYPE_ATTRIBUTES (t));
5605 : 4132 : if (class_tm_attr == NULL)
5606 : : return;
5607 : :
5608 : : /* Any method that does not yet have a tm attribute inherits
5609 : : the one from the class. */
5610 : 142 : for (fndecl = TYPE_FIELDS (t); fndecl; fndecl = DECL_CHAIN (fndecl))
5611 : 58 : if (DECL_DECLARES_FUNCTION_P (fndecl)
5612 : 106 : && !find_tm_attribute (TYPE_ATTRIBUTES (TREE_TYPE (fndecl))))
5613 : 28 : apply_tm_attr (fndecl, class_tm_attr);
5614 : : }
5615 : :
5616 : : /* Returns true if FN is a default constructor. */
5617 : :
5618 : : bool
5619 : 13976554 : default_ctor_p (const_tree fn)
5620 : : {
5621 : 13976554 : return (DECL_CONSTRUCTOR_P (fn)
5622 : 13976554 : && sufficient_parms_p (FUNCTION_FIRST_USER_PARMTYPE (fn)));
5623 : : }
5624 : :
5625 : : /* Returns true iff class T has a user-provided constructor that can be called
5626 : : with more than zero arguments. */
5627 : :
5628 : : bool
5629 : 63 : type_has_user_nondefault_constructor (tree t)
5630 : : {
5631 : 63 : if (!TYPE_HAS_USER_CONSTRUCTOR (t))
5632 : : return false;
5633 : :
5634 : 84 : for (tree fn : ovl_range (CLASSTYPE_CONSTRUCTORS (t)))
5635 : : {
5636 : 44 : if (user_provided_p (fn)
5637 : 44 : && (TREE_CODE (fn) == TEMPLATE_DECL
5638 : 23 : || (skip_artificial_parms_for (fn, DECL_ARGUMENTS (fn))
5639 : : != NULL_TREE)))
5640 : 4 : return true;
5641 : : }
5642 : :
5643 : 23 : return false;
5644 : : }
5645 : :
5646 : : /* Returns the defaulted constructor if T has one. Otherwise, returns
5647 : : NULL_TREE. */
5648 : :
5649 : : tree
5650 : 59 : in_class_defaulted_default_constructor (tree t)
5651 : : {
5652 : 59 : if (!TYPE_HAS_USER_CONSTRUCTOR (t))
5653 : : return NULL_TREE;
5654 : :
5655 : 81 : for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (t)); iter; ++iter)
5656 : : {
5657 : 52 : tree fn = *iter;
5658 : :
5659 : 104 : if (DECL_DEFAULTED_IN_CLASS_P (fn)
5660 : 78 : && default_ctor_p (fn))
5661 : 23 : return fn;
5662 : : }
5663 : :
5664 : 0 : return NULL_TREE;
5665 : : }
5666 : :
5667 : : /* Returns true iff FN is a user-provided function, i.e. user-declared
5668 : : and not defaulted at its first declaration. */
5669 : :
5670 : : bool
5671 : 83141727 : user_provided_p (tree fn)
5672 : : {
5673 : 83141727 : fn = STRIP_TEMPLATE (fn);
5674 : 83141727 : return (!DECL_ARTIFICIAL (fn)
5675 : 83141727 : && !(DECL_INITIALIZED_IN_CLASS_P (fn)
5676 : 68465068 : && (DECL_DEFAULTED_FN (fn) || DECL_DELETED_FN (fn))));
5677 : : }
5678 : :
5679 : : /* Returns true iff class T has a user-provided constructor. */
5680 : :
5681 : : bool
5682 : 36143571 : type_has_user_provided_constructor (tree t)
5683 : : {
5684 : 36143571 : if (!CLASS_TYPE_P (t))
5685 : : return false;
5686 : :
5687 : 36143511 : if (!TYPE_HAS_USER_CONSTRUCTOR (t))
5688 : : return false;
5689 : :
5690 : 7805321 : for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (t)); iter; ++iter)
5691 : 6509881 : if (user_provided_p (*iter))
5692 : 5214917 : return true;
5693 : :
5694 : 414617 : return false;
5695 : : }
5696 : :
5697 : : /* Returns true iff class T has a user-provided or explicit constructor. */
5698 : :
5699 : : bool
5700 : 33729170 : type_has_user_provided_or_explicit_constructor (tree t)
5701 : : {
5702 : 33729170 : if (!CLASS_TYPE_P (t))
5703 : : return false;
5704 : :
5705 : 33729170 : if (!TYPE_HAS_USER_CONSTRUCTOR (t))
5706 : : return false;
5707 : :
5708 : 7874708 : for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (t)); iter; ++iter)
5709 : : {
5710 : 6528199 : tree fn = *iter;
5711 : 6528199 : if (user_provided_p (fn) || DECL_NONCONVERTING_P (fn))
5712 : 5182137 : return true;
5713 : : }
5714 : :
5715 : 336671 : return false;
5716 : : }
5717 : :
5718 : : /* Returns true iff class T has a non-user-provided (i.e. implicitly
5719 : : declared or explicitly defaulted in the class body) default
5720 : : constructor. */
5721 : :
5722 : : bool
5723 : 2912942 : type_has_non_user_provided_default_constructor (tree t)
5724 : : {
5725 : 2912942 : if (!TYPE_HAS_DEFAULT_CONSTRUCTOR (t))
5726 : : return false;
5727 : 2912911 : if (CLASSTYPE_LAZY_DEFAULT_CTOR (t))
5728 : : return true;
5729 : :
5730 : 20266712 : for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (t)); iter; ++iter)
5731 : : {
5732 : 10733907 : tree fn = *iter;
5733 : 10733907 : if (TREE_CODE (fn) == FUNCTION_DECL
5734 : 7405975 : && default_ctor_p (fn)
5735 : 13598977 : && !user_provided_p (fn))
5736 : 1202330 : return true;
5737 : : }
5738 : :
5739 : 1709879 : return false;
5740 : : }
5741 : :
5742 : : /* TYPE is being used as a virtual base, and has a non-trivial move
5743 : : assignment. Return true if this is due to there being a user-provided
5744 : : move assignment in TYPE or one of its subobjects; if there isn't, then
5745 : : multiple move assignment can't cause any harm. */
5746 : :
5747 : : bool
5748 : 21 : vbase_has_user_provided_move_assign (tree type)
5749 : : {
5750 : : /* Does the type itself have a user-provided move assignment operator? */
5751 : 21 : if (!CLASSTYPE_LAZY_MOVE_ASSIGN (type))
5752 : 30 : for (ovl_iterator iter (get_class_binding_direct
5753 : 21 : (type, assign_op_identifier));
5754 : 51 : iter; ++iter)
5755 : 42 : if (user_provided_p (*iter) && move_fn_p (*iter))
5756 : 12 : return true;
5757 : :
5758 : : /* Do any of its bases? */
5759 : 9 : tree binfo = TYPE_BINFO (type);
5760 : 9 : tree base_binfo;
5761 : 12 : for (int i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
5762 : 6 : if (vbase_has_user_provided_move_assign (BINFO_TYPE (base_binfo)))
5763 : : return true;
5764 : :
5765 : : /* Or non-static data members? */
5766 : 27 : for (tree field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
5767 : : {
5768 : 21 : if (TREE_CODE (field) == FIELD_DECL
5769 : 3 : && CLASS_TYPE_P (TREE_TYPE (field))
5770 : 21 : && vbase_has_user_provided_move_assign (TREE_TYPE (field)))
5771 : : return true;
5772 : : }
5773 : :
5774 : : /* Seems not. */
5775 : : return false;
5776 : : }
5777 : :
5778 : : /* If default-initialization leaves part of TYPE uninitialized, returns
5779 : : a DECL for the field or TYPE itself (DR 253). */
5780 : :
5781 : : tree
5782 : 759386 : default_init_uninitialized_part (tree type)
5783 : : {
5784 : 759386 : tree t, r, binfo;
5785 : 759386 : int i;
5786 : :
5787 : 759386 : type = strip_array_types (type);
5788 : 759386 : if (!CLASS_TYPE_P (type))
5789 : : return type;
5790 : 646988 : if (!type_has_non_user_provided_default_constructor (type))
5791 : : return NULL_TREE;
5792 : 503552 : for (binfo = TYPE_BINFO (type), i = 0;
5793 : 503552 : BINFO_BASE_ITERATE (binfo, i, t); ++i)
5794 : : {
5795 : 38275 : r = default_init_uninitialized_part (BINFO_TYPE (t));
5796 : 38275 : if (r)
5797 : 5 : return r;
5798 : : }
5799 : 507979 : for (t = next_aggregate_field (TYPE_FIELDS (type)); t;
5800 : 42702 : t = next_aggregate_field (DECL_CHAIN (t)))
5801 : 150999 : if (!DECL_INITIAL (t) && !DECL_ARTIFICIAL (t))
5802 : : {
5803 : 108369 : r = default_init_uninitialized_part (TREE_TYPE (t));
5804 : 108369 : if (r)
5805 : 216586 : return DECL_P (r) ? r : t;
5806 : : }
5807 : :
5808 : : return NULL_TREE;
5809 : : }
5810 : :
5811 : : /* Returns true iff for class T, a trivial synthesized default constructor
5812 : : would be constexpr. */
5813 : :
5814 : : bool
5815 : 32695938 : trivial_default_constructor_is_constexpr (tree t)
5816 : : {
5817 : : /* A defaulted trivial default constructor is constexpr
5818 : : if there is nothing to initialize. */
5819 : 32695938 : gcc_assert (!TYPE_HAS_COMPLEX_DFLT (t));
5820 : : /* A class with a vptr doesn't have a trivial default ctor.
5821 : : In C++20, a class can have transient uninitialized members, e.g.:
5822 : :
5823 : : struct S { int i; constexpr S() = default; };
5824 : :
5825 : : should work. */
5826 : 32695938 : return (cxx_dialect >= cxx20
5827 : 32695938 : || is_really_empty_class (t, /*ignore_vptr*/true));
5828 : : }
5829 : :
5830 : : /* Returns true iff class T has a constexpr default constructor. */
5831 : :
5832 : : bool
5833 : 30170618 : type_has_constexpr_default_constructor (tree t)
5834 : : {
5835 : 30170618 : tree fns;
5836 : :
5837 : 30170618 : if (!CLASS_TYPE_P (t))
5838 : : {
5839 : : /* The caller should have stripped an enclosing array. */
5840 : 209 : gcc_assert (TREE_CODE (t) != ARRAY_TYPE);
5841 : : return false;
5842 : : }
5843 : 30170409 : if (CLASSTYPE_LAZY_DEFAULT_CTOR (t))
5844 : : {
5845 : 30166923 : if (!TYPE_HAS_COMPLEX_DFLT (t))
5846 : 30166856 : return trivial_default_constructor_is_constexpr (t);
5847 : : /* Non-trivial, we need to check subobject constructors. */
5848 : 67 : lazily_declare_fn (sfk_constructor, t);
5849 : : }
5850 : 3553 : fns = locate_ctor (t);
5851 : 3553 : return (fns && DECL_DECLARED_CONSTEXPR_P (fns));
5852 : : }
5853 : :
5854 : : /* Returns true iff class T has a constexpr default constructor or has an
5855 : : implicitly declared default constructor that we can't tell if it's constexpr
5856 : : without forcing a lazy declaration (which might cause undesired
5857 : : instantiations). */
5858 : :
5859 : : static bool
5860 : 30471936 : type_maybe_constexpr_default_constructor (tree t)
5861 : : {
5862 : 30471936 : if (CLASS_TYPE_P (t) && CLASSTYPE_LAZY_DEFAULT_CTOR (t)
5863 : 60943872 : && TYPE_HAS_COMPLEX_DFLT (t))
5864 : : /* Assume it's constexpr. */
5865 : : return true;
5866 : 30166856 : return type_has_constexpr_default_constructor (t);
5867 : : }
5868 : :
5869 : : /* Returns true iff class T has a constexpr destructor. */
5870 : :
5871 : : bool
5872 : 148 : type_has_constexpr_destructor (tree t)
5873 : : {
5874 : 148 : tree fns;
5875 : :
5876 : 148 : if (CLASSTYPE_LAZY_DESTRUCTOR (t))
5877 : : /* Non-trivial, we need to check subobject destructors. */
5878 : 6 : lazily_declare_fn (sfk_destructor, t);
5879 : 148 : fns = CLASSTYPE_DESTRUCTOR (t);
5880 : 148 : return (fns && DECL_DECLARED_CONSTEXPR_P (fns));
5881 : : }
5882 : :
5883 : : /* Returns true iff class T has a constexpr destructor or has an
5884 : : implicitly declared destructor that we can't tell if it's constexpr
5885 : : without forcing a lazy declaration (which might cause undesired
5886 : : instantiations). */
5887 : :
5888 : : static bool
5889 : 33566234 : type_maybe_constexpr_destructor (tree t)
5890 : : {
5891 : : /* Until C++20, only trivial destruction is constexpr. */
5892 : 33566234 : if (TYPE_HAS_TRIVIAL_DESTRUCTOR (t))
5893 : : return true;
5894 : 1084886 : if (cxx_dialect < cxx20)
5895 : : return false;
5896 : 336679 : if (CLASS_TYPE_P (t) && CLASSTYPE_LAZY_DESTRUCTOR (t))
5897 : : /* Assume it's constexpr. */
5898 : : return true;
5899 : 291555 : tree fn = CLASSTYPE_DESTRUCTOR (t);
5900 : 291555 : return (fn && maybe_constexpr_fn (fn));
5901 : : }
5902 : :
5903 : : /* Returns true iff class TYPE has a virtual destructor. */
5904 : :
5905 : : bool
5906 : 3040669 : type_has_virtual_destructor (tree type)
5907 : : {
5908 : 3040669 : tree dtor;
5909 : :
5910 : 3040669 : if (!NON_UNION_CLASS_TYPE_P (type))
5911 : : return false;
5912 : :
5913 : 3040525 : gcc_assert (COMPLETE_TYPE_P (type));
5914 : 3040525 : dtor = CLASSTYPE_DESTRUCTOR (type);
5915 : 3040525 : return (dtor && DECL_VIRTUAL_P (dtor));
5916 : : }
5917 : :
5918 : : /* True iff class TYPE has a non-deleted trivial default
5919 : : constructor. */
5920 : :
5921 : 10 : bool type_has_non_deleted_trivial_default_ctor (tree type)
5922 : : {
5923 : 10 : return TYPE_HAS_TRIVIAL_DFLT (type) && locate_ctor (type);
5924 : : }
5925 : :
5926 : : /* Returns true iff T, a class, has a move-assignment or
5927 : : move-constructor. Does not lazily declare either.
5928 : : If USER_P is false, any move function will do. If it is true, the
5929 : : move function must be user-declared.
5930 : :
5931 : : Note that user-declared here is different from "user-provided",
5932 : : which doesn't include functions that are defaulted in the
5933 : : class. */
5934 : :
5935 : : bool
5936 : 37318553 : classtype_has_move_assign_or_move_ctor_p (tree t, bool user_p)
5937 : : {
5938 : 37318553 : gcc_assert (user_p
5939 : : || (!CLASSTYPE_LAZY_MOVE_CTOR (t)
5940 : : && !CLASSTYPE_LAZY_MOVE_ASSIGN (t)));
5941 : :
5942 : 37318553 : if (!CLASSTYPE_LAZY_MOVE_CTOR (t))
5943 : 44287544 : for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (t)); iter; ++iter)
5944 : 7371273 : if ((!user_p || !DECL_ARTIFICIAL (*iter)) && move_fn_p (*iter))
5945 : 895644 : return true;
5946 : :
5947 : 36422909 : if (!CLASSTYPE_LAZY_MOVE_ASSIGN (t))
5948 : 916855 : for (ovl_iterator iter (get_class_binding_direct
5949 : 33115241 : (t, assign_op_identifier));
5950 : 34032096 : iter; ++iter)
5951 : 886310 : if ((!user_p || !DECL_ARTIFICIAL (*iter))
5952 : 102877 : && DECL_CONTEXT (*iter) == t
5953 : 975810 : && move_fn_p (*iter))
5954 : 1487 : return true;
5955 : :
5956 : : return false;
5957 : : }
5958 : :
5959 : : /* True iff T has a move constructor that is not deleted. */
5960 : :
5961 : : bool
5962 : 6 : classtype_has_non_deleted_move_ctor (tree t)
5963 : : {
5964 : 6 : if (CLASSTYPE_LAZY_MOVE_CTOR (t))
5965 : 0 : lazily_declare_fn (sfk_move_constructor, t);
5966 : 6 : for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (t)); iter; ++iter)
5967 : 6 : if (move_fn_p (*iter) && !DECL_DELETED_FN (*iter))
5968 : 6 : return true;
5969 : 0 : return false;
5970 : : }
5971 : :
5972 : : /* If T, a class, has a user-provided copy constructor, copy assignment
5973 : : operator, or destructor, returns that function. Otherwise, null. */
5974 : :
5975 : : tree
5976 : 4395305 : classtype_has_depr_implicit_copy (tree t)
5977 : : {
5978 : 4395305 : if (!CLASSTYPE_LAZY_COPY_CTOR (t))
5979 : 11253189 : for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (t)); iter; ++iter)
5980 : : {
5981 : 5609424 : tree fn = *iter;
5982 : 5609424 : if (user_provided_p (fn) && copy_fn_p (fn))
5983 : 740 : return fn;
5984 : : }
5985 : :
5986 : 4394565 : if (!CLASSTYPE_LAZY_COPY_ASSIGN (t))
5987 : 819513 : for (ovl_iterator iter (get_class_binding_direct
5988 : 1250963 : (t, assign_op_identifier));
5989 : 2070476 : iter; ++iter)
5990 : : {
5991 : 819918 : tree fn = *iter;
5992 : 819918 : if (DECL_CONTEXT (fn) == t
5993 : 819918 : && user_provided_p (fn) && copy_fn_p (fn))
5994 : 405 : return fn;
5995 : : }
5996 : :
5997 : 4394160 : if (!CLASSTYPE_LAZY_DESTRUCTOR (t))
5998 : : {
5999 : 1413346 : tree fn = CLASSTYPE_DESTRUCTOR (t);
6000 : 1413346 : if (user_provided_p (fn))
6001 : : return fn;
6002 : : }
6003 : :
6004 : : return NULL_TREE;
6005 : : }
6006 : :
6007 : : /* True iff T has a member or friend declaration of operator OP. */
6008 : :
6009 : : bool
6010 : 36143417 : classtype_has_op (tree t, tree_code op)
6011 : : {
6012 : 36143417 : tree name = ovl_op_identifier (op);
6013 : 36143417 : if (get_class_binding (t, name))
6014 : : return true;
6015 : 37415177 : for (tree f = DECL_FRIENDLIST (TYPE_MAIN_DECL (t)); f; f = TREE_CHAIN (f))
6016 : 2067595 : if (FRIEND_NAME (f) == name)
6017 : : return true;
6018 : : return false;
6019 : : }
6020 : :
6021 : :
6022 : : /* If T has a defaulted member or friend declaration of OP, return it. */
6023 : :
6024 : : tree
6025 : 35347582 : classtype_has_defaulted_op (tree t, tree_code op)
6026 : : {
6027 : 35347582 : tree name = ovl_op_identifier (op);
6028 : 35347603 : for (ovl_iterator oi (get_class_binding (t, name)); oi; ++oi)
6029 : : {
6030 : 69 : tree fn = *oi;
6031 : 69 : if (DECL_DEFAULTED_FN (fn))
6032 : 51 : return fn;
6033 : : }
6034 : 35869225 : for (tree f = DECL_FRIENDLIST (TYPE_MAIN_DECL (t)); f; f = TREE_CHAIN (f))
6035 : 521703 : if (FRIEND_NAME (f) == name)
6036 : 169 : for (tree l = FRIEND_DECLS (f); l; l = TREE_CHAIN (l))
6037 : : {
6038 : 94 : tree fn = TREE_VALUE (l);
6039 : 94 : if (DECL_DEFAULTED_FN (fn))
6040 : 9 : return fn;
6041 : : }
6042 : : return NULL_TREE;
6043 : : }
6044 : :
6045 : : /* Nonzero if we need to build up a constructor call when initializing an
6046 : : object of this class, either because it has a user-declared constructor
6047 : : or because it doesn't have a default constructor (so we need to give an
6048 : : error if no initializer is provided). Use TYPE_NEEDS_CONSTRUCTING when
6049 : : what you care about is whether or not an object can be produced by a
6050 : : constructor (e.g. so we don't set TREE_READONLY on const variables of
6051 : : such type); use this function when what you care about is whether or not
6052 : : to try to call a constructor to create an object. The latter case is
6053 : : the former plus some cases of constructors that cannot be called. */
6054 : :
6055 : : bool
6056 : 45234336 : type_build_ctor_call (tree t)
6057 : : {
6058 : 45234336 : tree inner;
6059 : 45234336 : if (TYPE_NEEDS_CONSTRUCTING (t))
6060 : : return true;
6061 : 40282500 : inner = strip_array_types (t);
6062 : 40282500 : if (!CLASS_TYPE_P (inner) || ANON_AGGR_TYPE_P (inner))
6063 : : return false;
6064 : 3295699 : if (!TYPE_HAS_DEFAULT_CONSTRUCTOR (inner))
6065 : : return true;
6066 : 2946809 : if (cxx_dialect < cxx11)
6067 : : return false;
6068 : : /* A user-declared constructor might be private, and a constructor might
6069 : : be trivial but deleted. */
6070 : 11188165 : for (ovl_iterator iter (get_class_binding (inner, complete_ctor_identifier));
6071 : 11188165 : iter; ++iter)
6072 : : {
6073 : 8609140 : tree fn = *iter;
6074 : 8609140 : if (!DECL_ARTIFICIAL (fn)
6075 : : || TREE_DEPRECATED (fn)
6076 : 8459548 : || TREE_UNAVAILABLE (fn)
6077 : 16968492 : || DECL_DELETED_FN (fn))
6078 : 357983 : return true;
6079 : : }
6080 : 2579025 : return false;
6081 : : }
6082 : :
6083 : : /* Like type_build_ctor_call, but for destructors. */
6084 : :
6085 : : bool
6086 : 85248885 : type_build_dtor_call (tree t)
6087 : : {
6088 : 85248885 : tree inner;
6089 : 85248885 : if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t))
6090 : : return true;
6091 : 62405789 : inner = strip_array_types (t);
6092 : 25141665 : if (!CLASS_TYPE_P (inner) || ANON_AGGR_TYPE_P (inner)
6093 : 87460163 : || !COMPLETE_TYPE_P (inner))
6094 : : return false;
6095 : 25054350 : if (cxx_dialect < cxx11)
6096 : : return false;
6097 : : /* A user-declared destructor might be private, and a destructor might
6098 : : be trivial but deleted. */
6099 : 43416887 : for (ovl_iterator iter (get_class_binding (inner, complete_dtor_identifier));
6100 : 43416887 : iter; ++iter)
6101 : : {
6102 : 24669397 : tree fn = *iter;
6103 : 24669397 : if (!DECL_ARTIFICIAL (fn)
6104 : : || TREE_DEPRECATED (fn)
6105 : 18401267 : || TREE_UNAVAILABLE (fn)
6106 : 43070664 : || DECL_DELETED_FN (fn))
6107 : 6268130 : return true;
6108 : : }
6109 : 18747490 : return false;
6110 : : }
6111 : :
6112 : : /* Returns TRUE iff we need a cookie when dynamically allocating an
6113 : : array whose elements have the indicated class TYPE. */
6114 : :
6115 : : static bool
6116 : 36143417 : type_requires_array_cookie (tree type)
6117 : : {
6118 : 36143417 : tree fns;
6119 : 36143417 : bool has_two_argument_delete_p = false;
6120 : :
6121 : 36143417 : gcc_assert (CLASS_TYPE_P (type));
6122 : :
6123 : : /* If there's a non-trivial destructor, we need a cookie. In order
6124 : : to iterate through the array calling the destructor for each
6125 : : element, we'll have to know how many elements there are. */
6126 : 36143417 : if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
6127 : : return true;
6128 : :
6129 : : /* If the usual deallocation function is a two-argument whose second
6130 : : argument is of type `size_t', then we have to pass the size of
6131 : : the array to the deallocation function, so we will need to store
6132 : : a cookie. */
6133 : 32785487 : fns = lookup_fnfields (TYPE_BINFO (type),
6134 : : ovl_op_identifier (false, VEC_DELETE_EXPR),
6135 : : /*protect=*/0, tf_warning_or_error);
6136 : : /* If there are no `operator []' members, or the lookup is
6137 : : ambiguous, then we don't need a cookie. */
6138 : 32785487 : if (!fns || fns == error_mark_node)
6139 : : return false;
6140 : : /* Loop through all of the functions. */
6141 : 295 : for (lkp_iterator iter (BASELINK_FUNCTIONS (fns)); iter; ++iter)
6142 : : {
6143 : 279 : tree fn = *iter;
6144 : :
6145 : : /* See if this function is a one-argument delete function. If
6146 : : it is, then it will be the usual deallocation function. */
6147 : 279 : tree second_parm = TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (fn)));
6148 : 279 : if (second_parm == void_list_node)
6149 : 119 : return false;
6150 : : /* Do not consider this function if its second argument is an
6151 : : ellipsis. */
6152 : 160 : if (!second_parm)
6153 : 4 : continue;
6154 : : /* Otherwise, if we have a two-argument function and the second
6155 : : argument is `size_t', it will be the usual deallocation
6156 : : function -- unless there is one-argument function, too. */
6157 : 156 : if (TREE_CHAIN (second_parm) == void_list_node
6158 : 156 : && same_type_p (TREE_VALUE (second_parm), size_type_node))
6159 : : has_two_argument_delete_p = true;
6160 : : }
6161 : :
6162 : 16 : return has_two_argument_delete_p;
6163 : : }
6164 : :
6165 : : /* Finish computing the `literal type' property of class type T.
6166 : :
6167 : : At this point, we have already processed base classes and
6168 : : non-static data members. We need to check whether the copy
6169 : : constructor is trivial, the destructor is trivial, and there
6170 : : is a trivial default constructor or at least one constexpr
6171 : : constructor other than the copy constructor. */
6172 : :
6173 : : static void
6174 : 36143417 : finalize_literal_type_property (tree t)
6175 : : {
6176 : 36143417 : tree fn;
6177 : :
6178 : 36143417 : if (cxx_dialect < cxx11)
6179 : 111188 : CLASSTYPE_LITERAL_P (t) = false;
6180 : 36032229 : else if (CLASSTYPE_LITERAL_P (t)
6181 : 36032229 : && !type_maybe_constexpr_destructor (t))
6182 : 880371 : CLASSTYPE_LITERAL_P (t) = false;
6183 : 67445406 : else if (CLASSTYPE_LITERAL_P (t) && LAMBDA_TYPE_P (t))
6184 : 236571 : CLASSTYPE_LITERAL_P (t) = (cxx_dialect >= cxx17);
6185 : 67364572 : else if (CLASSTYPE_LITERAL_P (t) && !TYPE_HAS_TRIVIAL_DFLT (t)
6186 : 1936520 : && CLASSTYPE_NON_AGGREGATE (t)
6187 : 36782731 : && !TYPE_HAS_CONSTEXPR_CTOR (t))
6188 : 565231 : CLASSTYPE_LITERAL_P (t) = false;
6189 : :
6190 : : /* C++14 DR 1684 removed this restriction. */
6191 : 36143417 : if (cxx_dialect < cxx14
6192 : 36265102 : && !CLASSTYPE_LITERAL_P (t) && !LAMBDA_TYPE_P (t))
6193 : 1355295 : for (fn = TYPE_FIELDS (t); fn; fn = DECL_CHAIN (fn))
6194 : 1226171 : if (TREE_CODE (fn) == FUNCTION_DECL
6195 : 599613 : && DECL_DECLARED_CONSTEXPR_P (fn)
6196 : 2280 : && DECL_IOBJ_MEMBER_FUNCTION_P (fn)
6197 : 1228132 : && !DECL_CONSTRUCTOR_P (fn))
6198 : : {
6199 : 495 : DECL_DECLARED_CONSTEXPR_P (fn) = false;
6200 : 495 : if (!DECL_GENERATED_P (fn))
6201 : : {
6202 : 3 : auto_diagnostic_group d;
6203 : 3 : if (pedwarn (DECL_SOURCE_LOCATION (fn), OPT_Wpedantic,
6204 : : "enclosing class of %<constexpr%> non-static "
6205 : : "member function %q+#D is not a literal type", fn))
6206 : 3 : explain_non_literal_class (t);
6207 : 3 : }
6208 : : }
6209 : 36143417 : }
6210 : :
6211 : : /* T is a non-literal type used in a context which requires a constant
6212 : : expression. Explain why it isn't literal. */
6213 : :
6214 : : void
6215 : 120 : explain_non_literal_class (tree t)
6216 : : {
6217 : 120 : static hash_set<tree> *diagnosed;
6218 : :
6219 : 120 : if (!CLASS_TYPE_P (t))
6220 : 46 : return;
6221 : 118 : t = TYPE_MAIN_VARIANT (t);
6222 : :
6223 : 118 : if (diagnosed == NULL)
6224 : 62 : diagnosed = new hash_set<tree>;
6225 : 118 : if (diagnosed->add (t))
6226 : : /* Already explained. */
6227 : : return;
6228 : :
6229 : 89 : auto_diagnostic_group d;
6230 : 89 : inform (UNKNOWN_LOCATION, "%q+T is not literal because:", t);
6231 : 120 : if (cxx_dialect < cxx17 && LAMBDA_TYPE_P (t))
6232 : 1 : inform (UNKNOWN_LOCATION,
6233 : : " %qT is a closure type, which is only literal in "
6234 : : "C++17 and later", t);
6235 : 88 : else if (cxx_dialect < cxx20 && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t))
6236 : 8 : inform (UNKNOWN_LOCATION, " %q+T has a non-trivial destructor", t);
6237 : 80 : else if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t)
6238 : 80 : && !type_maybe_constexpr_destructor (t))
6239 : 7 : inform (UNKNOWN_LOCATION, " %q+T does not have %<constexpr%> destructor",
6240 : : t);
6241 : 73 : else if (CLASSTYPE_NON_AGGREGATE (t)
6242 : 63 : && !TYPE_HAS_TRIVIAL_DFLT (t)
6243 : 123 : && !LAMBDA_TYPE_P (t)
6244 : 136 : && !TYPE_HAS_CONSTEXPR_CTOR (t))
6245 : : {
6246 : 40 : inform (UNKNOWN_LOCATION,
6247 : : " %q+T is not an aggregate, does not have a trivial "
6248 : : "default constructor, and has no %<constexpr%> constructor that "
6249 : : "is not a copy or move constructor", t);
6250 : 40 : if (type_has_non_user_provided_default_constructor (t))
6251 : : /* Note that we can't simply call locate_ctor because when the
6252 : : constructor is deleted it just returns NULL_TREE. */
6253 : 0 : for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (t)); iter; ++iter)
6254 : : {
6255 : 0 : tree fn = *iter;
6256 : 0 : tree parms = TYPE_ARG_TYPES (TREE_TYPE (fn));
6257 : :
6258 : 0 : parms = skip_artificial_parms_for (fn, parms);
6259 : :
6260 : 0 : if (sufficient_parms_p (parms))
6261 : : {
6262 : 0 : if (DECL_DELETED_FN (fn))
6263 : 0 : maybe_explain_implicit_delete (fn);
6264 : : else
6265 : 0 : explain_invalid_constexpr_fn (fn);
6266 : : break;
6267 : : }
6268 : : }
6269 : : }
6270 : : else
6271 : : {
6272 : 33 : tree binfo, base_binfo, field; int i;
6273 : 33 : for (binfo = TYPE_BINFO (t), i = 0;
6274 : 33 : BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
6275 : : {
6276 : 15 : tree basetype = TREE_TYPE (base_binfo);
6277 : 15 : if (!CLASSTYPE_LITERAL_P (basetype))
6278 : : {
6279 : 15 : inform (UNKNOWN_LOCATION,
6280 : : " base class %qT of %q+T is non-literal",
6281 : : basetype, t);
6282 : 15 : explain_non_literal_class (basetype);
6283 : 15 : return;
6284 : : }
6285 : : }
6286 : 138 : for (field = TYPE_FIELDS (t); field; field = TREE_CHAIN (field))
6287 : : {
6288 : 120 : tree ftype;
6289 : 120 : if (TREE_CODE (field) != FIELD_DECL)
6290 : 102 : continue;
6291 : 18 : ftype = TREE_TYPE (field);
6292 : 18 : if (!literal_type_p (ftype))
6293 : : {
6294 : 6 : inform (DECL_SOURCE_LOCATION (field),
6295 : : " non-static data member %qD has non-literal type",
6296 : : field);
6297 : 6 : if (CLASS_TYPE_P (ftype))
6298 : 6 : explain_non_literal_class (ftype);
6299 : : }
6300 : 18 : if (CP_TYPE_VOLATILE_P (ftype))
6301 : 12 : inform (DECL_SOURCE_LOCATION (field),
6302 : : " non-static data member %qD has volatile type", field);
6303 : : }
6304 : : }
6305 : 89 : }
6306 : :
6307 : : /* Check the validity of the bases and members declared in T. Add any
6308 : : implicitly-generated functions (like copy-constructors and
6309 : : assignment operators). Compute various flag bits (like
6310 : : CLASSTYPE_NON_LAYOUT_POD_T) for T. This routine works purely at the C++
6311 : : level: i.e., independently of the ABI in use. */
6312 : :
6313 : : static void
6314 : 36143417 : check_bases_and_members (tree t)
6315 : : {
6316 : : /* Nonzero if the implicitly generated copy constructor should take
6317 : : a non-const reference argument. */
6318 : 36143417 : int cant_have_const_ctor;
6319 : : /* Nonzero if the implicitly generated assignment operator
6320 : : should take a non-const reference argument. */
6321 : 36143417 : int no_const_asn_ref;
6322 : 36143417 : tree access_decls;
6323 : 36143417 : bool saved_complex_asn_ref;
6324 : 36143417 : bool saved_nontrivial_dtor;
6325 : 36143417 : tree fn;
6326 : :
6327 : : /* By default, we use const reference arguments and generate default
6328 : : constructors. */
6329 : 36143417 : cant_have_const_ctor = 0;
6330 : 36143417 : no_const_asn_ref = 0;
6331 : :
6332 : : /* Check all the base-classes and set FMEM members to point to arrays
6333 : : of potential interest. */
6334 : 36143417 : check_bases (t, &cant_have_const_ctor, &no_const_asn_ref);
6335 : :
6336 : : /* Deduce noexcept on destructor. This needs to happen after we've set
6337 : : triviality flags appropriately for our bases, and before checking
6338 : : overriden virtual functions via check_methods. */
6339 : 36143417 : if (cxx_dialect >= cxx11)
6340 : 36032229 : if (tree dtor = CLASSTYPE_DESTRUCTOR (t))
6341 : 6780090 : for (tree fn : ovl_range (dtor))
6342 : 3390045 : deduce_noexcept_on_destructor (fn);
6343 : :
6344 : : /* Check all the method declarations. */
6345 : 36143417 : check_methods (t);
6346 : :
6347 : : /* Save the initial values of these flags which only indicate whether
6348 : : or not the class has user-provided functions. As we analyze the
6349 : : bases and members we can set these flags for other reasons. */
6350 : 36143417 : saved_complex_asn_ref = TYPE_HAS_COMPLEX_COPY_ASSIGN (t);
6351 : 36143417 : saved_nontrivial_dtor = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t);
6352 : :
6353 : : /* Check all the data member declarations. We cannot call
6354 : : check_field_decls until we have called check_bases check_methods,
6355 : : as check_field_decls depends on TYPE_HAS_NONTRIVIAL_DESTRUCTOR
6356 : : being set appropriately. */
6357 : 36143417 : check_field_decls (t, &access_decls,
6358 : : &cant_have_const_ctor,
6359 : : &no_const_asn_ref);
6360 : :
6361 : : /* A nearly-empty class has to be vptr-containing; a nearly empty
6362 : : class contains just a vptr. */
6363 : 36143417 : if (!TYPE_CONTAINS_VPTR_P (t))
6364 : 34562496 : CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
6365 : :
6366 : : /* Do some bookkeeping that will guide the generation of implicitly
6367 : : declared member functions. */
6368 : 37724338 : TYPE_HAS_COMPLEX_COPY_CTOR (t) |= TYPE_CONTAINS_VPTR_P (t);
6369 : 37724338 : TYPE_HAS_COMPLEX_MOVE_CTOR (t) |= TYPE_CONTAINS_VPTR_P (t);
6370 : : /* We need to call a constructor for this class if it has a
6371 : : user-provided constructor, or if the default constructor is going
6372 : : to initialize the vptr. (This is not an if-and-only-if;
6373 : : TYPE_NEEDS_CONSTRUCTING is set elsewhere if bases or members
6374 : : themselves need constructing.) */
6375 : 36143417 : TYPE_NEEDS_CONSTRUCTING (t)
6376 : 36143417 : |= (type_has_user_provided_constructor (t) || TYPE_CONTAINS_VPTR_P (t));
6377 : : /* [dcl.init.aggr]
6378 : :
6379 : : An aggregate is an array or a class with no user-provided
6380 : : constructors ... and no virtual functions.
6381 : :
6382 : : Again, other conditions for being an aggregate are checked
6383 : : elsewhere. */
6384 : 36143417 : CLASSTYPE_NON_AGGREGATE (t)
6385 : 72286834 : |= ((cxx_dialect < cxx20
6386 : 36143417 : ? type_has_user_provided_or_explicit_constructor (t)
6387 : 12023801 : : TYPE_HAS_USER_CONSTRUCTOR (t))
6388 : 36143417 : || TYPE_POLYMORPHIC_P (t));
6389 : : /* This is the C++98/03 definition of POD; it changed in C++0x, but we
6390 : : retain the old definition internally for ABI reasons. */
6391 : 36143417 : CLASSTYPE_NON_LAYOUT_POD_P (t)
6392 : 36143417 : |= (CLASSTYPE_NON_AGGREGATE (t)
6393 : 36143417 : || saved_nontrivial_dtor || saved_complex_asn_ref);
6394 : 37724338 : CLASSTYPE_NON_STD_LAYOUT (t) |= TYPE_CONTAINS_VPTR_P (t);
6395 : 37724338 : TYPE_HAS_COMPLEX_COPY_ASSIGN (t) |= TYPE_CONTAINS_VPTR_P (t);
6396 : 37724338 : TYPE_HAS_COMPLEX_MOVE_ASSIGN (t) |= TYPE_CONTAINS_VPTR_P (t);
6397 : 37724338 : TYPE_HAS_COMPLEX_DFLT (t) |= TYPE_CONTAINS_VPTR_P (t);
6398 : :
6399 : : /* Is this class non-layout-POD because it wasn't an aggregate in C++98? */
6400 : 36143417 : if (CLASSTYPE_NON_POD_AGGREGATE (t))
6401 : : {
6402 : 369522 : if (CLASSTYPE_NON_LAYOUT_POD_P (t))
6403 : : /* It's non-POD for another reason. */
6404 : 345503 : CLASSTYPE_NON_POD_AGGREGATE (t) = false;
6405 : 24019 : else if (abi_version_at_least (17))
6406 : 24016 : CLASSTYPE_NON_LAYOUT_POD_P (t) = true;
6407 : : }
6408 : :
6409 : : /* If the only explicitly declared default constructor is user-provided,
6410 : : set TYPE_HAS_COMPLEX_DFLT. */
6411 : 36143417 : if (!TYPE_HAS_COMPLEX_DFLT (t)
6412 : 32627290 : && TYPE_HAS_DEFAULT_CONSTRUCTOR (t)
6413 : 38125649 : && !type_has_non_user_provided_default_constructor (t))
6414 : 1340311 : TYPE_HAS_COMPLEX_DFLT (t) = true;
6415 : :
6416 : : /* Warn if a public base of a polymorphic type has an accessible
6417 : : non-virtual destructor. It is only now that we know the class is
6418 : : polymorphic. Although a polymorphic base will have a already
6419 : : been diagnosed during its definition, we warn on use too. */
6420 : 36143417 : if (TYPE_POLYMORPHIC_P (t) && warn_nonvdtor)
6421 : : {
6422 : 110 : tree binfo = TYPE_BINFO (t);
6423 : 110 : vec<tree, va_gc> *accesses = BINFO_BASE_ACCESSES (binfo);
6424 : 110 : tree base_binfo;
6425 : 110 : unsigned i;
6426 : :
6427 : 149 : for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
6428 : : {
6429 : 39 : tree basetype = TREE_TYPE (base_binfo);
6430 : :
6431 : 39 : if ((*accesses)[i] == access_public_node
6432 : 23 : && (TYPE_POLYMORPHIC_P (basetype) || warn_ecpp)
6433 : 58 : && accessible_nvdtor_p (basetype))
6434 : 12 : warning (OPT_Wnon_virtual_dtor,
6435 : : "base class %q#T has accessible non-virtual destructor",
6436 : : basetype);
6437 : : }
6438 : : }
6439 : :
6440 : : /* If the class has no user-declared constructor, but does have
6441 : : non-static const or reference data members that can never be
6442 : : initialized, issue a warning. */
6443 : 36143417 : if (warn_uninitialized
6444 : : /* Classes with user-declared constructors are presumed to
6445 : : initialize these members. */
6446 : 336141 : && !TYPE_HAS_USER_CONSTRUCTOR (t)
6447 : : /* Aggregates can be initialized with brace-enclosed
6448 : : initializers. */
6449 : 36418563 : && CLASSTYPE_NON_AGGREGATE (t))
6450 : : {
6451 : 37611 : tree field;
6452 : :
6453 : 236229 : for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
6454 : : {
6455 : 198618 : tree type;
6456 : :
6457 : 375091 : if (TREE_CODE (field) != FIELD_DECL
6458 : 198618 : || DECL_INITIAL (field) != NULL_TREE)
6459 : 176473 : continue;
6460 : :
6461 : 22145 : type = TREE_TYPE (field);
6462 : 22145 : if (TYPE_REF_P (type))
6463 : 7 : warning_at (DECL_SOURCE_LOCATION (field),
6464 : : OPT_Wuninitialized, "non-static reference %q#D "
6465 : : "in class without a constructor", field);
6466 : 22138 : else if (CP_TYPE_CONST_P (type)
6467 : 22138 : && (!CLASS_TYPE_P (type)
6468 : 4 : || !TYPE_HAS_DEFAULT_CONSTRUCTOR (type)))
6469 : 11 : warning_at (DECL_SOURCE_LOCATION (field),
6470 : : OPT_Wuninitialized, "non-static const member %q#D "
6471 : : "in class without a constructor", field);
6472 : : }
6473 : : }
6474 : :
6475 : : /* Synthesize any needed methods. */
6476 : 36143417 : add_implicitly_declared_members (t, &access_decls,
6477 : : cant_have_const_ctor,
6478 : : no_const_asn_ref);
6479 : :
6480 : : /* Check defaulted declarations here so we have cant_have_const_ctor
6481 : : and don't need to worry about clones. */
6482 : 250794713 : for (fn = TYPE_FIELDS (t); fn; fn = DECL_CHAIN (fn))
6483 : 125326392 : if (DECL_DECLARES_FUNCTION_P (fn)
6484 : 109440479 : && !DECL_ARTIFICIAL (fn)
6485 : 323218500 : && DECL_DEFAULTED_IN_CLASS_P (fn))
6486 : : {
6487 : : /* ...except handle comparisons later, in finish_struct_1. */
6488 : 5039372 : if (special_function_p (fn) == sfk_comparison)
6489 : 8702 : continue;
6490 : :
6491 : 5030670 : int copy = copy_fn_p (fn);
6492 : 5030670 : if (copy > 0)
6493 : : {
6494 : 1965030 : bool imp_const_p
6495 : 3930060 : = (DECL_CONSTRUCTOR_P (fn) ? !cant_have_const_ctor
6496 : 997768 : : !no_const_asn_ref);
6497 : 1965030 : bool fn_const_p = (copy == 2);
6498 : :
6499 : 1965030 : if (fn_const_p && !imp_const_p)
6500 : : /* If the function is defaulted outside the class, we just
6501 : : give the synthesis error. Core Issue #1331 says this is
6502 : : no longer ill-formed, it is defined as deleted instead. */
6503 : 18 : DECL_DELETED_FN (fn) = true;
6504 : : }
6505 : 5030670 : defaulted_late_check (fn);
6506 : : }
6507 : :
6508 : 71883957 : if (LAMBDA_TYPE_P (t))
6509 : : /* "This class type is not an aggregate." */
6510 : 236744 : CLASSTYPE_NON_AGGREGATE (t) = 1;
6511 : :
6512 : : /* Compute the 'literal type' property before we
6513 : : do anything with non-static member functions. */
6514 : 36143417 : finalize_literal_type_property (t);
6515 : :
6516 : : /* Create the in-charge and not-in-charge variants of constructors
6517 : : and destructors. */
6518 : 36143417 : clone_constructors_and_destructors (t);
6519 : :
6520 : : /* Process the using-declarations. */
6521 : 74065458 : for (; access_decls; access_decls = TREE_CHAIN (access_decls))
6522 : 1778624 : handle_using_decl (TREE_VALUE (access_decls), t);
6523 : :
6524 : : /* Figure out whether or not we will need a cookie when dynamically
6525 : : allocating an array of this type. */
6526 : 36143417 : LANG_TYPE_CLASS_CHECK (t)->vec_new_uses_cookie
6527 : 36143417 : = type_requires_array_cookie (t);
6528 : :
6529 : : /* Classes marked hot or cold propagate the attribute to all members. We
6530 : : may do this now that methods are declared. This does miss some lazily
6531 : : declared special member functions (CLASSTYPE_LAZY_*), which are handled
6532 : : in lazily_declare_fn later on. */
6533 : 36143417 : propagate_class_warmth_attribute (t);
6534 : 36143417 : }
6535 : :
6536 : : /* If T needs a pointer to its virtual function table, set TYPE_VFIELD
6537 : : accordingly. If a new vfield was created (because T doesn't have a
6538 : : primary base class), then the newly created field is returned. It
6539 : : is not added to the TYPE_FIELDS list; it is the caller's
6540 : : responsibility to do that. Accumulate declared virtual functions
6541 : : on VIRTUALS_P. */
6542 : :
6543 : : static tree
6544 : 36143417 : create_vtable_ptr (tree t, tree* virtuals_p)
6545 : : {
6546 : 36143417 : tree fn;
6547 : :
6548 : : /* Collect the virtual functions declared in T. */
6549 : 304727158 : for (fn = TYPE_FIELDS (t); fn; fn = DECL_CHAIN (fn))
6550 : 268583741 : if (TREE_CODE (fn) == FUNCTION_DECL
6551 : 129104245 : && DECL_VINDEX (fn) && !DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (fn)
6552 : 275871494 : && TREE_CODE (DECL_VINDEX (fn)) != INTEGER_CST)
6553 : : {
6554 : 7287753 : tree new_virtual = make_node (TREE_LIST);
6555 : :
6556 : 7287753 : BV_FN (new_virtual) = fn;
6557 : 7287753 : BV_DELTA (new_virtual) = integer_zero_node;
6558 : 7287753 : BV_VCALL_INDEX (new_virtual) = NULL_TREE;
6559 : :
6560 : 7287753 : TREE_CHAIN (new_virtual) = *virtuals_p;
6561 : 7287753 : *virtuals_p = new_virtual;
6562 : : }
6563 : :
6564 : : /* If we couldn't find an appropriate base class, create a new field
6565 : : here. Even if there weren't any new virtual functions, we might need a
6566 : : new virtual function table if we're supposed to include vptrs in
6567 : : all classes that need them. */
6568 : 36143417 : if (!TYPE_VFIELD (t) && (*virtuals_p || TYPE_CONTAINS_VPTR_P (t)))
6569 : : {
6570 : : /* We build this decl with vtbl_ptr_type_node, which is a
6571 : : `vtable_entry_type*'. It might seem more precise to use
6572 : : `vtable_entry_type (*)[N]' where N is the number of virtual
6573 : : functions. However, that would require the vtable pointer in
6574 : : base classes to have a different type than the vtable pointer
6575 : : in derived classes. We could make that happen, but that
6576 : : still wouldn't solve all the problems. In particular, the
6577 : : type-based alias analysis code would decide that assignments
6578 : : to the base class vtable pointer can't alias assignments to
6579 : : the derived class vtable pointer, since they have different
6580 : : types. Thus, in a derived class destructor, where the base
6581 : : class constructor was inlined, we could generate bad code for
6582 : : setting up the vtable pointer.
6583 : :
6584 : : Therefore, we use one type for all vtable pointers. We still
6585 : : use a type-correct type; it's just doesn't indicate the array
6586 : : bounds. That's better than using `void*' or some such; it's
6587 : : cleaner, and it let's the alias analysis code know that these
6588 : : stores cannot alias stores to void*! */
6589 : 232386 : tree field;
6590 : :
6591 : 232386 : field = build_decl (input_location,
6592 : : FIELD_DECL, get_vfield_name (t), vtbl_ptr_type_node);
6593 : 232386 : DECL_VIRTUAL_P (field) = 1;
6594 : 232386 : DECL_ARTIFICIAL (field) = 1;
6595 : 232386 : DECL_FIELD_CONTEXT (field) = t;
6596 : 232386 : DECL_FCONTEXT (field) = t;
6597 : 232386 : if (TYPE_PACKED (t))
6598 : 4 : DECL_PACKED (field) = 1;
6599 : :
6600 : 232386 : TYPE_VFIELD (t) = field;
6601 : :
6602 : : /* This class is non-empty. */
6603 : 232386 : CLASSTYPE_EMPTY_P (t) = 0;
6604 : :
6605 : 232386 : return field;
6606 : : }
6607 : :
6608 : : return NULL_TREE;
6609 : : }
6610 : :
6611 : : /* Add OFFSET to all base types of BINFO which is a base in the
6612 : : hierarchy dominated by T.
6613 : :
6614 : : OFFSET, which is a type offset, is number of bytes. */
6615 : :
6616 : : static void
6617 : 22906393 : propagate_binfo_offsets (tree binfo, tree offset)
6618 : : {
6619 : 22906393 : int i;
6620 : 22906393 : tree primary_binfo;
6621 : 22906393 : tree base_binfo;
6622 : :
6623 : : /* Update BINFO's offset. */
6624 : 22906393 : BINFO_OFFSET (binfo)
6625 : 22906393 : = fold_convert (sizetype,
6626 : : size_binop (PLUS_EXPR,
6627 : : fold_convert (ssizetype, BINFO_OFFSET (binfo)),
6628 : : offset));
6629 : :
6630 : : /* Find the primary base class. */
6631 : 22906393 : primary_binfo = get_primary_binfo (binfo);
6632 : :
6633 : 23910332 : if (primary_binfo && BINFO_INHERITANCE_CHAIN (primary_binfo) == binfo)
6634 : 1002724 : propagate_binfo_offsets (primary_binfo, offset);
6635 : :
6636 : : /* Scan all of the bases, pushing the BINFO_OFFSET adjust
6637 : : downwards. */
6638 : 26765347 : for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
6639 : : {
6640 : : /* Don't do the primary base twice. */
6641 : 4862799 : if (base_binfo == primary_binfo)
6642 : 1003845 : continue;
6643 : :
6644 : 2855109 : if (BINFO_VIRTUAL_P (base_binfo))
6645 : 217489 : continue;
6646 : :
6647 : 2637620 : propagate_binfo_offsets (base_binfo, offset);
6648 : : }
6649 : 22906393 : }
6650 : :
6651 : : /* Set BINFO_OFFSET for all of the virtual bases for RLI->T. Update
6652 : : TYPE_ALIGN and TYPE_SIZE for T. OFFSETS gives the location of
6653 : : empty subobjects of T. */
6654 : :
6655 : : static void
6656 : 36143417 : layout_virtual_bases (record_layout_info rli, splay_tree offsets)
6657 : : {
6658 : 36143417 : tree vbase;
6659 : 36143417 : tree t = rli->t;
6660 : 36143417 : tree *next_field;
6661 : :
6662 : 36143417 : if (BINFO_N_BASE_BINFOS (TYPE_BINFO (t)) == 0)
6663 : : return;
6664 : :
6665 : : /* Find the last field. The artificial fields created for virtual
6666 : : bases will go after the last extant field to date. */
6667 : 18506784 : next_field = &TYPE_FIELDS (t);
6668 : 148890859 : while (*next_field)
6669 : 130384075 : next_field = &DECL_CHAIN (*next_field);
6670 : :
6671 : : /* Go through the virtual bases, allocating space for each virtual
6672 : : base that is not already a primary base class. These are
6673 : : allocated in inheritance graph order. */
6674 : 59910213 : for (vbase = TYPE_BINFO (t); vbase; vbase = TREE_CHAIN (vbase))
6675 : : {
6676 : 41403429 : if (!BINFO_VIRTUAL_P (vbase))
6677 : 41207087 : continue;
6678 : :
6679 : 196342 : if (!BINFO_PRIMARY_P (vbase))
6680 : : {
6681 : : /* This virtual base is not a primary base of any class in the
6682 : : hierarchy, so we have to add space for it. */
6683 : 187765 : next_field = build_base_field (rli, vbase,
6684 : : access_private_node,
6685 : : offsets, next_field);
6686 : : }
6687 : : }
6688 : : }
6689 : :
6690 : : /* Returns the offset of the byte just past the end of the base class
6691 : : BINFO. */
6692 : :
6693 : : static tree
6694 : 38336407 : end_of_base (tree binfo)
6695 : : {
6696 : 38336407 : tree size;
6697 : :
6698 : 38336407 : if (!CLASSTYPE_AS_BASE (BINFO_TYPE (binfo)))
6699 : 0 : size = TYPE_SIZE_UNIT (char_type_node);
6700 : 38336407 : else if (is_empty_class (BINFO_TYPE (binfo)))
6701 : : /* An empty class has zero CLASSTYPE_SIZE_UNIT, but we need to
6702 : : allocate some space for it. It cannot have virtual bases, so
6703 : : TYPE_SIZE_UNIT is fine. */
6704 : 34037520 : size = TYPE_SIZE_UNIT (BINFO_TYPE (binfo));
6705 : : else
6706 : 4298887 : size = CLASSTYPE_SIZE_UNIT (BINFO_TYPE (binfo));
6707 : :
6708 : 38336407 : return size_binop (PLUS_EXPR, BINFO_OFFSET (binfo), size);
6709 : : }
6710 : :
6711 : : /* Returns one of three variations of the ending offset of T. If MODE is
6712 : : eoc_nvsize, the result is the ABI "nvsize" (i.e. sizeof before allocating
6713 : : vbases). If MODE is eoc_vsize, the result is the sizeof after allocating
6714 : : vbases but before rounding, which is not named in the ABI. If MODE is
6715 : : eoc_nv_or_dsize, the result is the greater of "nvsize" and "dsize" (the size
6716 : : of the actual data in the class, kinda), as used for allocation of
6717 : : potentially-overlapping fields. */
6718 : :
6719 : : enum eoc_mode { eoc_nvsize, eoc_vsize, eoc_nv_or_dsize };
6720 : : static tree
6721 : 70765548 : end_of_class (tree t, eoc_mode mode)
6722 : : {
6723 : 70765548 : tree result = size_zero_node;
6724 : 70765548 : vec<tree, va_gc> *vbases;
6725 : 70765548 : tree binfo;
6726 : 70765548 : tree base_binfo;
6727 : 70765548 : tree offset;
6728 : 70765548 : int i;
6729 : :
6730 : 70765548 : for (binfo = TYPE_BINFO (t), i = 0;
6731 : 108996406 : BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
6732 : : {
6733 : 38230858 : if (BINFO_VIRTUAL_P (base_binfo)
6734 : 38230858 : && (!BINFO_PRIMARY_P (base_binfo)
6735 : 3944 : || BINFO_INHERITANCE_CHAIN (base_binfo) != TYPE_BINFO (t)))
6736 : 90793 : continue;
6737 : :
6738 : 38140065 : offset = end_of_base (base_binfo);
6739 : 38140065 : if (tree_int_cst_lt (result, offset))
6740 : 38230858 : result = offset;
6741 : : }
6742 : :
6743 : 638685175 : for (tree field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
6744 : 567919627 : if (TREE_CODE (field) == FIELD_DECL
6745 : 567919627 : && !DECL_FIELD_IS_BASE (field))
6746 : : {
6747 : 23174102 : tree size = DECL_SIZE_UNIT (field);
6748 : 23174102 : if (!size)
6749 : : /* DECL_SIZE_UNIT can be null for a flexible array. */
6750 : 2188 : continue;
6751 : :
6752 : 23171914 : if (is_empty_field (field))
6753 : : /* For empty fields DECL_SIZE_UNIT is 0, but we want the
6754 : : size of the type (usually 1) for computing nvsize. */
6755 : 78148 : size = TYPE_SIZE_UNIT (TREE_TYPE (field));
6756 : :
6757 : 23171914 : if (DECL_BIT_FIELD_TYPE (field))
6758 : : {
6759 : 431023 : offset = size_binop (PLUS_EXPR, bit_position (field),
6760 : : DECL_SIZE (field));
6761 : 431023 : offset = size_binop (CEIL_DIV_EXPR, offset, bitsize_unit_node);
6762 : 431023 : offset = fold_convert (sizetype, offset);
6763 : : }
6764 : : else
6765 : 22740891 : offset = size_binop (PLUS_EXPR, byte_position (field), size);
6766 : 23171914 : if (tree_int_cst_lt (result, offset))
6767 : 567919627 : result = offset;
6768 : : }
6769 : :
6770 : 70765548 : if (mode != eoc_nvsize)
6771 : 36357581 : for (vbases = CLASSTYPE_VBASECLASSES (t), i = 0;
6772 : 36357581 : vec_safe_iterate (vbases, i, &base_binfo); i++)
6773 : : {
6774 : 196349 : if (mode == eoc_nv_or_dsize)
6775 : : /* For dsize, don't count trailing empty bases. */
6776 : 7 : offset = size_binop (PLUS_EXPR, BINFO_OFFSET (base_binfo),
6777 : : CLASSTYPE_SIZE_UNIT (BINFO_TYPE (base_binfo)));
6778 : : else
6779 : 196342 : offset = end_of_base (base_binfo);
6780 : 196349 : if (tree_int_cst_lt (result, offset))
6781 : 185130 : result = offset;
6782 : : }
6783 : :
6784 : 70765548 : return result;
6785 : : }
6786 : :
6787 : : /* Warn as appropriate about the change in whether we pack into the tail
6788 : : padding of FIELD, a base field which has a C++14 aggregate type with default
6789 : : member initializers. */
6790 : :
6791 : : static void
6792 : 287765293 : check_non_pod_aggregate (tree field)
6793 : : {
6794 : 861799382 : if (!abi_version_crosses (17) || cxx_dialect < cxx14)
6795 : : return;
6796 : 632112 : if (TREE_CODE (field) != FIELD_DECL
6797 : 632112 : || (!DECL_FIELD_IS_BASE (field)
6798 : 39587 : && !field_poverlapping_p (field)))
6799 : 588104 : return;
6800 : 44008 : tree next = DECL_CHAIN (field);
6801 : 185087 : while (next && TREE_CODE (next) != FIELD_DECL) next = DECL_CHAIN (next);
6802 : 44008 : if (!next)
6803 : : return;
6804 : 3388 : tree type = TREE_TYPE (field);
6805 : 3388 : if (TYPE_IDENTIFIER (type) == as_base_identifier)
6806 : 1557 : type = TYPE_CONTEXT (type);
6807 : 3388 : if (!CLASS_TYPE_P (type) || !CLASSTYPE_NON_POD_AGGREGATE (type))
6808 : : return;
6809 : 24 : tree size = end_of_class (type, (DECL_FIELD_IS_BASE (field)
6810 : : ? eoc_nvsize : eoc_nv_or_dsize));
6811 : 22 : tree rounded = round_up_loc (input_location, size, DECL_ALIGN_UNIT (next));
6812 : 22 : if (tree_int_cst_lt (rounded, TYPE_SIZE_UNIT (type)))
6813 : : {
6814 : 16 : location_t loc = DECL_SOURCE_LOCATION (next);
6815 : 16 : if (DECL_FIELD_IS_BASE (next))
6816 : 8 : warning_at (loc, OPT_Wabi,"offset of %qT base class for "
6817 : : "%<-std=c++14%> and up changes in "
6818 : 8 : "%<-fabi-version=17%> (GCC 12)", TREE_TYPE (next));
6819 : : else
6820 : 8 : warning_at (loc, OPT_Wabi, "offset of %qD for "
6821 : : "%<-std=c++14%> and up changes in "
6822 : : "%<-fabi-version=17%> (GCC 12)", next);
6823 : : }
6824 : : }
6825 : :
6826 : : /* Warn about bases of T that are inaccessible because they are
6827 : : ambiguous. For example:
6828 : :
6829 : : struct S {};
6830 : : struct T : public S {};
6831 : : struct U : public S, public T {};
6832 : :
6833 : : Here, `(S*) new U' is not allowed because there are two `S'
6834 : : subobjects of U. */
6835 : :
6836 : : static void
6837 : 36143417 : maybe_warn_about_inaccessible_bases (tree t)
6838 : : {
6839 : 36143417 : int i;
6840 : 36143417 : vec<tree, va_gc> *vbases;
6841 : 36143417 : tree basetype;
6842 : 36143417 : tree binfo;
6843 : 36143417 : tree base_binfo;
6844 : :
6845 : : /* If not checking for warning then return early. */
6846 : 36143417 : if (!warn_inaccessible_base)
6847 : 36143417 : return;
6848 : :
6849 : : /* If there are no repeated bases, nothing can be ambiguous. */
6850 : 36143283 : if (!CLASSTYPE_REPEATED_BASE_P (t))
6851 : : return;
6852 : :
6853 : : /* Check direct bases. */
6854 : 4423 : for (binfo = TYPE_BINFO (t), i = 0;
6855 : 4423 : BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
6856 : : {
6857 : 3211 : basetype = BINFO_TYPE (base_binfo);
6858 : :
6859 : 3211 : if (!uniquely_derived_from_p (basetype, t))
6860 : 963 : warning (OPT_Winaccessible_base, "direct base %qT inaccessible "
6861 : : "in %qT due to ambiguity", basetype, t);
6862 : : }
6863 : :
6864 : : /* Check for ambiguous virtual bases. */
6865 : 1212 : if (extra_warnings)
6866 : 8 : for (vbases = CLASSTYPE_VBASECLASSES (t), i = 0;
6867 : 8 : vec_safe_iterate (vbases, i, &binfo); i++)
6868 : : {
6869 : 4 : basetype = BINFO_TYPE (binfo);
6870 : :
6871 : 4 : if (!uniquely_derived_from_p (basetype, t))
6872 : 4 : warning (OPT_Winaccessible_base, "virtual base %qT inaccessible in "
6873 : : "%qT due to ambiguity", basetype, t);
6874 : : }
6875 : : }
6876 : :
6877 : : /* Compare two INTEGER_CSTs K1 and K2. */
6878 : :
6879 : : static int
6880 : 8628846 : splay_tree_compare_integer_csts (splay_tree_key k1, splay_tree_key k2)
6881 : : {
6882 : 8628846 : return tree_int_cst_compare ((tree) k1, (tree) k2);
6883 : : }
6884 : :
6885 : : /* Increase the size indicated in RLI to account for empty classes
6886 : : that are "off the end" of the class. */
6887 : :
6888 : : static void
6889 : 36143417 : include_empty_classes (record_layout_info rli)
6890 : : {
6891 : 36143417 : tree eoc;
6892 : 36143417 : tree rli_size;
6893 : :
6894 : : /* It might be the case that we grew the class to allocate a
6895 : : zero-sized base class. That won't be reflected in RLI, yet,
6896 : : because we are willing to overlay multiple bases at the same
6897 : : offset. However, now we need to make sure that RLI is big enough
6898 : : to reflect the entire class. */
6899 : 36143417 : eoc = end_of_class (rli->t, eoc_vsize);
6900 : 36143417 : rli_size = rli_size_unit_so_far (rli);
6901 : 36143417 : if (TREE_CODE (rli_size) == INTEGER_CST
6902 : 36143417 : && tree_int_cst_lt (rli_size, eoc))
6903 : : {
6904 : : /* The size should have been rounded to a whole byte. */
6905 : 16142472 : gcc_assert (tree_int_cst_equal
6906 : : (rli->bitpos, round_down (rli->bitpos, BITS_PER_UNIT)));
6907 : 16142472 : rli->bitpos
6908 : 16142472 : = size_binop (PLUS_EXPR,
6909 : : rli->bitpos,
6910 : : size_binop (MULT_EXPR,
6911 : : fold_convert (bitsizetype,
6912 : : size_binop (MINUS_EXPR,
6913 : : eoc, rli_size)),
6914 : : bitsize_int (BITS_PER_UNIT)));
6915 : 16142472 : normalize_rli (rli);
6916 : : }
6917 : 36143417 : }
6918 : :
6919 : : /* Calculate the TYPE_SIZE, TYPE_ALIGN, etc for T. Calculate
6920 : : BINFO_OFFSETs for all of the base-classes. Position the vtable
6921 : : pointer. Accumulate declared virtual functions on VIRTUALS_P. */
6922 : :
6923 : : static void
6924 : 36143417 : layout_class_type (tree t, tree *virtuals_p)
6925 : : {
6926 : 36143417 : tree non_static_data_members;
6927 : 36143417 : tree field;
6928 : 36143417 : tree vptr;
6929 : 36143417 : record_layout_info rli;
6930 : : /* Maps offsets (represented as INTEGER_CSTs) to a TREE_LIST of
6931 : : types that appear at that offset. */
6932 : 36143417 : splay_tree empty_base_offsets;
6933 : : /* True if the last field laid out was a bit-field. */
6934 : 36143417 : bool last_field_was_bitfield = false;
6935 : : /* The location at which the next field should be inserted. */
6936 : 36143417 : tree *next_field;
6937 : :
6938 : : /* Keep track of the first non-static data member. */
6939 : 36143417 : non_static_data_members = TYPE_FIELDS (t);
6940 : :
6941 : : /* Start laying out the record. */
6942 : 36143417 : rli = start_record_layout (t);
6943 : :
6944 : : /* Mark all the primary bases in the hierarchy. */
6945 : 36143417 : determine_primary_bases (t);
6946 : :
6947 : : /* Create a pointer to our virtual function table. */
6948 : 36143417 : vptr = create_vtable_ptr (t, virtuals_p);
6949 : :
6950 : : /* The vptr is always the first thing in the class. */
6951 : 36143417 : if (vptr)
6952 : : {
6953 : 232386 : DECL_CHAIN (vptr) = TYPE_FIELDS (t);
6954 : 232386 : TYPE_FIELDS (t) = vptr;
6955 : 232386 : next_field = &DECL_CHAIN (vptr);
6956 : 232386 : place_field (rli, vptr);
6957 : : }
6958 : : else
6959 : 35911031 : next_field = &TYPE_FIELDS (t);
6960 : :
6961 : : /* Build FIELD_DECLs for all of the non-virtual base-types. */
6962 : 36143417 : empty_base_offsets = splay_tree_new (splay_tree_compare_integer_csts,
6963 : : NULL, NULL);
6964 : 36143417 : build_base_fields (rli, empty_base_offsets, next_field);
6965 : :
6966 : : /* Layout the non-static data members. */
6967 : 304727158 : for (field = non_static_data_members; field; field = DECL_CHAIN (field))
6968 : : {
6969 : 268583741 : tree type;
6970 : 268583741 : tree padding;
6971 : :
6972 : : /* We still pass things that aren't non-static data members to
6973 : : the back end, in case it wants to do something with them. */
6974 : 268583741 : if (TREE_CODE (field) != FIELD_DECL)
6975 : : {
6976 : 254248926 : place_field (rli, field);
6977 : : /* If the static data member has incomplete type, keep track
6978 : : of it so that it can be completed later. (The handling
6979 : : of pending statics in finish_record_layout is
6980 : : insufficient; consider:
6981 : :
6982 : : struct S1;
6983 : : struct S2 { static S1 s1; };
6984 : :
6985 : : At this point, finish_record_layout will be called, but
6986 : : S1 is still incomplete.) */
6987 : 254248926 : if (VAR_P (field))
6988 : : {
6989 : 9483582 : maybe_register_incomplete_var (field);
6990 : : /* The visibility of static data members is determined
6991 : : at their point of declaration, not their point of
6992 : : definition. */
6993 : 9483582 : determine_visibility (field);
6994 : : }
6995 : 254248926 : continue;
6996 : : }
6997 : :
6998 : 14334815 : type = TREE_TYPE (field);
6999 : 14334815 : if (type == error_mark_node)
7000 : 221 : continue;
7001 : :
7002 : 14334594 : padding = NULL_TREE;
7003 : :
7004 : 14334594 : bool might_overlap = field_poverlapping_p (field);
7005 : :
7006 : 67733 : if (might_overlap && CLASS_TYPE_P (type)
7007 : 14391531 : && (CLASSTYPE_NON_LAYOUT_POD_P (type) || CLASSTYPE_EMPTY_P (type)))
7008 : : {
7009 : : /* if D is a potentially-overlapping data member, update sizeof(C) to
7010 : : max (sizeof(C), offset(D)+max (nvsize(D), dsize(D))). */
7011 : 56873 : if (CLASSTYPE_EMPTY_P (type))
7012 : 39060 : DECL_SIZE (field) = DECL_SIZE_UNIT (field) = size_zero_node;
7013 : : else
7014 : : {
7015 : 17813 : tree size = end_of_class (type, eoc_nv_or_dsize);
7016 : 17813 : DECL_SIZE_UNIT (field) = size;
7017 : 17813 : DECL_SIZE (field) = bit_from_pos (size, bitsize_zero_node);
7018 : : }
7019 : : }
7020 : :
7021 : : /* If this field is a bit-field whose width is greater than its
7022 : : type, then there are some special rules for allocating
7023 : : it. */
7024 : 14334594 : if (DECL_C_BIT_FIELD (field)
7025 : 14334594 : && tree_int_cst_lt (TYPE_SIZE (type), DECL_SIZE (field)))
7026 : : {
7027 : 784 : bool was_unnamed_p = false;
7028 : : /* We must allocate the bits as if suitably aligned for the
7029 : : longest integer type that fits in this many bits. Then,
7030 : : we are supposed to use the left over bits as additional
7031 : : padding. */
7032 : :
7033 : : /* Do not pick a type bigger than MAX_FIXED_MODE_SIZE. */
7034 : 1568 : tree limit = size_int (MAX_FIXED_MODE_SIZE);
7035 : 784 : if (tree_int_cst_lt (DECL_SIZE (field), limit))
7036 : 757 : limit = DECL_SIZE (field);
7037 : :
7038 : 784 : tree integer_type = integer_types[itk_char];
7039 : 6770 : for (unsigned itk = itk_char; itk != itk_none; itk++)
7040 : 6743 : if (tree next = integer_types[itk])
7041 : : {
7042 : 6581 : if (tree_int_cst_lt (limit, TYPE_SIZE (next)))
7043 : : /* Too big, so our current guess is what we want. */
7044 : : break;
7045 : : /* Not bigger than limit, ok */
7046 : : integer_type = next;
7047 : : }
7048 : :
7049 : : /* Figure out how much additional padding is required. */
7050 : 784 : if (TREE_CODE (t) == UNION_TYPE)
7051 : : /* In a union, the padding field must have the full width
7052 : : of the bit-field; all fields start at offset zero. */
7053 : 79 : padding = DECL_SIZE (field);
7054 : : else
7055 : 705 : padding = size_binop (MINUS_EXPR, DECL_SIZE (field),
7056 : : TYPE_SIZE (integer_type));
7057 : :
7058 : 784 : if (integer_zerop (padding))
7059 : 7 : padding = NULL_TREE;
7060 : :
7061 : : /* An unnamed bitfield does not normally affect the
7062 : : alignment of the containing class on a target where
7063 : : PCC_BITFIELD_TYPE_MATTERS. But, the C++ ABI does not
7064 : : make any exceptions for unnamed bitfields when the
7065 : : bitfields are longer than their types. Therefore, we
7066 : : temporarily give the field a name. */
7067 : 784 : if (PCC_BITFIELD_TYPE_MATTERS && !DECL_NAME (field))
7068 : : {
7069 : 0 : was_unnamed_p = true;
7070 : 0 : DECL_NAME (field) = make_anon_name ();
7071 : : }
7072 : :
7073 : 784 : DECL_SIZE (field) = TYPE_SIZE (integer_type);
7074 : 784 : SET_DECL_ALIGN (field, TYPE_ALIGN (integer_type));
7075 : 784 : DECL_USER_ALIGN (field) = TYPE_USER_ALIGN (integer_type);
7076 : 784 : layout_nonempty_base_or_field (rli, field, NULL_TREE,
7077 : : empty_base_offsets);
7078 : 784 : if (was_unnamed_p)
7079 : 0 : DECL_NAME (field) = NULL_TREE;
7080 : : /* Now that layout has been performed, set the size of the
7081 : : field to the size of its declared type; the rest of the
7082 : : field is effectively invisible. */
7083 : 784 : DECL_SIZE (field) = TYPE_SIZE (type);
7084 : : /* We must also reset the DECL_MODE of the field. */
7085 : 784 : SET_DECL_MODE (field, TYPE_MODE (type));
7086 : : }
7087 : 14333810 : else if (might_overlap && is_empty_class (type))
7088 : : {
7089 : 39060 : SET_DECL_FIELD_ABI_IGNORED (field, 1);
7090 : 39060 : layout_empty_base_or_field (rli, field, empty_base_offsets);
7091 : : }
7092 : : else
7093 : 14294750 : layout_nonempty_base_or_field (rli, field, NULL_TREE,
7094 : : empty_base_offsets);
7095 : :
7096 : : /* Remember the location of any empty classes in FIELD. */
7097 : 14334594 : record_subobject_offsets (field, empty_base_offsets);
7098 : :
7099 : : /* If a bit-field does not immediately follow another bit-field,
7100 : : and yet it starts in the middle of a byte, we have failed to
7101 : : comply with the ABI. */
7102 : 14334594 : if (warn_abi
7103 : 104798 : && DECL_C_BIT_FIELD (field)
7104 : : /* The TREE_NO_WARNING flag gets set by Objective-C when
7105 : : laying out an Objective-C class. The ObjC ABI differs
7106 : : from the C++ ABI, and so we do not want a warning
7107 : : here. */
7108 : 3988 : && !warning_suppressed_p (field, OPT_Wabi)
7109 : 3988 : && !last_field_was_bitfield
7110 : 14335002 : && !integer_zerop (size_binop (TRUNC_MOD_EXPR,
7111 : : DECL_FIELD_BIT_OFFSET (field),
7112 : : bitsize_unit_node)))
7113 : 0 : warning_at (DECL_SOURCE_LOCATION (field), OPT_Wabi,
7114 : : "offset of %qD is not ABI-compliant and may "
7115 : : "change in a future version of GCC", field);
7116 : :
7117 : : /* The middle end uses the type of expressions to determine the
7118 : : possible range of expression values. In order to optimize
7119 : : "x.i > 7" to "false" for a 2-bit bitfield "i", the middle end
7120 : : must be made aware of the width of "i", via its type.
7121 : :
7122 : : Because C++ does not have integer types of arbitrary width,
7123 : : we must (for the purposes of the front end) convert from the
7124 : : type assigned here to the declared type of the bitfield
7125 : : whenever a bitfield expression is used as an rvalue.
7126 : : Similarly, when assigning a value to a bitfield, the value
7127 : : must be converted to the type given the bitfield here. */
7128 : 14334594 : if (DECL_C_BIT_FIELD (field))
7129 : : {
7130 : 367772 : unsigned HOST_WIDE_INT width;
7131 : 367772 : tree ftype = TREE_TYPE (field);
7132 : 367772 : width = tree_to_uhwi (DECL_SIZE (field));
7133 : 367772 : if (width != TYPE_PRECISION (ftype))
7134 : : {
7135 : 232876 : TREE_TYPE (field)
7136 : 465752 : = c_build_bitfield_integer_type (width,
7137 : 232876 : TYPE_UNSIGNED (ftype));
7138 : 232876 : TREE_TYPE (field)
7139 : 465752 : = cp_build_qualified_type (TREE_TYPE (field),
7140 : : cp_type_quals (ftype));
7141 : : }
7142 : : }
7143 : :
7144 : : /* If we needed additional padding after this field, add it
7145 : : now. */
7146 : 14334594 : if (padding)
7147 : : {
7148 : 777 : tree padding_field;
7149 : :
7150 : 777 : padding_field = build_decl (input_location,
7151 : : FIELD_DECL,
7152 : : NULL_TREE,
7153 : : char_type_node);
7154 : 777 : DECL_BIT_FIELD (padding_field) = 1;
7155 : 777 : DECL_SIZE (padding_field) = padding;
7156 : 777 : DECL_CONTEXT (padding_field) = t;
7157 : 777 : DECL_ARTIFICIAL (padding_field) = 1;
7158 : 777 : DECL_IGNORED_P (padding_field) = 1;
7159 : 777 : DECL_PADDING_P (padding_field) = 1;
7160 : 777 : layout_nonempty_base_or_field (rli, padding_field,
7161 : : NULL_TREE,
7162 : : empty_base_offsets);
7163 : : }
7164 : :
7165 : 14334594 : last_field_was_bitfield = DECL_C_BIT_FIELD (field);
7166 : : }
7167 : :
7168 : 36143417 : if (!integer_zerop (rli->bitpos))
7169 : : {
7170 : : /* Make sure that we are on a byte boundary so that the size of
7171 : : the class without virtual bases will always be a round number
7172 : : of bytes. */
7173 : 4180233 : rli->bitpos = round_up_loc (input_location, rli->bitpos, BITS_PER_UNIT);
7174 : 4180233 : normalize_rli (rli);
7175 : : }
7176 : :
7177 : : /* We used to remove zero width bitfields at this point since PR42217,
7178 : : while the C FE never did that. That caused ABI differences on various
7179 : : targets. Set the DECL_FIELD_CXX_ZERO_WIDTH_BIT_FIELD flag on them
7180 : : instead, so that the backends can emit -Wpsabi warnings in the cases
7181 : : where the ABI changed. */
7182 : 323908710 : for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
7183 : : {
7184 : 287765293 : if (TREE_CODE (field) == FIELD_DECL
7185 : 33516367 : && DECL_C_BIT_FIELD (field)
7186 : : /* We should not be confused by the fact that grokbitfield
7187 : : temporarily sets the width of the bit field into
7188 : : DECL_BIT_FIELD_REPRESENTATIVE (field).
7189 : : check_bitfield_decl eventually sets DECL_SIZE (field)
7190 : : to that width. */
7191 : 367775 : && (DECL_SIZE (field) == NULL_TREE
7192 : 367772 : || integer_zerop (DECL_SIZE (field)))
7193 : 287766383 : && TREE_TYPE (field) != error_mark_node)
7194 : 1087 : SET_DECL_FIELD_CXX_ZERO_WIDTH_BIT_FIELD (field, 1);
7195 : 287765293 : check_non_pod_aggregate (field);
7196 : : }
7197 : :
7198 : 36143417 : if (CLASSTYPE_NON_LAYOUT_POD_P (t) || CLASSTYPE_EMPTY_P (t))
7199 : : {
7200 : : /* T needs a different layout as a base (eliding virtual bases
7201 : : or whatever). Create that version. */
7202 : 34604296 : tree base_t = make_node (TREE_CODE (t));
7203 : 34604296 : tree base_d = create_implicit_typedef (as_base_identifier, base_t);
7204 : :
7205 : 34604296 : TYPE_CONTEXT (base_t) = t;
7206 : 34604296 : DECL_CONTEXT (base_d) = t;
7207 : :
7208 : 34604296 : set_instantiating_module (base_d);
7209 : :
7210 : : /* If the ABI version is not at least two, and the last
7211 : : field was a bit-field, RLI may not be on a byte
7212 : : boundary. In particular, rli_size_unit_so_far might
7213 : : indicate the last complete byte, while rli_size_so_far
7214 : : indicates the total number of bits used. Therefore,
7215 : : rli_size_so_far, rather than rli_size_unit_so_far, is
7216 : : used to compute TYPE_SIZE_UNIT. */
7217 : :
7218 : : /* Set the size and alignment for the new type. */
7219 : 34604296 : tree eoc = end_of_class (t, eoc_nvsize);
7220 : 34604296 : TYPE_SIZE_UNIT (base_t)
7221 : 34604296 : = size_binop (MAX_EXPR,
7222 : : fold_convert (sizetype,
7223 : : size_binop (CEIL_DIV_EXPR,
7224 : : rli_size_so_far (rli),
7225 : : bitsize_int (BITS_PER_UNIT))),
7226 : : eoc);
7227 : 34604296 : TYPE_SIZE (base_t)
7228 : 34604296 : = size_binop (MAX_EXPR,
7229 : : rli_size_so_far (rli),
7230 : : size_binop (MULT_EXPR,
7231 : : fold_convert (bitsizetype, eoc),
7232 : : bitsize_int (BITS_PER_UNIT)));
7233 : 34604296 : SET_TYPE_ALIGN (base_t, rli->record_align);
7234 : 34604296 : TYPE_USER_ALIGN (base_t) = TYPE_USER_ALIGN (t);
7235 : 34604296 : TYPE_TYPELESS_STORAGE (base_t) = TYPE_TYPELESS_STORAGE (t);
7236 : 34604296 : TYPE_CXX_ODR_P (base_t) = TYPE_CXX_ODR_P (t);
7237 : :
7238 : : /* Copy the non-static data members of T. This will include its
7239 : : direct non-virtual bases & vtable. */
7240 : 34604296 : next_field = &TYPE_FIELDS (base_t);
7241 : 314064529 : for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
7242 : 279460233 : if (TREE_CODE (field) == FIELD_DECL)
7243 : : {
7244 : 27538568 : *next_field = copy_node (field);
7245 : : /* Zap any NSDMI, it's not needed and might be a deferred
7246 : : parse. */
7247 : 27538568 : DECL_INITIAL (*next_field) = NULL_TREE;
7248 : 27538568 : DECL_CONTEXT (*next_field) = base_t;
7249 : 27538568 : next_field = &DECL_CHAIN (*next_field);
7250 : : }
7251 : 34604296 : *next_field = NULL_TREE;
7252 : :
7253 : : /* We use the base type for trivial assignments, and hence it
7254 : : needs a mode. */
7255 : 34604296 : compute_record_mode (base_t);
7256 : :
7257 : : /* Record the base version of the type. */
7258 : 34604296 : CLASSTYPE_AS_BASE (t) = base_t;
7259 : : }
7260 : : else
7261 : 1539121 : CLASSTYPE_AS_BASE (t) = t;
7262 : :
7263 : : /* Every empty class contains an empty class. */
7264 : 36143417 : if (CLASSTYPE_EMPTY_P (t))
7265 : 29328915 : CLASSTYPE_CONTAINS_EMPTY_CLASS_P (t) = 1;
7266 : :
7267 : : /* Set the TYPE_DECL for this type to contain the right
7268 : : value for DECL_OFFSET, so that we can use it as part
7269 : : of a COMPONENT_REF for multiple inheritance. */
7270 : 36143417 : layout_decl (TYPE_MAIN_DECL (t), 0);
7271 : :
7272 : : /* Now fix up any virtual base class types that we left lying
7273 : : around. We must get these done before we try to lay out the
7274 : : virtual function table. As a side-effect, this will remove the
7275 : : base subobject fields. */
7276 : 36143417 : layout_virtual_bases (rli, empty_base_offsets);
7277 : :
7278 : : /* Make sure that empty classes are reflected in RLI at this
7279 : : point. */
7280 : 36143417 : include_empty_classes (rli);
7281 : :
7282 : : /* Make sure not to create any structures with zero size. */
7283 : 36143417 : if (integer_zerop (rli_size_unit_so_far (rli)) && CLASSTYPE_EMPTY_P (t))
7284 : 13186695 : place_field (rli,
7285 : : build_decl (input_location,
7286 : : FIELD_DECL, NULL_TREE, char_type_node));
7287 : :
7288 : : /* If this is a non-POD, declaring it packed makes a difference to how it
7289 : : can be used as a field; don't let finalize_record_size undo it. */
7290 : 36143417 : if (TYPE_PACKED (t) && !layout_pod_type_p (t))
7291 : 79 : rli->packed_maybe_necessary = true;
7292 : :
7293 : : /* Let the back end lay out the type. */
7294 : 36143417 : finish_record_layout (rli, /*free_p=*/true);
7295 : :
7296 : : /* If we didn't end up needing an as-base type, don't use it. */
7297 : 36143417 : if (CLASSTYPE_AS_BASE (t) != t
7298 : : /* If T's CLASSTYPE_AS_BASE is TYPE_USER_ALIGN, but T is not,
7299 : : replacing the as-base type would change CLASSTYPE_USER_ALIGN,
7300 : : causing us to lose the user-specified alignment as in PR94050. */
7301 : 34604296 : && TYPE_USER_ALIGN (t) == TYPE_USER_ALIGN (CLASSTYPE_AS_BASE (t))
7302 : 70747699 : && tree_int_cst_equal (TYPE_SIZE (t),
7303 : 34604282 : TYPE_SIZE (CLASSTYPE_AS_BASE (t))))
7304 : 20601681 : CLASSTYPE_AS_BASE (t) = t;
7305 : :
7306 : 36143417 : if (TYPE_SIZE_UNIT (t)
7307 : 36143417 : && TREE_CODE (TYPE_SIZE_UNIT (t)) == INTEGER_CST
7308 : 36143417 : && !TREE_OVERFLOW (TYPE_SIZE_UNIT (t))
7309 : 72286822 : && !valid_constant_size_p (TYPE_SIZE_UNIT (t)))
7310 : 0 : error ("size of type %qT is too large (%qE bytes)", t, TYPE_SIZE_UNIT (t));
7311 : :
7312 : : /* Warn about bases that can't be talked about due to ambiguity. */
7313 : 36143417 : maybe_warn_about_inaccessible_bases (t);
7314 : :
7315 : : /* Now that we're done with layout, give the base fields the real types. */
7316 : 324093957 : for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
7317 : 287950540 : if (DECL_ARTIFICIAL (field) && IS_FAKE_BASE_TYPE (TREE_TYPE (field)))
7318 : 16623422 : TREE_TYPE (field) = TYPE_CONTEXT (TREE_TYPE (field));
7319 : :
7320 : : /* Clean up. */
7321 : 36143417 : splay_tree_delete (empty_base_offsets);
7322 : :
7323 : 36143417 : if (CLASSTYPE_EMPTY_P (t)
7324 : 65472332 : && tree_int_cst_lt (sizeof_biggest_empty_class,
7325 : 29328915 : TYPE_SIZE_UNIT (t)))
7326 : 55082 : sizeof_biggest_empty_class = TYPE_SIZE_UNIT (t);
7327 : 36143417 : }
7328 : :
7329 : : /* Determine the "key method" for the class type indicated by TYPE,
7330 : : and set CLASSTYPE_KEY_METHOD accordingly. */
7331 : :
7332 : : void
7333 : 1580921 : determine_key_method (tree type)
7334 : : {
7335 : 1580921 : tree method;
7336 : :
7337 : 1580921 : if (processing_template_decl
7338 : 1580921 : || CLASSTYPE_TEMPLATE_INSTANTIATION (type)
7339 : 2277439 : || CLASSTYPE_INTERFACE_KNOWN (type))
7340 : : return;
7341 : :
7342 : : /* The key method is the first non-pure virtual function that is not
7343 : : inline at the point of class definition. On some targets the
7344 : : key function may not be inline; those targets should not call
7345 : : this function until the end of the translation unit. */
7346 : 7091025 : for (method = TYPE_FIELDS (type); method; method = DECL_CHAIN (method))
7347 : 6938102 : if (TREE_CODE (method) == FUNCTION_DECL
7348 : 5669982 : && DECL_VINDEX (method) != NULL_TREE
7349 : 1114549 : && ! DECL_DECLARED_INLINE_P (method)
7350 : 7503528 : && ! DECL_PURE_VIRTUAL_P (method))
7351 : : {
7352 : 543560 : CLASSTYPE_KEY_METHOD (type) = method;
7353 : 543560 : break;
7354 : : }
7355 : :
7356 : : return;
7357 : : }
7358 : :
7359 : : /* Helper of find_flexarrays. Return true when FLD refers to a non-static
7360 : : class data member of non-zero size, otherwise false. */
7361 : :
7362 : : static inline bool
7363 : 33316678 : field_nonempty_p (const_tree fld)
7364 : : {
7365 : 33316678 : if (TREE_CODE (fld) == ERROR_MARK)
7366 : : return false;
7367 : :
7368 : 33316678 : tree type = TREE_TYPE (fld);
7369 : 33316678 : if (TREE_CODE (fld) == FIELD_DECL
7370 : 33316678 : && TREE_CODE (type) != ERROR_MARK
7371 : 66633356 : && (DECL_NAME (fld) || RECORD_OR_UNION_TYPE_P (type)))
7372 : : {
7373 : 33186345 : return TYPE_SIZE (type)
7374 : 33186345 : && (TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST
7375 : 33184507 : || !tree_int_cst_equal (size_zero_node, TYPE_SIZE (type)));
7376 : : }
7377 : :
7378 : : return false;
7379 : : }
7380 : :
7381 : : /* Used by find_flexarrays and related functions. */
7382 : :
7383 : : struct flexmems_t
7384 : : {
7385 : : /* The first flexible array member or non-zero array member found
7386 : : in the order of layout. */
7387 : : tree array;
7388 : : /* First non-static non-empty data member in the class or its bases. */
7389 : : tree first;
7390 : : /* The first non-static non-empty data member following either
7391 : : the flexible array member, if found, or the zero-length array member
7392 : : otherwise. AFTER[1] refers to the first such data member of a union
7393 : : of which the struct containing the flexible array member or zero-length
7394 : : array is a member, or NULL when no such union exists. This element is
7395 : : only used during searching, not for diagnosing problems. AFTER[0]
7396 : : refers to the first such data member that is not a member of such
7397 : : a union. */
7398 : : tree after[2];
7399 : :
7400 : : /* Refers to a struct (not union) in which the struct of which the flexible
7401 : : array is member is defined. Used to diagnose strictly (according to C)
7402 : : invalid uses of the latter structs. */
7403 : : tree enclosing;
7404 : : };
7405 : :
7406 : : /* Find either the first flexible array member or the first zero-length
7407 : : array, in that order of preference, among members of class T (but not
7408 : : its base classes), and set members of FMEM accordingly.
7409 : : BASE_P is true if T is a base class of another class.
7410 : : PUN is set to the outermost union in which the flexible array member
7411 : : (or zero-length array) is defined if one such union exists, otherwise
7412 : : to NULL.
7413 : : Similarly, PSTR is set to a data member of the outermost struct of
7414 : : which the flexible array is a member if one such struct exists,
7415 : : otherwise to NULL. */
7416 : :
7417 : : static void
7418 : 65284860 : find_flexarrays (tree t, flexmems_t *fmem, bool base_p,
7419 : : tree pun /* = NULL_TREE */,
7420 : : tree pstr /* = NULL_TREE */)
7421 : : {
7422 : : /* Set the "pointer" to the outermost enclosing union if not set
7423 : : yet and maintain it for the remainder of the recursion. */
7424 : 65284860 : if (!pun && TREE_CODE (t) == UNION_TYPE)
7425 : 1346664 : pun = t;
7426 : :
7427 : 930053348 : for (tree fld = TYPE_FIELDS (t); fld; fld = DECL_CHAIN (fld))
7428 : : {
7429 : 864769212 : if (fld == error_mark_node)
7430 : : return;
7431 : :
7432 : : /* Is FLD a typedef for an anonymous struct? */
7433 : :
7434 : : /* FIXME: Note that typedefs (as well as arrays) need to be fully
7435 : : handled elsewhere so that errors like the following are detected
7436 : : as well:
7437 : : typedef struct { int i, a[], j; } S; // bug c++/72753
7438 : : S s [2]; // bug c++/68489
7439 : : */
7440 : 865854464 : if (TREE_CODE (fld) == TYPE_DECL
7441 : 166766310 : && DECL_IMPLICIT_TYPEDEF_P (fld)
7442 : 8233567 : && CLASS_TYPE_P (TREE_TYPE (fld))
7443 : 870571844 : && IDENTIFIER_ANON_P (DECL_NAME (fld)))
7444 : : {
7445 : : /* Check the nested unnamed type referenced via a typedef
7446 : : independently of FMEM (since it's not a data member of
7447 : : the enclosing class). */
7448 : 1085252 : check_flexarrays (TREE_TYPE (fld));
7449 : 1085252 : continue;
7450 : : }
7451 : :
7452 : : /* Skip anything that's GCC-generated or not a (non-static) data
7453 : : member. */
7454 : 863683960 : if (DECL_ARTIFICIAL (fld) || TREE_CODE (fld) != FIELD_DECL)
7455 : 829253542 : continue;
7456 : :
7457 : : /* Type of the member. */
7458 : 34430418 : tree fldtype = TREE_TYPE (fld);
7459 : 34430418 : if (fldtype == error_mark_node)
7460 : : return;
7461 : :
7462 : : /* Determine the type of the array element or object referenced
7463 : : by the member so that it can be checked for flexible array
7464 : : members if it hasn't been yet. */
7465 : : tree eltype = fldtype;
7466 : 46905873 : while (TREE_CODE (eltype) == ARRAY_TYPE
7467 : 46905873 : || INDIRECT_TYPE_P (eltype))
7468 : 12475687 : eltype = TREE_TYPE (eltype);
7469 : :
7470 : 34430186 : if (RECORD_OR_UNION_TYPE_P (eltype))
7471 : : {
7472 : 11509233 : if (fmem->array && !fmem->after[bool (pun)])
7473 : : {
7474 : : /* Once the member after the flexible array has been found
7475 : : we're done. */
7476 : 492 : fmem->after[bool (pun)] = fld;
7477 : 492 : break;
7478 : : }
7479 : :
7480 : 16496513 : if (eltype == fldtype || TYPE_UNNAMED_P (eltype))
7481 : : {
7482 : : /* Descend into the non-static member struct or union and try
7483 : : to find a flexible array member or zero-length array among
7484 : : its members. This is only necessary for anonymous types
7485 : : and types in whose context the current type T has not been
7486 : : defined (the latter must not be checked again because they
7487 : : are already in the process of being checked by one of the
7488 : : recursive calls). */
7489 : :
7490 : 6543521 : tree first = fmem->first;
7491 : 6543521 : tree array = fmem->array;
7492 : :
7493 : : /* If this member isn't anonymous and a prior non-flexible array
7494 : : member has been seen in one of the enclosing structs, clear
7495 : : the FIRST member since it doesn't contribute to the flexible
7496 : : array struct's members. */
7497 : 6543521 : if (first && !array && !ANON_AGGR_TYPE_P (eltype))
7498 : 3206311 : fmem->first = NULL_TREE;
7499 : :
7500 : 10368829 : find_flexarrays (eltype, fmem, false, pun,
7501 : 3825308 : !pstr && TREE_CODE (t) == RECORD_TYPE ? fld : pstr);
7502 : :
7503 : 6543521 : if (fmem->array != array)
7504 : 1298 : continue;
7505 : :
7506 : 6542223 : if (first && !array && !ANON_AGGR_TYPE_P (eltype))
7507 : : {
7508 : : /* Restore the FIRST member reset above if no flexible
7509 : : array member has been found in this member's struct. */
7510 : 3205910 : fmem->first = first;
7511 : : }
7512 : :
7513 : : /* If the member struct contains the first flexible array
7514 : : member, or if this member is a base class, continue to
7515 : : the next member and avoid setting the FMEM->NEXT pointer
7516 : : to point to it. */
7517 : 6542223 : if (base_p)
7518 : 1111718 : continue;
7519 : : }
7520 : : }
7521 : :
7522 : 33316678 : if (field_nonempty_p (fld))
7523 : : {
7524 : : /* Remember the first non-static data member. */
7525 : 33181640 : if (!fmem->first)
7526 : 9222471 : fmem->first = fld;
7527 : :
7528 : : /* Remember the first non-static data member after the flexible
7529 : : array member, if one has been found, or the zero-length array
7530 : : if it has been found. */
7531 : 33181640 : if (fmem->array && !fmem->after[bool (pun)])
7532 : 1581 : fmem->after[bool (pun)] = fld;
7533 : : }
7534 : :
7535 : : /* Skip non-arrays. */
7536 : 33316678 : if (TREE_CODE (fldtype) != ARRAY_TYPE)
7537 : 30753564 : continue;
7538 : :
7539 : : /* Determine the upper bound of the array if it has one. */
7540 : 2563114 : if (TYPE_DOMAIN (fldtype))
7541 : : {
7542 : 2561276 : if (fmem->array)
7543 : : {
7544 : : /* Make a record of the zero-length array if either one
7545 : : such field or a flexible array member has been seen to
7546 : : handle the pathological and unlikely case of multiple
7547 : : such members. */
7548 : 1049 : if (!fmem->after[bool (pun)])
7549 : 79 : fmem->after[bool (pun)] = fld;
7550 : : }
7551 : 2560227 : else if (integer_all_onesp (TYPE_MAX_VALUE (TYPE_DOMAIN (fldtype))))
7552 : : {
7553 : : /* Remember the first zero-length array unless a flexible array
7554 : : member has already been seen. */
7555 : 2548 : fmem->array = fld;
7556 : 2548 : fmem->enclosing = pstr;
7557 : : }
7558 : : }
7559 : : else
7560 : : {
7561 : : /* Flexible array members have no upper bound. */
7562 : 1838 : if (fmem->array)
7563 : : {
7564 : 124 : if (TYPE_DOMAIN (TREE_TYPE (fmem->array)))
7565 : : {
7566 : : /* Replace the zero-length array if it's been stored and
7567 : : reset the after pointer. */
7568 : 38 : fmem->after[bool (pun)] = NULL_TREE;
7569 : 38 : fmem->array = fld;
7570 : 38 : fmem->enclosing = pstr;
7571 : : }
7572 : 86 : else if (!fmem->after[bool (pun)])
7573 : : /* Make a record of another flexible array member. */
7574 : 20 : fmem->after[bool (pun)] = fld;
7575 : : }
7576 : : else
7577 : : {
7578 : 1714 : fmem->array = fld;
7579 : 1714 : fmem->enclosing = pstr;
7580 : : }
7581 : : }
7582 : : }
7583 : : }
7584 : :
7585 : : /* Diagnose a strictly (by the C standard) invalid use of a struct with
7586 : : a flexible array member (or the zero-length array extension). */
7587 : :
7588 : : static void
7589 : 2349 : diagnose_invalid_flexarray (const flexmems_t *fmem)
7590 : : {
7591 : 2349 : if (fmem->array && fmem->enclosing)
7592 : : {
7593 : 672 : auto_diagnostic_group d;
7594 : 672 : if (pedwarn (location_of (fmem->enclosing), OPT_Wpedantic,
7595 : 672 : TYPE_DOMAIN (TREE_TYPE (fmem->array))
7596 : : ? G_("invalid use of %q#T with a zero-size array "
7597 : : "in %q#D")
7598 : : : G_("invalid use of %q#T with a flexible array member "
7599 : : "in %q#T"),
7600 : 672 : DECL_CONTEXT (fmem->array),
7601 : 672 : DECL_CONTEXT (fmem->enclosing)))
7602 : 88 : inform (DECL_SOURCE_LOCATION (fmem->array),
7603 : 88 : "array member %q#D declared here", fmem->array);
7604 : 672 : }
7605 : 2349 : }
7606 : :
7607 : : /* Issue diagnostics for invalid flexible array members or zero-length
7608 : : arrays that are not the last elements of the containing class or its
7609 : : base classes or that are its sole members. */
7610 : :
7611 : : static void
7612 : 35831863 : diagnose_flexarrays (tree t, const flexmems_t *fmem)
7613 : : {
7614 : 35831863 : if (!fmem->array)
7615 : : return;
7616 : :
7617 : 4262 : if (fmem->first && !fmem->after[0])
7618 : : {
7619 : 2058 : diagnose_invalid_flexarray (fmem);
7620 : 2058 : return;
7621 : : }
7622 : :
7623 : : /* Has a diagnostic been issued? */
7624 : 2204 : bool diagd = false;
7625 : :
7626 : 2204 : const char *msg = 0;
7627 : :
7628 : 2204 : if (TYPE_DOMAIN (TREE_TYPE (fmem->array)))
7629 : : {
7630 : 1683 : if (fmem->after[0])
7631 : : msg = G_("zero-size array member %qD not at end of %q#T");
7632 : 299 : else if (!fmem->first)
7633 : : msg = G_("zero-size array member %qD in an otherwise empty %q#T");
7634 : :
7635 : : if (msg)
7636 : : {
7637 : 1683 : location_t loc = DECL_SOURCE_LOCATION (fmem->array);
7638 : :
7639 : 1683 : auto_diagnostic_group d;
7640 : 1683 : if (pedwarn (loc, OPT_Wpedantic, msg, fmem->array, t))
7641 : : {
7642 : 192 : inform (location_of (t), "in the definition of %q#T", t);
7643 : 192 : diagd = true;
7644 : : }
7645 : 1683 : }
7646 : : }
7647 : : else
7648 : : {
7649 : 521 : if (fmem->after[0])
7650 : : msg = G_("flexible array member %qD not at end of %q#T");
7651 : 241 : else if (!fmem->first)
7652 : : msg = G_("flexible array member %qD in an otherwise empty %q#T");
7653 : :
7654 : : if (msg)
7655 : : {
7656 : 521 : location_t loc = DECL_SOURCE_LOCATION (fmem->array);
7657 : 521 : diagd = true;
7658 : :
7659 : 521 : auto_diagnostic_group d;
7660 : 521 : error_at (loc, msg, fmem->array, t);
7661 : :
7662 : : /* In the unlikely event that the member following the flexible
7663 : : array member is declared in a different class, or the member
7664 : : overlaps another member of a common union, point to it.
7665 : : Otherwise it should be obvious. */
7666 : 521 : if (fmem->after[0]
7667 : 521 : && ((DECL_CONTEXT (fmem->after[0])
7668 : 280 : != DECL_CONTEXT (fmem->array))))
7669 : : {
7670 : 136 : inform (DECL_SOURCE_LOCATION (fmem->after[0]),
7671 : : "next member %q#D declared here",
7672 : : fmem->after[0]);
7673 : 136 : inform (location_of (t), "in the definition of %q#T", t);
7674 : : }
7675 : 521 : }
7676 : : }
7677 : :
7678 : 2204 : if (!diagd && fmem->array && fmem->enclosing)
7679 : 291 : diagnose_invalid_flexarray (fmem);
7680 : : }
7681 : :
7682 : :
7683 : : /* Recursively check to make sure that any flexible array or zero-length
7684 : : array members of class T or its bases are valid (i.e., not the sole
7685 : : non-static data member of T and, if one exists, that it is the last
7686 : : non-static data member of T and its base classes. FMEM is expected
7687 : : to be initially null and is used internally by recursive calls to
7688 : : the function. Issue the appropriate diagnostics for the array member
7689 : : that fails the checks. */
7690 : :
7691 : : static void
7692 : 60138157 : check_flexarrays (tree t, flexmems_t *fmem /* = NULL */,
7693 : : bool base_p /* = false */)
7694 : : {
7695 : : /* Initialize the result of a search for flexible array and zero-length
7696 : : array members. Avoid doing any work if the most interesting FMEM data
7697 : : have already been populated. */
7698 : 120276314 : flexmems_t flexmems = flexmems_t ();
7699 : 60138157 : if (!fmem)
7700 : : fmem = &flexmems;
7701 : 22909488 : else if (fmem->array && fmem->first && fmem->after[0])
7702 : 12 : return;
7703 : :
7704 : 60138145 : tree fam = fmem->array;
7705 : :
7706 : : /* Recursively check the primary base class first. */
7707 : 60138145 : if (CLASSTYPE_HAS_PRIMARY_BASE_P (t))
7708 : : {
7709 : 2352906 : tree basetype = BINFO_TYPE (CLASSTYPE_PRIMARY_BINFO (t));
7710 : 2352906 : check_flexarrays (basetype, fmem, true);
7711 : : }
7712 : :
7713 : : /* Recursively check the base classes. */
7714 : 60138145 : int nbases = TYPE_BINFO (t) ? BINFO_N_BASE_BINFOS (TYPE_BINFO (t)) : 0;
7715 : 83114352 : for (int i = 0; i < nbases; ++i)
7716 : : {
7717 : 22976207 : tree base_binfo = BINFO_BASE_BINFO (TYPE_BINFO (t), i);
7718 : :
7719 : : /* The primary base class was already checked above. */
7720 : 22976207 : if (base_binfo == CLASSTYPE_PRIMARY_BINFO (t))
7721 : 2352727 : continue;
7722 : :
7723 : : /* Virtual base classes are at the end. */
7724 : 20623480 : if (BINFO_VIRTUAL_P (base_binfo))
7725 : 263244 : continue;
7726 : :
7727 : : /* Check the base class. */
7728 : 20360236 : check_flexarrays (BINFO_TYPE (base_binfo), fmem, /*base_p=*/true);
7729 : : }
7730 : :
7731 : 60138145 : if (fmem == &flexmems)
7732 : : {
7733 : : /* Check virtual base classes only once per derived class.
7734 : : I.e., this check is not performed recursively for base
7735 : : classes. */
7736 : 37228669 : int i;
7737 : 37228669 : tree base_binfo;
7738 : 37228669 : vec<tree, va_gc> *vbases;
7739 : 37425015 : for (vbases = CLASSTYPE_VBASECLASSES (t), i = 0;
7740 : 37425015 : vec_safe_iterate (vbases, i, &base_binfo); i++)
7741 : : {
7742 : : /* Check the virtual base class. */
7743 : 196346 : tree basetype = TREE_TYPE (base_binfo);
7744 : :
7745 : 196346 : check_flexarrays (basetype, fmem, /*base_p=*/true);
7746 : : }
7747 : : }
7748 : :
7749 : : /* Is the type unnamed (and therefore a member of it potentially
7750 : : an anonymous struct or union)? */
7751 : 122280821 : bool maybe_anon_p = TYPE_UNNAMED_P (t);
7752 : 1767440 : if (tree ctx = maybe_anon_p ? TYPE_CONTEXT (t) : NULL_TREE)
7753 : 1767440 : maybe_anon_p = RECORD_OR_UNION_TYPE_P (ctx);
7754 : :
7755 : : /* Search the members of the current (possibly derived) class, skipping
7756 : : unnamed structs and unions since those could be anonymous. */
7757 : 60138145 : if (fmem != &flexmems || !maybe_anon_p)
7758 : 81651069 : find_flexarrays (t, fmem, base_p || fam != fmem->array);
7759 : :
7760 : 60138145 : if (fmem == &flexmems && !maybe_anon_p)
7761 : : {
7762 : : /* Issue diagnostics for invalid flexible and zero-length array
7763 : : members found in base classes or among the members of the current
7764 : : class. Ignore anonymous structs and unions whose members are
7765 : : considered to be members of the enclosing class and thus will
7766 : : be diagnosed when checking it. */
7767 : 35831863 : diagnose_flexarrays (t, fmem);
7768 : : }
7769 : : }
7770 : :
7771 : : /* Perform processing required when the definition of T (a class type)
7772 : : is complete. Diagnose invalid definitions of flexible array members
7773 : : and zero-size arrays. */
7774 : :
7775 : : void
7776 : 36143417 : finish_struct_1 (tree t)
7777 : : {
7778 : 36143417 : tree x;
7779 : : /* A TREE_LIST. The TREE_VALUE of each node is a FUNCTION_DECL. */
7780 : 36143417 : tree virtuals = NULL_TREE;
7781 : :
7782 : 36143417 : if (COMPLETE_TYPE_P (t))
7783 : : {
7784 : 0 : gcc_assert (MAYBE_CLASS_TYPE_P (t));
7785 : 0 : error ("redefinition of %q#T", t);
7786 : 0 : popclass ();
7787 : 0 : return;
7788 : : }
7789 : :
7790 : : /* If this type was previously laid out as a forward reference,
7791 : : make sure we lay it out again. */
7792 : 36143417 : TYPE_SIZE (t) = NULL_TREE;
7793 : 36143417 : CLASSTYPE_PRIMARY_BINFO (t) = NULL_TREE;
7794 : :
7795 : : /* Make assumptions about the class; we'll reset the flags if
7796 : : necessary. */
7797 : 36143417 : CLASSTYPE_EMPTY_P (t) = 1;
7798 : 36143417 : CLASSTYPE_NEARLY_EMPTY_P (t) = 1;
7799 : 36143417 : CLASSTYPE_CONTAINS_EMPTY_CLASS_P (t) = 0;
7800 : 36143417 : CLASSTYPE_LITERAL_P (t) = true;
7801 : :
7802 : : /* Do end-of-class semantic processing: checking the validity of the
7803 : : bases and members and add implicitly generated methods. */
7804 : 36143417 : check_bases_and_members (t);
7805 : :
7806 : : /* Find the key method. */
7807 : 36143417 : if (TYPE_CONTAINS_VPTR_P (t))
7808 : : {
7809 : : /* The Itanium C++ ABI permits the key method to be chosen when
7810 : : the class is defined -- even though the key method so
7811 : : selected may later turn out to be an inline function. On
7812 : : some systems (such as ARM Symbian OS) the key method cannot
7813 : : be determined until the end of the translation unit. On such
7814 : : systems, we leave CLASSTYPE_KEY_METHOD set to NULL, which
7815 : : will cause the class to be added to KEYED_CLASSES. Then, in
7816 : : finish_file we will determine the key method. */
7817 : 1580921 : if (targetm.cxx.key_method_may_be_inline ())
7818 : 1580921 : determine_key_method (t);
7819 : :
7820 : : /* If a polymorphic class has no key method, we may emit the vtable
7821 : : in every translation unit where the class definition appears. If
7822 : : we're devirtualizing, we can look into the vtable even if we
7823 : : aren't emitting it. */
7824 : 1580921 : if (!CLASSTYPE_KEY_METHOD (t))
7825 : 1037361 : vec_safe_push (keyed_classes, t);
7826 : : }
7827 : :
7828 : : /* Layout the class itself. */
7829 : 36143417 : layout_class_type (t, &virtuals);
7830 : : /* COMPLETE_TYPE_P is now true. */
7831 : :
7832 : 36143417 : set_class_bindings (t);
7833 : :
7834 : : /* With the layout complete, check for flexible array members and
7835 : : zero-length arrays that might overlap other members in the final
7836 : : layout. */
7837 : 36143417 : check_flexarrays (t);
7838 : :
7839 : 36143417 : virtuals = modify_all_vtables (t, nreverse (virtuals));
7840 : :
7841 : : /* If necessary, create the primary vtable for this class. */
7842 : 36143417 : if (virtuals || TYPE_CONTAINS_VPTR_P (t))
7843 : : {
7844 : : /* We must enter these virtuals into the table. */
7845 : 1580921 : if (!CLASSTYPE_HAS_PRIMARY_BASE_P (t))
7846 : 232386 : build_primary_vtable (NULL_TREE, t);
7847 : 1348535 : else if (! BINFO_NEW_VTABLE_MARKED (TYPE_BINFO (t)))
7848 : : /* Here we know enough to change the type of our virtual
7849 : : function table, but we will wait until later this function. */
7850 : 0 : build_primary_vtable (CLASSTYPE_PRIMARY_BINFO (t), t);
7851 : :
7852 : : /* If we're warning about ABI tags, check the types of the new
7853 : : virtual functions. */
7854 : 1580921 : if (warn_abi_tag)
7855 : 8 : for (tree v = virtuals; v; v = TREE_CHAIN (v))
7856 : 4 : check_abi_tags (t, TREE_VALUE (v));
7857 : : }
7858 : :
7859 : 36143417 : if (TYPE_CONTAINS_VPTR_P (t))
7860 : : {
7861 : 1580921 : int vindex;
7862 : 1580921 : tree fn;
7863 : :
7864 : 1580921 : if (BINFO_VTABLE (TYPE_BINFO (t)))
7865 : 1580921 : gcc_assert (DECL_VIRTUAL_P (BINFO_VTABLE (TYPE_BINFO (t))));
7866 : 1580921 : if (!CLASSTYPE_HAS_PRIMARY_BASE_P (t))
7867 : 232386 : gcc_assert (BINFO_VIRTUALS (TYPE_BINFO (t)) == NULL_TREE);
7868 : :
7869 : : /* Add entries for virtual functions introduced by this class. */
7870 : 1580921 : BINFO_VIRTUALS (TYPE_BINFO (t))
7871 : 1580921 : = chainon (BINFO_VIRTUALS (TYPE_BINFO (t)), virtuals);
7872 : :
7873 : : /* Set DECL_VINDEX for all functions declared in this class. */
7874 : 1580921 : for (vindex = 0, fn = BINFO_VIRTUALS (TYPE_BINFO (t));
7875 : 10683422 : fn;
7876 : 9102501 : fn = TREE_CHAIN (fn),
7877 : 9102501 : vindex += (TARGET_VTABLE_USES_DESCRIPTORS
7878 : : ? TARGET_VTABLE_USES_DESCRIPTORS : 1))
7879 : : {
7880 : 9102501 : tree fndecl = BV_FN (fn);
7881 : :
7882 : 9102501 : if (DECL_THUNK_P (fndecl))
7883 : : /* A thunk. We should never be calling this entry directly
7884 : : from this vtable -- we'd use the entry for the non
7885 : : thunk base function. */
7886 : 158 : DECL_VINDEX (fndecl) = NULL_TREE;
7887 : 9102343 : else if (TREE_CODE (DECL_VINDEX (fndecl)) != INTEGER_CST)
7888 : 7287753 : DECL_VINDEX (fndecl) = build_int_cst (NULL_TREE, vindex);
7889 : : }
7890 : : }
7891 : :
7892 : 36143417 : finish_struct_bits (t);
7893 : :
7894 : 36143417 : set_method_tm_attributes (t);
7895 : 36143417 : if (flag_openmp || flag_openmp_simd)
7896 : 24154 : finish_omp_declare_simd_methods (t);
7897 : :
7898 : : /* Clear DECL_IN_AGGR_P for all member functions. Complete the rtl
7899 : : for any static member objects of the type we're working on. */
7900 : 324093957 : for (x = TYPE_FIELDS (t); x; x = DECL_CHAIN (x))
7901 : 287950540 : if (DECL_DECLARES_FUNCTION_P (x))
7902 : : {
7903 : : /* Synthesize constexpr defaulted comparisons. */
7904 : 163111536 : if (!DECL_ARTIFICIAL (x)
7905 : 162238207 : && DECL_DEFAULTED_IN_CLASS_P (x)
7906 : 175666702 : && special_function_p (x) == sfk_comparison)
7907 : 8702 : defaulted_late_check (x);
7908 : 163111536 : DECL_IN_AGGR_P (x) = false;
7909 : : }
7910 : 124839004 : else if (VAR_P (x) && TREE_STATIC (x)
7911 : 9483582 : && TREE_TYPE (x) != error_mark_node
7912 : 134322536 : && same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (x)), t))
7913 : 31302 : SET_DECL_MODE (x, TYPE_MODE (t));
7914 : :
7915 : : /* Complain if one of the field types requires lower visibility. */
7916 : 36143417 : constrain_class_visibility (t);
7917 : :
7918 : : /* Make the rtl for any new vtables we have created, and unmark
7919 : : the base types we marked. */
7920 : 36143417 : finish_vtbls (t);
7921 : :
7922 : : /* Build the VTT for T. */
7923 : 36143417 : build_vtt (t);
7924 : :
7925 : 36143417 : if (warn_nonvdtor
7926 : 194 : && TYPE_POLYMORPHIC_P (t) && accessible_nvdtor_p (t)
7927 : 36143464 : && !CLASSTYPE_FINAL (t))
7928 : 44 : warning (OPT_Wnon_virtual_dtor,
7929 : : "%q#T has virtual functions and accessible"
7930 : : " non-virtual destructor", t);
7931 : :
7932 : 36143417 : complete_vars (t);
7933 : :
7934 : 36143417 : if (warn_overloaded_virtual)
7935 : 329778 : warn_hidden (t);
7936 : :
7937 : : /* Class layout, assignment of virtual table slots, etc., is now
7938 : : complete. Give the back end a chance to tweak the visibility of
7939 : : the class or perform any other required target modifications. */
7940 : 36143417 : targetm.cxx.adjust_class_at_definition (t);
7941 : :
7942 : 36143417 : maybe_suppress_debug_info (t);
7943 : :
7944 : 36143417 : if (flag_vtable_verify)
7945 : 16 : vtv_save_class_info (t);
7946 : :
7947 : 36143417 : dump_class_hierarchy (t);
7948 : :
7949 : : /* Finish debugging output for this type. */
7950 : 36143417 : rest_of_type_compilation (t, ! LOCAL_CLASS_P (t));
7951 : :
7952 : 36143417 : if (TYPE_TRANSPARENT_AGGR (t))
7953 : : {
7954 : 180 : tree field = first_field (t);
7955 : 180 : if (field == NULL_TREE || error_operand_p (field))
7956 : : {
7957 : 12 : error ("type transparent %q#T does not have any fields", t);
7958 : 12 : TYPE_TRANSPARENT_AGGR (t) = 0;
7959 : : }
7960 : 168 : else if (DECL_ARTIFICIAL (field))
7961 : : {
7962 : 4 : if (DECL_FIELD_IS_BASE (field))
7963 : 4 : error ("type transparent class %qT has base classes", t);
7964 : : else
7965 : : {
7966 : 0 : gcc_checking_assert (DECL_VIRTUAL_P (field));
7967 : 0 : error ("type transparent class %qT has virtual functions", t);
7968 : : }
7969 : 4 : TYPE_TRANSPARENT_AGGR (t) = 0;
7970 : : }
7971 : 164 : else if (TYPE_MODE (t) != DECL_MODE (field))
7972 : : {
7973 : 4 : error ("type transparent %q#T cannot be made transparent because "
7974 : : "the type of the first field has a different ABI from the "
7975 : : "class overall", t);
7976 : 4 : TYPE_TRANSPARENT_AGGR (t) = 0;
7977 : : }
7978 : : }
7979 : : }
7980 : :
7981 : : /* When T was built up, the member declarations were added in reverse
7982 : : order. Rearrange them to declaration order. */
7983 : :
7984 : : void
7985 : 52159690 : unreverse_member_declarations (tree t)
7986 : : {
7987 : 52159690 : tree next;
7988 : 52159690 : tree prev;
7989 : 52159690 : tree x;
7990 : :
7991 : : /* The following lists are all in reverse order. Put them in
7992 : : declaration order now. */
7993 : 52159690 : CLASSTYPE_DECL_LIST (t) = nreverse (CLASSTYPE_DECL_LIST (t));
7994 : :
7995 : : /* For the TYPE_FIELDS, only the non TYPE_DECLs are in reverse
7996 : : order, so we can't just use nreverse. Due to stat_hack
7997 : : chicanery in finish_member_declaration. */
7998 : 52159690 : prev = NULL_TREE;
7999 : 52159690 : for (x = TYPE_FIELDS (t);
8000 : 263987645 : x && TREE_CODE (x) != TYPE_DECL;
8001 : 211827955 : x = next)
8002 : : {
8003 : 211827955 : next = DECL_CHAIN (x);
8004 : 211827955 : DECL_CHAIN (x) = prev;
8005 : 211827955 : prev = x;
8006 : : }
8007 : :
8008 : 52159690 : if (prev)
8009 : : {
8010 : 20928237 : DECL_CHAIN (TYPE_FIELDS (t)) = x;
8011 : 20928237 : TYPE_FIELDS (t) = prev;
8012 : : }
8013 : 52159690 : }
8014 : :
8015 : : /* Classes, structs or unions T marked with hotness attributes propagate
8016 : : the attribute to all methods. */
8017 : :
8018 : : void
8019 : 36143417 : propagate_class_warmth_attribute (tree t)
8020 : : {
8021 : 36143417 : if (t == NULL_TREE
8022 : 36143417 : || !(TREE_CODE (t) == RECORD_TYPE
8023 : : || TREE_CODE (t) == UNION_TYPE))
8024 : : return;
8025 : :
8026 : 36143417 : tree class_has_cold_attr
8027 : 36143417 : = lookup_attribute ("cold", TYPE_ATTRIBUTES (t));
8028 : 36143417 : tree class_has_hot_attr
8029 : 36143417 : = lookup_attribute ("hot", TYPE_ATTRIBUTES (t));
8030 : :
8031 : 36143417 : if (class_has_cold_attr || class_has_hot_attr)
8032 : 56 : for (tree f = TYPE_FIELDS (t); f; f = DECL_CHAIN (f))
8033 : 40 : if (DECL_DECLARES_FUNCTION_P (f))
8034 : 24 : maybe_propagate_warmth_attributes (STRIP_TEMPLATE (f), t);
8035 : : }
8036 : :
8037 : : tree
8038 : 24299840 : finish_struct (tree t, tree attributes)
8039 : : {
8040 : 24299840 : location_t saved_loc = input_location;
8041 : :
8042 : : /* Now that we've got all the field declarations, reverse everything
8043 : : as necessary. */
8044 : 24299840 : unreverse_member_declarations (t);
8045 : :
8046 : 24299840 : cplus_decl_attributes (&t, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
8047 : 24299840 : fixup_attribute_variants (t);
8048 : :
8049 : : /* Nadger the current location so that diagnostics point to the start of
8050 : : the struct, not the end. */
8051 : 24299840 : input_location = DECL_SOURCE_LOCATION (TYPE_NAME (t));
8052 : :
8053 : 24299840 : if (processing_template_decl)
8054 : : {
8055 : 16016273 : tree x;
8056 : :
8057 : 122357333 : for (x = TYPE_FIELDS (t); x; x = DECL_CHAIN (x))
8058 : 106341060 : if (DECL_DECLARES_FUNCTION_P (x))
8059 : : {
8060 : 60048108 : DECL_IN_AGGR_P (x) = false;
8061 : 60048108 : if (DECL_VIRTUAL_P (x))
8062 : 1748658 : CLASSTYPE_NON_AGGREGATE (t) = true;
8063 : : }
8064 : 46292952 : else if (TREE_CODE (x) == FIELD_DECL)
8065 : : {
8066 : 8168951 : if (TREE_PROTECTED (x) || TREE_PRIVATE (x))
8067 : 4475128 : CLASSTYPE_NON_AGGREGATE (t) = true;
8068 : : }
8069 : :
8070 : : /* Also add a USING_DECL for operator=. We know there'll be (at
8071 : : least) one, but we don't know the signature(s). We want name
8072 : : lookup not to fail or recurse into bases. This isn't added
8073 : : to the template decl list so we drop this at instantiation
8074 : : time. */
8075 : 16016273 : tree ass_op = build_lang_decl (USING_DECL, assign_op_identifier,
8076 : : NULL_TREE);
8077 : 16016273 : DECL_CONTEXT (ass_op) = t;
8078 : 16016273 : USING_DECL_SCOPE (ass_op) = t;
8079 : 16016273 : DECL_DEPENDENT_P (ass_op) = true;
8080 : 16016273 : DECL_ARTIFICIAL (ass_op) = true;
8081 : 16016273 : DECL_CHAIN (ass_op) = TYPE_FIELDS (t);
8082 : 16016273 : TYPE_FIELDS (t) = ass_op;
8083 : :
8084 : 16016273 : TYPE_SIZE (t) = bitsize_zero_node;
8085 : 16016273 : TYPE_SIZE_UNIT (t) = size_zero_node;
8086 : : /* COMPLETE_TYPE_P is now true. */
8087 : :
8088 : 16016273 : set_class_bindings (t);
8089 : :
8090 : : /* We need to emit an error message if this type was used as a parameter
8091 : : and it is an abstract type, even if it is a template. We construct
8092 : : a simple CLASSTYPE_PURE_VIRTUALS list without taking bases into
8093 : : account and we call complete_vars with this type, which will check
8094 : : the PARM_DECLS. Note that while the type is being defined,
8095 : : CLASSTYPE_PURE_VIRTUALS contains the list of the inline friends
8096 : : (see CLASSTYPE_INLINE_FRIENDS) so we need to clear it. */
8097 : 16016273 : CLASSTYPE_PURE_VIRTUALS (t) = NULL;
8098 : 138373606 : for (x = TYPE_FIELDS (t); x; x = DECL_CHAIN (x))
8099 : 122357333 : if (TREE_CODE (x) == FUNCTION_DECL && DECL_PURE_VIRTUAL_P (x))
8100 : 251798 : vec_safe_push (CLASSTYPE_PURE_VIRTUALS (t), x);
8101 : 16016273 : complete_vars (t);
8102 : :
8103 : : /* Remember current #pragma pack value. */
8104 : 16016273 : TYPE_PRECISION (t) = maximum_field_alignment;
8105 : :
8106 : 16016273 : if (cxx_dialect < cxx20)
8107 : : {
8108 : 11917068 : if (!CLASSTYPE_NON_AGGREGATE (t)
8109 : 11917068 : && type_has_user_provided_or_explicit_constructor (t))
8110 : 1262674 : CLASSTYPE_NON_AGGREGATE (t) = 1;
8111 : : }
8112 : 4099205 : else if (TYPE_HAS_USER_CONSTRUCTOR (t))
8113 : 1210998 : CLASSTYPE_NON_AGGREGATE (t) = 1;
8114 : :
8115 : : /* Fix up any variants we've already built. */
8116 : 16016273 : fixup_type_variants (t);
8117 : : }
8118 : : else
8119 : 8283567 : finish_struct_1 (t);
8120 : : /* COMPLETE_TYPE_P is now true. */
8121 : :
8122 : 24299840 : maybe_warn_about_overly_private_class (t);
8123 : :
8124 : 24299840 : if (is_std_init_list (t))
8125 : : {
8126 : : /* People keep complaining that the compiler crashes on an invalid
8127 : : definition of initializer_list, so I guess we should explicitly
8128 : : reject it. What the compiler internals care about is that it's a
8129 : : template and has a pointer field followed by size_type field. */
8130 : 13196 : bool ok = false;
8131 : 13196 : if (processing_template_decl)
8132 : : {
8133 : 13196 : tree f = next_aggregate_field (TYPE_FIELDS (t));
8134 : 13196 : if (f && TYPE_PTR_P (TREE_TYPE (f)))
8135 : : {
8136 : 13193 : f = next_aggregate_field (DECL_CHAIN (f));
8137 : 13193 : if (f && same_type_p (TREE_TYPE (f), size_type_node))
8138 : : ok = true;
8139 : : }
8140 : : }
8141 : : /* It also cannot be a union. */
8142 : 13196 : ok &= NON_UNION_CLASS_TYPE_P (t);
8143 : 13196 : if (!ok)
8144 : 9 : fatal_error (input_location, "definition of %qD does not match "
8145 : 9 : "%<#include <initializer_list>%>", TYPE_NAME (t));
8146 : : }
8147 : :
8148 : 24299831 : input_location = saved_loc;
8149 : :
8150 : 24299831 : TYPE_BEING_DEFINED (t) = 0;
8151 : :
8152 : 24299831 : if (current_class_type)
8153 : 24299831 : popclass ();
8154 : : else
8155 : 0 : error ("trying to finish struct, but kicked out due to previous parse errors");
8156 : :
8157 : 24299831 : if (flag_openmp)
8158 : 146696 : for (tree decl = TYPE_FIELDS (t); decl; decl = DECL_CHAIN (decl))
8159 : 121247 : if (TREE_CODE (decl) == FUNCTION_DECL
8160 : 121247 : && DECL_OBJECT_MEMBER_FUNCTION_P (decl))
8161 : 27729 : if (tree attr = lookup_attribute ("omp declare variant base",
8162 : 27729 : DECL_ATTRIBUTES (decl)))
8163 : 118 : omp_declare_variant_finalize (decl, attr);
8164 : :
8165 : 16016264 : if (processing_template_decl && at_function_scope_p ()
8166 : : /* Lambdas are defined by the LAMBDA_EXPR. */
8167 : 25520810 : && !LAMBDA_TYPE_P (t))
8168 : 180791 : add_stmt (build_min (TAG_DEFN, t));
8169 : :
8170 : 24299831 : return t;
8171 : : }
8172 : :
8173 : : /* Hash table to avoid endless recursion when handling references. */
8174 : : static hash_table<nofree_ptr_hash<tree_node> > *fixed_type_or_null_ref_ht;
8175 : :
8176 : : /* Return the dynamic type of INSTANCE, if known.
8177 : : Used to determine whether the virtual function table is needed
8178 : : or not.
8179 : :
8180 : : *NONNULL is set iff INSTANCE can be known to be nonnull, regardless
8181 : : of our knowledge of its type. *NONNULL should be initialized
8182 : : before this function is called. */
8183 : :
8184 : : static tree
8185 : 9578409 : fixed_type_or_null (tree instance, int *nonnull, int *cdtorp)
8186 : : {
8187 : : #define RECUR(T) fixed_type_or_null((T), nonnull, cdtorp)
8188 : :
8189 : 9578409 : switch (TREE_CODE (instance))
8190 : : {
8191 : 559124 : case INDIRECT_REF:
8192 : 559124 : if (INDIRECT_TYPE_P (TREE_TYPE (instance)))
8193 : : return NULL_TREE;
8194 : : else
8195 : 507988 : return RECUR (TREE_OPERAND (instance, 0));
8196 : :
8197 : 230535 : case CALL_EXPR:
8198 : : /* This is a call to a constructor, hence it's never zero. */
8199 : 230535 : if (CALL_EXPR_FN (instance)
8200 : 230535 : && TREE_HAS_CONSTRUCTOR (instance))
8201 : : {
8202 : 0 : if (nonnull)
8203 : 0 : *nonnull = 1;
8204 : 0 : return TREE_TYPE (instance);
8205 : : }
8206 : : return NULL_TREE;
8207 : :
8208 : 72820 : case SAVE_EXPR:
8209 : : /* This is a call to a constructor, hence it's never zero. */
8210 : 72820 : if (TREE_HAS_CONSTRUCTOR (instance))
8211 : : {
8212 : 0 : if (nonnull)
8213 : 0 : *nonnull = 1;
8214 : 0 : return TREE_TYPE (instance);
8215 : : }
8216 : 72820 : return RECUR (TREE_OPERAND (instance, 0));
8217 : :
8218 : 259 : case POINTER_PLUS_EXPR:
8219 : 259 : case PLUS_EXPR:
8220 : 259 : case MINUS_EXPR:
8221 : 259 : if (TREE_CODE (TREE_OPERAND (instance, 0)) == ADDR_EXPR)
8222 : 32 : return RECUR (TREE_OPERAND (instance, 0));
8223 : 227 : if (TREE_CODE (TREE_OPERAND (instance, 1)) == INTEGER_CST)
8224 : : /* Propagate nonnull. */
8225 : 0 : return RECUR (TREE_OPERAND (instance, 0));
8226 : :
8227 : : return NULL_TREE;
8228 : :
8229 : 2309548 : CASE_CONVERT:
8230 : 2309548 : return RECUR (TREE_OPERAND (instance, 0));
8231 : :
8232 : 874754 : case ADDR_EXPR:
8233 : 874754 : instance = TREE_OPERAND (instance, 0);
8234 : 874754 : if (nonnull)
8235 : : {
8236 : : /* Just because we see an ADDR_EXPR doesn't mean we're dealing
8237 : : with a real object -- given &p->f, p can still be null. */
8238 : 873313 : tree t = get_base_address (instance);
8239 : : /* ??? Probably should check DECL_WEAK here. */
8240 : 873313 : if (t && DECL_P (t))
8241 : 56292 : *nonnull = 1;
8242 : : }
8243 : 874754 : return RECUR (instance);
8244 : :
8245 : 536995 : case COMPONENT_REF:
8246 : : /* If this component is really a base class reference, then the field
8247 : : itself isn't definitive. */
8248 : 536995 : if (DECL_FIELD_IS_BASE (TREE_OPERAND (instance, 1)))
8249 : 1059 : return RECUR (TREE_OPERAND (instance, 0));
8250 : 535936 : return RECUR (TREE_OPERAND (instance, 1));
8251 : :
8252 : 632854 : case VAR_DECL:
8253 : 632854 : case FIELD_DECL:
8254 : 632854 : if (TREE_CODE (TREE_TYPE (instance)) == ARRAY_TYPE
8255 : 632854 : && MAYBE_CLASS_TYPE_P (TREE_TYPE (TREE_TYPE (instance))))
8256 : : {
8257 : 4 : if (nonnull)
8258 : 4 : *nonnull = 1;
8259 : 4 : return TREE_TYPE (TREE_TYPE (instance));
8260 : : }
8261 : : /* fall through. */
8262 : 4808824 : case TARGET_EXPR:
8263 : 4808824 : case PARM_DECL:
8264 : 4808824 : case RESULT_DECL:
8265 : 4808824 : if (MAYBE_CLASS_TYPE_P (TREE_TYPE (instance)))
8266 : : {
8267 : 874515 : if (nonnull)
8268 : 873177 : *nonnull = 1;
8269 : 874515 : return TREE_TYPE (instance);
8270 : : }
8271 : 3934309 : else if (instance == current_class_ptr)
8272 : : {
8273 : 3312352 : if (nonnull)
8274 : 2529113 : *nonnull = 1;
8275 : :
8276 : : /* if we're in a ctor or dtor, we know our type. If
8277 : : current_class_ptr is set but we aren't in a function, we're in
8278 : : an NSDMI (and therefore a constructor). */
8279 : 3312352 : if (current_scope () != current_function_decl
8280 : 3312352 : || (DECL_LANG_SPECIFIC (current_function_decl)
8281 : 6624564 : && (DECL_CONSTRUCTOR_P (current_function_decl)
8282 : 1335939 : || DECL_DESTRUCTOR_P (current_function_decl))))
8283 : : {
8284 : 2019490 : if (cdtorp)
8285 : 2019490 : *cdtorp = 1;
8286 : 2019490 : return TREE_TYPE (TREE_TYPE (instance));
8287 : : }
8288 : : }
8289 : 621957 : else if (TYPE_REF_P (TREE_TYPE (instance)))
8290 : : {
8291 : : /* We only need one hash table because it is always left empty. */
8292 : 541681 : if (!fixed_type_or_null_ref_ht)
8293 : 12560 : fixed_type_or_null_ref_ht
8294 : 12560 : = new hash_table<nofree_ptr_hash<tree_node> > (37);
8295 : :
8296 : : /* Reference variables should be references to objects. */
8297 : 541681 : if (nonnull)
8298 : 540042 : *nonnull = 1;
8299 : :
8300 : : /* Enter the INSTANCE in a table to prevent recursion; a
8301 : : variable's initializer may refer to the variable
8302 : : itself. */
8303 : 541681 : if (VAR_P (instance)
8304 : 14462 : && DECL_INITIAL (instance)
8305 : 1714 : && !type_dependent_expression_p_push (DECL_INITIAL (instance))
8306 : 543395 : && !fixed_type_or_null_ref_ht->find (instance))
8307 : : {
8308 : 1650 : tree type;
8309 : 1650 : tree_node **slot;
8310 : :
8311 : 1650 : slot = fixed_type_or_null_ref_ht->find_slot (instance, INSERT);
8312 : 1650 : *slot = instance;
8313 : 1650 : type = RECUR (DECL_INITIAL (instance));
8314 : 1650 : fixed_type_or_null_ref_ht->remove_elt (instance);
8315 : :
8316 : 1650 : return type;
8317 : : }
8318 : : }
8319 : : return NULL_TREE;
8320 : :
8321 : 62485 : case VIEW_CONVERT_EXPR:
8322 : 62485 : if (location_wrapper_p (instance))
8323 : 62485 : return RECUR (TREE_OPERAND (instance, 0));
8324 : : else
8325 : : /* TODO: Recursion may be correct for some non-location-wrapper
8326 : : uses of VIEW_CONVERT_EXPR. */
8327 : : return NULL_TREE;
8328 : :
8329 : : default:
8330 : : return NULL_TREE;
8331 : : }
8332 : : #undef RECUR
8333 : : }
8334 : :
8335 : : /* Return nonzero if the dynamic type of INSTANCE is known, and
8336 : : equivalent to the static type. We also handle the case where
8337 : : INSTANCE is really a pointer. Return negative if this is a
8338 : : ctor/dtor. There the dynamic type is known, but this might not be
8339 : : the most derived base of the original object, and hence virtual
8340 : : bases may not be laid out according to this type.
8341 : :
8342 : : Used to determine whether the virtual function table is needed
8343 : : or not.
8344 : :
8345 : : *NONNULL is set iff INSTANCE can be known to be nonnull, regardless
8346 : : of our knowledge of its type. *NONNULL should be initialized
8347 : : before this function is called. */
8348 : :
8349 : : int
8350 : 5025183 : resolves_to_fixed_type_p (tree instance, int* nonnull)
8351 : : {
8352 : 5025183 : tree t = TREE_TYPE (instance);
8353 : 5025183 : int cdtorp = 0;
8354 : 5025183 : tree fixed;
8355 : :
8356 : : /* processing_template_decl can be false in a template if we're in
8357 : : instantiate_non_dependent_expr, but we still want to suppress
8358 : : this check. */
8359 : 5025183 : if (in_template_context)
8360 : : {
8361 : : /* In a template we only care about the type of the result. */
8362 : 320623 : if (nonnull)
8363 : 320622 : *nonnull = true;
8364 : 320623 : return true;
8365 : : }
8366 : :
8367 : 4704560 : fixed = fixed_type_or_null (instance, nonnull, &cdtorp);
8368 : 4704560 : if (INDIRECT_TYPE_P (t))
8369 : 4704142 : t = TREE_TYPE (t);
8370 : 4704560 : if (CLASS_TYPE_P (t) && CLASSTYPE_FINAL (t))
8371 : : return 1;
8372 : 4608727 : if (fixed == NULL_TREE)
8373 : : return 0;
8374 : 2850904 : if (!same_type_ignoring_top_level_qualifiers_p (t, fixed))
8375 : : return 0;
8376 : 2850547 : return cdtorp ? -1 : 1;
8377 : : }
8378 : :
8379 : :
8380 : : void
8381 : 99426 : init_class_processing (void)
8382 : : {
8383 : 99426 : current_class_depth = 0;
8384 : 99426 : current_class_stack_size = 10;
8385 : 99426 : current_class_stack
8386 : 99426 : = XNEWVEC (struct class_stack_node, current_class_stack_size);
8387 : 99426 : sizeof_biggest_empty_class = size_zero_node;
8388 : :
8389 : 99426 : ridpointers[(int) RID_PUBLIC] = access_public_node;
8390 : 99426 : ridpointers[(int) RID_PRIVATE] = access_private_node;
8391 : 99426 : ridpointers[(int) RID_PROTECTED] = access_protected_node;
8392 : 99426 : }
8393 : :
8394 : : /* Restore the cached PREVIOUS_CLASS_LEVEL. */
8395 : :
8396 : : static void
8397 : 165302257 : restore_class_cache (void)
8398 : : {
8399 : 165302257 : tree type;
8400 : :
8401 : : /* We are re-entering the same class we just left, so we don't
8402 : : have to search the whole inheritance matrix to find all the
8403 : : decls to bind again. Instead, we install the cached
8404 : : class_shadowed list and walk through it binding names. */
8405 : 165302257 : push_binding_level (previous_class_level);
8406 : 165302257 : class_binding_level = previous_class_level;
8407 : : /* Restore IDENTIFIER_TYPE_VALUE. */
8408 : 165302257 : for (type = class_binding_level->type_shadowed;
8409 : 905639395 : type;
8410 : 740337138 : type = TREE_CHAIN (type))
8411 : 740337138 : SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (type), TREE_TYPE (type));
8412 : 165302257 : }
8413 : :
8414 : : /* Set global variables CURRENT_CLASS_NAME and CURRENT_CLASS_TYPE as
8415 : : appropriate for TYPE.
8416 : :
8417 : : So that we may avoid calls to lookup_name, we cache the _TYPE
8418 : : nodes of local TYPE_DECLs in the TREE_TYPE field of the name.
8419 : :
8420 : : For multiple inheritance, we perform a two-pass depth-first search
8421 : : of the type lattice. */
8422 : :
8423 : : void
8424 : 336337375 : pushclass (tree type)
8425 : : {
8426 : 336337375 : class_stack_node_t csn;
8427 : :
8428 : 336337375 : type = TYPE_MAIN_VARIANT (type);
8429 : :
8430 : : /* Make sure there is enough room for the new entry on the stack. */
8431 : 336337375 : if (current_class_depth + 1 >= current_class_stack_size)
8432 : : {
8433 : 10081 : current_class_stack_size *= 2;
8434 : 10081 : current_class_stack
8435 : 10081 : = XRESIZEVEC (struct class_stack_node, current_class_stack,
8436 : : current_class_stack_size);
8437 : : }
8438 : :
8439 : : /* Insert a new entry on the class stack. */
8440 : 336337375 : csn = current_class_stack + current_class_depth;
8441 : 336337375 : csn->name = current_class_name;
8442 : 336337375 : csn->type = current_class_type;
8443 : 336337375 : csn->access = current_access_specifier;
8444 : 336337375 : csn->names_used = 0;
8445 : 336337375 : csn->hidden = 0;
8446 : 336337375 : current_class_depth++;
8447 : :
8448 : : /* Now set up the new type. */
8449 : 336337375 : current_class_name = TYPE_NAME (type);
8450 : 336337375 : if (TREE_CODE (current_class_name) == TYPE_DECL)
8451 : 336337375 : current_class_name = DECL_NAME (current_class_name);
8452 : 336337375 : current_class_type = type;
8453 : :
8454 : : /* By default, things in classes are private, while things in
8455 : : structures or unions are public. */
8456 : 336337375 : current_access_specifier = (CLASSTYPE_DECLARED_CLASS (type)
8457 : 336337375 : ? access_private_node
8458 : : : access_public_node);
8459 : :
8460 : 336337375 : if (previous_class_level
8461 : 217333183 : && type != previous_class_level->this_entity
8462 : 48721206 : && current_class_depth == 1)
8463 : : {
8464 : : /* Forcibly remove any old class remnants. */
8465 : 23800998 : invalidate_class_lookup_cache ();
8466 : : }
8467 : :
8468 : 336337375 : if (!previous_class_level
8469 : 193532185 : || type != previous_class_level->this_entity
8470 : 168611977 : || current_class_depth > 1)
8471 : 171035118 : pushlevel_class ();
8472 : : else
8473 : 165302257 : restore_class_cache ();
8474 : 336337375 : }
8475 : :
8476 : : /* Get out of the current class scope. If we were in a class scope
8477 : : previously, that is the one popped to. */
8478 : :
8479 : : void
8480 : 336315693 : popclass (void)
8481 : : {
8482 : 336315693 : poplevel_class ();
8483 : :
8484 : 336315690 : current_class_depth--;
8485 : 336315690 : current_class_name = current_class_stack[current_class_depth].name;
8486 : 336315690 : current_class_type = current_class_stack[current_class_depth].type;
8487 : 336315690 : current_access_specifier = current_class_stack[current_class_depth].access;
8488 : 336315690 : if (current_class_stack[current_class_depth].names_used)
8489 : 13307270 : splay_tree_delete (current_class_stack[current_class_depth].names_used);
8490 : 336315690 : }
8491 : :
8492 : : /* Mark the top of the class stack as hidden. */
8493 : :
8494 : : void
8495 : 301948419 : push_class_stack (void)
8496 : : {
8497 : 301948419 : if (current_class_depth)
8498 : 200506888 : ++current_class_stack[current_class_depth - 1].hidden;
8499 : 301948419 : }
8500 : :
8501 : : /* Mark the top of the class stack as un-hidden. */
8502 : :
8503 : : void
8504 : 301820986 : pop_class_stack (void)
8505 : : {
8506 : 301820986 : if (current_class_depth)
8507 : 200488840 : --current_class_stack[current_class_depth - 1].hidden;
8508 : 301820986 : }
8509 : :
8510 : : /* If the class type currently being defined is either T or
8511 : : a nested type of T, returns the type from the current_class_stack,
8512 : : which might be equivalent to but not equal to T in case of
8513 : : constrained partial specializations. */
8514 : :
8515 : : tree
8516 : 3733641064 : currently_open_class (tree t)
8517 : : {
8518 : 3733641064 : int i;
8519 : :
8520 : 3733641064 : if (!CLASS_TYPE_P (t))
8521 : : return NULL_TREE;
8522 : :
8523 : 3638811268 : t = TYPE_MAIN_VARIANT (t);
8524 : :
8525 : : /* We start looking from 1 because entry 0 is from global scope,
8526 : : and has no type. */
8527 : 4291393372 : for (i = current_class_depth; i > 0; --i)
8528 : : {
8529 : 3693169078 : tree c;
8530 : 3693169078 : if (i == current_class_depth)
8531 : 3388407982 : c = current_class_type;
8532 : : else
8533 : : {
8534 : 304761096 : if (current_class_stack[i].hidden)
8535 : : break;
8536 : 247724379 : c = current_class_stack[i].type;
8537 : : }
8538 : 3636132361 : if (!c)
8539 : 129299401 : continue;
8540 : 3506832960 : if (same_type_p (c, t))
8541 : 2983550257 : return c;
8542 : : }
8543 : : return NULL_TREE;
8544 : : }
8545 : :
8546 : : /* If either current_class_type or one of its enclosing classes are derived
8547 : : from T, return the appropriate type. Used to determine how we found
8548 : : something via unqualified lookup. */
8549 : :
8550 : : tree
8551 : 115368877 : currently_open_derived_class (tree t)
8552 : : {
8553 : 115368877 : int i;
8554 : :
8555 : : /* The bases of a dependent type are unknown. */
8556 : 115368877 : if (dependent_type_p (t))
8557 : : return NULL_TREE;
8558 : :
8559 : 111361966 : if (!current_class_type)
8560 : : return NULL_TREE;
8561 : :
8562 : 111223666 : if (DERIVED_FROM_P (t, current_class_type))
8563 : 108360587 : return current_class_type;
8564 : :
8565 : 3200611 : for (i = current_class_depth - 1; i > 0; --i)
8566 : : {
8567 : 2870627 : if (current_class_stack[i].hidden)
8568 : : break;
8569 : 2870627 : if (DERIVED_FROM_P (t, current_class_stack[i].type))
8570 : 2533095 : return current_class_stack[i].type;
8571 : : }
8572 : :
8573 : : return NULL_TREE;
8574 : : }
8575 : :
8576 : : /* Return the outermost enclosing class type that is still open, or
8577 : : NULL_TREE. */
8578 : :
8579 : : tree
8580 : 13 : outermost_open_class (void)
8581 : : {
8582 : 13 : if (!current_class_type)
8583 : : return NULL_TREE;
8584 : 13 : tree r = NULL_TREE;
8585 : 13 : if (TYPE_BEING_DEFINED (current_class_type))
8586 : 13 : r = current_class_type;
8587 : 13 : for (int i = current_class_depth - 1; i > 0; --i)
8588 : : {
8589 : 0 : if (current_class_stack[i].hidden)
8590 : : break;
8591 : 0 : tree t = current_class_stack[i].type;
8592 : 0 : if (!TYPE_BEING_DEFINED (t))
8593 : : break;
8594 : 0 : r = t;
8595 : : }
8596 : : return r;
8597 : : }
8598 : :
8599 : : /* Returns the innermost class type which is not a lambda closure type. */
8600 : :
8601 : : tree
8602 : 1017558597 : current_nonlambda_class_type (void)
8603 : : {
8604 : 1017558597 : tree type = current_class_type;
8605 : 1824974038 : while (type && LAMBDA_TYPE_P (type))
8606 : 2168979 : type = decl_type_context (TYPE_NAME (type));
8607 : 1017558597 : return type;
8608 : : }
8609 : :
8610 : : /* When entering a class scope, all enclosing class scopes' names with
8611 : : static meaning (static variables, static functions, types and
8612 : : enumerators) have to be visible. This recursive function calls
8613 : : pushclass for all enclosing class contexts until global or a local
8614 : : scope is reached. TYPE is the enclosed class. */
8615 : :
8616 : : void
8617 : 541884880 : push_nested_class (tree type)
8618 : : {
8619 : : /* A namespace might be passed in error cases, like A::B:C. */
8620 : 541884880 : if (type == NULL_TREE
8621 : 541884880 : || !CLASS_TYPE_P (type))
8622 : : return;
8623 : :
8624 : 278234877 : push_nested_class (DECL_CONTEXT (TYPE_MAIN_DECL (type)));
8625 : :
8626 : 278234877 : pushclass (type);
8627 : : }
8628 : :
8629 : : /* Undoes a push_nested_class call. */
8630 : :
8631 : : void
8632 : 263631939 : pop_nested_class (void)
8633 : : {
8634 : 278213213 : tree context = DECL_CONTEXT (TYPE_MAIN_DECL (current_class_type));
8635 : :
8636 : 278213213 : popclass ();
8637 : 278213213 : if (context && CLASS_TYPE_P (context))
8638 : : pop_nested_class ();
8639 : 263631939 : }
8640 : :
8641 : : /* Returns the number of extern "LANG" blocks we are nested within. */
8642 : :
8643 : : int
8644 : 2794 : current_lang_depth (void)
8645 : : {
8646 : 2794 : return vec_safe_length (current_lang_base);
8647 : : }
8648 : :
8649 : : /* Set global variables CURRENT_LANG_NAME to appropriate value
8650 : : so that behavior of name-mangling machinery is correct. */
8651 : :
8652 : : void
8653 : 1143564 : push_lang_context (tree name)
8654 : : {
8655 : 1143564 : vec_safe_push (current_lang_base, current_lang_name);
8656 : :
8657 : 1143564 : if (name == lang_name_cplusplus)
8658 : 589848 : current_lang_name = name;
8659 : 553716 : else if (name == lang_name_c)
8660 : 553716 : current_lang_name = name;
8661 : : else
8662 : 0 : error ("language string %<\"%E\"%> not recognized", name);
8663 : 1143564 : }
8664 : :
8665 : : /* Get out of the current language scope. */
8666 : :
8667 : : void
8668 : 1143564 : pop_lang_context (void)
8669 : : {
8670 : 1143564 : current_lang_name = current_lang_base->pop ();
8671 : 1143564 : }
8672 : :
8673 : : /* Type instantiation routines. */
8674 : :
8675 : : /* Given an OVERLOAD and a TARGET_TYPE, return the function that
8676 : : matches the TARGET_TYPE. If there is no satisfactory match, return
8677 : : error_mark_node, and issue an error & warning messages under
8678 : : control of FLAGS. Permit pointers to member function if FLAGS
8679 : : permits. If TEMPLATE_ONLY, the name of the overloaded function was
8680 : : a template-id, and EXPLICIT_TARGS are the explicitly provided
8681 : : template arguments.
8682 : :
8683 : : If OVERLOAD is for one or more member functions, then ACCESS_PATH
8684 : : is the base path used to reference those member functions. If
8685 : : the address is resolved to a member function, access checks will be
8686 : : performed and errors issued if appropriate. */
8687 : :
8688 : : static tree
8689 : 234481 : resolve_address_of_overloaded_function (tree target_type,
8690 : : tree overload,
8691 : : tsubst_flags_t complain,
8692 : : bool template_only,
8693 : : tree explicit_targs,
8694 : : tree access_path)
8695 : : {
8696 : : /* Here's what the standard says:
8697 : :
8698 : : [over.over]
8699 : :
8700 : : If the name is a function template, template argument deduction
8701 : : is done, and if the argument deduction succeeds, the deduced
8702 : : arguments are used to generate a single template function, which
8703 : : is added to the set of overloaded functions considered.
8704 : :
8705 : : Non-member functions and static member functions match targets of
8706 : : type "pointer-to-function" or "reference-to-function." Nonstatic
8707 : : member functions match targets of type "pointer-to-member
8708 : : function;" the function type of the pointer to member is used to
8709 : : select the member function from the set of overloaded member
8710 : : functions. If a non-static member function is selected, the
8711 : : reference to the overloaded function name is required to have the
8712 : : form of a pointer to member as described in 5.3.1.
8713 : :
8714 : : If more than one function is selected, any template functions in
8715 : : the set are eliminated if the set also contains a non-template
8716 : : function, and any given template function is eliminated if the
8717 : : set contains a second template function that is more specialized
8718 : : than the first according to the partial ordering rules 14.5.5.2.
8719 : : After such eliminations, if any, there shall remain exactly one
8720 : : selected function. */
8721 : :
8722 : 234481 : int is_ptrmem = 0;
8723 : : /* We store the matches in a TREE_LIST rooted here. The functions
8724 : : are the TREE_PURPOSE, not the TREE_VALUE, in this list, for easy
8725 : : interoperability with most_specialized_instantiation. */
8726 : 234481 : tree matches = NULL_TREE;
8727 : 234481 : tree fn;
8728 : 234481 : tree target_fn_type;
8729 : :
8730 : : /* By the time we get here, we should be seeing only real
8731 : : pointer-to-member types, not the internal POINTER_TYPE to
8732 : : METHOD_TYPE representation. */
8733 : 234481 : gcc_assert (!TYPE_PTR_P (target_type)
8734 : : || TREE_CODE (TREE_TYPE (target_type)) != METHOD_TYPE);
8735 : :
8736 : 234481 : gcc_assert (is_overloaded_fn (overload));
8737 : :
8738 : : /* Check that the TARGET_TYPE is reasonable. */
8739 : 45063 : if (TYPE_PTRFN_P (target_type)
8740 : 234485 : || TYPE_REFFN_P (target_type))
8741 : : /* This is OK. */;
8742 : 189419 : else if (TYPE_PTRMEMFUNC_P (target_type))
8743 : : /* This is OK, too. */
8744 : : is_ptrmem = 1;
8745 : 188037 : else if (TREE_CODE (target_type) == FUNCTION_TYPE)
8746 : : /* This is OK, too. This comes from a conversion to reference
8747 : : type. */
8748 : 187983 : target_type = build_reference_type (target_type);
8749 : : else
8750 : : {
8751 : 54 : if (complain & tf_error)
8752 : 42 : error ("cannot resolve overloaded function %qD based on"
8753 : : " conversion to type %qT",
8754 : 42 : OVL_NAME (overload), target_type);
8755 : 54 : return error_mark_node;
8756 : : }
8757 : :
8758 : : /* Non-member functions and static member functions match targets of type
8759 : : "pointer-to-function" or "reference-to-function." Nonstatic member
8760 : : functions match targets of type "pointer-to-member-function;" the
8761 : : function type of the pointer to member is used to select the member
8762 : : function from the set of overloaded member functions.
8763 : :
8764 : : So figure out the FUNCTION_TYPE that we want to match against. */
8765 : 234427 : target_fn_type = static_fn_type (target_type);
8766 : :
8767 : : /* If we can find a non-template function that matches, we can just
8768 : : use it. There's no point in generating template instantiations
8769 : : if we're just going to throw them out anyhow. But, of course, we
8770 : : can only do this when we don't *need* a template function. */
8771 : 234427 : if (!template_only)
8772 : 1552300 : for (lkp_iterator iter (overload); iter; ++iter)
8773 : : {
8774 : 1340237 : tree fn = *iter;
8775 : :
8776 : 1340237 : if (TREE_CODE (fn) == TEMPLATE_DECL)
8777 : : /* We're not looking for templates just yet. */
8778 : 22652 : continue;
8779 : :
8780 : 1317585 : if ((TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE) != is_ptrmem)
8781 : : /* We're looking for a non-static member, and this isn't
8782 : : one, or vice versa. */
8783 : 768 : continue;
8784 : :
8785 : : /* Constraints must be satisfied. This is done before
8786 : : return type deduction since that instantiates the
8787 : : function. */
8788 : 1316817 : if (!constraints_satisfied_p (fn))
8789 : 3 : continue;
8790 : :
8791 : 1316814 : if (undeduced_auto_decl (fn))
8792 : : {
8793 : : /* Force instantiation to do return type deduction. */
8794 : 16 : maybe_instantiate_decl (fn);
8795 : 16 : require_deduced_type (fn);
8796 : : }
8797 : :
8798 : : /* In C++17 we need the noexcept-qualifier to compare types. */
8799 : 1316814 : if (flag_noexcept_type
8800 : 1316814 : && !maybe_instantiate_noexcept (fn, complain))
8801 : 0 : continue;
8802 : :
8803 : : /* See if there's a match. */
8804 : 1316814 : tree fntype = static_fn_type (fn);
8805 : 1316814 : if (same_type_p (target_fn_type, fntype)
8806 : 1316814 : || fnptr_conv_p (target_fn_type, fntype))
8807 : 190097 : matches = tree_cons (fn, NULL_TREE, matches);
8808 : : }
8809 : :
8810 : : /* Now, if we've already got a match (or matches), there's no need
8811 : : to proceed to the template functions. But, if we don't have a
8812 : : match we need to look at them, too. */
8813 : 212063 : if (!matches)
8814 : : {
8815 : 44938 : tree target_arg_types;
8816 : 44938 : tree target_ret_type;
8817 : 44938 : tree *args;
8818 : 44938 : unsigned int nargs, ia;
8819 : 44938 : tree arg;
8820 : :
8821 : 44938 : target_arg_types = TYPE_ARG_TYPES (target_fn_type);
8822 : 44938 : target_ret_type = TREE_TYPE (target_fn_type);
8823 : :
8824 : 44938 : nargs = list_length (target_arg_types);
8825 : 44938 : args = XALLOCAVEC (tree, nargs);
8826 : 44938 : for (arg = target_arg_types, ia = 0;
8827 : 138431 : arg != NULL_TREE;
8828 : 93493 : arg = TREE_CHAIN (arg), ++ia)
8829 : 93493 : args[ia] = TREE_VALUE (arg);
8830 : 44938 : nargs = ia;
8831 : :
8832 : 92570 : for (lkp_iterator iter (overload); iter; ++iter)
8833 : : {
8834 : 47632 : tree fn = *iter;
8835 : 47632 : tree instantiation;
8836 : 47632 : tree targs;
8837 : :
8838 : 47632 : if (TREE_CODE (fn) != TEMPLATE_DECL)
8839 : : /* We're only looking for templates. */
8840 : 2880 : continue;
8841 : :
8842 : 44752 : if ((TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
8843 : : != is_ptrmem)
8844 : : /* We're not looking for a non-static member, and this is
8845 : : one, or vice versa. */
8846 : 16 : continue;
8847 : :
8848 : 44736 : tree ret = target_ret_type;
8849 : :
8850 : : /* If the template has a deduced return type, don't expose it to
8851 : : template argument deduction. */
8852 : 44736 : if (undeduced_auto_decl (fn))
8853 : 214 : ret = NULL_TREE;
8854 : :
8855 : : /* Try to do argument deduction. */
8856 : 44736 : targs = make_tree_vec (DECL_NTPARMS (fn));
8857 : 44736 : instantiation = fn_type_unification (fn, explicit_targs, targs, args,
8858 : : nargs, ret,
8859 : : DEDUCE_EXACT, LOOKUP_NORMAL,
8860 : : NULL, false, false);
8861 : 44736 : if (instantiation == error_mark_node)
8862 : : /* Instantiation failed. */
8863 : 10886 : continue;
8864 : :
8865 : : /* Constraints must be satisfied. This is done before
8866 : : return type deduction since that instantiates the
8867 : : function. */
8868 : 33850 : if (flag_concepts && !constraints_satisfied_p (instantiation))
8869 : 0 : continue;
8870 : :
8871 : : /* And now force instantiation to do return type deduction. */
8872 : 33850 : if (undeduced_auto_decl (instantiation))
8873 : : {
8874 : 68 : ++function_depth;
8875 : 68 : instantiate_decl (instantiation, /*defer*/false, /*class*/false);
8876 : 68 : --function_depth;
8877 : :
8878 : 68 : require_deduced_type (instantiation);
8879 : : }
8880 : :
8881 : : /* In C++17 we need the noexcept-qualifier to compare types. */
8882 : 33850 : if (flag_noexcept_type)
8883 : 32473 : maybe_instantiate_noexcept (instantiation, complain);
8884 : :
8885 : : /* See if there's a match. */
8886 : 33850 : tree fntype = static_fn_type (instantiation);
8887 : 33850 : if (same_type_p (target_fn_type, fntype)
8888 : 33850 : || fnptr_conv_p (target_fn_type, fntype))
8889 : 33829 : matches = tree_cons (instantiation, fn, matches);
8890 : : }
8891 : :
8892 : : /* Now, remove all but the most specialized of the matches. */
8893 : 44938 : if (matches)
8894 : : {
8895 : 33722 : tree match = most_specialized_instantiation (matches);
8896 : :
8897 : 33722 : if (match != error_mark_node)
8898 : 33692 : matches = tree_cons (TREE_PURPOSE (match),
8899 : : NULL_TREE,
8900 : : NULL_TREE);
8901 : : }
8902 : : }
8903 : :
8904 : : /* Now we should have exactly one function in MATCHES. */
8905 : 33722 : if (matches == NULL_TREE)
8906 : : {
8907 : : /* There were *no* matches. */
8908 : 11216 : if (complain & tf_error)
8909 : : {
8910 : 98 : error ("no matches converting function %qD to type %q#T",
8911 : 98 : OVL_NAME (overload), target_type);
8912 : :
8913 : 98 : print_candidates (overload);
8914 : : }
8915 : 11216 : return error_mark_node;
8916 : : }
8917 : 223211 : else if (TREE_CHAIN (matches))
8918 : : {
8919 : : /* There were too many matches. First check if they're all
8920 : : the same function. */
8921 : 170 : tree match = NULL_TREE;
8922 : :
8923 : 170 : fn = TREE_PURPOSE (matches);
8924 : :
8925 : : /* For multi-versioned functions, more than one match is just fine and
8926 : : decls_match will return false as they are different. */
8927 : 778 : for (match = TREE_CHAIN (matches); match; match = TREE_CHAIN (match))
8928 : 638 : if (!decls_match (fn, TREE_PURPOSE (match))
8929 : 1256 : && !targetm.target_option.function_versions
8930 : 618 : (fn, TREE_PURPOSE (match)))
8931 : : break;
8932 : :
8933 : 170 : if (match)
8934 : : {
8935 : 30 : if (complain & tf_error)
8936 : : {
8937 : 10 : error ("converting overloaded function %qD to type %q#T is ambiguous",
8938 : 10 : OVL_NAME (overload), target_type);
8939 : :
8940 : : /* Since print_candidates expects the functions in the
8941 : : TREE_VALUE slot, we flip them here. */
8942 : 40 : for (match = matches; match; match = TREE_CHAIN (match))
8943 : 20 : TREE_VALUE (match) = TREE_PURPOSE (match);
8944 : :
8945 : 10 : print_candidates (matches);
8946 : : }
8947 : :
8948 : 30 : return error_mark_node;
8949 : : }
8950 : : }
8951 : :
8952 : : /* Good, exactly one match. Now, convert it to the correct type. */
8953 : 223181 : fn = TREE_PURPOSE (matches);
8954 : :
8955 : 445066 : if (DECL_OBJECT_MEMBER_FUNCTION_P (fn)
8956 : 223445 : && !(complain & tf_ptrmem_ok))
8957 : : {
8958 : : /* Previously we allowed this behavior for iobj member functions when the
8959 : : -fms-extensions flag is passed as MSVC allows this as a language
8960 : : extension. MSVC also allows this for xobj member functions, but the
8961 : : documentation for -fms-extensions states it's purpose is to support
8962 : : the use of microsoft headers. Until otherwise demonstrated, we should
8963 : : assume xobj member functions are not used in this manner in microsoft
8964 : : headers and forbid the incorrect syntax instead of supporting it for
8965 : : non-legacy uses. This should hopefully encourage conformance going
8966 : : forward.
8967 : : This comment is referred to in typeck.cc:cp_build_addr_expr_1. */
8968 : 326 : if (DECL_IOBJ_MEMBER_FUNCTION_P (fn) && flag_ms_extensions)
8969 : : /* Early escape. */;
8970 : 298 : else if (!(complain & tf_error))
8971 : 216 : return error_mark_node;
8972 : 82 : else if (DECL_XOBJ_MEMBER_FUNCTION_P (fn))
8973 : : {
8974 : 34 : auto_diagnostic_group d;
8975 : : /* Should match the error in typeck.cc:cp_build_addr_expr_1.
8976 : : We seem to lack the details here to match that diagnostic exactly,
8977 : : perhaps this could be fixed in the future? See PR113075 bug 2. */
8978 : 34 : error_at (input_location,
8979 : : "ISO C++ forbids taking the address of an unqualified"
8980 : : " or parenthesized non-static member function to form"
8981 : : " a pointer to explicit object member function.");
8982 : : /* This is incorrect, see PR113075 bug 3. */
8983 : 34 : inform (input_location,
8984 : : "a pointer to explicit object member function can only be "
8985 : : "formed with %<&%E%>", fn);
8986 : 34 : }
8987 : : else
8988 : : {
8989 : 48 : static int explained;
8990 : 48 : gcc_assert (DECL_IOBJ_MEMBER_FUNCTION_P (fn) && !flag_ms_extensions);
8991 : : /* Is there a reason this error message doesn't match the one in
8992 : : typeck.cc:cp_build_addr_expr_1? */
8993 : 48 : auto_diagnostic_group d;
8994 : 48 : if (permerror (input_location, "assuming pointer to member %qD", fn)
8995 : 48 : && !explained)
8996 : : {
8997 : 24 : inform (input_location, "(a pointer to member can only be "
8998 : : "formed with %<&%E%>)", fn);
8999 : 24 : explained = 1;
9000 : : }
9001 : 48 : }
9002 : : }
9003 : :
9004 : : /* If a pointer to a function that is multi-versioned is requested, the
9005 : : pointer to the dispatcher function is returned instead. This works
9006 : : well because indirectly calling the function will dispatch the right
9007 : : function version at run-time. */
9008 : 222965 : if (DECL_FUNCTION_VERSIONED (fn))
9009 : : {
9010 : 120 : fn = get_function_version_dispatcher (fn);
9011 : 120 : if (fn == NULL)
9012 : 12 : return error_mark_node;
9013 : : /* Mark all the versions corresponding to the dispatcher as used. */
9014 : 108 : if (!(complain & tf_conv))
9015 : 36 : mark_versions_used (fn);
9016 : : }
9017 : :
9018 : : /* If we're doing overload resolution purely for the purpose of
9019 : : determining conversion sequences, we should not consider the
9020 : : function used. If this conversion sequence is selected, the
9021 : : function will be marked as used at this point. */
9022 : 222953 : if (!(complain & tf_conv))
9023 : : {
9024 : : /* Make =delete work with SFINAE. */
9025 : 204466 : if (DECL_DELETED_FN (fn) && !(complain & tf_error))
9026 : 0 : return error_mark_node;
9027 : 204466 : if (!mark_used (fn, complain) && !(complain & tf_error))
9028 : 0 : return error_mark_node;
9029 : : }
9030 : :
9031 : : /* We could not check access to member functions when this
9032 : : expression was originally created since we did not know at that
9033 : : time to which function the expression referred. */
9034 : 222953 : if (DECL_FUNCTION_MEMBER_P (fn))
9035 : : {
9036 : 4515 : gcc_assert (access_path);
9037 : 4515 : perform_or_defer_access_check (access_path, fn, fn, complain);
9038 : : }
9039 : :
9040 : 222953 : if (TYPE_PTRFN_P (target_type) || TYPE_PTRMEMFUNC_P (target_type))
9041 : 35583 : return cp_build_addr_expr (fn, complain);
9042 : : else
9043 : : {
9044 : : /* The target must be a REFERENCE_TYPE. Above, cp_build_unary_op
9045 : : will mark the function as addressed, but here we must do it
9046 : : explicitly. */
9047 : 187370 : cxx_mark_addressable (fn);
9048 : :
9049 : 187370 : return fn;
9050 : : }
9051 : : }
9052 : :
9053 : : /* This function will instantiate the type of the expression given in
9054 : : RHS to match the type of LHSTYPE. If errors exist, then return
9055 : : error_mark_node. COMPLAIN is a bit mask. If TF_ERROR is set, then
9056 : : we complain on errors. If we are not complaining, never modify rhs,
9057 : : as overload resolution wants to try many possible instantiations, in
9058 : : the hope that at least one will work.
9059 : :
9060 : : For non-recursive calls, LHSTYPE should be a function, pointer to
9061 : : function, or a pointer to member function. */
9062 : :
9063 : : tree
9064 : 236784 : instantiate_type (tree lhstype, tree rhs, tsubst_flags_t complain)
9065 : : {
9066 : 242329 : tsubst_flags_t complain_in = complain;
9067 : 242329 : tree access_path = NULL_TREE;
9068 : :
9069 : 242329 : complain &= ~tf_ptrmem_ok;
9070 : :
9071 : 242329 : STRIP_ANY_LOCATION_WRAPPER (rhs);
9072 : :
9073 : 242329 : if (lhstype == unknown_type_node)
9074 : : {
9075 : 0 : if (complain & tf_error)
9076 : 0 : error ("not enough type information");
9077 : 0 : return error_mark_node;
9078 : : }
9079 : :
9080 : 242329 : if (TREE_TYPE (rhs) != NULL_TREE && ! (type_unknown_p (rhs)))
9081 : : {
9082 : 1560 : tree fntype = non_reference (lhstype);
9083 : 1560 : if (same_type_p (fntype, TREE_TYPE (rhs)))
9084 : : return rhs;
9085 : 319 : if (fnptr_conv_p (fntype, TREE_TYPE (rhs)))
9086 : : return rhs;
9087 : 317 : if (flag_ms_extensions
9088 : 24 : && TYPE_PTRMEMFUNC_P (fntype)
9089 : 341 : && !TYPE_PTRMEMFUNC_P (TREE_TYPE (rhs)))
9090 : : /* Microsoft allows `A::f' to be resolved to a
9091 : : pointer-to-member. */
9092 : : ;
9093 : : else
9094 : : {
9095 : 293 : if (complain & tf_error)
9096 : 53 : error ("cannot convert %qE from type %qT to type %qT",
9097 : 53 : rhs, TREE_TYPE (rhs), fntype);
9098 : 293 : return error_mark_node;
9099 : : }
9100 : : }
9101 : :
9102 : : /* If we instantiate a template, and it is a A ?: C expression
9103 : : with omitted B, look through the SAVE_EXPR. */
9104 : 240793 : if (TREE_CODE (rhs) == SAVE_EXPR)
9105 : 12 : rhs = TREE_OPERAND (rhs, 0);
9106 : :
9107 : 240793 : if (BASELINK_P (rhs))
9108 : : {
9109 : 4918 : access_path = BASELINK_ACCESS_BINFO (rhs);
9110 : 4918 : rhs = BASELINK_FUNCTIONS (rhs);
9111 : : }
9112 : :
9113 : : /* There are only a few kinds of expressions that may have a type
9114 : : dependent on overload resolution. */
9115 : 240793 : gcc_assert (TREE_CODE (rhs) == ADDR_EXPR
9116 : : || TREE_CODE (rhs) == COMPONENT_REF
9117 : : || is_overloaded_fn (rhs)
9118 : : || (flag_ms_extensions && TREE_CODE (rhs) == FUNCTION_DECL));
9119 : :
9120 : : /* This should really only be used when attempting to distinguish
9121 : : what sort of a pointer to function we have. For now, any
9122 : : arithmetic operation which is not supported on pointers
9123 : : is rejected as an error. */
9124 : :
9125 : 240793 : switch (TREE_CODE (rhs))
9126 : : {
9127 : 767 : case COMPONENT_REF:
9128 : 767 : {
9129 : 767 : tree member = TREE_OPERAND (rhs, 1);
9130 : :
9131 : 767 : member = instantiate_type (lhstype, member, complain);
9132 : 767 : if (member != error_mark_node
9133 : 767 : && TREE_SIDE_EFFECTS (TREE_OPERAND (rhs, 0)))
9134 : : /* Do not lose object's side effects. */
9135 : 36 : return build2 (COMPOUND_EXPR, TREE_TYPE (member),
9136 : 72 : TREE_OPERAND (rhs, 0), member);
9137 : : return member;
9138 : : }
9139 : :
9140 : 1942 : case OFFSET_REF:
9141 : 1942 : rhs = TREE_OPERAND (rhs, 1);
9142 : 1942 : if (BASELINK_P (rhs))
9143 : : return instantiate_type (lhstype, rhs, complain_in);
9144 : :
9145 : : /* This can happen if we are forming a pointer-to-member for a
9146 : : member template. */
9147 : 0 : gcc_assert (TREE_CODE (rhs) == TEMPLATE_ID_EXPR);
9148 : :
9149 : : /* Fall through. */
9150 : :
9151 : 22378 : case TEMPLATE_ID_EXPR:
9152 : 22378 : {
9153 : 22378 : tree fns = TREE_OPERAND (rhs, 0);
9154 : 22378 : tree args = TREE_OPERAND (rhs, 1);
9155 : :
9156 : 22378 : return
9157 : 22378 : resolve_address_of_overloaded_function (lhstype, fns, complain_in,
9158 : : /*template_only=*/true,
9159 : 22378 : args, access_path);
9160 : : }
9161 : :
9162 : 212103 : case OVERLOAD:
9163 : 212103 : case FUNCTION_DECL:
9164 : 212103 : return
9165 : 212103 : resolve_address_of_overloaded_function (lhstype, rhs, complain_in,
9166 : : /*template_only=*/false,
9167 : : /*explicit_targs=*/NULL_TREE,
9168 : 212103 : access_path);
9169 : :
9170 : 3603 : case ADDR_EXPR:
9171 : 3603 : {
9172 : 3603 : if (PTRMEM_OK_P (rhs))
9173 : 1942 : complain |= tf_ptrmem_ok;
9174 : :
9175 : 3603 : return instantiate_type (lhstype, TREE_OPERAND (rhs, 0), complain);
9176 : : }
9177 : :
9178 : 0 : case ERROR_MARK:
9179 : 0 : return error_mark_node;
9180 : :
9181 : 0 : default:
9182 : 0 : gcc_unreachable ();
9183 : : }
9184 : : return error_mark_node;
9185 : : }
9186 : :
9187 : : /* Return the name of the virtual function pointer field
9188 : : (as an IDENTIFIER_NODE) for the given TYPE. Note that
9189 : : this may have to look back through base types to find the
9190 : : ultimate field name. (For single inheritance, these could
9191 : : all be the same name. Who knows for multiple inheritance). */
9192 : :
9193 : : static tree
9194 : 232386 : get_vfield_name (tree type)
9195 : : {
9196 : 232386 : tree binfo, base_binfo;
9197 : :
9198 : 232386 : for (binfo = TYPE_BINFO (type);
9199 : 232386 : BINFO_N_BASE_BINFOS (binfo);
9200 : : binfo = base_binfo)
9201 : : {
9202 : 72503 : base_binfo = BINFO_BASE_BINFO (binfo, 0);
9203 : :
9204 : 145006 : if (BINFO_VIRTUAL_P (base_binfo)
9205 : 72503 : || !TYPE_CONTAINS_VPTR_P (BINFO_TYPE (base_binfo)))
9206 : : break;
9207 : : }
9208 : :
9209 : 232386 : type = BINFO_TYPE (binfo);
9210 : 232386 : tree ctor_name = constructor_name (type);
9211 : 232386 : char *buf = (char *) alloca (sizeof (VFIELD_NAME_FORMAT)
9212 : : + IDENTIFIER_LENGTH (ctor_name) + 2);
9213 : 232386 : sprintf (buf, VFIELD_NAME_FORMAT, IDENTIFIER_POINTER (ctor_name));
9214 : 232386 : return get_identifier (buf);
9215 : : }
9216 : :
9217 : : /* Build a dummy reference to ourselves so Derived::Base (and A::A) works,
9218 : : according to [class]:
9219 : : The class-name is also inserted
9220 : : into the scope of the class itself. For purposes of access checking,
9221 : : the inserted class name is treated as if it were a public member name. */
9222 : :
9223 : : void
9224 : 24299852 : build_self_reference (void)
9225 : : {
9226 : 24299852 : tree name = DECL_NAME (TYPE_NAME (current_class_type));
9227 : 24299852 : tree decl = build_lang_decl (TYPE_DECL, name, current_class_type);
9228 : :
9229 : 24299852 : DECL_NONLOCAL (decl) = 1;
9230 : 24299852 : DECL_CONTEXT (decl) = current_class_type;
9231 : 24299852 : DECL_ARTIFICIAL (decl) = 1;
9232 : 24299852 : SET_DECL_SELF_REFERENCE_P (decl);
9233 : 24299852 : set_underlying_type (decl);
9234 : 24299852 : set_instantiating_module (decl);
9235 : :
9236 : 24299852 : if (processing_template_decl)
9237 : 16016273 : decl = push_template_decl (decl);
9238 : :
9239 : 24299852 : tree saved_cas = current_access_specifier;
9240 : 24299852 : current_access_specifier = access_public_node;
9241 : 24299852 : finish_member_declaration (decl);
9242 : 24299852 : current_access_specifier = saved_cas;
9243 : 24299852 : }
9244 : :
9245 : : /* Returns 1 if TYPE contains only padding bytes. */
9246 : :
9247 : : int
9248 : 443780753 : is_empty_class (tree type)
9249 : : {
9250 : 443780753 : if (type == error_mark_node)
9251 : : return 0;
9252 : :
9253 : 443780749 : if (! CLASS_TYPE_P (type))
9254 : : return 0;
9255 : :
9256 : 275654886 : return CLASSTYPE_EMPTY_P (type);
9257 : : }
9258 : :
9259 : : /* Returns true if TYPE contains no actual data, just various
9260 : : possible combinations of empty classes. If IGNORE_VPTR is true,
9261 : : a vptr doesn't prevent the class from being considered empty. Typically
9262 : : we want to ignore the vptr on assignment, and not on initialization. */
9263 : :
9264 : : bool
9265 : 297581886 : is_really_empty_class (tree type, bool ignore_vptr)
9266 : : {
9267 : 297581886 : if (CLASS_TYPE_P (type))
9268 : : {
9269 : 51759694 : tree field;
9270 : 51759694 : tree binfo;
9271 : 51759694 : tree base_binfo;
9272 : 51759694 : int i;
9273 : :
9274 : : /* CLASSTYPE_EMPTY_P isn't set properly until the class is actually laid
9275 : : out, but we'd like to be able to check this before then. */
9276 : 51759694 : if (COMPLETE_TYPE_P (type) && is_empty_class (type))
9277 : : return true;
9278 : :
9279 : 35501085 : if (!ignore_vptr && TYPE_CONTAINS_VPTR_P (type))
9280 : : return false;
9281 : :
9282 : 47443010 : for (binfo = TYPE_BINFO (type), i = 0;
9283 : 47443010 : BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
9284 : 13085133 : if (!is_really_empty_class (BINFO_TYPE (base_binfo), ignore_vptr))
9285 : : return false;
9286 : 321288345 : for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
9287 : 302604872 : if (TREE_CODE (field) == FIELD_DECL
9288 : 17064782 : && !DECL_ARTIFICIAL (field)
9289 : : /* An unnamed bit-field is not a data member. */
9290 : 15804485 : && !DECL_UNNAMED_BIT_FIELD (field)
9291 : 318406823 : && !is_really_empty_class (TREE_TYPE (field), ignore_vptr))
9292 : : return false;
9293 : : return true;
9294 : : }
9295 : 245822192 : else if (TREE_CODE (type) == ARRAY_TYPE)
9296 : 1324419 : return (integer_zerop (array_type_nelts_top (type))
9297 : 1324419 : || is_really_empty_class (TREE_TYPE (type), ignore_vptr));
9298 : : return false;
9299 : : }
9300 : :
9301 : : /* Note that NAME was looked up while the current class was being
9302 : : defined and that the result of that lookup was DECL. */
9303 : :
9304 : : void
9305 : 1715195889 : maybe_note_name_used_in_class (tree name, tree decl)
9306 : : {
9307 : : /* If we're not defining a class, there's nothing to do. */
9308 : 1715195889 : if (!(innermost_scope_kind() == sk_class
9309 : 239941960 : && TYPE_BEING_DEFINED (current_class_type)
9310 : 418442419 : && !LAMBDA_TYPE_P (current_class_type)))
9311 : : return;
9312 : :
9313 : 209759043 : const cp_binding_level *blev = nullptr;
9314 : 209759043 : if (const cxx_binding *binding = IDENTIFIER_BINDING (name))
9315 : 175825764 : blev = binding->scope;
9316 : 209759043 : const cp_binding_level *lev = current_binding_level;
9317 : :
9318 : : /* Record the binding in the names_used tables for classes inside blev. */
9319 : 309887494 : for (int i = current_class_depth; i > 0; --i)
9320 : : {
9321 : 443667220 : tree type = (i == current_class_depth
9322 : 221833610 : ? current_class_type
9323 : 12074567 : : current_class_stack[i].type);
9324 : :
9325 : 238367903 : for (; lev; lev = lev->level_chain)
9326 : : {
9327 : 238367903 : if (lev == blev)
9328 : : /* We found the declaration. */
9329 : : return;
9330 : 116662744 : if (lev->kind == sk_class && lev->this_entity == type)
9331 : : /* This class is inside the declaration scope. */
9332 : : break;
9333 : : }
9334 : :
9335 : 100128451 : auto &names_used = current_class_stack[i-1].names_used;
9336 : 100128451 : if (!names_used)
9337 : 13307281 : names_used = splay_tree_new (splay_tree_compare_pointers, 0, 0);
9338 : :
9339 : 100128451 : tree use = build1_loc (input_location, VIEW_CONVERT_EXPR,
9340 : 100128451 : TREE_TYPE (decl), decl);
9341 : 100128451 : EXPR_LOCATION_WRAPPER_P (use) = 1;
9342 : 100128451 : splay_tree_insert (names_used,
9343 : : (splay_tree_key) name,
9344 : : (splay_tree_value) use);
9345 : : }
9346 : : }
9347 : :
9348 : : /* Note that NAME was declared (as DECL) in the current class. Check
9349 : : to see that the declaration is valid under [class.member.lookup]:
9350 : :
9351 : : If [the result of a search in T for N at point P] differs from the result of
9352 : : a search in T for N from immediately after the class-specifier of T, the
9353 : : program is ill-formed, no diagnostic required. */
9354 : :
9355 : : void
9356 : 255864421 : note_name_declared_in_class (tree name, tree decl)
9357 : : {
9358 : 255864421 : splay_tree names_used;
9359 : 255864421 : splay_tree_node n;
9360 : :
9361 : : /* Look to see if we ever used this name. */
9362 : 255864421 : names_used
9363 : 255864421 : = current_class_stack[current_class_depth - 1].names_used;
9364 : 255864421 : if (!names_used)
9365 : : return;
9366 : : /* The C language allows members to be declared with a type of the same
9367 : : name, and the C++ standard says this diagnostic is not required. So
9368 : : allow it in extern "C" blocks unless pedantic is specified.
9369 : : Allow it in all cases if -ms-extensions is specified. */
9370 : 81229526 : if ((!pedantic && current_lang_name == lang_name_c)
9371 : 79235038 : || flag_ms_extensions)
9372 : : return;
9373 : 79235030 : n = splay_tree_lookup (names_used, (splay_tree_key) name);
9374 : 79235030 : if (n)
9375 : : {
9376 : 80 : tree use = (tree) n->value;
9377 : 80 : location_t loc = EXPR_LOCATION (use);
9378 : 80 : tree olddecl = OVL_FIRST (TREE_OPERAND (use, 0));
9379 : : /* [basic.scope.class]
9380 : :
9381 : : A name N used in a class S shall refer to the same declaration
9382 : : in its context and when re-evaluated in the completed scope of
9383 : : S. */
9384 : 80 : auto ov = make_temp_override (global_dc->m_pedantic_errors);
9385 : 80 : if (TREE_CODE (decl) == TYPE_DECL
9386 : 28 : && TREE_CODE (olddecl) == TYPE_DECL
9387 : 108 : && same_type_p (TREE_TYPE (decl), TREE_TYPE (olddecl)))
9388 : : /* Different declaration, but same meaning; just warn. */;
9389 : 72 : else if (flag_permissive)
9390 : : /* Let -fpermissive make it a warning like past versions. */;
9391 : : else
9392 : : /* Make it an error. */
9393 : 44 : global_dc->m_pedantic_errors = 1;
9394 : 160 : if (pedwarn (location_of (decl), OPT_Wchanges_meaning,
9395 : : "declaration of %q#D changes meaning of %qD",
9396 : 80 : decl, OVL_NAME (decl)))
9397 : : {
9398 : 68 : inform (loc, "used here to mean %q#D", olddecl);
9399 : 68 : inform (location_of (olddecl), "declared here" );
9400 : : }
9401 : 80 : }
9402 : : }
9403 : :
9404 : : /* Returns the VAR_DECL for the complete vtable associated with BINFO.
9405 : : Secondary vtables are merged with primary vtables; this function
9406 : : will return the VAR_DECL for the primary vtable. */
9407 : :
9408 : : tree
9409 : 5422242 : get_vtbl_decl_for_binfo (tree binfo)
9410 : : {
9411 : 5422242 : tree decl;
9412 : :
9413 : 5422242 : decl = BINFO_VTABLE (binfo);
9414 : 5422242 : if (decl && TREE_CODE (decl) == POINTER_PLUS_EXPR)
9415 : : {
9416 : 5422238 : gcc_assert (TREE_CODE (TREE_OPERAND (decl, 0)) == ADDR_EXPR);
9417 : 5422238 : decl = TREE_OPERAND (TREE_OPERAND (decl, 0), 0);
9418 : : }
9419 : 5422238 : if (decl)
9420 : 5422238 : gcc_assert (VAR_P (decl));
9421 : 5422242 : return decl;
9422 : : }
9423 : :
9424 : :
9425 : : /* Returns the binfo for the primary base of BINFO. If the resulting
9426 : : BINFO is a virtual base, and it is inherited elsewhere in the
9427 : : hierarchy, then the returned binfo might not be the primary base of
9428 : : BINFO in the complete object. Check BINFO_PRIMARY_P or
9429 : : BINFO_LOST_PRIMARY_P to be sure. */
9430 : :
9431 : : static tree
9432 : 38708145 : get_primary_binfo (tree binfo)
9433 : : {
9434 : 38708145 : tree primary_base;
9435 : :
9436 : 38708145 : primary_base = CLASSTYPE_PRIMARY_BINFO (BINFO_TYPE (binfo));
9437 : 38708145 : if (!primary_base)
9438 : : return NULL_TREE;
9439 : :
9440 : 11653385 : return copied_binfo (primary_base, binfo);
9441 : : }
9442 : :
9443 : : /* As above, but iterate until we reach the binfo that actually provides the
9444 : : vptr for BINFO. */
9445 : :
9446 : : static tree
9447 : 2431237 : most_primary_binfo (tree binfo)
9448 : : {
9449 : 2431237 : tree b = binfo;
9450 : 7566596 : while (CLASSTYPE_HAS_PRIMARY_BASE_P (BINFO_TYPE (b))
9451 : 10270718 : && !BINFO_LOST_PRIMARY_P (b))
9452 : : {
9453 : 2704122 : tree primary_base = get_primary_binfo (b);
9454 : 2704122 : gcc_assert (BINFO_PRIMARY_P (primary_base)
9455 : : && BINFO_INHERITANCE_CHAIN (primary_base) == b);
9456 : : b = primary_base;
9457 : : }
9458 : 2431237 : return b;
9459 : : }
9460 : :
9461 : : /* Returns true if BINFO gets its vptr from a virtual base of the most derived
9462 : : type. Note that the virtual inheritance might be above or below BINFO in
9463 : : the hierarchy. */
9464 : :
9465 : : bool
9466 : 144 : vptr_via_virtual_p (tree binfo)
9467 : : {
9468 : 144 : if (TYPE_P (binfo))
9469 : 0 : binfo = TYPE_BINFO (binfo);
9470 : 144 : tree primary = most_primary_binfo (binfo);
9471 : : /* Don't limit binfo_via_virtual, we want to return true when BINFO itself is
9472 : : a morally virtual base. */
9473 : 144 : tree virt = binfo_via_virtual (primary, NULL_TREE);
9474 : 144 : return virt != NULL_TREE;
9475 : : }
9476 : :
9477 : : /* If INDENTED_P is zero, indent to INDENT. Return nonzero. */
9478 : :
9479 : : static int
9480 : 172 : maybe_indent_hierarchy (FILE * stream, int indent, int indented_p)
9481 : : {
9482 : 0 : if (!indented_p)
9483 : 52 : fprintf (stream, "%*s", indent, "");
9484 : 172 : return 1;
9485 : : }
9486 : :
9487 : : /* Dump the offsets of all the bases rooted at BINFO to STREAM.
9488 : : INDENT should be zero when called from the top level; it is
9489 : : incremented recursively. IGO indicates the next expected BINFO in
9490 : : inheritance graph ordering. */
9491 : :
9492 : : static tree
9493 : 136 : dump_class_hierarchy_r (FILE *stream,
9494 : : dump_flags_t flags,
9495 : : tree binfo,
9496 : : tree igo,
9497 : : int indent)
9498 : : {
9499 : 136 : int indented = 0;
9500 : 136 : tree base_binfo;
9501 : 136 : int i;
9502 : :
9503 : 272 : fprintf (stream, "%s (0x" HOST_WIDE_INT_PRINT_HEX ") ",
9504 : 136 : type_as_string (BINFO_TYPE (binfo), TFF_PLAIN_IDENTIFIER),
9505 : : (HOST_WIDE_INT) (uintptr_t) binfo);
9506 : 136 : if (binfo != igo)
9507 : : {
9508 : 36 : fprintf (stream, "alternative-path\n");
9509 : 36 : return igo;
9510 : : }
9511 : 100 : igo = TREE_CHAIN (binfo);
9512 : :
9513 : 100 : fprintf (stream, HOST_WIDE_INT_PRINT_DEC,
9514 : 100 : tree_to_shwi (BINFO_OFFSET (binfo)));
9515 : 100 : if (is_empty_class (BINFO_TYPE (binfo)))
9516 : 24 : fprintf (stream, " empty");
9517 : 76 : else if (CLASSTYPE_NEARLY_EMPTY_P (BINFO_TYPE (binfo)))
9518 : 16 : fprintf (stream, " nearly-empty");
9519 : 100 : if (BINFO_VIRTUAL_P (binfo))
9520 : 36 : fprintf (stream, " virtual");
9521 : 100 : fprintf (stream, "\n");
9522 : :
9523 : 100 : if (BINFO_PRIMARY_P (binfo))
9524 : : {
9525 : 20 : indented = maybe_indent_hierarchy (stream, indent + 3, indented);
9526 : 40 : fprintf (stream, " primary-for %s (0x" HOST_WIDE_INT_PRINT_HEX ")",
9527 : 20 : type_as_string (BINFO_TYPE (BINFO_INHERITANCE_CHAIN (binfo)),
9528 : : TFF_PLAIN_IDENTIFIER),
9529 : 20 : (HOST_WIDE_INT) (uintptr_t) BINFO_INHERITANCE_CHAIN (binfo));
9530 : : }
9531 : 100 : if (BINFO_LOST_PRIMARY_P (binfo))
9532 : : {
9533 : 12 : indented = maybe_indent_hierarchy (stream, indent + 3, indented);
9534 : 12 : fprintf (stream, " lost-primary");
9535 : : }
9536 : 100 : if (indented)
9537 : 32 : fprintf (stream, "\n");
9538 : :
9539 : 100 : if (!(flags & TDF_SLIM))
9540 : : {
9541 : 100 : int indented = 0;
9542 : :
9543 : 100 : if (BINFO_SUBVTT_INDEX (binfo))
9544 : : {
9545 : 28 : indented = maybe_indent_hierarchy (stream, indent + 3, indented);
9546 : 56 : fprintf (stream, " subvttidx=%s",
9547 : 28 : expr_as_string (BINFO_SUBVTT_INDEX (binfo),
9548 : : TFF_PLAIN_IDENTIFIER));
9549 : : }
9550 : 100 : if (BINFO_VPTR_INDEX (binfo))
9551 : : {
9552 : 40 : indented = maybe_indent_hierarchy (stream, indent + 3, indented);
9553 : 80 : fprintf (stream, " vptridx=%s",
9554 : 40 : expr_as_string (BINFO_VPTR_INDEX (binfo),
9555 : : TFF_PLAIN_IDENTIFIER));
9556 : : }
9557 : 100 : if (BINFO_VPTR_FIELD (binfo))
9558 : : {
9559 : 36 : indented = maybe_indent_hierarchy (stream, indent + 3, indented);
9560 : 72 : fprintf (stream, " vbaseoffset=%s",
9561 : 36 : expr_as_string (BINFO_VPTR_FIELD (binfo),
9562 : : TFF_PLAIN_IDENTIFIER));
9563 : : }
9564 : 100 : if (BINFO_VTABLE (binfo))
9565 : : {
9566 : 36 : indented = maybe_indent_hierarchy (stream, indent + 3, indented);
9567 : 36 : fprintf (stream, " vptr=%s",
9568 : 36 : expr_as_string (BINFO_VTABLE (binfo),
9569 : : TFF_PLAIN_IDENTIFIER));
9570 : : }
9571 : :
9572 : 100 : if (indented)
9573 : 68 : fprintf (stream, "\n");
9574 : : }
9575 : :
9576 : 204 : for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
9577 : 104 : igo = dump_class_hierarchy_r (stream, flags, base_binfo, igo, indent + 2);
9578 : :
9579 : : return igo;
9580 : : }
9581 : :
9582 : : /* Dump the BINFO hierarchy for T. */
9583 : :
9584 : : static void
9585 : 32 : dump_class_hierarchy_1 (FILE *stream, dump_flags_t flags, tree t)
9586 : : {
9587 : 32 : fprintf (stream, "Class %s\n", type_as_string (t, TFF_PLAIN_IDENTIFIER));
9588 : 128 : fprintf (stream, " size=" HOST_WIDE_INT_PRINT_UNSIGNED " align=%u\n",
9589 : 32 : tree_to_shwi (TYPE_SIZE (t)) / BITS_PER_UNIT,
9590 : 32 : TYPE_ALIGN (t) / BITS_PER_UNIT);
9591 : 32 : if (tree as_base = CLASSTYPE_AS_BASE (t))
9592 : 128 : fprintf (stream, " base size=" HOST_WIDE_INT_PRINT_UNSIGNED
9593 : : " base align=%u\n",
9594 : 32 : tree_to_shwi (TYPE_SIZE (as_base)) / BITS_PER_UNIT,
9595 : 32 : TYPE_ALIGN (as_base) / BITS_PER_UNIT);
9596 : 32 : dump_class_hierarchy_r (stream, flags, TYPE_BINFO (t), TYPE_BINFO (t), 0);
9597 : 32 : fprintf (stream, "\n");
9598 : 32 : }
9599 : :
9600 : : /* Debug interface to hierarchy dumping. */
9601 : :
9602 : : void
9603 : 0 : debug_class (tree t)
9604 : : {
9605 : 0 : dump_class_hierarchy_1 (stderr, TDF_SLIM, t);
9606 : 0 : }
9607 : :
9608 : : static void
9609 : 36143417 : dump_class_hierarchy (tree t)
9610 : : {
9611 : 36143417 : dump_flags_t flags;
9612 : 36143417 : if (FILE *stream = dump_begin (class_dump_id, &flags))
9613 : : {
9614 : 32 : dump_class_hierarchy_1 (stream, flags, t);
9615 : 32 : dump_end (class_dump_id, stream);
9616 : : }
9617 : 36143417 : }
9618 : :
9619 : : static void
9620 : 68 : dump_array (FILE * stream, tree decl)
9621 : : {
9622 : 68 : tree value;
9623 : 68 : unsigned HOST_WIDE_INT ix;
9624 : 68 : HOST_WIDE_INT elt;
9625 : 68 : tree size = TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (decl)));
9626 : :
9627 : 68 : elt = (tree_to_shwi (TYPE_SIZE (TREE_TYPE (TREE_TYPE (decl))))
9628 : : / BITS_PER_UNIT);
9629 : 68 : fprintf (stream, "%s:", decl_as_string (decl, TFF_PLAIN_IDENTIFIER));
9630 : 68 : fprintf (stream, " %s entries",
9631 : : expr_as_string (size_binop (PLUS_EXPR, size, size_one_node),
9632 : : TFF_PLAIN_IDENTIFIER));
9633 : 68 : fprintf (stream, "\n");
9634 : :
9635 : 692 : FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (DECL_INITIAL (decl)),
9636 : : ix, value)
9637 : 556 : fprintf (stream, "%-4ld %s\n", (long)(ix * elt),
9638 : : expr_as_string (value, TFF_PLAIN_IDENTIFIER));
9639 : 68 : }
9640 : :
9641 : : static void
9642 : 1831453 : dump_vtable (tree t, tree binfo, tree vtable)
9643 : : {
9644 : 1831453 : dump_flags_t flags;
9645 : 1831453 : FILE *stream = dump_begin (class_dump_id, &flags);
9646 : :
9647 : 1831453 : if (!stream)
9648 : 1831401 : return;
9649 : :
9650 : 52 : if (!(flags & TDF_SLIM))
9651 : : {
9652 : 52 : int ctor_vtbl_p = TYPE_BINFO (t) != binfo;
9653 : :
9654 : 128 : fprintf (stream, "%s for %s",
9655 : : ctor_vtbl_p ? "Construction vtable" : "Vtable",
9656 : 52 : type_as_string (BINFO_TYPE (binfo), TFF_PLAIN_IDENTIFIER));
9657 : 52 : if (ctor_vtbl_p)
9658 : : {
9659 : 28 : if (!BINFO_VIRTUAL_P (binfo))
9660 : 8 : fprintf (stream, " (0x" HOST_WIDE_INT_PRINT_HEX " instance)",
9661 : : (HOST_WIDE_INT) (uintptr_t) binfo);
9662 : 28 : fprintf (stream, " in %s", type_as_string (t, TFF_PLAIN_IDENTIFIER));
9663 : : }
9664 : 52 : fprintf (stream, "\n");
9665 : 52 : dump_array (stream, vtable);
9666 : 52 : fprintf (stream, "\n");
9667 : : }
9668 : :
9669 : 52 : dump_end (class_dump_id, stream);
9670 : : }
9671 : :
9672 : : static void
9673 : 182243 : dump_vtt (tree t, tree vtt)
9674 : : {
9675 : 182243 : dump_flags_t flags;
9676 : 182243 : FILE *stream = dump_begin (class_dump_id, &flags);
9677 : :
9678 : 182243 : if (!stream)
9679 : 182227 : return;
9680 : :
9681 : 16 : if (!(flags & TDF_SLIM))
9682 : : {
9683 : 16 : fprintf (stream, "VTT for %s\n",
9684 : : type_as_string (t, TFF_PLAIN_IDENTIFIER));
9685 : 16 : dump_array (stream, vtt);
9686 : 16 : fprintf (stream, "\n");
9687 : : }
9688 : :
9689 : 16 : dump_end (class_dump_id, stream);
9690 : : }
9691 : :
9692 : : /* Dump a function or thunk and its thunkees. */
9693 : :
9694 : : static void
9695 : 0 : dump_thunk (FILE *stream, int indent, tree thunk)
9696 : : {
9697 : 0 : static const char spaces[] = " ";
9698 : 0 : tree name = DECL_NAME (thunk);
9699 : 0 : tree thunks;
9700 : :
9701 : 0 : fprintf (stream, "%.*s%p %s %s", indent, spaces,
9702 : : (void *)thunk,
9703 : 0 : !DECL_THUNK_P (thunk) ? "function"
9704 : 0 : : DECL_THIS_THUNK_P (thunk) ? "this-thunk" : "covariant-thunk",
9705 : 0 : name ? IDENTIFIER_POINTER (name) : "<unset>");
9706 : 0 : if (DECL_THUNK_P (thunk))
9707 : : {
9708 : 0 : HOST_WIDE_INT fixed_adjust = THUNK_FIXED_OFFSET (thunk);
9709 : 0 : tree virtual_adjust = THUNK_VIRTUAL_OFFSET (thunk);
9710 : :
9711 : 0 : fprintf (stream, " fixed=" HOST_WIDE_INT_PRINT_DEC, fixed_adjust);
9712 : 0 : if (!virtual_adjust)
9713 : : /*NOP*/;
9714 : 0 : else if (DECL_THIS_THUNK_P (thunk))
9715 : 0 : fprintf (stream, " vcall=" HOST_WIDE_INT_PRINT_DEC,
9716 : : tree_to_shwi (virtual_adjust));
9717 : : else
9718 : 0 : fprintf (stream, " vbase=" HOST_WIDE_INT_PRINT_DEC "(%s)",
9719 : 0 : tree_to_shwi (BINFO_VPTR_FIELD (virtual_adjust)),
9720 : 0 : type_as_string (BINFO_TYPE (virtual_adjust), TFF_SCOPE));
9721 : 0 : if (THUNK_ALIAS (thunk))
9722 : 0 : fprintf (stream, " alias to %p", (void *)THUNK_ALIAS (thunk));
9723 : : }
9724 : 0 : fprintf (stream, "\n");
9725 : 0 : for (thunks = DECL_THUNKS (thunk); thunks; thunks = TREE_CHAIN (thunks))
9726 : 0 : dump_thunk (stream, indent + 2, thunks);
9727 : 0 : }
9728 : :
9729 : : /* Dump the thunks for FN. */
9730 : :
9731 : : void
9732 : 0 : debug_thunks (tree fn)
9733 : : {
9734 : 0 : dump_thunk (stderr, 0, fn);
9735 : 0 : }
9736 : :
9737 : : /* Virtual function table initialization. */
9738 : :
9739 : : /* Create all the necessary vtables for T and its base classes. */
9740 : :
9741 : : static void
9742 : 36143417 : finish_vtbls (tree t)
9743 : : {
9744 : 36143417 : tree vbase;
9745 : 36143417 : vec<constructor_elt, va_gc> *v = NULL;
9746 : 36143417 : tree vtable = BINFO_VTABLE (TYPE_BINFO (t));
9747 : :
9748 : : /* We lay out the primary and secondary vtables in one contiguous
9749 : : vtable. The primary vtable is first, followed by the non-virtual
9750 : : secondary vtables in inheritance graph order. */
9751 : 36143417 : accumulate_vtbl_inits (TYPE_BINFO (t), TYPE_BINFO (t), TYPE_BINFO (t),
9752 : : vtable, t, &v);
9753 : :
9754 : : /* Then come the virtual bases, also in inheritance graph order. */
9755 : 95183479 : for (vbase = TYPE_BINFO (t); vbase; vbase = TREE_CHAIN (vbase))
9756 : : {
9757 : 59040062 : if (!BINFO_VIRTUAL_P (vbase))
9758 : 58843720 : continue;
9759 : 196342 : accumulate_vtbl_inits (vbase, vbase, TYPE_BINFO (t), vtable, t, &v);
9760 : : }
9761 : :
9762 : 36143417 : if (BINFO_VTABLE (TYPE_BINFO (t)))
9763 : 1580921 : initialize_vtable (TYPE_BINFO (t), v);
9764 : 36143417 : }
9765 : :
9766 : : /* Initialize the vtable for BINFO with the INITS. */
9767 : :
9768 : : static void
9769 : 1580921 : initialize_vtable (tree binfo, vec<constructor_elt, va_gc> *inits)
9770 : : {
9771 : 1580921 : tree decl;
9772 : :
9773 : 3161842 : layout_vtable_decl (binfo, vec_safe_length (inits));
9774 : 1580921 : decl = get_vtbl_decl_for_binfo (binfo);
9775 : 1580921 : initialize_artificial_var (decl, inits);
9776 : 1580921 : dump_vtable (BINFO_TYPE (binfo), binfo, decl);
9777 : 1580921 : }
9778 : :
9779 : : /* Build the VTT (virtual table table) for T.
9780 : : A class requires a VTT if it has virtual bases.
9781 : :
9782 : : This holds
9783 : : 1 - primary virtual pointer for complete object T
9784 : : 2 - secondary VTTs for each direct non-virtual base of T which requires a
9785 : : VTT
9786 : : 3 - secondary virtual pointers for each direct or indirect base of T which
9787 : : has virtual bases or is reachable via a virtual path from T.
9788 : : 4 - secondary VTTs for each direct or indirect virtual base of T.
9789 : :
9790 : : Secondary VTTs look like complete object VTTs without part 4. */
9791 : :
9792 : : static void
9793 : 36143417 : build_vtt (tree t)
9794 : : {
9795 : 36143417 : tree type;
9796 : 36143417 : tree vtt;
9797 : 36143417 : tree index;
9798 : 36143417 : vec<constructor_elt, va_gc> *inits;
9799 : :
9800 : : /* Build up the initializers for the VTT. */
9801 : 36143417 : inits = NULL;
9802 : 36143417 : index = size_zero_node;
9803 : 36143417 : build_vtt_inits (TYPE_BINFO (t), t, &inits, &index);
9804 : :
9805 : : /* If we didn't need a VTT, we're done. */
9806 : 36143417 : if (!inits)
9807 : 35961174 : return;
9808 : :
9809 : : /* Figure out the type of the VTT. */
9810 : 364486 : type = build_array_of_n_type (const_ptr_type_node,
9811 : 182243 : inits->length ());
9812 : :
9813 : : /* Now, build the VTT object itself. */
9814 : 182243 : vtt = build_vtable (t, mangle_vtt_for_type (t), type);
9815 : 182243 : initialize_artificial_var (vtt, inits);
9816 : : /* Add the VTT to the vtables list. */
9817 : 182243 : DECL_CHAIN (vtt) = DECL_CHAIN (CLASSTYPE_VTABLES (t));
9818 : 182243 : DECL_CHAIN (CLASSTYPE_VTABLES (t)) = vtt;
9819 : :
9820 : 182243 : dump_vtt (t, vtt);
9821 : : }
9822 : :
9823 : : /* When building a secondary VTT, BINFO_VTABLE is set to a TREE_LIST with
9824 : : PURPOSE the RTTI_BINFO, VALUE the real vtable pointer for this binfo,
9825 : : and CHAIN the vtable pointer for this binfo after construction is
9826 : : complete. VALUE can also be another BINFO, in which case we recurse. */
9827 : :
9828 : : static tree
9829 : 1103003 : binfo_ctor_vtable (tree binfo)
9830 : : {
9831 : 1175520 : tree vt;
9832 : :
9833 : 1175520 : while (1)
9834 : : {
9835 : 1175520 : vt = BINFO_VTABLE (binfo);
9836 : 1175520 : if (TREE_CODE (vt) == TREE_LIST)
9837 : 739955 : vt = TREE_VALUE (vt);
9838 : 1175520 : if (TREE_CODE (vt) == TREE_BINFO)
9839 : : binfo = vt;
9840 : : else
9841 : : break;
9842 : : }
9843 : :
9844 : 1103003 : return vt;
9845 : : }
9846 : :
9847 : : /* Data for secondary VTT initialization. */
9848 : : struct secondary_vptr_vtt_init_data
9849 : : {
9850 : : /* Is this the primary VTT? */
9851 : : bool top_level_p;
9852 : :
9853 : : /* Current index into the VTT. */
9854 : : tree index;
9855 : :
9856 : : /* Vector of initializers built up. */
9857 : : vec<constructor_elt, va_gc> *inits;
9858 : :
9859 : : /* The type being constructed by this secondary VTT. */
9860 : : tree type_being_constructed;
9861 : : };
9862 : :
9863 : : /* Recursively build the VTT-initializer for BINFO (which is in the
9864 : : hierarchy dominated by T). INITS points to the end of the initializer
9865 : : list to date. INDEX is the VTT index where the next element will be
9866 : : replaced. Iff BINFO is the binfo for T, this is the top level VTT (i.e.
9867 : : not a subvtt for some base of T). When that is so, we emit the sub-VTTs
9868 : : for virtual bases of T. When it is not so, we build the constructor
9869 : : vtables for the BINFO-in-T variant. */
9870 : :
9871 : : static void
9872 : 36578955 : build_vtt_inits (tree binfo, tree t, vec<constructor_elt, va_gc> **inits,
9873 : : tree *index)
9874 : : {
9875 : 36578955 : int i;
9876 : 36578955 : tree b;
9877 : 36578955 : tree init;
9878 : 36578955 : secondary_vptr_vtt_init_data data;
9879 : 36578955 : int top_level_p = SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), t);
9880 : :
9881 : : /* We only need VTTs for subobjects with virtual bases. */
9882 : 36578955 : if (!CLASSTYPE_VBASECLASSES (BINFO_TYPE (binfo)))
9883 : 36146180 : return;
9884 : :
9885 : : /* We need to use a construction vtable if this is not the primary
9886 : : VTT. */
9887 : 432775 : if (!top_level_p)
9888 : : {
9889 : 250532 : build_ctor_vtbl_group (binfo, t);
9890 : :
9891 : : /* Record the offset in the VTT where this sub-VTT can be found. */
9892 : 250532 : BINFO_SUBVTT_INDEX (binfo) = *index;
9893 : : }
9894 : :
9895 : : /* Add the address of the primary vtable for the complete object. */
9896 : 432775 : init = binfo_ctor_vtable (binfo);
9897 : 432775 : CONSTRUCTOR_APPEND_ELT (*inits, NULL_TREE, init);
9898 : 432775 : if (top_level_p)
9899 : : {
9900 : 182243 : gcc_assert (!BINFO_VPTR_INDEX (binfo));
9901 : 182243 : BINFO_VPTR_INDEX (binfo) = *index;
9902 : : }
9903 : 432775 : *index = size_binop (PLUS_EXPR, *index, TYPE_SIZE_UNIT (ptr_type_node));
9904 : :
9905 : : /* Recursively add the secondary VTTs for non-virtual bases. */
9906 : 937791 : for (i = 0; BINFO_BASE_ITERATE (binfo, i, b); ++i)
9907 : 505016 : if (!BINFO_VIRTUAL_P (b))
9908 : 239196 : build_vtt_inits (b, t, inits, index);
9909 : :
9910 : : /* Add secondary virtual pointers for all subobjects of BINFO with
9911 : : either virtual bases or reachable along a virtual path, except
9912 : : subobjects that are non-virtual primary bases. */
9913 : 432775 : data.top_level_p = top_level_p;
9914 : 432775 : data.index = *index;
9915 : 432775 : data.inits = *inits;
9916 : 432775 : data.type_being_constructed = BINFO_TYPE (binfo);
9917 : :
9918 : 432775 : dfs_walk_once (binfo, dfs_build_secondary_vptr_vtt_inits, NULL, &data);
9919 : :
9920 : 432775 : *index = data.index;
9921 : :
9922 : : /* data.inits might have grown as we added secondary virtual pointers.
9923 : : Make sure our caller knows about the new vector. */
9924 : 432775 : *inits = data.inits;
9925 : :
9926 : 432775 : if (top_level_p)
9927 : : /* Add the secondary VTTs for virtual bases in inheritance graph
9928 : : order. */
9929 : 978939 : for (b = TYPE_BINFO (BINFO_TYPE (binfo)); b; b = TREE_CHAIN (b))
9930 : : {
9931 : 796696 : if (!BINFO_VIRTUAL_P (b))
9932 : 600354 : continue;
9933 : :
9934 : 196342 : build_vtt_inits (b, t, inits, index);
9935 : : }
9936 : : else
9937 : : /* Remove the ctor vtables we created. */
9938 : 250532 : dfs_walk_all (binfo, dfs_fixup_binfo_vtbls, NULL, binfo);
9939 : : }
9940 : :
9941 : : /* Called from build_vtt_inits via dfs_walk. BINFO is the binfo for the base
9942 : : in most derived. DATA is a SECONDARY_VPTR_VTT_INIT_DATA structure. */
9943 : :
9944 : : static tree
9945 : 1763655 : dfs_build_secondary_vptr_vtt_inits (tree binfo, void *data_)
9946 : : {
9947 : 1763655 : secondary_vptr_vtt_init_data *data = (secondary_vptr_vtt_init_data *)data_;
9948 : :
9949 : : /* We don't care about bases that don't have vtables. */
9950 : 1763655 : if (!TYPE_VFIELD (BINFO_TYPE (binfo)))
9951 : : return dfs_skip_bases;
9952 : :
9953 : : /* We're only interested in proper subobjects of the type being
9954 : : constructed. */
9955 : 1737502 : if (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), data->type_being_constructed))
9956 : : return NULL_TREE;
9957 : :
9958 : : /* We're only interested in bases with virtual bases or reachable
9959 : : via a virtual path from the type being constructed. */
9960 : 2129826 : if (!(CLASSTYPE_VBASECLASSES (BINFO_TYPE (binfo))
9961 : 825099 : || binfo_via_virtual (binfo, data->type_being_constructed)))
9962 : : return dfs_skip_bases;
9963 : :
9964 : : /* We're not interested in non-virtual primary bases. */
9965 : 1304088 : if (!BINFO_VIRTUAL_P (binfo) && BINFO_PRIMARY_P (binfo))
9966 : : return NULL_TREE;
9967 : :
9968 : : /* Record the index where this secondary vptr can be found. */
9969 : 670228 : if (data->top_level_p)
9970 : : {
9971 : 253322 : gcc_assert (!BINFO_VPTR_INDEX (binfo));
9972 : 253322 : BINFO_VPTR_INDEX (binfo) = data->index;
9973 : :
9974 : 253322 : if (BINFO_VIRTUAL_P (binfo))
9975 : : {
9976 : : /* It's a primary virtual base, and this is not a
9977 : : construction vtable. Find the base this is primary of in
9978 : : the inheritance graph, and use that base's vtable
9979 : : now. */
9980 : 203274 : while (BINFO_PRIMARY_P (binfo))
9981 : 10283 : binfo = BINFO_INHERITANCE_CHAIN (binfo);
9982 : : }
9983 : : }
9984 : :
9985 : : /* Add the initializer for the secondary vptr itself. */
9986 : 670228 : CONSTRUCTOR_APPEND_ELT (data->inits, NULL_TREE, binfo_ctor_vtable (binfo));
9987 : :
9988 : : /* Advance the vtt index. */
9989 : 670228 : data->index = size_binop (PLUS_EXPR, data->index,
9990 : : TYPE_SIZE_UNIT (ptr_type_node));
9991 : :
9992 : 670228 : return NULL_TREE;
9993 : : }
9994 : :
9995 : : /* Called from build_vtt_inits via dfs_walk. After building
9996 : : constructor vtables and generating the sub-vtt from them, we need
9997 : : to restore the BINFO_VTABLES that were scribbled on. DATA is the
9998 : : binfo of the base whose sub vtt was generated. */
9999 : :
10000 : : static tree
10001 : 1127774 : dfs_fixup_binfo_vtbls (tree binfo, void* data)
10002 : : {
10003 : 1127774 : tree vtable = BINFO_VTABLE (binfo);
10004 : :
10005 : 1127774 : if (!TYPE_CONTAINS_VPTR_P (BINFO_TYPE (binfo)))
10006 : : /* If this class has no vtable, none of its bases do. */
10007 : : return dfs_skip_bases;
10008 : :
10009 : 1028971 : if (!vtable)
10010 : : /* This might be a primary base, so have no vtable in this
10011 : : hierarchy. */
10012 : : return NULL_TREE;
10013 : :
10014 : : /* If we scribbled the construction vtable vptr into BINFO, clear it
10015 : : out now. */
10016 : 707412 : if (TREE_CODE (vtable) == TREE_LIST
10017 : 707412 : && (TREE_PURPOSE (vtable) == (tree) data))
10018 : 667438 : BINFO_VTABLE (binfo) = TREE_CHAIN (vtable);
10019 : :
10020 : : return NULL_TREE;
10021 : : }
10022 : :
10023 : : /* Build the construction vtable group for BINFO which is in the
10024 : : hierarchy dominated by T. */
10025 : :
10026 : : static void
10027 : 250532 : build_ctor_vtbl_group (tree binfo, tree t)
10028 : : {
10029 : 250532 : tree type;
10030 : 250532 : tree vtbl;
10031 : 250532 : tree id;
10032 : 250532 : tree vbase;
10033 : 250532 : vec<constructor_elt, va_gc> *v;
10034 : :
10035 : : /* See if we've already created this construction vtable group. */
10036 : 250532 : id = mangle_ctor_vtbl_for_type (t, binfo);
10037 : 250532 : if (get_global_binding (id))
10038 : 0 : return;
10039 : :
10040 : 250532 : gcc_assert (!SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), t));
10041 : : /* Build a version of VTBL (with the wrong type) for use in
10042 : : constructing the addresses of secondary vtables in the
10043 : : construction vtable group. */
10044 : 250532 : vtbl = build_vtable (t, id, ptr_type_node);
10045 : :
10046 : : /* Don't export construction vtables from shared libraries. Even on
10047 : : targets that don't support hidden visibility, this tells
10048 : : can_refer_decl_in_current_unit_p not to assume that it's safe to
10049 : : access from a different compilation unit (bz 54314). */
10050 : 250532 : DECL_VISIBILITY (vtbl) = VISIBILITY_HIDDEN;
10051 : 250532 : DECL_VISIBILITY_SPECIFIED (vtbl) = true;
10052 : :
10053 : 250532 : v = NULL;
10054 : 250532 : accumulate_vtbl_inits (binfo, TYPE_BINFO (TREE_TYPE (binfo)),
10055 : : binfo, vtbl, t, &v);
10056 : :
10057 : : /* Add the vtables for each of our virtual bases using the vbase in T
10058 : : binfo. */
10059 : 250532 : for (vbase = TYPE_BINFO (BINFO_TYPE (binfo));
10060 : 1222528 : vbase;
10061 : 971996 : vbase = TREE_CHAIN (vbase))
10062 : : {
10063 : 971996 : tree b;
10064 : :
10065 : 971996 : if (!BINFO_VIRTUAL_P (vbase))
10066 : 578288 : continue;
10067 : 393708 : b = copied_binfo (vbase, binfo);
10068 : :
10069 : 393708 : accumulate_vtbl_inits (b, vbase, binfo, vtbl, t, &v);
10070 : : }
10071 : :
10072 : : /* Figure out the type of the construction vtable. */
10073 : 250532 : type = build_array_of_n_type (vtable_entry_type, v->length ());
10074 : 250532 : layout_type (type);
10075 : 250532 : TREE_TYPE (vtbl) = type;
10076 : 250532 : DECL_SIZE (vtbl) = DECL_SIZE_UNIT (vtbl) = NULL_TREE;
10077 : 250532 : layout_decl (vtbl, 0);
10078 : :
10079 : : /* Initialize the construction vtable. */
10080 : 250532 : CLASSTYPE_VTABLES (t) = chainon (CLASSTYPE_VTABLES (t), vtbl);
10081 : 250532 : initialize_artificial_var (vtbl, v);
10082 : 250532 : dump_vtable (t, binfo, vtbl);
10083 : : }
10084 : :
10085 : : /* Add the vtbl initializers for BINFO (and its bases other than
10086 : : non-virtual primaries) to the list of INITS. BINFO is in the
10087 : : hierarchy dominated by T. RTTI_BINFO is the binfo within T of
10088 : : the constructor the vtbl inits should be accumulated for. (If this
10089 : : is the complete object vtbl then RTTI_BINFO will be TYPE_BINFO (T).)
10090 : : ORIG_BINFO is the binfo for this object within BINFO_TYPE (RTTI_BINFO).
10091 : : BINFO is the active base equivalent of ORIG_BINFO in the inheritance
10092 : : graph of T. Both BINFO and ORIG_BINFO will have the same BINFO_TYPE,
10093 : : but are not necessarily the same in terms of layout. */
10094 : :
10095 : : static void
10096 : 40257216 : accumulate_vtbl_inits (tree binfo,
10097 : : tree orig_binfo,
10098 : : tree rtti_binfo,
10099 : : tree vtbl,
10100 : : tree t,
10101 : : vec<constructor_elt, va_gc> **inits)
10102 : : {
10103 : 40257216 : int i;
10104 : 40257216 : tree base_binfo;
10105 : 40257216 : int ctor_vtbl_p = !SAME_BINFO_TYPE_P (BINFO_TYPE (rtti_binfo), t);
10106 : :
10107 : 40257216 : gcc_assert (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), BINFO_TYPE (orig_binfo)));
10108 : :
10109 : : /* If it doesn't have a vptr, we don't do anything. */
10110 : 40257216 : if (!TYPE_CONTAINS_VPTR_P (BINFO_TYPE (binfo)))
10111 : : return;
10112 : :
10113 : : /* If we're building a construction vtable, we're not interested in
10114 : : subobjects that don't require construction vtables. */
10115 : 5125368 : if (ctor_vtbl_p
10116 : 947959 : && !CLASSTYPE_VBASECLASSES (BINFO_TYPE (binfo))
10117 : 5593699 : && !binfo_via_virtual (orig_binfo, BINFO_TYPE (rtti_binfo)))
10118 : : return;
10119 : :
10120 : : /* Build the initializers for the BINFO-in-T vtable. */
10121 : 5125114 : dfs_accumulate_vtbl_inits (binfo, orig_binfo, rtti_binfo, vtbl, t, inits);
10122 : :
10123 : : /* Walk the BINFO and its bases. We walk in preorder so that as we
10124 : : initialize each vtable we can figure out at what offset the
10125 : : secondary vtable lies from the primary vtable. We can't use
10126 : : dfs_walk here because we need to iterate through bases of BINFO
10127 : : and RTTI_BINFO simultaneously. */
10128 : 14297294 : for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
10129 : : {
10130 : : /* Skip virtual bases. */
10131 : 4047066 : if (BINFO_VIRTUAL_P (base_binfo))
10132 : 773849 : continue;
10133 : 6546434 : accumulate_vtbl_inits (base_binfo,
10134 : 3273217 : BINFO_BASE_BINFO (orig_binfo, i),
10135 : : rtti_binfo, vtbl, t,
10136 : : inits);
10137 : : }
10138 : : }
10139 : :
10140 : : /* Called from accumulate_vtbl_inits. Adds the initializers for the
10141 : : BINFO vtable to L. */
10142 : :
10143 : : static void
10144 : 5125114 : dfs_accumulate_vtbl_inits (tree binfo,
10145 : : tree orig_binfo,
10146 : : tree rtti_binfo,
10147 : : tree orig_vtbl,
10148 : : tree t,
10149 : : vec<constructor_elt, va_gc> **l)
10150 : : {
10151 : 5125114 : tree vtbl = NULL_TREE;
10152 : 5125114 : int ctor_vtbl_p = !SAME_BINFO_TYPE_P (BINFO_TYPE (rtti_binfo), t);
10153 : 5125114 : int n_inits;
10154 : :
10155 : 5125114 : if (ctor_vtbl_p
10156 : 6072819 : && BINFO_VIRTUAL_P (orig_binfo) && BINFO_PRIMARY_P (orig_binfo))
10157 : : {
10158 : : /* In the hierarchy of BINFO_TYPE (RTTI_BINFO), this is a
10159 : : primary virtual base. If it is not the same primary in
10160 : : the hierarchy of T, we'll need to generate a ctor vtable
10161 : : for it, to place at its location in T. If it is the same
10162 : : primary, we still need a VTT entry for the vtable, but it
10163 : : should point to the ctor vtable for the base it is a
10164 : : primary for within the sub-hierarchy of RTTI_BINFO.
10165 : :
10166 : : There are three possible cases:
10167 : :
10168 : : 1) We are in the same place.
10169 : : 2) We are a primary base within a lost primary virtual base of
10170 : : RTTI_BINFO.
10171 : : 3) We are primary to something not a base of RTTI_BINFO. */
10172 : :
10173 : : tree b;
10174 : : tree last = NULL_TREE;
10175 : :
10176 : : /* First, look through the bases we are primary to for RTTI_BINFO
10177 : : or a virtual base. */
10178 : : b = binfo;
10179 : 74441 : while (BINFO_PRIMARY_P (b))
10180 : : {
10181 : 73906 : b = BINFO_INHERITANCE_CHAIN (b);
10182 : 73906 : last = b;
10183 : 146431 : if (BINFO_VIRTUAL_P (b) || b == rtti_binfo)
10184 : 72525 : goto found;
10185 : : }
10186 : : /* If we run out of primary links, keep looking down our
10187 : : inheritance chain; we might be an indirect primary. */
10188 : 1114 : for (b = last; b; b = BINFO_INHERITANCE_CHAIN (b))
10189 : 595 : if (BINFO_VIRTUAL_P (b) || b == rtti_binfo)
10190 : : break;
10191 : 519 : found:
10192 : :
10193 : : /* If we found RTTI_BINFO, this is case 1. If we found a virtual
10194 : : base B and it is a base of RTTI_BINFO, this is case 2. In
10195 : : either case, we share our vtable with LAST, i.e. the
10196 : : derived-most base within B of which we are a primary. */
10197 : 73060 : if (b == rtti_binfo
10198 : 73060 : || (b && binfo_for_vbase (BINFO_TYPE (b), BINFO_TYPE (rtti_binfo))))
10199 : : /* Just set our BINFO_VTABLE to point to LAST, as we may not have
10200 : : set LAST's BINFO_VTABLE yet. We'll extract the actual vptr in
10201 : : binfo_ctor_vtable after everything's been set up. */
10202 : : vtbl = last;
10203 : :
10204 : : /* Otherwise, this is case 3 and we get our own. */
10205 : : }
10206 : 5052054 : else if (!BINFO_NEW_VTABLE_MARKED (orig_binfo))
10207 : : return;
10208 : :
10209 : 2503002 : n_inits = vec_safe_length (*l);
10210 : :
10211 : 2503002 : if (!vtbl)
10212 : : {
10213 : 2431093 : tree index;
10214 : 2431093 : int non_fn_entries;
10215 : :
10216 : : /* Add the initializer for this vtable. */
10217 : 2431093 : build_vtbl_initializer (binfo, orig_binfo, t, rtti_binfo,
10218 : : &non_fn_entries, l);
10219 : :
10220 : : /* Figure out the position to which the VPTR should point. */
10221 : 2431093 : vtbl = build1 (ADDR_EXPR, vtbl_ptr_type_node, orig_vtbl);
10222 : 2431093 : index = size_binop (MULT_EXPR,
10223 : : TYPE_SIZE_UNIT (vtable_entry_type),
10224 : : size_int (non_fn_entries + n_inits));
10225 : 2431093 : vtbl = fold_build_pointer_plus (vtbl, index);
10226 : : }
10227 : :
10228 : 2503002 : if (ctor_vtbl_p)
10229 : : /* For a construction vtable, we can't overwrite BINFO_VTABLE.
10230 : : So, we make a TREE_LIST. Later, dfs_fixup_binfo_vtbls will
10231 : : straighten this out. */
10232 : 667438 : BINFO_VTABLE (binfo) = tree_cons (rtti_binfo, vtbl, BINFO_VTABLE (binfo));
10233 : 1835564 : else if (BINFO_PRIMARY_P (binfo) && BINFO_VIRTUAL_P (binfo))
10234 : : /* Throw away any unneeded intializers. */
10235 : 8577 : (*l)->truncate (n_inits);
10236 : : else
10237 : : /* For an ordinary vtable, set BINFO_VTABLE. */
10238 : 1826987 : BINFO_VTABLE (binfo) = vtbl;
10239 : : }
10240 : :
10241 : : static GTY(()) tree abort_fndecl_addr;
10242 : : static GTY(()) tree dvirt_fn;
10243 : :
10244 : : /* Construct the initializer for BINFO's virtual function table. BINFO
10245 : : is part of the hierarchy dominated by T. If we're building a
10246 : : construction vtable, the ORIG_BINFO is the binfo we should use to
10247 : : find the actual function pointers to put in the vtable - but they
10248 : : can be overridden on the path to most-derived in the graph that
10249 : : ORIG_BINFO belongs. Otherwise,
10250 : : ORIG_BINFO should be the same as BINFO. The RTTI_BINFO is the
10251 : : BINFO that should be indicated by the RTTI information in the
10252 : : vtable; it will be a base class of T, rather than T itself, if we
10253 : : are building a construction vtable.
10254 : :
10255 : : The value returned is a TREE_LIST suitable for wrapping in a
10256 : : CONSTRUCTOR to use as the DECL_INITIAL for a vtable. If
10257 : : NON_FN_ENTRIES_P is not NULL, *NON_FN_ENTRIES_P is set to the
10258 : : number of non-function entries in the vtable.
10259 : :
10260 : : It might seem that this function should never be called with a
10261 : : BINFO for which BINFO_PRIMARY_P holds, the vtable for such a
10262 : : base is always subsumed by a derived class vtable. However, when
10263 : : we are building construction vtables, we do build vtables for
10264 : : primary bases; we need these while the primary base is being
10265 : : constructed. */
10266 : :
10267 : : static void
10268 : 2431093 : build_vtbl_initializer (tree binfo,
10269 : : tree orig_binfo,
10270 : : tree t,
10271 : : tree rtti_binfo,
10272 : : int* non_fn_entries_p,
10273 : : vec<constructor_elt, va_gc> **inits)
10274 : : {
10275 : 2431093 : tree v;
10276 : 2431093 : vtbl_init_data vid;
10277 : 2431093 : unsigned ix, jx;
10278 : 2431093 : tree vbinfo;
10279 : 2431093 : vec<tree, va_gc> *vbases;
10280 : 2431093 : constructor_elt *e;
10281 : :
10282 : : /* Initialize VID. */
10283 : 2431093 : memset (&vid, 0, sizeof (vid));
10284 : 2431093 : vid.binfo = binfo;
10285 : 2431093 : vid.derived = t;
10286 : 2431093 : vid.rtti_binfo = rtti_binfo;
10287 : 2431093 : vid.primary_vtbl_p = SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), t);
10288 : 2431093 : vid.ctor_vtbl_p = !SAME_BINFO_TYPE_P (BINFO_TYPE (rtti_binfo), t);
10289 : 2431093 : vid.generate_vcall_entries = true;
10290 : : /* The first vbase or vcall offset is at index -3 in the vtable. */
10291 : 2431093 : vid.index = ssize_int(-3 * TARGET_VTABLE_DATA_ENTRY_DISTANCE);
10292 : :
10293 : : /* Add entries to the vtable for RTTI. */
10294 : 2431093 : build_rtti_vtbl_entries (binfo, &vid);
10295 : :
10296 : : /* Create an array for keeping track of the functions we've
10297 : : processed. When we see multiple functions with the same
10298 : : signature, we share the vcall offsets. */
10299 : 2431093 : vec_alloc (vid.fns, 32);
10300 : : /* Add the vcall and vbase offset entries. */
10301 : 2431093 : build_vcall_and_vbase_vtbl_entries (binfo, &vid);
10302 : :
10303 : : /* Clear BINFO_VTABLE_PATH_MARKED; it's set by
10304 : : build_vbase_offset_vtbl_entries. */
10305 : 2431093 : for (vbases = CLASSTYPE_VBASECLASSES (t), ix = 0;
10306 : 8820194 : vec_safe_iterate (vbases, ix, &vbinfo); ix++)
10307 : 6389101 : BINFO_VTABLE_PATH_MARKED (vbinfo) = 0;
10308 : :
10309 : : /* If the target requires padding between data entries, add that now. */
10310 : 2431093 : if (TARGET_VTABLE_DATA_ENTRY_DISTANCE > 1)
10311 : : {
10312 : : int n_entries = vec_safe_length (vid.inits);
10313 : :
10314 : : vec_safe_grow (vid.inits, TARGET_VTABLE_DATA_ENTRY_DISTANCE * n_entries,
10315 : : true);
10316 : :
10317 : : /* Move data entries into their new positions and add padding
10318 : : after the new positions. Iterate backwards so we don't
10319 : : overwrite entries that we would need to process later. */
10320 : : for (ix = n_entries - 1;
10321 : : vid.inits->iterate (ix, &e);
10322 : : ix--)
10323 : : {
10324 : : int j;
10325 : : int new_position = (TARGET_VTABLE_DATA_ENTRY_DISTANCE * ix
10326 : : + (TARGET_VTABLE_DATA_ENTRY_DISTANCE - 1));
10327 : :
10328 : : (*vid.inits)[new_position] = *e;
10329 : :
10330 : : for (j = 1; j < TARGET_VTABLE_DATA_ENTRY_DISTANCE; ++j)
10331 : : {
10332 : : constructor_elt *f = &(*vid.inits)[new_position - j];
10333 : : f->index = NULL_TREE;
10334 : : f->value = build1 (NOP_EXPR, vtable_entry_type,
10335 : : null_pointer_node);
10336 : : }
10337 : : }
10338 : : }
10339 : :
10340 : 2431093 : if (non_fn_entries_p)
10341 : 4862186 : *non_fn_entries_p = vec_safe_length (vid.inits);
10342 : :
10343 : : /* The initializers for virtual functions were built up in reverse
10344 : : order. Straighten them out and add them to the running list in one
10345 : : step. */
10346 : 2431093 : jx = vec_safe_length (*inits);
10347 : 2431093 : vec_safe_grow (*inits, jx + vid.inits->length (), true);
10348 : :
10349 : 2431093 : for (ix = vid.inits->length () - 1;
10350 : 9921605 : vid.inits->iterate (ix, &e);
10351 : 7490512 : ix--, jx++)
10352 : 7490512 : (**inits)[jx] = *e;
10353 : :
10354 : : /* Go through all the ordinary virtual functions, building up
10355 : : initializers. */
10356 : 13034497 : for (v = BINFO_VIRTUALS (orig_binfo); v; v = TREE_CHAIN (v))
10357 : : {
10358 : 10603404 : tree delta;
10359 : 10603404 : tree vcall_index;
10360 : 10603404 : tree fn, fn_original;
10361 : 10603404 : tree init = NULL_TREE;
10362 : :
10363 : 10603404 : fn = BV_FN (v);
10364 : 10603404 : fn_original = fn;
10365 : 10603404 : if (DECL_THUNK_P (fn))
10366 : : {
10367 : 617 : if (!DECL_NAME (fn))
10368 : 270 : finish_thunk (fn);
10369 : 617 : if (THUNK_ALIAS (fn))
10370 : : {
10371 : 0 : fn = THUNK_ALIAS (fn);
10372 : 0 : BV_FN (v) = fn;
10373 : : }
10374 : 617 : fn_original = THUNK_TARGET (fn);
10375 : : }
10376 : :
10377 : : /* If the only definition of this function signature along our
10378 : : primary base chain is from a lost primary, this vtable slot will
10379 : : never be used, so just zero it out. This is important to avoid
10380 : : requiring extra thunks which cannot be generated with the function.
10381 : :
10382 : : We first check this in update_vtable_entry_for_fn, so we handle
10383 : : restored primary bases properly; we also need to do it here so we
10384 : : zero out unused slots in ctor vtables, rather than filling them
10385 : : with erroneous values (though harmless, apart from relocation
10386 : : costs). */
10387 : 10603404 : if (BV_LOST_PRIMARY (v))
10388 : 868 : init = size_zero_node;
10389 : :
10390 : 868 : if (! init)
10391 : : {
10392 : : /* Pull the offset for `this', and the function to call, out of
10393 : : the list. */
10394 : 10602536 : delta = BV_DELTA (v);
10395 : 10602536 : vcall_index = BV_VCALL_INDEX (v);
10396 : :
10397 : 10602536 : gcc_assert (TREE_CODE (delta) == INTEGER_CST);
10398 : 10602536 : gcc_assert (TREE_CODE (fn) == FUNCTION_DECL);
10399 : :
10400 : : /* You can't call an abstract virtual function; it's abstract.
10401 : : So, we replace these functions with __pure_virtual. */
10402 : 10602536 : if (DECL_PURE_VIRTUAL_P (fn_original))
10403 : : {
10404 : 609350 : fn = abort_fndecl;
10405 : 609350 : if (!TARGET_VTABLE_USES_DESCRIPTORS)
10406 : : {
10407 : 609350 : if (abort_fndecl_addr == NULL)
10408 : 13206 : abort_fndecl_addr
10409 : 13206 : = fold_convert (vfunc_ptr_type_node,
10410 : : build_fold_addr_expr (fn));
10411 : 609350 : init = abort_fndecl_addr;
10412 : : }
10413 : : }
10414 : : /* Likewise for deleted virtuals. */
10415 : 9993186 : else if (DECL_DELETED_FN (fn_original))
10416 : : {
10417 : 47 : if (!dvirt_fn)
10418 : : {
10419 : 20 : tree name = get_identifier ("__cxa_deleted_virtual");
10420 : 20 : dvirt_fn = get_global_binding (name);
10421 : 20 : if (!dvirt_fn)
10422 : 20 : dvirt_fn = push_library_fn
10423 : 20 : (name,
10424 : : build_function_type_list (void_type_node, NULL_TREE),
10425 : : NULL_TREE, ECF_NORETURN | ECF_COLD);
10426 : : }
10427 : 47 : fn = dvirt_fn;
10428 : 47 : if (!TARGET_VTABLE_USES_DESCRIPTORS)
10429 : 47 : init = fold_convert (vfunc_ptr_type_node,
10430 : : build_fold_addr_expr (fn));
10431 : : }
10432 : : else
10433 : : {
10434 : 9993139 : if (!integer_zerop (delta) || vcall_index)
10435 : : {
10436 : 1021508 : fn = make_thunk (fn, /*this_adjusting=*/1,
10437 : : delta, vcall_index);
10438 : 1021508 : if (!DECL_NAME (fn))
10439 : 476014 : finish_thunk (fn);
10440 : : }
10441 : : /* Take the address of the function, considering it to be of an
10442 : : appropriate generic type. */
10443 : 9993139 : if (!TARGET_VTABLE_USES_DESCRIPTORS)
10444 : 9993139 : init = fold_convert (vfunc_ptr_type_node,
10445 : : build_fold_addr_expr (fn));
10446 : : /* Don't refer to a virtual destructor from a constructor
10447 : : vtable or a vtable for an abstract class, since destroying
10448 : : an object under construction is undefined behavior and we
10449 : : don't want it to be considered a candidate for speculative
10450 : : devirtualization. But do create the thunk for ABI
10451 : : compliance. */
10452 : 9993139 : if (DECL_DESTRUCTOR_P (fn_original)
10453 : 9993139 : && (CLASSTYPE_PURE_VIRTUALS (DECL_CONTEXT (fn_original))
10454 : 4273372 : || orig_binfo != binfo))
10455 : 1282040 : init = size_zero_node;
10456 : : }
10457 : : }
10458 : :
10459 : : /* And add it to the chain of initializers. */
10460 : 10603404 : if (TARGET_VTABLE_USES_DESCRIPTORS)
10461 : : {
10462 : : int i;
10463 : : if (init == size_zero_node)
10464 : : for (i = 0; i < TARGET_VTABLE_USES_DESCRIPTORS; ++i)
10465 : : CONSTRUCTOR_APPEND_ELT (*inits, size_int (jx++), init);
10466 : : else
10467 : : for (i = 0; i < TARGET_VTABLE_USES_DESCRIPTORS; ++i)
10468 : : {
10469 : : tree fdesc = build2 (FDESC_EXPR, vfunc_ptr_type_node,
10470 : : fn, build_int_cst (NULL_TREE, i));
10471 : : TREE_CONSTANT (fdesc) = 1;
10472 : :
10473 : : CONSTRUCTOR_APPEND_ELT (*inits, size_int (jx++), fdesc);
10474 : : }
10475 : : }
10476 : : else
10477 : 10603404 : CONSTRUCTOR_APPEND_ELT (*inits, size_int (jx++), init);
10478 : : }
10479 : 2431093 : }
10480 : :
10481 : : /* Adds to vid->inits the initializers for the vbase and vcall
10482 : : offsets in BINFO, which is in the hierarchy dominated by T. */
10483 : :
10484 : : static void
10485 : 5138963 : build_vcall_and_vbase_vtbl_entries (tree binfo, vtbl_init_data* vid)
10486 : : {
10487 : 5138963 : tree b;
10488 : :
10489 : : /* If this is a derived class, we must first create entries
10490 : : corresponding to the primary base class. */
10491 : 5138963 : b = get_primary_binfo (binfo);
10492 : 5138963 : if (b)
10493 : 2707870 : build_vcall_and_vbase_vtbl_entries (b, vid);
10494 : :
10495 : : /* Add the vbase entries for this base. */
10496 : 5138963 : build_vbase_offset_vtbl_entries (binfo, vid);
10497 : : /* Add the vcall entries for this base. */
10498 : 5138963 : build_vcall_offset_vtbl_entries (binfo, vid);
10499 : 5138963 : }
10500 : :
10501 : : /* Returns the initializers for the vbase offset entries in the vtable
10502 : : for BINFO (which is part of the class hierarchy dominated by T), in
10503 : : reverse order. VBASE_OFFSET_INDEX gives the vtable index
10504 : : where the next vbase offset will go. */
10505 : :
10506 : : static void
10507 : 5138963 : build_vbase_offset_vtbl_entries (tree binfo, vtbl_init_data* vid)
10508 : : {
10509 : 5138963 : tree vbase;
10510 : 5138963 : tree t;
10511 : 5138963 : tree non_primary_binfo;
10512 : :
10513 : : /* If there are no virtual baseclasses, then there is nothing to
10514 : : do. */
10515 : 5138963 : if (!CLASSTYPE_VBASECLASSES (BINFO_TYPE (binfo)))
10516 : : return;
10517 : :
10518 : 921596 : t = vid->derived;
10519 : :
10520 : : /* We might be a primary base class. Go up the inheritance hierarchy
10521 : : until we find the most derived class of which we are a primary base:
10522 : : it is the offset of that which we need to use. */
10523 : 921596 : non_primary_binfo = binfo;
10524 : 1439319 : while (BINFO_INHERITANCE_CHAIN (non_primary_binfo))
10525 : : {
10526 : 868245 : tree b;
10527 : :
10528 : : /* If we have reached a virtual base, then it must be a primary
10529 : : base (possibly multi-level) of vid->binfo, or we wouldn't
10530 : : have called build_vcall_and_vbase_vtbl_entries for it. But it
10531 : : might be a lost primary, so just skip down to vid->binfo. */
10532 : 868245 : if (BINFO_VIRTUAL_P (non_primary_binfo))
10533 : : {
10534 : 187555 : non_primary_binfo = vid->binfo;
10535 : 187555 : break;
10536 : : }
10537 : :
10538 : 680690 : b = BINFO_INHERITANCE_CHAIN (non_primary_binfo);
10539 : 680690 : if (get_primary_binfo (b) != non_primary_binfo)
10540 : : break;
10541 : : non_primary_binfo = b;
10542 : : }
10543 : :
10544 : : /* Go through the virtual bases, adding the offsets. */
10545 : 921596 : for (vbase = TYPE_BINFO (BINFO_TYPE (binfo));
10546 : 5587375 : vbase;
10547 : 4665779 : vbase = TREE_CHAIN (vbase))
10548 : : {
10549 : 4665779 : tree b;
10550 : 4665779 : tree delta;
10551 : :
10552 : 4665779 : if (!BINFO_VIRTUAL_P (vbase))
10553 : 2158046 : continue;
10554 : :
10555 : : /* Find the instance of this virtual base in the complete
10556 : : object. */
10557 : 2507733 : b = copied_binfo (vbase, binfo);
10558 : :
10559 : : /* If we've already got an offset for this virtual base, we
10560 : : don't need another one. */
10561 : 2507733 : if (BINFO_VTABLE_PATH_MARKED (b))
10562 : 308231 : continue;
10563 : 2199502 : BINFO_VTABLE_PATH_MARKED (b) = 1;
10564 : :
10565 : : /* Figure out where we can find this vbase offset. */
10566 : 2199502 : delta = size_binop (MULT_EXPR,
10567 : : vid->index,
10568 : : fold_convert (ssizetype,
10569 : : TYPE_SIZE_UNIT (vtable_entry_type)));
10570 : 2199502 : if (vid->primary_vtbl_p)
10571 : 196342 : BINFO_VPTR_FIELD (b) = delta;
10572 : :
10573 : 2199502 : if (binfo != TYPE_BINFO (t))
10574 : : /* The vbase offset had better be the same. */
10575 : 2141709 : gcc_assert (tree_int_cst_equal (delta, BINFO_VPTR_FIELD (vbase)));
10576 : :
10577 : : /* The next vbase will come at a more negative offset. */
10578 : 2199502 : vid->index = size_binop (MINUS_EXPR, vid->index,
10579 : : ssize_int (TARGET_VTABLE_DATA_ENTRY_DISTANCE));
10580 : :
10581 : : /* The initializer is the delta from BINFO to this virtual base.
10582 : : The vbase offsets go in reverse inheritance-graph order, and
10583 : : we are walking in inheritance graph order so these end up in
10584 : : the right order. */
10585 : 2199502 : delta = size_diffop_loc (input_location,
10586 : 2199502 : BINFO_OFFSET (b), BINFO_OFFSET (non_primary_binfo));
10587 : :
10588 : 6865281 : CONSTRUCTOR_APPEND_ELT (vid->inits, NULL_TREE,
10589 : : fold_build1_loc (input_location, NOP_EXPR,
10590 : : vtable_entry_type, delta));
10591 : : }
10592 : : }
10593 : :
10594 : : /* Adds the initializers for the vcall offset entries in the vtable
10595 : : for BINFO (which is part of the class hierarchy dominated by VID->DERIVED)
10596 : : to VID->INITS. */
10597 : :
10598 : : static void
10599 : 5138963 : build_vcall_offset_vtbl_entries (tree binfo, vtbl_init_data* vid)
10600 : : {
10601 : : /* We only need these entries if this base is a virtual base. We
10602 : : compute the indices -- but do not add to the vtable -- when
10603 : : building the main vtable for a class. */
10604 : 5138963 : if (binfo == TYPE_BINFO (vid->derived)
10605 : 8697005 : || (BINFO_VIRTUAL_P (binfo)
10606 : : /* If BINFO is RTTI_BINFO, then (since BINFO does not
10607 : : correspond to VID->DERIVED), we are building a primary
10608 : : construction virtual table. Since this is a primary
10609 : : virtual table, we do not need the vcall offsets for
10610 : : BINFO. */
10611 : 597366 : && binfo != vid->rtti_binfo))
10612 : : {
10613 : : /* We need a vcall offset for each of the virtual functions in this
10614 : : vtable. For example:
10615 : :
10616 : : class A { virtual void f (); };
10617 : : class B1 : virtual public A { virtual void f (); };
10618 : : class B2 : virtual public A { virtual void f (); };
10619 : : class C: public B1, public B2 { virtual void f (); };
10620 : :
10621 : : A C object has a primary base of B1, which has a primary base of A. A
10622 : : C also has a secondary base of B2, which no longer has a primary base
10623 : : of A. So the B2-in-C construction vtable needs a secondary vtable for
10624 : : A, which will adjust the A* to a B2* to call f. We have no way of
10625 : : knowing what (or even whether) this offset will be when we define B2,
10626 : : so we store this "vcall offset" in the A sub-vtable and look it up in
10627 : : a "virtual thunk" for B2::f.
10628 : :
10629 : : We need entries for all the functions in our primary vtable and
10630 : : in our non-virtual bases' secondary vtables. */
10631 : 2164610 : vid->vbase = binfo;
10632 : : /* If we are just computing the vcall indices -- but do not need
10633 : : the actual entries -- not that. */
10634 : 2164610 : if (!BINFO_VIRTUAL_P (binfo))
10635 : 1580921 : vid->generate_vcall_entries = false;
10636 : : /* Now, walk through the non-virtual bases, adding vcall offsets. */
10637 : 2164610 : add_vcall_offset_vtbl_entries_r (binfo, vid);
10638 : : }
10639 : 5138963 : }
10640 : :
10641 : : /* Build vcall offsets, starting with those for BINFO. */
10642 : :
10643 : : static void
10644 : 5863027 : add_vcall_offset_vtbl_entries_r (tree binfo, vtbl_init_data* vid)
10645 : : {
10646 : 5863027 : int i;
10647 : 5863027 : tree primary_binfo;
10648 : 5863027 : tree base_binfo;
10649 : :
10650 : : /* Don't walk into virtual bases -- except, of course, for the
10651 : : virtual base for which we are building vcall offsets. Any
10652 : : primary virtual base will have already had its offsets generated
10653 : : through the recursion in build_vcall_and_vbase_vtbl_entries. */
10654 : 5863027 : if (BINFO_VIRTUAL_P (binfo) && vid->vbase != binfo)
10655 : 5863027 : return;
10656 : :
10657 : : /* If BINFO has a primary base, process it first. */
10658 : 5378547 : primary_binfo = get_primary_binfo (binfo);
10659 : 5378547 : if (primary_binfo)
10660 : 2658367 : add_vcall_offset_vtbl_entries_r (primary_binfo, vid);
10661 : :
10662 : : /* Add BINFO itself to the list. */
10663 : 5378547 : add_vcall_offset_vtbl_entries_1 (binfo, vid);
10664 : :
10665 : : /* Scan the non-primary bases of BINFO. */
10666 : 14455304 : for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
10667 : 3698210 : if (base_binfo != primary_binfo)
10668 : 1040050 : add_vcall_offset_vtbl_entries_r (base_binfo, vid);
10669 : : }
10670 : :
10671 : : /* Called from build_vcall_offset_vtbl_entries_r. */
10672 : :
10673 : : static void
10674 : 5378547 : add_vcall_offset_vtbl_entries_1 (tree binfo, vtbl_init_data* vid)
10675 : : {
10676 : : /* Make entries for the rest of the virtuals. */
10677 : 5378547 : tree orig_fn;
10678 : :
10679 : : /* The ABI requires that the methods be processed in declaration
10680 : : order. */
10681 : 5378547 : for (orig_fn = TYPE_FIELDS (BINFO_TYPE (binfo));
10682 : 187832167 : orig_fn;
10683 : 182453620 : orig_fn = DECL_CHAIN (orig_fn))
10684 : 182453620 : if (TREE_CODE (orig_fn) == FUNCTION_DECL && DECL_VINDEX (orig_fn))
10685 : 23093765 : add_vcall_offset (orig_fn, binfo, vid);
10686 : 5378547 : }
10687 : :
10688 : : /* Add a vcall offset entry for ORIG_FN to the vtable. */
10689 : :
10690 : : static void
10691 : 23093765 : add_vcall_offset (tree orig_fn, tree binfo, vtbl_init_data *vid)
10692 : : {
10693 : 23093765 : size_t i;
10694 : 23093765 : tree vcall_offset;
10695 : 23093765 : tree derived_entry;
10696 : :
10697 : : /* If there is already an entry for a function with the same
10698 : : signature as FN, then we do not need a second vcall offset.
10699 : : Check the list of functions already present in the derived
10700 : : class vtable. */
10701 : 63690836 : FOR_EACH_VEC_SAFE_ELT (vid->fns, i, derived_entry)
10702 : : {
10703 : 55694315 : if (same_signature_p (derived_entry, orig_fn)
10704 : : /* We only use one vcall offset for virtual destructors,
10705 : : even though there are two virtual table entries. */
10706 : 105363160 : || (DECL_DESTRUCTOR_P (derived_entry)
10707 : 36691032 : && DECL_DESTRUCTOR_P (orig_fn)))
10708 : 23093765 : return;
10709 : : }
10710 : :
10711 : : /* If we are building these vcall offsets as part of building
10712 : : the vtable for the most derived class, remember the vcall
10713 : : offset. */
10714 : 7996521 : if (vid->binfo == TYPE_BINFO (vid->derived))
10715 : : {
10716 : 7570197 : tree_pair_s elt = {orig_fn, vid->index};
10717 : 7570197 : vec_safe_push (CLASSTYPE_VCALL_INDICES (vid->derived), elt);
10718 : : }
10719 : :
10720 : : /* The next vcall offset will be found at a more negative
10721 : : offset. */
10722 : 7996521 : vid->index = size_binop (MINUS_EXPR, vid->index,
10723 : : ssize_int (TARGET_VTABLE_DATA_ENTRY_DISTANCE));
10724 : :
10725 : : /* Keep track of this function. */
10726 : 7996521 : vec_safe_push (vid->fns, orig_fn);
10727 : :
10728 : 7996521 : if (vid->generate_vcall_entries)
10729 : : {
10730 : 428824 : tree base;
10731 : 428824 : tree fn;
10732 : :
10733 : : /* Find the overriding function. */
10734 : 428824 : fn = find_final_overrider (vid->rtti_binfo, binfo, orig_fn);
10735 : 428824 : if (fn == error_mark_node)
10736 : 36 : vcall_offset = build_zero_cst (vtable_entry_type);
10737 : : else
10738 : : {
10739 : 428788 : base = TREE_VALUE (fn);
10740 : :
10741 : : /* The vbase we're working on is a primary base of
10742 : : vid->binfo. But it might be a lost primary, so its
10743 : : BINFO_OFFSET might be wrong, so we just use the
10744 : : BINFO_OFFSET from vid->binfo. */
10745 : 1715152 : vcall_offset = size_diffop_loc (input_location,
10746 : 428788 : BINFO_OFFSET (base),
10747 : 428788 : BINFO_OFFSET (vid->binfo));
10748 : 428788 : vcall_offset = fold_build1_loc (input_location,
10749 : : NOP_EXPR, vtable_entry_type,
10750 : : vcall_offset);
10751 : : }
10752 : : /* Add the initializer to the vtable. */
10753 : 428824 : CONSTRUCTOR_APPEND_ELT (vid->inits, NULL_TREE, vcall_offset);
10754 : : }
10755 : : }
10756 : :
10757 : : /* Return vtbl initializers for the RTTI entries corresponding to the
10758 : : BINFO's vtable. The RTTI entries should indicate the object given
10759 : : by VID->rtti_binfo. */
10760 : :
10761 : : static void
10762 : 2431093 : build_rtti_vtbl_entries (tree binfo, vtbl_init_data* vid)
10763 : : {
10764 : 2431093 : tree b;
10765 : 2431093 : tree t;
10766 : 2431093 : tree offset;
10767 : 2431093 : tree decl;
10768 : 2431093 : tree init;
10769 : :
10770 : 2431093 : t = BINFO_TYPE (vid->rtti_binfo);
10771 : :
10772 : : /* To find the complete object, we will first convert to our most
10773 : : primary base, and then add the offset in the vtbl to that value. */
10774 : 2431093 : b = most_primary_binfo (binfo);
10775 : 2431093 : offset = size_diffop_loc (input_location,
10776 : 2431093 : BINFO_OFFSET (vid->rtti_binfo), BINFO_OFFSET (b));
10777 : :
10778 : : /* The second entry is the address of the typeinfo object. */
10779 : 2431093 : if (flag_rtti)
10780 : 2427254 : decl = build_address (get_tinfo_decl (t));
10781 : : else
10782 : 3839 : decl = integer_zero_node;
10783 : :
10784 : : /* Convert the declaration to a type that can be stored in the
10785 : : vtable. */
10786 : 2431093 : init = build_nop (vfunc_ptr_type_node, decl);
10787 : 2431093 : CONSTRUCTOR_APPEND_ELT (vid->inits, NULL_TREE, init);
10788 : :
10789 : : /* Add the offset-to-top entry. It comes earlier in the vtable than
10790 : : the typeinfo entry. Convert the offset to look like a
10791 : : function pointer, so that we can put it in the vtable. */
10792 : 2431093 : init = build_nop (vfunc_ptr_type_node, offset);
10793 : 2431093 : CONSTRUCTOR_APPEND_ELT (vid->inits, NULL_TREE, init);
10794 : 2431093 : }
10795 : :
10796 : : /* TRUE iff TYPE is uniquely derived from PARENT. Ignores
10797 : : accessibility. */
10798 : :
10799 : : bool
10800 : 3279 : uniquely_derived_from_p (tree parent, tree type)
10801 : : {
10802 : 3279 : tree base = lookup_base (type, parent, ba_unique, NULL, tf_none);
10803 : 3279 : return base && base != error_mark_node;
10804 : : }
10805 : :
10806 : : /* TRUE iff TYPE is publicly & uniquely derived from PARENT. */
10807 : :
10808 : : bool
10809 : 3616 : publicly_uniquely_derived_p (tree parent, tree type)
10810 : : {
10811 : 3616 : tree base = lookup_base (type, parent, ba_ignore_scope | ba_check,
10812 : : NULL, tf_none);
10813 : 3616 : return base && base != error_mark_node;
10814 : : }
10815 : :
10816 : : /* CTX1 and CTX2 are declaration contexts. Return the innermost common
10817 : : class between them, if any. */
10818 : :
10819 : : tree
10820 : 2071 : common_enclosing_class (tree ctx1, tree ctx2)
10821 : : {
10822 : 2071 : if (!TYPE_P (ctx1) || !TYPE_P (ctx2))
10823 : : return NULL_TREE;
10824 : 2071 : gcc_assert (ctx1 == TYPE_MAIN_VARIANT (ctx1)
10825 : : && ctx2 == TYPE_MAIN_VARIANT (ctx2));
10826 : 2071 : if (ctx1 == ctx2)
10827 : : return ctx1;
10828 : 116 : for (tree t = ctx1; TYPE_P (t); t = TYPE_CONTEXT (t))
10829 : 85 : TYPE_MARKED_P (t) = true;
10830 : 52 : tree found = NULL_TREE;
10831 : 52 : for (tree t = ctx2; TYPE_P (t); t = TYPE_CONTEXT (t))
10832 : 52 : if (TYPE_MARKED_P (t))
10833 : : {
10834 : : found = t;
10835 : : break;
10836 : : }
10837 : 116 : for (tree t = ctx1; TYPE_P (t); t = TYPE_CONTEXT (t))
10838 : 85 : TYPE_MARKED_P (t) = false;
10839 : : return found;
10840 : : }
10841 : :
10842 : : #include "gt-cp-class.h"
|