Branch data Line data Source code
1 : : /* Functions related to building -*- C++ -*- classes and their related objects.
2 : : Copyright (C) 1987-2025 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, bool = false,
145 : : bool = false, 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 : 174499699 : set_current_access_from_decl (tree decl)
214 : : {
215 : 174499699 : if (TREE_PRIVATE (decl))
216 : 16310786 : current_access_specifier = access_private_node;
217 : 158188913 : else if (TREE_PROTECTED (decl))
218 : 10702435 : current_access_specifier = access_protected_node;
219 : : else
220 : 147486478 : current_access_specifier = access_public_node;
221 : 174499699 : }
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 : 171329 : build_if_in_charge (tree true_stmt, tree false_stmt)
229 : : {
230 : 171329 : gcc_assert (DECL_HAS_IN_CHARGE_PARM_P (current_function_decl));
231 : 342658 : tree cmp = build2 (NE_EXPR, boolean_type_node,
232 : 171329 : current_in_charge_parm, integer_zero_node);
233 : 171329 : tree type = unlowered_expr_type (true_stmt);
234 : 171329 : if (VOID_TYPE_P (type))
235 : 9037 : type = unlowered_expr_type (false_stmt);
236 : 171329 : tree cond = build3 (COND_EXPR, type,
237 : : cmp, true_stmt, false_stmt);
238 : 171329 : 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 : 49395053 : build_base_path (enum tree_code code,
253 : : tree expr,
254 : : tree binfo,
255 : : int nonnull,
256 : : tsubst_flags_t complain)
257 : : {
258 : 49395053 : tree v_binfo = NULL_TREE;
259 : 49395053 : tree d_binfo = NULL_TREE;
260 : 49395053 : tree probe;
261 : 49395053 : tree offset;
262 : 49395053 : tree target_type;
263 : 49395053 : tree null_test = NULL;
264 : 49395053 : tree ptr_target_type;
265 : 49395053 : int fixed_type_p;
266 : 49395053 : int want_pointer = TYPE_PTR_P (TREE_TYPE (expr));
267 : 49395053 : bool has_empty = false;
268 : 49395053 : bool virtual_access;
269 : 49395053 : bool rvalue = false;
270 : :
271 : 49395053 : if (expr == error_mark_node || binfo == error_mark_node || !binfo)
272 : : return error_mark_node;
273 : :
274 : 106705069 : for (probe = binfo; probe; probe = BINFO_INHERITANCE_CHAIN (probe))
275 : : {
276 : 57310031 : d_binfo = probe;
277 : 57310031 : if (is_empty_class (BINFO_TYPE (probe)))
278 : 8314976 : has_empty = true;
279 : 114508222 : if (!v_binfo && BINFO_VIRTUAL_P (probe))
280 : : v_binfo = probe;
281 : : }
282 : :
283 : 49395038 : probe = TYPE_MAIN_VARIANT (TREE_TYPE (expr));
284 : 49395038 : if (want_pointer)
285 : 46107919 : probe = TYPE_MAIN_VARIANT (TREE_TYPE (probe));
286 : 49395038 : if (dependent_type_p (probe))
287 : 303677 : if (tree open = currently_open_class (probe))
288 : 49395038 : probe = open;
289 : :
290 : 49395038 : if (code == PLUS_EXPR
291 : 96192349 : && !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 : 6 : if (complain & tf_error)
299 : : {
300 : 6 : tree base = lookup_base (probe, BINFO_TYPE (d_binfo),
301 : : ba_unique, NULL, complain);
302 : 6 : gcc_assert (base == error_mark_node || !base);
303 : : }
304 : 6 : return error_mark_node;
305 : : }
306 : :
307 : 51992759 : gcc_assert ((code == MINUS_EXPR
308 : : && SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), probe))
309 : : || code == PLUS_EXPR);
310 : :
311 : 49395032 : if (binfo == d_binfo)
312 : : /* Nothing to do. */
313 : : return expr;
314 : :
315 : 7344772 : if (code == MINUS_EXPR && v_binfo)
316 : : {
317 : 12 : if (complain & tf_error)
318 : : {
319 : 9 : if (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), BINFO_TYPE (v_binfo)))
320 : : {
321 : 9 : if (want_pointer)
322 : 18 : error ("cannot convert from pointer to base class %qT to "
323 : : "pointer to derived class %qT because the base is "
324 : 9 : "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 : 12 : return error_mark_node;
344 : : }
345 : :
346 : 14689520 : bool uneval = (cp_unevaluated_operand != 0
347 : 7040820 : || processing_template_decl
348 : 14022442 : || in_template_context);
349 : :
350 : 7344760 : fixed_type_p = resolves_to_fixed_type_p (expr, &nonnull);
351 : :
352 : : /* Do we need to look in the vtable for the real offset? */
353 : 7344760 : virtual_access = (v_binfo && fixed_type_p <= 0);
354 : :
355 : : /* For a non-pointer simple base reference, express it as a COMPONENT_REF
356 : : without taking its address (and so causing lambda capture, 91933). */
357 : 7344760 : if (code == PLUS_EXPR
358 : 7344760 : && !want_pointer
359 : : && !has_empty
360 : 3286818 : && !uneval
361 : 2153905 : && !virtual_access)
362 : 2145151 : return build_simple_base_path (expr, binfo);
363 : :
364 : 5199609 : if (!want_pointer)
365 : : {
366 : 1141667 : rvalue = !lvalue_p (expr);
367 : : /* This must happen before the call to save_expr. */
368 : 1141667 : expr = cp_build_addr_expr (expr, complain);
369 : : }
370 : : else
371 : 4057942 : expr = mark_rvalue_use (expr);
372 : :
373 : 5199609 : offset = BINFO_OFFSET (binfo);
374 : 5266232 : target_type = code == PLUS_EXPR ? BINFO_TYPE (binfo) : BINFO_TYPE (d_binfo);
375 : : /* TARGET_TYPE has been extracted from BINFO, and, is therefore always
376 : : cv-unqualified. Extract the cv-qualifiers from EXPR so that the
377 : : expression returned matches the input. */
378 : 5199609 : target_type = cp_build_qualified_type
379 : 5199609 : (target_type, cp_type_quals (TREE_TYPE (TREE_TYPE (expr))));
380 : 5199609 : ptr_target_type = build_pointer_type (target_type);
381 : :
382 : : /* Don't bother with the calculations inside sizeof; they'll ICE if the
383 : : source type is incomplete and the pointer value doesn't matter. In a
384 : : template (even in instantiate_non_dependent_expr), we don't have vtables
385 : : set up properly yet, and the value doesn't matter there either; we're
386 : : just interested in the result of overload resolution. */
387 : 5199609 : if (uneval)
388 : : {
389 : 667112 : expr = build_nop (ptr_target_type, expr);
390 : 667112 : goto indout;
391 : : }
392 : :
393 : 4532497 : if (!COMPLETE_TYPE_P (probe))
394 : : {
395 : 6 : if (complain & tf_error)
396 : 18 : error ("cannot convert from %qT to base class %qT because %qT is "
397 : 6 : "incomplete", BINFO_TYPE (d_binfo), BINFO_TYPE (binfo),
398 : 6 : BINFO_TYPE (d_binfo));
399 : 6 : return error_mark_node;
400 : : }
401 : :
402 : : /* If we're in an NSDMI, we don't have the full constructor context yet
403 : : that we need for converting to a virtual base, so just build a stub
404 : : CONVERT_EXPR and expand it later in bot_replace. */
405 : 4532491 : if (virtual_access && fixed_type_p < 0
406 : 4532491 : && current_scope () != current_function_decl)
407 : : {
408 : 33 : expr = build1 (CONVERT_EXPR, ptr_target_type, expr);
409 : 33 : CONVERT_EXPR_VBASE_PATH (expr) = true;
410 : 33 : goto indout;
411 : : }
412 : :
413 : : /* Do we need to check for a null pointer? */
414 : 4532458 : if (want_pointer && !nonnull)
415 : : {
416 : : /* If we know the conversion will not actually change the value
417 : : of EXPR, then we can avoid testing the expression for NULL.
418 : : We have to avoid generating a COMPONENT_REF for a base class
419 : : field, because other parts of the compiler know that such
420 : : expressions are always non-NULL. */
421 : 72482 : if (!virtual_access && integer_zerop (offset))
422 : 70672 : return build_nop (ptr_target_type, expr);
423 : 1810 : null_test = error_mark_node;
424 : : }
425 : :
426 : : /* Protect against multiple evaluation if necessary. */
427 : 4461786 : if (TREE_SIDE_EFFECTS (expr) && (null_test || virtual_access))
428 : 1105 : expr = save_expr (expr);
429 : :
430 : : /* Store EXPR and build the real null test just before returning. */
431 : 4461786 : if (null_test)
432 : 1810 : null_test = expr;
433 : :
434 : : /* If this is a simple base reference, express it as a COMPONENT_REF. */
435 : 4461786 : if (code == PLUS_EXPR && !virtual_access
436 : : /* We don't build base fields for empty bases, and they aren't very
437 : : interesting to the optimizers anyway. */
438 : 4319448 : && !has_empty)
439 : : {
440 : 2573332 : expr = cp_build_fold_indirect_ref (expr);
441 : 2573332 : expr = build_simple_base_path (expr, binfo);
442 : 2573332 : if (rvalue && lvalue_p (expr))
443 : 0 : expr = move (expr);
444 : 2573332 : if (want_pointer)
445 : 2573332 : expr = build_address (expr);
446 : 2573332 : target_type = TREE_TYPE (expr);
447 : 2573332 : goto out;
448 : : }
449 : :
450 : 1888454 : if (virtual_access)
451 : : {
452 : : /* Going via virtual base V_BINFO. We need the static offset
453 : : from V_BINFO to BINFO, and the dynamic offset from D_BINFO to
454 : : V_BINFO. That offset is an entry in D_BINFO's vtable. */
455 : 90016 : tree v_offset;
456 : :
457 : 90016 : if (fixed_type_p < 0 && in_base_initializer)
458 : : {
459 : : /* In a base member initializer, we cannot rely on the
460 : : vtable being set up. We have to indirect via the
461 : : vtt_parm. */
462 : 6 : tree t;
463 : :
464 : 6 : t = TREE_TYPE (TYPE_VFIELD (current_class_type));
465 : 6 : t = build_pointer_type (t);
466 : 6 : v_offset = fold_convert (t, current_vtt_parm);
467 : 6 : v_offset = cp_build_fold_indirect_ref (v_offset);
468 : 6 : }
469 : : else
470 : : {
471 : 90010 : tree t = expr;
472 : 90010 : if (sanitize_flags_p (SANITIZE_VPTR)
473 : 90010 : && fixed_type_p == 0)
474 : : {
475 : 44 : t = cp_ubsan_maybe_instrument_cast_to_vbase (input_location,
476 : : probe, expr);
477 : 44 : if (t == NULL_TREE)
478 : 89966 : t = expr;
479 : : }
480 : 90010 : v_offset = build_vfield_ref (cp_build_fold_indirect_ref (t),
481 : 90010 : TREE_TYPE (TREE_TYPE (expr)));
482 : : }
483 : :
484 : 90016 : if (v_offset == error_mark_node)
485 : : return error_mark_node;
486 : :
487 : 90016 : v_offset = fold_build_pointer_plus (v_offset, BINFO_VPTR_FIELD (v_binfo));
488 : 90016 : v_offset = build1 (NOP_EXPR,
489 : : build_pointer_type (ptrdiff_type_node),
490 : : v_offset);
491 : 90016 : v_offset = cp_build_fold_indirect_ref (v_offset);
492 : 90016 : TREE_CONSTANT (v_offset) = 1;
493 : :
494 : 180032 : offset = convert_to_integer (ptrdiff_type_node,
495 : : size_diffop_loc (input_location, offset,
496 : 90016 : BINFO_OFFSET (v_binfo)));
497 : :
498 : 90016 : if (!integer_zerop (offset))
499 : 502 : v_offset = build2 (code, ptrdiff_type_node, v_offset, offset);
500 : :
501 : 90016 : if (fixed_type_p < 0)
502 : : /* Negative fixed_type_p means this is a constructor or destructor;
503 : : virtual base layout is fixed in in-charge [cd]tors, but not in
504 : : base [cd]tors. */
505 : 49352 : offset = build_if_in_charge
506 : 49352 : (convert_to_integer (ptrdiff_type_node, BINFO_OFFSET (binfo)),
507 : : v_offset);
508 : : else
509 : : offset = v_offset;
510 : : }
511 : :
512 : 1888454 : if (want_pointer)
513 : 1888454 : target_type = ptr_target_type;
514 : :
515 : 1888454 : if (!integer_zerop (offset))
516 : : {
517 : 90808 : offset = fold_convert (sizetype, offset);
518 : 90808 : if (code == MINUS_EXPR)
519 : 345 : offset = fold_build1_loc (input_location, NEGATE_EXPR, sizetype, offset);
520 : 90808 : expr = fold_build_pointer_plus (expr, offset);
521 : : }
522 : : else
523 : : null_test = NULL;
524 : :
525 : 1888454 : expr = build1 (NOP_EXPR, ptr_target_type, expr);
526 : :
527 : 2555599 : indout:
528 : 2555599 : if (!want_pointer)
529 : : {
530 : 1141667 : expr = cp_build_fold_indirect_ref (expr);
531 : 1141667 : if (rvalue)
532 : 239443 : expr = move (expr);
533 : : }
534 : :
535 : 1413932 : out:
536 : 5128931 : if (null_test)
537 : : /* Wrap EXPR in a null test. */
538 : 1810 : expr = build_if_nonnull (null_test, expr, complain);
539 : :
540 : : return expr;
541 : : }
542 : :
543 : : /* Subroutine of build_base_path; EXPR and BINFO are as in that function.
544 : : Perform a derived-to-base conversion by recursively building up a
545 : : sequence of COMPONENT_REFs to the appropriate base fields. */
546 : :
547 : : static tree
548 : 15898128 : build_simple_base_path (tree expr, tree binfo)
549 : : {
550 : 15898128 : tree type = BINFO_TYPE (binfo);
551 : 15898128 : tree d_binfo = BINFO_INHERITANCE_CHAIN (binfo);
552 : 15898128 : tree field;
553 : :
554 : 15898128 : if (d_binfo == NULL_TREE)
555 : : {
556 : 7779384 : tree temp;
557 : :
558 : 7779384 : gcc_assert (TYPE_MAIN_VARIANT (TREE_TYPE (expr)) == type);
559 : :
560 : : /* Transform `(a, b).x' into `(*(a, &b)).x', `(a ? b : c).x'
561 : : into `(*(a ? &b : &c)).x', and so on. A COND_EXPR is only
562 : : an lvalue in the front end; only _DECLs and _REFs are lvalues
563 : : in the back end. */
564 : 7779384 : temp = unary_complex_lvalue (ADDR_EXPR, expr);
565 : 7779384 : if (temp)
566 : 0 : expr = cp_build_fold_indirect_ref (temp);
567 : :
568 : 7779384 : return expr;
569 : : }
570 : :
571 : : /* Recurse. */
572 : 8118744 : expr = build_simple_base_path (expr, d_binfo);
573 : :
574 : 8118744 : for (field = TYPE_FIELDS (BINFO_TYPE (d_binfo));
575 : 44256701 : field; field = DECL_CHAIN (field))
576 : : /* Is this the base field created by build_base_field? */
577 : 44256701 : if (TREE_CODE (field) == FIELD_DECL
578 : 9322608 : && DECL_FIELD_IS_BASE (field)
579 : 9273230 : && TREE_TYPE (field) == type
580 : : /* If we're looking for a field in the most-derived class,
581 : : also check the field offset; we can have two base fields
582 : : of the same type if one is an indirect virtual base and one
583 : : is a direct non-virtual base. */
584 : 52375568 : && (BINFO_INHERITANCE_CHAIN (d_binfo)
585 : 7779366 : || tree_int_cst_equal (byte_position (field),
586 : 7779366 : BINFO_OFFSET (binfo))))
587 : : {
588 : : /* We don't use build_class_member_access_expr here, as that
589 : : has unnecessary checks, and more importantly results in
590 : : recursive calls to dfs_walk_once. */
591 : 8118744 : int type_quals = cp_type_quals (TREE_TYPE (expr));
592 : :
593 : 8118744 : expr = build3 (COMPONENT_REF,
594 : : cp_build_qualified_type (type, type_quals),
595 : : expr, field, NULL_TREE);
596 : : /* Mark the expression const or volatile, as appropriate.
597 : : Even though we've dealt with the type above, we still have
598 : : to mark the expression itself. */
599 : 8118744 : if (type_quals & TYPE_QUAL_CONST)
600 : 821795 : TREE_READONLY (expr) = 1;
601 : 8118744 : if (type_quals & TYPE_QUAL_VOLATILE)
602 : 23000 : TREE_THIS_VOLATILE (expr) = 1;
603 : :
604 : 8118744 : return expr;
605 : : }
606 : :
607 : : /* Didn't find the base field?!? */
608 : 0 : gcc_unreachable ();
609 : : }
610 : :
611 : : /* Convert OBJECT to the base TYPE. OBJECT is an expression whose
612 : : type is a class type or a pointer to a class type. In the former
613 : : case, TYPE is also a class type; in the latter it is another
614 : : pointer type. If CHECK_ACCESS is true, an error message is emitted
615 : : if TYPE is inaccessible. If OBJECT has pointer type, the value is
616 : : assumed to be non-NULL. */
617 : :
618 : : tree
619 : 1411678 : convert_to_base (tree object, tree type, bool check_access, bool nonnull,
620 : : tsubst_flags_t complain)
621 : : {
622 : 1411678 : tree binfo;
623 : 1411678 : tree object_type;
624 : :
625 : 1411678 : if (TYPE_PTR_P (TREE_TYPE (object)))
626 : : {
627 : 141104 : object_type = TREE_TYPE (TREE_TYPE (object));
628 : 141104 : type = TREE_TYPE (type);
629 : : }
630 : : else
631 : 1270574 : object_type = TREE_TYPE (object);
632 : :
633 : 1414223 : binfo = lookup_base (object_type, type, check_access ? ba_check : ba_unique,
634 : : NULL, complain);
635 : 1411678 : if (!binfo || binfo == error_mark_node)
636 : 95 : return error_mark_node;
637 : :
638 : 1411583 : return build_base_path (PLUS_EXPR, object, binfo, nonnull, complain);
639 : : }
640 : :
641 : : /* EXPR is an expression with unqualified class type. BASE is a base
642 : : binfo of that class type. Returns EXPR, converted to the BASE
643 : : type. This function assumes that EXPR is the most derived class;
644 : : therefore virtual bases can be found at their static offsets. */
645 : :
646 : : tree
647 : 1672558 : convert_to_base_statically (tree expr, tree base)
648 : : {
649 : 1672558 : tree expr_type;
650 : :
651 : 1672558 : expr_type = TREE_TYPE (expr);
652 : 1672558 : if (!SAME_BINFO_TYPE_P (BINFO_TYPE (base), expr_type))
653 : : {
654 : : /* If this is a non-empty base, use a COMPONENT_REF. */
655 : 1672558 : if (!is_empty_class (BINFO_TYPE (base)))
656 : 1267812 : return build_simple_base_path (expr, base);
657 : :
658 : : /* We use fold_build2 and fold_convert below to simplify the trees
659 : : provided to the optimizers. It is not safe to call these functions
660 : : when processing a template because they do not handle C++-specific
661 : : trees. */
662 : 404746 : gcc_assert (!processing_template_decl);
663 : 404746 : expr = cp_build_addr_expr (expr, tf_warning_or_error);
664 : 404746 : if (!integer_zerop (BINFO_OFFSET (base)))
665 : 189 : expr = fold_build_pointer_plus_loc (input_location,
666 : 189 : expr, BINFO_OFFSET (base));
667 : 404746 : expr = fold_convert (build_pointer_type (BINFO_TYPE (base)), expr);
668 : 404746 : expr = build_fold_indirect_ref_loc (input_location, expr);
669 : : }
670 : :
671 : : return expr;
672 : : }
673 : :
674 : : /* True IFF EXPR is a reference to an empty base class "subobject", as built in
675 : : convert_to_base_statically. We look for the result of the fold_convert
676 : : call, a NOP_EXPR from one pointer type to another, where the target is an
677 : : empty base of the original type. */
678 : :
679 : : bool
680 : 30744282 : is_empty_base_ref (tree expr)
681 : : {
682 : 30744282 : if (INDIRECT_REF_P (expr))
683 : 24777679 : expr = TREE_OPERAND (expr, 0);
684 : 30744282 : if (TREE_CODE (expr) != NOP_EXPR)
685 : : return false;
686 : 882487 : tree type = TREE_TYPE (expr);
687 : 882487 : if (!POINTER_TYPE_P (type))
688 : : return false;
689 : 882487 : type = TREE_TYPE (type);
690 : 882487 : if (!is_empty_class (type))
691 : : return false;
692 : 467639 : STRIP_NOPS (expr);
693 : 467639 : tree fromtype = TREE_TYPE (expr);
694 : 467639 : if (!POINTER_TYPE_P (fromtype))
695 : : return false;
696 : 467639 : fromtype = TREE_TYPE (fromtype);
697 : 460675 : return (CLASS_TYPE_P (fromtype)
698 : 460675 : && !same_type_ignoring_top_level_qualifiers_p (fromtype, type)
699 : 927685 : && DERIVED_FROM_P (type, fromtype));
700 : : }
701 : :
702 : : tree
703 : 1640289 : build_vfield_ref (tree datum, tree type)
704 : : {
705 : 1640289 : tree vfield, vcontext;
706 : :
707 : 1640289 : if (datum == error_mark_node
708 : : /* Can happen in case of duplicate base types (c++/59082). */
709 : 1640289 : || !TYPE_VFIELD (type))
710 : : return error_mark_node;
711 : :
712 : : /* First, convert to the requested type. */
713 : 1640289 : if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (datum), type))
714 : 0 : datum = convert_to_base (datum, type, /*check_access=*/false,
715 : : /*nonnull=*/true, tf_warning_or_error);
716 : :
717 : : /* Second, the requested type may not be the owner of its own vptr.
718 : : If not, convert to the base class that owns it. We cannot use
719 : : convert_to_base here, because VCONTEXT may appear more than once
720 : : in the inheritance hierarchy of TYPE, and thus direct conversion
721 : : between the types may be ambiguous. Following the path back up
722 : : one step at a time via primary bases avoids the problem. */
723 : 1640289 : vfield = TYPE_VFIELD (type);
724 : 1640289 : vcontext = DECL_CONTEXT (vfield);
725 : 3433378 : while (!same_type_ignoring_top_level_qualifiers_p (vcontext, type))
726 : : {
727 : 1793089 : datum = build_simple_base_path (datum, CLASSTYPE_PRIMARY_BINFO (type));
728 : 1793089 : type = TREE_TYPE (datum);
729 : : }
730 : :
731 : 1640289 : return build3 (COMPONENT_REF, TREE_TYPE (vfield), datum, vfield, NULL_TREE);
732 : : }
733 : :
734 : : /* Given an object INSTANCE, return an expression which yields the
735 : : vtable element corresponding to INDEX. There are many special
736 : : cases for INSTANCE which we take care of here, mainly to avoid
737 : : creating extra tree nodes when we don't have to. */
738 : :
739 : : tree
740 : 544390 : build_vtbl_ref (tree instance, tree idx)
741 : : {
742 : 544390 : tree aref;
743 : 544390 : tree vtbl = NULL_TREE;
744 : :
745 : : /* Try to figure out what a reference refers to, and
746 : : access its virtual function table directly. */
747 : :
748 : 544390 : int cdtorp = 0;
749 : 544390 : tree fixed_type = fixed_type_or_null (instance, NULL, &cdtorp);
750 : :
751 : 544390 : tree basetype = non_reference (TREE_TYPE (instance));
752 : :
753 : 544390 : if (fixed_type && !cdtorp)
754 : : {
755 : 116 : tree binfo = lookup_base (fixed_type, basetype,
756 : : ba_unique, NULL, tf_none);
757 : 116 : if (binfo && binfo != error_mark_node)
758 : 101 : vtbl = unshare_expr (BINFO_VTABLE (binfo));
759 : : }
760 : :
761 : 101 : if (!vtbl)
762 : 544359 : vtbl = build_vfield_ref (instance, basetype);
763 : :
764 : 544390 : aref = build_array_ref (input_location, vtbl, idx);
765 : 1088749 : TREE_CONSTANT (aref) |= TREE_CONSTANT (vtbl) && TREE_CONSTANT (idx);
766 : :
767 : 544390 : return aref;
768 : : }
769 : :
770 : : /* Given a stable object pointer INSTANCE_PTR, return an expression which
771 : : yields a function pointer corresponding to vtable element INDEX. */
772 : :
773 : : tree
774 : 543473 : build_vfn_ref (tree instance_ptr, tree idx)
775 : : {
776 : 543473 : tree aref;
777 : :
778 : 543473 : aref = build_vtbl_ref (cp_build_fold_indirect_ref (instance_ptr), idx);
779 : :
780 : : /* When using function descriptors, the address of the
781 : : vtable entry is treated as a function pointer. */
782 : 543473 : if (TARGET_VTABLE_USES_DESCRIPTORS)
783 : : aref = build1 (NOP_EXPR, TREE_TYPE (aref),
784 : : cp_build_addr_expr (aref, tf_warning_or_error));
785 : :
786 : : /* Remember this as a method reference, for later devirtualization. */
787 : 543473 : aref = build3 (OBJ_TYPE_REF, TREE_TYPE (aref), aref, instance_ptr,
788 : 543473 : fold_convert (TREE_TYPE (instance_ptr), idx));
789 : :
790 : 543473 : return aref;
791 : : }
792 : :
793 : : /* Return the name of the virtual function table (as an IDENTIFIER_NODE)
794 : : for the given TYPE. */
795 : :
796 : : static tree
797 : 1727805 : get_vtable_name (tree type)
798 : : {
799 : 0 : return mangle_vtbl_for_type (type);
800 : : }
801 : :
802 : : /* DECL is an entity associated with TYPE, like a virtual table or an
803 : : implicitly generated constructor. Determine whether or not DECL
804 : : should have external or internal linkage at the object file
805 : : level. This routine does not deal with COMDAT linkage and other
806 : : similar complexities; it simply sets TREE_PUBLIC if it possible for
807 : : entities in other translation units to contain copies of DECL, in
808 : : the abstract. */
809 : :
810 : : void
811 : 28202180 : set_linkage_according_to_type (tree /*type*/, tree decl)
812 : : {
813 : 28202180 : TREE_PUBLIC (decl) = 1;
814 : 28202180 : determine_visibility (decl);
815 : 28202180 : }
816 : :
817 : : /* Create a VAR_DECL for a primary or secondary vtable for CLASS_TYPE.
818 : : (For a secondary vtable for B-in-D, CLASS_TYPE should be D, not B.)
819 : : Use NAME for the name of the vtable, and VTABLE_TYPE for its type. */
820 : :
821 : : static tree
822 : 2178601 : build_vtable (tree class_type, tree name, tree vtable_type)
823 : : {
824 : 2178601 : tree decl;
825 : :
826 : 2178601 : decl = build_lang_decl (VAR_DECL, name, vtable_type);
827 : : /* vtable names are already mangled; give them their DECL_ASSEMBLER_NAME
828 : : now to avoid confusion in mangle_decl. */
829 : 2178601 : SET_DECL_ASSEMBLER_NAME (decl, name);
830 : 2178601 : DECL_CONTEXT (decl) = class_type;
831 : 2178601 : DECL_ARTIFICIAL (decl) = 1;
832 : 2178601 : TREE_STATIC (decl) = 1;
833 : 2178601 : TREE_READONLY (decl) = 1;
834 : 2178601 : DECL_VIRTUAL_P (decl) = 1;
835 : 2189621 : SET_DECL_ALIGN (decl, TARGET_VTABLE_ENTRY_ALIGN);
836 : 2178601 : DECL_USER_ALIGN (decl) = true;
837 : 2178601 : DECL_VTABLE_OR_VTT_P (decl) = 1;
838 : 2178601 : set_linkage_according_to_type (class_type, decl);
839 : : /* The vtable has not been defined -- yet. */
840 : 2178601 : DECL_EXTERNAL (decl) = 1;
841 : 2178601 : DECL_NOT_REALLY_EXTERN (decl) = 1;
842 : :
843 : : /* Mark the VAR_DECL node representing the vtable itself as a
844 : : "gratuitous" one, thereby forcing dwarfout.c to ignore it. It
845 : : is rather important that such things be ignored because any
846 : : effort to actually generate DWARF for them will run into
847 : : trouble when/if we encounter code like:
848 : :
849 : : #pragma interface
850 : : struct S { virtual void member (); };
851 : :
852 : : because the artificial declaration of the vtable itself (as
853 : : manufactured by the g++ front end) will say that the vtable is
854 : : a static member of `S' but only *after* the debug output for
855 : : the definition of `S' has already been output. This causes
856 : : grief because the DWARF entry for the definition of the vtable
857 : : will try to refer back to an earlier *declaration* of the
858 : : vtable as a static member of `S' and there won't be one. We
859 : : might be able to arrange to have the "vtable static member"
860 : : attached to the member list for `S' before the debug info for
861 : : `S' get written (which would solve the problem) but that would
862 : : require more intrusive changes to the g++ front end. */
863 : 2178601 : DECL_IGNORED_P (decl) = 1;
864 : :
865 : 2178601 : return decl;
866 : : }
867 : :
868 : : /* Get the VAR_DECL of the vtable for TYPE. TYPE need not be polymorphic,
869 : : or even complete. If this does not exist, create it. If COMPLETE is
870 : : nonzero, then complete the definition of it -- that will render it
871 : : impossible to actually build the vtable, but is useful to get at those
872 : : which are known to exist in the runtime. */
873 : :
874 : : tree
875 : 8217518 : get_vtable_decl (tree type, int complete)
876 : : {
877 : 8217518 : tree decl;
878 : :
879 : 8217518 : if (CLASSTYPE_VTABLES (type))
880 : 6489713 : return CLASSTYPE_VTABLES (type);
881 : :
882 : 1727805 : decl = build_vtable (type, get_vtable_name (type), vtbl_type_node);
883 : 1727805 : CLASSTYPE_VTABLES (type) = decl;
884 : :
885 : 1727805 : if (complete)
886 : : {
887 : 41474 : DECL_EXTERNAL (decl) = 1;
888 : 41474 : cp_finish_decl (decl, NULL_TREE, false, NULL_TREE, 0);
889 : : }
890 : :
891 : : return decl;
892 : : }
893 : :
894 : : /* Build the primary virtual function table for TYPE. If BINFO is
895 : : non-NULL, build the vtable starting with the initial approximation
896 : : that it is the same as the one which is the head of the association
897 : : list. Returns a nonzero value if a new vtable is actually
898 : : created. */
899 : :
900 : : static int
901 : 6478614 : build_primary_vtable (tree binfo, tree type)
902 : : {
903 : 6478614 : tree decl;
904 : 6478614 : tree virtuals;
905 : :
906 : 6478614 : decl = get_vtable_decl (type, /*complete=*/0);
907 : :
908 : 6478614 : if (binfo)
909 : : {
910 : 6234698 : if (BINFO_NEW_VTABLE_MARKED (binfo))
911 : : /* We have already created a vtable for this base, so there's
912 : : no need to do it again. */
913 : : return 0;
914 : :
915 : 1442415 : virtuals = copy_list (BINFO_VIRTUALS (binfo));
916 : 1442415 : TREE_TYPE (decl) = TREE_TYPE (get_vtbl_decl_for_binfo (binfo));
917 : 1442415 : DECL_SIZE (decl) = TYPE_SIZE (TREE_TYPE (decl));
918 : 1442415 : DECL_SIZE_UNIT (decl) = TYPE_SIZE_UNIT (TREE_TYPE (decl));
919 : : }
920 : : else
921 : : {
922 : 243916 : gcc_assert (TREE_TYPE (decl) == vtbl_type_node);
923 : : virtuals = NULL_TREE;
924 : : }
925 : :
926 : : /* Initialize the association list for this type, based
927 : : on our first approximation. */
928 : 1686331 : BINFO_VTABLE (TYPE_BINFO (type)) = decl;
929 : 1686331 : BINFO_VIRTUALS (TYPE_BINFO (type)) = virtuals;
930 : 1686331 : SET_BINFO_NEW_VTABLE_MARKED (TYPE_BINFO (type));
931 : : return 1;
932 : : }
933 : :
934 : : /* Give BINFO a new virtual function table which is initialized
935 : : with a skeleton-copy of its original initialization. The only
936 : : entry that changes is the `delta' entry, so we can really
937 : : share a lot of structure.
938 : :
939 : : FOR_TYPE is the most derived type which caused this table to
940 : : be needed.
941 : :
942 : : Returns nonzero if we haven't met BINFO before.
943 : :
944 : : The order in which vtables are built (by calling this function) for
945 : : an object must remain the same, otherwise a binary incompatibility
946 : : can result. */
947 : :
948 : : static int
949 : 763808 : build_secondary_vtable (tree binfo)
950 : : {
951 : 763808 : if (BINFO_NEW_VTABLE_MARKED (binfo))
952 : : /* We already created a vtable for this base. There's no need to
953 : : do it again. */
954 : : return 0;
955 : :
956 : : /* Remember that we've created a vtable for this BINFO, so that we
957 : : don't try to do so again. */
958 : 264738 : SET_BINFO_NEW_VTABLE_MARKED (binfo);
959 : :
960 : : /* Make fresh virtual list, so we can smash it later. */
961 : 264738 : BINFO_VIRTUALS (binfo) = copy_list (BINFO_VIRTUALS (binfo));
962 : :
963 : : /* Secondary vtables are laid out as part of the same structure as
964 : : the primary vtable. */
965 : 264738 : BINFO_VTABLE (binfo) = NULL_TREE;
966 : 264738 : return 1;
967 : : }
968 : :
969 : : /* Create a new vtable for BINFO which is the hierarchy dominated by
970 : : T. Return nonzero if we actually created a new vtable. */
971 : :
972 : : static int
973 : 6998506 : make_new_vtable (tree t, tree binfo)
974 : : {
975 : 6998506 : if (binfo == TYPE_BINFO (t))
976 : : /* In this case, it is *type*'s vtable we are modifying. We start
977 : : with the approximation that its vtable is that of the
978 : : immediate base class. */
979 : 6234698 : return build_primary_vtable (binfo, t);
980 : : else
981 : : /* This is our very own copy of `basetype' to play with. Later,
982 : : we will fill in all the virtual functions that override the
983 : : virtual functions in these base classes which are not defined
984 : : by the current type. */
985 : 763808 : return build_secondary_vtable (binfo);
986 : : }
987 : :
988 : : /* Make *VIRTUALS, an entry on the BINFO_VIRTUALS list for BINFO
989 : : (which is in the hierarchy dominated by T) list FNDECL as its
990 : : BV_FN. DELTA is the required constant adjustment from the `this'
991 : : pointer where the vtable entry appears to the `this' required when
992 : : the function is actually called. */
993 : :
994 : : static void
995 : 7262406 : modify_vtable_entry (tree t,
996 : : tree binfo,
997 : : tree fndecl,
998 : : tree delta,
999 : : tree *virtuals)
1000 : : {
1001 : 7262406 : tree v;
1002 : :
1003 : 7262406 : v = *virtuals;
1004 : :
1005 : 7262406 : if (fndecl != BV_FN (v)
1006 : 7262406 : || !tree_int_cst_equal (delta, BV_DELTA (v)))
1007 : : {
1008 : : /* We need a new vtable for BINFO. */
1009 : 5291353 : if (make_new_vtable (t, binfo))
1010 : : {
1011 : : /* If we really did make a new vtable, we also made a copy
1012 : : of the BINFO_VIRTUALS list. Now, we have to find the
1013 : : corresponding entry in that list. */
1014 : 0 : *virtuals = BINFO_VIRTUALS (binfo);
1015 : 0 : while (BV_FN (*virtuals) != BV_FN (v))
1016 : 0 : *virtuals = TREE_CHAIN (*virtuals);
1017 : : v = *virtuals;
1018 : : }
1019 : :
1020 : 5291353 : BV_DELTA (v) = delta;
1021 : 5291353 : BV_VCALL_INDEX (v) = NULL_TREE;
1022 : 5291353 : BV_FN (v) = fndecl;
1023 : : }
1024 : 7262406 : }
1025 : :
1026 : :
1027 : : /* Check if the object parameter of an iobj member function corresponds to
1028 : : another parameter type. CONTEXT is the class that the implicit object
1029 : : parameter is considered to refer to. */
1030 : :
1031 : : bool
1032 : 79611 : iobj_parm_corresponds_to (tree iobj_fn, tree xobj_param, tree context)
1033 : : {
1034 : 79611 : tree iobj_fn_type = TREE_TYPE (iobj_fn);
1035 : :
1036 : : /* If the iobj member function was introduced with a using declaration, the
1037 : : type of its object parameter is considered to be that of the class it was
1038 : : introduced into.
1039 : :
1040 : : [over.match.funcs.general.4]
1041 : : For non-conversion functions that are implicit object member
1042 : : functions nominated by a using-declaration in a derived class, the
1043 : : function is considered to be a member of the derived class for the purpose
1044 : : of defining the type of the implicit object parameter.
1045 : :
1046 : : Unfortunately, because of this rule, we can't just compare the xobj member
1047 : : function's DECL_CONTEXT to its object parameter.
1048 : :
1049 : : struct S;
1050 : :
1051 : : struct B {
1052 : : int f(this S&) { return 5; }
1053 : : };
1054 : :
1055 : : struct S : B {
1056 : : using B::f;
1057 : : int f() { return 10; }
1058 : : };
1059 : :
1060 : : The using declaration does not change the object parameter of B::f as it
1061 : : is an xobj member function. However, its object parameter still
1062 : : corresponds to S::f as it was declared with an object parameter of type
1063 : : S const&. The DECL_CONTEXT of B::f is B, so if we compare the type of the
1064 : : object parameter to that, it will not match. If we naively assume a
1065 : : different type from the DECL_CONTEXT for an xobj parameter means that the
1066 : : object parameters do not correspond, then the object parameters in the
1067 : : above example will be considered non-corresponding.
1068 : :
1069 : : As a result of this, B::f would incorrectly not be discarded, causing an
1070 : : ambiguity when f is called on an object of type S.
1071 : :
1072 : : This also impacts member functions with constraints as in the following
1073 : : example.
1074 : :
1075 : : template<typename = void>
1076 : : struct S;
1077 : :
1078 : : template<typename = void>
1079 : : struct B {
1080 : : int f(this S<>&) requires true { return 5; }
1081 : : };
1082 : :
1083 : : template<typename>
1084 : : struct S : B<> {
1085 : : using B<>::f;
1086 : : int f() { return 10; }
1087 : : };
1088 : :
1089 : : Once again, if we compare the DECL_CONTEXT of B<>::f to it's xobj
1090 : : parameter, it would not match. If the object parameters do not
1091 : : correspond, constraints are not taken into account, so in this example we
1092 : : would (probably) get an ambiguous lookup instead of correctly picking
1093 : : B<>::f.
1094 : :
1095 : : Because of this caveat, we must actually compare the type of the iobj
1096 : : parameter to the type of the xobj parameter, shortcuts will have these
1097 : : edge cases.
1098 : :
1099 : : Aside from the more complex reasons above, this logic also implicitly
1100 : : handles xobj parameters of pointer type, we don't have to explicitly
1101 : : check for that case. */
1102 : :
1103 : 79611 : if (!same_type_ignoring_top_level_qualifiers_p
1104 : 79611 : (context, non_reference (xobj_param)))
1105 : : return false;
1106 : :
1107 : : /* We don't get to bail yet even if we have a by-value xobj parameter,
1108 : : a by-value xobj parameter can correspond to an iobj parameter provided the
1109 : : iobj member function is not declared with a reference qualifier.
1110 : :
1111 : : From this point on, we know we are dealing with an xobj parameter that has
1112 : : an object parameter of the same type as the class it was declared in.
1113 : : We still don't know if we have a reference or by-value parameter yet
1114 : : though. */
1115 : :
1116 : 79608 : cp_ref_qualifier const iobj_ref_qual = type_memfn_rqual (iobj_fn_type);
1117 : : /* We only care about cv qualifiers when determining correspondence. */
1118 : 79608 : static constexpr cp_cv_quals cv_bits = TYPE_QUAL_VOLATILE
1119 : : | TYPE_QUAL_CONST;
1120 : 79608 : cp_cv_quals const iobj_cv_quals = type_memfn_quals (iobj_fn_type) & cv_bits;
1121 : : /* We need to ignore the ref qualifier of the xobj parameter if the iobj
1122 : : member function lacks a ref qualifier.
1123 : :
1124 : : [basic.scope.scope.3]
1125 : : Two non-static member functions have corresponding object parameters if:
1126 : : -- exactly one is an implicit object member function with no ref-qualifier
1127 : : and the types of their object parameters ([dcl.fct]), after removing
1128 : : top-level references, are the same, or
1129 : : -- their object parameters have the same type.
1130 : :
1131 : : The cv qualifiers of a by-value parameter are supposed to be discarded, so
1132 : : we ignore them.
1133 : :
1134 : : [dcl.fct.5]
1135 : : After producing the list of parameter types, any top-level cv-qualifiers
1136 : : modifying a parameter type are deleted when forming the function type.
1137 : :
1138 : : However, they still need to be taken into account when our xobj parameter
1139 : : is a reference that is being ignored (according to [basic.scope.scope.3]
1140 : : quoted above), but when we are actually dealing with a by-value xobj
1141 : : parameter we can proceed following this table.
1142 : : | iobj | xobj | equal |
1143 : : | none | none | X |
1144 : : | none | c | X |
1145 : : | none | v | X |
1146 : : | none | cv | X |
1147 : : | c | none | O |
1148 : : | c | c | O |
1149 : : | c | v | O |
1150 : : | c | cv | O |
1151 : : | v | none | O |
1152 : : | v | c | O |
1153 : : | v | v | O |
1154 : : | v | cv | O |
1155 : : | cv | none | O |
1156 : : | cv | c | O |
1157 : : | cv | v | O |
1158 : : | cv | cv | O |
1159 : :
1160 : : Additionally, if the iobj member function is ref qualified, we aren't
1161 : : ignoring the ref qualifier of the iobj parameter, so we can't be dealing
1162 : : with correspondence in that case either.
1163 : :
1164 : : So to recap, if we have a by-value xobj parameter, we know for sure that
1165 : : we aren't dealing with corresponding object parameters if the iobj member
1166 : : function has any cv-ref qualifiers. The only case where we might still be
1167 : : dealing with corresponding object parameters is when the iobj member
1168 : : function lacks any cv-ref qualification. */
1169 : 79608 : if (!TYPE_REF_P (xobj_param))
1170 : : {
1171 : 995 : if (iobj_ref_qual || iobj_cv_quals)
1172 : : return false;
1173 : : }
1174 : : else
1175 : : {
1176 : : /* We are dealing with an xobj parameter that is a reference now, but due
1177 : : to [basic.scope.scope.3] we need to ignore its ref qual. */
1178 : 157226 : cp_ref_qualifier const xobj_ref_qual = [&](){
1179 : 78613 : if (!TYPE_REF_P (xobj_param) || !iobj_ref_qual)
1180 : : return REF_QUAL_NONE;
1181 : 816 : return TYPE_REF_IS_RVALUE (xobj_param) ? REF_QUAL_RVALUE
1182 : : : REF_QUAL_LVALUE;
1183 : 78613 : }(); /* IILE. */
1184 : :
1185 : : /* Even if we are ignoring the reference qualifier, the xobj parameter
1186 : : was still a reference so we still take the cv qualifiers into
1187 : : account. */
1188 : 78613 : cp_cv_quals const xobj_cv_quals
1189 : 78613 : = cp_type_quals (TREE_TYPE (xobj_param)) & cv_bits;
1190 : :
1191 : : /* Finally, if the qualifications don't match exactly, the object
1192 : : parameters don't correspond. */
1193 : 78613 : if (iobj_ref_qual != xobj_ref_qual
1194 : 78229 : || iobj_cv_quals != xobj_cv_quals)
1195 : : return false;
1196 : : }
1197 : : /* If we got past everything else, the object parameters of fn1 and fn2
1198 : : definitely correspond. */
1199 : : return true;
1200 : : }
1201 : :
1202 : : /* True if FN and METHOD have corresponding object parms per
1203 : : [basic.scope.scope], or if one of them is a static member function (which
1204 : : are considered to have an object parm that corresponds to any other).
1205 : : CONTEXT is the class that an implicit object member function is considered
1206 : : to be a member of for the purpose of this comparison, per
1207 : : [over.match.funcs]. */
1208 : :
1209 : : bool
1210 : 398623066 : object_parms_correspond (tree fn, tree method, tree context)
1211 : : {
1212 : 398623066 : tree fn_type = TREE_TYPE (fn);
1213 : 398623066 : tree method_type = TREE_TYPE (method);
1214 : :
1215 : : /* Compare the quals on the 'this' parm. Don't compare
1216 : : the whole types, as used functions are treated as
1217 : : coming from the using class in overload resolution. */
1218 : 398623066 : if (DECL_IOBJ_MEMBER_FUNCTION_P (fn)
1219 : 398623066 : && DECL_IOBJ_MEMBER_FUNCTION_P (method))
1220 : : {
1221 : : /* Either both or neither need to be ref-qualified for
1222 : : differing quals to allow overloading. */
1223 : 396494662 : if ((FUNCTION_REF_QUALIFIED (fn_type)
1224 : 396494662 : == FUNCTION_REF_QUALIFIED (method_type))
1225 : 396494662 : && (type_memfn_quals (fn_type) != type_memfn_quals (method_type)
1226 : 379527764 : || type_memfn_rqual (fn_type) != type_memfn_rqual (method_type)))
1227 : 17077654 : return false;
1228 : 379417008 : return true;
1229 : : }
1230 : : /* Treat a static member function as corresponding to any object parm. */
1231 : 2128404 : else if (DECL_STATIC_FUNCTION_P (fn) || DECL_STATIC_FUNCTION_P (method))
1232 : : return true;
1233 : : /* Handle special correspondence rules for xobj vs xobj and xobj vs iobj
1234 : : member function declarations.
1235 : : We don't worry about static member functions here. */
1236 : 13697 : else if (DECL_XOBJ_MEMBER_FUNCTION_P (fn)
1237 : 26660 : && DECL_XOBJ_MEMBER_FUNCTION_P (method))
1238 : : {
1239 : 36741 : auto get_object_param = [] (tree fn)
1240 : : {
1241 : 24494 : return TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fn)));
1242 : : };
1243 : : /* We skip the object parameter below, check it here instead of
1244 : : making changes to that code. */
1245 : 12247 : tree fn_param = get_object_param (fn);
1246 : 12247 : tree method_param = get_object_param (method);
1247 : 12247 : if (!same_type_p (fn_param, method_param))
1248 : 2004 : return false;
1249 : : }
1250 : : else
1251 : : {
1252 : 1450 : tree xobj_fn = DECL_XOBJ_MEMBER_FUNCTION_P (fn) ? fn : method;
1253 : 1450 : tree iobj_fn = xobj_fn != fn ? fn : method;
1254 : 1450 : tree xobj_param = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (xobj_fn)));
1255 : :
1256 : 1450 : return iobj_parm_corresponds_to (iobj_fn, xobj_param, context);
1257 : : }
1258 : :
1259 : 10243 : return true;
1260 : : }
1261 : :
1262 : : /* Add method METHOD to class TYPE. If VIA_USING indicates whether
1263 : : METHOD is being injected via a using_decl. Returns true if the
1264 : : method could be added to the method vec. */
1265 : :
1266 : : bool
1267 : 292269654 : add_method (tree type, tree method, bool via_using)
1268 : : {
1269 : 292269654 : if (method == error_mark_node)
1270 : : return false;
1271 : :
1272 : 292269654 : gcc_assert (!DECL_EXTERN_C_P (method));
1273 : :
1274 : 292269654 : tree *slot = find_member_slot (type, DECL_NAME (method));
1275 : 292269654 : tree current_fns = slot ? *slot : NULL_TREE;
1276 : :
1277 : : /* See below. */
1278 : 292269654 : int losem = -1;
1279 : :
1280 : : /* Check to see if we've already got this method. */
1281 : 1253659656 : for (ovl_iterator iter (current_fns); iter; ++iter)
1282 : : {
1283 : 535258566 : tree fn = *iter;
1284 : :
1285 : 535258566 : if (TREE_CODE (fn) != TREE_CODE (method))
1286 : 135305994 : continue;
1287 : :
1288 : : /* Two using-declarations can coexist, we'll complain about ambiguity in
1289 : : overload resolution. */
1290 : 3208945 : if (via_using && iter.using_p ()
1291 : : /* Except handle inherited constructors specially. */
1292 : 403444232 : && ! DECL_CONSTRUCTOR_P (fn))
1293 : : {
1294 : 1348478 : if (fn == method)
1295 : : /* Don't add the same one twice. */
1296 : 7316 : return false;
1297 : 1348469 : continue;
1298 : : }
1299 : :
1300 : : /* [over.load] Member function declarations with the
1301 : : same name and the same parameter types cannot be
1302 : : overloaded if any of them is a static member
1303 : : function declaration.
1304 : :
1305 : : [over.load] Member function declarations with the same name and
1306 : : the same parameter-type-list as well as member function template
1307 : : declarations with the same name, the same parameter-type-list, and
1308 : : the same template parameter lists cannot be overloaded if any of
1309 : : them, but not all, have a ref-qualifier.
1310 : :
1311 : : [namespace.udecl] When a using-declaration brings names
1312 : : from a base class into a derived class scope, member
1313 : : functions in the derived class override and/or hide member
1314 : : functions with the same name and parameter types in a base
1315 : : class (rather than conflicting). */
1316 : 398604094 : if (!object_parms_correspond (fn, method, type))
1317 : 17080723 : continue;
1318 : :
1319 : 381523371 : tree fn_type = TREE_TYPE (fn);
1320 : 381523371 : tree method_type = TREE_TYPE (method);
1321 : :
1322 : 381523371 : tree real_fn = fn;
1323 : 381523371 : tree real_method = method;
1324 : :
1325 : : /* Templates and conversion ops must match return types. */
1326 : 762942047 : if ((DECL_CONV_FN_P (fn) || TREE_CODE (fn) == TEMPLATE_DECL)
1327 : 551876547 : && !same_type_p (TREE_TYPE (fn_type), TREE_TYPE (method_type)))
1328 : 3902880 : continue;
1329 : :
1330 : : /* For templates, the template parameters must be identical. */
1331 : 377620491 : if (TREE_CODE (fn) == TEMPLATE_DECL)
1332 : : {
1333 : 333109602 : if (!comp_template_parms (DECL_TEMPLATE_PARMS (fn),
1334 : 166554801 : DECL_TEMPLATE_PARMS (method)))
1335 : 130388998 : continue;
1336 : :
1337 : 36165803 : real_fn = DECL_TEMPLATE_RESULT (fn);
1338 : 36165803 : real_method = DECL_TEMPLATE_RESULT (method);
1339 : : }
1340 : :
1341 : 247231493 : tree parms1 = TYPE_ARG_TYPES (fn_type);
1342 : 247231493 : tree parms2 = TYPE_ARG_TYPES (method_type);
1343 : 247231493 : if (! DECL_STATIC_FUNCTION_P (real_fn))
1344 : 245964590 : parms1 = TREE_CHAIN (parms1);
1345 : 247231493 : if (! DECL_STATIC_FUNCTION_P (real_method))
1346 : 245956292 : parms2 = TREE_CHAIN (parms2);
1347 : :
1348 : : /* Bring back parameters omitted from an inherited ctor. The
1349 : : method and the function can have different omittedness. */
1350 : 247231493 : if (ctor_omit_inherited_parms (real_fn))
1351 : 729 : parms1 = FUNCTION_FIRST_USER_PARMTYPE (DECL_CLONED_FUNCTION (real_fn));
1352 : 247231493 : if (ctor_omit_inherited_parms (real_method))
1353 : 330 : parms2 = (FUNCTION_FIRST_USER_PARMTYPE
1354 : : (DECL_CLONED_FUNCTION (real_method)));
1355 : :
1356 : 247231493 : if (!compparms (parms1, parms2))
1357 : 245705004 : continue;
1358 : :
1359 : 1526489 : tree fn_constraints = get_constraints (fn);
1360 : 1526489 : tree method_constraints = get_constraints (method);
1361 : :
1362 : 1526489 : if (fn_constraints && method_constraints
1363 : 1239299 : && DECL_CONTEXT (fn) != type
1364 : 1526501 : && !processing_template_decl)
1365 : : {
1366 : 12 : if (TREE_CODE (fn) == TEMPLATE_DECL)
1367 : 12 : ++processing_template_decl;
1368 : 12 : if (tree outer_args = outer_template_args (fn))
1369 : 9 : fn_constraints = tsubst_constraint_info (fn_constraints,
1370 : : outer_args,
1371 : : tf_warning_or_error,
1372 : : fn);
1373 : 12 : if (tree outer_args = outer_template_args (method))
1374 : 9 : method_constraints = tsubst_constraint_info (method_constraints,
1375 : : outer_args,
1376 : : tf_warning_or_error,
1377 : : method);
1378 : 12 : if (TREE_CODE (fn) == TEMPLATE_DECL)
1379 : 12 : --processing_template_decl;
1380 : : }
1381 : :
1382 : 1526489 : if (!equivalent_constraints (fn_constraints, method_constraints))
1383 : : {
1384 : 1370263 : if (processing_template_decl)
1385 : : /* We can't check satisfaction in dependent context, wait until
1386 : : the class is instantiated. */
1387 : 180341 : continue;
1388 : :
1389 : 1189922 : special_function_kind sfk = special_memfn_p (method);
1390 : :
1391 : 2338328 : if (sfk == sfk_none
1392 : 179630 : || DECL_INHERITED_CTOR (fn)
1393 : 1279660 : || TREE_CODE (fn) == TEMPLATE_DECL)
1394 : : /* Member function templates and non-special member functions
1395 : : coexist if they are not equivalently constrained. A member
1396 : : function is not hidden by an inherited constructor. */
1397 : 1148406 : continue;
1398 : :
1399 : : /* P0848: For special member functions, deleted, unsatisfied, or
1400 : : less constrained overloads are ineligible. We implement this
1401 : : by removing them from CLASSTYPE_MEMBER_VEC. Destructors don't
1402 : : use the notion of eligibility, and the selected destructor can
1403 : : be deleted, but removing unsatisfied or less constrained
1404 : : overloads has the same effect as overload resolution. */
1405 : 41516 : bool dtor = (sfk == sfk_destructor);
1406 : 41516 : if (losem == -1)
1407 : 81907 : losem = ((!dtor && DECL_DELETED_FN (method))
1408 : 81865 : || !constraints_satisfied_p (method));
1409 : 40443 : bool losef = ((!dtor && DECL_DELETED_FN (fn))
1410 : 47348 : || !constraints_satisfied_p (fn));
1411 : 41516 : int win;
1412 : 41516 : if (losem || losef)
1413 : 40577 : win = losem - losef;
1414 : : else
1415 : 939 : win = more_constrained (fn, method);
1416 : 41516 : if (win > 0)
1417 : : /* Leave FN in the method vec, discard METHOD. */
1418 : : return false;
1419 : 35621 : else if (win < 0)
1420 : : {
1421 : : /* Remove FN, add METHOD. */
1422 : 32048 : current_fns = iter.remove_node (current_fns);
1423 : 32048 : continue;
1424 : : }
1425 : : else
1426 : : /* Let them coexist for now. */
1427 : 3573 : continue;
1428 : : }
1429 : :
1430 : : /* If these are versions of the same function, process and
1431 : : move on. */
1432 : 156253 : if (TREE_CODE (fn) == FUNCTION_DECL
1433 : 156226 : && maybe_version_functions (method, fn))
1434 : 27 : continue;
1435 : :
1436 : 312398 : if (DECL_INHERITED_CTOR (method))
1437 : : {
1438 : 2300 : if (!DECL_INHERITED_CTOR (fn))
1439 : : /* Defer to the other function. */
1440 : 1078 : return false;
1441 : :
1442 : 105 : tree basem = DECL_INHERITED_CTOR_BASE (method);
1443 : 210 : tree basef = DECL_INHERITED_CTOR_BASE (fn);
1444 : 105 : if (flag_new_inheriting_ctors)
1445 : : {
1446 : 102 : if (basem == basef)
1447 : : {
1448 : : /* Inheriting the same constructor along different
1449 : : paths, combine them. */
1450 : 90 : SET_DECL_INHERITED_CTOR
1451 : : (fn, ovl_make (DECL_INHERITED_CTOR (method),
1452 : : DECL_INHERITED_CTOR (fn)));
1453 : : /* And discard the new one. */
1454 : 30 : return false;
1455 : : }
1456 : : else
1457 : : /* Inherited ctors can coexist until overload
1458 : : resolution. */
1459 : 72 : continue;
1460 : : }
1461 : :
1462 : 3 : auto_diagnostic_group d;
1463 : 3 : error_at (DECL_SOURCE_LOCATION (method),
1464 : : "%q#D conflicts with version inherited from %qT",
1465 : : method, basef);
1466 : 3 : inform (DECL_SOURCE_LOCATION (fn),
1467 : : "version inherited from %qT declared here",
1468 : : basef);
1469 : 3 : return false;
1470 : 3 : }
1471 : :
1472 : 155049 : if (via_using)
1473 : : /* Defer to the local function. */
1474 : : return false;
1475 : 155037 : else if (iter.using_p ()
1476 : 155037 : || (flag_new_inheriting_ctors
1477 : 192508 : && DECL_INHERITED_CTOR (fn)))
1478 : : {
1479 : : /* Remove the inherited function. */
1480 : 154715 : current_fns = iter.remove_node (current_fns);
1481 : 154715 : continue;
1482 : : }
1483 : : else
1484 : : {
1485 : 322 : auto_diagnostic_group d;
1486 : 322 : error_at (DECL_SOURCE_LOCATION (method),
1487 : : "%q#D cannot be overloaded with %q#D", method, fn);
1488 : 322 : inform (DECL_SOURCE_LOCATION (fn),
1489 : : "previous declaration %q#D", fn);
1490 : 322 : return false;
1491 : 322 : }
1492 : : }
1493 : :
1494 : 292262338 : current_fns = ovl_insert (method, current_fns, via_using);
1495 : :
1496 : 538726930 : if (!COMPLETE_TYPE_P (type) && !DECL_CONV_FN_P (method)
1497 : 536800089 : && !push_class_level_binding (DECL_NAME (method), current_fns))
1498 : : return false;
1499 : :
1500 : 292262320 : if (!slot)
1501 : 132973385 : slot = add_member_slot (type, DECL_NAME (method));
1502 : :
1503 : : /* Maintain TYPE_HAS_USER_CONSTRUCTOR, etc. */
1504 : 292262320 : grok_special_member_properties (method);
1505 : :
1506 : 292262320 : *slot = current_fns;
1507 : :
1508 : 292262320 : return true;
1509 : : }
1510 : :
1511 : : /* Subroutines of finish_struct. */
1512 : :
1513 : : /* Change the access of FDECL to ACCESS in T. Return 1 if change was
1514 : : legit, otherwise return 0. */
1515 : :
1516 : : static int
1517 : 2530698 : alter_access (tree t, tree fdecl, tree access)
1518 : : {
1519 : 2530698 : tree elem;
1520 : :
1521 : 2530698 : retrofit_lang_decl (fdecl);
1522 : :
1523 : 2530698 : gcc_assert (!DECL_DISCRIMINATOR_P (fdecl));
1524 : :
1525 : 2530698 : elem = purpose_member (t, DECL_ACCESS (fdecl));
1526 : 2530698 : if (elem)
1527 : : {
1528 : 0 : if (TREE_VALUE (elem) != access)
1529 : : {
1530 : 0 : if (TREE_CODE (TREE_TYPE (fdecl)) == FUNCTION_DECL)
1531 : 0 : error ("conflicting access specifications for method"
1532 : 0 : " %q+D, ignored", TREE_TYPE (fdecl));
1533 : : else
1534 : 0 : error ("conflicting access specifications for field %qE, ignored",
1535 : 0 : DECL_NAME (fdecl));
1536 : : }
1537 : : else
1538 : : {
1539 : : /* They're changing the access to the same thing they changed
1540 : : it to before. That's OK. */
1541 : : ;
1542 : : }
1543 : : }
1544 : : else
1545 : : {
1546 : 2530698 : perform_or_defer_access_check (TYPE_BINFO (t), fdecl, fdecl,
1547 : : tf_warning_or_error);
1548 : 2530698 : DECL_ACCESS (fdecl) = tree_cons (t, access, DECL_ACCESS (fdecl));
1549 : 2530698 : return 1;
1550 : : }
1551 : : return 0;
1552 : : }
1553 : :
1554 : : /* Return the access node for DECL's access in its enclosing class. */
1555 : :
1556 : : tree
1557 : 564973 : declared_access (tree decl)
1558 : : {
1559 : 564973 : return (TREE_PRIVATE (decl) ? access_private_node
1560 : 554494 : : TREE_PROTECTED (decl) ? access_protected_node
1561 : 564973 : : access_public_node);
1562 : : }
1563 : :
1564 : : /* If DECL is a non-dependent using of non-ctor function members, push them
1565 : : and return true, otherwise return false. Called from
1566 : : finish_member_declaration. */
1567 : :
1568 : : bool
1569 : 162208788 : maybe_push_used_methods (tree decl)
1570 : : {
1571 : 162208788 : if (TREE_CODE (decl) != USING_DECL)
1572 : : return false;
1573 : 3011414 : tree used = strip_using_decl (decl);
1574 : 3011414 : if (!used || !is_overloaded_fn (used))
1575 : : return false;
1576 : :
1577 : : /* Add the functions to CLASSTYPE_MEMBER_VEC so that overload resolution
1578 : : works within the class body. */
1579 : 5134151 : for (tree f : ovl_range (used))
1580 : : {
1581 : 4848538 : if (DECL_CONSTRUCTOR_P (f))
1582 : : /* Inheriting constructors are handled separately. */
1583 : 66300 : return false;
1584 : :
1585 : 2357969 : bool added = add_method (current_class_type, f, true);
1586 : :
1587 : 2357969 : if (added)
1588 : 2357948 : alter_access (current_class_type, f, current_access_specifier);
1589 : :
1590 : : /* If add_method returns false because f was already declared, look
1591 : : for a duplicate using-declaration. */
1592 : : else
1593 : 72 : for (tree d = TYPE_FIELDS (current_class_type); d; d = DECL_CHAIN (d))
1594 : 57 : if (TREE_CODE (d) == USING_DECL
1595 : 24 : && DECL_NAME (d) == DECL_NAME (decl)
1596 : 66 : && same_type_p (USING_DECL_SCOPE (d), USING_DECL_SCOPE (decl)))
1597 : : {
1598 : 6 : diagnose_name_conflict (decl, d);
1599 : 6 : break;
1600 : : }
1601 : : }
1602 : 1488312 : return true;
1603 : : }
1604 : :
1605 : : /* Process the USING_DECL, which is a member of T. */
1606 : :
1607 : : static void
1608 : 2042297 : handle_using_decl (tree using_decl, tree t)
1609 : : {
1610 : 2042297 : tree decl = USING_DECL_DECLS (using_decl);
1611 : :
1612 : 2042297 : gcc_assert (!processing_template_decl && decl);
1613 : :
1614 : 2042297 : cp_emit_debug_info_for_using (decl, t);
1615 : :
1616 : 2042297 : if (is_overloaded_fn (decl))
1617 : : /* Handled in maybe_push_used_methods. */
1618 : 2042297 : return;
1619 : :
1620 : 554151 : tree name = DECL_NAME (using_decl);
1621 : 554151 : tree old_value = lookup_member (t, name, /*protect=*/0, /*want_type=*/false,
1622 : : tf_warning_or_error);
1623 : 554151 : if (old_value)
1624 : : {
1625 : 554151 : old_value = OVL_FIRST (old_value);
1626 : :
1627 : 554151 : if (DECL_P (old_value) && DECL_CONTEXT (old_value) == t)
1628 : : /* OK */;
1629 : : else
1630 : : old_value = NULL_TREE;
1631 : : }
1632 : :
1633 : 0 : if (! old_value)
1634 : : ;
1635 : 0 : else if (is_overloaded_fn (old_value))
1636 : : {
1637 : 0 : auto_diagnostic_group d;
1638 : 0 : error_at (DECL_SOURCE_LOCATION (using_decl), "%qD invalid in %q#T "
1639 : : "because of local method %q#D with same name",
1640 : : using_decl, t, old_value);
1641 : 0 : inform (DECL_SOURCE_LOCATION (old_value),
1642 : : "local method %q#D declared here", old_value);
1643 : 0 : return;
1644 : 0 : }
1645 : 0 : else if (!DECL_ARTIFICIAL (old_value))
1646 : : {
1647 : 0 : auto_diagnostic_group d;
1648 : 0 : error_at (DECL_SOURCE_LOCATION (using_decl), "%qD invalid in %q#T "
1649 : : "because of local member %q#D with same name",
1650 : : using_decl, t, old_value);
1651 : 0 : inform (DECL_SOURCE_LOCATION (old_value),
1652 : : "local member %q#D declared here", old_value);
1653 : 0 : return;
1654 : 0 : }
1655 : :
1656 : 554151 : iloc_sentinel ils (DECL_SOURCE_LOCATION (using_decl));
1657 : 554151 : tree access = declared_access (using_decl);
1658 : :
1659 : : /* Make type T see field decl FDECL with access ACCESS. */
1660 : 554151 : if (USING_DECL_UNRELATED_P (using_decl))
1661 : : {
1662 : : /* C++20 using enum can import non-inherited enumerators into class
1663 : : scope. We implement that by making a copy of the CONST_DECL for which
1664 : : CONST_DECL_USING_P is true. */
1665 : 381401 : gcc_assert (TREE_CODE (decl) == CONST_DECL);
1666 : :
1667 : 381401 : auto cas = make_temp_override (current_access_specifier, access);
1668 : 381401 : tree copy = copy_decl (decl);
1669 : 381401 : DECL_CONTEXT (copy) = t;
1670 : 381401 : DECL_ARTIFICIAL (copy) = true;
1671 : : /* We emitted debug info for the USING_DECL above; make sure we don't
1672 : : also emit anything for this clone. */
1673 : 381401 : DECL_IGNORED_P (copy) = true;
1674 : 381401 : DECL_SOURCE_LOCATION (copy) = DECL_SOURCE_LOCATION (using_decl);
1675 : 381401 : finish_member_declaration (copy);
1676 : 381401 : DECL_ABSTRACT_ORIGIN (copy) = decl;
1677 : 381401 : }
1678 : : else
1679 : 172750 : alter_access (t, decl, access);
1680 : 554151 : }
1681 : :
1682 : : /* Data structure for find_abi_tags_r, below. */
1683 : :
1684 : : struct abi_tag_data
1685 : : {
1686 : : tree t; // The type that we're checking for missing tags.
1687 : : tree subob; // The subobject of T that we're getting tags from.
1688 : : tree tags; // error_mark_node for diagnostics, or a list of missing tags.
1689 : : };
1690 : :
1691 : : /* Subroutine of find_abi_tags_r. Handle a single TAG found on the class TP
1692 : : in the context of P. TAG can be either an identifier (the DECL_NAME of
1693 : : a tag NAMESPACE_DECL) or a STRING_CST (a tag attribute). */
1694 : :
1695 : : static void
1696 : 15303548 : check_tag (tree tag, tree id, tree *tp, abi_tag_data *p)
1697 : : {
1698 : 15303548 : if (!IDENTIFIER_MARKED (id))
1699 : : {
1700 : 5125435 : if (p->tags != error_mark_node)
1701 : : {
1702 : : /* We're collecting tags from template arguments or from
1703 : : the type of a variable or function return type. */
1704 : 5125417 : p->tags = tree_cons (NULL_TREE, tag, p->tags);
1705 : :
1706 : : /* Don't inherit this tag multiple times. */
1707 : 5125417 : IDENTIFIER_MARKED (id) = true;
1708 : :
1709 : 5125417 : if (TYPE_P (p->t))
1710 : : {
1711 : : /* Tags inherited from type template arguments are only used
1712 : : to avoid warnings. */
1713 : 5103147 : ABI_TAG_IMPLICIT (p->tags) = true;
1714 : 5103147 : return;
1715 : : }
1716 : : /* For functions and variables we want to warn, too. */
1717 : : }
1718 : :
1719 : : /* Otherwise we're diagnosing missing tags. */
1720 : 22288 : if (TREE_CODE (p->t) == FUNCTION_DECL)
1721 : : {
1722 : 21872 : auto_diagnostic_group d;
1723 : 21872 : if (warning (OPT_Wabi_tag, "%qD inherits the %E ABI tag "
1724 : : "that %qT (used in its return type) has",
1725 : : p->t, tag, *tp))
1726 : 3 : inform (location_of (*tp), "%qT declared here", *tp);
1727 : 21872 : }
1728 : 416 : else if (VAR_P (p->t))
1729 : : {
1730 : 398 : auto_diagnostic_group d;
1731 : 398 : if (warning (OPT_Wabi_tag, "%qD inherits the %E ABI tag "
1732 : : "that %qT (used in its type) has", p->t, tag, *tp))
1733 : 3 : inform (location_of (*tp), "%qT declared here", *tp);
1734 : 398 : }
1735 : 18 : else if (TYPE_P (p->subob))
1736 : : {
1737 : 6 : auto_diagnostic_group d;
1738 : 6 : if (warning (OPT_Wabi_tag, "%qT does not have the %E ABI tag "
1739 : : "that base %qT has", p->t, tag, p->subob))
1740 : 6 : inform (location_of (p->subob), "%qT declared here",
1741 : : p->subob);
1742 : 6 : }
1743 : : else
1744 : : {
1745 : 12 : auto_diagnostic_group d;
1746 : 12 : if (warning (OPT_Wabi_tag, "%qT does not have the %E ABI tag "
1747 : : "that %qT (used in the type of %qD) has",
1748 : : p->t, tag, *tp, p->subob))
1749 : : {
1750 : 12 : inform (location_of (p->subob), "%qD declared here",
1751 : : p->subob);
1752 : 12 : inform (location_of (*tp), "%qT declared here", *tp);
1753 : : }
1754 : 12 : }
1755 : : }
1756 : : }
1757 : :
1758 : : /* Find all the ABI tags in the attribute list ATTR and either call
1759 : : check_tag (if TP is non-null) or set IDENTIFIER_MARKED to val. */
1760 : :
1761 : : static void
1762 : 2118358982 : mark_or_check_attr_tags (tree attr, tree *tp, abi_tag_data *p, bool val)
1763 : : {
1764 : 2118358982 : if (!attr)
1765 : : return;
1766 : 255255035 : for (; (attr = lookup_attribute ("abi_tag", attr));
1767 : 91865631 : attr = TREE_CHAIN (attr))
1768 : 183731292 : for (tree list = TREE_VALUE (attr); list;
1769 : 91865661 : list = TREE_CHAIN (list))
1770 : : {
1771 : 91865661 : tree tag = TREE_VALUE (list);
1772 : 91865661 : tree id = get_identifier (TREE_STRING_POINTER (tag));
1773 : 91865661 : if (tp)
1774 : 15303548 : check_tag (tag, id, tp, p);
1775 : : else
1776 : 76562113 : IDENTIFIER_MARKED (id) = val;
1777 : : }
1778 : : }
1779 : :
1780 : : /* Find all the ABI tags on T and its enclosing scopes and either call
1781 : : check_tag (if TP is non-null) or set IDENTIFIER_MARKED to val. */
1782 : :
1783 : : static void
1784 : 794603235 : mark_or_check_tags (tree t, tree *tp, abi_tag_data *p, bool val)
1785 : : {
1786 : 2912962217 : while (t != global_namespace)
1787 : : {
1788 : 2118358982 : tree attr;
1789 : 2118358982 : if (TYPE_P (t))
1790 : : {
1791 : 804768327 : attr = TYPE_ATTRIBUTES (t);
1792 : 804768327 : t = CP_TYPE_CONTEXT (t);
1793 : : }
1794 : : else
1795 : : {
1796 : 1313590655 : attr = DECL_ATTRIBUTES (t);
1797 : 1313590655 : t = CP_DECL_CONTEXT (t);
1798 : : }
1799 : 2118358982 : mark_or_check_attr_tags (attr, tp, p, val);
1800 : : }
1801 : 794603235 : }
1802 : :
1803 : : /* walk_tree callback for check_abi_tags: if the type at *TP involves any
1804 : : types with ABI tags, add the corresponding identifiers to the VEC in
1805 : : *DATA and set IDENTIFIER_MARKED. */
1806 : :
1807 : : static tree
1808 : 672990753 : find_abi_tags_r (tree *tp, int *walk_subtrees, void *data)
1809 : : {
1810 : 672990753 : if (TYPE_P (*tp) && *walk_subtrees == 1 && flag_abi_version != 14)
1811 : : /* Tell cp_walk_subtrees to look though typedefs. [PR98481] */
1812 : 436203602 : *walk_subtrees = 2;
1813 : :
1814 : 672990753 : if (!OVERLOAD_TYPE_P (*tp))
1815 : : return NULL_TREE;
1816 : :
1817 : : /* walk_tree shouldn't be walking into any subtrees of a RECORD_TYPE
1818 : : anyway, but let's make sure of it. */
1819 : 126863591 : *walk_subtrees = false;
1820 : :
1821 : 126863591 : abi_tag_data *p = static_cast<struct abi_tag_data*>(data);
1822 : :
1823 : 126863591 : mark_or_check_tags (*tp, tp, p, false);
1824 : :
1825 : 126863591 : return NULL_TREE;
1826 : : }
1827 : :
1828 : : /* walk_tree callback for mark_abi_tags: if *TP is a class, set
1829 : : IDENTIFIER_MARKED on its ABI tags. */
1830 : :
1831 : : static tree
1832 : 1079005844 : mark_abi_tags_r (tree *tp, int *walk_subtrees, void *data)
1833 : : {
1834 : 1079005844 : if (TYPE_P (*tp) && *walk_subtrees == 1 && flag_abi_version != 14)
1835 : : /* Tell cp_walk_subtrees to look though typedefs. */
1836 : 772691036 : *walk_subtrees = 2;
1837 : :
1838 : 1079005844 : if (!OVERLOAD_TYPE_P (*tp))
1839 : : return NULL_TREE;
1840 : :
1841 : : /* walk_tree shouldn't be walking into any subtrees of a RECORD_TYPE
1842 : : anyway, but let's make sure of it. */
1843 : 147127102 : *walk_subtrees = false;
1844 : :
1845 : 147127102 : bool *valp = static_cast<bool*>(data);
1846 : :
1847 : 147127102 : mark_or_check_tags (*tp, NULL, NULL, *valp);
1848 : :
1849 : 147127102 : return NULL_TREE;
1850 : : }
1851 : :
1852 : : /* Set IDENTIFIER_MARKED on all the ABI tags on T and its enclosing
1853 : : scopes. */
1854 : :
1855 : : static void
1856 : 520612542 : mark_abi_tags (tree t, bool val)
1857 : : {
1858 : 520612542 : mark_or_check_tags (t, NULL, NULL, val);
1859 : 520612542 : if (DECL_P (t))
1860 : : {
1861 : 656111752 : if (DECL_LANG_SPECIFIC (t) && DECL_USE_TEMPLATE (t)
1862 : 563373360 : && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (t)))
1863 : : {
1864 : : /* Template arguments are part of the signature. */
1865 : 15905696 : tree level = INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (t));
1866 : 40379230 : for (int j = 0; j < TREE_VEC_LENGTH (level); ++j)
1867 : : {
1868 : 24473534 : tree arg = TREE_VEC_ELT (level, j);
1869 : 24473534 : cp_walk_tree_without_duplicates (&arg, mark_abi_tags_r, &val);
1870 : : }
1871 : : }
1872 : 329297868 : if (TREE_CODE (t) == FUNCTION_DECL)
1873 : : /* A function's parameter types are part of the signature, so
1874 : : we don't need to inherit any tags that are also in them. */
1875 : 881897464 : for (tree arg = FUNCTION_FIRST_USER_PARMTYPE (t); arg;
1876 : 591914044 : arg = TREE_CHAIN (arg))
1877 : 591914044 : cp_walk_tree_without_duplicates (&TREE_VALUE (arg),
1878 : : mark_abi_tags_r, &val);
1879 : : }
1880 : 520612542 : }
1881 : :
1882 : : /* Check that T has all the ABI tags that subobject SUBOB has, or
1883 : : warn if not. If T is a (variable or function) declaration, also
1884 : : return any missing tags, and add them to T if JUST_CHECKING is false. */
1885 : :
1886 : : static tree
1887 : 201830911 : check_abi_tags (tree t, tree subob, bool just_checking = false)
1888 : : {
1889 : 201830911 : bool inherit = DECL_P (t);
1890 : :
1891 : 201830911 : if (!inherit && !warn_abi_tag)
1892 : : return NULL_TREE;
1893 : :
1894 : 164829360 : tree decl = TYPE_P (t) ? TYPE_NAME (t) : t;
1895 : 164829360 : if (!TREE_PUBLIC (decl))
1896 : : /* No need to worry about things local to this TU. */
1897 : : return NULL_TREE;
1898 : :
1899 : 164648955 : mark_abi_tags (t, true);
1900 : :
1901 : 164648955 : tree subtype = TYPE_P (subob) ? subob : TREE_TYPE (subob);
1902 : 164648955 : struct abi_tag_data data = { t, subob, error_mark_node };
1903 : 164648955 : if (inherit)
1904 : 164648934 : data.tags = NULL_TREE;
1905 : :
1906 : 164648955 : cp_walk_tree_without_duplicates (&subtype, find_abi_tags_r, &data);
1907 : :
1908 : 164648955 : if (!(inherit && data.tags))
1909 : : /* We don't need to do anything with data.tags. */;
1910 : 22255 : else if (just_checking)
1911 : 75 : for (tree t = data.tags; t; t = TREE_CHAIN (t))
1912 : : {
1913 : 42 : tree id = get_identifier (TREE_STRING_POINTER (TREE_VALUE (t)));
1914 : 42 : IDENTIFIER_MARKED (id) = false;
1915 : : }
1916 : : else
1917 : : {
1918 : 22222 : tree attr = lookup_attribute ("abi_tag", DECL_ATTRIBUTES (t));
1919 : 22222 : if (attr)
1920 : 0 : TREE_VALUE (attr) = chainon (data.tags, TREE_VALUE (attr));
1921 : : else
1922 : 22222 : DECL_ATTRIBUTES (t)
1923 : 44444 : = tree_cons (abi_tag_identifier, data.tags, DECL_ATTRIBUTES (t));
1924 : : }
1925 : :
1926 : 164648955 : mark_abi_tags (t, false);
1927 : :
1928 : 164648955 : return data.tags;
1929 : : }
1930 : :
1931 : : /* Check that DECL has all the ABI tags that are used in parts of its type
1932 : : that are not reflected in its mangled name. */
1933 : :
1934 : : void
1935 : 175108112 : check_abi_tags (tree decl)
1936 : : {
1937 : 175108112 : if (VAR_P (decl))
1938 : 19761064 : check_abi_tags (decl, TREE_TYPE (decl));
1939 : 155347048 : else if (TREE_CODE (decl) == FUNCTION_DECL
1940 : 155343878 : && !DECL_CONV_FN_P (decl)
1941 : 308896106 : && !mangle_return_type_p (decl))
1942 : 144984711 : check_abi_tags (decl, TREE_TYPE (TREE_TYPE (decl)));
1943 : 175108112 : }
1944 : :
1945 : : /* Return any ABI tags that are used in parts of the type of DECL
1946 : : that are not reflected in its mangled name. This function is only
1947 : : used in backward-compatible mangling for ABI <11. */
1948 : :
1949 : : tree
1950 : 84289 : missing_abi_tags (tree decl)
1951 : : {
1952 : 84289 : if (VAR_P (decl))
1953 : 1114 : return check_abi_tags (decl, TREE_TYPE (decl), true);
1954 : 83175 : else if (TREE_CODE (decl) == FUNCTION_DECL
1955 : : /* Don't check DECL_CONV_FN_P here like we do in check_abi_tags, so
1956 : : that we can use this function for setting need_abi_warning
1957 : : regardless of the current flag_abi_version. */
1958 : 83175 : && !mangle_return_type_p (decl))
1959 : 82447 : return check_abi_tags (decl, TREE_TYPE (TREE_TYPE (decl)), true);
1960 : : else
1961 : 728 : return NULL_TREE;
1962 : : }
1963 : :
1964 : : void
1965 : 96004483 : inherit_targ_abi_tags (tree t)
1966 : : {
1967 : 95657316 : if (!CLASS_TYPE_P (t)
1968 : 191661799 : || CLASSTYPE_TEMPLATE_INFO (t) == NULL_TREE)
1969 : 347167 : return;
1970 : :
1971 : 95657316 : mark_abi_tags (t, true);
1972 : :
1973 : 95657316 : tree args = CLASSTYPE_TI_ARGS (t);
1974 : 95657316 : struct abi_tag_data data = { t, NULL_TREE, NULL_TREE };
1975 : 573944078 : for (int i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
1976 : : {
1977 : 196218982 : tree level = TMPL_ARGS_LEVEL (args, i+1);
1978 : 273249309 : for (int j = 0; j < TREE_VEC_LENGTH (level); ++j)
1979 : : {
1980 : 175139818 : tree arg = TREE_VEC_ELT (level, j);
1981 : 175139818 : data.subob = arg;
1982 : 175139818 : cp_walk_tree_without_duplicates (&arg, find_abi_tags_r, &data);
1983 : : }
1984 : : }
1985 : :
1986 : : // If we found some tags on our template arguments, add them to our
1987 : : // abi_tag attribute.
1988 : 95657316 : if (data.tags)
1989 : : {
1990 : 5103147 : tree attr = lookup_attribute ("abi_tag", TYPE_ATTRIBUTES (t));
1991 : 5103147 : if (attr)
1992 : 3 : TREE_VALUE (attr) = chainon (data.tags, TREE_VALUE (attr));
1993 : : else
1994 : 5103144 : TYPE_ATTRIBUTES (t)
1995 : 10206288 : = tree_cons (abi_tag_identifier, data.tags, TYPE_ATTRIBUTES (t));
1996 : : }
1997 : :
1998 : 95657316 : mark_abi_tags (t, false);
1999 : : }
2000 : :
2001 : : /* Return true, iff class T has a non-virtual destructor that is
2002 : : accessible from outside the class heirarchy (i.e. is public, or
2003 : : there's a suitable friend. */
2004 : :
2005 : : static bool
2006 : 99 : accessible_nvdtor_p (tree t)
2007 : : {
2008 : 99 : tree dtor = CLASSTYPE_DESTRUCTOR (t);
2009 : :
2010 : : /* An implicitly declared destructor is always public. And,
2011 : : if it were virtual, we would have created it by now. */
2012 : 99 : if (!dtor)
2013 : : return true;
2014 : :
2015 : 78 : if (DECL_VINDEX (dtor))
2016 : : return false; /* Virtual */
2017 : :
2018 : 42 : if (!TREE_PRIVATE (dtor) && !TREE_PROTECTED (dtor))
2019 : : return true; /* Public */
2020 : :
2021 : 30 : if (CLASSTYPE_FRIEND_CLASSES (t)
2022 : 30 : || DECL_FRIENDLIST (TYPE_MAIN_DECL (t)))
2023 : 12 : return true; /* Has friends */
2024 : :
2025 : : return false;
2026 : : }
2027 : :
2028 : : /* Run through the base classes of T, updating CANT_HAVE_CONST_CTOR_P,
2029 : : and NO_CONST_ASN_REF_P. Also set flag bits in T based on
2030 : : properties of the bases. */
2031 : :
2032 : : static void
2033 : 39307783 : check_bases (tree t,
2034 : : int* cant_have_const_ctor_p,
2035 : : int* no_const_asn_ref_p)
2036 : : {
2037 : 39307783 : int i;
2038 : 39307783 : bool seen_non_virtual_nearly_empty_base_p = 0;
2039 : 39307783 : int seen_tm_mask = 0;
2040 : 39307783 : tree base_binfo;
2041 : 39307783 : tree binfo;
2042 : 39307783 : tree field = NULL_TREE;
2043 : :
2044 : 39307783 : if (!CLASSTYPE_NON_STD_LAYOUT (t))
2045 : 182303443 : for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
2046 : 149042189 : if (TREE_CODE (field) == FIELD_DECL)
2047 : : break;
2048 : :
2049 : 60021209 : for (binfo = TYPE_BINFO (t), i = 0;
2050 : 60021209 : BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
2051 : : {
2052 : 20713426 : tree basetype = TREE_TYPE (base_binfo);
2053 : :
2054 : 20713426 : gcc_assert (COMPLETE_TYPE_P (basetype));
2055 : :
2056 : 20713426 : if (CLASSTYPE_FINAL (basetype))
2057 : 14 : error ("cannot derive from %<final%> base %qT in derived type %qT",
2058 : : basetype, t);
2059 : :
2060 : : /* If any base class is non-literal, so is the derived class. */
2061 : 20713426 : if (!CLASSTYPE_LITERAL_P (basetype))
2062 : 2009778 : CLASSTYPE_LITERAL_P (t) = false;
2063 : :
2064 : : /* If the base class doesn't have copy constructors or
2065 : : assignment operators that take const references, then the
2066 : : derived class cannot have such a member automatically
2067 : : generated. */
2068 : 20713426 : if (TYPE_HAS_COPY_CTOR (basetype)
2069 : 20713426 : && ! TYPE_HAS_CONST_COPY_CTOR (basetype))
2070 : 60 : *cant_have_const_ctor_p = 1;
2071 : 20713426 : if (TYPE_HAS_COPY_ASSIGN (basetype)
2072 : 20713426 : && !TYPE_HAS_CONST_COPY_ASSIGN (basetype))
2073 : 18 : *no_const_asn_ref_p = 1;
2074 : :
2075 : 20713426 : if (BINFO_VIRTUAL_P (base_binfo))
2076 : : /* A virtual base does not effect nearly emptiness. */
2077 : : ;
2078 : 20664975 : else if (CLASSTYPE_NEARLY_EMPTY_P (basetype))
2079 : : {
2080 : 277349 : if (seen_non_virtual_nearly_empty_base_p)
2081 : : /* And if there is more than one nearly empty base, then the
2082 : : derived class is not nearly empty either. */
2083 : 1085 : CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
2084 : : else
2085 : : /* Remember we've seen one. */
2086 : : seen_non_virtual_nearly_empty_base_p = 1;
2087 : : }
2088 : 20387626 : else if (!is_empty_class (basetype))
2089 : : /* If the base class is not empty or nearly empty, then this
2090 : : class cannot be nearly empty. */
2091 : 1942185 : CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
2092 : :
2093 : : /* A lot of properties from the bases also apply to the derived
2094 : : class. */
2095 : 20713426 : TYPE_NEEDS_CONSTRUCTING (t) |= TYPE_NEEDS_CONSTRUCTING (basetype);
2096 : 62140278 : TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t)
2097 : 20713426 : |= TYPE_HAS_NONTRIVIAL_DESTRUCTOR (basetype);
2098 : 20713426 : TYPE_HAS_COMPLEX_COPY_ASSIGN (t)
2099 : 20713426 : |= (TYPE_HAS_COMPLEX_COPY_ASSIGN (basetype)
2100 : 20713426 : || !TYPE_HAS_COPY_ASSIGN (basetype));
2101 : 41426852 : TYPE_HAS_COMPLEX_COPY_CTOR (t) |= (TYPE_HAS_COMPLEX_COPY_CTOR (basetype)
2102 : 20713426 : || !TYPE_HAS_COPY_CTOR (basetype));
2103 : 62140278 : TYPE_HAS_COMPLEX_MOVE_ASSIGN (t)
2104 : 20713426 : |= TYPE_HAS_COMPLEX_MOVE_ASSIGN (basetype);
2105 : 20713426 : TYPE_HAS_COMPLEX_MOVE_CTOR (t) |= TYPE_HAS_COMPLEX_MOVE_CTOR (basetype);
2106 : 20713426 : TYPE_POLYMORPHIC_P (t) |= TYPE_POLYMORPHIC_P (basetype);
2107 : 62140278 : CLASSTYPE_CONTAINS_EMPTY_CLASS_P (t)
2108 : 20713426 : |= CLASSTYPE_CONTAINS_EMPTY_CLASS_P (basetype);
2109 : 41426852 : TYPE_HAS_COMPLEX_DFLT (t) |= (!TYPE_HAS_DEFAULT_CONSTRUCTOR (basetype)
2110 : 20713426 : || TYPE_HAS_COMPLEX_DFLT (basetype));
2111 : 20713426 : SET_CLASSTYPE_READONLY_FIELDS_NEED_INIT
2112 : : (t, CLASSTYPE_READONLY_FIELDS_NEED_INIT (t)
2113 : : | CLASSTYPE_READONLY_FIELDS_NEED_INIT (basetype));
2114 : 20713426 : SET_CLASSTYPE_REF_FIELDS_NEED_INIT
2115 : : (t, CLASSTYPE_REF_FIELDS_NEED_INIT (t)
2116 : : | CLASSTYPE_REF_FIELDS_NEED_INIT (basetype));
2117 : 20713426 : if (TYPE_HAS_MUTABLE_P (basetype))
2118 : 971658 : CLASSTYPE_HAS_MUTABLE (t) = 1;
2119 : :
2120 : : /* A standard-layout class is a class that:
2121 : : ...
2122 : : * has no non-standard-layout base classes, */
2123 : 20713426 : CLASSTYPE_NON_STD_LAYOUT (t) |= CLASSTYPE_NON_STD_LAYOUT (basetype);
2124 : 20713426 : if (!CLASSTYPE_NON_STD_LAYOUT (t))
2125 : : {
2126 : 18821455 : tree basefield;
2127 : : /* ...has no base classes of the same type as the first non-static
2128 : : data member... */
2129 : 511114 : if (field && DECL_CONTEXT (field) == t
2130 : 19276992 : && (same_type_ignoring_top_level_qualifiers_p
2131 : 455537 : (TREE_TYPE (field), basetype)))
2132 : 132 : CLASSTYPE_NON_STD_LAYOUT (t) = 1;
2133 : : /* DR 1813:
2134 : : ...has at most one base class subobject of any given type... */
2135 : 18821323 : else if (CLASSTYPE_REPEATED_BASE_P (t))
2136 : 370 : CLASSTYPE_NON_STD_LAYOUT (t) = 1;
2137 : : else
2138 : : /* ...has all non-static data members and bit-fields in the class
2139 : : and its base classes first declared in the same class. */
2140 : 264708879 : for (basefield = TYPE_FIELDS (basetype); basefield;
2141 : 245887926 : basefield = DECL_CHAIN (basefield))
2142 : 246697480 : if (TREE_CODE (basefield) == FIELD_DECL
2143 : 247640459 : && !(DECL_FIELD_IS_BASE (basefield)
2144 : 942979 : && is_empty_field (basefield)))
2145 : : {
2146 : 809554 : if (field)
2147 : 140924 : CLASSTYPE_NON_STD_LAYOUT (t) = 1;
2148 : : else
2149 : : field = basefield;
2150 : : break;
2151 : : }
2152 : : }
2153 : :
2154 : : /* Don't bother collecting tm attributes if transactional memory
2155 : : support is not enabled. */
2156 : 20713426 : if (flag_tm)
2157 : : {
2158 : 1270 : tree tm_attr = find_tm_attribute (TYPE_ATTRIBUTES (basetype));
2159 : 1270 : if (tm_attr)
2160 : 15 : seen_tm_mask |= tm_attr_to_mask (tm_attr);
2161 : : }
2162 : :
2163 : 20713426 : check_abi_tags (t, basetype);
2164 : : }
2165 : :
2166 : : /* If one of the base classes had TM attributes, and the current class
2167 : : doesn't define its own, then the current class inherits one. */
2168 : 39307783 : if (seen_tm_mask && !find_tm_attribute (TYPE_ATTRIBUTES (t)))
2169 : : {
2170 : 12 : tree tm_attr = tm_mask_to_attr (least_bit_hwi (seen_tm_mask));
2171 : 12 : TYPE_ATTRIBUTES (t) = tree_cons (tm_attr, NULL, TYPE_ATTRIBUTES (t));
2172 : : }
2173 : 39307783 : }
2174 : :
2175 : : /* Determine all the primary bases within T. Sets BINFO_PRIMARY_BASE_P for
2176 : : those that are primaries. Sets BINFO_LOST_PRIMARY_P for those
2177 : : that have had a nearly-empty virtual primary base stolen by some
2178 : : other base in the hierarchy. Determines CLASSTYPE_PRIMARY_BASE for
2179 : : T. */
2180 : :
2181 : : static void
2182 : 39307783 : determine_primary_bases (tree t)
2183 : : {
2184 : 39307783 : unsigned i;
2185 : 39307783 : tree primary = NULL_TREE;
2186 : 39307783 : tree type_binfo = TYPE_BINFO (t);
2187 : 39307783 : tree base_binfo;
2188 : :
2189 : : /* Determine the primary bases of our bases. */
2190 : 63253253 : for (base_binfo = TREE_CHAIN (type_binfo); base_binfo;
2191 : 23945470 : base_binfo = TREE_CHAIN (base_binfo))
2192 : : {
2193 : 23945470 : tree primary = CLASSTYPE_PRIMARY_BINFO (BINFO_TYPE (base_binfo));
2194 : :
2195 : : /* See if we're the non-virtual primary of our inheritance
2196 : : chain. */
2197 : 23945470 : if (!BINFO_VIRTUAL_P (base_binfo))
2198 : : {
2199 : 23741798 : tree parent = BINFO_INHERITANCE_CHAIN (base_binfo);
2200 : 23741798 : tree parent_primary = CLASSTYPE_PRIMARY_BINFO (BINFO_TYPE (parent));
2201 : :
2202 : 23741798 : if (parent_primary
2203 : 23741798 : && SAME_BINFO_TYPE_P (BINFO_TYPE (base_binfo),
2204 : : BINFO_TYPE (parent_primary)))
2205 : : /* We are the primary binfo. */
2206 : 1064983 : BINFO_PRIMARY_P (base_binfo) = 1;
2207 : : }
2208 : : /* Determine if we have a virtual primary base, and mark it so.
2209 : : */
2210 : 25017560 : if (primary && BINFO_VIRTUAL_P (primary))
2211 : : {
2212 : 7107 : tree this_primary = copied_binfo (primary, base_binfo);
2213 : :
2214 : 7107 : if (BINFO_PRIMARY_P (this_primary))
2215 : : /* Someone already claimed this base. */
2216 : 636 : BINFO_LOST_PRIMARY_P (base_binfo) = 1;
2217 : : else
2218 : : {
2219 : 6471 : tree delta;
2220 : :
2221 : 6471 : BINFO_PRIMARY_P (this_primary) = 1;
2222 : 6471 : BINFO_INHERITANCE_CHAIN (this_primary) = base_binfo;
2223 : :
2224 : : /* A virtual binfo might have been copied from within
2225 : : another hierarchy. As we're about to use it as a
2226 : : primary base, make sure the offsets match. */
2227 : 6471 : delta = size_diffop_loc (input_location,
2228 : 6471 : fold_convert (ssizetype,
2229 : : BINFO_OFFSET (base_binfo)),
2230 : 6471 : fold_convert (ssizetype,
2231 : : BINFO_OFFSET (this_primary)));
2232 : :
2233 : 6471 : propagate_binfo_offsets (this_primary, delta);
2234 : : }
2235 : : }
2236 : : }
2237 : :
2238 : : /* First look for a dynamic direct non-virtual base. */
2239 : 58401731 : for (i = 0; BINFO_BASE_ITERATE (type_binfo, i, base_binfo); i++)
2240 : : {
2241 : 20534727 : tree basetype = BINFO_TYPE (base_binfo);
2242 : :
2243 : 20534727 : if (TYPE_CONTAINS_VPTR_P (basetype) && !BINFO_VIRTUAL_P (base_binfo))
2244 : : {
2245 : 1440779 : primary = base_binfo;
2246 : 1440779 : goto found;
2247 : : }
2248 : : }
2249 : :
2250 : : /* A "nearly-empty" virtual base class can be the primary base
2251 : : class, if no non-virtual polymorphic base can be found. Look for
2252 : : a nearly-empty virtual dynamic base that is not already a primary
2253 : : base of something in the hierarchy. If there is no such base,
2254 : : just pick the first nearly-empty virtual base. */
2255 : :
2256 : 58568728 : for (base_binfo = TREE_CHAIN (type_binfo); base_binfo;
2257 : 20701724 : base_binfo = TREE_CHAIN (base_binfo))
2258 : 20703279 : if (BINFO_VIRTUAL_P (base_binfo)
2259 : 20703279 : && CLASSTYPE_NEARLY_EMPTY_P (BINFO_TYPE (base_binfo)))
2260 : : {
2261 : 1684 : if (!BINFO_PRIMARY_P (base_binfo))
2262 : : {
2263 : : /* Found one that is not primary. */
2264 : 1555 : primary = base_binfo;
2265 : 1555 : goto found;
2266 : : }
2267 : 129 : else if (!primary)
2268 : : /* Remember the first candidate. */
2269 : 20701724 : primary = base_binfo;
2270 : : }
2271 : :
2272 : 37865449 : found:
2273 : : /* If we've got a primary base, use it. */
2274 : 39307783 : if (primary)
2275 : : {
2276 : 1442415 : tree basetype = BINFO_TYPE (primary);
2277 : :
2278 : 1442415 : CLASSTYPE_PRIMARY_BINFO (t) = primary;
2279 : 1442415 : if (BINFO_PRIMARY_P (primary))
2280 : : /* We are stealing a primary base. */
2281 : 81 : BINFO_LOST_PRIMARY_P (BINFO_INHERITANCE_CHAIN (primary)) = 1;
2282 : 1442415 : BINFO_PRIMARY_P (primary) = 1;
2283 : 1442415 : if (BINFO_VIRTUAL_P (primary))
2284 : : {
2285 : 1636 : tree delta;
2286 : :
2287 : 1636 : BINFO_INHERITANCE_CHAIN (primary) = type_binfo;
2288 : : /* A virtual binfo might have been copied from within
2289 : : another hierarchy. As we're about to use it as a primary
2290 : : base, make sure the offsets match. */
2291 : 1636 : delta = size_diffop_loc (input_location, ssize_int (0),
2292 : 1636 : fold_convert (ssizetype, BINFO_OFFSET (primary)));
2293 : :
2294 : 1636 : propagate_binfo_offsets (primary, delta);
2295 : : }
2296 : :
2297 : 1442415 : primary = TYPE_BINFO (basetype);
2298 : :
2299 : 1442415 : TYPE_VFIELD (t) = TYPE_VFIELD (basetype);
2300 : 1442415 : BINFO_VTABLE (type_binfo) = BINFO_VTABLE (primary);
2301 : 1442415 : BINFO_VIRTUALS (type_binfo) = BINFO_VIRTUALS (primary);
2302 : : }
2303 : 39307783 : }
2304 : :
2305 : : /* Update the variant types of T. */
2306 : :
2307 : : void
2308 : 129350074 : fixup_type_variants (tree type)
2309 : : {
2310 : 129350074 : if (!type)
2311 : : return;
2312 : :
2313 : 129350074 : for (tree variant = TYPE_NEXT_VARIANT (type);
2314 : 257790291 : variant;
2315 : 128440217 : variant = TYPE_NEXT_VARIANT (variant))
2316 : : {
2317 : : /* These fields are in the _TYPE part of the node, not in
2318 : : the TYPE_LANG_SPECIFIC component, so they are not shared. */
2319 : 128440217 : TYPE_HAS_USER_CONSTRUCTOR (variant) = TYPE_HAS_USER_CONSTRUCTOR (type);
2320 : 128440217 : TYPE_NEEDS_CONSTRUCTING (variant) = TYPE_NEEDS_CONSTRUCTING (type);
2321 : 256880434 : TYPE_HAS_NONTRIVIAL_DESTRUCTOR (variant)
2322 : 128440217 : = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type);
2323 : :
2324 : 128440217 : TYPE_POLYMORPHIC_P (variant) = TYPE_POLYMORPHIC_P (type);
2325 : 128440217 : CLASSTYPE_FINAL (variant) = CLASSTYPE_FINAL (type);
2326 : :
2327 : 128440217 : TYPE_BINFO (variant) = TYPE_BINFO (type);
2328 : :
2329 : : /* Copy whatever these are holding today. */
2330 : 128440217 : TYPE_VFIELD (variant) = TYPE_VFIELD (type);
2331 : 128440217 : TYPE_FIELDS (variant) = TYPE_FIELDS (type);
2332 : :
2333 : 128440217 : TYPE_SIZE (variant) = TYPE_SIZE (type);
2334 : 128440217 : TYPE_SIZE_UNIT (variant) = TYPE_SIZE_UNIT (type);
2335 : :
2336 : 128440217 : if (!TYPE_USER_ALIGN (variant)
2337 : 2423202 : || TYPE_NAME (variant) == TYPE_NAME (type)
2338 : 129629551 : || TYPE_ALIGN_RAW (variant) < TYPE_ALIGN_RAW (type))
2339 : : {
2340 : 127250883 : TYPE_ALIGN_RAW (variant) = TYPE_ALIGN_RAW (type);
2341 : 127250883 : TYPE_USER_ALIGN (variant) = TYPE_USER_ALIGN (type);
2342 : : }
2343 : :
2344 : 128440217 : TYPE_PRECISION (variant) = TYPE_PRECISION (type);
2345 : 128440217 : TYPE_MODE_RAW (variant) = TYPE_MODE_RAW (type);
2346 : 128440217 : TYPE_EMPTY_P (variant) = TYPE_EMPTY_P (type);
2347 : 128440217 : TREE_ADDRESSABLE (variant) = TREE_ADDRESSABLE (type);
2348 : : }
2349 : : }
2350 : :
2351 : : /* KLASS is a class that we're applying may_alias to after the body is
2352 : : parsed. Fixup any POINTER_TO and REFERENCE_TO types. The
2353 : : canonical type(s) will be implicitly updated. */
2354 : :
2355 : : static void
2356 : 40148 : fixup_may_alias (tree klass)
2357 : : {
2358 : 40148 : tree t, v;
2359 : :
2360 : 59859 : for (t = TYPE_POINTER_TO (klass); t; t = TYPE_NEXT_PTR_TO (t))
2361 : 59118 : for (v = TYPE_MAIN_VARIANT (t); v; v = TYPE_NEXT_VARIANT (v))
2362 : 39407 : TYPE_REF_CAN_ALIAS_ALL (v) = true;
2363 : 40169 : for (t = TYPE_REFERENCE_TO (klass); t; t = TYPE_NEXT_REF_TO (t))
2364 : 42 : for (v = TYPE_MAIN_VARIANT (t); v; v = TYPE_NEXT_VARIANT (v))
2365 : 21 : TYPE_REF_CAN_ALIAS_ALL (v) = true;
2366 : 40148 : }
2367 : :
2368 : : /* Early variant fixups: we apply attributes at the beginning of the class
2369 : : definition, and we need to fix up any variants that have already been
2370 : : made via elaborated-type-specifier so that check_qualified_type works. */
2371 : :
2372 : : void
2373 : 80943661 : fixup_attribute_variants (tree t)
2374 : : {
2375 : 80943661 : tree variants;
2376 : :
2377 : 80943661 : if (!t)
2378 : : return;
2379 : :
2380 : 80943661 : tree attrs = TYPE_ATTRIBUTES (t);
2381 : 80943661 : unsigned align = TYPE_ALIGN (t);
2382 : 80943661 : bool user_align = TYPE_USER_ALIGN (t);
2383 : 80943661 : bool may_alias = lookup_attribute ("may_alias", attrs);
2384 : 80943661 : bool packed = TYPE_PACKED (t);
2385 : :
2386 : 80943661 : if (may_alias)
2387 : 20185 : fixup_may_alias (t);
2388 : :
2389 : 80943661 : for (variants = TYPE_NEXT_VARIANT (t);
2390 : 121524676 : variants;
2391 : 40581015 : variants = TYPE_NEXT_VARIANT (variants))
2392 : : {
2393 : : /* These are the two fields that check_qualified_type looks at and
2394 : : are affected by attributes. */
2395 : 40581015 : TYPE_ATTRIBUTES (variants) = attrs;
2396 : 40581015 : unsigned valign = align;
2397 : 40581015 : if (TYPE_USER_ALIGN (variants))
2398 : 18075 : valign = MAX (valign, TYPE_ALIGN (variants));
2399 : : else
2400 : 40562940 : TYPE_USER_ALIGN (variants) = user_align;
2401 : 40581015 : SET_TYPE_ALIGN (variants, valign);
2402 : 40581015 : TYPE_PACKED (variants) = packed;
2403 : 40581015 : if (may_alias)
2404 : 19963 : fixup_may_alias (variants);
2405 : : }
2406 : : }
2407 : :
2408 : : /* Set memoizing fields and bits of T (and its variants) for later
2409 : : use. */
2410 : :
2411 : : static void
2412 : 39307783 : finish_struct_bits (tree t)
2413 : : {
2414 : : /* If this type has a copy constructor or a destructor, force its
2415 : : mode to be BLKmode, and force its TREE_ADDRESSABLE bit to be
2416 : : nonzero. This will cause it to be passed by invisible reference
2417 : : and prevent it from being returned in a register. */
2418 : 39307783 : if (type_has_nontrivial_copy_init (t)
2419 : 39307783 : || TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t))
2420 : : {
2421 : 4637764 : SET_DECL_MODE (TYPE_MAIN_DECL (t), BLKmode);
2422 : 4637764 : SET_TYPE_MODE (t, BLKmode);
2423 : 4637764 : TREE_ADDRESSABLE (t) = 1;
2424 : : }
2425 : :
2426 : 39307783 : if (BINFO_N_BASE_BINFOS (TYPE_BINFO (t)) && TYPE_POLYMORPHIC_P (t))
2427 : : /* For a class w/o baseclasses, 'finish_struct' has set
2428 : : CLASSTYPE_PURE_VIRTUALS correctly (by definition).
2429 : : Similarly for a class whose base classes do not have vtables.
2430 : : When neither of these is true, we might have removed abstract
2431 : : virtuals (by providing a definition), added some (by declaring
2432 : : new ones), or redeclared ones from a base class. We need to
2433 : : recalculate what's really an abstract virtual at this point (by
2434 : : looking in the vtables). */
2435 : 1515562 : get_pure_virtuals (t);
2436 : :
2437 : : /* Fix up variants (if any). */
2438 : 39307783 : fixup_type_variants (t);
2439 : 39307783 : }
2440 : :
2441 : : /* Issue warnings about T having private constructors, but no friends,
2442 : : and so forth.
2443 : :
2444 : : HAS_NONPRIVATE_METHOD is nonzero if T has any non-private methods or
2445 : : static members. HAS_NONPRIVATE_STATIC_FN is nonzero if T has any
2446 : : non-private static member functions. */
2447 : :
2448 : : static void
2449 : 25735654 : maybe_warn_about_overly_private_class (tree t)
2450 : : {
2451 : 25735654 : int has_member_fn = 0;
2452 : 25735654 : int has_nonprivate_method = 0;
2453 : 25735654 : bool nonprivate_ctor = false;
2454 : :
2455 : 25735654 : if (!warn_ctor_dtor_privacy
2456 : : /* If the class has friends, those entities might create and
2457 : : access instances, so we should not warn. */
2458 : 48 : || (CLASSTYPE_FRIEND_CLASSES (t)
2459 : 48 : || DECL_FRIENDLIST (TYPE_MAIN_DECL (t)))
2460 : : /* We will have warned when the template was declared; there's
2461 : : no need to warn on every instantiation. */
2462 : 25735702 : || CLASSTYPE_TEMPLATE_INSTANTIATION (t))
2463 : : /* There's no reason to even consider warning about this
2464 : : class. */
2465 : : return;
2466 : :
2467 : : /* We only issue one warning, if more than one applies, because
2468 : : otherwise, on code like:
2469 : :
2470 : : class A {
2471 : : // Oops - forgot `public:'
2472 : : A();
2473 : : A(const A&);
2474 : : ~A();
2475 : : };
2476 : :
2477 : : we warn several times about essentially the same problem. */
2478 : :
2479 : : /* Check to see if all (non-constructor, non-destructor) member
2480 : : functions are private. (Since there are no friends or
2481 : : non-private statics, we can't ever call any of the private member
2482 : : functions.) */
2483 : 216 : for (tree fn = TYPE_FIELDS (t); fn; fn = DECL_CHAIN (fn))
2484 : 168 : if (TREE_CODE (fn) == USING_DECL
2485 : 18 : && DECL_NAME (fn) == ctor_identifier
2486 : 171 : && !TREE_PRIVATE (fn))
2487 : : nonprivate_ctor = true;
2488 : 165 : else if (!DECL_DECLARES_FUNCTION_P (fn))
2489 : : /* Not a function. */;
2490 : 96 : else if (DECL_ARTIFICIAL (fn))
2491 : : /* We're not interested in compiler-generated methods; they don't
2492 : : provide any way to call private members. */;
2493 : 96 : else if (!TREE_PRIVATE (fn))
2494 : : {
2495 : 48 : if (DECL_STATIC_FUNCTION_P (fn))
2496 : : /* A non-private static member function is just like a
2497 : : friend; it can create and invoke private member
2498 : : functions, and be accessed without a class
2499 : : instance. */
2500 : : return;
2501 : :
2502 : : has_nonprivate_method = 1;
2503 : : /* Keep searching for a static member function. */
2504 : : }
2505 : 96 : else if (!DECL_CONSTRUCTOR_P (fn) && !DECL_DESTRUCTOR_P (fn))
2506 : : has_member_fn = 1;
2507 : :
2508 : 48 : if (!has_nonprivate_method && has_member_fn)
2509 : : {
2510 : : /* There are no non-private methods, and there's at least one
2511 : : private member function that isn't a constructor or
2512 : : destructor. (If all the private members are
2513 : : constructors/destructors we want to use the code below that
2514 : : issues error messages specifically referring to
2515 : : constructors/destructors.) */
2516 : 12 : unsigned i;
2517 : 12 : tree binfo = TYPE_BINFO (t);
2518 : :
2519 : 12 : for (i = 0; i != BINFO_N_BASE_BINFOS (binfo); i++)
2520 : 0 : if (BINFO_BASE_ACCESS (binfo, i) != access_private_node)
2521 : : {
2522 : : has_nonprivate_method = 1;
2523 : : break;
2524 : : }
2525 : 12 : if (!has_nonprivate_method)
2526 : : {
2527 : 12 : warning (OPT_Wctor_dtor_privacy,
2528 : : "all member functions in class %qT are private", t);
2529 : 12 : return;
2530 : : }
2531 : : }
2532 : :
2533 : : /* Even if some of the member functions are non-private, the class
2534 : : won't be useful for much if all the constructors or destructors
2535 : : are private: such an object can never be created or destroyed. */
2536 : 36 : if (tree dtor = CLASSTYPE_DESTRUCTOR (t))
2537 : 6 : if (TREE_PRIVATE (dtor))
2538 : : {
2539 : 6 : warning (OPT_Wctor_dtor_privacy,
2540 : : "%q#T only defines a private destructor and has no friends",
2541 : : t);
2542 : 6 : return;
2543 : : }
2544 : :
2545 : : /* Warn about classes that have private constructors and no friends. */
2546 : 30 : if (TYPE_HAS_USER_CONSTRUCTOR (t)
2547 : : /* Implicitly generated constructors are always public. */
2548 : 30 : && !CLASSTYPE_LAZY_DEFAULT_CTOR (t))
2549 : : {
2550 : 21 : tree copy_or_move = NULL_TREE;
2551 : :
2552 : : /* If a non-template class does not define a copy
2553 : : constructor, one is defined for it, enabling it to avoid
2554 : : this warning. For a template class, this does not
2555 : : happen, and so we would normally get a warning on:
2556 : :
2557 : : template <class T> class C { private: C(); };
2558 : :
2559 : : To avoid this asymmetry, we check TYPE_HAS_COPY_CTOR. All
2560 : : complete non-template or fully instantiated classes have this
2561 : : flag set. */
2562 : 21 : if (!TYPE_HAS_COPY_CTOR (t))
2563 : : nonprivate_ctor = true;
2564 : : else
2565 : 51 : for (tree fn : ovl_range (CLASSTYPE_CONSTRUCTORS (t)))
2566 : 27 : if (TREE_PRIVATE (fn))
2567 : 12 : continue;
2568 : 15 : else if (copy_fn_p (fn) || move_fn_p (fn))
2569 : : /* Ideally, we wouldn't count any constructor that takes
2570 : : an argument of the class type as a parameter, because
2571 : : such things cannot be used to construct an instance of
2572 : : the class unless you already have one. */
2573 : : copy_or_move = fn;
2574 : : else
2575 : : {
2576 : : nonprivate_ctor = true;
2577 : : break;
2578 : : }
2579 : :
2580 : 21 : if (!nonprivate_ctor)
2581 : : {
2582 : 15 : auto_diagnostic_group d;
2583 : 15 : bool w = warning (OPT_Wctor_dtor_privacy,
2584 : : "%q#T only defines private constructors and has "
2585 : : "no friends", t);
2586 : 15 : if (w && copy_or_move)
2587 : 6 : inform (DECL_SOURCE_LOCATION (copy_or_move),
2588 : : "%q#D is public, but requires an existing %q#T object",
2589 : : copy_or_move, t);
2590 : 15 : return;
2591 : 15 : }
2592 : : }
2593 : : }
2594 : :
2595 : : /* Make BINFO's vtable have N entries, including RTTI entries,
2596 : : vbase and vcall offsets, etc. Set its type and call the back end
2597 : : to lay it out. */
2598 : :
2599 : : static void
2600 : 1686331 : layout_vtable_decl (tree binfo, int n)
2601 : : {
2602 : 1686331 : tree atype;
2603 : 1686331 : tree vtable;
2604 : :
2605 : 1686331 : atype = build_array_of_n_type (vtable_entry_type, n);
2606 : 1686331 : layout_type (atype);
2607 : :
2608 : : /* We may have to grow the vtable. */
2609 : 1686331 : vtable = get_vtbl_decl_for_binfo (binfo);
2610 : 1686331 : if (!same_type_p (TREE_TYPE (vtable), atype))
2611 : : {
2612 : 578346 : TREE_TYPE (vtable) = atype;
2613 : 578346 : DECL_SIZE (vtable) = DECL_SIZE_UNIT (vtable) = NULL_TREE;
2614 : 578346 : layout_decl (vtable, 0);
2615 : : }
2616 : 1686331 : }
2617 : :
2618 : : /* True iff FNDECL and BASE_FNDECL (both non-static member functions)
2619 : : have the same signature. */
2620 : :
2621 : : int
2622 : 77700231 : same_signature_p (const_tree fndecl, const_tree base_fndecl)
2623 : : {
2624 : : /* One destructor overrides another if they are the same kind of
2625 : : destructor. */
2626 : 119879453 : if (DECL_DESTRUCTOR_P (base_fndecl) && DECL_DESTRUCTOR_P (fndecl)
2627 : 98680783 : && special_function_p (base_fndecl) == special_function_p (fndecl))
2628 : : return 1;
2629 : : /* But a non-destructor never overrides a destructor, nor vice
2630 : : versa, nor do different kinds of destructors override
2631 : : one-another. For example, a complete object destructor does not
2632 : : override a deleting destructor. */
2633 : 189180606 : if (DECL_DESTRUCTOR_P (base_fndecl) || DECL_DESTRUCTOR_P (fndecl))
2634 : : return 0;
2635 : :
2636 : 46799612 : if (DECL_NAME (fndecl) == DECL_NAME (base_fndecl)
2637 : 46799612 : || (DECL_CONV_FN_P (fndecl)
2638 : 111 : && DECL_CONV_FN_P (base_fndecl)
2639 : 90 : && same_type_p (DECL_CONV_FN_TYPE (fndecl),
2640 : : DECL_CONV_FN_TYPE (base_fndecl))))
2641 : : {
2642 : 16413340 : tree fntype = TREE_TYPE (fndecl);
2643 : 16413340 : tree base_fntype = TREE_TYPE (base_fndecl);
2644 : 16413340 : if (type_memfn_quals (fntype) == type_memfn_quals (base_fntype)
2645 : 16412967 : && type_memfn_rqual (fntype) == type_memfn_rqual (base_fntype)
2646 : 32826304 : && compparms (FUNCTION_FIRST_USER_PARMTYPE (fndecl),
2647 : 16412964 : FUNCTION_FIRST_USER_PARMTYPE (base_fndecl)))
2648 : : return 1;
2649 : : }
2650 : : return 0;
2651 : : }
2652 : :
2653 : : /* Returns TRUE if DERIVED is a binfo containing the binfo BASE as a
2654 : : subobject. */
2655 : :
2656 : : static bool
2657 : 242311 : base_derived_from (tree derived, tree base)
2658 : : {
2659 : 242311 : tree probe;
2660 : :
2661 : 242587 : for (probe = base; probe; probe = BINFO_INHERITANCE_CHAIN (probe))
2662 : : {
2663 : 242482 : if (probe == derived)
2664 : : return true;
2665 : 1551 : else if (BINFO_VIRTUAL_P (probe))
2666 : : /* If we meet a virtual base, we can't follow the inheritance
2667 : : any more. See if the complete type of DERIVED contains
2668 : : such a virtual base. */
2669 : 1275 : return (binfo_for_vbase (BINFO_TYPE (probe), BINFO_TYPE (derived))
2670 : 1275 : != NULL_TREE);
2671 : : }
2672 : : return false;
2673 : : }
2674 : :
2675 : 7707567 : struct find_final_overrider_data {
2676 : : /* The function for which we are trying to find a final overrider. */
2677 : : tree fn;
2678 : : /* The base class in which the function was declared. */
2679 : : tree declaring_base;
2680 : : /* The candidate overriders. */
2681 : : tree candidates;
2682 : : /* Path to most derived. */
2683 : : auto_vec<tree> path;
2684 : : };
2685 : :
2686 : : /* Add the overrider along the current path to FFOD->CANDIDATES.
2687 : : Returns true if an overrider was found; false otherwise. */
2688 : :
2689 : : static bool
2690 : 12809686 : dfs_find_final_overrider_1 (tree binfo,
2691 : : find_final_overrider_data *ffod,
2692 : : unsigned depth)
2693 : : {
2694 : 12809686 : tree method;
2695 : :
2696 : : /* If BINFO is not the most derived type, try a more derived class.
2697 : : A definition there will overrider a definition here. */
2698 : 12809686 : if (depth)
2699 : : {
2700 : 4860348 : depth--;
2701 : 4860348 : if (dfs_find_final_overrider_1
2702 : 4860348 : (ffod->path[depth], ffod, depth))
2703 : : return true;
2704 : : }
2705 : :
2706 : 10073131 : method = look_for_overrides_here (BINFO_TYPE (binfo), ffod->fn);
2707 : 10073131 : if (method)
2708 : : {
2709 : 7949338 : tree *candidate = &ffod->candidates;
2710 : :
2711 : : /* Remove any candidates overridden by this new function. */
2712 : 7949860 : while (*candidate)
2713 : : {
2714 : : /* If *CANDIDATE overrides METHOD, then METHOD
2715 : : cannot override anything else on the list. */
2716 : 241789 : if (base_derived_from (TREE_VALUE (*candidate), binfo))
2717 : : return true;
2718 : : /* If METHOD overrides *CANDIDATE, remove *CANDIDATE. */
2719 : 522 : if (base_derived_from (binfo, TREE_VALUE (*candidate)))
2720 : 468 : *candidate = TREE_CHAIN (*candidate);
2721 : : else
2722 : 54 : candidate = &TREE_CHAIN (*candidate);
2723 : : }
2724 : :
2725 : : /* Add the new function. */
2726 : 7708071 : ffod->candidates = tree_cons (method, binfo, ffod->candidates);
2727 : 7708071 : return true;
2728 : : }
2729 : :
2730 : : return false;
2731 : : }
2732 : :
2733 : : /* Called from find_final_overrider via dfs_walk. */
2734 : :
2735 : : static tree
2736 : 29316777 : dfs_find_final_overrider_pre (tree binfo, void *data)
2737 : : {
2738 : 29316777 : find_final_overrider_data *ffod = (find_final_overrider_data *) data;
2739 : :
2740 : 29316777 : if (binfo == ffod->declaring_base)
2741 : 15898676 : dfs_find_final_overrider_1 (binfo, ffod, ffod->path.length ());
2742 : 29316777 : ffod->path.safe_push (binfo);
2743 : :
2744 : 29316777 : return NULL_TREE;
2745 : : }
2746 : :
2747 : : static tree
2748 : 29316777 : dfs_find_final_overrider_post (tree /*binfo*/, void *data)
2749 : : {
2750 : 29316777 : find_final_overrider_data *ffod = (find_final_overrider_data *) data;
2751 : 29316777 : ffod->path.pop ();
2752 : :
2753 : 29316777 : return NULL_TREE;
2754 : : }
2755 : :
2756 : : /* Returns a TREE_LIST whose TREE_PURPOSE is the final overrider for
2757 : : FN and whose TREE_VALUE is the binfo for the base where the
2758 : : overriding occurs. BINFO (in the hierarchy dominated by the binfo
2759 : : DERIVED) is the base object in which FN is declared. */
2760 : :
2761 : : static tree
2762 : 7707567 : find_final_overrider (tree derived, tree binfo, tree fn)
2763 : : {
2764 : 7707567 : find_final_overrider_data ffod;
2765 : :
2766 : : /* Getting this right is a little tricky. This is valid:
2767 : :
2768 : : struct S { virtual void f (); };
2769 : : struct T { virtual void f (); };
2770 : : struct U : public S, public T { };
2771 : :
2772 : : even though calling `f' in `U' is ambiguous. But,
2773 : :
2774 : : struct R { virtual void f(); };
2775 : : struct S : virtual public R { virtual void f (); };
2776 : : struct T : virtual public R { virtual void f (); };
2777 : : struct U : public S, public T { };
2778 : :
2779 : : is not -- there's no way to decide whether to put `S::f' or
2780 : : `T::f' in the vtable for `R'.
2781 : :
2782 : : The solution is to look at all paths to BINFO. If we find
2783 : : different overriders along any two, then there is a problem. */
2784 : 7707567 : if (DECL_THUNK_P (fn))
2785 : 0 : fn = THUNK_TARGET (fn);
2786 : :
2787 : : /* Determine the depth of the hierarchy. */
2788 : 7707567 : ffod.fn = fn;
2789 : 7707567 : ffod.declaring_base = binfo;
2790 : 7707567 : ffod.candidates = NULL_TREE;
2791 : 7707567 : ffod.path.create (30);
2792 : :
2793 : 7707567 : dfs_walk_all (derived, dfs_find_final_overrider_pre,
2794 : : dfs_find_final_overrider_post, &ffod);
2795 : :
2796 : : /* If there was no winner, issue an error message. */
2797 : 7707567 : if (!ffod.candidates || TREE_CHAIN (ffod.candidates))
2798 : 36 : return error_mark_node;
2799 : :
2800 : : return ffod.candidates;
2801 : 7707567 : }
2802 : :
2803 : : /* Return the index of the vcall offset for FN when TYPE is used as a
2804 : : virtual base. */
2805 : :
2806 : : static tree
2807 : 374733 : get_vcall_index (tree fn, tree type)
2808 : : {
2809 : 374733 : vec<tree_pair_s, va_gc> *indices = CLASSTYPE_VCALL_INDICES (type);
2810 : 374733 : tree_pair_p p;
2811 : 374733 : unsigned ix;
2812 : :
2813 : 380686 : FOR_EACH_VEC_SAFE_ELT (indices, ix, p)
2814 : 1138662 : if ((DECL_DESTRUCTOR_P (fn) && DECL_DESTRUCTOR_P (p->purpose))
2815 : 386334 : || same_signature_p (fn, p->purpose))
2816 : 374733 : return p->value;
2817 : :
2818 : : /* There should always be an appropriate index. */
2819 : 0 : gcc_unreachable ();
2820 : : }
2821 : :
2822 : : /* Given a DECL_VINDEX of a virtual function found in BINFO, return the final
2823 : : overrider at that index in the vtable. This should only be used when we
2824 : : know that BINFO is correct for the dynamic type of the object. */
2825 : :
2826 : : tree
2827 : 953 : lookup_vfn_in_binfo (tree idx, tree binfo)
2828 : : {
2829 : 953 : int ix = tree_to_shwi (idx);
2830 : 953 : if (TARGET_VTABLE_USES_DESCRIPTORS)
2831 : : ix /= MAX (TARGET_VTABLE_USES_DESCRIPTORS, 1);
2832 : 1158 : while (BINFO_PRIMARY_P (binfo))
2833 : : /* BINFO_VIRTUALS in a primary base isn't accurate, find the derived
2834 : : class that actually owns the vtable. */
2835 : 205 : binfo = BINFO_INHERITANCE_CHAIN (binfo);
2836 : 953 : tree virtuals = BINFO_VIRTUALS (binfo);
2837 : 953 : return TREE_VALUE (chain_index (ix, virtuals));
2838 : : }
2839 : :
2840 : : /* Update an entry in the vtable for BINFO, which is in the hierarchy
2841 : : dominated by T. FN is the old function; VIRTUALS points to the
2842 : : corresponding position in the new BINFO_VIRTUALS list. IX is the index
2843 : : of that entry in the list. */
2844 : :
2845 : : static void
2846 : 7262415 : update_vtable_entry_for_fn (tree t, tree binfo, tree fn, tree* virtuals,
2847 : : unsigned ix)
2848 : : {
2849 : 7262415 : tree b;
2850 : 7262415 : tree overrider;
2851 : 7262415 : tree delta;
2852 : 7262415 : tree virtual_base;
2853 : 7262415 : tree first_defn;
2854 : 7262415 : tree overrider_fn, overrider_target;
2855 : 7262415 : tree target_fn = DECL_THUNK_P (fn) ? THUNK_TARGET (fn) : fn;
2856 : 7262415 : tree over_return, base_return;
2857 : 7262415 : bool lost = false;
2858 : :
2859 : : /* Find the nearest primary base (possibly binfo itself) which defines
2860 : : this function; this is the class the caller will convert to when
2861 : : calling FN through BINFO. */
2862 : 9326906 : for (b = binfo; ; b = get_primary_binfo (b))
2863 : : {
2864 : 9326906 : gcc_assert (b);
2865 : 9326906 : if (look_for_overrides_here (BINFO_TYPE (b), target_fn))
2866 : : break;
2867 : :
2868 : : /* The nearest definition is from a lost primary. */
2869 : 2064491 : if (BINFO_LOST_PRIMARY_P (b))
2870 : 412 : lost = true;
2871 : : }
2872 : 7262415 : first_defn = b;
2873 : :
2874 : : /* Find the final overrider. */
2875 : 7262415 : overrider = find_final_overrider (TYPE_BINFO (t), b, target_fn);
2876 : 7262415 : if (overrider == error_mark_node)
2877 : : {
2878 : 9 : error ("no unique final overrider for %qD in %qT", target_fn, t);
2879 : 9 : return;
2880 : : }
2881 : 7262406 : overrider_target = overrider_fn = TREE_PURPOSE (overrider);
2882 : :
2883 : : /* Check for adjusting covariant return types. */
2884 : 7262406 : over_return = TREE_TYPE (TREE_TYPE (overrider_target));
2885 : 7262406 : base_return = TREE_TYPE (TREE_TYPE (target_fn));
2886 : :
2887 : 7262406 : if (INDIRECT_TYPE_P (over_return)
2888 : 645232 : && TREE_CODE (over_return) == TREE_CODE (base_return)
2889 : 645223 : && CLASS_TYPE_P (TREE_TYPE (over_return))
2890 : 43476 : && CLASS_TYPE_P (TREE_TYPE (base_return))
2891 : : /* If the overrider is invalid, don't even try. */
2892 : 7305876 : && !DECL_INVALID_OVERRIDER_P (overrider_target))
2893 : : {
2894 : : /* If FN is a covariant thunk, we must figure out the adjustment
2895 : : to the final base FN was converting to. As OVERRIDER_TARGET might
2896 : : also be converting to the return type of FN, we have to
2897 : : combine the two conversions here. */
2898 : 43437 : tree fixed_offset, virtual_offset;
2899 : :
2900 : 43437 : over_return = TREE_TYPE (over_return);
2901 : 43437 : base_return = TREE_TYPE (base_return);
2902 : :
2903 : 43437 : if (DECL_THUNK_P (fn))
2904 : : {
2905 : 60 : gcc_assert (DECL_RESULT_THUNK_P (fn));
2906 : 60 : fixed_offset = ssize_int (THUNK_FIXED_OFFSET (fn));
2907 : 60 : virtual_offset = THUNK_VIRTUAL_OFFSET (fn);
2908 : : }
2909 : : else
2910 : : fixed_offset = virtual_offset = NULL_TREE;
2911 : :
2912 : 60 : if (virtual_offset)
2913 : : /* Find the equivalent binfo within the return type of the
2914 : : overriding function. We will want the vbase offset from
2915 : : there. */
2916 : 54 : virtual_offset = binfo_for_vbase (BINFO_TYPE (virtual_offset),
2917 : : over_return);
2918 : 43383 : else if (!same_type_ignoring_top_level_qualifiers_p
2919 : 43383 : (over_return, base_return))
2920 : : {
2921 : : /* There was no existing virtual thunk (which takes
2922 : : precedence). So find the binfo of the base function's
2923 : : return type within the overriding function's return type.
2924 : : Fortunately we know the covariancy is valid (it
2925 : : has already been checked), so we can just iterate along
2926 : : the binfos, which have been chained in inheritance graph
2927 : : order. Of course it is lame that we have to repeat the
2928 : : search here anyway -- we should really be caching pieces
2929 : : of the vtable and avoiding this repeated work. */
2930 : 379 : tree thunk_binfo = NULL_TREE;
2931 : 379 : tree base_binfo = TYPE_BINFO (base_return);
2932 : :
2933 : : /* Find the base binfo within the overriding function's
2934 : : return type. We will always find a thunk_binfo, except
2935 : : when the covariancy is invalid (which we will have
2936 : : already diagnosed). */
2937 : 379 : if (base_binfo)
2938 : 984 : for (thunk_binfo = TYPE_BINFO (over_return); thunk_binfo;
2939 : 608 : thunk_binfo = TREE_CHAIN (thunk_binfo))
2940 : 984 : if (SAME_BINFO_TYPE_P (BINFO_TYPE (thunk_binfo),
2941 : : BINFO_TYPE (base_binfo)))
2942 : : break;
2943 : 379 : gcc_assert (thunk_binfo || errorcount);
2944 : :
2945 : : /* See if virtual inheritance is involved. */
2946 : 379 : for (virtual_offset = thunk_binfo;
2947 : 671 : virtual_offset;
2948 : 292 : virtual_offset = BINFO_INHERITANCE_CHAIN (virtual_offset))
2949 : 537 : if (BINFO_VIRTUAL_P (virtual_offset))
2950 : : break;
2951 : :
2952 : 379 : if (virtual_offset
2953 : 510 : || (thunk_binfo && !BINFO_OFFSET_ZEROP (thunk_binfo)))
2954 : : {
2955 : 320 : tree offset = fold_convert (ssizetype, BINFO_OFFSET (thunk_binfo));
2956 : :
2957 : 320 : if (virtual_offset)
2958 : : {
2959 : : /* We convert via virtual base. Adjust the fixed
2960 : : offset to be from there. */
2961 : 245 : offset =
2962 : 245 : size_diffop (offset,
2963 : : fold_convert (ssizetype,
2964 : : BINFO_OFFSET (virtual_offset)));
2965 : : }
2966 : 320 : if (fixed_offset)
2967 : : /* There was an existing fixed offset, this must be
2968 : : from the base just converted to, and the base the
2969 : : FN was thunking to. */
2970 : 6 : fixed_offset = size_binop (PLUS_EXPR, fixed_offset, offset);
2971 : : else
2972 : : fixed_offset = offset;
2973 : : }
2974 : : }
2975 : :
2976 : 43437 : if (fixed_offset || virtual_offset)
2977 : : /* Replace the overriding function with a covariant thunk. We
2978 : : will emit the overriding function in its own slot as
2979 : : well. */
2980 : 374 : overrider_fn = make_thunk (overrider_target, /*this_adjusting=*/0,
2981 : : fixed_offset, virtual_offset);
2982 : : }
2983 : : else
2984 : 7218969 : gcc_assert (DECL_INVALID_OVERRIDER_P (overrider_target) ||
2985 : : !DECL_THUNK_P (fn));
2986 : :
2987 : : /* If we need a covariant thunk, then we may need to adjust first_defn.
2988 : : The ABI specifies that the thunks emitted with a function are
2989 : : determined by which bases the function overrides, so we need to be
2990 : : sure that we're using a thunk for some overridden base; even if we
2991 : : know that the necessary this adjustment is zero, there may not be an
2992 : : appropriate zero-this-adjustment thunk for us to use since thunks for
2993 : : overriding virtual bases always use the vcall offset.
2994 : :
2995 : : Furthermore, just choosing any base that overrides this function isn't
2996 : : quite right, as this slot won't be used for calls through a type that
2997 : : puts a covariant thunk here. Calling the function through such a type
2998 : : will use a different slot, and that slot is the one that determines
2999 : : the thunk emitted for that base.
3000 : :
3001 : : So, keep looking until we find the base that we're really overriding
3002 : : in this slot: the nearest primary base that doesn't use a covariant
3003 : : thunk in this slot. */
3004 : 7262406 : if (overrider_target != overrider_fn)
3005 : : {
3006 : 374 : if (BINFO_TYPE (b) == DECL_CONTEXT (overrider_target))
3007 : : /* We already know that the overrider needs a covariant thunk. */
3008 : 132 : b = get_primary_binfo (b);
3009 : 30 : for (; ; b = get_primary_binfo (b))
3010 : : {
3011 : 404 : tree main_binfo = TYPE_BINFO (BINFO_TYPE (b));
3012 : 404 : tree bv = chain_index (ix, BINFO_VIRTUALS (main_binfo));
3013 : 404 : if (!DECL_THUNK_P (TREE_VALUE (bv)))
3014 : : break;
3015 : 30 : if (BINFO_LOST_PRIMARY_P (b))
3016 : 6 : lost = true;
3017 : 30 : }
3018 : : first_defn = b;
3019 : : }
3020 : :
3021 : : /* Assume that we will produce a thunk that convert all the way to
3022 : : the final overrider, and not to an intermediate virtual base. */
3023 : 7262406 : virtual_base = NULL_TREE;
3024 : :
3025 : : /* See if we can convert to an intermediate virtual base first, and then
3026 : : use the vcall offset located there to finish the conversion. */
3027 : 7468287 : for (; b; b = BINFO_INHERITANCE_CHAIN (b))
3028 : : {
3029 : : /* If we find the final overrider, then we can stop
3030 : : walking. */
3031 : 7468287 : if (SAME_BINFO_TYPE_P (BINFO_TYPE (b),
3032 : : BINFO_TYPE (TREE_VALUE (overrider))))
3033 : : break;
3034 : :
3035 : : /* If we find a virtual base, and we haven't yet found the
3036 : : overrider, then there is a virtual base between the
3037 : : declaring base (first_defn) and the final overrider. */
3038 : 580614 : if (BINFO_VIRTUAL_P (b))
3039 : : {
3040 : : virtual_base = b;
3041 : : break;
3042 : : }
3043 : : }
3044 : :
3045 : : /* Compute the constant adjustment to the `this' pointer. The
3046 : : `this' pointer, when this function is called, will point at BINFO
3047 : : (or one of its primary bases, which are at the same offset). */
3048 : 7262406 : if (virtual_base)
3049 : : /* The `this' pointer needs to be adjusted from the declaration to
3050 : : the nearest virtual base. */
3051 : 1124199 : delta = size_diffop_loc (input_location,
3052 : 374733 : fold_convert (ssizetype, BINFO_OFFSET (virtual_base)),
3053 : 374733 : fold_convert (ssizetype, BINFO_OFFSET (first_defn)));
3054 : 6887673 : else if (lost)
3055 : : /* If the nearest definition is in a lost primary, we don't need an
3056 : : entry in our vtable. Except possibly in a constructor vtable,
3057 : : if we happen to get our primary back. In that case, the offset
3058 : : will be zero, as it will be a primary base. */
3059 : 367 : delta = size_zero_node;
3060 : : else
3061 : : /* The `this' pointer needs to be adjusted from pointing to
3062 : : BINFO to pointing at the base where the final overrider
3063 : : appears. */
3064 : 20661918 : delta = size_diffop_loc (input_location,
3065 : 6887306 : fold_convert (ssizetype,
3066 : : BINFO_OFFSET (TREE_VALUE (overrider))),
3067 : 6887306 : fold_convert (ssizetype, BINFO_OFFSET (binfo)));
3068 : :
3069 : 7262406 : modify_vtable_entry (t, binfo, overrider_fn, delta, virtuals);
3070 : :
3071 : 7262406 : if (virtual_base)
3072 : 749466 : BV_VCALL_INDEX (*virtuals)
3073 : 749466 : = get_vcall_index (overrider_target, BINFO_TYPE (virtual_base));
3074 : : else
3075 : 6887673 : BV_VCALL_INDEX (*virtuals) = NULL_TREE;
3076 : :
3077 : 7262406 : BV_LOST_PRIMARY (*virtuals) = lost;
3078 : : }
3079 : :
3080 : : /* Called from modify_all_vtables via dfs_walk. */
3081 : :
3082 : : static tree
3083 : 42660116 : dfs_modify_vtables (tree binfo, void* data)
3084 : : {
3085 : 42660116 : tree t = (tree) data;
3086 : 42660116 : tree virtuals;
3087 : 42660116 : tree old_virtuals;
3088 : 42660116 : unsigned ix;
3089 : :
3090 : 42660116 : if (!TYPE_CONTAINS_VPTR_P (BINFO_TYPE (binfo)))
3091 : : /* A base without a vtable needs no modification, and its bases
3092 : : are uninteresting. */
3093 : : return dfs_skip_bases;
3094 : :
3095 : 4456831 : if (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), t)
3096 : 4456831 : && !CLASSTYPE_HAS_PRIMARY_BASE_P (t))
3097 : : /* Don't do the primary vtable, if it's new. */
3098 : : return NULL_TREE;
3099 : :
3100 : 4212915 : if (BINFO_PRIMARY_P (binfo) && !BINFO_VIRTUAL_P (binfo))
3101 : : /* There's no need to modify the vtable for a non-virtual primary
3102 : : base; we're not going to use that vtable anyhow. We do still
3103 : : need to do this for virtual primary bases, as they could become
3104 : : non-primary in a construction vtable. */
3105 : : return NULL_TREE;
3106 : :
3107 : 1707153 : make_new_vtable (t, binfo);
3108 : :
3109 : : /* Now, go through each of the virtual functions in the virtual
3110 : : function table for BINFO. Find the final overrider, and update
3111 : : the BINFO_VIRTUALS list appropriately. */
3112 : 1707153 : for (ix = 0, virtuals = BINFO_VIRTUALS (binfo),
3113 : 1707153 : old_virtuals = BINFO_VIRTUALS (TYPE_BINFO (BINFO_TYPE (binfo)));
3114 : 8969568 : virtuals;
3115 : 7262415 : ix++, virtuals = TREE_CHAIN (virtuals),
3116 : 7262415 : old_virtuals = TREE_CHAIN (old_virtuals))
3117 : 7262415 : update_vtable_entry_for_fn (t,
3118 : : binfo,
3119 : 7262415 : BV_FN (old_virtuals),
3120 : : &virtuals, ix);
3121 : :
3122 : : return NULL_TREE;
3123 : : }
3124 : :
3125 : : /* Update all of the primary and secondary vtables for T. Create new
3126 : : vtables as required, and initialize their RTTI information. Each
3127 : : of the functions in VIRTUALS is declared in T and may override a
3128 : : virtual function from a base class; find and modify the appropriate
3129 : : entries to point to the overriding functions. Returns a list, in
3130 : : declaration order, of the virtual functions that are declared in T,
3131 : : but do not appear in the primary base class vtable, and which
3132 : : should therefore be appended to the end of the vtable for T. */
3133 : :
3134 : : static tree
3135 : 39307783 : modify_all_vtables (tree t, tree virtuals)
3136 : : {
3137 : 39307783 : tree binfo = TYPE_BINFO (t);
3138 : 39307783 : tree *fnsp;
3139 : :
3140 : : /* Mangle the vtable name before entering dfs_walk (c++/51884). */
3141 : 39307783 : if (TYPE_CONTAINS_VPTR_P (t))
3142 : 1686331 : get_vtable_decl (t, false);
3143 : :
3144 : : /* Update all of the vtables. */
3145 : 39307783 : dfs_walk_once (binfo, dfs_modify_vtables, NULL, t);
3146 : :
3147 : : /* Add virtual functions not already in our primary vtable. These
3148 : : will be both those introduced by this class, and those overridden
3149 : : from secondary bases. It does not include virtuals merely
3150 : : inherited from secondary bases. */
3151 : 47052331 : for (fnsp = &virtuals; *fnsp; )
3152 : : {
3153 : 7744548 : tree fn = TREE_VALUE (*fnsp);
3154 : :
3155 : 7744548 : if (!value_member (fn, BINFO_VIRTUALS (binfo))
3156 : 12536656 : || DECL_VINDEX (fn) == error_mark_node)
3157 : : {
3158 : : /* We don't need to adjust the `this' pointer when
3159 : : calling this function. */
3160 : 2952440 : BV_DELTA (*fnsp) = integer_zero_node;
3161 : 2952440 : BV_VCALL_INDEX (*fnsp) = NULL_TREE;
3162 : :
3163 : : /* This is a function not already in our vtable. Keep it. */
3164 : 2952440 : fnsp = &TREE_CHAIN (*fnsp);
3165 : : }
3166 : : else
3167 : : /* We've already got an entry for this function. Skip it. */
3168 : 4792108 : *fnsp = TREE_CHAIN (*fnsp);
3169 : : }
3170 : :
3171 : 39307783 : return virtuals;
3172 : : }
3173 : :
3174 : : /* Get the base virtual function declarations in T that have the
3175 : : indicated NAME. */
3176 : :
3177 : : static void
3178 : 227253 : get_basefndecls (tree name, tree t, vec<tree> *base_fndecls)
3179 : : {
3180 : 227253 : bool found_decls = false;
3181 : :
3182 : : /* Find virtual functions in T with the indicated NAME. */
3183 : 326924 : for (tree method : ovl_range (get_class_binding (t, name)))
3184 : : {
3185 : 76179 : if (TREE_CODE (method) == FUNCTION_DECL && DECL_VINDEX (method))
3186 : : {
3187 : 20232 : base_fndecls->safe_push (method);
3188 : 20232 : found_decls = true;
3189 : : }
3190 : : }
3191 : :
3192 : 227253 : if (found_decls)
3193 : : return;
3194 : :
3195 : 208243 : int n_baseclasses = BINFO_N_BASE_BINFOS (TYPE_BINFO (t));
3196 : 237579 : for (int i = 0; i < n_baseclasses; i++)
3197 : : {
3198 : 29336 : tree basetype = BINFO_TYPE (BINFO_BASE_BINFO (TYPE_BINFO (t), i));
3199 : 29336 : get_basefndecls (name, basetype, base_fndecls);
3200 : : }
3201 : : }
3202 : :
3203 : : /* If this method overrides a virtual method from a base, then mark
3204 : : this member function as being virtual as well. Do 'final' and
3205 : : 'override' checks too. */
3206 : :
3207 : : void
3208 : 124617103 : check_for_override (tree decl, tree ctype)
3209 : : {
3210 : 124617103 : if (TREE_CODE (decl) == TEMPLATE_DECL)
3211 : : /* In [temp.mem] we have:
3212 : :
3213 : : A specialization of a member function template does not
3214 : : override a virtual function from a base class. */
3215 : : return;
3216 : :
3217 : : /* IDENTIFIER_VIRTUAL_P indicates whether the name has ever been
3218 : : used for a vfunc. That avoids the expensive look_for_overrides
3219 : : call that when we know there's nothing to find. As conversion
3220 : : operators for the same type can have distinct identifiers, we
3221 : : cannot optimize those in that way. */
3222 : 102616032 : if ((IDENTIFIER_VIRTUAL_P (DECL_NAME (decl))
3223 : 90181427 : || DECL_CONV_FN_P (decl))
3224 : 13512065 : && look_for_overrides (ctype, decl)
3225 : : /* Check staticness after we've checked if we 'override'. */
3226 : 106060664 : && !DECL_STATIC_FUNCTION_P (decl))
3227 : : {
3228 : : /* Set DECL_VINDEX to a value that is neither an INTEGER_CST nor
3229 : : the error_mark_node so that we know it is an overriding
3230 : : function. */
3231 : 3444626 : DECL_VINDEX (decl) = decl;
3232 : :
3233 : 3444626 : if (warn_override
3234 : 6 : && !DECL_OVERRIDE_P (decl)
3235 : 5 : && !DECL_FINAL_P (decl)
3236 : 3444630 : && !DECL_DESTRUCTOR_P (decl))
3237 : 3 : warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wsuggest_override,
3238 : : "%qD can be marked override", decl);
3239 : : }
3240 : 99171406 : else if (DECL_OVERRIDE_P (decl))
3241 : 20 : error ("%q+#D marked %<override%>, but does not override", decl);
3242 : :
3243 : 102616032 : if (DECL_VIRTUAL_P (decl))
3244 : : {
3245 : : /* Remember this identifier is virtual name. */
3246 : 6127952 : IDENTIFIER_VIRTUAL_P (DECL_NAME (decl)) = true;
3247 : :
3248 : 6127952 : if (!DECL_VINDEX (decl))
3249 : : /* It's a new vfunc. */
3250 : 2683342 : DECL_VINDEX (decl) = error_mark_node;
3251 : :
3252 : 6127952 : if (DECL_DESTRUCTOR_P (decl))
3253 : 1616568 : TYPE_HAS_NONTRIVIAL_DESTRUCTOR (ctype) = true;
3254 : : }
3255 : 96488080 : else if (DECL_FINAL_P (decl))
3256 : 19 : error ("%q+#D marked %<final%>, but is not virtual", decl);
3257 : : }
3258 : :
3259 : : /* Warn about hidden virtual functions that are not overridden in t.
3260 : : We know that constructors and destructors don't apply. */
3261 : :
3262 : : static void
3263 : 381391 : warn_hidden (tree t)
3264 : : {
3265 : 381391 : if (vec<tree, va_gc> *member_vec = CLASSTYPE_MEMBER_VEC (t))
3266 : 1987188 : for (unsigned ix = member_vec->length (); ix--;)
3267 : : {
3268 : 1853094 : tree fns = (*member_vec)[ix];
3269 : :
3270 : 1853094 : if (!OVL_P (fns))
3271 : 1852938 : continue;
3272 : :
3273 : 1022200 : tree name = OVL_NAME (fns);
3274 : 1022200 : size_t num_fns = 0; /* The number of fndecls in fns. */
3275 : 1022200 : auto_vec<tree, 20> base_fndecls;
3276 : 1022200 : tree base_binfo;
3277 : 1022200 : tree binfo;
3278 : 1022200 : unsigned j;
3279 : 1022200 : size_t num_overriders = 0;
3280 : 1022200 : hash_set<tree> overriden_base_fndecls;
3281 : : /* base_fndecls that are hidden but not overriden. The "value"
3282 : : contains the last fndecl we saw that hides the "key". */
3283 : 1022200 : hash_map<tree, tree> hidden_base_fndecls;
3284 : :
3285 : 1022200 : if (IDENTIFIER_CDTOR_P (name))
3286 : 345787 : continue;
3287 : :
3288 : : /* Iterate through all of the base classes looking for possibly
3289 : : hidden functions. */
3290 : 874330 : for (binfo = TYPE_BINFO (t), j = 0;
3291 : 874330 : BINFO_BASE_ITERATE (binfo, j, base_binfo); j++)
3292 : : {
3293 : 197917 : tree basetype = BINFO_TYPE (base_binfo);
3294 : 197917 : get_basefndecls (name, basetype, &base_fndecls);
3295 : : }
3296 : :
3297 : : /* If there are no functions to hide, continue. */
3298 : 676413 : if (base_fndecls.is_empty ())
3299 : 657409 : continue;
3300 : :
3301 : : /* Find all the base_fndecls that are overridden, as well as those
3302 : : that are hidden, in T. */
3303 : 40304 : for (tree fndecl : ovl_range (fns))
3304 : : {
3305 : 20145 : bool template_p = TREE_CODE (fndecl) == TEMPLATE_DECL;
3306 : 20145 : bool fndecl_overrides_p = false;
3307 : 20145 : fndecl = STRIP_TEMPLATE (fndecl);
3308 : 20145 : if (TREE_CODE (fndecl) != FUNCTION_DECL
3309 : 20145 : || fndecl == conv_op_marker)
3310 : 15 : continue;
3311 : 20130 : num_fns++;
3312 : 42539 : for (size_t k = 0; k < base_fndecls.length (); k++)
3313 : : {
3314 : 22409 : if (!base_fndecls[k] || !DECL_VINDEX (base_fndecls[k]))
3315 : 0 : continue;
3316 : 22409 : if (IDENTIFIER_CONV_OP_P (name)
3317 : 22409 : && !same_type_p (DECL_CONV_FN_TYPE (fndecl),
3318 : : DECL_CONV_FN_TYPE (base_fndecls[k])))
3319 : : /* If base_fndecl[k] and fndecl are conversion operators
3320 : : to different types, they're unrelated. */
3321 : : ;
3322 : 22376 : else if (!template_p /* Template methods don't override. */
3323 : 22376 : && same_signature_p (fndecl, base_fndecls[k]))
3324 : : {
3325 : 20019 : overriden_base_fndecls.add (base_fndecls[k]);
3326 : 20019 : fndecl_overrides_p = true;
3327 : : }
3328 : : else
3329 : : {
3330 : : /* fndecl hides base_fndecls[k]. */
3331 : 2357 : hidden_base_fndecls.put (base_fndecls[k], fndecl);
3332 : : }
3333 : : }
3334 : 20130 : if (fndecl_overrides_p)
3335 : 20013 : ++num_overriders;
3336 : : }
3337 : :
3338 : 19004 : if (warn_overloaded_virtual == 1 && num_overriders == num_fns)
3339 : : /* All the fns override a base virtual. */
3340 : 18848 : continue;
3341 : :
3342 : : /* Now give a warning for all hidden methods. Note that a method that
3343 : : is both in hidden_base_fndecls and overriden_base_fndecls is not
3344 : : hidden. */
3345 : 612 : for (auto hidden_base_fndecl : hidden_base_fndecls)
3346 : : {
3347 : 150 : tree hidden_fndecl = hidden_base_fndecl.first;
3348 : 261 : if (!hidden_fndecl
3349 : 150 : || overriden_base_fndecls.contains (hidden_fndecl))
3350 : 111 : continue;
3351 : 39 : auto_diagnostic_group d;
3352 : 39 : if (warning_at (location_of (hidden_fndecl),
3353 : 39 : OPT_Woverloaded_virtual_,
3354 : : "%qD was hidden", hidden_fndecl))
3355 : : {
3356 : 36 : tree hider = hidden_base_fndecl.second;
3357 : 36 : inform (location_of (hider), " by %qD", hider);
3358 : : }
3359 : 39 : }
3360 : 1022200 : }
3361 : 381391 : }
3362 : :
3363 : : /* Recursive helper for finish_struct_anon. */
3364 : :
3365 : : static void
3366 : 152836 : finish_struct_anon_r (tree field)
3367 : : {
3368 : 769382 : for (tree elt = TYPE_FIELDS (TREE_TYPE (field)); elt; elt = DECL_CHAIN (elt))
3369 : : {
3370 : : /* We're generally only interested in entities the user
3371 : : declared, but we also find nested classes by noticing
3372 : : the TYPE_DECL that we create implicitly. You're
3373 : : allowed to put one anonymous union inside another,
3374 : : though, so we explicitly tolerate that. We use
3375 : : TYPE_UNNAMED_P rather than ANON_AGGR_TYPE_P so that
3376 : : we also allow unnamed types used for defining fields. */
3377 : 616546 : if (DECL_ARTIFICIAL (elt)
3378 : 616546 : && (!DECL_IMPLICIT_TYPEDEF_P (elt)
3379 : 61068 : || TYPE_UNNAMED_P (TREE_TYPE (elt))))
3380 : 173191 : continue;
3381 : :
3382 : 443355 : TREE_PRIVATE (elt) = TREE_PRIVATE (field);
3383 : 443355 : TREE_PROTECTED (elt) = TREE_PROTECTED (field);
3384 : :
3385 : : /* Recurse into the anonymous aggregates to correctly handle
3386 : : access control (c++/24926):
3387 : :
3388 : : class A {
3389 : : union {
3390 : : union {
3391 : : int i;
3392 : : };
3393 : : };
3394 : : };
3395 : :
3396 : : int j=A().i; */
3397 : 443355 : if (DECL_NAME (elt) == NULL_TREE
3398 : 443355 : && ANON_AGGR_TYPE_P (TREE_TYPE (elt)))
3399 : 19613 : finish_struct_anon_r (elt);
3400 : : }
3401 : 152836 : }
3402 : :
3403 : : /* Fix up any anonymous union/struct members of T. */
3404 : :
3405 : : static void
3406 : 39307783 : finish_struct_anon (tree t)
3407 : : {
3408 : 269985150 : for (tree field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
3409 : : {
3410 : 230677367 : if (TREE_STATIC (field))
3411 : 10914464 : continue;
3412 : 219762903 : if (TREE_CODE (field) != FIELD_DECL)
3413 : 203916227 : continue;
3414 : :
3415 : 15846676 : if (DECL_NAME (field) == NULL_TREE
3416 : 15846676 : && ANON_AGGR_TYPE_P (TREE_TYPE (field)))
3417 : 133223 : finish_struct_anon_r (field);
3418 : : }
3419 : 39307783 : }
3420 : :
3421 : : /* Add T to CLASSTYPE_DECL_LIST of current_class_type which
3422 : : will be used later during class template instantiation.
3423 : : When FRIEND_P is zero, T can be a static member data (VAR_DECL),
3424 : : a non-static member data (FIELD_DECL), a member function
3425 : : (FUNCTION_DECL), a nested type (RECORD_TYPE, ENUM_TYPE),
3426 : : a typedef (TYPE_DECL) or a member class template (TEMPLATE_DECL)
3427 : : When FRIEND_P is nonzero, T is either a friend class
3428 : : (RECORD_TYPE, TEMPLATE_DECL) or a friend function
3429 : : (FUNCTION_DECL, TEMPLATE_DECL). */
3430 : :
3431 : : void
3432 : 381703753 : maybe_add_class_template_decl_list (tree type, tree t, int friend_p)
3433 : : {
3434 : 381703753 : if (CLASSTYPE_TEMPLATE_INFO (type)
3435 : 381703753 : && TREE_CODE (t) != CONST_DECL)
3436 : : {
3437 : 336924915 : tree purpose = friend_p ? NULL_TREE : type;
3438 : :
3439 : 336924915 : CLASSTYPE_DECL_LIST (type)
3440 : 673849830 : = tree_cons (purpose, t, CLASSTYPE_DECL_LIST (type));
3441 : : }
3442 : 381703753 : }
3443 : :
3444 : : /* This function is called from declare_virt_assop_and_dtor via
3445 : : dfs_walk_all.
3446 : :
3447 : : DATA is a type that direcly or indirectly inherits the base
3448 : : represented by BINFO. If BINFO contains a virtual assignment [copy
3449 : : assignment or move assigment] operator or a virtual constructor,
3450 : : declare that function in DATA if it hasn't been already declared. */
3451 : :
3452 : : static tree
3453 : 3465147 : dfs_declare_virt_assop_and_dtor (tree binfo, void *data)
3454 : : {
3455 : 3465147 : tree bv, fn, t = (tree)data;
3456 : 3465147 : tree opname = assign_op_identifier;
3457 : :
3458 : 3465147 : gcc_assert (t && CLASS_TYPE_P (t));
3459 : 3465147 : gcc_assert (binfo && TREE_CODE (binfo) == TREE_BINFO);
3460 : :
3461 : 3465147 : if (!TYPE_CONTAINS_VPTR_P (BINFO_TYPE (binfo)))
3462 : : /* A base without a vtable needs no modification, and its bases
3463 : : are uninteresting. */
3464 : : return dfs_skip_bases;
3465 : :
3466 : 2917506 : if (BINFO_PRIMARY_P (binfo))
3467 : : /* If this is a primary base, then we have already looked at the
3468 : : virtual functions of its vtable. */
3469 : : return NULL_TREE;
3470 : :
3471 : 11634385 : for (bv = BINFO_VIRTUALS (binfo); bv; bv = TREE_CHAIN (bv))
3472 : : {
3473 : 8716879 : fn = BV_FN (bv);
3474 : :
3475 : 8716879 : if (DECL_NAME (fn) == opname)
3476 : : {
3477 : 21 : if (CLASSTYPE_LAZY_COPY_ASSIGN (t))
3478 : 21 : lazily_declare_fn (sfk_copy_assignment, t);
3479 : 21 : if (CLASSTYPE_LAZY_MOVE_ASSIGN (t))
3480 : 14 : lazily_declare_fn (sfk_move_assignment, t);
3481 : : }
3482 : 8716858 : else if (DECL_DESTRUCTOR_P (fn)
3483 : 8716858 : && CLASSTYPE_LAZY_DESTRUCTOR (t))
3484 : 172346 : lazily_declare_fn (sfk_destructor, t);
3485 : : }
3486 : :
3487 : : return NULL_TREE;
3488 : : }
3489 : :
3490 : : /* If the class type T has a direct or indirect base that contains a
3491 : : virtual assignment operator or a virtual destructor, declare that
3492 : : function in T if it hasn't been already declared. */
3493 : :
3494 : : static void
3495 : 39307783 : declare_virt_assop_and_dtor (tree t)
3496 : : {
3497 : 39307783 : if (!(TYPE_POLYMORPHIC_P (t)
3498 : 1683862 : && (CLASSTYPE_LAZY_COPY_ASSIGN (t)
3499 : 629410 : || CLASSTYPE_LAZY_MOVE_ASSIGN (t)
3500 : 629410 : || CLASSTYPE_LAZY_DESTRUCTOR (t))))
3501 : : return;
3502 : :
3503 : 1110364 : dfs_walk_all (TYPE_BINFO (t),
3504 : : dfs_declare_virt_assop_and_dtor,
3505 : : NULL, t);
3506 : : }
3507 : :
3508 : : /* Declare the inheriting constructor for class T inherited from base
3509 : : constructor CTOR with the parameter array PARMS of size NPARMS. */
3510 : :
3511 : : static void
3512 : 90 : one_inheriting_sig (tree t, tree ctor, tree *parms, int nparms)
3513 : : {
3514 : 90 : gcc_assert (TYPE_MAIN_VARIANT (t) == t);
3515 : :
3516 : : /* We don't declare an inheriting ctor that would be a default,
3517 : : copy or move ctor for derived or base. */
3518 : 90 : if (nparms == 0)
3519 : : return;
3520 : 90 : if (nparms == 1
3521 : 87 : && TYPE_REF_P (parms[0]))
3522 : : {
3523 : 54 : tree parm = TYPE_MAIN_VARIANT (TREE_TYPE (parms[0]));
3524 : 54 : if (parm == t || parm == DECL_CONTEXT (ctor))
3525 : : return;
3526 : : }
3527 : :
3528 : 36 : tree parmlist = void_list_node;
3529 : 75 : for (int i = nparms - 1; i >= 0; i--)
3530 : 39 : parmlist = tree_cons (NULL_TREE, parms[i], parmlist);
3531 : 36 : tree fn = implicitly_declare_fn (sfk_inheriting_constructor,
3532 : : t, false, ctor, parmlist);
3533 : :
3534 : 36 : if (add_method (t, fn, false))
3535 : : {
3536 : 27 : DECL_CHAIN (fn) = TYPE_FIELDS (t);
3537 : 27 : TYPE_FIELDS (t) = fn;
3538 : : }
3539 : : }
3540 : :
3541 : : /* Declare all the inheriting constructors for class T inherited from base
3542 : : constructor CTOR. */
3543 : :
3544 : : static void
3545 : 299172 : one_inherited_ctor (tree ctor, tree t, tree using_decl)
3546 : : {
3547 : 299172 : tree parms = FUNCTION_FIRST_USER_PARMTYPE (ctor);
3548 : :
3549 : 299172 : if (flag_new_inheriting_ctors)
3550 : : {
3551 : 299082 : ctor = implicitly_declare_fn (sfk_inheriting_constructor,
3552 : : t, /*const*/false, ctor, parms);
3553 : 299082 : add_method (t, ctor, using_decl != NULL_TREE);
3554 : 299082 : return;
3555 : : }
3556 : :
3557 : 90 : tree *new_parms = XALLOCAVEC (tree, list_length (parms));
3558 : 90 : int i = 0;
3559 : 183 : for (; parms && parms != void_list_node; parms = TREE_CHAIN (parms))
3560 : : {
3561 : 93 : if (TREE_PURPOSE (parms))
3562 : 0 : one_inheriting_sig (t, ctor, new_parms, i);
3563 : 93 : new_parms[i++] = TREE_VALUE (parms);
3564 : : }
3565 : 90 : one_inheriting_sig (t, ctor, new_parms, i);
3566 : 90 : if (parms == NULL_TREE)
3567 : : {
3568 : 3 : auto_diagnostic_group d;
3569 : 3 : if (warning (OPT_Winherited_variadic_ctor,
3570 : : "the ellipsis in %qD is not inherited", ctor))
3571 : 3 : inform (DECL_SOURCE_LOCATION (ctor), "%qD declared here", ctor);
3572 : 3 : }
3573 : : }
3574 : :
3575 : : /* Implicitly declare T(). */
3576 : :
3577 : : static void
3578 : 33304313 : add_implicit_default_ctor (tree t)
3579 : : {
3580 : 33304313 : TYPE_HAS_DEFAULT_CONSTRUCTOR (t) = 1;
3581 : 33304313 : CLASSTYPE_LAZY_DEFAULT_CTOR (t) = 1;
3582 : 33304313 : if (cxx_dialect >= cxx11)
3583 : 33222726 : TYPE_HAS_CONSTEXPR_CTOR (t)
3584 : : /* Don't force the declaration to get a hard answer; if the
3585 : : definition would have made the class non-literal, it will still be
3586 : : non-literal because of the base or member in question, and that
3587 : : gives a better diagnostic. */
3588 : 66445452 : = type_maybe_constexpr_default_constructor (t);
3589 : 33304313 : }
3590 : :
3591 : : /* Create default constructors, assignment operators, and so forth for
3592 : : the type indicated by T, if they are needed. CANT_HAVE_CONST_CTOR,
3593 : : and CANT_HAVE_CONST_ASSIGNMENT are nonzero if, for whatever reason,
3594 : : the class cannot have a default constructor, copy constructor
3595 : : taking a const reference argument, or an assignment operator taking
3596 : : a const reference, respectively. */
3597 : :
3598 : : static void
3599 : 39307783 : add_implicitly_declared_members (tree t, tree* access_decls,
3600 : : int cant_have_const_cctor,
3601 : : int cant_have_const_assignment)
3602 : : {
3603 : : /* Destructor. */
3604 : 39307783 : if (!CLASSTYPE_DESTRUCTOR (t))
3605 : : /* In general, we create destructors lazily. */
3606 : 35679084 : CLASSTYPE_LAZY_DESTRUCTOR (t) = 1;
3607 : :
3608 : 39307783 : bool move_ok = false;
3609 : 39199846 : if (cxx_dialect >= cxx11 && CLASSTYPE_LAZY_DESTRUCTOR (t)
3610 : 35590878 : && !TYPE_HAS_COPY_CTOR (t) && !TYPE_HAS_COPY_ASSIGN (t)
3611 : 73925892 : && !classtype_has_move_assign_or_move_ctor_p (t, false))
3612 : : move_ok = true;
3613 : :
3614 : : /* [class.ctor]
3615 : :
3616 : : If there is no user-declared constructor for a class, a default
3617 : : constructor is implicitly declared. */
3618 : 39307783 : if (! TYPE_HAS_USER_CONSTRUCTOR (t))
3619 : 33258426 : add_implicit_default_ctor (t);
3620 : :
3621 : : /* [class.ctor]
3622 : :
3623 : : If a class definition does not explicitly declare a copy
3624 : : constructor, one is declared implicitly. */
3625 : 39307783 : if (! TYPE_HAS_COPY_CTOR (t))
3626 : : {
3627 : 36194137 : TYPE_HAS_COPY_CTOR (t) = 1;
3628 : 36194137 : TYPE_HAS_CONST_COPY_CTOR (t) = !cant_have_const_cctor;
3629 : 36194137 : CLASSTYPE_LAZY_COPY_CTOR (t) = 1;
3630 : 36194137 : if (move_ok)
3631 : 34374265 : CLASSTYPE_LAZY_MOVE_CTOR (t) = 1;
3632 : : }
3633 : :
3634 : : /* If there is no assignment operator, one will be created if and
3635 : : when it is needed. For now, just record whether or not the type
3636 : : of the parameter to the assignment operator will be a const or
3637 : : non-const reference. */
3638 : 39307783 : if (!TYPE_HAS_COPY_ASSIGN (t))
3639 : : {
3640 : 36324975 : TYPE_HAS_COPY_ASSIGN (t) = 1;
3641 : 36324975 : TYPE_HAS_CONST_COPY_ASSIGN (t) = !cant_have_const_assignment;
3642 : 36324975 : CLASSTYPE_LAZY_COPY_ASSIGN (t) = 1;
3643 : 70326360 : if (move_ok && !LAMBDA_TYPE_P (t))
3644 : 34071333 : CLASSTYPE_LAZY_MOVE_ASSIGN (t) = 1;
3645 : : }
3646 : :
3647 : : /* We can't be lazy about declaring functions that might override
3648 : : a virtual function from a base class. */
3649 : 39307783 : declare_virt_assop_and_dtor (t);
3650 : :
3651 : : /* If the class definition does not explicitly declare an == operator
3652 : : function, but declares a defaulted three-way comparison operator function,
3653 : : an == operator function is declared implicitly. */
3654 : 39307783 : if (!classtype_has_op (t, EQ_EXPR))
3655 : 38344075 : if (tree space = classtype_has_defaulted_op (t, SPACESHIP_EXPR))
3656 : : {
3657 : 246 : tree eq = implicitly_declare_fn (sfk_comparison, t, false, space,
3658 : : NULL_TREE);
3659 : 246 : bool is_friend = DECL_CONTEXT (space) != t;
3660 : 246 : if (is_friend)
3661 : 51 : do_friend (NULL_TREE, DECL_NAME (eq), eq,
3662 : : NO_SPECIAL, true);
3663 : : else
3664 : : {
3665 : 195 : add_method (t, eq, false);
3666 : 195 : DECL_CHAIN (eq) = TYPE_FIELDS (t);
3667 : 195 : TYPE_FIELDS (t) = eq;
3668 : : }
3669 : 246 : maybe_add_class_template_decl_list (t, eq, is_friend);
3670 : : }
3671 : :
3672 : 41415146 : while (*access_decls)
3673 : : {
3674 : 2107363 : tree using_decl = TREE_VALUE (*access_decls);
3675 : 2107363 : tree decl = USING_DECL_DECLS (using_decl);
3676 : 2107363 : if (DECL_NAME (using_decl) == ctor_identifier)
3677 : : {
3678 : : /* declare, then remove the decl */
3679 : 65066 : tree ctor_list = decl;
3680 : 65066 : location_t loc = input_location;
3681 : 65066 : input_location = DECL_SOURCE_LOCATION (using_decl);
3682 : 599480 : for (tree fn : ovl_range (ctor_list))
3683 : : {
3684 : 299172 : if (!TYPE_HAS_DEFAULT_CONSTRUCTOR (t) && default_ctor_p (fn))
3685 : : /* CWG2799: Inheriting a default constructor gives us a default
3686 : : constructor, not just an inherited constructor. */
3687 : 45887 : add_implicit_default_ctor (t);
3688 : 299172 : one_inherited_ctor (fn, t, using_decl);
3689 : : }
3690 : 65066 : *access_decls = TREE_CHAIN (*access_decls);
3691 : 65066 : input_location = loc;
3692 : : }
3693 : : else
3694 : 2042297 : access_decls = &TREE_CHAIN (*access_decls);
3695 : : }
3696 : 39307783 : }
3697 : :
3698 : : /* Cache of enum_min_precision values. */
3699 : : static GTY((deletable)) hash_map<tree, int> *enum_to_min_precision;
3700 : :
3701 : : /* Return the minimum precision of a bit-field needed to store all
3702 : : enumerators of ENUMERAL_TYPE TYPE. */
3703 : :
3704 : : static int
3705 : 34919 : enum_min_precision (tree type)
3706 : : {
3707 : 34919 : type = TYPE_MAIN_VARIANT (type);
3708 : : /* For unscoped enums without fixed underlying type and without mode
3709 : : attribute we can just use precision of the underlying type. */
3710 : 34919 : if (UNSCOPED_ENUM_P (type)
3711 : 19123 : && !ENUM_FIXED_UNDERLYING_TYPE_P (type)
3712 : 43711 : && !lookup_attribute ("mode", TYPE_ATTRIBUTES (type)))
3713 : 8780 : return TYPE_PRECISION (ENUM_UNDERLYING_TYPE (type));
3714 : :
3715 : 26139 : if (enum_to_min_precision == NULL)
3716 : 2659 : enum_to_min_precision = hash_map<tree, int>::create_ggc (37);
3717 : :
3718 : 26139 : bool existed;
3719 : 26139 : int &prec = enum_to_min_precision->get_or_insert (type, &existed);
3720 : 26139 : if (existed)
3721 : 15737 : return prec;
3722 : :
3723 : 10402 : tree minnode, maxnode;
3724 : 10402 : if (TYPE_VALUES (type))
3725 : : {
3726 : : minnode = maxnode = NULL_TREE;
3727 : 80245 : for (tree values = TYPE_VALUES (type);
3728 : 90644 : values; values = TREE_CHAIN (values))
3729 : : {
3730 : 80245 : tree decl = TREE_VALUE (values);
3731 : 80245 : tree value = DECL_INITIAL (decl);
3732 : 80245 : if (value == error_mark_node)
3733 : 0 : value = integer_zero_node;
3734 : 80245 : if (!minnode)
3735 : : minnode = maxnode = value;
3736 : 69846 : else if (tree_int_cst_lt (maxnode, value))
3737 : : maxnode = value;
3738 : 25760 : else if (tree_int_cst_lt (value, minnode))
3739 : 0 : minnode = value;
3740 : : }
3741 : : }
3742 : : else
3743 : 3 : minnode = maxnode = integer_zero_node;
3744 : :
3745 : 10402 : signop sgn = tree_int_cst_sgn (minnode) >= 0 ? UNSIGNED : SIGNED;
3746 : 10402 : int lowprec = tree_int_cst_min_precision (minnode, sgn);
3747 : 10402 : int highprec = tree_int_cst_min_precision (maxnode, sgn);
3748 : 10402 : prec = MAX (lowprec, highprec);
3749 : 10402 : return prec;
3750 : : }
3751 : :
3752 : : /* FIELD is a bit-field. We are finishing the processing for its
3753 : : enclosing type. Issue any appropriate messages and set appropriate
3754 : : flags. Returns false if an error has been diagnosed. */
3755 : :
3756 : : static bool
3757 : 426361 : check_bitfield_decl (tree field)
3758 : : {
3759 : 426361 : tree type = TREE_TYPE (field);
3760 : 426361 : tree w;
3761 : :
3762 : : /* Extract the declared width of the bitfield, which has been
3763 : : temporarily stashed in DECL_BIT_FIELD_REPRESENTATIVE by grokbitfield. */
3764 : 426361 : w = DECL_BIT_FIELD_REPRESENTATIVE (field);
3765 : 426361 : gcc_assert (w != NULL_TREE);
3766 : : /* Remove the bit-field width indicator so that the rest of the
3767 : : compiler does not treat that value as a qualifier. */
3768 : 426361 : DECL_BIT_FIELD_REPRESENTATIVE (field) = NULL_TREE;
3769 : :
3770 : : /* Detect invalid bit-field type. */
3771 : 426361 : if (!INTEGRAL_OR_ENUMERATION_TYPE_P (type))
3772 : : {
3773 : 3 : error_at (DECL_SOURCE_LOCATION (field),
3774 : : "bit-field %q#D with non-integral type %qT", field, type);
3775 : 3 : w = error_mark_node;
3776 : : }
3777 : : else
3778 : : {
3779 : 426358 : location_t loc = input_location;
3780 : : /* Avoid the non_lvalue wrapper added by fold for PLUS_EXPRs. */
3781 : 426358 : STRIP_NOPS (w);
3782 : :
3783 : : /* detect invalid field size. */
3784 : 426358 : input_location = DECL_SOURCE_LOCATION (field);
3785 : 426358 : w = cxx_constant_value (w);
3786 : 426358 : input_location = loc;
3787 : :
3788 : 426358 : if (TREE_CODE (w) != INTEGER_CST)
3789 : : {
3790 : 15 : error ("bit-field %q+D width not an integer constant", field);
3791 : 15 : w = error_mark_node;
3792 : : }
3793 : 426343 : else if (tree_int_cst_sgn (w) < 0)
3794 : : {
3795 : 0 : error ("negative width in bit-field %q+D", field);
3796 : 0 : w = error_mark_node;
3797 : : }
3798 : 426343 : else if (integer_zerop (w) && DECL_NAME (field) != 0)
3799 : : {
3800 : 15 : error ("zero width for bit-field %q+D", field);
3801 : 15 : w = error_mark_node;
3802 : : }
3803 : 426328 : else if ((TREE_CODE (type) != ENUMERAL_TYPE
3804 : 426328 : && TREE_CODE (type) != BOOLEAN_TYPE
3805 : 389956 : && compare_tree_int (w, TYPE_PRECISION (type)) > 0)
3806 : 426328 : || ((TREE_CODE (type) == ENUMERAL_TYPE
3807 : 425894 : || TREE_CODE (type) == BOOLEAN_TYPE)
3808 : 36372 : && tree_int_cst_lt (TYPE_SIZE (type), w)))
3809 : 774 : warning_at (DECL_SOURCE_LOCATION (field), 0,
3810 : : "width of %qD exceeds its type", field);
3811 : 425554 : else if (TREE_CODE (type) == ENUMERAL_TYPE)
3812 : : {
3813 : 34919 : int prec = enum_min_precision (type);
3814 : 34919 : if (compare_tree_int (w, prec) < 0)
3815 : 48 : warning_at (DECL_SOURCE_LOCATION (field), 0,
3816 : : "%qD is too small to hold all values of %q#T",
3817 : : field, type);
3818 : : }
3819 : : }
3820 : :
3821 : 426361 : if (w != error_mark_node)
3822 : : {
3823 : 426328 : DECL_SIZE (field) = fold_convert (bitsizetype, w);
3824 : 426328 : DECL_BIT_FIELD (field) = 1;
3825 : 426328 : return true;
3826 : : }
3827 : : else
3828 : : {
3829 : : /* Non-bit-fields are aligned for their type. */
3830 : 33 : DECL_BIT_FIELD (field) = 0;
3831 : 33 : CLEAR_DECL_C_BIT_FIELD (field);
3832 : 33 : return false;
3833 : : }
3834 : : }
3835 : :
3836 : : /* FIELD is a non bit-field. We are finishing the processing for its
3837 : : enclosing type T. Issue any appropriate messages and set appropriate
3838 : : flags. */
3839 : :
3840 : : static bool
3841 : 16288146 : check_field_decl (tree field,
3842 : : tree t,
3843 : : int* cant_have_const_ctor,
3844 : : int* no_const_asn_ref)
3845 : : {
3846 : 16288146 : tree type = strip_array_types (TREE_TYPE (field));
3847 : 16288146 : bool any_default_members = false;
3848 : :
3849 : : /* In C++98 an anonymous union cannot contain any fields which would change
3850 : : the settings of CANT_HAVE_CONST_CTOR and friends. */
3851 : 16288146 : if (ANON_UNION_TYPE_P (type) && cxx_dialect < cxx11)
3852 : : ;
3853 : : /* And, we don't set TYPE_HAS_CONST_COPY_CTOR, etc., for anonymous
3854 : : structs. So, we recurse through their fields here. */
3855 : 16287301 : else if (ANON_AGGR_TYPE_P (type))
3856 : : {
3857 : 765905 : for (tree fields = TYPE_FIELDS (type); fields;
3858 : 613931 : fields = DECL_CHAIN (fields))
3859 : 613931 : if (TREE_CODE (fields) == FIELD_DECL)
3860 : 441631 : any_default_members |= check_field_decl (fields, t,
3861 : : cant_have_const_ctor,
3862 : : no_const_asn_ref);
3863 : : }
3864 : : /* Check members with class type for constructors, destructors,
3865 : : etc. */
3866 : 16135327 : else if (CLASS_TYPE_P (type))
3867 : : {
3868 : : /* Never let anything with uninheritable virtuals
3869 : : make it through without complaint. */
3870 : 2874877 : abstract_virtuals_error (field, type);
3871 : :
3872 : 2874877 : if (TREE_CODE (t) == UNION_TYPE && cxx_dialect < cxx11)
3873 : : {
3874 : 2940 : static bool warned;
3875 : 2940 : auto_diagnostic_group d;
3876 : 2940 : int oldcount = errorcount;
3877 : 2940 : if (TYPE_NEEDS_CONSTRUCTING (type))
3878 : 2 : error ("member %q+#D with constructor not allowed in union",
3879 : : field);
3880 : 2940 : if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
3881 : 0 : error ("member %q+#D with destructor not allowed in union", field);
3882 : 2940 : if (TYPE_HAS_COMPLEX_COPY_ASSIGN (type))
3883 : 3 : error ("member %q+#D with copy assignment operator not allowed in union",
3884 : : field);
3885 : 2940 : if (!warned && errorcount > oldcount)
3886 : : {
3887 : 4 : inform (DECL_SOURCE_LOCATION (field), "unrestricted unions "
3888 : : "only available with %<-std=c++11%> or %<-std=gnu++11%>");
3889 : 4 : warned = true;
3890 : : }
3891 : 2940 : }
3892 : : else
3893 : : {
3894 : 2871937 : TYPE_NEEDS_CONSTRUCTING (t) |= TYPE_NEEDS_CONSTRUCTING (type);
3895 : 8615811 : TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t)
3896 : 2871937 : |= TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type);
3897 : 2871937 : TYPE_HAS_COMPLEX_COPY_ASSIGN (t)
3898 : 2871937 : |= (TYPE_HAS_COMPLEX_COPY_ASSIGN (type)
3899 : 2871937 : || !TYPE_HAS_COPY_ASSIGN (type));
3900 : 5743874 : TYPE_HAS_COMPLEX_COPY_CTOR (t) |= (TYPE_HAS_COMPLEX_COPY_CTOR (type)
3901 : 2871937 : || !TYPE_HAS_COPY_CTOR (type));
3902 : 2871937 : TYPE_HAS_COMPLEX_MOVE_ASSIGN (t) |= TYPE_HAS_COMPLEX_MOVE_ASSIGN (type);
3903 : 2871937 : TYPE_HAS_COMPLEX_MOVE_CTOR (t) |= TYPE_HAS_COMPLEX_MOVE_CTOR (type);
3904 : 5743874 : TYPE_HAS_COMPLEX_DFLT (t) |= (!TYPE_HAS_DEFAULT_CONSTRUCTOR (type)
3905 : 2871937 : || TYPE_HAS_COMPLEX_DFLT (type));
3906 : : }
3907 : :
3908 : 2874877 : if (TYPE_HAS_COPY_CTOR (type)
3909 : 2874877 : && !TYPE_HAS_CONST_COPY_CTOR (type))
3910 : 2569 : *cant_have_const_ctor = 1;
3911 : :
3912 : 2874877 : if (TYPE_HAS_COPY_ASSIGN (type)
3913 : 2874877 : && !TYPE_HAS_CONST_COPY_ASSIGN (type))
3914 : 55 : *no_const_asn_ref = 1;
3915 : : }
3916 : :
3917 : 16288146 : check_abi_tags (t, field);
3918 : :
3919 : 16288146 : if (DECL_INITIAL (field) != NULL_TREE)
3920 : : /* `build_class_init_list' does not recognize
3921 : : non-FIELD_DECLs. */
3922 : 895180 : any_default_members = true;
3923 : :
3924 : 16288146 : return any_default_members;
3925 : : }
3926 : :
3927 : : /* Check the data members (both static and non-static), class-scoped
3928 : : typedefs, etc., appearing in the declaration of T. Issue
3929 : : appropriate diagnostics. Sets ACCESS_DECLS to a list (in
3930 : : declaration order) of access declarations; each TREE_VALUE in this
3931 : : list is a USING_DECL.
3932 : :
3933 : : In addition, set the following flags:
3934 : :
3935 : : EMPTY_P
3936 : : The class is empty, i.e., contains no non-static data members.
3937 : :
3938 : : CANT_HAVE_CONST_CTOR_P
3939 : : This class cannot have an implicitly generated copy constructor
3940 : : taking a const reference.
3941 : :
3942 : : CANT_HAVE_CONST_ASN_REF
3943 : : This class cannot have an implicitly generated assignment
3944 : : operator taking a const reference.
3945 : :
3946 : : All of these flags should be initialized before calling this
3947 : : function. */
3948 : :
3949 : : static void
3950 : 39307783 : check_field_decls (tree t, tree *access_decls,
3951 : : int *cant_have_const_ctor_p,
3952 : : int *no_const_asn_ref_p)
3953 : : {
3954 : 39307783 : int cant_pack = 0;
3955 : :
3956 : : /* Assume there are no access declarations. */
3957 : 39307783 : *access_decls = NULL_TREE;
3958 : : /* Effective C has things to say about classes with pointer members. */
3959 : 39307783 : tree pointer_member = NULL_TREE;
3960 : : /* Default initialized members affect the whole class. */
3961 : 39307783 : tree default_init_member = NULL_TREE;
3962 : : /* Lack of any non-static data member of non-volatile literal
3963 : : type affects a union. */
3964 : 39307783 : bool found_nv_literal_p = false;
3965 : : /* Standard layout requires all FIELDS have same access. */
3966 : 39307783 : int field_access = -1;
3967 : :
3968 : 269985150 : for (tree field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
3969 : : {
3970 : 230677367 : tree type = TREE_TYPE (field);
3971 : :
3972 : 230677367 : switch (TREE_CODE (field))
3973 : : {
3974 : 0 : default:
3975 : 0 : gcc_unreachable ();
3976 : :
3977 : 2107363 : case USING_DECL:
3978 : : /* Save the access declarations for our caller. */
3979 : 2107363 : *access_decls = tree_cons (NULL_TREE, field, *access_decls);
3980 : 2107363 : break;
3981 : :
3982 : : case TYPE_DECL:
3983 : : case TEMPLATE_DECL:
3984 : : break;
3985 : :
3986 : : case FUNCTION_DECL:
3987 : : /* FIXME: We should fold in the checking from check_methods. */
3988 : : break;
3989 : :
3990 : 1653881 : case CONST_DECL:
3991 : 1653881 : DECL_NONLOCAL (field) = 1;
3992 : 1653881 : break;
3993 : :
3994 : 10624338 : case VAR_DECL:
3995 : 10624338 : if (TREE_CODE (t) == UNION_TYPE
3996 : 36 : && cxx_dialect < cxx11)
3997 : : {
3998 : : /* [class.union]
3999 : :
4000 : : (C++98) If a union contains a static data member,
4001 : : ... the program is ill-formed. */
4002 : 1 : if (cxx_dialect < cxx11)
4003 : 1 : error ("in C++98 %q+D may not be static because it is "
4004 : : "a member of a union", field);
4005 : : }
4006 : 10624338 : goto data_member;
4007 : :
4008 : 15846676 : case FIELD_DECL:
4009 : 15846676 : if (TREE_CODE (t) == UNION_TYPE)
4010 : : {
4011 : : /* [class.union]
4012 : :
4013 : : If a union contains ... or a [non-static data] member
4014 : : of reference type, the program is ill-formed. */
4015 : 1048080 : if (TYPE_REF_P (type))
4016 : 33 : error ("non-static data member %q+D in a union may not "
4017 : : "have reference type %qT", field, type);
4018 : : }
4019 : :
4020 : 14798596 : data_member:
4021 : : /* Common VAR_DECL & FIELD_DECL processing. */
4022 : 26471014 : DECL_CONTEXT (field) = t;
4023 : 26471014 : DECL_NONLOCAL (field) = 1;
4024 : :
4025 : : /* Template instantiation can cause this. Perhaps this
4026 : : should be a specific instantiation check? */
4027 : 26471014 : if (TREE_CODE (type) == FUNCTION_TYPE)
4028 : : {
4029 : 10 : error ("data member %q+D invalidly declared function type", field);
4030 : 10 : type = build_pointer_type (type);
4031 : 10 : TREE_TYPE (field) = type;
4032 : : }
4033 : 26471004 : else if (TREE_CODE (type) == METHOD_TYPE)
4034 : : {
4035 : 0 : error ("data member %q+D invalidly declared method type", field);
4036 : 0 : type = build_pointer_type (type);
4037 : 0 : TREE_TYPE (field) = type;
4038 : : }
4039 : :
4040 : : break;
4041 : : }
4042 : :
4043 : 230677367 : if (TREE_CODE (field) != FIELD_DECL)
4044 : 214830691 : continue;
4045 : :
4046 : 15846676 : if (type == error_mark_node)
4047 : 161 : continue;
4048 : :
4049 : : /* If it is not a union and at least one non-static data member is
4050 : : non-literal, the whole class becomes non-literal. Per Core/1453,
4051 : : volatile non-static data members and base classes are also not allowed.
4052 : : If it is a union, we might set CLASSTYPE_LITERAL_P after we've seen all
4053 : : members.
4054 : : Note: if the type is incomplete we will complain later on. */
4055 : 15846515 : if (COMPLETE_TYPE_P (type))
4056 : : {
4057 : 15845126 : if (!literal_type_p (type) || CP_TYPE_VOLATILE_P (type))
4058 : 1089541 : CLASSTYPE_LITERAL_P (t) = false;
4059 : : else
4060 : : found_nv_literal_p = true;
4061 : : }
4062 : :
4063 : 15846515 : int this_field_access = (TREE_PROTECTED (field) ? 1
4064 : 14168451 : : TREE_PRIVATE (field) ? 2 : 0);
4065 : 15846515 : if (field_access != this_field_access)
4066 : : {
4067 : : /* A standard-layout class is a class that:
4068 : :
4069 : : ... has the same access control (Clause 11) for all
4070 : : non-static data members, */
4071 : 6078640 : if (field_access < 0)
4072 : : field_access = this_field_access;
4073 : : else
4074 : 32243 : CLASSTYPE_NON_STD_LAYOUT (t) = 1;
4075 : :
4076 : : /* Aggregates must be public. */
4077 : 6078640 : if (this_field_access)
4078 : 2919397 : CLASSTYPE_NON_AGGREGATE (t) = 1;
4079 : : }
4080 : :
4081 : : /* If this is of reference type, check if it needs an init. */
4082 : 15846515 : if (TYPE_REF_P (type))
4083 : : {
4084 : 477922 : CLASSTYPE_NON_LAYOUT_POD_P (t) = 1;
4085 : 477922 : CLASSTYPE_NON_STD_LAYOUT (t) = 1;
4086 : 477922 : if (DECL_INITIAL (field) == NULL_TREE)
4087 : 477837 : SET_CLASSTYPE_REF_FIELDS_NEED_INIT (t, 1);
4088 : 477922 : if (cxx_dialect < cxx11)
4089 : : {
4090 : : /* ARM $12.6.2: [A member initializer list] (or, for an
4091 : : aggregate, initialization by a brace-enclosed list) is the
4092 : : only way to initialize non-static const and reference
4093 : : members. */
4094 : 1025 : TYPE_HAS_COMPLEX_COPY_ASSIGN (t) = 1;
4095 : 1025 : TYPE_HAS_COMPLEX_MOVE_ASSIGN (t) = 1;
4096 : : }
4097 : : }
4098 : :
4099 : 15846515 : type = strip_array_types (type);
4100 : :
4101 : 15846515 : if (TYPE_PACKED (t))
4102 : : {
4103 : 1047 : if (!layout_pod_type_p (type) && !TYPE_PACKED (type))
4104 : : {
4105 : 18 : warning_at (DECL_SOURCE_LOCATION (field), 0,
4106 : : "ignoring packed attribute because of"
4107 : : " unpacked non-POD field %q#D", field);
4108 : 18 : cant_pack = 1;
4109 : : }
4110 : 1029 : else if (DECL_C_BIT_FIELD (field)
4111 : 1029 : || TYPE_ALIGN (TREE_TYPE (field)) > BITS_PER_UNIT)
4112 : 794 : DECL_PACKED (field) = 1;
4113 : : }
4114 : :
4115 : 15846515 : if (DECL_C_BIT_FIELD (field)
4116 : 15846515 : && integer_zerop (DECL_BIT_FIELD_REPRESENTATIVE (field)))
4117 : : /* We don't treat zero-width bitfields as making a class
4118 : : non-empty. */
4119 : : ;
4120 : 15845378 : else if (field_poverlapping_p (field)
4121 : 15845378 : && is_empty_class (TREE_TYPE (field)))
4122 : : /* Empty data members also don't make a class non-empty. */
4123 : 180353 : CLASSTYPE_CONTAINS_EMPTY_CLASS_P (t) = 1;
4124 : : else
4125 : : {
4126 : : /* The class is non-empty. */
4127 : 15665025 : CLASSTYPE_EMPTY_P (t) = 0;
4128 : : /* The class is not even nearly empty. */
4129 : 15665025 : CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
4130 : : /* If one of the data members contains an empty class, so
4131 : : does T. */
4132 : 2798294 : if (CLASS_TYPE_P (type)
4133 : 18451274 : && CLASSTYPE_CONTAINS_EMPTY_CLASS_P (type))
4134 : 841843 : CLASSTYPE_CONTAINS_EMPTY_CLASS_P (t) = 1;
4135 : : }
4136 : :
4137 : : /* This is used by -Weffc++ (see below). Warn only for pointers
4138 : : to members which might hold dynamic memory. So do not warn
4139 : : for pointers to functions or pointers to members. */
4140 : 15846515 : if (TYPE_PTR_P (type)
4141 : 15846515 : && !TYPE_PTRFN_P (type))
4142 : : pointer_member = field;
4143 : :
4144 : 15846515 : if (CLASS_TYPE_P (type))
4145 : : {
4146 : 2966602 : if (CLASSTYPE_REF_FIELDS_NEED_INIT (type))
4147 : 3260 : SET_CLASSTYPE_REF_FIELDS_NEED_INIT (t, 1);
4148 : 2966602 : if (CLASSTYPE_READONLY_FIELDS_NEED_INIT (type))
4149 : 30039 : SET_CLASSTYPE_READONLY_FIELDS_NEED_INIT (t, 1);
4150 : : }
4151 : :
4152 : 15846515 : if (DECL_MUTABLE_P (field) || TYPE_HAS_MUTABLE_P (type))
4153 : 217296 : CLASSTYPE_HAS_MUTABLE (t) = 1;
4154 : :
4155 : 15846515 : if (DECL_MUTABLE_P (field))
4156 : : {
4157 : 134245 : if (TYPE_REF_P (type))
4158 : 3 : error ("member %q+D cannot be declared as a %<mutable%> "
4159 : : "reference", field);
4160 : 134242 : else if (CP_TYPE_CONST_P (type))
4161 : 3 : error ("member %q+D cannot be declared both %<const%> "
4162 : : "and %<mutable%>", field);
4163 : : }
4164 : :
4165 : 15846515 : if (! layout_pod_type_p (type))
4166 : : /* DR 148 now allows pointers to members (which are POD themselves),
4167 : : to be allowed in POD structs. */
4168 : 2494115 : CLASSTYPE_NON_LAYOUT_POD_P (t) = 1;
4169 : :
4170 : 15846515 : if (field_poverlapping_p (field))
4171 : : /* A potentially-overlapping non-static data member makes the class
4172 : : non-layout-POD. */
4173 : 237602 : CLASSTYPE_NON_LAYOUT_POD_P (t) = 1;
4174 : :
4175 : 15846515 : if (!std_layout_type_p (type))
4176 : 721252 : CLASSTYPE_NON_STD_LAYOUT (t) = 1;
4177 : :
4178 : 15846515 : if (! zero_init_p (type))
4179 : 158 : CLASSTYPE_NON_ZERO_INIT_P (t) = 1;
4180 : :
4181 : : /* We set DECL_C_BIT_FIELD in grokbitfield.
4182 : : If the type and width are valid, we'll also set DECL_BIT_FIELD. */
4183 : 15846515 : if (DECL_C_BIT_FIELD (field))
4184 : 426361 : check_bitfield_decl (field);
4185 : :
4186 : 15846515 : if (check_field_decl (field, t,
4187 : : cant_have_const_ctor_p, no_const_asn_ref_p))
4188 : : {
4189 : 895159 : if (default_init_member
4190 : 368169 : && TREE_CODE (t) == UNION_TYPE)
4191 : : {
4192 : 12 : auto_diagnostic_group d;
4193 : 12 : error ("multiple fields in union %qT initialized", t);
4194 : 12 : inform (DECL_SOURCE_LOCATION (default_init_member),
4195 : : "initialized member %q+D declared here",
4196 : : default_init_member);
4197 : 12 : }
4198 : : default_init_member = field;
4199 : : }
4200 : :
4201 : : /* Now that we've removed bit-field widths from DECL_INITIAL,
4202 : : anything left in DECL_INITIAL is an NSDMI that makes the class
4203 : : non-aggregate in C++11, and non-layout-POD always. */
4204 : 15846515 : if (DECL_INITIAL (field))
4205 : : {
4206 : 892514 : if (cxx_dialect < cxx14)
4207 : 966 : CLASSTYPE_NON_AGGREGATE (t) = true;
4208 : : else
4209 : 891548 : CLASSTYPE_NON_POD_AGGREGATE (t) = true;
4210 : : }
4211 : :
4212 : 15846515 : if (CP_TYPE_CONST_P (type))
4213 : : {
4214 : : /* If any field is const, the structure type is pseudo-const. */
4215 : 277556 : C_TYPE_FIELDS_READONLY (t) = 1;
4216 : 277556 : if (DECL_INITIAL (field) == NULL_TREE)
4217 : 228930 : SET_CLASSTYPE_READONLY_FIELDS_NEED_INIT (t, 1);
4218 : 277556 : if (cxx_dialect < cxx11)
4219 : : {
4220 : : /* ARM $12.6.2: [A member initializer list] (or, for an
4221 : : aggregate, initialization by a brace-enclosed list) is the
4222 : : only way to initialize non-static const and reference
4223 : : members. */
4224 : 676 : TYPE_HAS_COMPLEX_COPY_ASSIGN (t) = 1;
4225 : 676 : TYPE_HAS_COMPLEX_MOVE_ASSIGN (t) = 1;
4226 : : }
4227 : : }
4228 : : /* A field that is pseudo-const makes the structure likewise. */
4229 : 15568959 : else if (CLASS_TYPE_P (type))
4230 : : {
4231 : 2917069 : C_TYPE_FIELDS_READONLY (t) |= C_TYPE_FIELDS_READONLY (type);
4232 : 2917069 : SET_CLASSTYPE_READONLY_FIELDS_NEED_INIT (t,
4233 : : CLASSTYPE_READONLY_FIELDS_NEED_INIT (t)
4234 : : | CLASSTYPE_READONLY_FIELDS_NEED_INIT (type));
4235 : : }
4236 : :
4237 : : /* Core issue 80: A non-static data member is required to have a
4238 : : different name from the class iff the class has a
4239 : : user-declared constructor. */
4240 : 15846515 : if (constructor_name_p (DECL_NAME (field), t)
4241 : 15846515 : && TYPE_HAS_USER_CONSTRUCTOR (t))
4242 : 12 : permerror (DECL_SOURCE_LOCATION (field),
4243 : : "field %q#D with same name as class", field);
4244 : : }
4245 : :
4246 : : /* Per CWG 2096, a type is a literal type if it is a union, and at least
4247 : : one of its non-static data members is of non-volatile literal type. */
4248 : 39307783 : if (TREE_CODE (t) == UNION_TYPE && found_nv_literal_p)
4249 : 389577 : CLASSTYPE_LITERAL_P (t) = true;
4250 : :
4251 : : /* Effective C++ rule 11: if a class has dynamic memory held by pointers,
4252 : : it should also define a copy constructor and an assignment operator to
4253 : : implement the correct copy semantic (deep vs shallow, etc.). As it is
4254 : : not feasible to check whether the constructors do allocate dynamic memory
4255 : : and store it within members, we approximate the warning like this:
4256 : :
4257 : : -- Warn only if there are members which are pointers
4258 : : -- Warn only if there is a non-trivial constructor (otherwise,
4259 : : there cannot be memory allocated).
4260 : : -- Warn only if there is a non-trivial destructor. We assume that the
4261 : : user at least implemented the cleanup correctly, and a destructor
4262 : : is needed to free dynamic memory.
4263 : :
4264 : : This seems enough for practical purposes. */
4265 : 39307783 : if (warn_ecpp
4266 : 138 : && pointer_member
4267 : 15 : && TYPE_HAS_USER_CONSTRUCTOR (t)
4268 : 15 : && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t)
4269 : 39307795 : && !(TYPE_HAS_COPY_CTOR (t) && TYPE_HAS_COPY_ASSIGN (t)))
4270 : : {
4271 : 9 : auto_diagnostic_group d;
4272 : 9 : if (warning (OPT_Weffc__, "%q#T has pointer data members", t))
4273 : : {
4274 : 9 : if (! TYPE_HAS_COPY_CTOR (t))
4275 : : {
4276 : 6 : warning (OPT_Weffc__,
4277 : : " but does not declare %<%T(const %T&)%>", t, t);
4278 : 6 : if (!TYPE_HAS_COPY_ASSIGN (t))
4279 : 3 : warning (OPT_Weffc__, " or %<operator=(const %T&)%>", t);
4280 : : }
4281 : 3 : else if (! TYPE_HAS_COPY_ASSIGN (t))
4282 : 3 : warning (OPT_Weffc__,
4283 : : " but does not declare %<operator=(const %T&)%>", t);
4284 : 9 : inform (DECL_SOURCE_LOCATION (pointer_member),
4285 : : "pointer member %q+D declared here", pointer_member);
4286 : : }
4287 : 9 : }
4288 : :
4289 : : /* Non-static data member initializers make the default constructor
4290 : : non-trivial. */
4291 : 39307783 : if (default_init_member)
4292 : : {
4293 : 526990 : TYPE_NEEDS_CONSTRUCTING (t) = true;
4294 : 526990 : TYPE_HAS_COMPLEX_DFLT (t) = true;
4295 : : }
4296 : :
4297 : : /* If any of the fields couldn't be packed, unset TYPE_PACKED. */
4298 : 39307783 : if (cant_pack)
4299 : 12 : TYPE_PACKED (t) = 0;
4300 : :
4301 : : /* Check anonymous struct/anonymous union fields. */
4302 : 39307783 : finish_struct_anon (t);
4303 : :
4304 : : /* We've built up the list of access declarations in reverse order.
4305 : : Fix that now. */
4306 : 39307783 : *access_decls = nreverse (*access_decls);
4307 : 39307783 : }
4308 : :
4309 : : /* If TYPE is an empty class type, records its OFFSET in the table of
4310 : : OFFSETS. */
4311 : :
4312 : : static int
4313 : 25995649 : record_subobject_offset (tree type, tree offset, splay_tree offsets)
4314 : : {
4315 : 25995649 : splay_tree_node n;
4316 : :
4317 : 25995649 : if (!is_empty_class (type))
4318 : : return 0;
4319 : :
4320 : : /* Record the location of this empty object in OFFSETS. */
4321 : 23340094 : n = splay_tree_lookup (offsets, (splay_tree_key) offset);
4322 : 23340094 : if (!n)
4323 : 19681544 : n = splay_tree_insert (offsets,
4324 : : (splay_tree_key) offset,
4325 : : (splay_tree_value) NULL_TREE);
4326 : 46680188 : n->value = ((splay_tree_value)
4327 : 23340094 : tree_cons (NULL_TREE,
4328 : : type,
4329 : 23340094 : (tree) n->value));
4330 : :
4331 : 23340094 : return 0;
4332 : : }
4333 : :
4334 : : /* Returns nonzero if TYPE is an empty class type and there is
4335 : : already an entry in OFFSETS for the same TYPE as the same OFFSET. */
4336 : :
4337 : : static int
4338 : 599435 : check_subobject_offset (tree type, tree offset, splay_tree offsets)
4339 : : {
4340 : 599435 : splay_tree_node n;
4341 : 599435 : tree t;
4342 : :
4343 : 599435 : if (!is_empty_class (type))
4344 : : return 0;
4345 : :
4346 : : /* Record the location of this empty object in OFFSETS. */
4347 : 513500 : n = splay_tree_lookup (offsets, (splay_tree_key) offset);
4348 : 513500 : if (!n)
4349 : : return 0;
4350 : :
4351 : 513416 : enum { ignore, fast, slow, warn }
4352 : 513416 : cv_check = (abi_version_crosses (19) ? slow
4353 : 513383 : : abi_version_at_least (19) ? fast
4354 : 0 : : ignore);
4355 : 4024308 : for (t = (tree) n->value; t; t = TREE_CHAIN (t))
4356 : : {
4357 : 3513762 : tree elt = TREE_VALUE (t);
4358 : :
4359 : 3513762 : if (same_type_p (elt, type))
4360 : : return 1;
4361 : :
4362 : 3510892 : if (cv_check != ignore
4363 : 3510892 : && similar_type_p (elt, type))
4364 : : {
4365 : 12 : if (cv_check == fast)
4366 : : return 1;
4367 : : cv_check = warn;
4368 : : }
4369 : : }
4370 : :
4371 : 510546 : if (cv_check == warn)
4372 : : {
4373 : 12 : warning (OPT_Wabi, "layout of %qs member of type %qT changes in %qs",
4374 : : "[[no_unique_address]]", type, "-fabi-version=19");
4375 : 12 : if (abi_version_at_least (19))
4376 : : return 1;
4377 : : }
4378 : :
4379 : : return 0;
4380 : : }
4381 : :
4382 : : /* Walk through all the subobjects of TYPE (located at OFFSET). Call
4383 : : F for every subobject, passing it the type, offset, and table of
4384 : : OFFSETS. If VBASES_P is one, then virtual non-primary bases should
4385 : : be traversed.
4386 : :
4387 : : If MAX_OFFSET is non-NULL, then subobjects with an offset greater
4388 : : than MAX_OFFSET will not be walked.
4389 : :
4390 : : If F returns a nonzero value, the traversal ceases, and that value
4391 : : is returned. Otherwise, returns zero. */
4392 : :
4393 : : static int
4394 : 49288305 : walk_subobject_offsets (tree type,
4395 : : subobject_offset_fn f,
4396 : : tree offset,
4397 : : splay_tree offsets,
4398 : : tree max_offset,
4399 : : int vbases_p)
4400 : : {
4401 : 49288305 : int r = 0;
4402 : 49288305 : tree type_binfo = NULL_TREE;
4403 : :
4404 : : /* If this OFFSET is bigger than the MAX_OFFSET, then we should
4405 : : stop. */
4406 : 49288305 : if (max_offset && tree_int_cst_lt (max_offset, offset))
4407 : : return 0;
4408 : :
4409 : 40967217 : if (type == error_mark_node)
4410 : : return 0;
4411 : :
4412 : 40967217 : if (!TYPE_P (type))
4413 : : {
4414 : 5213847 : type_binfo = type;
4415 : 5213847 : type = BINFO_TYPE (type);
4416 : : }
4417 : :
4418 : 40967217 : if (CLASS_TYPE_P (type))
4419 : : {
4420 : 30742597 : tree field;
4421 : 30742597 : tree binfo;
4422 : 30742597 : int i;
4423 : :
4424 : : /* Avoid recursing into objects that are not interesting. */
4425 : 30742597 : if (!CLASSTYPE_CONTAINS_EMPTY_CLASS_P (type))
4426 : : return 0;
4427 : :
4428 : : /* Record the location of TYPE. */
4429 : 26595517 : r = (*f) (type, offset, offsets);
4430 : 26595517 : if (r)
4431 : : return r;
4432 : :
4433 : : /* Iterate through the direct base classes of TYPE. */
4434 : 26592635 : if (!type_binfo)
4435 : 22277874 : type_binfo = TYPE_BINFO (type);
4436 : 31674651 : for (i = 0; BINFO_BASE_ITERATE (type_binfo, i, binfo); i++)
4437 : : {
4438 : 5082934 : tree binfo_offset;
4439 : :
4440 : 5082934 : if (BINFO_VIRTUAL_P (binfo))
4441 : 1587 : continue;
4442 : :
4443 : 5081347 : tree orig_binfo;
4444 : : /* We cannot rely on BINFO_OFFSET being set for the base
4445 : : class yet, but the offsets for direct non-virtual
4446 : : bases can be calculated by going back to the TYPE. */
4447 : 5081347 : orig_binfo = BINFO_BASE_BINFO (TYPE_BINFO (type), i);
4448 : 5081347 : binfo_offset = size_binop (PLUS_EXPR,
4449 : : offset,
4450 : : BINFO_OFFSET (orig_binfo));
4451 : :
4452 : 5081347 : r = walk_subobject_offsets (binfo,
4453 : : f,
4454 : : binfo_offset,
4455 : : offsets,
4456 : : max_offset,
4457 : : /*vbases_p=*/0);
4458 : 5081347 : if (r)
4459 : : return r;
4460 : : }
4461 : :
4462 : 26591717 : if (CLASSTYPE_VBASECLASSES (type))
4463 : : {
4464 : 1298 : unsigned ix;
4465 : 1298 : vec<tree, va_gc> *vbases;
4466 : :
4467 : : /* Iterate through the virtual base classes of TYPE. In G++
4468 : : 3.2, we included virtual bases in the direct base class
4469 : : loop above, which results in incorrect results; the
4470 : : correct offsets for virtual bases are only known when
4471 : : working with the most derived type. */
4472 : 1298 : if (vbases_p)
4473 : 30 : for (vbases = CLASSTYPE_VBASECLASSES (type), ix = 0;
4474 : 30 : vec_safe_iterate (vbases, ix, &binfo); ix++)
4475 : : {
4476 : 15 : r = walk_subobject_offsets (binfo,
4477 : : f,
4478 : 15 : size_binop (PLUS_EXPR,
4479 : : offset,
4480 : : BINFO_OFFSET (binfo)),
4481 : : offsets,
4482 : : max_offset,
4483 : : /*vbases_p=*/0);
4484 : 15 : if (r)
4485 : : return r;
4486 : : }
4487 : : else
4488 : : {
4489 : : /* We still have to walk the primary base, if it is
4490 : : virtual. (If it is non-virtual, then it was walked
4491 : : above.) */
4492 : 1283 : tree vbase = get_primary_binfo (type_binfo);
4493 : :
4494 : 395 : if (vbase && BINFO_VIRTUAL_P (vbase)
4495 : 234 : && BINFO_PRIMARY_P (vbase)
4496 : 1517 : && BINFO_INHERITANCE_CHAIN (vbase) == type_binfo)
4497 : : {
4498 : 200 : r = (walk_subobject_offsets
4499 : 200 : (vbase, f, offset,
4500 : : offsets, max_offset, /*vbases_p=*/0));
4501 : 200 : if (r)
4502 : : return r;
4503 : : }
4504 : : }
4505 : : }
4506 : :
4507 : : /* Iterate through the fields of TYPE. */
4508 : 516325069 : for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
4509 : 489735685 : if (TREE_CODE (field) == FIELD_DECL
4510 : 10481483 : && TREE_TYPE (field) != error_mark_node
4511 : 500217142 : && !DECL_ARTIFICIAL (field))
4512 : : {
4513 : 5370158 : tree field_offset;
4514 : :
4515 : 5370158 : field_offset = byte_position (field);
4516 : :
4517 : 5370158 : r = walk_subobject_offsets (TREE_TYPE (field),
4518 : : f,
4519 : : size_binop (PLUS_EXPR,
4520 : : offset,
4521 : : field_offset),
4522 : : offsets,
4523 : : max_offset,
4524 : : /*vbases_p=*/1);
4525 : 5370158 : if (r)
4526 : : return r;
4527 : : }
4528 : : }
4529 : 10224620 : else if (TREE_CODE (type) == ARRAY_TYPE)
4530 : : {
4531 : 649850 : tree element_type = strip_array_types (type);
4532 : 649850 : tree domain = TYPE_DOMAIN (type);
4533 : 649850 : tree index;
4534 : :
4535 : : /* Avoid recursing into objects that are not interesting. */
4536 : 27830 : if (!CLASS_TYPE_P (element_type)
4537 : 27819 : || !CLASSTYPE_CONTAINS_EMPTY_CLASS_P (element_type)
4538 : 13179 : || !domain
4539 : 663010 : || integer_minus_onep (TYPE_MAX_VALUE (domain)))
4540 : 636907 : return 0;
4541 : :
4542 : : /* Step through each of the elements in the array. */
4543 : 12943 : for (index = size_zero_node;
4544 : 37291 : !tree_int_cst_lt (TYPE_MAX_VALUE (domain), index);
4545 : 24348 : index = size_binop (PLUS_EXPR, index, size_one_node))
4546 : : {
4547 : 36121 : r = walk_subobject_offsets (TREE_TYPE (type),
4548 : : f,
4549 : : offset,
4550 : : offsets,
4551 : : max_offset,
4552 : : /*vbases_p=*/1);
4553 : 36121 : if (r)
4554 : : return r;
4555 : 36121 : offset = size_binop (PLUS_EXPR, offset,
4556 : : TYPE_SIZE_UNIT (TREE_TYPE (type)));
4557 : : /* If this new OFFSET is bigger than the MAX_OFFSET, then
4558 : : there's no point in iterating through the remaining
4559 : : elements of the array. */
4560 : 36121 : if (max_offset && tree_int_cst_lt (max_offset, offset))
4561 : : break;
4562 : : }
4563 : : }
4564 : :
4565 : : return 0;
4566 : : }
4567 : :
4568 : : /* Return true iff FIELD_DECL DECL is potentially overlapping. */
4569 : :
4570 : : static bool
4571 : 68699401 : field_poverlapping_p (tree decl)
4572 : : {
4573 : : /* Base fields are actually potentially overlapping, but C++ bases go through
4574 : : a different code path based on binfos, and ObjC++ base fields are laid out
4575 : : in objc-act, so we don't want layout_class_type to mess with them. */
4576 : 68699401 : if (DECL_FIELD_IS_BASE (decl))
4577 : : {
4578 : 0 : gcc_checking_assert (c_dialect_objc ());
4579 : : return false;
4580 : : }
4581 : :
4582 : 68699401 : return lookup_attribute ("no_unique_address",
4583 : 137398802 : DECL_ATTRIBUTES (decl));
4584 : : }
4585 : :
4586 : : /* Return true iff DECL is an empty field, either for an empty base or a
4587 : : [[no_unique_address]] data member. */
4588 : :
4589 : : bool
4590 : 223364471 : is_empty_field (tree decl)
4591 : : {
4592 : 223364471 : if (!decl || TREE_CODE (decl) != FIELD_DECL)
4593 : : return false;
4594 : :
4595 : 98271056 : bool r = (is_empty_class (TREE_TYPE (decl))
4596 : 100488518 : && (DECL_FIELD_IS_BASE (decl)
4597 : 527150 : || field_poverlapping_p (decl)));
4598 : :
4599 : : /* Empty fields should have size zero. */
4600 : 2083234 : gcc_checking_assert (!r || integer_zerop (DECL_SIZE (decl)));
4601 : :
4602 : : return r;
4603 : : }
4604 : :
4605 : : /* Record all of the empty subobjects of DECL_OR_BINFO. */
4606 : :
4607 : : static void
4608 : 36708772 : record_subobject_offsets (tree decl_or_binfo,
4609 : : splay_tree offsets)
4610 : : {
4611 : 36708772 : tree type, offset;
4612 : 36708772 : bool overlapping, vbases_p;
4613 : :
4614 : 36708772 : if (DECL_P (decl_or_binfo))
4615 : : {
4616 : 15846515 : tree decl = decl_or_binfo;
4617 : 15846515 : type = TREE_TYPE (decl);
4618 : 15846515 : offset = byte_position (decl);
4619 : 15846515 : overlapping = field_poverlapping_p (decl);
4620 : 15846515 : vbases_p = true;
4621 : : }
4622 : : else
4623 : : {
4624 : 20862257 : type = BINFO_TYPE (decl_or_binfo);
4625 : 20862257 : offset = BINFO_OFFSET (decl_or_binfo);
4626 : 20862257 : overlapping = true;
4627 : 20862257 : vbases_p = false;
4628 : : }
4629 : :
4630 : 36708772 : tree max_offset;
4631 : : /* If recording subobjects for a non-static data member or a
4632 : : non-empty base class, we do not need to record offsets beyond
4633 : : the size of the biggest empty class. Additional data members
4634 : : will go at the end of the class. Additional base classes will go
4635 : : either at offset zero (if empty, in which case they cannot
4636 : : overlap with offsets past the size of the biggest empty class) or
4637 : : at the end of the class.
4638 : :
4639 : : However, if we are placing an empty base class, then we must record
4640 : : all offsets, as either the empty class is at offset zero (where
4641 : : other empty classes might later be placed) or at the end of the
4642 : : class (where other objects might then be placed, so other empty
4643 : : subobjects might later overlap). */
4644 : 20862257 : if (!overlapping
4645 : 36708772 : || !is_empty_class (type))
4646 : 18080788 : max_offset = sizeof_biggest_empty_class;
4647 : : else
4648 : : max_offset = NULL_TREE;
4649 : 36708772 : walk_subobject_offsets (type, record_subobject_offset, offset,
4650 : : offsets, max_offset, vbases_p);
4651 : 36708772 : }
4652 : :
4653 : : /* Returns nonzero if any of the empty subobjects of TYPE (located at
4654 : : OFFSET) conflict with entries in OFFSETS. If VBASES_P is nonzero,
4655 : : virtual bases of TYPE are examined. */
4656 : :
4657 : : static int
4658 : 35664284 : layout_conflict_p (tree type,
4659 : : tree offset,
4660 : : splay_tree offsets,
4661 : : int vbases_p)
4662 : : {
4663 : 35664284 : splay_tree_node max_node;
4664 : :
4665 : : /* Get the node in OFFSETS that indicates the maximum offset where
4666 : : an empty subobject is located. */
4667 : 35664284 : max_node = splay_tree_max (offsets);
4668 : : /* If there aren't any empty subobjects, then there's no point in
4669 : : performing this check. */
4670 : 35664284 : if (!max_node)
4671 : : return 0;
4672 : :
4673 : 2091362 : return walk_subobject_offsets (type, check_subobject_offset, offset,
4674 : 2091362 : offsets, (tree) (max_node->key),
4675 : 2091362 : vbases_p);
4676 : : }
4677 : :
4678 : : /* DECL is a FIELD_DECL corresponding either to a base subobject of a
4679 : : non-static data member of the type indicated by RLI. BINFO is the
4680 : : binfo corresponding to the base subobject, OFFSETS maps offsets to
4681 : : types already located at those offsets. This function determines
4682 : : the position of the DECL. */
4683 : :
4684 : : static void
4685 : 18081556 : layout_nonempty_base_or_field (record_layout_info rli,
4686 : : tree decl,
4687 : : tree binfo,
4688 : : splay_tree offsets)
4689 : : {
4690 : 18081556 : tree offset = NULL_TREE;
4691 : 18081556 : bool field_p;
4692 : 18081556 : tree type;
4693 : :
4694 : 18081556 : if (binfo)
4695 : : {
4696 : : /* For the purposes of determining layout conflicts, we want to
4697 : : use the class type of BINFO; TREE_TYPE (DECL) will be the
4698 : : CLASSTYPE_AS_BASE version, which does not contain entries for
4699 : : zero-sized bases. */
4700 : 2414626 : type = TREE_TYPE (binfo);
4701 : 2414626 : field_p = false;
4702 : : }
4703 : : else
4704 : : {
4705 : 15666930 : type = TREE_TYPE (decl);
4706 : 15666930 : field_p = true;
4707 : : }
4708 : :
4709 : : /* Try to place the field. It may take more than one try if we have
4710 : : a hard time placing the field without putting two objects of the
4711 : : same type at the same address. */
4712 : 232 : while (1)
4713 : : {
4714 : 18081788 : struct record_layout_info_s old_rli = *rli;
4715 : :
4716 : : /* Place this field. */
4717 : 18081788 : place_field (rli, decl);
4718 : 18081788 : offset = byte_position (decl);
4719 : :
4720 : : /* We have to check to see whether or not there is already
4721 : : something of the same type at the offset we're about to use.
4722 : : For example, consider:
4723 : :
4724 : : struct S {};
4725 : : struct T : public S { int i; };
4726 : : struct U : public S, public T {};
4727 : :
4728 : : Here, we put S at offset zero in U. Then, we can't put T at
4729 : : offset zero -- its S component would be at the same address
4730 : : as the S we already allocated. So, we have to skip ahead.
4731 : : Since all data members, including those whose type is an
4732 : : empty class, have nonzero size, any overlap can happen only
4733 : : with a direct or indirect base-class -- it can't happen with
4734 : : a data member. */
4735 : : /* In a union, overlap is permitted; all members are placed at
4736 : : offset zero. */
4737 : 18081788 : if (TREE_CODE (rli->t) == UNION_TYPE)
4738 : : break;
4739 : 19449478 : if (layout_conflict_p (field_p ? type : binfo, offset,
4740 : : offsets, field_p))
4741 : : {
4742 : : /* Strip off the size allocated to this field. That puts us
4743 : : at the first place we could have put the field with
4744 : : proper alignment. */
4745 : 232 : *rli = old_rli;
4746 : :
4747 : : /* Bump up by the alignment required for the type. */
4748 : 232 : rli->bitpos
4749 : 232 : = size_binop (PLUS_EXPR, rli->bitpos,
4750 : : bitsize_int (binfo
4751 : : ? CLASSTYPE_ALIGN (type)
4752 : : : TYPE_ALIGN (type)));
4753 : 232 : normalize_rli (rli);
4754 : : }
4755 : 17034561 : else if (TREE_CODE (type) == NULLPTR_TYPE
4756 : 89362 : && warn_abi && abi_version_crosses (9))
4757 : : {
4758 : : /* Before ABI v9, we were giving nullptr_t alignment of 1; if
4759 : : the offset wasn't aligned like a pointer when we started to
4760 : : layout this field, that affects its position. */
4761 : 12 : tree pos = rli_size_unit_so_far (&old_rli);
4762 : 12 : if (int_cst_value (pos) % TYPE_ALIGN_UNIT (ptr_type_node) != 0)
4763 : : {
4764 : 3 : if (abi_version_at_least (9))
4765 : 3 : warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wabi,
4766 : : "alignment of %qD increased in %<-fabi-version=9%> "
4767 : : "(GCC 5.2)", decl);
4768 : : else
4769 : 0 : warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wabi, "alignment "
4770 : : "of %qD will increase in %<-fabi-version=9%>",
4771 : : decl);
4772 : : }
4773 : : break;
4774 : : }
4775 : : else
4776 : : /* There was no conflict. We're done laying out this field. */
4777 : : break;
4778 : : }
4779 : :
4780 : : /* Now that we know where it will be placed, update its
4781 : : BINFO_OFFSET. */
4782 : 20496182 : if (binfo && CLASS_TYPE_P (BINFO_TYPE (binfo)))
4783 : : /* Indirect virtual bases may have a nonzero BINFO_OFFSET at
4784 : : this point because their BINFO_OFFSET is copied from another
4785 : : hierarchy. Therefore, we may not need to add the entire
4786 : : OFFSET. */
4787 : 2414626 : propagate_binfo_offsets (binfo,
4788 : : size_diffop_loc (input_location,
4789 : : fold_convert (ssizetype, offset),
4790 : 2414626 : fold_convert (ssizetype,
4791 : : BINFO_OFFSET (binfo))));
4792 : 18081556 : }
4793 : :
4794 : : /* Returns true if TYPE is empty and OFFSET is nonzero. */
4795 : :
4796 : : static int
4797 : 433 : empty_base_at_nonzero_offset_p (tree type,
4798 : : tree offset,
4799 : : splay_tree /*offsets*/)
4800 : : {
4801 : 433 : return is_empty_class (type) && !integer_zerop (offset);
4802 : : }
4803 : :
4804 : : /* Layout the empty base BINFO. EOC indicates the byte currently just
4805 : : past the end of the class, and should be correctly aligned for a
4806 : : class of the type indicated by BINFO; OFFSETS gives the offsets of
4807 : : the empty bases allocated so far. T is the most derived
4808 : : type. Return nonzero iff we added it at the end. */
4809 : :
4810 : : static bool
4811 : 18627984 : layout_empty_base_or_field (record_layout_info rli, tree binfo_or_decl,
4812 : : splay_tree offsets)
4813 : : {
4814 : 18627984 : tree alignment;
4815 : 18627984 : bool atend = false;
4816 : 18627984 : tree binfo = NULL_TREE;
4817 : 18627984 : tree decl = NULL_TREE;
4818 : 18627984 : tree type;
4819 : 18627984 : if (TREE_CODE (binfo_or_decl) == TREE_BINFO)
4820 : : {
4821 : 18447631 : binfo = binfo_or_decl;
4822 : 18447631 : type = BINFO_TYPE (binfo);
4823 : : }
4824 : : else
4825 : : {
4826 : 180353 : decl = binfo_or_decl;
4827 : 180353 : type = TREE_TYPE (decl);
4828 : : }
4829 : :
4830 : : /* On some platforms (ARM), even empty classes will not be
4831 : : byte-aligned. */
4832 : 18627984 : tree eoc = round_up_loc (input_location,
4833 : : rli_size_unit_so_far (rli),
4834 : 18627984 : CLASSTYPE_ALIGN_UNIT (type));
4835 : :
4836 : : /* This routine should only be used for empty classes. */
4837 : 18627984 : gcc_assert (is_empty_class (type));
4838 : :
4839 : 18627984 : if (decl && DECL_USER_ALIGN (decl))
4840 : 3 : alignment = size_int (DECL_ALIGN_UNIT (decl));
4841 : : else
4842 : 18627981 : alignment = size_int (CLASSTYPE_ALIGN_UNIT (type));
4843 : :
4844 : : /* This is an empty base class. We first try to put it at offset
4845 : : zero. */
4846 : 18627984 : tree offset = size_zero_node;
4847 : 18627984 : if (TREE_CODE (rli->t) != UNION_TYPE
4848 : 18627984 : && layout_conflict_p (type,
4849 : : offset,
4850 : : offsets,
4851 : : /*vbases_p=*/0))
4852 : : {
4853 : : /* That didn't work. Now, we move forward from the next
4854 : : available spot in the class. */
4855 : 2644 : atend = true;
4856 : : offset = eoc;
4857 : 3877 : while (1)
4858 : : {
4859 : 2644 : if (!layout_conflict_p (type,
4860 : : offset,
4861 : : offsets,
4862 : : /*vbases_p=*/0))
4863 : : /* We finally found a spot where there's no overlap. */
4864 : : break;
4865 : :
4866 : : /* There's overlap here, too. Bump along to the next spot. */
4867 : 1233 : offset = size_binop (PLUS_EXPR, offset, alignment);
4868 : : }
4869 : : }
4870 : :
4871 : 18627984 : if (decl && DECL_USER_ALIGN (decl))
4872 : : {
4873 : 3 : rli->record_align = MAX (rli->record_align, DECL_ALIGN (decl));
4874 : 3 : if (warn_packed)
4875 : 0 : rli->unpacked_align = MAX (rli->unpacked_align, DECL_ALIGN (decl));
4876 : 3 : TYPE_USER_ALIGN (rli->t) = 1;
4877 : : }
4878 : 18627981 : else if (CLASSTYPE_USER_ALIGN (type))
4879 : : {
4880 : 21 : rli->record_align = MAX (rli->record_align, CLASSTYPE_ALIGN (type));
4881 : 21 : if (warn_packed)
4882 : 0 : rli->unpacked_align = MAX (rli->unpacked_align, CLASSTYPE_ALIGN (type));
4883 : 21 : TYPE_USER_ALIGN (rli->t) = 1;
4884 : : }
4885 : :
4886 : 18627984 : if (binfo)
4887 : : /* Adjust BINFO_OFFSET (binfo) to be exactly OFFSET. */
4888 : 36895262 : propagate_binfo_offsets (binfo,
4889 : 18447631 : size_diffop (offset, BINFO_OFFSET (binfo)));
4890 : : else
4891 : : {
4892 : 180353 : DECL_FIELD_OFFSET (decl) = offset;
4893 : 180353 : DECL_FIELD_BIT_OFFSET (decl) = bitsize_zero_node;
4894 : 180353 : SET_DECL_OFFSET_ALIGN (decl, BITS_PER_UNIT);
4895 : : }
4896 : :
4897 : 18627984 : return atend;
4898 : : }
4899 : :
4900 : : /* Build the FIELD_DECL for BASETYPE as a base of T, add it to the chain of
4901 : : fields at NEXT_FIELD, and return it. */
4902 : :
4903 : : static tree
4904 : 20786891 : build_base_field_1 (tree t, tree binfo, tree access, tree *&next_field)
4905 : : {
4906 : : /* Create the FIELD_DECL. */
4907 : 20786891 : tree basetype = BINFO_TYPE (binfo);
4908 : 20786891 : tree as_base = CLASSTYPE_AS_BASE (basetype);
4909 : 20786891 : gcc_assert (as_base);
4910 : 20786891 : tree decl = build_decl (input_location, FIELD_DECL, NULL_TREE, as_base);
4911 : :
4912 : 20786891 : DECL_ARTIFICIAL (decl) = 1;
4913 : 20786891 : DECL_IGNORED_P (decl) = 1;
4914 : 20786891 : DECL_FIELD_CONTEXT (decl) = t;
4915 : 20786891 : if (is_empty_class (basetype))
4916 : : /* CLASSTYPE_SIZE is one byte, but the field needs to have size zero. */
4917 : 18372265 : DECL_SIZE (decl) = DECL_SIZE_UNIT (decl) = size_zero_node;
4918 : : else
4919 : : {
4920 : 2414626 : DECL_SIZE (decl) = CLASSTYPE_SIZE (basetype);
4921 : 2414626 : DECL_SIZE_UNIT (decl) = CLASSTYPE_SIZE_UNIT (basetype);
4922 : : }
4923 : 20786891 : SET_DECL_ALIGN (decl, CLASSTYPE_ALIGN (basetype));
4924 : 20786891 : DECL_USER_ALIGN (decl) = CLASSTYPE_USER_ALIGN (basetype);
4925 : 20786891 : SET_DECL_MODE (decl, TYPE_MODE (basetype));
4926 : 20786891 : DECL_FIELD_IS_BASE (decl) = 1;
4927 : :
4928 : 20786891 : if (access == access_private_node)
4929 : 436422 : TREE_PRIVATE (decl) = true;
4930 : 20350469 : else if (access == access_protected_node)
4931 : 70176 : TREE_PROTECTED (decl) = true;
4932 : :
4933 : : /* Add the new FIELD_DECL to the list of fields for T. */
4934 : 20786891 : DECL_CHAIN (decl) = *next_field;
4935 : 20786891 : *next_field = decl;
4936 : 20786891 : next_field = &DECL_CHAIN (decl);
4937 : :
4938 : 20786891 : return decl;
4939 : : }
4940 : :
4941 : : /* Layout the base given by BINFO in the class indicated by RLI.
4942 : : *BASE_ALIGN is a running maximum of the alignments of
4943 : : any base class. OFFSETS gives the location of empty base
4944 : : subobjects. T is the most derived type. Return nonzero if the new
4945 : : object cannot be nearly-empty. A new FIELD_DECL is inserted at
4946 : : *NEXT_FIELD, unless BINFO is for an empty base class.
4947 : :
4948 : : Returns the location at which the next field should be inserted. */
4949 : :
4950 : : static tree *
4951 : 20862257 : build_base_field (record_layout_info rli, tree binfo, tree access,
4952 : : splay_tree offsets, tree *next_field)
4953 : : {
4954 : 20862257 : tree t = rli->t;
4955 : 20862257 : tree basetype = BINFO_TYPE (binfo);
4956 : :
4957 : 20862257 : if (!COMPLETE_TYPE_P (basetype))
4958 : : /* This error is now reported in xref_tag, thus giving better
4959 : : location information. */
4960 : 0 : return next_field;
4961 : :
4962 : : /* Place the base class. */
4963 : 20862257 : if (!is_empty_class (basetype))
4964 : : {
4965 : 2414626 : tree decl;
4966 : :
4967 : : /* The containing class is non-empty because it has a non-empty
4968 : : base class. */
4969 : 2414626 : CLASSTYPE_EMPTY_P (t) = 0;
4970 : :
4971 : : /* Create the FIELD_DECL. */
4972 : 2414626 : decl = build_base_field_1 (t, binfo, access, next_field);
4973 : :
4974 : : /* Try to place the field. It may take more than one try if we
4975 : : have a hard time placing the field without putting two
4976 : : objects of the same type at the same address. */
4977 : 2414626 : layout_nonempty_base_or_field (rli, decl, binfo, offsets);
4978 : : }
4979 : : else
4980 : : {
4981 : 18447631 : bool atend = layout_empty_base_or_field (rli, binfo, offsets);
4982 : : /* A nearly-empty class "has no proper base class that is empty,
4983 : : not morally virtual, and at an offset other than zero." */
4984 : 18447631 : if (!BINFO_VIRTUAL_P (binfo) && CLASSTYPE_NEARLY_EMPTY_P (t))
4985 : : {
4986 : 424 : if (atend)
4987 : 94 : CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
4988 : : /* The check above (used in G++ 3.2) is insufficient because
4989 : : an empty class placed at offset zero might itself have an
4990 : : empty base at a nonzero offset. */
4991 : 330 : else if (walk_subobject_offsets (basetype,
4992 : : empty_base_at_nonzero_offset_p,
4993 : : size_zero_node,
4994 : : /*offsets=*/NULL,
4995 : : /*max_offset=*/NULL_TREE,
4996 : : /*vbases_p=*/true))
4997 : 6 : CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
4998 : : }
4999 : :
5000 : : /* We used to not create a FIELD_DECL for empty base classes because of
5001 : : back end issues with overlapping FIELD_DECLs, but that doesn't seem to
5002 : : be a problem anymore. We need them to handle initialization of C++17
5003 : : aggregate bases. */
5004 : 36821550 : if (cxx_dialect >= cxx17 && !BINFO_VIRTUAL_P (binfo))
5005 : : {
5006 : 18372265 : tree decl = build_base_field_1 (t, binfo, access, next_field);
5007 : 18372265 : DECL_FIELD_OFFSET (decl) = BINFO_OFFSET (binfo);
5008 : 18372265 : DECL_FIELD_BIT_OFFSET (decl) = bitsize_zero_node;
5009 : 18372265 : SET_DECL_OFFSET_ALIGN (decl, BITS_PER_UNIT);
5010 : 18372265 : SET_DECL_FIELD_ABI_IGNORED (decl, 1);
5011 : : }
5012 : :
5013 : : /* An empty virtual base causes a class to be non-empty
5014 : : -- but in that case we do not need to clear CLASSTYPE_EMPTY_P
5015 : : here because that was already done when the virtual table
5016 : : pointer was created. */
5017 : : }
5018 : :
5019 : : /* Record the offsets of BINFO and its base subobjects. */
5020 : 20862257 : record_subobject_offsets (binfo, offsets);
5021 : :
5022 : 20862257 : return next_field;
5023 : : }
5024 : :
5025 : : /* Layout all of the non-virtual base classes. Record empty
5026 : : subobjects in OFFSETS. T is the most derived type. Return nonzero
5027 : : if the type cannot be nearly empty. The fields created
5028 : : corresponding to the base classes will be inserted at
5029 : : *NEXT_FIELD. */
5030 : :
5031 : : static void
5032 : 39307783 : build_base_fields (record_layout_info rli,
5033 : : splay_tree offsets, tree *next_field)
5034 : : {
5035 : : /* Chain to hold all the new FIELD_DECLs which stand in for base class
5036 : : subobjects. */
5037 : 39307783 : tree t = rli->t;
5038 : 39307783 : tree binfo = TYPE_BINFO (t);
5039 : 39307783 : int n_baseclasses = BINFO_N_BASE_BINFOS (binfo);
5040 : :
5041 : : /* The primary base class is always allocated first. */
5042 : 39307783 : const tree primary_binfo = CLASSTYPE_PRIMARY_BINFO (t);
5043 : 39307783 : if (primary_binfo)
5044 : : {
5045 : : /* We need to walk BINFO_BASE_BINFO to find the access of the primary
5046 : : base, if it is direct. Indirect base fields are private. */
5047 : 1442415 : tree primary_access = access_private_node;
5048 : 1443291 : for (int i = 0; i < n_baseclasses; ++i)
5049 : : {
5050 : 1443222 : tree base_binfo = BINFO_BASE_BINFO (binfo, i);
5051 : 1443222 : if (base_binfo == primary_binfo)
5052 : : {
5053 : 1442346 : primary_access = BINFO_BASE_ACCESS (binfo, i);
5054 : 1442346 : break;
5055 : : }
5056 : : }
5057 : 1442415 : next_field = build_base_field (rli, primary_binfo,
5058 : : primary_access,
5059 : : offsets, next_field);
5060 : : }
5061 : :
5062 : : /* Now allocate the rest of the bases. */
5063 : 60021209 : for (int i = 0; i < n_baseclasses; ++i)
5064 : : {
5065 : 20713426 : tree base_binfo = BINFO_BASE_BINFO (binfo, i);
5066 : :
5067 : : /* The primary base was already allocated above, so we don't
5068 : : need to allocate it again here. */
5069 : 20713426 : if (base_binfo == primary_binfo)
5070 : 1442346 : continue;
5071 : :
5072 : : /* Virtual bases are added at the end (a primary virtual base
5073 : : will have already been added). */
5074 : 19271080 : if (BINFO_VIRTUAL_P (base_binfo))
5075 : 46884 : continue;
5076 : :
5077 : 19224196 : next_field = build_base_field (rli, base_binfo,
5078 : 19224196 : BINFO_BASE_ACCESS (binfo, i),
5079 : : offsets, next_field);
5080 : : }
5081 : 39307783 : }
5082 : :
5083 : : /* Go through the TYPE_FIELDS of T issuing any appropriate
5084 : : diagnostics, figuring out which methods override which other
5085 : : methods, and so forth. */
5086 : :
5087 : : static void
5088 : 39307783 : check_methods (tree t)
5089 : : {
5090 : 269985150 : for (tree x = TYPE_FIELDS (t); x; x = DECL_CHAIN (x))
5091 : 230677367 : if (DECL_DECLARES_FUNCTION_P (x))
5092 : : {
5093 : 117385421 : check_for_override (x, t);
5094 : :
5095 : 117385421 : if (DECL_PURE_VIRTUAL_P (x)
5096 : 117385421 : && (TREE_CODE (x) != FUNCTION_DECL || ! DECL_VINDEX (x)))
5097 : 3 : error ("initializer specified for non-virtual method %q+D", x);
5098 : : /* The name of the field is the original field name
5099 : : Save this in auxiliary field for later overloading. */
5100 : 117385421 : if (TREE_CODE (x) == FUNCTION_DECL && DECL_VINDEX (x))
5101 : : {
5102 : 5955619 : TYPE_POLYMORPHIC_P (t) = 1;
5103 : 5955619 : if (DECL_PURE_VIRTUAL_P (x))
5104 : 643189 : vec_safe_push (CLASSTYPE_PURE_VIRTUALS (t), x);
5105 : : }
5106 : :
5107 : 117385421 : if (!DECL_VIRTUAL_P (x)
5108 : 228815239 : && lookup_attribute ("transaction_safe_dynamic",
5109 : 111429818 : DECL_ATTRIBUTES (x)))
5110 : 2 : error_at (DECL_SOURCE_LOCATION (x),
5111 : : "%<transaction_safe_dynamic%> may only be specified for "
5112 : : "a virtual function");
5113 : : }
5114 : :
5115 : : /* Check whether the eligible special member functions (P0848) are
5116 : : user-provided. add_method arranged that the CLASSTYPE_MEMBER_VEC only
5117 : : has the eligible ones, unless none are eligible; TYPE_FIELDS also contains
5118 : : ineligible overloads, which is why this needs to be separate from the loop
5119 : : above. */
5120 : :
5121 : 39307783 : if (tree dtor = CLASSTYPE_DESTRUCTOR (t))
5122 : : {
5123 : 3628699 : if (TREE_CODE (dtor) == OVERLOAD)
5124 : : {
5125 : : /* P0848: At the end of the definition of a class, overload
5126 : : resolution is performed among the prospective destructors declared
5127 : : in that class with an empty argument list to select the destructor
5128 : : for the class, also known as the selected destructor. The program
5129 : : is ill-formed if overload resolution fails. */
5130 : 6 : int viable = 0;
5131 : 24 : for (tree fn : ovl_range (dtor))
5132 : 12 : if (constraints_satisfied_p (fn))
5133 : 6 : ++viable;
5134 : 6 : gcc_checking_assert (viable != 1);
5135 : :
5136 : 6 : auto_diagnostic_group d;
5137 : 6 : if (viable == 0)
5138 : 3 : error_at (location_of (t), "no viable destructor for %qT", t);
5139 : : else
5140 : 3 : error_at (location_of (t), "destructor for %qT is ambiguous", t);
5141 : 6 : print_candidates (dtor);
5142 : :
5143 : : /* Arbitrarily prune the overload set to a single function for
5144 : : sake of error recovery. */
5145 : 6 : tree *slot = find_member_slot (t, dtor_identifier);
5146 : 6 : *slot = get_first_fn (dtor);
5147 : 6 : }
5148 : 3628693 : else if (user_provided_p (dtor))
5149 : 2845911 : TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t) = true;
5150 : : }
5151 : :
5152 : 82914983 : for (tree fn : ovl_range (CLASSTYPE_CONSTRUCTORS (t)))
5153 : : {
5154 : 24752837 : if (!user_provided_p (fn))
5155 : : /* Might be trivial. */;
5156 : 19879463 : else if (TREE_CODE (fn) == TEMPLATE_DECL)
5157 : : /* Templates are never special members. */;
5158 : 12539154 : else if (copy_fn_p (fn)
5159 : 12539154 : && constraints_satisfied_p (fn))
5160 : 942498 : TYPE_HAS_COMPLEX_COPY_CTOR (t) = true;
5161 : 11596656 : else if (move_fn_p (fn)
5162 : 11596656 : && constraints_satisfied_p (fn))
5163 : 861611 : TYPE_HAS_COMPLEX_MOVE_CTOR (t) = true;
5164 : : }
5165 : :
5166 : 50610837 : for (tree fn : ovl_range (get_class_binding_direct (t, assign_op_identifier)))
5167 : : {
5168 : 7222724 : if (!user_provided_p (fn))
5169 : : /* Might be trivial. */;
5170 : 3833052 : else if (TREE_CODE (fn) == TEMPLATE_DECL)
5171 : : /* Templates are never special members. */;
5172 : 2797427 : else if (copy_fn_p (fn)
5173 : 2797427 : && constraints_satisfied_p (fn))
5174 : 603602 : TYPE_HAS_COMPLEX_COPY_ASSIGN (t) = true;
5175 : 2193825 : else if (move_fn_p (fn)
5176 : 2193825 : && constraints_satisfied_p (fn))
5177 : 811296 : TYPE_HAS_COMPLEX_MOVE_ASSIGN (t) = true;
5178 : : }
5179 : 39307783 : }
5180 : :
5181 : : /* FN is constructor, destructor or operator function. Clone the
5182 : : declaration to create a NAME'd variant. NEED_VTT_PARM_P and
5183 : : OMIT_INHERITED_PARMS_P are relevant if it's a cdtor. */
5184 : :
5185 : : static tree
5186 : 105652409 : copy_fndecl_with_name (tree fn, tree name, tree_code code,
5187 : : bool need_vtt_parm_p, bool omit_inherited_parms_p)
5188 : : {
5189 : : /* Copy the function. */
5190 : 105652409 : tree clone = copy_decl (fn);
5191 : : /* Reset the function name. */
5192 : 105652409 : DECL_NAME (clone) = name;
5193 : :
5194 : 105652409 : if (flag_concepts)
5195 : : /* Clone constraints. */
5196 : 39954513 : if (tree ci = get_constraints (fn))
5197 : 5060044 : set_constraints (clone, copy_node (ci));
5198 : :
5199 : 105652409 : SET_DECL_ASSEMBLER_NAME (clone, NULL_TREE);
5200 : 105652409 : DECL_ABSTRACT_P (clone) = false;
5201 : : /* There's no pending inline data for this function. */
5202 : 105652409 : DECL_PENDING_INLINE_INFO (clone) = NULL;
5203 : 105652409 : DECL_PENDING_INLINE_P (clone) = 0;
5204 : :
5205 : 105652409 : if (name == base_dtor_identifier)
5206 : : {
5207 : : /* The base-class destructor is not virtual. */
5208 : 8744787 : DECL_VIRTUAL_P (clone) = 0;
5209 : 8744787 : DECL_VINDEX (clone) = NULL_TREE;
5210 : : }
5211 : 96907622 : else if (code != ERROR_MARK)
5212 : : {
5213 : : /* Set the operator code. */
5214 : 246 : const ovl_op_info_t *ovl_op = OVL_OP_INFO (false, code);
5215 : 246 : DECL_OVERLOADED_OPERATOR_CODE_RAW (clone) = ovl_op->ovl_op_code;
5216 : :
5217 : : /* The operator could be virtual. */
5218 : 246 : if (DECL_VIRTUAL_P (clone))
5219 : 12 : IDENTIFIER_VIRTUAL_P (name) = true;
5220 : : }
5221 : :
5222 : 105652409 : if (omit_inherited_parms_p)
5223 : 120 : gcc_assert (DECL_HAS_IN_CHARGE_PARM_P (clone));
5224 : :
5225 : : /* If there was an in-charge parameter, drop it from the function
5226 : : type. */
5227 : 105652409 : if (DECL_HAS_IN_CHARGE_PARM_P (clone))
5228 : : {
5229 : 2397024 : tree basetype = TYPE_METHOD_BASETYPE (TREE_TYPE (clone));
5230 : 2397024 : tree parmtypes = TYPE_ARG_TYPES (TREE_TYPE (clone));
5231 : : /* Skip the `this' parameter. */
5232 : 2397024 : parmtypes = TREE_CHAIN (parmtypes);
5233 : : /* Skip the in-charge parameter. */
5234 : 2397024 : parmtypes = TREE_CHAIN (parmtypes);
5235 : : /* And the VTT parm, in a complete [cd]tor. */
5236 : 2397024 : if (DECL_HAS_VTT_PARM_P (fn) && !need_vtt_parm_p)
5237 : 1291556 : parmtypes = TREE_CHAIN (parmtypes);
5238 : 2397024 : if (omit_inherited_parms_p)
5239 : : {
5240 : : /* If we're omitting inherited parms, that just leaves the VTT. */
5241 : 120 : gcc_assert (need_vtt_parm_p);
5242 : 120 : parmtypes = tree_cons (NULL_TREE, vtt_parm_type, void_list_node);
5243 : : }
5244 : 2397024 : TREE_TYPE (clone)
5245 : 2397024 : = build_method_type_directly (basetype,
5246 : 2397024 : TREE_TYPE (TREE_TYPE (clone)),
5247 : : parmtypes);
5248 : 2397024 : TREE_TYPE (clone)
5249 : 2397024 : = cp_build_type_attribute_variant (TREE_TYPE (clone),
5250 : 2397024 : TYPE_ATTRIBUTES (TREE_TYPE (fn)));
5251 : 2397024 : TREE_TYPE (clone)
5252 : 4794048 : = cxx_copy_lang_qualifiers (TREE_TYPE (clone), TREE_TYPE (fn));
5253 : : }
5254 : :
5255 : : /* Copy the function parameters. */
5256 : 105652409 : DECL_ARGUMENTS (clone) = copy_list (DECL_ARGUMENTS (clone));
5257 : :
5258 : : /* Remove the in-charge parameter. */
5259 : 105652409 : if (DECL_HAS_IN_CHARGE_PARM_P (clone))
5260 : : {
5261 : 2397024 : DECL_CHAIN (DECL_ARGUMENTS (clone))
5262 : 2397024 : = DECL_CHAIN (DECL_CHAIN (DECL_ARGUMENTS (clone)));
5263 : 2397024 : DECL_HAS_IN_CHARGE_PARM_P (clone) = 0;
5264 : : }
5265 : :
5266 : : /* And the VTT parm, in a complete [cd]tor. */
5267 : 105652409 : if (DECL_HAS_VTT_PARM_P (fn))
5268 : : {
5269 : 2397024 : if (need_vtt_parm_p)
5270 : 1105468 : DECL_HAS_VTT_PARM_P (clone) = 1;
5271 : : else
5272 : : {
5273 : 2583112 : DECL_CHAIN (DECL_ARGUMENTS (clone))
5274 : 1291556 : = DECL_CHAIN (DECL_CHAIN (DECL_ARGUMENTS (clone)));
5275 : 1291556 : DECL_HAS_VTT_PARM_P (clone) = 0;
5276 : : }
5277 : : }
5278 : :
5279 : : /* A base constructor inheriting from a virtual base doesn't get the
5280 : : arguments. */
5281 : 105652409 : if (omit_inherited_parms_p)
5282 : 120 : DECL_CHAIN (DECL_CHAIN (DECL_ARGUMENTS (clone))) = NULL_TREE;
5283 : :
5284 : 329565323 : for (tree parms = DECL_ARGUMENTS (clone); parms; parms = DECL_CHAIN (parms))
5285 : : {
5286 : 223912914 : DECL_CONTEXT (parms) = clone;
5287 : 223912914 : cxx_dup_lang_specific_decl (parms);
5288 : : }
5289 : :
5290 : : /* Create the RTL for this function. */
5291 : 105652409 : SET_DECL_RTL (clone, NULL);
5292 : :
5293 : : /* Regardless of the current scope, this is a member function, so
5294 : : not at namespace scope. */
5295 : 105652409 : rest_of_decl_compilation (clone, /*top_level=*/0, at_eof);
5296 : :
5297 : 105652409 : return clone;
5298 : : }
5299 : :
5300 : : /* FN is an operator function, create a variant for CODE. */
5301 : :
5302 : : tree
5303 : 246 : copy_operator_fn (tree fn, tree_code code)
5304 : : {
5305 : 246 : return copy_fndecl_with_name (fn, ovl_op_identifier (code),
5306 : 246 : code, false, false);
5307 : : }
5308 : :
5309 : : /* FN is a constructor or destructor. Clone the declaration to create
5310 : : a specialized in-charge or not-in-charge version, as indicated by
5311 : : NAME. */
5312 : :
5313 : : static tree
5314 : 121232681 : build_clone (tree fn, tree name, bool need_vtt_parm_p,
5315 : : bool omit_inherited_parms_p)
5316 : : {
5317 : 121232681 : tree clone;
5318 : :
5319 : : /* If this is a template, do the rest on the DECL_TEMPLATE_RESULT. */
5320 : 121232681 : if (TREE_CODE (fn) == TEMPLATE_DECL)
5321 : : {
5322 : 15580518 : clone = copy_decl (fn);
5323 : 15580518 : DECL_NAME (clone) = name;
5324 : :
5325 : 15580518 : tree result = build_clone (DECL_TEMPLATE_RESULT (clone), name,
5326 : : need_vtt_parm_p, omit_inherited_parms_p);
5327 : 15580518 : DECL_TEMPLATE_RESULT (clone) = result;
5328 : :
5329 : 15580518 : DECL_TEMPLATE_INFO (result) = copy_node (DECL_TEMPLATE_INFO (result));
5330 : 15580518 : DECL_TI_TEMPLATE (result) = clone;
5331 : :
5332 : 15580518 : TREE_TYPE (clone) = TREE_TYPE (result);
5333 : : }
5334 : : else
5335 : : {
5336 : 105652163 : clone = copy_fndecl_with_name (fn, name, ERROR_MARK,
5337 : : need_vtt_parm_p, omit_inherited_parms_p);
5338 : 105652163 : DECL_CLONED_FUNCTION (clone) = fn;
5339 : :
5340 : 105652163 : maybe_prepare_return_this (clone);
5341 : : }
5342 : :
5343 : : /* Remember where this function came from. */
5344 : 121232681 : DECL_ABSTRACT_ORIGIN (clone) = fn;
5345 : :
5346 : : /* Make it easy to find the CLONE given the FN. Note the
5347 : : template_result of a template will be chained this way too. */
5348 : 121232681 : DECL_CHAIN (clone) = DECL_CHAIN (fn);
5349 : 121232681 : DECL_CHAIN (fn) = clone;
5350 : :
5351 : 121232681 : return clone;
5352 : : }
5353 : :
5354 : : /* Build the clones of FN, return the number of clones built. These
5355 : : will be inserted onto DECL_CHAIN of FN. */
5356 : :
5357 : : void
5358 : 52017370 : build_cdtor_clones (tree fn, bool needs_vtt_p, bool base_omits_inherited_p,
5359 : : bool update_methods)
5360 : : {
5361 : 52017370 : unsigned count = 0;
5362 : :
5363 : 52017370 : if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (fn))
5364 : : {
5365 : : /* For each constructor, we need two variants: an in-charge version
5366 : : and a not-in-charge version. */
5367 : 43272583 : build_clone (fn, complete_ctor_identifier, false, false);
5368 : 43272583 : build_clone (fn, base_ctor_identifier, needs_vtt_p,
5369 : : base_omits_inherited_p);
5370 : 43272583 : count += 2;
5371 : : }
5372 : : else
5373 : : {
5374 : 8744787 : gcc_assert (DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (fn));
5375 : :
5376 : : /* For each destructor, we need three variants: an in-charge
5377 : : version, a not-in-charge version, and an in-charge deleting
5378 : : version. We clone the deleting version first because that
5379 : : means it will go second on the TYPE_FIELDS list -- and that
5380 : : corresponds to the correct layout order in the virtual
5381 : : function table.
5382 : :
5383 : : For a non-virtual destructor, we do not build a deleting
5384 : : destructor. */
5385 : 8744787 : if (DECL_VIRTUAL_P (fn))
5386 : : {
5387 : 1617423 : build_clone (fn, deleting_dtor_identifier, false, false);
5388 : 1617423 : count++;
5389 : : }
5390 : 8744787 : build_clone (fn, complete_dtor_identifier, false, false);
5391 : 8744787 : build_clone (fn, base_dtor_identifier, needs_vtt_p, false);
5392 : 8744787 : count += 2;
5393 : : }
5394 : :
5395 : : /* The original is now an abstract function that is never
5396 : : emitted. */
5397 : 52017370 : DECL_ABSTRACT_P (fn) = true;
5398 : :
5399 : 52017370 : if (update_methods)
5400 : 131849700 : for (tree clone = fn; count--;)
5401 : : {
5402 : 88438656 : clone = DECL_CHAIN (clone);
5403 : 88438656 : add_method (DECL_CONTEXT (clone), clone, false);
5404 : : }
5405 : 52017370 : }
5406 : :
5407 : : /* Produce declarations for all appropriate clones of FN. If
5408 : : UPDATE_METHODS is true, the clones are added to the
5409 : : CLASSTYPE_MEMBER_VEC. */
5410 : :
5411 : : void
5412 : 52269026 : clone_cdtor (tree fn, bool update_methods)
5413 : : {
5414 : : /* Avoid inappropriate cloning. */
5415 : 52269026 : if (DECL_CHAIN (fn)
5416 : 52269026 : && DECL_CLONED_FUNCTION_P (DECL_CHAIN (fn)))
5417 : : return;
5418 : :
5419 : : /* Base cdtors need a vtt parm if there are virtual bases. */
5420 : 51994444 : bool vtt = CLASSTYPE_VBASECLASSES (DECL_CONTEXT (fn));
5421 : :
5422 : : /* Base ctor omits inherited parms it needs a vttparm and inherited
5423 : : from a virtual nase ctor. */
5424 : 51994444 : bool base_omits_inherited = (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (fn)
5425 : 51994444 : && base_ctor_omit_inherited_parms (fn));
5426 : :
5427 : 51994444 : build_cdtor_clones (fn, vtt, base_omits_inherited, update_methods);
5428 : : }
5429 : :
5430 : : /* DECL is an in charge constructor, which is being defined. This will
5431 : : have had an in class declaration, from whence clones were
5432 : : declared. An out-of-class definition can specify additional default
5433 : : arguments. As it is the clones that are involved in overload
5434 : : resolution, we must propagate the information from the DECL to its
5435 : : clones. */
5436 : :
5437 : : void
5438 : 18616988 : adjust_clone_args (tree decl)
5439 : : {
5440 : 18616988 : tree clone;
5441 : :
5442 : 29656482 : for (clone = DECL_CHAIN (decl); clone && DECL_CLONED_FUNCTION_P (clone);
5443 : 11039494 : clone = DECL_CHAIN (clone))
5444 : : {
5445 : 11039494 : tree orig_clone_parms = TYPE_ARG_TYPES (TREE_TYPE (clone));
5446 : 11039494 : tree orig_decl_parms = TYPE_ARG_TYPES (TREE_TYPE (decl));
5447 : 11039494 : tree decl_parms, clone_parms;
5448 : :
5449 : : /* Skip the 'this' parameter. */
5450 : 11039494 : orig_clone_parms = TREE_CHAIN (orig_clone_parms);
5451 : 11039494 : orig_decl_parms = TREE_CHAIN (orig_decl_parms);
5452 : :
5453 : 11039494 : if (DECL_HAS_IN_CHARGE_PARM_P (decl))
5454 : 22530 : orig_decl_parms = TREE_CHAIN (orig_decl_parms);
5455 : 11039494 : if (DECL_HAS_VTT_PARM_P (decl))
5456 : 22530 : orig_decl_parms = TREE_CHAIN (orig_decl_parms);
5457 : :
5458 : 11039494 : clone_parms = orig_clone_parms;
5459 : 11039494 : if (DECL_HAS_VTT_PARM_P (clone))
5460 : 11265 : clone_parms = TREE_CHAIN (clone_parms);
5461 : :
5462 : 21930614 : for (decl_parms = orig_decl_parms; decl_parms;
5463 : 10891120 : decl_parms = TREE_CHAIN (decl_parms),
5464 : 10891120 : clone_parms = TREE_CHAIN (clone_parms))
5465 : : {
5466 : 21928974 : if (clone_parms == void_list_node)
5467 : : {
5468 : 11037830 : gcc_assert (decl_parms == clone_parms
5469 : : || ctor_omit_inherited_parms (clone));
5470 : : break;
5471 : : }
5472 : :
5473 : 10891144 : gcc_checking_assert (same_type_p (TREE_VALUE (decl_parms),
5474 : : TREE_VALUE (clone_parms)));
5475 : :
5476 : 11848372 : if (TREE_PURPOSE (decl_parms) && !TREE_PURPOSE (clone_parms))
5477 : : {
5478 : : /* A default parameter has been added. Adjust the
5479 : : clone's parameters. */
5480 : 24 : clone_parms = orig_decl_parms;
5481 : :
5482 : 24 : if (DECL_HAS_VTT_PARM_P (clone))
5483 : : {
5484 : 3 : clone_parms = tree_cons (TREE_PURPOSE (orig_clone_parms),
5485 : 3 : TREE_VALUE (orig_clone_parms),
5486 : : clone_parms);
5487 : 3 : TREE_TYPE (clone_parms) = TREE_TYPE (orig_clone_parms);
5488 : : }
5489 : :
5490 : 24 : tree basetype = TYPE_METHOD_BASETYPE (TREE_TYPE (clone));
5491 : 24 : tree type
5492 : 24 : = build_method_type_directly (basetype,
5493 : 24 : TREE_TYPE (TREE_TYPE (clone)),
5494 : : clone_parms);
5495 : 24 : if (tree attrs = TYPE_ATTRIBUTES (TREE_TYPE (clone)))
5496 : 0 : type = cp_build_type_attribute_variant (type, attrs);
5497 : 24 : type = cxx_copy_lang_qualifiers (type, TREE_TYPE (clone));
5498 : 24 : TREE_TYPE (clone) = type;
5499 : :
5500 : 24 : clone_parms = NULL_TREE;
5501 : 24 : break;
5502 : : }
5503 : : }
5504 : 11039494 : gcc_assert (!clone_parms || clone_parms == void_list_node);
5505 : : }
5506 : 18616988 : }
5507 : :
5508 : : /* For each of the constructors and destructors in T, create an
5509 : : in-charge and not-in-charge variant. */
5510 : :
5511 : : static void
5512 : 39307783 : clone_constructors_and_destructors (tree t)
5513 : : {
5514 : : /* We do not need to propagate the usingness to the clone, at this
5515 : : point that is not needed. */
5516 : 83511054 : for (tree fn : ovl_range (CLASSTYPE_CONSTRUCTORS (t)))
5517 : 25050877 : clone_cdtor (fn, /*update_methods=*/true);
5518 : :
5519 : 39307783 : if (tree dtor = CLASSTYPE_DESTRUCTOR (t))
5520 : 3801045 : clone_cdtor (dtor, /*update_methods=*/true);
5521 : 39307783 : }
5522 : :
5523 : : /* Deduce noexcept for a destructor DTOR. */
5524 : :
5525 : : void
5526 : 3695606 : deduce_noexcept_on_destructor (tree dtor)
5527 : : {
5528 : 3695606 : if (!TYPE_RAISES_EXCEPTIONS (TREE_TYPE (dtor)))
5529 : 2407100 : TREE_TYPE (dtor) = build_exception_variant (TREE_TYPE (dtor),
5530 : : noexcept_deferred_spec);
5531 : 3695606 : }
5532 : :
5533 : : /* Subroutine of set_one_vmethod_tm_attributes. Search base classes
5534 : : of TYPE for virtual functions which FNDECL overrides. Return a
5535 : : mask of the tm attributes found therein. */
5536 : :
5537 : : static int
5538 : 925 : look_for_tm_attr_overrides (tree type, tree fndecl)
5539 : : {
5540 : 925 : tree binfo = TYPE_BINFO (type);
5541 : 925 : tree base_binfo;
5542 : 925 : int ix, found = 0;
5543 : :
5544 : 1549 : for (ix = 0; BINFO_BASE_ITERATE (binfo, ix, base_binfo); ++ix)
5545 : : {
5546 : 624 : tree o, basetype = BINFO_TYPE (base_binfo);
5547 : :
5548 : 624 : if (!TYPE_POLYMORPHIC_P (basetype))
5549 : 48 : continue;
5550 : :
5551 : 576 : o = look_for_overrides_here (basetype, fndecl);
5552 : 576 : if (o)
5553 : : {
5554 : 411 : if (lookup_attribute ("transaction_safe_dynamic",
5555 : 411 : DECL_ATTRIBUTES (o)))
5556 : : /* transaction_safe_dynamic is not inherited. */;
5557 : : else
5558 : 311 : found |= tm_attr_to_mask (find_tm_attribute
5559 : 311 : (TYPE_ATTRIBUTES (TREE_TYPE (o))));
5560 : : }
5561 : : else
5562 : 165 : found |= look_for_tm_attr_overrides (basetype, fndecl);
5563 : : }
5564 : :
5565 : 925 : return found;
5566 : : }
5567 : :
5568 : : /* Subroutine of set_method_tm_attributes. Handle the checks and
5569 : : inheritance for one virtual method FNDECL. */
5570 : :
5571 : : static void
5572 : 760 : set_one_vmethod_tm_attributes (tree type, tree fndecl)
5573 : : {
5574 : 760 : tree tm_attr;
5575 : 760 : int found, have;
5576 : :
5577 : 760 : found = look_for_tm_attr_overrides (type, fndecl);
5578 : :
5579 : : /* If FNDECL doesn't actually override anything (i.e. T is the
5580 : : class that first declares FNDECL virtual), then we're done. */
5581 : 760 : if (found == 0)
5582 : : return;
5583 : :
5584 : 45 : tm_attr = find_tm_attribute (TYPE_ATTRIBUTES (TREE_TYPE (fndecl)));
5585 : 45 : have = tm_attr_to_mask (tm_attr);
5586 : :
5587 : : /* Intel STM Language Extension 3.0, Section 4.2 table 4:
5588 : : tm_pure must match exactly, otherwise no weakening of
5589 : : tm_safe > tm_callable > nothing. */
5590 : : /* ??? The tm_pure attribute didn't make the transition to the
5591 : : multivendor language spec. */
5592 : 45 : if (have == TM_ATTR_PURE)
5593 : : {
5594 : 0 : if (found != TM_ATTR_PURE)
5595 : : {
5596 : 0 : found &= -found;
5597 : 0 : goto err_override;
5598 : : }
5599 : : }
5600 : : /* If the overridden function is tm_pure, then FNDECL must be. */
5601 : 45 : else if (found == TM_ATTR_PURE && tm_attr)
5602 : 6 : goto err_override;
5603 : : /* Look for base class combinations that cannot be satisfied. */
5604 : 39 : else if (found != TM_ATTR_PURE && (found & TM_ATTR_PURE))
5605 : : {
5606 : 0 : found &= ~TM_ATTR_PURE;
5607 : 0 : found &= -found;
5608 : 0 : error_at (DECL_SOURCE_LOCATION (fndecl),
5609 : : "method overrides both %<transaction_pure%> and %qE methods",
5610 : : tm_mask_to_attr (found));
5611 : : }
5612 : : /* If FNDECL did not declare an attribute, then inherit the most
5613 : : restrictive one. */
5614 : 39 : else if (tm_attr == NULL)
5615 : : {
5616 : 14 : apply_tm_attr (fndecl, tm_mask_to_attr (least_bit_hwi (found)));
5617 : : }
5618 : : /* Otherwise validate that we're not weaker than a function
5619 : : that is being overridden. */
5620 : : else
5621 : : {
5622 : 25 : found &= -found;
5623 : 25 : if (found <= TM_ATTR_CALLABLE && have > found)
5624 : 9 : goto err_override;
5625 : : }
5626 : : return;
5627 : :
5628 : 15 : err_override:
5629 : 15 : error_at (DECL_SOURCE_LOCATION (fndecl),
5630 : : "method declared %qE overriding %qE method",
5631 : : tm_attr, tm_mask_to_attr (found));
5632 : : }
5633 : :
5634 : : /* For each of the methods in T, propagate a class-level tm attribute. */
5635 : :
5636 : : static void
5637 : 39307783 : set_method_tm_attributes (tree t)
5638 : : {
5639 : 39307783 : tree class_tm_attr, fndecl;
5640 : :
5641 : : /* Don't bother collecting tm attributes if transactional memory
5642 : : support is not enabled. */
5643 : 39307783 : if (!flag_tm)
5644 : : return;
5645 : :
5646 : : /* Process virtual methods first, as they inherit directly from the
5647 : : base virtual function and also require validation of new attributes. */
5648 : 3690 : if (TYPE_CONTAINS_VPTR_P (t))
5649 : : {
5650 : 200 : tree vchain;
5651 : 960 : for (vchain = BINFO_VIRTUALS (TYPE_BINFO (t)); vchain;
5652 : 760 : vchain = TREE_CHAIN (vchain))
5653 : : {
5654 : 760 : fndecl = BV_FN (vchain);
5655 : 760 : if (DECL_THUNK_P (fndecl))
5656 : 3 : fndecl = THUNK_TARGET (fndecl);
5657 : 760 : set_one_vmethod_tm_attributes (t, fndecl);
5658 : : }
5659 : : }
5660 : :
5661 : : /* If the class doesn't have an attribute, nothing more to do. */
5662 : 3690 : class_tm_attr = find_tm_attribute (TYPE_ATTRIBUTES (t));
5663 : 3690 : if (class_tm_attr == NULL)
5664 : : return;
5665 : :
5666 : : /* Any method that does not yet have a tm attribute inherits
5667 : : the one from the class. */
5668 : 108 : for (fndecl = TYPE_FIELDS (t); fndecl; fndecl = DECL_CHAIN (fndecl))
5669 : 45 : if (DECL_DECLARES_FUNCTION_P (fndecl)
5670 : 81 : && !find_tm_attribute (TYPE_ATTRIBUTES (TREE_TYPE (fndecl))))
5671 : 21 : apply_tm_attr (fndecl, class_tm_attr);
5672 : : }
5673 : :
5674 : : /* Returns true if FN is a default constructor. */
5675 : :
5676 : : bool
5677 : 17185677 : default_ctor_p (const_tree fn)
5678 : : {
5679 : 17185677 : return (DECL_CONSTRUCTOR_P (fn)
5680 : 17185677 : && sufficient_parms_p (FUNCTION_FIRST_USER_PARMTYPE (fn)));
5681 : : }
5682 : :
5683 : : /* Returns true iff class T has a user-provided constructor that can be called
5684 : : with more than zero arguments. */
5685 : :
5686 : : bool
5687 : 55 : type_has_user_nondefault_constructor (tree t)
5688 : : {
5689 : 55 : if (!TYPE_HAS_USER_CONSTRUCTOR (t))
5690 : : return false;
5691 : :
5692 : 65 : for (tree fn : ovl_range (CLASSTYPE_CONSTRUCTORS (t)))
5693 : : {
5694 : 34 : if (user_provided_p (fn)
5695 : 34 : && (TREE_CODE (fn) == TEMPLATE_DECL
5696 : 18 : || (skip_artificial_parms_for (fn, DECL_ARGUMENTS (fn))
5697 : : != NULL_TREE)))
5698 : 3 : return true;
5699 : : }
5700 : :
5701 : 18 : return false;
5702 : : }
5703 : :
5704 : : /* Returns the defaulted constructor if T has one. Otherwise, returns
5705 : : NULL_TREE. */
5706 : :
5707 : : tree
5708 : 62 : in_class_defaulted_default_constructor (tree t)
5709 : : {
5710 : 62 : if (!TYPE_HAS_USER_CONSTRUCTOR (t))
5711 : : return NULL_TREE;
5712 : :
5713 : 123 : for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (t)); iter; ++iter)
5714 : : {
5715 : 78 : tree fn = *iter;
5716 : :
5717 : 156 : if (DECL_DEFAULTED_IN_CLASS_P (fn)
5718 : 114 : && default_ctor_p (fn))
5719 : 33 : return fn;
5720 : : }
5721 : :
5722 : 0 : return NULL_TREE;
5723 : : }
5724 : :
5725 : : /* Returns true iff FN is a user-provided function, i.e. user-declared
5726 : : and not defaulted at its first declaration. */
5727 : :
5728 : : bool
5729 : 100682808 : user_provided_p (tree fn)
5730 : : {
5731 : 100682808 : fn = STRIP_TEMPLATE (fn);
5732 : 100682808 : return (!DECL_ARTIFICIAL (fn)
5733 : 100682808 : && !(DECL_INITIALIZED_IN_CLASS_P (fn)
5734 : 84135239 : && (DECL_DEFAULTED_FN (fn) || DECL_DELETED_FN (fn))));
5735 : : }
5736 : :
5737 : : /* Returns true iff class T has a user-provided constructor. */
5738 : :
5739 : : bool
5740 : 39307921 : type_has_user_provided_constructor (tree t)
5741 : : {
5742 : 39307921 : if (!CLASS_TYPE_P (t))
5743 : : return false;
5744 : :
5745 : 39307865 : if (!TYPE_HAS_USER_CONSTRUCTOR (t))
5746 : : return false;
5747 : :
5748 : 8484761 : for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (t)); iter; ++iter)
5749 : 7028044 : if (user_provided_p (*iter))
5750 : 5571808 : return true;
5751 : :
5752 : 477554 : return false;
5753 : : }
5754 : :
5755 : : /* Returns true iff class T has a constructor that accepts a single argument
5756 : : and does not have a single parameter of type reference to T.
5757 : :
5758 : : This does not exclude explicit constructors because they are still
5759 : : considered for conversions within { } even though choosing one is
5760 : : ill-formed. */
5761 : :
5762 : : bool
5763 : 670645 : type_has_converting_constructor (tree t)
5764 : : {
5765 : 670645 : if (!CLASS_TYPE_P (t))
5766 : : return false;
5767 : :
5768 : 670645 : if (!TYPE_HAS_USER_CONSTRUCTOR (t))
5769 : : return false;
5770 : :
5771 : 1988737 : for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (t)); iter; ++iter)
5772 : : {
5773 : 1163393 : tree fn = *iter;
5774 : 1163393 : tree parm = FUNCTION_FIRST_USER_PARMTYPE (fn);
5775 : 1163393 : if (parm == NULL_TREE)
5776 : : /* Varargs. */
5777 : 338049 : return true;
5778 : 1479785 : if (parm == void_list_node
5779 : 1163392 : || !sufficient_parms_p (TREE_CHAIN (parm)))
5780 : : /* Can't accept a single argument, so won't be considered for
5781 : : conversion. */
5782 : 316393 : continue;
5783 : 846999 : if (TREE_CODE (fn) == TEMPLATE_DECL
5784 : 846999 : || TREE_CHAIN (parm) != void_list_node)
5785 : : /* Not a simple single parameter. */
5786 : : return true;
5787 : 684825 : if (TYPE_MAIN_VARIANT (non_reference (TREE_VALUE (parm)))
5788 : 684825 : != DECL_CONTEXT (fn))
5789 : : /* The single parameter has the wrong type. */
5790 : : return true;
5791 : 508951 : if (get_constraints (fn))
5792 : : /* Constrained. */
5793 : : return true;
5794 : : }
5795 : :
5796 : 112163 : return false;
5797 : : }
5798 : :
5799 : : /* Returns true iff class T has a user-provided or explicit constructor. */
5800 : :
5801 : : bool
5802 : 48518485 : type_has_user_provided_or_explicit_constructor (tree t)
5803 : : {
5804 : 48518485 : if (!CLASS_TYPE_P (t))
5805 : : return false;
5806 : :
5807 : 48518485 : if (!TYPE_HAS_USER_CONSTRUCTOR (t))
5808 : : return false;
5809 : :
5810 : 10776827 : for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (t)); iter; ++iter)
5811 : : {
5812 : 8874861 : tree fn = *iter;
5813 : 8874861 : if (user_provided_p (fn) || DECL_NONCONVERTING_P (fn))
5814 : 6973377 : return true;
5815 : : }
5816 : :
5817 : 501671 : return false;
5818 : : }
5819 : :
5820 : : /* Returns true iff class T has a non-user-provided (i.e. implicitly
5821 : : declared or explicitly defaulted in the class body) default
5822 : : constructor. */
5823 : :
5824 : : bool
5825 : 3035895 : type_has_non_user_provided_default_constructor (tree t)
5826 : : {
5827 : 3035895 : if (!TYPE_HAS_DEFAULT_CONSTRUCTOR (t))
5828 : : return false;
5829 : 3035848 : if (CLASSTYPE_LAZY_DEFAULT_CTOR (t))
5830 : : return true;
5831 : :
5832 : 21460568 : for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (t)); iter; ++iter)
5833 : : {
5834 : 11377510 : tree fn = *iter;
5835 : 11377510 : if (TREE_CODE (fn) == FUNCTION_DECL
5836 : 7877342 : && default_ctor_p (fn)
5837 : 14371857 : && !user_provided_p (fn))
5838 : 1295698 : return true;
5839 : : }
5840 : :
5841 : 1739210 : return false;
5842 : : }
5843 : :
5844 : : /* TYPE is being used as a virtual base, and has a non-trivial move
5845 : : assignment. Return true if this is due to there being a user-provided
5846 : : move assignment in TYPE or one of its subobjects; if there isn't, then
5847 : : multiple move assignment can't cause any harm. */
5848 : :
5849 : : bool
5850 : 27 : vbase_has_user_provided_move_assign (tree type)
5851 : : {
5852 : : /* Does the type itself have a user-provided move assignment operator? */
5853 : 27 : if (!CLASSTYPE_LAZY_MOVE_ASSIGN (type))
5854 : 30 : for (ovl_iterator iter (get_class_binding_direct
5855 : 27 : (type, assign_op_identifier));
5856 : 57 : iter; ++iter)
5857 : 48 : if (user_provided_p (*iter) && move_fn_p (*iter))
5858 : 18 : return true;
5859 : :
5860 : : /* Do any of its bases? */
5861 : 9 : tree binfo = TYPE_BINFO (type);
5862 : 9 : tree base_binfo;
5863 : 12 : for (int i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
5864 : 6 : if (vbase_has_user_provided_move_assign (BINFO_TYPE (base_binfo)))
5865 : : return true;
5866 : :
5867 : : /* Or non-static data members? */
5868 : 27 : for (tree field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
5869 : : {
5870 : 21 : if (TREE_CODE (field) == FIELD_DECL
5871 : 3 : && CLASS_TYPE_P (TREE_TYPE (field))
5872 : 21 : && vbase_has_user_provided_move_assign (TREE_TYPE (field)))
5873 : : return true;
5874 : : }
5875 : :
5876 : : /* Seems not. */
5877 : : return false;
5878 : : }
5879 : :
5880 : : /* If default-initialization leaves part of TYPE uninitialized, returns
5881 : : a DECL for the field or TYPE itself (DR 253). */
5882 : :
5883 : : tree
5884 : 816330 : default_init_uninitialized_part (tree type)
5885 : : {
5886 : 816330 : tree t, r, binfo;
5887 : 816330 : int i;
5888 : :
5889 : 816330 : type = strip_array_types (type);
5890 : 816330 : if (!CLASS_TYPE_P (type))
5891 : : return type;
5892 : 702472 : if (!type_has_non_user_provided_default_constructor (type))
5893 : : return NULL_TREE;
5894 : 564208 : for (binfo = TYPE_BINFO (type), i = 0;
5895 : 564208 : BINFO_BASE_ITERATE (binfo, i, t); ++i)
5896 : : {
5897 : 51994 : r = default_init_uninitialized_part (BINFO_TYPE (t));
5898 : 51994 : if (r)
5899 : : return r;
5900 : : }
5901 : 564756 : for (t = next_aggregate_field (TYPE_FIELDS (type)); t;
5902 : 52542 : t = next_aggregate_field (DECL_CHAIN (t)))
5903 : 162169 : if (!DECL_INITIAL (t) && !DECL_ARTIFICIAL (t))
5904 : : {
5905 : 109702 : r = default_init_uninitialized_part (TREE_TYPE (t));
5906 : 109702 : if (r)
5907 : 219240 : return DECL_P (r) ? r : t;
5908 : : }
5909 : :
5910 : : return NULL_TREE;
5911 : : }
5912 : :
5913 : : /* Returns true iff for class T, a trivial synthesized default constructor
5914 : : would be constexpr. */
5915 : :
5916 : : bool
5917 : 35488878 : trivial_default_constructor_is_constexpr (tree t)
5918 : : {
5919 : : /* A defaulted trivial default constructor is constexpr
5920 : : if there is nothing to initialize. */
5921 : 35488878 : gcc_assert (!TYPE_HAS_COMPLEX_DFLT (t));
5922 : : /* A class with a vptr doesn't have a trivial default ctor.
5923 : : In C++20, a class can have transient uninitialized members, e.g.:
5924 : :
5925 : : struct S { int i; constexpr S() = default; };
5926 : :
5927 : : should work. */
5928 : 35488878 : return (cxx_dialect >= cxx20
5929 : 35488878 : || is_really_empty_class (t, /*ignore_vptr*/true));
5930 : : }
5931 : :
5932 : : /* Returns true iff class T has a constexpr default constructor. */
5933 : :
5934 : : bool
5935 : 32880816 : type_has_constexpr_default_constructor (tree t)
5936 : : {
5937 : 32880816 : tree fns;
5938 : :
5939 : 32880816 : if (!CLASS_TYPE_P (t))
5940 : : {
5941 : : /* The caller should have stripped an enclosing array. */
5942 : 236 : gcc_assert (TREE_CODE (t) != ARRAY_TYPE);
5943 : : return false;
5944 : : }
5945 : 32880580 : if (CLASSTYPE_LAZY_DEFAULT_CTOR (t))
5946 : : {
5947 : 32879023 : if (!TYPE_HAS_COMPLEX_DFLT (t))
5948 : 32878965 : return trivial_default_constructor_is_constexpr (t);
5949 : : /* Non-trivial, we need to check subobject constructors. */
5950 : 58 : lazily_declare_fn (sfk_constructor, t);
5951 : : }
5952 : 1615 : fns = locate_ctor (t);
5953 : 1615 : return (fns && DECL_DECLARED_CONSTEXPR_P (fns));
5954 : : }
5955 : :
5956 : : /* Returns true iff class T has a constexpr default constructor or has an
5957 : : implicitly declared default constructor that we can't tell if it's constexpr
5958 : : without forcing a lazy declaration (which might cause undesired
5959 : : instantiations). */
5960 : :
5961 : : static bool
5962 : 33222726 : type_maybe_constexpr_default_constructor (tree t)
5963 : : {
5964 : 33222726 : if (CLASS_TYPE_P (t) && CLASSTYPE_LAZY_DEFAULT_CTOR (t)
5965 : 66445452 : && TYPE_HAS_COMPLEX_DFLT (t))
5966 : : /* Assume it's constexpr. */
5967 : : return true;
5968 : 32878965 : return type_has_constexpr_default_constructor (t);
5969 : : }
5970 : :
5971 : : /* Returns true iff class T has a constexpr destructor. */
5972 : :
5973 : : bool
5974 : 339 : type_has_constexpr_destructor (tree t)
5975 : : {
5976 : 339 : tree fns;
5977 : :
5978 : 339 : if (CLASSTYPE_LAZY_DESTRUCTOR (t))
5979 : : /* Non-trivial, we need to check subobject destructors. */
5980 : 10 : lazily_declare_fn (sfk_destructor, t);
5981 : 339 : fns = CLASSTYPE_DESTRUCTOR (t);
5982 : 339 : return (fns && DECL_DECLARED_CONSTEXPR_P (fns));
5983 : : }
5984 : :
5985 : : /* Returns true iff class T has a constexpr destructor or has an
5986 : : implicitly declared destructor that we can't tell if it's constexpr
5987 : : without forcing a lazy declaration (which might cause undesired
5988 : : instantiations). */
5989 : :
5990 : : static bool
5991 : 36704604 : type_maybe_constexpr_destructor (tree t)
5992 : : {
5993 : : /* Until C++20, only trivial destruction is constexpr. */
5994 : 36704604 : if (TYPE_HAS_TRIVIAL_DESTRUCTOR (t))
5995 : : return true;
5996 : 1230763 : if (cxx_dialect < cxx20)
5997 : : return false;
5998 : 464552 : if (CLASS_TYPE_P (t) && CLASSTYPE_LAZY_DESTRUCTOR (t))
5999 : : /* Assume it's constexpr. */
6000 : : return true;
6001 : 402210 : tree fn = CLASSTYPE_DESTRUCTOR (t);
6002 : 402210 : return (fn && maybe_constexpr_fn (fn));
6003 : : }
6004 : :
6005 : : /* Returns true iff class TYPE has a virtual destructor. */
6006 : :
6007 : : bool
6008 : 3124912 : type_has_virtual_destructor (tree type)
6009 : : {
6010 : 3124912 : tree dtor;
6011 : :
6012 : 3124912 : if (!NON_UNION_CLASS_TYPE_P (type))
6013 : : return false;
6014 : :
6015 : 3124784 : gcc_assert (COMPLETE_TYPE_P (type));
6016 : 3124784 : dtor = CLASSTYPE_DESTRUCTOR (type);
6017 : 3124784 : return (dtor && DECL_VIRTUAL_P (dtor));
6018 : : }
6019 : :
6020 : : /* True iff class TYPE has a non-deleted trivial default
6021 : : constructor. */
6022 : :
6023 : : bool
6024 : 19961 : type_has_non_deleted_trivial_default_ctor (tree type)
6025 : : {
6026 : 19961 : return TYPE_HAS_TRIVIAL_DFLT (type) && locate_ctor (type);
6027 : : }
6028 : :
6029 : : /* Returns true iff T, a class, has a move-assignment or
6030 : : move-constructor. Does not lazily declare either.
6031 : : If USER_P is false, any move function will do. If it is true, the
6032 : : move function must be user-declared.
6033 : :
6034 : : Note that user-declared here is different from "user-provided",
6035 : : which doesn't include functions that are defaulted in the
6036 : : class. */
6037 : :
6038 : : bool
6039 : 40489249 : classtype_has_move_assign_or_move_ctor_p (tree t, bool user_p)
6040 : : {
6041 : 40489249 : gcc_assert (user_p
6042 : : || (!CLASSTYPE_LAZY_MOVE_CTOR (t)
6043 : : && !CLASSTYPE_LAZY_MOVE_ASSIGN (t)));
6044 : :
6045 : 40489249 : if (!CLASSTYPE_LAZY_MOVE_CTOR (t))
6046 : 48451217 : for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (t)); iter; ++iter)
6047 : 8126552 : if ((!user_p || !DECL_ARTIFICIAL (*iter)) && move_fn_p (*iter))
6048 : 985622 : return true;
6049 : :
6050 : 39503627 : if (!CLASSTYPE_LAZY_MOVE_ASSIGN (t))
6051 : 797051 : for (ovl_iterator iter (get_class_binding_direct
6052 : 35956361 : (t, assign_op_identifier));
6053 : 36753412 : iter; ++iter)
6054 : 731683 : if ((!user_p || !DECL_ARTIFICIAL (*iter))
6055 : 158682 : && DECL_CONTEXT (*iter) == t
6056 : 876237 : && move_fn_p (*iter))
6057 : 1636 : return true;
6058 : :
6059 : : return false;
6060 : : }
6061 : :
6062 : : /* True iff T has a move constructor that is not deleted. */
6063 : :
6064 : : bool
6065 : 6 : classtype_has_non_deleted_move_ctor (tree t)
6066 : : {
6067 : 6 : if (CLASSTYPE_LAZY_MOVE_CTOR (t))
6068 : 0 : lazily_declare_fn (sfk_move_constructor, t);
6069 : 6 : for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (t)); iter; ++iter)
6070 : 6 : if (move_fn_p (*iter) && !DECL_DELETED_FN (*iter))
6071 : 6 : return true;
6072 : 0 : return false;
6073 : : }
6074 : :
6075 : : /* If T, a class, has a user-provided copy constructor, copy assignment
6076 : : operator, or destructor, returns that function. Otherwise, null. */
6077 : :
6078 : : tree
6079 : 4632121 : classtype_has_depr_implicit_copy (tree t)
6080 : : {
6081 : 4632121 : if (!CLASSTYPE_LAZY_COPY_CTOR (t))
6082 : 12291551 : for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (t)); iter; ++iter)
6083 : : {
6084 : 6120732 : tree fn = *iter;
6085 : 6120732 : if (user_provided_p (fn) && copy_fn_p (fn))
6086 : 712 : return fn;
6087 : : }
6088 : :
6089 : 4631409 : if (!CLASSTYPE_LAZY_COPY_ASSIGN (t))
6090 : 651879 : for (ovl_iterator iter (get_class_binding_direct
6091 : 1183608 : (t, assign_op_identifier));
6092 : 1835487 : iter; ++iter)
6093 : : {
6094 : 652282 : tree fn = *iter;
6095 : 652282 : if (DECL_CONTEXT (fn) == t
6096 : 652282 : && user_provided_p (fn) && copy_fn_p (fn))
6097 : 403 : return fn;
6098 : : }
6099 : :
6100 : 4631006 : if (!CLASSTYPE_LAZY_DESTRUCTOR (t))
6101 : : {
6102 : 1667486 : tree fn = CLASSTYPE_DESTRUCTOR (t);
6103 : 1667486 : if (user_provided_p (fn))
6104 : : return fn;
6105 : : }
6106 : :
6107 : : return NULL_TREE;
6108 : : }
6109 : :
6110 : : /* True iff T has a member or friend declaration of operator OP. */
6111 : :
6112 : : bool
6113 : 39307783 : classtype_has_op (tree t, tree_code op)
6114 : : {
6115 : 39307783 : tree name = ovl_op_identifier (op);
6116 : 39307783 : if (get_class_binding (t, name))
6117 : : return true;
6118 : 41190125 : for (tree f = DECL_FRIENDLIST (TYPE_MAIN_DECL (t)); f; f = TREE_CHAIN (f))
6119 : 2846050 : if (FRIEND_NAME (f) == name)
6120 : : return true;
6121 : : return false;
6122 : : }
6123 : :
6124 : :
6125 : : /* If T has a defaulted member or friend declaration of OP, return it. */
6126 : :
6127 : : tree
6128 : 38344075 : classtype_has_defaulted_op (tree t, tree_code op)
6129 : : {
6130 : 38344075 : tree name = ovl_op_identifier (op);
6131 : 38344113 : for (ovl_iterator oi (get_class_binding (t, name)); oi; ++oi)
6132 : : {
6133 : 228 : tree fn = *oi;
6134 : 228 : if (DECL_DEFAULTED_FN (fn))
6135 : 195 : return fn;
6136 : : }
6137 : 38960190 : for (tree f = DECL_FRIENDLIST (TYPE_MAIN_DECL (t)); f; f = TREE_CHAIN (f))
6138 : 616361 : if (FRIEND_NAME (f) == name)
6139 : 467 : for (tree l = FRIEND_DECLS (f); l; l = TREE_CHAIN (l))
6140 : : {
6141 : 264 : tree fn = TREE_VALUE (l);
6142 : 264 : if (DECL_DEFAULTED_FN (fn))
6143 : : return fn;
6144 : : }
6145 : : return NULL_TREE;
6146 : : }
6147 : :
6148 : : /* Nonzero if we need to build up a constructor call when initializing an
6149 : : object of this class, either because it has a user-declared constructor
6150 : : or because it doesn't have a default constructor (so we need to give an
6151 : : error if no initializer is provided). Use TYPE_NEEDS_CONSTRUCTING when
6152 : : what you care about is whether or not an object can be produced by a
6153 : : constructor (e.g. so we don't set TREE_READONLY on const variables of
6154 : : such type); use this function when what you care about is whether or not
6155 : : to try to call a constructor to create an object. The latter case is
6156 : : the former plus some cases of constructors that cannot be called. */
6157 : :
6158 : : bool
6159 : 52998710 : type_build_ctor_call (tree t)
6160 : : {
6161 : 52998710 : tree inner;
6162 : 52998710 : if (TYPE_NEEDS_CONSTRUCTING (t))
6163 : : return true;
6164 : 47018410 : inner = strip_array_types (t);
6165 : 47018410 : if (!CLASS_TYPE_P (inner) || ANON_AGGR_TYPE_P (inner))
6166 : : return false;
6167 : 3798570 : if (!TYPE_HAS_DEFAULT_CONSTRUCTOR (inner))
6168 : : return true;
6169 : 3399343 : if (cxx_dialect < cxx11)
6170 : : return false;
6171 : : /* A user-declared constructor might be private, and a constructor might
6172 : : be trivial but deleted. */
6173 : 12887956 : for (ovl_iterator iter (get_class_binding (inner, complete_ctor_identifier));
6174 : 12887956 : iter; ++iter)
6175 : : {
6176 : 9935017 : tree fn = *iter;
6177 : 9935017 : if (!DECL_ARTIFICIAL (fn)
6178 : 9758467 : || TREE_DEPRECATED (fn)
6179 : 9640647 : || TREE_UNAVAILABLE (fn)
6180 : 19575664 : || DECL_DELETED_FN (fn))
6181 : 437906 : return true;
6182 : : }
6183 : 2952939 : return false;
6184 : : }
6185 : :
6186 : : /* Like type_build_ctor_call, but for destructors. */
6187 : :
6188 : : bool
6189 : 99948393 : type_build_dtor_call (tree t)
6190 : : {
6191 : 99948393 : tree inner;
6192 : 99948393 : if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t))
6193 : : return true;
6194 : 75011748 : inner = strip_array_types (t);
6195 : 30654831 : if (!CLASS_TYPE_P (inner) || ANON_AGGR_TYPE_P (inner)
6196 : 105486594 : || !COMPLETE_TYPE_P (inner))
6197 : : return false;
6198 : 30474823 : if (cxx_dialect < cxx11)
6199 : : return false;
6200 : : /* A user-declared destructor might be private, and a destructor might
6201 : : be trivial but deleted. */
6202 : 54232446 : for (ovl_iterator iter (get_class_binding (inner, complete_dtor_identifier));
6203 : 54232446 : iter; ++iter)
6204 : : {
6205 : 30043544 : tree fn = *iter;
6206 : 30043544 : if (!DECL_ARTIFICIAL (fn)
6207 : 23792297 : || TREE_DEPRECATED (fn)
6208 : 23792297 : || TREE_UNAVAILABLE (fn)
6209 : 53835841 : || DECL_DELETED_FN (fn))
6210 : 6251250 : return true;
6211 : : }
6212 : 24188902 : return false;
6213 : : }
6214 : :
6215 : : /* Returns TRUE iff we need a cookie when dynamically allocating an
6216 : : array whose elements have the indicated class TYPE. */
6217 : :
6218 : : static bool
6219 : 39307783 : type_requires_array_cookie (tree type)
6220 : : {
6221 : 39307783 : tree fns;
6222 : 39307783 : bool has_two_argument_delete_p = false;
6223 : :
6224 : 39307783 : gcc_assert (CLASS_TYPE_P (type));
6225 : :
6226 : : /* If there's a non-trivial destructor, we need a cookie. In order
6227 : : to iterate through the array calling the destructor for each
6228 : : element, we'll have to know how many elements there are. */
6229 : 39307783 : if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
6230 : : return true;
6231 : :
6232 : : /* If the usual deallocation function is a two-argument whose second
6233 : : argument is of type `size_t', then we have to pass the size of
6234 : : the array to the deallocation function, so we will need to store
6235 : : a cookie. */
6236 : 35728282 : fns = lookup_fnfields (TYPE_BINFO (type),
6237 : : ovl_op_identifier (false, VEC_DELETE_EXPR),
6238 : : /*protect=*/0, tf_warning_or_error);
6239 : : /* If there are no `operator []' members, or the lookup is
6240 : : ambiguous, then we don't need a cookie. */
6241 : 35728282 : if (!fns || fns == error_mark_node)
6242 : : return false;
6243 : : /* Loop through all of the functions. */
6244 : 236 : for (lkp_iterator iter (BASELINK_FUNCTIONS (fns)); iter; ++iter)
6245 : : {
6246 : 224 : tree fn = *iter;
6247 : :
6248 : : /* See if this function is a one-argument delete function. If
6249 : : it is, then it will be the usual deallocation function. */
6250 : 224 : tree second_parm = TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (fn)));
6251 : 224 : if (second_parm == void_list_node)
6252 : 101 : return false;
6253 : : /* Do not consider this function if its second argument is an
6254 : : ellipsis. */
6255 : 123 : if (!second_parm)
6256 : 3 : continue;
6257 : : /* Otherwise, if we have a two-argument function and the second
6258 : : argument is `size_t', it will be the usual deallocation
6259 : : function -- unless there is one-argument function, too. */
6260 : 120 : if (TREE_CHAIN (second_parm) == void_list_node
6261 : 120 : && same_type_p (TREE_VALUE (second_parm), size_type_node))
6262 : : has_two_argument_delete_p = true;
6263 : : }
6264 : :
6265 : 12 : return has_two_argument_delete_p;
6266 : : }
6267 : :
6268 : : /* Finish computing the `literal type' property of class type T.
6269 : :
6270 : : At this point, we have already processed base classes and
6271 : : non-static data members. We need to check whether the copy
6272 : : constructor is trivial, the destructor is trivial, and there
6273 : : is a trivial default constructor or at least one constexpr
6274 : : constructor other than the copy constructor. */
6275 : :
6276 : : static void
6277 : 39307783 : finalize_literal_type_property (tree t)
6278 : : {
6279 : 39307783 : tree fn;
6280 : :
6281 : 39307783 : if (cxx_dialect < cxx11)
6282 : 107937 : CLASSTYPE_LITERAL_P (t) = false;
6283 : 39199846 : else if (CLASSTYPE_LITERAL_P (t)
6284 : 39199846 : && !type_maybe_constexpr_destructor (t))
6285 : 936534 : CLASSTYPE_LITERAL_P (t) = false;
6286 : 73649132 : else if (CLASSTYPE_LITERAL_P (t) && LAMBDA_TYPE_P (t))
6287 : 302762 : CLASSTYPE_LITERAL_P (t) = (cxx_dialect >= cxx17);
6288 : 73425845 : else if (CLASSTYPE_LITERAL_P (t) && !TYPE_HAS_TRIVIAL_DFLT (t)
6289 : 2284691 : && CLASSTYPE_NON_AGGREGATE (t)
6290 : 40162283 : && !TYPE_HAS_CONSTEXPR_CTOR (t))
6291 : 570791 : CLASSTYPE_LITERAL_P (t) = false;
6292 : :
6293 : : /* C++14 DR 1684 removed this restriction. */
6294 : 39307783 : if (cxx_dialect < cxx14
6295 : 39430350 : && !CLASSTYPE_LITERAL_P (t) && !LAMBDA_TYPE_P (t))
6296 : 1295773 : for (fn = TYPE_FIELDS (t); fn; fn = DECL_CHAIN (fn))
6297 : 1167055 : if (TREE_CODE (fn) == FUNCTION_DECL
6298 : 558970 : && DECL_DECLARED_CONSTEXPR_P (fn)
6299 : 2639 : && DECL_IOBJ_MEMBER_FUNCTION_P (fn)
6300 : 1169363 : && !DECL_CONSTRUCTOR_P (fn))
6301 : : {
6302 : 579 : DECL_DECLARED_CONSTEXPR_P (fn) = false;
6303 : 579 : if (!DECL_GENERATED_P (fn))
6304 : : {
6305 : 7 : auto_diagnostic_group d;
6306 : 7 : if (pedwarn (DECL_SOURCE_LOCATION (fn), OPT_Wpedantic,
6307 : : "enclosing class of %<constexpr%> non-static "
6308 : : "member function %q+#D is not a literal type", fn))
6309 : 5 : explain_non_literal_class (t);
6310 : 7 : }
6311 : : }
6312 : 39307783 : }
6313 : :
6314 : : /* T is a non-literal type used in a context which requires a constant
6315 : : expression. Explain why it isn't literal. */
6316 : :
6317 : : void
6318 : 143 : explain_non_literal_class (tree t)
6319 : : {
6320 : 143 : static hash_set<tree> *diagnosed;
6321 : :
6322 : 143 : if (!CLASS_TYPE_P (t))
6323 : 54 : return;
6324 : 140 : t = TYPE_MAIN_VARIANT (t);
6325 : :
6326 : 140 : if (diagnosed == NULL)
6327 : 77 : diagnosed = new hash_set<tree>;
6328 : 140 : if (diagnosed->add (t))
6329 : : /* Already explained. */
6330 : : return;
6331 : :
6332 : 104 : auto_diagnostic_group d;
6333 : 104 : inform (UNKNOWN_LOCATION, "%q+T is not literal because:", t);
6334 : 135 : if (cxx_dialect < cxx17 && LAMBDA_TYPE_P (t))
6335 : 2 : inform (UNKNOWN_LOCATION,
6336 : : " %qT is a closure type, which is only literal in "
6337 : : "C++17 and later", t);
6338 : 102 : else if (cxx_dialect < cxx20 && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t))
6339 : 8 : inform (UNKNOWN_LOCATION, " %q+T has a non-trivial destructor", t);
6340 : 94 : else if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t)
6341 : 94 : && !type_maybe_constexpr_destructor (t))
6342 : 13 : inform (UNKNOWN_LOCATION, " %q+T does not have %<constexpr%> destructor",
6343 : : t);
6344 : 81 : else if (CLASSTYPE_NON_AGGREGATE (t)
6345 : 71 : && !TYPE_HAS_TRIVIAL_DFLT (t)
6346 : 139 : && !LAMBDA_TYPE_P (t)
6347 : 152 : && !TYPE_HAS_CONSTEXPR_CTOR (t))
6348 : : {
6349 : 48 : inform (UNKNOWN_LOCATION,
6350 : : " %q+T is not an aggregate, does not have a trivial "
6351 : : "default constructor, and has no %<constexpr%> constructor that "
6352 : : "is not a copy or move constructor", t);
6353 : 48 : if (type_has_non_user_provided_default_constructor (t))
6354 : : /* Note that we can't simply call locate_ctor because when the
6355 : : constructor is deleted it just returns NULL_TREE. */
6356 : 0 : for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (t)); iter; ++iter)
6357 : : {
6358 : 0 : tree fn = *iter;
6359 : 0 : tree parms = TYPE_ARG_TYPES (TREE_TYPE (fn));
6360 : :
6361 : 0 : parms = skip_artificial_parms_for (fn, parms);
6362 : :
6363 : 0 : if (sufficient_parms_p (parms))
6364 : : {
6365 : 0 : if (DECL_DELETED_FN (fn))
6366 : 0 : maybe_explain_implicit_delete (fn);
6367 : : else
6368 : 0 : explain_invalid_constexpr_fn (fn);
6369 : : break;
6370 : : }
6371 : : }
6372 : : }
6373 : : else
6374 : : {
6375 : 33 : tree binfo, base_binfo, field; int i;
6376 : 33 : for (binfo = TYPE_BINFO (t), i = 0;
6377 : 33 : BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
6378 : : {
6379 : 15 : tree basetype = TREE_TYPE (base_binfo);
6380 : 15 : if (!CLASSTYPE_LITERAL_P (basetype))
6381 : : {
6382 : 15 : inform (UNKNOWN_LOCATION,
6383 : : " base class %qT of %q+T is non-literal",
6384 : : basetype, t);
6385 : 15 : explain_non_literal_class (basetype);
6386 : 15 : return;
6387 : : }
6388 : : }
6389 : 138 : for (field = TYPE_FIELDS (t); field; field = TREE_CHAIN (field))
6390 : : {
6391 : 120 : tree ftype;
6392 : 120 : if (TREE_CODE (field) != FIELD_DECL)
6393 : 102 : continue;
6394 : 18 : ftype = TREE_TYPE (field);
6395 : 18 : if (!literal_type_p (ftype))
6396 : : {
6397 : 6 : inform (DECL_SOURCE_LOCATION (field),
6398 : : " non-static data member %qD has non-literal type",
6399 : : field);
6400 : 6 : if (CLASS_TYPE_P (ftype))
6401 : 6 : explain_non_literal_class (ftype);
6402 : : }
6403 : 18 : if (CP_TYPE_VOLATILE_P (ftype))
6404 : 12 : inform (DECL_SOURCE_LOCATION (field),
6405 : : " non-static data member %qD has volatile type", field);
6406 : : }
6407 : : }
6408 : 104 : }
6409 : :
6410 : : /* Check the validity of the bases and members declared in T. Add any
6411 : : implicitly-generated functions (like copy-constructors and
6412 : : assignment operators). Compute various flag bits (like
6413 : : CLASSTYPE_NON_LAYOUT_POD_T) for T. This routine works purely at the C++
6414 : : level: i.e., independently of the ABI in use. */
6415 : :
6416 : : static void
6417 : 39307783 : check_bases_and_members (tree t)
6418 : : {
6419 : : /* Nonzero if the implicitly generated copy constructor should take
6420 : : a non-const reference argument. */
6421 : 39307783 : int cant_have_const_ctor;
6422 : : /* Nonzero if the implicitly generated assignment operator
6423 : : should take a non-const reference argument. */
6424 : 39307783 : int no_const_asn_ref;
6425 : 39307783 : tree access_decls;
6426 : 39307783 : bool saved_complex_asn_ref;
6427 : 39307783 : bool saved_nontrivial_dtor;
6428 : 39307783 : tree fn;
6429 : :
6430 : : /* By default, we use const reference arguments and generate default
6431 : : constructors. */
6432 : 39307783 : cant_have_const_ctor = 0;
6433 : 39307783 : no_const_asn_ref = 0;
6434 : :
6435 : : /* Check all the base-classes and set FMEM members to point to arrays
6436 : : of potential interest. */
6437 : 39307783 : check_bases (t, &cant_have_const_ctor, &no_const_asn_ref);
6438 : :
6439 : : /* Deduce noexcept on destructor. This needs to happen after we've set
6440 : : triviality flags appropriately for our bases, and before checking
6441 : : overriden virtual functions via check_methods. */
6442 : 39307783 : if (cxx_dialect >= cxx11)
6443 : 39199846 : if (tree dtor = CLASSTYPE_DESTRUCTOR (t))
6444 : 7217948 : for (tree fn : ovl_range (dtor))
6445 : 3608974 : deduce_noexcept_on_destructor (fn);
6446 : :
6447 : : /* Check all the method declarations. */
6448 : 39307783 : check_methods (t);
6449 : :
6450 : : /* Save the initial values of these flags which only indicate whether
6451 : : or not the class has user-provided functions. As we analyze the
6452 : : bases and members we can set these flags for other reasons. */
6453 : 39307783 : saved_complex_asn_ref = TYPE_HAS_COMPLEX_COPY_ASSIGN (t);
6454 : 39307783 : saved_nontrivial_dtor = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t);
6455 : :
6456 : : /* Check all the data member declarations. We cannot call
6457 : : check_field_decls until we have called check_bases check_methods,
6458 : : as check_field_decls depends on TYPE_HAS_NONTRIVIAL_DESTRUCTOR
6459 : : being set appropriately. */
6460 : 39307783 : check_field_decls (t, &access_decls,
6461 : : &cant_have_const_ctor,
6462 : : &no_const_asn_ref);
6463 : :
6464 : : /* A nearly-empty class has to be vptr-containing; a nearly empty
6465 : : class contains just a vptr. */
6466 : 39307783 : if (!TYPE_CONTAINS_VPTR_P (t))
6467 : 37621452 : CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
6468 : :
6469 : : /* Do some bookkeeping that will guide the generation of implicitly
6470 : : declared member functions. */
6471 : 40994114 : TYPE_HAS_COMPLEX_COPY_CTOR (t) |= TYPE_CONTAINS_VPTR_P (t);
6472 : 40994114 : TYPE_HAS_COMPLEX_MOVE_CTOR (t) |= TYPE_CONTAINS_VPTR_P (t);
6473 : : /* We need to call a constructor for this class if it has a
6474 : : user-provided constructor, or if the default constructor is going
6475 : : to initialize the vptr. (This is not an if-and-only-if;
6476 : : TYPE_NEEDS_CONSTRUCTING is set elsewhere if bases or members
6477 : : themselves need constructing.) */
6478 : 39307783 : TYPE_NEEDS_CONSTRUCTING (t)
6479 : 39307783 : |= (type_has_user_provided_constructor (t) || TYPE_CONTAINS_VPTR_P (t));
6480 : : /* [dcl.init.aggr]
6481 : :
6482 : : An aggregate is an array or a class with no user-provided
6483 : : constructors ... and no virtual functions.
6484 : :
6485 : : Again, other conditions for being an aggregate are checked
6486 : : elsewhere. */
6487 : 39307783 : CLASSTYPE_NON_AGGREGATE (t)
6488 : 39307783 : |= (type_has_user_provided_or_explicit_constructor (t)
6489 : 39307783 : || TYPE_POLYMORPHIC_P (t));
6490 : : /* This is the C++98/03 definition of POD; it changed in C++0x, but we
6491 : : retain the old definition internally for ABI reasons. */
6492 : 39307783 : CLASSTYPE_NON_LAYOUT_POD_P (t)
6493 : 39307783 : |= (CLASSTYPE_NON_AGGREGATE (t)
6494 : 39307783 : || saved_nontrivial_dtor || saved_complex_asn_ref);
6495 : 40994114 : CLASSTYPE_NON_STD_LAYOUT (t) |= TYPE_CONTAINS_VPTR_P (t);
6496 : 40994114 : TYPE_HAS_COMPLEX_COPY_ASSIGN (t) |= TYPE_CONTAINS_VPTR_P (t);
6497 : 40994114 : TYPE_HAS_COMPLEX_MOVE_ASSIGN (t) |= TYPE_CONTAINS_VPTR_P (t);
6498 : 40994114 : TYPE_HAS_COMPLEX_DFLT (t) |= TYPE_CONTAINS_VPTR_P (t);
6499 : :
6500 : : /* Is this class non-layout-POD because it wasn't an aggregate in C++98? */
6501 : 39307783 : if (CLASSTYPE_NON_POD_AGGREGATE (t))
6502 : : {
6503 : 526145 : if (CLASSTYPE_NON_LAYOUT_POD_P (t))
6504 : : /* It's non-POD for another reason. */
6505 : 499899 : CLASSTYPE_NON_POD_AGGREGATE (t) = false;
6506 : 26246 : else if (abi_version_at_least (17))
6507 : 26244 : CLASSTYPE_NON_LAYOUT_POD_P (t) = true;
6508 : : }
6509 : :
6510 : : /* P1008: Prohibit aggregates with user-declared constructors. */
6511 : 39307783 : if (cxx_dialect >= cxx20 && TYPE_HAS_USER_CONSTRUCTOR (t))
6512 : : {
6513 : 2015873 : CLASSTYPE_NON_AGGREGATE (t) = true;
6514 : 2015873 : if (!CLASSTYPE_NON_LAYOUT_POD_P (t))
6515 : : {
6516 : : /* c++/120012: The C++20 aggregate change affected layout. */
6517 : 2085 : if (!abi_version_at_least (21))
6518 : 3 : CLASSTYPE_NON_LAYOUT_POD_P (t) = true;
6519 : 4490 : if (abi_version_crosses (21))
6520 : 1765 : CLASSTYPE_NON_AGGREGATE_POD (t) = true;
6521 : : }
6522 : : }
6523 : :
6524 : : /* If the only explicitly declared default constructor is user-provided,
6525 : : set TYPE_HAS_COMPLEX_DFLT. */
6526 : 39307783 : if (!TYPE_HAS_COMPLEX_DFLT (t)
6527 : 35419840 : && TYPE_HAS_DEFAULT_CONSTRUCTOR (t)
6528 : 41299983 : && !type_has_non_user_provided_default_constructor (t))
6529 : 1324685 : TYPE_HAS_COMPLEX_DFLT (t) = true;
6530 : :
6531 : : /* Warn if a public base of a polymorphic type has an accessible
6532 : : non-virtual destructor. It is only now that we know the class is
6533 : : polymorphic. Although a polymorphic base will have a already
6534 : : been diagnosed during its definition, we warn on use too. */
6535 : 39307783 : if (TYPE_POLYMORPHIC_P (t) && warn_nonvdtor)
6536 : : {
6537 : 84 : tree binfo = TYPE_BINFO (t);
6538 : 84 : vec<tree, va_gc> *accesses = BINFO_BASE_ACCESSES (binfo);
6539 : 84 : tree base_binfo;
6540 : 84 : unsigned i;
6541 : :
6542 : 114 : for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
6543 : : {
6544 : 30 : tree basetype = TREE_TYPE (base_binfo);
6545 : :
6546 : 30 : if ((*accesses)[i] == access_public_node
6547 : 18 : && (TYPE_POLYMORPHIC_P (basetype) || warn_ecpp)
6548 : 45 : && accessible_nvdtor_p (basetype))
6549 : 9 : warning (OPT_Wnon_virtual_dtor,
6550 : : "base class %q#T has accessible non-virtual destructor",
6551 : : basetype);
6552 : : }
6553 : : }
6554 : :
6555 : : /* If the class has no user-declared constructor, but does have
6556 : : non-static const or reference data members that can never be
6557 : : initialized, issue a warning. */
6558 : 39307783 : if (warn_uninitialized
6559 : : /* Classes with user-declared constructors are presumed to
6560 : : initialize these members. */
6561 : 389821 : && !TYPE_HAS_USER_CONSTRUCTOR (t)
6562 : : /* Aggregates can be initialized with brace-enclosed
6563 : : initializers. */
6564 : 39632484 : && CLASSTYPE_NON_AGGREGATE (t))
6565 : : {
6566 : 38793 : tree field;
6567 : :
6568 : 240140 : for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
6569 : : {
6570 : 201347 : tree type;
6571 : :
6572 : 381517 : if (TREE_CODE (field) != FIELD_DECL
6573 : 201347 : || DECL_INITIAL (field) != NULL_TREE)
6574 : 180170 : continue;
6575 : :
6576 : 21177 : type = TREE_TYPE (field);
6577 : 21177 : if (TYPE_REF_P (type))
6578 : 6 : warning_at (DECL_SOURCE_LOCATION (field),
6579 : 6 : OPT_Wuninitialized, "non-static reference %q#D "
6580 : : "in class without a constructor", field);
6581 : 21171 : else if (CP_TYPE_CONST_P (type)
6582 : 21171 : && (!CLASS_TYPE_P (type)
6583 : 3 : || !TYPE_HAS_DEFAULT_CONSTRUCTOR (type)))
6584 : 9 : warning_at (DECL_SOURCE_LOCATION (field),
6585 : 9 : OPT_Wuninitialized, "non-static const member %q#D "
6586 : : "in class without a constructor", field);
6587 : : }
6588 : : }
6589 : :
6590 : : /* Synthesize any needed methods. */
6591 : 39307783 : add_implicitly_declared_members (t, &access_decls,
6592 : : cant_have_const_ctor,
6593 : : no_const_asn_ref);
6594 : :
6595 : : /* Check defaulted declarations here so we have cant_have_const_ctor
6596 : : and don't need to worry about clones. */
6597 : 270674791 : for (fn = TYPE_FIELDS (t); fn; fn = DECL_CHAIN (fn))
6598 : 135293026 : if (DECL_DECLARES_FUNCTION_P (fn)
6599 : 118075062 : && !DECL_ARTIFICIAL (fn)
6600 : 117111621 : && DECL_DEFAULTED_IN_CLASS_P (fn)
6601 : : /* ...except handle comparisons later, in finish_struct_1. */
6602 : 236858984 : && special_function_p (fn) != sfk_comparison)
6603 : : {
6604 : 5480161 : bool imp_const_p
6605 : 10960322 : = (DECL_CONSTRUCTOR_P (fn) ? !cant_have_const_ctor
6606 : 5480161 : : !no_const_asn_ref);
6607 : 5480241 : defaulted_late_check (fn, imp_const_p);
6608 : : }
6609 : :
6610 : 78222849 : if (LAMBDA_TYPE_P (t))
6611 : : /* "This class type is not an aggregate." */
6612 : 302936 : CLASSTYPE_NON_AGGREGATE (t) = 1;
6613 : :
6614 : : /* Compute the 'literal type' property before we
6615 : : do anything with non-static member functions. */
6616 : 39307783 : finalize_literal_type_property (t);
6617 : :
6618 : : /* Create the in-charge and not-in-charge variants of constructors
6619 : : and destructors. */
6620 : 39307783 : clone_constructors_and_destructors (t);
6621 : :
6622 : : /* Process the using-declarations. */
6623 : 80657863 : for (; access_decls; access_decls = TREE_CHAIN (access_decls))
6624 : 2042297 : handle_using_decl (TREE_VALUE (access_decls), t);
6625 : :
6626 : : /* Figure out whether or not we will need a cookie when dynamically
6627 : : allocating an array of this type. */
6628 : 39307783 : LANG_TYPE_CLASS_CHECK (t)->vec_new_uses_cookie
6629 : 39307783 : = type_requires_array_cookie (t);
6630 : :
6631 : : /* Classes marked hot or cold propagate the attribute to all members. We
6632 : : may do this now that methods are declared. This does miss some lazily
6633 : : declared special member functions (CLASSTYPE_LAZY_*), which are handled
6634 : : in lazily_declare_fn later on. */
6635 : 39307783 : propagate_class_warmth_attribute (t);
6636 : 39307783 : }
6637 : :
6638 : : /* If T needs a pointer to its virtual function table, set TYPE_VFIELD
6639 : : accordingly. If a new vfield was created (because T doesn't have a
6640 : : primary base class), then the newly created field is returned. It
6641 : : is not added to the TYPE_FIELDS list; it is the caller's
6642 : : responsibility to do that. Accumulate declared virtual functions
6643 : : on VIRTUALS_P. */
6644 : :
6645 : : static tree
6646 : 39307783 : create_vtable_ptr (tree t, tree* virtuals_p)
6647 : : {
6648 : 39307783 : tree fn;
6649 : :
6650 : : /* Collect the virtual functions declared in T. */
6651 : 329263540 : for (fn = TYPE_FIELDS (t); fn; fn = DECL_CHAIN (fn))
6652 : 289955757 : if (TREE_CODE (fn) == FUNCTION_DECL
6653 : 138805428 : && DECL_VINDEX (fn) && !DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (fn)
6654 : 297700305 : && TREE_CODE (DECL_VINDEX (fn)) != INTEGER_CST)
6655 : : {
6656 : 7744548 : tree new_virtual = make_node (TREE_LIST);
6657 : :
6658 : 7744548 : BV_FN (new_virtual) = fn;
6659 : 7744548 : BV_DELTA (new_virtual) = integer_zero_node;
6660 : 7744548 : BV_VCALL_INDEX (new_virtual) = NULL_TREE;
6661 : :
6662 : 7744548 : TREE_CHAIN (new_virtual) = *virtuals_p;
6663 : 7744548 : *virtuals_p = new_virtual;
6664 : : }
6665 : :
6666 : : /* If we couldn't find an appropriate base class, create a new field
6667 : : here. Even if there weren't any new virtual functions, we might need a
6668 : : new virtual function table if we're supposed to include vptrs in
6669 : : all classes that need them. */
6670 : 39307783 : if (!TYPE_VFIELD (t) && (*virtuals_p || TYPE_CONTAINS_VPTR_P (t)))
6671 : : {
6672 : : /* We build this decl with vtbl_ptr_type_node, which is a
6673 : : `vtable_entry_type*'. It might seem more precise to use
6674 : : `vtable_entry_type (*)[N]' where N is the number of virtual
6675 : : functions. However, that would require the vtable pointer in
6676 : : base classes to have a different type than the vtable pointer
6677 : : in derived classes. We could make that happen, but that
6678 : : still wouldn't solve all the problems. In particular, the
6679 : : type-based alias analysis code would decide that assignments
6680 : : to the base class vtable pointer can't alias assignments to
6681 : : the derived class vtable pointer, since they have different
6682 : : types. Thus, in a derived class destructor, where the base
6683 : : class constructor was inlined, we could generate bad code for
6684 : : setting up the vtable pointer.
6685 : :
6686 : : Therefore, we use one type for all vtable pointers. We still
6687 : : use a type-correct type; it's just doesn't indicate the array
6688 : : bounds. That's better than using `void*' or some such; it's
6689 : : cleaner, and it let's the alias analysis code know that these
6690 : : stores cannot alias stores to void*! */
6691 : 243916 : tree field;
6692 : :
6693 : 243916 : field = build_decl (input_location,
6694 : : FIELD_DECL, get_vfield_name (t), vtbl_ptr_type_node);
6695 : 243916 : DECL_VIRTUAL_P (field) = 1;
6696 : 243916 : DECL_ARTIFICIAL (field) = 1;
6697 : 243916 : DECL_FIELD_CONTEXT (field) = t;
6698 : 243916 : DECL_FCONTEXT (field) = t;
6699 : 243916 : if (TYPE_PACKED (t))
6700 : 3 : DECL_PACKED (field) = 1;
6701 : :
6702 : 243916 : TYPE_VFIELD (t) = field;
6703 : :
6704 : : /* This class is non-empty. */
6705 : 243916 : CLASSTYPE_EMPTY_P (t) = 0;
6706 : :
6707 : 243916 : return field;
6708 : : }
6709 : :
6710 : : return NULL_TREE;
6711 : : }
6712 : :
6713 : : /* Add OFFSET to all base types of BINFO which is a base in the
6714 : : hierarchy dominated by T.
6715 : :
6716 : : OFFSET, which is a type offset, is number of bytes. */
6717 : :
6718 : : static void
6719 : 23954449 : propagate_binfo_offsets (tree binfo, tree offset)
6720 : : {
6721 : 23954449 : int i;
6722 : 23954449 : tree primary_binfo;
6723 : 23954449 : tree base_binfo;
6724 : :
6725 : : /* Update BINFO's offset. */
6726 : 23954449 : BINFO_OFFSET (binfo)
6727 : 23954449 : = fold_convert (sizetype,
6728 : : size_binop (PLUS_EXPR,
6729 : : fold_convert (ssizetype, BINFO_OFFSET (binfo)),
6730 : : offset));
6731 : :
6732 : : /* Find the primary base class. */
6733 : 23954449 : primary_binfo = get_primary_binfo (binfo);
6734 : :
6735 : 25027501 : if (primary_binfo && BINFO_INHERITANCE_CHAIN (primary_binfo) == binfo)
6736 : 1072058 : propagate_binfo_offsets (primary_binfo, offset);
6737 : :
6738 : : /* Scan all of the bases, pushing the BINFO_OFFSET adjust
6739 : : downwards. */
6740 : 27265324 : for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
6741 : : {
6742 : : /* Don't do the primary base twice. */
6743 : 4383853 : if (base_binfo == primary_binfo)
6744 : 1072978 : continue;
6745 : :
6746 : 2237897 : if (BINFO_VIRTUAL_P (base_binfo))
6747 : 225870 : continue;
6748 : :
6749 : 2012027 : propagate_binfo_offsets (base_binfo, offset);
6750 : : }
6751 : 23954449 : }
6752 : :
6753 : : /* Set BINFO_OFFSET for all of the virtual bases for RLI->T. Update
6754 : : TYPE_ALIGN and TYPE_SIZE for T. OFFSETS gives the location of
6755 : : empty subobjects of T. */
6756 : :
6757 : : static void
6758 : 39307783 : layout_virtual_bases (record_layout_info rli, splay_tree offsets)
6759 : : {
6760 : 39307783 : tree vbase;
6761 : 39307783 : tree t = rli->t;
6762 : 39307783 : tree *next_field;
6763 : :
6764 : 39307783 : if (BINFO_N_BASE_BINFOS (TYPE_BINFO (t)) == 0)
6765 : : return;
6766 : :
6767 : : /* Find the last field. The artificial fields created for virtual
6768 : : bases will go after the last extant field to date. */
6769 : 20175913 : next_field = &TYPE_FIELDS (t);
6770 : 159296379 : while (*next_field)
6771 : 139120466 : next_field = &DECL_CHAIN (*next_field);
6772 : :
6773 : : /* Go through the virtual bases, allocating space for each virtual
6774 : : base that is not already a primary base class. These are
6775 : : allocated in inheritance graph order. */
6776 : 64297296 : for (vbase = TYPE_BINFO (t); vbase; vbase = TREE_CHAIN (vbase))
6777 : : {
6778 : 44121383 : if (!BINFO_VIRTUAL_P (vbase))
6779 : 43917711 : continue;
6780 : :
6781 : 203672 : if (!BINFO_PRIMARY_P (vbase))
6782 : : {
6783 : : /* This virtual base is not a primary base of any class in the
6784 : : hierarchy, so we have to add space for it. */
6785 : 195646 : tree access = access_private_node;
6786 : 195646 : if (publicly_virtually_derived_p (BINFO_TYPE (vbase), t))
6787 : 194787 : access = access_public_node;
6788 : 195646 : next_field = build_base_field (rli, vbase, access, offsets,
6789 : : next_field);
6790 : : }
6791 : : }
6792 : : }
6793 : :
6794 : : /* Returns the offset of the byte just past the end of the base class
6795 : : BINFO. */
6796 : :
6797 : : static tree
6798 : 41538723 : end_of_base (tree binfo)
6799 : : {
6800 : 41538723 : tree size;
6801 : :
6802 : 41538723 : if (!CLASSTYPE_AS_BASE (BINFO_TYPE (binfo)))
6803 : 0 : size = TYPE_SIZE_UNIT (char_type_node);
6804 : 41538723 : else if (is_empty_class (BINFO_TYPE (binfo)))
6805 : : /* An empty class has zero CLASSTYPE_SIZE_UNIT, but we need to
6806 : : allocate some space for it. It cannot have virtual bases, so
6807 : : TYPE_SIZE_UNIT is fine. */
6808 : 36894058 : size = TYPE_SIZE_UNIT (BINFO_TYPE (binfo));
6809 : : else
6810 : 4644665 : size = CLASSTYPE_SIZE_UNIT (BINFO_TYPE (binfo));
6811 : :
6812 : 41538723 : return size_binop (PLUS_EXPR, BINFO_OFFSET (binfo), size);
6813 : : }
6814 : :
6815 : : /* Returns one of three variations of the ending offset of T. If MODE is
6816 : : eoc_nvsize, the result is the ABI "nvsize" (i.e. sizeof before allocating
6817 : : vbases). If MODE is eoc_vsize, the result is the sizeof after allocating
6818 : : vbases but before rounding, which is not named in the ABI. If MODE is
6819 : : eoc_nv_or_dsize, the result is the greater of "nvsize" and "dsize" (the size
6820 : : of the actual data in the class, kinda), as used for allocation of
6821 : : potentially-overlapping fields. */
6822 : :
6823 : : enum eoc_mode { eoc_nvsize, eoc_vsize, eoc_nv_or_dsize };
6824 : : static tree
6825 : 77034266 : end_of_class (tree t, eoc_mode mode)
6826 : : {
6827 : 77034266 : tree result = size_zero_node;
6828 : 77034266 : vec<tree, va_gc> *vbases;
6829 : 77034266 : tree binfo;
6830 : 77034266 : tree base_binfo;
6831 : 77034266 : tree offset;
6832 : 77034266 : int i;
6833 : :
6834 : 77034266 : for (binfo = TYPE_BINFO (t), i = 0;
6835 : 118463094 : BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
6836 : : {
6837 : 41428828 : if (BINFO_VIRTUAL_P (base_binfo)
6838 : 41428828 : && (!BINFO_PRIMARY_P (base_binfo)
6839 : 3364 : || BINFO_INHERITANCE_CHAIN (base_binfo) != TYPE_BINFO (t)))
6840 : 93777 : continue;
6841 : :
6842 : 41335051 : offset = end_of_base (base_binfo);
6843 : 41335051 : if (tree_int_cst_lt (result, offset))
6844 : 41428828 : result = offset;
6845 : : }
6846 : :
6847 : 690787467 : for (tree field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
6848 : 613753201 : if (TREE_CODE (field) == FIELD_DECL
6849 : 613753201 : && !DECL_FIELD_IS_BASE (field))
6850 : : {
6851 : 25809777 : tree size = DECL_SIZE_UNIT (field);
6852 : 25809777 : if (!size)
6853 : : /* DECL_SIZE_UNIT can be null for a flexible array. */
6854 : 1985 : continue;
6855 : :
6856 : 25807792 : if (is_empty_field (field))
6857 : : /* For empty fields DECL_SIZE_UNIT is 0, but we want the
6858 : : size of the type (usually 1) for computing nvsize. */
6859 : 361232 : size = TYPE_SIZE_UNIT (TREE_TYPE (field));
6860 : :
6861 : 25807792 : if (DECL_BIT_FIELD_TYPE (field))
6862 : : {
6863 : 512261 : offset = size_binop (PLUS_EXPR, bit_position (field),
6864 : : DECL_SIZE (field));
6865 : 512261 : offset = size_binop (CEIL_DIV_EXPR, offset, bitsize_unit_node);
6866 : 512261 : offset = fold_convert (sizetype, offset);
6867 : : }
6868 : : else
6869 : 25295531 : offset = size_binop (PLUS_EXPR, byte_position (field), size);
6870 : 25807792 : if (tree_int_cst_lt (result, offset))
6871 : 613753201 : result = offset;
6872 : : }
6873 : :
6874 : 77034266 : if (mode != eoc_nvsize)
6875 : 39545577 : for (vbases = CLASSTYPE_VBASECLASSES (t), i = 0;
6876 : 39545577 : vec_safe_iterate (vbases, i, &base_binfo); i++)
6877 : : {
6878 : 203681 : if (mode == eoc_nv_or_dsize)
6879 : : /* For dsize, don't count trailing empty bases. */
6880 : 9 : offset = size_binop (PLUS_EXPR, BINFO_OFFSET (base_binfo),
6881 : : CLASSTYPE_SIZE_UNIT (BINFO_TYPE (base_binfo)));
6882 : : else
6883 : 203672 : offset = end_of_base (base_binfo);
6884 : 203681 : if (tree_int_cst_lt (result, offset))
6885 : 193330 : result = offset;
6886 : : }
6887 : :
6888 : 77034266 : return result;
6889 : : }
6890 : :
6891 : : /* Warn as appropriate about the change in whether we pack into the tail
6892 : : padding of FIELD, a base field which has a C++14 aggregate type with default
6893 : : member initializers. */
6894 : :
6895 : : static void
6896 : 310793108 : check_non_pod_aggregate (tree field)
6897 : : {
6898 : 932210774 : if ((!abi_version_crosses (17) || cxx_dialect < cxx14)
6899 : 633251872 : && (!abi_version_crosses (21) || cxx_dialect < cxx20))
6900 : : return;
6901 : 102122857 : if (TREE_CODE (field) != FIELD_DECL
6902 : 102122857 : || (!DECL_FIELD_IS_BASE (field)
6903 : 4787328 : && !field_poverlapping_p (field)))
6904 : 94456709 : return;
6905 : 7666148 : tree next = DECL_CHAIN (field);
6906 : 34576113 : while (next && TREE_CODE (next) != FIELD_DECL) next = DECL_CHAIN (next);
6907 : 7666148 : if (!next)
6908 : : return;
6909 : 512309 : tree type = TREE_TYPE (field);
6910 : 512309 : if (TYPE_IDENTIFIER (type) == as_base_identifier)
6911 : 272363 : type = TYPE_CONTEXT (type);
6912 : 492184 : if (!CLASS_TYPE_P (type)
6913 : 492182 : || is_empty_class (type)
6914 : 767065 : || (!CLASSTYPE_NON_POD_AGGREGATE (type)
6915 : 249884 : && !CLASSTYPE_NON_AGGREGATE_POD (type)))
6916 : 507433 : return;
6917 : 4896 : tree size = end_of_class (type, (DECL_FIELD_IS_BASE (field)
6918 : : ? eoc_nvsize : eoc_nv_or_dsize));
6919 : 4876 : tree rounded = round_up_loc (input_location, size, DECL_ALIGN_UNIT (next));
6920 : 4876 : if (tree_int_cst_lt (rounded, TYPE_SIZE_UNIT (type)))
6921 : : {
6922 : 28 : location_t loc = DECL_SOURCE_LOCATION (next);
6923 : 28 : if (DECL_FIELD_IS_BASE (next))
6924 : : {
6925 : 9 : if (abi_version_crosses (17)
6926 : 6 : && CLASSTYPE_NON_POD_AGGREGATE (type))
6927 : 6 : warning_at (loc, OPT_Wabi,"offset of %qT base class for "
6928 : : "%<-std=c++14%> and up changes in "
6929 : 6 : "%<-fabi-version=17%> (GCC 12)", TREE_TYPE (next));
6930 : 0 : else if (abi_version_crosses (21)
6931 : 0 : && CLASSTYPE_NON_AGGREGATE_POD (type))
6932 : 0 : warning_at (loc, OPT_Wabi,"offset of %qT base class for "
6933 : : "%<-std=c++20%> and up changes in "
6934 : 0 : "%<-fabi-version=21%> (GCC 16)", TREE_TYPE (next));
6935 : : }
6936 : : else
6937 : : {
6938 : 41 : if (abi_version_crosses (17)
6939 : 22 : && CLASSTYPE_NON_POD_AGGREGATE (type))
6940 : 12 : warning_at (loc, OPT_Wabi, "offset of %qD for "
6941 : : "%<-std=c++14%> and up changes in "
6942 : : "%<-fabi-version=17%> (GCC 12)", next);
6943 : 19 : else if (abi_version_crosses (21)
6944 : 10 : && CLASSTYPE_NON_AGGREGATE_POD (type))
6945 : 4 : warning_at (loc, OPT_Wabi, "offset of %qD for "
6946 : : "%<-std=c++20%> and up changes in "
6947 : : "%<-fabi-version=21%> (GCC 16)", next);
6948 : : }
6949 : : }
6950 : : }
6951 : :
6952 : : /* Warn about bases of T that are inaccessible because they are
6953 : : ambiguous. For example:
6954 : :
6955 : : struct S {};
6956 : : struct T : public S {};
6957 : : struct U : public S, public T {};
6958 : :
6959 : : Here, `(S*) new U' is not allowed because there are two `S'
6960 : : subobjects of U. */
6961 : :
6962 : : static void
6963 : 39307783 : maybe_warn_about_inaccessible_bases (tree t)
6964 : : {
6965 : 39307783 : int i;
6966 : 39307783 : vec<tree, va_gc> *vbases;
6967 : 39307783 : tree basetype;
6968 : 39307783 : tree binfo;
6969 : 39307783 : tree base_binfo;
6970 : :
6971 : : /* If not checking for warning then return early. */
6972 : 39307783 : if (!warn_inaccessible_base)
6973 : 39307783 : return;
6974 : :
6975 : : /* If there are no repeated bases, nothing can be ambiguous. */
6976 : 39307651 : if (!CLASSTYPE_REPEATED_BASE_P (t))
6977 : : return;
6978 : :
6979 : : /* Check direct bases. */
6980 : 3809 : for (binfo = TYPE_BINFO (t), i = 0;
6981 : 3809 : BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
6982 : : {
6983 : 2756 : basetype = BINFO_TYPE (base_binfo);
6984 : :
6985 : 2756 : if (!uniquely_derived_from_p (basetype, t))
6986 : 781 : warning (OPT_Winaccessible_base, "direct base %qT inaccessible "
6987 : : "in %qT due to ambiguity", basetype, t);
6988 : : }
6989 : :
6990 : : /* Check for ambiguous virtual bases. */
6991 : 1053 : if (extra_warnings)
6992 : 6 : for (vbases = CLASSTYPE_VBASECLASSES (t), i = 0;
6993 : 6 : vec_safe_iterate (vbases, i, &binfo); i++)
6994 : : {
6995 : 3 : basetype = BINFO_TYPE (binfo);
6996 : :
6997 : 3 : if (!uniquely_derived_from_p (basetype, t))
6998 : 3 : warning (OPT_Winaccessible_base, "virtual base %qT inaccessible in "
6999 : : "%qT due to ambiguity", basetype, t);
7000 : : }
7001 : : }
7002 : :
7003 : : /* Compare two INTEGER_CSTs K1 and K2. */
7004 : :
7005 : : static int
7006 : 8676237 : splay_tree_compare_integer_csts (splay_tree_key k1, splay_tree_key k2)
7007 : : {
7008 : 8676237 : return tree_int_cst_compare ((tree) k1, (tree) k2);
7009 : : }
7010 : :
7011 : : /* Increase the size indicated in RLI to account for empty classes
7012 : : that are "off the end" of the class. */
7013 : :
7014 : : static void
7015 : 39307783 : include_empty_classes (record_layout_info rli)
7016 : : {
7017 : 39307783 : tree eoc;
7018 : 39307783 : tree rli_size;
7019 : :
7020 : : /* It might be the case that we grew the class to allocate a
7021 : : zero-sized base class. That won't be reflected in RLI, yet,
7022 : : because we are willing to overlay multiple bases at the same
7023 : : offset. However, now we need to make sure that RLI is big enough
7024 : : to reflect the entire class. */
7025 : 39307783 : eoc = end_of_class (rli->t, eoc_vsize);
7026 : 39307783 : rli_size = rli_size_unit_so_far (rli);
7027 : 39307783 : if (TREE_CODE (rli_size) == INTEGER_CST
7028 : 39307783 : && tree_int_cst_lt (rli_size, eoc))
7029 : : {
7030 : : /* The size should have been rounded to a whole byte. */
7031 : 17727927 : gcc_assert (tree_int_cst_equal
7032 : : (rli->bitpos, round_down (rli->bitpos, BITS_PER_UNIT)));
7033 : 17727927 : rli->bitpos
7034 : 17727927 : = size_binop (PLUS_EXPR,
7035 : : rli->bitpos,
7036 : : size_binop (MULT_EXPR,
7037 : : fold_convert (bitsizetype,
7038 : : size_binop (MINUS_EXPR,
7039 : : eoc, rli_size)),
7040 : : bitsize_int (BITS_PER_UNIT)));
7041 : 17727927 : normalize_rli (rli);
7042 : : }
7043 : 39307783 : }
7044 : :
7045 : : /* Calculate the TYPE_SIZE, TYPE_ALIGN, etc for T. Calculate
7046 : : BINFO_OFFSETs for all of the base-classes. Position the vtable
7047 : : pointer. Accumulate declared virtual functions on VIRTUALS_P. */
7048 : :
7049 : : static void
7050 : 39307783 : layout_class_type (tree t, tree *virtuals_p)
7051 : : {
7052 : 39307783 : tree non_static_data_members;
7053 : 39307783 : tree field;
7054 : 39307783 : tree vptr;
7055 : 39307783 : record_layout_info rli;
7056 : : /* Maps offsets (represented as INTEGER_CSTs) to a TREE_LIST of
7057 : : types that appear at that offset. */
7058 : 39307783 : splay_tree empty_base_offsets;
7059 : : /* True if the last field laid out was a bit-field. */
7060 : 39307783 : bool last_field_was_bitfield = false;
7061 : : /* The location at which the next field should be inserted. */
7062 : 39307783 : tree *next_field;
7063 : :
7064 : : /* Keep track of the first non-static data member. */
7065 : 39307783 : non_static_data_members = TYPE_FIELDS (t);
7066 : :
7067 : : /* Start laying out the record. */
7068 : 39307783 : rli = start_record_layout (t);
7069 : :
7070 : : /* Mark all the primary bases in the hierarchy. */
7071 : 39307783 : determine_primary_bases (t);
7072 : :
7073 : : /* Create a pointer to our virtual function table. */
7074 : 39307783 : vptr = create_vtable_ptr (t, virtuals_p);
7075 : :
7076 : : /* The vptr is always the first thing in the class. */
7077 : 39307783 : if (vptr)
7078 : : {
7079 : 243916 : DECL_CHAIN (vptr) = TYPE_FIELDS (t);
7080 : 243916 : TYPE_FIELDS (t) = vptr;
7081 : 243916 : next_field = &DECL_CHAIN (vptr);
7082 : 243916 : place_field (rli, vptr);
7083 : : }
7084 : : else
7085 : 39063867 : next_field = &TYPE_FIELDS (t);
7086 : :
7087 : : /* Build FIELD_DECLs for all of the non-virtual base-types. */
7088 : 39307783 : empty_base_offsets = splay_tree_new (splay_tree_compare_integer_csts,
7089 : : NULL, NULL);
7090 : 39307783 : build_base_fields (rli, empty_base_offsets, next_field);
7091 : :
7092 : : /* Layout the non-static data members. */
7093 : 329263540 : for (field = non_static_data_members; field; field = DECL_CHAIN (field))
7094 : : {
7095 : 289955757 : tree type;
7096 : 289955757 : tree padding;
7097 : :
7098 : : /* We still pass things that aren't non-static data members to
7099 : : the back end, in case it wants to do something with them. */
7100 : 289955757 : if (TREE_CODE (field) != FIELD_DECL)
7101 : : {
7102 : 274109081 : place_field (rli, field);
7103 : : /* If the static data member has incomplete type, keep track
7104 : : of it so that it can be completed later. (The handling
7105 : : of pending statics in finish_record_layout is
7106 : : insufficient; consider:
7107 : :
7108 : : struct S1;
7109 : : struct S2 { static S1 s1; };
7110 : :
7111 : : At this point, finish_record_layout will be called, but
7112 : : S1 is still incomplete.) */
7113 : 274109081 : if (VAR_P (field))
7114 : : {
7115 : 10624338 : maybe_register_incomplete_var (field);
7116 : : /* The visibility of static data members is determined
7117 : : at their point of declaration, not their point of
7118 : : definition. */
7119 : 10624338 : determine_visibility (field);
7120 : : }
7121 : 274109081 : continue;
7122 : : }
7123 : :
7124 : 15846676 : type = TREE_TYPE (field);
7125 : 15846676 : if (type == error_mark_node)
7126 : 161 : continue;
7127 : :
7128 : 15846515 : padding = NULL_TREE;
7129 : :
7130 : 15846515 : bool might_overlap = field_poverlapping_p (field);
7131 : :
7132 : 237602 : if (might_overlap && CLASS_TYPE_P (type)
7133 : 16061115 : && (CLASSTYPE_NON_LAYOUT_POD_P (type) || CLASSTYPE_EMPTY_P (type)))
7134 : : {
7135 : : /* if D is a potentially-overlapping data member, update sizeof(C) to
7136 : : max (sizeof(C), offset(D)+max (nvsize(D), dsize(D))). */
7137 : 214446 : if (CLASSTYPE_EMPTY_P (type))
7138 : 180353 : DECL_SIZE (field) = DECL_SIZE_UNIT (field) = size_zero_node;
7139 : : else
7140 : : {
7141 : 34093 : tree size = end_of_class (type, eoc_nv_or_dsize);
7142 : 34093 : DECL_SIZE_UNIT (field) = size;
7143 : 34093 : DECL_SIZE (field) = bit_from_pos (size, bitsize_zero_node);
7144 : : }
7145 : : }
7146 : :
7147 : : /* If this field is a bit-field whose width is greater than its
7148 : : type, then there are some special rules for allocating
7149 : : it. */
7150 : 15846515 : if (DECL_C_BIT_FIELD (field)
7151 : 15846515 : && tree_int_cst_lt (TYPE_SIZE (type), DECL_SIZE (field)))
7152 : : {
7153 : 774 : bool was_unnamed_p = false;
7154 : : /* We must allocate the bits as if suitably aligned for the
7155 : : longest integer type that fits in this many bits. Then,
7156 : : we are supposed to use the left over bits as additional
7157 : : padding. */
7158 : :
7159 : : /* Do not pick a type bigger than MAX_FIXED_MODE_SIZE. */
7160 : 1548 : tree limit = size_int (MAX_FIXED_MODE_SIZE);
7161 : 774 : if (tree_int_cst_lt (DECL_SIZE (field), limit))
7162 : 753 : limit = DECL_SIZE (field);
7163 : :
7164 : 774 : tree integer_type = integer_types[itk_char];
7165 : 6620 : for (unsigned itk = itk_char; itk != itk_none; itk++)
7166 : 6599 : if (tree next = integer_types[itk])
7167 : : {
7168 : 6473 : if (tree_int_cst_lt (limit, TYPE_SIZE (next)))
7169 : : /* Too big, so our current guess is what we want. */
7170 : : break;
7171 : : /* Not bigger than limit, ok */
7172 : : integer_type = next;
7173 : : }
7174 : :
7175 : : /* Figure out how much additional padding is required. */
7176 : 774 : if (TREE_CODE (t) == UNION_TYPE)
7177 : : /* In a union, the padding field must have the full width
7178 : : of the bit-field; all fields start at offset zero. */
7179 : 77 : padding = DECL_SIZE (field);
7180 : : else
7181 : 697 : padding = size_binop (MINUS_EXPR, DECL_SIZE (field),
7182 : : TYPE_SIZE (integer_type));
7183 : :
7184 : 774 : if (integer_zerop (padding))
7185 : 6 : padding = NULL_TREE;
7186 : :
7187 : : /* An unnamed bitfield does not normally affect the
7188 : : alignment of the containing class on a target where
7189 : : PCC_BITFIELD_TYPE_MATTERS. But, the C++ ABI does not
7190 : : make any exceptions for unnamed bitfields when the
7191 : : bitfields are longer than their types. Therefore, we
7192 : : temporarily give the field a name. */
7193 : 774 : if (PCC_BITFIELD_TYPE_MATTERS && !DECL_NAME (field))
7194 : : {
7195 : 0 : was_unnamed_p = true;
7196 : 0 : DECL_NAME (field) = make_anon_name ();
7197 : : }
7198 : :
7199 : 774 : DECL_SIZE (field) = TYPE_SIZE (integer_type);
7200 : 774 : SET_DECL_ALIGN (field, TYPE_ALIGN (integer_type));
7201 : 774 : DECL_USER_ALIGN (field) = TYPE_USER_ALIGN (integer_type);
7202 : 774 : layout_nonempty_base_or_field (rli, field, NULL_TREE,
7203 : : empty_base_offsets);
7204 : 774 : if (was_unnamed_p)
7205 : 0 : DECL_NAME (field) = NULL_TREE;
7206 : : /* Now that layout has been performed, set the size of the
7207 : : field to the size of its declared type; the rest of the
7208 : : field is effectively invisible. */
7209 : 774 : DECL_SIZE (field) = TYPE_SIZE (type);
7210 : : /* We must also reset the DECL_MODE of the field. */
7211 : 774 : SET_DECL_MODE (field, TYPE_MODE (type));
7212 : : }
7213 : 15845741 : else if (might_overlap && is_empty_class (type))
7214 : : {
7215 : 180353 : SET_DECL_FIELD_ABI_IGNORED (field, 1);
7216 : 180353 : layout_empty_base_or_field (rli, field, empty_base_offsets);
7217 : : }
7218 : : else
7219 : 15665388 : layout_nonempty_base_or_field (rli, field, NULL_TREE,
7220 : : empty_base_offsets);
7221 : :
7222 : : /* Remember the location of any empty classes in FIELD. */
7223 : 15846515 : record_subobject_offsets (field, empty_base_offsets);
7224 : :
7225 : : /* If a bit-field does not immediately follow another bit-field,
7226 : : and yet it starts in the middle of a byte, we have failed to
7227 : : comply with the ABI. */
7228 : 15846515 : if (warn_abi
7229 : 14533132 : && DECL_C_BIT_FIELD (field)
7230 : : /* The TREE_NO_WARNING flag gets set by Objective-C when
7231 : : laying out an Objective-C class. The ObjC ABI differs
7232 : : from the C++ ABI, and so we do not want a warning
7233 : : here. */
7234 : 379638 : && !warning_suppressed_p (field, OPT_Wabi)
7235 : 379638 : && !last_field_was_bitfield
7236 : 15909328 : && !integer_zerop (size_binop (TRUNC_MOD_EXPR,
7237 : : DECL_FIELD_BIT_OFFSET (field),
7238 : : bitsize_unit_node)))
7239 : 0 : warning_at (DECL_SOURCE_LOCATION (field), OPT_Wabi,
7240 : : "offset of %qD is not ABI-compliant and may "
7241 : : "change in a future version of GCC", field);
7242 : :
7243 : : /* The middle end uses the type of expressions to determine the
7244 : : possible range of expression values. In order to optimize
7245 : : "x.i > 7" to "false" for a 2-bit bitfield "i", the middle end
7246 : : must be made aware of the width of "i", via its type.
7247 : :
7248 : : Because C++ does not have integer types of arbitrary width,
7249 : : we must (for the purposes of the front end) convert from the
7250 : : type assigned here to the declared type of the bitfield
7251 : : whenever a bitfield expression is used as an rvalue.
7252 : : Similarly, when assigning a value to a bitfield, the value
7253 : : must be converted to the type given the bitfield here. */
7254 : 15846515 : if (DECL_C_BIT_FIELD (field))
7255 : : {
7256 : 426328 : unsigned HOST_WIDE_INT width;
7257 : 426328 : tree ftype = TREE_TYPE (field);
7258 : 426328 : width = tree_to_uhwi (DECL_SIZE (field));
7259 : 426328 : if (width != TYPE_PRECISION (ftype))
7260 : : {
7261 : 285909 : TREE_TYPE (field)
7262 : 571818 : = c_build_bitfield_integer_type (width,
7263 : 285909 : TYPE_UNSIGNED (ftype));
7264 : 285909 : TREE_TYPE (field)
7265 : 571818 : = cp_build_qualified_type (TREE_TYPE (field),
7266 : : cp_type_quals (ftype));
7267 : : }
7268 : : }
7269 : :
7270 : : /* If we needed additional padding after this field, add it
7271 : : now. */
7272 : 15846515 : if (padding)
7273 : : {
7274 : 768 : tree padding_field;
7275 : :
7276 : 768 : padding_field = build_decl (input_location,
7277 : : FIELD_DECL,
7278 : : NULL_TREE,
7279 : : char_type_node);
7280 : 768 : DECL_BIT_FIELD (padding_field) = 1;
7281 : 768 : DECL_SIZE (padding_field) = padding;
7282 : 768 : DECL_CONTEXT (padding_field) = t;
7283 : 768 : DECL_ARTIFICIAL (padding_field) = 1;
7284 : 768 : DECL_IGNORED_P (padding_field) = 1;
7285 : 768 : DECL_PADDING_P (padding_field) = 1;
7286 : 768 : layout_nonempty_base_or_field (rli, padding_field,
7287 : : NULL_TREE,
7288 : : empty_base_offsets);
7289 : : }
7290 : :
7291 : 15846515 : last_field_was_bitfield = DECL_C_BIT_FIELD (field);
7292 : : }
7293 : :
7294 : 39307783 : if (!integer_zerop (rli->bitpos))
7295 : : {
7296 : : /* Make sure that we are on a byte boundary so that the size of
7297 : : the class without virtual bases will always be a round number
7298 : : of bytes. */
7299 : 4583354 : rli->bitpos = round_up_loc (input_location, rli->bitpos, BITS_PER_UNIT);
7300 : 4583354 : normalize_rli (rli);
7301 : : }
7302 : :
7303 : : /* We used to remove zero width bitfields at this point since PR42217,
7304 : : while the C FE never did that. That caused ABI differences on various
7305 : : targets. Set the DECL_FIELD_CXX_ZERO_WIDTH_BIT_FIELD flag on them
7306 : : instead, so that the backends can emit -Wpsabi warnings in the cases
7307 : : where the ABI changed. */
7308 : 350100891 : for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
7309 : : {
7310 : 310793108 : if (TREE_CODE (field) == FIELD_DECL
7311 : 36684027 : && DECL_C_BIT_FIELD (field)
7312 : : /* We should not be confused by the fact that grokbitfield
7313 : : temporarily sets the width of the bit field into
7314 : : DECL_BIT_FIELD_REPRESENTATIVE (field).
7315 : : check_bitfield_decl eventually sets DECL_SIZE (field)
7316 : : to that width. */
7317 : 426328 : && (DECL_SIZE (field) == NULL_TREE
7318 : 426328 : || integer_zerop (DECL_SIZE (field)))
7319 : 310794245 : && TREE_TYPE (field) != error_mark_node)
7320 : 1137 : SET_DECL_FIELD_CXX_ZERO_WIDTH_BIT_FIELD (field, 1);
7321 : 310793108 : check_non_pod_aggregate (field);
7322 : : }
7323 : :
7324 : 39307783 : if (CLASSTYPE_NON_LAYOUT_POD_P (t) || CLASSTYPE_EMPTY_P (t))
7325 : : {
7326 : : /* T needs a different layout as a base (eliding virtual bases
7327 : : or whatever). Create that version. */
7328 : 37687514 : tree base_t = make_node (TREE_CODE (t));
7329 : 37687514 : tree base_d = create_implicit_typedef (as_base_identifier, base_t);
7330 : :
7331 : 37687514 : TYPE_CONTEXT (base_t) = t;
7332 : 37687514 : DECL_CONTEXT (base_d) = t;
7333 : :
7334 : 37687514 : set_instantiating_module (base_d);
7335 : :
7336 : : /* If the ABI version is not at least two, and the last
7337 : : field was a bit-field, RLI may not be on a byte
7338 : : boundary. In particular, rli_size_unit_so_far might
7339 : : indicate the last complete byte, while rli_size_so_far
7340 : : indicates the total number of bits used. Therefore,
7341 : : rli_size_so_far, rather than rli_size_unit_so_far, is
7342 : : used to compute TYPE_SIZE_UNIT. */
7343 : :
7344 : : /* Set the size and alignment for the new type. */
7345 : 37687514 : tree eoc = end_of_class (t, eoc_nvsize);
7346 : 37687514 : TYPE_SIZE_UNIT (base_t)
7347 : 37687514 : = size_binop (MAX_EXPR,
7348 : : fold_convert (sizetype,
7349 : : size_binop (CEIL_DIV_EXPR,
7350 : : rli_size_so_far (rli),
7351 : : bitsize_int (BITS_PER_UNIT))),
7352 : : eoc);
7353 : 37687514 : TYPE_SIZE (base_t)
7354 : 37687514 : = size_binop (MAX_EXPR,
7355 : : rli_size_so_far (rli),
7356 : : size_binop (MULT_EXPR,
7357 : : fold_convert (bitsizetype, eoc),
7358 : : bitsize_int (BITS_PER_UNIT)));
7359 : 37687514 : SET_TYPE_ALIGN (base_t, rli->record_align);
7360 : 37687514 : TYPE_USER_ALIGN (base_t) = TYPE_USER_ALIGN (t);
7361 : 37687514 : TYPE_TYPELESS_STORAGE (base_t) = TYPE_TYPELESS_STORAGE (t);
7362 : 37687514 : TYPE_CXX_ODR_P (base_t) = TYPE_CXX_ODR_P (t);
7363 : :
7364 : : /* Copy the non-static data members of T. This will include its
7365 : : direct non-virtual bases & vtable. */
7366 : 37687514 : next_field = &TYPE_FIELDS (base_t);
7367 : 339443376 : for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
7368 : 301755862 : if (TREE_CODE (field) == FIELD_DECL)
7369 : : {
7370 : 30253758 : *next_field = copy_node (field);
7371 : : /* Zap any NSDMI, it's not needed and might be a deferred
7372 : : parse. */
7373 : 30253758 : DECL_INITIAL (*next_field) = NULL_TREE;
7374 : 30253758 : DECL_CONTEXT (*next_field) = base_t;
7375 : 30253758 : next_field = &DECL_CHAIN (*next_field);
7376 : : }
7377 : 37687514 : *next_field = NULL_TREE;
7378 : :
7379 : : /* We use the base type for trivial assignments, and hence it
7380 : : needs a mode. */
7381 : 37687514 : compute_record_mode (base_t);
7382 : :
7383 : : /* Record the base version of the type. */
7384 : 37687514 : CLASSTYPE_AS_BASE (t) = base_t;
7385 : : }
7386 : : else
7387 : 1620269 : CLASSTYPE_AS_BASE (t) = t;
7388 : :
7389 : : /* Every empty class contains an empty class. */
7390 : 39307783 : if (CLASSTYPE_EMPTY_P (t))
7391 : 31915336 : CLASSTYPE_CONTAINS_EMPTY_CLASS_P (t) = 1;
7392 : :
7393 : : /* Set the TYPE_DECL for this type to contain the right
7394 : : value for DECL_OFFSET, so that we can use it as part
7395 : : of a COMPONENT_REF for multiple inheritance. */
7396 : 39307783 : layout_decl (TYPE_MAIN_DECL (t), 0);
7397 : :
7398 : : /* Now fix up any virtual base class types that we left lying
7399 : : around. We must get these done before we try to lay out the
7400 : : virtual function table. As a side-effect, this will remove the
7401 : : base subobject fields. */
7402 : 39307783 : layout_virtual_bases (rli, empty_base_offsets);
7403 : :
7404 : : /* Make sure that empty classes are reflected in RLI at this
7405 : : point. */
7406 : 39307783 : include_empty_classes (rli);
7407 : :
7408 : : /* Make sure not to create any structures with zero size. */
7409 : 39307783 : if (integer_zerop (rli_size_unit_so_far (rli)) && CLASSTYPE_EMPTY_P (t))
7410 : 14187632 : place_field (rli,
7411 : : build_decl (input_location,
7412 : : FIELD_DECL, NULL_TREE, char_type_node));
7413 : :
7414 : : /* If this is a non-POD, declaring it packed makes a difference to how it
7415 : : can be used as a field; don't let finalize_record_size undo it. */
7416 : 39307783 : if (TYPE_PACKED (t) && !layout_pod_type_p (t))
7417 : 78 : rli->packed_maybe_necessary = true;
7418 : :
7419 : : /* Let the back end lay out the type. */
7420 : 39307783 : finish_record_layout (rli, /*free_p=*/true);
7421 : :
7422 : : /* If we didn't end up needing an as-base type, don't use it. */
7423 : 39307783 : if (CLASSTYPE_AS_BASE (t) != t
7424 : : /* If T's CLASSTYPE_AS_BASE is TYPE_USER_ALIGN, but T is not,
7425 : : replacing the as-base type would change CLASSTYPE_USER_ALIGN,
7426 : : causing us to lose the user-specified alignment as in PR94050. */
7427 : 37687514 : && TYPE_USER_ALIGN (t) == TYPE_USER_ALIGN (CLASSTYPE_AS_BASE (t))
7428 : 76995283 : && tree_int_cst_equal (TYPE_SIZE (t),
7429 : 37687500 : TYPE_SIZE (CLASSTYPE_AS_BASE (t))))
7430 : 22590785 : CLASSTYPE_AS_BASE (t) = t;
7431 : :
7432 : 39307783 : if (TYPE_SIZE_UNIT (t)
7433 : 39307783 : && TREE_CODE (TYPE_SIZE_UNIT (t)) == INTEGER_CST
7434 : 39307783 : && !TREE_OVERFLOW (TYPE_SIZE_UNIT (t))
7435 : 78615557 : && !valid_constant_size_p (TYPE_SIZE_UNIT (t)))
7436 : 0 : error ("size of type %qT is too large (%qE bytes)", t, TYPE_SIZE_UNIT (t));
7437 : :
7438 : : /* Warn about bases that can't be talked about due to ambiguity. */
7439 : 39307783 : maybe_warn_about_inaccessible_bases (t);
7440 : :
7441 : : /* Now that we're done with layout, give the base fields the real types. */
7442 : 350294347 : for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
7443 : 310986564 : if (DECL_ARTIFICIAL (field) && IS_FAKE_BASE_TYPE (TREE_TYPE (field)))
7444 : 18254559 : TREE_TYPE (field) = TYPE_CONTEXT (TREE_TYPE (field));
7445 : :
7446 : : /* Clean up. */
7447 : 39307783 : splay_tree_delete (empty_base_offsets);
7448 : :
7449 : 39307783 : if (CLASSTYPE_EMPTY_P (t)
7450 : 71223119 : && tree_int_cst_lt (sizeof_biggest_empty_class,
7451 : 31915336 : TYPE_SIZE_UNIT (t)))
7452 : 60271 : sizeof_biggest_empty_class = TYPE_SIZE_UNIT (t);
7453 : 39307783 : }
7454 : :
7455 : : /* Determine the "key method" for the class type indicated by TYPE,
7456 : : and set CLASSTYPE_KEY_METHOD accordingly. */
7457 : :
7458 : : void
7459 : 1686331 : determine_key_method (tree type)
7460 : : {
7461 : 1686331 : tree method;
7462 : :
7463 : 1686331 : if (processing_template_decl
7464 : 1686331 : || CLASSTYPE_TEMPLATE_INSTANTIATION (type)
7465 : 2420015 : || CLASSTYPE_INTERFACE_KNOWN (type))
7466 : : return;
7467 : :
7468 : : /* The key method is the first non-pure virtual function that is not
7469 : : inline at the point of class definition. On some targets the
7470 : : key function may not be inline; those targets should not call
7471 : : this function until the end of the translation unit. */
7472 : 7573955 : for (method = TYPE_FIELDS (type); method; method = DECL_CHAIN (method))
7473 : 7398751 : if (TREE_CODE (method) == FUNCTION_DECL
7474 : 6046475 : && DECL_VINDEX (method) != NULL_TREE
7475 : 1211706 : && ! DECL_DECLARED_INLINE_P (method)
7476 : 7979260 : && ! DECL_PURE_VIRTUAL_P (method))
7477 : : {
7478 : 558451 : SET_CLASSTYPE_KEY_METHOD (type, method);
7479 : 558451 : break;
7480 : : }
7481 : :
7482 : : return;
7483 : : }
7484 : :
7485 : : /* Helper of find_flexarrays. Return true when FLD refers to a non-static
7486 : : class data member of non-zero size, otherwise false. */
7487 : :
7488 : : static inline bool
7489 : 39349777 : field_nonempty_p (const_tree fld)
7490 : : {
7491 : 39349777 : if (TREE_CODE (fld) == ERROR_MARK)
7492 : : return false;
7493 : :
7494 : 39349777 : tree type = TREE_TYPE (fld);
7495 : 39349777 : if (TREE_CODE (fld) == FIELD_DECL
7496 : 39349777 : && TREE_CODE (type) != ERROR_MARK
7497 : 78699554 : && (DECL_NAME (fld) || RECORD_OR_UNION_TYPE_P (type)))
7498 : 39213897 : return TYPE_SIZE (type) && !integer_zerop (TYPE_SIZE (type));
7499 : :
7500 : : return false;
7501 : : }
7502 : :
7503 : : /* Used by find_flexarrays and related functions. */
7504 : :
7505 : : struct flexmems_t
7506 : : {
7507 : : /* The first flexible array member or non-zero array member found
7508 : : in the order of layout. */
7509 : : tree array;
7510 : : /* True if there is a non-static non-empty data member in the class or
7511 : : its bases. */
7512 : : bool first;
7513 : : /* The first non-static non-empty data member following either
7514 : : the flexible array member, if found, or the zero-length array member
7515 : : otherwise. AFTER[1] refers to the first such data member of a union
7516 : : of which the struct containing the flexible array member or zero-length
7517 : : array is a member, or NULL when no such union exists. This element is
7518 : : only used during searching, not for diagnosing problems. AFTER[0]
7519 : : refers to the first such data member that is not a member of such
7520 : : a union. */
7521 : : tree after[2];
7522 : :
7523 : : /* Refers to a struct (not union) in which the struct of which the flexible
7524 : : array is member is defined. Used to diagnose strictly (according to C)
7525 : : invalid uses of the latter structs. */
7526 : : tree enclosing;
7527 : : };
7528 : :
7529 : : /* Find either the first flexible array member or the first zero-length
7530 : : array, in that order of preference, among members of class T (but not
7531 : : its base classes), and set members of FMEM accordingly.
7532 : : BASE_P is true if T is a base class of another class.
7533 : : PUN is true when inside of a union (perhaps recursively).
7534 : : PSTR is set to a data member of the outermost struct of
7535 : : which the flexible array is a member if one such struct exists,
7536 : : otherwise to NULL. NESTED_P is true for recursive calls except ones
7537 : : handling anonymous aggregates. Those types are expected to be diagnosed
7538 : : on its own already and so only the last member is checked vs. what
7539 : : follows it in the outer type. */
7540 : :
7541 : : static void
7542 : 68760181 : find_flexarrays (tree t, flexmems_t *fmem, bool base_p,
7543 : : bool nested_p /* = false */, bool pun /* = false */,
7544 : : tree pstr /* = NULL_TREE */)
7545 : : {
7546 : 68760181 : if (TREE_CODE (t) == UNION_TYPE)
7547 : 1196129 : pun = true;
7548 : :
7549 : 68760181 : tree fld = TYPE_FIELDS (t);
7550 : 68760181 : if ((base_p || nested_p) && TREE_CODE (t) == RECORD_TYPE)
7551 : : {
7552 : : /* In bases or in nested structures, only process the last
7553 : : non-static data member. If we have say
7554 : : struct A { int a; int b[]; int c; };
7555 : : struct B { int d; int e[]; int f; };
7556 : : struct C : A { int g; B h, i; int j; };
7557 : : then the A::b followed by A::c should have been diagnosed
7558 : : already when completing struct A, and B::e followed by B::f
7559 : : when completing struct B, so no need to repeat that when completing
7560 : : struct C. So, only look at the last member so we cover e.g.
7561 : : struct D { int k; int l[]; };
7562 : : struct E : D { int m; };
7563 : : struct F { D n; int o; };
7564 : : where flexible array member at the end of D is fine, but it isn't
7565 : : correct that E::m in E or F::o in F follows it. */
7566 : : tree last_fld = NULL_TREE;
7567 : 52229391 : for (; (fld = next_subobject_field (fld)); fld = DECL_CHAIN (fld))
7568 : 23616412 : if (DECL_ARTIFICIAL (fld))
7569 : 6722732 : continue;
7570 : 16893680 : else if (TREE_TYPE (fld) == error_mark_node)
7571 : : return;
7572 : 16893659 : else if (TREE_CODE (TREE_TYPE (fld)) == ARRAY_TYPE
7573 : 16893659 : || field_nonempty_p (fld))
7574 : : last_fld = fld;
7575 : : fld = last_fld;
7576 : : }
7577 : 544114080 : for (; fld; fld = DECL_CHAIN (fld))
7578 : : {
7579 : 475354486 : if (fld == error_mark_node)
7580 : : return;
7581 : :
7582 : : /* Is FLD a typedef for an anonymous struct? */
7583 : :
7584 : : /* FIXME: Note that typedefs (as well as arrays) need to be fully
7585 : : handled elsewhere so that errors like the following are detected
7586 : : as well:
7587 : : typedef struct { int i, a[], j; } S; // bug c++/72753
7588 : : S s [2]; // bug c++/68489
7589 : : */
7590 : 476087700 : if (TREE_CODE (fld) == TYPE_DECL
7591 : 106493885 : && DECL_IMPLICIT_TYPEDEF_P (fld)
7592 : 7266807 : && CLASS_TYPE_P (TREE_TYPE (fld))
7593 : 480763531 : && IDENTIFIER_ANON_P (DECL_NAME (fld)))
7594 : : {
7595 : : /* Check the nested unnamed type referenced via a typedef
7596 : : independently of FMEM (since it's not a data member of
7597 : : the enclosing class). */
7598 : 733214 : check_flexarrays (TREE_TYPE (fld));
7599 : 733214 : continue;
7600 : : }
7601 : :
7602 : : /* Skip anything that's GCC-generated or not a (non-static) data
7603 : : member. */
7604 : 474621272 : if (DECL_ARTIFICIAL (fld) || TREE_CODE (fld) != FIELD_DECL)
7605 : 450413947 : continue;
7606 : :
7607 : : /* Type of the member. */
7608 : 24207325 : tree fldtype = TREE_TYPE (fld);
7609 : 24207325 : if (fldtype == error_mark_node)
7610 : : return;
7611 : :
7612 : : /* Determine the type of the array element or object referenced
7613 : : by the member so that it can be checked for flexible array
7614 : : members if it hasn't been yet. */
7615 : : tree eltype = fldtype;
7616 : 33404529 : while (TREE_CODE (eltype) == ARRAY_TYPE
7617 : 33404529 : || INDIRECT_TYPE_P (eltype))
7618 : 9197345 : eltype = TREE_TYPE (eltype);
7619 : :
7620 : 24207184 : if (RECORD_OR_UNION_TYPE_P (eltype))
7621 : : {
7622 : 8954963 : if (fmem->array && !fmem->after[pun])
7623 : : {
7624 : : /* Once the member after the flexible array has been found
7625 : : we're done. */
7626 : 425 : fmem->after[pun] = fld;
7627 : 425 : break;
7628 : : }
7629 : :
7630 : 12147902 : if (eltype == fldtype || TYPE_UNNAMED_P (eltype))
7631 : : {
7632 : : /* Descend into the non-static member struct or union and try
7633 : : to find a flexible array member or zero-length array among
7634 : : its members. This is only necessary for anonymous types
7635 : : and types in whose context the current type T has not been
7636 : : defined (the latter must not be checked again because they
7637 : : are already in the process of being checked by one of the
7638 : : recursive calls). */
7639 : :
7640 : 5793331 : bool first = fmem->first;
7641 : 5793331 : tree array = fmem->array;
7642 : 12276985 : bool maybe_anon_p = TYPE_UNNAMED_P (eltype);
7643 : 720388 : if (tree ctx = maybe_anon_p ? TYPE_CONTEXT (eltype) : NULL_TREE)
7644 : 720388 : maybe_anon_p = RECORD_OR_UNION_TYPE_P (ctx);
7645 : :
7646 : : /* If this member isn't anonymous and a prior non-flexible array
7647 : : member has been seen in one of the enclosing structs, clear
7648 : : the FIRST member since it doesn't contribute to the flexible
7649 : : array struct's members. */
7650 : 5793331 : if (first && !array && !ANON_AGGR_TYPE_P (eltype))
7651 : 1760557 : fmem->first = false;
7652 : :
7653 : 9873390 : find_flexarrays (eltype, fmem, false, !maybe_anon_p, pun,
7654 : 4080059 : !pstr && TREE_CODE (t) == RECORD_TYPE
7655 : : ? fld : pstr);
7656 : :
7657 : 5793331 : if (fmem->array != array)
7658 : : {
7659 : : /* If the recursive call passed true to nested_p,
7660 : : it only looked at the last field and we do not
7661 : : want to diagnose in that case the "in otherwise empty"
7662 : : case, just if it is followed by some other non-empty
7663 : : member. So set fmem->first. */
7664 : 1033 : if (!maybe_anon_p)
7665 : 252 : fmem->first = true;
7666 : 1033 : continue;
7667 : : }
7668 : :
7669 : 5792298 : if (first && !array && !ANON_AGGR_TYPE_P (eltype))
7670 : : /* Restore the FIRST member reset above if no flexible
7671 : : array member has been found in this member's struct. */
7672 : 1760177 : fmem->first = first;
7673 : :
7674 : : /* If the member struct contains the first flexible array
7675 : : member, or if this member is a base class, continue to
7676 : : the next member and avoid setting the FMEM->AFTER pointer
7677 : : to point to it. */
7678 : 5792298 : if (base_p)
7679 : 1058567 : continue;
7680 : : }
7681 : : }
7682 : :
7683 : 23147159 : if (field_nonempty_p (fld))
7684 : : {
7685 : : /* Remember the first non-static data member. */
7686 : 23006916 : if (!fmem->first)
7687 : 8580746 : fmem->first = true;
7688 : :
7689 : : /* Remember the first non-static data member after the flexible
7690 : : array member, if one has been found, or the zero-length array
7691 : : if it has been found. */
7692 : 23006916 : if (fmem->array && !fmem->after[pun])
7693 : 1507 : fmem->after[pun] = fld;
7694 : : }
7695 : :
7696 : : /* Skip non-arrays. */
7697 : 23147159 : if (TREE_CODE (fldtype) != ARRAY_TYPE)
7698 : 21292507 : continue;
7699 : :
7700 : : /* Determine the upper bound of the array if it has one. */
7701 : 1854652 : if (TYPE_DOMAIN (fldtype))
7702 : : {
7703 : 1853033 : if (fmem->array)
7704 : : {
7705 : : /* Make a record of the zero-length array if either one
7706 : : such field or a flexible array member has been seen to
7707 : : handle the pathological and unlikely case of multiple
7708 : : such members. */
7709 : 1040 : if (!fmem->after[pun])
7710 : 83 : fmem->after[pun] = fld;
7711 : : }
7712 : 1851993 : else if (integer_all_onesp (TYPE_MAX_VALUE (TYPE_DOMAIN (fldtype))))
7713 : : {
7714 : : /* Remember the first zero-length array unless a flexible array
7715 : : member has already been seen. */
7716 : 2437 : fmem->array = fld;
7717 : 2437 : fmem->enclosing = pstr;
7718 : : }
7719 : : }
7720 : : else
7721 : : {
7722 : : /* Flexible array members have no upper bound. */
7723 : 1619 : if (fmem->array)
7724 : : {
7725 : 79 : if (TYPE_DOMAIN (TREE_TYPE (fmem->array)))
7726 : : {
7727 : : /* Replace the zero-length array if it's been stored and
7728 : : reset the after pointer. */
7729 : 31 : fmem->after[pun] = NULL_TREE;
7730 : 31 : fmem->array = fld;
7731 : 31 : fmem->enclosing = pstr;
7732 : : }
7733 : 48 : else if (!fmem->after[pun])
7734 : : /* Make a record of another flexible array member. */
7735 : 39 : fmem->after[pun] = fld;
7736 : : }
7737 : : else
7738 : : {
7739 : 1540 : fmem->array = fld;
7740 : 1540 : fmem->enclosing = pstr;
7741 : : }
7742 : : }
7743 : : }
7744 : : }
7745 : :
7746 : : /* Diagnose a strictly (by the C standard) invalid use of a struct with
7747 : : a flexible array member (or the zero-length array extension). */
7748 : :
7749 : : static void
7750 : 2131 : diagnose_invalid_flexarray (const flexmems_t *fmem)
7751 : : {
7752 : 2131 : if (fmem->array && fmem->enclosing)
7753 : : {
7754 : 655 : auto_diagnostic_group d;
7755 : 655 : if (pedwarn (location_of (fmem->enclosing), OPT_Wpedantic,
7756 : 655 : TYPE_DOMAIN (TREE_TYPE (fmem->array))
7757 : : ? G_("invalid use of %q#T with a zero-size array in %q#D")
7758 : : : G_("invalid use of %q#T with a flexible array member "
7759 : : "in %q#T"),
7760 : 655 : DECL_CONTEXT (fmem->array),
7761 : 655 : DECL_CONTEXT (fmem->enclosing)))
7762 : 78 : inform (DECL_SOURCE_LOCATION (fmem->array),
7763 : 78 : "array member %q#D declared here", fmem->array);
7764 : 655 : }
7765 : 2131 : }
7766 : :
7767 : : /* Issue diagnostics for invalid flexible array members or zero-length
7768 : : arrays that are not the last elements of the containing class or its
7769 : : base classes or that are its sole members. */
7770 : :
7771 : : static void
7772 : 39009590 : diagnose_flexarrays (tree t, const flexmems_t *fmem)
7773 : : {
7774 : 39009590 : if (!fmem->array)
7775 : : return;
7776 : :
7777 : 3977 : if (fmem->first && !fmem->after[0])
7778 : : {
7779 : 1849 : diagnose_invalid_flexarray (fmem);
7780 : 1849 : return;
7781 : : }
7782 : :
7783 : : /* Has a diagnostic been issued? */
7784 : 2128 : bool diagd = false;
7785 : :
7786 : 2128 : const char *msg = 0;
7787 : 2128 : const char *msg_fam = 0;
7788 : :
7789 : 2128 : if (TYPE_DOMAIN (TREE_TYPE (fmem->array)))
7790 : : {
7791 : 1615 : if (fmem->after[0])
7792 : : msg = G_("zero-size array member %qD not at end of %q#T");
7793 : 256 : else if (!fmem->first)
7794 : : msg = G_("zero-size array member %qD in an otherwise empty %q#T");
7795 : :
7796 : : if (msg)
7797 : : {
7798 : 1615 : location_t loc = DECL_SOURCE_LOCATION (fmem->array);
7799 : :
7800 : 1615 : auto_diagnostic_group d;
7801 : 1615 : if (pedwarn (loc, OPT_Wpedantic, msg, fmem->array, t))
7802 : : {
7803 : 138 : inform (location_of (t), "in the definition of %q#T", t);
7804 : 138 : diagd = true;
7805 : : }
7806 : 1615 : }
7807 : : }
7808 : : else
7809 : : {
7810 : 513 : if (fmem->after[0])
7811 : : msg = G_("flexible array member %qD not at end of %q#T");
7812 : 316 : else if (!fmem->first)
7813 : 316 : msg_fam = G_("flexible array member %qD in an otherwise"
7814 : : " empty %q#T is a GCC extension");
7815 : :
7816 : 513 : if (msg || msg_fam)
7817 : : {
7818 : 513 : location_t loc = DECL_SOURCE_LOCATION (fmem->array);
7819 : 513 : diagd = true;
7820 : :
7821 : 513 : auto_diagnostic_group d;
7822 : 513 : if (msg)
7823 : 197 : error_at (loc, msg, fmem->array, t);
7824 : : else
7825 : 316 : pedwarn (loc, OPT_Wpedantic, msg_fam, fmem->array, t);
7826 : :
7827 : : /* In the unlikely event that the member following the flexible
7828 : : array member is declared in a different class, or the member
7829 : : overlaps another member of a common union, point to it.
7830 : : Otherwise it should be obvious. */
7831 : 513 : if (fmem->after[0]
7832 : 513 : && (DECL_CONTEXT (fmem->after[0])
7833 : 197 : != DECL_CONTEXT (fmem->array)))
7834 : : {
7835 : 77 : inform (DECL_SOURCE_LOCATION (fmem->after[0]),
7836 : : "next member %q#D declared here",
7837 : : fmem->after[0]);
7838 : 77 : inform (location_of (t), "in the definition of %q#T", t);
7839 : : }
7840 : 513 : }
7841 : : }
7842 : :
7843 : 2128 : if (!diagd && fmem->array && fmem->enclosing)
7844 : 282 : diagnose_invalid_flexarray (fmem);
7845 : : }
7846 : :
7847 : :
7848 : : /* Recursively check to make sure that any flexible array or zero-length
7849 : : array members of class T or its bases are valid (i.e., not the sole
7850 : : non-static data member of T and, if one exists, that it is the last
7851 : : non-static data member of T and its base classes. FMEM is expected
7852 : : to be initially null and is used internally by recursive calls to
7853 : : the function. Issue the appropriate diagnostics for the array member
7854 : : that fails the checks. */
7855 : :
7856 : : static void
7857 : 63998257 : check_flexarrays (tree t, flexmems_t *fmem /* = NULL */,
7858 : : bool base_p /* = false */)
7859 : : {
7860 : : /* Initialize the result of a search for flexible array and zero-length
7861 : : array members. Avoid doing any work if the most interesting FMEM data
7862 : : have already been populated. */
7863 : 63998257 : flexmems_t flexmems = flexmems_t ();
7864 : 63998257 : if (!fmem)
7865 : : fmem = &flexmems;
7866 : 23957260 : else if (fmem->array && fmem->first && fmem->after[0])
7867 : 0 : return;
7868 : :
7869 : 63998257 : tree fam = fmem->array;
7870 : :
7871 : : /* Recursively check the primary base class first. */
7872 : 63998257 : if (CLASSTYPE_HAS_PRIMARY_BASE_P (t))
7873 : : {
7874 : 2515845 : tree basetype = BINFO_TYPE (CLASSTYPE_PRIMARY_BINFO (t));
7875 : 2515845 : check_flexarrays (basetype, fmem, true);
7876 : : }
7877 : :
7878 : : /* Recursively check the base classes. */
7879 : 63998257 : int nbases = TYPE_BINFO (t) ? BINFO_N_BASE_BINFOS (TYPE_BINFO (t)) : 0;
7880 : 88024730 : for (int i = 0; i < nbases; ++i)
7881 : : {
7882 : 24026473 : tree base_binfo = BINFO_BASE_BINFO (TYPE_BINFO (t), i);
7883 : :
7884 : : /* The primary base class was already checked above. */
7885 : 24026473 : if (base_binfo == CLASSTYPE_PRIMARY_BINFO (t))
7886 : 2515702 : continue;
7887 : :
7888 : : /* Virtual base classes are at the end. */
7889 : 21510771 : if (BINFO_VIRTUAL_P (base_binfo))
7890 : 273031 : continue;
7891 : :
7892 : : /* Check the base class. */
7893 : 21237740 : check_flexarrays (BINFO_TYPE (base_binfo), fmem, /*base_p=*/true);
7894 : : }
7895 : :
7896 : 63998257 : if (fmem == &flexmems)
7897 : : {
7898 : : /* Check virtual base classes only once per derived class.
7899 : : I.e., this check is not performed recursively for base
7900 : : classes. */
7901 : 40040997 : int i;
7902 : 40040997 : tree base_binfo;
7903 : 40040997 : vec<tree, va_gc> *vbases;
7904 : 40244672 : for (vbases = CLASSTYPE_VBASECLASSES (t), i = 0;
7905 : 40244672 : vec_safe_iterate (vbases, i, &base_binfo); i++)
7906 : : {
7907 : : /* Check the virtual base class. */
7908 : 203675 : tree basetype = TREE_TYPE (base_binfo);
7909 : :
7910 : 203675 : check_flexarrays (basetype, fmem, /*base_p=*/true);
7911 : : }
7912 : : }
7913 : :
7914 : : /* Is the type unnamed (and therefore a member of it potentially
7915 : : an anonymous struct or union)? */
7916 : 129724770 : bool maybe_anon_p = TYPE_UNNAMED_P (t);
7917 : 1421273 : if (tree ctx = maybe_anon_p ? TYPE_CONTEXT (t) : NULL_TREE)
7918 : 1421273 : maybe_anon_p = RECORD_OR_UNION_TYPE_P (ctx);
7919 : :
7920 : : /* Search the members of the current (possibly derived) class, skipping
7921 : : unnamed structs and unions since those could be anonymous. */
7922 : 63998257 : if (fmem != &flexmems || !maybe_anon_p)
7923 : 86924288 : find_flexarrays (t, fmem, base_p || fam != fmem->array);
7924 : :
7925 : 63998257 : if (fmem == &flexmems && !maybe_anon_p)
7926 : : /* Issue diagnostics for invalid flexible and zero-length array
7927 : : members found in base classes or among the members of the current
7928 : : class. Ignore anonymous structs and unions whose members are
7929 : : considered to be members of the enclosing class and thus will
7930 : : be diagnosed when checking it. */
7931 : 39009590 : diagnose_flexarrays (t, fmem);
7932 : : }
7933 : :
7934 : : /* Perform processing required when the definition of T (a class type)
7935 : : is complete. Diagnose invalid definitions of flexible array members
7936 : : and zero-size arrays. */
7937 : :
7938 : : void
7939 : 39307783 : finish_struct_1 (tree t)
7940 : : {
7941 : 39307783 : tree x;
7942 : : /* A TREE_LIST. The TREE_VALUE of each node is a FUNCTION_DECL. */
7943 : 39307783 : tree virtuals = NULL_TREE;
7944 : :
7945 : 39307783 : if (COMPLETE_TYPE_P (t))
7946 : : {
7947 : 0 : gcc_assert (MAYBE_CLASS_TYPE_P (t));
7948 : 0 : error ("redefinition of %q#T", t);
7949 : 0 : popclass ();
7950 : 0 : return;
7951 : : }
7952 : :
7953 : 39307783 : if (location_t fcloc = failed_completion_location (t))
7954 : : {
7955 : 11 : auto_diagnostic_group adg;
7956 : 11 : if (warning (OPT_Wsfinae_incomplete_,
7957 : : "defining %qT, which previously failed to be complete "
7958 : : "in a SFINAE context", t)
7959 : 11 : && warn_sfinae_incomplete == 1)
7960 : 11 : inform (fcloc, "here. Use %qs for a diagnostic at that point",
7961 : : "-Wsfinae-incomplete=2");
7962 : 11 : }
7963 : :
7964 : : /* If this type was previously laid out as a forward reference,
7965 : : make sure we lay it out again. */
7966 : 39307783 : TYPE_SIZE (t) = NULL_TREE;
7967 : 39307783 : CLASSTYPE_PRIMARY_BINFO (t) = NULL_TREE;
7968 : :
7969 : : /* Make assumptions about the class; we'll reset the flags if
7970 : : necessary. */
7971 : 39307783 : CLASSTYPE_EMPTY_P (t) = 1;
7972 : 39307783 : CLASSTYPE_NEARLY_EMPTY_P (t) = 1;
7973 : 39307783 : CLASSTYPE_CONTAINS_EMPTY_CLASS_P (t) = 0;
7974 : 39307783 : CLASSTYPE_LITERAL_P (t) = true;
7975 : :
7976 : : /* Do end-of-class semantic processing: checking the validity of the
7977 : : bases and members and add implicitly generated methods. */
7978 : 39307783 : check_bases_and_members (t);
7979 : :
7980 : : /* Find the key method. */
7981 : 39307783 : if (TYPE_CONTAINS_VPTR_P (t))
7982 : : {
7983 : : /* The Itanium C++ ABI permits the key method to be chosen when
7984 : : the class is defined -- even though the key method so
7985 : : selected may later turn out to be an inline function. On
7986 : : some systems (such as ARM Symbian OS) the key method cannot
7987 : : be determined until the end of the translation unit. On such
7988 : : systems, we leave CLASSTYPE_KEY_METHOD set to NULL, which
7989 : : will cause the class to be added to KEYED_CLASSES. Then, in
7990 : : finish_file we will determine the key method. */
7991 : 1686331 : if (targetm.cxx.key_method_may_be_inline ())
7992 : 1686331 : determine_key_method (t);
7993 : :
7994 : : /* If a polymorphic class has no key method, we may emit the vtable
7995 : : in every translation unit where the class definition appears. If
7996 : : we're devirtualizing, we can look into the vtable even if we
7997 : : aren't emitting it.
7998 : :
7999 : : Additionally, if the class is attached to a named module, make sure
8000 : : to always emit the vtable in this TU. */
8001 : 1686331 : if (!CLASSTYPE_KEY_METHOD (t) || module_attach_p ())
8002 : 1127925 : vec_safe_push (keyed_classes, t);
8003 : : }
8004 : :
8005 : : /* Layout the class itself. */
8006 : 39307783 : layout_class_type (t, &virtuals);
8007 : : /* COMPLETE_TYPE_P is now true. */
8008 : :
8009 : 39307783 : set_class_bindings (t);
8010 : :
8011 : : /* With the layout complete, check for flexible array members and
8012 : : zero-length arrays that might overlap other members in the final
8013 : : layout. */
8014 : 39307783 : check_flexarrays (t);
8015 : :
8016 : 39307783 : virtuals = modify_all_vtables (t, nreverse (virtuals));
8017 : :
8018 : : /* If necessary, create the primary vtable for this class. */
8019 : 39307783 : if (virtuals || TYPE_CONTAINS_VPTR_P (t))
8020 : : {
8021 : : /* We must enter these virtuals into the table. */
8022 : 1686331 : if (!CLASSTYPE_HAS_PRIMARY_BASE_P (t))
8023 : 243916 : build_primary_vtable (NULL_TREE, t);
8024 : 1442415 : else if (! BINFO_NEW_VTABLE_MARKED (TYPE_BINFO (t)))
8025 : : /* Here we know enough to change the type of our virtual
8026 : : function table, but we will wait until later this function. */
8027 : 0 : build_primary_vtable (CLASSTYPE_PRIMARY_BINFO (t), t);
8028 : :
8029 : : /* If we're warning about ABI tags, check the types of the new
8030 : : virtual functions. */
8031 : 1686331 : if (warn_abi_tag)
8032 : 6 : for (tree v = virtuals; v; v = TREE_CHAIN (v))
8033 : 3 : check_abi_tags (t, TREE_VALUE (v));
8034 : : }
8035 : :
8036 : 39307783 : if (TYPE_CONTAINS_VPTR_P (t))
8037 : : {
8038 : 1686331 : int vindex;
8039 : 1686331 : tree fn;
8040 : :
8041 : 1686331 : if (BINFO_VTABLE (TYPE_BINFO (t)))
8042 : 1686331 : gcc_assert (DECL_VIRTUAL_P (BINFO_VTABLE (TYPE_BINFO (t))));
8043 : 1686331 : if (!CLASSTYPE_HAS_PRIMARY_BASE_P (t))
8044 : 243916 : gcc_assert (BINFO_VIRTUALS (TYPE_BINFO (t)) == NULL_TREE);
8045 : :
8046 : : /* Add entries for virtual functions introduced by this class. */
8047 : 1686331 : BINFO_VIRTUALS (TYPE_BINFO (t))
8048 : 1686331 : = chainon (BINFO_VIRTUALS (TYPE_BINFO (t)), virtuals);
8049 : :
8050 : : /* Set DECL_VINDEX for all functions declared in this class. */
8051 : 1686331 : for (vindex = 0, fn = BINFO_VIRTUALS (TYPE_BINFO (t));
8052 : 11396291 : fn;
8053 : 9709960 : fn = TREE_CHAIN (fn),
8054 : 9709960 : vindex += (TARGET_VTABLE_USES_DESCRIPTORS
8055 : : ? TARGET_VTABLE_USES_DESCRIPTORS : 1))
8056 : : {
8057 : 9709960 : tree fndecl = BV_FN (fn);
8058 : :
8059 : 9709960 : if (DECL_THUNK_P (fndecl))
8060 : : /* A thunk. We should never be calling this entry directly
8061 : : from this vtable -- we'd use the entry for the non
8062 : : thunk base function. */
8063 : 126 : DECL_VINDEX (fndecl) = NULL_TREE;
8064 : 9709834 : else if (TREE_CODE (DECL_VINDEX (fndecl)) != INTEGER_CST)
8065 : 7744548 : DECL_VINDEX (fndecl) = build_int_cst (NULL_TREE, vindex);
8066 : : }
8067 : : }
8068 : :
8069 : 39307783 : finish_struct_bits (t);
8070 : :
8071 : 39307783 : set_method_tm_attributes (t);
8072 : 39307783 : if (flag_openmp || flag_openmp_simd)
8073 : 97524 : finish_omp_declare_simd_methods (t);
8074 : :
8075 : : /* Clear DECL_IN_AGGR_P for all member functions. Complete the rtl
8076 : : for any static member objects of the type we're working on. */
8077 : 350294347 : for (x = TYPE_FIELDS (t); x; x = DECL_CHAIN (x))
8078 : 310986564 : if (DECL_DECLARES_FUNCTION_P (x))
8079 : : {
8080 : : /* Synthesize constexpr defaulted comparisons. */
8081 : 176282410 : if (!DECL_ARTIFICIAL (x)
8082 : 175318915 : && DECL_DEFAULTED_IN_CLASS_P (x)
8083 : 189955443 : && special_function_p (x) == sfk_comparison)
8084 : 11815 : defaulted_late_check (x);
8085 : 176282410 : DECL_IN_AGGR_P (x) = false;
8086 : : }
8087 : 10624338 : else if (VAR_P (x) && TREE_STATIC (x)
8088 : 10624338 : && TREE_TYPE (x) != error_mark_node
8089 : 145328427 : && same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (x)), t))
8090 : 41959 : SET_DECL_MODE (x, TYPE_MODE (t));
8091 : :
8092 : : /* Complain if one of the field types requires lower visibility. */
8093 : 39307783 : constrain_class_visibility (t);
8094 : :
8095 : : /* Make the rtl for any new vtables we have created, and unmark
8096 : : the base types we marked. */
8097 : 39307783 : finish_vtbls (t);
8098 : :
8099 : : /* Build the VTT for T. */
8100 : 39307783 : build_vtt (t);
8101 : :
8102 : 39307783 : if (warn_nonvdtor
8103 : 147 : && TYPE_POLYMORPHIC_P (t) && accessible_nvdtor_p (t)
8104 : 39307819 : && !CLASSTYPE_FINAL (t))
8105 : 33 : warning (OPT_Wnon_virtual_dtor,
8106 : : "%q#T has virtual functions and accessible"
8107 : : " non-virtual destructor", t);
8108 : :
8109 : 39307783 : complete_vars (t);
8110 : :
8111 : 39307783 : if (warn_overloaded_virtual)
8112 : 381391 : warn_hidden (t);
8113 : :
8114 : : /* Class layout, assignment of virtual table slots, etc., is now
8115 : : complete. Give the back end a chance to tweak the visibility of
8116 : : the class or perform any other required target modifications. */
8117 : 39307783 : targetm.cxx.adjust_class_at_definition (t);
8118 : :
8119 : 39307783 : maybe_suppress_debug_info (t);
8120 : :
8121 : 39307783 : if (flag_vtable_verify)
8122 : 12 : vtv_save_class_info (t);
8123 : :
8124 : 39307783 : dump_class_hierarchy (t);
8125 : :
8126 : : /* Finish debugging output for this type. */
8127 : 39307783 : rest_of_type_compilation (t, ! LOCAL_CLASS_P (t));
8128 : :
8129 : 39307783 : if (TYPE_TRANSPARENT_AGGR (t))
8130 : : {
8131 : 163 : tree field = first_field (t);
8132 : 163 : if (field == NULL_TREE || error_operand_p (field))
8133 : : {
8134 : 9 : error ("type transparent %q#T does not have any fields", t);
8135 : 9 : TYPE_TRANSPARENT_AGGR (t) = 0;
8136 : : }
8137 : 154 : else if (DECL_ARTIFICIAL (field))
8138 : : {
8139 : 3 : if (DECL_FIELD_IS_BASE (field))
8140 : 3 : error ("type transparent class %qT has base classes", t);
8141 : : else
8142 : : {
8143 : 0 : gcc_checking_assert (DECL_VIRTUAL_P (field));
8144 : 0 : error ("type transparent class %qT has virtual functions", t);
8145 : : }
8146 : 3 : TYPE_TRANSPARENT_AGGR (t) = 0;
8147 : : }
8148 : 151 : else if (TYPE_MODE (t) != DECL_MODE (field))
8149 : : {
8150 : 3 : error ("type transparent %q#T cannot be made transparent because "
8151 : : "the type of the first field has a different ABI from the "
8152 : : "class overall", t);
8153 : 3 : TYPE_TRANSPARENT_AGGR (t) = 0;
8154 : : }
8155 : : }
8156 : : }
8157 : :
8158 : : /* When T was built up, the member declarations were added in reverse
8159 : : order. Rearrange them to declaration order. */
8160 : :
8161 : : void
8162 : 56182071 : unreverse_member_declarations (tree t)
8163 : : {
8164 : 56182071 : tree next;
8165 : 56182071 : tree prev;
8166 : 56182071 : tree x;
8167 : :
8168 : : /* The following lists are all in reverse order. Put them in
8169 : : declaration order now. */
8170 : 56182071 : CLASSTYPE_DECL_LIST (t) = nreverse (CLASSTYPE_DECL_LIST (t));
8171 : :
8172 : : /* For the TYPE_FIELDS, only the non TYPE_DECLs are in reverse
8173 : : order, so we can't just use nreverse. Due to stat_hack
8174 : : chicanery in finish_member_declaration. */
8175 : 56182071 : prev = NULL_TREE;
8176 : 56182071 : for (x = TYPE_FIELDS (t);
8177 : 288302547 : x && TREE_CODE (x) != TYPE_DECL;
8178 : 232120476 : x = next)
8179 : : {
8180 : 232120476 : next = DECL_CHAIN (x);
8181 : 232120476 : DECL_CHAIN (x) = prev;
8182 : 232120476 : prev = x;
8183 : : }
8184 : :
8185 : 56182071 : if (prev)
8186 : : {
8187 : 22568260 : DECL_CHAIN (TYPE_FIELDS (t)) = x;
8188 : 22568260 : TYPE_FIELDS (t) = prev;
8189 : : }
8190 : 56182071 : }
8191 : :
8192 : : /* Classes, structs or unions T marked with hotness attributes propagate
8193 : : the attribute to all methods. */
8194 : :
8195 : : void
8196 : 39307783 : propagate_class_warmth_attribute (tree t)
8197 : : {
8198 : 39307783 : if (t == NULL_TREE
8199 : 39307783 : || !(TREE_CODE (t) == RECORD_TYPE
8200 : : || TREE_CODE (t) == UNION_TYPE))
8201 : : return;
8202 : :
8203 : 39307783 : tree class_has_cold_attr
8204 : 39307783 : = lookup_attribute ("cold", TYPE_ATTRIBUTES (t));
8205 : 39307783 : tree class_has_hot_attr
8206 : 39307783 : = lookup_attribute ("hot", TYPE_ATTRIBUTES (t));
8207 : :
8208 : 39307783 : if (class_has_cold_attr || class_has_hot_attr)
8209 : 42 : for (tree f = TYPE_FIELDS (t); f; f = DECL_CHAIN (f))
8210 : 30 : if (DECL_DECLARES_FUNCTION_P (f))
8211 : 18 : maybe_propagate_warmth_attributes (STRIP_TEMPLATE (f), t);
8212 : : }
8213 : :
8214 : : tree
8215 : 25735654 : finish_struct (tree t, tree attributes)
8216 : : {
8217 : 25735654 : location_t saved_loc = input_location;
8218 : :
8219 : : /* Now that we've got all the field declarations, reverse everything
8220 : : as necessary. */
8221 : 25735654 : unreverse_member_declarations (t);
8222 : :
8223 : 25735654 : cplus_decl_attributes (&t, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
8224 : 25735654 : fixup_attribute_variants (t);
8225 : :
8226 : : /* Nadger the current location so that diagnostics point to the start of
8227 : : the struct, not the end. */
8228 : 25735654 : input_location = DECL_SOURCE_LOCATION (TYPE_NAME (t));
8229 : :
8230 : 25735654 : if (processing_template_decl)
8231 : : {
8232 : 16874288 : tree x;
8233 : :
8234 : 134129046 : for (x = TYPE_FIELDS (t); x; x = DECL_CHAIN (x))
8235 : 117254758 : if (DECL_DECLARES_FUNCTION_P (x))
8236 : : {
8237 : 66389711 : DECL_IN_AGGR_P (x) = false;
8238 : 66389711 : if (DECL_VIRTUAL_P (x))
8239 : 1845073 : CLASSTYPE_NON_AGGREGATE (t) = true;
8240 : : }
8241 : 50865047 : else if (TREE_CODE (x) == FIELD_DECL)
8242 : : {
8243 : 9370279 : if (TREE_PROTECTED (x) || TREE_PRIVATE (x))
8244 : 5096571 : CLASSTYPE_NON_AGGREGATE (t) = true;
8245 : : }
8246 : :
8247 : : /* Also add a USING_DECL for operator=. We know there'll be (at
8248 : : least) one, but we don't know the signature(s). We want name
8249 : : lookup not to fail or recurse into bases. This isn't added
8250 : : to the template decl list so we drop this at instantiation
8251 : : time. */
8252 : 16874288 : tree ass_op = build_lang_decl (USING_DECL, assign_op_identifier,
8253 : : NULL_TREE);
8254 : 16874288 : DECL_CONTEXT (ass_op) = t;
8255 : 16874288 : USING_DECL_SCOPE (ass_op) = t;
8256 : 16874288 : DECL_DEPENDENT_P (ass_op) = true;
8257 : 16874288 : DECL_ARTIFICIAL (ass_op) = true;
8258 : 16874288 : DECL_CHAIN (ass_op) = TYPE_FIELDS (t);
8259 : 16874288 : TYPE_FIELDS (t) = ass_op;
8260 : :
8261 : 16874288 : TYPE_SIZE (t) = bitsize_zero_node;
8262 : 16874288 : TYPE_SIZE_UNIT (t) = size_zero_node;
8263 : : /* COMPLETE_TYPE_P is now true. */
8264 : :
8265 : 16874288 : set_class_bindings (t);
8266 : :
8267 : : /* We need to emit an error message if this type was used as a parameter
8268 : : and it is an abstract type, even if it is a template. We construct
8269 : : a simple CLASSTYPE_PURE_VIRTUALS list without taking bases into
8270 : : account and we call complete_vars with this type, which will check
8271 : : the PARM_DECLS. Note that while the type is being defined,
8272 : : CLASSTYPE_PURE_VIRTUALS contains the list of the inline friends
8273 : : (see CLASSTYPE_INLINE_FRIENDS) so we need to clear it. */
8274 : 16874288 : CLASSTYPE_PURE_VIRTUALS (t) = NULL;
8275 : 151003334 : for (x = TYPE_FIELDS (t); x; x = DECL_CHAIN (x))
8276 : 134129046 : if (TREE_CODE (x) == FUNCTION_DECL && DECL_PURE_VIRTUAL_P (x))
8277 : 265953 : vec_safe_push (CLASSTYPE_PURE_VIRTUALS (t), x);
8278 : 16874288 : complete_vars (t);
8279 : :
8280 : : /* Remember current #pragma pack value. */
8281 : 16874288 : TYPE_PRECISION (t) = maximum_field_alignment;
8282 : :
8283 : 16874288 : if (cxx_dialect < cxx20)
8284 : : {
8285 : 11498380 : if (!CLASSTYPE_NON_AGGREGATE (t)
8286 : 11498380 : && type_has_user_provided_or_explicit_constructor (t))
8287 : 1276616 : CLASSTYPE_NON_AGGREGATE (t) = 1;
8288 : : }
8289 : 5375908 : else if (TYPE_HAS_USER_CONSTRUCTOR (t))
8290 : 1607514 : CLASSTYPE_NON_AGGREGATE (t) = 1;
8291 : :
8292 : : /* Fix up any variants we've already built. */
8293 : 16874288 : fixup_type_variants (t);
8294 : : }
8295 : : else
8296 : 8861366 : finish_struct_1 (t);
8297 : : /* COMPLETE_TYPE_P is now true. */
8298 : :
8299 : 25735654 : maybe_warn_about_overly_private_class (t);
8300 : :
8301 : 25735654 : if (is_std_init_list (t))
8302 : : {
8303 : : /* People keep complaining that the compiler crashes on an invalid
8304 : : definition of initializer_list, so I guess we should explicitly
8305 : : reject it. What the compiler internals care about is that it's a
8306 : : template and has a pointer field followed by size_type field. */
8307 : 13957 : bool ok = false;
8308 : 13957 : if (processing_template_decl)
8309 : : {
8310 : 13957 : tree f = next_aggregate_field (TYPE_FIELDS (t));
8311 : 13957 : if (f && TYPE_PTR_P (TREE_TYPE (f)))
8312 : : {
8313 : 13954 : f = next_aggregate_field (DECL_CHAIN (f));
8314 : 13954 : if (f && same_type_p (TREE_TYPE (f), size_type_node))
8315 : : ok = true;
8316 : : }
8317 : : }
8318 : : /* It also cannot be a union. */
8319 : 13957 : ok &= NON_UNION_CLASS_TYPE_P (t);
8320 : 13954 : if (!ok)
8321 : 9 : fatal_error (input_location, "definition of %qD does not match "
8322 : 9 : "%<#include <initializer_list>%>", TYPE_NAME (t));
8323 : : }
8324 : :
8325 : 25735645 : input_location = saved_loc;
8326 : :
8327 : 25735645 : TYPE_BEING_DEFINED (t) = 0;
8328 : :
8329 : 25735645 : if (current_class_type)
8330 : 25735645 : popclass ();
8331 : : else
8332 : 0 : error ("trying to finish struct, but kicked out due to previous parse errors");
8333 : :
8334 : 25735645 : if (flag_openmp)
8335 : 640708 : for (tree decl = TYPE_FIELDS (t); decl; decl = DECL_CHAIN (decl))
8336 : 533834 : if (TREE_CODE (decl) == FUNCTION_DECL
8337 : 533834 : && DECL_OBJECT_MEMBER_FUNCTION_P (decl))
8338 : 116350 : if (tree attr = lookup_attribute ("omp declare variant base",
8339 : 116350 : DECL_ATTRIBUTES (decl)))
8340 : 112 : omp_declare_variant_finalize (decl, attr);
8341 : :
8342 : 16874279 : if (processing_template_decl && at_function_scope_p ()
8343 : : /* Lambdas are defined by the LAMBDA_EXPR. */
8344 : 27388656 : && !LAMBDA_TYPE_P (t))
8345 : 204224 : add_stmt (build_min (TAG_DEFN, t));
8346 : :
8347 : 25735645 : return t;
8348 : : }
8349 : :
8350 : : /* Hash table to avoid endless recursion when handling references. */
8351 : : static hash_table<nofree_ptr_hash<tree_node> > *fixed_type_or_null_ref_ht;
8352 : :
8353 : : /* Return the dynamic type of INSTANCE, if known.
8354 : : Used to determine whether the virtual function table is needed
8355 : : or not.
8356 : :
8357 : : *NONNULL is set iff INSTANCE can be known to be nonnull, regardless
8358 : : of our knowledge of its type. *NONNULL should be initialized
8359 : : before this function is called. */
8360 : :
8361 : : static tree
8362 : 15760006 : fixed_type_or_null (tree instance, int *nonnull, int *cdtorp)
8363 : : {
8364 : : #define RECUR(T) fixed_type_or_null((T), nonnull, cdtorp)
8365 : :
8366 : 15760006 : switch (TREE_CODE (instance))
8367 : : {
8368 : 2474972 : case INDIRECT_REF:
8369 : 2474972 : if (INDIRECT_TYPE_P (TREE_TYPE (instance)))
8370 : : return NULL_TREE;
8371 : : else
8372 : 2417558 : return RECUR (TREE_OPERAND (instance, 0));
8373 : :
8374 : 256173 : case CALL_EXPR:
8375 : : /* This is a call to a constructor, hence it's never zero. */
8376 : 256173 : if (CALL_EXPR_FN (instance)
8377 : 256173 : && TREE_HAS_CONSTRUCTOR (instance))
8378 : : {
8379 : 0 : if (nonnull)
8380 : 0 : *nonnull = 1;
8381 : 0 : return TREE_TYPE (instance);
8382 : : }
8383 : 256173 : if (CLASS_TYPE_P (TREE_TYPE (instance)))
8384 : : {
8385 : : /* We missed a build_cplus_new somewhere, likely due to tf_decltype
8386 : : mishandling. */
8387 : 0 : gcc_checking_assert (false);
8388 : : if (nonnull)
8389 : : *nonnull = 1;
8390 : : return TREE_TYPE (instance);
8391 : : }
8392 : : return NULL_TREE;
8393 : :
8394 : 63448 : case SAVE_EXPR:
8395 : : /* This is a call to a constructor, hence it's never zero. */
8396 : 63448 : if (TREE_HAS_CONSTRUCTOR (instance))
8397 : : {
8398 : 0 : if (nonnull)
8399 : 0 : *nonnull = 1;
8400 : 0 : return TREE_TYPE (instance);
8401 : : }
8402 : 63448 : return RECUR (TREE_OPERAND (instance, 0));
8403 : :
8404 : 263 : case POINTER_PLUS_EXPR:
8405 : 263 : case PLUS_EXPR:
8406 : 263 : case MINUS_EXPR:
8407 : 263 : if (TREE_CODE (TREE_OPERAND (instance, 0)) == ADDR_EXPR)
8408 : 24 : return RECUR (TREE_OPERAND (instance, 0));
8409 : 239 : if (TREE_CODE (TREE_OPERAND (instance, 1)) == INTEGER_CST)
8410 : : /* Propagate nonnull. */
8411 : 35 : return RECUR (TREE_OPERAND (instance, 0));
8412 : :
8413 : : return NULL_TREE;
8414 : :
8415 : 2915911 : CASE_CONVERT:
8416 : 2915911 : return RECUR (TREE_OPERAND (instance, 0));
8417 : :
8418 : 739278 : case ADDR_EXPR:
8419 : 739278 : instance = TREE_OPERAND (instance, 0);
8420 : 739278 : if (nonnull)
8421 : : {
8422 : : /* Just because we see an ADDR_EXPR doesn't mean we're dealing
8423 : : with a real object -- given &p->f, p can still be null. */
8424 : 737856 : tree t = get_base_address (instance);
8425 : : /* ??? Probably should check DECL_WEAK here. */
8426 : 737856 : if (t && DECL_P (t))
8427 : 89532 : *nonnull = 1;
8428 : : }
8429 : 739278 : return RECUR (instance);
8430 : :
8431 : 1213491 : case COMPONENT_REF:
8432 : : /* If this component is really a base class reference, then the field
8433 : : itself isn't definitive. */
8434 : 1213491 : if (DECL_FIELD_IS_BASE (TREE_OPERAND (instance, 1)))
8435 : 1237 : return RECUR (TREE_OPERAND (instance, 0));
8436 : 1212254 : return RECUR (TREE_OPERAND (instance, 1));
8437 : :
8438 : 1627478 : case VAR_DECL:
8439 : 1627478 : case FIELD_DECL:
8440 : 1627478 : if (TREE_CODE (TREE_TYPE (instance)) == ARRAY_TYPE
8441 : 1627478 : && MAYBE_CLASS_TYPE_P (TREE_TYPE (TREE_TYPE (instance))))
8442 : : {
8443 : 35 : if (nonnull)
8444 : 35 : *nonnull = 1;
8445 : 35 : return TREE_TYPE (TREE_TYPE (instance));
8446 : : }
8447 : : /* fall through. */
8448 : 7527724 : case TARGET_EXPR:
8449 : 7527724 : case PARM_DECL:
8450 : 7527724 : case RESULT_DECL:
8451 : 7527724 : if (MAYBE_CLASS_TYPE_P (TREE_TYPE (instance)))
8452 : : {
8453 : 1790427 : if (nonnull)
8454 : 1789080 : *nonnull = 1;
8455 : 1790427 : return TREE_TYPE (instance);
8456 : : }
8457 : 5737297 : else if (instance == current_class_ptr)
8458 : : {
8459 : 4424081 : if (nonnull)
8460 : 3695485 : *nonnull = 1;
8461 : :
8462 : : /* if we're in a ctor or dtor, we know our type. If
8463 : : current_class_ptr is set but we aren't in a function, we're in
8464 : : an NSDMI (and therefore a constructor). */
8465 : 4424081 : if (current_scope () != current_function_decl
8466 : 4424081 : || (DECL_LANG_SPECIFIC (current_function_decl)
8467 : 8847898 : && (DECL_CONSTRUCTOR_P (current_function_decl)
8468 : 2155041 : || DECL_DESTRUCTOR_P (current_function_decl))))
8469 : : {
8470 : 2363627 : if (cdtorp)
8471 : 2363627 : *cdtorp = 1;
8472 : 2363627 : return TREE_TYPE (TREE_TYPE (instance));
8473 : : }
8474 : : }
8475 : 1313216 : else if (TYPE_REF_P (TREE_TYPE (instance)))
8476 : : {
8477 : : /* We only need one hash table because it is always left empty. */
8478 : 1003702 : if (!fixed_type_or_null_ref_ht)
8479 : 13471 : fixed_type_or_null_ref_ht
8480 : 13471 : = new hash_table<nofree_ptr_hash<tree_node> > (37);
8481 : :
8482 : : /* Reference variables should be references to objects. */
8483 : 1003702 : if (nonnull)
8484 : 1001531 : *nonnull = 1;
8485 : :
8486 : : /* Enter the INSTANCE in a table to prevent recursion; a
8487 : : variable's initializer may refer to the variable
8488 : : itself. */
8489 : 1003702 : if (VAR_P (instance)
8490 : 24370 : && DECL_INITIAL (instance)
8491 : 2336 : && !type_dependent_expression_p_push (DECL_INITIAL (instance))
8492 : 1006038 : && !fixed_type_or_null_ref_ht->find (instance))
8493 : : {
8494 : 2280 : tree type;
8495 : 2280 : tree_node **slot;
8496 : :
8497 : 2280 : slot = fixed_type_or_null_ref_ht->find_slot (instance, INSERT);
8498 : 2280 : *slot = instance;
8499 : 2280 : type = RECUR (DECL_INITIAL (instance));
8500 : 2280 : fixed_type_or_null_ref_ht->remove_elt (instance);
8501 : :
8502 : 2280 : return type;
8503 : : }
8504 : : }
8505 : : return NULL_TREE;
8506 : :
8507 : 305893 : case VIEW_CONVERT_EXPR:
8508 : 305893 : if (location_wrapper_p (instance))
8509 : 305893 : return RECUR (TREE_OPERAND (instance, 0));
8510 : : else
8511 : : /* TODO: Recursion may be correct for some non-location-wrapper
8512 : : uses of VIEW_CONVERT_EXPR. */
8513 : : return NULL_TREE;
8514 : :
8515 : : default:
8516 : : return NULL_TREE;
8517 : : }
8518 : : #undef RECUR
8519 : : }
8520 : :
8521 : : /* Return nonzero if the dynamic type of INSTANCE is known, and
8522 : : equivalent to the static type. We also handle the case where
8523 : : INSTANCE is really a pointer. Return negative if this is a
8524 : : ctor/dtor. There the dynamic type is known, but this might not be
8525 : : the most derived base of the original object, and hence virtual
8526 : : bases may not be laid out according to this type.
8527 : :
8528 : : Used to determine whether the virtual function table is needed
8529 : : or not.
8530 : :
8531 : : *NONNULL is set iff INSTANCE can be known to be nonnull, regardless
8532 : : of our knowledge of its type. *NONNULL should be initialized
8533 : : before this function is called. */
8534 : :
8535 : : int
8536 : 7920884 : resolves_to_fixed_type_p (tree instance, int* nonnull)
8537 : : {
8538 : 7920884 : tree t = TREE_TYPE (instance);
8539 : 7920884 : int cdtorp = 0;
8540 : 7920884 : tree fixed;
8541 : :
8542 : : /* processing_template_decl can be false in a template if we're in
8543 : : instantiate_non_dependent_expr, but we still want to suppress
8544 : : this check. */
8545 : 7920884 : if (in_template_context)
8546 : : {
8547 : : /* In a template we only care about the type of the result. */
8548 : 363186 : if (nonnull)
8549 : 363184 : *nonnull = true;
8550 : 363186 : return true;
8551 : : }
8552 : :
8553 : 7557698 : fixed = fixed_type_or_null (instance, nonnull, &cdtorp);
8554 : 7557698 : if (INDIRECT_TYPE_P (t))
8555 : 4633117 : t = TREE_TYPE (t);
8556 : 7557698 : if (CLASS_TYPE_P (t) && CLASSTYPE_FINAL (t))
8557 : : return 1;
8558 : 7470651 : if (fixed == NULL_TREE)
8559 : : return 0;
8560 : 4105930 : if (!same_type_ignoring_top_level_qualifiers_p (t, fixed))
8561 : : return 0;
8562 : 4105464 : return cdtorp ? -1 : 1;
8563 : : }
8564 : :
8565 : :
8566 : : void
8567 : 96471 : init_class_processing (void)
8568 : : {
8569 : 96471 : current_class_depth = 0;
8570 : 96471 : current_class_stack_size = 10;
8571 : 96471 : current_class_stack
8572 : 96471 : = XNEWVEC (struct class_stack_node, current_class_stack_size);
8573 : 96471 : sizeof_biggest_empty_class = size_zero_node;
8574 : :
8575 : 96471 : ridpointers[(int) RID_PUBLIC] = access_public_node;
8576 : 96471 : ridpointers[(int) RID_PRIVATE] = access_private_node;
8577 : 96471 : ridpointers[(int) RID_PROTECTED] = access_protected_node;
8578 : 96471 : }
8579 : :
8580 : : /* Restore the cached PREVIOUS_CLASS_LEVEL. */
8581 : :
8582 : : static void
8583 : 180068778 : restore_class_cache (void)
8584 : : {
8585 : 180068778 : tree type;
8586 : :
8587 : : /* We are re-entering the same class we just left, so we don't
8588 : : have to search the whole inheritance matrix to find all the
8589 : : decls to bind again. Instead, we install the cached
8590 : : class_shadowed list and walk through it binding names. */
8591 : 180068778 : push_binding_level (previous_class_level);
8592 : 180068778 : class_binding_level = previous_class_level;
8593 : : /* Restore IDENTIFIER_TYPE_VALUE. */
8594 : 180068778 : for (type = class_binding_level->type_shadowed;
8595 : 990198198 : type;
8596 : 810129420 : type = TREE_CHAIN (type))
8597 : 810129420 : SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (type), TREE_TYPE (type));
8598 : 180068778 : }
8599 : :
8600 : : /* Set global variables CURRENT_CLASS_NAME and CURRENT_CLASS_TYPE as
8601 : : appropriate for TYPE.
8602 : :
8603 : : So that we may avoid calls to lookup_name, we cache the _TYPE
8604 : : nodes of local TYPE_DECLs in the TREE_TYPE field of the name.
8605 : :
8606 : : For multiple inheritance, we perform a two-pass depth-first search
8607 : : of the type lattice. */
8608 : :
8609 : : void
8610 : 372840351 : pushclass (tree type)
8611 : : {
8612 : 372840351 : class_stack_node_t csn;
8613 : :
8614 : 372840351 : type = TYPE_MAIN_VARIANT (type);
8615 : :
8616 : : /* Make sure there is enough room for the new entry on the stack. */
8617 : 372840351 : if (current_class_depth + 1 >= current_class_stack_size)
8618 : : {
8619 : 4050 : current_class_stack_size *= 2;
8620 : 4050 : current_class_stack
8621 : 4050 : = XRESIZEVEC (struct class_stack_node, current_class_stack,
8622 : : current_class_stack_size);
8623 : : }
8624 : :
8625 : : /* Insert a new entry on the class stack. */
8626 : 372840351 : csn = current_class_stack + current_class_depth;
8627 : 372840351 : csn->name = current_class_name;
8628 : 372840351 : csn->type = current_class_type;
8629 : 372840351 : csn->access = current_access_specifier;
8630 : 372840351 : csn->names_used = 0;
8631 : 372840351 : csn->hidden = 0;
8632 : 372840351 : current_class_depth++;
8633 : :
8634 : : /* Now set up the new type. */
8635 : 372840351 : current_class_name = TYPE_NAME (type);
8636 : 372840351 : if (TREE_CODE (current_class_name) == TYPE_DECL)
8637 : 372840351 : current_class_name = DECL_NAME (current_class_name);
8638 : 372840351 : current_class_type = type;
8639 : :
8640 : : /* By default, things in classes are private, while things in
8641 : : structures or unions are public. */
8642 : 372840351 : current_access_specifier = (CLASSTYPE_DECLARED_CLASS (type)
8643 : 372840351 : ? access_private_node
8644 : : : access_public_node);
8645 : :
8646 : 372840351 : if (previous_class_level
8647 : 237946385 : && type != previous_class_level->this_entity
8648 : 53741641 : && current_class_depth == 1)
8649 : : {
8650 : : /* Forcibly remove any old class remnants. */
8651 : 25258529 : invalidate_class_lookup_cache ();
8652 : : }
8653 : :
8654 : 372840351 : if (!previous_class_level
8655 : 212687856 : || type != previous_class_level->this_entity
8656 : 184204744 : || current_class_depth > 1)
8657 : 192771573 : pushlevel_class ();
8658 : : else
8659 : 180068778 : restore_class_cache ();
8660 : 372840351 : }
8661 : :
8662 : : /* Get out of the current class scope. If we were in a class scope
8663 : : previously, that is the one popped to. */
8664 : :
8665 : : void
8666 : 372824088 : popclass (void)
8667 : : {
8668 : 372824088 : poplevel_class ();
8669 : :
8670 : 372824085 : current_class_depth--;
8671 : 372824085 : current_class_name = current_class_stack[current_class_depth].name;
8672 : 372824085 : current_class_type = current_class_stack[current_class_depth].type;
8673 : 372824085 : current_access_specifier = current_class_stack[current_class_depth].access;
8674 : 372824085 : if (current_class_stack[current_class_depth].names_used)
8675 : 14280148 : splay_tree_delete (current_class_stack[current_class_depth].names_used);
8676 : 372824085 : }
8677 : :
8678 : : /* Mark the top of the class stack as hidden. */
8679 : :
8680 : : void
8681 : 378431369 : push_class_stack (void)
8682 : : {
8683 : 378431369 : if (current_class_depth)
8684 : 258842005 : ++current_class_stack[current_class_depth - 1].hidden;
8685 : 378431369 : }
8686 : :
8687 : : /* Mark the top of the class stack as un-hidden. */
8688 : :
8689 : : void
8690 : 378307808 : pop_class_stack (void)
8691 : : {
8692 : 378307808 : if (current_class_depth)
8693 : 258828469 : --current_class_stack[current_class_depth - 1].hidden;
8694 : 378307808 : }
8695 : :
8696 : : /* If the class type currently being defined is either T or
8697 : : a nested type of T, returns the type from the current_class_stack,
8698 : : which might be equivalent to but not equal to T in case of
8699 : : constrained partial specializations. */
8700 : :
8701 : : tree
8702 : 4505427703 : currently_open_class (tree t)
8703 : : {
8704 : 4505427703 : int i;
8705 : :
8706 : 4505427703 : if (!CLASS_TYPE_P (t))
8707 : : return NULL_TREE;
8708 : :
8709 : 4338652078 : t = TYPE_MAIN_VARIANT (t);
8710 : :
8711 : : /* We start looking from 1 because entry 0 is from global scope,
8712 : : and has no type. */
8713 : 5378819635 : for (i = current_class_depth; i > 0; --i)
8714 : : {
8715 : 4498239600 : tree c;
8716 : 4498239600 : if (i == current_class_depth)
8717 : 3987795414 : c = current_class_type;
8718 : : else
8719 : : {
8720 : 510444186 : if (current_class_stack[i].hidden)
8721 : : break;
8722 : 383066476 : c = current_class_stack[i].type;
8723 : : }
8724 : 4370861890 : if (!c)
8725 : 268799002 : continue;
8726 : 4102062888 : if (same_type_p (c, t))
8727 : : return c;
8728 : : }
8729 : : return NULL_TREE;
8730 : : }
8731 : :
8732 : : /* If either current_class_type or one of its enclosing classes are derived
8733 : : from T, return the appropriate type. Used to determine how we found
8734 : : something via unqualified lookup. */
8735 : :
8736 : : tree
8737 : 126477561 : currently_open_derived_class (tree t)
8738 : : {
8739 : 126477561 : int i;
8740 : :
8741 : : /* The bases of a dependent type are unknown. */
8742 : 126477561 : if (dependent_type_p (t))
8743 : : return NULL_TREE;
8744 : :
8745 : 121919644 : if (!current_class_type)
8746 : : return NULL_TREE;
8747 : :
8748 : 121489549 : if (DERIVED_FROM_P (t, current_class_type))
8749 : 115933047 : return current_class_type;
8750 : :
8751 : 6829962 : for (i = current_class_depth - 1; i > 0; --i)
8752 : : {
8753 : 4004638 : if (current_class_stack[i].hidden)
8754 : : break;
8755 : 4004638 : if (DERIVED_FROM_P (t, current_class_stack[i].type))
8756 : 2731178 : return current_class_stack[i].type;
8757 : : }
8758 : :
8759 : : return NULL_TREE;
8760 : : }
8761 : :
8762 : : /* Return the outermost enclosing class type that is still open, or
8763 : : NULL_TREE. */
8764 : :
8765 : : tree
8766 : 13 : outermost_open_class (void)
8767 : : {
8768 : 13 : if (!current_class_type)
8769 : : return NULL_TREE;
8770 : 13 : tree r = NULL_TREE;
8771 : 13 : if (TYPE_BEING_DEFINED (current_class_type))
8772 : 13 : r = current_class_type;
8773 : 13 : for (int i = current_class_depth - 1; i > 0; --i)
8774 : : {
8775 : 0 : if (current_class_stack[i].hidden)
8776 : : break;
8777 : 0 : tree t = current_class_stack[i].type;
8778 : 0 : if (!TYPE_BEING_DEFINED (t))
8779 : : break;
8780 : 0 : r = t;
8781 : : }
8782 : : return r;
8783 : : }
8784 : :
8785 : : /* Returns the innermost class type which is not a lambda closure type. */
8786 : :
8787 : : tree
8788 : 1150645374 : current_nonlambda_class_type (void)
8789 : : {
8790 : 1150645374 : tree type = current_class_type;
8791 : 2064002378 : while (type && LAMBDA_TYPE_P (type))
8792 : 2677111 : type = decl_type_context (TYPE_NAME (type));
8793 : 1150645374 : return type;
8794 : : }
8795 : :
8796 : : /* When entering a class scope, all enclosing class scopes' names with
8797 : : static meaning (static variables, static functions, types and
8798 : : enumerators) have to be visible. This recursive function calls
8799 : : pushclass for all enclosing class contexts until global or a local
8800 : : scope is reached. TYPE is the enclosed class. */
8801 : :
8802 : : void
8803 : 606477595 : push_nested_class (tree type)
8804 : : {
8805 : : /* A namespace might be passed in error cases, like A::B:C. */
8806 : 606477595 : if (type == NULL_TREE
8807 : 606477595 : || !CLASS_TYPE_P (type))
8808 : : return;
8809 : :
8810 : 311747146 : push_nested_class (DECL_CONTEXT (TYPE_MAIN_DECL (type)));
8811 : :
8812 : 311747146 : pushclass (type);
8813 : : }
8814 : :
8815 : : /* Undoes a push_nested_class call. */
8816 : :
8817 : : void
8818 : 294716901 : pop_nested_class (void)
8819 : : {
8820 : 311730898 : tree context = DECL_CONTEXT (TYPE_MAIN_DECL (current_class_type));
8821 : :
8822 : 311730898 : popclass ();
8823 : 311730898 : if (context && CLASS_TYPE_P (context))
8824 : : pop_nested_class ();
8825 : 294716901 : }
8826 : :
8827 : : /* Returns the number of extern "LANG" blocks we are nested within. */
8828 : :
8829 : : int
8830 : 3203 : current_lang_depth (void)
8831 : : {
8832 : 3203 : return vec_safe_length (current_lang_base);
8833 : : }
8834 : :
8835 : : /* Set global variables CURRENT_LANG_NAME to appropriate value
8836 : : so that behavior of name-mangling machinery is correct. */
8837 : :
8838 : : void
8839 : 1182765 : push_lang_context (tree name)
8840 : : {
8841 : 1182765 : vec_safe_push (current_lang_base, current_lang_name);
8842 : :
8843 : 1182765 : if (name == lang_name_cplusplus)
8844 : 612582 : current_lang_name = name;
8845 : 570183 : else if (name == lang_name_c)
8846 : 570183 : current_lang_name = name;
8847 : : else
8848 : 0 : error ("language string %<\"%E\"%> not recognized", name);
8849 : 1182765 : }
8850 : :
8851 : : /* Get out of the current language scope. */
8852 : :
8853 : : void
8854 : 1182765 : pop_lang_context (void)
8855 : : {
8856 : 1182765 : current_lang_name = current_lang_base->pop ();
8857 : 1182765 : }
8858 : :
8859 : : /* Type instantiation routines. */
8860 : :
8861 : : /* Given an OVERLOAD and a TARGET_TYPE, return the function that
8862 : : matches the TARGET_TYPE. If there is no satisfactory match, return
8863 : : error_mark_node, and issue an error & warning messages under
8864 : : control of FLAGS. Permit pointers to member function if FLAGS
8865 : : permits. If TEMPLATE_ONLY, the name of the overloaded function was
8866 : : a template-id, and EXPLICIT_TARGS are the explicitly provided
8867 : : template arguments.
8868 : :
8869 : : If OVERLOAD is for one or more member functions, then ACCESS_PATH
8870 : : is the base path used to reference those member functions. If
8871 : : the address is resolved to a member function, access checks will be
8872 : : performed and errors issued if appropriate. */
8873 : :
8874 : : static tree
8875 : 311023 : resolve_address_of_overloaded_function (tree target_type,
8876 : : tree overload,
8877 : : tsubst_flags_t complain,
8878 : : bool template_only,
8879 : : tree explicit_targs,
8880 : : tree access_path)
8881 : : {
8882 : : /* Here's what the standard says:
8883 : :
8884 : : [over.over]
8885 : :
8886 : : If the name is a function template, template argument deduction
8887 : : is done, and if the argument deduction succeeds, the deduced
8888 : : arguments are used to generate a single template function, which
8889 : : is added to the set of overloaded functions considered.
8890 : :
8891 : : Non-member functions and static member functions match targets of
8892 : : type "pointer-to-function" or "reference-to-function." Nonstatic
8893 : : member functions match targets of type "pointer-to-member
8894 : : function;" the function type of the pointer to member is used to
8895 : : select the member function from the set of overloaded member
8896 : : functions. If a non-static member function is selected, the
8897 : : reference to the overloaded function name is required to have the
8898 : : form of a pointer to member as described in 5.3.1.
8899 : :
8900 : : If more than one function is selected, any template functions in
8901 : : the set are eliminated if the set also contains a non-template
8902 : : function, and any given template function is eliminated if the
8903 : : set contains a second template function that is more specialized
8904 : : than the first according to the partial ordering rules 14.5.5.2.
8905 : : After such eliminations, if any, there shall remain exactly one
8906 : : selected function. */
8907 : :
8908 : 311023 : int is_ptrmem = 0;
8909 : : /* We store the matches in a TREE_LIST rooted here. The functions
8910 : : are the TREE_PURPOSE, not the TREE_VALUE, in this list, for easy
8911 : : interoperability with most_specialized_instantiation. */
8912 : 311023 : tree matches = NULL_TREE;
8913 : 311023 : tree fn;
8914 : 311023 : tree target_fn_type;
8915 : :
8916 : : /* By the time we get here, we should be seeing only real
8917 : : pointer-to-member types, not the internal POINTER_TYPE to
8918 : : METHOD_TYPE representation. */
8919 : 311023 : gcc_assert (!TYPE_PTR_P (target_type)
8920 : : || TREE_CODE (TREE_TYPE (target_type)) != METHOD_TYPE);
8921 : :
8922 : 311023 : gcc_assert (is_overloaded_fn (overload));
8923 : :
8924 : : /* Check that the TARGET_TYPE is reasonable. */
8925 : 46996 : if (TYPE_PTRFN_P (target_type)
8926 : 311027 : || TYPE_REFFN_P (target_type))
8927 : : /* This is OK. */;
8928 : 264029 : else if (TYPE_PTRMEMFUNC_P (target_type))
8929 : : /* This is OK, too. */
8930 : : is_ptrmem = 1;
8931 : 262771 : else if (TREE_CODE (target_type) == FUNCTION_TYPE)
8932 : : /* This is OK, too. This comes from a conversion to reference
8933 : : type. */
8934 : 262725 : target_type = build_reference_type (target_type);
8935 : : else
8936 : : {
8937 : 46 : if (complain & tf_error)
8938 : 37 : error ("cannot resolve overloaded function %qD based on"
8939 : : " conversion to type %qT",
8940 : 37 : OVL_NAME (overload), target_type);
8941 : 46 : return error_mark_node;
8942 : : }
8943 : :
8944 : : /* Non-member functions and static member functions match targets of type
8945 : : "pointer-to-function" or "reference-to-function." Nonstatic member
8946 : : functions match targets of type "pointer-to-member-function;" the
8947 : : function type of the pointer to member is used to select the member
8948 : : function from the set of overloaded member functions.
8949 : :
8950 : : So figure out the FUNCTION_TYPE that we want to match against. */
8951 : 310977 : target_fn_type = static_fn_type (target_type);
8952 : :
8953 : : /* If we can find a non-template function that matches, we can just
8954 : : use it. There's no point in generating template instantiations
8955 : : if we're just going to throw them out anyhow. But, of course, we
8956 : : can only do this when we don't *need* a template function. */
8957 : 310977 : if (!template_only)
8958 : 2145414 : for (lkp_iterator iter (overload); iter; ++iter)
8959 : : {
8960 : 1861034 : tree fn = *iter;
8961 : :
8962 : 1861034 : if (TREE_CODE (fn) == TEMPLATE_DECL)
8963 : : /* We're not looking for templates just yet. */
8964 : 20483 : continue;
8965 : :
8966 : 1840551 : if ((TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE) != is_ptrmem)
8967 : : /* We're looking for a non-static member, and this isn't
8968 : : one, or vice versa. */
8969 : 580 : continue;
8970 : :
8971 : : /* Constraints must be satisfied. This is done before
8972 : : return type deduction since that instantiates the
8973 : : function. */
8974 : 1839971 : if (!constraints_satisfied_p (fn))
8975 : 27 : continue;
8976 : :
8977 : : /* For target_version semantics, never resolve a non-default
8978 : : version. */
8979 : 1839944 : if (!TARGET_HAS_FMV_TARGET_ATTRIBUTE
8980 : : && TREE_CODE (fn) == FUNCTION_DECL
8981 : : && !is_function_default_version (fn))
8982 : : continue;
8983 : :
8984 : 1839944 : if (undeduced_auto_decl (fn))
8985 : : {
8986 : : /* Force instantiation to do return type deduction. */
8987 : 18 : maybe_instantiate_decl (fn);
8988 : 18 : require_deduced_type (fn);
8989 : : }
8990 : :
8991 : : /* In C++17 we need the noexcept-qualifier to compare types. */
8992 : 1839944 : if (flag_noexcept_type
8993 : 1839944 : && !maybe_instantiate_noexcept (fn, complain))
8994 : 0 : continue;
8995 : :
8996 : : /* See if there's a match. */
8997 : 1839944 : tree fntype = static_fn_type (fn);
8998 : 1839944 : if (same_type_p (target_fn_type, fntype)
8999 : 1839944 : || fnptr_conv_p (target_fn_type, fntype))
9000 : 264549 : matches = tree_cons (fn, NULL_TREE, matches);
9001 : : }
9002 : :
9003 : : /* Now, if we've already got a match (or matches), there's no need
9004 : : to proceed to the template functions. But, if we don't have a
9005 : : match we need to look at them, too. */
9006 : 284380 : if (!matches)
9007 : : {
9008 : 47001 : tree target_arg_types;
9009 : 47001 : tree target_ret_type;
9010 : 47001 : tree *args;
9011 : 47001 : unsigned int nargs, ia;
9012 : 47001 : tree arg;
9013 : :
9014 : 47001 : target_arg_types = TYPE_ARG_TYPES (target_fn_type);
9015 : 47001 : target_ret_type = TREE_TYPE (target_fn_type);
9016 : :
9017 : 47001 : nargs = list_length (target_arg_types);
9018 : 47001 : args = XALLOCAVEC (tree, nargs);
9019 : 47001 : for (arg = target_arg_types, ia = 0;
9020 : 150625 : arg != NULL_TREE;
9021 : 103624 : arg = TREE_CHAIN (arg), ++ia)
9022 : 103624 : args[ia] = TREE_VALUE (arg);
9023 : 47001 : nargs = ia;
9024 : :
9025 : 96918 : for (lkp_iterator iter (overload); iter; ++iter)
9026 : : {
9027 : 49917 : tree fn = *iter;
9028 : 49917 : tree instantiation;
9029 : 49917 : tree targs;
9030 : :
9031 : 49917 : if (TREE_CODE (fn) != TEMPLATE_DECL)
9032 : : /* We're only looking for templates. */
9033 : 3084 : continue;
9034 : :
9035 : 46833 : if ((TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
9036 : : != is_ptrmem)
9037 : : /* We're not looking for a non-static member, and this is
9038 : : one, or vice versa. */
9039 : 12 : continue;
9040 : :
9041 : 46821 : tree ret = target_ret_type;
9042 : :
9043 : : /* If the template has a deduced return type, don't expose it to
9044 : : template argument deduction. */
9045 : 46821 : if (undeduced_auto_decl (fn))
9046 : 230 : ret = NULL_TREE;
9047 : :
9048 : : /* Try to do argument deduction. */
9049 : 46821 : targs = make_tree_vec (DECL_NTPARMS (fn));
9050 : 46821 : instantiation = fn_type_unification (fn, explicit_targs, targs, args,
9051 : : nargs, ret,
9052 : : DEDUCE_EXACT, LOOKUP_NORMAL,
9053 : : NULL, false, false);
9054 : 46821 : if (instantiation == error_mark_node)
9055 : : /* Instantiation failed. */
9056 : 9816 : continue;
9057 : :
9058 : : /* Constraints must be satisfied. This is done before
9059 : : return type deduction since that instantiates the
9060 : : function. */
9061 : 37005 : if (flag_concepts && !constraints_satisfied_p (instantiation))
9062 : 0 : continue;
9063 : :
9064 : : /* And now force instantiation to do return type deduction. */
9065 : 37005 : if (undeduced_auto_decl (instantiation))
9066 : : {
9067 : 68 : ++function_depth;
9068 : 68 : instantiate_decl (instantiation, /*defer*/false, /*class*/false);
9069 : 68 : --function_depth;
9070 : :
9071 : 68 : require_deduced_type (instantiation);
9072 : : }
9073 : :
9074 : : /* In C++17 we need the noexcept-qualifier to compare types. */
9075 : 37005 : if (flag_noexcept_type)
9076 : 36174 : maybe_instantiate_noexcept (instantiation, complain);
9077 : :
9078 : : /* See if there's a match. */
9079 : 37005 : tree fntype = static_fn_type (instantiation);
9080 : 37005 : if (same_type_p (target_fn_type, fntype)
9081 : 37005 : || fnptr_conv_p (target_fn_type, fntype))
9082 : 36987 : matches = tree_cons (instantiation, fn, matches);
9083 : : }
9084 : :
9085 : : /* Now, remove all but the most specialized of the matches. */
9086 : 47001 : if (matches)
9087 : : {
9088 : 36882 : tree match = most_specialized_instantiation (matches);
9089 : :
9090 : 36882 : if (match != error_mark_node)
9091 : : {
9092 : 36855 : matches = match;
9093 : 36855 : TREE_CHAIN (match) = NULL_TREE;
9094 : : }
9095 : : }
9096 : : }
9097 : 263976 : else if (flag_concepts && TREE_CHAIN (matches))
9098 : 53 : if (tree match = most_constrained_function (matches))
9099 : : {
9100 : 9 : matches = match;
9101 : 9 : TREE_CHAIN (match) = NULL_TREE;
9102 : : }
9103 : :
9104 : : /* Now we should have exactly one function in MATCHES. */
9105 : 300858 : if (matches == NULL_TREE)
9106 : : {
9107 : : /* There were *no* matches. */
9108 : 10119 : if (complain & tf_error)
9109 : : {
9110 : 92 : auto_diagnostic_group d;
9111 : 184 : error ("no matches converting function %qD to type %q#T",
9112 : 92 : OVL_NAME (overload), target_type);
9113 : :
9114 : 92 : print_candidates (overload);
9115 : 92 : }
9116 : 10119 : return error_mark_node;
9117 : : }
9118 : 300858 : else if (TREE_CHAIN (matches))
9119 : : {
9120 : : /* There were too many matches. First check if they're all
9121 : : the same function. */
9122 : 159 : tree match = NULL_TREE;
9123 : :
9124 : 159 : fn = TREE_PURPOSE (matches);
9125 : :
9126 : : /* For multi-versioned functions, more than one match is just fine and
9127 : : decls_match will return false as they are different. */
9128 : 723 : for (match = TREE_CHAIN (matches); match; match = TREE_CHAIN (match))
9129 : 591 : if (!decls_match (fn, TREE_PURPOSE (match))
9130 : 1167 : && !disjoint_version_decls (fn, TREE_PURPOSE (match)))
9131 : : break;
9132 : :
9133 : 159 : if (match)
9134 : : {
9135 : 27 : if (complain & tf_error)
9136 : : {
9137 : 9 : auto_diagnostic_group d;
9138 : 9 : error ("converting overloaded function %qD to type %q#T is ambiguous",
9139 : 9 : OVL_NAME (overload), target_type);
9140 : :
9141 : : /* Since print_candidates expects the functions in the
9142 : : TREE_VALUE slot, we flip them here. */
9143 : 36 : for (match = matches; match; match = TREE_CHAIN (match))
9144 : 18 : TREE_VALUE (match) = TREE_PURPOSE (match);
9145 : :
9146 : 9 : print_candidates (matches);
9147 : 9 : }
9148 : :
9149 : 27 : return error_mark_node;
9150 : : }
9151 : : }
9152 : :
9153 : : /* Good, exactly one match. Now, convert it to the correct type. */
9154 : 300831 : fn = TREE_PURPOSE (matches);
9155 : :
9156 : 600488 : if (DECL_OBJECT_MEMBER_FUNCTION_P (fn)
9157 : 301095 : && !(complain & tf_ptrmem_ok))
9158 : : {
9159 : : /* Previously we allowed this behavior for iobj member functions when the
9160 : : -fms-extensions flag is passed as MSVC allows this as a language
9161 : : extension. MSVC also allows this for xobj member functions, but the
9162 : : documentation for -fms-extensions states it's purpose is to support
9163 : : the use of microsoft headers. Until otherwise demonstrated, we should
9164 : : assume xobj member functions are not used in this manner in microsoft
9165 : : headers and forbid the incorrect syntax instead of supporting it for
9166 : : non-legacy uses. This should hopefully encourage conformance going
9167 : : forward.
9168 : : This comment is referred to in typeck.cc:cp_build_addr_expr_1. */
9169 : 270 : if (DECL_IOBJ_MEMBER_FUNCTION_P (fn) && flag_ms_extensions)
9170 : : /* Early escape. */;
9171 : 249 : else if (!(complain & tf_error))
9172 : 179 : return error_mark_node;
9173 : 70 : else if (DECL_XOBJ_MEMBER_FUNCTION_P (fn))
9174 : : {
9175 : 34 : auto_diagnostic_group d;
9176 : : /* Should match the error in typeck.cc:cp_build_addr_expr_1.
9177 : : We seem to lack the details here to match that diagnostic exactly,
9178 : : perhaps this could be fixed in the future? See PR113075 bug 2. */
9179 : 34 : error_at (input_location,
9180 : : "ISO C++ forbids taking the address of an unqualified"
9181 : : " or parenthesized non-static member function to form"
9182 : : " a pointer to explicit object member function.");
9183 : : /* This is incorrect, see PR113075 bug 3. */
9184 : 34 : inform (input_location,
9185 : : "a pointer to explicit object member function can only be "
9186 : : "formed with %<&%E%>", fn);
9187 : 34 : }
9188 : : else
9189 : : {
9190 : 36 : static int explained;
9191 : 36 : gcc_assert (DECL_IOBJ_MEMBER_FUNCTION_P (fn) && !flag_ms_extensions);
9192 : : /* Is there a reason this error message doesn't match the one in
9193 : : typeck.cc:cp_build_addr_expr_1? */
9194 : 36 : auto_diagnostic_group d;
9195 : 36 : if (permerror (input_location, "assuming pointer to member %qD", fn)
9196 : 36 : && !explained)
9197 : : {
9198 : 18 : inform (input_location, "(a pointer to member can only be "
9199 : : "formed with %<&%E%>)", fn);
9200 : 18 : explained = 1;
9201 : : }
9202 : 36 : }
9203 : : }
9204 : :
9205 : : /* If a pointer to a function that is multi-versioned is requested, the
9206 : : pointer to the dispatcher function is returned instead. This works
9207 : : well because indirectly calling the function will dispatch the right
9208 : : function version at run-time.
9209 : : This is done at multiple_target.cc for target_version semantics. */
9210 : :
9211 : 300652 : if (DECL_FUNCTION_VERSIONED (fn) && TARGET_HAS_FMV_TARGET_ATTRIBUTE)
9212 : : {
9213 : 117 : fn = get_function_version_dispatcher (fn);
9214 : 117 : if (fn == NULL)
9215 : 9 : return error_mark_node;
9216 : : /* Mark all the versions corresponding to the dispatcher as used. */
9217 : 108 : if (!(complain & tf_conv))
9218 : 36 : mark_versions_used (fn);
9219 : : }
9220 : :
9221 : : /* If we're doing overload resolution purely for the purpose of
9222 : : determining conversion sequences, we should not consider the
9223 : : function used. If this conversion sequence is selected, the
9224 : : function will be marked as used at this point. */
9225 : 300571 : if (!(complain & tf_conv))
9226 : : {
9227 : : /* Make =delete work with SFINAE. */
9228 : 280091 : if (DECL_DELETED_FN (fn) && !(complain & tf_error))
9229 : 0 : return error_mark_node;
9230 : 280091 : if (!mark_used (fn, complain) && !(complain & tf_error))
9231 : 0 : return error_mark_node;
9232 : : }
9233 : :
9234 : : /* We could not check access to member functions when this
9235 : : expression was originally created since we did not know at that
9236 : : time to which function the expression referred. */
9237 : 300643 : if (DECL_FUNCTION_MEMBER_P (fn))
9238 : : {
9239 : 7451 : gcc_assert (access_path);
9240 : 7451 : perform_or_defer_access_check (access_path, fn, fn, complain);
9241 : : }
9242 : :
9243 : 300643 : if (TYPE_PTRFN_P (target_type) || TYPE_PTRMEMFUNC_P (target_type))
9244 : 38524 : return cp_build_addr_expr (fn, complain);
9245 : : else
9246 : : {
9247 : : /* The target must be a REFERENCE_TYPE. Above, cp_build_unary_op
9248 : : will mark the function as addressed, but here we must do it
9249 : : explicitly. */
9250 : 262119 : cxx_mark_addressable (fn);
9251 : :
9252 : 262119 : return fn;
9253 : : }
9254 : : }
9255 : :
9256 : : /* This function will instantiate the type of the expression given in
9257 : : RHS to match the type of LHSTYPE. If errors exist, then return
9258 : : error_mark_node. COMPLAIN is a bit mask. If TF_ERROR is set, then
9259 : : we complain on errors. If we are not complaining, never modify rhs,
9260 : : as overload resolution wants to try many possible instantiations, in
9261 : : the hope that at least one will work.
9262 : :
9263 : : For non-recursive calls, LHSTYPE should be a function, pointer to
9264 : : function, or a pointer to member function. */
9265 : :
9266 : : tree
9267 : 313466 : instantiate_type (tree lhstype, tree rhs, tsubst_flags_t complain)
9268 : : {
9269 : 319481 : tsubst_flags_t complain_in = complain;
9270 : 319481 : tree access_path = NULL_TREE;
9271 : :
9272 : 319481 : complain &= ~tf_ptrmem_ok;
9273 : :
9274 : 319481 : STRIP_ANY_LOCATION_WRAPPER (rhs);
9275 : :
9276 : 319481 : if (lhstype == unknown_type_node)
9277 : : {
9278 : 0 : if (complain & tf_error)
9279 : 0 : error ("not enough type information");
9280 : 0 : return error_mark_node;
9281 : : }
9282 : :
9283 : 319481 : if (TREE_TYPE (rhs) != NULL_TREE && ! (type_unknown_p (rhs)))
9284 : : {
9285 : 1852 : tree fntype = non_reference (lhstype);
9286 : 1852 : if (same_type_p (fntype, TREE_TYPE (rhs)))
9287 : : return rhs;
9288 : 321 : if (fnptr_conv_p (fntype, TREE_TYPE (rhs)))
9289 : : return rhs;
9290 : 318 : if (flag_ms_extensions
9291 : 18 : && TYPE_PTRMEMFUNC_P (fntype)
9292 : 336 : && !TYPE_PTRMEMFUNC_P (TREE_TYPE (rhs)))
9293 : : /* Microsoft allows `A::f' to be resolved to a
9294 : : pointer-to-member. */
9295 : : ;
9296 : : else
9297 : : {
9298 : 300 : if (complain & tf_error)
9299 : 43 : error ("cannot convert %qE from type %qT to type %qT",
9300 : 43 : rhs, TREE_TYPE (rhs), fntype);
9301 : 300 : return error_mark_node;
9302 : : }
9303 : : }
9304 : :
9305 : : /* If we instantiate a template, and it is a A ?: C expression
9306 : : with omitted B, look through the SAVE_EXPR. */
9307 : 317647 : if (TREE_CODE (rhs) == SAVE_EXPR)
9308 : 9 : rhs = TREE_OPERAND (rhs, 0);
9309 : :
9310 : 317647 : if (BASELINK_P (rhs))
9311 : : {
9312 : 7799 : access_path = BASELINK_ACCESS_BINFO (rhs);
9313 : 7799 : rhs = BASELINK_FUNCTIONS (rhs);
9314 : : }
9315 : :
9316 : : /* There are only a few kinds of expressions that may have a type
9317 : : dependent on overload resolution. */
9318 : 317647 : gcc_assert (TREE_CODE (rhs) == ADDR_EXPR
9319 : : || TREE_CODE (rhs) == COMPONENT_REF
9320 : : || is_overloaded_fn (rhs)
9321 : : || (flag_ms_extensions && TREE_CODE (rhs) == FUNCTION_DECL));
9322 : :
9323 : : /* This should really only be used when attempting to distinguish
9324 : : what sort of a pointer to function we have. For now, any
9325 : : arithmetic operation which is not supported on pointers
9326 : : is rejected as an error. */
9327 : :
9328 : 317647 : switch (TREE_CODE (rhs))
9329 : : {
9330 : 609 : case COMPONENT_REF:
9331 : 609 : {
9332 : 609 : tree member = TREE_OPERAND (rhs, 1);
9333 : :
9334 : 609 : member = instantiate_type (lhstype, member, complain);
9335 : 609 : if (member != error_mark_node
9336 : 609 : && TREE_SIDE_EFFECTS (TREE_OPERAND (rhs, 0)))
9337 : : /* Do not lose object's side effects. */
9338 : 27 : return build2 (COMPOUND_EXPR, TREE_TYPE (member),
9339 : 54 : TREE_OPERAND (rhs, 0), member);
9340 : : return member;
9341 : : }
9342 : :
9343 : 1955 : case OFFSET_REF:
9344 : 1955 : rhs = TREE_OPERAND (rhs, 1);
9345 : 1955 : if (BASELINK_P (rhs))
9346 : : return instantiate_type (lhstype, rhs, complain_in);
9347 : :
9348 : : /* This can happen if we are forming a pointer-to-member for a
9349 : : member template. */
9350 : 0 : gcc_assert (TREE_CODE (rhs) == TEMPLATE_ID_EXPR);
9351 : :
9352 : : /* Fall through. */
9353 : :
9354 : 26609 : case TEMPLATE_ID_EXPR:
9355 : 26609 : {
9356 : 26609 : tree fns = TREE_OPERAND (rhs, 0);
9357 : 26609 : tree args = TREE_OPERAND (rhs, 1);
9358 : :
9359 : 26609 : return
9360 : 26609 : resolve_address_of_overloaded_function (lhstype, fns, complain_in,
9361 : : /*template_only=*/true,
9362 : 26609 : args, access_path);
9363 : : }
9364 : :
9365 : 284414 : case OVERLOAD:
9366 : 284414 : case FUNCTION_DECL:
9367 : 284414 : return
9368 : 284414 : resolve_address_of_overloaded_function (lhstype, rhs, complain_in,
9369 : : /*template_only=*/false,
9370 : : /*explicit_targs=*/NULL_TREE,
9371 : 284414 : access_path);
9372 : :
9373 : 4060 : case ADDR_EXPR:
9374 : 4060 : {
9375 : 4060 : if (PTRMEM_OK_P (rhs))
9376 : 1955 : complain |= tf_ptrmem_ok;
9377 : :
9378 : 4060 : return instantiate_type (lhstype, TREE_OPERAND (rhs, 0), complain);
9379 : : }
9380 : :
9381 : 0 : case ERROR_MARK:
9382 : 0 : return error_mark_node;
9383 : :
9384 : 0 : default:
9385 : 0 : gcc_unreachable ();
9386 : : }
9387 : : return error_mark_node;
9388 : : }
9389 : :
9390 : : /* Return the name of the virtual function pointer field
9391 : : (as an IDENTIFIER_NODE) for the given TYPE. Note that
9392 : : this may have to look back through base types to find the
9393 : : ultimate field name. (For single inheritance, these could
9394 : : all be the same name. Who knows for multiple inheritance). */
9395 : :
9396 : : static tree
9397 : 243916 : get_vfield_name (tree type)
9398 : : {
9399 : 243916 : tree binfo, base_binfo;
9400 : :
9401 : 243916 : for (binfo = TYPE_BINFO (type);
9402 : 243916 : BINFO_N_BASE_BINFOS (binfo);
9403 : : binfo = base_binfo)
9404 : : {
9405 : 75616 : base_binfo = BINFO_BASE_BINFO (binfo, 0);
9406 : :
9407 : 151232 : if (BINFO_VIRTUAL_P (base_binfo)
9408 : 75616 : || !TYPE_CONTAINS_VPTR_P (BINFO_TYPE (base_binfo)))
9409 : : break;
9410 : : }
9411 : :
9412 : 243916 : type = BINFO_TYPE (binfo);
9413 : 243916 : tree ctor_name = constructor_name (type);
9414 : 243916 : char *buf = (char *) alloca (sizeof (VFIELD_NAME_FORMAT)
9415 : : + IDENTIFIER_LENGTH (ctor_name) + 2);
9416 : 243916 : sprintf (buf, VFIELD_NAME_FORMAT, IDENTIFIER_POINTER (ctor_name));
9417 : 243916 : return get_identifier (buf);
9418 : : }
9419 : :
9420 : : /* Build a dummy reference to ourselves so Derived::Base (and A::A) works,
9421 : : according to [class]:
9422 : : The class-name is also inserted
9423 : : into the scope of the class itself. For purposes of access checking,
9424 : : the inserted class name is treated as if it were a public member name. */
9425 : :
9426 : : void
9427 : 25735663 : build_self_reference (void)
9428 : : {
9429 : 25735663 : tree name = DECL_NAME (TYPE_NAME (current_class_type));
9430 : 25735663 : tree decl = build_lang_decl (TYPE_DECL, name, current_class_type);
9431 : :
9432 : 25735663 : DECL_NONLOCAL (decl) = 1;
9433 : 25735663 : DECL_CONTEXT (decl) = current_class_type;
9434 : 25735663 : DECL_ARTIFICIAL (decl) = 1;
9435 : 25735663 : SET_DECL_SELF_REFERENCE_P (decl);
9436 : 25735663 : set_underlying_type (decl);
9437 : 25735663 : set_instantiating_module (decl);
9438 : :
9439 : 25735663 : if (processing_template_decl)
9440 : 16874288 : decl = push_template_decl (decl);
9441 : :
9442 : 25735663 : tree saved_cas = current_access_specifier;
9443 : 25735663 : current_access_specifier = access_public_node;
9444 : 25735663 : finish_member_declaration (decl);
9445 : 25735663 : current_access_specifier = saved_cas;
9446 : 25735663 : }
9447 : :
9448 : : /* Returns 1 if TYPE contains only padding bytes. */
9449 : :
9450 : : int
9451 : 536892308 : is_empty_class (tree type)
9452 : : {
9453 : 536892308 : if (type == error_mark_node)
9454 : : return 0;
9455 : :
9456 : 536892305 : if (! CLASS_TYPE_P (type))
9457 : : return 0;
9458 : :
9459 : 321256004 : return CLASSTYPE_EMPTY_P (type);
9460 : : }
9461 : :
9462 : : /* Returns true if TYPE contains no actual data, just various
9463 : : possible combinations of empty classes. If IGNORE_VPTR is true,
9464 : : a vptr doesn't prevent the class from being considered empty. Typically
9465 : : we want to ignore the vptr on assignment, and not on initialization. */
9466 : :
9467 : : bool
9468 : 343371703 : is_really_empty_class (tree type, bool ignore_vptr)
9469 : : {
9470 : 343371703 : if (CLASS_TYPE_P (type))
9471 : : {
9472 : 58801735 : tree field;
9473 : 58801735 : tree binfo;
9474 : 58801735 : tree base_binfo;
9475 : 58801735 : int i;
9476 : :
9477 : : /* CLASSTYPE_EMPTY_P isn't set properly until the class is actually laid
9478 : : out, but we'd like to be able to check this before then. */
9479 : 58801735 : if (COMPLETE_TYPE_P (type) && is_empty_class (type))
9480 : : return true;
9481 : :
9482 : 41549159 : if (!ignore_vptr && TYPE_CONTAINS_VPTR_P (type))
9483 : : return false;
9484 : :
9485 : 54068005 : for (binfo = TYPE_BINFO (type), i = 0;
9486 : 54068005 : BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
9487 : 14045908 : if (!is_really_empty_class (BINFO_TYPE (base_binfo), ignore_vptr))
9488 : : return false;
9489 : 519731306 : for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
9490 : 502144609 : if (TREE_CODE (field) == FIELD_DECL
9491 : 24761766 : && !DECL_ARTIFICIAL (field)
9492 : : /* An unnamed bit-field is not a data member. */
9493 : 22611877 : && !DECL_UNNAMED_BIT_FIELD (field)
9494 : 524754014 : && !is_really_empty_class (TREE_TYPE (field), ignore_vptr))
9495 : : return false;
9496 : : return true;
9497 : : }
9498 : 284569968 : else if (TREE_CODE (type) == ARRAY_TYPE)
9499 : 1238262 : return (integer_zerop (array_type_nelts_top (type))
9500 : 1238262 : || is_really_empty_class (TREE_TYPE (type), ignore_vptr));
9501 : : return false;
9502 : : }
9503 : :
9504 : : /* Note that NAME was looked up while the current class was being
9505 : : defined and that the result of that lookup was DECL. */
9506 : :
9507 : : void
9508 : 1879662089 : maybe_note_name_used_in_class (tree name, tree decl)
9509 : : {
9510 : : /* If we're not defining a class, there's nothing to do. */
9511 : 1879662089 : if (!(innermost_scope_kind() == sk_class
9512 : 263207947 : && TYPE_BEING_DEFINED (current_class_type)
9513 : 464056458 : && !LAMBDA_TYPE_P (current_class_type)))
9514 : : return;
9515 : :
9516 : 232774411 : const cp_binding_level *blev = nullptr;
9517 : 232774411 : if (const cxx_binding *binding = IDENTIFIER_BINDING (name))
9518 : 194875218 : blev = binding->scope;
9519 : 232774411 : const cp_binding_level *lev = current_binding_level;
9520 : :
9521 : : /* Record the binding in the names_used tables for classes inside blev. */
9522 : 343122709 : for (int i = current_class_depth; i > 0; --i)
9523 : : {
9524 : 493607026 : tree type = (i == current_class_depth
9525 : 246803513 : ? current_class_type
9526 : 14029102 : : current_class_stack[i].type);
9527 : :
9528 : 265169953 : for (; lev; lev = lev->level_chain)
9529 : : {
9530 : 265169953 : if (lev == blev)
9531 : : /* We found the declaration. */
9532 : : return;
9533 : 128714738 : if (lev->kind == sk_class && lev->this_entity == type)
9534 : : /* This class is inside the declaration scope. */
9535 : : break;
9536 : : }
9537 : :
9538 : 110348298 : auto &names_used = current_class_stack[i-1].names_used;
9539 : 110348298 : if (!names_used)
9540 : 14280157 : names_used = splay_tree_new (splay_tree_compare_pointers, 0, 0);
9541 : :
9542 : 110348298 : tree use = build1_loc (input_location, VIEW_CONVERT_EXPR,
9543 : 110348298 : TREE_TYPE (decl), decl);
9544 : 110348298 : EXPR_LOCATION_WRAPPER_P (use) = 1;
9545 : 110348298 : splay_tree_insert (names_used,
9546 : : (splay_tree_key) name,
9547 : : (splay_tree_value) use);
9548 : : }
9549 : : }
9550 : :
9551 : : /* Note that NAME was declared (as DECL) in the current class. Check
9552 : : to see that the declaration is valid under [class.member.lookup]:
9553 : :
9554 : : If [the result of a search in T for N at point P] differs from the result of
9555 : : a search in T for N from immediately after the class-specifier of T, the
9556 : : program is ill-formed, no diagnostic required. */
9557 : :
9558 : : void
9559 : 278902724 : note_name_declared_in_class (tree name, tree decl)
9560 : : {
9561 : 278902724 : splay_tree names_used;
9562 : 278902724 : splay_tree_node n;
9563 : :
9564 : : /* Look to see if we ever used this name. */
9565 : 278902724 : names_used
9566 : 278902724 : = current_class_stack[current_class_depth - 1].names_used;
9567 : 278902724 : if (!names_used)
9568 : : return;
9569 : : /* The C language allows members to be declared with a type of the same
9570 : : name, and the C++ standard says this diagnostic is not required. So
9571 : : allow it in extern "C" blocks unless pedantic is specified.
9572 : : Allow it in all cases if -ms-extensions is specified. */
9573 : 90481110 : if ((!pedantic && current_lang_name == lang_name_c)
9574 : 88294739 : || flag_ms_extensions)
9575 : : return;
9576 : 88294733 : n = splay_tree_lookup (names_used, (splay_tree_key) name);
9577 : 88294733 : if (n)
9578 : : {
9579 : 60 : tree use = (tree) n->value;
9580 : 60 : location_t loc = EXPR_LOCATION (use);
9581 : 60 : tree olddecl = OVL_FIRST (TREE_OPERAND (use, 0));
9582 : : /* [basic.scope.class]
9583 : :
9584 : : A name N used in a class S shall refer to the same declaration
9585 : : in its context and when re-evaluated in the completed scope of
9586 : : S. */
9587 : 60 : auto ov = make_temp_override (global_dc->m_pedantic_errors);
9588 : 60 : if (TREE_CODE (decl) == TYPE_DECL
9589 : 21 : && TREE_CODE (olddecl) == TYPE_DECL
9590 : 81 : && same_type_p (TREE_TYPE (decl), TREE_TYPE (olddecl)))
9591 : : /* Different declaration, but same meaning; just warn. */;
9592 : 54 : else if (flag_permissive)
9593 : : /* Let -fpermissive make it a warning like past versions. */;
9594 : : else
9595 : : /* Make it an error. */
9596 : 33 : global_dc->m_pedantic_errors = 1;
9597 : :
9598 : 60 : auto_diagnostic_group d;
9599 : 120 : if (pedwarn (location_of (decl), OPT_Wchanges_meaning,
9600 : : "declaration of %q#D changes meaning of %qD",
9601 : 60 : decl, OVL_NAME (decl)))
9602 : : {
9603 : 51 : inform (loc, "used here to mean %q#D", olddecl);
9604 : 51 : inform (location_of (olddecl), "declared here" );
9605 : : }
9606 : 60 : }
9607 : : }
9608 : :
9609 : : /* Returns the VAR_DECL for the complete vtable associated with BINFO.
9610 : : Secondary vtables are merged with primary vtables; this function
9611 : : will return the VAR_DECL for the primary vtable. */
9612 : :
9613 : : tree
9614 : 5818507 : get_vtbl_decl_for_binfo (tree binfo)
9615 : : {
9616 : 5818507 : tree decl;
9617 : :
9618 : 5818507 : decl = BINFO_VTABLE (binfo);
9619 : 5818507 : if (decl && TREE_CODE (decl) == POINTER_PLUS_EXPR)
9620 : : {
9621 : 5818504 : gcc_assert (TREE_CODE (TREE_OPERAND (decl, 0)) == ADDR_EXPR);
9622 : 5818504 : decl = TREE_OPERAND (TREE_OPERAND (decl, 0), 0);
9623 : : }
9624 : 5818504 : if (decl)
9625 : 5818504 : gcc_assert (VAR_P (decl));
9626 : 5818507 : return decl;
9627 : : }
9628 : :
9629 : :
9630 : : /* Returns the binfo for the primary base of BINFO. If the resulting
9631 : : BINFO is a virtual base, and it is inherited elsewhere in the
9632 : : hierarchy, then the returned binfo might not be the primary base of
9633 : : BINFO in the complete object. Check BINFO_PRIMARY_P or
9634 : : BINFO_LOST_PRIMARY_P to be sure. */
9635 : :
9636 : : static tree
9637 : 40772410 : get_primary_binfo (tree binfo)
9638 : : {
9639 : 40772410 : tree primary_base;
9640 : :
9641 : 40772410 : primary_base = CLASSTYPE_PRIMARY_BINFO (BINFO_TYPE (binfo));
9642 : 40772410 : if (!primary_base)
9643 : : return NULL_TREE;
9644 : :
9645 : 12441195 : return copied_binfo (primary_base, binfo);
9646 : : }
9647 : :
9648 : : /* As above, but iterate until we reach the binfo that actually provides the
9649 : : vptr for BINFO. */
9650 : :
9651 : : static tree
9652 : 2569631 : most_primary_binfo (tree binfo)
9653 : : {
9654 : 2569631 : tree b = binfo;
9655 : 8018503 : while (CLASSTYPE_HAS_PRIMARY_BASE_P (BINFO_TYPE (b))
9656 : 10897744 : && !BINFO_LOST_PRIMARY_P (b))
9657 : : {
9658 : 2879241 : tree primary_base = get_primary_binfo (b);
9659 : 2879241 : gcc_assert (BINFO_PRIMARY_P (primary_base)
9660 : : && BINFO_INHERITANCE_CHAIN (primary_base) == b);
9661 : : b = primary_base;
9662 : : }
9663 : 2569631 : return b;
9664 : : }
9665 : :
9666 : : /* Returns true if BINFO gets its vptr from a virtual base of the most derived
9667 : : type. Note that the virtual inheritance might be above or below BINFO in
9668 : : the hierarchy. */
9669 : :
9670 : : bool
9671 : 149 : vptr_via_virtual_p (tree binfo)
9672 : : {
9673 : 149 : if (TYPE_P (binfo))
9674 : 0 : binfo = TYPE_BINFO (binfo);
9675 : 149 : tree primary = most_primary_binfo (binfo);
9676 : : /* Don't limit binfo_via_virtual, we want to return true when BINFO itself is
9677 : : a morally virtual base. */
9678 : 149 : tree virt = binfo_via_virtual (primary, NULL_TREE);
9679 : 149 : return virt != NULL_TREE;
9680 : : }
9681 : :
9682 : : /* If INDENTED_P is zero, indent to INDENT. Return nonzero. */
9683 : :
9684 : : static int
9685 : 129 : maybe_indent_hierarchy (FILE * stream, int indent, int indented_p)
9686 : : {
9687 : 0 : if (!indented_p)
9688 : 39 : fprintf (stream, "%*s", indent, "");
9689 : 129 : return 1;
9690 : : }
9691 : :
9692 : : /* Dump the offsets of all the bases rooted at BINFO to STREAM.
9693 : : INDENT should be zero when called from the top level; it is
9694 : : incremented recursively. IGO indicates the next expected BINFO in
9695 : : inheritance graph ordering. */
9696 : :
9697 : : static tree
9698 : 102 : dump_class_hierarchy_r (FILE *stream,
9699 : : dump_flags_t flags,
9700 : : tree binfo,
9701 : : tree igo,
9702 : : int indent)
9703 : : {
9704 : 102 : int indented = 0;
9705 : 102 : tree base_binfo;
9706 : 102 : int i;
9707 : :
9708 : 204 : fprintf (stream, "%s (0x" HOST_WIDE_INT_PRINT_HEX ") ",
9709 : 102 : type_as_string (BINFO_TYPE (binfo), TFF_PLAIN_IDENTIFIER),
9710 : : (HOST_WIDE_INT) (uintptr_t) binfo);
9711 : 102 : if (binfo != igo)
9712 : : {
9713 : 27 : fprintf (stream, "alternative-path\n");
9714 : 27 : return igo;
9715 : : }
9716 : 75 : igo = TREE_CHAIN (binfo);
9717 : :
9718 : 75 : fprintf (stream, HOST_WIDE_INT_PRINT_DEC,
9719 : 75 : tree_to_shwi (BINFO_OFFSET (binfo)));
9720 : 75 : if (is_empty_class (BINFO_TYPE (binfo)))
9721 : 18 : fprintf (stream, " empty");
9722 : 57 : else if (CLASSTYPE_NEARLY_EMPTY_P (BINFO_TYPE (binfo)))
9723 : 12 : fprintf (stream, " nearly-empty");
9724 : 75 : if (BINFO_VIRTUAL_P (binfo))
9725 : 27 : fprintf (stream, " virtual");
9726 : 75 : fprintf (stream, "\n");
9727 : :
9728 : 75 : if (BINFO_PRIMARY_P (binfo))
9729 : : {
9730 : 15 : indented = maybe_indent_hierarchy (stream, indent + 3, indented);
9731 : 30 : fprintf (stream, " primary-for %s (0x" HOST_WIDE_INT_PRINT_HEX ")",
9732 : 15 : type_as_string (BINFO_TYPE (BINFO_INHERITANCE_CHAIN (binfo)),
9733 : : TFF_PLAIN_IDENTIFIER),
9734 : 15 : (HOST_WIDE_INT) (uintptr_t) BINFO_INHERITANCE_CHAIN (binfo));
9735 : : }
9736 : 75 : if (BINFO_LOST_PRIMARY_P (binfo))
9737 : : {
9738 : 9 : indented = maybe_indent_hierarchy (stream, indent + 3, indented);
9739 : 9 : fprintf (stream, " lost-primary");
9740 : : }
9741 : 75 : if (indented)
9742 : 24 : fprintf (stream, "\n");
9743 : :
9744 : 75 : if (!(flags & TDF_SLIM))
9745 : : {
9746 : 75 : int indented = 0;
9747 : :
9748 : 75 : if (BINFO_SUBVTT_INDEX (binfo))
9749 : : {
9750 : 21 : indented = maybe_indent_hierarchy (stream, indent + 3, indented);
9751 : 42 : fprintf (stream, " subvttidx=%s",
9752 : 21 : expr_as_string (BINFO_SUBVTT_INDEX (binfo),
9753 : : TFF_PLAIN_IDENTIFIER));
9754 : : }
9755 : 75 : if (BINFO_VPTR_INDEX (binfo))
9756 : : {
9757 : 30 : indented = maybe_indent_hierarchy (stream, indent + 3, indented);
9758 : 60 : fprintf (stream, " vptridx=%s",
9759 : 30 : expr_as_string (BINFO_VPTR_INDEX (binfo),
9760 : : TFF_PLAIN_IDENTIFIER));
9761 : : }
9762 : 75 : if (BINFO_VPTR_FIELD (binfo))
9763 : : {
9764 : 27 : indented = maybe_indent_hierarchy (stream, indent + 3, indented);
9765 : 54 : fprintf (stream, " vbaseoffset=%s",
9766 : 27 : expr_as_string (BINFO_VPTR_FIELD (binfo),
9767 : : TFF_PLAIN_IDENTIFIER));
9768 : : }
9769 : 75 : if (BINFO_VTABLE (binfo))
9770 : : {
9771 : 27 : indented = maybe_indent_hierarchy (stream, indent + 3, indented);
9772 : 27 : fprintf (stream, " vptr=%s",
9773 : 27 : expr_as_string (BINFO_VTABLE (binfo),
9774 : : TFF_PLAIN_IDENTIFIER));
9775 : : }
9776 : :
9777 : 75 : if (indented)
9778 : 51 : fprintf (stream, "\n");
9779 : : }
9780 : :
9781 : 153 : for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
9782 : 78 : igo = dump_class_hierarchy_r (stream, flags, base_binfo, igo, indent + 2);
9783 : :
9784 : : return igo;
9785 : : }
9786 : :
9787 : : /* Dump the BINFO hierarchy for T. */
9788 : :
9789 : : static void
9790 : 24 : dump_class_hierarchy_1 (FILE *stream, dump_flags_t flags, tree t)
9791 : : {
9792 : 24 : fprintf (stream, "Class %s\n", type_as_string (t, TFF_PLAIN_IDENTIFIER));
9793 : 96 : fprintf (stream, " size=" HOST_WIDE_INT_PRINT_UNSIGNED " align=%u\n",
9794 : 24 : tree_to_shwi (TYPE_SIZE (t)) / BITS_PER_UNIT,
9795 : 24 : TYPE_ALIGN (t) / BITS_PER_UNIT);
9796 : 24 : if (tree as_base = CLASSTYPE_AS_BASE (t))
9797 : 96 : fprintf (stream, " base size=" HOST_WIDE_INT_PRINT_UNSIGNED
9798 : : " base align=%u\n",
9799 : 24 : tree_to_shwi (TYPE_SIZE (as_base)) / BITS_PER_UNIT,
9800 : 24 : TYPE_ALIGN (as_base) / BITS_PER_UNIT);
9801 : 24 : dump_class_hierarchy_r (stream, flags, TYPE_BINFO (t), TYPE_BINFO (t), 0);
9802 : 24 : fprintf (stream, "\n");
9803 : 24 : }
9804 : :
9805 : : /* Debug interface to hierarchy dumping. */
9806 : :
9807 : : void
9808 : 0 : debug_class (tree t)
9809 : : {
9810 : 0 : dump_class_hierarchy_1 (stderr, TDF_SLIM, t);
9811 : 0 : }
9812 : :
9813 : : static void
9814 : 39307783 : dump_class_hierarchy (tree t)
9815 : : {
9816 : 39307783 : dump_flags_t flags;
9817 : 39307783 : if (FILE *stream = dump_begin (class_dump_id, &flags))
9818 : : {
9819 : 24 : dump_class_hierarchy_1 (stream, flags, t);
9820 : 24 : dump_end (class_dump_id, stream);
9821 : : }
9822 : 39307783 : }
9823 : :
9824 : : static void
9825 : 51 : dump_array (FILE * stream, tree decl)
9826 : : {
9827 : 51 : tree value;
9828 : 51 : unsigned HOST_WIDE_INT ix;
9829 : 51 : HOST_WIDE_INT elt;
9830 : 51 : tree size = TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (decl)));
9831 : :
9832 : 51 : elt = (tree_to_shwi (TYPE_SIZE (TREE_TYPE (TREE_TYPE (decl))))
9833 : : / BITS_PER_UNIT);
9834 : 51 : fprintf (stream, "%s:", decl_as_string (decl, TFF_PLAIN_IDENTIFIER));
9835 : 51 : fprintf (stream, " %s entries",
9836 : : expr_as_string (size_binop (PLUS_EXPR, size, size_one_node),
9837 : : TFF_PLAIN_IDENTIFIER));
9838 : 51 : fprintf (stream, "\n");
9839 : :
9840 : 519 : FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (DECL_INITIAL (decl)),
9841 : : ix, value)
9842 : 417 : fprintf (stream, "%-4ld %s\n", (long)(ix * elt),
9843 : : expr_as_string (value, TFF_PLAIN_IDENTIFIER));
9844 : 51 : }
9845 : :
9846 : : static void
9847 : 1947148 : dump_vtable (tree t, tree binfo, tree vtable)
9848 : : {
9849 : 1947148 : dump_flags_t flags;
9850 : 1947148 : FILE *stream = dump_begin (class_dump_id, &flags);
9851 : :
9852 : 1947148 : if (!stream)
9853 : 1947109 : return;
9854 : :
9855 : 39 : if (!(flags & TDF_SLIM))
9856 : : {
9857 : 39 : int ctor_vtbl_p = TYPE_BINFO (t) != binfo;
9858 : :
9859 : 96 : fprintf (stream, "%s for %s",
9860 : : ctor_vtbl_p ? "Construction vtable" : "Vtable",
9861 : 39 : type_as_string (BINFO_TYPE (binfo), TFF_PLAIN_IDENTIFIER));
9862 : 39 : if (ctor_vtbl_p)
9863 : : {
9864 : 21 : if (!BINFO_VIRTUAL_P (binfo))
9865 : 6 : fprintf (stream, " (0x" HOST_WIDE_INT_PRINT_HEX " instance)",
9866 : : (HOST_WIDE_INT) (uintptr_t) binfo);
9867 : 21 : fprintf (stream, " in %s", type_as_string (t, TFF_PLAIN_IDENTIFIER));
9868 : : }
9869 : 39 : fprintf (stream, "\n");
9870 : 39 : dump_array (stream, vtable);
9871 : 39 : fprintf (stream, "\n");
9872 : : }
9873 : :
9874 : 39 : dump_end (class_dump_id, stream);
9875 : : }
9876 : :
9877 : : static void
9878 : 189979 : dump_vtt (tree t, tree vtt)
9879 : : {
9880 : 189979 : dump_flags_t flags;
9881 : 189979 : FILE *stream = dump_begin (class_dump_id, &flags);
9882 : :
9883 : 189979 : if (!stream)
9884 : 189967 : return;
9885 : :
9886 : 12 : if (!(flags & TDF_SLIM))
9887 : : {
9888 : 12 : fprintf (stream, "VTT for %s\n",
9889 : : type_as_string (t, TFF_PLAIN_IDENTIFIER));
9890 : 12 : dump_array (stream, vtt);
9891 : 12 : fprintf (stream, "\n");
9892 : : }
9893 : :
9894 : 12 : dump_end (class_dump_id, stream);
9895 : : }
9896 : :
9897 : : /* Dump a function or thunk and its thunkees. */
9898 : :
9899 : : static void
9900 : 0 : dump_thunk (FILE *stream, int indent, tree thunk)
9901 : : {
9902 : 0 : static const char spaces[] = " ";
9903 : 0 : tree name = DECL_NAME (thunk);
9904 : 0 : tree thunks;
9905 : :
9906 : 0 : fprintf (stream, "%.*s%p %s %s", indent, spaces,
9907 : : (void *)thunk,
9908 : 0 : !DECL_THUNK_P (thunk) ? "function"
9909 : 0 : : DECL_THIS_THUNK_P (thunk) ? "this-thunk" : "covariant-thunk",
9910 : 0 : name ? IDENTIFIER_POINTER (name) : "<unset>");
9911 : 0 : if (DECL_THUNK_P (thunk))
9912 : : {
9913 : 0 : HOST_WIDE_INT fixed_adjust = THUNK_FIXED_OFFSET (thunk);
9914 : 0 : tree virtual_adjust = THUNK_VIRTUAL_OFFSET (thunk);
9915 : :
9916 : 0 : fprintf (stream, " fixed=" HOST_WIDE_INT_PRINT_DEC, fixed_adjust);
9917 : 0 : if (!virtual_adjust)
9918 : : /*NOP*/;
9919 : 0 : else if (DECL_THIS_THUNK_P (thunk))
9920 : 0 : fprintf (stream, " vcall=" HOST_WIDE_INT_PRINT_DEC,
9921 : : tree_to_shwi (virtual_adjust));
9922 : : else
9923 : 0 : fprintf (stream, " vbase=" HOST_WIDE_INT_PRINT_DEC "(%s)",
9924 : 0 : tree_to_shwi (BINFO_VPTR_FIELD (virtual_adjust)),
9925 : 0 : type_as_string (BINFO_TYPE (virtual_adjust), TFF_SCOPE));
9926 : 0 : if (THUNK_ALIAS (thunk))
9927 : 0 : fprintf (stream, " alias to %p", (void *)THUNK_ALIAS (thunk));
9928 : : }
9929 : 0 : fprintf (stream, "\n");
9930 : 0 : for (thunks = DECL_THUNKS (thunk); thunks; thunks = TREE_CHAIN (thunks))
9931 : 0 : dump_thunk (stream, indent + 2, thunks);
9932 : 0 : }
9933 : :
9934 : : /* Dump the thunks for FN. */
9935 : :
9936 : : void
9937 : 0 : debug_thunks (tree fn)
9938 : : {
9939 : 0 : dump_thunk (stderr, 0, fn);
9940 : 0 : }
9941 : :
9942 : : /* Virtual function table initialization. */
9943 : :
9944 : : /* Create all the necessary vtables for T and its base classes. */
9945 : :
9946 : : static void
9947 : 39307783 : finish_vtbls (tree t)
9948 : : {
9949 : 39307783 : tree vbase;
9950 : 39307783 : vec<constructor_elt, va_gc> *v = NULL;
9951 : 39307783 : tree vtable = BINFO_VTABLE (TYPE_BINFO (t));
9952 : :
9953 : : /* We lay out the primary and secondary vtables in one contiguous
9954 : : vtable. The primary vtable is first, followed by the non-virtual
9955 : : secondary vtables in inheritance graph order. */
9956 : 39307783 : accumulate_vtbl_inits (TYPE_BINFO (t), TYPE_BINFO (t), TYPE_BINFO (t),
9957 : : vtable, t, &v);
9958 : :
9959 : : /* Then come the virtual bases, also in inheritance graph order. */
9960 : 102561036 : for (vbase = TYPE_BINFO (t); vbase; vbase = TREE_CHAIN (vbase))
9961 : : {
9962 : 63253253 : if (!BINFO_VIRTUAL_P (vbase))
9963 : 63049581 : continue;
9964 : 203672 : accumulate_vtbl_inits (vbase, vbase, TYPE_BINFO (t), vtable, t, &v);
9965 : : }
9966 : :
9967 : 39307783 : if (BINFO_VTABLE (TYPE_BINFO (t)))
9968 : 1686331 : initialize_vtable (TYPE_BINFO (t), v);
9969 : 39307783 : }
9970 : :
9971 : : /* Initialize the vtable for BINFO with the INITS. */
9972 : :
9973 : : static void
9974 : 1686331 : initialize_vtable (tree binfo, vec<constructor_elt, va_gc> *inits)
9975 : : {
9976 : 1686331 : tree decl;
9977 : :
9978 : 3372662 : layout_vtable_decl (binfo, vec_safe_length (inits));
9979 : 1686331 : decl = get_vtbl_decl_for_binfo (binfo);
9980 : 1686331 : initialize_artificial_var (decl, inits);
9981 : 1686331 : dump_vtable (BINFO_TYPE (binfo), binfo, decl);
9982 : 1686331 : }
9983 : :
9984 : : /* Build the VTT (virtual table table) for T.
9985 : : A class requires a VTT if it has virtual bases.
9986 : :
9987 : : This holds
9988 : : 1 - primary virtual pointer for complete object T
9989 : : 2 - secondary VTTs for each direct non-virtual base of T which requires a
9990 : : VTT
9991 : : 3 - secondary virtual pointers for each direct or indirect base of T which
9992 : : has virtual bases or is reachable via a virtual path from T.
9993 : : 4 - secondary VTTs for each direct or indirect virtual base of T.
9994 : :
9995 : : Secondary VTTs look like complete object VTTs without part 4. */
9996 : :
9997 : : static void
9998 : 39307783 : build_vtt (tree t)
9999 : : {
10000 : 39307783 : tree type;
10001 : 39307783 : tree vtt;
10002 : 39307783 : tree index;
10003 : 39307783 : vec<constructor_elt, va_gc> *inits;
10004 : :
10005 : : /* Build up the initializers for the VTT. */
10006 : 39307783 : inits = NULL;
10007 : 39307783 : index = size_zero_node;
10008 : 39307783 : build_vtt_inits (TYPE_BINFO (t), t, &inits, &index);
10009 : :
10010 : : /* If we didn't need a VTT, we're done. */
10011 : 39307783 : if (!inits)
10012 : 39117804 : return;
10013 : :
10014 : : /* Figure out the type of the VTT. */
10015 : 379958 : type = build_array_of_n_type (const_ptr_type_node,
10016 : 189979 : inits->length ());
10017 : :
10018 : : /* Now, build the VTT object itself. */
10019 : 189979 : vtt = build_vtable (t, mangle_vtt_for_type (t), type);
10020 : 189979 : initialize_artificial_var (vtt, inits);
10021 : : /* Add the VTT to the vtables list. */
10022 : 189979 : DECL_CHAIN (vtt) = DECL_CHAIN (CLASSTYPE_VTABLES (t));
10023 : 189979 : DECL_CHAIN (CLASSTYPE_VTABLES (t)) = vtt;
10024 : :
10025 : 189979 : dump_vtt (t, vtt);
10026 : : }
10027 : :
10028 : : /* When building a secondary VTT, BINFO_VTABLE is set to a TREE_LIST with
10029 : : PURPOSE the RTTI_BINFO, VALUE the real vtable pointer for this binfo,
10030 : : and CHAIN the vtable pointer for this binfo after construction is
10031 : : complete. VALUE can also be another BINFO, in which case we recurse. */
10032 : :
10033 : : static tree
10034 : 1143321 : binfo_ctor_vtable (tree binfo)
10035 : : {
10036 : 1215224 : tree vt;
10037 : :
10038 : 1215224 : while (1)
10039 : : {
10040 : 1215224 : vt = BINFO_VTABLE (binfo);
10041 : 1215224 : if (TREE_CODE (vt) == TREE_LIST)
10042 : 761709 : vt = TREE_VALUE (vt);
10043 : 1215224 : if (TREE_CODE (vt) == TREE_BINFO)
10044 : : binfo = vt;
10045 : : else
10046 : : break;
10047 : : }
10048 : :
10049 : 1143321 : return vt;
10050 : : }
10051 : :
10052 : : /* Data for secondary VTT initialization. */
10053 : : struct secondary_vptr_vtt_init_data
10054 : : {
10055 : : /* Is this the primary VTT? */
10056 : : bool top_level_p;
10057 : :
10058 : : /* Current index into the VTT. */
10059 : : tree index;
10060 : :
10061 : : /* Vector of initializers built up. */
10062 : : vec<constructor_elt, va_gc> *inits;
10063 : :
10064 : : /* The type being constructed by this secondary VTT. */
10065 : : tree type_being_constructed;
10066 : : };
10067 : :
10068 : : /* Recursively build the VTT-initializer for BINFO (which is in the
10069 : : hierarchy dominated by T). INITS points to the end of the initializer
10070 : : list to date. INDEX is the VTT index where the next element will be
10071 : : replaced. Iff BINFO is the binfo for T, this is the top level VTT (i.e.
10072 : : not a subvtt for some base of T). When that is so, we emit the sub-VTTs
10073 : : for virtual bases of T. When it is not so, we build the constructor
10074 : : vtables for the BINFO-in-T variant. */
10075 : :
10076 : : static void
10077 : 39760849 : build_vtt_inits (tree binfo, tree t, vec<constructor_elt, va_gc> **inits,
10078 : : tree *index)
10079 : : {
10080 : 39760849 : int i;
10081 : 39760849 : tree b;
10082 : 39760849 : tree init;
10083 : 39760849 : secondary_vptr_vtt_init_data data;
10084 : 39760849 : int top_level_p = SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), t);
10085 : :
10086 : : /* We only need VTTs for subobjects with virtual bases. */
10087 : 39760849 : if (!CLASSTYPE_VBASECLASSES (BINFO_TYPE (binfo)))
10088 : 39310053 : return;
10089 : :
10090 : : /* We need to use a construction vtable if this is not the primary
10091 : : VTT. */
10092 : 450796 : if (!top_level_p)
10093 : : {
10094 : 260817 : build_ctor_vtbl_group (binfo, t);
10095 : :
10096 : : /* Record the offset in the VTT where this sub-VTT can be found. */
10097 : 260817 : BINFO_SUBVTT_INDEX (binfo) = *index;
10098 : : }
10099 : :
10100 : : /* Add the address of the primary vtable for the complete object. */
10101 : 450796 : init = binfo_ctor_vtable (binfo);
10102 : 450796 : CONSTRUCTOR_APPEND_ELT (*inits, NULL_TREE, init);
10103 : 450796 : if (top_level_p)
10104 : : {
10105 : 189979 : gcc_assert (!BINFO_VPTR_INDEX (binfo));
10106 : 189979 : BINFO_VPTR_INDEX (binfo) = *index;
10107 : : }
10108 : 450796 : *index = size_binop (PLUS_EXPR, *index, TYPE_SIZE_UNIT (ptr_type_node));
10109 : :
10110 : : /* Recursively add the secondary VTTs for non-virtual bases. */
10111 : 975391 : for (i = 0; BINFO_BASE_ITERATE (binfo, i, b); ++i)
10112 : 524595 : if (!BINFO_VIRTUAL_P (b))
10113 : 249394 : build_vtt_inits (b, t, inits, index);
10114 : :
10115 : : /* Add secondary virtual pointers for all subobjects of BINFO with
10116 : : either virtual bases or reachable along a virtual path, except
10117 : : subobjects that are non-virtual primary bases. */
10118 : 450796 : data.top_level_p = top_level_p;
10119 : 450796 : data.index = *index;
10120 : 450796 : data.inits = *inits;
10121 : 450796 : data.type_being_constructed = BINFO_TYPE (binfo);
10122 : :
10123 : 450796 : dfs_walk_once (binfo, dfs_build_secondary_vptr_vtt_inits, NULL, &data);
10124 : :
10125 : 450796 : *index = data.index;
10126 : :
10127 : : /* data.inits might have grown as we added secondary virtual pointers.
10128 : : Make sure our caller knows about the new vector. */
10129 : 450796 : *inits = data.inits;
10130 : :
10131 : 450796 : if (top_level_p)
10132 : : /* Add the secondary VTTs for virtual bases in inheritance graph
10133 : : order. */
10134 : 1019775 : for (b = TYPE_BINFO (BINFO_TYPE (binfo)); b; b = TREE_CHAIN (b))
10135 : : {
10136 : 829796 : if (!BINFO_VIRTUAL_P (b))
10137 : 626124 : continue;
10138 : :
10139 : 203672 : build_vtt_inits (b, t, inits, index);
10140 : : }
10141 : : else
10142 : : /* Remove the ctor vtables we created. */
10143 : 260817 : dfs_walk_all (binfo, dfs_fixup_binfo_vtbls, NULL, binfo);
10144 : : }
10145 : :
10146 : : /* Called from build_vtt_inits via dfs_walk. BINFO is the binfo for the base
10147 : : in most derived. DATA is a SECONDARY_VPTR_VTT_INIT_DATA structure. */
10148 : :
10149 : : static tree
10150 : 1830078 : dfs_build_secondary_vptr_vtt_inits (tree binfo, void *data_)
10151 : : {
10152 : 1830078 : secondary_vptr_vtt_init_data *data = (secondary_vptr_vtt_init_data *)data_;
10153 : :
10154 : : /* We don't care about bases that don't have vtables. */
10155 : 1830078 : if (!TYPE_VFIELD (BINFO_TYPE (binfo)))
10156 : : return dfs_skip_bases;
10157 : :
10158 : : /* We're only interested in proper subobjects of the type being
10159 : : constructed. */
10160 : 1806525 : if (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), data->type_being_constructed))
10161 : : return NULL_TREE;
10162 : :
10163 : : /* We're only interested in bases with virtual bases or reachable
10164 : : via a virtual path from the type being constructed. */
10165 : 2219273 : if (!(CLASSTYPE_VBASECLASSES (BINFO_TYPE (binfo))
10166 : 863544 : || binfo_via_virtual (binfo, data->type_being_constructed)))
10167 : : return dfs_skip_bases;
10168 : :
10169 : : /* We're not interested in non-virtual primary bases. */
10170 : 1355203 : if (!BINFO_VIRTUAL_P (binfo) && BINFO_PRIMARY_P (binfo))
10171 : : return NULL_TREE;
10172 : :
10173 : : /* Record the index where this secondary vptr can be found. */
10174 : 692525 : if (data->top_level_p)
10175 : : {
10176 : 263536 : gcc_assert (!BINFO_VPTR_INDEX (binfo));
10177 : 263536 : BINFO_VPTR_INDEX (binfo) = data->index;
10178 : :
10179 : 263536 : if (BINFO_VIRTUAL_P (binfo))
10180 : : {
10181 : : /* It's a primary virtual base, and this is not a
10182 : : construction vtable. Find the base this is primary of in
10183 : : the inheritance graph, and use that base's vtable
10184 : : now. */
10185 : 210140 : while (BINFO_PRIMARY_P (binfo))
10186 : 9425 : binfo = BINFO_INHERITANCE_CHAIN (binfo);
10187 : : }
10188 : : }
10189 : :
10190 : : /* Add the initializer for the secondary vptr itself. */
10191 : 692525 : CONSTRUCTOR_APPEND_ELT (data->inits, NULL_TREE, binfo_ctor_vtable (binfo));
10192 : :
10193 : : /* Advance the vtt index. */
10194 : 692525 : data->index = size_binop (PLUS_EXPR, data->index,
10195 : : TYPE_SIZE_UNIT (ptr_type_node));
10196 : :
10197 : 692525 : return NULL_TREE;
10198 : : }
10199 : :
10200 : : /* Called from build_vtt_inits via dfs_walk. After building
10201 : : constructor vtables and generating the sub-vtt from them, we need
10202 : : to restore the BINFO_VTABLES that were scribbled on. DATA is the
10203 : : binfo of the base whose sub vtt was generated. */
10204 : :
10205 : : static tree
10206 : 1161958 : dfs_fixup_binfo_vtbls (tree binfo, void* data)
10207 : : {
10208 : 1161958 : tree vtable = BINFO_VTABLE (binfo);
10209 : :
10210 : 1161958 : if (!TYPE_CONTAINS_VPTR_P (BINFO_TYPE (binfo)))
10211 : : /* If this class has no vtable, none of its bases do. */
10212 : : return dfs_skip_bases;
10213 : :
10214 : 1066775 : if (!vtable)
10215 : : /* This might be a primary base, so have no vtable in this
10216 : : hierarchy. */
10217 : : return NULL_TREE;
10218 : :
10219 : : /* If we scribbled the construction vtable vptr into BINFO, clear it
10220 : : out now. */
10221 : 731452 : if (TREE_CODE (vtable) == TREE_LIST
10222 : 731452 : && (TREE_PURPOSE (vtable) == (tree) data))
10223 : 689806 : BINFO_VTABLE (binfo) = TREE_CHAIN (vtable);
10224 : :
10225 : : return NULL_TREE;
10226 : : }
10227 : :
10228 : : /* Build the construction vtable group for BINFO which is in the
10229 : : hierarchy dominated by T. */
10230 : :
10231 : : static void
10232 : 260817 : build_ctor_vtbl_group (tree binfo, tree t)
10233 : : {
10234 : 260817 : tree type;
10235 : 260817 : tree vtbl;
10236 : 260817 : tree id;
10237 : 260817 : tree vbase;
10238 : 260817 : vec<constructor_elt, va_gc> *v;
10239 : :
10240 : : /* See if we've already created this construction vtable group. */
10241 : 260817 : id = mangle_ctor_vtbl_for_type (t, binfo);
10242 : 260817 : if (get_global_binding (id))
10243 : 0 : return;
10244 : :
10245 : 260817 : gcc_assert (!SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), t));
10246 : : /* Build a version of VTBL (with the wrong type) for use in
10247 : : constructing the addresses of secondary vtables in the
10248 : : construction vtable group. */
10249 : 260817 : vtbl = build_vtable (t, id, ptr_type_node);
10250 : :
10251 : : /* Don't export construction vtables from shared libraries. Even on
10252 : : targets that don't support hidden visibility, this tells
10253 : : can_refer_decl_in_current_unit_p not to assume that it's safe to
10254 : : access from a different compilation unit (bz 54314). */
10255 : 260817 : DECL_VISIBILITY (vtbl) = VISIBILITY_HIDDEN;
10256 : 260817 : DECL_VISIBILITY_SPECIFIED (vtbl) = true;
10257 : :
10258 : 260817 : v = NULL;
10259 : 260817 : accumulate_vtbl_inits (binfo, TYPE_BINFO (TREE_TYPE (binfo)),
10260 : : binfo, vtbl, t, &v);
10261 : :
10262 : : /* Add the vtables for each of our virtual bases using the vbase in T
10263 : : binfo. */
10264 : 260817 : for (vbase = TYPE_BINFO (BINFO_TYPE (binfo));
10265 : 1265097 : vbase;
10266 : 1004280 : vbase = TREE_CHAIN (vbase))
10267 : : {
10268 : 1004280 : tree b;
10269 : :
10270 : 1004280 : if (!BINFO_VIRTUAL_P (vbase))
10271 : 601149 : continue;
10272 : 403131 : b = copied_binfo (vbase, binfo);
10273 : :
10274 : 403131 : accumulate_vtbl_inits (b, vbase, binfo, vtbl, t, &v);
10275 : : }
10276 : :
10277 : : /* Figure out the type of the construction vtable. */
10278 : 260817 : type = build_array_of_n_type (vtable_entry_type, v->length ());
10279 : 260817 : layout_type (type);
10280 : 260817 : TREE_TYPE (vtbl) = type;
10281 : 260817 : DECL_SIZE (vtbl) = DECL_SIZE_UNIT (vtbl) = NULL_TREE;
10282 : 260817 : layout_decl (vtbl, 0);
10283 : :
10284 : : /* Initialize the construction vtable. */
10285 : 260817 : CLASSTYPE_VTABLES (t) = chainon (CLASSTYPE_VTABLES (t), vtbl);
10286 : 260817 : initialize_artificial_var (vtbl, v);
10287 : 260817 : dump_vtable (t, binfo, vtbl);
10288 : : }
10289 : :
10290 : : /* Add the vtbl initializers for BINFO (and its bases other than
10291 : : non-virtual primaries) to the list of INITS. BINFO is in the
10292 : : hierarchy dominated by T. RTTI_BINFO is the binfo within T of
10293 : : the constructor the vtbl inits should be accumulated for. (If this
10294 : : is the complete object vtbl then RTTI_BINFO will be TYPE_BINFO (T).)
10295 : : ORIG_BINFO is the binfo for this object within BINFO_TYPE (RTTI_BINFO).
10296 : : BINFO is the active base equivalent of ORIG_BINFO in the inheritance
10297 : : graph of T. Both BINFO and ORIG_BINFO will have the same BINFO_TYPE,
10298 : : but are not necessarily the same in terms of layout. */
10299 : :
10300 : : static void
10301 : 43661756 : accumulate_vtbl_inits (tree binfo,
10302 : : tree orig_binfo,
10303 : : tree rtti_binfo,
10304 : : tree vtbl,
10305 : : tree t,
10306 : : vec<constructor_elt, va_gc> **inits)
10307 : : {
10308 : 43661756 : int i;
10309 : 43661756 : tree base_binfo;
10310 : 43661756 : int ctor_vtbl_p = !SAME_BINFO_TYPE_P (BINFO_TYPE (rtti_binfo), t);
10311 : :
10312 : 43661756 : gcc_assert (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), BINFO_TYPE (orig_binfo)));
10313 : :
10314 : : /* If it doesn't have a vptr, we don't do anything. */
10315 : 43661756 : if (!TYPE_CONTAINS_VPTR_P (BINFO_TYPE (binfo)))
10316 : : return;
10317 : :
10318 : : /* If we're building a construction vtable, we're not interested in
10319 : : subobjects that don't require construction vtables. */
10320 : 5439595 : if (ctor_vtbl_p
10321 : 982764 : && !CLASSTYPE_VBASECLASSES (BINFO_TYPE (binfo))
10322 : 5930174 : && !binfo_via_virtual (orig_binfo, BINFO_TYPE (rtti_binfo)))
10323 : : return;
10324 : :
10325 : : /* Build the initializers for the BINFO-in-T vtable. */
10326 : 5439394 : dfs_accumulate_vtbl_inits (binfo, orig_binfo, rtti_binfo, vtbl, t, inits);
10327 : :
10328 : : /* Walk the BINFO and its bases. We walk in preorder so that as we
10329 : : initialize each vtable we can figure out at what offset the
10330 : : secondary vtable lies from the primary vtable. We can't use
10331 : : dfs_walk here because we need to iterate through bases of BINFO
10332 : : and RTTI_BINFO simultaneously. */
10333 : 15158445 : for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
10334 : : {
10335 : : /* Skip virtual bases. */
10336 : 4279657 : if (BINFO_VIRTUAL_P (base_binfo))
10337 : 793304 : continue;
10338 : 6972706 : accumulate_vtbl_inits (base_binfo,
10339 : 3486353 : BINFO_BASE_BINFO (orig_binfo, i),
10340 : : rtti_binfo, vtbl, t,
10341 : : inits);
10342 : : }
10343 : : }
10344 : :
10345 : : /* Called from accumulate_vtbl_inits. Adds the initializers for the
10346 : : BINFO vtable to L. */
10347 : :
10348 : : static void
10349 : 5439394 : dfs_accumulate_vtbl_inits (tree binfo,
10350 : : tree orig_binfo,
10351 : : tree rtti_binfo,
10352 : : tree orig_vtbl,
10353 : : tree t,
10354 : : vec<constructor_elt, va_gc> **l)
10355 : : {
10356 : 5439394 : tree vtbl = NULL_TREE;
10357 : 5439394 : int ctor_vtbl_p = !SAME_BINFO_TYPE_P (BINFO_TYPE (rtti_binfo), t);
10358 : 5439394 : int n_inits;
10359 : :
10360 : 5439394 : if (ctor_vtbl_p
10361 : 6421957 : && BINFO_VIRTUAL_P (orig_binfo) && BINFO_PRIMARY_P (orig_binfo))
10362 : : {
10363 : : /* In the hierarchy of BINFO_TYPE (RTTI_BINFO), this is a
10364 : : primary virtual base. If it is not the same primary in
10365 : : the hierarchy of T, we'll need to generate a ctor vtable
10366 : : for it, to place at its location in T. If it is the same
10367 : : primary, we still need a VTT entry for the vtable, but it
10368 : : should point to the ctor vtable for the base it is a
10369 : : primary for within the sub-hierarchy of RTTI_BINFO.
10370 : :
10371 : : There are three possible cases:
10372 : :
10373 : : 1) We are in the same place.
10374 : : 2) We are a primary base within a lost primary virtual base of
10375 : : RTTI_BINFO.
10376 : : 3) We are primary to something not a base of RTTI_BINFO. */
10377 : :
10378 : : tree b;
10379 : : tree last = NULL_TREE;
10380 : :
10381 : : /* First, look through the bases we are primary to for RTTI_BINFO
10382 : : or a virtual base. */
10383 : : b = binfo;
10384 : 73384 : while (BINFO_PRIMARY_P (b))
10385 : : {
10386 : 72968 : b = BINFO_INHERITANCE_CHAIN (b);
10387 : 72968 : last = b;
10388 : 144869 : if (BINFO_VIRTUAL_P (b) || b == rtti_binfo)
10389 : 71901 : goto found;
10390 : : }
10391 : : /* If we run out of primary links, keep looking down our
10392 : : inheritance chain; we might be an indirect primary. */
10393 : 865 : for (b = last; b; b = BINFO_INHERITANCE_CHAIN (b))
10394 : 461 : if (BINFO_VIRTUAL_P (b) || b == rtti_binfo)
10395 : : break;
10396 : 404 : found:
10397 : :
10398 : : /* If we found RTTI_BINFO, this is case 1. If we found a virtual
10399 : : base B and it is a base of RTTI_BINFO, this is case 2. In
10400 : : either case, we share our vtable with LAST, i.e. the
10401 : : derived-most base within B of which we are a primary. */
10402 : 72317 : if (b == rtti_binfo
10403 : 72317 : || (b && binfo_for_vbase (BINFO_TYPE (b), BINFO_TYPE (rtti_binfo))))
10404 : : /* Just set our BINFO_VTABLE to point to LAST, as we may not have
10405 : : set LAST's BINFO_VTABLE yet. We'll extract the actual vptr in
10406 : : binfo_ctor_vtable after everything's been set up. */
10407 : : vtbl = last;
10408 : :
10409 : : /* Otherwise, this is case 3 and we get our own. */
10410 : : }
10411 : 5367077 : else if (!BINFO_NEW_VTABLE_MARKED (orig_binfo))
10412 : : return;
10413 : :
10414 : 2640875 : n_inits = vec_safe_length (*l);
10415 : :
10416 : 2640875 : if (!vtbl)
10417 : : {
10418 : 2569482 : tree index;
10419 : 2569482 : int non_fn_entries;
10420 : :
10421 : : /* Add the initializer for this vtable. */
10422 : 2569482 : build_vtbl_initializer (binfo, orig_binfo, t, rtti_binfo,
10423 : : &non_fn_entries, l);
10424 : :
10425 : : /* Figure out the position to which the VPTR should point. */
10426 : 2569482 : vtbl = build1 (ADDR_EXPR, vtbl_ptr_type_node, orig_vtbl);
10427 : 2569482 : index = size_binop (MULT_EXPR,
10428 : : TYPE_SIZE_UNIT (vtable_entry_type),
10429 : : size_int (non_fn_entries + n_inits));
10430 : 2569482 : vtbl = fold_build_pointer_plus (vtbl, index);
10431 : : }
10432 : :
10433 : 2640875 : if (ctor_vtbl_p)
10434 : : /* For a construction vtable, we can't overwrite BINFO_VTABLE.
10435 : : So, we make a TREE_LIST. Later, dfs_fixup_binfo_vtbls will
10436 : : straighten this out. */
10437 : 689806 : BINFO_VTABLE (binfo) = tree_cons (rtti_binfo, vtbl, BINFO_VTABLE (binfo));
10438 : 1951069 : else if (BINFO_PRIMARY_P (binfo) && BINFO_VIRTUAL_P (binfo))
10439 : : /* Throw away any unneeded intializers. */
10440 : 8026 : (*l)->truncate (n_inits);
10441 : : else
10442 : : /* For an ordinary vtable, set BINFO_VTABLE. */
10443 : 1943043 : BINFO_VTABLE (binfo) = vtbl;
10444 : : }
10445 : :
10446 : : static GTY(()) tree abort_fndecl_addr;
10447 : : static GTY(()) tree dvirt_fn;
10448 : :
10449 : : /* Construct the initializer for BINFO's virtual function table. BINFO
10450 : : is part of the hierarchy dominated by T. If we're building a
10451 : : construction vtable, the ORIG_BINFO is the binfo we should use to
10452 : : find the actual function pointers to put in the vtable - but they
10453 : : can be overridden on the path to most-derived in the graph that
10454 : : ORIG_BINFO belongs. Otherwise,
10455 : : ORIG_BINFO should be the same as BINFO. The RTTI_BINFO is the
10456 : : BINFO that should be indicated by the RTTI information in the
10457 : : vtable; it will be a base class of T, rather than T itself, if we
10458 : : are building a construction vtable.
10459 : :
10460 : : The value returned is a TREE_LIST suitable for wrapping in a
10461 : : CONSTRUCTOR to use as the DECL_INITIAL for a vtable. If
10462 : : NON_FN_ENTRIES_P is not NULL, *NON_FN_ENTRIES_P is set to the
10463 : : number of non-function entries in the vtable.
10464 : :
10465 : : It might seem that this function should never be called with a
10466 : : BINFO for which BINFO_PRIMARY_P holds, the vtable for such a
10467 : : base is always subsumed by a derived class vtable. However, when
10468 : : we are building construction vtables, we do build vtables for
10469 : : primary bases; we need these while the primary base is being
10470 : : constructed. */
10471 : :
10472 : : static void
10473 : 2569482 : build_vtbl_initializer (tree binfo,
10474 : : tree orig_binfo,
10475 : : tree t,
10476 : : tree rtti_binfo,
10477 : : int* non_fn_entries_p,
10478 : : vec<constructor_elt, va_gc> **inits)
10479 : : {
10480 : 2569482 : tree v;
10481 : 2569482 : vtbl_init_data vid;
10482 : 2569482 : unsigned ix, jx;
10483 : 2569482 : tree vbinfo;
10484 : 2569482 : vec<tree, va_gc> *vbases;
10485 : 2569482 : constructor_elt *e;
10486 : :
10487 : : /* Initialize VID. */
10488 : 2569482 : memset (&vid, 0, sizeof (vid));
10489 : 2569482 : vid.binfo = binfo;
10490 : 2569482 : vid.derived = t;
10491 : 2569482 : vid.rtti_binfo = rtti_binfo;
10492 : 2569482 : vid.primary_vtbl_p = SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), t);
10493 : 2569482 : vid.ctor_vtbl_p = !SAME_BINFO_TYPE_P (BINFO_TYPE (rtti_binfo), t);
10494 : 2569482 : vid.generate_vcall_entries = true;
10495 : : /* The first vbase or vcall offset is at index -3 in the vtable. */
10496 : 2569482 : vid.index = ssize_int(-3 * TARGET_VTABLE_DATA_ENTRY_DISTANCE);
10497 : :
10498 : : /* Add entries to the vtable for RTTI. */
10499 : 2569482 : build_rtti_vtbl_entries (binfo, &vid);
10500 : :
10501 : : /* Create an array for keeping track of the functions we've
10502 : : processed. When we see multiple functions with the same
10503 : : signature, we share the vcall offsets. */
10504 : 2569482 : vec_alloc (vid.fns, 32);
10505 : : /* Add the vcall and vbase offset entries. */
10506 : 2569482 : build_vcall_and_vbase_vtbl_entries (binfo, &vid);
10507 : :
10508 : : /* Clear BINFO_VTABLE_PATH_MARKED; it's set by
10509 : : build_vbase_offset_vtbl_entries. */
10510 : 2569482 : for (vbases = CLASSTYPE_VBASECLASSES (t), ix = 0;
10511 : 8988348 : vec_safe_iterate (vbases, ix, &vbinfo); ix++)
10512 : 6418866 : BINFO_VTABLE_PATH_MARKED (vbinfo) = 0;
10513 : :
10514 : : /* If the target requires padding between data entries, add that now. */
10515 : 2569482 : if (TARGET_VTABLE_DATA_ENTRY_DISTANCE > 1)
10516 : : {
10517 : : int n_entries = vec_safe_length (vid.inits);
10518 : :
10519 : : vec_safe_grow (vid.inits, TARGET_VTABLE_DATA_ENTRY_DISTANCE * n_entries,
10520 : : true);
10521 : :
10522 : : /* Move data entries into their new positions and add padding
10523 : : after the new positions. Iterate backwards so we don't
10524 : : overwrite entries that we would need to process later. */
10525 : : for (ix = n_entries - 1;
10526 : : vid.inits->iterate (ix, &e);
10527 : : ix--)
10528 : : {
10529 : : int j;
10530 : : int new_position = (TARGET_VTABLE_DATA_ENTRY_DISTANCE * ix
10531 : : + (TARGET_VTABLE_DATA_ENTRY_DISTANCE - 1));
10532 : :
10533 : : (*vid.inits)[new_position] = *e;
10534 : :
10535 : : for (j = 1; j < TARGET_VTABLE_DATA_ENTRY_DISTANCE; ++j)
10536 : : {
10537 : : constructor_elt *f = &(*vid.inits)[new_position - j];
10538 : : f->index = NULL_TREE;
10539 : : f->value = build1 (NOP_EXPR, vtable_entry_type,
10540 : : null_pointer_node);
10541 : : }
10542 : : }
10543 : : }
10544 : :
10545 : 2569482 : if (non_fn_entries_p)
10546 : 5138964 : *non_fn_entries_p = vec_safe_length (vid.inits);
10547 : :
10548 : : /* The initializers for virtual functions were built up in reverse
10549 : : order. Straighten them out and add them to the running list in one
10550 : : step. */
10551 : 2569482 : jx = vec_safe_length (*inits);
10552 : 2569482 : vec_safe_grow (*inits, jx + vid.inits->length (), true);
10553 : :
10554 : 2569482 : for (ix = vid.inits->length () - 1;
10555 : 10372216 : vid.inits->iterate (ix, &e);
10556 : 7802734 : ix--, jx++)
10557 : 7802734 : (**inits)[jx] = *e;
10558 : :
10559 : : /* Go through all the ordinary virtual functions, building up
10560 : : initializers. */
10561 : 13848129 : for (v = BINFO_VIRTUALS (orig_binfo); v; v = TREE_CHAIN (v))
10562 : : {
10563 : 11278647 : tree delta;
10564 : 11278647 : tree vcall_index;
10565 : 11278647 : tree fn, fn_original;
10566 : 11278647 : tree init = NULL_TREE;
10567 : :
10568 : 11278647 : fn = BV_FN (v);
10569 : 11278647 : fn_original = fn;
10570 : 11278647 : if (DECL_THUNK_P (fn))
10571 : : {
10572 : 476 : if (!DECL_NAME (fn))
10573 : 214 : finish_thunk (fn);
10574 : 476 : if (THUNK_ALIAS (fn))
10575 : : {
10576 : 0 : fn = THUNK_ALIAS (fn);
10577 : 0 : BV_FN (v) = fn;
10578 : : }
10579 : 476 : fn_original = THUNK_TARGET (fn);
10580 : : }
10581 : :
10582 : : /* If the only definition of this function signature along our
10583 : : primary base chain is from a lost primary, this vtable slot will
10584 : : never be used, so just zero it out. This is important to avoid
10585 : : requiring extra thunks which cannot be generated with the function.
10586 : :
10587 : : We first check this in update_vtable_entry_for_fn, so we handle
10588 : : restored primary bases properly; we also need to do it here so we
10589 : : zero out unused slots in ctor vtables, rather than filling them
10590 : : with erroneous values (though harmless, apart from relocation
10591 : : costs). */
10592 : 11278647 : if (BV_LOST_PRIMARY (v))
10593 : 745 : init = size_zero_node;
10594 : :
10595 : 745 : if (! init)
10596 : : {
10597 : : /* Pull the offset for `this', and the function to call, out of
10598 : : the list. */
10599 : 11277902 : delta = BV_DELTA (v);
10600 : 11277902 : vcall_index = BV_VCALL_INDEX (v);
10601 : :
10602 : 11277902 : gcc_assert (TREE_CODE (delta) == INTEGER_CST);
10603 : 11277902 : gcc_assert (TREE_CODE (fn) == FUNCTION_DECL);
10604 : :
10605 : : /* You can't call an abstract virtual function; it's abstract.
10606 : : So, we replace these functions with __pure_virtual. */
10607 : 11277902 : if (DECL_PURE_VIRTUAL_P (fn_original))
10608 : : {
10609 : 650335 : fn = abort_fndecl;
10610 : 650335 : if (!TARGET_VTABLE_USES_DESCRIPTORS)
10611 : : {
10612 : 650335 : if (abort_fndecl_addr == NULL)
10613 : 13636 : abort_fndecl_addr
10614 : 13636 : = fold_convert (vfunc_ptr_type_node,
10615 : : build_fold_addr_expr (fn));
10616 : 650335 : init = abort_fndecl_addr;
10617 : : }
10618 : : }
10619 : : /* Likewise for deleted virtuals. */
10620 : 10627567 : else if (DECL_DELETED_FN (fn_original))
10621 : : {
10622 : 45 : if (!dvirt_fn)
10623 : : {
10624 : 22 : tree name = get_identifier ("__cxa_deleted_virtual");
10625 : 22 : dvirt_fn = get_global_binding (name);
10626 : 22 : if (!dvirt_fn)
10627 : 22 : dvirt_fn = push_library_fn
10628 : 22 : (name,
10629 : : build_function_type_list (void_type_node, NULL_TREE),
10630 : : NULL_TREE, ECF_NORETURN | ECF_COLD);
10631 : : }
10632 : 45 : fn = dvirt_fn;
10633 : 45 : if (!TARGET_VTABLE_USES_DESCRIPTORS)
10634 : 45 : init = fold_convert (vfunc_ptr_type_node,
10635 : : build_fold_addr_expr (fn));
10636 : : }
10637 : : else
10638 : : {
10639 : 10627522 : if (!integer_zerop (delta) || vcall_index)
10640 : : {
10641 : 1068906 : fn = make_thunk (fn, /*this_adjusting=*/1,
10642 : : delta, vcall_index);
10643 : 1068906 : if (!DECL_NAME (fn))
10644 : 497439 : finish_thunk (fn);
10645 : : }
10646 : : /* Take the address of the function, considering it to be of an
10647 : : appropriate generic type. */
10648 : 10627522 : if (!TARGET_VTABLE_USES_DESCRIPTORS)
10649 : 10627522 : init = fold_convert (vfunc_ptr_type_node,
10650 : : build_fold_addr_expr (fn));
10651 : : /* Don't refer to a virtual destructor from a constructor
10652 : : vtable or a vtable for an abstract class, since destroying
10653 : : an object under construction is undefined behavior and we
10654 : : don't want it to be considered a candidate for speculative
10655 : : devirtualization. But do create the thunk for ABI
10656 : : compliance. */
10657 : 10627522 : if (DECL_DESTRUCTOR_P (fn_original)
10658 : 10627522 : && (CLASSTYPE_PURE_VIRTUALS (DECL_CONTEXT (fn_original))
10659 : 4496144 : || orig_binfo != binfo))
10660 : 1347578 : init = size_zero_node;
10661 : : }
10662 : : }
10663 : :
10664 : : /* And add it to the chain of initializers. */
10665 : 11278647 : if (TARGET_VTABLE_USES_DESCRIPTORS)
10666 : : {
10667 : : int i;
10668 : : if (init == size_zero_node)
10669 : : for (i = 0; i < TARGET_VTABLE_USES_DESCRIPTORS; ++i)
10670 : : CONSTRUCTOR_APPEND_ELT (*inits, NULL_TREE, init);
10671 : : else
10672 : : for (i = 0; i < TARGET_VTABLE_USES_DESCRIPTORS; ++i)
10673 : : {
10674 : : tree fdesc = build2 (FDESC_EXPR, vfunc_ptr_type_node,
10675 : : fn, build_int_cst (NULL_TREE, i));
10676 : : TREE_CONSTANT (fdesc) = 1;
10677 : :
10678 : : CONSTRUCTOR_APPEND_ELT (*inits, NULL_TREE, fdesc);
10679 : : }
10680 : : }
10681 : : else
10682 : 11278647 : CONSTRUCTOR_APPEND_ELT (*inits, NULL_TREE, init);
10683 : : }
10684 : 2569482 : }
10685 : :
10686 : : /* Adds to vid->inits the initializers for the vbase and vcall
10687 : : offsets in BINFO, which is in the hierarchy dominated by T. */
10688 : :
10689 : : static void
10690 : 5451760 : build_vcall_and_vbase_vtbl_entries (tree binfo, vtbl_init_data* vid)
10691 : : {
10692 : 5451760 : tree b;
10693 : :
10694 : : /* If this is a derived class, we must first create entries
10695 : : corresponding to the primary base class. */
10696 : 5451760 : b = get_primary_binfo (binfo);
10697 : 5451760 : if (b)
10698 : 2882278 : build_vcall_and_vbase_vtbl_entries (b, vid);
10699 : :
10700 : : /* Add the vbase entries for this base. */
10701 : 5451760 : build_vbase_offset_vtbl_entries (binfo, vid);
10702 : : /* Add the vcall entries for this base. */
10703 : 5451760 : build_vcall_offset_vtbl_entries (binfo, vid);
10704 : 5451760 : }
10705 : :
10706 : : /* Returns the initializers for the vbase offset entries in the vtable
10707 : : for BINFO (which is part of the class hierarchy dominated by T), in
10708 : : reverse order. VBASE_OFFSET_INDEX gives the vtable index
10709 : : where the next vbase offset will go. */
10710 : :
10711 : : static void
10712 : 5451760 : build_vbase_offset_vtbl_entries (tree binfo, vtbl_init_data* vid)
10713 : : {
10714 : 5451760 : tree vbase;
10715 : 5451760 : tree t;
10716 : 5451760 : tree non_primary_binfo;
10717 : :
10718 : : /* If there are no virtual baseclasses, then there is nothing to
10719 : : do. */
10720 : 5451760 : if (!CLASSTYPE_VBASECLASSES (BINFO_TYPE (binfo)))
10721 : : return;
10722 : :
10723 : 951473 : t = vid->derived;
10724 : :
10725 : : /* We might be a primary base class. Go up the inheritance hierarchy
10726 : : until we find the most derived class of which we are a primary base:
10727 : : it is the offset of that which we need to use. */
10728 : 951473 : non_primary_binfo = binfo;
10729 : 1491931 : while (BINFO_INHERITANCE_CHAIN (non_primary_binfo))
10730 : : {
10731 : 894609 : tree b;
10732 : :
10733 : : /* If we have reached a virtual base, then it must be a primary
10734 : : base (possibly multi-level) of vid->binfo, or we wouldn't
10735 : : have called build_vcall_and_vbase_vtbl_entries for it. But it
10736 : : might be a lost primary, so just skip down to vid->binfo. */
10737 : 894609 : if (BINFO_VIRTUAL_P (non_primary_binfo))
10738 : : {
10739 : 185005 : non_primary_binfo = vid->binfo;
10740 : 185005 : break;
10741 : : }
10742 : :
10743 : 709604 : b = BINFO_INHERITANCE_CHAIN (non_primary_binfo);
10744 : 709604 : if (get_primary_binfo (b) != non_primary_binfo)
10745 : : break;
10746 : : non_primary_binfo = b;
10747 : : }
10748 : :
10749 : : /* Go through the virtual bases, adding the offsets. */
10750 : 951473 : for (vbase = TYPE_BINFO (BINFO_TYPE (binfo));
10751 : 5717120 : vbase;
10752 : 4765647 : vbase = TREE_CHAIN (vbase))
10753 : : {
10754 : 4765647 : tree b;
10755 : 4765647 : tree delta;
10756 : :
10757 : 4765647 : if (!BINFO_VIRTUAL_P (vbase))
10758 : 2231411 : continue;
10759 : :
10760 : : /* Find the instance of this virtual base in the complete
10761 : : object. */
10762 : 2534236 : b = copied_binfo (vbase, binfo);
10763 : :
10764 : : /* If we've already got an offset for this virtual base, we
10765 : : don't need another one. */
10766 : 2534236 : if (BINFO_VTABLE_PATH_MARKED (b))
10767 : 315618 : continue;
10768 : 2218618 : BINFO_VTABLE_PATH_MARKED (b) = 1;
10769 : :
10770 : : /* Figure out where we can find this vbase offset. */
10771 : 2218618 : delta = size_binop (MULT_EXPR,
10772 : : vid->index,
10773 : : fold_convert (ssizetype,
10774 : : TYPE_SIZE_UNIT (vtable_entry_type)));
10775 : 2218618 : if (vid->primary_vtbl_p)
10776 : 203672 : BINFO_VPTR_FIELD (b) = delta;
10777 : :
10778 : 2218618 : if (binfo != TYPE_BINFO (t))
10779 : : /* The vbase offset had better be the same. */
10780 : 2159616 : gcc_assert (tree_int_cst_equal (delta, BINFO_VPTR_FIELD (vbase)));
10781 : :
10782 : : /* The next vbase will come at a more negative offset. */
10783 : 2218618 : vid->index = size_binop (MINUS_EXPR, vid->index,
10784 : : ssize_int (TARGET_VTABLE_DATA_ENTRY_DISTANCE));
10785 : :
10786 : : /* The initializer is the delta from BINFO to this virtual base.
10787 : : The vbase offsets go in reverse inheritance-graph order, and
10788 : : we are walking in inheritance graph order so these end up in
10789 : : the right order. */
10790 : 2218618 : delta = size_diffop_loc (input_location,
10791 : 2218618 : BINFO_OFFSET (b), BINFO_OFFSET (non_primary_binfo));
10792 : :
10793 : 6984265 : CONSTRUCTOR_APPEND_ELT (vid->inits, NULL_TREE,
10794 : : fold_build1_loc (input_location, NOP_EXPR,
10795 : : vtable_entry_type, delta));
10796 : : }
10797 : : }
10798 : :
10799 : : /* Adds the initializers for the vcall offset entries in the vtable
10800 : : for BINFO (which is part of the class hierarchy dominated by VID->DERIVED)
10801 : : to VID->INITS. */
10802 : :
10803 : : static void
10804 : 5451760 : build_vcall_offset_vtbl_entries (tree binfo, vtbl_init_data* vid)
10805 : : {
10806 : : /* We only need these entries if this base is a virtual base. We
10807 : : compute the indices -- but do not add to the vtable -- when
10808 : : building the main vtable for a class. */
10809 : 5451760 : if (binfo == TYPE_BINFO (vid->derived)
10810 : 9217189 : || (BINFO_VIRTUAL_P (binfo)
10811 : : /* If BINFO is RTTI_BINFO, then (since BINFO does not
10812 : : correspond to VID->DERIVED), we are building a primary
10813 : : construction virtual table. Since this is a primary
10814 : : virtual table, we do not need the vcall offsets for
10815 : : BINFO. */
10816 : 613804 : && binfo != vid->rtti_binfo))
10817 : : {
10818 : : /* We need a vcall offset for each of the virtual functions in this
10819 : : vtable. For example:
10820 : :
10821 : : class A { virtual void f (); };
10822 : : class B1 : virtual public A { virtual void f (); };
10823 : : class B2 : virtual public A { virtual void f (); };
10824 : : class C: public B1, public B2 { virtual void f (); };
10825 : :
10826 : : A C object has a primary base of B1, which has a primary base of A. A
10827 : : C also has a secondary base of B2, which no longer has a primary base
10828 : : of A. So the B2-in-C construction vtable needs a secondary vtable for
10829 : : A, which will adjust the A* to a B2* to call f. We have no way of
10830 : : knowing what (or even whether) this offset will be when we define B2,
10831 : : so we store this "vcall offset" in the A sub-vtable and look it up in
10832 : : a "virtual thunk" for B2::f.
10833 : :
10834 : : We need entries for all the functions in our primary vtable and
10835 : : in our non-virtual bases' secondary vtables. */
10836 : 2286781 : vid->vbase = binfo;
10837 : : /* If we are just computing the vcall indices -- but do not need
10838 : : the actual entries -- not that. */
10839 : 2286781 : if (!BINFO_VIRTUAL_P (binfo))
10840 : 1686331 : vid->generate_vcall_entries = false;
10841 : : /* Now, walk through the non-virtual bases, adding vcall offsets. */
10842 : 2286781 : add_vcall_offset_vtbl_entries_r (binfo, vid);
10843 : : }
10844 : 5451760 : }
10845 : :
10846 : : /* Build vcall offsets, starting with those for BINFO. */
10847 : :
10848 : : static void
10849 : 6203272 : add_vcall_offset_vtbl_entries_r (tree binfo, vtbl_init_data* vid)
10850 : : {
10851 : 6203272 : int i;
10852 : 6203272 : tree primary_binfo;
10853 : 6203272 : tree base_binfo;
10854 : :
10855 : : /* Don't walk into virtual bases -- except, of course, for the
10856 : : virtual base for which we are building vcall offsets. Any
10857 : : primary virtual base will have already had its offsets generated
10858 : : through the recursion in build_vcall_and_vbase_vtbl_entries. */
10859 : 6203272 : if (BINFO_VIRTUAL_P (binfo) && vid->vbase != binfo)
10860 : 6203272 : return;
10861 : :
10862 : : /* If BINFO has a primary base, process it first. */
10863 : 5711420 : primary_binfo = get_primary_binfo (binfo);
10864 : 5711420 : if (primary_binfo)
10865 : 2831972 : add_vcall_offset_vtbl_entries_r (primary_binfo, vid);
10866 : :
10867 : : /* Add BINFO itself to the list. */
10868 : 5711420 : add_vcall_offset_vtbl_entries_1 (binfo, vid);
10869 : :
10870 : : /* Scan the non-primary bases of BINFO. */
10871 : 15339167 : for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
10872 : 3916327 : if (base_binfo != primary_binfo)
10873 : 1084519 : add_vcall_offset_vtbl_entries_r (base_binfo, vid);
10874 : : }
10875 : :
10876 : : /* Called from build_vcall_offset_vtbl_entries_r. */
10877 : :
10878 : : static void
10879 : 5711420 : add_vcall_offset_vtbl_entries_1 (tree binfo, vtbl_init_data* vid)
10880 : : {
10881 : : /* Make entries for the rest of the virtuals. */
10882 : 5711420 : tree orig_fn;
10883 : :
10884 : : /* The ABI requires that the methods be processed in declaration
10885 : : order. */
10886 : 5711420 : for (orig_fn = TYPE_FIELDS (BINFO_TYPE (binfo));
10887 : 199444763 : orig_fn;
10888 : 193733343 : orig_fn = DECL_CHAIN (orig_fn))
10889 : 193733343 : if (TREE_CODE (orig_fn) == FUNCTION_DECL && DECL_VINDEX (orig_fn))
10890 : 24539205 : add_vcall_offset (orig_fn, binfo, vid);
10891 : 5711420 : }
10892 : :
10893 : : /* Add a vcall offset entry for ORIG_FN to the vtable. */
10894 : :
10895 : : static void
10896 : 24539205 : add_vcall_offset (tree orig_fn, tree binfo, vtbl_init_data *vid)
10897 : : {
10898 : 24539205 : size_t i;
10899 : 24539205 : tree vcall_offset;
10900 : 24539205 : tree derived_entry;
10901 : :
10902 : : /* If there is already an entry for a function with the same
10903 : : signature as FN, then we do not need a second vcall offset.
10904 : : Check the list of functions already present in the derived
10905 : : class vtable. */
10906 : 67146000 : FOR_EACH_VEC_SAFE_ELT (vid->fns, i, derived_entry)
10907 : : {
10908 : 58609374 : if (same_signature_p (derived_entry, orig_fn)
10909 : : /* We only use one vcall offset for virtual destructors,
10910 : : even though there are two virtual table entries. */
10911 : 110781483 : || (DECL_DESTRUCTOR_P (derived_entry)
10912 : 38741348 : && DECL_DESTRUCTOR_P (orig_fn)))
10913 : 24539205 : return;
10914 : : }
10915 : :
10916 : : /* If we are building these vcall offsets as part of building
10917 : : the vtable for the most derived class, remember the vcall
10918 : : offset. */
10919 : 8536626 : if (vid->binfo == TYPE_BINFO (vid->derived))
10920 : : {
10921 : 8093572 : tree_pair_s elt = {orig_fn, vid->index};
10922 : 8093572 : vec_safe_push (CLASSTYPE_VCALL_INDICES (vid->derived), elt);
10923 : : }
10924 : :
10925 : : /* The next vcall offset will be found at a more negative
10926 : : offset. */
10927 : 8536626 : vid->index = size_binop (MINUS_EXPR, vid->index,
10928 : : ssize_int (TARGET_VTABLE_DATA_ENTRY_DISTANCE));
10929 : :
10930 : : /* Keep track of this function. */
10931 : 8536626 : vec_safe_push (vid->fns, orig_fn);
10932 : :
10933 : 8536626 : if (vid->generate_vcall_entries)
10934 : : {
10935 : 445152 : tree base;
10936 : 445152 : tree fn;
10937 : :
10938 : : /* Find the overriding function. */
10939 : 445152 : fn = find_final_overrider (vid->rtti_binfo, binfo, orig_fn);
10940 : 445152 : if (fn == error_mark_node)
10941 : 27 : vcall_offset = build_zero_cst (vtable_entry_type);
10942 : : else
10943 : : {
10944 : 445125 : base = TREE_VALUE (fn);
10945 : :
10946 : : /* The vbase we're working on is a primary base of
10947 : : vid->binfo. But it might be a lost primary, so its
10948 : : BINFO_OFFSET might be wrong, so we just use the
10949 : : BINFO_OFFSET from vid->binfo. */
10950 : 1780500 : vcall_offset = size_diffop_loc (input_location,
10951 : 445125 : BINFO_OFFSET (base),
10952 : 445125 : BINFO_OFFSET (vid->binfo));
10953 : 445125 : vcall_offset = fold_build1_loc (input_location,
10954 : : NOP_EXPR, vtable_entry_type,
10955 : : vcall_offset);
10956 : : }
10957 : : /* Add the initializer to the vtable. */
10958 : 445152 : CONSTRUCTOR_APPEND_ELT (vid->inits, NULL_TREE, vcall_offset);
10959 : : }
10960 : : }
10961 : :
10962 : : /* Return vtbl initializers for the RTTI entries corresponding to the
10963 : : BINFO's vtable. The RTTI entries should indicate the object given
10964 : : by VID->rtti_binfo. */
10965 : :
10966 : : static void
10967 : 2569482 : build_rtti_vtbl_entries (tree binfo, vtbl_init_data* vid)
10968 : : {
10969 : 2569482 : tree b;
10970 : 2569482 : tree t;
10971 : 2569482 : tree offset;
10972 : 2569482 : tree decl;
10973 : 2569482 : tree init;
10974 : :
10975 : 2569482 : t = BINFO_TYPE (vid->rtti_binfo);
10976 : :
10977 : : /* To find the complete object, we will first convert to our most
10978 : : primary base, and then add the offset in the vtbl to that value. */
10979 : 2569482 : b = most_primary_binfo (binfo);
10980 : 2569482 : offset = size_diffop_loc (input_location,
10981 : 2569482 : BINFO_OFFSET (vid->rtti_binfo), BINFO_OFFSET (b));
10982 : :
10983 : : /* The second entry is the address of the typeinfo object. */
10984 : 2569482 : if (flag_rtti)
10985 : 2566583 : decl = build_address (get_tinfo_decl (t));
10986 : : else
10987 : 2899 : decl = integer_zero_node;
10988 : :
10989 : : /* Convert the declaration to a type that can be stored in the
10990 : : vtable. */
10991 : 2569482 : init = build_nop (vfunc_ptr_type_node, decl);
10992 : 2569482 : CONSTRUCTOR_APPEND_ELT (vid->inits, NULL_TREE, init);
10993 : :
10994 : : /* Add the offset-to-top entry. It comes earlier in the vtable than
10995 : : the typeinfo entry. Convert the offset to look like a
10996 : : function pointer, so that we can put it in the vtable. */
10997 : 2569482 : init = build_nop (vfunc_ptr_type_node, offset);
10998 : 2569482 : CONSTRUCTOR_APPEND_ELT (vid->inits, NULL_TREE, init);
10999 : 2569482 : }
11000 : :
11001 : : /* TRUE iff TYPE is uniquely derived from PARENT. Ignores
11002 : : accessibility. */
11003 : :
11004 : : bool
11005 : 2857 : uniquely_derived_from_p (tree parent, tree type)
11006 : : {
11007 : 2857 : tree base = lookup_base (type, parent, ba_unique, NULL, tf_none);
11008 : 2857 : return base && base != error_mark_node;
11009 : : }
11010 : :
11011 : : /* TRUE iff TYPE is publicly & uniquely derived from PARENT. */
11012 : :
11013 : : bool
11014 : 3738 : publicly_uniquely_derived_p (tree parent, tree type)
11015 : : {
11016 : 3738 : tree base = lookup_base (type, parent, ba_ignore_scope | ba_check,
11017 : : NULL, tf_none);
11018 : 3738 : return base && base != error_mark_node;
11019 : : }
11020 : :
11021 : : /* TRUE iff TYPE is publicly & virtually derived from PARENT. */
11022 : :
11023 : : bool
11024 : 195646 : publicly_virtually_derived_p (tree parent, tree type)
11025 : : {
11026 : 195646 : tree base = lookup_base (type, parent,
11027 : : ba_ignore_scope | ba_check | ba_require_virtual,
11028 : : NULL, tf_none);
11029 : 195646 : return base && base != error_mark_node;
11030 : : }
11031 : :
11032 : : /* CTX1 and CTX2 are declaration contexts. Return the innermost common
11033 : : class between them, if any. */
11034 : :
11035 : : tree
11036 : 2712 : common_enclosing_class (tree ctx1, tree ctx2)
11037 : : {
11038 : 2712 : if (!TYPE_P (ctx1) || !TYPE_P (ctx2))
11039 : : return NULL_TREE;
11040 : 2712 : gcc_assert (ctx1 == TYPE_MAIN_VARIANT (ctx1)
11041 : : && ctx2 == TYPE_MAIN_VARIANT (ctx2));
11042 : 2712 : if (ctx1 == ctx2)
11043 : : return ctx1;
11044 : 267 : for (tree t = ctx1; TYPE_P (t); t = TYPE_CONTEXT (t))
11045 : 195 : TYPE_MARKED_P (t) = true;
11046 : 129 : tree found = NULL_TREE;
11047 : 129 : for (tree t = ctx2; TYPE_P (t); t = TYPE_CONTEXT (t))
11048 : 129 : if (TYPE_MARKED_P (t))
11049 : : {
11050 : : found = t;
11051 : : break;
11052 : : }
11053 : 267 : for (tree t = ctx1; TYPE_P (t); t = TYPE_CONTEXT (t))
11054 : 195 : TYPE_MARKED_P (t) = false;
11055 : : return found;
11056 : : }
11057 : :
11058 : : #include "gt-cp-class.h"
|