PR c++/55261
[official-gcc.git] / gcc / cp / class.c
blob04f9df5a23a5af179199d2a6136cdb9f2031b6c2
1 /* Functions related to building classes and their related objects.
2 Copyright (C) 1987, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2007, 2008, 2009, 2010, 2011,
4 2012
5 Free Software Foundation, Inc.
6 Contributed by Michael Tiemann (tiemann@cygnus.com)
8 This file is part of GCC.
10 GCC is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 3, or (at your option)
13 any later version.
15 GCC is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with GCC; see the file COPYING3. If not see
22 <http://www.gnu.org/licenses/>. */
25 /* High-level class interface. */
27 #include "config.h"
28 #include "system.h"
29 #include "coretypes.h"
30 #include "tm.h"
31 #include "tree.h"
32 #include "cp-tree.h"
33 #include "flags.h"
34 #include "toplev.h"
35 #include "target.h"
36 #include "convert.h"
37 #include "cgraph.h"
38 #include "dumpfile.h"
39 #include "splay-tree.h"
40 #include "pointer-set.h"
41 #include "hash-table.h"
43 /* The number of nested classes being processed. If we are not in the
44 scope of any class, this is zero. */
46 int current_class_depth;
48 /* In order to deal with nested classes, we keep a stack of classes.
49 The topmost entry is the innermost class, and is the entry at index
50 CURRENT_CLASS_DEPTH */
52 typedef struct class_stack_node {
53 /* The name of the class. */
54 tree name;
56 /* The _TYPE node for the class. */
57 tree type;
59 /* The access specifier pending for new declarations in the scope of
60 this class. */
61 tree access;
63 /* If were defining TYPE, the names used in this class. */
64 splay_tree names_used;
66 /* Nonzero if this class is no longer open, because of a call to
67 push_to_top_level. */
68 size_t hidden;
69 }* class_stack_node_t;
71 typedef struct vtbl_init_data_s
73 /* The base for which we're building initializers. */
74 tree binfo;
75 /* The type of the most-derived type. */
76 tree derived;
77 /* The binfo for the dynamic type. This will be TYPE_BINFO (derived),
78 unless ctor_vtbl_p is true. */
79 tree rtti_binfo;
80 /* The negative-index vtable initializers built up so far. These
81 are in order from least negative index to most negative index. */
82 vec<constructor_elt, va_gc> *inits;
83 /* The binfo for the virtual base for which we're building
84 vcall offset initializers. */
85 tree vbase;
86 /* The functions in vbase for which we have already provided vcall
87 offsets. */
88 vec<tree, va_gc> *fns;
89 /* The vtable index of the next vcall or vbase offset. */
90 tree index;
91 /* Nonzero if we are building the initializer for the primary
92 vtable. */
93 int primary_vtbl_p;
94 /* Nonzero if we are building the initializer for a construction
95 vtable. */
96 int ctor_vtbl_p;
97 /* True when adding vcall offset entries to the vtable. False when
98 merely computing the indices. */
99 bool generate_vcall_entries;
100 } vtbl_init_data;
102 /* The type of a function passed to walk_subobject_offsets. */
103 typedef int (*subobject_offset_fn) (tree, tree, splay_tree);
105 /* The stack itself. This is a dynamically resized array. The
106 number of elements allocated is CURRENT_CLASS_STACK_SIZE. */
107 static int current_class_stack_size;
108 static class_stack_node_t current_class_stack;
110 /* The size of the largest empty class seen in this translation unit. */
111 static GTY (()) tree sizeof_biggest_empty_class;
113 /* An array of all local classes present in this translation unit, in
114 declaration order. */
115 vec<tree, va_gc> *local_classes;
117 static tree get_vfield_name (tree);
118 static void finish_struct_anon (tree);
119 static tree get_vtable_name (tree);
120 static tree get_basefndecls (tree, tree);
121 static int build_primary_vtable (tree, tree);
122 static int build_secondary_vtable (tree);
123 static void finish_vtbls (tree);
124 static void modify_vtable_entry (tree, tree, tree, tree, tree *);
125 static void finish_struct_bits (tree);
126 static int alter_access (tree, tree, tree);
127 static void handle_using_decl (tree, tree);
128 static tree dfs_modify_vtables (tree, void *);
129 static tree modify_all_vtables (tree, tree);
130 static void determine_primary_bases (tree);
131 static void finish_struct_methods (tree);
132 static void maybe_warn_about_overly_private_class (tree);
133 static int method_name_cmp (const void *, const void *);
134 static int resort_method_name_cmp (const void *, const void *);
135 static void add_implicitly_declared_members (tree, tree*, int, int);
136 static tree fixed_type_or_null (tree, int *, int *);
137 static tree build_simple_base_path (tree expr, tree binfo);
138 static tree build_vtbl_ref_1 (tree, tree);
139 static void build_vtbl_initializer (tree, tree, tree, tree, int *,
140 vec<constructor_elt, va_gc> **);
141 static int count_fields (tree);
142 static int add_fields_to_record_type (tree, struct sorted_fields_type*, int);
143 static void insert_into_classtype_sorted_fields (tree, tree, int);
144 static bool check_bitfield_decl (tree);
145 static void check_field_decl (tree, tree, int *, int *, int *);
146 static void check_field_decls (tree, tree *, int *, int *);
147 static tree *build_base_field (record_layout_info, tree, splay_tree, tree *);
148 static void build_base_fields (record_layout_info, splay_tree, tree *);
149 static void check_methods (tree);
150 static void remove_zero_width_bit_fields (tree);
151 static void check_bases (tree, int *, int *);
152 static void check_bases_and_members (tree);
153 static tree create_vtable_ptr (tree, tree *);
154 static void include_empty_classes (record_layout_info);
155 static void layout_class_type (tree, tree *);
156 static void propagate_binfo_offsets (tree, tree);
157 static void layout_virtual_bases (record_layout_info, splay_tree);
158 static void build_vbase_offset_vtbl_entries (tree, vtbl_init_data *);
159 static void add_vcall_offset_vtbl_entries_r (tree, vtbl_init_data *);
160 static void add_vcall_offset_vtbl_entries_1 (tree, vtbl_init_data *);
161 static void build_vcall_offset_vtbl_entries (tree, vtbl_init_data *);
162 static void add_vcall_offset (tree, tree, vtbl_init_data *);
163 static void layout_vtable_decl (tree, int);
164 static tree dfs_find_final_overrider_pre (tree, void *);
165 static tree dfs_find_final_overrider_post (tree, void *);
166 static tree find_final_overrider (tree, tree, tree);
167 static int make_new_vtable (tree, tree);
168 static tree get_primary_binfo (tree);
169 static int maybe_indent_hierarchy (FILE *, int, int);
170 static tree dump_class_hierarchy_r (FILE *, int, tree, tree, int);
171 static void dump_class_hierarchy (tree);
172 static void dump_class_hierarchy_1 (FILE *, int, tree);
173 static void dump_array (FILE *, tree);
174 static void dump_vtable (tree, tree, tree);
175 static void dump_vtt (tree, tree);
176 static void dump_thunk (FILE *, int, tree);
177 static tree build_vtable (tree, tree, tree);
178 static void initialize_vtable (tree, vec<constructor_elt, va_gc> *);
179 static void layout_nonempty_base_or_field (record_layout_info,
180 tree, tree, splay_tree);
181 static tree end_of_class (tree, int);
182 static bool layout_empty_base (record_layout_info, tree, tree, splay_tree);
183 static void accumulate_vtbl_inits (tree, tree, tree, tree, tree,
184 vec<constructor_elt, va_gc> **);
185 static void dfs_accumulate_vtbl_inits (tree, tree, tree, tree, tree,
186 vec<constructor_elt, va_gc> **);
187 static void build_rtti_vtbl_entries (tree, vtbl_init_data *);
188 static void build_vcall_and_vbase_vtbl_entries (tree, vtbl_init_data *);
189 static void clone_constructors_and_destructors (tree);
190 static tree build_clone (tree, tree);
191 static void update_vtable_entry_for_fn (tree, tree, tree, tree *, unsigned);
192 static void build_ctor_vtbl_group (tree, tree);
193 static void build_vtt (tree);
194 static tree binfo_ctor_vtable (tree);
195 static void build_vtt_inits (tree, tree, vec<constructor_elt, va_gc> **,
196 tree *);
197 static tree dfs_build_secondary_vptr_vtt_inits (tree, void *);
198 static tree dfs_fixup_binfo_vtbls (tree, void *);
199 static int record_subobject_offset (tree, tree, splay_tree);
200 static int check_subobject_offset (tree, tree, splay_tree);
201 static int walk_subobject_offsets (tree, subobject_offset_fn,
202 tree, splay_tree, tree, int);
203 static void record_subobject_offsets (tree, tree, splay_tree, bool);
204 static int layout_conflict_p (tree, tree, splay_tree, int);
205 static int splay_tree_compare_integer_csts (splay_tree_key k1,
206 splay_tree_key k2);
207 static void warn_about_ambiguous_bases (tree);
208 static bool type_requires_array_cookie (tree);
209 static bool contains_empty_class_p (tree);
210 static bool base_derived_from (tree, tree);
211 static int empty_base_at_nonzero_offset_p (tree, tree, splay_tree);
212 static tree end_of_base (tree);
213 static tree get_vcall_index (tree, tree);
215 /* Variables shared between class.c and call.c. */
217 int n_vtables = 0;
218 int n_vtable_entries = 0;
219 int n_vtable_searches = 0;
220 int n_vtable_elems = 0;
221 int n_convert_harshness = 0;
222 int n_compute_conversion_costs = 0;
223 int n_inner_fields_searched = 0;
225 /* Convert to or from a base subobject. EXPR is an expression of type
226 `A' or `A*', an expression of type `B' or `B*' is returned. To
227 convert A to a base B, CODE is PLUS_EXPR and BINFO is the binfo for
228 the B base instance within A. To convert base A to derived B, CODE
229 is MINUS_EXPR and BINFO is the binfo for the A instance within B.
230 In this latter case, A must not be a morally virtual base of B.
231 NONNULL is true if EXPR is known to be non-NULL (this is only
232 needed when EXPR is of pointer type). CV qualifiers are preserved
233 from EXPR. */
235 tree
236 build_base_path (enum tree_code code,
237 tree expr,
238 tree binfo,
239 int nonnull,
240 tsubst_flags_t complain)
242 tree v_binfo = NULL_TREE;
243 tree d_binfo = NULL_TREE;
244 tree probe;
245 tree offset;
246 tree target_type;
247 tree null_test = NULL;
248 tree ptr_target_type;
249 int fixed_type_p;
250 int want_pointer = TREE_CODE (TREE_TYPE (expr)) == POINTER_TYPE;
251 bool has_empty = false;
252 bool virtual_access;
254 if (expr == error_mark_node || binfo == error_mark_node || !binfo)
255 return error_mark_node;
257 for (probe = binfo; probe; probe = BINFO_INHERITANCE_CHAIN (probe))
259 d_binfo = probe;
260 if (is_empty_class (BINFO_TYPE (probe)))
261 has_empty = true;
262 if (!v_binfo && BINFO_VIRTUAL_P (probe))
263 v_binfo = probe;
266 probe = TYPE_MAIN_VARIANT (TREE_TYPE (expr));
267 if (want_pointer)
268 probe = TYPE_MAIN_VARIANT (TREE_TYPE (probe));
270 if (code == PLUS_EXPR
271 && !SAME_BINFO_TYPE_P (BINFO_TYPE (d_binfo), probe))
273 /* This can happen when adjust_result_of_qualified_name_lookup can't
274 find a unique base binfo in a call to a member function. We
275 couldn't give the diagnostic then since we might have been calling
276 a static member function, so we do it now. */
277 if (complain & tf_error)
279 tree base = lookup_base (probe, BINFO_TYPE (d_binfo),
280 ba_unique, NULL, complain);
281 gcc_assert (base == error_mark_node);
283 return error_mark_node;
286 gcc_assert ((code == MINUS_EXPR
287 && SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), probe))
288 || code == PLUS_EXPR);
290 if (binfo == d_binfo)
291 /* Nothing to do. */
292 return expr;
294 if (code == MINUS_EXPR && v_binfo)
296 if (complain & tf_error)
297 error ("cannot convert from base %qT to derived type %qT via "
298 "virtual base %qT", BINFO_TYPE (binfo), BINFO_TYPE (d_binfo),
299 BINFO_TYPE (v_binfo));
300 return error_mark_node;
303 if (!want_pointer)
304 /* This must happen before the call to save_expr. */
305 expr = cp_build_addr_expr (expr, complain);
306 else
307 expr = mark_rvalue_use (expr);
309 offset = BINFO_OFFSET (binfo);
310 fixed_type_p = resolves_to_fixed_type_p (expr, &nonnull);
311 target_type = code == PLUS_EXPR ? BINFO_TYPE (binfo) : BINFO_TYPE (d_binfo);
312 /* TARGET_TYPE has been extracted from BINFO, and, is therefore always
313 cv-unqualified. Extract the cv-qualifiers from EXPR so that the
314 expression returned matches the input. */
315 target_type = cp_build_qualified_type
316 (target_type, cp_type_quals (TREE_TYPE (TREE_TYPE (expr))));
317 ptr_target_type = build_pointer_type (target_type);
319 /* Do we need to look in the vtable for the real offset? */
320 virtual_access = (v_binfo && fixed_type_p <= 0);
322 /* Don't bother with the calculations inside sizeof; they'll ICE if the
323 source type is incomplete and the pointer value doesn't matter. In a
324 template (even in fold_non_dependent_expr), we don't have vtables set
325 up properly yet, and the value doesn't matter there either; we're just
326 interested in the result of overload resolution. */
327 if (cp_unevaluated_operand != 0
328 || in_template_function ())
330 expr = build_nop (ptr_target_type, expr);
331 if (!want_pointer)
332 expr = build_indirect_ref (EXPR_LOCATION (expr), expr, RO_NULL);
333 return expr;
336 /* If we're in an NSDMI, we don't have the full constructor context yet
337 that we need for converting to a virtual base, so just build a stub
338 CONVERT_EXPR and expand it later in bot_replace. */
339 if (virtual_access && fixed_type_p < 0
340 && current_scope () != current_function_decl)
342 expr = build1 (CONVERT_EXPR, ptr_target_type, expr);
343 CONVERT_EXPR_VBASE_PATH (expr) = true;
344 if (!want_pointer)
345 expr = build_indirect_ref (EXPR_LOCATION (expr), expr, RO_NULL);
346 return expr;
349 /* Do we need to check for a null pointer? */
350 if (want_pointer && !nonnull)
352 /* If we know the conversion will not actually change the value
353 of EXPR, then we can avoid testing the expression for NULL.
354 We have to avoid generating a COMPONENT_REF for a base class
355 field, because other parts of the compiler know that such
356 expressions are always non-NULL. */
357 if (!virtual_access && integer_zerop (offset))
358 return build_nop (ptr_target_type, expr);
359 null_test = error_mark_node;
362 /* Protect against multiple evaluation if necessary. */
363 if (TREE_SIDE_EFFECTS (expr) && (null_test || virtual_access))
364 expr = save_expr (expr);
366 /* Now that we've saved expr, build the real null test. */
367 if (null_test)
369 tree zero = cp_convert (TREE_TYPE (expr), nullptr_node, complain);
370 null_test = fold_build2_loc (input_location, NE_EXPR, boolean_type_node,
371 expr, zero);
374 /* If this is a simple base reference, express it as a COMPONENT_REF. */
375 if (code == PLUS_EXPR && !virtual_access
376 /* We don't build base fields for empty bases, and they aren't very
377 interesting to the optimizers anyway. */
378 && !has_empty)
380 expr = cp_build_indirect_ref (expr, RO_NULL, complain);
381 expr = build_simple_base_path (expr, binfo);
382 if (want_pointer)
383 expr = build_address (expr);
384 target_type = TREE_TYPE (expr);
385 goto out;
388 if (virtual_access)
390 /* Going via virtual base V_BINFO. We need the static offset
391 from V_BINFO to BINFO, and the dynamic offset from D_BINFO to
392 V_BINFO. That offset is an entry in D_BINFO's vtable. */
393 tree v_offset;
395 if (fixed_type_p < 0 && in_base_initializer)
397 /* In a base member initializer, we cannot rely on the
398 vtable being set up. We have to indirect via the
399 vtt_parm. */
400 tree t;
402 t = TREE_TYPE (TYPE_VFIELD (current_class_type));
403 t = build_pointer_type (t);
404 v_offset = convert (t, current_vtt_parm);
405 v_offset = cp_build_indirect_ref (v_offset, RO_NULL, complain);
407 else
408 v_offset = build_vfield_ref (cp_build_indirect_ref (expr, RO_NULL,
409 complain),
410 TREE_TYPE (TREE_TYPE (expr)));
412 v_offset = fold_build_pointer_plus (v_offset, BINFO_VPTR_FIELD (v_binfo));
413 v_offset = build1 (NOP_EXPR,
414 build_pointer_type (ptrdiff_type_node),
415 v_offset);
416 v_offset = cp_build_indirect_ref (v_offset, RO_NULL, complain);
417 TREE_CONSTANT (v_offset) = 1;
419 offset = convert_to_integer (ptrdiff_type_node,
420 size_diffop_loc (input_location, offset,
421 BINFO_OFFSET (v_binfo)));
423 if (!integer_zerop (offset))
424 v_offset = build2 (code, ptrdiff_type_node, v_offset, offset);
426 if (fixed_type_p < 0)
427 /* Negative fixed_type_p means this is a constructor or destructor;
428 virtual base layout is fixed in in-charge [cd]tors, but not in
429 base [cd]tors. */
430 offset = build3 (COND_EXPR, ptrdiff_type_node,
431 build2 (EQ_EXPR, boolean_type_node,
432 current_in_charge_parm, integer_zero_node),
433 v_offset,
434 convert_to_integer (ptrdiff_type_node,
435 BINFO_OFFSET (binfo)));
436 else
437 offset = v_offset;
440 if (want_pointer)
441 target_type = ptr_target_type;
443 expr = build1 (NOP_EXPR, ptr_target_type, expr);
445 if (!integer_zerop (offset))
447 offset = fold_convert (sizetype, offset);
448 if (code == MINUS_EXPR)
449 offset = fold_build1_loc (input_location, NEGATE_EXPR, sizetype, offset);
450 expr = fold_build_pointer_plus (expr, offset);
452 else
453 null_test = NULL;
455 if (!want_pointer)
456 expr = cp_build_indirect_ref (expr, RO_NULL, complain);
458 out:
459 if (null_test)
460 expr = fold_build3_loc (input_location, COND_EXPR, target_type, null_test, expr,
461 build_zero_cst (target_type));
463 return expr;
466 /* Subroutine of build_base_path; EXPR and BINFO are as in that function.
467 Perform a derived-to-base conversion by recursively building up a
468 sequence of COMPONENT_REFs to the appropriate base fields. */
470 static tree
471 build_simple_base_path (tree expr, tree binfo)
473 tree type = BINFO_TYPE (binfo);
474 tree d_binfo = BINFO_INHERITANCE_CHAIN (binfo);
475 tree field;
477 if (d_binfo == NULL_TREE)
479 tree temp;
481 gcc_assert (TYPE_MAIN_VARIANT (TREE_TYPE (expr)) == type);
483 /* Transform `(a, b).x' into `(*(a, &b)).x', `(a ? b : c).x'
484 into `(*(a ? &b : &c)).x', and so on. A COND_EXPR is only
485 an lvalue in the front end; only _DECLs and _REFs are lvalues
486 in the back end. */
487 temp = unary_complex_lvalue (ADDR_EXPR, expr);
488 if (temp)
489 expr = cp_build_indirect_ref (temp, RO_NULL, tf_warning_or_error);
491 return expr;
494 /* Recurse. */
495 expr = build_simple_base_path (expr, d_binfo);
497 for (field = TYPE_FIELDS (BINFO_TYPE (d_binfo));
498 field; field = DECL_CHAIN (field))
499 /* Is this the base field created by build_base_field? */
500 if (TREE_CODE (field) == FIELD_DECL
501 && DECL_FIELD_IS_BASE (field)
502 && TREE_TYPE (field) == type
503 /* If we're looking for a field in the most-derived class,
504 also check the field offset; we can have two base fields
505 of the same type if one is an indirect virtual base and one
506 is a direct non-virtual base. */
507 && (BINFO_INHERITANCE_CHAIN (d_binfo)
508 || tree_int_cst_equal (byte_position (field),
509 BINFO_OFFSET (binfo))))
511 /* We don't use build_class_member_access_expr here, as that
512 has unnecessary checks, and more importantly results in
513 recursive calls to dfs_walk_once. */
514 int type_quals = cp_type_quals (TREE_TYPE (expr));
516 expr = build3 (COMPONENT_REF,
517 cp_build_qualified_type (type, type_quals),
518 expr, field, NULL_TREE);
519 expr = fold_if_not_in_template (expr);
521 /* Mark the expression const or volatile, as appropriate.
522 Even though we've dealt with the type above, we still have
523 to mark the expression itself. */
524 if (type_quals & TYPE_QUAL_CONST)
525 TREE_READONLY (expr) = 1;
526 if (type_quals & TYPE_QUAL_VOLATILE)
527 TREE_THIS_VOLATILE (expr) = 1;
529 return expr;
532 /* Didn't find the base field?!? */
533 gcc_unreachable ();
536 /* Convert OBJECT to the base TYPE. OBJECT is an expression whose
537 type is a class type or a pointer to a class type. In the former
538 case, TYPE is also a class type; in the latter it is another
539 pointer type. If CHECK_ACCESS is true, an error message is emitted
540 if TYPE is inaccessible. If OBJECT has pointer type, the value is
541 assumed to be non-NULL. */
543 tree
544 convert_to_base (tree object, tree type, bool check_access, bool nonnull,
545 tsubst_flags_t complain)
547 tree binfo;
548 tree object_type;
550 if (TYPE_PTR_P (TREE_TYPE (object)))
552 object_type = TREE_TYPE (TREE_TYPE (object));
553 type = TREE_TYPE (type);
555 else
556 object_type = TREE_TYPE (object);
558 binfo = lookup_base (object_type, type, check_access ? ba_check : ba_unique,
559 NULL, complain);
560 if (!binfo || binfo == error_mark_node)
561 return error_mark_node;
563 return build_base_path (PLUS_EXPR, object, binfo, nonnull, complain);
566 /* EXPR is an expression with unqualified class type. BASE is a base
567 binfo of that class type. Returns EXPR, converted to the BASE
568 type. This function assumes that EXPR is the most derived class;
569 therefore virtual bases can be found at their static offsets. */
571 tree
572 convert_to_base_statically (tree expr, tree base)
574 tree expr_type;
576 expr_type = TREE_TYPE (expr);
577 if (!SAME_BINFO_TYPE_P (BINFO_TYPE (base), expr_type))
579 /* If this is a non-empty base, use a COMPONENT_REF. */
580 if (!is_empty_class (BINFO_TYPE (base)))
581 return build_simple_base_path (expr, base);
583 /* We use fold_build2 and fold_convert below to simplify the trees
584 provided to the optimizers. It is not safe to call these functions
585 when processing a template because they do not handle C++-specific
586 trees. */
587 gcc_assert (!processing_template_decl);
588 expr = cp_build_addr_expr (expr, tf_warning_or_error);
589 if (!integer_zerop (BINFO_OFFSET (base)))
590 expr = fold_build_pointer_plus_loc (input_location,
591 expr, BINFO_OFFSET (base));
592 expr = fold_convert (build_pointer_type (BINFO_TYPE (base)), expr);
593 expr = build_fold_indirect_ref_loc (input_location, expr);
596 return expr;
600 tree
601 build_vfield_ref (tree datum, tree type)
603 tree vfield, vcontext;
605 if (datum == error_mark_node)
606 return error_mark_node;
608 /* First, convert to the requested type. */
609 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (datum), type))
610 datum = convert_to_base (datum, type, /*check_access=*/false,
611 /*nonnull=*/true, tf_warning_or_error);
613 /* Second, the requested type may not be the owner of its own vptr.
614 If not, convert to the base class that owns it. We cannot use
615 convert_to_base here, because VCONTEXT may appear more than once
616 in the inheritance hierarchy of TYPE, and thus direct conversion
617 between the types may be ambiguous. Following the path back up
618 one step at a time via primary bases avoids the problem. */
619 vfield = TYPE_VFIELD (type);
620 vcontext = DECL_CONTEXT (vfield);
621 while (!same_type_ignoring_top_level_qualifiers_p (vcontext, type))
623 datum = build_simple_base_path (datum, CLASSTYPE_PRIMARY_BINFO (type));
624 type = TREE_TYPE (datum);
627 return build3 (COMPONENT_REF, TREE_TYPE (vfield), datum, vfield, NULL_TREE);
630 /* Given an object INSTANCE, return an expression which yields the
631 vtable element corresponding to INDEX. There are many special
632 cases for INSTANCE which we take care of here, mainly to avoid
633 creating extra tree nodes when we don't have to. */
635 static tree
636 build_vtbl_ref_1 (tree instance, tree idx)
638 tree aref;
639 tree vtbl = NULL_TREE;
641 /* Try to figure out what a reference refers to, and
642 access its virtual function table directly. */
644 int cdtorp = 0;
645 tree fixed_type = fixed_type_or_null (instance, NULL, &cdtorp);
647 tree basetype = non_reference (TREE_TYPE (instance));
649 if (fixed_type && !cdtorp)
651 tree binfo = lookup_base (fixed_type, basetype,
652 ba_unique, NULL, tf_none);
653 if (binfo && binfo != error_mark_node)
654 vtbl = unshare_expr (BINFO_VTABLE (binfo));
657 if (!vtbl)
658 vtbl = build_vfield_ref (instance, basetype);
660 aref = build_array_ref (input_location, vtbl, idx);
661 TREE_CONSTANT (aref) |= TREE_CONSTANT (vtbl) && TREE_CONSTANT (idx);
663 return aref;
666 tree
667 build_vtbl_ref (tree instance, tree idx)
669 tree aref = build_vtbl_ref_1 (instance, idx);
671 return aref;
674 /* Given a stable object pointer INSTANCE_PTR, return an expression which
675 yields a function pointer corresponding to vtable element INDEX. */
677 tree
678 build_vfn_ref (tree instance_ptr, tree idx)
680 tree aref;
682 aref = build_vtbl_ref_1 (cp_build_indirect_ref (instance_ptr, RO_NULL,
683 tf_warning_or_error),
684 idx);
686 /* When using function descriptors, the address of the
687 vtable entry is treated as a function pointer. */
688 if (TARGET_VTABLE_USES_DESCRIPTORS)
689 aref = build1 (NOP_EXPR, TREE_TYPE (aref),
690 cp_build_addr_expr (aref, tf_warning_or_error));
692 /* Remember this as a method reference, for later devirtualization. */
693 aref = build3 (OBJ_TYPE_REF, TREE_TYPE (aref), aref, instance_ptr, idx);
695 return aref;
698 /* Return the name of the virtual function table (as an IDENTIFIER_NODE)
699 for the given TYPE. */
701 static tree
702 get_vtable_name (tree type)
704 return mangle_vtbl_for_type (type);
707 /* DECL is an entity associated with TYPE, like a virtual table or an
708 implicitly generated constructor. Determine whether or not DECL
709 should have external or internal linkage at the object file
710 level. This routine does not deal with COMDAT linkage and other
711 similar complexities; it simply sets TREE_PUBLIC if it possible for
712 entities in other translation units to contain copies of DECL, in
713 the abstract. */
715 void
716 set_linkage_according_to_type (tree /*type*/, tree decl)
718 TREE_PUBLIC (decl) = 1;
719 determine_visibility (decl);
722 /* Create a VAR_DECL for a primary or secondary vtable for CLASS_TYPE.
723 (For a secondary vtable for B-in-D, CLASS_TYPE should be D, not B.)
724 Use NAME for the name of the vtable, and VTABLE_TYPE for its type. */
726 static tree
727 build_vtable (tree class_type, tree name, tree vtable_type)
729 tree decl;
731 decl = build_lang_decl (VAR_DECL, name, vtable_type);
732 /* vtable names are already mangled; give them their DECL_ASSEMBLER_NAME
733 now to avoid confusion in mangle_decl. */
734 SET_DECL_ASSEMBLER_NAME (decl, name);
735 DECL_CONTEXT (decl) = class_type;
736 DECL_ARTIFICIAL (decl) = 1;
737 TREE_STATIC (decl) = 1;
738 TREE_READONLY (decl) = 1;
739 DECL_VIRTUAL_P (decl) = 1;
740 DECL_ALIGN (decl) = TARGET_VTABLE_ENTRY_ALIGN;
741 DECL_VTABLE_OR_VTT_P (decl) = 1;
742 /* At one time the vtable info was grabbed 2 words at a time. This
743 fails on sparc unless you have 8-byte alignment. (tiemann) */
744 DECL_ALIGN (decl) = MAX (TYPE_ALIGN (double_type_node),
745 DECL_ALIGN (decl));
746 set_linkage_according_to_type (class_type, decl);
747 /* The vtable has not been defined -- yet. */
748 DECL_EXTERNAL (decl) = 1;
749 DECL_NOT_REALLY_EXTERN (decl) = 1;
751 /* Mark the VAR_DECL node representing the vtable itself as a
752 "gratuitous" one, thereby forcing dwarfout.c to ignore it. It
753 is rather important that such things be ignored because any
754 effort to actually generate DWARF for them will run into
755 trouble when/if we encounter code like:
757 #pragma interface
758 struct S { virtual void member (); };
760 because the artificial declaration of the vtable itself (as
761 manufactured by the g++ front end) will say that the vtable is
762 a static member of `S' but only *after* the debug output for
763 the definition of `S' has already been output. This causes
764 grief because the DWARF entry for the definition of the vtable
765 will try to refer back to an earlier *declaration* of the
766 vtable as a static member of `S' and there won't be one. We
767 might be able to arrange to have the "vtable static member"
768 attached to the member list for `S' before the debug info for
769 `S' get written (which would solve the problem) but that would
770 require more intrusive changes to the g++ front end. */
771 DECL_IGNORED_P (decl) = 1;
773 return decl;
776 /* Get the VAR_DECL of the vtable for TYPE. TYPE need not be polymorphic,
777 or even complete. If this does not exist, create it. If COMPLETE is
778 nonzero, then complete the definition of it -- that will render it
779 impossible to actually build the vtable, but is useful to get at those
780 which are known to exist in the runtime. */
782 tree
783 get_vtable_decl (tree type, int complete)
785 tree decl;
787 if (CLASSTYPE_VTABLES (type))
788 return CLASSTYPE_VTABLES (type);
790 decl = build_vtable (type, get_vtable_name (type), vtbl_type_node);
791 CLASSTYPE_VTABLES (type) = decl;
793 if (complete)
795 DECL_EXTERNAL (decl) = 1;
796 cp_finish_decl (decl, NULL_TREE, false, NULL_TREE, 0);
799 return decl;
802 /* Build the primary virtual function table for TYPE. If BINFO is
803 non-NULL, build the vtable starting with the initial approximation
804 that it is the same as the one which is the head of the association
805 list. Returns a nonzero value if a new vtable is actually
806 created. */
808 static int
809 build_primary_vtable (tree binfo, tree type)
811 tree decl;
812 tree virtuals;
814 decl = get_vtable_decl (type, /*complete=*/0);
816 if (binfo)
818 if (BINFO_NEW_VTABLE_MARKED (binfo))
819 /* We have already created a vtable for this base, so there's
820 no need to do it again. */
821 return 0;
823 virtuals = copy_list (BINFO_VIRTUALS (binfo));
824 TREE_TYPE (decl) = TREE_TYPE (get_vtbl_decl_for_binfo (binfo));
825 DECL_SIZE (decl) = TYPE_SIZE (TREE_TYPE (decl));
826 DECL_SIZE_UNIT (decl) = TYPE_SIZE_UNIT (TREE_TYPE (decl));
828 else
830 gcc_assert (TREE_TYPE (decl) == vtbl_type_node);
831 virtuals = NULL_TREE;
834 if (GATHER_STATISTICS)
836 n_vtables += 1;
837 n_vtable_elems += list_length (virtuals);
840 /* Initialize the association list for this type, based
841 on our first approximation. */
842 BINFO_VTABLE (TYPE_BINFO (type)) = decl;
843 BINFO_VIRTUALS (TYPE_BINFO (type)) = virtuals;
844 SET_BINFO_NEW_VTABLE_MARKED (TYPE_BINFO (type));
845 return 1;
848 /* Give BINFO a new virtual function table which is initialized
849 with a skeleton-copy of its original initialization. The only
850 entry that changes is the `delta' entry, so we can really
851 share a lot of structure.
853 FOR_TYPE is the most derived type which caused this table to
854 be needed.
856 Returns nonzero if we haven't met BINFO before.
858 The order in which vtables are built (by calling this function) for
859 an object must remain the same, otherwise a binary incompatibility
860 can result. */
862 static int
863 build_secondary_vtable (tree binfo)
865 if (BINFO_NEW_VTABLE_MARKED (binfo))
866 /* We already created a vtable for this base. There's no need to
867 do it again. */
868 return 0;
870 /* Remember that we've created a vtable for this BINFO, so that we
871 don't try to do so again. */
872 SET_BINFO_NEW_VTABLE_MARKED (binfo);
874 /* Make fresh virtual list, so we can smash it later. */
875 BINFO_VIRTUALS (binfo) = copy_list (BINFO_VIRTUALS (binfo));
877 /* Secondary vtables are laid out as part of the same structure as
878 the primary vtable. */
879 BINFO_VTABLE (binfo) = NULL_TREE;
880 return 1;
883 /* Create a new vtable for BINFO which is the hierarchy dominated by
884 T. Return nonzero if we actually created a new vtable. */
886 static int
887 make_new_vtable (tree t, tree binfo)
889 if (binfo == TYPE_BINFO (t))
890 /* In this case, it is *type*'s vtable we are modifying. We start
891 with the approximation that its vtable is that of the
892 immediate base class. */
893 return build_primary_vtable (binfo, t);
894 else
895 /* This is our very own copy of `basetype' to play with. Later,
896 we will fill in all the virtual functions that override the
897 virtual functions in these base classes which are not defined
898 by the current type. */
899 return build_secondary_vtable (binfo);
902 /* Make *VIRTUALS, an entry on the BINFO_VIRTUALS list for BINFO
903 (which is in the hierarchy dominated by T) list FNDECL as its
904 BV_FN. DELTA is the required constant adjustment from the `this'
905 pointer where the vtable entry appears to the `this' required when
906 the function is actually called. */
908 static void
909 modify_vtable_entry (tree t,
910 tree binfo,
911 tree fndecl,
912 tree delta,
913 tree *virtuals)
915 tree v;
917 v = *virtuals;
919 if (fndecl != BV_FN (v)
920 || !tree_int_cst_equal (delta, BV_DELTA (v)))
922 /* We need a new vtable for BINFO. */
923 if (make_new_vtable (t, binfo))
925 /* If we really did make a new vtable, we also made a copy
926 of the BINFO_VIRTUALS list. Now, we have to find the
927 corresponding entry in that list. */
928 *virtuals = BINFO_VIRTUALS (binfo);
929 while (BV_FN (*virtuals) != BV_FN (v))
930 *virtuals = TREE_CHAIN (*virtuals);
931 v = *virtuals;
934 BV_DELTA (v) = delta;
935 BV_VCALL_INDEX (v) = NULL_TREE;
936 BV_FN (v) = fndecl;
941 /* Add method METHOD to class TYPE. If USING_DECL is non-null, it is
942 the USING_DECL naming METHOD. Returns true if the method could be
943 added to the method vec. */
945 bool
946 add_method (tree type, tree method, tree using_decl)
948 unsigned slot;
949 tree overload;
950 bool template_conv_p = false;
951 bool conv_p;
952 vec<tree, va_gc> *method_vec;
953 bool complete_p;
954 bool insert_p = false;
955 tree current_fns;
956 tree fns;
958 if (method == error_mark_node)
959 return false;
961 complete_p = COMPLETE_TYPE_P (type);
962 conv_p = DECL_CONV_FN_P (method);
963 if (conv_p)
964 template_conv_p = (TREE_CODE (method) == TEMPLATE_DECL
965 && DECL_TEMPLATE_CONV_FN_P (method));
967 method_vec = CLASSTYPE_METHOD_VEC (type);
968 if (!method_vec)
970 /* Make a new method vector. We start with 8 entries. We must
971 allocate at least two (for constructors and destructors), and
972 we're going to end up with an assignment operator at some
973 point as well. */
974 vec_alloc (method_vec, 8);
975 /* Create slots for constructors and destructors. */
976 method_vec->quick_push (NULL_TREE);
977 method_vec->quick_push (NULL_TREE);
978 CLASSTYPE_METHOD_VEC (type) = method_vec;
981 /* Maintain TYPE_HAS_USER_CONSTRUCTOR, etc. */
982 grok_special_member_properties (method);
984 /* Constructors and destructors go in special slots. */
985 if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (method))
986 slot = CLASSTYPE_CONSTRUCTOR_SLOT;
987 else if (DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (method))
989 slot = CLASSTYPE_DESTRUCTOR_SLOT;
991 if (TYPE_FOR_JAVA (type))
993 if (!DECL_ARTIFICIAL (method))
994 error ("Java class %qT cannot have a destructor", type);
995 else if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
996 error ("Java class %qT cannot have an implicit non-trivial "
997 "destructor",
998 type);
1001 else
1003 tree m;
1005 insert_p = true;
1006 /* See if we already have an entry with this name. */
1007 for (slot = CLASSTYPE_FIRST_CONVERSION_SLOT;
1008 vec_safe_iterate (method_vec, slot, &m);
1009 ++slot)
1011 m = OVL_CURRENT (m);
1012 if (template_conv_p)
1014 if (TREE_CODE (m) == TEMPLATE_DECL
1015 && DECL_TEMPLATE_CONV_FN_P (m))
1016 insert_p = false;
1017 break;
1019 if (conv_p && !DECL_CONV_FN_P (m))
1020 break;
1021 if (DECL_NAME (m) == DECL_NAME (method))
1023 insert_p = false;
1024 break;
1026 if (complete_p
1027 && !DECL_CONV_FN_P (m)
1028 && DECL_NAME (m) > DECL_NAME (method))
1029 break;
1032 current_fns = insert_p ? NULL_TREE : (*method_vec)[slot];
1034 /* Check to see if we've already got this method. */
1035 for (fns = current_fns; fns; fns = OVL_NEXT (fns))
1037 tree fn = OVL_CURRENT (fns);
1038 tree fn_type;
1039 tree method_type;
1040 tree parms1;
1041 tree parms2;
1043 if (TREE_CODE (fn) != TREE_CODE (method))
1044 continue;
1046 /* [over.load] Member function declarations with the
1047 same name and the same parameter types cannot be
1048 overloaded if any of them is a static member
1049 function declaration.
1051 [namespace.udecl] When a using-declaration brings names
1052 from a base class into a derived class scope, member
1053 functions in the derived class override and/or hide member
1054 functions with the same name and parameter types in a base
1055 class (rather than conflicting). */
1056 fn_type = TREE_TYPE (fn);
1057 method_type = TREE_TYPE (method);
1058 parms1 = TYPE_ARG_TYPES (fn_type);
1059 parms2 = TYPE_ARG_TYPES (method_type);
1061 /* Compare the quals on the 'this' parm. Don't compare
1062 the whole types, as used functions are treated as
1063 coming from the using class in overload resolution. */
1064 if (! DECL_STATIC_FUNCTION_P (fn)
1065 && ! DECL_STATIC_FUNCTION_P (method)
1066 && TREE_TYPE (TREE_VALUE (parms1)) != error_mark_node
1067 && TREE_TYPE (TREE_VALUE (parms2)) != error_mark_node
1068 && (cp_type_quals (TREE_TYPE (TREE_VALUE (parms1)))
1069 != cp_type_quals (TREE_TYPE (TREE_VALUE (parms2)))))
1070 continue;
1072 /* For templates, the return type and template parameters
1073 must be identical. */
1074 if (TREE_CODE (fn) == TEMPLATE_DECL
1075 && (!same_type_p (TREE_TYPE (fn_type),
1076 TREE_TYPE (method_type))
1077 || !comp_template_parms (DECL_TEMPLATE_PARMS (fn),
1078 DECL_TEMPLATE_PARMS (method))))
1079 continue;
1081 if (! DECL_STATIC_FUNCTION_P (fn))
1082 parms1 = TREE_CHAIN (parms1);
1083 if (! DECL_STATIC_FUNCTION_P (method))
1084 parms2 = TREE_CHAIN (parms2);
1086 if (compparms (parms1, parms2)
1087 && (!DECL_CONV_FN_P (fn)
1088 || same_type_p (TREE_TYPE (fn_type),
1089 TREE_TYPE (method_type))))
1091 /* For function versions, their parms and types match
1092 but they are not duplicates. Record function versions
1093 as and when they are found. extern "C" functions are
1094 not treated as versions. */
1095 if (TREE_CODE (fn) == FUNCTION_DECL
1096 && TREE_CODE (method) == FUNCTION_DECL
1097 && !DECL_EXTERN_C_P (fn)
1098 && !DECL_EXTERN_C_P (method)
1099 && (DECL_FUNCTION_SPECIFIC_TARGET (fn)
1100 || DECL_FUNCTION_SPECIFIC_TARGET (method))
1101 && targetm.target_option.function_versions (fn, method))
1103 /* Mark functions as versions if necessary. Modify the mangled
1104 decl name if necessary. */
1105 if (!DECL_FUNCTION_VERSIONED (fn))
1107 DECL_FUNCTION_VERSIONED (fn) = 1;
1108 if (DECL_ASSEMBLER_NAME_SET_P (fn))
1109 mangle_decl (fn);
1111 if (!DECL_FUNCTION_VERSIONED (method))
1113 DECL_FUNCTION_VERSIONED (method) = 1;
1114 if (DECL_ASSEMBLER_NAME_SET_P (method))
1115 mangle_decl (method);
1117 record_function_versions (fn, method);
1118 continue;
1120 if (DECL_INHERITED_CTOR_BASE (method))
1122 if (DECL_INHERITED_CTOR_BASE (fn))
1124 error_at (DECL_SOURCE_LOCATION (method),
1125 "%q#D inherited from %qT", method,
1126 DECL_INHERITED_CTOR_BASE (method));
1127 error_at (DECL_SOURCE_LOCATION (fn),
1128 "conflicts with version inherited from %qT",
1129 DECL_INHERITED_CTOR_BASE (fn));
1131 /* Otherwise defer to the other function. */
1132 return false;
1134 if (using_decl)
1136 if (DECL_CONTEXT (fn) == type)
1137 /* Defer to the local function. */
1138 return false;
1140 else
1142 error ("%q+#D cannot be overloaded", method);
1143 error ("with %q+#D", fn);
1146 /* We don't call duplicate_decls here to merge the
1147 declarations because that will confuse things if the
1148 methods have inline definitions. In particular, we
1149 will crash while processing the definitions. */
1150 return false;
1154 /* A class should never have more than one destructor. */
1155 if (current_fns && DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (method))
1156 return false;
1158 /* Add the new binding. */
1159 if (using_decl)
1161 overload = ovl_cons (method, current_fns);
1162 OVL_USED (overload) = true;
1164 else
1165 overload = build_overload (method, current_fns);
1167 if (conv_p)
1168 TYPE_HAS_CONVERSION (type) = 1;
1169 else if (slot >= CLASSTYPE_FIRST_CONVERSION_SLOT && !complete_p)
1170 push_class_level_binding (DECL_NAME (method), overload);
1172 if (insert_p)
1174 bool reallocated;
1176 /* We only expect to add few methods in the COMPLETE_P case, so
1177 just make room for one more method in that case. */
1178 if (complete_p)
1179 reallocated = vec_safe_reserve_exact (method_vec, 1);
1180 else
1181 reallocated = vec_safe_reserve (method_vec, 1);
1182 if (reallocated)
1183 CLASSTYPE_METHOD_VEC (type) = method_vec;
1184 if (slot == method_vec->length ())
1185 method_vec->quick_push (overload);
1186 else
1187 method_vec->quick_insert (slot, overload);
1189 else
1190 /* Replace the current slot. */
1191 (*method_vec)[slot] = overload;
1192 return true;
1195 /* Subroutines of finish_struct. */
1197 /* Change the access of FDECL to ACCESS in T. Return 1 if change was
1198 legit, otherwise return 0. */
1200 static int
1201 alter_access (tree t, tree fdecl, tree access)
1203 tree elem;
1205 if (!DECL_LANG_SPECIFIC (fdecl))
1206 retrofit_lang_decl (fdecl);
1208 gcc_assert (!DECL_DISCRIMINATOR_P (fdecl));
1210 elem = purpose_member (t, DECL_ACCESS (fdecl));
1211 if (elem)
1213 if (TREE_VALUE (elem) != access)
1215 if (TREE_CODE (TREE_TYPE (fdecl)) == FUNCTION_DECL)
1216 error ("conflicting access specifications for method"
1217 " %q+D, ignored", TREE_TYPE (fdecl));
1218 else
1219 error ("conflicting access specifications for field %qE, ignored",
1220 DECL_NAME (fdecl));
1222 else
1224 /* They're changing the access to the same thing they changed
1225 it to before. That's OK. */
1229 else
1231 perform_or_defer_access_check (TYPE_BINFO (t), fdecl, fdecl,
1232 tf_warning_or_error);
1233 DECL_ACCESS (fdecl) = tree_cons (t, access, DECL_ACCESS (fdecl));
1234 return 1;
1236 return 0;
1239 /* Process the USING_DECL, which is a member of T. */
1241 static void
1242 handle_using_decl (tree using_decl, tree t)
1244 tree decl = USING_DECL_DECLS (using_decl);
1245 tree name = DECL_NAME (using_decl);
1246 tree access
1247 = TREE_PRIVATE (using_decl) ? access_private_node
1248 : TREE_PROTECTED (using_decl) ? access_protected_node
1249 : access_public_node;
1250 tree flist = NULL_TREE;
1251 tree old_value;
1253 gcc_assert (!processing_template_decl && decl);
1255 old_value = lookup_member (t, name, /*protect=*/0, /*want_type=*/false,
1256 tf_warning_or_error);
1257 if (old_value)
1259 if (is_overloaded_fn (old_value))
1260 old_value = OVL_CURRENT (old_value);
1262 if (DECL_P (old_value) && DECL_CONTEXT (old_value) == t)
1263 /* OK */;
1264 else
1265 old_value = NULL_TREE;
1268 cp_emit_debug_info_for_using (decl, USING_DECL_SCOPE (using_decl));
1270 if (is_overloaded_fn (decl))
1271 flist = decl;
1273 if (! old_value)
1275 else if (is_overloaded_fn (old_value))
1277 if (flist)
1278 /* It's OK to use functions from a base when there are functions with
1279 the same name already present in the current class. */;
1280 else
1282 error ("%q+D invalid in %q#T", using_decl, t);
1283 error (" because of local method %q+#D with same name",
1284 OVL_CURRENT (old_value));
1285 return;
1288 else if (!DECL_ARTIFICIAL (old_value))
1290 error ("%q+D invalid in %q#T", using_decl, t);
1291 error (" because of local member %q+#D with same name", old_value);
1292 return;
1295 /* Make type T see field decl FDECL with access ACCESS. */
1296 if (flist)
1297 for (; flist; flist = OVL_NEXT (flist))
1299 add_method (t, OVL_CURRENT (flist), using_decl);
1300 alter_access (t, OVL_CURRENT (flist), access);
1302 else
1303 alter_access (t, decl, access);
1306 /* walk_tree callback for check_abi_tags: if the type at *TP involves any
1307 types with abi tags, add the corresponding identifiers to the VEC in
1308 *DATA and set IDENTIFIER_MARKED. */
1310 struct abi_tag_data
1312 tree t;
1313 tree subob;
1316 static tree
1317 find_abi_tags_r (tree *tp, int */*walk_subtrees*/, void *data)
1319 if (!TAGGED_TYPE_P (*tp))
1320 return NULL_TREE;
1322 if (tree attributes = lookup_attribute ("abi_tag", TYPE_ATTRIBUTES (*tp)))
1324 struct abi_tag_data *p = static_cast<struct abi_tag_data*>(data);
1325 for (tree list = TREE_VALUE (attributes); list;
1326 list = TREE_CHAIN (list))
1328 tree tag = TREE_VALUE (list);
1329 tree id = get_identifier (TREE_STRING_POINTER (tag));
1330 if (!IDENTIFIER_MARKED (id))
1332 if (TYPE_P (p->subob))
1334 warning (OPT_Wabi_tag, "%qT does not have the %E abi tag "
1335 "that base %qT has", p->t, tag, p->subob);
1336 inform (location_of (p->subob), "%qT declared here",
1337 p->subob);
1339 else
1341 warning (OPT_Wabi_tag, "%qT does not have the %E abi tag "
1342 "that %qT (used in the type of %qD) has",
1343 p->t, tag, *tp, p->subob);
1344 inform (location_of (p->subob), "%qD declared here",
1345 p->subob);
1346 inform (location_of (*tp), "%qT declared here", *tp);
1351 return NULL_TREE;
1354 /* Check that class T has all the abi tags that subobject SUBOB has, or
1355 warn if not. */
1357 static void
1358 check_abi_tags (tree t, tree subob)
1360 tree attributes = lookup_attribute ("abi_tag", TYPE_ATTRIBUTES (t));
1361 if (attributes)
1363 for (tree list = TREE_VALUE (attributes); list;
1364 list = TREE_CHAIN (list))
1366 tree tag = TREE_VALUE (list);
1367 tree id = get_identifier (TREE_STRING_POINTER (tag));
1368 IDENTIFIER_MARKED (id) = true;
1372 tree subtype = TYPE_P (subob) ? subob : TREE_TYPE (subob);
1373 struct abi_tag_data data = { t, subob };
1375 cp_walk_tree_without_duplicates (&subtype, find_abi_tags_r, &data);
1377 if (attributes)
1379 for (tree list = TREE_VALUE (attributes); list;
1380 list = TREE_CHAIN (list))
1382 tree tag = TREE_VALUE (list);
1383 tree id = get_identifier (TREE_STRING_POINTER (tag));
1384 IDENTIFIER_MARKED (id) = false;
1389 /* Run through the base classes of T, updating CANT_HAVE_CONST_CTOR_P,
1390 and NO_CONST_ASN_REF_P. Also set flag bits in T based on
1391 properties of the bases. */
1393 static void
1394 check_bases (tree t,
1395 int* cant_have_const_ctor_p,
1396 int* no_const_asn_ref_p)
1398 int i;
1399 bool seen_non_virtual_nearly_empty_base_p = 0;
1400 int seen_tm_mask = 0;
1401 tree base_binfo;
1402 tree binfo;
1403 tree field = NULL_TREE;
1405 if (!CLASSTYPE_NON_STD_LAYOUT (t))
1406 for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
1407 if (TREE_CODE (field) == FIELD_DECL)
1408 break;
1410 for (binfo = TYPE_BINFO (t), i = 0;
1411 BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
1413 tree basetype = TREE_TYPE (base_binfo);
1415 gcc_assert (COMPLETE_TYPE_P (basetype));
1417 if (CLASSTYPE_FINAL (basetype))
1418 error ("cannot derive from %<final%> base %qT in derived type %qT",
1419 basetype, t);
1421 /* If any base class is non-literal, so is the derived class. */
1422 if (!CLASSTYPE_LITERAL_P (basetype))
1423 CLASSTYPE_LITERAL_P (t) = false;
1425 /* Effective C++ rule 14. We only need to check TYPE_POLYMORPHIC_P
1426 here because the case of virtual functions but non-virtual
1427 dtor is handled in finish_struct_1. */
1428 if (!TYPE_POLYMORPHIC_P (basetype))
1429 warning (OPT_Weffc__,
1430 "base class %q#T has a non-virtual destructor", basetype);
1432 /* If the base class doesn't have copy constructors or
1433 assignment operators that take const references, then the
1434 derived class cannot have such a member automatically
1435 generated. */
1436 if (TYPE_HAS_COPY_CTOR (basetype)
1437 && ! TYPE_HAS_CONST_COPY_CTOR (basetype))
1438 *cant_have_const_ctor_p = 1;
1439 if (TYPE_HAS_COPY_ASSIGN (basetype)
1440 && !TYPE_HAS_CONST_COPY_ASSIGN (basetype))
1441 *no_const_asn_ref_p = 1;
1443 if (BINFO_VIRTUAL_P (base_binfo))
1444 /* A virtual base does not effect nearly emptiness. */
1446 else if (CLASSTYPE_NEARLY_EMPTY_P (basetype))
1448 if (seen_non_virtual_nearly_empty_base_p)
1449 /* And if there is more than one nearly empty base, then the
1450 derived class is not nearly empty either. */
1451 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
1452 else
1453 /* Remember we've seen one. */
1454 seen_non_virtual_nearly_empty_base_p = 1;
1456 else if (!is_empty_class (basetype))
1457 /* If the base class is not empty or nearly empty, then this
1458 class cannot be nearly empty. */
1459 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
1461 /* A lot of properties from the bases also apply to the derived
1462 class. */
1463 TYPE_NEEDS_CONSTRUCTING (t) |= TYPE_NEEDS_CONSTRUCTING (basetype);
1464 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t)
1465 |= TYPE_HAS_NONTRIVIAL_DESTRUCTOR (basetype);
1466 TYPE_HAS_COMPLEX_COPY_ASSIGN (t)
1467 |= (TYPE_HAS_COMPLEX_COPY_ASSIGN (basetype)
1468 || !TYPE_HAS_COPY_ASSIGN (basetype));
1469 TYPE_HAS_COMPLEX_COPY_CTOR (t) |= (TYPE_HAS_COMPLEX_COPY_CTOR (basetype)
1470 || !TYPE_HAS_COPY_CTOR (basetype));
1471 TYPE_HAS_COMPLEX_MOVE_ASSIGN (t)
1472 |= TYPE_HAS_COMPLEX_MOVE_ASSIGN (basetype);
1473 TYPE_HAS_COMPLEX_MOVE_CTOR (t) |= TYPE_HAS_COMPLEX_MOVE_CTOR (basetype);
1474 TYPE_POLYMORPHIC_P (t) |= TYPE_POLYMORPHIC_P (basetype);
1475 CLASSTYPE_CONTAINS_EMPTY_CLASS_P (t)
1476 |= CLASSTYPE_CONTAINS_EMPTY_CLASS_P (basetype);
1477 TYPE_HAS_COMPLEX_DFLT (t) |= (!TYPE_HAS_DEFAULT_CONSTRUCTOR (basetype)
1478 || TYPE_HAS_COMPLEX_DFLT (basetype));
1480 /* A standard-layout class is a class that:
1482 * has no non-standard-layout base classes, */
1483 CLASSTYPE_NON_STD_LAYOUT (t) |= CLASSTYPE_NON_STD_LAYOUT (basetype);
1484 if (!CLASSTYPE_NON_STD_LAYOUT (t))
1486 tree basefield;
1487 /* ...has no base classes of the same type as the first non-static
1488 data member... */
1489 if (field && DECL_CONTEXT (field) == t
1490 && (same_type_ignoring_top_level_qualifiers_p
1491 (TREE_TYPE (field), basetype)))
1492 CLASSTYPE_NON_STD_LAYOUT (t) = 1;
1493 else
1494 /* ...either has no non-static data members in the most-derived
1495 class and at most one base class with non-static data
1496 members, or has no base classes with non-static data
1497 members */
1498 for (basefield = TYPE_FIELDS (basetype); basefield;
1499 basefield = DECL_CHAIN (basefield))
1500 if (TREE_CODE (basefield) == FIELD_DECL)
1502 if (field)
1503 CLASSTYPE_NON_STD_LAYOUT (t) = 1;
1504 else
1505 field = basefield;
1506 break;
1510 /* Don't bother collecting tm attributes if transactional memory
1511 support is not enabled. */
1512 if (flag_tm)
1514 tree tm_attr = find_tm_attribute (TYPE_ATTRIBUTES (basetype));
1515 if (tm_attr)
1516 seen_tm_mask |= tm_attr_to_mask (tm_attr);
1519 check_abi_tags (t, basetype);
1522 /* If one of the base classes had TM attributes, and the current class
1523 doesn't define its own, then the current class inherits one. */
1524 if (seen_tm_mask && !find_tm_attribute (TYPE_ATTRIBUTES (t)))
1526 tree tm_attr = tm_mask_to_attr (seen_tm_mask & -seen_tm_mask);
1527 TYPE_ATTRIBUTES (t) = tree_cons (tm_attr, NULL, TYPE_ATTRIBUTES (t));
1531 /* Determine all the primary bases within T. Sets BINFO_PRIMARY_BASE_P for
1532 those that are primaries. Sets BINFO_LOST_PRIMARY_P for those
1533 that have had a nearly-empty virtual primary base stolen by some
1534 other base in the hierarchy. Determines CLASSTYPE_PRIMARY_BASE for
1535 T. */
1537 static void
1538 determine_primary_bases (tree t)
1540 unsigned i;
1541 tree primary = NULL_TREE;
1542 tree type_binfo = TYPE_BINFO (t);
1543 tree base_binfo;
1545 /* Determine the primary bases of our bases. */
1546 for (base_binfo = TREE_CHAIN (type_binfo); base_binfo;
1547 base_binfo = TREE_CHAIN (base_binfo))
1549 tree primary = CLASSTYPE_PRIMARY_BINFO (BINFO_TYPE (base_binfo));
1551 /* See if we're the non-virtual primary of our inheritance
1552 chain. */
1553 if (!BINFO_VIRTUAL_P (base_binfo))
1555 tree parent = BINFO_INHERITANCE_CHAIN (base_binfo);
1556 tree parent_primary = CLASSTYPE_PRIMARY_BINFO (BINFO_TYPE (parent));
1558 if (parent_primary
1559 && SAME_BINFO_TYPE_P (BINFO_TYPE (base_binfo),
1560 BINFO_TYPE (parent_primary)))
1561 /* We are the primary binfo. */
1562 BINFO_PRIMARY_P (base_binfo) = 1;
1564 /* Determine if we have a virtual primary base, and mark it so.
1566 if (primary && BINFO_VIRTUAL_P (primary))
1568 tree this_primary = copied_binfo (primary, base_binfo);
1570 if (BINFO_PRIMARY_P (this_primary))
1571 /* Someone already claimed this base. */
1572 BINFO_LOST_PRIMARY_P (base_binfo) = 1;
1573 else
1575 tree delta;
1577 BINFO_PRIMARY_P (this_primary) = 1;
1578 BINFO_INHERITANCE_CHAIN (this_primary) = base_binfo;
1580 /* A virtual binfo might have been copied from within
1581 another hierarchy. As we're about to use it as a
1582 primary base, make sure the offsets match. */
1583 delta = size_diffop_loc (input_location,
1584 convert (ssizetype,
1585 BINFO_OFFSET (base_binfo)),
1586 convert (ssizetype,
1587 BINFO_OFFSET (this_primary)));
1589 propagate_binfo_offsets (this_primary, delta);
1594 /* First look for a dynamic direct non-virtual base. */
1595 for (i = 0; BINFO_BASE_ITERATE (type_binfo, i, base_binfo); i++)
1597 tree basetype = BINFO_TYPE (base_binfo);
1599 if (TYPE_CONTAINS_VPTR_P (basetype) && !BINFO_VIRTUAL_P (base_binfo))
1601 primary = base_binfo;
1602 goto found;
1606 /* A "nearly-empty" virtual base class can be the primary base
1607 class, if no non-virtual polymorphic base can be found. Look for
1608 a nearly-empty virtual dynamic base that is not already a primary
1609 base of something in the hierarchy. If there is no such base,
1610 just pick the first nearly-empty virtual base. */
1612 for (base_binfo = TREE_CHAIN (type_binfo); base_binfo;
1613 base_binfo = TREE_CHAIN (base_binfo))
1614 if (BINFO_VIRTUAL_P (base_binfo)
1615 && CLASSTYPE_NEARLY_EMPTY_P (BINFO_TYPE (base_binfo)))
1617 if (!BINFO_PRIMARY_P (base_binfo))
1619 /* Found one that is not primary. */
1620 primary = base_binfo;
1621 goto found;
1623 else if (!primary)
1624 /* Remember the first candidate. */
1625 primary = base_binfo;
1628 found:
1629 /* If we've got a primary base, use it. */
1630 if (primary)
1632 tree basetype = BINFO_TYPE (primary);
1634 CLASSTYPE_PRIMARY_BINFO (t) = primary;
1635 if (BINFO_PRIMARY_P (primary))
1636 /* We are stealing a primary base. */
1637 BINFO_LOST_PRIMARY_P (BINFO_INHERITANCE_CHAIN (primary)) = 1;
1638 BINFO_PRIMARY_P (primary) = 1;
1639 if (BINFO_VIRTUAL_P (primary))
1641 tree delta;
1643 BINFO_INHERITANCE_CHAIN (primary) = type_binfo;
1644 /* A virtual binfo might have been copied from within
1645 another hierarchy. As we're about to use it as a primary
1646 base, make sure the offsets match. */
1647 delta = size_diffop_loc (input_location, ssize_int (0),
1648 convert (ssizetype, BINFO_OFFSET (primary)));
1650 propagate_binfo_offsets (primary, delta);
1653 primary = TYPE_BINFO (basetype);
1655 TYPE_VFIELD (t) = TYPE_VFIELD (basetype);
1656 BINFO_VTABLE (type_binfo) = BINFO_VTABLE (primary);
1657 BINFO_VIRTUALS (type_binfo) = BINFO_VIRTUALS (primary);
1661 /* Update the variant types of T. */
1663 void
1664 fixup_type_variants (tree t)
1666 tree variants;
1668 if (!t)
1669 return;
1671 for (variants = TYPE_NEXT_VARIANT (t);
1672 variants;
1673 variants = TYPE_NEXT_VARIANT (variants))
1675 /* These fields are in the _TYPE part of the node, not in
1676 the TYPE_LANG_SPECIFIC component, so they are not shared. */
1677 TYPE_HAS_USER_CONSTRUCTOR (variants) = TYPE_HAS_USER_CONSTRUCTOR (t);
1678 TYPE_NEEDS_CONSTRUCTING (variants) = TYPE_NEEDS_CONSTRUCTING (t);
1679 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (variants)
1680 = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t);
1682 TYPE_POLYMORPHIC_P (variants) = TYPE_POLYMORPHIC_P (t);
1684 TYPE_BINFO (variants) = TYPE_BINFO (t);
1686 /* Copy whatever these are holding today. */
1687 TYPE_VFIELD (variants) = TYPE_VFIELD (t);
1688 TYPE_METHODS (variants) = TYPE_METHODS (t);
1689 TYPE_FIELDS (variants) = TYPE_FIELDS (t);
1693 /* Early variant fixups: we apply attributes at the beginning of the class
1694 definition, and we need to fix up any variants that have already been
1695 made via elaborated-type-specifier so that check_qualified_type works. */
1697 void
1698 fixup_attribute_variants (tree t)
1700 tree variants;
1702 if (!t)
1703 return;
1705 for (variants = TYPE_NEXT_VARIANT (t);
1706 variants;
1707 variants = TYPE_NEXT_VARIANT (variants))
1709 /* These are the two fields that check_qualified_type looks at and
1710 are affected by attributes. */
1711 TYPE_ATTRIBUTES (variants) = TYPE_ATTRIBUTES (t);
1712 TYPE_ALIGN (variants) = TYPE_ALIGN (t);
1716 /* Set memoizing fields and bits of T (and its variants) for later
1717 use. */
1719 static void
1720 finish_struct_bits (tree t)
1722 /* Fix up variants (if any). */
1723 fixup_type_variants (t);
1725 if (BINFO_N_BASE_BINFOS (TYPE_BINFO (t)) && TYPE_POLYMORPHIC_P (t))
1726 /* For a class w/o baseclasses, 'finish_struct' has set
1727 CLASSTYPE_PURE_VIRTUALS correctly (by definition).
1728 Similarly for a class whose base classes do not have vtables.
1729 When neither of these is true, we might have removed abstract
1730 virtuals (by providing a definition), added some (by declaring
1731 new ones), or redeclared ones from a base class. We need to
1732 recalculate what's really an abstract virtual at this point (by
1733 looking in the vtables). */
1734 get_pure_virtuals (t);
1736 /* If this type has a copy constructor or a destructor, force its
1737 mode to be BLKmode, and force its TREE_ADDRESSABLE bit to be
1738 nonzero. This will cause it to be passed by invisible reference
1739 and prevent it from being returned in a register. */
1740 if (type_has_nontrivial_copy_init (t)
1741 || TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t))
1743 tree variants;
1744 DECL_MODE (TYPE_MAIN_DECL (t)) = BLKmode;
1745 for (variants = t; variants; variants = TYPE_NEXT_VARIANT (variants))
1747 SET_TYPE_MODE (variants, BLKmode);
1748 TREE_ADDRESSABLE (variants) = 1;
1753 /* Issue warnings about T having private constructors, but no friends,
1754 and so forth.
1756 HAS_NONPRIVATE_METHOD is nonzero if T has any non-private methods or
1757 static members. HAS_NONPRIVATE_STATIC_FN is nonzero if T has any
1758 non-private static member functions. */
1760 static void
1761 maybe_warn_about_overly_private_class (tree t)
1763 int has_member_fn = 0;
1764 int has_nonprivate_method = 0;
1765 tree fn;
1767 if (!warn_ctor_dtor_privacy
1768 /* If the class has friends, those entities might create and
1769 access instances, so we should not warn. */
1770 || (CLASSTYPE_FRIEND_CLASSES (t)
1771 || DECL_FRIENDLIST (TYPE_MAIN_DECL (t)))
1772 /* We will have warned when the template was declared; there's
1773 no need to warn on every instantiation. */
1774 || CLASSTYPE_TEMPLATE_INSTANTIATION (t))
1775 /* There's no reason to even consider warning about this
1776 class. */
1777 return;
1779 /* We only issue one warning, if more than one applies, because
1780 otherwise, on code like:
1782 class A {
1783 // Oops - forgot `public:'
1784 A();
1785 A(const A&);
1786 ~A();
1789 we warn several times about essentially the same problem. */
1791 /* Check to see if all (non-constructor, non-destructor) member
1792 functions are private. (Since there are no friends or
1793 non-private statics, we can't ever call any of the private member
1794 functions.) */
1795 for (fn = TYPE_METHODS (t); fn; fn = DECL_CHAIN (fn))
1796 /* We're not interested in compiler-generated methods; they don't
1797 provide any way to call private members. */
1798 if (!DECL_ARTIFICIAL (fn))
1800 if (!TREE_PRIVATE (fn))
1802 if (DECL_STATIC_FUNCTION_P (fn))
1803 /* A non-private static member function is just like a
1804 friend; it can create and invoke private member
1805 functions, and be accessed without a class
1806 instance. */
1807 return;
1809 has_nonprivate_method = 1;
1810 /* Keep searching for a static member function. */
1812 else if (!DECL_CONSTRUCTOR_P (fn) && !DECL_DESTRUCTOR_P (fn))
1813 has_member_fn = 1;
1816 if (!has_nonprivate_method && has_member_fn)
1818 /* There are no non-private methods, and there's at least one
1819 private member function that isn't a constructor or
1820 destructor. (If all the private members are
1821 constructors/destructors we want to use the code below that
1822 issues error messages specifically referring to
1823 constructors/destructors.) */
1824 unsigned i;
1825 tree binfo = TYPE_BINFO (t);
1827 for (i = 0; i != BINFO_N_BASE_BINFOS (binfo); i++)
1828 if (BINFO_BASE_ACCESS (binfo, i) != access_private_node)
1830 has_nonprivate_method = 1;
1831 break;
1833 if (!has_nonprivate_method)
1835 warning (OPT_Wctor_dtor_privacy,
1836 "all member functions in class %qT are private", t);
1837 return;
1841 /* Even if some of the member functions are non-private, the class
1842 won't be useful for much if all the constructors or destructors
1843 are private: such an object can never be created or destroyed. */
1844 fn = CLASSTYPE_DESTRUCTORS (t);
1845 if (fn && TREE_PRIVATE (fn))
1847 warning (OPT_Wctor_dtor_privacy,
1848 "%q#T only defines a private destructor and has no friends",
1850 return;
1853 /* Warn about classes that have private constructors and no friends. */
1854 if (TYPE_HAS_USER_CONSTRUCTOR (t)
1855 /* Implicitly generated constructors are always public. */
1856 && (!CLASSTYPE_LAZY_DEFAULT_CTOR (t)
1857 || !CLASSTYPE_LAZY_COPY_CTOR (t)))
1859 int nonprivate_ctor = 0;
1861 /* If a non-template class does not define a copy
1862 constructor, one is defined for it, enabling it to avoid
1863 this warning. For a template class, this does not
1864 happen, and so we would normally get a warning on:
1866 template <class T> class C { private: C(); };
1868 To avoid this asymmetry, we check TYPE_HAS_COPY_CTOR. All
1869 complete non-template or fully instantiated classes have this
1870 flag set. */
1871 if (!TYPE_HAS_COPY_CTOR (t))
1872 nonprivate_ctor = 1;
1873 else
1874 for (fn = CLASSTYPE_CONSTRUCTORS (t); fn; fn = OVL_NEXT (fn))
1876 tree ctor = OVL_CURRENT (fn);
1877 /* Ideally, we wouldn't count copy constructors (or, in
1878 fact, any constructor that takes an argument of the
1879 class type as a parameter) because such things cannot
1880 be used to construct an instance of the class unless
1881 you already have one. But, for now at least, we're
1882 more generous. */
1883 if (! TREE_PRIVATE (ctor))
1885 nonprivate_ctor = 1;
1886 break;
1890 if (nonprivate_ctor == 0)
1892 warning (OPT_Wctor_dtor_privacy,
1893 "%q#T only defines private constructors and has no friends",
1895 return;
1900 static struct {
1901 gt_pointer_operator new_value;
1902 void *cookie;
1903 } resort_data;
1905 /* Comparison function to compare two TYPE_METHOD_VEC entries by name. */
1907 static int
1908 method_name_cmp (const void* m1_p, const void* m2_p)
1910 const tree *const m1 = (const tree *) m1_p;
1911 const tree *const m2 = (const tree *) m2_p;
1913 if (*m1 == NULL_TREE && *m2 == NULL_TREE)
1914 return 0;
1915 if (*m1 == NULL_TREE)
1916 return -1;
1917 if (*m2 == NULL_TREE)
1918 return 1;
1919 if (DECL_NAME (OVL_CURRENT (*m1)) < DECL_NAME (OVL_CURRENT (*m2)))
1920 return -1;
1921 return 1;
1924 /* This routine compares two fields like method_name_cmp but using the
1925 pointer operator in resort_field_decl_data. */
1927 static int
1928 resort_method_name_cmp (const void* m1_p, const void* m2_p)
1930 const tree *const m1 = (const tree *) m1_p;
1931 const tree *const m2 = (const tree *) m2_p;
1932 if (*m1 == NULL_TREE && *m2 == NULL_TREE)
1933 return 0;
1934 if (*m1 == NULL_TREE)
1935 return -1;
1936 if (*m2 == NULL_TREE)
1937 return 1;
1939 tree d1 = DECL_NAME (OVL_CURRENT (*m1));
1940 tree d2 = DECL_NAME (OVL_CURRENT (*m2));
1941 resort_data.new_value (&d1, resort_data.cookie);
1942 resort_data.new_value (&d2, resort_data.cookie);
1943 if (d1 < d2)
1944 return -1;
1946 return 1;
1949 /* Resort TYPE_METHOD_VEC because pointers have been reordered. */
1951 void
1952 resort_type_method_vec (void* obj,
1953 void* /*orig_obj*/,
1954 gt_pointer_operator new_value,
1955 void* cookie)
1957 vec<tree, va_gc> *method_vec = (vec<tree, va_gc> *) obj;
1958 int len = vec_safe_length (method_vec);
1959 size_t slot;
1960 tree fn;
1962 /* The type conversion ops have to live at the front of the vec, so we
1963 can't sort them. */
1964 for (slot = CLASSTYPE_FIRST_CONVERSION_SLOT;
1965 vec_safe_iterate (method_vec, slot, &fn);
1966 ++slot)
1967 if (!DECL_CONV_FN_P (OVL_CURRENT (fn)))
1968 break;
1970 if (len - slot > 1)
1972 resort_data.new_value = new_value;
1973 resort_data.cookie = cookie;
1974 qsort (method_vec->address () + slot, len - slot, sizeof (tree),
1975 resort_method_name_cmp);
1979 /* Warn about duplicate methods in fn_fields.
1981 Sort methods that are not special (i.e., constructors, destructors,
1982 and type conversion operators) so that we can find them faster in
1983 search. */
1985 static void
1986 finish_struct_methods (tree t)
1988 tree fn_fields;
1989 vec<tree, va_gc> *method_vec;
1990 int slot, len;
1992 method_vec = CLASSTYPE_METHOD_VEC (t);
1993 if (!method_vec)
1994 return;
1996 len = method_vec->length ();
1998 /* Clear DECL_IN_AGGR_P for all functions. */
1999 for (fn_fields = TYPE_METHODS (t); fn_fields;
2000 fn_fields = DECL_CHAIN (fn_fields))
2001 DECL_IN_AGGR_P (fn_fields) = 0;
2003 /* Issue warnings about private constructors and such. If there are
2004 no methods, then some public defaults are generated. */
2005 maybe_warn_about_overly_private_class (t);
2007 /* The type conversion ops have to live at the front of the vec, so we
2008 can't sort them. */
2009 for (slot = CLASSTYPE_FIRST_CONVERSION_SLOT;
2010 method_vec->iterate (slot, &fn_fields);
2011 ++slot)
2012 if (!DECL_CONV_FN_P (OVL_CURRENT (fn_fields)))
2013 break;
2014 if (len - slot > 1)
2015 qsort (method_vec->address () + slot,
2016 len-slot, sizeof (tree), method_name_cmp);
2019 /* Make BINFO's vtable have N entries, including RTTI entries,
2020 vbase and vcall offsets, etc. Set its type and call the back end
2021 to lay it out. */
2023 static void
2024 layout_vtable_decl (tree binfo, int n)
2026 tree atype;
2027 tree vtable;
2029 atype = build_array_of_n_type (vtable_entry_type, n);
2030 layout_type (atype);
2032 /* We may have to grow the vtable. */
2033 vtable = get_vtbl_decl_for_binfo (binfo);
2034 if (!same_type_p (TREE_TYPE (vtable), atype))
2036 TREE_TYPE (vtable) = atype;
2037 DECL_SIZE (vtable) = DECL_SIZE_UNIT (vtable) = NULL_TREE;
2038 layout_decl (vtable, 0);
2042 /* True iff FNDECL and BASE_FNDECL (both non-static member functions)
2043 have the same signature. */
2046 same_signature_p (const_tree fndecl, const_tree base_fndecl)
2048 /* One destructor overrides another if they are the same kind of
2049 destructor. */
2050 if (DECL_DESTRUCTOR_P (base_fndecl) && DECL_DESTRUCTOR_P (fndecl)
2051 && special_function_p (base_fndecl) == special_function_p (fndecl))
2052 return 1;
2053 /* But a non-destructor never overrides a destructor, nor vice
2054 versa, nor do different kinds of destructors override
2055 one-another. For example, a complete object destructor does not
2056 override a deleting destructor. */
2057 if (DECL_DESTRUCTOR_P (base_fndecl) || DECL_DESTRUCTOR_P (fndecl))
2058 return 0;
2060 if (DECL_NAME (fndecl) == DECL_NAME (base_fndecl)
2061 || (DECL_CONV_FN_P (fndecl)
2062 && DECL_CONV_FN_P (base_fndecl)
2063 && same_type_p (DECL_CONV_FN_TYPE (fndecl),
2064 DECL_CONV_FN_TYPE (base_fndecl))))
2066 tree types, base_types;
2067 types = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
2068 base_types = TYPE_ARG_TYPES (TREE_TYPE (base_fndecl));
2069 if ((cp_type_quals (TREE_TYPE (TREE_VALUE (base_types)))
2070 == cp_type_quals (TREE_TYPE (TREE_VALUE (types))))
2071 && compparms (TREE_CHAIN (base_types), TREE_CHAIN (types)))
2072 return 1;
2074 return 0;
2077 /* Returns TRUE if DERIVED is a binfo containing the binfo BASE as a
2078 subobject. */
2080 static bool
2081 base_derived_from (tree derived, tree base)
2083 tree probe;
2085 for (probe = base; probe; probe = BINFO_INHERITANCE_CHAIN (probe))
2087 if (probe == derived)
2088 return true;
2089 else if (BINFO_VIRTUAL_P (probe))
2090 /* If we meet a virtual base, we can't follow the inheritance
2091 any more. See if the complete type of DERIVED contains
2092 such a virtual base. */
2093 return (binfo_for_vbase (BINFO_TYPE (probe), BINFO_TYPE (derived))
2094 != NULL_TREE);
2096 return false;
2099 typedef struct find_final_overrider_data_s {
2100 /* The function for which we are trying to find a final overrider. */
2101 tree fn;
2102 /* The base class in which the function was declared. */
2103 tree declaring_base;
2104 /* The candidate overriders. */
2105 tree candidates;
2106 /* Path to most derived. */
2107 vec<tree> path;
2108 } find_final_overrider_data;
2110 /* Add the overrider along the current path to FFOD->CANDIDATES.
2111 Returns true if an overrider was found; false otherwise. */
2113 static bool
2114 dfs_find_final_overrider_1 (tree binfo,
2115 find_final_overrider_data *ffod,
2116 unsigned depth)
2118 tree method;
2120 /* If BINFO is not the most derived type, try a more derived class.
2121 A definition there will overrider a definition here. */
2122 if (depth)
2124 depth--;
2125 if (dfs_find_final_overrider_1
2126 (ffod->path[depth], ffod, depth))
2127 return true;
2130 method = look_for_overrides_here (BINFO_TYPE (binfo), ffod->fn);
2131 if (method)
2133 tree *candidate = &ffod->candidates;
2135 /* Remove any candidates overridden by this new function. */
2136 while (*candidate)
2138 /* If *CANDIDATE overrides METHOD, then METHOD
2139 cannot override anything else on the list. */
2140 if (base_derived_from (TREE_VALUE (*candidate), binfo))
2141 return true;
2142 /* If METHOD overrides *CANDIDATE, remove *CANDIDATE. */
2143 if (base_derived_from (binfo, TREE_VALUE (*candidate)))
2144 *candidate = TREE_CHAIN (*candidate);
2145 else
2146 candidate = &TREE_CHAIN (*candidate);
2149 /* Add the new function. */
2150 ffod->candidates = tree_cons (method, binfo, ffod->candidates);
2151 return true;
2154 return false;
2157 /* Called from find_final_overrider via dfs_walk. */
2159 static tree
2160 dfs_find_final_overrider_pre (tree binfo, void *data)
2162 find_final_overrider_data *ffod = (find_final_overrider_data *) data;
2164 if (binfo == ffod->declaring_base)
2165 dfs_find_final_overrider_1 (binfo, ffod, ffod->path.length ());
2166 ffod->path.safe_push (binfo);
2168 return NULL_TREE;
2171 static tree
2172 dfs_find_final_overrider_post (tree /*binfo*/, void *data)
2174 find_final_overrider_data *ffod = (find_final_overrider_data *) data;
2175 ffod->path.pop ();
2177 return NULL_TREE;
2180 /* Returns a TREE_LIST whose TREE_PURPOSE is the final overrider for
2181 FN and whose TREE_VALUE is the binfo for the base where the
2182 overriding occurs. BINFO (in the hierarchy dominated by the binfo
2183 DERIVED) is the base object in which FN is declared. */
2185 static tree
2186 find_final_overrider (tree derived, tree binfo, tree fn)
2188 find_final_overrider_data ffod;
2190 /* Getting this right is a little tricky. This is valid:
2192 struct S { virtual void f (); };
2193 struct T { virtual void f (); };
2194 struct U : public S, public T { };
2196 even though calling `f' in `U' is ambiguous. But,
2198 struct R { virtual void f(); };
2199 struct S : virtual public R { virtual void f (); };
2200 struct T : virtual public R { virtual void f (); };
2201 struct U : public S, public T { };
2203 is not -- there's no way to decide whether to put `S::f' or
2204 `T::f' in the vtable for `R'.
2206 The solution is to look at all paths to BINFO. If we find
2207 different overriders along any two, then there is a problem. */
2208 if (DECL_THUNK_P (fn))
2209 fn = THUNK_TARGET (fn);
2211 /* Determine the depth of the hierarchy. */
2212 ffod.fn = fn;
2213 ffod.declaring_base = binfo;
2214 ffod.candidates = NULL_TREE;
2215 ffod.path.create (30);
2217 dfs_walk_all (derived, dfs_find_final_overrider_pre,
2218 dfs_find_final_overrider_post, &ffod);
2220 ffod.path.release ();
2222 /* If there was no winner, issue an error message. */
2223 if (!ffod.candidates || TREE_CHAIN (ffod.candidates))
2224 return error_mark_node;
2226 return ffod.candidates;
2229 /* Return the index of the vcall offset for FN when TYPE is used as a
2230 virtual base. */
2232 static tree
2233 get_vcall_index (tree fn, tree type)
2235 vec<tree_pair_s, va_gc> *indices = CLASSTYPE_VCALL_INDICES (type);
2236 tree_pair_p p;
2237 unsigned ix;
2239 FOR_EACH_VEC_SAFE_ELT (indices, ix, p)
2240 if ((DECL_DESTRUCTOR_P (fn) && DECL_DESTRUCTOR_P (p->purpose))
2241 || same_signature_p (fn, p->purpose))
2242 return p->value;
2244 /* There should always be an appropriate index. */
2245 gcc_unreachable ();
2248 /* Update an entry in the vtable for BINFO, which is in the hierarchy
2249 dominated by T. FN is the old function; VIRTUALS points to the
2250 corresponding position in the new BINFO_VIRTUALS list. IX is the index
2251 of that entry in the list. */
2253 static void
2254 update_vtable_entry_for_fn (tree t, tree binfo, tree fn, tree* virtuals,
2255 unsigned ix)
2257 tree b;
2258 tree overrider;
2259 tree delta;
2260 tree virtual_base;
2261 tree first_defn;
2262 tree overrider_fn, overrider_target;
2263 tree target_fn = DECL_THUNK_P (fn) ? THUNK_TARGET (fn) : fn;
2264 tree over_return, base_return;
2265 bool lost = false;
2267 /* Find the nearest primary base (possibly binfo itself) which defines
2268 this function; this is the class the caller will convert to when
2269 calling FN through BINFO. */
2270 for (b = binfo; ; b = get_primary_binfo (b))
2272 gcc_assert (b);
2273 if (look_for_overrides_here (BINFO_TYPE (b), target_fn))
2274 break;
2276 /* The nearest definition is from a lost primary. */
2277 if (BINFO_LOST_PRIMARY_P (b))
2278 lost = true;
2280 first_defn = b;
2282 /* Find the final overrider. */
2283 overrider = find_final_overrider (TYPE_BINFO (t), b, target_fn);
2284 if (overrider == error_mark_node)
2286 error ("no unique final overrider for %qD in %qT", target_fn, t);
2287 return;
2289 overrider_target = overrider_fn = TREE_PURPOSE (overrider);
2291 /* Check for adjusting covariant return types. */
2292 over_return = TREE_TYPE (TREE_TYPE (overrider_target));
2293 base_return = TREE_TYPE (TREE_TYPE (target_fn));
2295 if (POINTER_TYPE_P (over_return)
2296 && TREE_CODE (over_return) == TREE_CODE (base_return)
2297 && CLASS_TYPE_P (TREE_TYPE (over_return))
2298 && CLASS_TYPE_P (TREE_TYPE (base_return))
2299 /* If the overrider is invalid, don't even try. */
2300 && !DECL_INVALID_OVERRIDER_P (overrider_target))
2302 /* If FN is a covariant thunk, we must figure out the adjustment
2303 to the final base FN was converting to. As OVERRIDER_TARGET might
2304 also be converting to the return type of FN, we have to
2305 combine the two conversions here. */
2306 tree fixed_offset, virtual_offset;
2308 over_return = TREE_TYPE (over_return);
2309 base_return = TREE_TYPE (base_return);
2311 if (DECL_THUNK_P (fn))
2313 gcc_assert (DECL_RESULT_THUNK_P (fn));
2314 fixed_offset = ssize_int (THUNK_FIXED_OFFSET (fn));
2315 virtual_offset = THUNK_VIRTUAL_OFFSET (fn);
2317 else
2318 fixed_offset = virtual_offset = NULL_TREE;
2320 if (virtual_offset)
2321 /* Find the equivalent binfo within the return type of the
2322 overriding function. We will want the vbase offset from
2323 there. */
2324 virtual_offset = binfo_for_vbase (BINFO_TYPE (virtual_offset),
2325 over_return);
2326 else if (!same_type_ignoring_top_level_qualifiers_p
2327 (over_return, base_return))
2329 /* There was no existing virtual thunk (which takes
2330 precedence). So find the binfo of the base function's
2331 return type within the overriding function's return type.
2332 We cannot call lookup base here, because we're inside a
2333 dfs_walk, and will therefore clobber the BINFO_MARKED
2334 flags. Fortunately we know the covariancy is valid (it
2335 has already been checked), so we can just iterate along
2336 the binfos, which have been chained in inheritance graph
2337 order. Of course it is lame that we have to repeat the
2338 search here anyway -- we should really be caching pieces
2339 of the vtable and avoiding this repeated work. */
2340 tree thunk_binfo, base_binfo;
2342 /* Find the base binfo within the overriding function's
2343 return type. We will always find a thunk_binfo, except
2344 when the covariancy is invalid (which we will have
2345 already diagnosed). */
2346 for (base_binfo = TYPE_BINFO (base_return),
2347 thunk_binfo = TYPE_BINFO (over_return);
2348 thunk_binfo;
2349 thunk_binfo = TREE_CHAIN (thunk_binfo))
2350 if (SAME_BINFO_TYPE_P (BINFO_TYPE (thunk_binfo),
2351 BINFO_TYPE (base_binfo)))
2352 break;
2354 /* See if virtual inheritance is involved. */
2355 for (virtual_offset = thunk_binfo;
2356 virtual_offset;
2357 virtual_offset = BINFO_INHERITANCE_CHAIN (virtual_offset))
2358 if (BINFO_VIRTUAL_P (virtual_offset))
2359 break;
2361 if (virtual_offset
2362 || (thunk_binfo && !BINFO_OFFSET_ZEROP (thunk_binfo)))
2364 tree offset = convert (ssizetype, BINFO_OFFSET (thunk_binfo));
2366 if (virtual_offset)
2368 /* We convert via virtual base. Adjust the fixed
2369 offset to be from there. */
2370 offset =
2371 size_diffop (offset,
2372 convert (ssizetype,
2373 BINFO_OFFSET (virtual_offset)));
2375 if (fixed_offset)
2376 /* There was an existing fixed offset, this must be
2377 from the base just converted to, and the base the
2378 FN was thunking to. */
2379 fixed_offset = size_binop (PLUS_EXPR, fixed_offset, offset);
2380 else
2381 fixed_offset = offset;
2385 if (fixed_offset || virtual_offset)
2386 /* Replace the overriding function with a covariant thunk. We
2387 will emit the overriding function in its own slot as
2388 well. */
2389 overrider_fn = make_thunk (overrider_target, /*this_adjusting=*/0,
2390 fixed_offset, virtual_offset);
2392 else
2393 gcc_assert (DECL_INVALID_OVERRIDER_P (overrider_target) ||
2394 !DECL_THUNK_P (fn));
2396 /* If we need a covariant thunk, then we may need to adjust first_defn.
2397 The ABI specifies that the thunks emitted with a function are
2398 determined by which bases the function overrides, so we need to be
2399 sure that we're using a thunk for some overridden base; even if we
2400 know that the necessary this adjustment is zero, there may not be an
2401 appropriate zero-this-adjusment thunk for us to use since thunks for
2402 overriding virtual bases always use the vcall offset.
2404 Furthermore, just choosing any base that overrides this function isn't
2405 quite right, as this slot won't be used for calls through a type that
2406 puts a covariant thunk here. Calling the function through such a type
2407 will use a different slot, and that slot is the one that determines
2408 the thunk emitted for that base.
2410 So, keep looking until we find the base that we're really overriding
2411 in this slot: the nearest primary base that doesn't use a covariant
2412 thunk in this slot. */
2413 if (overrider_target != overrider_fn)
2415 if (BINFO_TYPE (b) == DECL_CONTEXT (overrider_target))
2416 /* We already know that the overrider needs a covariant thunk. */
2417 b = get_primary_binfo (b);
2418 for (; ; b = get_primary_binfo (b))
2420 tree main_binfo = TYPE_BINFO (BINFO_TYPE (b));
2421 tree bv = chain_index (ix, BINFO_VIRTUALS (main_binfo));
2422 if (!DECL_THUNK_P (TREE_VALUE (bv)))
2423 break;
2424 if (BINFO_LOST_PRIMARY_P (b))
2425 lost = true;
2427 first_defn = b;
2430 /* Assume that we will produce a thunk that convert all the way to
2431 the final overrider, and not to an intermediate virtual base. */
2432 virtual_base = NULL_TREE;
2434 /* See if we can convert to an intermediate virtual base first, and then
2435 use the vcall offset located there to finish the conversion. */
2436 for (; b; b = BINFO_INHERITANCE_CHAIN (b))
2438 /* If we find the final overrider, then we can stop
2439 walking. */
2440 if (SAME_BINFO_TYPE_P (BINFO_TYPE (b),
2441 BINFO_TYPE (TREE_VALUE (overrider))))
2442 break;
2444 /* If we find a virtual base, and we haven't yet found the
2445 overrider, then there is a virtual base between the
2446 declaring base (first_defn) and the final overrider. */
2447 if (BINFO_VIRTUAL_P (b))
2449 virtual_base = b;
2450 break;
2454 /* Compute the constant adjustment to the `this' pointer. The
2455 `this' pointer, when this function is called, will point at BINFO
2456 (or one of its primary bases, which are at the same offset). */
2457 if (virtual_base)
2458 /* The `this' pointer needs to be adjusted from the declaration to
2459 the nearest virtual base. */
2460 delta = size_diffop_loc (input_location,
2461 convert (ssizetype, BINFO_OFFSET (virtual_base)),
2462 convert (ssizetype, BINFO_OFFSET (first_defn)));
2463 else if (lost)
2464 /* If the nearest definition is in a lost primary, we don't need an
2465 entry in our vtable. Except possibly in a constructor vtable,
2466 if we happen to get our primary back. In that case, the offset
2467 will be zero, as it will be a primary base. */
2468 delta = size_zero_node;
2469 else
2470 /* The `this' pointer needs to be adjusted from pointing to
2471 BINFO to pointing at the base where the final overrider
2472 appears. */
2473 delta = size_diffop_loc (input_location,
2474 convert (ssizetype,
2475 BINFO_OFFSET (TREE_VALUE (overrider))),
2476 convert (ssizetype, BINFO_OFFSET (binfo)));
2478 modify_vtable_entry (t, binfo, overrider_fn, delta, virtuals);
2480 if (virtual_base)
2481 BV_VCALL_INDEX (*virtuals)
2482 = get_vcall_index (overrider_target, BINFO_TYPE (virtual_base));
2483 else
2484 BV_VCALL_INDEX (*virtuals) = NULL_TREE;
2486 BV_LOST_PRIMARY (*virtuals) = lost;
2489 /* Called from modify_all_vtables via dfs_walk. */
2491 static tree
2492 dfs_modify_vtables (tree binfo, void* data)
2494 tree t = (tree) data;
2495 tree virtuals;
2496 tree old_virtuals;
2497 unsigned ix;
2499 if (!TYPE_CONTAINS_VPTR_P (BINFO_TYPE (binfo)))
2500 /* A base without a vtable needs no modification, and its bases
2501 are uninteresting. */
2502 return dfs_skip_bases;
2504 if (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), t)
2505 && !CLASSTYPE_HAS_PRIMARY_BASE_P (t))
2506 /* Don't do the primary vtable, if it's new. */
2507 return NULL_TREE;
2509 if (BINFO_PRIMARY_P (binfo) && !BINFO_VIRTUAL_P (binfo))
2510 /* There's no need to modify the vtable for a non-virtual primary
2511 base; we're not going to use that vtable anyhow. We do still
2512 need to do this for virtual primary bases, as they could become
2513 non-primary in a construction vtable. */
2514 return NULL_TREE;
2516 make_new_vtable (t, binfo);
2518 /* Now, go through each of the virtual functions in the virtual
2519 function table for BINFO. Find the final overrider, and update
2520 the BINFO_VIRTUALS list appropriately. */
2521 for (ix = 0, virtuals = BINFO_VIRTUALS (binfo),
2522 old_virtuals = BINFO_VIRTUALS (TYPE_BINFO (BINFO_TYPE (binfo)));
2523 virtuals;
2524 ix++, virtuals = TREE_CHAIN (virtuals),
2525 old_virtuals = TREE_CHAIN (old_virtuals))
2526 update_vtable_entry_for_fn (t,
2527 binfo,
2528 BV_FN (old_virtuals),
2529 &virtuals, ix);
2531 return NULL_TREE;
2534 /* Update all of the primary and secondary vtables for T. Create new
2535 vtables as required, and initialize their RTTI information. Each
2536 of the functions in VIRTUALS is declared in T and may override a
2537 virtual function from a base class; find and modify the appropriate
2538 entries to point to the overriding functions. Returns a list, in
2539 declaration order, of the virtual functions that are declared in T,
2540 but do not appear in the primary base class vtable, and which
2541 should therefore be appended to the end of the vtable for T. */
2543 static tree
2544 modify_all_vtables (tree t, tree virtuals)
2546 tree binfo = TYPE_BINFO (t);
2547 tree *fnsp;
2549 /* Update all of the vtables. */
2550 dfs_walk_once (binfo, dfs_modify_vtables, NULL, t);
2552 /* Add virtual functions not already in our primary vtable. These
2553 will be both those introduced by this class, and those overridden
2554 from secondary bases. It does not include virtuals merely
2555 inherited from secondary bases. */
2556 for (fnsp = &virtuals; *fnsp; )
2558 tree fn = TREE_VALUE (*fnsp);
2560 if (!value_member (fn, BINFO_VIRTUALS (binfo))
2561 || DECL_VINDEX (fn) == error_mark_node)
2563 /* We don't need to adjust the `this' pointer when
2564 calling this function. */
2565 BV_DELTA (*fnsp) = integer_zero_node;
2566 BV_VCALL_INDEX (*fnsp) = NULL_TREE;
2568 /* This is a function not already in our vtable. Keep it. */
2569 fnsp = &TREE_CHAIN (*fnsp);
2571 else
2572 /* We've already got an entry for this function. Skip it. */
2573 *fnsp = TREE_CHAIN (*fnsp);
2576 return virtuals;
2579 /* Get the base virtual function declarations in T that have the
2580 indicated NAME. */
2582 static tree
2583 get_basefndecls (tree name, tree t)
2585 tree methods;
2586 tree base_fndecls = NULL_TREE;
2587 int n_baseclasses = BINFO_N_BASE_BINFOS (TYPE_BINFO (t));
2588 int i;
2590 /* Find virtual functions in T with the indicated NAME. */
2591 i = lookup_fnfields_1 (t, name);
2592 if (i != -1)
2593 for (methods = (*CLASSTYPE_METHOD_VEC (t))[i];
2594 methods;
2595 methods = OVL_NEXT (methods))
2597 tree method = OVL_CURRENT (methods);
2599 if (TREE_CODE (method) == FUNCTION_DECL
2600 && DECL_VINDEX (method))
2601 base_fndecls = tree_cons (NULL_TREE, method, base_fndecls);
2604 if (base_fndecls)
2605 return base_fndecls;
2607 for (i = 0; i < n_baseclasses; i++)
2609 tree basetype = BINFO_TYPE (BINFO_BASE_BINFO (TYPE_BINFO (t), i));
2610 base_fndecls = chainon (get_basefndecls (name, basetype),
2611 base_fndecls);
2614 return base_fndecls;
2617 /* If this declaration supersedes the declaration of
2618 a method declared virtual in the base class, then
2619 mark this field as being virtual as well. */
2621 void
2622 check_for_override (tree decl, tree ctype)
2624 bool overrides_found = false;
2625 if (TREE_CODE (decl) == TEMPLATE_DECL)
2626 /* In [temp.mem] we have:
2628 A specialization of a member function template does not
2629 override a virtual function from a base class. */
2630 return;
2631 if ((DECL_DESTRUCTOR_P (decl)
2632 || IDENTIFIER_VIRTUAL_P (DECL_NAME (decl))
2633 || DECL_CONV_FN_P (decl))
2634 && look_for_overrides (ctype, decl)
2635 && !DECL_STATIC_FUNCTION_P (decl))
2636 /* Set DECL_VINDEX to a value that is neither an INTEGER_CST nor
2637 the error_mark_node so that we know it is an overriding
2638 function. */
2640 DECL_VINDEX (decl) = decl;
2641 overrides_found = true;
2644 if (DECL_VIRTUAL_P (decl))
2646 if (!DECL_VINDEX (decl))
2647 DECL_VINDEX (decl) = error_mark_node;
2648 IDENTIFIER_VIRTUAL_P (DECL_NAME (decl)) = 1;
2649 if (DECL_DESTRUCTOR_P (decl))
2650 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (ctype) = true;
2652 else if (DECL_FINAL_P (decl))
2653 error ("%q+#D marked final, but is not virtual", decl);
2654 if (DECL_OVERRIDE_P (decl) && !overrides_found)
2655 error ("%q+#D marked override, but does not override", decl);
2658 /* Warn about hidden virtual functions that are not overridden in t.
2659 We know that constructors and destructors don't apply. */
2661 static void
2662 warn_hidden (tree t)
2664 vec<tree, va_gc> *method_vec = CLASSTYPE_METHOD_VEC (t);
2665 tree fns;
2666 size_t i;
2668 /* We go through each separately named virtual function. */
2669 for (i = CLASSTYPE_FIRST_CONVERSION_SLOT;
2670 vec_safe_iterate (method_vec, i, &fns);
2671 ++i)
2673 tree fn;
2674 tree name;
2675 tree fndecl;
2676 tree base_fndecls;
2677 tree base_binfo;
2678 tree binfo;
2679 int j;
2681 /* All functions in this slot in the CLASSTYPE_METHOD_VEC will
2682 have the same name. Figure out what name that is. */
2683 name = DECL_NAME (OVL_CURRENT (fns));
2684 /* There are no possibly hidden functions yet. */
2685 base_fndecls = NULL_TREE;
2686 /* Iterate through all of the base classes looking for possibly
2687 hidden functions. */
2688 for (binfo = TYPE_BINFO (t), j = 0;
2689 BINFO_BASE_ITERATE (binfo, j, base_binfo); j++)
2691 tree basetype = BINFO_TYPE (base_binfo);
2692 base_fndecls = chainon (get_basefndecls (name, basetype),
2693 base_fndecls);
2696 /* If there are no functions to hide, continue. */
2697 if (!base_fndecls)
2698 continue;
2700 /* Remove any overridden functions. */
2701 for (fn = fns; fn; fn = OVL_NEXT (fn))
2703 fndecl = OVL_CURRENT (fn);
2704 if (DECL_VINDEX (fndecl))
2706 tree *prev = &base_fndecls;
2708 while (*prev)
2709 /* If the method from the base class has the same
2710 signature as the method from the derived class, it
2711 has been overridden. */
2712 if (same_signature_p (fndecl, TREE_VALUE (*prev)))
2713 *prev = TREE_CHAIN (*prev);
2714 else
2715 prev = &TREE_CHAIN (*prev);
2719 /* Now give a warning for all base functions without overriders,
2720 as they are hidden. */
2721 while (base_fndecls)
2723 /* Here we know it is a hider, and no overrider exists. */
2724 warning (OPT_Woverloaded_virtual, "%q+D was hidden", TREE_VALUE (base_fndecls));
2725 warning (OPT_Woverloaded_virtual, " by %q+D", fns);
2726 base_fndecls = TREE_CHAIN (base_fndecls);
2731 /* Check for things that are invalid. There are probably plenty of other
2732 things we should check for also. */
2734 static void
2735 finish_struct_anon (tree t)
2737 tree field;
2739 for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
2741 if (TREE_STATIC (field))
2742 continue;
2743 if (TREE_CODE (field) != FIELD_DECL)
2744 continue;
2746 if (DECL_NAME (field) == NULL_TREE
2747 && ANON_AGGR_TYPE_P (TREE_TYPE (field)))
2749 bool is_union = TREE_CODE (TREE_TYPE (field)) == UNION_TYPE;
2750 tree elt = TYPE_FIELDS (TREE_TYPE (field));
2751 for (; elt; elt = DECL_CHAIN (elt))
2753 /* We're generally only interested in entities the user
2754 declared, but we also find nested classes by noticing
2755 the TYPE_DECL that we create implicitly. You're
2756 allowed to put one anonymous union inside another,
2757 though, so we explicitly tolerate that. We use
2758 TYPE_ANONYMOUS_P rather than ANON_AGGR_TYPE_P so that
2759 we also allow unnamed types used for defining fields. */
2760 if (DECL_ARTIFICIAL (elt)
2761 && (!DECL_IMPLICIT_TYPEDEF_P (elt)
2762 || TYPE_ANONYMOUS_P (TREE_TYPE (elt))))
2763 continue;
2765 if (TREE_CODE (elt) != FIELD_DECL)
2767 if (is_union)
2768 permerror (input_location, "%q+#D invalid; an anonymous union can "
2769 "only have non-static data members", elt);
2770 else
2771 permerror (input_location, "%q+#D invalid; an anonymous struct can "
2772 "only have non-static data members", elt);
2773 continue;
2776 if (TREE_PRIVATE (elt))
2778 if (is_union)
2779 permerror (input_location, "private member %q+#D in anonymous union", elt);
2780 else
2781 permerror (input_location, "private member %q+#D in anonymous struct", elt);
2783 else if (TREE_PROTECTED (elt))
2785 if (is_union)
2786 permerror (input_location, "protected member %q+#D in anonymous union", elt);
2787 else
2788 permerror (input_location, "protected member %q+#D in anonymous struct", elt);
2791 TREE_PRIVATE (elt) = TREE_PRIVATE (field);
2792 TREE_PROTECTED (elt) = TREE_PROTECTED (field);
2798 /* Add T to CLASSTYPE_DECL_LIST of current_class_type which
2799 will be used later during class template instantiation.
2800 When FRIEND_P is zero, T can be a static member data (VAR_DECL),
2801 a non-static member data (FIELD_DECL), a member function
2802 (FUNCTION_DECL), a nested type (RECORD_TYPE, ENUM_TYPE),
2803 a typedef (TYPE_DECL) or a member class template (TEMPLATE_DECL)
2804 When FRIEND_P is nonzero, T is either a friend class
2805 (RECORD_TYPE, TEMPLATE_DECL) or a friend function
2806 (FUNCTION_DECL, TEMPLATE_DECL). */
2808 void
2809 maybe_add_class_template_decl_list (tree type, tree t, int friend_p)
2811 /* Save some memory by not creating TREE_LIST if TYPE is not template. */
2812 if (CLASSTYPE_TEMPLATE_INFO (type))
2813 CLASSTYPE_DECL_LIST (type)
2814 = tree_cons (friend_p ? NULL_TREE : type,
2815 t, CLASSTYPE_DECL_LIST (type));
2818 /* This function is called from declare_virt_assop_and_dtor via
2819 dfs_walk_all.
2821 DATA is a type that direcly or indirectly inherits the base
2822 represented by BINFO. If BINFO contains a virtual assignment [copy
2823 assignment or move assigment] operator or a virtual constructor,
2824 declare that function in DATA if it hasn't been already declared. */
2826 static tree
2827 dfs_declare_virt_assop_and_dtor (tree binfo, void *data)
2829 tree bv, fn, t = (tree)data;
2830 tree opname = ansi_assopname (NOP_EXPR);
2832 gcc_assert (t && CLASS_TYPE_P (t));
2833 gcc_assert (binfo && TREE_CODE (binfo) == TREE_BINFO);
2835 if (!TYPE_CONTAINS_VPTR_P (BINFO_TYPE (binfo)))
2836 /* A base without a vtable needs no modification, and its bases
2837 are uninteresting. */
2838 return dfs_skip_bases;
2840 if (BINFO_PRIMARY_P (binfo))
2841 /* If this is a primary base, then we have already looked at the
2842 virtual functions of its vtable. */
2843 return NULL_TREE;
2845 for (bv = BINFO_VIRTUALS (binfo); bv; bv = TREE_CHAIN (bv))
2847 fn = BV_FN (bv);
2849 if (DECL_NAME (fn) == opname)
2851 if (CLASSTYPE_LAZY_COPY_ASSIGN (t))
2852 lazily_declare_fn (sfk_copy_assignment, t);
2853 if (CLASSTYPE_LAZY_MOVE_ASSIGN (t))
2854 lazily_declare_fn (sfk_move_assignment, t);
2856 else if (DECL_DESTRUCTOR_P (fn)
2857 && CLASSTYPE_LAZY_DESTRUCTOR (t))
2858 lazily_declare_fn (sfk_destructor, t);
2861 return NULL_TREE;
2864 /* If the class type T has a direct or indirect base that contains a
2865 virtual assignment operator or a virtual destructor, declare that
2866 function in T if it hasn't been already declared. */
2868 static void
2869 declare_virt_assop_and_dtor (tree t)
2871 if (!(TYPE_POLYMORPHIC_P (t)
2872 && (CLASSTYPE_LAZY_COPY_ASSIGN (t)
2873 || CLASSTYPE_LAZY_MOVE_ASSIGN (t)
2874 || CLASSTYPE_LAZY_DESTRUCTOR (t))))
2875 return;
2877 dfs_walk_all (TYPE_BINFO (t),
2878 dfs_declare_virt_assop_and_dtor,
2879 NULL, t);
2882 /* Declare the inheriting constructor for class T inherited from base
2883 constructor CTOR with the parameter array PARMS of size NPARMS. */
2885 static void
2886 one_inheriting_sig (tree t, tree ctor, tree *parms, int nparms)
2888 /* We don't declare an inheriting ctor that would be a default,
2889 copy or move ctor. */
2890 if (nparms == 0
2891 || (nparms == 1
2892 && TREE_CODE (parms[0]) == REFERENCE_TYPE
2893 && TYPE_MAIN_VARIANT (TREE_TYPE (parms[0])) == t))
2894 return;
2895 int i;
2896 tree parmlist = void_list_node;
2897 for (i = nparms - 1; i >= 0; i--)
2898 parmlist = tree_cons (NULL_TREE, parms[i], parmlist);
2899 tree fn = implicitly_declare_fn (sfk_inheriting_constructor,
2900 t, false, ctor, parmlist);
2901 if (add_method (t, fn, NULL_TREE))
2903 DECL_CHAIN (fn) = TYPE_METHODS (t);
2904 TYPE_METHODS (t) = fn;
2908 /* Declare all the inheriting constructors for class T inherited from base
2909 constructor CTOR. */
2911 static void
2912 one_inherited_ctor (tree ctor, tree t)
2914 tree parms = FUNCTION_FIRST_USER_PARMTYPE (ctor);
2916 tree *new_parms = XALLOCAVEC (tree, list_length (parms));
2917 int i = 0;
2918 for (; parms && parms != void_list_node; parms = TREE_CHAIN (parms))
2920 if (TREE_PURPOSE (parms))
2921 one_inheriting_sig (t, ctor, new_parms, i);
2922 new_parms[i++] = TREE_VALUE (parms);
2924 one_inheriting_sig (t, ctor, new_parms, i);
2925 if (parms == NULL_TREE)
2927 warning (OPT_Winherited_variadic_ctor,
2928 "the ellipsis in %qD is not inherited", ctor);
2929 inform (DECL_SOURCE_LOCATION (ctor), "%qD declared here", ctor);
2933 /* Create default constructors, assignment operators, and so forth for
2934 the type indicated by T, if they are needed. CANT_HAVE_CONST_CTOR,
2935 and CANT_HAVE_CONST_ASSIGNMENT are nonzero if, for whatever reason,
2936 the class cannot have a default constructor, copy constructor
2937 taking a const reference argument, or an assignment operator taking
2938 a const reference, respectively. */
2940 static void
2941 add_implicitly_declared_members (tree t, tree* access_decls,
2942 int cant_have_const_cctor,
2943 int cant_have_const_assignment)
2945 bool move_ok = false;
2947 if (cxx_dialect >= cxx0x && !CLASSTYPE_DESTRUCTORS (t)
2948 && !TYPE_HAS_COPY_CTOR (t) && !TYPE_HAS_COPY_ASSIGN (t)
2949 && !type_has_move_constructor (t) && !type_has_move_assign (t))
2950 move_ok = true;
2952 /* Destructor. */
2953 if (!CLASSTYPE_DESTRUCTORS (t))
2955 /* In general, we create destructors lazily. */
2956 CLASSTYPE_LAZY_DESTRUCTOR (t) = 1;
2958 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t)
2959 && TYPE_FOR_JAVA (t))
2960 /* But if this is a Java class, any non-trivial destructor is
2961 invalid, even if compiler-generated. Therefore, if the
2962 destructor is non-trivial we create it now. */
2963 lazily_declare_fn (sfk_destructor, t);
2966 /* [class.ctor]
2968 If there is no user-declared constructor for a class, a default
2969 constructor is implicitly declared. */
2970 if (! TYPE_HAS_USER_CONSTRUCTOR (t))
2972 TYPE_HAS_DEFAULT_CONSTRUCTOR (t) = 1;
2973 CLASSTYPE_LAZY_DEFAULT_CTOR (t) = 1;
2974 if (cxx_dialect >= cxx0x)
2975 TYPE_HAS_CONSTEXPR_CTOR (t)
2976 /* This might force the declaration. */
2977 = type_has_constexpr_default_constructor (t);
2980 /* [class.ctor]
2982 If a class definition does not explicitly declare a copy
2983 constructor, one is declared implicitly. */
2984 if (! TYPE_HAS_COPY_CTOR (t) && ! TYPE_FOR_JAVA (t))
2986 TYPE_HAS_COPY_CTOR (t) = 1;
2987 TYPE_HAS_CONST_COPY_CTOR (t) = !cant_have_const_cctor;
2988 CLASSTYPE_LAZY_COPY_CTOR (t) = 1;
2989 if (move_ok)
2990 CLASSTYPE_LAZY_MOVE_CTOR (t) = 1;
2993 /* If there is no assignment operator, one will be created if and
2994 when it is needed. For now, just record whether or not the type
2995 of the parameter to the assignment operator will be a const or
2996 non-const reference. */
2997 if (!TYPE_HAS_COPY_ASSIGN (t) && !TYPE_FOR_JAVA (t))
2999 TYPE_HAS_COPY_ASSIGN (t) = 1;
3000 TYPE_HAS_CONST_COPY_ASSIGN (t) = !cant_have_const_assignment;
3001 CLASSTYPE_LAZY_COPY_ASSIGN (t) = 1;
3002 if (move_ok)
3003 CLASSTYPE_LAZY_MOVE_ASSIGN (t) = 1;
3006 /* We can't be lazy about declaring functions that might override
3007 a virtual function from a base class. */
3008 declare_virt_assop_and_dtor (t);
3010 while (*access_decls)
3012 tree using_decl = TREE_VALUE (*access_decls);
3013 tree decl = USING_DECL_DECLS (using_decl);
3014 if (DECL_SELF_REFERENCE_P (decl))
3016 /* declare, then remove the decl */
3017 tree ctor_list = lookup_fnfields_slot (TREE_TYPE (decl),
3018 ctor_identifier);
3019 location_t loc = input_location;
3020 input_location = DECL_SOURCE_LOCATION (using_decl);
3021 if (ctor_list)
3022 for (; ctor_list; ctor_list = OVL_NEXT (ctor_list))
3023 one_inherited_ctor (OVL_CURRENT (ctor_list), t);
3024 *access_decls = TREE_CHAIN (*access_decls);
3025 input_location = loc;
3027 else
3028 access_decls = &TREE_CHAIN (*access_decls);
3032 /* Subroutine of insert_into_classtype_sorted_fields. Recursively
3033 count the number of fields in TYPE, including anonymous union
3034 members. */
3036 static int
3037 count_fields (tree fields)
3039 tree x;
3040 int n_fields = 0;
3041 for (x = fields; x; x = DECL_CHAIN (x))
3043 if (TREE_CODE (x) == FIELD_DECL && ANON_AGGR_TYPE_P (TREE_TYPE (x)))
3044 n_fields += count_fields (TYPE_FIELDS (TREE_TYPE (x)));
3045 else
3046 n_fields += 1;
3048 return n_fields;
3051 /* Subroutine of insert_into_classtype_sorted_fields. Recursively add
3052 all the fields in the TREE_LIST FIELDS to the SORTED_FIELDS_TYPE
3053 elts, starting at offset IDX. */
3055 static int
3056 add_fields_to_record_type (tree fields, struct sorted_fields_type *field_vec, int idx)
3058 tree x;
3059 for (x = fields; x; x = DECL_CHAIN (x))
3061 if (TREE_CODE (x) == FIELD_DECL && ANON_AGGR_TYPE_P (TREE_TYPE (x)))
3062 idx = add_fields_to_record_type (TYPE_FIELDS (TREE_TYPE (x)), field_vec, idx);
3063 else
3064 field_vec->elts[idx++] = x;
3066 return idx;
3069 /* Add all of the enum values of ENUMTYPE, to the FIELD_VEC elts,
3070 starting at offset IDX. */
3072 static int
3073 add_enum_fields_to_record_type (tree enumtype,
3074 struct sorted_fields_type *field_vec,
3075 int idx)
3077 tree values;
3078 for (values = TYPE_VALUES (enumtype); values; values = TREE_CHAIN (values))
3079 field_vec->elts[idx++] = TREE_VALUE (values);
3080 return idx;
3083 /* FIELD is a bit-field. We are finishing the processing for its
3084 enclosing type. Issue any appropriate messages and set appropriate
3085 flags. Returns false if an error has been diagnosed. */
3087 static bool
3088 check_bitfield_decl (tree field)
3090 tree type = TREE_TYPE (field);
3091 tree w;
3093 /* Extract the declared width of the bitfield, which has been
3094 temporarily stashed in DECL_INITIAL. */
3095 w = DECL_INITIAL (field);
3096 gcc_assert (w != NULL_TREE);
3097 /* Remove the bit-field width indicator so that the rest of the
3098 compiler does not treat that value as an initializer. */
3099 DECL_INITIAL (field) = NULL_TREE;
3101 /* Detect invalid bit-field type. */
3102 if (!INTEGRAL_OR_ENUMERATION_TYPE_P (type))
3104 error ("bit-field %q+#D with non-integral type", field);
3105 w = error_mark_node;
3107 else
3109 location_t loc = input_location;
3110 /* Avoid the non_lvalue wrapper added by fold for PLUS_EXPRs. */
3111 STRIP_NOPS (w);
3113 /* detect invalid field size. */
3114 input_location = DECL_SOURCE_LOCATION (field);
3115 w = cxx_constant_value (w);
3116 input_location = loc;
3118 if (TREE_CODE (w) != INTEGER_CST)
3120 error ("bit-field %q+D width not an integer constant", field);
3121 w = error_mark_node;
3123 else if (tree_int_cst_sgn (w) < 0)
3125 error ("negative width in bit-field %q+D", field);
3126 w = error_mark_node;
3128 else if (integer_zerop (w) && DECL_NAME (field) != 0)
3130 error ("zero width for bit-field %q+D", field);
3131 w = error_mark_node;
3133 else if (compare_tree_int (w, TYPE_PRECISION (type)) > 0
3134 && TREE_CODE (type) != ENUMERAL_TYPE
3135 && TREE_CODE (type) != BOOLEAN_TYPE)
3136 warning (0, "width of %q+D exceeds its type", field);
3137 else if (TREE_CODE (type) == ENUMERAL_TYPE
3138 && (0 > (compare_tree_int
3139 (w, TYPE_PRECISION (ENUM_UNDERLYING_TYPE (type))))))
3140 warning (0, "%q+D is too small to hold all values of %q#T", field, type);
3143 if (w != error_mark_node)
3145 DECL_SIZE (field) = convert (bitsizetype, w);
3146 DECL_BIT_FIELD (field) = 1;
3147 return true;
3149 else
3151 /* Non-bit-fields are aligned for their type. */
3152 DECL_BIT_FIELD (field) = 0;
3153 CLEAR_DECL_C_BIT_FIELD (field);
3154 return false;
3158 /* FIELD is a non bit-field. We are finishing the processing for its
3159 enclosing type T. Issue any appropriate messages and set appropriate
3160 flags. */
3162 static void
3163 check_field_decl (tree field,
3164 tree t,
3165 int* cant_have_const_ctor,
3166 int* no_const_asn_ref,
3167 int* any_default_members)
3169 tree type = strip_array_types (TREE_TYPE (field));
3171 /* In C++98 an anonymous union cannot contain any fields which would change
3172 the settings of CANT_HAVE_CONST_CTOR and friends. */
3173 if (ANON_UNION_TYPE_P (type) && cxx_dialect < cxx0x)
3175 /* And, we don't set TYPE_HAS_CONST_COPY_CTOR, etc., for anonymous
3176 structs. So, we recurse through their fields here. */
3177 else if (ANON_AGGR_TYPE_P (type))
3179 tree fields;
3181 for (fields = TYPE_FIELDS (type); fields; fields = DECL_CHAIN (fields))
3182 if (TREE_CODE (fields) == FIELD_DECL && !DECL_C_BIT_FIELD (field))
3183 check_field_decl (fields, t, cant_have_const_ctor,
3184 no_const_asn_ref, any_default_members);
3186 /* Check members with class type for constructors, destructors,
3187 etc. */
3188 else if (CLASS_TYPE_P (type))
3190 /* Never let anything with uninheritable virtuals
3191 make it through without complaint. */
3192 abstract_virtuals_error (field, type);
3194 if (TREE_CODE (t) == UNION_TYPE && cxx_dialect < cxx0x)
3196 static bool warned;
3197 int oldcount = errorcount;
3198 if (TYPE_NEEDS_CONSTRUCTING (type))
3199 error ("member %q+#D with constructor not allowed in union",
3200 field);
3201 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
3202 error ("member %q+#D with destructor not allowed in union", field);
3203 if (TYPE_HAS_COMPLEX_COPY_ASSIGN (type))
3204 error ("member %q+#D with copy assignment operator not allowed in union",
3205 field);
3206 if (!warned && errorcount > oldcount)
3208 inform (DECL_SOURCE_LOCATION (field), "unrestricted unions "
3209 "only available with -std=c++11 or -std=gnu++11");
3210 warned = true;
3213 else
3215 TYPE_NEEDS_CONSTRUCTING (t) |= TYPE_NEEDS_CONSTRUCTING (type);
3216 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t)
3217 |= TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type);
3218 TYPE_HAS_COMPLEX_COPY_ASSIGN (t)
3219 |= (TYPE_HAS_COMPLEX_COPY_ASSIGN (type)
3220 || !TYPE_HAS_COPY_ASSIGN (type));
3221 TYPE_HAS_COMPLEX_COPY_CTOR (t) |= (TYPE_HAS_COMPLEX_COPY_CTOR (type)
3222 || !TYPE_HAS_COPY_CTOR (type));
3223 TYPE_HAS_COMPLEX_MOVE_ASSIGN (t) |= TYPE_HAS_COMPLEX_MOVE_ASSIGN (type);
3224 TYPE_HAS_COMPLEX_MOVE_CTOR (t) |= TYPE_HAS_COMPLEX_MOVE_CTOR (type);
3225 TYPE_HAS_COMPLEX_DFLT (t) |= (!TYPE_HAS_DEFAULT_CONSTRUCTOR (type)
3226 || TYPE_HAS_COMPLEX_DFLT (type));
3229 if (TYPE_HAS_COPY_CTOR (type)
3230 && !TYPE_HAS_CONST_COPY_CTOR (type))
3231 *cant_have_const_ctor = 1;
3233 if (TYPE_HAS_COPY_ASSIGN (type)
3234 && !TYPE_HAS_CONST_COPY_ASSIGN (type))
3235 *no_const_asn_ref = 1;
3238 check_abi_tags (t, field);
3240 if (DECL_INITIAL (field) != NULL_TREE)
3242 /* `build_class_init_list' does not recognize
3243 non-FIELD_DECLs. */
3244 if (TREE_CODE (t) == UNION_TYPE && *any_default_members != 0)
3245 error ("multiple fields in union %qT initialized", t);
3246 *any_default_members = 1;
3250 /* Check the data members (both static and non-static), class-scoped
3251 typedefs, etc., appearing in the declaration of T. Issue
3252 appropriate diagnostics. Sets ACCESS_DECLS to a list (in
3253 declaration order) of access declarations; each TREE_VALUE in this
3254 list is a USING_DECL.
3256 In addition, set the following flags:
3258 EMPTY_P
3259 The class is empty, i.e., contains no non-static data members.
3261 CANT_HAVE_CONST_CTOR_P
3262 This class cannot have an implicitly generated copy constructor
3263 taking a const reference.
3265 CANT_HAVE_CONST_ASN_REF
3266 This class cannot have an implicitly generated assignment
3267 operator taking a const reference.
3269 All of these flags should be initialized before calling this
3270 function.
3272 Returns a pointer to the end of the TYPE_FIELDs chain; additional
3273 fields can be added by adding to this chain. */
3275 static void
3276 check_field_decls (tree t, tree *access_decls,
3277 int *cant_have_const_ctor_p,
3278 int *no_const_asn_ref_p)
3280 tree *field;
3281 tree *next;
3282 bool has_pointers;
3283 int any_default_members;
3284 int cant_pack = 0;
3285 int field_access = -1;
3287 /* Assume there are no access declarations. */
3288 *access_decls = NULL_TREE;
3289 /* Assume this class has no pointer members. */
3290 has_pointers = false;
3291 /* Assume none of the members of this class have default
3292 initializations. */
3293 any_default_members = 0;
3295 for (field = &TYPE_FIELDS (t); *field; field = next)
3297 tree x = *field;
3298 tree type = TREE_TYPE (x);
3299 int this_field_access;
3301 next = &DECL_CHAIN (x);
3303 if (TREE_CODE (x) == USING_DECL)
3305 /* Save the access declarations for our caller. */
3306 *access_decls = tree_cons (NULL_TREE, x, *access_decls);
3307 continue;
3310 if (TREE_CODE (x) == TYPE_DECL
3311 || TREE_CODE (x) == TEMPLATE_DECL)
3312 continue;
3314 /* If we've gotten this far, it's a data member, possibly static,
3315 or an enumerator. */
3316 if (TREE_CODE (x) != CONST_DECL)
3317 DECL_CONTEXT (x) = t;
3319 /* When this goes into scope, it will be a non-local reference. */
3320 DECL_NONLOCAL (x) = 1;
3322 if (TREE_CODE (t) == UNION_TYPE)
3324 /* [class.union]
3326 If a union contains a static data member, or a member of
3327 reference type, the program is ill-formed. */
3328 if (TREE_CODE (x) == VAR_DECL)
3330 error ("%q+D may not be static because it is a member of a union", x);
3331 continue;
3333 if (TREE_CODE (type) == REFERENCE_TYPE)
3335 error ("%q+D may not have reference type %qT because"
3336 " it is a member of a union",
3337 x, type);
3338 continue;
3342 /* Perform error checking that did not get done in
3343 grokdeclarator. */
3344 if (TREE_CODE (type) == FUNCTION_TYPE)
3346 error ("field %q+D invalidly declared function type", x);
3347 type = build_pointer_type (type);
3348 TREE_TYPE (x) = type;
3350 else if (TREE_CODE (type) == METHOD_TYPE)
3352 error ("field %q+D invalidly declared method type", x);
3353 type = build_pointer_type (type);
3354 TREE_TYPE (x) = type;
3357 if (type == error_mark_node)
3358 continue;
3360 if (TREE_CODE (x) == CONST_DECL || TREE_CODE (x) == VAR_DECL)
3361 continue;
3363 /* Now it can only be a FIELD_DECL. */
3365 if (TREE_PRIVATE (x) || TREE_PROTECTED (x))
3366 CLASSTYPE_NON_AGGREGATE (t) = 1;
3368 /* If at least one non-static data member is non-literal, the whole
3369 class becomes non-literal. Note: if the type is incomplete we
3370 will complain later on. */
3371 if (COMPLETE_TYPE_P (type) && !literal_type_p (type))
3372 CLASSTYPE_LITERAL_P (t) = false;
3374 /* A standard-layout class is a class that:
3376 has the same access control (Clause 11) for all non-static data members,
3377 ... */
3378 this_field_access = TREE_PROTECTED (x) ? 1 : TREE_PRIVATE (x) ? 2 : 0;
3379 if (field_access == -1)
3380 field_access = this_field_access;
3381 else if (this_field_access != field_access)
3382 CLASSTYPE_NON_STD_LAYOUT (t) = 1;
3384 /* If this is of reference type, check if it needs an init. */
3385 if (TREE_CODE (type) == REFERENCE_TYPE)
3387 CLASSTYPE_NON_LAYOUT_POD_P (t) = 1;
3388 CLASSTYPE_NON_STD_LAYOUT (t) = 1;
3389 if (DECL_INITIAL (x) == NULL_TREE)
3390 SET_CLASSTYPE_REF_FIELDS_NEED_INIT (t, 1);
3392 /* ARM $12.6.2: [A member initializer list] (or, for an
3393 aggregate, initialization by a brace-enclosed list) is the
3394 only way to initialize nonstatic const and reference
3395 members. */
3396 TYPE_HAS_COMPLEX_COPY_ASSIGN (t) = 1;
3397 TYPE_HAS_COMPLEX_MOVE_ASSIGN (t) = 1;
3400 type = strip_array_types (type);
3402 if (TYPE_PACKED (t))
3404 if (!layout_pod_type_p (type) && !TYPE_PACKED (type))
3406 warning
3408 "ignoring packed attribute because of unpacked non-POD field %q+#D",
3410 cant_pack = 1;
3412 else if (DECL_C_BIT_FIELD (x)
3413 || TYPE_ALIGN (TREE_TYPE (x)) > BITS_PER_UNIT)
3414 DECL_PACKED (x) = 1;
3417 if (DECL_C_BIT_FIELD (x) && integer_zerop (DECL_INITIAL (x)))
3418 /* We don't treat zero-width bitfields as making a class
3419 non-empty. */
3421 else
3423 /* The class is non-empty. */
3424 CLASSTYPE_EMPTY_P (t) = 0;
3425 /* The class is not even nearly empty. */
3426 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
3427 /* If one of the data members contains an empty class,
3428 so does T. */
3429 if (CLASS_TYPE_P (type)
3430 && CLASSTYPE_CONTAINS_EMPTY_CLASS_P (type))
3431 CLASSTYPE_CONTAINS_EMPTY_CLASS_P (t) = 1;
3434 /* This is used by -Weffc++ (see below). Warn only for pointers
3435 to members which might hold dynamic memory. So do not warn
3436 for pointers to functions or pointers to members. */
3437 if (TYPE_PTR_P (type)
3438 && !TYPE_PTRFN_P (type))
3439 has_pointers = true;
3441 if (CLASS_TYPE_P (type))
3443 if (CLASSTYPE_REF_FIELDS_NEED_INIT (type))
3444 SET_CLASSTYPE_REF_FIELDS_NEED_INIT (t, 1);
3445 if (CLASSTYPE_READONLY_FIELDS_NEED_INIT (type))
3446 SET_CLASSTYPE_READONLY_FIELDS_NEED_INIT (t, 1);
3449 if (DECL_MUTABLE_P (x) || TYPE_HAS_MUTABLE_P (type))
3450 CLASSTYPE_HAS_MUTABLE (t) = 1;
3452 if (! layout_pod_type_p (type))
3453 /* DR 148 now allows pointers to members (which are POD themselves),
3454 to be allowed in POD structs. */
3455 CLASSTYPE_NON_LAYOUT_POD_P (t) = 1;
3457 if (!std_layout_type_p (type))
3458 CLASSTYPE_NON_STD_LAYOUT (t) = 1;
3460 if (! zero_init_p (type))
3461 CLASSTYPE_NON_ZERO_INIT_P (t) = 1;
3463 /* We set DECL_C_BIT_FIELD in grokbitfield.
3464 If the type and width are valid, we'll also set DECL_BIT_FIELD. */
3465 if (! DECL_C_BIT_FIELD (x) || ! check_bitfield_decl (x))
3466 check_field_decl (x, t,
3467 cant_have_const_ctor_p,
3468 no_const_asn_ref_p,
3469 &any_default_members);
3471 /* Now that we've removed bit-field widths from DECL_INITIAL,
3472 anything left in DECL_INITIAL is an NSDMI that makes the class
3473 non-aggregate. */
3474 if (DECL_INITIAL (x))
3475 CLASSTYPE_NON_AGGREGATE (t) = true;
3477 /* If any field is const, the structure type is pseudo-const. */
3478 if (CP_TYPE_CONST_P (type))
3480 C_TYPE_FIELDS_READONLY (t) = 1;
3481 if (DECL_INITIAL (x) == NULL_TREE)
3482 SET_CLASSTYPE_READONLY_FIELDS_NEED_INIT (t, 1);
3484 /* ARM $12.6.2: [A member initializer list] (or, for an
3485 aggregate, initialization by a brace-enclosed list) is the
3486 only way to initialize nonstatic const and reference
3487 members. */
3488 TYPE_HAS_COMPLEX_COPY_ASSIGN (t) = 1;
3489 TYPE_HAS_COMPLEX_MOVE_ASSIGN (t) = 1;
3491 /* A field that is pseudo-const makes the structure likewise. */
3492 else if (CLASS_TYPE_P (type))
3494 C_TYPE_FIELDS_READONLY (t) |= C_TYPE_FIELDS_READONLY (type);
3495 SET_CLASSTYPE_READONLY_FIELDS_NEED_INIT (t,
3496 CLASSTYPE_READONLY_FIELDS_NEED_INIT (t)
3497 | CLASSTYPE_READONLY_FIELDS_NEED_INIT (type));
3500 /* Core issue 80: A nonstatic data member is required to have a
3501 different name from the class iff the class has a
3502 user-declared constructor. */
3503 if (constructor_name_p (DECL_NAME (x), t)
3504 && TYPE_HAS_USER_CONSTRUCTOR (t))
3505 permerror (input_location, "field %q+#D with same name as class", x);
3508 /* Effective C++ rule 11: if a class has dynamic memory held by pointers,
3509 it should also define a copy constructor and an assignment operator to
3510 implement the correct copy semantic (deep vs shallow, etc.). As it is
3511 not feasible to check whether the constructors do allocate dynamic memory
3512 and store it within members, we approximate the warning like this:
3514 -- Warn only if there are members which are pointers
3515 -- Warn only if there is a non-trivial constructor (otherwise,
3516 there cannot be memory allocated).
3517 -- Warn only if there is a non-trivial destructor. We assume that the
3518 user at least implemented the cleanup correctly, and a destructor
3519 is needed to free dynamic memory.
3521 This seems enough for practical purposes. */
3522 if (warn_ecpp
3523 && has_pointers
3524 && TYPE_HAS_USER_CONSTRUCTOR (t)
3525 && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t)
3526 && !(TYPE_HAS_COPY_CTOR (t) && TYPE_HAS_COPY_ASSIGN (t)))
3528 warning (OPT_Weffc__, "%q#T has pointer data members", t);
3530 if (! TYPE_HAS_COPY_CTOR (t))
3532 warning (OPT_Weffc__,
3533 " but does not override %<%T(const %T&)%>", t, t);
3534 if (!TYPE_HAS_COPY_ASSIGN (t))
3535 warning (OPT_Weffc__, " or %<operator=(const %T&)%>", t);
3537 else if (! TYPE_HAS_COPY_ASSIGN (t))
3538 warning (OPT_Weffc__,
3539 " but does not override %<operator=(const %T&)%>", t);
3542 /* Non-static data member initializers make the default constructor
3543 non-trivial. */
3544 if (any_default_members)
3546 TYPE_NEEDS_CONSTRUCTING (t) = true;
3547 TYPE_HAS_COMPLEX_DFLT (t) = true;
3550 /* If any of the fields couldn't be packed, unset TYPE_PACKED. */
3551 if (cant_pack)
3552 TYPE_PACKED (t) = 0;
3554 /* Check anonymous struct/anonymous union fields. */
3555 finish_struct_anon (t);
3557 /* We've built up the list of access declarations in reverse order.
3558 Fix that now. */
3559 *access_decls = nreverse (*access_decls);
3562 /* If TYPE is an empty class type, records its OFFSET in the table of
3563 OFFSETS. */
3565 static int
3566 record_subobject_offset (tree type, tree offset, splay_tree offsets)
3568 splay_tree_node n;
3570 if (!is_empty_class (type))
3571 return 0;
3573 /* Record the location of this empty object in OFFSETS. */
3574 n = splay_tree_lookup (offsets, (splay_tree_key) offset);
3575 if (!n)
3576 n = splay_tree_insert (offsets,
3577 (splay_tree_key) offset,
3578 (splay_tree_value) NULL_TREE);
3579 n->value = ((splay_tree_value)
3580 tree_cons (NULL_TREE,
3581 type,
3582 (tree) n->value));
3584 return 0;
3587 /* Returns nonzero if TYPE is an empty class type and there is
3588 already an entry in OFFSETS for the same TYPE as the same OFFSET. */
3590 static int
3591 check_subobject_offset (tree type, tree offset, splay_tree offsets)
3593 splay_tree_node n;
3594 tree t;
3596 if (!is_empty_class (type))
3597 return 0;
3599 /* Record the location of this empty object in OFFSETS. */
3600 n = splay_tree_lookup (offsets, (splay_tree_key) offset);
3601 if (!n)
3602 return 0;
3604 for (t = (tree) n->value; t; t = TREE_CHAIN (t))
3605 if (same_type_p (TREE_VALUE (t), type))
3606 return 1;
3608 return 0;
3611 /* Walk through all the subobjects of TYPE (located at OFFSET). Call
3612 F for every subobject, passing it the type, offset, and table of
3613 OFFSETS. If VBASES_P is one, then virtual non-primary bases should
3614 be traversed.
3616 If MAX_OFFSET is non-NULL, then subobjects with an offset greater
3617 than MAX_OFFSET will not be walked.
3619 If F returns a nonzero value, the traversal ceases, and that value
3620 is returned. Otherwise, returns zero. */
3622 static int
3623 walk_subobject_offsets (tree type,
3624 subobject_offset_fn f,
3625 tree offset,
3626 splay_tree offsets,
3627 tree max_offset,
3628 int vbases_p)
3630 int r = 0;
3631 tree type_binfo = NULL_TREE;
3633 /* If this OFFSET is bigger than the MAX_OFFSET, then we should
3634 stop. */
3635 if (max_offset && INT_CST_LT (max_offset, offset))
3636 return 0;
3638 if (type == error_mark_node)
3639 return 0;
3641 if (!TYPE_P (type))
3643 if (abi_version_at_least (2))
3644 type_binfo = type;
3645 type = BINFO_TYPE (type);
3648 if (CLASS_TYPE_P (type))
3650 tree field;
3651 tree binfo;
3652 int i;
3654 /* Avoid recursing into objects that are not interesting. */
3655 if (!CLASSTYPE_CONTAINS_EMPTY_CLASS_P (type))
3656 return 0;
3658 /* Record the location of TYPE. */
3659 r = (*f) (type, offset, offsets);
3660 if (r)
3661 return r;
3663 /* Iterate through the direct base classes of TYPE. */
3664 if (!type_binfo)
3665 type_binfo = TYPE_BINFO (type);
3666 for (i = 0; BINFO_BASE_ITERATE (type_binfo, i, binfo); i++)
3668 tree binfo_offset;
3670 if (abi_version_at_least (2)
3671 && BINFO_VIRTUAL_P (binfo))
3672 continue;
3674 if (!vbases_p
3675 && BINFO_VIRTUAL_P (binfo)
3676 && !BINFO_PRIMARY_P (binfo))
3677 continue;
3679 if (!abi_version_at_least (2))
3680 binfo_offset = size_binop (PLUS_EXPR,
3681 offset,
3682 BINFO_OFFSET (binfo));
3683 else
3685 tree orig_binfo;
3686 /* We cannot rely on BINFO_OFFSET being set for the base
3687 class yet, but the offsets for direct non-virtual
3688 bases can be calculated by going back to the TYPE. */
3689 orig_binfo = BINFO_BASE_BINFO (TYPE_BINFO (type), i);
3690 binfo_offset = size_binop (PLUS_EXPR,
3691 offset,
3692 BINFO_OFFSET (orig_binfo));
3695 r = walk_subobject_offsets (binfo,
3697 binfo_offset,
3698 offsets,
3699 max_offset,
3700 (abi_version_at_least (2)
3701 ? /*vbases_p=*/0 : vbases_p));
3702 if (r)
3703 return r;
3706 if (abi_version_at_least (2) && CLASSTYPE_VBASECLASSES (type))
3708 unsigned ix;
3709 vec<tree, va_gc> *vbases;
3711 /* Iterate through the virtual base classes of TYPE. In G++
3712 3.2, we included virtual bases in the direct base class
3713 loop above, which results in incorrect results; the
3714 correct offsets for virtual bases are only known when
3715 working with the most derived type. */
3716 if (vbases_p)
3717 for (vbases = CLASSTYPE_VBASECLASSES (type), ix = 0;
3718 vec_safe_iterate (vbases, ix, &binfo); ix++)
3720 r = walk_subobject_offsets (binfo,
3722 size_binop (PLUS_EXPR,
3723 offset,
3724 BINFO_OFFSET (binfo)),
3725 offsets,
3726 max_offset,
3727 /*vbases_p=*/0);
3728 if (r)
3729 return r;
3731 else
3733 /* We still have to walk the primary base, if it is
3734 virtual. (If it is non-virtual, then it was walked
3735 above.) */
3736 tree vbase = get_primary_binfo (type_binfo);
3738 if (vbase && BINFO_VIRTUAL_P (vbase)
3739 && BINFO_PRIMARY_P (vbase)
3740 && BINFO_INHERITANCE_CHAIN (vbase) == type_binfo)
3742 r = (walk_subobject_offsets
3743 (vbase, f, offset,
3744 offsets, max_offset, /*vbases_p=*/0));
3745 if (r)
3746 return r;
3751 /* Iterate through the fields of TYPE. */
3752 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
3753 if (TREE_CODE (field) == FIELD_DECL && !DECL_ARTIFICIAL (field))
3755 tree field_offset;
3757 if (abi_version_at_least (2))
3758 field_offset = byte_position (field);
3759 else
3760 /* In G++ 3.2, DECL_FIELD_OFFSET was used. */
3761 field_offset = DECL_FIELD_OFFSET (field);
3763 r = walk_subobject_offsets (TREE_TYPE (field),
3765 size_binop (PLUS_EXPR,
3766 offset,
3767 field_offset),
3768 offsets,
3769 max_offset,
3770 /*vbases_p=*/1);
3771 if (r)
3772 return r;
3775 else if (TREE_CODE (type) == ARRAY_TYPE)
3777 tree element_type = strip_array_types (type);
3778 tree domain = TYPE_DOMAIN (type);
3779 tree index;
3781 /* Avoid recursing into objects that are not interesting. */
3782 if (!CLASS_TYPE_P (element_type)
3783 || !CLASSTYPE_CONTAINS_EMPTY_CLASS_P (element_type))
3784 return 0;
3786 /* Step through each of the elements in the array. */
3787 for (index = size_zero_node;
3788 /* G++ 3.2 had an off-by-one error here. */
3789 (abi_version_at_least (2)
3790 ? !INT_CST_LT (TYPE_MAX_VALUE (domain), index)
3791 : INT_CST_LT (index, TYPE_MAX_VALUE (domain)));
3792 index = size_binop (PLUS_EXPR, index, size_one_node))
3794 r = walk_subobject_offsets (TREE_TYPE (type),
3796 offset,
3797 offsets,
3798 max_offset,
3799 /*vbases_p=*/1);
3800 if (r)
3801 return r;
3802 offset = size_binop (PLUS_EXPR, offset,
3803 TYPE_SIZE_UNIT (TREE_TYPE (type)));
3804 /* If this new OFFSET is bigger than the MAX_OFFSET, then
3805 there's no point in iterating through the remaining
3806 elements of the array. */
3807 if (max_offset && INT_CST_LT (max_offset, offset))
3808 break;
3812 return 0;
3815 /* Record all of the empty subobjects of TYPE (either a type or a
3816 binfo). If IS_DATA_MEMBER is true, then a non-static data member
3817 is being placed at OFFSET; otherwise, it is a base class that is
3818 being placed at OFFSET. */
3820 static void
3821 record_subobject_offsets (tree type,
3822 tree offset,
3823 splay_tree offsets,
3824 bool is_data_member)
3826 tree max_offset;
3827 /* If recording subobjects for a non-static data member or a
3828 non-empty base class , we do not need to record offsets beyond
3829 the size of the biggest empty class. Additional data members
3830 will go at the end of the class. Additional base classes will go
3831 either at offset zero (if empty, in which case they cannot
3832 overlap with offsets past the size of the biggest empty class) or
3833 at the end of the class.
3835 However, if we are placing an empty base class, then we must record
3836 all offsets, as either the empty class is at offset zero (where
3837 other empty classes might later be placed) or at the end of the
3838 class (where other objects might then be placed, so other empty
3839 subobjects might later overlap). */
3840 if (is_data_member
3841 || !is_empty_class (BINFO_TYPE (type)))
3842 max_offset = sizeof_biggest_empty_class;
3843 else
3844 max_offset = NULL_TREE;
3845 walk_subobject_offsets (type, record_subobject_offset, offset,
3846 offsets, max_offset, is_data_member);
3849 /* Returns nonzero if any of the empty subobjects of TYPE (located at
3850 OFFSET) conflict with entries in OFFSETS. If VBASES_P is nonzero,
3851 virtual bases of TYPE are examined. */
3853 static int
3854 layout_conflict_p (tree type,
3855 tree offset,
3856 splay_tree offsets,
3857 int vbases_p)
3859 splay_tree_node max_node;
3861 /* Get the node in OFFSETS that indicates the maximum offset where
3862 an empty subobject is located. */
3863 max_node = splay_tree_max (offsets);
3864 /* If there aren't any empty subobjects, then there's no point in
3865 performing this check. */
3866 if (!max_node)
3867 return 0;
3869 return walk_subobject_offsets (type, check_subobject_offset, offset,
3870 offsets, (tree) (max_node->key),
3871 vbases_p);
3874 /* DECL is a FIELD_DECL corresponding either to a base subobject of a
3875 non-static data member of the type indicated by RLI. BINFO is the
3876 binfo corresponding to the base subobject, OFFSETS maps offsets to
3877 types already located at those offsets. This function determines
3878 the position of the DECL. */
3880 static void
3881 layout_nonempty_base_or_field (record_layout_info rli,
3882 tree decl,
3883 tree binfo,
3884 splay_tree offsets)
3886 tree offset = NULL_TREE;
3887 bool field_p;
3888 tree type;
3890 if (binfo)
3892 /* For the purposes of determining layout conflicts, we want to
3893 use the class type of BINFO; TREE_TYPE (DECL) will be the
3894 CLASSTYPE_AS_BASE version, which does not contain entries for
3895 zero-sized bases. */
3896 type = TREE_TYPE (binfo);
3897 field_p = false;
3899 else
3901 type = TREE_TYPE (decl);
3902 field_p = true;
3905 /* Try to place the field. It may take more than one try if we have
3906 a hard time placing the field without putting two objects of the
3907 same type at the same address. */
3908 while (1)
3910 struct record_layout_info_s old_rli = *rli;
3912 /* Place this field. */
3913 place_field (rli, decl);
3914 offset = byte_position (decl);
3916 /* We have to check to see whether or not there is already
3917 something of the same type at the offset we're about to use.
3918 For example, consider:
3920 struct S {};
3921 struct T : public S { int i; };
3922 struct U : public S, public T {};
3924 Here, we put S at offset zero in U. Then, we can't put T at
3925 offset zero -- its S component would be at the same address
3926 as the S we already allocated. So, we have to skip ahead.
3927 Since all data members, including those whose type is an
3928 empty class, have nonzero size, any overlap can happen only
3929 with a direct or indirect base-class -- it can't happen with
3930 a data member. */
3931 /* In a union, overlap is permitted; all members are placed at
3932 offset zero. */
3933 if (TREE_CODE (rli->t) == UNION_TYPE)
3934 break;
3935 /* G++ 3.2 did not check for overlaps when placing a non-empty
3936 virtual base. */
3937 if (!abi_version_at_least (2) && binfo && BINFO_VIRTUAL_P (binfo))
3938 break;
3939 if (layout_conflict_p (field_p ? type : binfo, offset,
3940 offsets, field_p))
3942 /* Strip off the size allocated to this field. That puts us
3943 at the first place we could have put the field with
3944 proper alignment. */
3945 *rli = old_rli;
3947 /* Bump up by the alignment required for the type. */
3948 rli->bitpos
3949 = size_binop (PLUS_EXPR, rli->bitpos,
3950 bitsize_int (binfo
3951 ? CLASSTYPE_ALIGN (type)
3952 : TYPE_ALIGN (type)));
3953 normalize_rli (rli);
3955 else
3956 /* There was no conflict. We're done laying out this field. */
3957 break;
3960 /* Now that we know where it will be placed, update its
3961 BINFO_OFFSET. */
3962 if (binfo && CLASS_TYPE_P (BINFO_TYPE (binfo)))
3963 /* Indirect virtual bases may have a nonzero BINFO_OFFSET at
3964 this point because their BINFO_OFFSET is copied from another
3965 hierarchy. Therefore, we may not need to add the entire
3966 OFFSET. */
3967 propagate_binfo_offsets (binfo,
3968 size_diffop_loc (input_location,
3969 convert (ssizetype, offset),
3970 convert (ssizetype,
3971 BINFO_OFFSET (binfo))));
3974 /* Returns true if TYPE is empty and OFFSET is nonzero. */
3976 static int
3977 empty_base_at_nonzero_offset_p (tree type,
3978 tree offset,
3979 splay_tree /*offsets*/)
3981 return is_empty_class (type) && !integer_zerop (offset);
3984 /* Layout the empty base BINFO. EOC indicates the byte currently just
3985 past the end of the class, and should be correctly aligned for a
3986 class of the type indicated by BINFO; OFFSETS gives the offsets of
3987 the empty bases allocated so far. T is the most derived
3988 type. Return nonzero iff we added it at the end. */
3990 static bool
3991 layout_empty_base (record_layout_info rli, tree binfo,
3992 tree eoc, splay_tree offsets)
3994 tree alignment;
3995 tree basetype = BINFO_TYPE (binfo);
3996 bool atend = false;
3998 /* This routine should only be used for empty classes. */
3999 gcc_assert (is_empty_class (basetype));
4000 alignment = ssize_int (CLASSTYPE_ALIGN_UNIT (basetype));
4002 if (!integer_zerop (BINFO_OFFSET (binfo)))
4004 if (abi_version_at_least (2))
4005 propagate_binfo_offsets
4006 (binfo, size_diffop_loc (input_location,
4007 size_zero_node, BINFO_OFFSET (binfo)));
4008 else
4009 warning (OPT_Wabi,
4010 "offset of empty base %qT may not be ABI-compliant and may"
4011 "change in a future version of GCC",
4012 BINFO_TYPE (binfo));
4015 /* This is an empty base class. We first try to put it at offset
4016 zero. */
4017 if (layout_conflict_p (binfo,
4018 BINFO_OFFSET (binfo),
4019 offsets,
4020 /*vbases_p=*/0))
4022 /* That didn't work. Now, we move forward from the next
4023 available spot in the class. */
4024 atend = true;
4025 propagate_binfo_offsets (binfo, convert (ssizetype, eoc));
4026 while (1)
4028 if (!layout_conflict_p (binfo,
4029 BINFO_OFFSET (binfo),
4030 offsets,
4031 /*vbases_p=*/0))
4032 /* We finally found a spot where there's no overlap. */
4033 break;
4035 /* There's overlap here, too. Bump along to the next spot. */
4036 propagate_binfo_offsets (binfo, alignment);
4040 if (CLASSTYPE_USER_ALIGN (basetype))
4042 rli->record_align = MAX (rli->record_align, CLASSTYPE_ALIGN (basetype));
4043 if (warn_packed)
4044 rli->unpacked_align = MAX (rli->unpacked_align, CLASSTYPE_ALIGN (basetype));
4045 TYPE_USER_ALIGN (rli->t) = 1;
4048 return atend;
4051 /* Layout the base given by BINFO in the class indicated by RLI.
4052 *BASE_ALIGN is a running maximum of the alignments of
4053 any base class. OFFSETS gives the location of empty base
4054 subobjects. T is the most derived type. Return nonzero if the new
4055 object cannot be nearly-empty. A new FIELD_DECL is inserted at
4056 *NEXT_FIELD, unless BINFO is for an empty base class.
4058 Returns the location at which the next field should be inserted. */
4060 static tree *
4061 build_base_field (record_layout_info rli, tree binfo,
4062 splay_tree offsets, tree *next_field)
4064 tree t = rli->t;
4065 tree basetype = BINFO_TYPE (binfo);
4067 if (!COMPLETE_TYPE_P (basetype))
4068 /* This error is now reported in xref_tag, thus giving better
4069 location information. */
4070 return next_field;
4072 /* Place the base class. */
4073 if (!is_empty_class (basetype))
4075 tree decl;
4077 /* The containing class is non-empty because it has a non-empty
4078 base class. */
4079 CLASSTYPE_EMPTY_P (t) = 0;
4081 /* Create the FIELD_DECL. */
4082 decl = build_decl (input_location,
4083 FIELD_DECL, NULL_TREE, CLASSTYPE_AS_BASE (basetype));
4084 DECL_ARTIFICIAL (decl) = 1;
4085 DECL_IGNORED_P (decl) = 1;
4086 DECL_FIELD_CONTEXT (decl) = t;
4087 if (CLASSTYPE_AS_BASE (basetype))
4089 DECL_SIZE (decl) = CLASSTYPE_SIZE (basetype);
4090 DECL_SIZE_UNIT (decl) = CLASSTYPE_SIZE_UNIT (basetype);
4091 DECL_ALIGN (decl) = CLASSTYPE_ALIGN (basetype);
4092 DECL_USER_ALIGN (decl) = CLASSTYPE_USER_ALIGN (basetype);
4093 DECL_MODE (decl) = TYPE_MODE (basetype);
4094 DECL_FIELD_IS_BASE (decl) = 1;
4096 /* Try to place the field. It may take more than one try if we
4097 have a hard time placing the field without putting two
4098 objects of the same type at the same address. */
4099 layout_nonempty_base_or_field (rli, decl, binfo, offsets);
4100 /* Add the new FIELD_DECL to the list of fields for T. */
4101 DECL_CHAIN (decl) = *next_field;
4102 *next_field = decl;
4103 next_field = &DECL_CHAIN (decl);
4106 else
4108 tree eoc;
4109 bool atend;
4111 /* On some platforms (ARM), even empty classes will not be
4112 byte-aligned. */
4113 eoc = round_up_loc (input_location,
4114 rli_size_unit_so_far (rli),
4115 CLASSTYPE_ALIGN_UNIT (basetype));
4116 atend = layout_empty_base (rli, binfo, eoc, offsets);
4117 /* A nearly-empty class "has no proper base class that is empty,
4118 not morally virtual, and at an offset other than zero." */
4119 if (!BINFO_VIRTUAL_P (binfo) && CLASSTYPE_NEARLY_EMPTY_P (t))
4121 if (atend)
4122 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
4123 /* The check above (used in G++ 3.2) is insufficient because
4124 an empty class placed at offset zero might itself have an
4125 empty base at a nonzero offset. */
4126 else if (walk_subobject_offsets (basetype,
4127 empty_base_at_nonzero_offset_p,
4128 size_zero_node,
4129 /*offsets=*/NULL,
4130 /*max_offset=*/NULL_TREE,
4131 /*vbases_p=*/true))
4133 if (abi_version_at_least (2))
4134 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
4135 else
4136 warning (OPT_Wabi,
4137 "class %qT will be considered nearly empty in a "
4138 "future version of GCC", t);
4142 /* We do not create a FIELD_DECL for empty base classes because
4143 it might overlap some other field. We want to be able to
4144 create CONSTRUCTORs for the class by iterating over the
4145 FIELD_DECLs, and the back end does not handle overlapping
4146 FIELD_DECLs. */
4148 /* An empty virtual base causes a class to be non-empty
4149 -- but in that case we do not need to clear CLASSTYPE_EMPTY_P
4150 here because that was already done when the virtual table
4151 pointer was created. */
4154 /* Record the offsets of BINFO and its base subobjects. */
4155 record_subobject_offsets (binfo,
4156 BINFO_OFFSET (binfo),
4157 offsets,
4158 /*is_data_member=*/false);
4160 return next_field;
4163 /* Layout all of the non-virtual base classes. Record empty
4164 subobjects in OFFSETS. T is the most derived type. Return nonzero
4165 if the type cannot be nearly empty. The fields created
4166 corresponding to the base classes will be inserted at
4167 *NEXT_FIELD. */
4169 static void
4170 build_base_fields (record_layout_info rli,
4171 splay_tree offsets, tree *next_field)
4173 /* Chain to hold all the new FIELD_DECLs which stand in for base class
4174 subobjects. */
4175 tree t = rli->t;
4176 int n_baseclasses = BINFO_N_BASE_BINFOS (TYPE_BINFO (t));
4177 int i;
4179 /* The primary base class is always allocated first. */
4180 if (CLASSTYPE_HAS_PRIMARY_BASE_P (t))
4181 next_field = build_base_field (rli, CLASSTYPE_PRIMARY_BINFO (t),
4182 offsets, next_field);
4184 /* Now allocate the rest of the bases. */
4185 for (i = 0; i < n_baseclasses; ++i)
4187 tree base_binfo;
4189 base_binfo = BINFO_BASE_BINFO (TYPE_BINFO (t), i);
4191 /* The primary base was already allocated above, so we don't
4192 need to allocate it again here. */
4193 if (base_binfo == CLASSTYPE_PRIMARY_BINFO (t))
4194 continue;
4196 /* Virtual bases are added at the end (a primary virtual base
4197 will have already been added). */
4198 if (BINFO_VIRTUAL_P (base_binfo))
4199 continue;
4201 next_field = build_base_field (rli, base_binfo,
4202 offsets, next_field);
4206 /* Go through the TYPE_METHODS of T issuing any appropriate
4207 diagnostics, figuring out which methods override which other
4208 methods, and so forth. */
4210 static void
4211 check_methods (tree t)
4213 tree x;
4215 for (x = TYPE_METHODS (t); x; x = DECL_CHAIN (x))
4217 check_for_override (x, t);
4218 if (DECL_PURE_VIRTUAL_P (x) && ! DECL_VINDEX (x))
4219 error ("initializer specified for non-virtual method %q+D", x);
4220 /* The name of the field is the original field name
4221 Save this in auxiliary field for later overloading. */
4222 if (DECL_VINDEX (x))
4224 TYPE_POLYMORPHIC_P (t) = 1;
4225 if (DECL_PURE_VIRTUAL_P (x))
4226 vec_safe_push (CLASSTYPE_PURE_VIRTUALS (t), x);
4228 /* All user-provided destructors are non-trivial.
4229 Constructors and assignment ops are handled in
4230 grok_special_member_properties. */
4231 if (DECL_DESTRUCTOR_P (x) && user_provided_p (x))
4232 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t) = 1;
4236 /* FN is a constructor or destructor. Clone the declaration to create
4237 a specialized in-charge or not-in-charge version, as indicated by
4238 NAME. */
4240 static tree
4241 build_clone (tree fn, tree name)
4243 tree parms;
4244 tree clone;
4246 /* Copy the function. */
4247 clone = copy_decl (fn);
4248 /* Reset the function name. */
4249 DECL_NAME (clone) = name;
4250 SET_DECL_ASSEMBLER_NAME (clone, NULL_TREE);
4251 /* Remember where this function came from. */
4252 DECL_ABSTRACT_ORIGIN (clone) = fn;
4253 /* Make it easy to find the CLONE given the FN. */
4254 DECL_CHAIN (clone) = DECL_CHAIN (fn);
4255 DECL_CHAIN (fn) = clone;
4257 /* If this is a template, do the rest on the DECL_TEMPLATE_RESULT. */
4258 if (TREE_CODE (clone) == TEMPLATE_DECL)
4260 tree result = build_clone (DECL_TEMPLATE_RESULT (clone), name);
4261 DECL_TEMPLATE_RESULT (clone) = result;
4262 DECL_TEMPLATE_INFO (result) = copy_node (DECL_TEMPLATE_INFO (result));
4263 DECL_TI_TEMPLATE (result) = clone;
4264 TREE_TYPE (clone) = TREE_TYPE (result);
4265 return clone;
4268 DECL_CLONED_FUNCTION (clone) = fn;
4269 /* There's no pending inline data for this function. */
4270 DECL_PENDING_INLINE_INFO (clone) = NULL;
4271 DECL_PENDING_INLINE_P (clone) = 0;
4273 /* The base-class destructor is not virtual. */
4274 if (name == base_dtor_identifier)
4276 DECL_VIRTUAL_P (clone) = 0;
4277 if (TREE_CODE (clone) != TEMPLATE_DECL)
4278 DECL_VINDEX (clone) = NULL_TREE;
4281 /* If there was an in-charge parameter, drop it from the function
4282 type. */
4283 if (DECL_HAS_IN_CHARGE_PARM_P (clone))
4285 tree basetype;
4286 tree parmtypes;
4287 tree exceptions;
4289 exceptions = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (clone));
4290 basetype = TYPE_METHOD_BASETYPE (TREE_TYPE (clone));
4291 parmtypes = TYPE_ARG_TYPES (TREE_TYPE (clone));
4292 /* Skip the `this' parameter. */
4293 parmtypes = TREE_CHAIN (parmtypes);
4294 /* Skip the in-charge parameter. */
4295 parmtypes = TREE_CHAIN (parmtypes);
4296 /* And the VTT parm, in a complete [cd]tor. */
4297 if (DECL_HAS_VTT_PARM_P (fn)
4298 && ! DECL_NEEDS_VTT_PARM_P (clone))
4299 parmtypes = TREE_CHAIN (parmtypes);
4300 /* If this is subobject constructor or destructor, add the vtt
4301 parameter. */
4302 TREE_TYPE (clone)
4303 = build_method_type_directly (basetype,
4304 TREE_TYPE (TREE_TYPE (clone)),
4305 parmtypes);
4306 if (exceptions)
4307 TREE_TYPE (clone) = build_exception_variant (TREE_TYPE (clone),
4308 exceptions);
4309 TREE_TYPE (clone)
4310 = cp_build_type_attribute_variant (TREE_TYPE (clone),
4311 TYPE_ATTRIBUTES (TREE_TYPE (fn)));
4314 /* Copy the function parameters. */
4315 DECL_ARGUMENTS (clone) = copy_list (DECL_ARGUMENTS (clone));
4316 /* Remove the in-charge parameter. */
4317 if (DECL_HAS_IN_CHARGE_PARM_P (clone))
4319 DECL_CHAIN (DECL_ARGUMENTS (clone))
4320 = DECL_CHAIN (DECL_CHAIN (DECL_ARGUMENTS (clone)));
4321 DECL_HAS_IN_CHARGE_PARM_P (clone) = 0;
4323 /* And the VTT parm, in a complete [cd]tor. */
4324 if (DECL_HAS_VTT_PARM_P (fn))
4326 if (DECL_NEEDS_VTT_PARM_P (clone))
4327 DECL_HAS_VTT_PARM_P (clone) = 1;
4328 else
4330 DECL_CHAIN (DECL_ARGUMENTS (clone))
4331 = DECL_CHAIN (DECL_CHAIN (DECL_ARGUMENTS (clone)));
4332 DECL_HAS_VTT_PARM_P (clone) = 0;
4336 for (parms = DECL_ARGUMENTS (clone); parms; parms = DECL_CHAIN (parms))
4338 DECL_CONTEXT (parms) = clone;
4339 cxx_dup_lang_specific_decl (parms);
4342 /* Create the RTL for this function. */
4343 SET_DECL_RTL (clone, NULL);
4344 rest_of_decl_compilation (clone, /*top_level=*/1, at_eof);
4346 if (pch_file)
4347 note_decl_for_pch (clone);
4349 return clone;
4352 /* Implementation of DECL_CLONED_FUNCTION and DECL_CLONED_FUNCTION_P, do
4353 not invoke this function directly.
4355 For a non-thunk function, returns the address of the slot for storing
4356 the function it is a clone of. Otherwise returns NULL_TREE.
4358 If JUST_TESTING, looks through TEMPLATE_DECL and returns NULL if
4359 cloned_function is unset. This is to support the separate
4360 DECL_CLONED_FUNCTION and DECL_CLONED_FUNCTION_P modes; using the latter
4361 on a template makes sense, but not the former. */
4363 tree *
4364 decl_cloned_function_p (const_tree decl, bool just_testing)
4366 tree *ptr;
4367 if (just_testing)
4368 decl = STRIP_TEMPLATE (decl);
4370 if (TREE_CODE (decl) != FUNCTION_DECL
4371 || !DECL_LANG_SPECIFIC (decl)
4372 || DECL_LANG_SPECIFIC (decl)->u.fn.thunk_p)
4374 #if defined ENABLE_TREE_CHECKING && (GCC_VERSION >= 2007)
4375 if (!just_testing)
4376 lang_check_failed (__FILE__, __LINE__, __FUNCTION__);
4377 else
4378 #endif
4379 return NULL;
4382 ptr = &DECL_LANG_SPECIFIC (decl)->u.fn.u5.cloned_function;
4383 if (just_testing && *ptr == NULL_TREE)
4384 return NULL;
4385 else
4386 return ptr;
4389 /* Produce declarations for all appropriate clones of FN. If
4390 UPDATE_METHOD_VEC_P is nonzero, the clones are added to the
4391 CLASTYPE_METHOD_VEC as well. */
4393 void
4394 clone_function_decl (tree fn, int update_method_vec_p)
4396 tree clone;
4398 /* Avoid inappropriate cloning. */
4399 if (DECL_CHAIN (fn)
4400 && DECL_CLONED_FUNCTION_P (DECL_CHAIN (fn)))
4401 return;
4403 if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (fn))
4405 /* For each constructor, we need two variants: an in-charge version
4406 and a not-in-charge version. */
4407 clone = build_clone (fn, complete_ctor_identifier);
4408 if (update_method_vec_p)
4409 add_method (DECL_CONTEXT (clone), clone, NULL_TREE);
4410 clone = build_clone (fn, base_ctor_identifier);
4411 if (update_method_vec_p)
4412 add_method (DECL_CONTEXT (clone), clone, NULL_TREE);
4414 else
4416 gcc_assert (DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (fn));
4418 /* For each destructor, we need three variants: an in-charge
4419 version, a not-in-charge version, and an in-charge deleting
4420 version. We clone the deleting version first because that
4421 means it will go second on the TYPE_METHODS list -- and that
4422 corresponds to the correct layout order in the virtual
4423 function table.
4425 For a non-virtual destructor, we do not build a deleting
4426 destructor. */
4427 if (DECL_VIRTUAL_P (fn))
4429 clone = build_clone (fn, deleting_dtor_identifier);
4430 if (update_method_vec_p)
4431 add_method (DECL_CONTEXT (clone), clone, NULL_TREE);
4433 clone = build_clone (fn, complete_dtor_identifier);
4434 if (update_method_vec_p)
4435 add_method (DECL_CONTEXT (clone), clone, NULL_TREE);
4436 clone = build_clone (fn, base_dtor_identifier);
4437 if (update_method_vec_p)
4438 add_method (DECL_CONTEXT (clone), clone, NULL_TREE);
4441 /* Note that this is an abstract function that is never emitted. */
4442 DECL_ABSTRACT (fn) = 1;
4445 /* DECL is an in charge constructor, which is being defined. This will
4446 have had an in class declaration, from whence clones were
4447 declared. An out-of-class definition can specify additional default
4448 arguments. As it is the clones that are involved in overload
4449 resolution, we must propagate the information from the DECL to its
4450 clones. */
4452 void
4453 adjust_clone_args (tree decl)
4455 tree clone;
4457 for (clone = DECL_CHAIN (decl); clone && DECL_CLONED_FUNCTION_P (clone);
4458 clone = DECL_CHAIN (clone))
4460 tree orig_clone_parms = TYPE_ARG_TYPES (TREE_TYPE (clone));
4461 tree orig_decl_parms = TYPE_ARG_TYPES (TREE_TYPE (decl));
4462 tree decl_parms, clone_parms;
4464 clone_parms = orig_clone_parms;
4466 /* Skip the 'this' parameter. */
4467 orig_clone_parms = TREE_CHAIN (orig_clone_parms);
4468 orig_decl_parms = TREE_CHAIN (orig_decl_parms);
4470 if (DECL_HAS_IN_CHARGE_PARM_P (decl))
4471 orig_decl_parms = TREE_CHAIN (orig_decl_parms);
4472 if (DECL_HAS_VTT_PARM_P (decl))
4473 orig_decl_parms = TREE_CHAIN (orig_decl_parms);
4475 clone_parms = orig_clone_parms;
4476 if (DECL_HAS_VTT_PARM_P (clone))
4477 clone_parms = TREE_CHAIN (clone_parms);
4479 for (decl_parms = orig_decl_parms; decl_parms;
4480 decl_parms = TREE_CHAIN (decl_parms),
4481 clone_parms = TREE_CHAIN (clone_parms))
4483 gcc_assert (same_type_p (TREE_TYPE (decl_parms),
4484 TREE_TYPE (clone_parms)));
4486 if (TREE_PURPOSE (decl_parms) && !TREE_PURPOSE (clone_parms))
4488 /* A default parameter has been added. Adjust the
4489 clone's parameters. */
4490 tree exceptions = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (clone));
4491 tree attrs = TYPE_ATTRIBUTES (TREE_TYPE (clone));
4492 tree basetype = TYPE_METHOD_BASETYPE (TREE_TYPE (clone));
4493 tree type;
4495 clone_parms = orig_decl_parms;
4497 if (DECL_HAS_VTT_PARM_P (clone))
4499 clone_parms = tree_cons (TREE_PURPOSE (orig_clone_parms),
4500 TREE_VALUE (orig_clone_parms),
4501 clone_parms);
4502 TREE_TYPE (clone_parms) = TREE_TYPE (orig_clone_parms);
4504 type = build_method_type_directly (basetype,
4505 TREE_TYPE (TREE_TYPE (clone)),
4506 clone_parms);
4507 if (exceptions)
4508 type = build_exception_variant (type, exceptions);
4509 if (attrs)
4510 type = cp_build_type_attribute_variant (type, attrs);
4511 TREE_TYPE (clone) = type;
4513 clone_parms = NULL_TREE;
4514 break;
4517 gcc_assert (!clone_parms);
4521 /* For each of the constructors and destructors in T, create an
4522 in-charge and not-in-charge variant. */
4524 static void
4525 clone_constructors_and_destructors (tree t)
4527 tree fns;
4529 /* If for some reason we don't have a CLASSTYPE_METHOD_VEC, we bail
4530 out now. */
4531 if (!CLASSTYPE_METHOD_VEC (t))
4532 return;
4534 for (fns = CLASSTYPE_CONSTRUCTORS (t); fns; fns = OVL_NEXT (fns))
4535 clone_function_decl (OVL_CURRENT (fns), /*update_method_vec_p=*/1);
4536 for (fns = CLASSTYPE_DESTRUCTORS (t); fns; fns = OVL_NEXT (fns))
4537 clone_function_decl (OVL_CURRENT (fns), /*update_method_vec_p=*/1);
4540 /* Deduce noexcept for a destructor DTOR. */
4542 void
4543 deduce_noexcept_on_destructor (tree dtor)
4545 if (!TYPE_RAISES_EXCEPTIONS (TREE_TYPE (dtor)))
4547 tree ctx = DECL_CONTEXT (dtor);
4548 tree implicit_fn = implicitly_declare_fn (sfk_destructor, ctx,
4549 /*const_p=*/false,
4550 NULL, NULL);
4551 tree eh_spec = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (implicit_fn));
4552 TREE_TYPE (dtor) = build_exception_variant (TREE_TYPE (dtor), eh_spec);
4556 /* For each destructor in T, deduce noexcept:
4558 12.4/3: A declaration of a destructor that does not have an
4559 exception-specification is implicitly considered to have the
4560 same exception-specification as an implicit declaration (15.4). */
4562 static void
4563 deduce_noexcept_on_destructors (tree t)
4565 tree fns;
4567 /* If for some reason we don't have a CLASSTYPE_METHOD_VEC, we bail
4568 out now. */
4569 if (!CLASSTYPE_METHOD_VEC (t))
4570 return;
4572 for (fns = CLASSTYPE_DESTRUCTORS (t); fns; fns = OVL_NEXT (fns))
4573 deduce_noexcept_on_destructor (OVL_CURRENT (fns));
4576 /* Subroutine of set_one_vmethod_tm_attributes. Search base classes
4577 of TYPE for virtual functions which FNDECL overrides. Return a
4578 mask of the tm attributes found therein. */
4580 static int
4581 look_for_tm_attr_overrides (tree type, tree fndecl)
4583 tree binfo = TYPE_BINFO (type);
4584 tree base_binfo;
4585 int ix, found = 0;
4587 for (ix = 0; BINFO_BASE_ITERATE (binfo, ix, base_binfo); ++ix)
4589 tree o, basetype = BINFO_TYPE (base_binfo);
4591 if (!TYPE_POLYMORPHIC_P (basetype))
4592 continue;
4594 o = look_for_overrides_here (basetype, fndecl);
4595 if (o)
4596 found |= tm_attr_to_mask (find_tm_attribute
4597 (TYPE_ATTRIBUTES (TREE_TYPE (o))));
4598 else
4599 found |= look_for_tm_attr_overrides (basetype, fndecl);
4602 return found;
4605 /* Subroutine of set_method_tm_attributes. Handle the checks and
4606 inheritance for one virtual method FNDECL. */
4608 static void
4609 set_one_vmethod_tm_attributes (tree type, tree fndecl)
4611 tree tm_attr;
4612 int found, have;
4614 found = look_for_tm_attr_overrides (type, fndecl);
4616 /* If FNDECL doesn't actually override anything (i.e. T is the
4617 class that first declares FNDECL virtual), then we're done. */
4618 if (found == 0)
4619 return;
4621 tm_attr = find_tm_attribute (TYPE_ATTRIBUTES (TREE_TYPE (fndecl)));
4622 have = tm_attr_to_mask (tm_attr);
4624 /* Intel STM Language Extension 3.0, Section 4.2 table 4:
4625 tm_pure must match exactly, otherwise no weakening of
4626 tm_safe > tm_callable > nothing. */
4627 /* ??? The tm_pure attribute didn't make the transition to the
4628 multivendor language spec. */
4629 if (have == TM_ATTR_PURE)
4631 if (found != TM_ATTR_PURE)
4633 found &= -found;
4634 goto err_override;
4637 /* If the overridden function is tm_pure, then FNDECL must be. */
4638 else if (found == TM_ATTR_PURE && tm_attr)
4639 goto err_override;
4640 /* Look for base class combinations that cannot be satisfied. */
4641 else if (found != TM_ATTR_PURE && (found & TM_ATTR_PURE))
4643 found &= ~TM_ATTR_PURE;
4644 found &= -found;
4645 error_at (DECL_SOURCE_LOCATION (fndecl),
4646 "method overrides both %<transaction_pure%> and %qE methods",
4647 tm_mask_to_attr (found));
4649 /* If FNDECL did not declare an attribute, then inherit the most
4650 restrictive one. */
4651 else if (tm_attr == NULL)
4653 apply_tm_attr (fndecl, tm_mask_to_attr (found & -found));
4655 /* Otherwise validate that we're not weaker than a function
4656 that is being overridden. */
4657 else
4659 found &= -found;
4660 if (found <= TM_ATTR_CALLABLE && have > found)
4661 goto err_override;
4663 return;
4665 err_override:
4666 error_at (DECL_SOURCE_LOCATION (fndecl),
4667 "method declared %qE overriding %qE method",
4668 tm_attr, tm_mask_to_attr (found));
4671 /* For each of the methods in T, propagate a class-level tm attribute. */
4673 static void
4674 set_method_tm_attributes (tree t)
4676 tree class_tm_attr, fndecl;
4678 /* Don't bother collecting tm attributes if transactional memory
4679 support is not enabled. */
4680 if (!flag_tm)
4681 return;
4683 /* Process virtual methods first, as they inherit directly from the
4684 base virtual function and also require validation of new attributes. */
4685 if (TYPE_CONTAINS_VPTR_P (t))
4687 tree vchain;
4688 for (vchain = BINFO_VIRTUALS (TYPE_BINFO (t)); vchain;
4689 vchain = TREE_CHAIN (vchain))
4691 fndecl = BV_FN (vchain);
4692 if (DECL_THUNK_P (fndecl))
4693 fndecl = THUNK_TARGET (fndecl);
4694 set_one_vmethod_tm_attributes (t, fndecl);
4698 /* If the class doesn't have an attribute, nothing more to do. */
4699 class_tm_attr = find_tm_attribute (TYPE_ATTRIBUTES (t));
4700 if (class_tm_attr == NULL)
4701 return;
4703 /* Any method that does not yet have a tm attribute inherits
4704 the one from the class. */
4705 for (fndecl = TYPE_METHODS (t); fndecl; fndecl = TREE_CHAIN (fndecl))
4707 if (!find_tm_attribute (TYPE_ATTRIBUTES (TREE_TYPE (fndecl))))
4708 apply_tm_attr (fndecl, class_tm_attr);
4712 /* Returns true iff class T has a user-defined constructor other than
4713 the default constructor. */
4715 bool
4716 type_has_user_nondefault_constructor (tree t)
4718 tree fns;
4720 if (!TYPE_HAS_USER_CONSTRUCTOR (t))
4721 return false;
4723 for (fns = CLASSTYPE_CONSTRUCTORS (t); fns; fns = OVL_NEXT (fns))
4725 tree fn = OVL_CURRENT (fns);
4726 if (!DECL_ARTIFICIAL (fn)
4727 && (TREE_CODE (fn) == TEMPLATE_DECL
4728 || (skip_artificial_parms_for (fn, DECL_ARGUMENTS (fn))
4729 != NULL_TREE)))
4730 return true;
4733 return false;
4736 /* Returns the defaulted constructor if T has one. Otherwise, returns
4737 NULL_TREE. */
4739 tree
4740 in_class_defaulted_default_constructor (tree t)
4742 tree fns, args;
4744 if (!TYPE_HAS_USER_CONSTRUCTOR (t))
4745 return NULL_TREE;
4747 for (fns = CLASSTYPE_CONSTRUCTORS (t); fns; fns = OVL_NEXT (fns))
4749 tree fn = OVL_CURRENT (fns);
4751 if (DECL_DEFAULTED_IN_CLASS_P (fn))
4753 args = FUNCTION_FIRST_USER_PARMTYPE (fn);
4754 while (args && TREE_PURPOSE (args))
4755 args = TREE_CHAIN (args);
4756 if (!args || args == void_list_node)
4757 return fn;
4761 return NULL_TREE;
4764 /* Returns true iff FN is a user-provided function, i.e. user-declared
4765 and not defaulted at its first declaration; or explicit, private,
4766 protected, or non-const. */
4768 bool
4769 user_provided_p (tree fn)
4771 if (TREE_CODE (fn) == TEMPLATE_DECL)
4772 return true;
4773 else
4774 return (!DECL_ARTIFICIAL (fn)
4775 && !DECL_DEFAULTED_IN_CLASS_P (fn));
4778 /* Returns true iff class T has a user-provided constructor. */
4780 bool
4781 type_has_user_provided_constructor (tree t)
4783 tree fns;
4785 if (!CLASS_TYPE_P (t))
4786 return false;
4788 if (!TYPE_HAS_USER_CONSTRUCTOR (t))
4789 return false;
4791 /* This can happen in error cases; avoid crashing. */
4792 if (!CLASSTYPE_METHOD_VEC (t))
4793 return false;
4795 for (fns = CLASSTYPE_CONSTRUCTORS (t); fns; fns = OVL_NEXT (fns))
4796 if (user_provided_p (OVL_CURRENT (fns)))
4797 return true;
4799 return false;
4802 /* Returns true iff class T has a user-provided default constructor. */
4804 bool
4805 type_has_user_provided_default_constructor (tree t)
4807 tree fns;
4809 if (!TYPE_HAS_USER_CONSTRUCTOR (t))
4810 return false;
4812 for (fns = CLASSTYPE_CONSTRUCTORS (t); fns; fns = OVL_NEXT (fns))
4814 tree fn = OVL_CURRENT (fns);
4815 if (TREE_CODE (fn) == FUNCTION_DECL
4816 && user_provided_p (fn)
4817 && sufficient_parms_p (FUNCTION_FIRST_USER_PARMTYPE (fn)))
4818 return true;
4821 return false;
4824 /* If default-initialization leaves part of TYPE uninitialized, returns
4825 a DECL for the field or TYPE itself (DR 253). */
4827 tree
4828 default_init_uninitialized_part (tree type)
4830 tree t, r, binfo;
4831 int i;
4833 type = strip_array_types (type);
4834 if (!CLASS_TYPE_P (type))
4835 return type;
4836 if (type_has_user_provided_default_constructor (type))
4837 return NULL_TREE;
4838 for (binfo = TYPE_BINFO (type), i = 0;
4839 BINFO_BASE_ITERATE (binfo, i, t); ++i)
4841 r = default_init_uninitialized_part (BINFO_TYPE (t));
4842 if (r)
4843 return r;
4845 for (t = TYPE_FIELDS (type); t; t = DECL_CHAIN (t))
4846 if (TREE_CODE (t) == FIELD_DECL
4847 && !DECL_ARTIFICIAL (t)
4848 && !DECL_INITIAL (t))
4850 r = default_init_uninitialized_part (TREE_TYPE (t));
4851 if (r)
4852 return DECL_P (r) ? r : t;
4855 return NULL_TREE;
4858 /* Returns true iff for class T, a trivial synthesized default constructor
4859 would be constexpr. */
4861 bool
4862 trivial_default_constructor_is_constexpr (tree t)
4864 /* A defaulted trivial default constructor is constexpr
4865 if there is nothing to initialize. */
4866 gcc_assert (!TYPE_HAS_COMPLEX_DFLT (t));
4867 return is_really_empty_class (t);
4870 /* Returns true iff class T has a constexpr default constructor. */
4872 bool
4873 type_has_constexpr_default_constructor (tree t)
4875 tree fns;
4877 if (!CLASS_TYPE_P (t))
4879 /* The caller should have stripped an enclosing array. */
4880 gcc_assert (TREE_CODE (t) != ARRAY_TYPE);
4881 return false;
4883 if (CLASSTYPE_LAZY_DEFAULT_CTOR (t))
4885 if (!TYPE_HAS_COMPLEX_DFLT (t))
4886 return trivial_default_constructor_is_constexpr (t);
4887 /* Non-trivial, we need to check subobject constructors. */
4888 lazily_declare_fn (sfk_constructor, t);
4890 fns = locate_ctor (t);
4891 return (fns && DECL_DECLARED_CONSTEXPR_P (fns));
4894 /* Returns true iff class TYPE has a virtual destructor. */
4896 bool
4897 type_has_virtual_destructor (tree type)
4899 tree dtor;
4901 if (!CLASS_TYPE_P (type))
4902 return false;
4904 gcc_assert (COMPLETE_TYPE_P (type));
4905 dtor = CLASSTYPE_DESTRUCTORS (type);
4906 return (dtor && DECL_VIRTUAL_P (dtor));
4909 /* Returns true iff class T has a move constructor. */
4911 bool
4912 type_has_move_constructor (tree t)
4914 tree fns;
4916 if (CLASSTYPE_LAZY_MOVE_CTOR (t))
4918 gcc_assert (COMPLETE_TYPE_P (t));
4919 lazily_declare_fn (sfk_move_constructor, t);
4922 if (!CLASSTYPE_METHOD_VEC (t))
4923 return false;
4925 for (fns = CLASSTYPE_CONSTRUCTORS (t); fns; fns = OVL_NEXT (fns))
4926 if (move_fn_p (OVL_CURRENT (fns)))
4927 return true;
4929 return false;
4932 /* Returns true iff class T has a move assignment operator. */
4934 bool
4935 type_has_move_assign (tree t)
4937 tree fns;
4939 if (CLASSTYPE_LAZY_MOVE_ASSIGN (t))
4941 gcc_assert (COMPLETE_TYPE_P (t));
4942 lazily_declare_fn (sfk_move_assignment, t);
4945 for (fns = lookup_fnfields_slot_nolazy (t, ansi_assopname (NOP_EXPR));
4946 fns; fns = OVL_NEXT (fns))
4947 if (move_fn_p (OVL_CURRENT (fns)))
4948 return true;
4950 return false;
4953 /* Returns true iff class T has a move constructor that was explicitly
4954 declared in the class body. Note that this is different from
4955 "user-provided", which doesn't include functions that are defaulted in
4956 the class. */
4958 bool
4959 type_has_user_declared_move_constructor (tree t)
4961 tree fns;
4963 if (CLASSTYPE_LAZY_MOVE_CTOR (t))
4964 return false;
4966 if (!CLASSTYPE_METHOD_VEC (t))
4967 return false;
4969 for (fns = CLASSTYPE_CONSTRUCTORS (t); fns; fns = OVL_NEXT (fns))
4971 tree fn = OVL_CURRENT (fns);
4972 if (move_fn_p (fn) && !DECL_ARTIFICIAL (fn))
4973 return true;
4976 return false;
4979 /* Returns true iff class T has a move assignment operator that was
4980 explicitly declared in the class body. */
4982 bool
4983 type_has_user_declared_move_assign (tree t)
4985 tree fns;
4987 if (CLASSTYPE_LAZY_MOVE_ASSIGN (t))
4988 return false;
4990 for (fns = lookup_fnfields_slot_nolazy (t, ansi_assopname (NOP_EXPR));
4991 fns; fns = OVL_NEXT (fns))
4993 tree fn = OVL_CURRENT (fns);
4994 if (move_fn_p (fn) && !DECL_ARTIFICIAL (fn))
4995 return true;
4998 return false;
5001 /* Nonzero if we need to build up a constructor call when initializing an
5002 object of this class, either because it has a user-provided constructor
5003 or because it doesn't have a default constructor (so we need to give an
5004 error if no initializer is provided). Use TYPE_NEEDS_CONSTRUCTING when
5005 what you care about is whether or not an object can be produced by a
5006 constructor (e.g. so we don't set TREE_READONLY on const variables of
5007 such type); use this function when what you care about is whether or not
5008 to try to call a constructor to create an object. The latter case is
5009 the former plus some cases of constructors that cannot be called. */
5011 bool
5012 type_build_ctor_call (tree t)
5014 tree inner;
5015 if (TYPE_NEEDS_CONSTRUCTING (t))
5016 return true;
5017 inner = strip_array_types (t);
5018 return (CLASS_TYPE_P (inner) && !TYPE_HAS_DEFAULT_CONSTRUCTOR (inner)
5019 && !ANON_AGGR_TYPE_P (inner));
5022 /* Remove all zero-width bit-fields from T. */
5024 static void
5025 remove_zero_width_bit_fields (tree t)
5027 tree *fieldsp;
5029 fieldsp = &TYPE_FIELDS (t);
5030 while (*fieldsp)
5032 if (TREE_CODE (*fieldsp) == FIELD_DECL
5033 && DECL_C_BIT_FIELD (*fieldsp)
5034 /* We should not be confused by the fact that grokbitfield
5035 temporarily sets the width of the bit field into
5036 DECL_INITIAL (*fieldsp).
5037 check_bitfield_decl eventually sets DECL_SIZE (*fieldsp)
5038 to that width. */
5039 && integer_zerop (DECL_SIZE (*fieldsp)))
5040 *fieldsp = DECL_CHAIN (*fieldsp);
5041 else
5042 fieldsp = &DECL_CHAIN (*fieldsp);
5046 /* Returns TRUE iff we need a cookie when dynamically allocating an
5047 array whose elements have the indicated class TYPE. */
5049 static bool
5050 type_requires_array_cookie (tree type)
5052 tree fns;
5053 bool has_two_argument_delete_p = false;
5055 gcc_assert (CLASS_TYPE_P (type));
5057 /* If there's a non-trivial destructor, we need a cookie. In order
5058 to iterate through the array calling the destructor for each
5059 element, we'll have to know how many elements there are. */
5060 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
5061 return true;
5063 /* If the usual deallocation function is a two-argument whose second
5064 argument is of type `size_t', then we have to pass the size of
5065 the array to the deallocation function, so we will need to store
5066 a cookie. */
5067 fns = lookup_fnfields (TYPE_BINFO (type),
5068 ansi_opname (VEC_DELETE_EXPR),
5069 /*protect=*/0);
5070 /* If there are no `operator []' members, or the lookup is
5071 ambiguous, then we don't need a cookie. */
5072 if (!fns || fns == error_mark_node)
5073 return false;
5074 /* Loop through all of the functions. */
5075 for (fns = BASELINK_FUNCTIONS (fns); fns; fns = OVL_NEXT (fns))
5077 tree fn;
5078 tree second_parm;
5080 /* Select the current function. */
5081 fn = OVL_CURRENT (fns);
5082 /* See if this function is a one-argument delete function. If
5083 it is, then it will be the usual deallocation function. */
5084 second_parm = TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (fn)));
5085 if (second_parm == void_list_node)
5086 return false;
5087 /* Do not consider this function if its second argument is an
5088 ellipsis. */
5089 if (!second_parm)
5090 continue;
5091 /* Otherwise, if we have a two-argument function and the second
5092 argument is `size_t', it will be the usual deallocation
5093 function -- unless there is one-argument function, too. */
5094 if (TREE_CHAIN (second_parm) == void_list_node
5095 && same_type_p (TREE_VALUE (second_parm), size_type_node))
5096 has_two_argument_delete_p = true;
5099 return has_two_argument_delete_p;
5102 /* Finish computing the `literal type' property of class type T.
5104 At this point, we have already processed base classes and
5105 non-static data members. We need to check whether the copy
5106 constructor is trivial, the destructor is trivial, and there
5107 is a trivial default constructor or at least one constexpr
5108 constructor other than the copy constructor. */
5110 static void
5111 finalize_literal_type_property (tree t)
5113 tree fn;
5115 if (cxx_dialect < cxx0x
5116 || TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t))
5117 CLASSTYPE_LITERAL_P (t) = false;
5118 else if (CLASSTYPE_LITERAL_P (t) && !TYPE_HAS_TRIVIAL_DFLT (t)
5119 && CLASSTYPE_NON_AGGREGATE (t)
5120 && !TYPE_HAS_CONSTEXPR_CTOR (t))
5121 CLASSTYPE_LITERAL_P (t) = false;
5123 if (!CLASSTYPE_LITERAL_P (t))
5124 for (fn = TYPE_METHODS (t); fn; fn = DECL_CHAIN (fn))
5125 if (DECL_DECLARED_CONSTEXPR_P (fn)
5126 && TREE_CODE (fn) != TEMPLATE_DECL
5127 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
5128 && !DECL_CONSTRUCTOR_P (fn))
5130 DECL_DECLARED_CONSTEXPR_P (fn) = false;
5131 if (!DECL_GENERATED_P (fn))
5133 error ("enclosing class of constexpr non-static member "
5134 "function %q+#D is not a literal type", fn);
5135 explain_non_literal_class (t);
5140 /* T is a non-literal type used in a context which requires a constant
5141 expression. Explain why it isn't literal. */
5143 void
5144 explain_non_literal_class (tree t)
5146 static struct pointer_set_t *diagnosed;
5148 if (!CLASS_TYPE_P (t))
5149 return;
5150 t = TYPE_MAIN_VARIANT (t);
5152 if (diagnosed == NULL)
5153 diagnosed = pointer_set_create ();
5154 if (pointer_set_insert (diagnosed, t) != 0)
5155 /* Already explained. */
5156 return;
5158 inform (0, "%q+T is not literal because:", t);
5159 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t))
5160 inform (0, " %q+T has a non-trivial destructor", t);
5161 else if (CLASSTYPE_NON_AGGREGATE (t)
5162 && !TYPE_HAS_TRIVIAL_DFLT (t)
5163 && !TYPE_HAS_CONSTEXPR_CTOR (t))
5165 inform (0, " %q+T is not an aggregate, does not have a trivial "
5166 "default constructor, and has no constexpr constructor that "
5167 "is not a copy or move constructor", t);
5168 if (TYPE_HAS_DEFAULT_CONSTRUCTOR (t)
5169 && !type_has_user_provided_default_constructor (t))
5171 /* Note that we can't simply call locate_ctor because when the
5172 constructor is deleted it just returns NULL_TREE. */
5173 tree fns;
5174 for (fns = CLASSTYPE_CONSTRUCTORS (t); fns; fns = OVL_NEXT (fns))
5176 tree fn = OVL_CURRENT (fns);
5177 tree parms = TYPE_ARG_TYPES (TREE_TYPE (fn));
5179 parms = skip_artificial_parms_for (fn, parms);
5181 if (sufficient_parms_p (parms))
5183 if (DECL_DELETED_FN (fn))
5184 maybe_explain_implicit_delete (fn);
5185 else
5186 explain_invalid_constexpr_fn (fn);
5187 break;
5192 else
5194 tree binfo, base_binfo, field; int i;
5195 for (binfo = TYPE_BINFO (t), i = 0;
5196 BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
5198 tree basetype = TREE_TYPE (base_binfo);
5199 if (!CLASSTYPE_LITERAL_P (basetype))
5201 inform (0, " base class %qT of %q+T is non-literal",
5202 basetype, t);
5203 explain_non_literal_class (basetype);
5204 return;
5207 for (field = TYPE_FIELDS (t); field; field = TREE_CHAIN (field))
5209 tree ftype;
5210 if (TREE_CODE (field) != FIELD_DECL)
5211 continue;
5212 ftype = TREE_TYPE (field);
5213 if (!literal_type_p (ftype))
5215 inform (0, " non-static data member %q+D has "
5216 "non-literal type", field);
5217 if (CLASS_TYPE_P (ftype))
5218 explain_non_literal_class (ftype);
5224 /* Check the validity of the bases and members declared in T. Add any
5225 implicitly-generated functions (like copy-constructors and
5226 assignment operators). Compute various flag bits (like
5227 CLASSTYPE_NON_LAYOUT_POD_T) for T. This routine works purely at the C++
5228 level: i.e., independently of the ABI in use. */
5230 static void
5231 check_bases_and_members (tree t)
5233 /* Nonzero if the implicitly generated copy constructor should take
5234 a non-const reference argument. */
5235 int cant_have_const_ctor;
5236 /* Nonzero if the implicitly generated assignment operator
5237 should take a non-const reference argument. */
5238 int no_const_asn_ref;
5239 tree access_decls;
5240 bool saved_complex_asn_ref;
5241 bool saved_nontrivial_dtor;
5242 tree fn;
5244 /* By default, we use const reference arguments and generate default
5245 constructors. */
5246 cant_have_const_ctor = 0;
5247 no_const_asn_ref = 0;
5249 /* Deduce noexcept on destructors. */
5250 if (cxx_dialect >= cxx0x)
5251 deduce_noexcept_on_destructors (t);
5253 /* Check all the base-classes. */
5254 check_bases (t, &cant_have_const_ctor,
5255 &no_const_asn_ref);
5257 /* Check all the method declarations. */
5258 check_methods (t);
5260 /* Save the initial values of these flags which only indicate whether
5261 or not the class has user-provided functions. As we analyze the
5262 bases and members we can set these flags for other reasons. */
5263 saved_complex_asn_ref = TYPE_HAS_COMPLEX_COPY_ASSIGN (t);
5264 saved_nontrivial_dtor = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t);
5266 /* Check all the data member declarations. We cannot call
5267 check_field_decls until we have called check_bases check_methods,
5268 as check_field_decls depends on TYPE_HAS_NONTRIVIAL_DESTRUCTOR
5269 being set appropriately. */
5270 check_field_decls (t, &access_decls,
5271 &cant_have_const_ctor,
5272 &no_const_asn_ref);
5274 /* A nearly-empty class has to be vptr-containing; a nearly empty
5275 class contains just a vptr. */
5276 if (!TYPE_CONTAINS_VPTR_P (t))
5277 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
5279 /* Do some bookkeeping that will guide the generation of implicitly
5280 declared member functions. */
5281 TYPE_HAS_COMPLEX_COPY_CTOR (t) |= TYPE_CONTAINS_VPTR_P (t);
5282 TYPE_HAS_COMPLEX_MOVE_CTOR (t) |= TYPE_CONTAINS_VPTR_P (t);
5283 /* We need to call a constructor for this class if it has a
5284 user-provided constructor, or if the default constructor is going
5285 to initialize the vptr. (This is not an if-and-only-if;
5286 TYPE_NEEDS_CONSTRUCTING is set elsewhere if bases or members
5287 themselves need constructing.) */
5288 TYPE_NEEDS_CONSTRUCTING (t)
5289 |= (type_has_user_provided_constructor (t) || TYPE_CONTAINS_VPTR_P (t));
5290 /* [dcl.init.aggr]
5292 An aggregate is an array or a class with no user-provided
5293 constructors ... and no virtual functions.
5295 Again, other conditions for being an aggregate are checked
5296 elsewhere. */
5297 CLASSTYPE_NON_AGGREGATE (t)
5298 |= (type_has_user_provided_constructor (t) || TYPE_POLYMORPHIC_P (t));
5299 /* This is the C++98/03 definition of POD; it changed in C++0x, but we
5300 retain the old definition internally for ABI reasons. */
5301 CLASSTYPE_NON_LAYOUT_POD_P (t)
5302 |= (CLASSTYPE_NON_AGGREGATE (t)
5303 || saved_nontrivial_dtor || saved_complex_asn_ref);
5304 CLASSTYPE_NON_STD_LAYOUT (t) |= TYPE_CONTAINS_VPTR_P (t);
5305 TYPE_HAS_COMPLEX_COPY_ASSIGN (t) |= TYPE_CONTAINS_VPTR_P (t);
5306 TYPE_HAS_COMPLEX_MOVE_ASSIGN (t) |= TYPE_CONTAINS_VPTR_P (t);
5307 TYPE_HAS_COMPLEX_DFLT (t) |= TYPE_CONTAINS_VPTR_P (t);
5309 /* If the class has no user-declared constructor, but does have
5310 non-static const or reference data members that can never be
5311 initialized, issue a warning. */
5312 if (warn_uninitialized
5313 /* Classes with user-declared constructors are presumed to
5314 initialize these members. */
5315 && !TYPE_HAS_USER_CONSTRUCTOR (t)
5316 /* Aggregates can be initialized with brace-enclosed
5317 initializers. */
5318 && CLASSTYPE_NON_AGGREGATE (t))
5320 tree field;
5322 for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
5324 tree type;
5326 if (TREE_CODE (field) != FIELD_DECL
5327 || DECL_INITIAL (field) != NULL_TREE)
5328 continue;
5330 type = TREE_TYPE (field);
5331 if (TREE_CODE (type) == REFERENCE_TYPE)
5332 warning (OPT_Wuninitialized, "non-static reference %q+#D "
5333 "in class without a constructor", field);
5334 else if (CP_TYPE_CONST_P (type)
5335 && (!CLASS_TYPE_P (type)
5336 || !TYPE_HAS_DEFAULT_CONSTRUCTOR (type)))
5337 warning (OPT_Wuninitialized, "non-static const member %q+#D "
5338 "in class without a constructor", field);
5342 /* Synthesize any needed methods. */
5343 add_implicitly_declared_members (t, &access_decls,
5344 cant_have_const_ctor,
5345 no_const_asn_ref);
5347 /* Check defaulted declarations here so we have cant_have_const_ctor
5348 and don't need to worry about clones. */
5349 for (fn = TYPE_METHODS (t); fn; fn = DECL_CHAIN (fn))
5350 if (!DECL_ARTIFICIAL (fn) && DECL_DEFAULTED_IN_CLASS_P (fn))
5352 int copy = copy_fn_p (fn);
5353 if (copy > 0)
5355 bool imp_const_p
5356 = (DECL_CONSTRUCTOR_P (fn) ? !cant_have_const_ctor
5357 : !no_const_asn_ref);
5358 bool fn_const_p = (copy == 2);
5360 if (fn_const_p && !imp_const_p)
5361 /* If the function is defaulted outside the class, we just
5362 give the synthesis error. */
5363 error ("%q+D declared to take const reference, but implicit "
5364 "declaration would take non-const", fn);
5366 defaulted_late_check (fn);
5369 if (LAMBDA_TYPE_P (t))
5371 /* "The closure type associated with a lambda-expression has a deleted
5372 default constructor and a deleted copy assignment operator." */
5373 TYPE_NEEDS_CONSTRUCTING (t) = 1;
5374 TYPE_HAS_COMPLEX_DFLT (t) = 1;
5375 TYPE_HAS_COMPLEX_COPY_ASSIGN (t) = 1;
5376 CLASSTYPE_LAZY_MOVE_ASSIGN (t) = 0;
5378 /* "This class type is not an aggregate." */
5379 CLASSTYPE_NON_AGGREGATE (t) = 1;
5382 /* Compute the 'literal type' property before we
5383 do anything with non-static member functions. */
5384 finalize_literal_type_property (t);
5386 /* Create the in-charge and not-in-charge variants of constructors
5387 and destructors. */
5388 clone_constructors_and_destructors (t);
5390 /* Process the using-declarations. */
5391 for (; access_decls; access_decls = TREE_CHAIN (access_decls))
5392 handle_using_decl (TREE_VALUE (access_decls), t);
5394 /* Build and sort the CLASSTYPE_METHOD_VEC. */
5395 finish_struct_methods (t);
5397 /* Figure out whether or not we will need a cookie when dynamically
5398 allocating an array of this type. */
5399 TYPE_LANG_SPECIFIC (t)->u.c.vec_new_uses_cookie
5400 = type_requires_array_cookie (t);
5403 /* If T needs a pointer to its virtual function table, set TYPE_VFIELD
5404 accordingly. If a new vfield was created (because T doesn't have a
5405 primary base class), then the newly created field is returned. It
5406 is not added to the TYPE_FIELDS list; it is the caller's
5407 responsibility to do that. Accumulate declared virtual functions
5408 on VIRTUALS_P. */
5410 static tree
5411 create_vtable_ptr (tree t, tree* virtuals_p)
5413 tree fn;
5415 /* Collect the virtual functions declared in T. */
5416 for (fn = TYPE_METHODS (t); fn; fn = DECL_CHAIN (fn))
5417 if (DECL_VINDEX (fn) && !DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (fn)
5418 && TREE_CODE (DECL_VINDEX (fn)) != INTEGER_CST)
5420 tree new_virtual = make_node (TREE_LIST);
5422 BV_FN (new_virtual) = fn;
5423 BV_DELTA (new_virtual) = integer_zero_node;
5424 BV_VCALL_INDEX (new_virtual) = NULL_TREE;
5426 TREE_CHAIN (new_virtual) = *virtuals_p;
5427 *virtuals_p = new_virtual;
5430 /* If we couldn't find an appropriate base class, create a new field
5431 here. Even if there weren't any new virtual functions, we might need a
5432 new virtual function table if we're supposed to include vptrs in
5433 all classes that need them. */
5434 if (!TYPE_VFIELD (t) && (*virtuals_p || TYPE_CONTAINS_VPTR_P (t)))
5436 /* We build this decl with vtbl_ptr_type_node, which is a
5437 `vtable_entry_type*'. It might seem more precise to use
5438 `vtable_entry_type (*)[N]' where N is the number of virtual
5439 functions. However, that would require the vtable pointer in
5440 base classes to have a different type than the vtable pointer
5441 in derived classes. We could make that happen, but that
5442 still wouldn't solve all the problems. In particular, the
5443 type-based alias analysis code would decide that assignments
5444 to the base class vtable pointer can't alias assignments to
5445 the derived class vtable pointer, since they have different
5446 types. Thus, in a derived class destructor, where the base
5447 class constructor was inlined, we could generate bad code for
5448 setting up the vtable pointer.
5450 Therefore, we use one type for all vtable pointers. We still
5451 use a type-correct type; it's just doesn't indicate the array
5452 bounds. That's better than using `void*' or some such; it's
5453 cleaner, and it let's the alias analysis code know that these
5454 stores cannot alias stores to void*! */
5455 tree field;
5457 field = build_decl (input_location,
5458 FIELD_DECL, get_vfield_name (t), vtbl_ptr_type_node);
5459 DECL_VIRTUAL_P (field) = 1;
5460 DECL_ARTIFICIAL (field) = 1;
5461 DECL_FIELD_CONTEXT (field) = t;
5462 DECL_FCONTEXT (field) = t;
5463 if (TYPE_PACKED (t))
5464 DECL_PACKED (field) = 1;
5466 TYPE_VFIELD (t) = field;
5468 /* This class is non-empty. */
5469 CLASSTYPE_EMPTY_P (t) = 0;
5471 return field;
5474 return NULL_TREE;
5477 /* Add OFFSET to all base types of BINFO which is a base in the
5478 hierarchy dominated by T.
5480 OFFSET, which is a type offset, is number of bytes. */
5482 static void
5483 propagate_binfo_offsets (tree binfo, tree offset)
5485 int i;
5486 tree primary_binfo;
5487 tree base_binfo;
5489 /* Update BINFO's offset. */
5490 BINFO_OFFSET (binfo)
5491 = convert (sizetype,
5492 size_binop (PLUS_EXPR,
5493 convert (ssizetype, BINFO_OFFSET (binfo)),
5494 offset));
5496 /* Find the primary base class. */
5497 primary_binfo = get_primary_binfo (binfo);
5499 if (primary_binfo && BINFO_INHERITANCE_CHAIN (primary_binfo) == binfo)
5500 propagate_binfo_offsets (primary_binfo, offset);
5502 /* Scan all of the bases, pushing the BINFO_OFFSET adjust
5503 downwards. */
5504 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
5506 /* Don't do the primary base twice. */
5507 if (base_binfo == primary_binfo)
5508 continue;
5510 if (BINFO_VIRTUAL_P (base_binfo))
5511 continue;
5513 propagate_binfo_offsets (base_binfo, offset);
5517 /* Set BINFO_OFFSET for all of the virtual bases for RLI->T. Update
5518 TYPE_ALIGN and TYPE_SIZE for T. OFFSETS gives the location of
5519 empty subobjects of T. */
5521 static void
5522 layout_virtual_bases (record_layout_info rli, splay_tree offsets)
5524 tree vbase;
5525 tree t = rli->t;
5526 bool first_vbase = true;
5527 tree *next_field;
5529 if (BINFO_N_BASE_BINFOS (TYPE_BINFO (t)) == 0)
5530 return;
5532 if (!abi_version_at_least(2))
5534 /* In G++ 3.2, we incorrectly rounded the size before laying out
5535 the virtual bases. */
5536 finish_record_layout (rli, /*free_p=*/false);
5537 #ifdef STRUCTURE_SIZE_BOUNDARY
5538 /* Packed structures don't need to have minimum size. */
5539 if (! TYPE_PACKED (t))
5540 TYPE_ALIGN (t) = MAX (TYPE_ALIGN (t), (unsigned) STRUCTURE_SIZE_BOUNDARY);
5541 #endif
5542 rli->offset = TYPE_SIZE_UNIT (t);
5543 rli->bitpos = bitsize_zero_node;
5544 rli->record_align = TYPE_ALIGN (t);
5547 /* Find the last field. The artificial fields created for virtual
5548 bases will go after the last extant field to date. */
5549 next_field = &TYPE_FIELDS (t);
5550 while (*next_field)
5551 next_field = &DECL_CHAIN (*next_field);
5553 /* Go through the virtual bases, allocating space for each virtual
5554 base that is not already a primary base class. These are
5555 allocated in inheritance graph order. */
5556 for (vbase = TYPE_BINFO (t); vbase; vbase = TREE_CHAIN (vbase))
5558 if (!BINFO_VIRTUAL_P (vbase))
5559 continue;
5561 if (!BINFO_PRIMARY_P (vbase))
5563 tree basetype = TREE_TYPE (vbase);
5565 /* This virtual base is not a primary base of any class in the
5566 hierarchy, so we have to add space for it. */
5567 next_field = build_base_field (rli, vbase,
5568 offsets, next_field);
5570 /* If the first virtual base might have been placed at a
5571 lower address, had we started from CLASSTYPE_SIZE, rather
5572 than TYPE_SIZE, issue a warning. There can be both false
5573 positives and false negatives from this warning in rare
5574 cases; to deal with all the possibilities would probably
5575 require performing both layout algorithms and comparing
5576 the results which is not particularly tractable. */
5577 if (warn_abi
5578 && first_vbase
5579 && (tree_int_cst_lt
5580 (size_binop (CEIL_DIV_EXPR,
5581 round_up_loc (input_location,
5582 CLASSTYPE_SIZE (t),
5583 CLASSTYPE_ALIGN (basetype)),
5584 bitsize_unit_node),
5585 BINFO_OFFSET (vbase))))
5586 warning (OPT_Wabi,
5587 "offset of virtual base %qT is not ABI-compliant and "
5588 "may change in a future version of GCC",
5589 basetype);
5591 first_vbase = false;
5596 /* Returns the offset of the byte just past the end of the base class
5597 BINFO. */
5599 static tree
5600 end_of_base (tree binfo)
5602 tree size;
5604 if (!CLASSTYPE_AS_BASE (BINFO_TYPE (binfo)))
5605 size = TYPE_SIZE_UNIT (char_type_node);
5606 else if (is_empty_class (BINFO_TYPE (binfo)))
5607 /* An empty class has zero CLASSTYPE_SIZE_UNIT, but we need to
5608 allocate some space for it. It cannot have virtual bases, so
5609 TYPE_SIZE_UNIT is fine. */
5610 size = TYPE_SIZE_UNIT (BINFO_TYPE (binfo));
5611 else
5612 size = CLASSTYPE_SIZE_UNIT (BINFO_TYPE (binfo));
5614 return size_binop (PLUS_EXPR, BINFO_OFFSET (binfo), size);
5617 /* Returns the offset of the byte just past the end of the base class
5618 with the highest offset in T. If INCLUDE_VIRTUALS_P is zero, then
5619 only non-virtual bases are included. */
5621 static tree
5622 end_of_class (tree t, int include_virtuals_p)
5624 tree result = size_zero_node;
5625 vec<tree, va_gc> *vbases;
5626 tree binfo;
5627 tree base_binfo;
5628 tree offset;
5629 int i;
5631 for (binfo = TYPE_BINFO (t), i = 0;
5632 BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
5634 if (!include_virtuals_p
5635 && BINFO_VIRTUAL_P (base_binfo)
5636 && (!BINFO_PRIMARY_P (base_binfo)
5637 || BINFO_INHERITANCE_CHAIN (base_binfo) != TYPE_BINFO (t)))
5638 continue;
5640 offset = end_of_base (base_binfo);
5641 if (INT_CST_LT_UNSIGNED (result, offset))
5642 result = offset;
5645 /* G++ 3.2 did not check indirect virtual bases. */
5646 if (abi_version_at_least (2) && include_virtuals_p)
5647 for (vbases = CLASSTYPE_VBASECLASSES (t), i = 0;
5648 vec_safe_iterate (vbases, i, &base_binfo); i++)
5650 offset = end_of_base (base_binfo);
5651 if (INT_CST_LT_UNSIGNED (result, offset))
5652 result = offset;
5655 return result;
5658 /* Warn about bases of T that are inaccessible because they are
5659 ambiguous. For example:
5661 struct S {};
5662 struct T : public S {};
5663 struct U : public S, public T {};
5665 Here, `(S*) new U' is not allowed because there are two `S'
5666 subobjects of U. */
5668 static void
5669 warn_about_ambiguous_bases (tree t)
5671 int i;
5672 vec<tree, va_gc> *vbases;
5673 tree basetype;
5674 tree binfo;
5675 tree base_binfo;
5677 /* If there are no repeated bases, nothing can be ambiguous. */
5678 if (!CLASSTYPE_REPEATED_BASE_P (t))
5679 return;
5681 /* Check direct bases. */
5682 for (binfo = TYPE_BINFO (t), i = 0;
5683 BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
5685 basetype = BINFO_TYPE (base_binfo);
5687 if (!uniquely_derived_from_p (basetype, t))
5688 warning (0, "direct base %qT inaccessible in %qT due to ambiguity",
5689 basetype, t);
5692 /* Check for ambiguous virtual bases. */
5693 if (extra_warnings)
5694 for (vbases = CLASSTYPE_VBASECLASSES (t), i = 0;
5695 vec_safe_iterate (vbases, i, &binfo); i++)
5697 basetype = BINFO_TYPE (binfo);
5699 if (!uniquely_derived_from_p (basetype, t))
5700 warning (OPT_Wextra, "virtual base %qT inaccessible in %qT due "
5701 "to ambiguity", basetype, t);
5705 /* Compare two INTEGER_CSTs K1 and K2. */
5707 static int
5708 splay_tree_compare_integer_csts (splay_tree_key k1, splay_tree_key k2)
5710 return tree_int_cst_compare ((tree) k1, (tree) k2);
5713 /* Increase the size indicated in RLI to account for empty classes
5714 that are "off the end" of the class. */
5716 static void
5717 include_empty_classes (record_layout_info rli)
5719 tree eoc;
5720 tree rli_size;
5722 /* It might be the case that we grew the class to allocate a
5723 zero-sized base class. That won't be reflected in RLI, yet,
5724 because we are willing to overlay multiple bases at the same
5725 offset. However, now we need to make sure that RLI is big enough
5726 to reflect the entire class. */
5727 eoc = end_of_class (rli->t,
5728 CLASSTYPE_AS_BASE (rli->t) != NULL_TREE);
5729 rli_size = rli_size_unit_so_far (rli);
5730 if (TREE_CODE (rli_size) == INTEGER_CST
5731 && INT_CST_LT_UNSIGNED (rli_size, eoc))
5733 if (!abi_version_at_least (2))
5734 /* In version 1 of the ABI, the size of a class that ends with
5735 a bitfield was not rounded up to a whole multiple of a
5736 byte. Because rli_size_unit_so_far returns only the number
5737 of fully allocated bytes, any extra bits were not included
5738 in the size. */
5739 rli->bitpos = round_down (rli->bitpos, BITS_PER_UNIT);
5740 else
5741 /* The size should have been rounded to a whole byte. */
5742 gcc_assert (tree_int_cst_equal
5743 (rli->bitpos, round_down (rli->bitpos, BITS_PER_UNIT)));
5744 rli->bitpos
5745 = size_binop (PLUS_EXPR,
5746 rli->bitpos,
5747 size_binop (MULT_EXPR,
5748 convert (bitsizetype,
5749 size_binop (MINUS_EXPR,
5750 eoc, rli_size)),
5751 bitsize_int (BITS_PER_UNIT)));
5752 normalize_rli (rli);
5756 /* Calculate the TYPE_SIZE, TYPE_ALIGN, etc for T. Calculate
5757 BINFO_OFFSETs for all of the base-classes. Position the vtable
5758 pointer. Accumulate declared virtual functions on VIRTUALS_P. */
5760 static void
5761 layout_class_type (tree t, tree *virtuals_p)
5763 tree non_static_data_members;
5764 tree field;
5765 tree vptr;
5766 record_layout_info rli;
5767 /* Maps offsets (represented as INTEGER_CSTs) to a TREE_LIST of
5768 types that appear at that offset. */
5769 splay_tree empty_base_offsets;
5770 /* True if the last field layed out was a bit-field. */
5771 bool last_field_was_bitfield = false;
5772 /* The location at which the next field should be inserted. */
5773 tree *next_field;
5774 /* T, as a base class. */
5775 tree base_t;
5777 /* Keep track of the first non-static data member. */
5778 non_static_data_members = TYPE_FIELDS (t);
5780 /* Start laying out the record. */
5781 rli = start_record_layout (t);
5783 /* Mark all the primary bases in the hierarchy. */
5784 determine_primary_bases (t);
5786 /* Create a pointer to our virtual function table. */
5787 vptr = create_vtable_ptr (t, virtuals_p);
5789 /* The vptr is always the first thing in the class. */
5790 if (vptr)
5792 DECL_CHAIN (vptr) = TYPE_FIELDS (t);
5793 TYPE_FIELDS (t) = vptr;
5794 next_field = &DECL_CHAIN (vptr);
5795 place_field (rli, vptr);
5797 else
5798 next_field = &TYPE_FIELDS (t);
5800 /* Build FIELD_DECLs for all of the non-virtual base-types. */
5801 empty_base_offsets = splay_tree_new (splay_tree_compare_integer_csts,
5802 NULL, NULL);
5803 build_base_fields (rli, empty_base_offsets, next_field);
5805 /* Layout the non-static data members. */
5806 for (field = non_static_data_members; field; field = DECL_CHAIN (field))
5808 tree type;
5809 tree padding;
5811 /* We still pass things that aren't non-static data members to
5812 the back end, in case it wants to do something with them. */
5813 if (TREE_CODE (field) != FIELD_DECL)
5815 place_field (rli, field);
5816 /* If the static data member has incomplete type, keep track
5817 of it so that it can be completed later. (The handling
5818 of pending statics in finish_record_layout is
5819 insufficient; consider:
5821 struct S1;
5822 struct S2 { static S1 s1; };
5824 At this point, finish_record_layout will be called, but
5825 S1 is still incomplete.) */
5826 if (TREE_CODE (field) == VAR_DECL)
5828 maybe_register_incomplete_var (field);
5829 /* The visibility of static data members is determined
5830 at their point of declaration, not their point of
5831 definition. */
5832 determine_visibility (field);
5834 continue;
5837 type = TREE_TYPE (field);
5838 if (type == error_mark_node)
5839 continue;
5841 padding = NULL_TREE;
5843 /* If this field is a bit-field whose width is greater than its
5844 type, then there are some special rules for allocating
5845 it. */
5846 if (DECL_C_BIT_FIELD (field)
5847 && INT_CST_LT (TYPE_SIZE (type), DECL_SIZE (field)))
5849 unsigned int itk;
5850 tree integer_type;
5851 bool was_unnamed_p = false;
5852 /* We must allocate the bits as if suitably aligned for the
5853 longest integer type that fits in this many bits. type
5854 of the field. Then, we are supposed to use the left over
5855 bits as additional padding. */
5856 for (itk = itk_char; itk != itk_none; ++itk)
5857 if (integer_types[itk] != NULL_TREE
5858 && (INT_CST_LT (size_int (MAX_FIXED_MODE_SIZE),
5859 TYPE_SIZE (integer_types[itk]))
5860 || INT_CST_LT (DECL_SIZE (field),
5861 TYPE_SIZE (integer_types[itk]))))
5862 break;
5864 /* ITK now indicates a type that is too large for the
5865 field. We have to back up by one to find the largest
5866 type that fits. */
5869 --itk;
5870 integer_type = integer_types[itk];
5871 } while (itk > 0 && integer_type == NULL_TREE);
5873 /* Figure out how much additional padding is required. GCC
5874 3.2 always created a padding field, even if it had zero
5875 width. */
5876 if (!abi_version_at_least (2)
5877 || INT_CST_LT (TYPE_SIZE (integer_type), DECL_SIZE (field)))
5879 if (abi_version_at_least (2) && TREE_CODE (t) == UNION_TYPE)
5880 /* In a union, the padding field must have the full width
5881 of the bit-field; all fields start at offset zero. */
5882 padding = DECL_SIZE (field);
5883 else
5885 if (TREE_CODE (t) == UNION_TYPE)
5886 warning (OPT_Wabi, "size assigned to %qT may not be "
5887 "ABI-compliant and may change in a future "
5888 "version of GCC",
5890 padding = size_binop (MINUS_EXPR, DECL_SIZE (field),
5891 TYPE_SIZE (integer_type));
5894 #ifdef PCC_BITFIELD_TYPE_MATTERS
5895 /* An unnamed bitfield does not normally affect the
5896 alignment of the containing class on a target where
5897 PCC_BITFIELD_TYPE_MATTERS. But, the C++ ABI does not
5898 make any exceptions for unnamed bitfields when the
5899 bitfields are longer than their types. Therefore, we
5900 temporarily give the field a name. */
5901 if (PCC_BITFIELD_TYPE_MATTERS && !DECL_NAME (field))
5903 was_unnamed_p = true;
5904 DECL_NAME (field) = make_anon_name ();
5906 #endif
5907 DECL_SIZE (field) = TYPE_SIZE (integer_type);
5908 DECL_ALIGN (field) = TYPE_ALIGN (integer_type);
5909 DECL_USER_ALIGN (field) = TYPE_USER_ALIGN (integer_type);
5910 layout_nonempty_base_or_field (rli, field, NULL_TREE,
5911 empty_base_offsets);
5912 if (was_unnamed_p)
5913 DECL_NAME (field) = NULL_TREE;
5914 /* Now that layout has been performed, set the size of the
5915 field to the size of its declared type; the rest of the
5916 field is effectively invisible. */
5917 DECL_SIZE (field) = TYPE_SIZE (type);
5918 /* We must also reset the DECL_MODE of the field. */
5919 if (abi_version_at_least (2))
5920 DECL_MODE (field) = TYPE_MODE (type);
5921 else if (warn_abi
5922 && DECL_MODE (field) != TYPE_MODE (type))
5923 /* Versions of G++ before G++ 3.4 did not reset the
5924 DECL_MODE. */
5925 warning (OPT_Wabi,
5926 "the offset of %qD may not be ABI-compliant and may "
5927 "change in a future version of GCC", field);
5929 else
5930 layout_nonempty_base_or_field (rli, field, NULL_TREE,
5931 empty_base_offsets);
5933 /* Remember the location of any empty classes in FIELD. */
5934 if (abi_version_at_least (2))
5935 record_subobject_offsets (TREE_TYPE (field),
5936 byte_position(field),
5937 empty_base_offsets,
5938 /*is_data_member=*/true);
5940 /* If a bit-field does not immediately follow another bit-field,
5941 and yet it starts in the middle of a byte, we have failed to
5942 comply with the ABI. */
5943 if (warn_abi
5944 && DECL_C_BIT_FIELD (field)
5945 /* The TREE_NO_WARNING flag gets set by Objective-C when
5946 laying out an Objective-C class. The ObjC ABI differs
5947 from the C++ ABI, and so we do not want a warning
5948 here. */
5949 && !TREE_NO_WARNING (field)
5950 && !last_field_was_bitfield
5951 && !integer_zerop (size_binop (TRUNC_MOD_EXPR,
5952 DECL_FIELD_BIT_OFFSET (field),
5953 bitsize_unit_node)))
5954 warning (OPT_Wabi, "offset of %q+D is not ABI-compliant and may "
5955 "change in a future version of GCC", field);
5957 /* G++ used to use DECL_FIELD_OFFSET as if it were the byte
5958 offset of the field. */
5959 if (warn_abi
5960 && !abi_version_at_least (2)
5961 && !tree_int_cst_equal (DECL_FIELD_OFFSET (field),
5962 byte_position (field))
5963 && contains_empty_class_p (TREE_TYPE (field)))
5964 warning (OPT_Wabi, "%q+D contains empty classes which may cause base "
5965 "classes to be placed at different locations in a "
5966 "future version of GCC", field);
5968 /* The middle end uses the type of expressions to determine the
5969 possible range of expression values. In order to optimize
5970 "x.i > 7" to "false" for a 2-bit bitfield "i", the middle end
5971 must be made aware of the width of "i", via its type.
5973 Because C++ does not have integer types of arbitrary width,
5974 we must (for the purposes of the front end) convert from the
5975 type assigned here to the declared type of the bitfield
5976 whenever a bitfield expression is used as an rvalue.
5977 Similarly, when assigning a value to a bitfield, the value
5978 must be converted to the type given the bitfield here. */
5979 if (DECL_C_BIT_FIELD (field))
5981 unsigned HOST_WIDE_INT width;
5982 tree ftype = TREE_TYPE (field);
5983 width = tree_low_cst (DECL_SIZE (field), /*unsignedp=*/1);
5984 if (width != TYPE_PRECISION (ftype))
5986 TREE_TYPE (field)
5987 = c_build_bitfield_integer_type (width,
5988 TYPE_UNSIGNED (ftype));
5989 TREE_TYPE (field)
5990 = cp_build_qualified_type (TREE_TYPE (field),
5991 cp_type_quals (ftype));
5995 /* If we needed additional padding after this field, add it
5996 now. */
5997 if (padding)
5999 tree padding_field;
6001 padding_field = build_decl (input_location,
6002 FIELD_DECL,
6003 NULL_TREE,
6004 char_type_node);
6005 DECL_BIT_FIELD (padding_field) = 1;
6006 DECL_SIZE (padding_field) = padding;
6007 DECL_CONTEXT (padding_field) = t;
6008 DECL_ARTIFICIAL (padding_field) = 1;
6009 DECL_IGNORED_P (padding_field) = 1;
6010 layout_nonempty_base_or_field (rli, padding_field,
6011 NULL_TREE,
6012 empty_base_offsets);
6015 last_field_was_bitfield = DECL_C_BIT_FIELD (field);
6018 if (abi_version_at_least (2) && !integer_zerop (rli->bitpos))
6020 /* Make sure that we are on a byte boundary so that the size of
6021 the class without virtual bases will always be a round number
6022 of bytes. */
6023 rli->bitpos = round_up_loc (input_location, rli->bitpos, BITS_PER_UNIT);
6024 normalize_rli (rli);
6027 /* G++ 3.2 does not allow virtual bases to be overlaid with tail
6028 padding. */
6029 if (!abi_version_at_least (2))
6030 include_empty_classes(rli);
6032 /* Delete all zero-width bit-fields from the list of fields. Now
6033 that the type is laid out they are no longer important. */
6034 remove_zero_width_bit_fields (t);
6036 /* Create the version of T used for virtual bases. We do not use
6037 make_class_type for this version; this is an artificial type. For
6038 a POD type, we just reuse T. */
6039 if (CLASSTYPE_NON_LAYOUT_POD_P (t) || CLASSTYPE_EMPTY_P (t))
6041 base_t = make_node (TREE_CODE (t));
6043 /* Set the size and alignment for the new type. In G++ 3.2, all
6044 empty classes were considered to have size zero when used as
6045 base classes. */
6046 if (!abi_version_at_least (2) && CLASSTYPE_EMPTY_P (t))
6048 TYPE_SIZE (base_t) = bitsize_zero_node;
6049 TYPE_SIZE_UNIT (base_t) = size_zero_node;
6050 if (warn_abi && !integer_zerop (rli_size_unit_so_far (rli)))
6051 warning (OPT_Wabi,
6052 "layout of classes derived from empty class %qT "
6053 "may change in a future version of GCC",
6056 else
6058 tree eoc;
6060 /* If the ABI version is not at least two, and the last
6061 field was a bit-field, RLI may not be on a byte
6062 boundary. In particular, rli_size_unit_so_far might
6063 indicate the last complete byte, while rli_size_so_far
6064 indicates the total number of bits used. Therefore,
6065 rli_size_so_far, rather than rli_size_unit_so_far, is
6066 used to compute TYPE_SIZE_UNIT. */
6067 eoc = end_of_class (t, /*include_virtuals_p=*/0);
6068 TYPE_SIZE_UNIT (base_t)
6069 = size_binop (MAX_EXPR,
6070 convert (sizetype,
6071 size_binop (CEIL_DIV_EXPR,
6072 rli_size_so_far (rli),
6073 bitsize_int (BITS_PER_UNIT))),
6074 eoc);
6075 TYPE_SIZE (base_t)
6076 = size_binop (MAX_EXPR,
6077 rli_size_so_far (rli),
6078 size_binop (MULT_EXPR,
6079 convert (bitsizetype, eoc),
6080 bitsize_int (BITS_PER_UNIT)));
6082 TYPE_ALIGN (base_t) = rli->record_align;
6083 TYPE_USER_ALIGN (base_t) = TYPE_USER_ALIGN (t);
6085 /* Copy the fields from T. */
6086 next_field = &TYPE_FIELDS (base_t);
6087 for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
6088 if (TREE_CODE (field) == FIELD_DECL)
6090 *next_field = build_decl (input_location,
6091 FIELD_DECL,
6092 DECL_NAME (field),
6093 TREE_TYPE (field));
6094 DECL_CONTEXT (*next_field) = base_t;
6095 DECL_FIELD_OFFSET (*next_field) = DECL_FIELD_OFFSET (field);
6096 DECL_FIELD_BIT_OFFSET (*next_field)
6097 = DECL_FIELD_BIT_OFFSET (field);
6098 DECL_SIZE (*next_field) = DECL_SIZE (field);
6099 DECL_MODE (*next_field) = DECL_MODE (field);
6100 next_field = &DECL_CHAIN (*next_field);
6103 /* Record the base version of the type. */
6104 CLASSTYPE_AS_BASE (t) = base_t;
6105 TYPE_CONTEXT (base_t) = t;
6107 else
6108 CLASSTYPE_AS_BASE (t) = t;
6110 /* Every empty class contains an empty class. */
6111 if (CLASSTYPE_EMPTY_P (t))
6112 CLASSTYPE_CONTAINS_EMPTY_CLASS_P (t) = 1;
6114 /* Set the TYPE_DECL for this type to contain the right
6115 value for DECL_OFFSET, so that we can use it as part
6116 of a COMPONENT_REF for multiple inheritance. */
6117 layout_decl (TYPE_MAIN_DECL (t), 0);
6119 /* Now fix up any virtual base class types that we left lying
6120 around. We must get these done before we try to lay out the
6121 virtual function table. As a side-effect, this will remove the
6122 base subobject fields. */
6123 layout_virtual_bases (rli, empty_base_offsets);
6125 /* Make sure that empty classes are reflected in RLI at this
6126 point. */
6127 include_empty_classes(rli);
6129 /* Make sure not to create any structures with zero size. */
6130 if (integer_zerop (rli_size_unit_so_far (rli)) && CLASSTYPE_EMPTY_P (t))
6131 place_field (rli,
6132 build_decl (input_location,
6133 FIELD_DECL, NULL_TREE, char_type_node));
6135 /* If this is a non-POD, declaring it packed makes a difference to how it
6136 can be used as a field; don't let finalize_record_size undo it. */
6137 if (TYPE_PACKED (t) && !layout_pod_type_p (t))
6138 rli->packed_maybe_necessary = true;
6140 /* Let the back end lay out the type. */
6141 finish_record_layout (rli, /*free_p=*/true);
6143 /* Warn about bases that can't be talked about due to ambiguity. */
6144 warn_about_ambiguous_bases (t);
6146 /* Now that we're done with layout, give the base fields the real types. */
6147 for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
6148 if (DECL_ARTIFICIAL (field) && IS_FAKE_BASE_TYPE (TREE_TYPE (field)))
6149 TREE_TYPE (field) = TYPE_CONTEXT (TREE_TYPE (field));
6151 /* Clean up. */
6152 splay_tree_delete (empty_base_offsets);
6154 if (CLASSTYPE_EMPTY_P (t)
6155 && tree_int_cst_lt (sizeof_biggest_empty_class,
6156 TYPE_SIZE_UNIT (t)))
6157 sizeof_biggest_empty_class = TYPE_SIZE_UNIT (t);
6160 /* Determine the "key method" for the class type indicated by TYPE,
6161 and set CLASSTYPE_KEY_METHOD accordingly. */
6163 void
6164 determine_key_method (tree type)
6166 tree method;
6168 if (TYPE_FOR_JAVA (type)
6169 || processing_template_decl
6170 || CLASSTYPE_TEMPLATE_INSTANTIATION (type)
6171 || CLASSTYPE_INTERFACE_KNOWN (type))
6172 return;
6174 /* The key method is the first non-pure virtual function that is not
6175 inline at the point of class definition. On some targets the
6176 key function may not be inline; those targets should not call
6177 this function until the end of the translation unit. */
6178 for (method = TYPE_METHODS (type); method != NULL_TREE;
6179 method = DECL_CHAIN (method))
6180 if (DECL_VINDEX (method) != NULL_TREE
6181 && ! DECL_DECLARED_INLINE_P (method)
6182 && ! DECL_PURE_VIRTUAL_P (method))
6184 CLASSTYPE_KEY_METHOD (type) = method;
6185 break;
6188 return;
6192 /* Allocate and return an instance of struct sorted_fields_type with
6193 N fields. */
6195 static struct sorted_fields_type *
6196 sorted_fields_type_new (int n)
6198 struct sorted_fields_type *sft;
6199 sft = ggc_alloc_sorted_fields_type (sizeof (struct sorted_fields_type)
6200 + n * sizeof (tree));
6201 sft->len = n;
6203 return sft;
6207 /* Perform processing required when the definition of T (a class type)
6208 is complete. */
6210 void
6211 finish_struct_1 (tree t)
6213 tree x;
6214 /* A TREE_LIST. The TREE_VALUE of each node is a FUNCTION_DECL. */
6215 tree virtuals = NULL_TREE;
6217 if (COMPLETE_TYPE_P (t))
6219 gcc_assert (MAYBE_CLASS_TYPE_P (t));
6220 error ("redefinition of %q#T", t);
6221 popclass ();
6222 return;
6225 /* If this type was previously laid out as a forward reference,
6226 make sure we lay it out again. */
6227 TYPE_SIZE (t) = NULL_TREE;
6228 CLASSTYPE_PRIMARY_BINFO (t) = NULL_TREE;
6230 /* Make assumptions about the class; we'll reset the flags if
6231 necessary. */
6232 CLASSTYPE_EMPTY_P (t) = 1;
6233 CLASSTYPE_NEARLY_EMPTY_P (t) = 1;
6234 CLASSTYPE_CONTAINS_EMPTY_CLASS_P (t) = 0;
6235 CLASSTYPE_LITERAL_P (t) = true;
6237 /* Do end-of-class semantic processing: checking the validity of the
6238 bases and members and add implicitly generated methods. */
6239 check_bases_and_members (t);
6241 /* Find the key method. */
6242 if (TYPE_CONTAINS_VPTR_P (t))
6244 /* The Itanium C++ ABI permits the key method to be chosen when
6245 the class is defined -- even though the key method so
6246 selected may later turn out to be an inline function. On
6247 some systems (such as ARM Symbian OS) the key method cannot
6248 be determined until the end of the translation unit. On such
6249 systems, we leave CLASSTYPE_KEY_METHOD set to NULL, which
6250 will cause the class to be added to KEYED_CLASSES. Then, in
6251 finish_file we will determine the key method. */
6252 if (targetm.cxx.key_method_may_be_inline ())
6253 determine_key_method (t);
6255 /* If a polymorphic class has no key method, we may emit the vtable
6256 in every translation unit where the class definition appears. */
6257 if (CLASSTYPE_KEY_METHOD (t) == NULL_TREE)
6258 keyed_classes = tree_cons (NULL_TREE, t, keyed_classes);
6261 /* Layout the class itself. */
6262 layout_class_type (t, &virtuals);
6263 if (CLASSTYPE_AS_BASE (t) != t)
6264 /* We use the base type for trivial assignments, and hence it
6265 needs a mode. */
6266 compute_record_mode (CLASSTYPE_AS_BASE (t));
6268 virtuals = modify_all_vtables (t, nreverse (virtuals));
6270 /* If necessary, create the primary vtable for this class. */
6271 if (virtuals || TYPE_CONTAINS_VPTR_P (t))
6273 /* We must enter these virtuals into the table. */
6274 if (!CLASSTYPE_HAS_PRIMARY_BASE_P (t))
6275 build_primary_vtable (NULL_TREE, t);
6276 else if (! BINFO_NEW_VTABLE_MARKED (TYPE_BINFO (t)))
6277 /* Here we know enough to change the type of our virtual
6278 function table, but we will wait until later this function. */
6279 build_primary_vtable (CLASSTYPE_PRIMARY_BINFO (t), t);
6281 /* If we're warning about ABI tags, check the types of the new
6282 virtual functions. */
6283 if (warn_abi_tag)
6284 for (tree v = virtuals; v; v = TREE_CHAIN (v))
6285 check_abi_tags (t, TREE_VALUE (v));
6288 if (TYPE_CONTAINS_VPTR_P (t))
6290 int vindex;
6291 tree fn;
6293 if (BINFO_VTABLE (TYPE_BINFO (t)))
6294 gcc_assert (DECL_VIRTUAL_P (BINFO_VTABLE (TYPE_BINFO (t))));
6295 if (!CLASSTYPE_HAS_PRIMARY_BASE_P (t))
6296 gcc_assert (BINFO_VIRTUALS (TYPE_BINFO (t)) == NULL_TREE);
6298 /* Add entries for virtual functions introduced by this class. */
6299 BINFO_VIRTUALS (TYPE_BINFO (t))
6300 = chainon (BINFO_VIRTUALS (TYPE_BINFO (t)), virtuals);
6302 /* Set DECL_VINDEX for all functions declared in this class. */
6303 for (vindex = 0, fn = BINFO_VIRTUALS (TYPE_BINFO (t));
6305 fn = TREE_CHAIN (fn),
6306 vindex += (TARGET_VTABLE_USES_DESCRIPTORS
6307 ? TARGET_VTABLE_USES_DESCRIPTORS : 1))
6309 tree fndecl = BV_FN (fn);
6311 if (DECL_THUNK_P (fndecl))
6312 /* A thunk. We should never be calling this entry directly
6313 from this vtable -- we'd use the entry for the non
6314 thunk base function. */
6315 DECL_VINDEX (fndecl) = NULL_TREE;
6316 else if (TREE_CODE (DECL_VINDEX (fndecl)) != INTEGER_CST)
6317 DECL_VINDEX (fndecl) = build_int_cst (NULL_TREE, vindex);
6321 finish_struct_bits (t);
6322 set_method_tm_attributes (t);
6324 /* Complete the rtl for any static member objects of the type we're
6325 working on. */
6326 for (x = TYPE_FIELDS (t); x; x = DECL_CHAIN (x))
6327 if (TREE_CODE (x) == VAR_DECL && TREE_STATIC (x)
6328 && TREE_TYPE (x) != error_mark_node
6329 && same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (x)), t))
6330 DECL_MODE (x) = TYPE_MODE (t);
6332 /* Done with FIELDS...now decide whether to sort these for
6333 faster lookups later.
6335 We use a small number because most searches fail (succeeding
6336 ultimately as the search bores through the inheritance
6337 hierarchy), and we want this failure to occur quickly. */
6339 insert_into_classtype_sorted_fields (TYPE_FIELDS (t), t, 8);
6341 /* Complain if one of the field types requires lower visibility. */
6342 constrain_class_visibility (t);
6344 /* Make the rtl for any new vtables we have created, and unmark
6345 the base types we marked. */
6346 finish_vtbls (t);
6348 /* Build the VTT for T. */
6349 build_vtt (t);
6351 /* This warning does not make sense for Java classes, since they
6352 cannot have destructors. */
6353 if (!TYPE_FOR_JAVA (t) && warn_nonvdtor && TYPE_POLYMORPHIC_P (t))
6355 tree dtor;
6357 dtor = CLASSTYPE_DESTRUCTORS (t);
6358 if (/* An implicitly declared destructor is always public. And,
6359 if it were virtual, we would have created it by now. */
6360 !dtor
6361 || (!DECL_VINDEX (dtor)
6362 && (/* public non-virtual */
6363 (!TREE_PRIVATE (dtor) && !TREE_PROTECTED (dtor))
6364 || (/* non-public non-virtual with friends */
6365 (TREE_PRIVATE (dtor) || TREE_PROTECTED (dtor))
6366 && (CLASSTYPE_FRIEND_CLASSES (t)
6367 || DECL_FRIENDLIST (TYPE_MAIN_DECL (t)))))))
6368 warning (OPT_Wnon_virtual_dtor,
6369 "%q#T has virtual functions and accessible"
6370 " non-virtual destructor", t);
6373 complete_vars (t);
6375 if (warn_overloaded_virtual)
6376 warn_hidden (t);
6378 /* Class layout, assignment of virtual table slots, etc., is now
6379 complete. Give the back end a chance to tweak the visibility of
6380 the class or perform any other required target modifications. */
6381 targetm.cxx.adjust_class_at_definition (t);
6383 maybe_suppress_debug_info (t);
6385 dump_class_hierarchy (t);
6387 /* Finish debugging output for this type. */
6388 rest_of_type_compilation (t, ! LOCAL_CLASS_P (t));
6390 if (TYPE_TRANSPARENT_AGGR (t))
6392 tree field = first_field (t);
6393 if (field == NULL_TREE || error_operand_p (field))
6395 error ("type transparent %q#T does not have any fields", t);
6396 TYPE_TRANSPARENT_AGGR (t) = 0;
6398 else if (DECL_ARTIFICIAL (field))
6400 if (DECL_FIELD_IS_BASE (field))
6401 error ("type transparent class %qT has base classes", t);
6402 else
6404 gcc_checking_assert (DECL_VIRTUAL_P (field));
6405 error ("type transparent class %qT has virtual functions", t);
6407 TYPE_TRANSPARENT_AGGR (t) = 0;
6409 else if (TYPE_MODE (t) != DECL_MODE (field))
6411 error ("type transparent %q#T cannot be made transparent because "
6412 "the type of the first field has a different ABI from the "
6413 "class overall", t);
6414 TYPE_TRANSPARENT_AGGR (t) = 0;
6419 /* Insert FIELDS into T for the sorted case if the FIELDS count is
6420 equal to THRESHOLD or greater than THRESHOLD. */
6422 static void
6423 insert_into_classtype_sorted_fields (tree fields, tree t, int threshold)
6425 int n_fields = count_fields (fields);
6426 if (n_fields >= threshold)
6428 struct sorted_fields_type *field_vec = sorted_fields_type_new (n_fields);
6429 add_fields_to_record_type (fields, field_vec, 0);
6430 qsort (field_vec->elts, n_fields, sizeof (tree), field_decl_cmp);
6431 CLASSTYPE_SORTED_FIELDS (t) = field_vec;
6435 /* Insert lately defined enum ENUMTYPE into T for the sorted case. */
6437 void
6438 insert_late_enum_def_into_classtype_sorted_fields (tree enumtype, tree t)
6440 struct sorted_fields_type *sorted_fields = CLASSTYPE_SORTED_FIELDS (t);
6441 if (sorted_fields)
6443 int i;
6444 int n_fields
6445 = list_length (TYPE_VALUES (enumtype)) + sorted_fields->len;
6446 struct sorted_fields_type *field_vec = sorted_fields_type_new (n_fields);
6448 for (i = 0; i < sorted_fields->len; ++i)
6449 field_vec->elts[i] = sorted_fields->elts[i];
6451 add_enum_fields_to_record_type (enumtype, field_vec,
6452 sorted_fields->len);
6453 qsort (field_vec->elts, n_fields, sizeof (tree), field_decl_cmp);
6454 CLASSTYPE_SORTED_FIELDS (t) = field_vec;
6458 /* When T was built up, the member declarations were added in reverse
6459 order. Rearrange them to declaration order. */
6461 void
6462 unreverse_member_declarations (tree t)
6464 tree next;
6465 tree prev;
6466 tree x;
6468 /* The following lists are all in reverse order. Put them in
6469 declaration order now. */
6470 TYPE_METHODS (t) = nreverse (TYPE_METHODS (t));
6471 CLASSTYPE_DECL_LIST (t) = nreverse (CLASSTYPE_DECL_LIST (t));
6473 /* Actually, for the TYPE_FIELDS, only the non TYPE_DECLs are in
6474 reverse order, so we can't just use nreverse. */
6475 prev = NULL_TREE;
6476 for (x = TYPE_FIELDS (t);
6477 x && TREE_CODE (x) != TYPE_DECL;
6478 x = next)
6480 next = DECL_CHAIN (x);
6481 DECL_CHAIN (x) = prev;
6482 prev = x;
6484 if (prev)
6486 DECL_CHAIN (TYPE_FIELDS (t)) = x;
6487 if (prev)
6488 TYPE_FIELDS (t) = prev;
6492 tree
6493 finish_struct (tree t, tree attributes)
6495 location_t saved_loc = input_location;
6497 /* Now that we've got all the field declarations, reverse everything
6498 as necessary. */
6499 unreverse_member_declarations (t);
6501 cplus_decl_attributes (&t, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
6503 /* Nadger the current location so that diagnostics point to the start of
6504 the struct, not the end. */
6505 input_location = DECL_SOURCE_LOCATION (TYPE_NAME (t));
6507 if (processing_template_decl)
6509 tree x;
6511 finish_struct_methods (t);
6512 TYPE_SIZE (t) = bitsize_zero_node;
6513 TYPE_SIZE_UNIT (t) = size_zero_node;
6515 /* We need to emit an error message if this type was used as a parameter
6516 and it is an abstract type, even if it is a template. We construct
6517 a simple CLASSTYPE_PURE_VIRTUALS list without taking bases into
6518 account and we call complete_vars with this type, which will check
6519 the PARM_DECLS. Note that while the type is being defined,
6520 CLASSTYPE_PURE_VIRTUALS contains the list of the inline friends
6521 (see CLASSTYPE_INLINE_FRIENDS) so we need to clear it. */
6522 CLASSTYPE_PURE_VIRTUALS (t) = NULL;
6523 for (x = TYPE_METHODS (t); x; x = DECL_CHAIN (x))
6524 if (DECL_PURE_VIRTUAL_P (x))
6525 vec_safe_push (CLASSTYPE_PURE_VIRTUALS (t), x);
6526 complete_vars (t);
6527 /* We need to add the target functions to the CLASSTYPE_METHOD_VEC if
6528 an enclosing scope is a template class, so that this function be
6529 found by lookup_fnfields_1 when the using declaration is not
6530 instantiated yet. */
6531 for (x = TYPE_FIELDS (t); x; x = DECL_CHAIN (x))
6532 if (TREE_CODE (x) == USING_DECL)
6534 tree fn = strip_using_decl (x);
6535 if (is_overloaded_fn (fn))
6536 for (; fn; fn = OVL_NEXT (fn))
6537 add_method (t, OVL_CURRENT (fn), x);
6540 /* Remember current #pragma pack value. */
6541 TYPE_PRECISION (t) = maximum_field_alignment;
6543 /* Fix up any variants we've already built. */
6544 for (x = TYPE_NEXT_VARIANT (t); x; x = TYPE_NEXT_VARIANT (x))
6546 TYPE_SIZE (x) = TYPE_SIZE (t);
6547 TYPE_SIZE_UNIT (x) = TYPE_SIZE_UNIT (t);
6548 TYPE_FIELDS (x) = TYPE_FIELDS (t);
6549 TYPE_METHODS (x) = TYPE_METHODS (t);
6552 else
6553 finish_struct_1 (t);
6555 input_location = saved_loc;
6557 TYPE_BEING_DEFINED (t) = 0;
6559 if (current_class_type)
6560 popclass ();
6561 else
6562 error ("trying to finish struct, but kicked out due to previous parse errors");
6564 if (processing_template_decl && at_function_scope_p ()
6565 /* Lambdas are defined by the LAMBDA_EXPR. */
6566 && !LAMBDA_TYPE_P (t))
6567 add_stmt (build_min (TAG_DEFN, t));
6569 return t;
6572 /* Hash table to avoid endless recursion when handling references. */
6573 static hash_table <pointer_hash <tree_node> > fixed_type_or_null_ref_ht;
6575 /* Return the dynamic type of INSTANCE, if known.
6576 Used to determine whether the virtual function table is needed
6577 or not.
6579 *NONNULL is set iff INSTANCE can be known to be nonnull, regardless
6580 of our knowledge of its type. *NONNULL should be initialized
6581 before this function is called. */
6583 static tree
6584 fixed_type_or_null (tree instance, int *nonnull, int *cdtorp)
6586 #define RECUR(T) fixed_type_or_null((T), nonnull, cdtorp)
6588 switch (TREE_CODE (instance))
6590 case INDIRECT_REF:
6591 if (POINTER_TYPE_P (TREE_TYPE (instance)))
6592 return NULL_TREE;
6593 else
6594 return RECUR (TREE_OPERAND (instance, 0));
6596 case CALL_EXPR:
6597 /* This is a call to a constructor, hence it's never zero. */
6598 if (TREE_HAS_CONSTRUCTOR (instance))
6600 if (nonnull)
6601 *nonnull = 1;
6602 return TREE_TYPE (instance);
6604 return NULL_TREE;
6606 case SAVE_EXPR:
6607 /* This is a call to a constructor, hence it's never zero. */
6608 if (TREE_HAS_CONSTRUCTOR (instance))
6610 if (nonnull)
6611 *nonnull = 1;
6612 return TREE_TYPE (instance);
6614 return RECUR (TREE_OPERAND (instance, 0));
6616 case POINTER_PLUS_EXPR:
6617 case PLUS_EXPR:
6618 case MINUS_EXPR:
6619 if (TREE_CODE (TREE_OPERAND (instance, 0)) == ADDR_EXPR)
6620 return RECUR (TREE_OPERAND (instance, 0));
6621 if (TREE_CODE (TREE_OPERAND (instance, 1)) == INTEGER_CST)
6622 /* Propagate nonnull. */
6623 return RECUR (TREE_OPERAND (instance, 0));
6625 return NULL_TREE;
6627 CASE_CONVERT:
6628 return RECUR (TREE_OPERAND (instance, 0));
6630 case ADDR_EXPR:
6631 instance = TREE_OPERAND (instance, 0);
6632 if (nonnull)
6634 /* Just because we see an ADDR_EXPR doesn't mean we're dealing
6635 with a real object -- given &p->f, p can still be null. */
6636 tree t = get_base_address (instance);
6637 /* ??? Probably should check DECL_WEAK here. */
6638 if (t && DECL_P (t))
6639 *nonnull = 1;
6641 return RECUR (instance);
6643 case COMPONENT_REF:
6644 /* If this component is really a base class reference, then the field
6645 itself isn't definitive. */
6646 if (DECL_FIELD_IS_BASE (TREE_OPERAND (instance, 1)))
6647 return RECUR (TREE_OPERAND (instance, 0));
6648 return RECUR (TREE_OPERAND (instance, 1));
6650 case VAR_DECL:
6651 case FIELD_DECL:
6652 if (TREE_CODE (TREE_TYPE (instance)) == ARRAY_TYPE
6653 && MAYBE_CLASS_TYPE_P (TREE_TYPE (TREE_TYPE (instance))))
6655 if (nonnull)
6656 *nonnull = 1;
6657 return TREE_TYPE (TREE_TYPE (instance));
6659 /* fall through... */
6660 case TARGET_EXPR:
6661 case PARM_DECL:
6662 case RESULT_DECL:
6663 if (MAYBE_CLASS_TYPE_P (TREE_TYPE (instance)))
6665 if (nonnull)
6666 *nonnull = 1;
6667 return TREE_TYPE (instance);
6669 else if (instance == current_class_ptr)
6671 if (nonnull)
6672 *nonnull = 1;
6674 /* if we're in a ctor or dtor, we know our type. If
6675 current_class_ptr is set but we aren't in a function, we're in
6676 an NSDMI (and therefore a constructor). */
6677 if (current_scope () != current_function_decl
6678 || (DECL_LANG_SPECIFIC (current_function_decl)
6679 && (DECL_CONSTRUCTOR_P (current_function_decl)
6680 || DECL_DESTRUCTOR_P (current_function_decl))))
6682 if (cdtorp)
6683 *cdtorp = 1;
6684 return TREE_TYPE (TREE_TYPE (instance));
6687 else if (TREE_CODE (TREE_TYPE (instance)) == REFERENCE_TYPE)
6689 /* We only need one hash table because it is always left empty. */
6690 if (!fixed_type_or_null_ref_ht.is_created ())
6691 fixed_type_or_null_ref_ht.create (37);
6693 /* Reference variables should be references to objects. */
6694 if (nonnull)
6695 *nonnull = 1;
6697 /* Enter the INSTANCE in a table to prevent recursion; a
6698 variable's initializer may refer to the variable
6699 itself. */
6700 if (TREE_CODE (instance) == VAR_DECL
6701 && DECL_INITIAL (instance)
6702 && !type_dependent_expression_p_push (DECL_INITIAL (instance))
6703 && !fixed_type_or_null_ref_ht.find (instance))
6705 tree type;
6706 tree_node **slot;
6708 slot = fixed_type_or_null_ref_ht.find_slot (instance, INSERT);
6709 *slot = instance;
6710 type = RECUR (DECL_INITIAL (instance));
6711 fixed_type_or_null_ref_ht.remove_elt (instance);
6713 return type;
6716 return NULL_TREE;
6718 default:
6719 return NULL_TREE;
6721 #undef RECUR
6724 /* Return nonzero if the dynamic type of INSTANCE is known, and
6725 equivalent to the static type. We also handle the case where
6726 INSTANCE is really a pointer. Return negative if this is a
6727 ctor/dtor. There the dynamic type is known, but this might not be
6728 the most derived base of the original object, and hence virtual
6729 bases may not be layed out according to this type.
6731 Used to determine whether the virtual function table is needed
6732 or not.
6734 *NONNULL is set iff INSTANCE can be known to be nonnull, regardless
6735 of our knowledge of its type. *NONNULL should be initialized
6736 before this function is called. */
6739 resolves_to_fixed_type_p (tree instance, int* nonnull)
6741 tree t = TREE_TYPE (instance);
6742 int cdtorp = 0;
6743 tree fixed;
6745 /* processing_template_decl can be false in a template if we're in
6746 fold_non_dependent_expr, but we still want to suppress this check. */
6747 if (in_template_function ())
6749 /* In a template we only care about the type of the result. */
6750 if (nonnull)
6751 *nonnull = true;
6752 return true;
6755 fixed = fixed_type_or_null (instance, nonnull, &cdtorp);
6756 if (fixed == NULL_TREE)
6757 return 0;
6758 if (POINTER_TYPE_P (t))
6759 t = TREE_TYPE (t);
6760 if (!same_type_ignoring_top_level_qualifiers_p (t, fixed))
6761 return 0;
6762 return cdtorp ? -1 : 1;
6766 void
6767 init_class_processing (void)
6769 current_class_depth = 0;
6770 current_class_stack_size = 10;
6771 current_class_stack
6772 = XNEWVEC (struct class_stack_node, current_class_stack_size);
6773 vec_alloc (local_classes, 8);
6774 sizeof_biggest_empty_class = size_zero_node;
6776 ridpointers[(int) RID_PUBLIC] = access_public_node;
6777 ridpointers[(int) RID_PRIVATE] = access_private_node;
6778 ridpointers[(int) RID_PROTECTED] = access_protected_node;
6781 /* Restore the cached PREVIOUS_CLASS_LEVEL. */
6783 static void
6784 restore_class_cache (void)
6786 tree type;
6788 /* We are re-entering the same class we just left, so we don't
6789 have to search the whole inheritance matrix to find all the
6790 decls to bind again. Instead, we install the cached
6791 class_shadowed list and walk through it binding names. */
6792 push_binding_level (previous_class_level);
6793 class_binding_level = previous_class_level;
6794 /* Restore IDENTIFIER_TYPE_VALUE. */
6795 for (type = class_binding_level->type_shadowed;
6796 type;
6797 type = TREE_CHAIN (type))
6798 SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (type), TREE_TYPE (type));
6801 /* Set global variables CURRENT_CLASS_NAME and CURRENT_CLASS_TYPE as
6802 appropriate for TYPE.
6804 So that we may avoid calls to lookup_name, we cache the _TYPE
6805 nodes of local TYPE_DECLs in the TREE_TYPE field of the name.
6807 For multiple inheritance, we perform a two-pass depth-first search
6808 of the type lattice. */
6810 void
6811 pushclass (tree type)
6813 class_stack_node_t csn;
6815 type = TYPE_MAIN_VARIANT (type);
6817 /* Make sure there is enough room for the new entry on the stack. */
6818 if (current_class_depth + 1 >= current_class_stack_size)
6820 current_class_stack_size *= 2;
6821 current_class_stack
6822 = XRESIZEVEC (struct class_stack_node, current_class_stack,
6823 current_class_stack_size);
6826 /* Insert a new entry on the class stack. */
6827 csn = current_class_stack + current_class_depth;
6828 csn->name = current_class_name;
6829 csn->type = current_class_type;
6830 csn->access = current_access_specifier;
6831 csn->names_used = 0;
6832 csn->hidden = 0;
6833 current_class_depth++;
6835 /* Now set up the new type. */
6836 current_class_name = TYPE_NAME (type);
6837 if (TREE_CODE (current_class_name) == TYPE_DECL)
6838 current_class_name = DECL_NAME (current_class_name);
6839 current_class_type = type;
6841 /* By default, things in classes are private, while things in
6842 structures or unions are public. */
6843 current_access_specifier = (CLASSTYPE_DECLARED_CLASS (type)
6844 ? access_private_node
6845 : access_public_node);
6847 if (previous_class_level
6848 && type != previous_class_level->this_entity
6849 && current_class_depth == 1)
6851 /* Forcibly remove any old class remnants. */
6852 invalidate_class_lookup_cache ();
6855 if (!previous_class_level
6856 || type != previous_class_level->this_entity
6857 || current_class_depth > 1)
6858 pushlevel_class ();
6859 else
6860 restore_class_cache ();
6863 /* When we exit a toplevel class scope, we save its binding level so
6864 that we can restore it quickly. Here, we've entered some other
6865 class, so we must invalidate our cache. */
6867 void
6868 invalidate_class_lookup_cache (void)
6870 previous_class_level = NULL;
6873 /* Get out of the current class scope. If we were in a class scope
6874 previously, that is the one popped to. */
6876 void
6877 popclass (void)
6879 poplevel_class ();
6881 current_class_depth--;
6882 current_class_name = current_class_stack[current_class_depth].name;
6883 current_class_type = current_class_stack[current_class_depth].type;
6884 current_access_specifier = current_class_stack[current_class_depth].access;
6885 if (current_class_stack[current_class_depth].names_used)
6886 splay_tree_delete (current_class_stack[current_class_depth].names_used);
6889 /* Mark the top of the class stack as hidden. */
6891 void
6892 push_class_stack (void)
6894 if (current_class_depth)
6895 ++current_class_stack[current_class_depth - 1].hidden;
6898 /* Mark the top of the class stack as un-hidden. */
6900 void
6901 pop_class_stack (void)
6903 if (current_class_depth)
6904 --current_class_stack[current_class_depth - 1].hidden;
6907 /* Returns 1 if the class type currently being defined is either T or
6908 a nested type of T. */
6910 bool
6911 currently_open_class (tree t)
6913 int i;
6915 if (!CLASS_TYPE_P (t))
6916 return false;
6918 t = TYPE_MAIN_VARIANT (t);
6920 /* We start looking from 1 because entry 0 is from global scope,
6921 and has no type. */
6922 for (i = current_class_depth; i > 0; --i)
6924 tree c;
6925 if (i == current_class_depth)
6926 c = current_class_type;
6927 else
6929 if (current_class_stack[i].hidden)
6930 break;
6931 c = current_class_stack[i].type;
6933 if (!c)
6934 continue;
6935 if (same_type_p (c, t))
6936 return true;
6938 return false;
6941 /* If either current_class_type or one of its enclosing classes are derived
6942 from T, return the appropriate type. Used to determine how we found
6943 something via unqualified lookup. */
6945 tree
6946 currently_open_derived_class (tree t)
6948 int i;
6950 /* The bases of a dependent type are unknown. */
6951 if (dependent_type_p (t))
6952 return NULL_TREE;
6954 if (!current_class_type)
6955 return NULL_TREE;
6957 if (DERIVED_FROM_P (t, current_class_type))
6958 return current_class_type;
6960 for (i = current_class_depth - 1; i > 0; --i)
6962 if (current_class_stack[i].hidden)
6963 break;
6964 if (DERIVED_FROM_P (t, current_class_stack[i].type))
6965 return current_class_stack[i].type;
6968 return NULL_TREE;
6971 /* Returns the innermost class type which is not a lambda closure type. */
6973 tree
6974 current_nonlambda_class_type (void)
6976 int i;
6978 /* We start looking from 1 because entry 0 is from global scope,
6979 and has no type. */
6980 for (i = current_class_depth; i > 0; --i)
6982 tree c;
6983 if (i == current_class_depth)
6984 c = current_class_type;
6985 else
6987 if (current_class_stack[i].hidden)
6988 break;
6989 c = current_class_stack[i].type;
6991 if (!c)
6992 continue;
6993 if (!LAMBDA_TYPE_P (c))
6994 return c;
6996 return NULL_TREE;
6999 /* When entering a class scope, all enclosing class scopes' names with
7000 static meaning (static variables, static functions, types and
7001 enumerators) have to be visible. This recursive function calls
7002 pushclass for all enclosing class contexts until global or a local
7003 scope is reached. TYPE is the enclosed class. */
7005 void
7006 push_nested_class (tree type)
7008 /* A namespace might be passed in error cases, like A::B:C. */
7009 if (type == NULL_TREE
7010 || !CLASS_TYPE_P (type))
7011 return;
7013 push_nested_class (DECL_CONTEXT (TYPE_MAIN_DECL (type)));
7015 pushclass (type);
7018 /* Undoes a push_nested_class call. */
7020 void
7021 pop_nested_class (void)
7023 tree context = DECL_CONTEXT (TYPE_MAIN_DECL (current_class_type));
7025 popclass ();
7026 if (context && CLASS_TYPE_P (context))
7027 pop_nested_class ();
7030 /* Returns the number of extern "LANG" blocks we are nested within. */
7033 current_lang_depth (void)
7035 return vec_safe_length (current_lang_base);
7038 /* Set global variables CURRENT_LANG_NAME to appropriate value
7039 so that behavior of name-mangling machinery is correct. */
7041 void
7042 push_lang_context (tree name)
7044 vec_safe_push (current_lang_base, current_lang_name);
7046 if (name == lang_name_cplusplus)
7048 current_lang_name = name;
7050 else if (name == lang_name_java)
7052 current_lang_name = name;
7053 /* DECL_IGNORED_P is initially set for these types, to avoid clutter.
7054 (See record_builtin_java_type in decl.c.) However, that causes
7055 incorrect debug entries if these types are actually used.
7056 So we re-enable debug output after extern "Java". */
7057 DECL_IGNORED_P (TYPE_NAME (java_byte_type_node)) = 0;
7058 DECL_IGNORED_P (TYPE_NAME (java_short_type_node)) = 0;
7059 DECL_IGNORED_P (TYPE_NAME (java_int_type_node)) = 0;
7060 DECL_IGNORED_P (TYPE_NAME (java_long_type_node)) = 0;
7061 DECL_IGNORED_P (TYPE_NAME (java_float_type_node)) = 0;
7062 DECL_IGNORED_P (TYPE_NAME (java_double_type_node)) = 0;
7063 DECL_IGNORED_P (TYPE_NAME (java_char_type_node)) = 0;
7064 DECL_IGNORED_P (TYPE_NAME (java_boolean_type_node)) = 0;
7066 else if (name == lang_name_c)
7068 current_lang_name = name;
7070 else
7071 error ("language string %<\"%E\"%> not recognized", name);
7074 /* Get out of the current language scope. */
7076 void
7077 pop_lang_context (void)
7079 current_lang_name = current_lang_base->pop ();
7082 /* Type instantiation routines. */
7084 /* Given an OVERLOAD and a TARGET_TYPE, return the function that
7085 matches the TARGET_TYPE. If there is no satisfactory match, return
7086 error_mark_node, and issue an error & warning messages under
7087 control of FLAGS. Permit pointers to member function if FLAGS
7088 permits. If TEMPLATE_ONLY, the name of the overloaded function was
7089 a template-id, and EXPLICIT_TARGS are the explicitly provided
7090 template arguments.
7092 If OVERLOAD is for one or more member functions, then ACCESS_PATH
7093 is the base path used to reference those member functions. If
7094 the address is resolved to a member function, access checks will be
7095 performed and errors issued if appropriate. */
7097 static tree
7098 resolve_address_of_overloaded_function (tree target_type,
7099 tree overload,
7100 tsubst_flags_t flags,
7101 bool template_only,
7102 tree explicit_targs,
7103 tree access_path)
7105 /* Here's what the standard says:
7107 [over.over]
7109 If the name is a function template, template argument deduction
7110 is done, and if the argument deduction succeeds, the deduced
7111 arguments are used to generate a single template function, which
7112 is added to the set of overloaded functions considered.
7114 Non-member functions and static member functions match targets of
7115 type "pointer-to-function" or "reference-to-function." Nonstatic
7116 member functions match targets of type "pointer-to-member
7117 function;" the function type of the pointer to member is used to
7118 select the member function from the set of overloaded member
7119 functions. If a nonstatic member function is selected, the
7120 reference to the overloaded function name is required to have the
7121 form of a pointer to member as described in 5.3.1.
7123 If more than one function is selected, any template functions in
7124 the set are eliminated if the set also contains a non-template
7125 function, and any given template function is eliminated if the
7126 set contains a second template function that is more specialized
7127 than the first according to the partial ordering rules 14.5.5.2.
7128 After such eliminations, if any, there shall remain exactly one
7129 selected function. */
7131 int is_ptrmem = 0;
7132 /* We store the matches in a TREE_LIST rooted here. The functions
7133 are the TREE_PURPOSE, not the TREE_VALUE, in this list, for easy
7134 interoperability with most_specialized_instantiation. */
7135 tree matches = NULL_TREE;
7136 tree fn;
7137 tree target_fn_type;
7139 /* By the time we get here, we should be seeing only real
7140 pointer-to-member types, not the internal POINTER_TYPE to
7141 METHOD_TYPE representation. */
7142 gcc_assert (TREE_CODE (target_type) != POINTER_TYPE
7143 || TREE_CODE (TREE_TYPE (target_type)) != METHOD_TYPE);
7145 gcc_assert (is_overloaded_fn (overload));
7147 /* Check that the TARGET_TYPE is reasonable. */
7148 if (TYPE_PTRFN_P (target_type))
7149 /* This is OK. */;
7150 else if (TYPE_PTRMEMFUNC_P (target_type))
7151 /* This is OK, too. */
7152 is_ptrmem = 1;
7153 else if (TREE_CODE (target_type) == FUNCTION_TYPE)
7154 /* This is OK, too. This comes from a conversion to reference
7155 type. */
7156 target_type = build_reference_type (target_type);
7157 else
7159 if (flags & tf_error)
7160 error ("cannot resolve overloaded function %qD based on"
7161 " conversion to type %qT",
7162 DECL_NAME (OVL_FUNCTION (overload)), target_type);
7163 return error_mark_node;
7166 /* Non-member functions and static member functions match targets of type
7167 "pointer-to-function" or "reference-to-function." Nonstatic member
7168 functions match targets of type "pointer-to-member-function;" the
7169 function type of the pointer to member is used to select the member
7170 function from the set of overloaded member functions.
7172 So figure out the FUNCTION_TYPE that we want to match against. */
7173 target_fn_type = static_fn_type (target_type);
7175 /* If we can find a non-template function that matches, we can just
7176 use it. There's no point in generating template instantiations
7177 if we're just going to throw them out anyhow. But, of course, we
7178 can only do this when we don't *need* a template function. */
7179 if (!template_only)
7181 tree fns;
7183 for (fns = overload; fns; fns = OVL_NEXT (fns))
7185 tree fn = OVL_CURRENT (fns);
7187 if (TREE_CODE (fn) == TEMPLATE_DECL)
7188 /* We're not looking for templates just yet. */
7189 continue;
7191 if ((TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
7192 != is_ptrmem)
7193 /* We're looking for a non-static member, and this isn't
7194 one, or vice versa. */
7195 continue;
7197 /* Ignore functions which haven't been explicitly
7198 declared. */
7199 if (DECL_ANTICIPATED (fn))
7200 continue;
7202 /* See if there's a match. */
7203 if (same_type_p (target_fn_type, static_fn_type (fn)))
7204 matches = tree_cons (fn, NULL_TREE, matches);
7208 /* Now, if we've already got a match (or matches), there's no need
7209 to proceed to the template functions. But, if we don't have a
7210 match we need to look at them, too. */
7211 if (!matches)
7213 tree target_arg_types;
7214 tree target_ret_type;
7215 tree fns;
7216 tree *args;
7217 unsigned int nargs, ia;
7218 tree arg;
7220 target_arg_types = TYPE_ARG_TYPES (target_fn_type);
7221 target_ret_type = TREE_TYPE (target_fn_type);
7223 nargs = list_length (target_arg_types);
7224 args = XALLOCAVEC (tree, nargs);
7225 for (arg = target_arg_types, ia = 0;
7226 arg != NULL_TREE && arg != void_list_node;
7227 arg = TREE_CHAIN (arg), ++ia)
7228 args[ia] = TREE_VALUE (arg);
7229 nargs = ia;
7231 for (fns = overload; fns; fns = OVL_NEXT (fns))
7233 tree fn = OVL_CURRENT (fns);
7234 tree instantiation;
7235 tree targs;
7237 if (TREE_CODE (fn) != TEMPLATE_DECL)
7238 /* We're only looking for templates. */
7239 continue;
7241 if ((TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
7242 != is_ptrmem)
7243 /* We're not looking for a non-static member, and this is
7244 one, or vice versa. */
7245 continue;
7247 /* Try to do argument deduction. */
7248 targs = make_tree_vec (DECL_NTPARMS (fn));
7249 instantiation = fn_type_unification (fn, explicit_targs, targs, args,
7250 nargs, target_ret_type,
7251 DEDUCE_EXACT, LOOKUP_NORMAL,
7252 false);
7253 if (instantiation == error_mark_node)
7254 /* Instantiation failed. */
7255 continue;
7257 /* See if there's a match. */
7258 if (same_type_p (target_fn_type, static_fn_type (instantiation)))
7259 matches = tree_cons (instantiation, fn, matches);
7262 /* Now, remove all but the most specialized of the matches. */
7263 if (matches)
7265 tree match = most_specialized_instantiation (matches);
7267 if (match != error_mark_node)
7268 matches = tree_cons (TREE_PURPOSE (match),
7269 NULL_TREE,
7270 NULL_TREE);
7274 /* Now we should have exactly one function in MATCHES. */
7275 if (matches == NULL_TREE)
7277 /* There were *no* matches. */
7278 if (flags & tf_error)
7280 error ("no matches converting function %qD to type %q#T",
7281 DECL_NAME (OVL_CURRENT (overload)),
7282 target_type);
7284 print_candidates (overload);
7286 return error_mark_node;
7288 else if (TREE_CHAIN (matches))
7290 /* There were too many matches. First check if they're all
7291 the same function. */
7292 tree match = NULL_TREE;
7294 fn = TREE_PURPOSE (matches);
7296 /* For multi-versioned functions, more than one match is just fine and
7297 decls_match will return false as they are different. */
7298 for (match = TREE_CHAIN (matches); match; match = TREE_CHAIN (match))
7299 if (!decls_match (fn, TREE_PURPOSE (match))
7300 && !targetm.target_option.function_versions
7301 (fn, TREE_PURPOSE (match)))
7302 break;
7304 if (match)
7306 if (flags & tf_error)
7308 error ("converting overloaded function %qD to type %q#T is ambiguous",
7309 DECL_NAME (OVL_FUNCTION (overload)),
7310 target_type);
7312 /* Since print_candidates expects the functions in the
7313 TREE_VALUE slot, we flip them here. */
7314 for (match = matches; match; match = TREE_CHAIN (match))
7315 TREE_VALUE (match) = TREE_PURPOSE (match);
7317 print_candidates (matches);
7320 return error_mark_node;
7324 /* Good, exactly one match. Now, convert it to the correct type. */
7325 fn = TREE_PURPOSE (matches);
7327 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
7328 && !(flags & tf_ptrmem_ok) && !flag_ms_extensions)
7330 static int explained;
7332 if (!(flags & tf_error))
7333 return error_mark_node;
7335 permerror (input_location, "assuming pointer to member %qD", fn);
7336 if (!explained)
7338 inform (input_location, "(a pointer to member can only be formed with %<&%E%>)", fn);
7339 explained = 1;
7343 /* If a pointer to a function that is multi-versioned is requested, the
7344 pointer to the dispatcher function is returned instead. This works
7345 well because indirectly calling the function will dispatch the right
7346 function version at run-time. */
7347 if (DECL_FUNCTION_VERSIONED (fn))
7349 fn = get_function_version_dispatcher (fn);
7350 if (fn == NULL)
7351 return error_mark_node;
7352 /* Mark all the versions corresponding to the dispatcher as used. */
7353 if (!(flags & tf_conv))
7354 mark_versions_used (fn);
7357 /* If we're doing overload resolution purely for the purpose of
7358 determining conversion sequences, we should not consider the
7359 function used. If this conversion sequence is selected, the
7360 function will be marked as used at this point. */
7361 if (!(flags & tf_conv))
7363 /* Make =delete work with SFINAE. */
7364 if (DECL_DELETED_FN (fn) && !(flags & tf_error))
7365 return error_mark_node;
7367 mark_used (fn);
7370 /* We could not check access to member functions when this
7371 expression was originally created since we did not know at that
7372 time to which function the expression referred. */
7373 if (DECL_FUNCTION_MEMBER_P (fn))
7375 gcc_assert (access_path);
7376 perform_or_defer_access_check (access_path, fn, fn, flags);
7379 if (TYPE_PTRFN_P (target_type) || TYPE_PTRMEMFUNC_P (target_type))
7380 return cp_build_addr_expr (fn, flags);
7381 else
7383 /* The target must be a REFERENCE_TYPE. Above, cp_build_unary_op
7384 will mark the function as addressed, but here we must do it
7385 explicitly. */
7386 cxx_mark_addressable (fn);
7388 return fn;
7392 /* This function will instantiate the type of the expression given in
7393 RHS to match the type of LHSTYPE. If errors exist, then return
7394 error_mark_node. FLAGS is a bit mask. If TF_ERROR is set, then
7395 we complain on errors. If we are not complaining, never modify rhs,
7396 as overload resolution wants to try many possible instantiations, in
7397 the hope that at least one will work.
7399 For non-recursive calls, LHSTYPE should be a function, pointer to
7400 function, or a pointer to member function. */
7402 tree
7403 instantiate_type (tree lhstype, tree rhs, tsubst_flags_t flags)
7405 tsubst_flags_t flags_in = flags;
7406 tree access_path = NULL_TREE;
7408 flags &= ~tf_ptrmem_ok;
7410 if (lhstype == unknown_type_node)
7412 if (flags & tf_error)
7413 error ("not enough type information");
7414 return error_mark_node;
7417 if (TREE_TYPE (rhs) != NULL_TREE && ! (type_unknown_p (rhs)))
7419 if (same_type_p (lhstype, TREE_TYPE (rhs)))
7420 return rhs;
7421 if (flag_ms_extensions
7422 && TYPE_PTRMEMFUNC_P (lhstype)
7423 && !TYPE_PTRMEMFUNC_P (TREE_TYPE (rhs)))
7424 /* Microsoft allows `A::f' to be resolved to a
7425 pointer-to-member. */
7427 else
7429 if (flags & tf_error)
7430 error ("cannot convert %qE from type %qT to type %qT",
7431 rhs, TREE_TYPE (rhs), lhstype);
7432 return error_mark_node;
7436 if (BASELINK_P (rhs))
7438 access_path = BASELINK_ACCESS_BINFO (rhs);
7439 rhs = BASELINK_FUNCTIONS (rhs);
7442 /* If we are in a template, and have a NON_DEPENDENT_EXPR, we cannot
7443 deduce any type information. */
7444 if (TREE_CODE (rhs) == NON_DEPENDENT_EXPR)
7446 if (flags & tf_error)
7447 error ("not enough type information");
7448 return error_mark_node;
7451 /* There only a few kinds of expressions that may have a type
7452 dependent on overload resolution. */
7453 gcc_assert (TREE_CODE (rhs) == ADDR_EXPR
7454 || TREE_CODE (rhs) == COMPONENT_REF
7455 || really_overloaded_fn (rhs)
7456 || (flag_ms_extensions && TREE_CODE (rhs) == FUNCTION_DECL));
7458 /* This should really only be used when attempting to distinguish
7459 what sort of a pointer to function we have. For now, any
7460 arithmetic operation which is not supported on pointers
7461 is rejected as an error. */
7463 switch (TREE_CODE (rhs))
7465 case COMPONENT_REF:
7467 tree member = TREE_OPERAND (rhs, 1);
7469 member = instantiate_type (lhstype, member, flags);
7470 if (member != error_mark_node
7471 && TREE_SIDE_EFFECTS (TREE_OPERAND (rhs, 0)))
7472 /* Do not lose object's side effects. */
7473 return build2 (COMPOUND_EXPR, TREE_TYPE (member),
7474 TREE_OPERAND (rhs, 0), member);
7475 return member;
7478 case OFFSET_REF:
7479 rhs = TREE_OPERAND (rhs, 1);
7480 if (BASELINK_P (rhs))
7481 return instantiate_type (lhstype, rhs, flags_in);
7483 /* This can happen if we are forming a pointer-to-member for a
7484 member template. */
7485 gcc_assert (TREE_CODE (rhs) == TEMPLATE_ID_EXPR);
7487 /* Fall through. */
7489 case TEMPLATE_ID_EXPR:
7491 tree fns = TREE_OPERAND (rhs, 0);
7492 tree args = TREE_OPERAND (rhs, 1);
7494 return
7495 resolve_address_of_overloaded_function (lhstype, fns, flags_in,
7496 /*template_only=*/true,
7497 args, access_path);
7500 case OVERLOAD:
7501 case FUNCTION_DECL:
7502 return
7503 resolve_address_of_overloaded_function (lhstype, rhs, flags_in,
7504 /*template_only=*/false,
7505 /*explicit_targs=*/NULL_TREE,
7506 access_path);
7508 case ADDR_EXPR:
7510 if (PTRMEM_OK_P (rhs))
7511 flags |= tf_ptrmem_ok;
7513 return instantiate_type (lhstype, TREE_OPERAND (rhs, 0), flags);
7516 case ERROR_MARK:
7517 return error_mark_node;
7519 default:
7520 gcc_unreachable ();
7522 return error_mark_node;
7525 /* Return the name of the virtual function pointer field
7526 (as an IDENTIFIER_NODE) for the given TYPE. Note that
7527 this may have to look back through base types to find the
7528 ultimate field name. (For single inheritance, these could
7529 all be the same name. Who knows for multiple inheritance). */
7531 static tree
7532 get_vfield_name (tree type)
7534 tree binfo, base_binfo;
7535 char *buf;
7537 for (binfo = TYPE_BINFO (type);
7538 BINFO_N_BASE_BINFOS (binfo);
7539 binfo = base_binfo)
7541 base_binfo = BINFO_BASE_BINFO (binfo, 0);
7543 if (BINFO_VIRTUAL_P (base_binfo)
7544 || !TYPE_CONTAINS_VPTR_P (BINFO_TYPE (base_binfo)))
7545 break;
7548 type = BINFO_TYPE (binfo);
7549 buf = (char *) alloca (sizeof (VFIELD_NAME_FORMAT)
7550 + TYPE_NAME_LENGTH (type) + 2);
7551 sprintf (buf, VFIELD_NAME_FORMAT,
7552 IDENTIFIER_POINTER (constructor_name (type)));
7553 return get_identifier (buf);
7556 void
7557 print_class_statistics (void)
7559 if (! GATHER_STATISTICS)
7560 return;
7562 fprintf (stderr, "convert_harshness = %d\n", n_convert_harshness);
7563 fprintf (stderr, "compute_conversion_costs = %d\n", n_compute_conversion_costs);
7564 if (n_vtables)
7566 fprintf (stderr, "vtables = %d; vtable searches = %d\n",
7567 n_vtables, n_vtable_searches);
7568 fprintf (stderr, "vtable entries = %d; vtable elems = %d\n",
7569 n_vtable_entries, n_vtable_elems);
7573 /* Build a dummy reference to ourselves so Derived::Base (and A::A) works,
7574 according to [class]:
7575 The class-name is also inserted
7576 into the scope of the class itself. For purposes of access checking,
7577 the inserted class name is treated as if it were a public member name. */
7579 void
7580 build_self_reference (void)
7582 tree name = constructor_name (current_class_type);
7583 tree value = build_lang_decl (TYPE_DECL, name, current_class_type);
7584 tree saved_cas;
7586 DECL_NONLOCAL (value) = 1;
7587 DECL_CONTEXT (value) = current_class_type;
7588 DECL_ARTIFICIAL (value) = 1;
7589 SET_DECL_SELF_REFERENCE_P (value);
7590 set_underlying_type (value);
7592 if (processing_template_decl)
7593 value = push_template_decl (value);
7595 saved_cas = current_access_specifier;
7596 current_access_specifier = access_public_node;
7597 finish_member_declaration (value);
7598 current_access_specifier = saved_cas;
7601 /* Returns 1 if TYPE contains only padding bytes. */
7604 is_empty_class (tree type)
7606 if (type == error_mark_node)
7607 return 0;
7609 if (! CLASS_TYPE_P (type))
7610 return 0;
7612 /* In G++ 3.2, whether or not a class was empty was determined by
7613 looking at its size. */
7614 if (abi_version_at_least (2))
7615 return CLASSTYPE_EMPTY_P (type);
7616 else
7617 return integer_zerop (CLASSTYPE_SIZE (type));
7620 /* Returns true if TYPE contains an empty class. */
7622 static bool
7623 contains_empty_class_p (tree type)
7625 if (is_empty_class (type))
7626 return true;
7627 if (CLASS_TYPE_P (type))
7629 tree field;
7630 tree binfo;
7631 tree base_binfo;
7632 int i;
7634 for (binfo = TYPE_BINFO (type), i = 0;
7635 BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
7636 if (contains_empty_class_p (BINFO_TYPE (base_binfo)))
7637 return true;
7638 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
7639 if (TREE_CODE (field) == FIELD_DECL
7640 && !DECL_ARTIFICIAL (field)
7641 && is_empty_class (TREE_TYPE (field)))
7642 return true;
7644 else if (TREE_CODE (type) == ARRAY_TYPE)
7645 return contains_empty_class_p (TREE_TYPE (type));
7646 return false;
7649 /* Returns true if TYPE contains no actual data, just various
7650 possible combinations of empty classes and possibly a vptr. */
7652 bool
7653 is_really_empty_class (tree type)
7655 if (CLASS_TYPE_P (type))
7657 tree field;
7658 tree binfo;
7659 tree base_binfo;
7660 int i;
7662 /* CLASSTYPE_EMPTY_P isn't set properly until the class is actually laid
7663 out, but we'd like to be able to check this before then. */
7664 if (COMPLETE_TYPE_P (type) && is_empty_class (type))
7665 return true;
7667 for (binfo = TYPE_BINFO (type), i = 0;
7668 BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
7669 if (!is_really_empty_class (BINFO_TYPE (base_binfo)))
7670 return false;
7671 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
7672 if (TREE_CODE (field) == FIELD_DECL
7673 && !DECL_ARTIFICIAL (field)
7674 && !is_really_empty_class (TREE_TYPE (field)))
7675 return false;
7676 return true;
7678 else if (TREE_CODE (type) == ARRAY_TYPE)
7679 return is_really_empty_class (TREE_TYPE (type));
7680 return false;
7683 /* Note that NAME was looked up while the current class was being
7684 defined and that the result of that lookup was DECL. */
7686 void
7687 maybe_note_name_used_in_class (tree name, tree decl)
7689 splay_tree names_used;
7691 /* If we're not defining a class, there's nothing to do. */
7692 if (!(innermost_scope_kind() == sk_class
7693 && TYPE_BEING_DEFINED (current_class_type)
7694 && !LAMBDA_TYPE_P (current_class_type)))
7695 return;
7697 /* If there's already a binding for this NAME, then we don't have
7698 anything to worry about. */
7699 if (lookup_member (current_class_type, name,
7700 /*protect=*/0, /*want_type=*/false, tf_warning_or_error))
7701 return;
7703 if (!current_class_stack[current_class_depth - 1].names_used)
7704 current_class_stack[current_class_depth - 1].names_used
7705 = splay_tree_new (splay_tree_compare_pointers, 0, 0);
7706 names_used = current_class_stack[current_class_depth - 1].names_used;
7708 splay_tree_insert (names_used,
7709 (splay_tree_key) name,
7710 (splay_tree_value) decl);
7713 /* Note that NAME was declared (as DECL) in the current class. Check
7714 to see that the declaration is valid. */
7716 void
7717 note_name_declared_in_class (tree name, tree decl)
7719 splay_tree names_used;
7720 splay_tree_node n;
7722 /* Look to see if we ever used this name. */
7723 names_used
7724 = current_class_stack[current_class_depth - 1].names_used;
7725 if (!names_used)
7726 return;
7727 /* The C language allows members to be declared with a type of the same
7728 name, and the C++ standard says this diagnostic is not required. So
7729 allow it in extern "C" blocks unless predantic is specified.
7730 Allow it in all cases if -ms-extensions is specified. */
7731 if ((!pedantic && current_lang_name == lang_name_c)
7732 || flag_ms_extensions)
7733 return;
7734 n = splay_tree_lookup (names_used, (splay_tree_key) name);
7735 if (n)
7737 /* [basic.scope.class]
7739 A name N used in a class S shall refer to the same declaration
7740 in its context and when re-evaluated in the completed scope of
7741 S. */
7742 permerror (input_location, "declaration of %q#D", decl);
7743 permerror (input_location, "changes meaning of %qD from %q+#D",
7744 DECL_NAME (OVL_CURRENT (decl)), (tree) n->value);
7748 /* Returns the VAR_DECL for the complete vtable associated with BINFO.
7749 Secondary vtables are merged with primary vtables; this function
7750 will return the VAR_DECL for the primary vtable. */
7752 tree
7753 get_vtbl_decl_for_binfo (tree binfo)
7755 tree decl;
7757 decl = BINFO_VTABLE (binfo);
7758 if (decl && TREE_CODE (decl) == POINTER_PLUS_EXPR)
7760 gcc_assert (TREE_CODE (TREE_OPERAND (decl, 0)) == ADDR_EXPR);
7761 decl = TREE_OPERAND (TREE_OPERAND (decl, 0), 0);
7763 if (decl)
7764 gcc_assert (TREE_CODE (decl) == VAR_DECL);
7765 return decl;
7769 /* Returns the binfo for the primary base of BINFO. If the resulting
7770 BINFO is a virtual base, and it is inherited elsewhere in the
7771 hierarchy, then the returned binfo might not be the primary base of
7772 BINFO in the complete object. Check BINFO_PRIMARY_P or
7773 BINFO_LOST_PRIMARY_P to be sure. */
7775 static tree
7776 get_primary_binfo (tree binfo)
7778 tree primary_base;
7780 primary_base = CLASSTYPE_PRIMARY_BINFO (BINFO_TYPE (binfo));
7781 if (!primary_base)
7782 return NULL_TREE;
7784 return copied_binfo (primary_base, binfo);
7787 /* If INDENTED_P is zero, indent to INDENT. Return nonzero. */
7789 static int
7790 maybe_indent_hierarchy (FILE * stream, int indent, int indented_p)
7792 if (!indented_p)
7793 fprintf (stream, "%*s", indent, "");
7794 return 1;
7797 /* Dump the offsets of all the bases rooted at BINFO to STREAM.
7798 INDENT should be zero when called from the top level; it is
7799 incremented recursively. IGO indicates the next expected BINFO in
7800 inheritance graph ordering. */
7802 static tree
7803 dump_class_hierarchy_r (FILE *stream,
7804 int flags,
7805 tree binfo,
7806 tree igo,
7807 int indent)
7809 int indented = 0;
7810 tree base_binfo;
7811 int i;
7813 indented = maybe_indent_hierarchy (stream, indent, 0);
7814 fprintf (stream, "%s (0x%lx) ",
7815 type_as_string (BINFO_TYPE (binfo), TFF_PLAIN_IDENTIFIER),
7816 (unsigned long) binfo);
7817 if (binfo != igo)
7819 fprintf (stream, "alternative-path\n");
7820 return igo;
7822 igo = TREE_CHAIN (binfo);
7824 fprintf (stream, HOST_WIDE_INT_PRINT_DEC,
7825 tree_low_cst (BINFO_OFFSET (binfo), 0));
7826 if (is_empty_class (BINFO_TYPE (binfo)))
7827 fprintf (stream, " empty");
7828 else if (CLASSTYPE_NEARLY_EMPTY_P (BINFO_TYPE (binfo)))
7829 fprintf (stream, " nearly-empty");
7830 if (BINFO_VIRTUAL_P (binfo))
7831 fprintf (stream, " virtual");
7832 fprintf (stream, "\n");
7834 indented = 0;
7835 if (BINFO_PRIMARY_P (binfo))
7837 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
7838 fprintf (stream, " primary-for %s (0x%lx)",
7839 type_as_string (BINFO_TYPE (BINFO_INHERITANCE_CHAIN (binfo)),
7840 TFF_PLAIN_IDENTIFIER),
7841 (unsigned long)BINFO_INHERITANCE_CHAIN (binfo));
7843 if (BINFO_LOST_PRIMARY_P (binfo))
7845 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
7846 fprintf (stream, " lost-primary");
7848 if (indented)
7849 fprintf (stream, "\n");
7851 if (!(flags & TDF_SLIM))
7853 int indented = 0;
7855 if (BINFO_SUBVTT_INDEX (binfo))
7857 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
7858 fprintf (stream, " subvttidx=%s",
7859 expr_as_string (BINFO_SUBVTT_INDEX (binfo),
7860 TFF_PLAIN_IDENTIFIER));
7862 if (BINFO_VPTR_INDEX (binfo))
7864 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
7865 fprintf (stream, " vptridx=%s",
7866 expr_as_string (BINFO_VPTR_INDEX (binfo),
7867 TFF_PLAIN_IDENTIFIER));
7869 if (BINFO_VPTR_FIELD (binfo))
7871 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
7872 fprintf (stream, " vbaseoffset=%s",
7873 expr_as_string (BINFO_VPTR_FIELD (binfo),
7874 TFF_PLAIN_IDENTIFIER));
7876 if (BINFO_VTABLE (binfo))
7878 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
7879 fprintf (stream, " vptr=%s",
7880 expr_as_string (BINFO_VTABLE (binfo),
7881 TFF_PLAIN_IDENTIFIER));
7884 if (indented)
7885 fprintf (stream, "\n");
7888 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
7889 igo = dump_class_hierarchy_r (stream, flags, base_binfo, igo, indent + 2);
7891 return igo;
7894 /* Dump the BINFO hierarchy for T. */
7896 static void
7897 dump_class_hierarchy_1 (FILE *stream, int flags, tree t)
7899 fprintf (stream, "Class %s\n", type_as_string (t, TFF_PLAIN_IDENTIFIER));
7900 fprintf (stream, " size=%lu align=%lu\n",
7901 (unsigned long)(tree_low_cst (TYPE_SIZE (t), 0) / BITS_PER_UNIT),
7902 (unsigned long)(TYPE_ALIGN (t) / BITS_PER_UNIT));
7903 fprintf (stream, " base size=%lu base align=%lu\n",
7904 (unsigned long)(tree_low_cst (TYPE_SIZE (CLASSTYPE_AS_BASE (t)), 0)
7905 / BITS_PER_UNIT),
7906 (unsigned long)(TYPE_ALIGN (CLASSTYPE_AS_BASE (t))
7907 / BITS_PER_UNIT));
7908 dump_class_hierarchy_r (stream, flags, TYPE_BINFO (t), TYPE_BINFO (t), 0);
7909 fprintf (stream, "\n");
7912 /* Debug interface to hierarchy dumping. */
7914 void
7915 debug_class (tree t)
7917 dump_class_hierarchy_1 (stderr, TDF_SLIM, t);
7920 static void
7921 dump_class_hierarchy (tree t)
7923 int flags;
7924 FILE *stream = dump_begin (TDI_class, &flags);
7926 if (stream)
7928 dump_class_hierarchy_1 (stream, flags, t);
7929 dump_end (TDI_class, stream);
7933 static void
7934 dump_array (FILE * stream, tree decl)
7936 tree value;
7937 unsigned HOST_WIDE_INT ix;
7938 HOST_WIDE_INT elt;
7939 tree size = TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (decl)));
7941 elt = (tree_low_cst (TYPE_SIZE (TREE_TYPE (TREE_TYPE (decl))), 0)
7942 / BITS_PER_UNIT);
7943 fprintf (stream, "%s:", decl_as_string (decl, TFF_PLAIN_IDENTIFIER));
7944 fprintf (stream, " %s entries",
7945 expr_as_string (size_binop (PLUS_EXPR, size, size_one_node),
7946 TFF_PLAIN_IDENTIFIER));
7947 fprintf (stream, "\n");
7949 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (DECL_INITIAL (decl)),
7950 ix, value)
7951 fprintf (stream, "%-4ld %s\n", (long)(ix * elt),
7952 expr_as_string (value, TFF_PLAIN_IDENTIFIER));
7955 static void
7956 dump_vtable (tree t, tree binfo, tree vtable)
7958 int flags;
7959 FILE *stream = dump_begin (TDI_class, &flags);
7961 if (!stream)
7962 return;
7964 if (!(flags & TDF_SLIM))
7966 int ctor_vtbl_p = TYPE_BINFO (t) != binfo;
7968 fprintf (stream, "%s for %s",
7969 ctor_vtbl_p ? "Construction vtable" : "Vtable",
7970 type_as_string (BINFO_TYPE (binfo), TFF_PLAIN_IDENTIFIER));
7971 if (ctor_vtbl_p)
7973 if (!BINFO_VIRTUAL_P (binfo))
7974 fprintf (stream, " (0x%lx instance)", (unsigned long)binfo);
7975 fprintf (stream, " in %s", type_as_string (t, TFF_PLAIN_IDENTIFIER));
7977 fprintf (stream, "\n");
7978 dump_array (stream, vtable);
7979 fprintf (stream, "\n");
7982 dump_end (TDI_class, stream);
7985 static void
7986 dump_vtt (tree t, tree vtt)
7988 int flags;
7989 FILE *stream = dump_begin (TDI_class, &flags);
7991 if (!stream)
7992 return;
7994 if (!(flags & TDF_SLIM))
7996 fprintf (stream, "VTT for %s\n",
7997 type_as_string (t, TFF_PLAIN_IDENTIFIER));
7998 dump_array (stream, vtt);
7999 fprintf (stream, "\n");
8002 dump_end (TDI_class, stream);
8005 /* Dump a function or thunk and its thunkees. */
8007 static void
8008 dump_thunk (FILE *stream, int indent, tree thunk)
8010 static const char spaces[] = " ";
8011 tree name = DECL_NAME (thunk);
8012 tree thunks;
8014 fprintf (stream, "%.*s%p %s %s", indent, spaces,
8015 (void *)thunk,
8016 !DECL_THUNK_P (thunk) ? "function"
8017 : DECL_THIS_THUNK_P (thunk) ? "this-thunk" : "covariant-thunk",
8018 name ? IDENTIFIER_POINTER (name) : "<unset>");
8019 if (DECL_THUNK_P (thunk))
8021 HOST_WIDE_INT fixed_adjust = THUNK_FIXED_OFFSET (thunk);
8022 tree virtual_adjust = THUNK_VIRTUAL_OFFSET (thunk);
8024 fprintf (stream, " fixed=" HOST_WIDE_INT_PRINT_DEC, fixed_adjust);
8025 if (!virtual_adjust)
8026 /*NOP*/;
8027 else if (DECL_THIS_THUNK_P (thunk))
8028 fprintf (stream, " vcall=" HOST_WIDE_INT_PRINT_DEC,
8029 tree_low_cst (virtual_adjust, 0));
8030 else
8031 fprintf (stream, " vbase=" HOST_WIDE_INT_PRINT_DEC "(%s)",
8032 tree_low_cst (BINFO_VPTR_FIELD (virtual_adjust), 0),
8033 type_as_string (BINFO_TYPE (virtual_adjust), TFF_SCOPE));
8034 if (THUNK_ALIAS (thunk))
8035 fprintf (stream, " alias to %p", (void *)THUNK_ALIAS (thunk));
8037 fprintf (stream, "\n");
8038 for (thunks = DECL_THUNKS (thunk); thunks; thunks = TREE_CHAIN (thunks))
8039 dump_thunk (stream, indent + 2, thunks);
8042 /* Dump the thunks for FN. */
8044 void
8045 debug_thunks (tree fn)
8047 dump_thunk (stderr, 0, fn);
8050 /* Virtual function table initialization. */
8052 /* Create all the necessary vtables for T and its base classes. */
8054 static void
8055 finish_vtbls (tree t)
8057 tree vbase;
8058 vec<constructor_elt, va_gc> *v = NULL;
8059 tree vtable = BINFO_VTABLE (TYPE_BINFO (t));
8061 /* We lay out the primary and secondary vtables in one contiguous
8062 vtable. The primary vtable is first, followed by the non-virtual
8063 secondary vtables in inheritance graph order. */
8064 accumulate_vtbl_inits (TYPE_BINFO (t), TYPE_BINFO (t), TYPE_BINFO (t),
8065 vtable, t, &v);
8067 /* Then come the virtual bases, also in inheritance graph order. */
8068 for (vbase = TYPE_BINFO (t); vbase; vbase = TREE_CHAIN (vbase))
8070 if (!BINFO_VIRTUAL_P (vbase))
8071 continue;
8072 accumulate_vtbl_inits (vbase, vbase, TYPE_BINFO (t), vtable, t, &v);
8075 if (BINFO_VTABLE (TYPE_BINFO (t)))
8076 initialize_vtable (TYPE_BINFO (t), v);
8079 /* Initialize the vtable for BINFO with the INITS. */
8081 static void
8082 initialize_vtable (tree binfo, vec<constructor_elt, va_gc> *inits)
8084 tree decl;
8086 layout_vtable_decl (binfo, vec_safe_length (inits));
8087 decl = get_vtbl_decl_for_binfo (binfo);
8088 initialize_artificial_var (decl, inits);
8089 dump_vtable (BINFO_TYPE (binfo), binfo, decl);
8092 /* Build the VTT (virtual table table) for T.
8093 A class requires a VTT if it has virtual bases.
8095 This holds
8096 1 - primary virtual pointer for complete object T
8097 2 - secondary VTTs for each direct non-virtual base of T which requires a
8099 3 - secondary virtual pointers for each direct or indirect base of T which
8100 has virtual bases or is reachable via a virtual path from T.
8101 4 - secondary VTTs for each direct or indirect virtual base of T.
8103 Secondary VTTs look like complete object VTTs without part 4. */
8105 static void
8106 build_vtt (tree t)
8108 tree type;
8109 tree vtt;
8110 tree index;
8111 vec<constructor_elt, va_gc> *inits;
8113 /* Build up the initializers for the VTT. */
8114 inits = NULL;
8115 index = size_zero_node;
8116 build_vtt_inits (TYPE_BINFO (t), t, &inits, &index);
8118 /* If we didn't need a VTT, we're done. */
8119 if (!inits)
8120 return;
8122 /* Figure out the type of the VTT. */
8123 type = build_array_of_n_type (const_ptr_type_node,
8124 inits->length ());
8126 /* Now, build the VTT object itself. */
8127 vtt = build_vtable (t, mangle_vtt_for_type (t), type);
8128 initialize_artificial_var (vtt, inits);
8129 /* Add the VTT to the vtables list. */
8130 DECL_CHAIN (vtt) = DECL_CHAIN (CLASSTYPE_VTABLES (t));
8131 DECL_CHAIN (CLASSTYPE_VTABLES (t)) = vtt;
8133 dump_vtt (t, vtt);
8136 /* When building a secondary VTT, BINFO_VTABLE is set to a TREE_LIST with
8137 PURPOSE the RTTI_BINFO, VALUE the real vtable pointer for this binfo,
8138 and CHAIN the vtable pointer for this binfo after construction is
8139 complete. VALUE can also be another BINFO, in which case we recurse. */
8141 static tree
8142 binfo_ctor_vtable (tree binfo)
8144 tree vt;
8146 while (1)
8148 vt = BINFO_VTABLE (binfo);
8149 if (TREE_CODE (vt) == TREE_LIST)
8150 vt = TREE_VALUE (vt);
8151 if (TREE_CODE (vt) == TREE_BINFO)
8152 binfo = vt;
8153 else
8154 break;
8157 return vt;
8160 /* Data for secondary VTT initialization. */
8161 typedef struct secondary_vptr_vtt_init_data_s
8163 /* Is this the primary VTT? */
8164 bool top_level_p;
8166 /* Current index into the VTT. */
8167 tree index;
8169 /* Vector of initializers built up. */
8170 vec<constructor_elt, va_gc> *inits;
8172 /* The type being constructed by this secondary VTT. */
8173 tree type_being_constructed;
8174 } secondary_vptr_vtt_init_data;
8176 /* Recursively build the VTT-initializer for BINFO (which is in the
8177 hierarchy dominated by T). INITS points to the end of the initializer
8178 list to date. INDEX is the VTT index where the next element will be
8179 replaced. Iff BINFO is the binfo for T, this is the top level VTT (i.e.
8180 not a subvtt for some base of T). When that is so, we emit the sub-VTTs
8181 for virtual bases of T. When it is not so, we build the constructor
8182 vtables for the BINFO-in-T variant. */
8184 static void
8185 build_vtt_inits (tree binfo, tree t, vec<constructor_elt, va_gc> **inits,
8186 tree *index)
8188 int i;
8189 tree b;
8190 tree init;
8191 secondary_vptr_vtt_init_data data;
8192 int top_level_p = SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), t);
8194 /* We only need VTTs for subobjects with virtual bases. */
8195 if (!CLASSTYPE_VBASECLASSES (BINFO_TYPE (binfo)))
8196 return;
8198 /* We need to use a construction vtable if this is not the primary
8199 VTT. */
8200 if (!top_level_p)
8202 build_ctor_vtbl_group (binfo, t);
8204 /* Record the offset in the VTT where this sub-VTT can be found. */
8205 BINFO_SUBVTT_INDEX (binfo) = *index;
8208 /* Add the address of the primary vtable for the complete object. */
8209 init = binfo_ctor_vtable (binfo);
8210 CONSTRUCTOR_APPEND_ELT (*inits, NULL_TREE, init);
8211 if (top_level_p)
8213 gcc_assert (!BINFO_VPTR_INDEX (binfo));
8214 BINFO_VPTR_INDEX (binfo) = *index;
8216 *index = size_binop (PLUS_EXPR, *index, TYPE_SIZE_UNIT (ptr_type_node));
8218 /* Recursively add the secondary VTTs for non-virtual bases. */
8219 for (i = 0; BINFO_BASE_ITERATE (binfo, i, b); ++i)
8220 if (!BINFO_VIRTUAL_P (b))
8221 build_vtt_inits (b, t, inits, index);
8223 /* Add secondary virtual pointers for all subobjects of BINFO with
8224 either virtual bases or reachable along a virtual path, except
8225 subobjects that are non-virtual primary bases. */
8226 data.top_level_p = top_level_p;
8227 data.index = *index;
8228 data.inits = *inits;
8229 data.type_being_constructed = BINFO_TYPE (binfo);
8231 dfs_walk_once (binfo, dfs_build_secondary_vptr_vtt_inits, NULL, &data);
8233 *index = data.index;
8235 /* data.inits might have grown as we added secondary virtual pointers.
8236 Make sure our caller knows about the new vector. */
8237 *inits = data.inits;
8239 if (top_level_p)
8240 /* Add the secondary VTTs for virtual bases in inheritance graph
8241 order. */
8242 for (b = TYPE_BINFO (BINFO_TYPE (binfo)); b; b = TREE_CHAIN (b))
8244 if (!BINFO_VIRTUAL_P (b))
8245 continue;
8247 build_vtt_inits (b, t, inits, index);
8249 else
8250 /* Remove the ctor vtables we created. */
8251 dfs_walk_all (binfo, dfs_fixup_binfo_vtbls, NULL, binfo);
8254 /* Called from build_vtt_inits via dfs_walk. BINFO is the binfo for the base
8255 in most derived. DATA is a SECONDARY_VPTR_VTT_INIT_DATA structure. */
8257 static tree
8258 dfs_build_secondary_vptr_vtt_inits (tree binfo, void *data_)
8260 secondary_vptr_vtt_init_data *data = (secondary_vptr_vtt_init_data *)data_;
8262 /* We don't care about bases that don't have vtables. */
8263 if (!TYPE_VFIELD (BINFO_TYPE (binfo)))
8264 return dfs_skip_bases;
8266 /* We're only interested in proper subobjects of the type being
8267 constructed. */
8268 if (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), data->type_being_constructed))
8269 return NULL_TREE;
8271 /* We're only interested in bases with virtual bases or reachable
8272 via a virtual path from the type being constructed. */
8273 if (!(CLASSTYPE_VBASECLASSES (BINFO_TYPE (binfo))
8274 || binfo_via_virtual (binfo, data->type_being_constructed)))
8275 return dfs_skip_bases;
8277 /* We're not interested in non-virtual primary bases. */
8278 if (!BINFO_VIRTUAL_P (binfo) && BINFO_PRIMARY_P (binfo))
8279 return NULL_TREE;
8281 /* Record the index where this secondary vptr can be found. */
8282 if (data->top_level_p)
8284 gcc_assert (!BINFO_VPTR_INDEX (binfo));
8285 BINFO_VPTR_INDEX (binfo) = data->index;
8287 if (BINFO_VIRTUAL_P (binfo))
8289 /* It's a primary virtual base, and this is not a
8290 construction vtable. Find the base this is primary of in
8291 the inheritance graph, and use that base's vtable
8292 now. */
8293 while (BINFO_PRIMARY_P (binfo))
8294 binfo = BINFO_INHERITANCE_CHAIN (binfo);
8298 /* Add the initializer for the secondary vptr itself. */
8299 CONSTRUCTOR_APPEND_ELT (data->inits, NULL_TREE, binfo_ctor_vtable (binfo));
8301 /* Advance the vtt index. */
8302 data->index = size_binop (PLUS_EXPR, data->index,
8303 TYPE_SIZE_UNIT (ptr_type_node));
8305 return NULL_TREE;
8308 /* Called from build_vtt_inits via dfs_walk. After building
8309 constructor vtables and generating the sub-vtt from them, we need
8310 to restore the BINFO_VTABLES that were scribbled on. DATA is the
8311 binfo of the base whose sub vtt was generated. */
8313 static tree
8314 dfs_fixup_binfo_vtbls (tree binfo, void* data)
8316 tree vtable = BINFO_VTABLE (binfo);
8318 if (!TYPE_CONTAINS_VPTR_P (BINFO_TYPE (binfo)))
8319 /* If this class has no vtable, none of its bases do. */
8320 return dfs_skip_bases;
8322 if (!vtable)
8323 /* This might be a primary base, so have no vtable in this
8324 hierarchy. */
8325 return NULL_TREE;
8327 /* If we scribbled the construction vtable vptr into BINFO, clear it
8328 out now. */
8329 if (TREE_CODE (vtable) == TREE_LIST
8330 && (TREE_PURPOSE (vtable) == (tree) data))
8331 BINFO_VTABLE (binfo) = TREE_CHAIN (vtable);
8333 return NULL_TREE;
8336 /* Build the construction vtable group for BINFO which is in the
8337 hierarchy dominated by T. */
8339 static void
8340 build_ctor_vtbl_group (tree binfo, tree t)
8342 tree type;
8343 tree vtbl;
8344 tree id;
8345 tree vbase;
8346 vec<constructor_elt, va_gc> *v;
8348 /* See if we've already created this construction vtable group. */
8349 id = mangle_ctor_vtbl_for_type (t, binfo);
8350 if (IDENTIFIER_GLOBAL_VALUE (id))
8351 return;
8353 gcc_assert (!SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), t));
8354 /* Build a version of VTBL (with the wrong type) for use in
8355 constructing the addresses of secondary vtables in the
8356 construction vtable group. */
8357 vtbl = build_vtable (t, id, ptr_type_node);
8358 DECL_CONSTRUCTION_VTABLE_P (vtbl) = 1;
8360 v = NULL;
8361 accumulate_vtbl_inits (binfo, TYPE_BINFO (TREE_TYPE (binfo)),
8362 binfo, vtbl, t, &v);
8364 /* Add the vtables for each of our virtual bases using the vbase in T
8365 binfo. */
8366 for (vbase = TYPE_BINFO (BINFO_TYPE (binfo));
8367 vbase;
8368 vbase = TREE_CHAIN (vbase))
8370 tree b;
8372 if (!BINFO_VIRTUAL_P (vbase))
8373 continue;
8374 b = copied_binfo (vbase, binfo);
8376 accumulate_vtbl_inits (b, vbase, binfo, vtbl, t, &v);
8379 /* Figure out the type of the construction vtable. */
8380 type = build_array_of_n_type (vtable_entry_type, v->length ());
8381 layout_type (type);
8382 TREE_TYPE (vtbl) = type;
8383 DECL_SIZE (vtbl) = DECL_SIZE_UNIT (vtbl) = NULL_TREE;
8384 layout_decl (vtbl, 0);
8386 /* Initialize the construction vtable. */
8387 CLASSTYPE_VTABLES (t) = chainon (CLASSTYPE_VTABLES (t), vtbl);
8388 initialize_artificial_var (vtbl, v);
8389 dump_vtable (t, binfo, vtbl);
8392 /* Add the vtbl initializers for BINFO (and its bases other than
8393 non-virtual primaries) to the list of INITS. BINFO is in the
8394 hierarchy dominated by T. RTTI_BINFO is the binfo within T of
8395 the constructor the vtbl inits should be accumulated for. (If this
8396 is the complete object vtbl then RTTI_BINFO will be TYPE_BINFO (T).)
8397 ORIG_BINFO is the binfo for this object within BINFO_TYPE (RTTI_BINFO).
8398 BINFO is the active base equivalent of ORIG_BINFO in the inheritance
8399 graph of T. Both BINFO and ORIG_BINFO will have the same BINFO_TYPE,
8400 but are not necessarily the same in terms of layout. */
8402 static void
8403 accumulate_vtbl_inits (tree binfo,
8404 tree orig_binfo,
8405 tree rtti_binfo,
8406 tree vtbl,
8407 tree t,
8408 vec<constructor_elt, va_gc> **inits)
8410 int i;
8411 tree base_binfo;
8412 int ctor_vtbl_p = !SAME_BINFO_TYPE_P (BINFO_TYPE (rtti_binfo), t);
8414 gcc_assert (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), BINFO_TYPE (orig_binfo)));
8416 /* If it doesn't have a vptr, we don't do anything. */
8417 if (!TYPE_CONTAINS_VPTR_P (BINFO_TYPE (binfo)))
8418 return;
8420 /* If we're building a construction vtable, we're not interested in
8421 subobjects that don't require construction vtables. */
8422 if (ctor_vtbl_p
8423 && !CLASSTYPE_VBASECLASSES (BINFO_TYPE (binfo))
8424 && !binfo_via_virtual (orig_binfo, BINFO_TYPE (rtti_binfo)))
8425 return;
8427 /* Build the initializers for the BINFO-in-T vtable. */
8428 dfs_accumulate_vtbl_inits (binfo, orig_binfo, rtti_binfo, vtbl, t, inits);
8430 /* Walk the BINFO and its bases. We walk in preorder so that as we
8431 initialize each vtable we can figure out at what offset the
8432 secondary vtable lies from the primary vtable. We can't use
8433 dfs_walk here because we need to iterate through bases of BINFO
8434 and RTTI_BINFO simultaneously. */
8435 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
8437 /* Skip virtual bases. */
8438 if (BINFO_VIRTUAL_P (base_binfo))
8439 continue;
8440 accumulate_vtbl_inits (base_binfo,
8441 BINFO_BASE_BINFO (orig_binfo, i),
8442 rtti_binfo, vtbl, t,
8443 inits);
8447 /* Called from accumulate_vtbl_inits. Adds the initializers for the
8448 BINFO vtable to L. */
8450 static void
8451 dfs_accumulate_vtbl_inits (tree binfo,
8452 tree orig_binfo,
8453 tree rtti_binfo,
8454 tree orig_vtbl,
8455 tree t,
8456 vec<constructor_elt, va_gc> **l)
8458 tree vtbl = NULL_TREE;
8459 int ctor_vtbl_p = !SAME_BINFO_TYPE_P (BINFO_TYPE (rtti_binfo), t);
8460 int n_inits;
8462 if (ctor_vtbl_p
8463 && BINFO_VIRTUAL_P (orig_binfo) && BINFO_PRIMARY_P (orig_binfo))
8465 /* In the hierarchy of BINFO_TYPE (RTTI_BINFO), this is a
8466 primary virtual base. If it is not the same primary in
8467 the hierarchy of T, we'll need to generate a ctor vtable
8468 for it, to place at its location in T. If it is the same
8469 primary, we still need a VTT entry for the vtable, but it
8470 should point to the ctor vtable for the base it is a
8471 primary for within the sub-hierarchy of RTTI_BINFO.
8473 There are three possible cases:
8475 1) We are in the same place.
8476 2) We are a primary base within a lost primary virtual base of
8477 RTTI_BINFO.
8478 3) We are primary to something not a base of RTTI_BINFO. */
8480 tree b;
8481 tree last = NULL_TREE;
8483 /* First, look through the bases we are primary to for RTTI_BINFO
8484 or a virtual base. */
8485 b = binfo;
8486 while (BINFO_PRIMARY_P (b))
8488 b = BINFO_INHERITANCE_CHAIN (b);
8489 last = b;
8490 if (BINFO_VIRTUAL_P (b) || b == rtti_binfo)
8491 goto found;
8493 /* If we run out of primary links, keep looking down our
8494 inheritance chain; we might be an indirect primary. */
8495 for (b = last; b; b = BINFO_INHERITANCE_CHAIN (b))
8496 if (BINFO_VIRTUAL_P (b) || b == rtti_binfo)
8497 break;
8498 found:
8500 /* If we found RTTI_BINFO, this is case 1. If we found a virtual
8501 base B and it is a base of RTTI_BINFO, this is case 2. In
8502 either case, we share our vtable with LAST, i.e. the
8503 derived-most base within B of which we are a primary. */
8504 if (b == rtti_binfo
8505 || (b && binfo_for_vbase (BINFO_TYPE (b), BINFO_TYPE (rtti_binfo))))
8506 /* Just set our BINFO_VTABLE to point to LAST, as we may not have
8507 set LAST's BINFO_VTABLE yet. We'll extract the actual vptr in
8508 binfo_ctor_vtable after everything's been set up. */
8509 vtbl = last;
8511 /* Otherwise, this is case 3 and we get our own. */
8513 else if (!BINFO_NEW_VTABLE_MARKED (orig_binfo))
8514 return;
8516 n_inits = vec_safe_length (*l);
8518 if (!vtbl)
8520 tree index;
8521 int non_fn_entries;
8523 /* Add the initializer for this vtable. */
8524 build_vtbl_initializer (binfo, orig_binfo, t, rtti_binfo,
8525 &non_fn_entries, l);
8527 /* Figure out the position to which the VPTR should point. */
8528 vtbl = build1 (ADDR_EXPR, vtbl_ptr_type_node, orig_vtbl);
8529 index = size_binop (MULT_EXPR,
8530 TYPE_SIZE_UNIT (vtable_entry_type),
8531 size_int (non_fn_entries + n_inits));
8532 vtbl = fold_build_pointer_plus (vtbl, index);
8535 if (ctor_vtbl_p)
8536 /* For a construction vtable, we can't overwrite BINFO_VTABLE.
8537 So, we make a TREE_LIST. Later, dfs_fixup_binfo_vtbls will
8538 straighten this out. */
8539 BINFO_VTABLE (binfo) = tree_cons (rtti_binfo, vtbl, BINFO_VTABLE (binfo));
8540 else if (BINFO_PRIMARY_P (binfo) && BINFO_VIRTUAL_P (binfo))
8541 /* Throw away any unneeded intializers. */
8542 (*l)->truncate (n_inits);
8543 else
8544 /* For an ordinary vtable, set BINFO_VTABLE. */
8545 BINFO_VTABLE (binfo) = vtbl;
8548 static GTY(()) tree abort_fndecl_addr;
8550 /* Construct the initializer for BINFO's virtual function table. BINFO
8551 is part of the hierarchy dominated by T. If we're building a
8552 construction vtable, the ORIG_BINFO is the binfo we should use to
8553 find the actual function pointers to put in the vtable - but they
8554 can be overridden on the path to most-derived in the graph that
8555 ORIG_BINFO belongs. Otherwise,
8556 ORIG_BINFO should be the same as BINFO. The RTTI_BINFO is the
8557 BINFO that should be indicated by the RTTI information in the
8558 vtable; it will be a base class of T, rather than T itself, if we
8559 are building a construction vtable.
8561 The value returned is a TREE_LIST suitable for wrapping in a
8562 CONSTRUCTOR to use as the DECL_INITIAL for a vtable. If
8563 NON_FN_ENTRIES_P is not NULL, *NON_FN_ENTRIES_P is set to the
8564 number of non-function entries in the vtable.
8566 It might seem that this function should never be called with a
8567 BINFO for which BINFO_PRIMARY_P holds, the vtable for such a
8568 base is always subsumed by a derived class vtable. However, when
8569 we are building construction vtables, we do build vtables for
8570 primary bases; we need these while the primary base is being
8571 constructed. */
8573 static void
8574 build_vtbl_initializer (tree binfo,
8575 tree orig_binfo,
8576 tree t,
8577 tree rtti_binfo,
8578 int* non_fn_entries_p,
8579 vec<constructor_elt, va_gc> **inits)
8581 tree v;
8582 vtbl_init_data vid;
8583 unsigned ix, jx;
8584 tree vbinfo;
8585 vec<tree, va_gc> *vbases;
8586 constructor_elt *e;
8588 /* Initialize VID. */
8589 memset (&vid, 0, sizeof (vid));
8590 vid.binfo = binfo;
8591 vid.derived = t;
8592 vid.rtti_binfo = rtti_binfo;
8593 vid.primary_vtbl_p = SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), t);
8594 vid.ctor_vtbl_p = !SAME_BINFO_TYPE_P (BINFO_TYPE (rtti_binfo), t);
8595 vid.generate_vcall_entries = true;
8596 /* The first vbase or vcall offset is at index -3 in the vtable. */
8597 vid.index = ssize_int(-3 * TARGET_VTABLE_DATA_ENTRY_DISTANCE);
8599 /* Add entries to the vtable for RTTI. */
8600 build_rtti_vtbl_entries (binfo, &vid);
8602 /* Create an array for keeping track of the functions we've
8603 processed. When we see multiple functions with the same
8604 signature, we share the vcall offsets. */
8605 vec_alloc (vid.fns, 32);
8606 /* Add the vcall and vbase offset entries. */
8607 build_vcall_and_vbase_vtbl_entries (binfo, &vid);
8609 /* Clear BINFO_VTABLE_PATH_MARKED; it's set by
8610 build_vbase_offset_vtbl_entries. */
8611 for (vbases = CLASSTYPE_VBASECLASSES (t), ix = 0;
8612 vec_safe_iterate (vbases, ix, &vbinfo); ix++)
8613 BINFO_VTABLE_PATH_MARKED (vbinfo) = 0;
8615 /* If the target requires padding between data entries, add that now. */
8616 if (TARGET_VTABLE_DATA_ENTRY_DISTANCE > 1)
8618 int n_entries = vec_safe_length (vid.inits);
8620 vec_safe_grow (vid.inits, TARGET_VTABLE_DATA_ENTRY_DISTANCE * n_entries);
8622 /* Move data entries into their new positions and add padding
8623 after the new positions. Iterate backwards so we don't
8624 overwrite entries that we would need to process later. */
8625 for (ix = n_entries - 1;
8626 vid.inits->iterate (ix, &e);
8627 ix--)
8629 int j;
8630 int new_position = (TARGET_VTABLE_DATA_ENTRY_DISTANCE * ix
8631 + (TARGET_VTABLE_DATA_ENTRY_DISTANCE - 1));
8633 (*vid.inits)[new_position] = *e;
8635 for (j = 1; j < TARGET_VTABLE_DATA_ENTRY_DISTANCE; ++j)
8637 constructor_elt *f = &(*vid.inits)[new_position - j];
8638 f->index = NULL_TREE;
8639 f->value = build1 (NOP_EXPR, vtable_entry_type,
8640 null_pointer_node);
8645 if (non_fn_entries_p)
8646 *non_fn_entries_p = vec_safe_length (vid.inits);
8648 /* The initializers for virtual functions were built up in reverse
8649 order. Straighten them out and add them to the running list in one
8650 step. */
8651 jx = vec_safe_length (*inits);
8652 vec_safe_grow (*inits, jx + vid.inits->length ());
8654 for (ix = vid.inits->length () - 1;
8655 vid.inits->iterate (ix, &e);
8656 ix--, jx++)
8657 (**inits)[jx] = *e;
8659 /* Go through all the ordinary virtual functions, building up
8660 initializers. */
8661 for (v = BINFO_VIRTUALS (orig_binfo); v; v = TREE_CHAIN (v))
8663 tree delta;
8664 tree vcall_index;
8665 tree fn, fn_original;
8666 tree init = NULL_TREE;
8668 fn = BV_FN (v);
8669 fn_original = fn;
8670 if (DECL_THUNK_P (fn))
8672 if (!DECL_NAME (fn))
8673 finish_thunk (fn);
8674 if (THUNK_ALIAS (fn))
8676 fn = THUNK_ALIAS (fn);
8677 BV_FN (v) = fn;
8679 fn_original = THUNK_TARGET (fn);
8682 /* If the only definition of this function signature along our
8683 primary base chain is from a lost primary, this vtable slot will
8684 never be used, so just zero it out. This is important to avoid
8685 requiring extra thunks which cannot be generated with the function.
8687 We first check this in update_vtable_entry_for_fn, so we handle
8688 restored primary bases properly; we also need to do it here so we
8689 zero out unused slots in ctor vtables, rather than filling them
8690 with erroneous values (though harmless, apart from relocation
8691 costs). */
8692 if (BV_LOST_PRIMARY (v))
8693 init = size_zero_node;
8695 if (! init)
8697 /* Pull the offset for `this', and the function to call, out of
8698 the list. */
8699 delta = BV_DELTA (v);
8700 vcall_index = BV_VCALL_INDEX (v);
8702 gcc_assert (TREE_CODE (delta) == INTEGER_CST);
8703 gcc_assert (TREE_CODE (fn) == FUNCTION_DECL);
8705 /* You can't call an abstract virtual function; it's abstract.
8706 So, we replace these functions with __pure_virtual. */
8707 if (DECL_PURE_VIRTUAL_P (fn_original))
8709 fn = abort_fndecl;
8710 if (!TARGET_VTABLE_USES_DESCRIPTORS)
8712 if (abort_fndecl_addr == NULL)
8713 abort_fndecl_addr
8714 = fold_convert (vfunc_ptr_type_node,
8715 build_fold_addr_expr (fn));
8716 init = abort_fndecl_addr;
8719 /* Likewise for deleted virtuals. */
8720 else if (DECL_DELETED_FN (fn_original))
8722 fn = get_identifier ("__cxa_deleted_virtual");
8723 if (!get_global_value_if_present (fn, &fn))
8724 fn = push_library_fn (fn, (build_function_type_list
8725 (void_type_node, NULL_TREE)),
8726 NULL_TREE);
8727 if (!TARGET_VTABLE_USES_DESCRIPTORS)
8728 init = fold_convert (vfunc_ptr_type_node,
8729 build_fold_addr_expr (fn));
8731 else
8733 if (!integer_zerop (delta) || vcall_index)
8735 fn = make_thunk (fn, /*this_adjusting=*/1, delta, vcall_index);
8736 if (!DECL_NAME (fn))
8737 finish_thunk (fn);
8739 /* Take the address of the function, considering it to be of an
8740 appropriate generic type. */
8741 if (!TARGET_VTABLE_USES_DESCRIPTORS)
8742 init = fold_convert (vfunc_ptr_type_node,
8743 build_fold_addr_expr (fn));
8747 /* And add it to the chain of initializers. */
8748 if (TARGET_VTABLE_USES_DESCRIPTORS)
8750 int i;
8751 if (init == size_zero_node)
8752 for (i = 0; i < TARGET_VTABLE_USES_DESCRIPTORS; ++i)
8753 CONSTRUCTOR_APPEND_ELT (*inits, NULL_TREE, init);
8754 else
8755 for (i = 0; i < TARGET_VTABLE_USES_DESCRIPTORS; ++i)
8757 tree fdesc = build2 (FDESC_EXPR, vfunc_ptr_type_node,
8758 fn, build_int_cst (NULL_TREE, i));
8759 TREE_CONSTANT (fdesc) = 1;
8761 CONSTRUCTOR_APPEND_ELT (*inits, NULL_TREE, fdesc);
8764 else
8765 CONSTRUCTOR_APPEND_ELT (*inits, NULL_TREE, init);
8769 /* Adds to vid->inits the initializers for the vbase and vcall
8770 offsets in BINFO, which is in the hierarchy dominated by T. */
8772 static void
8773 build_vcall_and_vbase_vtbl_entries (tree binfo, vtbl_init_data* vid)
8775 tree b;
8777 /* If this is a derived class, we must first create entries
8778 corresponding to the primary base class. */
8779 b = get_primary_binfo (binfo);
8780 if (b)
8781 build_vcall_and_vbase_vtbl_entries (b, vid);
8783 /* Add the vbase entries for this base. */
8784 build_vbase_offset_vtbl_entries (binfo, vid);
8785 /* Add the vcall entries for this base. */
8786 build_vcall_offset_vtbl_entries (binfo, vid);
8789 /* Returns the initializers for the vbase offset entries in the vtable
8790 for BINFO (which is part of the class hierarchy dominated by T), in
8791 reverse order. VBASE_OFFSET_INDEX gives the vtable index
8792 where the next vbase offset will go. */
8794 static void
8795 build_vbase_offset_vtbl_entries (tree binfo, vtbl_init_data* vid)
8797 tree vbase;
8798 tree t;
8799 tree non_primary_binfo;
8801 /* If there are no virtual baseclasses, then there is nothing to
8802 do. */
8803 if (!CLASSTYPE_VBASECLASSES (BINFO_TYPE (binfo)))
8804 return;
8806 t = vid->derived;
8808 /* We might be a primary base class. Go up the inheritance hierarchy
8809 until we find the most derived class of which we are a primary base:
8810 it is the offset of that which we need to use. */
8811 non_primary_binfo = binfo;
8812 while (BINFO_INHERITANCE_CHAIN (non_primary_binfo))
8814 tree b;
8816 /* If we have reached a virtual base, then it must be a primary
8817 base (possibly multi-level) of vid->binfo, or we wouldn't
8818 have called build_vcall_and_vbase_vtbl_entries for it. But it
8819 might be a lost primary, so just skip down to vid->binfo. */
8820 if (BINFO_VIRTUAL_P (non_primary_binfo))
8822 non_primary_binfo = vid->binfo;
8823 break;
8826 b = BINFO_INHERITANCE_CHAIN (non_primary_binfo);
8827 if (get_primary_binfo (b) != non_primary_binfo)
8828 break;
8829 non_primary_binfo = b;
8832 /* Go through the virtual bases, adding the offsets. */
8833 for (vbase = TYPE_BINFO (BINFO_TYPE (binfo));
8834 vbase;
8835 vbase = TREE_CHAIN (vbase))
8837 tree b;
8838 tree delta;
8840 if (!BINFO_VIRTUAL_P (vbase))
8841 continue;
8843 /* Find the instance of this virtual base in the complete
8844 object. */
8845 b = copied_binfo (vbase, binfo);
8847 /* If we've already got an offset for this virtual base, we
8848 don't need another one. */
8849 if (BINFO_VTABLE_PATH_MARKED (b))
8850 continue;
8851 BINFO_VTABLE_PATH_MARKED (b) = 1;
8853 /* Figure out where we can find this vbase offset. */
8854 delta = size_binop (MULT_EXPR,
8855 vid->index,
8856 convert (ssizetype,
8857 TYPE_SIZE_UNIT (vtable_entry_type)));
8858 if (vid->primary_vtbl_p)
8859 BINFO_VPTR_FIELD (b) = delta;
8861 if (binfo != TYPE_BINFO (t))
8862 /* The vbase offset had better be the same. */
8863 gcc_assert (tree_int_cst_equal (delta, BINFO_VPTR_FIELD (vbase)));
8865 /* The next vbase will come at a more negative offset. */
8866 vid->index = size_binop (MINUS_EXPR, vid->index,
8867 ssize_int (TARGET_VTABLE_DATA_ENTRY_DISTANCE));
8869 /* The initializer is the delta from BINFO to this virtual base.
8870 The vbase offsets go in reverse inheritance-graph order, and
8871 we are walking in inheritance graph order so these end up in
8872 the right order. */
8873 delta = size_diffop_loc (input_location,
8874 BINFO_OFFSET (b), BINFO_OFFSET (non_primary_binfo));
8876 CONSTRUCTOR_APPEND_ELT (vid->inits, NULL_TREE,
8877 fold_build1_loc (input_location, NOP_EXPR,
8878 vtable_entry_type, delta));
8882 /* Adds the initializers for the vcall offset entries in the vtable
8883 for BINFO (which is part of the class hierarchy dominated by VID->DERIVED)
8884 to VID->INITS. */
8886 static void
8887 build_vcall_offset_vtbl_entries (tree binfo, vtbl_init_data* vid)
8889 /* We only need these entries if this base is a virtual base. We
8890 compute the indices -- but do not add to the vtable -- when
8891 building the main vtable for a class. */
8892 if (binfo == TYPE_BINFO (vid->derived)
8893 || (BINFO_VIRTUAL_P (binfo)
8894 /* If BINFO is RTTI_BINFO, then (since BINFO does not
8895 correspond to VID->DERIVED), we are building a primary
8896 construction virtual table. Since this is a primary
8897 virtual table, we do not need the vcall offsets for
8898 BINFO. */
8899 && binfo != vid->rtti_binfo))
8901 /* We need a vcall offset for each of the virtual functions in this
8902 vtable. For example:
8904 class A { virtual void f (); };
8905 class B1 : virtual public A { virtual void f (); };
8906 class B2 : virtual public A { virtual void f (); };
8907 class C: public B1, public B2 { virtual void f (); };
8909 A C object has a primary base of B1, which has a primary base of A. A
8910 C also has a secondary base of B2, which no longer has a primary base
8911 of A. So the B2-in-C construction vtable needs a secondary vtable for
8912 A, which will adjust the A* to a B2* to call f. We have no way of
8913 knowing what (or even whether) this offset will be when we define B2,
8914 so we store this "vcall offset" in the A sub-vtable and look it up in
8915 a "virtual thunk" for B2::f.
8917 We need entries for all the functions in our primary vtable and
8918 in our non-virtual bases' secondary vtables. */
8919 vid->vbase = binfo;
8920 /* If we are just computing the vcall indices -- but do not need
8921 the actual entries -- not that. */
8922 if (!BINFO_VIRTUAL_P (binfo))
8923 vid->generate_vcall_entries = false;
8924 /* Now, walk through the non-virtual bases, adding vcall offsets. */
8925 add_vcall_offset_vtbl_entries_r (binfo, vid);
8929 /* Build vcall offsets, starting with those for BINFO. */
8931 static void
8932 add_vcall_offset_vtbl_entries_r (tree binfo, vtbl_init_data* vid)
8934 int i;
8935 tree primary_binfo;
8936 tree base_binfo;
8938 /* Don't walk into virtual bases -- except, of course, for the
8939 virtual base for which we are building vcall offsets. Any
8940 primary virtual base will have already had its offsets generated
8941 through the recursion in build_vcall_and_vbase_vtbl_entries. */
8942 if (BINFO_VIRTUAL_P (binfo) && vid->vbase != binfo)
8943 return;
8945 /* If BINFO has a primary base, process it first. */
8946 primary_binfo = get_primary_binfo (binfo);
8947 if (primary_binfo)
8948 add_vcall_offset_vtbl_entries_r (primary_binfo, vid);
8950 /* Add BINFO itself to the list. */
8951 add_vcall_offset_vtbl_entries_1 (binfo, vid);
8953 /* Scan the non-primary bases of BINFO. */
8954 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
8955 if (base_binfo != primary_binfo)
8956 add_vcall_offset_vtbl_entries_r (base_binfo, vid);
8959 /* Called from build_vcall_offset_vtbl_entries_r. */
8961 static void
8962 add_vcall_offset_vtbl_entries_1 (tree binfo, vtbl_init_data* vid)
8964 /* Make entries for the rest of the virtuals. */
8965 if (abi_version_at_least (2))
8967 tree orig_fn;
8969 /* The ABI requires that the methods be processed in declaration
8970 order. G++ 3.2 used the order in the vtable. */
8971 for (orig_fn = TYPE_METHODS (BINFO_TYPE (binfo));
8972 orig_fn;
8973 orig_fn = DECL_CHAIN (orig_fn))
8974 if (DECL_VINDEX (orig_fn))
8975 add_vcall_offset (orig_fn, binfo, vid);
8977 else
8979 tree derived_virtuals;
8980 tree base_virtuals;
8981 tree orig_virtuals;
8982 /* If BINFO is a primary base, the most derived class which has
8983 BINFO as a primary base; otherwise, just BINFO. */
8984 tree non_primary_binfo;
8986 /* We might be a primary base class. Go up the inheritance hierarchy
8987 until we find the most derived class of which we are a primary base:
8988 it is the BINFO_VIRTUALS there that we need to consider. */
8989 non_primary_binfo = binfo;
8990 while (BINFO_INHERITANCE_CHAIN (non_primary_binfo))
8992 tree b;
8994 /* If we have reached a virtual base, then it must be vid->vbase,
8995 because we ignore other virtual bases in
8996 add_vcall_offset_vtbl_entries_r. In turn, it must be a primary
8997 base (possibly multi-level) of vid->binfo, or we wouldn't
8998 have called build_vcall_and_vbase_vtbl_entries for it. But it
8999 might be a lost primary, so just skip down to vid->binfo. */
9000 if (BINFO_VIRTUAL_P (non_primary_binfo))
9002 gcc_assert (non_primary_binfo == vid->vbase);
9003 non_primary_binfo = vid->binfo;
9004 break;
9007 b = BINFO_INHERITANCE_CHAIN (non_primary_binfo);
9008 if (get_primary_binfo (b) != non_primary_binfo)
9009 break;
9010 non_primary_binfo = b;
9013 if (vid->ctor_vtbl_p)
9014 /* For a ctor vtable we need the equivalent binfo within the hierarchy
9015 where rtti_binfo is the most derived type. */
9016 non_primary_binfo
9017 = original_binfo (non_primary_binfo, vid->rtti_binfo);
9019 for (base_virtuals = BINFO_VIRTUALS (binfo),
9020 derived_virtuals = BINFO_VIRTUALS (non_primary_binfo),
9021 orig_virtuals = BINFO_VIRTUALS (TYPE_BINFO (BINFO_TYPE (binfo)));
9022 base_virtuals;
9023 base_virtuals = TREE_CHAIN (base_virtuals),
9024 derived_virtuals = TREE_CHAIN (derived_virtuals),
9025 orig_virtuals = TREE_CHAIN (orig_virtuals))
9027 tree orig_fn;
9029 /* Find the declaration that originally caused this function to
9030 be present in BINFO_TYPE (binfo). */
9031 orig_fn = BV_FN (orig_virtuals);
9033 /* When processing BINFO, we only want to generate vcall slots for
9034 function slots introduced in BINFO. So don't try to generate
9035 one if the function isn't even defined in BINFO. */
9036 if (!SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), DECL_CONTEXT (orig_fn)))
9037 continue;
9039 add_vcall_offset (orig_fn, binfo, vid);
9044 /* Add a vcall offset entry for ORIG_FN to the vtable. */
9046 static void
9047 add_vcall_offset (tree orig_fn, tree binfo, vtbl_init_data *vid)
9049 size_t i;
9050 tree vcall_offset;
9051 tree derived_entry;
9053 /* If there is already an entry for a function with the same
9054 signature as FN, then we do not need a second vcall offset.
9055 Check the list of functions already present in the derived
9056 class vtable. */
9057 FOR_EACH_VEC_SAFE_ELT (vid->fns, i, derived_entry)
9059 if (same_signature_p (derived_entry, orig_fn)
9060 /* We only use one vcall offset for virtual destructors,
9061 even though there are two virtual table entries. */
9062 || (DECL_DESTRUCTOR_P (derived_entry)
9063 && DECL_DESTRUCTOR_P (orig_fn)))
9064 return;
9067 /* If we are building these vcall offsets as part of building
9068 the vtable for the most derived class, remember the vcall
9069 offset. */
9070 if (vid->binfo == TYPE_BINFO (vid->derived))
9072 tree_pair_s elt = {orig_fn, vid->index};
9073 vec_safe_push (CLASSTYPE_VCALL_INDICES (vid->derived), elt);
9076 /* The next vcall offset will be found at a more negative
9077 offset. */
9078 vid->index = size_binop (MINUS_EXPR, vid->index,
9079 ssize_int (TARGET_VTABLE_DATA_ENTRY_DISTANCE));
9081 /* Keep track of this function. */
9082 vec_safe_push (vid->fns, orig_fn);
9084 if (vid->generate_vcall_entries)
9086 tree base;
9087 tree fn;
9089 /* Find the overriding function. */
9090 fn = find_final_overrider (vid->rtti_binfo, binfo, orig_fn);
9091 if (fn == error_mark_node)
9092 vcall_offset = build_zero_cst (vtable_entry_type);
9093 else
9095 base = TREE_VALUE (fn);
9097 /* The vbase we're working on is a primary base of
9098 vid->binfo. But it might be a lost primary, so its
9099 BINFO_OFFSET might be wrong, so we just use the
9100 BINFO_OFFSET from vid->binfo. */
9101 vcall_offset = size_diffop_loc (input_location,
9102 BINFO_OFFSET (base),
9103 BINFO_OFFSET (vid->binfo));
9104 vcall_offset = fold_build1_loc (input_location,
9105 NOP_EXPR, vtable_entry_type,
9106 vcall_offset);
9108 /* Add the initializer to the vtable. */
9109 CONSTRUCTOR_APPEND_ELT (vid->inits, NULL_TREE, vcall_offset);
9113 /* Return vtbl initializers for the RTTI entries corresponding to the
9114 BINFO's vtable. The RTTI entries should indicate the object given
9115 by VID->rtti_binfo. */
9117 static void
9118 build_rtti_vtbl_entries (tree binfo, vtbl_init_data* vid)
9120 tree b;
9121 tree t;
9122 tree offset;
9123 tree decl;
9124 tree init;
9126 t = BINFO_TYPE (vid->rtti_binfo);
9128 /* To find the complete object, we will first convert to our most
9129 primary base, and then add the offset in the vtbl to that value. */
9130 b = binfo;
9131 while (CLASSTYPE_HAS_PRIMARY_BASE_P (BINFO_TYPE (b))
9132 && !BINFO_LOST_PRIMARY_P (b))
9134 tree primary_base;
9136 primary_base = get_primary_binfo (b);
9137 gcc_assert (BINFO_PRIMARY_P (primary_base)
9138 && BINFO_INHERITANCE_CHAIN (primary_base) == b);
9139 b = primary_base;
9141 offset = size_diffop_loc (input_location,
9142 BINFO_OFFSET (vid->rtti_binfo), BINFO_OFFSET (b));
9144 /* The second entry is the address of the typeinfo object. */
9145 if (flag_rtti)
9146 decl = build_address (get_tinfo_decl (t));
9147 else
9148 decl = integer_zero_node;
9150 /* Convert the declaration to a type that can be stored in the
9151 vtable. */
9152 init = build_nop (vfunc_ptr_type_node, decl);
9153 CONSTRUCTOR_APPEND_ELT (vid->inits, NULL_TREE, init);
9155 /* Add the offset-to-top entry. It comes earlier in the vtable than
9156 the typeinfo entry. Convert the offset to look like a
9157 function pointer, so that we can put it in the vtable. */
9158 init = build_nop (vfunc_ptr_type_node, offset);
9159 CONSTRUCTOR_APPEND_ELT (vid->inits, NULL_TREE, init);
9162 /* TRUE iff TYPE is uniquely derived from PARENT. Ignores
9163 accessibility. */
9165 bool
9166 uniquely_derived_from_p (tree parent, tree type)
9168 tree base = lookup_base (type, parent, ba_unique, NULL, tf_none);
9169 return base && base != error_mark_node;
9172 /* TRUE iff TYPE is publicly & uniquely derived from PARENT. */
9174 bool
9175 publicly_uniquely_derived_p (tree parent, tree type)
9177 tree base = lookup_base (type, parent, ba_ignore_scope | ba_check,
9178 NULL, tf_none);
9179 return base && base != error_mark_node;
9182 #include "gt-cp-class.h"