Merge from trunk.
[official-gcc.git] / gcc / cp / class.c
blob0d7a1db4d18f74f689ed8d69d4a3cc1d1caa0f49
1 /* Functions related to building classes and their related objects.
2 Copyright (C) 1987-2014 Free Software Foundation, Inc.
3 Contributed by Michael Tiemann (tiemann@cygnus.com)
5 This file is part of GCC.
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.
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.
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/>. */
22 /* High-level class interface. */
24 #include "config.h"
25 #include "system.h"
26 #include "coretypes.h"
27 #include "tm.h"
28 #include "tree.h"
29 #include "stringpool.h"
30 #include "stor-layout.h"
31 #include "attribs.h"
32 #include "pointer-set.h"
33 #include "hash-table.h"
34 #include "cp-tree.h"
35 #include "flags.h"
36 #include "toplev.h"
37 #include "target.h"
38 #include "convert.h"
39 #include "cgraph.h"
40 #include "dumpfile.h"
41 #include "splay-tree.h"
42 #include "gimplify.h"
43 #include "wide-int.h"
45 /* The number of nested classes being processed. If we are not in the
46 scope of any class, this is zero. */
48 int current_class_depth;
50 /* In order to deal with nested classes, we keep a stack of classes.
51 The topmost entry is the innermost class, and is the entry at index
52 CURRENT_CLASS_DEPTH */
54 typedef struct class_stack_node {
55 /* The name of the class. */
56 tree name;
58 /* The _TYPE node for the class. */
59 tree type;
61 /* The access specifier pending for new declarations in the scope of
62 this class. */
63 tree access;
65 /* If were defining TYPE, the names used in this class. */
66 splay_tree names_used;
68 /* Nonzero if this class is no longer open, because of a call to
69 push_to_top_level. */
70 size_t hidden;
71 }* class_stack_node_t;
73 typedef struct vtbl_init_data_s
75 /* The base for which we're building initializers. */
76 tree binfo;
77 /* The type of the most-derived type. */
78 tree derived;
79 /* The binfo for the dynamic type. This will be TYPE_BINFO (derived),
80 unless ctor_vtbl_p is true. */
81 tree rtti_binfo;
82 /* The negative-index vtable initializers built up so far. These
83 are in order from least negative index to most negative index. */
84 vec<constructor_elt, va_gc> *inits;
85 /* The binfo for the virtual base for which we're building
86 vcall offset initializers. */
87 tree vbase;
88 /* The functions in vbase for which we have already provided vcall
89 offsets. */
90 vec<tree, va_gc> *fns;
91 /* The vtable index of the next vcall or vbase offset. */
92 tree index;
93 /* Nonzero if we are building the initializer for the primary
94 vtable. */
95 int primary_vtbl_p;
96 /* Nonzero if we are building the initializer for a construction
97 vtable. */
98 int ctor_vtbl_p;
99 /* True when adding vcall offset entries to the vtable. False when
100 merely computing the indices. */
101 bool generate_vcall_entries;
102 } vtbl_init_data;
104 /* The type of a function passed to walk_subobject_offsets. */
105 typedef int (*subobject_offset_fn) (tree, tree, splay_tree);
107 /* The stack itself. This is a dynamically resized array. The
108 number of elements allocated is CURRENT_CLASS_STACK_SIZE. */
109 static int current_class_stack_size;
110 static class_stack_node_t current_class_stack;
112 /* The size of the largest empty class seen in this translation unit. */
113 static GTY (()) tree sizeof_biggest_empty_class;
115 /* An array of all local classes present in this translation unit, in
116 declaration order. */
117 vec<tree, va_gc> *local_classes;
119 static tree get_vfield_name (tree);
120 static void finish_struct_anon (tree);
121 static tree get_vtable_name (tree);
122 static tree get_basefndecls (tree, tree);
123 static int build_primary_vtable (tree, tree);
124 static int build_secondary_vtable (tree);
125 static void finish_vtbls (tree);
126 static void modify_vtable_entry (tree, tree, tree, tree, tree *);
127 static void finish_struct_bits (tree);
128 static int alter_access (tree, tree, tree);
129 static void handle_using_decl (tree, tree);
130 static tree dfs_modify_vtables (tree, void *);
131 static tree modify_all_vtables (tree, tree);
132 static void determine_primary_bases (tree);
133 static void finish_struct_methods (tree);
134 static void maybe_warn_about_overly_private_class (tree);
135 static int method_name_cmp (const void *, const void *);
136 static int resort_method_name_cmp (const void *, const void *);
137 static void add_implicitly_declared_members (tree, tree*, int, int);
138 static tree fixed_type_or_null (tree, int *, int *);
139 static tree build_simple_base_path (tree expr, tree binfo);
140 static tree build_vtbl_ref_1 (tree, tree);
141 static void build_vtbl_initializer (tree, tree, tree, tree, int *,
142 vec<constructor_elt, va_gc> **);
143 static int count_fields (tree);
144 static int add_fields_to_record_type (tree, struct sorted_fields_type*, int);
145 static void insert_into_classtype_sorted_fields (tree, tree, int);
146 static bool check_bitfield_decl (tree);
147 static void check_field_decl (tree, tree, int *, int *, int *);
148 static void check_field_decls (tree, tree *, int *, int *);
149 static tree *build_base_field (record_layout_info, tree, splay_tree, tree *);
150 static void build_base_fields (record_layout_info, splay_tree, tree *);
151 static void check_methods (tree);
152 static void remove_zero_width_bit_fields (tree);
153 static bool accessible_nvdtor_p (tree);
154 static void check_bases (tree, int *, int *);
155 static void check_bases_and_members (tree);
156 static tree create_vtable_ptr (tree, tree *);
157 static void include_empty_classes (record_layout_info);
158 static void layout_class_type (tree, tree *);
159 static void propagate_binfo_offsets (tree, tree);
160 static void layout_virtual_bases (record_layout_info, splay_tree);
161 static void build_vbase_offset_vtbl_entries (tree, vtbl_init_data *);
162 static void add_vcall_offset_vtbl_entries_r (tree, vtbl_init_data *);
163 static void add_vcall_offset_vtbl_entries_1 (tree, vtbl_init_data *);
164 static void build_vcall_offset_vtbl_entries (tree, vtbl_init_data *);
165 static void add_vcall_offset (tree, tree, vtbl_init_data *);
166 static void layout_vtable_decl (tree, int);
167 static tree dfs_find_final_overrider_pre (tree, void *);
168 static tree dfs_find_final_overrider_post (tree, void *);
169 static tree find_final_overrider (tree, tree, tree);
170 static int make_new_vtable (tree, tree);
171 static tree get_primary_binfo (tree);
172 static int maybe_indent_hierarchy (FILE *, int, int);
173 static tree dump_class_hierarchy_r (FILE *, int, tree, tree, int);
174 static void dump_class_hierarchy (tree);
175 static void dump_class_hierarchy_1 (FILE *, int, tree);
176 static void dump_array (FILE *, tree);
177 static void dump_vtable (tree, tree, tree);
178 static void dump_vtt (tree, tree);
179 static void dump_thunk (FILE *, int, tree);
180 static tree build_vtable (tree, tree, tree);
181 static void initialize_vtable (tree, vec<constructor_elt, va_gc> *);
182 static void layout_nonempty_base_or_field (record_layout_info,
183 tree, tree, splay_tree);
184 static tree end_of_class (tree, int);
185 static bool layout_empty_base (record_layout_info, tree, tree, splay_tree);
186 static void accumulate_vtbl_inits (tree, tree, tree, tree, tree,
187 vec<constructor_elt, va_gc> **);
188 static void dfs_accumulate_vtbl_inits (tree, tree, tree, tree, tree,
189 vec<constructor_elt, va_gc> **);
190 static void build_rtti_vtbl_entries (tree, vtbl_init_data *);
191 static void build_vcall_and_vbase_vtbl_entries (tree, vtbl_init_data *);
192 static void clone_constructors_and_destructors (tree);
193 static tree build_clone (tree, tree);
194 static void update_vtable_entry_for_fn (tree, tree, tree, tree *, unsigned);
195 static void build_ctor_vtbl_group (tree, tree);
196 static void build_vtt (tree);
197 static tree binfo_ctor_vtable (tree);
198 static void build_vtt_inits (tree, tree, vec<constructor_elt, va_gc> **,
199 tree *);
200 static tree dfs_build_secondary_vptr_vtt_inits (tree, void *);
201 static tree dfs_fixup_binfo_vtbls (tree, void *);
202 static int record_subobject_offset (tree, tree, splay_tree);
203 static int check_subobject_offset (tree, tree, splay_tree);
204 static int walk_subobject_offsets (tree, subobject_offset_fn,
205 tree, splay_tree, tree, int);
206 static void record_subobject_offsets (tree, tree, splay_tree, bool);
207 static int layout_conflict_p (tree, tree, splay_tree, int);
208 static int splay_tree_compare_integer_csts (splay_tree_key k1,
209 splay_tree_key k2);
210 static void warn_about_ambiguous_bases (tree);
211 static bool type_requires_array_cookie (tree);
212 static bool contains_empty_class_p (tree);
213 static bool base_derived_from (tree, tree);
214 static int empty_base_at_nonzero_offset_p (tree, tree, splay_tree);
215 static tree end_of_base (tree);
216 static tree get_vcall_index (tree, tree);
218 /* Variables shared between class.c and call.c. */
220 int n_vtables = 0;
221 int n_vtable_entries = 0;
222 int n_vtable_searches = 0;
223 int n_vtable_elems = 0;
224 int n_convert_harshness = 0;
225 int n_compute_conversion_costs = 0;
226 int n_inner_fields_searched = 0;
228 /* Convert to or from a base subobject. EXPR is an expression of type
229 `A' or `A*', an expression of type `B' or `B*' is returned. To
230 convert A to a base B, CODE is PLUS_EXPR and BINFO is the binfo for
231 the B base instance within A. To convert base A to derived B, CODE
232 is MINUS_EXPR and BINFO is the binfo for the A instance within B.
233 In this latter case, A must not be a morally virtual base of B.
234 NONNULL is true if EXPR is known to be non-NULL (this is only
235 needed when EXPR is of pointer type). CV qualifiers are preserved
236 from EXPR. */
238 tree
239 build_base_path (enum tree_code code,
240 tree expr,
241 tree binfo,
242 int nonnull,
243 tsubst_flags_t complain)
245 tree v_binfo = NULL_TREE;
246 tree d_binfo = NULL_TREE;
247 tree probe;
248 tree offset;
249 tree target_type;
250 tree null_test = NULL;
251 tree ptr_target_type;
252 int fixed_type_p;
253 int want_pointer = TYPE_PTR_P (TREE_TYPE (expr));
254 bool has_empty = false;
255 bool virtual_access;
257 if (expr == error_mark_node || binfo == error_mark_node || !binfo)
258 return error_mark_node;
260 for (probe = binfo; probe; probe = BINFO_INHERITANCE_CHAIN (probe))
262 d_binfo = probe;
263 if (is_empty_class (BINFO_TYPE (probe)))
264 has_empty = true;
265 if (!v_binfo && BINFO_VIRTUAL_P (probe))
266 v_binfo = probe;
269 probe = TYPE_MAIN_VARIANT (TREE_TYPE (expr));
270 if (want_pointer)
271 probe = TYPE_MAIN_VARIANT (TREE_TYPE (probe));
273 if (code == PLUS_EXPR
274 && !SAME_BINFO_TYPE_P (BINFO_TYPE (d_binfo), probe))
276 /* This can happen when adjust_result_of_qualified_name_lookup can't
277 find a unique base binfo in a call to a member function. We
278 couldn't give the diagnostic then since we might have been calling
279 a static member function, so we do it now. */
280 if (complain & tf_error)
282 tree base = lookup_base (probe, BINFO_TYPE (d_binfo),
283 ba_unique, NULL, complain);
284 gcc_assert (base == error_mark_node);
286 return error_mark_node;
289 gcc_assert ((code == MINUS_EXPR
290 && SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), probe))
291 || code == PLUS_EXPR);
293 if (binfo == d_binfo)
294 /* Nothing to do. */
295 return expr;
297 if (code == MINUS_EXPR && v_binfo)
299 if (complain & tf_error)
301 if (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), BINFO_TYPE (v_binfo)))
303 if (want_pointer)
304 error ("cannot convert from pointer to base class %qT to "
305 "pointer to derived class %qT because the base is "
306 "virtual", BINFO_TYPE (binfo), BINFO_TYPE (d_binfo));
307 else
308 error ("cannot convert from base class %qT to derived "
309 "class %qT because the base is virtual",
310 BINFO_TYPE (binfo), BINFO_TYPE (d_binfo));
312 else
314 if (want_pointer)
315 error ("cannot convert from pointer to base class %qT to "
316 "pointer to derived class %qT via virtual base %qT",
317 BINFO_TYPE (binfo), BINFO_TYPE (d_binfo),
318 BINFO_TYPE (v_binfo));
319 else
320 error ("cannot convert from base class %qT to derived "
321 "class %qT via virtual base %qT", BINFO_TYPE (binfo),
322 BINFO_TYPE (d_binfo), BINFO_TYPE (v_binfo));
325 return error_mark_node;
328 if (!want_pointer)
329 /* This must happen before the call to save_expr. */
330 expr = cp_build_addr_expr (expr, complain);
331 else
332 expr = mark_rvalue_use (expr);
334 offset = BINFO_OFFSET (binfo);
335 fixed_type_p = resolves_to_fixed_type_p (expr, &nonnull);
336 target_type = code == PLUS_EXPR ? BINFO_TYPE (binfo) : BINFO_TYPE (d_binfo);
337 /* TARGET_TYPE has been extracted from BINFO, and, is therefore always
338 cv-unqualified. Extract the cv-qualifiers from EXPR so that the
339 expression returned matches the input. */
340 target_type = cp_build_qualified_type
341 (target_type, cp_type_quals (TREE_TYPE (TREE_TYPE (expr))));
342 ptr_target_type = build_pointer_type (target_type);
344 /* Do we need to look in the vtable for the real offset? */
345 virtual_access = (v_binfo && fixed_type_p <= 0);
347 /* Don't bother with the calculations inside sizeof; they'll ICE if the
348 source type is incomplete and the pointer value doesn't matter. In a
349 template (even in fold_non_dependent_expr), we don't have vtables set
350 up properly yet, and the value doesn't matter there either; we're just
351 interested in the result of overload resolution. */
352 if (cp_unevaluated_operand != 0
353 || in_template_function ())
355 expr = build_nop (ptr_target_type, expr);
356 if (!want_pointer)
357 expr = build_indirect_ref (EXPR_LOCATION (expr), expr, RO_NULL);
358 return expr;
361 /* If we're in an NSDMI, we don't have the full constructor context yet
362 that we need for converting to a virtual base, so just build a stub
363 CONVERT_EXPR and expand it later in bot_replace. */
364 if (virtual_access && fixed_type_p < 0
365 && current_scope () != current_function_decl)
367 expr = build1 (CONVERT_EXPR, ptr_target_type, expr);
368 CONVERT_EXPR_VBASE_PATH (expr) = true;
369 if (!want_pointer)
370 expr = build_indirect_ref (EXPR_LOCATION (expr), expr, RO_NULL);
371 return expr;
374 /* Do we need to check for a null pointer? */
375 if (want_pointer && !nonnull)
377 /* If we know the conversion will not actually change the value
378 of EXPR, then we can avoid testing the expression for NULL.
379 We have to avoid generating a COMPONENT_REF for a base class
380 field, because other parts of the compiler know that such
381 expressions are always non-NULL. */
382 if (!virtual_access && integer_zerop (offset))
383 return build_nop (ptr_target_type, expr);
384 null_test = error_mark_node;
387 /* Protect against multiple evaluation if necessary. */
388 if (TREE_SIDE_EFFECTS (expr) && (null_test || virtual_access))
389 expr = save_expr (expr);
391 /* Now that we've saved expr, build the real null test. */
392 if (null_test)
394 tree zero = cp_convert (TREE_TYPE (expr), nullptr_node, complain);
395 null_test = fold_build2_loc (input_location, NE_EXPR, boolean_type_node,
396 expr, zero);
399 /* If this is a simple base reference, express it as a COMPONENT_REF. */
400 if (code == PLUS_EXPR && !virtual_access
401 /* We don't build base fields for empty bases, and they aren't very
402 interesting to the optimizers anyway. */
403 && !has_empty)
405 expr = cp_build_indirect_ref (expr, RO_NULL, complain);
406 expr = build_simple_base_path (expr, binfo);
407 if (want_pointer)
408 expr = build_address (expr);
409 target_type = TREE_TYPE (expr);
410 goto out;
413 if (virtual_access)
415 /* Going via virtual base V_BINFO. We need the static offset
416 from V_BINFO to BINFO, and the dynamic offset from D_BINFO to
417 V_BINFO. That offset is an entry in D_BINFO's vtable. */
418 tree v_offset;
420 if (fixed_type_p < 0 && in_base_initializer)
422 /* In a base member initializer, we cannot rely on the
423 vtable being set up. We have to indirect via the
424 vtt_parm. */
425 tree t;
427 t = TREE_TYPE (TYPE_VFIELD (current_class_type));
428 t = build_pointer_type (t);
429 v_offset = convert (t, current_vtt_parm);
430 v_offset = cp_build_indirect_ref (v_offset, RO_NULL, complain);
432 else
433 v_offset = build_vfield_ref (cp_build_indirect_ref (expr, RO_NULL,
434 complain),
435 TREE_TYPE (TREE_TYPE (expr)));
437 if (v_offset == error_mark_node)
438 return error_mark_node;
440 v_offset = fold_build_pointer_plus (v_offset, BINFO_VPTR_FIELD (v_binfo));
441 v_offset = build1 (NOP_EXPR,
442 build_pointer_type (ptrdiff_type_node),
443 v_offset);
444 v_offset = cp_build_indirect_ref (v_offset, RO_NULL, complain);
445 TREE_CONSTANT (v_offset) = 1;
447 offset = convert_to_integer (ptrdiff_type_node,
448 size_diffop_loc (input_location, offset,
449 BINFO_OFFSET (v_binfo)));
451 if (!integer_zerop (offset))
452 v_offset = build2 (code, ptrdiff_type_node, v_offset, offset);
454 if (fixed_type_p < 0)
455 /* Negative fixed_type_p means this is a constructor or destructor;
456 virtual base layout is fixed in in-charge [cd]tors, but not in
457 base [cd]tors. */
458 offset = build3 (COND_EXPR, ptrdiff_type_node,
459 build2 (EQ_EXPR, boolean_type_node,
460 current_in_charge_parm, integer_zero_node),
461 v_offset,
462 convert_to_integer (ptrdiff_type_node,
463 BINFO_OFFSET (binfo)));
464 else
465 offset = v_offset;
468 if (want_pointer)
469 target_type = ptr_target_type;
471 expr = build1 (NOP_EXPR, ptr_target_type, expr);
473 if (!integer_zerop (offset))
475 offset = fold_convert (sizetype, offset);
476 if (code == MINUS_EXPR)
477 offset = fold_build1_loc (input_location, NEGATE_EXPR, sizetype, offset);
478 expr = fold_build_pointer_plus (expr, offset);
480 else
481 null_test = NULL;
483 if (!want_pointer)
484 expr = cp_build_indirect_ref (expr, RO_NULL, complain);
486 out:
487 if (null_test)
488 expr = fold_build3_loc (input_location, COND_EXPR, target_type, null_test, expr,
489 build_zero_cst (target_type));
491 return expr;
494 /* Subroutine of build_base_path; EXPR and BINFO are as in that function.
495 Perform a derived-to-base conversion by recursively building up a
496 sequence of COMPONENT_REFs to the appropriate base fields. */
498 static tree
499 build_simple_base_path (tree expr, tree binfo)
501 tree type = BINFO_TYPE (binfo);
502 tree d_binfo = BINFO_INHERITANCE_CHAIN (binfo);
503 tree field;
505 if (d_binfo == NULL_TREE)
507 tree temp;
509 gcc_assert (TYPE_MAIN_VARIANT (TREE_TYPE (expr)) == type);
511 /* Transform `(a, b).x' into `(*(a, &b)).x', `(a ? b : c).x'
512 into `(*(a ? &b : &c)).x', and so on. A COND_EXPR is only
513 an lvalue in the front end; only _DECLs and _REFs are lvalues
514 in the back end. */
515 temp = unary_complex_lvalue (ADDR_EXPR, expr);
516 if (temp)
517 expr = cp_build_indirect_ref (temp, RO_NULL, tf_warning_or_error);
519 return expr;
522 /* Recurse. */
523 expr = build_simple_base_path (expr, d_binfo);
525 for (field = TYPE_FIELDS (BINFO_TYPE (d_binfo));
526 field; field = DECL_CHAIN (field))
527 /* Is this the base field created by build_base_field? */
528 if (TREE_CODE (field) == FIELD_DECL
529 && DECL_FIELD_IS_BASE (field)
530 && TREE_TYPE (field) == type
531 /* If we're looking for a field in the most-derived class,
532 also check the field offset; we can have two base fields
533 of the same type if one is an indirect virtual base and one
534 is a direct non-virtual base. */
535 && (BINFO_INHERITANCE_CHAIN (d_binfo)
536 || tree_int_cst_equal (byte_position (field),
537 BINFO_OFFSET (binfo))))
539 /* We don't use build_class_member_access_expr here, as that
540 has unnecessary checks, and more importantly results in
541 recursive calls to dfs_walk_once. */
542 int type_quals = cp_type_quals (TREE_TYPE (expr));
544 expr = build3 (COMPONENT_REF,
545 cp_build_qualified_type (type, type_quals),
546 expr, field, NULL_TREE);
547 expr = fold_if_not_in_template (expr);
549 /* Mark the expression const or volatile, as appropriate.
550 Even though we've dealt with the type above, we still have
551 to mark the expression itself. */
552 if (type_quals & TYPE_QUAL_CONST)
553 TREE_READONLY (expr) = 1;
554 if (type_quals & TYPE_QUAL_VOLATILE)
555 TREE_THIS_VOLATILE (expr) = 1;
557 return expr;
560 /* Didn't find the base field?!? */
561 gcc_unreachable ();
564 /* Convert OBJECT to the base TYPE. OBJECT is an expression whose
565 type is a class type or a pointer to a class type. In the former
566 case, TYPE is also a class type; in the latter it is another
567 pointer type. If CHECK_ACCESS is true, an error message is emitted
568 if TYPE is inaccessible. If OBJECT has pointer type, the value is
569 assumed to be non-NULL. */
571 tree
572 convert_to_base (tree object, tree type, bool check_access, bool nonnull,
573 tsubst_flags_t complain)
575 tree binfo;
576 tree object_type;
578 if (TYPE_PTR_P (TREE_TYPE (object)))
580 object_type = TREE_TYPE (TREE_TYPE (object));
581 type = TREE_TYPE (type);
583 else
584 object_type = TREE_TYPE (object);
586 binfo = lookup_base (object_type, type, check_access ? ba_check : ba_unique,
587 NULL, complain);
588 if (!binfo || binfo == error_mark_node)
589 return error_mark_node;
591 return build_base_path (PLUS_EXPR, object, binfo, nonnull, complain);
594 /* EXPR is an expression with unqualified class type. BASE is a base
595 binfo of that class type. Returns EXPR, converted to the BASE
596 type. This function assumes that EXPR is the most derived class;
597 therefore virtual bases can be found at their static offsets. */
599 tree
600 convert_to_base_statically (tree expr, tree base)
602 tree expr_type;
604 expr_type = TREE_TYPE (expr);
605 if (!SAME_BINFO_TYPE_P (BINFO_TYPE (base), expr_type))
607 /* If this is a non-empty base, use a COMPONENT_REF. */
608 if (!is_empty_class (BINFO_TYPE (base)))
609 return build_simple_base_path (expr, base);
611 /* We use fold_build2 and fold_convert below to simplify the trees
612 provided to the optimizers. It is not safe to call these functions
613 when processing a template because they do not handle C++-specific
614 trees. */
615 gcc_assert (!processing_template_decl);
616 expr = cp_build_addr_expr (expr, tf_warning_or_error);
617 if (!integer_zerop (BINFO_OFFSET (base)))
618 expr = fold_build_pointer_plus_loc (input_location,
619 expr, BINFO_OFFSET (base));
620 expr = fold_convert (build_pointer_type (BINFO_TYPE (base)), expr);
621 expr = build_fold_indirect_ref_loc (input_location, expr);
624 return expr;
628 tree
629 build_vfield_ref (tree datum, tree type)
631 tree vfield, vcontext;
633 if (datum == error_mark_node
634 /* Can happen in case of duplicate base types (c++/59082). */
635 || !TYPE_VFIELD (type))
636 return error_mark_node;
638 /* First, convert to the requested type. */
639 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (datum), type))
640 datum = convert_to_base (datum, type, /*check_access=*/false,
641 /*nonnull=*/true, tf_warning_or_error);
643 /* Second, the requested type may not be the owner of its own vptr.
644 If not, convert to the base class that owns it. We cannot use
645 convert_to_base here, because VCONTEXT may appear more than once
646 in the inheritance hierarchy of TYPE, and thus direct conversion
647 between the types may be ambiguous. Following the path back up
648 one step at a time via primary bases avoids the problem. */
649 vfield = TYPE_VFIELD (type);
650 vcontext = DECL_CONTEXT (vfield);
651 while (!same_type_ignoring_top_level_qualifiers_p (vcontext, type))
653 datum = build_simple_base_path (datum, CLASSTYPE_PRIMARY_BINFO (type));
654 type = TREE_TYPE (datum);
657 return build3 (COMPONENT_REF, TREE_TYPE (vfield), datum, vfield, NULL_TREE);
660 /* Given an object INSTANCE, return an expression which yields the
661 vtable element corresponding to INDEX. There are many special
662 cases for INSTANCE which we take care of here, mainly to avoid
663 creating extra tree nodes when we don't have to. */
665 static tree
666 build_vtbl_ref_1 (tree instance, tree idx)
668 tree aref;
669 tree vtbl = NULL_TREE;
671 /* Try to figure out what a reference refers to, and
672 access its virtual function table directly. */
674 int cdtorp = 0;
675 tree fixed_type = fixed_type_or_null (instance, NULL, &cdtorp);
677 tree basetype = non_reference (TREE_TYPE (instance));
679 if (fixed_type && !cdtorp)
681 tree binfo = lookup_base (fixed_type, basetype,
682 ba_unique, NULL, tf_none);
683 if (binfo && binfo != error_mark_node)
684 vtbl = unshare_expr (BINFO_VTABLE (binfo));
687 if (!vtbl)
688 vtbl = build_vfield_ref (instance, basetype);
690 aref = build_array_ref (input_location, vtbl, idx);
691 TREE_CONSTANT (aref) |= TREE_CONSTANT (vtbl) && TREE_CONSTANT (idx);
693 return aref;
696 tree
697 build_vtbl_ref (tree instance, tree idx)
699 tree aref = build_vtbl_ref_1 (instance, idx);
701 return aref;
704 /* Given a stable object pointer INSTANCE_PTR, return an expression which
705 yields a function pointer corresponding to vtable element INDEX. */
707 tree
708 build_vfn_ref (tree instance_ptr, tree idx)
710 tree aref;
712 aref = build_vtbl_ref_1 (cp_build_indirect_ref (instance_ptr, RO_NULL,
713 tf_warning_or_error),
714 idx);
716 /* When using function descriptors, the address of the
717 vtable entry is treated as a function pointer. */
718 if (TARGET_VTABLE_USES_DESCRIPTORS)
719 aref = build1 (NOP_EXPR, TREE_TYPE (aref),
720 cp_build_addr_expr (aref, tf_warning_or_error));
722 /* Remember this as a method reference, for later devirtualization. */
723 aref = build3 (OBJ_TYPE_REF, TREE_TYPE (aref), aref, instance_ptr, idx);
725 return aref;
728 /* Return the name of the virtual function table (as an IDENTIFIER_NODE)
729 for the given TYPE. */
731 static tree
732 get_vtable_name (tree type)
734 return mangle_vtbl_for_type (type);
737 /* DECL is an entity associated with TYPE, like a virtual table or an
738 implicitly generated constructor. Determine whether or not DECL
739 should have external or internal linkage at the object file
740 level. This routine does not deal with COMDAT linkage and other
741 similar complexities; it simply sets TREE_PUBLIC if it possible for
742 entities in other translation units to contain copies of DECL, in
743 the abstract. */
745 void
746 set_linkage_according_to_type (tree /*type*/, tree decl)
748 TREE_PUBLIC (decl) = 1;
749 determine_visibility (decl);
752 /* Create a VAR_DECL for a primary or secondary vtable for CLASS_TYPE.
753 (For a secondary vtable for B-in-D, CLASS_TYPE should be D, not B.)
754 Use NAME for the name of the vtable, and VTABLE_TYPE for its type. */
756 static tree
757 build_vtable (tree class_type, tree name, tree vtable_type)
759 tree decl;
761 decl = build_lang_decl (VAR_DECL, name, vtable_type);
762 /* vtable names are already mangled; give them their DECL_ASSEMBLER_NAME
763 now to avoid confusion in mangle_decl. */
764 SET_DECL_ASSEMBLER_NAME (decl, name);
765 DECL_CONTEXT (decl) = class_type;
766 DECL_ARTIFICIAL (decl) = 1;
767 TREE_STATIC (decl) = 1;
768 TREE_READONLY (decl) = 1;
769 DECL_VIRTUAL_P (decl) = 1;
770 DECL_ALIGN (decl) = TARGET_VTABLE_ENTRY_ALIGN;
771 DECL_USER_ALIGN (decl) = true;
772 DECL_VTABLE_OR_VTT_P (decl) = 1;
773 set_linkage_according_to_type (class_type, decl);
774 /* The vtable has not been defined -- yet. */
775 DECL_EXTERNAL (decl) = 1;
776 DECL_NOT_REALLY_EXTERN (decl) = 1;
778 /* Mark the VAR_DECL node representing the vtable itself as a
779 "gratuitous" one, thereby forcing dwarfout.c to ignore it. It
780 is rather important that such things be ignored because any
781 effort to actually generate DWARF for them will run into
782 trouble when/if we encounter code like:
784 #pragma interface
785 struct S { virtual void member (); };
787 because the artificial declaration of the vtable itself (as
788 manufactured by the g++ front end) will say that the vtable is
789 a static member of `S' but only *after* the debug output for
790 the definition of `S' has already been output. This causes
791 grief because the DWARF entry for the definition of the vtable
792 will try to refer back to an earlier *declaration* of the
793 vtable as a static member of `S' and there won't be one. We
794 might be able to arrange to have the "vtable static member"
795 attached to the member list for `S' before the debug info for
796 `S' get written (which would solve the problem) but that would
797 require more intrusive changes to the g++ front end. */
798 DECL_IGNORED_P (decl) = 1;
800 return decl;
803 /* Get the VAR_DECL of the vtable for TYPE. TYPE need not be polymorphic,
804 or even complete. If this does not exist, create it. If COMPLETE is
805 nonzero, then complete the definition of it -- that will render it
806 impossible to actually build the vtable, but is useful to get at those
807 which are known to exist in the runtime. */
809 tree
810 get_vtable_decl (tree type, int complete)
812 tree decl;
814 if (CLASSTYPE_VTABLES (type))
815 return CLASSTYPE_VTABLES (type);
817 decl = build_vtable (type, get_vtable_name (type), vtbl_type_node);
818 CLASSTYPE_VTABLES (type) = decl;
820 if (complete)
822 DECL_EXTERNAL (decl) = 1;
823 cp_finish_decl (decl, NULL_TREE, false, NULL_TREE, 0);
826 return decl;
829 /* Build the primary virtual function table for TYPE. If BINFO is
830 non-NULL, build the vtable starting with the initial approximation
831 that it is the same as the one which is the head of the association
832 list. Returns a nonzero value if a new vtable is actually
833 created. */
835 static int
836 build_primary_vtable (tree binfo, tree type)
838 tree decl;
839 tree virtuals;
841 decl = get_vtable_decl (type, /*complete=*/0);
843 if (binfo)
845 if (BINFO_NEW_VTABLE_MARKED (binfo))
846 /* We have already created a vtable for this base, so there's
847 no need to do it again. */
848 return 0;
850 virtuals = copy_list (BINFO_VIRTUALS (binfo));
851 TREE_TYPE (decl) = TREE_TYPE (get_vtbl_decl_for_binfo (binfo));
852 DECL_SIZE (decl) = TYPE_SIZE (TREE_TYPE (decl));
853 DECL_SIZE_UNIT (decl) = TYPE_SIZE_UNIT (TREE_TYPE (decl));
855 else
857 gcc_assert (TREE_TYPE (decl) == vtbl_type_node);
858 virtuals = NULL_TREE;
861 if (GATHER_STATISTICS)
863 n_vtables += 1;
864 n_vtable_elems += list_length (virtuals);
867 /* Initialize the association list for this type, based
868 on our first approximation. */
869 BINFO_VTABLE (TYPE_BINFO (type)) = decl;
870 BINFO_VIRTUALS (TYPE_BINFO (type)) = virtuals;
871 SET_BINFO_NEW_VTABLE_MARKED (TYPE_BINFO (type));
872 return 1;
875 /* Give BINFO a new virtual function table which is initialized
876 with a skeleton-copy of its original initialization. The only
877 entry that changes is the `delta' entry, so we can really
878 share a lot of structure.
880 FOR_TYPE is the most derived type which caused this table to
881 be needed.
883 Returns nonzero if we haven't met BINFO before.
885 The order in which vtables are built (by calling this function) for
886 an object must remain the same, otherwise a binary incompatibility
887 can result. */
889 static int
890 build_secondary_vtable (tree binfo)
892 if (BINFO_NEW_VTABLE_MARKED (binfo))
893 /* We already created a vtable for this base. There's no need to
894 do it again. */
895 return 0;
897 /* Remember that we've created a vtable for this BINFO, so that we
898 don't try to do so again. */
899 SET_BINFO_NEW_VTABLE_MARKED (binfo);
901 /* Make fresh virtual list, so we can smash it later. */
902 BINFO_VIRTUALS (binfo) = copy_list (BINFO_VIRTUALS (binfo));
904 /* Secondary vtables are laid out as part of the same structure as
905 the primary vtable. */
906 BINFO_VTABLE (binfo) = NULL_TREE;
907 return 1;
910 /* Create a new vtable for BINFO which is the hierarchy dominated by
911 T. Return nonzero if we actually created a new vtable. */
913 static int
914 make_new_vtable (tree t, tree binfo)
916 if (binfo == TYPE_BINFO (t))
917 /* In this case, it is *type*'s vtable we are modifying. We start
918 with the approximation that its vtable is that of the
919 immediate base class. */
920 return build_primary_vtable (binfo, t);
921 else
922 /* This is our very own copy of `basetype' to play with. Later,
923 we will fill in all the virtual functions that override the
924 virtual functions in these base classes which are not defined
925 by the current type. */
926 return build_secondary_vtable (binfo);
929 /* Make *VIRTUALS, an entry on the BINFO_VIRTUALS list for BINFO
930 (which is in the hierarchy dominated by T) list FNDECL as its
931 BV_FN. DELTA is the required constant adjustment from the `this'
932 pointer where the vtable entry appears to the `this' required when
933 the function is actually called. */
935 static void
936 modify_vtable_entry (tree t,
937 tree binfo,
938 tree fndecl,
939 tree delta,
940 tree *virtuals)
942 tree v;
944 v = *virtuals;
946 if (fndecl != BV_FN (v)
947 || !tree_int_cst_equal (delta, BV_DELTA (v)))
949 /* We need a new vtable for BINFO. */
950 if (make_new_vtable (t, binfo))
952 /* If we really did make a new vtable, we also made a copy
953 of the BINFO_VIRTUALS list. Now, we have to find the
954 corresponding entry in that list. */
955 *virtuals = BINFO_VIRTUALS (binfo);
956 while (BV_FN (*virtuals) != BV_FN (v))
957 *virtuals = TREE_CHAIN (*virtuals);
958 v = *virtuals;
961 BV_DELTA (v) = delta;
962 BV_VCALL_INDEX (v) = NULL_TREE;
963 BV_FN (v) = fndecl;
967 // Returns the template associated with the member FN or
968 // NULL if the declaration is neither a template nor temploid.
969 static inline tree
970 get_member_fn_template (tree fn)
972 if (TREE_CODE (fn) == TEMPLATE_DECL)
973 return fn;
974 if (TREE_CODE (fn) == FUNCTION_DECL && DECL_TEMPLATE_INFO (fn))
975 return DECL_TI_TEMPLATE (fn);
976 return NULL_TREE;
979 // Returns true if NEWDECL and OLDDECL are member functions with with
980 // different constraints. If NEWDECL and OLDDECL are non-template members
981 // or specializations of non-template members, the overloads are
982 // differentiated by the template constraints.
984 // Note that the types of the functions are assumed to be equivalent.
985 static inline bool
986 are_constrained_member_overloads (tree newdecl, tree olddecl)
988 newdecl = get_member_fn_template (newdecl);
989 olddecl = get_member_fn_template (olddecl);
991 // If neither is a template or temploid, then they cannot
992 // be constrained declarations.
993 if (!newdecl && !olddecl)
994 return false;
995 else
996 return !equivalently_constrained (newdecl, olddecl);
1000 /* Add method METHOD to class TYPE. If USING_DECL is non-null, it is
1001 the USING_DECL naming METHOD. Returns true if the method could be
1002 added to the method vec. */
1004 bool
1005 add_method (tree type, tree method, tree using_decl)
1007 unsigned slot;
1008 tree overload;
1009 bool template_conv_p = false;
1010 bool conv_p;
1011 vec<tree, va_gc> *method_vec;
1012 bool complete_p;
1013 bool insert_p = false;
1014 tree current_fns;
1015 tree fns;
1017 if (method == error_mark_node)
1018 return false;
1020 complete_p = COMPLETE_TYPE_P (type);
1021 conv_p = DECL_CONV_FN_P (method);
1022 if (conv_p)
1023 template_conv_p = (TREE_CODE (method) == TEMPLATE_DECL
1024 && DECL_TEMPLATE_CONV_FN_P (method));
1026 method_vec = CLASSTYPE_METHOD_VEC (type);
1027 if (!method_vec)
1029 /* Make a new method vector. We start with 8 entries. We must
1030 allocate at least two (for constructors and destructors), and
1031 we're going to end up with an assignment operator at some
1032 point as well. */
1033 vec_alloc (method_vec, 8);
1034 /* Create slots for constructors and destructors. */
1035 method_vec->quick_push (NULL_TREE);
1036 method_vec->quick_push (NULL_TREE);
1037 CLASSTYPE_METHOD_VEC (type) = method_vec;
1040 /* Maintain TYPE_HAS_USER_CONSTRUCTOR, etc. */
1041 grok_special_member_properties (method);
1043 /* Constructors and destructors go in special slots. */
1044 if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (method))
1045 slot = CLASSTYPE_CONSTRUCTOR_SLOT;
1046 else if (DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (method))
1048 slot = CLASSTYPE_DESTRUCTOR_SLOT;
1050 if (TYPE_FOR_JAVA (type))
1052 if (!DECL_ARTIFICIAL (method))
1053 error ("Java class %qT cannot have a destructor", type);
1054 else if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
1055 error ("Java class %qT cannot have an implicit non-trivial "
1056 "destructor",
1057 type);
1060 else
1062 tree m;
1064 insert_p = true;
1065 /* See if we already have an entry with this name. */
1066 for (slot = CLASSTYPE_FIRST_CONVERSION_SLOT;
1067 vec_safe_iterate (method_vec, slot, &m);
1068 ++slot)
1070 m = OVL_CURRENT (m);
1071 if (template_conv_p)
1073 if (TREE_CODE (m) == TEMPLATE_DECL
1074 && DECL_TEMPLATE_CONV_FN_P (m))
1075 insert_p = false;
1076 break;
1078 if (conv_p && !DECL_CONV_FN_P (m))
1079 break;
1080 if (DECL_NAME (m) == DECL_NAME (method))
1082 insert_p = false;
1083 break;
1085 if (complete_p
1086 && !DECL_CONV_FN_P (m)
1087 && DECL_NAME (m) > DECL_NAME (method))
1088 break;
1091 current_fns = insert_p ? NULL_TREE : (*method_vec)[slot];
1093 /* Check to see if we've already got this method. */
1094 for (fns = current_fns; fns; fns = OVL_NEXT (fns))
1096 tree fn = OVL_CURRENT (fns);
1097 tree fn_type;
1098 tree method_type;
1099 tree parms1;
1100 tree parms2;
1102 if (TREE_CODE (fn) != TREE_CODE (method))
1103 continue;
1105 /* [over.load] Member function declarations with the
1106 same name and the same parameter types cannot be
1107 overloaded if any of them is a static member
1108 function declaration.
1110 [over.load] Member function declarations with the same name and
1111 the same parameter-type-list as well as member function template
1112 declarations with the same name, the same parameter-type-list, and
1113 the same template parameter lists cannot be overloaded if any of
1114 them, but not all, have a ref-qualifier.
1116 [namespace.udecl] When a using-declaration brings names
1117 from a base class into a derived class scope, member
1118 functions in the derived class override and/or hide member
1119 functions with the same name and parameter types in a base
1120 class (rather than conflicting). */
1121 fn_type = TREE_TYPE (fn);
1122 method_type = TREE_TYPE (method);
1123 parms1 = TYPE_ARG_TYPES (fn_type);
1124 parms2 = TYPE_ARG_TYPES (method_type);
1126 /* Compare the quals on the 'this' parm. Don't compare
1127 the whole types, as used functions are treated as
1128 coming from the using class in overload resolution. */
1129 if (! DECL_STATIC_FUNCTION_P (fn)
1130 && ! DECL_STATIC_FUNCTION_P (method)
1131 /* Either both or neither need to be ref-qualified for
1132 differing quals to allow overloading. */
1133 && (FUNCTION_REF_QUALIFIED (fn_type)
1134 == FUNCTION_REF_QUALIFIED (method_type))
1135 && (type_memfn_quals (fn_type) != type_memfn_quals (method_type)
1136 || type_memfn_rqual (fn_type) != type_memfn_rqual (method_type)))
1137 continue;
1139 /* For templates, the return type and template parameters
1140 must be identical. */
1141 if (TREE_CODE (fn) == TEMPLATE_DECL
1142 && (!same_type_p (TREE_TYPE (fn_type),
1143 TREE_TYPE (method_type))
1144 || !comp_template_parms (DECL_TEMPLATE_PARMS (fn),
1145 DECL_TEMPLATE_PARMS (method))))
1146 continue;
1148 if (! DECL_STATIC_FUNCTION_P (fn))
1149 parms1 = TREE_CHAIN (parms1);
1150 if (! DECL_STATIC_FUNCTION_P (method))
1151 parms2 = TREE_CHAIN (parms2);
1153 if (compparms (parms1, parms2)
1154 && (!DECL_CONV_FN_P (fn)
1155 || same_type_p (TREE_TYPE (fn_type),
1156 TREE_TYPE (method_type))))
1158 /* For function versions, their parms and types match
1159 but they are not duplicates. Record function versions
1160 as and when they are found. extern "C" functions are
1161 not treated as versions. */
1162 if (TREE_CODE (fn) == FUNCTION_DECL
1163 && TREE_CODE (method) == FUNCTION_DECL
1164 && !DECL_EXTERN_C_P (fn)
1165 && !DECL_EXTERN_C_P (method)
1166 && targetm.target_option.function_versions (fn, method))
1168 /* Mark functions as versions if necessary. Modify the mangled
1169 decl name if necessary. */
1170 if (!DECL_FUNCTION_VERSIONED (fn))
1172 DECL_FUNCTION_VERSIONED (fn) = 1;
1173 if (DECL_ASSEMBLER_NAME_SET_P (fn))
1174 mangle_decl (fn);
1176 if (!DECL_FUNCTION_VERSIONED (method))
1178 DECL_FUNCTION_VERSIONED (method) = 1;
1179 if (DECL_ASSEMBLER_NAME_SET_P (method))
1180 mangle_decl (method);
1182 record_function_versions (fn, method);
1183 continue;
1185 if (DECL_INHERITED_CTOR_BASE (method))
1187 if (DECL_INHERITED_CTOR_BASE (fn))
1189 error_at (DECL_SOURCE_LOCATION (method),
1190 "%q#D inherited from %qT", method,
1191 DECL_INHERITED_CTOR_BASE (method));
1192 error_at (DECL_SOURCE_LOCATION (fn),
1193 "conflicts with version inherited from %qT",
1194 DECL_INHERITED_CTOR_BASE (fn));
1196 /* Otherwise defer to the other function. */
1197 return false;
1199 if (using_decl)
1201 if (DECL_CONTEXT (fn) == type)
1202 /* Defer to the local function. */
1203 return false;
1205 else if (are_constrained_member_overloads (fn, method))
1206 continue;
1207 else
1209 error ("%q+#D cannot be overloaded", method);
1210 error ("with %q+#D", fn);
1213 /* We don't call duplicate_decls here to merge the
1214 declarations because that will confuse things if the
1215 methods have inline definitions. In particular, we
1216 will crash while processing the definitions. */
1217 return false;
1221 /* A class should never have more than one destructor. */
1222 if (current_fns && DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (method))
1223 return false;
1225 /* Add the new binding. */
1226 if (using_decl)
1228 overload = ovl_cons (method, current_fns);
1229 OVL_USED (overload) = true;
1231 else
1232 overload = build_overload (method, current_fns);
1234 if (conv_p)
1235 TYPE_HAS_CONVERSION (type) = 1;
1236 else if (slot >= CLASSTYPE_FIRST_CONVERSION_SLOT && !complete_p)
1237 push_class_level_binding (DECL_NAME (method), overload);
1239 if (insert_p)
1241 bool reallocated;
1243 /* We only expect to add few methods in the COMPLETE_P case, so
1244 just make room for one more method in that case. */
1245 if (complete_p)
1246 reallocated = vec_safe_reserve_exact (method_vec, 1);
1247 else
1248 reallocated = vec_safe_reserve (method_vec, 1);
1249 if (reallocated)
1250 CLASSTYPE_METHOD_VEC (type) = method_vec;
1251 if (slot == method_vec->length ())
1252 method_vec->quick_push (overload);
1253 else
1254 method_vec->quick_insert (slot, overload);
1256 else
1257 /* Replace the current slot. */
1258 (*method_vec)[slot] = overload;
1259 return true;
1262 /* Subroutines of finish_struct. */
1264 /* Change the access of FDECL to ACCESS in T. Return 1 if change was
1265 legit, otherwise return 0. */
1267 static int
1268 alter_access (tree t, tree fdecl, tree access)
1270 tree elem;
1272 if (!DECL_LANG_SPECIFIC (fdecl))
1273 retrofit_lang_decl (fdecl);
1275 gcc_assert (!DECL_DISCRIMINATOR_P (fdecl));
1277 elem = purpose_member (t, DECL_ACCESS (fdecl));
1278 if (elem)
1280 if (TREE_VALUE (elem) != access)
1282 if (TREE_CODE (TREE_TYPE (fdecl)) == FUNCTION_DECL)
1283 error ("conflicting access specifications for method"
1284 " %q+D, ignored", TREE_TYPE (fdecl));
1285 else
1286 error ("conflicting access specifications for field %qE, ignored",
1287 DECL_NAME (fdecl));
1289 else
1291 /* They're changing the access to the same thing they changed
1292 it to before. That's OK. */
1296 else
1298 perform_or_defer_access_check (TYPE_BINFO (t), fdecl, fdecl,
1299 tf_warning_or_error);
1300 DECL_ACCESS (fdecl) = tree_cons (t, access, DECL_ACCESS (fdecl));
1301 return 1;
1303 return 0;
1306 /* Process the USING_DECL, which is a member of T. */
1308 static void
1309 handle_using_decl (tree using_decl, tree t)
1311 tree decl = USING_DECL_DECLS (using_decl);
1312 tree name = DECL_NAME (using_decl);
1313 tree access
1314 = TREE_PRIVATE (using_decl) ? access_private_node
1315 : TREE_PROTECTED (using_decl) ? access_protected_node
1316 : access_public_node;
1317 tree flist = NULL_TREE;
1318 tree old_value;
1320 gcc_assert (!processing_template_decl && decl);
1322 old_value = lookup_member (t, name, /*protect=*/0, /*want_type=*/false,
1323 tf_warning_or_error);
1324 if (old_value)
1326 if (is_overloaded_fn (old_value))
1327 old_value = OVL_CURRENT (old_value);
1329 if (DECL_P (old_value) && DECL_CONTEXT (old_value) == t)
1330 /* OK */;
1331 else
1332 old_value = NULL_TREE;
1335 cp_emit_debug_info_for_using (decl, t);
1337 if (is_overloaded_fn (decl))
1338 flist = decl;
1340 if (! old_value)
1342 else if (is_overloaded_fn (old_value))
1344 if (flist)
1345 /* It's OK to use functions from a base when there are functions with
1346 the same name already present in the current class. */;
1347 else
1349 error ("%q+D invalid in %q#T", using_decl, t);
1350 error (" because of local method %q+#D with same name",
1351 OVL_CURRENT (old_value));
1352 return;
1355 else if (!DECL_ARTIFICIAL (old_value))
1357 error ("%q+D invalid in %q#T", using_decl, t);
1358 error (" because of local member %q+#D with same name", old_value);
1359 return;
1362 /* Make type T see field decl FDECL with access ACCESS. */
1363 if (flist)
1364 for (; flist; flist = OVL_NEXT (flist))
1366 add_method (t, OVL_CURRENT (flist), using_decl);
1367 alter_access (t, OVL_CURRENT (flist), access);
1369 else
1370 alter_access (t, decl, access);
1373 /* walk_tree callback for check_abi_tags: if the type at *TP involves any
1374 types with abi tags, add the corresponding identifiers to the VEC in
1375 *DATA and set IDENTIFIER_MARKED. */
1377 struct abi_tag_data
1379 tree t;
1380 tree subob;
1381 // error_mark_node to get diagnostics; otherwise collect missing tags here
1382 tree tags;
1385 static tree
1386 find_abi_tags_r (tree *tp, int *walk_subtrees, void *data)
1388 if (!OVERLOAD_TYPE_P (*tp))
1389 return NULL_TREE;
1391 /* walk_tree shouldn't be walking into any subtrees of a RECORD_TYPE
1392 anyway, but let's make sure of it. */
1393 *walk_subtrees = false;
1395 if (tree attributes = lookup_attribute ("abi_tag", TYPE_ATTRIBUTES (*tp)))
1397 struct abi_tag_data *p = static_cast<struct abi_tag_data*>(data);
1398 for (tree list = TREE_VALUE (attributes); list;
1399 list = TREE_CHAIN (list))
1401 tree tag = TREE_VALUE (list);
1402 tree id = get_identifier (TREE_STRING_POINTER (tag));
1403 if (!IDENTIFIER_MARKED (id))
1405 if (p->tags != error_mark_node)
1407 /* We're collecting tags from template arguments. */
1408 tree str = build_string (IDENTIFIER_LENGTH (id),
1409 IDENTIFIER_POINTER (id));
1410 p->tags = tree_cons (NULL_TREE, str, p->tags);
1411 ABI_TAG_IMPLICIT (p->tags) = true;
1413 /* Don't inherit this tag multiple times. */
1414 IDENTIFIER_MARKED (id) = true;
1417 /* Otherwise we're diagnosing missing tags. */
1418 else if (TYPE_P (p->subob))
1420 if (warning (OPT_Wabi_tag, "%qT does not have the %E abi tag "
1421 "that base %qT has", p->t, tag, p->subob))
1422 inform (location_of (p->subob), "%qT declared here",
1423 p->subob);
1425 else
1427 if (warning (OPT_Wabi_tag, "%qT does not have the %E abi tag "
1428 "that %qT (used in the type of %qD) has",
1429 p->t, tag, *tp, p->subob))
1431 inform (location_of (p->subob), "%qD declared here",
1432 p->subob);
1433 inform (location_of (*tp), "%qT declared here", *tp);
1439 return NULL_TREE;
1442 /* Set IDENTIFIER_MARKED on all the ABI tags on T and its (transitively
1443 complete) template arguments. */
1445 static void
1446 mark_type_abi_tags (tree t, bool val)
1448 tree attributes = lookup_attribute ("abi_tag", TYPE_ATTRIBUTES (t));
1449 if (attributes)
1451 for (tree list = TREE_VALUE (attributes); list;
1452 list = TREE_CHAIN (list))
1454 tree tag = TREE_VALUE (list);
1455 tree id = get_identifier (TREE_STRING_POINTER (tag));
1456 IDENTIFIER_MARKED (id) = val;
1461 /* Check that class T has all the abi tags that subobject SUBOB has, or
1462 warn if not. */
1464 static void
1465 check_abi_tags (tree t, tree subob)
1467 mark_type_abi_tags (t, true);
1469 tree subtype = TYPE_P (subob) ? subob : TREE_TYPE (subob);
1470 struct abi_tag_data data = { t, subob, error_mark_node };
1472 cp_walk_tree_without_duplicates (&subtype, find_abi_tags_r, &data);
1474 mark_type_abi_tags (t, false);
1477 void
1478 inherit_targ_abi_tags (tree t)
1480 if (CLASSTYPE_TEMPLATE_INFO (t) == NULL_TREE)
1481 return;
1483 mark_type_abi_tags (t, true);
1485 tree args = CLASSTYPE_TI_ARGS (t);
1486 struct abi_tag_data data = { t, NULL_TREE, NULL_TREE };
1487 for (int i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
1489 tree level = TMPL_ARGS_LEVEL (args, i+1);
1490 for (int j = 0; j < TREE_VEC_LENGTH (level); ++j)
1492 tree arg = TREE_VEC_ELT (level, j);
1493 data.subob = arg;
1494 cp_walk_tree_without_duplicates (&arg, find_abi_tags_r, &data);
1498 // If we found some tags on our template arguments, add them to our
1499 // abi_tag attribute.
1500 if (data.tags)
1502 tree attr = lookup_attribute ("abi_tag", TYPE_ATTRIBUTES (t));
1503 if (attr)
1504 TREE_VALUE (attr) = chainon (data.tags, TREE_VALUE (attr));
1505 else
1506 TYPE_ATTRIBUTES (t)
1507 = tree_cons (get_identifier ("abi_tag"), data.tags,
1508 TYPE_ATTRIBUTES (t));
1511 mark_type_abi_tags (t, false);
1514 /* Return true, iff class T has a non-virtual destructor that is
1515 accessible from outside the class heirarchy (i.e. is public, or
1516 there's a suitable friend. */
1518 static bool
1519 accessible_nvdtor_p (tree t)
1521 tree dtor = CLASSTYPE_DESTRUCTORS (t);
1523 /* An implicitly declared destructor is always public. And,
1524 if it were virtual, we would have created it by now. */
1525 if (!dtor)
1526 return true;
1528 if (DECL_VINDEX (dtor))
1529 return false; /* Virtual */
1531 if (!TREE_PRIVATE (dtor) && !TREE_PROTECTED (dtor))
1532 return true; /* Public */
1534 if (CLASSTYPE_FRIEND_CLASSES (t)
1535 || DECL_FRIENDLIST (TYPE_MAIN_DECL (t)))
1536 return true; /* Has friends */
1538 return false;
1541 /* Run through the base classes of T, updating CANT_HAVE_CONST_CTOR_P,
1542 and NO_CONST_ASN_REF_P. Also set flag bits in T based on
1543 properties of the bases. */
1545 static void
1546 check_bases (tree t,
1547 int* cant_have_const_ctor_p,
1548 int* no_const_asn_ref_p)
1550 int i;
1551 bool seen_non_virtual_nearly_empty_base_p = 0;
1552 int seen_tm_mask = 0;
1553 tree base_binfo;
1554 tree binfo;
1555 tree field = NULL_TREE;
1557 if (!CLASSTYPE_NON_STD_LAYOUT (t))
1558 for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
1559 if (TREE_CODE (field) == FIELD_DECL)
1560 break;
1562 for (binfo = TYPE_BINFO (t), i = 0;
1563 BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
1565 tree basetype = TREE_TYPE (base_binfo);
1567 gcc_assert (COMPLETE_TYPE_P (basetype));
1569 if (CLASSTYPE_FINAL (basetype))
1570 error ("cannot derive from %<final%> base %qT in derived type %qT",
1571 basetype, t);
1573 /* If any base class is non-literal, so is the derived class. */
1574 if (!CLASSTYPE_LITERAL_P (basetype))
1575 CLASSTYPE_LITERAL_P (t) = false;
1577 /* If the base class doesn't have copy constructors or
1578 assignment operators that take const references, then the
1579 derived class cannot have such a member automatically
1580 generated. */
1581 if (TYPE_HAS_COPY_CTOR (basetype)
1582 && ! TYPE_HAS_CONST_COPY_CTOR (basetype))
1583 *cant_have_const_ctor_p = 1;
1584 if (TYPE_HAS_COPY_ASSIGN (basetype)
1585 && !TYPE_HAS_CONST_COPY_ASSIGN (basetype))
1586 *no_const_asn_ref_p = 1;
1588 if (BINFO_VIRTUAL_P (base_binfo))
1589 /* A virtual base does not effect nearly emptiness. */
1591 else if (CLASSTYPE_NEARLY_EMPTY_P (basetype))
1593 if (seen_non_virtual_nearly_empty_base_p)
1594 /* And if there is more than one nearly empty base, then the
1595 derived class is not nearly empty either. */
1596 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
1597 else
1598 /* Remember we've seen one. */
1599 seen_non_virtual_nearly_empty_base_p = 1;
1601 else if (!is_empty_class (basetype))
1602 /* If the base class is not empty or nearly empty, then this
1603 class cannot be nearly empty. */
1604 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
1606 /* A lot of properties from the bases also apply to the derived
1607 class. */
1608 TYPE_NEEDS_CONSTRUCTING (t) |= TYPE_NEEDS_CONSTRUCTING (basetype);
1609 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t)
1610 |= TYPE_HAS_NONTRIVIAL_DESTRUCTOR (basetype);
1611 TYPE_HAS_COMPLEX_COPY_ASSIGN (t)
1612 |= (TYPE_HAS_COMPLEX_COPY_ASSIGN (basetype)
1613 || !TYPE_HAS_COPY_ASSIGN (basetype));
1614 TYPE_HAS_COMPLEX_COPY_CTOR (t) |= (TYPE_HAS_COMPLEX_COPY_CTOR (basetype)
1615 || !TYPE_HAS_COPY_CTOR (basetype));
1616 TYPE_HAS_COMPLEX_MOVE_ASSIGN (t)
1617 |= TYPE_HAS_COMPLEX_MOVE_ASSIGN (basetype);
1618 TYPE_HAS_COMPLEX_MOVE_CTOR (t) |= TYPE_HAS_COMPLEX_MOVE_CTOR (basetype);
1619 TYPE_POLYMORPHIC_P (t) |= TYPE_POLYMORPHIC_P (basetype);
1620 CLASSTYPE_CONTAINS_EMPTY_CLASS_P (t)
1621 |= CLASSTYPE_CONTAINS_EMPTY_CLASS_P (basetype);
1622 TYPE_HAS_COMPLEX_DFLT (t) |= (!TYPE_HAS_DEFAULT_CONSTRUCTOR (basetype)
1623 || TYPE_HAS_COMPLEX_DFLT (basetype));
1624 SET_CLASSTYPE_READONLY_FIELDS_NEED_INIT
1625 (t, CLASSTYPE_READONLY_FIELDS_NEED_INIT (t)
1626 | CLASSTYPE_READONLY_FIELDS_NEED_INIT (basetype));
1627 SET_CLASSTYPE_REF_FIELDS_NEED_INIT
1628 (t, CLASSTYPE_REF_FIELDS_NEED_INIT (t)
1629 | CLASSTYPE_REF_FIELDS_NEED_INIT (basetype));
1631 /* A standard-layout class is a class that:
1633 * has no non-standard-layout base classes, */
1634 CLASSTYPE_NON_STD_LAYOUT (t) |= CLASSTYPE_NON_STD_LAYOUT (basetype);
1635 if (!CLASSTYPE_NON_STD_LAYOUT (t))
1637 tree basefield;
1638 /* ...has no base classes of the same type as the first non-static
1639 data member... */
1640 if (field && DECL_CONTEXT (field) == t
1641 && (same_type_ignoring_top_level_qualifiers_p
1642 (TREE_TYPE (field), basetype)))
1643 CLASSTYPE_NON_STD_LAYOUT (t) = 1;
1644 else
1645 /* ...either has no non-static data members in the most-derived
1646 class and at most one base class with non-static data
1647 members, or has no base classes with non-static data
1648 members */
1649 for (basefield = TYPE_FIELDS (basetype); basefield;
1650 basefield = DECL_CHAIN (basefield))
1651 if (TREE_CODE (basefield) == FIELD_DECL)
1653 if (field)
1654 CLASSTYPE_NON_STD_LAYOUT (t) = 1;
1655 else
1656 field = basefield;
1657 break;
1661 /* Don't bother collecting tm attributes if transactional memory
1662 support is not enabled. */
1663 if (flag_tm)
1665 tree tm_attr = find_tm_attribute (TYPE_ATTRIBUTES (basetype));
1666 if (tm_attr)
1667 seen_tm_mask |= tm_attr_to_mask (tm_attr);
1670 check_abi_tags (t, basetype);
1673 /* If one of the base classes had TM attributes, and the current class
1674 doesn't define its own, then the current class inherits one. */
1675 if (seen_tm_mask && !find_tm_attribute (TYPE_ATTRIBUTES (t)))
1677 tree tm_attr = tm_mask_to_attr (seen_tm_mask & -seen_tm_mask);
1678 TYPE_ATTRIBUTES (t) = tree_cons (tm_attr, NULL, TYPE_ATTRIBUTES (t));
1682 /* Determine all the primary bases within T. Sets BINFO_PRIMARY_BASE_P for
1683 those that are primaries. Sets BINFO_LOST_PRIMARY_P for those
1684 that have had a nearly-empty virtual primary base stolen by some
1685 other base in the hierarchy. Determines CLASSTYPE_PRIMARY_BASE for
1686 T. */
1688 static void
1689 determine_primary_bases (tree t)
1691 unsigned i;
1692 tree primary = NULL_TREE;
1693 tree type_binfo = TYPE_BINFO (t);
1694 tree base_binfo;
1696 /* Determine the primary bases of our bases. */
1697 for (base_binfo = TREE_CHAIN (type_binfo); base_binfo;
1698 base_binfo = TREE_CHAIN (base_binfo))
1700 tree primary = CLASSTYPE_PRIMARY_BINFO (BINFO_TYPE (base_binfo));
1702 /* See if we're the non-virtual primary of our inheritance
1703 chain. */
1704 if (!BINFO_VIRTUAL_P (base_binfo))
1706 tree parent = BINFO_INHERITANCE_CHAIN (base_binfo);
1707 tree parent_primary = CLASSTYPE_PRIMARY_BINFO (BINFO_TYPE (parent));
1709 if (parent_primary
1710 && SAME_BINFO_TYPE_P (BINFO_TYPE (base_binfo),
1711 BINFO_TYPE (parent_primary)))
1712 /* We are the primary binfo. */
1713 BINFO_PRIMARY_P (base_binfo) = 1;
1715 /* Determine if we have a virtual primary base, and mark it so.
1717 if (primary && BINFO_VIRTUAL_P (primary))
1719 tree this_primary = copied_binfo (primary, base_binfo);
1721 if (BINFO_PRIMARY_P (this_primary))
1722 /* Someone already claimed this base. */
1723 BINFO_LOST_PRIMARY_P (base_binfo) = 1;
1724 else
1726 tree delta;
1728 BINFO_PRIMARY_P (this_primary) = 1;
1729 BINFO_INHERITANCE_CHAIN (this_primary) = base_binfo;
1731 /* A virtual binfo might have been copied from within
1732 another hierarchy. As we're about to use it as a
1733 primary base, make sure the offsets match. */
1734 delta = size_diffop_loc (input_location,
1735 convert (ssizetype,
1736 BINFO_OFFSET (base_binfo)),
1737 convert (ssizetype,
1738 BINFO_OFFSET (this_primary)));
1740 propagate_binfo_offsets (this_primary, delta);
1745 /* First look for a dynamic direct non-virtual base. */
1746 for (i = 0; BINFO_BASE_ITERATE (type_binfo, i, base_binfo); i++)
1748 tree basetype = BINFO_TYPE (base_binfo);
1750 if (TYPE_CONTAINS_VPTR_P (basetype) && !BINFO_VIRTUAL_P (base_binfo))
1752 primary = base_binfo;
1753 goto found;
1757 /* A "nearly-empty" virtual base class can be the primary base
1758 class, if no non-virtual polymorphic base can be found. Look for
1759 a nearly-empty virtual dynamic base that is not already a primary
1760 base of something in the hierarchy. If there is no such base,
1761 just pick the first nearly-empty virtual base. */
1763 for (base_binfo = TREE_CHAIN (type_binfo); base_binfo;
1764 base_binfo = TREE_CHAIN (base_binfo))
1765 if (BINFO_VIRTUAL_P (base_binfo)
1766 && CLASSTYPE_NEARLY_EMPTY_P (BINFO_TYPE (base_binfo)))
1768 if (!BINFO_PRIMARY_P (base_binfo))
1770 /* Found one that is not primary. */
1771 primary = base_binfo;
1772 goto found;
1774 else if (!primary)
1775 /* Remember the first candidate. */
1776 primary = base_binfo;
1779 found:
1780 /* If we've got a primary base, use it. */
1781 if (primary)
1783 tree basetype = BINFO_TYPE (primary);
1785 CLASSTYPE_PRIMARY_BINFO (t) = primary;
1786 if (BINFO_PRIMARY_P (primary))
1787 /* We are stealing a primary base. */
1788 BINFO_LOST_PRIMARY_P (BINFO_INHERITANCE_CHAIN (primary)) = 1;
1789 BINFO_PRIMARY_P (primary) = 1;
1790 if (BINFO_VIRTUAL_P (primary))
1792 tree delta;
1794 BINFO_INHERITANCE_CHAIN (primary) = type_binfo;
1795 /* A virtual binfo might have been copied from within
1796 another hierarchy. As we're about to use it as a primary
1797 base, make sure the offsets match. */
1798 delta = size_diffop_loc (input_location, ssize_int (0),
1799 convert (ssizetype, BINFO_OFFSET (primary)));
1801 propagate_binfo_offsets (primary, delta);
1804 primary = TYPE_BINFO (basetype);
1806 TYPE_VFIELD (t) = TYPE_VFIELD (basetype);
1807 BINFO_VTABLE (type_binfo) = BINFO_VTABLE (primary);
1808 BINFO_VIRTUALS (type_binfo) = BINFO_VIRTUALS (primary);
1812 /* Update the variant types of T. */
1814 void
1815 fixup_type_variants (tree t)
1817 tree variants;
1819 if (!t)
1820 return;
1822 for (variants = TYPE_NEXT_VARIANT (t);
1823 variants;
1824 variants = TYPE_NEXT_VARIANT (variants))
1826 /* These fields are in the _TYPE part of the node, not in
1827 the TYPE_LANG_SPECIFIC component, so they are not shared. */
1828 TYPE_HAS_USER_CONSTRUCTOR (variants) = TYPE_HAS_USER_CONSTRUCTOR (t);
1829 TYPE_NEEDS_CONSTRUCTING (variants) = TYPE_NEEDS_CONSTRUCTING (t);
1830 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (variants)
1831 = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t);
1833 TYPE_POLYMORPHIC_P (variants) = TYPE_POLYMORPHIC_P (t);
1835 TYPE_BINFO (variants) = TYPE_BINFO (t);
1837 /* Copy whatever these are holding today. */
1838 TYPE_VFIELD (variants) = TYPE_VFIELD (t);
1839 TYPE_METHODS (variants) = TYPE_METHODS (t);
1840 TYPE_FIELDS (variants) = TYPE_FIELDS (t);
1844 /* Early variant fixups: we apply attributes at the beginning of the class
1845 definition, and we need to fix up any variants that have already been
1846 made via elaborated-type-specifier so that check_qualified_type works. */
1848 void
1849 fixup_attribute_variants (tree t)
1851 tree variants;
1853 if (!t)
1854 return;
1856 for (variants = TYPE_NEXT_VARIANT (t);
1857 variants;
1858 variants = TYPE_NEXT_VARIANT (variants))
1860 /* These are the two fields that check_qualified_type looks at and
1861 are affected by attributes. */
1862 TYPE_ATTRIBUTES (variants) = TYPE_ATTRIBUTES (t);
1863 TYPE_ALIGN (variants) = TYPE_ALIGN (t);
1867 /* Set memoizing fields and bits of T (and its variants) for later
1868 use. */
1870 static void
1871 finish_struct_bits (tree t)
1873 /* Fix up variants (if any). */
1874 fixup_type_variants (t);
1876 if (BINFO_N_BASE_BINFOS (TYPE_BINFO (t)) && TYPE_POLYMORPHIC_P (t))
1877 /* For a class w/o baseclasses, 'finish_struct' has set
1878 CLASSTYPE_PURE_VIRTUALS correctly (by definition).
1879 Similarly for a class whose base classes do not have vtables.
1880 When neither of these is true, we might have removed abstract
1881 virtuals (by providing a definition), added some (by declaring
1882 new ones), or redeclared ones from a base class. We need to
1883 recalculate what's really an abstract virtual at this point (by
1884 looking in the vtables). */
1885 get_pure_virtuals (t);
1887 /* If this type has a copy constructor or a destructor, force its
1888 mode to be BLKmode, and force its TREE_ADDRESSABLE bit to be
1889 nonzero. This will cause it to be passed by invisible reference
1890 and prevent it from being returned in a register. */
1891 if (type_has_nontrivial_copy_init (t)
1892 || TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t))
1894 tree variants;
1895 DECL_MODE (TYPE_MAIN_DECL (t)) = BLKmode;
1896 for (variants = t; variants; variants = TYPE_NEXT_VARIANT (variants))
1898 SET_TYPE_MODE (variants, BLKmode);
1899 TREE_ADDRESSABLE (variants) = 1;
1904 /* Issue warnings about T having private constructors, but no friends,
1905 and so forth.
1907 HAS_NONPRIVATE_METHOD is nonzero if T has any non-private methods or
1908 static members. HAS_NONPRIVATE_STATIC_FN is nonzero if T has any
1909 non-private static member functions. */
1911 static void
1912 maybe_warn_about_overly_private_class (tree t)
1914 int has_member_fn = 0;
1915 int has_nonprivate_method = 0;
1916 tree fn;
1918 if (!warn_ctor_dtor_privacy
1919 /* If the class has friends, those entities might create and
1920 access instances, so we should not warn. */
1921 || (CLASSTYPE_FRIEND_CLASSES (t)
1922 || DECL_FRIENDLIST (TYPE_MAIN_DECL (t)))
1923 /* We will have warned when the template was declared; there's
1924 no need to warn on every instantiation. */
1925 || CLASSTYPE_TEMPLATE_INSTANTIATION (t))
1926 /* There's no reason to even consider warning about this
1927 class. */
1928 return;
1930 /* We only issue one warning, if more than one applies, because
1931 otherwise, on code like:
1933 class A {
1934 // Oops - forgot `public:'
1935 A();
1936 A(const A&);
1937 ~A();
1940 we warn several times about essentially the same problem. */
1942 /* Check to see if all (non-constructor, non-destructor) member
1943 functions are private. (Since there are no friends or
1944 non-private statics, we can't ever call any of the private member
1945 functions.) */
1946 for (fn = TYPE_METHODS (t); fn; fn = DECL_CHAIN (fn))
1947 /* We're not interested in compiler-generated methods; they don't
1948 provide any way to call private members. */
1949 if (!DECL_ARTIFICIAL (fn))
1951 if (!TREE_PRIVATE (fn))
1953 if (DECL_STATIC_FUNCTION_P (fn))
1954 /* A non-private static member function is just like a
1955 friend; it can create and invoke private member
1956 functions, and be accessed without a class
1957 instance. */
1958 return;
1960 has_nonprivate_method = 1;
1961 /* Keep searching for a static member function. */
1963 else if (!DECL_CONSTRUCTOR_P (fn) && !DECL_DESTRUCTOR_P (fn))
1964 has_member_fn = 1;
1967 if (!has_nonprivate_method && has_member_fn)
1969 /* There are no non-private methods, and there's at least one
1970 private member function that isn't a constructor or
1971 destructor. (If all the private members are
1972 constructors/destructors we want to use the code below that
1973 issues error messages specifically referring to
1974 constructors/destructors.) */
1975 unsigned i;
1976 tree binfo = TYPE_BINFO (t);
1978 for (i = 0; i != BINFO_N_BASE_BINFOS (binfo); i++)
1979 if (BINFO_BASE_ACCESS (binfo, i) != access_private_node)
1981 has_nonprivate_method = 1;
1982 break;
1984 if (!has_nonprivate_method)
1986 warning (OPT_Wctor_dtor_privacy,
1987 "all member functions in class %qT are private", t);
1988 return;
1992 /* Even if some of the member functions are non-private, the class
1993 won't be useful for much if all the constructors or destructors
1994 are private: such an object can never be created or destroyed. */
1995 fn = CLASSTYPE_DESTRUCTORS (t);
1996 if (fn && TREE_PRIVATE (fn))
1998 warning (OPT_Wctor_dtor_privacy,
1999 "%q#T only defines a private destructor and has no friends",
2001 return;
2004 /* Warn about classes that have private constructors and no friends. */
2005 if (TYPE_HAS_USER_CONSTRUCTOR (t)
2006 /* Implicitly generated constructors are always public. */
2007 && (!CLASSTYPE_LAZY_DEFAULT_CTOR (t)
2008 || !CLASSTYPE_LAZY_COPY_CTOR (t)))
2010 int nonprivate_ctor = 0;
2012 /* If a non-template class does not define a copy
2013 constructor, one is defined for it, enabling it to avoid
2014 this warning. For a template class, this does not
2015 happen, and so we would normally get a warning on:
2017 template <class T> class C { private: C(); };
2019 To avoid this asymmetry, we check TYPE_HAS_COPY_CTOR. All
2020 complete non-template or fully instantiated classes have this
2021 flag set. */
2022 if (!TYPE_HAS_COPY_CTOR (t))
2023 nonprivate_ctor = 1;
2024 else
2025 for (fn = CLASSTYPE_CONSTRUCTORS (t); fn; fn = OVL_NEXT (fn))
2027 tree ctor = OVL_CURRENT (fn);
2028 /* Ideally, we wouldn't count copy constructors (or, in
2029 fact, any constructor that takes an argument of the
2030 class type as a parameter) because such things cannot
2031 be used to construct an instance of the class unless
2032 you already have one. But, for now at least, we're
2033 more generous. */
2034 if (! TREE_PRIVATE (ctor))
2036 nonprivate_ctor = 1;
2037 break;
2041 if (nonprivate_ctor == 0)
2043 warning (OPT_Wctor_dtor_privacy,
2044 "%q#T only defines private constructors and has no friends",
2046 return;
2051 static struct {
2052 gt_pointer_operator new_value;
2053 void *cookie;
2054 } resort_data;
2056 /* Comparison function to compare two TYPE_METHOD_VEC entries by name. */
2058 static int
2059 method_name_cmp (const void* m1_p, const void* m2_p)
2061 const tree *const m1 = (const tree *) m1_p;
2062 const tree *const m2 = (const tree *) m2_p;
2064 if (*m1 == NULL_TREE && *m2 == NULL_TREE)
2065 return 0;
2066 if (*m1 == NULL_TREE)
2067 return -1;
2068 if (*m2 == NULL_TREE)
2069 return 1;
2070 if (DECL_NAME (OVL_CURRENT (*m1)) < DECL_NAME (OVL_CURRENT (*m2)))
2071 return -1;
2072 return 1;
2075 /* This routine compares two fields like method_name_cmp but using the
2076 pointer operator in resort_field_decl_data. */
2078 static int
2079 resort_method_name_cmp (const void* m1_p, const void* m2_p)
2081 const tree *const m1 = (const tree *) m1_p;
2082 const tree *const m2 = (const tree *) m2_p;
2083 if (*m1 == NULL_TREE && *m2 == NULL_TREE)
2084 return 0;
2085 if (*m1 == NULL_TREE)
2086 return -1;
2087 if (*m2 == NULL_TREE)
2088 return 1;
2090 tree d1 = DECL_NAME (OVL_CURRENT (*m1));
2091 tree d2 = DECL_NAME (OVL_CURRENT (*m2));
2092 resort_data.new_value (&d1, resort_data.cookie);
2093 resort_data.new_value (&d2, resort_data.cookie);
2094 if (d1 < d2)
2095 return -1;
2097 return 1;
2100 /* Resort TYPE_METHOD_VEC because pointers have been reordered. */
2102 void
2103 resort_type_method_vec (void* obj,
2104 void* /*orig_obj*/,
2105 gt_pointer_operator new_value,
2106 void* cookie)
2108 vec<tree, va_gc> *method_vec = (vec<tree, va_gc> *) obj;
2109 int len = vec_safe_length (method_vec);
2110 size_t slot;
2111 tree fn;
2113 /* The type conversion ops have to live at the front of the vec, so we
2114 can't sort them. */
2115 for (slot = CLASSTYPE_FIRST_CONVERSION_SLOT;
2116 vec_safe_iterate (method_vec, slot, &fn);
2117 ++slot)
2118 if (!DECL_CONV_FN_P (OVL_CURRENT (fn)))
2119 break;
2121 if (len - slot > 1)
2123 resort_data.new_value = new_value;
2124 resort_data.cookie = cookie;
2125 qsort (method_vec->address () + slot, len - slot, sizeof (tree),
2126 resort_method_name_cmp);
2130 /* Warn about duplicate methods in fn_fields.
2132 Sort methods that are not special (i.e., constructors, destructors,
2133 and type conversion operators) so that we can find them faster in
2134 search. */
2136 static void
2137 finish_struct_methods (tree t)
2139 tree fn_fields;
2140 vec<tree, va_gc> *method_vec;
2141 int slot, len;
2143 method_vec = CLASSTYPE_METHOD_VEC (t);
2144 if (!method_vec)
2145 return;
2147 len = method_vec->length ();
2149 /* Clear DECL_IN_AGGR_P for all functions. */
2150 for (fn_fields = TYPE_METHODS (t); fn_fields;
2151 fn_fields = DECL_CHAIN (fn_fields))
2152 DECL_IN_AGGR_P (fn_fields) = 0;
2154 /* Issue warnings about private constructors and such. If there are
2155 no methods, then some public defaults are generated. */
2156 maybe_warn_about_overly_private_class (t);
2158 /* The type conversion ops have to live at the front of the vec, so we
2159 can't sort them. */
2160 for (slot = CLASSTYPE_FIRST_CONVERSION_SLOT;
2161 method_vec->iterate (slot, &fn_fields);
2162 ++slot)
2163 if (!DECL_CONV_FN_P (OVL_CURRENT (fn_fields)))
2164 break;
2165 if (len - slot > 1)
2166 qsort (method_vec->address () + slot,
2167 len-slot, sizeof (tree), method_name_cmp);
2170 /* Make BINFO's vtable have N entries, including RTTI entries,
2171 vbase and vcall offsets, etc. Set its type and call the back end
2172 to lay it out. */
2174 static void
2175 layout_vtable_decl (tree binfo, int n)
2177 tree atype;
2178 tree vtable;
2180 atype = build_array_of_n_type (vtable_entry_type, n);
2181 layout_type (atype);
2183 /* We may have to grow the vtable. */
2184 vtable = get_vtbl_decl_for_binfo (binfo);
2185 if (!same_type_p (TREE_TYPE (vtable), atype))
2187 TREE_TYPE (vtable) = atype;
2188 DECL_SIZE (vtable) = DECL_SIZE_UNIT (vtable) = NULL_TREE;
2189 layout_decl (vtable, 0);
2193 /* True iff FNDECL and BASE_FNDECL (both non-static member functions)
2194 have the same signature. */
2197 same_signature_p (const_tree fndecl, const_tree base_fndecl)
2199 /* One destructor overrides another if they are the same kind of
2200 destructor. */
2201 if (DECL_DESTRUCTOR_P (base_fndecl) && DECL_DESTRUCTOR_P (fndecl)
2202 && special_function_p (base_fndecl) == special_function_p (fndecl))
2203 return 1;
2204 /* But a non-destructor never overrides a destructor, nor vice
2205 versa, nor do different kinds of destructors override
2206 one-another. For example, a complete object destructor does not
2207 override a deleting destructor. */
2208 if (DECL_DESTRUCTOR_P (base_fndecl) || DECL_DESTRUCTOR_P (fndecl))
2209 return 0;
2211 if (DECL_NAME (fndecl) == DECL_NAME (base_fndecl)
2212 || (DECL_CONV_FN_P (fndecl)
2213 && DECL_CONV_FN_P (base_fndecl)
2214 && same_type_p (DECL_CONV_FN_TYPE (fndecl),
2215 DECL_CONV_FN_TYPE (base_fndecl))))
2217 tree fntype = TREE_TYPE (fndecl);
2218 tree base_fntype = TREE_TYPE (base_fndecl);
2219 if (type_memfn_quals (fntype) == type_memfn_quals (base_fntype)
2220 && type_memfn_rqual (fntype) == type_memfn_rqual (base_fntype)
2221 && compparms (FUNCTION_FIRST_USER_PARMTYPE (fndecl),
2222 FUNCTION_FIRST_USER_PARMTYPE (base_fndecl)))
2223 return 1;
2225 return 0;
2228 /* Returns TRUE if DERIVED is a binfo containing the binfo BASE as a
2229 subobject. */
2231 static bool
2232 base_derived_from (tree derived, tree base)
2234 tree probe;
2236 for (probe = base; probe; probe = BINFO_INHERITANCE_CHAIN (probe))
2238 if (probe == derived)
2239 return true;
2240 else if (BINFO_VIRTUAL_P (probe))
2241 /* If we meet a virtual base, we can't follow the inheritance
2242 any more. See if the complete type of DERIVED contains
2243 such a virtual base. */
2244 return (binfo_for_vbase (BINFO_TYPE (probe), BINFO_TYPE (derived))
2245 != NULL_TREE);
2247 return false;
2250 typedef struct find_final_overrider_data_s {
2251 /* The function for which we are trying to find a final overrider. */
2252 tree fn;
2253 /* The base class in which the function was declared. */
2254 tree declaring_base;
2255 /* The candidate overriders. */
2256 tree candidates;
2257 /* Path to most derived. */
2258 vec<tree> path;
2259 } find_final_overrider_data;
2261 /* Add the overrider along the current path to FFOD->CANDIDATES.
2262 Returns true if an overrider was found; false otherwise. */
2264 static bool
2265 dfs_find_final_overrider_1 (tree binfo,
2266 find_final_overrider_data *ffod,
2267 unsigned depth)
2269 tree method;
2271 /* If BINFO is not the most derived type, try a more derived class.
2272 A definition there will overrider a definition here. */
2273 if (depth)
2275 depth--;
2276 if (dfs_find_final_overrider_1
2277 (ffod->path[depth], ffod, depth))
2278 return true;
2281 method = look_for_overrides_here (BINFO_TYPE (binfo), ffod->fn);
2282 if (method)
2284 tree *candidate = &ffod->candidates;
2286 /* Remove any candidates overridden by this new function. */
2287 while (*candidate)
2289 /* If *CANDIDATE overrides METHOD, then METHOD
2290 cannot override anything else on the list. */
2291 if (base_derived_from (TREE_VALUE (*candidate), binfo))
2292 return true;
2293 /* If METHOD overrides *CANDIDATE, remove *CANDIDATE. */
2294 if (base_derived_from (binfo, TREE_VALUE (*candidate)))
2295 *candidate = TREE_CHAIN (*candidate);
2296 else
2297 candidate = &TREE_CHAIN (*candidate);
2300 /* Add the new function. */
2301 ffod->candidates = tree_cons (method, binfo, ffod->candidates);
2302 return true;
2305 return false;
2308 /* Called from find_final_overrider via dfs_walk. */
2310 static tree
2311 dfs_find_final_overrider_pre (tree binfo, void *data)
2313 find_final_overrider_data *ffod = (find_final_overrider_data *) data;
2315 if (binfo == ffod->declaring_base)
2316 dfs_find_final_overrider_1 (binfo, ffod, ffod->path.length ());
2317 ffod->path.safe_push (binfo);
2319 return NULL_TREE;
2322 static tree
2323 dfs_find_final_overrider_post (tree /*binfo*/, void *data)
2325 find_final_overrider_data *ffod = (find_final_overrider_data *) data;
2326 ffod->path.pop ();
2328 return NULL_TREE;
2331 /* Returns a TREE_LIST whose TREE_PURPOSE is the final overrider for
2332 FN and whose TREE_VALUE is the binfo for the base where the
2333 overriding occurs. BINFO (in the hierarchy dominated by the binfo
2334 DERIVED) is the base object in which FN is declared. */
2336 static tree
2337 find_final_overrider (tree derived, tree binfo, tree fn)
2339 find_final_overrider_data ffod;
2341 /* Getting this right is a little tricky. This is valid:
2343 struct S { virtual void f (); };
2344 struct T { virtual void f (); };
2345 struct U : public S, public T { };
2347 even though calling `f' in `U' is ambiguous. But,
2349 struct R { virtual void f(); };
2350 struct S : virtual public R { virtual void f (); };
2351 struct T : virtual public R { virtual void f (); };
2352 struct U : public S, public T { };
2354 is not -- there's no way to decide whether to put `S::f' or
2355 `T::f' in the vtable for `R'.
2357 The solution is to look at all paths to BINFO. If we find
2358 different overriders along any two, then there is a problem. */
2359 if (DECL_THUNK_P (fn))
2360 fn = THUNK_TARGET (fn);
2362 /* Determine the depth of the hierarchy. */
2363 ffod.fn = fn;
2364 ffod.declaring_base = binfo;
2365 ffod.candidates = NULL_TREE;
2366 ffod.path.create (30);
2368 dfs_walk_all (derived, dfs_find_final_overrider_pre,
2369 dfs_find_final_overrider_post, &ffod);
2371 ffod.path.release ();
2373 /* If there was no winner, issue an error message. */
2374 if (!ffod.candidates || TREE_CHAIN (ffod.candidates))
2375 return error_mark_node;
2377 return ffod.candidates;
2380 /* Return the index of the vcall offset for FN when TYPE is used as a
2381 virtual base. */
2383 static tree
2384 get_vcall_index (tree fn, tree type)
2386 vec<tree_pair_s, va_gc> *indices = CLASSTYPE_VCALL_INDICES (type);
2387 tree_pair_p p;
2388 unsigned ix;
2390 FOR_EACH_VEC_SAFE_ELT (indices, ix, p)
2391 if ((DECL_DESTRUCTOR_P (fn) && DECL_DESTRUCTOR_P (p->purpose))
2392 || same_signature_p (fn, p->purpose))
2393 return p->value;
2395 /* There should always be an appropriate index. */
2396 gcc_unreachable ();
2399 /* Update an entry in the vtable for BINFO, which is in the hierarchy
2400 dominated by T. FN is the old function; VIRTUALS points to the
2401 corresponding position in the new BINFO_VIRTUALS list. IX is the index
2402 of that entry in the list. */
2404 static void
2405 update_vtable_entry_for_fn (tree t, tree binfo, tree fn, tree* virtuals,
2406 unsigned ix)
2408 tree b;
2409 tree overrider;
2410 tree delta;
2411 tree virtual_base;
2412 tree first_defn;
2413 tree overrider_fn, overrider_target;
2414 tree target_fn = DECL_THUNK_P (fn) ? THUNK_TARGET (fn) : fn;
2415 tree over_return, base_return;
2416 bool lost = false;
2418 /* Find the nearest primary base (possibly binfo itself) which defines
2419 this function; this is the class the caller will convert to when
2420 calling FN through BINFO. */
2421 for (b = binfo; ; b = get_primary_binfo (b))
2423 gcc_assert (b);
2424 if (look_for_overrides_here (BINFO_TYPE (b), target_fn))
2425 break;
2427 /* The nearest definition is from a lost primary. */
2428 if (BINFO_LOST_PRIMARY_P (b))
2429 lost = true;
2431 first_defn = b;
2433 /* Find the final overrider. */
2434 overrider = find_final_overrider (TYPE_BINFO (t), b, target_fn);
2435 if (overrider == error_mark_node)
2437 error ("no unique final overrider for %qD in %qT", target_fn, t);
2438 return;
2440 overrider_target = overrider_fn = TREE_PURPOSE (overrider);
2442 /* Check for adjusting covariant return types. */
2443 over_return = TREE_TYPE (TREE_TYPE (overrider_target));
2444 base_return = TREE_TYPE (TREE_TYPE (target_fn));
2446 if (POINTER_TYPE_P (over_return)
2447 && TREE_CODE (over_return) == TREE_CODE (base_return)
2448 && CLASS_TYPE_P (TREE_TYPE (over_return))
2449 && CLASS_TYPE_P (TREE_TYPE (base_return))
2450 /* If the overrider is invalid, don't even try. */
2451 && !DECL_INVALID_OVERRIDER_P (overrider_target))
2453 /* If FN is a covariant thunk, we must figure out the adjustment
2454 to the final base FN was converting to. As OVERRIDER_TARGET might
2455 also be converting to the return type of FN, we have to
2456 combine the two conversions here. */
2457 tree fixed_offset, virtual_offset;
2459 over_return = TREE_TYPE (over_return);
2460 base_return = TREE_TYPE (base_return);
2462 if (DECL_THUNK_P (fn))
2464 gcc_assert (DECL_RESULT_THUNK_P (fn));
2465 fixed_offset = ssize_int (THUNK_FIXED_OFFSET (fn));
2466 virtual_offset = THUNK_VIRTUAL_OFFSET (fn);
2468 else
2469 fixed_offset = virtual_offset = NULL_TREE;
2471 if (virtual_offset)
2472 /* Find the equivalent binfo within the return type of the
2473 overriding function. We will want the vbase offset from
2474 there. */
2475 virtual_offset = binfo_for_vbase (BINFO_TYPE (virtual_offset),
2476 over_return);
2477 else if (!same_type_ignoring_top_level_qualifiers_p
2478 (over_return, base_return))
2480 /* There was no existing virtual thunk (which takes
2481 precedence). So find the binfo of the base function's
2482 return type within the overriding function's return type.
2483 We cannot call lookup base here, because we're inside a
2484 dfs_walk, and will therefore clobber the BINFO_MARKED
2485 flags. Fortunately we know the covariancy is valid (it
2486 has already been checked), so we can just iterate along
2487 the binfos, which have been chained in inheritance graph
2488 order. Of course it is lame that we have to repeat the
2489 search here anyway -- we should really be caching pieces
2490 of the vtable and avoiding this repeated work. */
2491 tree thunk_binfo, base_binfo;
2493 /* Find the base binfo within the overriding function's
2494 return type. We will always find a thunk_binfo, except
2495 when the covariancy is invalid (which we will have
2496 already diagnosed). */
2497 for (base_binfo = TYPE_BINFO (base_return),
2498 thunk_binfo = TYPE_BINFO (over_return);
2499 thunk_binfo;
2500 thunk_binfo = TREE_CHAIN (thunk_binfo))
2501 if (SAME_BINFO_TYPE_P (BINFO_TYPE (thunk_binfo),
2502 BINFO_TYPE (base_binfo)))
2503 break;
2505 /* See if virtual inheritance is involved. */
2506 for (virtual_offset = thunk_binfo;
2507 virtual_offset;
2508 virtual_offset = BINFO_INHERITANCE_CHAIN (virtual_offset))
2509 if (BINFO_VIRTUAL_P (virtual_offset))
2510 break;
2512 if (virtual_offset
2513 || (thunk_binfo && !BINFO_OFFSET_ZEROP (thunk_binfo)))
2515 tree offset = convert (ssizetype, BINFO_OFFSET (thunk_binfo));
2517 if (virtual_offset)
2519 /* We convert via virtual base. Adjust the fixed
2520 offset to be from there. */
2521 offset =
2522 size_diffop (offset,
2523 convert (ssizetype,
2524 BINFO_OFFSET (virtual_offset)));
2526 if (fixed_offset)
2527 /* There was an existing fixed offset, this must be
2528 from the base just converted to, and the base the
2529 FN was thunking to. */
2530 fixed_offset = size_binop (PLUS_EXPR, fixed_offset, offset);
2531 else
2532 fixed_offset = offset;
2536 if (fixed_offset || virtual_offset)
2537 /* Replace the overriding function with a covariant thunk. We
2538 will emit the overriding function in its own slot as
2539 well. */
2540 overrider_fn = make_thunk (overrider_target, /*this_adjusting=*/0,
2541 fixed_offset, virtual_offset);
2543 else
2544 gcc_assert (DECL_INVALID_OVERRIDER_P (overrider_target) ||
2545 !DECL_THUNK_P (fn));
2547 /* If we need a covariant thunk, then we may need to adjust first_defn.
2548 The ABI specifies that the thunks emitted with a function are
2549 determined by which bases the function overrides, so we need to be
2550 sure that we're using a thunk for some overridden base; even if we
2551 know that the necessary this adjustment is zero, there may not be an
2552 appropriate zero-this-adjusment thunk for us to use since thunks for
2553 overriding virtual bases always use the vcall offset.
2555 Furthermore, just choosing any base that overrides this function isn't
2556 quite right, as this slot won't be used for calls through a type that
2557 puts a covariant thunk here. Calling the function through such a type
2558 will use a different slot, and that slot is the one that determines
2559 the thunk emitted for that base.
2561 So, keep looking until we find the base that we're really overriding
2562 in this slot: the nearest primary base that doesn't use a covariant
2563 thunk in this slot. */
2564 if (overrider_target != overrider_fn)
2566 if (BINFO_TYPE (b) == DECL_CONTEXT (overrider_target))
2567 /* We already know that the overrider needs a covariant thunk. */
2568 b = get_primary_binfo (b);
2569 for (; ; b = get_primary_binfo (b))
2571 tree main_binfo = TYPE_BINFO (BINFO_TYPE (b));
2572 tree bv = chain_index (ix, BINFO_VIRTUALS (main_binfo));
2573 if (!DECL_THUNK_P (TREE_VALUE (bv)))
2574 break;
2575 if (BINFO_LOST_PRIMARY_P (b))
2576 lost = true;
2578 first_defn = b;
2581 /* Assume that we will produce a thunk that convert all the way to
2582 the final overrider, and not to an intermediate virtual base. */
2583 virtual_base = NULL_TREE;
2585 /* See if we can convert to an intermediate virtual base first, and then
2586 use the vcall offset located there to finish the conversion. */
2587 for (; b; b = BINFO_INHERITANCE_CHAIN (b))
2589 /* If we find the final overrider, then we can stop
2590 walking. */
2591 if (SAME_BINFO_TYPE_P (BINFO_TYPE (b),
2592 BINFO_TYPE (TREE_VALUE (overrider))))
2593 break;
2595 /* If we find a virtual base, and we haven't yet found the
2596 overrider, then there is a virtual base between the
2597 declaring base (first_defn) and the final overrider. */
2598 if (BINFO_VIRTUAL_P (b))
2600 virtual_base = b;
2601 break;
2605 /* Compute the constant adjustment to the `this' pointer. The
2606 `this' pointer, when this function is called, will point at BINFO
2607 (or one of its primary bases, which are at the same offset). */
2608 if (virtual_base)
2609 /* The `this' pointer needs to be adjusted from the declaration to
2610 the nearest virtual base. */
2611 delta = size_diffop_loc (input_location,
2612 convert (ssizetype, BINFO_OFFSET (virtual_base)),
2613 convert (ssizetype, BINFO_OFFSET (first_defn)));
2614 else if (lost)
2615 /* If the nearest definition is in a lost primary, we don't need an
2616 entry in our vtable. Except possibly in a constructor vtable,
2617 if we happen to get our primary back. In that case, the offset
2618 will be zero, as it will be a primary base. */
2619 delta = size_zero_node;
2620 else
2621 /* The `this' pointer needs to be adjusted from pointing to
2622 BINFO to pointing at the base where the final overrider
2623 appears. */
2624 delta = size_diffop_loc (input_location,
2625 convert (ssizetype,
2626 BINFO_OFFSET (TREE_VALUE (overrider))),
2627 convert (ssizetype, BINFO_OFFSET (binfo)));
2629 modify_vtable_entry (t, binfo, overrider_fn, delta, virtuals);
2631 if (virtual_base)
2632 BV_VCALL_INDEX (*virtuals)
2633 = get_vcall_index (overrider_target, BINFO_TYPE (virtual_base));
2634 else
2635 BV_VCALL_INDEX (*virtuals) = NULL_TREE;
2637 BV_LOST_PRIMARY (*virtuals) = lost;
2640 /* Called from modify_all_vtables via dfs_walk. */
2642 static tree
2643 dfs_modify_vtables (tree binfo, void* data)
2645 tree t = (tree) data;
2646 tree virtuals;
2647 tree old_virtuals;
2648 unsigned ix;
2650 if (!TYPE_CONTAINS_VPTR_P (BINFO_TYPE (binfo)))
2651 /* A base without a vtable needs no modification, and its bases
2652 are uninteresting. */
2653 return dfs_skip_bases;
2655 if (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), t)
2656 && !CLASSTYPE_HAS_PRIMARY_BASE_P (t))
2657 /* Don't do the primary vtable, if it's new. */
2658 return NULL_TREE;
2660 if (BINFO_PRIMARY_P (binfo) && !BINFO_VIRTUAL_P (binfo))
2661 /* There's no need to modify the vtable for a non-virtual primary
2662 base; we're not going to use that vtable anyhow. We do still
2663 need to do this for virtual primary bases, as they could become
2664 non-primary in a construction vtable. */
2665 return NULL_TREE;
2667 make_new_vtable (t, binfo);
2669 /* Now, go through each of the virtual functions in the virtual
2670 function table for BINFO. Find the final overrider, and update
2671 the BINFO_VIRTUALS list appropriately. */
2672 for (ix = 0, virtuals = BINFO_VIRTUALS (binfo),
2673 old_virtuals = BINFO_VIRTUALS (TYPE_BINFO (BINFO_TYPE (binfo)));
2674 virtuals;
2675 ix++, virtuals = TREE_CHAIN (virtuals),
2676 old_virtuals = TREE_CHAIN (old_virtuals))
2677 update_vtable_entry_for_fn (t,
2678 binfo,
2679 BV_FN (old_virtuals),
2680 &virtuals, ix);
2682 return NULL_TREE;
2685 /* Update all of the primary and secondary vtables for T. Create new
2686 vtables as required, and initialize their RTTI information. Each
2687 of the functions in VIRTUALS is declared in T and may override a
2688 virtual function from a base class; find and modify the appropriate
2689 entries to point to the overriding functions. Returns a list, in
2690 declaration order, of the virtual functions that are declared in T,
2691 but do not appear in the primary base class vtable, and which
2692 should therefore be appended to the end of the vtable for T. */
2694 static tree
2695 modify_all_vtables (tree t, tree virtuals)
2697 tree binfo = TYPE_BINFO (t);
2698 tree *fnsp;
2700 /* Mangle the vtable name before entering dfs_walk (c++/51884). */
2701 if (TYPE_CONTAINS_VPTR_P (t))
2702 get_vtable_decl (t, false);
2704 /* Update all of the vtables. */
2705 dfs_walk_once (binfo, dfs_modify_vtables, NULL, t);
2707 /* Add virtual functions not already in our primary vtable. These
2708 will be both those introduced by this class, and those overridden
2709 from secondary bases. It does not include virtuals merely
2710 inherited from secondary bases. */
2711 for (fnsp = &virtuals; *fnsp; )
2713 tree fn = TREE_VALUE (*fnsp);
2715 if (!value_member (fn, BINFO_VIRTUALS (binfo))
2716 || DECL_VINDEX (fn) == error_mark_node)
2718 /* We don't need to adjust the `this' pointer when
2719 calling this function. */
2720 BV_DELTA (*fnsp) = integer_zero_node;
2721 BV_VCALL_INDEX (*fnsp) = NULL_TREE;
2723 /* This is a function not already in our vtable. Keep it. */
2724 fnsp = &TREE_CHAIN (*fnsp);
2726 else
2727 /* We've already got an entry for this function. Skip it. */
2728 *fnsp = TREE_CHAIN (*fnsp);
2731 return virtuals;
2734 /* Get the base virtual function declarations in T that have the
2735 indicated NAME. */
2737 static tree
2738 get_basefndecls (tree name, tree t)
2740 tree methods;
2741 tree base_fndecls = NULL_TREE;
2742 int n_baseclasses = BINFO_N_BASE_BINFOS (TYPE_BINFO (t));
2743 int i;
2745 /* Find virtual functions in T with the indicated NAME. */
2746 i = lookup_fnfields_1 (t, name);
2747 if (i != -1)
2748 for (methods = (*CLASSTYPE_METHOD_VEC (t))[i];
2749 methods;
2750 methods = OVL_NEXT (methods))
2752 tree method = OVL_CURRENT (methods);
2754 if (TREE_CODE (method) == FUNCTION_DECL
2755 && DECL_VINDEX (method))
2756 base_fndecls = tree_cons (NULL_TREE, method, base_fndecls);
2759 if (base_fndecls)
2760 return base_fndecls;
2762 for (i = 0; i < n_baseclasses; i++)
2764 tree basetype = BINFO_TYPE (BINFO_BASE_BINFO (TYPE_BINFO (t), i));
2765 base_fndecls = chainon (get_basefndecls (name, basetype),
2766 base_fndecls);
2769 return base_fndecls;
2772 /* If this declaration supersedes the declaration of
2773 a method declared virtual in the base class, then
2774 mark this field as being virtual as well. */
2776 void
2777 check_for_override (tree decl, tree ctype)
2779 bool overrides_found = false;
2780 if (TREE_CODE (decl) == TEMPLATE_DECL)
2781 /* In [temp.mem] we have:
2783 A specialization of a member function template does not
2784 override a virtual function from a base class. */
2785 return;
2786 if ((DECL_DESTRUCTOR_P (decl)
2787 || IDENTIFIER_VIRTUAL_P (DECL_NAME (decl))
2788 || DECL_CONV_FN_P (decl))
2789 && look_for_overrides (ctype, decl)
2790 && !DECL_STATIC_FUNCTION_P (decl))
2791 /* Set DECL_VINDEX to a value that is neither an INTEGER_CST nor
2792 the error_mark_node so that we know it is an overriding
2793 function. */
2795 DECL_VINDEX (decl) = decl;
2796 overrides_found = true;
2799 if (DECL_VIRTUAL_P (decl))
2801 if (!DECL_VINDEX (decl))
2802 DECL_VINDEX (decl) = error_mark_node;
2803 IDENTIFIER_VIRTUAL_P (DECL_NAME (decl)) = 1;
2804 if (DECL_DESTRUCTOR_P (decl))
2805 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (ctype) = true;
2807 else if (DECL_FINAL_P (decl))
2808 error ("%q+#D marked final, but is not virtual", decl);
2809 if (DECL_OVERRIDE_P (decl) && !overrides_found)
2810 error ("%q+#D marked override, but does not override", decl);
2813 /* Warn about hidden virtual functions that are not overridden in t.
2814 We know that constructors and destructors don't apply. */
2816 static void
2817 warn_hidden (tree t)
2819 vec<tree, va_gc> *method_vec = CLASSTYPE_METHOD_VEC (t);
2820 tree fns;
2821 size_t i;
2823 /* We go through each separately named virtual function. */
2824 for (i = CLASSTYPE_FIRST_CONVERSION_SLOT;
2825 vec_safe_iterate (method_vec, i, &fns);
2826 ++i)
2828 tree fn;
2829 tree name;
2830 tree fndecl;
2831 tree base_fndecls;
2832 tree base_binfo;
2833 tree binfo;
2834 int j;
2836 /* All functions in this slot in the CLASSTYPE_METHOD_VEC will
2837 have the same name. Figure out what name that is. */
2838 name = DECL_NAME (OVL_CURRENT (fns));
2839 /* There are no possibly hidden functions yet. */
2840 base_fndecls = NULL_TREE;
2841 /* Iterate through all of the base classes looking for possibly
2842 hidden functions. */
2843 for (binfo = TYPE_BINFO (t), j = 0;
2844 BINFO_BASE_ITERATE (binfo, j, base_binfo); j++)
2846 tree basetype = BINFO_TYPE (base_binfo);
2847 base_fndecls = chainon (get_basefndecls (name, basetype),
2848 base_fndecls);
2851 /* If there are no functions to hide, continue. */
2852 if (!base_fndecls)
2853 continue;
2855 /* Remove any overridden functions. */
2856 for (fn = fns; fn; fn = OVL_NEXT (fn))
2858 fndecl = OVL_CURRENT (fn);
2859 if (DECL_VINDEX (fndecl))
2861 tree *prev = &base_fndecls;
2863 while (*prev)
2864 /* If the method from the base class has the same
2865 signature as the method from the derived class, it
2866 has been overridden. */
2867 if (same_signature_p (fndecl, TREE_VALUE (*prev)))
2868 *prev = TREE_CHAIN (*prev);
2869 else
2870 prev = &TREE_CHAIN (*prev);
2874 /* Now give a warning for all base functions without overriders,
2875 as they are hidden. */
2876 while (base_fndecls)
2878 /* Here we know it is a hider, and no overrider exists. */
2879 warning (OPT_Woverloaded_virtual, "%q+D was hidden", TREE_VALUE (base_fndecls));
2880 warning (OPT_Woverloaded_virtual, " by %q+D", fns);
2881 base_fndecls = TREE_CHAIN (base_fndecls);
2886 /* Recursive helper for finish_struct_anon. */
2888 static void
2889 finish_struct_anon_r (tree field, bool complain)
2891 bool is_union = TREE_CODE (TREE_TYPE (field)) == UNION_TYPE;
2892 tree elt = TYPE_FIELDS (TREE_TYPE (field));
2893 for (; elt; elt = DECL_CHAIN (elt))
2895 /* We're generally only interested in entities the user
2896 declared, but we also find nested classes by noticing
2897 the TYPE_DECL that we create implicitly. You're
2898 allowed to put one anonymous union inside another,
2899 though, so we explicitly tolerate that. We use
2900 TYPE_ANONYMOUS_P rather than ANON_AGGR_TYPE_P so that
2901 we also allow unnamed types used for defining fields. */
2902 if (DECL_ARTIFICIAL (elt)
2903 && (!DECL_IMPLICIT_TYPEDEF_P (elt)
2904 || TYPE_ANONYMOUS_P (TREE_TYPE (elt))))
2905 continue;
2907 if (TREE_CODE (elt) != FIELD_DECL)
2909 /* We already complained about static data members in
2910 finish_static_data_member_decl. */
2911 if (complain && TREE_CODE (elt) != VAR_DECL)
2913 if (is_union)
2914 permerror (input_location,
2915 "%q+#D invalid; an anonymous union can "
2916 "only have non-static data members", elt);
2917 else
2918 permerror (input_location,
2919 "%q+#D invalid; an anonymous struct can "
2920 "only have non-static data members", elt);
2922 continue;
2925 if (complain)
2927 if (TREE_PRIVATE (elt))
2929 if (is_union)
2930 permerror (input_location,
2931 "private member %q+#D in anonymous union", elt);
2932 else
2933 permerror (input_location,
2934 "private member %q+#D in anonymous struct", elt);
2936 else if (TREE_PROTECTED (elt))
2938 if (is_union)
2939 permerror (input_location,
2940 "protected member %q+#D in anonymous union", elt);
2941 else
2942 permerror (input_location,
2943 "protected member %q+#D in anonymous struct", elt);
2947 TREE_PRIVATE (elt) = TREE_PRIVATE (field);
2948 TREE_PROTECTED (elt) = TREE_PROTECTED (field);
2950 /* Recurse into the anonymous aggregates to handle correctly
2951 access control (c++/24926):
2953 class A {
2954 union {
2955 union {
2956 int i;
2961 int j=A().i; */
2962 if (DECL_NAME (elt) == NULL_TREE
2963 && ANON_AGGR_TYPE_P (TREE_TYPE (elt)))
2964 finish_struct_anon_r (elt, /*complain=*/false);
2968 /* Check for things that are invalid. There are probably plenty of other
2969 things we should check for also. */
2971 static void
2972 finish_struct_anon (tree t)
2974 for (tree field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
2976 if (TREE_STATIC (field))
2977 continue;
2978 if (TREE_CODE (field) != FIELD_DECL)
2979 continue;
2981 if (DECL_NAME (field) == NULL_TREE
2982 && ANON_AGGR_TYPE_P (TREE_TYPE (field)))
2983 finish_struct_anon_r (field, /*complain=*/true);
2987 /* Add T to CLASSTYPE_DECL_LIST of current_class_type which
2988 will be used later during class template instantiation.
2989 When FRIEND_P is zero, T can be a static member data (VAR_DECL),
2990 a non-static member data (FIELD_DECL), a member function
2991 (FUNCTION_DECL), a nested type (RECORD_TYPE, ENUM_TYPE),
2992 a typedef (TYPE_DECL) or a member class template (TEMPLATE_DECL)
2993 When FRIEND_P is nonzero, T is either a friend class
2994 (RECORD_TYPE, TEMPLATE_DECL) or a friend function
2995 (FUNCTION_DECL, TEMPLATE_DECL). */
2997 void
2998 maybe_add_class_template_decl_list (tree type, tree t, int friend_p)
3000 /* Save some memory by not creating TREE_LIST if TYPE is not template. */
3001 if (CLASSTYPE_TEMPLATE_INFO (type))
3002 CLASSTYPE_DECL_LIST (type)
3003 = tree_cons (friend_p ? NULL_TREE : type,
3004 t, CLASSTYPE_DECL_LIST (type));
3007 /* This function is called from declare_virt_assop_and_dtor via
3008 dfs_walk_all.
3010 DATA is a type that direcly or indirectly inherits the base
3011 represented by BINFO. If BINFO contains a virtual assignment [copy
3012 assignment or move assigment] operator or a virtual constructor,
3013 declare that function in DATA if it hasn't been already declared. */
3015 static tree
3016 dfs_declare_virt_assop_and_dtor (tree binfo, void *data)
3018 tree bv, fn, t = (tree)data;
3019 tree opname = ansi_assopname (NOP_EXPR);
3021 gcc_assert (t && CLASS_TYPE_P (t));
3022 gcc_assert (binfo && TREE_CODE (binfo) == TREE_BINFO);
3024 if (!TYPE_CONTAINS_VPTR_P (BINFO_TYPE (binfo)))
3025 /* A base without a vtable needs no modification, and its bases
3026 are uninteresting. */
3027 return dfs_skip_bases;
3029 if (BINFO_PRIMARY_P (binfo))
3030 /* If this is a primary base, then we have already looked at the
3031 virtual functions of its vtable. */
3032 return NULL_TREE;
3034 for (bv = BINFO_VIRTUALS (binfo); bv; bv = TREE_CHAIN (bv))
3036 fn = BV_FN (bv);
3038 if (DECL_NAME (fn) == opname)
3040 if (CLASSTYPE_LAZY_COPY_ASSIGN (t))
3041 lazily_declare_fn (sfk_copy_assignment, t);
3042 if (CLASSTYPE_LAZY_MOVE_ASSIGN (t))
3043 lazily_declare_fn (sfk_move_assignment, t);
3045 else if (DECL_DESTRUCTOR_P (fn)
3046 && CLASSTYPE_LAZY_DESTRUCTOR (t))
3047 lazily_declare_fn (sfk_destructor, t);
3050 return NULL_TREE;
3053 /* If the class type T has a direct or indirect base that contains a
3054 virtual assignment operator or a virtual destructor, declare that
3055 function in T if it hasn't been already declared. */
3057 static void
3058 declare_virt_assop_and_dtor (tree t)
3060 if (!(TYPE_POLYMORPHIC_P (t)
3061 && (CLASSTYPE_LAZY_COPY_ASSIGN (t)
3062 || CLASSTYPE_LAZY_MOVE_ASSIGN (t)
3063 || CLASSTYPE_LAZY_DESTRUCTOR (t))))
3064 return;
3066 dfs_walk_all (TYPE_BINFO (t),
3067 dfs_declare_virt_assop_and_dtor,
3068 NULL, t);
3071 /* Declare the inheriting constructor for class T inherited from base
3072 constructor CTOR with the parameter array PARMS of size NPARMS. */
3074 static void
3075 one_inheriting_sig (tree t, tree ctor, tree *parms, int nparms)
3077 /* We don't declare an inheriting ctor that would be a default,
3078 copy or move ctor for derived or base. */
3079 if (nparms == 0)
3080 return;
3081 if (nparms == 1
3082 && TREE_CODE (parms[0]) == REFERENCE_TYPE)
3084 tree parm = TYPE_MAIN_VARIANT (TREE_TYPE (parms[0]));
3085 if (parm == t || parm == DECL_CONTEXT (ctor))
3086 return;
3089 tree parmlist = void_list_node;
3090 for (int i = nparms - 1; i >= 0; i--)
3091 parmlist = tree_cons (NULL_TREE, parms[i], parmlist);
3092 tree fn = implicitly_declare_fn (sfk_inheriting_constructor,
3093 t, false, ctor, parmlist);
3094 if (add_method (t, fn, NULL_TREE))
3096 DECL_CHAIN (fn) = TYPE_METHODS (t);
3097 TYPE_METHODS (t) = fn;
3101 /* Declare all the inheriting constructors for class T inherited from base
3102 constructor CTOR. */
3104 static void
3105 one_inherited_ctor (tree ctor, tree t)
3107 tree parms = FUNCTION_FIRST_USER_PARMTYPE (ctor);
3109 tree *new_parms = XALLOCAVEC (tree, list_length (parms));
3110 int i = 0;
3111 for (; parms && parms != void_list_node; parms = TREE_CHAIN (parms))
3113 if (TREE_PURPOSE (parms))
3114 one_inheriting_sig (t, ctor, new_parms, i);
3115 new_parms[i++] = TREE_VALUE (parms);
3117 one_inheriting_sig (t, ctor, new_parms, i);
3118 if (parms == NULL_TREE)
3120 if (warning (OPT_Winherited_variadic_ctor,
3121 "the ellipsis in %qD is not inherited", ctor))
3122 inform (DECL_SOURCE_LOCATION (ctor), "%qD declared here", ctor);
3126 /* Create default constructors, assignment operators, and so forth for
3127 the type indicated by T, if they are needed. CANT_HAVE_CONST_CTOR,
3128 and CANT_HAVE_CONST_ASSIGNMENT are nonzero if, for whatever reason,
3129 the class cannot have a default constructor, copy constructor
3130 taking a const reference argument, or an assignment operator taking
3131 a const reference, respectively. */
3133 static void
3134 add_implicitly_declared_members (tree t, tree* access_decls,
3135 int cant_have_const_cctor,
3136 int cant_have_const_assignment)
3138 bool move_ok = false;
3140 if (cxx_dialect >= cxx11 && !CLASSTYPE_DESTRUCTORS (t)
3141 && !TYPE_HAS_COPY_CTOR (t) && !TYPE_HAS_COPY_ASSIGN (t)
3142 && !type_has_move_constructor (t) && !type_has_move_assign (t))
3143 move_ok = true;
3145 /* Destructor. */
3146 if (!CLASSTYPE_DESTRUCTORS (t))
3148 /* In general, we create destructors lazily. */
3149 CLASSTYPE_LAZY_DESTRUCTOR (t) = 1;
3151 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t)
3152 && TYPE_FOR_JAVA (t))
3153 /* But if this is a Java class, any non-trivial destructor is
3154 invalid, even if compiler-generated. Therefore, if the
3155 destructor is non-trivial we create it now. */
3156 lazily_declare_fn (sfk_destructor, t);
3159 /* [class.ctor]
3161 If there is no user-declared constructor for a class, a default
3162 constructor is implicitly declared. */
3163 if (! TYPE_HAS_USER_CONSTRUCTOR (t))
3165 TYPE_HAS_DEFAULT_CONSTRUCTOR (t) = 1;
3166 CLASSTYPE_LAZY_DEFAULT_CTOR (t) = 1;
3167 if (cxx_dialect >= cxx11)
3168 TYPE_HAS_CONSTEXPR_CTOR (t)
3169 /* This might force the declaration. */
3170 = type_has_constexpr_default_constructor (t);
3173 /* [class.ctor]
3175 If a class definition does not explicitly declare a copy
3176 constructor, one is declared implicitly. */
3177 if (! TYPE_HAS_COPY_CTOR (t) && ! TYPE_FOR_JAVA (t))
3179 TYPE_HAS_COPY_CTOR (t) = 1;
3180 TYPE_HAS_CONST_COPY_CTOR (t) = !cant_have_const_cctor;
3181 CLASSTYPE_LAZY_COPY_CTOR (t) = 1;
3182 if (move_ok)
3183 CLASSTYPE_LAZY_MOVE_CTOR (t) = 1;
3186 /* If there is no assignment operator, one will be created if and
3187 when it is needed. For now, just record whether or not the type
3188 of the parameter to the assignment operator will be a const or
3189 non-const reference. */
3190 if (!TYPE_HAS_COPY_ASSIGN (t) && !TYPE_FOR_JAVA (t))
3192 TYPE_HAS_COPY_ASSIGN (t) = 1;
3193 TYPE_HAS_CONST_COPY_ASSIGN (t) = !cant_have_const_assignment;
3194 CLASSTYPE_LAZY_COPY_ASSIGN (t) = 1;
3195 if (move_ok)
3196 CLASSTYPE_LAZY_MOVE_ASSIGN (t) = 1;
3199 /* We can't be lazy about declaring functions that might override
3200 a virtual function from a base class. */
3201 declare_virt_assop_and_dtor (t);
3203 while (*access_decls)
3205 tree using_decl = TREE_VALUE (*access_decls);
3206 tree decl = USING_DECL_DECLS (using_decl);
3207 if (DECL_NAME (using_decl) == ctor_identifier)
3209 /* declare, then remove the decl */
3210 tree ctor_list = decl;
3211 location_t loc = input_location;
3212 input_location = DECL_SOURCE_LOCATION (using_decl);
3213 if (ctor_list)
3214 for (; ctor_list; ctor_list = OVL_NEXT (ctor_list))
3215 one_inherited_ctor (OVL_CURRENT (ctor_list), t);
3216 *access_decls = TREE_CHAIN (*access_decls);
3217 input_location = loc;
3219 else
3220 access_decls = &TREE_CHAIN (*access_decls);
3224 /* Subroutine of insert_into_classtype_sorted_fields. Recursively
3225 count the number of fields in TYPE, including anonymous union
3226 members. */
3228 static int
3229 count_fields (tree fields)
3231 tree x;
3232 int n_fields = 0;
3233 for (x = fields; x; x = DECL_CHAIN (x))
3235 if (TREE_CODE (x) == FIELD_DECL && ANON_AGGR_TYPE_P (TREE_TYPE (x)))
3236 n_fields += count_fields (TYPE_FIELDS (TREE_TYPE (x)));
3237 else
3238 n_fields += 1;
3240 return n_fields;
3243 /* Subroutine of insert_into_classtype_sorted_fields. Recursively add
3244 all the fields in the TREE_LIST FIELDS to the SORTED_FIELDS_TYPE
3245 elts, starting at offset IDX. */
3247 static int
3248 add_fields_to_record_type (tree fields, struct sorted_fields_type *field_vec, int idx)
3250 tree x;
3251 for (x = fields; x; x = DECL_CHAIN (x))
3253 if (TREE_CODE (x) == FIELD_DECL && ANON_AGGR_TYPE_P (TREE_TYPE (x)))
3254 idx = add_fields_to_record_type (TYPE_FIELDS (TREE_TYPE (x)), field_vec, idx);
3255 else
3256 field_vec->elts[idx++] = x;
3258 return idx;
3261 /* Add all of the enum values of ENUMTYPE, to the FIELD_VEC elts,
3262 starting at offset IDX. */
3264 static int
3265 add_enum_fields_to_record_type (tree enumtype,
3266 struct sorted_fields_type *field_vec,
3267 int idx)
3269 tree values;
3270 for (values = TYPE_VALUES (enumtype); values; values = TREE_CHAIN (values))
3271 field_vec->elts[idx++] = TREE_VALUE (values);
3272 return idx;
3275 /* FIELD is a bit-field. We are finishing the processing for its
3276 enclosing type. Issue any appropriate messages and set appropriate
3277 flags. Returns false if an error has been diagnosed. */
3279 static bool
3280 check_bitfield_decl (tree field)
3282 tree type = TREE_TYPE (field);
3283 tree w;
3285 /* Extract the declared width of the bitfield, which has been
3286 temporarily stashed in DECL_INITIAL. */
3287 w = DECL_INITIAL (field);
3288 gcc_assert (w != NULL_TREE);
3289 /* Remove the bit-field width indicator so that the rest of the
3290 compiler does not treat that value as an initializer. */
3291 DECL_INITIAL (field) = NULL_TREE;
3293 /* Detect invalid bit-field type. */
3294 if (!INTEGRAL_OR_ENUMERATION_TYPE_P (type))
3296 error ("bit-field %q+#D with non-integral type", field);
3297 w = error_mark_node;
3299 else
3301 location_t loc = input_location;
3302 /* Avoid the non_lvalue wrapper added by fold for PLUS_EXPRs. */
3303 STRIP_NOPS (w);
3305 /* detect invalid field size. */
3306 input_location = DECL_SOURCE_LOCATION (field);
3307 w = cxx_constant_value (w);
3308 input_location = loc;
3310 if (TREE_CODE (w) != INTEGER_CST)
3312 error ("bit-field %q+D width not an integer constant", field);
3313 w = error_mark_node;
3315 else if (tree_int_cst_sgn (w) < 0)
3317 error ("negative width in bit-field %q+D", field);
3318 w = error_mark_node;
3320 else if (integer_zerop (w) && DECL_NAME (field) != 0)
3322 error ("zero width for bit-field %q+D", field);
3323 w = error_mark_node;
3325 else if ((TREE_CODE (type) != ENUMERAL_TYPE
3326 && TREE_CODE (type) != BOOLEAN_TYPE
3327 && compare_tree_int (w, TYPE_PRECISION (type)) > 0)
3328 || ((TREE_CODE (type) == ENUMERAL_TYPE
3329 || TREE_CODE (type) == BOOLEAN_TYPE)
3330 && tree_int_cst_lt (TYPE_SIZE (type), w)))
3331 warning (0, "width of %q+D exceeds its type", field);
3332 else if (TREE_CODE (type) == ENUMERAL_TYPE
3333 && (0 > (compare_tree_int
3334 (w, TYPE_PRECISION (ENUM_UNDERLYING_TYPE (type))))))
3335 warning (0, "%q+D is too small to hold all values of %q#T", field, type);
3338 if (w != error_mark_node)
3340 DECL_SIZE (field) = convert (bitsizetype, w);
3341 DECL_BIT_FIELD (field) = 1;
3342 return true;
3344 else
3346 /* Non-bit-fields are aligned for their type. */
3347 DECL_BIT_FIELD (field) = 0;
3348 CLEAR_DECL_C_BIT_FIELD (field);
3349 return false;
3353 /* FIELD is a non bit-field. We are finishing the processing for its
3354 enclosing type T. Issue any appropriate messages and set appropriate
3355 flags. */
3357 static void
3358 check_field_decl (tree field,
3359 tree t,
3360 int* cant_have_const_ctor,
3361 int* no_const_asn_ref,
3362 int* any_default_members)
3364 tree type = strip_array_types (TREE_TYPE (field));
3366 /* In C++98 an anonymous union cannot contain any fields which would change
3367 the settings of CANT_HAVE_CONST_CTOR and friends. */
3368 if (ANON_UNION_TYPE_P (type) && cxx_dialect < cxx11)
3370 /* And, we don't set TYPE_HAS_CONST_COPY_CTOR, etc., for anonymous
3371 structs. So, we recurse through their fields here. */
3372 else if (ANON_AGGR_TYPE_P (type))
3374 tree fields;
3376 for (fields = TYPE_FIELDS (type); fields; fields = DECL_CHAIN (fields))
3377 if (TREE_CODE (fields) == FIELD_DECL && !DECL_C_BIT_FIELD (field))
3378 check_field_decl (fields, t, cant_have_const_ctor,
3379 no_const_asn_ref, any_default_members);
3381 /* Check members with class type for constructors, destructors,
3382 etc. */
3383 else if (CLASS_TYPE_P (type))
3385 /* Never let anything with uninheritable virtuals
3386 make it through without complaint. */
3387 abstract_virtuals_error (field, type);
3389 if (TREE_CODE (t) == UNION_TYPE && cxx_dialect < cxx11)
3391 static bool warned;
3392 int oldcount = errorcount;
3393 if (TYPE_NEEDS_CONSTRUCTING (type))
3394 error ("member %q+#D with constructor not allowed in union",
3395 field);
3396 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
3397 error ("member %q+#D with destructor not allowed in union", field);
3398 if (TYPE_HAS_COMPLEX_COPY_ASSIGN (type))
3399 error ("member %q+#D with copy assignment operator not allowed in union",
3400 field);
3401 if (!warned && errorcount > oldcount)
3403 inform (DECL_SOURCE_LOCATION (field), "unrestricted unions "
3404 "only available with -std=c++11 or -std=gnu++11");
3405 warned = true;
3408 else
3410 TYPE_NEEDS_CONSTRUCTING (t) |= TYPE_NEEDS_CONSTRUCTING (type);
3411 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t)
3412 |= TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type);
3413 TYPE_HAS_COMPLEX_COPY_ASSIGN (t)
3414 |= (TYPE_HAS_COMPLEX_COPY_ASSIGN (type)
3415 || !TYPE_HAS_COPY_ASSIGN (type));
3416 TYPE_HAS_COMPLEX_COPY_CTOR (t) |= (TYPE_HAS_COMPLEX_COPY_CTOR (type)
3417 || !TYPE_HAS_COPY_CTOR (type));
3418 TYPE_HAS_COMPLEX_MOVE_ASSIGN (t) |= TYPE_HAS_COMPLEX_MOVE_ASSIGN (type);
3419 TYPE_HAS_COMPLEX_MOVE_CTOR (t) |= TYPE_HAS_COMPLEX_MOVE_CTOR (type);
3420 TYPE_HAS_COMPLEX_DFLT (t) |= (!TYPE_HAS_DEFAULT_CONSTRUCTOR (type)
3421 || TYPE_HAS_COMPLEX_DFLT (type));
3424 if (TYPE_HAS_COPY_CTOR (type)
3425 && !TYPE_HAS_CONST_COPY_CTOR (type))
3426 *cant_have_const_ctor = 1;
3428 if (TYPE_HAS_COPY_ASSIGN (type)
3429 && !TYPE_HAS_CONST_COPY_ASSIGN (type))
3430 *no_const_asn_ref = 1;
3433 check_abi_tags (t, field);
3435 if (DECL_INITIAL (field) != NULL_TREE)
3437 /* `build_class_init_list' does not recognize
3438 non-FIELD_DECLs. */
3439 if (TREE_CODE (t) == UNION_TYPE && *any_default_members != 0)
3440 error ("multiple fields in union %qT initialized", t);
3441 *any_default_members = 1;
3445 /* Check the data members (both static and non-static), class-scoped
3446 typedefs, etc., appearing in the declaration of T. Issue
3447 appropriate diagnostics. Sets ACCESS_DECLS to a list (in
3448 declaration order) of access declarations; each TREE_VALUE in this
3449 list is a USING_DECL.
3451 In addition, set the following flags:
3453 EMPTY_P
3454 The class is empty, i.e., contains no non-static data members.
3456 CANT_HAVE_CONST_CTOR_P
3457 This class cannot have an implicitly generated copy constructor
3458 taking a const reference.
3460 CANT_HAVE_CONST_ASN_REF
3461 This class cannot have an implicitly generated assignment
3462 operator taking a const reference.
3464 All of these flags should be initialized before calling this
3465 function.
3467 Returns a pointer to the end of the TYPE_FIELDs chain; additional
3468 fields can be added by adding to this chain. */
3470 static void
3471 check_field_decls (tree t, tree *access_decls,
3472 int *cant_have_const_ctor_p,
3473 int *no_const_asn_ref_p)
3475 tree *field;
3476 tree *next;
3477 bool has_pointers;
3478 int any_default_members;
3479 int cant_pack = 0;
3480 int field_access = -1;
3482 /* Assume there are no access declarations. */
3483 *access_decls = NULL_TREE;
3484 /* Assume this class has no pointer members. */
3485 has_pointers = false;
3486 /* Assume none of the members of this class have default
3487 initializations. */
3488 any_default_members = 0;
3490 for (field = &TYPE_FIELDS (t); *field; field = next)
3492 tree x = *field;
3493 tree type = TREE_TYPE (x);
3494 int this_field_access;
3496 next = &DECL_CHAIN (x);
3498 if (TREE_CODE (x) == USING_DECL)
3500 /* Save the access declarations for our caller. */
3501 *access_decls = tree_cons (NULL_TREE, x, *access_decls);
3502 continue;
3505 if (TREE_CODE (x) == TYPE_DECL
3506 || TREE_CODE (x) == TEMPLATE_DECL)
3507 continue;
3509 /* If we've gotten this far, it's a data member, possibly static,
3510 or an enumerator. */
3511 if (TREE_CODE (x) != CONST_DECL)
3512 DECL_CONTEXT (x) = t;
3514 /* When this goes into scope, it will be a non-local reference. */
3515 DECL_NONLOCAL (x) = 1;
3517 if (TREE_CODE (t) == UNION_TYPE
3518 && cxx_dialect < cxx11)
3520 /* [class.union] (C++98)
3522 If a union contains a static data member, or a member of
3523 reference type, the program is ill-formed.
3525 In C++11 this limitation doesn't exist anymore. */
3526 if (VAR_P (x))
3528 error ("in C++98 %q+D may not be static because it is "
3529 "a member of a union", x);
3530 continue;
3532 if (TREE_CODE (type) == REFERENCE_TYPE)
3534 error ("in C++98 %q+D may not have reference type %qT "
3535 "because it is a member of a union", x, type);
3536 continue;
3540 /* Perform error checking that did not get done in
3541 grokdeclarator. */
3542 if (TREE_CODE (type) == FUNCTION_TYPE)
3544 error ("field %q+D invalidly declared function type", x);
3545 type = build_pointer_type (type);
3546 TREE_TYPE (x) = type;
3548 else if (TREE_CODE (type) == METHOD_TYPE)
3550 error ("field %q+D invalidly declared method type", x);
3551 type = build_pointer_type (type);
3552 TREE_TYPE (x) = type;
3555 if (type == error_mark_node)
3556 continue;
3558 if (TREE_CODE (x) == CONST_DECL || VAR_P (x))
3559 continue;
3561 /* Now it can only be a FIELD_DECL. */
3563 if (TREE_PRIVATE (x) || TREE_PROTECTED (x))
3564 CLASSTYPE_NON_AGGREGATE (t) = 1;
3566 /* If at least one non-static data member is non-literal, the whole
3567 class becomes non-literal. Note: if the type is incomplete we
3568 will complain later on. */
3569 if (COMPLETE_TYPE_P (type) && !literal_type_p (type))
3570 CLASSTYPE_LITERAL_P (t) = false;
3572 /* A standard-layout class is a class that:
3574 has the same access control (Clause 11) for all non-static data members,
3575 ... */
3576 this_field_access = TREE_PROTECTED (x) ? 1 : TREE_PRIVATE (x) ? 2 : 0;
3577 if (field_access == -1)
3578 field_access = this_field_access;
3579 else if (this_field_access != field_access)
3580 CLASSTYPE_NON_STD_LAYOUT (t) = 1;
3582 /* If this is of reference type, check if it needs an init. */
3583 if (TREE_CODE (type) == REFERENCE_TYPE)
3585 CLASSTYPE_NON_LAYOUT_POD_P (t) = 1;
3586 CLASSTYPE_NON_STD_LAYOUT (t) = 1;
3587 if (DECL_INITIAL (x) == NULL_TREE)
3588 SET_CLASSTYPE_REF_FIELDS_NEED_INIT (t, 1);
3590 /* ARM $12.6.2: [A member initializer list] (or, for an
3591 aggregate, initialization by a brace-enclosed list) is the
3592 only way to initialize nonstatic const and reference
3593 members. */
3594 TYPE_HAS_COMPLEX_COPY_ASSIGN (t) = 1;
3595 TYPE_HAS_COMPLEX_MOVE_ASSIGN (t) = 1;
3598 type = strip_array_types (type);
3600 if (TYPE_PACKED (t))
3602 if (!layout_pod_type_p (type) && !TYPE_PACKED (type))
3604 warning
3606 "ignoring packed attribute because of unpacked non-POD field %q+#D",
3608 cant_pack = 1;
3610 else if (DECL_C_BIT_FIELD (x)
3611 || TYPE_ALIGN (TREE_TYPE (x)) > BITS_PER_UNIT)
3612 DECL_PACKED (x) = 1;
3615 if (DECL_C_BIT_FIELD (x) && integer_zerop (DECL_INITIAL (x)))
3616 /* We don't treat zero-width bitfields as making a class
3617 non-empty. */
3619 else
3621 /* The class is non-empty. */
3622 CLASSTYPE_EMPTY_P (t) = 0;
3623 /* The class is not even nearly empty. */
3624 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
3625 /* If one of the data members contains an empty class,
3626 so does T. */
3627 if (CLASS_TYPE_P (type)
3628 && CLASSTYPE_CONTAINS_EMPTY_CLASS_P (type))
3629 CLASSTYPE_CONTAINS_EMPTY_CLASS_P (t) = 1;
3632 /* This is used by -Weffc++ (see below). Warn only for pointers
3633 to members which might hold dynamic memory. So do not warn
3634 for pointers to functions or pointers to members. */
3635 if (TYPE_PTR_P (type)
3636 && !TYPE_PTRFN_P (type))
3637 has_pointers = true;
3639 if (CLASS_TYPE_P (type))
3641 if (CLASSTYPE_REF_FIELDS_NEED_INIT (type))
3642 SET_CLASSTYPE_REF_FIELDS_NEED_INIT (t, 1);
3643 if (CLASSTYPE_READONLY_FIELDS_NEED_INIT (type))
3644 SET_CLASSTYPE_READONLY_FIELDS_NEED_INIT (t, 1);
3647 if (DECL_MUTABLE_P (x) || TYPE_HAS_MUTABLE_P (type))
3648 CLASSTYPE_HAS_MUTABLE (t) = 1;
3650 if (DECL_MUTABLE_P (x))
3652 if (CP_TYPE_CONST_P (type))
3654 error ("member %q+D cannot be declared both %<const%> "
3655 "and %<mutable%>", x);
3656 continue;
3658 if (TREE_CODE (type) == REFERENCE_TYPE)
3660 error ("member %q+D cannot be declared as a %<mutable%> "
3661 "reference", x);
3662 continue;
3666 if (! layout_pod_type_p (type))
3667 /* DR 148 now allows pointers to members (which are POD themselves),
3668 to be allowed in POD structs. */
3669 CLASSTYPE_NON_LAYOUT_POD_P (t) = 1;
3671 if (!std_layout_type_p (type))
3672 CLASSTYPE_NON_STD_LAYOUT (t) = 1;
3674 if (! zero_init_p (type))
3675 CLASSTYPE_NON_ZERO_INIT_P (t) = 1;
3677 /* We set DECL_C_BIT_FIELD in grokbitfield.
3678 If the type and width are valid, we'll also set DECL_BIT_FIELD. */
3679 if (! DECL_C_BIT_FIELD (x) || ! check_bitfield_decl (x))
3680 check_field_decl (x, t,
3681 cant_have_const_ctor_p,
3682 no_const_asn_ref_p,
3683 &any_default_members);
3685 /* Now that we've removed bit-field widths from DECL_INITIAL,
3686 anything left in DECL_INITIAL is an NSDMI that makes the class
3687 non-aggregate. */
3688 if (DECL_INITIAL (x))
3689 CLASSTYPE_NON_AGGREGATE (t) = true;
3691 /* If any field is const, the structure type is pseudo-const. */
3692 if (CP_TYPE_CONST_P (type))
3694 C_TYPE_FIELDS_READONLY (t) = 1;
3695 if (DECL_INITIAL (x) == NULL_TREE)
3696 SET_CLASSTYPE_READONLY_FIELDS_NEED_INIT (t, 1);
3698 /* ARM $12.6.2: [A member initializer list] (or, for an
3699 aggregate, initialization by a brace-enclosed list) is the
3700 only way to initialize nonstatic const and reference
3701 members. */
3702 TYPE_HAS_COMPLEX_COPY_ASSIGN (t) = 1;
3703 TYPE_HAS_COMPLEX_MOVE_ASSIGN (t) = 1;
3705 /* A field that is pseudo-const makes the structure likewise. */
3706 else if (CLASS_TYPE_P (type))
3708 C_TYPE_FIELDS_READONLY (t) |= C_TYPE_FIELDS_READONLY (type);
3709 SET_CLASSTYPE_READONLY_FIELDS_NEED_INIT (t,
3710 CLASSTYPE_READONLY_FIELDS_NEED_INIT (t)
3711 | CLASSTYPE_READONLY_FIELDS_NEED_INIT (type));
3714 /* Core issue 80: A nonstatic data member is required to have a
3715 different name from the class iff the class has a
3716 user-declared constructor. */
3717 if (constructor_name_p (DECL_NAME (x), t)
3718 && TYPE_HAS_USER_CONSTRUCTOR (t))
3719 permerror (input_location, "field %q+#D with same name as class", x);
3722 /* Effective C++ rule 11: if a class has dynamic memory held by pointers,
3723 it should also define a copy constructor and an assignment operator to
3724 implement the correct copy semantic (deep vs shallow, etc.). As it is
3725 not feasible to check whether the constructors do allocate dynamic memory
3726 and store it within members, we approximate the warning like this:
3728 -- Warn only if there are members which are pointers
3729 -- Warn only if there is a non-trivial constructor (otherwise,
3730 there cannot be memory allocated).
3731 -- Warn only if there is a non-trivial destructor. We assume that the
3732 user at least implemented the cleanup correctly, and a destructor
3733 is needed to free dynamic memory.
3735 This seems enough for practical purposes. */
3736 if (warn_ecpp
3737 && has_pointers
3738 && TYPE_HAS_USER_CONSTRUCTOR (t)
3739 && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t)
3740 && !(TYPE_HAS_COPY_CTOR (t) && TYPE_HAS_COPY_ASSIGN (t)))
3742 warning (OPT_Weffc__, "%q#T has pointer data members", t);
3744 if (! TYPE_HAS_COPY_CTOR (t))
3746 warning (OPT_Weffc__,
3747 " but does not override %<%T(const %T&)%>", t, t);
3748 if (!TYPE_HAS_COPY_ASSIGN (t))
3749 warning (OPT_Weffc__, " or %<operator=(const %T&)%>", t);
3751 else if (! TYPE_HAS_COPY_ASSIGN (t))
3752 warning (OPT_Weffc__,
3753 " but does not override %<operator=(const %T&)%>", t);
3756 /* Non-static data member initializers make the default constructor
3757 non-trivial. */
3758 if (any_default_members)
3760 TYPE_NEEDS_CONSTRUCTING (t) = true;
3761 TYPE_HAS_COMPLEX_DFLT (t) = true;
3764 /* If any of the fields couldn't be packed, unset TYPE_PACKED. */
3765 if (cant_pack)
3766 TYPE_PACKED (t) = 0;
3768 /* Check anonymous struct/anonymous union fields. */
3769 finish_struct_anon (t);
3771 /* We've built up the list of access declarations in reverse order.
3772 Fix that now. */
3773 *access_decls = nreverse (*access_decls);
3776 /* If TYPE is an empty class type, records its OFFSET in the table of
3777 OFFSETS. */
3779 static int
3780 record_subobject_offset (tree type, tree offset, splay_tree offsets)
3782 splay_tree_node n;
3784 if (!is_empty_class (type))
3785 return 0;
3787 /* Record the location of this empty object in OFFSETS. */
3788 n = splay_tree_lookup (offsets, (splay_tree_key) offset);
3789 if (!n)
3790 n = splay_tree_insert (offsets,
3791 (splay_tree_key) offset,
3792 (splay_tree_value) NULL_TREE);
3793 n->value = ((splay_tree_value)
3794 tree_cons (NULL_TREE,
3795 type,
3796 (tree) n->value));
3798 return 0;
3801 /* Returns nonzero if TYPE is an empty class type and there is
3802 already an entry in OFFSETS for the same TYPE as the same OFFSET. */
3804 static int
3805 check_subobject_offset (tree type, tree offset, splay_tree offsets)
3807 splay_tree_node n;
3808 tree t;
3810 if (!is_empty_class (type))
3811 return 0;
3813 /* Record the location of this empty object in OFFSETS. */
3814 n = splay_tree_lookup (offsets, (splay_tree_key) offset);
3815 if (!n)
3816 return 0;
3818 for (t = (tree) n->value; t; t = TREE_CHAIN (t))
3819 if (same_type_p (TREE_VALUE (t), type))
3820 return 1;
3822 return 0;
3825 /* Walk through all the subobjects of TYPE (located at OFFSET). Call
3826 F for every subobject, passing it the type, offset, and table of
3827 OFFSETS. If VBASES_P is one, then virtual non-primary bases should
3828 be traversed.
3830 If MAX_OFFSET is non-NULL, then subobjects with an offset greater
3831 than MAX_OFFSET will not be walked.
3833 If F returns a nonzero value, the traversal ceases, and that value
3834 is returned. Otherwise, returns zero. */
3836 static int
3837 walk_subobject_offsets (tree type,
3838 subobject_offset_fn f,
3839 tree offset,
3840 splay_tree offsets,
3841 tree max_offset,
3842 int vbases_p)
3844 int r = 0;
3845 tree type_binfo = NULL_TREE;
3847 /* If this OFFSET is bigger than the MAX_OFFSET, then we should
3848 stop. */
3849 if (max_offset && tree_int_cst_lt (max_offset, offset))
3850 return 0;
3852 if (type == error_mark_node)
3853 return 0;
3855 if (!TYPE_P (type))
3857 type_binfo = type;
3858 type = BINFO_TYPE (type);
3861 if (CLASS_TYPE_P (type))
3863 tree field;
3864 tree binfo;
3865 int i;
3867 /* Avoid recursing into objects that are not interesting. */
3868 if (!CLASSTYPE_CONTAINS_EMPTY_CLASS_P (type))
3869 return 0;
3871 /* Record the location of TYPE. */
3872 r = (*f) (type, offset, offsets);
3873 if (r)
3874 return r;
3876 /* Iterate through the direct base classes of TYPE. */
3877 if (!type_binfo)
3878 type_binfo = TYPE_BINFO (type);
3879 for (i = 0; BINFO_BASE_ITERATE (type_binfo, i, binfo); i++)
3881 tree binfo_offset;
3883 if (BINFO_VIRTUAL_P (binfo))
3884 continue;
3886 tree orig_binfo;
3887 /* We cannot rely on BINFO_OFFSET being set for the base
3888 class yet, but the offsets for direct non-virtual
3889 bases can be calculated by going back to the TYPE. */
3890 orig_binfo = BINFO_BASE_BINFO (TYPE_BINFO (type), i);
3891 binfo_offset = size_binop (PLUS_EXPR,
3892 offset,
3893 BINFO_OFFSET (orig_binfo));
3895 r = walk_subobject_offsets (binfo,
3897 binfo_offset,
3898 offsets,
3899 max_offset,
3900 /*vbases_p=*/0);
3901 if (r)
3902 return r;
3905 if (CLASSTYPE_VBASECLASSES (type))
3907 unsigned ix;
3908 vec<tree, va_gc> *vbases;
3910 /* Iterate through the virtual base classes of TYPE. In G++
3911 3.2, we included virtual bases in the direct base class
3912 loop above, which results in incorrect results; the
3913 correct offsets for virtual bases are only known when
3914 working with the most derived type. */
3915 if (vbases_p)
3916 for (vbases = CLASSTYPE_VBASECLASSES (type), ix = 0;
3917 vec_safe_iterate (vbases, ix, &binfo); ix++)
3919 r = walk_subobject_offsets (binfo,
3921 size_binop (PLUS_EXPR,
3922 offset,
3923 BINFO_OFFSET (binfo)),
3924 offsets,
3925 max_offset,
3926 /*vbases_p=*/0);
3927 if (r)
3928 return r;
3930 else
3932 /* We still have to walk the primary base, if it is
3933 virtual. (If it is non-virtual, then it was walked
3934 above.) */
3935 tree vbase = get_primary_binfo (type_binfo);
3937 if (vbase && BINFO_VIRTUAL_P (vbase)
3938 && BINFO_PRIMARY_P (vbase)
3939 && BINFO_INHERITANCE_CHAIN (vbase) == type_binfo)
3941 r = (walk_subobject_offsets
3942 (vbase, f, offset,
3943 offsets, max_offset, /*vbases_p=*/0));
3944 if (r)
3945 return r;
3950 /* Iterate through the fields of TYPE. */
3951 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
3952 if (TREE_CODE (field) == FIELD_DECL
3953 && TREE_TYPE (field) != error_mark_node
3954 && !DECL_ARTIFICIAL (field))
3956 tree field_offset;
3958 field_offset = byte_position (field);
3960 r = walk_subobject_offsets (TREE_TYPE (field),
3962 size_binop (PLUS_EXPR,
3963 offset,
3964 field_offset),
3965 offsets,
3966 max_offset,
3967 /*vbases_p=*/1);
3968 if (r)
3969 return r;
3972 else if (TREE_CODE (type) == ARRAY_TYPE)
3974 tree element_type = strip_array_types (type);
3975 tree domain = TYPE_DOMAIN (type);
3976 tree index;
3978 /* Avoid recursing into objects that are not interesting. */
3979 if (!CLASS_TYPE_P (element_type)
3980 || !CLASSTYPE_CONTAINS_EMPTY_CLASS_P (element_type))
3981 return 0;
3983 /* Step through each of the elements in the array. */
3984 for (index = size_zero_node;
3985 !tree_int_cst_lt (TYPE_MAX_VALUE (domain), index);
3986 index = size_binop (PLUS_EXPR, index, size_one_node))
3988 r = walk_subobject_offsets (TREE_TYPE (type),
3990 offset,
3991 offsets,
3992 max_offset,
3993 /*vbases_p=*/1);
3994 if (r)
3995 return r;
3996 offset = size_binop (PLUS_EXPR, offset,
3997 TYPE_SIZE_UNIT (TREE_TYPE (type)));
3998 /* If this new OFFSET is bigger than the MAX_OFFSET, then
3999 there's no point in iterating through the remaining
4000 elements of the array. */
4001 if (max_offset && tree_int_cst_lt (max_offset, offset))
4002 break;
4006 return 0;
4009 /* Record all of the empty subobjects of TYPE (either a type or a
4010 binfo). If IS_DATA_MEMBER is true, then a non-static data member
4011 is being placed at OFFSET; otherwise, it is a base class that is
4012 being placed at OFFSET. */
4014 static void
4015 record_subobject_offsets (tree type,
4016 tree offset,
4017 splay_tree offsets,
4018 bool is_data_member)
4020 tree max_offset;
4021 /* If recording subobjects for a non-static data member or a
4022 non-empty base class , we do not need to record offsets beyond
4023 the size of the biggest empty class. Additional data members
4024 will go at the end of the class. Additional base classes will go
4025 either at offset zero (if empty, in which case they cannot
4026 overlap with offsets past the size of the biggest empty class) or
4027 at the end of the class.
4029 However, if we are placing an empty base class, then we must record
4030 all offsets, as either the empty class is at offset zero (where
4031 other empty classes might later be placed) or at the end of the
4032 class (where other objects might then be placed, so other empty
4033 subobjects might later overlap). */
4034 if (is_data_member
4035 || !is_empty_class (BINFO_TYPE (type)))
4036 max_offset = sizeof_biggest_empty_class;
4037 else
4038 max_offset = NULL_TREE;
4039 walk_subobject_offsets (type, record_subobject_offset, offset,
4040 offsets, max_offset, is_data_member);
4043 /* Returns nonzero if any of the empty subobjects of TYPE (located at
4044 OFFSET) conflict with entries in OFFSETS. If VBASES_P is nonzero,
4045 virtual bases of TYPE are examined. */
4047 static int
4048 layout_conflict_p (tree type,
4049 tree offset,
4050 splay_tree offsets,
4051 int vbases_p)
4053 splay_tree_node max_node;
4055 /* Get the node in OFFSETS that indicates the maximum offset where
4056 an empty subobject is located. */
4057 max_node = splay_tree_max (offsets);
4058 /* If there aren't any empty subobjects, then there's no point in
4059 performing this check. */
4060 if (!max_node)
4061 return 0;
4063 return walk_subobject_offsets (type, check_subobject_offset, offset,
4064 offsets, (tree) (max_node->key),
4065 vbases_p);
4068 /* DECL is a FIELD_DECL corresponding either to a base subobject of a
4069 non-static data member of the type indicated by RLI. BINFO is the
4070 binfo corresponding to the base subobject, OFFSETS maps offsets to
4071 types already located at those offsets. This function determines
4072 the position of the DECL. */
4074 static void
4075 layout_nonempty_base_or_field (record_layout_info rli,
4076 tree decl,
4077 tree binfo,
4078 splay_tree offsets)
4080 tree offset = NULL_TREE;
4081 bool field_p;
4082 tree type;
4084 if (binfo)
4086 /* For the purposes of determining layout conflicts, we want to
4087 use the class type of BINFO; TREE_TYPE (DECL) will be the
4088 CLASSTYPE_AS_BASE version, which does not contain entries for
4089 zero-sized bases. */
4090 type = TREE_TYPE (binfo);
4091 field_p = false;
4093 else
4095 type = TREE_TYPE (decl);
4096 field_p = true;
4099 /* Try to place the field. It may take more than one try if we have
4100 a hard time placing the field without putting two objects of the
4101 same type at the same address. */
4102 while (1)
4104 struct record_layout_info_s old_rli = *rli;
4106 /* Place this field. */
4107 place_field (rli, decl);
4108 offset = byte_position (decl);
4110 /* We have to check to see whether or not there is already
4111 something of the same type at the offset we're about to use.
4112 For example, consider:
4114 struct S {};
4115 struct T : public S { int i; };
4116 struct U : public S, public T {};
4118 Here, we put S at offset zero in U. Then, we can't put T at
4119 offset zero -- its S component would be at the same address
4120 as the S we already allocated. So, we have to skip ahead.
4121 Since all data members, including those whose type is an
4122 empty class, have nonzero size, any overlap can happen only
4123 with a direct or indirect base-class -- it can't happen with
4124 a data member. */
4125 /* In a union, overlap is permitted; all members are placed at
4126 offset zero. */
4127 if (TREE_CODE (rli->t) == UNION_TYPE)
4128 break;
4129 if (layout_conflict_p (field_p ? type : binfo, offset,
4130 offsets, field_p))
4132 /* Strip off the size allocated to this field. That puts us
4133 at the first place we could have put the field with
4134 proper alignment. */
4135 *rli = old_rli;
4137 /* Bump up by the alignment required for the type. */
4138 rli->bitpos
4139 = size_binop (PLUS_EXPR, rli->bitpos,
4140 bitsize_int (binfo
4141 ? CLASSTYPE_ALIGN (type)
4142 : TYPE_ALIGN (type)));
4143 normalize_rli (rli);
4145 else
4146 /* There was no conflict. We're done laying out this field. */
4147 break;
4150 /* Now that we know where it will be placed, update its
4151 BINFO_OFFSET. */
4152 if (binfo && CLASS_TYPE_P (BINFO_TYPE (binfo)))
4153 /* Indirect virtual bases may have a nonzero BINFO_OFFSET at
4154 this point because their BINFO_OFFSET is copied from another
4155 hierarchy. Therefore, we may not need to add the entire
4156 OFFSET. */
4157 propagate_binfo_offsets (binfo,
4158 size_diffop_loc (input_location,
4159 convert (ssizetype, offset),
4160 convert (ssizetype,
4161 BINFO_OFFSET (binfo))));
4164 /* Returns true if TYPE is empty and OFFSET is nonzero. */
4166 static int
4167 empty_base_at_nonzero_offset_p (tree type,
4168 tree offset,
4169 splay_tree /*offsets*/)
4171 return is_empty_class (type) && !integer_zerop (offset);
4174 /* Layout the empty base BINFO. EOC indicates the byte currently just
4175 past the end of the class, and should be correctly aligned for a
4176 class of the type indicated by BINFO; OFFSETS gives the offsets of
4177 the empty bases allocated so far. T is the most derived
4178 type. Return nonzero iff we added it at the end. */
4180 static bool
4181 layout_empty_base (record_layout_info rli, tree binfo,
4182 tree eoc, splay_tree offsets)
4184 tree alignment;
4185 tree basetype = BINFO_TYPE (binfo);
4186 bool atend = false;
4188 /* This routine should only be used for empty classes. */
4189 gcc_assert (is_empty_class (basetype));
4190 alignment = ssize_int (CLASSTYPE_ALIGN_UNIT (basetype));
4192 if (!integer_zerop (BINFO_OFFSET (binfo)))
4193 propagate_binfo_offsets
4194 (binfo, size_diffop_loc (input_location,
4195 size_zero_node, BINFO_OFFSET (binfo)));
4197 /* This is an empty base class. We first try to put it at offset
4198 zero. */
4199 if (layout_conflict_p (binfo,
4200 BINFO_OFFSET (binfo),
4201 offsets,
4202 /*vbases_p=*/0))
4204 /* That didn't work. Now, we move forward from the next
4205 available spot in the class. */
4206 atend = true;
4207 propagate_binfo_offsets (binfo, convert (ssizetype, eoc));
4208 while (1)
4210 if (!layout_conflict_p (binfo,
4211 BINFO_OFFSET (binfo),
4212 offsets,
4213 /*vbases_p=*/0))
4214 /* We finally found a spot where there's no overlap. */
4215 break;
4217 /* There's overlap here, too. Bump along to the next spot. */
4218 propagate_binfo_offsets (binfo, alignment);
4222 if (CLASSTYPE_USER_ALIGN (basetype))
4224 rli->record_align = MAX (rli->record_align, CLASSTYPE_ALIGN (basetype));
4225 if (warn_packed)
4226 rli->unpacked_align = MAX (rli->unpacked_align, CLASSTYPE_ALIGN (basetype));
4227 TYPE_USER_ALIGN (rli->t) = 1;
4230 return atend;
4233 /* Layout the base given by BINFO in the class indicated by RLI.
4234 *BASE_ALIGN is a running maximum of the alignments of
4235 any base class. OFFSETS gives the location of empty base
4236 subobjects. T is the most derived type. Return nonzero if the new
4237 object cannot be nearly-empty. A new FIELD_DECL is inserted at
4238 *NEXT_FIELD, unless BINFO is for an empty base class.
4240 Returns the location at which the next field should be inserted. */
4242 static tree *
4243 build_base_field (record_layout_info rli, tree binfo,
4244 splay_tree offsets, tree *next_field)
4246 tree t = rli->t;
4247 tree basetype = BINFO_TYPE (binfo);
4249 if (!COMPLETE_TYPE_P (basetype))
4250 /* This error is now reported in xref_tag, thus giving better
4251 location information. */
4252 return next_field;
4254 /* Place the base class. */
4255 if (!is_empty_class (basetype))
4257 tree decl;
4259 /* The containing class is non-empty because it has a non-empty
4260 base class. */
4261 CLASSTYPE_EMPTY_P (t) = 0;
4263 /* Create the FIELD_DECL. */
4264 decl = build_decl (input_location,
4265 FIELD_DECL, NULL_TREE, CLASSTYPE_AS_BASE (basetype));
4266 DECL_ARTIFICIAL (decl) = 1;
4267 DECL_IGNORED_P (decl) = 1;
4268 DECL_FIELD_CONTEXT (decl) = t;
4269 if (CLASSTYPE_AS_BASE (basetype))
4271 DECL_SIZE (decl) = CLASSTYPE_SIZE (basetype);
4272 DECL_SIZE_UNIT (decl) = CLASSTYPE_SIZE_UNIT (basetype);
4273 DECL_ALIGN (decl) = CLASSTYPE_ALIGN (basetype);
4274 DECL_USER_ALIGN (decl) = CLASSTYPE_USER_ALIGN (basetype);
4275 DECL_MODE (decl) = TYPE_MODE (basetype);
4276 DECL_FIELD_IS_BASE (decl) = 1;
4278 /* Try to place the field. It may take more than one try if we
4279 have a hard time placing the field without putting two
4280 objects of the same type at the same address. */
4281 layout_nonempty_base_or_field (rli, decl, binfo, offsets);
4282 /* Add the new FIELD_DECL to the list of fields for T. */
4283 DECL_CHAIN (decl) = *next_field;
4284 *next_field = decl;
4285 next_field = &DECL_CHAIN (decl);
4288 else
4290 tree eoc;
4291 bool atend;
4293 /* On some platforms (ARM), even empty classes will not be
4294 byte-aligned. */
4295 eoc = round_up_loc (input_location,
4296 rli_size_unit_so_far (rli),
4297 CLASSTYPE_ALIGN_UNIT (basetype));
4298 atend = layout_empty_base (rli, binfo, eoc, offsets);
4299 /* A nearly-empty class "has no proper base class that is empty,
4300 not morally virtual, and at an offset other than zero." */
4301 if (!BINFO_VIRTUAL_P (binfo) && CLASSTYPE_NEARLY_EMPTY_P (t))
4303 if (atend)
4304 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
4305 /* The check above (used in G++ 3.2) is insufficient because
4306 an empty class placed at offset zero might itself have an
4307 empty base at a nonzero offset. */
4308 else if (walk_subobject_offsets (basetype,
4309 empty_base_at_nonzero_offset_p,
4310 size_zero_node,
4311 /*offsets=*/NULL,
4312 /*max_offset=*/NULL_TREE,
4313 /*vbases_p=*/true))
4314 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
4317 /* We do not create a FIELD_DECL for empty base classes because
4318 it might overlap some other field. We want to be able to
4319 create CONSTRUCTORs for the class by iterating over the
4320 FIELD_DECLs, and the back end does not handle overlapping
4321 FIELD_DECLs. */
4323 /* An empty virtual base causes a class to be non-empty
4324 -- but in that case we do not need to clear CLASSTYPE_EMPTY_P
4325 here because that was already done when the virtual table
4326 pointer was created. */
4329 /* Record the offsets of BINFO and its base subobjects. */
4330 record_subobject_offsets (binfo,
4331 BINFO_OFFSET (binfo),
4332 offsets,
4333 /*is_data_member=*/false);
4335 return next_field;
4338 /* Layout all of the non-virtual base classes. Record empty
4339 subobjects in OFFSETS. T is the most derived type. Return nonzero
4340 if the type cannot be nearly empty. The fields created
4341 corresponding to the base classes will be inserted at
4342 *NEXT_FIELD. */
4344 static void
4345 build_base_fields (record_layout_info rli,
4346 splay_tree offsets, tree *next_field)
4348 /* Chain to hold all the new FIELD_DECLs which stand in for base class
4349 subobjects. */
4350 tree t = rli->t;
4351 int n_baseclasses = BINFO_N_BASE_BINFOS (TYPE_BINFO (t));
4352 int i;
4354 /* The primary base class is always allocated first. */
4355 if (CLASSTYPE_HAS_PRIMARY_BASE_P (t))
4356 next_field = build_base_field (rli, CLASSTYPE_PRIMARY_BINFO (t),
4357 offsets, next_field);
4359 /* Now allocate the rest of the bases. */
4360 for (i = 0; i < n_baseclasses; ++i)
4362 tree base_binfo;
4364 base_binfo = BINFO_BASE_BINFO (TYPE_BINFO (t), i);
4366 /* The primary base was already allocated above, so we don't
4367 need to allocate it again here. */
4368 if (base_binfo == CLASSTYPE_PRIMARY_BINFO (t))
4369 continue;
4371 /* Virtual bases are added at the end (a primary virtual base
4372 will have already been added). */
4373 if (BINFO_VIRTUAL_P (base_binfo))
4374 continue;
4376 next_field = build_base_field (rli, base_binfo,
4377 offsets, next_field);
4381 /* Go through the TYPE_METHODS of T issuing any appropriate
4382 diagnostics, figuring out which methods override which other
4383 methods, and so forth. */
4385 static void
4386 check_methods (tree t)
4388 tree x;
4390 for (x = TYPE_METHODS (t); x; x = DECL_CHAIN (x))
4392 check_for_override (x, t);
4393 if (DECL_PURE_VIRTUAL_P (x) && (TREE_CODE (x) != FUNCTION_DECL || ! DECL_VINDEX (x)))
4394 error ("initializer specified for non-virtual method %q+D", x);
4395 /* The name of the field is the original field name
4396 Save this in auxiliary field for later overloading. */
4397 if (TREE_CODE (x) == FUNCTION_DECL && DECL_VINDEX (x))
4399 TYPE_POLYMORPHIC_P (t) = 1;
4400 if (DECL_PURE_VIRTUAL_P (x))
4401 vec_safe_push (CLASSTYPE_PURE_VIRTUALS (t), x);
4403 /* All user-provided destructors are non-trivial.
4404 Constructors and assignment ops are handled in
4405 grok_special_member_properties. */
4406 if (DECL_DESTRUCTOR_P (x) && user_provided_p (x))
4407 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t) = 1;
4411 /* FN is a constructor or destructor. Clone the declaration to create
4412 a specialized in-charge or not-in-charge version, as indicated by
4413 NAME. */
4415 static tree
4416 build_clone (tree fn, tree name)
4418 tree parms;
4419 tree clone;
4421 /* Copy the function. */
4422 clone = copy_decl (fn);
4423 /* Reset the function name. */
4424 DECL_NAME (clone) = name;
4425 SET_DECL_ASSEMBLER_NAME (clone, NULL_TREE);
4426 /* Remember where this function came from. */
4427 DECL_ABSTRACT_ORIGIN (clone) = fn;
4428 /* Make it easy to find the CLONE given the FN. */
4429 DECL_CHAIN (clone) = DECL_CHAIN (fn);
4430 DECL_CHAIN (fn) = clone;
4432 /* If this is a template, do the rest on the DECL_TEMPLATE_RESULT. */
4433 if (TREE_CODE (clone) == TEMPLATE_DECL)
4435 tree result = build_clone (DECL_TEMPLATE_RESULT (clone), name);
4436 DECL_TEMPLATE_RESULT (clone) = result;
4437 DECL_TEMPLATE_INFO (result) = copy_node (DECL_TEMPLATE_INFO (result));
4438 DECL_TI_TEMPLATE (result) = clone;
4439 TREE_TYPE (clone) = TREE_TYPE (result);
4440 return clone;
4443 DECL_CLONED_FUNCTION (clone) = fn;
4444 /* There's no pending inline data for this function. */
4445 DECL_PENDING_INLINE_INFO (clone) = NULL;
4446 DECL_PENDING_INLINE_P (clone) = 0;
4448 /* The base-class destructor is not virtual. */
4449 if (name == base_dtor_identifier)
4451 DECL_VIRTUAL_P (clone) = 0;
4452 if (TREE_CODE (clone) != TEMPLATE_DECL)
4453 DECL_VINDEX (clone) = NULL_TREE;
4456 /* If there was an in-charge parameter, drop it from the function
4457 type. */
4458 if (DECL_HAS_IN_CHARGE_PARM_P (clone))
4460 tree basetype;
4461 tree parmtypes;
4462 tree exceptions;
4464 exceptions = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (clone));
4465 basetype = TYPE_METHOD_BASETYPE (TREE_TYPE (clone));
4466 parmtypes = TYPE_ARG_TYPES (TREE_TYPE (clone));
4467 /* Skip the `this' parameter. */
4468 parmtypes = TREE_CHAIN (parmtypes);
4469 /* Skip the in-charge parameter. */
4470 parmtypes = TREE_CHAIN (parmtypes);
4471 /* And the VTT parm, in a complete [cd]tor. */
4472 if (DECL_HAS_VTT_PARM_P (fn)
4473 && ! DECL_NEEDS_VTT_PARM_P (clone))
4474 parmtypes = TREE_CHAIN (parmtypes);
4475 /* If this is subobject constructor or destructor, add the vtt
4476 parameter. */
4477 TREE_TYPE (clone)
4478 = build_method_type_directly (basetype,
4479 TREE_TYPE (TREE_TYPE (clone)),
4480 parmtypes);
4481 if (exceptions)
4482 TREE_TYPE (clone) = build_exception_variant (TREE_TYPE (clone),
4483 exceptions);
4484 TREE_TYPE (clone)
4485 = cp_build_type_attribute_variant (TREE_TYPE (clone),
4486 TYPE_ATTRIBUTES (TREE_TYPE (fn)));
4489 /* Copy the function parameters. */
4490 DECL_ARGUMENTS (clone) = copy_list (DECL_ARGUMENTS (clone));
4491 /* Remove the in-charge parameter. */
4492 if (DECL_HAS_IN_CHARGE_PARM_P (clone))
4494 DECL_CHAIN (DECL_ARGUMENTS (clone))
4495 = DECL_CHAIN (DECL_CHAIN (DECL_ARGUMENTS (clone)));
4496 DECL_HAS_IN_CHARGE_PARM_P (clone) = 0;
4498 /* And the VTT parm, in a complete [cd]tor. */
4499 if (DECL_HAS_VTT_PARM_P (fn))
4501 if (DECL_NEEDS_VTT_PARM_P (clone))
4502 DECL_HAS_VTT_PARM_P (clone) = 1;
4503 else
4505 DECL_CHAIN (DECL_ARGUMENTS (clone))
4506 = DECL_CHAIN (DECL_CHAIN (DECL_ARGUMENTS (clone)));
4507 DECL_HAS_VTT_PARM_P (clone) = 0;
4511 for (parms = DECL_ARGUMENTS (clone); parms; parms = DECL_CHAIN (parms))
4513 DECL_CONTEXT (parms) = clone;
4514 cxx_dup_lang_specific_decl (parms);
4517 /* Create the RTL for this function. */
4518 SET_DECL_RTL (clone, NULL);
4519 rest_of_decl_compilation (clone, /*top_level=*/1, at_eof);
4521 if (pch_file)
4522 note_decl_for_pch (clone);
4524 return clone;
4527 /* Implementation of DECL_CLONED_FUNCTION and DECL_CLONED_FUNCTION_P, do
4528 not invoke this function directly.
4530 For a non-thunk function, returns the address of the slot for storing
4531 the function it is a clone of. Otherwise returns NULL_TREE.
4533 If JUST_TESTING, looks through TEMPLATE_DECL and returns NULL if
4534 cloned_function is unset. This is to support the separate
4535 DECL_CLONED_FUNCTION and DECL_CLONED_FUNCTION_P modes; using the latter
4536 on a template makes sense, but not the former. */
4538 tree *
4539 decl_cloned_function_p (const_tree decl, bool just_testing)
4541 tree *ptr;
4542 if (just_testing)
4543 decl = STRIP_TEMPLATE (decl);
4545 if (TREE_CODE (decl) != FUNCTION_DECL
4546 || !DECL_LANG_SPECIFIC (decl)
4547 || DECL_LANG_SPECIFIC (decl)->u.fn.thunk_p)
4549 #if defined ENABLE_TREE_CHECKING && (GCC_VERSION >= 2007)
4550 if (!just_testing)
4551 lang_check_failed (__FILE__, __LINE__, __FUNCTION__);
4552 else
4553 #endif
4554 return NULL;
4557 ptr = &DECL_LANG_SPECIFIC (decl)->u.fn.u5.cloned_function;
4558 if (just_testing && *ptr == NULL_TREE)
4559 return NULL;
4560 else
4561 return ptr;
4564 /* Produce declarations for all appropriate clones of FN. If
4565 UPDATE_METHOD_VEC_P is nonzero, the clones are added to the
4566 CLASTYPE_METHOD_VEC as well. */
4568 void
4569 clone_function_decl (tree fn, int update_method_vec_p)
4571 tree clone;
4573 /* Avoid inappropriate cloning. */
4574 if (DECL_CHAIN (fn)
4575 && DECL_CLONED_FUNCTION_P (DECL_CHAIN (fn)))
4576 return;
4578 if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (fn))
4580 /* For each constructor, we need two variants: an in-charge version
4581 and a not-in-charge version. */
4582 clone = build_clone (fn, complete_ctor_identifier);
4583 if (update_method_vec_p)
4584 add_method (DECL_CONTEXT (clone), clone, NULL_TREE);
4585 clone = build_clone (fn, base_ctor_identifier);
4586 if (update_method_vec_p)
4587 add_method (DECL_CONTEXT (clone), clone, NULL_TREE);
4589 else
4591 gcc_assert (DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (fn));
4593 /* For each destructor, we need three variants: an in-charge
4594 version, a not-in-charge version, and an in-charge deleting
4595 version. We clone the deleting version first because that
4596 means it will go second on the TYPE_METHODS list -- and that
4597 corresponds to the correct layout order in the virtual
4598 function table.
4600 For a non-virtual destructor, we do not build a deleting
4601 destructor. */
4602 if (DECL_VIRTUAL_P (fn))
4604 clone = build_clone (fn, deleting_dtor_identifier);
4605 if (update_method_vec_p)
4606 add_method (DECL_CONTEXT (clone), clone, NULL_TREE);
4608 clone = build_clone (fn, complete_dtor_identifier);
4609 if (update_method_vec_p)
4610 add_method (DECL_CONTEXT (clone), clone, NULL_TREE);
4611 clone = build_clone (fn, base_dtor_identifier);
4612 if (update_method_vec_p)
4613 add_method (DECL_CONTEXT (clone), clone, NULL_TREE);
4616 /* Note that this is an abstract function that is never emitted. */
4617 DECL_ABSTRACT (fn) = 1;
4620 /* DECL is an in charge constructor, which is being defined. This will
4621 have had an in class declaration, from whence clones were
4622 declared. An out-of-class definition can specify additional default
4623 arguments. As it is the clones that are involved in overload
4624 resolution, we must propagate the information from the DECL to its
4625 clones. */
4627 void
4628 adjust_clone_args (tree decl)
4630 tree clone;
4632 for (clone = DECL_CHAIN (decl); clone && DECL_CLONED_FUNCTION_P (clone);
4633 clone = DECL_CHAIN (clone))
4635 tree orig_clone_parms = TYPE_ARG_TYPES (TREE_TYPE (clone));
4636 tree orig_decl_parms = TYPE_ARG_TYPES (TREE_TYPE (decl));
4637 tree decl_parms, clone_parms;
4639 clone_parms = orig_clone_parms;
4641 /* Skip the 'this' parameter. */
4642 orig_clone_parms = TREE_CHAIN (orig_clone_parms);
4643 orig_decl_parms = TREE_CHAIN (orig_decl_parms);
4645 if (DECL_HAS_IN_CHARGE_PARM_P (decl))
4646 orig_decl_parms = TREE_CHAIN (orig_decl_parms);
4647 if (DECL_HAS_VTT_PARM_P (decl))
4648 orig_decl_parms = TREE_CHAIN (orig_decl_parms);
4650 clone_parms = orig_clone_parms;
4651 if (DECL_HAS_VTT_PARM_P (clone))
4652 clone_parms = TREE_CHAIN (clone_parms);
4654 for (decl_parms = orig_decl_parms; decl_parms;
4655 decl_parms = TREE_CHAIN (decl_parms),
4656 clone_parms = TREE_CHAIN (clone_parms))
4658 gcc_assert (same_type_p (TREE_TYPE (decl_parms),
4659 TREE_TYPE (clone_parms)));
4661 if (TREE_PURPOSE (decl_parms) && !TREE_PURPOSE (clone_parms))
4663 /* A default parameter has been added. Adjust the
4664 clone's parameters. */
4665 tree exceptions = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (clone));
4666 tree attrs = TYPE_ATTRIBUTES (TREE_TYPE (clone));
4667 tree basetype = TYPE_METHOD_BASETYPE (TREE_TYPE (clone));
4668 tree type;
4670 clone_parms = orig_decl_parms;
4672 if (DECL_HAS_VTT_PARM_P (clone))
4674 clone_parms = tree_cons (TREE_PURPOSE (orig_clone_parms),
4675 TREE_VALUE (orig_clone_parms),
4676 clone_parms);
4677 TREE_TYPE (clone_parms) = TREE_TYPE (orig_clone_parms);
4679 type = build_method_type_directly (basetype,
4680 TREE_TYPE (TREE_TYPE (clone)),
4681 clone_parms);
4682 if (exceptions)
4683 type = build_exception_variant (type, exceptions);
4684 if (attrs)
4685 type = cp_build_type_attribute_variant (type, attrs);
4686 TREE_TYPE (clone) = type;
4688 clone_parms = NULL_TREE;
4689 break;
4692 gcc_assert (!clone_parms);
4696 /* For each of the constructors and destructors in T, create an
4697 in-charge and not-in-charge variant. */
4699 static void
4700 clone_constructors_and_destructors (tree t)
4702 tree fns;
4704 /* If for some reason we don't have a CLASSTYPE_METHOD_VEC, we bail
4705 out now. */
4706 if (!CLASSTYPE_METHOD_VEC (t))
4707 return;
4709 for (fns = CLASSTYPE_CONSTRUCTORS (t); fns; fns = OVL_NEXT (fns))
4710 clone_function_decl (OVL_CURRENT (fns), /*update_method_vec_p=*/1);
4711 for (fns = CLASSTYPE_DESTRUCTORS (t); fns; fns = OVL_NEXT (fns))
4712 clone_function_decl (OVL_CURRENT (fns), /*update_method_vec_p=*/1);
4715 /* Deduce noexcept for a destructor DTOR. */
4717 void
4718 deduce_noexcept_on_destructor (tree dtor)
4720 if (!TYPE_RAISES_EXCEPTIONS (TREE_TYPE (dtor)))
4722 tree eh_spec = unevaluated_noexcept_spec ();
4723 TREE_TYPE (dtor) = build_exception_variant (TREE_TYPE (dtor), eh_spec);
4727 /* For each destructor in T, deduce noexcept:
4729 12.4/3: A declaration of a destructor that does not have an
4730 exception-specification is implicitly considered to have the
4731 same exception-specification as an implicit declaration (15.4). */
4733 static void
4734 deduce_noexcept_on_destructors (tree t)
4736 /* If for some reason we don't have a CLASSTYPE_METHOD_VEC, we bail
4737 out now. */
4738 if (!CLASSTYPE_METHOD_VEC (t))
4739 return;
4741 for (tree fns = CLASSTYPE_DESTRUCTORS (t); fns; fns = OVL_NEXT (fns))
4742 deduce_noexcept_on_destructor (OVL_CURRENT (fns));
4745 /* Subroutine of set_one_vmethod_tm_attributes. Search base classes
4746 of TYPE for virtual functions which FNDECL overrides. Return a
4747 mask of the tm attributes found therein. */
4749 static int
4750 look_for_tm_attr_overrides (tree type, tree fndecl)
4752 tree binfo = TYPE_BINFO (type);
4753 tree base_binfo;
4754 int ix, found = 0;
4756 for (ix = 0; BINFO_BASE_ITERATE (binfo, ix, base_binfo); ++ix)
4758 tree o, basetype = BINFO_TYPE (base_binfo);
4760 if (!TYPE_POLYMORPHIC_P (basetype))
4761 continue;
4763 o = look_for_overrides_here (basetype, fndecl);
4764 if (o)
4765 found |= tm_attr_to_mask (find_tm_attribute
4766 (TYPE_ATTRIBUTES (TREE_TYPE (o))));
4767 else
4768 found |= look_for_tm_attr_overrides (basetype, fndecl);
4771 return found;
4774 /* Subroutine of set_method_tm_attributes. Handle the checks and
4775 inheritance for one virtual method FNDECL. */
4777 static void
4778 set_one_vmethod_tm_attributes (tree type, tree fndecl)
4780 tree tm_attr;
4781 int found, have;
4783 found = look_for_tm_attr_overrides (type, fndecl);
4785 /* If FNDECL doesn't actually override anything (i.e. T is the
4786 class that first declares FNDECL virtual), then we're done. */
4787 if (found == 0)
4788 return;
4790 tm_attr = find_tm_attribute (TYPE_ATTRIBUTES (TREE_TYPE (fndecl)));
4791 have = tm_attr_to_mask (tm_attr);
4793 /* Intel STM Language Extension 3.0, Section 4.2 table 4:
4794 tm_pure must match exactly, otherwise no weakening of
4795 tm_safe > tm_callable > nothing. */
4796 /* ??? The tm_pure attribute didn't make the transition to the
4797 multivendor language spec. */
4798 if (have == TM_ATTR_PURE)
4800 if (found != TM_ATTR_PURE)
4802 found &= -found;
4803 goto err_override;
4806 /* If the overridden function is tm_pure, then FNDECL must be. */
4807 else if (found == TM_ATTR_PURE && tm_attr)
4808 goto err_override;
4809 /* Look for base class combinations that cannot be satisfied. */
4810 else if (found != TM_ATTR_PURE && (found & TM_ATTR_PURE))
4812 found &= ~TM_ATTR_PURE;
4813 found &= -found;
4814 error_at (DECL_SOURCE_LOCATION (fndecl),
4815 "method overrides both %<transaction_pure%> and %qE methods",
4816 tm_mask_to_attr (found));
4818 /* If FNDECL did not declare an attribute, then inherit the most
4819 restrictive one. */
4820 else if (tm_attr == NULL)
4822 apply_tm_attr (fndecl, tm_mask_to_attr (found & -found));
4824 /* Otherwise validate that we're not weaker than a function
4825 that is being overridden. */
4826 else
4828 found &= -found;
4829 if (found <= TM_ATTR_CALLABLE && have > found)
4830 goto err_override;
4832 return;
4834 err_override:
4835 error_at (DECL_SOURCE_LOCATION (fndecl),
4836 "method declared %qE overriding %qE method",
4837 tm_attr, tm_mask_to_attr (found));
4840 /* For each of the methods in T, propagate a class-level tm attribute. */
4842 static void
4843 set_method_tm_attributes (tree t)
4845 tree class_tm_attr, fndecl;
4847 /* Don't bother collecting tm attributes if transactional memory
4848 support is not enabled. */
4849 if (!flag_tm)
4850 return;
4852 /* Process virtual methods first, as they inherit directly from the
4853 base virtual function and also require validation of new attributes. */
4854 if (TYPE_CONTAINS_VPTR_P (t))
4856 tree vchain;
4857 for (vchain = BINFO_VIRTUALS (TYPE_BINFO (t)); vchain;
4858 vchain = TREE_CHAIN (vchain))
4860 fndecl = BV_FN (vchain);
4861 if (DECL_THUNK_P (fndecl))
4862 fndecl = THUNK_TARGET (fndecl);
4863 set_one_vmethod_tm_attributes (t, fndecl);
4867 /* If the class doesn't have an attribute, nothing more to do. */
4868 class_tm_attr = find_tm_attribute (TYPE_ATTRIBUTES (t));
4869 if (class_tm_attr == NULL)
4870 return;
4872 /* Any method that does not yet have a tm attribute inherits
4873 the one from the class. */
4874 for (fndecl = TYPE_METHODS (t); fndecl; fndecl = TREE_CHAIN (fndecl))
4876 if (!find_tm_attribute (TYPE_ATTRIBUTES (TREE_TYPE (fndecl))))
4877 apply_tm_attr (fndecl, class_tm_attr);
4881 /* Returns true iff class T has a user-defined constructor other than
4882 the default constructor. */
4884 bool
4885 type_has_user_nondefault_constructor (tree t)
4887 tree fns;
4889 if (!TYPE_HAS_USER_CONSTRUCTOR (t))
4890 return false;
4892 for (fns = CLASSTYPE_CONSTRUCTORS (t); fns; fns = OVL_NEXT (fns))
4894 tree fn = OVL_CURRENT (fns);
4895 if (!DECL_ARTIFICIAL (fn)
4896 && (TREE_CODE (fn) == TEMPLATE_DECL
4897 || (skip_artificial_parms_for (fn, DECL_ARGUMENTS (fn))
4898 != NULL_TREE)))
4899 return true;
4902 return false;
4905 /* Returns the defaulted constructor if T has one. Otherwise, returns
4906 NULL_TREE. */
4908 tree
4909 in_class_defaulted_default_constructor (tree t)
4911 tree fns, args;
4913 if (!TYPE_HAS_USER_CONSTRUCTOR (t))
4914 return NULL_TREE;
4916 for (fns = CLASSTYPE_CONSTRUCTORS (t); fns; fns = OVL_NEXT (fns))
4918 tree fn = OVL_CURRENT (fns);
4920 if (DECL_DEFAULTED_IN_CLASS_P (fn))
4922 args = FUNCTION_FIRST_USER_PARMTYPE (fn);
4923 while (args && TREE_PURPOSE (args))
4924 args = TREE_CHAIN (args);
4925 if (!args || args == void_list_node)
4926 return fn;
4930 return NULL_TREE;
4933 /* Returns true iff FN is a user-provided function, i.e. user-declared
4934 and not defaulted at its first declaration; or explicit, private,
4935 protected, or non-const. */
4937 bool
4938 user_provided_p (tree fn)
4940 if (TREE_CODE (fn) == TEMPLATE_DECL)
4941 return true;
4942 else
4943 return (!DECL_ARTIFICIAL (fn)
4944 && !(DECL_INITIALIZED_IN_CLASS_P (fn)
4945 && (DECL_DEFAULTED_FN (fn) || DECL_DELETED_FN (fn))));
4948 /* Returns true iff class T has a user-provided constructor. */
4950 bool
4951 type_has_user_provided_constructor (tree t)
4953 tree fns;
4955 if (!CLASS_TYPE_P (t))
4956 return false;
4958 if (!TYPE_HAS_USER_CONSTRUCTOR (t))
4959 return false;
4961 /* This can happen in error cases; avoid crashing. */
4962 if (!CLASSTYPE_METHOD_VEC (t))
4963 return false;
4965 for (fns = CLASSTYPE_CONSTRUCTORS (t); fns; fns = OVL_NEXT (fns))
4966 if (user_provided_p (OVL_CURRENT (fns)))
4967 return true;
4969 return false;
4972 /* Returns true iff class T has a user-provided default constructor. */
4974 bool
4975 type_has_user_provided_default_constructor (tree t)
4977 tree fns;
4979 if (!TYPE_HAS_USER_CONSTRUCTOR (t))
4980 return false;
4982 for (fns = CLASSTYPE_CONSTRUCTORS (t); fns; fns = OVL_NEXT (fns))
4984 tree fn = OVL_CURRENT (fns);
4985 if (TREE_CODE (fn) == FUNCTION_DECL
4986 && user_provided_p (fn)
4987 && sufficient_parms_p (FUNCTION_FIRST_USER_PARMTYPE (fn)))
4988 return true;
4991 return false;
4994 /* TYPE is being used as a virtual base, and has a non-trivial move
4995 assignment. Return true if this is due to there being a user-provided
4996 move assignment in TYPE or one of its subobjects; if there isn't, then
4997 multiple move assignment can't cause any harm. */
4999 bool
5000 vbase_has_user_provided_move_assign (tree type)
5002 /* Does the type itself have a user-provided move assignment operator? */
5003 for (tree fns
5004 = lookup_fnfields_slot_nolazy (type, ansi_assopname (NOP_EXPR));
5005 fns; fns = OVL_NEXT (fns))
5007 tree fn = OVL_CURRENT (fns);
5008 if (move_fn_p (fn) && user_provided_p (fn))
5009 return true;
5012 /* Do any of its bases? */
5013 tree binfo = TYPE_BINFO (type);
5014 tree base_binfo;
5015 for (int i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
5016 if (vbase_has_user_provided_move_assign (BINFO_TYPE (base_binfo)))
5017 return true;
5019 /* Or non-static data members? */
5020 for (tree field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
5022 if (TREE_CODE (field) == FIELD_DECL
5023 && CLASS_TYPE_P (TREE_TYPE (field))
5024 && vbase_has_user_provided_move_assign (TREE_TYPE (field)))
5025 return true;
5028 /* Seems not. */
5029 return false;
5032 /* If default-initialization leaves part of TYPE uninitialized, returns
5033 a DECL for the field or TYPE itself (DR 253). */
5035 tree
5036 default_init_uninitialized_part (tree type)
5038 tree t, r, binfo;
5039 int i;
5041 type = strip_array_types (type);
5042 if (!CLASS_TYPE_P (type))
5043 return type;
5044 if (type_has_user_provided_default_constructor (type))
5045 return NULL_TREE;
5046 for (binfo = TYPE_BINFO (type), i = 0;
5047 BINFO_BASE_ITERATE (binfo, i, t); ++i)
5049 r = default_init_uninitialized_part (BINFO_TYPE (t));
5050 if (r)
5051 return r;
5053 for (t = TYPE_FIELDS (type); t; t = DECL_CHAIN (t))
5054 if (TREE_CODE (t) == FIELD_DECL
5055 && !DECL_ARTIFICIAL (t)
5056 && !DECL_INITIAL (t))
5058 r = default_init_uninitialized_part (TREE_TYPE (t));
5059 if (r)
5060 return DECL_P (r) ? r : t;
5063 return NULL_TREE;
5066 /* Returns true iff for class T, a trivial synthesized default constructor
5067 would be constexpr. */
5069 bool
5070 trivial_default_constructor_is_constexpr (tree t)
5072 /* A defaulted trivial default constructor is constexpr
5073 if there is nothing to initialize. */
5074 gcc_assert (!TYPE_HAS_COMPLEX_DFLT (t));
5075 return is_really_empty_class (t);
5078 /* Returns true iff class T has a constexpr default constructor. */
5080 bool
5081 type_has_constexpr_default_constructor (tree t)
5083 tree fns;
5085 if (!CLASS_TYPE_P (t))
5087 /* The caller should have stripped an enclosing array. */
5088 gcc_assert (TREE_CODE (t) != ARRAY_TYPE);
5089 return false;
5091 if (CLASSTYPE_LAZY_DEFAULT_CTOR (t))
5093 if (!TYPE_HAS_COMPLEX_DFLT (t))
5094 return trivial_default_constructor_is_constexpr (t);
5095 /* Non-trivial, we need to check subobject constructors. */
5096 lazily_declare_fn (sfk_constructor, t);
5098 fns = locate_ctor (t);
5099 return (fns && DECL_DECLARED_CONSTEXPR_P (fns));
5102 /* Returns true iff class TYPE has a virtual destructor. */
5104 bool
5105 type_has_virtual_destructor (tree type)
5107 tree dtor;
5109 if (!CLASS_TYPE_P (type))
5110 return false;
5112 gcc_assert (COMPLETE_TYPE_P (type));
5113 dtor = CLASSTYPE_DESTRUCTORS (type);
5114 return (dtor && DECL_VIRTUAL_P (dtor));
5117 /* Returns true iff class T has a move constructor. */
5119 bool
5120 type_has_move_constructor (tree t)
5122 tree fns;
5124 if (CLASSTYPE_LAZY_MOVE_CTOR (t))
5126 gcc_assert (COMPLETE_TYPE_P (t));
5127 lazily_declare_fn (sfk_move_constructor, t);
5130 if (!CLASSTYPE_METHOD_VEC (t))
5131 return false;
5133 for (fns = CLASSTYPE_CONSTRUCTORS (t); fns; fns = OVL_NEXT (fns))
5134 if (move_fn_p (OVL_CURRENT (fns)))
5135 return true;
5137 return false;
5140 /* Returns true iff class T has a move assignment operator. */
5142 bool
5143 type_has_move_assign (tree t)
5145 tree fns;
5147 if (CLASSTYPE_LAZY_MOVE_ASSIGN (t))
5149 gcc_assert (COMPLETE_TYPE_P (t));
5150 lazily_declare_fn (sfk_move_assignment, t);
5153 for (fns = lookup_fnfields_slot_nolazy (t, ansi_assopname (NOP_EXPR));
5154 fns; fns = OVL_NEXT (fns))
5155 if (move_fn_p (OVL_CURRENT (fns)))
5156 return true;
5158 return false;
5161 /* Returns true iff class T has a move constructor that was explicitly
5162 declared in the class body. Note that this is different from
5163 "user-provided", which doesn't include functions that are defaulted in
5164 the class. */
5166 bool
5167 type_has_user_declared_move_constructor (tree t)
5169 tree fns;
5171 if (CLASSTYPE_LAZY_MOVE_CTOR (t))
5172 return false;
5174 if (!CLASSTYPE_METHOD_VEC (t))
5175 return false;
5177 for (fns = CLASSTYPE_CONSTRUCTORS (t); fns; fns = OVL_NEXT (fns))
5179 tree fn = OVL_CURRENT (fns);
5180 if (move_fn_p (fn) && !DECL_ARTIFICIAL (fn))
5181 return true;
5184 return false;
5187 /* Returns true iff class T has a move assignment operator that was
5188 explicitly declared in the class body. */
5190 bool
5191 type_has_user_declared_move_assign (tree t)
5193 tree fns;
5195 if (CLASSTYPE_LAZY_MOVE_ASSIGN (t))
5196 return false;
5198 for (fns = lookup_fnfields_slot_nolazy (t, ansi_assopname (NOP_EXPR));
5199 fns; fns = OVL_NEXT (fns))
5201 tree fn = OVL_CURRENT (fns);
5202 if (move_fn_p (fn) && !DECL_ARTIFICIAL (fn))
5203 return true;
5206 return false;
5209 /* Nonzero if we need to build up a constructor call when initializing an
5210 object of this class, either because it has a user-declared constructor
5211 or because it doesn't have a default constructor (so we need to give an
5212 error if no initializer is provided). Use TYPE_NEEDS_CONSTRUCTING when
5213 what you care about is whether or not an object can be produced by a
5214 constructor (e.g. so we don't set TREE_READONLY on const variables of
5215 such type); use this function when what you care about is whether or not
5216 to try to call a constructor to create an object. The latter case is
5217 the former plus some cases of constructors that cannot be called. */
5219 bool
5220 type_build_ctor_call (tree t)
5222 tree inner;
5223 if (TYPE_NEEDS_CONSTRUCTING (t))
5224 return true;
5225 inner = strip_array_types (t);
5226 if (!CLASS_TYPE_P (inner) || ANON_AGGR_TYPE_P (inner))
5227 return false;
5228 if (!TYPE_HAS_DEFAULT_CONSTRUCTOR (inner))
5229 return true;
5230 if (cxx_dialect < cxx11)
5231 return false;
5232 /* A user-declared constructor might be private, and a constructor might
5233 be trivial but deleted. */
5234 for (tree fns = lookup_fnfields_slot (inner, complete_ctor_identifier);
5235 fns; fns = OVL_NEXT (fns))
5237 tree fn = OVL_CURRENT (fns);
5238 if (!DECL_ARTIFICIAL (fn)
5239 || DECL_DELETED_FN (fn))
5240 return true;
5242 return false;
5245 /* Like type_build_ctor_call, but for destructors. */
5247 bool
5248 type_build_dtor_call (tree t)
5250 tree inner;
5251 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t))
5252 return true;
5253 inner = strip_array_types (t);
5254 if (!CLASS_TYPE_P (inner) || ANON_AGGR_TYPE_P (inner)
5255 || !COMPLETE_TYPE_P (inner))
5256 return false;
5257 if (cxx_dialect < cxx11)
5258 return false;
5259 /* A user-declared destructor might be private, and a destructor might
5260 be trivial but deleted. */
5261 for (tree fns = lookup_fnfields_slot (inner, complete_dtor_identifier);
5262 fns; fns = OVL_NEXT (fns))
5264 tree fn = OVL_CURRENT (fns);
5265 if (!DECL_ARTIFICIAL (fn)
5266 || DECL_DELETED_FN (fn))
5267 return true;
5269 return false;
5272 /* Remove all zero-width bit-fields from T. */
5274 static void
5275 remove_zero_width_bit_fields (tree t)
5277 tree *fieldsp;
5279 fieldsp = &TYPE_FIELDS (t);
5280 while (*fieldsp)
5282 if (TREE_CODE (*fieldsp) == FIELD_DECL
5283 && DECL_C_BIT_FIELD (*fieldsp)
5284 /* We should not be confused by the fact that grokbitfield
5285 temporarily sets the width of the bit field into
5286 DECL_INITIAL (*fieldsp).
5287 check_bitfield_decl eventually sets DECL_SIZE (*fieldsp)
5288 to that width. */
5289 && integer_zerop (DECL_SIZE (*fieldsp)))
5290 *fieldsp = DECL_CHAIN (*fieldsp);
5291 else
5292 fieldsp = &DECL_CHAIN (*fieldsp);
5296 /* Returns TRUE iff we need a cookie when dynamically allocating an
5297 array whose elements have the indicated class TYPE. */
5299 static bool
5300 type_requires_array_cookie (tree type)
5302 tree fns;
5303 bool has_two_argument_delete_p = false;
5305 gcc_assert (CLASS_TYPE_P (type));
5307 /* If there's a non-trivial destructor, we need a cookie. In order
5308 to iterate through the array calling the destructor for each
5309 element, we'll have to know how many elements there are. */
5310 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
5311 return true;
5313 /* If the usual deallocation function is a two-argument whose second
5314 argument is of type `size_t', then we have to pass the size of
5315 the array to the deallocation function, so we will need to store
5316 a cookie. */
5317 fns = lookup_fnfields (TYPE_BINFO (type),
5318 ansi_opname (VEC_DELETE_EXPR),
5319 /*protect=*/0);
5320 /* If there are no `operator []' members, or the lookup is
5321 ambiguous, then we don't need a cookie. */
5322 if (!fns || fns == error_mark_node)
5323 return false;
5324 /* Loop through all of the functions. */
5325 for (fns = BASELINK_FUNCTIONS (fns); fns; fns = OVL_NEXT (fns))
5327 tree fn;
5328 tree second_parm;
5330 /* Select the current function. */
5331 fn = OVL_CURRENT (fns);
5332 /* See if this function is a one-argument delete function. If
5333 it is, then it will be the usual deallocation function. */
5334 second_parm = TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (fn)));
5335 if (second_parm == void_list_node)
5336 return false;
5337 /* Do not consider this function if its second argument is an
5338 ellipsis. */
5339 if (!second_parm)
5340 continue;
5341 /* Otherwise, if we have a two-argument function and the second
5342 argument is `size_t', it will be the usual deallocation
5343 function -- unless there is one-argument function, too. */
5344 if (TREE_CHAIN (second_parm) == void_list_node
5345 && same_type_p (TREE_VALUE (second_parm), size_type_node))
5346 has_two_argument_delete_p = true;
5349 return has_two_argument_delete_p;
5352 /* Finish computing the `literal type' property of class type T.
5354 At this point, we have already processed base classes and
5355 non-static data members. We need to check whether the copy
5356 constructor is trivial, the destructor is trivial, and there
5357 is a trivial default constructor or at least one constexpr
5358 constructor other than the copy constructor. */
5360 static void
5361 finalize_literal_type_property (tree t)
5363 tree fn;
5365 if (cxx_dialect < cxx11
5366 || TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t))
5367 CLASSTYPE_LITERAL_P (t) = false;
5368 else if (CLASSTYPE_LITERAL_P (t) && !TYPE_HAS_TRIVIAL_DFLT (t)
5369 && CLASSTYPE_NON_AGGREGATE (t)
5370 && !TYPE_HAS_CONSTEXPR_CTOR (t))
5371 CLASSTYPE_LITERAL_P (t) = false;
5373 if (!CLASSTYPE_LITERAL_P (t))
5374 for (fn = TYPE_METHODS (t); fn; fn = DECL_CHAIN (fn))
5375 if (DECL_DECLARED_CONSTEXPR_P (fn)
5376 && TREE_CODE (fn) != TEMPLATE_DECL
5377 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
5378 && !DECL_CONSTRUCTOR_P (fn))
5380 DECL_DECLARED_CONSTEXPR_P (fn) = false;
5381 if (!DECL_GENERATED_P (fn))
5383 error ("enclosing class of constexpr non-static member "
5384 "function %q+#D is not a literal type", fn);
5385 explain_non_literal_class (t);
5390 /* T is a non-literal type used in a context which requires a constant
5391 expression. Explain why it isn't literal. */
5393 void
5394 explain_non_literal_class (tree t)
5396 static struct pointer_set_t *diagnosed;
5398 if (!CLASS_TYPE_P (t))
5399 return;
5400 t = TYPE_MAIN_VARIANT (t);
5402 if (diagnosed == NULL)
5403 diagnosed = pointer_set_create ();
5404 if (pointer_set_insert (diagnosed, t) != 0)
5405 /* Already explained. */
5406 return;
5408 inform (0, "%q+T is not literal because:", t);
5409 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t))
5410 inform (0, " %q+T has a non-trivial destructor", t);
5411 else if (CLASSTYPE_NON_AGGREGATE (t)
5412 && !TYPE_HAS_TRIVIAL_DFLT (t)
5413 && !TYPE_HAS_CONSTEXPR_CTOR (t))
5415 inform (0, " %q+T is not an aggregate, does not have a trivial "
5416 "default constructor, and has no constexpr constructor that "
5417 "is not a copy or move constructor", t);
5418 if (TYPE_HAS_DEFAULT_CONSTRUCTOR (t)
5419 && !type_has_user_provided_default_constructor (t))
5421 /* Note that we can't simply call locate_ctor because when the
5422 constructor is deleted it just returns NULL_TREE. */
5423 tree fns;
5424 for (fns = CLASSTYPE_CONSTRUCTORS (t); fns; fns = OVL_NEXT (fns))
5426 tree fn = OVL_CURRENT (fns);
5427 tree parms = TYPE_ARG_TYPES (TREE_TYPE (fn));
5429 parms = skip_artificial_parms_for (fn, parms);
5431 if (sufficient_parms_p (parms))
5433 if (DECL_DELETED_FN (fn))
5434 maybe_explain_implicit_delete (fn);
5435 else
5436 explain_invalid_constexpr_fn (fn);
5437 break;
5442 else
5444 tree binfo, base_binfo, field; int i;
5445 for (binfo = TYPE_BINFO (t), i = 0;
5446 BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
5448 tree basetype = TREE_TYPE (base_binfo);
5449 if (!CLASSTYPE_LITERAL_P (basetype))
5451 inform (0, " base class %qT of %q+T is non-literal",
5452 basetype, t);
5453 explain_non_literal_class (basetype);
5454 return;
5457 for (field = TYPE_FIELDS (t); field; field = TREE_CHAIN (field))
5459 tree ftype;
5460 if (TREE_CODE (field) != FIELD_DECL)
5461 continue;
5462 ftype = TREE_TYPE (field);
5463 if (!literal_type_p (ftype))
5465 inform (0, " non-static data member %q+D has "
5466 "non-literal type", field);
5467 if (CLASS_TYPE_P (ftype))
5468 explain_non_literal_class (ftype);
5474 /* Check the validity of the bases and members declared in T. Add any
5475 implicitly-generated functions (like copy-constructors and
5476 assignment operators). Compute various flag bits (like
5477 CLASSTYPE_NON_LAYOUT_POD_T) for T. This routine works purely at the C++
5478 level: i.e., independently of the ABI in use. */
5480 static void
5481 check_bases_and_members (tree t)
5483 /* Nonzero if the implicitly generated copy constructor should take
5484 a non-const reference argument. */
5485 int cant_have_const_ctor;
5486 /* Nonzero if the implicitly generated assignment operator
5487 should take a non-const reference argument. */
5488 int no_const_asn_ref;
5489 tree access_decls;
5490 bool saved_complex_asn_ref;
5491 bool saved_nontrivial_dtor;
5492 tree fn;
5494 /* Pick up any abi_tags from our template arguments before checking. */
5495 inherit_targ_abi_tags (t);
5497 /* By default, we use const reference arguments and generate default
5498 constructors. */
5499 cant_have_const_ctor = 0;
5500 no_const_asn_ref = 0;
5502 /* Check all the base-classes. */
5503 check_bases (t, &cant_have_const_ctor,
5504 &no_const_asn_ref);
5506 /* Deduce noexcept on destructors. This needs to happen after we've set
5507 triviality flags appropriately for our bases. */
5508 if (cxx_dialect >= cxx11)
5509 deduce_noexcept_on_destructors (t);
5511 /* Check all the method declarations. */
5512 check_methods (t);
5514 /* Save the initial values of these flags which only indicate whether
5515 or not the class has user-provided functions. As we analyze the
5516 bases and members we can set these flags for other reasons. */
5517 saved_complex_asn_ref = TYPE_HAS_COMPLEX_COPY_ASSIGN (t);
5518 saved_nontrivial_dtor = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t);
5520 /* Check all the data member declarations. We cannot call
5521 check_field_decls until we have called check_bases check_methods,
5522 as check_field_decls depends on TYPE_HAS_NONTRIVIAL_DESTRUCTOR
5523 being set appropriately. */
5524 check_field_decls (t, &access_decls,
5525 &cant_have_const_ctor,
5526 &no_const_asn_ref);
5528 /* A nearly-empty class has to be vptr-containing; a nearly empty
5529 class contains just a vptr. */
5530 if (!TYPE_CONTAINS_VPTR_P (t))
5531 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
5533 /* Do some bookkeeping that will guide the generation of implicitly
5534 declared member functions. */
5535 TYPE_HAS_COMPLEX_COPY_CTOR (t) |= TYPE_CONTAINS_VPTR_P (t);
5536 TYPE_HAS_COMPLEX_MOVE_CTOR (t) |= TYPE_CONTAINS_VPTR_P (t);
5537 /* We need to call a constructor for this class if it has a
5538 user-provided constructor, or if the default constructor is going
5539 to initialize the vptr. (This is not an if-and-only-if;
5540 TYPE_NEEDS_CONSTRUCTING is set elsewhere if bases or members
5541 themselves need constructing.) */
5542 TYPE_NEEDS_CONSTRUCTING (t)
5543 |= (type_has_user_provided_constructor (t) || TYPE_CONTAINS_VPTR_P (t));
5544 /* [dcl.init.aggr]
5546 An aggregate is an array or a class with no user-provided
5547 constructors ... and no virtual functions.
5549 Again, other conditions for being an aggregate are checked
5550 elsewhere. */
5551 CLASSTYPE_NON_AGGREGATE (t)
5552 |= (type_has_user_provided_constructor (t) || TYPE_POLYMORPHIC_P (t));
5553 /* This is the C++98/03 definition of POD; it changed in C++0x, but we
5554 retain the old definition internally for ABI reasons. */
5555 CLASSTYPE_NON_LAYOUT_POD_P (t)
5556 |= (CLASSTYPE_NON_AGGREGATE (t)
5557 || saved_nontrivial_dtor || saved_complex_asn_ref);
5558 CLASSTYPE_NON_STD_LAYOUT (t) |= TYPE_CONTAINS_VPTR_P (t);
5559 TYPE_HAS_COMPLEX_COPY_ASSIGN (t) |= TYPE_CONTAINS_VPTR_P (t);
5560 TYPE_HAS_COMPLEX_MOVE_ASSIGN (t) |= TYPE_CONTAINS_VPTR_P (t);
5561 TYPE_HAS_COMPLEX_DFLT (t) |= TYPE_CONTAINS_VPTR_P (t);
5563 /* Warn if a public base of a polymorphic type has an accessible
5564 non-virtual destructor. It is only now that we know the class is
5565 polymorphic. Although a polymorphic base will have a already
5566 been diagnosed during its definition, we warn on use too. */
5567 if (TYPE_POLYMORPHIC_P (t) && warn_nonvdtor)
5569 tree binfo = TYPE_BINFO (t);
5570 vec<tree, va_gc> *accesses = BINFO_BASE_ACCESSES (binfo);
5571 tree base_binfo;
5572 unsigned i;
5574 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
5576 tree basetype = TREE_TYPE (base_binfo);
5578 if ((*accesses)[i] == access_public_node
5579 && (TYPE_POLYMORPHIC_P (basetype) || warn_ecpp)
5580 && accessible_nvdtor_p (basetype))
5581 warning (OPT_Wnon_virtual_dtor,
5582 "base class %q#T has accessible non-virtual destructor",
5583 basetype);
5587 /* If the class has no user-declared constructor, but does have
5588 non-static const or reference data members that can never be
5589 initialized, issue a warning. */
5590 if (warn_uninitialized
5591 /* Classes with user-declared constructors are presumed to
5592 initialize these members. */
5593 && !TYPE_HAS_USER_CONSTRUCTOR (t)
5594 /* Aggregates can be initialized with brace-enclosed
5595 initializers. */
5596 && CLASSTYPE_NON_AGGREGATE (t))
5598 tree field;
5600 for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
5602 tree type;
5604 if (TREE_CODE (field) != FIELD_DECL
5605 || DECL_INITIAL (field) != NULL_TREE)
5606 continue;
5608 type = TREE_TYPE (field);
5609 if (TREE_CODE (type) == REFERENCE_TYPE)
5610 warning (OPT_Wuninitialized, "non-static reference %q+#D "
5611 "in class without a constructor", field);
5612 else if (CP_TYPE_CONST_P (type)
5613 && (!CLASS_TYPE_P (type)
5614 || !TYPE_HAS_DEFAULT_CONSTRUCTOR (type)))
5615 warning (OPT_Wuninitialized, "non-static const member %q+#D "
5616 "in class without a constructor", field);
5620 /* Synthesize any needed methods. */
5621 add_implicitly_declared_members (t, &access_decls,
5622 cant_have_const_ctor,
5623 no_const_asn_ref);
5625 /* Check defaulted declarations here so we have cant_have_const_ctor
5626 and don't need to worry about clones. */
5627 for (fn = TYPE_METHODS (t); fn; fn = DECL_CHAIN (fn))
5628 if (!DECL_ARTIFICIAL (fn) && DECL_DEFAULTED_IN_CLASS_P (fn))
5630 int copy = copy_fn_p (fn);
5631 if (copy > 0)
5633 bool imp_const_p
5634 = (DECL_CONSTRUCTOR_P (fn) ? !cant_have_const_ctor
5635 : !no_const_asn_ref);
5636 bool fn_const_p = (copy == 2);
5638 if (fn_const_p && !imp_const_p)
5639 /* If the function is defaulted outside the class, we just
5640 give the synthesis error. */
5641 error ("%q+D declared to take const reference, but implicit "
5642 "declaration would take non-const", fn);
5644 defaulted_late_check (fn);
5647 if (LAMBDA_TYPE_P (t))
5649 /* "The closure type associated with a lambda-expression has a deleted
5650 default constructor and a deleted copy assignment operator." */
5651 TYPE_NEEDS_CONSTRUCTING (t) = 1;
5652 TYPE_HAS_COMPLEX_DFLT (t) = 1;
5653 TYPE_HAS_COMPLEX_COPY_ASSIGN (t) = 1;
5654 CLASSTYPE_LAZY_MOVE_ASSIGN (t) = 0;
5656 /* "This class type is not an aggregate." */
5657 CLASSTYPE_NON_AGGREGATE (t) = 1;
5660 /* Compute the 'literal type' property before we
5661 do anything with non-static member functions. */
5662 finalize_literal_type_property (t);
5664 /* Create the in-charge and not-in-charge variants of constructors
5665 and destructors. */
5666 clone_constructors_and_destructors (t);
5668 /* Process the using-declarations. */
5669 for (; access_decls; access_decls = TREE_CHAIN (access_decls))
5670 handle_using_decl (TREE_VALUE (access_decls), t);
5672 /* Build and sort the CLASSTYPE_METHOD_VEC. */
5673 finish_struct_methods (t);
5675 /* Figure out whether or not we will need a cookie when dynamically
5676 allocating an array of this type. */
5677 TYPE_LANG_SPECIFIC (t)->u.c.vec_new_uses_cookie
5678 = type_requires_array_cookie (t);
5681 /* If T needs a pointer to its virtual function table, set TYPE_VFIELD
5682 accordingly. If a new vfield was created (because T doesn't have a
5683 primary base class), then the newly created field is returned. It
5684 is not added to the TYPE_FIELDS list; it is the caller's
5685 responsibility to do that. Accumulate declared virtual functions
5686 on VIRTUALS_P. */
5688 static tree
5689 create_vtable_ptr (tree t, tree* virtuals_p)
5691 tree fn;
5693 /* Collect the virtual functions declared in T. */
5694 for (fn = TYPE_METHODS (t); fn; fn = DECL_CHAIN (fn))
5695 if (TREE_CODE (fn) == FUNCTION_DECL
5696 && DECL_VINDEX (fn) && !DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (fn)
5697 && TREE_CODE (DECL_VINDEX (fn)) != INTEGER_CST)
5699 tree new_virtual = make_node (TREE_LIST);
5701 BV_FN (new_virtual) = fn;
5702 BV_DELTA (new_virtual) = integer_zero_node;
5703 BV_VCALL_INDEX (new_virtual) = NULL_TREE;
5705 TREE_CHAIN (new_virtual) = *virtuals_p;
5706 *virtuals_p = new_virtual;
5709 /* If we couldn't find an appropriate base class, create a new field
5710 here. Even if there weren't any new virtual functions, we might need a
5711 new virtual function table if we're supposed to include vptrs in
5712 all classes that need them. */
5713 if (!TYPE_VFIELD (t) && (*virtuals_p || TYPE_CONTAINS_VPTR_P (t)))
5715 /* We build this decl with vtbl_ptr_type_node, which is a
5716 `vtable_entry_type*'. It might seem more precise to use
5717 `vtable_entry_type (*)[N]' where N is the number of virtual
5718 functions. However, that would require the vtable pointer in
5719 base classes to have a different type than the vtable pointer
5720 in derived classes. We could make that happen, but that
5721 still wouldn't solve all the problems. In particular, the
5722 type-based alias analysis code would decide that assignments
5723 to the base class vtable pointer can't alias assignments to
5724 the derived class vtable pointer, since they have different
5725 types. Thus, in a derived class destructor, where the base
5726 class constructor was inlined, we could generate bad code for
5727 setting up the vtable pointer.
5729 Therefore, we use one type for all vtable pointers. We still
5730 use a type-correct type; it's just doesn't indicate the array
5731 bounds. That's better than using `void*' or some such; it's
5732 cleaner, and it let's the alias analysis code know that these
5733 stores cannot alias stores to void*! */
5734 tree field;
5736 field = build_decl (input_location,
5737 FIELD_DECL, get_vfield_name (t), vtbl_ptr_type_node);
5738 DECL_VIRTUAL_P (field) = 1;
5739 DECL_ARTIFICIAL (field) = 1;
5740 DECL_FIELD_CONTEXT (field) = t;
5741 DECL_FCONTEXT (field) = t;
5742 if (TYPE_PACKED (t))
5743 DECL_PACKED (field) = 1;
5745 TYPE_VFIELD (t) = field;
5747 /* This class is non-empty. */
5748 CLASSTYPE_EMPTY_P (t) = 0;
5750 return field;
5753 return NULL_TREE;
5756 /* Add OFFSET to all base types of BINFO which is a base in the
5757 hierarchy dominated by T.
5759 OFFSET, which is a type offset, is number of bytes. */
5761 static void
5762 propagate_binfo_offsets (tree binfo, tree offset)
5764 int i;
5765 tree primary_binfo;
5766 tree base_binfo;
5768 /* Update BINFO's offset. */
5769 BINFO_OFFSET (binfo)
5770 = convert (sizetype,
5771 size_binop (PLUS_EXPR,
5772 convert (ssizetype, BINFO_OFFSET (binfo)),
5773 offset));
5775 /* Find the primary base class. */
5776 primary_binfo = get_primary_binfo (binfo);
5778 if (primary_binfo && BINFO_INHERITANCE_CHAIN (primary_binfo) == binfo)
5779 propagate_binfo_offsets (primary_binfo, offset);
5781 /* Scan all of the bases, pushing the BINFO_OFFSET adjust
5782 downwards. */
5783 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
5785 /* Don't do the primary base twice. */
5786 if (base_binfo == primary_binfo)
5787 continue;
5789 if (BINFO_VIRTUAL_P (base_binfo))
5790 continue;
5792 propagate_binfo_offsets (base_binfo, offset);
5796 /* Set BINFO_OFFSET for all of the virtual bases for RLI->T. Update
5797 TYPE_ALIGN and TYPE_SIZE for T. OFFSETS gives the location of
5798 empty subobjects of T. */
5800 static void
5801 layout_virtual_bases (record_layout_info rli, splay_tree offsets)
5803 tree vbase;
5804 tree t = rli->t;
5805 tree *next_field;
5807 if (BINFO_N_BASE_BINFOS (TYPE_BINFO (t)) == 0)
5808 return;
5810 /* Find the last field. The artificial fields created for virtual
5811 bases will go after the last extant field to date. */
5812 next_field = &TYPE_FIELDS (t);
5813 while (*next_field)
5814 next_field = &DECL_CHAIN (*next_field);
5816 /* Go through the virtual bases, allocating space for each virtual
5817 base that is not already a primary base class. These are
5818 allocated in inheritance graph order. */
5819 for (vbase = TYPE_BINFO (t); vbase; vbase = TREE_CHAIN (vbase))
5821 if (!BINFO_VIRTUAL_P (vbase))
5822 continue;
5824 if (!BINFO_PRIMARY_P (vbase))
5826 /* This virtual base is not a primary base of any class in the
5827 hierarchy, so we have to add space for it. */
5828 next_field = build_base_field (rli, vbase,
5829 offsets, next_field);
5834 /* Returns the offset of the byte just past the end of the base class
5835 BINFO. */
5837 static tree
5838 end_of_base (tree binfo)
5840 tree size;
5842 if (!CLASSTYPE_AS_BASE (BINFO_TYPE (binfo)))
5843 size = TYPE_SIZE_UNIT (char_type_node);
5844 else if (is_empty_class (BINFO_TYPE (binfo)))
5845 /* An empty class has zero CLASSTYPE_SIZE_UNIT, but we need to
5846 allocate some space for it. It cannot have virtual bases, so
5847 TYPE_SIZE_UNIT is fine. */
5848 size = TYPE_SIZE_UNIT (BINFO_TYPE (binfo));
5849 else
5850 size = CLASSTYPE_SIZE_UNIT (BINFO_TYPE (binfo));
5852 return size_binop (PLUS_EXPR, BINFO_OFFSET (binfo), size);
5855 /* Returns the offset of the byte just past the end of the base class
5856 with the highest offset in T. If INCLUDE_VIRTUALS_P is zero, then
5857 only non-virtual bases are included. */
5859 static tree
5860 end_of_class (tree t, int include_virtuals_p)
5862 tree result = size_zero_node;
5863 vec<tree, va_gc> *vbases;
5864 tree binfo;
5865 tree base_binfo;
5866 tree offset;
5867 int i;
5869 for (binfo = TYPE_BINFO (t), i = 0;
5870 BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
5872 if (!include_virtuals_p
5873 && BINFO_VIRTUAL_P (base_binfo)
5874 && (!BINFO_PRIMARY_P (base_binfo)
5875 || BINFO_INHERITANCE_CHAIN (base_binfo) != TYPE_BINFO (t)))
5876 continue;
5878 offset = end_of_base (base_binfo);
5879 if (tree_int_cst_lt (result, offset))
5880 result = offset;
5883 if (include_virtuals_p)
5884 for (vbases = CLASSTYPE_VBASECLASSES (t), i = 0;
5885 vec_safe_iterate (vbases, i, &base_binfo); i++)
5887 offset = end_of_base (base_binfo);
5888 if (tree_int_cst_lt (result, offset))
5889 result = offset;
5892 return result;
5895 /* Warn about bases of T that are inaccessible because they are
5896 ambiguous. For example:
5898 struct S {};
5899 struct T : public S {};
5900 struct U : public S, public T {};
5902 Here, `(S*) new U' is not allowed because there are two `S'
5903 subobjects of U. */
5905 static void
5906 warn_about_ambiguous_bases (tree t)
5908 int i;
5909 vec<tree, va_gc> *vbases;
5910 tree basetype;
5911 tree binfo;
5912 tree base_binfo;
5914 /* If there are no repeated bases, nothing can be ambiguous. */
5915 if (!CLASSTYPE_REPEATED_BASE_P (t))
5916 return;
5918 /* Check direct bases. */
5919 for (binfo = TYPE_BINFO (t), i = 0;
5920 BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
5922 basetype = BINFO_TYPE (base_binfo);
5924 if (!uniquely_derived_from_p (basetype, t))
5925 warning (0, "direct base %qT inaccessible in %qT due to ambiguity",
5926 basetype, t);
5929 /* Check for ambiguous virtual bases. */
5930 if (extra_warnings)
5931 for (vbases = CLASSTYPE_VBASECLASSES (t), i = 0;
5932 vec_safe_iterate (vbases, i, &binfo); i++)
5934 basetype = BINFO_TYPE (binfo);
5936 if (!uniquely_derived_from_p (basetype, t))
5937 warning (OPT_Wextra, "virtual base %qT inaccessible in %qT due "
5938 "to ambiguity", basetype, t);
5942 /* Compare two INTEGER_CSTs K1 and K2. */
5944 static int
5945 splay_tree_compare_integer_csts (splay_tree_key k1, splay_tree_key k2)
5947 return tree_int_cst_compare ((tree) k1, (tree) k2);
5950 /* Increase the size indicated in RLI to account for empty classes
5951 that are "off the end" of the class. */
5953 static void
5954 include_empty_classes (record_layout_info rli)
5956 tree eoc;
5957 tree rli_size;
5959 /* It might be the case that we grew the class to allocate a
5960 zero-sized base class. That won't be reflected in RLI, yet,
5961 because we are willing to overlay multiple bases at the same
5962 offset. However, now we need to make sure that RLI is big enough
5963 to reflect the entire class. */
5964 eoc = end_of_class (rli->t,
5965 CLASSTYPE_AS_BASE (rli->t) != NULL_TREE);
5966 rli_size = rli_size_unit_so_far (rli);
5967 if (TREE_CODE (rli_size) == INTEGER_CST
5968 && tree_int_cst_lt (rli_size, eoc))
5970 /* The size should have been rounded to a whole byte. */
5971 gcc_assert (tree_int_cst_equal
5972 (rli->bitpos, round_down (rli->bitpos, BITS_PER_UNIT)));
5973 rli->bitpos
5974 = size_binop (PLUS_EXPR,
5975 rli->bitpos,
5976 size_binop (MULT_EXPR,
5977 convert (bitsizetype,
5978 size_binop (MINUS_EXPR,
5979 eoc, rli_size)),
5980 bitsize_int (BITS_PER_UNIT)));
5981 normalize_rli (rli);
5985 /* Calculate the TYPE_SIZE, TYPE_ALIGN, etc for T. Calculate
5986 BINFO_OFFSETs for all of the base-classes. Position the vtable
5987 pointer. Accumulate declared virtual functions on VIRTUALS_P. */
5989 static void
5990 layout_class_type (tree t, tree *virtuals_p)
5992 tree non_static_data_members;
5993 tree field;
5994 tree vptr;
5995 record_layout_info rli;
5996 /* Maps offsets (represented as INTEGER_CSTs) to a TREE_LIST of
5997 types that appear at that offset. */
5998 splay_tree empty_base_offsets;
5999 /* True if the last field laid out was a bit-field. */
6000 bool last_field_was_bitfield = false;
6001 /* The location at which the next field should be inserted. */
6002 tree *next_field;
6003 /* T, as a base class. */
6004 tree base_t;
6006 /* Keep track of the first non-static data member. */
6007 non_static_data_members = TYPE_FIELDS (t);
6009 /* Start laying out the record. */
6010 rli = start_record_layout (t);
6012 /* Mark all the primary bases in the hierarchy. */
6013 determine_primary_bases (t);
6015 /* Create a pointer to our virtual function table. */
6016 vptr = create_vtable_ptr (t, virtuals_p);
6018 /* The vptr is always the first thing in the class. */
6019 if (vptr)
6021 DECL_CHAIN (vptr) = TYPE_FIELDS (t);
6022 TYPE_FIELDS (t) = vptr;
6023 next_field = &DECL_CHAIN (vptr);
6024 place_field (rli, vptr);
6026 else
6027 next_field = &TYPE_FIELDS (t);
6029 /* Build FIELD_DECLs for all of the non-virtual base-types. */
6030 empty_base_offsets = splay_tree_new (splay_tree_compare_integer_csts,
6031 NULL, NULL);
6032 build_base_fields (rli, empty_base_offsets, next_field);
6034 /* Layout the non-static data members. */
6035 for (field = non_static_data_members; field; field = DECL_CHAIN (field))
6037 tree type;
6038 tree padding;
6040 /* We still pass things that aren't non-static data members to
6041 the back end, in case it wants to do something with them. */
6042 if (TREE_CODE (field) != FIELD_DECL)
6044 place_field (rli, field);
6045 /* If the static data member has incomplete type, keep track
6046 of it so that it can be completed later. (The handling
6047 of pending statics in finish_record_layout is
6048 insufficient; consider:
6050 struct S1;
6051 struct S2 { static S1 s1; };
6053 At this point, finish_record_layout will be called, but
6054 S1 is still incomplete.) */
6055 if (VAR_P (field))
6057 maybe_register_incomplete_var (field);
6058 /* The visibility of static data members is determined
6059 at their point of declaration, not their point of
6060 definition. */
6061 determine_visibility (field);
6063 continue;
6066 type = TREE_TYPE (field);
6067 if (type == error_mark_node)
6068 continue;
6070 padding = NULL_TREE;
6072 /* If this field is a bit-field whose width is greater than its
6073 type, then there are some special rules for allocating
6074 it. */
6075 if (DECL_C_BIT_FIELD (field)
6076 && tree_int_cst_lt (TYPE_SIZE (type), DECL_SIZE (field)))
6078 unsigned int itk;
6079 tree integer_type;
6080 bool was_unnamed_p = false;
6081 /* We must allocate the bits as if suitably aligned for the
6082 longest integer type that fits in this many bits. type
6083 of the field. Then, we are supposed to use the left over
6084 bits as additional padding. */
6085 for (itk = itk_char; itk != itk_none; ++itk)
6086 if (integer_types[itk] != NULL_TREE
6087 && (tree_int_cst_lt (size_int (MAX_FIXED_MODE_SIZE),
6088 TYPE_SIZE (integer_types[itk]))
6089 || tree_int_cst_lt (DECL_SIZE (field),
6090 TYPE_SIZE (integer_types[itk]))))
6091 break;
6093 /* ITK now indicates a type that is too large for the
6094 field. We have to back up by one to find the largest
6095 type that fits. */
6098 --itk;
6099 integer_type = integer_types[itk];
6100 } while (itk > 0 && integer_type == NULL_TREE);
6102 /* Figure out how much additional padding is required. */
6103 if (tree_int_cst_lt (TYPE_SIZE (integer_type), DECL_SIZE (field)))
6105 if (TREE_CODE (t) == UNION_TYPE)
6106 /* In a union, the padding field must have the full width
6107 of the bit-field; all fields start at offset zero. */
6108 padding = DECL_SIZE (field);
6109 else
6110 padding = size_binop (MINUS_EXPR, DECL_SIZE (field),
6111 TYPE_SIZE (integer_type));
6113 #ifdef PCC_BITFIELD_TYPE_MATTERS
6114 /* An unnamed bitfield does not normally affect the
6115 alignment of the containing class on a target where
6116 PCC_BITFIELD_TYPE_MATTERS. But, the C++ ABI does not
6117 make any exceptions for unnamed bitfields when the
6118 bitfields are longer than their types. Therefore, we
6119 temporarily give the field a name. */
6120 if (PCC_BITFIELD_TYPE_MATTERS && !DECL_NAME (field))
6122 was_unnamed_p = true;
6123 DECL_NAME (field) = make_anon_name ();
6125 #endif
6126 DECL_SIZE (field) = TYPE_SIZE (integer_type);
6127 DECL_ALIGN (field) = TYPE_ALIGN (integer_type);
6128 DECL_USER_ALIGN (field) = TYPE_USER_ALIGN (integer_type);
6129 layout_nonempty_base_or_field (rli, field, NULL_TREE,
6130 empty_base_offsets);
6131 if (was_unnamed_p)
6132 DECL_NAME (field) = NULL_TREE;
6133 /* Now that layout has been performed, set the size of the
6134 field to the size of its declared type; the rest of the
6135 field is effectively invisible. */
6136 DECL_SIZE (field) = TYPE_SIZE (type);
6137 /* We must also reset the DECL_MODE of the field. */
6138 DECL_MODE (field) = TYPE_MODE (type);
6140 else
6141 layout_nonempty_base_or_field (rli, field, NULL_TREE,
6142 empty_base_offsets);
6144 /* Remember the location of any empty classes in FIELD. */
6145 record_subobject_offsets (TREE_TYPE (field),
6146 byte_position(field),
6147 empty_base_offsets,
6148 /*is_data_member=*/true);
6150 /* If a bit-field does not immediately follow another bit-field,
6151 and yet it starts in the middle of a byte, we have failed to
6152 comply with the ABI. */
6153 if (warn_abi
6154 && DECL_C_BIT_FIELD (field)
6155 /* The TREE_NO_WARNING flag gets set by Objective-C when
6156 laying out an Objective-C class. The ObjC ABI differs
6157 from the C++ ABI, and so we do not want a warning
6158 here. */
6159 && !TREE_NO_WARNING (field)
6160 && !last_field_was_bitfield
6161 && !integer_zerop (size_binop (TRUNC_MOD_EXPR,
6162 DECL_FIELD_BIT_OFFSET (field),
6163 bitsize_unit_node)))
6164 warning (OPT_Wabi, "offset of %q+D is not ABI-compliant and may "
6165 "change in a future version of GCC", field);
6167 /* The middle end uses the type of expressions to determine the
6168 possible range of expression values. In order to optimize
6169 "x.i > 7" to "false" for a 2-bit bitfield "i", the middle end
6170 must be made aware of the width of "i", via its type.
6172 Because C++ does not have integer types of arbitrary width,
6173 we must (for the purposes of the front end) convert from the
6174 type assigned here to the declared type of the bitfield
6175 whenever a bitfield expression is used as an rvalue.
6176 Similarly, when assigning a value to a bitfield, the value
6177 must be converted to the type given the bitfield here. */
6178 if (DECL_C_BIT_FIELD (field))
6180 unsigned HOST_WIDE_INT width;
6181 tree ftype = TREE_TYPE (field);
6182 width = tree_to_uhwi (DECL_SIZE (field));
6183 if (width != TYPE_PRECISION (ftype))
6185 TREE_TYPE (field)
6186 = c_build_bitfield_integer_type (width,
6187 TYPE_UNSIGNED (ftype));
6188 TREE_TYPE (field)
6189 = cp_build_qualified_type (TREE_TYPE (field),
6190 cp_type_quals (ftype));
6194 /* If we needed additional padding after this field, add it
6195 now. */
6196 if (padding)
6198 tree padding_field;
6200 padding_field = build_decl (input_location,
6201 FIELD_DECL,
6202 NULL_TREE,
6203 char_type_node);
6204 DECL_BIT_FIELD (padding_field) = 1;
6205 DECL_SIZE (padding_field) = padding;
6206 DECL_CONTEXT (padding_field) = t;
6207 DECL_ARTIFICIAL (padding_field) = 1;
6208 DECL_IGNORED_P (padding_field) = 1;
6209 layout_nonempty_base_or_field (rli, padding_field,
6210 NULL_TREE,
6211 empty_base_offsets);
6214 last_field_was_bitfield = DECL_C_BIT_FIELD (field);
6217 if (!integer_zerop (rli->bitpos))
6219 /* Make sure that we are on a byte boundary so that the size of
6220 the class without virtual bases will always be a round number
6221 of bytes. */
6222 rli->bitpos = round_up_loc (input_location, rli->bitpos, BITS_PER_UNIT);
6223 normalize_rli (rli);
6226 /* Delete all zero-width bit-fields from the list of fields. Now
6227 that the type is laid out they are no longer important. */
6228 remove_zero_width_bit_fields (t);
6230 /* Create the version of T used for virtual bases. We do not use
6231 make_class_type for this version; this is an artificial type. For
6232 a POD type, we just reuse T. */
6233 if (CLASSTYPE_NON_LAYOUT_POD_P (t) || CLASSTYPE_EMPTY_P (t))
6235 base_t = make_node (TREE_CODE (t));
6237 /* Set the size and alignment for the new type. */
6238 tree eoc;
6240 /* If the ABI version is not at least two, and the last
6241 field was a bit-field, RLI may not be on a byte
6242 boundary. In particular, rli_size_unit_so_far might
6243 indicate the last complete byte, while rli_size_so_far
6244 indicates the total number of bits used. Therefore,
6245 rli_size_so_far, rather than rli_size_unit_so_far, is
6246 used to compute TYPE_SIZE_UNIT. */
6247 eoc = end_of_class (t, /*include_virtuals_p=*/0);
6248 TYPE_SIZE_UNIT (base_t)
6249 = size_binop (MAX_EXPR,
6250 convert (sizetype,
6251 size_binop (CEIL_DIV_EXPR,
6252 rli_size_so_far (rli),
6253 bitsize_int (BITS_PER_UNIT))),
6254 eoc);
6255 TYPE_SIZE (base_t)
6256 = size_binop (MAX_EXPR,
6257 rli_size_so_far (rli),
6258 size_binop (MULT_EXPR,
6259 convert (bitsizetype, eoc),
6260 bitsize_int (BITS_PER_UNIT)));
6261 TYPE_ALIGN (base_t) = rli->record_align;
6262 TYPE_USER_ALIGN (base_t) = TYPE_USER_ALIGN (t);
6264 /* Copy the fields from T. */
6265 next_field = &TYPE_FIELDS (base_t);
6266 for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
6267 if (TREE_CODE (field) == FIELD_DECL)
6269 *next_field = build_decl (input_location,
6270 FIELD_DECL,
6271 DECL_NAME (field),
6272 TREE_TYPE (field));
6273 DECL_CONTEXT (*next_field) = base_t;
6274 DECL_FIELD_OFFSET (*next_field) = DECL_FIELD_OFFSET (field);
6275 DECL_FIELD_BIT_OFFSET (*next_field)
6276 = DECL_FIELD_BIT_OFFSET (field);
6277 DECL_SIZE (*next_field) = DECL_SIZE (field);
6278 DECL_MODE (*next_field) = DECL_MODE (field);
6279 next_field = &DECL_CHAIN (*next_field);
6282 /* Record the base version of the type. */
6283 CLASSTYPE_AS_BASE (t) = base_t;
6284 TYPE_CONTEXT (base_t) = t;
6286 else
6287 CLASSTYPE_AS_BASE (t) = t;
6289 /* Every empty class contains an empty class. */
6290 if (CLASSTYPE_EMPTY_P (t))
6291 CLASSTYPE_CONTAINS_EMPTY_CLASS_P (t) = 1;
6293 /* Set the TYPE_DECL for this type to contain the right
6294 value for DECL_OFFSET, so that we can use it as part
6295 of a COMPONENT_REF for multiple inheritance. */
6296 layout_decl (TYPE_MAIN_DECL (t), 0);
6298 /* Now fix up any virtual base class types that we left lying
6299 around. We must get these done before we try to lay out the
6300 virtual function table. As a side-effect, this will remove the
6301 base subobject fields. */
6302 layout_virtual_bases (rli, empty_base_offsets);
6304 /* Make sure that empty classes are reflected in RLI at this
6305 point. */
6306 include_empty_classes(rli);
6308 /* Make sure not to create any structures with zero size. */
6309 if (integer_zerop (rli_size_unit_so_far (rli)) && CLASSTYPE_EMPTY_P (t))
6310 place_field (rli,
6311 build_decl (input_location,
6312 FIELD_DECL, NULL_TREE, char_type_node));
6314 /* If this is a non-POD, declaring it packed makes a difference to how it
6315 can be used as a field; don't let finalize_record_size undo it. */
6316 if (TYPE_PACKED (t) && !layout_pod_type_p (t))
6317 rli->packed_maybe_necessary = true;
6319 /* Let the back end lay out the type. */
6320 finish_record_layout (rli, /*free_p=*/true);
6322 if (TYPE_SIZE_UNIT (t)
6323 && TREE_CODE (TYPE_SIZE_UNIT (t)) == INTEGER_CST
6324 && !TREE_OVERFLOW (TYPE_SIZE_UNIT (t))
6325 && !valid_constant_size_p (TYPE_SIZE_UNIT (t)))
6326 error ("type %qT is too large", t);
6328 /* Warn about bases that can't be talked about due to ambiguity. */
6329 warn_about_ambiguous_bases (t);
6331 /* Now that we're done with layout, give the base fields the real types. */
6332 for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
6333 if (DECL_ARTIFICIAL (field) && IS_FAKE_BASE_TYPE (TREE_TYPE (field)))
6334 TREE_TYPE (field) = TYPE_CONTEXT (TREE_TYPE (field));
6336 /* Clean up. */
6337 splay_tree_delete (empty_base_offsets);
6339 if (CLASSTYPE_EMPTY_P (t)
6340 && tree_int_cst_lt (sizeof_biggest_empty_class,
6341 TYPE_SIZE_UNIT (t)))
6342 sizeof_biggest_empty_class = TYPE_SIZE_UNIT (t);
6345 /* Determine the "key method" for the class type indicated by TYPE,
6346 and set CLASSTYPE_KEY_METHOD accordingly. */
6348 void
6349 determine_key_method (tree type)
6351 tree method;
6353 if (TYPE_FOR_JAVA (type)
6354 || processing_template_decl
6355 || CLASSTYPE_TEMPLATE_INSTANTIATION (type)
6356 || CLASSTYPE_INTERFACE_KNOWN (type))
6357 return;
6359 /* The key method is the first non-pure virtual function that is not
6360 inline at the point of class definition. On some targets the
6361 key function may not be inline; those targets should not call
6362 this function until the end of the translation unit. */
6363 for (method = TYPE_METHODS (type); method != NULL_TREE;
6364 method = DECL_CHAIN (method))
6365 if (TREE_CODE (method) == FUNCTION_DECL
6366 && DECL_VINDEX (method) != NULL_TREE
6367 && ! DECL_DECLARED_INLINE_P (method)
6368 && ! DECL_PURE_VIRTUAL_P (method))
6370 CLASSTYPE_KEY_METHOD (type) = method;
6371 break;
6374 return;
6378 /* Allocate and return an instance of struct sorted_fields_type with
6379 N fields. */
6381 static struct sorted_fields_type *
6382 sorted_fields_type_new (int n)
6384 struct sorted_fields_type *sft;
6385 sft = (sorted_fields_type *) ggc_internal_alloc (sizeof (sorted_fields_type)
6386 + n * sizeof (tree));
6387 sft->len = n;
6389 return sft;
6393 /* Perform processing required when the definition of T (a class type)
6394 is complete. */
6396 void
6397 finish_struct_1 (tree t)
6399 tree x;
6400 /* A TREE_LIST. The TREE_VALUE of each node is a FUNCTION_DECL. */
6401 tree virtuals = NULL_TREE;
6403 if (COMPLETE_TYPE_P (t))
6405 gcc_assert (MAYBE_CLASS_TYPE_P (t));
6406 error ("redefinition of %q#T", t);
6407 popclass ();
6408 return;
6411 /* If this type was previously laid out as a forward reference,
6412 make sure we lay it out again. */
6413 TYPE_SIZE (t) = NULL_TREE;
6414 CLASSTYPE_PRIMARY_BINFO (t) = NULL_TREE;
6416 /* Make assumptions about the class; we'll reset the flags if
6417 necessary. */
6418 CLASSTYPE_EMPTY_P (t) = 1;
6419 CLASSTYPE_NEARLY_EMPTY_P (t) = 1;
6420 CLASSTYPE_CONTAINS_EMPTY_CLASS_P (t) = 0;
6421 CLASSTYPE_LITERAL_P (t) = true;
6423 /* Do end-of-class semantic processing: checking the validity of the
6424 bases and members and add implicitly generated methods. */
6425 check_bases_and_members (t);
6427 /* Find the key method. */
6428 if (TYPE_CONTAINS_VPTR_P (t))
6430 /* The Itanium C++ ABI permits the key method to be chosen when
6431 the class is defined -- even though the key method so
6432 selected may later turn out to be an inline function. On
6433 some systems (such as ARM Symbian OS) the key method cannot
6434 be determined until the end of the translation unit. On such
6435 systems, we leave CLASSTYPE_KEY_METHOD set to NULL, which
6436 will cause the class to be added to KEYED_CLASSES. Then, in
6437 finish_file we will determine the key method. */
6438 if (targetm.cxx.key_method_may_be_inline ())
6439 determine_key_method (t);
6441 /* If a polymorphic class has no key method, we may emit the vtable
6442 in every translation unit where the class definition appears. */
6443 if (CLASSTYPE_KEY_METHOD (t) == NULL_TREE)
6444 keyed_classes = tree_cons (NULL_TREE, t, keyed_classes);
6447 /* Layout the class itself. */
6448 layout_class_type (t, &virtuals);
6449 if (CLASSTYPE_AS_BASE (t) != t)
6450 /* We use the base type for trivial assignments, and hence it
6451 needs a mode. */
6452 compute_record_mode (CLASSTYPE_AS_BASE (t));
6454 virtuals = modify_all_vtables (t, nreverse (virtuals));
6456 /* If necessary, create the primary vtable for this class. */
6457 if (virtuals || TYPE_CONTAINS_VPTR_P (t))
6459 /* We must enter these virtuals into the table. */
6460 if (!CLASSTYPE_HAS_PRIMARY_BASE_P (t))
6461 build_primary_vtable (NULL_TREE, t);
6462 else if (! BINFO_NEW_VTABLE_MARKED (TYPE_BINFO (t)))
6463 /* Here we know enough to change the type of our virtual
6464 function table, but we will wait until later this function. */
6465 build_primary_vtable (CLASSTYPE_PRIMARY_BINFO (t), t);
6467 /* If we're warning about ABI tags, check the types of the new
6468 virtual functions. */
6469 if (warn_abi_tag)
6470 for (tree v = virtuals; v; v = TREE_CHAIN (v))
6471 check_abi_tags (t, TREE_VALUE (v));
6474 if (TYPE_CONTAINS_VPTR_P (t))
6476 int vindex;
6477 tree fn;
6479 if (BINFO_VTABLE (TYPE_BINFO (t)))
6480 gcc_assert (DECL_VIRTUAL_P (BINFO_VTABLE (TYPE_BINFO (t))));
6481 if (!CLASSTYPE_HAS_PRIMARY_BASE_P (t))
6482 gcc_assert (BINFO_VIRTUALS (TYPE_BINFO (t)) == NULL_TREE);
6484 /* Add entries for virtual functions introduced by this class. */
6485 BINFO_VIRTUALS (TYPE_BINFO (t))
6486 = chainon (BINFO_VIRTUALS (TYPE_BINFO (t)), virtuals);
6488 /* Set DECL_VINDEX for all functions declared in this class. */
6489 for (vindex = 0, fn = BINFO_VIRTUALS (TYPE_BINFO (t));
6491 fn = TREE_CHAIN (fn),
6492 vindex += (TARGET_VTABLE_USES_DESCRIPTORS
6493 ? TARGET_VTABLE_USES_DESCRIPTORS : 1))
6495 tree fndecl = BV_FN (fn);
6497 if (DECL_THUNK_P (fndecl))
6498 /* A thunk. We should never be calling this entry directly
6499 from this vtable -- we'd use the entry for the non
6500 thunk base function. */
6501 DECL_VINDEX (fndecl) = NULL_TREE;
6502 else if (TREE_CODE (DECL_VINDEX (fndecl)) != INTEGER_CST)
6503 DECL_VINDEX (fndecl) = build_int_cst (NULL_TREE, vindex);
6507 finish_struct_bits (t);
6508 set_method_tm_attributes (t);
6510 /* Complete the rtl for any static member objects of the type we're
6511 working on. */
6512 for (x = TYPE_FIELDS (t); x; x = DECL_CHAIN (x))
6513 if (VAR_P (x) && TREE_STATIC (x)
6514 && TREE_TYPE (x) != error_mark_node
6515 && same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (x)), t))
6516 DECL_MODE (x) = TYPE_MODE (t);
6518 /* Done with FIELDS...now decide whether to sort these for
6519 faster lookups later.
6521 We use a small number because most searches fail (succeeding
6522 ultimately as the search bores through the inheritance
6523 hierarchy), and we want this failure to occur quickly. */
6525 insert_into_classtype_sorted_fields (TYPE_FIELDS (t), t, 8);
6527 /* Complain if one of the field types requires lower visibility. */
6528 constrain_class_visibility (t);
6530 /* Make the rtl for any new vtables we have created, and unmark
6531 the base types we marked. */
6532 finish_vtbls (t);
6534 /* Build the VTT for T. */
6535 build_vtt (t);
6537 /* This warning does not make sense for Java classes, since they
6538 cannot have destructors. */
6539 if (!TYPE_FOR_JAVA (t) && warn_nonvdtor
6540 && TYPE_POLYMORPHIC_P (t) && accessible_nvdtor_p (t))
6541 warning (OPT_Wnon_virtual_dtor,
6542 "%q#T has virtual functions and accessible"
6543 " non-virtual destructor", t);
6545 complete_vars (t);
6547 if (warn_overloaded_virtual)
6548 warn_hidden (t);
6550 /* Class layout, assignment of virtual table slots, etc., is now
6551 complete. Give the back end a chance to tweak the visibility of
6552 the class or perform any other required target modifications. */
6553 targetm.cxx.adjust_class_at_definition (t);
6555 maybe_suppress_debug_info (t);
6557 if (flag_vtable_verify)
6558 vtv_save_class_info (t);
6560 dump_class_hierarchy (t);
6562 /* Finish debugging output for this type. */
6563 rest_of_type_compilation (t, ! LOCAL_CLASS_P (t));
6565 if (TYPE_TRANSPARENT_AGGR (t))
6567 tree field = first_field (t);
6568 if (field == NULL_TREE || error_operand_p (field))
6570 error ("type transparent %q#T does not have any fields", t);
6571 TYPE_TRANSPARENT_AGGR (t) = 0;
6573 else if (DECL_ARTIFICIAL (field))
6575 if (DECL_FIELD_IS_BASE (field))
6576 error ("type transparent class %qT has base classes", t);
6577 else
6579 gcc_checking_assert (DECL_VIRTUAL_P (field));
6580 error ("type transparent class %qT has virtual functions", t);
6582 TYPE_TRANSPARENT_AGGR (t) = 0;
6584 else if (TYPE_MODE (t) != DECL_MODE (field))
6586 error ("type transparent %q#T cannot be made transparent because "
6587 "the type of the first field has a different ABI from the "
6588 "class overall", t);
6589 TYPE_TRANSPARENT_AGGR (t) = 0;
6594 /* Insert FIELDS into T for the sorted case if the FIELDS count is
6595 equal to THRESHOLD or greater than THRESHOLD. */
6597 static void
6598 insert_into_classtype_sorted_fields (tree fields, tree t, int threshold)
6600 int n_fields = count_fields (fields);
6601 if (n_fields >= threshold)
6603 struct sorted_fields_type *field_vec = sorted_fields_type_new (n_fields);
6604 add_fields_to_record_type (fields, field_vec, 0);
6605 qsort (field_vec->elts, n_fields, sizeof (tree), field_decl_cmp);
6606 CLASSTYPE_SORTED_FIELDS (t) = field_vec;
6610 /* Insert lately defined enum ENUMTYPE into T for the sorted case. */
6612 void
6613 insert_late_enum_def_into_classtype_sorted_fields (tree enumtype, tree t)
6615 struct sorted_fields_type *sorted_fields = CLASSTYPE_SORTED_FIELDS (t);
6616 if (sorted_fields)
6618 int i;
6619 int n_fields
6620 = list_length (TYPE_VALUES (enumtype)) + sorted_fields->len;
6621 struct sorted_fields_type *field_vec = sorted_fields_type_new (n_fields);
6623 for (i = 0; i < sorted_fields->len; ++i)
6624 field_vec->elts[i] = sorted_fields->elts[i];
6626 add_enum_fields_to_record_type (enumtype, field_vec,
6627 sorted_fields->len);
6628 qsort (field_vec->elts, n_fields, sizeof (tree), field_decl_cmp);
6629 CLASSTYPE_SORTED_FIELDS (t) = field_vec;
6633 /* When T was built up, the member declarations were added in reverse
6634 order. Rearrange them to declaration order. */
6636 void
6637 unreverse_member_declarations (tree t)
6639 tree next;
6640 tree prev;
6641 tree x;
6643 /* The following lists are all in reverse order. Put them in
6644 declaration order now. */
6645 TYPE_METHODS (t) = nreverse (TYPE_METHODS (t));
6646 CLASSTYPE_DECL_LIST (t) = nreverse (CLASSTYPE_DECL_LIST (t));
6648 /* Actually, for the TYPE_FIELDS, only the non TYPE_DECLs are in
6649 reverse order, so we can't just use nreverse. */
6650 prev = NULL_TREE;
6651 for (x = TYPE_FIELDS (t);
6652 x && TREE_CODE (x) != TYPE_DECL;
6653 x = next)
6655 next = DECL_CHAIN (x);
6656 DECL_CHAIN (x) = prev;
6657 prev = x;
6659 if (prev)
6661 DECL_CHAIN (TYPE_FIELDS (t)) = x;
6662 if (prev)
6663 TYPE_FIELDS (t) = prev;
6667 tree
6668 finish_struct (tree t, tree attributes)
6670 location_t saved_loc = input_location;
6672 /* Now that we've got all the field declarations, reverse everything
6673 as necessary. */
6674 unreverse_member_declarations (t);
6676 cplus_decl_attributes (&t, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
6678 /* Nadger the current location so that diagnostics point to the start of
6679 the struct, not the end. */
6680 input_location = DECL_SOURCE_LOCATION (TYPE_NAME (t));
6682 if (processing_template_decl)
6684 tree x;
6686 finish_struct_methods (t);
6687 TYPE_SIZE (t) = bitsize_zero_node;
6688 TYPE_SIZE_UNIT (t) = size_zero_node;
6690 /* We need to emit an error message if this type was used as a parameter
6691 and it is an abstract type, even if it is a template. We construct
6692 a simple CLASSTYPE_PURE_VIRTUALS list without taking bases into
6693 account and we call complete_vars with this type, which will check
6694 the PARM_DECLS. Note that while the type is being defined,
6695 CLASSTYPE_PURE_VIRTUALS contains the list of the inline friends
6696 (see CLASSTYPE_INLINE_FRIENDS) so we need to clear it. */
6697 CLASSTYPE_PURE_VIRTUALS (t) = NULL;
6698 for (x = TYPE_METHODS (t); x; x = DECL_CHAIN (x))
6699 if (DECL_PURE_VIRTUAL_P (x))
6700 vec_safe_push (CLASSTYPE_PURE_VIRTUALS (t), x);
6701 complete_vars (t);
6702 /* We need to add the target functions to the CLASSTYPE_METHOD_VEC if
6703 an enclosing scope is a template class, so that this function be
6704 found by lookup_fnfields_1 when the using declaration is not
6705 instantiated yet. */
6706 for (x = TYPE_FIELDS (t); x; x = DECL_CHAIN (x))
6707 if (TREE_CODE (x) == USING_DECL)
6709 tree fn = strip_using_decl (x);
6710 if (is_overloaded_fn (fn))
6711 for (; fn; fn = OVL_NEXT (fn))
6712 add_method (t, OVL_CURRENT (fn), x);
6715 /* Remember current #pragma pack value. */
6716 TYPE_PRECISION (t) = maximum_field_alignment;
6718 /* Fix up any variants we've already built. */
6719 for (x = TYPE_NEXT_VARIANT (t); x; x = TYPE_NEXT_VARIANT (x))
6721 TYPE_SIZE (x) = TYPE_SIZE (t);
6722 TYPE_SIZE_UNIT (x) = TYPE_SIZE_UNIT (t);
6723 TYPE_FIELDS (x) = TYPE_FIELDS (t);
6724 TYPE_METHODS (x) = TYPE_METHODS (t);
6727 else
6728 finish_struct_1 (t);
6730 input_location = saved_loc;
6732 TYPE_BEING_DEFINED (t) = 0;
6734 if (current_class_type)
6735 popclass ();
6736 else
6737 error ("trying to finish struct, but kicked out due to previous parse errors");
6739 if (processing_template_decl && at_function_scope_p ()
6740 /* Lambdas are defined by the LAMBDA_EXPR. */
6741 && !LAMBDA_TYPE_P (t))
6742 add_stmt (build_min (TAG_DEFN, t));
6744 return t;
6747 /* Hash table to avoid endless recursion when handling references. */
6748 static hash_table<pointer_hash<tree_node> > *fixed_type_or_null_ref_ht;
6750 /* Return the dynamic type of INSTANCE, if known.
6751 Used to determine whether the virtual function table is needed
6752 or not.
6754 *NONNULL is set iff INSTANCE can be known to be nonnull, regardless
6755 of our knowledge of its type. *NONNULL should be initialized
6756 before this function is called. */
6758 static tree
6759 fixed_type_or_null (tree instance, int *nonnull, int *cdtorp)
6761 #define RECUR(T) fixed_type_or_null((T), nonnull, cdtorp)
6763 switch (TREE_CODE (instance))
6765 case INDIRECT_REF:
6766 if (POINTER_TYPE_P (TREE_TYPE (instance)))
6767 return NULL_TREE;
6768 else
6769 return RECUR (TREE_OPERAND (instance, 0));
6771 case CALL_EXPR:
6772 /* This is a call to a constructor, hence it's never zero. */
6773 if (TREE_HAS_CONSTRUCTOR (instance))
6775 if (nonnull)
6776 *nonnull = 1;
6777 return TREE_TYPE (instance);
6779 return NULL_TREE;
6781 case SAVE_EXPR:
6782 /* This is a call to a constructor, hence it's never zero. */
6783 if (TREE_HAS_CONSTRUCTOR (instance))
6785 if (nonnull)
6786 *nonnull = 1;
6787 return TREE_TYPE (instance);
6789 return RECUR (TREE_OPERAND (instance, 0));
6791 case POINTER_PLUS_EXPR:
6792 case PLUS_EXPR:
6793 case MINUS_EXPR:
6794 if (TREE_CODE (TREE_OPERAND (instance, 0)) == ADDR_EXPR)
6795 return RECUR (TREE_OPERAND (instance, 0));
6796 if (TREE_CODE (TREE_OPERAND (instance, 1)) == INTEGER_CST)
6797 /* Propagate nonnull. */
6798 return RECUR (TREE_OPERAND (instance, 0));
6800 return NULL_TREE;
6802 CASE_CONVERT:
6803 return RECUR (TREE_OPERAND (instance, 0));
6805 case ADDR_EXPR:
6806 instance = TREE_OPERAND (instance, 0);
6807 if (nonnull)
6809 /* Just because we see an ADDR_EXPR doesn't mean we're dealing
6810 with a real object -- given &p->f, p can still be null. */
6811 tree t = get_base_address (instance);
6812 /* ??? Probably should check DECL_WEAK here. */
6813 if (t && DECL_P (t))
6814 *nonnull = 1;
6816 return RECUR (instance);
6818 case COMPONENT_REF:
6819 /* If this component is really a base class reference, then the field
6820 itself isn't definitive. */
6821 if (DECL_FIELD_IS_BASE (TREE_OPERAND (instance, 1)))
6822 return RECUR (TREE_OPERAND (instance, 0));
6823 return RECUR (TREE_OPERAND (instance, 1));
6825 case VAR_DECL:
6826 case FIELD_DECL:
6827 if (TREE_CODE (TREE_TYPE (instance)) == ARRAY_TYPE
6828 && MAYBE_CLASS_TYPE_P (TREE_TYPE (TREE_TYPE (instance))))
6830 if (nonnull)
6831 *nonnull = 1;
6832 return TREE_TYPE (TREE_TYPE (instance));
6834 /* fall through... */
6835 case TARGET_EXPR:
6836 case PARM_DECL:
6837 case RESULT_DECL:
6838 if (MAYBE_CLASS_TYPE_P (TREE_TYPE (instance)))
6840 if (nonnull)
6841 *nonnull = 1;
6842 return TREE_TYPE (instance);
6844 else if (instance == current_class_ptr)
6846 if (nonnull)
6847 *nonnull = 1;
6849 /* if we're in a ctor or dtor, we know our type. If
6850 current_class_ptr is set but we aren't in a function, we're in
6851 an NSDMI (and therefore a constructor). */
6852 if (current_scope () != current_function_decl
6853 || (DECL_LANG_SPECIFIC (current_function_decl)
6854 && (DECL_CONSTRUCTOR_P (current_function_decl)
6855 || DECL_DESTRUCTOR_P (current_function_decl))))
6857 if (cdtorp)
6858 *cdtorp = 1;
6859 return TREE_TYPE (TREE_TYPE (instance));
6862 else if (TREE_CODE (TREE_TYPE (instance)) == REFERENCE_TYPE)
6864 /* We only need one hash table because it is always left empty. */
6865 if (!fixed_type_or_null_ref_ht)
6866 fixed_type_or_null_ref_ht
6867 = new hash_table<pointer_hash<tree_node> > (37);
6869 /* Reference variables should be references to objects. */
6870 if (nonnull)
6871 *nonnull = 1;
6873 /* Enter the INSTANCE in a table to prevent recursion; a
6874 variable's initializer may refer to the variable
6875 itself. */
6876 if (VAR_P (instance)
6877 && DECL_INITIAL (instance)
6878 && !type_dependent_expression_p_push (DECL_INITIAL (instance))
6879 && !fixed_type_or_null_ref_ht->find (instance))
6881 tree type;
6882 tree_node **slot;
6884 slot = fixed_type_or_null_ref_ht->find_slot (instance, INSERT);
6885 *slot = instance;
6886 type = RECUR (DECL_INITIAL (instance));
6887 fixed_type_or_null_ref_ht->remove_elt (instance);
6889 return type;
6892 return NULL_TREE;
6894 default:
6895 return NULL_TREE;
6897 #undef RECUR
6900 /* Return nonzero if the dynamic type of INSTANCE is known, and
6901 equivalent to the static type. We also handle the case where
6902 INSTANCE is really a pointer. Return negative if this is a
6903 ctor/dtor. There the dynamic type is known, but this might not be
6904 the most derived base of the original object, and hence virtual
6905 bases may not be laid out according to this type.
6907 Used to determine whether the virtual function table is needed
6908 or not.
6910 *NONNULL is set iff INSTANCE can be known to be nonnull, regardless
6911 of our knowledge of its type. *NONNULL should be initialized
6912 before this function is called. */
6915 resolves_to_fixed_type_p (tree instance, int* nonnull)
6917 tree t = TREE_TYPE (instance);
6918 int cdtorp = 0;
6919 tree fixed;
6921 /* processing_template_decl can be false in a template if we're in
6922 fold_non_dependent_expr, but we still want to suppress this check. */
6923 if (in_template_function ())
6925 /* In a template we only care about the type of the result. */
6926 if (nonnull)
6927 *nonnull = true;
6928 return true;
6931 fixed = fixed_type_or_null (instance, nonnull, &cdtorp);
6932 if (fixed == NULL_TREE)
6933 return 0;
6934 if (POINTER_TYPE_P (t))
6935 t = TREE_TYPE (t);
6936 if (!same_type_ignoring_top_level_qualifiers_p (t, fixed))
6937 return 0;
6938 return cdtorp ? -1 : 1;
6942 void
6943 init_class_processing (void)
6945 current_class_depth = 0;
6946 current_class_stack_size = 10;
6947 current_class_stack
6948 = XNEWVEC (struct class_stack_node, current_class_stack_size);
6949 vec_alloc (local_classes, 8);
6950 sizeof_biggest_empty_class = size_zero_node;
6952 ridpointers[(int) RID_PUBLIC] = access_public_node;
6953 ridpointers[(int) RID_PRIVATE] = access_private_node;
6954 ridpointers[(int) RID_PROTECTED] = access_protected_node;
6957 /* Restore the cached PREVIOUS_CLASS_LEVEL. */
6959 static void
6960 restore_class_cache (void)
6962 tree type;
6964 /* We are re-entering the same class we just left, so we don't
6965 have to search the whole inheritance matrix to find all the
6966 decls to bind again. Instead, we install the cached
6967 class_shadowed list and walk through it binding names. */
6968 push_binding_level (previous_class_level);
6969 class_binding_level = previous_class_level;
6970 /* Restore IDENTIFIER_TYPE_VALUE. */
6971 for (type = class_binding_level->type_shadowed;
6972 type;
6973 type = TREE_CHAIN (type))
6974 SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (type), TREE_TYPE (type));
6977 /* Set global variables CURRENT_CLASS_NAME and CURRENT_CLASS_TYPE as
6978 appropriate for TYPE.
6980 So that we may avoid calls to lookup_name, we cache the _TYPE
6981 nodes of local TYPE_DECLs in the TREE_TYPE field of the name.
6983 For multiple inheritance, we perform a two-pass depth-first search
6984 of the type lattice. */
6986 void
6987 pushclass (tree type)
6989 class_stack_node_t csn;
6991 type = TYPE_MAIN_VARIANT (type);
6993 /* Make sure there is enough room for the new entry on the stack. */
6994 if (current_class_depth + 1 >= current_class_stack_size)
6996 current_class_stack_size *= 2;
6997 current_class_stack
6998 = XRESIZEVEC (struct class_stack_node, current_class_stack,
6999 current_class_stack_size);
7002 /* Insert a new entry on the class stack. */
7003 csn = current_class_stack + current_class_depth;
7004 csn->name = current_class_name;
7005 csn->type = current_class_type;
7006 csn->access = current_access_specifier;
7007 csn->names_used = 0;
7008 csn->hidden = 0;
7009 current_class_depth++;
7011 /* Now set up the new type. */
7012 current_class_name = TYPE_NAME (type);
7013 if (TREE_CODE (current_class_name) == TYPE_DECL)
7014 current_class_name = DECL_NAME (current_class_name);
7015 current_class_type = type;
7017 /* By default, things in classes are private, while things in
7018 structures or unions are public. */
7019 current_access_specifier = (CLASSTYPE_DECLARED_CLASS (type)
7020 ? access_private_node
7021 : access_public_node);
7023 if (previous_class_level
7024 && type != previous_class_level->this_entity
7025 && current_class_depth == 1)
7027 /* Forcibly remove any old class remnants. */
7028 invalidate_class_lookup_cache ();
7031 if (!previous_class_level
7032 || type != previous_class_level->this_entity
7033 || current_class_depth > 1)
7034 pushlevel_class ();
7035 else
7036 restore_class_cache ();
7039 /* When we exit a toplevel class scope, we save its binding level so
7040 that we can restore it quickly. Here, we've entered some other
7041 class, so we must invalidate our cache. */
7043 void
7044 invalidate_class_lookup_cache (void)
7046 previous_class_level = NULL;
7049 /* Get out of the current class scope. If we were in a class scope
7050 previously, that is the one popped to. */
7052 void
7053 popclass (void)
7055 poplevel_class ();
7057 current_class_depth--;
7058 current_class_name = current_class_stack[current_class_depth].name;
7059 current_class_type = current_class_stack[current_class_depth].type;
7060 current_access_specifier = current_class_stack[current_class_depth].access;
7061 if (current_class_stack[current_class_depth].names_used)
7062 splay_tree_delete (current_class_stack[current_class_depth].names_used);
7065 /* Mark the top of the class stack as hidden. */
7067 void
7068 push_class_stack (void)
7070 if (current_class_depth)
7071 ++current_class_stack[current_class_depth - 1].hidden;
7074 /* Mark the top of the class stack as un-hidden. */
7076 void
7077 pop_class_stack (void)
7079 if (current_class_depth)
7080 --current_class_stack[current_class_depth - 1].hidden;
7083 /* Returns 1 if the class type currently being defined is either T or
7084 a nested type of T. */
7086 bool
7087 currently_open_class (tree t)
7089 int i;
7091 if (!CLASS_TYPE_P (t))
7092 return false;
7094 t = TYPE_MAIN_VARIANT (t);
7096 /* We start looking from 1 because entry 0 is from global scope,
7097 and has no type. */
7098 for (i = current_class_depth; i > 0; --i)
7100 tree c;
7101 if (i == current_class_depth)
7102 c = current_class_type;
7103 else
7105 if (current_class_stack[i].hidden)
7106 break;
7107 c = current_class_stack[i].type;
7109 if (!c)
7110 continue;
7111 if (same_type_p (c, t))
7112 return true;
7114 return false;
7117 /* If either current_class_type or one of its enclosing classes are derived
7118 from T, return the appropriate type. Used to determine how we found
7119 something via unqualified lookup. */
7121 tree
7122 currently_open_derived_class (tree t)
7124 int i;
7126 /* The bases of a dependent type are unknown. */
7127 if (dependent_type_p (t))
7128 return NULL_TREE;
7130 if (!current_class_type)
7131 return NULL_TREE;
7133 if (DERIVED_FROM_P (t, current_class_type))
7134 return current_class_type;
7136 for (i = current_class_depth - 1; i > 0; --i)
7138 if (current_class_stack[i].hidden)
7139 break;
7140 if (DERIVED_FROM_P (t, current_class_stack[i].type))
7141 return current_class_stack[i].type;
7144 return NULL_TREE;
7147 /* Returns the innermost class type which is not a lambda closure type. */
7149 tree
7150 current_nonlambda_class_type (void)
7152 int i;
7154 /* We start looking from 1 because entry 0 is from global scope,
7155 and has no type. */
7156 for (i = current_class_depth; i > 0; --i)
7158 tree c;
7159 if (i == current_class_depth)
7160 c = current_class_type;
7161 else
7163 if (current_class_stack[i].hidden)
7164 break;
7165 c = current_class_stack[i].type;
7167 if (!c)
7168 continue;
7169 if (!LAMBDA_TYPE_P (c))
7170 return c;
7172 return NULL_TREE;
7175 /* When entering a class scope, all enclosing class scopes' names with
7176 static meaning (static variables, static functions, types and
7177 enumerators) have to be visible. This recursive function calls
7178 pushclass for all enclosing class contexts until global or a local
7179 scope is reached. TYPE is the enclosed class. */
7181 void
7182 push_nested_class (tree type)
7184 /* A namespace might be passed in error cases, like A::B:C. */
7185 if (type == NULL_TREE
7186 || !CLASS_TYPE_P (type))
7187 return;
7189 push_nested_class (DECL_CONTEXT (TYPE_MAIN_DECL (type)));
7191 pushclass (type);
7194 /* Undoes a push_nested_class call. */
7196 void
7197 pop_nested_class (void)
7199 tree context = DECL_CONTEXT (TYPE_MAIN_DECL (current_class_type));
7201 popclass ();
7202 if (context && CLASS_TYPE_P (context))
7203 pop_nested_class ();
7206 /* Returns the number of extern "LANG" blocks we are nested within. */
7209 current_lang_depth (void)
7211 return vec_safe_length (current_lang_base);
7214 /* Set global variables CURRENT_LANG_NAME to appropriate value
7215 so that behavior of name-mangling machinery is correct. */
7217 void
7218 push_lang_context (tree name)
7220 vec_safe_push (current_lang_base, current_lang_name);
7222 if (name == lang_name_cplusplus)
7224 current_lang_name = name;
7226 else if (name == lang_name_java)
7228 current_lang_name = name;
7229 /* DECL_IGNORED_P is initially set for these types, to avoid clutter.
7230 (See record_builtin_java_type in decl.c.) However, that causes
7231 incorrect debug entries if these types are actually used.
7232 So we re-enable debug output after extern "Java". */
7233 DECL_IGNORED_P (TYPE_NAME (java_byte_type_node)) = 0;
7234 DECL_IGNORED_P (TYPE_NAME (java_short_type_node)) = 0;
7235 DECL_IGNORED_P (TYPE_NAME (java_int_type_node)) = 0;
7236 DECL_IGNORED_P (TYPE_NAME (java_long_type_node)) = 0;
7237 DECL_IGNORED_P (TYPE_NAME (java_float_type_node)) = 0;
7238 DECL_IGNORED_P (TYPE_NAME (java_double_type_node)) = 0;
7239 DECL_IGNORED_P (TYPE_NAME (java_char_type_node)) = 0;
7240 DECL_IGNORED_P (TYPE_NAME (java_boolean_type_node)) = 0;
7242 else if (name == lang_name_c)
7244 current_lang_name = name;
7246 else
7247 error ("language string %<\"%E\"%> not recognized", name);
7250 /* Get out of the current language scope. */
7252 void
7253 pop_lang_context (void)
7255 current_lang_name = current_lang_base->pop ();
7258 /* Type instantiation routines. */
7260 /* Given an OVERLOAD and a TARGET_TYPE, return the function that
7261 matches the TARGET_TYPE. If there is no satisfactory match, return
7262 error_mark_node, and issue an error & warning messages under
7263 control of FLAGS. Permit pointers to member function if FLAGS
7264 permits. If TEMPLATE_ONLY, the name of the overloaded function was
7265 a template-id, and EXPLICIT_TARGS are the explicitly provided
7266 template arguments.
7268 If OVERLOAD is for one or more member functions, then ACCESS_PATH
7269 is the base path used to reference those member functions. If
7270 the address is resolved to a member function, access checks will be
7271 performed and errors issued if appropriate. */
7273 static tree
7274 resolve_address_of_overloaded_function (tree target_type,
7275 tree overload,
7276 tsubst_flags_t flags,
7277 bool template_only,
7278 tree explicit_targs,
7279 tree access_path)
7281 /* Here's what the standard says:
7283 [over.over]
7285 If the name is a function template, template argument deduction
7286 is done, and if the argument deduction succeeds, the deduced
7287 arguments are used to generate a single template function, which
7288 is added to the set of overloaded functions considered.
7290 Non-member functions and static member functions match targets of
7291 type "pointer-to-function" or "reference-to-function." Nonstatic
7292 member functions match targets of type "pointer-to-member
7293 function;" the function type of the pointer to member is used to
7294 select the member function from the set of overloaded member
7295 functions. If a nonstatic member function is selected, the
7296 reference to the overloaded function name is required to have the
7297 form of a pointer to member as described in 5.3.1.
7299 If more than one function is selected, any template functions in
7300 the set are eliminated if the set also contains a non-template
7301 function, and any given template function is eliminated if the
7302 set contains a second template function that is more specialized
7303 than the first according to the partial ordering rules 14.5.5.2.
7304 After such eliminations, if any, there shall remain exactly one
7305 selected function. */
7307 int is_ptrmem = 0;
7308 /* We store the matches in a TREE_LIST rooted here. The functions
7309 are the TREE_PURPOSE, not the TREE_VALUE, in this list, for easy
7310 interoperability with most_specialized_instantiation. */
7311 tree matches = NULL_TREE;
7312 tree fn;
7313 tree target_fn_type;
7315 /* By the time we get here, we should be seeing only real
7316 pointer-to-member types, not the internal POINTER_TYPE to
7317 METHOD_TYPE representation. */
7318 gcc_assert (!TYPE_PTR_P (target_type)
7319 || TREE_CODE (TREE_TYPE (target_type)) != METHOD_TYPE);
7321 gcc_assert (is_overloaded_fn (overload));
7323 /* Check that the TARGET_TYPE is reasonable. */
7324 if (TYPE_PTRFN_P (target_type)
7325 || TYPE_REFFN_P (target_type))
7326 /* This is OK. */;
7327 else if (TYPE_PTRMEMFUNC_P (target_type))
7328 /* This is OK, too. */
7329 is_ptrmem = 1;
7330 else if (TREE_CODE (target_type) == FUNCTION_TYPE)
7331 /* This is OK, too. This comes from a conversion to reference
7332 type. */
7333 target_type = build_reference_type (target_type);
7334 else
7336 if (flags & tf_error)
7337 error ("cannot resolve overloaded function %qD based on"
7338 " conversion to type %qT",
7339 DECL_NAME (OVL_FUNCTION (overload)), target_type);
7340 return error_mark_node;
7343 /* Non-member functions and static member functions match targets of type
7344 "pointer-to-function" or "reference-to-function." Nonstatic member
7345 functions match targets of type "pointer-to-member-function;" the
7346 function type of the pointer to member is used to select the member
7347 function from the set of overloaded member functions.
7349 So figure out the FUNCTION_TYPE that we want to match against. */
7350 target_fn_type = static_fn_type (target_type);
7352 /* If we can find a non-template function that matches, we can just
7353 use it. There's no point in generating template instantiations
7354 if we're just going to throw them out anyhow. But, of course, we
7355 can only do this when we don't *need* a template function. */
7356 if (!template_only)
7358 tree fns;
7360 for (fns = overload; fns; fns = OVL_NEXT (fns))
7362 tree fn = OVL_CURRENT (fns);
7364 if (TREE_CODE (fn) == TEMPLATE_DECL)
7365 /* We're not looking for templates just yet. */
7366 continue;
7368 if ((TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
7369 != is_ptrmem)
7370 /* We're looking for a non-static member, and this isn't
7371 one, or vice versa. */
7372 continue;
7374 /* Ignore functions which haven't been explicitly
7375 declared. */
7376 if (DECL_ANTICIPATED (fn))
7377 continue;
7379 /* See if there's a match. */
7380 if (same_type_p (target_fn_type, static_fn_type (fn)))
7381 matches = tree_cons (fn, NULL_TREE, matches);
7385 /* Now, if we've already got a match (or matches), there's no need
7386 to proceed to the template functions. But, if we don't have a
7387 match we need to look at them, too. */
7388 if (!matches)
7390 tree target_arg_types;
7391 tree target_ret_type;
7392 tree fns;
7393 tree *args;
7394 unsigned int nargs, ia;
7395 tree arg;
7397 target_arg_types = TYPE_ARG_TYPES (target_fn_type);
7398 target_ret_type = TREE_TYPE (target_fn_type);
7400 nargs = list_length (target_arg_types);
7401 args = XALLOCAVEC (tree, nargs);
7402 for (arg = target_arg_types, ia = 0;
7403 arg != NULL_TREE && arg != void_list_node;
7404 arg = TREE_CHAIN (arg), ++ia)
7405 args[ia] = TREE_VALUE (arg);
7406 nargs = ia;
7408 for (fns = overload; fns; fns = OVL_NEXT (fns))
7410 tree fn = OVL_CURRENT (fns);
7411 tree instantiation;
7412 tree targs;
7414 if (TREE_CODE (fn) != TEMPLATE_DECL)
7415 /* We're only looking for templates. */
7416 continue;
7418 if ((TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
7419 != is_ptrmem)
7420 /* We're not looking for a non-static member, and this is
7421 one, or vice versa. */
7422 continue;
7424 tree ret = target_ret_type;
7426 /* If the template has a deduced return type, don't expose it to
7427 template argument deduction. */
7428 if (undeduced_auto_decl (fn))
7429 ret = NULL_TREE;
7431 /* Try to do argument deduction. */
7432 targs = make_tree_vec (DECL_NTPARMS (fn));
7433 instantiation = fn_type_unification (fn, explicit_targs, targs, args,
7434 nargs, ret,
7435 DEDUCE_EXACT, LOOKUP_NORMAL,
7436 false, false);
7437 if (instantiation == error_mark_node)
7438 /* Instantiation failed. */
7439 continue;
7441 /* And now force instantiation to do return type deduction. */
7442 if (undeduced_auto_decl (instantiation))
7444 ++function_depth;
7445 instantiate_decl (instantiation, /*defer*/false, /*class*/false);
7446 --function_depth;
7448 require_deduced_type (instantiation);
7451 /* See if there's a match. */
7452 if (same_type_p (target_fn_type, static_fn_type (instantiation)))
7453 matches = tree_cons (instantiation, fn, matches);
7456 /* Now, remove all but the most specialized of the matches. */
7457 if (matches)
7459 tree match = most_specialized_instantiation (matches);
7461 if (match != error_mark_node)
7462 matches = tree_cons (TREE_PURPOSE (match),
7463 NULL_TREE,
7464 NULL_TREE);
7468 /* Now we should have exactly one function in MATCHES. */
7469 if (matches == NULL_TREE)
7471 /* There were *no* matches. */
7472 if (flags & tf_error)
7474 error ("no matches converting function %qD to type %q#T",
7475 DECL_NAME (OVL_CURRENT (overload)),
7476 target_type);
7478 print_candidates (overload);
7480 return error_mark_node;
7482 else if (TREE_CHAIN (matches))
7484 /* There were too many matches. First check if they're all
7485 the same function. */
7486 tree match = NULL_TREE;
7488 fn = TREE_PURPOSE (matches);
7490 /* For multi-versioned functions, more than one match is just fine and
7491 decls_match will return false as they are different. */
7492 for (match = TREE_CHAIN (matches); match; match = TREE_CHAIN (match))
7493 if (!decls_match (fn, TREE_PURPOSE (match))
7494 && !targetm.target_option.function_versions
7495 (fn, TREE_PURPOSE (match)))
7496 break;
7498 if (match)
7500 if (flags & tf_error)
7502 error ("converting overloaded function %qD to type %q#T is ambiguous",
7503 DECL_NAME (OVL_FUNCTION (overload)),
7504 target_type);
7506 /* Since print_candidates expects the functions in the
7507 TREE_VALUE slot, we flip them here. */
7508 for (match = matches; match; match = TREE_CHAIN (match))
7509 TREE_VALUE (match) = TREE_PURPOSE (match);
7511 print_candidates (matches);
7514 return error_mark_node;
7518 /* Good, exactly one match. Now, convert it to the correct type. */
7519 fn = TREE_PURPOSE (matches);
7521 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
7522 && !(flags & tf_ptrmem_ok) && !flag_ms_extensions)
7524 static int explained;
7526 if (!(flags & tf_error))
7527 return error_mark_node;
7529 permerror (input_location, "assuming pointer to member %qD", fn);
7530 if (!explained)
7532 inform (input_location, "(a pointer to member can only be formed with %<&%E%>)", fn);
7533 explained = 1;
7537 /* If a pointer to a function that is multi-versioned is requested, the
7538 pointer to the dispatcher function is returned instead. This works
7539 well because indirectly calling the function will dispatch the right
7540 function version at run-time. */
7541 if (DECL_FUNCTION_VERSIONED (fn))
7543 fn = get_function_version_dispatcher (fn);
7544 if (fn == NULL)
7545 return error_mark_node;
7546 /* Mark all the versions corresponding to the dispatcher as used. */
7547 if (!(flags & tf_conv))
7548 mark_versions_used (fn);
7551 /* If we're doing overload resolution purely for the purpose of
7552 determining conversion sequences, we should not consider the
7553 function used. If this conversion sequence is selected, the
7554 function will be marked as used at this point. */
7555 if (!(flags & tf_conv))
7557 /* Make =delete work with SFINAE. */
7558 if (DECL_DELETED_FN (fn) && !(flags & tf_error))
7559 return error_mark_node;
7561 mark_used (fn);
7564 /* We could not check access to member functions when this
7565 expression was originally created since we did not know at that
7566 time to which function the expression referred. */
7567 if (DECL_FUNCTION_MEMBER_P (fn))
7569 gcc_assert (access_path);
7570 perform_or_defer_access_check (access_path, fn, fn, flags);
7573 if (TYPE_PTRFN_P (target_type) || TYPE_PTRMEMFUNC_P (target_type))
7574 return cp_build_addr_expr (fn, flags);
7575 else
7577 /* The target must be a REFERENCE_TYPE. Above, cp_build_unary_op
7578 will mark the function as addressed, but here we must do it
7579 explicitly. */
7580 cxx_mark_addressable (fn);
7582 return fn;
7586 /* This function will instantiate the type of the expression given in
7587 RHS to match the type of LHSTYPE. If errors exist, then return
7588 error_mark_node. FLAGS is a bit mask. If TF_ERROR is set, then
7589 we complain on errors. If we are not complaining, never modify rhs,
7590 as overload resolution wants to try many possible instantiations, in
7591 the hope that at least one will work.
7593 For non-recursive calls, LHSTYPE should be a function, pointer to
7594 function, or a pointer to member function. */
7596 tree
7597 instantiate_type (tree lhstype, tree rhs, tsubst_flags_t flags)
7599 tsubst_flags_t flags_in = flags;
7600 tree access_path = NULL_TREE;
7602 flags &= ~tf_ptrmem_ok;
7604 if (lhstype == unknown_type_node)
7606 if (flags & tf_error)
7607 error ("not enough type information");
7608 return error_mark_node;
7611 if (TREE_TYPE (rhs) != NULL_TREE && ! (type_unknown_p (rhs)))
7613 tree fntype = non_reference (lhstype);
7614 if (same_type_p (fntype, TREE_TYPE (rhs)))
7615 return rhs;
7616 if (flag_ms_extensions
7617 && TYPE_PTRMEMFUNC_P (fntype)
7618 && !TYPE_PTRMEMFUNC_P (TREE_TYPE (rhs)))
7619 /* Microsoft allows `A::f' to be resolved to a
7620 pointer-to-member. */
7622 else
7624 if (flags & tf_error)
7625 error ("cannot convert %qE from type %qT to type %qT",
7626 rhs, TREE_TYPE (rhs), fntype);
7627 return error_mark_node;
7631 if (BASELINK_P (rhs))
7633 access_path = BASELINK_ACCESS_BINFO (rhs);
7634 rhs = BASELINK_FUNCTIONS (rhs);
7637 /* If we are in a template, and have a NON_DEPENDENT_EXPR, we cannot
7638 deduce any type information. */
7639 if (TREE_CODE (rhs) == NON_DEPENDENT_EXPR)
7641 if (flags & tf_error)
7642 error ("not enough type information");
7643 return error_mark_node;
7646 /* There only a few kinds of expressions that may have a type
7647 dependent on overload resolution. */
7648 gcc_assert (TREE_CODE (rhs) == ADDR_EXPR
7649 || TREE_CODE (rhs) == COMPONENT_REF
7650 || is_overloaded_fn (rhs)
7651 || (flag_ms_extensions && TREE_CODE (rhs) == FUNCTION_DECL));
7653 /* This should really only be used when attempting to distinguish
7654 what sort of a pointer to function we have. For now, any
7655 arithmetic operation which is not supported on pointers
7656 is rejected as an error. */
7658 switch (TREE_CODE (rhs))
7660 case COMPONENT_REF:
7662 tree member = TREE_OPERAND (rhs, 1);
7664 member = instantiate_type (lhstype, member, flags);
7665 if (member != error_mark_node
7666 && TREE_SIDE_EFFECTS (TREE_OPERAND (rhs, 0)))
7667 /* Do not lose object's side effects. */
7668 return build2 (COMPOUND_EXPR, TREE_TYPE (member),
7669 TREE_OPERAND (rhs, 0), member);
7670 return member;
7673 case OFFSET_REF:
7674 rhs = TREE_OPERAND (rhs, 1);
7675 if (BASELINK_P (rhs))
7676 return instantiate_type (lhstype, rhs, flags_in);
7678 /* This can happen if we are forming a pointer-to-member for a
7679 member template. */
7680 gcc_assert (TREE_CODE (rhs) == TEMPLATE_ID_EXPR);
7682 /* Fall through. */
7684 case TEMPLATE_ID_EXPR:
7686 tree fns = TREE_OPERAND (rhs, 0);
7687 tree args = TREE_OPERAND (rhs, 1);
7689 return
7690 resolve_address_of_overloaded_function (lhstype, fns, flags_in,
7691 /*template_only=*/true,
7692 args, access_path);
7695 case OVERLOAD:
7696 case FUNCTION_DECL:
7697 return
7698 resolve_address_of_overloaded_function (lhstype, rhs, flags_in,
7699 /*template_only=*/false,
7700 /*explicit_targs=*/NULL_TREE,
7701 access_path);
7703 case ADDR_EXPR:
7705 if (PTRMEM_OK_P (rhs))
7706 flags |= tf_ptrmem_ok;
7708 return instantiate_type (lhstype, TREE_OPERAND (rhs, 0), flags);
7711 case ERROR_MARK:
7712 return error_mark_node;
7714 default:
7715 gcc_unreachable ();
7717 return error_mark_node;
7720 /* Return the name of the virtual function pointer field
7721 (as an IDENTIFIER_NODE) for the given TYPE. Note that
7722 this may have to look back through base types to find the
7723 ultimate field name. (For single inheritance, these could
7724 all be the same name. Who knows for multiple inheritance). */
7726 static tree
7727 get_vfield_name (tree type)
7729 tree binfo, base_binfo;
7730 char *buf;
7732 for (binfo = TYPE_BINFO (type);
7733 BINFO_N_BASE_BINFOS (binfo);
7734 binfo = base_binfo)
7736 base_binfo = BINFO_BASE_BINFO (binfo, 0);
7738 if (BINFO_VIRTUAL_P (base_binfo)
7739 || !TYPE_CONTAINS_VPTR_P (BINFO_TYPE (base_binfo)))
7740 break;
7743 type = BINFO_TYPE (binfo);
7744 buf = (char *) alloca (sizeof (VFIELD_NAME_FORMAT)
7745 + TYPE_NAME_LENGTH (type) + 2);
7746 sprintf (buf, VFIELD_NAME_FORMAT,
7747 IDENTIFIER_POINTER (constructor_name (type)));
7748 return get_identifier (buf);
7751 void
7752 print_class_statistics (void)
7754 if (! GATHER_STATISTICS)
7755 return;
7757 fprintf (stderr, "convert_harshness = %d\n", n_convert_harshness);
7758 fprintf (stderr, "compute_conversion_costs = %d\n", n_compute_conversion_costs);
7759 if (n_vtables)
7761 fprintf (stderr, "vtables = %d; vtable searches = %d\n",
7762 n_vtables, n_vtable_searches);
7763 fprintf (stderr, "vtable entries = %d; vtable elems = %d\n",
7764 n_vtable_entries, n_vtable_elems);
7768 /* Build a dummy reference to ourselves so Derived::Base (and A::A) works,
7769 according to [class]:
7770 The class-name is also inserted
7771 into the scope of the class itself. For purposes of access checking,
7772 the inserted class name is treated as if it were a public member name. */
7774 void
7775 build_self_reference (void)
7777 tree name = constructor_name (current_class_type);
7778 tree value = build_lang_decl (TYPE_DECL, name, current_class_type);
7779 tree saved_cas;
7781 DECL_NONLOCAL (value) = 1;
7782 DECL_CONTEXT (value) = current_class_type;
7783 DECL_ARTIFICIAL (value) = 1;
7784 SET_DECL_SELF_REFERENCE_P (value);
7785 set_underlying_type (value);
7787 if (processing_template_decl)
7788 value = push_template_decl (value);
7790 saved_cas = current_access_specifier;
7791 current_access_specifier = access_public_node;
7792 finish_member_declaration (value);
7793 current_access_specifier = saved_cas;
7796 /* Returns 1 if TYPE contains only padding bytes. */
7799 is_empty_class (tree type)
7801 if (type == error_mark_node)
7802 return 0;
7804 if (! CLASS_TYPE_P (type))
7805 return 0;
7807 return CLASSTYPE_EMPTY_P (type);
7810 /* Returns true if TYPE contains an empty class. */
7812 static bool
7813 contains_empty_class_p (tree type)
7815 if (is_empty_class (type))
7816 return true;
7817 if (CLASS_TYPE_P (type))
7819 tree field;
7820 tree binfo;
7821 tree base_binfo;
7822 int i;
7824 for (binfo = TYPE_BINFO (type), i = 0;
7825 BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
7826 if (contains_empty_class_p (BINFO_TYPE (base_binfo)))
7827 return true;
7828 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
7829 if (TREE_CODE (field) == FIELD_DECL
7830 && !DECL_ARTIFICIAL (field)
7831 && is_empty_class (TREE_TYPE (field)))
7832 return true;
7834 else if (TREE_CODE (type) == ARRAY_TYPE)
7835 return contains_empty_class_p (TREE_TYPE (type));
7836 return false;
7839 /* Returns true if TYPE contains no actual data, just various
7840 possible combinations of empty classes and possibly a vptr. */
7842 bool
7843 is_really_empty_class (tree type)
7845 if (CLASS_TYPE_P (type))
7847 tree field;
7848 tree binfo;
7849 tree base_binfo;
7850 int i;
7852 /* CLASSTYPE_EMPTY_P isn't set properly until the class is actually laid
7853 out, but we'd like to be able to check this before then. */
7854 if (COMPLETE_TYPE_P (type) && is_empty_class (type))
7855 return true;
7857 for (binfo = TYPE_BINFO (type), i = 0;
7858 BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
7859 if (!is_really_empty_class (BINFO_TYPE (base_binfo)))
7860 return false;
7861 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
7862 if (TREE_CODE (field) == FIELD_DECL
7863 && !DECL_ARTIFICIAL (field)
7864 && !is_really_empty_class (TREE_TYPE (field)))
7865 return false;
7866 return true;
7868 else if (TREE_CODE (type) == ARRAY_TYPE)
7869 return is_really_empty_class (TREE_TYPE (type));
7870 return false;
7873 /* Note that NAME was looked up while the current class was being
7874 defined and that the result of that lookup was DECL. */
7876 void
7877 maybe_note_name_used_in_class (tree name, tree decl)
7879 splay_tree names_used;
7881 /* If we're not defining a class, there's nothing to do. */
7882 if (!(innermost_scope_kind() == sk_class
7883 && TYPE_BEING_DEFINED (current_class_type)
7884 && !LAMBDA_TYPE_P (current_class_type)))
7885 return;
7887 /* If there's already a binding for this NAME, then we don't have
7888 anything to worry about. */
7889 if (lookup_member (current_class_type, name,
7890 /*protect=*/0, /*want_type=*/false, tf_warning_or_error))
7891 return;
7893 if (!current_class_stack[current_class_depth - 1].names_used)
7894 current_class_stack[current_class_depth - 1].names_used
7895 = splay_tree_new (splay_tree_compare_pointers, 0, 0);
7896 names_used = current_class_stack[current_class_depth - 1].names_used;
7898 splay_tree_insert (names_used,
7899 (splay_tree_key) name,
7900 (splay_tree_value) decl);
7903 /* Note that NAME was declared (as DECL) in the current class. Check
7904 to see that the declaration is valid. */
7906 void
7907 note_name_declared_in_class (tree name, tree decl)
7909 splay_tree names_used;
7910 splay_tree_node n;
7912 /* Look to see if we ever used this name. */
7913 names_used
7914 = current_class_stack[current_class_depth - 1].names_used;
7915 if (!names_used)
7916 return;
7917 /* The C language allows members to be declared with a type of the same
7918 name, and the C++ standard says this diagnostic is not required. So
7919 allow it in extern "C" blocks unless predantic is specified.
7920 Allow it in all cases if -ms-extensions is specified. */
7921 if ((!pedantic && current_lang_name == lang_name_c)
7922 || flag_ms_extensions)
7923 return;
7924 n = splay_tree_lookup (names_used, (splay_tree_key) name);
7925 if (n)
7927 /* [basic.scope.class]
7929 A name N used in a class S shall refer to the same declaration
7930 in its context and when re-evaluated in the completed scope of
7931 S. */
7932 permerror (input_location, "declaration of %q#D", decl);
7933 permerror (input_location, "changes meaning of %qD from %q+#D",
7934 DECL_NAME (OVL_CURRENT (decl)), (tree) n->value);
7938 /* Returns the VAR_DECL for the complete vtable associated with BINFO.
7939 Secondary vtables are merged with primary vtables; this function
7940 will return the VAR_DECL for the primary vtable. */
7942 tree
7943 get_vtbl_decl_for_binfo (tree binfo)
7945 tree decl;
7947 decl = BINFO_VTABLE (binfo);
7948 if (decl && TREE_CODE (decl) == POINTER_PLUS_EXPR)
7950 gcc_assert (TREE_CODE (TREE_OPERAND (decl, 0)) == ADDR_EXPR);
7951 decl = TREE_OPERAND (TREE_OPERAND (decl, 0), 0);
7953 if (decl)
7954 gcc_assert (VAR_P (decl));
7955 return decl;
7959 /* Returns the binfo for the primary base of BINFO. If the resulting
7960 BINFO is a virtual base, and it is inherited elsewhere in the
7961 hierarchy, then the returned binfo might not be the primary base of
7962 BINFO in the complete object. Check BINFO_PRIMARY_P or
7963 BINFO_LOST_PRIMARY_P to be sure. */
7965 static tree
7966 get_primary_binfo (tree binfo)
7968 tree primary_base;
7970 primary_base = CLASSTYPE_PRIMARY_BINFO (BINFO_TYPE (binfo));
7971 if (!primary_base)
7972 return NULL_TREE;
7974 return copied_binfo (primary_base, binfo);
7977 /* If INDENTED_P is zero, indent to INDENT. Return nonzero. */
7979 static int
7980 maybe_indent_hierarchy (FILE * stream, int indent, int indented_p)
7982 if (!indented_p)
7983 fprintf (stream, "%*s", indent, "");
7984 return 1;
7987 /* Dump the offsets of all the bases rooted at BINFO to STREAM.
7988 INDENT should be zero when called from the top level; it is
7989 incremented recursively. IGO indicates the next expected BINFO in
7990 inheritance graph ordering. */
7992 static tree
7993 dump_class_hierarchy_r (FILE *stream,
7994 int flags,
7995 tree binfo,
7996 tree igo,
7997 int indent)
7999 int indented = 0;
8000 tree base_binfo;
8001 int i;
8003 indented = maybe_indent_hierarchy (stream, indent, 0);
8004 fprintf (stream, "%s (0x" HOST_WIDE_INT_PRINT_HEX ") ",
8005 type_as_string (BINFO_TYPE (binfo), TFF_PLAIN_IDENTIFIER),
8006 (HOST_WIDE_INT) (uintptr_t) binfo);
8007 if (binfo != igo)
8009 fprintf (stream, "alternative-path\n");
8010 return igo;
8012 igo = TREE_CHAIN (binfo);
8014 fprintf (stream, HOST_WIDE_INT_PRINT_DEC,
8015 tree_to_shwi (BINFO_OFFSET (binfo)));
8016 if (is_empty_class (BINFO_TYPE (binfo)))
8017 fprintf (stream, " empty");
8018 else if (CLASSTYPE_NEARLY_EMPTY_P (BINFO_TYPE (binfo)))
8019 fprintf (stream, " nearly-empty");
8020 if (BINFO_VIRTUAL_P (binfo))
8021 fprintf (stream, " virtual");
8022 fprintf (stream, "\n");
8024 indented = 0;
8025 if (BINFO_PRIMARY_P (binfo))
8027 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
8028 fprintf (stream, " primary-for %s (0x" HOST_WIDE_INT_PRINT_HEX ")",
8029 type_as_string (BINFO_TYPE (BINFO_INHERITANCE_CHAIN (binfo)),
8030 TFF_PLAIN_IDENTIFIER),
8031 (HOST_WIDE_INT) (uintptr_t) BINFO_INHERITANCE_CHAIN (binfo));
8033 if (BINFO_LOST_PRIMARY_P (binfo))
8035 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
8036 fprintf (stream, " lost-primary");
8038 if (indented)
8039 fprintf (stream, "\n");
8041 if (!(flags & TDF_SLIM))
8043 int indented = 0;
8045 if (BINFO_SUBVTT_INDEX (binfo))
8047 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
8048 fprintf (stream, " subvttidx=%s",
8049 expr_as_string (BINFO_SUBVTT_INDEX (binfo),
8050 TFF_PLAIN_IDENTIFIER));
8052 if (BINFO_VPTR_INDEX (binfo))
8054 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
8055 fprintf (stream, " vptridx=%s",
8056 expr_as_string (BINFO_VPTR_INDEX (binfo),
8057 TFF_PLAIN_IDENTIFIER));
8059 if (BINFO_VPTR_FIELD (binfo))
8061 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
8062 fprintf (stream, " vbaseoffset=%s",
8063 expr_as_string (BINFO_VPTR_FIELD (binfo),
8064 TFF_PLAIN_IDENTIFIER));
8066 if (BINFO_VTABLE (binfo))
8068 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
8069 fprintf (stream, " vptr=%s",
8070 expr_as_string (BINFO_VTABLE (binfo),
8071 TFF_PLAIN_IDENTIFIER));
8074 if (indented)
8075 fprintf (stream, "\n");
8078 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
8079 igo = dump_class_hierarchy_r (stream, flags, base_binfo, igo, indent + 2);
8081 return igo;
8084 /* Dump the BINFO hierarchy for T. */
8086 static void
8087 dump_class_hierarchy_1 (FILE *stream, int flags, tree t)
8089 fprintf (stream, "Class %s\n", type_as_string (t, TFF_PLAIN_IDENTIFIER));
8090 fprintf (stream, " size=%lu align=%lu\n",
8091 (unsigned long)(tree_to_shwi (TYPE_SIZE (t)) / BITS_PER_UNIT),
8092 (unsigned long)(TYPE_ALIGN (t) / BITS_PER_UNIT));
8093 fprintf (stream, " base size=%lu base align=%lu\n",
8094 (unsigned long)(tree_to_shwi (TYPE_SIZE (CLASSTYPE_AS_BASE (t)))
8095 / BITS_PER_UNIT),
8096 (unsigned long)(TYPE_ALIGN (CLASSTYPE_AS_BASE (t))
8097 / BITS_PER_UNIT));
8098 dump_class_hierarchy_r (stream, flags, TYPE_BINFO (t), TYPE_BINFO (t), 0);
8099 fprintf (stream, "\n");
8102 /* Debug interface to hierarchy dumping. */
8104 void
8105 debug_class (tree t)
8107 dump_class_hierarchy_1 (stderr, TDF_SLIM, t);
8110 static void
8111 dump_class_hierarchy (tree t)
8113 int flags;
8114 FILE *stream = get_dump_info (TDI_class, &flags);
8116 if (stream)
8118 dump_class_hierarchy_1 (stream, flags, t);
8122 static void
8123 dump_array (FILE * stream, tree decl)
8125 tree value;
8126 unsigned HOST_WIDE_INT ix;
8127 HOST_WIDE_INT elt;
8128 tree size = TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (decl)));
8130 elt = (tree_to_shwi (TYPE_SIZE (TREE_TYPE (TREE_TYPE (decl))))
8131 / BITS_PER_UNIT);
8132 fprintf (stream, "%s:", decl_as_string (decl, TFF_PLAIN_IDENTIFIER));
8133 fprintf (stream, " %s entries",
8134 expr_as_string (size_binop (PLUS_EXPR, size, size_one_node),
8135 TFF_PLAIN_IDENTIFIER));
8136 fprintf (stream, "\n");
8138 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (DECL_INITIAL (decl)),
8139 ix, value)
8140 fprintf (stream, "%-4ld %s\n", (long)(ix * elt),
8141 expr_as_string (value, TFF_PLAIN_IDENTIFIER));
8144 static void
8145 dump_vtable (tree t, tree binfo, tree vtable)
8147 int flags;
8148 FILE *stream = get_dump_info (TDI_class, &flags);
8150 if (!stream)
8151 return;
8153 if (!(flags & TDF_SLIM))
8155 int ctor_vtbl_p = TYPE_BINFO (t) != binfo;
8157 fprintf (stream, "%s for %s",
8158 ctor_vtbl_p ? "Construction vtable" : "Vtable",
8159 type_as_string (BINFO_TYPE (binfo), TFF_PLAIN_IDENTIFIER));
8160 if (ctor_vtbl_p)
8162 if (!BINFO_VIRTUAL_P (binfo))
8163 fprintf (stream, " (0x" HOST_WIDE_INT_PRINT_HEX " instance)",
8164 (HOST_WIDE_INT) (uintptr_t) binfo);
8165 fprintf (stream, " in %s", type_as_string (t, TFF_PLAIN_IDENTIFIER));
8167 fprintf (stream, "\n");
8168 dump_array (stream, vtable);
8169 fprintf (stream, "\n");
8173 static void
8174 dump_vtt (tree t, tree vtt)
8176 int flags;
8177 FILE *stream = get_dump_info (TDI_class, &flags);
8179 if (!stream)
8180 return;
8182 if (!(flags & TDF_SLIM))
8184 fprintf (stream, "VTT for %s\n",
8185 type_as_string (t, TFF_PLAIN_IDENTIFIER));
8186 dump_array (stream, vtt);
8187 fprintf (stream, "\n");
8191 /* Dump a function or thunk and its thunkees. */
8193 static void
8194 dump_thunk (FILE *stream, int indent, tree thunk)
8196 static const char spaces[] = " ";
8197 tree name = DECL_NAME (thunk);
8198 tree thunks;
8200 fprintf (stream, "%.*s%p %s %s", indent, spaces,
8201 (void *)thunk,
8202 !DECL_THUNK_P (thunk) ? "function"
8203 : DECL_THIS_THUNK_P (thunk) ? "this-thunk" : "covariant-thunk",
8204 name ? IDENTIFIER_POINTER (name) : "<unset>");
8205 if (DECL_THUNK_P (thunk))
8207 HOST_WIDE_INT fixed_adjust = THUNK_FIXED_OFFSET (thunk);
8208 tree virtual_adjust = THUNK_VIRTUAL_OFFSET (thunk);
8210 fprintf (stream, " fixed=" HOST_WIDE_INT_PRINT_DEC, fixed_adjust);
8211 if (!virtual_adjust)
8212 /*NOP*/;
8213 else if (DECL_THIS_THUNK_P (thunk))
8214 fprintf (stream, " vcall=" HOST_WIDE_INT_PRINT_DEC,
8215 tree_to_shwi (virtual_adjust));
8216 else
8217 fprintf (stream, " vbase=" HOST_WIDE_INT_PRINT_DEC "(%s)",
8218 tree_to_shwi (BINFO_VPTR_FIELD (virtual_adjust)),
8219 type_as_string (BINFO_TYPE (virtual_adjust), TFF_SCOPE));
8220 if (THUNK_ALIAS (thunk))
8221 fprintf (stream, " alias to %p", (void *)THUNK_ALIAS (thunk));
8223 fprintf (stream, "\n");
8224 for (thunks = DECL_THUNKS (thunk); thunks; thunks = TREE_CHAIN (thunks))
8225 dump_thunk (stream, indent + 2, thunks);
8228 /* Dump the thunks for FN. */
8230 void
8231 debug_thunks (tree fn)
8233 dump_thunk (stderr, 0, fn);
8236 /* Virtual function table initialization. */
8238 /* Create all the necessary vtables for T and its base classes. */
8240 static void
8241 finish_vtbls (tree t)
8243 tree vbase;
8244 vec<constructor_elt, va_gc> *v = NULL;
8245 tree vtable = BINFO_VTABLE (TYPE_BINFO (t));
8247 /* We lay out the primary and secondary vtables in one contiguous
8248 vtable. The primary vtable is first, followed by the non-virtual
8249 secondary vtables in inheritance graph order. */
8250 accumulate_vtbl_inits (TYPE_BINFO (t), TYPE_BINFO (t), TYPE_BINFO (t),
8251 vtable, t, &v);
8253 /* Then come the virtual bases, also in inheritance graph order. */
8254 for (vbase = TYPE_BINFO (t); vbase; vbase = TREE_CHAIN (vbase))
8256 if (!BINFO_VIRTUAL_P (vbase))
8257 continue;
8258 accumulate_vtbl_inits (vbase, vbase, TYPE_BINFO (t), vtable, t, &v);
8261 if (BINFO_VTABLE (TYPE_BINFO (t)))
8262 initialize_vtable (TYPE_BINFO (t), v);
8265 /* Initialize the vtable for BINFO with the INITS. */
8267 static void
8268 initialize_vtable (tree binfo, vec<constructor_elt, va_gc> *inits)
8270 tree decl;
8272 layout_vtable_decl (binfo, vec_safe_length (inits));
8273 decl = get_vtbl_decl_for_binfo (binfo);
8274 initialize_artificial_var (decl, inits);
8275 dump_vtable (BINFO_TYPE (binfo), binfo, decl);
8278 /* Build the VTT (virtual table table) for T.
8279 A class requires a VTT if it has virtual bases.
8281 This holds
8282 1 - primary virtual pointer for complete object T
8283 2 - secondary VTTs for each direct non-virtual base of T which requires a
8285 3 - secondary virtual pointers for each direct or indirect base of T which
8286 has virtual bases or is reachable via a virtual path from T.
8287 4 - secondary VTTs for each direct or indirect virtual base of T.
8289 Secondary VTTs look like complete object VTTs without part 4. */
8291 static void
8292 build_vtt (tree t)
8294 tree type;
8295 tree vtt;
8296 tree index;
8297 vec<constructor_elt, va_gc> *inits;
8299 /* Build up the initializers for the VTT. */
8300 inits = NULL;
8301 index = size_zero_node;
8302 build_vtt_inits (TYPE_BINFO (t), t, &inits, &index);
8304 /* If we didn't need a VTT, we're done. */
8305 if (!inits)
8306 return;
8308 /* Figure out the type of the VTT. */
8309 type = build_array_of_n_type (const_ptr_type_node,
8310 inits->length ());
8312 /* Now, build the VTT object itself. */
8313 vtt = build_vtable (t, mangle_vtt_for_type (t), type);
8314 initialize_artificial_var (vtt, inits);
8315 /* Add the VTT to the vtables list. */
8316 DECL_CHAIN (vtt) = DECL_CHAIN (CLASSTYPE_VTABLES (t));
8317 DECL_CHAIN (CLASSTYPE_VTABLES (t)) = vtt;
8319 dump_vtt (t, vtt);
8322 /* When building a secondary VTT, BINFO_VTABLE is set to a TREE_LIST with
8323 PURPOSE the RTTI_BINFO, VALUE the real vtable pointer for this binfo,
8324 and CHAIN the vtable pointer for this binfo after construction is
8325 complete. VALUE can also be another BINFO, in which case we recurse. */
8327 static tree
8328 binfo_ctor_vtable (tree binfo)
8330 tree vt;
8332 while (1)
8334 vt = BINFO_VTABLE (binfo);
8335 if (TREE_CODE (vt) == TREE_LIST)
8336 vt = TREE_VALUE (vt);
8337 if (TREE_CODE (vt) == TREE_BINFO)
8338 binfo = vt;
8339 else
8340 break;
8343 return vt;
8346 /* Data for secondary VTT initialization. */
8347 typedef struct secondary_vptr_vtt_init_data_s
8349 /* Is this the primary VTT? */
8350 bool top_level_p;
8352 /* Current index into the VTT. */
8353 tree index;
8355 /* Vector of initializers built up. */
8356 vec<constructor_elt, va_gc> *inits;
8358 /* The type being constructed by this secondary VTT. */
8359 tree type_being_constructed;
8360 } secondary_vptr_vtt_init_data;
8362 /* Recursively build the VTT-initializer for BINFO (which is in the
8363 hierarchy dominated by T). INITS points to the end of the initializer
8364 list to date. INDEX is the VTT index where the next element will be
8365 replaced. Iff BINFO is the binfo for T, this is the top level VTT (i.e.
8366 not a subvtt for some base of T). When that is so, we emit the sub-VTTs
8367 for virtual bases of T. When it is not so, we build the constructor
8368 vtables for the BINFO-in-T variant. */
8370 static void
8371 build_vtt_inits (tree binfo, tree t, vec<constructor_elt, va_gc> **inits,
8372 tree *index)
8374 int i;
8375 tree b;
8376 tree init;
8377 secondary_vptr_vtt_init_data data;
8378 int top_level_p = SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), t);
8380 /* We only need VTTs for subobjects with virtual bases. */
8381 if (!CLASSTYPE_VBASECLASSES (BINFO_TYPE (binfo)))
8382 return;
8384 /* We need to use a construction vtable if this is not the primary
8385 VTT. */
8386 if (!top_level_p)
8388 build_ctor_vtbl_group (binfo, t);
8390 /* Record the offset in the VTT where this sub-VTT can be found. */
8391 BINFO_SUBVTT_INDEX (binfo) = *index;
8394 /* Add the address of the primary vtable for the complete object. */
8395 init = binfo_ctor_vtable (binfo);
8396 CONSTRUCTOR_APPEND_ELT (*inits, NULL_TREE, init);
8397 if (top_level_p)
8399 gcc_assert (!BINFO_VPTR_INDEX (binfo));
8400 BINFO_VPTR_INDEX (binfo) = *index;
8402 *index = size_binop (PLUS_EXPR, *index, TYPE_SIZE_UNIT (ptr_type_node));
8404 /* Recursively add the secondary VTTs for non-virtual bases. */
8405 for (i = 0; BINFO_BASE_ITERATE (binfo, i, b); ++i)
8406 if (!BINFO_VIRTUAL_P (b))
8407 build_vtt_inits (b, t, inits, index);
8409 /* Add secondary virtual pointers for all subobjects of BINFO with
8410 either virtual bases or reachable along a virtual path, except
8411 subobjects that are non-virtual primary bases. */
8412 data.top_level_p = top_level_p;
8413 data.index = *index;
8414 data.inits = *inits;
8415 data.type_being_constructed = BINFO_TYPE (binfo);
8417 dfs_walk_once (binfo, dfs_build_secondary_vptr_vtt_inits, NULL, &data);
8419 *index = data.index;
8421 /* data.inits might have grown as we added secondary virtual pointers.
8422 Make sure our caller knows about the new vector. */
8423 *inits = data.inits;
8425 if (top_level_p)
8426 /* Add the secondary VTTs for virtual bases in inheritance graph
8427 order. */
8428 for (b = TYPE_BINFO (BINFO_TYPE (binfo)); b; b = TREE_CHAIN (b))
8430 if (!BINFO_VIRTUAL_P (b))
8431 continue;
8433 build_vtt_inits (b, t, inits, index);
8435 else
8436 /* Remove the ctor vtables we created. */
8437 dfs_walk_all (binfo, dfs_fixup_binfo_vtbls, NULL, binfo);
8440 /* Called from build_vtt_inits via dfs_walk. BINFO is the binfo for the base
8441 in most derived. DATA is a SECONDARY_VPTR_VTT_INIT_DATA structure. */
8443 static tree
8444 dfs_build_secondary_vptr_vtt_inits (tree binfo, void *data_)
8446 secondary_vptr_vtt_init_data *data = (secondary_vptr_vtt_init_data *)data_;
8448 /* We don't care about bases that don't have vtables. */
8449 if (!TYPE_VFIELD (BINFO_TYPE (binfo)))
8450 return dfs_skip_bases;
8452 /* We're only interested in proper subobjects of the type being
8453 constructed. */
8454 if (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), data->type_being_constructed))
8455 return NULL_TREE;
8457 /* We're only interested in bases with virtual bases or reachable
8458 via a virtual path from the type being constructed. */
8459 if (!(CLASSTYPE_VBASECLASSES (BINFO_TYPE (binfo))
8460 || binfo_via_virtual (binfo, data->type_being_constructed)))
8461 return dfs_skip_bases;
8463 /* We're not interested in non-virtual primary bases. */
8464 if (!BINFO_VIRTUAL_P (binfo) && BINFO_PRIMARY_P (binfo))
8465 return NULL_TREE;
8467 /* Record the index where this secondary vptr can be found. */
8468 if (data->top_level_p)
8470 gcc_assert (!BINFO_VPTR_INDEX (binfo));
8471 BINFO_VPTR_INDEX (binfo) = data->index;
8473 if (BINFO_VIRTUAL_P (binfo))
8475 /* It's a primary virtual base, and this is not a
8476 construction vtable. Find the base this is primary of in
8477 the inheritance graph, and use that base's vtable
8478 now. */
8479 while (BINFO_PRIMARY_P (binfo))
8480 binfo = BINFO_INHERITANCE_CHAIN (binfo);
8484 /* Add the initializer for the secondary vptr itself. */
8485 CONSTRUCTOR_APPEND_ELT (data->inits, NULL_TREE, binfo_ctor_vtable (binfo));
8487 /* Advance the vtt index. */
8488 data->index = size_binop (PLUS_EXPR, data->index,
8489 TYPE_SIZE_UNIT (ptr_type_node));
8491 return NULL_TREE;
8494 /* Called from build_vtt_inits via dfs_walk. After building
8495 constructor vtables and generating the sub-vtt from them, we need
8496 to restore the BINFO_VTABLES that were scribbled on. DATA is the
8497 binfo of the base whose sub vtt was generated. */
8499 static tree
8500 dfs_fixup_binfo_vtbls (tree binfo, void* data)
8502 tree vtable = BINFO_VTABLE (binfo);
8504 if (!TYPE_CONTAINS_VPTR_P (BINFO_TYPE (binfo)))
8505 /* If this class has no vtable, none of its bases do. */
8506 return dfs_skip_bases;
8508 if (!vtable)
8509 /* This might be a primary base, so have no vtable in this
8510 hierarchy. */
8511 return NULL_TREE;
8513 /* If we scribbled the construction vtable vptr into BINFO, clear it
8514 out now. */
8515 if (TREE_CODE (vtable) == TREE_LIST
8516 && (TREE_PURPOSE (vtable) == (tree) data))
8517 BINFO_VTABLE (binfo) = TREE_CHAIN (vtable);
8519 return NULL_TREE;
8522 /* Build the construction vtable group for BINFO which is in the
8523 hierarchy dominated by T. */
8525 static void
8526 build_ctor_vtbl_group (tree binfo, tree t)
8528 tree type;
8529 tree vtbl;
8530 tree id;
8531 tree vbase;
8532 vec<constructor_elt, va_gc> *v;
8534 /* See if we've already created this construction vtable group. */
8535 id = mangle_ctor_vtbl_for_type (t, binfo);
8536 if (IDENTIFIER_GLOBAL_VALUE (id))
8537 return;
8539 gcc_assert (!SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), t));
8540 /* Build a version of VTBL (with the wrong type) for use in
8541 constructing the addresses of secondary vtables in the
8542 construction vtable group. */
8543 vtbl = build_vtable (t, id, ptr_type_node);
8544 DECL_CONSTRUCTION_VTABLE_P (vtbl) = 1;
8545 /* Don't export construction vtables from shared libraries. Even on
8546 targets that don't support hidden visibility, this tells
8547 can_refer_decl_in_current_unit_p not to assume that it's safe to
8548 access from a different compilation unit (bz 54314). */
8549 DECL_VISIBILITY (vtbl) = VISIBILITY_HIDDEN;
8550 DECL_VISIBILITY_SPECIFIED (vtbl) = true;
8552 v = NULL;
8553 accumulate_vtbl_inits (binfo, TYPE_BINFO (TREE_TYPE (binfo)),
8554 binfo, vtbl, t, &v);
8556 /* Add the vtables for each of our virtual bases using the vbase in T
8557 binfo. */
8558 for (vbase = TYPE_BINFO (BINFO_TYPE (binfo));
8559 vbase;
8560 vbase = TREE_CHAIN (vbase))
8562 tree b;
8564 if (!BINFO_VIRTUAL_P (vbase))
8565 continue;
8566 b = copied_binfo (vbase, binfo);
8568 accumulate_vtbl_inits (b, vbase, binfo, vtbl, t, &v);
8571 /* Figure out the type of the construction vtable. */
8572 type = build_array_of_n_type (vtable_entry_type, v->length ());
8573 layout_type (type);
8574 TREE_TYPE (vtbl) = type;
8575 DECL_SIZE (vtbl) = DECL_SIZE_UNIT (vtbl) = NULL_TREE;
8576 layout_decl (vtbl, 0);
8578 /* Initialize the construction vtable. */
8579 CLASSTYPE_VTABLES (t) = chainon (CLASSTYPE_VTABLES (t), vtbl);
8580 initialize_artificial_var (vtbl, v);
8581 dump_vtable (t, binfo, vtbl);
8584 /* Add the vtbl initializers for BINFO (and its bases other than
8585 non-virtual primaries) to the list of INITS. BINFO is in the
8586 hierarchy dominated by T. RTTI_BINFO is the binfo within T of
8587 the constructor the vtbl inits should be accumulated for. (If this
8588 is the complete object vtbl then RTTI_BINFO will be TYPE_BINFO (T).)
8589 ORIG_BINFO is the binfo for this object within BINFO_TYPE (RTTI_BINFO).
8590 BINFO is the active base equivalent of ORIG_BINFO in the inheritance
8591 graph of T. Both BINFO and ORIG_BINFO will have the same BINFO_TYPE,
8592 but are not necessarily the same in terms of layout. */
8594 static void
8595 accumulate_vtbl_inits (tree binfo,
8596 tree orig_binfo,
8597 tree rtti_binfo,
8598 tree vtbl,
8599 tree t,
8600 vec<constructor_elt, va_gc> **inits)
8602 int i;
8603 tree base_binfo;
8604 int ctor_vtbl_p = !SAME_BINFO_TYPE_P (BINFO_TYPE (rtti_binfo), t);
8606 gcc_assert (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), BINFO_TYPE (orig_binfo)));
8608 /* If it doesn't have a vptr, we don't do anything. */
8609 if (!TYPE_CONTAINS_VPTR_P (BINFO_TYPE (binfo)))
8610 return;
8612 /* If we're building a construction vtable, we're not interested in
8613 subobjects that don't require construction vtables. */
8614 if (ctor_vtbl_p
8615 && !CLASSTYPE_VBASECLASSES (BINFO_TYPE (binfo))
8616 && !binfo_via_virtual (orig_binfo, BINFO_TYPE (rtti_binfo)))
8617 return;
8619 /* Build the initializers for the BINFO-in-T vtable. */
8620 dfs_accumulate_vtbl_inits (binfo, orig_binfo, rtti_binfo, vtbl, t, inits);
8622 /* Walk the BINFO and its bases. We walk in preorder so that as we
8623 initialize each vtable we can figure out at what offset the
8624 secondary vtable lies from the primary vtable. We can't use
8625 dfs_walk here because we need to iterate through bases of BINFO
8626 and RTTI_BINFO simultaneously. */
8627 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
8629 /* Skip virtual bases. */
8630 if (BINFO_VIRTUAL_P (base_binfo))
8631 continue;
8632 accumulate_vtbl_inits (base_binfo,
8633 BINFO_BASE_BINFO (orig_binfo, i),
8634 rtti_binfo, vtbl, t,
8635 inits);
8639 /* Called from accumulate_vtbl_inits. Adds the initializers for the
8640 BINFO vtable to L. */
8642 static void
8643 dfs_accumulate_vtbl_inits (tree binfo,
8644 tree orig_binfo,
8645 tree rtti_binfo,
8646 tree orig_vtbl,
8647 tree t,
8648 vec<constructor_elt, va_gc> **l)
8650 tree vtbl = NULL_TREE;
8651 int ctor_vtbl_p = !SAME_BINFO_TYPE_P (BINFO_TYPE (rtti_binfo), t);
8652 int n_inits;
8654 if (ctor_vtbl_p
8655 && BINFO_VIRTUAL_P (orig_binfo) && BINFO_PRIMARY_P (orig_binfo))
8657 /* In the hierarchy of BINFO_TYPE (RTTI_BINFO), this is a
8658 primary virtual base. If it is not the same primary in
8659 the hierarchy of T, we'll need to generate a ctor vtable
8660 for it, to place at its location in T. If it is the same
8661 primary, we still need a VTT entry for the vtable, but it
8662 should point to the ctor vtable for the base it is a
8663 primary for within the sub-hierarchy of RTTI_BINFO.
8665 There are three possible cases:
8667 1) We are in the same place.
8668 2) We are a primary base within a lost primary virtual base of
8669 RTTI_BINFO.
8670 3) We are primary to something not a base of RTTI_BINFO. */
8672 tree b;
8673 tree last = NULL_TREE;
8675 /* First, look through the bases we are primary to for RTTI_BINFO
8676 or a virtual base. */
8677 b = binfo;
8678 while (BINFO_PRIMARY_P (b))
8680 b = BINFO_INHERITANCE_CHAIN (b);
8681 last = b;
8682 if (BINFO_VIRTUAL_P (b) || b == rtti_binfo)
8683 goto found;
8685 /* If we run out of primary links, keep looking down our
8686 inheritance chain; we might be an indirect primary. */
8687 for (b = last; b; b = BINFO_INHERITANCE_CHAIN (b))
8688 if (BINFO_VIRTUAL_P (b) || b == rtti_binfo)
8689 break;
8690 found:
8692 /* If we found RTTI_BINFO, this is case 1. If we found a virtual
8693 base B and it is a base of RTTI_BINFO, this is case 2. In
8694 either case, we share our vtable with LAST, i.e. the
8695 derived-most base within B of which we are a primary. */
8696 if (b == rtti_binfo
8697 || (b && binfo_for_vbase (BINFO_TYPE (b), BINFO_TYPE (rtti_binfo))))
8698 /* Just set our BINFO_VTABLE to point to LAST, as we may not have
8699 set LAST's BINFO_VTABLE yet. We'll extract the actual vptr in
8700 binfo_ctor_vtable after everything's been set up. */
8701 vtbl = last;
8703 /* Otherwise, this is case 3 and we get our own. */
8705 else if (!BINFO_NEW_VTABLE_MARKED (orig_binfo))
8706 return;
8708 n_inits = vec_safe_length (*l);
8710 if (!vtbl)
8712 tree index;
8713 int non_fn_entries;
8715 /* Add the initializer for this vtable. */
8716 build_vtbl_initializer (binfo, orig_binfo, t, rtti_binfo,
8717 &non_fn_entries, l);
8719 /* Figure out the position to which the VPTR should point. */
8720 vtbl = build1 (ADDR_EXPR, vtbl_ptr_type_node, orig_vtbl);
8721 index = size_binop (MULT_EXPR,
8722 TYPE_SIZE_UNIT (vtable_entry_type),
8723 size_int (non_fn_entries + n_inits));
8724 vtbl = fold_build_pointer_plus (vtbl, index);
8727 if (ctor_vtbl_p)
8728 /* For a construction vtable, we can't overwrite BINFO_VTABLE.
8729 So, we make a TREE_LIST. Later, dfs_fixup_binfo_vtbls will
8730 straighten this out. */
8731 BINFO_VTABLE (binfo) = tree_cons (rtti_binfo, vtbl, BINFO_VTABLE (binfo));
8732 else if (BINFO_PRIMARY_P (binfo) && BINFO_VIRTUAL_P (binfo))
8733 /* Throw away any unneeded intializers. */
8734 (*l)->truncate (n_inits);
8735 else
8736 /* For an ordinary vtable, set BINFO_VTABLE. */
8737 BINFO_VTABLE (binfo) = vtbl;
8740 static GTY(()) tree abort_fndecl_addr;
8742 /* Construct the initializer for BINFO's virtual function table. BINFO
8743 is part of the hierarchy dominated by T. If we're building a
8744 construction vtable, the ORIG_BINFO is the binfo we should use to
8745 find the actual function pointers to put in the vtable - but they
8746 can be overridden on the path to most-derived in the graph that
8747 ORIG_BINFO belongs. Otherwise,
8748 ORIG_BINFO should be the same as BINFO. The RTTI_BINFO is the
8749 BINFO that should be indicated by the RTTI information in the
8750 vtable; it will be a base class of T, rather than T itself, if we
8751 are building a construction vtable.
8753 The value returned is a TREE_LIST suitable for wrapping in a
8754 CONSTRUCTOR to use as the DECL_INITIAL for a vtable. If
8755 NON_FN_ENTRIES_P is not NULL, *NON_FN_ENTRIES_P is set to the
8756 number of non-function entries in the vtable.
8758 It might seem that this function should never be called with a
8759 BINFO for which BINFO_PRIMARY_P holds, the vtable for such a
8760 base is always subsumed by a derived class vtable. However, when
8761 we are building construction vtables, we do build vtables for
8762 primary bases; we need these while the primary base is being
8763 constructed. */
8765 static void
8766 build_vtbl_initializer (tree binfo,
8767 tree orig_binfo,
8768 tree t,
8769 tree rtti_binfo,
8770 int* non_fn_entries_p,
8771 vec<constructor_elt, va_gc> **inits)
8773 tree v;
8774 vtbl_init_data vid;
8775 unsigned ix, jx;
8776 tree vbinfo;
8777 vec<tree, va_gc> *vbases;
8778 constructor_elt *e;
8780 /* Initialize VID. */
8781 memset (&vid, 0, sizeof (vid));
8782 vid.binfo = binfo;
8783 vid.derived = t;
8784 vid.rtti_binfo = rtti_binfo;
8785 vid.primary_vtbl_p = SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), t);
8786 vid.ctor_vtbl_p = !SAME_BINFO_TYPE_P (BINFO_TYPE (rtti_binfo), t);
8787 vid.generate_vcall_entries = true;
8788 /* The first vbase or vcall offset is at index -3 in the vtable. */
8789 vid.index = ssize_int(-3 * TARGET_VTABLE_DATA_ENTRY_DISTANCE);
8791 /* Add entries to the vtable for RTTI. */
8792 build_rtti_vtbl_entries (binfo, &vid);
8794 /* Create an array for keeping track of the functions we've
8795 processed. When we see multiple functions with the same
8796 signature, we share the vcall offsets. */
8797 vec_alloc (vid.fns, 32);
8798 /* Add the vcall and vbase offset entries. */
8799 build_vcall_and_vbase_vtbl_entries (binfo, &vid);
8801 /* Clear BINFO_VTABLE_PATH_MARKED; it's set by
8802 build_vbase_offset_vtbl_entries. */
8803 for (vbases = CLASSTYPE_VBASECLASSES (t), ix = 0;
8804 vec_safe_iterate (vbases, ix, &vbinfo); ix++)
8805 BINFO_VTABLE_PATH_MARKED (vbinfo) = 0;
8807 /* If the target requires padding between data entries, add that now. */
8808 if (TARGET_VTABLE_DATA_ENTRY_DISTANCE > 1)
8810 int n_entries = vec_safe_length (vid.inits);
8812 vec_safe_grow (vid.inits, TARGET_VTABLE_DATA_ENTRY_DISTANCE * n_entries);
8814 /* Move data entries into their new positions and add padding
8815 after the new positions. Iterate backwards so we don't
8816 overwrite entries that we would need to process later. */
8817 for (ix = n_entries - 1;
8818 vid.inits->iterate (ix, &e);
8819 ix--)
8821 int j;
8822 int new_position = (TARGET_VTABLE_DATA_ENTRY_DISTANCE * ix
8823 + (TARGET_VTABLE_DATA_ENTRY_DISTANCE - 1));
8825 (*vid.inits)[new_position] = *e;
8827 for (j = 1; j < TARGET_VTABLE_DATA_ENTRY_DISTANCE; ++j)
8829 constructor_elt *f = &(*vid.inits)[new_position - j];
8830 f->index = NULL_TREE;
8831 f->value = build1 (NOP_EXPR, vtable_entry_type,
8832 null_pointer_node);
8837 if (non_fn_entries_p)
8838 *non_fn_entries_p = vec_safe_length (vid.inits);
8840 /* The initializers for virtual functions were built up in reverse
8841 order. Straighten them out and add them to the running list in one
8842 step. */
8843 jx = vec_safe_length (*inits);
8844 vec_safe_grow (*inits, jx + vid.inits->length ());
8846 for (ix = vid.inits->length () - 1;
8847 vid.inits->iterate (ix, &e);
8848 ix--, jx++)
8849 (**inits)[jx] = *e;
8851 /* Go through all the ordinary virtual functions, building up
8852 initializers. */
8853 for (v = BINFO_VIRTUALS (orig_binfo); v; v = TREE_CHAIN (v))
8855 tree delta;
8856 tree vcall_index;
8857 tree fn, fn_original;
8858 tree init = NULL_TREE;
8860 fn = BV_FN (v);
8861 fn_original = fn;
8862 if (DECL_THUNK_P (fn))
8864 if (!DECL_NAME (fn))
8865 finish_thunk (fn);
8866 if (THUNK_ALIAS (fn))
8868 fn = THUNK_ALIAS (fn);
8869 BV_FN (v) = fn;
8871 fn_original = THUNK_TARGET (fn);
8874 /* If the only definition of this function signature along our
8875 primary base chain is from a lost primary, this vtable slot will
8876 never be used, so just zero it out. This is important to avoid
8877 requiring extra thunks which cannot be generated with the function.
8879 We first check this in update_vtable_entry_for_fn, so we handle
8880 restored primary bases properly; we also need to do it here so we
8881 zero out unused slots in ctor vtables, rather than filling them
8882 with erroneous values (though harmless, apart from relocation
8883 costs). */
8884 if (BV_LOST_PRIMARY (v))
8885 init = size_zero_node;
8887 if (! init)
8889 /* Pull the offset for `this', and the function to call, out of
8890 the list. */
8891 delta = BV_DELTA (v);
8892 vcall_index = BV_VCALL_INDEX (v);
8894 gcc_assert (TREE_CODE (delta) == INTEGER_CST);
8895 gcc_assert (TREE_CODE (fn) == FUNCTION_DECL);
8897 /* You can't call an abstract virtual function; it's abstract.
8898 So, we replace these functions with __pure_virtual. */
8899 if (DECL_PURE_VIRTUAL_P (fn_original))
8901 fn = abort_fndecl;
8902 if (!TARGET_VTABLE_USES_DESCRIPTORS)
8904 if (abort_fndecl_addr == NULL)
8905 abort_fndecl_addr
8906 = fold_convert (vfunc_ptr_type_node,
8907 build_fold_addr_expr (fn));
8908 init = abort_fndecl_addr;
8911 /* Likewise for deleted virtuals. */
8912 else if (DECL_DELETED_FN (fn_original))
8914 fn = get_identifier ("__cxa_deleted_virtual");
8915 if (!get_global_value_if_present (fn, &fn))
8916 fn = push_library_fn (fn, (build_function_type_list
8917 (void_type_node, NULL_TREE)),
8918 NULL_TREE, ECF_NORETURN);
8919 if (!TARGET_VTABLE_USES_DESCRIPTORS)
8920 init = fold_convert (vfunc_ptr_type_node,
8921 build_fold_addr_expr (fn));
8923 else
8925 if (!integer_zerop (delta) || vcall_index)
8927 fn = make_thunk (fn, /*this_adjusting=*/1, delta, vcall_index);
8928 if (!DECL_NAME (fn))
8929 finish_thunk (fn);
8931 /* Take the address of the function, considering it to be of an
8932 appropriate generic type. */
8933 if (!TARGET_VTABLE_USES_DESCRIPTORS)
8934 init = fold_convert (vfunc_ptr_type_node,
8935 build_fold_addr_expr (fn));
8936 /* Don't refer to a virtual destructor from a constructor
8937 vtable or a vtable for an abstract class, since destroying
8938 an object under construction is undefined behavior and we
8939 don't want it to be considered a candidate for speculative
8940 devirtualization. But do create the thunk for ABI
8941 compliance. */
8942 if (DECL_DESTRUCTOR_P (fn_original)
8943 && (CLASSTYPE_PURE_VIRTUALS (DECL_CONTEXT (fn_original))
8944 || orig_binfo != binfo))
8945 init = size_zero_node;
8949 /* And add it to the chain of initializers. */
8950 if (TARGET_VTABLE_USES_DESCRIPTORS)
8952 int i;
8953 if (init == size_zero_node)
8954 for (i = 0; i < TARGET_VTABLE_USES_DESCRIPTORS; ++i)
8955 CONSTRUCTOR_APPEND_ELT (*inits, NULL_TREE, init);
8956 else
8957 for (i = 0; i < TARGET_VTABLE_USES_DESCRIPTORS; ++i)
8959 tree fdesc = build2 (FDESC_EXPR, vfunc_ptr_type_node,
8960 fn, build_int_cst (NULL_TREE, i));
8961 TREE_CONSTANT (fdesc) = 1;
8963 CONSTRUCTOR_APPEND_ELT (*inits, NULL_TREE, fdesc);
8966 else
8967 CONSTRUCTOR_APPEND_ELT (*inits, NULL_TREE, init);
8971 /* Adds to vid->inits the initializers for the vbase and vcall
8972 offsets in BINFO, which is in the hierarchy dominated by T. */
8974 static void
8975 build_vcall_and_vbase_vtbl_entries (tree binfo, vtbl_init_data* vid)
8977 tree b;
8979 /* If this is a derived class, we must first create entries
8980 corresponding to the primary base class. */
8981 b = get_primary_binfo (binfo);
8982 if (b)
8983 build_vcall_and_vbase_vtbl_entries (b, vid);
8985 /* Add the vbase entries for this base. */
8986 build_vbase_offset_vtbl_entries (binfo, vid);
8987 /* Add the vcall entries for this base. */
8988 build_vcall_offset_vtbl_entries (binfo, vid);
8991 /* Returns the initializers for the vbase offset entries in the vtable
8992 for BINFO (which is part of the class hierarchy dominated by T), in
8993 reverse order. VBASE_OFFSET_INDEX gives the vtable index
8994 where the next vbase offset will go. */
8996 static void
8997 build_vbase_offset_vtbl_entries (tree binfo, vtbl_init_data* vid)
8999 tree vbase;
9000 tree t;
9001 tree non_primary_binfo;
9003 /* If there are no virtual baseclasses, then there is nothing to
9004 do. */
9005 if (!CLASSTYPE_VBASECLASSES (BINFO_TYPE (binfo)))
9006 return;
9008 t = vid->derived;
9010 /* We might be a primary base class. Go up the inheritance hierarchy
9011 until we find the most derived class of which we are a primary base:
9012 it is the offset of that which we need to use. */
9013 non_primary_binfo = binfo;
9014 while (BINFO_INHERITANCE_CHAIN (non_primary_binfo))
9016 tree b;
9018 /* If we have reached a virtual base, then it must be a primary
9019 base (possibly multi-level) of vid->binfo, or we wouldn't
9020 have called build_vcall_and_vbase_vtbl_entries for it. But it
9021 might be a lost primary, so just skip down to vid->binfo. */
9022 if (BINFO_VIRTUAL_P (non_primary_binfo))
9024 non_primary_binfo = vid->binfo;
9025 break;
9028 b = BINFO_INHERITANCE_CHAIN (non_primary_binfo);
9029 if (get_primary_binfo (b) != non_primary_binfo)
9030 break;
9031 non_primary_binfo = b;
9034 /* Go through the virtual bases, adding the offsets. */
9035 for (vbase = TYPE_BINFO (BINFO_TYPE (binfo));
9036 vbase;
9037 vbase = TREE_CHAIN (vbase))
9039 tree b;
9040 tree delta;
9042 if (!BINFO_VIRTUAL_P (vbase))
9043 continue;
9045 /* Find the instance of this virtual base in the complete
9046 object. */
9047 b = copied_binfo (vbase, binfo);
9049 /* If we've already got an offset for this virtual base, we
9050 don't need another one. */
9051 if (BINFO_VTABLE_PATH_MARKED (b))
9052 continue;
9053 BINFO_VTABLE_PATH_MARKED (b) = 1;
9055 /* Figure out where we can find this vbase offset. */
9056 delta = size_binop (MULT_EXPR,
9057 vid->index,
9058 convert (ssizetype,
9059 TYPE_SIZE_UNIT (vtable_entry_type)));
9060 if (vid->primary_vtbl_p)
9061 BINFO_VPTR_FIELD (b) = delta;
9063 if (binfo != TYPE_BINFO (t))
9064 /* The vbase offset had better be the same. */
9065 gcc_assert (tree_int_cst_equal (delta, BINFO_VPTR_FIELD (vbase)));
9067 /* The next vbase will come at a more negative offset. */
9068 vid->index = size_binop (MINUS_EXPR, vid->index,
9069 ssize_int (TARGET_VTABLE_DATA_ENTRY_DISTANCE));
9071 /* The initializer is the delta from BINFO to this virtual base.
9072 The vbase offsets go in reverse inheritance-graph order, and
9073 we are walking in inheritance graph order so these end up in
9074 the right order. */
9075 delta = size_diffop_loc (input_location,
9076 BINFO_OFFSET (b), BINFO_OFFSET (non_primary_binfo));
9078 CONSTRUCTOR_APPEND_ELT (vid->inits, NULL_TREE,
9079 fold_build1_loc (input_location, NOP_EXPR,
9080 vtable_entry_type, delta));
9084 /* Adds the initializers for the vcall offset entries in the vtable
9085 for BINFO (which is part of the class hierarchy dominated by VID->DERIVED)
9086 to VID->INITS. */
9088 static void
9089 build_vcall_offset_vtbl_entries (tree binfo, vtbl_init_data* vid)
9091 /* We only need these entries if this base is a virtual base. We
9092 compute the indices -- but do not add to the vtable -- when
9093 building the main vtable for a class. */
9094 if (binfo == TYPE_BINFO (vid->derived)
9095 || (BINFO_VIRTUAL_P (binfo)
9096 /* If BINFO is RTTI_BINFO, then (since BINFO does not
9097 correspond to VID->DERIVED), we are building a primary
9098 construction virtual table. Since this is a primary
9099 virtual table, we do not need the vcall offsets for
9100 BINFO. */
9101 && binfo != vid->rtti_binfo))
9103 /* We need a vcall offset for each of the virtual functions in this
9104 vtable. For example:
9106 class A { virtual void f (); };
9107 class B1 : virtual public A { virtual void f (); };
9108 class B2 : virtual public A { virtual void f (); };
9109 class C: public B1, public B2 { virtual void f (); };
9111 A C object has a primary base of B1, which has a primary base of A. A
9112 C also has a secondary base of B2, which no longer has a primary base
9113 of A. So the B2-in-C construction vtable needs a secondary vtable for
9114 A, which will adjust the A* to a B2* to call f. We have no way of
9115 knowing what (or even whether) this offset will be when we define B2,
9116 so we store this "vcall offset" in the A sub-vtable and look it up in
9117 a "virtual thunk" for B2::f.
9119 We need entries for all the functions in our primary vtable and
9120 in our non-virtual bases' secondary vtables. */
9121 vid->vbase = binfo;
9122 /* If we are just computing the vcall indices -- but do not need
9123 the actual entries -- not that. */
9124 if (!BINFO_VIRTUAL_P (binfo))
9125 vid->generate_vcall_entries = false;
9126 /* Now, walk through the non-virtual bases, adding vcall offsets. */
9127 add_vcall_offset_vtbl_entries_r (binfo, vid);
9131 /* Build vcall offsets, starting with those for BINFO. */
9133 static void
9134 add_vcall_offset_vtbl_entries_r (tree binfo, vtbl_init_data* vid)
9136 int i;
9137 tree primary_binfo;
9138 tree base_binfo;
9140 /* Don't walk into virtual bases -- except, of course, for the
9141 virtual base for which we are building vcall offsets. Any
9142 primary virtual base will have already had its offsets generated
9143 through the recursion in build_vcall_and_vbase_vtbl_entries. */
9144 if (BINFO_VIRTUAL_P (binfo) && vid->vbase != binfo)
9145 return;
9147 /* If BINFO has a primary base, process it first. */
9148 primary_binfo = get_primary_binfo (binfo);
9149 if (primary_binfo)
9150 add_vcall_offset_vtbl_entries_r (primary_binfo, vid);
9152 /* Add BINFO itself to the list. */
9153 add_vcall_offset_vtbl_entries_1 (binfo, vid);
9155 /* Scan the non-primary bases of BINFO. */
9156 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
9157 if (base_binfo != primary_binfo)
9158 add_vcall_offset_vtbl_entries_r (base_binfo, vid);
9161 /* Called from build_vcall_offset_vtbl_entries_r. */
9163 static void
9164 add_vcall_offset_vtbl_entries_1 (tree binfo, vtbl_init_data* vid)
9166 /* Make entries for the rest of the virtuals. */
9167 tree orig_fn;
9169 /* The ABI requires that the methods be processed in declaration
9170 order. */
9171 for (orig_fn = TYPE_METHODS (BINFO_TYPE (binfo));
9172 orig_fn;
9173 orig_fn = DECL_CHAIN (orig_fn))
9174 if (TREE_CODE (orig_fn) == FUNCTION_DECL && DECL_VINDEX (orig_fn))
9175 add_vcall_offset (orig_fn, binfo, vid);
9178 /* Add a vcall offset entry for ORIG_FN to the vtable. */
9180 static void
9181 add_vcall_offset (tree orig_fn, tree binfo, vtbl_init_data *vid)
9183 size_t i;
9184 tree vcall_offset;
9185 tree derived_entry;
9187 /* If there is already an entry for a function with the same
9188 signature as FN, then we do not need a second vcall offset.
9189 Check the list of functions already present in the derived
9190 class vtable. */
9191 FOR_EACH_VEC_SAFE_ELT (vid->fns, i, derived_entry)
9193 if (same_signature_p (derived_entry, orig_fn)
9194 /* We only use one vcall offset for virtual destructors,
9195 even though there are two virtual table entries. */
9196 || (DECL_DESTRUCTOR_P (derived_entry)
9197 && DECL_DESTRUCTOR_P (orig_fn)))
9198 return;
9201 /* If we are building these vcall offsets as part of building
9202 the vtable for the most derived class, remember the vcall
9203 offset. */
9204 if (vid->binfo == TYPE_BINFO (vid->derived))
9206 tree_pair_s elt = {orig_fn, vid->index};
9207 vec_safe_push (CLASSTYPE_VCALL_INDICES (vid->derived), elt);
9210 /* The next vcall offset will be found at a more negative
9211 offset. */
9212 vid->index = size_binop (MINUS_EXPR, vid->index,
9213 ssize_int (TARGET_VTABLE_DATA_ENTRY_DISTANCE));
9215 /* Keep track of this function. */
9216 vec_safe_push (vid->fns, orig_fn);
9218 if (vid->generate_vcall_entries)
9220 tree base;
9221 tree fn;
9223 /* Find the overriding function. */
9224 fn = find_final_overrider (vid->rtti_binfo, binfo, orig_fn);
9225 if (fn == error_mark_node)
9226 vcall_offset = build_zero_cst (vtable_entry_type);
9227 else
9229 base = TREE_VALUE (fn);
9231 /* The vbase we're working on is a primary base of
9232 vid->binfo. But it might be a lost primary, so its
9233 BINFO_OFFSET might be wrong, so we just use the
9234 BINFO_OFFSET from vid->binfo. */
9235 vcall_offset = size_diffop_loc (input_location,
9236 BINFO_OFFSET (base),
9237 BINFO_OFFSET (vid->binfo));
9238 vcall_offset = fold_build1_loc (input_location,
9239 NOP_EXPR, vtable_entry_type,
9240 vcall_offset);
9242 /* Add the initializer to the vtable. */
9243 CONSTRUCTOR_APPEND_ELT (vid->inits, NULL_TREE, vcall_offset);
9247 /* Return vtbl initializers for the RTTI entries corresponding to the
9248 BINFO's vtable. The RTTI entries should indicate the object given
9249 by VID->rtti_binfo. */
9251 static void
9252 build_rtti_vtbl_entries (tree binfo, vtbl_init_data* vid)
9254 tree b;
9255 tree t;
9256 tree offset;
9257 tree decl;
9258 tree init;
9260 t = BINFO_TYPE (vid->rtti_binfo);
9262 /* To find the complete object, we will first convert to our most
9263 primary base, and then add the offset in the vtbl to that value. */
9264 b = binfo;
9265 while (CLASSTYPE_HAS_PRIMARY_BASE_P (BINFO_TYPE (b))
9266 && !BINFO_LOST_PRIMARY_P (b))
9268 tree primary_base;
9270 primary_base = get_primary_binfo (b);
9271 gcc_assert (BINFO_PRIMARY_P (primary_base)
9272 && BINFO_INHERITANCE_CHAIN (primary_base) == b);
9273 b = primary_base;
9275 offset = size_diffop_loc (input_location,
9276 BINFO_OFFSET (vid->rtti_binfo), BINFO_OFFSET (b));
9278 /* The second entry is the address of the typeinfo object. */
9279 if (flag_rtti)
9280 decl = build_address (get_tinfo_decl (t));
9281 else
9282 decl = integer_zero_node;
9284 /* Convert the declaration to a type that can be stored in the
9285 vtable. */
9286 init = build_nop (vfunc_ptr_type_node, decl);
9287 CONSTRUCTOR_APPEND_ELT (vid->inits, NULL_TREE, init);
9289 /* Add the offset-to-top entry. It comes earlier in the vtable than
9290 the typeinfo entry. Convert the offset to look like a
9291 function pointer, so that we can put it in the vtable. */
9292 init = build_nop (vfunc_ptr_type_node, offset);
9293 CONSTRUCTOR_APPEND_ELT (vid->inits, NULL_TREE, init);
9296 /* TRUE iff TYPE is uniquely derived from PARENT. Ignores
9297 accessibility. */
9299 bool
9300 uniquely_derived_from_p (tree parent, tree type)
9302 tree base = lookup_base (type, parent, ba_unique, NULL, tf_none);
9303 return base && base != error_mark_node;
9306 /* TRUE iff TYPE is publicly & uniquely derived from PARENT. */
9308 bool
9309 publicly_uniquely_derived_p (tree parent, tree type)
9311 tree base = lookup_base (type, parent, ba_ignore_scope | ba_check,
9312 NULL, tf_none);
9313 return base && base != error_mark_node;
9316 /* CTX1 and CTX2 are declaration contexts. Return the innermost common
9317 class between them, if any. */
9319 tree
9320 common_enclosing_class (tree ctx1, tree ctx2)
9322 if (!TYPE_P (ctx1) || !TYPE_P (ctx2))
9323 return NULL_TREE;
9324 gcc_assert (ctx1 == TYPE_MAIN_VARIANT (ctx1)
9325 && ctx2 == TYPE_MAIN_VARIANT (ctx2));
9326 if (ctx1 == ctx2)
9327 return ctx1;
9328 for (tree t = ctx1; TYPE_P (t); t = TYPE_CONTEXT (t))
9329 TYPE_MARKED_P (t) = true;
9330 tree found = NULL_TREE;
9331 for (tree t = ctx2; TYPE_P (t); t = TYPE_CONTEXT (t))
9332 if (TYPE_MARKED_P (t))
9334 found = t;
9335 break;
9337 for (tree t = ctx1; TYPE_P (t); t = TYPE_CONTEXT (t))
9338 TYPE_MARKED_P (t) = false;
9339 return found;
9342 #include "gt-cp-class.h"