]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/cp/class.c
gcc/
[thirdparty/gcc.git] / gcc / cp / class.c
1 /* Functions related to building classes and their related objects.
2 Copyright (C) 1987-2015 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 "alias.h"
28 #include "symtab.h"
29 #include "options.h"
30 #include "tm.h"
31 #include "tree.h"
32 #include "stringpool.h"
33 #include "stor-layout.h"
34 #include "attribs.h"
35 #include "cp-tree.h"
36 #include "flags.h"
37 #include "toplev.h"
38 #include "target.h"
39 #include "convert.h"
40 #include "plugin-api.h"
41 #include "hard-reg-set.h"
42 #include "function.h"
43 #include "ipa-ref.h"
44 #include "cgraph.h"
45 #include "dumpfile.h"
46 #include "splay-tree.h"
47 #include "gimplify.h"
48
49 /* The number of nested classes being processed. If we are not in the
50 scope of any class, this is zero. */
51
52 int current_class_depth;
53
54 /* In order to deal with nested classes, we keep a stack of classes.
55 The topmost entry is the innermost class, and is the entry at index
56 CURRENT_CLASS_DEPTH */
57
58 typedef struct class_stack_node {
59 /* The name of the class. */
60 tree name;
61
62 /* The _TYPE node for the class. */
63 tree type;
64
65 /* The access specifier pending for new declarations in the scope of
66 this class. */
67 tree access;
68
69 /* If were defining TYPE, the names used in this class. */
70 splay_tree names_used;
71
72 /* Nonzero if this class is no longer open, because of a call to
73 push_to_top_level. */
74 size_t hidden;
75 }* class_stack_node_t;
76
77 typedef struct vtbl_init_data_s
78 {
79 /* The base for which we're building initializers. */
80 tree binfo;
81 /* The type of the most-derived type. */
82 tree derived;
83 /* The binfo for the dynamic type. This will be TYPE_BINFO (derived),
84 unless ctor_vtbl_p is true. */
85 tree rtti_binfo;
86 /* The negative-index vtable initializers built up so far. These
87 are in order from least negative index to most negative index. */
88 vec<constructor_elt, va_gc> *inits;
89 /* The binfo for the virtual base for which we're building
90 vcall offset initializers. */
91 tree vbase;
92 /* The functions in vbase for which we have already provided vcall
93 offsets. */
94 vec<tree, va_gc> *fns;
95 /* The vtable index of the next vcall or vbase offset. */
96 tree index;
97 /* Nonzero if we are building the initializer for the primary
98 vtable. */
99 int primary_vtbl_p;
100 /* Nonzero if we are building the initializer for a construction
101 vtable. */
102 int ctor_vtbl_p;
103 /* True when adding vcall offset entries to the vtable. False when
104 merely computing the indices. */
105 bool generate_vcall_entries;
106 } vtbl_init_data;
107
108 /* The type of a function passed to walk_subobject_offsets. */
109 typedef int (*subobject_offset_fn) (tree, tree, splay_tree);
110
111 /* The stack itself. This is a dynamically resized array. The
112 number of elements allocated is CURRENT_CLASS_STACK_SIZE. */
113 static int current_class_stack_size;
114 static class_stack_node_t current_class_stack;
115
116 /* The size of the largest empty class seen in this translation unit. */
117 static GTY (()) tree sizeof_biggest_empty_class;
118
119 /* An array of all local classes present in this translation unit, in
120 declaration order. */
121 vec<tree, va_gc> *local_classes;
122
123 static tree get_vfield_name (tree);
124 static void finish_struct_anon (tree);
125 static tree get_vtable_name (tree);
126 static void get_basefndecls (tree, tree, vec<tree> *);
127 static int build_primary_vtable (tree, tree);
128 static int build_secondary_vtable (tree);
129 static void finish_vtbls (tree);
130 static void modify_vtable_entry (tree, tree, tree, tree, tree *);
131 static void finish_struct_bits (tree);
132 static int alter_access (tree, tree, tree);
133 static void handle_using_decl (tree, tree);
134 static tree dfs_modify_vtables (tree, void *);
135 static tree modify_all_vtables (tree, tree);
136 static void determine_primary_bases (tree);
137 static void finish_struct_methods (tree);
138 static void maybe_warn_about_overly_private_class (tree);
139 static int method_name_cmp (const void *, const void *);
140 static int resort_method_name_cmp (const void *, const void *);
141 static void add_implicitly_declared_members (tree, tree*, int, int);
142 static tree fixed_type_or_null (tree, int *, int *);
143 static tree build_simple_base_path (tree expr, tree binfo);
144 static tree build_vtbl_ref_1 (tree, tree);
145 static void build_vtbl_initializer (tree, tree, tree, tree, int *,
146 vec<constructor_elt, va_gc> **);
147 static int count_fields (tree);
148 static int add_fields_to_record_type (tree, struct sorted_fields_type*, int);
149 static void insert_into_classtype_sorted_fields (tree, tree, int);
150 static bool check_bitfield_decl (tree);
151 static void check_field_decl (tree, tree, int *, int *, int *);
152 static void check_field_decls (tree, tree *, int *, int *);
153 static tree *build_base_field (record_layout_info, tree, splay_tree, tree *);
154 static void build_base_fields (record_layout_info, splay_tree, tree *);
155 static void check_methods (tree);
156 static void remove_zero_width_bit_fields (tree);
157 static bool accessible_nvdtor_p (tree);
158 static void check_bases (tree, int *, int *);
159 static void check_bases_and_members (tree);
160 static tree create_vtable_ptr (tree, tree *);
161 static void include_empty_classes (record_layout_info);
162 static void layout_class_type (tree, tree *);
163 static void propagate_binfo_offsets (tree, tree);
164 static void layout_virtual_bases (record_layout_info, splay_tree);
165 static void build_vbase_offset_vtbl_entries (tree, vtbl_init_data *);
166 static void add_vcall_offset_vtbl_entries_r (tree, vtbl_init_data *);
167 static void add_vcall_offset_vtbl_entries_1 (tree, vtbl_init_data *);
168 static void build_vcall_offset_vtbl_entries (tree, vtbl_init_data *);
169 static void add_vcall_offset (tree, tree, vtbl_init_data *);
170 static void layout_vtable_decl (tree, int);
171 static tree dfs_find_final_overrider_pre (tree, void *);
172 static tree dfs_find_final_overrider_post (tree, void *);
173 static tree find_final_overrider (tree, tree, tree);
174 static int make_new_vtable (tree, tree);
175 static tree get_primary_binfo (tree);
176 static int maybe_indent_hierarchy (FILE *, int, int);
177 static tree dump_class_hierarchy_r (FILE *, int, tree, tree, int);
178 static void dump_class_hierarchy (tree);
179 static void dump_class_hierarchy_1 (FILE *, int, tree);
180 static void dump_array (FILE *, tree);
181 static void dump_vtable (tree, tree, tree);
182 static void dump_vtt (tree, tree);
183 static void dump_thunk (FILE *, int, tree);
184 static tree build_vtable (tree, tree, tree);
185 static void initialize_vtable (tree, vec<constructor_elt, va_gc> *);
186 static void layout_nonempty_base_or_field (record_layout_info,
187 tree, tree, splay_tree);
188 static tree end_of_class (tree, int);
189 static bool layout_empty_base (record_layout_info, tree, tree, splay_tree);
190 static void accumulate_vtbl_inits (tree, tree, tree, tree, tree,
191 vec<constructor_elt, va_gc> **);
192 static void dfs_accumulate_vtbl_inits (tree, tree, tree, tree, tree,
193 vec<constructor_elt, va_gc> **);
194 static void build_rtti_vtbl_entries (tree, vtbl_init_data *);
195 static void build_vcall_and_vbase_vtbl_entries (tree, vtbl_init_data *);
196 static void clone_constructors_and_destructors (tree);
197 static tree build_clone (tree, tree);
198 static void update_vtable_entry_for_fn (tree, tree, tree, tree *, unsigned);
199 static void build_ctor_vtbl_group (tree, tree);
200 static void build_vtt (tree);
201 static tree binfo_ctor_vtable (tree);
202 static void build_vtt_inits (tree, tree, vec<constructor_elt, va_gc> **,
203 tree *);
204 static tree dfs_build_secondary_vptr_vtt_inits (tree, void *);
205 static tree dfs_fixup_binfo_vtbls (tree, void *);
206 static int record_subobject_offset (tree, tree, splay_tree);
207 static int check_subobject_offset (tree, tree, splay_tree);
208 static int walk_subobject_offsets (tree, subobject_offset_fn,
209 tree, splay_tree, tree, int);
210 static void record_subobject_offsets (tree, tree, splay_tree, bool);
211 static int layout_conflict_p (tree, tree, splay_tree, int);
212 static int splay_tree_compare_integer_csts (splay_tree_key k1,
213 splay_tree_key k2);
214 static void warn_about_ambiguous_bases (tree);
215 static bool type_requires_array_cookie (tree);
216 static bool base_derived_from (tree, tree);
217 static int empty_base_at_nonzero_offset_p (tree, tree, splay_tree);
218 static tree end_of_base (tree);
219 static tree get_vcall_index (tree, tree);
220
221 /* Variables shared between class.c and call.c. */
222
223 int n_vtables = 0;
224 int n_vtable_entries = 0;
225 int n_vtable_searches = 0;
226 int n_vtable_elems = 0;
227 int n_convert_harshness = 0;
228 int n_compute_conversion_costs = 0;
229 int n_inner_fields_searched = 0;
230
231 /* Convert to or from a base subobject. EXPR is an expression of type
232 `A' or `A*', an expression of type `B' or `B*' is returned. To
233 convert A to a base B, CODE is PLUS_EXPR and BINFO is the binfo for
234 the B base instance within A. To convert base A to derived B, CODE
235 is MINUS_EXPR and BINFO is the binfo for the A instance within B.
236 In this latter case, A must not be a morally virtual base of B.
237 NONNULL is true if EXPR is known to be non-NULL (this is only
238 needed when EXPR is of pointer type). CV qualifiers are preserved
239 from EXPR. */
240
241 tree
242 build_base_path (enum tree_code code,
243 tree expr,
244 tree binfo,
245 int nonnull,
246 tsubst_flags_t complain)
247 {
248 tree v_binfo = NULL_TREE;
249 tree d_binfo = NULL_TREE;
250 tree probe;
251 tree offset;
252 tree target_type;
253 tree null_test = NULL;
254 tree ptr_target_type;
255 int fixed_type_p;
256 int want_pointer = TYPE_PTR_P (TREE_TYPE (expr));
257 bool has_empty = false;
258 bool virtual_access;
259 bool rvalue = false;
260
261 if (expr == error_mark_node || binfo == error_mark_node || !binfo)
262 return error_mark_node;
263
264 for (probe = binfo; probe; probe = BINFO_INHERITANCE_CHAIN (probe))
265 {
266 d_binfo = probe;
267 if (is_empty_class (BINFO_TYPE (probe)))
268 has_empty = true;
269 if (!v_binfo && BINFO_VIRTUAL_P (probe))
270 v_binfo = probe;
271 }
272
273 probe = TYPE_MAIN_VARIANT (TREE_TYPE (expr));
274 if (want_pointer)
275 probe = TYPE_MAIN_VARIANT (TREE_TYPE (probe));
276
277 if (code == PLUS_EXPR
278 && !SAME_BINFO_TYPE_P (BINFO_TYPE (d_binfo), probe))
279 {
280 /* This can happen when adjust_result_of_qualified_name_lookup can't
281 find a unique base binfo in a call to a member function. We
282 couldn't give the diagnostic then since we might have been calling
283 a static member function, so we do it now. */
284 if (complain & tf_error)
285 {
286 tree base = lookup_base (probe, BINFO_TYPE (d_binfo),
287 ba_unique, NULL, complain);
288 gcc_assert (base == error_mark_node);
289 }
290 return error_mark_node;
291 }
292
293 gcc_assert ((code == MINUS_EXPR
294 && SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), probe))
295 || code == PLUS_EXPR);
296
297 if (binfo == d_binfo)
298 /* Nothing to do. */
299 return expr;
300
301 if (code == MINUS_EXPR && v_binfo)
302 {
303 if (complain & tf_error)
304 {
305 if (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), BINFO_TYPE (v_binfo)))
306 {
307 if (want_pointer)
308 error ("cannot convert from pointer to base class %qT to "
309 "pointer to derived class %qT because the base is "
310 "virtual", BINFO_TYPE (binfo), BINFO_TYPE (d_binfo));
311 else
312 error ("cannot convert from base class %qT to derived "
313 "class %qT because the base is virtual",
314 BINFO_TYPE (binfo), BINFO_TYPE (d_binfo));
315 }
316 else
317 {
318 if (want_pointer)
319 error ("cannot convert from pointer to base class %qT to "
320 "pointer to derived class %qT via virtual base %qT",
321 BINFO_TYPE (binfo), BINFO_TYPE (d_binfo),
322 BINFO_TYPE (v_binfo));
323 else
324 error ("cannot convert from base class %qT to derived "
325 "class %qT via virtual base %qT", BINFO_TYPE (binfo),
326 BINFO_TYPE (d_binfo), BINFO_TYPE (v_binfo));
327 }
328 }
329 return error_mark_node;
330 }
331
332 if (!want_pointer)
333 {
334 rvalue = !real_lvalue_p (expr);
335 /* This must happen before the call to save_expr. */
336 expr = cp_build_addr_expr (expr, complain);
337 }
338 else
339 expr = mark_rvalue_use (expr);
340
341 offset = BINFO_OFFSET (binfo);
342 fixed_type_p = resolves_to_fixed_type_p (expr, &nonnull);
343 target_type = code == PLUS_EXPR ? BINFO_TYPE (binfo) : BINFO_TYPE (d_binfo);
344 /* TARGET_TYPE has been extracted from BINFO, and, is therefore always
345 cv-unqualified. Extract the cv-qualifiers from EXPR so that the
346 expression returned matches the input. */
347 target_type = cp_build_qualified_type
348 (target_type, cp_type_quals (TREE_TYPE (TREE_TYPE (expr))));
349 ptr_target_type = build_pointer_type (target_type);
350
351 /* Do we need to look in the vtable for the real offset? */
352 virtual_access = (v_binfo && fixed_type_p <= 0);
353
354 /* Don't bother with the calculations inside sizeof; they'll ICE if the
355 source type is incomplete and the pointer value doesn't matter. In a
356 template (even in instantiate_non_dependent_expr), we don't have vtables
357 set up properly yet, and the value doesn't matter there either; we're
358 just interested in the result of overload resolution. */
359 if (cp_unevaluated_operand != 0
360 || in_template_function ())
361 {
362 expr = build_nop (ptr_target_type, expr);
363 goto indout;
364 }
365
366 /* If we're in an NSDMI, we don't have the full constructor context yet
367 that we need for converting to a virtual base, so just build a stub
368 CONVERT_EXPR and expand it later in bot_replace. */
369 if (virtual_access && fixed_type_p < 0
370 && current_scope () != current_function_decl)
371 {
372 expr = build1 (CONVERT_EXPR, ptr_target_type, expr);
373 CONVERT_EXPR_VBASE_PATH (expr) = true;
374 goto indout;
375 }
376
377 /* Do we need to check for a null pointer? */
378 if (want_pointer && !nonnull)
379 {
380 /* If we know the conversion will not actually change the value
381 of EXPR, then we can avoid testing the expression for NULL.
382 We have to avoid generating a COMPONENT_REF for a base class
383 field, because other parts of the compiler know that such
384 expressions are always non-NULL. */
385 if (!virtual_access && integer_zerop (offset))
386 return build_nop (ptr_target_type, expr);
387 null_test = error_mark_node;
388 }
389
390 /* Protect against multiple evaluation if necessary. */
391 if (TREE_SIDE_EFFECTS (expr) && (null_test || virtual_access))
392 expr = save_expr (expr);
393
394 /* Now that we've saved expr, build the real null test. */
395 if (null_test)
396 {
397 tree zero = cp_convert (TREE_TYPE (expr), nullptr_node, complain);
398 null_test = fold_build2_loc (input_location, NE_EXPR, boolean_type_node,
399 expr, zero);
400 }
401
402 /* If this is a simple base reference, express it as a COMPONENT_REF. */
403 if (code == PLUS_EXPR && !virtual_access
404 /* We don't build base fields for empty bases, and they aren't very
405 interesting to the optimizers anyway. */
406 && !has_empty)
407 {
408 expr = cp_build_indirect_ref (expr, RO_NULL, complain);
409 expr = build_simple_base_path (expr, binfo);
410 if (rvalue)
411 expr = move (expr);
412 if (want_pointer)
413 expr = build_address (expr);
414 target_type = TREE_TYPE (expr);
415 goto out;
416 }
417
418 if (virtual_access)
419 {
420 /* Going via virtual base V_BINFO. We need the static offset
421 from V_BINFO to BINFO, and the dynamic offset from D_BINFO to
422 V_BINFO. That offset is an entry in D_BINFO's vtable. */
423 tree v_offset;
424
425 if (fixed_type_p < 0 && in_base_initializer)
426 {
427 /* In a base member initializer, we cannot rely on the
428 vtable being set up. We have to indirect via the
429 vtt_parm. */
430 tree t;
431
432 t = TREE_TYPE (TYPE_VFIELD (current_class_type));
433 t = build_pointer_type (t);
434 v_offset = convert (t, current_vtt_parm);
435 v_offset = cp_build_indirect_ref (v_offset, RO_NULL, complain);
436 }
437 else
438 {
439 tree t = expr;
440 if ((flag_sanitize & SANITIZE_VPTR) && fixed_type_p == 0)
441 {
442 t = cp_ubsan_maybe_instrument_cast_to_vbase (input_location,
443 probe, expr);
444 if (t == NULL_TREE)
445 t = expr;
446 }
447 v_offset = build_vfield_ref (cp_build_indirect_ref (t, RO_NULL,
448 complain),
449 TREE_TYPE (TREE_TYPE (expr)));
450 }
451
452 if (v_offset == error_mark_node)
453 return error_mark_node;
454
455 v_offset = fold_build_pointer_plus (v_offset, BINFO_VPTR_FIELD (v_binfo));
456 v_offset = build1 (NOP_EXPR,
457 build_pointer_type (ptrdiff_type_node),
458 v_offset);
459 v_offset = cp_build_indirect_ref (v_offset, RO_NULL, complain);
460 TREE_CONSTANT (v_offset) = 1;
461
462 offset = convert_to_integer (ptrdiff_type_node,
463 size_diffop_loc (input_location, offset,
464 BINFO_OFFSET (v_binfo)));
465
466 if (!integer_zerop (offset))
467 v_offset = build2 (code, ptrdiff_type_node, v_offset, offset);
468
469 if (fixed_type_p < 0)
470 /* Negative fixed_type_p means this is a constructor or destructor;
471 virtual base layout is fixed in in-charge [cd]tors, but not in
472 base [cd]tors. */
473 offset = build3 (COND_EXPR, ptrdiff_type_node,
474 build2 (EQ_EXPR, boolean_type_node,
475 current_in_charge_parm, integer_zero_node),
476 v_offset,
477 convert_to_integer (ptrdiff_type_node,
478 BINFO_OFFSET (binfo)));
479 else
480 offset = v_offset;
481 }
482
483 if (want_pointer)
484 target_type = ptr_target_type;
485
486 expr = build1 (NOP_EXPR, ptr_target_type, expr);
487
488 if (!integer_zerop (offset))
489 {
490 offset = fold_convert (sizetype, offset);
491 if (code == MINUS_EXPR)
492 offset = fold_build1_loc (input_location, NEGATE_EXPR, sizetype, offset);
493 expr = fold_build_pointer_plus (expr, offset);
494 }
495 else
496 null_test = NULL;
497
498 indout:
499 if (!want_pointer)
500 {
501 expr = cp_build_indirect_ref (expr, RO_NULL, complain);
502 if (rvalue)
503 expr = move (expr);
504 }
505
506 out:
507 if (null_test)
508 expr = fold_build3_loc (input_location, COND_EXPR, target_type, null_test, expr,
509 build_zero_cst (target_type));
510
511 return expr;
512 }
513
514 /* Subroutine of build_base_path; EXPR and BINFO are as in that function.
515 Perform a derived-to-base conversion by recursively building up a
516 sequence of COMPONENT_REFs to the appropriate base fields. */
517
518 static tree
519 build_simple_base_path (tree expr, tree binfo)
520 {
521 tree type = BINFO_TYPE (binfo);
522 tree d_binfo = BINFO_INHERITANCE_CHAIN (binfo);
523 tree field;
524
525 if (d_binfo == NULL_TREE)
526 {
527 tree temp;
528
529 gcc_assert (TYPE_MAIN_VARIANT (TREE_TYPE (expr)) == type);
530
531 /* Transform `(a, b).x' into `(*(a, &b)).x', `(a ? b : c).x'
532 into `(*(a ? &b : &c)).x', and so on. A COND_EXPR is only
533 an lvalue in the front end; only _DECLs and _REFs are lvalues
534 in the back end. */
535 temp = unary_complex_lvalue (ADDR_EXPR, expr);
536 if (temp)
537 expr = cp_build_indirect_ref (temp, RO_NULL, tf_warning_or_error);
538
539 return expr;
540 }
541
542 /* Recurse. */
543 expr = build_simple_base_path (expr, d_binfo);
544
545 for (field = TYPE_FIELDS (BINFO_TYPE (d_binfo));
546 field; field = DECL_CHAIN (field))
547 /* Is this the base field created by build_base_field? */
548 if (TREE_CODE (field) == FIELD_DECL
549 && DECL_FIELD_IS_BASE (field)
550 && TREE_TYPE (field) == type
551 /* If we're looking for a field in the most-derived class,
552 also check the field offset; we can have two base fields
553 of the same type if one is an indirect virtual base and one
554 is a direct non-virtual base. */
555 && (BINFO_INHERITANCE_CHAIN (d_binfo)
556 || tree_int_cst_equal (byte_position (field),
557 BINFO_OFFSET (binfo))))
558 {
559 /* We don't use build_class_member_access_expr here, as that
560 has unnecessary checks, and more importantly results in
561 recursive calls to dfs_walk_once. */
562 int type_quals = cp_type_quals (TREE_TYPE (expr));
563
564 expr = build3 (COMPONENT_REF,
565 cp_build_qualified_type (type, type_quals),
566 expr, field, NULL_TREE);
567 expr = fold_if_not_in_template (expr);
568
569 /* Mark the expression const or volatile, as appropriate.
570 Even though we've dealt with the type above, we still have
571 to mark the expression itself. */
572 if (type_quals & TYPE_QUAL_CONST)
573 TREE_READONLY (expr) = 1;
574 if (type_quals & TYPE_QUAL_VOLATILE)
575 TREE_THIS_VOLATILE (expr) = 1;
576
577 return expr;
578 }
579
580 /* Didn't find the base field?!? */
581 gcc_unreachable ();
582 }
583
584 /* Convert OBJECT to the base TYPE. OBJECT is an expression whose
585 type is a class type or a pointer to a class type. In the former
586 case, TYPE is also a class type; in the latter it is another
587 pointer type. If CHECK_ACCESS is true, an error message is emitted
588 if TYPE is inaccessible. If OBJECT has pointer type, the value is
589 assumed to be non-NULL. */
590
591 tree
592 convert_to_base (tree object, tree type, bool check_access, bool nonnull,
593 tsubst_flags_t complain)
594 {
595 tree binfo;
596 tree object_type;
597
598 if (TYPE_PTR_P (TREE_TYPE (object)))
599 {
600 object_type = TREE_TYPE (TREE_TYPE (object));
601 type = TREE_TYPE (type);
602 }
603 else
604 object_type = TREE_TYPE (object);
605
606 binfo = lookup_base (object_type, type, check_access ? ba_check : ba_unique,
607 NULL, complain);
608 if (!binfo || binfo == error_mark_node)
609 return error_mark_node;
610
611 return build_base_path (PLUS_EXPR, object, binfo, nonnull, complain);
612 }
613
614 /* EXPR is an expression with unqualified class type. BASE is a base
615 binfo of that class type. Returns EXPR, converted to the BASE
616 type. This function assumes that EXPR is the most derived class;
617 therefore virtual bases can be found at their static offsets. */
618
619 tree
620 convert_to_base_statically (tree expr, tree base)
621 {
622 tree expr_type;
623
624 expr_type = TREE_TYPE (expr);
625 if (!SAME_BINFO_TYPE_P (BINFO_TYPE (base), expr_type))
626 {
627 /* If this is a non-empty base, use a COMPONENT_REF. */
628 if (!is_empty_class (BINFO_TYPE (base)))
629 return build_simple_base_path (expr, base);
630
631 /* We use fold_build2 and fold_convert below to simplify the trees
632 provided to the optimizers. It is not safe to call these functions
633 when processing a template because they do not handle C++-specific
634 trees. */
635 gcc_assert (!processing_template_decl);
636 expr = cp_build_addr_expr (expr, tf_warning_or_error);
637 if (!integer_zerop (BINFO_OFFSET (base)))
638 expr = fold_build_pointer_plus_loc (input_location,
639 expr, BINFO_OFFSET (base));
640 expr = fold_convert (build_pointer_type (BINFO_TYPE (base)), expr);
641 expr = build_fold_indirect_ref_loc (input_location, expr);
642 }
643
644 return expr;
645 }
646
647 \f
648 tree
649 build_vfield_ref (tree datum, tree type)
650 {
651 tree vfield, vcontext;
652
653 if (datum == error_mark_node
654 /* Can happen in case of duplicate base types (c++/59082). */
655 || !TYPE_VFIELD (type))
656 return error_mark_node;
657
658 /* First, convert to the requested type. */
659 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (datum), type))
660 datum = convert_to_base (datum, type, /*check_access=*/false,
661 /*nonnull=*/true, tf_warning_or_error);
662
663 /* Second, the requested type may not be the owner of its own vptr.
664 If not, convert to the base class that owns it. We cannot use
665 convert_to_base here, because VCONTEXT may appear more than once
666 in the inheritance hierarchy of TYPE, and thus direct conversion
667 between the types may be ambiguous. Following the path back up
668 one step at a time via primary bases avoids the problem. */
669 vfield = TYPE_VFIELD (type);
670 vcontext = DECL_CONTEXT (vfield);
671 while (!same_type_ignoring_top_level_qualifiers_p (vcontext, type))
672 {
673 datum = build_simple_base_path (datum, CLASSTYPE_PRIMARY_BINFO (type));
674 type = TREE_TYPE (datum);
675 }
676
677 return build3 (COMPONENT_REF, TREE_TYPE (vfield), datum, vfield, NULL_TREE);
678 }
679
680 /* Given an object INSTANCE, return an expression which yields the
681 vtable element corresponding to INDEX. There are many special
682 cases for INSTANCE which we take care of here, mainly to avoid
683 creating extra tree nodes when we don't have to. */
684
685 static tree
686 build_vtbl_ref_1 (tree instance, tree idx)
687 {
688 tree aref;
689 tree vtbl = NULL_TREE;
690
691 /* Try to figure out what a reference refers to, and
692 access its virtual function table directly. */
693
694 int cdtorp = 0;
695 tree fixed_type = fixed_type_or_null (instance, NULL, &cdtorp);
696
697 tree basetype = non_reference (TREE_TYPE (instance));
698
699 if (fixed_type && !cdtorp)
700 {
701 tree binfo = lookup_base (fixed_type, basetype,
702 ba_unique, NULL, tf_none);
703 if (binfo && binfo != error_mark_node)
704 vtbl = unshare_expr (BINFO_VTABLE (binfo));
705 }
706
707 if (!vtbl)
708 vtbl = build_vfield_ref (instance, basetype);
709
710 aref = build_array_ref (input_location, vtbl, idx);
711 TREE_CONSTANT (aref) |= TREE_CONSTANT (vtbl) && TREE_CONSTANT (idx);
712
713 return aref;
714 }
715
716 tree
717 build_vtbl_ref (tree instance, tree idx)
718 {
719 tree aref = build_vtbl_ref_1 (instance, idx);
720
721 return aref;
722 }
723
724 /* Given a stable object pointer INSTANCE_PTR, return an expression which
725 yields a function pointer corresponding to vtable element INDEX. */
726
727 tree
728 build_vfn_ref (tree instance_ptr, tree idx)
729 {
730 tree aref;
731
732 aref = build_vtbl_ref_1 (cp_build_indirect_ref (instance_ptr, RO_NULL,
733 tf_warning_or_error),
734 idx);
735
736 /* When using function descriptors, the address of the
737 vtable entry is treated as a function pointer. */
738 if (TARGET_VTABLE_USES_DESCRIPTORS)
739 aref = build1 (NOP_EXPR, TREE_TYPE (aref),
740 cp_build_addr_expr (aref, tf_warning_or_error));
741
742 /* Remember this as a method reference, for later devirtualization. */
743 aref = build3 (OBJ_TYPE_REF, TREE_TYPE (aref), aref, instance_ptr, idx);
744
745 return aref;
746 }
747
748 /* Return the name of the virtual function table (as an IDENTIFIER_NODE)
749 for the given TYPE. */
750
751 static tree
752 get_vtable_name (tree type)
753 {
754 return mangle_vtbl_for_type (type);
755 }
756
757 /* DECL is an entity associated with TYPE, like a virtual table or an
758 implicitly generated constructor. Determine whether or not DECL
759 should have external or internal linkage at the object file
760 level. This routine does not deal with COMDAT linkage and other
761 similar complexities; it simply sets TREE_PUBLIC if it possible for
762 entities in other translation units to contain copies of DECL, in
763 the abstract. */
764
765 void
766 set_linkage_according_to_type (tree /*type*/, tree decl)
767 {
768 TREE_PUBLIC (decl) = 1;
769 determine_visibility (decl);
770 }
771
772 /* Create a VAR_DECL for a primary or secondary vtable for CLASS_TYPE.
773 (For a secondary vtable for B-in-D, CLASS_TYPE should be D, not B.)
774 Use NAME for the name of the vtable, and VTABLE_TYPE for its type. */
775
776 static tree
777 build_vtable (tree class_type, tree name, tree vtable_type)
778 {
779 tree decl;
780
781 decl = build_lang_decl (VAR_DECL, name, vtable_type);
782 /* vtable names are already mangled; give them their DECL_ASSEMBLER_NAME
783 now to avoid confusion in mangle_decl. */
784 SET_DECL_ASSEMBLER_NAME (decl, name);
785 DECL_CONTEXT (decl) = class_type;
786 DECL_ARTIFICIAL (decl) = 1;
787 TREE_STATIC (decl) = 1;
788 TREE_READONLY (decl) = 1;
789 DECL_VIRTUAL_P (decl) = 1;
790 DECL_ALIGN (decl) = TARGET_VTABLE_ENTRY_ALIGN;
791 DECL_USER_ALIGN (decl) = true;
792 DECL_VTABLE_OR_VTT_P (decl) = 1;
793 set_linkage_according_to_type (class_type, decl);
794 /* The vtable has not been defined -- yet. */
795 DECL_EXTERNAL (decl) = 1;
796 DECL_NOT_REALLY_EXTERN (decl) = 1;
797
798 /* Mark the VAR_DECL node representing the vtable itself as a
799 "gratuitous" one, thereby forcing dwarfout.c to ignore it. It
800 is rather important that such things be ignored because any
801 effort to actually generate DWARF for them will run into
802 trouble when/if we encounter code like:
803
804 #pragma interface
805 struct S { virtual void member (); };
806
807 because the artificial declaration of the vtable itself (as
808 manufactured by the g++ front end) will say that the vtable is
809 a static member of `S' but only *after* the debug output for
810 the definition of `S' has already been output. This causes
811 grief because the DWARF entry for the definition of the vtable
812 will try to refer back to an earlier *declaration* of the
813 vtable as a static member of `S' and there won't be one. We
814 might be able to arrange to have the "vtable static member"
815 attached to the member list for `S' before the debug info for
816 `S' get written (which would solve the problem) but that would
817 require more intrusive changes to the g++ front end. */
818 DECL_IGNORED_P (decl) = 1;
819
820 return decl;
821 }
822
823 /* Get the VAR_DECL of the vtable for TYPE. TYPE need not be polymorphic,
824 or even complete. If this does not exist, create it. If COMPLETE is
825 nonzero, then complete the definition of it -- that will render it
826 impossible to actually build the vtable, but is useful to get at those
827 which are known to exist in the runtime. */
828
829 tree
830 get_vtable_decl (tree type, int complete)
831 {
832 tree decl;
833
834 if (CLASSTYPE_VTABLES (type))
835 return CLASSTYPE_VTABLES (type);
836
837 decl = build_vtable (type, get_vtable_name (type), vtbl_type_node);
838 CLASSTYPE_VTABLES (type) = decl;
839
840 if (complete)
841 {
842 DECL_EXTERNAL (decl) = 1;
843 cp_finish_decl (decl, NULL_TREE, false, NULL_TREE, 0);
844 }
845
846 return decl;
847 }
848
849 /* Build the primary virtual function table for TYPE. If BINFO is
850 non-NULL, build the vtable starting with the initial approximation
851 that it is the same as the one which is the head of the association
852 list. Returns a nonzero value if a new vtable is actually
853 created. */
854
855 static int
856 build_primary_vtable (tree binfo, tree type)
857 {
858 tree decl;
859 tree virtuals;
860
861 decl = get_vtable_decl (type, /*complete=*/0);
862
863 if (binfo)
864 {
865 if (BINFO_NEW_VTABLE_MARKED (binfo))
866 /* We have already created a vtable for this base, so there's
867 no need to do it again. */
868 return 0;
869
870 virtuals = copy_list (BINFO_VIRTUALS (binfo));
871 TREE_TYPE (decl) = TREE_TYPE (get_vtbl_decl_for_binfo (binfo));
872 DECL_SIZE (decl) = TYPE_SIZE (TREE_TYPE (decl));
873 DECL_SIZE_UNIT (decl) = TYPE_SIZE_UNIT (TREE_TYPE (decl));
874 }
875 else
876 {
877 gcc_assert (TREE_TYPE (decl) == vtbl_type_node);
878 virtuals = NULL_TREE;
879 }
880
881 if (GATHER_STATISTICS)
882 {
883 n_vtables += 1;
884 n_vtable_elems += list_length (virtuals);
885 }
886
887 /* Initialize the association list for this type, based
888 on our first approximation. */
889 BINFO_VTABLE (TYPE_BINFO (type)) = decl;
890 BINFO_VIRTUALS (TYPE_BINFO (type)) = virtuals;
891 SET_BINFO_NEW_VTABLE_MARKED (TYPE_BINFO (type));
892 return 1;
893 }
894
895 /* Give BINFO a new virtual function table which is initialized
896 with a skeleton-copy of its original initialization. The only
897 entry that changes is the `delta' entry, so we can really
898 share a lot of structure.
899
900 FOR_TYPE is the most derived type which caused this table to
901 be needed.
902
903 Returns nonzero if we haven't met BINFO before.
904
905 The order in which vtables are built (by calling this function) for
906 an object must remain the same, otherwise a binary incompatibility
907 can result. */
908
909 static int
910 build_secondary_vtable (tree binfo)
911 {
912 if (BINFO_NEW_VTABLE_MARKED (binfo))
913 /* We already created a vtable for this base. There's no need to
914 do it again. */
915 return 0;
916
917 /* Remember that we've created a vtable for this BINFO, so that we
918 don't try to do so again. */
919 SET_BINFO_NEW_VTABLE_MARKED (binfo);
920
921 /* Make fresh virtual list, so we can smash it later. */
922 BINFO_VIRTUALS (binfo) = copy_list (BINFO_VIRTUALS (binfo));
923
924 /* Secondary vtables are laid out as part of the same structure as
925 the primary vtable. */
926 BINFO_VTABLE (binfo) = NULL_TREE;
927 return 1;
928 }
929
930 /* Create a new vtable for BINFO which is the hierarchy dominated by
931 T. Return nonzero if we actually created a new vtable. */
932
933 static int
934 make_new_vtable (tree t, tree binfo)
935 {
936 if (binfo == TYPE_BINFO (t))
937 /* In this case, it is *type*'s vtable we are modifying. We start
938 with the approximation that its vtable is that of the
939 immediate base class. */
940 return build_primary_vtable (binfo, t);
941 else
942 /* This is our very own copy of `basetype' to play with. Later,
943 we will fill in all the virtual functions that override the
944 virtual functions in these base classes which are not defined
945 by the current type. */
946 return build_secondary_vtable (binfo);
947 }
948
949 /* Make *VIRTUALS, an entry on the BINFO_VIRTUALS list for BINFO
950 (which is in the hierarchy dominated by T) list FNDECL as its
951 BV_FN. DELTA is the required constant adjustment from the `this'
952 pointer where the vtable entry appears to the `this' required when
953 the function is actually called. */
954
955 static void
956 modify_vtable_entry (tree t,
957 tree binfo,
958 tree fndecl,
959 tree delta,
960 tree *virtuals)
961 {
962 tree v;
963
964 v = *virtuals;
965
966 if (fndecl != BV_FN (v)
967 || !tree_int_cst_equal (delta, BV_DELTA (v)))
968 {
969 /* We need a new vtable for BINFO. */
970 if (make_new_vtable (t, binfo))
971 {
972 /* If we really did make a new vtable, we also made a copy
973 of the BINFO_VIRTUALS list. Now, we have to find the
974 corresponding entry in that list. */
975 *virtuals = BINFO_VIRTUALS (binfo);
976 while (BV_FN (*virtuals) != BV_FN (v))
977 *virtuals = TREE_CHAIN (*virtuals);
978 v = *virtuals;
979 }
980
981 BV_DELTA (v) = delta;
982 BV_VCALL_INDEX (v) = NULL_TREE;
983 BV_FN (v) = fndecl;
984 }
985 }
986
987 \f
988 /* Add method METHOD to class TYPE. If USING_DECL is non-null, it is
989 the USING_DECL naming METHOD. Returns true if the method could be
990 added to the method vec. */
991
992 bool
993 add_method (tree type, tree method, tree using_decl)
994 {
995 unsigned slot;
996 tree overload;
997 bool template_conv_p = false;
998 bool conv_p;
999 vec<tree, va_gc> *method_vec;
1000 bool complete_p;
1001 bool insert_p = false;
1002 tree current_fns;
1003 tree fns;
1004
1005 if (method == error_mark_node)
1006 return false;
1007
1008 complete_p = COMPLETE_TYPE_P (type);
1009 conv_p = DECL_CONV_FN_P (method);
1010 if (conv_p)
1011 template_conv_p = (TREE_CODE (method) == TEMPLATE_DECL
1012 && DECL_TEMPLATE_CONV_FN_P (method));
1013
1014 method_vec = CLASSTYPE_METHOD_VEC (type);
1015 if (!method_vec)
1016 {
1017 /* Make a new method vector. We start with 8 entries. We must
1018 allocate at least two (for constructors and destructors), and
1019 we're going to end up with an assignment operator at some
1020 point as well. */
1021 vec_alloc (method_vec, 8);
1022 /* Create slots for constructors and destructors. */
1023 method_vec->quick_push (NULL_TREE);
1024 method_vec->quick_push (NULL_TREE);
1025 CLASSTYPE_METHOD_VEC (type) = method_vec;
1026 }
1027
1028 /* Maintain TYPE_HAS_USER_CONSTRUCTOR, etc. */
1029 grok_special_member_properties (method);
1030
1031 /* Constructors and destructors go in special slots. */
1032 if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (method))
1033 slot = CLASSTYPE_CONSTRUCTOR_SLOT;
1034 else if (DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (method))
1035 {
1036 slot = CLASSTYPE_DESTRUCTOR_SLOT;
1037
1038 if (TYPE_FOR_JAVA (type))
1039 {
1040 if (!DECL_ARTIFICIAL (method))
1041 error ("Java class %qT cannot have a destructor", type);
1042 else if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
1043 error ("Java class %qT cannot have an implicit non-trivial "
1044 "destructor",
1045 type);
1046 }
1047 }
1048 else
1049 {
1050 tree m;
1051
1052 insert_p = true;
1053 /* See if we already have an entry with this name. */
1054 for (slot = CLASSTYPE_FIRST_CONVERSION_SLOT;
1055 vec_safe_iterate (method_vec, slot, &m);
1056 ++slot)
1057 {
1058 m = OVL_CURRENT (m);
1059 if (template_conv_p)
1060 {
1061 if (TREE_CODE (m) == TEMPLATE_DECL
1062 && DECL_TEMPLATE_CONV_FN_P (m))
1063 insert_p = false;
1064 break;
1065 }
1066 if (conv_p && !DECL_CONV_FN_P (m))
1067 break;
1068 if (DECL_NAME (m) == DECL_NAME (method))
1069 {
1070 insert_p = false;
1071 break;
1072 }
1073 if (complete_p
1074 && !DECL_CONV_FN_P (m)
1075 && DECL_NAME (m) > DECL_NAME (method))
1076 break;
1077 }
1078 }
1079 current_fns = insert_p ? NULL_TREE : (*method_vec)[slot];
1080
1081 /* Check to see if we've already got this method. */
1082 for (fns = current_fns; fns; fns = OVL_NEXT (fns))
1083 {
1084 tree fn = OVL_CURRENT (fns);
1085 tree fn_type;
1086 tree method_type;
1087 tree parms1;
1088 tree parms2;
1089
1090 if (TREE_CODE (fn) != TREE_CODE (method))
1091 continue;
1092
1093 /* [over.load] Member function declarations with the
1094 same name and the same parameter types cannot be
1095 overloaded if any of them is a static member
1096 function declaration.
1097
1098 [over.load] Member function declarations with the same name and
1099 the same parameter-type-list as well as member function template
1100 declarations with the same name, the same parameter-type-list, and
1101 the same template parameter lists cannot be overloaded if any of
1102 them, but not all, have a ref-qualifier.
1103
1104 [namespace.udecl] When a using-declaration brings names
1105 from a base class into a derived class scope, member
1106 functions in the derived class override and/or hide member
1107 functions with the same name and parameter types in a base
1108 class (rather than conflicting). */
1109 fn_type = TREE_TYPE (fn);
1110 method_type = TREE_TYPE (method);
1111 parms1 = TYPE_ARG_TYPES (fn_type);
1112 parms2 = TYPE_ARG_TYPES (method_type);
1113
1114 /* Compare the quals on the 'this' parm. Don't compare
1115 the whole types, as used functions are treated as
1116 coming from the using class in overload resolution. */
1117 if (! DECL_STATIC_FUNCTION_P (fn)
1118 && ! DECL_STATIC_FUNCTION_P (method)
1119 /* Either both or neither need to be ref-qualified for
1120 differing quals to allow overloading. */
1121 && (FUNCTION_REF_QUALIFIED (fn_type)
1122 == FUNCTION_REF_QUALIFIED (method_type))
1123 && (type_memfn_quals (fn_type) != type_memfn_quals (method_type)
1124 || type_memfn_rqual (fn_type) != type_memfn_rqual (method_type)))
1125 continue;
1126
1127 /* For templates, the return type and template parameters
1128 must be identical. */
1129 if (TREE_CODE (fn) == TEMPLATE_DECL
1130 && (!same_type_p (TREE_TYPE (fn_type),
1131 TREE_TYPE (method_type))
1132 || !comp_template_parms (DECL_TEMPLATE_PARMS (fn),
1133 DECL_TEMPLATE_PARMS (method))))
1134 continue;
1135
1136 if (! DECL_STATIC_FUNCTION_P (fn))
1137 parms1 = TREE_CHAIN (parms1);
1138 if (! DECL_STATIC_FUNCTION_P (method))
1139 parms2 = TREE_CHAIN (parms2);
1140
1141 if (compparms (parms1, parms2)
1142 && (!DECL_CONV_FN_P (fn)
1143 || same_type_p (TREE_TYPE (fn_type),
1144 TREE_TYPE (method_type))))
1145 {
1146 /* For function versions, their parms and types match
1147 but they are not duplicates. Record function versions
1148 as and when they are found. extern "C" functions are
1149 not treated as versions. */
1150 if (TREE_CODE (fn) == FUNCTION_DECL
1151 && TREE_CODE (method) == FUNCTION_DECL
1152 && !DECL_EXTERN_C_P (fn)
1153 && !DECL_EXTERN_C_P (method)
1154 && targetm.target_option.function_versions (fn, method))
1155 {
1156 /* Mark functions as versions if necessary. Modify the mangled
1157 decl name if necessary. */
1158 if (!DECL_FUNCTION_VERSIONED (fn))
1159 {
1160 DECL_FUNCTION_VERSIONED (fn) = 1;
1161 if (DECL_ASSEMBLER_NAME_SET_P (fn))
1162 mangle_decl (fn);
1163 }
1164 if (!DECL_FUNCTION_VERSIONED (method))
1165 {
1166 DECL_FUNCTION_VERSIONED (method) = 1;
1167 if (DECL_ASSEMBLER_NAME_SET_P (method))
1168 mangle_decl (method);
1169 }
1170 cgraph_node::record_function_versions (fn, method);
1171 continue;
1172 }
1173 if (DECL_INHERITED_CTOR_BASE (method))
1174 {
1175 if (DECL_INHERITED_CTOR_BASE (fn))
1176 {
1177 error_at (DECL_SOURCE_LOCATION (method),
1178 "%q#D inherited from %qT", method,
1179 DECL_INHERITED_CTOR_BASE (method));
1180 error_at (DECL_SOURCE_LOCATION (fn),
1181 "conflicts with version inherited from %qT",
1182 DECL_INHERITED_CTOR_BASE (fn));
1183 }
1184 /* Otherwise defer to the other function. */
1185 return false;
1186 }
1187 if (using_decl)
1188 {
1189 if (DECL_CONTEXT (fn) == type)
1190 /* Defer to the local function. */
1191 return false;
1192 }
1193 else
1194 {
1195 error ("%q+#D cannot be overloaded", method);
1196 error ("with %q+#D", fn);
1197 }
1198
1199 /* We don't call duplicate_decls here to merge the
1200 declarations because that will confuse things if the
1201 methods have inline definitions. In particular, we
1202 will crash while processing the definitions. */
1203 return false;
1204 }
1205 }
1206
1207 /* A class should never have more than one destructor. */
1208 if (current_fns && DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (method))
1209 return false;
1210
1211 /* Add the new binding. */
1212 if (using_decl)
1213 {
1214 overload = ovl_cons (method, current_fns);
1215 OVL_USED (overload) = true;
1216 }
1217 else
1218 overload = build_overload (method, current_fns);
1219
1220 if (conv_p)
1221 TYPE_HAS_CONVERSION (type) = 1;
1222 else if (slot >= CLASSTYPE_FIRST_CONVERSION_SLOT && !complete_p)
1223 push_class_level_binding (DECL_NAME (method), overload);
1224
1225 if (insert_p)
1226 {
1227 bool reallocated;
1228
1229 /* We only expect to add few methods in the COMPLETE_P case, so
1230 just make room for one more method in that case. */
1231 if (complete_p)
1232 reallocated = vec_safe_reserve_exact (method_vec, 1);
1233 else
1234 reallocated = vec_safe_reserve (method_vec, 1);
1235 if (reallocated)
1236 CLASSTYPE_METHOD_VEC (type) = method_vec;
1237 if (slot == method_vec->length ())
1238 method_vec->quick_push (overload);
1239 else
1240 method_vec->quick_insert (slot, overload);
1241 }
1242 else
1243 /* Replace the current slot. */
1244 (*method_vec)[slot] = overload;
1245 return true;
1246 }
1247
1248 /* Subroutines of finish_struct. */
1249
1250 /* Change the access of FDECL to ACCESS in T. Return 1 if change was
1251 legit, otherwise return 0. */
1252
1253 static int
1254 alter_access (tree t, tree fdecl, tree access)
1255 {
1256 tree elem;
1257
1258 if (!DECL_LANG_SPECIFIC (fdecl))
1259 retrofit_lang_decl (fdecl);
1260
1261 gcc_assert (!DECL_DISCRIMINATOR_P (fdecl));
1262
1263 elem = purpose_member (t, DECL_ACCESS (fdecl));
1264 if (elem)
1265 {
1266 if (TREE_VALUE (elem) != access)
1267 {
1268 if (TREE_CODE (TREE_TYPE (fdecl)) == FUNCTION_DECL)
1269 error ("conflicting access specifications for method"
1270 " %q+D, ignored", TREE_TYPE (fdecl));
1271 else
1272 error ("conflicting access specifications for field %qE, ignored",
1273 DECL_NAME (fdecl));
1274 }
1275 else
1276 {
1277 /* They're changing the access to the same thing they changed
1278 it to before. That's OK. */
1279 ;
1280 }
1281 }
1282 else
1283 {
1284 perform_or_defer_access_check (TYPE_BINFO (t), fdecl, fdecl,
1285 tf_warning_or_error);
1286 DECL_ACCESS (fdecl) = tree_cons (t, access, DECL_ACCESS (fdecl));
1287 return 1;
1288 }
1289 return 0;
1290 }
1291
1292 /* Process the USING_DECL, which is a member of T. */
1293
1294 static void
1295 handle_using_decl (tree using_decl, tree t)
1296 {
1297 tree decl = USING_DECL_DECLS (using_decl);
1298 tree name = DECL_NAME (using_decl);
1299 tree access
1300 = TREE_PRIVATE (using_decl) ? access_private_node
1301 : TREE_PROTECTED (using_decl) ? access_protected_node
1302 : access_public_node;
1303 tree flist = NULL_TREE;
1304 tree old_value;
1305
1306 gcc_assert (!processing_template_decl && decl);
1307
1308 old_value = lookup_member (t, name, /*protect=*/0, /*want_type=*/false,
1309 tf_warning_or_error);
1310 if (old_value)
1311 {
1312 if (is_overloaded_fn (old_value))
1313 old_value = OVL_CURRENT (old_value);
1314
1315 if (DECL_P (old_value) && DECL_CONTEXT (old_value) == t)
1316 /* OK */;
1317 else
1318 old_value = NULL_TREE;
1319 }
1320
1321 cp_emit_debug_info_for_using (decl, t);
1322
1323 if (is_overloaded_fn (decl))
1324 flist = decl;
1325
1326 if (! old_value)
1327 ;
1328 else if (is_overloaded_fn (old_value))
1329 {
1330 if (flist)
1331 /* It's OK to use functions from a base when there are functions with
1332 the same name already present in the current class. */;
1333 else
1334 {
1335 error ("%q+D invalid in %q#T", using_decl, t);
1336 error (" because of local method %q+#D with same name",
1337 OVL_CURRENT (old_value));
1338 return;
1339 }
1340 }
1341 else if (!DECL_ARTIFICIAL (old_value))
1342 {
1343 error ("%q+D invalid in %q#T", using_decl, t);
1344 error (" because of local member %q+#D with same name", old_value);
1345 return;
1346 }
1347
1348 /* Make type T see field decl FDECL with access ACCESS. */
1349 if (flist)
1350 for (; flist; flist = OVL_NEXT (flist))
1351 {
1352 add_method (t, OVL_CURRENT (flist), using_decl);
1353 alter_access (t, OVL_CURRENT (flist), access);
1354 }
1355 else
1356 alter_access (t, decl, access);
1357 }
1358 \f
1359 /* Data structure for find_abi_tags_r, below. */
1360
1361 struct abi_tag_data
1362 {
1363 tree t; // The type that we're checking for missing tags.
1364 tree subob; // The subobject of T that we're getting tags from.
1365 tree tags; // error_mark_node for diagnostics, or a list of missing tags.
1366 };
1367
1368 /* Subroutine of find_abi_tags_r. Handle a single TAG found on the class TP
1369 in the context of P. TAG can be either an identifier (the DECL_NAME of
1370 a tag NAMESPACE_DECL) or a STRING_CST (a tag attribute). */
1371
1372 static void
1373 check_tag (tree tag, tree id, tree *tp, abi_tag_data *p)
1374 {
1375 if (!IDENTIFIER_MARKED (id))
1376 {
1377 if (p->tags != error_mark_node)
1378 {
1379 /* We're collecting tags from template arguments or from
1380 the type of a variable or function return type. */
1381 p->tags = tree_cons (NULL_TREE, tag, p->tags);
1382
1383 /* Don't inherit this tag multiple times. */
1384 IDENTIFIER_MARKED (id) = true;
1385
1386 if (TYPE_P (p->t))
1387 {
1388 /* Tags inherited from type template arguments are only used
1389 to avoid warnings. */
1390 ABI_TAG_IMPLICIT (p->tags) = true;
1391 return;
1392 }
1393 /* For functions and variables we want to warn, too. */
1394 }
1395
1396 /* Otherwise we're diagnosing missing tags. */
1397 if (TREE_CODE (p->t) == FUNCTION_DECL)
1398 {
1399 if (warning (OPT_Wabi_tag, "%qD inherits the %E ABI tag "
1400 "that %qT (used in its return type) has",
1401 p->t, tag, *tp))
1402 inform (location_of (*tp), "%qT declared here", *tp);
1403 }
1404 else if (TREE_CODE (p->t) == VAR_DECL)
1405 {
1406 if (warning (OPT_Wabi_tag, "%qD inherits the %E ABI tag "
1407 "that %qT (used in its type) has", p->t, tag, *tp))
1408 inform (location_of (*tp), "%qT declared here", *tp);
1409 }
1410 else if (TYPE_P (p->subob))
1411 {
1412 if (warning (OPT_Wabi_tag, "%qT does not have the %E ABI tag "
1413 "that base %qT has", p->t, tag, p->subob))
1414 inform (location_of (p->subob), "%qT declared here",
1415 p->subob);
1416 }
1417 else
1418 {
1419 if (warning (OPT_Wabi_tag, "%qT does not have the %E ABI tag "
1420 "that %qT (used in the type of %qD) has",
1421 p->t, tag, *tp, p->subob))
1422 {
1423 inform (location_of (p->subob), "%qD declared here",
1424 p->subob);
1425 inform (location_of (*tp), "%qT declared here", *tp);
1426 }
1427 }
1428 }
1429 }
1430
1431 /* Find all the ABI tags in the attribute list ATTR and either call
1432 check_tag (if TP is non-null) or set IDENTIFIER_MARKED to val. */
1433
1434 static void
1435 mark_or_check_attr_tags (tree attr, tree *tp, abi_tag_data *p, bool val)
1436 {
1437 if (!attr)
1438 return;
1439 for (; (attr = lookup_attribute ("abi_tag", attr));
1440 attr = TREE_CHAIN (attr))
1441 for (tree list = TREE_VALUE (attr); list;
1442 list = TREE_CHAIN (list))
1443 {
1444 tree tag = TREE_VALUE (list);
1445 tree id = get_identifier (TREE_STRING_POINTER (tag));
1446 if (tp)
1447 check_tag (tag, id, tp, p);
1448 else
1449 IDENTIFIER_MARKED (id) = val;
1450 }
1451 }
1452
1453 /* Find all the ABI tags on T and its enclosing scopes and either call
1454 check_tag (if TP is non-null) or set IDENTIFIER_MARKED to val. */
1455
1456 static void
1457 mark_or_check_tags (tree t, tree *tp, abi_tag_data *p, bool val)
1458 {
1459 while (t != global_namespace)
1460 {
1461 tree attr;
1462 if (TYPE_P (t))
1463 {
1464 attr = TYPE_ATTRIBUTES (t);
1465 t = CP_TYPE_CONTEXT (t);
1466 }
1467 else
1468 {
1469 attr = DECL_ATTRIBUTES (t);
1470 t = CP_DECL_CONTEXT (t);
1471 }
1472 mark_or_check_attr_tags (attr, tp, p, val);
1473 }
1474 }
1475
1476 /* walk_tree callback for check_abi_tags: if the type at *TP involves any
1477 types with ABI tags, add the corresponding identifiers to the VEC in
1478 *DATA and set IDENTIFIER_MARKED. */
1479
1480 static tree
1481 find_abi_tags_r (tree *tp, int *walk_subtrees, void *data)
1482 {
1483 if (!OVERLOAD_TYPE_P (*tp))
1484 return NULL_TREE;
1485
1486 /* walk_tree shouldn't be walking into any subtrees of a RECORD_TYPE
1487 anyway, but let's make sure of it. */
1488 *walk_subtrees = false;
1489
1490 abi_tag_data *p = static_cast<struct abi_tag_data*>(data);
1491
1492 mark_or_check_tags (*tp, tp, p, false);
1493
1494 return NULL_TREE;
1495 }
1496
1497 /* walk_tree callback for mark_abi_tags: if *TP is a class, set
1498 IDENTIFIER_MARKED on its ABI tags. */
1499
1500 static tree
1501 mark_abi_tags_r (tree *tp, int *walk_subtrees, void *data)
1502 {
1503 if (!OVERLOAD_TYPE_P (*tp))
1504 return NULL_TREE;
1505
1506 /* walk_tree shouldn't be walking into any subtrees of a RECORD_TYPE
1507 anyway, but let's make sure of it. */
1508 *walk_subtrees = false;
1509
1510 bool *valp = static_cast<bool*>(data);
1511
1512 mark_or_check_tags (*tp, NULL, NULL, *valp);
1513
1514 return NULL_TREE;
1515 }
1516
1517 /* Set IDENTIFIER_MARKED on all the ABI tags on T and its enclosing
1518 scopes. */
1519
1520 static void
1521 mark_abi_tags (tree t, bool val)
1522 {
1523 mark_or_check_tags (t, NULL, NULL, val);
1524 if (DECL_P (t))
1525 {
1526 if (DECL_LANG_SPECIFIC (t) && DECL_USE_TEMPLATE (t)
1527 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (t)))
1528 {
1529 /* Template arguments are part of the signature. */
1530 tree level = INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (t));
1531 for (int j = 0; j < TREE_VEC_LENGTH (level); ++j)
1532 {
1533 tree arg = TREE_VEC_ELT (level, j);
1534 cp_walk_tree_without_duplicates (&arg, mark_abi_tags_r, &val);
1535 }
1536 }
1537 if (TREE_CODE (t) == FUNCTION_DECL)
1538 /* A function's parameter types are part of the signature, so
1539 we don't need to inherit any tags that are also in them. */
1540 for (tree arg = FUNCTION_FIRST_USER_PARMTYPE (t); arg;
1541 arg = TREE_CHAIN (arg))
1542 cp_walk_tree_without_duplicates (&TREE_VALUE (arg),
1543 mark_abi_tags_r, &val);
1544 }
1545 }
1546
1547 /* Check that T has all the ABI tags that subobject SUBOB has, or
1548 warn if not. If T is a (variable or function) declaration, also
1549 add any missing tags. */
1550
1551 static void
1552 check_abi_tags (tree t, tree subob)
1553 {
1554 bool inherit = DECL_P (t);
1555
1556 if (!inherit && !warn_abi_tag)
1557 return;
1558
1559 tree decl = TYPE_P (t) ? TYPE_NAME (t) : t;
1560 if (!TREE_PUBLIC (decl))
1561 /* No need to worry about things local to this TU. */
1562 return;
1563
1564 mark_abi_tags (t, true);
1565
1566 tree subtype = TYPE_P (subob) ? subob : TREE_TYPE (subob);
1567 struct abi_tag_data data = { t, subob, error_mark_node };
1568 if (inherit)
1569 data.tags = NULL_TREE;
1570
1571 cp_walk_tree_without_duplicates (&subtype, find_abi_tags_r, &data);
1572
1573 if (inherit && data.tags)
1574 {
1575 tree attr = lookup_attribute ("abi_tag", DECL_ATTRIBUTES (t));
1576 if (attr)
1577 TREE_VALUE (attr) = chainon (data.tags, TREE_VALUE (attr));
1578 else
1579 DECL_ATTRIBUTES (t)
1580 = tree_cons (get_identifier ("abi_tag"), data.tags,
1581 DECL_ATTRIBUTES (t));
1582 }
1583
1584 mark_abi_tags (t, false);
1585 }
1586
1587 /* Check that DECL has all the ABI tags that are used in parts of its type
1588 that are not reflected in its mangled name. */
1589
1590 void
1591 check_abi_tags (tree decl)
1592 {
1593 if (TREE_CODE (decl) == VAR_DECL)
1594 check_abi_tags (decl, TREE_TYPE (decl));
1595 else if (TREE_CODE (decl) == FUNCTION_DECL
1596 && !mangle_return_type_p (decl))
1597 check_abi_tags (decl, TREE_TYPE (TREE_TYPE (decl)));
1598 }
1599
1600 void
1601 inherit_targ_abi_tags (tree t)
1602 {
1603 if (!CLASS_TYPE_P (t)
1604 || CLASSTYPE_TEMPLATE_INFO (t) == NULL_TREE)
1605 return;
1606
1607 mark_abi_tags (t, true);
1608
1609 tree args = CLASSTYPE_TI_ARGS (t);
1610 struct abi_tag_data data = { t, NULL_TREE, NULL_TREE };
1611 for (int i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
1612 {
1613 tree level = TMPL_ARGS_LEVEL (args, i+1);
1614 for (int j = 0; j < TREE_VEC_LENGTH (level); ++j)
1615 {
1616 tree arg = TREE_VEC_ELT (level, j);
1617 data.subob = arg;
1618 cp_walk_tree_without_duplicates (&arg, find_abi_tags_r, &data);
1619 }
1620 }
1621
1622 // If we found some tags on our template arguments, add them to our
1623 // abi_tag attribute.
1624 if (data.tags)
1625 {
1626 tree attr = lookup_attribute ("abi_tag", TYPE_ATTRIBUTES (t));
1627 if (attr)
1628 TREE_VALUE (attr) = chainon (data.tags, TREE_VALUE (attr));
1629 else
1630 TYPE_ATTRIBUTES (t)
1631 = tree_cons (get_identifier ("abi_tag"), data.tags,
1632 TYPE_ATTRIBUTES (t));
1633 }
1634
1635 mark_abi_tags (t, false);
1636 }
1637
1638 /* Return true, iff class T has a non-virtual destructor that is
1639 accessible from outside the class heirarchy (i.e. is public, or
1640 there's a suitable friend. */
1641
1642 static bool
1643 accessible_nvdtor_p (tree t)
1644 {
1645 tree dtor = CLASSTYPE_DESTRUCTORS (t);
1646
1647 /* An implicitly declared destructor is always public. And,
1648 if it were virtual, we would have created it by now. */
1649 if (!dtor)
1650 return true;
1651
1652 if (DECL_VINDEX (dtor))
1653 return false; /* Virtual */
1654
1655 if (!TREE_PRIVATE (dtor) && !TREE_PROTECTED (dtor))
1656 return true; /* Public */
1657
1658 if (CLASSTYPE_FRIEND_CLASSES (t)
1659 || DECL_FRIENDLIST (TYPE_MAIN_DECL (t)))
1660 return true; /* Has friends */
1661
1662 return false;
1663 }
1664
1665 /* Run through the base classes of T, updating CANT_HAVE_CONST_CTOR_P,
1666 and NO_CONST_ASN_REF_P. Also set flag bits in T based on
1667 properties of the bases. */
1668
1669 static void
1670 check_bases (tree t,
1671 int* cant_have_const_ctor_p,
1672 int* no_const_asn_ref_p)
1673 {
1674 int i;
1675 bool seen_non_virtual_nearly_empty_base_p = 0;
1676 int seen_tm_mask = 0;
1677 tree base_binfo;
1678 tree binfo;
1679 tree field = NULL_TREE;
1680
1681 if (!CLASSTYPE_NON_STD_LAYOUT (t))
1682 for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
1683 if (TREE_CODE (field) == FIELD_DECL)
1684 break;
1685
1686 for (binfo = TYPE_BINFO (t), i = 0;
1687 BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
1688 {
1689 tree basetype = TREE_TYPE (base_binfo);
1690
1691 gcc_assert (COMPLETE_TYPE_P (basetype));
1692
1693 if (CLASSTYPE_FINAL (basetype))
1694 error ("cannot derive from %<final%> base %qT in derived type %qT",
1695 basetype, t);
1696
1697 /* If any base class is non-literal, so is the derived class. */
1698 if (!CLASSTYPE_LITERAL_P (basetype))
1699 CLASSTYPE_LITERAL_P (t) = false;
1700
1701 /* If the base class doesn't have copy constructors or
1702 assignment operators that take const references, then the
1703 derived class cannot have such a member automatically
1704 generated. */
1705 if (TYPE_HAS_COPY_CTOR (basetype)
1706 && ! TYPE_HAS_CONST_COPY_CTOR (basetype))
1707 *cant_have_const_ctor_p = 1;
1708 if (TYPE_HAS_COPY_ASSIGN (basetype)
1709 && !TYPE_HAS_CONST_COPY_ASSIGN (basetype))
1710 *no_const_asn_ref_p = 1;
1711
1712 if (BINFO_VIRTUAL_P (base_binfo))
1713 /* A virtual base does not effect nearly emptiness. */
1714 ;
1715 else if (CLASSTYPE_NEARLY_EMPTY_P (basetype))
1716 {
1717 if (seen_non_virtual_nearly_empty_base_p)
1718 /* And if there is more than one nearly empty base, then the
1719 derived class is not nearly empty either. */
1720 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
1721 else
1722 /* Remember we've seen one. */
1723 seen_non_virtual_nearly_empty_base_p = 1;
1724 }
1725 else if (!is_empty_class (basetype))
1726 /* If the base class is not empty or nearly empty, then this
1727 class cannot be nearly empty. */
1728 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
1729
1730 /* A lot of properties from the bases also apply to the derived
1731 class. */
1732 TYPE_NEEDS_CONSTRUCTING (t) |= TYPE_NEEDS_CONSTRUCTING (basetype);
1733 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t)
1734 |= TYPE_HAS_NONTRIVIAL_DESTRUCTOR (basetype);
1735 TYPE_HAS_COMPLEX_COPY_ASSIGN (t)
1736 |= (TYPE_HAS_COMPLEX_COPY_ASSIGN (basetype)
1737 || !TYPE_HAS_COPY_ASSIGN (basetype));
1738 TYPE_HAS_COMPLEX_COPY_CTOR (t) |= (TYPE_HAS_COMPLEX_COPY_CTOR (basetype)
1739 || !TYPE_HAS_COPY_CTOR (basetype));
1740 TYPE_HAS_COMPLEX_MOVE_ASSIGN (t)
1741 |= TYPE_HAS_COMPLEX_MOVE_ASSIGN (basetype);
1742 TYPE_HAS_COMPLEX_MOVE_CTOR (t) |= TYPE_HAS_COMPLEX_MOVE_CTOR (basetype);
1743 TYPE_POLYMORPHIC_P (t) |= TYPE_POLYMORPHIC_P (basetype);
1744 CLASSTYPE_CONTAINS_EMPTY_CLASS_P (t)
1745 |= CLASSTYPE_CONTAINS_EMPTY_CLASS_P (basetype);
1746 TYPE_HAS_COMPLEX_DFLT (t) |= (!TYPE_HAS_DEFAULT_CONSTRUCTOR (basetype)
1747 || TYPE_HAS_COMPLEX_DFLT (basetype));
1748 SET_CLASSTYPE_READONLY_FIELDS_NEED_INIT
1749 (t, CLASSTYPE_READONLY_FIELDS_NEED_INIT (t)
1750 | CLASSTYPE_READONLY_FIELDS_NEED_INIT (basetype));
1751 SET_CLASSTYPE_REF_FIELDS_NEED_INIT
1752 (t, CLASSTYPE_REF_FIELDS_NEED_INIT (t)
1753 | CLASSTYPE_REF_FIELDS_NEED_INIT (basetype));
1754
1755 /* A standard-layout class is a class that:
1756 ...
1757 * has no non-standard-layout base classes, */
1758 CLASSTYPE_NON_STD_LAYOUT (t) |= CLASSTYPE_NON_STD_LAYOUT (basetype);
1759 if (!CLASSTYPE_NON_STD_LAYOUT (t))
1760 {
1761 tree basefield;
1762 /* ...has no base classes of the same type as the first non-static
1763 data member... */
1764 if (field && DECL_CONTEXT (field) == t
1765 && (same_type_ignoring_top_level_qualifiers_p
1766 (TREE_TYPE (field), basetype)))
1767 CLASSTYPE_NON_STD_LAYOUT (t) = 1;
1768 else
1769 /* ...either has no non-static data members in the most-derived
1770 class and at most one base class with non-static data
1771 members, or has no base classes with non-static data
1772 members */
1773 for (basefield = TYPE_FIELDS (basetype); basefield;
1774 basefield = DECL_CHAIN (basefield))
1775 if (TREE_CODE (basefield) == FIELD_DECL)
1776 {
1777 if (field)
1778 CLASSTYPE_NON_STD_LAYOUT (t) = 1;
1779 else
1780 field = basefield;
1781 break;
1782 }
1783 }
1784
1785 /* Don't bother collecting tm attributes if transactional memory
1786 support is not enabled. */
1787 if (flag_tm)
1788 {
1789 tree tm_attr = find_tm_attribute (TYPE_ATTRIBUTES (basetype));
1790 if (tm_attr)
1791 seen_tm_mask |= tm_attr_to_mask (tm_attr);
1792 }
1793
1794 check_abi_tags (t, basetype);
1795 }
1796
1797 /* If one of the base classes had TM attributes, and the current class
1798 doesn't define its own, then the current class inherits one. */
1799 if (seen_tm_mask && !find_tm_attribute (TYPE_ATTRIBUTES (t)))
1800 {
1801 tree tm_attr = tm_mask_to_attr (seen_tm_mask & -seen_tm_mask);
1802 TYPE_ATTRIBUTES (t) = tree_cons (tm_attr, NULL, TYPE_ATTRIBUTES (t));
1803 }
1804 }
1805
1806 /* Determine all the primary bases within T. Sets BINFO_PRIMARY_BASE_P for
1807 those that are primaries. Sets BINFO_LOST_PRIMARY_P for those
1808 that have had a nearly-empty virtual primary base stolen by some
1809 other base in the hierarchy. Determines CLASSTYPE_PRIMARY_BASE for
1810 T. */
1811
1812 static void
1813 determine_primary_bases (tree t)
1814 {
1815 unsigned i;
1816 tree primary = NULL_TREE;
1817 tree type_binfo = TYPE_BINFO (t);
1818 tree base_binfo;
1819
1820 /* Determine the primary bases of our bases. */
1821 for (base_binfo = TREE_CHAIN (type_binfo); base_binfo;
1822 base_binfo = TREE_CHAIN (base_binfo))
1823 {
1824 tree primary = CLASSTYPE_PRIMARY_BINFO (BINFO_TYPE (base_binfo));
1825
1826 /* See if we're the non-virtual primary of our inheritance
1827 chain. */
1828 if (!BINFO_VIRTUAL_P (base_binfo))
1829 {
1830 tree parent = BINFO_INHERITANCE_CHAIN (base_binfo);
1831 tree parent_primary = CLASSTYPE_PRIMARY_BINFO (BINFO_TYPE (parent));
1832
1833 if (parent_primary
1834 && SAME_BINFO_TYPE_P (BINFO_TYPE (base_binfo),
1835 BINFO_TYPE (parent_primary)))
1836 /* We are the primary binfo. */
1837 BINFO_PRIMARY_P (base_binfo) = 1;
1838 }
1839 /* Determine if we have a virtual primary base, and mark it so.
1840 */
1841 if (primary && BINFO_VIRTUAL_P (primary))
1842 {
1843 tree this_primary = copied_binfo (primary, base_binfo);
1844
1845 if (BINFO_PRIMARY_P (this_primary))
1846 /* Someone already claimed this base. */
1847 BINFO_LOST_PRIMARY_P (base_binfo) = 1;
1848 else
1849 {
1850 tree delta;
1851
1852 BINFO_PRIMARY_P (this_primary) = 1;
1853 BINFO_INHERITANCE_CHAIN (this_primary) = base_binfo;
1854
1855 /* A virtual binfo might have been copied from within
1856 another hierarchy. As we're about to use it as a
1857 primary base, make sure the offsets match. */
1858 delta = size_diffop_loc (input_location,
1859 convert (ssizetype,
1860 BINFO_OFFSET (base_binfo)),
1861 convert (ssizetype,
1862 BINFO_OFFSET (this_primary)));
1863
1864 propagate_binfo_offsets (this_primary, delta);
1865 }
1866 }
1867 }
1868
1869 /* First look for a dynamic direct non-virtual base. */
1870 for (i = 0; BINFO_BASE_ITERATE (type_binfo, i, base_binfo); i++)
1871 {
1872 tree basetype = BINFO_TYPE (base_binfo);
1873
1874 if (TYPE_CONTAINS_VPTR_P (basetype) && !BINFO_VIRTUAL_P (base_binfo))
1875 {
1876 primary = base_binfo;
1877 goto found;
1878 }
1879 }
1880
1881 /* A "nearly-empty" virtual base class can be the primary base
1882 class, if no non-virtual polymorphic base can be found. Look for
1883 a nearly-empty virtual dynamic base that is not already a primary
1884 base of something in the hierarchy. If there is no such base,
1885 just pick the first nearly-empty virtual base. */
1886
1887 for (base_binfo = TREE_CHAIN (type_binfo); base_binfo;
1888 base_binfo = TREE_CHAIN (base_binfo))
1889 if (BINFO_VIRTUAL_P (base_binfo)
1890 && CLASSTYPE_NEARLY_EMPTY_P (BINFO_TYPE (base_binfo)))
1891 {
1892 if (!BINFO_PRIMARY_P (base_binfo))
1893 {
1894 /* Found one that is not primary. */
1895 primary = base_binfo;
1896 goto found;
1897 }
1898 else if (!primary)
1899 /* Remember the first candidate. */
1900 primary = base_binfo;
1901 }
1902
1903 found:
1904 /* If we've got a primary base, use it. */
1905 if (primary)
1906 {
1907 tree basetype = BINFO_TYPE (primary);
1908
1909 CLASSTYPE_PRIMARY_BINFO (t) = primary;
1910 if (BINFO_PRIMARY_P (primary))
1911 /* We are stealing a primary base. */
1912 BINFO_LOST_PRIMARY_P (BINFO_INHERITANCE_CHAIN (primary)) = 1;
1913 BINFO_PRIMARY_P (primary) = 1;
1914 if (BINFO_VIRTUAL_P (primary))
1915 {
1916 tree delta;
1917
1918 BINFO_INHERITANCE_CHAIN (primary) = type_binfo;
1919 /* A virtual binfo might have been copied from within
1920 another hierarchy. As we're about to use it as a primary
1921 base, make sure the offsets match. */
1922 delta = size_diffop_loc (input_location, ssize_int (0),
1923 convert (ssizetype, BINFO_OFFSET (primary)));
1924
1925 propagate_binfo_offsets (primary, delta);
1926 }
1927
1928 primary = TYPE_BINFO (basetype);
1929
1930 TYPE_VFIELD (t) = TYPE_VFIELD (basetype);
1931 BINFO_VTABLE (type_binfo) = BINFO_VTABLE (primary);
1932 BINFO_VIRTUALS (type_binfo) = BINFO_VIRTUALS (primary);
1933 }
1934 }
1935
1936 /* Update the variant types of T. */
1937
1938 void
1939 fixup_type_variants (tree t)
1940 {
1941 tree variants;
1942
1943 if (!t)
1944 return;
1945
1946 for (variants = TYPE_NEXT_VARIANT (t);
1947 variants;
1948 variants = TYPE_NEXT_VARIANT (variants))
1949 {
1950 /* These fields are in the _TYPE part of the node, not in
1951 the TYPE_LANG_SPECIFIC component, so they are not shared. */
1952 TYPE_HAS_USER_CONSTRUCTOR (variants) = TYPE_HAS_USER_CONSTRUCTOR (t);
1953 TYPE_NEEDS_CONSTRUCTING (variants) = TYPE_NEEDS_CONSTRUCTING (t);
1954 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (variants)
1955 = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t);
1956
1957 TYPE_POLYMORPHIC_P (variants) = TYPE_POLYMORPHIC_P (t);
1958
1959 TYPE_BINFO (variants) = TYPE_BINFO (t);
1960
1961 /* Copy whatever these are holding today. */
1962 TYPE_VFIELD (variants) = TYPE_VFIELD (t);
1963 TYPE_FIELDS (variants) = TYPE_FIELDS (t);
1964 }
1965 }
1966
1967 /* Early variant fixups: we apply attributes at the beginning of the class
1968 definition, and we need to fix up any variants that have already been
1969 made via elaborated-type-specifier so that check_qualified_type works. */
1970
1971 void
1972 fixup_attribute_variants (tree t)
1973 {
1974 tree variants;
1975
1976 if (!t)
1977 return;
1978
1979 tree attrs = TYPE_ATTRIBUTES (t);
1980 unsigned align = TYPE_ALIGN (t);
1981 bool user_align = TYPE_USER_ALIGN (t);
1982
1983 for (variants = TYPE_NEXT_VARIANT (t);
1984 variants;
1985 variants = TYPE_NEXT_VARIANT (variants))
1986 {
1987 /* These are the two fields that check_qualified_type looks at and
1988 are affected by attributes. */
1989 TYPE_ATTRIBUTES (variants) = attrs;
1990 unsigned valign = align;
1991 if (TYPE_USER_ALIGN (variants))
1992 valign = MAX (valign, TYPE_ALIGN (variants));
1993 else
1994 TYPE_USER_ALIGN (variants) = user_align;
1995 TYPE_ALIGN (variants) = valign;
1996 }
1997 }
1998 \f
1999 /* Set memoizing fields and bits of T (and its variants) for later
2000 use. */
2001
2002 static void
2003 finish_struct_bits (tree t)
2004 {
2005 /* Fix up variants (if any). */
2006 fixup_type_variants (t);
2007
2008 if (BINFO_N_BASE_BINFOS (TYPE_BINFO (t)) && TYPE_POLYMORPHIC_P (t))
2009 /* For a class w/o baseclasses, 'finish_struct' has set
2010 CLASSTYPE_PURE_VIRTUALS correctly (by definition).
2011 Similarly for a class whose base classes do not have vtables.
2012 When neither of these is true, we might have removed abstract
2013 virtuals (by providing a definition), added some (by declaring
2014 new ones), or redeclared ones from a base class. We need to
2015 recalculate what's really an abstract virtual at this point (by
2016 looking in the vtables). */
2017 get_pure_virtuals (t);
2018
2019 /* If this type has a copy constructor or a destructor, force its
2020 mode to be BLKmode, and force its TREE_ADDRESSABLE bit to be
2021 nonzero. This will cause it to be passed by invisible reference
2022 and prevent it from being returned in a register. */
2023 if (type_has_nontrivial_copy_init (t)
2024 || TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t))
2025 {
2026 tree variants;
2027 DECL_MODE (TYPE_MAIN_DECL (t)) = BLKmode;
2028 for (variants = t; variants; variants = TYPE_NEXT_VARIANT (variants))
2029 {
2030 SET_TYPE_MODE (variants, BLKmode);
2031 TREE_ADDRESSABLE (variants) = 1;
2032 }
2033 }
2034 }
2035
2036 /* Issue warnings about T having private constructors, but no friends,
2037 and so forth.
2038
2039 HAS_NONPRIVATE_METHOD is nonzero if T has any non-private methods or
2040 static members. HAS_NONPRIVATE_STATIC_FN is nonzero if T has any
2041 non-private static member functions. */
2042
2043 static void
2044 maybe_warn_about_overly_private_class (tree t)
2045 {
2046 int has_member_fn = 0;
2047 int has_nonprivate_method = 0;
2048 tree fn;
2049
2050 if (!warn_ctor_dtor_privacy
2051 /* If the class has friends, those entities might create and
2052 access instances, so we should not warn. */
2053 || (CLASSTYPE_FRIEND_CLASSES (t)
2054 || DECL_FRIENDLIST (TYPE_MAIN_DECL (t)))
2055 /* We will have warned when the template was declared; there's
2056 no need to warn on every instantiation. */
2057 || CLASSTYPE_TEMPLATE_INSTANTIATION (t))
2058 /* There's no reason to even consider warning about this
2059 class. */
2060 return;
2061
2062 /* We only issue one warning, if more than one applies, because
2063 otherwise, on code like:
2064
2065 class A {
2066 // Oops - forgot `public:'
2067 A();
2068 A(const A&);
2069 ~A();
2070 };
2071
2072 we warn several times about essentially the same problem. */
2073
2074 /* Check to see if all (non-constructor, non-destructor) member
2075 functions are private. (Since there are no friends or
2076 non-private statics, we can't ever call any of the private member
2077 functions.) */
2078 for (fn = TYPE_METHODS (t); fn; fn = DECL_CHAIN (fn))
2079 /* We're not interested in compiler-generated methods; they don't
2080 provide any way to call private members. */
2081 if (!DECL_ARTIFICIAL (fn))
2082 {
2083 if (!TREE_PRIVATE (fn))
2084 {
2085 if (DECL_STATIC_FUNCTION_P (fn))
2086 /* A non-private static member function is just like a
2087 friend; it can create and invoke private member
2088 functions, and be accessed without a class
2089 instance. */
2090 return;
2091
2092 has_nonprivate_method = 1;
2093 /* Keep searching for a static member function. */
2094 }
2095 else if (!DECL_CONSTRUCTOR_P (fn) && !DECL_DESTRUCTOR_P (fn))
2096 has_member_fn = 1;
2097 }
2098
2099 if (!has_nonprivate_method && has_member_fn)
2100 {
2101 /* There are no non-private methods, and there's at least one
2102 private member function that isn't a constructor or
2103 destructor. (If all the private members are
2104 constructors/destructors we want to use the code below that
2105 issues error messages specifically referring to
2106 constructors/destructors.) */
2107 unsigned i;
2108 tree binfo = TYPE_BINFO (t);
2109
2110 for (i = 0; i != BINFO_N_BASE_BINFOS (binfo); i++)
2111 if (BINFO_BASE_ACCESS (binfo, i) != access_private_node)
2112 {
2113 has_nonprivate_method = 1;
2114 break;
2115 }
2116 if (!has_nonprivate_method)
2117 {
2118 warning (OPT_Wctor_dtor_privacy,
2119 "all member functions in class %qT are private", t);
2120 return;
2121 }
2122 }
2123
2124 /* Even if some of the member functions are non-private, the class
2125 won't be useful for much if all the constructors or destructors
2126 are private: such an object can never be created or destroyed. */
2127 fn = CLASSTYPE_DESTRUCTORS (t);
2128 if (fn && TREE_PRIVATE (fn))
2129 {
2130 warning (OPT_Wctor_dtor_privacy,
2131 "%q#T only defines a private destructor and has no friends",
2132 t);
2133 return;
2134 }
2135
2136 /* Warn about classes that have private constructors and no friends. */
2137 if (TYPE_HAS_USER_CONSTRUCTOR (t)
2138 /* Implicitly generated constructors are always public. */
2139 && (!CLASSTYPE_LAZY_DEFAULT_CTOR (t)
2140 || !CLASSTYPE_LAZY_COPY_CTOR (t)))
2141 {
2142 int nonprivate_ctor = 0;
2143
2144 /* If a non-template class does not define a copy
2145 constructor, one is defined for it, enabling it to avoid
2146 this warning. For a template class, this does not
2147 happen, and so we would normally get a warning on:
2148
2149 template <class T> class C { private: C(); };
2150
2151 To avoid this asymmetry, we check TYPE_HAS_COPY_CTOR. All
2152 complete non-template or fully instantiated classes have this
2153 flag set. */
2154 if (!TYPE_HAS_COPY_CTOR (t))
2155 nonprivate_ctor = 1;
2156 else
2157 for (fn = CLASSTYPE_CONSTRUCTORS (t); fn; fn = OVL_NEXT (fn))
2158 {
2159 tree ctor = OVL_CURRENT (fn);
2160 /* Ideally, we wouldn't count copy constructors (or, in
2161 fact, any constructor that takes an argument of the
2162 class type as a parameter) because such things cannot
2163 be used to construct an instance of the class unless
2164 you already have one. But, for now at least, we're
2165 more generous. */
2166 if (! TREE_PRIVATE (ctor))
2167 {
2168 nonprivate_ctor = 1;
2169 break;
2170 }
2171 }
2172
2173 if (nonprivate_ctor == 0)
2174 {
2175 warning (OPT_Wctor_dtor_privacy,
2176 "%q#T only defines private constructors and has no friends",
2177 t);
2178 return;
2179 }
2180 }
2181 }
2182
2183 static struct {
2184 gt_pointer_operator new_value;
2185 void *cookie;
2186 } resort_data;
2187
2188 /* Comparison function to compare two TYPE_METHOD_VEC entries by name. */
2189
2190 static int
2191 method_name_cmp (const void* m1_p, const void* m2_p)
2192 {
2193 const tree *const m1 = (const tree *) m1_p;
2194 const tree *const m2 = (const tree *) m2_p;
2195
2196 if (*m1 == NULL_TREE && *m2 == NULL_TREE)
2197 return 0;
2198 if (*m1 == NULL_TREE)
2199 return -1;
2200 if (*m2 == NULL_TREE)
2201 return 1;
2202 if (DECL_NAME (OVL_CURRENT (*m1)) < DECL_NAME (OVL_CURRENT (*m2)))
2203 return -1;
2204 return 1;
2205 }
2206
2207 /* This routine compares two fields like method_name_cmp but using the
2208 pointer operator in resort_field_decl_data. */
2209
2210 static int
2211 resort_method_name_cmp (const void* m1_p, const void* m2_p)
2212 {
2213 const tree *const m1 = (const tree *) m1_p;
2214 const tree *const m2 = (const tree *) m2_p;
2215 if (*m1 == NULL_TREE && *m2 == NULL_TREE)
2216 return 0;
2217 if (*m1 == NULL_TREE)
2218 return -1;
2219 if (*m2 == NULL_TREE)
2220 return 1;
2221 {
2222 tree d1 = DECL_NAME (OVL_CURRENT (*m1));
2223 tree d2 = DECL_NAME (OVL_CURRENT (*m2));
2224 resort_data.new_value (&d1, resort_data.cookie);
2225 resort_data.new_value (&d2, resort_data.cookie);
2226 if (d1 < d2)
2227 return -1;
2228 }
2229 return 1;
2230 }
2231
2232 /* Resort TYPE_METHOD_VEC because pointers have been reordered. */
2233
2234 void
2235 resort_type_method_vec (void* obj,
2236 void* /*orig_obj*/,
2237 gt_pointer_operator new_value,
2238 void* cookie)
2239 {
2240 vec<tree, va_gc> *method_vec = (vec<tree, va_gc> *) obj;
2241 int len = vec_safe_length (method_vec);
2242 size_t slot;
2243 tree fn;
2244
2245 /* The type conversion ops have to live at the front of the vec, so we
2246 can't sort them. */
2247 for (slot = CLASSTYPE_FIRST_CONVERSION_SLOT;
2248 vec_safe_iterate (method_vec, slot, &fn);
2249 ++slot)
2250 if (!DECL_CONV_FN_P (OVL_CURRENT (fn)))
2251 break;
2252
2253 if (len - slot > 1)
2254 {
2255 resort_data.new_value = new_value;
2256 resort_data.cookie = cookie;
2257 qsort (method_vec->address () + slot, len - slot, sizeof (tree),
2258 resort_method_name_cmp);
2259 }
2260 }
2261
2262 /* Warn about duplicate methods in fn_fields.
2263
2264 Sort methods that are not special (i.e., constructors, destructors,
2265 and type conversion operators) so that we can find them faster in
2266 search. */
2267
2268 static void
2269 finish_struct_methods (tree t)
2270 {
2271 tree fn_fields;
2272 vec<tree, va_gc> *method_vec;
2273 int slot, len;
2274
2275 method_vec = CLASSTYPE_METHOD_VEC (t);
2276 if (!method_vec)
2277 return;
2278
2279 len = method_vec->length ();
2280
2281 /* Clear DECL_IN_AGGR_P for all functions. */
2282 for (fn_fields = TYPE_METHODS (t); fn_fields;
2283 fn_fields = DECL_CHAIN (fn_fields))
2284 DECL_IN_AGGR_P (fn_fields) = 0;
2285
2286 /* Issue warnings about private constructors and such. If there are
2287 no methods, then some public defaults are generated. */
2288 maybe_warn_about_overly_private_class (t);
2289
2290 /* The type conversion ops have to live at the front of the vec, so we
2291 can't sort them. */
2292 for (slot = CLASSTYPE_FIRST_CONVERSION_SLOT;
2293 method_vec->iterate (slot, &fn_fields);
2294 ++slot)
2295 if (!DECL_CONV_FN_P (OVL_CURRENT (fn_fields)))
2296 break;
2297 if (len - slot > 1)
2298 qsort (method_vec->address () + slot,
2299 len-slot, sizeof (tree), method_name_cmp);
2300 }
2301
2302 /* Make BINFO's vtable have N entries, including RTTI entries,
2303 vbase and vcall offsets, etc. Set its type and call the back end
2304 to lay it out. */
2305
2306 static void
2307 layout_vtable_decl (tree binfo, int n)
2308 {
2309 tree atype;
2310 tree vtable;
2311
2312 atype = build_array_of_n_type (vtable_entry_type, n);
2313 layout_type (atype);
2314
2315 /* We may have to grow the vtable. */
2316 vtable = get_vtbl_decl_for_binfo (binfo);
2317 if (!same_type_p (TREE_TYPE (vtable), atype))
2318 {
2319 TREE_TYPE (vtable) = atype;
2320 DECL_SIZE (vtable) = DECL_SIZE_UNIT (vtable) = NULL_TREE;
2321 layout_decl (vtable, 0);
2322 }
2323 }
2324
2325 /* True iff FNDECL and BASE_FNDECL (both non-static member functions)
2326 have the same signature. */
2327
2328 int
2329 same_signature_p (const_tree fndecl, const_tree base_fndecl)
2330 {
2331 /* One destructor overrides another if they are the same kind of
2332 destructor. */
2333 if (DECL_DESTRUCTOR_P (base_fndecl) && DECL_DESTRUCTOR_P (fndecl)
2334 && special_function_p (base_fndecl) == special_function_p (fndecl))
2335 return 1;
2336 /* But a non-destructor never overrides a destructor, nor vice
2337 versa, nor do different kinds of destructors override
2338 one-another. For example, a complete object destructor does not
2339 override a deleting destructor. */
2340 if (DECL_DESTRUCTOR_P (base_fndecl) || DECL_DESTRUCTOR_P (fndecl))
2341 return 0;
2342
2343 if (DECL_NAME (fndecl) == DECL_NAME (base_fndecl)
2344 || (DECL_CONV_FN_P (fndecl)
2345 && DECL_CONV_FN_P (base_fndecl)
2346 && same_type_p (DECL_CONV_FN_TYPE (fndecl),
2347 DECL_CONV_FN_TYPE (base_fndecl))))
2348 {
2349 tree fntype = TREE_TYPE (fndecl);
2350 tree base_fntype = TREE_TYPE (base_fndecl);
2351 if (type_memfn_quals (fntype) == type_memfn_quals (base_fntype)
2352 && type_memfn_rqual (fntype) == type_memfn_rqual (base_fntype)
2353 && compparms (FUNCTION_FIRST_USER_PARMTYPE (fndecl),
2354 FUNCTION_FIRST_USER_PARMTYPE (base_fndecl)))
2355 return 1;
2356 }
2357 return 0;
2358 }
2359
2360 /* Returns TRUE if DERIVED is a binfo containing the binfo BASE as a
2361 subobject. */
2362
2363 static bool
2364 base_derived_from (tree derived, tree base)
2365 {
2366 tree probe;
2367
2368 for (probe = base; probe; probe = BINFO_INHERITANCE_CHAIN (probe))
2369 {
2370 if (probe == derived)
2371 return true;
2372 else if (BINFO_VIRTUAL_P (probe))
2373 /* If we meet a virtual base, we can't follow the inheritance
2374 any more. See if the complete type of DERIVED contains
2375 such a virtual base. */
2376 return (binfo_for_vbase (BINFO_TYPE (probe), BINFO_TYPE (derived))
2377 != NULL_TREE);
2378 }
2379 return false;
2380 }
2381
2382 typedef struct find_final_overrider_data_s {
2383 /* The function for which we are trying to find a final overrider. */
2384 tree fn;
2385 /* The base class in which the function was declared. */
2386 tree declaring_base;
2387 /* The candidate overriders. */
2388 tree candidates;
2389 /* Path to most derived. */
2390 vec<tree> path;
2391 } find_final_overrider_data;
2392
2393 /* Add the overrider along the current path to FFOD->CANDIDATES.
2394 Returns true if an overrider was found; false otherwise. */
2395
2396 static bool
2397 dfs_find_final_overrider_1 (tree binfo,
2398 find_final_overrider_data *ffod,
2399 unsigned depth)
2400 {
2401 tree method;
2402
2403 /* If BINFO is not the most derived type, try a more derived class.
2404 A definition there will overrider a definition here. */
2405 if (depth)
2406 {
2407 depth--;
2408 if (dfs_find_final_overrider_1
2409 (ffod->path[depth], ffod, depth))
2410 return true;
2411 }
2412
2413 method = look_for_overrides_here (BINFO_TYPE (binfo), ffod->fn);
2414 if (method)
2415 {
2416 tree *candidate = &ffod->candidates;
2417
2418 /* Remove any candidates overridden by this new function. */
2419 while (*candidate)
2420 {
2421 /* If *CANDIDATE overrides METHOD, then METHOD
2422 cannot override anything else on the list. */
2423 if (base_derived_from (TREE_VALUE (*candidate), binfo))
2424 return true;
2425 /* If METHOD overrides *CANDIDATE, remove *CANDIDATE. */
2426 if (base_derived_from (binfo, TREE_VALUE (*candidate)))
2427 *candidate = TREE_CHAIN (*candidate);
2428 else
2429 candidate = &TREE_CHAIN (*candidate);
2430 }
2431
2432 /* Add the new function. */
2433 ffod->candidates = tree_cons (method, binfo, ffod->candidates);
2434 return true;
2435 }
2436
2437 return false;
2438 }
2439
2440 /* Called from find_final_overrider via dfs_walk. */
2441
2442 static tree
2443 dfs_find_final_overrider_pre (tree binfo, void *data)
2444 {
2445 find_final_overrider_data *ffod = (find_final_overrider_data *) data;
2446
2447 if (binfo == ffod->declaring_base)
2448 dfs_find_final_overrider_1 (binfo, ffod, ffod->path.length ());
2449 ffod->path.safe_push (binfo);
2450
2451 return NULL_TREE;
2452 }
2453
2454 static tree
2455 dfs_find_final_overrider_post (tree /*binfo*/, void *data)
2456 {
2457 find_final_overrider_data *ffod = (find_final_overrider_data *) data;
2458 ffod->path.pop ();
2459
2460 return NULL_TREE;
2461 }
2462
2463 /* Returns a TREE_LIST whose TREE_PURPOSE is the final overrider for
2464 FN and whose TREE_VALUE is the binfo for the base where the
2465 overriding occurs. BINFO (in the hierarchy dominated by the binfo
2466 DERIVED) is the base object in which FN is declared. */
2467
2468 static tree
2469 find_final_overrider (tree derived, tree binfo, tree fn)
2470 {
2471 find_final_overrider_data ffod;
2472
2473 /* Getting this right is a little tricky. This is valid:
2474
2475 struct S { virtual void f (); };
2476 struct T { virtual void f (); };
2477 struct U : public S, public T { };
2478
2479 even though calling `f' in `U' is ambiguous. But,
2480
2481 struct R { virtual void f(); };
2482 struct S : virtual public R { virtual void f (); };
2483 struct T : virtual public R { virtual void f (); };
2484 struct U : public S, public T { };
2485
2486 is not -- there's no way to decide whether to put `S::f' or
2487 `T::f' in the vtable for `R'.
2488
2489 The solution is to look at all paths to BINFO. If we find
2490 different overriders along any two, then there is a problem. */
2491 if (DECL_THUNK_P (fn))
2492 fn = THUNK_TARGET (fn);
2493
2494 /* Determine the depth of the hierarchy. */
2495 ffod.fn = fn;
2496 ffod.declaring_base = binfo;
2497 ffod.candidates = NULL_TREE;
2498 ffod.path.create (30);
2499
2500 dfs_walk_all (derived, dfs_find_final_overrider_pre,
2501 dfs_find_final_overrider_post, &ffod);
2502
2503 ffod.path.release ();
2504
2505 /* If there was no winner, issue an error message. */
2506 if (!ffod.candidates || TREE_CHAIN (ffod.candidates))
2507 return error_mark_node;
2508
2509 return ffod.candidates;
2510 }
2511
2512 /* Return the index of the vcall offset for FN when TYPE is used as a
2513 virtual base. */
2514
2515 static tree
2516 get_vcall_index (tree fn, tree type)
2517 {
2518 vec<tree_pair_s, va_gc> *indices = CLASSTYPE_VCALL_INDICES (type);
2519 tree_pair_p p;
2520 unsigned ix;
2521
2522 FOR_EACH_VEC_SAFE_ELT (indices, ix, p)
2523 if ((DECL_DESTRUCTOR_P (fn) && DECL_DESTRUCTOR_P (p->purpose))
2524 || same_signature_p (fn, p->purpose))
2525 return p->value;
2526
2527 /* There should always be an appropriate index. */
2528 gcc_unreachable ();
2529 }
2530
2531 /* Update an entry in the vtable for BINFO, which is in the hierarchy
2532 dominated by T. FN is the old function; VIRTUALS points to the
2533 corresponding position in the new BINFO_VIRTUALS list. IX is the index
2534 of that entry in the list. */
2535
2536 static void
2537 update_vtable_entry_for_fn (tree t, tree binfo, tree fn, tree* virtuals,
2538 unsigned ix)
2539 {
2540 tree b;
2541 tree overrider;
2542 tree delta;
2543 tree virtual_base;
2544 tree first_defn;
2545 tree overrider_fn, overrider_target;
2546 tree target_fn = DECL_THUNK_P (fn) ? THUNK_TARGET (fn) : fn;
2547 tree over_return, base_return;
2548 bool lost = false;
2549
2550 /* Find the nearest primary base (possibly binfo itself) which defines
2551 this function; this is the class the caller will convert to when
2552 calling FN through BINFO. */
2553 for (b = binfo; ; b = get_primary_binfo (b))
2554 {
2555 gcc_assert (b);
2556 if (look_for_overrides_here (BINFO_TYPE (b), target_fn))
2557 break;
2558
2559 /* The nearest definition is from a lost primary. */
2560 if (BINFO_LOST_PRIMARY_P (b))
2561 lost = true;
2562 }
2563 first_defn = b;
2564
2565 /* Find the final overrider. */
2566 overrider = find_final_overrider (TYPE_BINFO (t), b, target_fn);
2567 if (overrider == error_mark_node)
2568 {
2569 error ("no unique final overrider for %qD in %qT", target_fn, t);
2570 return;
2571 }
2572 overrider_target = overrider_fn = TREE_PURPOSE (overrider);
2573
2574 /* Check for adjusting covariant return types. */
2575 over_return = TREE_TYPE (TREE_TYPE (overrider_target));
2576 base_return = TREE_TYPE (TREE_TYPE (target_fn));
2577
2578 if (POINTER_TYPE_P (over_return)
2579 && TREE_CODE (over_return) == TREE_CODE (base_return)
2580 && CLASS_TYPE_P (TREE_TYPE (over_return))
2581 && CLASS_TYPE_P (TREE_TYPE (base_return))
2582 /* If the overrider is invalid, don't even try. */
2583 && !DECL_INVALID_OVERRIDER_P (overrider_target))
2584 {
2585 /* If FN is a covariant thunk, we must figure out the adjustment
2586 to the final base FN was converting to. As OVERRIDER_TARGET might
2587 also be converting to the return type of FN, we have to
2588 combine the two conversions here. */
2589 tree fixed_offset, virtual_offset;
2590
2591 over_return = TREE_TYPE (over_return);
2592 base_return = TREE_TYPE (base_return);
2593
2594 if (DECL_THUNK_P (fn))
2595 {
2596 gcc_assert (DECL_RESULT_THUNK_P (fn));
2597 fixed_offset = ssize_int (THUNK_FIXED_OFFSET (fn));
2598 virtual_offset = THUNK_VIRTUAL_OFFSET (fn);
2599 }
2600 else
2601 fixed_offset = virtual_offset = NULL_TREE;
2602
2603 if (virtual_offset)
2604 /* Find the equivalent binfo within the return type of the
2605 overriding function. We will want the vbase offset from
2606 there. */
2607 virtual_offset = binfo_for_vbase (BINFO_TYPE (virtual_offset),
2608 over_return);
2609 else if (!same_type_ignoring_top_level_qualifiers_p
2610 (over_return, base_return))
2611 {
2612 /* There was no existing virtual thunk (which takes
2613 precedence). So find the binfo of the base function's
2614 return type within the overriding function's return type.
2615 We cannot call lookup base here, because we're inside a
2616 dfs_walk, and will therefore clobber the BINFO_MARKED
2617 flags. Fortunately we know the covariancy is valid (it
2618 has already been checked), so we can just iterate along
2619 the binfos, which have been chained in inheritance graph
2620 order. Of course it is lame that we have to repeat the
2621 search here anyway -- we should really be caching pieces
2622 of the vtable and avoiding this repeated work. */
2623 tree thunk_binfo, base_binfo;
2624
2625 /* Find the base binfo within the overriding function's
2626 return type. We will always find a thunk_binfo, except
2627 when the covariancy is invalid (which we will have
2628 already diagnosed). */
2629 for (base_binfo = TYPE_BINFO (base_return),
2630 thunk_binfo = TYPE_BINFO (over_return);
2631 thunk_binfo;
2632 thunk_binfo = TREE_CHAIN (thunk_binfo))
2633 if (SAME_BINFO_TYPE_P (BINFO_TYPE (thunk_binfo),
2634 BINFO_TYPE (base_binfo)))
2635 break;
2636
2637 /* See if virtual inheritance is involved. */
2638 for (virtual_offset = thunk_binfo;
2639 virtual_offset;
2640 virtual_offset = BINFO_INHERITANCE_CHAIN (virtual_offset))
2641 if (BINFO_VIRTUAL_P (virtual_offset))
2642 break;
2643
2644 if (virtual_offset
2645 || (thunk_binfo && !BINFO_OFFSET_ZEROP (thunk_binfo)))
2646 {
2647 tree offset = convert (ssizetype, BINFO_OFFSET (thunk_binfo));
2648
2649 if (virtual_offset)
2650 {
2651 /* We convert via virtual base. Adjust the fixed
2652 offset to be from there. */
2653 offset =
2654 size_diffop (offset,
2655 convert (ssizetype,
2656 BINFO_OFFSET (virtual_offset)));
2657 }
2658 if (fixed_offset)
2659 /* There was an existing fixed offset, this must be
2660 from the base just converted to, and the base the
2661 FN was thunking to. */
2662 fixed_offset = size_binop (PLUS_EXPR, fixed_offset, offset);
2663 else
2664 fixed_offset = offset;
2665 }
2666 }
2667
2668 if (fixed_offset || virtual_offset)
2669 /* Replace the overriding function with a covariant thunk. We
2670 will emit the overriding function in its own slot as
2671 well. */
2672 overrider_fn = make_thunk (overrider_target, /*this_adjusting=*/0,
2673 fixed_offset, virtual_offset);
2674 }
2675 else
2676 gcc_assert (DECL_INVALID_OVERRIDER_P (overrider_target) ||
2677 !DECL_THUNK_P (fn));
2678
2679 /* If we need a covariant thunk, then we may need to adjust first_defn.
2680 The ABI specifies that the thunks emitted with a function are
2681 determined by which bases the function overrides, so we need to be
2682 sure that we're using a thunk for some overridden base; even if we
2683 know that the necessary this adjustment is zero, there may not be an
2684 appropriate zero-this-adjusment thunk for us to use since thunks for
2685 overriding virtual bases always use the vcall offset.
2686
2687 Furthermore, just choosing any base that overrides this function isn't
2688 quite right, as this slot won't be used for calls through a type that
2689 puts a covariant thunk here. Calling the function through such a type
2690 will use a different slot, and that slot is the one that determines
2691 the thunk emitted for that base.
2692
2693 So, keep looking until we find the base that we're really overriding
2694 in this slot: the nearest primary base that doesn't use a covariant
2695 thunk in this slot. */
2696 if (overrider_target != overrider_fn)
2697 {
2698 if (BINFO_TYPE (b) == DECL_CONTEXT (overrider_target))
2699 /* We already know that the overrider needs a covariant thunk. */
2700 b = get_primary_binfo (b);
2701 for (; ; b = get_primary_binfo (b))
2702 {
2703 tree main_binfo = TYPE_BINFO (BINFO_TYPE (b));
2704 tree bv = chain_index (ix, BINFO_VIRTUALS (main_binfo));
2705 if (!DECL_THUNK_P (TREE_VALUE (bv)))
2706 break;
2707 if (BINFO_LOST_PRIMARY_P (b))
2708 lost = true;
2709 }
2710 first_defn = b;
2711 }
2712
2713 /* Assume that we will produce a thunk that convert all the way to
2714 the final overrider, and not to an intermediate virtual base. */
2715 virtual_base = NULL_TREE;
2716
2717 /* See if we can convert to an intermediate virtual base first, and then
2718 use the vcall offset located there to finish the conversion. */
2719 for (; b; b = BINFO_INHERITANCE_CHAIN (b))
2720 {
2721 /* If we find the final overrider, then we can stop
2722 walking. */
2723 if (SAME_BINFO_TYPE_P (BINFO_TYPE (b),
2724 BINFO_TYPE (TREE_VALUE (overrider))))
2725 break;
2726
2727 /* If we find a virtual base, and we haven't yet found the
2728 overrider, then there is a virtual base between the
2729 declaring base (first_defn) and the final overrider. */
2730 if (BINFO_VIRTUAL_P (b))
2731 {
2732 virtual_base = b;
2733 break;
2734 }
2735 }
2736
2737 /* Compute the constant adjustment to the `this' pointer. The
2738 `this' pointer, when this function is called, will point at BINFO
2739 (or one of its primary bases, which are at the same offset). */
2740 if (virtual_base)
2741 /* The `this' pointer needs to be adjusted from the declaration to
2742 the nearest virtual base. */
2743 delta = size_diffop_loc (input_location,
2744 convert (ssizetype, BINFO_OFFSET (virtual_base)),
2745 convert (ssizetype, BINFO_OFFSET (first_defn)));
2746 else if (lost)
2747 /* If the nearest definition is in a lost primary, we don't need an
2748 entry in our vtable. Except possibly in a constructor vtable,
2749 if we happen to get our primary back. In that case, the offset
2750 will be zero, as it will be a primary base. */
2751 delta = size_zero_node;
2752 else
2753 /* The `this' pointer needs to be adjusted from pointing to
2754 BINFO to pointing at the base where the final overrider
2755 appears. */
2756 delta = size_diffop_loc (input_location,
2757 convert (ssizetype,
2758 BINFO_OFFSET (TREE_VALUE (overrider))),
2759 convert (ssizetype, BINFO_OFFSET (binfo)));
2760
2761 modify_vtable_entry (t, binfo, overrider_fn, delta, virtuals);
2762
2763 if (virtual_base)
2764 BV_VCALL_INDEX (*virtuals)
2765 = get_vcall_index (overrider_target, BINFO_TYPE (virtual_base));
2766 else
2767 BV_VCALL_INDEX (*virtuals) = NULL_TREE;
2768
2769 BV_LOST_PRIMARY (*virtuals) = lost;
2770 }
2771
2772 /* Called from modify_all_vtables via dfs_walk. */
2773
2774 static tree
2775 dfs_modify_vtables (tree binfo, void* data)
2776 {
2777 tree t = (tree) data;
2778 tree virtuals;
2779 tree old_virtuals;
2780 unsigned ix;
2781
2782 if (!TYPE_CONTAINS_VPTR_P (BINFO_TYPE (binfo)))
2783 /* A base without a vtable needs no modification, and its bases
2784 are uninteresting. */
2785 return dfs_skip_bases;
2786
2787 if (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), t)
2788 && !CLASSTYPE_HAS_PRIMARY_BASE_P (t))
2789 /* Don't do the primary vtable, if it's new. */
2790 return NULL_TREE;
2791
2792 if (BINFO_PRIMARY_P (binfo) && !BINFO_VIRTUAL_P (binfo))
2793 /* There's no need to modify the vtable for a non-virtual primary
2794 base; we're not going to use that vtable anyhow. We do still
2795 need to do this for virtual primary bases, as they could become
2796 non-primary in a construction vtable. */
2797 return NULL_TREE;
2798
2799 make_new_vtable (t, binfo);
2800
2801 /* Now, go through each of the virtual functions in the virtual
2802 function table for BINFO. Find the final overrider, and update
2803 the BINFO_VIRTUALS list appropriately. */
2804 for (ix = 0, virtuals = BINFO_VIRTUALS (binfo),
2805 old_virtuals = BINFO_VIRTUALS (TYPE_BINFO (BINFO_TYPE (binfo)));
2806 virtuals;
2807 ix++, virtuals = TREE_CHAIN (virtuals),
2808 old_virtuals = TREE_CHAIN (old_virtuals))
2809 update_vtable_entry_for_fn (t,
2810 binfo,
2811 BV_FN (old_virtuals),
2812 &virtuals, ix);
2813
2814 return NULL_TREE;
2815 }
2816
2817 /* Update all of the primary and secondary vtables for T. Create new
2818 vtables as required, and initialize their RTTI information. Each
2819 of the functions in VIRTUALS is declared in T and may override a
2820 virtual function from a base class; find and modify the appropriate
2821 entries to point to the overriding functions. Returns a list, in
2822 declaration order, of the virtual functions that are declared in T,
2823 but do not appear in the primary base class vtable, and which
2824 should therefore be appended to the end of the vtable for T. */
2825
2826 static tree
2827 modify_all_vtables (tree t, tree virtuals)
2828 {
2829 tree binfo = TYPE_BINFO (t);
2830 tree *fnsp;
2831
2832 /* Mangle the vtable name before entering dfs_walk (c++/51884). */
2833 if (TYPE_CONTAINS_VPTR_P (t))
2834 get_vtable_decl (t, false);
2835
2836 /* Update all of the vtables. */
2837 dfs_walk_once (binfo, dfs_modify_vtables, NULL, t);
2838
2839 /* Add virtual functions not already in our primary vtable. These
2840 will be both those introduced by this class, and those overridden
2841 from secondary bases. It does not include virtuals merely
2842 inherited from secondary bases. */
2843 for (fnsp = &virtuals; *fnsp; )
2844 {
2845 tree fn = TREE_VALUE (*fnsp);
2846
2847 if (!value_member (fn, BINFO_VIRTUALS (binfo))
2848 || DECL_VINDEX (fn) == error_mark_node)
2849 {
2850 /* We don't need to adjust the `this' pointer when
2851 calling this function. */
2852 BV_DELTA (*fnsp) = integer_zero_node;
2853 BV_VCALL_INDEX (*fnsp) = NULL_TREE;
2854
2855 /* This is a function not already in our vtable. Keep it. */
2856 fnsp = &TREE_CHAIN (*fnsp);
2857 }
2858 else
2859 /* We've already got an entry for this function. Skip it. */
2860 *fnsp = TREE_CHAIN (*fnsp);
2861 }
2862
2863 return virtuals;
2864 }
2865
2866 /* Get the base virtual function declarations in T that have the
2867 indicated NAME. */
2868
2869 static void
2870 get_basefndecls (tree name, tree t, vec<tree> *base_fndecls)
2871 {
2872 tree methods;
2873 int n_baseclasses = BINFO_N_BASE_BINFOS (TYPE_BINFO (t));
2874 int i;
2875
2876 /* Find virtual functions in T with the indicated NAME. */
2877 i = lookup_fnfields_1 (t, name);
2878 bool found_decls = false;
2879 if (i != -1)
2880 for (methods = (*CLASSTYPE_METHOD_VEC (t))[i];
2881 methods;
2882 methods = OVL_NEXT (methods))
2883 {
2884 tree method = OVL_CURRENT (methods);
2885
2886 if (TREE_CODE (method) == FUNCTION_DECL
2887 && DECL_VINDEX (method))
2888 {
2889 base_fndecls->safe_push (method);
2890 found_decls = true;
2891 }
2892 }
2893
2894 if (found_decls)
2895 return;
2896
2897 for (i = 0; i < n_baseclasses; i++)
2898 {
2899 tree basetype = BINFO_TYPE (BINFO_BASE_BINFO (TYPE_BINFO (t), i));
2900 get_basefndecls (name, basetype, base_fndecls);
2901 }
2902 }
2903
2904 /* If this declaration supersedes the declaration of
2905 a method declared virtual in the base class, then
2906 mark this field as being virtual as well. */
2907
2908 void
2909 check_for_override (tree decl, tree ctype)
2910 {
2911 bool overrides_found = false;
2912 if (TREE_CODE (decl) == TEMPLATE_DECL)
2913 /* In [temp.mem] we have:
2914
2915 A specialization of a member function template does not
2916 override a virtual function from a base class. */
2917 return;
2918 if ((DECL_DESTRUCTOR_P (decl)
2919 || IDENTIFIER_VIRTUAL_P (DECL_NAME (decl))
2920 || DECL_CONV_FN_P (decl))
2921 && look_for_overrides (ctype, decl)
2922 && !DECL_STATIC_FUNCTION_P (decl))
2923 /* Set DECL_VINDEX to a value that is neither an INTEGER_CST nor
2924 the error_mark_node so that we know it is an overriding
2925 function. */
2926 {
2927 DECL_VINDEX (decl) = decl;
2928 overrides_found = true;
2929 if (warn_override && !DECL_OVERRIDE_P (decl)
2930 && !DECL_DESTRUCTOR_P (decl))
2931 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wsuggest_override,
2932 "%q+D can be marked override", decl);
2933 }
2934
2935 if (DECL_VIRTUAL_P (decl))
2936 {
2937 if (!DECL_VINDEX (decl))
2938 DECL_VINDEX (decl) = error_mark_node;
2939 IDENTIFIER_VIRTUAL_P (DECL_NAME (decl)) = 1;
2940 if (DECL_DESTRUCTOR_P (decl))
2941 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (ctype) = true;
2942 }
2943 else if (DECL_FINAL_P (decl))
2944 error ("%q+#D marked %<final%>, but is not virtual", decl);
2945 if (DECL_OVERRIDE_P (decl) && !overrides_found)
2946 error ("%q+#D marked %<override%>, but does not override", decl);
2947 }
2948
2949 /* Warn about hidden virtual functions that are not overridden in t.
2950 We know that constructors and destructors don't apply. */
2951
2952 static void
2953 warn_hidden (tree t)
2954 {
2955 vec<tree, va_gc> *method_vec = CLASSTYPE_METHOD_VEC (t);
2956 tree fns;
2957 size_t i;
2958
2959 /* We go through each separately named virtual function. */
2960 for (i = CLASSTYPE_FIRST_CONVERSION_SLOT;
2961 vec_safe_iterate (method_vec, i, &fns);
2962 ++i)
2963 {
2964 tree fn;
2965 tree name;
2966 tree fndecl;
2967 tree base_binfo;
2968 tree binfo;
2969 int j;
2970
2971 /* All functions in this slot in the CLASSTYPE_METHOD_VEC will
2972 have the same name. Figure out what name that is. */
2973 name = DECL_NAME (OVL_CURRENT (fns));
2974 /* There are no possibly hidden functions yet. */
2975 auto_vec<tree, 20> base_fndecls;
2976 /* Iterate through all of the base classes looking for possibly
2977 hidden functions. */
2978 for (binfo = TYPE_BINFO (t), j = 0;
2979 BINFO_BASE_ITERATE (binfo, j, base_binfo); j++)
2980 {
2981 tree basetype = BINFO_TYPE (base_binfo);
2982 get_basefndecls (name, basetype, &base_fndecls);
2983 }
2984
2985 /* If there are no functions to hide, continue. */
2986 if (base_fndecls.is_empty ())
2987 continue;
2988
2989 /* Remove any overridden functions. */
2990 for (fn = fns; fn; fn = OVL_NEXT (fn))
2991 {
2992 fndecl = OVL_CURRENT (fn);
2993 if (TREE_CODE (fndecl) == FUNCTION_DECL
2994 && DECL_VINDEX (fndecl))
2995 {
2996 /* If the method from the base class has the same
2997 signature as the method from the derived class, it
2998 has been overridden. */
2999 for (size_t k = 0; k < base_fndecls.length (); k++)
3000 if (base_fndecls[k]
3001 && same_signature_p (fndecl, base_fndecls[k]))
3002 base_fndecls[k] = NULL_TREE;
3003 }
3004 }
3005
3006 /* Now give a warning for all base functions without overriders,
3007 as they are hidden. */
3008 size_t k;
3009 tree base_fndecl;
3010 FOR_EACH_VEC_ELT (base_fndecls, k, base_fndecl)
3011 if (base_fndecl)
3012 {
3013 /* Here we know it is a hider, and no overrider exists. */
3014 warning (OPT_Woverloaded_virtual, "%q+D was hidden", base_fndecl);
3015 warning (OPT_Woverloaded_virtual, " by %q+D", fns);
3016 }
3017 }
3018 }
3019
3020 /* Recursive helper for finish_struct_anon. */
3021
3022 static void
3023 finish_struct_anon_r (tree field, bool complain)
3024 {
3025 bool is_union = TREE_CODE (TREE_TYPE (field)) == UNION_TYPE;
3026 tree elt = TYPE_FIELDS (TREE_TYPE (field));
3027 for (; elt; elt = DECL_CHAIN (elt))
3028 {
3029 /* We're generally only interested in entities the user
3030 declared, but we also find nested classes by noticing
3031 the TYPE_DECL that we create implicitly. You're
3032 allowed to put one anonymous union inside another,
3033 though, so we explicitly tolerate that. We use
3034 TYPE_ANONYMOUS_P rather than ANON_AGGR_TYPE_P so that
3035 we also allow unnamed types used for defining fields. */
3036 if (DECL_ARTIFICIAL (elt)
3037 && (!DECL_IMPLICIT_TYPEDEF_P (elt)
3038 || TYPE_ANONYMOUS_P (TREE_TYPE (elt))))
3039 continue;
3040
3041 if (TREE_CODE (elt) != FIELD_DECL)
3042 {
3043 /* We already complained about static data members in
3044 finish_static_data_member_decl. */
3045 if (complain && TREE_CODE (elt) != VAR_DECL)
3046 {
3047 if (is_union)
3048 permerror (input_location,
3049 "%q+#D invalid; an anonymous union can "
3050 "only have non-static data members", elt);
3051 else
3052 permerror (input_location,
3053 "%q+#D invalid; an anonymous struct can "
3054 "only have non-static data members", elt);
3055 }
3056 continue;
3057 }
3058
3059 if (complain)
3060 {
3061 if (TREE_PRIVATE (elt))
3062 {
3063 if (is_union)
3064 permerror (input_location,
3065 "private member %q+#D in anonymous union", elt);
3066 else
3067 permerror (input_location,
3068 "private member %q+#D in anonymous struct", elt);
3069 }
3070 else if (TREE_PROTECTED (elt))
3071 {
3072 if (is_union)
3073 permerror (input_location,
3074 "protected member %q+#D in anonymous union", elt);
3075 else
3076 permerror (input_location,
3077 "protected member %q+#D in anonymous struct", elt);
3078 }
3079 }
3080
3081 TREE_PRIVATE (elt) = TREE_PRIVATE (field);
3082 TREE_PROTECTED (elt) = TREE_PROTECTED (field);
3083
3084 /* Recurse into the anonymous aggregates to handle correctly
3085 access control (c++/24926):
3086
3087 class A {
3088 union {
3089 union {
3090 int i;
3091 };
3092 };
3093 };
3094
3095 int j=A().i; */
3096 if (DECL_NAME (elt) == NULL_TREE
3097 && ANON_AGGR_TYPE_P (TREE_TYPE (elt)))
3098 finish_struct_anon_r (elt, /*complain=*/false);
3099 }
3100 }
3101
3102 /* Check for things that are invalid. There are probably plenty of other
3103 things we should check for also. */
3104
3105 static void
3106 finish_struct_anon (tree t)
3107 {
3108 for (tree field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
3109 {
3110 if (TREE_STATIC (field))
3111 continue;
3112 if (TREE_CODE (field) != FIELD_DECL)
3113 continue;
3114
3115 if (DECL_NAME (field) == NULL_TREE
3116 && ANON_AGGR_TYPE_P (TREE_TYPE (field)))
3117 finish_struct_anon_r (field, /*complain=*/true);
3118 }
3119 }
3120
3121 /* Add T to CLASSTYPE_DECL_LIST of current_class_type which
3122 will be used later during class template instantiation.
3123 When FRIEND_P is zero, T can be a static member data (VAR_DECL),
3124 a non-static member data (FIELD_DECL), a member function
3125 (FUNCTION_DECL), a nested type (RECORD_TYPE, ENUM_TYPE),
3126 a typedef (TYPE_DECL) or a member class template (TEMPLATE_DECL)
3127 When FRIEND_P is nonzero, T is either a friend class
3128 (RECORD_TYPE, TEMPLATE_DECL) or a friend function
3129 (FUNCTION_DECL, TEMPLATE_DECL). */
3130
3131 void
3132 maybe_add_class_template_decl_list (tree type, tree t, int friend_p)
3133 {
3134 /* Save some memory by not creating TREE_LIST if TYPE is not template. */
3135 if (CLASSTYPE_TEMPLATE_INFO (type))
3136 CLASSTYPE_DECL_LIST (type)
3137 = tree_cons (friend_p ? NULL_TREE : type,
3138 t, CLASSTYPE_DECL_LIST (type));
3139 }
3140
3141 /* This function is called from declare_virt_assop_and_dtor via
3142 dfs_walk_all.
3143
3144 DATA is a type that direcly or indirectly inherits the base
3145 represented by BINFO. If BINFO contains a virtual assignment [copy
3146 assignment or move assigment] operator or a virtual constructor,
3147 declare that function in DATA if it hasn't been already declared. */
3148
3149 static tree
3150 dfs_declare_virt_assop_and_dtor (tree binfo, void *data)
3151 {
3152 tree bv, fn, t = (tree)data;
3153 tree opname = ansi_assopname (NOP_EXPR);
3154
3155 gcc_assert (t && CLASS_TYPE_P (t));
3156 gcc_assert (binfo && TREE_CODE (binfo) == TREE_BINFO);
3157
3158 if (!TYPE_CONTAINS_VPTR_P (BINFO_TYPE (binfo)))
3159 /* A base without a vtable needs no modification, and its bases
3160 are uninteresting. */
3161 return dfs_skip_bases;
3162
3163 if (BINFO_PRIMARY_P (binfo))
3164 /* If this is a primary base, then we have already looked at the
3165 virtual functions of its vtable. */
3166 return NULL_TREE;
3167
3168 for (bv = BINFO_VIRTUALS (binfo); bv; bv = TREE_CHAIN (bv))
3169 {
3170 fn = BV_FN (bv);
3171
3172 if (DECL_NAME (fn) == opname)
3173 {
3174 if (CLASSTYPE_LAZY_COPY_ASSIGN (t))
3175 lazily_declare_fn (sfk_copy_assignment, t);
3176 if (CLASSTYPE_LAZY_MOVE_ASSIGN (t))
3177 lazily_declare_fn (sfk_move_assignment, t);
3178 }
3179 else if (DECL_DESTRUCTOR_P (fn)
3180 && CLASSTYPE_LAZY_DESTRUCTOR (t))
3181 lazily_declare_fn (sfk_destructor, t);
3182 }
3183
3184 return NULL_TREE;
3185 }
3186
3187 /* If the class type T has a direct or indirect base that contains a
3188 virtual assignment operator or a virtual destructor, declare that
3189 function in T if it hasn't been already declared. */
3190
3191 static void
3192 declare_virt_assop_and_dtor (tree t)
3193 {
3194 if (!(TYPE_POLYMORPHIC_P (t)
3195 && (CLASSTYPE_LAZY_COPY_ASSIGN (t)
3196 || CLASSTYPE_LAZY_MOVE_ASSIGN (t)
3197 || CLASSTYPE_LAZY_DESTRUCTOR (t))))
3198 return;
3199
3200 dfs_walk_all (TYPE_BINFO (t),
3201 dfs_declare_virt_assop_and_dtor,
3202 NULL, t);
3203 }
3204
3205 /* Declare the inheriting constructor for class T inherited from base
3206 constructor CTOR with the parameter array PARMS of size NPARMS. */
3207
3208 static void
3209 one_inheriting_sig (tree t, tree ctor, tree *parms, int nparms)
3210 {
3211 /* We don't declare an inheriting ctor that would be a default,
3212 copy or move ctor for derived or base. */
3213 if (nparms == 0)
3214 return;
3215 if (nparms == 1
3216 && TREE_CODE (parms[0]) == REFERENCE_TYPE)
3217 {
3218 tree parm = TYPE_MAIN_VARIANT (TREE_TYPE (parms[0]));
3219 if (parm == t || parm == DECL_CONTEXT (ctor))
3220 return;
3221 }
3222
3223 tree parmlist = void_list_node;
3224 for (int i = nparms - 1; i >= 0; i--)
3225 parmlist = tree_cons (NULL_TREE, parms[i], parmlist);
3226 tree fn = implicitly_declare_fn (sfk_inheriting_constructor,
3227 t, false, ctor, parmlist);
3228 gcc_assert (TYPE_MAIN_VARIANT (t) == t);
3229 if (add_method (t, fn, NULL_TREE))
3230 {
3231 DECL_CHAIN (fn) = TYPE_METHODS (t);
3232 TYPE_METHODS (t) = fn;
3233 }
3234 }
3235
3236 /* Declare all the inheriting constructors for class T inherited from base
3237 constructor CTOR. */
3238
3239 static void
3240 one_inherited_ctor (tree ctor, tree t)
3241 {
3242 tree parms = FUNCTION_FIRST_USER_PARMTYPE (ctor);
3243
3244 tree *new_parms = XALLOCAVEC (tree, list_length (parms));
3245 int i = 0;
3246 for (; parms && parms != void_list_node; parms = TREE_CHAIN (parms))
3247 {
3248 if (TREE_PURPOSE (parms))
3249 one_inheriting_sig (t, ctor, new_parms, i);
3250 new_parms[i++] = TREE_VALUE (parms);
3251 }
3252 one_inheriting_sig (t, ctor, new_parms, i);
3253 if (parms == NULL_TREE)
3254 {
3255 if (warning (OPT_Winherited_variadic_ctor,
3256 "the ellipsis in %qD is not inherited", ctor))
3257 inform (DECL_SOURCE_LOCATION (ctor), "%qD declared here", ctor);
3258 }
3259 }
3260
3261 /* Create default constructors, assignment operators, and so forth for
3262 the type indicated by T, if they are needed. CANT_HAVE_CONST_CTOR,
3263 and CANT_HAVE_CONST_ASSIGNMENT are nonzero if, for whatever reason,
3264 the class cannot have a default constructor, copy constructor
3265 taking a const reference argument, or an assignment operator taking
3266 a const reference, respectively. */
3267
3268 static void
3269 add_implicitly_declared_members (tree t, tree* access_decls,
3270 int cant_have_const_cctor,
3271 int cant_have_const_assignment)
3272 {
3273 bool move_ok = false;
3274
3275 if (cxx_dialect >= cxx11 && !CLASSTYPE_DESTRUCTORS (t)
3276 && !TYPE_HAS_COPY_CTOR (t) && !TYPE_HAS_COPY_ASSIGN (t)
3277 && !type_has_move_constructor (t) && !type_has_move_assign (t))
3278 move_ok = true;
3279
3280 /* Destructor. */
3281 if (!CLASSTYPE_DESTRUCTORS (t))
3282 {
3283 /* In general, we create destructors lazily. */
3284 CLASSTYPE_LAZY_DESTRUCTOR (t) = 1;
3285
3286 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t)
3287 && TYPE_FOR_JAVA (t))
3288 /* But if this is a Java class, any non-trivial destructor is
3289 invalid, even if compiler-generated. Therefore, if the
3290 destructor is non-trivial we create it now. */
3291 lazily_declare_fn (sfk_destructor, t);
3292 }
3293
3294 /* [class.ctor]
3295
3296 If there is no user-declared constructor for a class, a default
3297 constructor is implicitly declared. */
3298 if (! TYPE_HAS_USER_CONSTRUCTOR (t))
3299 {
3300 TYPE_HAS_DEFAULT_CONSTRUCTOR (t) = 1;
3301 CLASSTYPE_LAZY_DEFAULT_CTOR (t) = 1;
3302 if (cxx_dialect >= cxx11)
3303 TYPE_HAS_CONSTEXPR_CTOR (t)
3304 /* This might force the declaration. */
3305 = type_has_constexpr_default_constructor (t);
3306 }
3307
3308 /* [class.ctor]
3309
3310 If a class definition does not explicitly declare a copy
3311 constructor, one is declared implicitly. */
3312 if (! TYPE_HAS_COPY_CTOR (t) && ! TYPE_FOR_JAVA (t))
3313 {
3314 TYPE_HAS_COPY_CTOR (t) = 1;
3315 TYPE_HAS_CONST_COPY_CTOR (t) = !cant_have_const_cctor;
3316 CLASSTYPE_LAZY_COPY_CTOR (t) = 1;
3317 if (move_ok)
3318 CLASSTYPE_LAZY_MOVE_CTOR (t) = 1;
3319 }
3320
3321 /* If there is no assignment operator, one will be created if and
3322 when it is needed. For now, just record whether or not the type
3323 of the parameter to the assignment operator will be a const or
3324 non-const reference. */
3325 if (!TYPE_HAS_COPY_ASSIGN (t) && !TYPE_FOR_JAVA (t))
3326 {
3327 TYPE_HAS_COPY_ASSIGN (t) = 1;
3328 TYPE_HAS_CONST_COPY_ASSIGN (t) = !cant_have_const_assignment;
3329 CLASSTYPE_LAZY_COPY_ASSIGN (t) = 1;
3330 if (move_ok && !LAMBDA_TYPE_P (t))
3331 CLASSTYPE_LAZY_MOVE_ASSIGN (t) = 1;
3332 }
3333
3334 /* We can't be lazy about declaring functions that might override
3335 a virtual function from a base class. */
3336 declare_virt_assop_and_dtor (t);
3337
3338 while (*access_decls)
3339 {
3340 tree using_decl = TREE_VALUE (*access_decls);
3341 tree decl = USING_DECL_DECLS (using_decl);
3342 if (DECL_NAME (using_decl) == ctor_identifier)
3343 {
3344 /* declare, then remove the decl */
3345 tree ctor_list = decl;
3346 location_t loc = input_location;
3347 input_location = DECL_SOURCE_LOCATION (using_decl);
3348 if (ctor_list)
3349 for (; ctor_list; ctor_list = OVL_NEXT (ctor_list))
3350 one_inherited_ctor (OVL_CURRENT (ctor_list), t);
3351 *access_decls = TREE_CHAIN (*access_decls);
3352 input_location = loc;
3353 }
3354 else
3355 access_decls = &TREE_CHAIN (*access_decls);
3356 }
3357 }
3358
3359 /* Subroutine of insert_into_classtype_sorted_fields. Recursively
3360 count the number of fields in TYPE, including anonymous union
3361 members. */
3362
3363 static int
3364 count_fields (tree fields)
3365 {
3366 tree x;
3367 int n_fields = 0;
3368 for (x = fields; x; x = DECL_CHAIN (x))
3369 {
3370 if (TREE_CODE (x) == FIELD_DECL && ANON_AGGR_TYPE_P (TREE_TYPE (x)))
3371 n_fields += count_fields (TYPE_FIELDS (TREE_TYPE (x)));
3372 else
3373 n_fields += 1;
3374 }
3375 return n_fields;
3376 }
3377
3378 /* Subroutine of insert_into_classtype_sorted_fields. Recursively add
3379 all the fields in the TREE_LIST FIELDS to the SORTED_FIELDS_TYPE
3380 elts, starting at offset IDX. */
3381
3382 static int
3383 add_fields_to_record_type (tree fields, struct sorted_fields_type *field_vec, int idx)
3384 {
3385 tree x;
3386 for (x = fields; x; x = DECL_CHAIN (x))
3387 {
3388 if (TREE_CODE (x) == FIELD_DECL && ANON_AGGR_TYPE_P (TREE_TYPE (x)))
3389 idx = add_fields_to_record_type (TYPE_FIELDS (TREE_TYPE (x)), field_vec, idx);
3390 else
3391 field_vec->elts[idx++] = x;
3392 }
3393 return idx;
3394 }
3395
3396 /* Add all of the enum values of ENUMTYPE, to the FIELD_VEC elts,
3397 starting at offset IDX. */
3398
3399 static int
3400 add_enum_fields_to_record_type (tree enumtype,
3401 struct sorted_fields_type *field_vec,
3402 int idx)
3403 {
3404 tree values;
3405 for (values = TYPE_VALUES (enumtype); values; values = TREE_CHAIN (values))
3406 field_vec->elts[idx++] = TREE_VALUE (values);
3407 return idx;
3408 }
3409
3410 /* FIELD is a bit-field. We are finishing the processing for its
3411 enclosing type. Issue any appropriate messages and set appropriate
3412 flags. Returns false if an error has been diagnosed. */
3413
3414 static bool
3415 check_bitfield_decl (tree field)
3416 {
3417 tree type = TREE_TYPE (field);
3418 tree w;
3419
3420 /* Extract the declared width of the bitfield, which has been
3421 temporarily stashed in DECL_INITIAL. */
3422 w = DECL_INITIAL (field);
3423 gcc_assert (w != NULL_TREE);
3424 /* Remove the bit-field width indicator so that the rest of the
3425 compiler does not treat that value as an initializer. */
3426 DECL_INITIAL (field) = NULL_TREE;
3427
3428 /* Detect invalid bit-field type. */
3429 if (!INTEGRAL_OR_ENUMERATION_TYPE_P (type))
3430 {
3431 error ("bit-field %q+#D with non-integral type", field);
3432 w = error_mark_node;
3433 }
3434 else
3435 {
3436 location_t loc = input_location;
3437 /* Avoid the non_lvalue wrapper added by fold for PLUS_EXPRs. */
3438 STRIP_NOPS (w);
3439
3440 /* detect invalid field size. */
3441 input_location = DECL_SOURCE_LOCATION (field);
3442 w = cxx_constant_value (w);
3443 input_location = loc;
3444
3445 if (TREE_CODE (w) != INTEGER_CST)
3446 {
3447 error ("bit-field %q+D width not an integer constant", field);
3448 w = error_mark_node;
3449 }
3450 else if (tree_int_cst_sgn (w) < 0)
3451 {
3452 error ("negative width in bit-field %q+D", field);
3453 w = error_mark_node;
3454 }
3455 else if (integer_zerop (w) && DECL_NAME (field) != 0)
3456 {
3457 error ("zero width for bit-field %q+D", field);
3458 w = error_mark_node;
3459 }
3460 else if ((TREE_CODE (type) != ENUMERAL_TYPE
3461 && TREE_CODE (type) != BOOLEAN_TYPE
3462 && compare_tree_int (w, TYPE_PRECISION (type)) > 0)
3463 || ((TREE_CODE (type) == ENUMERAL_TYPE
3464 || TREE_CODE (type) == BOOLEAN_TYPE)
3465 && tree_int_cst_lt (TYPE_SIZE (type), w)))
3466 warning (0, "width of %q+D exceeds its type", field);
3467 else if (TREE_CODE (type) == ENUMERAL_TYPE
3468 && (0 > (compare_tree_int
3469 (w, TYPE_PRECISION (ENUM_UNDERLYING_TYPE (type))))))
3470 warning (0, "%q+D is too small to hold all values of %q#T", field, type);
3471 }
3472
3473 if (w != error_mark_node)
3474 {
3475 DECL_SIZE (field) = convert (bitsizetype, w);
3476 DECL_BIT_FIELD (field) = 1;
3477 return true;
3478 }
3479 else
3480 {
3481 /* Non-bit-fields are aligned for their type. */
3482 DECL_BIT_FIELD (field) = 0;
3483 CLEAR_DECL_C_BIT_FIELD (field);
3484 return false;
3485 }
3486 }
3487
3488 /* FIELD is a non bit-field. We are finishing the processing for its
3489 enclosing type T. Issue any appropriate messages and set appropriate
3490 flags. */
3491
3492 static void
3493 check_field_decl (tree field,
3494 tree t,
3495 int* cant_have_const_ctor,
3496 int* no_const_asn_ref,
3497 int* any_default_members)
3498 {
3499 tree type = strip_array_types (TREE_TYPE (field));
3500
3501 /* In C++98 an anonymous union cannot contain any fields which would change
3502 the settings of CANT_HAVE_CONST_CTOR and friends. */
3503 if (ANON_UNION_TYPE_P (type) && cxx_dialect < cxx11)
3504 ;
3505 /* And, we don't set TYPE_HAS_CONST_COPY_CTOR, etc., for anonymous
3506 structs. So, we recurse through their fields here. */
3507 else if (ANON_AGGR_TYPE_P (type))
3508 {
3509 tree fields;
3510
3511 for (fields = TYPE_FIELDS (type); fields; fields = DECL_CHAIN (fields))
3512 if (TREE_CODE (fields) == FIELD_DECL && !DECL_C_BIT_FIELD (field))
3513 check_field_decl (fields, t, cant_have_const_ctor,
3514 no_const_asn_ref, any_default_members);
3515 }
3516 /* Check members with class type for constructors, destructors,
3517 etc. */
3518 else if (CLASS_TYPE_P (type))
3519 {
3520 /* Never let anything with uninheritable virtuals
3521 make it through without complaint. */
3522 abstract_virtuals_error (field, type);
3523
3524 if (TREE_CODE (t) == UNION_TYPE && cxx_dialect < cxx11)
3525 {
3526 static bool warned;
3527 int oldcount = errorcount;
3528 if (TYPE_NEEDS_CONSTRUCTING (type))
3529 error ("member %q+#D with constructor not allowed in union",
3530 field);
3531 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
3532 error ("member %q+#D with destructor not allowed in union", field);
3533 if (TYPE_HAS_COMPLEX_COPY_ASSIGN (type))
3534 error ("member %q+#D with copy assignment operator not allowed in union",
3535 field);
3536 if (!warned && errorcount > oldcount)
3537 {
3538 inform (DECL_SOURCE_LOCATION (field), "unrestricted unions "
3539 "only available with -std=c++11 or -std=gnu++11");
3540 warned = true;
3541 }
3542 }
3543 else
3544 {
3545 TYPE_NEEDS_CONSTRUCTING (t) |= TYPE_NEEDS_CONSTRUCTING (type);
3546 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t)
3547 |= TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type);
3548 TYPE_HAS_COMPLEX_COPY_ASSIGN (t)
3549 |= (TYPE_HAS_COMPLEX_COPY_ASSIGN (type)
3550 || !TYPE_HAS_COPY_ASSIGN (type));
3551 TYPE_HAS_COMPLEX_COPY_CTOR (t) |= (TYPE_HAS_COMPLEX_COPY_CTOR (type)
3552 || !TYPE_HAS_COPY_CTOR (type));
3553 TYPE_HAS_COMPLEX_MOVE_ASSIGN (t) |= TYPE_HAS_COMPLEX_MOVE_ASSIGN (type);
3554 TYPE_HAS_COMPLEX_MOVE_CTOR (t) |= TYPE_HAS_COMPLEX_MOVE_CTOR (type);
3555 TYPE_HAS_COMPLEX_DFLT (t) |= (!TYPE_HAS_DEFAULT_CONSTRUCTOR (type)
3556 || TYPE_HAS_COMPLEX_DFLT (type));
3557 }
3558
3559 if (TYPE_HAS_COPY_CTOR (type)
3560 && !TYPE_HAS_CONST_COPY_CTOR (type))
3561 *cant_have_const_ctor = 1;
3562
3563 if (TYPE_HAS_COPY_ASSIGN (type)
3564 && !TYPE_HAS_CONST_COPY_ASSIGN (type))
3565 *no_const_asn_ref = 1;
3566 }
3567
3568 check_abi_tags (t, field);
3569
3570 if (DECL_INITIAL (field) != NULL_TREE)
3571 {
3572 /* `build_class_init_list' does not recognize
3573 non-FIELD_DECLs. */
3574 if (TREE_CODE (t) == UNION_TYPE && *any_default_members != 0)
3575 error ("multiple fields in union %qT initialized", t);
3576 *any_default_members = 1;
3577 }
3578 }
3579
3580 /* Check the data members (both static and non-static), class-scoped
3581 typedefs, etc., appearing in the declaration of T. Issue
3582 appropriate diagnostics. Sets ACCESS_DECLS to a list (in
3583 declaration order) of access declarations; each TREE_VALUE in this
3584 list is a USING_DECL.
3585
3586 In addition, set the following flags:
3587
3588 EMPTY_P
3589 The class is empty, i.e., contains no non-static data members.
3590
3591 CANT_HAVE_CONST_CTOR_P
3592 This class cannot have an implicitly generated copy constructor
3593 taking a const reference.
3594
3595 CANT_HAVE_CONST_ASN_REF
3596 This class cannot have an implicitly generated assignment
3597 operator taking a const reference.
3598
3599 All of these flags should be initialized before calling this
3600 function.
3601
3602 Returns a pointer to the end of the TYPE_FIELDs chain; additional
3603 fields can be added by adding to this chain. */
3604
3605 static void
3606 check_field_decls (tree t, tree *access_decls,
3607 int *cant_have_const_ctor_p,
3608 int *no_const_asn_ref_p)
3609 {
3610 tree *field;
3611 tree *next;
3612 bool has_pointers;
3613 int any_default_members;
3614 int cant_pack = 0;
3615 int field_access = -1;
3616
3617 /* Assume there are no access declarations. */
3618 *access_decls = NULL_TREE;
3619 /* Assume this class has no pointer members. */
3620 has_pointers = false;
3621 /* Assume none of the members of this class have default
3622 initializations. */
3623 any_default_members = 0;
3624
3625 for (field = &TYPE_FIELDS (t); *field; field = next)
3626 {
3627 tree x = *field;
3628 tree type = TREE_TYPE (x);
3629 int this_field_access;
3630
3631 next = &DECL_CHAIN (x);
3632
3633 if (TREE_CODE (x) == USING_DECL)
3634 {
3635 /* Save the access declarations for our caller. */
3636 *access_decls = tree_cons (NULL_TREE, x, *access_decls);
3637 continue;
3638 }
3639
3640 if (TREE_CODE (x) == TYPE_DECL
3641 || TREE_CODE (x) == TEMPLATE_DECL)
3642 continue;
3643
3644 /* If we've gotten this far, it's a data member, possibly static,
3645 or an enumerator. */
3646 if (TREE_CODE (x) != CONST_DECL)
3647 DECL_CONTEXT (x) = t;
3648
3649 /* When this goes into scope, it will be a non-local reference. */
3650 DECL_NONLOCAL (x) = 1;
3651
3652 if (TREE_CODE (t) == UNION_TYPE
3653 && cxx_dialect < cxx11)
3654 {
3655 /* [class.union] (C++98)
3656
3657 If a union contains a static data member, or a member of
3658 reference type, the program is ill-formed.
3659
3660 In C++11 this limitation doesn't exist anymore. */
3661 if (VAR_P (x))
3662 {
3663 error ("in C++98 %q+D may not be static because it is "
3664 "a member of a union", x);
3665 continue;
3666 }
3667 if (TREE_CODE (type) == REFERENCE_TYPE)
3668 {
3669 error ("in C++98 %q+D may not have reference type %qT "
3670 "because it is a member of a union", x, type);
3671 continue;
3672 }
3673 }
3674
3675 /* Perform error checking that did not get done in
3676 grokdeclarator. */
3677 if (TREE_CODE (type) == FUNCTION_TYPE)
3678 {
3679 error ("field %q+D invalidly declared function type", x);
3680 type = build_pointer_type (type);
3681 TREE_TYPE (x) = type;
3682 }
3683 else if (TREE_CODE (type) == METHOD_TYPE)
3684 {
3685 error ("field %q+D invalidly declared method type", x);
3686 type = build_pointer_type (type);
3687 TREE_TYPE (x) = type;
3688 }
3689
3690 if (type == error_mark_node)
3691 continue;
3692
3693 if (TREE_CODE (x) == CONST_DECL || VAR_P (x))
3694 continue;
3695
3696 /* Now it can only be a FIELD_DECL. */
3697
3698 if (TREE_PRIVATE (x) || TREE_PROTECTED (x))
3699 CLASSTYPE_NON_AGGREGATE (t) = 1;
3700
3701 /* If at least one non-static data member is non-literal, the whole
3702 class becomes non-literal. Per Core/1453, volatile non-static
3703 data members and base classes are also not allowed.
3704 Note: if the type is incomplete we will complain later on. */
3705 if (COMPLETE_TYPE_P (type)
3706 && (!literal_type_p (type) || CP_TYPE_VOLATILE_P (type)))
3707 CLASSTYPE_LITERAL_P (t) = false;
3708
3709 /* A standard-layout class is a class that:
3710 ...
3711 has the same access control (Clause 11) for all non-static data members,
3712 ... */
3713 this_field_access = TREE_PROTECTED (x) ? 1 : TREE_PRIVATE (x) ? 2 : 0;
3714 if (field_access == -1)
3715 field_access = this_field_access;
3716 else if (this_field_access != field_access)
3717 CLASSTYPE_NON_STD_LAYOUT (t) = 1;
3718
3719 /* If this is of reference type, check if it needs an init. */
3720 if (TREE_CODE (type) == REFERENCE_TYPE)
3721 {
3722 CLASSTYPE_NON_LAYOUT_POD_P (t) = 1;
3723 CLASSTYPE_NON_STD_LAYOUT (t) = 1;
3724 if (DECL_INITIAL (x) == NULL_TREE)
3725 SET_CLASSTYPE_REF_FIELDS_NEED_INIT (t, 1);
3726 if (cxx_dialect < cxx11)
3727 {
3728 /* ARM $12.6.2: [A member initializer list] (or, for an
3729 aggregate, initialization by a brace-enclosed list) is the
3730 only way to initialize nonstatic const and reference
3731 members. */
3732 TYPE_HAS_COMPLEX_COPY_ASSIGN (t) = 1;
3733 TYPE_HAS_COMPLEX_MOVE_ASSIGN (t) = 1;
3734 }
3735 }
3736
3737 type = strip_array_types (type);
3738
3739 if (TYPE_PACKED (t))
3740 {
3741 if (!layout_pod_type_p (type) && !TYPE_PACKED (type))
3742 {
3743 warning
3744 (0,
3745 "ignoring packed attribute because of unpacked non-POD field %q+#D",
3746 x);
3747 cant_pack = 1;
3748 }
3749 else if (DECL_C_BIT_FIELD (x)
3750 || TYPE_ALIGN (TREE_TYPE (x)) > BITS_PER_UNIT)
3751 DECL_PACKED (x) = 1;
3752 }
3753
3754 if (DECL_C_BIT_FIELD (x) && integer_zerop (DECL_INITIAL (x)))
3755 /* We don't treat zero-width bitfields as making a class
3756 non-empty. */
3757 ;
3758 else
3759 {
3760 /* The class is non-empty. */
3761 CLASSTYPE_EMPTY_P (t) = 0;
3762 /* The class is not even nearly empty. */
3763 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
3764 /* If one of the data members contains an empty class,
3765 so does T. */
3766 if (CLASS_TYPE_P (type)
3767 && CLASSTYPE_CONTAINS_EMPTY_CLASS_P (type))
3768 CLASSTYPE_CONTAINS_EMPTY_CLASS_P (t) = 1;
3769 }
3770
3771 /* This is used by -Weffc++ (see below). Warn only for pointers
3772 to members which might hold dynamic memory. So do not warn
3773 for pointers to functions or pointers to members. */
3774 if (TYPE_PTR_P (type)
3775 && !TYPE_PTRFN_P (type))
3776 has_pointers = true;
3777
3778 if (CLASS_TYPE_P (type))
3779 {
3780 if (CLASSTYPE_REF_FIELDS_NEED_INIT (type))
3781 SET_CLASSTYPE_REF_FIELDS_NEED_INIT (t, 1);
3782 if (CLASSTYPE_READONLY_FIELDS_NEED_INIT (type))
3783 SET_CLASSTYPE_READONLY_FIELDS_NEED_INIT (t, 1);
3784 }
3785
3786 if (DECL_MUTABLE_P (x) || TYPE_HAS_MUTABLE_P (type))
3787 CLASSTYPE_HAS_MUTABLE (t) = 1;
3788
3789 if (DECL_MUTABLE_P (x))
3790 {
3791 if (CP_TYPE_CONST_P (type))
3792 {
3793 error ("member %q+D cannot be declared both %<const%> "
3794 "and %<mutable%>", x);
3795 continue;
3796 }
3797 if (TREE_CODE (type) == REFERENCE_TYPE)
3798 {
3799 error ("member %q+D cannot be declared as a %<mutable%> "
3800 "reference", x);
3801 continue;
3802 }
3803 }
3804
3805 if (! layout_pod_type_p (type))
3806 /* DR 148 now allows pointers to members (which are POD themselves),
3807 to be allowed in POD structs. */
3808 CLASSTYPE_NON_LAYOUT_POD_P (t) = 1;
3809
3810 if (!std_layout_type_p (type))
3811 CLASSTYPE_NON_STD_LAYOUT (t) = 1;
3812
3813 if (! zero_init_p (type))
3814 CLASSTYPE_NON_ZERO_INIT_P (t) = 1;
3815
3816 /* We set DECL_C_BIT_FIELD in grokbitfield.
3817 If the type and width are valid, we'll also set DECL_BIT_FIELD. */
3818 if (! DECL_C_BIT_FIELD (x) || ! check_bitfield_decl (x))
3819 check_field_decl (x, t,
3820 cant_have_const_ctor_p,
3821 no_const_asn_ref_p,
3822 &any_default_members);
3823
3824 /* Now that we've removed bit-field widths from DECL_INITIAL,
3825 anything left in DECL_INITIAL is an NSDMI that makes the class
3826 non-aggregate in C++11. */
3827 if (DECL_INITIAL (x) && cxx_dialect < cxx14)
3828 CLASSTYPE_NON_AGGREGATE (t) = true;
3829
3830 /* If any field is const, the structure type is pseudo-const. */
3831 if (CP_TYPE_CONST_P (type))
3832 {
3833 C_TYPE_FIELDS_READONLY (t) = 1;
3834 if (DECL_INITIAL (x) == NULL_TREE)
3835 SET_CLASSTYPE_READONLY_FIELDS_NEED_INIT (t, 1);
3836 if (cxx_dialect < cxx11)
3837 {
3838 /* ARM $12.6.2: [A member initializer list] (or, for an
3839 aggregate, initialization by a brace-enclosed list) is the
3840 only way to initialize nonstatic const and reference
3841 members. */
3842 TYPE_HAS_COMPLEX_COPY_ASSIGN (t) = 1;
3843 TYPE_HAS_COMPLEX_MOVE_ASSIGN (t) = 1;
3844 }
3845 }
3846 /* A field that is pseudo-const makes the structure likewise. */
3847 else if (CLASS_TYPE_P (type))
3848 {
3849 C_TYPE_FIELDS_READONLY (t) |= C_TYPE_FIELDS_READONLY (type);
3850 SET_CLASSTYPE_READONLY_FIELDS_NEED_INIT (t,
3851 CLASSTYPE_READONLY_FIELDS_NEED_INIT (t)
3852 | CLASSTYPE_READONLY_FIELDS_NEED_INIT (type));
3853 }
3854
3855 /* Core issue 80: A nonstatic data member is required to have a
3856 different name from the class iff the class has a
3857 user-declared constructor. */
3858 if (constructor_name_p (DECL_NAME (x), t)
3859 && TYPE_HAS_USER_CONSTRUCTOR (t))
3860 permerror (input_location, "field %q+#D with same name as class", x);
3861 }
3862
3863 /* Effective C++ rule 11: if a class has dynamic memory held by pointers,
3864 it should also define a copy constructor and an assignment operator to
3865 implement the correct copy semantic (deep vs shallow, etc.). As it is
3866 not feasible to check whether the constructors do allocate dynamic memory
3867 and store it within members, we approximate the warning like this:
3868
3869 -- Warn only if there are members which are pointers
3870 -- Warn only if there is a non-trivial constructor (otherwise,
3871 there cannot be memory allocated).
3872 -- Warn only if there is a non-trivial destructor. We assume that the
3873 user at least implemented the cleanup correctly, and a destructor
3874 is needed to free dynamic memory.
3875
3876 This seems enough for practical purposes. */
3877 if (warn_ecpp
3878 && has_pointers
3879 && TYPE_HAS_USER_CONSTRUCTOR (t)
3880 && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t)
3881 && !(TYPE_HAS_COPY_CTOR (t) && TYPE_HAS_COPY_ASSIGN (t)))
3882 {
3883 warning (OPT_Weffc__, "%q#T has pointer data members", t);
3884
3885 if (! TYPE_HAS_COPY_CTOR (t))
3886 {
3887 warning (OPT_Weffc__,
3888 " but does not override %<%T(const %T&)%>", t, t);
3889 if (!TYPE_HAS_COPY_ASSIGN (t))
3890 warning (OPT_Weffc__, " or %<operator=(const %T&)%>", t);
3891 }
3892 else if (! TYPE_HAS_COPY_ASSIGN (t))
3893 warning (OPT_Weffc__,
3894 " but does not override %<operator=(const %T&)%>", t);
3895 }
3896
3897 /* Non-static data member initializers make the default constructor
3898 non-trivial. */
3899 if (any_default_members)
3900 {
3901 TYPE_NEEDS_CONSTRUCTING (t) = true;
3902 TYPE_HAS_COMPLEX_DFLT (t) = true;
3903 }
3904
3905 /* If any of the fields couldn't be packed, unset TYPE_PACKED. */
3906 if (cant_pack)
3907 TYPE_PACKED (t) = 0;
3908
3909 /* Check anonymous struct/anonymous union fields. */
3910 finish_struct_anon (t);
3911
3912 /* We've built up the list of access declarations in reverse order.
3913 Fix that now. */
3914 *access_decls = nreverse (*access_decls);
3915 }
3916
3917 /* If TYPE is an empty class type, records its OFFSET in the table of
3918 OFFSETS. */
3919
3920 static int
3921 record_subobject_offset (tree type, tree offset, splay_tree offsets)
3922 {
3923 splay_tree_node n;
3924
3925 if (!is_empty_class (type))
3926 return 0;
3927
3928 /* Record the location of this empty object in OFFSETS. */
3929 n = splay_tree_lookup (offsets, (splay_tree_key) offset);
3930 if (!n)
3931 n = splay_tree_insert (offsets,
3932 (splay_tree_key) offset,
3933 (splay_tree_value) NULL_TREE);
3934 n->value = ((splay_tree_value)
3935 tree_cons (NULL_TREE,
3936 type,
3937 (tree) n->value));
3938
3939 return 0;
3940 }
3941
3942 /* Returns nonzero if TYPE is an empty class type and there is
3943 already an entry in OFFSETS for the same TYPE as the same OFFSET. */
3944
3945 static int
3946 check_subobject_offset (tree type, tree offset, splay_tree offsets)
3947 {
3948 splay_tree_node n;
3949 tree t;
3950
3951 if (!is_empty_class (type))
3952 return 0;
3953
3954 /* Record the location of this empty object in OFFSETS. */
3955 n = splay_tree_lookup (offsets, (splay_tree_key) offset);
3956 if (!n)
3957 return 0;
3958
3959 for (t = (tree) n->value; t; t = TREE_CHAIN (t))
3960 if (same_type_p (TREE_VALUE (t), type))
3961 return 1;
3962
3963 return 0;
3964 }
3965
3966 /* Walk through all the subobjects of TYPE (located at OFFSET). Call
3967 F for every subobject, passing it the type, offset, and table of
3968 OFFSETS. If VBASES_P is one, then virtual non-primary bases should
3969 be traversed.
3970
3971 If MAX_OFFSET is non-NULL, then subobjects with an offset greater
3972 than MAX_OFFSET will not be walked.
3973
3974 If F returns a nonzero value, the traversal ceases, and that value
3975 is returned. Otherwise, returns zero. */
3976
3977 static int
3978 walk_subobject_offsets (tree type,
3979 subobject_offset_fn f,
3980 tree offset,
3981 splay_tree offsets,
3982 tree max_offset,
3983 int vbases_p)
3984 {
3985 int r = 0;
3986 tree type_binfo = NULL_TREE;
3987
3988 /* If this OFFSET is bigger than the MAX_OFFSET, then we should
3989 stop. */
3990 if (max_offset && tree_int_cst_lt (max_offset, offset))
3991 return 0;
3992
3993 if (type == error_mark_node)
3994 return 0;
3995
3996 if (!TYPE_P (type))
3997 {
3998 type_binfo = type;
3999 type = BINFO_TYPE (type);
4000 }
4001
4002 if (CLASS_TYPE_P (type))
4003 {
4004 tree field;
4005 tree binfo;
4006 int i;
4007
4008 /* Avoid recursing into objects that are not interesting. */
4009 if (!CLASSTYPE_CONTAINS_EMPTY_CLASS_P (type))
4010 return 0;
4011
4012 /* Record the location of TYPE. */
4013 r = (*f) (type, offset, offsets);
4014 if (r)
4015 return r;
4016
4017 /* Iterate through the direct base classes of TYPE. */
4018 if (!type_binfo)
4019 type_binfo = TYPE_BINFO (type);
4020 for (i = 0; BINFO_BASE_ITERATE (type_binfo, i, binfo); i++)
4021 {
4022 tree binfo_offset;
4023
4024 if (BINFO_VIRTUAL_P (binfo))
4025 continue;
4026
4027 tree orig_binfo;
4028 /* We cannot rely on BINFO_OFFSET being set for the base
4029 class yet, but the offsets for direct non-virtual
4030 bases can be calculated by going back to the TYPE. */
4031 orig_binfo = BINFO_BASE_BINFO (TYPE_BINFO (type), i);
4032 binfo_offset = size_binop (PLUS_EXPR,
4033 offset,
4034 BINFO_OFFSET (orig_binfo));
4035
4036 r = walk_subobject_offsets (binfo,
4037 f,
4038 binfo_offset,
4039 offsets,
4040 max_offset,
4041 /*vbases_p=*/0);
4042 if (r)
4043 return r;
4044 }
4045
4046 if (CLASSTYPE_VBASECLASSES (type))
4047 {
4048 unsigned ix;
4049 vec<tree, va_gc> *vbases;
4050
4051 /* Iterate through the virtual base classes of TYPE. In G++
4052 3.2, we included virtual bases in the direct base class
4053 loop above, which results in incorrect results; the
4054 correct offsets for virtual bases are only known when
4055 working with the most derived type. */
4056 if (vbases_p)
4057 for (vbases = CLASSTYPE_VBASECLASSES (type), ix = 0;
4058 vec_safe_iterate (vbases, ix, &binfo); ix++)
4059 {
4060 r = walk_subobject_offsets (binfo,
4061 f,
4062 size_binop (PLUS_EXPR,
4063 offset,
4064 BINFO_OFFSET (binfo)),
4065 offsets,
4066 max_offset,
4067 /*vbases_p=*/0);
4068 if (r)
4069 return r;
4070 }
4071 else
4072 {
4073 /* We still have to walk the primary base, if it is
4074 virtual. (If it is non-virtual, then it was walked
4075 above.) */
4076 tree vbase = get_primary_binfo (type_binfo);
4077
4078 if (vbase && BINFO_VIRTUAL_P (vbase)
4079 && BINFO_PRIMARY_P (vbase)
4080 && BINFO_INHERITANCE_CHAIN (vbase) == type_binfo)
4081 {
4082 r = (walk_subobject_offsets
4083 (vbase, f, offset,
4084 offsets, max_offset, /*vbases_p=*/0));
4085 if (r)
4086 return r;
4087 }
4088 }
4089 }
4090
4091 /* Iterate through the fields of TYPE. */
4092 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
4093 if (TREE_CODE (field) == FIELD_DECL
4094 && TREE_TYPE (field) != error_mark_node
4095 && !DECL_ARTIFICIAL (field))
4096 {
4097 tree field_offset;
4098
4099 field_offset = byte_position (field);
4100
4101 r = walk_subobject_offsets (TREE_TYPE (field),
4102 f,
4103 size_binop (PLUS_EXPR,
4104 offset,
4105 field_offset),
4106 offsets,
4107 max_offset,
4108 /*vbases_p=*/1);
4109 if (r)
4110 return r;
4111 }
4112 }
4113 else if (TREE_CODE (type) == ARRAY_TYPE)
4114 {
4115 tree element_type = strip_array_types (type);
4116 tree domain = TYPE_DOMAIN (type);
4117 tree index;
4118
4119 /* Avoid recursing into objects that are not interesting. */
4120 if (!CLASS_TYPE_P (element_type)
4121 || !CLASSTYPE_CONTAINS_EMPTY_CLASS_P (element_type))
4122 return 0;
4123
4124 /* Step through each of the elements in the array. */
4125 for (index = size_zero_node;
4126 !tree_int_cst_lt (TYPE_MAX_VALUE (domain), index);
4127 index = size_binop (PLUS_EXPR, index, size_one_node))
4128 {
4129 r = walk_subobject_offsets (TREE_TYPE (type),
4130 f,
4131 offset,
4132 offsets,
4133 max_offset,
4134 /*vbases_p=*/1);
4135 if (r)
4136 return r;
4137 offset = size_binop (PLUS_EXPR, offset,
4138 TYPE_SIZE_UNIT (TREE_TYPE (type)));
4139 /* If this new OFFSET is bigger than the MAX_OFFSET, then
4140 there's no point in iterating through the remaining
4141 elements of the array. */
4142 if (max_offset && tree_int_cst_lt (max_offset, offset))
4143 break;
4144 }
4145 }
4146
4147 return 0;
4148 }
4149
4150 /* Record all of the empty subobjects of TYPE (either a type or a
4151 binfo). If IS_DATA_MEMBER is true, then a non-static data member
4152 is being placed at OFFSET; otherwise, it is a base class that is
4153 being placed at OFFSET. */
4154
4155 static void
4156 record_subobject_offsets (tree type,
4157 tree offset,
4158 splay_tree offsets,
4159 bool is_data_member)
4160 {
4161 tree max_offset;
4162 /* If recording subobjects for a non-static data member or a
4163 non-empty base class , we do not need to record offsets beyond
4164 the size of the biggest empty class. Additional data members
4165 will go at the end of the class. Additional base classes will go
4166 either at offset zero (if empty, in which case they cannot
4167 overlap with offsets past the size of the biggest empty class) or
4168 at the end of the class.
4169
4170 However, if we are placing an empty base class, then we must record
4171 all offsets, as either the empty class is at offset zero (where
4172 other empty classes might later be placed) or at the end of the
4173 class (where other objects might then be placed, so other empty
4174 subobjects might later overlap). */
4175 if (is_data_member
4176 || !is_empty_class (BINFO_TYPE (type)))
4177 max_offset = sizeof_biggest_empty_class;
4178 else
4179 max_offset = NULL_TREE;
4180 walk_subobject_offsets (type, record_subobject_offset, offset,
4181 offsets, max_offset, is_data_member);
4182 }
4183
4184 /* Returns nonzero if any of the empty subobjects of TYPE (located at
4185 OFFSET) conflict with entries in OFFSETS. If VBASES_P is nonzero,
4186 virtual bases of TYPE are examined. */
4187
4188 static int
4189 layout_conflict_p (tree type,
4190 tree offset,
4191 splay_tree offsets,
4192 int vbases_p)
4193 {
4194 splay_tree_node max_node;
4195
4196 /* Get the node in OFFSETS that indicates the maximum offset where
4197 an empty subobject is located. */
4198 max_node = splay_tree_max (offsets);
4199 /* If there aren't any empty subobjects, then there's no point in
4200 performing this check. */
4201 if (!max_node)
4202 return 0;
4203
4204 return walk_subobject_offsets (type, check_subobject_offset, offset,
4205 offsets, (tree) (max_node->key),
4206 vbases_p);
4207 }
4208
4209 /* DECL is a FIELD_DECL corresponding either to a base subobject of a
4210 non-static data member of the type indicated by RLI. BINFO is the
4211 binfo corresponding to the base subobject, OFFSETS maps offsets to
4212 types already located at those offsets. This function determines
4213 the position of the DECL. */
4214
4215 static void
4216 layout_nonempty_base_or_field (record_layout_info rli,
4217 tree decl,
4218 tree binfo,
4219 splay_tree offsets)
4220 {
4221 tree offset = NULL_TREE;
4222 bool field_p;
4223 tree type;
4224
4225 if (binfo)
4226 {
4227 /* For the purposes of determining layout conflicts, we want to
4228 use the class type of BINFO; TREE_TYPE (DECL) will be the
4229 CLASSTYPE_AS_BASE version, which does not contain entries for
4230 zero-sized bases. */
4231 type = TREE_TYPE (binfo);
4232 field_p = false;
4233 }
4234 else
4235 {
4236 type = TREE_TYPE (decl);
4237 field_p = true;
4238 }
4239
4240 /* Try to place the field. It may take more than one try if we have
4241 a hard time placing the field without putting two objects of the
4242 same type at the same address. */
4243 while (1)
4244 {
4245 struct record_layout_info_s old_rli = *rli;
4246
4247 /* Place this field. */
4248 place_field (rli, decl);
4249 offset = byte_position (decl);
4250
4251 /* We have to check to see whether or not there is already
4252 something of the same type at the offset we're about to use.
4253 For example, consider:
4254
4255 struct S {};
4256 struct T : public S { int i; };
4257 struct U : public S, public T {};
4258
4259 Here, we put S at offset zero in U. Then, we can't put T at
4260 offset zero -- its S component would be at the same address
4261 as the S we already allocated. So, we have to skip ahead.
4262 Since all data members, including those whose type is an
4263 empty class, have nonzero size, any overlap can happen only
4264 with a direct or indirect base-class -- it can't happen with
4265 a data member. */
4266 /* In a union, overlap is permitted; all members are placed at
4267 offset zero. */
4268 if (TREE_CODE (rli->t) == UNION_TYPE)
4269 break;
4270 if (layout_conflict_p (field_p ? type : binfo, offset,
4271 offsets, field_p))
4272 {
4273 /* Strip off the size allocated to this field. That puts us
4274 at the first place we could have put the field with
4275 proper alignment. */
4276 *rli = old_rli;
4277
4278 /* Bump up by the alignment required for the type. */
4279 rli->bitpos
4280 = size_binop (PLUS_EXPR, rli->bitpos,
4281 bitsize_int (binfo
4282 ? CLASSTYPE_ALIGN (type)
4283 : TYPE_ALIGN (type)));
4284 normalize_rli (rli);
4285 }
4286 else
4287 /* There was no conflict. We're done laying out this field. */
4288 break;
4289 }
4290
4291 /* Now that we know where it will be placed, update its
4292 BINFO_OFFSET. */
4293 if (binfo && CLASS_TYPE_P (BINFO_TYPE (binfo)))
4294 /* Indirect virtual bases may have a nonzero BINFO_OFFSET at
4295 this point because their BINFO_OFFSET is copied from another
4296 hierarchy. Therefore, we may not need to add the entire
4297 OFFSET. */
4298 propagate_binfo_offsets (binfo,
4299 size_diffop_loc (input_location,
4300 convert (ssizetype, offset),
4301 convert (ssizetype,
4302 BINFO_OFFSET (binfo))));
4303 }
4304
4305 /* Returns true if TYPE is empty and OFFSET is nonzero. */
4306
4307 static int
4308 empty_base_at_nonzero_offset_p (tree type,
4309 tree offset,
4310 splay_tree /*offsets*/)
4311 {
4312 return is_empty_class (type) && !integer_zerop (offset);
4313 }
4314
4315 /* Layout the empty base BINFO. EOC indicates the byte currently just
4316 past the end of the class, and should be correctly aligned for a
4317 class of the type indicated by BINFO; OFFSETS gives the offsets of
4318 the empty bases allocated so far. T is the most derived
4319 type. Return nonzero iff we added it at the end. */
4320
4321 static bool
4322 layout_empty_base (record_layout_info rli, tree binfo,
4323 tree eoc, splay_tree offsets)
4324 {
4325 tree alignment;
4326 tree basetype = BINFO_TYPE (binfo);
4327 bool atend = false;
4328
4329 /* This routine should only be used for empty classes. */
4330 gcc_assert (is_empty_class (basetype));
4331 alignment = ssize_int (CLASSTYPE_ALIGN_UNIT (basetype));
4332
4333 if (!integer_zerop (BINFO_OFFSET (binfo)))
4334 propagate_binfo_offsets
4335 (binfo, size_diffop_loc (input_location,
4336 size_zero_node, BINFO_OFFSET (binfo)));
4337
4338 /* This is an empty base class. We first try to put it at offset
4339 zero. */
4340 if (layout_conflict_p (binfo,
4341 BINFO_OFFSET (binfo),
4342 offsets,
4343 /*vbases_p=*/0))
4344 {
4345 /* That didn't work. Now, we move forward from the next
4346 available spot in the class. */
4347 atend = true;
4348 propagate_binfo_offsets (binfo, convert (ssizetype, eoc));
4349 while (1)
4350 {
4351 if (!layout_conflict_p (binfo,
4352 BINFO_OFFSET (binfo),
4353 offsets,
4354 /*vbases_p=*/0))
4355 /* We finally found a spot where there's no overlap. */
4356 break;
4357
4358 /* There's overlap here, too. Bump along to the next spot. */
4359 propagate_binfo_offsets (binfo, alignment);
4360 }
4361 }
4362
4363 if (CLASSTYPE_USER_ALIGN (basetype))
4364 {
4365 rli->record_align = MAX (rli->record_align, CLASSTYPE_ALIGN (basetype));
4366 if (warn_packed)
4367 rli->unpacked_align = MAX (rli->unpacked_align, CLASSTYPE_ALIGN (basetype));
4368 TYPE_USER_ALIGN (rli->t) = 1;
4369 }
4370
4371 return atend;
4372 }
4373
4374 /* Layout the base given by BINFO in the class indicated by RLI.
4375 *BASE_ALIGN is a running maximum of the alignments of
4376 any base class. OFFSETS gives the location of empty base
4377 subobjects. T is the most derived type. Return nonzero if the new
4378 object cannot be nearly-empty. A new FIELD_DECL is inserted at
4379 *NEXT_FIELD, unless BINFO is for an empty base class.
4380
4381 Returns the location at which the next field should be inserted. */
4382
4383 static tree *
4384 build_base_field (record_layout_info rli, tree binfo,
4385 splay_tree offsets, tree *next_field)
4386 {
4387 tree t = rli->t;
4388 tree basetype = BINFO_TYPE (binfo);
4389
4390 if (!COMPLETE_TYPE_P (basetype))
4391 /* This error is now reported in xref_tag, thus giving better
4392 location information. */
4393 return next_field;
4394
4395 /* Place the base class. */
4396 if (!is_empty_class (basetype))
4397 {
4398 tree decl;
4399
4400 /* The containing class is non-empty because it has a non-empty
4401 base class. */
4402 CLASSTYPE_EMPTY_P (t) = 0;
4403
4404 /* Create the FIELD_DECL. */
4405 decl = build_decl (input_location,
4406 FIELD_DECL, NULL_TREE, CLASSTYPE_AS_BASE (basetype));
4407 DECL_ARTIFICIAL (decl) = 1;
4408 DECL_IGNORED_P (decl) = 1;
4409 DECL_FIELD_CONTEXT (decl) = t;
4410 if (CLASSTYPE_AS_BASE (basetype))
4411 {
4412 DECL_SIZE (decl) = CLASSTYPE_SIZE (basetype);
4413 DECL_SIZE_UNIT (decl) = CLASSTYPE_SIZE_UNIT (basetype);
4414 DECL_ALIGN (decl) = CLASSTYPE_ALIGN (basetype);
4415 DECL_USER_ALIGN (decl) = CLASSTYPE_USER_ALIGN (basetype);
4416 DECL_MODE (decl) = TYPE_MODE (basetype);
4417 DECL_FIELD_IS_BASE (decl) = 1;
4418
4419 /* Try to place the field. It may take more than one try if we
4420 have a hard time placing the field without putting two
4421 objects of the same type at the same address. */
4422 layout_nonempty_base_or_field (rli, decl, binfo, offsets);
4423 /* Add the new FIELD_DECL to the list of fields for T. */
4424 DECL_CHAIN (decl) = *next_field;
4425 *next_field = decl;
4426 next_field = &DECL_CHAIN (decl);
4427 }
4428 }
4429 else
4430 {
4431 tree eoc;
4432 bool atend;
4433
4434 /* On some platforms (ARM), even empty classes will not be
4435 byte-aligned. */
4436 eoc = round_up_loc (input_location,
4437 rli_size_unit_so_far (rli),
4438 CLASSTYPE_ALIGN_UNIT (basetype));
4439 atend = layout_empty_base (rli, binfo, eoc, offsets);
4440 /* A nearly-empty class "has no proper base class that is empty,
4441 not morally virtual, and at an offset other than zero." */
4442 if (!BINFO_VIRTUAL_P (binfo) && CLASSTYPE_NEARLY_EMPTY_P (t))
4443 {
4444 if (atend)
4445 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
4446 /* The check above (used in G++ 3.2) is insufficient because
4447 an empty class placed at offset zero might itself have an
4448 empty base at a nonzero offset. */
4449 else if (walk_subobject_offsets (basetype,
4450 empty_base_at_nonzero_offset_p,
4451 size_zero_node,
4452 /*offsets=*/NULL,
4453 /*max_offset=*/NULL_TREE,
4454 /*vbases_p=*/true))
4455 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
4456 }
4457
4458 /* We do not create a FIELD_DECL for empty base classes because
4459 it might overlap some other field. We want to be able to
4460 create CONSTRUCTORs for the class by iterating over the
4461 FIELD_DECLs, and the back end does not handle overlapping
4462 FIELD_DECLs. */
4463
4464 /* An empty virtual base causes a class to be non-empty
4465 -- but in that case we do not need to clear CLASSTYPE_EMPTY_P
4466 here because that was already done when the virtual table
4467 pointer was created. */
4468 }
4469
4470 /* Record the offsets of BINFO and its base subobjects. */
4471 record_subobject_offsets (binfo,
4472 BINFO_OFFSET (binfo),
4473 offsets,
4474 /*is_data_member=*/false);
4475
4476 return next_field;
4477 }
4478
4479 /* Layout all of the non-virtual base classes. Record empty
4480 subobjects in OFFSETS. T is the most derived type. Return nonzero
4481 if the type cannot be nearly empty. The fields created
4482 corresponding to the base classes will be inserted at
4483 *NEXT_FIELD. */
4484
4485 static void
4486 build_base_fields (record_layout_info rli,
4487 splay_tree offsets, tree *next_field)
4488 {
4489 /* Chain to hold all the new FIELD_DECLs which stand in for base class
4490 subobjects. */
4491 tree t = rli->t;
4492 int n_baseclasses = BINFO_N_BASE_BINFOS (TYPE_BINFO (t));
4493 int i;
4494
4495 /* The primary base class is always allocated first. */
4496 if (CLASSTYPE_HAS_PRIMARY_BASE_P (t))
4497 next_field = build_base_field (rli, CLASSTYPE_PRIMARY_BINFO (t),
4498 offsets, next_field);
4499
4500 /* Now allocate the rest of the bases. */
4501 for (i = 0; i < n_baseclasses; ++i)
4502 {
4503 tree base_binfo;
4504
4505 base_binfo = BINFO_BASE_BINFO (TYPE_BINFO (t), i);
4506
4507 /* The primary base was already allocated above, so we don't
4508 need to allocate it again here. */
4509 if (base_binfo == CLASSTYPE_PRIMARY_BINFO (t))
4510 continue;
4511
4512 /* Virtual bases are added at the end (a primary virtual base
4513 will have already been added). */
4514 if (BINFO_VIRTUAL_P (base_binfo))
4515 continue;
4516
4517 next_field = build_base_field (rli, base_binfo,
4518 offsets, next_field);
4519 }
4520 }
4521
4522 /* Go through the TYPE_METHODS of T issuing any appropriate
4523 diagnostics, figuring out which methods override which other
4524 methods, and so forth. */
4525
4526 static void
4527 check_methods (tree t)
4528 {
4529 tree x;
4530
4531 for (x = TYPE_METHODS (t); x; x = DECL_CHAIN (x))
4532 {
4533 check_for_override (x, t);
4534 if (DECL_PURE_VIRTUAL_P (x) && (TREE_CODE (x) != FUNCTION_DECL || ! DECL_VINDEX (x)))
4535 error ("initializer specified for non-virtual method %q+D", x);
4536 /* The name of the field is the original field name
4537 Save this in auxiliary field for later overloading. */
4538 if (TREE_CODE (x) == FUNCTION_DECL && DECL_VINDEX (x))
4539 {
4540 TYPE_POLYMORPHIC_P (t) = 1;
4541 if (DECL_PURE_VIRTUAL_P (x))
4542 vec_safe_push (CLASSTYPE_PURE_VIRTUALS (t), x);
4543 }
4544 /* All user-provided destructors are non-trivial.
4545 Constructors and assignment ops are handled in
4546 grok_special_member_properties. */
4547 if (DECL_DESTRUCTOR_P (x) && user_provided_p (x))
4548 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t) = 1;
4549 }
4550 }
4551
4552 /* FN is a constructor or destructor. Clone the declaration to create
4553 a specialized in-charge or not-in-charge version, as indicated by
4554 NAME. */
4555
4556 static tree
4557 build_clone (tree fn, tree name)
4558 {
4559 tree parms;
4560 tree clone;
4561
4562 /* Copy the function. */
4563 clone = copy_decl (fn);
4564 /* Reset the function name. */
4565 DECL_NAME (clone) = name;
4566 /* Remember where this function came from. */
4567 DECL_ABSTRACT_ORIGIN (clone) = fn;
4568 /* Make it easy to find the CLONE given the FN. */
4569 DECL_CHAIN (clone) = DECL_CHAIN (fn);
4570 DECL_CHAIN (fn) = clone;
4571
4572 /* If this is a template, do the rest on the DECL_TEMPLATE_RESULT. */
4573 if (TREE_CODE (clone) == TEMPLATE_DECL)
4574 {
4575 tree result = build_clone (DECL_TEMPLATE_RESULT (clone), name);
4576 DECL_TEMPLATE_RESULT (clone) = result;
4577 DECL_TEMPLATE_INFO (result) = copy_node (DECL_TEMPLATE_INFO (result));
4578 DECL_TI_TEMPLATE (result) = clone;
4579 TREE_TYPE (clone) = TREE_TYPE (result);
4580 return clone;
4581 }
4582
4583 SET_DECL_ASSEMBLER_NAME (clone, NULL_TREE);
4584 DECL_CLONED_FUNCTION (clone) = fn;
4585 /* There's no pending inline data for this function. */
4586 DECL_PENDING_INLINE_INFO (clone) = NULL;
4587 DECL_PENDING_INLINE_P (clone) = 0;
4588
4589 /* The base-class destructor is not virtual. */
4590 if (name == base_dtor_identifier)
4591 {
4592 DECL_VIRTUAL_P (clone) = 0;
4593 if (TREE_CODE (clone) != TEMPLATE_DECL)
4594 DECL_VINDEX (clone) = NULL_TREE;
4595 }
4596
4597 /* If there was an in-charge parameter, drop it from the function
4598 type. */
4599 if (DECL_HAS_IN_CHARGE_PARM_P (clone))
4600 {
4601 tree basetype;
4602 tree parmtypes;
4603 tree exceptions;
4604
4605 exceptions = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (clone));
4606 basetype = TYPE_METHOD_BASETYPE (TREE_TYPE (clone));
4607 parmtypes = TYPE_ARG_TYPES (TREE_TYPE (clone));
4608 /* Skip the `this' parameter. */
4609 parmtypes = TREE_CHAIN (parmtypes);
4610 /* Skip the in-charge parameter. */
4611 parmtypes = TREE_CHAIN (parmtypes);
4612 /* And the VTT parm, in a complete [cd]tor. */
4613 if (DECL_HAS_VTT_PARM_P (fn)
4614 && ! DECL_NEEDS_VTT_PARM_P (clone))
4615 parmtypes = TREE_CHAIN (parmtypes);
4616 /* If this is subobject constructor or destructor, add the vtt
4617 parameter. */
4618 TREE_TYPE (clone)
4619 = build_method_type_directly (basetype,
4620 TREE_TYPE (TREE_TYPE (clone)),
4621 parmtypes);
4622 if (exceptions)
4623 TREE_TYPE (clone) = build_exception_variant (TREE_TYPE (clone),
4624 exceptions);
4625 TREE_TYPE (clone)
4626 = cp_build_type_attribute_variant (TREE_TYPE (clone),
4627 TYPE_ATTRIBUTES (TREE_TYPE (fn)));
4628 }
4629
4630 /* Copy the function parameters. */
4631 DECL_ARGUMENTS (clone) = copy_list (DECL_ARGUMENTS (clone));
4632 /* Remove the in-charge parameter. */
4633 if (DECL_HAS_IN_CHARGE_PARM_P (clone))
4634 {
4635 DECL_CHAIN (DECL_ARGUMENTS (clone))
4636 = DECL_CHAIN (DECL_CHAIN (DECL_ARGUMENTS (clone)));
4637 DECL_HAS_IN_CHARGE_PARM_P (clone) = 0;
4638 }
4639 /* And the VTT parm, in a complete [cd]tor. */
4640 if (DECL_HAS_VTT_PARM_P (fn))
4641 {
4642 if (DECL_NEEDS_VTT_PARM_P (clone))
4643 DECL_HAS_VTT_PARM_P (clone) = 1;
4644 else
4645 {
4646 DECL_CHAIN (DECL_ARGUMENTS (clone))
4647 = DECL_CHAIN (DECL_CHAIN (DECL_ARGUMENTS (clone)));
4648 DECL_HAS_VTT_PARM_P (clone) = 0;
4649 }
4650 }
4651
4652 for (parms = DECL_ARGUMENTS (clone); parms; parms = DECL_CHAIN (parms))
4653 {
4654 DECL_CONTEXT (parms) = clone;
4655 cxx_dup_lang_specific_decl (parms);
4656 }
4657
4658 /* Create the RTL for this function. */
4659 SET_DECL_RTL (clone, NULL);
4660 rest_of_decl_compilation (clone, /*top_level=*/1, at_eof);
4661
4662 if (pch_file)
4663 note_decl_for_pch (clone);
4664
4665 return clone;
4666 }
4667
4668 /* Implementation of DECL_CLONED_FUNCTION and DECL_CLONED_FUNCTION_P, do
4669 not invoke this function directly.
4670
4671 For a non-thunk function, returns the address of the slot for storing
4672 the function it is a clone of. Otherwise returns NULL_TREE.
4673
4674 If JUST_TESTING, looks through TEMPLATE_DECL and returns NULL if
4675 cloned_function is unset. This is to support the separate
4676 DECL_CLONED_FUNCTION and DECL_CLONED_FUNCTION_P modes; using the latter
4677 on a template makes sense, but not the former. */
4678
4679 tree *
4680 decl_cloned_function_p (const_tree decl, bool just_testing)
4681 {
4682 tree *ptr;
4683 if (just_testing)
4684 decl = STRIP_TEMPLATE (decl);
4685
4686 if (TREE_CODE (decl) != FUNCTION_DECL
4687 || !DECL_LANG_SPECIFIC (decl)
4688 || DECL_LANG_SPECIFIC (decl)->u.fn.thunk_p)
4689 {
4690 #if defined ENABLE_TREE_CHECKING && (GCC_VERSION >= 2007)
4691 if (!just_testing)
4692 lang_check_failed (__FILE__, __LINE__, __FUNCTION__);
4693 else
4694 #endif
4695 return NULL;
4696 }
4697
4698 ptr = &DECL_LANG_SPECIFIC (decl)->u.fn.u5.cloned_function;
4699 if (just_testing && *ptr == NULL_TREE)
4700 return NULL;
4701 else
4702 return ptr;
4703 }
4704
4705 /* Produce declarations for all appropriate clones of FN. If
4706 UPDATE_METHOD_VEC_P is nonzero, the clones are added to the
4707 CLASTYPE_METHOD_VEC as well. */
4708
4709 void
4710 clone_function_decl (tree fn, int update_method_vec_p)
4711 {
4712 tree clone;
4713
4714 /* Avoid inappropriate cloning. */
4715 if (DECL_CHAIN (fn)
4716 && DECL_CLONED_FUNCTION_P (DECL_CHAIN (fn)))
4717 return;
4718
4719 if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (fn))
4720 {
4721 /* For each constructor, we need two variants: an in-charge version
4722 and a not-in-charge version. */
4723 clone = build_clone (fn, complete_ctor_identifier);
4724 if (update_method_vec_p)
4725 add_method (DECL_CONTEXT (clone), clone, NULL_TREE);
4726 clone = build_clone (fn, base_ctor_identifier);
4727 if (update_method_vec_p)
4728 add_method (DECL_CONTEXT (clone), clone, NULL_TREE);
4729 }
4730 else
4731 {
4732 gcc_assert (DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (fn));
4733
4734 /* For each destructor, we need three variants: an in-charge
4735 version, a not-in-charge version, and an in-charge deleting
4736 version. We clone the deleting version first because that
4737 means it will go second on the TYPE_METHODS list -- and that
4738 corresponds to the correct layout order in the virtual
4739 function table.
4740
4741 For a non-virtual destructor, we do not build a deleting
4742 destructor. */
4743 if (DECL_VIRTUAL_P (fn))
4744 {
4745 clone = build_clone (fn, deleting_dtor_identifier);
4746 if (update_method_vec_p)
4747 add_method (DECL_CONTEXT (clone), clone, NULL_TREE);
4748 }
4749 clone = build_clone (fn, complete_dtor_identifier);
4750 if (update_method_vec_p)
4751 add_method (DECL_CONTEXT (clone), clone, NULL_TREE);
4752 clone = build_clone (fn, base_dtor_identifier);
4753 if (update_method_vec_p)
4754 add_method (DECL_CONTEXT (clone), clone, NULL_TREE);
4755 }
4756
4757 /* Note that this is an abstract function that is never emitted. */
4758 DECL_ABSTRACT_P (fn) = true;
4759 }
4760
4761 /* DECL is an in charge constructor, which is being defined. This will
4762 have had an in class declaration, from whence clones were
4763 declared. An out-of-class definition can specify additional default
4764 arguments. As it is the clones that are involved in overload
4765 resolution, we must propagate the information from the DECL to its
4766 clones. */
4767
4768 void
4769 adjust_clone_args (tree decl)
4770 {
4771 tree clone;
4772
4773 for (clone = DECL_CHAIN (decl); clone && DECL_CLONED_FUNCTION_P (clone);
4774 clone = DECL_CHAIN (clone))
4775 {
4776 tree orig_clone_parms = TYPE_ARG_TYPES (TREE_TYPE (clone));
4777 tree orig_decl_parms = TYPE_ARG_TYPES (TREE_TYPE (decl));
4778 tree decl_parms, clone_parms;
4779
4780 clone_parms = orig_clone_parms;
4781
4782 /* Skip the 'this' parameter. */
4783 orig_clone_parms = TREE_CHAIN (orig_clone_parms);
4784 orig_decl_parms = TREE_CHAIN (orig_decl_parms);
4785
4786 if (DECL_HAS_IN_CHARGE_PARM_P (decl))
4787 orig_decl_parms = TREE_CHAIN (orig_decl_parms);
4788 if (DECL_HAS_VTT_PARM_P (decl))
4789 orig_decl_parms = TREE_CHAIN (orig_decl_parms);
4790
4791 clone_parms = orig_clone_parms;
4792 if (DECL_HAS_VTT_PARM_P (clone))
4793 clone_parms = TREE_CHAIN (clone_parms);
4794
4795 for (decl_parms = orig_decl_parms; decl_parms;
4796 decl_parms = TREE_CHAIN (decl_parms),
4797 clone_parms = TREE_CHAIN (clone_parms))
4798 {
4799 gcc_assert (same_type_p (TREE_TYPE (decl_parms),
4800 TREE_TYPE (clone_parms)));
4801
4802 if (TREE_PURPOSE (decl_parms) && !TREE_PURPOSE (clone_parms))
4803 {
4804 /* A default parameter has been added. Adjust the
4805 clone's parameters. */
4806 tree exceptions = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (clone));
4807 tree attrs = TYPE_ATTRIBUTES (TREE_TYPE (clone));
4808 tree basetype = TYPE_METHOD_BASETYPE (TREE_TYPE (clone));
4809 tree type;
4810
4811 clone_parms = orig_decl_parms;
4812
4813 if (DECL_HAS_VTT_PARM_P (clone))
4814 {
4815 clone_parms = tree_cons (TREE_PURPOSE (orig_clone_parms),
4816 TREE_VALUE (orig_clone_parms),
4817 clone_parms);
4818 TREE_TYPE (clone_parms) = TREE_TYPE (orig_clone_parms);
4819 }
4820 type = build_method_type_directly (basetype,
4821 TREE_TYPE (TREE_TYPE (clone)),
4822 clone_parms);
4823 if (exceptions)
4824 type = build_exception_variant (type, exceptions);
4825 if (attrs)
4826 type = cp_build_type_attribute_variant (type, attrs);
4827 TREE_TYPE (clone) = type;
4828
4829 clone_parms = NULL_TREE;
4830 break;
4831 }
4832 }
4833 gcc_assert (!clone_parms);
4834 }
4835 }
4836
4837 /* For each of the constructors and destructors in T, create an
4838 in-charge and not-in-charge variant. */
4839
4840 static void
4841 clone_constructors_and_destructors (tree t)
4842 {
4843 tree fns;
4844
4845 /* If for some reason we don't have a CLASSTYPE_METHOD_VEC, we bail
4846 out now. */
4847 if (!CLASSTYPE_METHOD_VEC (t))
4848 return;
4849
4850 for (fns = CLASSTYPE_CONSTRUCTORS (t); fns; fns = OVL_NEXT (fns))
4851 clone_function_decl (OVL_CURRENT (fns), /*update_method_vec_p=*/1);
4852 for (fns = CLASSTYPE_DESTRUCTORS (t); fns; fns = OVL_NEXT (fns))
4853 clone_function_decl (OVL_CURRENT (fns), /*update_method_vec_p=*/1);
4854 }
4855
4856 /* Deduce noexcept for a destructor DTOR. */
4857
4858 void
4859 deduce_noexcept_on_destructor (tree dtor)
4860 {
4861 if (!TYPE_RAISES_EXCEPTIONS (TREE_TYPE (dtor)))
4862 {
4863 tree eh_spec = unevaluated_noexcept_spec ();
4864 TREE_TYPE (dtor) = build_exception_variant (TREE_TYPE (dtor), eh_spec);
4865 }
4866 }
4867
4868 /* For each destructor in T, deduce noexcept:
4869
4870 12.4/3: A declaration of a destructor that does not have an
4871 exception-specification is implicitly considered to have the
4872 same exception-specification as an implicit declaration (15.4). */
4873
4874 static void
4875 deduce_noexcept_on_destructors (tree t)
4876 {
4877 /* If for some reason we don't have a CLASSTYPE_METHOD_VEC, we bail
4878 out now. */
4879 if (!CLASSTYPE_METHOD_VEC (t))
4880 return;
4881
4882 for (tree fns = CLASSTYPE_DESTRUCTORS (t); fns; fns = OVL_NEXT (fns))
4883 deduce_noexcept_on_destructor (OVL_CURRENT (fns));
4884 }
4885
4886 /* Subroutine of set_one_vmethod_tm_attributes. Search base classes
4887 of TYPE for virtual functions which FNDECL overrides. Return a
4888 mask of the tm attributes found therein. */
4889
4890 static int
4891 look_for_tm_attr_overrides (tree type, tree fndecl)
4892 {
4893 tree binfo = TYPE_BINFO (type);
4894 tree base_binfo;
4895 int ix, found = 0;
4896
4897 for (ix = 0; BINFO_BASE_ITERATE (binfo, ix, base_binfo); ++ix)
4898 {
4899 tree o, basetype = BINFO_TYPE (base_binfo);
4900
4901 if (!TYPE_POLYMORPHIC_P (basetype))
4902 continue;
4903
4904 o = look_for_overrides_here (basetype, fndecl);
4905 if (o)
4906 found |= tm_attr_to_mask (find_tm_attribute
4907 (TYPE_ATTRIBUTES (TREE_TYPE (o))));
4908 else
4909 found |= look_for_tm_attr_overrides (basetype, fndecl);
4910 }
4911
4912 return found;
4913 }
4914
4915 /* Subroutine of set_method_tm_attributes. Handle the checks and
4916 inheritance for one virtual method FNDECL. */
4917
4918 static void
4919 set_one_vmethod_tm_attributes (tree type, tree fndecl)
4920 {
4921 tree tm_attr;
4922 int found, have;
4923
4924 found = look_for_tm_attr_overrides (type, fndecl);
4925
4926 /* If FNDECL doesn't actually override anything (i.e. T is the
4927 class that first declares FNDECL virtual), then we're done. */
4928 if (found == 0)
4929 return;
4930
4931 tm_attr = find_tm_attribute (TYPE_ATTRIBUTES (TREE_TYPE (fndecl)));
4932 have = tm_attr_to_mask (tm_attr);
4933
4934 /* Intel STM Language Extension 3.0, Section 4.2 table 4:
4935 tm_pure must match exactly, otherwise no weakening of
4936 tm_safe > tm_callable > nothing. */
4937 /* ??? The tm_pure attribute didn't make the transition to the
4938 multivendor language spec. */
4939 if (have == TM_ATTR_PURE)
4940 {
4941 if (found != TM_ATTR_PURE)
4942 {
4943 found &= -found;
4944 goto err_override;
4945 }
4946 }
4947 /* If the overridden function is tm_pure, then FNDECL must be. */
4948 else if (found == TM_ATTR_PURE && tm_attr)
4949 goto err_override;
4950 /* Look for base class combinations that cannot be satisfied. */
4951 else if (found != TM_ATTR_PURE && (found & TM_ATTR_PURE))
4952 {
4953 found &= ~TM_ATTR_PURE;
4954 found &= -found;
4955 error_at (DECL_SOURCE_LOCATION (fndecl),
4956 "method overrides both %<transaction_pure%> and %qE methods",
4957 tm_mask_to_attr (found));
4958 }
4959 /* If FNDECL did not declare an attribute, then inherit the most
4960 restrictive one. */
4961 else if (tm_attr == NULL)
4962 {
4963 apply_tm_attr (fndecl, tm_mask_to_attr (found & -found));
4964 }
4965 /* Otherwise validate that we're not weaker than a function
4966 that is being overridden. */
4967 else
4968 {
4969 found &= -found;
4970 if (found <= TM_ATTR_CALLABLE && have > found)
4971 goto err_override;
4972 }
4973 return;
4974
4975 err_override:
4976 error_at (DECL_SOURCE_LOCATION (fndecl),
4977 "method declared %qE overriding %qE method",
4978 tm_attr, tm_mask_to_attr (found));
4979 }
4980
4981 /* For each of the methods in T, propagate a class-level tm attribute. */
4982
4983 static void
4984 set_method_tm_attributes (tree t)
4985 {
4986 tree class_tm_attr, fndecl;
4987
4988 /* Don't bother collecting tm attributes if transactional memory
4989 support is not enabled. */
4990 if (!flag_tm)
4991 return;
4992
4993 /* Process virtual methods first, as they inherit directly from the
4994 base virtual function and also require validation of new attributes. */
4995 if (TYPE_CONTAINS_VPTR_P (t))
4996 {
4997 tree vchain;
4998 for (vchain = BINFO_VIRTUALS (TYPE_BINFO (t)); vchain;
4999 vchain = TREE_CHAIN (vchain))
5000 {
5001 fndecl = BV_FN (vchain);
5002 if (DECL_THUNK_P (fndecl))
5003 fndecl = THUNK_TARGET (fndecl);
5004 set_one_vmethod_tm_attributes (t, fndecl);
5005 }
5006 }
5007
5008 /* If the class doesn't have an attribute, nothing more to do. */
5009 class_tm_attr = find_tm_attribute (TYPE_ATTRIBUTES (t));
5010 if (class_tm_attr == NULL)
5011 return;
5012
5013 /* Any method that does not yet have a tm attribute inherits
5014 the one from the class. */
5015 for (fndecl = TYPE_METHODS (t); fndecl; fndecl = TREE_CHAIN (fndecl))
5016 {
5017 if (!find_tm_attribute (TYPE_ATTRIBUTES (TREE_TYPE (fndecl))))
5018 apply_tm_attr (fndecl, class_tm_attr);
5019 }
5020 }
5021
5022 /* Returns true iff class T has a user-defined constructor other than
5023 the default constructor. */
5024
5025 bool
5026 type_has_user_nondefault_constructor (tree t)
5027 {
5028 tree fns;
5029
5030 if (!TYPE_HAS_USER_CONSTRUCTOR (t))
5031 return false;
5032
5033 for (fns = CLASSTYPE_CONSTRUCTORS (t); fns; fns = OVL_NEXT (fns))
5034 {
5035 tree fn = OVL_CURRENT (fns);
5036 if (!DECL_ARTIFICIAL (fn)
5037 && (TREE_CODE (fn) == TEMPLATE_DECL
5038 || (skip_artificial_parms_for (fn, DECL_ARGUMENTS (fn))
5039 != NULL_TREE)))
5040 return true;
5041 }
5042
5043 return false;
5044 }
5045
5046 /* Returns the defaulted constructor if T has one. Otherwise, returns
5047 NULL_TREE. */
5048
5049 tree
5050 in_class_defaulted_default_constructor (tree t)
5051 {
5052 tree fns, args;
5053
5054 if (!TYPE_HAS_USER_CONSTRUCTOR (t))
5055 return NULL_TREE;
5056
5057 for (fns = CLASSTYPE_CONSTRUCTORS (t); fns; fns = OVL_NEXT (fns))
5058 {
5059 tree fn = OVL_CURRENT (fns);
5060
5061 if (DECL_DEFAULTED_IN_CLASS_P (fn))
5062 {
5063 args = FUNCTION_FIRST_USER_PARMTYPE (fn);
5064 while (args && TREE_PURPOSE (args))
5065 args = TREE_CHAIN (args);
5066 if (!args || args == void_list_node)
5067 return fn;
5068 }
5069 }
5070
5071 return NULL_TREE;
5072 }
5073
5074 /* Returns true iff FN is a user-provided function, i.e. user-declared
5075 and not defaulted at its first declaration; or explicit, private,
5076 protected, or non-const. */
5077
5078 bool
5079 user_provided_p (tree fn)
5080 {
5081 if (TREE_CODE (fn) == TEMPLATE_DECL)
5082 return true;
5083 else
5084 return (!DECL_ARTIFICIAL (fn)
5085 && !(DECL_INITIALIZED_IN_CLASS_P (fn)
5086 && (DECL_DEFAULTED_FN (fn) || DECL_DELETED_FN (fn))));
5087 }
5088
5089 /* Returns true iff class T has a user-provided constructor. */
5090
5091 bool
5092 type_has_user_provided_constructor (tree t)
5093 {
5094 tree fns;
5095
5096 if (!CLASS_TYPE_P (t))
5097 return false;
5098
5099 if (!TYPE_HAS_USER_CONSTRUCTOR (t))
5100 return false;
5101
5102 /* This can happen in error cases; avoid crashing. */
5103 if (!CLASSTYPE_METHOD_VEC (t))
5104 return false;
5105
5106 for (fns = CLASSTYPE_CONSTRUCTORS (t); fns; fns = OVL_NEXT (fns))
5107 if (user_provided_p (OVL_CURRENT (fns)))
5108 return true;
5109
5110 return false;
5111 }
5112
5113 /* Returns true iff class T has a non-user-provided (i.e. implicitly
5114 declared or explicitly defaulted in the class body) default
5115 constructor. */
5116
5117 bool
5118 type_has_non_user_provided_default_constructor (tree t)
5119 {
5120 tree fns;
5121
5122 if (!TYPE_HAS_DEFAULT_CONSTRUCTOR (t))
5123 return false;
5124 if (CLASSTYPE_LAZY_DEFAULT_CTOR (t))
5125 return true;
5126
5127 for (fns = CLASSTYPE_CONSTRUCTORS (t); fns; fns = OVL_NEXT (fns))
5128 {
5129 tree fn = OVL_CURRENT (fns);
5130 if (TREE_CODE (fn) == FUNCTION_DECL
5131 && !user_provided_p (fn)
5132 && sufficient_parms_p (FUNCTION_FIRST_USER_PARMTYPE (fn)))
5133 return true;
5134 }
5135
5136 return false;
5137 }
5138
5139 /* TYPE is being used as a virtual base, and has a non-trivial move
5140 assignment. Return true if this is due to there being a user-provided
5141 move assignment in TYPE or one of its subobjects; if there isn't, then
5142 multiple move assignment can't cause any harm. */
5143
5144 bool
5145 vbase_has_user_provided_move_assign (tree type)
5146 {
5147 /* Does the type itself have a user-provided move assignment operator? */
5148 for (tree fns
5149 = lookup_fnfields_slot_nolazy (type, ansi_assopname (NOP_EXPR));
5150 fns; fns = OVL_NEXT (fns))
5151 {
5152 tree fn = OVL_CURRENT (fns);
5153 if (move_fn_p (fn) && user_provided_p (fn))
5154 return true;
5155 }
5156
5157 /* Do any of its bases? */
5158 tree binfo = TYPE_BINFO (type);
5159 tree base_binfo;
5160 for (int i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
5161 if (vbase_has_user_provided_move_assign (BINFO_TYPE (base_binfo)))
5162 return true;
5163
5164 /* Or non-static data members? */
5165 for (tree field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
5166 {
5167 if (TREE_CODE (field) == FIELD_DECL
5168 && CLASS_TYPE_P (TREE_TYPE (field))
5169 && vbase_has_user_provided_move_assign (TREE_TYPE (field)))
5170 return true;
5171 }
5172
5173 /* Seems not. */
5174 return false;
5175 }
5176
5177 /* If default-initialization leaves part of TYPE uninitialized, returns
5178 a DECL for the field or TYPE itself (DR 253). */
5179
5180 tree
5181 default_init_uninitialized_part (tree type)
5182 {
5183 tree t, r, binfo;
5184 int i;
5185
5186 type = strip_array_types (type);
5187 if (!CLASS_TYPE_P (type))
5188 return type;
5189 if (!type_has_non_user_provided_default_constructor (type))
5190 return NULL_TREE;
5191 for (binfo = TYPE_BINFO (type), i = 0;
5192 BINFO_BASE_ITERATE (binfo, i, t); ++i)
5193 {
5194 r = default_init_uninitialized_part (BINFO_TYPE (t));
5195 if (r)
5196 return r;
5197 }
5198 for (t = TYPE_FIELDS (type); t; t = DECL_CHAIN (t))
5199 if (TREE_CODE (t) == FIELD_DECL
5200 && !DECL_ARTIFICIAL (t)
5201 && !DECL_INITIAL (t))
5202 {
5203 r = default_init_uninitialized_part (TREE_TYPE (t));
5204 if (r)
5205 return DECL_P (r) ? r : t;
5206 }
5207
5208 return NULL_TREE;
5209 }
5210
5211 /* Returns true iff for class T, a trivial synthesized default constructor
5212 would be constexpr. */
5213
5214 bool
5215 trivial_default_constructor_is_constexpr (tree t)
5216 {
5217 /* A defaulted trivial default constructor is constexpr
5218 if there is nothing to initialize. */
5219 gcc_assert (!TYPE_HAS_COMPLEX_DFLT (t));
5220 return is_really_empty_class (t);
5221 }
5222
5223 /* Returns true iff class T has a constexpr default constructor. */
5224
5225 bool
5226 type_has_constexpr_default_constructor (tree t)
5227 {
5228 tree fns;
5229
5230 if (!CLASS_TYPE_P (t))
5231 {
5232 /* The caller should have stripped an enclosing array. */
5233 gcc_assert (TREE_CODE (t) != ARRAY_TYPE);
5234 return false;
5235 }
5236 if (CLASSTYPE_LAZY_DEFAULT_CTOR (t))
5237 {
5238 if (!TYPE_HAS_COMPLEX_DFLT (t))
5239 return trivial_default_constructor_is_constexpr (t);
5240 /* Non-trivial, we need to check subobject constructors. */
5241 lazily_declare_fn (sfk_constructor, t);
5242 }
5243 fns = locate_ctor (t);
5244 return (fns && DECL_DECLARED_CONSTEXPR_P (fns));
5245 }
5246
5247 /* Returns true iff class TYPE has a virtual destructor. */
5248
5249 bool
5250 type_has_virtual_destructor (tree type)
5251 {
5252 tree dtor;
5253
5254 if (!CLASS_TYPE_P (type))
5255 return false;
5256
5257 gcc_assert (COMPLETE_TYPE_P (type));
5258 dtor = CLASSTYPE_DESTRUCTORS (type);
5259 return (dtor && DECL_VIRTUAL_P (dtor));
5260 }
5261
5262 /* Returns true iff class T has a move constructor. */
5263
5264 bool
5265 type_has_move_constructor (tree t)
5266 {
5267 tree fns;
5268
5269 if (CLASSTYPE_LAZY_MOVE_CTOR (t))
5270 {
5271 gcc_assert (COMPLETE_TYPE_P (t));
5272 lazily_declare_fn (sfk_move_constructor, t);
5273 }
5274
5275 if (!CLASSTYPE_METHOD_VEC (t))
5276 return false;
5277
5278 for (fns = CLASSTYPE_CONSTRUCTORS (t); fns; fns = OVL_NEXT (fns))
5279 if (move_fn_p (OVL_CURRENT (fns)))
5280 return true;
5281
5282 return false;
5283 }
5284
5285 /* Returns true iff class T has a move assignment operator. */
5286
5287 bool
5288 type_has_move_assign (tree t)
5289 {
5290 tree fns;
5291
5292 if (CLASSTYPE_LAZY_MOVE_ASSIGN (t))
5293 {
5294 gcc_assert (COMPLETE_TYPE_P (t));
5295 lazily_declare_fn (sfk_move_assignment, t);
5296 }
5297
5298 for (fns = lookup_fnfields_slot_nolazy (t, ansi_assopname (NOP_EXPR));
5299 fns; fns = OVL_NEXT (fns))
5300 if (move_fn_p (OVL_CURRENT (fns)))
5301 return true;
5302
5303 return false;
5304 }
5305
5306 /* Returns true iff class T has a move constructor that was explicitly
5307 declared in the class body. Note that this is different from
5308 "user-provided", which doesn't include functions that are defaulted in
5309 the class. */
5310
5311 bool
5312 type_has_user_declared_move_constructor (tree t)
5313 {
5314 tree fns;
5315
5316 if (CLASSTYPE_LAZY_MOVE_CTOR (t))
5317 return false;
5318
5319 if (!CLASSTYPE_METHOD_VEC (t))
5320 return false;
5321
5322 for (fns = CLASSTYPE_CONSTRUCTORS (t); fns; fns = OVL_NEXT (fns))
5323 {
5324 tree fn = OVL_CURRENT (fns);
5325 if (move_fn_p (fn) && !DECL_ARTIFICIAL (fn))
5326 return true;
5327 }
5328
5329 return false;
5330 }
5331
5332 /* Returns true iff class T has a move assignment operator that was
5333 explicitly declared in the class body. */
5334
5335 bool
5336 type_has_user_declared_move_assign (tree t)
5337 {
5338 tree fns;
5339
5340 if (CLASSTYPE_LAZY_MOVE_ASSIGN (t))
5341 return false;
5342
5343 for (fns = lookup_fnfields_slot_nolazy (t, ansi_assopname (NOP_EXPR));
5344 fns; fns = OVL_NEXT (fns))
5345 {
5346 tree fn = OVL_CURRENT (fns);
5347 if (move_fn_p (fn) && !DECL_ARTIFICIAL (fn))
5348 return true;
5349 }
5350
5351 return false;
5352 }
5353
5354 /* Nonzero if we need to build up a constructor call when initializing an
5355 object of this class, either because it has a user-declared constructor
5356 or because it doesn't have a default constructor (so we need to give an
5357 error if no initializer is provided). Use TYPE_NEEDS_CONSTRUCTING when
5358 what you care about is whether or not an object can be produced by a
5359 constructor (e.g. so we don't set TREE_READONLY on const variables of
5360 such type); use this function when what you care about is whether or not
5361 to try to call a constructor to create an object. The latter case is
5362 the former plus some cases of constructors that cannot be called. */
5363
5364 bool
5365 type_build_ctor_call (tree t)
5366 {
5367 tree inner;
5368 if (TYPE_NEEDS_CONSTRUCTING (t))
5369 return true;
5370 inner = strip_array_types (t);
5371 if (!CLASS_TYPE_P (inner) || ANON_AGGR_TYPE_P (inner))
5372 return false;
5373 if (!TYPE_HAS_DEFAULT_CONSTRUCTOR (inner))
5374 return true;
5375 if (cxx_dialect < cxx11)
5376 return false;
5377 /* A user-declared constructor might be private, and a constructor might
5378 be trivial but deleted. */
5379 for (tree fns = lookup_fnfields_slot (inner, complete_ctor_identifier);
5380 fns; fns = OVL_NEXT (fns))
5381 {
5382 tree fn = OVL_CURRENT (fns);
5383 if (!DECL_ARTIFICIAL (fn)
5384 || DECL_DELETED_FN (fn))
5385 return true;
5386 }
5387 return false;
5388 }
5389
5390 /* Like type_build_ctor_call, but for destructors. */
5391
5392 bool
5393 type_build_dtor_call (tree t)
5394 {
5395 tree inner;
5396 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t))
5397 return true;
5398 inner = strip_array_types (t);
5399 if (!CLASS_TYPE_P (inner) || ANON_AGGR_TYPE_P (inner)
5400 || !COMPLETE_TYPE_P (inner))
5401 return false;
5402 if (cxx_dialect < cxx11)
5403 return false;
5404 /* A user-declared destructor might be private, and a destructor might
5405 be trivial but deleted. */
5406 for (tree fns = lookup_fnfields_slot (inner, complete_dtor_identifier);
5407 fns; fns = OVL_NEXT (fns))
5408 {
5409 tree fn = OVL_CURRENT (fns);
5410 if (!DECL_ARTIFICIAL (fn)
5411 || DECL_DELETED_FN (fn))
5412 return true;
5413 }
5414 return false;
5415 }
5416
5417 /* Remove all zero-width bit-fields from T. */
5418
5419 static void
5420 remove_zero_width_bit_fields (tree t)
5421 {
5422 tree *fieldsp;
5423
5424 fieldsp = &TYPE_FIELDS (t);
5425 while (*fieldsp)
5426 {
5427 if (TREE_CODE (*fieldsp) == FIELD_DECL
5428 && DECL_C_BIT_FIELD (*fieldsp)
5429 /* We should not be confused by the fact that grokbitfield
5430 temporarily sets the width of the bit field into
5431 DECL_INITIAL (*fieldsp).
5432 check_bitfield_decl eventually sets DECL_SIZE (*fieldsp)
5433 to that width. */
5434 && (DECL_SIZE (*fieldsp) == NULL_TREE
5435 || integer_zerop (DECL_SIZE (*fieldsp))))
5436 *fieldsp = DECL_CHAIN (*fieldsp);
5437 else
5438 fieldsp = &DECL_CHAIN (*fieldsp);
5439 }
5440 }
5441
5442 /* Returns TRUE iff we need a cookie when dynamically allocating an
5443 array whose elements have the indicated class TYPE. */
5444
5445 static bool
5446 type_requires_array_cookie (tree type)
5447 {
5448 tree fns;
5449 bool has_two_argument_delete_p = false;
5450
5451 gcc_assert (CLASS_TYPE_P (type));
5452
5453 /* If there's a non-trivial destructor, we need a cookie. In order
5454 to iterate through the array calling the destructor for each
5455 element, we'll have to know how many elements there are. */
5456 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
5457 return true;
5458
5459 /* If the usual deallocation function is a two-argument whose second
5460 argument is of type `size_t', then we have to pass the size of
5461 the array to the deallocation function, so we will need to store
5462 a cookie. */
5463 fns = lookup_fnfields (TYPE_BINFO (type),
5464 ansi_opname (VEC_DELETE_EXPR),
5465 /*protect=*/0);
5466 /* If there are no `operator []' members, or the lookup is
5467 ambiguous, then we don't need a cookie. */
5468 if (!fns || fns == error_mark_node)
5469 return false;
5470 /* Loop through all of the functions. */
5471 for (fns = BASELINK_FUNCTIONS (fns); fns; fns = OVL_NEXT (fns))
5472 {
5473 tree fn;
5474 tree second_parm;
5475
5476 /* Select the current function. */
5477 fn = OVL_CURRENT (fns);
5478 /* See if this function is a one-argument delete function. If
5479 it is, then it will be the usual deallocation function. */
5480 second_parm = TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (fn)));
5481 if (second_parm == void_list_node)
5482 return false;
5483 /* Do not consider this function if its second argument is an
5484 ellipsis. */
5485 if (!second_parm)
5486 continue;
5487 /* Otherwise, if we have a two-argument function and the second
5488 argument is `size_t', it will be the usual deallocation
5489 function -- unless there is one-argument function, too. */
5490 if (TREE_CHAIN (second_parm) == void_list_node
5491 && same_type_p (TREE_VALUE (second_parm), size_type_node))
5492 has_two_argument_delete_p = true;
5493 }
5494
5495 return has_two_argument_delete_p;
5496 }
5497
5498 /* Finish computing the `literal type' property of class type T.
5499
5500 At this point, we have already processed base classes and
5501 non-static data members. We need to check whether the copy
5502 constructor is trivial, the destructor is trivial, and there
5503 is a trivial default constructor or at least one constexpr
5504 constructor other than the copy constructor. */
5505
5506 static void
5507 finalize_literal_type_property (tree t)
5508 {
5509 tree fn;
5510
5511 if (cxx_dialect < cxx11
5512 || TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t))
5513 CLASSTYPE_LITERAL_P (t) = false;
5514 else if (CLASSTYPE_LITERAL_P (t) && !TYPE_HAS_TRIVIAL_DFLT (t)
5515 && CLASSTYPE_NON_AGGREGATE (t)
5516 && !TYPE_HAS_CONSTEXPR_CTOR (t))
5517 CLASSTYPE_LITERAL_P (t) = false;
5518
5519 if (!CLASSTYPE_LITERAL_P (t))
5520 for (fn = TYPE_METHODS (t); fn; fn = DECL_CHAIN (fn))
5521 if (DECL_DECLARED_CONSTEXPR_P (fn)
5522 && TREE_CODE (fn) != TEMPLATE_DECL
5523 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
5524 && !DECL_CONSTRUCTOR_P (fn))
5525 {
5526 DECL_DECLARED_CONSTEXPR_P (fn) = false;
5527 if (!DECL_GENERATED_P (fn))
5528 {
5529 error ("enclosing class of constexpr non-static member "
5530 "function %q+#D is not a literal type", fn);
5531 explain_non_literal_class (t);
5532 }
5533 }
5534 }
5535
5536 /* T is a non-literal type used in a context which requires a constant
5537 expression. Explain why it isn't literal. */
5538
5539 void
5540 explain_non_literal_class (tree t)
5541 {
5542 static hash_set<tree> *diagnosed;
5543
5544 if (!CLASS_TYPE_P (t))
5545 return;
5546 t = TYPE_MAIN_VARIANT (t);
5547
5548 if (diagnosed == NULL)
5549 diagnosed = new hash_set<tree>;
5550 if (diagnosed->add (t))
5551 /* Already explained. */
5552 return;
5553
5554 inform (0, "%q+T is not literal because:", t);
5555 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t))
5556 inform (0, " %q+T has a non-trivial destructor", t);
5557 else if (CLASSTYPE_NON_AGGREGATE (t)
5558 && !TYPE_HAS_TRIVIAL_DFLT (t)
5559 && !TYPE_HAS_CONSTEXPR_CTOR (t))
5560 {
5561 inform (0, " %q+T is not an aggregate, does not have a trivial "
5562 "default constructor, and has no constexpr constructor that "
5563 "is not a copy or move constructor", t);
5564 if (type_has_non_user_provided_default_constructor (t))
5565 {
5566 /* Note that we can't simply call locate_ctor because when the
5567 constructor is deleted it just returns NULL_TREE. */
5568 tree fns;
5569 for (fns = CLASSTYPE_CONSTRUCTORS (t); fns; fns = OVL_NEXT (fns))
5570 {
5571 tree fn = OVL_CURRENT (fns);
5572 tree parms = TYPE_ARG_TYPES (TREE_TYPE (fn));
5573
5574 parms = skip_artificial_parms_for (fn, parms);
5575
5576 if (sufficient_parms_p (parms))
5577 {
5578 if (DECL_DELETED_FN (fn))
5579 maybe_explain_implicit_delete (fn);
5580 else
5581 explain_invalid_constexpr_fn (fn);
5582 break;
5583 }
5584 }
5585 }
5586 }
5587 else
5588 {
5589 tree binfo, base_binfo, field; int i;
5590 for (binfo = TYPE_BINFO (t), i = 0;
5591 BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
5592 {
5593 tree basetype = TREE_TYPE (base_binfo);
5594 if (!CLASSTYPE_LITERAL_P (basetype))
5595 {
5596 inform (0, " base class %qT of %q+T is non-literal",
5597 basetype, t);
5598 explain_non_literal_class (basetype);
5599 return;
5600 }
5601 }
5602 for (field = TYPE_FIELDS (t); field; field = TREE_CHAIN (field))
5603 {
5604 tree ftype;
5605 if (TREE_CODE (field) != FIELD_DECL)
5606 continue;
5607 ftype = TREE_TYPE (field);
5608 if (!literal_type_p (ftype))
5609 {
5610 inform (0, " non-static data member %q+D has "
5611 "non-literal type", field);
5612 if (CLASS_TYPE_P (ftype))
5613 explain_non_literal_class (ftype);
5614 }
5615 if (CP_TYPE_VOLATILE_P (ftype))
5616 inform (0, " non-static data member %q+D has "
5617 "volatile type", field);
5618 }
5619 }
5620 }
5621
5622 /* Check the validity of the bases and members declared in T. Add any
5623 implicitly-generated functions (like copy-constructors and
5624 assignment operators). Compute various flag bits (like
5625 CLASSTYPE_NON_LAYOUT_POD_T) for T. This routine works purely at the C++
5626 level: i.e., independently of the ABI in use. */
5627
5628 static void
5629 check_bases_and_members (tree t)
5630 {
5631 /* Nonzero if the implicitly generated copy constructor should take
5632 a non-const reference argument. */
5633 int cant_have_const_ctor;
5634 /* Nonzero if the implicitly generated assignment operator
5635 should take a non-const reference argument. */
5636 int no_const_asn_ref;
5637 tree access_decls;
5638 bool saved_complex_asn_ref;
5639 bool saved_nontrivial_dtor;
5640 tree fn;
5641
5642 /* By default, we use const reference arguments and generate default
5643 constructors. */
5644 cant_have_const_ctor = 0;
5645 no_const_asn_ref = 0;
5646
5647 /* Check all the base-classes. */
5648 check_bases (t, &cant_have_const_ctor,
5649 &no_const_asn_ref);
5650
5651 /* Deduce noexcept on destructors. This needs to happen after we've set
5652 triviality flags appropriately for our bases. */
5653 if (cxx_dialect >= cxx11)
5654 deduce_noexcept_on_destructors (t);
5655
5656 /* Check all the method declarations. */
5657 check_methods (t);
5658
5659 /* Save the initial values of these flags which only indicate whether
5660 or not the class has user-provided functions. As we analyze the
5661 bases and members we can set these flags for other reasons. */
5662 saved_complex_asn_ref = TYPE_HAS_COMPLEX_COPY_ASSIGN (t);
5663 saved_nontrivial_dtor = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t);
5664
5665 /* Check all the data member declarations. We cannot call
5666 check_field_decls until we have called check_bases check_methods,
5667 as check_field_decls depends on TYPE_HAS_NONTRIVIAL_DESTRUCTOR
5668 being set appropriately. */
5669 check_field_decls (t, &access_decls,
5670 &cant_have_const_ctor,
5671 &no_const_asn_ref);
5672
5673 /* A nearly-empty class has to be vptr-containing; a nearly empty
5674 class contains just a vptr. */
5675 if (!TYPE_CONTAINS_VPTR_P (t))
5676 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
5677
5678 /* Do some bookkeeping that will guide the generation of implicitly
5679 declared member functions. */
5680 TYPE_HAS_COMPLEX_COPY_CTOR (t) |= TYPE_CONTAINS_VPTR_P (t);
5681 TYPE_HAS_COMPLEX_MOVE_CTOR (t) |= TYPE_CONTAINS_VPTR_P (t);
5682 /* We need to call a constructor for this class if it has a
5683 user-provided constructor, or if the default constructor is going
5684 to initialize the vptr. (This is not an if-and-only-if;
5685 TYPE_NEEDS_CONSTRUCTING is set elsewhere if bases or members
5686 themselves need constructing.) */
5687 TYPE_NEEDS_CONSTRUCTING (t)
5688 |= (type_has_user_provided_constructor (t) || TYPE_CONTAINS_VPTR_P (t));
5689 /* [dcl.init.aggr]
5690
5691 An aggregate is an array or a class with no user-provided
5692 constructors ... and no virtual functions.
5693
5694 Again, other conditions for being an aggregate are checked
5695 elsewhere. */
5696 CLASSTYPE_NON_AGGREGATE (t)
5697 |= (type_has_user_provided_constructor (t) || TYPE_POLYMORPHIC_P (t));
5698 /* This is the C++98/03 definition of POD; it changed in C++0x, but we
5699 retain the old definition internally for ABI reasons. */
5700 CLASSTYPE_NON_LAYOUT_POD_P (t)
5701 |= (CLASSTYPE_NON_AGGREGATE (t)
5702 || saved_nontrivial_dtor || saved_complex_asn_ref);
5703 CLASSTYPE_NON_STD_LAYOUT (t) |= TYPE_CONTAINS_VPTR_P (t);
5704 TYPE_HAS_COMPLEX_COPY_ASSIGN (t) |= TYPE_CONTAINS_VPTR_P (t);
5705 TYPE_HAS_COMPLEX_MOVE_ASSIGN (t) |= TYPE_CONTAINS_VPTR_P (t);
5706 TYPE_HAS_COMPLEX_DFLT (t) |= TYPE_CONTAINS_VPTR_P (t);
5707
5708 /* If the only explicitly declared default constructor is user-provided,
5709 set TYPE_HAS_COMPLEX_DFLT. */
5710 if (!TYPE_HAS_COMPLEX_DFLT (t)
5711 && TYPE_HAS_DEFAULT_CONSTRUCTOR (t)
5712 && !type_has_non_user_provided_default_constructor (t))
5713 TYPE_HAS_COMPLEX_DFLT (t) = true;
5714
5715 /* Warn if a public base of a polymorphic type has an accessible
5716 non-virtual destructor. It is only now that we know the class is
5717 polymorphic. Although a polymorphic base will have a already
5718 been diagnosed during its definition, we warn on use too. */
5719 if (TYPE_POLYMORPHIC_P (t) && warn_nonvdtor)
5720 {
5721 tree binfo = TYPE_BINFO (t);
5722 vec<tree, va_gc> *accesses = BINFO_BASE_ACCESSES (binfo);
5723 tree base_binfo;
5724 unsigned i;
5725
5726 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
5727 {
5728 tree basetype = TREE_TYPE (base_binfo);
5729
5730 if ((*accesses)[i] == access_public_node
5731 && (TYPE_POLYMORPHIC_P (basetype) || warn_ecpp)
5732 && accessible_nvdtor_p (basetype))
5733 warning (OPT_Wnon_virtual_dtor,
5734 "base class %q#T has accessible non-virtual destructor",
5735 basetype);
5736 }
5737 }
5738
5739 /* If the class has no user-declared constructor, but does have
5740 non-static const or reference data members that can never be
5741 initialized, issue a warning. */
5742 if (warn_uninitialized
5743 /* Classes with user-declared constructors are presumed to
5744 initialize these members. */
5745 && !TYPE_HAS_USER_CONSTRUCTOR (t)
5746 /* Aggregates can be initialized with brace-enclosed
5747 initializers. */
5748 && CLASSTYPE_NON_AGGREGATE (t))
5749 {
5750 tree field;
5751
5752 for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
5753 {
5754 tree type;
5755
5756 if (TREE_CODE (field) != FIELD_DECL
5757 || DECL_INITIAL (field) != NULL_TREE)
5758 continue;
5759
5760 type = TREE_TYPE (field);
5761 if (TREE_CODE (type) == REFERENCE_TYPE)
5762 warning (OPT_Wuninitialized, "non-static reference %q+#D "
5763 "in class without a constructor", field);
5764 else if (CP_TYPE_CONST_P (type)
5765 && (!CLASS_TYPE_P (type)
5766 || !TYPE_HAS_DEFAULT_CONSTRUCTOR (type)))
5767 warning (OPT_Wuninitialized, "non-static const member %q+#D "
5768 "in class without a constructor", field);
5769 }
5770 }
5771
5772 /* Synthesize any needed methods. */
5773 add_implicitly_declared_members (t, &access_decls,
5774 cant_have_const_ctor,
5775 no_const_asn_ref);
5776
5777 /* Check defaulted declarations here so we have cant_have_const_ctor
5778 and don't need to worry about clones. */
5779 for (fn = TYPE_METHODS (t); fn; fn = DECL_CHAIN (fn))
5780 if (!DECL_ARTIFICIAL (fn) && DECL_DEFAULTED_IN_CLASS_P (fn))
5781 {
5782 int copy = copy_fn_p (fn);
5783 if (copy > 0)
5784 {
5785 bool imp_const_p
5786 = (DECL_CONSTRUCTOR_P (fn) ? !cant_have_const_ctor
5787 : !no_const_asn_ref);
5788 bool fn_const_p = (copy == 2);
5789
5790 if (fn_const_p && !imp_const_p)
5791 /* If the function is defaulted outside the class, we just
5792 give the synthesis error. */
5793 error ("%q+D declared to take const reference, but implicit "
5794 "declaration would take non-const", fn);
5795 }
5796 defaulted_late_check (fn);
5797 }
5798
5799 if (LAMBDA_TYPE_P (t))
5800 {
5801 /* "This class type is not an aggregate." */
5802 CLASSTYPE_NON_AGGREGATE (t) = 1;
5803 }
5804
5805 /* Compute the 'literal type' property before we
5806 do anything with non-static member functions. */
5807 finalize_literal_type_property (t);
5808
5809 /* Create the in-charge and not-in-charge variants of constructors
5810 and destructors. */
5811 clone_constructors_and_destructors (t);
5812
5813 /* Process the using-declarations. */
5814 for (; access_decls; access_decls = TREE_CHAIN (access_decls))
5815 handle_using_decl (TREE_VALUE (access_decls), t);
5816
5817 /* Build and sort the CLASSTYPE_METHOD_VEC. */
5818 finish_struct_methods (t);
5819
5820 /* Figure out whether or not we will need a cookie when dynamically
5821 allocating an array of this type. */
5822 TYPE_LANG_SPECIFIC (t)->u.c.vec_new_uses_cookie
5823 = type_requires_array_cookie (t);
5824 }
5825
5826 /* If T needs a pointer to its virtual function table, set TYPE_VFIELD
5827 accordingly. If a new vfield was created (because T doesn't have a
5828 primary base class), then the newly created field is returned. It
5829 is not added to the TYPE_FIELDS list; it is the caller's
5830 responsibility to do that. Accumulate declared virtual functions
5831 on VIRTUALS_P. */
5832
5833 static tree
5834 create_vtable_ptr (tree t, tree* virtuals_p)
5835 {
5836 tree fn;
5837
5838 /* Collect the virtual functions declared in T. */
5839 for (fn = TYPE_METHODS (t); fn; fn = DECL_CHAIN (fn))
5840 if (TREE_CODE (fn) == FUNCTION_DECL
5841 && DECL_VINDEX (fn) && !DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (fn)
5842 && TREE_CODE (DECL_VINDEX (fn)) != INTEGER_CST)
5843 {
5844 tree new_virtual = make_node (TREE_LIST);
5845
5846 BV_FN (new_virtual) = fn;
5847 BV_DELTA (new_virtual) = integer_zero_node;
5848 BV_VCALL_INDEX (new_virtual) = NULL_TREE;
5849
5850 TREE_CHAIN (new_virtual) = *virtuals_p;
5851 *virtuals_p = new_virtual;
5852 }
5853
5854 /* If we couldn't find an appropriate base class, create a new field
5855 here. Even if there weren't any new virtual functions, we might need a
5856 new virtual function table if we're supposed to include vptrs in
5857 all classes that need them. */
5858 if (!TYPE_VFIELD (t) && (*virtuals_p || TYPE_CONTAINS_VPTR_P (t)))
5859 {
5860 /* We build this decl with vtbl_ptr_type_node, which is a
5861 `vtable_entry_type*'. It might seem more precise to use
5862 `vtable_entry_type (*)[N]' where N is the number of virtual
5863 functions. However, that would require the vtable pointer in
5864 base classes to have a different type than the vtable pointer
5865 in derived classes. We could make that happen, but that
5866 still wouldn't solve all the problems. In particular, the
5867 type-based alias analysis code would decide that assignments
5868 to the base class vtable pointer can't alias assignments to
5869 the derived class vtable pointer, since they have different
5870 types. Thus, in a derived class destructor, where the base
5871 class constructor was inlined, we could generate bad code for
5872 setting up the vtable pointer.
5873
5874 Therefore, we use one type for all vtable pointers. We still
5875 use a type-correct type; it's just doesn't indicate the array
5876 bounds. That's better than using `void*' or some such; it's
5877 cleaner, and it let's the alias analysis code know that these
5878 stores cannot alias stores to void*! */
5879 tree field;
5880
5881 field = build_decl (input_location,
5882 FIELD_DECL, get_vfield_name (t), vtbl_ptr_type_node);
5883 DECL_VIRTUAL_P (field) = 1;
5884 DECL_ARTIFICIAL (field) = 1;
5885 DECL_FIELD_CONTEXT (field) = t;
5886 DECL_FCONTEXT (field) = t;
5887 if (TYPE_PACKED (t))
5888 DECL_PACKED (field) = 1;
5889
5890 TYPE_VFIELD (t) = field;
5891
5892 /* This class is non-empty. */
5893 CLASSTYPE_EMPTY_P (t) = 0;
5894
5895 return field;
5896 }
5897
5898 return NULL_TREE;
5899 }
5900
5901 /* Add OFFSET to all base types of BINFO which is a base in the
5902 hierarchy dominated by T.
5903
5904 OFFSET, which is a type offset, is number of bytes. */
5905
5906 static void
5907 propagate_binfo_offsets (tree binfo, tree offset)
5908 {
5909 int i;
5910 tree primary_binfo;
5911 tree base_binfo;
5912
5913 /* Update BINFO's offset. */
5914 BINFO_OFFSET (binfo)
5915 = convert (sizetype,
5916 size_binop (PLUS_EXPR,
5917 convert (ssizetype, BINFO_OFFSET (binfo)),
5918 offset));
5919
5920 /* Find the primary base class. */
5921 primary_binfo = get_primary_binfo (binfo);
5922
5923 if (primary_binfo && BINFO_INHERITANCE_CHAIN (primary_binfo) == binfo)
5924 propagate_binfo_offsets (primary_binfo, offset);
5925
5926 /* Scan all of the bases, pushing the BINFO_OFFSET adjust
5927 downwards. */
5928 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
5929 {
5930 /* Don't do the primary base twice. */
5931 if (base_binfo == primary_binfo)
5932 continue;
5933
5934 if (BINFO_VIRTUAL_P (base_binfo))
5935 continue;
5936
5937 propagate_binfo_offsets (base_binfo, offset);
5938 }
5939 }
5940
5941 /* Set BINFO_OFFSET for all of the virtual bases for RLI->T. Update
5942 TYPE_ALIGN and TYPE_SIZE for T. OFFSETS gives the location of
5943 empty subobjects of T. */
5944
5945 static void
5946 layout_virtual_bases (record_layout_info rli, splay_tree offsets)
5947 {
5948 tree vbase;
5949 tree t = rli->t;
5950 tree *next_field;
5951
5952 if (BINFO_N_BASE_BINFOS (TYPE_BINFO (t)) == 0)
5953 return;
5954
5955 /* Find the last field. The artificial fields created for virtual
5956 bases will go after the last extant field to date. */
5957 next_field = &TYPE_FIELDS (t);
5958 while (*next_field)
5959 next_field = &DECL_CHAIN (*next_field);
5960
5961 /* Go through the virtual bases, allocating space for each virtual
5962 base that is not already a primary base class. These are
5963 allocated in inheritance graph order. */
5964 for (vbase = TYPE_BINFO (t); vbase; vbase = TREE_CHAIN (vbase))
5965 {
5966 if (!BINFO_VIRTUAL_P (vbase))
5967 continue;
5968
5969 if (!BINFO_PRIMARY_P (vbase))
5970 {
5971 /* This virtual base is not a primary base of any class in the
5972 hierarchy, so we have to add space for it. */
5973 next_field = build_base_field (rli, vbase,
5974 offsets, next_field);
5975 }
5976 }
5977 }
5978
5979 /* Returns the offset of the byte just past the end of the base class
5980 BINFO. */
5981
5982 static tree
5983 end_of_base (tree binfo)
5984 {
5985 tree size;
5986
5987 if (!CLASSTYPE_AS_BASE (BINFO_TYPE (binfo)))
5988 size = TYPE_SIZE_UNIT (char_type_node);
5989 else if (is_empty_class (BINFO_TYPE (binfo)))
5990 /* An empty class has zero CLASSTYPE_SIZE_UNIT, but we need to
5991 allocate some space for it. It cannot have virtual bases, so
5992 TYPE_SIZE_UNIT is fine. */
5993 size = TYPE_SIZE_UNIT (BINFO_TYPE (binfo));
5994 else
5995 size = CLASSTYPE_SIZE_UNIT (BINFO_TYPE (binfo));
5996
5997 return size_binop (PLUS_EXPR, BINFO_OFFSET (binfo), size);
5998 }
5999
6000 /* Returns the offset of the byte just past the end of the base class
6001 with the highest offset in T. If INCLUDE_VIRTUALS_P is zero, then
6002 only non-virtual bases are included. */
6003
6004 static tree
6005 end_of_class (tree t, int include_virtuals_p)
6006 {
6007 tree result = size_zero_node;
6008 vec<tree, va_gc> *vbases;
6009 tree binfo;
6010 tree base_binfo;
6011 tree offset;
6012 int i;
6013
6014 for (binfo = TYPE_BINFO (t), i = 0;
6015 BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
6016 {
6017 if (!include_virtuals_p
6018 && BINFO_VIRTUAL_P (base_binfo)
6019 && (!BINFO_PRIMARY_P (base_binfo)
6020 || BINFO_INHERITANCE_CHAIN (base_binfo) != TYPE_BINFO (t)))
6021 continue;
6022
6023 offset = end_of_base (base_binfo);
6024 if (tree_int_cst_lt (result, offset))
6025 result = offset;
6026 }
6027
6028 if (include_virtuals_p)
6029 for (vbases = CLASSTYPE_VBASECLASSES (t), i = 0;
6030 vec_safe_iterate (vbases, i, &base_binfo); i++)
6031 {
6032 offset = end_of_base (base_binfo);
6033 if (tree_int_cst_lt (result, offset))
6034 result = offset;
6035 }
6036
6037 return result;
6038 }
6039
6040 /* Warn about bases of T that are inaccessible because they are
6041 ambiguous. For example:
6042
6043 struct S {};
6044 struct T : public S {};
6045 struct U : public S, public T {};
6046
6047 Here, `(S*) new U' is not allowed because there are two `S'
6048 subobjects of U. */
6049
6050 static void
6051 warn_about_ambiguous_bases (tree t)
6052 {
6053 int i;
6054 vec<tree, va_gc> *vbases;
6055 tree basetype;
6056 tree binfo;
6057 tree base_binfo;
6058
6059 /* If there are no repeated bases, nothing can be ambiguous. */
6060 if (!CLASSTYPE_REPEATED_BASE_P (t))
6061 return;
6062
6063 /* Check direct bases. */
6064 for (binfo = TYPE_BINFO (t), i = 0;
6065 BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
6066 {
6067 basetype = BINFO_TYPE (base_binfo);
6068
6069 if (!uniquely_derived_from_p (basetype, t))
6070 warning (0, "direct base %qT inaccessible in %qT due to ambiguity",
6071 basetype, t);
6072 }
6073
6074 /* Check for ambiguous virtual bases. */
6075 if (extra_warnings)
6076 for (vbases = CLASSTYPE_VBASECLASSES (t), i = 0;
6077 vec_safe_iterate (vbases, i, &binfo); i++)
6078 {
6079 basetype = BINFO_TYPE (binfo);
6080
6081 if (!uniquely_derived_from_p (basetype, t))
6082 warning (OPT_Wextra, "virtual base %qT inaccessible in %qT due "
6083 "to ambiguity", basetype, t);
6084 }
6085 }
6086
6087 /* Compare two INTEGER_CSTs K1 and K2. */
6088
6089 static int
6090 splay_tree_compare_integer_csts (splay_tree_key k1, splay_tree_key k2)
6091 {
6092 return tree_int_cst_compare ((tree) k1, (tree) k2);
6093 }
6094
6095 /* Increase the size indicated in RLI to account for empty classes
6096 that are "off the end" of the class. */
6097
6098 static void
6099 include_empty_classes (record_layout_info rli)
6100 {
6101 tree eoc;
6102 tree rli_size;
6103
6104 /* It might be the case that we grew the class to allocate a
6105 zero-sized base class. That won't be reflected in RLI, yet,
6106 because we are willing to overlay multiple bases at the same
6107 offset. However, now we need to make sure that RLI is big enough
6108 to reflect the entire class. */
6109 eoc = end_of_class (rli->t,
6110 CLASSTYPE_AS_BASE (rli->t) != NULL_TREE);
6111 rli_size = rli_size_unit_so_far (rli);
6112 if (TREE_CODE (rli_size) == INTEGER_CST
6113 && tree_int_cst_lt (rli_size, eoc))
6114 {
6115 /* The size should have been rounded to a whole byte. */
6116 gcc_assert (tree_int_cst_equal
6117 (rli->bitpos, round_down (rli->bitpos, BITS_PER_UNIT)));
6118 rli->bitpos
6119 = size_binop (PLUS_EXPR,
6120 rli->bitpos,
6121 size_binop (MULT_EXPR,
6122 convert (bitsizetype,
6123 size_binop (MINUS_EXPR,
6124 eoc, rli_size)),
6125 bitsize_int (BITS_PER_UNIT)));
6126 normalize_rli (rli);
6127 }
6128 }
6129
6130 /* Calculate the TYPE_SIZE, TYPE_ALIGN, etc for T. Calculate
6131 BINFO_OFFSETs for all of the base-classes. Position the vtable
6132 pointer. Accumulate declared virtual functions on VIRTUALS_P. */
6133
6134 static void
6135 layout_class_type (tree t, tree *virtuals_p)
6136 {
6137 tree non_static_data_members;
6138 tree field;
6139 tree vptr;
6140 record_layout_info rli;
6141 /* Maps offsets (represented as INTEGER_CSTs) to a TREE_LIST of
6142 types that appear at that offset. */
6143 splay_tree empty_base_offsets;
6144 /* True if the last field laid out was a bit-field. */
6145 bool last_field_was_bitfield = false;
6146 /* The location at which the next field should be inserted. */
6147 tree *next_field;
6148 /* T, as a base class. */
6149 tree base_t;
6150
6151 /* Keep track of the first non-static data member. */
6152 non_static_data_members = TYPE_FIELDS (t);
6153
6154 /* Start laying out the record. */
6155 rli = start_record_layout (t);
6156
6157 /* Mark all the primary bases in the hierarchy. */
6158 determine_primary_bases (t);
6159
6160 /* Create a pointer to our virtual function table. */
6161 vptr = create_vtable_ptr (t, virtuals_p);
6162
6163 /* The vptr is always the first thing in the class. */
6164 if (vptr)
6165 {
6166 DECL_CHAIN (vptr) = TYPE_FIELDS (t);
6167 TYPE_FIELDS (t) = vptr;
6168 next_field = &DECL_CHAIN (vptr);
6169 place_field (rli, vptr);
6170 }
6171 else
6172 next_field = &TYPE_FIELDS (t);
6173
6174 /* Build FIELD_DECLs for all of the non-virtual base-types. */
6175 empty_base_offsets = splay_tree_new (splay_tree_compare_integer_csts,
6176 NULL, NULL);
6177 build_base_fields (rli, empty_base_offsets, next_field);
6178
6179 /* Layout the non-static data members. */
6180 for (field = non_static_data_members; field; field = DECL_CHAIN (field))
6181 {
6182 tree type;
6183 tree padding;
6184
6185 /* We still pass things that aren't non-static data members to
6186 the back end, in case it wants to do something with them. */
6187 if (TREE_CODE (field) != FIELD_DECL)
6188 {
6189 place_field (rli, field);
6190 /* If the static data member has incomplete type, keep track
6191 of it so that it can be completed later. (The handling
6192 of pending statics in finish_record_layout is
6193 insufficient; consider:
6194
6195 struct S1;
6196 struct S2 { static S1 s1; };
6197
6198 At this point, finish_record_layout will be called, but
6199 S1 is still incomplete.) */
6200 if (VAR_P (field))
6201 {
6202 maybe_register_incomplete_var (field);
6203 /* The visibility of static data members is determined
6204 at their point of declaration, not their point of
6205 definition. */
6206 determine_visibility (field);
6207 }
6208 continue;
6209 }
6210
6211 type = TREE_TYPE (field);
6212 if (type == error_mark_node)
6213 continue;
6214
6215 padding = NULL_TREE;
6216
6217 /* If this field is a bit-field whose width is greater than its
6218 type, then there are some special rules for allocating
6219 it. */
6220 if (DECL_C_BIT_FIELD (field)
6221 && tree_int_cst_lt (TYPE_SIZE (type), DECL_SIZE (field)))
6222 {
6223 unsigned int itk;
6224 tree integer_type;
6225 bool was_unnamed_p = false;
6226 /* We must allocate the bits as if suitably aligned for the
6227 longest integer type that fits in this many bits. type
6228 of the field. Then, we are supposed to use the left over
6229 bits as additional padding. */
6230 for (itk = itk_char; itk != itk_none; ++itk)
6231 if (integer_types[itk] != NULL_TREE
6232 && (tree_int_cst_lt (size_int (MAX_FIXED_MODE_SIZE),
6233 TYPE_SIZE (integer_types[itk]))
6234 || tree_int_cst_lt (DECL_SIZE (field),
6235 TYPE_SIZE (integer_types[itk]))))
6236 break;
6237
6238 /* ITK now indicates a type that is too large for the
6239 field. We have to back up by one to find the largest
6240 type that fits. */
6241 do
6242 {
6243 --itk;
6244 integer_type = integer_types[itk];
6245 } while (itk > 0 && integer_type == NULL_TREE);
6246
6247 /* Figure out how much additional padding is required. */
6248 if (tree_int_cst_lt (TYPE_SIZE (integer_type), DECL_SIZE (field)))
6249 {
6250 if (TREE_CODE (t) == UNION_TYPE)
6251 /* In a union, the padding field must have the full width
6252 of the bit-field; all fields start at offset zero. */
6253 padding = DECL_SIZE (field);
6254 else
6255 padding = size_binop (MINUS_EXPR, DECL_SIZE (field),
6256 TYPE_SIZE (integer_type));
6257 }
6258
6259 /* An unnamed bitfield does not normally affect the
6260 alignment of the containing class on a target where
6261 PCC_BITFIELD_TYPE_MATTERS. But, the C++ ABI does not
6262 make any exceptions for unnamed bitfields when the
6263 bitfields are longer than their types. Therefore, we
6264 temporarily give the field a name. */
6265 if (PCC_BITFIELD_TYPE_MATTERS && !DECL_NAME (field))
6266 {
6267 was_unnamed_p = true;
6268 DECL_NAME (field) = make_anon_name ();
6269 }
6270
6271 DECL_SIZE (field) = TYPE_SIZE (integer_type);
6272 DECL_ALIGN (field) = TYPE_ALIGN (integer_type);
6273 DECL_USER_ALIGN (field) = TYPE_USER_ALIGN (integer_type);
6274 layout_nonempty_base_or_field (rli, field, NULL_TREE,
6275 empty_base_offsets);
6276 if (was_unnamed_p)
6277 DECL_NAME (field) = NULL_TREE;
6278 /* Now that layout has been performed, set the size of the
6279 field to the size of its declared type; the rest of the
6280 field is effectively invisible. */
6281 DECL_SIZE (field) = TYPE_SIZE (type);
6282 /* We must also reset the DECL_MODE of the field. */
6283 DECL_MODE (field) = TYPE_MODE (type);
6284 }
6285 else
6286 layout_nonempty_base_or_field (rli, field, NULL_TREE,
6287 empty_base_offsets);
6288
6289 /* Remember the location of any empty classes in FIELD. */
6290 record_subobject_offsets (TREE_TYPE (field),
6291 byte_position(field),
6292 empty_base_offsets,
6293 /*is_data_member=*/true);
6294
6295 /* If a bit-field does not immediately follow another bit-field,
6296 and yet it starts in the middle of a byte, we have failed to
6297 comply with the ABI. */
6298 if (warn_abi
6299 && DECL_C_BIT_FIELD (field)
6300 /* The TREE_NO_WARNING flag gets set by Objective-C when
6301 laying out an Objective-C class. The ObjC ABI differs
6302 from the C++ ABI, and so we do not want a warning
6303 here. */
6304 && !TREE_NO_WARNING (field)
6305 && !last_field_was_bitfield
6306 && !integer_zerop (size_binop (TRUNC_MOD_EXPR,
6307 DECL_FIELD_BIT_OFFSET (field),
6308 bitsize_unit_node)))
6309 warning (OPT_Wabi, "offset of %q+D is not ABI-compliant and may "
6310 "change in a future version of GCC", field);
6311
6312 /* The middle end uses the type of expressions to determine the
6313 possible range of expression values. In order to optimize
6314 "x.i > 7" to "false" for a 2-bit bitfield "i", the middle end
6315 must be made aware of the width of "i", via its type.
6316
6317 Because C++ does not have integer types of arbitrary width,
6318 we must (for the purposes of the front end) convert from the
6319 type assigned here to the declared type of the bitfield
6320 whenever a bitfield expression is used as an rvalue.
6321 Similarly, when assigning a value to a bitfield, the value
6322 must be converted to the type given the bitfield here. */
6323 if (DECL_C_BIT_FIELD (field))
6324 {
6325 unsigned HOST_WIDE_INT width;
6326 tree ftype = TREE_TYPE (field);
6327 width = tree_to_uhwi (DECL_SIZE (field));
6328 if (width != TYPE_PRECISION (ftype))
6329 {
6330 TREE_TYPE (field)
6331 = c_build_bitfield_integer_type (width,
6332 TYPE_UNSIGNED (ftype));
6333 TREE_TYPE (field)
6334 = cp_build_qualified_type (TREE_TYPE (field),
6335 cp_type_quals (ftype));
6336 }
6337 }
6338
6339 /* If we needed additional padding after this field, add it
6340 now. */
6341 if (padding)
6342 {
6343 tree padding_field;
6344
6345 padding_field = build_decl (input_location,
6346 FIELD_DECL,
6347 NULL_TREE,
6348 char_type_node);
6349 DECL_BIT_FIELD (padding_field) = 1;
6350 DECL_SIZE (padding_field) = padding;
6351 DECL_CONTEXT (padding_field) = t;
6352 DECL_ARTIFICIAL (padding_field) = 1;
6353 DECL_IGNORED_P (padding_field) = 1;
6354 layout_nonempty_base_or_field (rli, padding_field,
6355 NULL_TREE,
6356 empty_base_offsets);
6357 }
6358
6359 last_field_was_bitfield = DECL_C_BIT_FIELD (field);
6360 }
6361
6362 if (!integer_zerop (rli->bitpos))
6363 {
6364 /* Make sure that we are on a byte boundary so that the size of
6365 the class without virtual bases will always be a round number
6366 of bytes. */
6367 rli->bitpos = round_up_loc (input_location, rli->bitpos, BITS_PER_UNIT);
6368 normalize_rli (rli);
6369 }
6370
6371 /* Delete all zero-width bit-fields from the list of fields. Now
6372 that the type is laid out they are no longer important. */
6373 remove_zero_width_bit_fields (t);
6374
6375 /* Create the version of T used for virtual bases. We do not use
6376 make_class_type for this version; this is an artificial type. For
6377 a POD type, we just reuse T. */
6378 if (CLASSTYPE_NON_LAYOUT_POD_P (t) || CLASSTYPE_EMPTY_P (t))
6379 {
6380 base_t = make_node (TREE_CODE (t));
6381
6382 /* Set the size and alignment for the new type. */
6383 tree eoc;
6384
6385 /* If the ABI version is not at least two, and the last
6386 field was a bit-field, RLI may not be on a byte
6387 boundary. In particular, rli_size_unit_so_far might
6388 indicate the last complete byte, while rli_size_so_far
6389 indicates the total number of bits used. Therefore,
6390 rli_size_so_far, rather than rli_size_unit_so_far, is
6391 used to compute TYPE_SIZE_UNIT. */
6392 eoc = end_of_class (t, /*include_virtuals_p=*/0);
6393 TYPE_SIZE_UNIT (base_t)
6394 = size_binop (MAX_EXPR,
6395 convert (sizetype,
6396 size_binop (CEIL_DIV_EXPR,
6397 rli_size_so_far (rli),
6398 bitsize_int (BITS_PER_UNIT))),
6399 eoc);
6400 TYPE_SIZE (base_t)
6401 = size_binop (MAX_EXPR,
6402 rli_size_so_far (rli),
6403 size_binop (MULT_EXPR,
6404 convert (bitsizetype, eoc),
6405 bitsize_int (BITS_PER_UNIT)));
6406 TYPE_ALIGN (base_t) = rli->record_align;
6407 TYPE_USER_ALIGN (base_t) = TYPE_USER_ALIGN (t);
6408
6409 /* Copy the fields from T. */
6410 next_field = &TYPE_FIELDS (base_t);
6411 for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
6412 if (TREE_CODE (field) == FIELD_DECL)
6413 {
6414 *next_field = build_decl (input_location,
6415 FIELD_DECL,
6416 DECL_NAME (field),
6417 TREE_TYPE (field));
6418 DECL_CONTEXT (*next_field) = base_t;
6419 DECL_FIELD_OFFSET (*next_field) = DECL_FIELD_OFFSET (field);
6420 DECL_FIELD_BIT_OFFSET (*next_field)
6421 = DECL_FIELD_BIT_OFFSET (field);
6422 DECL_SIZE (*next_field) = DECL_SIZE (field);
6423 DECL_MODE (*next_field) = DECL_MODE (field);
6424 next_field = &DECL_CHAIN (*next_field);
6425 }
6426
6427 /* Record the base version of the type. */
6428 CLASSTYPE_AS_BASE (t) = base_t;
6429 TYPE_CONTEXT (base_t) = t;
6430 }
6431 else
6432 CLASSTYPE_AS_BASE (t) = t;
6433
6434 /* Every empty class contains an empty class. */
6435 if (CLASSTYPE_EMPTY_P (t))
6436 CLASSTYPE_CONTAINS_EMPTY_CLASS_P (t) = 1;
6437
6438 /* Set the TYPE_DECL for this type to contain the right
6439 value for DECL_OFFSET, so that we can use it as part
6440 of a COMPONENT_REF for multiple inheritance. */
6441 layout_decl (TYPE_MAIN_DECL (t), 0);
6442
6443 /* Now fix up any virtual base class types that we left lying
6444 around. We must get these done before we try to lay out the
6445 virtual function table. As a side-effect, this will remove the
6446 base subobject fields. */
6447 layout_virtual_bases (rli, empty_base_offsets);
6448
6449 /* Make sure that empty classes are reflected in RLI at this
6450 point. */
6451 include_empty_classes(rli);
6452
6453 /* Make sure not to create any structures with zero size. */
6454 if (integer_zerop (rli_size_unit_so_far (rli)) && CLASSTYPE_EMPTY_P (t))
6455 place_field (rli,
6456 build_decl (input_location,
6457 FIELD_DECL, NULL_TREE, char_type_node));
6458
6459 /* If this is a non-POD, declaring it packed makes a difference to how it
6460 can be used as a field; don't let finalize_record_size undo it. */
6461 if (TYPE_PACKED (t) && !layout_pod_type_p (t))
6462 rli->packed_maybe_necessary = true;
6463
6464 /* Let the back end lay out the type. */
6465 finish_record_layout (rli, /*free_p=*/true);
6466
6467 if (TYPE_SIZE_UNIT (t)
6468 && TREE_CODE (TYPE_SIZE_UNIT (t)) == INTEGER_CST
6469 && !TREE_OVERFLOW (TYPE_SIZE_UNIT (t))
6470 && !valid_constant_size_p (TYPE_SIZE_UNIT (t)))
6471 error ("type %qT is too large", t);
6472
6473 /* Warn about bases that can't be talked about due to ambiguity. */
6474 warn_about_ambiguous_bases (t);
6475
6476 /* Now that we're done with layout, give the base fields the real types. */
6477 for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
6478 if (DECL_ARTIFICIAL (field) && IS_FAKE_BASE_TYPE (TREE_TYPE (field)))
6479 TREE_TYPE (field) = TYPE_CONTEXT (TREE_TYPE (field));
6480
6481 /* Clean up. */
6482 splay_tree_delete (empty_base_offsets);
6483
6484 if (CLASSTYPE_EMPTY_P (t)
6485 && tree_int_cst_lt (sizeof_biggest_empty_class,
6486 TYPE_SIZE_UNIT (t)))
6487 sizeof_biggest_empty_class = TYPE_SIZE_UNIT (t);
6488 }
6489
6490 /* Determine the "key method" for the class type indicated by TYPE,
6491 and set CLASSTYPE_KEY_METHOD accordingly. */
6492
6493 void
6494 determine_key_method (tree type)
6495 {
6496 tree method;
6497
6498 if (TYPE_FOR_JAVA (type)
6499 || processing_template_decl
6500 || CLASSTYPE_TEMPLATE_INSTANTIATION (type)
6501 || CLASSTYPE_INTERFACE_KNOWN (type))
6502 return;
6503
6504 /* The key method is the first non-pure virtual function that is not
6505 inline at the point of class definition. On some targets the
6506 key function may not be inline; those targets should not call
6507 this function until the end of the translation unit. */
6508 for (method = TYPE_METHODS (type); method != NULL_TREE;
6509 method = DECL_CHAIN (method))
6510 if (TREE_CODE (method) == FUNCTION_DECL
6511 && DECL_VINDEX (method) != NULL_TREE
6512 && ! DECL_DECLARED_INLINE_P (method)
6513 && ! DECL_PURE_VIRTUAL_P (method))
6514 {
6515 CLASSTYPE_KEY_METHOD (type) = method;
6516 break;
6517 }
6518
6519 return;
6520 }
6521
6522
6523 /* Allocate and return an instance of struct sorted_fields_type with
6524 N fields. */
6525
6526 static struct sorted_fields_type *
6527 sorted_fields_type_new (int n)
6528 {
6529 struct sorted_fields_type *sft;
6530 sft = (sorted_fields_type *) ggc_internal_alloc (sizeof (sorted_fields_type)
6531 + n * sizeof (tree));
6532 sft->len = n;
6533
6534 return sft;
6535 }
6536
6537
6538 /* Perform processing required when the definition of T (a class type)
6539 is complete. */
6540
6541 void
6542 finish_struct_1 (tree t)
6543 {
6544 tree x;
6545 /* A TREE_LIST. The TREE_VALUE of each node is a FUNCTION_DECL. */
6546 tree virtuals = NULL_TREE;
6547
6548 if (COMPLETE_TYPE_P (t))
6549 {
6550 gcc_assert (MAYBE_CLASS_TYPE_P (t));
6551 error ("redefinition of %q#T", t);
6552 popclass ();
6553 return;
6554 }
6555
6556 /* If this type was previously laid out as a forward reference,
6557 make sure we lay it out again. */
6558 TYPE_SIZE (t) = NULL_TREE;
6559 CLASSTYPE_PRIMARY_BINFO (t) = NULL_TREE;
6560
6561 /* Make assumptions about the class; we'll reset the flags if
6562 necessary. */
6563 CLASSTYPE_EMPTY_P (t) = 1;
6564 CLASSTYPE_NEARLY_EMPTY_P (t) = 1;
6565 CLASSTYPE_CONTAINS_EMPTY_CLASS_P (t) = 0;
6566 CLASSTYPE_LITERAL_P (t) = true;
6567
6568 /* Do end-of-class semantic processing: checking the validity of the
6569 bases and members and add implicitly generated methods. */
6570 check_bases_and_members (t);
6571
6572 /* Find the key method. */
6573 if (TYPE_CONTAINS_VPTR_P (t))
6574 {
6575 /* The Itanium C++ ABI permits the key method to be chosen when
6576 the class is defined -- even though the key method so
6577 selected may later turn out to be an inline function. On
6578 some systems (such as ARM Symbian OS) the key method cannot
6579 be determined until the end of the translation unit. On such
6580 systems, we leave CLASSTYPE_KEY_METHOD set to NULL, which
6581 will cause the class to be added to KEYED_CLASSES. Then, in
6582 finish_file we will determine the key method. */
6583 if (targetm.cxx.key_method_may_be_inline ())
6584 determine_key_method (t);
6585
6586 /* If a polymorphic class has no key method, we may emit the vtable
6587 in every translation unit where the class definition appears. If
6588 we're devirtualizing, we can look into the vtable even if we
6589 aren't emitting it. */
6590 if (CLASSTYPE_KEY_METHOD (t) == NULL_TREE)
6591 keyed_classes = tree_cons (NULL_TREE, t, keyed_classes);
6592 }
6593
6594 /* Layout the class itself. */
6595 layout_class_type (t, &virtuals);
6596 if (CLASSTYPE_AS_BASE (t) != t)
6597 /* We use the base type for trivial assignments, and hence it
6598 needs a mode. */
6599 compute_record_mode (CLASSTYPE_AS_BASE (t));
6600
6601 virtuals = modify_all_vtables (t, nreverse (virtuals));
6602
6603 /* If necessary, create the primary vtable for this class. */
6604 if (virtuals || TYPE_CONTAINS_VPTR_P (t))
6605 {
6606 /* We must enter these virtuals into the table. */
6607 if (!CLASSTYPE_HAS_PRIMARY_BASE_P (t))
6608 build_primary_vtable (NULL_TREE, t);
6609 else if (! BINFO_NEW_VTABLE_MARKED (TYPE_BINFO (t)))
6610 /* Here we know enough to change the type of our virtual
6611 function table, but we will wait until later this function. */
6612 build_primary_vtable (CLASSTYPE_PRIMARY_BINFO (t), t);
6613
6614 /* If we're warning about ABI tags, check the types of the new
6615 virtual functions. */
6616 if (warn_abi_tag)
6617 for (tree v = virtuals; v; v = TREE_CHAIN (v))
6618 check_abi_tags (t, TREE_VALUE (v));
6619 }
6620
6621 if (TYPE_CONTAINS_VPTR_P (t))
6622 {
6623 int vindex;
6624 tree fn;
6625
6626 if (BINFO_VTABLE (TYPE_BINFO (t)))
6627 gcc_assert (DECL_VIRTUAL_P (BINFO_VTABLE (TYPE_BINFO (t))));
6628 if (!CLASSTYPE_HAS_PRIMARY_BASE_P (t))
6629 gcc_assert (BINFO_VIRTUALS (TYPE_BINFO (t)) == NULL_TREE);
6630
6631 /* Add entries for virtual functions introduced by this class. */
6632 BINFO_VIRTUALS (TYPE_BINFO (t))
6633 = chainon (BINFO_VIRTUALS (TYPE_BINFO (t)), virtuals);
6634
6635 /* Set DECL_VINDEX for all functions declared in this class. */
6636 for (vindex = 0, fn = BINFO_VIRTUALS (TYPE_BINFO (t));
6637 fn;
6638 fn = TREE_CHAIN (fn),
6639 vindex += (TARGET_VTABLE_USES_DESCRIPTORS
6640 ? TARGET_VTABLE_USES_DESCRIPTORS : 1))
6641 {
6642 tree fndecl = BV_FN (fn);
6643
6644 if (DECL_THUNK_P (fndecl))
6645 /* A thunk. We should never be calling this entry directly
6646 from this vtable -- we'd use the entry for the non
6647 thunk base function. */
6648 DECL_VINDEX (fndecl) = NULL_TREE;
6649 else if (TREE_CODE (DECL_VINDEX (fndecl)) != INTEGER_CST)
6650 DECL_VINDEX (fndecl) = build_int_cst (NULL_TREE, vindex);
6651 }
6652 }
6653
6654 finish_struct_bits (t);
6655 set_method_tm_attributes (t);
6656
6657 /* Complete the rtl for any static member objects of the type we're
6658 working on. */
6659 for (x = TYPE_FIELDS (t); x; x = DECL_CHAIN (x))
6660 if (VAR_P (x) && TREE_STATIC (x)
6661 && TREE_TYPE (x) != error_mark_node
6662 && same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (x)), t))
6663 DECL_MODE (x) = TYPE_MODE (t);
6664
6665 /* Done with FIELDS...now decide whether to sort these for
6666 faster lookups later.
6667
6668 We use a small number because most searches fail (succeeding
6669 ultimately as the search bores through the inheritance
6670 hierarchy), and we want this failure to occur quickly. */
6671
6672 insert_into_classtype_sorted_fields (TYPE_FIELDS (t), t, 8);
6673
6674 /* Complain if one of the field types requires lower visibility. */
6675 constrain_class_visibility (t);
6676
6677 /* Make the rtl for any new vtables we have created, and unmark
6678 the base types we marked. */
6679 finish_vtbls (t);
6680
6681 /* Build the VTT for T. */
6682 build_vtt (t);
6683
6684 /* This warning does not make sense for Java classes, since they
6685 cannot have destructors. */
6686 if (!TYPE_FOR_JAVA (t) && warn_nonvdtor
6687 && TYPE_POLYMORPHIC_P (t) && accessible_nvdtor_p (t)
6688 && !CLASSTYPE_FINAL (t))
6689 warning (OPT_Wnon_virtual_dtor,
6690 "%q#T has virtual functions and accessible"
6691 " non-virtual destructor", t);
6692
6693 complete_vars (t);
6694
6695 if (warn_overloaded_virtual)
6696 warn_hidden (t);
6697
6698 /* Class layout, assignment of virtual table slots, etc., is now
6699 complete. Give the back end a chance to tweak the visibility of
6700 the class or perform any other required target modifications. */
6701 targetm.cxx.adjust_class_at_definition (t);
6702
6703 maybe_suppress_debug_info (t);
6704
6705 if (flag_vtable_verify)
6706 vtv_save_class_info (t);
6707
6708 dump_class_hierarchy (t);
6709
6710 /* Finish debugging output for this type. */
6711 rest_of_type_compilation (t, ! LOCAL_CLASS_P (t));
6712
6713 if (TYPE_TRANSPARENT_AGGR (t))
6714 {
6715 tree field = first_field (t);
6716 if (field == NULL_TREE || error_operand_p (field))
6717 {
6718 error ("type transparent %q#T does not have any fields", t);
6719 TYPE_TRANSPARENT_AGGR (t) = 0;
6720 }
6721 else if (DECL_ARTIFICIAL (field))
6722 {
6723 if (DECL_FIELD_IS_BASE (field))
6724 error ("type transparent class %qT has base classes", t);
6725 else
6726 {
6727 gcc_checking_assert (DECL_VIRTUAL_P (field));
6728 error ("type transparent class %qT has virtual functions", t);
6729 }
6730 TYPE_TRANSPARENT_AGGR (t) = 0;
6731 }
6732 else if (TYPE_MODE (t) != DECL_MODE (field))
6733 {
6734 error ("type transparent %q#T cannot be made transparent because "
6735 "the type of the first field has a different ABI from the "
6736 "class overall", t);
6737 TYPE_TRANSPARENT_AGGR (t) = 0;
6738 }
6739 }
6740 }
6741
6742 /* Insert FIELDS into T for the sorted case if the FIELDS count is
6743 equal to THRESHOLD or greater than THRESHOLD. */
6744
6745 static void
6746 insert_into_classtype_sorted_fields (tree fields, tree t, int threshold)
6747 {
6748 int n_fields = count_fields (fields);
6749 if (n_fields >= threshold)
6750 {
6751 struct sorted_fields_type *field_vec = sorted_fields_type_new (n_fields);
6752 add_fields_to_record_type (fields, field_vec, 0);
6753 qsort (field_vec->elts, n_fields, sizeof (tree), field_decl_cmp);
6754 CLASSTYPE_SORTED_FIELDS (t) = field_vec;
6755 }
6756 }
6757
6758 /* Insert lately defined enum ENUMTYPE into T for the sorted case. */
6759
6760 void
6761 insert_late_enum_def_into_classtype_sorted_fields (tree enumtype, tree t)
6762 {
6763 struct sorted_fields_type *sorted_fields = CLASSTYPE_SORTED_FIELDS (t);
6764 if (sorted_fields)
6765 {
6766 int i;
6767 int n_fields
6768 = list_length (TYPE_VALUES (enumtype)) + sorted_fields->len;
6769 struct sorted_fields_type *field_vec = sorted_fields_type_new (n_fields);
6770
6771 for (i = 0; i < sorted_fields->len; ++i)
6772 field_vec->elts[i] = sorted_fields->elts[i];
6773
6774 add_enum_fields_to_record_type (enumtype, field_vec,
6775 sorted_fields->len);
6776 qsort (field_vec->elts, n_fields, sizeof (tree), field_decl_cmp);
6777 CLASSTYPE_SORTED_FIELDS (t) = field_vec;
6778 }
6779 }
6780
6781 /* When T was built up, the member declarations were added in reverse
6782 order. Rearrange them to declaration order. */
6783
6784 void
6785 unreverse_member_declarations (tree t)
6786 {
6787 tree next;
6788 tree prev;
6789 tree x;
6790
6791 /* The following lists are all in reverse order. Put them in
6792 declaration order now. */
6793 TYPE_METHODS (t) = nreverse (TYPE_METHODS (t));
6794 CLASSTYPE_DECL_LIST (t) = nreverse (CLASSTYPE_DECL_LIST (t));
6795
6796 /* Actually, for the TYPE_FIELDS, only the non TYPE_DECLs are in
6797 reverse order, so we can't just use nreverse. */
6798 prev = NULL_TREE;
6799 for (x = TYPE_FIELDS (t);
6800 x && TREE_CODE (x) != TYPE_DECL;
6801 x = next)
6802 {
6803 next = DECL_CHAIN (x);
6804 DECL_CHAIN (x) = prev;
6805 prev = x;
6806 }
6807 if (prev)
6808 {
6809 DECL_CHAIN (TYPE_FIELDS (t)) = x;
6810 if (prev)
6811 TYPE_FIELDS (t) = prev;
6812 }
6813 }
6814
6815 tree
6816 finish_struct (tree t, tree attributes)
6817 {
6818 location_t saved_loc = input_location;
6819
6820 /* Now that we've got all the field declarations, reverse everything
6821 as necessary. */
6822 unreverse_member_declarations (t);
6823
6824 cplus_decl_attributes (&t, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
6825
6826 /* Nadger the current location so that diagnostics point to the start of
6827 the struct, not the end. */
6828 input_location = DECL_SOURCE_LOCATION (TYPE_NAME (t));
6829
6830 if (processing_template_decl)
6831 {
6832 tree x;
6833
6834 finish_struct_methods (t);
6835 TYPE_SIZE (t) = bitsize_zero_node;
6836 TYPE_SIZE_UNIT (t) = size_zero_node;
6837
6838 /* We need to emit an error message if this type was used as a parameter
6839 and it is an abstract type, even if it is a template. We construct
6840 a simple CLASSTYPE_PURE_VIRTUALS list without taking bases into
6841 account and we call complete_vars with this type, which will check
6842 the PARM_DECLS. Note that while the type is being defined,
6843 CLASSTYPE_PURE_VIRTUALS contains the list of the inline friends
6844 (see CLASSTYPE_INLINE_FRIENDS) so we need to clear it. */
6845 CLASSTYPE_PURE_VIRTUALS (t) = NULL;
6846 for (x = TYPE_METHODS (t); x; x = DECL_CHAIN (x))
6847 if (DECL_PURE_VIRTUAL_P (x))
6848 vec_safe_push (CLASSTYPE_PURE_VIRTUALS (t), x);
6849 complete_vars (t);
6850 /* We need to add the target functions to the CLASSTYPE_METHOD_VEC if
6851 an enclosing scope is a template class, so that this function be
6852 found by lookup_fnfields_1 when the using declaration is not
6853 instantiated yet. */
6854 for (x = TYPE_FIELDS (t); x; x = DECL_CHAIN (x))
6855 if (TREE_CODE (x) == USING_DECL)
6856 {
6857 tree fn = strip_using_decl (x);
6858 if (is_overloaded_fn (fn))
6859 for (; fn; fn = OVL_NEXT (fn))
6860 add_method (t, OVL_CURRENT (fn), x);
6861 }
6862
6863 /* Remember current #pragma pack value. */
6864 TYPE_PRECISION (t) = maximum_field_alignment;
6865
6866 /* Fix up any variants we've already built. */
6867 for (x = TYPE_NEXT_VARIANT (t); x; x = TYPE_NEXT_VARIANT (x))
6868 {
6869 TYPE_SIZE (x) = TYPE_SIZE (t);
6870 TYPE_SIZE_UNIT (x) = TYPE_SIZE_UNIT (t);
6871 TYPE_FIELDS (x) = TYPE_FIELDS (t);
6872 TYPE_METHODS (x) = TYPE_METHODS (t);
6873 }
6874 }
6875 else
6876 finish_struct_1 (t);
6877
6878 if (is_std_init_list (t))
6879 {
6880 /* People keep complaining that the compiler crashes on an invalid
6881 definition of initializer_list, so I guess we should explicitly
6882 reject it. What the compiler internals care about is that it's a
6883 template and has a pointer field followed by an integer field. */
6884 bool ok = false;
6885 if (processing_template_decl)
6886 {
6887 tree f = next_initializable_field (TYPE_FIELDS (t));
6888 if (f && TREE_CODE (TREE_TYPE (f)) == POINTER_TYPE)
6889 {
6890 f = next_initializable_field (DECL_CHAIN (f));
6891 if (f && same_type_p (TREE_TYPE (f), size_type_node))
6892 ok = true;
6893 }
6894 }
6895 if (!ok)
6896 fatal_error (input_location,
6897 "definition of std::initializer_list does not match "
6898 "#include <initializer_list>");
6899 }
6900
6901 input_location = saved_loc;
6902
6903 TYPE_BEING_DEFINED (t) = 0;
6904
6905 if (current_class_type)
6906 popclass ();
6907 else
6908 error ("trying to finish struct, but kicked out due to previous parse errors");
6909
6910 if (processing_template_decl && at_function_scope_p ()
6911 /* Lambdas are defined by the LAMBDA_EXPR. */
6912 && !LAMBDA_TYPE_P (t))
6913 add_stmt (build_min (TAG_DEFN, t));
6914
6915 return t;
6916 }
6917 \f
6918 /* Hash table to avoid endless recursion when handling references. */
6919 static hash_table<nofree_ptr_hash<tree_node> > *fixed_type_or_null_ref_ht;
6920
6921 /* Return the dynamic type of INSTANCE, if known.
6922 Used to determine whether the virtual function table is needed
6923 or not.
6924
6925 *NONNULL is set iff INSTANCE can be known to be nonnull, regardless
6926 of our knowledge of its type. *NONNULL should be initialized
6927 before this function is called. */
6928
6929 static tree
6930 fixed_type_or_null (tree instance, int *nonnull, int *cdtorp)
6931 {
6932 #define RECUR(T) fixed_type_or_null((T), nonnull, cdtorp)
6933
6934 switch (TREE_CODE (instance))
6935 {
6936 case INDIRECT_REF:
6937 if (POINTER_TYPE_P (TREE_TYPE (instance)))
6938 return NULL_TREE;
6939 else
6940 return RECUR (TREE_OPERAND (instance, 0));
6941
6942 case CALL_EXPR:
6943 /* This is a call to a constructor, hence it's never zero. */
6944 if (TREE_HAS_CONSTRUCTOR (instance))
6945 {
6946 if (nonnull)
6947 *nonnull = 1;
6948 return TREE_TYPE (instance);
6949 }
6950 return NULL_TREE;
6951
6952 case SAVE_EXPR:
6953 /* This is a call to a constructor, hence it's never zero. */
6954 if (TREE_HAS_CONSTRUCTOR (instance))
6955 {
6956 if (nonnull)
6957 *nonnull = 1;
6958 return TREE_TYPE (instance);
6959 }
6960 return RECUR (TREE_OPERAND (instance, 0));
6961
6962 case POINTER_PLUS_EXPR:
6963 case PLUS_EXPR:
6964 case MINUS_EXPR:
6965 if (TREE_CODE (TREE_OPERAND (instance, 0)) == ADDR_EXPR)
6966 return RECUR (TREE_OPERAND (instance, 0));
6967 if (TREE_CODE (TREE_OPERAND (instance, 1)) == INTEGER_CST)
6968 /* Propagate nonnull. */
6969 return RECUR (TREE_OPERAND (instance, 0));
6970
6971 return NULL_TREE;
6972
6973 CASE_CONVERT:
6974 return RECUR (TREE_OPERAND (instance, 0));
6975
6976 case ADDR_EXPR:
6977 instance = TREE_OPERAND (instance, 0);
6978 if (nonnull)
6979 {
6980 /* Just because we see an ADDR_EXPR doesn't mean we're dealing
6981 with a real object -- given &p->f, p can still be null. */
6982 tree t = get_base_address (instance);
6983 /* ??? Probably should check DECL_WEAK here. */
6984 if (t && DECL_P (t))
6985 *nonnull = 1;
6986 }
6987 return RECUR (instance);
6988
6989 case COMPONENT_REF:
6990 /* If this component is really a base class reference, then the field
6991 itself isn't definitive. */
6992 if (DECL_FIELD_IS_BASE (TREE_OPERAND (instance, 1)))
6993 return RECUR (TREE_OPERAND (instance, 0));
6994 return RECUR (TREE_OPERAND (instance, 1));
6995
6996 case VAR_DECL:
6997 case FIELD_DECL:
6998 if (TREE_CODE (TREE_TYPE (instance)) == ARRAY_TYPE
6999 && MAYBE_CLASS_TYPE_P (TREE_TYPE (TREE_TYPE (instance))))
7000 {
7001 if (nonnull)
7002 *nonnull = 1;
7003 return TREE_TYPE (TREE_TYPE (instance));
7004 }
7005 /* fall through... */
7006 case TARGET_EXPR:
7007 case PARM_DECL:
7008 case RESULT_DECL:
7009 if (MAYBE_CLASS_TYPE_P (TREE_TYPE (instance)))
7010 {
7011 if (nonnull)
7012 *nonnull = 1;
7013 return TREE_TYPE (instance);
7014 }
7015 else if (instance == current_class_ptr)
7016 {
7017 if (nonnull)
7018 *nonnull = 1;
7019
7020 /* if we're in a ctor or dtor, we know our type. If
7021 current_class_ptr is set but we aren't in a function, we're in
7022 an NSDMI (and therefore a constructor). */
7023 if (current_scope () != current_function_decl
7024 || (DECL_LANG_SPECIFIC (current_function_decl)
7025 && (DECL_CONSTRUCTOR_P (current_function_decl)
7026 || DECL_DESTRUCTOR_P (current_function_decl))))
7027 {
7028 if (cdtorp)
7029 *cdtorp = 1;
7030 return TREE_TYPE (TREE_TYPE (instance));
7031 }
7032 }
7033 else if (TREE_CODE (TREE_TYPE (instance)) == REFERENCE_TYPE)
7034 {
7035 /* We only need one hash table because it is always left empty. */
7036 if (!fixed_type_or_null_ref_ht)
7037 fixed_type_or_null_ref_ht
7038 = new hash_table<nofree_ptr_hash<tree_node> > (37);
7039
7040 /* Reference variables should be references to objects. */
7041 if (nonnull)
7042 *nonnull = 1;
7043
7044 /* Enter the INSTANCE in a table to prevent recursion; a
7045 variable's initializer may refer to the variable
7046 itself. */
7047 if (VAR_P (instance)
7048 && DECL_INITIAL (instance)
7049 && !type_dependent_expression_p_push (DECL_INITIAL (instance))
7050 && !fixed_type_or_null_ref_ht->find (instance))
7051 {
7052 tree type;
7053 tree_node **slot;
7054
7055 slot = fixed_type_or_null_ref_ht->find_slot (instance, INSERT);
7056 *slot = instance;
7057 type = RECUR (DECL_INITIAL (instance));
7058 fixed_type_or_null_ref_ht->remove_elt (instance);
7059
7060 return type;
7061 }
7062 }
7063 return NULL_TREE;
7064
7065 default:
7066 return NULL_TREE;
7067 }
7068 #undef RECUR
7069 }
7070
7071 /* Return nonzero if the dynamic type of INSTANCE is known, and
7072 equivalent to the static type. We also handle the case where
7073 INSTANCE is really a pointer. Return negative if this is a
7074 ctor/dtor. There the dynamic type is known, but this might not be
7075 the most derived base of the original object, and hence virtual
7076 bases may not be laid out according to this type.
7077
7078 Used to determine whether the virtual function table is needed
7079 or not.
7080
7081 *NONNULL is set iff INSTANCE can be known to be nonnull, regardless
7082 of our knowledge of its type. *NONNULL should be initialized
7083 before this function is called. */
7084
7085 int
7086 resolves_to_fixed_type_p (tree instance, int* nonnull)
7087 {
7088 tree t = TREE_TYPE (instance);
7089 int cdtorp = 0;
7090 tree fixed;
7091
7092 /* processing_template_decl can be false in a template if we're in
7093 instantiate_non_dependent_expr, but we still want to suppress
7094 this check. */
7095 if (in_template_function ())
7096 {
7097 /* In a template we only care about the type of the result. */
7098 if (nonnull)
7099 *nonnull = true;
7100 return true;
7101 }
7102
7103 fixed = fixed_type_or_null (instance, nonnull, &cdtorp);
7104 if (fixed == NULL_TREE)
7105 return 0;
7106 if (POINTER_TYPE_P (t))
7107 t = TREE_TYPE (t);
7108 if (!same_type_ignoring_top_level_qualifiers_p (t, fixed))
7109 return 0;
7110 return cdtorp ? -1 : 1;
7111 }
7112
7113 \f
7114 void
7115 init_class_processing (void)
7116 {
7117 current_class_depth = 0;
7118 current_class_stack_size = 10;
7119 current_class_stack
7120 = XNEWVEC (struct class_stack_node, current_class_stack_size);
7121 vec_alloc (local_classes, 8);
7122 sizeof_biggest_empty_class = size_zero_node;
7123
7124 ridpointers[(int) RID_PUBLIC] = access_public_node;
7125 ridpointers[(int) RID_PRIVATE] = access_private_node;
7126 ridpointers[(int) RID_PROTECTED] = access_protected_node;
7127 }
7128
7129 /* Restore the cached PREVIOUS_CLASS_LEVEL. */
7130
7131 static void
7132 restore_class_cache (void)
7133 {
7134 tree type;
7135
7136 /* We are re-entering the same class we just left, so we don't
7137 have to search the whole inheritance matrix to find all the
7138 decls to bind again. Instead, we install the cached
7139 class_shadowed list and walk through it binding names. */
7140 push_binding_level (previous_class_level);
7141 class_binding_level = previous_class_level;
7142 /* Restore IDENTIFIER_TYPE_VALUE. */
7143 for (type = class_binding_level->type_shadowed;
7144 type;
7145 type = TREE_CHAIN (type))
7146 SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (type), TREE_TYPE (type));
7147 }
7148
7149 /* Set global variables CURRENT_CLASS_NAME and CURRENT_CLASS_TYPE as
7150 appropriate for TYPE.
7151
7152 So that we may avoid calls to lookup_name, we cache the _TYPE
7153 nodes of local TYPE_DECLs in the TREE_TYPE field of the name.
7154
7155 For multiple inheritance, we perform a two-pass depth-first search
7156 of the type lattice. */
7157
7158 void
7159 pushclass (tree type)
7160 {
7161 class_stack_node_t csn;
7162
7163 type = TYPE_MAIN_VARIANT (type);
7164
7165 /* Make sure there is enough room for the new entry on the stack. */
7166 if (current_class_depth + 1 >= current_class_stack_size)
7167 {
7168 current_class_stack_size *= 2;
7169 current_class_stack
7170 = XRESIZEVEC (struct class_stack_node, current_class_stack,
7171 current_class_stack_size);
7172 }
7173
7174 /* Insert a new entry on the class stack. */
7175 csn = current_class_stack + current_class_depth;
7176 csn->name = current_class_name;
7177 csn->type = current_class_type;
7178 csn->access = current_access_specifier;
7179 csn->names_used = 0;
7180 csn->hidden = 0;
7181 current_class_depth++;
7182
7183 /* Now set up the new type. */
7184 current_class_name = TYPE_NAME (type);
7185 if (TREE_CODE (current_class_name) == TYPE_DECL)
7186 current_class_name = DECL_NAME (current_class_name);
7187 current_class_type = type;
7188
7189 /* By default, things in classes are private, while things in
7190 structures or unions are public. */
7191 current_access_specifier = (CLASSTYPE_DECLARED_CLASS (type)
7192 ? access_private_node
7193 : access_public_node);
7194
7195 if (previous_class_level
7196 && type != previous_class_level->this_entity
7197 && current_class_depth == 1)
7198 {
7199 /* Forcibly remove any old class remnants. */
7200 invalidate_class_lookup_cache ();
7201 }
7202
7203 if (!previous_class_level
7204 || type != previous_class_level->this_entity
7205 || current_class_depth > 1)
7206 pushlevel_class ();
7207 else
7208 restore_class_cache ();
7209 }
7210
7211 /* When we exit a toplevel class scope, we save its binding level so
7212 that we can restore it quickly. Here, we've entered some other
7213 class, so we must invalidate our cache. */
7214
7215 void
7216 invalidate_class_lookup_cache (void)
7217 {
7218 previous_class_level = NULL;
7219 }
7220
7221 /* Get out of the current class scope. If we were in a class scope
7222 previously, that is the one popped to. */
7223
7224 void
7225 popclass (void)
7226 {
7227 poplevel_class ();
7228
7229 current_class_depth--;
7230 current_class_name = current_class_stack[current_class_depth].name;
7231 current_class_type = current_class_stack[current_class_depth].type;
7232 current_access_specifier = current_class_stack[current_class_depth].access;
7233 if (current_class_stack[current_class_depth].names_used)
7234 splay_tree_delete (current_class_stack[current_class_depth].names_used);
7235 }
7236
7237 /* Mark the top of the class stack as hidden. */
7238
7239 void
7240 push_class_stack (void)
7241 {
7242 if (current_class_depth)
7243 ++current_class_stack[current_class_depth - 1].hidden;
7244 }
7245
7246 /* Mark the top of the class stack as un-hidden. */
7247
7248 void
7249 pop_class_stack (void)
7250 {
7251 if (current_class_depth)
7252 --current_class_stack[current_class_depth - 1].hidden;
7253 }
7254
7255 /* Returns 1 if the class type currently being defined is either T or
7256 a nested type of T. */
7257
7258 bool
7259 currently_open_class (tree t)
7260 {
7261 int i;
7262
7263 if (!CLASS_TYPE_P (t))
7264 return false;
7265
7266 t = TYPE_MAIN_VARIANT (t);
7267
7268 /* We start looking from 1 because entry 0 is from global scope,
7269 and has no type. */
7270 for (i = current_class_depth; i > 0; --i)
7271 {
7272 tree c;
7273 if (i == current_class_depth)
7274 c = current_class_type;
7275 else
7276 {
7277 if (current_class_stack[i].hidden)
7278 break;
7279 c = current_class_stack[i].type;
7280 }
7281 if (!c)
7282 continue;
7283 if (same_type_p (c, t))
7284 return true;
7285 }
7286 return false;
7287 }
7288
7289 /* If either current_class_type or one of its enclosing classes are derived
7290 from T, return the appropriate type. Used to determine how we found
7291 something via unqualified lookup. */
7292
7293 tree
7294 currently_open_derived_class (tree t)
7295 {
7296 int i;
7297
7298 /* The bases of a dependent type are unknown. */
7299 if (dependent_type_p (t))
7300 return NULL_TREE;
7301
7302 if (!current_class_type)
7303 return NULL_TREE;
7304
7305 if (DERIVED_FROM_P (t, current_class_type))
7306 return current_class_type;
7307
7308 for (i = current_class_depth - 1; i > 0; --i)
7309 {
7310 if (current_class_stack[i].hidden)
7311 break;
7312 if (DERIVED_FROM_P (t, current_class_stack[i].type))
7313 return current_class_stack[i].type;
7314 }
7315
7316 return NULL_TREE;
7317 }
7318
7319 /* Return the outermost enclosing class type that is still open, or
7320 NULL_TREE. */
7321
7322 tree
7323 outermost_open_class (void)
7324 {
7325 if (!current_class_type)
7326 return NULL_TREE;
7327 tree r = NULL_TREE;
7328 if (TYPE_BEING_DEFINED (current_class_type))
7329 r = current_class_type;
7330 for (int i = current_class_depth - 1; i > 0; --i)
7331 {
7332 if (current_class_stack[i].hidden)
7333 break;
7334 tree t = current_class_stack[i].type;
7335 if (!TYPE_BEING_DEFINED (t))
7336 break;
7337 r = t;
7338 }
7339 return r;
7340 }
7341
7342 /* Returns the innermost class type which is not a lambda closure type. */
7343
7344 tree
7345 current_nonlambda_class_type (void)
7346 {
7347 int i;
7348
7349 /* We start looking from 1 because entry 0 is from global scope,
7350 and has no type. */
7351 for (i = current_class_depth; i > 0; --i)
7352 {
7353 tree c;
7354 if (i == current_class_depth)
7355 c = current_class_type;
7356 else
7357 {
7358 if (current_class_stack[i].hidden)
7359 break;
7360 c = current_class_stack[i].type;
7361 }
7362 if (!c)
7363 continue;
7364 if (!LAMBDA_TYPE_P (c))
7365 return c;
7366 }
7367 return NULL_TREE;
7368 }
7369
7370 /* When entering a class scope, all enclosing class scopes' names with
7371 static meaning (static variables, static functions, types and
7372 enumerators) have to be visible. This recursive function calls
7373 pushclass for all enclosing class contexts until global or a local
7374 scope is reached. TYPE is the enclosed class. */
7375
7376 void
7377 push_nested_class (tree type)
7378 {
7379 /* A namespace might be passed in error cases, like A::B:C. */
7380 if (type == NULL_TREE
7381 || !CLASS_TYPE_P (type))
7382 return;
7383
7384 push_nested_class (DECL_CONTEXT (TYPE_MAIN_DECL (type)));
7385
7386 pushclass (type);
7387 }
7388
7389 /* Undoes a push_nested_class call. */
7390
7391 void
7392 pop_nested_class (void)
7393 {
7394 tree context = DECL_CONTEXT (TYPE_MAIN_DECL (current_class_type));
7395
7396 popclass ();
7397 if (context && CLASS_TYPE_P (context))
7398 pop_nested_class ();
7399 }
7400
7401 /* Returns the number of extern "LANG" blocks we are nested within. */
7402
7403 int
7404 current_lang_depth (void)
7405 {
7406 return vec_safe_length (current_lang_base);
7407 }
7408
7409 /* Set global variables CURRENT_LANG_NAME to appropriate value
7410 so that behavior of name-mangling machinery is correct. */
7411
7412 void
7413 push_lang_context (tree name)
7414 {
7415 vec_safe_push (current_lang_base, current_lang_name);
7416
7417 if (name == lang_name_cplusplus)
7418 {
7419 current_lang_name = name;
7420 }
7421 else if (name == lang_name_java)
7422 {
7423 current_lang_name = name;
7424 /* DECL_IGNORED_P is initially set for these types, to avoid clutter.
7425 (See record_builtin_java_type in decl.c.) However, that causes
7426 incorrect debug entries if these types are actually used.
7427 So we re-enable debug output after extern "Java". */
7428 DECL_IGNORED_P (TYPE_NAME (java_byte_type_node)) = 0;
7429 DECL_IGNORED_P (TYPE_NAME (java_short_type_node)) = 0;
7430 DECL_IGNORED_P (TYPE_NAME (java_int_type_node)) = 0;
7431 DECL_IGNORED_P (TYPE_NAME (java_long_type_node)) = 0;
7432 DECL_IGNORED_P (TYPE_NAME (java_float_type_node)) = 0;
7433 DECL_IGNORED_P (TYPE_NAME (java_double_type_node)) = 0;
7434 DECL_IGNORED_P (TYPE_NAME (java_char_type_node)) = 0;
7435 DECL_IGNORED_P (TYPE_NAME (java_boolean_type_node)) = 0;
7436 }
7437 else if (name == lang_name_c)
7438 {
7439 current_lang_name = name;
7440 }
7441 else
7442 error ("language string %<\"%E\"%> not recognized", name);
7443 }
7444
7445 /* Get out of the current language scope. */
7446
7447 void
7448 pop_lang_context (void)
7449 {
7450 current_lang_name = current_lang_base->pop ();
7451 }
7452 \f
7453 /* Type instantiation routines. */
7454
7455 /* Given an OVERLOAD and a TARGET_TYPE, return the function that
7456 matches the TARGET_TYPE. If there is no satisfactory match, return
7457 error_mark_node, and issue an error & warning messages under
7458 control of FLAGS. Permit pointers to member function if FLAGS
7459 permits. If TEMPLATE_ONLY, the name of the overloaded function was
7460 a template-id, and EXPLICIT_TARGS are the explicitly provided
7461 template arguments.
7462
7463 If OVERLOAD is for one or more member functions, then ACCESS_PATH
7464 is the base path used to reference those member functions. If
7465 the address is resolved to a member function, access checks will be
7466 performed and errors issued if appropriate. */
7467
7468 static tree
7469 resolve_address_of_overloaded_function (tree target_type,
7470 tree overload,
7471 tsubst_flags_t complain,
7472 bool template_only,
7473 tree explicit_targs,
7474 tree access_path)
7475 {
7476 /* Here's what the standard says:
7477
7478 [over.over]
7479
7480 If the name is a function template, template argument deduction
7481 is done, and if the argument deduction succeeds, the deduced
7482 arguments are used to generate a single template function, which
7483 is added to the set of overloaded functions considered.
7484
7485 Non-member functions and static member functions match targets of
7486 type "pointer-to-function" or "reference-to-function." Nonstatic
7487 member functions match targets of type "pointer-to-member
7488 function;" the function type of the pointer to member is used to
7489 select the member function from the set of overloaded member
7490 functions. If a nonstatic member function is selected, the
7491 reference to the overloaded function name is required to have the
7492 form of a pointer to member as described in 5.3.1.
7493
7494 If more than one function is selected, any template functions in
7495 the set are eliminated if the set also contains a non-template
7496 function, and any given template function is eliminated if the
7497 set contains a second template function that is more specialized
7498 than the first according to the partial ordering rules 14.5.5.2.
7499 After such eliminations, if any, there shall remain exactly one
7500 selected function. */
7501
7502 int is_ptrmem = 0;
7503 /* We store the matches in a TREE_LIST rooted here. The functions
7504 are the TREE_PURPOSE, not the TREE_VALUE, in this list, for easy
7505 interoperability with most_specialized_instantiation. */
7506 tree matches = NULL_TREE;
7507 tree fn;
7508 tree target_fn_type;
7509
7510 /* By the time we get here, we should be seeing only real
7511 pointer-to-member types, not the internal POINTER_TYPE to
7512 METHOD_TYPE representation. */
7513 gcc_assert (!TYPE_PTR_P (target_type)
7514 || TREE_CODE (TREE_TYPE (target_type)) != METHOD_TYPE);
7515
7516 gcc_assert (is_overloaded_fn (overload));
7517
7518 /* Check that the TARGET_TYPE is reasonable. */
7519 if (TYPE_PTRFN_P (target_type)
7520 || TYPE_REFFN_P (target_type))
7521 /* This is OK. */;
7522 else if (TYPE_PTRMEMFUNC_P (target_type))
7523 /* This is OK, too. */
7524 is_ptrmem = 1;
7525 else if (TREE_CODE (target_type) == FUNCTION_TYPE)
7526 /* This is OK, too. This comes from a conversion to reference
7527 type. */
7528 target_type = build_reference_type (target_type);
7529 else
7530 {
7531 if (complain & tf_error)
7532 error ("cannot resolve overloaded function %qD based on"
7533 " conversion to type %qT",
7534 DECL_NAME (OVL_FUNCTION (overload)), target_type);
7535 return error_mark_node;
7536 }
7537
7538 /* Non-member functions and static member functions match targets of type
7539 "pointer-to-function" or "reference-to-function." Nonstatic member
7540 functions match targets of type "pointer-to-member-function;" the
7541 function type of the pointer to member is used to select the member
7542 function from the set of overloaded member functions.
7543
7544 So figure out the FUNCTION_TYPE that we want to match against. */
7545 target_fn_type = static_fn_type (target_type);
7546
7547 /* If we can find a non-template function that matches, we can just
7548 use it. There's no point in generating template instantiations
7549 if we're just going to throw them out anyhow. But, of course, we
7550 can only do this when we don't *need* a template function. */
7551 if (!template_only)
7552 {
7553 tree fns;
7554
7555 for (fns = overload; fns; fns = OVL_NEXT (fns))
7556 {
7557 tree fn = OVL_CURRENT (fns);
7558
7559 if (TREE_CODE (fn) == TEMPLATE_DECL)
7560 /* We're not looking for templates just yet. */
7561 continue;
7562
7563 if ((TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
7564 != is_ptrmem)
7565 /* We're looking for a non-static member, and this isn't
7566 one, or vice versa. */
7567 continue;
7568
7569 /* Ignore functions which haven't been explicitly
7570 declared. */
7571 if (DECL_ANTICIPATED (fn))
7572 continue;
7573
7574 /* See if there's a match. */
7575 if (same_type_p (target_fn_type, static_fn_type (fn)))
7576 matches = tree_cons (fn, NULL_TREE, matches);
7577 }
7578 }
7579
7580 /* Now, if we've already got a match (or matches), there's no need
7581 to proceed to the template functions. But, if we don't have a
7582 match we need to look at them, too. */
7583 if (!matches)
7584 {
7585 tree target_arg_types;
7586 tree target_ret_type;
7587 tree fns;
7588 tree *args;
7589 unsigned int nargs, ia;
7590 tree arg;
7591
7592 target_arg_types = TYPE_ARG_TYPES (target_fn_type);
7593 target_ret_type = TREE_TYPE (target_fn_type);
7594
7595 nargs = list_length (target_arg_types);
7596 args = XALLOCAVEC (tree, nargs);
7597 for (arg = target_arg_types, ia = 0;
7598 arg != NULL_TREE && arg != void_list_node;
7599 arg = TREE_CHAIN (arg), ++ia)
7600 args[ia] = TREE_VALUE (arg);
7601 nargs = ia;
7602
7603 for (fns = overload; fns; fns = OVL_NEXT (fns))
7604 {
7605 tree fn = OVL_CURRENT (fns);
7606 tree instantiation;
7607 tree targs;
7608
7609 if (TREE_CODE (fn) != TEMPLATE_DECL)
7610 /* We're only looking for templates. */
7611 continue;
7612
7613 if ((TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
7614 != is_ptrmem)
7615 /* We're not looking for a non-static member, and this is
7616 one, or vice versa. */
7617 continue;
7618
7619 tree ret = target_ret_type;
7620
7621 /* If the template has a deduced return type, don't expose it to
7622 template argument deduction. */
7623 if (undeduced_auto_decl (fn))
7624 ret = NULL_TREE;
7625
7626 /* Try to do argument deduction. */
7627 targs = make_tree_vec (DECL_NTPARMS (fn));
7628 instantiation = fn_type_unification (fn, explicit_targs, targs, args,
7629 nargs, ret,
7630 DEDUCE_EXACT, LOOKUP_NORMAL,
7631 false, false);
7632 if (instantiation == error_mark_node)
7633 /* Instantiation failed. */
7634 continue;
7635
7636 /* And now force instantiation to do return type deduction. */
7637 if (undeduced_auto_decl (instantiation))
7638 {
7639 ++function_depth;
7640 instantiate_decl (instantiation, /*defer*/false, /*class*/false);
7641 --function_depth;
7642
7643 require_deduced_type (instantiation);
7644 }
7645
7646 /* See if there's a match. */
7647 if (same_type_p (target_fn_type, static_fn_type (instantiation)))
7648 matches = tree_cons (instantiation, fn, matches);
7649 }
7650
7651 /* Now, remove all but the most specialized of the matches. */
7652 if (matches)
7653 {
7654 tree match = most_specialized_instantiation (matches);
7655
7656 if (match != error_mark_node)
7657 matches = tree_cons (TREE_PURPOSE (match),
7658 NULL_TREE,
7659 NULL_TREE);
7660 }
7661 }
7662
7663 /* Now we should have exactly one function in MATCHES. */
7664 if (matches == NULL_TREE)
7665 {
7666 /* There were *no* matches. */
7667 if (complain & tf_error)
7668 {
7669 error ("no matches converting function %qD to type %q#T",
7670 DECL_NAME (OVL_CURRENT (overload)),
7671 target_type);
7672
7673 print_candidates (overload);
7674 }
7675 return error_mark_node;
7676 }
7677 else if (TREE_CHAIN (matches))
7678 {
7679 /* There were too many matches. First check if they're all
7680 the same function. */
7681 tree match = NULL_TREE;
7682
7683 fn = TREE_PURPOSE (matches);
7684
7685 /* For multi-versioned functions, more than one match is just fine and
7686 decls_match will return false as they are different. */
7687 for (match = TREE_CHAIN (matches); match; match = TREE_CHAIN (match))
7688 if (!decls_match (fn, TREE_PURPOSE (match))
7689 && !targetm.target_option.function_versions
7690 (fn, TREE_PURPOSE (match)))
7691 break;
7692
7693 if (match)
7694 {
7695 if (complain & tf_error)
7696 {
7697 error ("converting overloaded function %qD to type %q#T is ambiguous",
7698 DECL_NAME (OVL_FUNCTION (overload)),
7699 target_type);
7700
7701 /* Since print_candidates expects the functions in the
7702 TREE_VALUE slot, we flip them here. */
7703 for (match = matches; match; match = TREE_CHAIN (match))
7704 TREE_VALUE (match) = TREE_PURPOSE (match);
7705
7706 print_candidates (matches);
7707 }
7708
7709 return error_mark_node;
7710 }
7711 }
7712
7713 /* Good, exactly one match. Now, convert it to the correct type. */
7714 fn = TREE_PURPOSE (matches);
7715
7716 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
7717 && !(complain & tf_ptrmem_ok) && !flag_ms_extensions)
7718 {
7719 static int explained;
7720
7721 if (!(complain & tf_error))
7722 return error_mark_node;
7723
7724 permerror (input_location, "assuming pointer to member %qD", fn);
7725 if (!explained)
7726 {
7727 inform (input_location, "(a pointer to member can only be formed with %<&%E%>)", fn);
7728 explained = 1;
7729 }
7730 }
7731
7732 /* If a pointer to a function that is multi-versioned is requested, the
7733 pointer to the dispatcher function is returned instead. This works
7734 well because indirectly calling the function will dispatch the right
7735 function version at run-time. */
7736 if (DECL_FUNCTION_VERSIONED (fn))
7737 {
7738 fn = get_function_version_dispatcher (fn);
7739 if (fn == NULL)
7740 return error_mark_node;
7741 /* Mark all the versions corresponding to the dispatcher as used. */
7742 if (!(complain & tf_conv))
7743 mark_versions_used (fn);
7744 }
7745
7746 /* If we're doing overload resolution purely for the purpose of
7747 determining conversion sequences, we should not consider the
7748 function used. If this conversion sequence is selected, the
7749 function will be marked as used at this point. */
7750 if (!(complain & tf_conv))
7751 {
7752 /* Make =delete work with SFINAE. */
7753 if (DECL_DELETED_FN (fn) && !(complain & tf_error))
7754 return error_mark_node;
7755 if (!mark_used (fn, complain) && !(complain & tf_error))
7756 return error_mark_node;
7757 }
7758
7759 /* We could not check access to member functions when this
7760 expression was originally created since we did not know at that
7761 time to which function the expression referred. */
7762 if (DECL_FUNCTION_MEMBER_P (fn))
7763 {
7764 gcc_assert (access_path);
7765 perform_or_defer_access_check (access_path, fn, fn, complain);
7766 }
7767
7768 if (TYPE_PTRFN_P (target_type) || TYPE_PTRMEMFUNC_P (target_type))
7769 return cp_build_addr_expr (fn, complain);
7770 else
7771 {
7772 /* The target must be a REFERENCE_TYPE. Above, cp_build_unary_op
7773 will mark the function as addressed, but here we must do it
7774 explicitly. */
7775 cxx_mark_addressable (fn);
7776
7777 return fn;
7778 }
7779 }
7780
7781 /* This function will instantiate the type of the expression given in
7782 RHS to match the type of LHSTYPE. If errors exist, then return
7783 error_mark_node. COMPLAIN is a bit mask. If TF_ERROR is set, then
7784 we complain on errors. If we are not complaining, never modify rhs,
7785 as overload resolution wants to try many possible instantiations, in
7786 the hope that at least one will work.
7787
7788 For non-recursive calls, LHSTYPE should be a function, pointer to
7789 function, or a pointer to member function. */
7790
7791 tree
7792 instantiate_type (tree lhstype, tree rhs, tsubst_flags_t complain)
7793 {
7794 tsubst_flags_t complain_in = complain;
7795 tree access_path = NULL_TREE;
7796
7797 complain &= ~tf_ptrmem_ok;
7798
7799 if (lhstype == unknown_type_node)
7800 {
7801 if (complain & tf_error)
7802 error ("not enough type information");
7803 return error_mark_node;
7804 }
7805
7806 if (TREE_TYPE (rhs) != NULL_TREE && ! (type_unknown_p (rhs)))
7807 {
7808 tree fntype = non_reference (lhstype);
7809 if (same_type_p (fntype, TREE_TYPE (rhs)))
7810 return rhs;
7811 if (flag_ms_extensions
7812 && TYPE_PTRMEMFUNC_P (fntype)
7813 && !TYPE_PTRMEMFUNC_P (TREE_TYPE (rhs)))
7814 /* Microsoft allows `A::f' to be resolved to a
7815 pointer-to-member. */
7816 ;
7817 else
7818 {
7819 if (complain & tf_error)
7820 error ("cannot convert %qE from type %qT to type %qT",
7821 rhs, TREE_TYPE (rhs), fntype);
7822 return error_mark_node;
7823 }
7824 }
7825
7826 if (BASELINK_P (rhs))
7827 {
7828 access_path = BASELINK_ACCESS_BINFO (rhs);
7829 rhs = BASELINK_FUNCTIONS (rhs);
7830 }
7831
7832 /* If we are in a template, and have a NON_DEPENDENT_EXPR, we cannot
7833 deduce any type information. */
7834 if (TREE_CODE (rhs) == NON_DEPENDENT_EXPR)
7835 {
7836 if (complain & tf_error)
7837 error ("not enough type information");
7838 return error_mark_node;
7839 }
7840
7841 /* There only a few kinds of expressions that may have a type
7842 dependent on overload resolution. */
7843 gcc_assert (TREE_CODE (rhs) == ADDR_EXPR
7844 || TREE_CODE (rhs) == COMPONENT_REF
7845 || is_overloaded_fn (rhs)
7846 || (flag_ms_extensions && TREE_CODE (rhs) == FUNCTION_DECL));
7847
7848 /* This should really only be used when attempting to distinguish
7849 what sort of a pointer to function we have. For now, any
7850 arithmetic operation which is not supported on pointers
7851 is rejected as an error. */
7852
7853 switch (TREE_CODE (rhs))
7854 {
7855 case COMPONENT_REF:
7856 {
7857 tree member = TREE_OPERAND (rhs, 1);
7858
7859 member = instantiate_type (lhstype, member, complain);
7860 if (member != error_mark_node
7861 && TREE_SIDE_EFFECTS (TREE_OPERAND (rhs, 0)))
7862 /* Do not lose object's side effects. */
7863 return build2 (COMPOUND_EXPR, TREE_TYPE (member),
7864 TREE_OPERAND (rhs, 0), member);
7865 return member;
7866 }
7867
7868 case OFFSET_REF:
7869 rhs = TREE_OPERAND (rhs, 1);
7870 if (BASELINK_P (rhs))
7871 return instantiate_type (lhstype, rhs, complain_in);
7872
7873 /* This can happen if we are forming a pointer-to-member for a
7874 member template. */
7875 gcc_assert (TREE_CODE (rhs) == TEMPLATE_ID_EXPR);
7876
7877 /* Fall through. */
7878
7879 case TEMPLATE_ID_EXPR:
7880 {
7881 tree fns = TREE_OPERAND (rhs, 0);
7882 tree args = TREE_OPERAND (rhs, 1);
7883
7884 return
7885 resolve_address_of_overloaded_function (lhstype, fns, complain_in,
7886 /*template_only=*/true,
7887 args, access_path);
7888 }
7889
7890 case OVERLOAD:
7891 case FUNCTION_DECL:
7892 return
7893 resolve_address_of_overloaded_function (lhstype, rhs, complain_in,
7894 /*template_only=*/false,
7895 /*explicit_targs=*/NULL_TREE,
7896 access_path);
7897
7898 case ADDR_EXPR:
7899 {
7900 if (PTRMEM_OK_P (rhs))
7901 complain |= tf_ptrmem_ok;
7902
7903 return instantiate_type (lhstype, TREE_OPERAND (rhs, 0), complain);
7904 }
7905
7906 case ERROR_MARK:
7907 return error_mark_node;
7908
7909 default:
7910 gcc_unreachable ();
7911 }
7912 return error_mark_node;
7913 }
7914 \f
7915 /* Return the name of the virtual function pointer field
7916 (as an IDENTIFIER_NODE) for the given TYPE. Note that
7917 this may have to look back through base types to find the
7918 ultimate field name. (For single inheritance, these could
7919 all be the same name. Who knows for multiple inheritance). */
7920
7921 static tree
7922 get_vfield_name (tree type)
7923 {
7924 tree binfo, base_binfo;
7925 char *buf;
7926
7927 for (binfo = TYPE_BINFO (type);
7928 BINFO_N_BASE_BINFOS (binfo);
7929 binfo = base_binfo)
7930 {
7931 base_binfo = BINFO_BASE_BINFO (binfo, 0);
7932
7933 if (BINFO_VIRTUAL_P (base_binfo)
7934 || !TYPE_CONTAINS_VPTR_P (BINFO_TYPE (base_binfo)))
7935 break;
7936 }
7937
7938 type = BINFO_TYPE (binfo);
7939 buf = (char *) alloca (sizeof (VFIELD_NAME_FORMAT)
7940 + TYPE_NAME_LENGTH (type) + 2);
7941 sprintf (buf, VFIELD_NAME_FORMAT,
7942 IDENTIFIER_POINTER (constructor_name (type)));
7943 return get_identifier (buf);
7944 }
7945
7946 void
7947 print_class_statistics (void)
7948 {
7949 if (! GATHER_STATISTICS)
7950 return;
7951
7952 fprintf (stderr, "convert_harshness = %d\n", n_convert_harshness);
7953 fprintf (stderr, "compute_conversion_costs = %d\n", n_compute_conversion_costs);
7954 if (n_vtables)
7955 {
7956 fprintf (stderr, "vtables = %d; vtable searches = %d\n",
7957 n_vtables, n_vtable_searches);
7958 fprintf (stderr, "vtable entries = %d; vtable elems = %d\n",
7959 n_vtable_entries, n_vtable_elems);
7960 }
7961 }
7962
7963 /* Build a dummy reference to ourselves so Derived::Base (and A::A) works,
7964 according to [class]:
7965 The class-name is also inserted
7966 into the scope of the class itself. For purposes of access checking,
7967 the inserted class name is treated as if it were a public member name. */
7968
7969 void
7970 build_self_reference (void)
7971 {
7972 tree name = constructor_name (current_class_type);
7973 tree value = build_lang_decl (TYPE_DECL, name, current_class_type);
7974 tree saved_cas;
7975
7976 DECL_NONLOCAL (value) = 1;
7977 DECL_CONTEXT (value) = current_class_type;
7978 DECL_ARTIFICIAL (value) = 1;
7979 SET_DECL_SELF_REFERENCE_P (value);
7980 set_underlying_type (value);
7981
7982 if (processing_template_decl)
7983 value = push_template_decl (value);
7984
7985 saved_cas = current_access_specifier;
7986 current_access_specifier = access_public_node;
7987 finish_member_declaration (value);
7988 current_access_specifier = saved_cas;
7989 }
7990
7991 /* Returns 1 if TYPE contains only padding bytes. */
7992
7993 int
7994 is_empty_class (tree type)
7995 {
7996 if (type == error_mark_node)
7997 return 0;
7998
7999 if (! CLASS_TYPE_P (type))
8000 return 0;
8001
8002 return CLASSTYPE_EMPTY_P (type);
8003 }
8004
8005 /* Returns true if TYPE contains no actual data, just various
8006 possible combinations of empty classes and possibly a vptr. */
8007
8008 bool
8009 is_really_empty_class (tree type)
8010 {
8011 if (CLASS_TYPE_P (type))
8012 {
8013 tree field;
8014 tree binfo;
8015 tree base_binfo;
8016 int i;
8017
8018 /* CLASSTYPE_EMPTY_P isn't set properly until the class is actually laid
8019 out, but we'd like to be able to check this before then. */
8020 if (COMPLETE_TYPE_P (type) && is_empty_class (type))
8021 return true;
8022
8023 for (binfo = TYPE_BINFO (type), i = 0;
8024 BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
8025 if (!is_really_empty_class (BINFO_TYPE (base_binfo)))
8026 return false;
8027 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
8028 if (TREE_CODE (field) == FIELD_DECL
8029 && !DECL_ARTIFICIAL (field)
8030 && !is_really_empty_class (TREE_TYPE (field)))
8031 return false;
8032 return true;
8033 }
8034 else if (TREE_CODE (type) == ARRAY_TYPE)
8035 return is_really_empty_class (TREE_TYPE (type));
8036 return false;
8037 }
8038
8039 /* Note that NAME was looked up while the current class was being
8040 defined and that the result of that lookup was DECL. */
8041
8042 void
8043 maybe_note_name_used_in_class (tree name, tree decl)
8044 {
8045 splay_tree names_used;
8046
8047 /* If we're not defining a class, there's nothing to do. */
8048 if (!(innermost_scope_kind() == sk_class
8049 && TYPE_BEING_DEFINED (current_class_type)
8050 && !LAMBDA_TYPE_P (current_class_type)))
8051 return;
8052
8053 /* If there's already a binding for this NAME, then we don't have
8054 anything to worry about. */
8055 if (lookup_member (current_class_type, name,
8056 /*protect=*/0, /*want_type=*/false, tf_warning_or_error))
8057 return;
8058
8059 if (!current_class_stack[current_class_depth - 1].names_used)
8060 current_class_stack[current_class_depth - 1].names_used
8061 = splay_tree_new (splay_tree_compare_pointers, 0, 0);
8062 names_used = current_class_stack[current_class_depth - 1].names_used;
8063
8064 splay_tree_insert (names_used,
8065 (splay_tree_key) name,
8066 (splay_tree_value) decl);
8067 }
8068
8069 /* Note that NAME was declared (as DECL) in the current class. Check
8070 to see that the declaration is valid. */
8071
8072 void
8073 note_name_declared_in_class (tree name, tree decl)
8074 {
8075 splay_tree names_used;
8076 splay_tree_node n;
8077
8078 /* Look to see if we ever used this name. */
8079 names_used
8080 = current_class_stack[current_class_depth - 1].names_used;
8081 if (!names_used)
8082 return;
8083 /* The C language allows members to be declared with a type of the same
8084 name, and the C++ standard says this diagnostic is not required. So
8085 allow it in extern "C" blocks unless predantic is specified.
8086 Allow it in all cases if -ms-extensions is specified. */
8087 if ((!pedantic && current_lang_name == lang_name_c)
8088 || flag_ms_extensions)
8089 return;
8090 n = splay_tree_lookup (names_used, (splay_tree_key) name);
8091 if (n)
8092 {
8093 /* [basic.scope.class]
8094
8095 A name N used in a class S shall refer to the same declaration
8096 in its context and when re-evaluated in the completed scope of
8097 S. */
8098 permerror (input_location, "declaration of %q#D", decl);
8099 permerror (input_location, "changes meaning of %qD from %q+#D",
8100 DECL_NAME (OVL_CURRENT (decl)), (tree) n->value);
8101 }
8102 }
8103
8104 /* Returns the VAR_DECL for the complete vtable associated with BINFO.
8105 Secondary vtables are merged with primary vtables; this function
8106 will return the VAR_DECL for the primary vtable. */
8107
8108 tree
8109 get_vtbl_decl_for_binfo (tree binfo)
8110 {
8111 tree decl;
8112
8113 decl = BINFO_VTABLE (binfo);
8114 if (decl && TREE_CODE (decl) == POINTER_PLUS_EXPR)
8115 {
8116 gcc_assert (TREE_CODE (TREE_OPERAND (decl, 0)) == ADDR_EXPR);
8117 decl = TREE_OPERAND (TREE_OPERAND (decl, 0), 0);
8118 }
8119 if (decl)
8120 gcc_assert (VAR_P (decl));
8121 return decl;
8122 }
8123
8124
8125 /* Returns the binfo for the primary base of BINFO. If the resulting
8126 BINFO is a virtual base, and it is inherited elsewhere in the
8127 hierarchy, then the returned binfo might not be the primary base of
8128 BINFO in the complete object. Check BINFO_PRIMARY_P or
8129 BINFO_LOST_PRIMARY_P to be sure. */
8130
8131 static tree
8132 get_primary_binfo (tree binfo)
8133 {
8134 tree primary_base;
8135
8136 primary_base = CLASSTYPE_PRIMARY_BINFO (BINFO_TYPE (binfo));
8137 if (!primary_base)
8138 return NULL_TREE;
8139
8140 return copied_binfo (primary_base, binfo);
8141 }
8142
8143 /* If INDENTED_P is zero, indent to INDENT. Return nonzero. */
8144
8145 static int
8146 maybe_indent_hierarchy (FILE * stream, int indent, int indented_p)
8147 {
8148 if (!indented_p)
8149 fprintf (stream, "%*s", indent, "");
8150 return 1;
8151 }
8152
8153 /* Dump the offsets of all the bases rooted at BINFO to STREAM.
8154 INDENT should be zero when called from the top level; it is
8155 incremented recursively. IGO indicates the next expected BINFO in
8156 inheritance graph ordering. */
8157
8158 static tree
8159 dump_class_hierarchy_r (FILE *stream,
8160 int flags,
8161 tree binfo,
8162 tree igo,
8163 int indent)
8164 {
8165 int indented = 0;
8166 tree base_binfo;
8167 int i;
8168
8169 indented = maybe_indent_hierarchy (stream, indent, 0);
8170 fprintf (stream, "%s (0x" HOST_WIDE_INT_PRINT_HEX ") ",
8171 type_as_string (BINFO_TYPE (binfo), TFF_PLAIN_IDENTIFIER),
8172 (HOST_WIDE_INT) (uintptr_t) binfo);
8173 if (binfo != igo)
8174 {
8175 fprintf (stream, "alternative-path\n");
8176 return igo;
8177 }
8178 igo = TREE_CHAIN (binfo);
8179
8180 fprintf (stream, HOST_WIDE_INT_PRINT_DEC,
8181 tree_to_shwi (BINFO_OFFSET (binfo)));
8182 if (is_empty_class (BINFO_TYPE (binfo)))
8183 fprintf (stream, " empty");
8184 else if (CLASSTYPE_NEARLY_EMPTY_P (BINFO_TYPE (binfo)))
8185 fprintf (stream, " nearly-empty");
8186 if (BINFO_VIRTUAL_P (binfo))
8187 fprintf (stream, " virtual");
8188 fprintf (stream, "\n");
8189
8190 indented = 0;
8191 if (BINFO_PRIMARY_P (binfo))
8192 {
8193 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
8194 fprintf (stream, " primary-for %s (0x" HOST_WIDE_INT_PRINT_HEX ")",
8195 type_as_string (BINFO_TYPE (BINFO_INHERITANCE_CHAIN (binfo)),
8196 TFF_PLAIN_IDENTIFIER),
8197 (HOST_WIDE_INT) (uintptr_t) BINFO_INHERITANCE_CHAIN (binfo));
8198 }
8199 if (BINFO_LOST_PRIMARY_P (binfo))
8200 {
8201 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
8202 fprintf (stream, " lost-primary");
8203 }
8204 if (indented)
8205 fprintf (stream, "\n");
8206
8207 if (!(flags & TDF_SLIM))
8208 {
8209 int indented = 0;
8210
8211 if (BINFO_SUBVTT_INDEX (binfo))
8212 {
8213 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
8214 fprintf (stream, " subvttidx=%s",
8215 expr_as_string (BINFO_SUBVTT_INDEX (binfo),
8216 TFF_PLAIN_IDENTIFIER));
8217 }
8218 if (BINFO_VPTR_INDEX (binfo))
8219 {
8220 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
8221 fprintf (stream, " vptridx=%s",
8222 expr_as_string (BINFO_VPTR_INDEX (binfo),
8223 TFF_PLAIN_IDENTIFIER));
8224 }
8225 if (BINFO_VPTR_FIELD (binfo))
8226 {
8227 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
8228 fprintf (stream, " vbaseoffset=%s",
8229 expr_as_string (BINFO_VPTR_FIELD (binfo),
8230 TFF_PLAIN_IDENTIFIER));
8231 }
8232 if (BINFO_VTABLE (binfo))
8233 {
8234 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
8235 fprintf (stream, " vptr=%s",
8236 expr_as_string (BINFO_VTABLE (binfo),
8237 TFF_PLAIN_IDENTIFIER));
8238 }
8239
8240 if (indented)
8241 fprintf (stream, "\n");
8242 }
8243
8244 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
8245 igo = dump_class_hierarchy_r (stream, flags, base_binfo, igo, indent + 2);
8246
8247 return igo;
8248 }
8249
8250 /* Dump the BINFO hierarchy for T. */
8251
8252 static void
8253 dump_class_hierarchy_1 (FILE *stream, int flags, tree t)
8254 {
8255 fprintf (stream, "Class %s\n", type_as_string (t, TFF_PLAIN_IDENTIFIER));
8256 fprintf (stream, " size=%lu align=%lu\n",
8257 (unsigned long)(tree_to_shwi (TYPE_SIZE (t)) / BITS_PER_UNIT),
8258 (unsigned long)(TYPE_ALIGN (t) / BITS_PER_UNIT));
8259 fprintf (stream, " base size=%lu base align=%lu\n",
8260 (unsigned long)(tree_to_shwi (TYPE_SIZE (CLASSTYPE_AS_BASE (t)))
8261 / BITS_PER_UNIT),
8262 (unsigned long)(TYPE_ALIGN (CLASSTYPE_AS_BASE (t))
8263 / BITS_PER_UNIT));
8264 dump_class_hierarchy_r (stream, flags, TYPE_BINFO (t), TYPE_BINFO (t), 0);
8265 fprintf (stream, "\n");
8266 }
8267
8268 /* Debug interface to hierarchy dumping. */
8269
8270 void
8271 debug_class (tree t)
8272 {
8273 dump_class_hierarchy_1 (stderr, TDF_SLIM, t);
8274 }
8275
8276 static void
8277 dump_class_hierarchy (tree t)
8278 {
8279 int flags;
8280 FILE *stream = get_dump_info (TDI_class, &flags);
8281
8282 if (stream)
8283 {
8284 dump_class_hierarchy_1 (stream, flags, t);
8285 }
8286 }
8287
8288 static void
8289 dump_array (FILE * stream, tree decl)
8290 {
8291 tree value;
8292 unsigned HOST_WIDE_INT ix;
8293 HOST_WIDE_INT elt;
8294 tree size = TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (decl)));
8295
8296 elt = (tree_to_shwi (TYPE_SIZE (TREE_TYPE (TREE_TYPE (decl))))
8297 / BITS_PER_UNIT);
8298 fprintf (stream, "%s:", decl_as_string (decl, TFF_PLAIN_IDENTIFIER));
8299 fprintf (stream, " %s entries",
8300 expr_as_string (size_binop (PLUS_EXPR, size, size_one_node),
8301 TFF_PLAIN_IDENTIFIER));
8302 fprintf (stream, "\n");
8303
8304 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (DECL_INITIAL (decl)),
8305 ix, value)
8306 fprintf (stream, "%-4ld %s\n", (long)(ix * elt),
8307 expr_as_string (value, TFF_PLAIN_IDENTIFIER));
8308 }
8309
8310 static void
8311 dump_vtable (tree t, tree binfo, tree vtable)
8312 {
8313 int flags;
8314 FILE *stream = get_dump_info (TDI_class, &flags);
8315
8316 if (!stream)
8317 return;
8318
8319 if (!(flags & TDF_SLIM))
8320 {
8321 int ctor_vtbl_p = TYPE_BINFO (t) != binfo;
8322
8323 fprintf (stream, "%s for %s",
8324 ctor_vtbl_p ? "Construction vtable" : "Vtable",
8325 type_as_string (BINFO_TYPE (binfo), TFF_PLAIN_IDENTIFIER));
8326 if (ctor_vtbl_p)
8327 {
8328 if (!BINFO_VIRTUAL_P (binfo))
8329 fprintf (stream, " (0x" HOST_WIDE_INT_PRINT_HEX " instance)",
8330 (HOST_WIDE_INT) (uintptr_t) binfo);
8331 fprintf (stream, " in %s", type_as_string (t, TFF_PLAIN_IDENTIFIER));
8332 }
8333 fprintf (stream, "\n");
8334 dump_array (stream, vtable);
8335 fprintf (stream, "\n");
8336 }
8337 }
8338
8339 static void
8340 dump_vtt (tree t, tree vtt)
8341 {
8342 int flags;
8343 FILE *stream = get_dump_info (TDI_class, &flags);
8344
8345 if (!stream)
8346 return;
8347
8348 if (!(flags & TDF_SLIM))
8349 {
8350 fprintf (stream, "VTT for %s\n",
8351 type_as_string (t, TFF_PLAIN_IDENTIFIER));
8352 dump_array (stream, vtt);
8353 fprintf (stream, "\n");
8354 }
8355 }
8356
8357 /* Dump a function or thunk and its thunkees. */
8358
8359 static void
8360 dump_thunk (FILE *stream, int indent, tree thunk)
8361 {
8362 static const char spaces[] = " ";
8363 tree name = DECL_NAME (thunk);
8364 tree thunks;
8365
8366 fprintf (stream, "%.*s%p %s %s", indent, spaces,
8367 (void *)thunk,
8368 !DECL_THUNK_P (thunk) ? "function"
8369 : DECL_THIS_THUNK_P (thunk) ? "this-thunk" : "covariant-thunk",
8370 name ? IDENTIFIER_POINTER (name) : "<unset>");
8371 if (DECL_THUNK_P (thunk))
8372 {
8373 HOST_WIDE_INT fixed_adjust = THUNK_FIXED_OFFSET (thunk);
8374 tree virtual_adjust = THUNK_VIRTUAL_OFFSET (thunk);
8375
8376 fprintf (stream, " fixed=" HOST_WIDE_INT_PRINT_DEC, fixed_adjust);
8377 if (!virtual_adjust)
8378 /*NOP*/;
8379 else if (DECL_THIS_THUNK_P (thunk))
8380 fprintf (stream, " vcall=" HOST_WIDE_INT_PRINT_DEC,
8381 tree_to_shwi (virtual_adjust));
8382 else
8383 fprintf (stream, " vbase=" HOST_WIDE_INT_PRINT_DEC "(%s)",
8384 tree_to_shwi (BINFO_VPTR_FIELD (virtual_adjust)),
8385 type_as_string (BINFO_TYPE (virtual_adjust), TFF_SCOPE));
8386 if (THUNK_ALIAS (thunk))
8387 fprintf (stream, " alias to %p", (void *)THUNK_ALIAS (thunk));
8388 }
8389 fprintf (stream, "\n");
8390 for (thunks = DECL_THUNKS (thunk); thunks; thunks = TREE_CHAIN (thunks))
8391 dump_thunk (stream, indent + 2, thunks);
8392 }
8393
8394 /* Dump the thunks for FN. */
8395
8396 void
8397 debug_thunks (tree fn)
8398 {
8399 dump_thunk (stderr, 0, fn);
8400 }
8401
8402 /* Virtual function table initialization. */
8403
8404 /* Create all the necessary vtables for T and its base classes. */
8405
8406 static void
8407 finish_vtbls (tree t)
8408 {
8409 tree vbase;
8410 vec<constructor_elt, va_gc> *v = NULL;
8411 tree vtable = BINFO_VTABLE (TYPE_BINFO (t));
8412
8413 /* We lay out the primary and secondary vtables in one contiguous
8414 vtable. The primary vtable is first, followed by the non-virtual
8415 secondary vtables in inheritance graph order. */
8416 accumulate_vtbl_inits (TYPE_BINFO (t), TYPE_BINFO (t), TYPE_BINFO (t),
8417 vtable, t, &v);
8418
8419 /* Then come the virtual bases, also in inheritance graph order. */
8420 for (vbase = TYPE_BINFO (t); vbase; vbase = TREE_CHAIN (vbase))
8421 {
8422 if (!BINFO_VIRTUAL_P (vbase))
8423 continue;
8424 accumulate_vtbl_inits (vbase, vbase, TYPE_BINFO (t), vtable, t, &v);
8425 }
8426
8427 if (BINFO_VTABLE (TYPE_BINFO (t)))
8428 initialize_vtable (TYPE_BINFO (t), v);
8429 }
8430
8431 /* Initialize the vtable for BINFO with the INITS. */
8432
8433 static void
8434 initialize_vtable (tree binfo, vec<constructor_elt, va_gc> *inits)
8435 {
8436 tree decl;
8437
8438 layout_vtable_decl (binfo, vec_safe_length (inits));
8439 decl = get_vtbl_decl_for_binfo (binfo);
8440 initialize_artificial_var (decl, inits);
8441 dump_vtable (BINFO_TYPE (binfo), binfo, decl);
8442 }
8443
8444 /* Build the VTT (virtual table table) for T.
8445 A class requires a VTT if it has virtual bases.
8446
8447 This holds
8448 1 - primary virtual pointer for complete object T
8449 2 - secondary VTTs for each direct non-virtual base of T which requires a
8450 VTT
8451 3 - secondary virtual pointers for each direct or indirect base of T which
8452 has virtual bases or is reachable via a virtual path from T.
8453 4 - secondary VTTs for each direct or indirect virtual base of T.
8454
8455 Secondary VTTs look like complete object VTTs without part 4. */
8456
8457 static void
8458 build_vtt (tree t)
8459 {
8460 tree type;
8461 tree vtt;
8462 tree index;
8463 vec<constructor_elt, va_gc> *inits;
8464
8465 /* Build up the initializers for the VTT. */
8466 inits = NULL;
8467 index = size_zero_node;
8468 build_vtt_inits (TYPE_BINFO (t), t, &inits, &index);
8469
8470 /* If we didn't need a VTT, we're done. */
8471 if (!inits)
8472 return;
8473
8474 /* Figure out the type of the VTT. */
8475 type = build_array_of_n_type (const_ptr_type_node,
8476 inits->length ());
8477
8478 /* Now, build the VTT object itself. */
8479 vtt = build_vtable (t, mangle_vtt_for_type (t), type);
8480 initialize_artificial_var (vtt, inits);
8481 /* Add the VTT to the vtables list. */
8482 DECL_CHAIN (vtt) = DECL_CHAIN (CLASSTYPE_VTABLES (t));
8483 DECL_CHAIN (CLASSTYPE_VTABLES (t)) = vtt;
8484
8485 dump_vtt (t, vtt);
8486 }
8487
8488 /* When building a secondary VTT, BINFO_VTABLE is set to a TREE_LIST with
8489 PURPOSE the RTTI_BINFO, VALUE the real vtable pointer for this binfo,
8490 and CHAIN the vtable pointer for this binfo after construction is
8491 complete. VALUE can also be another BINFO, in which case we recurse. */
8492
8493 static tree
8494 binfo_ctor_vtable (tree binfo)
8495 {
8496 tree vt;
8497
8498 while (1)
8499 {
8500 vt = BINFO_VTABLE (binfo);
8501 if (TREE_CODE (vt) == TREE_LIST)
8502 vt = TREE_VALUE (vt);
8503 if (TREE_CODE (vt) == TREE_BINFO)
8504 binfo = vt;
8505 else
8506 break;
8507 }
8508
8509 return vt;
8510 }
8511
8512 /* Data for secondary VTT initialization. */
8513 typedef struct secondary_vptr_vtt_init_data_s
8514 {
8515 /* Is this the primary VTT? */
8516 bool top_level_p;
8517
8518 /* Current index into the VTT. */
8519 tree index;
8520
8521 /* Vector of initializers built up. */
8522 vec<constructor_elt, va_gc> *inits;
8523
8524 /* The type being constructed by this secondary VTT. */
8525 tree type_being_constructed;
8526 } secondary_vptr_vtt_init_data;
8527
8528 /* Recursively build the VTT-initializer for BINFO (which is in the
8529 hierarchy dominated by T). INITS points to the end of the initializer
8530 list to date. INDEX is the VTT index where the next element will be
8531 replaced. Iff BINFO is the binfo for T, this is the top level VTT (i.e.
8532 not a subvtt for some base of T). When that is so, we emit the sub-VTTs
8533 for virtual bases of T. When it is not so, we build the constructor
8534 vtables for the BINFO-in-T variant. */
8535
8536 static void
8537 build_vtt_inits (tree binfo, tree t, vec<constructor_elt, va_gc> **inits,
8538 tree *index)
8539 {
8540 int i;
8541 tree b;
8542 tree init;
8543 secondary_vptr_vtt_init_data data;
8544 int top_level_p = SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), t);
8545
8546 /* We only need VTTs for subobjects with virtual bases. */
8547 if (!CLASSTYPE_VBASECLASSES (BINFO_TYPE (binfo)))
8548 return;
8549
8550 /* We need to use a construction vtable if this is not the primary
8551 VTT. */
8552 if (!top_level_p)
8553 {
8554 build_ctor_vtbl_group (binfo, t);
8555
8556 /* Record the offset in the VTT where this sub-VTT can be found. */
8557 BINFO_SUBVTT_INDEX (binfo) = *index;
8558 }
8559
8560 /* Add the address of the primary vtable for the complete object. */
8561 init = binfo_ctor_vtable (binfo);
8562 CONSTRUCTOR_APPEND_ELT (*inits, NULL_TREE, init);
8563 if (top_level_p)
8564 {
8565 gcc_assert (!BINFO_VPTR_INDEX (binfo));
8566 BINFO_VPTR_INDEX (binfo) = *index;
8567 }
8568 *index = size_binop (PLUS_EXPR, *index, TYPE_SIZE_UNIT (ptr_type_node));
8569
8570 /* Recursively add the secondary VTTs for non-virtual bases. */
8571 for (i = 0; BINFO_BASE_ITERATE (binfo, i, b); ++i)
8572 if (!BINFO_VIRTUAL_P (b))
8573 build_vtt_inits (b, t, inits, index);
8574
8575 /* Add secondary virtual pointers for all subobjects of BINFO with
8576 either virtual bases or reachable along a virtual path, except
8577 subobjects that are non-virtual primary bases. */
8578 data.top_level_p = top_level_p;
8579 data.index = *index;
8580 data.inits = *inits;
8581 data.type_being_constructed = BINFO_TYPE (binfo);
8582
8583 dfs_walk_once (binfo, dfs_build_secondary_vptr_vtt_inits, NULL, &data);
8584
8585 *index = data.index;
8586
8587 /* data.inits might have grown as we added secondary virtual pointers.
8588 Make sure our caller knows about the new vector. */
8589 *inits = data.inits;
8590
8591 if (top_level_p)
8592 /* Add the secondary VTTs for virtual bases in inheritance graph
8593 order. */
8594 for (b = TYPE_BINFO (BINFO_TYPE (binfo)); b; b = TREE_CHAIN (b))
8595 {
8596 if (!BINFO_VIRTUAL_P (b))
8597 continue;
8598
8599 build_vtt_inits (b, t, inits, index);
8600 }
8601 else
8602 /* Remove the ctor vtables we created. */
8603 dfs_walk_all (binfo, dfs_fixup_binfo_vtbls, NULL, binfo);
8604 }
8605
8606 /* Called from build_vtt_inits via dfs_walk. BINFO is the binfo for the base
8607 in most derived. DATA is a SECONDARY_VPTR_VTT_INIT_DATA structure. */
8608
8609 static tree
8610 dfs_build_secondary_vptr_vtt_inits (tree binfo, void *data_)
8611 {
8612 secondary_vptr_vtt_init_data *data = (secondary_vptr_vtt_init_data *)data_;
8613
8614 /* We don't care about bases that don't have vtables. */
8615 if (!TYPE_VFIELD (BINFO_TYPE (binfo)))
8616 return dfs_skip_bases;
8617
8618 /* We're only interested in proper subobjects of the type being
8619 constructed. */
8620 if (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), data->type_being_constructed))
8621 return NULL_TREE;
8622
8623 /* We're only interested in bases with virtual bases or reachable
8624 via a virtual path from the type being constructed. */
8625 if (!(CLASSTYPE_VBASECLASSES (BINFO_TYPE (binfo))
8626 || binfo_via_virtual (binfo, data->type_being_constructed)))
8627 return dfs_skip_bases;
8628
8629 /* We're not interested in non-virtual primary bases. */
8630 if (!BINFO_VIRTUAL_P (binfo) && BINFO_PRIMARY_P (binfo))
8631 return NULL_TREE;
8632
8633 /* Record the index where this secondary vptr can be found. */
8634 if (data->top_level_p)
8635 {
8636 gcc_assert (!BINFO_VPTR_INDEX (binfo));
8637 BINFO_VPTR_INDEX (binfo) = data->index;
8638
8639 if (BINFO_VIRTUAL_P (binfo))
8640 {
8641 /* It's a primary virtual base, and this is not a
8642 construction vtable. Find the base this is primary of in
8643 the inheritance graph, and use that base's vtable
8644 now. */
8645 while (BINFO_PRIMARY_P (binfo))
8646 binfo = BINFO_INHERITANCE_CHAIN (binfo);
8647 }
8648 }
8649
8650 /* Add the initializer for the secondary vptr itself. */
8651 CONSTRUCTOR_APPEND_ELT (data->inits, NULL_TREE, binfo_ctor_vtable (binfo));
8652
8653 /* Advance the vtt index. */
8654 data->index = size_binop (PLUS_EXPR, data->index,
8655 TYPE_SIZE_UNIT (ptr_type_node));
8656
8657 return NULL_TREE;
8658 }
8659
8660 /* Called from build_vtt_inits via dfs_walk. After building
8661 constructor vtables and generating the sub-vtt from them, we need
8662 to restore the BINFO_VTABLES that were scribbled on. DATA is the
8663 binfo of the base whose sub vtt was generated. */
8664
8665 static tree
8666 dfs_fixup_binfo_vtbls (tree binfo, void* data)
8667 {
8668 tree vtable = BINFO_VTABLE (binfo);
8669
8670 if (!TYPE_CONTAINS_VPTR_P (BINFO_TYPE (binfo)))
8671 /* If this class has no vtable, none of its bases do. */
8672 return dfs_skip_bases;
8673
8674 if (!vtable)
8675 /* This might be a primary base, so have no vtable in this
8676 hierarchy. */
8677 return NULL_TREE;
8678
8679 /* If we scribbled the construction vtable vptr into BINFO, clear it
8680 out now. */
8681 if (TREE_CODE (vtable) == TREE_LIST
8682 && (TREE_PURPOSE (vtable) == (tree) data))
8683 BINFO_VTABLE (binfo) = TREE_CHAIN (vtable);
8684
8685 return NULL_TREE;
8686 }
8687
8688 /* Build the construction vtable group for BINFO which is in the
8689 hierarchy dominated by T. */
8690
8691 static void
8692 build_ctor_vtbl_group (tree binfo, tree t)
8693 {
8694 tree type;
8695 tree vtbl;
8696 tree id;
8697 tree vbase;
8698 vec<constructor_elt, va_gc> *v;
8699
8700 /* See if we've already created this construction vtable group. */
8701 id = mangle_ctor_vtbl_for_type (t, binfo);
8702 if (IDENTIFIER_GLOBAL_VALUE (id))
8703 return;
8704
8705 gcc_assert (!SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), t));
8706 /* Build a version of VTBL (with the wrong type) for use in
8707 constructing the addresses of secondary vtables in the
8708 construction vtable group. */
8709 vtbl = build_vtable (t, id, ptr_type_node);
8710 DECL_CONSTRUCTION_VTABLE_P (vtbl) = 1;
8711 /* Don't export construction vtables from shared libraries. Even on
8712 targets that don't support hidden visibility, this tells
8713 can_refer_decl_in_current_unit_p not to assume that it's safe to
8714 access from a different compilation unit (bz 54314). */
8715 DECL_VISIBILITY (vtbl) = VISIBILITY_HIDDEN;
8716 DECL_VISIBILITY_SPECIFIED (vtbl) = true;
8717
8718 v = NULL;
8719 accumulate_vtbl_inits (binfo, TYPE_BINFO (TREE_TYPE (binfo)),
8720 binfo, vtbl, t, &v);
8721
8722 /* Add the vtables for each of our virtual bases using the vbase in T
8723 binfo. */
8724 for (vbase = TYPE_BINFO (BINFO_TYPE (binfo));
8725 vbase;
8726 vbase = TREE_CHAIN (vbase))
8727 {
8728 tree b;
8729
8730 if (!BINFO_VIRTUAL_P (vbase))
8731 continue;
8732 b = copied_binfo (vbase, binfo);
8733
8734 accumulate_vtbl_inits (b, vbase, binfo, vtbl, t, &v);
8735 }
8736
8737 /* Figure out the type of the construction vtable. */
8738 type = build_array_of_n_type (vtable_entry_type, v->length ());
8739 layout_type (type);
8740 TREE_TYPE (vtbl) = type;
8741 DECL_SIZE (vtbl) = DECL_SIZE_UNIT (vtbl) = NULL_TREE;
8742 layout_decl (vtbl, 0);
8743
8744 /* Initialize the construction vtable. */
8745 CLASSTYPE_VTABLES (t) = chainon (CLASSTYPE_VTABLES (t), vtbl);
8746 initialize_artificial_var (vtbl, v);
8747 dump_vtable (t, binfo, vtbl);
8748 }
8749
8750 /* Add the vtbl initializers for BINFO (and its bases other than
8751 non-virtual primaries) to the list of INITS. BINFO is in the
8752 hierarchy dominated by T. RTTI_BINFO is the binfo within T of
8753 the constructor the vtbl inits should be accumulated for. (If this
8754 is the complete object vtbl then RTTI_BINFO will be TYPE_BINFO (T).)
8755 ORIG_BINFO is the binfo for this object within BINFO_TYPE (RTTI_BINFO).
8756 BINFO is the active base equivalent of ORIG_BINFO in the inheritance
8757 graph of T. Both BINFO and ORIG_BINFO will have the same BINFO_TYPE,
8758 but are not necessarily the same in terms of layout. */
8759
8760 static void
8761 accumulate_vtbl_inits (tree binfo,
8762 tree orig_binfo,
8763 tree rtti_binfo,
8764 tree vtbl,
8765 tree t,
8766 vec<constructor_elt, va_gc> **inits)
8767 {
8768 int i;
8769 tree base_binfo;
8770 int ctor_vtbl_p = !SAME_BINFO_TYPE_P (BINFO_TYPE (rtti_binfo), t);
8771
8772 gcc_assert (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), BINFO_TYPE (orig_binfo)));
8773
8774 /* If it doesn't have a vptr, we don't do anything. */
8775 if (!TYPE_CONTAINS_VPTR_P (BINFO_TYPE (binfo)))
8776 return;
8777
8778 /* If we're building a construction vtable, we're not interested in
8779 subobjects that don't require construction vtables. */
8780 if (ctor_vtbl_p
8781 && !CLASSTYPE_VBASECLASSES (BINFO_TYPE (binfo))
8782 && !binfo_via_virtual (orig_binfo, BINFO_TYPE (rtti_binfo)))
8783 return;
8784
8785 /* Build the initializers for the BINFO-in-T vtable. */
8786 dfs_accumulate_vtbl_inits (binfo, orig_binfo, rtti_binfo, vtbl, t, inits);
8787
8788 /* Walk the BINFO and its bases. We walk in preorder so that as we
8789 initialize each vtable we can figure out at what offset the
8790 secondary vtable lies from the primary vtable. We can't use
8791 dfs_walk here because we need to iterate through bases of BINFO
8792 and RTTI_BINFO simultaneously. */
8793 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
8794 {
8795 /* Skip virtual bases. */
8796 if (BINFO_VIRTUAL_P (base_binfo))
8797 continue;
8798 accumulate_vtbl_inits (base_binfo,
8799 BINFO_BASE_BINFO (orig_binfo, i),
8800 rtti_binfo, vtbl, t,
8801 inits);
8802 }
8803 }
8804
8805 /* Called from accumulate_vtbl_inits. Adds the initializers for the
8806 BINFO vtable to L. */
8807
8808 static void
8809 dfs_accumulate_vtbl_inits (tree binfo,
8810 tree orig_binfo,
8811 tree rtti_binfo,
8812 tree orig_vtbl,
8813 tree t,
8814 vec<constructor_elt, va_gc> **l)
8815 {
8816 tree vtbl = NULL_TREE;
8817 int ctor_vtbl_p = !SAME_BINFO_TYPE_P (BINFO_TYPE (rtti_binfo), t);
8818 int n_inits;
8819
8820 if (ctor_vtbl_p
8821 && BINFO_VIRTUAL_P (orig_binfo) && BINFO_PRIMARY_P (orig_binfo))
8822 {
8823 /* In the hierarchy of BINFO_TYPE (RTTI_BINFO), this is a
8824 primary virtual base. If it is not the same primary in
8825 the hierarchy of T, we'll need to generate a ctor vtable
8826 for it, to place at its location in T. If it is the same
8827 primary, we still need a VTT entry for the vtable, but it
8828 should point to the ctor vtable for the base it is a
8829 primary for within the sub-hierarchy of RTTI_BINFO.
8830
8831 There are three possible cases:
8832
8833 1) We are in the same place.
8834 2) We are a primary base within a lost primary virtual base of
8835 RTTI_BINFO.
8836 3) We are primary to something not a base of RTTI_BINFO. */
8837
8838 tree b;
8839 tree last = NULL_TREE;
8840
8841 /* First, look through the bases we are primary to for RTTI_BINFO
8842 or a virtual base. */
8843 b = binfo;
8844 while (BINFO_PRIMARY_P (b))
8845 {
8846 b = BINFO_INHERITANCE_CHAIN (b);
8847 last = b;
8848 if (BINFO_VIRTUAL_P (b) || b == rtti_binfo)
8849 goto found;
8850 }
8851 /* If we run out of primary links, keep looking down our
8852 inheritance chain; we might be an indirect primary. */
8853 for (b = last; b; b = BINFO_INHERITANCE_CHAIN (b))
8854 if (BINFO_VIRTUAL_P (b) || b == rtti_binfo)
8855 break;
8856 found:
8857
8858 /* If we found RTTI_BINFO, this is case 1. If we found a virtual
8859 base B and it is a base of RTTI_BINFO, this is case 2. In
8860 either case, we share our vtable with LAST, i.e. the
8861 derived-most base within B of which we are a primary. */
8862 if (b == rtti_binfo
8863 || (b && binfo_for_vbase (BINFO_TYPE (b), BINFO_TYPE (rtti_binfo))))
8864 /* Just set our BINFO_VTABLE to point to LAST, as we may not have
8865 set LAST's BINFO_VTABLE yet. We'll extract the actual vptr in
8866 binfo_ctor_vtable after everything's been set up. */
8867 vtbl = last;
8868
8869 /* Otherwise, this is case 3 and we get our own. */
8870 }
8871 else if (!BINFO_NEW_VTABLE_MARKED (orig_binfo))
8872 return;
8873
8874 n_inits = vec_safe_length (*l);
8875
8876 if (!vtbl)
8877 {
8878 tree index;
8879 int non_fn_entries;
8880
8881 /* Add the initializer for this vtable. */
8882 build_vtbl_initializer (binfo, orig_binfo, t, rtti_binfo,
8883 &non_fn_entries, l);
8884
8885 /* Figure out the position to which the VPTR should point. */
8886 vtbl = build1 (ADDR_EXPR, vtbl_ptr_type_node, orig_vtbl);
8887 index = size_binop (MULT_EXPR,
8888 TYPE_SIZE_UNIT (vtable_entry_type),
8889 size_int (non_fn_entries + n_inits));
8890 vtbl = fold_build_pointer_plus (vtbl, index);
8891 }
8892
8893 if (ctor_vtbl_p)
8894 /* For a construction vtable, we can't overwrite BINFO_VTABLE.
8895 So, we make a TREE_LIST. Later, dfs_fixup_binfo_vtbls will
8896 straighten this out. */
8897 BINFO_VTABLE (binfo) = tree_cons (rtti_binfo, vtbl, BINFO_VTABLE (binfo));
8898 else if (BINFO_PRIMARY_P (binfo) && BINFO_VIRTUAL_P (binfo))
8899 /* Throw away any unneeded intializers. */
8900 (*l)->truncate (n_inits);
8901 else
8902 /* For an ordinary vtable, set BINFO_VTABLE. */
8903 BINFO_VTABLE (binfo) = vtbl;
8904 }
8905
8906 static GTY(()) tree abort_fndecl_addr;
8907
8908 /* Construct the initializer for BINFO's virtual function table. BINFO
8909 is part of the hierarchy dominated by T. If we're building a
8910 construction vtable, the ORIG_BINFO is the binfo we should use to
8911 find the actual function pointers to put in the vtable - but they
8912 can be overridden on the path to most-derived in the graph that
8913 ORIG_BINFO belongs. Otherwise,
8914 ORIG_BINFO should be the same as BINFO. The RTTI_BINFO is the
8915 BINFO that should be indicated by the RTTI information in the
8916 vtable; it will be a base class of T, rather than T itself, if we
8917 are building a construction vtable.
8918
8919 The value returned is a TREE_LIST suitable for wrapping in a
8920 CONSTRUCTOR to use as the DECL_INITIAL for a vtable. If
8921 NON_FN_ENTRIES_P is not NULL, *NON_FN_ENTRIES_P is set to the
8922 number of non-function entries in the vtable.
8923
8924 It might seem that this function should never be called with a
8925 BINFO for which BINFO_PRIMARY_P holds, the vtable for such a
8926 base is always subsumed by a derived class vtable. However, when
8927 we are building construction vtables, we do build vtables for
8928 primary bases; we need these while the primary base is being
8929 constructed. */
8930
8931 static void
8932 build_vtbl_initializer (tree binfo,
8933 tree orig_binfo,
8934 tree t,
8935 tree rtti_binfo,
8936 int* non_fn_entries_p,
8937 vec<constructor_elt, va_gc> **inits)
8938 {
8939 tree v;
8940 vtbl_init_data vid;
8941 unsigned ix, jx;
8942 tree vbinfo;
8943 vec<tree, va_gc> *vbases;
8944 constructor_elt *e;
8945
8946 /* Initialize VID. */
8947 memset (&vid, 0, sizeof (vid));
8948 vid.binfo = binfo;
8949 vid.derived = t;
8950 vid.rtti_binfo = rtti_binfo;
8951 vid.primary_vtbl_p = SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), t);
8952 vid.ctor_vtbl_p = !SAME_BINFO_TYPE_P (BINFO_TYPE (rtti_binfo), t);
8953 vid.generate_vcall_entries = true;
8954 /* The first vbase or vcall offset is at index -3 in the vtable. */
8955 vid.index = ssize_int(-3 * TARGET_VTABLE_DATA_ENTRY_DISTANCE);
8956
8957 /* Add entries to the vtable for RTTI. */
8958 build_rtti_vtbl_entries (binfo, &vid);
8959
8960 /* Create an array for keeping track of the functions we've
8961 processed. When we see multiple functions with the same
8962 signature, we share the vcall offsets. */
8963 vec_alloc (vid.fns, 32);
8964 /* Add the vcall and vbase offset entries. */
8965 build_vcall_and_vbase_vtbl_entries (binfo, &vid);
8966
8967 /* Clear BINFO_VTABLE_PATH_MARKED; it's set by
8968 build_vbase_offset_vtbl_entries. */
8969 for (vbases = CLASSTYPE_VBASECLASSES (t), ix = 0;
8970 vec_safe_iterate (vbases, ix, &vbinfo); ix++)
8971 BINFO_VTABLE_PATH_MARKED (vbinfo) = 0;
8972
8973 /* If the target requires padding between data entries, add that now. */
8974 if (TARGET_VTABLE_DATA_ENTRY_DISTANCE > 1)
8975 {
8976 int n_entries = vec_safe_length (vid.inits);
8977
8978 vec_safe_grow (vid.inits, TARGET_VTABLE_DATA_ENTRY_DISTANCE * n_entries);
8979
8980 /* Move data entries into their new positions and add padding
8981 after the new positions. Iterate backwards so we don't
8982 overwrite entries that we would need to process later. */
8983 for (ix = n_entries - 1;
8984 vid.inits->iterate (ix, &e);
8985 ix--)
8986 {
8987 int j;
8988 int new_position = (TARGET_VTABLE_DATA_ENTRY_DISTANCE * ix
8989 + (TARGET_VTABLE_DATA_ENTRY_DISTANCE - 1));
8990
8991 (*vid.inits)[new_position] = *e;
8992
8993 for (j = 1; j < TARGET_VTABLE_DATA_ENTRY_DISTANCE; ++j)
8994 {
8995 constructor_elt *f = &(*vid.inits)[new_position - j];
8996 f->index = NULL_TREE;
8997 f->value = build1 (NOP_EXPR, vtable_entry_type,
8998 null_pointer_node);
8999 }
9000 }
9001 }
9002
9003 if (non_fn_entries_p)
9004 *non_fn_entries_p = vec_safe_length (vid.inits);
9005
9006 /* The initializers for virtual functions were built up in reverse
9007 order. Straighten them out and add them to the running list in one
9008 step. */
9009 jx = vec_safe_length (*inits);
9010 vec_safe_grow (*inits, jx + vid.inits->length ());
9011
9012 for (ix = vid.inits->length () - 1;
9013 vid.inits->iterate (ix, &e);
9014 ix--, jx++)
9015 (**inits)[jx] = *e;
9016
9017 /* Go through all the ordinary virtual functions, building up
9018 initializers. */
9019 for (v = BINFO_VIRTUALS (orig_binfo); v; v = TREE_CHAIN (v))
9020 {
9021 tree delta;
9022 tree vcall_index;
9023 tree fn, fn_original;
9024 tree init = NULL_TREE;
9025
9026 fn = BV_FN (v);
9027 fn_original = fn;
9028 if (DECL_THUNK_P (fn))
9029 {
9030 if (!DECL_NAME (fn))
9031 finish_thunk (fn);
9032 if (THUNK_ALIAS (fn))
9033 {
9034 fn = THUNK_ALIAS (fn);
9035 BV_FN (v) = fn;
9036 }
9037 fn_original = THUNK_TARGET (fn);
9038 }
9039
9040 /* If the only definition of this function signature along our
9041 primary base chain is from a lost primary, this vtable slot will
9042 never be used, so just zero it out. This is important to avoid
9043 requiring extra thunks which cannot be generated with the function.
9044
9045 We first check this in update_vtable_entry_for_fn, so we handle
9046 restored primary bases properly; we also need to do it here so we
9047 zero out unused slots in ctor vtables, rather than filling them
9048 with erroneous values (though harmless, apart from relocation
9049 costs). */
9050 if (BV_LOST_PRIMARY (v))
9051 init = size_zero_node;
9052
9053 if (! init)
9054 {
9055 /* Pull the offset for `this', and the function to call, out of
9056 the list. */
9057 delta = BV_DELTA (v);
9058 vcall_index = BV_VCALL_INDEX (v);
9059
9060 gcc_assert (TREE_CODE (delta) == INTEGER_CST);
9061 gcc_assert (TREE_CODE (fn) == FUNCTION_DECL);
9062
9063 /* You can't call an abstract virtual function; it's abstract.
9064 So, we replace these functions with __pure_virtual. */
9065 if (DECL_PURE_VIRTUAL_P (fn_original))
9066 {
9067 fn = abort_fndecl;
9068 if (!TARGET_VTABLE_USES_DESCRIPTORS)
9069 {
9070 if (abort_fndecl_addr == NULL)
9071 abort_fndecl_addr
9072 = fold_convert (vfunc_ptr_type_node,
9073 build_fold_addr_expr (fn));
9074 init = abort_fndecl_addr;
9075 }
9076 }
9077 /* Likewise for deleted virtuals. */
9078 else if (DECL_DELETED_FN (fn_original))
9079 {
9080 fn = get_identifier ("__cxa_deleted_virtual");
9081 if (!get_global_value_if_present (fn, &fn))
9082 fn = push_library_fn (fn, (build_function_type_list
9083 (void_type_node, NULL_TREE)),
9084 NULL_TREE, ECF_NORETURN);
9085 if (!TARGET_VTABLE_USES_DESCRIPTORS)
9086 init = fold_convert (vfunc_ptr_type_node,
9087 build_fold_addr_expr (fn));
9088 }
9089 else
9090 {
9091 if (!integer_zerop (delta) || vcall_index)
9092 {
9093 fn = make_thunk (fn, /*this_adjusting=*/1, delta, vcall_index);
9094 if (!DECL_NAME (fn))
9095 finish_thunk (fn);
9096 }
9097 /* Take the address of the function, considering it to be of an
9098 appropriate generic type. */
9099 if (!TARGET_VTABLE_USES_DESCRIPTORS)
9100 init = fold_convert (vfunc_ptr_type_node,
9101 build_fold_addr_expr (fn));
9102 /* Don't refer to a virtual destructor from a constructor
9103 vtable or a vtable for an abstract class, since destroying
9104 an object under construction is undefined behavior and we
9105 don't want it to be considered a candidate for speculative
9106 devirtualization. But do create the thunk for ABI
9107 compliance. */
9108 if (DECL_DESTRUCTOR_P (fn_original)
9109 && (CLASSTYPE_PURE_VIRTUALS (DECL_CONTEXT (fn_original))
9110 || orig_binfo != binfo))
9111 init = size_zero_node;
9112 }
9113 }
9114
9115 /* And add it to the chain of initializers. */
9116 if (TARGET_VTABLE_USES_DESCRIPTORS)
9117 {
9118 int i;
9119 if (init == size_zero_node)
9120 for (i = 0; i < TARGET_VTABLE_USES_DESCRIPTORS; ++i)
9121 CONSTRUCTOR_APPEND_ELT (*inits, NULL_TREE, init);
9122 else
9123 for (i = 0; i < TARGET_VTABLE_USES_DESCRIPTORS; ++i)
9124 {
9125 tree fdesc = build2 (FDESC_EXPR, vfunc_ptr_type_node,
9126 fn, build_int_cst (NULL_TREE, i));
9127 TREE_CONSTANT (fdesc) = 1;
9128
9129 CONSTRUCTOR_APPEND_ELT (*inits, NULL_TREE, fdesc);
9130 }
9131 }
9132 else
9133 CONSTRUCTOR_APPEND_ELT (*inits, NULL_TREE, init);
9134 }
9135 }
9136
9137 /* Adds to vid->inits the initializers for the vbase and vcall
9138 offsets in BINFO, which is in the hierarchy dominated by T. */
9139
9140 static void
9141 build_vcall_and_vbase_vtbl_entries (tree binfo, vtbl_init_data* vid)
9142 {
9143 tree b;
9144
9145 /* If this is a derived class, we must first create entries
9146 corresponding to the primary base class. */
9147 b = get_primary_binfo (binfo);
9148 if (b)
9149 build_vcall_and_vbase_vtbl_entries (b, vid);
9150
9151 /* Add the vbase entries for this base. */
9152 build_vbase_offset_vtbl_entries (binfo, vid);
9153 /* Add the vcall entries for this base. */
9154 build_vcall_offset_vtbl_entries (binfo, vid);
9155 }
9156
9157 /* Returns the initializers for the vbase offset entries in the vtable
9158 for BINFO (which is part of the class hierarchy dominated by T), in
9159 reverse order. VBASE_OFFSET_INDEX gives the vtable index
9160 where the next vbase offset will go. */
9161
9162 static void
9163 build_vbase_offset_vtbl_entries (tree binfo, vtbl_init_data* vid)
9164 {
9165 tree vbase;
9166 tree t;
9167 tree non_primary_binfo;
9168
9169 /* If there are no virtual baseclasses, then there is nothing to
9170 do. */
9171 if (!CLASSTYPE_VBASECLASSES (BINFO_TYPE (binfo)))
9172 return;
9173
9174 t = vid->derived;
9175
9176 /* We might be a primary base class. Go up the inheritance hierarchy
9177 until we find the most derived class of which we are a primary base:
9178 it is the offset of that which we need to use. */
9179 non_primary_binfo = binfo;
9180 while (BINFO_INHERITANCE_CHAIN (non_primary_binfo))
9181 {
9182 tree b;
9183
9184 /* If we have reached a virtual base, then it must be a primary
9185 base (possibly multi-level) of vid->binfo, or we wouldn't
9186 have called build_vcall_and_vbase_vtbl_entries for it. But it
9187 might be a lost primary, so just skip down to vid->binfo. */
9188 if (BINFO_VIRTUAL_P (non_primary_binfo))
9189 {
9190 non_primary_binfo = vid->binfo;
9191 break;
9192 }
9193
9194 b = BINFO_INHERITANCE_CHAIN (non_primary_binfo);
9195 if (get_primary_binfo (b) != non_primary_binfo)
9196 break;
9197 non_primary_binfo = b;
9198 }
9199
9200 /* Go through the virtual bases, adding the offsets. */
9201 for (vbase = TYPE_BINFO (BINFO_TYPE (binfo));
9202 vbase;
9203 vbase = TREE_CHAIN (vbase))
9204 {
9205 tree b;
9206 tree delta;
9207
9208 if (!BINFO_VIRTUAL_P (vbase))
9209 continue;
9210
9211 /* Find the instance of this virtual base in the complete
9212 object. */
9213 b = copied_binfo (vbase, binfo);
9214
9215 /* If we've already got an offset for this virtual base, we
9216 don't need another one. */
9217 if (BINFO_VTABLE_PATH_MARKED (b))
9218 continue;
9219 BINFO_VTABLE_PATH_MARKED (b) = 1;
9220
9221 /* Figure out where we can find this vbase offset. */
9222 delta = size_binop (MULT_EXPR,
9223 vid->index,
9224 convert (ssizetype,
9225 TYPE_SIZE_UNIT (vtable_entry_type)));
9226 if (vid->primary_vtbl_p)
9227 BINFO_VPTR_FIELD (b) = delta;
9228
9229 if (binfo != TYPE_BINFO (t))
9230 /* The vbase offset had better be the same. */
9231 gcc_assert (tree_int_cst_equal (delta, BINFO_VPTR_FIELD (vbase)));
9232
9233 /* The next vbase will come at a more negative offset. */
9234 vid->index = size_binop (MINUS_EXPR, vid->index,
9235 ssize_int (TARGET_VTABLE_DATA_ENTRY_DISTANCE));
9236
9237 /* The initializer is the delta from BINFO to this virtual base.
9238 The vbase offsets go in reverse inheritance-graph order, and
9239 we are walking in inheritance graph order so these end up in
9240 the right order. */
9241 delta = size_diffop_loc (input_location,
9242 BINFO_OFFSET (b), BINFO_OFFSET (non_primary_binfo));
9243
9244 CONSTRUCTOR_APPEND_ELT (vid->inits, NULL_TREE,
9245 fold_build1_loc (input_location, NOP_EXPR,
9246 vtable_entry_type, delta));
9247 }
9248 }
9249
9250 /* Adds the initializers for the vcall offset entries in the vtable
9251 for BINFO (which is part of the class hierarchy dominated by VID->DERIVED)
9252 to VID->INITS. */
9253
9254 static void
9255 build_vcall_offset_vtbl_entries (tree binfo, vtbl_init_data* vid)
9256 {
9257 /* We only need these entries if this base is a virtual base. We
9258 compute the indices -- but do not add to the vtable -- when
9259 building the main vtable for a class. */
9260 if (binfo == TYPE_BINFO (vid->derived)
9261 || (BINFO_VIRTUAL_P (binfo)
9262 /* If BINFO is RTTI_BINFO, then (since BINFO does not
9263 correspond to VID->DERIVED), we are building a primary
9264 construction virtual table. Since this is a primary
9265 virtual table, we do not need the vcall offsets for
9266 BINFO. */
9267 && binfo != vid->rtti_binfo))
9268 {
9269 /* We need a vcall offset for each of the virtual functions in this
9270 vtable. For example:
9271
9272 class A { virtual void f (); };
9273 class B1 : virtual public A { virtual void f (); };
9274 class B2 : virtual public A { virtual void f (); };
9275 class C: public B1, public B2 { virtual void f (); };
9276
9277 A C object has a primary base of B1, which has a primary base of A. A
9278 C also has a secondary base of B2, which no longer has a primary base
9279 of A. So the B2-in-C construction vtable needs a secondary vtable for
9280 A, which will adjust the A* to a B2* to call f. We have no way of
9281 knowing what (or even whether) this offset will be when we define B2,
9282 so we store this "vcall offset" in the A sub-vtable and look it up in
9283 a "virtual thunk" for B2::f.
9284
9285 We need entries for all the functions in our primary vtable and
9286 in our non-virtual bases' secondary vtables. */
9287 vid->vbase = binfo;
9288 /* If we are just computing the vcall indices -- but do not need
9289 the actual entries -- not that. */
9290 if (!BINFO_VIRTUAL_P (binfo))
9291 vid->generate_vcall_entries = false;
9292 /* Now, walk through the non-virtual bases, adding vcall offsets. */
9293 add_vcall_offset_vtbl_entries_r (binfo, vid);
9294 }
9295 }
9296
9297 /* Build vcall offsets, starting with those for BINFO. */
9298
9299 static void
9300 add_vcall_offset_vtbl_entries_r (tree binfo, vtbl_init_data* vid)
9301 {
9302 int i;
9303 tree primary_binfo;
9304 tree base_binfo;
9305
9306 /* Don't walk into virtual bases -- except, of course, for the
9307 virtual base for which we are building vcall offsets. Any
9308 primary virtual base will have already had its offsets generated
9309 through the recursion in build_vcall_and_vbase_vtbl_entries. */
9310 if (BINFO_VIRTUAL_P (binfo) && vid->vbase != binfo)
9311 return;
9312
9313 /* If BINFO has a primary base, process it first. */
9314 primary_binfo = get_primary_binfo (binfo);
9315 if (primary_binfo)
9316 add_vcall_offset_vtbl_entries_r (primary_binfo, vid);
9317
9318 /* Add BINFO itself to the list. */
9319 add_vcall_offset_vtbl_entries_1 (binfo, vid);
9320
9321 /* Scan the non-primary bases of BINFO. */
9322 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
9323 if (base_binfo != primary_binfo)
9324 add_vcall_offset_vtbl_entries_r (base_binfo, vid);
9325 }
9326
9327 /* Called from build_vcall_offset_vtbl_entries_r. */
9328
9329 static void
9330 add_vcall_offset_vtbl_entries_1 (tree binfo, vtbl_init_data* vid)
9331 {
9332 /* Make entries for the rest of the virtuals. */
9333 tree orig_fn;
9334
9335 /* The ABI requires that the methods be processed in declaration
9336 order. */
9337 for (orig_fn = TYPE_METHODS (BINFO_TYPE (binfo));
9338 orig_fn;
9339 orig_fn = DECL_CHAIN (orig_fn))
9340 if (TREE_CODE (orig_fn) == FUNCTION_DECL && DECL_VINDEX (orig_fn))
9341 add_vcall_offset (orig_fn, binfo, vid);
9342 }
9343
9344 /* Add a vcall offset entry for ORIG_FN to the vtable. */
9345
9346 static void
9347 add_vcall_offset (tree orig_fn, tree binfo, vtbl_init_data *vid)
9348 {
9349 size_t i;
9350 tree vcall_offset;
9351 tree derived_entry;
9352
9353 /* If there is already an entry for a function with the same
9354 signature as FN, then we do not need a second vcall offset.
9355 Check the list of functions already present in the derived
9356 class vtable. */
9357 FOR_EACH_VEC_SAFE_ELT (vid->fns, i, derived_entry)
9358 {
9359 if (same_signature_p (derived_entry, orig_fn)
9360 /* We only use one vcall offset for virtual destructors,
9361 even though there are two virtual table entries. */
9362 || (DECL_DESTRUCTOR_P (derived_entry)
9363 && DECL_DESTRUCTOR_P (orig_fn)))
9364 return;
9365 }
9366
9367 /* If we are building these vcall offsets as part of building
9368 the vtable for the most derived class, remember the vcall
9369 offset. */
9370 if (vid->binfo == TYPE_BINFO (vid->derived))
9371 {
9372 tree_pair_s elt = {orig_fn, vid->index};
9373 vec_safe_push (CLASSTYPE_VCALL_INDICES (vid->derived), elt);
9374 }
9375
9376 /* The next vcall offset will be found at a more negative
9377 offset. */
9378 vid->index = size_binop (MINUS_EXPR, vid->index,
9379 ssize_int (TARGET_VTABLE_DATA_ENTRY_DISTANCE));
9380
9381 /* Keep track of this function. */
9382 vec_safe_push (vid->fns, orig_fn);
9383
9384 if (vid->generate_vcall_entries)
9385 {
9386 tree base;
9387 tree fn;
9388
9389 /* Find the overriding function. */
9390 fn = find_final_overrider (vid->rtti_binfo, binfo, orig_fn);
9391 if (fn == error_mark_node)
9392 vcall_offset = build_zero_cst (vtable_entry_type);
9393 else
9394 {
9395 base = TREE_VALUE (fn);
9396
9397 /* The vbase we're working on is a primary base of
9398 vid->binfo. But it might be a lost primary, so its
9399 BINFO_OFFSET might be wrong, so we just use the
9400 BINFO_OFFSET from vid->binfo. */
9401 vcall_offset = size_diffop_loc (input_location,
9402 BINFO_OFFSET (base),
9403 BINFO_OFFSET (vid->binfo));
9404 vcall_offset = fold_build1_loc (input_location,
9405 NOP_EXPR, vtable_entry_type,
9406 vcall_offset);
9407 }
9408 /* Add the initializer to the vtable. */
9409 CONSTRUCTOR_APPEND_ELT (vid->inits, NULL_TREE, vcall_offset);
9410 }
9411 }
9412
9413 /* Return vtbl initializers for the RTTI entries corresponding to the
9414 BINFO's vtable. The RTTI entries should indicate the object given
9415 by VID->rtti_binfo. */
9416
9417 static void
9418 build_rtti_vtbl_entries (tree binfo, vtbl_init_data* vid)
9419 {
9420 tree b;
9421 tree t;
9422 tree offset;
9423 tree decl;
9424 tree init;
9425
9426 t = BINFO_TYPE (vid->rtti_binfo);
9427
9428 /* To find the complete object, we will first convert to our most
9429 primary base, and then add the offset in the vtbl to that value. */
9430 b = binfo;
9431 while (CLASSTYPE_HAS_PRIMARY_BASE_P (BINFO_TYPE (b))
9432 && !BINFO_LOST_PRIMARY_P (b))
9433 {
9434 tree primary_base;
9435
9436 primary_base = get_primary_binfo (b);
9437 gcc_assert (BINFO_PRIMARY_P (primary_base)
9438 && BINFO_INHERITANCE_CHAIN (primary_base) == b);
9439 b = primary_base;
9440 }
9441 offset = size_diffop_loc (input_location,
9442 BINFO_OFFSET (vid->rtti_binfo), BINFO_OFFSET (b));
9443
9444 /* The second entry is the address of the typeinfo object. */
9445 if (flag_rtti)
9446 decl = build_address (get_tinfo_decl (t));
9447 else
9448 decl = integer_zero_node;
9449
9450 /* Convert the declaration to a type that can be stored in the
9451 vtable. */
9452 init = build_nop (vfunc_ptr_type_node, decl);
9453 CONSTRUCTOR_APPEND_ELT (vid->inits, NULL_TREE, init);
9454
9455 /* Add the offset-to-top entry. It comes earlier in the vtable than
9456 the typeinfo entry. Convert the offset to look like a
9457 function pointer, so that we can put it in the vtable. */
9458 init = build_nop (vfunc_ptr_type_node, offset);
9459 CONSTRUCTOR_APPEND_ELT (vid->inits, NULL_TREE, init);
9460 }
9461
9462 /* TRUE iff TYPE is uniquely derived from PARENT. Ignores
9463 accessibility. */
9464
9465 bool
9466 uniquely_derived_from_p (tree parent, tree type)
9467 {
9468 tree base = lookup_base (type, parent, ba_unique, NULL, tf_none);
9469 return base && base != error_mark_node;
9470 }
9471
9472 /* TRUE iff TYPE is publicly & uniquely derived from PARENT. */
9473
9474 bool
9475 publicly_uniquely_derived_p (tree parent, tree type)
9476 {
9477 tree base = lookup_base (type, parent, ba_ignore_scope | ba_check,
9478 NULL, tf_none);
9479 return base && base != error_mark_node;
9480 }
9481
9482 /* CTX1 and CTX2 are declaration contexts. Return the innermost common
9483 class between them, if any. */
9484
9485 tree
9486 common_enclosing_class (tree ctx1, tree ctx2)
9487 {
9488 if (!TYPE_P (ctx1) || !TYPE_P (ctx2))
9489 return NULL_TREE;
9490 gcc_assert (ctx1 == TYPE_MAIN_VARIANT (ctx1)
9491 && ctx2 == TYPE_MAIN_VARIANT (ctx2));
9492 if (ctx1 == ctx2)
9493 return ctx1;
9494 for (tree t = ctx1; TYPE_P (t); t = TYPE_CONTEXT (t))
9495 TYPE_MARKED_P (t) = true;
9496 tree found = NULL_TREE;
9497 for (tree t = ctx2; TYPE_P (t); t = TYPE_CONTEXT (t))
9498 if (TYPE_MARKED_P (t))
9499 {
9500 found = t;
9501 break;
9502 }
9503 for (tree t = ctx1; TYPE_P (t); t = TYPE_CONTEXT (t))
9504 TYPE_MARKED_P (t) = false;
9505 return found;
9506 }
9507
9508 #include "gt-cp-class.h"