1 | /* Functions related to building -*- C++ -*- classes and their related objects. |
2 | Copyright (C) 1987-2024 Free Software Foundation, Inc. |
3 | Contributed by Michael Tiemann (tiemann@cygnus.com) |
4 | |
5 | This file is part of GCC. |
6 | |
7 | GCC is free software; you can redistribute it and/or modify |
8 | it under the terms of the GNU General Public License as published by |
9 | the Free Software Foundation; either version 3, or (at your option) |
10 | any later version. |
11 | |
12 | GCC is distributed in the hope that it will be useful, |
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15 | GNU General Public License for more details. |
16 | |
17 | You should have received a copy of the GNU General Public License |
18 | along with GCC; see the file COPYING3. If not see |
19 | <http://www.gnu.org/licenses/>. */ |
20 | |
21 | |
22 | /* High-level class interface. */ |
23 | |
24 | #include "config.h" |
25 | #include "system.h" |
26 | #include "coretypes.h" |
27 | #include "target.h" |
28 | #include "cp-tree.h" |
29 | #include "stringpool.h" |
30 | #include "cgraph.h" |
31 | #include "stor-layout.h" |
32 | #include "attribs.h" |
33 | #include "flags.h" |
34 | #include "toplev.h" |
35 | #include "convert.h" |
36 | #include "dumpfile.h" |
37 | #include "gimplify.h" |
38 | #include "intl.h" |
39 | #include "asan.h" |
40 | |
41 | /* Id for dumping the class hierarchy. */ |
42 | int class_dump_id; |
43 | |
44 | /* The number of nested classes being processed. If we are not in the |
45 | scope of any class, this is zero. */ |
46 | |
47 | int current_class_depth; |
48 | |
49 | /* In order to deal with nested classes, we keep a stack of classes. |
50 | The topmost entry is the innermost class, and is the entry at index |
51 | CURRENT_CLASS_DEPTH */ |
52 | |
53 | typedef struct class_stack_node { |
54 | /* The name of the class. */ |
55 | tree name; |
56 | |
57 | /* The _TYPE node for the class. */ |
58 | tree type; |
59 | |
60 | /* The access specifier pending for new declarations in the scope of |
61 | this class. */ |
62 | tree access; |
63 | |
64 | /* If were defining TYPE, the names used in this class. */ |
65 | splay_tree names_used; |
66 | |
67 | /* Nonzero if this class is no longer open, because of a call to |
68 | push_to_top_level. */ |
69 | size_t hidden; |
70 | }* class_stack_node_t; |
71 | |
72 | struct vtbl_init_data |
73 | { |
74 | /* The base for which we're building initializers. */ |
75 | tree binfo; |
76 | /* The type of the most-derived type. */ |
77 | tree derived; |
78 | /* The binfo for the dynamic type. This will be TYPE_BINFO (derived), |
79 | unless ctor_vtbl_p is true. */ |
80 | tree rtti_binfo; |
81 | /* The negative-index vtable initializers built up so far. These |
82 | are in order from least negative index to most negative index. */ |
83 | vec<constructor_elt, va_gc> *inits; |
84 | /* The binfo for the virtual base for which we're building |
85 | vcall offset initializers. */ |
86 | tree vbase; |
87 | /* The functions in vbase for which we have already provided vcall |
88 | offsets. */ |
89 | vec<tree, va_gc> *fns; |
90 | /* The vtable index of the next vcall or vbase offset. */ |
91 | tree index; |
92 | /* Nonzero if we are building the initializer for the primary |
93 | vtable. */ |
94 | int primary_vtbl_p; |
95 | /* Nonzero if we are building the initializer for a construction |
96 | vtable. */ |
97 | int ctor_vtbl_p; |
98 | /* True when adding vcall offset entries to the vtable. False when |
99 | merely computing the indices. */ |
100 | bool generate_vcall_entries; |
101 | }; |
102 | |
103 | /* The type of a function passed to walk_subobject_offsets. */ |
104 | typedef int (*subobject_offset_fn) (tree, tree, splay_tree); |
105 | |
106 | /* The stack itself. This is a dynamically resized array. The |
107 | number of elements allocated is CURRENT_CLASS_STACK_SIZE. */ |
108 | static int current_class_stack_size; |
109 | static class_stack_node_t current_class_stack; |
110 | |
111 | /* The size of the largest empty class seen in this translation unit. */ |
112 | static GTY (()) tree sizeof_biggest_empty_class; |
113 | |
114 | static tree get_vfield_name (tree); |
115 | static void finish_struct_anon (tree); |
116 | static tree get_vtable_name (tree); |
117 | static void get_basefndecls (tree, tree, vec<tree> *); |
118 | static int build_primary_vtable (tree, tree); |
119 | static int build_secondary_vtable (tree); |
120 | static void finish_vtbls (tree); |
121 | static void modify_vtable_entry (tree, tree, tree, tree, tree *); |
122 | static void finish_struct_bits (tree); |
123 | static int alter_access (tree, tree, tree); |
124 | static void handle_using_decl (tree, tree); |
125 | static tree dfs_modify_vtables (tree, void *); |
126 | static tree modify_all_vtables (tree, tree); |
127 | static void determine_primary_bases (tree); |
128 | static void maybe_warn_about_overly_private_class (tree); |
129 | static void add_implicitly_declared_members (tree, tree*, int, int); |
130 | static tree fixed_type_or_null (tree, int *, int *); |
131 | static tree build_simple_base_path (tree expr, tree binfo); |
132 | static void build_vtbl_initializer (tree, tree, tree, tree, int *, |
133 | vec<constructor_elt, va_gc> **); |
134 | static bool check_bitfield_decl (tree); |
135 | static bool check_field_decl (tree, tree, int *, int *); |
136 | static void check_field_decls (tree, tree *, int *, int *); |
137 | static void build_base_fields (record_layout_info, splay_tree, tree *); |
138 | static void check_methods (tree); |
139 | static bool accessible_nvdtor_p (tree); |
140 | |
141 | /* Used by find_flexarrays and related functions. */ |
142 | struct flexmems_t; |
143 | static void diagnose_flexarrays (tree, const flexmems_t *); |
144 | static void find_flexarrays (tree, flexmems_t *, bool = false, |
145 | tree = NULL_TREE, tree = NULL_TREE); |
146 | static void check_flexarrays (tree, flexmems_t * = NULL, bool = false); |
147 | static void check_bases (tree, int *, int *); |
148 | static void check_bases_and_members (tree); |
149 | static tree create_vtable_ptr (tree, tree *); |
150 | static void include_empty_classes (record_layout_info); |
151 | static void layout_class_type (tree, tree *); |
152 | static void propagate_binfo_offsets (tree, tree); |
153 | static void layout_virtual_bases (record_layout_info, splay_tree); |
154 | static void build_vbase_offset_vtbl_entries (tree, vtbl_init_data *); |
155 | static void add_vcall_offset_vtbl_entries_r (tree, vtbl_init_data *); |
156 | static void add_vcall_offset_vtbl_entries_1 (tree, vtbl_init_data *); |
157 | static void build_vcall_offset_vtbl_entries (tree, vtbl_init_data *); |
158 | static void add_vcall_offset (tree, tree, vtbl_init_data *); |
159 | static void layout_vtable_decl (tree, int); |
160 | static tree dfs_find_final_overrider_pre (tree, void *); |
161 | static tree dfs_find_final_overrider_post (tree, void *); |
162 | static tree find_final_overrider (tree, tree, tree); |
163 | static int make_new_vtable (tree, tree); |
164 | static tree get_primary_binfo (tree); |
165 | static int maybe_indent_hierarchy (FILE *, int, int); |
166 | static tree dump_class_hierarchy_r (FILE *, dump_flags_t, tree, tree, int); |
167 | static void dump_class_hierarchy (tree); |
168 | static void dump_class_hierarchy_1 (FILE *, dump_flags_t, tree); |
169 | static void dump_array (FILE *, tree); |
170 | static void dump_vtable (tree, tree, tree); |
171 | static void dump_vtt (tree, tree); |
172 | static void dump_thunk (FILE *, int, tree); |
173 | static tree build_vtable (tree, tree, tree); |
174 | static void initialize_vtable (tree, vec<constructor_elt, va_gc> *); |
175 | static void layout_nonempty_base_or_field (record_layout_info, |
176 | tree, tree, splay_tree); |
177 | static void accumulate_vtbl_inits (tree, tree, tree, tree, tree, |
178 | vec<constructor_elt, va_gc> **); |
179 | static void dfs_accumulate_vtbl_inits (tree, tree, tree, tree, tree, |
180 | vec<constructor_elt, va_gc> **); |
181 | static void build_rtti_vtbl_entries (tree, vtbl_init_data *); |
182 | static void build_vcall_and_vbase_vtbl_entries (tree, vtbl_init_data *); |
183 | static void clone_constructors_and_destructors (tree); |
184 | static void update_vtable_entry_for_fn (tree, tree, tree, tree *, unsigned); |
185 | static void build_ctor_vtbl_group (tree, tree); |
186 | static void build_vtt (tree); |
187 | static tree binfo_ctor_vtable (tree); |
188 | static void build_vtt_inits (tree, tree, vec<constructor_elt, va_gc> **, |
189 | tree *); |
190 | static tree dfs_build_secondary_vptr_vtt_inits (tree, void *); |
191 | static tree dfs_fixup_binfo_vtbls (tree, void *); |
192 | static int record_subobject_offset (tree, tree, splay_tree); |
193 | static int check_subobject_offset (tree, tree, splay_tree); |
194 | static int walk_subobject_offsets (tree, subobject_offset_fn, |
195 | tree, splay_tree, tree, int); |
196 | static int layout_conflict_p (tree, tree, splay_tree, int); |
197 | static int splay_tree_compare_integer_csts (splay_tree_key k1, |
198 | splay_tree_key k2); |
199 | static void maybe_warn_about_inaccessible_bases (tree); |
200 | static bool type_requires_array_cookie (tree); |
201 | static bool base_derived_from (tree, tree); |
202 | static int empty_base_at_nonzero_offset_p (tree, tree, splay_tree); |
203 | static tree end_of_base (tree); |
204 | static tree get_vcall_index (tree, tree); |
205 | static bool type_maybe_constexpr_default_constructor (tree); |
206 | static bool type_maybe_constexpr_destructor (tree); |
207 | static bool field_poverlapping_p (tree); |
208 | static void propagate_class_warmth_attribute (tree); |
209 | |
210 | /* Set CURRENT_ACCESS_SPECIFIER based on the protection of DECL. */ |
211 | |
212 | void |
213 | set_current_access_from_decl (tree decl) |
214 | { |
215 | if (TREE_PRIVATE (decl)) |
216 | current_access_specifier = access_private_node; |
217 | else if (TREE_PROTECTED (decl)) |
218 | current_access_specifier = access_protected_node; |
219 | else |
220 | current_access_specifier = access_public_node; |
221 | } |
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 | build_if_in_charge (tree true_stmt, tree false_stmt) |
229 | { |
230 | gcc_assert (DECL_HAS_IN_CHARGE_PARM_P (current_function_decl)); |
231 | tree cmp = build2 (NE_EXPR, boolean_type_node, |
232 | current_in_charge_parm, integer_zero_node); |
233 | tree type = unlowered_expr_type (true_stmt); |
234 | if (VOID_TYPE_P (type)) |
235 | type = unlowered_expr_type (false_stmt); |
236 | tree cond = build3 (COND_EXPR, type, |
237 | cmp, true_stmt, false_stmt); |
238 | 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 | build_base_path (enum tree_code code, |
253 | tree expr, |
254 | tree binfo, |
255 | int nonnull, |
256 | tsubst_flags_t complain) |
257 | { |
258 | tree v_binfo = NULL_TREE; |
259 | tree d_binfo = NULL_TREE; |
260 | tree probe; |
261 | tree offset; |
262 | tree target_type; |
263 | tree null_test = NULL; |
264 | tree ptr_target_type; |
265 | int fixed_type_p; |
266 | int want_pointer = TYPE_PTR_P (TREE_TYPE (expr)); |
267 | bool has_empty = false; |
268 | bool virtual_access; |
269 | bool rvalue = false; |
270 | |
271 | if (expr == error_mark_node || binfo == error_mark_node || !binfo) |
272 | return error_mark_node; |
273 | |
274 | for (probe = binfo; probe; probe = BINFO_INHERITANCE_CHAIN (probe)) |
275 | { |
276 | d_binfo = probe; |
277 | if (is_empty_class (BINFO_TYPE (probe))) |
278 | has_empty = true; |
279 | if (!v_binfo && BINFO_VIRTUAL_P (probe)) |
280 | v_binfo = probe; |
281 | } |
282 | |
283 | probe = TYPE_MAIN_VARIANT (TREE_TYPE (expr)); |
284 | if (want_pointer) |
285 | probe = TYPE_MAIN_VARIANT (TREE_TYPE (probe)); |
286 | if (dependent_type_p (probe)) |
287 | if (tree open = currently_open_class (probe)) |
288 | probe = open; |
289 | |
290 | if (code == PLUS_EXPR |
291 | && !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 | if (complain & tf_error) |
299 | { |
300 | tree base = lookup_base (probe, BINFO_TYPE (d_binfo), |
301 | ba_unique, NULL, complain); |
302 | gcc_assert (base == error_mark_node || !base); |
303 | } |
304 | return error_mark_node; |
305 | } |
306 | |
307 | gcc_assert ((code == MINUS_EXPR |
308 | && SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), probe)) |
309 | || code == PLUS_EXPR); |
310 | |
311 | if (binfo == d_binfo) |
312 | /* Nothing to do. */ |
313 | return expr; |
314 | |
315 | if (code == MINUS_EXPR && v_binfo) |
316 | { |
317 | if (complain & tf_error) |
318 | { |
319 | if (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), BINFO_TYPE (v_binfo))) |
320 | { |
321 | if (want_pointer) |
322 | error ("cannot convert from pointer to base class %qT to " |
323 | "pointer to derived class %qT because the base is " |
324 | "virtual" , BINFO_TYPE (binfo), BINFO_TYPE (d_binfo)); |
325 | else |
326 | error ("cannot convert from base class %qT to derived " |
327 | "class %qT because the base is virtual" , |
328 | BINFO_TYPE (binfo), BINFO_TYPE (d_binfo)); |
329 | } |
330 | else |
331 | { |
332 | if (want_pointer) |
333 | error ("cannot convert from pointer to base class %qT to " |
334 | "pointer to derived class %qT via virtual base %qT" , |
335 | BINFO_TYPE (binfo), BINFO_TYPE (d_binfo), |
336 | BINFO_TYPE (v_binfo)); |
337 | else |
338 | error ("cannot convert from base class %qT to derived " |
339 | "class %qT via virtual base %qT" , BINFO_TYPE (binfo), |
340 | BINFO_TYPE (d_binfo), BINFO_TYPE (v_binfo)); |
341 | } |
342 | } |
343 | return error_mark_node; |
344 | } |
345 | |
346 | bool uneval = (cp_unevaluated_operand != 0 |
347 | || processing_template_decl |
348 | || in_template_context); |
349 | |
350 | /* For a non-pointer simple base reference, express it as a COMPONENT_REF |
351 | without taking its address (and so causing lambda capture, 91933). */ |
352 | if (code == PLUS_EXPR && !v_binfo && !want_pointer && !has_empty && !uneval) |
353 | return build_simple_base_path (expr, binfo); |
354 | |
355 | if (!want_pointer) |
356 | { |
357 | rvalue = !lvalue_p (expr); |
358 | /* This must happen before the call to save_expr. */ |
359 | expr = cp_build_addr_expr (expr, complain); |
360 | } |
361 | else |
362 | expr = mark_rvalue_use (expr); |
363 | |
364 | offset = BINFO_OFFSET (binfo); |
365 | fixed_type_p = resolves_to_fixed_type_p (expr, &nonnull); |
366 | target_type = code == PLUS_EXPR ? BINFO_TYPE (binfo) : BINFO_TYPE (d_binfo); |
367 | /* TARGET_TYPE has been extracted from BINFO, and, is therefore always |
368 | cv-unqualified. Extract the cv-qualifiers from EXPR so that the |
369 | expression returned matches the input. */ |
370 | target_type = cp_build_qualified_type |
371 | (target_type, cp_type_quals (TREE_TYPE (TREE_TYPE (expr)))); |
372 | ptr_target_type = build_pointer_type (target_type); |
373 | |
374 | /* Do we need to look in the vtable for the real offset? */ |
375 | virtual_access = (v_binfo && fixed_type_p <= 0); |
376 | |
377 | /* Don't bother with the calculations inside sizeof; they'll ICE if the |
378 | source type is incomplete and the pointer value doesn't matter. In a |
379 | template (even in instantiate_non_dependent_expr), we don't have vtables |
380 | set up properly yet, and the value doesn't matter there either; we're |
381 | just interested in the result of overload resolution. */ |
382 | if (uneval) |
383 | { |
384 | expr = build_nop (ptr_target_type, expr); |
385 | goto indout; |
386 | } |
387 | |
388 | if (!COMPLETE_TYPE_P (probe)) |
389 | { |
390 | if (complain & tf_error) |
391 | error ("cannot convert from %qT to base class %qT because %qT is " |
392 | "incomplete" , BINFO_TYPE (d_binfo), BINFO_TYPE (binfo), |
393 | BINFO_TYPE (d_binfo)); |
394 | return error_mark_node; |
395 | } |
396 | |
397 | /* If we're in an NSDMI, we don't have the full constructor context yet |
398 | that we need for converting to a virtual base, so just build a stub |
399 | CONVERT_EXPR and expand it later in bot_replace. */ |
400 | if (virtual_access && fixed_type_p < 0 |
401 | && current_scope () != current_function_decl) |
402 | { |
403 | expr = build1 (CONVERT_EXPR, ptr_target_type, expr); |
404 | CONVERT_EXPR_VBASE_PATH (expr) = true; |
405 | goto indout; |
406 | } |
407 | |
408 | /* Do we need to check for a null pointer? */ |
409 | if (want_pointer && !nonnull) |
410 | { |
411 | /* If we know the conversion will not actually change the value |
412 | of EXPR, then we can avoid testing the expression for NULL. |
413 | We have to avoid generating a COMPONENT_REF for a base class |
414 | field, because other parts of the compiler know that such |
415 | expressions are always non-NULL. */ |
416 | if (!virtual_access && integer_zerop (offset)) |
417 | return build_nop (ptr_target_type, expr); |
418 | null_test = error_mark_node; |
419 | } |
420 | |
421 | /* Protect against multiple evaluation if necessary. */ |
422 | if (TREE_SIDE_EFFECTS (expr) && (null_test || virtual_access)) |
423 | expr = save_expr (expr); |
424 | |
425 | /* Store EXPR and build the real null test just before returning. */ |
426 | if (null_test) |
427 | null_test = expr; |
428 | |
429 | /* If this is a simple base reference, express it as a COMPONENT_REF. */ |
430 | if (code == PLUS_EXPR && !virtual_access |
431 | /* We don't build base fields for empty bases, and they aren't very |
432 | interesting to the optimizers anyway. */ |
433 | && !has_empty) |
434 | { |
435 | expr = cp_build_fold_indirect_ref (expr); |
436 | expr = build_simple_base_path (expr, binfo); |
437 | if (rvalue && lvalue_p (expr)) |
438 | expr = move (expr); |
439 | if (want_pointer) |
440 | expr = build_address (expr); |
441 | target_type = TREE_TYPE (expr); |
442 | goto out; |
443 | } |
444 | |
445 | if (virtual_access) |
446 | { |
447 | /* Going via virtual base V_BINFO. We need the static offset |
448 | from V_BINFO to BINFO, and the dynamic offset from D_BINFO to |
449 | V_BINFO. That offset is an entry in D_BINFO's vtable. */ |
450 | tree v_offset; |
451 | |
452 | if (fixed_type_p < 0 && in_base_initializer) |
453 | { |
454 | /* In a base member initializer, we cannot rely on the |
455 | vtable being set up. We have to indirect via the |
456 | vtt_parm. */ |
457 | tree t; |
458 | |
459 | t = TREE_TYPE (TYPE_VFIELD (current_class_type)); |
460 | t = build_pointer_type (t); |
461 | v_offset = fold_convert (t, current_vtt_parm); |
462 | v_offset = cp_build_fold_indirect_ref (v_offset); |
463 | } |
464 | else |
465 | { |
466 | tree t = expr; |
467 | if (sanitize_flags_p (flag: SANITIZE_VPTR) |
468 | && fixed_type_p == 0) |
469 | { |
470 | t = cp_ubsan_maybe_instrument_cast_to_vbase (input_location, |
471 | probe, expr); |
472 | if (t == NULL_TREE) |
473 | t = expr; |
474 | } |
475 | v_offset = build_vfield_ref (cp_build_fold_indirect_ref (t), |
476 | TREE_TYPE (TREE_TYPE (expr))); |
477 | } |
478 | |
479 | if (v_offset == error_mark_node) |
480 | return error_mark_node; |
481 | |
482 | v_offset = fold_build_pointer_plus (v_offset, BINFO_VPTR_FIELD (v_binfo)); |
483 | v_offset = build1 (NOP_EXPR, |
484 | build_pointer_type (ptrdiff_type_node), |
485 | v_offset); |
486 | v_offset = cp_build_fold_indirect_ref (v_offset); |
487 | TREE_CONSTANT (v_offset) = 1; |
488 | |
489 | offset = convert_to_integer (ptrdiff_type_node, |
490 | size_diffop_loc (input_location, offset, |
491 | BINFO_OFFSET (v_binfo))); |
492 | |
493 | if (!integer_zerop (offset)) |
494 | v_offset = build2 (code, ptrdiff_type_node, v_offset, offset); |
495 | |
496 | if (fixed_type_p < 0) |
497 | /* Negative fixed_type_p means this is a constructor or destructor; |
498 | virtual base layout is fixed in in-charge [cd]tors, but not in |
499 | base [cd]tors. */ |
500 | offset = build_if_in_charge |
501 | (true_stmt: convert_to_integer (ptrdiff_type_node, BINFO_OFFSET (binfo)), |
502 | false_stmt: v_offset); |
503 | else |
504 | offset = v_offset; |
505 | } |
506 | |
507 | if (want_pointer) |
508 | target_type = ptr_target_type; |
509 | |
510 | if (!integer_zerop (offset)) |
511 | { |
512 | offset = fold_convert (sizetype, offset); |
513 | if (code == MINUS_EXPR) |
514 | offset = fold_build1_loc (input_location, NEGATE_EXPR, sizetype, offset); |
515 | expr = fold_build_pointer_plus (expr, offset); |
516 | } |
517 | else |
518 | null_test = NULL; |
519 | |
520 | expr = build1 (NOP_EXPR, ptr_target_type, expr); |
521 | |
522 | indout: |
523 | if (!want_pointer) |
524 | { |
525 | expr = cp_build_fold_indirect_ref (expr); |
526 | if (rvalue) |
527 | expr = move (expr); |
528 | } |
529 | |
530 | out: |
531 | if (null_test) |
532 | /* Wrap EXPR in a null test. */ |
533 | expr = build_if_nonnull (null_test, expr, complain); |
534 | |
535 | return expr; |
536 | } |
537 | |
538 | /* Subroutine of build_base_path; EXPR and BINFO are as in that function. |
539 | Perform a derived-to-base conversion by recursively building up a |
540 | sequence of COMPONENT_REFs to the appropriate base fields. */ |
541 | |
542 | static tree |
543 | build_simple_base_path (tree expr, tree binfo) |
544 | { |
545 | tree type = BINFO_TYPE (binfo); |
546 | tree d_binfo = BINFO_INHERITANCE_CHAIN (binfo); |
547 | tree field; |
548 | |
549 | if (d_binfo == NULL_TREE) |
550 | { |
551 | tree temp; |
552 | |
553 | gcc_assert (TYPE_MAIN_VARIANT (TREE_TYPE (expr)) == type); |
554 | |
555 | /* Transform `(a, b).x' into `(*(a, &b)).x', `(a ? b : c).x' |
556 | into `(*(a ? &b : &c)).x', and so on. A COND_EXPR is only |
557 | an lvalue in the front end; only _DECLs and _REFs are lvalues |
558 | in the back end. */ |
559 | temp = unary_complex_lvalue (ADDR_EXPR, expr); |
560 | if (temp) |
561 | expr = cp_build_fold_indirect_ref (temp); |
562 | |
563 | return expr; |
564 | } |
565 | |
566 | /* Recurse. */ |
567 | expr = build_simple_base_path (expr, binfo: d_binfo); |
568 | |
569 | for (field = TYPE_FIELDS (BINFO_TYPE (d_binfo)); |
570 | field; field = DECL_CHAIN (field)) |
571 | /* Is this the base field created by build_base_field? */ |
572 | if (TREE_CODE (field) == FIELD_DECL |
573 | && DECL_FIELD_IS_BASE (field) |
574 | && TREE_TYPE (field) == type |
575 | /* If we're looking for a field in the most-derived class, |
576 | also check the field offset; we can have two base fields |
577 | of the same type if one is an indirect virtual base and one |
578 | is a direct non-virtual base. */ |
579 | && (BINFO_INHERITANCE_CHAIN (d_binfo) |
580 | || tree_int_cst_equal (byte_position (field), |
581 | BINFO_OFFSET (binfo)))) |
582 | { |
583 | /* We don't use build_class_member_access_expr here, as that |
584 | has unnecessary checks, and more importantly results in |
585 | recursive calls to dfs_walk_once. */ |
586 | int type_quals = cp_type_quals (TREE_TYPE (expr)); |
587 | |
588 | expr = build3 (COMPONENT_REF, |
589 | cp_build_qualified_type (type, type_quals), |
590 | expr, field, NULL_TREE); |
591 | /* Mark the expression const or volatile, as appropriate. |
592 | Even though we've dealt with the type above, we still have |
593 | to mark the expression itself. */ |
594 | if (type_quals & TYPE_QUAL_CONST) |
595 | TREE_READONLY (expr) = 1; |
596 | if (type_quals & TYPE_QUAL_VOLATILE) |
597 | TREE_THIS_VOLATILE (expr) = 1; |
598 | |
599 | return expr; |
600 | } |
601 | |
602 | /* Didn't find the base field?!? */ |
603 | gcc_unreachable (); |
604 | } |
605 | |
606 | /* Convert OBJECT to the base TYPE. OBJECT is an expression whose |
607 | type is a class type or a pointer to a class type. In the former |
608 | case, TYPE is also a class type; in the latter it is another |
609 | pointer type. If CHECK_ACCESS is true, an error message is emitted |
610 | if TYPE is inaccessible. If OBJECT has pointer type, the value is |
611 | assumed to be non-NULL. */ |
612 | |
613 | tree |
614 | convert_to_base (tree object, tree type, bool check_access, bool nonnull, |
615 | tsubst_flags_t complain) |
616 | { |
617 | tree binfo; |
618 | tree object_type; |
619 | |
620 | if (TYPE_PTR_P (TREE_TYPE (object))) |
621 | { |
622 | object_type = TREE_TYPE (TREE_TYPE (object)); |
623 | type = TREE_TYPE (type); |
624 | } |
625 | else |
626 | object_type = TREE_TYPE (object); |
627 | |
628 | binfo = lookup_base (object_type, type, check_access ? ba_check : ba_unique, |
629 | NULL, complain); |
630 | if (!binfo || binfo == error_mark_node) |
631 | return error_mark_node; |
632 | |
633 | return build_base_path (code: PLUS_EXPR, expr: object, binfo, nonnull, complain); |
634 | } |
635 | |
636 | /* EXPR is an expression with unqualified class type. BASE is a base |
637 | binfo of that class type. Returns EXPR, converted to the BASE |
638 | type. This function assumes that EXPR is the most derived class; |
639 | therefore virtual bases can be found at their static offsets. */ |
640 | |
641 | tree |
642 | convert_to_base_statically (tree expr, tree base) |
643 | { |
644 | tree expr_type; |
645 | |
646 | expr_type = TREE_TYPE (expr); |
647 | if (!SAME_BINFO_TYPE_P (BINFO_TYPE (base), expr_type)) |
648 | { |
649 | /* If this is a non-empty base, use a COMPONENT_REF. */ |
650 | if (!is_empty_class (BINFO_TYPE (base))) |
651 | return build_simple_base_path (expr, binfo: base); |
652 | |
653 | /* We use fold_build2 and fold_convert below to simplify the trees |
654 | provided to the optimizers. It is not safe to call these functions |
655 | when processing a template because they do not handle C++-specific |
656 | trees. */ |
657 | gcc_assert (!processing_template_decl); |
658 | expr = cp_build_addr_expr (expr, tf_warning_or_error); |
659 | if (!integer_zerop (BINFO_OFFSET (base))) |
660 | expr = fold_build_pointer_plus_loc (loc: input_location, |
661 | ptr: expr, BINFO_OFFSET (base)); |
662 | expr = fold_convert (build_pointer_type (BINFO_TYPE (base)), expr); |
663 | expr = build_fold_indirect_ref_loc (input_location, expr); |
664 | } |
665 | |
666 | return expr; |
667 | } |
668 | |
669 | /* True IFF EXPR is a reference to an empty base class "subobject", as built in |
670 | convert_to_base_statically. We look for the result of the fold_convert |
671 | call, a NOP_EXPR from one pointer type to another, where the target is an |
672 | empty base of the original type. */ |
673 | |
674 | bool |
675 | is_empty_base_ref (tree expr) |
676 | { |
677 | if (INDIRECT_REF_P (expr)) |
678 | expr = TREE_OPERAND (expr, 0); |
679 | if (TREE_CODE (expr) != NOP_EXPR) |
680 | return false; |
681 | tree type = TREE_TYPE (expr); |
682 | if (!POINTER_TYPE_P (type)) |
683 | return false; |
684 | type = TREE_TYPE (type); |
685 | if (!is_empty_class (type)) |
686 | return false; |
687 | STRIP_NOPS (expr); |
688 | tree fromtype = TREE_TYPE (expr); |
689 | if (!POINTER_TYPE_P (fromtype)) |
690 | return false; |
691 | fromtype = TREE_TYPE (fromtype); |
692 | return (CLASS_TYPE_P (fromtype) |
693 | && !same_type_ignoring_top_level_qualifiers_p (fromtype, type) |
694 | && DERIVED_FROM_P (type, fromtype)); |
695 | } |
696 | |
697 | tree |
698 | build_vfield_ref (tree datum, tree type) |
699 | { |
700 | tree vfield, vcontext; |
701 | |
702 | if (datum == error_mark_node |
703 | /* Can happen in case of duplicate base types (c++/59082). */ |
704 | || !TYPE_VFIELD (type)) |
705 | return error_mark_node; |
706 | |
707 | /* First, convert to the requested type. */ |
708 | if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (datum), type)) |
709 | datum = convert_to_base (object: datum, type, /*check_access=*/false, |
710 | /*nonnull=*/true, complain: tf_warning_or_error); |
711 | |
712 | /* Second, the requested type may not be the owner of its own vptr. |
713 | If not, convert to the base class that owns it. We cannot use |
714 | convert_to_base here, because VCONTEXT may appear more than once |
715 | in the inheritance hierarchy of TYPE, and thus direct conversion |
716 | between the types may be ambiguous. Following the path back up |
717 | one step at a time via primary bases avoids the problem. */ |
718 | vfield = TYPE_VFIELD (type); |
719 | vcontext = DECL_CONTEXT (vfield); |
720 | while (!same_type_ignoring_top_level_qualifiers_p (vcontext, type)) |
721 | { |
722 | datum = build_simple_base_path (expr: datum, CLASSTYPE_PRIMARY_BINFO (type)); |
723 | type = TREE_TYPE (datum); |
724 | } |
725 | |
726 | return build3 (COMPONENT_REF, TREE_TYPE (vfield), datum, vfield, NULL_TREE); |
727 | } |
728 | |
729 | /* Given an object INSTANCE, return an expression which yields the |
730 | vtable element corresponding to INDEX. There are many special |
731 | cases for INSTANCE which we take care of here, mainly to avoid |
732 | creating extra tree nodes when we don't have to. */ |
733 | |
734 | tree |
735 | build_vtbl_ref (tree instance, tree idx) |
736 | { |
737 | tree aref; |
738 | tree vtbl = NULL_TREE; |
739 | |
740 | /* Try to figure out what a reference refers to, and |
741 | access its virtual function table directly. */ |
742 | |
743 | int cdtorp = 0; |
744 | tree fixed_type = fixed_type_or_null (instance, NULL, &cdtorp); |
745 | |
746 | tree basetype = non_reference (TREE_TYPE (instance)); |
747 | |
748 | if (fixed_type && !cdtorp) |
749 | { |
750 | tree binfo = lookup_base (fixed_type, basetype, |
751 | ba_unique, NULL, tf_none); |
752 | if (binfo && binfo != error_mark_node) |
753 | vtbl = unshare_expr (BINFO_VTABLE (binfo)); |
754 | } |
755 | |
756 | if (!vtbl) |
757 | vtbl = build_vfield_ref (datum: instance, type: basetype); |
758 | |
759 | aref = build_array_ref (input_location, vtbl, idx); |
760 | TREE_CONSTANT (aref) |= TREE_CONSTANT (vtbl) && TREE_CONSTANT (idx); |
761 | |
762 | return aref; |
763 | } |
764 | |
765 | /* Given a stable object pointer INSTANCE_PTR, return an expression which |
766 | yields a function pointer corresponding to vtable element INDEX. */ |
767 | |
768 | tree |
769 | build_vfn_ref (tree instance_ptr, tree idx) |
770 | { |
771 | tree aref; |
772 | |
773 | aref = build_vtbl_ref (instance: cp_build_fold_indirect_ref (instance_ptr), idx); |
774 | |
775 | /* When using function descriptors, the address of the |
776 | vtable entry is treated as a function pointer. */ |
777 | if (TARGET_VTABLE_USES_DESCRIPTORS) |
778 | aref = build1 (NOP_EXPR, TREE_TYPE (aref), |
779 | cp_build_addr_expr (aref, tf_warning_or_error)); |
780 | |
781 | /* Remember this as a method reference, for later devirtualization. */ |
782 | aref = build3 (OBJ_TYPE_REF, TREE_TYPE (aref), aref, instance_ptr, |
783 | fold_convert (TREE_TYPE (instance_ptr), idx)); |
784 | |
785 | return aref; |
786 | } |
787 | |
788 | /* Return the name of the virtual function table (as an IDENTIFIER_NODE) |
789 | for the given TYPE. */ |
790 | |
791 | static tree |
792 | get_vtable_name (tree type) |
793 | { |
794 | return mangle_vtbl_for_type (type); |
795 | } |
796 | |
797 | /* DECL is an entity associated with TYPE, like a virtual table or an |
798 | implicitly generated constructor. Determine whether or not DECL |
799 | should have external or internal linkage at the object file |
800 | level. This routine does not deal with COMDAT linkage and other |
801 | similar complexities; it simply sets TREE_PUBLIC if it possible for |
802 | entities in other translation units to contain copies of DECL, in |
803 | the abstract. */ |
804 | |
805 | void |
806 | set_linkage_according_to_type (tree /*type*/, tree decl) |
807 | { |
808 | TREE_PUBLIC (decl) = 1; |
809 | determine_visibility (decl); |
810 | } |
811 | |
812 | /* Create a VAR_DECL for a primary or secondary vtable for CLASS_TYPE. |
813 | (For a secondary vtable for B-in-D, CLASS_TYPE should be D, not B.) |
814 | Use NAME for the name of the vtable, and VTABLE_TYPE for its type. */ |
815 | |
816 | static tree |
817 | build_vtable (tree class_type, tree name, tree vtable_type) |
818 | { |
819 | tree decl; |
820 | |
821 | decl = build_lang_decl (VAR_DECL, name, vtable_type); |
822 | /* vtable names are already mangled; give them their DECL_ASSEMBLER_NAME |
823 | now to avoid confusion in mangle_decl. */ |
824 | SET_DECL_ASSEMBLER_NAME (decl, name); |
825 | DECL_CONTEXT (decl) = class_type; |
826 | DECL_ARTIFICIAL (decl) = 1; |
827 | TREE_STATIC (decl) = 1; |
828 | TREE_READONLY (decl) = 1; |
829 | DECL_VIRTUAL_P (decl) = 1; |
830 | SET_DECL_ALIGN (decl, TARGET_VTABLE_ENTRY_ALIGN); |
831 | DECL_USER_ALIGN (decl) = true; |
832 | DECL_VTABLE_OR_VTT_P (decl) = 1; |
833 | set_linkage_according_to_type (class_type, decl); |
834 | /* The vtable has not been defined -- yet. */ |
835 | DECL_EXTERNAL (decl) = 1; |
836 | DECL_NOT_REALLY_EXTERN (decl) = 1; |
837 | |
838 | /* Mark the VAR_DECL node representing the vtable itself as a |
839 | "gratuitous" one, thereby forcing dwarfout.c to ignore it. It |
840 | is rather important that such things be ignored because any |
841 | effort to actually generate DWARF for them will run into |
842 | trouble when/if we encounter code like: |
843 | |
844 | #pragma interface |
845 | struct S { virtual void member (); }; |
846 | |
847 | because the artificial declaration of the vtable itself (as |
848 | manufactured by the g++ front end) will say that the vtable is |
849 | a static member of `S' but only *after* the debug output for |
850 | the definition of `S' has already been output. This causes |
851 | grief because the DWARF entry for the definition of the vtable |
852 | will try to refer back to an earlier *declaration* of the |
853 | vtable as a static member of `S' and there won't be one. We |
854 | might be able to arrange to have the "vtable static member" |
855 | attached to the member list for `S' before the debug info for |
856 | `S' get written (which would solve the problem) but that would |
857 | require more intrusive changes to the g++ front end. */ |
858 | DECL_IGNORED_P (decl) = 1; |
859 | |
860 | return decl; |
861 | } |
862 | |
863 | /* Get the VAR_DECL of the vtable for TYPE. TYPE need not be polymorphic, |
864 | or even complete. If this does not exist, create it. If COMPLETE is |
865 | nonzero, then complete the definition of it -- that will render it |
866 | impossible to actually build the vtable, but is useful to get at those |
867 | which are known to exist in the runtime. */ |
868 | |
869 | tree |
870 | get_vtable_decl (tree type, int complete) |
871 | { |
872 | tree decl; |
873 | |
874 | if (CLASSTYPE_VTABLES (type)) |
875 | return CLASSTYPE_VTABLES (type); |
876 | |
877 | decl = build_vtable (class_type: type, name: get_vtable_name (type), vtbl_type_node); |
878 | CLASSTYPE_VTABLES (type) = decl; |
879 | |
880 | if (complete) |
881 | { |
882 | DECL_EXTERNAL (decl) = 1; |
883 | cp_finish_decl (decl, NULL_TREE, false, NULL_TREE, 0); |
884 | } |
885 | |
886 | return decl; |
887 | } |
888 | |
889 | /* Build the primary virtual function table for TYPE. If BINFO is |
890 | non-NULL, build the vtable starting with the initial approximation |
891 | that it is the same as the one which is the head of the association |
892 | list. Returns a nonzero value if a new vtable is actually |
893 | created. */ |
894 | |
895 | static int |
896 | build_primary_vtable (tree binfo, tree type) |
897 | { |
898 | tree decl; |
899 | tree virtuals; |
900 | |
901 | decl = get_vtable_decl (type, /*complete=*/0); |
902 | |
903 | if (binfo) |
904 | { |
905 | if (BINFO_NEW_VTABLE_MARKED (binfo)) |
906 | /* We have already created a vtable for this base, so there's |
907 | no need to do it again. */ |
908 | return 0; |
909 | |
910 | virtuals = copy_list (BINFO_VIRTUALS (binfo)); |
911 | TREE_TYPE (decl) = TREE_TYPE (get_vtbl_decl_for_binfo (binfo)); |
912 | DECL_SIZE (decl) = TYPE_SIZE (TREE_TYPE (decl)); |
913 | DECL_SIZE_UNIT (decl) = TYPE_SIZE_UNIT (TREE_TYPE (decl)); |
914 | } |
915 | else |
916 | { |
917 | gcc_assert (TREE_TYPE (decl) == vtbl_type_node); |
918 | virtuals = NULL_TREE; |
919 | } |
920 | |
921 | /* Initialize the association list for this type, based |
922 | on our first approximation. */ |
923 | BINFO_VTABLE (TYPE_BINFO (type)) = decl; |
924 | BINFO_VIRTUALS (TYPE_BINFO (type)) = virtuals; |
925 | SET_BINFO_NEW_VTABLE_MARKED (TYPE_BINFO (type)); |
926 | return 1; |
927 | } |
928 | |
929 | /* Give BINFO a new virtual function table which is initialized |
930 | with a skeleton-copy of its original initialization. The only |
931 | entry that changes is the `delta' entry, so we can really |
932 | share a lot of structure. |
933 | |
934 | FOR_TYPE is the most derived type which caused this table to |
935 | be needed. |
936 | |
937 | Returns nonzero if we haven't met BINFO before. |
938 | |
939 | The order in which vtables are built (by calling this function) for |
940 | an object must remain the same, otherwise a binary incompatibility |
941 | can result. */ |
942 | |
943 | static int |
944 | build_secondary_vtable (tree binfo) |
945 | { |
946 | if (BINFO_NEW_VTABLE_MARKED (binfo)) |
947 | /* We already created a vtable for this base. There's no need to |
948 | do it again. */ |
949 | return 0; |
950 | |
951 | /* Remember that we've created a vtable for this BINFO, so that we |
952 | don't try to do so again. */ |
953 | SET_BINFO_NEW_VTABLE_MARKED (binfo); |
954 | |
955 | /* Make fresh virtual list, so we can smash it later. */ |
956 | BINFO_VIRTUALS (binfo) = copy_list (BINFO_VIRTUALS (binfo)); |
957 | |
958 | /* Secondary vtables are laid out as part of the same structure as |
959 | the primary vtable. */ |
960 | BINFO_VTABLE (binfo) = NULL_TREE; |
961 | return 1; |
962 | } |
963 | |
964 | /* Create a new vtable for BINFO which is the hierarchy dominated by |
965 | T. Return nonzero if we actually created a new vtable. */ |
966 | |
967 | static int |
968 | make_new_vtable (tree t, tree binfo) |
969 | { |
970 | if (binfo == TYPE_BINFO (t)) |
971 | /* In this case, it is *type*'s vtable we are modifying. We start |
972 | with the approximation that its vtable is that of the |
973 | immediate base class. */ |
974 | return build_primary_vtable (binfo, type: t); |
975 | else |
976 | /* This is our very own copy of `basetype' to play with. Later, |
977 | we will fill in all the virtual functions that override the |
978 | virtual functions in these base classes which are not defined |
979 | by the current type. */ |
980 | return build_secondary_vtable (binfo); |
981 | } |
982 | |
983 | /* Make *VIRTUALS, an entry on the BINFO_VIRTUALS list for BINFO |
984 | (which is in the hierarchy dominated by T) list FNDECL as its |
985 | BV_FN. DELTA is the required constant adjustment from the `this' |
986 | pointer where the vtable entry appears to the `this' required when |
987 | the function is actually called. */ |
988 | |
989 | static void |
990 | modify_vtable_entry (tree t, |
991 | tree binfo, |
992 | tree fndecl, |
993 | tree delta, |
994 | tree *virtuals) |
995 | { |
996 | tree v; |
997 | |
998 | v = *virtuals; |
999 | |
1000 | if (fndecl != BV_FN (v) |
1001 | || !tree_int_cst_equal (delta, BV_DELTA (v))) |
1002 | { |
1003 | /* We need a new vtable for BINFO. */ |
1004 | if (make_new_vtable (t, binfo)) |
1005 | { |
1006 | /* If we really did make a new vtable, we also made a copy |
1007 | of the BINFO_VIRTUALS list. Now, we have to find the |
1008 | corresponding entry in that list. */ |
1009 | *virtuals = BINFO_VIRTUALS (binfo); |
1010 | while (BV_FN (*virtuals) != BV_FN (v)) |
1011 | *virtuals = TREE_CHAIN (*virtuals); |
1012 | v = *virtuals; |
1013 | } |
1014 | |
1015 | BV_DELTA (v) = delta; |
1016 | BV_VCALL_INDEX (v) = NULL_TREE; |
1017 | BV_FN (v) = fndecl; |
1018 | } |
1019 | } |
1020 | |
1021 | |
1022 | /* Check if the object parameter of an iobj member function corresponds to |
1023 | another parameter type. CONTEXT is the class that the implicit object |
1024 | parameter is considered to refer to. */ |
1025 | |
1026 | bool |
1027 | iobj_parm_corresponds_to (tree iobj_fn, tree xobj_param, tree context) |
1028 | { |
1029 | tree iobj_fn_type = TREE_TYPE (iobj_fn); |
1030 | |
1031 | /* If the iobj member function was introduced with a using declaration, the |
1032 | type of its object parameter is considered to be that of the class it was |
1033 | introduced into. |
1034 | |
1035 | [over.match.funcs.general.4] |
1036 | For non-conversion functions that are implicit object member |
1037 | functions nominated by a using-declaration in a derived class, the |
1038 | function is considered to be a member of the derived class for the purpose |
1039 | of defining the type of the implicit object parameter. |
1040 | |
1041 | Unfortunately, because of this rule, we can't just compare the xobj member |
1042 | function's DECL_CONTEXT to its object parameter. |
1043 | |
1044 | struct S; |
1045 | |
1046 | struct B { |
1047 | int f(this S&) { return 5; } |
1048 | }; |
1049 | |
1050 | struct S : B { |
1051 | using B::f; |
1052 | int f() { return 10; } |
1053 | }; |
1054 | |
1055 | The using declaration does not change the object parameter of B::f as it |
1056 | is an xobj member function. However, its object parameter still |
1057 | corresponds to S::f as it was declared with an object parameter of type |
1058 | S const&. The DECL_CONTEXT of B::f is B, so if we compare the type of the |
1059 | object parameter to that, it will not match. If we naively assume a |
1060 | different type from the DECL_CONTEXT for an xobj parameter means that the |
1061 | object parameters do not correspond, then the object parameters in the |
1062 | above example will be considered non-corresponding. |
1063 | |
1064 | As a result of this, B::f would incorrectly not be discarded, causing an |
1065 | ambiguity when f is called on an object of type S. |
1066 | |
1067 | This also impacts member functions with constraints as in the following |
1068 | example. |
1069 | |
1070 | template<typename = void> |
1071 | struct S; |
1072 | |
1073 | template<typename = void> |
1074 | struct B { |
1075 | int f(this S<>&) requires true { return 5; } |
1076 | }; |
1077 | |
1078 | template<typename> |
1079 | struct S : B<> { |
1080 | using B<>::f; |
1081 | int f() { return 10; } |
1082 | }; |
1083 | |
1084 | Once again, if we compare the DECL_CONTEXT of B<>::f to it's xobj |
1085 | parameter, it would not match. If the object parameters do not |
1086 | correspond, constraints are not taken into account, so in this example we |
1087 | would (probably) get an ambiguous lookup instead of correctly picking |
1088 | B<>::f. |
1089 | |
1090 | Because of this caveat, we must actually compare the type of the iobj |
1091 | parameter to the type of the xobj parameter, shortcuts will have these |
1092 | edge cases. |
1093 | |
1094 | Aside from the more complex reasons above, this logic also implicitly |
1095 | handles xobj parameters of pointer type, we don't have to explicitly |
1096 | check for that case. */ |
1097 | |
1098 | if (!same_type_ignoring_top_level_qualifiers_p |
1099 | (context, non_reference (xobj_param))) |
1100 | return false; |
1101 | |
1102 | /* We don't get to bail yet even if we have a by-value xobj parameter, |
1103 | a by-value xobj parameter can correspond to an iobj parameter provided the |
1104 | iobj member function is not declared with a reference qualifier. |
1105 | |
1106 | From this point on, we know we are dealing with an xobj parameter that has |
1107 | an object parameter of the same type as the class it was declared in. |
1108 | We still don't know if we have a reference or by-value parameter yet |
1109 | though. */ |
1110 | |
1111 | cp_ref_qualifier const iobj_ref_qual = type_memfn_rqual (iobj_fn_type); |
1112 | /* We only care about cv qualifiers when determining correspondence. */ |
1113 | static constexpr cp_cv_quals cv_bits = TYPE_QUAL_VOLATILE |
1114 | | TYPE_QUAL_CONST; |
1115 | cp_cv_quals const iobj_cv_quals = type_memfn_quals (iobj_fn_type) & cv_bits; |
1116 | /* We need to ignore the ref qualifier of the xobj parameter if the iobj |
1117 | member function lacks a ref qualifier. |
1118 | |
1119 | [basic.scope.scope.3] |
1120 | Two non-static member functions have corresponding object parameters if: |
1121 | -- exactly one is an implicit object member function with no ref-qualifier |
1122 | and the types of their object parameters ([dcl.fct]), after removing |
1123 | top-level references, are the same, or |
1124 | -- their object parameters have the same type. |
1125 | |
1126 | The cv qualifiers of a by-value parameter are supposed to be discarded, so |
1127 | we ignore them. |
1128 | |
1129 | [dcl.fct.5] |
1130 | After producing the list of parameter types, any top-level cv-qualifiers |
1131 | modifying a parameter type are deleted when forming the function type. |
1132 | |
1133 | However, they still need to be taken into account when our xobj parameter |
1134 | is a reference that is being ignored (according to [basic.scope.scope.3] |
1135 | quoted above), but when we are actually dealing with a by-value xobj |
1136 | parameter we can proceed following this table. |
1137 | | iobj | xobj | equal | |
1138 | | none | none | X | |
1139 | | none | c | X | |
1140 | | none | v | X | |
1141 | | none | cv | X | |
1142 | | c | none | O | |
1143 | | c | c | O | |
1144 | | c | v | O | |
1145 | | c | cv | O | |
1146 | | v | none | O | |
1147 | | v | c | O | |
1148 | | v | v | O | |
1149 | | v | cv | O | |
1150 | | cv | none | O | |
1151 | | cv | c | O | |
1152 | | cv | v | O | |
1153 | | cv | cv | O | |
1154 | |
1155 | Additionally, if the iobj member function is ref qualified, we aren't |
1156 | ignoring the ref qualifier of the iobj parameter, so we can't be dealing |
1157 | with correspondence in that case either. |
1158 | |
1159 | So to recap, if we have a by-value xobj parameter, we know for sure that |
1160 | we aren't dealing with corresponding object parameters if the iobj member |
1161 | function has any cv-ref qualifiers. The only case where we might still be |
1162 | dealing with corresponding object parameters is when the iobj member |
1163 | function lacks any cv-ref qualification. */ |
1164 | if (!TYPE_REF_P (xobj_param)) |
1165 | { |
1166 | if (iobj_ref_qual || iobj_cv_quals) |
1167 | return false; |
1168 | } |
1169 | else |
1170 | { |
1171 | /* We are dealing with an xobj parameter that is a reference now, but due |
1172 | to [basic.scope.scope.3] we need to ignore its ref qual. */ |
1173 | cp_ref_qualifier const xobj_ref_qual = [&](){ |
1174 | if (!TYPE_REF_P (xobj_param) || !iobj_ref_qual) |
1175 | return REF_QUAL_NONE; |
1176 | return TYPE_REF_IS_RVALUE (xobj_param) ? REF_QUAL_RVALUE |
1177 | : REF_QUAL_LVALUE; |
1178 | }(); /* IILE. */ |
1179 | |
1180 | /* Even if we are ignoring the reference qualifier, the xobj parameter |
1181 | was still a reference so we still take the cv qualifiers into |
1182 | account. */ |
1183 | cp_cv_quals const xobj_cv_quals |
1184 | = cp_type_quals (TREE_TYPE (xobj_param)) & cv_bits; |
1185 | |
1186 | /* Finally, if the qualifications don't match exactly, the object |
1187 | parameters don't correspond. */ |
1188 | if (iobj_ref_qual != xobj_ref_qual |
1189 | || iobj_cv_quals != xobj_cv_quals) |
1190 | return false; |
1191 | } |
1192 | /* If we got past everything else, the object parameters of fn1 and fn2 |
1193 | definitely correspond. */ |
1194 | return true; |
1195 | } |
1196 | |
1197 | /* True if FN and METHOD have corresponding object parms per |
1198 | [basic.scope.scope], or if one of them is a static member function (which |
1199 | are considered to have an object parm that corresponds to any other). |
1200 | CONTEXT is the class that an implicit object member function is considered |
1201 | to be a member of for the purpose of this comparison, per |
1202 | [over.match.funcs]. */ |
1203 | |
1204 | bool |
1205 | object_parms_correspond (tree fn, tree method, tree context) |
1206 | { |
1207 | tree fn_type = TREE_TYPE (fn); |
1208 | tree method_type = TREE_TYPE (method); |
1209 | |
1210 | /* Compare the quals on the 'this' parm. Don't compare |
1211 | the whole types, as used functions are treated as |
1212 | coming from the using class in overload resolution. */ |
1213 | if (DECL_IOBJ_MEMBER_FUNCTION_P (fn) |
1214 | && DECL_IOBJ_MEMBER_FUNCTION_P (method)) |
1215 | { |
1216 | /* Either both or neither need to be ref-qualified for |
1217 | differing quals to allow overloading. */ |
1218 | if ((FUNCTION_REF_QUALIFIED (fn_type) |
1219 | == FUNCTION_REF_QUALIFIED (method_type)) |
1220 | && (type_memfn_quals (fn_type) != type_memfn_quals (method_type) |
1221 | || type_memfn_rqual (fn_type) != type_memfn_rqual (method_type))) |
1222 | return false; |
1223 | return true; |
1224 | } |
1225 | /* Treat a static member function as corresponding to any object parm. */ |
1226 | else if (DECL_STATIC_FUNCTION_P (fn) || DECL_STATIC_FUNCTION_P (method)) |
1227 | return true; |
1228 | /* Handle special correspondence rules for xobj vs xobj and xobj vs iobj |
1229 | member function declarations. |
1230 | We don't worry about static member functions here. */ |
1231 | else if (DECL_XOBJ_MEMBER_FUNCTION_P (fn) |
1232 | && DECL_XOBJ_MEMBER_FUNCTION_P (method)) |
1233 | { |
1234 | auto get_object_param = [] (tree fn) |
1235 | { |
1236 | return TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fn))); |
1237 | }; |
1238 | /* We skip the object parameter below, check it here instead of |
1239 | making changes to that code. */ |
1240 | tree fn_param = get_object_param (fn); |
1241 | tree method_param = get_object_param (method); |
1242 | if (!same_type_p (fn_param, method_param)) |
1243 | return false; |
1244 | } |
1245 | else |
1246 | { |
1247 | tree xobj_fn = DECL_XOBJ_MEMBER_FUNCTION_P (fn) ? fn : method; |
1248 | tree iobj_fn = xobj_fn != fn ? fn : method; |
1249 | tree xobj_param = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (xobj_fn))); |
1250 | |
1251 | return iobj_parm_corresponds_to (iobj_fn, xobj_param, context); |
1252 | } |
1253 | |
1254 | return true; |
1255 | } |
1256 | |
1257 | /* Add method METHOD to class TYPE. If VIA_USING indicates whether |
1258 | METHOD is being injected via a using_decl. Returns true if the |
1259 | method could be added to the method vec. */ |
1260 | |
1261 | bool |
1262 | add_method (tree type, tree method, bool via_using) |
1263 | { |
1264 | if (method == error_mark_node) |
1265 | return false; |
1266 | |
1267 | gcc_assert (!DECL_EXTERN_C_P (method)); |
1268 | |
1269 | tree *slot = find_member_slot (klass: type, DECL_NAME (method)); |
1270 | tree current_fns = slot ? *slot : NULL_TREE; |
1271 | |
1272 | /* See below. */ |
1273 | int losem = -1; |
1274 | |
1275 | /* Check to see if we've already got this method. */ |
1276 | for (ovl_iterator iter (current_fns); iter; ++iter) |
1277 | { |
1278 | tree fn = *iter; |
1279 | |
1280 | if (TREE_CODE (fn) != TREE_CODE (method)) |
1281 | continue; |
1282 | |
1283 | /* Two using-declarations can coexist, we'll complain about ambiguity in |
1284 | overload resolution. */ |
1285 | if (via_using && iter.using_p () |
1286 | /* Except handle inherited constructors specially. */ |
1287 | && ! DECL_CONSTRUCTOR_P (fn)) |
1288 | { |
1289 | if (fn == method) |
1290 | /* Don't add the same one twice. */ |
1291 | return false; |
1292 | continue; |
1293 | } |
1294 | |
1295 | /* [over.load] Member function declarations with the |
1296 | same name and the same parameter types cannot be |
1297 | overloaded if any of them is a static member |
1298 | function declaration. |
1299 | |
1300 | [over.load] Member function declarations with the same name and |
1301 | the same parameter-type-list as well as member function template |
1302 | declarations with the same name, the same parameter-type-list, and |
1303 | the same template parameter lists cannot be overloaded if any of |
1304 | them, but not all, have a ref-qualifier. |
1305 | |
1306 | [namespace.udecl] When a using-declaration brings names |
1307 | from a base class into a derived class scope, member |
1308 | functions in the derived class override and/or hide member |
1309 | functions with the same name and parameter types in a base |
1310 | class (rather than conflicting). */ |
1311 | if (!object_parms_correspond (fn, method, context: type)) |
1312 | continue; |
1313 | |
1314 | tree fn_type = TREE_TYPE (fn); |
1315 | tree method_type = TREE_TYPE (method); |
1316 | |
1317 | tree real_fn = fn; |
1318 | tree real_method = method; |
1319 | |
1320 | /* Templates and conversion ops must match return types. */ |
1321 | if ((DECL_CONV_FN_P (fn) || TREE_CODE (fn) == TEMPLATE_DECL) |
1322 | && !same_type_p (TREE_TYPE (fn_type), TREE_TYPE (method_type))) |
1323 | continue; |
1324 | |
1325 | /* For templates, the template parameters must be identical. */ |
1326 | if (TREE_CODE (fn) == TEMPLATE_DECL) |
1327 | { |
1328 | if (!comp_template_parms (DECL_TEMPLATE_PARMS (fn), |
1329 | DECL_TEMPLATE_PARMS (method))) |
1330 | continue; |
1331 | |
1332 | real_fn = DECL_TEMPLATE_RESULT (fn); |
1333 | real_method = DECL_TEMPLATE_RESULT (method); |
1334 | } |
1335 | |
1336 | tree parms1 = TYPE_ARG_TYPES (fn_type); |
1337 | tree parms2 = TYPE_ARG_TYPES (method_type); |
1338 | if (! DECL_STATIC_FUNCTION_P (real_fn)) |
1339 | parms1 = TREE_CHAIN (parms1); |
1340 | if (! DECL_STATIC_FUNCTION_P (real_method)) |
1341 | parms2 = TREE_CHAIN (parms2); |
1342 | |
1343 | /* Bring back parameters omitted from an inherited ctor. The |
1344 | method and the function can have different omittedness. */ |
1345 | if (ctor_omit_inherited_parms (real_fn)) |
1346 | parms1 = FUNCTION_FIRST_USER_PARMTYPE (DECL_CLONED_FUNCTION (real_fn)); |
1347 | if (ctor_omit_inherited_parms (real_method)) |
1348 | parms2 = (FUNCTION_FIRST_USER_PARMTYPE |
1349 | (DECL_CLONED_FUNCTION (real_method))); |
1350 | |
1351 | if (!compparms (parms1, parms2)) |
1352 | continue; |
1353 | |
1354 | if (!equivalently_constrained (fn, method)) |
1355 | { |
1356 | if (processing_template_decl) |
1357 | /* We can't check satisfaction in dependent context, wait until |
1358 | the class is instantiated. */ |
1359 | continue; |
1360 | |
1361 | special_function_kind sfk = special_memfn_p (method); |
1362 | |
1363 | if (sfk == sfk_none |
1364 | || DECL_INHERITED_CTOR (fn) |
1365 | || TREE_CODE (fn) == TEMPLATE_DECL) |
1366 | /* Member function templates and non-special member functions |
1367 | coexist if they are not equivalently constrained. A member |
1368 | function is not hidden by an inherited constructor. */ |
1369 | continue; |
1370 | |
1371 | /* P0848: For special member functions, deleted, unsatisfied, or |
1372 | less constrained overloads are ineligible. We implement this |
1373 | by removing them from CLASSTYPE_MEMBER_VEC. Destructors don't |
1374 | use the notion of eligibility, and the selected destructor can |
1375 | be deleted, but removing unsatisfied or less constrained |
1376 | overloads has the same effect as overload resolution. */ |
1377 | bool dtor = (sfk == sfk_destructor); |
1378 | if (losem == -1) |
1379 | losem = ((!dtor && DECL_DELETED_FN (method)) |
1380 | || !constraints_satisfied_p (method)); |
1381 | bool losef = ((!dtor && DECL_DELETED_FN (fn)) |
1382 | || !constraints_satisfied_p (fn)); |
1383 | int win; |
1384 | if (losem || losef) |
1385 | win = losem - losef; |
1386 | else |
1387 | win = more_constrained (fn, method); |
1388 | if (win > 0) |
1389 | /* Leave FN in the method vec, discard METHOD. */ |
1390 | return false; |
1391 | else if (win < 0) |
1392 | { |
1393 | /* Remove FN, add METHOD. */ |
1394 | current_fns = iter.remove_node (head: current_fns); |
1395 | continue; |
1396 | } |
1397 | else |
1398 | /* Let them coexist for now. */ |
1399 | continue; |
1400 | } |
1401 | |
1402 | /* If these are versions of the same function, process and |
1403 | move on. */ |
1404 | if (TREE_CODE (fn) == FUNCTION_DECL |
1405 | && maybe_version_functions (method, fn, true)) |
1406 | continue; |
1407 | |
1408 | if (DECL_INHERITED_CTOR (method)) |
1409 | { |
1410 | if (!DECL_INHERITED_CTOR (fn)) |
1411 | /* Defer to the other function. */ |
1412 | return false; |
1413 | |
1414 | tree basem = DECL_INHERITED_CTOR_BASE (method); |
1415 | tree basef = DECL_INHERITED_CTOR_BASE (fn); |
1416 | if (flag_new_inheriting_ctors) |
1417 | { |
1418 | if (basem == basef) |
1419 | { |
1420 | /* Inheriting the same constructor along different |
1421 | paths, combine them. */ |
1422 | SET_DECL_INHERITED_CTOR |
1423 | (fn, ovl_make (DECL_INHERITED_CTOR (method), |
1424 | DECL_INHERITED_CTOR (fn))); |
1425 | /* And discard the new one. */ |
1426 | return false; |
1427 | } |
1428 | else |
1429 | /* Inherited ctors can coexist until overload |
1430 | resolution. */ |
1431 | continue; |
1432 | } |
1433 | |
1434 | error_at (DECL_SOURCE_LOCATION (method), |
1435 | "%q#D conflicts with version inherited from %qT" , |
1436 | method, basef); |
1437 | inform (DECL_SOURCE_LOCATION (fn), |
1438 | "version inherited from %qT declared here" , |
1439 | basef); |
1440 | return false; |
1441 | } |
1442 | |
1443 | if (via_using) |
1444 | /* Defer to the local function. */ |
1445 | return false; |
1446 | else if (iter.using_p () |
1447 | || (flag_new_inheriting_ctors |
1448 | && DECL_INHERITED_CTOR (fn))) |
1449 | { |
1450 | /* Remove the inherited function. */ |
1451 | current_fns = iter.remove_node (head: current_fns); |
1452 | continue; |
1453 | } |
1454 | else |
1455 | { |
1456 | error_at (DECL_SOURCE_LOCATION (method), |
1457 | "%q#D cannot be overloaded with %q#D" , method, fn); |
1458 | inform (DECL_SOURCE_LOCATION (fn), |
1459 | "previous declaration %q#D" , fn); |
1460 | return false; |
1461 | } |
1462 | } |
1463 | |
1464 | current_fns = ovl_insert (fn: method, maybe_ovl: current_fns, using_or_hidden: via_using); |
1465 | |
1466 | if (!COMPLETE_TYPE_P (type) && !DECL_CONV_FN_P (method) |
1467 | && !push_class_level_binding (DECL_NAME (method), current_fns)) |
1468 | return false; |
1469 | |
1470 | if (!slot) |
1471 | slot = add_member_slot (klass: type, DECL_NAME (method)); |
1472 | |
1473 | /* Maintain TYPE_HAS_USER_CONSTRUCTOR, etc. */ |
1474 | grok_special_member_properties (method); |
1475 | |
1476 | *slot = current_fns; |
1477 | |
1478 | return true; |
1479 | } |
1480 | |
1481 | /* Subroutines of finish_struct. */ |
1482 | |
1483 | /* Change the access of FDECL to ACCESS in T. Return 1 if change was |
1484 | legit, otherwise return 0. */ |
1485 | |
1486 | static int |
1487 | alter_access (tree t, tree fdecl, tree access) |
1488 | { |
1489 | tree elem; |
1490 | |
1491 | retrofit_lang_decl (fdecl); |
1492 | |
1493 | gcc_assert (!DECL_DISCRIMINATOR_P (fdecl)); |
1494 | |
1495 | elem = purpose_member (t, DECL_ACCESS (fdecl)); |
1496 | if (elem) |
1497 | { |
1498 | if (TREE_VALUE (elem) != access) |
1499 | { |
1500 | if (TREE_CODE (TREE_TYPE (fdecl)) == FUNCTION_DECL) |
1501 | error ("conflicting access specifications for method" |
1502 | " %q+D, ignored" , TREE_TYPE (fdecl)); |
1503 | else |
1504 | error ("conflicting access specifications for field %qE, ignored" , |
1505 | DECL_NAME (fdecl)); |
1506 | } |
1507 | else |
1508 | { |
1509 | /* They're changing the access to the same thing they changed |
1510 | it to before. That's OK. */ |
1511 | ; |
1512 | } |
1513 | } |
1514 | else |
1515 | { |
1516 | perform_or_defer_access_check (TYPE_BINFO (t), fdecl, fdecl, |
1517 | tf_warning_or_error); |
1518 | DECL_ACCESS (fdecl) = tree_cons (t, access, DECL_ACCESS (fdecl)); |
1519 | return 1; |
1520 | } |
1521 | return 0; |
1522 | } |
1523 | |
1524 | /* Return the access node for DECL's access in its enclosing class. */ |
1525 | |
1526 | tree |
1527 | declared_access (tree decl) |
1528 | { |
1529 | return (TREE_PRIVATE (decl) ? access_private_node |
1530 | : TREE_PROTECTED (decl) ? access_protected_node |
1531 | : access_public_node); |
1532 | } |
1533 | |
1534 | /* If DECL is a non-dependent using of non-ctor function members, push them |
1535 | and return true, otherwise return false. Called from |
1536 | finish_member_declaration. */ |
1537 | |
1538 | bool |
1539 | maybe_push_used_methods (tree decl) |
1540 | { |
1541 | if (TREE_CODE (decl) != USING_DECL) |
1542 | return false; |
1543 | tree used = strip_using_decl (decl); |
1544 | if (!used || !is_overloaded_fn (used)) |
1545 | return false; |
1546 | |
1547 | /* Add the functions to CLASSTYPE_MEMBER_VEC so that overload resolution |
1548 | works within the class body. */ |
1549 | for (tree f : ovl_range (used)) |
1550 | { |
1551 | if (DECL_CONSTRUCTOR_P (f)) |
1552 | /* Inheriting constructors are handled separately. */ |
1553 | return false; |
1554 | |
1555 | bool added = add_method (current_class_type, method: f, via_using: true); |
1556 | |
1557 | if (added) |
1558 | alter_access (current_class_type, fdecl: f, current_access_specifier); |
1559 | |
1560 | /* If add_method returns false because f was already declared, look |
1561 | for a duplicate using-declaration. */ |
1562 | else |
1563 | for (tree d = TYPE_FIELDS (current_class_type); d; d = DECL_CHAIN (d)) |
1564 | if (TREE_CODE (d) == USING_DECL |
1565 | && DECL_NAME (d) == DECL_NAME (decl) |
1566 | && same_type_p (USING_DECL_SCOPE (d), USING_DECL_SCOPE (decl))) |
1567 | { |
1568 | diagnose_name_conflict (decl, d); |
1569 | break; |
1570 | } |
1571 | } |
1572 | return true; |
1573 | } |
1574 | |
1575 | /* Process the USING_DECL, which is a member of T. */ |
1576 | |
1577 | static void |
1578 | handle_using_decl (tree using_decl, tree t) |
1579 | { |
1580 | tree decl = USING_DECL_DECLS (using_decl); |
1581 | |
1582 | gcc_assert (!processing_template_decl && decl); |
1583 | |
1584 | cp_emit_debug_info_for_using (decl, t); |
1585 | |
1586 | if (is_overloaded_fn (decl)) |
1587 | /* Handled in maybe_push_used_methods. */ |
1588 | return; |
1589 | |
1590 | tree name = DECL_NAME (using_decl); |
1591 | tree old_value = lookup_member (t, name, /*protect=*/0, /*want_type=*/false, |
1592 | tf_warning_or_error); |
1593 | if (old_value) |
1594 | { |
1595 | old_value = OVL_FIRST (old_value); |
1596 | |
1597 | if (DECL_P (old_value) && DECL_CONTEXT (old_value) == t) |
1598 | /* OK */; |
1599 | else |
1600 | old_value = NULL_TREE; |
1601 | } |
1602 | |
1603 | if (! old_value) |
1604 | ; |
1605 | else if (is_overloaded_fn (old_value)) |
1606 | { |
1607 | error_at (DECL_SOURCE_LOCATION (using_decl), "%qD invalid in %q#T " |
1608 | "because of local method %q#D with same name" , |
1609 | using_decl, t, old_value); |
1610 | inform (DECL_SOURCE_LOCATION (old_value), |
1611 | "local method %q#D declared here" , old_value); |
1612 | return; |
1613 | } |
1614 | else if (!DECL_ARTIFICIAL (old_value)) |
1615 | { |
1616 | error_at (DECL_SOURCE_LOCATION (using_decl), "%qD invalid in %q#T " |
1617 | "because of local member %q#D with same name" , |
1618 | using_decl, t, old_value); |
1619 | inform (DECL_SOURCE_LOCATION (old_value), |
1620 | "local member %q#D declared here" , old_value); |
1621 | return; |
1622 | } |
1623 | |
1624 | iloc_sentinel ils (DECL_SOURCE_LOCATION (using_decl)); |
1625 | tree access = declared_access (decl: using_decl); |
1626 | |
1627 | /* Make type T see field decl FDECL with access ACCESS. */ |
1628 | if (USING_DECL_UNRELATED_P (using_decl)) |
1629 | { |
1630 | /* C++20 using enum can import non-inherited enumerators into class |
1631 | scope. We implement that by making a copy of the CONST_DECL for which |
1632 | CONST_DECL_USING_P is true. */ |
1633 | gcc_assert (TREE_CODE (decl) == CONST_DECL); |
1634 | |
1635 | auto cas = make_temp_override (current_access_specifier, overrider: access); |
1636 | tree copy = copy_decl (decl); |
1637 | DECL_CONTEXT (copy) = t; |
1638 | DECL_ARTIFICIAL (copy) = true; |
1639 | /* We emitted debug info for the USING_DECL above; make sure we don't |
1640 | also emit anything for this clone. */ |
1641 | DECL_IGNORED_P (copy) = true; |
1642 | DECL_SOURCE_LOCATION (copy) = DECL_SOURCE_LOCATION (using_decl); |
1643 | finish_member_declaration (copy); |
1644 | DECL_ABSTRACT_ORIGIN (copy) = decl; |
1645 | } |
1646 | else |
1647 | alter_access (t, fdecl: decl, access); |
1648 | } |
1649 | |
1650 | /* Data structure for find_abi_tags_r, below. */ |
1651 | |
1652 | struct abi_tag_data |
1653 | { |
1654 | tree t; // The type that we're checking for missing tags. |
1655 | tree subob; // The subobject of T that we're getting tags from. |
1656 | tree tags; // error_mark_node for diagnostics, or a list of missing tags. |
1657 | }; |
1658 | |
1659 | /* Subroutine of find_abi_tags_r. Handle a single TAG found on the class TP |
1660 | in the context of P. TAG can be either an identifier (the DECL_NAME of |
1661 | a tag NAMESPACE_DECL) or a STRING_CST (a tag attribute). */ |
1662 | |
1663 | static void |
1664 | check_tag (tree tag, tree id, tree *tp, abi_tag_data *p) |
1665 | { |
1666 | if (!IDENTIFIER_MARKED (id)) |
1667 | { |
1668 | if (p->tags != error_mark_node) |
1669 | { |
1670 | /* We're collecting tags from template arguments or from |
1671 | the type of a variable or function return type. */ |
1672 | p->tags = tree_cons (NULL_TREE, tag, p->tags); |
1673 | |
1674 | /* Don't inherit this tag multiple times. */ |
1675 | IDENTIFIER_MARKED (id) = true; |
1676 | |
1677 | if (TYPE_P (p->t)) |
1678 | { |
1679 | /* Tags inherited from type template arguments are only used |
1680 | to avoid warnings. */ |
1681 | ABI_TAG_IMPLICIT (p->tags) = true; |
1682 | return; |
1683 | } |
1684 | /* For functions and variables we want to warn, too. */ |
1685 | } |
1686 | |
1687 | /* Otherwise we're diagnosing missing tags. */ |
1688 | if (TREE_CODE (p->t) == FUNCTION_DECL) |
1689 | { |
1690 | auto_diagnostic_group d; |
1691 | if (warning (OPT_Wabi_tag, "%qD inherits the %E ABI tag " |
1692 | "that %qT (used in its return type) has" , |
1693 | p->t, tag, *tp)) |
1694 | inform (location_of (*tp), "%qT declared here" , *tp); |
1695 | } |
1696 | else if (VAR_P (p->t)) |
1697 | { |
1698 | auto_diagnostic_group d; |
1699 | if (warning (OPT_Wabi_tag, "%qD inherits the %E ABI tag " |
1700 | "that %qT (used in its type) has" , p->t, tag, *tp)) |
1701 | inform (location_of (*tp), "%qT declared here" , *tp); |
1702 | } |
1703 | else if (TYPE_P (p->subob)) |
1704 | { |
1705 | auto_diagnostic_group d; |
1706 | if (warning (OPT_Wabi_tag, "%qT does not have the %E ABI tag " |
1707 | "that base %qT has" , p->t, tag, p->subob)) |
1708 | inform (location_of (p->subob), "%qT declared here" , |
1709 | p->subob); |
1710 | } |
1711 | else |
1712 | { |
1713 | auto_diagnostic_group d; |
1714 | if (warning (OPT_Wabi_tag, "%qT does not have the %E ABI tag " |
1715 | "that %qT (used in the type of %qD) has" , |
1716 | p->t, tag, *tp, p->subob)) |
1717 | { |
1718 | inform (location_of (p->subob), "%qD declared here" , |
1719 | p->subob); |
1720 | inform (location_of (*tp), "%qT declared here" , *tp); |
1721 | } |
1722 | } |
1723 | } |
1724 | } |
1725 | |
1726 | /* Find all the ABI tags in the attribute list ATTR and either call |
1727 | check_tag (if TP is non-null) or set IDENTIFIER_MARKED to val. */ |
1728 | |
1729 | static void |
1730 | mark_or_check_attr_tags (tree attr, tree *tp, abi_tag_data *p, bool val) |
1731 | { |
1732 | if (!attr) |
1733 | return; |
1734 | for (; (attr = lookup_attribute (attr_name: "abi_tag" , list: attr)); |
1735 | attr = TREE_CHAIN (attr)) |
1736 | for (tree list = TREE_VALUE (attr); list; |
1737 | list = TREE_CHAIN (list)) |
1738 | { |
1739 | tree tag = TREE_VALUE (list); |
1740 | tree id = get_identifier (TREE_STRING_POINTER (tag)); |
1741 | if (tp) |
1742 | check_tag (tag, id, tp, p); |
1743 | else |
1744 | IDENTIFIER_MARKED (id) = val; |
1745 | } |
1746 | } |
1747 | |
1748 | /* Find all the ABI tags on T and its enclosing scopes and either call |
1749 | check_tag (if TP is non-null) or set IDENTIFIER_MARKED to val. */ |
1750 | |
1751 | static void |
1752 | mark_or_check_tags (tree t, tree *tp, abi_tag_data *p, bool val) |
1753 | { |
1754 | while (t != global_namespace) |
1755 | { |
1756 | tree attr; |
1757 | if (TYPE_P (t)) |
1758 | { |
1759 | attr = TYPE_ATTRIBUTES (t); |
1760 | t = CP_TYPE_CONTEXT (t); |
1761 | } |
1762 | else |
1763 | { |
1764 | attr = DECL_ATTRIBUTES (t); |
1765 | t = CP_DECL_CONTEXT (t); |
1766 | } |
1767 | mark_or_check_attr_tags (attr, tp, p, val); |
1768 | } |
1769 | } |
1770 | |
1771 | /* walk_tree callback for check_abi_tags: if the type at *TP involves any |
1772 | types with ABI tags, add the corresponding identifiers to the VEC in |
1773 | *DATA and set IDENTIFIER_MARKED. */ |
1774 | |
1775 | static tree |
1776 | find_abi_tags_r (tree *tp, int *walk_subtrees, void *data) |
1777 | { |
1778 | if (TYPE_P (*tp) && *walk_subtrees == 1 && flag_abi_version != 14) |
1779 | /* Tell cp_walk_subtrees to look though typedefs. [PR98481] */ |
1780 | *walk_subtrees = 2; |
1781 | |
1782 | if (!OVERLOAD_TYPE_P (*tp)) |
1783 | return NULL_TREE; |
1784 | |
1785 | /* walk_tree shouldn't be walking into any subtrees of a RECORD_TYPE |
1786 | anyway, but let's make sure of it. */ |
1787 | *walk_subtrees = false; |
1788 | |
1789 | abi_tag_data *p = static_cast<struct abi_tag_data*>(data); |
1790 | |
1791 | mark_or_check_tags (t: *tp, tp, p, val: false); |
1792 | |
1793 | return NULL_TREE; |
1794 | } |
1795 | |
1796 | /* walk_tree callback for mark_abi_tags: if *TP is a class, set |
1797 | IDENTIFIER_MARKED on its ABI tags. */ |
1798 | |
1799 | static tree |
1800 | mark_abi_tags_r (tree *tp, int *walk_subtrees, void *data) |
1801 | { |
1802 | if (TYPE_P (*tp) && *walk_subtrees == 1 && flag_abi_version != 14) |
1803 | /* Tell cp_walk_subtrees to look though typedefs. */ |
1804 | *walk_subtrees = 2; |
1805 | |
1806 | if (!OVERLOAD_TYPE_P (*tp)) |
1807 | return NULL_TREE; |
1808 | |
1809 | /* walk_tree shouldn't be walking into any subtrees of a RECORD_TYPE |
1810 | anyway, but let's make sure of it. */ |
1811 | *walk_subtrees = false; |
1812 | |
1813 | bool *valp = static_cast<bool*>(data); |
1814 | |
1815 | mark_or_check_tags (t: *tp, NULL, NULL, val: *valp); |
1816 | |
1817 | return NULL_TREE; |
1818 | } |
1819 | |
1820 | /* Set IDENTIFIER_MARKED on all the ABI tags on T and its enclosing |
1821 | scopes. */ |
1822 | |
1823 | static void |
1824 | mark_abi_tags (tree t, bool val) |
1825 | { |
1826 | mark_or_check_tags (t, NULL, NULL, val); |
1827 | if (DECL_P (t)) |
1828 | { |
1829 | if (DECL_LANG_SPECIFIC (t) && DECL_USE_TEMPLATE (t) |
1830 | && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (t))) |
1831 | { |
1832 | /* Template arguments are part of the signature. */ |
1833 | tree level = INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (t)); |
1834 | for (int j = 0; j < TREE_VEC_LENGTH (level); ++j) |
1835 | { |
1836 | tree arg = TREE_VEC_ELT (level, j); |
1837 | cp_walk_tree_without_duplicates (&arg, mark_abi_tags_r, &val); |
1838 | } |
1839 | } |
1840 | if (TREE_CODE (t) == FUNCTION_DECL) |
1841 | /* A function's parameter types are part of the signature, so |
1842 | we don't need to inherit any tags that are also in them. */ |
1843 | for (tree arg = FUNCTION_FIRST_USER_PARMTYPE (t); arg; |
1844 | arg = TREE_CHAIN (arg)) |
1845 | cp_walk_tree_without_duplicates (&TREE_VALUE (arg), |
1846 | mark_abi_tags_r, &val); |
1847 | } |
1848 | } |
1849 | |
1850 | /* Check that T has all the ABI tags that subobject SUBOB has, or |
1851 | warn if not. If T is a (variable or function) declaration, also |
1852 | return any missing tags, and add them to T if JUST_CHECKING is false. */ |
1853 | |
1854 | static tree |
1855 | check_abi_tags (tree t, tree subob, bool just_checking = false) |
1856 | { |
1857 | bool inherit = DECL_P (t); |
1858 | |
1859 | if (!inherit && !warn_abi_tag) |
1860 | return NULL_TREE; |
1861 | |
1862 | tree decl = TYPE_P (t) ? TYPE_NAME (t) : t; |
1863 | if (!TREE_PUBLIC (decl)) |
1864 | /* No need to worry about things local to this TU. */ |
1865 | return NULL_TREE; |
1866 | |
1867 | mark_abi_tags (t, val: true); |
1868 | |
1869 | tree subtype = TYPE_P (subob) ? subob : TREE_TYPE (subob); |
1870 | struct abi_tag_data data = { .t: t, .subob: subob, error_mark_node }; |
1871 | if (inherit) |
1872 | data.tags = NULL_TREE; |
1873 | |
1874 | cp_walk_tree_without_duplicates (&subtype, find_abi_tags_r, &data); |
1875 | |
1876 | if (!(inherit && data.tags)) |
1877 | /* We don't need to do anything with data.tags. */; |
1878 | else if (just_checking) |
1879 | for (tree t = data.tags; t; t = TREE_CHAIN (t)) |
1880 | { |
1881 | tree id = get_identifier (TREE_STRING_POINTER (TREE_VALUE (t))); |
1882 | IDENTIFIER_MARKED (id) = false; |
1883 | } |
1884 | else |
1885 | { |
1886 | tree attr = lookup_attribute (attr_name: "abi_tag" , DECL_ATTRIBUTES (t)); |
1887 | if (attr) |
1888 | TREE_VALUE (attr) = chainon (data.tags, TREE_VALUE (attr)); |
1889 | else |
1890 | DECL_ATTRIBUTES (t) |
1891 | = tree_cons (abi_tag_identifier, data.tags, DECL_ATTRIBUTES (t)); |
1892 | } |
1893 | |
1894 | mark_abi_tags (t, val: false); |
1895 | |
1896 | return data.tags; |
1897 | } |
1898 | |
1899 | /* Check that DECL has all the ABI tags that are used in parts of its type |
1900 | that are not reflected in its mangled name. */ |
1901 | |
1902 | void |
1903 | check_abi_tags (tree decl) |
1904 | { |
1905 | if (VAR_P (decl)) |
1906 | check_abi_tags (t: decl, TREE_TYPE (decl)); |
1907 | else if (TREE_CODE (decl) == FUNCTION_DECL |
1908 | && !DECL_CONV_FN_P (decl) |
1909 | && !mangle_return_type_p (decl)) |
1910 | check_abi_tags (t: decl, TREE_TYPE (TREE_TYPE (decl))); |
1911 | } |
1912 | |
1913 | /* Return any ABI tags that are used in parts of the type of DECL |
1914 | that are not reflected in its mangled name. This function is only |
1915 | used in backward-compatible mangling for ABI <11. */ |
1916 | |
1917 | tree |
1918 | missing_abi_tags (tree decl) |
1919 | { |
1920 | if (VAR_P (decl)) |
1921 | return check_abi_tags (t: decl, TREE_TYPE (decl), just_checking: true); |
1922 | else if (TREE_CODE (decl) == FUNCTION_DECL |
1923 | /* Don't check DECL_CONV_FN_P here like we do in check_abi_tags, so |
1924 | that we can use this function for setting need_abi_warning |
1925 | regardless of the current flag_abi_version. */ |
1926 | && !mangle_return_type_p (decl)) |
1927 | return check_abi_tags (t: decl, TREE_TYPE (TREE_TYPE (decl)), just_checking: true); |
1928 | else |
1929 | return NULL_TREE; |
1930 | } |
1931 | |
1932 | void |
1933 | inherit_targ_abi_tags (tree t) |
1934 | { |
1935 | if (!CLASS_TYPE_P (t) |
1936 | || CLASSTYPE_TEMPLATE_INFO (t) == NULL_TREE) |
1937 | return; |
1938 | |
1939 | mark_abi_tags (t, val: true); |
1940 | |
1941 | tree args = CLASSTYPE_TI_ARGS (t); |
1942 | struct abi_tag_data data = { .t: t, NULL_TREE, NULL_TREE }; |
1943 | for (int i = 0; i < TMPL_ARGS_DEPTH (args); ++i) |
1944 | { |
1945 | tree level = TMPL_ARGS_LEVEL (args, i+1); |
1946 | for (int j = 0; j < TREE_VEC_LENGTH (level); ++j) |
1947 | { |
1948 | tree arg = TREE_VEC_ELT (level, j); |
1949 | data.subob = arg; |
1950 | cp_walk_tree_without_duplicates (&arg, find_abi_tags_r, &data); |
1951 | } |
1952 | } |
1953 | |
1954 | // If we found some tags on our template arguments, add them to our |
1955 | // abi_tag attribute. |
1956 | if (data.tags) |
1957 | { |
1958 | tree attr = lookup_attribute (attr_name: "abi_tag" , TYPE_ATTRIBUTES (t)); |
1959 | if (attr) |
1960 | TREE_VALUE (attr) = chainon (data.tags, TREE_VALUE (attr)); |
1961 | else |
1962 | TYPE_ATTRIBUTES (t) |
1963 | = tree_cons (abi_tag_identifier, data.tags, TYPE_ATTRIBUTES (t)); |
1964 | } |
1965 | |
1966 | mark_abi_tags (t, val: false); |
1967 | } |
1968 | |
1969 | /* Return true, iff class T has a non-virtual destructor that is |
1970 | accessible from outside the class heirarchy (i.e. is public, or |
1971 | there's a suitable friend. */ |
1972 | |
1973 | static bool |
1974 | accessible_nvdtor_p (tree t) |
1975 | { |
1976 | tree dtor = CLASSTYPE_DESTRUCTOR (t); |
1977 | |
1978 | /* An implicitly declared destructor is always public. And, |
1979 | if it were virtual, we would have created it by now. */ |
1980 | if (!dtor) |
1981 | return true; |
1982 | |
1983 | if (DECL_VINDEX (dtor)) |
1984 | return false; /* Virtual */ |
1985 | |
1986 | if (!TREE_PRIVATE (dtor) && !TREE_PROTECTED (dtor)) |
1987 | return true; /* Public */ |
1988 | |
1989 | if (CLASSTYPE_FRIEND_CLASSES (t) |
1990 | || DECL_FRIENDLIST (TYPE_MAIN_DECL (t))) |
1991 | return true; /* Has friends */ |
1992 | |
1993 | return false; |
1994 | } |
1995 | |
1996 | /* Run through the base classes of T, updating CANT_HAVE_CONST_CTOR_P, |
1997 | and NO_CONST_ASN_REF_P. Also set flag bits in T based on |
1998 | properties of the bases. */ |
1999 | |
2000 | static void |
2001 | check_bases (tree t, |
2002 | int* cant_have_const_ctor_p, |
2003 | int* no_const_asn_ref_p) |
2004 | { |
2005 | int i; |
2006 | bool seen_non_virtual_nearly_empty_base_p = 0; |
2007 | int seen_tm_mask = 0; |
2008 | tree base_binfo; |
2009 | tree binfo; |
2010 | tree field = NULL_TREE; |
2011 | |
2012 | if (!CLASSTYPE_NON_STD_LAYOUT (t)) |
2013 | for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field)) |
2014 | if (TREE_CODE (field) == FIELD_DECL) |
2015 | break; |
2016 | |
2017 | for (binfo = TYPE_BINFO (t), i = 0; |
2018 | BINFO_BASE_ITERATE (binfo, i, base_binfo); i++) |
2019 | { |
2020 | tree basetype = TREE_TYPE (base_binfo); |
2021 | |
2022 | gcc_assert (COMPLETE_TYPE_P (basetype)); |
2023 | |
2024 | if (CLASSTYPE_FINAL (basetype)) |
2025 | error ("cannot derive from %<final%> base %qT in derived type %qT" , |
2026 | basetype, t); |
2027 | |
2028 | /* If any base class is non-literal, so is the derived class. */ |
2029 | if (!CLASSTYPE_LITERAL_P (basetype)) |
2030 | CLASSTYPE_LITERAL_P (t) = false; |
2031 | |
2032 | /* If the base class doesn't have copy constructors or |
2033 | assignment operators that take const references, then the |
2034 | derived class cannot have such a member automatically |
2035 | generated. */ |
2036 | if (TYPE_HAS_COPY_CTOR (basetype) |
2037 | && ! TYPE_HAS_CONST_COPY_CTOR (basetype)) |
2038 | *cant_have_const_ctor_p = 1; |
2039 | if (TYPE_HAS_COPY_ASSIGN (basetype) |
2040 | && !TYPE_HAS_CONST_COPY_ASSIGN (basetype)) |
2041 | *no_const_asn_ref_p = 1; |
2042 | |
2043 | if (BINFO_VIRTUAL_P (base_binfo)) |
2044 | /* A virtual base does not effect nearly emptiness. */ |
2045 | ; |
2046 | else if (CLASSTYPE_NEARLY_EMPTY_P (basetype)) |
2047 | { |
2048 | if (seen_non_virtual_nearly_empty_base_p) |
2049 | /* And if there is more than one nearly empty base, then the |
2050 | derived class is not nearly empty either. */ |
2051 | CLASSTYPE_NEARLY_EMPTY_P (t) = 0; |
2052 | else |
2053 | /* Remember we've seen one. */ |
2054 | seen_non_virtual_nearly_empty_base_p = 1; |
2055 | } |
2056 | else if (!is_empty_class (basetype)) |
2057 | /* If the base class is not empty or nearly empty, then this |
2058 | class cannot be nearly empty. */ |
2059 | CLASSTYPE_NEARLY_EMPTY_P (t) = 0; |
2060 | |
2061 | /* A lot of properties from the bases also apply to the derived |
2062 | class. */ |
2063 | TYPE_NEEDS_CONSTRUCTING (t) |= TYPE_NEEDS_CONSTRUCTING (basetype); |
2064 | TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t) |
2065 | |= TYPE_HAS_NONTRIVIAL_DESTRUCTOR (basetype); |
2066 | TYPE_HAS_COMPLEX_COPY_ASSIGN (t) |
2067 | |= (TYPE_HAS_COMPLEX_COPY_ASSIGN (basetype) |
2068 | || !TYPE_HAS_COPY_ASSIGN (basetype)); |
2069 | TYPE_HAS_COMPLEX_COPY_CTOR (t) |= (TYPE_HAS_COMPLEX_COPY_CTOR (basetype) |
2070 | || !TYPE_HAS_COPY_CTOR (basetype)); |
2071 | TYPE_HAS_COMPLEX_MOVE_ASSIGN (t) |
2072 | |= TYPE_HAS_COMPLEX_MOVE_ASSIGN (basetype); |
2073 | TYPE_HAS_COMPLEX_MOVE_CTOR (t) |= TYPE_HAS_COMPLEX_MOVE_CTOR (basetype); |
2074 | TYPE_POLYMORPHIC_P (t) |= TYPE_POLYMORPHIC_P (basetype); |
2075 | CLASSTYPE_CONTAINS_EMPTY_CLASS_P (t) |
2076 | |= CLASSTYPE_CONTAINS_EMPTY_CLASS_P (basetype); |
2077 | TYPE_HAS_COMPLEX_DFLT (t) |= (!TYPE_HAS_DEFAULT_CONSTRUCTOR (basetype) |
2078 | || TYPE_HAS_COMPLEX_DFLT (basetype)); |
2079 | SET_CLASSTYPE_READONLY_FIELDS_NEED_INIT |
2080 | (t, CLASSTYPE_READONLY_FIELDS_NEED_INIT (t) |
2081 | | CLASSTYPE_READONLY_FIELDS_NEED_INIT (basetype)); |
2082 | SET_CLASSTYPE_REF_FIELDS_NEED_INIT |
2083 | (t, CLASSTYPE_REF_FIELDS_NEED_INIT (t) |
2084 | | CLASSTYPE_REF_FIELDS_NEED_INIT (basetype)); |
2085 | if (TYPE_HAS_MUTABLE_P (basetype)) |
2086 | CLASSTYPE_HAS_MUTABLE (t) = 1; |
2087 | |
2088 | /* A standard-layout class is a class that: |
2089 | ... |
2090 | * has no non-standard-layout base classes, */ |
2091 | CLASSTYPE_NON_STD_LAYOUT (t) |= CLASSTYPE_NON_STD_LAYOUT (basetype); |
2092 | if (!CLASSTYPE_NON_STD_LAYOUT (t)) |
2093 | { |
2094 | tree basefield; |
2095 | /* ...has no base classes of the same type as the first non-static |
2096 | data member... */ |
2097 | if (field && DECL_CONTEXT (field) == t |
2098 | && (same_type_ignoring_top_level_qualifiers_p |
2099 | (TREE_TYPE (field), basetype))) |
2100 | CLASSTYPE_NON_STD_LAYOUT (t) = 1; |
2101 | /* DR 1813: |
2102 | ...has at most one base class subobject of any given type... */ |
2103 | else if (CLASSTYPE_REPEATED_BASE_P (t)) |
2104 | CLASSTYPE_NON_STD_LAYOUT (t) = 1; |
2105 | else |
2106 | /* ...has all non-static data members and bit-fields in the class |
2107 | and its base classes first declared in the same class. */ |
2108 | for (basefield = TYPE_FIELDS (basetype); basefield; |
2109 | basefield = DECL_CHAIN (basefield)) |
2110 | if (TREE_CODE (basefield) == FIELD_DECL |
2111 | && !(DECL_FIELD_IS_BASE (basefield) |
2112 | && is_empty_field (basefield))) |
2113 | { |
2114 | if (field) |
2115 | CLASSTYPE_NON_STD_LAYOUT (t) = 1; |
2116 | else |
2117 | field = basefield; |
2118 | break; |
2119 | } |
2120 | } |
2121 | |
2122 | /* Don't bother collecting tm attributes if transactional memory |
2123 | support is not enabled. */ |
2124 | if (flag_tm) |
2125 | { |
2126 | tree tm_attr = find_tm_attribute (TYPE_ATTRIBUTES (basetype)); |
2127 | if (tm_attr) |
2128 | seen_tm_mask |= tm_attr_to_mask (tm_attr); |
2129 | } |
2130 | |
2131 | check_abi_tags (t, subob: basetype); |
2132 | } |
2133 | |
2134 | /* If one of the base classes had TM attributes, and the current class |
2135 | doesn't define its own, then the current class inherits one. */ |
2136 | if (seen_tm_mask && !find_tm_attribute (TYPE_ATTRIBUTES (t))) |
2137 | { |
2138 | tree tm_attr = tm_mask_to_attr (least_bit_hwi (x: seen_tm_mask)); |
2139 | TYPE_ATTRIBUTES (t) = tree_cons (tm_attr, NULL, TYPE_ATTRIBUTES (t)); |
2140 | } |
2141 | } |
2142 | |
2143 | /* Determine all the primary bases within T. Sets BINFO_PRIMARY_BASE_P for |
2144 | those that are primaries. Sets BINFO_LOST_PRIMARY_P for those |
2145 | that have had a nearly-empty virtual primary base stolen by some |
2146 | other base in the hierarchy. Determines CLASSTYPE_PRIMARY_BASE for |
2147 | T. */ |
2148 | |
2149 | static void |
2150 | determine_primary_bases (tree t) |
2151 | { |
2152 | unsigned i; |
2153 | tree primary = NULL_TREE; |
2154 | tree type_binfo = TYPE_BINFO (t); |
2155 | tree base_binfo; |
2156 | |
2157 | /* Determine the primary bases of our bases. */ |
2158 | for (base_binfo = TREE_CHAIN (type_binfo); base_binfo; |
2159 | base_binfo = TREE_CHAIN (base_binfo)) |
2160 | { |
2161 | tree primary = CLASSTYPE_PRIMARY_BINFO (BINFO_TYPE (base_binfo)); |
2162 | |
2163 | /* See if we're the non-virtual primary of our inheritance |
2164 | chain. */ |
2165 | if (!BINFO_VIRTUAL_P (base_binfo)) |
2166 | { |
2167 | tree parent = BINFO_INHERITANCE_CHAIN (base_binfo); |
2168 | tree parent_primary = CLASSTYPE_PRIMARY_BINFO (BINFO_TYPE (parent)); |
2169 | |
2170 | if (parent_primary |
2171 | && SAME_BINFO_TYPE_P (BINFO_TYPE (base_binfo), |
2172 | BINFO_TYPE (parent_primary))) |
2173 | /* We are the primary binfo. */ |
2174 | BINFO_PRIMARY_P (base_binfo) = 1; |
2175 | } |
2176 | /* Determine if we have a virtual primary base, and mark it so. |
2177 | */ |
2178 | if (primary && BINFO_VIRTUAL_P (primary)) |
2179 | { |
2180 | tree this_primary = copied_binfo (primary, base_binfo); |
2181 | |
2182 | if (BINFO_PRIMARY_P (this_primary)) |
2183 | /* Someone already claimed this base. */ |
2184 | BINFO_LOST_PRIMARY_P (base_binfo) = 1; |
2185 | else |
2186 | { |
2187 | tree delta; |
2188 | |
2189 | BINFO_PRIMARY_P (this_primary) = 1; |
2190 | BINFO_INHERITANCE_CHAIN (this_primary) = base_binfo; |
2191 | |
2192 | /* A virtual binfo might have been copied from within |
2193 | another hierarchy. As we're about to use it as a |
2194 | primary base, make sure the offsets match. */ |
2195 | delta = size_diffop_loc (input_location, |
2196 | fold_convert (ssizetype, |
2197 | BINFO_OFFSET (base_binfo)), |
2198 | fold_convert (ssizetype, |
2199 | BINFO_OFFSET (this_primary))); |
2200 | |
2201 | propagate_binfo_offsets (this_primary, delta); |
2202 | } |
2203 | } |
2204 | } |
2205 | |
2206 | /* First look for a dynamic direct non-virtual base. */ |
2207 | for (i = 0; BINFO_BASE_ITERATE (type_binfo, i, base_binfo); i++) |
2208 | { |
2209 | tree basetype = BINFO_TYPE (base_binfo); |
2210 | |
2211 | if (TYPE_CONTAINS_VPTR_P (basetype) && !BINFO_VIRTUAL_P (base_binfo)) |
2212 | { |
2213 | primary = base_binfo; |
2214 | goto found; |
2215 | } |
2216 | } |
2217 | |
2218 | /* A "nearly-empty" virtual base class can be the primary base |
2219 | class, if no non-virtual polymorphic base can be found. Look for |
2220 | a nearly-empty virtual dynamic base that is not already a primary |
2221 | base of something in the hierarchy. If there is no such base, |
2222 | just pick the first nearly-empty virtual base. */ |
2223 | |
2224 | for (base_binfo = TREE_CHAIN (type_binfo); base_binfo; |
2225 | base_binfo = TREE_CHAIN (base_binfo)) |
2226 | if (BINFO_VIRTUAL_P (base_binfo) |
2227 | && CLASSTYPE_NEARLY_EMPTY_P (BINFO_TYPE (base_binfo))) |
2228 | { |
2229 | if (!BINFO_PRIMARY_P (base_binfo)) |
2230 | { |
2231 | /* Found one that is not primary. */ |
2232 | primary = base_binfo; |
2233 | goto found; |
2234 | } |
2235 | else if (!primary) |
2236 | /* Remember the first candidate. */ |
2237 | primary = base_binfo; |
2238 | } |
2239 | |
2240 | found: |
2241 | /* If we've got a primary base, use it. */ |
2242 | if (primary) |
2243 | { |
2244 | tree basetype = BINFO_TYPE (primary); |
2245 | |
2246 | CLASSTYPE_PRIMARY_BINFO (t) = primary; |
2247 | if (BINFO_PRIMARY_P (primary)) |
2248 | /* We are stealing a primary base. */ |
2249 | BINFO_LOST_PRIMARY_P (BINFO_INHERITANCE_CHAIN (primary)) = 1; |
2250 | BINFO_PRIMARY_P (primary) = 1; |
2251 | if (BINFO_VIRTUAL_P (primary)) |
2252 | { |
2253 | tree delta; |
2254 | |
2255 | BINFO_INHERITANCE_CHAIN (primary) = type_binfo; |
2256 | /* A virtual binfo might have been copied from within |
2257 | another hierarchy. As we're about to use it as a primary |
2258 | base, make sure the offsets match. */ |
2259 | delta = size_diffop_loc (input_location, ssize_int (0), |
2260 | fold_convert (ssizetype, BINFO_OFFSET (primary))); |
2261 | |
2262 | propagate_binfo_offsets (primary, delta); |
2263 | } |
2264 | |
2265 | primary = TYPE_BINFO (basetype); |
2266 | |
2267 | TYPE_VFIELD (t) = TYPE_VFIELD (basetype); |
2268 | BINFO_VTABLE (type_binfo) = BINFO_VTABLE (primary); |
2269 | BINFO_VIRTUALS (type_binfo) = BINFO_VIRTUALS (primary); |
2270 | } |
2271 | } |
2272 | |
2273 | /* Update the variant types of T. */ |
2274 | |
2275 | void |
2276 | fixup_type_variants (tree type) |
2277 | { |
2278 | if (!type) |
2279 | return; |
2280 | |
2281 | for (tree variant = TYPE_NEXT_VARIANT (type); |
2282 | variant; |
2283 | variant = TYPE_NEXT_VARIANT (variant)) |
2284 | { |
2285 | /* These fields are in the _TYPE part of the node, not in |
2286 | the TYPE_LANG_SPECIFIC component, so they are not shared. */ |
2287 | TYPE_HAS_USER_CONSTRUCTOR (variant) = TYPE_HAS_USER_CONSTRUCTOR (type); |
2288 | TYPE_NEEDS_CONSTRUCTING (variant) = TYPE_NEEDS_CONSTRUCTING (type); |
2289 | TYPE_HAS_NONTRIVIAL_DESTRUCTOR (variant) |
2290 | = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type); |
2291 | |
2292 | TYPE_POLYMORPHIC_P (variant) = TYPE_POLYMORPHIC_P (type); |
2293 | CLASSTYPE_FINAL (variant) = CLASSTYPE_FINAL (type); |
2294 | |
2295 | TYPE_BINFO (variant) = TYPE_BINFO (type); |
2296 | |
2297 | /* Copy whatever these are holding today. */ |
2298 | TYPE_VFIELD (variant) = TYPE_VFIELD (type); |
2299 | TYPE_FIELDS (variant) = TYPE_FIELDS (type); |
2300 | |
2301 | TYPE_SIZE (variant) = TYPE_SIZE (type); |
2302 | TYPE_SIZE_UNIT (variant) = TYPE_SIZE_UNIT (type); |
2303 | |
2304 | if (!TYPE_USER_ALIGN (variant) |
2305 | || TYPE_NAME (variant) == TYPE_NAME (type) |
2306 | || TYPE_ALIGN_RAW (variant) < TYPE_ALIGN_RAW (type)) |
2307 | { |
2308 | TYPE_ALIGN_RAW (variant) = TYPE_ALIGN_RAW (type); |
2309 | TYPE_USER_ALIGN (variant) = TYPE_USER_ALIGN (type); |
2310 | } |
2311 | |
2312 | TYPE_PRECISION (variant) = TYPE_PRECISION (type); |
2313 | TYPE_MODE_RAW (variant) = TYPE_MODE_RAW (type); |
2314 | TYPE_EMPTY_P (variant) = TYPE_EMPTY_P (type); |
2315 | } |
2316 | } |
2317 | |
2318 | /* KLASS is a class that we're applying may_alias to after the body is |
2319 | parsed. Fixup any POINTER_TO and REFERENCE_TO types. The |
2320 | canonical type(s) will be implicitly updated. */ |
2321 | |
2322 | static void |
2323 | fixup_may_alias (tree klass) |
2324 | { |
2325 | tree t, v; |
2326 | |
2327 | for (t = TYPE_POINTER_TO (klass); t; t = TYPE_NEXT_PTR_TO (t)) |
2328 | for (v = TYPE_MAIN_VARIANT (t); v; v = TYPE_NEXT_VARIANT (v)) |
2329 | TYPE_REF_CAN_ALIAS_ALL (v) = true; |
2330 | for (t = TYPE_REFERENCE_TO (klass); t; t = TYPE_NEXT_REF_TO (t)) |
2331 | for (v = TYPE_MAIN_VARIANT (t); v; v = TYPE_NEXT_VARIANT (v)) |
2332 | TYPE_REF_CAN_ALIAS_ALL (v) = true; |
2333 | } |
2334 | |
2335 | /* Early variant fixups: we apply attributes at the beginning of the class |
2336 | definition, and we need to fix up any variants that have already been |
2337 | made via elaborated-type-specifier so that check_qualified_type works. */ |
2338 | |
2339 | void |
2340 | fixup_attribute_variants (tree t) |
2341 | { |
2342 | tree variants; |
2343 | |
2344 | if (!t) |
2345 | return; |
2346 | |
2347 | tree attrs = TYPE_ATTRIBUTES (t); |
2348 | unsigned align = TYPE_ALIGN (t); |
2349 | bool user_align = TYPE_USER_ALIGN (t); |
2350 | bool may_alias = lookup_attribute (attr_name: "may_alias" , list: attrs); |
2351 | bool packed = TYPE_PACKED (t); |
2352 | |
2353 | if (may_alias) |
2354 | fixup_may_alias (klass: t); |
2355 | |
2356 | for (variants = TYPE_NEXT_VARIANT (t); |
2357 | variants; |
2358 | variants = TYPE_NEXT_VARIANT (variants)) |
2359 | { |
2360 | /* These are the two fields that check_qualified_type looks at and |
2361 | are affected by attributes. */ |
2362 | TYPE_ATTRIBUTES (variants) = attrs; |
2363 | unsigned valign = align; |
2364 | if (TYPE_USER_ALIGN (variants)) |
2365 | valign = MAX (valign, TYPE_ALIGN (variants)); |
2366 | else |
2367 | TYPE_USER_ALIGN (variants) = user_align; |
2368 | SET_TYPE_ALIGN (variants, valign); |
2369 | TYPE_PACKED (variants) = packed; |
2370 | if (may_alias) |
2371 | fixup_may_alias (klass: variants); |
2372 | } |
2373 | } |
2374 | |
2375 | /* Set memoizing fields and bits of T (and its variants) for later |
2376 | use. */ |
2377 | |
2378 | static void |
2379 | finish_struct_bits (tree t) |
2380 | { |
2381 | /* Fix up variants (if any). */ |
2382 | fixup_type_variants (type: t); |
2383 | |
2384 | if (BINFO_N_BASE_BINFOS (TYPE_BINFO (t)) && TYPE_POLYMORPHIC_P (t)) |
2385 | /* For a class w/o baseclasses, 'finish_struct' has set |
2386 | CLASSTYPE_PURE_VIRTUALS correctly (by definition). |
2387 | Similarly for a class whose base classes do not have vtables. |
2388 | When neither of these is true, we might have removed abstract |
2389 | virtuals (by providing a definition), added some (by declaring |
2390 | new ones), or redeclared ones from a base class. We need to |
2391 | recalculate what's really an abstract virtual at this point (by |
2392 | looking in the vtables). */ |
2393 | get_pure_virtuals (t); |
2394 | |
2395 | /* If this type has a copy constructor or a destructor, force its |
2396 | mode to be BLKmode, and force its TREE_ADDRESSABLE bit to be |
2397 | nonzero. This will cause it to be passed by invisible reference |
2398 | and prevent it from being returned in a register. */ |
2399 | if (type_has_nontrivial_copy_init (t) |
2400 | || TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t)) |
2401 | { |
2402 | tree variants; |
2403 | SET_DECL_MODE (TYPE_MAIN_DECL (t), BLKmode); |
2404 | for (variants = t; variants; variants = TYPE_NEXT_VARIANT (variants)) |
2405 | { |
2406 | SET_TYPE_MODE (variants, BLKmode); |
2407 | TREE_ADDRESSABLE (variants) = 1; |
2408 | } |
2409 | } |
2410 | } |
2411 | |
2412 | /* Issue warnings about T having private constructors, but no friends, |
2413 | and so forth. |
2414 | |
2415 | HAS_NONPRIVATE_METHOD is nonzero if T has any non-private methods or |
2416 | static members. HAS_NONPRIVATE_STATIC_FN is nonzero if T has any |
2417 | non-private static member functions. */ |
2418 | |
2419 | static void |
2420 | maybe_warn_about_overly_private_class (tree t) |
2421 | { |
2422 | int has_member_fn = 0; |
2423 | int has_nonprivate_method = 0; |
2424 | bool nonprivate_ctor = false; |
2425 | |
2426 | if (!warn_ctor_dtor_privacy |
2427 | /* If the class has friends, those entities might create and |
2428 | access instances, so we should not warn. */ |
2429 | || (CLASSTYPE_FRIEND_CLASSES (t) |
2430 | || DECL_FRIENDLIST (TYPE_MAIN_DECL (t))) |
2431 | /* We will have warned when the template was declared; there's |
2432 | no need to warn on every instantiation. */ |
2433 | || CLASSTYPE_TEMPLATE_INSTANTIATION (t)) |
2434 | /* There's no reason to even consider warning about this |
2435 | class. */ |
2436 | return; |
2437 | |
2438 | /* We only issue one warning, if more than one applies, because |
2439 | otherwise, on code like: |
2440 | |
2441 | class A { |
2442 | // Oops - forgot `public:' |
2443 | A(); |
2444 | A(const A&); |
2445 | ~A(); |
2446 | }; |
2447 | |
2448 | we warn several times about essentially the same problem. */ |
2449 | |
2450 | /* Check to see if all (non-constructor, non-destructor) member |
2451 | functions are private. (Since there are no friends or |
2452 | non-private statics, we can't ever call any of the private member |
2453 | functions.) */ |
2454 | for (tree fn = TYPE_FIELDS (t); fn; fn = DECL_CHAIN (fn)) |
2455 | if (TREE_CODE (fn) == USING_DECL |
2456 | && DECL_NAME (fn) == ctor_identifier |
2457 | && !TREE_PRIVATE (fn)) |
2458 | nonprivate_ctor = true; |
2459 | else if (!DECL_DECLARES_FUNCTION_P (fn)) |
2460 | /* Not a function. */; |
2461 | else if (DECL_ARTIFICIAL (fn)) |
2462 | /* We're not interested in compiler-generated methods; they don't |
2463 | provide any way to call private members. */; |
2464 | else if (!TREE_PRIVATE (fn)) |
2465 | { |
2466 | if (DECL_STATIC_FUNCTION_P (fn)) |
2467 | /* A non-private static member function is just like a |
2468 | friend; it can create and invoke private member |
2469 | functions, and be accessed without a class |
2470 | instance. */ |
2471 | return; |
2472 | |
2473 | has_nonprivate_method = 1; |
2474 | /* Keep searching for a static member function. */ |
2475 | } |
2476 | else if (!DECL_CONSTRUCTOR_P (fn) && !DECL_DESTRUCTOR_P (fn)) |
2477 | has_member_fn = 1; |
2478 | |
2479 | if (!has_nonprivate_method && has_member_fn) |
2480 | { |
2481 | /* There are no non-private methods, and there's at least one |
2482 | private member function that isn't a constructor or |
2483 | destructor. (If all the private members are |
2484 | constructors/destructors we want to use the code below that |
2485 | issues error messages specifically referring to |
2486 | constructors/destructors.) */ |
2487 | unsigned i; |
2488 | tree binfo = TYPE_BINFO (t); |
2489 | |
2490 | for (i = 0; i != BINFO_N_BASE_BINFOS (binfo); i++) |
2491 | if (BINFO_BASE_ACCESS (binfo, i) != access_private_node) |
2492 | { |
2493 | has_nonprivate_method = 1; |
2494 | break; |
2495 | } |
2496 | if (!has_nonprivate_method) |
2497 | { |
2498 | warning (OPT_Wctor_dtor_privacy, |
2499 | "all member functions in class %qT are private" , t); |
2500 | return; |
2501 | } |
2502 | } |
2503 | |
2504 | /* Even if some of the member functions are non-private, the class |
2505 | won't be useful for much if all the constructors or destructors |
2506 | are private: such an object can never be created or destroyed. */ |
2507 | if (tree dtor = CLASSTYPE_DESTRUCTOR (t)) |
2508 | if (TREE_PRIVATE (dtor)) |
2509 | { |
2510 | warning (OPT_Wctor_dtor_privacy, |
2511 | "%q#T only defines a private destructor and has no friends" , |
2512 | t); |
2513 | return; |
2514 | } |
2515 | |
2516 | /* Warn about classes that have private constructors and no friends. */ |
2517 | if (TYPE_HAS_USER_CONSTRUCTOR (t) |
2518 | /* Implicitly generated constructors are always public. */ |
2519 | && !CLASSTYPE_LAZY_DEFAULT_CTOR (t)) |
2520 | { |
2521 | tree copy_or_move = NULL_TREE; |
2522 | |
2523 | /* If a non-template class does not define a copy |
2524 | constructor, one is defined for it, enabling it to avoid |
2525 | this warning. For a template class, this does not |
2526 | happen, and so we would normally get a warning on: |
2527 | |
2528 | template <class T> class C { private: C(); }; |
2529 | |
2530 | To avoid this asymmetry, we check TYPE_HAS_COPY_CTOR. All |
2531 | complete non-template or fully instantiated classes have this |
2532 | flag set. */ |
2533 | if (!TYPE_HAS_COPY_CTOR (t)) |
2534 | nonprivate_ctor = true; |
2535 | else |
2536 | for (tree fn : ovl_range (CLASSTYPE_CONSTRUCTORS (t))) |
2537 | if (TREE_PRIVATE (fn)) |
2538 | continue; |
2539 | else if (copy_fn_p (fn) || move_fn_p (fn)) |
2540 | /* Ideally, we wouldn't count any constructor that takes |
2541 | an argument of the class type as a parameter, because |
2542 | such things cannot be used to construct an instance of |
2543 | the class unless you already have one. */ |
2544 | copy_or_move = fn; |
2545 | else |
2546 | { |
2547 | nonprivate_ctor = true; |
2548 | break; |
2549 | } |
2550 | |
2551 | if (!nonprivate_ctor) |
2552 | { |
2553 | bool w = warning (OPT_Wctor_dtor_privacy, |
2554 | "%q#T only defines private constructors and has " |
2555 | "no friends" , t); |
2556 | if (w && copy_or_move) |
2557 | inform (DECL_SOURCE_LOCATION (copy_or_move), |
2558 | "%q#D is public, but requires an existing %q#T object" , |
2559 | copy_or_move, t); |
2560 | return; |
2561 | } |
2562 | } |
2563 | } |
2564 | |
2565 | /* Make BINFO's vtable have N entries, including RTTI entries, |
2566 | vbase and vcall offsets, etc. Set its type and call the back end |
2567 | to lay it out. */ |
2568 | |
2569 | static void |
2570 | layout_vtable_decl (tree binfo, int n) |
2571 | { |
2572 | tree atype; |
2573 | tree vtable; |
2574 | |
2575 | atype = build_array_of_n_type (vtable_entry_type, n); |
2576 | layout_type (atype); |
2577 | |
2578 | /* We may have to grow the vtable. */ |
2579 | vtable = get_vtbl_decl_for_binfo (binfo); |
2580 | if (!same_type_p (TREE_TYPE (vtable), atype)) |
2581 | { |
2582 | TREE_TYPE (vtable) = atype; |
2583 | DECL_SIZE (vtable) = DECL_SIZE_UNIT (vtable) = NULL_TREE; |
2584 | layout_decl (vtable, 0); |
2585 | } |
2586 | } |
2587 | |
2588 | /* True iff FNDECL and BASE_FNDECL (both non-static member functions) |
2589 | have the same signature. */ |
2590 | |
2591 | int |
2592 | same_signature_p (const_tree fndecl, const_tree base_fndecl) |
2593 | { |
2594 | /* One destructor overrides another if they are the same kind of |
2595 | destructor. */ |
2596 | if (DECL_DESTRUCTOR_P (base_fndecl) && DECL_DESTRUCTOR_P (fndecl) |
2597 | && special_function_p (base_fndecl) == special_function_p (fndecl)) |
2598 | return 1; |
2599 | /* But a non-destructor never overrides a destructor, nor vice |
2600 | versa, nor do different kinds of destructors override |
2601 | one-another. For example, a complete object destructor does not |
2602 | override a deleting destructor. */ |
2603 | if (DECL_DESTRUCTOR_P (base_fndecl) || DECL_DESTRUCTOR_P (fndecl)) |
2604 | return 0; |
2605 | |
2606 | if (DECL_NAME (fndecl) == DECL_NAME (base_fndecl) |
2607 | || (DECL_CONV_FN_P (fndecl) |
2608 | && DECL_CONV_FN_P (base_fndecl) |
2609 | && same_type_p (DECL_CONV_FN_TYPE (fndecl), |
2610 | DECL_CONV_FN_TYPE (base_fndecl)))) |
2611 | { |
2612 | tree fntype = TREE_TYPE (fndecl); |
2613 | tree base_fntype = TREE_TYPE (base_fndecl); |
2614 | if (type_memfn_quals (fntype) == type_memfn_quals (base_fntype) |
2615 | && type_memfn_rqual (fntype) == type_memfn_rqual (base_fntype) |
2616 | && compparms (FUNCTION_FIRST_USER_PARMTYPE (fndecl), |
2617 | FUNCTION_FIRST_USER_PARMTYPE (base_fndecl))) |
2618 | return 1; |
2619 | } |
2620 | return 0; |
2621 | } |
2622 | |
2623 | /* Returns TRUE if DERIVED is a binfo containing the binfo BASE as a |
2624 | subobject. */ |
2625 | |
2626 | static bool |
2627 | base_derived_from (tree derived, tree base) |
2628 | { |
2629 | tree probe; |
2630 | |
2631 | for (probe = base; probe; probe = BINFO_INHERITANCE_CHAIN (probe)) |
2632 | { |
2633 | if (probe == derived) |
2634 | return true; |
2635 | else if (BINFO_VIRTUAL_P (probe)) |
2636 | /* If we meet a virtual base, we can't follow the inheritance |
2637 | any more. See if the complete type of DERIVED contains |
2638 | such a virtual base. */ |
2639 | return (binfo_for_vbase (BINFO_TYPE (probe), BINFO_TYPE (derived)) |
2640 | != NULL_TREE); |
2641 | } |
2642 | return false; |
2643 | } |
2644 | |
2645 | struct find_final_overrider_data { |
2646 | /* The function for which we are trying to find a final overrider. */ |
2647 | tree fn; |
2648 | /* The base class in which the function was declared. */ |
2649 | tree declaring_base; |
2650 | /* The candidate overriders. */ |
2651 | tree candidates; |
2652 | /* Path to most derived. */ |
2653 | auto_vec<tree> path; |
2654 | }; |
2655 | |
2656 | /* Add the overrider along the current path to FFOD->CANDIDATES. |
2657 | Returns true if an overrider was found; false otherwise. */ |
2658 | |
2659 | static bool |
2660 | dfs_find_final_overrider_1 (tree binfo, |
2661 | find_final_overrider_data *ffod, |
2662 | unsigned depth) |
2663 | { |
2664 | tree method; |
2665 | |
2666 | /* If BINFO is not the most derived type, try a more derived class. |
2667 | A definition there will overrider a definition here. */ |
2668 | if (depth) |
2669 | { |
2670 | depth--; |
2671 | if (dfs_find_final_overrider_1 |
2672 | (binfo: ffod->path[depth], ffod, depth)) |
2673 | return true; |
2674 | } |
2675 | |
2676 | method = look_for_overrides_here (BINFO_TYPE (binfo), ffod->fn); |
2677 | if (method) |
2678 | { |
2679 | tree *candidate = &ffod->candidates; |
2680 | |
2681 | /* Remove any candidates overridden by this new function. */ |
2682 | while (*candidate) |
2683 | { |
2684 | /* If *CANDIDATE overrides METHOD, then METHOD |
2685 | cannot override anything else on the list. */ |
2686 | if (base_derived_from (TREE_VALUE (*candidate), base: binfo)) |
2687 | return true; |
2688 | /* If METHOD overrides *CANDIDATE, remove *CANDIDATE. */ |
2689 | if (base_derived_from (derived: binfo, TREE_VALUE (*candidate))) |
2690 | *candidate = TREE_CHAIN (*candidate); |
2691 | else |
2692 | candidate = &TREE_CHAIN (*candidate); |
2693 | } |
2694 | |
2695 | /* Add the new function. */ |
2696 | ffod->candidates = tree_cons (method, binfo, ffod->candidates); |
2697 | return true; |
2698 | } |
2699 | |
2700 | return false; |
2701 | } |
2702 | |
2703 | /* Called from find_final_overrider via dfs_walk. */ |
2704 | |
2705 | static tree |
2706 | dfs_find_final_overrider_pre (tree binfo, void *data) |
2707 | { |
2708 | find_final_overrider_data *ffod = (find_final_overrider_data *) data; |
2709 | |
2710 | if (binfo == ffod->declaring_base) |
2711 | dfs_find_final_overrider_1 (binfo, ffod, depth: ffod->path.length ()); |
2712 | ffod->path.safe_push (obj: binfo); |
2713 | |
2714 | return NULL_TREE; |
2715 | } |
2716 | |
2717 | static tree |
2718 | dfs_find_final_overrider_post (tree /*binfo*/, void *data) |
2719 | { |
2720 | find_final_overrider_data *ffod = (find_final_overrider_data *) data; |
2721 | ffod->path.pop (); |
2722 | |
2723 | return NULL_TREE; |
2724 | } |
2725 | |
2726 | /* Returns a TREE_LIST whose TREE_PURPOSE is the final overrider for |
2727 | FN and whose TREE_VALUE is the binfo for the base where the |
2728 | overriding occurs. BINFO (in the hierarchy dominated by the binfo |
2729 | DERIVED) is the base object in which FN is declared. */ |
2730 | |
2731 | static tree |
2732 | find_final_overrider (tree derived, tree binfo, tree fn) |
2733 | { |
2734 | find_final_overrider_data ffod; |
2735 | |
2736 | /* Getting this right is a little tricky. This is valid: |
2737 | |
2738 | struct S { virtual void f (); }; |
2739 | struct T { virtual void f (); }; |
2740 | struct U : public S, public T { }; |
2741 | |
2742 | even though calling `f' in `U' is ambiguous. But, |
2743 | |
2744 | struct R { virtual void f(); }; |
2745 | struct S : virtual public R { virtual void f (); }; |
2746 | struct T : virtual public R { virtual void f (); }; |
2747 | struct U : public S, public T { }; |
2748 | |
2749 | is not -- there's no way to decide whether to put `S::f' or |
2750 | `T::f' in the vtable for `R'. |
2751 | |
2752 | The solution is to look at all paths to BINFO. If we find |
2753 | different overriders along any two, then there is a problem. */ |
2754 | if (DECL_THUNK_P (fn)) |
2755 | fn = THUNK_TARGET (fn); |
2756 | |
2757 | /* Determine the depth of the hierarchy. */ |
2758 | ffod.fn = fn; |
2759 | ffod.declaring_base = binfo; |
2760 | ffod.candidates = NULL_TREE; |
2761 | ffod.path.create (nelems: 30); |
2762 | |
2763 | dfs_walk_all (derived, dfs_find_final_overrider_pre, |
2764 | dfs_find_final_overrider_post, &ffod); |
2765 | |
2766 | /* If there was no winner, issue an error message. */ |
2767 | if (!ffod.candidates || TREE_CHAIN (ffod.candidates)) |
2768 | return error_mark_node; |
2769 | |
2770 | return ffod.candidates; |
2771 | } |
2772 | |
2773 | /* Return the index of the vcall offset for FN when TYPE is used as a |
2774 | virtual base. */ |
2775 | |
2776 | static tree |
2777 | get_vcall_index (tree fn, tree type) |
2778 | { |
2779 | vec<tree_pair_s, va_gc> *indices = CLASSTYPE_VCALL_INDICES (type); |
2780 | tree_pair_p p; |
2781 | unsigned ix; |
2782 | |
2783 | FOR_EACH_VEC_SAFE_ELT (indices, ix, p) |
2784 | if ((DECL_DESTRUCTOR_P (fn) && DECL_DESTRUCTOR_P (p->purpose)) |
2785 | || same_signature_p (fndecl: fn, base_fndecl: p->purpose)) |
2786 | return p->value; |
2787 | |
2788 | /* There should always be an appropriate index. */ |
2789 | gcc_unreachable (); |
2790 | } |
2791 | |
2792 | /* Given a DECL_VINDEX of a virtual function found in BINFO, return the final |
2793 | overrider at that index in the vtable. This should only be used when we |
2794 | know that BINFO is correct for the dynamic type of the object. */ |
2795 | |
2796 | tree |
2797 | lookup_vfn_in_binfo (tree idx, tree binfo) |
2798 | { |
2799 | int ix = tree_to_shwi (idx); |
2800 | if (TARGET_VTABLE_USES_DESCRIPTORS) |
2801 | ix /= MAX (TARGET_VTABLE_USES_DESCRIPTORS, 1); |
2802 | while (BINFO_PRIMARY_P (binfo)) |
2803 | /* BINFO_VIRTUALS in a primary base isn't accurate, find the derived |
2804 | class that actually owns the vtable. */ |
2805 | binfo = BINFO_INHERITANCE_CHAIN (binfo); |
2806 | tree virtuals = BINFO_VIRTUALS (binfo); |
2807 | return TREE_VALUE (chain_index (ix, virtuals)); |
2808 | } |
2809 | |
2810 | /* Update an entry in the vtable for BINFO, which is in the hierarchy |
2811 | dominated by T. FN is the old function; VIRTUALS points to the |
2812 | corresponding position in the new BINFO_VIRTUALS list. IX is the index |
2813 | of that entry in the list. */ |
2814 | |
2815 | static void |
2816 | update_vtable_entry_for_fn (tree t, tree binfo, tree fn, tree* virtuals, |
2817 | unsigned ix) |
2818 | { |
2819 | tree b; |
2820 | tree overrider; |
2821 | tree delta; |
2822 | tree virtual_base; |
2823 | tree first_defn; |
2824 | tree overrider_fn, overrider_target; |
2825 | tree target_fn = DECL_THUNK_P (fn) ? THUNK_TARGET (fn) : fn; |
2826 | tree over_return, base_return; |
2827 | bool lost = false; |
2828 | |
2829 | /* Find the nearest primary base (possibly binfo itself) which defines |
2830 | this function; this is the class the caller will convert to when |
2831 | calling FN through BINFO. */ |
2832 | for (b = binfo; ; b = get_primary_binfo (b)) |
2833 | { |
2834 | gcc_assert (b); |
2835 | if (look_for_overrides_here (BINFO_TYPE (b), target_fn)) |
2836 | break; |
2837 | |
2838 | /* The nearest definition is from a lost primary. */ |
2839 | if (BINFO_LOST_PRIMARY_P (b)) |
2840 | lost = true; |
2841 | } |
2842 | first_defn = b; |
2843 | |
2844 | /* Find the final overrider. */ |
2845 | overrider = find_final_overrider (TYPE_BINFO (t), binfo: b, fn: target_fn); |
2846 | if (overrider == error_mark_node) |
2847 | { |
2848 | error ("no unique final overrider for %qD in %qT" , target_fn, t); |
2849 | return; |
2850 | } |
2851 | overrider_target = overrider_fn = TREE_PURPOSE (overrider); |
2852 | |
2853 | /* Check for adjusting covariant return types. */ |
2854 | over_return = TREE_TYPE (TREE_TYPE (overrider_target)); |
2855 | base_return = TREE_TYPE (TREE_TYPE (target_fn)); |
2856 | |
2857 | if (INDIRECT_TYPE_P (over_return) |
2858 | && TREE_CODE (over_return) == TREE_CODE (base_return) |
2859 | && CLASS_TYPE_P (TREE_TYPE (over_return)) |
2860 | && CLASS_TYPE_P (TREE_TYPE (base_return)) |
2861 | /* If the overrider is invalid, don't even try. */ |
2862 | && !DECL_INVALID_OVERRIDER_P (overrider_target)) |
2863 | { |
2864 | /* If FN is a covariant thunk, we must figure out the adjustment |
2865 | to the final base FN was converting to. As OVERRIDER_TARGET might |
2866 | also be converting to the return type of FN, we have to |
2867 | combine the two conversions here. */ |
2868 | tree fixed_offset, virtual_offset; |
2869 | |
2870 | over_return = TREE_TYPE (over_return); |
2871 | base_return = TREE_TYPE (base_return); |
2872 | |
2873 | if (DECL_THUNK_P (fn)) |
2874 | { |
2875 | gcc_assert (DECL_RESULT_THUNK_P (fn)); |
2876 | fixed_offset = ssize_int (THUNK_FIXED_OFFSET (fn)); |
2877 | virtual_offset = THUNK_VIRTUAL_OFFSET (fn); |
2878 | } |
2879 | else |
2880 | fixed_offset = virtual_offset = NULL_TREE; |
2881 | |
2882 | if (virtual_offset) |
2883 | /* Find the equivalent binfo within the return type of the |
2884 | overriding function. We will want the vbase offset from |
2885 | there. */ |
2886 | virtual_offset = binfo_for_vbase (BINFO_TYPE (virtual_offset), |
2887 | over_return); |
2888 | else if (!same_type_ignoring_top_level_qualifiers_p |
2889 | (over_return, base_return)) |
2890 | { |
2891 | /* There was no existing virtual thunk (which takes |
2892 | precedence). So find the binfo of the base function's |
2893 | return type within the overriding function's return type. |
2894 | Fortunately we know the covariancy is valid (it |
2895 | has already been checked), so we can just iterate along |
2896 | the binfos, which have been chained in inheritance graph |
2897 | order. Of course it is lame that we have to repeat the |
2898 | search here anyway -- we should really be caching pieces |
2899 | of the vtable and avoiding this repeated work. */ |
2900 | tree thunk_binfo = NULL_TREE; |
2901 | tree base_binfo = TYPE_BINFO (base_return); |
2902 | |
2903 | /* Find the base binfo within the overriding function's |
2904 | return type. We will always find a thunk_binfo, except |
2905 | when the covariancy is invalid (which we will have |
2906 | already diagnosed). */ |
2907 | if (base_binfo) |
2908 | for (thunk_binfo = TYPE_BINFO (over_return); thunk_binfo; |
2909 | thunk_binfo = TREE_CHAIN (thunk_binfo)) |
2910 | if (SAME_BINFO_TYPE_P (BINFO_TYPE (thunk_binfo), |
2911 | BINFO_TYPE (base_binfo))) |
2912 | break; |
2913 | gcc_assert (thunk_binfo || errorcount); |
2914 | |
2915 | /* See if virtual inheritance is involved. */ |
2916 | for (virtual_offset = thunk_binfo; |
2917 | virtual_offset; |
2918 | virtual_offset = BINFO_INHERITANCE_CHAIN (virtual_offset)) |
2919 | if (BINFO_VIRTUAL_P (virtual_offset)) |
2920 | break; |
2921 | |
2922 | if (virtual_offset |
2923 | || (thunk_binfo && !BINFO_OFFSET_ZEROP (thunk_binfo))) |
2924 | { |
2925 | tree offset = fold_convert (ssizetype, BINFO_OFFSET (thunk_binfo)); |
2926 | |
2927 | if (virtual_offset) |
2928 | { |
2929 | /* We convert via virtual base. Adjust the fixed |
2930 | offset to be from there. */ |
2931 | offset = |
2932 | size_diffop (offset, |
2933 | fold_convert (ssizetype, |
2934 | BINFO_OFFSET (virtual_offset))); |
2935 | } |
2936 | if (fixed_offset) |
2937 | /* There was an existing fixed offset, this must be |
2938 | from the base just converted to, and the base the |
2939 | FN was thunking to. */ |
2940 | fixed_offset = size_binop (PLUS_EXPR, fixed_offset, offset); |
2941 | else |
2942 | fixed_offset = offset; |
2943 | } |
2944 | } |
2945 | |
2946 | if (fixed_offset || virtual_offset) |
2947 | /* Replace the overriding function with a covariant thunk. We |
2948 | will emit the overriding function in its own slot as |
2949 | well. */ |
2950 | overrider_fn = make_thunk (overrider_target, /*this_adjusting=*/0, |
2951 | fixed_offset, virtual_offset); |
2952 | } |
2953 | else |
2954 | gcc_assert (DECL_INVALID_OVERRIDER_P (overrider_target) || |
2955 | !DECL_THUNK_P (fn)); |
2956 | |
2957 | /* If we need a covariant thunk, then we may need to adjust first_defn. |
2958 | The ABI specifies that the thunks emitted with a function are |
2959 | determined by which bases the function overrides, so we need to be |
2960 | sure that we're using a thunk for some overridden base; even if we |
2961 | know that the necessary this adjustment is zero, there may not be an |
2962 | appropriate zero-this-adjustment thunk for us to use since thunks for |
2963 | overriding virtual bases always use the vcall offset. |
2964 | |
2965 | Furthermore, just choosing any base that overrides this function isn't |
2966 | quite right, as this slot won't be used for calls through a type that |
2967 | puts a covariant thunk here. Calling the function through such a type |
2968 | will use a different slot, and that slot is the one that determines |
2969 | the thunk emitted for that base. |
2970 | |
2971 | So, keep looking until we find the base that we're really overriding |
2972 | in this slot: the nearest primary base that doesn't use a covariant |
2973 | thunk in this slot. */ |
2974 | if (overrider_target != overrider_fn) |
2975 | { |
2976 | if (BINFO_TYPE (b) == DECL_CONTEXT (overrider_target)) |
2977 | /* We already know that the overrider needs a covariant thunk. */ |
2978 | b = get_primary_binfo (b); |
2979 | for (; ; b = get_primary_binfo (b)) |
2980 | { |
2981 | tree main_binfo = TYPE_BINFO (BINFO_TYPE (b)); |
2982 | tree bv = chain_index (ix, BINFO_VIRTUALS (main_binfo)); |
2983 | if (!DECL_THUNK_P (TREE_VALUE (bv))) |
2984 | break; |
2985 | if (BINFO_LOST_PRIMARY_P (b)) |
2986 | lost = true; |
2987 | } |
2988 | first_defn = b; |
2989 | } |
2990 | |
2991 | /* Assume that we will produce a thunk that convert all the way to |
2992 | the final overrider, and not to an intermediate virtual base. */ |
2993 | virtual_base = NULL_TREE; |
2994 | |
2995 | /* See if we can convert to an intermediate virtual base first, and then |
2996 | use the vcall offset located there to finish the conversion. */ |
2997 | for (; b; b = BINFO_INHERITANCE_CHAIN (b)) |
2998 | { |
2999 | /* If we find the final overrider, then we can stop |
3000 | walking. */ |
3001 | if (SAME_BINFO_TYPE_P (BINFO_TYPE (b), |
3002 | BINFO_TYPE (TREE_VALUE (overrider)))) |
3003 | break; |
3004 | |
3005 | /* If we find a virtual base, and we haven't yet found the |
3006 | overrider, then there is a virtual base between the |
3007 | declaring base (first_defn) and the final overrider. */ |
3008 | if (BINFO_VIRTUAL_P (b)) |
3009 | { |
3010 | virtual_base = b; |
3011 | break; |
3012 | } |
3013 | } |
3014 | |
3015 | /* Compute the constant adjustment to the `this' pointer. The |
3016 | `this' pointer, when this function is called, will point at BINFO |
3017 | (or one of its primary bases, which are at the same offset). */ |
3018 | if (virtual_base) |
3019 | /* The `this' pointer needs to be adjusted from the declaration to |
3020 | the nearest virtual base. */ |
3021 | delta = size_diffop_loc (input_location, |
3022 | fold_convert (ssizetype, BINFO_OFFSET (virtual_base)), |
3023 | fold_convert (ssizetype, BINFO_OFFSET (first_defn))); |
3024 | else if (lost) |
3025 | /* If the nearest definition is in a lost primary, we don't need an |
3026 | entry in our vtable. Except possibly in a constructor vtable, |
3027 | if we happen to get our primary back. In that case, the offset |
3028 | will be zero, as it will be a primary base. */ |
3029 | delta = size_zero_node; |
3030 | else |
3031 | /* The `this' pointer needs to be adjusted from pointing to |
3032 | BINFO to pointing at the base where the final overrider |
3033 | appears. */ |
3034 | delta = size_diffop_loc (input_location, |
3035 | fold_convert (ssizetype, |
3036 | BINFO_OFFSET (TREE_VALUE (overrider))), |
3037 | fold_convert (ssizetype, BINFO_OFFSET (binfo))); |
3038 | |
3039 | modify_vtable_entry (t, binfo, fndecl: overrider_fn, delta, virtuals); |
3040 | |
3041 | if (virtual_base) |
3042 | BV_VCALL_INDEX (*virtuals) |
3043 | = get_vcall_index (fn: overrider_target, BINFO_TYPE (virtual_base)); |
3044 | else |
3045 | BV_VCALL_INDEX (*virtuals) = NULL_TREE; |
3046 | |
3047 | BV_LOST_PRIMARY (*virtuals) = lost; |
3048 | } |
3049 | |
3050 | /* Called from modify_all_vtables via dfs_walk. */ |
3051 | |
3052 | static tree |
3053 | dfs_modify_vtables (tree binfo, void* data) |
3054 | { |
3055 | tree t = (tree) data; |
3056 | tree virtuals; |
3057 | tree old_virtuals; |
3058 | unsigned ix; |
3059 | |
3060 | if (!TYPE_CONTAINS_VPTR_P (BINFO_TYPE (binfo))) |
3061 | /* A base without a vtable needs no modification, and its bases |
3062 | are uninteresting. */ |
3063 | return dfs_skip_bases; |
3064 | |
3065 | if (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), t) |
3066 | && !CLASSTYPE_HAS_PRIMARY_BASE_P (t)) |
3067 | /* Don't do the primary vtable, if it's new. */ |
3068 | return NULL_TREE; |
3069 | |
3070 | if (BINFO_PRIMARY_P (binfo) && !BINFO_VIRTUAL_P (binfo)) |
3071 | /* There's no need to modify the vtable for a non-virtual primary |
3072 | base; we're not going to use that vtable anyhow. We do still |
3073 | need to do this for virtual primary bases, as they could become |
3074 | non-primary in a construction vtable. */ |
3075 | return NULL_TREE; |
3076 | |
3077 | make_new_vtable (t, binfo); |
3078 | |
3079 | /* Now, go through each of the virtual functions in the virtual |
3080 | function table for BINFO. Find the final overrider, and update |
3081 | the BINFO_VIRTUALS list appropriately. */ |
3082 | for (ix = 0, virtuals = BINFO_VIRTUALS (binfo), |
3083 | old_virtuals = BINFO_VIRTUALS (TYPE_BINFO (BINFO_TYPE (binfo))); |
3084 | virtuals; |
3085 | ix++, virtuals = TREE_CHAIN (virtuals), |
3086 | old_virtuals = TREE_CHAIN (old_virtuals)) |
3087 | update_vtable_entry_for_fn (t, |
3088 | binfo, |
3089 | BV_FN (old_virtuals), |
3090 | virtuals: &virtuals, ix); |
3091 | |
3092 | return NULL_TREE; |
3093 | } |
3094 | |
3095 | /* Update all of the primary and secondary vtables for T. Create new |
3096 | vtables as required, and initialize their RTTI information. Each |
3097 | of the functions in VIRTUALS is declared in T and may override a |
3098 | virtual function from a base class; find and modify the appropriate |
3099 | entries to point to the overriding functions. Returns a list, in |
3100 | declaration order, of the virtual functions that are declared in T, |
3101 | but do not appear in the primary base class vtable, and which |
3102 | should therefore be appended to the end of the vtable for T. */ |
3103 | |
3104 | static tree |
3105 | modify_all_vtables (tree t, tree virtuals) |
3106 | { |
3107 | tree binfo = TYPE_BINFO (t); |
3108 | tree *fnsp; |
3109 | |
3110 | /* Mangle the vtable name before entering dfs_walk (c++/51884). */ |
3111 | if (TYPE_CONTAINS_VPTR_P (t)) |
3112 | get_vtable_decl (type: t, complete: false); |
3113 | |
3114 | /* Update all of the vtables. */ |
3115 | dfs_walk_once (binfo, dfs_modify_vtables, NULL, t); |
3116 | |
3117 | /* Add virtual functions not already in our primary vtable. These |
3118 | will be both those introduced by this class, and those overridden |
3119 | from secondary bases. It does not include virtuals merely |
3120 | inherited from secondary bases. */ |
3121 | for (fnsp = &virtuals; *fnsp; ) |
3122 | { |
3123 | tree fn = TREE_VALUE (*fnsp); |
3124 | |
3125 | if (!value_member (fn, BINFO_VIRTUALS (binfo)) |
3126 | || DECL_VINDEX (fn) == error_mark_node) |
3127 | { |
3128 | /* We don't need to adjust the `this' pointer when |
3129 | calling this function. */ |
3130 | BV_DELTA (*fnsp) = integer_zero_node; |
3131 | BV_VCALL_INDEX (*fnsp) = NULL_TREE; |
3132 | |
3133 | /* This is a function not already in our vtable. Keep it. */ |
3134 | fnsp = &TREE_CHAIN (*fnsp); |
3135 | } |
3136 | else |
3137 | /* We've already got an entry for this function. Skip it. */ |
3138 | *fnsp = TREE_CHAIN (*fnsp); |
3139 | } |
3140 | |
3141 | return virtuals; |
3142 | } |
3143 | |
3144 | /* Get the base virtual function declarations in T that have the |
3145 | indicated NAME. */ |
3146 | |
3147 | static void |
3148 | get_basefndecls (tree name, tree t, vec<tree> *base_fndecls) |
3149 | { |
3150 | bool found_decls = false; |
3151 | |
3152 | /* Find virtual functions in T with the indicated NAME. */ |
3153 | for (tree method : ovl_range (get_class_binding (t, name))) |
3154 | { |
3155 | if (TREE_CODE (method) == FUNCTION_DECL && DECL_VINDEX (method)) |
3156 | { |
3157 | base_fndecls->safe_push (obj: method); |
3158 | found_decls = true; |
3159 | } |
3160 | } |
3161 | |
3162 | if (found_decls) |
3163 | return; |
3164 | |
3165 | int n_baseclasses = BINFO_N_BASE_BINFOS (TYPE_BINFO (t)); |
3166 | for (int i = 0; i < n_baseclasses; i++) |
3167 | { |
3168 | tree basetype = BINFO_TYPE (BINFO_BASE_BINFO (TYPE_BINFO (t), i)); |
3169 | get_basefndecls (name, t: basetype, base_fndecls); |
3170 | } |
3171 | } |
3172 | |
3173 | /* If this method overrides a virtual method from a base, then mark |
3174 | this member function as being virtual as well. Do 'final' and |
3175 | 'override' checks too. */ |
3176 | |
3177 | void |
3178 | check_for_override (tree decl, tree ctype) |
3179 | { |
3180 | if (TREE_CODE (decl) == TEMPLATE_DECL) |
3181 | /* In [temp.mem] we have: |
3182 | |
3183 | A specialization of a member function template does not |
3184 | override a virtual function from a base class. */ |
3185 | return; |
3186 | |
3187 | /* IDENTIFIER_VIRTUAL_P indicates whether the name has ever been |
3188 | used for a vfunc. That avoids the expensive look_for_overrides |
3189 | call that when we know there's nothing to find. As conversion |
3190 | operators for the same type can have distinct identifiers, we |
3191 | cannot optimize those in that way. */ |
3192 | if ((IDENTIFIER_VIRTUAL_P (DECL_NAME (decl)) |
3193 | || DECL_CONV_FN_P (decl)) |
3194 | && look_for_overrides (ctype, decl) |
3195 | /* Check staticness after we've checked if we 'override'. */ |
3196 | && !DECL_STATIC_FUNCTION_P (decl)) |
3197 | { |
3198 | /* Set DECL_VINDEX to a value that is neither an INTEGER_CST nor |
3199 | the error_mark_node so that we know it is an overriding |
3200 | function. */ |
3201 | DECL_VINDEX (decl) = decl; |
3202 | |
3203 | if (warn_override |
3204 | && !DECL_OVERRIDE_P (decl) |
3205 | && !DECL_FINAL_P (decl) |
3206 | && !DECL_DESTRUCTOR_P (decl)) |
3207 | warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wsuggest_override, |
3208 | "%qD can be marked override" , decl); |
3209 | } |
3210 | else if (DECL_OVERRIDE_P (decl)) |
3211 | error ("%q+#D marked %<override%>, but does not override" , decl); |
3212 | |
3213 | if (DECL_VIRTUAL_P (decl)) |
3214 | { |
3215 | /* Remember this identifier is virtual name. */ |
3216 | IDENTIFIER_VIRTUAL_P (DECL_NAME (decl)) = true; |
3217 | |
3218 | if (!DECL_VINDEX (decl)) |
3219 | /* It's a new vfunc. */ |
3220 | DECL_VINDEX (decl) = error_mark_node; |
3221 | |
3222 | if (DECL_DESTRUCTOR_P (decl)) |
3223 | TYPE_HAS_NONTRIVIAL_DESTRUCTOR (ctype) = true; |
3224 | } |
3225 | else if (DECL_FINAL_P (decl)) |
3226 | error ("%q+#D marked %<final%>, but is not virtual" , decl); |
3227 | } |
3228 | |
3229 | /* Warn about hidden virtual functions that are not overridden in t. |
3230 | We know that constructors and destructors don't apply. */ |
3231 | |
3232 | static void |
3233 | warn_hidden (tree t) |
3234 | { |
3235 | if (vec<tree, va_gc> *member_vec = CLASSTYPE_MEMBER_VEC (t)) |
3236 | for (unsigned ix = member_vec->length (); ix--;) |
3237 | { |
3238 | tree fns = (*member_vec)[ix]; |
3239 | |
3240 | if (!OVL_P (fns)) |
3241 | continue; |
3242 | |
3243 | tree name = OVL_NAME (fns); |
3244 | auto_vec<tree, 20> base_fndecls; |
3245 | tree base_binfo; |
3246 | tree binfo; |
3247 | unsigned j; |
3248 | |
3249 | if (IDENTIFIER_CDTOR_P (name)) |
3250 | continue; |
3251 | |
3252 | /* Iterate through all of the base classes looking for possibly |
3253 | hidden functions. */ |
3254 | for (binfo = TYPE_BINFO (t), j = 0; |
3255 | BINFO_BASE_ITERATE (binfo, j, base_binfo); j++) |
3256 | { |
3257 | tree basetype = BINFO_TYPE (base_binfo); |
3258 | get_basefndecls (name, t: basetype, base_fndecls: &base_fndecls); |
3259 | } |
3260 | |
3261 | /* If there are no functions to hide, continue. */ |
3262 | if (base_fndecls.is_empty ()) |
3263 | continue; |
3264 | |
3265 | /* Remove any overridden functions. */ |
3266 | bool seen_non_override = false; |
3267 | for (tree fndecl : ovl_range (fns)) |
3268 | { |
3269 | bool any_override = false; |
3270 | if (TREE_CODE (fndecl) == FUNCTION_DECL |
3271 | && DECL_VINDEX (fndecl)) |
3272 | { |
3273 | /* If the method from the base class has the same |
3274 | signature as the method from the derived class, it |
3275 | has been overridden. Note that we can't move on |
3276 | after finding one match: fndecl might override |
3277 | multiple base fns. */ |
3278 | for (size_t k = 0; k < base_fndecls.length (); k++) |
3279 | if (base_fndecls[k] |
3280 | && same_signature_p (fndecl, base_fndecl: base_fndecls[k])) |
3281 | { |
3282 | base_fndecls[k] = NULL_TREE; |
3283 | any_override = true; |
3284 | } |
3285 | } |
3286 | if (!any_override) |
3287 | seen_non_override = true; |
3288 | } |
3289 | |
3290 | if (!seen_non_override && warn_overloaded_virtual == 1) |
3291 | /* All the derived fns override base virtuals. */ |
3292 | return; |
3293 | |
3294 | /* Now give a warning for all base functions without overriders, |
3295 | as they are hidden. */ |
3296 | for (tree base_fndecl : base_fndecls) |
3297 | if (base_fndecl) |
3298 | { |
3299 | auto_diagnostic_group d; |
3300 | /* Here we know it is a hider, and no overrider exists. */ |
3301 | if (warning_at (location_of (base_fndecl), |
3302 | OPT_Woverloaded_virtual_, |
3303 | "%qD was hidden" , base_fndecl)) |
3304 | inform (location_of (fns), " by %qD" , fns); |
3305 | } |
3306 | } |
3307 | } |
3308 | |
3309 | /* Recursive helper for finish_struct_anon. */ |
3310 | |
3311 | static void |
3312 | finish_struct_anon_r (tree field) |
3313 | { |
3314 | for (tree elt = TYPE_FIELDS (TREE_TYPE (field)); elt; elt = DECL_CHAIN (elt)) |
3315 | { |
3316 | /* We're generally only interested in entities the user |
3317 | declared, but we also find nested classes by noticing |
3318 | the TYPE_DECL that we create implicitly. You're |
3319 | allowed to put one anonymous union inside another, |
3320 | though, so we explicitly tolerate that. We use |
3321 | TYPE_UNNAMED_P rather than ANON_AGGR_TYPE_P so that |
3322 | we also allow unnamed types used for defining fields. */ |
3323 | if (DECL_ARTIFICIAL (elt) |
3324 | && (!DECL_IMPLICIT_TYPEDEF_P (elt) |
3325 | || TYPE_UNNAMED_P (TREE_TYPE (elt)))) |
3326 | continue; |
3327 | |
3328 | TREE_PRIVATE (elt) = TREE_PRIVATE (field); |
3329 | TREE_PROTECTED (elt) = TREE_PROTECTED (field); |
3330 | |
3331 | /* Recurse into the anonymous aggregates to correctly handle |
3332 | access control (c++/24926): |
3333 | |
3334 | class A { |
3335 | union { |
3336 | union { |
3337 | int i; |
3338 | }; |
3339 | }; |
3340 | }; |
3341 | |
3342 | int j=A().i; */ |
3343 | if (DECL_NAME (elt) == NULL_TREE |
3344 | && ANON_AGGR_TYPE_P (TREE_TYPE (elt))) |
3345 | finish_struct_anon_r (field: elt); |
3346 | } |
3347 | } |
3348 | |
3349 | /* Fix up any anonymous union/struct members of T. */ |
3350 | |
3351 | static void |
3352 | finish_struct_anon (tree t) |
3353 | { |
3354 | for (tree field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field)) |
3355 | { |
3356 | if (TREE_STATIC (field)) |
3357 | continue; |
3358 | if (TREE_CODE (field) != FIELD_DECL) |
3359 | continue; |
3360 | |
3361 | if (DECL_NAME (field) == NULL_TREE |
3362 | && ANON_AGGR_TYPE_P (TREE_TYPE (field))) |
3363 | finish_struct_anon_r (field); |
3364 | } |
3365 | } |
3366 | |
3367 | /* Add T to CLASSTYPE_DECL_LIST of current_class_type which |
3368 | will be used later during class template instantiation. |
3369 | When FRIEND_P is zero, T can be a static member data (VAR_DECL), |
3370 | a non-static member data (FIELD_DECL), a member function |
3371 | (FUNCTION_DECL), a nested type (RECORD_TYPE, ENUM_TYPE), |
3372 | a typedef (TYPE_DECL) or a member class template (TEMPLATE_DECL) |
3373 | When FRIEND_P is nonzero, T is either a friend class |
3374 | (RECORD_TYPE, TEMPLATE_DECL) or a friend function |
3375 | (FUNCTION_DECL, TEMPLATE_DECL). */ |
3376 | |
3377 | void |
3378 | maybe_add_class_template_decl_list (tree type, tree t, int friend_p) |
3379 | { |
3380 | if (CLASSTYPE_TEMPLATE_INFO (type) |
3381 | && TREE_CODE (t) != CONST_DECL) |
3382 | { |
3383 | tree purpose = friend_p ? NULL_TREE : type; |
3384 | |
3385 | CLASSTYPE_DECL_LIST (type) |
3386 | = tree_cons (purpose, t, CLASSTYPE_DECL_LIST (type)); |
3387 | } |
3388 | } |
3389 | |
3390 | /* This function is called from declare_virt_assop_and_dtor via |
3391 | dfs_walk_all. |
3392 | |
3393 | DATA is a type that direcly or indirectly inherits the base |
3394 | represented by BINFO. If BINFO contains a virtual assignment [copy |
3395 | assignment or move assigment] operator or a virtual constructor, |
3396 | declare that function in DATA if it hasn't been already declared. */ |
3397 | |
3398 | static tree |
3399 | dfs_declare_virt_assop_and_dtor (tree binfo, void *data) |
3400 | { |
3401 | tree bv, fn, t = (tree)data; |
3402 | tree opname = assign_op_identifier; |
3403 | |
3404 | gcc_assert (t && CLASS_TYPE_P (t)); |
3405 | gcc_assert (binfo && TREE_CODE (binfo) == TREE_BINFO); |
3406 | |
3407 | if (!TYPE_CONTAINS_VPTR_P (BINFO_TYPE (binfo))) |
3408 | /* A base without a vtable needs no modification, and its bases |
3409 | are uninteresting. */ |
3410 | return dfs_skip_bases; |
3411 | |
3412 | if (BINFO_PRIMARY_P (binfo)) |
3413 | /* If this is a primary base, then we have already looked at the |
3414 | virtual functions of its vtable. */ |
3415 | return NULL_TREE; |
3416 | |
3417 | for (bv = BINFO_VIRTUALS (binfo); bv; bv = TREE_CHAIN (bv)) |
3418 | { |
3419 | fn = BV_FN (bv); |
3420 | |
3421 | if (DECL_NAME (fn) == opname) |
3422 | { |
3423 | if (CLASSTYPE_LAZY_COPY_ASSIGN (t)) |
3424 | lazily_declare_fn (sfk_copy_assignment, t); |
3425 | if (CLASSTYPE_LAZY_MOVE_ASSIGN (t)) |
3426 | lazily_declare_fn (sfk_move_assignment, t); |
3427 | } |
3428 | else if (DECL_DESTRUCTOR_P (fn) |
3429 | && CLASSTYPE_LAZY_DESTRUCTOR (t)) |
3430 | lazily_declare_fn (sfk_destructor, t); |
3431 | } |
3432 | |
3433 | return NULL_TREE; |
3434 | } |
3435 | |
3436 | /* If the class type T has a direct or indirect base that contains a |
3437 | virtual assignment operator or a virtual destructor, declare that |
3438 | function in T if it hasn't been already declared. */ |
3439 | |
3440 | static void |
3441 | declare_virt_assop_and_dtor (tree t) |
3442 | { |
3443 | if (!(TYPE_POLYMORPHIC_P (t) |
3444 | && (CLASSTYPE_LAZY_COPY_ASSIGN (t) |
3445 | || CLASSTYPE_LAZY_MOVE_ASSIGN (t) |
3446 | || CLASSTYPE_LAZY_DESTRUCTOR (t)))) |
3447 | return; |
3448 | |
3449 | dfs_walk_all (TYPE_BINFO (t), |
3450 | dfs_declare_virt_assop_and_dtor, |
3451 | NULL, t); |
3452 | } |
3453 | |
3454 | /* Declare the inheriting constructor for class T inherited from base |
3455 | constructor CTOR with the parameter array PARMS of size NPARMS. */ |
3456 | |
3457 | static void |
3458 | one_inheriting_sig (tree t, tree ctor, tree *parms, int nparms) |
3459 | { |
3460 | gcc_assert (TYPE_MAIN_VARIANT (t) == t); |
3461 | |
3462 | /* We don't declare an inheriting ctor that would be a default, |
3463 | copy or move ctor for derived or base. */ |
3464 | if (nparms == 0) |
3465 | return; |
3466 | if (nparms == 1 |
3467 | && TYPE_REF_P (parms[0])) |
3468 | { |
3469 | tree parm = TYPE_MAIN_VARIANT (TREE_TYPE (parms[0])); |
3470 | if (parm == t || parm == DECL_CONTEXT (ctor)) |
3471 | return; |
3472 | } |
3473 | |
3474 | tree parmlist = void_list_node; |
3475 | for (int i = nparms - 1; i >= 0; i--) |
3476 | parmlist = tree_cons (NULL_TREE, parms[i], parmlist); |
3477 | tree fn = implicitly_declare_fn (sfk_inheriting_constructor, |
3478 | t, false, ctor, parmlist); |
3479 | |
3480 | if (add_method (type: t, method: fn, via_using: false)) |
3481 | { |
3482 | DECL_CHAIN (fn) = TYPE_FIELDS (t); |
3483 | TYPE_FIELDS (t) = fn; |
3484 | } |
3485 | } |
3486 | |
3487 | /* Declare all the inheriting constructors for class T inherited from base |
3488 | constructor CTOR. */ |
3489 | |
3490 | static void |
3491 | one_inherited_ctor (tree ctor, tree t, tree using_decl) |
3492 | { |
3493 | tree parms = FUNCTION_FIRST_USER_PARMTYPE (ctor); |
3494 | |
3495 | if (flag_new_inheriting_ctors) |
3496 | { |
3497 | ctor = implicitly_declare_fn (sfk_inheriting_constructor, |
3498 | t, /*const*/false, ctor, parms); |
3499 | add_method (type: t, method: ctor, via_using: using_decl != NULL_TREE); |
3500 | return; |
3501 | } |
3502 | |
3503 | tree *new_parms = XALLOCAVEC (tree, list_length (parms)); |
3504 | int i = 0; |
3505 | for (; parms && parms != void_list_node; parms = TREE_CHAIN (parms)) |
3506 | { |
3507 | if (TREE_PURPOSE (parms)) |
3508 | one_inheriting_sig (t, ctor, parms: new_parms, nparms: i); |
3509 | new_parms[i++] = TREE_VALUE (parms); |
3510 | } |
3511 | one_inheriting_sig (t, ctor, parms: new_parms, nparms: i); |
3512 | if (parms == NULL_TREE) |
3513 | { |
3514 | auto_diagnostic_group d; |
3515 | if (warning (OPT_Winherited_variadic_ctor, |
3516 | "the ellipsis in %qD is not inherited" , ctor)) |
3517 | inform (DECL_SOURCE_LOCATION (ctor), "%qD declared here" , ctor); |
3518 | } |
3519 | } |
3520 | |
3521 | /* Implicitly declare T(). */ |
3522 | |
3523 | static void |
3524 | add_implicit_default_ctor (tree t) |
3525 | { |
3526 | TYPE_HAS_DEFAULT_CONSTRUCTOR (t) = 1; |
3527 | CLASSTYPE_LAZY_DEFAULT_CTOR (t) = 1; |
3528 | if (cxx_dialect >= cxx11) |
3529 | TYPE_HAS_CONSTEXPR_CTOR (t) |
3530 | /* Don't force the declaration to get a hard answer; if the |
3531 | definition would have made the class non-literal, it will still be |
3532 | non-literal because of the base or member in question, and that |
3533 | gives a better diagnostic. */ |
3534 | = type_maybe_constexpr_default_constructor (t); |
3535 | } |
3536 | |
3537 | /* Create default constructors, assignment operators, and so forth for |
3538 | the type indicated by T, if they are needed. CANT_HAVE_CONST_CTOR, |
3539 | and CANT_HAVE_CONST_ASSIGNMENT are nonzero if, for whatever reason, |
3540 | the class cannot have a default constructor, copy constructor |
3541 | taking a const reference argument, or an assignment operator taking |
3542 | a const reference, respectively. */ |
3543 | |
3544 | static void |
3545 | add_implicitly_declared_members (tree t, tree* access_decls, |
3546 | int cant_have_const_cctor, |
3547 | int cant_have_const_assignment) |
3548 | { |
3549 | /* Destructor. */ |
3550 | if (!CLASSTYPE_DESTRUCTOR (t)) |
3551 | /* In general, we create destructors lazily. */ |
3552 | CLASSTYPE_LAZY_DESTRUCTOR (t) = 1; |
3553 | |
3554 | bool move_ok = false; |
3555 | if (cxx_dialect >= cxx11 && CLASSTYPE_LAZY_DESTRUCTOR (t) |
3556 | && !TYPE_HAS_COPY_CTOR (t) && !TYPE_HAS_COPY_ASSIGN (t) |
3557 | && !classtype_has_move_assign_or_move_ctor_p (t, user_declared: false)) |
3558 | move_ok = true; |
3559 | |
3560 | /* [class.ctor] |
3561 | |
3562 | If there is no user-declared constructor for a class, a default |
3563 | constructor is implicitly declared. */ |
3564 | if (! TYPE_HAS_USER_CONSTRUCTOR (t)) |
3565 | add_implicit_default_ctor (t); |
3566 | |
3567 | /* [class.ctor] |
3568 | |
3569 | If a class definition does not explicitly declare a copy |
3570 | constructor, one is declared implicitly. */ |
3571 | if (! TYPE_HAS_COPY_CTOR (t)) |
3572 | { |
3573 | TYPE_HAS_COPY_CTOR (t) = 1; |
3574 | TYPE_HAS_CONST_COPY_CTOR (t) = !cant_have_const_cctor; |
3575 | CLASSTYPE_LAZY_COPY_CTOR (t) = 1; |
3576 | if (move_ok) |
3577 | CLASSTYPE_LAZY_MOVE_CTOR (t) = 1; |
3578 | } |
3579 | |
3580 | /* If there is no assignment operator, one will be created if and |
3581 | when it is needed. For now, just record whether or not the type |
3582 | of the parameter to the assignment operator will be a const or |
3583 | non-const reference. */ |
3584 | if (!TYPE_HAS_COPY_ASSIGN (t)) |
3585 | { |
3586 | TYPE_HAS_COPY_ASSIGN (t) = 1; |
3587 | TYPE_HAS_CONST_COPY_ASSIGN (t) = !cant_have_const_assignment; |
3588 | CLASSTYPE_LAZY_COPY_ASSIGN (t) = 1; |
3589 | if (move_ok && !LAMBDA_TYPE_P (t)) |
3590 | CLASSTYPE_LAZY_MOVE_ASSIGN (t) = 1; |
3591 | } |
3592 | |
3593 | /* We can't be lazy about declaring functions that might override |
3594 | a virtual function from a base class. */ |
3595 | declare_virt_assop_and_dtor (t); |
3596 | |
3597 | /* If the class definition does not explicitly declare an == operator |
3598 | function, but declares a defaulted three-way comparison operator function, |
3599 | an == operator function is declared implicitly. */ |
3600 | if (!classtype_has_op (t, EQ_EXPR)) |
3601 | if (tree space = classtype_has_defaulted_op (t, SPACESHIP_EXPR)) |
3602 | { |
3603 | tree eq = implicitly_declare_fn (sfk_comparison, t, false, space, |
3604 | NULL_TREE); |
3605 | bool is_friend = DECL_CONTEXT (space) != t; |
3606 | if (is_friend) |
3607 | do_friend (NULL_TREE, DECL_NAME (eq), eq, |
3608 | NO_SPECIAL, true); |
3609 | else |
3610 | { |
3611 | add_method (type: t, method: eq, via_using: false); |
3612 | DECL_CHAIN (eq) = TYPE_FIELDS (t); |
3613 | TYPE_FIELDS (t) = eq; |
3614 | } |
3615 | maybe_add_class_template_decl_list (type: t, t: eq, friend_p: is_friend); |
3616 | } |
3617 | |
3618 | while (*access_decls) |
3619 | { |
3620 | tree using_decl = TREE_VALUE (*access_decls); |
3621 | tree decl = USING_DECL_DECLS (using_decl); |
3622 | if (DECL_NAME (using_decl) == ctor_identifier) |
3623 | { |
3624 | /* declare, then remove the decl */ |
3625 | tree ctor_list = decl; |
3626 | location_t loc = input_location; |
3627 | input_location = DECL_SOURCE_LOCATION (using_decl); |
3628 | for (tree fn : ovl_range (ctor_list)) |
3629 | { |
3630 | if (!TYPE_HAS_DEFAULT_CONSTRUCTOR (t) && default_ctor_p (fn)) |
3631 | /* CWG2799: Inheriting a default constructor gives us a default |
3632 | constructor, not just an inherited constructor. */ |
3633 | add_implicit_default_ctor (t); |
3634 | one_inherited_ctor (ctor: fn, t, using_decl); |
3635 | } |
3636 | *access_decls = TREE_CHAIN (*access_decls); |
3637 | input_location = loc; |
3638 | } |
3639 | else |
3640 | access_decls = &TREE_CHAIN (*access_decls); |
3641 | } |
3642 | } |
3643 | |
3644 | /* Cache of enum_min_precision values. */ |
3645 | static GTY((deletable)) hash_map<tree, int> *enum_to_min_precision; |
3646 | |
3647 | /* Return the minimum precision of a bit-field needed to store all |
3648 | enumerators of ENUMERAL_TYPE TYPE. */ |
3649 | |
3650 | static int |
3651 | enum_min_precision (tree type) |
3652 | { |
3653 | type = TYPE_MAIN_VARIANT (type); |
3654 | /* For unscoped enums without fixed underlying type and without mode |
3655 | attribute we can just use precision of the underlying type. */ |
3656 | if (UNSCOPED_ENUM_P (type) |
3657 | && !ENUM_FIXED_UNDERLYING_TYPE_P (type) |
3658 | && !lookup_attribute (attr_name: "mode" , TYPE_ATTRIBUTES (type))) |
3659 | return TYPE_PRECISION (ENUM_UNDERLYING_TYPE (type)); |
3660 | |
3661 | if (enum_to_min_precision == NULL) |
3662 | enum_to_min_precision = hash_map<tree, int>::create_ggc (size: 37); |
3663 | |
3664 | bool existed; |
3665 | int &prec = enum_to_min_precision->get_or_insert (k: type, existed: &existed); |
3666 | if (existed) |
3667 | return prec; |
3668 | |
3669 | tree minnode, maxnode; |
3670 | if (TYPE_VALUES (type)) |
3671 | { |
3672 | minnode = maxnode = NULL_TREE; |
3673 | for (tree values = TYPE_VALUES (type); |
3674 | values; values = TREE_CHAIN (values)) |
3675 | { |
3676 | tree decl = TREE_VALUE (values); |
3677 | tree value = DECL_INITIAL (decl); |
3678 | if (value == error_mark_node) |
3679 | value = integer_zero_node; |
3680 | if (!minnode) |
3681 | minnode = maxnode = value; |
3682 | else if (tree_int_cst_lt (t1: maxnode, t2: value)) |
3683 | maxnode = value; |
3684 | else if (tree_int_cst_lt (t1: value, t2: minnode)) |
3685 | minnode = value; |
3686 | } |
3687 | } |
3688 | else |
3689 | minnode = maxnode = integer_zero_node; |
3690 | |
3691 | signop sgn = tree_int_cst_sgn (minnode) >= 0 ? UNSIGNED : SIGNED; |
3692 | int lowprec = tree_int_cst_min_precision (minnode, sgn); |
3693 | int highprec = tree_int_cst_min_precision (maxnode, sgn); |
3694 | prec = MAX (lowprec, highprec); |
3695 | return prec; |
3696 | } |
3697 | |
3698 | /* FIELD is a bit-field. We are finishing the processing for its |
3699 | enclosing type. Issue any appropriate messages and set appropriate |
3700 | flags. Returns false if an error has been diagnosed. */ |
3701 | |
3702 | static bool |
3703 | check_bitfield_decl (tree field) |
3704 | { |
3705 | tree type = TREE_TYPE (field); |
3706 | tree w; |
3707 | |
3708 | /* Extract the declared width of the bitfield, which has been |
3709 | temporarily stashed in DECL_BIT_FIELD_REPRESENTATIVE by grokbitfield. */ |
3710 | w = DECL_BIT_FIELD_REPRESENTATIVE (field); |
3711 | gcc_assert (w != NULL_TREE); |
3712 | /* Remove the bit-field width indicator so that the rest of the |
3713 | compiler does not treat that value as a qualifier. */ |
3714 | DECL_BIT_FIELD_REPRESENTATIVE (field) = NULL_TREE; |
3715 | |
3716 | /* Detect invalid bit-field type. */ |
3717 | if (!INTEGRAL_OR_ENUMERATION_TYPE_P (type)) |
3718 | { |
3719 | error_at (DECL_SOURCE_LOCATION (field), |
3720 | "bit-field %q#D with non-integral type %qT" , field, type); |
3721 | w = error_mark_node; |
3722 | } |
3723 | else |
3724 | { |
3725 | location_t loc = input_location; |
3726 | /* Avoid the non_lvalue wrapper added by fold for PLUS_EXPRs. */ |
3727 | STRIP_NOPS (w); |
3728 | |
3729 | /* detect invalid field size. */ |
3730 | input_location = DECL_SOURCE_LOCATION (field); |
3731 | w = cxx_constant_value (w); |
3732 | input_location = loc; |
3733 | |
3734 | if (TREE_CODE (w) != INTEGER_CST) |
3735 | { |
3736 | error ("bit-field %q+D width not an integer constant" , field); |
3737 | w = error_mark_node; |
3738 | } |
3739 | else if (tree_int_cst_sgn (w) < 0) |
3740 | { |
3741 | error ("negative width in bit-field %q+D" , field); |
3742 | w = error_mark_node; |
3743 | } |
3744 | else if (integer_zerop (w) && DECL_NAME (field) != 0) |
3745 | { |
3746 | error ("zero width for bit-field %q+D" , field); |
3747 | w = error_mark_node; |
3748 | } |
3749 | else if ((TREE_CODE (type) != ENUMERAL_TYPE |
3750 | && TREE_CODE (type) != BOOLEAN_TYPE |
3751 | && compare_tree_int (w, TYPE_PRECISION (type)) > 0) |
3752 | || ((TREE_CODE (type) == ENUMERAL_TYPE |
3753 | || TREE_CODE (type) == BOOLEAN_TYPE) |
3754 | && tree_int_cst_lt (TYPE_SIZE (type), t2: w))) |
3755 | warning_at (DECL_SOURCE_LOCATION (field), 0, |
3756 | "width of %qD exceeds its type" , field); |
3757 | else if (TREE_CODE (type) == ENUMERAL_TYPE) |
3758 | { |
3759 | int prec = enum_min_precision (type); |
3760 | if (compare_tree_int (w, prec) < 0) |
3761 | warning_at (DECL_SOURCE_LOCATION (field), 0, |
3762 | "%qD is too small to hold all values of %q#T" , |
3763 | field, type); |
3764 | } |
3765 | } |
3766 | |
3767 | if (w != error_mark_node) |
3768 | { |
3769 | DECL_SIZE (field) = fold_convert (bitsizetype, w); |
3770 | DECL_BIT_FIELD (field) = 1; |
3771 | return true; |
3772 | } |
3773 | else |
3774 | { |
3775 | /* Non-bit-fields are aligned for their type. */ |
3776 | DECL_BIT_FIELD (field) = 0; |
3777 | CLEAR_DECL_C_BIT_FIELD (field); |
3778 | return false; |
3779 | } |
3780 | } |
3781 | |
3782 | /* FIELD is a non bit-field. We are finishing the processing for its |
3783 | enclosing type T. Issue any appropriate messages and set appropriate |
3784 | flags. */ |
3785 | |
3786 | static bool |
3787 | check_field_decl (tree field, |
3788 | tree t, |
3789 | int* cant_have_const_ctor, |
3790 | int* no_const_asn_ref) |
3791 | { |
3792 | tree type = strip_array_types (TREE_TYPE (field)); |
3793 | bool any_default_members = false; |
3794 | |
3795 | /* In C++98 an anonymous union cannot contain any fields which would change |
3796 | the settings of CANT_HAVE_CONST_CTOR and friends. */ |
3797 | if (ANON_UNION_TYPE_P (type) && cxx_dialect < cxx11) |
3798 | ; |
3799 | /* And, we don't set TYPE_HAS_CONST_COPY_CTOR, etc., for anonymous |
3800 | structs. So, we recurse through their fields here. */ |
3801 | else if (ANON_AGGR_TYPE_P (type)) |
3802 | { |
3803 | for (tree fields = TYPE_FIELDS (type); fields; |
3804 | fields = DECL_CHAIN (fields)) |
3805 | if (TREE_CODE (fields) == FIELD_DECL) |
3806 | any_default_members |= check_field_decl (field: fields, t, |
3807 | cant_have_const_ctor, |
3808 | no_const_asn_ref); |
3809 | } |
3810 | /* Check members with class type for constructors, destructors, |
3811 | etc. */ |
3812 | else if (CLASS_TYPE_P (type)) |
3813 | { |
3814 | /* Never let anything with uninheritable virtuals |
3815 | make it through without complaint. */ |
3816 | abstract_virtuals_error (field, type); |
3817 | |
3818 | if (TREE_CODE (t) == UNION_TYPE && cxx_dialect < cxx11) |
3819 | { |
3820 | static bool warned; |
3821 | int oldcount = errorcount; |
3822 | if (TYPE_NEEDS_CONSTRUCTING (type)) |
3823 | error ("member %q+#D with constructor not allowed in union" , |
3824 | field); |
3825 | if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)) |
3826 | error ("member %q+#D with destructor not allowed in union" , field); |
3827 | if (TYPE_HAS_COMPLEX_COPY_ASSIGN (type)) |
3828 | error ("member %q+#D with copy assignment operator not allowed in union" , |
3829 | field); |
3830 | if (!warned && errorcount > oldcount) |
3831 | { |
3832 | inform (DECL_SOURCE_LOCATION (field), "unrestricted unions " |
3833 | "only available with %<-std=c++11%> or %<-std=gnu++11%>" ); |
3834 | warned = true; |
3835 | } |
3836 | } |
3837 | else |
3838 | { |
3839 | TYPE_NEEDS_CONSTRUCTING (t) |= TYPE_NEEDS_CONSTRUCTING (type); |
3840 | TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t) |
3841 | |= TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type); |
3842 | TYPE_HAS_COMPLEX_COPY_ASSIGN (t) |
3843 | |= (TYPE_HAS_COMPLEX_COPY_ASSIGN (type) |
3844 | || !TYPE_HAS_COPY_ASSIGN (type)); |
3845 | TYPE_HAS_COMPLEX_COPY_CTOR (t) |= (TYPE_HAS_COMPLEX_COPY_CTOR (type) |
3846 | || !TYPE_HAS_COPY_CTOR (type)); |
3847 | TYPE_HAS_COMPLEX_MOVE_ASSIGN (t) |= TYPE_HAS_COMPLEX_MOVE_ASSIGN (type); |
3848 | TYPE_HAS_COMPLEX_MOVE_CTOR (t) |= TYPE_HAS_COMPLEX_MOVE_CTOR (type); |
3849 | TYPE_HAS_COMPLEX_DFLT (t) |= (!TYPE_HAS_DEFAULT_CONSTRUCTOR (type) |
3850 | || TYPE_HAS_COMPLEX_DFLT (type)); |
3851 | } |
3852 | |
3853 | if (TYPE_HAS_COPY_CTOR (type) |
3854 | && !TYPE_HAS_CONST_COPY_CTOR (type)) |
3855 | *cant_have_const_ctor = 1; |
3856 | |
3857 | if (TYPE_HAS_COPY_ASSIGN (type) |
3858 | && !TYPE_HAS_CONST_COPY_ASSIGN (type)) |
3859 | *no_const_asn_ref = 1; |
3860 | } |
3861 | |
3862 | check_abi_tags (t, subob: field); |
3863 | |
3864 | if (DECL_INITIAL (field) != NULL_TREE) |
3865 | /* `build_class_init_list' does not recognize |
3866 | non-FIELD_DECLs. */ |
3867 | any_default_members = true; |
3868 | |
3869 | return any_default_members; |
3870 | } |
3871 | |
3872 | /* Check the data members (both static and non-static), class-scoped |
3873 | typedefs, etc., appearing in the declaration of T. Issue |
3874 | appropriate diagnostics. Sets ACCESS_DECLS to a list (in |
3875 | declaration order) of access declarations; each TREE_VALUE in this |
3876 | list is a USING_DECL. |
3877 | |
3878 | In addition, set the following flags: |
3879 | |
3880 | EMPTY_P |
3881 | The class is empty, i.e., contains no non-static data members. |
3882 | |
3883 | CANT_HAVE_CONST_CTOR_P |
3884 | This class cannot have an implicitly generated copy constructor |
3885 | taking a const reference. |
3886 | |
3887 | CANT_HAVE_CONST_ASN_REF |
3888 | This class cannot have an implicitly generated assignment |
3889 | operator taking a const reference. |
3890 | |
3891 | All of these flags should be initialized before calling this |
3892 | function. */ |
3893 | |
3894 | static void |
3895 | check_field_decls (tree t, tree *access_decls, |
3896 | int *cant_have_const_ctor_p, |
3897 | int *no_const_asn_ref_p) |
3898 | { |
3899 | int cant_pack = 0; |
3900 | |
3901 | /* Assume there are no access declarations. */ |
3902 | *access_decls = NULL_TREE; |
3903 | /* Effective C has things to say about classes with pointer members. */ |
3904 | tree pointer_member = NULL_TREE; |
3905 | /* Default initialized members affect the whole class. */ |
3906 | tree default_init_member = NULL_TREE; |
3907 | /* Lack of any non-static data member of non-volatile literal |
3908 | type affects a union. */ |
3909 | bool found_nv_literal_p = false; |
3910 | /* Standard layout requires all FIELDS have same access. */ |
3911 | int field_access = -1; |
3912 | |
3913 | for (tree field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field)) |
3914 | { |
3915 | tree type = TREE_TYPE (field); |
3916 | |
3917 | switch (TREE_CODE (field)) |
3918 | { |
3919 | default: |
3920 | gcc_unreachable (); |
3921 | |
3922 | case USING_DECL: |
3923 | /* Save the access declarations for our caller. */ |
3924 | *access_decls = tree_cons (NULL_TREE, field, *access_decls); |
3925 | break; |
3926 | |
3927 | case TYPE_DECL: |
3928 | case TEMPLATE_DECL: |
3929 | break; |
3930 | |
3931 | case FUNCTION_DECL: |
3932 | /* FIXME: We should fold in the checking from check_methods. */ |
3933 | break; |
3934 | |
3935 | case CONST_DECL: |
3936 | DECL_NONLOCAL (field) = 1; |
3937 | break; |
3938 | |
3939 | case VAR_DECL: |
3940 | if (TREE_CODE (t) == UNION_TYPE |
3941 | && cxx_dialect < cxx11) |
3942 | { |
3943 | /* [class.union] |
3944 | |
3945 | (C++98) If a union contains a static data member, |
3946 | ... the program is ill-formed. */ |
3947 | if (cxx_dialect < cxx11) |
3948 | error ("in C++98 %q+D may not be static because it is " |
3949 | "a member of a union" , field); |
3950 | } |
3951 | goto data_member; |
3952 | |
3953 | case FIELD_DECL: |
3954 | if (TREE_CODE (t) == UNION_TYPE) |
3955 | { |
3956 | /* [class.union] |
3957 | |
3958 | If a union contains ... or a [non-static data] member |
3959 | of reference type, the program is ill-formed. */ |
3960 | if (TYPE_REF_P (type)) |
3961 | error ("non-static data member %q+D in a union may not " |
3962 | "have reference type %qT" , field, type); |
3963 | } |
3964 | |
3965 | data_member: |
3966 | /* Common VAR_DECL & FIELD_DECL processing. */ |
3967 | DECL_CONTEXT (field) = t; |
3968 | DECL_NONLOCAL (field) = 1; |
3969 | |
3970 | /* Template instantiation can cause this. Perhaps this |
3971 | should be a specific instantiation check? */ |
3972 | if (TREE_CODE (type) == FUNCTION_TYPE) |
3973 | { |
3974 | error ("data member %q+D invalidly declared function type" , field); |
3975 | type = build_pointer_type (type); |
3976 | TREE_TYPE (field) = type; |
3977 | } |
3978 | else if (TREE_CODE (type) == METHOD_TYPE) |
3979 | { |
3980 | error ("data member %q+D invalidly declared method type" , field); |
3981 | type = build_pointer_type (type); |
3982 | TREE_TYPE (field) = type; |
3983 | } |
3984 | |
3985 | break; |
3986 | } |
3987 | |
3988 | if (TREE_CODE (field) != FIELD_DECL) |
3989 | continue; |
3990 | |
3991 | if (type == error_mark_node) |
3992 | continue; |
3993 | |
3994 | /* If it is not a union and at least one non-static data member is |
3995 | non-literal, the whole class becomes non-literal. Per Core/1453, |
3996 | volatile non-static data members and base classes are also not allowed. |
3997 | If it is a union, we might set CLASSTYPE_LITERAL_P after we've seen all |
3998 | members. |
3999 | Note: if the type is incomplete we will complain later on. */ |
4000 | if (COMPLETE_TYPE_P (type)) |
4001 | { |
4002 | if (!literal_type_p (type) || CP_TYPE_VOLATILE_P (type)) |
4003 | CLASSTYPE_LITERAL_P (t) = false; |
4004 | else |
4005 | found_nv_literal_p = true; |
4006 | } |
4007 | |
4008 | int this_field_access = (TREE_PROTECTED (field) ? 1 |
4009 | : TREE_PRIVATE (field) ? 2 : 0); |
4010 | if (field_access != this_field_access) |
4011 | { |
4012 | /* A standard-layout class is a class that: |
4013 | |
4014 | ... has the same access control (Clause 11) for all |
4015 | non-static data members, */ |
4016 | if (field_access < 0) |
4017 | field_access = this_field_access; |
4018 | else |
4019 | CLASSTYPE_NON_STD_LAYOUT (t) = 1; |
4020 | |
4021 | /* Aggregates must be public. */ |
4022 | if (this_field_access) |
4023 | CLASSTYPE_NON_AGGREGATE (t) = 1; |
4024 | } |
4025 | |
4026 | /* If this is of reference type, check if it needs an init. */ |
4027 | if (TYPE_REF_P (type)) |
4028 | { |
4029 | CLASSTYPE_NON_LAYOUT_POD_P (t) = 1; |
4030 | CLASSTYPE_NON_STD_LAYOUT (t) = 1; |
4031 | if (DECL_INITIAL (field) == NULL_TREE) |
4032 | SET_CLASSTYPE_REF_FIELDS_NEED_INIT (t, 1); |
4033 | if (cxx_dialect < cxx11) |
4034 | { |
4035 | /* ARM $12.6.2: [A member initializer list] (or, for an |
4036 | aggregate, initialization by a brace-enclosed list) is the |
4037 | only way to initialize non-static const and reference |
4038 | members. */ |
4039 | TYPE_HAS_COMPLEX_COPY_ASSIGN (t) = 1; |
4040 | TYPE_HAS_COMPLEX_MOVE_ASSIGN (t) = 1; |
4041 | } |
4042 | } |
4043 | |
4044 | type = strip_array_types (type); |
4045 | |
4046 | if (TYPE_PACKED (t)) |
4047 | { |
4048 | if (!layout_pod_type_p (type) && !TYPE_PACKED (type)) |
4049 | { |
4050 | warning_at (DECL_SOURCE_LOCATION (field), 0, |
4051 | "ignoring packed attribute because of" |
4052 | " unpacked non-POD field %q#D" , field); |
4053 | cant_pack = 1; |
4054 | } |
4055 | else if (DECL_C_BIT_FIELD (field) |
4056 | || TYPE_ALIGN (TREE_TYPE (field)) > BITS_PER_UNIT) |
4057 | DECL_PACKED (field) = 1; |
4058 | } |
4059 | |
4060 | if (DECL_C_BIT_FIELD (field) |
4061 | && integer_zerop (DECL_BIT_FIELD_REPRESENTATIVE (field))) |
4062 | /* We don't treat zero-width bitfields as making a class |
4063 | non-empty. */ |
4064 | ; |
4065 | else if (field_poverlapping_p (field) |
4066 | && is_empty_class (TREE_TYPE (field))) |
4067 | /* Empty data members also don't make a class non-empty. */ |
4068 | CLASSTYPE_CONTAINS_EMPTY_CLASS_P (t) = 1; |
4069 | else |
4070 | { |
4071 | /* The class is non-empty. */ |
4072 | CLASSTYPE_EMPTY_P (t) = 0; |
4073 | /* The class is not even nearly empty. */ |
4074 | CLASSTYPE_NEARLY_EMPTY_P (t) = 0; |
4075 | /* If one of the data members contains an empty class, so |
4076 | does T. */ |
4077 | if (CLASS_TYPE_P (type) |
4078 | && CLASSTYPE_CONTAINS_EMPTY_CLASS_P (type)) |
4079 | CLASSTYPE_CONTAINS_EMPTY_CLASS_P (t) = 1; |
4080 | } |
4081 | |
4082 | /* This is used by -Weffc++ (see below). Warn only for pointers |
4083 | to members which might hold dynamic memory. So do not warn |
4084 | for pointers to functions or pointers to members. */ |
4085 | if (TYPE_PTR_P (type) |
4086 | && !TYPE_PTRFN_P (type)) |
4087 | pointer_member = field; |
4088 | |
4089 | if (CLASS_TYPE_P (type)) |
4090 | { |
4091 | if (CLASSTYPE_REF_FIELDS_NEED_INIT (type)) |
4092 | SET_CLASSTYPE_REF_FIELDS_NEED_INIT (t, 1); |
4093 | if (CLASSTYPE_READONLY_FIELDS_NEED_INIT (type)) |
4094 | SET_CLASSTYPE_READONLY_FIELDS_NEED_INIT (t, 1); |
4095 | } |
4096 | |
4097 | if (DECL_MUTABLE_P (field) || TYPE_HAS_MUTABLE_P (type)) |
4098 | CLASSTYPE_HAS_MUTABLE (t) = 1; |
4099 | |
4100 | if (DECL_MUTABLE_P (field)) |
4101 | { |
4102 | if (TYPE_REF_P (type)) |
4103 | error ("member %q+D cannot be declared as a %<mutable%> " |
4104 | "reference" , field); |
4105 | else if (CP_TYPE_CONST_P (type)) |
4106 | error ("member %q+D cannot be declared both %<const%> " |
4107 | "and %<mutable%>" , field); |
4108 | } |
4109 | |
4110 | if (! layout_pod_type_p (type)) |
4111 | /* DR 148 now allows pointers to members (which are POD themselves), |
4112 | to be allowed in POD structs. */ |
4113 | CLASSTYPE_NON_LAYOUT_POD_P (t) = 1; |
4114 | |
4115 | if (field_poverlapping_p (field)) |
4116 | /* A potentially-overlapping non-static data member makes the class |
4117 | non-layout-POD. */ |
4118 | CLASSTYPE_NON_LAYOUT_POD_P (t) = 1; |
4119 | |
4120 | if (!std_layout_type_p (type)) |
4121 | CLASSTYPE_NON_STD_LAYOUT (t) = 1; |
4122 | |
4123 | if (! zero_init_p (type)) |
4124 | CLASSTYPE_NON_ZERO_INIT_P (t) = 1; |
4125 | |
4126 | /* We set DECL_C_BIT_FIELD in grokbitfield. |
4127 | If the type and width are valid, we'll also set DECL_BIT_FIELD. */ |
4128 | if (DECL_C_BIT_FIELD (field)) |
4129 | check_bitfield_decl (field); |
4130 | |
4131 | if (check_field_decl (field, t, |
4132 | cant_have_const_ctor: cant_have_const_ctor_p, no_const_asn_ref: no_const_asn_ref_p)) |
4133 | { |
4134 | if (default_init_member |
4135 | && TREE_CODE (t) == UNION_TYPE) |
4136 | { |
4137 | error ("multiple fields in union %qT initialized" , t); |
4138 | inform (DECL_SOURCE_LOCATION (default_init_member), |
4139 | "initialized member %q+D declared here" , |
4140 | default_init_member); |
4141 | } |
4142 | default_init_member = field; |
4143 | } |
4144 | |
4145 | /* Now that we've removed bit-field widths from DECL_INITIAL, |
4146 | anything left in DECL_INITIAL is an NSDMI that makes the class |
4147 | non-aggregate in C++11, and non-layout-POD always. */ |
4148 | if (DECL_INITIAL (field)) |
4149 | { |
4150 | if (cxx_dialect < cxx14) |
4151 | CLASSTYPE_NON_AGGREGATE (t) = true; |
4152 | else |
4153 | CLASSTYPE_NON_POD_AGGREGATE (t) = true; |
4154 | } |
4155 | |
4156 | if (CP_TYPE_CONST_P (type)) |
4157 | { |
4158 | /* If any field is const, the structure type is pseudo-const. */ |
4159 | C_TYPE_FIELDS_READONLY (t) = 1; |
4160 | if (DECL_INITIAL (field) == NULL_TREE) |
4161 | SET_CLASSTYPE_READONLY_FIELDS_NEED_INIT (t, 1); |
4162 | if (cxx_dialect < cxx11) |
4163 | { |
4164 | /* ARM $12.6.2: [A member initializer list] (or, for an |
4165 | aggregate, initialization by a brace-enclosed list) is the |
4166 | only way to initialize non-static const and reference |
4167 | members. */ |
4168 | TYPE_HAS_COMPLEX_COPY_ASSIGN (t) = 1; |
4169 | TYPE_HAS_COMPLEX_MOVE_ASSIGN (t) = 1; |
4170 | } |
4171 | } |
4172 | /* A field that is pseudo-const makes the structure likewise. */ |
4173 | else if (CLASS_TYPE_P (type)) |
4174 | { |
4175 | C_TYPE_FIELDS_READONLY (t) |= C_TYPE_FIELDS_READONLY (type); |
4176 | SET_CLASSTYPE_READONLY_FIELDS_NEED_INIT (t, |
4177 | CLASSTYPE_READONLY_FIELDS_NEED_INIT (t) |
4178 | | CLASSTYPE_READONLY_FIELDS_NEED_INIT (type)); |
4179 | } |
4180 | |
4181 | /* Core issue 80: A non-static data member is required to have a |
4182 | different name from the class iff the class has a |
4183 | user-declared constructor. */ |
4184 | if (constructor_name_p (DECL_NAME (field), t) |
4185 | && TYPE_HAS_USER_CONSTRUCTOR (t)) |
4186 | permerror (DECL_SOURCE_LOCATION (field), |
4187 | "field %q#D with same name as class" , field); |
4188 | } |
4189 | |
4190 | /* Per CWG 2096, a type is a literal type if it is a union, and at least |
4191 | one of its non-static data members is of non-volatile literal type. */ |
4192 | if (TREE_CODE (t) == UNION_TYPE && found_nv_literal_p) |
4193 | CLASSTYPE_LITERAL_P (t) = true; |
4194 | |
4195 | /* Effective C++ rule 11: if a class has dynamic memory held by pointers, |
4196 | it should also define a copy constructor and an assignment operator to |
4197 | implement the correct copy semantic (deep vs shallow, etc.). As it is |
4198 | not feasible to check whether the constructors do allocate dynamic memory |
4199 | and store it within members, we approximate the warning like this: |
4200 | |
4201 | -- Warn only if there are members which are pointers |
4202 | -- Warn only if there is a non-trivial constructor (otherwise, |
4203 | there cannot be memory allocated). |
4204 | -- Warn only if there is a non-trivial destructor. We assume that the |
4205 | user at least implemented the cleanup correctly, and a destructor |
4206 | is needed to free dynamic memory. |
4207 | |
4208 | This seems enough for practical purposes. */ |
4209 | if (warn_ecpp |
4210 | && pointer_member |
4211 | && TYPE_HAS_USER_CONSTRUCTOR (t) |
4212 | && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t) |
4213 | && !(TYPE_HAS_COPY_CTOR (t) && TYPE_HAS_COPY_ASSIGN (t))) |
4214 | { |
4215 | if (warning (OPT_Weffc__, "%q#T has pointer data members" , t)) |
4216 | { |
4217 | if (! TYPE_HAS_COPY_CTOR (t)) |
4218 | { |
4219 | warning (OPT_Weffc__, |
4220 | " but does not declare %<%T(const %T&)%>" , t, t); |
4221 | if (!TYPE_HAS_COPY_ASSIGN (t)) |
4222 | warning (OPT_Weffc__, " or %<operator=(const %T&)%>" , t); |
4223 | } |
4224 | else if (! TYPE_HAS_COPY_ASSIGN (t)) |
4225 | warning (OPT_Weffc__, |
4226 | " but does not declare %<operator=(const %T&)%>" , t); |
4227 | inform (DECL_SOURCE_LOCATION (pointer_member), |
4228 | "pointer member %q+D declared here" , pointer_member); |
4229 | } |
4230 | } |
4231 | |
4232 | /* Non-static data member initializers make the default constructor |
4233 | non-trivial. */ |
4234 | if (default_init_member) |
4235 | { |
4236 | TYPE_NEEDS_CONSTRUCTING (t) = true; |
4237 | TYPE_HAS_COMPLEX_DFLT (t) = true; |
4238 | } |
4239 | |
4240 | /* If any of the fields couldn't be packed, unset TYPE_PACKED. */ |
4241 | if (cant_pack) |
4242 | TYPE_PACKED (t) = 0; |
4243 | |
4244 | /* Check anonymous struct/anonymous union fields. */ |
4245 | finish_struct_anon (t); |
4246 | |
4247 | /* We've built up the list of access declarations in reverse order. |
4248 | Fix that now. */ |
4249 | *access_decls = nreverse (*access_decls); |
4250 | } |
4251 | |
4252 | /* If TYPE is an empty class type, records its OFFSET in the table of |
4253 | OFFSETS. */ |
4254 | |
4255 | static int |
4256 | record_subobject_offset (tree type, tree offset, splay_tree offsets) |
4257 | { |
4258 | splay_tree_node n; |
4259 | |
4260 | if (!is_empty_class (type)) |
4261 | return 0; |
4262 | |
4263 | /* Record the location of this empty object in OFFSETS. */ |
4264 | n = splay_tree_lookup (offsets, (splay_tree_key) offset); |
4265 | if (!n) |
4266 | n = splay_tree_insert (offsets, |
4267 | (splay_tree_key) offset, |
4268 | (splay_tree_value) NULL_TREE); |
4269 | n->value = ((splay_tree_value) |
4270 | tree_cons (NULL_TREE, |
4271 | type, |
4272 | (tree) n->value)); |
4273 | |
4274 | return 0; |
4275 | } |
4276 | |
4277 | /* Returns nonzero if TYPE is an empty class type and there is |
4278 | already an entry in OFFSETS for the same TYPE as the same OFFSET. */ |
4279 | |
4280 | static int |
4281 | check_subobject_offset (tree type, tree offset, splay_tree offsets) |
4282 | { |
4283 | splay_tree_node n; |
4284 | tree t; |
4285 | |
4286 | if (!is_empty_class (type)) |
4287 | return 0; |
4288 | |
4289 | /* Record the location of this empty object in OFFSETS. */ |
4290 | n = splay_tree_lookup (offsets, (splay_tree_key) offset); |
4291 | if (!n) |
4292 | return 0; |
4293 | |
4294 | enum { ignore, fast, slow, warn } |
4295 | cv_check = (abi_version_crosses (19) ? slow |
4296 | : abi_version_at_least (19) ? fast |
4297 | : ignore); |
4298 | for (t = (tree) n->value; t; t = TREE_CHAIN (t)) |
4299 | { |
4300 | tree elt = TREE_VALUE (t); |
4301 | |
4302 | if (same_type_p (elt, type)) |
4303 | return 1; |
4304 | |
4305 | if (cv_check != ignore |
4306 | && similar_type_p (elt, type)) |
4307 | { |
4308 | if (cv_check == fast) |
4309 | return 1; |
4310 | cv_check = warn; |
4311 | } |
4312 | } |
4313 | |
4314 | if (cv_check == warn) |
4315 | { |
4316 | warning (OPT_Wabi, "layout of %qs member of type %qT changes in %qs" , |
4317 | "[[no_unique_address]]" , type, "-fabi-version=19" ); |
4318 | if (abi_version_at_least (19)) |
4319 | return 1; |
4320 | } |
4321 | |
4322 | return 0; |
4323 | } |
4324 | |
4325 | /* Walk through all the subobjects of TYPE (located at OFFSET). Call |
4326 | F for every subobject, passing it the type, offset, and table of |
4327 | OFFSETS. If VBASES_P is one, then virtual non-primary bases should |
4328 | be traversed. |
4329 | |
4330 | If MAX_OFFSET is non-NULL, then subobjects with an offset greater |
4331 | than MAX_OFFSET will not be walked. |
4332 | |
4333 | If F returns a nonzero value, the traversal ceases, and that value |
4334 | is returned. Otherwise, returns zero. */ |
4335 | |
4336 | static int |
4337 | walk_subobject_offsets (tree type, |
4338 | subobject_offset_fn f, |
4339 | tree offset, |
4340 | splay_tree offsets, |
4341 | tree max_offset, |
4342 | int vbases_p) |
4343 | { |
4344 | int r = 0; |
4345 | tree type_binfo = NULL_TREE; |
4346 | |
4347 | /* If this OFFSET is bigger than the MAX_OFFSET, then we should |
4348 | stop. */ |
4349 | if (max_offset && tree_int_cst_lt (t1: max_offset, t2: offset)) |
4350 | return 0; |
4351 | |
4352 | if (type == error_mark_node) |
4353 | return 0; |
4354 | |
4355 | if (!TYPE_P (type)) |
4356 | { |
4357 | type_binfo = type; |
4358 | type = BINFO_TYPE (type); |
4359 | } |
4360 | |
4361 | if (CLASS_TYPE_P (type)) |
4362 | { |
4363 | tree field; |
4364 | tree binfo; |
4365 | int i; |
4366 | |
4367 | /* Avoid recursing into objects that are not interesting. */ |
4368 | if (!CLASSTYPE_CONTAINS_EMPTY_CLASS_P (type)) |
4369 | return 0; |
4370 | |
4371 | /* Record the location of TYPE. */ |
4372 | r = (*f) (type, offset, offsets); |
4373 | if (r) |
4374 | return r; |
4375 | |
4376 | /* Iterate through the direct base classes of TYPE. */ |
4377 | if (!type_binfo) |
4378 | type_binfo = TYPE_BINFO (type); |
4379 | for (i = 0; BINFO_BASE_ITERATE (type_binfo, i, binfo); i++) |
4380 | { |
4381 | tree binfo_offset; |
4382 | |
4383 | if (BINFO_VIRTUAL_P (binfo)) |
4384 | continue; |
4385 | |
4386 | tree orig_binfo; |
4387 | /* We cannot rely on BINFO_OFFSET being set for the base |
4388 | class yet, but the offsets for direct non-virtual |
4389 | bases can be calculated by going back to the TYPE. */ |
4390 | orig_binfo = BINFO_BASE_BINFO (TYPE_BINFO (type), i); |
4391 | binfo_offset = size_binop (PLUS_EXPR, |
4392 | offset, |
4393 | BINFO_OFFSET (orig_binfo)); |
4394 | |
4395 | r = walk_subobject_offsets (type: binfo, |
4396 | f, |
4397 | offset: binfo_offset, |
4398 | offsets, |
4399 | max_offset, |
4400 | /*vbases_p=*/0); |
4401 | if (r) |
4402 | return r; |
4403 | } |
4404 | |
4405 | if (CLASSTYPE_VBASECLASSES (type)) |
4406 | { |
4407 | unsigned ix; |
4408 | vec<tree, va_gc> *vbases; |
4409 | |
4410 | /* Iterate through the virtual base classes of TYPE. In G++ |
4411 | 3.2, we included virtual bases in the direct base class |
4412 | loop above, which results in incorrect results; the |
4413 | correct offsets for virtual bases are only known when |
4414 | working with the most derived type. */ |
4415 | if (vbases_p) |
4416 | for (vbases = CLASSTYPE_VBASECLASSES (type), ix = 0; |
4417 | vec_safe_iterate (v: vbases, ix, ptr: &binfo); ix++) |
4418 | { |
4419 | r = walk_subobject_offsets (type: binfo, |
4420 | f, |
4421 | size_binop (PLUS_EXPR, |
4422 | offset, |
4423 | BINFO_OFFSET (binfo)), |
4424 | offsets, |
4425 | max_offset, |
4426 | /*vbases_p=*/0); |
4427 | if (r) |
4428 | return r; |
4429 | } |
4430 | else |
4431 | { |
4432 | /* We still have to walk the primary base, if it is |
4433 | virtual. (If it is non-virtual, then it was walked |
4434 | above.) */ |
4435 | tree vbase = get_primary_binfo (type_binfo); |
4436 | |
4437 | if (vbase && BINFO_VIRTUAL_P (vbase) |
4438 | && BINFO_PRIMARY_P (vbase) |
4439 | && BINFO_INHERITANCE_CHAIN (vbase) == type_binfo) |
4440 | { |
4441 | r = (walk_subobject_offsets |
4442 | (type: vbase, f, offset, |
4443 | offsets, max_offset, /*vbases_p=*/0)); |
4444 | if (r) |
4445 | return r; |
4446 | } |
4447 | } |
4448 | } |
4449 | |
4450 | /* Iterate through the fields of TYPE. */ |
4451 | for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field)) |
4452 | if (TREE_CODE (field) == FIELD_DECL |
4453 | && TREE_TYPE (field) != error_mark_node |
4454 | && !DECL_ARTIFICIAL (field)) |
4455 | { |
4456 | tree field_offset; |
4457 | |
4458 | field_offset = byte_position (field); |
4459 | |
4460 | r = walk_subobject_offsets (TREE_TYPE (field), |
4461 | f, |
4462 | size_binop (PLUS_EXPR, |
4463 | offset, |
4464 | field_offset), |
4465 | offsets, |
4466 | max_offset, |
4467 | /*vbases_p=*/1); |
4468 | if (r) |
4469 | return r; |
4470 | } |
4471 | } |
4472 | else if (TREE_CODE (type) == ARRAY_TYPE) |
4473 | { |
4474 | tree element_type = strip_array_types (type); |
4475 | tree domain = TYPE_DOMAIN (type); |
4476 | tree index; |
4477 | |
4478 | /* Avoid recursing into objects that are not interesting. */ |
4479 | if (!CLASS_TYPE_P (element_type) |
4480 | || !CLASSTYPE_CONTAINS_EMPTY_CLASS_P (element_type) |
4481 | || !domain |
4482 | || integer_minus_onep (TYPE_MAX_VALUE (domain))) |
4483 | return 0; |
4484 | |
4485 | /* Step through each of the elements in the array. */ |
4486 | for (index = size_zero_node; |
4487 | !tree_int_cst_lt (TYPE_MAX_VALUE (domain), t2: index); |
4488 | index = size_binop (PLUS_EXPR, index, size_one_node)) |
4489 | { |
4490 | r = walk_subobject_offsets (TREE_TYPE (type), |
4491 | f, |
4492 | offset, |
4493 | offsets, |
4494 | max_offset, |
4495 | /*vbases_p=*/1); |
4496 | if (r) |
4497 | return r; |
4498 | offset = size_binop (PLUS_EXPR, offset, |
4499 | TYPE_SIZE_UNIT (TREE_TYPE (type))); |
4500 | /* If this new OFFSET is bigger than the MAX_OFFSET, then |
4501 | there's no point in iterating through the remaining |
4502 | elements of the array. */ |
4503 | if (max_offset && tree_int_cst_lt (t1: max_offset, t2: offset)) |
4504 | break; |
4505 | } |
4506 | } |
4507 | |
4508 | return 0; |
4509 | } |
4510 | |
4511 | /* Return true iff FIELD_DECL DECL is potentially overlapping. */ |
4512 | |
4513 | static bool |
4514 | field_poverlapping_p (tree decl) |
4515 | { |
4516 | /* Base fields are actually potentially overlapping, but C++ bases go through |
4517 | a different code path based on binfos, and ObjC++ base fields are laid out |
4518 | in objc-act, so we don't want layout_class_type to mess with them. */ |
4519 | if (DECL_FIELD_IS_BASE (decl)) |
4520 | { |
4521 | gcc_checking_assert (c_dialect_objc ()); |
4522 | return false; |
4523 | } |
4524 | |
4525 | return lookup_attribute (attr_name: "no_unique_address" , |
4526 | DECL_ATTRIBUTES (decl)); |
4527 | } |
4528 | |
4529 | /* Return true iff DECL is an empty field, either for an empty base or a |
4530 | [[no_unique_address]] data member. */ |
4531 | |
4532 | bool |
4533 | is_empty_field (tree decl) |
4534 | { |
4535 | if (!decl || TREE_CODE (decl) != FIELD_DECL) |
4536 | return false; |
4537 | |
4538 | bool r = (is_empty_class (TREE_TYPE (decl)) |
4539 | && (DECL_FIELD_IS_BASE (decl) |
4540 | || field_poverlapping_p (decl))); |
4541 | |
4542 | /* Empty fields should have size zero. */ |
4543 | gcc_checking_assert (!r || integer_zerop (DECL_SIZE (decl))); |
4544 | |
4545 | return r; |
4546 | } |
4547 | |
4548 | /* Record all of the empty subobjects of DECL_OR_BINFO. */ |
4549 | |
4550 | static void |
4551 | record_subobject_offsets (tree decl_or_binfo, |
4552 | splay_tree offsets) |
4553 | { |
4554 | tree type, offset; |
4555 | bool overlapping, vbases_p; |
4556 | |
4557 | if (DECL_P (decl_or_binfo)) |
4558 | { |
4559 | tree decl = decl_or_binfo; |
4560 | type = TREE_TYPE (decl); |
4561 | offset = byte_position (decl); |
4562 | overlapping = field_poverlapping_p (decl); |
4563 | vbases_p = true; |
4564 | } |
4565 | else |
4566 | { |
4567 | type = BINFO_TYPE (decl_or_binfo); |
4568 | offset = BINFO_OFFSET (decl_or_binfo); |
4569 | overlapping = true; |
4570 | vbases_p = false; |
4571 | } |
4572 | |
4573 | tree max_offset; |
4574 | /* If recording subobjects for a non-static data member or a |
4575 | non-empty base class, we do not need to record offsets beyond |
4576 | the size of the biggest empty class. Additional data members |
4577 | will go at the end of the class. Additional base classes will go |
4578 | either at offset zero (if empty, in which case they cannot |
4579 | overlap with offsets past the size of the biggest empty class) or |
4580 | at the end of the class. |
4581 | |
4582 | However, if we are placing an empty base class, then we must record |
4583 | all offsets, as either the empty class is at offset zero (where |
4584 | other empty classes might later be placed) or at the end of the |
4585 | class (where other objects might then be placed, so other empty |
4586 | subobjects might later overlap). */ |
4587 | if (!overlapping |
4588 | || !is_empty_class (type)) |
4589 | max_offset = sizeof_biggest_empty_class; |
4590 | else |
4591 | max_offset = NULL_TREE; |
4592 | walk_subobject_offsets (type, f: record_subobject_offset, offset, |
4593 | offsets, max_offset, vbases_p); |
4594 | } |
4595 | |
4596 | /* Returns nonzero if any of the empty subobjects of TYPE (located at |
4597 | OFFSET) conflict with entries in OFFSETS. If VBASES_P is nonzero, |
4598 | virtual bases of TYPE are examined. */ |
4599 | |
4600 | static int |
4601 | layout_conflict_p (tree type, |
4602 | tree offset, |
4603 | splay_tree offsets, |
4604 | int vbases_p) |
4605 | { |
4606 | splay_tree_node max_node; |
4607 | |
4608 | /* Get the node in OFFSETS that indicates the maximum offset where |
4609 | an empty subobject is located. */ |
4610 | max_node = splay_tree_max (offsets); |
4611 | /* If there aren't any empty subobjects, then there's no point in |
4612 | performing this check. */ |
4613 | if (!max_node) |
4614 | return 0; |
4615 | |
4616 | return walk_subobject_offsets (type, f: check_subobject_offset, offset, |
4617 | offsets, max_offset: (tree) (max_node->key), |
4618 | vbases_p); |
4619 | } |
4620 | |
4621 | /* DECL is a FIELD_DECL corresponding either to a base subobject of a |
4622 | non-static data member of the type indicated by RLI. BINFO is the |
4623 | binfo corresponding to the base subobject, OFFSETS maps offsets to |
4624 | types already located at those offsets. This function determines |
4625 | the position of the DECL. */ |
4626 | |
4627 | static void |
4628 | layout_nonempty_base_or_field (record_layout_info rli, |
4629 | tree decl, |
4630 | tree binfo, |
4631 | splay_tree offsets) |
4632 | { |
4633 | tree offset = NULL_TREE; |
4634 | bool field_p; |
4635 | tree type; |
4636 | |
4637 | if (binfo) |
4638 | { |
4639 | /* For the purposes of determining layout conflicts, we want to |
4640 | use the class type of BINFO; TREE_TYPE (DECL) will be the |
4641 | CLASSTYPE_AS_BASE version, which does not contain entries for |
4642 | zero-sized bases. */ |
4643 | type = TREE_TYPE (binfo); |
4644 | field_p = false; |
4645 | } |
4646 | else |
4647 | { |
4648 | type = TREE_TYPE (decl); |
4649 | field_p = true; |
4650 | } |
4651 | |
4652 | /* Try to place the field. It may take more than one try if we have |
4653 | a hard time placing the field without putting two objects of the |
4654 | same type at the same address. */ |
4655 | while (1) |
4656 | { |
4657 | struct record_layout_info_s old_rli = *rli; |
4658 | |
4659 | /* Place this field. */ |
4660 | place_field (rli, decl); |
4661 | offset = byte_position (decl); |
4662 | |
4663 | /* We have to check to see whether or not there is already |
4664 | something of the same type at the offset we're about to use. |
4665 | For example, consider: |
4666 | |
4667 | struct S {}; |
4668 | struct T : public S { int i; }; |
4669 | struct U : public S, public T {}; |
4670 | |
4671 | Here, we put S at offset zero in U. Then, we can't put T at |
4672 | offset zero -- its S component would be at the same address |
4673 | as the S we already allocated. So, we have to skip ahead. |
4674 | Since all data members, including those whose type is an |
4675 | empty class, have nonzero size, any overlap can happen only |
4676 | with a direct or indirect base-class -- it can't happen with |
4677 | a data member. */ |
4678 | /* In a union, overlap is permitted; all members are placed at |
4679 | offset zero. */ |
4680 | if (TREE_CODE (rli->t) == UNION_TYPE) |
4681 | break; |
4682 | if (layout_conflict_p (type: field_p ? type : binfo, offset, |
4683 | offsets, vbases_p: field_p)) |
4684 | { |
4685 | /* Strip off the size allocated to this field. That puts us |
4686 | at the first place we could have put the field with |
4687 | proper alignment. */ |
4688 | *rli = old_rli; |
4689 | |
4690 | /* Bump up by the alignment required for the type. */ |
4691 | rli->bitpos |
4692 | = size_binop (PLUS_EXPR, rli->bitpos, |
4693 | bitsize_int (binfo |
4694 | ? CLASSTYPE_ALIGN (type) |
4695 | : TYPE_ALIGN (type))); |
4696 | normalize_rli (rli); |
4697 | } |
4698 | else if (TREE_CODE (type) == NULLPTR_TYPE |
4699 | && warn_abi && abi_version_crosses (9)) |
4700 | { |
4701 | /* Before ABI v9, we were giving nullptr_t alignment of 1; if |
4702 | the offset wasn't aligned like a pointer when we started to |
4703 | layout this field, that affects its position. */ |
4704 | tree pos = rli_size_unit_so_far (&old_rli); |
4705 | if (int_cst_value (pos) % TYPE_ALIGN_UNIT (ptr_type_node) != 0) |
4706 | { |
4707 | if (abi_version_at_least (9)) |
4708 | warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wabi, |
4709 | "alignment of %qD increased in %<-fabi-version=9%> " |
4710 | "(GCC 5.2)" , decl); |
4711 | else |
4712 | warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wabi, "alignment " |
4713 | "of %qD will increase in %<-fabi-version=9%>" , |
4714 | decl); |
4715 | } |
4716 | break; |
4717 | } |
4718 | else |
4719 | /* There was no conflict. We're done laying out this field. */ |
4720 | break; |
4721 | } |
4722 | |
4723 | /* Now that we know where it will be placed, update its |
4724 | BINFO_OFFSET. */ |
4725 | if (binfo && CLASS_TYPE_P (BINFO_TYPE (binfo))) |
4726 | /* Indirect virtual bases may have a nonzero BINFO_OFFSET at |
4727 | this point because their BINFO_OFFSET is copied from another |
4728 | hierarchy. Therefore, we may not need to add the entire |
4729 | OFFSET. */ |
4730 | propagate_binfo_offsets (binfo, |
4731 | size_diffop_loc (input_location, |
4732 | fold_convert (ssizetype, offset), |
4733 | fold_convert (ssizetype, |
4734 | BINFO_OFFSET (binfo)))); |
4735 | } |
4736 | |
4737 | /* Returns true if TYPE is empty and OFFSET is nonzero. */ |
4738 | |
4739 | static int |
4740 | empty_base_at_nonzero_offset_p (tree type, |
4741 | tree offset, |
4742 | splay_tree /*offsets*/) |
4743 | { |
4744 | return is_empty_class (type) && !integer_zerop (offset); |
4745 | } |
4746 | |
4747 | /* Layout the empty base BINFO. EOC indicates the byte currently just |
4748 | past the end of the class, and should be correctly aligned for a |
4749 | class of the type indicated by BINFO; OFFSETS gives the offsets of |
4750 | the empty bases allocated so far. T is the most derived |
4751 | type. Return nonzero iff we added it at the end. */ |
4752 | |
4753 | static bool |
4754 | layout_empty_base_or_field (record_layout_info rli, tree binfo_or_decl, |
4755 | splay_tree offsets) |
4756 | { |
4757 | tree alignment; |
4758 | bool atend = false; |
4759 | tree binfo = NULL_TREE; |
4760 | tree decl = NULL_TREE; |
4761 | tree type; |
4762 | if (TREE_CODE (binfo_or_decl) == TREE_BINFO) |
4763 | { |
4764 | binfo = binfo_or_decl; |
4765 | type = BINFO_TYPE (binfo); |
4766 | } |
4767 | else |
4768 | { |
4769 | decl = binfo_or_decl; |
4770 | type = TREE_TYPE (decl); |
4771 | } |
4772 | |
4773 | /* On some platforms (ARM), even empty classes will not be |
4774 | byte-aligned. */ |
4775 | tree eoc = round_up_loc (input_location, |
4776 | rli_size_unit_so_far (rli), |
4777 | CLASSTYPE_ALIGN_UNIT (type)); |
4778 | |
4779 | /* This routine should only be used for empty classes. */ |
4780 | gcc_assert (is_empty_class (type)); |
4781 | |
4782 | if (decl && DECL_USER_ALIGN (decl)) |
4783 | alignment = size_int (DECL_ALIGN_UNIT (decl)); |
4784 | else |
4785 | alignment = size_int (CLASSTYPE_ALIGN_UNIT (type)); |
4786 | |
4787 | /* This is an empty base class. We first try to put it at offset |
4788 | zero. */ |
4789 | tree offset = size_zero_node; |
4790 | if (TREE_CODE (rli->t) != UNION_TYPE |
4791 | && layout_conflict_p (type, |
4792 | offset, |
4793 | offsets, |
4794 | /*vbases_p=*/0)) |
4795 | { |
4796 | /* That didn't work. Now, we move forward from the next |
4797 | available spot in the class. */ |
4798 | atend = true; |
4799 | offset = eoc; |
4800 | while (1) |
4801 | { |
4802 | if (!layout_conflict_p (type, |
4803 | offset, |
4804 | offsets, |
4805 | /*vbases_p=*/0)) |
4806 | /* We finally found a spot where there's no overlap. */ |
4807 | break; |
4808 | |
4809 | /* There's overlap here, too. Bump along to the next spot. */ |
4810 | offset = size_binop (PLUS_EXPR, offset, alignment); |
4811 | } |
4812 | } |
4813 | |
4814 | if (decl && DECL_USER_ALIGN (decl)) |
4815 | { |
4816 | rli->record_align = MAX (rli->record_align, DECL_ALIGN (decl)); |
4817 | if (warn_packed) |
4818 | rli->unpacked_align = MAX (rli->unpacked_align, DECL_ALIGN (decl)); |
4819 | TYPE_USER_ALIGN (rli->t) = 1; |
4820 | } |
4821 | else if (CLASSTYPE_USER_ALIGN (type)) |
4822 | { |
4823 | rli->record_align = MAX (rli->record_align, CLASSTYPE_ALIGN (type)); |
4824 | if (warn_packed) |
4825 | rli->unpacked_align = MAX (rli->unpacked_align, CLASSTYPE_ALIGN (type)); |
4826 | TYPE_USER_ALIGN (rli->t) = 1; |
4827 | } |
4828 | |
4829 | if (binfo) |
4830 | /* Adjust BINFO_OFFSET (binfo) to be exactly OFFSET. */ |
4831 | propagate_binfo_offsets (binfo, |
4832 | size_diffop (offset, BINFO_OFFSET (binfo))); |
4833 | else |
4834 | { |
4835 | DECL_FIELD_OFFSET (decl) = offset; |
4836 | DECL_FIELD_BIT_OFFSET (decl) = bitsize_zero_node; |
4837 | SET_DECL_OFFSET_ALIGN (decl, BITS_PER_UNIT); |
4838 | } |
4839 | |
4840 | return atend; |
4841 | } |
4842 | |
4843 | /* Build the FIELD_DECL for BASETYPE as a base of T, add it to the chain of |
4844 | fields at NEXT_FIELD, and return it. */ |
4845 | |
4846 | static tree |
4847 | build_base_field_1 (tree t, tree binfo, tree access, tree *&next_field) |
4848 | { |
4849 | /* Create the FIELD_DECL. */ |
4850 | tree basetype = BINFO_TYPE (binfo); |
4851 | tree as_base = CLASSTYPE_AS_BASE (basetype); |
4852 | gcc_assert (as_base); |
4853 | tree decl = build_decl (input_location, FIELD_DECL, NULL_TREE, as_base); |
4854 | |
4855 | DECL_ARTIFICIAL (decl) = 1; |
4856 | DECL_IGNORED_P (decl) = 1; |
4857 | DECL_FIELD_CONTEXT (decl) = t; |
4858 | if (is_empty_class (basetype)) |
4859 | /* CLASSTYPE_SIZE is one byte, but the field needs to have size zero. */ |
4860 | DECL_SIZE (decl) = DECL_SIZE_UNIT (decl) = size_zero_node; |
4861 | else |
4862 | { |
4863 | DECL_SIZE (decl) = CLASSTYPE_SIZE (basetype); |
4864 | DECL_SIZE_UNIT (decl) = CLASSTYPE_SIZE_UNIT (basetype); |
4865 | } |
4866 | SET_DECL_ALIGN (decl, CLASSTYPE_ALIGN (basetype)); |
4867 | DECL_USER_ALIGN (decl) = CLASSTYPE_USER_ALIGN (basetype); |
4868 | SET_DECL_MODE (decl, TYPE_MODE (basetype)); |
4869 | DECL_FIELD_IS_BASE (decl) = 1; |
4870 | |
4871 | if (access == access_private_node) |
4872 | TREE_PRIVATE (decl) = true; |
4873 | else if (access == access_protected_node) |
4874 | TREE_PROTECTED (decl) = true; |
4875 | |
4876 | /* Add the new FIELD_DECL to the list of fields for T. */ |
4877 | DECL_CHAIN (decl) = *next_field; |
4878 | *next_field = decl; |
4879 | next_field = &DECL_CHAIN (decl); |
4880 | |
4881 | return decl; |
4882 | } |
4883 | |
4884 | /* Layout the base given by BINFO in the class indicated by RLI. |
4885 | *BASE_ALIGN is a running maximum of the alignments of |
4886 | any base class. OFFSETS gives the location of empty base |
4887 | subobjects. T is the most derived type. Return nonzero if the new |
4888 | object cannot be nearly-empty. A new FIELD_DECL is inserted at |
4889 | *NEXT_FIELD, unless BINFO is for an empty base class. |
4890 | |
4891 | Returns the location at which the next field should be inserted. */ |
4892 | |
4893 | static tree * |
4894 | build_base_field (record_layout_info rli, tree binfo, tree access, |
4895 | splay_tree offsets, tree *next_field) |
4896 | { |
4897 | tree t = rli->t; |
4898 | tree basetype = BINFO_TYPE (binfo); |
4899 | |
4900 | if (!COMPLETE_TYPE_P (basetype)) |
4901 | /* This error is now reported in xref_tag, thus giving better |
4902 | location information. */ |
4903 | return next_field; |
4904 | |
4905 | /* Place the base class. */ |
4906 | if (!is_empty_class (basetype)) |
4907 | { |
4908 | tree decl; |
4909 | |
4910 | /* The containing class is non-empty because it has a non-empty |
4911 | base class. */ |
4912 | CLASSTYPE_EMPTY_P (t) = 0; |
4913 | |
4914 | /* Create the FIELD_DECL. */ |
4915 | decl = build_base_field_1 (t, binfo, access, next_field); |
4916 | |
4917 | /* Try to place the field. It may take more than one try if we |
4918 | have a hard time placing the field without putting two |
4919 | objects of the same type at the same address. */ |
4920 | layout_nonempty_base_or_field (rli, decl, binfo, offsets); |
4921 | } |
4922 | else |
4923 | { |
4924 | bool atend = layout_empty_base_or_field (rli, binfo_or_decl: binfo, offsets); |
4925 | /* A nearly-empty class "has no proper base class that is empty, |
4926 | not morally virtual, and at an offset other than zero." */ |
4927 | if (!BINFO_VIRTUAL_P (binfo) && CLASSTYPE_NEARLY_EMPTY_P (t)) |
4928 | { |
4929 | if (atend) |
4930 | CLASSTYPE_NEARLY_EMPTY_P (t) = 0; |
4931 | /* The check above (used in G++ 3.2) is insufficient because |
4932 | an empty class placed at offset zero might itself have an |
4933 | empty base at a nonzero offset. */ |
4934 | else if (walk_subobject_offsets (type: basetype, |
4935 | f: empty_base_at_nonzero_offset_p, |
4936 | size_zero_node, |
4937 | /*offsets=*/NULL, |
4938 | /*max_offset=*/NULL_TREE, |
4939 | /*vbases_p=*/true)) |
4940 | CLASSTYPE_NEARLY_EMPTY_P (t) = 0; |
4941 | } |
4942 | |
4943 | /* We used to not create a FIELD_DECL for empty base classes because of |
4944 | back end issues with overlapping FIELD_DECLs, but that doesn't seem to |
4945 | be a problem anymore. We need them to handle initialization of C++17 |
4946 | aggregate bases. */ |
4947 | if (cxx_dialect >= cxx17 && !BINFO_VIRTUAL_P (binfo)) |
4948 | { |
4949 | tree decl = build_base_field_1 (t, binfo, access, next_field); |
4950 | DECL_FIELD_OFFSET (decl) = BINFO_OFFSET (binfo); |
4951 | DECL_FIELD_BIT_OFFSET (decl) = bitsize_zero_node; |
4952 | SET_DECL_OFFSET_ALIGN (decl, BITS_PER_UNIT); |
4953 | SET_DECL_FIELD_ABI_IGNORED (decl, 1); |
4954 | } |
4955 | |
4956 | /* An empty virtual base causes a class to be non-empty |
4957 | -- but in that case we do not need to clear CLASSTYPE_EMPTY_P |
4958 | here because that was already done when the virtual table |
4959 | pointer was created. */ |
4960 | } |
4961 | |
4962 | /* Record the offsets of BINFO and its base subobjects. */ |
4963 | record_subobject_offsets (decl_or_binfo: binfo, offsets); |
4964 | |
4965 | return next_field; |
4966 | } |
4967 | |
4968 | /* Layout all of the non-virtual base classes. Record empty |
4969 | subobjects in OFFSETS. T is the most derived type. Return nonzero |
4970 | if the type cannot be nearly empty. The fields created |
4971 | corresponding to the base classes will be inserted at |
4972 | *NEXT_FIELD. */ |
4973 | |
4974 | static void |
4975 | build_base_fields (record_layout_info rli, |
4976 | splay_tree offsets, tree *next_field) |
4977 | { |
4978 | /* Chain to hold all the new FIELD_DECLs which stand in for base class |
4979 | subobjects. */ |
4980 | tree t = rli->t; |
4981 | tree binfo = TYPE_BINFO (t); |
4982 | int n_baseclasses = BINFO_N_BASE_BINFOS (binfo); |
4983 | |
4984 | /* The primary base class is always allocated first. */ |
4985 | const tree primary_binfo = CLASSTYPE_PRIMARY_BINFO (t); |
4986 | if (primary_binfo) |
4987 | { |
4988 | /* We need to walk BINFO_BASE_BINFO to find the access of the primary |
4989 | base, if it is direct. Indirect base fields are private. */ |
4990 | tree primary_access = access_private_node; |
4991 | for (int i = 0; i < n_baseclasses; ++i) |
4992 | { |
4993 | tree base_binfo = BINFO_BASE_BINFO (binfo, i); |
4994 | if (base_binfo == primary_binfo) |
4995 | { |
4996 | primary_access = BINFO_BASE_ACCESS (binfo, i); |
4997 | break; |
4998 | } |
4999 | } |
5000 | next_field = build_base_field (rli, binfo: primary_binfo, |
5001 | access: primary_access, |
5002 | offsets, next_field); |
5003 | } |
5004 | |
5005 | /* Now allocate the rest of the bases. */ |
5006 | for (int i = 0; i < n_baseclasses; ++i) |
5007 | { |
5008 | tree base_binfo = BINFO_BASE_BINFO (binfo, i); |
5009 | |
5010 | /* The primary base was already allocated above, so we don't |
5011 | need to allocate it again here. */ |
5012 | if (base_binfo == primary_binfo) |
5013 | continue; |
5014 | |
5015 | /* Virtual bases are added at the end (a primary virtual base |
5016 | will have already been added). */ |
5017 | if (BINFO_VIRTUAL_P (base_binfo)) |
5018 | continue; |
5019 | |
5020 | next_field = build_base_field (rli, binfo: base_binfo, |
5021 | BINFO_BASE_ACCESS (binfo, i), |
5022 | offsets, next_field); |
5023 | } |
5024 | } |
5025 | |
5026 | /* Go through the TYPE_FIELDS of T issuing any appropriate |
5027 | diagnostics, figuring out which methods override which other |
5028 | methods, and so forth. */ |
5029 | |
5030 | static void |
5031 | check_methods (tree t) |
5032 | { |
5033 | for (tree x = TYPE_FIELDS (t); x; x = DECL_CHAIN (x)) |
5034 | if (DECL_DECLARES_FUNCTION_P (x)) |
5035 | { |
5036 | check_for_override (decl: x, ctype: t); |
5037 | |
5038 | if (DECL_PURE_VIRTUAL_P (x) |
5039 | && (TREE_CODE (x) != FUNCTION_DECL || ! DECL_VINDEX (x))) |
5040 | error ("initializer specified for non-virtual method %q+D" , x); |
5041 | /* The name of the field is the original field name |
5042 | Save this in auxiliary field for later overloading. */ |
5043 | if (TREE_CODE (x) == FUNCTION_DECL && DECL_VINDEX (x)) |
5044 | { |
5045 | TYPE_POLYMORPHIC_P (t) = 1; |
5046 | if (DECL_PURE_VIRTUAL_P (x)) |
5047 | vec_safe_push (CLASSTYPE_PURE_VIRTUALS (t), obj: x); |
5048 | } |
5049 | |
5050 | if (!DECL_VIRTUAL_P (x) |
5051 | && lookup_attribute (attr_name: "transaction_safe_dynamic" , |
5052 | DECL_ATTRIBUTES (x))) |
5053 | error_at (DECL_SOURCE_LOCATION (x), |
5054 | "%<transaction_safe_dynamic%> may only be specified for " |
5055 | "a virtual function" ); |
5056 | } |
5057 | |
5058 | /* Check whether the eligible special member functions (P0848) are |
5059 | user-provided. add_method arranged that the CLASSTYPE_MEMBER_VEC only |
5060 | has the eligible ones, unless none are eligible; TYPE_FIELDS also contains |
5061 | ineligible overloads, which is why this needs to be separate from the loop |
5062 | above. */ |
5063 | |
5064 | if (tree dtor = CLASSTYPE_DESTRUCTOR (t)) |
5065 | { |
5066 | if (TREE_CODE (dtor) == OVERLOAD) |
5067 | { |
5068 | /* P0848: At the end of the definition of a class, overload |
5069 | resolution is performed among the prospective destructors declared |
5070 | in that class with an empty argument list to select the destructor |
5071 | for the class, also known as the selected destructor. The program |
5072 | is ill-formed if overload resolution fails. */ |
5073 | int viable = 0; |
5074 | for (tree fn : ovl_range (dtor)) |
5075 | if (constraints_satisfied_p (fn)) |
5076 | ++viable; |
5077 | gcc_checking_assert (viable != 1); |
5078 | |
5079 | auto_diagnostic_group d; |
5080 | if (viable == 0) |
5081 | error_at (location_of (t), "no viable destructor for %qT" , t); |
5082 | else |
5083 | error_at (location_of (t), "destructor for %qT is ambiguous" , t); |
5084 | print_candidates (dtor); |
5085 | |
5086 | /* Arbitrarily prune the overload set to a single function for |
5087 | sake of error recovery. */ |
5088 | tree *slot = find_member_slot (klass: t, dtor_identifier); |
5089 | *slot = get_first_fn (dtor); |
5090 | } |
5091 | else if (user_provided_p (dtor)) |
5092 | TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t) = true; |
5093 | } |
5094 | |
5095 | for (tree fn : ovl_range (CLASSTYPE_CONSTRUCTORS (t))) |
5096 | { |
5097 | if (!user_provided_p (fn)) |
5098 | /* Might be trivial. */; |
5099 | else if (TREE_CODE (fn) == TEMPLATE_DECL) |
5100 | /* Templates are never special members. */; |
5101 | else if (copy_fn_p (fn) |
5102 | && constraints_satisfied_p (fn)) |
5103 | TYPE_HAS_COMPLEX_COPY_CTOR (t) = true; |
5104 | else if (move_fn_p (fn) |
5105 | && constraints_satisfied_p (fn)) |
5106 | TYPE_HAS_COMPLEX_MOVE_CTOR (t) = true; |
5107 | } |
5108 | |
5109 | for (tree fn : ovl_range (get_class_binding_direct (t, assign_op_identifier))) |
5110 | { |
5111 | if (!user_provided_p (fn)) |
5112 | /* Might be trivial. */; |
5113 | else if (TREE_CODE (fn) == TEMPLATE_DECL) |
5114 | /* Templates are never special members. */; |
5115 | else if (copy_fn_p (fn) |
5116 | && constraints_satisfied_p (fn)) |
5117 | TYPE_HAS_COMPLEX_COPY_ASSIGN (t) = true; |
5118 | else if (move_fn_p (fn) |
5119 | && constraints_satisfied_p (fn)) |
5120 | TYPE_HAS_COMPLEX_MOVE_ASSIGN (t) = true; |
5121 | } |
5122 | } |
5123 | |
5124 | /* FN is constructor, destructor or operator function. Clone the |
5125 | declaration to create a NAME'd variant. NEED_VTT_PARM_P and |
5126 | OMIT_INHERITED_PARMS_P are relevant if it's a cdtor. */ |
5127 | |
5128 | static tree |
5129 | copy_fndecl_with_name (tree fn, tree name, tree_code code, |
5130 | bool need_vtt_parm_p, bool omit_inherited_parms_p) |
5131 | { |
5132 | /* Copy the function. */ |
5133 | tree clone = copy_decl (fn); |
5134 | /* Reset the function name. */ |
5135 | DECL_NAME (clone) = name; |
5136 | |
5137 | if (flag_concepts) |
5138 | /* Clone constraints. */ |
5139 | if (tree ci = get_constraints (fn)) |
5140 | set_constraints (clone, copy_node (ci)); |
5141 | |
5142 | SET_DECL_ASSEMBLER_NAME (clone, NULL_TREE); |
5143 | /* There's no pending inline data for this function. */ |
5144 | DECL_PENDING_INLINE_INFO (clone) = NULL; |
5145 | DECL_PENDING_INLINE_P (clone) = 0; |
5146 | |
5147 | if (name == base_dtor_identifier) |
5148 | { |
5149 | /* The base-class destructor is not virtual. */ |
5150 | DECL_VIRTUAL_P (clone) = 0; |
5151 | DECL_VINDEX (clone) = NULL_TREE; |
5152 | } |
5153 | else if (code != ERROR_MARK) |
5154 | { |
5155 | /* Set the operator code. */ |
5156 | const ovl_op_info_t *ovl_op = OVL_OP_INFO (false, code); |
5157 | DECL_OVERLOADED_OPERATOR_CODE_RAW (clone) = ovl_op->ovl_op_code; |
5158 | |
5159 | /* The operator could be virtual. */ |
5160 | if (DECL_VIRTUAL_P (clone)) |
5161 | IDENTIFIER_VIRTUAL_P (name) = true; |
5162 | } |
5163 | |
5164 | if (omit_inherited_parms_p) |
5165 | gcc_assert (DECL_HAS_IN_CHARGE_PARM_P (clone)); |
5166 | |
5167 | /* If there was an in-charge parameter, drop it from the function |
5168 | type. */ |
5169 | if (DECL_HAS_IN_CHARGE_PARM_P (clone)) |
5170 | { |
5171 | tree basetype = TYPE_METHOD_BASETYPE (TREE_TYPE (clone)); |
5172 | tree parmtypes = TYPE_ARG_TYPES (TREE_TYPE (clone)); |
5173 | /* Skip the `this' parameter. */ |
5174 | parmtypes = TREE_CHAIN (parmtypes); |
5175 | /* Skip the in-charge parameter. */ |
5176 | parmtypes = TREE_CHAIN (parmtypes); |
5177 | /* And the VTT parm, in a complete [cd]tor. */ |
5178 | if (DECL_HAS_VTT_PARM_P (fn) && !need_vtt_parm_p) |
5179 | parmtypes = TREE_CHAIN (parmtypes); |
5180 | if (omit_inherited_parms_p) |
5181 | { |
5182 | /* If we're omitting inherited parms, that just leaves the VTT. */ |
5183 | gcc_assert (need_vtt_parm_p); |
5184 | parmtypes = tree_cons (NULL_TREE, vtt_parm_type, void_list_node); |
5185 | } |
5186 | TREE_TYPE (clone) |
5187 | = build_method_type_directly (basetype, |
5188 | TREE_TYPE (TREE_TYPE (clone)), |
5189 | parmtypes); |
5190 | TREE_TYPE (clone) |
5191 | = cp_build_type_attribute_variant (TREE_TYPE (clone), |
5192 | TYPE_ATTRIBUTES (TREE_TYPE (fn))); |
5193 | TREE_TYPE (clone) |
5194 | = cxx_copy_lang_qualifiers (TREE_TYPE (clone), TREE_TYPE (fn)); |
5195 | } |
5196 | |
5197 | /* Copy the function parameters. */ |
5198 | DECL_ARGUMENTS (clone) = copy_list (DECL_ARGUMENTS (clone)); |
5199 | |
5200 | /* Remove the in-charge parameter. */ |
5201 | if (DECL_HAS_IN_CHARGE_PARM_P (clone)) |
5202 | { |
5203 | DECL_CHAIN (DECL_ARGUMENTS (clone)) |
5204 | = DECL_CHAIN (DECL_CHAIN (DECL_ARGUMENTS (clone))); |
5205 | DECL_HAS_IN_CHARGE_PARM_P (clone) = 0; |
5206 | } |
5207 | |
5208 | /* And the VTT parm, in a complete [cd]tor. */ |
5209 | if (DECL_HAS_VTT_PARM_P (fn)) |
5210 | { |
5211 | if (need_vtt_parm_p) |
5212 | DECL_HAS_VTT_PARM_P (clone) = 1; |
5213 | else |
5214 | { |
5215 | DECL_CHAIN (DECL_ARGUMENTS (clone)) |
5216 | = DECL_CHAIN (DECL_CHAIN (DECL_ARGUMENTS (clone))); |
5217 | DECL_HAS_VTT_PARM_P (clone) = 0; |
5218 | } |
5219 | } |
5220 | |
5221 | /* A base constructor inheriting from a virtual base doesn't get the |
5222 | arguments. */ |
5223 | if (omit_inherited_parms_p) |
5224 | DECL_CHAIN (DECL_CHAIN (DECL_ARGUMENTS (clone))) = NULL_TREE; |
5225 | |
5226 | for (tree parms = DECL_ARGUMENTS (clone); parms; parms = DECL_CHAIN (parms)) |
5227 | { |
5228 | DECL_CONTEXT (parms) = clone; |
5229 | cxx_dup_lang_specific_decl (parms); |
5230 | } |
5231 | |
5232 | /* Create the RTL for this function. */ |
5233 | SET_DECL_RTL (clone, NULL); |
5234 | |
5235 | /* Regardless of the current scope, this is a member function, so |
5236 | not at namespace scope. */ |
5237 | rest_of_decl_compilation (clone, /*top_level=*/0, at_eof); |
5238 | |
5239 | return clone; |
5240 | } |
5241 | |
5242 | /* FN is an operator function, create a variant for CODE. */ |
5243 | |
5244 | tree |
5245 | copy_operator_fn (tree fn, tree_code code) |
5246 | { |
5247 | return copy_fndecl_with_name (fn, name: ovl_op_identifier (code), |
5248 | code, need_vtt_parm_p: false, omit_inherited_parms_p: false); |
5249 | } |
5250 | |
5251 | /* FN is a constructor or destructor. Clone the declaration to create |
5252 | a specialized in-charge or not-in-charge version, as indicated by |
5253 | NAME. */ |
5254 | |
5255 | static tree |
5256 | build_clone (tree fn, tree name, bool need_vtt_parm_p, |
5257 | bool omit_inherited_parms_p) |
5258 | { |
5259 | tree clone; |
5260 | |
5261 | /* If this is a template, do the rest on the DECL_TEMPLATE_RESULT. */ |
5262 | if (TREE_CODE (fn) == TEMPLATE_DECL) |
5263 | { |
5264 | clone = copy_decl (fn); |
5265 | DECL_NAME (clone) = name; |
5266 | |
5267 | tree result = build_clone (DECL_TEMPLATE_RESULT (clone), name, |
5268 | need_vtt_parm_p, omit_inherited_parms_p); |
5269 | DECL_TEMPLATE_RESULT (clone) = result; |
5270 | |
5271 | DECL_TEMPLATE_INFO (result) = copy_node (DECL_TEMPLATE_INFO (result)); |
5272 | DECL_TI_TEMPLATE (result) = clone; |
5273 | |
5274 | TREE_TYPE (clone) = TREE_TYPE (result); |
5275 | } |
5276 | else |
5277 | { |
5278 | clone = copy_fndecl_with_name (fn, name, code: ERROR_MARK, |
5279 | need_vtt_parm_p, omit_inherited_parms_p); |
5280 | DECL_CLONED_FUNCTION (clone) = fn; |
5281 | |
5282 | maybe_prepare_return_this (clone); |
5283 | } |
5284 | |
5285 | /* Remember where this function came from. */ |
5286 | DECL_ABSTRACT_ORIGIN (clone) = fn; |
5287 | |
5288 | /* Make it easy to find the CLONE given the FN. Note the |
5289 | template_result of a template will be chained this way too. */ |
5290 | DECL_CHAIN (clone) = DECL_CHAIN (fn); |
5291 | DECL_CHAIN (fn) = clone; |
5292 | |
5293 | return clone; |
5294 | } |
5295 | |
5296 | /* Build the clones of FN, return the number of clones built. These |
5297 | will be inserted onto DECL_CHAIN of FN. */ |
5298 | |
5299 | void |
5300 | build_cdtor_clones (tree fn, bool needs_vtt_p, bool base_omits_inherited_p, |
5301 | bool update_methods) |
5302 | { |
5303 | unsigned count = 0; |
5304 | |
5305 | if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (fn)) |
5306 | { |
5307 | /* For each constructor, we need two variants: an in-charge version |
5308 | and a not-in-charge version. */ |
5309 | build_clone (fn, complete_ctor_identifier, need_vtt_parm_p: false, omit_inherited_parms_p: false); |
5310 | build_clone (fn, base_ctor_identifier, need_vtt_parm_p: needs_vtt_p, |
5311 | omit_inherited_parms_p: base_omits_inherited_p); |
5312 | count += 2; |
5313 | } |
5314 | else |
5315 | { |
5316 | gcc_assert (DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (fn)); |
5317 | |
5318 | /* For each destructor, we need three variants: an in-charge |
5319 | version, a not-in-charge version, and an in-charge deleting |
5320 | version. We clone the deleting version first because that |
5321 | means it will go second on the TYPE_FIELDS list -- and that |
5322 | corresponds to the correct layout order in the virtual |
5323 | function table. |
5324 | |
5325 | For a non-virtual destructor, we do not build a deleting |
5326 | destructor. */ |
5327 | if (DECL_VIRTUAL_P (fn)) |
5328 | { |
5329 | build_clone (fn, deleting_dtor_identifier, need_vtt_parm_p: false, omit_inherited_parms_p: false); |
5330 | count++; |
5331 | } |
5332 | build_clone (fn, complete_dtor_identifier, need_vtt_parm_p: false, omit_inherited_parms_p: false); |
5333 | build_clone (fn, base_dtor_identifier, need_vtt_parm_p: needs_vtt_p, omit_inherited_parms_p: false); |
5334 | count += 2; |
5335 | } |
5336 | |
5337 | /* The original is now an abstract function that is never |
5338 | emitted. */ |
5339 | DECL_ABSTRACT_P (fn) = true; |
5340 | |
5341 | if (update_methods) |
5342 | for (tree clone = fn; count--;) |
5343 | { |
5344 | clone = DECL_CHAIN (clone); |
5345 | add_method (DECL_CONTEXT (clone), method: clone, via_using: false); |
5346 | } |
5347 | } |
5348 | |
5349 | /* Produce declarations for all appropriate clones of FN. If |
5350 | UPDATE_METHODS is true, the clones are added to the |
5351 | CLASSTYPE_MEMBER_VEC. */ |
5352 | |
5353 | void |
5354 | clone_cdtor (tree fn, bool update_methods) |
5355 | { |
5356 | /* Avoid inappropriate cloning. */ |
5357 | if (DECL_CHAIN (fn) |
5358 | && DECL_CLONED_FUNCTION_P (DECL_CHAIN (fn))) |
5359 | return; |
5360 | |
5361 | /* Base cdtors need a vtt parm if there are virtual bases. */ |
5362 | bool vtt = CLASSTYPE_VBASECLASSES (DECL_CONTEXT (fn)); |
5363 | |
5364 | /* Base ctor omits inherited parms it needs a vttparm and inherited |
5365 | from a virtual nase ctor. */ |
5366 | bool base_omits_inherited = (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (fn) |
5367 | && base_ctor_omit_inherited_parms (fn)); |
5368 | |
5369 | build_cdtor_clones (fn, needs_vtt_p: vtt, base_omits_inherited_p: base_omits_inherited, update_methods); |
5370 | } |
5371 | |
5372 | /* DECL is an in charge constructor, which is being defined. This will |
5373 | have had an in class declaration, from whence clones were |
5374 | declared. An out-of-class definition can specify additional default |
5375 | arguments. As it is the clones that are involved in overload |
5376 | resolution, we must propagate the information from the DECL to its |
5377 | clones. */ |
5378 | |
5379 | void |
5380 | adjust_clone_args (tree decl) |
5381 | { |
5382 | tree clone; |
5383 | |
5384 | for (clone = DECL_CHAIN (decl); clone && DECL_CLONED_FUNCTION_P (clone); |
5385 | clone = DECL_CHAIN (clone)) |
5386 | { |
5387 | tree orig_clone_parms = TYPE_ARG_TYPES (TREE_TYPE (clone)); |
5388 | tree orig_decl_parms = TYPE_ARG_TYPES (TREE_TYPE (decl)); |
5389 | tree decl_parms, clone_parms; |
5390 | |
5391 | /* Skip the 'this' parameter. */ |
5392 | orig_clone_parms = TREE_CHAIN (orig_clone_parms); |
5393 | orig_decl_parms = TREE_CHAIN (orig_decl_parms); |
5394 | |
5395 | if (DECL_HAS_IN_CHARGE_PARM_P (decl)) |
5396 | orig_decl_parms = TREE_CHAIN (orig_decl_parms); |
5397 | if (DECL_HAS_VTT_PARM_P (decl)) |
5398 | orig_decl_parms = TREE_CHAIN (orig_decl_parms); |
5399 | |
5400 | clone_parms = orig_clone_parms; |
5401 | if (DECL_HAS_VTT_PARM_P (clone)) |
5402 | clone_parms = TREE_CHAIN (clone_parms); |
5403 | |
5404 | for (decl_parms = orig_decl_parms; decl_parms; |
5405 | decl_parms = TREE_CHAIN (decl_parms), |
5406 | clone_parms = TREE_CHAIN (clone_parms)) |
5407 | { |
5408 | if (clone_parms == void_list_node) |
5409 | { |
5410 | gcc_assert (decl_parms == clone_parms |
5411 | || ctor_omit_inherited_parms (clone)); |
5412 | break; |
5413 | } |
5414 | |
5415 | gcc_checking_assert (same_type_p (TREE_VALUE (decl_parms), |
5416 | TREE_VALUE (clone_parms))); |
5417 | |
5418 | if (TREE_PURPOSE (decl_parms) && !TREE_PURPOSE (clone_parms)) |
5419 | { |
5420 | /* A default parameter has been added. Adjust the |
5421 | clone's parameters. */ |
5422 | clone_parms = orig_decl_parms; |
5423 | |
5424 | if (DECL_HAS_VTT_PARM_P (clone)) |
5425 | { |
5426 | clone_parms = tree_cons (TREE_PURPOSE (orig_clone_parms), |
5427 | TREE_VALUE (orig_clone_parms), |
5428 | clone_parms); |
5429 | TREE_TYPE (clone_parms) = TREE_TYPE (orig_clone_parms); |
5430 | } |
5431 | |
5432 | tree basetype = TYPE_METHOD_BASETYPE (TREE_TYPE (clone)); |
5433 | tree type |
5434 | = build_method_type_directly (basetype, |
5435 | TREE_TYPE (TREE_TYPE (clone)), |
5436 | clone_parms); |
5437 | if (tree attrs = TYPE_ATTRIBUTES (TREE_TYPE (clone))) |
5438 | type = cp_build_type_attribute_variant (type, attrs); |
5439 | type = cxx_copy_lang_qualifiers (type, TREE_TYPE (clone)); |
5440 | TREE_TYPE (clone) = type; |
5441 | |
5442 | clone_parms = NULL_TREE; |
5443 | break; |
5444 | } |
5445 | } |
5446 | gcc_assert (!clone_parms || clone_parms == void_list_node); |
5447 | } |
5448 | } |
5449 | |
5450 | /* For each of the constructors and destructors in T, create an |
5451 | in-charge and not-in-charge variant. */ |
5452 | |
5453 | static void |
5454 | clone_constructors_and_destructors (tree t) |
5455 | { |
5456 | /* We do not need to propagate the usingness to the clone, at this |
5457 | point that is not needed. */ |
5458 | for (tree fn : ovl_range (CLASSTYPE_CONSTRUCTORS (t))) |
5459 | clone_cdtor (fn, /*update_methods=*/true); |
5460 | |
5461 | if (tree dtor = CLASSTYPE_DESTRUCTOR (t)) |
5462 | clone_cdtor (fn: dtor, /*update_methods=*/true); |
5463 | } |
5464 | |
5465 | /* Deduce noexcept for a destructor DTOR. */ |
5466 | |
5467 | void |
5468 | deduce_noexcept_on_destructor (tree dtor) |
5469 | { |
5470 | if (!TYPE_RAISES_EXCEPTIONS (TREE_TYPE (dtor))) |
5471 | TREE_TYPE (dtor) = build_exception_variant (TREE_TYPE (dtor), |
5472 | noexcept_deferred_spec); |
5473 | } |
5474 | |
5475 | /* Subroutine of set_one_vmethod_tm_attributes. Search base classes |
5476 | of TYPE for virtual functions which FNDECL overrides. Return a |
5477 | mask of the tm attributes found therein. */ |
5478 | |
5479 | static int |
5480 | look_for_tm_attr_overrides (tree type, tree fndecl) |
5481 | { |
5482 | tree binfo = TYPE_BINFO (type); |
5483 | tree base_binfo; |
5484 | int ix, found = 0; |
5485 | |
5486 | for (ix = 0; BINFO_BASE_ITERATE (binfo, ix, base_binfo); ++ix) |
5487 | { |
5488 | tree o, basetype = BINFO_TYPE (base_binfo); |
5489 | |
5490 | if (!TYPE_POLYMORPHIC_P (basetype)) |
5491 | continue; |
5492 | |
5493 | o = look_for_overrides_here (basetype, fndecl); |
5494 | if (o) |
5495 | { |
5496 | if (lookup_attribute (attr_name: "transaction_safe_dynamic" , |
5497 | DECL_ATTRIBUTES (o))) |
5498 | /* transaction_safe_dynamic is not inherited. */; |
5499 | else |
5500 | found |= tm_attr_to_mask (find_tm_attribute |
5501 | (TYPE_ATTRIBUTES (TREE_TYPE (o)))); |
5502 | } |
5503 | else |
5504 | found |= look_for_tm_attr_overrides (type: basetype, fndecl); |
5505 | } |
5506 | |
5507 | return found; |
5508 | } |
5509 | |
5510 | /* Subroutine of set_method_tm_attributes. Handle the checks and |
5511 | inheritance for one virtual method FNDECL. */ |
5512 | |
5513 | static void |
5514 | set_one_vmethod_tm_attributes (tree type, tree fndecl) |
5515 | { |
5516 | tree tm_attr; |
5517 | int found, have; |
5518 | |
5519 | found = look_for_tm_attr_overrides (type, fndecl); |
5520 | |
5521 | /* If FNDECL doesn't actually override anything (i.e. T is the |
5522 | class that first declares FNDECL virtual), then we're done. */ |
5523 | if (found == 0) |
5524 | return; |
5525 | |
5526 | tm_attr = find_tm_attribute (TYPE_ATTRIBUTES (TREE_TYPE (fndecl))); |
5527 | have = tm_attr_to_mask (tm_attr); |
5528 | |
5529 | /* Intel STM Language Extension 3.0, Section 4.2 table 4: |
5530 | tm_pure must match exactly, otherwise no weakening of |
5531 | tm_safe > tm_callable > nothing. */ |
5532 | /* ??? The tm_pure attribute didn't make the transition to the |
5533 | multivendor language spec. */ |
5534 | if (have == TM_ATTR_PURE) |
5535 | { |
5536 | if (found != TM_ATTR_PURE) |
5537 | { |
5538 | found &= -found; |
5539 | goto err_override; |
5540 | } |
5541 | } |
5542 | /* If the overridden function is tm_pure, then FNDECL must be. */ |
5543 | else if (found == TM_ATTR_PURE && tm_attr) |
5544 | goto err_override; |
5545 | /* Look for base class combinations that cannot be satisfied. */ |
5546 | else if (found != TM_ATTR_PURE && (found & TM_ATTR_PURE)) |
5547 | { |
5548 | found &= ~TM_ATTR_PURE; |
5549 | found &= -found; |
5550 | error_at (DECL_SOURCE_LOCATION (fndecl), |
5551 | "method overrides both %<transaction_pure%> and %qE methods" , |
5552 | tm_mask_to_attr (found)); |
5553 | } |
5554 | /* If FNDECL did not declare an attribute, then inherit the most |
5555 | restrictive one. */ |
5556 | else if (tm_attr == NULL) |
5557 | { |
5558 | apply_tm_attr (fndecl, tm_mask_to_attr (least_bit_hwi (x: found))); |
5559 | } |
5560 | /* Otherwise validate that we're not weaker than a function |
5561 | that is being overridden. */ |
5562 | else |
5563 | { |
5564 | found &= -found; |
5565 | if (found <= TM_ATTR_CALLABLE && have > found) |
5566 | goto err_override; |
5567 | } |
5568 | return; |
5569 | |
5570 | err_override: |
5571 | error_at (DECL_SOURCE_LOCATION (fndecl), |
5572 | "method declared %qE overriding %qE method" , |
5573 | tm_attr, tm_mask_to_attr (found)); |
5574 | } |
5575 | |
5576 | /* For each of the methods in T, propagate a class-level tm attribute. */ |
5577 | |
5578 | static void |
5579 | set_method_tm_attributes (tree t) |
5580 | { |
5581 | tree class_tm_attr, fndecl; |
5582 | |
5583 | /* Don't bother collecting tm attributes if transactional memory |
5584 | support is not enabled. */ |
5585 | if (!flag_tm) |
5586 | return; |
5587 | |
5588 | /* Process virtual methods first, as they inherit directly from the |
5589 | base virtual function and also require validation of new attributes. */ |
5590 | if (TYPE_CONTAINS_VPTR_P (t)) |
5591 | { |
5592 | tree vchain; |
5593 | for (vchain = BINFO_VIRTUALS (TYPE_BINFO (t)); vchain; |
5594 | vchain = TREE_CHAIN (vchain)) |
5595 | { |
5596 | fndecl = BV_FN (vchain); |
5597 | if (DECL_THUNK_P (fndecl)) |
5598 | fndecl = THUNK_TARGET (fndecl); |
5599 | set_one_vmethod_tm_attributes (type: t, fndecl); |
5600 | } |
5601 | } |
5602 | |
5603 | /* If the class doesn't have an attribute, nothing more to do. */ |
5604 | class_tm_attr = find_tm_attribute (TYPE_ATTRIBUTES (t)); |
5605 | if (class_tm_attr == NULL) |
5606 | return; |
5607 | |
5608 | /* Any method that does not yet have a tm attribute inherits |
5609 | the one from the class. */ |
5610 | for (fndecl = TYPE_FIELDS (t); fndecl; fndecl = DECL_CHAIN (fndecl)) |
5611 | if (DECL_DECLARES_FUNCTION_P (fndecl) |
5612 | && !find_tm_attribute (TYPE_ATTRIBUTES (TREE_TYPE (fndecl)))) |
5613 | apply_tm_attr (fndecl, class_tm_attr); |
5614 | } |
5615 | |
5616 | /* Returns true if FN is a default constructor. */ |
5617 | |
5618 | bool |
5619 | default_ctor_p (const_tree fn) |
5620 | { |
5621 | return (DECL_CONSTRUCTOR_P (fn) |
5622 | && sufficient_parms_p (FUNCTION_FIRST_USER_PARMTYPE (fn))); |
5623 | } |
5624 | |
5625 | /* Returns true iff class T has a user-provided constructor that can be called |
5626 | with more than zero arguments. */ |
5627 | |
5628 | bool |
5629 | type_has_user_nondefault_constructor (tree t) |
5630 | { |
5631 | if (!TYPE_HAS_USER_CONSTRUCTOR (t)) |
5632 | return false; |
5633 | |
5634 | for (tree fn : ovl_range (CLASSTYPE_CONSTRUCTORS (t))) |
5635 | { |
5636 | if (user_provided_p (fn) |
5637 | && (TREE_CODE (fn) == TEMPLATE_DECL |
5638 | || (skip_artificial_parms_for (fn, DECL_ARGUMENTS (fn)) |
5639 | != NULL_TREE))) |
5640 | return true; |
5641 | } |
5642 | |
5643 | return false; |
5644 | } |
5645 | |
5646 | /* Returns the defaulted constructor if T has one. Otherwise, returns |
5647 | NULL_TREE. */ |
5648 | |
5649 | tree |
5650 | in_class_defaulted_default_constructor (tree t) |
5651 | { |
5652 | if (!TYPE_HAS_USER_CONSTRUCTOR (t)) |
5653 | return NULL_TREE; |
5654 | |
5655 | for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (t)); iter; ++iter) |
5656 | { |
5657 | tree fn = *iter; |
5658 | |
5659 | if (DECL_DEFAULTED_IN_CLASS_P (fn) |
5660 | && default_ctor_p (fn)) |
5661 | return fn; |
5662 | } |
5663 | |
5664 | return NULL_TREE; |
5665 | } |
5666 | |
5667 | /* Returns true iff FN is a user-provided function, i.e. user-declared |
5668 | and not defaulted at its first declaration. */ |
5669 | |
5670 | bool |
5671 | user_provided_p (tree fn) |
5672 | { |
5673 | fn = STRIP_TEMPLATE (fn); |
5674 | return (!DECL_ARTIFICIAL (fn) |
5675 | && !(DECL_INITIALIZED_IN_CLASS_P (fn) |
5676 | && (DECL_DEFAULTED_FN (fn) || DECL_DELETED_FN (fn)))); |
5677 | } |
5678 | |
5679 | /* Returns true iff class T has a user-provided constructor. */ |
5680 | |
5681 | bool |
5682 | type_has_user_provided_constructor (tree t) |
5683 | { |
5684 | if (!CLASS_TYPE_P (t)) |
5685 | return false; |
5686 | |
5687 | if (!TYPE_HAS_USER_CONSTRUCTOR (t)) |
5688 | return false; |
5689 | |
5690 | for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (t)); iter; ++iter) |
5691 | if (user_provided_p (fn: *iter)) |
5692 | return true; |
5693 | |
5694 | return false; |
5695 | } |
5696 | |
5697 | /* Returns true iff class T has a user-provided or explicit constructor. */ |
5698 | |
5699 | bool |
5700 | type_has_user_provided_or_explicit_constructor (tree t) |
5701 | { |
5702 | if (!CLASS_TYPE_P (t)) |
5703 | return false; |
5704 | |
5705 | if (!TYPE_HAS_USER_CONSTRUCTOR (t)) |
5706 | return false; |
5707 | |
5708 | for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (t)); iter; ++iter) |
5709 | { |
5710 | tree fn = *iter; |
5711 | if (user_provided_p (fn) || DECL_NONCONVERTING_P (fn)) |
5712 | return true; |
5713 | } |
5714 | |
5715 | return false; |
5716 | } |
5717 | |
5718 | /* Returns true iff class T has a non-user-provided (i.e. implicitly |
5719 | declared or explicitly defaulted in the class body) default |
5720 | constructor. */ |
5721 | |
5722 | bool |
5723 | type_has_non_user_provided_default_constructor (tree t) |
5724 | { |
5725 | if (!TYPE_HAS_DEFAULT_CONSTRUCTOR (t)) |
5726 | return false; |
5727 | if (CLASSTYPE_LAZY_DEFAULT_CTOR (t)) |
5728 | return true; |
5729 | |
5730 | for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (t)); iter; ++iter) |
5731 | { |
5732 | tree fn = *iter; |
5733 | if (TREE_CODE (fn) == FUNCTION_DECL |
5734 | && default_ctor_p (fn) |
5735 | && !user_provided_p (fn)) |
5736 | return true; |
5737 | } |
5738 | |
5739 | return false; |
5740 | } |
5741 | |
5742 | /* TYPE is being used as a virtual base, and has a non-trivial move |
5743 | assignment. Return true if this is due to there being a user-provided |
5744 | move assignment in TYPE or one of its subobjects; if there isn't, then |
5745 | multiple move assignment can't cause any harm. */ |
5746 | |
5747 | bool |
5748 | vbase_has_user_provided_move_assign (tree type) |
5749 | { |
5750 | /* Does the type itself have a user-provided move assignment operator? */ |
5751 | if (!CLASSTYPE_LAZY_MOVE_ASSIGN (type)) |
5752 | for (ovl_iterator iter (get_class_binding_direct |
5753 | (type, assign_op_identifier)); |
5754 | iter; ++iter) |
5755 | if (user_provided_p (fn: *iter) && move_fn_p (*iter)) |
5756 | return true; |
5757 | |
5758 | /* Do any of its bases? */ |
5759 | tree binfo = TYPE_BINFO (type); |
5760 | tree base_binfo; |
5761 | for (int i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i) |
5762 | if (vbase_has_user_provided_move_assign (BINFO_TYPE (base_binfo))) |
5763 | return true; |
5764 | |
5765 | /* Or non-static data members? */ |
5766 | for (tree field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field)) |
5767 | { |
5768 | if (TREE_CODE (field) == FIELD_DECL |
5769 | && CLASS_TYPE_P (TREE_TYPE (field)) |
5770 | && vbase_has_user_provided_move_assign (TREE_TYPE (field))) |
5771 | return true; |
5772 | } |
5773 | |
5774 | /* Seems not. */ |
5775 | return false; |
5776 | } |
5777 | |
5778 | /* If default-initialization leaves part of TYPE uninitialized, returns |
5779 | a DECL for the field or TYPE itself (DR 253). */ |
5780 | |
5781 | tree |
5782 | default_init_uninitialized_part (tree type) |
5783 | { |
5784 | tree t, r, binfo; |
5785 | int i; |
5786 | |
5787 | type = strip_array_types (type); |
5788 | if (!CLASS_TYPE_P (type)) |
5789 | return type; |
5790 | if (!type_has_non_user_provided_default_constructor (t: type)) |
5791 | return NULL_TREE; |
5792 | for (binfo = TYPE_BINFO (type), i = 0; |
5793 | BINFO_BASE_ITERATE (binfo, i, t); ++i) |
5794 | { |
5795 | r = default_init_uninitialized_part (BINFO_TYPE (t)); |
5796 | if (r) |
5797 | return r; |
5798 | } |
5799 | for (t = next_aggregate_field (TYPE_FIELDS (type)); t; |
5800 | t = next_aggregate_field (DECL_CHAIN (t))) |
5801 | if (!DECL_INITIAL (t) && !DECL_ARTIFICIAL (t)) |
5802 | { |
5803 | r = default_init_uninitialized_part (TREE_TYPE (t)); |
5804 | if (r) |
5805 | return DECL_P (r) ? r : t; |
5806 | } |
5807 | |
5808 | return NULL_TREE; |
5809 | } |
5810 | |
5811 | /* Returns true iff for class T, a trivial synthesized default constructor |
5812 | would be constexpr. */ |
5813 | |
5814 | bool |
5815 | trivial_default_constructor_is_constexpr (tree t) |
5816 | { |
5817 | /* A defaulted trivial default constructor is constexpr |
5818 | if there is nothing to initialize. */ |
5819 | gcc_assert (!TYPE_HAS_COMPLEX_DFLT (t)); |
5820 | /* A class with a vptr doesn't have a trivial default ctor. |
5821 | In C++20, a class can have transient uninitialized members, e.g.: |
5822 | |
5823 | struct S { int i; constexpr S() = default; }; |
5824 | |
5825 | should work. */ |
5826 | return (cxx_dialect >= cxx20 |
5827 | || is_really_empty_class (t, /*ignore_vptr*/true)); |
5828 | } |
5829 | |
5830 | /* Returns true iff class T has a constexpr default constructor. */ |
5831 | |
5832 | bool |
5833 | type_has_constexpr_default_constructor (tree t) |
5834 | { |
5835 | tree fns; |
5836 | |
5837 | if (!CLASS_TYPE_P (t)) |
5838 | { |
5839 | /* The caller should have stripped an enclosing array. */ |
5840 | gcc_assert (TREE_CODE (t) != ARRAY_TYPE); |
5841 | return false; |
5842 | } |
5843 | if (CLASSTYPE_LAZY_DEFAULT_CTOR (t)) |
5844 | { |
5845 | if (!TYPE_HAS_COMPLEX_DFLT (t)) |
5846 | return trivial_default_constructor_is_constexpr (t); |
5847 | /* Non-trivial, we need to check subobject constructors. */ |
5848 | lazily_declare_fn (sfk_constructor, t); |
5849 | } |
5850 | fns = locate_ctor (t); |
5851 | return (fns && DECL_DECLARED_CONSTEXPR_P (fns)); |
5852 | } |
5853 | |
5854 | /* Returns true iff class T has a constexpr default constructor or has an |
5855 | implicitly declared default constructor that we can't tell if it's constexpr |
5856 | without forcing a lazy declaration (which might cause undesired |
5857 | instantiations). */ |
5858 | |
5859 | static bool |
5860 | type_maybe_constexpr_default_constructor (tree t) |
5861 | { |
5862 | if (CLASS_TYPE_P (t) && CLASSTYPE_LAZY_DEFAULT_CTOR (t) |
5863 | && TYPE_HAS_COMPLEX_DFLT (t)) |
5864 | /* Assume it's constexpr. */ |
5865 | return true; |
5866 | return type_has_constexpr_default_constructor (t); |
5867 | } |
5868 | |
5869 | /* Returns true iff class T has a constexpr destructor. */ |
5870 | |
5871 | bool |
5872 | type_has_constexpr_destructor (tree t) |
5873 | { |
5874 | tree fns; |
5875 | |
5876 | if (CLASSTYPE_LAZY_DESTRUCTOR (t)) |
5877 | /* Non-trivial, we need to check subobject destructors. */ |
5878 | lazily_declare_fn (sfk_destructor, t); |
5879 | fns = CLASSTYPE_DESTRUCTOR (t); |
5880 | return (fns && DECL_DECLARED_CONSTEXPR_P (fns)); |
5881 | } |
5882 | |
5883 | /* Returns true iff class T has a constexpr destructor or has an |
5884 | implicitly declared destructor that we can't tell if it's constexpr |
5885 | without forcing a lazy declaration (which might cause undesired |
5886 | instantiations). */ |
5887 | |
5888 | static bool |
5889 | type_maybe_constexpr_destructor (tree t) |
5890 | { |
5891 | /* Until C++20, only trivial destruction is constexpr. */ |
5892 | if (TYPE_HAS_TRIVIAL_DESTRUCTOR (t)) |
5893 | return true; |
5894 | if (cxx_dialect < cxx20) |
5895 | return false; |
5896 | if (CLASS_TYPE_P (t) && CLASSTYPE_LAZY_DESTRUCTOR (t)) |
5897 | /* Assume it's constexpr. */ |
5898 | return true; |
5899 | tree fn = CLASSTYPE_DESTRUCTOR (t); |
5900 | return (fn && maybe_constexpr_fn (fn)); |
5901 | } |
5902 | |
5903 | /* Returns true iff class TYPE has a virtual destructor. */ |
5904 | |
5905 | bool |
5906 | type_has_virtual_destructor (tree type) |
5907 | { |
5908 | tree dtor; |
5909 | |
5910 | if (!NON_UNION_CLASS_TYPE_P (type)) |
5911 | return false; |
5912 | |
5913 | gcc_assert (COMPLETE_TYPE_P (type)); |
5914 | dtor = CLASSTYPE_DESTRUCTOR (type); |
5915 | return (dtor && DECL_VIRTUAL_P (dtor)); |
5916 | } |
5917 | |
5918 | /* True iff class TYPE has a non-deleted trivial default |
5919 | constructor. */ |
5920 | |
5921 | bool type_has_non_deleted_trivial_default_ctor (tree type) |
5922 | { |
5923 | return TYPE_HAS_TRIVIAL_DFLT (type) && locate_ctor (type); |
5924 | } |
5925 | |
5926 | /* Returns true iff T, a class, has a move-assignment or |
5927 | move-constructor. Does not lazily declare either. |
5928 | If USER_P is false, any move function will do. If it is true, the |
5929 | move function must be user-declared. |
5930 | |
5931 | Note that user-declared here is different from "user-provided", |
5932 | which doesn't include functions that are defaulted in the |
5933 | class. */ |
5934 | |
5935 | bool |
5936 | classtype_has_move_assign_or_move_ctor_p (tree t, bool user_p) |
5937 | { |
5938 | gcc_assert (user_p |
5939 | || (!CLASSTYPE_LAZY_MOVE_CTOR (t) |
5940 | && !CLASSTYPE_LAZY_MOVE_ASSIGN (t))); |
5941 | |
5942 | if (!CLASSTYPE_LAZY_MOVE_CTOR (t)) |
5943 | for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (t)); iter; ++iter) |
5944 | if ((!user_p || !DECL_ARTIFICIAL (*iter)) && move_fn_p (*iter)) |
5945 | return true; |
5946 | |
5947 | if (!CLASSTYPE_LAZY_MOVE_ASSIGN (t)) |
5948 | for (ovl_iterator iter (get_class_binding_direct |
5949 | (t, assign_op_identifier)); |
5950 | iter; ++iter) |
5951 | if ((!user_p || !DECL_ARTIFICIAL (*iter)) |
5952 | && DECL_CONTEXT (*iter) == t |
5953 | && move_fn_p (*iter)) |
5954 | return true; |
5955 | |
5956 | return false; |
5957 | } |
5958 | |
5959 | /* True iff T has a move constructor that is not deleted. */ |
5960 | |
5961 | bool |
5962 | classtype_has_non_deleted_move_ctor (tree t) |
5963 | { |
5964 | if (CLASSTYPE_LAZY_MOVE_CTOR (t)) |
5965 | lazily_declare_fn (sfk_move_constructor, t); |
5966 | for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (t)); iter; ++iter) |
5967 | if (move_fn_p (*iter) && !DECL_DELETED_FN (*iter)) |
5968 | return true; |
5969 | return false; |
5970 | } |
5971 | |
5972 | /* If T, a class, has a user-provided copy constructor, copy assignment |
5973 | operator, or destructor, returns that function. Otherwise, null. */ |
5974 | |
5975 | tree |
5976 | classtype_has_depr_implicit_copy (tree t) |
5977 | { |
5978 | if (!CLASSTYPE_LAZY_COPY_CTOR (t)) |
5979 | for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (t)); iter; ++iter) |
5980 | { |
5981 | tree fn = *iter; |
5982 | if (user_provided_p (fn) && copy_fn_p (fn)) |
5983 | return fn; |
5984 | } |
5985 | |
5986 | if (!CLASSTYPE_LAZY_COPY_ASSIGN (t)) |
5987 | for (ovl_iterator iter (get_class_binding_direct |
5988 | (t, assign_op_identifier)); |
5989 | iter; ++iter) |
5990 | { |
5991 | tree fn = *iter; |
5992 | if (DECL_CONTEXT (fn) == t |
5993 | && user_provided_p (fn) && copy_fn_p (fn)) |
5994 | return fn; |
5995 | } |
5996 | |
5997 | if (!CLASSTYPE_LAZY_DESTRUCTOR (t)) |
5998 | { |
5999 | tree fn = CLASSTYPE_DESTRUCTOR (t); |
6000 | if (user_provided_p (fn)) |
6001 | return fn; |
6002 | } |
6003 | |
6004 | return NULL_TREE; |
6005 | } |
6006 | |
6007 | /* True iff T has a member or friend declaration of operator OP. */ |
6008 | |
6009 | bool |
6010 | classtype_has_op (tree t, tree_code op) |
6011 | { |
6012 | tree name = ovl_op_identifier (code: op); |
6013 | if (get_class_binding (t, name)) |
6014 | return true; |
6015 | for (tree f = DECL_FRIENDLIST (TYPE_MAIN_DECL (t)); f; f = TREE_CHAIN (f)) |
6016 | if (FRIEND_NAME (f) == name) |
6017 | return true; |
6018 | return false; |
6019 | } |
6020 | |
6021 | |
6022 | /* If T has a defaulted member or friend declaration of OP, return it. */ |
6023 | |
6024 | tree |
6025 | classtype_has_defaulted_op (tree t, tree_code op) |
6026 | { |
6027 | tree name = ovl_op_identifier (code: op); |
6028 | for (ovl_iterator oi (get_class_binding (t, name)); oi; ++oi) |
6029 | { |
6030 | tree fn = *oi; |
6031 | if (DECL_DEFAULTED_FN (fn)) |
6032 | return fn; |
6033 | } |
6034 | for (tree f = DECL_FRIENDLIST (TYPE_MAIN_DECL (t)); f; f = TREE_CHAIN (f)) |
6035 | if (FRIEND_NAME (f) == name) |
6036 | for (tree l = FRIEND_DECLS (f); l; l = TREE_CHAIN (l)) |
6037 | { |
6038 | tree fn = TREE_VALUE (l); |
6039 | if (DECL_DEFAULTED_FN (fn)) |
6040 | return fn; |
6041 | } |
6042 | return NULL_TREE; |
6043 | } |
6044 | |
6045 | /* Nonzero if we need to build up a constructor call when initializing an |
6046 | object of this class, either because it has a user-declared constructor |
6047 | or because it doesn't have a default constructor (so we need to give an |
6048 | error if no initializer is provided). Use TYPE_NEEDS_CONSTRUCTING when |
6049 | what you care about is whether or not an object can be produced by a |
6050 | constructor (e.g. so we don't set TREE_READONLY on const variables of |
6051 | such type); use this function when what you care about is whether or not |
6052 | to try to call a constructor to create an object. The latter case is |
6053 | the former plus some cases of constructors that cannot be called. */ |
6054 | |
6055 | bool |
6056 | type_build_ctor_call (tree t) |
6057 | { |
6058 | tree inner; |
6059 | if (TYPE_NEEDS_CONSTRUCTING (t)) |
6060 | return true; |
6061 | inner = strip_array_types (type: t); |
6062 | if (!CLASS_TYPE_P (inner) || ANON_AGGR_TYPE_P (inner)) |
6063 | return false; |
6064 | if (!TYPE_HAS_DEFAULT_CONSTRUCTOR (inner)) |
6065 | return true; |
6066 | if (cxx_dialect < cxx11) |
6067 | return false; |
6068 | /* A user-declared constructor might be private, and a constructor might |
6069 | be trivial but deleted. */ |
6070 | for (ovl_iterator iter (get_class_binding (inner, complete_ctor_identifier)); |
6071 | iter; ++iter) |
6072 | { |
6073 | tree fn = *iter; |
6074 | if (!DECL_ARTIFICIAL (fn) |
6075 | || TREE_DEPRECATED (fn) |
6076 | || TREE_UNAVAILABLE (fn) |
6077 | || DECL_DELETED_FN (fn)) |
6078 | return true; |
6079 | } |
6080 | return false; |
6081 | } |
6082 | |
6083 | /* Like type_build_ctor_call, but for destructors. */ |
6084 | |
6085 | bool |
6086 | type_build_dtor_call (tree t) |
6087 | { |
6088 | tree inner; |
6089 | if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t)) |
6090 | return true; |
6091 | inner = strip_array_types (type: t); |
6092 | if (!CLASS_TYPE_P (inner) || ANON_AGGR_TYPE_P (inner) |
6093 | || !COMPLETE_TYPE_P (inner)) |
6094 | return false; |
6095 | if (cxx_dialect < cxx11) |
6096 | return false; |
6097 | /* A user-declared destructor might be private, and a destructor might |
6098 | be trivial but deleted. */ |
6099 | for (ovl_iterator iter (get_class_binding (inner, complete_dtor_identifier)); |
6100 | iter; ++iter) |
6101 | { |
6102 | tree fn = *iter; |
6103 | if (!DECL_ARTIFICIAL (fn) |
6104 | || TREE_DEPRECATED (fn) |
6105 | || TREE_UNAVAILABLE (fn) |
6106 | || DECL_DELETED_FN (fn)) |
6107 | return true; |
6108 | } |
6109 | return false; |
6110 | } |
6111 | |
6112 | /* Returns TRUE iff we need a cookie when dynamically allocating an |
6113 | array whose elements have the indicated class TYPE. */ |
6114 | |
6115 | static bool |
6116 | type_requires_array_cookie (tree type) |
6117 | { |
6118 | tree fns; |
6119 | bool has_two_argument_delete_p = false; |
6120 | |
6121 | gcc_assert (CLASS_TYPE_P (type)); |
6122 | |
6123 | /* If there's a non-trivial destructor, we need a cookie. In order |
6124 | to iterate through the array calling the destructor for each |
6125 | element, we'll have to know how many elements there are. */ |
6126 | if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)) |
6127 | return true; |
6128 | |
6129 | /* If the usual deallocation function is a two-argument whose second |
6130 | argument is of type `size_t', then we have to pass the size of |
6131 | the array to the deallocation function, so we will need to store |
6132 | a cookie. */ |
6133 | fns = lookup_fnfields (TYPE_BINFO (type), |
6134 | ovl_op_identifier (isass: false, code: VEC_DELETE_EXPR), |
6135 | /*protect=*/0, tf_warning_or_error); |
6136 | /* If there are no `operator []' members, or the lookup is |
6137 | ambiguous, then we don't need a cookie. */ |
6138 | if (!fns || fns == error_mark_node) |
6139 | return false; |
6140 | /* Loop through all of the functions. */ |
6141 | for (lkp_iterator iter (BASELINK_FUNCTIONS (fns)); iter; ++iter) |
6142 | { |
6143 | tree fn = *iter; |
6144 | |
6145 | /* See if this function is a one-argument delete function. If |
6146 | it is, then it will be the usual deallocation function. */ |
6147 | tree second_parm = TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (fn))); |
6148 | if (second_parm == void_list_node) |
6149 | return false; |
6150 | /* Do not consider this function if its second argument is an |
6151 | ellipsis. */ |
6152 | if (!second_parm) |
6153 | continue; |
6154 | /* Otherwise, if we have a two-argument function and the second |
6155 | argument is `size_t', it will be the usual deallocation |
6156 | function -- unless there is one-argument function, too. */ |
6157 | if (TREE_CHAIN (second_parm) == void_list_node |
6158 | && same_type_p (TREE_VALUE (second_parm), size_type_node)) |
6159 | has_two_argument_delete_p = true; |
6160 | } |
6161 | |
6162 | return has_two_argument_delete_p; |
6163 | } |
6164 | |
6165 | /* Finish computing the `literal type' property of class type T. |
6166 | |
6167 | At this point, we have already processed base classes and |
6168 | non-static data members. We need to check whether the copy |
6169 | constructor is trivial, the destructor is trivial, and there |
6170 | is a trivial default constructor or at least one constexpr |
6171 | constructor other than the copy constructor. */ |
6172 | |
6173 | static void |
6174 | finalize_literal_type_property (tree t) |
6175 | { |
6176 | tree fn; |
6177 | |
6178 | if (cxx_dialect < cxx11) |
6179 | CLASSTYPE_LITERAL_P (t) = false; |
6180 | else if (CLASSTYPE_LITERAL_P (t) |
6181 | && !type_maybe_constexpr_destructor (t)) |
6182 | CLASSTYPE_LITERAL_P (t) = false; |
6183 | else if (CLASSTYPE_LITERAL_P (t) && LAMBDA_TYPE_P (t)) |
6184 | CLASSTYPE_LITERAL_P (t) = (cxx_dialect >= cxx17); |
6185 | else if (CLASSTYPE_LITERAL_P (t) && !TYPE_HAS_TRIVIAL_DFLT (t) |
6186 | && CLASSTYPE_NON_AGGREGATE (t) |
6187 | && !TYPE_HAS_CONSTEXPR_CTOR (t)) |
6188 | CLASSTYPE_LITERAL_P (t) = false; |
6189 | |
6190 | /* C++14 DR 1684 removed this restriction. */ |
6191 | if (cxx_dialect < cxx14 |
6192 | && !CLASSTYPE_LITERAL_P (t) && !LAMBDA_TYPE_P (t)) |
6193 | for (fn = TYPE_FIELDS (t); fn; fn = DECL_CHAIN (fn)) |
6194 | if (TREE_CODE (fn) == FUNCTION_DECL |
6195 | && DECL_DECLARED_CONSTEXPR_P (fn) |
6196 | && DECL_IOBJ_MEMBER_FUNCTION_P (fn) |
6197 | && !DECL_CONSTRUCTOR_P (fn)) |
6198 | { |
6199 | DECL_DECLARED_CONSTEXPR_P (fn) = false; |
6200 | if (!DECL_GENERATED_P (fn)) |
6201 | { |
6202 | auto_diagnostic_group d; |
6203 | if (pedwarn (DECL_SOURCE_LOCATION (fn), OPT_Wpedantic, |
6204 | "enclosing class of %<constexpr%> non-static " |
6205 | "member function %q+#D is not a literal type" , fn)) |
6206 | explain_non_literal_class (t); |
6207 | } |
6208 | } |
6209 | } |
6210 | |
6211 | /* T is a non-literal type used in a context which requires a constant |
6212 | expression. Explain why it isn't literal. */ |
6213 | |
6214 | void |
6215 | explain_non_literal_class (tree t) |
6216 | { |
6217 | static hash_set<tree> *diagnosed; |
6218 | |
6219 | if (!CLASS_TYPE_P (t)) |
6220 | return; |
6221 | t = TYPE_MAIN_VARIANT (t); |
6222 | |
6223 | if (diagnosed == NULL) |
6224 | diagnosed = new hash_set<tree>; |
6225 | if (diagnosed->add (k: t)) |
6226 | /* Already explained. */ |
6227 | return; |
6228 | |
6229 | auto_diagnostic_group d; |
6230 | inform (UNKNOWN_LOCATION, "%q+T is not literal because:" , t); |
6231 | if (cxx_dialect < cxx17 && LAMBDA_TYPE_P (t)) |
6232 | inform (UNKNOWN_LOCATION, |
6233 | " %qT is a closure type, which is only literal in " |
6234 | "C++17 and later" , t); |
6235 | else if (cxx_dialect < cxx20 && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t)) |
6236 | inform (UNKNOWN_LOCATION, " %q+T has a non-trivial destructor" , t); |
6237 | else if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t) |
6238 | && !type_maybe_constexpr_destructor (t)) |
6239 | inform (UNKNOWN_LOCATION, " %q+T does not have %<constexpr%> destructor" , |
6240 | t); |
6241 | else if (CLASSTYPE_NON_AGGREGATE (t) |
6242 | && !TYPE_HAS_TRIVIAL_DFLT (t) |
6243 | && !LAMBDA_TYPE_P (t) |
6244 | && !TYPE_HAS_CONSTEXPR_CTOR (t)) |
6245 | { |
6246 | inform (UNKNOWN_LOCATION, |
6247 | " %q+T is not an aggregate, does not have a trivial " |
6248 | "default constructor, and has no %<constexpr%> constructor that " |
6249 | "is not a copy or move constructor" , t); |
6250 | if (type_has_non_user_provided_default_constructor (t)) |
6251 | /* Note that we can't simply call locate_ctor because when the |
6252 | constructor is deleted it just returns NULL_TREE. */ |
6253 | for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (t)); iter; ++iter) |
6254 | { |
6255 | tree fn = *iter; |
6256 | tree parms = TYPE_ARG_TYPES (TREE_TYPE (fn)); |
6257 | |
6258 | parms = skip_artificial_parms_for (fn, parms); |
6259 | |
6260 | if (sufficient_parms_p (parms)) |
6261 | { |
6262 | if (DECL_DELETED_FN (fn)) |
6263 | maybe_explain_implicit_delete (fn); |
6264 | else |
6265 | explain_invalid_constexpr_fn (fn); |
6266 | break; |
6267 | } |
6268 | } |
6269 | } |
6270 | else |
6271 | { |
6272 | tree binfo, base_binfo, field; int i; |
6273 | for (binfo = TYPE_BINFO (t), i = 0; |
6274 | BINFO_BASE_ITERATE (binfo, i, base_binfo); i++) |
6275 | { |
6276 | tree basetype = TREE_TYPE (base_binfo); |
6277 | if (!CLASSTYPE_LITERAL_P (basetype)) |
6278 | { |
6279 | inform (UNKNOWN_LOCATION, |
6280 | " base class %qT of %q+T is non-literal" , |
6281 | basetype, t); |
6282 | explain_non_literal_class (t: basetype); |
6283 | return; |
6284 | } |
6285 | } |
6286 | for (field = TYPE_FIELDS (t); field; field = TREE_CHAIN (field)) |
6287 | { |
6288 | tree ftype; |
6289 | if (TREE_CODE (field) != FIELD_DECL) |
6290 | continue; |
6291 | ftype = TREE_TYPE (field); |
6292 | if (!literal_type_p (ftype)) |
6293 | { |
6294 | inform (DECL_SOURCE_LOCATION (field), |
6295 | " non-static data member %qD has non-literal type" , |
6296 | field); |
6297 | if (CLASS_TYPE_P (ftype)) |
6298 | explain_non_literal_class (t: ftype); |
6299 | } |
6300 | if (CP_TYPE_VOLATILE_P (ftype)) |
6301 | inform (DECL_SOURCE_LOCATION (field), |
6302 | " non-static data member %qD has volatile type" , field); |
6303 | } |
6304 | } |
6305 | } |
6306 | |
6307 | /* Check the validity of the bases and members declared in T. Add any |
6308 | implicitly-generated functions (like copy-constructors and |
6309 | assignment operators). Compute various flag bits (like |
6310 | CLASSTYPE_NON_LAYOUT_POD_T) for T. This routine works purely at the C++ |
6311 | level: i.e., independently of the ABI in use. */ |
6312 | |
6313 | static void |
6314 | check_bases_and_members (tree t) |
6315 | { |
6316 | /* Nonzero if the implicitly generated copy constructor should take |
6317 | a non-const reference argument. */ |
6318 | int cant_have_const_ctor; |
6319 | /* Nonzero if the implicitly generated assignment operator |
6320 | should take a non-const reference argument. */ |
6321 | int no_const_asn_ref; |
6322 | tree access_decls; |
6323 | bool saved_complex_asn_ref; |
6324 | bool saved_nontrivial_dtor; |
6325 | tree fn; |
6326 | |
6327 | /* By default, we use const reference arguments and generate default |
6328 | constructors. */ |
6329 | cant_have_const_ctor = 0; |
6330 | no_const_asn_ref = 0; |
6331 | |
6332 | /* Check all the base-classes and set FMEM members to point to arrays |
6333 | of potential interest. */ |
6334 | check_bases (t, cant_have_const_ctor_p: &cant_have_const_ctor, no_const_asn_ref_p: &no_const_asn_ref); |
6335 | |
6336 | /* Deduce noexcept on destructor. This needs to happen after we've set |
6337 | triviality flags appropriately for our bases, and before checking |
6338 | overriden virtual functions via check_methods. */ |
6339 | if (cxx_dialect >= cxx11) |
6340 | if (tree dtor = CLASSTYPE_DESTRUCTOR (t)) |
6341 | for (tree fn : ovl_range (dtor)) |
6342 | deduce_noexcept_on_destructor (dtor: fn); |
6343 | |
6344 | /* Check all the method declarations. */ |
6345 | check_methods (t); |
6346 | |
6347 | /* Save the initial values of these flags which only indicate whether |
6348 | or not the class has user-provided functions. As we analyze the |
6349 | bases and members we can set these flags for other reasons. */ |
6350 | saved_complex_asn_ref = TYPE_HAS_COMPLEX_COPY_ASSIGN (t); |
6351 | saved_nontrivial_dtor = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t); |
6352 | |
6353 | /* Check all the data member declarations. We cannot call |
6354 | check_field_decls until we have called check_bases check_methods, |
6355 | as check_field_decls depends on TYPE_HAS_NONTRIVIAL_DESTRUCTOR |
6356 | being set appropriately. */ |
6357 | check_field_decls (t, access_decls: &access_decls, |
6358 | cant_have_const_ctor_p: &cant_have_const_ctor, |
6359 | no_const_asn_ref_p: &no_const_asn_ref); |
6360 | |
6361 | /* A nearly-empty class has to be vptr-containing; a nearly empty |
6362 | class contains just a vptr. */ |
6363 | if (!TYPE_CONTAINS_VPTR_P (t)) |
6364 | CLASSTYPE_NEARLY_EMPTY_P (t) = 0; |
6365 | |
6366 | /* Do some bookkeeping that will guide the generation of implicitly |
6367 | declared member functions. */ |
6368 | TYPE_HAS_COMPLEX_COPY_CTOR (t) |= TYPE_CONTAINS_VPTR_P (t); |
6369 | TYPE_HAS_COMPLEX_MOVE_CTOR (t) |= TYPE_CONTAINS_VPTR_P (t); |
6370 | /* We need to call a constructor for this class if it has a |
6371 | user-provided constructor, or if the default constructor is going |
6372 | to initialize the vptr. (This is not an if-and-only-if; |
6373 | TYPE_NEEDS_CONSTRUCTING is set elsewhere if bases or members |
6374 | themselves need constructing.) */ |
6375 | TYPE_NEEDS_CONSTRUCTING (t) |
6376 | |= (type_has_user_provided_constructor (t) || TYPE_CONTAINS_VPTR_P (t)); |
6377 | /* [dcl.init.aggr] |
6378 | |
6379 | An aggregate is an array or a class with no user-provided |
6380 | constructors ... and no virtual functions. |
6381 | |
6382 | Again, other conditions for being an aggregate are checked |
6383 | elsewhere. */ |
6384 | CLASSTYPE_NON_AGGREGATE (t) |
6385 | |= ((cxx_dialect < cxx20 |
6386 | ? type_has_user_provided_or_explicit_constructor (t) |
6387 | : TYPE_HAS_USER_CONSTRUCTOR (t)) |
6388 | || TYPE_POLYMORPHIC_P (t)); |
6389 | /* This is the C++98/03 definition of POD; it changed in C++0x, but we |
6390 | retain the old definition internally for ABI reasons. */ |
6391 | CLASSTYPE_NON_LAYOUT_POD_P (t) |
6392 | |= (CLASSTYPE_NON_AGGREGATE (t) |
6393 | || saved_nontrivial_dtor || saved_complex_asn_ref); |
6394 | CLASSTYPE_NON_STD_LAYOUT (t) |= TYPE_CONTAINS_VPTR_P (t); |
6395 | TYPE_HAS_COMPLEX_COPY_ASSIGN (t) |= TYPE_CONTAINS_VPTR_P (t); |
6396 | TYPE_HAS_COMPLEX_MOVE_ASSIGN (t) |= TYPE_CONTAINS_VPTR_P (t); |
6397 | TYPE_HAS_COMPLEX_DFLT (t) |= TYPE_CONTAINS_VPTR_P (t); |
6398 | |
6399 | /* Is this class non-layout-POD because it wasn't an aggregate in C++98? */ |
6400 | if (CLASSTYPE_NON_POD_AGGREGATE (t)) |
6401 | { |
6402 | if (CLASSTYPE_NON_LAYOUT_POD_P (t)) |
6403 | /* It's non-POD for another reason. */ |
6404 | CLASSTYPE_NON_POD_AGGREGATE (t) = false; |
6405 | else if (abi_version_at_least (17)) |
6406 | CLASSTYPE_NON_LAYOUT_POD_P (t) = true; |
6407 | } |
6408 | |
6409 | /* If the only explicitly declared default constructor is user-provided, |
6410 | set TYPE_HAS_COMPLEX_DFLT. */ |
6411 | if (!TYPE_HAS_COMPLEX_DFLT (t) |
6412 | && TYPE_HAS_DEFAULT_CONSTRUCTOR (t) |
6413 | && !type_has_non_user_provided_default_constructor (t)) |
6414 | TYPE_HAS_COMPLEX_DFLT (t) = true; |
6415 | |
6416 | /* Warn if a public base of a polymorphic type has an accessible |
6417 | non-virtual destructor. It is only now that we know the class is |
6418 | polymorphic. Although a polymorphic base will have a already |
6419 | been diagnosed during its definition, we warn on use too. */ |
6420 | if (TYPE_POLYMORPHIC_P (t) && warn_nonvdtor) |
6421 | { |
6422 | tree binfo = TYPE_BINFO (t); |
6423 | vec<tree, va_gc> *accesses = BINFO_BASE_ACCESSES (binfo); |
6424 | tree base_binfo; |
6425 | unsigned i; |
6426 | |
6427 | for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++) |
6428 | { |
6429 | tree basetype = TREE_TYPE (base_binfo); |
6430 | |
6431 | if ((*accesses)[i] == access_public_node |
6432 | && (TYPE_POLYMORPHIC_P (basetype) || warn_ecpp) |
6433 | && accessible_nvdtor_p (t: basetype)) |
6434 | warning (OPT_Wnon_virtual_dtor, |
6435 | "base class %q#T has accessible non-virtual destructor" , |
6436 | basetype); |
6437 | } |
6438 | } |
6439 | |
6440 | /* If the class has no user-declared constructor, but does have |
6441 | non-static const or reference data members that can never be |
6442 | initialized, issue a warning. */ |
6443 | if (warn_uninitialized |
6444 | /* Classes with user-declared constructors are presumed to |
6445 | initialize these members. */ |
6446 | && !TYPE_HAS_USER_CONSTRUCTOR (t) |
6447 | /* Aggregates can be initialized with brace-enclosed |
6448 | initializers. */ |
6449 | && CLASSTYPE_NON_AGGREGATE (t)) |
6450 | { |
6451 | tree field; |
6452 | |
6453 | for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field)) |
6454 | { |
6455 | tree type; |
6456 | |
6457 | if (TREE_CODE (field) != FIELD_DECL |
6458 | || DECL_INITIAL (field) != NULL_TREE) |
6459 | continue; |
6460 | |
6461 | type = TREE_TYPE (field); |
6462 | if (TYPE_REF_P (type)) |
6463 | warning_at (DECL_SOURCE_LOCATION (field), |
6464 | OPT_Wuninitialized, "non-static reference %q#D " |
6465 | "in class without a constructor" , field); |
6466 | else if (CP_TYPE_CONST_P (type) |
6467 | && (!CLASS_TYPE_P (type) |
6468 | || !TYPE_HAS_DEFAULT_CONSTRUCTOR (type))) |
6469 | warning_at (DECL_SOURCE_LOCATION (field), |
6470 | OPT_Wuninitialized, "non-static const member %q#D " |
6471 | "in class without a constructor" , field); |
6472 | } |
6473 | } |
6474 | |
6475 | /* Synthesize any needed methods. */ |
6476 | add_implicitly_declared_members (t, access_decls: &access_decls, |
6477 | cant_have_const_cctor: cant_have_const_ctor, |
6478 | cant_have_const_assignment: no_const_asn_ref); |
6479 | |
6480 | /* Check defaulted declarations here so we have cant_have_const_ctor |
6481 | and don't need to worry about clones. */ |
6482 | for (fn = TYPE_FIELDS (t); fn; fn = DECL_CHAIN (fn)) |
6483 | if (DECL_DECLARES_FUNCTION_P (fn) |
6484 | && !DECL_ARTIFICIAL (fn) |
6485 | && DECL_DEFAULTED_IN_CLASS_P (fn)) |
6486 | { |
6487 | /* ...except handle comparisons later, in finish_struct_1. */ |
6488 | if (special_function_p (fn) == sfk_comparison) |
6489 | continue; |
6490 | |
6491 | int copy = copy_fn_p (fn); |
6492 | if (copy > 0) |
6493 | { |
6494 | bool imp_const_p |
6495 | = (DECL_CONSTRUCTOR_P (fn) ? !cant_have_const_ctor |
6496 | : !no_const_asn_ref); |
6497 | bool fn_const_p = (copy == 2); |
6498 | |
6499 | if (fn_const_p && !imp_const_p) |
6500 | /* If the function is defaulted outside the class, we just |
6501 | give the synthesis error. Core Issue #1331 says this is |
6502 | no longer ill-formed, it is defined as deleted instead. */ |
6503 | DECL_DELETED_FN (fn) = true; |
6504 | } |
6505 | defaulted_late_check (fn); |
6506 | } |
6507 | |
6508 | if (LAMBDA_TYPE_P (t)) |
6509 | /* "This class type is not an aggregate." */ |
6510 | CLASSTYPE_NON_AGGREGATE (t) = 1; |
6511 | |
6512 | /* Compute the 'literal type' property before we |
6513 | do anything with non-static member functions. */ |
6514 | finalize_literal_type_property (t); |
6515 | |
6516 | /* Create the in-charge and not-in-charge variants of constructors |
6517 | and destructors. */ |
6518 | clone_constructors_and_destructors (t); |
6519 | |
6520 | /* Process the using-declarations. */ |
6521 | for (; access_decls; access_decls = TREE_CHAIN (access_decls)) |
6522 | handle_using_decl (TREE_VALUE (access_decls), t); |
6523 | |
6524 | /* Figure out whether or not we will need a cookie when dynamically |
6525 | allocating an array of this type. */ |
6526 | LANG_TYPE_CLASS_CHECK (t)->vec_new_uses_cookie |
6527 | = type_requires_array_cookie (type: t); |
6528 | |
6529 | /* Classes marked hot or cold propagate the attribute to all members. We |
6530 | may do this now that methods are declared. This does miss some lazily |
6531 | declared special member functions (CLASSTYPE_LAZY_*), which are handled |
6532 | in lazily_declare_fn later on. */ |
6533 | propagate_class_warmth_attribute (t); |
6534 | } |
6535 | |
6536 | /* If T needs a pointer to its virtual function table, set TYPE_VFIELD |
6537 | accordingly. If a new vfield was created (because T doesn't have a |
6538 | primary base class), then the newly created field is returned. It |
6539 | is not added to the TYPE_FIELDS list; it is the caller's |
6540 | responsibility to do that. Accumulate declared virtual functions |
6541 | on VIRTUALS_P. */ |
6542 | |
6543 | static tree |
6544 | create_vtable_ptr (tree t, tree* virtuals_p) |
6545 | { |
6546 | tree fn; |
6547 | |
6548 | /* Collect the virtual functions declared in T. */ |
6549 | for (fn = TYPE_FIELDS (t); fn; fn = DECL_CHAIN (fn)) |
6550 | if (TREE_CODE (fn) == FUNCTION_DECL |
6551 | && DECL_VINDEX (fn) && !DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (fn) |
6552 | && TREE_CODE (DECL_VINDEX (fn)) != INTEGER_CST) |
6553 | { |
6554 | tree new_virtual = make_node (TREE_LIST); |
6555 | |
6556 | BV_FN (new_virtual) = fn; |
6557 | BV_DELTA (new_virtual) = integer_zero_node; |
6558 | BV_VCALL_INDEX (new_virtual) = NULL_TREE; |
6559 | |
6560 | TREE_CHAIN (new_virtual) = *virtuals_p; |
6561 | *virtuals_p = new_virtual; |
6562 | } |
6563 | |
6564 | /* If we couldn't find an appropriate base class, create a new field |
6565 | here. Even if there weren't any new virtual functions, we might need a |
6566 | new virtual function table if we're supposed to include vptrs in |
6567 | all classes that need them. */ |
6568 | if (!TYPE_VFIELD (t) && (*virtuals_p || TYPE_CONTAINS_VPTR_P (t))) |
6569 | { |
6570 | /* We build this decl with vtbl_ptr_type_node, which is a |
6571 | `vtable_entry_type*'. It might seem more precise to use |
6572 | `vtable_entry_type (*)[N]' where N is the number of virtual |
6573 | functions. However, that would require the vtable pointer in |
6574 | base classes to have a different type than the vtable pointer |
6575 | in derived classes. We could make that happen, but that |
6576 | still wouldn't solve all the problems. In particular, the |
6577 | type-based alias analysis code would decide that assignments |
6578 | to the base class vtable pointer can't alias assignments to |
6579 | the derived class vtable pointer, since they have different |
6580 | types. Thus, in a derived class destructor, where the base |
6581 | class constructor was inlined, we could generate bad code for |
6582 | setting up the vtable pointer. |
6583 | |
6584 | Therefore, we use one type for all vtable pointers. We still |
6585 | use a type-correct type; it's just doesn't indicate the array |
6586 | bounds. That's better than using `void*' or some such; it's |
6587 | cleaner, and it let's the alias analysis code know that these |
6588 | stores cannot alias stores to void*! */ |
6589 | tree field; |
6590 | |
6591 | field = build_decl (input_location, |
6592 | FIELD_DECL, get_vfield_name (t), vtbl_ptr_type_node); |
6593 | DECL_VIRTUAL_P (field) = 1; |
6594 | DECL_ARTIFICIAL (field) = 1; |
6595 | DECL_FIELD_CONTEXT (field) = t; |
6596 | DECL_FCONTEXT (field) = t; |
6597 | if (TYPE_PACKED (t)) |
6598 | DECL_PACKED (field) = 1; |
6599 | |
6600 | TYPE_VFIELD (t) = field; |
6601 | |
6602 | /* This class is non-empty. */ |
6603 | CLASSTYPE_EMPTY_P (t) = 0; |
6604 | |
6605 | return field; |
6606 | } |
6607 | |
6608 | return NULL_TREE; |
6609 | } |
6610 | |
6611 | /* Add OFFSET to all base types of BINFO which is a base in the |
6612 | hierarchy dominated by T. |
6613 | |
6614 | OFFSET, which is a type offset, is number of bytes. */ |
6615 | |
6616 | static void |
6617 | propagate_binfo_offsets (tree binfo, tree offset) |
6618 | { |
6619 | int i; |
6620 | tree primary_binfo; |
6621 | tree base_binfo; |
6622 | |
6623 | /* Update BINFO's offset. */ |
6624 | BINFO_OFFSET (binfo) |
6625 | = fold_convert (sizetype, |
6626 | size_binop (PLUS_EXPR, |
6627 | fold_convert (ssizetype, BINFO_OFFSET (binfo)), |
6628 | offset)); |
6629 | |
6630 | /* Find the primary base class. */ |
6631 | primary_binfo = get_primary_binfo (binfo); |
6632 | |
6633 | if (primary_binfo && BINFO_INHERITANCE_CHAIN (primary_binfo) == binfo) |
6634 | propagate_binfo_offsets (binfo: primary_binfo, offset); |
6635 | |
6636 | /* Scan all of the bases, pushing the BINFO_OFFSET adjust |
6637 | downwards. */ |
6638 | for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i) |
6639 | { |
6640 | /* Don't do the primary base twice. */ |
6641 | if (base_binfo == primary_binfo) |
6642 | continue; |
6643 | |
6644 | if (BINFO_VIRTUAL_P (base_binfo)) |
6645 | continue; |
6646 | |
6647 | propagate_binfo_offsets (binfo: base_binfo, offset); |
6648 | } |
6649 | } |
6650 | |
6651 | /* Set BINFO_OFFSET for all of the virtual bases for RLI->T. Update |
6652 | TYPE_ALIGN and TYPE_SIZE for T. OFFSETS gives the location of |
6653 | empty subobjects of T. */ |
6654 | |
6655 | static void |
6656 | layout_virtual_bases (record_layout_info rli, splay_tree offsets) |
6657 | { |
6658 | tree vbase; |
6659 | tree t = rli->t; |
6660 | tree *next_field; |
6661 | |
6662 | if (BINFO_N_BASE_BINFOS (TYPE_BINFO (t)) == 0) |
6663 | return; |
6664 | |
6665 | /* Find the last field. The artificial fields created for virtual |
6666 | bases will go after the last extant field to date. */ |
6667 | next_field = &TYPE_FIELDS (t); |
6668 | while (*next_field) |
6669 | next_field = &DECL_CHAIN (*next_field); |
6670 | |
6671 | /* Go through the virtual bases, allocating space for each virtual |
6672 | base that is not already a primary base class. These are |
6673 | allocated in inheritance graph order. */ |
6674 | for (vbase = TYPE_BINFO (t); vbase; vbase = TREE_CHAIN (vbase)) |
6675 | { |
6676 | if (!BINFO_VIRTUAL_P (vbase)) |
6677 | continue; |
6678 | |
6679 | if (!BINFO_PRIMARY_P (vbase)) |
6680 | { |
6681 | /* This virtual base is not a primary base of any class in the |
6682 | hierarchy, so we have to add space for it. */ |
6683 | next_field = build_base_field (rli, binfo: vbase, |
6684 | access_private_node, |
6685 | offsets, next_field); |
6686 | } |
6687 | } |
6688 | } |
6689 | |
6690 | /* Returns the offset of the byte just past the end of the base class |
6691 | BINFO. */ |
6692 | |
6693 | static tree |
6694 | end_of_base (tree binfo) |
6695 | { |
6696 | tree size; |
6697 | |
6698 | if (!CLASSTYPE_AS_BASE (BINFO_TYPE (binfo))) |
6699 | size = TYPE_SIZE_UNIT (char_type_node); |
6700 | else if (is_empty_class (BINFO_TYPE (binfo))) |
6701 | /* An empty class has zero CLASSTYPE_SIZE_UNIT, but we need to |
6702 | allocate some space for it. It cannot have virtual bases, so |
6703 | TYPE_SIZE_UNIT is fine. */ |
6704 | size = TYPE_SIZE_UNIT (BINFO_TYPE (binfo)); |
6705 | else |
6706 | size = CLASSTYPE_SIZE_UNIT (BINFO_TYPE (binfo)); |
6707 | |
6708 | return size_binop (PLUS_EXPR, BINFO_OFFSET (binfo), size); |
6709 | } |
6710 | |
6711 | /* Returns one of three variations of the ending offset of T. If MODE is |
6712 | eoc_nvsize, the result is the ABI "nvsize" (i.e. sizeof before allocating |
6713 | vbases). If MODE is eoc_vsize, the result is the sizeof after allocating |
6714 | vbases but before rounding, which is not named in the ABI. If MODE is |
6715 | eoc_nv_or_dsize, the result is the greater of "nvsize" and "dsize" (the size |
6716 | of the actual data in the class, kinda), as used for allocation of |
6717 | potentially-overlapping fields. */ |
6718 | |
6719 | enum eoc_mode { eoc_nvsize, eoc_vsize, eoc_nv_or_dsize }; |
6720 | static tree |
6721 | end_of_class (tree t, eoc_mode mode) |
6722 | { |
6723 | tree result = size_zero_node; |
6724 | vec<tree, va_gc> *vbases; |
6725 | tree binfo; |
6726 | tree base_binfo; |
6727 | tree offset; |
6728 | int i; |
6729 | |
6730 | for (binfo = TYPE_BINFO (t), i = 0; |
6731 | BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i) |
6732 | { |
6733 | if (BINFO_VIRTUAL_P (base_binfo) |
6734 | && (!BINFO_PRIMARY_P (base_binfo) |
6735 | || BINFO_INHERITANCE_CHAIN (base_binfo) != TYPE_BINFO (t))) |
6736 | continue; |
6737 | |
6738 | offset = end_of_base (binfo: base_binfo); |
6739 | if (tree_int_cst_lt (t1: result, t2: offset)) |
6740 | result = offset; |
6741 | } |
6742 | |
6743 | for (tree field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field)) |
6744 | if (TREE_CODE (field) == FIELD_DECL |
6745 | && !DECL_FIELD_IS_BASE (field)) |
6746 | { |
6747 | tree size = DECL_SIZE_UNIT (field); |
6748 | if (!size) |
6749 | /* DECL_SIZE_UNIT can be null for a flexible array. */ |
6750 | continue; |
6751 | |
6752 | if (is_empty_field (decl: field)) |
6753 | /* For empty fields DECL_SIZE_UNIT is 0, but we want the |
6754 | size of the type (usually 1) for computing nvsize. */ |
6755 | size = TYPE_SIZE_UNIT (TREE_TYPE (field)); |
6756 | |
6757 | if (DECL_BIT_FIELD_TYPE (field)) |
6758 | { |
6759 | offset = size_binop (PLUS_EXPR, bit_position (field), |
6760 | DECL_SIZE (field)); |
6761 | offset = size_binop (CEIL_DIV_EXPR, offset, bitsize_unit_node); |
6762 | offset = fold_convert (sizetype, offset); |
6763 | } |
6764 | else |
6765 | offset = size_binop (PLUS_EXPR, byte_position (field), size); |
6766 | if (tree_int_cst_lt (t1: result, t2: offset)) |
6767 | result = offset; |
6768 | } |
6769 | |
6770 | if (mode != eoc_nvsize) |
6771 | for (vbases = CLASSTYPE_VBASECLASSES (t), i = 0; |
6772 | vec_safe_iterate (v: vbases, ix: i, ptr: &base_binfo); i++) |
6773 | { |
6774 | if (mode == eoc_nv_or_dsize) |
6775 | /* For dsize, don't count trailing empty bases. */ |
6776 | offset = size_binop (PLUS_EXPR, BINFO_OFFSET (base_binfo), |
6777 | CLASSTYPE_SIZE_UNIT (BINFO_TYPE (base_binfo))); |
6778 | else |
6779 | offset = end_of_base (binfo: base_binfo); |
6780 | if (tree_int_cst_lt (t1: result, t2: offset)) |
6781 | result = offset; |
6782 | } |
6783 | |
6784 | return result; |
6785 | } |
6786 | |
6787 | /* Warn as appropriate about the change in whether we pack into the tail |
6788 | padding of FIELD, a base field which has a C++14 aggregate type with default |
6789 | member initializers. */ |
6790 | |
6791 | static void |
6792 | check_non_pod_aggregate (tree field) |
6793 | { |
6794 | if (!abi_version_crosses (17) || cxx_dialect < cxx14) |
6795 | return; |
6796 | if (TREE_CODE (field) != FIELD_DECL |
6797 | || (!DECL_FIELD_IS_BASE (field) |
6798 | && !field_poverlapping_p (decl: field))) |
6799 | return; |
6800 | tree next = DECL_CHAIN (field); |
6801 | while (next && TREE_CODE (next) != FIELD_DECL) next = DECL_CHAIN (next); |
6802 | if (!next) |
6803 | return; |
6804 | tree type = TREE_TYPE (field); |
6805 | if (TYPE_IDENTIFIER (type) == as_base_identifier) |
6806 | type = TYPE_CONTEXT (type); |
6807 | if (!CLASS_TYPE_P (type) || !CLASSTYPE_NON_POD_AGGREGATE (type)) |
6808 | return; |
6809 | tree size = end_of_class (t: type, mode: (DECL_FIELD_IS_BASE (field) |
6810 | ? eoc_nvsize : eoc_nv_or_dsize)); |
6811 | tree rounded = round_up_loc (input_location, size, DECL_ALIGN_UNIT (next)); |
6812 | if (tree_int_cst_lt (t1: rounded, TYPE_SIZE_UNIT (type))) |
6813 | { |
6814 | location_t loc = DECL_SOURCE_LOCATION (next); |
6815 | if (DECL_FIELD_IS_BASE (next)) |
6816 | warning_at (loc, OPT_Wabi,"offset of %qT base class for " |
6817 | "%<-std=c++14%> and up changes in " |
6818 | "%<-fabi-version=17%> (GCC 12)" , TREE_TYPE (next)); |
6819 | else |
6820 | warning_at (loc, OPT_Wabi, "offset of %qD for " |
6821 | "%<-std=c++14%> and up changes in " |
6822 | "%<-fabi-version=17%> (GCC 12)" , next); |
6823 | } |
6824 | } |
6825 | |
6826 | /* Warn about bases of T that are inaccessible because they are |
6827 | ambiguous. For example: |
6828 | |
6829 | struct S {}; |
6830 | struct T : public S {}; |
6831 | struct U : public S, public T {}; |
6832 | |
6833 | Here, `(S*) new U' is not allowed because there are two `S' |
6834 | subobjects of U. */ |
6835 | |
6836 | static void |
6837 | maybe_warn_about_inaccessible_bases (tree t) |
6838 | { |
6839 | int i; |
6840 | vec<tree, va_gc> *vbases; |
6841 | tree basetype; |
6842 | tree binfo; |
6843 | tree base_binfo; |
6844 | |
6845 | /* If not checking for warning then return early. */ |
6846 | if (!warn_inaccessible_base) |
6847 | return; |
6848 | |
6849 | /* If there are no repeated bases, nothing can be ambiguous. */ |
6850 | if (!CLASSTYPE_REPEATED_BASE_P (t)) |
6851 | return; |
6852 | |
6853 | /* Check direct bases. */ |
6854 | for (binfo = TYPE_BINFO (t), i = 0; |
6855 | BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i) |
6856 | { |
6857 | basetype = BINFO_TYPE (base_binfo); |
6858 | |
6859 | if (!uniquely_derived_from_p (basetype, t)) |
6860 | warning (OPT_Winaccessible_base, "direct base %qT inaccessible " |
6861 | "in %qT due to ambiguity" , basetype, t); |
6862 | } |
6863 | |
6864 | /* Check for ambiguous virtual bases. */ |
6865 | if (extra_warnings) |
6866 | for (vbases = CLASSTYPE_VBASECLASSES (t), i = 0; |
6867 | vec_safe_iterate (v: vbases, ix: i, ptr: &binfo); i++) |
6868 | { |
6869 | basetype = BINFO_TYPE (binfo); |
6870 | |
6871 | if (!uniquely_derived_from_p (basetype, t)) |
6872 | warning (OPT_Winaccessible_base, "virtual base %qT inaccessible in " |
6873 | "%qT due to ambiguity" , basetype, t); |
6874 | } |
6875 | } |
6876 | |
6877 | /* Compare two INTEGER_CSTs K1 and K2. */ |
6878 | |
6879 | static int |
6880 | splay_tree_compare_integer_csts (splay_tree_key k1, splay_tree_key k2) |
6881 | { |
6882 | return tree_int_cst_compare (t1: (tree) k1, t2: (tree) k2); |
6883 | } |
6884 | |
6885 | /* Increase the size indicated in RLI to account for empty classes |
6886 | that are "off the end" of the class. */ |
6887 | |
6888 | static void |
6889 | include_empty_classes (record_layout_info rli) |
6890 | { |
6891 | tree eoc; |
6892 | tree rli_size; |
6893 | |
6894 | /* It might be the case that we grew the class to allocate a |
6895 | zero-sized base class. That won't be reflected in RLI, yet, |
6896 | because we are willing to overlay multiple bases at the same |
6897 | offset. However, now we need to make sure that RLI is big enough |
6898 | to reflect the entire class. */ |
6899 | eoc = end_of_class (t: rli->t, mode: eoc_vsize); |
6900 | rli_size = rli_size_unit_so_far (rli); |
6901 | if (TREE_CODE (rli_size) == INTEGER_CST |
6902 | && tree_int_cst_lt (t1: rli_size, t2: eoc)) |
6903 | { |
6904 | /* The size should have been rounded to a whole byte. */ |
6905 | gcc_assert (tree_int_cst_equal |
6906 | (rli->bitpos, round_down (rli->bitpos, BITS_PER_UNIT))); |
6907 | rli->bitpos |
6908 | = size_binop (PLUS_EXPR, |
6909 | rli->bitpos, |
6910 | size_binop (MULT_EXPR, |
6911 | fold_convert (bitsizetype, |
6912 | size_binop (MINUS_EXPR, |
6913 | eoc, rli_size)), |
6914 | bitsize_int (BITS_PER_UNIT))); |
6915 | normalize_rli (rli); |
6916 | } |
6917 | } |
6918 | |
6919 | /* Calculate the TYPE_SIZE, TYPE_ALIGN, etc for T. Calculate |
6920 | BINFO_OFFSETs for all of the base-classes. Position the vtable |
6921 | pointer. Accumulate declared virtual functions on VIRTUALS_P. */ |
6922 | |
6923 | static void |
6924 | layout_class_type (tree t, tree *virtuals_p) |
6925 | { |
6926 | tree non_static_data_members; |
6927 | tree field; |
6928 | tree vptr; |
6929 | record_layout_info rli; |
6930 | /* Maps offsets (represented as INTEGER_CSTs) to a TREE_LIST of |
6931 | types that appear at that offset. */ |
6932 | splay_tree empty_base_offsets; |
6933 | /* True if the last field laid out was a bit-field. */ |
6934 | bool last_field_was_bitfield = false; |
6935 | /* The location at which the next field should be inserted. */ |
6936 | tree *next_field; |
6937 | |
6938 | /* Keep track of the first non-static data member. */ |
6939 | non_static_data_members = TYPE_FIELDS (t); |
6940 | |
6941 | /* Start laying out the record. */ |
6942 | rli = start_record_layout (t); |
6943 | |
6944 | /* Mark all the primary bases in the hierarchy. */ |
6945 | determine_primary_bases (t); |
6946 | |
6947 | /* Create a pointer to our virtual function table. */ |
6948 | vptr = create_vtable_ptr (t, virtuals_p); |
6949 | |
6950 | /* The vptr is always the first thing in the class. */ |
6951 | if (vptr) |
6952 | { |
6953 | DECL_CHAIN (vptr) = TYPE_FIELDS (t); |
6954 | TYPE_FIELDS (t) = vptr; |
6955 | next_field = &DECL_CHAIN (vptr); |
6956 | place_field (rli, vptr); |
6957 | } |
6958 | else |
6959 | next_field = &TYPE_FIELDS (t); |
6960 | |
6961 | /* Build FIELD_DECLs for all of the non-virtual base-types. */ |
6962 | empty_base_offsets = splay_tree_new (splay_tree_compare_integer_csts, |
6963 | NULL, NULL); |
6964 | build_base_fields (rli, offsets: empty_base_offsets, next_field); |
6965 | |
6966 | /* Layout the non-static data members. */ |
6967 | for (field = non_static_data_members; field; field = DECL_CHAIN (field)) |
6968 | { |
6969 | tree type; |
6970 | tree padding; |
6971 | |
6972 | /* We still pass things that aren't non-static data members to |
6973 | the back end, in case it wants to do something with them. */ |
6974 | if (TREE_CODE (field) != FIELD_DECL) |
6975 | { |
6976 | place_field (rli, field); |
6977 | /* If the static data member has incomplete type, keep track |
6978 | of it so that it can be completed later. (The handling |
6979 | of pending statics in finish_record_layout is |
6980 | insufficient; consider: |
6981 | |
6982 | struct S1; |
6983 | struct S2 { static S1 s1; }; |
6984 | |
6985 | At this point, finish_record_layout will be called, but |
6986 | S1 is still incomplete.) */ |
6987 | if (VAR_P (field)) |
6988 | { |
6989 | maybe_register_incomplete_var (field); |
6990 | /* The visibility of static data members is determined |
6991 | at their point of declaration, not their point of |
6992 | definition. */ |
6993 | determine_visibility (field); |
6994 | } |
6995 | continue; |
6996 | } |
6997 | |
6998 | type = TREE_TYPE (field); |
6999 | if (type == error_mark_node) |
7000 | continue; |
7001 | |
7002 | padding = NULL_TREE; |
7003 | |
7004 | bool might_overlap = field_poverlapping_p (decl: field); |
7005 | |
7006 | if (might_overlap && CLASS_TYPE_P (type) |
7007 | && (CLASSTYPE_NON_LAYOUT_POD_P (type) || CLASSTYPE_EMPTY_P (type))) |
7008 | { |
7009 | /* if D is a potentially-overlapping data member, update sizeof(C) to |
7010 | max (sizeof(C), offset(D)+max (nvsize(D), dsize(D))). */ |
7011 | if (CLASSTYPE_EMPTY_P (type)) |
7012 | DECL_SIZE (field) = DECL_SIZE_UNIT (field) = size_zero_node; |
7013 | else |
7014 | { |
7015 | tree size = end_of_class (t: type, mode: eoc_nv_or_dsize); |
7016 | DECL_SIZE_UNIT (field) = size; |
7017 | DECL_SIZE (field) = bit_from_pos (size, bitsize_zero_node); |
7018 | } |
7019 | } |
7020 | |
7021 | /* If this field is a bit-field whose width is greater than its |
7022 | type, then there are some special rules for allocating |
7023 | it. */ |
7024 | if (DECL_C_BIT_FIELD (field) |
7025 | && tree_int_cst_lt (TYPE_SIZE (type), DECL_SIZE (field))) |
7026 | { |
7027 | bool was_unnamed_p = false; |
7028 | /* We must allocate the bits as if suitably aligned for the |
7029 | longest integer type that fits in this many bits. Then, |
7030 | we are supposed to use the left over bits as additional |
7031 | padding. */ |
7032 | |
7033 | /* Do not pick a type bigger than MAX_FIXED_MODE_SIZE. */ |
7034 | tree limit = size_int (MAX_FIXED_MODE_SIZE); |
7035 | if (tree_int_cst_lt (DECL_SIZE (field), t2: limit)) |
7036 | limit = DECL_SIZE (field); |
7037 | |
7038 | tree integer_type = integer_types[itk_char]; |
7039 | for (unsigned itk = itk_char; itk != itk_none; itk++) |
7040 | if (tree next = integer_types[itk]) |
7041 | { |
7042 | if (tree_int_cst_lt (t1: limit, TYPE_SIZE (next))) |
7043 | /* Too big, so our current guess is what we want. */ |
7044 | break; |
7045 | /* Not bigger than limit, ok */ |
7046 | integer_type = next; |
7047 | } |
7048 | |
7049 | /* Figure out how much additional padding is required. */ |
7050 | if (TREE_CODE (t) == UNION_TYPE) |
7051 | /* In a union, the padding field must have the full width |
7052 | of the bit-field; all fields start at offset zero. */ |
7053 | padding = DECL_SIZE (field); |
7054 | else |
7055 | padding = size_binop (MINUS_EXPR, DECL_SIZE (field), |
7056 | TYPE_SIZE (integer_type)); |
7057 | |
7058 | if (integer_zerop (padding)) |
7059 | padding = NULL_TREE; |
7060 | |
7061 | /* An unnamed bitfield does not normally affect the |
7062 | alignment of the containing class on a target where |
7063 | PCC_BITFIELD_TYPE_MATTERS. But, the C++ ABI does not |
7064 | make any exceptions for unnamed bitfields when the |
7065 | bitfields are longer than their types. Therefore, we |
7066 | temporarily give the field a name. */ |
7067 | if (PCC_BITFIELD_TYPE_MATTERS && !DECL_NAME (field)) |
7068 | { |
7069 | was_unnamed_p = true; |
7070 | DECL_NAME (field) = make_anon_name (); |
7071 | } |
7072 | |
7073 | DECL_SIZE (field) = TYPE_SIZE (integer_type); |
7074 | SET_DECL_ALIGN (field, TYPE_ALIGN (integer_type)); |
7075 | DECL_USER_ALIGN (field) = TYPE_USER_ALIGN (integer_type); |
7076 | layout_nonempty_base_or_field (rli, decl: field, NULL_TREE, |
7077 | offsets: empty_base_offsets); |
7078 | if (was_unnamed_p) |
7079 | DECL_NAME (field) = NULL_TREE; |
7080 | /* Now that layout has been performed, set the size of the |
7081 | field to the size of its declared type; the rest of the |
7082 | field is effectively invisible. */ |
7083 | DECL_SIZE (field) = TYPE_SIZE (type); |
7084 | /* We must also reset the DECL_MODE of the field. */ |
7085 | SET_DECL_MODE (field, TYPE_MODE (type)); |
7086 | } |
7087 | else if (might_overlap && is_empty_class (type)) |
7088 | { |
7089 | SET_DECL_FIELD_ABI_IGNORED (field, 1); |
7090 | layout_empty_base_or_field (rli, binfo_or_decl: field, offsets: empty_base_offsets); |
7091 | } |
7092 | else |
7093 | layout_nonempty_base_or_field (rli, decl: field, NULL_TREE, |
7094 | offsets: empty_base_offsets); |
7095 | |
7096 | /* Remember the location of any empty classes in FIELD. */ |
7097 | record_subobject_offsets (decl_or_binfo: field, offsets: empty_base_offsets); |
7098 | |
7099 | /* If a bit-field does not immediately follow another bit-field, |
7100 | and yet it starts in the middle of a byte, we have failed to |
7101 | comply with the ABI. */ |
7102 | if (warn_abi |
7103 | && DECL_C_BIT_FIELD (field) |
7104 | /* The TREE_NO_WARNING flag gets set by Objective-C when |
7105 | laying out an Objective-C class. The ObjC ABI differs |
7106 | from the C++ ABI, and so we do not want a warning |
7107 | here. */ |
7108 | && !warning_suppressed_p (field, OPT_Wabi) |
7109 | && !last_field_was_bitfield |
7110 | && !integer_zerop (size_binop (TRUNC_MOD_EXPR, |
7111 | DECL_FIELD_BIT_OFFSET (field), |
7112 | bitsize_unit_node))) |
7113 | warning_at (DECL_SOURCE_LOCATION (field), OPT_Wabi, |
7114 | "offset of %qD is not ABI-compliant and may " |
7115 | "change in a future version of GCC" , field); |
7116 | |
7117 | /* The middle end uses the type of expressions to determine the |
7118 | possible range of expression values. In order to optimize |
7119 | "x.i > 7" to "false" for a 2-bit bitfield "i", the middle end |
7120 | must be made aware of the width of "i", via its type. |
7121 | |
7122 | Because C++ does not have integer types of arbitrary width, |
7123 | we must (for the purposes of the front end) convert from the |
7124 | type assigned here to the declared type of the bitfield |
7125 | whenever a bitfield expression is used as an rvalue. |
7126 | Similarly, when assigning a value to a bitfield, the value |
7127 | must be converted to the type given the bitfield here. */ |
7128 | if (DECL_C_BIT_FIELD (field)) |
7129 | { |
7130 | unsigned HOST_WIDE_INT width; |
7131 | tree ftype = TREE_TYPE (field); |
7132 | width = tree_to_uhwi (DECL_SIZE (field)); |
7133 | if (width != TYPE_PRECISION (ftype)) |
7134 | { |
7135 | TREE_TYPE (field) |
7136 | = c_build_bitfield_integer_type (width, |
7137 | TYPE_UNSIGNED (ftype)); |
7138 | TREE_TYPE (field) |
7139 | = cp_build_qualified_type (TREE_TYPE (field), |
7140 | cp_type_quals (ftype)); |
7141 | } |
7142 | } |
7143 | |
7144 | /* If we needed additional padding after this field, add it |
7145 | now. */ |
7146 | if (padding) |
7147 | { |
7148 | tree padding_field; |
7149 | |
7150 | padding_field = build_decl (input_location, |
7151 | FIELD_DECL, |
7152 | NULL_TREE, |
7153 | char_type_node); |
7154 | DECL_BIT_FIELD (padding_field) = 1; |
7155 | DECL_SIZE (padding_field) = padding; |
7156 | DECL_CONTEXT (padding_field) = t; |
7157 | DECL_ARTIFICIAL (padding_field) = 1; |
7158 | DECL_IGNORED_P (padding_field) = 1; |
7159 | DECL_PADDING_P (padding_field) = 1; |
7160 | layout_nonempty_base_or_field (rli, decl: padding_field, |
7161 | NULL_TREE, |
7162 | offsets: empty_base_offsets); |
7163 | } |
7164 | |
7165 | last_field_was_bitfield = DECL_C_BIT_FIELD (field); |
7166 | } |
7167 | |
7168 | if (!integer_zerop (rli->bitpos)) |
7169 | { |
7170 | /* Make sure that we are on a byte boundary so that the size of |
7171 | the class without virtual bases will always be a round number |
7172 | of bytes. */ |
7173 | rli->bitpos = round_up_loc (input_location, rli->bitpos, BITS_PER_UNIT); |
7174 | normalize_rli (rli); |
7175 | } |
7176 | |
7177 | /* We used to remove zero width bitfields at this point since PR42217, |
7178 | while the C FE never did that. That caused ABI differences on various |
7179 | targets. Set the DECL_FIELD_CXX_ZERO_WIDTH_BIT_FIELD flag on them |
7180 | instead, so that the backends can emit -Wpsabi warnings in the cases |
7181 | where the ABI changed. */ |
7182 | for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field)) |
7183 | { |
7184 | if (TREE_CODE (field) == FIELD_DECL |
7185 | && DECL_C_BIT_FIELD (field) |
7186 | /* We should not be confused by the fact that grokbitfield |
7187 | temporarily sets the width of the bit field into |
7188 | DECL_BIT_FIELD_REPRESENTATIVE (field). |
7189 | check_bitfield_decl eventually sets DECL_SIZE (field) |
7190 | to that width. */ |
7191 | && (DECL_SIZE (field) == NULL_TREE |
7192 | || integer_zerop (DECL_SIZE (field))) |
7193 | && TREE_TYPE (field) != error_mark_node) |
7194 | SET_DECL_FIELD_CXX_ZERO_WIDTH_BIT_FIELD (field, 1); |
7195 | check_non_pod_aggregate (field); |
7196 | } |
7197 | |
7198 | if (CLASSTYPE_NON_LAYOUT_POD_P (t) || CLASSTYPE_EMPTY_P (t)) |
7199 | { |
7200 | /* T needs a different layout as a base (eliding virtual bases |
7201 | or whatever). Create that version. */ |
7202 | tree base_t = make_node (TREE_CODE (t)); |
7203 | tree base_d = create_implicit_typedef (as_base_identifier, base_t); |
7204 | |
7205 | TYPE_CONTEXT (base_t) = t; |
7206 | DECL_CONTEXT (base_d) = t; |
7207 | |
7208 | set_instantiating_module (base_d); |
7209 | |
7210 | /* If the ABI version is not at least two, and the last |
7211 | field was a bit-field, RLI may not be on a byte |
7212 | boundary. In particular, rli_size_unit_so_far might |
7213 | indicate the last complete byte, while rli_size_so_far |
7214 | indicates the total number of bits used. Therefore, |
7215 | rli_size_so_far, rather than rli_size_unit_so_far, is |
7216 | used to compute TYPE_SIZE_UNIT. */ |
7217 | |
7218 | /* Set the size and alignment for the new type. */ |
7219 | tree eoc = end_of_class (t, mode: eoc_nvsize); |
7220 | TYPE_SIZE_UNIT (base_t) |
7221 | = size_binop (MAX_EXPR, |
7222 | fold_convert (sizetype, |
7223 | size_binop (CEIL_DIV_EXPR, |
7224 | rli_size_so_far (rli), |
7225 | bitsize_int (BITS_PER_UNIT))), |
7226 | eoc); |
7227 | TYPE_SIZE (base_t) |
7228 | = size_binop (MAX_EXPR, |
7229 | rli_size_so_far (rli), |
7230 | size_binop (MULT_EXPR, |
7231 | fold_convert (bitsizetype, eoc), |
7232 | bitsize_int (BITS_PER_UNIT))); |
7233 | SET_TYPE_ALIGN (base_t, rli->record_align); |
7234 | TYPE_USER_ALIGN (base_t) = TYPE_USER_ALIGN (t); |
7235 | TYPE_TYPELESS_STORAGE (base_t) = TYPE_TYPELESS_STORAGE (t); |
7236 | TYPE_CXX_ODR_P (base_t) = TYPE_CXX_ODR_P (t); |
7237 | |
7238 | /* Copy the non-static data members of T. This will include its |
7239 | direct non-virtual bases & vtable. */ |
7240 | next_field = &TYPE_FIELDS (base_t); |
7241 | for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field)) |
7242 | if (TREE_CODE (field) == FIELD_DECL) |
7243 | { |
7244 | *next_field = copy_node (field); |
7245 | /* Zap any NSDMI, it's not needed and might be a deferred |
7246 | parse. */ |
7247 | DECL_INITIAL (*next_field) = NULL_TREE; |
7248 | DECL_CONTEXT (*next_field) = base_t; |
7249 | next_field = &DECL_CHAIN (*next_field); |
7250 | } |
7251 | *next_field = NULL_TREE; |
7252 | |
7253 | /* We use the base type for trivial assignments, and hence it |
7254 | needs a mode. */ |
7255 | compute_record_mode (base_t); |
7256 | |
7257 | /* Record the base version of the type. */ |
7258 | CLASSTYPE_AS_BASE (t) = base_t; |
7259 | } |
7260 | else |
7261 | CLASSTYPE_AS_BASE (t) = t; |
7262 | |
7263 | /* Every empty class contains an empty class. */ |
7264 | if (CLASSTYPE_EMPTY_P (t)) |
7265 | CLASSTYPE_CONTAINS_EMPTY_CLASS_P (t) = 1; |
7266 | |
7267 | /* Set the TYPE_DECL for this type to contain the right |
7268 | value for DECL_OFFSET, so that we can use it as part |
7269 | of a COMPONENT_REF for multiple inheritance. */ |
7270 | layout_decl (TYPE_MAIN_DECL (t), 0); |
7271 | |
7272 | /* Now fix up any virtual base class types that we left lying |
7273 | around. We must get these done before we try to lay out the |
7274 | virtual function table. As a side-effect, this will remove the |
7275 | base subobject fields. */ |
7276 | layout_virtual_bases (rli, offsets: empty_base_offsets); |
7277 | |
7278 | /* Make sure that empty classes are reflected in RLI at this |
7279 | point. */ |
7280 | include_empty_classes (rli); |
7281 | |
7282 | /* Make sure not to create any structures with zero size. */ |
7283 | if (integer_zerop (rli_size_unit_so_far (rli)) && CLASSTYPE_EMPTY_P (t)) |
7284 | place_field (rli, |
7285 | build_decl (input_location, |
7286 | FIELD_DECL, NULL_TREE, char_type_node)); |
7287 | |
7288 | /* If this is a non-POD, declaring it packed makes a difference to how it |
7289 | can be used as a field; don't let finalize_record_size undo it. */ |
7290 | if (TYPE_PACKED (t) && !layout_pod_type_p (t)) |
7291 | rli->packed_maybe_necessary = true; |
7292 | |
7293 | /* Let the back end lay out the type. */ |
7294 | finish_record_layout (rli, /*free_p=*/true); |
7295 | |
7296 | /* If we didn't end up needing an as-base type, don't use it. */ |
7297 | if (CLASSTYPE_AS_BASE (t) != t |
7298 | /* If T's CLASSTYPE_AS_BASE is TYPE_USER_ALIGN, but T is not, |
7299 | replacing the as-base type would change CLASSTYPE_USER_ALIGN, |
7300 | causing us to lose the user-specified alignment as in PR94050. */ |
7301 | && TYPE_USER_ALIGN (t) == TYPE_USER_ALIGN (CLASSTYPE_AS_BASE (t)) |
7302 | && tree_int_cst_equal (TYPE_SIZE (t), |
7303 | TYPE_SIZE (CLASSTYPE_AS_BASE (t)))) |
7304 | CLASSTYPE_AS_BASE (t) = t; |
7305 | |
7306 | if (TYPE_SIZE_UNIT (t) |
7307 | && TREE_CODE (TYPE_SIZE_UNIT (t)) == INTEGER_CST |
7308 | && !TREE_OVERFLOW (TYPE_SIZE_UNIT (t)) |
7309 | && !valid_constant_size_p (TYPE_SIZE_UNIT (t))) |
7310 | error ("size of type %qT is too large (%qE bytes)" , t, TYPE_SIZE_UNIT (t)); |
7311 | |
7312 | /* Warn about bases that can't be talked about due to ambiguity. */ |
7313 | maybe_warn_about_inaccessible_bases (t); |
7314 | |
7315 | /* Now that we're done with layout, give the base fields the real types. */ |
7316 | for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field)) |
7317 | if (DECL_ARTIFICIAL (field) && IS_FAKE_BASE_TYPE (TREE_TYPE (field))) |
7318 | TREE_TYPE (field) = TYPE_CONTEXT (TREE_TYPE (field)); |
7319 | |
7320 | /* Clean up. */ |
7321 | splay_tree_delete (empty_base_offsets); |
7322 | |
7323 | if (CLASSTYPE_EMPTY_P (t) |
7324 | && tree_int_cst_lt (t1: sizeof_biggest_empty_class, |
7325 | TYPE_SIZE_UNIT (t))) |
7326 | sizeof_biggest_empty_class = TYPE_SIZE_UNIT (t); |
7327 | } |
7328 | |
7329 | /* Determine the "key method" for the class type indicated by TYPE, |
7330 | and set CLASSTYPE_KEY_METHOD accordingly. */ |
7331 | |
7332 | void |
7333 | determine_key_method (tree type) |
7334 | { |
7335 | tree method; |
7336 | |
7337 | if (processing_template_decl |
7338 | || CLASSTYPE_TEMPLATE_INSTANTIATION (type) |
7339 | || CLASSTYPE_INTERFACE_KNOWN (type)) |
7340 | return; |
7341 | |
7342 | /* The key method is the first non-pure virtual function that is not |
7343 | inline at the point of class definition. On some targets the |
7344 | key function may not be inline; those targets should not call |
7345 | this function until the end of the translation unit. */ |
7346 | for (method = TYPE_FIELDS (type); method; method = DECL_CHAIN (method)) |
7347 | if (TREE_CODE (method) == FUNCTION_DECL |
7348 | && DECL_VINDEX (method) != NULL_TREE |
7349 | && ! DECL_DECLARED_INLINE_P (method) |
7350 | && ! DECL_PURE_VIRTUAL_P (method)) |
7351 | { |
7352 | CLASSTYPE_KEY_METHOD (type) = method; |
7353 | break; |
7354 | } |
7355 | |
7356 | return; |
7357 | } |
7358 | |
7359 | /* Helper of find_flexarrays. Return true when FLD refers to a non-static |
7360 | class data member of non-zero size, otherwise false. */ |
7361 | |
7362 | static inline bool |
7363 | field_nonempty_p (const_tree fld) |
7364 | { |
7365 | if (TREE_CODE (fld) == ERROR_MARK) |
7366 | return false; |
7367 | |
7368 | tree type = TREE_TYPE (fld); |
7369 | if (TREE_CODE (fld) == FIELD_DECL |
7370 | && TREE_CODE (type) != ERROR_MARK |
7371 | && (DECL_NAME (fld) || RECORD_OR_UNION_TYPE_P (type))) |
7372 | { |
7373 | return TYPE_SIZE (type) |
7374 | && (TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST |
7375 | || !tree_int_cst_equal (size_zero_node, TYPE_SIZE (type))); |
7376 | } |
7377 | |
7378 | return false; |
7379 | } |
7380 | |
7381 | /* Used by find_flexarrays and related functions. */ |
7382 | |
7383 | struct flexmems_t |
7384 | { |
7385 | /* The first flexible array member or non-zero array member found |
7386 | in the order of layout. */ |
7387 | tree array; |
7388 | /* First non-static non-empty data member in the class or its bases. */ |
7389 | tree first; |
7390 | /* The first non-static non-empty data member following either |
7391 | the flexible array member, if found, or the zero-length array member |
7392 | otherwise. AFTER[1] refers to the first such data member of a union |
7393 | of which the struct containing the flexible array member or zero-length |
7394 | array is a member, or NULL when no such union exists. This element is |
7395 | only used during searching, not for diagnosing problems. AFTER[0] |
7396 | refers to the first such data member that is not a member of such |
7397 | a union. */ |
7398 | tree after[2]; |
7399 | |
7400 | /* Refers to a struct (not union) in which the struct of which the flexible |
7401 | array is member is defined. Used to diagnose strictly (according to C) |
7402 | invalid uses of the latter structs. */ |
7403 | tree enclosing; |
7404 | }; |
7405 | |
7406 | /* Find either the first flexible array member or the first zero-length |
7407 | array, in that order of preference, among members of class T (but not |
7408 | its base classes), and set members of FMEM accordingly. |
7409 | BASE_P is true if T is a base class of another class. |
7410 | PUN is set to the outermost union in which the flexible array member |
7411 | (or zero-length array) is defined if one such union exists, otherwise |
7412 | to NULL. |
7413 | Similarly, PSTR is set to a data member of the outermost struct of |
7414 | which the flexible array is a member if one such struct exists, |
7415 | otherwise to NULL. */ |
7416 | |
7417 | static void |
7418 | find_flexarrays (tree t, flexmems_t *fmem, bool base_p, |
7419 | tree pun /* = NULL_TREE */, |
7420 | tree pstr /* = NULL_TREE */) |
7421 | { |
7422 | /* Set the "pointer" to the outermost enclosing union if not set |
7423 | yet and maintain it for the remainder of the recursion. */ |
7424 | if (!pun && TREE_CODE (t) == UNION_TYPE) |
7425 | pun = t; |
7426 | |
7427 | for (tree fld = TYPE_FIELDS (t); fld; fld = DECL_CHAIN (fld)) |
7428 | { |
7429 | if (fld == error_mark_node) |
7430 | return; |
7431 | |
7432 | /* Is FLD a typedef for an anonymous struct? */ |
7433 | |
7434 | /* FIXME: Note that typedefs (as well as arrays) need to be fully |
7435 | handled elsewhere so that errors like the following are detected |
7436 | as well: |
7437 | typedef struct { int i, a[], j; } S; // bug c++/72753 |
7438 | S s [2]; // bug c++/68489 |
7439 | */ |
7440 | if (TREE_CODE (fld) == TYPE_DECL |
7441 | && DECL_IMPLICIT_TYPEDEF_P (fld) |
7442 | && CLASS_TYPE_P (TREE_TYPE (fld)) |
7443 | && IDENTIFIER_ANON_P (DECL_NAME (fld))) |
7444 | { |
7445 | /* Check the nested unnamed type referenced via a typedef |
7446 | independently of FMEM (since it's not a data member of |
7447 | the enclosing class). */ |
7448 | check_flexarrays (TREE_TYPE (fld)); |
7449 | continue; |
7450 | } |
7451 | |
7452 | /* Skip anything that's GCC-generated or not a (non-static) data |
7453 | member. */ |
7454 | if (DECL_ARTIFICIAL (fld) || TREE_CODE (fld) != FIELD_DECL) |
7455 | continue; |
7456 | |
7457 | /* Type of the member. */ |
7458 | tree fldtype = TREE_TYPE (fld); |
7459 | if (fldtype == error_mark_node) |
7460 | return; |
7461 | |
7462 | /* Determine the type of the array element or object referenced |
7463 | by the member so that it can be checked for flexible array |
7464 | members if it hasn't been yet. */ |
7465 | tree eltype = fldtype; |
7466 | while (TREE_CODE (eltype) == ARRAY_TYPE |
7467 | || INDIRECT_TYPE_P (eltype)) |
7468 | eltype = TREE_TYPE (eltype); |
7469 | |
7470 | if (RECORD_OR_UNION_TYPE_P (eltype)) |
7471 | { |
7472 | if (fmem->array && !fmem->after[bool (pun)]) |
7473 | { |
7474 | /* Once the member after the flexible array has been found |
7475 | we're done. */ |
7476 | fmem->after[bool (pun)] = fld; |
7477 | break; |
7478 | } |
7479 | |
7480 | if (eltype == fldtype || TYPE_UNNAMED_P (eltype)) |
7481 | { |
7482 | /* Descend into the non-static member struct or union and try |
7483 | to find a flexible array member or zero-length array among |
7484 | its members. This is only necessary for anonymous types |
7485 | and types in whose context the current type T has not been |
7486 | defined (the latter must not be checked again because they |
7487 | are already in the process of being checked by one of the |
7488 | recursive calls). */ |
7489 | |
7490 | tree first = fmem->first; |
7491 | tree array = fmem->array; |
7492 | |
7493 | /* If this member isn't anonymous and a prior non-flexible array |
7494 | member has been seen in one of the enclosing structs, clear |
7495 | the FIRST member since it doesn't contribute to the flexible |
7496 | array struct's members. */ |
7497 | if (first && !array && !ANON_AGGR_TYPE_P (eltype)) |
7498 | fmem->first = NULL_TREE; |
7499 | |
7500 | find_flexarrays (t: eltype, fmem, base_p: false, pun, |
7501 | pstr: !pstr && TREE_CODE (t) == RECORD_TYPE ? fld : pstr); |
7502 | |
7503 | if (fmem->array != array) |
7504 | continue; |
7505 | |
7506 | if (first && !array && !ANON_AGGR_TYPE_P (eltype)) |
7507 | { |
7508 | /* Restore the FIRST member reset above if no flexible |
7509 | array member has been found in this member's struct. */ |
7510 | fmem->first = first; |
7511 | } |
7512 | |
7513 | /* If the member struct contains the first flexible array |
7514 | member, or if this member is a base class, continue to |
7515 | the next member and avoid setting the FMEM->NEXT pointer |
7516 | to point to it. */ |
7517 | if (base_p) |
7518 | continue; |
7519 | } |
7520 | } |
7521 | |
7522 | if (field_nonempty_p (fld)) |
7523 | { |
7524 | /* Remember the first non-static data member. */ |
7525 | if (!fmem->first) |
7526 | fmem->first = fld; |
7527 | |
7528 | /* Remember the first non-static data member after the flexible |
7529 | array member, if one has been found, or the zero-length array |
7530 | if it has been found. */ |
7531 | if (fmem->array && !fmem->after[bool (pun)]) |
7532 | fmem->after[bool (pun)] = fld; |
7533 | } |
7534 | |
7535 | /* Skip non-arrays. */ |
7536 | if (TREE_CODE (fldtype) != ARRAY_TYPE) |
7537 | continue; |
7538 | |
7539 | /* Determine the upper bound of the array if it has one. */ |
7540 | if (TYPE_DOMAIN (fldtype)) |
7541 | { |
7542 | if (fmem->array) |
7543 | { |
7544 | /* Make a record of the zero-length array if either one |
7545 | such field or a flexible array member has been seen to |
7546 | handle the pathological and unlikely case of multiple |
7547 | such members. */ |
7548 | if (!fmem->after[bool (pun)]) |
7549 | fmem->after[bool (pun)] = fld; |
7550 | } |
7551 | else if (integer_all_onesp (TYPE_MAX_VALUE (TYPE_DOMAIN (fldtype)))) |
7552 | { |
7553 | /* Remember the first zero-length array unless a flexible array |
7554 | member has already been seen. */ |
7555 | fmem->array = fld; |
7556 | fmem->enclosing = pstr; |
7557 | } |
7558 | } |
7559 | else |
7560 | { |
7561 | /* Flexible array members have no upper bound. */ |
7562 | if (fmem->array) |
7563 | { |
7564 | if (TYPE_DOMAIN (TREE_TYPE (fmem->array))) |
7565 | { |
7566 | /* Replace the zero-length array if it's been stored and |
7567 | reset the after pointer. */ |
7568 | fmem->after[bool (pun)] = NULL_TREE; |
7569 | fmem->array = fld; |
7570 | fmem->enclosing = pstr; |
7571 | } |
7572 | else if (!fmem->after[bool (pun)]) |
7573 | /* Make a record of another flexible array member. */ |
7574 | fmem->after[bool (pun)] = fld; |
7575 | } |
7576 | else |
7577 | { |
7578 | fmem->array = fld; |
7579 | fmem->enclosing = pstr; |
7580 | } |
7581 | } |
7582 | } |
7583 | } |
7584 | |
7585 | /* Diagnose a strictly (by the C standard) invalid use of a struct with |
7586 | a flexible array member (or the zero-length array extension). */ |
7587 | |
7588 | static void |
7589 | diagnose_invalid_flexarray (const flexmems_t *fmem) |
7590 | { |
7591 | if (fmem->array && fmem->enclosing) |
7592 | { |
7593 | auto_diagnostic_group d; |
7594 | if (pedwarn (location_of (fmem->enclosing), OPT_Wpedantic, |
7595 | TYPE_DOMAIN (TREE_TYPE (fmem->array)) |
7596 | ? G_("invalid use of %q#T with a zero-size array " |
7597 | "in %q#D" ) |
7598 | : G_("invalid use of %q#T with a flexible array member " |
7599 | "in %q#T" ), |
7600 | DECL_CONTEXT (fmem->array), |
7601 | DECL_CONTEXT (fmem->enclosing))) |
7602 | inform (DECL_SOURCE_LOCATION (fmem->array), |
7603 | "array member %q#D declared here" , fmem->array); |
7604 | } |
7605 | } |
7606 | |
7607 | /* Issue diagnostics for invalid flexible array members or zero-length |
7608 | arrays that are not the last elements of the containing class or its |
7609 | base classes or that are its sole members. */ |
7610 | |
7611 | static void |
7612 | diagnose_flexarrays (tree t, const flexmems_t *fmem) |
7613 | { |
7614 | if (!fmem->array) |
7615 | return; |
7616 | |
7617 | if (fmem->first && !fmem->after[0]) |
7618 | { |
7619 | diagnose_invalid_flexarray (fmem); |
7620 | return; |
7621 | } |
7622 | |
7623 | /* Has a diagnostic been issued? */ |
7624 | bool diagd = false; |
7625 | |
7626 | const char *msg = 0; |
7627 | |
7628 | if (TYPE_DOMAIN (TREE_TYPE (fmem->array))) |
7629 | { |
7630 | if (fmem->after[0]) |
7631 | msg = G_("zero-size array member %qD not at end of %q#T" ); |
7632 | else if (!fmem->first) |
7633 | msg = G_("zero-size array member %qD in an otherwise empty %q#T" ); |
7634 | |
7635 | if (msg) |
7636 | { |
7637 | location_t loc = DECL_SOURCE_LOCATION (fmem->array); |
7638 | |
7639 | auto_diagnostic_group d; |
7640 | if (pedwarn (loc, OPT_Wpedantic, msg, fmem->array, t)) |
7641 | { |
7642 | inform (location_of (t), "in the definition of %q#T" , t); |
7643 | diagd = true; |
7644 | } |
7645 | } |
7646 | } |
7647 | else |
7648 | { |
7649 | if (fmem->after[0]) |
7650 | msg = G_("flexible array member %qD not at end of %q#T" ); |
7651 | else if (!fmem->first) |
7652 | msg = G_("flexible array member %qD in an otherwise empty %q#T" ); |
7653 | |
7654 | if (msg) |
7655 | { |
7656 | location_t loc = DECL_SOURCE_LOCATION (fmem->array); |
7657 | diagd = true; |
7658 | |
7659 | auto_diagnostic_group d; |
7660 | error_at (loc, msg, fmem->array, t); |
7661 | |
7662 | /* In the unlikely event that the member following the flexible |
7663 | array member is declared in a different class, or the member |
7664 | overlaps another member of a common union, point to it. |
7665 | Otherwise it should be obvious. */ |
7666 | if (fmem->after[0] |
7667 | && ((DECL_CONTEXT (fmem->after[0]) |
7668 | != DECL_CONTEXT (fmem->array)))) |
7669 | { |
7670 | inform (DECL_SOURCE_LOCATION (fmem->after[0]), |
7671 | "next member %q#D declared here" , |
7672 | fmem->after[0]); |
7673 | inform (location_of (t), "in the definition of %q#T" , t); |
7674 | } |
7675 | } |
7676 | } |
7677 | |
7678 | if (!diagd && fmem->array && fmem->enclosing) |
7679 | diagnose_invalid_flexarray (fmem); |
7680 | } |
7681 | |
7682 | |
7683 | /* Recursively check to make sure that any flexible array or zero-length |
7684 | array members of class T or its bases are valid (i.e., not the sole |
7685 | non-static data member of T and, if one exists, that it is the last |
7686 | non-static data member of T and its base classes. FMEM is expected |
7687 | to be initially null and is used internally by recursive calls to |
7688 | the function. Issue the appropriate diagnostics for the array member |
7689 | that fails the checks. */ |
7690 | |
7691 | static void |
7692 | check_flexarrays (tree t, flexmems_t *fmem /* = NULL */, |
7693 | bool base_p /* = false */) |
7694 | { |
7695 | /* Initialize the result of a search for flexible array and zero-length |
7696 | array members. Avoid doing any work if the most interesting FMEM data |
7697 | have already been populated. */ |
7698 | flexmems_t flexmems = flexmems_t (); |
7699 | if (!fmem) |
7700 | fmem = &flexmems; |
7701 | else if (fmem->array && fmem->first && fmem->after[0]) |
7702 | return; |
7703 | |
7704 | tree fam = fmem->array; |
7705 | |
7706 | /* Recursively check the primary base class first. */ |
7707 | if (CLASSTYPE_HAS_PRIMARY_BASE_P (t)) |
7708 | { |
7709 | tree basetype = BINFO_TYPE (CLASSTYPE_PRIMARY_BINFO (t)); |
7710 | check_flexarrays (t: basetype, fmem, base_p: true); |
7711 | } |
7712 | |
7713 | /* Recursively check the base classes. */ |
7714 | int nbases = TYPE_BINFO (t) ? BINFO_N_BASE_BINFOS (TYPE_BINFO (t)) : 0; |
7715 | for (int i = 0; i < nbases; ++i) |
7716 | { |
7717 | tree base_binfo = BINFO_BASE_BINFO (TYPE_BINFO (t), i); |
7718 | |
7719 | /* The primary base class was already checked above. */ |
7720 | if (base_binfo == CLASSTYPE_PRIMARY_BINFO (t)) |
7721 | continue; |
7722 | |
7723 | /* Virtual base classes are at the end. */ |
7724 | if (BINFO_VIRTUAL_P (base_binfo)) |
7725 | continue; |
7726 | |
7727 | /* Check the base class. */ |
7728 | check_flexarrays (BINFO_TYPE (base_binfo), fmem, /*base_p=*/true); |
7729 | } |
7730 | |
7731 | if (fmem == &flexmems) |
7732 | { |
7733 | /* Check virtual base classes only once per derived class. |
7734 | I.e., this check is not performed recursively for base |
7735 | classes. */ |
7736 | int i; |
7737 | tree base_binfo; |
7738 | vec<tree, va_gc> *vbases; |
7739 | for (vbases = CLASSTYPE_VBASECLASSES (t), i = 0; |
7740 | vec_safe_iterate (v: vbases, ix: i, ptr: &base_binfo); i++) |
7741 | { |
7742 | /* Check the virtual base class. */ |
7743 | tree basetype = TREE_TYPE (base_binfo); |
7744 | |
7745 | check_flexarrays (t: basetype, fmem, /*base_p=*/true); |
7746 | } |
7747 | } |
7748 | |
7749 | /* Is the type unnamed (and therefore a member of it potentially |
7750 | an anonymous struct or union)? */ |
7751 | bool maybe_anon_p = TYPE_UNNAMED_P (t); |
7752 | if (tree ctx = maybe_anon_p ? TYPE_CONTEXT (t) : NULL_TREE) |
7753 | maybe_anon_p = RECORD_OR_UNION_TYPE_P (ctx); |
7754 | |
7755 | /* Search the members of the current (possibly derived) class, skipping |
7756 | unnamed structs and unions since those could be anonymous. */ |
7757 | if (fmem != &flexmems || !maybe_anon_p) |
7758 | find_flexarrays (t, fmem, base_p: base_p || fam != fmem->array); |
7759 | |
7760 | if (fmem == &flexmems && !maybe_anon_p) |
7761 | { |
7762 | /* Issue diagnostics for invalid flexible and zero-length array |
7763 | members found in base classes or among the members of the current |
7764 | class. Ignore anonymous structs and unions whose members are |
7765 | considered to be members of the enclosing class and thus will |
7766 | be diagnosed when checking it. */ |
7767 | diagnose_flexarrays (t, fmem); |
7768 | } |
7769 | } |
7770 | |
7771 | /* Perform processing required when the definition of T (a class type) |
7772 | is complete. Diagnose invalid definitions of flexible array members |
7773 | and zero-size arrays. */ |
7774 | |
7775 | void |
7776 | finish_struct_1 (tree t) |
7777 | { |
7778 | tree x; |
7779 | /* A TREE_LIST. The TREE_VALUE of each node is a FUNCTION_DECL. */ |
7780 | tree virtuals = NULL_TREE; |
7781 | |
7782 | if (COMPLETE_TYPE_P (t)) |
7783 | { |
7784 | gcc_assert (MAYBE_CLASS_TYPE_P (t)); |
7785 | error ("redefinition of %q#T" , t); |
7786 | popclass (); |
7787 | return; |
7788 | } |
7789 | |
7790 | /* If this type was previously laid out as a forward reference, |
7791 | make sure we lay it out again. */ |
7792 | TYPE_SIZE (t) = NULL_TREE; |
7793 | CLASSTYPE_PRIMARY_BINFO (t) = NULL_TREE; |
7794 | |
7795 | /* Make assumptions about the class; we'll reset the flags if |
7796 | necessary. */ |
7797 | CLASSTYPE_EMPTY_P (t) = 1; |
7798 | CLASSTYPE_NEARLY_EMPTY_P (t) = 1; |
7799 | CLASSTYPE_CONTAINS_EMPTY_CLASS_P (t) = 0; |
7800 | CLASSTYPE_LITERAL_P (t) = true; |
7801 | |
7802 | /* Do end-of-class semantic processing: checking the validity of the |
7803 | bases and members and add implicitly generated methods. */ |
7804 | check_bases_and_members (t); |
7805 | |
7806 | /* Find the key method. */ |
7807 | if (TYPE_CONTAINS_VPTR_P (t)) |
7808 | { |
7809 | /* The Itanium C++ ABI permits the key method to be chosen when |
7810 | the class is defined -- even though the key method so |
7811 | selected may later turn out to be an inline function. On |
7812 | some systems (such as ARM Symbian OS) the key method cannot |
7813 | be determined until the end of the translation unit. On such |
7814 | systems, we leave CLASSTYPE_KEY_METHOD set to NULL, which |
7815 | will cause the class to be added to KEYED_CLASSES. Then, in |
7816 | finish_file we will determine the key method. */ |
7817 | if (targetm.cxx.key_method_may_be_inline ()) |
7818 | determine_key_method (type: t); |
7819 | |
7820 | /* If a polymorphic class has no key method, we may emit the vtable |
7821 | in every translation unit where the class definition appears. If |
7822 | we're devirtualizing, we can look into the vtable even if we |
7823 | aren't emitting it. */ |
7824 | if (!CLASSTYPE_KEY_METHOD (t)) |
7825 | vec_safe_push (v&: keyed_classes, obj: t); |
7826 | } |
7827 | |
7828 | /* Layout the class itself. */ |
7829 | layout_class_type (t, virtuals_p: &virtuals); |
7830 | /* COMPLETE_TYPE_P is now true. */ |
7831 | |
7832 | set_class_bindings (t); |
7833 | |
7834 | /* With the layout complete, check for flexible array members and |
7835 | zero-length arrays that might overlap other members in the final |
7836 | layout. */ |
7837 | check_flexarrays (t); |
7838 | |
7839 | virtuals = modify_all_vtables (t, virtuals: nreverse (virtuals)); |
7840 | |
7841 | /* If necessary, create the primary vtable for this class. */ |
7842 | if (virtuals || TYPE_CONTAINS_VPTR_P (t)) |
7843 | { |
7844 | /* We must enter these virtuals into the table. */ |
7845 | if (!CLASSTYPE_HAS_PRIMARY_BASE_P (t)) |
7846 | build_primary_vtable (NULL_TREE, type: t); |
7847 | else if (! BINFO_NEW_VTABLE_MARKED (TYPE_BINFO (t))) |
7848 | /* Here we know enough to change the type of our virtual |
7849 | function table, but we will wait until later this function. */ |
7850 | build_primary_vtable (CLASSTYPE_PRIMARY_BINFO (t), type: t); |
7851 | |
7852 | /* If we're warning about ABI tags, check the types of the new |
7853 | virtual functions. */ |
7854 | if (warn_abi_tag) |
7855 | for (tree v = virtuals; v; v = TREE_CHAIN (v)) |
7856 | check_abi_tags (t, TREE_VALUE (v)); |
7857 | } |
7858 | |
7859 | if (TYPE_CONTAINS_VPTR_P (t)) |
7860 | { |
7861 | int vindex; |
7862 | tree fn; |
7863 | |
7864 | if (BINFO_VTABLE (TYPE_BINFO (t))) |
7865 | gcc_assert (DECL_VIRTUAL_P (BINFO_VTABLE (TYPE_BINFO (t)))); |
7866 | if (!CLASSTYPE_HAS_PRIMARY_BASE_P (t)) |
7867 | gcc_assert (BINFO_VIRTUALS (TYPE_BINFO (t)) == NULL_TREE); |
7868 | |
7869 | /* Add entries for virtual functions introduced by this class. */ |
7870 | BINFO_VIRTUALS (TYPE_BINFO (t)) |
7871 | = chainon (BINFO_VIRTUALS (TYPE_BINFO (t)), virtuals); |
7872 | |
7873 | /* Set DECL_VINDEX for all functions declared in this class. */ |
7874 | for (vindex = 0, fn = BINFO_VIRTUALS (TYPE_BINFO (t)); |
7875 | fn; |
7876 | fn = TREE_CHAIN (fn), |
7877 | vindex += (TARGET_VTABLE_USES_DESCRIPTORS |
7878 | ? TARGET_VTABLE_USES_DESCRIPTORS : 1)) |
7879 | { |
7880 | tree fndecl = BV_FN (fn); |
7881 | |
7882 | if (DECL_THUNK_P (fndecl)) |
7883 | /* A thunk. We should never be calling this entry directly |
7884 | from this vtable -- we'd use the entry for the non |
7885 | thunk base function. */ |
7886 | DECL_VINDEX (fndecl) = NULL_TREE; |
7887 | else if (TREE_CODE (DECL_VINDEX (fndecl)) != INTEGER_CST) |
7888 | DECL_VINDEX (fndecl) = build_int_cst (NULL_TREE, vindex); |
7889 | } |
7890 | } |
7891 | |
7892 | finish_struct_bits (t); |
7893 | |
7894 | set_method_tm_attributes (t); |
7895 | if (flag_openmp || flag_openmp_simd) |
7896 | finish_omp_declare_simd_methods (t); |
7897 | |
7898 | /* Clear DECL_IN_AGGR_P for all member functions. Complete the rtl |
7899 | for any static member objects of the type we're working on. */ |
7900 | for (x = TYPE_FIELDS (t); x; x = DECL_CHAIN (x)) |
7901 | if (DECL_DECLARES_FUNCTION_P (x)) |
7902 | { |
7903 | /* Synthesize constexpr defaulted comparisons. */ |
7904 | if (!DECL_ARTIFICIAL (x) |
7905 | && DECL_DEFAULTED_IN_CLASS_P (x) |
7906 | && special_function_p (x) == sfk_comparison) |
7907 | defaulted_late_check (x); |
7908 | DECL_IN_AGGR_P (x) = false; |
7909 | } |
7910 | else if (VAR_P (x) && TREE_STATIC (x) |
7911 | && TREE_TYPE (x) != error_mark_node |
7912 | && same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (x)), t)) |
7913 | SET_DECL_MODE (x, TYPE_MODE (t)); |
7914 | |
7915 | /* Complain if one of the field types requires lower visibility. */ |
7916 | constrain_class_visibility (t); |
7917 | |
7918 | /* Make the rtl for any new vtables we have created, and unmark |
7919 | the base types we marked. */ |
7920 | finish_vtbls (t); |
7921 | |
7922 | /* Build the VTT for T. */ |
7923 | build_vtt (t); |
7924 | |
7925 | if (warn_nonvdtor |
7926 | && TYPE_POLYMORPHIC_P (t) && accessible_nvdtor_p (t) |
7927 | && !CLASSTYPE_FINAL (t)) |
7928 | warning (OPT_Wnon_virtual_dtor, |
7929 | "%q#T has virtual functions and accessible" |
7930 | " non-virtual destructor" , t); |
7931 | |
7932 | complete_vars (t); |
7933 | |
7934 | if (warn_overloaded_virtual) |
7935 | warn_hidden (t); |
7936 | |
7937 | /* Class layout, assignment of virtual table slots, etc., is now |
7938 | complete. Give the back end a chance to tweak the visibility of |
7939 | the class or perform any other required target modifications. */ |
7940 | targetm.cxx.adjust_class_at_definition (t); |
7941 | |
7942 | maybe_suppress_debug_info (t); |
7943 | |
7944 | if (flag_vtable_verify) |
7945 | vtv_save_class_info (t); |
7946 | |
7947 | dump_class_hierarchy (t); |
7948 | |
7949 | /* Finish debugging output for this type. */ |
7950 | rest_of_type_compilation (t, ! LOCAL_CLASS_P (t)); |
7951 | |
7952 | if (TYPE_TRANSPARENT_AGGR (t)) |
7953 | { |
7954 | tree field = first_field (t); |
7955 | if (field == NULL_TREE || error_operand_p (t: field)) |
7956 | { |
7957 | error ("type transparent %q#T does not have any fields" , t); |
7958 | TYPE_TRANSPARENT_AGGR (t) = 0; |
7959 | } |
7960 | else if (DECL_ARTIFICIAL (field)) |
7961 | { |
7962 | if (DECL_FIELD_IS_BASE (field)) |
7963 | error ("type transparent class %qT has base classes" , t); |
7964 | else |
7965 | { |
7966 | gcc_checking_assert (DECL_VIRTUAL_P (field)); |
7967 | error ("type transparent class %qT has virtual functions" , t); |
7968 | } |
7969 | TYPE_TRANSPARENT_AGGR (t) = 0; |
7970 | } |
7971 | else if (TYPE_MODE (t) != DECL_MODE (field)) |
7972 | { |
7973 | error ("type transparent %q#T cannot be made transparent because " |
7974 | "the type of the first field has a different ABI from the " |
7975 | "class overall" , t); |
7976 | TYPE_TRANSPARENT_AGGR (t) = 0; |
7977 | } |
7978 | } |
7979 | } |
7980 | |
7981 | /* When T was built up, the member declarations were added in reverse |
7982 | order. Rearrange them to declaration order. */ |
7983 | |
7984 | void |
7985 | unreverse_member_declarations (tree t) |
7986 | { |
7987 | tree next; |
7988 | tree prev; |
7989 | tree x; |
7990 | |
7991 | /* The following lists are all in reverse order. Put them in |
7992 | declaration order now. */ |
7993 | CLASSTYPE_DECL_LIST (t) = nreverse (CLASSTYPE_DECL_LIST (t)); |
7994 | |
7995 | /* For the TYPE_FIELDS, only the non TYPE_DECLs are in reverse |
7996 | order, so we can't just use nreverse. Due to stat_hack |
7997 | chicanery in finish_member_declaration. */ |
7998 | prev = NULL_TREE; |
7999 | for (x = TYPE_FIELDS (t); |
8000 | x && TREE_CODE (x) != TYPE_DECL; |
8001 | x = next) |
8002 | { |
8003 | next = DECL_CHAIN (x); |
8004 | DECL_CHAIN (x) = prev; |
8005 | prev = x; |
8006 | } |
8007 | |
8008 | if (prev) |
8009 | { |
8010 | DECL_CHAIN (TYPE_FIELDS (t)) = x; |
8011 | TYPE_FIELDS (t) = prev; |
8012 | } |
8013 | } |
8014 | |
8015 | /* Classes, structs or unions T marked with hotness attributes propagate |
8016 | the attribute to all methods. */ |
8017 | |
8018 | void |
8019 | propagate_class_warmth_attribute (tree t) |
8020 | { |
8021 | if (t == NULL_TREE |
8022 | || !(TREE_CODE (t) == RECORD_TYPE |
8023 | || TREE_CODE (t) == UNION_TYPE)) |
8024 | return; |
8025 | |
8026 | tree class_has_cold_attr |
8027 | = lookup_attribute (attr_name: "cold" , TYPE_ATTRIBUTES (t)); |
8028 | tree class_has_hot_attr |
8029 | = lookup_attribute (attr_name: "hot" , TYPE_ATTRIBUTES (t)); |
8030 | |
8031 | if (class_has_cold_attr || class_has_hot_attr) |
8032 | for (tree f = TYPE_FIELDS (t); f; f = DECL_CHAIN (f)) |
8033 | if (DECL_DECLARES_FUNCTION_P (f)) |
8034 | maybe_propagate_warmth_attributes (STRIP_TEMPLATE (f), t); |
8035 | } |
8036 | |
8037 | tree |
8038 | finish_struct (tree t, tree attributes) |
8039 | { |
8040 | location_t saved_loc = input_location; |
8041 | |
8042 | /* Now that we've got all the field declarations, reverse everything |
8043 | as necessary. */ |
8044 | unreverse_member_declarations (t); |
8045 | |
8046 | cplus_decl_attributes (&t, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE); |
8047 | fixup_attribute_variants (t); |
8048 | |
8049 | /* Nadger the current location so that diagnostics point to the start of |
8050 | the struct, not the end. */ |
8051 | input_location = DECL_SOURCE_LOCATION (TYPE_NAME (t)); |
8052 | |
8053 | if (processing_template_decl) |
8054 | { |
8055 | tree x; |
8056 | |
8057 | for (x = TYPE_FIELDS (t); x; x = DECL_CHAIN (x)) |
8058 | if (DECL_DECLARES_FUNCTION_P (x)) |
8059 | { |
8060 | DECL_IN_AGGR_P (x) = false; |
8061 | if (DECL_VIRTUAL_P (x)) |
8062 | CLASSTYPE_NON_AGGREGATE (t) = true; |
8063 | } |
8064 | else if (TREE_CODE (x) == FIELD_DECL) |
8065 | { |
8066 | if (TREE_PROTECTED (x) || TREE_PRIVATE (x)) |
8067 | CLASSTYPE_NON_AGGREGATE (t) = true; |
8068 | } |
8069 | |
8070 | /* Also add a USING_DECL for operator=. We know there'll be (at |
8071 | least) one, but we don't know the signature(s). We want name |
8072 | lookup not to fail or recurse into bases. This isn't added |
8073 | to the template decl list so we drop this at instantiation |
8074 | time. */ |
8075 | tree ass_op = build_lang_decl (USING_DECL, assign_op_identifier, |
8076 | NULL_TREE); |
8077 | DECL_CONTEXT (ass_op) = t; |
8078 | USING_DECL_SCOPE (ass_op) = t; |
8079 | DECL_DEPENDENT_P (ass_op) = true; |
8080 | DECL_ARTIFICIAL (ass_op) = true; |
8081 | DECL_CHAIN (ass_op) = TYPE_FIELDS (t); |
8082 | TYPE_FIELDS (t) = ass_op; |
8083 | |
8084 | TYPE_SIZE (t) = bitsize_zero_node; |
8085 | TYPE_SIZE_UNIT (t) = size_zero_node; |
8086 | /* COMPLETE_TYPE_P is now true. */ |
8087 | |
8088 | set_class_bindings (t); |
8089 | |
8090 | /* We need to emit an error message if this type was used as a parameter |
8091 | and it is an abstract type, even if it is a template. We construct |
8092 | a simple CLASSTYPE_PURE_VIRTUALS list without taking bases into |
8093 | account and we call complete_vars with this type, which will check |
8094 | the PARM_DECLS. Note that while the type is being defined, |
8095 | CLASSTYPE_PURE_VIRTUALS contains the list of the inline friends |
8096 | (see CLASSTYPE_INLINE_FRIENDS) so we need to clear it. */ |
8097 | CLASSTYPE_PURE_VIRTUALS (t) = NULL; |
8098 | for (x = TYPE_FIELDS (t); x; x = DECL_CHAIN (x)) |
8099 | if (TREE_CODE (x) == FUNCTION_DECL && DECL_PURE_VIRTUAL_P (x)) |
8100 | vec_safe_push (CLASSTYPE_PURE_VIRTUALS (t), obj: x); |
8101 | complete_vars (t); |
8102 | |
8103 | /* Remember current #pragma pack value. */ |
8104 | TYPE_PRECISION (t) = maximum_field_alignment; |
8105 | |
8106 | if (cxx_dialect < cxx20) |
8107 | { |
8108 | if (!CLASSTYPE_NON_AGGREGATE (t) |
8109 | && type_has_user_provided_or_explicit_constructor (t)) |
8110 | CLASSTYPE_NON_AGGREGATE (t) = 1; |
8111 | } |
8112 | else if (TYPE_HAS_USER_CONSTRUCTOR (t)) |
8113 | CLASSTYPE_NON_AGGREGATE (t) = 1; |
8114 | |
8115 | /* Fix up any variants we've already built. */ |
8116 | fixup_type_variants (type: t); |
8117 | } |
8118 | else |
8119 | finish_struct_1 (t); |
8120 | /* COMPLETE_TYPE_P is now true. */ |
8121 | |
8122 | maybe_warn_about_overly_private_class (t); |
8123 | |
8124 | if (is_std_init_list (t)) |
8125 | { |
8126 | /* People keep complaining that the compiler crashes on an invalid |
8127 | definition of initializer_list, so I guess we should explicitly |
8128 | reject it. What the compiler internals care about is that it's a |
8129 | template and has a pointer field followed by size_type field. */ |
8130 | bool ok = false; |
8131 | if (processing_template_decl) |
8132 | { |
8133 | tree f = next_aggregate_field (TYPE_FIELDS (t)); |
8134 | if (f && TYPE_PTR_P (TREE_TYPE (f))) |
8135 | { |
8136 | f = next_aggregate_field (DECL_CHAIN (f)); |
8137 | if (f && same_type_p (TREE_TYPE (f), size_type_node)) |
8138 | ok = true; |
8139 | } |
8140 | } |
8141 | /* It also cannot be a union. */ |
8142 | ok &= NON_UNION_CLASS_TYPE_P (t); |
8143 | if (!ok) |
8144 | fatal_error (input_location, "definition of %qD does not match " |
8145 | "%<#include <initializer_list>%>" , TYPE_NAME (t)); |
8146 | } |
8147 | |
8148 | input_location = saved_loc; |
8149 | |
8150 | TYPE_BEING_DEFINED (t) = 0; |
8151 | |
8152 | if (current_class_type) |
8153 | popclass (); |
8154 | else |
8155 | error ("trying to finish struct, but kicked out due to previous parse errors" ); |
8156 | |
8157 | if (flag_openmp) |
8158 | for (tree decl = TYPE_FIELDS (t); decl; decl = DECL_CHAIN (decl)) |
8159 | if (TREE_CODE (decl) == FUNCTION_DECL |
8160 | && DECL_OBJECT_MEMBER_FUNCTION_P (decl)) |
8161 | if (tree attr = lookup_attribute (attr_name: "omp declare variant base" , |
8162 | DECL_ATTRIBUTES (decl))) |
8163 | omp_declare_variant_finalize (decl, attr); |
8164 | |
8165 | if (processing_template_decl && at_function_scope_p () |
8166 | /* Lambdas are defined by the LAMBDA_EXPR. */ |
8167 | && !LAMBDA_TYPE_P (t)) |
8168 | add_stmt (build_min (TAG_DEFN, t)); |
8169 | |
8170 | return t; |
8171 | } |
8172 | |
8173 | /* Hash table to avoid endless recursion when handling references. */ |
8174 | static hash_table<nofree_ptr_hash<tree_node> > *fixed_type_or_null_ref_ht; |
8175 | |
8176 | /* Return the dynamic type of INSTANCE, if known. |
8177 | Used to determine whether the virtual function table is needed |
8178 | or not. |
8179 | |
8180 | *NONNULL is set iff INSTANCE can be known to be nonnull, regardless |
8181 | of our knowledge of its type. *NONNULL should be initialized |
8182 | before this function is called. */ |
8183 | |
8184 | static tree |
8185 | fixed_type_or_null (tree instance, int *nonnull, int *cdtorp) |
8186 | { |
8187 | #define RECUR(T) fixed_type_or_null((T), nonnull, cdtorp) |
8188 | |
8189 | switch (TREE_CODE (instance)) |
8190 | { |
8191 | case INDIRECT_REF: |
8192 | if (INDIRECT_TYPE_P (TREE_TYPE (instance))) |
8193 | return NULL_TREE; |
8194 | else |
8195 | return RECUR (TREE_OPERAND (instance, 0)); |
8196 | |
8197 | case CALL_EXPR: |
8198 | /* This is a call to a constructor, hence it's never zero. */ |
8199 | if (CALL_EXPR_FN (instance) |
8200 | && TREE_HAS_CONSTRUCTOR (instance)) |
8201 | { |
8202 | if (nonnull) |
8203 | *nonnull = 1; |
8204 | return TREE_TYPE (instance); |
8205 | } |
8206 | return NULL_TREE; |
8207 | |
8208 | case SAVE_EXPR: |
8209 | /* This is a call to a constructor, hence it's never zero. */ |
8210 | if (TREE_HAS_CONSTRUCTOR (instance)) |
8211 | { |
8212 | if (nonnull) |
8213 | *nonnull = 1; |
8214 | return TREE_TYPE (instance); |
8215 | } |
8216 | return RECUR (TREE_OPERAND (instance, 0)); |
8217 | |
8218 | case POINTER_PLUS_EXPR: |
8219 | case PLUS_EXPR: |
8220 | case MINUS_EXPR: |
8221 | if (TREE_CODE (TREE_OPERAND (instance, 0)) == ADDR_EXPR) |
8222 | return RECUR (TREE_OPERAND (instance, 0)); |
8223 | if (TREE_CODE (TREE_OPERAND (instance, 1)) == INTEGER_CST) |
8224 | /* Propagate nonnull. */ |
8225 | return RECUR (TREE_OPERAND (instance, 0)); |
8226 | |
8227 | return NULL_TREE; |
8228 | |
8229 | CASE_CONVERT: |
8230 | return RECUR (TREE_OPERAND (instance, 0)); |
8231 | |
8232 | case ADDR_EXPR: |
8233 | instance = TREE_OPERAND (instance, 0); |
8234 | if (nonnull) |
8235 | { |
8236 | /* Just because we see an ADDR_EXPR doesn't mean we're dealing |
8237 | with a real object -- given &p->f, p can still be null. */ |
8238 | tree t = get_base_address (t: instance); |
8239 | /* ??? Probably should check DECL_WEAK here. */ |
8240 | if (t && DECL_P (t)) |
8241 | *nonnull = 1; |
8242 | } |
8243 | return RECUR (instance); |
8244 | |
8245 | case COMPONENT_REF: |
8246 | /* If this component is really a base class reference, then the field |
8247 | itself isn't definitive. */ |
8248 | if (DECL_FIELD_IS_BASE (TREE_OPERAND (instance, 1))) |
8249 | return RECUR (TREE_OPERAND (instance, 0)); |
8250 | return RECUR (TREE_OPERAND (instance, 1)); |
8251 | |
8252 | case VAR_DECL: |
8253 | case FIELD_DECL: |
8254 | if (TREE_CODE (TREE_TYPE (instance)) == ARRAY_TYPE |
8255 | && MAYBE_CLASS_TYPE_P (TREE_TYPE (TREE_TYPE (instance)))) |
8256 | { |
8257 | if (nonnull) |
8258 | *nonnull = 1; |
8259 | return TREE_TYPE (TREE_TYPE (instance)); |
8260 | } |
8261 | /* fall through. */ |
8262 | case TARGET_EXPR: |
8263 | case PARM_DECL: |
8264 | case RESULT_DECL: |
8265 | if (MAYBE_CLASS_TYPE_P (TREE_TYPE (instance))) |
8266 | { |
8267 | if (nonnull) |
8268 | *nonnull = 1; |
8269 | return TREE_TYPE (instance); |
8270 | } |
8271 | else if (instance == current_class_ptr) |
8272 | { |
8273 | if (nonnull) |
8274 | *nonnull = 1; |
8275 | |
8276 | /* if we're in a ctor or dtor, we know our type. If |
8277 | current_class_ptr is set but we aren't in a function, we're in |
8278 | an NSDMI (and therefore a constructor). */ |
8279 | if (current_scope () != current_function_decl |
8280 | || (DECL_LANG_SPECIFIC (current_function_decl) |
8281 | && (DECL_CONSTRUCTOR_P (current_function_decl) |
8282 | || DECL_DESTRUCTOR_P (current_function_decl)))) |
8283 | { |
8284 | if (cdtorp) |
8285 | *cdtorp = 1; |
8286 | return TREE_TYPE (TREE_TYPE (instance)); |
8287 | } |
8288 | } |
8289 | else if (TYPE_REF_P (TREE_TYPE (instance))) |
8290 | { |
8291 | /* We only need one hash table because it is always left empty. */ |
8292 | if (!fixed_type_or_null_ref_ht) |
8293 | fixed_type_or_null_ref_ht |
8294 | = new hash_table<nofree_ptr_hash<tree_node> > (37); |
8295 | |
8296 | /* Reference variables should be references to objects. */ |
8297 | if (nonnull) |
8298 | *nonnull = 1; |
8299 | |
8300 | /* Enter the INSTANCE in a table to prevent recursion; a |
8301 | variable's initializer may refer to the variable |
8302 | itself. */ |
8303 | if (VAR_P (instance) |
8304 | && DECL_INITIAL (instance) |
8305 | && !type_dependent_expression_p_push (DECL_INITIAL (instance)) |
8306 | && !fixed_type_or_null_ref_ht->find (value: instance)) |
8307 | { |
8308 | tree type; |
8309 | tree_node **slot; |
8310 | |
8311 | slot = fixed_type_or_null_ref_ht->find_slot (value: instance, insert: INSERT); |
8312 | *slot = instance; |
8313 | type = RECUR (DECL_INITIAL (instance)); |
8314 | fixed_type_or_null_ref_ht->remove_elt (value: instance); |
8315 | |
8316 | return type; |
8317 | } |
8318 | } |
8319 | return NULL_TREE; |
8320 | |
8321 | case VIEW_CONVERT_EXPR: |
8322 | if (location_wrapper_p (exp: instance)) |
8323 | return RECUR (TREE_OPERAND (instance, 0)); |
8324 | else |
8325 | /* TODO: Recursion may be correct for some non-location-wrapper |
8326 | uses of VIEW_CONVERT_EXPR. */ |
8327 | return NULL_TREE; |
8328 | |
8329 | default: |
8330 | return NULL_TREE; |
8331 | } |
8332 | #undef RECUR |
8333 | } |
8334 | |
8335 | /* Return nonzero if the dynamic type of INSTANCE is known, and |
8336 | equivalent to the static type. We also handle the case where |
8337 | INSTANCE is really a pointer. Return negative if this is a |
8338 | ctor/dtor. There the dynamic type is known, but this might not be |
8339 | the most derived base of the original object, and hence virtual |
8340 | bases may not be laid out according to this type. |
8341 | |
8342 | Used to determine whether the virtual function table is needed |
8343 | or not. |
8344 | |
8345 | *NONNULL is set iff INSTANCE can be known to be nonnull, regardless |
8346 | of our knowledge of its type. *NONNULL should be initialized |
8347 | before this function is called. */ |
8348 | |
8349 | int |
8350 | resolves_to_fixed_type_p (tree instance, int* nonnull) |
8351 | { |
8352 | tree t = TREE_TYPE (instance); |
8353 | int cdtorp = 0; |
8354 | tree fixed; |
8355 | |
8356 | /* processing_template_decl can be false in a template if we're in |
8357 | instantiate_non_dependent_expr, but we still want to suppress |
8358 | this check. */ |
8359 | if (in_template_context) |
8360 | { |
8361 | /* In a template we only care about the type of the result. */ |
8362 | if (nonnull) |
8363 | *nonnull = true; |
8364 | return true; |
8365 | } |
8366 | |
8367 | fixed = fixed_type_or_null (instance, nonnull, cdtorp: &cdtorp); |
8368 | if (INDIRECT_TYPE_P (t)) |
8369 | t = TREE_TYPE (t); |
8370 | if (CLASS_TYPE_P (t) && CLASSTYPE_FINAL (t)) |
8371 | return 1; |
8372 | if (fixed == NULL_TREE) |
8373 | return 0; |
8374 | if (!same_type_ignoring_top_level_qualifiers_p (t, fixed)) |
8375 | return 0; |
8376 | return cdtorp ? -1 : 1; |
8377 | } |
8378 | |
8379 | |
8380 | void |
8381 | init_class_processing (void) |
8382 | { |
8383 | current_class_depth = 0; |
8384 | current_class_stack_size = 10; |
8385 | current_class_stack |
8386 | = XNEWVEC (struct class_stack_node, current_class_stack_size); |
8387 | sizeof_biggest_empty_class = size_zero_node; |
8388 | |
8389 | ridpointers[(int) RID_PUBLIC] = access_public_node; |
8390 | ridpointers[(int) RID_PRIVATE] = access_private_node; |
8391 | ridpointers[(int) RID_PROTECTED] = access_protected_node; |
8392 | } |
8393 | |
8394 | /* Restore the cached PREVIOUS_CLASS_LEVEL. */ |
8395 | |
8396 | static void |
8397 | restore_class_cache (void) |
8398 | { |
8399 | tree type; |
8400 | |
8401 | /* We are re-entering the same class we just left, so we don't |
8402 | have to search the whole inheritance matrix to find all the |
8403 | decls to bind again. Instead, we install the cached |
8404 | class_shadowed list and walk through it binding names. */ |
8405 | push_binding_level (previous_class_level); |
8406 | class_binding_level = previous_class_level; |
8407 | /* Restore IDENTIFIER_TYPE_VALUE. */ |
8408 | for (type = class_binding_level->type_shadowed; |
8409 | type; |
8410 | type = TREE_CHAIN (type)) |
8411 | SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (type), TREE_TYPE (type)); |
8412 | } |
8413 | |
8414 | /* Set global variables CURRENT_CLASS_NAME and CURRENT_CLASS_TYPE as |
8415 | appropriate for TYPE. |
8416 | |
8417 | So that we may avoid calls to lookup_name, we cache the _TYPE |
8418 | nodes of local TYPE_DECLs in the TREE_TYPE field of the name. |
8419 | |
8420 | For multiple inheritance, we perform a two-pass depth-first search |
8421 | of the type lattice. */ |
8422 | |
8423 | void |
8424 | pushclass (tree type) |
8425 | { |
8426 | class_stack_node_t csn; |
8427 | |
8428 | type = TYPE_MAIN_VARIANT (type); |
8429 | |
8430 | /* Make sure there is enough room for the new entry on the stack. */ |
8431 | if (current_class_depth + 1 >= current_class_stack_size) |
8432 | { |
8433 | current_class_stack_size *= 2; |
8434 | current_class_stack |
8435 | = XRESIZEVEC (struct class_stack_node, current_class_stack, |
8436 | current_class_stack_size); |
8437 | } |
8438 | |
8439 | /* Insert a new entry on the class stack. */ |
8440 | csn = current_class_stack + current_class_depth; |
8441 | csn->name = current_class_name; |
8442 | csn->type = current_class_type; |
8443 | csn->access = current_access_specifier; |
8444 | csn->names_used = 0; |
8445 | csn->hidden = 0; |
8446 | current_class_depth++; |
8447 | |
8448 | /* Now set up the new type. */ |
8449 | current_class_name = TYPE_NAME (type); |
8450 | if (TREE_CODE (current_class_name) == TYPE_DECL) |
8451 | current_class_name = DECL_NAME (current_class_name); |
8452 | current_class_type = type; |
8453 | |
8454 | /* By default, things in classes are private, while things in |
8455 | structures or unions are public. */ |
8456 | current_access_specifier = (CLASSTYPE_DECLARED_CLASS (type) |
8457 | ? access_private_node |
8458 | : access_public_node); |
8459 | |
8460 | if (previous_class_level |
8461 | && type != previous_class_level->this_entity |
8462 | && current_class_depth == 1) |
8463 | { |
8464 | /* Forcibly remove any old class remnants. */ |
8465 | invalidate_class_lookup_cache (); |
8466 | } |
8467 | |
8468 | if (!previous_class_level |
8469 | || type != previous_class_level->this_entity |
8470 | || current_class_depth > 1) |
8471 | pushlevel_class (); |
8472 | else |
8473 | restore_class_cache (); |
8474 | } |
8475 | |
8476 | /* Get out of the current class scope. If we were in a class scope |
8477 | previously, that is the one popped to. */ |
8478 | |
8479 | void |
8480 | popclass (void) |
8481 | { |
8482 | poplevel_class (); |
8483 | |
8484 | current_class_depth--; |
8485 | current_class_name = current_class_stack[current_class_depth].name; |
8486 | current_class_type = current_class_stack[current_class_depth].type; |
8487 | current_access_specifier = current_class_stack[current_class_depth].access; |
8488 | if (current_class_stack[current_class_depth].names_used) |
8489 | splay_tree_delete (current_class_stack[current_class_depth].names_used); |
8490 | } |
8491 | |
8492 | /* Mark the top of the class stack as hidden. */ |
8493 | |
8494 | void |
8495 | push_class_stack (void) |
8496 | { |
8497 | if (current_class_depth) |
8498 | ++current_class_stack[current_class_depth - 1].hidden; |
8499 | } |
8500 | |
8501 | /* Mark the top of the class stack as un-hidden. */ |
8502 | |
8503 | void |
8504 | pop_class_stack (void) |
8505 | { |
8506 | if (current_class_depth) |
8507 | --current_class_stack[current_class_depth - 1].hidden; |
8508 | } |
8509 | |
8510 | /* If the class type currently being defined is either T or |
8511 | a nested type of T, returns the type from the current_class_stack, |
8512 | which might be equivalent to but not equal to T in case of |
8513 | constrained partial specializations. */ |
8514 | |
8515 | tree |
8516 | currently_open_class (tree t) |
8517 | { |
8518 | int i; |
8519 | |
8520 | if (!CLASS_TYPE_P (t)) |
8521 | return NULL_TREE; |
8522 | |
8523 | t = TYPE_MAIN_VARIANT (t); |
8524 | |
8525 | /* We start looking from 1 because entry 0 is from global scope, |
8526 | and has no type. */ |
8527 | for (i = current_class_depth; i > 0; --i) |
8528 | { |
8529 | tree c; |
8530 | if (i == current_class_depth) |
8531 | c = current_class_type; |
8532 | else |
8533 | { |
8534 | if (current_class_stack[i].hidden) |
8535 | break; |
8536 | c = current_class_stack[i].type; |
8537 | } |
8538 | if (!c) |
8539 | continue; |
8540 | if (same_type_p (c, t)) |
8541 | return c; |
8542 | } |
8543 | return NULL_TREE; |
8544 | } |
8545 | |
8546 | /* If either current_class_type or one of its enclosing classes are derived |
8547 | from T, return the appropriate type. Used to determine how we found |
8548 | something via unqualified lookup. */ |
8549 | |
8550 | tree |
8551 | currently_open_derived_class (tree t) |
8552 | { |
8553 | int i; |
8554 | |
8555 | /* The bases of a dependent type are unknown. */ |
8556 | if (dependent_type_p (t)) |
8557 | return NULL_TREE; |
8558 | |
8559 | if (!current_class_type) |
8560 | return NULL_TREE; |
8561 | |
8562 | if (DERIVED_FROM_P (t, current_class_type)) |
8563 | return current_class_type; |
8564 | |
8565 | for (i = current_class_depth - 1; i > 0; --i) |
8566 | { |
8567 | if (current_class_stack[i].hidden) |
8568 | break; |
8569 | if (DERIVED_FROM_P (t, current_class_stack[i].type)) |
8570 | return current_class_stack[i].type; |
8571 | } |
8572 | |
8573 | return NULL_TREE; |
8574 | } |
8575 | |
8576 | /* Return the outermost enclosing class type that is still open, or |
8577 | NULL_TREE. */ |
8578 | |
8579 | tree |
8580 | outermost_open_class (void) |
8581 | { |
8582 | if (!current_class_type) |
8583 | return NULL_TREE; |
8584 | tree r = NULL_TREE; |
8585 | if (TYPE_BEING_DEFINED (current_class_type)) |
8586 | r = current_class_type; |
8587 | for (int i = current_class_depth - 1; i > 0; --i) |
8588 | { |
8589 | if (current_class_stack[i].hidden) |
8590 | break; |
8591 | tree t = current_class_stack[i].type; |
8592 | if (!TYPE_BEING_DEFINED (t)) |
8593 | break; |
8594 | r = t; |
8595 | } |
8596 | return r; |
8597 | } |
8598 | |
8599 | /* Returns the innermost class type which is not a lambda closure type. */ |
8600 | |
8601 | tree |
8602 | current_nonlambda_class_type (void) |
8603 | { |
8604 | tree type = current_class_type; |
8605 | while (type && LAMBDA_TYPE_P (type)) |
8606 | type = decl_type_context (TYPE_NAME (type)); |
8607 | return type; |
8608 | } |
8609 | |
8610 | /* When entering a class scope, all enclosing class scopes' names with |
8611 | static meaning (static variables, static functions, types and |
8612 | enumerators) have to be visible. This recursive function calls |
8613 | pushclass for all enclosing class contexts until global or a local |
8614 | scope is reached. TYPE is the enclosed class. */ |
8615 | |
8616 | void |
8617 | push_nested_class (tree type) |
8618 | { |
8619 | /* A namespace might be passed in error cases, like A::B:C. */ |
8620 | if (type == NULL_TREE |
8621 | || !CLASS_TYPE_P (type)) |
8622 | return; |
8623 | |
8624 | push_nested_class (DECL_CONTEXT (TYPE_MAIN_DECL (type))); |
8625 | |
8626 | pushclass (type); |
8627 | } |
8628 | |
8629 | /* Undoes a push_nested_class call. */ |
8630 | |
8631 | void |
8632 | pop_nested_class (void) |
8633 | { |
8634 | tree context = DECL_CONTEXT (TYPE_MAIN_DECL (current_class_type)); |
8635 | |
8636 | popclass (); |
8637 | if (context && CLASS_TYPE_P (context)) |
8638 | pop_nested_class (); |
8639 | } |
8640 | |
8641 | /* Returns the number of extern "LANG" blocks we are nested within. */ |
8642 | |
8643 | int |
8644 | current_lang_depth (void) |
8645 | { |
8646 | return vec_safe_length (current_lang_base); |
8647 | } |
8648 | |
8649 | /* Set global variables CURRENT_LANG_NAME to appropriate value |
8650 | so that behavior of name-mangling machinery is correct. */ |
8651 | |
8652 | void |
8653 | push_lang_context (tree name) |
8654 | { |
8655 | vec_safe_push (current_lang_base, current_lang_name); |
8656 | |
8657 | if (name == lang_name_cplusplus) |
8658 | current_lang_name = name; |
8659 | else if (name == lang_name_c) |
8660 | current_lang_name = name; |
8661 | else |
8662 | error ("language string %<\"%E\"%> not recognized" , name); |
8663 | } |
8664 | |
8665 | /* Get out of the current language scope. */ |
8666 | |
8667 | void |
8668 | pop_lang_context (void) |
8669 | { |
8670 | current_lang_name = current_lang_base->pop (); |
8671 | } |
8672 | |
8673 | /* Type instantiation routines. */ |
8674 | |
8675 | /* Given an OVERLOAD and a TARGET_TYPE, return the function that |
8676 | matches the TARGET_TYPE. If there is no satisfactory match, return |
8677 | error_mark_node, and issue an error & warning messages under |
8678 | control of FLAGS. Permit pointers to member function if FLAGS |
8679 | permits. If TEMPLATE_ONLY, the name of the overloaded function was |
8680 | a template-id, and EXPLICIT_TARGS are the explicitly provided |
8681 | template arguments. |
8682 | |
8683 | If OVERLOAD is for one or more member functions, then ACCESS_PATH |
8684 | is the base path used to reference those member functions. If |
8685 | the address is resolved to a member function, access checks will be |
8686 | performed and errors issued if appropriate. */ |
8687 | |
8688 | static tree |
8689 | resolve_address_of_overloaded_function (tree target_type, |
8690 | tree overload, |
8691 | tsubst_flags_t complain, |
8692 | bool template_only, |
8693 | tree explicit_targs, |
8694 | tree access_path) |
8695 | { |
8696 | /* Here's what the standard says: |
8697 | |
8698 | [over.over] |
8699 | |
8700 | If the name is a function template, template argument deduction |
8701 | is done, and if the argument deduction succeeds, the deduced |
8702 | arguments are used to generate a single template function, which |
8703 | is added to the set of overloaded functions considered. |
8704 | |
8705 | Non-member functions and static member functions match targets of |
8706 | type "pointer-to-function" or "reference-to-function." Nonstatic |
8707 | member functions match targets of type "pointer-to-member |
8708 | function;" the function type of the pointer to member is used to |
8709 | select the member function from the set of overloaded member |
8710 | functions. If a non-static member function is selected, the |
8711 | reference to the overloaded function name is required to have the |
8712 | form of a pointer to member as described in 5.3.1. |
8713 | |
8714 | If more than one function is selected, any template functions in |
8715 | the set are eliminated if the set also contains a non-template |
8716 | function, and any given template function is eliminated if the |
8717 | set contains a second template function that is more specialized |
8718 | than the first according to the partial ordering rules 14.5.5.2. |
8719 | After such eliminations, if any, there shall remain exactly one |
8720 | selected function. */ |
8721 | |
8722 | int is_ptrmem = 0; |
8723 | /* We store the matches in a TREE_LIST rooted here. The functions |
8724 | are the TREE_PURPOSE, not the TREE_VALUE, in this list, for easy |
8725 | interoperability with most_specialized_instantiation. */ |
8726 | tree matches = NULL_TREE; |
8727 | tree fn; |
8728 | tree target_fn_type; |
8729 | |
8730 | /* By the time we get here, we should be seeing only real |
8731 | pointer-to-member types, not the internal POINTER_TYPE to |
8732 | METHOD_TYPE representation. */ |
8733 | gcc_assert (!TYPE_PTR_P (target_type) |
8734 | || TREE_CODE (TREE_TYPE (target_type)) != METHOD_TYPE); |
8735 | |
8736 | gcc_assert (is_overloaded_fn (overload)); |
8737 | |
8738 | /* Check that the TARGET_TYPE is reasonable. */ |
8739 | if (TYPE_PTRFN_P (target_type) |
8740 | || TYPE_REFFN_P (target_type)) |
8741 | /* This is OK. */; |
8742 | else if (TYPE_PTRMEMFUNC_P (target_type)) |
8743 | /* This is OK, too. */ |
8744 | is_ptrmem = 1; |
8745 | else if (TREE_CODE (target_type) == FUNCTION_TYPE) |
8746 | /* This is OK, too. This comes from a conversion to reference |
8747 | type. */ |
8748 | target_type = build_reference_type (target_type); |
8749 | else |
8750 | { |
8751 | if (complain & tf_error) |
8752 | error ("cannot resolve overloaded function %qD based on" |
8753 | " conversion to type %qT" , |
8754 | OVL_NAME (overload), target_type); |
8755 | return error_mark_node; |
8756 | } |
8757 | |
8758 | /* Non-member functions and static member functions match targets of type |
8759 | "pointer-to-function" or "reference-to-function." Nonstatic member |
8760 | functions match targets of type "pointer-to-member-function;" the |
8761 | function type of the pointer to member is used to select the member |
8762 | function from the set of overloaded member functions. |
8763 | |
8764 | So figure out the FUNCTION_TYPE that we want to match against. */ |
8765 | target_fn_type = static_fn_type (target_type); |
8766 | |
8767 | /* If we can find a non-template function that matches, we can just |
8768 | use it. There's no point in generating template instantiations |
8769 | if we're just going to throw them out anyhow. But, of course, we |
8770 | can only do this when we don't *need* a template function. */ |
8771 | if (!template_only) |
8772 | for (lkp_iterator iter (overload); iter; ++iter) |
8773 | { |
8774 | tree fn = *iter; |
8775 | |
8776 | if (TREE_CODE (fn) == TEMPLATE_DECL) |
8777 | /* We're not looking for templates just yet. */ |
8778 | continue; |
8779 | |
8780 | if ((TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE) != is_ptrmem) |
8781 | /* We're looking for a non-static member, and this isn't |
8782 | one, or vice versa. */ |
8783 | continue; |
8784 | |
8785 | /* Constraints must be satisfied. This is done before |
8786 | return type deduction since that instantiates the |
8787 | function. */ |
8788 | if (!constraints_satisfied_p (fn)) |
8789 | continue; |
8790 | |
8791 | if (undeduced_auto_decl (fn)) |
8792 | { |
8793 | /* Force instantiation to do return type deduction. */ |
8794 | maybe_instantiate_decl (fn); |
8795 | require_deduced_type (fn); |
8796 | } |
8797 | |
8798 | /* In C++17 we need the noexcept-qualifier to compare types. */ |
8799 | if (flag_noexcept_type |
8800 | && !maybe_instantiate_noexcept (fn, complain)) |
8801 | continue; |
8802 | |
8803 | /* See if there's a match. */ |
8804 | tree fntype = static_fn_type (fn); |
8805 | if (same_type_p (target_fn_type, fntype) |
8806 | || fnptr_conv_p (target_fn_type, fntype)) |
8807 | matches = tree_cons (fn, NULL_TREE, matches); |
8808 | } |
8809 | |
8810 | /* Now, if we've already got a match (or matches), there's no need |
8811 | to proceed to the template functions. But, if we don't have a |
8812 | match we need to look at them, too. */ |
8813 | if (!matches) |
8814 | { |
8815 | tree target_arg_types; |
8816 | tree target_ret_type; |
8817 | tree *args; |
8818 | unsigned int nargs, ia; |
8819 | tree arg; |
8820 | |
8821 | target_arg_types = TYPE_ARG_TYPES (target_fn_type); |
8822 | target_ret_type = TREE_TYPE (target_fn_type); |
8823 | |
8824 | nargs = list_length (target_arg_types); |
8825 | args = XALLOCAVEC (tree, nargs); |
8826 | for (arg = target_arg_types, ia = 0; |
8827 | arg != NULL_TREE; |
8828 | arg = TREE_CHAIN (arg), ++ia) |
8829 | args[ia] = TREE_VALUE (arg); |
8830 | nargs = ia; |
8831 | |
8832 | for (lkp_iterator iter (overload); iter; ++iter) |
8833 | { |
8834 | tree fn = *iter; |
8835 | tree instantiation; |
8836 | tree targs; |
8837 | |
8838 | if (TREE_CODE (fn) != TEMPLATE_DECL) |
8839 | /* We're only looking for templates. */ |
8840 | continue; |
8841 | |
8842 | if ((TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE) |
8843 | != is_ptrmem) |
8844 | /* We're not looking for a non-static member, and this is |
8845 | one, or vice versa. */ |
8846 | continue; |
8847 | |
8848 | tree ret = target_ret_type; |
8849 | |
8850 | /* If the template has a deduced return type, don't expose it to |
8851 | template argument deduction. */ |
8852 | if (undeduced_auto_decl (fn)) |
8853 | ret = NULL_TREE; |
8854 | |
8855 | /* Try to do argument deduction. */ |
8856 | targs = make_tree_vec (DECL_NTPARMS (fn)); |
8857 | instantiation = fn_type_unification (fn, explicit_targs, targs, args, |
8858 | nargs, ret, |
8859 | DEDUCE_EXACT, LOOKUP_NORMAL, |
8860 | NULL, false, false); |
8861 | if (instantiation == error_mark_node) |
8862 | /* Instantiation failed. */ |
8863 | continue; |
8864 | |
8865 | /* Constraints must be satisfied. This is done before |
8866 | return type deduction since that instantiates the |
8867 | function. */ |
8868 | if (flag_concepts && !constraints_satisfied_p (instantiation)) |
8869 | continue; |
8870 | |
8871 | /* And now force instantiation to do return type deduction. */ |
8872 | if (undeduced_auto_decl (instantiation)) |
8873 | { |
8874 | ++function_depth; |
8875 | instantiate_decl (instantiation, /*defer*/false, /*class*/false); |
8876 | --function_depth; |
8877 | |
8878 | require_deduced_type (instantiation); |
8879 | } |
8880 | |
8881 | /* In C++17 we need the noexcept-qualifier to compare types. */ |
8882 | if (flag_noexcept_type) |
8883 | maybe_instantiate_noexcept (instantiation, complain); |
8884 | |
8885 | /* See if there's a match. */ |
8886 | tree fntype = static_fn_type (instantiation); |
8887 | if (same_type_p (target_fn_type, fntype) |
8888 | || fnptr_conv_p (target_fn_type, fntype)) |
8889 | matches = tree_cons (instantiation, fn, matches); |
8890 | } |
8891 | |
8892 | /* Now, remove all but the most specialized of the matches. */ |
8893 | if (matches) |
8894 | { |
8895 | tree match = most_specialized_instantiation (matches); |
8896 | |
8897 | if (match != error_mark_node) |
8898 | matches = tree_cons (TREE_PURPOSE (match), |
8899 | NULL_TREE, |
8900 | NULL_TREE); |
8901 | } |
8902 | } |
8903 | |
8904 | /* Now we should have exactly one function in MATCHES. */ |
8905 | if (matches == NULL_TREE) |
8906 | { |
8907 | /* There were *no* matches. */ |
8908 | if (complain & tf_error) |
8909 | { |
8910 | error ("no matches converting function %qD to type %q#T" , |
8911 | OVL_NAME (overload), target_type); |
8912 | |
8913 | print_candidates (overload); |
8914 | } |
8915 | return error_mark_node; |
8916 | } |
8917 | else if (TREE_CHAIN (matches)) |
8918 | { |
8919 | /* There were too many matches. First check if they're all |
8920 | the same function. */ |
8921 | tree match = NULL_TREE; |
8922 | |
8923 | fn = TREE_PURPOSE (matches); |
8924 | |
8925 | /* For multi-versioned functions, more than one match is just fine and |
8926 | decls_match will return false as they are different. */ |
8927 | for (match = TREE_CHAIN (matches); match; match = TREE_CHAIN (match)) |
8928 | if (!decls_match (fn, TREE_PURPOSE (match)) |
8929 | && !targetm.target_option.function_versions |
8930 | (fn, TREE_PURPOSE (match))) |
8931 | break; |
8932 | |
8933 | if (match) |
8934 | { |
8935 | if (complain & tf_error) |
8936 | { |
8937 | error ("converting overloaded function %qD to type %q#T is ambiguous" , |
8938 | OVL_NAME (overload), target_type); |
8939 | |
8940 | /* Since print_candidates expects the functions in the |
8941 | TREE_VALUE slot, we flip them here. */ |
8942 | for (match = matches; match; match = TREE_CHAIN (match)) |
8943 | TREE_VALUE (match) = TREE_PURPOSE (match); |
8944 | |
8945 | print_candidates (matches); |
8946 | } |
8947 | |
8948 | return error_mark_node; |
8949 | } |
8950 | } |
8951 | |
8952 | /* Good, exactly one match. Now, convert it to the correct type. */ |
8953 | fn = TREE_PURPOSE (matches); |
8954 | |
8955 | if (DECL_OBJECT_MEMBER_FUNCTION_P (fn) |
8956 | && !(complain & tf_ptrmem_ok)) |
8957 | { |
8958 | /* Previously we allowed this behavior for iobj member functions when the |
8959 | -fms-extensions flag is passed as MSVC allows this as a language |
8960 | extension. MSVC also allows this for xobj member functions, but the |
8961 | documentation for -fms-extensions states it's purpose is to support |
8962 | the use of microsoft headers. Until otherwise demonstrated, we should |
8963 | assume xobj member functions are not used in this manner in microsoft |
8964 | headers and forbid the incorrect syntax instead of supporting it for |
8965 | non-legacy uses. This should hopefully encourage conformance going |
8966 | forward. |
8967 | This comment is referred to in typeck.cc:cp_build_addr_expr_1. */ |
8968 | if (DECL_IOBJ_MEMBER_FUNCTION_P (fn) && flag_ms_extensions) |
8969 | /* Early escape. */; |
8970 | else if (!(complain & tf_error)) |
8971 | return error_mark_node; |
8972 | else if (DECL_XOBJ_MEMBER_FUNCTION_P (fn)) |
8973 | { |
8974 | auto_diagnostic_group d; |
8975 | /* Should match the error in typeck.cc:cp_build_addr_expr_1. |
8976 | We seem to lack the details here to match that diagnostic exactly, |
8977 | perhaps this could be fixed in the future? See PR113075 bug 2. */ |
8978 | error_at (input_location, |
8979 | "ISO C++ forbids taking the address of an unqualified" |
8980 | " or parenthesized non-static member function to form" |
8981 | " a pointer to explicit object member function." ); |
8982 | /* This is incorrect, see PR113075 bug 3. */ |
8983 | inform (input_location, |
8984 | "a pointer to explicit object member function can only be " |
8985 | "formed with %<&%E%>" , fn); |
8986 | } |
8987 | else |
8988 | { |
8989 | static int explained; |
8990 | gcc_assert (DECL_IOBJ_MEMBER_FUNCTION_P (fn) && !flag_ms_extensions); |
8991 | /* Is there a reason this error message doesn't match the one in |
8992 | typeck.cc:cp_build_addr_expr_1? */ |
8993 | auto_diagnostic_group d; |
8994 | if (permerror (input_location, "assuming pointer to member %qD" , fn) |
8995 | && !explained) |
8996 | { |
8997 | inform (input_location, "(a pointer to member can only be " |
8998 | "formed with %<&%E%>)" , fn); |
8999 | explained = 1; |
9000 | } |
9001 | } |
9002 | } |
9003 | |
9004 | /* If a pointer to a function that is multi-versioned is requested, the |
9005 | pointer to the dispatcher function is returned instead. This works |
9006 | well because indirectly calling the function will dispatch the right |
9007 | function version at run-time. */ |
9008 | if (DECL_FUNCTION_VERSIONED (fn)) |
9009 | { |
9010 | fn = get_function_version_dispatcher (fn); |
9011 | if (fn == NULL) |
9012 | return error_mark_node; |
9013 | /* Mark all the versions corresponding to the dispatcher as used. */ |
9014 | if (!(complain & tf_conv)) |
9015 | mark_versions_used (fn); |
9016 | } |
9017 | |
9018 | /* If we're doing overload resolution purely for the purpose of |
9019 | determining conversion sequences, we should not consider the |
9020 | function used. If this conversion sequence is selected, the |
9021 | function will be marked as used at this point. */ |
9022 | if (!(complain & tf_conv)) |
9023 | { |
9024 | /* Make =delete work with SFINAE. */ |
9025 | if (DECL_DELETED_FN (fn) && !(complain & tf_error)) |
9026 | return error_mark_node; |
9027 | if (!mark_used (fn, complain) && !(complain & tf_error)) |
9028 | return error_mark_node; |
9029 | } |
9030 | |
9031 | /* We could not check access to member functions when this |
9032 | expression was originally created since we did not know at that |
9033 | time to which function the expression referred. */ |
9034 | if (DECL_FUNCTION_MEMBER_P (fn)) |
9035 | { |
9036 | gcc_assert (access_path); |
9037 | perform_or_defer_access_check (access_path, fn, fn, complain); |
9038 | } |
9039 | |
9040 | if (TYPE_PTRFN_P (target_type) || TYPE_PTRMEMFUNC_P (target_type)) |
9041 | return cp_build_addr_expr (fn, complain); |
9042 | else |
9043 | { |
9044 | /* The target must be a REFERENCE_TYPE. Above, cp_build_unary_op |
9045 | will mark the function as addressed, but here we must do it |
9046 | explicitly. */ |
9047 | cxx_mark_addressable (fn); |
9048 | |
9049 | return fn; |
9050 | } |
9051 | } |
9052 | |
9053 | /* This function will instantiate the type of the expression given in |
9054 | RHS to match the type of LHSTYPE. If errors exist, then return |
9055 | error_mark_node. COMPLAIN is a bit mask. If TF_ERROR is set, then |
9056 | we complain on errors. If we are not complaining, never modify rhs, |
9057 | as overload resolution wants to try many possible instantiations, in |
9058 | the hope that at least one will work. |
9059 | |
9060 | For non-recursive calls, LHSTYPE should be a function, pointer to |
9061 | function, or a pointer to member function. */ |
9062 | |
9063 | tree |
9064 | instantiate_type (tree lhstype, tree rhs, tsubst_flags_t complain) |
9065 | { |
9066 | tsubst_flags_t complain_in = complain; |
9067 | tree access_path = NULL_TREE; |
9068 | |
9069 | complain &= ~tf_ptrmem_ok; |
9070 | |
9071 | STRIP_ANY_LOCATION_WRAPPER (rhs); |
9072 | |
9073 | if (lhstype == unknown_type_node) |
9074 | { |
9075 | if (complain & tf_error) |
9076 | error ("not enough type information" ); |
9077 | return error_mark_node; |
9078 | } |
9079 | |
9080 | if (TREE_TYPE (rhs) != NULL_TREE && ! (type_unknown_p (expr: rhs))) |
9081 | { |
9082 | tree fntype = non_reference (lhstype); |
9083 | if (same_type_p (fntype, TREE_TYPE (rhs))) |
9084 | return rhs; |
9085 | if (fnptr_conv_p (fntype, TREE_TYPE (rhs))) |
9086 | return rhs; |
9087 | if (flag_ms_extensions |
9088 | && TYPE_PTRMEMFUNC_P (fntype) |
9089 | && !TYPE_PTRMEMFUNC_P (TREE_TYPE (rhs))) |
9090 | /* Microsoft allows `A::f' to be resolved to a |
9091 | pointer-to-member. */ |
9092 | ; |
9093 | else |
9094 | { |
9095 | if (complain & tf_error) |
9096 | error ("cannot convert %qE from type %qT to type %qT" , |
9097 | rhs, TREE_TYPE (rhs), fntype); |
9098 | return error_mark_node; |
9099 | } |
9100 | } |
9101 | |
9102 | /* If we instantiate a template, and it is a A ?: C expression |
9103 | with omitted B, look through the SAVE_EXPR. */ |
9104 | if (TREE_CODE (rhs) == SAVE_EXPR) |
9105 | rhs = TREE_OPERAND (rhs, 0); |
9106 | |
9107 | if (BASELINK_P (rhs)) |
9108 | { |
9109 | access_path = BASELINK_ACCESS_BINFO (rhs); |
9110 | rhs = BASELINK_FUNCTIONS (rhs); |
9111 | } |
9112 | |
9113 | /* There are only a few kinds of expressions that may have a type |
9114 | dependent on overload resolution. */ |
9115 | gcc_assert (TREE_CODE (rhs) == ADDR_EXPR |
9116 | || TREE_CODE (rhs) == COMPONENT_REF |
9117 | || is_overloaded_fn (rhs) |
9118 | || (flag_ms_extensions && TREE_CODE (rhs) == FUNCTION_DECL)); |
9119 | |
9120 | /* This should really only be used when attempting to distinguish |
9121 | what sort of a pointer to function we have. For now, any |
9122 | arithmetic operation which is not supported on pointers |
9123 | is rejected as an error. */ |
9124 | |
9125 | switch (TREE_CODE (rhs)) |
9126 | { |
9127 | case COMPONENT_REF: |
9128 | { |
9129 | tree member = TREE_OPERAND (rhs, 1); |
9130 | |
9131 | member = instantiate_type (lhstype, rhs: member, complain); |
9132 | if (member != error_mark_node |
9133 | && TREE_SIDE_EFFECTS (TREE_OPERAND (rhs, 0))) |
9134 | /* Do not lose object's side effects. */ |
9135 | return build2 (COMPOUND_EXPR, TREE_TYPE (member), |
9136 | TREE_OPERAND (rhs, 0), member); |
9137 | return member; |
9138 | } |
9139 | |
9140 | case OFFSET_REF: |
9141 | rhs = TREE_OPERAND (rhs, 1); |
9142 | if (BASELINK_P (rhs)) |
9143 | return instantiate_type (lhstype, rhs, complain: complain_in); |
9144 | |
9145 | /* This can happen if we are forming a pointer-to-member for a |
9146 | member template. */ |
9147 | gcc_assert (TREE_CODE (rhs) == TEMPLATE_ID_EXPR); |
9148 | |
9149 | /* Fall through. */ |
9150 | |
9151 | case TEMPLATE_ID_EXPR: |
9152 | { |
9153 | tree fns = TREE_OPERAND (rhs, 0); |
9154 | tree args = TREE_OPERAND (rhs, 1); |
9155 | |
9156 | return |
9157 | resolve_address_of_overloaded_function (target_type: lhstype, overload: fns, complain: complain_in, |
9158 | /*template_only=*/true, |
9159 | explicit_targs: args, access_path); |
9160 | } |
9161 | |
9162 | case OVERLOAD: |
9163 | case FUNCTION_DECL: |
9164 | return |
9165 | resolve_address_of_overloaded_function (target_type: lhstype, overload: rhs, complain: complain_in, |
9166 | /*template_only=*/false, |
9167 | /*explicit_targs=*/NULL_TREE, |
9168 | access_path); |
9169 | |
9170 | case ADDR_EXPR: |
9171 | { |
9172 | if (PTRMEM_OK_P (rhs)) |
9173 | complain |= tf_ptrmem_ok; |
9174 | |
9175 | return instantiate_type (lhstype, TREE_OPERAND (rhs, 0), complain); |
9176 | } |
9177 | |
9178 | case ERROR_MARK: |
9179 | return error_mark_node; |
9180 | |
9181 | default: |
9182 | gcc_unreachable (); |
9183 | } |
9184 | return error_mark_node; |
9185 | } |
9186 | |
9187 | /* Return the name of the virtual function pointer field |
9188 | (as an IDENTIFIER_NODE) for the given TYPE. Note that |
9189 | this may have to look back through base types to find the |
9190 | ultimate field name. (For single inheritance, these could |
9191 | all be the same name. Who knows for multiple inheritance). */ |
9192 | |
9193 | static tree |
9194 | get_vfield_name (tree type) |
9195 | { |
9196 | tree binfo, base_binfo; |
9197 | |
9198 | for (binfo = TYPE_BINFO (type); |
9199 | BINFO_N_BASE_BINFOS (binfo); |
9200 | binfo = base_binfo) |
9201 | { |
9202 | base_binfo = BINFO_BASE_BINFO (binfo, 0); |
9203 | |
9204 | if (BINFO_VIRTUAL_P (base_binfo) |
9205 | || !TYPE_CONTAINS_VPTR_P (BINFO_TYPE (base_binfo))) |
9206 | break; |
9207 | } |
9208 | |
9209 | type = BINFO_TYPE (binfo); |
9210 | tree ctor_name = constructor_name (type); |
9211 | char *buf = (char *) alloca (sizeof (VFIELD_NAME_FORMAT) |
9212 | + IDENTIFIER_LENGTH (ctor_name) + 2); |
9213 | sprintf (s: buf, VFIELD_NAME_FORMAT, IDENTIFIER_POINTER (ctor_name)); |
9214 | return get_identifier (buf); |
9215 | } |
9216 | |
9217 | /* Build a dummy reference to ourselves so Derived::Base (and A::A) works, |
9218 | according to [class]: |
9219 | The class-name is also inserted |
9220 | into the scope of the class itself. For purposes of access checking, |
9221 | the inserted class name is treated as if it were a public member name. */ |
9222 | |
9223 | void |
9224 | build_self_reference (void) |
9225 | { |
9226 | tree name = DECL_NAME (TYPE_NAME (current_class_type)); |
9227 | tree decl = build_lang_decl (TYPE_DECL, name, current_class_type); |
9228 | |
9229 | DECL_NONLOCAL (decl) = 1; |
9230 | DECL_CONTEXT (decl) = current_class_type; |
9231 | DECL_ARTIFICIAL (decl) = 1; |
9232 | SET_DECL_SELF_REFERENCE_P (decl); |
9233 | set_underlying_type (decl); |
9234 | set_instantiating_module (decl); |
9235 | |
9236 | if (processing_template_decl) |
9237 | decl = push_template_decl (decl); |
9238 | |
9239 | tree saved_cas = current_access_specifier; |
9240 | current_access_specifier = access_public_node; |
9241 | finish_member_declaration (decl); |
9242 | current_access_specifier = saved_cas; |
9243 | } |
9244 | |
9245 | /* Returns 1 if TYPE contains only padding bytes. */ |
9246 | |
9247 | int |
9248 | is_empty_class (tree type) |
9249 | { |
9250 | if (type == error_mark_node) |
9251 | return 0; |
9252 | |
9253 | if (! CLASS_TYPE_P (type)) |
9254 | return 0; |
9255 | |
9256 | return CLASSTYPE_EMPTY_P (type); |
9257 | } |
9258 | |
9259 | /* Returns true if TYPE contains no actual data, just various |
9260 | possible combinations of empty classes. If IGNORE_VPTR is true, |
9261 | a vptr doesn't prevent the class from being considered empty. Typically |
9262 | we want to ignore the vptr on assignment, and not on initialization. */ |
9263 | |
9264 | bool |
9265 | is_really_empty_class (tree type, bool ignore_vptr) |
9266 | { |
9267 | if (CLASS_TYPE_P (type)) |
9268 | { |
9269 | tree field; |
9270 | tree binfo; |
9271 | tree base_binfo; |
9272 | int i; |
9273 | |
9274 | /* CLASSTYPE_EMPTY_P isn't set properly until the class is actually laid |
9275 | out, but we'd like to be able to check this before then. */ |
9276 | if (COMPLETE_TYPE_P (type) && is_empty_class (type)) |
9277 | return true; |
9278 | |
9279 | if (!ignore_vptr && TYPE_CONTAINS_VPTR_P (type)) |
9280 | return false; |
9281 | |
9282 | for (binfo = TYPE_BINFO (type), i = 0; |
9283 | BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i) |
9284 | if (!is_really_empty_class (BINFO_TYPE (base_binfo), ignore_vptr)) |
9285 | return false; |
9286 | for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field)) |
9287 | if (TREE_CODE (field) == FIELD_DECL |
9288 | && !DECL_ARTIFICIAL (field) |
9289 | /* An unnamed bit-field is not a data member. */ |
9290 | && !DECL_UNNAMED_BIT_FIELD (field) |
9291 | && !is_really_empty_class (TREE_TYPE (field), ignore_vptr)) |
9292 | return false; |
9293 | return true; |
9294 | } |
9295 | else if (TREE_CODE (type) == ARRAY_TYPE) |
9296 | return (integer_zerop (array_type_nelts_top (type)) |
9297 | || is_really_empty_class (TREE_TYPE (type), ignore_vptr)); |
9298 | return false; |
9299 | } |
9300 | |
9301 | /* Note that NAME was looked up while the current class was being |
9302 | defined and that the result of that lookup was DECL. */ |
9303 | |
9304 | void |
9305 | maybe_note_name_used_in_class (tree name, tree decl) |
9306 | { |
9307 | /* If we're not defining a class, there's nothing to do. */ |
9308 | if (!(innermost_scope_kind() == sk_class |
9309 | && TYPE_BEING_DEFINED (current_class_type) |
9310 | && !LAMBDA_TYPE_P (current_class_type))) |
9311 | return; |
9312 | |
9313 | const cp_binding_level *blev = nullptr; |
9314 | if (const cxx_binding *binding = IDENTIFIER_BINDING (name)) |
9315 | blev = binding->scope; |
9316 | const cp_binding_level *lev = current_binding_level; |
9317 | |
9318 | /* Record the binding in the names_used tables for classes inside blev. */ |
9319 | for (int i = current_class_depth; i > 0; --i) |
9320 | { |
9321 | tree type = (i == current_class_depth |
9322 | ? current_class_type |
9323 | : current_class_stack[i].type); |
9324 | |
9325 | for (; lev; lev = lev->level_chain) |
9326 | { |
9327 | if (lev == blev) |
9328 | /* We found the declaration. */ |
9329 | return; |
9330 | if (lev->kind == sk_class && lev->this_entity == type) |
9331 | /* This class is inside the declaration scope. */ |
9332 | break; |
9333 | } |
9334 | |
9335 | auto &names_used = current_class_stack[i-1].names_used; |
9336 | if (!names_used) |
9337 | names_used = splay_tree_new (splay_tree_compare_pointers, 0, 0); |
9338 | |
9339 | tree use = build1_loc (loc: input_location, code: VIEW_CONVERT_EXPR, |
9340 | TREE_TYPE (decl), arg1: decl); |
9341 | EXPR_LOCATION_WRAPPER_P (use) = 1; |
9342 | splay_tree_insert (names_used, |
9343 | (splay_tree_key) name, |
9344 | (splay_tree_value) use); |
9345 | } |
9346 | } |
9347 | |
9348 | /* Note that NAME was declared (as DECL) in the current class. Check |
9349 | to see that the declaration is valid under [class.member.lookup]: |
9350 | |
9351 | If [the result of a search in T for N at point P] differs from the result of |
9352 | a search in T for N from immediately after the class-specifier of T, the |
9353 | program is ill-formed, no diagnostic required. */ |
9354 | |
9355 | void |
9356 | note_name_declared_in_class (tree name, tree decl) |
9357 | { |
9358 | splay_tree names_used; |
9359 | splay_tree_node n; |
9360 | |
9361 | /* Look to see if we ever used this name. */ |
9362 | names_used |
9363 | = current_class_stack[current_class_depth - 1].names_used; |
9364 | if (!names_used) |
9365 | return; |
9366 | /* The C language allows members to be declared with a type of the same |
9367 | name, and the C++ standard says this diagnostic is not required. So |
9368 | allow it in extern "C" blocks unless pedantic is specified. |
9369 | Allow it in all cases if -ms-extensions is specified. */ |
9370 | if ((!pedantic && current_lang_name == lang_name_c) |
9371 | || flag_ms_extensions) |
9372 | return; |
9373 | n = splay_tree_lookup (names_used, (splay_tree_key) name); |
9374 | if (n) |
9375 | { |
9376 | tree use = (tree) n->value; |
9377 | location_t loc = EXPR_LOCATION (use); |
9378 | tree olddecl = OVL_FIRST (TREE_OPERAND (use, 0)); |
9379 | /* [basic.scope.class] |
9380 | |
9381 | A name N used in a class S shall refer to the same declaration |
9382 | in its context and when re-evaluated in the completed scope of |
9383 | S. */ |
9384 | auto ov = make_temp_override (var&: global_dc->m_pedantic_errors); |
9385 | if (TREE_CODE (decl) == TYPE_DECL |
9386 | && TREE_CODE (olddecl) == TYPE_DECL |
9387 | && same_type_p (TREE_TYPE (decl), TREE_TYPE (olddecl))) |
9388 | /* Different declaration, but same meaning; just warn. */; |
9389 | else if (flag_permissive) |
9390 | /* Let -fpermissive make it a warning like past versions. */; |
9391 | else |
9392 | /* Make it an error. */ |
9393 | global_dc->m_pedantic_errors = 1; |
9394 | if (pedwarn (location_of (decl), OPT_Wchanges_meaning, |
9395 | "declaration of %q#D changes meaning of %qD" , |
9396 | decl, OVL_NAME (decl))) |
9397 | { |
9398 | inform (loc, "used here to mean %q#D" , olddecl); |
9399 | inform (location_of (olddecl), "declared here" ); |
9400 | } |
9401 | } |
9402 | } |
9403 | |
9404 | /* Returns the VAR_DECL for the complete vtable associated with BINFO. |
9405 | Secondary vtables are merged with primary vtables; this function |
9406 | will return the VAR_DECL for the primary vtable. */ |
9407 | |
9408 | tree |
9409 | get_vtbl_decl_for_binfo (tree binfo) |
9410 | { |
9411 | tree decl; |
9412 | |
9413 | decl = BINFO_VTABLE (binfo); |
9414 | if (decl && TREE_CODE (decl) == POINTER_PLUS_EXPR) |
9415 | { |
9416 | gcc_assert (TREE_CODE (TREE_OPERAND (decl, 0)) == ADDR_EXPR); |
9417 | decl = TREE_OPERAND (TREE_OPERAND (decl, 0), 0); |
9418 | } |
9419 | if (decl) |
9420 | gcc_assert (VAR_P (decl)); |
9421 | return decl; |
9422 | } |
9423 | |
9424 | |
9425 | /* Returns the binfo for the primary base of BINFO. If the resulting |
9426 | BINFO is a virtual base, and it is inherited elsewhere in the |
9427 | hierarchy, then the returned binfo might not be the primary base of |
9428 | BINFO in the complete object. Check BINFO_PRIMARY_P or |
9429 | BINFO_LOST_PRIMARY_P to be sure. */ |
9430 | |
9431 | static tree |
9432 | get_primary_binfo (tree binfo) |
9433 | { |
9434 | tree primary_base; |
9435 | |
9436 | primary_base = CLASSTYPE_PRIMARY_BINFO (BINFO_TYPE (binfo)); |
9437 | if (!primary_base) |
9438 | return NULL_TREE; |
9439 | |
9440 | return copied_binfo (primary_base, binfo); |
9441 | } |
9442 | |
9443 | /* As above, but iterate until we reach the binfo that actually provides the |
9444 | vptr for BINFO. */ |
9445 | |
9446 | static tree |
9447 | most_primary_binfo (tree binfo) |
9448 | { |
9449 | tree b = binfo; |
9450 | while (CLASSTYPE_HAS_PRIMARY_BASE_P (BINFO_TYPE (b)) |
9451 | && !BINFO_LOST_PRIMARY_P (b)) |
9452 | { |
9453 | tree primary_base = get_primary_binfo (binfo: b); |
9454 | gcc_assert (BINFO_PRIMARY_P (primary_base) |
9455 | && BINFO_INHERITANCE_CHAIN (primary_base) == b); |
9456 | b = primary_base; |
9457 | } |
9458 | return b; |
9459 | } |
9460 | |
9461 | /* Returns true if BINFO gets its vptr from a virtual base of the most derived |
9462 | type. Note that the virtual inheritance might be above or below BINFO in |
9463 | the hierarchy. */ |
9464 | |
9465 | bool |
9466 | vptr_via_virtual_p (tree binfo) |
9467 | { |
9468 | if (TYPE_P (binfo)) |
9469 | binfo = TYPE_BINFO (binfo); |
9470 | tree primary = most_primary_binfo (binfo); |
9471 | /* Don't limit binfo_via_virtual, we want to return true when BINFO itself is |
9472 | a morally virtual base. */ |
9473 | tree virt = binfo_via_virtual (primary, NULL_TREE); |
9474 | return virt != NULL_TREE; |
9475 | } |
9476 | |
9477 | /* If INDENTED_P is zero, indent to INDENT. Return nonzero. */ |
9478 | |
9479 | static int |
9480 | maybe_indent_hierarchy (FILE * stream, int indent, int indented_p) |
9481 | { |
9482 | if (!indented_p) |
9483 | fprintf (stream: stream, format: "%*s" , indent, "" ); |
9484 | return 1; |
9485 | } |
9486 | |
9487 | /* Dump the offsets of all the bases rooted at BINFO to STREAM. |
9488 | INDENT should be zero when called from the top level; it is |
9489 | incremented recursively. IGO indicates the next expected BINFO in |
9490 | inheritance graph ordering. */ |
9491 | |
9492 | static tree |
9493 | dump_class_hierarchy_r (FILE *stream, |
9494 | dump_flags_t flags, |
9495 | tree binfo, |
9496 | tree igo, |
9497 | int indent) |
9498 | { |
9499 | int indented = 0; |
9500 | tree base_binfo; |
9501 | int i; |
9502 | |
9503 | fprintf (stream: stream, format: "%s (0x" HOST_WIDE_INT_PRINT_HEX ") " , |
9504 | type_as_string (BINFO_TYPE (binfo), TFF_PLAIN_IDENTIFIER), |
9505 | (HOST_WIDE_INT) (uintptr_t) binfo); |
9506 | if (binfo != igo) |
9507 | { |
9508 | fprintf (stream: stream, format: "alternative-path\n" ); |
9509 | return igo; |
9510 | } |
9511 | igo = TREE_CHAIN (binfo); |
9512 | |
9513 | fprintf (stream: stream, HOST_WIDE_INT_PRINT_DEC, |
9514 | tree_to_shwi (BINFO_OFFSET (binfo))); |
9515 | if (is_empty_class (BINFO_TYPE (binfo))) |
9516 | fprintf (stream: stream, format: " empty" ); |
9517 | else if (CLASSTYPE_NEARLY_EMPTY_P (BINFO_TYPE (binfo))) |
9518 | fprintf (stream: stream, format: " nearly-empty" ); |
9519 | if (BINFO_VIRTUAL_P (binfo)) |
9520 | fprintf (stream: stream, format: " virtual" ); |
9521 | fprintf (stream: stream, format: "\n" ); |
9522 | |
9523 | if (BINFO_PRIMARY_P (binfo)) |
9524 | { |
9525 | indented = maybe_indent_hierarchy (stream, indent: indent + 3, indented_p: indented); |
9526 | fprintf (stream: stream, format: " primary-for %s (0x" HOST_WIDE_INT_PRINT_HEX ")" , |
9527 | type_as_string (BINFO_TYPE (BINFO_INHERITANCE_CHAIN (binfo)), |
9528 | TFF_PLAIN_IDENTIFIER), |
9529 | (HOST_WIDE_INT) (uintptr_t) BINFO_INHERITANCE_CHAIN (binfo)); |
9530 | } |
9531 | if (BINFO_LOST_PRIMARY_P (binfo)) |
9532 | { |
9533 | indented = maybe_indent_hierarchy (stream, indent: indent + 3, indented_p: indented); |
9534 | fprintf (stream: stream, format: " lost-primary" ); |
9535 | } |
9536 | if (indented) |
9537 | fprintf (stream: stream, format: "\n" ); |
9538 | |
9539 | if (!(flags & TDF_SLIM)) |
9540 | { |
9541 | int indented = 0; |
9542 | |
9543 | if (BINFO_SUBVTT_INDEX (binfo)) |
9544 | { |
9545 | indented = maybe_indent_hierarchy (stream, indent: indent + 3, indented_p: indented); |
9546 | fprintf (stream: stream, format: " subvttidx=%s" , |
9547 | expr_as_string (BINFO_SUBVTT_INDEX (binfo), |
9548 | TFF_PLAIN_IDENTIFIER)); |
9549 | } |
9550 | if (BINFO_VPTR_INDEX (binfo)) |
9551 | { |
9552 | indented = maybe_indent_hierarchy (stream, indent: indent + 3, indented_p: indented); |
9553 | fprintf (stream: stream, format: " vptridx=%s" , |
9554 | expr_as_string (BINFO_VPTR_INDEX (binfo), |
9555 | TFF_PLAIN_IDENTIFIER)); |
9556 | } |
9557 | if (BINFO_VPTR_FIELD (binfo)) |
9558 | { |
9559 | indented = maybe_indent_hierarchy (stream, indent: indent + 3, indented_p: indented); |
9560 | fprintf (stream: stream, format: " vbaseoffset=%s" , |
9561 | expr_as_string (BINFO_VPTR_FIELD (binfo), |
9562 | TFF_PLAIN_IDENTIFIER)); |
9563 | } |
9564 | if (BINFO_VTABLE (binfo)) |
9565 | { |
9566 | indented = maybe_indent_hierarchy (stream, indent: indent + 3, indented_p: indented); |
9567 | fprintf (stream: stream, format: " vptr=%s" , |
9568 | expr_as_string (BINFO_VTABLE (binfo), |
9569 | TFF_PLAIN_IDENTIFIER)); |
9570 | } |
9571 | |
9572 | if (indented) |
9573 | fprintf (stream: stream, format: "\n" ); |
9574 | } |
9575 | |
9576 | for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++) |
9577 | igo = dump_class_hierarchy_r (stream, flags, binfo: base_binfo, igo, indent: indent + 2); |
9578 | |
9579 | return igo; |
9580 | } |
9581 | |
9582 | /* Dump the BINFO hierarchy for T. */ |
9583 | |
9584 | static void |
9585 | dump_class_hierarchy_1 (FILE *stream, dump_flags_t flags, tree t) |
9586 | { |
9587 | fprintf (stream: stream, format: "Class %s\n" , type_as_string (t, TFF_PLAIN_IDENTIFIER)); |
9588 | fprintf (stream: stream, format: " size=" HOST_WIDE_INT_PRINT_UNSIGNED " align=%u\n" , |
9589 | tree_to_shwi (TYPE_SIZE (t)) / BITS_PER_UNIT, |
9590 | TYPE_ALIGN (t) / BITS_PER_UNIT); |
9591 | if (tree as_base = CLASSTYPE_AS_BASE (t)) |
9592 | fprintf (stream: stream, format: " base size=" HOST_WIDE_INT_PRINT_UNSIGNED |
9593 | " base align=%u\n" , |
9594 | tree_to_shwi (TYPE_SIZE (as_base)) / BITS_PER_UNIT, |
9595 | TYPE_ALIGN (as_base) / BITS_PER_UNIT); |
9596 | dump_class_hierarchy_r (stream, flags, TYPE_BINFO (t), TYPE_BINFO (t), indent: 0); |
9597 | fprintf (stream: stream, format: "\n" ); |
9598 | } |
9599 | |
9600 | /* Debug interface to hierarchy dumping. */ |
9601 | |
9602 | void |
9603 | debug_class (tree t) |
9604 | { |
9605 | dump_class_hierarchy_1 (stderr, flags: TDF_SLIM, t); |
9606 | } |
9607 | |
9608 | static void |
9609 | dump_class_hierarchy (tree t) |
9610 | { |
9611 | dump_flags_t flags; |
9612 | if (FILE *stream = dump_begin (class_dump_id, &flags)) |
9613 | { |
9614 | dump_class_hierarchy_1 (stream, flags, t); |
9615 | dump_end (class_dump_id, stream); |
9616 | } |
9617 | } |
9618 | |
9619 | static void |
9620 | dump_array (FILE * stream, tree decl) |
9621 | { |
9622 | tree value; |
9623 | unsigned HOST_WIDE_INT ix; |
9624 | HOST_WIDE_INT elt; |
9625 | tree size = TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (decl))); |
9626 | |
9627 | elt = (tree_to_shwi (TYPE_SIZE (TREE_TYPE (TREE_TYPE (decl)))) |
9628 | / BITS_PER_UNIT); |
9629 | fprintf (stream: stream, format: "%s:" , decl_as_string (decl, TFF_PLAIN_IDENTIFIER)); |
9630 | fprintf (stream: stream, format: " %s entries" , |
9631 | expr_as_string (size_binop (PLUS_EXPR, size, size_one_node), |
9632 | TFF_PLAIN_IDENTIFIER)); |
9633 | fprintf (stream: stream, format: "\n" ); |
9634 | |
9635 | FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (DECL_INITIAL (decl)), |
9636 | ix, value) |
9637 | fprintf (stream: stream, format: "%-4ld %s\n" , (long)(ix * elt), |
9638 | expr_as_string (value, TFF_PLAIN_IDENTIFIER)); |
9639 | } |
9640 | |
9641 | static void |
9642 | dump_vtable (tree t, tree binfo, tree vtable) |
9643 | { |
9644 | dump_flags_t flags; |
9645 | FILE *stream = dump_begin (class_dump_id, &flags); |
9646 | |
9647 | if (!stream) |
9648 | return; |
9649 | |
9650 | if (!(flags & TDF_SLIM)) |
9651 | { |
9652 | int ctor_vtbl_p = TYPE_BINFO (t) != binfo; |
9653 | |
9654 | fprintf (stream: stream, format: "%s for %s" , |
9655 | ctor_vtbl_p ? "Construction vtable" : "Vtable" , |
9656 | type_as_string (BINFO_TYPE (binfo), TFF_PLAIN_IDENTIFIER)); |
9657 | if (ctor_vtbl_p) |
9658 | { |
9659 | if (!BINFO_VIRTUAL_P (binfo)) |
9660 | fprintf (stream: stream, format: " (0x" HOST_WIDE_INT_PRINT_HEX " instance)" , |
9661 | (HOST_WIDE_INT) (uintptr_t) binfo); |
9662 | fprintf (stream: stream, format: " in %s" , type_as_string (t, TFF_PLAIN_IDENTIFIER)); |
9663 | } |
9664 | fprintf (stream: stream, format: "\n" ); |
9665 | dump_array (stream, decl: vtable); |
9666 | fprintf (stream: stream, format: "\n" ); |
9667 | } |
9668 | |
9669 | dump_end (class_dump_id, stream); |
9670 | } |
9671 | |
9672 | static void |
9673 | dump_vtt (tree t, tree vtt) |
9674 | { |
9675 | dump_flags_t flags; |
9676 | FILE *stream = dump_begin (class_dump_id, &flags); |
9677 | |
9678 | if (!stream) |
9679 | return; |
9680 | |
9681 | if (!(flags & TDF_SLIM)) |
9682 | { |
9683 | fprintf (stream: stream, format: "VTT for %s\n" , |
9684 | type_as_string (t, TFF_PLAIN_IDENTIFIER)); |
9685 | dump_array (stream, decl: vtt); |
9686 | fprintf (stream: stream, format: "\n" ); |
9687 | } |
9688 | |
9689 | dump_end (class_dump_id, stream); |
9690 | } |
9691 | |
9692 | /* Dump a function or thunk and its thunkees. */ |
9693 | |
9694 | static void |
9695 | dump_thunk (FILE *stream, int indent, tree thunk) |
9696 | { |
9697 | static const char spaces[] = " " ; |
9698 | tree name = DECL_NAME (thunk); |
9699 | tree thunks; |
9700 | |
9701 | fprintf (stream: stream, format: "%.*s%p %s %s" , indent, spaces, |
9702 | (void *)thunk, |
9703 | !DECL_THUNK_P (thunk) ? "function" |
9704 | : DECL_THIS_THUNK_P (thunk) ? "this-thunk" : "covariant-thunk" , |
9705 | name ? IDENTIFIER_POINTER (name) : "<unset>" ); |
9706 | if (DECL_THUNK_P (thunk)) |
9707 | { |
9708 | HOST_WIDE_INT fixed_adjust = THUNK_FIXED_OFFSET (thunk); |
9709 | tree virtual_adjust = THUNK_VIRTUAL_OFFSET (thunk); |
9710 | |
9711 | fprintf (stream: stream, format: " fixed=" HOST_WIDE_INT_PRINT_DEC, fixed_adjust); |
9712 | if (!virtual_adjust) |
9713 | /*NOP*/; |
9714 | else if (DECL_THIS_THUNK_P (thunk)) |
9715 | fprintf (stream: stream, format: " vcall=" HOST_WIDE_INT_PRINT_DEC, |
9716 | tree_to_shwi (virtual_adjust)); |
9717 | else |
9718 | fprintf (stream: stream, format: " vbase=" HOST_WIDE_INT_PRINT_DEC "(%s)" , |
9719 | tree_to_shwi (BINFO_VPTR_FIELD (virtual_adjust)), |
9720 | type_as_string (BINFO_TYPE (virtual_adjust), TFF_SCOPE)); |
9721 | if (THUNK_ALIAS (thunk)) |
9722 | fprintf (stream: stream, format: " alias to %p" , (void *)THUNK_ALIAS (thunk)); |
9723 | } |
9724 | fprintf (stream: stream, format: "\n" ); |
9725 | for (thunks = DECL_THUNKS (thunk); thunks; thunks = TREE_CHAIN (thunks)) |
9726 | dump_thunk (stream, indent: indent + 2, thunk: thunks); |
9727 | } |
9728 | |
9729 | /* Dump the thunks for FN. */ |
9730 | |
9731 | void |
9732 | debug_thunks (tree fn) |
9733 | { |
9734 | dump_thunk (stderr, indent: 0, thunk: fn); |
9735 | } |
9736 | |
9737 | /* Virtual function table initialization. */ |
9738 | |
9739 | /* Create all the necessary vtables for T and its base classes. */ |
9740 | |
9741 | static void |
9742 | finish_vtbls (tree t) |
9743 | { |
9744 | tree vbase; |
9745 | vec<constructor_elt, va_gc> *v = NULL; |
9746 | tree vtable = BINFO_VTABLE (TYPE_BINFO (t)); |
9747 | |
9748 | /* We lay out the primary and secondary vtables in one contiguous |
9749 | vtable. The primary vtable is first, followed by the non-virtual |
9750 | secondary vtables in inheritance graph order. */ |
9751 | accumulate_vtbl_inits (TYPE_BINFO (t), TYPE_BINFO (t), TYPE_BINFO (t), |
9752 | vtable, t, &v); |
9753 | |
9754 | /* Then come the virtual bases, also in inheritance graph order. */ |
9755 | for (vbase = TYPE_BINFO (t); vbase; vbase = TREE_CHAIN (vbase)) |
9756 | { |
9757 | if (!BINFO_VIRTUAL_P (vbase)) |
9758 | continue; |
9759 | accumulate_vtbl_inits (vbase, vbase, TYPE_BINFO (t), vtable, t, &v); |
9760 | } |
9761 | |
9762 | if (BINFO_VTABLE (TYPE_BINFO (t))) |
9763 | initialize_vtable (TYPE_BINFO (t), v); |
9764 | } |
9765 | |
9766 | /* Initialize the vtable for BINFO with the INITS. */ |
9767 | |
9768 | static void |
9769 | initialize_vtable (tree binfo, vec<constructor_elt, va_gc> *inits) |
9770 | { |
9771 | tree decl; |
9772 | |
9773 | layout_vtable_decl (binfo, n: vec_safe_length (v: inits)); |
9774 | decl = get_vtbl_decl_for_binfo (binfo); |
9775 | initialize_artificial_var (decl, inits); |
9776 | dump_vtable (BINFO_TYPE (binfo), binfo, vtable: decl); |
9777 | } |
9778 | |
9779 | /* Build the VTT (virtual table table) for T. |
9780 | A class requires a VTT if it has virtual bases. |
9781 | |
9782 | This holds |
9783 | 1 - primary virtual pointer for complete object T |
9784 | 2 - secondary VTTs for each direct non-virtual base of T which requires a |
9785 | VTT |
9786 | 3 - secondary virtual pointers for each direct or indirect base of T which |
9787 | has virtual bases or is reachable via a virtual path from T. |
9788 | 4 - secondary VTTs for each direct or indirect virtual base of T. |
9789 | |
9790 | Secondary VTTs look like complete object VTTs without part 4. */ |
9791 | |
9792 | static void |
9793 | build_vtt (tree t) |
9794 | { |
9795 | tree type; |
9796 | tree vtt; |
9797 | tree index; |
9798 | vec<constructor_elt, va_gc> *inits; |
9799 | |
9800 | /* Build up the initializers for the VTT. */ |
9801 | inits = NULL; |
9802 | index = size_zero_node; |
9803 | build_vtt_inits (TYPE_BINFO (t), t, &inits, &index); |
9804 | |
9805 | /* If we didn't need a VTT, we're done. */ |
9806 | if (!inits) |
9807 | return; |
9808 | |
9809 | /* Figure out the type of the VTT. */ |
9810 | type = build_array_of_n_type (const_ptr_type_node, |
9811 | inits->length ()); |
9812 | |
9813 | /* Now, build the VTT object itself. */ |
9814 | vtt = build_vtable (class_type: t, name: mangle_vtt_for_type (t), vtable_type: type); |
9815 | initialize_artificial_var (vtt, inits); |
9816 | /* Add the VTT to the vtables list. */ |
9817 | DECL_CHAIN (vtt) = DECL_CHAIN (CLASSTYPE_VTABLES (t)); |
9818 | DECL_CHAIN (CLASSTYPE_VTABLES (t)) = vtt; |
9819 | |
9820 | dump_vtt (t, vtt); |
9821 | } |
9822 | |
9823 | /* When building a secondary VTT, BINFO_VTABLE is set to a TREE_LIST with |
9824 | PURPOSE the RTTI_BINFO, VALUE the real vtable pointer for this binfo, |
9825 | and CHAIN the vtable pointer for this binfo after construction is |
9826 | complete. VALUE can also be another BINFO, in which case we recurse. */ |
9827 | |
9828 | static tree |
9829 | binfo_ctor_vtable (tree binfo) |
9830 | { |
9831 | tree vt; |
9832 | |
9833 | while (1) |
9834 | { |
9835 | vt = BINFO_VTABLE (binfo); |
9836 | if (TREE_CODE (vt) == TREE_LIST) |
9837 | vt = TREE_VALUE (vt); |
9838 | if (TREE_CODE (vt) == TREE_BINFO) |
9839 | binfo = vt; |
9840 | else |
9841 | break; |
9842 | } |
9843 | |
9844 | return vt; |
9845 | } |
9846 | |
9847 | /* Data for secondary VTT initialization. */ |
9848 | struct secondary_vptr_vtt_init_data |
9849 | { |
9850 | /* Is this the primary VTT? */ |
9851 | bool top_level_p; |
9852 | |
9853 | /* Current index into the VTT. */ |
9854 | tree index; |
9855 | |
9856 | /* Vector of initializers built up. */ |
9857 | vec<constructor_elt, va_gc> *inits; |
9858 | |
9859 | /* The type being constructed by this secondary VTT. */ |
9860 | tree type_being_constructed; |
9861 | }; |
9862 | |
9863 | /* Recursively build the VTT-initializer for BINFO (which is in the |
9864 | hierarchy dominated by T). INITS points to the end of the initializer |
9865 | list to date. INDEX is the VTT index where the next element will be |
9866 | replaced. Iff BINFO is the binfo for T, this is the top level VTT (i.e. |
9867 | not a subvtt for some base of T). When that is so, we emit the sub-VTTs |
9868 | for virtual bases of T. When it is not so, we build the constructor |
9869 | vtables for the BINFO-in-T variant. */ |
9870 | |
9871 | static void |
9872 | build_vtt_inits (tree binfo, tree t, vec<constructor_elt, va_gc> **inits, |
9873 | tree *index) |
9874 | { |
9875 | int i; |
9876 | tree b; |
9877 | tree init; |
9878 | secondary_vptr_vtt_init_data data; |
9879 | int top_level_p = SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), t); |
9880 | |
9881 | /* We only need VTTs for subobjects with virtual bases. */ |
9882 | if (!CLASSTYPE_VBASECLASSES (BINFO_TYPE (binfo))) |
9883 | return; |
9884 | |
9885 | /* We need to use a construction vtable if this is not the primary |
9886 | VTT. */ |
9887 | if (!top_level_p) |
9888 | { |
9889 | build_ctor_vtbl_group (binfo, t); |
9890 | |
9891 | /* Record the offset in the VTT where this sub-VTT can be found. */ |
9892 | BINFO_SUBVTT_INDEX (binfo) = *index; |
9893 | } |
9894 | |
9895 | /* Add the address of the primary vtable for the complete object. */ |
9896 | init = binfo_ctor_vtable (binfo); |
9897 | CONSTRUCTOR_APPEND_ELT (*inits, NULL_TREE, init); |
9898 | if (top_level_p) |
9899 | { |
9900 | gcc_assert (!BINFO_VPTR_INDEX (binfo)); |
9901 | BINFO_VPTR_INDEX (binfo) = *index; |
9902 | } |
9903 | *index = size_binop (PLUS_EXPR, *index, TYPE_SIZE_UNIT (ptr_type_node)); |
9904 | |
9905 | /* Recursively add the secondary VTTs for non-virtual bases. */ |
9906 | for (i = 0; BINFO_BASE_ITERATE (binfo, i, b); ++i) |
9907 | if (!BINFO_VIRTUAL_P (b)) |
9908 | build_vtt_inits (binfo: b, t, inits, index); |
9909 | |
9910 | /* Add secondary virtual pointers for all subobjects of BINFO with |
9911 | either virtual bases or reachable along a virtual path, except |
9912 | subobjects that are non-virtual primary bases. */ |
9913 | data.top_level_p = top_level_p; |
9914 | data.index = *index; |
9915 | data.inits = *inits; |
9916 | data.type_being_constructed = BINFO_TYPE (binfo); |
9917 | |
9918 | dfs_walk_once (binfo, dfs_build_secondary_vptr_vtt_inits, NULL, &data); |
9919 | |
9920 | *index = data.index; |
9921 | |
9922 | /* data.inits might have grown as we added secondary virtual pointers. |
9923 | Make sure our caller knows about the new vector. */ |
9924 | *inits = data.inits; |
9925 | |
9926 | if (top_level_p) |
9927 | /* Add the secondary VTTs for virtual bases in inheritance graph |
9928 | order. */ |
9929 | for (b = TYPE_BINFO (BINFO_TYPE (binfo)); b; b = TREE_CHAIN (b)) |
9930 | { |
9931 | if (!BINFO_VIRTUAL_P (b)) |
9932 | continue; |
9933 | |
9934 | build_vtt_inits (binfo: b, t, inits, index); |
9935 | } |
9936 | else |
9937 | /* Remove the ctor vtables we created. */ |
9938 | dfs_walk_all (binfo, dfs_fixup_binfo_vtbls, NULL, binfo); |
9939 | } |
9940 | |
9941 | /* Called from build_vtt_inits via dfs_walk. BINFO is the binfo for the base |
9942 | in most derived. DATA is a SECONDARY_VPTR_VTT_INIT_DATA structure. */ |
9943 | |
9944 | static tree |
9945 | dfs_build_secondary_vptr_vtt_inits (tree binfo, void *data_) |
9946 | { |
9947 | secondary_vptr_vtt_init_data *data = (secondary_vptr_vtt_init_data *)data_; |
9948 | |
9949 | /* We don't care about bases that don't have vtables. */ |
9950 | if (!TYPE_VFIELD (BINFO_TYPE (binfo))) |
9951 | return dfs_skip_bases; |
9952 | |
9953 | /* We're only interested in proper subobjects of the type being |
9954 | constructed. */ |
9955 | if (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), data->type_being_constructed)) |
9956 | return NULL_TREE; |
9957 | |
9958 | /* We're only interested in bases with virtual bases or reachable |
9959 | via a virtual path from the type being constructed. */ |
9960 | if (!(CLASSTYPE_VBASECLASSES (BINFO_TYPE (binfo)) |
9961 | || binfo_via_virtual (binfo, data->type_being_constructed))) |
9962 | return dfs_skip_bases; |
9963 | |
9964 | /* We're not interested in non-virtual primary bases. */ |
9965 | if (!BINFO_VIRTUAL_P (binfo) && BINFO_PRIMARY_P (binfo)) |
9966 | return NULL_TREE; |
9967 | |
9968 | /* Record the index where this secondary vptr can be found. */ |
9969 | if (data->top_level_p) |
9970 | { |
9971 | gcc_assert (!BINFO_VPTR_INDEX (binfo)); |
9972 | BINFO_VPTR_INDEX (binfo) = data->index; |
9973 | |
9974 | if (BINFO_VIRTUAL_P (binfo)) |
9975 | { |
9976 | /* It's a primary virtual base, and this is not a |
9977 | construction vtable. Find the base this is primary of in |
9978 | the inheritance graph, and use that base's vtable |
9979 | now. */ |
9980 | while (BINFO_PRIMARY_P (binfo)) |
9981 | binfo = BINFO_INHERITANCE_CHAIN (binfo); |
9982 | } |
9983 | } |
9984 | |
9985 | /* Add the initializer for the secondary vptr itself. */ |
9986 | CONSTRUCTOR_APPEND_ELT (data->inits, NULL_TREE, binfo_ctor_vtable (binfo)); |
9987 | |
9988 | /* Advance the vtt index. */ |
9989 | data->index = size_binop (PLUS_EXPR, data->index, |
9990 | TYPE_SIZE_UNIT (ptr_type_node)); |
9991 | |
9992 | return NULL_TREE; |
9993 | } |
9994 | |
9995 | /* Called from build_vtt_inits via dfs_walk. After building |
9996 | constructor vtables and generating the sub-vtt from them, we need |
9997 | to restore the BINFO_VTABLES that were scribbled on. DATA is the |
9998 | binfo of the base whose sub vtt was generated. */ |
9999 | |
10000 | static tree |
10001 | dfs_fixup_binfo_vtbls (tree binfo, void* data) |
10002 | { |
10003 | tree vtable = BINFO_VTABLE (binfo); |
10004 | |
10005 | if (!TYPE_CONTAINS_VPTR_P (BINFO_TYPE (binfo))) |
10006 | /* If this class has no vtable, none of its bases do. */ |
10007 | return dfs_skip_bases; |
10008 | |
10009 | if (!vtable) |
10010 | /* This might be a primary base, so have no vtable in this |
10011 | hierarchy. */ |
10012 | return NULL_TREE; |
10013 | |
10014 | /* If we scribbled the construction vtable vptr into BINFO, clear it |
10015 | out now. */ |
10016 | if (TREE_CODE (vtable) == TREE_LIST |
10017 | && (TREE_PURPOSE (vtable) == (tree) data)) |
10018 | BINFO_VTABLE (binfo) = TREE_CHAIN (vtable); |
10019 | |
10020 | return NULL_TREE; |
10021 | } |
10022 | |
10023 | /* Build the construction vtable group for BINFO which is in the |
10024 | hierarchy dominated by T. */ |
10025 | |
10026 | static void |
10027 | build_ctor_vtbl_group (tree binfo, tree t) |
10028 | { |
10029 | tree type; |
10030 | tree vtbl; |
10031 | tree id; |
10032 | tree vbase; |
10033 | vec<constructor_elt, va_gc> *v; |
10034 | |
10035 | /* See if we've already created this construction vtable group. */ |
10036 | id = mangle_ctor_vtbl_for_type (t, binfo); |
10037 | if (get_global_binding (id)) |
10038 | return; |
10039 | |
10040 | gcc_assert (!SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), t)); |
10041 | /* Build a version of VTBL (with the wrong type) for use in |
10042 | constructing the addresses of secondary vtables in the |
10043 | construction vtable group. */ |
10044 | vtbl = build_vtable (class_type: t, name: id, ptr_type_node); |
10045 | |
10046 | /* Don't export construction vtables from shared libraries. Even on |
10047 | targets that don't support hidden visibility, this tells |
10048 | can_refer_decl_in_current_unit_p not to assume that it's safe to |
10049 | access from a different compilation unit (bz 54314). */ |
10050 | DECL_VISIBILITY (vtbl) = VISIBILITY_HIDDEN; |
10051 | DECL_VISIBILITY_SPECIFIED (vtbl) = true; |
10052 | |
10053 | v = NULL; |
10054 | accumulate_vtbl_inits (binfo, TYPE_BINFO (TREE_TYPE (binfo)), |
10055 | binfo, vtbl, t, &v); |
10056 | |
10057 | /* Add the vtables for each of our virtual bases using the vbase in T |
10058 | binfo. */ |
10059 | for (vbase = TYPE_BINFO (BINFO_TYPE (binfo)); |
10060 | vbase; |
10061 | vbase = TREE_CHAIN (vbase)) |
10062 | { |
10063 | tree b; |
10064 | |
10065 | if (!BINFO_VIRTUAL_P (vbase)) |
10066 | continue; |
10067 | b = copied_binfo (vbase, binfo); |
10068 | |
10069 | accumulate_vtbl_inits (b, vbase, binfo, vtbl, t, &v); |
10070 | } |
10071 | |
10072 | /* Figure out the type of the construction vtable. */ |
10073 | type = build_array_of_n_type (vtable_entry_type, v->length ()); |
10074 | layout_type (type); |
10075 | TREE_TYPE (vtbl) = type; |
10076 | DECL_SIZE (vtbl) = DECL_SIZE_UNIT (vtbl) = NULL_TREE; |
10077 | layout_decl (vtbl, 0); |
10078 | |
10079 | /* Initialize the construction vtable. */ |
10080 | CLASSTYPE_VTABLES (t) = chainon (CLASSTYPE_VTABLES (t), vtbl); |
10081 | initialize_artificial_var (vtbl, v); |
10082 | dump_vtable (t, binfo, vtable: vtbl); |
10083 | } |
10084 | |
10085 | /* Add the vtbl initializers for BINFO (and its bases other than |
10086 | non-virtual primaries) to the list of INITS. BINFO is in the |
10087 | hierarchy dominated by T. RTTI_BINFO is the binfo within T of |
10088 | the constructor the vtbl inits should be accumulated for. (If this |
10089 | is the complete object vtbl then RTTI_BINFO will be TYPE_BINFO (T).) |
10090 | ORIG_BINFO is the binfo for this object within BINFO_TYPE (RTTI_BINFO). |
10091 | BINFO is the active base equivalent of ORIG_BINFO in the inheritance |
10092 | graph of T. Both BINFO and ORIG_BINFO will have the same BINFO_TYPE, |
10093 | but are not necessarily the same in terms of layout. */ |
10094 | |
10095 | static void |
10096 | accumulate_vtbl_inits (tree binfo, |
10097 | tree orig_binfo, |
10098 | tree rtti_binfo, |
10099 | tree vtbl, |
10100 | tree t, |
10101 | vec<constructor_elt, va_gc> **inits) |
10102 | { |
10103 | int i; |
10104 | tree base_binfo; |
10105 | int ctor_vtbl_p = !SAME_BINFO_TYPE_P (BINFO_TYPE (rtti_binfo), t); |
10106 | |
10107 | gcc_assert (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), BINFO_TYPE (orig_binfo))); |
10108 | |
10109 | /* If it doesn't have a vptr, we don't do anything. */ |
10110 | if (!TYPE_CONTAINS_VPTR_P (BINFO_TYPE (binfo))) |
10111 | return; |
10112 | |
10113 | /* If we're building a construction vtable, we're not interested in |
10114 | subobjects that don't require construction vtables. */ |
10115 | if (ctor_vtbl_p |
10116 | && !CLASSTYPE_VBASECLASSES (BINFO_TYPE (binfo)) |
10117 | && !binfo_via_virtual (orig_binfo, BINFO_TYPE (rtti_binfo))) |
10118 | return; |
10119 | |
10120 | /* Build the initializers for the BINFO-in-T vtable. */ |
10121 | dfs_accumulate_vtbl_inits (binfo, orig_binfo, rtti_binfo, vtbl, t, inits); |
10122 | |
10123 | /* Walk the BINFO and its bases. We walk in preorder so that as we |
10124 | initialize each vtable we can figure out at what offset the |
10125 | secondary vtable lies from the primary vtable. We can't use |
10126 | dfs_walk here because we need to iterate through bases of BINFO |
10127 | and RTTI_BINFO simultaneously. */ |
10128 | for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i) |
10129 | { |
10130 | /* Skip virtual bases. */ |
10131 | if (BINFO_VIRTUAL_P (base_binfo)) |
10132 | continue; |
10133 | accumulate_vtbl_inits (binfo: base_binfo, |
10134 | BINFO_BASE_BINFO (orig_binfo, i), |
10135 | rtti_binfo, vtbl, t, |
10136 | inits); |
10137 | } |
10138 | } |
10139 | |
10140 | /* Called from accumulate_vtbl_inits. Adds the initializers for the |
10141 | BINFO vtable to L. */ |
10142 | |
10143 | static void |
10144 | dfs_accumulate_vtbl_inits (tree binfo, |
10145 | tree orig_binfo, |
10146 | tree rtti_binfo, |
10147 | tree orig_vtbl, |
10148 | tree t, |
10149 | vec<constructor_elt, va_gc> **l) |
10150 | { |
10151 | tree vtbl = NULL_TREE; |
10152 | int ctor_vtbl_p = !SAME_BINFO_TYPE_P (BINFO_TYPE (rtti_binfo), t); |
10153 | int n_inits; |
10154 | |
10155 | if (ctor_vtbl_p |
10156 | && BINFO_VIRTUAL_P (orig_binfo) && BINFO_PRIMARY_P (orig_binfo)) |
10157 | { |
10158 | /* In the hierarchy of BINFO_TYPE (RTTI_BINFO), this is a |
10159 | primary virtual base. If it is not the same primary in |
10160 | the hierarchy of T, we'll need to generate a ctor vtable |
10161 | for it, to place at its location in T. If it is the same |
10162 | primary, we still need a VTT entry for the vtable, but it |
10163 | should point to the ctor vtable for the base it is a |
10164 | primary for within the sub-hierarchy of RTTI_BINFO. |
10165 | |
10166 | There are three possible cases: |
10167 | |
10168 | 1) We are in the same place. |
10169 | 2) We are a primary base within a lost primary virtual base of |
10170 | RTTI_BINFO. |
10171 | 3) We are primary to something not a base of RTTI_BINFO. */ |
10172 | |
10173 | tree b; |
10174 | tree last = NULL_TREE; |
10175 | |
10176 | /* First, look through the bases we are primary to for RTTI_BINFO |
10177 | or a virtual base. */ |
10178 | b = binfo; |
10179 | while (BINFO_PRIMARY_P (b)) |
10180 | { |
10181 | b = BINFO_INHERITANCE_CHAIN (b); |
10182 | last = b; |
10183 | if (BINFO_VIRTUAL_P (b) || b == rtti_binfo) |
10184 | goto found; |
10185 | } |
10186 | /* If we run out of primary links, keep looking down our |
10187 | inheritance chain; we might be an indirect primary. */ |
10188 | for (b = last; b; b = BINFO_INHERITANCE_CHAIN (b)) |
10189 | if (BINFO_VIRTUAL_P (b) || b == rtti_binfo) |
10190 | break; |
10191 | found: |
10192 | |
10193 | /* If we found RTTI_BINFO, this is case 1. If we found a virtual |
10194 | base B and it is a base of RTTI_BINFO, this is case 2. In |
10195 | either case, we share our vtable with LAST, i.e. the |
10196 | derived-most base within B of which we are a primary. */ |
10197 | if (b == rtti_binfo |
10198 | || (b && binfo_for_vbase (BINFO_TYPE (b), BINFO_TYPE (rtti_binfo)))) |
10199 | /* Just set our BINFO_VTABLE to point to LAST, as we may not have |
10200 | set LAST's BINFO_VTABLE yet. We'll extract the actual vptr in |
10201 | binfo_ctor_vtable after everything's been set up. */ |
10202 | vtbl = last; |
10203 | |
10204 | /* Otherwise, this is case 3 and we get our own. */ |
10205 | } |
10206 | else if (!BINFO_NEW_VTABLE_MARKED (orig_binfo)) |
10207 | return; |
10208 | |
10209 | n_inits = vec_safe_length (v: *l); |
10210 | |
10211 | if (!vtbl) |
10212 | { |
10213 | tree index; |
10214 | int non_fn_entries; |
10215 | |
10216 | /* Add the initializer for this vtable. */ |
10217 | build_vtbl_initializer (binfo, orig_binfo, t, rtti_binfo, |
10218 | &non_fn_entries, l); |
10219 | |
10220 | /* Figure out the position to which the VPTR should point. */ |
10221 | vtbl = build1 (ADDR_EXPR, vtbl_ptr_type_node, orig_vtbl); |
10222 | index = size_binop (MULT_EXPR, |
10223 | TYPE_SIZE_UNIT (vtable_entry_type), |
10224 | size_int (non_fn_entries + n_inits)); |
10225 | vtbl = fold_build_pointer_plus (vtbl, index); |
10226 | } |
10227 | |
10228 | if (ctor_vtbl_p) |
10229 | /* For a construction vtable, we can't overwrite BINFO_VTABLE. |
10230 | So, we make a TREE_LIST. Later, dfs_fixup_binfo_vtbls will |
10231 | straighten this out. */ |
10232 | BINFO_VTABLE (binfo) = tree_cons (rtti_binfo, vtbl, BINFO_VTABLE (binfo)); |
10233 | else if (BINFO_PRIMARY_P (binfo) && BINFO_VIRTUAL_P (binfo)) |
10234 | /* Throw away any unneeded intializers. */ |
10235 | (*l)->truncate (size: n_inits); |
10236 | else |
10237 | /* For an ordinary vtable, set BINFO_VTABLE. */ |
10238 | BINFO_VTABLE (binfo) = vtbl; |
10239 | } |
10240 | |
10241 | static GTY(()) tree abort_fndecl_addr; |
10242 | static GTY(()) tree dvirt_fn; |
10243 | |
10244 | /* Construct the initializer for BINFO's virtual function table. BINFO |
10245 | is part of the hierarchy dominated by T. If we're building a |
10246 | construction vtable, the ORIG_BINFO is the binfo we should use to |
10247 | find the actual function pointers to put in the vtable - but they |
10248 | can be overridden on the path to most-derived in the graph that |
10249 | ORIG_BINFO belongs. Otherwise, |
10250 | ORIG_BINFO should be the same as BINFO. The RTTI_BINFO is the |
10251 | BINFO that should be indicated by the RTTI information in the |
10252 | vtable; it will be a base class of T, rather than T itself, if we |
10253 | are building a construction vtable. |
10254 | |
10255 | The value returned is a TREE_LIST suitable for wrapping in a |
10256 | CONSTRUCTOR to use as the DECL_INITIAL for a vtable. If |
10257 | NON_FN_ENTRIES_P is not NULL, *NON_FN_ENTRIES_P is set to the |
10258 | number of non-function entries in the vtable. |
10259 | |
10260 | It might seem that this function should never be called with a |
10261 | BINFO for which BINFO_PRIMARY_P holds, the vtable for such a |
10262 | base is always subsumed by a derived class vtable. However, when |
10263 | we are building construction vtables, we do build vtables for |
10264 | primary bases; we need these while the primary base is being |
10265 | constructed. */ |
10266 | |
10267 | static void |
10268 | build_vtbl_initializer (tree binfo, |
10269 | tree orig_binfo, |
10270 | tree t, |
10271 | tree rtti_binfo, |
10272 | int* non_fn_entries_p, |
10273 | vec<constructor_elt, va_gc> **inits) |
10274 | { |
10275 | tree v; |
10276 | vtbl_init_data vid; |
10277 | unsigned ix, jx; |
10278 | tree vbinfo; |
10279 | vec<tree, va_gc> *vbases; |
10280 | constructor_elt *e; |
10281 | |
10282 | /* Initialize VID. */ |
10283 | memset (s: &vid, c: 0, n: sizeof (vid)); |
10284 | vid.binfo = binfo; |
10285 | vid.derived = t; |
10286 | vid.rtti_binfo = rtti_binfo; |
10287 | vid.primary_vtbl_p = SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), t); |
10288 | vid.ctor_vtbl_p = !SAME_BINFO_TYPE_P (BINFO_TYPE (rtti_binfo), t); |
10289 | vid.generate_vcall_entries = true; |
10290 | /* The first vbase or vcall offset is at index -3 in the vtable. */ |
10291 | vid.index = ssize_int(-3 * TARGET_VTABLE_DATA_ENTRY_DISTANCE); |
10292 | |
10293 | /* Add entries to the vtable for RTTI. */ |
10294 | build_rtti_vtbl_entries (binfo, &vid); |
10295 | |
10296 | /* Create an array for keeping track of the functions we've |
10297 | processed. When we see multiple functions with the same |
10298 | signature, we share the vcall offsets. */ |
10299 | vec_alloc (v&: vid.fns, nelems: 32); |
10300 | /* Add the vcall and vbase offset entries. */ |
10301 | build_vcall_and_vbase_vtbl_entries (binfo, &vid); |
10302 | |
10303 | /* Clear BINFO_VTABLE_PATH_MARKED; it's set by |
10304 | build_vbase_offset_vtbl_entries. */ |
10305 | for (vbases = CLASSTYPE_VBASECLASSES (t), ix = 0; |
10306 | vec_safe_iterate (v: vbases, ix, ptr: &vbinfo); ix++) |
10307 | BINFO_VTABLE_PATH_MARKED (vbinfo) = 0; |
10308 | |
10309 | /* If the target requires padding between data entries, add that now. */ |
10310 | if (TARGET_VTABLE_DATA_ENTRY_DISTANCE > 1) |
10311 | { |
10312 | int n_entries = vec_safe_length (v: vid.inits); |
10313 | |
10314 | vec_safe_grow (v&: vid.inits, TARGET_VTABLE_DATA_ENTRY_DISTANCE * n_entries, |
10315 | exact: true); |
10316 | |
10317 | /* Move data entries into their new positions and add padding |
10318 | after the new positions. Iterate backwards so we don't |
10319 | overwrite entries that we would need to process later. */ |
10320 | for (ix = n_entries - 1; |
10321 | vid.inits->iterate (ix, ptr: &e); |
10322 | ix--) |
10323 | { |
10324 | int j; |
10325 | int new_position = (TARGET_VTABLE_DATA_ENTRY_DISTANCE * ix |
10326 | + (TARGET_VTABLE_DATA_ENTRY_DISTANCE - 1)); |
10327 | |
10328 | (*vid.inits)[new_position] = *e; |
10329 | |
10330 | for (j = 1; j < TARGET_VTABLE_DATA_ENTRY_DISTANCE; ++j) |
10331 | { |
10332 | constructor_elt *f = &(*vid.inits)[new_position - j]; |
10333 | f->index = NULL_TREE; |
10334 | f->value = build1 (NOP_EXPR, vtable_entry_type, |
10335 | null_pointer_node); |
10336 | } |
10337 | } |
10338 | } |
10339 | |
10340 | if (non_fn_entries_p) |
10341 | *non_fn_entries_p = vec_safe_length (v: vid.inits); |
10342 | |
10343 | /* The initializers for virtual functions were built up in reverse |
10344 | order. Straighten them out and add them to the running list in one |
10345 | step. */ |
10346 | jx = vec_safe_length (v: *inits); |
10347 | vec_safe_grow (v&: *inits, len: jx + vid.inits->length (), exact: true); |
10348 | |
10349 | for (ix = vid.inits->length () - 1; |
10350 | vid.inits->iterate (ix, ptr: &e); |
10351 | ix--, jx++) |
10352 | (**inits)[jx] = *e; |
10353 | |
10354 | /* Go through all the ordinary virtual functions, building up |
10355 | initializers. */ |
10356 | for (v = BINFO_VIRTUALS (orig_binfo); v; v = TREE_CHAIN (v)) |
10357 | { |
10358 | tree delta; |
10359 | tree vcall_index; |
10360 | tree fn, fn_original; |
10361 | tree init = NULL_TREE; |
10362 | |
10363 | fn = BV_FN (v); |
10364 | fn_original = fn; |
10365 | if (DECL_THUNK_P (fn)) |
10366 | { |
10367 | if (!DECL_NAME (fn)) |
10368 | finish_thunk (fn); |
10369 | if (THUNK_ALIAS (fn)) |
10370 | { |
10371 | fn = THUNK_ALIAS (fn); |
10372 | BV_FN (v) = fn; |
10373 | } |
10374 | fn_original = THUNK_TARGET (fn); |
10375 | } |
10376 | |
10377 | /* If the only definition of this function signature along our |
10378 | primary base chain is from a lost primary, this vtable slot will |
10379 | never be used, so just zero it out. This is important to avoid |
10380 | requiring extra thunks which cannot be generated with the function. |
10381 | |
10382 | We first check this in update_vtable_entry_for_fn, so we handle |
10383 | restored primary bases properly; we also need to do it here so we |
10384 | zero out unused slots in ctor vtables, rather than filling them |
10385 | with erroneous values (though harmless, apart from relocation |
10386 | costs). */ |
10387 | if (BV_LOST_PRIMARY (v)) |
10388 | init = size_zero_node; |
10389 | |
10390 | if (! init) |
10391 | { |
10392 | /* Pull the offset for `this', and the function to call, out of |
10393 | the list. */ |
10394 | delta = BV_DELTA (v); |
10395 | vcall_index = BV_VCALL_INDEX (v); |
10396 | |
10397 | gcc_assert (TREE_CODE (delta) == INTEGER_CST); |
10398 | gcc_assert (TREE_CODE (fn) == FUNCTION_DECL); |
10399 | |
10400 | /* You can't call an abstract virtual function; it's abstract. |
10401 | So, we replace these functions with __pure_virtual. */ |
10402 | if (DECL_PURE_VIRTUAL_P (fn_original)) |
10403 | { |
10404 | fn = abort_fndecl; |
10405 | if (!TARGET_VTABLE_USES_DESCRIPTORS) |
10406 | { |
10407 | if (abort_fndecl_addr == NULL) |
10408 | abort_fndecl_addr |
10409 | = fold_convert (vfunc_ptr_type_node, |
10410 | build_fold_addr_expr (fn)); |
10411 | init = abort_fndecl_addr; |
10412 | } |
10413 | } |
10414 | /* Likewise for deleted virtuals. */ |
10415 | else if (DECL_DELETED_FN (fn_original)) |
10416 | { |
10417 | if (!dvirt_fn) |
10418 | { |
10419 | tree name = get_identifier ("__cxa_deleted_virtual" ); |
10420 | dvirt_fn = get_global_binding (id: name); |
10421 | if (!dvirt_fn) |
10422 | dvirt_fn = push_library_fn |
10423 | (name, |
10424 | build_function_type_list (void_type_node, NULL_TREE), |
10425 | NULL_TREE, ECF_NORETURN | ECF_COLD); |
10426 | } |
10427 | fn = dvirt_fn; |
10428 | if (!TARGET_VTABLE_USES_DESCRIPTORS) |
10429 | init = fold_convert (vfunc_ptr_type_node, |
10430 | build_fold_addr_expr (fn)); |
10431 | } |
10432 | else |
10433 | { |
10434 | if (!integer_zerop (delta) || vcall_index) |
10435 | { |
10436 | fn = make_thunk (fn, /*this_adjusting=*/1, |
10437 | delta, vcall_index); |
10438 | if (!DECL_NAME (fn)) |
10439 | finish_thunk (fn); |
10440 | } |
10441 | /* Take the address of the function, considering it to be of an |
10442 | appropriate generic type. */ |
10443 | if (!TARGET_VTABLE_USES_DESCRIPTORS) |
10444 | init = fold_convert (vfunc_ptr_type_node, |
10445 | build_fold_addr_expr (fn)); |
10446 | /* Don't refer to a virtual destructor from a constructor |
10447 | vtable or a vtable for an abstract class, since destroying |
10448 | an object under construction is undefined behavior and we |
10449 | don't want it to be considered a candidate for speculative |
10450 | devirtualization. But do create the thunk for ABI |
10451 | compliance. */ |
10452 | if (DECL_DESTRUCTOR_P (fn_original) |
10453 | && (CLASSTYPE_PURE_VIRTUALS (DECL_CONTEXT (fn_original)) |
10454 | || orig_binfo != binfo)) |
10455 | init = size_zero_node; |
10456 | } |
10457 | } |
10458 | |
10459 | /* And add it to the chain of initializers. */ |
10460 | if (TARGET_VTABLE_USES_DESCRIPTORS) |
10461 | { |
10462 | int i; |
10463 | if (init == size_zero_node) |
10464 | for (i = 0; i < TARGET_VTABLE_USES_DESCRIPTORS; ++i) |
10465 | CONSTRUCTOR_APPEND_ELT (*inits, size_int (jx++), init); |
10466 | else |
10467 | for (i = 0; i < TARGET_VTABLE_USES_DESCRIPTORS; ++i) |
10468 | { |
10469 | tree fdesc = build2 (FDESC_EXPR, vfunc_ptr_type_node, |
10470 | fn, build_int_cst (NULL_TREE, i)); |
10471 | TREE_CONSTANT (fdesc) = 1; |
10472 | |
10473 | CONSTRUCTOR_APPEND_ELT (*inits, size_int (jx++), fdesc); |
10474 | } |
10475 | } |
10476 | else |
10477 | CONSTRUCTOR_APPEND_ELT (*inits, size_int (jx++), init); |
10478 | } |
10479 | } |
10480 | |
10481 | /* Adds to vid->inits the initializers for the vbase and vcall |
10482 | offsets in BINFO, which is in the hierarchy dominated by T. */ |
10483 | |
10484 | static void |
10485 | build_vcall_and_vbase_vtbl_entries (tree binfo, vtbl_init_data* vid) |
10486 | { |
10487 | tree b; |
10488 | |
10489 | /* If this is a derived class, we must first create entries |
10490 | corresponding to the primary base class. */ |
10491 | b = get_primary_binfo (binfo); |
10492 | if (b) |
10493 | build_vcall_and_vbase_vtbl_entries (binfo: b, vid); |
10494 | |
10495 | /* Add the vbase entries for this base. */ |
10496 | build_vbase_offset_vtbl_entries (binfo, vid); |
10497 | /* Add the vcall entries for this base. */ |
10498 | build_vcall_offset_vtbl_entries (binfo, vid); |
10499 | } |
10500 | |
10501 | /* Returns the initializers for the vbase offset entries in the vtable |
10502 | for BINFO (which is part of the class hierarchy dominated by T), in |
10503 | reverse order. VBASE_OFFSET_INDEX gives the vtable index |
10504 | where the next vbase offset will go. */ |
10505 | |
10506 | static void |
10507 | build_vbase_offset_vtbl_entries (tree binfo, vtbl_init_data* vid) |
10508 | { |
10509 | tree vbase; |
10510 | tree t; |
10511 | tree non_primary_binfo; |
10512 | |
10513 | /* If there are no virtual baseclasses, then there is nothing to |
10514 | do. */ |
10515 | if (!CLASSTYPE_VBASECLASSES (BINFO_TYPE (binfo))) |
10516 | return; |
10517 | |
10518 | t = vid->derived; |
10519 | |
10520 | /* We might be a primary base class. Go up the inheritance hierarchy |
10521 | until we find the most derived class of which we are a primary base: |
10522 | it is the offset of that which we need to use. */ |
10523 | non_primary_binfo = binfo; |
10524 | while (BINFO_INHERITANCE_CHAIN (non_primary_binfo)) |
10525 | { |
10526 | tree b; |
10527 | |
10528 | /* If we have reached a virtual base, then it must be a primary |
10529 | base (possibly multi-level) of vid->binfo, or we wouldn't |
10530 | have called build_vcall_and_vbase_vtbl_entries for it. But it |
10531 | might be a lost primary, so just skip down to vid->binfo. */ |
10532 | if (BINFO_VIRTUAL_P (non_primary_binfo)) |
10533 | { |
10534 | non_primary_binfo = vid->binfo; |
10535 | break; |
10536 | } |
10537 | |
10538 | b = BINFO_INHERITANCE_CHAIN (non_primary_binfo); |
10539 | if (get_primary_binfo (binfo: b) != non_primary_binfo) |
10540 | break; |
10541 | non_primary_binfo = b; |
10542 | } |
10543 | |
10544 | /* Go through the virtual bases, adding the offsets. */ |
10545 | for (vbase = TYPE_BINFO (BINFO_TYPE (binfo)); |
10546 | vbase; |
10547 | vbase = TREE_CHAIN (vbase)) |
10548 | { |
10549 | tree b; |
10550 | tree delta; |
10551 | |
10552 | if (!BINFO_VIRTUAL_P (vbase)) |
10553 | continue; |
10554 | |
10555 | /* Find the instance of this virtual base in the complete |
10556 | object. */ |
10557 | b = copied_binfo (vbase, binfo); |
10558 | |
10559 | /* If we've already got an offset for this virtual base, we |
10560 | don't need another one. */ |
10561 | if (BINFO_VTABLE_PATH_MARKED (b)) |
10562 | continue; |
10563 | BINFO_VTABLE_PATH_MARKED (b) = 1; |
10564 | |
10565 | /* Figure out where we can find this vbase offset. */ |
10566 | delta = size_binop (MULT_EXPR, |
10567 | vid->index, |
10568 | fold_convert (ssizetype, |
10569 | TYPE_SIZE_UNIT (vtable_entry_type))); |
10570 | if (vid->primary_vtbl_p) |
10571 | BINFO_VPTR_FIELD (b) = delta; |
10572 | |
10573 | if (binfo != TYPE_BINFO (t)) |
10574 | /* The vbase offset had better be the same. */ |
10575 | gcc_assert (tree_int_cst_equal (delta, BINFO_VPTR_FIELD (vbase))); |
10576 | |
10577 | /* The next vbase will come at a more negative offset. */ |
10578 | vid->index = size_binop (MINUS_EXPR, vid->index, |
10579 | ssize_int (TARGET_VTABLE_DATA_ENTRY_DISTANCE)); |
10580 | |
10581 | /* The initializer is the delta from BINFO to this virtual base. |
10582 | The vbase offsets go in reverse inheritance-graph order, and |
10583 | we are walking in inheritance graph order so these end up in |
10584 | the right order. */ |
10585 | delta = size_diffop_loc (input_location, |
10586 | BINFO_OFFSET (b), BINFO_OFFSET (non_primary_binfo)); |
10587 | |
10588 | CONSTRUCTOR_APPEND_ELT (vid->inits, NULL_TREE, |
10589 | fold_build1_loc (input_location, NOP_EXPR, |
10590 | vtable_entry_type, delta)); |
10591 | } |
10592 | } |
10593 | |
10594 | /* Adds the initializers for the vcall offset entries in the vtable |
10595 | for BINFO (which is part of the class hierarchy dominated by VID->DERIVED) |
10596 | to VID->INITS. */ |
10597 | |
10598 | static void |
10599 | build_vcall_offset_vtbl_entries (tree binfo, vtbl_init_data* vid) |
10600 | { |
10601 | /* We only need these entries if this base is a virtual base. We |
10602 | compute the indices -- but do not add to the vtable -- when |
10603 | building the main vtable for a class. */ |
10604 | if (binfo == TYPE_BINFO (vid->derived) |
10605 | || (BINFO_VIRTUAL_P (binfo) |
10606 | /* If BINFO is RTTI_BINFO, then (since BINFO does not |
10607 | correspond to VID->DERIVED), we are building a primary |
10608 | construction virtual table. Since this is a primary |
10609 | virtual table, we do not need the vcall offsets for |
10610 | BINFO. */ |
10611 | && binfo != vid->rtti_binfo)) |
10612 | { |
10613 | /* We need a vcall offset for each of the virtual functions in this |
10614 | vtable. For example: |
10615 | |
10616 | class A { virtual void f (); }; |
10617 | class B1 : virtual public A { virtual void f (); }; |
10618 | class B2 : virtual public A { virtual void f (); }; |
10619 | class C: public B1, public B2 { virtual void f (); }; |
10620 | |
10621 | A C object has a primary base of B1, which has a primary base of A. A |
10622 | C also has a secondary base of B2, which no longer has a primary base |
10623 | of A. So the B2-in-C construction vtable needs a secondary vtable for |
10624 | A, which will adjust the A* to a B2* to call f. We have no way of |
10625 | knowing what (or even whether) this offset will be when we define B2, |
10626 | so we store this "vcall offset" in the A sub-vtable and look it up in |
10627 | a "virtual thunk" for B2::f. |
10628 | |
10629 | We need entries for all the functions in our primary vtable and |
10630 | in our non-virtual bases' secondary vtables. */ |
10631 | vid->vbase = binfo; |
10632 | /* If we are just computing the vcall indices -- but do not need |
10633 | the actual entries -- not that. */ |
10634 | if (!BINFO_VIRTUAL_P (binfo)) |
10635 | vid->generate_vcall_entries = false; |
10636 | /* Now, walk through the non-virtual bases, adding vcall offsets. */ |
10637 | add_vcall_offset_vtbl_entries_r (binfo, vid); |
10638 | } |
10639 | } |
10640 | |
10641 | /* Build vcall offsets, starting with those for BINFO. */ |
10642 | |
10643 | static void |
10644 | add_vcall_offset_vtbl_entries_r (tree binfo, vtbl_init_data* vid) |
10645 | { |
10646 | int i; |
10647 | tree primary_binfo; |
10648 | tree base_binfo; |
10649 | |
10650 | /* Don't walk into virtual bases -- except, of course, for the |
10651 | virtual base for which we are building vcall offsets. Any |
10652 | primary virtual base will have already had its offsets generated |
10653 | through the recursion in build_vcall_and_vbase_vtbl_entries. */ |
10654 | if (BINFO_VIRTUAL_P (binfo) && vid->vbase != binfo) |
10655 | return; |
10656 | |
10657 | /* If BINFO has a primary base, process it first. */ |
10658 | primary_binfo = get_primary_binfo (binfo); |
10659 | if (primary_binfo) |
10660 | add_vcall_offset_vtbl_entries_r (binfo: primary_binfo, vid); |
10661 | |
10662 | /* Add BINFO itself to the list. */ |
10663 | add_vcall_offset_vtbl_entries_1 (binfo, vid); |
10664 | |
10665 | /* Scan the non-primary bases of BINFO. */ |
10666 | for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i) |
10667 | if (base_binfo != primary_binfo) |
10668 | add_vcall_offset_vtbl_entries_r (binfo: base_binfo, vid); |
10669 | } |
10670 | |
10671 | /* Called from build_vcall_offset_vtbl_entries_r. */ |
10672 | |
10673 | static void |
10674 | add_vcall_offset_vtbl_entries_1 (tree binfo, vtbl_init_data* vid) |
10675 | { |
10676 | /* Make entries for the rest of the virtuals. */ |
10677 | tree orig_fn; |
10678 | |
10679 | /* The ABI requires that the methods be processed in declaration |
10680 | order. */ |
10681 | for (orig_fn = TYPE_FIELDS (BINFO_TYPE (binfo)); |
10682 | orig_fn; |
10683 | orig_fn = DECL_CHAIN (orig_fn)) |
10684 | if (TREE_CODE (orig_fn) == FUNCTION_DECL && DECL_VINDEX (orig_fn)) |
10685 | add_vcall_offset (orig_fn, binfo, vid); |
10686 | } |
10687 | |
10688 | /* Add a vcall offset entry for ORIG_FN to the vtable. */ |
10689 | |
10690 | static void |
10691 | add_vcall_offset (tree orig_fn, tree binfo, vtbl_init_data *vid) |
10692 | { |
10693 | size_t i; |
10694 | tree vcall_offset; |
10695 | tree derived_entry; |
10696 | |
10697 | /* If there is already an entry for a function with the same |
10698 | signature as FN, then we do not need a second vcall offset. |
10699 | Check the list of functions already present in the derived |
10700 | class vtable. */ |
10701 | FOR_EACH_VEC_SAFE_ELT (vid->fns, i, derived_entry) |
10702 | { |
10703 | if (same_signature_p (fndecl: derived_entry, base_fndecl: orig_fn) |
10704 | /* We only use one vcall offset for virtual destructors, |
10705 | even though there are two virtual table entries. */ |
10706 | || (DECL_DESTRUCTOR_P (derived_entry) |
10707 | && DECL_DESTRUCTOR_P (orig_fn))) |
10708 | return; |
10709 | } |
10710 | |
10711 | /* If we are building these vcall offsets as part of building |
10712 | the vtable for the most derived class, remember the vcall |
10713 | offset. */ |
10714 | if (vid->binfo == TYPE_BINFO (vid->derived)) |
10715 | { |
10716 | tree_pair_s elt = {.purpose: orig_fn, .value: vid->index}; |
10717 | vec_safe_push (CLASSTYPE_VCALL_INDICES (vid->derived), obj: elt); |
10718 | } |
10719 | |
10720 | /* The next vcall offset will be found at a more negative |
10721 | offset. */ |
10722 | vid->index = size_binop (MINUS_EXPR, vid->index, |
10723 | ssize_int (TARGET_VTABLE_DATA_ENTRY_DISTANCE)); |
10724 | |
10725 | /* Keep track of this function. */ |
10726 | vec_safe_push (v&: vid->fns, obj: orig_fn); |
10727 | |
10728 | if (vid->generate_vcall_entries) |
10729 | { |
10730 | tree base; |
10731 | tree fn; |
10732 | |
10733 | /* Find the overriding function. */ |
10734 | fn = find_final_overrider (derived: vid->rtti_binfo, binfo, fn: orig_fn); |
10735 | if (fn == error_mark_node) |
10736 | vcall_offset = build_zero_cst (vtable_entry_type); |
10737 | else |
10738 | { |
10739 | base = TREE_VALUE (fn); |
10740 | |
10741 | /* The vbase we're working on is a primary base of |
10742 | vid->binfo. But it might be a lost primary, so its |
10743 | BINFO_OFFSET might be wrong, so we just use the |
10744 | BINFO_OFFSET from vid->binfo. */ |
10745 | vcall_offset = size_diffop_loc (input_location, |
10746 | BINFO_OFFSET (base), |
10747 | BINFO_OFFSET (vid->binfo)); |
10748 | vcall_offset = fold_build1_loc (input_location, |
10749 | NOP_EXPR, vtable_entry_type, |
10750 | vcall_offset); |
10751 | } |
10752 | /* Add the initializer to the vtable. */ |
10753 | CONSTRUCTOR_APPEND_ELT (vid->inits, NULL_TREE, vcall_offset); |
10754 | } |
10755 | } |
10756 | |
10757 | /* Return vtbl initializers for the RTTI entries corresponding to the |
10758 | BINFO's vtable. The RTTI entries should indicate the object given |
10759 | by VID->rtti_binfo. */ |
10760 | |
10761 | static void |
10762 | build_rtti_vtbl_entries (tree binfo, vtbl_init_data* vid) |
10763 | { |
10764 | tree b; |
10765 | tree t; |
10766 | tree offset; |
10767 | tree decl; |
10768 | tree init; |
10769 | |
10770 | t = BINFO_TYPE (vid->rtti_binfo); |
10771 | |
10772 | /* To find the complete object, we will first convert to our most |
10773 | primary base, and then add the offset in the vtbl to that value. */ |
10774 | b = most_primary_binfo (binfo); |
10775 | offset = size_diffop_loc (input_location, |
10776 | BINFO_OFFSET (vid->rtti_binfo), BINFO_OFFSET (b)); |
10777 | |
10778 | /* The second entry is the address of the typeinfo object. */ |
10779 | if (flag_rtti) |
10780 | decl = build_address (get_tinfo_decl (t)); |
10781 | else |
10782 | decl = integer_zero_node; |
10783 | |
10784 | /* Convert the declaration to a type that can be stored in the |
10785 | vtable. */ |
10786 | init = build_nop (vfunc_ptr_type_node, decl); |
10787 | CONSTRUCTOR_APPEND_ELT (vid->inits, NULL_TREE, init); |
10788 | |
10789 | /* Add the offset-to-top entry. It comes earlier in the vtable than |
10790 | the typeinfo entry. Convert the offset to look like a |
10791 | function pointer, so that we can put it in the vtable. */ |
10792 | init = build_nop (vfunc_ptr_type_node, offset); |
10793 | CONSTRUCTOR_APPEND_ELT (vid->inits, NULL_TREE, init); |
10794 | } |
10795 | |
10796 | /* TRUE iff TYPE is uniquely derived from PARENT. Ignores |
10797 | accessibility. */ |
10798 | |
10799 | bool |
10800 | uniquely_derived_from_p (tree parent, tree type) |
10801 | { |
10802 | tree base = lookup_base (type, parent, ba_unique, NULL, tf_none); |
10803 | return base && base != error_mark_node; |
10804 | } |
10805 | |
10806 | /* TRUE iff TYPE is publicly & uniquely derived from PARENT. */ |
10807 | |
10808 | bool |
10809 | publicly_uniquely_derived_p (tree parent, tree type) |
10810 | { |
10811 | tree base = lookup_base (type, parent, ba_ignore_scope | ba_check, |
10812 | NULL, tf_none); |
10813 | return base && base != error_mark_node; |
10814 | } |
10815 | |
10816 | /* CTX1 and CTX2 are declaration contexts. Return the innermost common |
10817 | class between them, if any. */ |
10818 | |
10819 | tree |
10820 | common_enclosing_class (tree ctx1, tree ctx2) |
10821 | { |
10822 | if (!TYPE_P (ctx1) || !TYPE_P (ctx2)) |
10823 | return NULL_TREE; |
10824 | gcc_assert (ctx1 == TYPE_MAIN_VARIANT (ctx1) |
10825 | && ctx2 == TYPE_MAIN_VARIANT (ctx2)); |
10826 | if (ctx1 == ctx2) |
10827 | return ctx1; |
10828 | for (tree t = ctx1; TYPE_P (t); t = TYPE_CONTEXT (t)) |
10829 | TYPE_MARKED_P (t) = true; |
10830 | tree found = NULL_TREE; |
10831 | for (tree t = ctx2; TYPE_P (t); t = TYPE_CONTEXT (t)) |
10832 | if (TYPE_MARKED_P (t)) |
10833 | { |
10834 | found = t; |
10835 | break; |
10836 | } |
10837 | for (tree t = ctx1; TYPE_P (t); t = TYPE_CONTEXT (t)) |
10838 | TYPE_MARKED_P (t) = false; |
10839 | return found; |
10840 | } |
10841 | |
10842 | #include "gt-cp-class.h" |
10843 | |